@@ -95,10 +95,22 @@ function buildAgentPayload(name: string, agentId?: string) {
9595}
9696
9797function dispatchAgentHook ( payload : unknown ) : unknown {
98+ return resolveDispatchAgentHook ( ) ( payload ) ;
99+ }
100+
101+ function resolveDispatchAgentHook ( ) : ( ...args : unknown [ ] ) => unknown {
98102 if ( ! capturedDispatchAgentHook ) {
99103 throw new Error ( "dispatchAgentHook missing" ) ;
100104 }
101- return capturedDispatchAgentHook ( payload ) ;
105+ return capturedDispatchAgentHook ;
106+ }
107+
108+ function createDeferred ( ) {
109+ let resolve ! : ( ) => void ;
110+ const promise = new Promise < void > ( ( innerResolve ) => {
111+ resolve = innerResolve ;
112+ } ) ;
113+ return { promise, resolve } ;
102114}
103115
104116type HookLogMeta = {
@@ -139,6 +151,7 @@ describe("dispatchAgentHook trust handling", () => {
139151 beforeEach ( ( ) => {
140152 resetGatewayWorkAdmission ( ) ;
141153 vi . clearAllMocks ( ) ;
154+ loadConfigMock . mockImplementation ( ( ) => ( { } ) ) ;
142155 capturedDispatchAgentHook = undefined ;
143156 createGatewayHooksRequestHandler ( buildMinimalParams ( ) ) ;
144157 } ) ;
@@ -180,6 +193,167 @@ describe("dispatchAgentHook trust handling", () => {
180193 expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ;
181194 } ) ;
182195
196+ it ( "serializes canonical aliases for the same session in dispatch order" , async ( ) => {
197+ const dispatch = resolveDispatchAgentHook ( ) ;
198+ const firstGate = createDeferred ( ) ;
199+ runCronIsolatedAgentTurnMock . mockImplementationOnce ( async ( ) => {
200+ await firstGate . promise ;
201+ return { status : "ok" , summary : "first done" , delivered : false } ;
202+ } ) ;
203+ runCronIsolatedAgentTurnMock . mockResolvedValueOnce ( {
204+ status : "ok" ,
205+ summary : "second done" ,
206+ delivered : false ,
207+ } ) ;
208+
209+ dispatch ( {
210+ ...buildAgentPayload ( "First" ) ,
211+ message : "first" ,
212+ sessionKey : "main" ,
213+ } ) ;
214+ dispatch ( {
215+ ...buildAgentPayload ( "Second" ) ,
216+ message : "second" ,
217+ sessionKey : "agent:main:main" ,
218+ } ) ;
219+
220+ await waitForFast ( ( ) => expect ( runCronIsolatedAgentTurnMock ) . toHaveBeenCalledTimes ( 1 ) ) ;
221+ expect ( runCronIsolatedAgentTurnMock . mock . calls [ 0 ] ?. [ 0 ] ) . toMatchObject ( {
222+ message : "first" ,
223+ sessionKey : "main" ,
224+ } ) ;
225+
226+ firstGate . resolve ( ) ;
227+
228+ await waitForFast ( ( ) => expect ( runCronIsolatedAgentTurnMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
229+ expect ( runCronIsolatedAgentTurnMock . mock . calls [ 1 ] ?. [ 0 ] ) . toMatchObject ( {
230+ message : "second" ,
231+ sessionKey : "agent:main:main" ,
232+ } ) ;
233+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
234+ } ) ;
235+
236+ it ( "runs different sessions in parallel" , async ( ) => {
237+ const dispatch = resolveDispatchAgentHook ( ) ;
238+ const firstGate = createDeferred ( ) ;
239+ const secondGate = createDeferred ( ) ;
240+ runCronIsolatedAgentTurnMock . mockImplementationOnce ( async ( ) => {
241+ await firstGate . promise ;
242+ return { status : "ok" , summary : "first done" , delivered : false } ;
243+ } ) ;
244+
245+ runCronIsolatedAgentTurnMock . mockImplementationOnce ( async ( ) => {
246+ await secondGate . promise ;
247+ return { status : "ok" , summary : "second done" , delivered : false } ;
248+ } ) ;
249+
250+ dispatch ( {
251+ ...buildAgentPayload ( "First" ) ,
252+ message : "first" ,
253+ sessionKey : "agent:main:session-a" ,
254+ } ) ;
255+ dispatch ( {
256+ ...buildAgentPayload ( "Second" ) ,
257+ message : "second" ,
258+ sessionKey : "agent:main:session-b" ,
259+ } ) ;
260+
261+ expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 2 ) ;
262+
263+ try {
264+ await waitForFast ( ( ) => expect ( runCronIsolatedAgentTurnMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
265+ } finally {
266+ firstGate . resolve ( ) ;
267+ secondGate . resolve ( ) ;
268+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
269+ }
270+ } ) ;
271+
272+ it ( "uses fresh config when a queued hook starts after reload" , async ( ) => {
273+ const dispatch = resolveDispatchAgentHook ( ) ;
274+ let currentConfig : { session ?: { mainKey ?: string } } = { } ;
275+ loadConfigMock . mockImplementation ( ( ) => currentConfig ) ;
276+ const firstGate = createDeferred ( ) ;
277+ runCronIsolatedAgentTurnMock . mockImplementationOnce ( async ( ) => {
278+ await firstGate . promise ;
279+ return { status : "ok" , summary : "first done" , delivered : false } ;
280+ } ) ;
281+ runCronIsolatedAgentTurnMock . mockResolvedValueOnce ( {
282+ status : "ok" ,
283+ summary : "second done" ,
284+ delivered : false ,
285+ } ) ;
286+
287+ dispatch ( { ...buildAgentPayload ( "First" ) , message : "first" , sessionKey : "main" } ) ;
288+ dispatch ( { ...buildAgentPayload ( "Second" ) , message : "second" , sessionKey : "main" } ) ;
289+ await waitForFast ( ( ) => expect ( runCronIsolatedAgentTurnMock ) . toHaveBeenCalledTimes ( 1 ) ) ;
290+
291+ currentConfig = { session : { mainKey : "reloaded" } } ;
292+ firstGate . resolve ( ) ;
293+
294+ await waitForFast ( ( ) => expect ( runCronIsolatedAgentTurnMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
295+ expect ( runCronIsolatedAgentTurnMock . mock . calls [ 1 ] ?. [ 0 ] ) . toMatchObject ( {
296+ agentId : "main" ,
297+ cfg : currentConfig ,
298+ message : "second" ,
299+ sessionKey : "main" ,
300+ } ) ;
301+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
302+ } ) ;
303+
304+ it ( "continues a same-session hook queue after a failed run" , async ( ) => {
305+ const dispatch = resolveDispatchAgentHook ( ) ;
306+ runCronIsolatedAgentTurnMock . mockRejectedValueOnce ( new Error ( "agent exploded" ) ) ;
307+ runCronIsolatedAgentTurnMock . mockResolvedValueOnce ( {
308+ status : "ok" ,
309+ summary : "second done" ,
310+ delivered : false ,
311+ } ) ;
312+
313+ dispatch ( {
314+ ...buildAgentPayload ( "First" ) ,
315+ message : "first" ,
316+ sessionKey : "shared-session" ,
317+ } ) ;
318+ dispatch ( {
319+ ...buildAgentPayload ( "Second" ) ,
320+ message : "second" ,
321+ sessionKey : "shared-session" ,
322+ } ) ;
323+
324+ await waitForFast ( ( ) =>
325+ expect ( enqueueSystemEventMock ) . toHaveBeenCalledWith (
326+ "Hook First (error): Error: agent exploded" ,
327+ {
328+ sessionKey : "agent:main:main" ,
329+ } ,
330+ ) ,
331+ ) ;
332+ await waitForFast ( ( ) => expect ( runCronIsolatedAgentTurnMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
333+ expect ( runCronIsolatedAgentTurnMock . mock . calls [ 1 ] ?. [ 0 ] ) . toMatchObject ( {
334+ message : "second" ,
335+ sessionKey : "shared-session" ,
336+ } ) ;
337+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
338+ } ) ;
339+
340+ it ( "reports runtime-config failures after returning a run id" , async ( ) => {
341+ loadConfigMock . mockImplementationOnce ( ( ) => {
342+ throw new Error ( "config exploded" ) ;
343+ } ) ;
344+
345+ const runId = dispatchAgentHook ( buildAgentPayload ( "Config" ) ) ;
346+
347+ expect ( runId ) . toEqual ( expect . any ( String ) ) ;
348+ await waitForFast ( ( ) =>
349+ expect ( enqueueSystemEventMock ) . toHaveBeenCalledWith (
350+ "Hook Config (error): Error: config exploded" ,
351+ { sessionKey : "main-session" } ,
352+ ) ,
353+ ) ;
354+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
355+ } ) ;
356+
183357 it ( "does not announce successful deliver:false hook results" , async ( ) => {
184358 runCronIsolatedAgentTurnMock . mockResolvedValueOnce ( {
185359 status : "ok" ,
0 commit comments