Skip to content
TypeScript

fix(server): deliver subagent results without history handoffs (#22117)

Julius Marminge4f5ff17172c5
diff --git a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
index 6de4e537b1f..d11bc51dfbf 100644
--- a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+++ b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
@@ -671,6 +671,93 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => {
assert.include(message?.text ?? "", String(firstTaskId));
assert.include(message?.text ?? "", String(secondTaskId));
assert.include(message?.text ?? "", "task_status");
+ assert.include(message?.text ?? "", "child finished");
+ }),
+ );
+
+ it.effect.each([
+ {
+ name: "oversized UTF-8 result",
+ first: "界".repeat(6_000),
+ second: "small result",
+ inlineFirst: false,
+ inlineSecond: true,
+ },
+ {
+ name: "combined results",
+ first: "a".repeat(10_000),
+ second: "b".repeat(10_000),
+ inlineFirst: true,
+ inlineSecond: false,
+ },
+ ])("uses task_status for $name beyond the inline budget", (testCase) =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const sink = yield* EventSink.EventSinkV2;
+ const now = yield* DateTime.now;
+ const threadId = ThreadId.make(`thread:inline-budget:${testCase.name}`);
+ const runId = RunId.make(`run:inline-budget:${testCase.name}`);
+ const firstTaskId = NodeId.make(`node:inline-first:${testCase.name}`);
+ const secondTaskId = NodeId.make(`node:inline-second:${testCase.name}`);
+ const messageId = MessageId.make(`message:delegated-delivery:${threadId}`);
+ yield* seedParentWithTerminalTask({
+ threadId,
+ projectId: ProjectId.make(`project:inline-budget:${testCase.name}`),
+ runId,
+ rootNodeId: NodeId.make(`node:inline-budget:${testCase.name}`),
+ taskId: firstTaskId,
+ deliveryState: "claimed",
+ completionWake: "always",
+ deliveryTaskIds: [firstTaskId, secondTaskId],
+ now,
+ });
+ const parent = yield* orchestrator.getThreadProjection(threadId);
+ const task = parent.subagents[0]!;
+ yield* sink.write({
+ events: [
+ { id: firstTaskId, result: testCase.first, status: "failed" as const },
+ { id: secondTaskId, result: testCase.second, status: "cancelled" as const },
+ ].map(({ id, result, status }) => ({
+ id: EventId.make(`event:inline-budget:${id}`),
+ type: "subagent.updated" as const,
+ threadId,
+ runId,
+ nodeId: id,
+ occurredAt: now,
+ payload: { ...task, id, result, status, title: "Inspect regression" },
+ })),
+ });
+ yield* orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(`command:inline-budget:${testCase.name}`),
+ threadId,
+ messageId,
+ text: "Delegated tasks completed.",
+ attachments: [],
+ dispatchMode: { type: "queue_after_active" },
+ createdBy: "agent",
+ creationSource: "server",
+ delegatedCompletion: {
+ parentRunId: runId,
+ generation: 1,
+ taskIds: [firstTaskId, secondTaskId],
+ },
+ });
+ const projection = yield* orchestrator.getThreadProjection(threadId);
+ const message = projection.messages.find((candidate) => candidate.id === messageId)!;
+ assert.equal(message.text.includes(testCase.first), testCase.inlineFirst);
+ assert.equal(message.text.includes(testCase.second), testCase.inlineSecond);
+ const deferredTaskId = testCase.inlineFirst ? secondTaskId : firstTaskId;
+ assert.include(
+ message.text,
+ `Delegated task ${deferredTaskId} (Inspect regression) [${testCase.inlineFirst ? "cancelled" : "failed"}]`,
+ );
+ assert.include(message.text, `Use task_status with taskId ${deferredTaskId}`);
+ assert.deepEqual(message.delegatedCompletion?.taskIds, [firstTaskId, secondTaskId]);
+ assert.equal(
+ projection.subagents.find((candidate) => candidate.id === deferredTaskId)?.result,
+ testCase.inlineFirst ? testCase.second : testCase.first,
+ );
}),
);
@@ -1644,6 +1731,98 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
);
it.layer(layerTest)("delegated tasks across a server restart", (it) => {
+ it.effect("queues a child result without creating a provider history handoff", () =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const eventSink = yield* EventSink.EventSinkV2;
+ const now = yield* DateTime.now;
+ const threadId = ThreadId.make("thread:result-message-parent");
+ const projectId = ProjectId.make("project:result-message");
+ const runId = RunId.make("run:result-message-parent");
+ const rootNodeId = NodeId.make("node:result-message-parent");
+ yield* seedParentWithTerminalTask({
+ threadId,
+ projectId,
+ runId,
+ rootNodeId,
+ taskId: NodeId.make("node:result-message-seed"),
+ deliveryState: "acknowledged",
+ now,
+ });
+ const child = yield* seedRestartCancelledChild({
+ parentThreadId: threadId,
+ projectId,
+ parentRunId: runId,
+ rootNodeId,
+ name: "result-message-child",
+ completionWake: "always",
+ continuationPending: false,
+ runStatus: "completed",
+ now,
+ });
+ const parent = yield* orchestrator.getThreadProjection(threadId);
+ const childProjection = yield* orchestrator.getThreadProjection(child.childThreadId);
+ const providerThreadId = ProviderThreadId.make("provider-thread:result-message-child");
+ yield* eventSink.write({
+ commandId: CommandId.make("command:result-message-child-provider"),
+ events: [
+ {
+ id: EventId.make("event:result-message-child-provider"),
+ type: "provider-thread.updated",
+ threadId: child.childThreadId,
+ occurredAt: now,
+ payload: {
+ ...parent.providerThreads[0]!,
+ id: providerThreadId,
+ appThreadId: child.childThreadId,
+ },
+ },
+ {
+ id: EventId.make("event:result-message-child-run"),
+ type: "run.updated",
+ threadId: child.childThreadId,
+ runId: child.childRunId,
+ occurredAt: now,
+ payload: { ...childProjection.runs[0]!, providerThreadId },
+ },
+ ],
+ });
+
+ yield* orchestrator.recoverDelegatedTasks;
+ const recovered = yield* orchestrator.getThreadProjection(threadId);
+ const task = recovered.subagents.find((candidate) => candidate.id === child.taskId)!;
+ assert.equal(task.status, "completed");
+ assert.isNotNull(task.result);
+ assert.isEmpty(recovered.contextHandoffs);
+ assert.equal(recovered.contextTransfers.length, 1);
+
+ const delivery = recovered.runs.find((candidate) => candidate.id === runId)!
+ .delegatedCompletion!.delivery!;
+ const messageId = delivery.messageId;
+ yield* orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make("command:result-message-delivery"),
+ threadId,
+ messageId,
+ text: "Delegated task finished.",
+ attachments: [],
+ dispatchMode: { type: "queue_after_active" },
+ createdBy: "agent",
+ creationSource: "server",
+ delegatedCompletion: {
+ parentRunId: runId,
+ generation: delivery.generation,
+ taskIds: [child.taskId],
+ },
+ });
+ const queued = yield* orchestrator.getThreadProjection(threadId);
+ assert.include(
+ queued.messages.find((message) => message.id === messageId)?.text ?? "",
+ task.result!,
+ );
+ }),
+ );
+
it.effect("holds a restart-cancelled child for its continuation's result", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 1d50d19f59f..94ced3e9922 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -596,11 +596,28 @@ function legacyPullRequestLink(
);
}
-function delegatedCompletionWakeDetail(taskIds: ReadonlyArray<string>): string {
- const taskList = taskIds.join(", ");
- return taskIds.length === 1
- ? `Delegated task ${taskList} reached a terminal state. Use task_status with taskId ${taskList} to read the result.`
- : `Delegated tasks ${taskList} reached terminal states. Use task_status with each taskId to read the results.`;
+const DELEGATED_RESULT_INLINE_BYTES = 16_000;
+
+function delegatedCompletionWakeDetail(
+ taskIds: ReadonlyArray<string>,
+ tasks: ReadonlyArray<OrchestrationV2Subagent>,
+): string {
+ let remainingBytes = DELEGATED_RESULT_INLINE_BYTES;
+ return taskIds
+ .map((taskId) => {
+ const task = tasks.find((candidate) => candidate.id === taskId);
+ const label = `Delegated task ${taskId}${task?.title == null ? "" : ` (${task.title})`}${task === undefined ? " reached a terminal state" : ` [${task.status}]`}`;
+ if (task?.result != null) {
+ const detail = `${label}:\n${task.result}`;
+ const bytes = Buffer.byteLength(detail);
+ if (bytes <= remainingBytes) {
+ remainingBytes -= bytes;
+ return detail;
+ }
+ }
+ return `${label}. Use task_status with taskId ${taskId} to read the result.`;
+ })
+ .join("\n\n");
}
function isTerminalDelegatedTaskStatus(status: OrchestrationV2Subagent["status"]): boolean {
@@ -2247,7 +2264,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
occurredAt: now,
payload: {
...message,
- text: delegatedCompletionWakeDetail(remainingTaskIds),
+ text: delegatedCompletionWakeDetail(remainingTaskIds, projection.subagents),
delegatedCompletion: {
parentRunId: parentRun.id,
generation: delivery.generation,
@@ -5092,7 +5109,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const dispatchText =
delegatedCompletion === undefined
? command.text
- : delegatedCompletionWakeDetail(delegatedCompletion.taskIds);
+ : delegatedCompletionWakeDetail(delegatedCompletion.taskIds, projection.subagents);
const sourcePlanProjection =
command.sourcePlanRef === undefined
? null
@@ -10345,7 +10362,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
? undefined
: {
...message,
- text: delegatedCompletionWakeDetail(taskIds),
+ text: delegatedCompletionWakeDetail(
+ taskIds,
+ input.parentProjection.subagents.map((task) =>
+ task.id === input.updatedTask.id ? input.updatedTask : task,
+ ),
+ ),
delegatedCompletion: {
parentRunId: input.parentRun.id,
generation: delivery.generation,
@@ -10635,46 +10657,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
targetThreadId: parentThreadId,
type: "subagent_result",
});
- const childProviderThread =
- childRun.providerThreadId === null
- ? undefined
- : childProjection.providerThreads.find(
- (candidate) => candidate.id === childRun.providerThreadId,
- );
- const parentProviderThread =
- parentRun?.providerThreadId === null || parentRun?.providerThreadId === undefined
- ? undefined
- : parentProjection.providerThreads.find(
- (candidate) => candidate.id === parentRun.providerThreadId,
- );
- const resultHandoff: OrchestrationV2ContextHandoff | null =
- parentRun === undefined ||
- childProviderThread === undefined ||
- parentProviderThread === undefined
- ? null
- : {
- id: yield* idAllocator.allocate.contextHandoff({
- threadId: parentThreadId,
- fromProviderInstanceId: childRun.providerInstanceId,
- toProviderInstanceId: parentRun.providerInstanceId,
- }),
- transferId: resultTransferId,
- threadId: parentThreadId,
- targetRunId: parentRun.id,
- fromProviderThreadIds: [childProviderThread.id],
- toProviderThreadId: parentProviderThread.id,
- coveredRunOrdinals: {
- from: childRun.ordinal,
- to: childRun.ordinal,
- },
- strategy: "manual_context",
- status: "ready",
- summaryMessageId: result.messageId,
- summaryText: result.text,
- createdByProviderInstanceId: childRun.providerInstanceId,
- createdAt: now,
- updatedAt: now,
- };
const resultTransfer: OrchestrationV2ContextTransfer = {
id: resultTransferId,
type: "subagent_result",
@@ -10690,13 +10672,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
parentRun?.providerInstanceId ?? parentProjection.thread.providerInstanceId,
targetRunId: parentRun?.id ?? null,
status: "consumed",
- resolution:
- resultHandoff === null
- ? null
- : {
- strategy: "portable_context",
- contextHandoffId: resultHandoff.id,
- },
+ resolution: null,
createdBy: "system",
error: null,
createdAt: now,
@@ -10785,18 +10761,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
},
},
]),
- ...(resultHandoff === null
- ? []
- : [
- {
- type: "context-handoff.updated" as const,
- threadId: parentThreadId,
- ...(parentRun === undefined ? {} : { runId: parentRun.id }),
- providerInstanceId: childRun.providerInstanceId,
- occurredAt: now,
- payload: resultHandoff,
- },
- ]),
{
type: "context-transfer.created",
threadId: parentThreadId,
diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.memory.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.memory.test.ts
index 566e35bf3dc..91f36959e76 100644
--- a/apps/server/src/orchestration-v2/ProviderTurnStartService.memory.test.ts
+++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.memory.test.ts
@@ -6,7 +6,7 @@ import { expect, it } from "vite-plus/test";
const execFile = NodeUtil.promisify(NodeChildProcess.execFile);
-it.each(["regular", "handoff", "handoff-compact", "handoff-failure"])(
+it.each(["regular", "handoff", "handoff-compact", "handoff-failure", "subagent-result"])(
"releases startup history while %s run workers remain alive",
async (mode) => {
const root = NodeURL.fileURLToPath(new URL("../../../../", import.meta.url));
diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts
index 85048cfbebc..325c4557636 100644
--- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts
@@ -252,8 +252,16 @@ export const layer: Layer.Layer<
const checkpointScope = projection.checkpointScopes.find(
(candidate) => candidate.id === rootNode?.checkpointScopeId,
);
+ // Older versions also stored subagent replies as history handoffs. Their
+ // queued completion messages carry the results without a history budget.
+ const subagentResultTransfers = new Set(
+ projection.contextTransfers
+ .filter((transfer) => transfer.type === "subagent_result")
+ .map((transfer) => transfer.id),
+ );
const handoffs = projection.contextHandoffs.filter(
(handoff) =>
+ (handoff.transferId == null || !subagentResultTransfers.has(handoff.transferId)) &&
handoff.status === "ready" &&
(handoff.targetRunId === run.id ||
(handoff.toProviderThreadId === run.providerThreadId &&
diff --git a/apps/server/src/orchestration-v2/testkit/ProviderTurnStartMemory.fixture.mjs b/apps/server/src/orchestration-v2/testkit/ProviderTurnStartMemory.fixture.mjs
index 840da274232..6ac1f2b172b 100644
--- a/apps/server/src/orchestration-v2/testkit/ProviderTurnStartMemory.fixture.mjs
+++ b/apps/server/src/orchestration-v2/testkit/ProviderTurnStartMemory.fixture.mjs
@@ -4,7 +4,7 @@ import * as NodeCrypto from "node:crypto";
import * as NodeAssert from "node:assert/strict";
const root = process.argv[2];
const mode = process.argv[3];
-const withHandoff = mode.startsWith("handoff");
+const withHandoff = mode.startsWith("handoff") || mode === "subagent-result";
const require = NodeModule.createRequire(root + "/apps/server/package.json");
const load = (name) => import(NodeURL.pathToFileURL(require.resolve("effect/" + name)));
const [Effect, Layer, FileSystem, Path] = await Promise.all([
@@ -160,7 +160,10 @@ function fixture(index) {
driver: "codex",
providerSessionId: "session",
nativeThreadRef: { nativeId: "native", driver: "codex", strength: "strong" },
- contextUsage: { usedTokens: 0, maxTokens: 128000 },
+ contextUsage:
+ mode === "subagent-result"
+ ? { usedTokens: 390458, maxTokens: 475000 }
+ : { usedTokens: 0, maxTokens: 128000 },
handoffIds: [],
},
],
@@ -170,19 +173,21 @@ function fixture(index) {
? [
{
id: "handoff",
+ transferId: mode === "subagent-result" ? "subagent-transfer" : null,
threadId: "thread",
targetRunId: runId,
toProviderThreadId: "provider",
fromProviderThreadIds: ["provider"],
coveredRunOrdinals: { from: 1, to: 1 },
status: "ready",
- strategy: "full_thread_summary",
+ strategy: mode === "subagent-result" ? "manual_context" : "full_thread_summary",
summaryText: "Synthetic summary",
handoffIds: [],
},
]
: [],
- contextTransfers: [],
+ contextTransfers:
+ mode === "subagent-result" ? [{ id: "subagent-transfer", type: "subagent_result" }] : [],
providerSessions: [],
providerTurns: [],
subagents: [],