write the claude session id through instead of debouncing it

the sessionKey to claude uuid map is what --resume needs to reattach a
conversation after the agent sidecar dies, so a 30s debounce put exactly
the wrong state behind a window. flushAndSave on SIGTERM covers a pm2
restart but not a crash or SIGKILL, which is the case resume exists for.
an equality guard keeps onSessionId from thrashing the file.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-06 22:49:32 +00:00
co-authored by Claude Opus 5
parent 143a6453d3
commit f4be4fd431
+22 -2
View File
@@ -71,14 +71,34 @@ export function updateState(patch: Partial<PersistedState>): void {
scheduleSave();
}
/**
* Both of these write through instead of debouncing.
*
* This map is the only thing that lets a conversation be reattached with `--resume` after the agent
* sidecar dies, so it is precisely the state that must not be in a 30s window when the process is
* killed rather than asked to stop — `flushAndSave` on SIGTERM covers a `pm2 restart`, but not a
* crash or a SIGKILL, which is the case the resume path exists for. Losing it silently starts the
* next turn as a fresh Claude session and orphans the transcript on disk.
*
* There is nothing to debounce: `onSessionId` fires on every message but with the same id, so the
* equality guard collapses it to one write per session, and clearing happens once.
*/
export function setClaudeSession(sessionKey: string, sessionId: string): void {
if (currentState.claudeSessions[sessionKey] === sessionId) return;
currentState.claudeSessions[sessionKey] = sessionId;
scheduleSave();
writeThrough();
}
export function clearClaudeSession(sessionKey: string): void {
if (!(sessionKey in currentState.claudeSessions)) return;
delete currentState.claudeSessions[sessionKey];
scheduleSave();
writeThrough();
}
function writeThrough(): void {
void flushAndSave().catch((err) => {
console.error('[claude-state] save failed:', err instanceof Error ? err.message : err);
});
}
export function getClaudeSession(sessionKey: string): string | undefined {