Skip to content

Commit 26cf033

Browse files
fix(server): bound queued subagent result payloads
1 parent cd39a86 commit 26cf033

2 files changed

Lines changed: 94 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,88 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => {
674674
}),
675675
);
676676

677+
it.effect.each([
678+
{
679+
name: "oversized UTF-8 result",
680+
first: "界".repeat(6_000),
681+
second: "small result",
682+
inlineFirst: false,
683+
inlineSecond: true,
684+
},
685+
{
686+
name: "combined results",
687+
first: "a".repeat(10_000),
688+
second: "b".repeat(10_000),
689+
inlineFirst: true,
690+
inlineSecond: false,
691+
},
692+
])("uses task_status for $name beyond the inline budget", (testCase) =>
693+
Effect.gen(function* () {
694+
const orchestrator = yield* Orchestrator.OrchestratorV2;
695+
const sink = yield* EventSink.EventSinkV2;
696+
const now = yield* DateTime.now;
697+
const threadId = ThreadId.make(`thread:inline-budget:${testCase.name}`);
698+
const runId = RunId.make(`run:inline-budget:${testCase.name}`);
699+
const firstTaskId = NodeId.make(`node:inline-first:${testCase.name}`);
700+
const secondTaskId = NodeId.make(`node:inline-second:${testCase.name}`);
701+
const messageId = MessageId.make(`message:delegated-delivery:${threadId}`);
702+
yield* seedParentWithTerminalTask({
703+
threadId,
704+
projectId: ProjectId.make(`project:inline-budget:${testCase.name}`),
705+
runId,
706+
rootNodeId: NodeId.make(`node:inline-budget:${testCase.name}`),
707+
taskId: firstTaskId,
708+
deliveryState: "claimed",
709+
completionWake: "always",
710+
deliveryTaskIds: [firstTaskId, secondTaskId],
711+
now,
712+
});
713+
const parent = yield* orchestrator.getThreadProjection(threadId);
714+
const task = parent.subagents[0]!;
715+
yield* sink.write({
716+
events: [
717+
{ id: firstTaskId, result: testCase.first },
718+
{ id: secondTaskId, result: testCase.second },
719+
].map(({ id, result }) => ({
720+
id: EventId.make(`event:inline-budget:${id}`),
721+
type: "subagent.updated" as const,
722+
threadId,
723+
runId,
724+
nodeId: id,
725+
occurredAt: now,
726+
payload: { ...task, id, result },
727+
})),
728+
});
729+
yield* orchestrator.dispatch({
730+
type: "message.dispatch",
731+
commandId: CommandId.make(`command:inline-budget:${testCase.name}`),
732+
threadId,
733+
messageId,
734+
text: "Delegated tasks completed.",
735+
attachments: [],
736+
dispatchMode: { type: "queue_after_active" },
737+
createdBy: "agent",
738+
creationSource: "server",
739+
delegatedCompletion: {
740+
parentRunId: runId,
741+
generation: 1,
742+
taskIds: [firstTaskId, secondTaskId],
743+
},
744+
});
745+
const projection = yield* orchestrator.getThreadProjection(threadId);
746+
const message = projection.messages.find((candidate) => candidate.id === messageId)!;
747+
assert.equal(message.text.includes(testCase.first), testCase.inlineFirst);
748+
assert.equal(message.text.includes(testCase.second), testCase.inlineSecond);
749+
const deferredTaskId = testCase.inlineFirst ? secondTaskId : firstTaskId;
750+
assert.include(message.text, `Use task_status with taskId ${deferredTaskId}`);
751+
assert.deepEqual(message.delegatedCompletion?.taskIds, [firstTaskId, secondTaskId]);
752+
assert.equal(
753+
projection.subagents.find((candidate) => candidate.id === deferredTaskId)?.result,
754+
testCase.inlineFirst ? testCase.second : testCase.first,
755+
);
756+
}),
757+
);
758+
677759
it.effect("does not re-offer when wake-policy upgrades after delivered ownership settled", () =>
678760
Effect.gen(function* () {
679761
const orchestrator = yield* Orchestrator.OrchestratorV2;

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,25 @@ function legacyPullRequestLink(
590590
);
591591
}
592592

593+
const DELEGATED_RESULT_INLINE_BYTES = 16_000;
594+
593595
function delegatedCompletionWakeDetail(
594596
taskIds: ReadonlyArray<string>,
595597
tasks: ReadonlyArray<OrchestrationV2Subagent>,
596598
): string {
599+
let remainingBytes = DELEGATED_RESULT_INLINE_BYTES;
597600
return taskIds
598601
.map((taskId) => {
599602
const task = tasks.find((candidate) => candidate.id === taskId);
600-
return task?.result == null
601-
? `Delegated task ${taskId} reached a terminal state. Use task_status with taskId ${taskId} to read the result.`
602-
: `Delegated task ${taskId}${task.title === null ? "" : ` (${task.title})`} [${task.status}]:\n${task.result}`;
603+
if (task?.result != null) {
604+
const detail = `Delegated task ${taskId}${task.title === null ? "" : ` (${task.title})`} [${task.status}]:\n${task.result}`;
605+
const bytes = Buffer.byteLength(detail);
606+
if (bytes <= remainingBytes) {
607+
remainingBytes -= bytes;
608+
return detail;
609+
}
610+
}
611+
return `Delegated task ${taskId} reached a terminal state. Use task_status with taskId ${taskId} to read the result.`;
603612
})
604613
.join("\n\n");
605614
}

0 commit comments

Comments
 (0)