Skip to content

[codegen] The MASD table space: schema, row and COPY templates - #195

Merged
mcraveiro merged 1 commit into
mainfrom
feature/masd-table-space
Nov 23, 2026
Merged

mcraveiro merged 1 commit into
mainfrom
feature/masd-table-space

Conversation

@mcraveiro

Copy link
Copy Markdown
Member

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

  • Model types. compass.model reads masd.codegen.entity and masd.codegen.schema into typed structs (types/masd_entity.hpp). Every property, profile, profile feature, * SQL section and table header has a closed list. Anything else is a model_error that names what the format has, and compass generate reports it with the model's path. It is never skipped silently.
  • Format additions. These resolve the gaps the merged models' Notes listed: :member:, :enum:, key_kind: owner, written_by: writer, a ** Unique table, :generated:, and a schema model's * Functions.
  • Technical space. 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 resolve exactly as before. share/masd/templates gains the facets masd.sql.schema, masd.cpp.row and masd.cpp.copy (off by default). Each archetype page holds its Mustache source under * Template, and the tangled .mustache files sit beside them.
  • Context builder. compass.codegen/ops/masd_context.cpp derives 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.
  • Profiles and project library. The profiles are pages in projects/modeling/variability_*.org, where physical_space_overrides reads 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.org is unchanged here because check --ratchet runs the head's configuration over the base, and the base needs the library before codegen can run on it.

Evidence

  • New tests: compass.model.tests "[masd]" (5 cases) and compass.codegen.tests "[masd]" (5 cases). The codegen tests render the real tangled templates.
  • Full ctest on the stacked branch: 1,685 of 1,685 pass.
  • ORE Studio stays byte-identical. evidence/masd_schema_ore_diff.sh ran compass diff on 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 with library=@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

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 added a commit that referenced this pull request Nov 23, 2026
Plans, measurements, decisions and limits of both tasks, their pull
requests #195 and #196, and their state in the story's table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mcraveiro
mcraveiro merged commit 7df7ed1 into main Nov 23, 2026
4 checks passed
@mcraveiro
mcraveiro deleted the feature/masd-table-space branch November 23, 2026 17:23

@mcraveiro mcraveiro left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.tests and compass.db.tests from #196's branch (which contains this one) with linux-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-initializers over masd_context.cpp, masd_entity.cpp, generate.cpp and 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

  1. should-fix. A misspelled top-level section is skipped silently. projects/compass.model/src/ops/masd_entity.cpp:258-290 looks up * Flags, * Columns, * Foreign keys and * SQL by 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 keys to * Foreign key in compass.db.section.org. compass generate exited 0, printed no diagnostic, and rewrote schema.sql with hash bytea NOT NULL in place of hash bytea NOT NULL REFERENCES corpus.document ON DELETE CASCADE. compass check reported 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 raise model_error for any other heading. Add a test case beside "a property the format does not have is a diagnostic".

  2. should-fix. A table whose schema the schema model does not list disappears from schema.sql with exit 0. masd_context.cpp:341-346 keeps only e.schema == schema, and masd_context.hpp:48-49 documents the omission as intended for the bookkeeping schema. I changed :schema: corpus to :schema: corpsu in compass.db.section.org. compass generate exited 0 and dropped corpus.section from schema.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 * Schemas table say which schemas the script does not create (for example a created_by column with runner for compass), and make an entity whose schema is in neither list a model_error naming the entity.

  3. should-fix. Errors raised by the context builder lose the model's path and the table. generate.cpp:660 catches mops::model_error only, while masd_context.cpp:119 and :357 throw std::runtime_error. I set a column's :type: to uuid with no :cpp_type:. The whole run aborted with compass: 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 prints projects/compass.db/modeling/compass.db.section.org: the column .... Fix: throw model_error from base_type and ordered (both are statements about the model), or catch std::runtime_error at generate.cpp:660 and prefix the path as the loader branch does. Add the entity name to the base_type message.

  4. nit. :computed: writer and :written_by: writer are two spellings of one fact. masd_entity.cpp:171-181 accepts both, and rejects :computed: database with "it is writer". One model uses computed (compass.db.section.org), the others use written_by. A closed format should have one spelling. Fix: drop computed from column_keys and change the one model. Also accept written_by: row explicitly, since it is the default the error message does not mention.

  5. 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, so in-progress or default loads and then fails to compile (masd_entity.cpp:166). An enum column's :default: is not checked against :values:, so initializer (masd_context.cpp:142-147) can emit an enumerator that does not exist. An index row with an empty name or columns loads (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.

  1. should-fix. Three predicates encode one classification. in_row, in_copy and is_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_parameter is defined as "written by the row, not in the row, not identity", and in_copy is the union of the other two. A new key_kind or written_by value has to be threaded through all three, and nothing forces that. Fix: one function returning an enum class column_role { member, parameter, none } with a switch over key_kind that has no default, so the compiler flags a new kind. add_copy, the member loop and the parameter loop then switch on the role.

  2. should-fix. The masd branch in plan_model is bolted on, with a copy of the row loop. generate.cpp:653-664 returns early for the two masd types, and add_rows (:557-571) duplicates the loop at :674-687 except for template_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, then masd_context parses it again, and a schema model re-parses every sibling entity that the same run already parsed. Fix: make the existing loop call add_rows with an optional per-template context hook, and cache the parsed masd_entity by path for the run (cache already exists for families).

  3. should-fix. The ORE Studio evidence cannot tell "identical" from "both runs failed". evidence/masd_schema_ore_diff.sh:55-59 runs compass diff --files ... || true and 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 by codegen) so the reader sees that codegen ran. Rerun and attach the output.

  4. nit. Hidden assumptions in the builder and the facets.

    • masd_context.cpp:381-386 and :413-415 rebuild <component>/types/<entity>_row.hpp and <component>/io/<entity>_copy.hpp as 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, so environment_row.hpp defines environment.
    • custom_types comes from compass.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_profile hard-codes projects/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_writer with a field overload per member type. masd.cpp.copy.org names the header but not the overload set. State it there.
  5. nit. mentions special-cases names that end in int. masd_context.cpp:82 treats any name ending in int as a prefix, to make std::int match std::int32_t. The same function matches custom types (:170), so a registered geo::point would also match geo::pointer. Fix: carry an explicit prefix flag in std_headers instead of inferring it from spelling.

  6. nit. An index's opclass is 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: reject opclass on a multi-column index until the format can say which column it belongs to.

  7. 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 table sql_to_cpp and std_headers (:46-68) are data that the custom type registry already shows how to keep in the library. Neither is urgent.

  8. 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 the ores.codegen. loop, name_of returns masd_entity, and manifest.cpp:29-36 keeps a third list of full type strings. Fix: one table of {full type string, model_type, context key} that all three read.

Tests

  1. should-fix. The loader's refusals are tested for three of its closed lists. ops_load_masd_entity_tests.cpp covers an unknown column property, an unknown flag, a bad written_by, an unknown profile, an unknown profile feature and a missing column. It does not cover an unknown * SQL section, an unknown index, check or unique table header, an unknown foreign key property, a bad on_delete, a duplicate column, a key_kind without primary_key, values on a non-text column, or any refusal of the schema model. Each is one SECTION. Findings 1 and 2 need their own cases.

  2. 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.sql and row header for the fixture would. #196's catalog test covers this for compass, so this matters for the library's own tests only.

This branch was previously deployed

1 inactive deployment
CI — b21f02a5 Deployed Nov 23, 2026 by mcraveiro via linux-clang-release-ninja #171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant