Skip to content

Commit 5d62cde

Browse files
committed
fix(server): recover remote delegated launches after source restart
1 parent 8657eec commit 5d62cde

16 files changed

Lines changed: 1096 additions & 72 deletions

‎apps/mobile/src/features/threads/subagent-card-presentation.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as DateTime from "effect/DateTime";
22
import { describe, expect, it } from "vite-plus/test";
33
import { subagentCardDetail, subagentCardElapsed } from "./subagent-card-presentation";
4+
import { resolveSubagentRowPresentation, subagentThreadTarget } from "./threadAgentsPresentation";
45

56
describe("subagent card", () => {
67
it.each([
@@ -17,6 +18,35 @@ describe("subagent card", () => {
1718
const later = DateTime.makeUnsafe("2026-09-21T12:02:00Z");
1819
const done = { status: "completed" as const, startedAt: start, completedAt: end };
1920

21+
it("keeps a pending remote launch visible without opening a nonexistent thread", () => {
22+
const pending = {
23+
title: null,
24+
prompt: "Audit the timestamps",
25+
status: "running" as const,
26+
result: null,
27+
childThreadId: null,
28+
remoteChild: { environmentId: "environment-box" as never, label: "Box" },
29+
};
30+
expect(resolveSubagentRowPresentation(pending, true)).toMatchObject({
31+
runsOn: "Box",
32+
statusLabel: "Launching",
33+
canOpenThread: false,
34+
});
35+
expect(subagentThreadTarget(pending, "environment-here" as never, () => true)).toBeNull();
36+
const launchError = "Launch unconfirmed; work may still be running on Box.";
37+
expect(
38+
resolveSubagentRowPresentation(
39+
{
40+
...pending,
41+
status: "cancelled",
42+
result: "Cancelled.",
43+
remoteChild: { ...pending.remoteChild, launchError },
44+
},
45+
true,
46+
),
47+
).toMatchObject({ statusLabel: "Cancelled", detail: launchError, canOpenThread: false });
48+
});
49+
2050
it("counts live work but never uses a settled agent's age as its duration", () => {
2151
const unfinished = { ...done, completedAt: null };
2252
expect(subagentCardElapsed([unfinished], DateTime.toEpochMillis(later))).toBeNull();

‎apps/mobile/src/features/threads/threadAgentsPresentation.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export function subagentThreadTarget(
4848
return { environmentId, threadId: subagent.childThreadId };
4949
}
5050
const remote = subagent.remoteChild;
51-
return remote !== undefined && isKnownEnvironment(remote.environmentId)
51+
return remote !== undefined &&
52+
remote.threadId !== undefined &&
53+
isKnownEnvironment(remote.environmentId)
5254
? { environmentId: remote.environmentId, threadId: remote.threadId }
5355
: null;
5456
}
@@ -100,12 +102,19 @@ export function resolveSubagentRowPresentation(
100102
const live = isActiveSubagentStatus(subagent.status);
101103
return {
102104
title: rowTitle(subagent),
103-
detail: subagentDetailPreview(subagent),
104-
statusLabel: rowStatusLabel(subagent.status),
105+
detail: subagentDetailPreview({
106+
...subagent,
107+
result: subagent.remoteChild?.launchError ?? subagent.result,
108+
}),
109+
statusLabel:
110+
live && subagent.remoteChild !== undefined && subagent.remoteChild.threadId === undefined
111+
? "Launching"
112+
: rowStatusLabel(subagent.status),
105113
tone: rowTone(subagent.status),
106114
live,
107115
canOpenThread:
108-
subagent.childThreadId !== null || (subagent.remoteChild !== undefined && canOpenRemote),
116+
subagent.childThreadId !== null ||
117+
(subagent.remoteChild?.threadId !== undefined && canOpenRemote),
109118
runsOn: subagent.remoteChild?.label ?? null,
110119
};
111120
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,26 @@ const make = Effect.gen(function* () {
17581758
childThreadId: null,
17591759
...(task.remoteChild === undefined
17601760
? {}
1761-
: { remoteChild: task.remoteChild, link: formatThreadLink(task.remoteChild) }),
1761+
: {
1762+
...(task.remoteChild.threadId === undefined
1763+
? {}
1764+
: {
1765+
link: formatThreadLink({
1766+
environmentId: task.remoteChild.environmentId,
1767+
threadId: task.remoteChild.threadId,
1768+
}),
1769+
}),
1770+
remoteChild: {
1771+
environmentId: task.remoteChild.environmentId,
1772+
label: task.remoteChild.label,
1773+
...(task.remoteChild.launchError === undefined
1774+
? {}
1775+
: { launchError: task.remoteChild.launchError }),
1776+
...(task.remoteChild.threadId === undefined
1777+
? {}
1778+
: { threadId: task.remoteChild.threadId }),
1779+
},
1780+
}),
17621781
childRunId: null,
17631782
childNodeId: task.id,
17641783
status,

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

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
590590
return command.threadId;
591591
case "delegated_task.request":
592592
case "delegated_task.remote.request":
593+
case "delegated_task.remote.launch":
594+
case "delegated_task.remote.attach":
593595
case "delegated_task.remote.complete":
594596
case "delegated_task.remote.stop":
595597
case "delegated_task.wake-policy":
@@ -8165,6 +8167,147 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
81658167
});
81668168
});
81678169

8170+
/** Launch/attachment need the durable task, not a live parent provider run. */
8171+
const dispatchRemoteDelegatedTaskLaunch = Effect.fn(
8172+
"orchestrationV2.dispatch.remoteDelegatedTaskLaunch",
8173+
)(function* (
8174+
command: Extract<
8175+
OrchestrationV2InternalCommand,
8176+
{
8177+
readonly type: "delegated_task.remote.launch" | "delegated_task.remote.attach";
8178+
}
8179+
>,
8180+
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
8181+
) {
8182+
const parentProjection = yield* projectionStore
8183+
.getThreadRecords(command.parentThreadId, ["subagents", "turnItems", "nodes"], {
8184+
turnItemTypes: ["subagent"],
8185+
})
8186+
.pipe(
8187+
Effect.mapError(
8188+
(cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }),
8189+
),
8190+
);
8191+
const task = parentProjection.subagents.find(
8192+
(candidate) =>
8193+
candidate.id === command.taskId &&
8194+
candidate.origin === "app_owned" &&
8195+
candidate.remoteChild !== undefined,
8196+
);
8197+
if (task?.remoteChild === undefined) {
8198+
return yield* new OrchestratorDispatchError({
8199+
commandId: command.commandId,
8200+
commandType: command.type,
8201+
cause: "The remote launch does not belong to this parent task.",
8202+
});
8203+
}
8204+
const remote = task.remoteChild;
8205+
if (command.type === "delegated_task.remote.launch") {
8206+
if (remote.threadId !== undefined || remote.launchStarted === true || task.result !== null)
8207+
return;
8208+
if (remote.launch === undefined) {
8209+
return yield* new OrchestratorDispatchError({
8210+
commandId: command.commandId,
8211+
commandType: command.type,
8212+
cause: "The pending remote task has no launch inputs.",
8213+
});
8214+
}
8215+
} else {
8216+
if (
8217+
remote.environmentId !== command.environmentId ||
8218+
(remote.threadId !== undefined && remote.threadId !== command.threadId) ||
8219+
(remote.runId !== undefined && remote.runId !== command.runId) ||
8220+
(remote.threadId !== undefined && remote.runId === undefined && command.runId !== null)
8221+
) {
8222+
return yield* new OrchestratorDispatchError({
8223+
commandId: command.commandId,
8224+
commandType: command.type,
8225+
cause: "The accepted launch does not match this remote task.",
8226+
});
8227+
}
8228+
if (remote.threadId !== undefined) return;
8229+
if (remote.launchStarted !== true) {
8230+
return yield* new OrchestratorDispatchError({
8231+
commandId: command.commandId,
8232+
commandType: command.type,
8233+
cause: "The remote task's launch has not been started.",
8234+
});
8235+
}
8236+
}
8237+
const now = yield* DateTime.now;
8238+
const remoteChild =
8239+
command.type === "delegated_task.remote.launch"
8240+
? { ...remote, launchStarted: true }
8241+
: {
8242+
...remote,
8243+
threadId: command.threadId,
8244+
...(command.runId === null ? {} : { runId: command.runId }),
8245+
};
8246+
const status =
8247+
command.type === "delegated_task.remote.attach" && task.result === null
8248+
? "running"
8249+
: task.status;
8250+
const emitEvent = emit(events, command);
8251+
yield* emitEvent({
8252+
type: "subagent.updated",
8253+
threadId: command.parentThreadId,
8254+
...(task.runId === null ? {} : { runId: task.runId }),
8255+
nodeId: task.id,
8256+
driver: task.driver,
8257+
occurredAt: now,
8258+
payload: {
8259+
...task,
8260+
remoteChild,
8261+
status,
8262+
...(command.type === "delegated_task.remote.attach"
8263+
? {
8264+
providerInstanceId: command.modelSelection.instanceId,
8265+
model: command.modelSelection.model,
8266+
}
8267+
: {}),
8268+
updatedAt: now,
8269+
},
8270+
});
8271+
const item = parentProjection.turnItems.find(
8272+
(candidate) => candidate.type === "subagent" && candidate.subagentId === task.id,
8273+
);
8274+
if (item !== undefined && item.type === "subagent") {
8275+
yield* emitEvent({
8276+
type: "turn-item.updated",
8277+
threadId: command.parentThreadId,
8278+
...(item.runId === null ? {} : { runId: item.runId }),
8279+
...(item.nodeId === null ? {} : { nodeId: item.nodeId }),
8280+
driver: task.driver,
8281+
occurredAt: now,
8282+
payload: {
8283+
...item,
8284+
remoteChild,
8285+
status:
8286+
command.type === "delegated_task.remote.attach" && task.result === null
8287+
? "running"
8288+
: item.status,
8289+
updatedAt: now,
8290+
},
8291+
});
8292+
}
8293+
const node = parentProjection.nodes.find((candidate) => candidate.id === task.id);
8294+
if (
8295+
node !== undefined &&
8296+
command.type === "delegated_task.remote.attach" &&
8297+
task.result === null
8298+
) {
8299+
yield* emitEvent({
8300+
type: "node.updated",
8301+
threadId: command.parentThreadId,
8302+
...(node.runId === null ? {} : { runId: node.runId }),
8303+
nodeId: node.id,
8304+
driver: task.driver,
8305+
occurredAt: now,
8306+
payload: { ...node, status: "running" },
8307+
});
8308+
}
8309+
});
8310+
81688311
/**
81698312
* Completes a task that ran in a linked environment: the parent half of
81708313
* `finalizeAppOwnedSubagent`, with the result its thread there ended with.
@@ -8220,7 +8363,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
82208363
const remoteChild =
82218364
command.stoppedThere === undefined
82228365
? task.remoteChild
8223-
: { ...task.remoteChild!, stoppedThere: true };
8366+
: {
8367+
...task.remoteChild!,
8368+
stoppedThere: true,
8369+
...(command.launchError === undefined ? {} : { launchError: command.launchError }),
8370+
};
82248371
// The first result stands; a repeated follower report records nothing, which
82258372
// the dispatcher refuses. A stop there after the result is still recorded, but
82268373
// a run that ended by itself does not settle a stop the task here still owes.
@@ -13339,6 +13486,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1333913486
case "delegated_task.remote.request":
1334013487
yield* dispatchRemoteDelegatedTaskRequest(command, events);
1334113488
break;
13489+
case "delegated_task.remote.launch":
13490+
case "delegated_task.remote.attach":
13491+
yield* dispatchRemoteDelegatedTaskLaunch(command, events);
13492+
break;
1334213493
case "delegated_task.remote.complete":
1334313494
yield* dispatchRemoteDelegatedTaskComplete(command, events);
1334413495
break;
@@ -13463,6 +13614,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1346313614
command.type === "thread.interrupt.unresponsive" ||
1346413615
command.type === "thread.stop" ||
1346513616
command.type === "subagent.stop" ||
13617+
command.type === "delegated_task.remote.launch" ||
13618+
command.type === "delegated_task.remote.attach" ||
1346613619
// Closing a share again, or after it ended, is not an error.
1346713620
command.type === "thread.guest-share.close" ||
1346813621
(command.type === "thread.delete" && command.onlyIfUnused === true),

0 commit comments

Comments
 (0)