417acb0f81
* Add enforceable anti-pattern detection for try-catch abuse PROBLEM: - Overly-broad try-catch blocks waste 10+ hours of debugging time - Empty catch blocks silently swallow errors - AI assistants use try-catch to paper over uncertainty instead of doing research SOLUTION: 1. Created detect-error-handling-antipatterns.ts test - Detects empty catch blocks (45 CRITICAL found) - Detects catch without logging (45 CRITICAL total) - Detects large try blocks (>10 lines) - Detects generic catch without type checking - Detects catch-and-continue on critical paths - Exit code 1 if critical issues found 2. Updated CLAUDE.md with MANDATORY ERROR HANDLING RULES - 5-question pre-flight checklist before any try-catch - FORBIDDEN patterns with examples - ALLOWED patterns with examples - Meta-rule: UNCERTAINTY TRIGGERS RESEARCH, NOT TRY-CATCH - Critical path protection list 3. Created comprehensive try-catch audit report - Documents all 96 try-catch blocks in worker service - Identifies critical issue at worker-service.ts:748-750 - Categorizes patterns and provides recommendations This is enforceable via test, not just instructions that can be ignored. Current state: 163 anti-patterns detected (45 critical, 47 high, 71 medium) Next: Fix critical issues identified by test 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix: add logging to 5 critical empty catch blocks (Wave 1) Wave 1 of error handling cleanup - fixing empty catch blocks that silently swallow errors without any trace. Fixed files: - src/bin/import-xml-observations.ts:80 - Log skipped invalid JSON - src/utils/bun-path.ts:33 - Log when bun not in PATH - src/utils/cursor-utils.ts:44 - Log failed registry reads - src/utils/cursor-utils.ts:149 - Log corrupt MCP config - src/shared/worker-utils.ts:128 - Log failed health checks All catch blocks now have proper logging with context and error details. Progress: 41 → 39 CRITICAL issues remaining 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix: add logging to promise catches on critical paths (Wave 2) Wave 2 of error handling cleanup - fixing empty promise catch handlers that silently swallow errors on critical code paths. These are the patterns that caused the 10-hour debugging session. Fixed empty promise catches: - worker-service.ts:642 - Background initialization failures - SDKAgent.ts:372,446 - Session processor errors - GeminiAgent.ts:408,475 - Finalization failures - OpenRouterAgent.ts:451,518 - Finalization failures - SessionManager.ts:289 - Generator promise failures Added justification comments to catch-and-continue blocks: - worker-service.ts:68 - PID file removal (cleanup, non-critical) - worker-service.ts:130 - Cursor context update (non-critical) All promise rejection handlers now log errors with context, preventing silent failures that were nearly impossible to debug. Note: The anti-pattern detector only tracks try-catch blocks, not standalone promise chains. These fixes address the root cause of the original 10-hour debugging session even though the detector count remains unchanged. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix: add logging and documentation to error handling patterns (Wave 3) Wave 3 of error handling cleanup - comprehensive review and fixes for remaining critical issues identified by the anti-pattern detector. Changes organized by severity: **Wave 3.1: Fixed 2 EMPTY_CATCH blocks** - worker-service.ts:162 - Health check polling now logs failures - worker-service.ts:610 - Process cleanup logs failures **Wave 3.2: Reviewed 12 CATCH_AND_CONTINUE patterns** - Verified all are correct (log errors AND exit/return HTTP errors) - Added justification comment to session recovery (line 829) - All patterns properly notify callers of failures **Wave 3.3: Fixed 29 NO_LOGGING_IN_CATCH issues** Added logging to 16 catch blocks: - UI layer: useSettings.ts, useContextPreview.ts (console logging) - Servers: mcp-server.ts health checks and tool execution - Worker: version fetch, cleanup, config corruption - Routes: error handler, session recovery, settings validation - Services: branch checkout, timeline queries Documented 13 intentional exceptions with comments explaining why: - Hot paths (port checks, process checks in tight loops) - Error accumulation (transcript parser collects for batch retrieval) - Special cases (logger can't log its own failures) - Fallback parsing (JSON parse in optional data structures) All changes follow error handling guidelines from CLAUDE.md: - Appropriate log levels (error/warn/debug) - Context objects with relevant details - Descriptive messages explaining failures - Error extraction pattern for Error instances Progress: 41 → 29 detector warnings Remaining warnings are conservative flags on verified-correct patterns (catch-and-continue blocks that properly log + notify callers). Build verified successful. All error handling now provides visibility for debugging while avoiding excessive logging on hot paths. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * feat: add queue:clear command to remove failed messages Added functionality to clear failed messages from the observation queue: **Changes:** - PendingMessageStore: Added clearFailed() method to delete failed messages - DataRoutes: Added DELETE /api/pending-queue/failed endpoint - CLI: Created scripts/clear-failed-queue.ts for interactive queue clearing - package.json: Added npm run queue:clear script **Usage:** npm run queue:clear # Interactive - prompts for confirmation npm run queue:clear -- --force # Non-interactive - clears without prompt Failed messages are observations that exceeded max retry count. They remain in the queue for debugging but won't be processed. This command removes them to clean up the queue. Works alongside existing queue:check and queue:process commands to provide complete queue management capabilities. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * feat: add --all flag to queue:clear for complete queue reset Extended queue clearing functionality to support clearing all messages, not just failed ones. **Changes:** - PendingMessageStore: Added clearAll() method to clear pending, processing, and failed - DataRoutes: Added DELETE /api/pending-queue/all endpoint - clear-failed-queue.ts: Added --all flag to clear everything - Updated help text and UI to distinguish between failed-only and all-clear modes **Usage:** npm run queue:clear # Clear failed only (interactive) npm run queue:clear -- --all # Clear ALL messages (interactive) npm run queue:clear -- --all --force # Clear all without confirmation The --all flag provides a complete queue reset, removing pending, processing, and failed messages. Useful when you want a fresh start or need to cancel stuck sessions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * feat: add comprehensive documentation for session ID architecture and validation tests * feat: add logs viewer with clear functionality to UI - Add LogsRoutes API endpoint for fetching and clearing worker logs - Create LogsModal component with auto-refresh and clear button - Integrate logs viewer button into Header component - Add comprehensive CSS styling for logs modal - Logs accessible via new document icon button in header Logs viewer features: - Display last 1000 lines of current day's log file - Auto-refresh toggle (2s interval) - Clear logs button with confirmation - Monospace font for readable log output - Responsive modal design matching existing UI 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * refactor: redesign logs as Chrome DevTools-style console drawer Major UX improvements to match Chrome DevTools console: - Convert from modal to bottom drawer that slides up - Move toggle button to bottom-left corner (floating button) - Add draggable resize handle for height adjustment - Use plain monospace font (SF Mono/Monaco/Consolas) instead of Monaspace - Simplify controls with icon-only buttons - Add Console tab UI matching DevTools aesthetic Changes: - Renamed LogsModal to LogsDrawer with drawer implementation - Added resize functionality with mouse drag - Removed logs button from header - Added floating console toggle button in bottom-left - Updated all CSS to match Chrome console styling - Minimum height: 150px, maximum: window height - 100px 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix: suppress /api/logs endpoint logging to reduce noise Skip logging GET /api/logs requests in HTTP middleware to prevent log spam from auto-refresh polling (every 2s). Keeps the auto-refresh feature functional while eliminating the repetitive log entries. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * refactor: enhance error handling guidelines with approved overrides for justified exceptions --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
387 lines
12 KiB
TypeScript
387 lines
12 KiB
TypeScript
/**
|
|
* Structured Logger for claude-mem Worker Service
|
|
* Provides readable, traceable logging with correlation IDs and data flow tracking
|
|
*/
|
|
|
|
import { SettingsDefaultsManager } from '../shared/SettingsDefaultsManager.js';
|
|
import { appendFileSync, existsSync, mkdirSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
export enum LogLevel {
|
|
DEBUG = 0,
|
|
INFO = 1,
|
|
WARN = 2,
|
|
ERROR = 3,
|
|
SILENT = 4
|
|
}
|
|
|
|
export type Component = 'HOOK' | 'WORKER' | 'SDK' | 'PARSER' | 'DB' | 'SYSTEM' | 'HTTP' | 'SESSION' | 'CHROMA';
|
|
|
|
interface LogContext {
|
|
sessionId?: number;
|
|
memorySessionId?: string;
|
|
correlationId?: string;
|
|
[key: string]: any;
|
|
}
|
|
|
|
class Logger {
|
|
private level: LogLevel | null = null;
|
|
private useColor: boolean;
|
|
private logFilePath: string | null = null;
|
|
|
|
constructor() {
|
|
// Disable colors when output is not a TTY (e.g., PM2 logs)
|
|
this.useColor = process.stdout.isTTY ?? false;
|
|
this.initializeLogFile();
|
|
}
|
|
|
|
/**
|
|
* Initialize log file path and ensure directory exists
|
|
*/
|
|
private initializeLogFile(): void {
|
|
try {
|
|
// Get data directory from settings
|
|
const dataDir = SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR');
|
|
const logsDir = join(dataDir, 'logs');
|
|
|
|
// Ensure logs directory exists
|
|
if (!existsSync(logsDir)) {
|
|
mkdirSync(logsDir, { recursive: true });
|
|
}
|
|
|
|
// Create log file path with date
|
|
const date = new Date().toISOString().split('T')[0];
|
|
this.logFilePath = join(logsDir, `claude-mem-${date}.log`);
|
|
} catch (error) {
|
|
// If log file initialization fails, just log to console
|
|
console.error('[LOGGER] Failed to initialize log file:', error);
|
|
this.logFilePath = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lazy-load log level from settings file (not hardcoded defaults!)
|
|
*/
|
|
private getLevel(): LogLevel {
|
|
if (this.level === null) {
|
|
try {
|
|
// Load settings from file to get user's actual log level
|
|
const dataDir = SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR');
|
|
const settingsPath = join(dataDir, 'settings.json');
|
|
const settings = SettingsDefaultsManager.loadFromFile(settingsPath);
|
|
const envLevel = settings.CLAUDE_MEM_LOG_LEVEL.toUpperCase();
|
|
this.level = LogLevel[envLevel as keyof typeof LogLevel] ?? LogLevel.INFO;
|
|
} catch (error) {
|
|
// Fallback to INFO if settings can't be loaded
|
|
console.error('[LOGGER] Failed to load settings, using INFO level:', error);
|
|
this.level = LogLevel.INFO;
|
|
}
|
|
}
|
|
return this.level;
|
|
}
|
|
|
|
/**
|
|
* Create correlation ID for tracking an observation through the pipeline
|
|
*/
|
|
correlationId(sessionId: number, observationNum: number): string {
|
|
return `obs-${sessionId}-${observationNum}`;
|
|
}
|
|
|
|
/**
|
|
* Create session correlation ID
|
|
*/
|
|
sessionId(sessionId: number): string {
|
|
return `session-${sessionId}`;
|
|
}
|
|
|
|
/**
|
|
* Format data for logging - create compact summaries instead of full dumps
|
|
*/
|
|
private formatData(data: any): string {
|
|
if (data === null || data === undefined) return '';
|
|
if (typeof data === 'string') return data;
|
|
if (typeof data === 'number') return data.toString();
|
|
if (typeof data === 'boolean') return data.toString();
|
|
|
|
// For objects, create compact summaries
|
|
if (typeof data === 'object') {
|
|
// If it's an error, show message and stack in debug mode
|
|
if (data instanceof Error) {
|
|
return this.getLevel() === LogLevel.DEBUG
|
|
? `${data.message}\n${data.stack}`
|
|
: data.message;
|
|
}
|
|
|
|
// For arrays, show count
|
|
if (Array.isArray(data)) {
|
|
return `[${data.length} items]`;
|
|
}
|
|
|
|
// For objects, show key count
|
|
const keys = Object.keys(data);
|
|
if (keys.length === 0) return '{}';
|
|
if (keys.length <= 3) {
|
|
// Show small objects inline
|
|
return JSON.stringify(data);
|
|
}
|
|
return `{${keys.length} keys: ${keys.slice(0, 3).join(', ')}...}`;
|
|
}
|
|
|
|
return String(data);
|
|
}
|
|
|
|
/**
|
|
* Format a tool name and input for compact display
|
|
*/
|
|
formatTool(toolName: string, toolInput?: any): string {
|
|
if (!toolInput) return toolName;
|
|
|
|
const input = typeof toolInput === 'string' ? JSON.parse(toolInput) : toolInput;
|
|
|
|
// Bash: show full command
|
|
if (toolName === 'Bash' && input.command) {
|
|
return `${toolName}(${input.command})`;
|
|
}
|
|
|
|
// File operations: show full path
|
|
if (input.file_path) {
|
|
return `${toolName}(${input.file_path})`;
|
|
}
|
|
|
|
// NotebookEdit: show full notebook path
|
|
if (input.notebook_path) {
|
|
return `${toolName}(${input.notebook_path})`;
|
|
}
|
|
|
|
// Glob: show full pattern
|
|
if (toolName === 'Glob' && input.pattern) {
|
|
return `${toolName}(${input.pattern})`;
|
|
}
|
|
|
|
// Grep: show full pattern
|
|
if (toolName === 'Grep' && input.pattern) {
|
|
return `${toolName}(${input.pattern})`;
|
|
}
|
|
|
|
// WebFetch/WebSearch: show full URL or query
|
|
if (input.url) {
|
|
return `${toolName}(${input.url})`;
|
|
}
|
|
|
|
if (input.query) {
|
|
return `${toolName}(${input.query})`;
|
|
}
|
|
|
|
// Task: show subagent_type or full description
|
|
if (toolName === 'Task') {
|
|
if (input.subagent_type) {
|
|
return `${toolName}(${input.subagent_type})`;
|
|
}
|
|
if (input.description) {
|
|
return `${toolName}(${input.description})`;
|
|
}
|
|
}
|
|
|
|
// Skill: show skill name
|
|
if (toolName === 'Skill' && input.skill) {
|
|
return `${toolName}(${input.skill})`;
|
|
}
|
|
|
|
// LSP: show operation type
|
|
if (toolName === 'LSP' && input.operation) {
|
|
return `${toolName}(${input.operation})`;
|
|
}
|
|
|
|
// Default: just show tool name
|
|
return toolName;
|
|
}
|
|
|
|
/**
|
|
* Format timestamp in local timezone (YYYY-MM-DD HH:MM:SS.mmm)
|
|
*/
|
|
private formatTimestamp(date: Date): string {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
const ms = String(date.getMilliseconds()).padStart(3, '0');
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${ms}`;
|
|
}
|
|
|
|
/**
|
|
* Core logging method
|
|
*/
|
|
private log(
|
|
level: LogLevel,
|
|
component: Component,
|
|
message: string,
|
|
context?: LogContext,
|
|
data?: any
|
|
): void {
|
|
if (level < this.getLevel()) return;
|
|
|
|
const timestamp = this.formatTimestamp(new Date());
|
|
const levelStr = LogLevel[level].padEnd(5);
|
|
const componentStr = component.padEnd(6);
|
|
|
|
// Build correlation ID part
|
|
let correlationStr = '';
|
|
if (context?.correlationId) {
|
|
correlationStr = `[${context.correlationId}] `;
|
|
} else if (context?.sessionId) {
|
|
correlationStr = `[session-${context.sessionId}] `;
|
|
}
|
|
|
|
// Build data part
|
|
let dataStr = '';
|
|
if (data !== undefined && data !== null) {
|
|
// Handle Error objects specially - they don't JSON.stringify properly
|
|
if (data instanceof Error) {
|
|
dataStr = this.getLevel() === LogLevel.DEBUG
|
|
? `\n${data.message}\n${data.stack}`
|
|
: ` ${data.message}`;
|
|
} else if (this.getLevel() === LogLevel.DEBUG && typeof data === 'object') {
|
|
// In debug mode, show full JSON for objects
|
|
dataStr = '\n' + JSON.stringify(data, null, 2);
|
|
} else {
|
|
dataStr = ' ' + this.formatData(data);
|
|
}
|
|
}
|
|
|
|
// Build additional context
|
|
let contextStr = '';
|
|
if (context) {
|
|
const { sessionId, memorySessionId, correlationId, ...rest } = context;
|
|
if (Object.keys(rest).length > 0) {
|
|
const pairs = Object.entries(rest).map(([k, v]) => `${k}=${v}`);
|
|
contextStr = ` {${pairs.join(', ')}}`;
|
|
}
|
|
}
|
|
|
|
const logLine = `[${timestamp}] [${levelStr}] [${componentStr}] ${correlationStr}${message}${contextStr}${dataStr}`;
|
|
|
|
// Output to log file ONLY (worker runs in background, console is useless)
|
|
if (this.logFilePath) {
|
|
try {
|
|
appendFileSync(this.logFilePath, logLine + '\n', 'utf8');
|
|
} catch (error) {
|
|
// Logger can't log its own failures - use stderr as last resort
|
|
// This is expected during disk full / permission errors
|
|
process.stderr.write(`[LOGGER] Failed to write to log file: ${error}\n`);
|
|
}
|
|
} else {
|
|
// If no log file available, write to stderr as fallback
|
|
process.stderr.write(logLine + '\n');
|
|
}
|
|
}
|
|
|
|
// Public logging methods
|
|
debug(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.log(LogLevel.DEBUG, component, message, context, data);
|
|
}
|
|
|
|
info(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.log(LogLevel.INFO, component, message, context, data);
|
|
}
|
|
|
|
warn(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.log(LogLevel.WARN, component, message, context, data);
|
|
}
|
|
|
|
error(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.log(LogLevel.ERROR, component, message, context, data);
|
|
}
|
|
|
|
/**
|
|
* Log data flow: input → processing
|
|
*/
|
|
dataIn(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.info(component, `→ ${message}`, context, data);
|
|
}
|
|
|
|
/**
|
|
* Log data flow: processing → output
|
|
*/
|
|
dataOut(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.info(component, `← ${message}`, context, data);
|
|
}
|
|
|
|
/**
|
|
* Log successful completion
|
|
*/
|
|
success(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.info(component, `✓ ${message}`, context, data);
|
|
}
|
|
|
|
/**
|
|
* Log failure
|
|
*/
|
|
failure(component: Component, message: string, context?: LogContext, data?: any): void {
|
|
this.error(component, `✗ ${message}`, context, data);
|
|
}
|
|
|
|
/**
|
|
* Log timing information
|
|
*/
|
|
timing(component: Component, message: string, durationMs: number, context?: LogContext): void {
|
|
this.info(component, `⏱ ${message}`, context, { duration: `${durationMs}ms` });
|
|
}
|
|
|
|
/**
|
|
* Happy Path Error - logs when the expected "happy path" fails but we have a fallback
|
|
*
|
|
* Semantic meaning: "When the happy path fails, this is an error, but we have a fallback."
|
|
*
|
|
* Use 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
|
|
*
|
|
* 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)
|
|
*
|
|
* @param component - Component where error occurred
|
|
* @param message - Error message describing what went wrong
|
|
* @param context - Optional context (sessionId, correlationId, etc)
|
|
* @param data - Optional data to include
|
|
* @param fallback - Value to return (defaults to empty string)
|
|
* @returns The fallback value
|
|
*/
|
|
happyPathError<T = string>(
|
|
component: Component,
|
|
message: string,
|
|
context?: LogContext,
|
|
data?: any,
|
|
fallback: T = '' as T
|
|
): T {
|
|
// Capture stack trace to get caller location
|
|
const stack = new Error().stack || '';
|
|
const stackLines = stack.split('\n');
|
|
// Line 0: "Error"
|
|
// Line 1: "at happyPathError ..."
|
|
// Line 2: "at <CALLER> ..." <- We want this one
|
|
const callerLine = stackLines[2] || '';
|
|
const callerMatch = callerLine.match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/);
|
|
const location = callerMatch
|
|
? `${callerMatch[1].split('/').pop()}:${callerMatch[2]}`
|
|
: 'unknown';
|
|
|
|
// Log as a warning with location info
|
|
const enhancedContext = {
|
|
...context,
|
|
location
|
|
};
|
|
|
|
this.warn(component, `[HAPPY-PATH] ${message}`, enhancedContext, data);
|
|
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
// Export singleton instance
|
|
export const logger = new Logger();
|