@@ -11,8 +11,10 @@ import { expectSubagentFollowupReactivation } from "./subagent-followup.test-hel
1111import type { GatewayRequestContext , RespondFn } from "./types.js" ;
1212
1313const loadSessionEntryMock = vi . fn ( ) ;
14+ const loadSessionEntryReadOnlyMock = vi . fn ( ) ;
1415const readSessionMessageCountAsyncMock = vi . fn ( ) ;
1516const loadGatewaySessionRowMock = vi . fn ( ) ;
17+ const resolveDeletedAgentIdFromSessionKeyMock = vi . fn ( ) ;
1618const getLatestSubagentRunByChildSessionKeyMock = vi . fn ( ) ;
1719const replaceSubagentRunAfterSteerMock = vi . fn ( ) ;
1820const chatSendMock = vi . fn ( ) ;
@@ -45,14 +47,13 @@ vi.mock("../../auto-reply/reply/queue/cleanup.js", async () => {
4547 } ;
4648} ) ;
4749
48- vi . mock ( "../session-utils.js" , async ( ) => {
49- const actual = await vi . importActual < typeof import ( "../session-utils.js" ) > ( "../session-utils.js" ) ;
50- return {
51- ...actual ,
52- loadSessionEntry : ( ...args : unknown [ ] ) => loadSessionEntryMock ( ...args ) ,
53- loadGatewaySessionRow : ( ...args : unknown [ ] ) => loadGatewaySessionRowMock ( ...args ) ,
54- } ;
55- } ) ;
50+ vi . mock ( "../session-utils.js" , ( ) => ( {
51+ loadSessionEntry : ( ...args : unknown [ ] ) => loadSessionEntryMock ( ...args ) ,
52+ loadSessionEntryReadOnly : ( ...args : unknown [ ] ) => loadSessionEntryReadOnlyMock ( ...args ) ,
53+ loadGatewaySessionRow : ( ...args : unknown [ ] ) => loadGatewaySessionRowMock ( ...args ) ,
54+ resolveDeletedAgentIdFromSessionKey : ( ...args : unknown [ ] ) =>
55+ resolveDeletedAgentIdFromSessionKeyMock ( ...args ) ,
56+ } ) ) ;
5657
5758vi . mock ( "../session-transcript-readers.js" , async ( ) => {
5859 const actual = await vi . importActual < typeof import ( "../session-transcript-readers.js" ) > (
@@ -112,8 +113,10 @@ function createRequestContext(overrides: Record<string, unknown> = {}): GatewayR
112113describe ( "sessions.send completed subagent follow-up status" , ( ) => {
113114 beforeEach ( ( ) => {
114115 loadSessionEntryMock . mockReset ( ) ;
116+ loadSessionEntryReadOnlyMock . mockReset ( ) ;
115117 readSessionMessageCountAsyncMock . mockReset ( ) . mockResolvedValue ( 0 ) ;
116118 loadGatewaySessionRowMock . mockReset ( ) ;
119+ resolveDeletedAgentIdFromSessionKeyMock . mockReset ( ) . mockReturnValue ( null ) ;
117120 getLatestSubagentRunByChildSessionKeyMock . mockReset ( ) ;
118121 replaceSubagentRunAfterSteerMock . mockReset ( ) ;
119122 chatSendMock . mockReset ( ) ;
@@ -137,6 +140,37 @@ describe("sessions.send completed subagent follow-up status", () => {
137140 ) ;
138141 } ) ;
139142
143+ for ( const method of [ "sessions.send" , "sessions.steer" ] as const ) {
144+ it ( `${ method } rejects keys belonging to a deleted agent` , async ( ) => {
145+ const orphanKey = "agent:deleted-agent:main" ;
146+ loadSessionEntryMock . mockReturnValue ( {
147+ cfg : { } ,
148+ canonicalKey : orphanKey ,
149+ storePath : "/tmp/sessions.json" ,
150+ entry : { sessionId : "sess-orphan" } ,
151+ } ) ;
152+ resolveDeletedAgentIdFromSessionKeyMock . mockReturnValue ( "deleted-agent" ) ;
153+
154+ const respondMock = vi . fn ( ) ;
155+ await expectDefined (
156+ sessionMessagingHandlers [ method ] ,
157+ "sessionMessagingHandlers[method] test invariant" ,
158+ ) ( {
159+ req : { id : "req-deleted-agent" } as never ,
160+ params : { key : orphanKey , message : "hi" } ,
161+ respond : respondMock as unknown as RespondFn ,
162+ context : createRequestContext ( ) ,
163+ client : null ,
164+ isWebchatConnect : ( ) => false ,
165+ } ) ;
166+
167+ expect ( respondMock ) . toHaveBeenCalledWith ( false , undefined , {
168+ code : ErrorCodes . INVALID_REQUEST ,
169+ message : 'Agent "deleted-agent" no longer exists in configuration' ,
170+ } ) ;
171+ } ) ;
172+ }
173+
140174 it ( "reactivates completed subagent sessions before broadcasting sessions.changed" , async ( ) => {
141175 const childSessionKey = "agent:main:subagent:followup" ;
142176 const completedRun = {
0 commit comments