Skip to content

Commit 5dafde2

Browse files
committed
fix(mcp): 等待线程时识别待填写的秘密输入
- 将秘密输入卡片及其更新事件纳入等待结果,保持仅等待终态的内部调用语义。 - 任务状态只报告所属活跃运行的待填输入,不返回卡片内容或秘密值。 - 忽略普通流式消息更新,限定读取秘密输入记录。 - 复现修复前四项失败;修复后 69 项测试及受影响范围检查通过。
1 parent 8965642 commit 5dafde2

5 files changed

Lines changed: 210 additions & 42 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ it("taskStatus returns task.providerInstanceId rather than the driver kind", asy
245245
],
246246
visibleTurnItems: [],
247247
runtimeRequests: [],
248+
turnItems: [],
248249
messages: [],
249250
contextTransfers: [
250251
{

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ describe("OrchestratorMcpService", () => {
112112
subagents: [],
113113
providerThreads: [],
114114
runtimeRequests: [],
115+
turnItems: [],
115116
} as unknown as OrchestrationV2ThreadProjection;
116117
let hasNestedWork = true;
117118
const layerDependencies = Layer.mergeAll(
@@ -300,6 +301,9 @@ describe("OrchestratorMcpService", () => {
300301
const taskId = NodeId.make("node:mcp-next-task");
301302
let finished = false;
302303
let pendingRequest = false;
304+
let pendingSecret = false;
305+
let secretCompleted = false;
306+
let secretOwner: "task" | "finished" | "unrelated" = "task";
303307
const parentProjection = () =>
304308
({
305309
thread: { id: parentThreadId },
@@ -328,14 +332,28 @@ describe("OrchestratorMcpService", () => {
328332
const childProjection = () =>
329333
({
330334
thread: { id: childThreadId, title: "Child task" },
331-
runs: [childRun()],
335+
runs: [
336+
childRun(),
337+
{ id: "run:finished", ordinal: 0, status: "completed" },
338+
{ id: "run:unrelated", ordinal: 2, status: "running" },
339+
],
332340
contextTransfers: [],
333341
messages: [],
334342
subagents: [],
335343
providerThreads: [],
336344
runtimeRequests: pendingRequest ? [{ status: "pending" }] : [],
337345
nodes: [],
338-
turnItems: [],
346+
turnItems: pendingSecret
347+
? [
348+
{
349+
type: "secret_request",
350+
runId: secretOwner === "task" ? childRun().id : `run:${secretOwner}`,
351+
nodeId: "node:secret",
352+
status: secretCompleted ? "completed" : "waiting",
353+
label: "Private credential request",
354+
},
355+
]
356+
: [],
339357
}) as unknown as OrchestrationV2ThreadProjection;
340358
const layerDependencies = Layer.mergeAll(
341359
NodeServices.layer,
@@ -392,9 +410,29 @@ describe("OrchestratorMcpService", () => {
392410
assert.notProperty(asking, "next");
393411
pendingRequest = false;
394412

413+
pendingSecret = true;
414+
const secret = yield* service.taskStatus(scope, taskId);
415+
assert.isTrue(secret.hasPendingQuestion);
416+
assert.notProperty(secret, "next");
417+
assert.notInclude(JSON.stringify(secret), "Private credential request");
418+
secretCompleted = true;
419+
assert.isFalse((yield* service.taskStatus(scope, taskId)).hasPendingQuestion);
420+
secretCompleted = false;
421+
for (const owner of ["finished", "unrelated"] as const) {
422+
secretOwner = owner;
423+
const unrelated = yield* service.taskStatus(scope, taskId);
424+
assert.isFalse(unrelated.hasPendingQuestion);
425+
assert.equal(unrelated.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT);
426+
}
427+
secretOwner = "task";
428+
pendingSecret = false;
429+
assert.isFalse((yield* service.taskStatus(scope, taskId)).hasPendingQuestion);
430+
395431
finished = true;
432+
pendingSecret = true;
396433
const completed = yield* service.taskStatus(scope, taskId);
397434
assert.equal(completed.status, "completed");
435+
assert.isFalse(completed.hasPendingQuestion);
398436
assert.notProperty(completed, "next");
399437
const waited = yield* service.waitForThread(scope, { threadId: childThreadId });
400438
assert.equal(waited.status, "completed");
@@ -434,6 +472,7 @@ describe("OrchestratorMcpService", () => {
434472
subagents: [],
435473
providerThreads: [],
436474
runtimeRequests: [],
475+
turnItems: [],
437476
} as unknown as OrchestrationV2ThreadProjection;
438477
const layerDependencies = Layer.mergeAll(
439478
NodeServices.layer,
@@ -621,6 +660,7 @@ describe("OrchestratorMcpService", () => {
621660
subagents: [],
622661
providerThreads: [],
623662
runtimeRequests: [],
663+
turnItems: [],
624664
} as unknown as OrchestrationV2ThreadProjection;
625665
const layerDependencies = Layer.mergeAll(
626666
NodeServices.layer,
@@ -701,6 +741,7 @@ describe("OrchestratorMcpService", () => {
701741
subagents: [],
702742
providerThreads: [],
703743
runtimeRequests: [],
744+
turnItems: [],
704745
} as unknown as OrchestrationV2ThreadProjection;
705746
const layerDependencies = Layer.mergeAll(
706747
NodeServices.layer,
@@ -787,6 +828,7 @@ describe("OrchestratorMcpService", () => {
787828
subagents: [],
788829
providerThreads: [],
789830
runtimeRequests: [],
831+
turnItems: [],
790832
} as unknown as OrchestrationV2ThreadProjection;
791833
const layerDependencies = Layer.mergeAll(
792834
NodeServices.layer,
@@ -884,6 +926,7 @@ describe("OrchestratorMcpService", () => {
884926
],
885927
providerThreads: [],
886928
runtimeRequests: [],
929+
turnItems: [],
887930
},
888931
],
889932
[
@@ -896,6 +939,7 @@ describe("OrchestratorMcpService", () => {
896939
subagents: [],
897940
providerThreads: [],
898941
runtimeRequests: [],
942+
turnItems: [],
899943
},
900944
],
901945
]) as unknown as ReadonlyMap<ThreadId, OrchestrationV2ThreadProjection>;

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,12 @@ function endsTaskWait(result: OrchestratorMcpDelegateTaskResult): boolean {
502502

503503
const isApprovalRequestKind = Schema.is(ProviderRequestKind);
504504

505-
function pendingTaskApproval(
506-
child: Pick<OrchestrationV2ThreadProjection, "runs" | "nodes" | "runtimeRequests">,
505+
function activeTaskRunIds(
506+
child: Pick<OrchestrationV2ThreadProjection, "runs">,
507507
taskId: NodeId,
508508
originalRun: OrchestrationV2Run | undefined,
509509
) {
510-
const taskRunIds = new Set(
510+
return new Set(
511511
child.runs
512512
.filter(
513513
(run) =>
@@ -518,6 +518,12 @@ function pendingTaskApproval(
518518
)
519519
.map((run) => run.id),
520520
);
521+
}
522+
523+
function pendingTaskApproval(
524+
child: Pick<OrchestrationV2ThreadProjection, "nodes" | "runtimeRequests">,
525+
taskRunIds: ReadonlySet<RunId>,
526+
) {
521527
let approval: OrchestratorMcpDelegateTaskResult["pendingApproval"] = null;
522528
let oldestCreatedAt = Infinity;
523529
for (const request of child.runtimeRequests) {
@@ -1504,8 +1510,13 @@ const make = Effect.gen(function* () {
15041510
"providerThreads",
15051511
"runtimeRequests",
15061512
"nodes",
1513+
"turnItems",
15071514
],
1508-
{ messageRoles: ["user"], omitSettledLeafNodes: true },
1515+
{
1516+
messageRoles: ["user"],
1517+
omitSettledLeafNodes: true,
1518+
turnItemTypes: ["secret_request"],
1519+
},
15091520
)
15101521
.pipe(Effect.mapError(threadManagementFailure));
15111522
const childRun = delegatedTaskRun(childControls, task);
@@ -1598,6 +1609,7 @@ const make = Effect.gen(function* () {
15981609
instanceId: task.providerInstanceId,
15991610
model: task.model,
16001611
};
1612+
const taskRunIds = activeTaskRunIds(childControls, task.id, childRun);
16011613
const result = {
16021614
taskId: task.id,
16031615
childThreadId: task.childThreadId,
@@ -1622,16 +1634,24 @@ const make = Effect.gen(function* () {
16221634
: latestTerminalResultContextTransferId,
16231635
hasPendingQuestion:
16241636
!isTerminalTaskStatus(status) &&
1625-
childControls.runtimeRequests.some(
1626-
(request) =>
1627-
request.kind === "user_input" &&
1628-
request.status === "pending" &&
1629-
request.responseCapability.type === "live",
1630-
),
1637+
(childControls.turnItems.some(
1638+
(item) =>
1639+
item.type === "secret_request" &&
1640+
item.status === "waiting" &&
1641+
item.nodeId !== null &&
1642+
item.runId !== null &&
1643+
taskRunIds.has(item.runId),
1644+
) ||
1645+
childControls.runtimeRequests.some(
1646+
(request) =>
1647+
request.kind === "user_input" &&
1648+
request.status === "pending" &&
1649+
request.responseCapability.type === "live",
1650+
)),
16311651
pendingApproval:
16321652
isTerminalTaskStatus(status) || task.completionDelivery?.state === "disposed"
16331653
? null
1634-
: pendingTaskApproval(childControls, task.id, childRun),
1654+
: pendingTaskApproval(childControls, taskRunIds),
16351655
waitTimedOut,
16361656
} satisfies OrchestratorMcpDelegateTaskResult;
16371657
const response: OrchestratorMcpDelegateTaskResult =

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

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ it.effect.each([
252252
: [{ id: requestRunId, ordinal: scenario.requestOrdinal, status: "running" }]),
253253
],
254254
nodes: [{ id: "node:request", runId: requestRunId }],
255+
turnItems: [],
255256
runtimeRequests: [
256257
{
257258
id: "request:input",
@@ -296,7 +297,64 @@ it.effect.each([
296297
}),
297298
);
298299

299-
it.effect.each(["command", "user_input"] as const)(
300+
it.effect.each([
301+
{ selected: "running", owner: "running", ordinal: 1, status: "waiting", blocks: true },
302+
{ selected: "queued", owner: "running", ordinal: 0, status: "waiting", blocks: true },
303+
{ selected: "queued", owner: "running", ordinal: 2, status: "waiting", blocks: true },
304+
{ selected: "running", owner: "running", ordinal: 2, status: "waiting", blocks: false },
305+
{ selected: "running", owner: "completed", ordinal: 0, status: "waiting", blocks: false },
306+
{ selected: "running", owner: "running", ordinal: 1, status: "completed", blocks: false },
307+
{ selected: "completed", owner: "completed", ordinal: 1, status: "waiting", blocks: false },
308+
] as const)("waitForThread scopes secret input to unfinished work: %j", (scenario) =>
309+
Effect.gen(function* () {
310+
const projectId = ProjectId.make("project:wait-secret");
311+
const threadId = ThreadId.make("thread:wait-secret");
312+
const runId = RunId.make("run:wait-secret");
313+
const ownerId = scenario.ordinal === 1 ? runId : RunId.make("run:secret-owner");
314+
const projection = {
315+
thread: { id: threadId, projectId, deletedAt: null },
316+
runs: [
317+
{ id: runId, ordinal: 1, status: scenario.selected },
318+
...(ownerId === runId
319+
? []
320+
: [{ id: ownerId, ordinal: scenario.ordinal, status: scenario.owner }]),
321+
],
322+
nodes: [],
323+
runtimeRequests: [],
324+
turnItems: [
325+
{ type: "secret_request", runId: ownerId, nodeId: "node:secret", status: scenario.status },
326+
],
327+
} as unknown as OrchestrationV2ThreadProjection;
328+
const layerTest = ThreadManagementService.layer.pipe(
329+
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
330+
Layer.provide(
331+
Layer.mock(Orchestrator.OrchestratorV2)({
332+
getThreadEventSequence: () => Effect.succeed(0),
333+
getThreadRecords: () => Effect.succeed(projection),
334+
restartContinuationPending: () => Effect.succeed(false),
335+
streamStoredEventsFrom: () => Stream.never,
336+
}),
337+
),
338+
Layer.provide(ServerSettings.layerTest()),
339+
Layer.provide(layerReceiptsTest),
340+
);
341+
const service = yield* ThreadManagementService.ThreadManagementService.pipe(
342+
Effect.provide(layerTest),
343+
);
344+
const fiber = yield* service
345+
.waitForThread({ projectId, threadId, runId, timeoutMs: 60_000 })
346+
.pipe(Effect.forkChild);
347+
yield* TestClock.adjust("1 minute");
348+
expect(yield* Fiber.join(fiber)).toMatchObject({
349+
run: { id: runId, status: scenario.selected },
350+
timedOut: !scenario.blocks && scenario.selected !== "completed",
351+
hasPendingQuestion: scenario.blocks,
352+
pendingApproval: null,
353+
});
354+
}),
355+
);
356+
357+
it.effect.each(["command", "user_input", "secret_request"] as const)(
300358
"waitForThread wakes on a new %s while terminal-only callers keep waiting",
301359
(kind) =>
302360
Effect.gen(function* () {
@@ -307,23 +365,27 @@ it.effect.each(["command", "user_input"] as const)(
307365
const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>();
308366
const now = yield* DateTime.now;
309367
let requests: OrchestrationV2ThreadProjection["runtimeRequests"] = [];
368+
let turnItems: OrchestrationV2ThreadProjection["turnItems"] = [];
369+
let reads = 0;
370+
const eventType = kind === "secret_request" ? "turn-item.updated" : "runtime-request.updated";
310371
const layerTest = ThreadManagementService.layer.pipe(
311372
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
312373
Layer.provide(
313374
Layer.mock(Orchestrator.OrchestratorV2)({
314375
getThreadEventSequence: () => Effect.succeed(7),
315376
getThreadRecords: () =>
316-
Effect.sync(
317-
() =>
318-
({
319-
thread: { id: threadId, projectId, deletedAt: null },
320-
runs: [{ id: runId, ordinal: 1, status: "running" }],
321-
nodes: [{ id: "node:request", runId }],
322-
runtimeRequests: requests,
323-
}) as unknown as OrchestrationV2ThreadProjection,
324-
),
377+
Effect.sync(() => {
378+
reads += 1;
379+
return {
380+
thread: { id: threadId, projectId, deletedAt: null },
381+
runs: [{ id: runId, ordinal: 1, status: "running" }],
382+
nodes: [{ id: "node:request", runId }],
383+
runtimeRequests: requests,
384+
turnItems,
385+
} as unknown as OrchestrationV2ThreadProjection;
386+
}),
325387
streamStoredEventsFrom: (input) =>
326-
input?.eventType === "runtime-request.updated"
388+
input?.eventType === eventType
327389
? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
328390
Stream.drain,
329391
Stream.concat(Stream.fromQueue(events)),
@@ -341,25 +403,36 @@ it.effect.each(["command", "user_input"] as const)(
341403
.waitForThread({ projectId, threadId, runId, timeoutMs: 600_000 })
342404
.pipe(Effect.forkChild);
343405
yield* Deferred.await(subscribed);
344-
requests = [
345-
{
346-
id: "request:approval",
347-
nodeId: "node:request",
348-
kind,
349-
status: "pending",
350-
responseCapability: { type: "live" },
351-
createdAt: now,
352-
},
353-
] as unknown as OrchestrationV2ThreadProjection["runtimeRequests"];
406+
if (kind === "secret_request") {
407+
turnItems = [
408+
{ type: kind, runId, nodeId: "node:secret", status: "waiting" },
409+
] as unknown as OrchestrationV2ThreadProjection["turnItems"];
410+
yield* Queue.offer(events, {
411+
sequence: 8,
412+
event: { type: eventType, threadId, payload: { type: "assistant_message" } },
413+
} as unknown as OrchestrationV2StoredEvent);
414+
} else {
415+
requests = [
416+
{
417+
id: "request:approval",
418+
nodeId: "node:request",
419+
kind,
420+
status: "pending",
421+
responseCapability: { type: "live" },
422+
createdAt: now,
423+
},
424+
] as unknown as OrchestrationV2ThreadProjection["runtimeRequests"];
425+
}
354426
yield* Queue.offer(events, {
355-
sequence: 8,
356-
event: { type: "runtime-request.updated", threadId, payload: requests[0] },
427+
sequence: 9,
428+
event: { type: eventType, threadId, payload: turnItems[0] ?? requests[0] },
357429
} as unknown as OrchestrationV2StoredEvent);
358430
expect(yield* Fiber.join(fiber)).toMatchObject({
359431
timedOut: false,
360-
hasPendingQuestion: kind === "user_input",
432+
hasPendingQuestion: kind !== "command",
361433
pendingApproval: kind === "command" ? { requestId: "request:approval", kind, runId } : null,
362434
});
435+
expect(reads).toBe(2);
363436
const terminalWait = yield* service
364437
.waitForThread({ projectId, threadId, runId, timeoutMs: 60_000, terminalOnly: true })
365438
.pipe(Effect.forkChild);
@@ -410,6 +483,7 @@ it.effect("waitForThread follows a restart continuation instead of the cut run",
410483
runs,
411484
nodes: [],
412485
runtimeRequests: [],
486+
turnItems: [],
413487
}) as unknown as OrchestrationV2ThreadProjection,
414488
),
415489
}),

0 commit comments

Comments
 (0)