Open History to see the last commit
1import { 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, VcsCheckpointRange } from "../vcs/VcsDriver.ts";
8import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
9
10export interface CaptureCheckpointInput {
11 readonly cwd: string;
12 readonly checkpointRef: CheckpointRef;
13 /** The checkpoint this one follows, so a turn that switched branches starts where it landed. */
14 readonly previousCheckpointRef?: CheckpointRef;
15 /** Folders below `cwd` (relative, `/`-separated) that have their own checkpoints. */
16 readonly excludePaths?: ReadonlyArray<string>;
17}
18
19export type CheckpointRange = VcsCheckpointRange;
20
21export interface CaptureHeadCheckpointInput {
22 readonly cwd: string;
23 readonly checkpointRef: CheckpointRef;
24 /** A commit `resolveCleanHead` returned for this folder. */
25 readonly head: string;
26 /**
27 * When the folder was seen clean, in epoch seconds, if that was earlier than now. Commits made
28 * after a checkpoint count as the turn's own work, so a late snapshot must not postdate them.
29 */
30 readonly seenAt?: number;
31}
32
33export interface RestoreCheckpointInput {
34 readonly cwd: string;
35 readonly checkpointRef: CheckpointRef;
36 readonly fallbackToHead?: boolean;
37 /** Folders below `cwd` the restore leaves as they are, as in a capture. */
38 readonly excludePaths?: ReadonlyArray<string>;
39}
40
41export interface DiffCheckpointsInput {
42 readonly cwd: string;
43 readonly fromCheckpointRef: CheckpointRef;
44 readonly toCheckpointRef: CheckpointRef;
45 readonly fallbackFromToHead?: boolean;
46 readonly ignoreWhitespace: boolean;
47 readonly format?: "patch" | "numstat";
48 /** Maximum bytes collected from the diff command. */
49 readonly maxOutputBytes?: number;
50 /** Limits the diff to these exact paths. An empty list yields an empty diff. */
51 readonly filePaths?: ReadonlyArray<string>;
52 /** Folders below `cwd` left out of the diff, as in a capture. */
53 readonly excludePaths?: ReadonlyArray<string>;
54 /** The refs' range as their capture returned it; the refs are then not read. */
55 readonly range?: VcsCheckpointRange;
56}
57
58export interface ListAuthoredPathsInput {
59 readonly cwd: string;
60 readonly fromCheckpointRef: CheckpointRef;
61 readonly toCheckpointRef: CheckpointRef;
62 /** The refs' range as their capture returned it; the refs are then not read. */
63 readonly range?: VcsCheckpointRange;
64}
65
66export interface DeleteCheckpointRefsInput {
67 readonly cwd: string;
68 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
69}
70
71export class CheckpointStore extends Context.Service<
72 CheckpointStore,
73 {
74 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
75
76 /** The range from `previousCheckpointRef`, or null when there is none to compare with. */
77 readonly captureCheckpoint: (
78 input: CaptureCheckpointInput,
79 ) => Effect.Effect<VcsCheckpointRange | null, CheckpointStoreError>;
80
81 /**
82 * HEAD's commit when the folder matches it exactly: nothing staged, modified or untracked.
83 * Null otherwise. One status, where a capture stages the whole worktree.
84 */
85 readonly resolveCleanHead: (cwd: string) => Effect.Effect<string | null, CheckpointStoreError>;
86
87 /** Snapshots a folder that was clean at `head` from that commit, without reading its files. */
88 readonly captureHeadCheckpoint: (
89 input: CaptureHeadCheckpointInput,
90 ) => Effect.Effect<void, CheckpointStoreError>;
91
92 readonly hasCheckpointRef: (
93 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
94 ) => Effect.Effect<boolean, CheckpointStoreError>;
95
96 readonly copyCheckpointRef: VcsCheckpointOps["copyCheckpointRef"];
97
98 readonly restoreCheckpoint: (
99 input: RestoreCheckpointInput,
100 ) => Effect.Effect<boolean, CheckpointStoreError>;
101
102 readonly diffCheckpoints: (
103 input: DiffCheckpointsInput,
104 ) => Effect.Effect<string, CheckpointStoreError>;
105
106 /** Null means HEAD is unchanged or unrecorded; all changed paths then belong to the turn. */
107 readonly listAuthoredPaths: (
108 input: ListAuthoredPathsInput,
109 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
110
111 readonly deleteCheckpointRefs: (
112 input: DeleteCheckpointRefsInput,
113 ) => Effect.Effect<void, CheckpointStoreError>;
114 }
115>()("t3/checkpointing/CheckpointStore") {}
116
117/** @public */
118export const make = Effect.gen(function* () {
119 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
120
121 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
122 cwd: string,
123 ) {
124 const handle = yield* vcsRegistry.resolve({ cwd });
125 return handle.driver.checkpoints satisfies VcsCheckpointOps;
126 });
127
128 // The registry reads an unreadable repository as none; ask Git again so that fails here.
129 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = Effect.fn(
130 "CheckpointStore.isGitRepository",
131 )(function* (cwd) {
132 if ((yield* vcsRegistry.detect({ cwd, requestedKind: "git" })) !== null) return true;
133 const git = yield* vcsRegistry.get("git");
134 return (yield* git.detectRepository(cwd)) !== null;
135 });
136
137 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
138 "captureCheckpoint",
139 )(function* (input) {
140 const checkpoints = yield* resolveCheckpoints(input.cwd);
141 return yield* checkpoints.captureCheckpoint(input);
142 });
143
144 const resolveCleanHead: CheckpointStore["Service"]["resolveCleanHead"] = Effect.fn(
145 "CheckpointStore.resolveCleanHead",
146 )(function* (cwd) {
147 const checkpoints = yield* resolveCheckpoints(cwd);
148 return yield* checkpoints.resolveCleanHead(cwd);
149 });
150
151 const captureHeadCheckpoint: CheckpointStore["Service"]["captureHeadCheckpoint"] = Effect.fn(
152 "CheckpointStore.captureHeadCheckpoint",
153 )(function* (input) {
154 const checkpoints = yield* resolveCheckpoints(input.cwd);
155 return yield* checkpoints.captureHeadCheckpoint(input);
156 });
157
158 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
159 "hasCheckpointRef",
160 )(function* (input) {
161 const checkpoints = yield* resolveCheckpoints(input.cwd);
162 return yield* checkpoints.hasCheckpointRef(input);
163 });
164
165 const copyCheckpointRef: CheckpointStore["Service"]["copyCheckpointRef"] = Effect.fn(
166 "CheckpointStore.copyCheckpointRef",
167 )(function* (input) {
168 const checkpoints = yield* resolveCheckpoints(input.cwd);
169 return yield* checkpoints.copyCheckpointRef(input);
170 });
171
172 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
173 "restoreCheckpoint",
174 )(function* (input) {
175 const checkpoints = yield* resolveCheckpoints(input.cwd);
176 return yield* checkpoints.restoreCheckpoint(input);
177 });
178
179 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
180 "diffCheckpoints",
181 )(function* (input) {
182 const checkpoints = yield* resolveCheckpoints(input.cwd);
183 return yield* checkpoints.diffCheckpoints(input);
184 });
185
186 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
187 "listAuthoredPaths",
188 )(function* (input) {
189 const checkpoints = yield* resolveCheckpoints(input.cwd);
190 return yield* checkpoints.listAuthoredPaths(input);
191 });
192
193 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
194 "deleteCheckpointRefs",
195 )(function* (input) {
196 const checkpoints = yield* resolveCheckpoints(input.cwd);
197 return yield* checkpoints.deleteCheckpointRefs(input);
198 });
199
200 return CheckpointStore.of({
201 isGitRepository,
202 captureCheckpoint,
203 resolveCleanHead,
204 captureHeadCheckpoint,
205 hasCheckpointRef,
206 copyCheckpointRef,
207 restoreCheckpoint,
208 diffCheckpoints,
209 listAuthoredPaths,
210 deleteCheckpointRefs,
211 });
212});
213
214export const layer = Layer.effect(CheckpointStore, make);