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

CheckpointStore.ts

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