Feature Request: Allow Stop hook to clear context and continuing (for autonomous long-running tasks without compaction)
Feature Request: Allow Stop hook to clear context and continuing (for autonomous long-running tasks without compaction)
Problem
The official guidance is clear: compaction is lossy and fresh sessions with artifacts are preferred.
"Starting fresh vs compacting: When a context window is cleared, consider starting with a brand new context window rather than using compaction. Claude 4.5 models are extremely effective at discovering state from the local filesystem." — Anthropic Prompting Best Practices
"Break tasks into discrete sessions where each agent instance starts fresh but uses artifacts (e.g., git history, progress files) to bridge context." — Effective Harnesses for Long-Running Agents
For human-supervised sessions, this is easy: /clear and re-prompt manually.
For autonomous long-running tasks (like those using Ralph Wiggum), there is no way to achieve this programmatically. The only option is auto-compact, which contradicts the guidance.
What Exists (90% of the Pattern)
The Stop hook already supports:
decision: "block"— prevent exit, feedreasonas next input (this is how Ralph Wiggum loops)- Context window data exposed via stdin JSON (
context_window.total_input_tokens,context_window.context_window_size)
So a Stop hook can:
- ✅ Detect "context is getting full"
- ✅ Build a rollover prompt from git history / state files
- ❌ Clear context and feed that prompt — not possible
Proposed Solution
Add decision: "clear" to Stop hook:
{
"decision": "clear",
"reason": "Continue building feature X. Previous work: <git log summary>. Remaining: <from state file>."
}
Behavior:
- Clear context (equivalent to
/clear) - Feed
reasonas the first input in fresh context - Session continues seamlessly
Why This Matters
- Completes an existing pattern — Ralph Wiggum proves Stop hook looping works. This just adds the "fresh context" variant.
- Enables lossless long-running tasks — Currently impossible without external orchestration or lossy compaction.
- Aligns tooling with guidance — If "fresh sessions > compaction" is the recommendation, the hook system should support it.
- Minimal change — One new decision value for an existing hook. No new hook types, no new APIs.
Current Workarounds (All Inadequate)
| Approach | Problem |
|----------|---------|
| Auto-compact | Lossy, "summary of a summary" degradation |
| External orchestrator | Heavy, requires daemon/process manager outside Claude Code |
| Manual /clear | Not autonomous, defeats purpose of long-running tasks |
| Stop hook + block | Can loop, but context keeps growing until compaction |
Example Use Case
Ralph Wiggum with rollover (if this feature existed):
# In stop-hook.sh
CONTEXT_PCT=$(echo "$HOOK_INPUT" | jq ".context_window.total_input_tokens / .context_window.context_window_size * 100")
if (( $(echo "$CONTEXT_PCT > 80" | bc -l) )); then
# Build rollover prompt from filesystem state
ROLLOVER_PROMPT="Previous work: $(git log --oneline -10)
Remaining tasks: $(cat .claude/tasks.md)
Original goal: $ORIGINAL_PROMPT"
echo "{\"decision\": \"clear\", \"reason\": \"$ROLLOVER_PROMPT\"}"
else
echo "{\"decision\": \"block\", \"reason\": \"$ORIGINAL_PROMPT\"}"
fi
This would enable lossless autonomous loops with zero external tooling.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗