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

CheckpointStore.ts

172 lines5.8 KB

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;
29​
30export interface RestoreCheckpointInput {
31 readonly cwd: string;
32 readonly checkpointRef: CheckpointRef;
33 readonly fallbackToHead?: boolean;
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";
44​
45export interface DeleteCheckpointRefsInput {
46 readonly cwd: string;
47 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
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 /** Check whether a checkpoint ref exists. */
67 readonly hasCheckpointRef: (
68 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
69 ) => Effect.Effect<boolean, CheckpointStoreError>;
70​
71 /**
72 * Restore workspace and staging state to a checkpoint.
73 *
74 * Optionally falls back to current `HEAD` when the checkpoint ref is missing.
75 */
76 readonly restoreCheckpoint: (
77 input: RestoreCheckpointInput,
78 ) => Effect.Effect<boolean, CheckpointStoreError>;
79​
80 /**
81 * Compute a diff between two checkpoint refs. Defaults to a full patch.
82 *
83 * Numstat output has NUL-delimited paths for file summaries.
84 * Can optionally treat a missing "from" ref as `HEAD`.
85 */
86 readonly diffCheckpoints: (
87 input: DiffCheckpointsInput,
88 ) => Effect.Effect<string, CheckpointStoreError>;
89​
90 /**
91 * Delete the provided checkpoint refs.
92 *
93 * Best-effort delete: missing refs are tolerated.
94 */
95 readonly deleteCheckpointRefs: (
96 input: DeleteCheckpointRefsInput,
97 ) => Effect.Effect<void, CheckpointStoreError>;
98 }
99>()("t3/checkpointing/CheckpointStore") {}
100​
101export const make = Effect.gen(function* () {
102 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
103​
104 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
105 operation: string,
106 cwd: string,
107 ) {
108 const handle = yield* vcsRegistry.resolve({ cwd });
109 if (!handle.driver.checkpoints) {
110 return yield* new VcsUnsupportedOperationError({
111 operation,
112 kind: handle.kind,
113 detail: `${handle.kind} driver does not implement checkpoint operations.`,
114 });
115 }
116 return handle.driver.checkpoints satisfies VcsCheckpointOps;
117 });
118​
119 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
120 vcsRegistry
121 .detect({ cwd, requestedKind: "git" })
122 .pipe(Effect.map((repository) => repository !== null));
123​
124 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
125 "captureCheckpoint",
126 )(function* (input) {
127 const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
128 return yield* checkpoints.captureCheckpoint(input);
129 });
130​
131 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
132 "hasCheckpointRef",
133 )(function* (input) {
134 const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
135 return yield* checkpoints.hasCheckpointRef(input);
136 });
137​
138 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
139 "restoreCheckpoint",
140 )(function* (input) {
141 const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
142 return yield* checkpoints.restoreCheckpoint(input);
143 });
144​
145 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
146 "diffCheckpoints",
147 )(function* (input) {
148 const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
149 return yield* checkpoints.diffCheckpoints(input);
150 });
151​
152 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
153 "deleteCheckpointRefs",
154 )(function* (input) {
155 const checkpoints = yield* resolveCheckpoints(
156 "CheckpointStore.deleteCheckpointRefs",
157 input.cwd,
158 );
159 return yield* checkpoints.deleteCheckpointRefs(input);
160 });
161​
162 return CheckpointStore.of({
163 isGitRepository,
164 captureCheckpoint,
165 hasCheckpointRef,
166 restoreCheckpoint,
167 diffCheckpoints,
168 deleteCheckpointRefs,
169 });
170});
171​
172export const layer = Layer.effect(CheckpointStore, make);