GeezerVison is showing github.com — fix(mcp): 等待线程时识别待填写的秘密输入 · pingdotgg/t3code@5dafde2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -245,6 +245,7 @@ it("taskStatus returns task.providerInstanceId rather than the driver kind", asy | 245 | 245 | ], | 246 | 246 | visibleTurnItems: [], | 247 | 247 | runtimeRequests: [], | | 248 | + turnItems: [], | 248 | 249 | messages: [], | 249 | 250 | contextTransfers: [ | 250 | 251 | { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.test.ts +46-2Lines changed: 46 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -112,6 +112,7 @@ describe("OrchestratorMcpService", () => { | 112 | 112 | subagents: [], | 113 | 113 | providerThreads: [], | 114 | 114 | runtimeRequests: [], | | 115 | + turnItems: [], | 115 | 116 | } as unknown as OrchestrationV2ThreadProjection; | 116 | 117 | let hasNestedWork = true; | 117 | 118 | const layerDependencies = Layer.mergeAll( | @@ -300,6 +301,9 @@ describe("OrchestratorMcpService", () => { | 300 | 301 | const taskId = NodeId.make("node:mcp-next-task"); | 301 | 302 | let finished = false; | 302 | 303 | let pendingRequest = false; | | 304 | + let pendingSecret = false; | | 305 | + let secretCompleted = false; | | 306 | + let secretOwner: "task" | "finished" | "unrelated" = "task"; | 303 | 307 | const parentProjection = () => | 304 | 308 | ({ | 305 | 309 | thread: { id: parentThreadId }, | @@ -328,14 +332,28 @@ describe("OrchestratorMcpService", () => { | 328 | 332 | const childProjection = () => | 329 | 333 | ({ | 330 | 334 | 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 | + ], | 332 | 340 | contextTransfers: [], | 333 | 341 | messages: [], | 334 | 342 | subagents: [], | 335 | 343 | providerThreads: [], | 336 | 344 | runtimeRequests: pendingRequest ? [{ status: "pending" }] : [], | 337 | 345 | 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 | + : [], | 339 | 357 | }) as unknown as OrchestrationV2ThreadProjection; | 340 | 358 | const layerDependencies = Layer.mergeAll( | 341 | 359 | NodeServices.layer, | @@ -392,9 +410,29 @@ describe("OrchestratorMcpService", () => { | 392 | 410 | assert.notProperty(asking, "next"); | 393 | 411 | pendingRequest = false; | 394 | 412 |
| | 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 | + | 395 | 431 | finished = true; | | 432 | + pendingSecret = true; | 396 | 433 | const completed = yield* service.taskStatus(scope, taskId); | 397 | 434 | assert.equal(completed.status, "completed"); | | 435 | + assert.isFalse(completed.hasPendingQuestion); | 398 | 436 | assert.notProperty(completed, "next"); | 399 | 437 | const waited = yield* service.waitForThread(scope, { threadId: childThreadId }); | 400 | 438 | assert.equal(waited.status, "completed"); | @@ -434,6 +472,7 @@ describe("OrchestratorMcpService", () => { | 434 | 472 | subagents: [], | 435 | 473 | providerThreads: [], | 436 | 474 | runtimeRequests: [], | | 475 | + turnItems: [], | 437 | 476 | } as unknown as OrchestrationV2ThreadProjection; | 438 | 477 | const layerDependencies = Layer.mergeAll( | 439 | 478 | NodeServices.layer, | @@ -621,6 +660,7 @@ describe("OrchestratorMcpService", () => { | 621 | 660 | subagents: [], | 622 | 661 | providerThreads: [], | 623 | 662 | runtimeRequests: [], | | 663 | + turnItems: [], | 624 | 664 | } as unknown as OrchestrationV2ThreadProjection; | 625 | 665 | const layerDependencies = Layer.mergeAll( | 626 | 666 | NodeServices.layer, | @@ -701,6 +741,7 @@ describe("OrchestratorMcpService", () => { | 701 | 741 | subagents: [], | 702 | 742 | providerThreads: [], | 703 | 743 | runtimeRequests: [], | | 744 | + turnItems: [], | 704 | 745 | } as unknown as OrchestrationV2ThreadProjection; | 705 | 746 | const layerDependencies = Layer.mergeAll( | 706 | 747 | NodeServices.layer, | @@ -787,6 +828,7 @@ describe("OrchestratorMcpService", () => { | 787 | 828 | subagents: [], | 788 | 829 | providerThreads: [], | 789 | 830 | runtimeRequests: [], | | 831 | + turnItems: [], | 790 | 832 | } as unknown as OrchestrationV2ThreadProjection; | 791 | 833 | const layerDependencies = Layer.mergeAll( | 792 | 834 | NodeServices.layer, | @@ -884,6 +926,7 @@ describe("OrchestratorMcpService", () => { | 884 | 926 | ], | 885 | 927 | providerThreads: [], | 886 | 928 | runtimeRequests: [], | | 929 | + turnItems: [], | 887 | 930 | }, | 888 | 931 | ], | 889 | 932 | [ | @@ -896,6 +939,7 @@ describe("OrchestratorMcpService", () => { | 896 | 939 | subagents: [], | 897 | 940 | providerThreads: [], | 898 | 941 | runtimeRequests: [], | | 942 | + turnItems: [], | 899 | 943 | }, | 900 | 944 | ], | 901 | 945 | ]) as unknown as ReadonlyMap<ThreadId, OrchestrationV2ThreadProjection>; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.ts +31-11Lines changed: 31 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -502,12 +502,12 @@ function endsTaskWait(result: OrchestratorMcpDelegateTaskResult): boolean { | 502 | 502 |
| 503 | 503 | const isApprovalRequestKind = Schema.is(ProviderRequestKind); | 504 | 504 |
| 505 | | -function pendingTaskApproval( | 506 | | - child: Pick<OrchestrationV2ThreadProjection, "runs" | "nodes" | "runtimeRequests">, | | 505 | +function activeTaskRunIds( | | 506 | + child: Pick<OrchestrationV2ThreadProjection, "runs">, | 507 | 507 | taskId: NodeId, | 508 | 508 | originalRun: OrchestrationV2Run | undefined, | 509 | 509 | ) { | 510 | | - const taskRunIds = new Set( | | 510 | + return new Set( | 511 | 511 | child.runs | 512 | 512 | .filter( | 513 | 513 | (run) => | @@ -518,6 +518,12 @@ function pendingTaskApproval( | 518 | 518 | ) | 519 | 519 | .map((run) => run.id), | 520 | 520 | ); | | 521 | +} | | 522 | + | | 523 | +function pendingTaskApproval( | | 524 | + child: Pick<OrchestrationV2ThreadProjection, "nodes" | "runtimeRequests">, | | 525 | + taskRunIds: ReadonlySet<RunId>, | | 526 | +) { | 521 | 527 | let approval: OrchestratorMcpDelegateTaskResult["pendingApproval"] = null; | 522 | 528 | let oldestCreatedAt = Infinity; | 523 | 529 | for (const request of child.runtimeRequests) { | @@ -1504,8 +1510,13 @@ const make = Effect.gen(function* () { | 1504 | 1510 | "providerThreads", | 1505 | 1511 | "runtimeRequests", | 1506 | 1512 | "nodes", | | 1513 | + "turnItems", | 1507 | 1514 | ], | 1508 | | - { messageRoles: ["user"], omitSettledLeafNodes: true }, | | 1515 | + { | | 1516 | + messageRoles: ["user"], | | 1517 | + omitSettledLeafNodes: true, | | 1518 | + turnItemTypes: ["secret_request"], | | 1519 | + }, | 1509 | 1520 | ) | 1510 | 1521 | .pipe(Effect.mapError(threadManagementFailure)); | 1511 | 1522 | const childRun = delegatedTaskRun(childControls, task); | @@ -1598,6 +1609,7 @@ const make = Effect.gen(function* () { | 1598 | 1609 | instanceId: task.providerInstanceId, | 1599 | 1610 | model: task.model, | 1600 | 1611 | }; | | 1612 | + const taskRunIds = activeTaskRunIds(childControls, task.id, childRun); | 1601 | 1613 | const result = { | 1602 | 1614 | taskId: task.id, | 1603 | 1615 | childThreadId: task.childThreadId, | @@ -1622,16 +1634,24 @@ const make = Effect.gen(function* () { | 1622 | 1634 | : latestTerminalResultContextTransferId, | 1623 | 1635 | hasPendingQuestion: | 1624 | 1636 | !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 | + )), | 1631 | 1651 | pendingApproval: | 1632 | 1652 | isTerminalTaskStatus(status) || task.completionDelivery?.state === "disposed" | 1633 | 1653 | ? null | 1634 | | - : pendingTaskApproval(childControls, task.id, childRun), | | 1654 | + : pendingTaskApproval(childControls, taskRunIds), | 1635 | 1655 | waitTimedOut, | 1636 | 1656 | } satisfies OrchestratorMcpDelegateTaskResult; | 1637 | 1657 | const response: OrchestratorMcpDelegateTaskResult = | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadManagementService.test.ts +98-24Lines changed: 98 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -252,6 +252,7 @@ it.effect.each([ | 252 | 252 | : [{ id: requestRunId, ordinal: scenario.requestOrdinal, status: "running" }]), | 253 | 253 | ], | 254 | 254 | nodes: [{ id: "node:request", runId: requestRunId }], | | 255 | + turnItems: [], | 255 | 256 | runtimeRequests: [ | 256 | 257 | { | 257 | 258 | id: "request:input", | @@ -296,7 +297,64 @@ it.effect.each([ | 296 | 297 | }), | 297 | 298 | ); | 298 | 299 |
| 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)( | 300 | 358 | "waitForThread wakes on a new %s while terminal-only callers keep waiting", | 301 | 359 | (kind) => | 302 | 360 | Effect.gen(function* () { | @@ -307,23 +365,27 @@ it.effect.each(["command", "user_input"] as const)( | 307 | 365 | const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>(); | 308 | 366 | const now = yield* DateTime.now; | 309 | 367 | 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"; | 310 | 371 | const layerTest = ThreadManagementService.layer.pipe( | 311 | 372 | Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})), | 312 | 373 | Layer.provide( | 313 | 374 | Layer.mock(Orchestrator.OrchestratorV2)({ | 314 | 375 | getThreadEventSequence: () => Effect.succeed(7), | 315 | 376 | 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 | + }), | 325 | 387 | streamStoredEventsFrom: (input) => | 326 | | - input?.eventType === "runtime-request.updated" | | 388 | + input?.eventType === eventType | 327 | 389 | ? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe( | 328 | 390 | Stream.drain, | 329 | 391 | Stream.concat(Stream.fromQueue(events)), | @@ -341,25 +403,36 @@ it.effect.each(["command", "user_input"] as const)( | 341 | 403 | .waitForThread({ projectId, threadId, runId, timeoutMs: 600_000 }) | 342 | 404 | .pipe(Effect.forkChild); | 343 | 405 | 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 | + } | 354 | 426 | 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] }, | 357 | 429 | } as unknown as OrchestrationV2StoredEvent); | 358 | 430 | expect(yield* Fiber.join(fiber)).toMatchObject({ | 359 | 431 | timedOut: false, | 360 | | - hasPendingQuestion: kind === "user_input", | | 432 | + hasPendingQuestion: kind !== "command", | 361 | 433 | pendingApproval: kind === "command" ? { requestId: "request:approval", kind, runId } : null, | 362 | 434 | }); | | 435 | + expect(reads).toBe(2); | 363 | 436 | const terminalWait = yield* service | 364 | 437 | .waitForThread({ projectId, threadId, runId, timeoutMs: 60_000, terminalOnly: true }) | 365 | 438 | .pipe(Effect.forkChild); | @@ -410,6 +483,7 @@ it.effect("waitForThread follows a restart continuation instead of the cut run", | 410 | 483 | runs, | 411 | 484 | nodes: [], | 412 | 485 | runtimeRequests: [], | | 486 | + turnItems: [], | 413 | 487 | }) as unknown as OrchestrationV2ThreadProjection, | 414 | 488 | ), | 415 | 489 | }), | |
|