Skip to content

Commit 9f9eaff

Browse files
authored
perf(server): task waits skip unrelated sibling updates (#27920)
1 parent 01bd0a2 commit 9f9eaff

2 files changed

Lines changed: 168 additions & 6 deletions

File tree

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

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import {
1919
RunId,
2020
ThreadId,
2121
type OrchestrationV2ServerCommand,
22+
type OrchestrationV2StoredEvent,
2223
type OrchestrationV2ThreadProjection,
2324
type ServerProvider,
2425
} from "@t3tools/contracts";
2526
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
2627
import * as DateTime from "effect/DateTime";
28+
import * as Deferred from "effect/Deferred";
2729
import * as Effect from "effect/Effect";
2830
import * as Fiber from "effect/Fiber";
2931
import * as Layer from "effect/Layer";
@@ -1128,6 +1130,155 @@ describe("OrchestratorMcpService provider resolution", () => {
11281130
turnItems: [],
11291131
} as unknown as OrchestrationV2ThreadProjection;
11301132

1133+
it.effect.each([
1134+
"parent completion",
1135+
"nested child completion",
1136+
"child run completion",
1137+
"child question",
1138+
"child approval",
1139+
] as const)("a task wait still returns on %s", (wake) =>
1140+
Effect.gen(function* () {
1141+
const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>();
1142+
const subscribed = yield* Deferred.make<void>();
1143+
const nestedId = NodeId.make("node:mcp-wait-nested");
1144+
const childRunId = RunId.make("run:mcp-wait-child");
1145+
const childRootId = NodeId.make("node:mcp-wait-child-root");
1146+
const eventThreadId = wake === "parent completion" ? parentThreadId : childThreadId;
1147+
const eventType =
1148+
wake === "child run completion"
1149+
? "run.updated"
1150+
: wake === "child question" || wake === "child approval"
1151+
? "runtime-request.updated"
1152+
: "subagent.updated";
1153+
const terminal = wake.endsWith("completion");
1154+
let ready = false;
1155+
const task = () => ({
1156+
id: taskId,
1157+
threadId: parentThreadId,
1158+
runId: parentRunId,
1159+
parentNodeId,
1160+
origin: "app_owned",
1161+
providerInstanceId: codexInstanceId,
1162+
driver: "codex",
1163+
childThreadId,
1164+
model: "gpt-5.4",
1165+
status: ready && terminal ? "completed" : "running",
1166+
result: ready && wake === "parent completion" ? "done" : null,
1167+
});
1168+
const child = () =>
1169+
({
1170+
...childProjection,
1171+
runs: [
1172+
{
1173+
id: childRunId,
1174+
ordinal: 1,
1175+
status:
1176+
wake === "nested child completion" || (ready && terminal) ? "completed" : "running",
1177+
startedAt: DateTime.makeUnsafe(0),
1178+
completedAt: ready && terminal ? DateTime.makeUnsafe(1) : null,
1179+
},
1180+
],
1181+
subagents:
1182+
wake === "nested child completion"
1183+
? [{ id: nestedId, status: ready ? "completed" : "running" }]
1184+
: [],
1185+
nodes: [{ id: childRootId, runId: childRunId }],
1186+
runtimeRequests:
1187+
ready && !terminal
1188+
? [
1189+
{
1190+
id: "request:mcp-wait-answer",
1191+
nodeId: childRootId,
1192+
kind: wake === "child question" ? "user_input" : "command",
1193+
status: "pending",
1194+
responseCapability: { type: "live" },
1195+
createdAt: DateTime.makeUnsafe(0),
1196+
},
1197+
]
1198+
: [],
1199+
}) as unknown as OrchestrationV2ThreadProjection;
1200+
const dependencies = Layer.mergeAll(
1201+
NodeServices.layer,
1202+
Layer.mock(ThreadManagementService.ThreadManagementService)({
1203+
hasReceipt: () => Effect.succeed(false),
1204+
assertProviderInstanceEnabled: () => Effect.void,
1205+
isProviderInstanceEnabledForProject: () => Effect.succeed(true),
1206+
getThreadRecords: (threadId) =>
1207+
Effect.sync(() => (threadId === parentThreadId ? parentProjection([task()]) : child())),
1208+
getThreadEventSequence: () => Effect.succeed(0),
1209+
delegatedTaskResultPending: () => Effect.succeed(false),
1210+
streamStoredEventsFrom: (input) =>
1211+
input?.threadId === eventThreadId && input.eventType === eventType
1212+
? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
1213+
Stream.drain,
1214+
Stream.concat(Stream.fromQueue(events)),
1215+
)
1216+
: Stream.never,
1217+
dispatch: () =>
1218+
Effect.succeed({
1219+
sequence: 1,
1220+
storedEvents: [{ event: { type: "subagent.updated", payload: task() } }],
1221+
} as never),
1222+
}),
1223+
ServerSettings.layerTest(),
1224+
providerRegistryLayer([
1225+
providerSnapshot({
1226+
instanceId: codexInstanceId,
1227+
driver: ProviderDriverKind.make("codex"),
1228+
model: "gpt-5.4",
1229+
}),
1230+
]),
1231+
adapterRegistryLayer([codexInstanceId]),
1232+
Layer.mock(ProjectService.ProjectService)({}),
1233+
Layer.mock(SecretRequests.SecretRequests)({}),
1234+
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
1235+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
1236+
);
1237+
yield* Effect.gen(function* () {
1238+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
1239+
const waiting = yield* service
1240+
.delegateTask(scope, { task: "Review the change.", mode: "wait" })
1241+
.pipe(Effect.forkChild);
1242+
yield* Deferred.await(subscribed);
1243+
ready = true;
1244+
if (wake === "parent completion") {
1245+
yield* Queue.offer(events, {
1246+
sequence: 2,
1247+
event: {
1248+
type: "subagent.updated",
1249+
threadId: parentThreadId,
1250+
payload: { ...task(), id: nestedId },
1251+
},
1252+
} as unknown as OrchestrationV2StoredEvent);
1253+
}
1254+
yield* Queue.offer(events, {
1255+
sequence: 3,
1256+
event: {
1257+
type: eventType,
1258+
threadId: eventThreadId,
1259+
payload: { id: wake === "parent completion" ? taskId : nestedId },
1260+
},
1261+
} as unknown as OrchestrationV2StoredEvent);
1262+
const result = yield* Fiber.join(waiting);
1263+
assert.isFalse(result.waitTimedOut);
1264+
if (terminal) {
1265+
assert.equal(result.status, "completed");
1266+
if (wake === "parent completion") assert.equal(result.summary, "done");
1267+
} else {
1268+
assert.equal(result.status, "running");
1269+
if (wake === "child question") assert.isTrue(result.hasPendingQuestion);
1270+
else {
1271+
assert.deepEqual(result.pendingApproval, {
1272+
requestId: "request:mcp-wait-answer",
1273+
kind: "command",
1274+
runId: childRunId,
1275+
});
1276+
}
1277+
}
1278+
}).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(dependencies))));
1279+
}),
1280+
);
1281+
11311282
it.effect(
11321283
"advertises orchestration capability from registered adapters rather than a driver allowlist",
11331284
() =>

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,12 +1791,23 @@ const make = Effect.gen(function* () {
17911791
: TASK_WAKE_EVENTS;
17921792
return yield* Stream.mergeAll(
17931793
wakeEvents.map(({ thread, eventType }) =>
1794-
threadManagement.streamStoredEventsFrom({
1795-
threadId:
1796-
thread === "parent" || childThreadId === null ? scope.thread.threadId : childThreadId,
1797-
afterSequence,
1798-
eventType,
1799-
}),
1794+
threadManagement
1795+
.streamStoredEventsFrom({
1796+
threadId:
1797+
thread === "parent" || childThreadId === null
1798+
? scope.thread.threadId
1799+
: childThreadId,
1800+
afterSequence,
1801+
eventType,
1802+
})
1803+
.pipe(
1804+
Stream.filter(
1805+
({ event }) =>
1806+
thread === "child" ||
1807+
event.type !== "subagent.updated" ||
1808+
event.payload.id === taskId,
1809+
),
1810+
),
18001811
),
18011812
{ concurrency: "unbounded" },
18021813
).pipe(

0 commit comments

Comments
 (0)