Files
claude-mem/src/hooks/context-hook.ts
T
Erik M Jacobs 3ea180c1ef refactor(worker): Remove file-based locking and improve Windows stability
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>
2025-12-29 16:44:40 -05:00

75 lines
2.1 KiB
TypeScript

/**
* Context Hook - SessionStart
*
* Pure HTTP client - calls worker to generate context.
* 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 { ensureWorkerRunning, getWorkerPort } from "../shared/worker-utils.js";
import { HOOK_TIMEOUTS } from "../shared/hook-constants.js";
import { getProjectName } from "../utils/project-name.js";
import { logger } from "../utils/logger.js";
export interface SessionStartInput {
session_id: string;
transcript_path: string;
cwd: string;
hook_event_name?: string;
}
async function contextHook(input?: SessionStartInput): Promise<string> {
// Ensure worker is running before any other logic
await ensureWorkerRunning();
const cwd = input?.cwd ?? process.cwd();
const project = getProjectName(cwd);
const port = getWorkerPort();
const url = `http://127.0.0.1:${port}/api/context/inject?project=${encodeURIComponent(project)}`;
// Note: Removed AbortSignal.timeout due to Windows Bun cleanup issue (libuv assertion)
// Worker service has its own timeouts, so client-side timeout is redundant
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Context generation failed: ${response.status}`);
}
const result = await response.text();
return result.trim();
}
// Entry Point - handle stdin/stdout
const forceColors = process.argv.includes("--colors");
if (stdin.isTTY || forceColors) {
contextHook(undefined).then((text) => {
console.log(text);
process.exit(0);
});
} else {
let input = "";
stdin.on("data", (chunk) => (input += chunk));
stdin.on("end", async () => {
let parsed: SessionStartInput | undefined;
try {
parsed = input.trim() ? JSON.parse(input) : undefined;
} catch (error) {
throw new Error(`Failed to parse hook input: ${error instanceof Error ? error.message : String(error)}`);
}
const text = await contextHook(parsed);
console.log(
JSON.stringify({
hookSpecificOutput: {
hookEventName: "SessionStart",
additionalContext: text,
},
})
);
process.exit(0);
});
}