Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
type ProviderInstanceId,
type ProviderOptionDescriptor,
type ProviderOptionSelection,
ProviderRequestKind,
type RuntimeMode,
type ScheduledTask,
type ScheduledTaskUpsertInput,
Expand Down Expand Up @@ -416,6 +417,53 @@ function isTerminalTaskStatus(
);
}

function endsTaskWait(result: OrchestratorMcpDelegateTaskResult): boolean {
return (
result.hasPendingQuestion ||
result.pendingApproval !== null ||
isTerminalTaskStatus(result.status)
);
}

const isApprovalRequestKind = Schema.is(ProviderRequestKind);

function pendingTaskApproval(
child: Pick<OrchestrationV2ThreadProjection, "runs" | "nodes" | "runtimeRequests">,
taskId: NodeId,
originalRun: OrchestrationV2Run | undefined,
) {
const taskRunIds = new Set(
child.runs
.filter(
(run) =>
!ThreadManagementService.isTerminalRunStatus(run.status) &&
(run.delegatedTaskId === taskId ||
// Legacy tasks own only their original run, not later turns in the child thread.
(run.delegatedTaskId === undefined && run.id === originalRun?.id)),
)
.map((run) => run.id),
);
let approval: OrchestratorMcpDelegateTaskResult["pendingApproval"] = null;
let oldestCreatedAt = Infinity;
for (const request of child.runtimeRequests) {
if (
request.status !== "pending" ||
request.responseCapability.type !== "live" ||
!isApprovalRequestKind(request.kind)
) {
continue;
}
const runId = child.nodes.find((node) => node.id === request.nodeId)?.runId;
if (runId === undefined || runId === null || !taskRunIds.has(runId)) continue;
const createdAt = DateTime.toEpochMillis(request.createdAt);
if (createdAt < oldestCreatedAt) {
oldestCreatedAt = createdAt;
approval = { requestId: request.id, kind: request.kind, runId };
}
}
return approval;
}

function directAppOwnedChildTasks(
parent: Pick<OrchestrationV2ThreadProjection, "thread" | "subagents">,
target: Pick<OrchestrationV2ThreadProjection, "thread" | "runs">,
Expand Down Expand Up @@ -1379,8 +1427,16 @@ const make = Effect.gen(function* () {
const childControls = yield* threadManagement
.getThreadRecords(
task.childThreadId,
["runs", "messages", "contextTransfers", "subagents", "providerThreads", "runtimeRequests"],
{ messageRoles: ["user"] },
[
"runs",
"messages",
"contextTransfers",
"subagents",
"providerThreads",
"runtimeRequests",
"nodes",
],
{ messageRoles: ["user"], omitSettledLeafNodes: true },
)
.pipe(Effect.mapError(threadManagementFailure));
const childRun = delegatedTaskRun(childControls, task);
Expand Down Expand Up @@ -1502,6 +1558,10 @@ const make = Effect.gen(function* () {
request.status === "pending" &&
request.responseCapability.type === "live",
),
pendingApproval:
isTerminalTaskStatus(status) || task.completionDelivery?.state === "disposed"
? null
: pendingTaskApproval(childControls, task.id, childRun),
waitTimedOut,
} satisfies OrchestratorMcpDelegateTaskResult;
if (
Expand Down Expand Up @@ -1552,7 +1612,7 @@ const make = Effect.gen(function* () {
.getThreadEventSequence(scope.thread.threadId)
.pipe(Effect.mapError(streamError));
const initial = yield* readTask(scope, taskId, false, true);
if (initial.hasPendingQuestion || isTerminalTaskStatus(initial.status)) {
if (endsTaskWait(initial)) {
return Option.some(initial);
}
// One stream per event type, so transcript events never fill a buffer.
Expand All @@ -1568,7 +1628,7 @@ const make = Effect.gen(function* () {
).pipe(
Stream.mapError(streamError),
Stream.mapEffect(() => readTask(scope, taskId, false, true)),
Stream.filter((result) => result.hasPendingQuestion || isTerminalTaskStatus(result.status)),
Stream.filter(endsTaskWait),
Stream.runHead,
);
},
Expand Down
Loading
Loading