fix: ensure worker is running before executing hook logic in multiple scripts

This commit is contained in:
Alex Newman
2025-12-09 00:46:23 -05:00
parent ba2c098ec1
commit a00ca2b3ec
10 changed files with 28 additions and 44 deletions
+3 -3
View File
@@ -22,6 +22,9 @@ export interface SessionEndInput {
* Cleanup Hook Main Logic - Fire-and-forget HTTP client
*/
async function cleanupHook(input?: SessionEndInput): Promise<void> {
// Ensure worker is running before any other logic
await ensureWorkerRunning();
silentDebug('[cleanup-hook] Hook fired', {
session_id: input?.session_id,
cwd: input?.cwd,
@@ -44,9 +47,6 @@ async function cleanupHook(input?: SessionEndInput): Promise<void> {
const { session_id, reason } = input;
// Ensure worker is running
await ensureWorkerRunning();
const port = getWorkerPort();
try {
+4 -26
View File
@@ -9,7 +9,7 @@
import path from "path";
import { stdin } from "process";
import { execSync } from "child_process";
import { getWorkerPort } from "../shared/worker-utils.js";
import { ensureWorkerRunning, getWorkerPort } from "../shared/worker-utils.js";
export interface SessionStartInput {
session_id?: string;
@@ -20,36 +20,14 @@ export interface SessionStartInput {
[key: string]: any;
}
async function waitForPort(port: number, maxWaitMs: number = 10000): Promise<boolean> {
const startTime = Date.now();
const pollInterval = 100;
while (Date.now() - startTime < maxWaitMs) {
try {
execSync(`curl -s -f -m 1 "http://127.0.0.1:${port}/api/health" > /dev/null 2>&1`, {
timeout: 1000,
});
return true;
} catch {
await new Promise((resolve) => setTimeout(resolve, pollInterval));
}
}
return false;
}
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 = cwd ? path.basename(cwd) : "unknown-project";
const port = getWorkerPort();
// Wait for worker to be available
const isAvailable = await waitForPort(port);
if (!isAvailable) {
throw new Error(
`Worker service not available on port ${port} after 10s. Try: npm run worker:restart`
);
}
const url = `http://127.0.0.1:${port}/api/context/inject?project=${encodeURIComponent(project)}`;
const result = execSync(`curl -s "${url}"`, { encoding: "utf-8", timeout: 5000 });
return result.trim();
+3 -3
View File
@@ -53,6 +53,9 @@ export interface UserPromptSubmitInput {
* New Hook Main Logic
*/
async function newHook(input?: UserPromptSubmitInput): Promise<void> {
// Ensure worker is running before any other logic
await ensureWorkerRunning();
if (!input) {
throw new Error('newHook requires input');
}
@@ -79,9 +82,6 @@ async function newHook(input?: UserPromptSubmitInput): Promise<void> {
cwd_was: cwd
});
// Ensure worker is running
await ensureWorkerRunning();
const db = new SessionStore();
// CRITICAL: Use session_id from hook as THE source of truth
+3 -3
View File
@@ -33,6 +33,9 @@ const SKIP_TOOLS = new Set([
* 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');
}
@@ -44,9 +47,6 @@ async function saveHook(input?: PostToolUseInput): Promise<void> {
return;
}
// Ensure worker is running
await ensureWorkerRunning();
const port = getWorkerPort();
const toolStr = logger.formatTool(tool_name, tool_input);
+3 -3
View File
@@ -130,15 +130,15 @@ function extractLastAssistantMessage(transcriptPath: string): string {
* Summary Hook Main Logic - Fire-and-forget HTTP client
*/
async function summaryHook(input?: StopInput): Promise<void> {
// Ensure worker is running before any other logic
await ensureWorkerRunning();
if (!input) {
throw new Error('summaryHook requires input');
}
const { session_id } = input;
// Ensure worker is running
await ensureWorkerRunning();
const port = getWorkerPort();
// Extract last user AND assistant messages from transcript