Skip to content

Commit e2bb9e6

Browse files
t3dotggclaude
andauthored
fix(server): subagent questions reach a busy parent agent (pingdotgg#29909)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5d3d78a commit e2bb9e6

4 files changed

Lines changed: 132 additions & 25 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ export const layerExecutor: Layer.Layer<
278278
const message = projection.messages.find((item) => item.id === messageId);
279279
const run = projection.runs.find((item) => item.id === message?.runId);
280280
if (message === undefined || run === undefined) return yield* error;
281+
// Notifications must be queued messages.
282+
const queued =
283+
message.delegatedCompletion !== undefined ||
284+
message.notification !== undefined;
281285
// Reuse message ids and receipts so retries cannot duplicate messages or follow-ups.
282286
yield* threads
283287
.dispatch({
@@ -292,10 +296,7 @@ export const layerExecutor: Layer.Layer<
292296
? {}
293297
: { modelSelection: run.modelSelection }),
294298
dispatchMode: {
295-
type:
296-
message.delegatedCompletion === undefined
297-
? "start_immediately"
298-
: "queue_after_active",
299+
type: queued ? "queue_after_active" : "start_immediately",
299300
},
300301
createdBy: message.createdBy,
301302
creationSource: message.creationSource,

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4685,6 +4685,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
46854685
readonly senderThreadId?: OrchestrationV2ConversationMessage["senderThreadId"];
46864686
readonly idleCompaction?: OrchestrationV2ConversationMessage["idleCompaction"];
46874687
readonly delegatedCompletion?: OrchestrationV2ConversationMessage["delegatedCompletion"];
4688+
readonly notification?: OrchestrationV2ConversationMessage["notification"];
46884689
readonly forceRestart: boolean;
46894690
}) {
46904691
const targetRun = input.projection.runs.find((candidate) => candidate.id === input.targetRunId);
@@ -4838,6 +4839,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48384839
...(input.delegatedCompletion === undefined
48394840
? {}
48404841
: { delegatedCompletion: input.delegatedCompletion }),
4842+
...(input.notification === undefined ? {} : { notification: input.notification }),
48414843
...(input.scheduledTaskId === undefined
48424844
? {}
48434845
: { scheduledTaskId: input.scheduledTaskId }),
@@ -4939,6 +4941,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49394941
input.projection.thread.providerInstanceId !== input.modelSelection.instanceId;
49404942
if (
49414943
input.delegatedCompletion === undefined &&
4944+
input.notification === undefined &&
49424945
(instanceChanged ||
49434946
!modelSelectionsEqual(input.projection.thread.modelSelection, input.modelSelection))
49444947
) {
@@ -5729,13 +5732,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
57295732
taskIds: delivery.taskIds,
57305733
};
57315734
}
5732-
// Route durable mailbox deliveries under the thread lock, using the live
5733-
// session's capabilities. Never interrupt/restart a turn for a notification.
5735+
// Route durable mailbox deliveries and delegated task questions under the
5736+
// thread lock, using the live session's capabilities. A question queued
5737+
// behind a long parent turn is never seen. Never interrupt/restart a turn
5738+
// for a notification.
57345739
if (
5735-
delegatedCompletion !== undefined &&
5736-
delegatedCompletion.taskIds.every(
5737-
(id) => projection.subagents.find((task) => task.id === id)?.completionWake === "always",
5738-
)
5740+
delegatedCompletion !== undefined
5741+
? delegatedCompletion.taskIds.every(
5742+
(id) =>
5743+
projection.subagents.find((task) => task.id === id)?.completionWake === "always",
5744+
)
5745+
: command.notification?.source.kind === "delegated_task"
57395746
) {
57405747
const active = projection.runs.find((run) => run.status === "running");
57415748
const providerThread = projection.providerThreads.find(
@@ -5759,7 +5766,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
57595766
if (
57605767
Option.isSome(session) &&
57615768
session.value.providerSession.capabilities.turns.supportsActiveSteering &&
5762-
session.value.providerSession.capabilities.turns.activeSteeringInterruptsTools !== true
5769+
session.value.providerSession.capabilities.turns.activeSteeringInterruptsTools !== true &&
5770+
!(yield* stopReachedRun(command, command.threadId, active.id))
57635771
) {
57645772
dispatchMode = { type: "steer_active", targetRunId: active.id };
57655773
}
@@ -5826,11 +5834,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
58265834
effects,
58275835
projection,
58285836
modelSelection:
5829-
delegatedCompletion === undefined
5837+
delegatedCompletion === undefined && command.notification === undefined
58305838
? modelSelection
58315839
: (projection.runs.find((run) => run.id === dispatchMode.targetRunId)?.modelSelection ??
58325840
modelSelection),
58335841
delegatedCompletion,
5842+
...(command.notification === undefined ? {} : { notification: command.notification }),
58345843
targetRunId: dispatchMode.targetRunId,
58355844
messageId: command.messageId,
58365845
text: dispatchText,

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ it.effect.each(
4444
[true, "with interrupting native steering"],
4545
[true, "settled only"],
4646
[false, "during continuation"],
47+
["question", "during delivery"],
4748
] as const
48-
).map(([mailbox, timing]) => ({
49-
mailbox,
49+
).map(([kind, timing]) => ({
50+
mailbox: kind === true,
51+
question: kind === "question",
5052
timing,
51-
label: mailbox ? "mailbox notification" : "steering",
53+
label:
54+
kind === "question" ? "delegated task question" : kind ? "mailbox notification" : "steering",
5255
})),
53-
)("delivers $label when completion wins $timing", ({ mailbox, timing }) =>
56+
)("delivers $label when completion wins $timing", ({ mailbox, question, timing }) =>
5457
Effect.scoped(
5558
Effect.gen(function* () {
5659
const cwd = yield* checkpointWorkspace(`steering-completion-${timing.replaceAll(" ", "-")}`);
@@ -255,6 +258,21 @@ it.effect.each(
255258
],
256259
});
257260
}
261+
if (question) {
262+
const { thread } = yield* orchestrator.getThreadProjection(threadId);
263+
yield* (yield* EventSink.EventSinkV2).write({
264+
events: [
265+
{
266+
id: EventId.make("event:composer-selection"),
267+
type: "thread.model-selection-updated",
268+
threadId,
269+
providerInstanceId: instanceId,
270+
occurredAt: yield* DateTime.now,
271+
payload: { ...thread, modelSelection: composerSelection },
272+
},
273+
],
274+
});
275+
}
258276
const dispatchSteer = orchestrator.dispatch({
259277
type: "message.dispatch",
260278
commandId: CommandId.make("steer"),
@@ -270,11 +288,21 @@ it.effect.each(
270288
sizeBytes: 10,
271289
},
272290
],
273-
dispatchMode: mailbox
274-
? { type: "queue_after_active" }
275-
: { type: "steer_active", targetRunId: first.runId },
276-
createdBy: mailbox ? "agent" : "user",
277-
creationSource: mailbox ? "server" : "web",
291+
dispatchMode:
292+
mailbox || question
293+
? { type: "queue_after_active" }
294+
: { type: "steer_active", targetRunId: first.runId },
295+
createdBy: mailbox || question ? "agent" : "user",
296+
creationSource: mailbox || question ? "server" : "web",
297+
...(question
298+
? {
299+
notification: {
300+
source: { kind: "delegated_task", taskIds: [taskId] },
301+
outcome: "updated",
302+
summary: "Delegated task has a question",
303+
},
304+
}
305+
: {}),
278306
...(mailbox
279307
? {
280308
delegatedCompletion: {
@@ -340,6 +368,8 @@ it.effect.each(
340368
if (delivery !== null) {
341369
yield* Deferred.succeed(rejectSteer, undefined);
342370
yield* Fiber.join(delivery);
371+
}
372+
if (delivery !== null && !question) {
343373
const requeued = yield* orchestrator.getThreadProjection(threadId);
344374
const item = requeued.turnItems.find(
345375
(item) => item.type === "user_message" && item.messageId === messageId,
@@ -399,7 +429,11 @@ it.effect.each(
399429
]);
400430
assert.equal(steerCalls, timing === "during delivery" ? 1 : 0);
401431
const final = yield* orchestrator.getThreadProjection(threadId);
402-
if (!mailbox) {
432+
if (question) {
433+
assert.deepEqual(final.thread.modelSelection, composerSelection);
434+
assert.deepEqual(started[1]?.modelSelection, composerSelection);
435+
}
436+
if (!mailbox && !question) {
403437
assert.equal(
404438
final.turnItems.find(
405439
(item) => item.type === "user_message" && item.messageId === messageId,
@@ -414,7 +448,7 @@ it.effect.each(
414448
);
415449
assert.equal(
416450
final.turnItems.filter((item) =>
417-
mailbox
451+
mailbox || question
418452
? item.type === "notification"
419453
: item.type === "user_message" && item.messageId === messageId,
420454
).length,
@@ -617,6 +651,65 @@ const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function*
617651
return { cwd, events, started, steered, interrupted, layer, startFirstTurn };
618652
});
619653

654+
it.effect("steers a delegated task question notice into the running parent turn", () =>
655+
Effect.scoped(
656+
Effect.gen(function* () {
657+
const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
658+
"delegated-question-steer",
659+
);
660+
yield* Effect.gen(function* () {
661+
const orchestrator = yield* Orchestrator.OrchestratorV2;
662+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
663+
const threadId = yield* startFirstTurn;
664+
const { thread } = yield* orchestrator.getThreadProjection(threadId);
665+
yield* (yield* EventSink.EventSinkV2).write({
666+
events: [
667+
{
668+
id: EventId.make("event:composer-selection"),
669+
type: "thread.model-selection-updated",
670+
threadId,
671+
providerInstanceId: instanceId,
672+
occurredAt: yield* DateTime.now,
673+
payload: { ...thread, modelSelection: composerSelection },
674+
},
675+
],
676+
});
677+
const messageId = MessageId.make("message:delegated-task-question:child-question");
678+
const notification = {
679+
source: {
680+
kind: "delegated_task" as const,
681+
taskIds: [NodeId.make("task:question")],
682+
childThreadId: ThreadId.make("thread:question-child"),
683+
},
684+
outcome: "updated" as const,
685+
summary: "Delegated task has a question",
686+
};
687+
yield* orchestrator.dispatch({
688+
type: "message.dispatch",
689+
commandId: CommandId.make("question-wake"),
690+
threadId,
691+
messageId,
692+
text: "Delegated task asked a question",
693+
notification,
694+
attachments: [],
695+
dispatchMode: { type: "queue_after_active" },
696+
createdBy: "agent",
697+
creationSource: "server",
698+
});
699+
yield* worker.drain();
700+
const after = yield* orchestrator.getThreadProjection(threadId);
701+
assert.deepEqual(steered, ["Delegated task asked a question"]);
702+
assert.lengthOf(started, 1);
703+
assert.isFalse(after.runs.some((run) => run.status === "queued"));
704+
const message = after.messages.find((candidate) => candidate.id === messageId);
705+
assert.equal(message?.runId, started[0]?.runId);
706+
assert.deepEqual(message?.notification, notification);
707+
assert.deepEqual(after.thread.modelSelection, composerSelection);
708+
}).pipe(Effect.provide(layer));
709+
}),
710+
),
711+
);
712+
620713
it.effect("sends a selected held message once and preserves the remaining queue order", () =>
621714
Effect.scoped(
622715
Effect.gen(function* () {

‎docs/orchestration-v2/orchestrator-mcp-server.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,12 @@ Users rarely watch child threads, so a child question would otherwise go
515515
unanswered. When a child opens a pending `user_input` request, the parent gets
516516
a notification message telling it to answer with the pending request tools or
517517
ask the user. The message says whether the child is blocked or, for a Codex
518-
async question, still working. If the question is answered or dismissed before
519-
the notification starts a turn, that queued turn is cancelled. Approvals are not
518+
async question, still working. If the parent's turn is running and its provider
519+
can steer without interrupting tools, the notification steers into that turn,
520+
like a background task's completion. A queued notice would wait behind a
521+
long-running orchestrator turn that may never end. Otherwise it queues. If the
522+
question is answered or dismissed before a queued notification starts a turn,
523+
that queued turn is cancelled. Approvals are not
520524
routed, because those tools cannot answer them.
521525
A parent still blocked in a `delegate_task` wait gets the blocking question
522526
through that wait instead.

0 commit comments

Comments
 (0)