Skip to content

feat: Add context-safety-net plugin to mitigate auto-compact context loss - #80883

Open
jeshiomurmu wants to merge 1 commit into
anthropics:mainfrom
jeshiomurmu:feat/context-safety-net-plugin
Open

feat: Add context-safety-net plugin to mitigate auto-compact context loss#80883
jeshiomurmu wants to merge 1 commit into
anthropics:mainfrom
jeshiomurmu:feat/context-safety-net-plugin

Conversation

@jeshiomurmu

Copy link
Copy Markdown

Problem

Auto-compaction in long sessions frequently causes silent context degradation (e.g., issue #42542, #13112, #28721). Users lose track of critical "anchor" files, and the agent continues working blind. Currently, there is no deterministic, first-party way to recover this state without manually restarting the session or relying on brittle LLM summary parsing.

Solution

This PR introduces the context-safety-net plugin. It provides a deterministic, user-controlled safety net for compaction events.

Key Design Principles (Why this is safe to merge)

  • Zero Core Changes: Built entirely on the existing PreCompact/PostCompact hook APIs and Statusline API. No modifications to the agent loop or compaction internals.
  • Read-Only by Default: The PreCompact hook captures state using git diff and git ls-files. It does not use git stash or mutate repository state.
  • Deterministic, Not Probabilistic: The PostCompact hook performs set arithmetic on file metadata. It does not parse natural language summaries.
  • Explicit Agency: The plugin does not inject system messages or automatically reload files. It outputs a user-visible warning and exposes explicit slash commands (/snapshot, /restore, /compare) for recovery.
  • Structured Config: Uses a schema-validatable .claude/context-anchors.json instead of plain text.
  • Zero External Dependencies: Bash scripts use native grep/sed for JSON parsing, ensuring cross-platform compatibility (macOS, Linux, Windows Git Bash) without requiring users to install jq.

How it Works

  • Before Compaction: The PreCompact hook snapshots the git state and anchor files to ~/.claude/context-safety-net/.
  • After Compaction: The PostCompact hook checks if anchor files are missing from the working directory.
  • User Notification: If missing, it prints a clean warning: ⚠️ Anchor files missing... Run /project:restore to reload.
  • Recovery: The user runs /project:restore <name> to explicitly instruct Claude to re-read the anchor files into context.

Reviewer Notes

  • Review surface: ~400 lines (Bash + Markdown).
  • Risk: Near-zero. Fully opt-in, hooks are additive, no new APIs required.
  • Issue Closure: Directly addresses the recovery mechanics requested in multiple compaction-related issues.

Checklist

  • Plugin is opt-in and scoped to plugins/context-safety-net/
  • Hooks use read-only git operations
  • No system message injection or hidden context manipulation
  • Anchor configuration is structured JSON

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