Skip to content

Commit 8b06234

Browse files
committed
fix(server): notify parents about delegated child approvals
1 parent 339179f commit 8b06234

5 files changed

Lines changed: 334 additions & 37 deletions

File tree

apps/server/src/orchestration-v2/DelegatedTaskQuestionReactor.ts renamed to apps/server/src/orchestration-v2/DelegatedTaskRequestReactor.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ import * as Orchestrator from "./Orchestrator.ts";
1818
import * as ProjectionStore from "./ProjectionStore.ts";
1919

2020
/**
21-
* Wakes the parent of an app-owned delegated task when the task asks a question. The question
22-
* blocks the task until someone answers it, and the user rarely watches child threads, so the
23-
* parent agent is told to answer it or to ask the user. Only questions are routed: approvals
24-
* cannot be answered through the pending request tools.
21+
* Reports a local delegated child's question or approval request to its parent.
22+
* The parent can answer questions; approvals always need the user.
2523
*/
26-
export class DelegatedTaskQuestionReactor extends Context.Service<
27-
DelegatedTaskQuestionReactor,
24+
export class DelegatedTaskRequestReactor extends Context.Service<
25+
DelegatedTaskRequestReactor,
2826
{
2927
readonly start: () => Effect.Effect<void, never, Scope.Scope>;
3028
readonly handle: (event: OrchestrationV2DomainEvent) => Effect.Effect<void>;
3129
}
32-
>()("t3/orchestration-v2/DelegatedTaskQuestionReactor") {}
30+
>()("t3/orchestration-v2/DelegatedTaskRequestReactor") {}
3331

3432
const make = Effect.gen(function* () {
3533
const engine = yield* Orchestrator.OrchestratorV2;
@@ -42,20 +40,24 @@ const make = Effect.gen(function* () {
4240
if (task.childThreadId === null) return;
4341
const key = `${task.threadId}:${request.id}`;
4442
const label = reportLabel(task.title?.trim() || task.prompt);
45-
const situation =
46-
request.responseCapability.type === "message"
43+
const question = request.kind === "user_input";
44+
const situation = !question
45+
? `Delegated task ${task.id} needs the user's approval.`
46+
: request.responseCapability.type === "message"
4747
? `Delegated task ${task.id} asked a question and is still working.`
4848
: `Delegated task ${task.id} is blocked on a question and cannot continue until it is answered.`;
4949
yield* engine.dispatch({
5050
type: "message.dispatch",
5151
commandId: CommandId.make(`server:delegated-task-question:${key}`),
5252
threadId: task.threadId,
5353
messageId: MessageId.make(`message:delegated-task-question:${key}`),
54-
text: `${situation} Read it with t3_pending_request_read using threadId ${task.childThreadId} and requestId ${request.id}. Answer it with t3_pending_request_respond when the answer follows from the task or the user's instructions; otherwise ask the user, then answer it. If it is no longer pending, it was answered or dismissed.`,
54+
text: !question
55+
? `${situation} Ask the user to open [the child thread](t3-thread://v1/${task.childThreadId}) and handle request ${request.id} there. Only the user can approve; do not answer this request with t3_pending_request_respond. If it is no longer pending, no action is needed.`
56+
: `${situation} Read it with t3_pending_request_read using threadId ${task.childThreadId} and requestId ${request.id}. Answer it with t3_pending_request_respond when the answer follows from the task or the user's instructions; otherwise ask the user, then answer it. If it is no longer pending, it was answered or dismissed.`,
5557
notification: {
5658
source: { kind: "delegated_task", taskIds: [task.id], childThreadId: task.childThreadId },
5759
outcome: "updated",
58-
summary: `Delegated task${label === undefined ? "" : ` "${label}"`} has a question`,
60+
summary: `Delegated task${label === undefined ? "" : ` "${label}"`} ${question ? "has a question" : "needs approval"}`,
5961
},
6062
attachments: [],
6163
dispatchMode: { type: "queue_after_active" },
@@ -64,7 +66,7 @@ const make = Effect.gen(function* () {
6466
});
6567
});
6668

67-
const handle: DelegatedTaskQuestionReactor["Service"]["handle"] = (event) =>
69+
const handle: DelegatedTaskRequestReactor["Service"]["handle"] = (event) =>
6870
Effect.gen(function* () {
6971
if (event.type === "run.updated") {
7072
if (!["completed", "failed", "waiting"].includes(event.payload.status)) return;
@@ -86,15 +88,16 @@ const make = Effect.gen(function* () {
8688
]);
8789
for (const request of child.runtimeRequests) {
8890
if (
89-
request.kind !== "user_input" ||
91+
request.kind === "dynamic_tool_call" ||
92+
request.kind === "auth_refresh" ||
9093
request.status !== "pending" ||
91-
request.responseCapability.type !== "live"
94+
(request.kind === "user_input" && request.responseCapability.type !== "live")
9295
) {
9396
continue;
9497
}
9598
yield* wakeParent(task, request).pipe(
9699
Effect.catchCause((cause) =>
97-
Effect.logWarning("catching up a delegated task question failed", {
100+
Effect.logWarning("catching up a delegated task request failed", {
98101
taskId: task.id,
99102
requestId: request.id,
100103
cause,
@@ -105,7 +108,11 @@ const make = Effect.gen(function* () {
105108
}
106109
return;
107110
}
108-
if (event.type !== "runtime-request.updated" || event.payload.kind !== "user_input") {
111+
if (
112+
event.type !== "runtime-request.updated" ||
113+
event.payload.kind === "dynamic_tool_call" ||
114+
event.payload.kind === "auth_refresh"
115+
) {
109116
return;
110117
}
111118
const child = yield* projections.getThread(event.threadId);
@@ -143,15 +150,19 @@ const make = Effect.gen(function* () {
143150
);
144151
if (
145152
task === undefined ||
153+
task.childThreadId !== child.id ||
154+
task.completionDelivery?.state === "disposed" ||
155+
["completed", "failed", "cancelled", "interrupted"].includes(task.status) ||
146156
parent.thread.archivedAt !== null ||
147157
parent.thread.deletedAt !== null
148158
) {
149159
return;
150160
}
151-
// A blocking delegate_task wait delivers the question itself.
161+
// A blocking wait delivers a live question itself. Approval notices wait
162+
// until the parent ends; approvals do not yet end a blocking wait.
152163
if (
153164
task.completionWake === "settled_only" &&
154-
event.payload.responseCapability.type === "live" &&
165+
(event.payload.kind !== "user_input" || event.payload.responseCapability.type === "live") &&
155166
parent.runs.some(
156167
(run) =>
157168
run.id === task.runId &&
@@ -163,23 +174,23 @@ const make = Effect.gen(function* () {
163174
yield* wakeParent(task, request);
164175
}).pipe(
165176
Effect.catchCause((cause) =>
166-
Effect.logWarning("waking the parent of a delegated task question failed", {
177+
Effect.logWarning("waking the parent of a delegated task request failed", {
167178
threadId: event.threadId,
168179
cause,
169180
}),
170181
),
171182
);
172183

173-
const start: DelegatedTaskQuestionReactor["Service"]["start"] = () =>
184+
const start: DelegatedTaskRequestReactor["Service"]["start"] = () =>
174185
forkParked(
175186
Stream.runForEach(engine.streamDomainEvents, handle).pipe(
176187
Effect.catchCause((cause) =>
177-
Effect.logWarning("delegated task question event stream failed", { cause }),
188+
Effect.logWarning("delegated task request event stream failed", { cause }),
178189
),
179190
),
180191
);
181192

182-
return { start, handle } satisfies DelegatedTaskQuestionReactor["Service"];
193+
return { start, handle } satisfies DelegatedTaskRequestReactor["Service"];
183194
});
184195

185-
export const layer = Layer.effect(DelegatedTaskQuestionReactor, make);
196+
export const layer = Layer.effect(DelegatedTaskRequestReactor, make);

0 commit comments

Comments
 (0)