@@ -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