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}
43
44export interface DeleteCheckpointRefsInput {
45 readonly cwd: string;
46 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
47}
48
49/** Service tag for checkpoint persistence and restore operations. */
50export class CheckpointStore extends Context.Service<
51 CheckpointStore,
52 {
53 /** Check whether cwd is inside a Git worktree. */
54 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
55
56 /**
57 * Capture a checkpoint commit and store it at the provided checkpoint ref.
58 *
59 * Uses an isolated temporary Git index and writes a hidden ref.
60 */
61 readonly captureCheckpoint: (
62 input: CaptureCheckpointInput,
63 ) => Effect.Effect<void, CheckpointStoreError>;
64
65 /** Check whether a checkpoint ref exists. */
66 readonly hasCheckpointRef: (
67 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
68 ) => Effect.Effect<boolean, CheckpointStoreError>;
69
70 /**
71 * Restore workspace and staging state to a checkpoint.
72 *
73 * Optionally falls back to current `HEAD` when the checkpoint ref is missing.
74 */
75 readonly restoreCheckpoint: (
76 input: RestoreCheckpointInput,
77 ) => Effect.Effect<boolean, CheckpointStoreError>;
78
79 /**
80 * Compute a patch diff between two checkpoint refs.
81 *
82 * Can optionally treat a missing "from" ref as `HEAD`.
83 */
84 readonly diffCheckpoints: (
85 input: DiffCheckpointsInput,
86 ) => Effect.Effect<string, CheckpointStoreError>;
87
88 /**
89 * Delete the provided checkpoint refs.
90 *
91 * Best-effort delete: missing refs are tolerated.
92 */
93 readonly deleteCheckpointRefs: (
94 input: DeleteCheckpointRefsInput,
95 ) => Effect.Effect<void, CheckpointStoreError>;
96 }
97>()("t3/checkpointing/CheckpointStore") {}
98
99export const make = Effect.gen(function* () {
100 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
101
102 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
103 operation: string,
104 cwd: string,
105 ) {
106 const handle = yield* vcsRegistry.resolve({ cwd });
107 if (!handle.driver.checkpoints) {
108 return yield* new VcsUnsupportedOperationError({
109 operation,
110 kind: handle.kind,
111 detail: `${handle.kind} driver does not implement checkpoint operations.`,
112 });
113 }
114 return handle.driver.checkpoints satisfies VcsCheckpointOps;
115 });
116
117 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
118 vcsRegistry
119 .detect({ cwd, requestedKind: "git" })
120 .pipe(Effect.map((repository) => repository !== null));
121
122 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
123 "captureCheckpoint",
124 )(function* (input) {
125 const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
126 return yield* checkpoints.captureCheckpoint(input);
127 });
128
129 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
130 "hasCheckpointRef",
131 )(function* (input) {
132 const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
133 return yield* checkpoints.hasCheckpointRef(input);
134 });
135
136 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
137 "restoreCheckpoint",
138 )(function* (input) {
139 const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
140 return yield* checkpoints.restoreCheckpoint(input);
141 });
142
143 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
144 "diffCheckpoints",
145 )(function* (input) {
146 const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
147 return yield* checkpoints.diffCheckpoints(input);
148 });
149
150 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
151 "deleteCheckpointRefs",
152 )(function* (input) {
153 const checkpoints = yield* resolveCheckpoints(
154 "CheckpointStore.deleteCheckpointRefs",
155 input.cwd,
156 );
157 return yield* checkpoints.deleteCheckpointRefs(input);
158 });
159
160 return CheckpointStore.of({
161 isGitRepository,
162 captureCheckpoint,
163 hasCheckpointRef,
164 restoreCheckpoint,
165 diffCheckpoints,
166 deleteCheckpointRefs,
167 });
168});
169
170export const layer = Layer.effect(CheckpointStore, make);