@@ -221,22 +221,21 @@ function clientHasAdminScope(client: GatewayRequestHandlerOptions["client"]): bo
221221 return scopes . includes ( ADMIN_SCOPE ) ;
222222}
223223
224- function respondDeletedAgentSessionForKey ( params : {
225- sessionKey : string ;
226- agentId ?: string ;
224+ function respondDeletedAgentSession ( params : {
225+ cfg : OpenClawConfig ;
226+ canonicalKey : string ;
227+ entry ?: SessionEntry | null ;
228+ acpMetadataSessionKey ?: string ;
227229 respond : GatewayRequestHandlerOptions [ "respond" ] ;
228230} ) : boolean {
229- const sessionLoadOptions = {
230- ... ( params . agentId ? { agentId : params . agentId } : { } ) ,
231- clone : false ,
232- } ;
233- const { cfg , entry , canonicalKey , legacyKey } = loadSessionEntry (
234- params . sessionKey ,
235- sessionLoadOptions ,
231+ const deletedAgentId = resolveDeletedAgentIdFromSessionKey (
232+ params . cfg ,
233+ params . canonicalKey ,
234+ params . entry ,
235+ {
236+ acpMetadataSessionKey : params . acpMetadataSessionKey ?? params . canonicalKey ,
237+ } ,
236238 ) ;
237- const deletedAgentId = resolveDeletedAgentIdFromSessionKey ( cfg , canonicalKey , entry , {
238- acpMetadataSessionKey : legacyKey ?? canonicalKey ,
239- } ) ;
240239 if ( deletedAgentId === null ) {
241240 return false ;
242241 }
@@ -251,6 +250,24 @@ function respondDeletedAgentSessionForKey(params: {
251250 return true ;
252251}
253252
253+ function respondDeletedAgentSessionForKey ( params : {
254+ sessionKey : string ;
255+ agentId ?: string ;
256+ respond : GatewayRequestHandlerOptions [ "respond" ] ;
257+ } ) : boolean {
258+ const { cfg, entry, canonicalKey, legacyKey } = loadSessionEntry ( params . sessionKey , {
259+ ...( params . agentId ? { agentId : params . agentId } : { } ) ,
260+ clone : false ,
261+ } ) ;
262+ return respondDeletedAgentSession ( {
263+ cfg,
264+ canonicalKey,
265+ entry,
266+ acpMetadataSessionKey : legacyKey ,
267+ respond : params . respond ,
268+ } ) ;
269+ }
270+
254271function resolveAllowModelOverrideFromClient (
255272 client : GatewayRequestHandlerOptions [ "client" ] ,
256273) : boolean {
@@ -1505,11 +1522,8 @@ export const agentHandlers: GatewayRequestHandlers = {
15051522 return ;
15061523 }
15071524 }
1508- // Reject orphaned deleted-agent session keys before any side effect (dedupe
1509- // reservation, attachment media offload, reset, dispatch), matching the
1510- // chat.send / sessions.send invariant. Voice-wake auto-routing below only
1511- // retargets to knownAgents, so a post-routing key is never deleted; guarding
1512- // the original requestedSessionKey here covers every dispatch path.
1525+ // Keep deleted-agent rejection ahead of dedupe, media offload, reset, and
1526+ // dispatch so agent RPC shares the chat.send / sessions.send boundary.
15131527 if (
15141528 requestedSessionKey &&
15151529 respondDeletedAgentSessionForKey ( { sessionKey : requestedSessionKey , agentId, respond } )
@@ -1834,18 +1848,15 @@ export const agentHandlers: GatewayRequestHandlers = {
18341848 legacyKey,
18351849 } = loadSessionEntry ( requestedSessionKey , sessionLoadOptions ) ;
18361850 cfgForAgent = cfgLocal ;
1837- const deletedAgentId = resolveDeletedAgentIdFromSessionKey ( cfgLocal , canonicalKey , entry , {
1838- acpMetadataSessionKey : legacyKey ?? canonicalKey ,
1839- } ) ;
1840- if ( deletedAgentId !== null ) {
1841- respond (
1842- false ,
1843- undefined ,
1844- errorShape (
1845- ErrorCodes . INVALID_REQUEST ,
1846- `Agent "${ deletedAgentId } " no longer exists in configuration` ,
1847- ) ,
1848- ) ;
1851+ if (
1852+ respondDeletedAgentSession ( {
1853+ cfg : cfgLocal ,
1854+ canonicalKey,
1855+ entry,
1856+ acpMetadataSessionKey : legacyKey ,
1857+ respond,
1858+ } )
1859+ ) {
18491860 return ;
18501861 }
18511862 const sessionMaintenanceConfig = resolveMaintenanceConfigFromInput (
0 commit comments