Skip to content

Commit 9f71d86

Browse files
committed
feat(mobile): share Stop controls for agents and workflows
1 parent 08bea6c commit 9f71d86

5 files changed

Lines changed: 249 additions & 84 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts";
3+
import { useState } from "react";
4+
import { Alert, Pressable } from "react-native";
5+
6+
import { AppActivityIndicator } from "../../components/AppActivityIndicator";
7+
import { AppText as Text } from "../../components/AppText";
8+
import { SymbolView } from "../../components/AppSymbol";
9+
import { orchestrationEnvironment } from "../../state/orchestration";
10+
import { threadEnvironment } from "../../state/threads";
11+
import { useAtomCommand } from "../../state/use-atom-command";
12+
import {
13+
resolveSubagentRowPresentation,
14+
resolveSubagentStopTarget,
15+
} from "./threadAgentsPresentation";
16+
17+
export function SubagentStopButton(props: {
18+
readonly environmentId: EnvironmentId;
19+
readonly subagent: OrchestrationV2Subagent;
20+
}) {
21+
const target = resolveSubagentStopTarget(props.subagent);
22+
const canStopWorkflow = useAtomValue(
23+
orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
24+
);
25+
const canInterrupt = useAtomValue(
26+
orchestrationEnvironment.v2.dispatchCommand.permissionAtom(props.environmentId),
27+
);
28+
const stopWorkflow = useAtomCommand(orchestrationEnvironment.stopWorkflow);
29+
const interruptTurn = useAtomCommand(threadEnvironment.interruptTurn);
30+
const [stopping, setStopping] = useState(false);
31+
32+
if (target === null || !(target.kind === "workflow" ? canStopWorkflow : canInterrupt))
33+
return null;
34+
35+
const workflow = target.kind === "workflow";
36+
const label =
37+
props.subagent.workflow?.name ?? resolveSubagentRowPresentation(props.subagent).title;
38+
const confirmStop = () =>
39+
Alert.alert(
40+
workflow ? "Stop workflow?" : "Stop agent?",
41+
workflow ? `Every running agent in ${label} stops.` : `Stop ${label}?`,
42+
[
43+
{ text: "Cancel", style: "cancel" },
44+
{
45+
text: "Stop",
46+
style: "destructive",
47+
onPress: async () => {
48+
setStopping(true);
49+
try {
50+
const result =
51+
target.kind === "workflow"
52+
? await stopWorkflow({ environmentId: props.environmentId, input: target.input })
53+
: await interruptTurn({
54+
environmentId: props.environmentId,
55+
input: target.input,
56+
});
57+
if (result._tag === "Failure") {
58+
Alert.alert(workflow ? "Could not stop workflow" : "Could not stop agent");
59+
}
60+
} finally {
61+
setStopping(false);
62+
}
63+
},
64+
},
65+
],
66+
);
67+
68+
return (
69+
<Pressable
70+
accessibilityRole="button"
71+
accessibilityLabel={`Stop ${workflow ? "workflow" : "agent"} ${label}`}
72+
accessibilityState={{ disabled: stopping, busy: stopping }}
73+
disabled={stopping}
74+
onPress={confirmStop}
75+
className="min-h-11 min-w-11 flex-row items-center justify-center gap-1.5 rounded-full bg-subtle px-3 active:opacity-70 disabled:opacity-50"
76+
>
77+
{stopping ? (
78+
<AppActivityIndicator size="small" />
79+
) : (
80+
<SymbolView name="stop.fill" size={10} tintColorClassName="accent-danger-foreground" />
81+
)}
82+
<Text className="font-t3-medium text-xs text-danger-foreground">Stop</Text>
83+
</Pressable>
84+
);
85+
}

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { useUniwindTheme } from "../../lib/useUniwindTheme";
2525
import { environmentThreadDetails } from "../../state/threads";
2626
import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
2727
import { SubagentRow } from "./SubagentRow";
28+
import { SubagentStopButton } from "./SubagentStopButton";
2829
import { ThreadWorkflowGroup } from "./ThreadWorkflowGroup";
2930
import { MaterialScreenContent } from "../../components/MaterialScreenContent";
3031
import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
@@ -106,7 +107,6 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
106107
<ThreadWorkflowGroup
107108
key={subagent.id}
108109
environmentId={target.environmentId}
109-
coordinator={subagent}
110110
group={group}
111111
header={row}
112112
onOpen={openChildThread}
@@ -179,26 +179,28 @@ function AgentRow(props: {
179179
</View>
180180
);
181181

182-
if (childThreadId === null) {
183-
return (
184-
<View
185-
accessible
186-
accessibilityHint="Provider-managed agent. Its work appears in the transcript."
187-
>
188-
{row}
189-
</View>
190-
);
191-
}
192-
193182
return (
194-
<Pressable
195-
accessibilityRole="link"
196-
accessibilityHint="Opens this agent's thread"
197-
onPress={() => props.onOpen(childThreadId)}
198-
className="active:opacity-70"
199-
>
200-
{row}
201-
</Pressable>
183+
<View className="flex-row items-center gap-3">
184+
{childThreadId === null ? (
185+
<View
186+
accessible
187+
accessibilityHint="Provider-managed agent. Its work appears in the transcript."
188+
className="min-w-0 flex-1"
189+
>
190+
{row}
191+
</View>
192+
) : (
193+
<Pressable
194+
accessibilityRole="link"
195+
accessibilityHint="Opens this agent thread"
196+
onPress={() => props.onOpen(childThreadId)}
197+
className="min-w-0 flex-1 active:opacity-70"
198+
>
199+
{row}
200+
</Pressable>
201+
)}
202+
<SubagentStopButton environmentId={props.environmentId} subagent={subagent} />
203+
</View>
202204
);
203205
}
204206

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

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useAtomValue } from "@effect/atom-react";
21
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
32
import {
43
isActiveSubagentStatus,
@@ -7,70 +6,33 @@ import {
76
type RuntimeSubagent,
87
type SubagentWorkflowGroup,
98
} from "@t3tools/client-runtime/state/subagentRuntime";
10-
import {
11-
NodeId,
12-
ThreadId,
13-
type EnvironmentId,
14-
type OrchestrationV2Subagent,
15-
} from "@t3tools/contracts";
9+
import { ThreadId, type EnvironmentId } from "@t3tools/contracts";
1610
import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchestrationTiming";
1711
import { useState, type ReactNode } from "react";
18-
import { Alert, Pressable, View } from "react-native";
12+
import { Pressable, View } from "react-native";
1913

20-
import { AppActivityIndicator } from "../../components/AppActivityIndicator";
2114
import { AppText as Text } from "../../components/AppText";
2215
import { SymbolView } from "../../components/AppSymbol";
2316
import { cn } from "../../lib/cn";
2417
import { useThreadShell } from "../../state/entities";
25-
import { orchestrationEnvironment } from "../../state/orchestration";
26-
import { useAtomCommand } from "../../state/use-atom-command";
2718
import { subagentCardDetail } from "./subagent-card-presentation";
2819
import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot";
2920
import { resolveSubagentRowPresentation, workflowGroupSummary } from "./threadAgentsPresentation";
3021
import { useVisibleSecondClock } from "./use-visible-second-clock";
3122

3223
export function ThreadWorkflowGroup(props: {
3324
readonly environmentId: EnvironmentId;
34-
readonly coordinator: OrchestrationV2Subagent;
3525
readonly group: SubagentWorkflowGroup;
3626
readonly header: ReactNode;
3727
readonly onOpen: (threadId: ThreadId) => void;
3828
}) {
39-
const { coordinator, group } = props;
29+
const { group } = props;
4030
const [expanded, setExpanded] = useState(() => isActiveSubagentStatus(group.workflow.status));
4131
const [phaseOpen, setPhaseOpen] = useState<ReadonlyMap<number, boolean>>(() => new Map());
4232
const label = group.workflow.workflowName ?? group.workflow.title;
4333
const summary = workflowGroupSummary(group);
4434
const phases = group.phases.filter((phase) => phase.members.length > 0);
4535

46-
const canOperate = useAtomValue(
47-
orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
48-
);
49-
const stopWorkflow = useAtomCommand(orchestrationEnvironment.stopWorkflow);
50-
const [stopping, setStopping] = useState(false);
51-
const canStop =
52-
canOperate &&
53-
group.workflow.status === "running" &&
54-
coordinator.origin === "provider_native" &&
55-
coordinator.driver === "claudeAgent";
56-
const confirmStop = () =>
57-
Alert.alert("Stop workflow?", `Every running agent in ${label} stops.`, [
58-
{ text: "Cancel", style: "cancel" },
59-
{
60-
text: "Stop",
61-
style: "destructive",
62-
onPress: async () => {
63-
setStopping(true);
64-
const result = await stopWorkflow({
65-
environmentId: props.environmentId,
66-
input: { threadId: coordinator.threadId, subagentId: NodeId.make(coordinator.id) },
67-
});
68-
setStopping(false);
69-
if (result._tag === "Failure") Alert.alert("Could not stop workflow");
70-
},
71-
},
72-
]);
73-
7436
return (
7537
<View className="border-b border-border pb-2">
7638
{props.header}
@@ -93,28 +55,6 @@ export function ThreadWorkflowGroup(props: {
9355
{summary}
9456
</Text>
9557
</Pressable>
96-
{canStop ? (
97-
<Pressable
98-
accessibilityRole="button"
99-
accessibilityLabel={`Stop workflow ${label}`}
100-
accessibilityState={{ disabled: stopping, busy: stopping }}
101-
disabled={stopping}
102-
hitSlop={8}
103-
onPress={confirmStop}
104-
className="min-h-9 flex-row items-center gap-1.5 rounded-full bg-subtle px-3 active:opacity-70 disabled:opacity-50"
105-
>
106-
{stopping ? (
107-
<AppActivityIndicator size="small" />
108-
) : (
109-
<SymbolView
110-
name="stop.fill"
111-
size={10}
112-
tintColorClassName="accent-danger-foreground"
113-
/>
114-
)}
115-
<Text className="font-t3-medium text-xs text-danger-foreground">Stop</Text>
116-
</Pressable>
117-
) : null}
11858
</View>
11959
{expanded ? (
12060
<View className="ml-[3px] border-l border-border pl-3.5">

‎apps/mobile/src/features/threads/threadAgentsPresentation.test.ts‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { describe, expect, it } from "vite-plus/test";
2+
import { NodeId, ProviderDriverKind, RunId, ThreadId } from "@t3tools/contracts";
23

3-
import { resolveSubagentRowPresentation, workflowGroupSummary } from "./threadAgentsPresentation";
4+
import {
5+
resolveSubagentRowPresentation,
6+
resolveSubagentStopTarget,
7+
workflowGroupSummary,
8+
} from "./threadAgentsPresentation";
49

510
const base = {
611
title: null,
@@ -93,3 +98,92 @@ describe("workflowGroupSummary", () => {
9398
);
9499
});
95100
});
101+
102+
const stopAgent = {
103+
id: NodeId.make("agent"),
104+
threadId: ThreadId.make("parent"),
105+
childThreadId: ThreadId.make("child"),
106+
status: "running" as const,
107+
origin: "app_owned" as const,
108+
driver: ProviderDriverKind.make("codex"),
109+
runId: RunId.make("run"),
110+
nativeTaskRef: null,
111+
};
112+
113+
describe("resolveSubagentStopTarget", () => {
114+
it.each(["pending", "running", "waiting"] as const)(
115+
"interrupts only the child of an app-owned %s agent",
116+
(status) => {
117+
expect(resolveSubagentStopTarget({ ...stopAgent, status })).toEqual({
118+
kind: "subagent",
119+
input: { threadId: stopAgent.childThreadId },
120+
});
121+
expect(resolveSubagentStopTarget({ ...stopAgent, status, childThreadId: null })).toBeNull();
122+
},
123+
);
124+
125+
it.each(["idle", "completed", "failed", "cancelled", "interrupted"] as const)(
126+
"offers no Stop for a %s agent",
127+
(status) => {
128+
expect(resolveSubagentStopTarget({ ...stopAgent, status })).toBeNull();
129+
},
130+
);
131+
132+
const native = {
133+
...stopAgent,
134+
origin: "provider_native" as const,
135+
driver: ProviderDriverKind.make("claudeAgent"),
136+
childThreadId: null,
137+
nativeTaskRef: {
138+
driver: ProviderDriverKind.make("claudeAgent"),
139+
strength: "strong" as const,
140+
nativeId: "native-task",
141+
},
142+
};
143+
144+
it("targets a supported native task without interrupting its parent run", () => {
145+
expect(resolveSubagentStopTarget(native)).toEqual({
146+
kind: "subagent",
147+
input: { threadId: native.threadId, subagentId: native.id },
148+
});
149+
});
150+
151+
it("hides native Stop when the task cannot be identified or stopped", () => {
152+
expect(resolveSubagentStopTarget({ ...native, nativeTaskRef: null })).toBeNull();
153+
expect(
154+
resolveSubagentStopTarget({
155+
...native,
156+
nativeTaskRef: { ...native.nativeTaskRef, strength: "weak" },
157+
}),
158+
).toBeNull();
159+
expect(
160+
resolveSubagentStopTarget({
161+
...native,
162+
nativeTaskRef: { ...native.nativeTaskRef, nativeId: null },
163+
}),
164+
).toBeNull();
165+
expect(resolveSubagentStopTarget({ ...native, runId: null })).toBeNull();
166+
expect(resolveSubagentStopTarget({ ...native, status: "completed" })).toBeNull();
167+
expect(
168+
resolveSubagentStopTarget({ ...native, driver: ProviderDriverKind.make("codex") }),
169+
).toBeNull();
170+
});
171+
172+
it("stops the whole workflow through its own command", () => {
173+
const workflow = { name: "Review", phases: [], agents: [] };
174+
expect(resolveSubagentStopTarget({ ...native, workflow })).toEqual({
175+
kind: "workflow",
176+
input: { threadId: native.threadId, subagentId: native.id },
177+
});
178+
expect(resolveSubagentStopTarget({ ...native, workflow, status: "waiting" })).toBeNull();
179+
expect(resolveSubagentStopTarget({ ...stopAgent, workflow })).toBeNull();
180+
expect(resolveSubagentStopTarget({ ...native, workflow, nativeTaskRef: null })).toBeNull();
181+
expect(
182+
resolveSubagentStopTarget({
183+
...native,
184+
workflow,
185+
nativeTaskRef: { ...native.nativeTaskRef, nativeId: "" },
186+
}),
187+
).toBeNull();
188+
});
189+
});

0 commit comments

Comments
 (0)