Skip to content

feat(adf): register modules adf migrate creates in manifest.adf - #348

Merged
stackbilt-admin merged 2 commits into
mainfrom
auto/feature/issue-239
Oct 16, 2026
Merged

stackbilt-admin merged 2 commits into
mainfrom
auto/feature/issue-239

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Closes #239
Closes #241

Summary

charter adf migrate now registers every module it routes content into, in .ai/manifest.adf and in the hybrid CLAUDE.md module index. This follows the approved blueprint for #239 (Q1–Q5 resolved as recommended).

  • @stackbilt/adf: headingToModule returns {module, keyword}. RoutingTrace gains headingKeyword, and classifyElement now sets the trace even without a trigger map (candidateScores is {} in that case).
  • New packages/cli/src/commands/adf-manifest-register.ts: one shared writer, registerManifestModules. It round-trips through parseAdf/formatAdf, matches entries by normalized parsed path, appends only and never rewrites an entry, and creates a missing section in canonical order. Each module gets a status of planned, added, already-registered or skipped (with skipReason). The file also holds the §4 trigger inference (inferTriggers), REGISTER formatting, and the dry-run dedupe across sources. STOPWORDS moved here from adf-suggest, which now imports it.
  • adf-migrate.ts: registration happens after applyMigrationToModule and before writePointerWithRetained. A source other than CLAUDE.md that adds a module re-runs updateModuleIndex('CLAUDE.md'). Each module gets a REGISTER action, and sources[].registrations[] is added to the JSON. I removed the dead weight-promotion block and the empty Helpers banner; adf_migrate_loc is 748 / 792.
  • bootstrap.ts: registerOrphansInManifest (a line splice that was not idempotent) and registerModulesInDefaultLoad are deleted, and orphans go through the shared writer. They get a lowercase stem trigger, so Observability.adf gets observability.
  • Untouched: adfCreate's upsert writer, and the charter adf register hint (bootstrap: warnings tell users to run 'charter adf register', which does not exist #347).
  • Warnings follow the contract templates: manifest missing, DEFAULT_LOAD module listed under ON_DEMAND, section not a list. In text mode they print as Warning: …. In JSON mode they go to stderr so the JSON on stdout stays valid.

Invariants and where they are tested

  • INV-1 idempotency: in adf-migrate-register.test.ts, "a re-run writes nothing and adds no entries" compares a snapshot of the whole tree. Also adf-manifest-register.test.ts "appends entries and is idempotent on a second call", and the bootstrap orphan re-run test.
  • INV-2 manifest before pointer: "registers a new module ON_DEMAND, before the pointer index is written". The CLAUDE.md index from the same source contains infra.adf.
  • INV-3 late index refresh: "refreshes the CLAUDE.md index when a later source adds a module". AGENTS.md creates infra.adf and the CLAUDE.md index lists it.
  • INV-4 bootstrap, no double registration: "registers each migrated module once, and a re-run finds no orphans". A second bootstrap gives no orphan warning and a byte-identical manifest. Also "orphan registration goes through the shared writer".
  • INV-5 append-only: "never merges new triggers into an existing entry", plus the writer unit tests "never rewrites an existing entry, matching by normalized parsed path" and "warns and does not move a DEFAULT_LOAD module listed under ON_DEMAND", and the migrate-level core.adf-under-ON_DEMAND test.
  • INV-6 dry-run writes nothing: "--dry-run writes nothing and reports the planned registration" compares tree snapshots. Also the writer test "writes nothing under dryRun".

Every row of the behavior matrix (§3) has a test in adf-migrate-register.test.ts or adf-manifest-register.test.ts: new module ON_DEMAND/DEFAULT_LOAD, scaffold-listed module, registered module, touched and untouched orphans, core.adf under ON_DEMAND, two sources routing to one module, late index refresh, re-run, new source routing to an existing module, dry-run, multi-source dry-run dedupe, JSON shape, --replace, missing manifest, bootstrap --yes, and --ai-dir. @stackbilt/adf has its own headingKeyword tests in content-classifier.test.ts, and the ALLOWED_SHORT_TOKENS sync with the heading vocabulary is checked by a test.

Notes

  • formatAdf applies the canonical section order, so a manifest that had RULES after ON_DEMAND gets it moved up on the first registration write. The old bootstrap DEFAULT_LOAD writer and adf create already did this; entries are not changed.
  • An unparseable manifest gives the status skipped with manifest-unparseable, but no warning, because the contract's warning envelope has no template for that case.
  • Known gap in multi-source dry-run: a later source's dry-run routing does not see triggers planned by earlier sources. A real run does. This is documented in docs/cli-reference.md.

Test results: pnpm build, pnpm typecheck and pnpm test all ran. 1043 tests pass and 5 fail, the same 5 that already fail on Windows on main (bootstrap #307 fixture, context-refresh git signals, serve-context snapshot, and two precommit-hook auto-tidy tests).

Implemented via constellation-loop against a locked blueprint.

🤖 Generated with Claude Code

Contract amendments (approved during review, round 1):

  • The shared writer inserts text and never reformats the manifest. The blueprint's parseAdf/formatAdf round-trip lost comments, HTML comments and tables, CRLF line endings and section order on hand-edited manifests. Before writing, the result is re-parsed and must equal the old document plus exactly the new entries; otherwise nothing is written.
  • already-registered rows report the manifest's real placement and triggers as spelled there, with triggerSources: []. The TriggerSchema and 1..6 refinements govern only the triggers migrate writes (planned/added).
  • Two warnings were added beyond the contract's templates: the manifest could not be parsed, and the manifest could not be updated without changing its other entries (the re-parse guard). Both skip with manifest-unparseable.
  • Follow-ups: adf migrate: trigger inference keeps generic and numeric tokens #349 (weak generic/numeric triggers, case-insensitive path matching), bootstrap: warnings tell users to run 'charter adf register', which does not exist #347 (adf register message).

#239)

adf migrate now registers every module it routes content into: core.adf
and state.adf under DEFAULT_LOAD, others under ON_DEMAND with triggers
inferred from the migrated headings (RoutingTrace.headingKeyword from
@stackbilt/adf). The manifest is written after the module merge and
before the pointer, so the hybrid CLAUDE.md module index includes the new
entries; a later non-CLAUDE.md source refreshes that index.

One shared append-only writer (adf-manifest-register.ts) replaces
bootstrap's two orphan writers, including the non-idempotent line splice.
This completes the migrate half of #241.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@stackbilt-admin stackbilt-admin 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.

CHANGES_REQUESTED

Adversarial review of #348 against the approved blueprint (diagram.md plus issue-239.contract.ts, Q1–Q5). I built origin/auto/feature/issue-239 @ 5853a52 in a clean worktree. pnpm build passes. vitest run gives 1043 passed and 5 failed, the same 5 known Windows failures listed in the PR body. I also ran manifest round-trip harnesses and bootstrap --yes / doctor end to end.

Required changes

  1. The shared writer destroys hand-edited manifest content. This is blocking, and bootstrap is now more destructive than on main. packages/cli/src/commands/adf-manifest-register.ts:324 (fs.writeFileSync(manifestPath, formatAdf(doc))) rewrites the whole manifest through parseAdf/formatAdf whenever one entry is appended. Migrate now does this automatically, and so does bootstrap's orphan pass, which was a byte-preserving line splice on main. These are the results of registering one module (infra.adf) on realistic manifests:

    • Preamble comment lines are deleted, and a stray duplicate ADF: 0.1 section is appended at the end of the file. Input: # Team manifest: keep RULES last, ping @platform before editing on line 1, then ADF: 0.1. The parser ignores lines before the first header, so the version line is parsed as a section named ADF and re-emitted after formatAdf writes its own version line. I reproduced this end to end with bootstrap --yes and a pre-existing orphan. Output:
      -# Team manifest: keep RULES last, ping @platform before editing
       ADF: 0.1
      +📐 RULES: ...(moved up)
      ...
      +  - backend.adf (Triggers on: backend)
      +  - Observability.adf (Triggers on: observability)
      -📐 RULES: ...
      +ADF: 0.1
      
    • HTML comments and markdown tables in text sections (e.g. CONTEXT) are deleted by normalizeSection in the formatter (stripHtmlComments / stripMarkdownTables). Routing notes <!-- TODO: revisit after Q3 --> plus a 3-row | module | owner | table became Routing notes with nothing else.
    • CRLF files are rewritten as LF, so the whole file churns. Blank lines inside list sections are dropped. - - backend.adf is rewritten. Tab-indented text is reindented. A one-line CUSTOM_THING [advisory]: hello with body text has its inline value moved into the body.
    • Reordering sections into canonical order happens as well (on this repo's own .ai/manifest.adf, SYNC moves above METRICS). That much is acceptable on its own.

    On main, bootstrap's registerOrphansInManifest inserted two lines and left everything else intact, so this PR turns a non-destructive path into a destructive one. Required fix: append at the text level. Find the target section's header line, insert - <entry> after its last list item, use the file's existing EOL, and leave every other byte alone. When the section is missing, append a new section block. As a guard, re-parse the result and check that parseManifest(after) equals parseManifest(before) plus the new entries, and skip with a warning otherwise. Add tests that assert byte-for-byte preservation for a preamble comment, a CONTEXT section with an HTML comment and a table, a CRLF file, and this repo's own manifest (before + inserted lines === after). The current test at adf-manifest-register.test.ts ("creates missing sections in canonical order") locks in the reordering and should be relaxed.

  2. An empty section header is reported as "not a list", so registration is skipped. At adf-manifest-register.ts:309, a manifest containing 📂 ON_DEMAND: with no items (left behind after a user deletes every entry) parses as {type:'text', value:''}. The result is skipped / section-not-list and the warning "ON_DEMAND … is not a list". DEFAULT_LOAD: with no items behaves the same way, which leaves core.adf unregistered and doctor failing. The old splice writer handled this case. Treat an empty text section as an empty list, and add a test.

  3. REGISTER/JSON output for already-registered modules reports inferred triggers that are not in the manifest. Repro: manifest has frontend.adf (Triggers on: React, CSS, UI), and CLAUDE.md has ## React components. Both text and JSON print frontend.adf → ON_DEMAND: react, components [already registered], and the reason says "triggers inferred from 1 heading keyword(s)…". None of those triggers exist or will be written. The same problem affects core.adf when it sits under ON_DEMAND: the line reads core.adf → DEFAULT_LOAD [already registered] while the module is actually under ON_DEMAND. Ruling on judgement call (c): printing a REGISTER line for already-registered modules is correct, because the contract defines the [already registered] suffix. The triggers shown must be the existing entry's, though. At adf-manifest-register.ts:304, for already-registered entries, fill triggers (and triggerSources) from the parsed manifest entry, or report triggers: [] with no inference text in reason. Do not report a plan the writer refused to apply. The core.adf-under-ON_DEMAND case can keep loadPolicy: 'DEFAULT_LOAD' as the classification, since the warning already names the real placement.

  4. An unparseable manifest is skipped silently. This is judgement call (a), and the answer is no, it is not acceptable. At adf-manifest-register.ts:290-291, the contract's own RegistrationStatus doc says skipped means "Nothing written; a warning is emitted". The missing template is a gap in the warning envelope, and silence is the worse reading. As it stands, migrate creates modules, registers none of them, says nothing, and doctor fails later. Emit a warning in the same style, e.g. {manifestPath} could not be parsed ({error}); {count} module(s) were written but not registered, and assert it in the existing skips with manifest-unparseable test.

Non-blocking notes

  • JSON vs text (orchestrator item 2): does not reproduce. I used a fresh tree, the scaffold manifest, and CLAUDE.md with ## Deployment / ## React components / ## Docker images. Text and JSON dry-runs both report infra.adf → ON_DEMAND: deployment, docker, images [planned] with triggerSources: [heading-keyword, heading-keyword, heading-token]. The impl's earlier e2e must have run JSON on a different fixture. No change needed beyond item 3.
  • Trigger inference against §4 conforms: weight ×2 for the keyword only when the heading route chose this module, per-item counting, stopwords plus generic words, a 2-letter allowlist with a sync test, dropping other modules' triggers, cap 6, named seeds first, lowercase stem fallback, and deterministic output. On whether "images" surviving means the lists are too thin: yes. images, components, workflow, release, apps and via all survive as triggers. STOPWORDS lacks via, use, how, when, all, not, and the generic list lacks setup, usage, guide, general, conventions, best, practices, tips, misc. Purely numeric tokens (2024) also pass. Consider a follow-up to widen the lists and drop all-digit tokens. A multi-word trigger owned by another module (dependency graph) does not block its single-word parts. Separately, a stem fallback shorter than 3 characters that is not in the allowlist (e.g. ux.adf gives ux) would violate TriggerSchema. That is an edge case.
  • Invariant tests: INV-2, -3, -4, -5 and -6 would each fail if their invariant broke. INV-1 is weak at the migrate level. The re-run test's sources are already pointers, so migrateSource returns before the writer is called. Idempotency is really covered by the writer unit test and by INV-5's GEMINI.md re-run. Consider a migrate-level re-run with a fresh non-pointer source routing to the same new module.
  • @stackbilt/adf compatibility (item 6): headingToModule is module-private and not exported from packages/adf/src/index.ts, so its return-type change is not a public API break. RoutingTrace.headingKeyword is an additive optional field. classifyElement now always sets routingTrace, which is a behaviour change for anyone who used routingTrace !== undefined to mean "content routing ran". The two in-repo consumers (adf-migrate.ts misroute audit, adf-tidy.ts itemRoutes) behave the same because candidateScores is {}. External consumers need a minor bump (feature), not a major one. phraseOverride is now an explicit undefined key, so toStrictEqual consumers would notice.
  • Bootstrap (item 7): On a fresh repo, bootstrap --yes registers infra.adf (Triggers on: deployment, docker, images) once. A re-run gives no orphan warning and a byte-identical manifest, and doctor passes the wiring checks. With pre-existing orphans, both are registered (lowercase stem) and doctor passes, apart from the destruction in item 1. Behaviour changes to note in CHANGELOG: orphan triggers are now lowercase, a non-list DEFAULT_LOAD now skips where it used to raw-append, and in --format json bootstrap swallows writer warnings entirely (bootstrap.ts:233).
  • Windows paths (item 8): entries are always written from normalizeModulePath output, so forward slashes only. No backslashes reach the manifest. The target field in JSON is .ai\\manifest.adf on Windows, which matches the other actions. Matching is case-sensitive: if a user has Infra.adf and migrate targets infra.adf, Windows gets two entries for the same file. This is an edge case.
  • Under --dry-run with no manifest, the warning says modules "were written". That is false in dry-run. Consider "would be written".
  • Judgement call (b), JSON-mode warnings on stderr, is fine.
  • adf_migrate_loc is 748 / 792, under the 765 target. Good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants