Open History to see the last commit
1/**
2 * CheckpointStore - Repository interface for filesystem-backed workspace checkpoints.
3 *
4 * Owns hidden Git-ref checkpoint capture/restore and diff computation for a
5 * workspace thread timeline. It does not store user-facing checkpoint metadata
6 * and does not coordinate provider conversation rollback.
7 *
8 * The live adapter resolves the active VCS driver once per checkpoint operation
9 * and delegates to the driver's optional checkpoint capability.
10 *
11 * Uses Effect `Context.Service` for dependency injection and exposes typed
12 * domain errors for checkpoint storage operations.
13 *
14 * @module CheckpointStore
15 */
16import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts";
17import * as Context from "effect/Context";
18import * as Effect from "effect/Effect";
19import * as Layer from "effect/Layer";
20
21import type { CheckpointStoreError } from "./Errors.ts";
22import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
23import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
24
25export interface CaptureCheckpointInput {
26 readonly cwd: string;
27 readonly checkpointRef: CheckpointRef;
28}
29
30export interface RestoreCheckpointInput {
31 readonly cwd: string;
32 readonly checkpointRef: CheckpointRef;
33 readonly fallbackToHead?: boolean;
34}
35
36export interface DiffCheckpointsInput {
37 readonly cwd: string;
38 readonly fromCheckpointRef: CheckpointRef;
39 readonly toCheckpointRef: CheckpointRef;
40 readonly fallbackFromToHead?: boolean;
41 readonly ignoreWhitespace: boolean;
42 readonly format?: "patch" | "numstat";
43}
44
45export interface DeleteCheckpointRefsInput {
46 readonly cwd: string;
47 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
48}
49
50/** Service tag for checkpoint persistence and restore operations. */
51export class CheckpointStore extends Context.Service<
52 CheckpointStore,
53 {
54 /** Check whether cwd is inside a Git worktree. */
55 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
56
57 /**
58 * Capture a checkpoint commit and store it at the provided checkpoint ref.
59 *
60 * Uses an isolated temporary Git index and writes a hidden ref.
61 */
62 readonly captureCheckpoint: (
63 input: CaptureCheckpointInput,
64 ) => Effect.Effect<void, CheckpointStoreError>;
65
66 /**
67 * Best-effort warm-up of the driver's checkpoint machinery so the first
68 * blocking capture in a fresh workspace is fast. No-op for drivers
69 * without warm support.
70 */
71 readonly warmCheckpoint: (input: {
72 readonly cwd: string;
73 }) => Effect.Effect<void, CheckpointStoreError>;
74
75 /** Check whether a checkpoint ref exists. */
76 readonly hasCheckpointRef: (
77 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
78 ) => Effect.Effect<boolean, CheckpointStoreError>;
79
80 /**
81 * Restore workspace and staging state to a checkpoint.
82 *
83 * Optionally falls back to current `HEAD` when the checkpoint ref is missing.
84 */
85 readonly restoreCheckpoint: (
86 input: RestoreCheckpointInput,
87 ) => Effect.Effect<boolean, CheckpointStoreError>;
88
89 /**
90 * Compute a diff between two checkpoint refs. Defaults to a full patch.
91 *
92 * Numstat output has NUL-delimited paths for file summaries.
93 * Can optionally treat a missing "from" ref as `HEAD`.
94 */
95 readonly diffCheckpoints: (
96 input: DiffCheckpointsInput,
97 ) => Effect.Effect<string, CheckpointStoreError>;
98
99 /**
100 * Delete the provided checkpoint refs.
101 *
102 * Best-effort delete: missing refs are tolerated.
103 */
104 readonly deleteCheckpointRefs: (
105 input: DeleteCheckpointRefsInput,
106 ) => Effect.Effect<void, CheckpointStoreError>;
107 }
108>()("t3/checkpointing/CheckpointStore") {}
109
110/** @public Service construction is part of the canonical Effect module API. */
111export const make = Effect.gen(function* () {
112 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
113
114 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
115 operation: string,
116 cwd: string,
117 ) {
118 const handle = yield* vcsRegistry.resolve({ cwd });
119 if (!handle.driver.checkpoints) {
120 return yield* new VcsUnsupportedOperationError({
121 operation,
122 kind: handle.kind,
123 detail: `${handle.kind} driver does not implement checkpoint operations.`,
124 });
125 }
126 return handle.driver.checkpoints satisfies VcsCheckpointOps;
127 });
128
129 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
130 vcsRegistry
131 .detect({ cwd, requestedKind: "git" })
132 .pipe(Effect.map((repository) => repository !== null));
133
134 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
135 "captureCheckpoint",
136 )(function* (input) {
137 const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
138 return yield* checkpoints.captureCheckpoint(input);
139 });
140
141 const warmCheckpoint: CheckpointStore["Service"]["warmCheckpoint"] = Effect.fn("warmCheckpoint")(
142 function* (input) {
143 const checkpoints = yield* resolveCheckpoints("CheckpointStore.warmCheckpoint", input.cwd);
144 return yield* checkpoints.warmCheckpoint?.(input) ?? Effect.void;
145 },
146 );
147
148 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
149 "hasCheckpointRef",
150 )(function* (input) {
151 const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
152 return yield* checkpoints.hasCheckpointRef(input);
153 });
154
155 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
156 "restoreCheckpoint",
157 )(function* (input) {
158 const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
159 return yield* checkpoints.restoreCheckpoint(input);
160 });
161
162 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
163 "diffCheckpoints",
164 )(function* (input) {
165 const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
166 return yield* checkpoints.diffCheckpoints(input);
167 });
168
169 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
170 "deleteCheckpointRefs",
171 )(function* (input) {
172 const checkpoints = yield* resolveCheckpoints(
173 "CheckpointStore.deleteCheckpointRefs",
174 input.cwd,
175 );
176 return yield* checkpoints.deleteCheckpointRefs(input);
177 });
178
179 return CheckpointStore.of({
180 isGitRepository,
181 captureCheckpoint,
182 warmCheckpoint,
183 hasCheckpointRef,
184 restoreCheckpoint,
185 diffCheckpoints,
186 deleteCheckpointRefs,
187 });
188});
189
190export const layer = Layer.effect(CheckpointStore, make);