@@ -46,6 +46,7 @@ import type {
4646 ScopedThreadRef ,
4747 ServerConfig as T3ServerConfig ,
4848 UsageLimitsReport ,
49+ RunId ,
4950} from "@t3tools/contracts" ;
5051import {
5152 appendCodexArtifactTemplateUsePrompt ,
@@ -156,8 +157,10 @@ import {
156157import { appAtomRegistry } from "../../state/atom-registry" ;
157158import { threadStreamHaptics } from "./threadStreamHaptics" ;
158159import { useAtomCommand } from "../../state/use-atom-command" ;
160+ import { orchestrationEnvironment } from "../../state/orchestration" ;
159161import { useDelayedStatus } from "../../lib/useDelayedStatus" ;
160162import type { PendingUserInputDraftAnswer } from "@t3tools/client-runtime/state/pending-user-input" ;
163+ import type { RunStopPresentation } from "@t3tools/client-runtime/state/thread-execution" ;
161164import type {
162165 PendingApproval ,
163166 PendingUserInput ,
@@ -339,6 +342,9 @@ const threadVisitTracker = createThreadVisitTracker({
339342
340343// Past the composer's 180 ms height animation.
341344const COMPOSER_HEIGHT_SETTLE_MS = 220 ;
345+ const NO_RUN_STOP_ATOM = Atom . make < RunStopPresentation | null > ( null ) . pipe (
346+ Atom . withLabel ( "mobile-thread-run-stop:empty" ) ,
347+ ) ;
342348
343349// Connection failures survive retries and app suspension.
344350const RECONNECT_ERROR_DELAY_MS = 5_000 ;
@@ -539,6 +545,50 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
539545 const unsettleThread = useAtomCommand ( threadEnvironment . unsettle , { reportFailure : false } ) ;
540546 const updateMetadata = useAtomCommand ( threadEnvironment . updateMetadata , { reportFailure : false } ) ;
541547 const visitThread = useAtomCommand ( threadEnvironment . visit , { reportFailure : false } ) ;
548+ const anyRunStop = useAtomValue (
549+ props . selectedThreadDetailRef === null
550+ ? NO_RUN_STOP_ATOM
551+ : environmentThreadDetails . runStopAtom ( props . selectedThreadDetailRef ) ,
552+ ) ;
553+ // A background Stop matters only while that work is still pending.
554+ const runStop =
555+ anyRunStop ?. background && props . selectedThread . pendingBackgroundTasks . length === 0
556+ ? null
557+ : anyRunStop ;
558+ const forceStopThread = useAtomCommand ( orchestrationEnvironment . forceStopThread , {
559+ reportFailure : false ,
560+ } ) ;
561+ const canForceStop =
562+ useAtomValue ( orchestrationEnvironment . forceStopThread . permissionAtom ( props . environmentId ) ) &&
563+ props . serverConfig ?. environment . capabilities . threadForceStop === true ;
564+ const [ forceStoppingKey , setForceStoppingKey ] = useState < string | null > ( null ) ;
565+ // The run that ignored Stop, so a confirmation left open cannot hit a newer run.
566+ const forceStop = async ( runId : RunId | undefined ) => {
567+ const threadKey = selectedThreadKey ;
568+ setForceStoppingKey ( threadKey ) ;
569+ const result = await forceStopThread ( {
570+ environmentId : props . environmentId ,
571+ input :
572+ runId === undefined
573+ ? { threadId : props . selectedThread . id }
574+ : { threadId : props . selectedThread . id , runId } ,
575+ } ) ;
576+ setForceStoppingKey ( ( current ) => ( current === threadKey ? null : current ) ) ;
577+ if ( result . _tag === "Failure" ) {
578+ if ( ! isAtomCommandInterrupted ( result ) ) Dialog . alert ( "Could not force stop the agent" ) ;
579+ return ;
580+ }
581+ const others = result . value . sharedWithThreadIds . length ;
582+ if ( result . value . outcome === "releasing" ) {
583+ Dialog . alert ( "Turn stopped; the agent is still shutting down" ) ;
584+ }
585+ if ( result . value . outcome === "shared" ) {
586+ Dialog . alert (
587+ "Turn stopped; the agent process kept running" ,
588+ `It is shared with ${ others === 0 ? "other" : others } working ${ others === 1 ? "thread" : "threads" } . Stop those to free it.` ,
589+ ) ;
590+ }
591+ } ;
542592 const [ statusRequest , setStatusRequest ] = useState < { threadKey : string } | null > ( null ) ;
543593 const statusPending = statusRequest ?. threadKey === selectedThreadKey ;
544594 const [ lifecycleNow , setLifecycleNow ] = useState ( ( ) => new Date ( ) . toISOString ( ) ) ;
@@ -630,6 +680,32 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
630680 if ( connectionStatus !== null && ! props . draftMode ) {
631681 return connectionStatus ;
632682 }
683+ // Before the request check: an agent stuck on an approval may also ignore Stop.
684+ if ( runStop !== null && contentPresentationKind === "ready" ) {
685+ const unresponsive = runStop . state === "unresponsive" ;
686+ return {
687+ kind : "stop" ,
688+ unresponsive,
689+ label : unresponsive ? "Not responding to Stop" : "Stopping…" ,
690+ action :
691+ unresponsive && canForceStop
692+ ? {
693+ label : forceStoppingKey === selectedThreadKey ? "Force stopping..." : "Force stop" ,
694+ accessibilityLabel : "Force stop the agent" ,
695+ disabled : forceStoppingKey === selectedThreadKey ,
696+ onPress : ( ) =>
697+ Dialog . confirm ( {
698+ title : "Force stop the agent?" ,
699+ message :
700+ "It has not answered Stop. Force stop ends the turn and kills the agent's processes, unless other working threads share them." ,
701+ confirmText : "Force stop" ,
702+ destructive : true ,
703+ onConfirm : ( ) => void forceStop ( runStop . runId ) ,
704+ } ) ,
705+ }
706+ : null ,
707+ } ;
708+ }
633709 if ( props . activePendingApproval !== null || props . activePendingUserInput !== null ) {
634710 return null ;
635711 }
0 commit comments