fix(todo-continuation-enforcer): prevent post-compaction agent fallback to General

After compaction, message history is truncated and the original agent
(e.g. Prometheus) can no longer be resolved from messages. The todo
continuation enforcer would then inject a continuation prompt with
agent=undefined, causing the host to default to General -- which has
write permissions Prometheus should never have.

Root cause chain:
1. handler.ts had no session.compacted handler (unlike Atlas)
2. idle-event.ts relied on finding a compaction marker in truncated
   message history -- the marker disappears after real compaction
3. continuation-injection.ts proceeded when agentName was undefined
   because the skipAgents check only matched truthy agent names
4. prometheus-md-only/agent-resolution.ts did not filter compaction
   agent from message history fallback results

Fixes:
- Add session.compacted handler that sets hasRecentCompaction state flag
- Replace fragile history-based compaction detection with state flag
- Block continuation injection when agent is unknown post-compaction
- Filter compaction agent in Prometheus agent resolution fallback
This commit is contained in:
conversun
2026-03-13 00:29:34 +08:00
parent 0412e40780
commit 22b4b30dd7
5 changed files with 34 additions and 7 deletions
@@ -120,6 +120,14 @@ export async function injectContinuation(args: {
return
}
if (!agentName) {
const compactionState = sessionStateStore.getExistingState(sessionID)
if (compactionState?.hasRecentCompaction) {
log(`[${HOOK_NAME}] Skipped: agent unknown after compaction`, { sessionID })
return
}
}
if (!hasWritePermission(tools)) {
log(`[${HOOK_NAME}] Skipped: agent lacks write permission`, { sessionID, agent: agentName })
return