Skip to content

Commit 2f2730c

Browse files
only21milmaria-rcksjuliusmarminge
authored
fix(server): task_status says starting until the child's provider starts (pingdotgg#24475)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent fd0ffd0 commit 2f2730c

6 files changed

Lines changed: 155 additions & 17 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,95 @@ describe("OrchestratorMcpService", () => {
351351
}),
352352
);
353353

354+
it.effect("reports a child run the provider has not started yet as starting", () =>
355+
Effect.gen(function* () {
356+
const parentThreadId = ThreadId.make("thread:mcp-starting-parent");
357+
const childThreadId = ThreadId.make("thread:mcp-starting-child");
358+
const childRunId = RunId.make("run:mcp-starting-child");
359+
const taskId = NodeId.make("node:mcp-starting-task");
360+
const parentProjection = {
361+
thread: { id: parentThreadId },
362+
runs: [],
363+
contextTransfers: [],
364+
subagents: [
365+
{
366+
id: taskId,
367+
threadId: parentThreadId,
368+
origin: "app_owned",
369+
childThreadId,
370+
driver: "codex",
371+
model: "gpt-5.6-terra",
372+
status: "running",
373+
result: null,
374+
completionDelivery: null,
375+
},
376+
],
377+
} as unknown as OrchestrationV2ThreadProjection;
378+
let providerStarted = false;
379+
const childProjection = () =>
380+
({
381+
thread: { id: childThreadId, title: "Child task" },
382+
runs: [
383+
{
384+
id: childRunId,
385+
ordinal: 1,
386+
status: providerStarted ? "running" : "starting",
387+
startedAt: providerStarted ? DateTime.makeUnsafe("2026-10-24T10:00:00Z") : null,
388+
delegatedTaskId: taskId,
389+
},
390+
],
391+
contextTransfers: [],
392+
messages: [],
393+
subagents: [],
394+
providerThreads: [{ status: "not_loaded", pendingBackgroundTasks: [] }],
395+
providerTurns: [],
396+
runtimeRequests: [],
397+
turnItems: [],
398+
}) as unknown as OrchestrationV2ThreadProjection;
399+
const layerDependencies = Layer.mergeAll(
400+
NodeServices.layer,
401+
NodeCrypto.layer,
402+
Layer.mock(ThreadManagementService.ThreadManagementService)({
403+
getThreadRecords: (threadId) =>
404+
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection()),
405+
}),
406+
ServerSettings.layerTest(),
407+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
408+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
409+
list: () => Effect.succeed([]),
410+
}),
411+
Layer.mock(ProjectService.ProjectService)({}),
412+
Layer.mock(SecretRequests.SecretRequests)({}),
413+
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
414+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
415+
);
416+
const scope: McpInvocationScope = {
417+
environmentId: EnvironmentId.make("environment:mcp-starting"),
418+
requestNamespace: "provider-session:mcp-starting",
419+
thread: {
420+
threadId: parentThreadId,
421+
providerSessionId: "provider-session:mcp-starting",
422+
providerInstanceId: ProviderInstanceId.make("codex"),
423+
},
424+
client: undefined,
425+
capabilities: new Set(["orchestration"]),
426+
issuedAt: 1,
427+
};
428+
429+
yield* Effect.gen(function* () {
430+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
431+
const waiting = yield* service.taskStatus(scope, taskId);
432+
assert.equal(waiting.status, "starting");
433+
assert.equal(waiting.workState, "working");
434+
assert.equal(waiting.childRunId, childRunId);
435+
providerStarted = true;
436+
const started = yield* service.taskStatus(scope, taskId);
437+
assert.equal(started.status, "running");
438+
assert.equal(started.workState, "working");
439+
}).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies))));
440+
}),
441+
);
442+
354443
it.effect("tells the parent to end its turn only while its task still runs", () =>
355444
Effect.gen(function* () {
356445
const parentThreadId = ThreadId.make("thread:mcp-next-parent");
@@ -2665,6 +2754,7 @@ describe("OrchestratorMcpService provider resolution", () => {
26652754
const parent = yield* orchestrator.getThreadProjection(parentThreadId);
26662755
return { taskId: parent.subagents[0]!.id };
26672756
}).pipe(Effect.orDie),
2757+
runStatus: () => Effect.succeed(null),
26682758
});
26692759
}),
26702760
).pipe(Layer.provide(layerWatchedThreads));

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ function taskStatusForRun(
457457
switch (run === undefined ? undefined : runOutcome(run)) {
458458
case "queued":
459459
return "queued";
460+
case "starting":
461+
return "starting";
460462
case "waiting":
461463
return "waiting";
462464
case "completed":
@@ -469,7 +471,6 @@ function taskStatusForRun(
469471
case "interrupted":
470472
return "interrupted";
471473
case "preparing":
472-
case "starting":
473474
case "running":
474475
case undefined:
475476
return "running";
@@ -1669,6 +1670,7 @@ const make = Effect.gen(function* () {
16691670
.pipe(Effect.mapError(threadManagementFailure)));
16701671
const workState =
16711672
task.result !== null ? "result_available" : heldForRestart ? "working" : taskWorkState;
1673+
const runStatus = taskStatusForRun(taskResultRun ?? childRun);
16721674
const status =
16731675
task.result !== null
16741676
? taskStatusForRun(
@@ -1681,12 +1683,11 @@ const make = Effect.gen(function* () {
16811683
)
16821684
: workState === "result_available"
16831685
? // A cancelled task's finished turn settles it as interrupted.
1684-
task.cancelledRunId !== undefined &&
1685-
taskStatusForRun(taskResultRun ?? childRun) === "completed"
1686+
task.cancelledRunId !== undefined && runStatus === "completed"
16861687
? "interrupted"
1687-
: taskStatusForRun(taskResultRun ?? childRun)
1688-
: taskStatusForRun(taskResultRun ?? childRun) === "queued"
1689-
? "queued"
1688+
: runStatus
1689+
: runStatus === "queued" || runStatus === "starting"
1690+
? runStatus
16901691
: "running";
16911692
const derivedResult =
16921693
task.result !== null
@@ -1877,7 +1878,9 @@ const make = Effect.gen(function* () {
18771878
): Effect.fn.Return<OrchestratorMcpDelegateTaskResult, OrchestratorMcpFailure> {
18781879
const status: OrchestratorMcpDelegateTaskResult["status"] =
18791880
task.result === null
1880-
? "running"
1881+
? (yield* remoteDelegation.runStatus(scope, task)) === "starting"
1882+
? "starting"
1883+
: "running"
18811884
: task.status === "completed" ||
18821885
task.status === "failed" ||
18831886
task.status === "cancelled" ||

‎apps/server/src/peer/RemoteDelegation.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,15 @@ it.effect("a task delegated to the box wakes the laptop's parent when it ends th
952952
expect(retried.taskId).toBe(delegated.taskId);
953953
expect((yield* a.orchestrator.getThreadProjection(parentThreadId)).subagents).toHaveLength(1);
954954

955+
// Its provider there has not started the turn yet.
956+
yield* Ref.set(thread.status, "starting");
957+
const starting = yield* a.call("task_status", { taskId: delegated.taskId });
958+
expect(starting.structuredContent).toMatchObject({
959+
status: "starting",
960+
workState: "working",
961+
});
962+
yield* Ref.set(thread.status, "running");
963+
955964
const historyReads: Array<string> = [];
956965
const historyTracer = Tracer.make({
957966
span(options) {

‎apps/server/src/peer/RemoteDelegation.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ export class RemoteDelegation extends Context.Service<
121121
readonly clientRequestId?: string | undefined;
122122
},
123123
) => Effect.Effect<OrchestratorMcpTaskCancelResult, OrchestratorMcpFailure>;
124+
/**
125+
* The status of the task's run there, or null when it cannot be read: an
126+
* unreachable machine reads as no news rather than failing the read here.
127+
*/
128+
readonly runStatus: (
129+
scope: ThreadScope,
130+
task: OrchestrationV2Subagent,
131+
) => Effect.Effect<OrchestratorMcpThreadRun["status"] | null>;
124132
/**
125133
* Follows every open remote task until it ends, including after a
126134
* restart, and a finished one again when a Stop here owes it a stop there.
@@ -978,6 +986,20 @@ const make = Effect.gen(function* () {
978986
},
979987
);
980988

989+
const runStatus = Effect.fn("RemoteDelegation.runStatus")(
990+
function* (scope: ThreadScope, task: OrchestrationV2Subagent) {
991+
const remote = task.remoteChild;
992+
if (remote?.runId === undefined) return null;
993+
const read = yield* forwarding.readThread(
994+
followScope(scope.thread, remote),
995+
remote.environmentId,
996+
{ threadId: remote.threadId, limit: 1, runLimit: 50 },
997+
);
998+
return read.recentRuns.find((run) => run.runId === remote.runId)?.status ?? null;
999+
},
1000+
Effect.orElseSucceed(() => null),
1001+
);
1002+
9811003
const start: RemoteDelegation["Service"]["start"] = () =>
9821004
forkParked(
9831005
Effect.gen(function* () {
@@ -1004,7 +1026,7 @@ const make = Effect.gen(function* () {
10041026
}).pipe(Effect.ignoreCause({ log: true })),
10051027
);
10061028

1007-
return RemoteDelegation.of({ delegate, followUp, cancel, start });
1029+
return RemoteDelegation.of({ delegate, followUp, cancel, runStatus, start });
10081030
});
10091031

10101032
/** A defect, logged, as a failure, so a retry covers it. */

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,15 @@ type DelegateTaskResult = {
293293
childThreadId: string;
294294
childRunId: string | null;
295295
childNodeId: string;
296-
status: "queued" | "running" | "waiting" | "completed" | "failed" | "cancelled" | "interrupted";
296+
status:
297+
| "queued"
298+
| "starting"
299+
| "running"
300+
| "waiting"
301+
| "completed"
302+
| "failed"
303+
| "cancelled"
304+
| "interrupted";
297305
workState: "working" | "waiting_for_children" | "result_available";
298306
hasPendingChildRuns: boolean;
299307
providerInstanceId: string;
@@ -318,12 +326,15 @@ type DelegateTaskResult = {
318326

319327
Reads a delegated task from the parent thread's durable projection. A task ID
320328
from another parent thread is rejected. `childRunId` identifies the original
321-
run. `workState` distinguishes active work, a finished turn waiting for children,
322-
and an available result. The task remains nonterminal until its known work
323-
finishes. Its published `summary` and result transfer then remain stable across
324-
later follow-ups. `hasPendingChildRuns` reports later queued or executing turns;
325-
`latestTerminal*` exposes later executed, non-monitor results without replacing
326-
the published task result.
329+
run. `status: "starting"` means the child's provider has not started its turn
330+
yet, so a task that stays there has not begun work. A task in a linked
331+
environment reads its run's status there for this. `workState` distinguishes
332+
active work, a finished turn waiting for children, and an available result. The
333+
task remains nonterminal until its known work finishes. Its published `summary`
334+
and result transfer then remain stable across later follow-ups.
335+
`hasPendingChildRuns` reports later queued or executing turns; `latestTerminal*`
336+
exposes later executed, non-monitor results without replacing the published task
337+
result.
327338

328339
### `task_cancel`
329340

‎packages/contracts/src/orchestratorMcp.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export type OrchestratorMcpTaskRole = typeof OrchestratorMcpTaskRole.Type;
148148

149149
export const OrchestratorMcpDelegatedTaskStatus = Schema.Literals([
150150
"queued",
151+
"starting",
151152
"running",
152153
"waiting",
153154
"completed",
@@ -220,7 +221,10 @@ export const OrchestratorMcpDelegateTaskResult = Schema.Struct({
220221
link: Schema.optional(Schema.String),
221222
childRunId: Schema.NullOr(RunId),
222223
childNodeId: NodeId,
223-
status: OrchestratorMcpDelegatedTaskStatus,
224+
status: OrchestratorMcpDelegatedTaskStatus.annotate({
225+
description:
226+
"starting means the child's provider has not started its turn yet, so no work has begun.",
227+
}),
224228
workState: Schema.Literals(["working", "waiting_for_children", "result_available"]),
225229
hasPendingChildRuns: Schema.Boolean,
226230
latestTerminalRunId: Schema.NullOr(RunId),
@@ -288,7 +292,6 @@ export type OrchestratorMcpCreateThreadsInput = typeof OrchestratorMcpCreateThre
288292
export const OrchestratorMcpCreatedThreadStatus = Schema.Union([
289293
Schema.Literal("idle"),
290294
Schema.Literal("preparing"),
291-
Schema.Literal("starting"),
292295
OrchestratorMcpDelegatedTaskStatus,
293296
Schema.Literal("rolled_back"),
294297
]);

0 commit comments

Comments
 (0)