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 /** Null means HEAD is unchanged or unrecorded; all changed paths then belong to the turn. */
67 readonly listAuthoredPaths: (
68 input: ListAuthoredPathsInput,
69 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
70
71 readonly deleteCheckpointRefs: (
72 input: DeleteCheckpointRefsInput,
73 ) => Effect.Effect<void, CheckpointStoreError>;
74 }
75>()("t3/checkpointing/CheckpointStore") {}
76
77/** @public */
78export const make = Effect.gen(function* () {
79 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
80
81 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
82 operation: string,
83 cwd: string,
84 ) {
85 const handle = yield* vcsRegistry.resolve({ cwd });
86 if (!handle.driver.checkpoints) {
87 return yield* new VcsUnsupportedOperationError({
88 operation,
89 kind: handle.kind,
90 detail: `${handle.kind} driver does not implement checkpoint operations.`,
91 });
92 }
93 return handle.driver.checkpoints satisfies VcsCheckpointOps;
94 });
95
96 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
97 vcsRegistry
98 .detect({ cwd, requestedKind: "git" })
99 .pipe(Effect.map((repository) => repository !== null));
100
101 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
102 "captureCheckpoint",
103 )(function* (input) {
104 const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
105 return yield* checkpoints.captureCheckpoint(input);
106 });
107
108 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
109 "hasCheckpointRef",
110 )(function* (input) {
111 const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
112 return yield* checkpoints.hasCheckpointRef(input);
113 });
114
115 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
116 "restoreCheckpoint",
117 )(function* (input) {
118 const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
119 return yield* checkpoints.restoreCheckpoint(input);
120 });
121
122 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
123 "diffCheckpoints",
124 )(function* (input) {
125 const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
126 return yield* checkpoints.diffCheckpoints(input);
127 });
128
129 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
130 "listAuthoredPaths",
131 )(function* (input) {
132 const checkpoints = yield* resolveCheckpoints("CheckpointStore.listAuthoredPaths", input.cwd);
133 return yield* checkpoints.listAuthoredPaths(input);
134 });
135
136 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
137 "deleteCheckpointRefs",
138 )(function* (input) {
139 const checkpoints = yield* resolveCheckpoints(
140 "CheckpointStore.deleteCheckpointRefs",
141 input.cwd,
142 );
143 return yield* checkpoints.deleteCheckpointRefs(input);
144 });
145
146 return CheckpointStore.of({
147 isGitRepository,
148 captureCheckpoint,
149 hasCheckpointRef,
150 restoreCheckpoint,
151 diffCheckpoints,
152 listAuthoredPaths,
153 deleteCheckpointRefs,
154 });
155});
156
157export const layer = Layer.effect(CheckpointStore, make);