@@ -3,6 +3,7 @@ import type { Principal, Run } from '../core/contracts.js';
33import { terminal , visibleSubject } from '../core/contracts.js' ;
44import { responseObject } from './response-object.js' ;
55import { Fault } from '../core/util.js' ;
6+ import { terminalNotice } from '../store/terminal-publication.js' ;
67import { isTerminalEvent } from '../core/events.js' ;
78import type { KernelRuntime } from './runtime.js' ;
89import { authorize , checkProviderKey } from './submission.js' ;
@@ -40,11 +41,17 @@ export function finishOrphan(rt: KernelRuntime, run: Run, unlinked = false): voi
4041 : 'The parent run is no longer active; this queued sub-agent run was not started.' ,
4142 } ;
4243 delete run . checkpoint . suspension ;
43- rt . store . checkpointActions ( run , 'expire' , [ ] , ( view ) => ( {
44- type : 'response.cancelled' ,
45- data : { response : responseObject ( view ) } ,
46- } ) ) ;
47- releaseRun ( rt , run ) ;
44+ rt . store . checkpointActions (
45+ run ,
46+ 'expire' ,
47+ [ ] ,
48+ ( view ) => ( {
49+ type : 'response.cancelled' ,
50+ data : { response : responseObject ( view ) } ,
51+ } ) ,
52+ undefined ,
53+ ( ) => releaseRun ( rt , run ) ,
54+ ) ;
4855}
4956export function releaseRun ( rt : KernelRuntime , run : Run ) : void {
5057 if ( run . status === 'completed' ) rememberProjectResult ( rt . store , run ) ;
@@ -89,6 +96,7 @@ export function forgetProviderKey(rt: KernelRuntime, runId: string, status: Run[
8996export async function cancel ( rt : KernelRuntime , principal : Principal , runId : string ) : Promise < Run > {
9097 const run = rt . store . run ( runId , principal . owner , visibleSubject ( principal ) ) ;
9198 authorize ( principal , run . agentId , 'invoke' ) ;
99+ rt . store . terminalPublications . requireConfirmed ( runId ) ;
92100 if ( terminal ( run . status ) ) return run ;
93101 if ( run . status === 'queued' ) {
94102 rt . admission . finishParked ( run , 'cancelled' ) ;
@@ -125,6 +133,7 @@ export function resume(
125133) : Run {
126134 const run = rt . store . run ( runId , principal . owner , visibleSubject ( principal ) ) ;
127135 authorize ( principal , run . agentId , 'invoke' ) ;
136+ rt . store . terminalPublications . requireConfirmed ( runId ) ;
128137 if ( run . status !== 'interrupted' )
129138 throw new Fault ( 'not_resumable' , 'Only interrupted responses can be resumed.' , 409 ) ;
130139 if ( providerKey !== undefined ) {
@@ -231,11 +240,14 @@ export async function wait(
231240 return new Promise < Run > ( ( resolve , reject ) => {
232241 const cleanup = ( ) => {
233242 rt . store . events . off ( runId , onEvent ) ;
243+ rt . store . events . off ( terminalNotice ( runId ) , onUnconfirmed ) ;
234244 rt . shutdown . signal . removeEventListener ( 'abort' , onClose ) ;
235245 signal ?. removeEventListener ( 'abort' , onAbort ) ;
236246 } ;
237247 const check = ( ) => {
238248 try {
249+ const issue = rt . store . terminalPublications . issue ( runId ) ;
250+ if ( issue ) return onUnconfirmed ( issue ) ;
239251 const run = rt . store . run ( runId ) ;
240252 if ( terminal ( run . status ) || ( includeActions && run . status === 'requires_action' ) ) {
241253 cleanup ( ) ;
@@ -254,6 +266,10 @@ export async function wait(
254266 cleanup ( ) ;
255267 reject ( new Fault ( 'shutting_down' , 'Gateway is shutting down.' , 503 ) ) ;
256268 } ;
269+ const onUnconfirmed = ( fault : Fault ) => {
270+ cleanup ( ) ;
271+ reject ( fault ) ;
272+ } ;
257273 const onAbort = ( ) => {
258274 cleanup ( ) ;
259275 reject (
@@ -267,6 +283,7 @@ export async function wait(
267283 ) ;
268284 } ;
269285 rt . store . events . on ( runId , onEvent ) ;
286+ rt . store . events . on ( terminalNotice ( runId ) , onUnconfirmed ) ;
270287 rt . shutdown . signal . addEventListener ( 'abort' , onClose , { once : true } ) ;
271288 signal ?. addEventListener ( 'abort' , onAbort , { once : true } ) ;
272289 if ( rt . shutdown . signal . aborted ) onClose ( ) ;
0 commit comments