Feature request: agent-invoked /compact with preservation control + PostCompact hook + exported $CLAUDE_SESSION_ID
Feature request: agent-invoked /compact with preservation control + PostCompact hook + exported $CLAUDE_SESSION_ID
Summary
Three small, independent additions that together would make agent-managed context lifecycle (checkpoint → reset → resume) safe and autonomous:
- Agent-invokable
/compactwith preservation instructions — let an agent proactively compact, specifying what to keep (analogous to the user-facing/compact <instructions>form, but callable as a tool). - A
PostCompact(and ideallyPostClear) hook event — fires after compaction/clear completes, so a hook can re-inject a structured "here's the state you must preserve" block. - Exported
$CLAUDE_SESSION_ID(or equivalent) — so a hook/skill can target a session-scoped checkpoint file.
Why these three together
In a production agentic-coding harness that runs multi-hour continuous sessions, context accumulates to 80%+ and automatic compaction degrades quality (context rot). The user-level mitigations we've shipped (lowered auto-compact threshold via CLAUDE_AUTOCOMPACT_PCT_OVERRIDE, a "Compact Instructions" CLAUDE.md section, a PostToolUse rolling-checkpoint hook) help at the margin. The next level — having the agent itself manage context lifecycle (e.g. "checkpoint my state, /clear, resume from the checkpoint") — is currently blocked because:
- The agent cannot invoke
/clearor/compactprogrammatically; they are user-only commands. - There is no hook event after
/clearor compaction completes, so even if/clearhappened, nothing can re-inject the saved state. $CLAUDE_SESSION_IDis not exported to hooks/skills, so a checkpoint file can't be reliably session-scoped.
Without all three, the only way to do agent-managed context lifecycle is a semi-manual flow ("agent writes checkpoint → user types /clear → user re-prompts → agent resumes") which defeats the autonomy.
What we'd build with these
A /checkpoint-and-reset skill that: (1) writes a structured session-state JSON (in-flight audit markers, current task + branch, uncommitted-work flag, last N edits, next action) to a session-scoped path; (2) invokes /compact (or /clear) with preservation instructions; (3) a PostCompact hook reads the JSON and re-injects it as a system-reminder so the post-reset agent picks up exactly where it left off — with the load-bearing enforcement state (audit markers, pending-findings) intact rather than lost to a lossy summarizer pass.
The critical detail: the enforcement-state preservation is what makes this safe. Today, a /clear-then-resume flow would orphan the audit-discipline markers (claude-audit-active-*, claude-audit-findings-pending-*, etc.) and the post-reset agent would silently skip an enforced step. A PostCompact hook that re-injects "you have a pending audit-findings capture; do it before any edit" closes that gap.
Suggested minimal API
/compactas a tool the agent can call:compact({preserve: "audit markers, current task, uncommitted-work flag"})→ returns when compaction completes.- New hook event
PostCompact(andPostClear) insettings.jsonhooks config, payload includes the pre-compaction context summary so the hook can decide what to re-add. $CLAUDE_SESSION_IDexported to all hook subprocess environments and available to skills (alongside$CLAUDE_PROJECT_DIRwhich is already exported).
Distinguishing this from related issues
- Distinct from #58103 (user-configurable cache breakpoints) — that's about prompt-cache hit rate; this is about context lifecycle control.
- Distinct from #58112 (skill-catalog re-injection) — that's a specific over-injection bug; this is a new capability.
- Complementary to the existing
contextCompactionThreshold/CLAUDE_AUTOCOMPACT_PCT_OVERRIDEknob (which controls when auto-compaction triggers) — this is about giving the agent agency over the lifecycle, not just tuning the threshold.
Reference reading
- https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents (context resets vs compaction)
- https://www.anthropic.com/engineering/harness-design-long-running-apps (structured handoff artifacts)
- https://code.claude.com/docs/en/hooks (current hook events — no PostCompact)
- https://code.claude.com/docs/en/memory (Compact Instructions section)
- https://www.trychroma.com/research/context-rot
Happy to provide our internal failure-log entries on context-leak patterns + the design analysis if useful.
🤖 Filed by Claude Opus 4.7 (1M context) acting on behalf of the Qapnova engineering team.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗