Skip to content

Commit 60527b5

Browse files
committed
fix(server): a finished delegated task keeps its child's question
Only a cancelled or interrupted task dismisses its child's pending async questions. A completed or failed task leaves them pending so the parent can answer one, which resumes the child.
1 parent 3b9b63b commit 60527b5

2 files changed

Lines changed: 77 additions & 54 deletions

File tree

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11284,10 +11284,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1128411284
),
1128511285
);
1128611286

11287-
// Async questions do not stop a delegated child's run. Once its task result is
11288-
// final, nothing reads the answers, so the questions close instead of
11289-
// showing on the parent forever. The parent lock is held throughout, so a
11290-
// follow-up run cannot start between the check and the dismissal.
11287+
// Async questions do not stop a delegated child's run. A cancelled or
11288+
// interrupted task's questions close, since nobody wants that work anymore.
11289+
// A finished task keeps them: answering one resumes the child. The parent
11290+
// lock is held throughout, so a follow-up run cannot start between the
11291+
// check and the dismissal.
1129111292
const finishAppOwnedSubagent = (
1129211293
parentThreadId: ThreadId,
1129311294
childThreadId: ThreadId,
@@ -11300,10 +11301,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1130011301
const settledContinuationOf = options?.settledContinuationOf;
1130111302
if (yield* delegatedTaskResultPending(childThreadId, undefined, settledContinuationOf))
1130211303
return;
11303-
const { runtimeRequests } = yield* projectionStore.getThreadRecords(childThreadId, [
11304-
"runtimeRequests",
11305-
]);
11306-
for (const request of runtimeRequests) {
11304+
const child = yield* projectionStore.getThreadRecords(
11305+
childThreadId,
11306+
["runtimeRequests", "runs", "messages", "subagents", "providerThreads"],
11307+
{ messageRoles: ["user"] },
11308+
);
11309+
const { resultRun } = delegatedTaskProgress(child);
11310+
const status =
11311+
resultRun === undefined ? null : delegatedTaskTerminalStatus(runOutcome(resultRun));
11312+
if (status !== "cancelled" && status !== "interrupted") return;
11313+
for (const request of child.runtimeRequests) {
1130711314
if (
1130811315
request.status !== "pending" ||
1130911316
request.kind !== "user_input" ||

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

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -540,54 +540,70 @@ it.effect.each(["thread.stop", "run.interrupt"] as const)(
540540
}).pipe(Effect.provide(layerTest.pipe(Layer.provideMerge(TestClock.layer())))),
541541
);
542542

543-
it.effect("a delegated task's async question closes once Stop ends the task", () =>
544-
Effect.gen(function* () {
545-
const orchestrator = yield* Orchestrator.OrchestratorV2;
546-
const parentThreadId = ThreadId.make("thread:stop-question-parent");
547-
yield* createWatchingThread(parentThreadId, 20);
548-
yield* send(parentThreadId, "first", "start_immediately");
549-
const childThreadId = yield* delegate(parentThreadId, "question task");
550-
const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!;
551-
// A Codex async question, which does not end the run that asked it.
552-
const requestId = RuntimeRequestId.make("request:stop-question");
553-
const nodeId = NodeId.make("node:stop-question");
554-
const now = yield* DateTime.now;
555-
yield* (yield* ProjectionStore.ProjectionStoreV2).apply({
556-
id: EventId.make("event:stop-question:request"),
557-
type: "runtime-request.updated",
558-
threadId: childThreadId,
559-
runId: childRun.id,
560-
nodeId,
561-
occurredAt: now,
562-
payload: {
563-
id: requestId,
543+
it.effect.each([
544+
{ ending: "cancelled", question: "resolved" },
545+
{ ending: "completed", question: "pending" },
546+
] as const)(
547+
"a $ending delegated task leaves its async question $question",
548+
({ ending, question }) =>
549+
Effect.gen(function* () {
550+
const orchestrator = yield* Orchestrator.OrchestratorV2;
551+
const parentThreadId = ThreadId.make(`thread:stop-question-parent:${ending}`);
552+
yield* createWatchingThread(parentThreadId, 20);
553+
yield* send(parentThreadId, "first", "start_immediately");
554+
const childThreadId = yield* delegate(parentThreadId, `question task ${ending}`);
555+
const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!;
556+
// A Codex async question, which does not end the run that asked it.
557+
const requestId = RuntimeRequestId.make("request:stop-question");
558+
const nodeId = NodeId.make("node:stop-question");
559+
const now = yield* DateTime.now;
560+
yield* (yield* ProjectionStore.ProjectionStoreV2).apply({
561+
id: EventId.make("event:stop-question:request"),
562+
type: "runtime-request.updated",
563+
threadId: childThreadId,
564+
runId: childRun.id,
564565
nodeId,
565-
providerTurnId: null,
566-
nativeRequestRef: null,
567-
kind: "user_input",
568-
status: "pending",
569-
responseCapability: { type: "message" },
570-
createdAt: now,
571-
resolvedAt: null,
572-
},
573-
});
574-
const requestStatus = Effect.map(
575-
orchestrator.getThreadProjection(childThreadId),
576-
(projection) => projection.runtimeRequests[0]?.status,
577-
);
566+
occurredAt: now,
567+
payload: {
568+
id: requestId,
569+
nodeId,
570+
providerTurnId: null,
571+
nativeRequestRef: null,
572+
kind: "user_input",
573+
status: "pending",
574+
responseCapability: { type: "message" },
575+
createdAt: now,
576+
resolvedAt: null,
577+
},
578+
});
579+
const requestStatus = Effect.map(
580+
orchestrator.getThreadProjection(childThreadId),
581+
(projection) => projection.runtimeRequests[0]?.status,
582+
);
578583

579-
// While the task runs, its question stays open.
580-
yield* orchestrator.recoverDelegatedTask(childThreadId, childRun.id);
581-
assert.equal(yield* requestStatus, "pending");
582-
// What task_cancel dispatches.
583-
yield* orchestrator.dispatch({
584-
type: "thread.stop",
585-
commandId: CommandId.make("stop-question-child"),
586-
threadId: childThreadId,
587-
});
588-
yield* orchestrator.recoverDelegatedTask(childThreadId, childRun.id);
589-
assert.equal(yield* requestStatus, "resolved");
590-
}).pipe(Effect.provide(layerTest)),
584+
// While the task runs, its question stays open.
585+
yield* orchestrator.recoverDelegatedTask(childThreadId, childRun.id);
586+
assert.equal(yield* requestStatus, "pending");
587+
if (ending === "cancelled") {
588+
// What task_cancel dispatches.
589+
yield* orchestrator.dispatch({
590+
type: "thread.stop",
591+
commandId: CommandId.make("stop-question-child"),
592+
threadId: childThreadId,
593+
});
594+
} else {
595+
yield* (yield* ProjectionStore.ProjectionStoreV2).apply({
596+
id: EventId.make("event:stop-question:completed"),
597+
type: "run.updated",
598+
threadId: childThreadId,
599+
runId: childRun.id,
600+
occurredAt: now,
601+
payload: { ...childRun, status: "completed", startedAt: now, completedAt: now },
602+
});
603+
}
604+
yield* orchestrator.recoverDelegatedTask(childThreadId, childRun.id);
605+
assert.equal(yield* requestStatus, question);
606+
}).pipe(Effect.provide(layerTest)),
591607
);
592608

593609
it.effect("a delegated task that cannot be stopped fails the walk after its siblings stop", () =>

0 commit comments

Comments
 (0)