3ea180c1ef
This commit simplifies worker startup coordination and addresses Windows-specific issues: **Lock Removal**: - Removed entire file-based locking system (~100 lines) - Replaced with health-check-first approach - Port binding provides natural mutual exclusion - multiple spawns fail cleanly **Windows Stability**: - Removed all AbortSignal.timeout() calls to reduce Bun libuv assertion errors - Added 500ms shutdown delays on Windows to prevent zombie ports - Worker service has its own timeouts, so client-side timeouts are redundant **Package.json Updates**: - Updated worker scripts to use worker-service.cjs directly - Removed references to deleted worker-cli.js and worker-wrapper.cjs **Key Changes**: - src/services/worker-service.ts: Lock removal, shutdown delays, simplified start logic - src/hooks/*.ts: Removed AbortSignal.timeout from all HTTP requests - src/shared/worker-utils.ts: Removed AbortSignal.timeout from health checks - package.json: Updated worker:* scripts Resolves startup hangs, reduces assertion errors, and prevents zombie port issues. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
/**
|
|
* Save Hook - PostToolUse
|
|
*
|
|
* Pure HTTP client - sends data to worker, worker handles all database operations
|
|
* including privacy checks. This allows the hook to run under any runtime
|
|
* (Node.js or Bun) since it has no native module dependencies.
|
|
*/
|
|
|
|
import { stdin } from 'process';
|
|
import { STANDARD_HOOK_RESPONSE } from './hook-response.js';
|
|
import { logger } from '../utils/logger.js';
|
|
import { ensureWorkerRunning, getWorkerPort } from '../shared/worker-utils.js';
|
|
import { HOOK_TIMEOUTS } from '../shared/hook-constants.js';
|
|
|
|
export interface PostToolUseInput {
|
|
session_id: string;
|
|
cwd: string;
|
|
tool_name: string;
|
|
tool_input: any;
|
|
tool_response: any;
|
|
}
|
|
|
|
/**
|
|
* Save Hook Main Logic - Fire-and-forget HTTP client
|
|
*/
|
|
async function saveHook(input?: PostToolUseInput): Promise<void> {
|
|
// Ensure worker is running before any other logic
|
|
await ensureWorkerRunning();
|
|
|
|
if (!input) {
|
|
throw new Error('saveHook requires input');
|
|
}
|
|
|
|
const { session_id, cwd, tool_name, tool_input, tool_response } = input;
|
|
|
|
const port = getWorkerPort();
|
|
|
|
const toolStr = logger.formatTool(tool_name, tool_input);
|
|
|
|
logger.dataIn('HOOK', `PostToolUse: ${toolStr}`, {
|
|
workerPort: port
|
|
});
|
|
|
|
// Validate required fields before sending to worker
|
|
if (!cwd) {
|
|
throw new Error(`Missing cwd in PostToolUse hook input for session ${session_id}, tool ${tool_name}`);
|
|
}
|
|
|
|
// Send to worker - worker handles privacy check and database operations
|
|
const response = await fetch(`http://127.0.0.1:${port}/api/sessions/observations`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
contentSessionId: session_id,
|
|
tool_name,
|
|
tool_input,
|
|
tool_response,
|
|
cwd
|
|
})
|
|
// Note: Removed signal to avoid Windows Bun cleanup issue (libuv assertion)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Observation storage failed: ${response.status}`);
|
|
}
|
|
|
|
logger.debug('HOOK', 'Observation sent successfully', { toolName: tool_name });
|
|
|
|
console.log(STANDARD_HOOK_RESPONSE);
|
|
}
|
|
|
|
// Entry Point
|
|
let input = '';
|
|
stdin.on('data', (chunk) => input += chunk);
|
|
stdin.on('end', async () => {
|
|
let parsed: PostToolUseInput | undefined;
|
|
try {
|
|
parsed = input ? JSON.parse(input) : undefined;
|
|
} catch (error) {
|
|
throw new Error(`Failed to parse hook input: ${error instanceof Error ? error.message : String(error)}`);
|
|
}
|
|
await saveHook(parsed);
|
|
});
|