GeezerVison is showing github.com — fix(server): preserve settled runs during delegated acknowledgements … · aorwall/t3code@32bf48f
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts +98Lines changed: 98 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -974,6 +974,104 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => { | 974 | 974 | ); | 975 | 975 | }), | 976 | 976 | ); | | 977 | + | | 978 | + it.effect.each([ | | 979 | + { before: "running", after: "failed", capture: false }, | | 980 | + { before: "waiting", after: "completed", capture: true }, | | 981 | + ] as const)( | | 982 | + "keeps a $after run that settled between task_status's acknowledge read and commit", | | 983 | + ({ before, after, capture }) => | | 984 | + Effect.gen(function* () { | | 985 | + const orchestrator = yield* Orchestrator.OrchestratorV2; | | 986 | + const sink = yield* EventSink.EventSinkV2; | | 987 | + const now = yield* DateTime.now; | | 988 | + const threadId = ThreadId.make(`thread:ack-settle-race-${after}`); | | 989 | + const runId = RunId.make(`run:ack-settle-race-${after}`); | | 990 | + const taskId = NodeId.make(`node:ack-settle-race-${after}-task`); | | 991 | + const checkpointId = CheckpointId.make(`checkpoint:ack-settle-race-${after}`); | | 992 | + yield* seedParentWithTerminalTask({ | | 993 | + threadId, | | 994 | + projectId: ProjectId.make(`project:ack-settle-race-${after}`), | | 995 | + runId, | | 996 | + rootNodeId: NodeId.make(`node:ack-settle-race-${after}-root`), | | 997 | + taskId, | | 998 | + deliveryState: "claimed", | | 999 | + deliveryTaskIds: [taskId], | | 1000 | + now, | | 1001 | + }); | | 1002 | + const parent = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; | | 1003 | + yield* sink.write({ | | 1004 | + events: [ | | 1005 | + { | | 1006 | + id: EventId.make(`event:ack-settle-race-${after}-before`), | | 1007 | + type: "run.updated", | | 1008 | + threadId, | | 1009 | + runId, | | 1010 | + occurredAt: now, | | 1011 | + payload: { ...parent, status: before }, | | 1012 | + }, | | 1013 | + ], | | 1014 | + }); | | 1015 | + const { delegatedCompletion: _cohort, ...settledRun } = parent; | | 1016 | + const commit = sink.commitCommand; | | 1017 | + let settled = false; | | 1018 | + yield* Effect.acquireUseRelease( | | 1019 | + Effect.sync(() => | | 1020 | + vi.spyOn(sink, "commitCommand").mockImplementation((input) => | | 1021 | + Effect.gen(function* () { | | 1022 | + if ( | | 1023 | + !settled && | | 1024 | + input.commandType === "delegated_task.completion-delivery.acknowledge" | | 1025 | + ) { | | 1026 | + settled = true; | | 1027 | + // The provider turn ends (or its checkpoint lands) after acknowledge read the run. | | 1028 | + yield* commit({ | | 1029 | + commandId: CommandId.make(`command:ack-settle-race-${after}`), | | 1030 | + threadId, | | 1031 | + commandType: capture ? "checkpoint.capture" : "provider.event", | | 1032 | + acceptedAt: now, | | 1033 | + effects: [], | | 1034 | + events: [ | | 1035 | + { | | 1036 | + id: EventId.make(`event:ack-settle-race-${after}-settled`), | | 1037 | + type: "run.updated", | | 1038 | + threadId, | | 1039 | + runId, | | 1040 | + occurredAt: now, | | 1041 | + payload: { | | 1042 | + ...settledRun, | | 1043 | + status: after, | | 1044 | + completedAt: now, | | 1045 | + checkpointId: capture ? checkpointId : null, | | 1046 | + }, | | 1047 | + }, | | 1048 | + ], | | 1049 | + }); | | 1050 | + } | | 1051 | + return yield* commit(input); | | 1052 | + }), | | 1053 | + ), | | 1054 | + ), | | 1055 | + () => | | 1056 | + orchestrator.dispatch({ | | 1057 | + type: "delegated_task.completion-delivery.acknowledge", | | 1058 | + commandId: CommandId.make(`command:ack-settle-race-${after}:task-status`), | | 1059 | + parentThreadId: threadId, | | 1060 | + taskId, | | 1061 | + observedByRunId: runId, | | 1062 | + }), | | 1063 | + (spy) => Effect.sync(() => spy.mockRestore()), | | 1064 | + ); | | 1065 | + assert.isTrue(settled); | | 1066 | + const run = (yield* orchestrator.getThreadProjection(threadId)).runs.find( | | 1067 | + (candidate) => candidate.id === runId, | | 1068 | + )!; | | 1069 | + assert.deepEqual( | | 1070 | + { status: run.status, completedAt: run.completedAt, checkpointId: run.checkpointId }, | | 1071 | + { status: after, completedAt: now, checkpointId: capture ? checkpointId : null }, | | 1072 | + ); | | 1073 | + }), | | 1074 | + ); | 977 | 1075 | }); | 978 | 1076 |
| 979 | 1077 | const reconcileCommandId = (name: string) => CommandId.make(`command:runtime-reconcile:${name}`); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/EventSink.ts +31-21Lines changed: 31 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -146,6 +146,8 @@ export interface EventSinkV2Shape { | 146 | 146 | readonly effectTypes: ReadonlyArray<EffectOutbox.OrchestrationEffectRequestV2["type"]>; | 147 | 147 | readonly reason: string; | 148 | 148 | }; | | 149 | + /** Like writeWithEffects' updateDelegatedCompletionOnly, for this run's run.updated only. */ | | 150 | + readonly updateDelegatedCompletionOnlyRunId?: RunId; | 149 | 151 | }) => Effect.Effect< | 150 | 152 | { | 151 | 153 | readonly receipt: CommandReceiptStore.CommandReceiptV2; | @@ -383,6 +385,28 @@ const layerBase: Layer.Layer< | 383 | 385 | return yield* runOutcomes.prepare(storedEvents); | 384 | 386 | }); | 385 | 387 |
| | 388 | + // The enclosing write or commit span already traces this per-event projection merge. | | 389 | + const updateDelegatedCompletionOnly = Effect.fnUntraced(function* ( | | 390 | + events: ReadonlyArray<OrchestrationV2DomainEvent>, | | 391 | + includesRun: (runId: RunId) => boolean, | | 392 | + ) { | | 393 | + return yield* Effect.forEach(events, (event) => | | 394 | + Effect.gen(function* () { | | 395 | + if (event.type !== "run.updated" || !includesRun(event.payload.id)) return event; | | 396 | + const { runs } = yield* projectionStore.getThreadRecords(event.threadId, ["runs"], { | | 397 | + runIds: [event.payload.id], | | 398 | + }); | | 399 | + const current = runs[0]; | | 400 | + return current === undefined | | 401 | + ? event | | 402 | + : { | | 403 | + ...event, | | 404 | + payload: { ...current, delegatedCompletion: event.payload.delegatedCompletion }, | | 405 | + }; | | 406 | + }), | | 407 | + ); | | 408 | + }); | | 409 | + | 386 | 410 | const writeEffect = Effect.fn("orchestrationV2.EventSink.write")(function* ( | 387 | 411 | input: Parameters<EventSinkV2Shape["writeWithEffects"]>[0], | 388 | 412 | ) { | @@ -411,26 +435,7 @@ const layerBase: Layer.Layer< | 411 | 435 | // Checkpoint capture can finish after a cohort writer reads its parent run. | 412 | 436 | // Merge in this transaction so the event log and projection keep that completion. | 413 | 437 | const events = input.updateDelegatedCompletionOnly | 414 | | - ? yield* Effect.forEach(input.events, (event) => | 415 | | - Effect.gen(function* () { | 416 | | - if (event.type !== "run.updated") return event; | 417 | | - const { runs } = yield* projectionStore.getThreadRecords( | 418 | | - event.threadId, | 419 | | - ["runs"], | 420 | | - { runIds: [event.payload.id] }, | 421 | | - ); | 422 | | - const current = runs[0]; | 423 | | - return current === undefined | 424 | | - ? event | 425 | | - : { | 426 | | - ...event, | 427 | | - payload: { | 428 | | - ...current, | 429 | | - delegatedCompletion: event.payload.delegatedCompletion, | 430 | | - }, | 431 | | - }; | 432 | | - }), | 433 | | - ) | | 438 | + ? yield* updateDelegatedCompletionOnly(input.events, () => true) | 434 | 439 | : input.events; | 435 | 440 | const normalized = yield* normalizeEvents( | 436 | 441 | input.guardPendingUserInputCancellations === true | @@ -646,7 +651,12 @@ const layerBase: Layer.Layer< | 646 | 651 | return { ...existing, committed: false as const, cancelledEffectIds: [] }; | 647 | 652 | } | 648 | 653 |
| 649 | | - const normalized = yield* normalizeEvents(input.events); | | 654 | + const runId = input.updateDelegatedCompletionOnlyRunId; | | 655 | + const normalized = yield* normalizeEvents( | | 656 | + runId === undefined | | 657 | + ? input.events | | 658 | + : yield* updateDelegatedCompletionOnly(input.events, (id) => id === runId), | | 659 | + ); | 650 | 660 | const storedEvents = yield* eventStore.append({ | 651 | 661 | commandId: input.commandId, | 652 | 662 | events: normalized, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2358,7 +2358,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 2358 | 2358 | run: deliveryRun, | 2359 | 2359 | now, | 2360 | 2360 | }); | 2361 | | - return; | | 2361 | + return parentRun.id; | 2362 | 2362 | } | 2363 | 2363 | const message = completionDeliveryMessage(projection, delivery); | 2364 | 2364 | if (message !== undefined) { | @@ -2382,6 +2382,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 2382 | 2382 | }); | 2383 | 2383 | } | 2384 | 2384 | } | | 2385 | + return parentRun.id; | 2385 | 2386 | }); | 2386 | 2387 |
| 2387 | 2388 | const disposeDelegatedCompletionCohort = Effect.fn( | @@ -11752,6 +11753,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 11752 | 11753 | readonly effectTypes: ReadonlyArray<OrchestrationEffectRequestV2["type"]>; | 11753 | 11754 | readonly reason: string; | 11754 | 11755 | }; | | 11756 | + readonly updateDelegatedCompletionOnlyRunId?: RunId; | 11755 | 11757 | }, | 11756 | 11758 | OrchestratorV2Error | 11757 | 11759 | > { | @@ -11769,6 +11771,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 11769 | 11771 | readonly reason: string; | 11770 | 11772 | } | 11771 | 11773 | | undefined; | | 11774 | + let updateDelegatedCompletionOnlyRunId: RunId | undefined; | 11772 | 11775 | switch (command.type) { | 11773 | 11776 | case "thread.create": | 11774 | 11777 | yield* dispatchThreadCreate(command, events); | @@ -12080,7 +12083,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 12080 | 12083 | break; | 12081 | 12084 | case "delegated_task.completion-delivery.acknowledge": | 12082 | 12085 | case "delegated_task.completion-delivery.dispose": | 12083 | | - yield* dispatchDelegatedTaskCompletionDeliveryResolution(command, events); | | 12086 | + updateDelegatedCompletionOnlyRunId = | | 12087 | + yield* dispatchDelegatedTaskCompletionDeliveryResolution(command, events); | 12084 | 12088 | break; | 12085 | 12089 | case "thread.created.record": | 12086 | 12090 | yield* dispatchCreatedThreadRecord(command, events); | @@ -12095,6 +12099,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 12095 | 12099 | events: yield* Ref.get(events), | 12096 | 12100 | effects: yield* Ref.get(effects), | 12097 | 12101 | ...(cancelUnsettledEffects === undefined ? {} : { cancelUnsettledEffects }), | | 12102 | + ...(updateDelegatedCompletionOnlyRunId === undefined | | 12103 | + ? {} | | 12104 | + : { updateDelegatedCompletionOnlyRunId }), | 12098 | 12105 | }; | 12099 | 12106 | }); | 12100 | 12107 |
| @@ -12271,6 +12278,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 12271 | 12278 | ...(plan.cancelUnsettledEffects === undefined | 12272 | 12279 | ? {} | 12273 | 12280 | : { cancelUnsettledEffects: plan.cancelUnsettledEffects }), | | 12281 | + ...(plan.updateDelegatedCompletionOnlyRunId === undefined | | 12282 | + ? {} | | 12283 | + : { updateDelegatedCompletionOnlyRunId: plan.updateDelegatedCompletionOnlyRunId }), | 12274 | 12284 | }) | 12275 | 12285 | .pipe( | 12276 | 12286 | Effect.mapError( | |
|