Skip to content

Commit 00b1700

Browse files
committed
fix(client-runtime): derive Stop targets from the parent projection
1 parent 812e7b6 commit 00b1700

4 files changed

Lines changed: 96 additions & 44 deletions

File tree

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -684,15 +684,7 @@ function ThreadRouteContent(
684684
environmentId: selectedThread.environmentId,
685685
input: {
686686
threadId: selectedThread.id,
687-
...(composer.interruptibleRunId === null
688-
? {
689-
backgroundChildThreadIds: selectedThread.pendingBackgroundTasks.flatMap((task) =>
690-
task.kind === "subagent" && task.childThreadId !== undefined
691-
? [task.childThreadId]
692-
: [],
693-
),
694-
}
695-
: { runId: composer.interruptibleRunId }),
687+
...(composer.interruptibleRunId === null ? {} : { runId: composer.interruptibleRunId }),
696688
},
697689
});
698690
}, [composer.interruptibleRunId, interruptThreadTurn, selectedThread]);

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6848,12 +6848,7 @@ export default function ChatView(props: ChatViewProps) {
68486848
setStoppingBackgroundWorkKey(requestKey);
68496849
const result = await interruptThreadTurn({
68506850
environmentId,
6851-
input: {
6852-
threadId: activeThread.id,
6853-
backgroundChildThreadIds: activeBackgroundTasks.flatMap((task) =>
6854-
task.kind === "subagent" && task.childThreadId !== undefined ? [task.childThreadId] : [],
6855-
),
6856-
},
6851+
input: { threadId: activeThread.id },
68576852
});
68586853
// Acceptance does not confirm termination. Allow retry while the provider
68596854
// finishes stopping the tasks or reports a failure.
@@ -6867,7 +6862,7 @@ export default function ChatView(props: ChatViewProps) {
68676862
);
68686863
}
68696864
}
6870-
}, [activeThread, activeBackgroundTasks, environmentId, interruptThreadTurn, setThreadError]);
6865+
}, [activeThread, environmentId, interruptThreadTurn, setThreadError]);
68716866
const onOpenRelatedThread = useCallback(
68726867
(threadId: ThreadId) => {
68736868
void navigate({

‎packages/client-runtime/src/operations/commands.test.ts‎

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,15 @@ describe("V2 environment commands", () => {
580580
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
581581
);
582582

583-
it.effect.each(["running", "queued", "completed"] as const)(
584-
"stops a %s inferred delegated follow-up when the parent has no projected background work",
585-
(status) =>
583+
it.effect.each([
584+
{ status: "running", parentStatus: "completed" },
585+
{ status: "queued", parentStatus: "completed" },
586+
{ status: "completed", parentStatus: "completed" },
587+
{ status: "running", parentStatus: "running" },
588+
{ status: "running", parentStatus: "rolled_back" },
589+
] as const)(
590+
"discovers a $status delegated follow-up from a $parentStatus parent projection",
591+
({ status, parentStatus }) =>
586592
Effect.gen(function* () {
587593
const childThreadId = ThreadId.make("resumed-child");
588594
const childRunId = RunId.make("follow-up");
@@ -596,7 +602,7 @@ describe("V2 environment commands", () => {
596602
userMessageId: MessageId.make("original-message"),
597603
rootNodeId: null,
598604
activeAttemptId: null,
599-
status: "completed" as const,
605+
status: parentStatus,
600606
requestedAt: v2Now,
601607
startedAt: v2Now,
602608
completedAt: v2Now,
@@ -650,12 +656,44 @@ describe("V2 environment commands", () => {
650656
};
651657
const nativeId = ThreadId.make("native-child");
652658
const unrelatedId = ThreadId.make("unrelated-child");
659+
const archivedId = ThreadId.make("archived-child");
660+
const deletedId = ThreadId.make("deleted-child");
661+
const task = parent.subagents[0]!;
653662
const commands: OrchestrationV2Command[] = [];
663+
const projectionRequests: ThreadId[] = [];
654664
const supervisor = yield* makeSupervisor({
655665
commands,
656666
projects: [],
667+
projectionRequests,
657668
projections: new Map([
658-
[v2ThreadId, parent],
669+
[
670+
v2ThreadId,
671+
{
672+
...parent,
673+
subagents: [
674+
task,
675+
{ ...task, id: NodeId.make("duplicate-link") },
676+
...[nativeId, unrelatedId, archivedId, deletedId].map((id) => ({
677+
...task,
678+
id: NodeId.make(`task-${id}`),
679+
childThreadId: id,
680+
})),
681+
{ ...task, id: NodeId.make("unlinked"), childThreadId: null },
682+
{
683+
...task,
684+
id: NodeId.make("native-task"),
685+
childThreadId: ThreadId.make("excluded-native-task-child"),
686+
origin: "provider_native",
687+
},
688+
{
689+
...task,
690+
id: NodeId.make("original-active"),
691+
childThreadId: ThreadId.make("excluded-active-task-child"),
692+
status: "running",
693+
},
694+
],
695+
},
696+
],
659697
[childThreadId, child],
660698
[
661699
nativeId,
@@ -672,19 +710,43 @@ describe("V2 environment commands", () => {
672710
},
673711
},
674712
],
713+
[
714+
archivedId,
715+
{ ...child, thread: { ...child.thread, id: archivedId, archivedAt: v2Now } },
716+
],
717+
[deletedId, { ...child, thread: { ...child.thread, id: deletedId, deletedAt: v2Now } }],
675718
]),
676719
});
677720
const result = yield* interruptThreadTurn({
678721
threadId: v2ThreadId,
679722
commandId: CommandId.make("stop-background"),
680-
backgroundChildThreadIds: [
681-
childThreadId,
682-
childThreadId,
683-
nativeId,
684-
unrelatedId,
685-
v2ThreadId,
686-
],
687723
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
724+
if (parentStatus !== "completed") {
725+
expect(projectionRequests).toEqual([v2ThreadId]);
726+
expect(result.sequence).toBe(parentStatus === "running" ? 1 : 0);
727+
expect(commands).toEqual(
728+
parentStatus === "running"
729+
? [
730+
{
731+
type: "run.interrupt",
732+
commandId: "stop-background",
733+
threadId: v2ThreadId,
734+
runId: run.id,
735+
holdQueue: true,
736+
},
737+
]
738+
: [],
739+
);
740+
return;
741+
}
742+
expect(projectionRequests).toEqual([
743+
v2ThreadId,
744+
childThreadId,
745+
nativeId,
746+
unrelatedId,
747+
archivedId,
748+
deletedId,
749+
]);
688750
expect(result.sequence).toBe(status === "completed" ? 0 : 1);
689751
expect(commands).toEqual(
690752
status === "completed"

‎packages/client-runtime/src/operations/commands.ts‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ export interface StartThreadTurnInput extends ThreadCommandInput {
183183
}
184184

185185
export interface InterruptThreadTurnInput extends ThreadCommandInput {
186-
/** App-owned children named by the settled parent's waiting roster. */
187-
readonly backgroundChildThreadIds?: ReadonlyArray<ThreadId>;
188186
readonly runId?: RunId;
189187
/** Temporary caller compatibility while UI naming moves from turns to runs. */
190188
readonly turnId?: string;
@@ -802,21 +800,26 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea
802800
}
803801
}
804802
let childSequence = 0;
805-
if ((input.backgroundChildThreadIds?.length ?? 0) > 0 && parentProjection === undefined) {
806-
parentProjection = yield* getProjection(input.threadId);
807-
}
808-
for (const childThreadId of new Set(input.backgroundChildThreadIds ?? [])) {
809-
// Active original delegations remain owned by the parent's existing Stop
810-
// path. Only resumed terminal tasks need a separate child interrupt.
811-
if (
812-
!parentProjection?.subagents.some(
813-
(task) =>
814-
task.origin === "app_owned" &&
815-
task.childThreadId === childThreadId &&
816-
["completed", "failed", "interrupted", "cancelled"].includes(task.status),
817-
)
803+
// The original task stays terminal after a follow-up. Its child link is
804+
// still authoritative; read the child's current run when stopping a settled
805+
// parent's background work, without asking callers to supply the roster.
806+
const resumedChildThreadIds =
807+
parentProjection !== undefined &&
808+
!parentProjection.runs.some((run) =>
809+
["preparing", "starting", "running"].includes(run.status),
810+
) &&
811+
["completed", "waiting", "failed", "interrupted", "cancelled"].includes(
812+
parentProjection.runs.at(-1)?.status ?? "",
818813
)
819-
continue;
814+
? parentProjection.subagents.flatMap((task) =>
815+
task.origin === "app_owned" &&
816+
task.childThreadId !== null &&
817+
["completed", "failed", "interrupted", "cancelled"].includes(task.status)
818+
? [task.childThreadId]
819+
: [],
820+
)
821+
: [];
822+
for (const childThreadId of new Set(resumedChildThreadIds)) {
820823
const child = yield* getProjection(childThreadId);
821824
if (
822825
child.thread.lineage.parentThreadId !== input.threadId ||

0 commit comments

Comments
 (0)