@@ -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// Anchors, the submitted message, follow state and pending scrolls belong to
344350// one thread, so the body remounts when the thread changes.
@@ -528,6 +534,50 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
528534 const unsettleThread = useAtomCommand ( threadEnvironment . unsettle , { reportFailure : false } ) ;
529535 const updateMetadata = useAtomCommand ( threadEnvironment . updateMetadata , { reportFailure : false } ) ;
530536 const visitThread = useAtomCommand ( threadEnvironment . visit , { reportFailure : false } ) ;
537+ const anyRunStop = useAtomValue (
538+ props . selectedThreadDetailRef === null
539+ ? NO_RUN_STOP_ATOM
540+ : environmentThreadDetails . runStopAtom ( props . selectedThreadDetailRef ) ,
541+ ) ;
542+ // A background Stop matters only while that work is still pending.
543+ const runStop =
544+ anyRunStop ?. background && props . selectedThread . pendingBackgroundTasks . length === 0
545+ ? null
546+ : anyRunStop ;
547+ const forceStopThread = useAtomCommand ( orchestrationEnvironment . forceStopThread , {
548+ reportFailure : false ,
549+ } ) ;
550+ const canForceStop =
551+ useAtomValue ( orchestrationEnvironment . forceStopThread . permissionAtom ( props . environmentId ) ) &&
552+ props . serverConfig ?. environment . capabilities . threadForceStop === true ;
553+ const [ forceStoppingKey , setForceStoppingKey ] = useState < string | null > ( null ) ;
554+ // The run that ignored Stop, so a confirmation left open cannot hit a newer run.
555+ const forceStop = async ( runId : RunId | undefined ) => {
556+ const threadKey = selectedThreadKey ;
557+ setForceStoppingKey ( threadKey ) ;
558+ const result = await forceStopThread ( {
559+ environmentId : props . environmentId ,
560+ input :
561+ runId === undefined
562+ ? { threadId : props . selectedThread . id }
563+ : { threadId : props . selectedThread . id , runId } ,
564+ } ) ;
565+ setForceStoppingKey ( ( current ) => ( current === threadKey ? null : current ) ) ;
566+ if ( result . _tag === "Failure" ) {
567+ if ( ! isAtomCommandInterrupted ( result ) ) Dialog . alert ( "Could not force stop the agent" ) ;
568+ return ;
569+ }
570+ const others = result . value . sharedWithThreadIds . length ;
571+ if ( result . value . outcome === "releasing" ) {
572+ Dialog . alert ( "Turn stopped; the agent is still shutting down" ) ;
573+ }
574+ if ( result . value . outcome === "shared" ) {
575+ Dialog . alert (
576+ "Turn stopped; the agent process kept running" ,
577+ `It is shared with ${ others === 0 ? "other" : others } working ${ others === 1 ? "thread" : "threads" } . Stop those to free it.` ,
578+ ) ;
579+ }
580+ } ;
531581 const [ statusRequest , setStatusRequest ] = useState < { threadKey : string } | null > ( null ) ;
532582 const statusPending = statusRequest ?. threadKey === selectedThreadKey ;
533583 const [ lifecycleNow , setLifecycleNow ] = useState ( ( ) => new Date ( ) . toISOString ( ) ) ;
@@ -619,6 +669,32 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
619669 if ( connectionStatus !== null && ! props . draftMode ) {
620670 return connectionStatus ;
621671 }
672+ // Before the request check: an agent stuck on an approval may also ignore Stop.
673+ if ( runStop !== null && contentPresentationKind === "ready" ) {
674+ const unresponsive = runStop . state === "unresponsive" ;
675+ return {
676+ kind : "stop" ,
677+ unresponsive,
678+ label : unresponsive ? "Not responding to Stop" : "Stopping…" ,
679+ action :
680+ unresponsive && canForceStop
681+ ? {
682+ label : forceStoppingKey === selectedThreadKey ? "Force stopping..." : "Force stop" ,
683+ accessibilityLabel : "Force stop the agent" ,
684+ disabled : forceStoppingKey === selectedThreadKey ,
685+ onPress : ( ) =>
686+ Dialog . confirm ( {
687+ title : "Force stop the agent?" ,
688+ message :
689+ "It has not answered Stop. Force stop ends the turn and kills the agent's processes, unless other working threads share them." ,
690+ confirmText : "Force stop" ,
691+ destructive : true ,
692+ onConfirm : ( ) => void forceStop ( runStop . runId ) ,
693+ } ) ,
694+ }
695+ : null ,
696+ } ;
697+ }
622698 if ( props . activePendingApproval !== null || props . activePendingUserInput !== null ) {
623699 return null ;
624700 }
0 commit comments