Repository navigation
Description
When automatic compaction happens in the middle of an agent run, the stdout of a SessionStart hook with matcher compact runs but is not shown to the model in that run.
It is queued and only injected when the next user prompt starts a new run.
In print mode (-p) that next prompt is a new process, so the text is lost.
Steps (pi-code 1.7.0, pi 1.5.2; the code is unchanged on v1.8.0 and main):
- User settings (
$CLAUDE_CONFIG_DIR/settings.json): one SessionStart hook, matchercompact, commandecho RESUME-MARKER-TEXT. - Project
.pi/settings.jsonthat compacts early:{"compaction": {"keepRecentTokens": 5000, "modelOverrides": {"anthropic/claude-haiku-5-5": {"reserveTokens": 975000}}}}. pi --approve -ne -e <pi-code>/extensions/hooks/index.ts -nc --model anthropic/claude-haiku-5-5 --thinking low -p "Call the bash tool fifteen times, one call per turn. On call number N (1 to 15) run exactly: seq 1 900; echo CALL N OF 15. Do not write any text between the calls. After the call that prints CALL 15 OF 15, reply with the single word DONE."- Send a second prompt to the same session, once with
--session <id> -p "..."and once in the same process (RPC mode, twopromptcommands).
Expected: after the compaction, RESUME-MARKER-TEXT reaches the model before its next call; the handler's own comment describes this hook as firing "when the session continues after compaction".
Actual:
- The session file has one
compactionentry (threshold, tokensBefore 25891) afterCALL 11 OF 15. The model then makes four more bash calls (CALL 12toCALL 15) and replies DONE. RESUME-MARKER-TEXT appears nowhere in the file. - Second prompt via
--session <id> -p: still absent. The new process fires SessionStart with sourceresume, which resets the queue. - Second prompt in the same RPC process: a
custom_messagewith customTypeclaude-hook-contextcontaining RESUME-MARKER-TEXT is written right after the new user message, one prompt late.
Cause, as far as the code shows (extensions/hooks/index.ts):
the session_compact handler runs the SessionStart compact hooks and pushes their stdout to pendingSessionContext.
Only the before_agent_start handler drains that array, which fires when a user prompt starts a run, not on the turns that follow a compaction inside a run.
The model_select handler uses the same queue, so PostModelSwitch output in the middle of a run likely has the same delay (not tested).
Why it matters: the usual reason for a compact SessionStart hook is to give the model a "you were just compacted, re-read X before continuing" notice.
With this behaviour that notice never reaches an agent that is working on its own across a compaction, which is the case where it is needed most.
It also arrives stale when the user next types, and by the code one copy per compaction since the last prompt (push).
Suggested direction: when session_compact fires while the agent is streaming, deliver the hook output right away as a steering message, for example pi.sendMessage({ customType: 'claude-hook-context', content, display: false }, { deliverAs: 'steer' }), which pi delivers before the next LLM call.
Keep the before_agent_start path for compactions that happen while idle.
Flushing pendingSessionContext at the next turn boundary would also work.
Activity