Files
t3code/apps/server/src/checkpointing/CheckpointStore.ts
T
Theo BrowneandClaude Fable 5 fc3a695383 feat: attribute turn diffs so git-only changes stop polluting changed files
The changed-files card and per-turn inline diff are derived from full-tree
checkpoint snapshots, so any tree-rewriting git command the agent runs
(pull, checkout, cherry-pick, rebase) shows up as hundreds of "changed
files" the agent never authored.

This adds a read-side attribution layer on top of the existing checkpoint
mechanism (capture, storage, and revert are untouched):

- Checkpoint commits now record the HEAD oid and branch in their commit
  message. Legacy checkpoints without metadata degrade to today's behavior.
- attributeCheckpointDiff classifies each path in a checkpoint tree delta:
  a path is "git" only when its exact blob transition also appears in the
  HEAD-movement delta for the turn and no commit authored during the turn
  touched it. Every ambiguity resolves to "agent" (shown), so
  misclassification can add noise but never hide agent work. Merge commits
  use the combined diff so only conflict resolutions count as agent work.
- The checkpoint reactor tags each file with an origin; attribution
  failures degrade to unattributed files and never block the pipeline.
- getTurnDiff/getFullThreadDiff drop git-attributed file sections from the
  patch unless the new includeGitChanges input is set.
- The changed-files card lists agent files only, with a muted
  "Updated via git — N files" row for demoted changes. The diff panel
  shows a banner with a show/hide toggle so the full tree delta stays one
  click away.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-23 13:43:20 -07:00

205 lines
6.9 KiB
TypeScript

/**
* CheckpointStore - Repository interface for filesystem-backed workspace checkpoints.
*
* Owns hidden Git-ref checkpoint capture/restore and diff computation for a
* workspace thread timeline. It does not store user-facing checkpoint metadata
* and does not coordinate provider conversation rollback.
*
* The live adapter resolves the active VCS driver once per checkpoint operation
* and delegates to the driver's optional checkpoint capability.
*
* Uses Effect `Context.Service` for dependency injection and exposes typed
* domain errors for checkpoint storage operations.
*
* @module CheckpointStore
*/
import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import type { CheckpointStoreError } from "./Errors.ts";
import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
export interface CaptureCheckpointInput {
readonly cwd: string;
readonly checkpointRef: CheckpointRef;
}
export interface RestoreCheckpointInput {
readonly cwd: string;
readonly checkpointRef: CheckpointRef;
readonly fallbackToHead?: boolean;
}
export interface DiffCheckpointsInput {
readonly cwd: string;
readonly fromCheckpointRef: CheckpointRef;
readonly toCheckpointRef: CheckpointRef;
readonly fallbackFromToHead?: boolean;
readonly ignoreWhitespace: boolean;
}
export interface DeleteCheckpointRefsInput {
readonly cwd: string;
readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
}
export interface AttributeCheckpointDiffInput {
readonly cwd: string;
readonly fromCheckpointRef: CheckpointRef;
readonly toCheckpointRef: CheckpointRef;
}
/**
* Per-path origin classification of a checkpoint tree delta, or null when
* attribution is unavailable (see VcsCheckpointAttribution).
*/
export type CheckpointAttribution = ReadonlyMap<string, "agent" | "git"> | null;
/** Service tag for checkpoint persistence and restore operations. */
export class CheckpointStore extends Context.Service<
CheckpointStore,
{
/** Check whether cwd is inside a Git worktree. */
readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
/**
* Capture a checkpoint commit and store it at the provided checkpoint ref.
*
* Uses an isolated temporary Git index and writes a hidden ref.
*/
readonly captureCheckpoint: (
input: CaptureCheckpointInput,
) => Effect.Effect<void, CheckpointStoreError>;
/** Check whether a checkpoint ref exists. */
readonly hasCheckpointRef: (
input: Omit<RestoreCheckpointInput, "fallbackToHead">,
) => Effect.Effect<boolean, CheckpointStoreError>;
/**
* Restore workspace and staging state to a checkpoint.
*
* Optionally falls back to current `HEAD` when the checkpoint ref is missing.
*/
readonly restoreCheckpoint: (
input: RestoreCheckpointInput,
) => Effect.Effect<boolean, CheckpointStoreError>;
/**
* Compute a patch diff between two checkpoint refs.
*
* Can optionally treat a missing "from" ref as `HEAD`.
*/
readonly diffCheckpoints: (
input: DiffCheckpointsInput,
) => Effect.Effect<string, CheckpointStoreError>;
/**
* Delete the provided checkpoint refs.
*
* Best-effort delete: missing refs are tolerated.
*/
readonly deleteCheckpointRefs: (
input: DeleteCheckpointRefsInput,
) => Effect.Effect<void, CheckpointStoreError>;
/**
* Classify each path in a checkpoint-to-checkpoint tree delta as
* agent-authored or explained by pre-existing git history.
*
* Returns null when attribution is unavailable (legacy checkpoints
* without HEAD metadata, or truncated git output).
*/
readonly attributeCheckpointDiff: (
input: AttributeCheckpointDiffInput,
) => Effect.Effect<CheckpointAttribution, CheckpointStoreError>;
}
>()("t3/checkpointing/CheckpointStore") {}
export const make = Effect.gen(function* () {
const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
operation: string,
cwd: string,
) {
const handle = yield* vcsRegistry.resolve({ cwd });
if (!handle.driver.checkpoints) {
return yield* new VcsUnsupportedOperationError({
operation,
kind: handle.kind,
detail: `${handle.kind} driver does not implement checkpoint operations.`,
});
}
return handle.driver.checkpoints satisfies VcsCheckpointOps;
});
const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
vcsRegistry
.detect({ cwd, requestedKind: "git" })
.pipe(Effect.map((repository) => repository !== null));
const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
"captureCheckpoint",
)(function* (input) {
const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
return yield* checkpoints.captureCheckpoint(input);
});
const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
"hasCheckpointRef",
)(function* (input) {
const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
return yield* checkpoints.hasCheckpointRef(input);
});
const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
"restoreCheckpoint",
)(function* (input) {
const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
return yield* checkpoints.restoreCheckpoint(input);
});
const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
"diffCheckpoints",
)(function* (input) {
const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
return yield* checkpoints.diffCheckpoints(input);
});
const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
"deleteCheckpointRefs",
)(function* (input) {
const checkpoints = yield* resolveCheckpoints(
"CheckpointStore.deleteCheckpointRefs",
input.cwd,
);
return yield* checkpoints.deleteCheckpointRefs(input);
});
const attributeCheckpointDiff: CheckpointStore["Service"]["attributeCheckpointDiff"] = Effect.fn(
"attributeCheckpointDiff",
)(function* (input) {
const checkpoints = yield* resolveCheckpoints(
"CheckpointStore.attributeCheckpointDiff",
input.cwd,
);
return yield* checkpoints.attributeCheckpointDiff(input);
});
return CheckpointStore.of({
isGitRepository,
captureCheckpoint,
hasCheckpointRef,
restoreCheckpoint,
diffCheckpoints,
deleteCheckpointRefs,
attributeCheckpointDiff,
});
});
export const layer = Layer.effect(CheckpointStore, make);