|
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, |
@@ -1070,6 +1071,146 @@ const seedRestartCancelledChild = (input: { |
1070 | 1071 | it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))( |
1071 | 1072 | "subagent follow-ups", |
1072 | 1073 | (it) => { |
| 1074 | + it.effect("keeps a checkpoint completion committed between finalize's read and write", () => |
| 1075 | + Effect.gen(function* () { |
| 1076 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1077 | + const sink = yield* EventSink.EventSinkV2; |
| 1078 | + const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 1079 | + const now = yield* DateTime.now; |
| 1080 | + const threadId = ThreadId.make("thread:completion-capture-race"); |
| 1081 | + const projectId = ProjectId.make("project:completion-capture-race"); |
| 1082 | + const runId = RunId.make("run:completion-capture-race"); |
| 1083 | + const rootNodeId = NodeId.make("node:completion-capture-race"); |
| 1084 | + const checkpointId = CheckpointId.make("checkpoint:completion-capture-race"); |
| 1085 | + yield* seedParentWithTerminalTask({ |
| 1086 | + threadId, |
| 1087 | + projectId, |
| 1088 | + runId, |
| 1089 | + rootNodeId, |
| 1090 | + taskId: NodeId.make("node:completion-capture-seed"), |
| 1091 | + deliveryState: "acknowledged", |
| 1092 | + now, |
| 1093 | + }); |
| 1094 | + const parent = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; |
| 1095 | + const waiting = { ...parent, status: "waiting" as const }; |
| 1096 | + yield* sink.write({ |
| 1097 | + events: [ |
| 1098 | + { |
| 1099 | + id: EventId.make("event:completion-capture-waiting"), |
| 1100 | + type: "run.updated", |
| 1101 | + threadId, |
| 1102 | + runId, |
| 1103 | + occurredAt: now, |
| 1104 | + payload: waiting, |
| 1105 | + }, |
| 1106 | + ], |
| 1107 | + }); |
| 1108 | + const child = yield* seedRestartCancelledChild({ |
| 1109 | + parentThreadId: threadId, |
| 1110 | + projectId, |
| 1111 | + parentRunId: runId, |
| 1112 | + rootNodeId, |
| 1113 | + name: "completion-capture-child", |
| 1114 | + completionWake: "always", |
| 1115 | + continuationPending: false, |
| 1116 | + runStatus: "completed", |
| 1117 | + now, |
| 1118 | + }); |
| 1119 | + const write = sink.writeWithEffects; |
| 1120 | + let captured = false; |
| 1121 | + const committedParentRuns: Array<OrchestrationV2Run> = []; |
| 1122 | + yield* Effect.acquireUseRelease( |
| 1123 | + Effect.sync(() => |
| 1124 | + vi.spyOn(sink, "writeWithEffects").mockImplementation((input) => |
| 1125 | + Effect.gen(function* () { |
| 1126 | + if ( |
| 1127 | + !captured && |
| 1128 | + input.events.some( |
| 1129 | + (event) => |
| 1130 | + event.type === "run.updated" && |
| 1131 | + event.payload.id === runId && |
| 1132 | + event.payload.delegatedCompletion?.delivery != null, |
| 1133 | + ) |
| 1134 | + ) { |
| 1135 | + captured = true; |
| 1136 | + // Commit capture after finalize planned its stale waiting snapshot. |
| 1137 | + const { delegatedCompletion: _cohort, ...captureRun } = waiting; |
| 1138 | + yield* sink.commitCommand({ |
| 1139 | + commandId: CommandId.make("command:completion-capture-race"), |
| 1140 | + threadId, |
| 1141 | + commandType: "checkpoint.capture", |
| 1142 | + acceptedAt: now, |
| 1143 | + effects: [], |
| 1144 | + events: [ |
| 1145 | + { |
| 1146 | + id: EventId.make("event:completion-capture-completed"), |
| 1147 | + type: "run.updated", |
| 1148 | + threadId, |
| 1149 | + runId, |
| 1150 | + occurredAt: now, |
| 1151 | + payload: { |
| 1152 | + ...captureRun, |
| 1153 | + status: "completed", |
| 1154 | + completedAt: now, |
| 1155 | + checkpointId, |
| 1156 | + }, |
| 1157 | + }, |
| 1158 | + ], |
| 1159 | + }); |
| 1160 | + } |
| 1161 | + const stored = yield* write(input); |
| 1162 | + for (const { event } of stored) { |
| 1163 | + if (event.type === "run.updated" && event.payload.id === runId) { |
| 1164 | + committedParentRuns.push(event.payload); |
| 1165 | + } |
| 1166 | + } |
| 1167 | + return stored; |
| 1168 | + }), |
| 1169 | + ), |
| 1170 | + ), |
| 1171 | + () => orchestrator.recoverDelegatedTasks, |
| 1172 | + (spy) => Effect.sync(() => spy.mockRestore()), |
| 1173 | + ); |
| 1174 | + assert.isTrue(captured); |
| 1175 | + assert.equal(committedParentRuns.length, 1); |
| 1176 | + const committedParentRun = committedParentRuns[0]; |
| 1177 | + assert.equal(committedParentRun?.status, "completed"); |
| 1178 | + assert.deepEqual(committedParentRun?.completedAt, now); |
| 1179 | + assert.equal(committedParentRun?.checkpointId, checkpointId); |
| 1180 | + assert.deepEqual(committedParentRun?.delegatedCompletion?.delivery?.taskIds, [ |
| 1181 | + child.taskId, |
| 1182 | + ]); |
| 1183 | + const finished = yield* orchestrator.getThreadProjection(threadId); |
| 1184 | + const completed = finished.runs.find((run) => run.id === runId)!; |
| 1185 | + assert.equal(completed.status, "completed"); |
| 1186 | + assert.deepEqual(completed.completedAt, now); |
| 1187 | + assert.equal(completed.checkpointId, checkpointId); |
| 1188 | + assert.deepEqual(completed.delegatedCompletion?.delivery?.taskIds, [child.taskId]); |
| 1189 | + const wake = yield* requests.take; |
| 1190 | + assert.equal(wake.delegatedCompletion?.parentRunId, runId); |
| 1191 | + const delivery = completed.delegatedCompletion!.delivery!; |
| 1192 | + yield* orchestrator.dispatch({ |
| 1193 | + type: "message.dispatch", |
| 1194 | + createdBy: "agent", |
| 1195 | + creationSource: "server", |
| 1196 | + commandId: CommandId.make("command:completion-capture-delivery"), |
| 1197 | + threadId, |
| 1198 | + messageId: delivery.messageId, |
| 1199 | + text: "Child finished", |
| 1200 | + attachments: [], |
| 1201 | + delegatedCompletion: { |
| 1202 | + parentRunId: runId, |
| 1203 | + generation: delivery.generation, |
| 1204 | + taskIds: delivery.taskIds, |
| 1205 | + }, |
| 1206 | + dispatchMode: { type: "queue_after_active" }, |
| 1207 | + }); |
| 1208 | + const delivered = yield* orchestrator.getThreadProjection(threadId); |
| 1209 | + const deliveryRun = delivered.runs.find((run) => run.userMessageId === delivery.messageId); |
| 1210 | + assert.isDefined(deliveryRun); |
| 1211 | + assert.notEqual(deliveryRun?.status, "queued"); |
| 1212 | + }), |
| 1213 | + ); |
1073 | 1214 | it.effect("pauses queued follow-up cards and resumes them", () => |
1074 | 1215 | Effect.gen(function* () { |
1075 | 1216 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
|
0 commit comments