Open History to see the last commit
1import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import type { CheckpointStoreError } from "./Errors.ts";
7import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
8import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
9
10export interface CaptureCheckpointInput {
11 readonly cwd: string;
12 readonly checkpointRef: CheckpointRef;
13}
14
15export interface RestoreCheckpointInput {
16 readonly cwd: string;
17 readonly checkpointRef: CheckpointRef;
18 readonly fallbackToHead?: boolean;
19}
20
21export interface DiffCheckpointsInput {
22 readonly cwd: string;
23 readonly fromCheckpointRef: CheckpointRef;
24 readonly toCheckpointRef: CheckpointRef;
25 readonly fallbackFromToHead?: boolean;
26 readonly ignoreWhitespace: boolean;
27 readonly format?: "patch" | "numstat";
28 /** Maximum bytes collected from the diff command. */
29 readonly maxOutputBytes?: number;
30 /** Limits the diff to these exact paths. An empty list yields an empty diff. */
31 readonly filePaths?: ReadonlyArray<string>;
32}
33
34export interface ListAuthoredPathsInput {
35 readonly cwd: string;
36 readonly fromCheckpointRef: CheckpointRef;
37 readonly toCheckpointRef: CheckpointRef;
38}
39
40export interface DeleteCheckpointRefsInput {
41 readonly cwd: string;
42 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
43}
44
45export class CheckpointStore extends Context.Service<
46 CheckpointStore,
47 {
48 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
49
50 readonly captureCheckpoint: (
51 input: CaptureCheckpointInput,
52 ) => Effect.Effect<void, CheckpointStoreError>;
53
54 readonly hasCheckpointRef: (
55 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
56 ) => Effect.Effect<boolean, CheckpointStoreError>;
57
58 readonly restoreCheckpoint: (
59 input: RestoreCheckpointInput,
60 ) => Effect.Effect<boolean, CheckpointStoreError>;
61
62 readonly diffCheckpoints: (
63 input: DiffCheckpointsInput,
64 ) => Effect.Effect<string, CheckpointStoreError>;
65
66 /**
67 * Returns null when HEAD did not move or a checkpoint does not record HEAD.
68 * Then every changed path belongs to the turn.
69 */
70 readonly listAuthoredPaths: (
71 input: ListAuthoredPathsInput,
72 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
73
74 readonly deleteCheckpointRefs: (
75 input: DeleteCheckpointRefsInput,
76 ) => Effect.Effect<void, CheckpointStoreError>;
77 }
78>()("t3/checkpointing/CheckpointStore") {}
79
80/** @public Service construction is part of the canonical Effect module API. */
81export const make = Effect.gen(function* () {
82 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
83
84 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
85 operation: string,
86 cwd: string,
87 ) {
88 const handle = yield* vcsRegistry.resolve({ cwd });
89 if (!handle.driver.checkpoints) {
90 return yield* new VcsUnsupportedOperationError({
91 operation,
92 kind: handle.kind,
93 detail: `${handle.kind} driver does not implement checkpoint operations.`,
94 });
95 }
96 return handle.driver.checkpoints satisfies VcsCheckpointOps;
97 });
98
99 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
100 vcsRegistry
101 .detect({ cwd, requestedKind: "git" })
102 .pipe(Effect.map((repository) => repository !== null));
103
104 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
105 "captureCheckpoint",
106 )(function* (input) {
107 const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
108 return yield* checkpoints.captureCheckpoint(input);
109 });
110
111 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
112 "hasCheckpointRef",
113 )(function* (input) {
114 const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
115 return yield* checkpoints.hasCheckpointRef(input);
116 });
117
118 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
119 "restoreCheckpoint",
120 )(function* (input) {
121 const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
122 return yield* checkpoints.restoreCheckpoint(input);
123 });
124
125 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
126 "diffCheckpoints",
127 )(function* (input) {
128 const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
129 return yield* checkpoints.diffCheckpoints(input);
130 });
131
132 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
133 "listAuthoredPaths",
134 )(function* (input) {
135 const checkpoints = yield* resolveCheckpoints("CheckpointStore.listAuthoredPaths", input.cwd);
136 return yield* checkpoints.listAuthoredPaths(input);
137 });
138
139 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
140 "deleteCheckpointRefs",
141 )(function* (input) {
142 const checkpoints = yield* resolveCheckpoints(
143 "CheckpointStore.deleteCheckpointRefs",
144 input.cwd,
145 );
146 return yield* checkpoints.deleteCheckpointRefs(input);
147 });
148
149 return CheckpointStore.of({
150 isGitRepository,
151 captureCheckpoint,
152 hasCheckpointRef,
153 restoreCheckpoint,
154 diffCheckpoints,
155 listAuthoredPaths,
156 deleteCheckpointRefs,
157 });
158});
159
160export const layer = Layer.effect(CheckpointStore, make);