Skip to content

Commit d8abb52

Browse files
committed
fix(mobile): stop background work after the turn settles
1 parent ce96e4c commit d8abb52

4 files changed

Lines changed: 115 additions & 5 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ export interface ThreadDetailScreenProps {
166166
readonly onNativePasteText: (paste: ComposerTextPaste) => Promise<void>;
167167
readonly onRemoveDraftImage: (imageId: string) => void;
168168
readonly onStopThread: () => void;
169+
/** True from the stop request until the server clears the thread's background liveness. */
170+
readonly stoppingBackgroundWork: boolean;
171+
readonly onStopBackgroundWork: () => void;
169172
readonly onSendMessage: () => Promise<MessageId | null>;
170173
readonly onReconnectEnvironment: () => void;
171174
readonly onUpdateThreadModelSelection: (modelSelection: ModelSelection) => void;
@@ -408,6 +411,14 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
408411
if (props.activeWorkStartedAt !== null && contentPresentationKind === "ready") {
409412
return { kind: "working", startedAt: props.activeWorkStartedAt };
410413
}
414+
if (props.selectedThread.backgroundLiveness && contentPresentationKind === "ready") {
415+
return {
416+
kind: "background",
417+
liveness: props.selectedThread.backgroundLiveness,
418+
stopping: props.stoppingBackgroundWork,
419+
onStop: props.onStopBackgroundWork,
420+
};
421+
}
411422
return null;
412423
})();
413424
const showWorkingControl = floatingStatus !== null;

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,39 @@ function ThreadRouteContent(
647647
},
648648
});
649649
}, [interruptThreadTurn, selectedThread]);
650+
// Background work (subagent fleets, watch loops) can outlive the turn. The
651+
// server stops it by session, so the interrupt needs no turn id.
652+
const backgroundLiveness = selectedThread?.backgroundLiveness ?? null;
653+
const [backgroundStopRequest, setBackgroundStopRequest] = useState<{
654+
threadKey: string;
655+
} | null>(null);
656+
// "Stopping…" holds until the liveness clears; the interrupt command
657+
// returning only means the request was accepted.
658+
if (
659+
backgroundStopRequest !== null &&
660+
(backgroundStopRequest.threadKey !==
661+
(selectedThread ? scopedThreadKey(selectedThread.environmentId, selectedThread.id) : null) ||
662+
backgroundLiveness === null)
663+
) {
664+
setBackgroundStopRequest(null);
665+
}
666+
const handleStopBackgroundWork = useCallback(() => {
667+
if (!selectedThread) {
668+
return;
669+
}
670+
const threadId = selectedThread.id;
671+
const request = { threadKey: scopedThreadKey(selectedThread.environmentId, threadId) };
672+
setBackgroundStopRequest(request);
673+
void interruptThreadTurn({
674+
environmentId: selectedThread.environmentId,
675+
input: { threadId },
676+
}).then((result) => {
677+
// A late failure must not clear a newer request, even on the same thread.
678+
if (result._tag === "Failure") {
679+
setBackgroundStopRequest((current) => (current === request ? null : current));
680+
}
681+
});
682+
}, [interruptThreadTurn, selectedThread]);
650683

651684
const handleOpenTerminal = useCallback(
652685
(nextTerminalId?: string | null) => {
@@ -1023,6 +1056,8 @@ function ThreadRouteContent(
10231056
onRemoveDraftImage={composer.onRemoveDraftImage}
10241057
serverConfig={serverConfig}
10251058
onStopThread={awaitingBootstrapTurn ? handleCancelWorktreeSetup : handleStopThread}
1059+
stoppingBackgroundWork={backgroundStopRequest !== null}
1060+
onStopBackgroundWork={handleStopBackgroundWork}
10261061
onSendMessage={composer.onSendMessage}
10271062
onReconnectEnvironment={handleReconnectEnvironment}
10281063
onUpdateThreadModelSelection={composer.onUpdateModelSelection}

‎apps/mobile/src/features/threads/floating-working-control.tsx‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ export function FloatingWorkingControl(props: {
126126
return null;
127127
}
128128

129-
// Only the connection label is a button (tap to reconnect); the others
130-
// pass touches through to the feed like before.
131-
const statusInteractive = props.status?.kind === "connection";
129+
// The connection label is a button (tap to reconnect) and the background
130+
// label carries a Stop button; the others pass touches through to the feed.
131+
const statusInteractive =
132+
props.status?.kind === "connection" || props.status?.kind === "background";
132133
const capsuleInteractive = statusInteractive || props.devicePreview !== null;
133134
// The host stays centered on the capsule, but its measurement constraint
134135
// comes from the overlay, independent of the capsule's current width.
@@ -137,7 +138,7 @@ export function FloatingWorkingControl(props: {
137138
<DevicePreviewButton {...props.devicePreview} compact={false} />
138139
) : props.status !== null ? (
139140
<>
140-
<Animated.View className="h-11" style={capsuleSizerStyle} />
141+
<Animated.View pointerEvents="none" className="h-11" style={capsuleSizerStyle} />
141142
<View
142143
pointerEvents={statusInteractive ? "box-none" : "none"}
143144
className="absolute h-11 items-center justify-center"
@@ -147,7 +148,9 @@ export function FloatingWorkingControl(props: {
147148
key={
148149
props.status.kind === "working" || props.status.kind === "compacting"
149150
? props.status.kind
150-
: `${props.status.kind}:${props.status.label}`
151+
: props.status.kind === "background"
152+
? `background:${props.status.liveness}`
153+
: `${props.status.kind}:${props.status.label}`
151154
}
152155
status={props.status}
153156
onLayout={handleLabelLayout}
@@ -295,6 +298,9 @@ function FloatingStatusLabel(props: {
295298
if (props.status.kind === "compacting") {
296299
return <CompactingLabel key="compacting" onLayout={props.onLayout} />;
297300
}
301+
if (props.status.kind === "background") {
302+
return <BackgroundWorkLabel key="background" status={props.status} onLayout={props.onLayout} />;
303+
}
298304
if (props.status.kind === "connection") {
299305
return (
300306
<StatusLabelRow
@@ -385,6 +391,55 @@ function StatusLabelRow(props: {
385391
);
386392
}
387393

394+
// Stop is its own button inside the row rather than the whole pill, so a
395+
// stray tap on the label cannot end background work. Everything but Stop
396+
// passes touches through to the feed, like the non-interactive labels.
397+
function BackgroundWorkLabel(props: {
398+
readonly status: Extract<FloatingWorkingStatus, { kind: "background" }>;
399+
readonly onLayout: (event: LayoutChangeEvent) => void;
400+
}) {
401+
const label = props.status.liveness === "monitoring" ? "Monitoring" : "Working";
402+
return (
403+
<Animated.View
404+
pointerEvents="box-none"
405+
className="absolute max-w-full"
406+
entering={LABEL_ENTERING}
407+
exiting={LABEL_EXITING}
408+
onLayout={props.onLayout}
409+
>
410+
<View pointerEvents="box-none" className="h-11 flex-row items-center pl-4">
411+
<View
412+
pointerEvents="none"
413+
accessible
414+
accessibilityLabel={label}
415+
className="flex-row items-center gap-2"
416+
>
417+
{/* Monitoring is a calm watch state, matching the web banner (no pulse). */}
418+
{props.status.liveness === "working" ? (
419+
<ActivityIndicator size="small" colorClassName="accent-icon-muted" />
420+
) : null}
421+
<Text className="font-t3-medium text-xs text-foreground" numberOfLines={1}>
422+
{label}
423+
</Text>
424+
</View>
425+
<View pointerEvents="none" className="ml-3 h-4 w-px bg-border" />
426+
<Pressable
427+
accessibilityLabel={props.status.stopping ? "Stopping" : "Stop background work"}
428+
accessibilityRole="button"
429+
accessibilityState={{ disabled: props.status.stopping }}
430+
className="h-11 justify-center pl-3 pr-4 active:opacity-70"
431+
disabled={props.status.stopping}
432+
onPress={props.status.onStop}
433+
>
434+
<Text className="font-t3-medium text-xs text-danger-foreground">
435+
{props.status.stopping ? "Stopping…" : "Stop"}
436+
</Text>
437+
</Pressable>
438+
</View>
439+
</Animated.View>
440+
);
441+
}
442+
388443
function WorkingDuration(props: {
389444
readonly startedAt: string;
390445
readonly onLayout: (event: LayoutChangeEvent) => void;

‎apps/mobile/src/features/threads/floating-working-status.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ export type FloatingWorkingStatus =
99
| { readonly kind: "working"; readonly startedAt: string }
1010
| { readonly kind: "syncing"; readonly label: string }
1111
| { readonly kind: "compacting" }
12+
// The turn settled but subagents or watch loops are still live on the
13+
// server (thread.backgroundLiveness). The composer's stop button is gone
14+
// by then, so this variant carries the only stop control.
15+
| {
16+
readonly kind: "background";
17+
readonly liveness: "working" | "monitoring";
18+
readonly stopping: boolean;
19+
readonly onStop: () => void;
20+
}
1221
// A task whose thread the server has not created yet: the worktree may
1322
// still be checking out, so there is no turn to time.
1423
| { readonly kind: "preparing"; readonly label: string }

0 commit comments

Comments
 (0)