Skip to content

fix(adf): stop tidy ingesting compiled vendor output - #319

Merged
stackbilt-admin merged 2 commits into
mainfrom
auto/bugfix/issue-296
Sep 20, 2026
Merged

stackbilt-admin merged 2 commits into
mainfrom
auto/bugfix/issue-296

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

Closes #296

The bug

POINTER_MARKERS was one constant answering two incompatible questions, and COMPILE_BANNER_MARKER was its first element:

Reported consequences, all committed silently by the pre-commit hook: HTML marker comments arriving as CONTEXT bullets, METRICS / the module index / state.adf's STATE landing inside core.adf CONSTRAINTS, a deliberately deleted rule restored to [load-bearing], and a hand-authored 26-line core.adf reaching 66 lines.

The fix

Split the constant, with a note at the definition explaining why the two must not be merged again:

  • POINTER_MARKERS — pointer shape. No longer contains COMPILE_BANNER_MARKER.
  • CHARTER_OWNED_MARKERS — ownership. COMPILE_BANNER_MARKER plus every pointer marker.

Every current consumer was classified deliberately:

Site Question Set Behavior change
adf-compile.ts:243 overwrite guard ownership — safe to overwrite? CHARTER_OWNED_MARKERS none (#295/#300 guard intact)
adf-tidy.ts:243 analyzeVendorFile shape — is this a thin pointer? POINTER_MARKERS + explicit banner check the fix
adf-tidy.ts:341 pointer-preamble line skip shape — is this a preamble line? POINTER_MARKERS none — a banner line is already skipped by the <!-- / # rules, and compiled files no longer reach this function
adf-migrate.ts:220 "already a thin pointer" ownership — must I avoid ingesting this? CHARTER_OWNED_MARKERS none. Narrowing here would have made migrate ingest compiled output — the same bug in a second command
doctor.ts:313 agent-config pointer check ownership — is this un-migrated hand-written bloat? CHARTER_OWNED_MARKERS none
doctor.ts:402 keyword-density line filter boilerplate-line filter; the banner is boilerplate CHARTER_OWNED_MARKERS none
score.ts:1085 isSubstantiveInstruction strips boilerplate before judging substance CHARTER_OWNED_MARKERS none
bootstrap.ts:1076 migrate-source selection ownership — same as adf-migrate CHARTER_OWNED_MARKERS none
bootstrap.ts:1403 isAlreadyThinPointer ownership — may I write a pointer over this? CHARTER_OWNED_MARKERS none

Only adf tidy changes behavior. Everything else keeps the superset and is byte-for-byte equivalent to today.

Skip and warn, not a silent skip. A silent skip leaves a user running charter adf tidy with an unexplained "nothing to do". Tidy now reports the file with status: "compiled", adds a warnings[] entry to TidyResult (JSON and text), and names charter adf compile --write as the way to regenerate it. The text-mode warning prints before printTextResult's early return, which would otherwise have said "all clean" about a run that skipped a file.

Tests

Three new tests in packages/cli/src/__tests__/integration/vendor-bloat.test.ts. Measured against this branch's parent (ba616de), all three fail on main and pass with the fix — 3 failed / 7 passed → 10 passed.

Genuine bug detectors:

  1. Round trip — compile CLAUDE.md, then tidy; every file in .ai/ and CLAUDE.md itself must be byte-identical. On main, tidy reports CLAUDE.md: 5 items → core.adf (5) and rewrites both.
  2. Reverted deletion — compile a core.adf carrying two load-bearing rules, delete one from .ai/, then tidy. On main the deleted rule comes back from the stale artifact, along with the manifest's BUDGET and state.adf's STATE. This is the issue's most damaging symptom and the most reliable red, because the deleted rule is absent from existingAdf and so cannot be dedup'd away.
  3. Text-mode warning — the skip is explained rather than silent.

Existing guards against an over-eager fix, unchanged and still passing: adf-compile.test.ts "overwrites an existing compile artifact without --force" and "overwrites a previous compile artifact without --force" pin the banner's membership in the ownership set, so narrowing adf-compile.ts:243 to POINTER_MARKERS would fail them. vendor-bloat.test.ts scenarios 4 and 5 pin that an ordinary bloated thin pointer is still tidied and routed.

pnpm test 953/953 (950 baseline + 3), pnpm run typecheck clean (exit 0), ADF evidence gate PASS (adf_commands_loc 1017/1041).

Deliberately not in this PR

  • The .githooks/pre-commit:14-19 workaround stays. It greps for the banner and continues — the same test in the wrong layer. It should be removed after this ships, separately, so the two cannot drift silently in the meantime.
  • The vendor-mode design question is adf: no repo records whether vendor files are compiled or pointers, so compile --check and tidy disagree forever #318 — compile --check reporting thin pointers as permanently stale, adf sync --write writing an empty lock, and whether vendor mode deserves a config setting. Not implemented here; referenced in the CHANGELOG entry.

Reviewer notes

  • doctor's bloat analysis (doctor.ts:390+) already runs its section-overlap and keyword-density checks on compiled files, since a compile artifact has been in pointerFiles ever since the banner joined POINTER_MARKERS. That can produce false WARNs on the rendered ruleset. Pre-existing, orthogonal to bug(adf tidy): ingests compiled vendor output back into core.adf, duplicating content and reverting deliberate deletions #296, untouched here.
  • adf-migrate's skipReason: 'Already a thin pointer' is now imprecise for compiled input. Pre-existing wording, left alone to avoid touching assertions.
  • bootstrap's isAlreadyThinPointer name now answers the ownership question rather than the shape question. A rename is cheap but was left out to keep the diff to the bug.

🤖 Generated with Claude Code

stackbilt-admin and others added 2 commits September 20, 2026 16:52
POINTER_MARKERS answered two incompatible questions with one list, and
COMPILE_BANNER_MARKER was a member of it. `adf compile --write` renders the
whole ruleset into CLAUDE.md behind that banner, so `adf tidy` read the
artifact as a thin pointer and treated the compiled copy of .ai/ as content
added beyond the pointer — folding .ai/ back into itself and resurrecting
deliberately deleted rules.

Split the constant:

- POINTER_MARKERS       — "is this a thin pointer?" (shape). Excludes the
  banner. Used only by adf-tidy.
- CHARTER_OWNED_MARKERS — "did Charter author this?" (ownership). Banner plus
  every pointer marker. Used by adf-compile's overwrite guard, adf-migrate,
  bootstrap, doctor and score, all of which keep their current behavior.

tidy now skips a compiled file and says so, naming `adf compile --write` as
the way to regenerate it, rather than skipping in silence.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The comment cited "#295's sibling case", which does not exist — both #295
entries are about pointer stubs, and the banner-only check predates #295.
State what narrowing the guard would actually do, and name the test that
pins it.

Co-Authored-By: Claude Opus 5 (1M context) <[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.

APPROVED

Reviewed at 2c1186e (= PR head = local HEAD); base compared against origin/main @ ba616de (local main confirmed identical, not stale). All findings below are NITs; none block.

The central claim is proven: only adf tidy changes behavior

Ordered set-equality holds. Extracted both arrays programmatically:

  • main:packages/cli/src/commands/adf.ts POINTER_MARKERS = 8 elements, index 0 COMPILE_BANNER_MARKER + 7 string literals.
  • branch CHARTER_OWNED_MARKERS = [COMPILE_BANNER_MARKER, ...POINTER_MARKERS] expands to the same 8 elements in the same order. JSON.stringify comparison: true.

Order is moot anyway. Every consumer of either list is .some(marker => ...) (adf-compile.ts:249, adf-migrate.ts:220, doctor.ts:313, doctor.ts:402, bootstrap.ts:1076, bootstrap.ts:1403, adf-tidy.ts:268, adf-tidy.ts:366) or a full-array for...of + replaceAll (score.ts:1085). No .findIndex, no [0], no first-match dependence anywhere in the repo. Grepped POINTER_MARKERS|CHARTER_OWNED_MARKERS|COMPILE_BANNER_MARKER across all .ts/.js/.mjs/.sh outside node_modules/dist — no other consumer exists.

Relocation table verified independently, all nine sites:

site question list equivalent to main?
adf-compile.ts:249 ownership (overwrite guard, #295) CHARTER_OWNED_MARKERS yes
adf-tidy.ts:268 shape (the fix) POINTER_MARKERS narrowed no — this is the intended change
adf-tidy.ts:366 shape (preamble skip) POINTER_MARKERS narrowed yes, unreachable for compiled files — see below
adf-migrate.ts:220 ownership (don't ingest) CHARTER_OWNED_MARKERS yes
doctor.ts:313 ownership (don't advise migrate) CHARTER_OWNED_MARKERS yes
doctor.ts:402 boilerplate filter CHARTER_OWNED_MARKERS yes
score.ts:1085 boilerplate filter CHARTER_OWNED_MARKERS yes
bootstrap.ts:1076 ownership (migrate source selection) CHARTER_OWNED_MARKERS yes
bootstrap.ts:1403 ownership — ruled below CHARTER_OWNED_MARKERS yes

adf-tidy.ts:366 call-graph claim confirmed. extractBeyondPointer has exactly one caller: adf-tidy.ts:273, inside analyzeVendorFile, after the compiled early-return at adf-tidy.ts:263. The only other textual hit is a comment at adf-tidy.ts:568. A compiled file can never reach the preamble-skip loop, so the banner line never stops being skipped as preamble. Claim holds.

bootstrap.ts:1403 isAlreadyThinPointer — ruled: ownership is correct, the name is wrong. Its sole caller is bootstrap.ts:1021, the else if (!isAlreadyThinPointer(pointerPath)) arm of a chain where both remaining branches skip writing the pointer. The list only selects which warning text is emitted:

  • CHARTER_OWNED_MARKERS (this PR, == main) → "CLAUDE.md already exists; skipping (use --force to overwrite)".
  • narrowed POINTER_MARKERS → "CLAUDE.md has custom content; skipping pointer (run 'charter adf migrate' first…)" — which would be actively wrong advice, because adf-migrate.ts:220 correctly refuses to ingest a compile artifact. The user would be sent to a command that no-ops.

So the question being asked is "is this Charter's file, so don't tell the user to migrate it?" — ownership. Not a latent bug. See NIT 5 for the name.

The fix actually stops the round trip — reproduced both directions

I ran the branch's vendor-bloat.test.ts against main's source (all seven changed command files reverted with git restore --source=main --worktree, tests left on the branch):

main:   Tests  3 failed | 7 passed (10)
branch: Tests  10 passed (10)

Exactly the claimed split. All three new tests are genuine detectors; none passes vacuously on main.

The deleted-rule test is the one that matters most, and it pins the real defect:

AssertionError: expected 'ADF: 0.1\n\n📐 RULES:\n  - Database a…' not to contain 'Never import from dist'

Two things follow. First, the rule the user deleted from .ai/core.adf was genuinely resurrected on main — so it was demonstrably absent from existingAdf, and dedup cannot be what makes it pass on the branch. Second, note the module it landed in: the rule was deleted from core.adf CONSTRAINTS and came back under a RULES: section of a different module. The test therefore pins the cross-contamination symptom as well as the resurrection.

The byte-identity test (vendor-bloat.test.ts:363) snapshots every file in .ai/ plus CLAUDE.md and asserts toEqual/toBe after a full compile --write → tidy cycle. It fails on main with a 5-key object mismatch.

The #295/#300 guard is real, and stronger than claimed

I narrowed adf-compile.ts:249 back to POINTER_MARKERS in a scratch state and ran adf-compile.test.ts:

Tests  3 failed | 30 passed (33)
 × overwrites an existing compile artifact without --force        (adf-compile.test.ts:320)
 × overwrites a previous compile artifact without --force         (adf-compile.test.ts:384)
 × does not flag a re-compile of an existing artifact             (adf-compile.test.ts:512)

The implementer cited :320 and :384; a third test at :512 also fails. The guard is not imaginary and is broader than stated. Source restored byte-for-byte afterwards.

The inverse direction is also guarded: reverting adf-tidy.ts alone fails the three new #296 tests. And re-merging the lists in adf.ts would not resurrect the bug on its own, because the explicit banner check at adf-tidy.ts:263 runs before the pointer test — the fix is defended twice over.

Also ran

  • Full CLI suite on the branch: 343 passed (31 files), 0 failed.
  • pnpm exec tsc --noEmit at repo root: exit 0.

Findings

  1. [NIT] .githooks/pre-commit:16 and packages/cli/src/commands/hook.ts:53-59 — the compiled-file skip in both hooks is dead code, and that is why #296 escaped the hook. The loop continues past a compiled vendor file so it never enters STAGED_VENDORS, but the tidy invocation two lines later (.githooks/pre-commit:26, hook.ts:62) passes no --source. adfTidyCommand at packages/cli/src/commands/adf-tidy.ts:106-108 falls back to VENDOR_FILES.filter(existsSync) — every vendor file on disk, regardless of what the hook filtered. The grep has never protected anything; it only suppressed the reporting path. Pre-existing, not introduced here, and now genuinely redundant since adf-tidy.ts:263 does the real work. Worth naming because it is the reason the guard was believed to exist. Either delete both greps or pass --source per staged file; a follow-up, not this PR.

  2. [NIT] harness/types.ts:43 mirrors TidyFileResult['status'] and is now stale — it still reads 'clean' | 'tidied' | 'not-found' and lacks 'compiled'. Harmless in practice: the root tsconfig.json include is ["packages/**/*.ts"], so harness/ is not covered by the pre-commit tsc --noEmit; and nothing in harness/*.ts switches on status — the only reads are totalExtracted (harness/evaluator.ts:44, harness/runner.ts:197,355,489,494) and modulesModified. harness/types.ts:54-60 TidyOutput likewise omits the new warnings array, and harness/runner.ts:145 already constructs a fallback missing moduleWarnings, so the mirror was drifting before this PR. Documentation drift only.

  3. [NIT] Nothing else parses TidyResult positionally or switches exhaustively on status. Checked the fourth-value question explicitly. .githooks/pre-commit:27 greps "totalExtracted": *[0-9]+ out of the JSON — key-based, unaffected by the new warnings array or the new status value, and the compiled path contributes itemsExtracted: 0 so totalExtracted stays 0 and the hook correctly reports clean. precommit-hook.test.ts:117 pins that same grep string and still passes. --ci (adf-tidy.ts:230) gates on totalExtracted > 0 && dryRun, so a compiled-only run exits SUCCESS — correct, a compile artifact is not a CI failure.

  4. [NIT] The bare content.includes(COMPILE_BANNER_MARKER) substring test at adf-tidy.ts:263 is reachable in principle but the failure mode is acceptable. A hand-authored thin pointer that quotes the literal string GENERATED by charter adf compile — in documentation, an example, or a quoted remedy — would be classified as compile output and skipped. Three things make a stricter top-of-file/first-N-lines check not worth requiring here: (a) the skip is loud, not silent — adf-tidy.ts:135-141 names the file and explains why, and printTextResult prints it before the early return, so the user sees it every run; (b) doctor.ts:367 and both hooks already use the byte-identical test, so tightening one without the others would create a three-way disagreement about what "compiled" means, which is a worse version of the #296 conflation; (c) the new warning text itself contains charter adf compile --write, not the banner marker, so tidy's own output cannot self-trigger the check if pasted into a vendor file. Behavior is also strictly better than main for this case, where such a file was treated as a pointer and its content ingested. If this is ever tightened, tighten all three sites in one change.

  5. [NIT] bootstrap.ts:1400 isAlreadyThinPointer is misnamed for what it now asks. It uses CHARTER_OWNED_MARKERS and answers "did Charter author this?", but the name asks the shape question the PR just spent its comment block separating. A compile artifact makes it return true while not being a thin pointer. Behavior is correct (see ruling above) and identical to main; only the name perpetuates the conflation. Suggest isCharterOwnedFile.

  6. [NIT] doctor.ts:313/doctor.ts:329 report a compile artifact as a "thin pointer". isPointer is computed from the ownership list, and the PASS detail reads "N agent config file(s) are thin pointers to .ai/.". For a compiled CLAUDE.md that sentence is false. The classification itself is right — a compile artifact must not trigger Run: charter adf migrate --dry-run — and doctor.ts:367 already skips compiled files before the bloat scan, so only the message wording is inaccurate. Pre-existing and identical to main; noted because it is the same conflation this PR is about.

Working-tree hygiene

Verified after both scratch experiments. git status --porcelain reports only:

?? .ai/context.adf
?? .ai/context.snapshot.json

and git diff --quiet exits 0. No tracked file modified, nothing staged, none of CLAUDE.md / AGENTS.md / .cursorrules / GEMINI.md touched. The compile-guard experiment restored adf-compile.ts from a scratchpad copy taken before editing; the main-repro used git restore --source=main --worktree (index untouched) and was undone with git restore --worktree packages/cli/src/commands/. Both restorations confirmed by a clean git diff --stat. All scratch artifacts live outside the repo. Nothing committed or pushed.

@stackbilt-admin
stackbilt-admin merged commit 156c522 into main Sep 20, 2026
5 checks passed
@stackbilt-admin
stackbilt-admin deleted the auto/bugfix/issue-296 branch September 20, 2026 22:15
stackbilt-admin added a commit that referenced this pull request Oct 15, 2026
…arity (#336)

#320 read the compiled-file grep in the pre-commit hooks as dead since tidy skips compiled files itself (#319). It is not: the grep keeps compiled files out of STAGED_VENDORS, which gates the whole tidy block, so a compile-only commit skips tidy and the .ai/*.adf re-stage. Deleting it would sweep unrelated module edits into those commits. Keep it, make the comment say so in both copies, and add a test pinning the grep and the parity between .githooks/pre-commit and the generated hook.

Refs #320

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

bug(adf tidy): ingests compiled vendor output back into core.adf, duplicating content and reverting deliberate deletions

1 participant