@@ -23,6 +23,11 @@ import {
2323 RuntimeMode ,
2424} from "@t3tools/contracts" ;
2525import { effectiveSnoozed , threadWokeAt } from "@t3tools/client-runtime/state/thread-settled" ;
26+ import {
27+ BACKGROUND_WORK_STOP_RETRY_AFTER_MS ,
28+ type BackgroundWorkStopRequest ,
29+ isBackgroundWorkStopPending ,
30+ } from "@t3tools/client-runtime/state/thread-execution" ;
2631import { useAcknowledgeThreadWoke , useThreadActions } from "../../hooks/useThreadActions" ;
2732import { settleAndAdvance } from "../../settleAndAdvance" ;
2833import type { ChatShortcutCommands } from "./useChatShortcuts" ;
@@ -386,21 +391,41 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
386391 // The interrupt path also accepts a completed run while its provider still
387392 // has background work.
388393 const activeBackgroundTasks = ! isWorking && activeThread ? pendingBackgroundTasks : [ ] ;
389- const [ stoppingBackgroundWorkKey , setStoppingBackgroundWorkKey ] = useState < string | null > ( null ) ;
390- const isStoppingBackgroundWork =
391- stoppingBackgroundWorkKey === `${ environmentId } :${ activeThreadId } ` ;
394+ const [ backgroundWorkStop , setBackgroundWorkStop ] = useState < BackgroundWorkStopRequest | null > (
395+ null ,
396+ ) ;
397+ const isStoppingBackgroundWork = isBackgroundWorkStopPending (
398+ backgroundWorkStop ,
399+ `${ environmentId } :${ activeThreadId } ` ,
400+ pendingBackgroundTasks ,
401+ ) ;
402+ useEffect ( ( ) => {
403+ if ( backgroundWorkStop ?. accepted !== true ) return ;
404+ const timer = setTimeout ( ( ) => {
405+ setBackgroundWorkStop ( ( current ) => ( current === backgroundWorkStop ? null : current ) ) ;
406+ } , BACKGROUND_WORK_STOP_RETRY_AFTER_MS ) ;
407+ return ( ) => clearTimeout ( timer ) ;
408+ } , [ backgroundWorkStop ] ) ;
392409 const handleStopBackgroundWork = async ( ) => {
393410 if ( ! activeThread || ! readEnvironmentScope ( environmentId , AuthOrchestrationOperateScope ) )
394411 return ;
395- const requestKey = `${ environmentId } :${ activeThread . id } ` ;
396- setStoppingBackgroundWorkKey ( requestKey ) ;
412+ const request : BackgroundWorkStopRequest = {
413+ threadKey : `${ environmentId } :${ activeThread . id } ` ,
414+ taskIds : pendingBackgroundTasks . map ( ( task ) => task . taskId ) ,
415+ accepted : false ,
416+ } ;
417+ setBackgroundWorkStop ( request ) ;
397418 const result = await interruptThreadTurn ( {
398419 environmentId,
399420 input : { threadId : activeThread . id } ,
400421 } ) ;
401- // Acceptance does not confirm termination. Allow retry while the provider
402- // finishes stopping the tasks or reports a failure.
403- setStoppingBackgroundWorkKey ( ( current ) => ( current === requestKey ? null : current ) ) ;
422+ setBackgroundWorkStop ( ( current ) =>
423+ current !== request
424+ ? current
425+ : result . _tag === "Failure"
426+ ? null
427+ : { ...request , accepted : true } ,
428+ ) ;
404429 if ( result . _tag === "Failure" ) {
405430 if ( ! isAtomCommandInterrupted ( result ) ) {
406431 const error = squashAtomCommandFailure ( result ) ;
0 commit comments