Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions apps/server/src/checkpointing/CheckpointStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";

import * as NodeServices from "@effect/platform-node/NodeServices";
Expand Down Expand Up @@ -134,6 +135,117 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
expect(yield* checkpointStore.isGitRepository(nested)).toBe(true);
}),
);
describe("restoreCheckpoint", () => {
it.effect("restores tracked, staged and untracked files only inside the workspace", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
yield* initRepoWithCommit(tmp);
const fs = yield* FileSystem.FileSystem;
const cwd = NodePath.join(tmp, "app");
yield* fs.makeDirectory(cwd);
const tracked = NodePath.join(cwd, "tracked.txt");
yield* writeTextFile(tracked, "committed\n");
yield* git(tmp, ["add", "."]);
yield* git(tmp, ["commit", "-m", "workspace"]);
const head = yield* git(tmp, ["rev-parse", "HEAD"]);
const store = yield* CheckpointStore.CheckpointStore;
const input = {
cwd,
checkpointRef: checkpointRefForThreadTurn(ThreadId.make("restore-subtree"), 0),
};
yield* writeTextFile(tracked, "checkpoint\n");
yield* writeTextFile(NodePath.join(cwd, "captured.txt"), "captured untracked\n");
yield* store.captureCheckpoint(input);

yield* writeTextFile(tracked, "staged\n");
yield* writeTextFile(NodePath.join(cwd, "staged-new.txt"), "staged addition\n");
yield* writeTextFile(NodePath.join(tmp, "README.md"), "sibling staged\n");
yield* git(tmp, ["add", "."]);
yield* writeTextFile(tracked, "unstaged\n");
yield* fs.remove(NodePath.join(cwd, "captured.txt"));
yield* writeTextFile(NodePath.join(cwd, "untracked.txt"), "discard\n");
yield* writeTextFile(NodePath.join(tmp, "outside.txt"), "keep\n");
const siblingIndex = yield* git(tmp, ["diff", "--cached", "--", "README.md"]);

expect(yield* store.restoreCheckpoint(input)).toBe(true);
expect(yield* fs.readFileString(tracked)).toBe("checkpoint\n");
expect(yield* fs.readFileString(NodePath.join(cwd, "captured.txt"))).toBe(
"captured untracked\n",
);
expect(yield* fs.exists(NodePath.join(cwd, "staged-new.txt"))).toBe(false);
expect(yield* fs.exists(NodePath.join(cwd, "untracked.txt"))).toBe(false);
expect(yield* git(tmp, ["diff", "--cached", "--", "app"])).toBe("");
expect(yield* fs.readFileString(NodePath.join(tmp, "outside.txt"))).toBe("keep\n");
expect(yield* fs.readFileString(NodePath.join(tmp, "README.md"))).toBe("sibling staged\n");
expect(yield* git(tmp, ["diff", "--cached", "--", "README.md"])).toBe(siblingIndex);
expect(yield* git(tmp, ["rev-parse", "HEAD"])).toBe(head);
}),
);

it.effect("leaves files and the index untouched when the checkpoint ref is missing", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
yield* initRepoWithCommit(tmp);
const fs = yield* FileSystem.FileSystem;
const store = yield* CheckpointStore.CheckpointStore;
yield* writeTextFile(NodePath.join(tmp, "README.md"), "staged\n");
yield* git(tmp, ["add", "README.md"]);
yield* writeTextFile(NodePath.join(tmp, "README.md"), "unstaged\n");
yield* writeTextFile(NodePath.join(tmp, "untracked.txt"), "keep\n");
const index = yield* fs.readFile(NodePath.join(tmp, ".git/index"));

expect(
yield* store.restoreCheckpoint({
cwd: tmp,
checkpointRef: checkpointRefForThreadTurn(ThreadId.make("missing-restore"), 0),
fallbackToHead: false,
}),
).toBe(false);
expect(yield* fs.readFileString(NodePath.join(tmp, "README.md"))).toBe("unstaged\n");
expect(yield* fs.readFileString(NodePath.join(tmp, "untracked.txt"))).toBe("keep\n");
expect(yield* fs.readFile(NodePath.join(tmp, ".git/index"))).toEqual(index);
}),
);
});

it.effect("fingerprints all destructive restore inputs without changing the index", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
yield* initRepoWithCommit(tmp);
const store = yield* CheckpointStore.CheckpointStore;
const fs = yield* FileSystem.FileSystem;
const readme = NodePath.join(tmp, "README.md");
const before = yield* store.readWorkspaceFingerprint(tmp);
yield* writeTextFile(readme, "staged\n");
yield* git(tmp, ["add", "README.md"]);
yield* writeTextFile(readme, "# test\n");
const index = yield* fs.readFile(NodePath.join(tmp, ".git/index"));
const stagedOnly = yield* store.readWorkspaceFingerprint(tmp);
expect(stagedOnly).not.toBe(before);
yield* writeTextFile(readme, "working tree\n");
const tracked = yield* store.readWorkspaceFingerprint(tmp);
expect(tracked).not.toBe(stagedOnly);
yield* writeTextFile(NodePath.join(tmp, "untracked.txt"), "untracked\n");
const untracked = yield* store.readWorkspaceFingerprint(tmp);
expect(untracked).not.toBe(tracked);
expect(yield* store.readWorkspaceFingerprint(tmp)).toBe(untracked);
yield* git(tmp, ["config", "core.autocrlf", "true"]);
yield* writeTextFile(readme, "same normalized contents\n");
const lf = yield* store.readWorkspaceFingerprint(tmp);
yield* writeTextFile(readme, "same normalized contents\r\n");
expect(yield* store.readWorkspaceFingerprint(tmp)).not.toBe(lf);
if ((yield* HostProcess.Platform) !== "win32") {
const link = NodePath.join(tmp, "raw-target-link");
yield* Effect.tryPromise(() => NodeFSP.symlink(Buffer.from([0xfe]), link));
const firstTarget = yield* store.readWorkspaceFingerprint(tmp);
yield* fs.remove(link);
yield* Effect.tryPromise(() => NodeFSP.symlink(Buffer.from([0xff]), link));
expect(yield* store.readWorkspaceFingerprint(tmp)).not.toBe(firstTarget);
}
expect(yield* fs.readFile(NodePath.join(tmp, ".git/index"))).toEqual(index);
}),
);

describe("diffCheckpoints", () => {
it.effect("returns full oversized checkpoint diffs without truncation", () =>
Effect.gen(function* () {
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/checkpointing/CheckpointStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface DeleteCheckpointRefsInput {
export class CheckpointStore extends Context.Service<
CheckpointStore,
{
readonly readWorkspaceFingerprint: (cwd: string) => Effect.Effect<string, CheckpointStoreError>;
readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;

readonly captureCheckpoint: (
Expand Down Expand Up @@ -146,6 +147,10 @@ export const make = Effect.gen(function* () {
});

return CheckpointStore.of({
readWorkspaceFingerprint: (cwd) =>
resolveCheckpoints("CheckpointStore.readWorkspaceFingerprint", cwd).pipe(
Effect.flatMap((ops) => ops.readWorkspaceFingerprint(cwd)),
),
isGitRepository,
captureCheckpoint,
hasCheckpointRef,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import {
Expand Down Expand Up @@ -106,6 +107,7 @@ it.effect.each([
const layerTest = CheckpointRollbackService.layer.pipe(
Layer.provide(
Layer.mergeAll(
Layer.mock(CheckpointStore.CheckpointStore)({}),
NodeServices.layer,
Layer.mock(ServerConfig.ServerConfig)({ worktreesDir: "/synthetic-worktrees" } as never),
ServerSettings.layerTest(),
Expand Down Expand Up @@ -192,7 +194,7 @@ it.effect.each([
const error = yield* service
.execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles })
.pipe(Effect.flip);
assert.equal(error.reason, "shared-workspace");
assert.equal("reason" in error ? error.reason : undefined, "shared-workspace");
assert.deepEqual(calls, []);
assert.equal(yield* fs.readFileString(otherFile), "other thread's uncommitted work");
} else {
Expand Down
62 changes: 61 additions & 1 deletion apps/server/src/orchestration-v2/CheckpointRestoreSafety.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import type { OrchestrationV2AppThread, OrchestrationV2CheckpointScope } from "@t3tools/contracts";
import { CheckpointId, CheckpointScopeId, ThreadId } from "@t3tools/contracts";
// Synchronous hashing keeps the command decision pure; only the bounded digest is persisted.
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeCrypto from "node:crypto";
import * as Schema from "effect/Schema";
import type {
OrchestrationV2AppThread,
OrchestrationV2CheckpointScope,
OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import type * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
Expand Down Expand Up @@ -96,3 +105,54 @@ export const isCheckpointRestoreIsolated = Effect.fn("orchestrationV2.isCheckpoi
return !shared;
},
);

const encodeRestoreState = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));

// Excludes rollback request metadata, which admission itself updates.
export const checkpointRestoreThreadState = (
projection: Pick<
OrchestrationV2ThreadProjection,
| "thread"
| "runs"
| "attempts"
| "providerTurns"
| "providerThreads"
| "checkpointScopes"
| "checkpoints"
>,
) =>
NodeCrypto.createHash("sha256")
.update(
encodeRestoreState([
projection.thread.archivedAt,
projection.thread.deletedAt,
projection.thread.worktreePath,
projection.thread.modelSelection,
projection.thread.activeProviderThreadId,
projection.runs,
projection.attempts,
projection.providerTurns,
projection.providerThreads,
projection.checkpointScopes,
projection.checkpoints,
]),
)
.digest("hex");

export const checkpointRestoreThreadIdle = (
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs">,
) =>
projection.thread.archivedAt == null &&
projection.thread.deletedAt == null &&
!projection.runs.some((run) =>
["preparing", "queued", "starting", "running", "waiting"].includes(run.status),
);

export const CheckpointRestoreRejectedRequest = Schema.fromJsonString(
Schema.Struct({
type: Schema.Literal("checkpoint_restore_rejected"),
threadId: ThreadId,
scopeId: CheckpointScopeId,
checkpointId: CheckpointId,
}),
);
Loading
Loading