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

CheckpointStore.ts

197 lines7.1 KB

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