@@ -6,6 +6,7 @@ import path from "node:path";
66import { beforeEach , describe , expect , test , vi } from "vitest" ;
77import { WebSocket } from "ws" ;
88import { createDeferred } from "../../test/helpers/promise.js" ;
9+ import { replyRunRegistry } from "../auto-reply/reply/reply-run-registry.js" ;
910import { loadSessionEntry } from "../config/sessions/session-accessor.js" ;
1011import { replaceTranscriptEvents } from "../config/sessions/session-accessor.sqlite-transcript-write.js" ;
1112import { emitAgentEvent } from "../infra/agent-events.js" ;
@@ -22,7 +23,10 @@ import {
2223 resetGatewayWorkAdmission ,
2324 tryBeginGatewaySuspendAdmission ,
2425} from "../process/gateway-work-admission.js" ;
25- import { beginSessionWorkAdmission } from "../sessions/session-lifecycle-admission.js" ;
26+ import {
27+ beginSessionWorkAdmission ,
28+ getActiveSessionWorkAdmissionCount ,
29+ } from "../sessions/session-lifecycle-admission.js" ;
2630import { extractFirstTextBlock } from "../shared/chat-message-content.js" ;
2731import { captureEnv , setTestEnvValue } from "../test-utils/env.js" ;
2832import { GATEWAY_CLIENT_MODES , GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js" ;
@@ -441,6 +445,87 @@ describe("gateway server chat", () => {
441445 } ) ;
442446 } ) ;
443447
448+ test ( "chat.send interrupt releases its admission when backend cancellation throws" , async ( ) => {
449+ await withMainSessionStore ( async ( ) => {
450+ const activeRunStarted = createDeferred ( ) ;
451+ mockGetReplyFromConfigOnce ( async ( _ctx , opts ) => {
452+ activeRunStarted . resolve ( undefined ) ;
453+ if ( ! opts ?. abortSignal ?. aborted ) {
454+ await new Promise < void > ( ( resolve ) => {
455+ opts ?. abortSignal ?. addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } ) ;
456+ } ) ;
457+ }
458+ return undefined ;
459+ } ) ;
460+ const active = await rpcReq ( ws , "chat.send" , {
461+ sessionKey : "main" ,
462+ message : "captured active turn" ,
463+ idempotencyKey : "idem-chat-interrupt-throw-old" ,
464+ } ) ;
465+ expect ( active . ok ) . toBe ( true ) ;
466+ await activeRunStarted . promise ;
467+
468+ const operation = replyRunRegistry . get ( "agent:main:main" ) ;
469+ expect ( operation ) . toBeDefined ( ) ;
470+ operation ?. attachBackend ( {
471+ kind : "embedded" ,
472+ cancel : ( ) => {
473+ throw new Error ( "cancel failed" ) ;
474+ } ,
475+ isStreaming : ( ) => true ,
476+ } ) ;
477+
478+ const res = await rpcReq ( ws , "chat.send" , {
479+ sessionKey : "main" ,
480+ message : "replace the captured turn" ,
481+ queueMode : "interrupt" ,
482+ idempotencyKey : "idem-chat-interrupt-throw-new" ,
483+ } ) ;
484+ expect ( res . ok ) . toBe ( false ) ;
485+ await waitForFast ( ( ) => expect ( getActiveSessionWorkAdmissionCount ( ) ) . toBe ( 0 ) ) ;
486+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
487+
488+ const reset = await rpcReq ( ws , "sessions.reset" , { key : "main" , reason : "new" } ) ;
489+ expect ( reset . ok ) . toBe ( true ) ;
490+ } ) ;
491+ } ) ;
492+
493+ test ( "chat.send interrupt releases its admission when session interruption throws" , async ( ) => {
494+ await withMainSessionStore ( async ( ) => {
495+ const storePath = testState . sessionStorePath ;
496+ if ( ! storePath ) {
497+ throw new Error ( "session store path was not initialized" ) ;
498+ }
499+ const activeAdmission = await beginSessionWorkAdmission ( {
500+ scope : storePath ,
501+ identities : [ "agent:main:main" , "sess-main" ] ,
502+ assertAllowed : ( ) => { } ,
503+ onInterrupt : ( ) => {
504+ activeAdmission . release ( ) ;
505+ throw new Error ( "session interruption failed" ) ;
506+ } ,
507+ } ) ;
508+
509+ try {
510+ const res = await rpcReq ( ws , "chat.send" , {
511+ sessionKey : "main" ,
512+ message : "replace non-reply session work" ,
513+ queueMode : "interrupt" ,
514+ idempotencyKey : "idem-chat-interrupt-non-reply-throw" ,
515+ } ) ;
516+
517+ expect ( res . ok ) . toBe ( false ) ;
518+ await waitForFast ( ( ) => expect ( getActiveSessionWorkAdmissionCount ( ) ) . toBe ( 0 ) ) ;
519+ await waitForFast ( ( ) => expect ( getActiveGatewayRootWorkCount ( ) ) . toBe ( 0 ) ) ;
520+
521+ const reset = await rpcReq ( ws , "sessions.reset" , { key : "main" , reason : "new" } ) ;
522+ expect ( reset . ok ) . toBe ( true ) ;
523+ } finally {
524+ activeAdmission . release ( ) ;
525+ }
526+ } ) ;
527+ } ) ;
528+
444529 test ( "chat.send interrupt drains a non-reply session admission before dispatching" , async ( ) => {
445530 await withMainSessionStore ( async ( ) => {
446531 const storePath = testState . sessionStorePath ;
0 commit comments