|
1 | 1 | import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts"; |
2 | 2 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
3 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
4 | | -import { assert, it } from "@effect/vitest"; |
| 4 | +import { assert, it, vi } from "@effect/vitest"; |
5 | 5 | import { |
| 6 | + CheckpointId, |
6 | 7 | CommandId, |
7 | 8 | EventId, |
8 | 9 | MessageId, |
@@ -935,6 +936,146 @@ const seedRestartCancelledChild = (input: { |
935 | 936 | it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))( |
936 | 937 | "subagent follow-ups", |
937 | 938 | (it) => { |
| 939 | + it.effect("keeps a checkpoint completion committed between finalize's read and write", () => |
| 940 | + Effect.gen(function* () { |
| 941 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 942 | + const sink = yield* EventSink.EventSinkV2; |
| 943 | + const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 944 | + const now = yield* DateTime.now; |
| 945 | + const threadId = ThreadId.make("thread:completion-capture-race"); |
| 946 | + const projectId = ProjectId.make("project:completion-capture-race"); |
| 947 | + const runId = RunId.make("run:completion-capture-race"); |
| 948 | + const rootNodeId = NodeId.make("node:completion-capture-race"); |
| 949 | + const checkpointId = CheckpointId.make("checkpoint:completion-capture-race"); |
| 950 | + yield* seedParentWithTerminalTask({ |
| 951 | + threadId, |
| 952 | + projectId, |
| 953 | + runId, |
| 954 | + rootNodeId, |
| 955 | + taskId: NodeId.make("node:completion-capture-seed"), |
| 956 | + deliveryState: "acknowledged", |
| 957 | + now, |
| 958 | + }); |
| 959 | + const parent = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; |
| 960 | + const waiting = { ...parent, status: "waiting" as const }; |
| 961 | + yield* sink.write({ |
| 962 | + events: [ |
| 963 | + { |
| 964 | + id: EventId.make("event:completion-capture-waiting"), |
| 965 | + type: "run.updated", |
| 966 | + threadId, |
| 967 | + runId, |
| 968 | + occurredAt: now, |
| 969 | + payload: waiting, |
| 970 | + }, |
| 971 | + ], |
| 972 | + }); |
| 973 | + const child = yield* seedRestartCancelledChild({ |
| 974 | + parentThreadId: threadId, |
| 975 | + projectId, |
| 976 | + parentRunId: runId, |
| 977 | + rootNodeId, |
| 978 | + name: "completion-capture-child", |
| 979 | + completionWake: "always", |
| 980 | + continuationPending: false, |
| 981 | + runStatus: "completed", |
| 982 | + now, |
| 983 | + }); |
| 984 | + const write = sink.writeWithEffects; |
| 985 | + let captured = false; |
| 986 | + const committedParentRuns: Array<OrchestrationV2Run> = []; |
| 987 | + yield* Effect.acquireUseRelease( |
| 988 | + Effect.sync(() => |
| 989 | + vi.spyOn(sink, "writeWithEffects").mockImplementation((input) => |
| 990 | + Effect.gen(function* () { |
| 991 | + if ( |
| 992 | + !captured && |
| 993 | + input.events.some( |
| 994 | + (event) => |
| 995 | + event.type === "run.updated" && |
| 996 | + event.payload.id === runId && |
| 997 | + event.payload.delegatedCompletion?.delivery != null, |
| 998 | + ) |
| 999 | + ) { |
| 1000 | + captured = true; |
| 1001 | + // Commit capture after finalize planned its stale waiting snapshot. |
| 1002 | + const { delegatedCompletion: _cohort, ...captureRun } = waiting; |
| 1003 | + yield* sink.commitCommand({ |
| 1004 | + commandId: CommandId.make("command:completion-capture-race"), |
| 1005 | + threadId, |
| 1006 | + commandType: "checkpoint.capture", |
| 1007 | + acceptedAt: now, |
| 1008 | + effects: [], |
| 1009 | + events: [ |
| 1010 | + { |
| 1011 | + id: EventId.make("event:completion-capture-completed"), |
| 1012 | + type: "run.updated", |
| 1013 | + threadId, |
| 1014 | + runId, |
| 1015 | + occurredAt: now, |
| 1016 | + payload: { |
| 1017 | + ...captureRun, |
| 1018 | + status: "completed", |
| 1019 | + completedAt: now, |
| 1020 | + checkpointId, |
| 1021 | + }, |
| 1022 | + }, |
| 1023 | + ], |
| 1024 | + }); |
| 1025 | + } |
| 1026 | + const stored = yield* write(input); |
| 1027 | + for (const { event } of stored) { |
| 1028 | + if (event.type === "run.updated" && event.payload.id === runId) { |
| 1029 | + committedParentRuns.push(event.payload); |
| 1030 | + } |
| 1031 | + } |
| 1032 | + return stored; |
| 1033 | + }), |
| 1034 | + ), |
| 1035 | + ), |
| 1036 | + () => orchestrator.recoverDelegatedTasks, |
| 1037 | + (spy) => Effect.sync(() => spy.mockRestore()), |
| 1038 | + ); |
| 1039 | + assert.isTrue(captured); |
| 1040 | + assert.equal(committedParentRuns.length, 1); |
| 1041 | + const committedParentRun = committedParentRuns[0]; |
| 1042 | + assert.equal(committedParentRun?.status, "completed"); |
| 1043 | + assert.deepEqual(committedParentRun?.completedAt, now); |
| 1044 | + assert.equal(committedParentRun?.checkpointId, checkpointId); |
| 1045 | + assert.deepEqual(committedParentRun?.delegatedCompletion?.delivery?.taskIds, [ |
| 1046 | + child.taskId, |
| 1047 | + ]); |
| 1048 | + const finished = yield* orchestrator.getThreadProjection(threadId); |
| 1049 | + const completed = finished.runs.find((run) => run.id === runId)!; |
| 1050 | + assert.equal(completed.status, "completed"); |
| 1051 | + assert.deepEqual(completed.completedAt, now); |
| 1052 | + assert.equal(completed.checkpointId, checkpointId); |
| 1053 | + assert.deepEqual(completed.delegatedCompletion?.delivery?.taskIds, [child.taskId]); |
| 1054 | + const wake = yield* requests.take; |
| 1055 | + assert.equal(wake.delegatedCompletion?.parentRunId, runId); |
| 1056 | + const delivery = completed.delegatedCompletion!.delivery!; |
| 1057 | + yield* orchestrator.dispatch({ |
| 1058 | + type: "message.dispatch", |
| 1059 | + createdBy: "agent", |
| 1060 | + creationSource: "server", |
| 1061 | + commandId: CommandId.make("command:completion-capture-delivery"), |
| 1062 | + threadId, |
| 1063 | + messageId: delivery.messageId, |
| 1064 | + text: "Child finished", |
| 1065 | + attachments: [], |
| 1066 | + delegatedCompletion: { |
| 1067 | + parentRunId: runId, |
| 1068 | + generation: delivery.generation, |
| 1069 | + taskIds: delivery.taskIds, |
| 1070 | + }, |
| 1071 | + dispatchMode: { type: "queue_after_active" }, |
| 1072 | + }); |
| 1073 | + const delivered = yield* orchestrator.getThreadProjection(threadId); |
| 1074 | + const deliveryRun = delivered.runs.find((run) => run.userMessageId === delivery.messageId); |
| 1075 | + assert.isDefined(deliveryRun); |
| 1076 | + assert.notEqual(deliveryRun?.status, "queued"); |
| 1077 | + }), |
| 1078 | + ); |
938 | 1079 | const outcomes = [ |
939 | 1080 | "completed", |
940 | 1081 | "failed", |
|
0 commit comments