claude-code - 💡(How to fix) Fix Feature request: agent-invoked /compact with preservation control + PostCompact hook + exported $CLAUDE_SESSION_ID

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Three small, independent additions that together would make agent-managed context lifecycle (checkpoint → reset → resume) safe and autonomous:

  1. Agent-invokable /compact with preservation instructions — let an agent proactively compact, specifying what to keep (analogous to the user-facing /compact <instructions> form, but callable as a tool).
  2. A PostCompact (and ideally PostClear) hook event — fires after compaction/clear completes, so a hook can re-inject a structured "here's the state you must preserve" block.
  3. Exported $CLAUDE_SESSION_ID (or equivalent) — so a hook/skill can target a session-scoped checkpoint file.

Root Cause

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:

Fix Action

Fix / Workaround

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:

RAW_BUFFERClick to expand / collapse

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:

  1. Agent-invokable /compact with preservation instructions — let an agent proactively compact, specifying what to keep (analogous to the user-facing /compact <instructions> form, but callable as a tool).
  2. A PostCompact (and ideally PostClear) hook event — fires after compaction/clear completes, so a hook can re-inject a structured "here's the state you must preserve" block.
  3. 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 /clear or /compact programmatically; they are user-only commands.
  • There is no hook event after /clear or compaction completes, so even if /clear happened, nothing can re-inject the saved state.
  • $CLAUDE_SESSION_ID is 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

  • /compact as a tool the agent can call: compact({preserve: "audit markers, current task, uncommitted-work flag"}) → returns when compaction completes.
  • New hook event PostCompact (and PostClear) in settings.json hooks config, payload includes the pre-compaction context summary so the hook can decide what to re-add.
  • $CLAUDE_SESSION_ID exported to all hook subprocess environments and available to skills (alongside $CLAUDE_PROJECT_DIR which 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_OVERRIDE knob (which controls when auto-compaction triggers) — this is about giving the agent agency over the lifecycle, not just tuning the threshold.

Reference reading

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.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING