Skip to content

Commit 0394c18

Browse files
Merge remote-tracking branch 'origin/main' into pr-32524
2 parents 45db815 + 20c3dd3 commit 0394c18

16 files changed

Lines changed: 699 additions & 210 deletions

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ describe("OrchestratorMcpService", () => {
9191
const parentProjection = {
9292
thread: { id: parentThreadId },
9393
runs: [],
94-
contextTransfers: [
95-
{
96-
id: resultTransferId,
97-
type: "subagent_result",
98-
sourceThreadId: childThreadId,
99-
targetThreadId: parentThreadId,
100-
sourcePoint: { runId: childRunId },
101-
},
102-
],
10394
subagents: [
10495
{
10596
id: taskId,
@@ -131,7 +122,15 @@ describe("OrchestratorMcpService", () => {
131122
completedAt: DateTime.makeUnsafe("2026-10-03T10:05:00Z"),
132123
},
133124
],
134-
contextTransfers: [],
125+
contextTransfers: [
126+
{
127+
id: resultTransferId,
128+
type: "subagent_result",
129+
sourceThreadId: childThreadId,
130+
targetThreadId: parentThreadId,
131+
sourcePoint: { runId: childRunId },
132+
},
133+
],
135134
messages: [
136135
{
137136
id: MessageId.make("message:mcp-ack-result"),
@@ -160,6 +159,7 @@ describe("OrchestratorMcpService", () => {
160159
return hasNestedWork
161160
? {
162161
...childProjection,
162+
contextTransfers: [],
163163
subagents: [
164164
{ ...parentProjection.subagents[0]!, status: "running" as const },
165165
],
@@ -169,7 +169,6 @@ describe("OrchestratorMcpService", () => {
169169
const snapshot = hasNestedWork
170170
? {
171171
...parentProjection,
172-
contextTransfers: [],
173172
subagents: parentProjection.subagents.map((task) => ({
174173
...task,
175174
result: null,

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

Lines changed: 43 additions & 100 deletions
Large diffs are not rendered by default.

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,34 @@ describe("orchestrator MCP toolkit", () => {
216216
},
217217
CodexOrchestratorReplayHarness,
218218
);
219+
// SQL spans carry no bound parameters, so this records which thread each tool read.
220+
const threadRecordReads: Array<{
221+
readonly threadId: ThreadId;
222+
readonly fields: ReadonlyArray<string>;
223+
}> = [];
219224
const layerOrchestration = Layer.merge(
220225
layerOrchestrator,
221-
ThreadManagementService.layer.pipe(
222-
Layer.provide(layerOrchestrator),
223-
Layer.provide(ServerSettings.layerTest()),
226+
Layer.effect(
227+
ThreadManagementService.ThreadManagementService,
228+
Effect.gen(function* () {
229+
const service = yield* ThreadManagementService.ThreadManagementService;
230+
return ThreadManagementService.ThreadManagementService.of({
231+
...service,
232+
getThreadRecords: (threadId, fields, filter) =>
233+
Effect.sync(() => threadRecordReads.push({ threadId, fields })).pipe(
234+
Effect.andThen(service.getThreadRecords(threadId, fields, filter)),
235+
),
236+
});
237+
}),
238+
).pipe(
224239
Layer.provide(
225-
CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
240+
ThreadManagementService.layer.pipe(
241+
Layer.provide(layerOrchestrator),
242+
Layer.provide(ServerSettings.layerTest()),
243+
Layer.provide(
244+
CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
245+
),
246+
),
226247
),
227248
),
228249
);
@@ -603,6 +624,9 @@ describe("orchestrator MCP toolkit", () => {
603624
);
604625
const transferReads: Array<string> = [];
605626
const messageReads: Array<string> = [];
627+
const runReads: Array<string> = [];
628+
const projectionTableReads: Array<string> = [];
629+
const acknowledgementReads: Array<string> = [];
606630
const tracer = Tracer.make({
607631
span(options) {
608632
const span = new Tracer.NativeSpan(options);
@@ -611,11 +635,29 @@ describe("orchestrator MCP toolkit", () => {
611635
end(endTime, exit);
612636
const query = span.attributes.get("db.query.text");
613637
if (typeof query !== "string") return;
638+
let parent = Option.getOrUndefined(span.parent);
639+
while (parent?._tag === "Span") {
640+
if (
641+
parent.name === "Orchestrator.dispatchDelegatedTaskCompletionDeliveryResolution"
642+
) {
643+
acknowledgementReads.push(query);
644+
break;
645+
}
646+
parent = Option.getOrUndefined(parent.parent);
647+
}
648+
for (const [, table] of query.matchAll(
649+
/FROM orchestration_v2_projection_(\w+)/gi,
650+
)) {
651+
projectionTableReads.push(table!);
652+
}
614653
if (
615654
/SELECT[\s\S]*FROM orchestration_v2_projection_context_transfers/i.test(query)
616655
) {
617656
transferReads.push(query);
618657
}
658+
if (/SELECT\s+payload_json\s+FROM orchestration_v2_projection_runs/i.test(query)) {
659+
runReads.push(query);
660+
}
619661
if (
620662
/SELECT\s+payload_json\s+FROM orchestration_v2_projection_messages/i.test(query)
621663
) {
@@ -687,18 +729,53 @@ describe("orchestrator MCP toolkit", () => {
687729
expect(read.isError).not.toBe(true);
688730
expect(transferReads.length).toBeGreaterThan(0);
689731
expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
732+
// The acknowledgement reads the task and its spawning run by id, not the parent's history.
733+
expect(acknowledgementReads.length).toBeGreaterThan(0);
734+
expect(
735+
acknowledgementReads.filter(
736+
(query) =>
737+
/FROM orchestration_v2_projection_(nodes|run_attempts)\b/.test(query) ||
738+
(/FROM orchestration_v2_projection_runs\b/.test(query) &&
739+
!query.includes("AND run_id IN")) ||
740+
(/FROM orchestration_v2_projection_subagents\b/.test(query) &&
741+
!query.includes("AND subagent_id IN")),
742+
),
743+
).toEqual([]);
690744
const afterRead = yield* orchestrator.getThreadProjection(parentThreadId);
691745
expect(
692746
afterRead.subagents.find((task) => task.id === queuedFollowup.taskId)
693747
?.completionDelivery?.state,
694748
).toBe("acknowledged");
695749

696750
const messageReadsBeforeCapabilities = messageReads.length;
751+
const tableReadsBeforeCapabilities = projectionTableReads.length;
697752
const capabilities = yield* invoke("orchestrator_capabilities", {}).pipe(
698753
Effect.withTracer(tracer),
699754
);
700755
expect(capabilities.isError).not.toBe(true);
701756
expect(messageReads).toHaveLength(messageReadsBeforeCapabilities);
757+
// The caller check reads only the thread row, never its history.
758+
expect(new Set(projectionTableReads.slice(tableReadsBeforeCapabilities))).toEqual(
759+
new Set(["threads"]),
760+
);
761+
const runReadsBeforeStatus = runReads.length;
762+
const transferReadsBeforeStatus = transferReads.length;
763+
const threadRecordReadsBeforeStatus = threadRecordReads.length;
764+
const statusAgain = yield* invoke("task_status", { taskId: delegated.taskId }).pipe(
765+
Effect.withTracer(tracer),
766+
);
767+
expect(statusAgain.isError).not.toBe(true);
768+
// A task wait reads result transfers and runs from the child; the parent's stay unread.
769+
expect(runReads).toHaveLength(runReadsBeforeStatus + 1);
770+
expect(transferReads).toHaveLength(transferReadsBeforeStatus + 1);
771+
expect(
772+
threadRecordReads
773+
.slice(threadRecordReadsBeforeStatus)
774+
.filter(
775+
({ fields }) => fields.includes("runs") || fields.includes("contextTransfers"),
776+
)
777+
.map(({ threadId }) => threadId),
778+
).toEqual([delegated.childThreadId]);
702779
}).pipe(Effect.provide(layerTest));
703780
}),
704781
),

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,73 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => {
10551055
}),
10561056
);
10571057

1058+
it.effect("acknowledging a queued wake's results rewrites it, then cancels it", () =>
1059+
Effect.gen(function* () {
1060+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1061+
const now = yield* DateTime.now;
1062+
const threadId = ThreadId.make("queued-wake-ack-parent");
1063+
const runId = RunId.make("queued-wake-ack-origin");
1064+
const taskId = NodeId.make("queued-wake-ack-first");
1065+
const siblingId = NodeId.make("queued-wake-ack-sibling");
1066+
yield* seedParentWithTerminalTask({
1067+
threadId,
1068+
projectId: ProjectId.make("queued-wake-ack-project"),
1069+
runId,
1070+
rootNodeId: NodeId.make("queued-wake-ack-root"),
1071+
taskId,
1072+
deliveryState: "claimed",
1073+
deliveryTaskIds: [taskId, siblingId],
1074+
siblingTasks: [{ id: siblingId, deliveryState: "claimed" }],
1075+
completionWake: "always",
1076+
now,
1077+
});
1078+
const delivery = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!
1079+
.delegatedCompletion!.delivery!;
1080+
yield* orchestrator.dispatch({
1081+
type: "message.dispatch",
1082+
createdBy: "agent",
1083+
creationSource: "server",
1084+
commandId: CommandId.make("queued-wake-ack-wake"),
1085+
threadId,
1086+
messageId: delivery.messageId,
1087+
text: "Delegated tasks completed",
1088+
attachments: [],
1089+
modelSelection,
1090+
dispatchMode: { type: "queue_after_active" },
1091+
delegatedCompletion: {
1092+
parentRunId: runId,
1093+
generation: delivery.generation,
1094+
taskIds: delivery.taskIds,
1095+
},
1096+
});
1097+
const acknowledge = (id: NodeId) =>
1098+
orchestrator.dispatch({
1099+
type: "delegated_task.completion-delivery.acknowledge",
1100+
commandId: CommandId.make(`queued-wake-ack:${id}`),
1101+
parentThreadId: threadId,
1102+
taskId: id,
1103+
observedByRunId: runId,
1104+
});
1105+
1106+
yield* acknowledge(taskId);
1107+
const rewritten = yield* orchestrator.getThreadProjection(threadId);
1108+
const wake = rewritten.runs.find((run) => run.userMessageId === delivery.messageId)!;
1109+
assert.equal(wake.status, "queued");
1110+
const message = rewritten.messages.find((row) => row.id === delivery.messageId);
1111+
assert.deepEqual(message?.delegatedCompletion?.taskIds, [siblingId]);
1112+
assert.equal(message?.text, `Delegated task ${siblingId} [completed]:\nchild finished`);
1113+
1114+
yield* acknowledge(siblingId);
1115+
const cancelled = yield* orchestrator.getThreadProjection(threadId);
1116+
assert.equal(cancelled.runs.find((run) => run.id === wake.id)?.status, "cancelled");
1117+
assert.equal(
1118+
cancelled.nodes.find((node) => node.id === wake.rootNodeId)?.status,
1119+
"cancelled",
1120+
);
1121+
assert.isNull(cancelled.runs.find((run) => run.id === runId)?.delegatedCompletion?.delivery);
1122+
}),
1123+
);
1124+
10581125
it.effect.each([
10591126
{ command: "acknowledge", before: "running", after: "failed", capture: false },
10601127
{ command: "acknowledge", before: "waiting", after: "completed", capture: true },

0 commit comments

Comments
 (0)