@@ -7,17 +7,14 @@ import {
77import { resolveDefaultAgentId } from "../../agents/agent-scope.js" ;
88import { normalizeAgentId } from "../../routing/session-key.js" ;
99import { parseAgentSessionKey } from "../../sessions/session-key-utils.js" ;
10- import { abortChatRunById } from "../chat-abort.js" ;
11- import { abortQueuedChatTurnById } from "../chat-queued-turns.js" ;
10+ import { abortChatRunById , type ChatAbortControllerEntry } from "../chat-abort.js" ;
11+ import { abortQueuedChatTurnById , type QueuedChatTurnEntry } from "../chat-queued-turns.js" ;
1212import { pendingChatSendDedupeKey } from "../server-shared.js" ;
1313import { loadSessionEntry , resolveSessionStoreKey } from "../session-utils.js" ;
1414import { asWorkerInferenceControl } from "../worker-environments/inference-control.js" ;
1515import {
1616 canRequesterAbortChatRun ,
17- canRequesterAbortChatRunWithoutSessionMatch ,
1817 canRequesterAbortPreRegisteredRun ,
19- canRequesterAbortQueuedChatTurn ,
20- canRequesterAbortQueuedChatTurnWithoutSessionMatch ,
2118 readPreRegisteredAgentDedupePayloadForSession ,
2219 resolveChatAbortRequester ,
2320 resolveStoredGlobalRunAgentId ,
@@ -28,7 +25,6 @@ import {
2825 abortChatRunsForSessionKeyWithPartials ,
2926 cancelWorkerInferenceForSession ,
3027 createChatAbortOps ,
31- ensureChatQueuedTurns ,
3228 persistAbortedPartials ,
3329} from "./chat-abort-runtime.js" ;
3430import {
@@ -43,6 +39,11 @@ type ChatAbortLifecycle = {
4339 excludeRunIds ?: ReadonlySet < string > ;
4440} ;
4541
42+ type ChatAbortTarget = Pick <
43+ ChatAbortControllerEntry | QueuedChatTurnEntry ,
44+ "sessionKey" | "agentId" | "ownerConnId" | "ownerDeviceId"
45+ > ;
46+
4647export async function handleChatAbortRequestWithLifecycle (
4748 { params, respond, context, client } : GatewayRequestHandlerOptions ,
4849 lifecycle : ChatAbortLifecycle = { } ,
@@ -130,45 +131,60 @@ export async function handleChatAbortRequestWithLifecycle(
130131 return ;
131132 }
132133 const normalizedAgentIdOverride = abortAgentId ?. toLowerCase ( ) ;
134+ const authorizeRunTarget = ( target : ChatAbortTarget ) : boolean => {
135+ if (
136+ target . sessionKey !== rawSessionKey &&
137+ target . sessionKey !== canonicalAbortSessionKey &&
138+ ! canRequesterAbortChatRun ( target , requester , { requireOwnerMatch : true } )
139+ ) {
140+ respond (
141+ false ,
142+ undefined ,
143+ errorShape ( ErrorCodes . INVALID_REQUEST , "runId does not match sessionKey" ) ,
144+ ) ;
145+ return false ;
146+ }
147+ if (
148+ normalizedAgentIdOverride &&
149+ target . sessionKey === "global" &&
150+ resolveStoredGlobalRunAgentId ( target . agentId , defaultAgentId ) !== normalizedAgentIdOverride
151+ ) {
152+ respond (
153+ false ,
154+ undefined ,
155+ errorShape ( ErrorCodes . INVALID_REQUEST , "runId does not match agentId" ) ,
156+ ) ;
157+ return false ;
158+ }
159+ if ( ! canRequesterAbortChatRun ( target , requester ) ) {
160+ respond ( false , undefined , errorShape ( ErrorCodes . INVALID_REQUEST , "unauthorized" ) ) ;
161+ return false ;
162+ }
163+ return true ;
164+ } ;
133165
134166 const active = context . chatAbortControllers . get ( runId ) ;
135167 if ( ! active ) {
136168 const readPendingRunForAbort = (
137169 entry : GatewayRequestContext [ "dedupe" ] extends Map < string , infer T > ? T | undefined : never ,
138170 ) => {
139- const canonicalMatch = readPreRegisteredAgentDedupePayloadForSession ( {
140- entry,
141- runId,
142- sessionKey : canonicalAbortSessionKey ,
143- agentId : abortAgentId ,
144- defaultAgentId,
145- includeHidden : true ,
146- } ) ;
147- if ( canonicalMatch ) {
148- return {
149- sessionKey : normalizeUnknownText ( canonicalMatch . sessionKey )
150- ? canonicalAbortSessionKey
151- : undefined ,
152- payload : canonicalMatch ,
153- } ;
154- }
155- if ( rawSessionKey === canonicalAbortSessionKey ) {
156- return undefined ;
171+ for ( const sessionKey of new Set ( [ canonicalAbortSessionKey , rawSessionKey ] ) ) {
172+ const payload = readPreRegisteredAgentDedupePayloadForSession ( {
173+ entry,
174+ runId,
175+ sessionKey,
176+ agentId : abortAgentId ,
177+ defaultAgentId,
178+ includeHidden : true ,
179+ } ) ;
180+ if ( payload ) {
181+ return {
182+ sessionKey : normalizeUnknownText ( payload . sessionKey ) ? sessionKey : undefined ,
183+ payload,
184+ } ;
185+ }
157186 }
158- const aliasMatch = readPreRegisteredAgentDedupePayloadForSession ( {
159- entry,
160- runId,
161- sessionKey : rawSessionKey ,
162- agentId : abortAgentId ,
163- defaultAgentId,
164- includeHidden : true ,
165- } ) ;
166- return aliasMatch
167- ? {
168- sessionKey : normalizeUnknownText ( aliasMatch . sessionKey ) ? rawSessionKey : undefined ,
169- payload : aliasMatch ,
170- }
171- : undefined ;
187+ return undefined ;
172188 } ;
173189 const pendingChatMatch = readPendingRunForAbort (
174190 context . dedupe . get ( pendingChatSendDedupeKey ( runId ) ) ,
@@ -207,35 +223,10 @@ export async function handleChatAbortRequestWithLifecycle(
207223 }
208224 // Queued followup/collect turns keep a cancel identity after chat.send
209225 // terminalizes; abort them here so Esc cannot report done while they run.
210- const chatQueuedTurns = ensureChatQueuedTurns ( context ) ;
226+ const chatQueuedTurns = context . chatQueuedTurns ;
211227 const queued = chatQueuedTurns . get ( runId ) ;
212228 if ( queued ) {
213- const abortSessionKeysForQueued = new Set ( [ rawSessionKey , canonicalAbortSessionKey ] ) ;
214- if (
215- ! abortSessionKeysForQueued . has ( queued . sessionKey ) &&
216- ! canRequesterAbortQueuedChatTurnWithoutSessionMatch ( queued , requester )
217- ) {
218- respond (
219- false ,
220- undefined ,
221- errorShape ( ErrorCodes . INVALID_REQUEST , "runId does not match sessionKey" ) ,
222- ) ;
223- return ;
224- }
225- if (
226- normalizedAgentIdOverride &&
227- queued . sessionKey === "global" &&
228- resolveStoredGlobalRunAgentId ( queued . agentId , defaultAgentId ) !== normalizedAgentIdOverride
229- ) {
230- respond (
231- false ,
232- undefined ,
233- errorShape ( ErrorCodes . INVALID_REQUEST , "runId does not match agentId" ) ,
234- ) ;
235- return ;
236- }
237- if ( ! canRequesterAbortQueuedChatTurn ( queued , requester ) ) {
238- respond ( false , undefined , errorShape ( ErrorCodes . INVALID_REQUEST , "unauthorized" ) ) ;
229+ if ( ! authorizeRunTarget ( queued ) ) {
239230 return ;
240231 }
241232 const queuedRes = abortQueuedChatTurnById ( chatQueuedTurns , {
@@ -265,32 +256,7 @@ export async function handleChatAbortRequestWithLifecycle(
265256 respondWithWorkerRuns ( [ ] ) ;
266257 return ;
267258 }
268- const abortSessionKeysForRun = new Set ( [ rawSessionKey , canonicalAbortSessionKey ] ) ;
269- if (
270- ! abortSessionKeysForRun . has ( active . sessionKey ) &&
271- ! canRequesterAbortChatRunWithoutSessionMatch ( active , requester )
272- ) {
273- respond (
274- false ,
275- undefined ,
276- errorShape ( ErrorCodes . INVALID_REQUEST , "runId does not match sessionKey" ) ,
277- ) ;
278- return ;
279- }
280- if (
281- normalizedAgentIdOverride &&
282- active . sessionKey === "global" &&
283- resolveStoredGlobalRunAgentId ( active . agentId , defaultAgentId ) !== normalizedAgentIdOverride
284- ) {
285- respond (
286- false ,
287- undefined ,
288- errorShape ( ErrorCodes . INVALID_REQUEST , "runId does not match agentId" ) ,
289- ) ;
290- return ;
291- }
292- if ( ! canRequesterAbortChatRun ( active , requester ) ) {
293- respond ( false , undefined , errorShape ( ErrorCodes . INVALID_REQUEST , "unauthorized" ) ) ;
259+ if ( ! authorizeRunTarget ( active ) ) {
294260 return ;
295261 }
296262
0 commit comments