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 * Uses Effect `ServiceMap.Service` for dependency injection and exposes typed
9 * domain errors for checkpoint storage operations.
10 *
11 * @module CheckpointStore
12 */
13import { ServiceMap } from "effect";
14import type { Effect } from "effect";
15
16import type { CheckpointStoreError } from "../Errors.ts";
17import { CheckpointRef } from "@t3tools/contracts";
18
19export interface CaptureCheckpointInput {
20 readonly cwd: string;
21 readonly checkpointRef: CheckpointRef;
22}
23
24export interface RestoreCheckpointInput {
25 readonly cwd: string;
26 readonly checkpointRef: CheckpointRef;
27 readonly fallbackToHead?: boolean;
28}
29
30export interface DiffCheckpointsInput {
31 readonly cwd: string;
32 readonly fromCheckpointRef: CheckpointRef;
33 readonly toCheckpointRef: CheckpointRef;
34 readonly fallbackFromToHead?: boolean;
35}
36
37export interface DeleteCheckpointRefsInput {
38 readonly cwd: string;
39 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
40}
41
42/**
43 * CheckpointStoreShape - Service API for checkpoint capture/restore and diff access.
44 */
45export interface CheckpointStoreShape {
46 /**
47 * Check whether cwd is inside a Git worktree.
48 */
49 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
50
51 /**
52 * Capture a checkpoint commit and store it at the provided checkpoint ref.
53 *
54 * Uses an isolated temporary Git index and writes a hidden ref.
55 */
56 readonly captureCheckpoint: (
57 input: CaptureCheckpointInput,
58 ) => Effect.Effect<void, CheckpointStoreError>;
59
60 /**
61 * Check whether a checkpoint ref exists.
62 */
63 readonly hasCheckpointRef: (
64 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
65 ) => Effect.Effect<boolean, CheckpointStoreError>;
66
67 /**
68 * Restore workspace/staging state to a checkpoint.
69 *
70 * Optionally falls back to current `HEAD` when the checkpoint ref is missing.
71 */
72 readonly restoreCheckpoint: (
73 input: RestoreCheckpointInput,
74 ) => Effect.Effect<boolean, CheckpointStoreError>;
75
76 /**
77 * Compute patch diff between two checkpoint refs.
78 *
79 * Can optionally treat missing "from" ref as `HEAD`.
80 */
81 readonly diffCheckpoints: (
82 input: DiffCheckpointsInput,
83 ) => Effect.Effect<string, CheckpointStoreError>;
84
85 /**
86 * Delete the provided checkpoint refs.
87 *
88 * Best-effort delete: missing refs are tolerated.
89 */
90 readonly deleteCheckpointRefs: (
91 input: DeleteCheckpointRefsInput,
92 ) => Effect.Effect<void, CheckpointStoreError>;
93}
94
95/**
96 * CheckpointStore - Service tag for checkpoint persistence and restore operations.
97 */
98export class CheckpointStore extends ServiceMap.Service<CheckpointStore, CheckpointStoreShape>()(
99 "t3/checkpointing/Services/CheckpointStore",
100) {}