Skip to content

fix(context-usage): skip the main-session .compacted marker on a subagent compaction - #17994

Merged
melodic-automation-lanes[bot] merged 1 commit into
mainfrom
work-resolve-issue/issue-17899-44933040273-1
Nov 21, 2026
Merged

melodic-automation-lanes[bot] merged 1 commit into
mainfrom
work-resolve-issue/issue-17899-44933040273-1

Conversation

@melodic-automation-lanes

Copy link
Copy Markdown
Contributor

Closes #17899

Summary

Opened by the work-resolve-issue lane. The run's own summary:

post-compact-mark.sh now exits without writing the main session's .compacted marker when the PostCompact payload carries a non-empty agent_id. Claude Code 2.1.323 fixed PostCompact not receiving agent_id/agent_type when a subagent compacts (changelog read this run). The hooks page lists agent_id as a common input field set for the subagent or in-process teammate the hook fires in. A main session under --agent sends agent_type with no agent_id, so the check keys on agent_id alone and that session still gets its marker. The guard is a jq-free regex like the hook's existing session_id read: an escaped "agent_id" inside compact_summary cannot match, and an empty or null agent_id counts as absent. The reader contract and a patch changelog fragment state the skip and its version floor.

Not verified

  • This lane cannot run interpreters, so I did not run post-compact-mark.test.sh myself, and check-affected's output does not name it among its passes. Left to the PR's CI.
  • check-affected skipped comment-hygiene (the ci-workflows fetch failed), size-check (script missing in the lane), typos (not installed) and three validate-plugins sub-checks that run code from the checked tree. comment-lint runs only in CI. The new hook comment links its outside source, which the…
  • No live probe of a subagent PostCompact payload. The done-when allows 'a probe (or the hooks page)', and I used the 2.1.323 changelog entry plus the hooks page's common-field definition instead.
  • The ai-slop deterministic detector (detect.sh) was not run in the tidy pass because the lane allows only the named wrappers. I read the changed markdown lines against the rubric by hand and found nothing.
  • The early exit skips the hook's telemetry for a subagent compaction, the same as its other skip exits. Operators will not see subagent compactions in post-compact-mark telemetry.
  • An in-process teammate compaction also carries agent_id, so it is skipped too. The upstream docs do not say whether a teammate has its own session_id, which would make that marker its own.

Fix

  • plugins/context-usage/hooks/post-compact-mark.sh: exits 0 before any write when the payload has a non-empty agent_id string, with a comment that links the 2.1.323 changelog entry
  • plugins/context-usage/hooks/post-compact-mark.test.sh: three new cases: a subagent payload (agent_id + agent_type) writes no marker, a main-session --agent payload (agent_type only) still writes it, and an escaped agent_id inside compact_summary does not suppress it
  • plugins/context-usage/reference/reader-contract.md: the Evidence-degraded marker section says a subagent compaction writes no marker on 2.1.323 and later, and that earlier builds still mark the main session; the trigger paragraph now says 'whatever the trigger'
  • .changes/context-usage/work-resolve-issue-issue-17899-44933040273-1-9f51552a.md: patch fragment under Fixed

Verification

The runner ran each check from the base copy against 2160af1:

  • scripts/check-changelog-fragments.sh --check: passed
  • scripts/check-changed-skills.sh --merge-base: passed

The run reports:

  • read https: //raw.githubusercontent.com/anthropics/claude-code/main/CHANGELOG.md: the 2.1.323 section lists 'Fixed PreCompact, PostCompact and post-compaction SessionStart hooks not receiving agent_id and agent_type when a subagent compacts'
  • read https: //code.claude.com/docs/en/hooks: agent_id is a common input field, 'Unique identifier for the subagent or in-process teammate the hook fires in'; agent_type is present under --agent or inside a subagent; PostCompact input is the common fields plus trigger and compact_summary (its…
  • read https: //code.claude.com/docs/en/changelog#2-1-323: the docs changelog page was cut off before 2.1.323, so the raw GitHub CHANGELOG above is the source for the entry
  • check-changelog-fragments: all 11 fragments valid (run before and after tidy and the review fixes)
  • check-changed-skills: no changed skills, nothing to gate
  • check-affected: 15 passed, 8 skipped over 4 changed files (final run after the review fixes)
  • fresh-context review subagent over the full diff: no correctness bugs; its four wording and lint findings were applied (test header comment change reverted, version floor added to the contract, 'only' dropped from the hook comment, 'whatever the trigger' in the contract)
  • The lane run: https://github.com/melodic-software/claude-code-plugins/actions/runs/44933040273

Left out: the items under Not verified.

Related

#17899

Lane-Activity: work-resolve-issue/resolve-issue
@github-actions

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 2160af1aac25818ce7a4af2aef6b46d3b35c105c: read 4 of 4 changed file(s); 0 finding(s), 0 as inline comments.

Security review: no findings

I read the whole diff (4 files) and found no security issues.

What the change does: when the PostCompact payload has a non-empty agent_id, plugins/context-usage/hooks/post-compact-mark.sh now exits 0 before writing anything (L22-L24). The other three files are a test file, a reference doc and a changelog fragment.

Why nothing qualifies:

  • Input and matching: the new guard is a bash regex over the hook's stdin payload. It only reads the payload, makes no shell call and builds no path from it. A \"agent_id\" written inside compact_summary cannot match, because the backslash before the closing quote breaks the "agent_id" pattern, and the new test at post-compact-mark.test.sh L51-L56 covers that case.
  • Who controls the input: Claude Code writes the payload, not an outside author, and no untrusted text reaches a credentialed lane.
  • What the marker is: the .compacted marker is local evidence that context was lost. It is not an authorization or merge control. Skipping it for a subagent compaction widens no trust, token scope, kill switch or merge hold.
  • Nothing else touched: no trusted path, plugin manifest or standing instruction changed. reference/reader-contract.md only narrows its description to match the new behavior.

critical-findings: 0

@github-actions

Copy link
Copy Markdown
Contributor

claude-review reviewed 2160af1aac25818ce7a4af2aef6b46d3b35c105c: read 4 of 4 changed file(s); 0 finding(s), 0 as inline comments.

Code review: PR #17994, context-usage: skip the main-session .compacted marker on a subagent compaction

No blocking or advisory findings. I read all four changed files in full: the hook, its test, the reader contract and the changelog fragment.

What changed. post-compact-mark.sh:55 now exits early when the PostCompact payload has a non-empty agent_id string. It exits before reading session_id, creating the directory or writing anything.

Correctness checks:

  • Text inside compact_summary cannot trigger the skip. In valid JSON, every " inside a string value is escaped as \". So the bare "agent_id" followed by : that the regex needs can only appear as a top-level key. The PostCompact payload has no nested objects.
  • Empty or null values count as absent. "agent_id":"" and "agent_id":null both fail the "[^"] tail, so the marker is still written. This matches what the PR body says.
  • A truncated payload fails safe. If cg::read_payload drops a trailing agent_id, the hook falls back to the old behaviour and writes the marker. It never loses a marker.
  • The new tests are sound. They follow the existing run helper and use distinct session ids. The --agent payload (agent_type with no agent_id) proves the subagent case isn't a no-op skip for some other reason. The escaped \"agent_id\" case inside the single-quoted JSON is valid JSON and checks the false-match guard.
  • No overlap with the earlier test. The printf shim from the earlier fallback test lives in a subshell, so it does not reach the new cases.

Closing issue #17899, done-when items:

  • "post-compact-mark.sh skips the main-session marker for a subagent compaction, with a test": met. The ssub test would fail if the guard broke.
  • "exits without writing the marker when agent_id is present, and post-compact-mark.test.sh covers both payloads": met. The ssub case covers the subagent payload and smain covers the main-session payload.
  • "A probe (or the hooks page) confirms…": the author says they used the 2.1.323 changelog and the hooks page, and ran no live probe. The issue allows the hooks page, so this is met as the author claims. I did not confirm those pages myself.

Author-claimed and unverified: the PR's verification list (changelog-fragment check, changed-skills check, check-affected). The author also says they did not run post-compact-mark.test.sh, and I could not run it either. It is left to CI.

Rules and other checks:

  • Path-scoped rules: .claude/rules/mod-authoring.md matches plugins/*/hooks/** but covers hooks modules. This change edits a shell hook only, so the rule doesn't apply. .claude/rules/cost-claims.md matches reference/reader-contract.md, and the change states no cost figures.
  • Loosened checks: none. The only assertions added are new ones.
  • Plugin manifests: none changed. The changelog fragment's bump: patch fits a bug fix.
  • Paved paths: I did not resolve a paved-path index. Nothing in the change adds a second way of doing something, since it reuses the hook's own jq-free regex style.

The PR body notes that telemetry is skipped on a subagent compaction, as on the hook's other skip exits. It also notes that an in-process teammate compaction is skipped too. Both are disclosed, so I raise neither.

Loosened checks would be marked: fail (default; no loosened_check_verdict: line in the prompt); patterns: 0. The prompt had no loosened_check_decision_record: line, so the citation requirement was not checked.

Verifier dropped 0 of 0 candidates.

@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

The unattended merge path is about to arm native auto-merge at head 2160af1aac25818ce7a4af2aef6b46d3b35c105c, the head its arming checks judged. The arm fails if the head moves first.

@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

Merge-Risk: max=MEDIUM demote=false head=2160af1aac25818ce7a4af2aef6b46d3b35c105c rubric=10.4.5 rater=claude-opus-5-5 checker=claude-opus-5-5

@melodic-automation-lanes
melodic-automation-lanes Bot added this pull request to the merge queue Nov 21, 2026
Merged via the queue into main with commit 5fe6666 Nov 21, 2026
51 checks passed
@melodic-automation-lanes
melodic-automation-lanes Bot deleted the work-resolve-issue/issue-17899-44933040273-1 branch November 21, 2026 18:56
@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

The post-merge check verified merge commit 5fe66662375885c8410269fcfebad949b4495597; later checks count it verified from this comment.

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.

context-usage: post-compact-mark.sh writes the main session's .compacted marker on a subagent compaction (2.1.323 agent_id)

0 participants