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:
File diff suppressed because one or more lines are too long
@@ -164,10 +164,31 @@ class WorkerService {
|
|||||||
const sessionDbId = parseInt(req.params.sessionDbId, 10);
|
const sessionDbId = parseInt(req.params.sessionDbId, 10);
|
||||||
const { tool_name, tool_input, tool_output, prompt_number } = req.body;
|
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) {
|
if (!session) {
|
||||||
res.status(404).json({ error: 'Session not found' });
|
// Auto-create session if it doesn't exist (e.g., worker restarted)
|
||||||
return;
|
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
|
// Create correlation ID for tracking this observation
|
||||||
@@ -199,10 +220,31 @@ class WorkerService {
|
|||||||
const sessionDbId = parseInt(req.params.sessionDbId, 10);
|
const sessionDbId = parseInt(req.params.sessionDbId, 10);
|
||||||
const { prompt_number } = req.body;
|
const { prompt_number } = req.body;
|
||||||
|
|
||||||
const session = this.sessions.get(sessionDbId);
|
let session = this.sessions.get(sessionDbId);
|
||||||
if (!session) {
|
if (!session) {
|
||||||
res.status(404).json({ error: 'Session not found' });
|
// Auto-create session if it doesn't exist (e.g., worker restarted)
|
||||||
return;
|
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', {
|
logger.dataIn('WORKER', 'Summary requested', {
|
||||||
|
|||||||
Reference in New Issue
Block a user