Skip to content

feat: PostCompact hook to recover state after context compaction #1047

@qozle

Description

@qozle

Problem

When Claude Code compacts conversation context, all dynamic state injected by LoadContext at session start is lost:

  • Active work sessions and their progress
  • Algorithm phase and ISC criteria
  • Learning signal trends
  • Key behavioral rules that prevent common violations

The model continues operating but has amnesia about what it was doing, leading to:

  • Restarting completed work
  • Losing Algorithm phase mid-execution
  • Violating rules it was following before compaction

Related to #798 (PostCompactRecovery) and #793 (compaction context loss).

Proposed fix

Add a PostCompact hook that re-injects critical state from disk:

  1. Active work sessions — from MEMORY/STATE/work.json (last 48h)
  2. Learning signal trends — from MEMORY/STATE/learning-cache.sh
  3. Key behavioral rules — hardcoded essentials that prevent common post-compaction violations
  4. Algorithm state — current phase, ISC criteria, PRD path from most recent MEMORY/WORK/ entry

All data comes from small cached files — runs in <50ms with no inference calls.

Output is a <system-reminder> block written to stdout, which Claude Code injects into the conversation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions