Standardize and enhance error handling across hooks and worker service (#295)

* Enhance error logging in hooks

- Added detailed error logging in context-hook, new-hook, save-hook, and summary-hook to capture status, project, port, and relevant session information on failures.
- Improved error messages thrown in save-hook and summary-hook to include specific context about the failure.

* Refactor migration logging to use console.log instead of console.error

- Updated SessionSearch and SessionStore classes to replace console.error with console.log for migration-related messages.
- Added notes in the documentation to clarify the use of console.log for migration messages due to the unavailability of the structured logger during constructor execution.

* Refactor SDKAgent and silent-debug utility to simplify error handling

- Updated SDKAgent to use direct defaults instead of happy_path_error__with_fallback for non-critical fields such as last_user_message, last_assistant_message, title, filesRead, filesModified, concepts, and summary.request.
- Enhanced silent-debug documentation to clarify appropriate use cases for happy_path_error__with_fallback, emphasizing its role in handling unexpected null/undefined values while discouraging its use for nullable fields with valid defaults.

* fix: correct happy_path_error__with_fallback usage to prevent false errors

Fixes false "Missing cwd" and "Missing transcript_path" errors that were
flooding silent.log even when values were present.

Root cause: happy_path_error__with_fallback was being called unconditionally
instead of only when the value was actually missing.

Pattern changed from:
  value: happy_path_error__with_fallback('Missing', {}, value || '')

To correct usage:
  value: value || happy_path_error__with_fallback('Missing', {}, '')

Fixed in:
- src/hooks/save-hook.ts (PostToolUse hook)
- src/hooks/summary-hook.ts (Stop hook)
- src/services/worker/http/routes/SessionRoutes.ts (2 instances)

Impact: Eliminates false error noise, making actual errors visible.

Addresses issue #260 - users were seeing "Missing cwd" errors despite
Claude Code correctly passing all required fields.

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* Enhance error logging and handling across services

- Improved error messages in SessionStore to include project context when fetching boundary observations and timestamps.
- Updated ChromaSync error handling to provide more informative messages regarding client initialization failures, including the project context.
- Enhanced error logging in WorkerService to include the package path when reading version fails.
- Added detailed error logging in worker-utils to capture expected and running versions during health checks.
- Extended WorkerErrorMessageOptions to include actualError for more informative restart instructions.

* Refactor error handling in hooks to use standardized fetch error handler

- Introduced a new error handler `handleFetchError` in `shared/error-handler.ts` to standardize logging and user-facing error messages for fetch failures across hooks.
- Updated `context-hook.ts`, `new-hook.ts`, `save-hook.ts`, and `summary-hook.ts` to utilize the new error handler, improving consistency and maintainability.
- Removed redundant imports and error handling logic related to worker restart instructions from the hooks.

* feat: add comprehensive error handling tests for hooks and ChromaSync client

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Alex Newman
2025-12-13 23:25:43 -05:00
committed by GitHub
parent d42ab1298c
commit 52d2f72a82
27 changed files with 1434 additions and 199 deletions
+7 -2
View File
@@ -11,7 +11,7 @@ import { stdin } from "process";
import { ensureWorkerRunning, getWorkerPort } from "../shared/worker-utils.js";
import { HOOK_TIMEOUTS } from "../shared/hook-constants.js";
import { handleWorkerError } from "../shared/hook-error-handler.js";
import { getWorkerRestartInstructions } from "../utils/error-messages.js";
import { handleFetchError } from "./shared/error-handler.js";
export interface SessionStartInput {
session_id: string;
@@ -35,7 +35,12 @@ async function contextHook(input?: SessionStartInput): Promise<string> {
if (!response.ok) {
const errorText = await response.text();
throw new Error(getWorkerRestartInstructions({ includeSkillFallback: true }));
handleFetchError(response, errorText, {
hookName: 'context',
operation: 'Context generation',
project,
port
});
}
const result = await response.text();
+14 -3
View File
@@ -4,7 +4,7 @@ import { createHookResponse } from './hook-response.js';
import { ensureWorkerRunning, getWorkerPort } from '../shared/worker-utils.js';
import { happy_path_error__with_fallback } from '../utils/silent-debug.js';
import { handleWorkerError } from '../shared/hook-error-handler.js';
import { getWorkerRestartInstructions } from '../utils/error-messages.js';
import { handleFetchError } from './shared/error-handler.js';
export interface UserPromptSubmitInput {
session_id: string;
@@ -53,7 +53,12 @@ async function newHook(input?: UserPromptSubmitInput): Promise<void> {
if (!initResponse.ok) {
const errorText = await initResponse.text();
throw new Error(getWorkerRestartInstructions({ includeSkillFallback: true }));
handleFetchError(initResponse, errorText, {
hookName: 'new',
operation: 'Session initialization',
project,
port
});
}
const initResult = await initResponse.json();
@@ -87,7 +92,13 @@ async function newHook(input?: UserPromptSubmitInput): Promise<void> {
if (!response.ok) {
const errorText = await response.text();
throw new Error(getWorkerRestartInstructions({ includeSkillFallback: true }));
handleFetchError(response, errorText, {
hookName: 'new',
operation: 'SDK agent start',
project,
port,
sessionId: String(sessionDbId)
});
}
} catch (error: any) {
handleWorkerError(error);
+10 -7
View File
@@ -13,7 +13,7 @@ import { ensureWorkerRunning, getWorkerPort } from '../shared/worker-utils.js';
import { HOOK_TIMEOUTS } from '../shared/hook-constants.js';
import { happy_path_error__with_fallback } from '../utils/silent-debug.js';
import { handleWorkerError } from '../shared/hook-error-handler.js';
import { getWorkerRestartInstructions } from '../utils/error-messages.js';
import { handleFetchError } from './shared/error-handler.js';
export interface PostToolUseInput {
session_id: string;
@@ -54,10 +54,10 @@ async function saveHook(input?: PostToolUseInput): Promise<void> {
tool_name,
tool_input,
tool_response,
cwd: happy_path_error__with_fallback(
cwd: cwd || happy_path_error__with_fallback(
'Missing cwd in PostToolUse hook input',
{ session_id, tool_name },
cwd || ''
''
)
}),
signal: AbortSignal.timeout(HOOK_TIMEOUTS.DEFAULT)
@@ -65,10 +65,13 @@ async function saveHook(input?: PostToolUseInput): Promise<void> {
if (!response.ok) {
const errorText = await response.text();
logger.failure('HOOK', 'Failed to send observation', {
status: response.status
}, errorText);
throw new Error(getWorkerRestartInstructions({ includeSkillFallback: true }));
handleFetchError(response, errorText, {
hookName: 'save',
operation: 'Observation storage',
toolName: tool_name,
sessionId: session_id,
port
});
}
logger.debug('HOOK', 'Observation sent successfully', { toolName: tool_name });
+37
View File
@@ -0,0 +1,37 @@
import { logger } from '../../utils/logger.js';
import { getWorkerRestartInstructions } from '../../utils/error-messages.js';
export interface HookErrorContext {
hookName: string;
operation: string;
project?: string;
sessionId?: string;
toolName?: string;
port?: number;
}
/**
* Standardized error handler for hook fetch failures.
*
* This function:
* 1. Logs the error with full context to worker logs
* 2. Throws a user-facing error with restart instructions
*
* Use this for all fetch errors in hooks to ensure consistent error handling.
*/
export function handleFetchError(
response: Response,
errorText: string,
context: HookErrorContext
): never {
logger.error('HOOK', `${context.operation} failed`, {
status: response.status,
...context
}, errorText);
const userMessage = context.toolName
? `Failed ${context.operation} for ${context.toolName}: ${getWorkerRestartInstructions()}`
: `${context.operation} failed: ${getWorkerRestartInstructions()}`;
throw new Error(userMessage);
}
+9 -7
View File
@@ -16,8 +16,8 @@ import { ensureWorkerRunning, getWorkerPort } from '../shared/worker-utils.js';
import { HOOK_TIMEOUTS } from '../shared/hook-constants.js';
import { happy_path_error__with_fallback } from '../utils/silent-debug.js';
import { handleWorkerError } from '../shared/hook-error-handler.js';
import { handleFetchError } from './shared/error-handler.js';
import { extractLastMessage } from '../shared/transcript-parser.js';
import { getWorkerRestartInstructions } from '../utils/error-messages.js';
export interface StopInput {
session_id: string;
@@ -41,10 +41,10 @@ async function summaryHook(input?: StopInput): Promise<void> {
const port = getWorkerPort();
// Extract last user AND assistant messages from transcript
const transcriptPath = happy_path_error__with_fallback(
const transcriptPath = input.transcript_path || happy_path_error__with_fallback(
'Missing transcript_path in Stop hook input',
{ session_id },
input.transcript_path || ''
''
);
const lastUserMessage = extractLastMessage(transcriptPath, 'user');
const lastAssistantMessage = extractLastMessage(transcriptPath, 'assistant', true);
@@ -70,10 +70,12 @@ async function summaryHook(input?: StopInput): Promise<void> {
if (!response.ok) {
const errorText = await response.text();
logger.failure('HOOK', 'Failed to generate summary', {
status: response.status
}, errorText);
throw new Error(getWorkerRestartInstructions({ includeSkillFallback: true }));
handleFetchError(response, errorText, {
hookName: 'summary',
operation: 'Summary generation',
sessionId: session_id,
port
});
}
logger.debug('HOOK', 'Summary request sent successfully');
+5 -2
View File
@@ -44,6 +44,9 @@ export class SessionSearch {
* - Tables maintained but search paths removed
* - Triggers still fire to keep tables synchronized
*
* Note: Using console.log for migration messages since they run during constructor
* before structured logger is available. Actual errors use console.error.
*
* TODO: Remove FTS5 infrastructure in future major version (v7.0.0)
*/
private ensureFTSTables(): void {
@@ -57,7 +60,7 @@ export class SessionSearch {
return;
}
console.error('[SessionSearch] Creating FTS5 tables...');
console.log('[SessionSearch] Creating FTS5 tables...');
// Create observations_fts virtual table
this.db.run(`
@@ -141,7 +144,7 @@ export class SessionSearch {
END;
`);
console.error('[SessionSearch] FTS5 tables created successfully');
console.log('[SessionSearch] FTS5 tables created successfully');
} catch (error: any) {
console.error('[SessionSearch] FTS migration error:', error.message);
}
+24 -21
View File
@@ -45,6 +45,9 @@ export class SessionStore {
/**
* Initialize database schema using migrations (migration004)
* This runs the core SDK tables migration if no tables exist
*
* Note: Using console.log for migration messages since they run during constructor
* before structured logger is available. Actual errors use console.error.
*/
private initializeSchema(): void {
try {
@@ -64,7 +67,7 @@ export class SessionStore {
// Only run migration004 if no migrations have been applied
// This creates the sdk_sessions, observations, and session_summaries tables
if (maxApplied === 0) {
console.error('[SessionStore] Initializing fresh database with migration004...');
console.log('[SessionStore] Initializing fresh database with migration004...');
// Migration004: SDK agent architecture tables
this.db.run(`
@@ -128,7 +131,7 @@ export class SessionStore {
// Record migration004 as applied
this.db.prepare('INSERT INTO schema_versions (version, applied_at) VALUES (?, ?)').run(4, new Date().toISOString());
console.error('[SessionStore] Migration004 applied successfully');
console.log('[SessionStore] Migration004 applied successfully');
}
} catch (error: any) {
console.error('[SessionStore] Schema initialization error:', error.message);
@@ -151,7 +154,7 @@ export class SessionStore {
if (!hasWorkerPort) {
this.db.run('ALTER TABLE sdk_sessions ADD COLUMN worker_port INTEGER');
console.error('[SessionStore] Added worker_port column to sdk_sessions table');
console.log('[SessionStore] Added worker_port column to sdk_sessions table');
}
// Record migration
@@ -176,7 +179,7 @@ export class SessionStore {
if (!hasPromptCounter) {
this.db.run('ALTER TABLE sdk_sessions ADD COLUMN prompt_counter INTEGER DEFAULT 0');
console.error('[SessionStore] Added prompt_counter column to sdk_sessions table');
console.log('[SessionStore] Added prompt_counter column to sdk_sessions table');
}
// Check observations for prompt_number
@@ -185,7 +188,7 @@ export class SessionStore {
if (!obsHasPromptNumber) {
this.db.run('ALTER TABLE observations ADD COLUMN prompt_number INTEGER');
console.error('[SessionStore] Added prompt_number column to observations table');
console.log('[SessionStore] Added prompt_number column to observations table');
}
// Check session_summaries for prompt_number
@@ -194,7 +197,7 @@ export class SessionStore {
if (!sumHasPromptNumber) {
this.db.run('ALTER TABLE session_summaries ADD COLUMN prompt_number INTEGER');
console.error('[SessionStore] Added prompt_number column to session_summaries table');
console.log('[SessionStore] Added prompt_number column to session_summaries table');
}
// Record migration
@@ -223,7 +226,7 @@ export class SessionStore {
return;
}
console.error('[SessionStore] Removing UNIQUE constraint from session_summaries.sdk_session_id...');
console.log('[SessionStore] Removing UNIQUE constraint from session_summaries.sdk_session_id...');
// Begin transaction
this.db.run('BEGIN TRANSACTION');
@@ -278,7 +281,7 @@ export class SessionStore {
// Record migration
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(7, new Date().toISOString());
console.error('[SessionStore] Successfully removed UNIQUE constraint from session_summaries.sdk_session_id');
console.log('[SessionStore] Successfully removed UNIQUE constraint from session_summaries.sdk_session_id');
} catch (error: any) {
// Rollback on error
this.db.run('ROLLBACK');
@@ -308,7 +311,7 @@ export class SessionStore {
return;
}
console.error('[SessionStore] Adding hierarchical fields to observations table...');
console.log('[SessionStore] Adding hierarchical fields to observations table...');
// Add new columns
this.db.run(`
@@ -324,7 +327,7 @@ export class SessionStore {
// Record migration
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(8, new Date().toISOString());
console.error('[SessionStore] Successfully added hierarchical fields to observations table');
console.log('[SessionStore] Successfully added hierarchical fields to observations table');
} catch (error: any) {
console.error('[SessionStore] Migration error (add hierarchical fields):', error.message);
}
@@ -350,7 +353,7 @@ export class SessionStore {
return;
}
console.error('[SessionStore] Making observations.text nullable...');
console.log('[SessionStore] Making observations.text nullable...');
// Begin transaction
this.db.run('BEGIN TRANSACTION');
@@ -407,7 +410,7 @@ export class SessionStore {
// Record migration
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(9, new Date().toISOString());
console.error('[SessionStore] Successfully made observations.text nullable');
console.log('[SessionStore] Successfully made observations.text nullable');
} catch (error: any) {
// Rollback on error
this.db.run('ROLLBACK');
@@ -435,7 +438,7 @@ export class SessionStore {
return;
}
console.error('[SessionStore] Creating user_prompts table with FTS5 support...');
console.log('[SessionStore] Creating user_prompts table with FTS5 support...');
// Begin transaction
this.db.run('BEGIN TRANSACTION');
@@ -494,7 +497,7 @@ export class SessionStore {
// Record migration
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(10, new Date().toISOString());
console.error('[SessionStore] Successfully created user_prompts table with FTS5 support');
console.log('[SessionStore] Successfully created user_prompts table with FTS5 support');
} catch (error: any) {
// Rollback on error
this.db.run('ROLLBACK');
@@ -522,7 +525,7 @@ export class SessionStore {
if (!obsHasDiscoveryTokens) {
this.db.run('ALTER TABLE observations ADD COLUMN discovery_tokens INTEGER DEFAULT 0');
console.error('[SessionStore] Added discovery_tokens column to observations table');
console.log('[SessionStore] Added discovery_tokens column to observations table');
}
// Check if discovery_tokens column exists in session_summaries table
@@ -531,7 +534,7 @@ export class SessionStore {
if (!sumHasDiscoveryTokens) {
this.db.run('ALTER TABLE session_summaries ADD COLUMN discovery_tokens INTEGER DEFAULT 0');
console.error('[SessionStore] Added discovery_tokens column to session_summaries table');
console.log('[SessionStore] Added discovery_tokens column to session_summaries table');
}
// Record migration only after successful column verification/addition
@@ -1251,7 +1254,7 @@ export class SessionStore {
now.toISOString(),
nowEpoch
);
console.error(`[SessionStore] Auto-created session record for session_id: ${sdkSessionId}`);
console.log(`[SessionStore] Auto-created session record for session_id: ${sdkSessionId}`);
}
const stmt = this.db.prepare(`
@@ -1325,7 +1328,7 @@ export class SessionStore {
now.toISOString(),
nowEpoch
);
console.error(`[SessionStore] Auto-created session record for session_id: ${sdkSessionId}`);
console.log(`[SessionStore] Auto-created session record for session_id: ${sdkSessionId}`);
}
const stmt = this.db.prepare(`
@@ -1531,7 +1534,7 @@ export class SessionStore {
startEpoch = beforeRecords.length > 0 ? beforeRecords[beforeRecords.length - 1].created_at_epoch : anchorEpoch;
endEpoch = afterRecords.length > 0 ? afterRecords[afterRecords.length - 1].created_at_epoch : anchorEpoch;
} catch (err: any) {
console.error('[SessionStore] Error getting boundary observations:', err.message);
console.error('[SessionStore] Error getting boundary observations:', err.message, project ? `(project: ${project})` : '(all projects)');
return { observations: [], sessions: [], prompts: [] };
}
} else {
@@ -1563,7 +1566,7 @@ export class SessionStore {
startEpoch = beforeRecords.length > 0 ? beforeRecords[beforeRecords.length - 1].created_at_epoch : anchorEpoch;
endEpoch = afterRecords.length > 0 ? afterRecords[afterRecords.length - 1].created_at_epoch : anchorEpoch;
} catch (err: any) {
console.error('[SessionStore] Error getting boundary timestamps:', err.message);
console.error('[SessionStore] Error getting boundary timestamps:', err.message, project ? `(project: ${project})` : '(all projects)');
return { observations: [], sessions: [], prompts: [] };
}
}
@@ -1618,7 +1621,7 @@ export class SessionStore {
}))
};
} catch (err: any) {
console.error('[SessionStore] Error querying timeline records:', err.message);
console.error('[SessionStore] Error querying timeline records:', err.message, project ? `(project: ${project})` : '(all projects)');
return { observations: [], sessions: [], prompts: [] };
}
}
+16 -4
View File
@@ -138,7 +138,10 @@ export class ChromaSync {
await this.ensureConnection();
if (!this.client) {
throw new Error('Chroma client not initialized');
throw new Error(
'Chroma client not initialized. Call ensureConnection() before using client methods.' +
` Project: ${this.project}`
);
}
try {
@@ -319,7 +322,10 @@ export class ChromaSync {
await this.ensureCollection();
if (!this.client) {
throw new Error('Chroma client not initialized');
throw new Error(
'Chroma client not initialized. Call ensureConnection() before using client methods.' +
` Project: ${this.project}`
);
}
try {
@@ -496,7 +502,10 @@ export class ChromaSync {
await this.ensureConnection();
if (!this.client) {
throw new Error('Chroma client not initialized');
throw new Error(
'Chroma client not initialized. Call ensureConnection() before using client methods.' +
` Project: ${this.project}`
);
}
const observationIds = new Set<number>();
@@ -750,7 +759,10 @@ export class ChromaSync {
await this.ensureConnection();
if (!this.client) {
throw new Error('Chroma client not initialized');
throw new Error(
'Chroma client not initialized. Call ensureConnection() before using client methods.' +
` Project: ${this.project}`
);
}
const whereStringified = whereFilter ? JSON.stringify(whereFilter) : undefined;
+7 -2
View File
@@ -123,8 +123,13 @@ export class WorkerService {
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
res.status(200).json({ version: packageJson.version });
} catch (error) {
logger.error('SYSTEM', 'Failed to read version', {}, error as Error);
res.status(500).json({ error: 'Failed to read version' });
logger.error('SYSTEM', 'Failed to read version', {
packagePath: packageJsonPath
}, error as Error);
res.status(500).json({
error: 'Failed to read version',
path: packageJsonPath
});
}
});
+9 -17
View File
@@ -233,16 +233,8 @@ export class SDKAgent {
sdk_session_id: session.sdkSessionId,
project: session.project,
user_prompt: session.userPrompt,
last_user_message: happy_path_error__with_fallback(
'Missing last_user_message for summary in SDKAgent',
{ sessionDbId: session.sessionDbId, sdkSessionId: session.sdkSessionId },
message.last_user_message || ''
),
last_assistant_message: happy_path_error__with_fallback(
'Missing last_assistant_message for summary in SDKAgent',
{ sessionDbId: session.sessionDbId, sdkSessionId: session.sdkSessionId },
message.last_assistant_message || ''
)
last_user_message: message.last_user_message || '',
last_assistant_message: message.last_assistant_message || ''
})
},
session_id: session.claudeSessionId,
@@ -276,16 +268,16 @@ export class SDKAgent {
sessionId: session.sessionDbId,
obsId,
type: obs.type,
title: obs.title || happy_path_error__with_fallback('obs.title is null', { obsId, type: obs.type }, '(untitled)'),
filesRead: obs.files_read?.length ?? (happy_path_error__with_fallback('obs.files_read is null/undefined', { obsId }), 0),
filesModified: obs.files_modified?.length ?? (happy_path_error__with_fallback('obs.files_modified is null/undefined', { obsId }), 0),
concepts: obs.concepts?.length ?? (happy_path_error__with_fallback('obs.concepts is null/undefined', { obsId }), 0)
title: obs.title || '(untitled)',
filesRead: obs.files_read?.length ?? 0,
filesModified: obs.files_modified?.length ?? 0,
concepts: obs.concepts?.length ?? 0
});
// Sync to Chroma with error logging
const chromaStart = Date.now();
const obsType = obs.type;
const obsTitle = obs.title || happy_path_error__with_fallback('obs.title is null for Chroma sync', { obsId, type: obs.type }, '(untitled)');
const obsTitle = obs.title || '(untitled)';
this.dbManager.getChromaSync().syncObservation(
obsId,
session.claudeSessionId,
@@ -353,14 +345,14 @@ export class SDKAgent {
logger.info('SDK', 'Summary saved', {
sessionId: session.sessionDbId,
summaryId,
request: summary.request || happy_path_error__with_fallback('summary.request is null', { summaryId }, '(no request)'),
request: summary.request || '(no request)',
hasCompleted: !!summary.completed,
hasNextSteps: !!summary.next_steps
});
// Sync to Chroma with error logging
const chromaStart = Date.now();
const summaryRequest = summary.request || happy_path_error__with_fallback('summary.request is null for Chroma sync', { summaryId }, '(no request)');
const summaryRequest = summary.request || '(no request)';
this.dbManager.getChromaSync().syncSummary(
summaryId,
session.claudeSessionId,
@@ -342,10 +342,10 @@ export class SessionRoutes extends BaseRouteHandler {
tool_input: cleanedToolInput,
tool_response: cleanedToolResponse,
prompt_number: promptNumber,
cwd: happy_path_error__with_fallback(
cwd: cwd || happy_path_error__with_fallback(
'Missing cwd when queueing observation in SessionRoutes',
{ sessionDbId, tool_name },
cwd || ''
''
)
});
@@ -394,10 +394,10 @@ export class SessionRoutes extends BaseRouteHandler {
// Queue summarize
this.sessionManager.queueSummarize(
sessionDbId,
happy_path_error__with_fallback(
last_user_message || happy_path_error__with_fallback(
'Missing last_user_message when queueing summary in SessionRoutes',
{ sessionDbId },
last_user_message || ''
''
),
last_assistant_message
);
+5 -1
View File
@@ -139,7 +139,11 @@ async function ensureWorkerVersionMatches(): Promise<void> {
// Verify it's healthy
if (!await isWorkerHealthy()) {
logger.error('SYSTEM', 'Worker failed to restart after version mismatch');
logger.error('SYSTEM', 'Worker failed to restart after version mismatch', {
expectedVersion: pluginVersion,
runningVersion: workerVersion,
port
});
}
}
}
+8 -1
View File
@@ -6,6 +6,7 @@ export interface WorkerErrorMessageOptions {
port?: number;
includeSkillFallback?: boolean;
customPrefix?: string;
actualError?: string;
}
/**
@@ -19,7 +20,8 @@ export function getWorkerRestartInstructions(
const {
port,
includeSkillFallback = false,
customPrefix
customPrefix,
actualError
} = options;
const isWindows = process.platform === 'win32';
@@ -49,5 +51,10 @@ export function getWorkerRestartInstructions(
message += `\n\nIf that doesn't work, try: /troubleshoot`;
}
// Prepend actual error if provided
if (actualError) {
message = `Worker Error: ${actualError}\n\n${message}`;
}
return message;
}
+20 -15
View File
@@ -3,26 +3,31 @@
*
* Semantic meaning: "When the happy path fails, this is an error, but we have a fallback."
*
* NOTE: This utility is to be used like Frank's Red Hot, we put that shit on everything.
*
* USE THIS INSTEAD OF SILENT FAILURES!
* Stop doing this: `const value = something || '';`
* Start doing this: `const value = something || happy_path_error__with_fallback('something was undefined');`
*
* Logs to ~/.claude-mem/silent.log and returns a fallback value.
* Check logs with `npm run logs:silent`
*
* Usage:
* import { happy_path_error__with_fallback } from '../utils/silent-debug.js';
* Use happy_path_error__with_fallback for:
* ✅ Unexpected null/undefined values that should theoretically never happen
* ✅ Defensive coding where silent fallback is acceptable
* ✅ Situations where you want to track unexpected nulls without breaking execution
*
* const title = obs.title || happy_path_error__with_fallback('obs.title missing', { obs });
* const name = user.name || happy_path_error__with_fallback('user.name missing', { user }, 'Anonymous');
* DO NOT use for:
* ❌ Nullable fields with valid default behavior (use direct || defaults)
* ❌ Critical validation failures (use logger.warn or throw Error)
* ❌ Try-catch blocks where error is already logged (redundant)
*
* try {
* doSomething();
* } catch (error) {
* happy_path_error__with_fallback('doSomething failed', { error });
* }
* Good examples:
* // Truly unexpected null (should never happen in theory)
* const id = session.id || happy_path_error__with_fallback('session.id missing', { session });
*
* Bad examples (use direct defaults instead):
* // Nullable field with valid empty default
* const title = obs.title || happy_path_error__with_fallback('obs.title missing', { obs }, '(untitled)');
* // BETTER: const title = obs.title || '(untitled)';
*
* // Array that can validly be undefined/null
* const count = obs.files?.length ?? (happy_path_error__with_fallback('obs.files missing', { obs }), 0);
* // BETTER: const count = obs.files?.length ?? 0;
*/
import { appendFileSync } from 'fs';