fix: Auto-create sessions in worker when observations/summaries arrive

The worker was still returning 404 "Session not found" when trying to add
observations or summaries. This happened if:
- Worker restarted (sessions lost from memory)
- Race condition where observations arrive before /init
- Session continued after /exit

Changes:
- handleObservation: Auto-create session if not in memory map
- handleSummarize: Auto-create session if not in memory map

This completes the "no validation" philosophy: worker accepts any session_id
and auto-creates the session state if needed. Saves ALL data without blockage.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Alex Newman
2025-10-21 21:46:50 -04:00
parent c27f07023c
commit 635cc87462
2 changed files with 61 additions and 19 deletions
File diff suppressed because one or more lines are too long
+48 -6
View File
@@ -164,10 +164,31 @@ class WorkerService {
const sessionDbId = parseInt(req.params.sessionDbId, 10);
const { tool_name, tool_input, tool_output, prompt_number } = req.body;
const session = this.sessions.get(sessionDbId);
let session = this.sessions.get(sessionDbId);
if (!session) {
res.status(404).json({ error: 'Session not found' });
return;
// Auto-create session if it doesn't exist (e.g., worker restarted)
session = {
sessionDbId,
sdkSessionId: null,
project: '',
userPrompt: '',
pendingMessages: [],
abortController: new AbortController(),
generatorPromise: null,
lastPromptNumber: 0,
observationCounter: 0,
startTime: Date.now()
};
this.sessions.set(sessionDbId, session);
// Start SDK agent in background
session.generatorPromise = this.runSDKAgent(session).catch(err => {
logger.failure('WORKER', 'SDK agent error', { sessionId: sessionDbId }, err);
const db = new SessionStore();
db.markSessionFailed(sessionDbId);
db.close();
this.sessions.delete(sessionDbId);
});
}
// Create correlation ID for tracking this observation
@@ -199,10 +220,31 @@ class WorkerService {
const sessionDbId = parseInt(req.params.sessionDbId, 10);
const { prompt_number } = req.body;
const session = this.sessions.get(sessionDbId);
let session = this.sessions.get(sessionDbId);
if (!session) {
res.status(404).json({ error: 'Session not found' });
return;
// Auto-create session if it doesn't exist (e.g., worker restarted)
session = {
sessionDbId,
sdkSessionId: null,
project: '',
userPrompt: '',
pendingMessages: [],
abortController: new AbortController(),
generatorPromise: null,
lastPromptNumber: 0,
observationCounter: 0,
startTime: Date.now()
};
this.sessions.set(sessionDbId, session);
// Start SDK agent in background
session.generatorPromise = this.runSDKAgent(session).catch(err => {
logger.failure('WORKER', 'SDK agent error', { sessionId: sessionDbId }, err);
const db = new SessionStore();
db.markSessionFailed(sessionDbId);
db.close();
this.sessions.delete(sessionDbId);
});
}
logger.dataIn('WORKER', 'Summary requested', {