Skip to content

Commit d65fca2

Browse files
committed
fix(gateway): retain detached hook admission
1 parent a4c1e22 commit d65fca2

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/gateway/server/hooks.agent-trust.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
* Hook endpoint trust tests for agent dispatch and gateway network config.
33
*/
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import {
6+
getActiveGatewayRootWorkCount,
7+
isGatewaySubordinateWorkAdmissionClosed,
8+
resetGatewayWorkAdmission,
9+
tryBeginGatewayRootWorkAdmission,
10+
} from "../../process/gateway-work-admission.js";
511

612
const enqueueSystemEventMock = vi.fn();
713
const requestHeartbeatMock = vi.fn();
@@ -124,15 +130,49 @@ function logWarnMetaFor(message: string, predicate?: (meta: HookLogMeta) => bool
124130

125131
describe("dispatchAgentHook trust handling", () => {
126132
beforeEach(() => {
133+
resetGatewayWorkAdmission();
127134
vi.clearAllMocks();
128135
capturedDispatchAgentHook = undefined;
129136
createGatewayHooksRequestHandler(buildMinimalParams());
130137
});
131138

132139
afterEach(() => {
140+
resetGatewayWorkAdmission();
133141
vi.restoreAllMocks();
134142
});
135143

144+
it("retains detached agent work after the hook request releases admission", async () => {
145+
let continueRun = () => {};
146+
let subordinateAdmissionClosed: boolean | undefined;
147+
const runGate = new Promise<void>((resolve) => {
148+
continueRun = resolve;
149+
});
150+
runCronIsolatedAgentTurnMock.mockImplementationOnce(async () => {
151+
await runGate;
152+
subordinateAdmissionClosed = isGatewaySubordinateWorkAdmissionClosed();
153+
return { status: "ok", summary: "done", delivered: false };
154+
});
155+
const requestAdmission = tryBeginGatewayRootWorkAdmission();
156+
expect(requestAdmission).not.toBeNull();
157+
158+
await requestAdmission?.run(async () => {
159+
dispatchAgentHook(buildAgentPayload("Async hook"));
160+
expect(getActiveGatewayRootWorkCount()).toBe(2);
161+
});
162+
requestAdmission?.release();
163+
164+
expect(getActiveGatewayRootWorkCount()).toBe(1);
165+
continueRun();
166+
await vi.waitFor(() =>
167+
expect(logHooksInfoMock).toHaveBeenCalledWith(
168+
"hook agent run completed without announcement",
169+
expect.any(Object),
170+
),
171+
);
172+
expect(subordinateAdmissionClosed).toBe(false);
173+
expect(getActiveGatewayRootWorkCount()).toBe(0);
174+
});
175+
136176
it("does not announce successful deliver:false hook results", async () => {
137177
runCronIsolatedAgentTurnMock.mockResolvedValueOnce({
138178
status: "ok",

‎src/gateway/server/hooks.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { CronJob } from "../../cron/types.js";
2020
import { requestHeartbeat } from "../../infra/heartbeat-wake.js";
2121
import { enqueueSystemEvent } from "../../infra/system-events.js";
2222
import type { createSubsystemLogger } from "../../logging/subsystem.js";
23+
import { runWithGatewayIndependentRootWorkContinuation } from "../../process/gateway-work-admission.js";
2324
import type { HookAgentDispatchPayload, HooksConfigResolved } from "../hooks.js";
2425
import { createHooksRequestHandler, type HookClientIpConfig } from "./hooks-request-handler.js";
2526

@@ -150,7 +151,7 @@ export function createGatewayHooksRequestHandler(params: {
150151
};
151152

152153
let hookEventSessionKey: string | undefined;
153-
void (async () => {
154+
void runWithGatewayIndependentRootWorkContinuation(async () => {
154155
try {
155156
// Agent hooks run after the HTTP response path has returned, so failure
156157
// handling must record a system event instead of throwing to the caller.
@@ -222,7 +223,7 @@ export function createGatewayHooksRequestHandler(params: {
222223
});
223224
}
224225
}
225-
})();
226+
});
226227

227228
return runId;
228229
};

0 commit comments

Comments
 (0)