+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
286 lines
10 KiB
TypeScript
286 lines
10 KiB
TypeScript
import {
|
|
CheckpointScopeId,
|
|
CommandId,
|
|
type OrchestrationV2Checkpoint,
|
|
type OrchestrationV2ExecutionNode,
|
|
type OrchestrationV2ProviderThread,
|
|
type OrchestrationV2Run,
|
|
type OrchestrationV2TurnItem,
|
|
RunId,
|
|
ThreadId,
|
|
} from "@t3tools/contracts";
|
|
import * as Context from "effect/Context";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Schema from "effect/Schema";
|
|
|
|
import * as CheckpointService from "./CheckpointService.ts";
|
|
import * as EventSink from "./EventSink.ts";
|
|
import * as IdAllocator from "./IdAllocator.ts";
|
|
import * as ProjectionStore from "./ProjectionStore.ts";
|
|
|
|
export class CheckpointCaptureExecutionError extends Schema.TaggedError<CheckpointCaptureExecutionError>()(
|
|
"CheckpointCaptureExecutionError",
|
|
{
|
|
threadId: ThreadId,
|
|
runId: RunId,
|
|
scopeId: CheckpointScopeId,
|
|
cause: Schema.optional(Schema.Defect()),
|
|
},
|
|
) {}
|
|
|
|
const isCheckpointCaptureExecutionError = Schema.is(CheckpointCaptureExecutionError);
|
|
|
|
export interface CheckpointCaptureServiceV2Shape {
|
|
readonly execute: (input: {
|
|
readonly threadId: ThreadId;
|
|
readonly runId: RunId;
|
|
readonly scopeId: CheckpointScopeId;
|
|
}) => Effect.Effect<void, CheckpointCaptureExecutionError>;
|
|
}
|
|
|
|
export class CheckpointCaptureServiceV2 extends Context.Service<
|
|
CheckpointCaptureServiceV2,
|
|
CheckpointCaptureServiceV2Shape
|
|
>()("t3/orchestration-v2/CheckpointCaptureService/CheckpointCaptureServiceV2") {}
|
|
|
|
export const layer: Layer.Layer<
|
|
CheckpointCaptureServiceV2,
|
|
never,
|
|
| CheckpointService.CheckpointServiceV2
|
|
| EventSink.EventSinkV2
|
|
| IdAllocator.IdAllocatorV2
|
|
| ProjectionStore.ProjectionStoreV2
|
|
> = Layer.effect(
|
|
CheckpointCaptureServiceV2,
|
|
Effect.gen(function* () {
|
|
const checkpoints = yield* CheckpointService.CheckpointServiceV2;
|
|
const eventSink = yield* EventSink.EventSinkV2;
|
|
const ids = yield* IdAllocator.IdAllocatorV2;
|
|
const projections = yield* ProjectionStore.ProjectionStoreV2;
|
|
|
|
const execute = Effect.fn("orchestrationV2.checkpointCapture.execute")(function* (input: {
|
|
readonly threadId: ThreadId;
|
|
readonly runId: RunId;
|
|
readonly scopeId: CheckpointScopeId;
|
|
}) {
|
|
const { run, rootNode, scope, providerThread, readyCheckpointOrdinals } =
|
|
yield* projections.getCheckpointCaptureContext(input.threadId, input);
|
|
// A stopped run is already terminal. Its checkpoint is the rollback point
|
|
// for the message after it, so capture leaves its status alone.
|
|
const stopped = run?.status === "interrupted" || run?.status === "cancelled";
|
|
|
|
// The effect is at-least-once. A settled run with a checkpoint proves
|
|
// that an earlier execution committed its result.
|
|
if (
|
|
run !== undefined &&
|
|
run.checkpointId !== null &&
|
|
(run.status === "completed" || stopped)
|
|
) {
|
|
return;
|
|
}
|
|
// Rollback shares this effect lane, so it can only land before a capture
|
|
// runs, e.g. while a failed capture waits to retry. The workspace now
|
|
// holds the rollback target, and the run must stay discarded.
|
|
if (run?.status === "rolled_back") {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
run === undefined ||
|
|
(run.status !== "waiting" && !stopped) ||
|
|
rootNode === undefined ||
|
|
scope === undefined ||
|
|
rootNode.checkpointScopeId !== scope.id ||
|
|
providerThread === undefined
|
|
) {
|
|
return yield* new CheckpointCaptureExecutionError({
|
|
threadId: input.threadId,
|
|
runId: input.runId,
|
|
scopeId: input.scopeId,
|
|
cause: "The persisted checkpoint capture target is incomplete or no longer waiting.",
|
|
});
|
|
}
|
|
|
|
const capturedAt = yield* DateTime.now;
|
|
const baselineOrdinalWithinScope = Math.max(0, run.ordinal - 1);
|
|
const hasReadyCheckpoint = (ordinalWithinScope: number) =>
|
|
readyCheckpointOrdinals.includes(ordinalWithinScope);
|
|
const threadStartCheckpoint =
|
|
baselineOrdinalWithinScope === 0 || hasReadyCheckpoint(0)
|
|
? null
|
|
: yield* checkpoints.materializeBaselineCheckpoint({
|
|
scope,
|
|
ordinalWithinScope: 0,
|
|
});
|
|
const baselineCheckpoint = hasReadyCheckpoint(baselineOrdinalWithinScope)
|
|
? null
|
|
: yield* checkpoints.materializeBaselineCheckpoint({
|
|
scope,
|
|
ordinalWithinScope: baselineOrdinalWithinScope,
|
|
});
|
|
const checkpoint = yield* checkpoints.capture({
|
|
scope,
|
|
runId: run.id,
|
|
nodeId: rootNode.id,
|
|
ordinalWithinScope: run.ordinal,
|
|
appRunOrdinal: run.ordinal,
|
|
capturedAt,
|
|
});
|
|
// Match RunExecutionService: capture loaded the waiting run before
|
|
// materializing baselines. Omit delegatedCompletion so a newer cohort
|
|
// write during capture is not overwritten by this stale snapshot
|
|
// (ProjectionStore preserves the field when absent from the payload).
|
|
const { delegatedCompletion: _delegatedCompletion, ...runWithoutDelegatedCompletion } = run;
|
|
const commandId = CommandId.make(`command:effect:checkpoint.capture:${run.id}`);
|
|
yield* eventSink.commitCommand({
|
|
commandId,
|
|
threadId: input.threadId,
|
|
commandType: "checkpoint.capture",
|
|
acceptedAt: capturedAt,
|
|
effects: [],
|
|
events: [
|
|
...(threadStartCheckpoint === null
|
|
? []
|
|
: [
|
|
{
|
|
id: yield* ids.allocate.event({ threadId: input.threadId, commandId }),
|
|
type: "checkpoint.captured" as const,
|
|
threadId: input.threadId,
|
|
nodeId: threadStartCheckpoint.nodeId,
|
|
driver: providerThread.driver,
|
|
providerInstanceId: run.providerInstanceId,
|
|
occurredAt: capturedAt,
|
|
payload: threadStartCheckpoint,
|
|
},
|
|
]),
|
|
...(baselineCheckpoint === null
|
|
? []
|
|
: [
|
|
{
|
|
id: yield* ids.allocate.event({ threadId: input.threadId, commandId }),
|
|
type: "checkpoint.captured" as const,
|
|
threadId: input.threadId,
|
|
nodeId: baselineCheckpoint.nodeId,
|
|
driver: providerThread.driver,
|
|
providerInstanceId: run.providerInstanceId,
|
|
occurredAt: capturedAt,
|
|
payload: baselineCheckpoint,
|
|
},
|
|
]),
|
|
{
|
|
id: yield* ids.allocate.event({ threadId: input.threadId, commandId }),
|
|
type: "checkpoint.captured",
|
|
threadId: input.threadId,
|
|
runId: run.id,
|
|
nodeId: rootNode.id,
|
|
driver: providerThread.driver,
|
|
providerInstanceId: run.providerInstanceId,
|
|
occurredAt: capturedAt,
|
|
payload: checkpoint,
|
|
},
|
|
{
|
|
id: yield* ids.allocate.event({ threadId: input.threadId, commandId }),
|
|
type: "turn-item.updated",
|
|
threadId: input.threadId,
|
|
runId: run.id,
|
|
nodeId: rootNode.id,
|
|
driver: providerThread.driver,
|
|
providerInstanceId: run.providerInstanceId,
|
|
occurredAt: capturedAt,
|
|
payload: makeCheckpointTurnItem({
|
|
idAllocator: ids,
|
|
run,
|
|
rootNode,
|
|
providerThread,
|
|
checkpoint,
|
|
completedAt: capturedAt,
|
|
}),
|
|
},
|
|
{
|
|
id: yield* ids.allocate.event({ threadId: input.threadId, commandId }),
|
|
type: "run.updated",
|
|
threadId: input.threadId,
|
|
runId: run.id,
|
|
nodeId: rootNode.id,
|
|
providerInstanceId: run.providerInstanceId,
|
|
occurredAt: capturedAt,
|
|
payload: stopped
|
|
? { ...runWithoutDelegatedCompletion, checkpointId: checkpoint.id }
|
|
: {
|
|
...runWithoutDelegatedCompletion,
|
|
status: "completed",
|
|
completedAt: capturedAt,
|
|
checkpointId: checkpoint.id,
|
|
},
|
|
},
|
|
...(stopped
|
|
? []
|
|
: [
|
|
{
|
|
id: yield* ids.allocate.event({ threadId: input.threadId, commandId }),
|
|
type: "node.updated" as const,
|
|
threadId: input.threadId,
|
|
runId: run.id,
|
|
nodeId: rootNode.id,
|
|
providerInstanceId: run.providerInstanceId,
|
|
occurredAt: capturedAt,
|
|
payload: {
|
|
...rootNode,
|
|
status: "completed" as const,
|
|
completedAt: capturedAt,
|
|
checkpointScopeId: scope.id,
|
|
},
|
|
},
|
|
]),
|
|
],
|
|
});
|
|
});
|
|
|
|
return CheckpointCaptureServiceV2.of({
|
|
execute: (input) =>
|
|
execute(input).pipe(
|
|
Effect.mapError((cause) =>
|
|
isCheckpointCaptureExecutionError(cause)
|
|
? cause
|
|
: new CheckpointCaptureExecutionError({ ...input, cause }),
|
|
),
|
|
),
|
|
});
|
|
}),
|
|
);
|
|
|
|
function makeCheckpointTurnItem(input: {
|
|
readonly idAllocator: IdAllocator.IdAllocatorV2Shape;
|
|
readonly run: OrchestrationV2Run;
|
|
readonly rootNode: OrchestrationV2ExecutionNode;
|
|
readonly providerThread: OrchestrationV2ProviderThread;
|
|
readonly checkpoint: OrchestrationV2Checkpoint;
|
|
readonly completedAt: DateTime.Utc;
|
|
}): OrchestrationV2TurnItem {
|
|
return {
|
|
id: input.idAllocator.derive.turnItemFromProviderItem({
|
|
driver: input.providerThread.driver,
|
|
nativeItemId: `checkpoint:${input.checkpoint.id}`,
|
|
}),
|
|
threadId: input.run.threadId,
|
|
runId: input.run.id,
|
|
nodeId: input.rootNode.id,
|
|
providerThreadId: input.providerThread.id,
|
|
providerTurnId: input.rootNode.providerTurnId,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal: input.run.ordinal * 100 + 99,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: input.completedAt,
|
|
completedAt: input.completedAt,
|
|
updatedAt: input.completedAt,
|
|
type: "checkpoint",
|
|
checkpointId: input.checkpoint.id,
|
|
scopeId: input.checkpoint.scopeId,
|
|
files: input.checkpoint.files,
|
|
};
|
|
}
|