Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
# Phase 0 Task 1: Add Comprehensive Logging to Summary Hook
|
||||
|
||||
## Overview
|
||||
Added comprehensive logging to the Stop hook (summary hook) to verify it fires on normal exit and successfully sends the FINALIZE message to the worker socket.
|
||||
|
||||
## Files Modified
|
||||
|
||||
### `/Users/alexnewman/Scripts/claude-mem/src/hooks/summary.ts`
|
||||
Added 8 logging points throughout the hook execution flow.
|
||||
|
||||
## Logging Points Added
|
||||
|
||||
All logs use the `[claude-mem summary]` prefix for easy searching and use `console.error()` to output to stderr (visible in terminal).
|
||||
|
||||
### 1. Hook Entry Point (Line 18-20)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Hook fired', {
|
||||
input: input ? { session_id: input.session_id, cwd: input.cwd } : null
|
||||
});
|
||||
```
|
||||
**Purpose:** Confirms the hook was called by Claude Code and logs the input parameters.
|
||||
|
||||
### 2. Session Search (Line 34)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Searching for active SDK session', { session_id });
|
||||
```
|
||||
**Purpose:** Logs the session_id being searched for in the database.
|
||||
|
||||
### 3. Session Not Found (Line 43)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] No active SDK session found', { session_id });
|
||||
```
|
||||
**Purpose:** Logs when no active session is found (normal for non-SDK sessions).
|
||||
|
||||
### 4. Session Found (Line 48-52)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Active SDK session found', {
|
||||
session_id: session.id,
|
||||
collection_name: session.collection_name,
|
||||
worker_pid: session.worker_pid
|
||||
});
|
||||
```
|
||||
**Purpose:** Logs when an active session is found with its details for verification.
|
||||
|
||||
### 5. Before Socket Send (Line 62-65)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Attempting to send FINALIZE message to worker socket', {
|
||||
socketPath,
|
||||
message
|
||||
});
|
||||
```
|
||||
**Purpose:** Logs the socket path and message content before attempting connection.
|
||||
|
||||
### 6. Socket Connection Established (Line 68)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Socket connection established, sending message');
|
||||
```
|
||||
**Purpose:** Confirms successful socket connection before writing data.
|
||||
|
||||
### 7. Socket Error Handler (Line 75-79)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Socket error occurred', {
|
||||
error: err.message,
|
||||
code: (err as any).code,
|
||||
socketPath
|
||||
});
|
||||
```
|
||||
**Purpose:** Logs detailed error information if socket connection fails (includes error code like ENOENT, ECONNREFUSED).
|
||||
|
||||
### 8. Socket Close Handler (Line 84)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Socket connection closed successfully');
|
||||
```
|
||||
**Purpose:** Confirms the socket connection closed cleanly after sending message.
|
||||
|
||||
### 9. Catch Block (Line 91-95)
|
||||
```typescript
|
||||
console.error('[claude-mem summary] Unexpected error in hook', {
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
name: error.name
|
||||
});
|
||||
```
|
||||
**Purpose:** Logs any unexpected errors with full stack trace for debugging.
|
||||
|
||||
## How to Test
|
||||
|
||||
### Basic Test (Normal Exit)
|
||||
1. Start a Claude Code session in a project with claude-mem configured
|
||||
2. Have a conversation that triggers SDK memory operations
|
||||
3. Exit Claude Code normally (Ctrl+D or type "exit")
|
||||
4. Check terminal stderr for log sequence:
|
||||
```
|
||||
[claude-mem summary] Hook fired
|
||||
[claude-mem summary] Searching for active SDK session
|
||||
[claude-mem summary] Active SDK session found
|
||||
[claude-mem summary] Attempting to send FINALIZE message to worker socket
|
||||
[claude-mem summary] Socket connection established, sending message
|
||||
[claude-mem summary] Socket connection closed successfully
|
||||
```
|
||||
|
||||
### Test Cases
|
||||
|
||||
#### Case 1: Normal Exit with Active Session
|
||||
**Expected logs:**
|
||||
1. Hook fired (with session_id and cwd)
|
||||
2. Searching for active SDK session
|
||||
3. Active SDK session found (with session details)
|
||||
4. Attempting to send FINALIZE message (with socket path)
|
||||
5. Socket connection established
|
||||
6. Socket connection closed successfully
|
||||
|
||||
#### Case 2: Exit with No Active Session
|
||||
**Expected logs:**
|
||||
1. Hook fired
|
||||
2. Searching for active SDK session
|
||||
3. No active SDK session found
|
||||
|
||||
#### Case 3: Worker Socket Already Closed
|
||||
**Expected logs:**
|
||||
1. Hook fired
|
||||
2. Searching for active SDK session
|
||||
3. Active SDK session found
|
||||
4. Attempting to send FINALIZE message
|
||||
5. Socket error occurred (with ENOENT or ECONNREFUSED code)
|
||||
|
||||
#### Case 4: Database Error
|
||||
**Expected logs:**
|
||||
1. Hook fired
|
||||
2. Searching for active SDK session
|
||||
3. Unexpected error in hook (with stack trace)
|
||||
|
||||
### Log Filtering
|
||||
To view only summary hook logs:
|
||||
```bash
|
||||
claude-code 2>&1 | grep "\[claude-mem summary\]"
|
||||
```
|
||||
|
||||
## Behavior Guarantees
|
||||
|
||||
1. **No Breaking Changes:** All existing functionality remains identical
|
||||
2. **Non-Blocking:** All errors are caught and logged but don't block Claude Code
|
||||
3. **Clean Exit:** Hook always returns proper JSON response to Claude Code
|
||||
4. **Searchable:** All logs use consistent `[claude-mem summary]` prefix
|
||||
|
||||
## Issues and Concerns
|
||||
|
||||
### None Discovered
|
||||
- The existing error handling is robust
|
||||
- All error paths properly log and exit gracefully
|
||||
- No changes needed to logic, only observability added
|
||||
|
||||
### Potential Observations During Testing
|
||||
- If socket errors are common, may indicate worker timing issues
|
||||
- If "No active SDK session found" appears frequently, may indicate database query issues
|
||||
- If hook never fires, indicates Claude Code hook registration problem
|
||||
- If socket path is wrong, indicates paths.ts configuration issue
|
||||
|
||||
## Next Steps
|
||||
|
||||
After testing with these logs:
|
||||
1. Verify hook fires on every Claude Code exit
|
||||
2. Verify FINALIZE message reaches worker socket
|
||||
3. Check for any unexpected error patterns
|
||||
4. Use logs to diagnose any issues with worker finalization flow
|
||||
Reference in New Issue
Block a user