Repository navigation
[codegen] The MASD table space: schema, row and COPY templates - #195
Conversation
Add the model types masd.codegen.entity and masd.codegen.schema, a small context builder for them, and the masd.sql and masd.cpp technical spaces of the MASD library, so a project can generate plain PostgreSQL tables, their row types and COPY writers from models without ORE Studio's tenancy, history or libraries. - compass.model reads both types into typed structs. An unknown property, profile, profile feature, SQL section or table header is a model_error naming what the format has, never a silent skip. The format gains :member:, :enum:, key_kind owner, written_by writer, a ** Unique table, :generated: and a schema model's * Functions. - The graph reads masd.*.org nodes beside the product's own; model_units resolves each facet under its own root, so ORE Studio's addresses are resolved exactly as before. - share/masd/templates holds the facets masd.sql.schema, masd.cpp.row and masd.cpp.copy (off by default) and their archetype pages, each with its Mustache source under * Template, and the tangled .mustache files. - Profiles content-addressed-row, owned-child-row, activity-record, environment-state and bookkeeping are pages of projects/modeling, with the registry of custom C++ types and compass's project library. ORE Studio's compass diff on a scratch copy of main is the same as 0.0.13's, as configured and with @Compass layered; see evidence/masd_schema_ore_diff.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mcraveiro
left a comment
There was a problem hiding this comment.
Independent review of #195 at b21f02a. The pull request merged while this review ran, so every finding below is a follow-up, either in #196 or in its own pull request.
Nothing here is must-fix. I found no defect in what compass generates today. The findings are places where the loader or the generator stays silent when the claim is that it never does, and places where the context builder will cost the next person more than it should.
What I ran
- Built
compass,compass.model.tests,compass.codegen.testsandcompass.db.testsfrom #196's branch (which contains this one) withlinux-clang-release-ninja. compass.model.tests "[masd]": 22 assertions in 5 cases pass.compass.codegen.tests "[masd]": 28 assertions in 5 cases pass.g++ -fsyntax-only -Wall -Wextra -Wmissing-field-initializersovermasd_context.cpp,masd_entity.cpp,generate.cppand both new test files: no warning. libc++ is not installed on this machine, so libc++ and MSVC are by inspection only; I found nothing there.- I did not rerun the ORE Studio byte-identity script. See finding 8 for what it can and cannot show.
- Each mutation below was made in the worktree, run through
compass generate, and reverted.
Findings
Loader
-
should-fix. A misspelled top-level section is skipped silently.
projects/compass.model/src/ops/masd_entity.cpp:258-290looks up* Flags,* Columns,* Foreign keysand* SQLby name and treats an absent section as empty. The closed list covers**sections under* SQL(line 46) but not the top level. I renamed* Foreign keysto* Foreign keyincompass.db.section.org.compass generateexited 0, printed no diagnostic, and rewroteschema.sqlwithhash bytea NOT NULLin place ofhash bytea NOT NULL REFERENCES corpus.document ON DELETE CASCADE.compass checkreported nothing for the model. This contradicts "It is never skipped silently" in the description. In compass the catalog test would catch it later, but a product without that test loses the foreign key. Fix: give the entity and schema models a closed list of top-level sections that carry data (Flags,Columns,Foreign keys,SQL,Schemas,Functions) plus the prose sections the document type allows (C++,Notes,See also), and raisemodel_errorfor any other heading. Add a test case beside "a property the format does not have is a diagnostic". -
should-fix. A table whose schema the schema model does not list disappears from
schema.sqlwith exit 0.masd_context.cpp:341-346keeps onlye.schema == schema, andmasd_context.hpp:48-49documents the omission as intended for the bookkeeping schema. I changed:schema: corpusto:schema: corpsuincompass.db.section.org.compass generateexited 0 and droppedcorpus.sectionfromschema.sql. The only signal was the generic "generate deleted 3 lines its output lacks" warning. The intended case and the typo are indistinguishable. Fix: let the* Schemastable say which schemas the script does not create (for example acreated_bycolumn withrunnerforcompass), and make an entity whose schema is in neither list amodel_errornaming the entity. -
should-fix. Errors raised by the context builder lose the model's path and the table.
generate.cpp:660catchesmops::model_erroronly, whilemasd_context.cpp:119and:357throwstd::runtime_error. I set a column's:type:touuidwith no:cpp_type:. The whole run aborted withcompass: codegen: the column 'text' is of SQL type uuid, which has no C++ type; state one with :cpp_type:. Fourteen models have columns, and the message names neither the model nor the table. A loader error for the same model printsprojects/compass.db/modeling/compass.db.section.org: the column .... Fix: throwmodel_errorfrombase_typeandordered(both are statements about the model), or catchstd::runtime_erroratgenerate.cpp:660and prefix the path as the loader branch does. Add the entity name to thebase_typemessage. -
nit.
:computed: writerand:written_by: writerare two spellings of one fact.masd_entity.cpp:171-181accepts both, and rejects:computed: databasewith "it is writer". One model usescomputed(compass.db.section.org), the others usewritten_by. A closed format should have one spelling. Fix: dropcomputedfromcolumn_keysand change the one model. Also acceptwritten_by: rowexplicitly, since it is the default the error message does not mention. -
nit. Several values pass the loader and fail later, far from the model.
:values:entries become C++ enumerators and are not checked to be identifiers, soin-progressordefaultloads and then fails to compile (masd_entity.cpp:166). An enum column's:default:is not checked against:values:, soinitializer(masd_context.cpp:142-147) can emit an enumerator that does not exist. An index row with an emptynameorcolumnsloads (masd_entity.cpp:221-231) and fails in PostgreSQL. Fix: validate all three in the loader, where the message can name the model.
Context builder
masd_context.cpp is flat and readable. I do not call it spaghetti. The findings below are about where knowledge sits, not about control flow.
-
should-fix. Three predicates encode one classification.
in_row,in_copyandis_parameter(masd_context.cpp:97-109) each re-derive from(writer, key)whether a column is a row member, a writer parameter, or neither.is_parameteris defined as "written by the row, not in the row, not identity", andin_copyis the union of the other two. A newkey_kindorwritten_byvalue has to be threaded through all three, and nothing forces that. Fix: one function returning anenum class column_role { member, parameter, none }with aswitchoverkey_kindthat has nodefault, so the compiler flags a new kind.add_copy, the member loop and the parameter loop then switch on the role. -
should-fix. The masd branch in
plan_modelis bolted on, with a copy of the row loop.generate.cpp:653-664returns early for the two masd types, andadd_rows(:557-571) duplicates the loop at:674-687except fortemplate_context. The two will drift. Each model is also read twice:load_masd_entity(masd_entity.cpp:343) parses the whole model to copy five strings into the value tables, thenmasd_contextparses it again, and a schema model re-parses every sibling entity that the same run already parsed. Fix: make the existing loop calladd_rowswith an optional per-template context hook, and cache the parsedmasd_entityby path for the run (cachealready exists for families). -
should-fix. The ORE Studio evidence cannot tell "identical" from "both runs failed".
evidence/masd_schema_ore_diff.sh:55-59runscompass diff --files ... || trueand compares the two outputs. "0 files, same" is also what two runs that abort on the same configuration error produce. The product owner rule is byte identity, and this is the only evidence for it. Fix: record each run's exit status and require 0, and print a positive count from stderr's summary line (units run bycodegen) so the reader sees that codegen ran. Rerun and attach the output. -
nit. Hidden assumptions in the builder and the facets.
masd_context.cpp:381-386and:413-415rebuild<component>/types/<entity>_row.hppand<component>/io/<entity>_copy.hppas strings, while the archetype pages state the same layout in#+output:. A change to one does not change the other. Derive the include path and guard from the unit's resolved output path.- The header is always
<entity>_row.hpp, even when:row_type:renames the struct, soenvironment_row.hppdefinesenvironment. custom_typescomes fromcompass.model/ops/typescript.hpp(masd_context.hpp:25). A C++ row generator should not depend on a header named for TypeScript. Move the alias to its own header.apply_profilehard-codesprojects/modeling/variability_<profile>.org(masd_entity.cpp:131-133). That is ORE Studio's layout. It is fine today, but it belongs next to the other path conventions, not in the entity loader.- The COPY facet needs the component to provide
ops::copy_writerwith afieldoverload per member type.masd.cpp.copy.orgnames the header but not the overload set. State it there.
-
nit.
mentionsspecial-cases names that end inint.masd_context.cpp:82treats any name ending inintas a prefix, to makestd::intmatchstd::int32_t. The same function matches custom types (:170), so a registeredgeo::pointwould also matchgeo::pointer. Fix: carry an explicitprefixflag instd_headersinstead of inferring it from spelling. -
nit. An index's
opclassis applied to every column.masd_context.cpp:323-324. A two-column index with one non-default operator class cannot be stated, and stating it produces wrong DDL without an error. Fix: rejectopclasson a multi-column index until the format can say which column it belongs to. -
nit. Presentation leaks into the builder.
wrapped(columns, 68)(masd_context.cpp:187-198,:246) wraps the COPY column list for the C++ template's line width. The schema type tablesql_to_cppandstd_headers(:46-68) are data that the custom type registry already shows how to keep in the library. Neither is urgent. -
nit. Three lists of model type names.
model_type_of(load.cpp:32-43) special-cases the two masd names and then excludes them from theores.codegen.loop,name_ofreturnsmasd_entity, andmanifest.cpp:29-36keeps a third list of full type strings. Fix: one table of{full type string, model_type, context key}that all three read.
Tests
-
should-fix. The loader's refusals are tested for three of its closed lists.
ops_load_masd_entity_tests.cppcovers an unknown column property, an unknown flag, a badwritten_by, an unknown profile, an unknown profile feature and a missing column. It does not cover an unknown* SQLsection, an unknown index, check or unique table header, an unknown foreign key property, a badon_delete, a duplicate column, akey_kindwithoutprimary_key,valueson a non-text column, or any refusal of the schema model. Each is oneSECTION. Findings 1 and 2 need their own cases. -
nit. The context tests assert substrings of rendered text. They would not notice an extra definition, a reordered column or a stray index. One golden comparison of a whole rendered
schema.sqland row header for the fixture would. #196's catalog test covers this for compass, so this matters for the library's own tests only.
First of two stacked pull requests for the task "Generate compass's schema and row types". This one adds the generator; the second turns it on for compass.
What changes
compass.modelreadsmasd.codegen.entityandmasd.codegen.schemainto typed structs (types/masd_entity.hpp). Every property, profile, profile feature,* SQLsection and table header has a closed list. Anything else is amodel_errorthat names what the format has, andcompass generatereports it with the model's path. It is never skipped silently.:member:,:enum:,key_kind: owner,written_by: writer, a** Uniquetable,:generated:, and a schema model's* Functions.masd.*.orgnodes beside the product's own.model_unitsresolves each facet under its own root, so ORE Studio's addresses resolve exactly as before.share/masd/templatesgains the facetsmasd.sql.schema,masd.cpp.rowandmasd.cpp.copy(off by default). Each archetype page holds its Mustache source under* Template, and the tangled.mustachefiles sit beside them.compass.codegen/ops/masd_context.cppderives the row members and their types, the includes, enumerations, COPY parameters and fields, column definitions, constraints and indexes. It orders tables so each one follows the tables it references.projects/modeling/variability_*.org, wherephysical_space_overridesreads them. This PR also adds the custom-type registry and compass's project library: the catalogue, the licence and the modelines.Nothing in compass is generated yet.
.compass.orgis unchanged here becausecheck --ratchetruns the head's configuration over the base, and the base needs the library before codegen can run on it.Evidence
compass.model.tests "[masd]"(5 cases) andcompass.codegen.tests "[masd]"(5 cases). The codegen tests render the real tangled templates.cteston the stacked branch: 1,685 of 1,685 pass.evidence/masd_schema_ore_diff.shrancompass diffon a scratch copy of ORE Studio main, with 0.0.13 and with this binary. Output was the same (0 files) as ORE Studio configures codegen, and also withlibrary=@compass,projects/ores.codegen/library, which puts the masd nodes in its graph.compass check --ratchet origin/main --no-blame: 0 new.🤖 Generated with Claude Code