Skip to content

Commit 29a6a1a

Browse files
fix(client-runtime): a stopped task's promoted turn stops the tasks it delegated
When stopping a queued follow-up task races with its turn starting, the client interrupted that turn by id but left the tasks it had delegated running. It now stops the child thread of each live app-owned task that run delegated, which also stops their own delegated work, and leaves another task's turn and delegated work alone. AI-assisted; verified with the tests and tools listed above.
1 parent 3c05c1c commit 29a6a1a

2 files changed

Lines changed: 75 additions & 16 deletions

File tree

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ describe("V2 environment commands", () => {
548548
const queuedTaskId = NodeId.make("queued-follow-up-task");
549549
const laterTaskId = NodeId.make("later-follow-up-task");
550550
const queuedRunId = RunId.make("queued-follow-up-run");
551+
const grandchildThreadId = ThreadId.make("grandchild-thread");
551552
const task = (id: NodeId, status: "running" | "pending") => ({
552553
id,
553554
threadId: v2ThreadId,
@@ -603,6 +604,24 @@ describe("V2 environment commands", () => {
603604
childRun(queuedRunId, 3, queued, queuedTaskId),
604605
childRun(RunId.make("later-follow-up-run"), 4, "queued", laterTaskId),
605606
],
607+
// Once promoted, the queued task's turn delegated a task of its own, beside the other's.
608+
subagents:
609+
queued === "queued"
610+
? []
611+
: [
612+
{
613+
...task(NodeId.make("other-grandchild-task"), "running"),
614+
threadId: childThreadId,
615+
runId: RunId.make("running-follow-up-run"),
616+
childThreadId: ThreadId.make("other-grandchild-thread"),
617+
},
618+
{
619+
...task(NodeId.make("grandchild-task"), "running"),
620+
threadId: childThreadId,
621+
runId: queuedRunId,
622+
childThreadId: grandchildThreadId,
623+
},
624+
],
606625
});
607626
const childProjections = new Map([[childThreadId, childProjection("running", "queued")]]);
608627
const commands: OrchestrationV2Command[] = [];
@@ -638,8 +657,9 @@ describe("V2 environment commands", () => {
638657
return {
639658
childThreadId,
640659
queuedRunId,
660+
grandchildThreadId,
641661
commands: commands.filter(
642-
(command) => "threadId" in command && command.threadId === childThreadId,
662+
(command) => "threadId" in command && command.threadId !== v2ThreadId,
643663
),
644664
};
645665
});
@@ -674,6 +694,11 @@ describe("V2 environment commands", () => {
674694
threadId: stopped.childThreadId,
675695
runId: stopped.queuedRunId,
676696
},
697+
{
698+
type: "thread.stop",
699+
commandId: expect.any(String),
700+
threadId: stopped.grandchildThreadId,
701+
},
677702
]);
678703
}).pipe(Effect.provide(layerTestRandom)),
679704
);
@@ -688,6 +713,11 @@ describe("V2 environment commands", () => {
688713
threadId: stopped.childThreadId,
689714
runId: stopped.queuedRunId,
690715
},
716+
{
717+
type: "thread.stop",
718+
commandId: expect.any(String),
719+
threadId: stopped.grandchildThreadId,
720+
},
691721
]);
692722
}).pipe(Effect.provide(layerTestRandom)),
693723
);

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ import {
2424
type RuntimeMode,
2525
type RuntimeRequestId,
2626
type ThreadId,
27+
type OrchestrationV2Run,
2728
type ThreadEnvMode,
2829
type UploadChatAttachment,
2930
type GuestMessageId,
3031
delegatedTaskYieldsLiveRun,
3132
isOrchestrationV2RunActive,
33+
isOrchestrationV2WorkActive,
3234
} from "@t3tools/contracts";
3335
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
3436
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
@@ -816,19 +818,46 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea
816818
);
817819
const settled = { sequence: 0 };
818820
// By its id: a thread-wide stop could reach, or hold, another task's turn. A turn that
819-
// ended before the interrupt landed needs nothing more.
820-
const interruptOwnRun = (runId: RunId) =>
821-
Effect.flatMap(allocateCommandId({}), (commandId) =>
822-
dispatch({ type: "run.interrupt", commandId, threadId: childThreadId, runId }),
823-
).pipe(
824-
Effect.catch((error) =>
825-
Effect.flatMap(readRun(runId), (run) =>
826-
run !== undefined && isOrchestrationV2RunActive(run.status)
827-
? Effect.fail(error)
828-
: Effect.succeed(settled),
821+
// ended before the interrupt landed needs nothing more, but the tasks it delegated
822+
// still go, and only those.
823+
const stopOwnRun = Effect.fn("EnvironmentCommands.stopOwnRun")(function* (
824+
run: OrchestrationV2Run,
825+
) {
826+
let result = settled;
827+
if (isOrchestrationV2RunActive(run.status)) {
828+
result = yield* dispatch({
829+
type: "run.interrupt",
830+
commandId: yield* allocateCommandId({}),
831+
threadId: childThreadId,
832+
runId: run.id,
833+
}).pipe(
834+
Effect.catch((error) =>
835+
Effect.flatMap(readRun(run.id), (latest) =>
836+
latest !== undefined && isOrchestrationV2RunActive(latest.status)
837+
? Effect.fail(error)
838+
: Effect.succeed(settled),
839+
),
829840
),
830-
),
831-
);
841+
);
842+
}
843+
// A child thread's stop also stops the tasks it delegated, all the way down.
844+
const { subagents } = yield* getProjection(childThreadId);
845+
for (const task of subagents) {
846+
if (
847+
task.runId !== run.id ||
848+
task.origin !== "app_owned" ||
849+
task.childThreadId === null ||
850+
!isOrchestrationV2WorkActive(task.status)
851+
)
852+
continue;
853+
result = yield* dispatch({
854+
type: "thread.stop",
855+
commandId: yield* allocateCommandId({}),
856+
threadId: task.childThreadId,
857+
});
858+
}
859+
return result;
860+
});
832861
let result = settled;
833862
for (const run of child.runs) {
834863
if (run.status !== "queued" || run.delegatedTaskId !== subagent.id) continue;
@@ -843,9 +872,9 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea
843872
Effect.flatMap(readRun(run.id), (latest) =>
844873
latest?.status === "queued"
845874
? Effect.fail(error)
846-
: latest !== undefined && isOrchestrationV2RunActive(latest.status)
847-
? interruptOwnRun(run.id)
848-
: Effect.succeed(settled),
875+
: latest === undefined
876+
? Effect.succeed(settled)
877+
: stopOwnRun(latest),
849878
),
850879
),
851880
);

0 commit comments

Comments
 (0)