Fix critical session persistence bug in new-hook (#224)

* Fix critical session persistence bug in new-hook

Restores the second POST call to /sessions/:sessionDbId/init that was
incorrectly removed as "duplicate" in the hooks refactor.

The two POST calls serve different purposes:
- POST /api/sessions/init: Creates DB session, saves prompt (no SDK agent)
- POST /sessions/:sessionDbId/init: Starts the SDK agent session

Without the second call, the SDK agent never started, causing:
- First prompts saved to DB but never processed
- Subsequent prompts queued but no agent running to consume them
- Each new prompt creating orphaned sessions instead of continuing

This fix restores proper session continuation across multiple prompts.

Fixes conversation fragmentation reported in production.

* updated agent-sdk

* Skip meta-observations for session-memory file operations

Added a check to skip meta-observations when file operations (Edit, Write, Read, NotebookEdit) are performed on session-memory files. If the file path includes 'session-memory', a debug log is generated and the response indicates that the observation was skipped.
This commit is contained in:
Alex Newman
2025-12-10 14:24:19 -05:00
committed by GitHub
parent 41fbb87aa0
commit 8bdec6abc0
6 changed files with 218 additions and 91 deletions
+21
View File
@@ -71,6 +71,27 @@ async function newHook(input?: UserPromptSubmitInput): Promise<void> {
handleWorkerError(error);
}
// Strip leading slash from commands for memory agent
// /review 101 → review 101 (more semantic for observations)
const cleanedPrompt = prompt.startsWith('/') ? prompt.substring(1) : prompt;
try {
// Initialize SDK agent session via HTTP (starts the agent!)
const response = await fetch(`http://127.0.0.1:${port}/sessions/${sessionDbId}/init`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ userPrompt: cleanedPrompt, promptNumber }),
signal: AbortSignal.timeout(5000)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to start SDK agent: ${response.status} ${errorText}`);
}
} catch (error: any) {
handleWorkerError(error);
}
console.log(createHookResponse('UserPromptSubmit', true));
}
@@ -275,6 +275,25 @@ export class SessionRoutes extends BaseRouteHandler {
return;
}
// Skip meta-observations: file operations on session-memory files
const fileOperationTools = new Set(['Edit', 'Write', 'Read', 'NotebookEdit']);
if (fileOperationTools.has(tool_name) && tool_input) {
try {
const filePath = tool_input.file_path || tool_input.notebook_path;
if (filePath && filePath.includes('session-memory')) {
logger.debug('SESSION', 'Skipping meta-observation for session-memory file', {
tool_name,
file_path: filePath
});
res.json({ status: 'skipped', reason: 'session_memory_meta' });
return;
}
} catch (error) {
// If we can't parse tool_input, continue normally
logger.debug('SESSION', 'Could not check file_path for session-memory filter', { tool_name }, error);
}
}
const store = this.dbManager.getSessionStore();
// Get or create session