Skip to content

Read docs through one internal compiler (no output change) - #6484

Merged
josephfarina merged 3 commits into
mainfrom
docs/compiled-topics
Sep 24, 2026
Merged

josephfarina merged 3 commits into
mainfrom
docs/compiled-topics

Conversation

@josephfarina

@josephfarina josephfarina commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Follows #6471, now merged. This branch is rebased onto main, and its diff is only the compiler.

Maintainer impact

astryx docs, astryx doctor and astryx search each drove doc loading themselves: overlay loading, extension merge, key stamping and token-reference resolution were called from four places, each in its own order. Every new reader (the docs sites, MCP, the docs graph) would have added another copy. This puts those steps in one internal compiler, and every current reader consumes its output.

Intended invariant

Every docs() response (docs.list, docs.detail, docs.index, docs.detail.section), every docs error, astryx doctor's docs checks and astryx search results stay byte-identical. The compiler is internal: no new package export, and docs() stays the public way in.

Change

  • foundation/doc-compiler/compile.mjs — lowerReferenceTopic overlays each file, merges extensions in order and stamps section keys; linkReferenceTopic / linkReferenceSection resolve token references against the target topic lowered for the same language. The output is a schema-versioned node in plain JSON with a stage (lowered, then linked). A section's authored title travels as data (sourceTitles), not a symbol. An authored value JSON cannot hold (a function, a Date, undefined) takes its JSON form when the node is built, which is what --json output always showed; no shipped doc has one.
  • foundation/doc-compiler/lenses.mjs — the response shapes, read off compiled nodes. Every view is a fresh copy.
  • foundation/doc-compiler/ir.mjs — the sealed parser for a node read back from JSON: plain JSON throughout, typed blocks, package names (not paths) in provenance, and token-reference resolutions exactly when the node is linked. It returns the value as given, so key order survives.
  • api/docs/_adapter.mjs loads authored files into compiler input and memoizes lowered nodes per catalog. Memoized nodes are frozen, since every read of the catalog shares them. A file that fails to load is recorded, and the compiler throws it in the order a reader meets it, so error messages are unchanged.
  • The detail, index and section leaves, doctor's docs check and search read compiled nodes. The index and section lookup read the lowered node, and a section read links only its own section, so a broken reference elsewhere still cannot fail it.
  • Only the docs adapter and leaves may import the compiler's merge, key and link functions: an ESLint no-restricted-imports rule for api/ and clients/, plus a test that parses every file's imports (aliases, namespace imports, re-exports and dynamic import()).

Evidence

  • Before: the same load, merge and link steps were reachable from four call sites.
  • After, compared against the previous head:
    • 16,556 in-process reads byte-for-byte identical, across this repo and three consumer projects (topic extensions, translation overlays, an overlay that throws, a provider-ID conflict): 516 topic reads and 516 section indexes in every language option; 12,900 section reads by key, exact title and upper-cased title; 2,580 partial, ambiguous, blank and unknown section queries plus unknown-topic errors; doctor's docs checks and 28 searches.
    • 1,122 astryx docs commands identical on stdout, stderr and exit code: text, --json, --index, --lang zh|dense, every section by key, and errors.
  • Every shipped topic, in every language it ships, survives a JSON round trip: the read-back node validates and gives identical responses.
  • Mutation checks: 29 mutations over three rounds, and each one fails a test except one. That one removes the post-merge duplicate-key check, which no input can reach: the base doc and every extension are each validated for unique section keys before merging, and merge replaces the section that already holds an extension section's key or authored title, keeping that section's id, and appends only when the key is absent. So by induction over extensions, no merge can leave two sections under one key. The check stays as a tripwire for a future change to merge.

Scope

  • No intended public API, product behavior, visual direction, or policy change.
  • Product changes discovered during the work were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

  • Full CLI suite passes (266 files, 4,195 tests); new tests cover lowering, linking, the node contract, the lenses, view isolation, and the import rule.
  • tsc strict / api-dts / authoring-contract, eslint, check-changesets / cli-structure / sync / knowledge, README check.
  • Rebased onto main after Add typed documentation graph contracts #6471 merged: the three commits replayed without conflicts, and every file they change is byte-identical to the head the evidence above was run on.

Next in this stack: the other doc kinds (components, hooks, pages), then list and search from one cached bundle.

@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Sep 23, 2026 11:54pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 23, 2026
@astracat-bot

astracat-bot Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Sep 24, 2026, 12:07 AM UTC

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a clean internal refactor: all reference-topic docs reads now go through one compiler, and I verified the behavior-preserving points (error order and messages, overlay and merge order, single-level reference linking, per-section isolation) alongside passing suites for the compiler, docs readers, doctor, and search.

No action needed from the contributor. Note it is stacked and should land after its base.

[Automated review]

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a clean internal refactor: all reference-topic docs reads now go through one compiler, and I verified the behavior-preserving points (error order and messages, overlay and merge order, single-level reference linking, per-section isolation) alongside passing suites for the compiler, docs readers, doctor, and search.

No action needed from the contributor. Note it is stacked and should land after its base.

[Automated review]

@josephfarina
josephfarina changed the base branch from docs/wave-1-typed-graph-contracts to main September 23, 2026 22:35
@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Sep 23, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a clean internal refactor: all reference-topic docs reads now go through one compiler, and I verified the behavior-preserving points (error order and messages, overlay and merge order, single-level reference linking, per-section isolation) alongside passing suites for the compiler, docs readers, doctor, and search.

No action needed from the contributor. Note it is stacked and should land after its base.

[Automated review]

`astryx docs`, doctor and search each drove doc loading themselves: overlays,
extension merge, key stamping and token-ref resolution were called from four
places in their own order. That now happens in one place. The doc compiler
lowers a topic (overlay each file, merge extensions, stamp keys) and links its
token references, and every reader reads the compiled node through a lens.

Output is unchanged: 16,556 reads (topics, indexes, sections, queries, errors,
doctor's docs checks, search) compared byte-for-byte against the previous head,
across this repo and three consumer projects.
Two extensions replacing one section: the later-configured one wins. A
mutation that merged extensions in reverse survived every existing test.
Three review findings, none of which changed any output:

- A memoized node was shared by every read of one catalog, and the lenses
  handed out its blocks, so editing one result changed the next. Memoized nodes
  are now frozen and every view is a fresh copy.
- A node could hold what JSON cannot (a function, a Date, `undefined`) and the
  parser accepted it. The compiler now builds the node as JSON, and the parser
  rejects anything else, plus untyped blocks, paths in provenance, and
  resolutions that do not match the node's new `stage` (lowered or linked).
- The rule that only the docs adapter and leaves drive the compiler was a regex
  over call names, which an import alias defeated. It is now an ESLint
  `no-restricted-imports` rule, plus a test that parses every import, including
  dynamic `import()`.
@josephfarina
josephfarina merged commit bcf01fe into main Sep 24, 2026
34 checks passed

This branch was successfully deployed

1 active deployment
Preview — 448330c7 Deployed Sep 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant