Refactor hooks and worker service for improved session management and logging

- Updated new-hook.js, save-hook.js, and summary-hook.js to enhance logging and session handling.
- Simplified session auto-creation logic in worker-service.ts to prioritize observation data preservation.
- Added a blocking wait in ensureWorkerRunning function to prevent race conditions during worker startup.
- Improved error handling and logging consistency across hooks.
This commit is contained in:
Alex Newman
2025-11-04 15:28:45 -05:00
parent 1b9cec2a95
commit 9fb43d8b06
6 changed files with 53 additions and 75 deletions
+6 -36
View File
@@ -250,24 +250,9 @@ class WorkerService {
let session = this.sessions.get(sessionDbId);
if (!session) {
// INTENTIONAL: Auto-create session to preserve observation data
//
// Design rationale:
// - Session IDs come from Claude Code's hook system and are unified across all hooks
// - UserPromptSubmit (new-hook) normally creates the session first
// - BUT if new-hook fails, subsequent hooks (save-hook, summary-hook) still fire
// - Without auto-creation, all observations from that session would be LOST
//
// Data preservation priority:
// - Observations (tool usage, decisions, bugs, features) are the valuable data
// - Session metadata is just organizational
// - Better to have "orphaned" observations than no observations at all
// - Can query observations later and see sets without userPromptSubmit (that's OK)
//
// Why non-null assertion is safe:
// - If session doesn't exist in DB, it means hooks are firing without new-hook
// - This creates the session record inline to preserve the observation data
// - Session ID is guaranteed valid (comes from Claude Code)
// Auto-create session if not in memory (worker restart, etc.)
// Sessions are organizational metadata - observations are first-class data in vector store
// Session ID comes from Claude Code hooks (guaranteed valid)
const db = new SessionStore();
const dbSession = db.getSessionById(sessionDbId);
db.close();
@@ -324,24 +309,9 @@ class WorkerService {
let session = this.sessions.get(sessionDbId);
if (!session) {
// INTENTIONAL: Auto-create session to preserve observation data
//
// Design rationale:
// - Session IDs come from Claude Code's hook system and are unified across all hooks
// - UserPromptSubmit (new-hook) normally creates the session first
// - BUT if new-hook fails, subsequent hooks (save-hook, summary-hook) still fire
// - Without auto-creation, all observations from that session would be LOST
//
// Data preservation priority:
// - Observations (tool usage, decisions, bugs, features) are the valuable data
// - Session metadata is just organizational
// - Better to have "orphaned" observations than no observations at all
// - Can query observations later and see sets without userPromptSubmit (that's OK)
//
// Why non-null assertion is safe:
// - If session doesn't exist in DB, it means hooks are firing without new-hook
// - This creates the session record inline to preserve the observation data
// - Session ID is guaranteed valid (comes from Claude Code)
// Auto-create session if not in memory (worker restart, etc.)
// Sessions are organizational metadata - observations are first-class data in vector store
// Session ID comes from Claude Code hooks (guaranteed valid)
const db = new SessionStore();
const dbSession = db.getSessionById(sessionDbId);
db.close();
+8
View File
@@ -35,6 +35,14 @@ export function ensureWorkerRunning(): void {
cwd: packageRoot,
stdio: 'ignore'
});
// Give PM2 200ms to actually start the worker
// Prevents race condition where hooks fire before worker is ready
// Simple blocking wait - no complex health checks needed
const start = Date.now();
while (Date.now() - start < 200) {
// Busy wait
}
}
});
}