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 restoreCheckpoint: (
97 input: RestoreCheckpointInput,
98 ) => Effect.Effect<boolean, CheckpointStoreError>;
99
100 readonly diffCheckpoints: (
101 input: DiffCheckpointsInput,
102 ) => Effect.Effect<string, CheckpointStoreError>;
103
104 /** Null means HEAD is unchanged or unrecorded; all changed paths then belong to the turn. */
105 readonly listAuthoredPaths: (
106 input: ListAuthoredPathsInput,
107 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
108
109 readonly deleteCheckpointRefs: (
110 input: DeleteCheckpointRefsInput,
111 ) => Effect.Effect<void, CheckpointStoreError>;
112 }
113>()("t3/checkpointing/CheckpointStore") {}
114
115/** @public */
116export const make = Effect.gen(function* () {
117 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
118
119 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
120 cwd: string,
121 ) {
122 const handle = yield* vcsRegistry.resolve({ cwd });
123 return handle.driver.checkpoints satisfies VcsCheckpointOps;
124 });
125
126 // The registry reads an unreadable repository as none; ask Git again so that fails here.
127 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = Effect.fn(
128 "CheckpointStore.isGitRepository",
129 )(function* (cwd) {
130 if ((yield* vcsRegistry.detect({ cwd, requestedKind: "git" })) !== null) return true;
131 const git = yield* vcsRegistry.get("git");
132 return (yield* git.detectRepository(cwd)) !== null;
133 });
134
135 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
136 "captureCheckpoint",
137 )(function* (input) {
138 const checkpoints = yield* resolveCheckpoints(input.cwd);
139 return yield* checkpoints.captureCheckpoint(input);
140 });
141
142 const resolveCleanHead: CheckpointStore["Service"]["resolveCleanHead"] = Effect.fn(
143 "CheckpointStore.resolveCleanHead",
144 )(function* (cwd) {
145 const checkpoints = yield* resolveCheckpoints(cwd);
146 return yield* checkpoints.resolveCleanHead(cwd);
147 });
148
149 const captureHeadCheckpoint: CheckpointStore["Service"]["captureHeadCheckpoint"] = Effect.fn(
150 "CheckpointStore.captureHeadCheckpoint",
151 )(function* (input) {
152 const checkpoints = yield* resolveCheckpoints(input.cwd);
153 return yield* checkpoints.captureHeadCheckpoint(input);
154 });
155
156 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
157 "hasCheckpointRef",
158 )(function* (input) {
159 const checkpoints = yield* resolveCheckpoints(input.cwd);
160 return yield* checkpoints.hasCheckpointRef(input);
161 });
162
163 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
164 "restoreCheckpoint",
165 )(function* (input) {
166 const checkpoints = yield* resolveCheckpoints(input.cwd);
167 return yield* checkpoints.restoreCheckpoint(input);
168 });
169
170 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
171 "diffCheckpoints",
172 )(function* (input) {
173 const checkpoints = yield* resolveCheckpoints(input.cwd);
174 return yield* checkpoints.diffCheckpoints(input);
175 });
176
177 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
178 "listAuthoredPaths",
179 )(function* (input) {
180 const checkpoints = yield* resolveCheckpoints(input.cwd);
181 return yield* checkpoints.listAuthoredPaths(input);
182 });
183
184 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
185 "deleteCheckpointRefs",
186 )(function* (input) {
187 const checkpoints = yield* resolveCheckpoints(input.cwd);
188 return yield* checkpoints.deleteCheckpointRefs(input);
189 });
190
191 return CheckpointStore.of({
192 isGitRepository,
193 captureCheckpoint,
194 resolveCleanHead,
195 captureHeadCheckpoint,
196 hasCheckpointRef,
197 restoreCheckpoint,
198 diffCheckpoints,
199 listAuthoredPaths,
200 deleteCheckpointRefs,
201 });
202});
203
204export const layer = Layer.effect(CheckpointStore, make);