Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CheckpointPolicy.ts

84 lines2.7 KB

1import {
2 CheckpointId,
3 CheckpointScopeId,
4 NodeId,
5 OrchestrationV2Checkpoint,
6 OrchestrationV2CheckpointScope,
7 OrchestrationV2ExecutionNode,
8 OrchestrationV2Run,
9 OrchestrationV2ThreadProjection,
10 RunId,
11 ThreadId,
12} from "@t3tools/contracts";
13import * as Context from "effect/Context";
14import * as Schema from "effect/Schema";
15import type * as Effect from "effect/Effect";
16​
17export class CheckpointPolicyPrepareRunError extends Schema.TaggedErrorClass<CheckpointPolicyPrepareRunError>()(
18 "CheckpointPolicyPrepareRunError",
19 {
20 threadId: ThreadId,
21 runId: RunId,
22 cause: Schema.optional(Schema.Defect()),
23 },
24) {
25 override get message(): string {
26 return `Failed to prepare checkpoint policy for run ${this.runId}.`;
27 }
29​
30export class CheckpointPolicyFinalizeNodeError extends Schema.TaggedErrorClass<CheckpointPolicyFinalizeNodeError>()(
31 "CheckpointPolicyFinalizeNodeError",
32 {
33 threadId: ThreadId,
34 nodeId: NodeId,
35 scopeId: Schema.optional(CheckpointScopeId),
36 cause: Schema.optional(Schema.Defect()),
37 },
38) {
39 override get message(): string {
40 return `Failed to finalize checkpoint policy for node ${this.nodeId}.`;
41 }
43​
44export class CheckpointPolicyRollbackError extends Schema.TaggedErrorClass<CheckpointPolicyRollbackError>()(
45 "CheckpointPolicyRollbackError",
46 {
47 threadId: ThreadId,
48 scopeId: CheckpointScopeId,
49 checkpointId: CheckpointId,
50 cause: Schema.optional(Schema.Defect()),
51 },
52) {
53 override get message(): string {
54 return `Failed to apply checkpoint rollback ${this.checkpointId} for scope ${this.scopeId}.`;
55 }
57​
58export const CheckpointPolicyV2Error = Schema.Union([
59 CheckpointPolicyPrepareRunError,
60 CheckpointPolicyFinalizeNodeError,
61 CheckpointPolicyRollbackError,
62]);
63export type CheckpointPolicyV2Error = typeof CheckpointPolicyV2Error.Type;
64​
65export interface CheckpointPolicyV2Shape {
66 readonly prepareRun: (input: {
67 readonly projection: OrchestrationV2ThreadProjection;
68 readonly run: OrchestrationV2Run;
69 }) => Effect.Effect<ReadonlyArray<OrchestrationV2CheckpointScope>, CheckpointPolicyV2Error>;
70 readonly finalizeNode: (input: {
71 readonly projection: OrchestrationV2ThreadProjection;
72 readonly node: OrchestrationV2ExecutionNode;
73 }) => Effect.Effect<ReadonlyArray<OrchestrationV2Checkpoint>, CheckpointPolicyV2Error>;
74 readonly rollback: (input: {
75 readonly projection: OrchestrationV2ThreadProjection;
76 readonly scopeId: CheckpointScopeId;
77 readonly checkpointId: CheckpointId;
78 }) => Effect.Effect<ReadonlyArray<OrchestrationV2Checkpoint>, CheckpointPolicyV2Error>;
80​
81export class CheckpointPolicyV2 extends Context.Service<
82 CheckpointPolicyV2,
83 CheckpointPolicyV2Shape
84>()("t3/orchestration-v2/CheckpointPolicy/CheckpointPolicyV2") {}