Skip to content

fix(adf): accept charter pointer stubs in compile overwrite guard - #300

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

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

Conversation

@stackbilt-admin

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

Copy link
Copy Markdown
Member

Fixes the compile item of #295.

The issue's second item — the bootstrap "Next steps" line lists lowercase agents.md where AGENTS.md is written, and omits GEMINI.md — is delivered by the #297 PR, since #297 changes which file bootstrap writes. Deliberately not auto-closing #295; close it by hand once #297 lands.

What

adf compile --write refused every vendor file a clean bootstrap had just generated, because the overwrite guard (adf-compile.ts runWriteMode) keyed on COMPILE_BANNER_MARKER alone and bootstrap's pointer stubs do not carry it. The documented two-command onboarding therefore ended in four warnings and a prompt to use --force on the very first compile — training exactly the habit the guard exists to prevent.

The guard now admits a file when it matches POINTER_MARKERS. That list already contains COMPILE_BANNER_MARKER, so this subsumes the previous check rather than widening it arbitrarily; it additionally recognises the thin pointer stubs written by bootstrap and adf init --emit-pointers. Hand-authored content carrying no charter marker is still refused.

Verified end to end on a fresh bootstrap --yes with the built CLI:

$ charter adf compile --target all --write      # no --force
  [ok] Written CLAUDE.md (replaced charter pointer stub)
  [ok] Written AGENTS.md
  [ok] Written .cursorrules (replaced charter pointer stub)
  [ok] Written GEMINI.md (replaced charter pointer stub)
exit=0

Retained-section protection (review round 1)

Admitting pointer stubs opened a new data-loss path: compile --write would eat the exact file shape charter adf tidy produces — a thin pointer with a retained ## Environment block — which the pre-commit hook manufactures automatically. The compiler emits no Environment section and tidy routes none of that content into .ai/, so git was the only recovery.

The guard now refuses when a pointer-admitted file carries retained content (## Environment, ## Module Index, ## Session *, ## * Protocol) that diverges from every pointer template for that filename. stripCharterSentinels runs on both sides first, so a bootstrap-populated ## Module Index still compiles without --force and #295 stays fixed.

Reproducing the reviewer's case against this repo's own CLAUDE.md:

$ charter adf compile --target claude --write    # no --force
  [warn] Refused to overwrite CLAUDE.md — it is a charter pointer carrying your own
         content under ## Environment. Compiled output has no such section,
         and .ai/ does not hold this content, so it would be lost.
         Move it into a .ai/ module first, or pass --force to discard it.
exit=1
# file byte-identical; Environment notes intact

Refuse rather than preserve: appending retained sections to compiled output would make runCheckMode's strict byte comparison (adf-compile.ts) report every file with an Environment block as permanently [stale], which would have to be reconciled here too.

Why not the reporter's option 1

The issue suggests stamping the compile banner into the bootstrap pointer templates. That marker is load-bearing for two other consumers, both of which read it as "compiler output — skip":

  • packages/cli/src/commands/doctor.ts:305 — the vendor-file bloat scan iterates pointer files and skips any carrying the marker. Stamping the templates would have made that scan dead code for the files it was written for.
  • packages/cli/src/commands/hook.ts:53 — the pre-commit hook drops staged vendor files matching the marker from STAGED_VENDORS, so a staged bootstrapped CLAUDE.md would no longer trigger auto-tidy (the feat(hook): pre-commit interception of vendor file bloat with auto-tidy #24 path).

Separately, adf-tidy.ts restorePointer rewrites CLAUDE.md wholesale from POINTER_CLAUDE_MD, so a single charter adf tidy pass would have stripped the banner back off and reintroduced the bug.

Tradeoff for the reviewer

compile --write now refuses rather than destroys: a pointer carrying user-authored retained content stops the write and names the heading responsible. --force remains the documented escape hatch, and it discards that content — unchanged from before this PR, and now the only way to lose it. A pristine pointer (nothing written into the ## Environment slot) compiles with no --force, which is the #295 fix.

Not covered: a file that already carries the compile banner and has had an Environment block added to it afterwards is still overwritten without --force. That is pre-existing behavior on a file whose first line reads DO NOT EDIT, and is out of scope here.

Follow-up (non-blocking, from review)

The --format json written array is bare filenames, so a JSON consumer cannot distinguish a pointer conversion from a re-compile; the (replaced charter pointer stub) annotation exists only on the text path. Worth a replacedPointers field in a separate PR.

Tests

New adf compile — --write overwrite guard block in packages/cli/src/__tests__/adf-compile.test.ts covers: pointer stub written, hybrid pointer written, populated module index written, existing compile artifact written, full bootstrap pointer set compiling under --target all with no refusals, hand-authored content still refused, pointer with ## Environment notes refused with the heading named, pointer with a ## Release Protocol section refused, --force overwriting a pointer with notes, and the (replaced charter pointer stub) annotation appearing only on the conversion write.

The pre-existing test refuses to overwrite pointer stubs without --force asserted the behavior this PR deliberately inverts; it was replaced by the corrected expectation rather than dropped. pnpm run typecheck clean, pnpm test 882/882.

Implemented via constellation-loop.

🤖 Generated with Claude Code

…oses #295)

The --write overwrite guard keyed on COMPILE_BANNER_MARKER alone, so the
first `adf compile --target all --write` after a clean bootstrap refused
every vendor file Charter itself had just written, pushing users toward
--force on their first compile — the exact habit the guard exists to
prevent.

Widen the guard to POINTER_MARKERS, which already includes
COMPILE_BANNER_MARKER and additionally recognises the thin pointer stubs
written by `bootstrap` and `adf init --emit-pointers`. Hand-authored files
carrying no charter marker are still refused.

The alternative fix (stamping the banner into the pointer templates)
was rejected: COMPILE_BANNER_MARKER also means "skip this file" to the
doctor bloat scan (doctor.ts) and the pre-commit tidy trigger (hook.ts),
so stamping it into pointers would have silently disabled both.

Resolves-Request: #295

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.

CHANGES_REQUESTED

The guard logic itself is right, and the reasoning for rejecting the reporter's option 1 holds up against source. The blocking issue is #2 below: compile --write now destroys the output that charter adf tidy produces, without --force.

Verified as claimed

Baseline reproduced. pnpm run typecheck clean, pnpm test 878/878 (62 files) on auto/bugfix/issue-295. Matches the PR.

The rejection of option 1 is correct. COMPILE_BANNER_MARKER is 'GENERATED by charter adf compile' (packages/adf/src/compiler.ts:85), which is the literal string hook.ts:53 greps for before dropping a file from STAGED_VENDORS, and doctor.ts:305 continues on. Stamping it into the pointer templates would break both, and adf-tidy.ts restorePointer would strip it back off on the next tidy. Confirmed against source, not taken on faith.

Concern 1 — guard not eroded. adf-compile.test.ts still refuses hand-authored content carrying no charter marker is a real refusal case, and the pre-existing --force overwrites a file without the compile banner test still holds. Verified empirically with the built CLI: a CLAUDE.md containing no marker gives [warn] Refused to overwrite ... hand-authored content found., exit 1, file byte-identical. The guard has not degraded to "always overwrite."

Concern 3 — replacement is genuine. The deleted refuses to overwrite pointer stubs without --force used a pointerStub that is a prefix of POINTER_CLAUDE_MD. The new overwrites a thin pointer stub without --force asserts the inverted expectation against the full constant, and the refusal purpose moved to a new dedicated test. Replaced, not weakened or dropped.

Concern 4 — cycle is safe. adf.ts:28 imports adfCompileCommand, dereferenced only at adf.ts:268 inside a function; adf-compile.ts:29 imports POINTER_MARKERS, dereferenced only at adf-compile.ts:161 inside runWriteMode. Neither touches the other at module-evaluation time, so no TDZ hit in either entry order. This is not a new pattern — adf-tidy.ts:25/adf-tidy.ts:243 and adf-migrate.ts:32/adf-migrate.ts:220 already hold the identical cycle in production code. No objection.

Blocking

Concern 2 — this is not a pre-existing exposure, it is a new one. I verified the claim and it is worse than the PR body frames it.

Reproduced with the built CLI on a copy of this repo's own CLAUDE.md plus its .ai/:

BEFORE: ## Environment  (3 WSL / hooksPath / build-error rules)
$ charter adf compile --target claude --write     # no --force
  [ok] Written CLAUDE.md (replaced charter pointer stub)
exit=0
AFTER:  ## Environment  -> gone

Three load-bearing project instructions deleted, exit 0, recoverable only from git.

The PR argues this exposure is inherent to any fix for #295. That defence does not survive the specifics:

  • Pre-PR, compile could only destroy Environment content in a file whose first line reads DO NOT EDIT. A user editing there was overriding an explicit instruction.
  • Post-PR, it destroys the exact file shape charter adf tidy produces. adf-tidy.ts:290 readRetainedSections deliberately re-appends ## Environment after the pointer, isRetainedHeading (adf-tidy.ts:277) marks it retained, and the pre-commit hook runs tidy automatically. So the standard Charter workflow now manufactures a file that the next compile --write silently eats. Two Charter commands disagree about the same file with no --force between them.
  • The content is there by design: POINTER_CLAUDE_MD and POINTER_CLAUDE_MD_HYBRID (adf.ts:309, adf.ts:326) both ship ## Environment with <!-- Add runtime/OS/shell-specific notes here (not stack rules) -->, and doctor.ts:317 whitelists Environment as // Environment is legitimate. Charter invites the content into that slot, then deletes it.
  • There is no round trip. Confirmed: charter adf tidy on this repo's CLAUDE.md reports "All vendor files are clean" and routes nothing into .ai/; renderToVendorFormat (packages/adf/src/compiler.ts:161-220) emits banner, title, role, default sections and module index, and no Environment. Git is the only recovery.

The (replaced charter pointer stub) annotation is not adequate mitigation. It is a parenthetical on a success line, it appears after the write, and it names the stub rather than the loss.

Required changes

  1. packages/cli/src/commands/adf-compile.ts:147-172 — compile --write without --force must not discard retained sections from a file admitted via a pointer marker. Two shapes work; your call:

    • Preserve: export readRetainedSections from adf-tidy.ts:290 and append its result to the compiled output. Fixes the pre-existing banner-bearing case too. Caveat: runCheckMode at adf-compile.ts:234 is a strict onDisk === result.output byte comparison, so appending makes --check report permanently [stale] for any file with an Environment block. That has to be reconciled in the same PR.
    • Refuse: keep the pointer-marker admission but refuse when the file carries retained content the template does not, e.g. readRetainedSections(existing) !== readRetainedSections(template). Smaller, leaves --check untouched, and keeps the guard's promise intact. Run stripCharterSentinels first, or a populated hybrid ## Module Index will diverge from the template and re-break #295.
  2. packages/cli/src/commands/bootstrap.ts:376 — Closes #295 drops the issue's second item. The next-steps line is still git add .charter .ai CLAUDE.md .cursorrules agents.md: lowercase agents.md where AGENTS.md is written, and GEMINI.md missing. Fix it here (trivial, same area) or downgrade the PR to Fixes the compile item of #295 and split the rest.

Non-blocking

  1. adf-compile.ts:181 — the pointer-replacement flag exists only on the text path. The JSON written array is bare filenames, so a JSON consumer cannot distinguish a conversion from a re-compile. Worth a replacedPointers field once change 1 settles.

  2. POINTER_MARKERS (adf.ts:291) are prose strings, not sentinels, so a hand-authored file quoting one is admitted. Pre-existing and shared with five other call sites — not this PR's to fix, noting it only because this PR adds a destructive consumer.

Review round 1 on #300: admitting pointer stubs made `compile --write`
destroy the exact file shape `charter adf tidy` produces and the
pre-commit hook manufactures — a thin pointer with a retained
`## Environment` block. The compiler emits no Environment section and
tidy routes none of it into .ai/, so the content was recoverable only
from git.

Keep the pointer-marker admission, but refuse when the file carries
retained content (Environment, Module Index, Session *, *Protocol) that
diverges from every pointer template for that filename. Sentinels are
stripped first, so a bootstrap-populated `## Module Index` still compiles
without --force and #295 stays fixed. The refusal names the offending
heading and points at --force as the discarding escape hatch.

Refuse rather than preserve: appending retained sections to compiled
output would make runCheckMode's byte comparison report every file with
an Environment block as permanently stale.

Resolves-Request: #295

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

Round 2. The Refuse shape closes the hole. I re-ran every check myself against a fresh build of 92e0ab1 rather than reading the transcript.

Baseline reproduced: pnpm run typecheck clean, pnpm test 882/882 (62 files). Matches the PR.

1. Round-1 repro is closed

Same setup as round 1 — a copy of this repo's own CLAUDE.md with its WSL ## Environment block, plus its .ai/:

$ charter adf compile --target claude --write     # no --force
  [warn] Refused to overwrite CLAUDE.md — it is a charter pointer carrying your own
         content under ## Environment. Compiled output has no such section,
         and .ai/ does not hold this content, so it would be lost.
         Move it into a .ai/ module first, or pass --force to discard it.
exit=1

File md5 byte-identical before and after. The message names the heading, says why compiled output cannot hold it, and gives both ways forward. That is a better refusal than the one it replaces.

I also confirmed the workflow framing from round 1 directly: running charter adf tidy first and then compile --write now refuses (adf-compile.ts:255-271). Compile no longer eats what tidy produces, which was the blocking finding.

2. #295 still passes end to end

Real bootstrap --yes in a fresh git repo, then adf compile --target all --write, no --force:

  [ok] Written CLAUDE.md (replaced charter pointer stub)
  [ok] Written AGENTS.md
  [ok] Written .cursorrules (replaced charter pointer stub)
  [ok] Written GEMINI.md (replaced charter pointer stub)
exit=0

Zero warnings, zero --force. The original bug stays fixed.

3. The stripCharterSentinels caveat is handled

This is the part I most wanted to see fail, and it does not. In the run above, bootstrap had populated the hybrid ## Module Index with a real four-row table, and CLAUDE.md still compiled clean. retainedSectionMap (adf-compile.ts:169-171) runs stripCharterSentinels before readRetainedSections, so the populated index and the template's empty one both reduce to an empty body and compare equal. Verified against genuine bootstrap output, not only the synthetic fixture at adf-compile.test.ts:427-441.

pointerTemplatesFor (adf-compile.ts:157) keys on exactly the four values in TARGET_FILENAMES (packages/adf/src/compiler.ts:67-72) — CLAUDE.md, AGENTS.md, .cursorrules, GEMINI.md — so no target falls through to the ?? [] branch and gets spuriously refused. Both CLAUDE.md shapes are in the list, so adf init --emit-pointers output is admitted alongside bootstrap's.

4. Test quality

The still-refuses-hand-authored case survived the rework intact (adf-compile.test.ts:398-410) — I re-verified it empirically too: a marker-free CLAUDE.md gives exit 1 and the generic hand-authored warning, file untouched.

The two new refusal tests assert on cause, not just exit code: refuses a pointer carrying user notes under ## Environment (adf-compile.test.ts:443-465) captures console.error and asserts the warning contains ## Environment and --force, plus byte-equality of the file. refuses a pointer carrying a retained operational protocol section (adf-compile.test.ts:467-480) does the same for ## Release Protocol, which covers the isRetainedHeading Protocol branch (adf-tidy.ts:281) rather than only Environment. --force overwrites a pointer carrying user notes (adf-compile.test.ts:482-498) pins the escape hatch. Verified independently: --force writes and drops the block, as advertised.

Good instinct including the populated-index case as a test rather than leaving it to manual checking — it is the regression most likely to be reintroduced by a future refactor of the comparison.

5. Module cycle — no reintroduction

pointerTemplatesFor builds its Record inside the function body (adf-compile.ts:157-165), so the POINTER_* bindings are dereferenced at call time. This is precisely why the module-scope const failed with Cannot read properties of undefined — worth keeping the comment at adf-compile.ts:149-153 for exactly that reason.

Grepped for any module-scope binding dereferencing POINTER_*, readRetainedSections, retainedSectionMap or pointerTemplatesFor in adf-compile.ts: none, all inside function bodies. The new import { readRetainedSections } from './adf-tidy' (adf-compile.ts:39) adds adf → adf-compile → adf-tidy → adf, but readRetainedSections is a hoisted function declaration and is only called from inside retainedSectionMap, so no evaluation-order exposure. 882/882 passing is consistent with this.

Non-blocking, no action needed on this PR

  1. The scoped-out Preserve case is still live, and I confirmed its exact shape so the follow-up issue can be written precisely: compile over an already-compiled file (banner present) with a user-added ## Environment still destroys it silently at exit 0. The guard only runs under if (replacedPointer) (adf-compile.ts:256), which is false once the banner is there. This predates the PR and is strictly narrower than what round 1 blocked on — the pointer/tidy shape, which is where the content actually accumulates, is now protected. Worth carrying into that issue verbatim.

  2. Cosmetic: --force over a pointer stub logs [ok] Written CLAUDE.md with no (replaced charter pointer stub) annotation, since --force short-circuits the block that sets replacedPointer (adf-compile.ts:236). Harmless, and arguably correct given --force is explicit.

Changes 1 and 2 from round 1 are both resolved — the retained-section hole here, and the bootstrap.ts:376 item by splitting to #297 with the PR body scoped to Fixes the compile item of #295. No further objections.

@stackbilt-admin
stackbilt-admin merged commit 1856a64 into main Sep 20, 2026
5 checks passed
@stackbilt-admin
stackbilt-admin deleted the auto/bugfix/issue-295 branch September 20, 2026 00:56
stackbilt-admin added a commit that referenced this pull request Sep 20, 2026
POINTER_MARKERS was one constant answering two incompatible questions, with
COMPILE_BANNER_MARKER as its first element:

  compile's overwrite guard asks "did Charter author this, so may I replace it?"
  -- a compile artifact qualifies, and #295/#300 added the banner for that.

  tidy's analyzeVendorFile asks "is this a thin pointer whose extra content is
  bloat to ingest?" -- a compile artifact is the opposite: it is the full ruleset
  rendered FROM .ai/.

So tidy treated compiled output as a pointer and folded .ai/ back into itself.
The reported damage: HTML marker comments became CONTEXT bullets accumulating a
copy per run, METRICS and state.adf's STATE cross-contaminated core.adf
CONSTRAINTS, a 26-line hand-authored core.adf became 66 lines, and a rule the
user had deliberately deleted was silently restored to [load-bearing]. All of it
landed during the pre-commit hook, so it was committed unnoticed.

The constant is split. CHARTER_OWNED_MARKERS is [COMPILE_BANNER_MARKER,
...POINTER_MARKERS] -- set-equal to the old list, so all eight other consumers
are byte-for-byte unchanged; adf tidy is the only command whose behavior moves.
Narrowing adf-migrate.ts to the pointer list would have reproduced this exact bug
in a second command, and narrowing bootstrap's isAlreadyThinPointer would have
advised users to run a migrate that correctly refuses compile artifacts.

analyzeVendorFile now returns early on the banner, before the pointer test, and
tidy reports the skip loudly rather than silently: a run whose only file was
compiled would otherwise print "all clean" with no explanation.

The vendor-mode question underneath this -- nothing records whether a repo uses
compiled files or pointers, which is why compile --check calls a deliberate
pointer permanently stale -- is deferred to #318.

Closes #296
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