Skip to content

[0.38] fix(compile): Claude AGENTS.md routing follow-ups from the #4005 review (#4787) - #4805

Merged
Daniel Meppiel (danielmeppiel) merged 6 commits into
mainfrom
danielmeppiel-p2-4787-agents-md-routing
Nov 22, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 6 commits into
mainfrom
danielmeppiel-p2-4787-agents-md-routing

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

Seven follow-up defects in the Claude AGENTS.md routing that #4005 introduced, from its Copilot re-reviews:

  1. Pack bundles unadmitted instructions. routed_claude_rule_renders now builds the canonical DeployableSourcePlan (Claude target, skill subset from effective_dependency_selection). render_agents_md_routed_rules selects sources through it, so a root-level *.instructions.md that install never deploys is never packed.

  2. Compile reminder never clears after link rewriting, and 7. scope or body edits keep the reminder silent. After compiling, the reminder is no longer decided by a substring check against compiled text. A Claude-only compile now records, for each AGENTS.md it wrote:

    • the file's digest;
    • an exact fingerprint of each routed instruction it carries (normalized applyTo plus the normalized source body).

    The record lives in APM home (compiled-agents-md/<root key>.json), not in the repository. Install compares a routed instruction's fingerprint with the record for the one AGENTS.md its scope names: the root for always-on, exactly <dir>/AGENTS.md for a directory. The file must still match its recorded digest, and its CLAUDE.md must import it. As a result:

    • compile's link rewriting no longer matters;
    • an applyTo change or a shortened body asks for apm compile again;
    • any later rewrite of the file by something else asks again too.
  3. managed_section without markers. The blocker check now validates the configured markers through the managed-section owner (validate_managed_section). A hand-authored AGENTS.md whose markers are missing, duplicated or reversed keeps the rules in .claude/rules/, and install warns.

  4. Repeated AGENTS.md reads. The integrator holds one CompiledCoverage per project root for the install. The record, each file's digest and each directory's CLAUDE.md import check are read once.

  5. apm compile --target claude --root <dir> dropped directory placements. pin_directory_placements now checks that the directory exists under the source root and writes the placement under the output root.

  6. Claude shims after a failed AGENTS.md write. AGENTS.md outputs are recorded only after the batch is written (or for a dry-run preview). compile() skips CLAUDE.md generation when AGENTS.md compilation failed.

The record is written outside compilation/ (core/agents_md_compile_record.py, under the routing owner's selectors), so compiled outputs still go only through CompiledOutputWriter. The routing guard pins the new wiring: _compile_distributed must call record_compiled_instructions, and _claude_rule_sources must call CompiledCoverage.

Issue and approved scope

Issue: Fixes #4787

Human scope-approval comment: #4787 (apm-scope:v1 record on the issue)

Does this PR complete the issue, or what remains? It completes the issue. The missing-cache-directory case stays in #4755, as the record says.

Notes:

  • A project compiled before this change has no record, so the next install prints the apm compile reminder once. The compile after that writes the record and the reminder stays quiet.
  • With compilation.agents_md.mode: managed_section, editing the hand-authored part of AGENTS.md changes its digest, so the reminder appears once until the next compile.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

New cells in tests/unit/compilation/test_claude_agents_md.py (TestRoutingFollowUps, plus test_managed_section_without_one_marker_pair_keeps_the_rules). Each asserts behaviour that main lacks:

  • item 1: a root-level instruction is not rendered;
  • item 2: no reminder after a compile rewrote a context link;
  • item 7: reminder after an applyTo narrowing or a shortened body;
  • item 4: AGENTS.md read count independent of the routed instruction count;
  • item 5: --root writes src/AGENTS.md and its shim;
  • item 6: a batch rejected by the output gate (hidden Unicode) leaves no CLAUDE.md;
  • item 3: missing, duplicated or reversed markers keep the rule and warn.

The existing reminder cell now compiles for real instead of using a hand-written AGENTS.md. The managed-section blocker cell adds the markers its "routes" case needs.

Validation evidence:

  • Local pytest is suspended on the shared host (P2 rule 17), so CI is the test gate. One earlier guarded run of the unit file passed 60 cells; the read-count cell was rewritten after that run.
  • ruff check / ruff format --check (0.15.12, CI paths): clean.
  • scripts/lint_architecture_boundaries.py --root .: exit 0.
  • pylint R0801: 10.00/10. lint-auth-signals.sh: clean.
  • check_complexity_ratchet.py --base-ref main: clean.
  • check_test_assertions.py: assertion-quality ratchet clean. tests.quality.taxonomy --changed: clean.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

…ew (#4787)

- pack renders routed rules only from sources the canonical deployment
  source plan admits (item 1);
- compile records each routed instruction's scope and body fingerprint per
  AGENTS.md (with the file digest); install compares it through one
  CompiledCoverage per root, so link rewriting stays quiet and applyTo or
  body edits remind (items 2, 4, 7);
- managed_section mode validates its markers through the managed-section
  owner before the route opens (item 3);
- compile --root checks directory placements under the source root (item 5);
- AGENTS.md outputs are recorded only once written, and no CLAUDE.md shim
  is written after a failed AGENTS.md batch (item 6).

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.38.0 milestone Nov 22, 2026
Copilot AI balanced review requested due to automatic review settings November 22, 2026 19:48

Copilot AI 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.

🟡 Changes recommended

Unresolved compile-record timing and multi-target coverage gaps cause repeated recompilation prompts, while missing managed-section marker validation can retire native rules for a file that compilation rejects.

3 open issues
What changed in this PR

Addresses #4787’s follow-ups to Claude AGENTS.md routing introduced in #4005, covering pack source selection, compile reminders, managed sections, and output ordering.

Changes:

  • Uses install’s source plan when packing routed Claude instructions.
  • Adds persisted instruction fingerprints and file digests, with cached coverage checks for compile reminders.
  • Validates managed-section markers and adjusts directory placement and shim generation, with regression tests and documentation.
File Description
tests/​unit/​compilation/​test_claude_agents_md.py Adds regression coverage for the seven follow-up cases.
src/​apm_cli/​integration/​instruction_integrator.py Uses source plans for pack rendering and caches compiled coverage.
src/​apm_cli/​core/​agents_md_compile_record.py Persists compile records outside the project.
src/​apm_cli/​compilation/​managed_section.py Exposes managed-section marker validation.
src/​apm_cli/​compilation/​claude_agents_md.py Adds fingerprint-based coverage and adjusts routing checks.
src/​apm_cli/​compilation/​agents_compiler.py Records written outputs and skips shims after AGENTS.md failures.
scripts/​architecture_linter/​checks/​install_claude_agents_md_routing.py Guards compile-record and coverage wiring.
docs/​src/​content/​docs/​consumer/​update-and-refresh.mdx Explains marker requirements and reminder behavior.
changelog.d/​4805.fixed.md Summarizes the user-facing routing fixes.
.apm/​architecture/​owners/​install-deployment.json Registers compile coverage under the routing owner.

Review effort: Balanced

…#4787)

apm-spec-waiver: Claude AGENTS.md routing, compile reminder and pack source selection are local client behaviour; no manifest, lockfile or resolver contract changes

Co-authored-by: Copilot App <[email protected]>

Copilot AI 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.

🟡 Changes recommended

Unresolved marker-validation and coverage-tracking defects can retire native rules before compilation fails, repeatedly flag successful compiles as stale, and miss instructions whose bodies were cleared.

6 open issues

Review effort: Balanced

…and empty bodies (#4787)

- record coverage whenever Claude consumes the compiled AGENTS.md (multi-
  target and --target all) and match fingerprints wherever the shared
  optimizer placed them;
- re-pin the record after the CLI's constitution rewrite;
- compare an emptied body's fingerprint too;
- managed-section mode validates markers in a generated AGENTS.md too;
- routing verdicts and the blocker are cached per install root.

apm-spec-waiver: Claude AGENTS.md routing, compile reminder and pack source selection are local client behaviour; no manifest, lockfile or resolver contract changes

Co-authored-by: Copilot App <[email protected]>

Copilot AI 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.

🔵 Needs a closer look

Scope and fingerprint tracking defects can leave stale AGENTS.md guidance undetected, while invalid marker handling can retire native rules even though compilation cannot deliver their guidance.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Invalid marker values can retire rules without compiling guidance

src/​apm_cli/​compilation/​claude_agents_md.py:193

An explicit start_marker: null (or another non-string marker) falls back to the default here, but CompilationConfig.from_apm_yml preserves that configured value. If AGENTS.md contains the default markers, install therefore accepts it and retires the native rules, while compile rejects the null marker and cannot deliver the guidance. Use defaults only for absent keys, and make explicitly invalid values fail managed-section validation so install retains the rules.

Medium severity Identical fingerprints across scopes can mask stale guidance

src/​apm_cli/​compilation/​claude_agents_md.py:563

If instructions A and B both contain Use ASCII. but have scopes ** and src/**, narrowing A to src/** makes B's recorded fingerprint satisfy this lookup for both sources. Install then treats A as unchanged even though its old guidance remains global in the root AGENTS.md. Record a stable source identity alongside each fingerprint and compare the same source during install, with a regression for identical bodies at different scopes.

Medium severity Scope changes can leave stale compiled guidance undetected

src/​apm_cli/​integration/​instruction_integrator.py:585

Changing a local instruction from applyTo: '**' to **/*.py excludes it from routed, so this check never marks it stale. A local-only reinstall then prints no compile reminder even though AGENTS.md still contains the old always-on guidance. Check previously compiled contributions before filtering out native rules, and add a regression for this scope transition.

6 resolved since last review

Review effort: Balanced

Copilot AI 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.

🔵 Needs a closer look

Human review is needed to assess validation and routing inconsistencies that can retire native rules before compilation rejects AGENTS.md or leave stale global guidance after routing changes.

No open issues 🎉

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit d8b37cb into main Nov 22, 2026
74 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-p2-4787-agents-md-routing branch November 22, 2026 21:20
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
…t instruction

#4805 makes compile record which routed instructions each Claude AGENTS.md carries under APM home (.apm/compiled-agents-md, core/agents_md_compile_record.py), so nothing new lands in the repository. I5 now admits that one prefix for a non-read-only compile only. A dry-run compile, and every other command, stays forbidden from writing it. Fixes test_claude_routes_an_always_on_instruction_to_agents_md.

On main d8b37cb (#4805), the kit packages' '**' instruction no longer reaches .claude/rules for Claude in the M-SOURCE context pass. That is the #4005 contract (always-on guidance routes to AGENTS.md through compile), so the spine's rule-evidence oracle was stale for an always-on instruction. The kit instruction is now path-scoped ('**/*.py'), like M-SOURCE's own packages, so .claude/rules/<pkg>.md stays a deterministic, strictly checked destination; test_file_routing covers the '**' route. Fixes test_source_spine_covers_every_form_and_op.

Co-authored-by: Copilot App <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
… it was copied into

Exposed by following #4805 with a path-scoped kit instruction. With '**', install rendered the routed Claude rule for the bundle; a path-scoped rule is not routed, so nothing re-renders it. apm install <bundle> is imperative (reference/cli/install.md): it copies the bundle into the targets current at that moment. A later manifest install, after widening targets to claude, deploys nothing new for the bundle and reports no changes (reproduced with the CLI). instructions_witnessed now requires a bundle's instruction under the targets of its last bundle install that are still current (bundle_targets), not under every current target. Fixes test_source_spine_covers_every_form_and_op (install bundle p-pack-bundle: .claude/rules/p-pack.md).

Co-authored-by: Copilot App <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
…ive controls (#4805)

The I5 allowance for compile's APM-home record now names the exact file core/agents_md_compile_record.py writes for the output root (.apm/compiled-agents-md/<sha256(root)[:32]>.json), and only for a non-dry-run compile. tests/quality/test_oracle_compile_record.py holds the negative controls: a dry-run compile, another root's record, and an install writing the record all fail I5; a compile writing its own root's record passes.

Co-authored-by: Copilot App <[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] Claude AGENTS.md routing follow-ups from #4005 review (pack source plan, compile reminder, managed_section, compile --root, shim ordering, perf)

2 participants