Skip to content

Commit b0e0c65

Browse files
fix(server): Stop always completes, and an agent that ignores it can be force stopped
A provider starved of memory never answered Stop: Claude's interrupt request had no timeout, so the turn stayed running for hours and the user had no way out. Every provider interrupt now goes through one 15 s bound in the session manager. If the provider has not confirmed by then, the Stop is marked unresponsive and the thread offers Force stop, which kills the thread's own agent scope with cgroup.kill (or tears down its session elsewhere) and ends the turn. A process shared with other working threads is never killed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dd14848 commit b0e0c65

36 files changed

Lines changed: 1123 additions & 85 deletions

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ import {
156156
import { appAtomRegistry } from "../../state/atom-registry";
157157
import { threadStreamHaptics } from "./threadStreamHaptics";
158158
import { useAtomCommand } from "../../state/use-atom-command";
159+
import { orchestrationEnvironment } from "../../state/orchestration";
159160
import { useDelayedStatus } from "../../lib/useDelayedStatus";
160161
import type { PendingUserInputDraftAnswer } from "@t3tools/client-runtime/state/pending-user-input";
162+
import type { RunStopPresentation } from "@t3tools/client-runtime/state/thread-execution";
161163
import type {
162164
PendingApproval,
163165
PendingUserInput,
@@ -339,6 +341,9 @@ const threadVisitTracker = createThreadVisitTracker({
339341

340342
// Past the composer's 180 ms height animation.
341343
const COMPOSER_HEIGHT_SETTLE_MS = 220;
344+
const NO_RUN_STOP_ATOM = Atom.make<RunStopPresentation | null>(null).pipe(
345+
Atom.withLabel("mobile-thread-run-stop:empty"),
346+
);
342347

343348
// Anchors, the submitted message, follow state and pending scrolls belong to
344349
// one thread, so the body remounts when the thread changes.
@@ -528,6 +533,43 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
528533
const unsettleThread = useAtomCommand(threadEnvironment.unsettle, { reportFailure: false });
529534
const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata, { reportFailure: false });
530535
const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
536+
const anyRunStop = useAtomValue(
537+
props.selectedThreadDetailRef === null
538+
? NO_RUN_STOP_ATOM
539+
: environmentThreadDetails.runStopAtom(props.selectedThreadDetailRef),
540+
);
541+
// A background Stop matters only while that work is still pending.
542+
const runStop =
543+
anyRunStop?.background && props.selectedThread.pendingBackgroundTasks.length === 0
544+
? null
545+
: anyRunStop;
546+
const forceStopThread = useAtomCommand(orchestrationEnvironment.forceStopThread, {
547+
reportFailure: false,
548+
});
549+
const canForceStop =
550+
useAtomValue(orchestrationEnvironment.forceStopThread.permissionAtom(props.environmentId)) &&
551+
props.serverConfig?.environment.capabilities.threadForceStop === true;
552+
const [forceStoppingKey, setForceStoppingKey] = useState<string | null>(null);
553+
const forceStop = async () => {
554+
const threadKey = selectedThreadKey;
555+
setForceStoppingKey(threadKey);
556+
const result = await forceStopThread({
557+
environmentId: props.environmentId,
558+
input: { threadId: props.selectedThread.id },
559+
});
560+
setForceStoppingKey((current) => (current === threadKey ? null : current));
561+
if (result._tag === "Failure") {
562+
if (!isAtomCommandInterrupted(result)) Dialog.alert("Could not force stop the agent");
563+
return;
564+
}
565+
const others = result.value.sharedWithThreadIds.length;
566+
if (result.value.outcome === "shared") {
567+
Dialog.alert(
568+
"Turn stopped; the agent process kept running",
569+
`It is shared with ${others} other working ${others === 1 ? "thread" : "threads"}. Stop those to free it.`,
570+
);
571+
}
572+
};
531573
const [statusRequest, setStatusRequest] = useState<{ threadKey: string } | null>(null);
532574
const statusPending = statusRequest?.threadKey === selectedThreadKey;
533575
const [lifecycleNow, setLifecycleNow] = useState(() => new Date().toISOString());
@@ -647,6 +689,31 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
647689
},
648690
};
649691
}
692+
if (runStop !== null && contentPresentationKind === "ready") {
693+
const unresponsive = runStop.state === "unresponsive";
694+
return {
695+
kind: "stop",
696+
unresponsive,
697+
label: unresponsive ? "Not responding to Stop" : "Stopping…",
698+
action:
699+
unresponsive && canForceStop
700+
? {
701+
label: forceStoppingKey === selectedThreadKey ? "Force stopping..." : "Force stop",
702+
accessibilityLabel: "Force stop the agent",
703+
disabled: forceStoppingKey === selectedThreadKey,
704+
onPress: () =>
705+
Dialog.confirm({
706+
title: "Force stop the agent?",
707+
message:
708+
"It has not answered Stop. Force stop kills its processes and ends the turn.",
709+
confirmText: "Force stop",
710+
destructive: true,
711+
onConfirm: () => void forceStop(),
712+
}),
713+
}
714+
: null,
715+
};
716+
}
650717
if (props.isCompacting && contentPresentationKind === "ready") {
651718
return { kind: "compacting" };
652719
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ export function FloatingWorkingControl(props: {
8383
const [queueWidth, setQueueWidth] = useState(0);
8484
const [actionWidth, setActionWidth] = useState(0);
8585
const action =
86-
props.status?.kind === "background" || props.status?.kind === "lifecycle"
86+
props.status?.kind === "background" ||
87+
props.status?.kind === "lifecycle" ||
88+
props.status?.kind === "stop"
8789
? props.status.action
8890
: null;
8991
const [agentsWidth, setAgentsWidth] = useState(0);
@@ -471,6 +473,30 @@ function FloatingStatusLabel(props: {
471473
</StatusLabelRow>
472474
);
473475
}
476+
if (props.status.kind === "stop") {
477+
return (
478+
<StatusLabelRow
479+
key="stop"
480+
accessibilityLabel={props.status.label}
481+
className="gap-2"
482+
onLayout={props.onLayout}
483+
>
484+
{props.status.unresponsive ? (
485+
<SymbolView
486+
name="exclamationmark.triangle"
487+
size={13}
488+
tintColorClassName="accent-warning-foreground"
489+
type="monochrome"
490+
/>
491+
) : (
492+
<AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
493+
)}
494+
<Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
495+
{props.status.label}
496+
</Text>
497+
</StatusLabelRow>
498+
);
499+
}
474500
if (props.status.kind === "goal") {
475501
return (
476502
<StatusLabelRow

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export type FloatingWorkingStatus =
3131
readonly label: string;
3232
readonly action: FloatingStatusAction;
3333
}
34+
| {
35+
readonly kind: "stop";
36+
readonly label: string;
37+
readonly unresponsive: boolean;
38+
readonly action: FloatingStatusAction | null;
39+
}
3440
| { readonly kind: "goal"; readonly label: string; readonly accessibilityLabel: string }
3541
| { readonly kind: "preparing"; readonly label: string }
3642
| {

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const RPC_REQUIRED_SCOPES = {
4242
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: AuthOrchestrationOperateScope,
4343
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: AuthOrchestrationReadScope,
4444
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
45+
[ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope,
4546
[ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: AuthOrchestrationReadScope,
4647
[ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: AuthOrchestrationReadScope,
4748
[ORCHESTRATION_V2_WS_METHODS.searchThreads]: AuthOrchestrationReadScope,

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export const make = Effect.gen(function* () {
240240
projectSettingsOverrides: true,
241241
projectProviderInstanceOverrides: true,
242242
threadSnooze: true,
243+
threadForceStop: true,
243244
environmentThemes: true,
244245
usageLimitSources: true,
245246
providerWorkspaceSnapshotsOnDemand: true,

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const RPC_AGGREGATES = {
1414
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: "orchestrationV2",
1515
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: "orchestration",
1616
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: "orchestration",
17+
[ORCHESTRATION_V2_WS_METHODS.forceStopThread]: "orchestration",
1718
[ORCHESTRATION_V2_WS_METHODS.getTurnItem]: "orchestration",
1819
[ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: "orchestration",
1920
[ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: "orchestration",

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ const WORKFLOW_MEMBER_TRANSCRIPT_FLUSH_MS = 500;
322322
// A steer waits at most this long, from its request, for its foreground
323323
// subagents to move to the background before it is sent.
324324
const BACKGROUND_MOVE_TIMEOUT_MS = 5_000;
325+
// How long a Stop waits for the CLI to exit before the user is offered Force stop.
326+
const CLAUDE_STOP_CONFIRM_TIMEOUT_MS = 10_000;
325327

326328
const CLAUDE_GROUP_WATCH_SCRIPT = [
327329
"{ trap '' TERM; read -r _ <&3; kill -TERM 0; sleep 5; kill -KILL 0; } </dev/null >/dev/null 2>&1 &",
@@ -10433,44 +10435,36 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1043310435
yield* existing.query.interrupt;
1043410436
yield* existing.query.close.pipe(Effect.ignore);
1043510437
const closed = yield* Deferred.await(existing.closed).pipe(
10436-
Effect.timeoutOption("10 seconds"),
10438+
Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS),
1043710439
);
1043810440
if (Option.isSome(closed)) {
1043910441
return;
1044010442
}
10441-
10442-
const completedAt = yield* DateTime.now;
10443+
// The turn stays open: the CLI may still be working, and the user decides
10444+
// whether to Force stop it.
1044310445
yield* Effect.logWarning("orchestration-v2.claude-query-interrupt-timeout", {
1044410446
providerSessionId: input.providerSessionId,
1044510447
providerThreadId: turnInput.providerThread.id,
1044610448
providerTurnId: turnInput.providerTurnId,
1044710449
});
10448-
const ownedLiveQuery = yield* Ref.modify(queryContext, (current) =>
10449-
current?.query === existing.query ? [true, null] : [false, current],
10450-
);
10451-
if (ownedLiveQuery) {
10452-
existing.stopping = true;
10453-
yield* finalizeActiveTurnAfterQueryExit(existing);
10454-
yield* endSubagentCallsOfClosedQuery(existing.nativeThreadId, "interrupted");
10455-
} else {
10456-
yield* finalizeActiveTurn({
10457-
context: currentTurn,
10458-
status: "interrupted",
10459-
completedAt,
10460-
});
10461-
}
10462-
yield* Deferred.succeed(existing.closed, undefined);
10450+
return yield* new ProviderAdapter.ProviderAdapterInterruptTimeoutError({
10451+
driver: CLAUDE_PROVIDER,
10452+
providerThreadId: turnInput.providerThread.id,
10453+
providerTurnId: turnInput.providerTurnId,
10454+
timeoutMs: CLAUDE_STOP_CONFIRM_TIMEOUT_MS,
10455+
});
1046310456
},
1046410457
(effect, turnInput) =>
1046510458
effect.pipe(
10466-
Effect.mapError(
10467-
(cause) =>
10468-
new ProviderAdapter.ProviderAdapterInterruptError({
10469-
driver: CLAUDE_PROVIDER,
10470-
providerThreadId: turnInput.providerThread.id,
10471-
providerTurnId: turnInput.providerTurnId,
10472-
cause,
10473-
}),
10459+
Effect.mapError((cause) =>
10460+
cause._tag === "ProviderAdapterInterruptTimeoutError"
10461+
? cause
10462+
: new ProviderAdapter.ProviderAdapterInterruptError({
10463+
driver: CLAUDE_PROVIDER,
10464+
providerThreadId: turnInput.providerThread.id,
10465+
providerTurnId: turnInput.providerTurnId,
10466+
cause,
10467+
}),
1047410468
),
1047510469
),
1047610470
);

‎apps/server/src/orchestration-v2/EffectWorker.test.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function layerExecutorFor(input: {
111111
closeInstance: () => Effect.void,
112112
release: () => record("release"),
113113
detach: () => record("detach"),
114+
otherWorkingThreads: () => Effect.succeed([]),
114115
}),
115116
),
116117
Layer.succeed(
@@ -167,7 +168,18 @@ function layerExecutorFor(input: {
167168
);
168169
}
169170

170-
it.effect("settles a stopped run when its adapter has already lost the native turn", () =>
171+
it.effect.each([
172+
{
173+
name: "settles a stopped run when its adapter has already lost the native turn",
174+
unresponsive: false,
175+
dispatched: ["thread.background-work.settle"],
176+
},
177+
{
178+
name: "leaves the run running and flags the Stop when the provider never confirms it",
179+
unresponsive: true,
180+
dispatched: ["thread.interrupt.unresponsive"],
181+
},
182+
])("$name", ({ unresponsive, dispatched }) =>
171183
Effect.gen(function* () {
172184
const now = yield* DateTime.now;
173185
const events = yield* Ref.make<ReadonlyArray<string>>([]);
@@ -178,7 +190,8 @@ it.effect("settles a stopped run when its adapter has already lost the native tu
178190
threadId,
179191
operation: "interrupt",
180192
providerTurnId,
181-
cause: "Provider turn is not active.",
193+
...(unresponsive ? { unresponsive } : {}),
194+
cause: unresponsive ? "Timed out." : "Provider turn is not active.",
182195
}),
183196
threads: {
184197
dispatch: (command) =>
@@ -199,7 +212,7 @@ it.effect("settles a stopped run when its adapter has already lost the native tu
199212
},
200213
});
201214
}).pipe(Effect.provide(layer));
202-
assert.deepEqual(yield* Ref.get(events), ["thread.background-work.settle"]);
215+
assert.deepEqual(yield* Ref.get(events), dispatched);
203216
}),
204217
);
205218

‎apps/server/src/orchestration-v2/EffectWorker.ts‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,34 +177,49 @@ export const layerExecutor: Layer.Layer<
177177
cause,
178178
}),
179179
),
180+
Effect.map((outcome) => ({ unresponsive: false, outcome }) as const),
181+
// Settling here would end the run while the agent may still be running; the
182+
// user decides with Force stop instead.
183+
Effect.catchIf(
184+
(cause) => request.subagent === undefined && cause.unresponsive === true,
185+
() => Effect.succeed({ unresponsive: true, outcome: undefined } as const),
186+
),
180187
Effect.catchIf(
181188
(cause) =>
182189
request.subagent === undefined &&
183190
isNonRetryableProviderTurnControlFailure(
184191
request.type,
185192
Cause.pretty(Cause.fail(cause)),
186193
),
187-
() => Effect.void,
194+
() => Effect.succeed({ unresponsive: false, outcome: undefined } as const),
188195
),
189-
Effect.andThen((outcome) =>
190-
request.subagent !== undefined && outcome === undefined
191-
? Effect.void
192-
: threads.dispatch({
193-
type: "thread.background-work.settle",
194-
commandId: CommandId.make(`${effect.id}:background-work-settled`),
195-
...(request.subagent === undefined || outcome === undefined
196-
? {}
197-
: {
198-
subagent: {
199-
id: request.subagent.id,
200-
stoppedAt: DateTime.makeUnsafe(effect.createdAt),
201-
outcome,
202-
},
203-
}),
196+
Effect.andThen(({ unresponsive, outcome }) =>
197+
unresponsive
198+
? threads.dispatch({
199+
type: "thread.interrupt.unresponsive",
200+
commandId: CommandId.make(`${effect.id}:unresponsive`),
204201
threadId: effect.threadId,
205202
providerThreadId: request.providerThreadId,
206203
providerTurnId: request.providerTurnId,
207-
}),
204+
})
205+
: request.subagent !== undefined && outcome === undefined
206+
? Effect.void
207+
: threads.dispatch({
208+
type: "thread.background-work.settle",
209+
commandId: CommandId.make(`${effect.id}:background-work-settled`),
210+
...(request.subagent === undefined || outcome === undefined
211+
? {}
212+
: {
213+
subagent: {
214+
id: request.subagent.id,
215+
stoppedAt: DateTime.makeUnsafe(effect.createdAt),
216+
outcome,
217+
},
218+
}),
219+
threadId: effect.threadId,
220+
providerThreadId: request.providerThreadId,
221+
providerTurnId: request.providerTurnId,
222+
}),
208223
),
209224
Effect.mapError(
210225
(cause) =>

0 commit comments

Comments
 (0)