|
2 | 2 | * Hook endpoint trust tests for agent dispatch and gateway network config. |
3 | 3 | */ |
4 | 4 | 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"; |
5 | 11 |
|
6 | 12 | const enqueueSystemEventMock = vi.fn(); |
7 | 13 | const requestHeartbeatMock = vi.fn(); |
@@ -124,15 +130,49 @@ function logWarnMetaFor(message: string, predicate?: (meta: HookLogMeta) => bool |
124 | 130 |
|
125 | 131 | describe("dispatchAgentHook trust handling", () => { |
126 | 132 | beforeEach(() => { |
| 133 | + resetGatewayWorkAdmission(); |
127 | 134 | vi.clearAllMocks(); |
128 | 135 | capturedDispatchAgentHook = undefined; |
129 | 136 | createGatewayHooksRequestHandler(buildMinimalParams()); |
130 | 137 | }); |
131 | 138 |
|
132 | 139 | afterEach(() => { |
| 140 | + resetGatewayWorkAdmission(); |
133 | 141 | vi.restoreAllMocks(); |
134 | 142 | }); |
135 | 143 |
|
| 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 | + |
136 | 176 | it("does not announce successful deliver:false hook results", async () => { |
137 | 177 | runCronIsolatedAgentTurnMock.mockResolvedValueOnce({ |
138 | 178 | status: "ok", |
|
0 commit comments