Skip to content

Commit 441ade6

Browse files
fix(server): task_status acknowledge no longer reverts a run that settled mid-commit
Fixes #24743 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2bcb551 commit 441ade6

2 files changed

Lines changed: 42 additions & 23 deletions

File tree

‎apps/server/src/orchestration-v2/EventSink.ts‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export interface EventSinkV2Shape {
146146
readonly effectTypes: ReadonlyArray<EffectOutbox.OrchestrationEffectRequestV2["type"]>;
147147
readonly reason: string;
148148
};
149+
/** Like writeWithEffects' updateDelegatedCompletionOnly, for this run's run.updated only. */
150+
readonly updateDelegatedCompletionOnlyRunId?: RunId;
149151
}) => Effect.Effect<
150152
{
151153
readonly receipt: CommandReceiptStore.CommandReceiptV2;
@@ -383,6 +385,27 @@ const layerBase: Layer.Layer<
383385
return yield* runOutcomes.prepare(storedEvents);
384386
});
385387

388+
const updateDelegatedCompletionOnly = Effect.fnUntraced(function* (
389+
events: ReadonlyArray<OrchestrationV2DomainEvent>,
390+
includesRun: (runId: RunId) => boolean,
391+
) {
392+
return yield* Effect.forEach(events, (event) =>
393+
Effect.gen(function* () {
394+
if (event.type !== "run.updated" || !includesRun(event.payload.id)) return event;
395+
const { runs } = yield* projectionStore.getThreadRecords(event.threadId, ["runs"], {
396+
runIds: [event.payload.id],
397+
});
398+
const current = runs[0];
399+
return current === undefined
400+
? event
401+
: {
402+
...event,
403+
payload: { ...current, delegatedCompletion: event.payload.delegatedCompletion },
404+
};
405+
}),
406+
);
407+
});
408+
386409
const writeEffect = Effect.fn("orchestrationV2.EventSink.write")(function* (
387410
input: Parameters<EventSinkV2Shape["writeWithEffects"]>[0],
388411
) {
@@ -411,26 +434,7 @@ const layerBase: Layer.Layer<
411434
// Checkpoint capture can finish after a cohort writer reads its parent run.
412435
// Merge in this transaction so the event log and projection keep that completion.
413436
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-
)
437+
? yield* updateDelegatedCompletionOnly(input.events, () => true)
434438
: input.events;
435439
const normalized = yield* normalizeEvents(
436440
input.guardPendingUserInputCancellations === true
@@ -646,7 +650,12 @@ const layerBase: Layer.Layer<
646650
return { ...existing, committed: false as const, cancelledEffectIds: [] };
647651
}
648652

649-
const normalized = yield* normalizeEvents(input.events);
653+
const runId = input.updateDelegatedCompletionOnlyRunId;
654+
const normalized = yield* normalizeEvents(
655+
runId === undefined
656+
? input.events
657+
: yield* updateDelegatedCompletionOnly(input.events, (id) => id === runId),
658+
);
650659
const storedEvents = yield* eventStore.append({
651660
commandId: input.commandId,
652661
events: normalized,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23582358
run: deliveryRun,
23592359
now,
23602360
});
2361-
return;
2361+
return parentRun.id;
23622362
}
23632363
const message = completionDeliveryMessage(projection, delivery);
23642364
if (message !== undefined) {
@@ -2382,6 +2382,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23822382
});
23832383
}
23842384
}
2385+
return parentRun.id;
23852386
});
23862387

23872388
const disposeDelegatedCompletionCohort = Effect.fn(
@@ -11752,6 +11753,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1175211753
readonly effectTypes: ReadonlyArray<OrchestrationEffectRequestV2["type"]>;
1175311754
readonly reason: string;
1175411755
};
11756+
readonly updateDelegatedCompletionOnlyRunId?: RunId;
1175511757
},
1175611758
OrchestratorV2Error
1175711759
> {
@@ -11769,6 +11771,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1176911771
readonly reason: string;
1177011772
}
1177111773
| undefined;
11774+
let updateDelegatedCompletionOnlyRunId: RunId | undefined;
1177211775
switch (command.type) {
1177311776
case "thread.create":
1177411777
yield* dispatchThreadCreate(command, events);
@@ -12080,7 +12083,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1208012083
break;
1208112084
case "delegated_task.completion-delivery.acknowledge":
1208212085
case "delegated_task.completion-delivery.dispose":
12083-
yield* dispatchDelegatedTaskCompletionDeliveryResolution(command, events);
12086+
updateDelegatedCompletionOnlyRunId =
12087+
yield* dispatchDelegatedTaskCompletionDeliveryResolution(command, events);
1208412088
break;
1208512089
case "thread.created.record":
1208612090
yield* dispatchCreatedThreadRecord(command, events);
@@ -12095,6 +12099,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1209512099
events: yield* Ref.get(events),
1209612100
effects: yield* Ref.get(effects),
1209712101
...(cancelUnsettledEffects === undefined ? {} : { cancelUnsettledEffects }),
12102+
...(updateDelegatedCompletionOnlyRunId === undefined
12103+
? {}
12104+
: { updateDelegatedCompletionOnlyRunId }),
1209812105
};
1209912106
});
1210012107

@@ -12271,6 +12278,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1227112278
...(plan.cancelUnsettledEffects === undefined
1227212279
? {}
1227312280
: { cancelUnsettledEffects: plan.cancelUnsettledEffects }),
12281+
...(plan.updateDelegatedCompletionOnlyRunId === undefined
12282+
? {}
12283+
: { updateDelegatedCompletionOnlyRunId: plan.updateDelegatedCompletionOnlyRunId }),
1227412284
})
1227512285
.pipe(
1227612286
Effect.mapError(

0 commit comments

Comments
 (0)