@@ -79,6 +79,8 @@ export type Info = {
7979 error ?: string
8080 metadata ?: Record < string , unknown >
8181 notificationID ?: SessionMessage . ID
82+ /** Cancelled with `quiet`: the caller reported the cancel itself, so no completion notice follows. */
83+ quiet ?: true
8284}
8385
8486type Active = {
@@ -174,8 +176,10 @@ export interface Interface {
174176 readonly block : ( input : BlockInput ) => Effect . Effect < BlockResult | undefined >
175177 readonly background : ( id : string ) => Effect . Effect < Info | undefined >
176178 readonly backgroundAll : ( input : BackgroundAllInput ) => Effect . Effect < Info [ ] >
177- readonly cancel : ( id : string ) => Effect . Effect < Info | undefined >
178- readonly cancelSession : ( sessionID : SessionSchema . ID ) => Effect . Effect < Info [ ] >
179+ /** With `quiet`, no completion notice follows and the job's outbox row is removed. */
180+ readonly cancel : ( id : string , options ?: { readonly quiet ?: boolean } ) => Effect . Effect < Info | undefined >
181+ /** With `quiet`, like a quiet `cancel` for each job: none of them sends a notice. */
182+ readonly cancelSession : ( sessionID : SessionSchema . ID , options ?: { readonly quiet ?: boolean } ) => Effect . Effect < Info [ ] >
179183 readonly pendingBackground : Effect . Effect < readonly Background [ ] >
180184 readonly runningBackground : Effect . Effect < readonly Recovery [ ] >
181185 readonly backgroundJobs : Effect . Effect < readonly JobList . Item [ ] >
@@ -437,7 +441,7 @@ export const make = Effect.gen(function* () {
437441 const wait : Interface [ "wait" ] = Effect . fn ( "Job.wait" ) ( function * ( input ) {
438442 const job = ( yield * SynchronizedRef . get ( state . jobs ) ) . get ( input . id )
439443 if ( ! job ) return { timedOut : false }
440- return yield * Effect . gen ( function * ( ) {
444+ return yield * Effect . gen ( function * ( ) : Effect . fn . Return < WaitResult > {
441445 if ( job . info . status !== "running" ) return { info : snapshot ( job ) , timedOut : false }
442446 if ( input . timeout === undefined ) return { info : yield * Deferred . await ( job . done ) , timedOut : false }
443447 if ( input . timeout <= 0 ) return { info : snapshot ( job ) , timedOut : true }
@@ -449,6 +453,8 @@ export const make = Effect.gen(function* () {
449453 Effect . tap ( ( result ) =>
450454 result . info ?. status === "running" || job . recovery ? Effect . void : consume ( input . id , job . scope ) ,
451455 ) ,
456+ // A quietly cancelled job is gone, as if it had been acknowledged: its caller reported it, so no one else does.
457+ Effect . map ( ( result ) => ( result . info ?. quiet ? { timedOut : false } : result ) ) ,
452458 )
453459 } )
454460
@@ -556,26 +562,34 @@ export const make = Effect.gen(function* () {
556562 return result . map ( ( item ) => item . info )
557563 } )
558564
559- const cancelInternal = Effect . fnUntraced ( function * ( id : string , persist = true ) {
565+ // "caller" saves a cancelled outbox row for its notice, "quiet" leaves no row and no entry because the caller
566+ // reports the cancel itself, and "teardown" (cancelSession) only stops the work.
567+ // Uninterruptible: once the entry says cancelled, the rest must run, or waiters hang and the work keeps going.
568+ const cancelInternal = Effect . fnUntraced ( function * ( id : string , mode : "caller" | "quiet" | "teardown" ) {
569+ const quiet = mode === "quiet"
560570 const completed_at = yield * Clock . currentTimeMillis
561571 const result = yield * SynchronizedRef . modifyEffect (
562572 state . jobs ,
563573 Effect . fnUntraced ( function * ( jobs ) : Effect . fn . Return < readonly [ FinishResult , Map < string , Active > ] > {
564574 const job = jobs . get ( id )
565575 if ( ! job ) return [ { } , jobs ]
566576 if ( job . info . status !== "running" ) return [ { info : snapshot ( job ) , generation : job . scope } , jobs ]
577+ // Settling work is already reporting its outcome, so a quiet cancel can't keep it from notifying.
578+ if ( quiet && job . settling ) return [ { info : snapshot ( job ) } , jobs ]
567579 const next = {
568580 ...job ,
569581 blockingSessions : new Map < SessionSchema . ID , number > ( ) ,
570- cancelledByCaller : persist ,
582+ cancelledByCaller : mode !== "teardown" ,
571583 info : {
572584 ...job . info ,
573585 status : "cancelled" as const ,
574586 completed_at,
587+ ...( quiet ? { quiet : true as const } : { } ) ,
575588 } ,
576589 }
577590 // Settling work owns its outbox row: the settlement commit writes or deletes it.
578- if ( persist && ! job . settling ) yield * persistBackground ( next )
591+ if ( quiet && job . info . notificationID ) yield * deleteBackgroundRow ( job . info . notificationID )
592+ else if ( mode === "caller" && ! job . settling ) yield * persistBackground ( next )
579593 return [
580594 {
581595 info : snapshot ( next ) ,
@@ -589,21 +603,34 @@ export const make = Effect.gen(function* () {
589603 ]
590604 } ) ,
591605 )
592- if ( result . cancel ) yield * result . cancel
606+ // A failing hook must not keep the job from settling.
607+ if ( result . cancel ) yield * Effect . ignoreCause ( result . cancel )
593608 if ( result . info && result . done ) yield * Deferred . succeed ( result . done , result . info )
594609 if ( result . done ) yield * updated ( result . listed )
595610 if ( result . scope ) yield * Scope . close ( result . scope , Exit . void )
596611 if ( result . generation ) yield * consume ( id , result . generation )
612+ // Nothing acknowledges a quiet cancel, so its entry goes now. Notifiers already waiting got its info above.
613+ if ( result . info ?. quiet && result . scope ) {
614+ const scope = result . scope
615+ yield * SynchronizedRef . update ( state . jobs , ( jobs ) => {
616+ if ( jobs . get ( id ) ?. scope !== scope ) return jobs
617+ const next = new Map ( jobs )
618+ next . delete ( id )
619+ return next
620+ } )
621+ }
597622 return result . info
598- } )
623+ } , Effect . uninterruptible )
599624
600- const cancel : Interface [ "cancel" ] = Effect . fn ( "Job.cancel" ) ( ( id ) => cancelInternal ( id , true ) )
625+ const cancel : Interface [ "cancel" ] = Effect . fn ( "Job.cancel" ) ( ( id , options ) =>
626+ cancelInternal ( id , options ?. quiet ? "quiet" : "caller" ) ,
627+ )
601628
602- const cancelSession : Interface [ "cancelSession" ] = Effect . fn ( "Job.cancelSession" ) ( function * ( sessionID ) {
629+ const cancelSession : Interface [ "cancelSession" ] = Effect . fn ( "Job.cancelSession" ) ( function * ( sessionID , options ) {
603630 const ids = [ ...( yield * SynchronizedRef . get ( state . jobs ) ) . values ( ) ]
604631 . filter ( ( job ) => job . info . status === "running" && matchesSession ( job , sessionID ) )
605632 . map ( ( job ) => job . info . id )
606- const cancelled = yield * Effect . forEach ( ids , ( id ) => cancelInternal ( id , false ) )
633+ const cancelled = yield * Effect . forEach ( ids , ( id ) => cancelInternal ( id , options ?. quiet ? "quiet" : "teardown" ) )
607634 yield * db
608635 . delete ( JobTable )
609636 . where ( or ( eq ( JobTable . session_id , sessionID ) , eq ( JobTable . child_session_id , sessionID ) ) )
0 commit comments