Compare commits

..
Author SHA1 Message Date
Julius Marmingeandcodex b7789116f7 Fix large sidebar collapse performance (Issue #3962)
Co-authored-by: codex <[email protected]>
2026-07-20 17:27:42 +02:00
19 changed files with 570 additions and 1990 deletions

No files matched your search

@@ -1,5 +1,4 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeCrypto from "node:crypto";
import * as NodePath from "node:path";
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -113,247 +112,9 @@ it.layer(TestLayer)("CheckpointStore.layer", (it) => {
expect(yield* checkpointStore.isGitRepository(tmp)).toBe(true);
}),
);
it.effect("recognizes an owned Git repository through a symbolic path alias", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
const fileSystem = yield* FileSystem.FileSystem;
const repositoryPath = NodePath.join(tmp, "repository");
const aliasPath = NodePath.join(tmp, "repository-alias");
yield* fileSystem.makeDirectory(repositoryPath);
yield* initRepoWithCommit(repositoryPath);
yield* fileSystem.symlink(repositoryPath, aliasPath);
const checkpointStore = yield* CheckpointStore.CheckpointStore;
expect(yield* checkpointStore.isGitRepository(aliasPath)).toBe(true);
}),
);
it.effect("treats a standalone project nested under another repository as non-Git", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
const fileSystem = yield* FileSystem.FileSystem;
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const project = NodePath.join(tmp, "project");
yield* git(tmp, ["init"]);
yield* fileSystem.makeDirectory(project);
expect(yield* checkpointStore.isGitRepository(project)).toBe(false);
}),
);
});
describe("diffCheckpoints", () => {
it.effect("captures and restores an initialized repository before its first commit", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-unborn-git-test-");
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const threadId = ThreadId.make("thread-unborn-git-checkpoint-store");
const baselineRef = checkpointRefForThreadTurn(threadId, 0);
const sourcePath = NodePath.join(tmp, "source.txt");
yield* git(tmp, ["init"]);
yield* writeTextFile(sourcePath, "before\n");
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: baselineRef });
yield* writeTextFile(sourcePath, "after\n");
expect(
yield* checkpointStore.restoreCheckpoint({
cwd: tmp,
checkpointRef: baselineRef,
}),
).toBe(true);
const fileSystem = yield* FileSystem.FileSystem;
expect(yield* fileSystem.readFileString(sourcePath)).toBe("before\n");
}),
);
it.effect("restores an empty checkpoint in a repository before its first commit", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-unborn-empty-test-");
const fileSystem = yield* FileSystem.FileSystem;
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const baselineRef = checkpointRefForThreadTurn(
ThreadId.make("thread-unborn-empty-checkpoint-store"),
0,
);
const createdPath = NodePath.join(tmp, "created.txt");
const postCheckpointIgnorePath = NodePath.join(tmp, ".gitignore");
const ignoredCreatedPath = NodePath.join(tmp, "state.cache");
yield* git(tmp, ["init"]);
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: baselineRef });
yield* writeTextFile(createdPath, "created after baseline\n");
yield* writeTextFile(postCheckpointIgnorePath, "*.cache\n");
yield* writeTextFile(ignoredCreatedPath, "ignored after baseline\n");
expect(
yield* checkpointStore.restoreCheckpoint({
cwd: tmp,
checkpointRef: baselineRef,
}),
).toBe(true);
expect(yield* fileSystem.exists(createdPath)).toBe(false);
expect(yield* fileSystem.exists(postCheckpointIgnorePath)).toBe(false);
expect(yield* fileSystem.exists(ignoredCreatedPath)).toBe(false);
}),
);
it.effect("preserves files ignored by the restored checkpoint", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-unborn-ignored-test-");
const fileSystem = yield* FileSystem.FileSystem;
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const baselineRef = checkpointRefForThreadTurn(
ThreadId.make("thread-unborn-ignored-checkpoint-store"),
0,
);
const ignorePath = NodePath.join(tmp, ".gitignore");
const ignoredPath = NodePath.join(tmp, "local.cache");
yield* git(tmp, ["init"]);
yield* writeTextFile(ignorePath, "*.cache\n");
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: baselineRef });
yield* writeTextFile(ignoredPath, "local-only data\n");
expect(
yield* checkpointStore.restoreCheckpoint({
cwd: tmp,
checkpointRef: baselineRef,
}),
).toBe(true);
expect(yield* fileSystem.readFileString(ignorePath)).toBe("*.cache\n");
expect(yield* fileSystem.readFileString(ignoredPath)).toBe("local-only data\n");
}),
);
it.effect("uses shadow checkpoints for a project nested under an unrelated repository", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-nested-project-test-");
const fileSystem = yield* FileSystem.FileSystem;
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const project = NodePath.join(tmp, "project");
const threadId = ThreadId.make("thread-nested-project-checkpoint-store");
const baselineRef = checkpointRefForThreadTurn(threadId, 0);
const changedRef = checkpointRefForThreadTurn(threadId, 1);
const createdPath = NodePath.join(project, "created.txt");
yield* git(tmp, ["init"]);
yield* fileSystem.makeDirectory(project);
yield* checkpointStore.captureCheckpoint({ cwd: project, checkpointRef: baselineRef });
yield* writeTextFile(createdPath, "created\n");
yield* checkpointStore.captureCheckpoint({ cwd: project, checkpointRef: changedRef });
expect(
yield* checkpointStore.restoreCheckpoint({
cwd: project,
checkpointRef: baselineRef,
}),
).toBe(true);
expect(yield* fileSystem.exists(createdPath)).toBe(false);
expect(yield* checkpointStore.isGitRepository(project)).toBe(false);
}),
);
it.effect("captures and restores files without initializing Git in the workspace", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-shadow-test-");
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const threadId = ThreadId.make("thread-shadow-checkpoint-store");
const baselineRef = checkpointRefForThreadTurn(threadId, 0);
const changedRef = checkpointRefForThreadTurn(threadId, 1);
const sourcePath = NodePath.join(tmp, "source.txt");
const createdPath = NodePath.join(tmp, "created.txt");
yield* writeTextFile(sourcePath, "before\n");
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: baselineRef });
yield* writeTextFile(sourcePath, "after\n");
yield* writeTextFile(createdPath, "new\n");
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: changedRef });
const diff = yield* checkpointStore.diffCheckpoints({
cwd: tmp,
fromCheckpointRef: baselineRef,
toCheckpointRef: changedRef,
ignoreWhitespace: false,
});
expect(diff).toContain("+after");
expect(diff).toContain("created.txt");
expect(
yield* checkpointStore.restoreCheckpoint({
cwd: tmp,
checkpointRef: baselineRef,
}),
).toBe(true);
const fileSystem = yield* FileSystem.FileSystem;
expect(yield* fileSystem.readFileString(sourcePath)).toBe("before\n");
expect(yield* fileSystem.exists(createdPath)).toBe(false);
expect(yield* checkpointStore.isGitRepository(tmp)).toBe(false);
}),
);
it.effect("fails shadow checkpoint diffs when a ref is unavailable", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-shadow-invalid-ref-test-");
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const threadId = ThreadId.make("thread-shadow-invalid-ref-checkpoint-store");
const baselineRef = checkpointRefForThreadTurn(threadId, 0);
const missingRef = checkpointRefForThreadTurn(threadId, 1);
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: baselineRef });
const error = yield* Effect.flip(
checkpointStore.diffCheckpoints({
cwd: tmp,
fromCheckpointRef: baselineRef,
toCheckpointRef: missingRef,
ignoreWhitespace: false,
}),
);
expect(error._tag).toBe("VcsProcessExitError");
expect(error.message).toContain("git diff");
}),
);
it.effect("surfaces corrupted shadow checkpoint refs instead of treating them as missing", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir("checkpoint-store-shadow-corrupt-ref-test-");
const checkpointStore = yield* CheckpointStore.CheckpointStore;
const fileSystem = yield* FileSystem.FileSystem;
const config = yield* ServerConfig.ServerConfig;
const checkpointRef = checkpointRefForThreadTurn(
ThreadId.make("thread-shadow-corrupt-ref-checkpoint-store"),
0,
);
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef });
const shadowRepositoryPath = NodePath.join(
config.stateDir,
"checkpoints",
NodeCrypto.createHash("sha256").update(NodePath.resolve(tmp)).digest("hex"),
);
yield* fileSystem.writeFileString(
NodePath.join(shadowRepositoryPath, checkpointRef),
"not-a-commit\n",
);
const error = yield* Effect.flip(
checkpointStore.hasCheckpointRef({ cwd: tmp, checkpointRef }),
);
expect(error._tag).toBe("VcsProcessExitError");
expect(error.message).toContain("git for-each-ref");
const captureError = yield* Effect.flip(
checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef }),
);
expect(captureError._tag).toBe("VcsProcessExitError");
expect(
yield* Effect.flip(checkpointStore.hasCheckpointRef({ cwd: tmp, checkpointRef })),
).toMatchObject({ _tag: "VcsProcessExitError" });
}),
);
it.effect("returns full oversized checkpoint diffs without truncation", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
+19 -258
View File
@@ -1,9 +1,9 @@
/**
* CheckpointStore - Repository interface for filesystem-backed workspace checkpoints.
*
* Owns hidden checkpoint capture/restore and diff computation for a workspace
* thread timeline. Git workspaces use their repository's hidden refs; other
* workspaces use a T3-managed shadow Git store outside the project directory.
* 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.
@@ -13,21 +13,14 @@
*
* @module CheckpointStore
*/
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeCrypto from "node:crypto";
import * as NodePath from "node:path";
import { type CheckpointRef, VcsProcessExitError } from "@t3tools/contracts";
import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import type { CheckpointStoreError } from "./Errors.ts";
import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
import { ServerConfig } from "../config.ts";
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
import * as VcsProcess from "../vcs/VcsProcess.ts";
export interface CaptureCheckpointInput {
readonly cwd: string;
@@ -57,7 +50,7 @@ export interface DeleteCheckpointRefsInput {
export class CheckpointStore extends Context.Service<
CheckpointStore,
{
/** Check whether cwd is inside a user-managed Git worktree. */
/** Check whether cwd is inside a Git worktree. */
readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
/**
@@ -105,258 +98,26 @@ export class CheckpointStore extends Context.Service<
export const make = Effect.gen(function* () {
const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
const vcsProcess = yield* VcsProcess.VcsProcess;
const fileSystem = yield* FileSystem.FileSystem;
const config = yield* ServerConfig;
const shadowRepositoryPath = (cwd: string) =>
NodePath.join(
config.stateDir,
"checkpoints",
NodeCrypto.createHash("sha256").update(NodePath.resolve(cwd)).digest("hex"),
);
const canonicalizeWorkspacePath = (value: string) =>
fileSystem.realPath(value).pipe(Effect.orElseSucceed(() => NodePath.resolve(value)));
const runShadowGit = Effect.fn("CheckpointStore.runShadowGit")(function* (input: {
readonly operation: string;
readonly cwd: string;
readonly args: ReadonlyArray<string>;
readonly env?: NodeJS.ProcessEnv;
readonly allowNonZeroExit?: boolean;
readonly maxOutputBytes?: number;
}) {
const gitDir = shadowRepositoryPath(input.cwd);
yield* vcsProcess.run({
operation: `${input.operation}.init`,
command: "git",
cwd: input.cwd,
args: ["init", "--bare", "--quiet", gitDir],
});
return yield* vcsProcess.run({
operation: input.operation,
command: "git",
cwd: input.cwd,
args: [`--git-dir=${gitDir}`, `--work-tree=${NodePath.resolve(input.cwd)}`, ...input.args],
...(input.env ? { env: input.env } : {}),
...(input.allowNonZeroExit !== undefined ? { allowNonZeroExit: input.allowNonZeroExit } : {}),
...(input.maxOutputBytes !== undefined ? { maxOutputBytes: input.maxOutputBytes } : {}),
});
});
const resolveShadowCommit = Effect.fn("CheckpointStore.resolveShadowCommit")(function* (input: {
readonly cwd: string;
readonly checkpointRef: CheckpointRef;
}) {
const refResult = yield* runShadowGit({
operation: "CheckpointStore.shadow.resolveCheckpointRef",
cwd: input.cwd,
args: ["for-each-ref", "--format=%(objectname)", "--", input.checkpointRef],
allowNonZeroExit: true,
});
const refStdout = refResult.stdout.trim();
const refStderr = refResult.stderr.trim();
if (refResult.exitCode !== 0 || (refStdout.length === 0 && refStderr.length > 0)) {
return yield* new VcsProcessExitError({
operation: "CheckpointStore.shadow.resolveCheckpointRef",
command: "git for-each-ref",
cwd: input.cwd,
exitCode: refResult.exitCode === 0 ? 1 : refResult.exitCode,
detail: "Failed to resolve shadow checkpoint ref.",
stderrLength: refStderr.length,
stderrTruncated: refResult.stderrTruncated,
});
}
if (refStdout.length === 0) {
return null;
}
const result = yield* runShadowGit({
operation: "CheckpointStore.shadow.resolveCheckpointCommit",
cwd: input.cwd,
args: ["rev-parse", "--verify", "--quiet", `${input.checkpointRef}^{commit}`],
allowNonZeroExit: true,
});
const stdout = result.stdout.trim();
const stderr = result.stderr.trim();
if (result.exitCode !== 0) {
return yield* new VcsProcessExitError({
operation: "CheckpointStore.shadow.resolveCheckpointCommit",
command: "git rev-parse",
cwd: input.cwd,
exitCode: result.exitCode,
detail: `Checkpoint ref ${input.checkpointRef} does not resolve to a commit.`,
stderrLength: stderr.length,
stderrTruncated: result.stderrTruncated,
});
}
return stdout.length > 0 ? stdout : null;
});
const shadowCheckpoints: VcsCheckpointOps = {
captureCheckpoint: Effect.fn("CheckpointStore.shadow.captureCheckpoint")(function* (input) {
const gitDir = shadowRepositoryPath(input.cwd);
const tempIndexPath = NodePath.join(gitDir, `t3-checkpoint-index-${NodeCrypto.randomUUID()}`);
const env: NodeJS.ProcessEnv = {
...process.env,
GIT_INDEX_FILE: tempIndexPath,
GIT_AUTHOR_NAME: "T3 Code",
GIT_AUTHOR_EMAIL: "[email protected]",
GIT_COMMITTER_NAME: "T3 Code",
GIT_COMMITTER_EMAIL: "[email protected]",
};
const cleanup = fileSystem.remove(tempIndexPath, { force: true }).pipe(Effect.ignore);
yield* Effect.gen(function* () {
yield* runShadowGit({
operation: "CheckpointStore.shadow.captureCheckpoint.readTree",
cwd: input.cwd,
args: ["read-tree", "--empty"],
env,
});
yield* runShadowGit({
operation: "CheckpointStore.shadow.captureCheckpoint.add",
cwd: input.cwd,
args: ["add", "-A", "--", "."],
env,
});
const tree = yield* runShadowGit({
operation: "CheckpointStore.shadow.captureCheckpoint.writeTree",
cwd: input.cwd,
args: ["write-tree"],
env,
});
const commit = yield* runShadowGit({
operation: "CheckpointStore.shadow.captureCheckpoint.commitTree",
cwd: input.cwd,
args: [
"commit-tree",
tree.stdout.trim(),
"-m",
`t3 checkpoint ref=${input.checkpointRef}`,
],
env,
});
yield* runShadowGit({
operation: "CheckpointStore.shadow.captureCheckpoint.updateRef",
cwd: input.cwd,
args: ["update-ref", input.checkpointRef, commit.stdout.trim()],
});
}).pipe(Effect.ensuring(cleanup));
}),
hasCheckpointRef: (input) =>
resolveShadowCommit(input).pipe(Effect.map((commit) => commit !== null)),
restoreCheckpoint: Effect.fn("CheckpointStore.shadow.restoreCheckpoint")(function* (input) {
const commit = yield* resolveShadowCommit(input);
if (!commit) {
// Shadow repositories intentionally have no HEAD: every restorable
// state must be addressed by its explicit checkpoint ref. Fail closed
// so callers never roll conversation history back without matching files.
return false;
}
const tempIndexPath = NodePath.join(
shadowRepositoryPath(input.cwd),
`t3-checkpoint-index-${NodeCrypto.randomUUID()}`,
);
const env: NodeJS.ProcessEnv = { ...process.env, GIT_INDEX_FILE: tempIndexPath };
const cleanup = fileSystem.remove(tempIndexPath, { force: true }).pipe(Effect.ignore);
yield* Effect.gen(function* () {
yield* runShadowGit({
operation: "CheckpointStore.shadow.restoreCheckpoint.readTree",
cwd: input.cwd,
args: ["read-tree", commit],
env,
});
yield* runShadowGit({
operation: "CheckpointStore.shadow.restoreCheckpoint.checkout",
cwd: input.cwd,
args: ["checkout-index", "--all", "--force"],
env,
});
yield* runShadowGit({
operation: "CheckpointStore.shadow.restoreCheckpoint.clean",
cwd: input.cwd,
args: ["clean", "-fd", "--", "."],
env,
});
}).pipe(Effect.ensuring(cleanup));
return true;
}),
diffCheckpoints: Effect.fn("CheckpointStore.shadow.diffCheckpoints")(function* (input) {
const result = yield* runShadowGit({
operation: "CheckpointStore.shadow.diffCheckpoints",
cwd: input.cwd,
args: [
"diff",
"--patch",
"--no-color",
"--no-ext-diff",
"--no-textconv",
...(input.ignoreWhitespace ? ["--ignore-all-space"] : []),
`${input.fromCheckpointRef}^{commit}`,
`${input.toCheckpointRef}^{commit}`,
],
allowNonZeroExit: true,
maxOutputBytes: 50 * 1024 * 1024,
});
if (result.exitCode !== 0) {
return yield* new VcsProcessExitError({
operation: "CheckpointStore.shadow.diffCheckpoints",
command: "git diff",
cwd: input.cwd,
exitCode: result.exitCode,
detail: "Checkpoint ref is unavailable for diff operation.",
stderrLength: result.stderr.trim().length,
stderrTruncated: result.stderrTruncated,
});
}
return result.stdout;
}),
deleteCheckpointRefs: Effect.fn("CheckpointStore.shadow.deleteCheckpointRefs")(
function* (input) {
yield* Effect.forEach(
input.checkpointRefs,
(checkpointRef) =>
runShadowGit({
operation: "CheckpointStore.shadow.deleteCheckpointRefs",
cwd: input.cwd,
args: ["update-ref", "-d", checkpointRef],
allowNonZeroExit: true,
}),
{ concurrency: 1, discard: true },
);
},
),
};
const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
_operation: string,
operation: string,
cwd: string,
) {
const handle = yield* vcsRegistry.detect({ cwd });
const workspaceOwnsRepository =
handle !== null &&
(yield* canonicalizeWorkspacePath(handle.repository.rootPath)) ===
(yield* canonicalizeWorkspacePath(cwd));
return workspaceOwnsRepository
? (handle.driver.checkpoints ?? shadowCheckpoints)
: shadowCheckpoints;
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"] = Effect.fn(
"CheckpointStore.isGitRepository",
)(function* (cwd) {
const repository = yield* vcsRegistry.detect({ cwd, requestedKind: "git" });
return (
repository !== null &&
(yield* canonicalizeWorkspacePath(repository.repository.rootPath)) ===
(yield* canonicalizeWorkspacePath(cwd))
);
});
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",
@@ -35,7 +35,7 @@ import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts";
import * as VcsProcess from "../../vcs/VcsProcess.ts";
import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
import * as RepositoryIdentityResolver from "../../project/RepositoryIdentityResolver.ts";
import { CheckpointReactorLive, conversationTurnCountForTurn } from "./CheckpointReactor.ts";
import { CheckpointReactorLive } from "./CheckpointReactor.ts";
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
@@ -229,24 +229,6 @@ function gitShowFileAtRef(cwd: string, ref: string, filePath: string): string {
return runGit(cwd, ["show", `${ref}:${filePath}`]);
}
describe("conversationTurnCountForTurn", () => {
it("counts steering prompts through the last assistant message for the same turn", () => {
const steeredTurnId = asTurnId("steered-turn");
const laterTurnId = asTurnId("later-turn");
const messages = [
{ role: "user", turnId: null },
{ role: "assistant", turnId: steeredTurnId },
{ role: "user", turnId: null },
{ role: "assistant", turnId: steeredTurnId },
{ role: "user", turnId: null },
{ role: "assistant", turnId: laterTurnId },
];
expect(conversationTurnCountForTurn(messages, steeredTurnId)).toBe(2);
expect(conversationTurnCountForTurn(messages, laterTurnId)).toBe(3);
});
});
async function waitForGitRefExists(cwd: string, ref: string, timeoutMs = 15_000) {
const deadline = (await Effect.runPromise(Clock.currentTimeMillis)) + timeoutMs;
const poll = async (): Promise<void> => {
@@ -431,7 +413,6 @@ describe("CheckpointReactor", () => {
return {
engine,
runPromise: <A, E>(effect: Effect.Effect<A, E>) => runtime!.runPromise(effect),
readModel: () => Effect.runPromise(snapshotQuery.getSnapshot()),
provider,
cwd,
@@ -443,7 +424,7 @@ describe("CheckpointReactor", () => {
const harness = await createHarness({ seedFilesystemCheckpoints: false });
const createdAt = "2026-01-01T00:00:00.000Z";
await harness.runPromise(
await Effect.runPromise(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-session-set-capture"),
@@ -805,81 +786,6 @@ describe("CheckpointReactor", () => {
).toBe("v2\n");
});
it("keeps the authoritative turn count when a placeholder is processed late", async () => {
const harness = await createHarness({ seedFilesystemCheckpoints: false });
const threadId = ThreadId.make("thread-1");
const createdAt = "2026-01-01T00:00:00.000Z";
for (const turnNumber of [1, 2]) {
const turnId = asTurnId(`late-placeholder-turn-${turnNumber}`);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make(`cmd-late-placeholder-user-${turnNumber}`),
threadId,
message: {
messageId: MessageId.make(`late-placeholder-user-${turnNumber}`),
role: "user",
text: `User ${turnNumber}`,
attachments: [],
},
runtimeMode: "approval-required",
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.message.assistant.delta",
commandId: CommandId.make(`cmd-late-placeholder-assistant-delta-${turnNumber}`),
threadId,
messageId: MessageId.make(`late-placeholder-assistant-${turnNumber}`),
delta: `Assistant ${turnNumber}`,
turnId,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.message.assistant.complete",
commandId: CommandId.make(`cmd-late-placeholder-assistant-complete-${turnNumber}`),
threadId,
messageId: MessageId.make(`late-placeholder-assistant-${turnNumber}`),
turnId,
createdAt,
}),
);
}
await harness.runPromise(
harness.engine.dispatch({
type: "thread.turn.diff.complete",
commandId: CommandId.make("cmd-late-placeholder-diff"),
threadId,
turnId: asTurnId("late-placeholder-turn-1"),
completedAt: createdAt,
checkpointRef: checkpointRefForThreadTurn(threadId, 1),
status: "missing",
files: [],
checkpointTurnCount: 1,
createdAt,
}),
);
const thread = await waitForThread(
harness.readModel,
(entry) =>
entry.checkpoints.some((checkpoint) => checkpoint.checkpointTurnCount === 1) &&
entry.activities.some((activity) => activity.kind === "checkpoint.captured"),
);
expect(thread.checkpoints.some((checkpoint) => checkpoint.checkpointTurnCount === 1)).toBe(
true,
);
expect(thread.checkpoints.some((checkpoint) => checkpoint.checkpointTurnCount === 2)).toBe(
false,
);
});
it("ignores non-v2 checkpoint.captured runtime events", async () => {
const harness = await createHarness();
const createdAt = "2026-01-01T00:00:00.000Z";
@@ -922,7 +828,7 @@ describe("CheckpointReactor", () => {
);
});
it("captures completed turns in a non-Git workspace and continues processing", async () => {
it("continues processing runtime events after a single checkpoint runtime failure", async () => {
const nonRepositorySessionCwd = NodeFS.mkdtempSync(
NodePath.join(NodeOS.tmpdir(), "t3-checkpoint-runtime-non-repo-"),
);
@@ -973,12 +879,12 @@ describe("CheckpointReactor", () => {
turnId: asTurnId("turn-after-runtime-failure"),
});
const thread = await waitForThread(harness.readModel, (entry) =>
entry.checkpoints.some((checkpoint) => checkpoint.checkpointTurnCount === 1),
await waitForGitRefExists(
harness.cwd,
checkpointRefForThreadTurn(ThreadId.make("thread-1"), 0),
);
expect(thread.checkpoints).toHaveLength(1);
expect(
thread.activities.some((activity) => activity.kind === "checkpoint.capture.failed"),
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.make("thread-1"), 0)),
).toBe(true);
});
@@ -1063,247 +969,6 @@ describe("CheckpointReactor", () => {
).toBe(false);
});
it("reverts legacy predecessor turns in mixed checkpoint history", async () => {
const harness = await createHarness();
const threadId = ThreadId.make("thread-1");
const createdAt = "2026-01-01T00:00:00.000Z";
await harness.runPromise(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-legacy-session-set"),
threadId,
session: {
threadId,
status: "ready",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: createdAt,
},
createdAt,
}),
);
for (const turnNumber of [1, 2, 3]) {
const turnId = asTurnId(`legacy-turn-${turnNumber}`);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make(`cmd-legacy-user-${turnNumber}`),
threadId,
message: {
messageId: MessageId.make(`legacy-user-${turnNumber}`),
role: "user",
text: `User ${turnNumber}`,
attachments: [],
},
runtimeMode: "approval-required",
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.message.assistant.delta",
commandId: CommandId.make(`cmd-legacy-assistant-delta-${turnNumber}`),
threadId,
messageId: MessageId.make(`legacy-assistant-${turnNumber}`),
delta: `Assistant ${turnNumber}`,
turnId,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.message.assistant.complete",
commandId: CommandId.make(`cmd-legacy-assistant-complete-${turnNumber}`),
threadId,
messageId: MessageId.make(`legacy-assistant-${turnNumber}`),
turnId,
createdAt,
}),
);
}
await harness.runPromise(
harness.engine.dispatch({
type: "thread.turn.diff.complete",
commandId: CommandId.make("cmd-mixed-checkpoint"),
threadId,
turnId: asTurnId("legacy-turn-3"),
completedAt: createdAt,
checkpointRef: checkpointRefForThreadTurn(threadId, 1),
status: "ready",
files: [],
checkpointTurnCount: 1,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.checkpoint.revert",
commandId: CommandId.make("cmd-legacy-revert-request"),
threadId,
turnCount: 2,
createdAt,
}),
);
await waitForEvent(harness.engine, (event) => event.type === "thread.reverted");
expect(harness.provider.rollbackConversation).toHaveBeenCalledWith({
threadId,
numTurns: 1,
});
});
it("rewinds legacy conversation history when no filesystem checkpoints exist", async () => {
const harness = await createHarness({ seedFilesystemCheckpoints: false });
const threadId = ThreadId.make("thread-1");
const createdAt = "2026-01-01T00:00:00.000Z";
await harness.runPromise(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-legacy-only-session-set"),
threadId,
session: {
threadId,
status: "ready",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: createdAt,
},
createdAt,
}),
);
for (const turnNumber of [1, 2]) {
const turnId = asTurnId(`legacy-only-turn-${turnNumber}`);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make(`cmd-legacy-only-user-${turnNumber}`),
threadId,
message: {
messageId: MessageId.make(`legacy-only-user-${turnNumber}`),
role: "user",
text: `User ${turnNumber}`,
attachments: [],
},
runtimeMode: "approval-required",
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.message.assistant.delta",
commandId: CommandId.make(`cmd-legacy-only-assistant-delta-${turnNumber}`),
threadId,
messageId: MessageId.make(`legacy-only-assistant-${turnNumber}`),
delta: `Assistant ${turnNumber}`,
turnId,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.message.assistant.complete",
commandId: CommandId.make(`cmd-legacy-only-assistant-complete-${turnNumber}`),
threadId,
messageId: MessageId.make(`legacy-only-assistant-${turnNumber}`),
turnId,
createdAt,
}),
);
}
runGit(harness.cwd, ["update-ref", "-d", checkpointRefForThreadTurn(threadId, 1)]);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.checkpoint.revert",
commandId: CommandId.make("cmd-legacy-only-revert"),
threadId,
turnCount: 1,
createdAt,
}),
);
await waitForEvent(harness.engine, (event) => event.type === "thread.reverted");
expect(harness.provider.rollbackConversation).toHaveBeenCalledWith({
threadId,
numTurns: 1,
});
});
it("does not rewind conversation when a shadow filesystem checkpoint is missing", async () => {
const workspace = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-shadow-revert-"));
tempDirs.push(workspace);
NodeFS.writeFileSync(NodePath.join(workspace, "keep.txt"), "unchanged\n", "utf8");
const harness = await createHarness({
seedFilesystemCheckpoints: false,
projectWorkspaceRoot: workspace,
threadWorktreePath: workspace,
providerSessionCwd: workspace,
});
const threadId = ThreadId.make("thread-1");
const createdAt = "2026-01-01T00:00:00.000Z";
await harness.runPromise(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-shadow-missing-session-set"),
threadId,
session: {
threadId,
status: "ready",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: createdAt,
},
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.turn.diff.complete",
commandId: CommandId.make("cmd-shadow-missing-checkpoint-summary"),
threadId,
turnId: asTurnId("shadow-missing-turn"),
completedAt: createdAt,
checkpointRef: checkpointRefForThreadTurn(threadId, 1),
status: "ready",
files: [],
checkpointTurnCount: 1,
createdAt,
}),
);
await harness.runPromise(
harness.engine.dispatch({
type: "thread.checkpoint.revert",
commandId: CommandId.make("cmd-shadow-missing-revert"),
threadId,
turnCount: 0,
createdAt,
}),
);
const thread = await waitForThread(harness.readModel, (entry) =>
entry.activities.some((activity) => activity.kind === "checkpoint.revert.failed"),
);
expect(thread.checkpoints).toHaveLength(1);
expect(harness.provider.rollbackConversation).not.toHaveBeenCalled();
expect(NodeFS.readFileSync(NodePath.join(workspace, "keep.txt"), "utf8")).toBe("unchanged\n");
});
it("executes provider revert and emits thread.reverted for claude sessions", async () => {
const harness = await createHarness({ providerName: ProviderDriverKind.make("claudeAgent") });
const createdAt = "2026-01-01T00:00:00.000Z";
@@ -32,6 +32,7 @@ import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts"
import { RuntimeReceiptBus } from "../Services/RuntimeReceiptBus.ts";
import type { CheckpointStoreError } from "../../checkpointing/Errors.ts";
import type { OrchestrationDispatchError } from "../Errors.ts";
import { isGitRepository } from "../../git/Utils.ts";
import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
import * as WorkspaceEntries from "../../workspace/WorkspaceEntries.ts";
@@ -58,39 +59,6 @@ function sameId(left: string | null | undefined, right: string | null | undefine
return left === right;
}
export function countConversationTurns(messages: ReadonlyArray<{ readonly role: string }>): number {
return messages.reduce((count, message) => count + (message.role === "user" ? 1 : 0), 0);
}
export function conversationTurnCountForTurn(
messages: ReadonlyArray<{
readonly role: string;
readonly turnId: TurnId | null;
}>,
turnId: TurnId,
): number | undefined {
let turnCount = 0;
let matchedTurnCount: number | undefined;
for (const message of messages) {
if (message.role === "user") {
turnCount += 1;
}
if (message.role === "assistant" && sameId(message.turnId, turnId)) {
matchedTurnCount = turnCount > 0 ? turnCount : undefined;
}
}
return matchedTurnCount;
}
function maxCheckpointTurnCount(
checkpoints: ReadonlyArray<{ readonly checkpointTurnCount: number }>,
): number {
return checkpoints.reduce(
(maxTurnCount, checkpoint) => Math.max(maxTurnCount, checkpoint.checkpointTurnCount),
0,
);
}
function checkpointStatusFromRuntime(status: string | undefined): "ready" | "missing" | "error" {
switch (status) {
case "failed":
@@ -206,10 +174,12 @@ const make = Effect.gen(function* () {
return project ? [project] : [];
});
const isGitWorkspace = (cwd: string) => isGitRepository(cwd);
// Resolves the workspace CWD for checkpoint operations, preferring the
// active provider session CWD and falling back to the thread/project config.
// CheckpointStore supports both user Git repositories and T3-managed shadow
// repositories, so a project does not need to initialize Git first.
// Returns undefined when no CWD can be determined or the workspace is not
// a git repository.
const resolveCheckpointCwd = Effect.fn("resolveCheckpointCwd")(function* (input: {
readonly threadId: ThreadId;
readonly thread: { readonly projectId: ProjectId; readonly worktreePath: string | null };
@@ -236,6 +206,9 @@ const make = Effect.gen(function* () {
if (!cwd) {
return undefined;
}
if (!isGitWorkspace(cwd)) {
return undefined;
}
return cwd;
});
@@ -262,21 +235,10 @@ const make = Effect.gen(function* () {
const fromCheckpointRef = checkpointRefForThreadTurn(input.threadId, fromTurnCount);
const targetCheckpointRef = checkpointRefForThreadTurn(input.threadId, input.turnCount);
const fromCheckpointExists = yield* checkpointStore
.hasCheckpointRef({
cwd: input.cwd,
checkpointRef: fromCheckpointRef,
})
.pipe(
Effect.catchTags({
VcsProcessExitError: (error) =>
Effect.logWarning("previous checkpoint ref probe failed", {
checkpointRef: fromCheckpointRef,
cwd: input.cwd,
detail: error.message,
}).pipe(Effect.as(false)),
}),
);
const fromCheckpointExists = yield* checkpointStore.hasCheckpointRef({
cwd: input.cwd,
checkpointRef: fromCheckpointRef,
});
if (!fromCheckpointExists) {
yield* Effect.logWarning("checkpoint capture missing pre-turn baseline", {
threadId: input.threadId,
@@ -431,10 +393,13 @@ const make = Effect.gen(function* () {
const existingPlaceholder = thread.checkpoints.find(
(checkpoint) => checkpoint.turnId === turnId && checkpoint.status === "missing",
);
const currentTurnCount = maxCheckpointTurnCount(thread.checkpoints);
const currentTurnCount = thread.checkpoints.reduce(
(maxTurnCount, checkpoint) => Math.max(maxTurnCount, checkpoint.checkpointTurnCount),
0,
);
const nextTurnCount = existingPlaceholder
? existingPlaceholder.checkpointTurnCount
: (conversationTurnCountForTurn(thread.messages, turnId) ?? currentTurnCount + 1);
: currentTurnCount + 1;
yield* captureAndDispatchCheckpoint({
threadId: thread.id,
@@ -534,9 +499,9 @@ const make = Effect.gen(function* () {
return;
}
const currentTurnCount = Math.max(
maxCheckpointTurnCount(thread.checkpoints),
Math.max(0, countConversationTurns(thread.messages) - 1),
const currentTurnCount = thread.checkpoints.reduce(
(maxTurnCount, checkpoint) => Math.max(maxTurnCount, checkpoint.checkpointTurnCount),
0,
);
const baselineCheckpointRef = checkpointRefForThreadTurn(thread.id, currentTurnCount);
const baselineExists = yield* checkpointStore.hasCheckpointRef({
@@ -616,13 +581,9 @@ const make = Effect.gen(function* () {
return;
}
const projectedConversationTurnCount = countConversationTurns(thread.messages);
const currentTurnCount = Math.max(
maxCheckpointTurnCount(thread.checkpoints),
event.type === "thread.message-sent" &&
thread.messages.some((message) => message.id === event.payload.messageId)
? Math.max(0, projectedConversationTurnCount - 1)
: projectedConversationTurnCount,
const currentTurnCount = thread.checkpoints.reduce(
(maxTurnCount, checkpoint) => Math.max(maxTurnCount, checkpoint.checkpointTurnCount),
0,
);
const baselineCheckpointRef = checkpointRefForThreadTurn(threadId, currentTurnCount);
const baselineExists = yield* checkpointStore.hasCheckpointRef({
@@ -672,8 +633,20 @@ const make = Effect.gen(function* () {
}).pipe(Effect.catch(() => Effect.void));
return;
}
const checkpointTurnCount = maxCheckpointTurnCount(thread.checkpoints);
const currentTurnCount = Math.max(checkpointTurnCount, countConversationTurns(thread.messages));
if (!isGitWorkspace(sessionRuntime.value.cwd)) {
yield* appendRevertFailureActivity({
threadId: event.payload.threadId,
turnCount: event.payload.turnCount,
detail: "Checkpoints are unavailable because this project is not a git repository.",
createdAt: now,
}).pipe(Effect.catch(() => Effect.void));
return;
}
const currentTurnCount = thread.checkpoints.reduce(
(maxTurnCount, checkpoint) => Math.max(maxTurnCount, checkpoint.checkpointTurnCount),
0,
);
if (event.payload.turnCount > currentTurnCount) {
yield* appendRevertFailureActivity({
@@ -685,37 +658,42 @@ const make = Effect.gen(function* () {
return;
}
// Legacy/imported threads can predate filesystem checkpoints entirely. They
// still support conversation-only rewind. Once a thread has checkpoint
// summaries, however, fail closed if the matching filesystem state cannot
// be restored so conversation and workspace never diverge silently.
if (checkpointTurnCount > 0) {
const summarizedCheckpoint = thread.checkpoints.find(
(checkpoint) => checkpoint.checkpointTurnCount === event.payload.turnCount,
);
const targetCheckpointRef =
summarizedCheckpoint?.checkpointRef ??
checkpointRefForThreadTurn(event.payload.threadId, event.payload.turnCount);
const restored = yield* checkpointStore.restoreCheckpoint({
cwd: sessionRuntime.value.cwd,
checkpointRef: targetCheckpointRef,
fallbackToHead: event.payload.turnCount === 0,
});
if (!restored) {
yield* appendRevertFailureActivity({
threadId: event.payload.threadId,
turnCount: event.payload.turnCount,
detail: `Filesystem checkpoint is unavailable for turn ${event.payload.turnCount}. Conversation history was not changed.`,
createdAt: now,
}).pipe(Effect.catch(() => Effect.void));
return;
}
const targetCheckpointRef =
event.payload.turnCount === 0
? checkpointRefForThreadTurn(event.payload.threadId, 0)
: thread.checkpoints.find(
(checkpoint) => checkpoint.checkpointTurnCount === event.payload.turnCount,
)?.checkpointRef;
// Refresh the workspace entry index so the @-mention file picker reflects
// the reverted filesystem state before conversation history is rewound.
yield* workspaceEntries.refresh(sessionRuntime.value.cwd);
if (!targetCheckpointRef) {
yield* appendRevertFailureActivity({
threadId: event.payload.threadId,
turnCount: event.payload.turnCount,
detail: `Checkpoint ref for turn ${event.payload.turnCount} is unavailable in read model.`,
createdAt: now,
}).pipe(Effect.catch(() => Effect.void));
return;
}
const restored = yield* checkpointStore.restoreCheckpoint({
cwd: sessionRuntime.value.cwd,
checkpointRef: targetCheckpointRef,
fallbackToHead: event.payload.turnCount === 0,
});
if (!restored) {
yield* appendRevertFailureActivity({
threadId: event.payload.threadId,
turnCount: event.payload.turnCount,
detail: `Filesystem checkpoint is unavailable for turn ${event.payload.turnCount}.`,
createdAt: now,
}).pipe(Effect.catch(() => Effect.void));
return;
}
// Refresh the workspace entry index so the @-mention file picker
// reflects the reverted filesystem state.
yield* workspaceEntries.refresh(sessionRuntime.value.cwd);
const rolledBackTurns = Math.max(0, currentTurnCount - event.payload.turnCount);
if (rolledBackTurns > 0) {
yield* providerService.rollbackConversation({
@@ -788,28 +788,6 @@ it.layer(
},
});
yield* appendAndProject({
type: "thread.message-sent",
eventId: EventId.make("evt-revert-files-user-keep"),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: "2026-01-01T00:00:00.100Z",
commandId: CommandId.make("cmd-revert-files-user-keep"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-revert-files-user-keep"),
metadata: {},
payload: {
threadId,
messageId: MessageId.make("message-user-keep"),
role: "user",
text: "Keep this turn",
turnId: null,
streaming: false,
createdAt: "2026-01-01T00:00:00.100Z",
updatedAt: "2026-01-01T00:00:00.100Z",
},
});
yield* appendAndProject({
type: "thread.turn-diff-completed",
eventId: EventId.make("evt-revert-files-3"),
@@ -858,8 +836,8 @@ it.layer(
],
turnId: TurnId.make("turn-keep"),
streaming: false,
createdAt: "2026-01-01T00:00:00.200Z",
updatedAt: "2026-01-01T00:00:00.200Z",
createdAt: now,
updatedAt: now,
},
});
@@ -885,28 +863,6 @@ it.layer(
},
});
yield* appendAndProject({
type: "thread.message-sent",
eventId: EventId.make("evt-revert-files-user-remove"),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: "2026-01-01T00:00:00.300Z",
commandId: CommandId.make("cmd-revert-files-user-remove"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-revert-files-user-remove"),
metadata: {},
payload: {
threadId,
messageId: MessageId.make("message-user-remove"),
role: "user",
text: "Revert here",
turnId: null,
streaming: false,
createdAt: "2026-01-01T00:00:00.300Z",
updatedAt: "2026-01-01T00:00:00.300Z",
},
});
yield* appendAndProject({
type: "thread.message-sent",
eventId: EventId.make("evt-revert-files-6"),
@@ -933,8 +889,8 @@ it.layer(
],
turnId: TurnId.make("turn-remove"),
streaming: false,
createdAt: "2026-01-01T00:00:00.400Z",
updatedAt: "2026-01-01T00:00:00.400Z",
createdAt: now,
updatedAt: now,
},
});
@@ -2237,7 +2193,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
}),
);
it.effect("preserves an uncheckpointed predecessor when reverting mixed history", () =>
it.effect("does not fallback-retain messages whose turnId is removed by revert", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
const eventStore = yield* OrchestrationEventStore;
@@ -2295,24 +2251,24 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
});
yield* appendAndProject({
type: "thread.message-sent",
eventId: EventId.make("evt-revert-user-keep"),
type: "thread.turn-diff-completed",
eventId: EventId.make("evt-revert-3"),
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-revert"),
occurredAt: "2026-02-26T12:00:02.050Z",
commandId: CommandId.make("cmd-revert-user-keep"),
occurredAt: "2026-02-26T12:00:02.000Z",
commandId: CommandId.make("cmd-revert-3"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-revert-user-keep"),
correlationId: CorrelationId.make("cmd-revert-3"),
metadata: {},
payload: {
threadId: ThreadId.make("thread-revert"),
messageId: MessageId.make("user-keep"),
role: "user",
text: "start",
turnId: null,
streaming: false,
createdAt: "2026-02-26T12:00:02.050Z",
updatedAt: "2026-02-26T12:00:02.050Z",
turnId: TurnId.make("turn-1"),
checkpointTurnCount: 1,
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-revert/turn/1"),
status: "ready",
files: [],
assistantMessageId: MessageId.make("assistant-keep"),
completedAt: "2026-02-26T12:00:02.000Z",
},
});
@@ -2351,63 +2307,15 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
payload: {
threadId: ThreadId.make("thread-revert"),
turnId: TurnId.make("turn-2"),
checkpointTurnCount: 1,
checkpointTurnCount: 2,
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-revert/turn/2"),
status: "ready",
files: [],
assistantMessageId: null,
assistantMessageId: MessageId.make("assistant-remove"),
completedAt: "2026-02-26T12:00:03.000Z",
},
});
yield* appendAndProject({
type: "thread.activity-appended",
eventId: EventId.make("evt-revert-checkpoint-activity"),
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-revert"),
occurredAt: "2026-02-26T12:00:03.010Z",
commandId: CommandId.make("cmd-revert-checkpoint-activity"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-revert-checkpoint-activity"),
metadata: {},
payload: {
threadId: ThreadId.make("thread-revert"),
activity: {
id: EventId.make("checkpoint-only-activity"),
tone: "tool",
kind: "command",
summary: "Retained checkpoint activity",
payload: {},
turnId: TurnId.make("turn-2"),
createdAt: "2026-02-26T12:00:03.010Z",
},
},
});
yield* appendAndProject({
type: "thread.proposed-plan-upserted",
eventId: EventId.make("evt-revert-checkpoint-plan"),
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-revert"),
occurredAt: "2026-02-26T12:00:03.020Z",
commandId: CommandId.make("cmd-revert-checkpoint-plan"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-revert-checkpoint-plan"),
metadata: {},
payload: {
threadId: ThreadId.make("thread-revert"),
proposedPlan: {
id: "checkpoint-only-plan",
turnId: TurnId.make("turn-2"),
planMarkdown: "## Retained checkpoint plan",
implementedAt: null,
implementationThreadId: null,
createdAt: "2026-02-26T12:00:03.020Z",
updatedAt: "2026-02-26T12:00:03.020Z",
},
},
});
yield* appendAndProject({
type: "thread.message-sent",
eventId: EventId.make("evt-revert-6"),
@@ -2423,7 +2331,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
messageId: MessageId.make("user-remove"),
role: "user",
text: "removed",
turnId: null,
turnId: TurnId.make("turn-2"),
streaming: false,
createdAt: "2026-02-26T12:00:03.050Z",
updatedAt: "2026-02-26T12:00:03.050Z",
@@ -2445,7 +2353,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
messageId: MessageId.make("assistant-remove"),
role: "assistant",
text: "removed",
turnId: TurnId.make("turn-1"),
turnId: TurnId.make("turn-2"),
streaming: false,
createdAt: "2026-02-26T12:00:03.100Z",
updatedAt: "2026-02-26T12:00:03.100Z",
@@ -2482,44 +2390,12 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
ORDER BY created_at ASC, message_id ASC
`;
assert.deepEqual(messageRows, [
{
messageId: "user-keep",
turnId: null,
role: "user",
},
{
messageId: "assistant-keep",
turnId: "turn-1",
role: "assistant",
},
{
messageId: "user-remove",
turnId: null,
role: "user",
},
]);
const turnRows = yield* sql<{ readonly turnId: string }>`
SELECT turn_id AS "turnId"
FROM projection_turns
WHERE thread_id = 'thread-revert'
ORDER BY requested_at ASC, turn_id ASC
`;
assert.deepEqual(turnRows, [{ turnId: "turn-1" }, { turnId: "turn-2" }]);
const activityRows = yield* sql<{ readonly activityId: string }>`
SELECT activity_id AS "activityId"
FROM projection_thread_activities
WHERE thread_id = 'thread-revert'
`;
assert.deepEqual(activityRows, [{ activityId: "checkpoint-only-activity" }]);
const planRows = yield* sql<{ readonly planId: string }>`
SELECT plan_id AS "planId"
FROM projection_thread_proposed_plans
WHERE thread_id = 'thread-revert'
`;
assert.deepEqual(planRows, [{ planId: "checkpoint-only-plan" }]);
}),
);
});
@@ -205,47 +205,86 @@ function deriveHasActionableProposedPlan(input: {
function retainProjectionMessagesAfterRevert(
messages: ReadonlyArray<ProjectionThreadMessage>,
turnCount: number,
): ReadonlyArray<ProjectionThreadMessage> {
let userMessageIndex = 0;
let reachedTargetUser = false;
return messages.filter((message) => {
if (message.role === "system") {
return true;
}
if (reachedTargetUser) {
return false;
}
if (message.role === "user") {
reachedTargetUser = userMessageIndex === turnCount;
userMessageIndex += 1;
}
return true;
});
}
function retainProjectionTurnsAfterRevert(
turns: ReadonlyArray<ProjectionTurn>,
turnCount: number,
): ReadonlyArray<ProjectionTurn> {
const sortedTurns = turns
.filter((turn) => turn.turnId !== null)
.toSorted(
(left, right) =>
left.requestedAt.localeCompare(right.requestedAt) ||
(left.turnId ?? "").localeCompare(right.turnId ?? ""),
);
const retainedTurnIds = new Set(
sortedTurns
.slice(0, turnCount)
.concat(
sortedTurns.filter(
(turn) => turn.checkpointTurnCount !== null && turn.checkpointTurnCount <= turnCount,
),
)
.flatMap((turn) => (turn.turnId === null ? [] : [turn.turnId])),
): ReadonlyArray<ProjectionThreadMessage> {
const retainedMessageIds = new Set<string>();
const retainedTurnIds = new Set<string>();
const keptTurns = turns.filter(
(turn) =>
turn.turnId !== null &&
turn.checkpointTurnCount !== null &&
turn.checkpointTurnCount <= turnCount,
);
return sortedTurns.filter((turn) => turn.turnId !== null && retainedTurnIds.has(turn.turnId));
for (const turn of keptTurns) {
if (turn.turnId !== null) {
retainedTurnIds.add(turn.turnId);
}
if (turn.pendingMessageId !== null) {
retainedMessageIds.add(turn.pendingMessageId);
}
if (turn.assistantMessageId !== null) {
retainedMessageIds.add(turn.assistantMessageId);
}
}
for (const message of messages) {
if (message.role === "system") {
retainedMessageIds.add(message.messageId);
continue;
}
if (message.turnId !== null && retainedTurnIds.has(message.turnId)) {
retainedMessageIds.add(message.messageId);
}
}
const retainedUserCount = messages.filter(
(message) => message.role === "user" && retainedMessageIds.has(message.messageId),
).length;
const missingUserCount = Math.max(0, turnCount - retainedUserCount);
if (missingUserCount > 0) {
const fallbackUserMessages = messages
.filter(
(message) =>
message.role === "user" &&
!retainedMessageIds.has(message.messageId) &&
(message.turnId === null || retainedTurnIds.has(message.turnId)),
)
.toSorted(
(left, right) =>
left.createdAt.localeCompare(right.createdAt) ||
left.messageId.localeCompare(right.messageId),
)
.slice(0, missingUserCount);
for (const message of fallbackUserMessages) {
retainedMessageIds.add(message.messageId);
}
}
const retainedAssistantCount = messages.filter(
(message) => message.role === "assistant" && retainedMessageIds.has(message.messageId),
).length;
const missingAssistantCount = Math.max(0, turnCount - retainedAssistantCount);
if (missingAssistantCount > 0) {
const fallbackAssistantMessages = messages
.filter(
(message) =>
message.role === "assistant" &&
!retainedMessageIds.has(message.messageId) &&
(message.turnId === null || retainedTurnIds.has(message.turnId)),
)
.toSorted(
(left, right) =>
left.createdAt.localeCompare(right.createdAt) ||
left.messageId.localeCompare(right.messageId),
)
.slice(0, missingAssistantCount);
for (const message of fallbackAssistantMessages) {
retainedMessageIds.add(message.messageId);
}
}
return messages.filter((message) => retainedMessageIds.has(message.messageId));
}
function retainProjectionActivitiesAfterRevert(
@@ -254,9 +293,14 @@ function retainProjectionActivitiesAfterRevert(
turnCount: number,
): ReadonlyArray<ProjectionThreadActivity> {
const retainedTurnIds = new Set<string>(
retainProjectionTurnsAfterRevert(turns, turnCount).flatMap((turn) =>
turn.turnId === null ? [] : [turn.turnId],
),
turns
.filter(
(turn) =>
turn.turnId !== null &&
turn.checkpointTurnCount !== null &&
turn.checkpointTurnCount <= turnCount,
)
.flatMap((turn) => (turn.turnId === null ? [] : [turn.turnId])),
);
return activities.filter(
(activity) => activity.turnId === null || retainedTurnIds.has(activity.turnId),
@@ -269,9 +313,14 @@ function retainProjectionProposedPlansAfterRevert(
turnCount: number,
): ReadonlyArray<ProjectionThreadProposedPlan> {
const retainedTurnIds = new Set<string>(
retainProjectionTurnsAfterRevert(turns, turnCount).flatMap((turn) =>
turn.turnId === null ? [] : [turn.turnId],
),
turns
.filter(
(turn) =>
turn.turnId !== null &&
turn.checkpointTurnCount !== null &&
turn.checkpointTurnCount <= turnCount,
)
.flatMap((turn) => (turn.turnId === null ? [] : [turn.turnId])),
);
return proposedPlans.filter(
(proposedPlan) => proposedPlan.turnId === null || retainedTurnIds.has(proposedPlan.turnId),
@@ -723,22 +772,19 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
return;
}
const existingTurns = yield* projectionTurnRepository.listByThreadId({
const retainedTurns = yield* projectionTurnRepository.listByThreadId({
threadId: event.payload.threadId,
});
const retainedTurns = retainProjectionTurnsAfterRevert(
existingTurns,
event.payload.turnCount,
);
let latestTurnId: ProjectionTurn["turnId"] = null;
let latestCheckpointTurnCount = -1;
for (let index = 0; index < retainedTurns.length; index += 1) {
const turn = retainedTurns[index];
if (!turn || turn.turnId === null) {
continue;
}
if (turn.checkpointTurnCount === null) {
latestTurnId = turn.turnId;
if (
!turn ||
turn.turnId === null ||
turn.checkpointTurnCount === null ||
turn.checkpointTurnCount > event.payload.turnCount
) {
continue;
}
if (turn.checkpointTurnCount > latestCheckpointTurnCount) {
@@ -810,8 +856,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
return;
}
const existingTurns = yield* projectionTurnRepository.listByThreadId({
threadId: event.payload.threadId,
});
const keptRows = retainProjectionMessagesAfterRevert(
existingRows,
existingTurns,
event.payload.turnCount,
);
if (keptRows.length === existingRows.length) {
@@ -1255,9 +1305,11 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
const existingTurns = yield* projectionTurnRepository.listByThreadId({
threadId: event.payload.threadId,
});
const keptTurns = retainProjectionTurnsAfterRevert(
existingTurns,
event.payload.turnCount,
const keptTurns = existingTurns.filter(
(turn) =>
turn.turnId !== null &&
turn.checkpointTurnCount !== null &&
turn.checkpointTurnCount <= event.payload.turnCount,
);
yield* projectionTurnRepository.deleteByThreadId({
threadId: event.payload.threadId,
+38 -85
View File
@@ -484,7 +484,7 @@ describe("orchestration projector", () => {
expect(message?.updatedAt).toBe(completeAt);
});
it("preserves an uncheckpointed predecessor when reverting mixed history", async () => {
it("prunes reverted turn messages from in-memory thread snapshot", async () => {
const createdAt = "2026-02-23T10:00:00.000Z";
const model = createEmptyReadModel(createdAt);
@@ -555,6 +555,24 @@ describe("orchestration projector", () => {
}),
makeEvent({
sequence: 4,
type: "thread.turn-diff-completed",
aggregateKind: "thread",
aggregateId: "thread-1",
occurredAt: "2026-02-23T10:00:02.500Z",
commandId: "cmd-turn-1-complete",
payload: {
threadId: "thread-1",
turnId: "turn-1",
checkpointTurnCount: 1,
checkpointRef: "refs/t3/checkpoints/thread-1/turn/1",
status: "ready",
files: [],
assistantMessageId: "assistant-msg-1",
completedAt: "2026-02-23T10:00:02.500Z",
},
}),
makeEvent({
sequence: 5,
type: "thread.activity-appended",
aggregateKind: "thread",
aggregateId: "thread-1",
@@ -574,7 +592,7 @@ describe("orchestration projector", () => {
},
}),
makeEvent({
sequence: 5,
sequence: 6,
type: "thread.message-sent",
aggregateKind: "thread",
aggregateId: "thread-1",
@@ -592,7 +610,7 @@ describe("orchestration projector", () => {
},
}),
makeEvent({
sequence: 6,
sequence: 7,
type: "thread.message-sent",
aggregateKind: "thread",
aggregateId: "thread-1",
@@ -610,7 +628,7 @@ describe("orchestration projector", () => {
},
}),
makeEvent({
sequence: 7,
sequence: 8,
type: "thread.turn-diff-completed",
aggregateKind: "thread",
aggregateId: "thread-1",
@@ -628,7 +646,7 @@ describe("orchestration projector", () => {
},
}),
makeEvent({
sequence: 8,
sequence: 9,
type: "thread.activity-appended",
aggregateKind: "thread",
aggregateId: "thread-1",
@@ -648,7 +666,7 @@ describe("orchestration projector", () => {
},
}),
makeEvent({
sequence: 9,
sequence: 10,
type: "thread.reverted",
aggregateKind: "thread",
aggregateId: "thread-1",
@@ -672,17 +690,16 @@ describe("orchestration projector", () => {
[
{ role: "user", text: "First edit" },
{ role: "assistant", text: "Updated README to v2.\n" },
{ role: "user", text: "Second edit" },
],
);
expect(
thread?.activities.map((activity) => ({ id: activity.id, turnId: activity.turnId })),
).toEqual([{ id: "activity-1", turnId: "turn-1" }]);
expect(thread?.checkpoints).toEqual([]);
expect(thread?.latestTurn).toBeNull();
expect(thread?.checkpoints.map((checkpoint) => checkpoint.checkpointTurnCount)).toEqual([1]);
expect(thread?.latestTurn?.turnId).toBe("turn-1");
});
it("removes assistant output produced after the selected steering message", async () => {
it("does not fallback-retain messages tied to removed turn IDs", async () => {
const createdAt = "2026-02-26T12:00:00.000Z";
const model = createEmptyReadModel(createdAt);
@@ -717,24 +734,6 @@ describe("orchestration projector", () => {
const events: ReadonlyArray<OrchestrationEvent> = [
makeEvent({
sequence: 2,
type: "thread.message-sent",
aggregateKind: "thread",
aggregateId: "thread-revert",
occurredAt: "2026-02-26T12:00:00.500Z",
commandId: "cmd-user-keep",
payload: {
threadId: "thread-revert",
messageId: "user-keep",
role: "user",
text: "start",
turnId: null,
streaming: false,
createdAt: "2026-02-26T12:00:00.500Z",
updatedAt: "2026-02-26T12:00:00.500Z",
},
}),
makeEvent({
sequence: 3,
type: "thread.turn-diff-completed",
aggregateKind: "thread",
aggregateId: "thread-revert",
@@ -742,17 +741,17 @@ describe("orchestration projector", () => {
commandId: "cmd-turn-1",
payload: {
threadId: "thread-revert",
turnId: "checkpoint-only-turn",
turnId: "turn-1",
checkpointTurnCount: 1,
checkpointRef: "refs/t3/checkpoints/thread-revert/turn/1",
status: "ready",
files: [],
assistantMessageId: null,
assistantMessageId: "assistant-keep",
completedAt: "2026-02-26T12:00:01.000Z",
},
}),
makeEvent({
sequence: 4,
sequence: 3,
type: "thread.message-sent",
aggregateKind: "thread",
aggregateId: "thread-revert",
@@ -770,47 +769,7 @@ describe("orchestration projector", () => {
},
}),
makeEvent({
sequence: 5,
type: "thread.activity-appended",
aggregateKind: "thread",
aggregateId: "thread-revert",
occurredAt: "2026-02-26T12:00:01.200Z",
commandId: "cmd-checkpoint-only-activity",
payload: {
threadId: "thread-revert",
activity: {
id: "checkpoint-only-activity",
tone: "tool",
kind: "command",
summary: "Retained activity",
payload: {},
turnId: "checkpoint-only-turn",
createdAt: "2026-02-26T12:00:01.200Z",
},
},
}),
makeEvent({
sequence: 6,
type: "thread.proposed-plan-upserted",
aggregateKind: "thread",
aggregateId: "thread-revert",
occurredAt: "2026-02-26T12:00:01.300Z",
commandId: "cmd-checkpoint-only-plan",
payload: {
threadId: "thread-revert",
proposedPlan: {
id: "checkpoint-only-plan",
turnId: "checkpoint-only-turn",
planMarkdown: "## Retained plan",
implementedAt: null,
implementationThreadId: null,
createdAt: "2026-02-26T12:00:01.300Z",
updatedAt: "2026-02-26T12:00:01.300Z",
},
},
}),
makeEvent({
sequence: 7,
sequence: 4,
type: "thread.turn-diff-completed",
aggregateKind: "thread",
aggregateId: "thread-revert",
@@ -823,12 +782,12 @@ describe("orchestration projector", () => {
checkpointRef: "refs/t3/checkpoints/thread-revert/turn/2",
status: "ready",
files: [],
assistantMessageId: null,
assistantMessageId: "assistant-remove",
completedAt: "2026-02-26T12:00:02.000Z",
},
}),
makeEvent({
sequence: 8,
sequence: 5,
type: "thread.message-sent",
aggregateKind: "thread",
aggregateId: "thread-revert",
@@ -839,14 +798,14 @@ describe("orchestration projector", () => {
messageId: "user-remove",
role: "user",
text: "removed",
turnId: null,
turnId: "turn-2",
streaming: false,
createdAt: "2026-02-26T12:00:02.050Z",
updatedAt: "2026-02-26T12:00:02.050Z",
},
}),
makeEvent({
sequence: 9,
sequence: 6,
type: "thread.message-sent",
aggregateKind: "thread",
aggregateId: "thread-revert",
@@ -857,14 +816,14 @@ describe("orchestration projector", () => {
messageId: "assistant-remove",
role: "assistant",
text: "removed",
turnId: "turn-1",
turnId: "turn-2",
streaming: false,
createdAt: "2026-02-26T12:00:02.100Z",
updatedAt: "2026-02-26T12:00:02.100Z",
},
}),
makeEvent({
sequence: 10,
sequence: 7,
type: "thread.reverted",
aggregateKind: "thread",
aggregateId: "thread-revert",
@@ -890,13 +849,7 @@ describe("orchestration projector", () => {
role: message.role,
turnId: message.turnId,
})),
).toEqual([
{ id: "user-keep", role: "user", turnId: null },
{ id: "assistant-keep", role: "assistant", turnId: "turn-1" },
{ id: "user-remove", role: "user", turnId: null },
]);
expect(thread?.activities.map((activity) => activity.id)).toEqual(["checkpoint-only-activity"]);
expect(thread?.proposedPlans.map((plan) => plan.id)).toEqual(["checkpoint-only-plan"]);
).toEqual([{ id: "assistant-keep", role: "assistant", turnId: "turn-1" }]);
});
it("caps message and checkpoint retention for long-lived threads", async () => {
+55 -15
View File
@@ -82,23 +82,65 @@ function decodeForEvent<A>(
function retainThreadMessagesAfterRevert(
messages: ReadonlyArray<OrchestrationMessage>,
retainedTurnIds: ReadonlySet<string>,
turnCount: number,
): ReadonlyArray<OrchestrationMessage> {
let userMessageIndex = 0;
let reachedTargetUser = false;
return messages.filter((message) => {
const retainedMessageIds = new Set<string>();
for (const message of messages) {
if (message.role === "system") {
return true;
retainedMessageIds.add(message.id);
continue;
}
if (reachedTargetUser) {
return false;
if (message.turnId !== null && retainedTurnIds.has(message.turnId)) {
retainedMessageIds.add(message.id);
}
if (message.role === "user") {
reachedTargetUser = userMessageIndex === turnCount;
userMessageIndex += 1;
}
const retainedUserCount = messages.filter(
(message) => message.role === "user" && retainedMessageIds.has(message.id),
).length;
const missingUserCount = Math.max(0, turnCount - retainedUserCount);
if (missingUserCount > 0) {
const fallbackUserMessages = messages
.filter(
(message) =>
message.role === "user" &&
!retainedMessageIds.has(message.id) &&
(message.turnId === null || retainedTurnIds.has(message.turnId)),
)
.toSorted(
(left, right) =>
left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id),
)
.slice(0, missingUserCount);
for (const message of fallbackUserMessages) {
retainedMessageIds.add(message.id);
}
return true;
});
}
const retainedAssistantCount = messages.filter(
(message) => message.role === "assistant" && retainedMessageIds.has(message.id),
).length;
const missingAssistantCount = Math.max(0, turnCount - retainedAssistantCount);
if (missingAssistantCount > 0) {
const fallbackAssistantMessages = messages
.filter(
(message) =>
message.role === "assistant" &&
!retainedMessageIds.has(message.id) &&
(message.turnId === null || retainedTurnIds.has(message.turnId)),
)
.toSorted(
(left, right) =>
left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id),
)
.slice(0, missingAssistantCount);
for (const message of fallbackAssistantMessages) {
retainedMessageIds.add(message.id);
}
}
return messages.filter((message) => retainedMessageIds.has(message.id));
}
function retainThreadActivitiesAfterRevert(
@@ -581,14 +623,12 @@ export function projectEvent(
.filter((entry) => entry.checkpointTurnCount <= payload.turnCount)
.toSorted((left, right) => left.checkpointTurnCount - right.checkpointTurnCount)
.slice(-MAX_THREAD_CHECKPOINTS);
const retainedTurnIds = new Set(checkpoints.map((checkpoint) => checkpoint.turnId));
const messages = retainThreadMessagesAfterRevert(
thread.messages,
retainedTurnIds,
payload.turnCount,
).slice(-MAX_THREAD_MESSAGES);
const retainedTurnIds = new Set([
...checkpoints.map((checkpoint) => checkpoint.turnId),
...messages.flatMap((message) => (message.turnId === null ? [] : [message.turnId])),
]);
const proposedPlans = retainThreadProposedPlansAfterRevert(
thread.proposedPlans,
retainedTurnIds,
+9 -61
View File
@@ -747,62 +747,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
return false;
}
const headExists = yield* hasHeadCommit(input.cwd);
if (!headExists) {
const gitCommonDir = yield* resolveGitCommonDir(input.cwd);
const tempIndexPath = path.join(
gitCommonDir,
`t3-checkpoint-restore-index-${NodeCrypto.randomUUID()}`,
);
const restoreEnv: NodeJS.ProcessEnv = {
...process.env,
GIT_INDEX_FILE: tempIndexPath,
};
const cleanupTempIndex = fileSystem
.remove(tempIndexPath, { force: true })
.pipe(Effect.ignore);
yield* Effect.gen(function* () {
// The real index is empty in an unborn repository. Clean before
// restoring so untracked paths cannot block the checkout.
yield* execute({
operation,
cwd: input.cwd,
args: ["clean", "-fd", "--", "."],
});
const snapshotEntries = yield* execute({
operation,
cwd: input.cwd,
args: ["ls-tree", "-r", "--name-only", commitOid],
});
if (snapshotEntries.stdout.trim().length > 0) {
yield* execute({
operation,
cwd: input.cwd,
args: ["restore", "--source", commitOid, "--worktree", "--", "."],
});
}
// Load the checkpoint tree into a temporary index before the final
// clean. Snapshot files are then protected as tracked, while paths
// ignored only by post-checkpoint rules are removed safely without -x.
yield* execute({
operation,
cwd: input.cwd,
args: ["read-tree", commitOid],
env: restoreEnv,
});
yield* execute({
operation,
cwd: input.cwd,
args: ["clean", "-fd", "--", "."],
env: restoreEnv,
});
}).pipe(Effect.ensuring(cleanupTempIndex));
return true;
}
yield* execute({
operation,
cwd: input.cwd,
@@ -813,11 +757,15 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
cwd: input.cwd,
args: ["clean", "-fd", "--", "."],
});
yield* execute({
operation,
cwd: input.cwd,
args: ["reset", "--quiet", "--", "."],
});
const headExists = yield* hasHeadCommit(input.cwd);
if (headExists) {
yield* execute({
operation,
cwd: input.cwd,
args: ["reset", "--quiet", "--", "."],
});
}
return true;
}),
@@ -1,5 +1,4 @@
import {
CheckpointRef,
EnvironmentId,
MessageId,
ProjectId,
@@ -17,7 +16,6 @@ import {
buildThreadTurnInterruptInput,
createLocalDispatchSnapshot,
deriveComposerSendState,
deriveRevertTurnCountByUserMessageId,
getStartedThreadModelChangeBlockReason,
hasServerAcknowledgedLocalDispatch,
reconcileMountedTerminalThreadIds,
@@ -26,101 +24,6 @@ import {
shouldWriteThreadErrorToCurrentServerThread,
} from "./ChatView.logic";
function timelineMessage(
id: string,
role: "user" | "assistant",
turnId: TurnId | null,
createdAt: string,
) {
return {
id: `entry-${id}`,
kind: "message" as const,
createdAt,
message: {
id: MessageId.make(id),
role,
text: id,
turnId,
createdAt,
updatedAt: createdAt,
streaming: false,
},
};
}
describe("deriveRevertTurnCountByUserMessageId", () => {
const user = timelineMessage("user-1", "user", null, "2026-03-29T00:00:00.000Z");
const assistant = timelineMessage(
"assistant-1",
"assistant",
TurnId.make("turn-1"),
"2026-03-29T00:00:01.000Z",
);
it("falls back to the stable turn id when the assistant message id is absent", () => {
const result = deriveRevertTurnCountByUserMessageId({
timelineEntries: [user, assistant],
checkpoints: [
{
turnId: TurnId.make("turn-1"),
checkpointTurnCount: 1,
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-1/turn/1"),
status: "ready",
files: [],
assistantMessageId: null,
completedAt: assistant.createdAt,
},
],
});
expect(result.get(user.message.id)).toBe(0);
});
it("uses conversation order for completed legacy turns without checkpoints", () => {
const secondUser = timelineMessage("user-2", "user", null, "2026-03-29T00:00:02.000Z");
const secondAssistant = timelineMessage(
"assistant-2",
"assistant",
TurnId.make("turn-2"),
"2026-03-29T00:00:03.000Z",
);
const result = deriveRevertTurnCountByUserMessageId({
timelineEntries: [user, assistant, secondUser, secondAssistant],
checkpoints: [],
});
expect(result.get(user.message.id)).toBe(0);
expect(result.get(secondUser.message.id)).toBe(1);
});
it("uses conversation order for unmatched legacy turns in mixed checkpoint histories", () => {
const secondUser = timelineMessage("user-2", "user", null, "2026-03-29T00:00:02.000Z");
const secondAssistant = timelineMessage(
"assistant-2",
"assistant",
TurnId.make("turn-2"),
"2026-03-29T00:00:03.000Z",
);
const result = deriveRevertTurnCountByUserMessageId({
timelineEntries: [user, assistant, secondUser, secondAssistant],
checkpoints: [
{
turnId: TurnId.make("turn-2"),
checkpointTurnCount: 2,
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-1/turn/2"),
status: "ready",
files: [],
assistantMessageId: secondAssistant.message.id,
completedAt: secondAssistant.createdAt,
},
],
});
expect(result.get(user.message.id)).toBe(0);
expect(result.get(secondUser.message.id)).toBe(1);
});
});
const environmentId = EnvironmentId.make("environment-local");
const projectId = ProjectId.make("project-1");
const threadId = ThreadId.make("thread-1");
+1 -58
View File
@@ -9,8 +9,7 @@ import {
type ThreadId,
type TurnId,
} from "@t3tools/contracts";
import { type ChatMessage, type SessionPhase, type Thread, type TurnDiffSummary } from "../types";
import type { TimelineEntry } from "../session-logic";
import { type ChatMessage, type SessionPhase, type Thread } from "../types";
import { type ComposerImageAttachment, type DraftThreadState } from "../composerDraftStore";
import * as Schema from "effect/Schema";
import { appAtomRegistry } from "../rpc/atomRegistry";
@@ -28,62 +27,6 @@ export const MAX_HIDDEN_MOUNTED_PREVIEW_THREADS = 3;
export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
export function deriveRevertTurnCountByUserMessageId(input: {
timelineEntries: ReadonlyArray<TimelineEntry>;
checkpoints: ReadonlyArray<TurnDiffSummary>;
}): Map<ChatMessage["id"], number> {
const result = new Map<ChatMessage["id"], number>();
const checkpointByAssistantMessageId = new Map(
input.checkpoints.flatMap((checkpoint) =>
checkpoint.assistantMessageId === null
? []
: ([[checkpoint.assistantMessageId, checkpoint]] as const),
),
);
const checkpointByTurnId = new Map(
input.checkpoints.map((checkpoint) => [checkpoint.turnId, checkpoint] as const),
);
let userTurnIndex = 0;
for (let index = 0; index < input.timelineEntries.length; index += 1) {
const entry = input.timelineEntries[index];
if (!entry || entry.kind !== "message" || entry.message.role !== "user") {
continue;
}
for (let nextIndex = index + 1; nextIndex < input.timelineEntries.length; nextIndex += 1) {
const nextEntry = input.timelineEntries[nextIndex];
if (!nextEntry || nextEntry.kind !== "message") {
continue;
}
if (nextEntry.message.role === "user") {
break;
}
if (nextEntry.message.role !== "assistant") {
continue;
}
const checkpoint =
checkpointByAssistantMessageId.get(nextEntry.message.id) ??
(nextEntry.message.turnId === null
? undefined
: checkpointByTurnId.get(nextEntry.message.turnId));
if (checkpoint) {
result.set(entry.message.id, userTurnIndex);
break;
}
if (!nextEntry.message.streaming) {
result.set(entry.message.id, userTurnIndex);
break;
}
}
userTurnIndex += 1;
}
return result;
}
export function buildLocalDraftThread(
threadId: ThreadId,
draftThread: DraftThreadState,
+37 -14
View File
@@ -231,7 +231,6 @@ import {
collectUserMessageBlobPreviewUrls,
createLocalDispatchSnapshot,
deriveComposerSendState,
deriveRevertTurnCountByUserMessageId,
hasServerAcknowledgedLocalDispatch,
getStartedThreadModelChangeBlockReason,
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
@@ -2209,7 +2208,8 @@ function ChatViewContent(props: ChatViewProps) {
attachDraftHeroComposerAnchorRef,
captureDraftHeroComposerRect,
] = useDraftHeroLayoutTransition(isDraftHeroState);
const { turnDiffSummaries } = useTurnDiffSummaries(activeThread);
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
useTurnDiffSummaries(activeThread);
const turnDiffSummaryByAssistantMessageId = useMemo(() => {
const byMessageId = new Map<MessageId, TurnDiffSummary>();
for (const summary of turnDiffSummaries) {
@@ -2219,11 +2219,37 @@ function ChatViewContent(props: ChatViewProps) {
return byMessageId;
}, [turnDiffSummaries]);
const revertTurnCountByUserMessageId = useMemo(() => {
return deriveRevertTurnCountByUserMessageId({
timelineEntries,
checkpoints: turnDiffSummaries,
});
}, [timelineEntries, turnDiffSummaries]);
const byUserMessageId = new Map<MessageId, number>();
for (let index = 0; index < timelineEntries.length; index += 1) {
const entry = timelineEntries[index];
if (!entry || entry.kind !== "message" || entry.message.role !== "user") {
continue;
}
for (let nextIndex = index + 1; nextIndex < timelineEntries.length; nextIndex += 1) {
const nextEntry = timelineEntries[nextIndex];
if (!nextEntry || nextEntry.kind !== "message") {
continue;
}
if (nextEntry.message.role === "user") {
break;
}
const summary = turnDiffSummaryByAssistantMessageId.get(nextEntry.message.id);
if (!summary) {
continue;
}
const turnCount =
summary.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[summary.turnId];
if (typeof turnCount !== "number") {
break;
}
byUserMessageId.set(entry.message.id, Math.max(0, turnCount - 1));
break;
}
}
return byUserMessageId;
}, [inferredCheckpointTurnCountByTurnId, timelineEntries, turnDiffSummaryByAssistantMessageId]);
const gitCwd = activeProject
? projectScriptCwd({
@@ -3946,21 +3972,18 @@ function ChatViewContent(props: ChatViewProps) {
if (activeEnvironmentUnavailable && activeEnvironmentUnavailableLabel) {
setThreadError(
activeThread.id,
`Reconnect ${activeEnvironmentUnavailableLabel} before reverting this conversation.`,
`Reconnect ${activeEnvironmentUnavailableLabel} before reverting checkpoints.`,
);
return;
}
if (phase === "running" || isSendBusy || isConnecting) {
setThreadError(
activeThread.id,
"Interrupt the current turn before reverting this conversation.",
);
setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");
return;
}
const confirmed = await localApi.dialogs.confirm(
[
"Revert this conversation to this message?",
"This will discard newer messages and restore workspace files when a snapshot is available.",
`Revert this thread to checkpoint ${turnCount}?`,
"This will discard newer messages and turn diffs in this thread.",
"This action cannot be undone.",
].join("\n"),
);
+11 -12
View File
@@ -224,6 +224,10 @@ import {
type SidebarProjectSnapshot,
} from "../sidebarProjectGrouping";
import { SidebarProviderUpdatePill } from "./sidebar/SidebarProviderUpdatePill";
import {
type SidebarThreadListAutoAnimateRef,
useSidebarThreadListAutoAnimateRef,
} from "./sidebar/useSidebarThreadListAutoAnimate";
const SIDEBAR_SORT_LABELS: Record<SidebarProjectSortOrder, string> = {
updated_at: "Last user message",
created_at: "Created at",
@@ -913,7 +917,7 @@ interface SidebarProjectThreadListProps {
confirmingArchiveThreadKey: string | null;
setConfirmingArchiveThreadKey: React.Dispatch<React.SetStateAction<string | null>>;
confirmArchiveButtonRefs: React.RefObject<Map<string, HTMLButtonElement>>;
attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
handleThreadClick: (
event: React.MouseEvent,
threadRef: ScopedThreadRef,
@@ -982,7 +986,9 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList(
return (
<SidebarMenuSub
ref={attachThreadListAutoAnimateRef}
ref={(node) => {
attachThreadListAutoAnimateRef(node, renderedThreads.length);
}}
className="mx-0.5 my-0 w-full translate-x-0 gap-0.5 overflow-hidden px-1 py-0 sm:mx-1 sm:px-1.5"
>
{shouldShowThreadPanel && showEmptyThreadState ? (
@@ -1075,7 +1081,7 @@ interface SidebarProjectItemProps {
archiveThread: ReturnType<typeof useThreadActions>["archiveThread"];
deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];
threadJumpLabelByKey: ReadonlyMap<string, string>;
attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
expandThreadListForProject: (projectKey: string) => void;
collapseThreadListForProject: (projectKey: string) => void;
dragInProgressRef: React.RefObject<boolean>;
@@ -2921,7 +2927,7 @@ interface SidebarProjectsContentProps {
newThreadShortcutLabel: string | null;
commandPaletteShortcutLabel: string | null;
threadJumpLabelByKey: ReadonlyMap<string, string>;
attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
expandThreadListForProject: (projectKey: string) => void;
collapseThreadListForProject: (projectKey: string) => void;
dragInProgressRef: React.RefObject<boolean>;
@@ -3419,14 +3425,7 @@ export default function Sidebar() {
animatedProjectListsRef.current.add(node);
}, []);
const animatedThreadListsRef = useRef(new WeakSet<HTMLElement>());
const attachThreadListAutoAnimateRef = useCallback((node: HTMLElement | null) => {
if (!node || animatedThreadListsRef.current.has(node)) {
return;
}
autoAnimate(node, SIDEBAR_LIST_ANIMATION_OPTIONS);
animatedThreadListsRef.current.add(node);
}, []);
const attachThreadListAutoAnimateRef = useSidebarThreadListAutoAnimateRef();
const visibleThreads = useMemo(
() => sidebarThreads.filter((thread) => thread.archivedAt === null),
@@ -271,21 +271,6 @@ describe("MessagesTimeline", () => {
expect(markup).toContain("1 changed file");
});
it("renders a disabled revert action while a user turn has no checkpoint yet", async () => {
const { MessagesTimeline } = await import("./MessagesTimeline");
const markup = renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
isWorking
activeTurnInProgress
timelineEntries={[buildUserTimelineEntry("Current prompt.")]}
/>,
);
expect(markup).toContain('aria-label="Revert to this message"');
expect(markup).toContain("disabled");
});
it("uses LegendList isNearEnd when deciding whether the live edge is visible", async () => {
const {
resolveTimelineIsAtEnd,
@@ -328,8 +328,6 @@ export const MessagesTimeline = memo(function MessagesTimeline({
const [timelineViewportElement, setTimelineViewportElement] = useState<HTMLDivElement | null>(
null,
);
const timelineViewportWidthRef = useRef<number | null>(null);
const [timelineLayoutGeneration, setTimelineLayoutGeneration] = useState(0);
const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false);
const [minimapHitStripWidth, setMinimapHitStripWidth] = useState(0);
const handleAnchorReady = useCallback(
@@ -397,62 +395,25 @@ export const MessagesTimeline = memo(function MessagesTimeline({
return;
}
let layoutRefreshTimeout: number | undefined;
const measure = (forceLayoutRefresh = false) => {
const viewportWidth = Math.round(timelineViewportElement.getBoundingClientRect().width);
const measure = () => {
const viewportWidth = timelineViewportElement.getBoundingClientRect().width;
const nextHasPersistentGutter = resolveTimelineMinimapHasPersistentGutter(viewportWidth);
setMinimapHasPersistentGutter((current) =>
current === nextHasPersistentGutter ? current : nextHasPersistentGutter,
);
setMinimapHitStripWidth(resolveTimelineMinimapHitStripWidth(viewportWidth));
if (viewportWidth <= 0) {
return;
}
if (timelineViewportWidthRef.current === null) {
timelineViewportWidthRef.current =
viewportWidth > 0 ? viewportWidth : timelineViewportWidthRef.current;
return;
}
if (!forceLayoutRefresh && viewportWidth === timelineViewportWidthRef.current) {
return;
}
timelineViewportWidthRef.current = viewportWidth;
if (layoutRefreshTimeout !== undefined) {
window.clearTimeout(layoutRefreshTimeout);
}
layoutRefreshTimeout = window.setTimeout(() => {
// LegendList caches both row measurements and recycled container widths. A desktop
// window resize can briefly make the chat only a few pixels wide. Clearing sizes alone
// leaves some recycled rows one-character-wide, so remount the list after resize settles.
flushSync(() => {
setTimelineLayoutGeneration((generation) => generation + 1);
});
}, 120);
};
const handleViewportResize = () => measure(true);
const frame = requestAnimationFrame(() => measure());
const frame = requestAnimationFrame(measure);
const observer = new ResizeObserver(() => measure());
const observer = new ResizeObserver(measure);
observer.observe(timelineViewportElement);
window.addEventListener("resize", handleViewportResize);
window.visualViewport?.addEventListener("resize", handleViewportResize);
const pollId = window.setInterval(() => measure(), 250);
return () => {
cancelAnimationFrame(frame);
if (layoutRefreshTimeout !== undefined) {
window.clearTimeout(layoutRefreshTimeout);
}
observer.disconnect();
window.removeEventListener("resize", handleViewportResize);
window.visualViewport?.removeEventListener("resize", handleViewportResize);
window.clearInterval(pollId);
};
}, [timelineViewportElement]);
}, [timelineViewportElement, rows.length]);
const sharedState = useMemo<TimelineRowSharedState>(
() => ({
@@ -521,13 +482,8 @@ export const MessagesTimeline = memo(function MessagesTimeline({
return (
<TimelineRowCtx value={sharedState}>
<TimelineRowActivityCtx value={activityState}>
<div
ref={setTimelineViewportElement}
className="relative h-full min-h-0"
data-timeline-layout-generation={timelineLayoutGeneration}
>
<div ref={setTimelineViewportElement} className="relative h-full min-h-0">
<LegendList<MessagesTimelineRow>
key={timelineLayoutGeneration}
ref={listRef}
data={rows}
keyExtractor={keyExtractor}
@@ -999,7 +955,7 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
</TooltipPopup>
</Tooltip>
<div className="flex items-center gap-0.5">
<RevertUserMessageButton messageId={row.message.id} canRevert={canRevertAgentWork} />
{canRevertAgentWork && <RevertUserMessageButton messageId={row.message.id} />}
{displayedUserMessage.copyText && (
<MessageCopyButton text={displayedUserMessage.copyText} variant="ghost" />
)}
@@ -1010,37 +966,27 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
);
}
function RevertUserMessageButton({
messageId,
canRevert,
}: {
messageId: MessageId;
canRevert: boolean;
}) {
function RevertUserMessageButton({ messageId }: { messageId: MessageId }) {
const ctx = use(TimelineRowCtx);
const activity = use(TimelineRowActivityCtx);
const disabled = activity.isRevertingCheckpoint || activity.isWorking || !canRevert;
const tooltip = activity.isWorking
? "Stop the current turn before reverting"
: canRevert
? "Revert to this message"
: "Revert is unavailable until this turn finishes";
return (
<Tooltip>
<TooltipTrigger render={<span className="inline-flex" />}>
<Button
type="button"
size="xs"
variant="ghost"
disabled={disabled}
onClick={() => ctx.onRevertUserMessage(messageId)}
aria-label="Revert to this message"
>
<Undo2Icon className="size-3" />
</Button>
<TooltipTrigger
render={
<Button
type="button"
size="xs"
variant="ghost"
disabled={activity.isRevertingCheckpoint || activity.isWorking}
onClick={() => ctx.onRevertUserMessage(messageId)}
aria-label="Revert to this message"
/>
}
>
<Undo2Icon className="size-3" />
</TooltipTrigger>
<TooltipPopup side="top">{tooltip}</TooltipPopup>
<TooltipPopup side="top">Revert to this message</TooltipPopup>
</Tooltip>
);
}
@@ -0,0 +1,90 @@
import type { AnimationController } from "@formkit/auto-animate";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const testState = vi.hoisted(() => ({
autoAnimate: vi.fn(),
}));
vi.mock("react", async (importOriginal) => {
const actual = await importOriginal<typeof import("react")>();
return {
...actual,
useCallback: <T,>(callback: T): T => callback,
useRef: <T,>(initialValue: T): { current: T } => ({ current: initialValue }),
};
});
vi.mock("@formkit/auto-animate", () => ({
autoAnimate: testState.autoAnimate,
}));
import {
MAX_ANIMATED_SIDEBAR_THREAD_ROWS,
shouldAnimateSidebarThreadList,
useSidebarThreadListAutoAnimateRef,
} from "./useSidebarThreadListAutoAnimate";
function makeController(): AnimationController {
return {
parent: {} as Element,
enable: vi.fn(),
disable: vi.fn(),
isEnabled: vi.fn(() => true),
destroy: vi.fn(),
};
}
describe("useSidebarThreadListAutoAnimateRef", () => {
beforeEach(() => {
testState.autoAnimate.mockReset();
});
it("keeps animation enabled for ordinary sidebar thread lists", () => {
const controller = makeController();
testState.autoAnimate.mockReturnValue(controller);
const attach = useSidebarThreadListAutoAnimateRef();
const node = {} as HTMLElement;
attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS);
expect(testState.autoAnimate).toHaveBeenCalledOnce();
expect(controller.enable).toHaveBeenCalledOnce();
expect(controller.disable).not.toHaveBeenCalled();
});
it("bypasses animations when the list grows beyond the threshold", () => {
const controller = makeController();
testState.autoAnimate.mockReturnValue(controller);
const attach = useSidebarThreadListAutoAnimateRef();
const node = {} as HTMLElement;
attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS);
attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1);
expect(testState.autoAnimate).toHaveBeenCalledOnce();
expect(controller.disable).toHaveBeenCalledOnce();
expect(controller.enable).toHaveBeenCalledOnce();
});
it("keeps animation disabled through a bulk removal before restoring it", async () => {
const controller = makeController();
testState.autoAnimate.mockReturnValue(controller);
const attach = useSidebarThreadListAutoAnimateRef();
const node = {} as HTMLElement;
attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1);
attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS - 1);
expect(controller.disable).toHaveBeenCalledTimes(2);
expect(controller.enable).not.toHaveBeenCalled();
await Promise.resolve();
expect(controller.enable).toHaveBeenCalledOnce();
});
it("uses the threshold as the exact animation boundary", () => {
expect(shouldAnimateSidebarThreadList(MAX_ANIMATED_SIDEBAR_THREAD_ROWS)).toBe(true);
expect(shouldAnimateSidebarThreadList(MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1)).toBe(false);
});
});
@@ -0,0 +1,70 @@
import {
autoAnimate,
type AnimationController,
type AutoAnimateOptions,
} from "@formkit/auto-animate";
import { useCallback, useRef } from "react";
export const MAX_ANIMATED_SIDEBAR_THREAD_ROWS = 20;
const SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS = {
duration: 180,
easing: "ease-out",
} as const satisfies Partial<AutoAnimateOptions>;
export function shouldAnimateSidebarThreadList(visibleRowCount: number): boolean {
return visibleRowCount <= MAX_ANIMATED_SIDEBAR_THREAD_ROWS;
}
export type SidebarThreadListAutoAnimateRef = (
node: HTMLElement | null,
visibleRowCount: number,
) => void;
interface SidebarThreadListAnimationState {
controller: AnimationController;
visibleRowCount: number;
}
export function useSidebarThreadListAutoAnimateRef(): SidebarThreadListAutoAnimateRef {
const animationStateRef = useRef(new WeakMap<HTMLElement, SidebarThreadListAnimationState>());
return useCallback((node: HTMLElement | null, visibleRowCount: number) => {
if (!node) return;
let animationState = animationStateRef.current.get(node);
if (!animationState) {
animationState = {
controller: autoAnimate(node, SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS),
visibleRowCount,
};
animationStateRef.current.set(node, animationState);
}
const previousRowCount = animationState.visibleRowCount;
animationState.visibleRowCount = visibleRowCount;
if (
shouldAnimateSidebarThreadList(previousRowCount) &&
shouldAnimateSidebarThreadList(visibleRowCount)
) {
animationState.controller.enable();
return;
}
animationState.controller.disable();
if (!shouldAnimateSidebarThreadList(visibleRowCount)) return;
// Keep the controller disabled until the MutationObserver has processed the bulk removal,
// then restore normal animation behavior for later changes to the now-small list.
const stateForEnable = animationState;
queueMicrotask(() => {
const currentState = animationStateRef.current.get(node);
if (
currentState === stateForEnable &&
shouldAnimateSidebarThreadList(currentState.visibleRowCount)
) {
currentState.controller.enable();
}
});
}, []);
}
@@ -599,320 +599,6 @@ describe("applyThreadDetailEvent", () => {
});
describe("thread.reverted", () => {
it("retains earlier turns and the selected legacy user prompt", () => {
const legacyThread: OrchestrationThread = {
...baseThread,
messages: [
{
id: MessageId.make("legacy-user-1"),
role: "user",
text: "First",
turnId: null,
streaming: false,
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
{
id: MessageId.make("legacy-assistant-1"),
role: "assistant",
text: "Response 1",
turnId: TurnId.make("legacy-turn-1"),
streaming: false,
createdAt: "2026-04-01T02:00:00.000Z",
updatedAt: "2026-04-01T02:00:00.000Z",
},
{
id: MessageId.make("legacy-user-2"),
role: "user",
text: "Second",
turnId: null,
streaming: false,
createdAt: "2026-04-01T03:00:00.000Z",
updatedAt: "2026-04-01T03:00:00.000Z",
},
{
id: MessageId.make("legacy-assistant-2"),
role: "assistant",
text: "Response 2",
turnId: TurnId.make("legacy-turn-2"),
streaming: false,
createdAt: "2026-04-01T04:00:00.000Z",
updatedAt: "2026-04-01T04:00:00.000Z",
},
],
checkpoints: [],
};
const result = applyThreadDetailEvent(legacyThread, {
...baseEventFields,
sequence: 14,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.reverted",
payload: {
threadId: ThreadId.make("thread-1"),
turnCount: 1,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.messages.map((message) => message.id)).toEqual([
MessageId.make("legacy-user-1"),
MessageId.make("legacy-assistant-1"),
MessageId.make("legacy-user-2"),
]);
}
});
it("retains uncheckpointed predecessor responses in mixed histories", () => {
const mixedThread: OrchestrationThread = {
...baseThread,
messages: [
{
id: MessageId.make("mixed-user-1"),
role: "user",
text: "First",
turnId: null,
streaming: false,
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
{
id: MessageId.make("mixed-assistant-1"),
role: "assistant",
text: "Response 1",
turnId: TurnId.make("mixed-turn-1"),
streaming: false,
createdAt: "2026-04-01T02:00:00.000Z",
updatedAt: "2026-04-01T02:00:00.000Z",
},
{
id: MessageId.make("mixed-user-2"),
role: "user",
text: "Second",
turnId: null,
streaming: false,
createdAt: "2026-04-01T03:00:00.000Z",
updatedAt: "2026-04-01T03:00:00.000Z",
},
{
id: MessageId.make("mixed-assistant-2"),
role: "assistant",
text: "Response 2",
turnId: TurnId.make("mixed-turn-2"),
streaming: false,
createdAt: "2026-04-01T04:00:00.000Z",
updatedAt: "2026-04-01T04:00:00.000Z",
},
{
id: MessageId.make("mixed-user-3"),
role: "user",
text: "Third",
turnId: null,
streaming: false,
createdAt: "2026-04-01T05:00:00.000Z",
updatedAt: "2026-04-01T05:00:00.000Z",
},
{
id: MessageId.make("mixed-assistant-3"),
role: "assistant",
text: "Response 3",
turnId: TurnId.make("mixed-turn-3"),
streaming: false,
createdAt: "2026-04-01T06:00:00.000Z",
updatedAt: "2026-04-01T06:00:00.000Z",
},
],
checkpoints: [
{
turnId: TurnId.make("mixed-turn-2"),
checkpointTurnCount: 2,
checkpointRef: CheckpointRef.make("mixed-ref-2"),
status: "ready",
files: [],
assistantMessageId: MessageId.make("mixed-assistant-2"),
completedAt: "2026-04-01T04:00:00.000Z",
},
],
};
const result = applyThreadDetailEvent(mixedThread, {
...baseEventFields,
sequence: 14,
occurredAt: "2026-04-01T07:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.reverted",
payload: {
threadId: ThreadId.make("thread-1"),
turnCount: 2,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.messages.map((message) => message.id)).toEqual([
MessageId.make("mixed-user-1"),
MessageId.make("mixed-assistant-1"),
MessageId.make("mixed-user-2"),
MessageId.make("mixed-assistant-2"),
MessageId.make("mixed-user-3"),
]);
}
});
it("removes assistant output produced after the selected steering message", () => {
const steeringThread: OrchestrationThread = {
...baseThread,
messages: [
{
id: MessageId.make("steering-user-initial"),
role: "user",
text: "Start the task",
turnId: null,
streaming: false,
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
{
id: MessageId.make("steering-assistant-before"),
role: "assistant",
text: "Working on it",
turnId: TurnId.make("steering-turn"),
streaming: false,
createdAt: "2026-04-01T02:00:00.000Z",
updatedAt: "2026-04-01T02:00:00.000Z",
},
{
id: MessageId.make("steering-user-target"),
role: "user",
text: "Actually, change direction",
turnId: null,
streaming: false,
createdAt: "2026-04-01T03:00:00.000Z",
updatedAt: "2026-04-01T03:00:00.000Z",
},
{
id: MessageId.make("steering-assistant-after"),
role: "assistant",
text: "Changed direction",
turnId: TurnId.make("steering-turn"),
streaming: false,
createdAt: "2026-04-01T04:00:00.000Z",
updatedAt: "2026-04-01T04:00:00.000Z",
},
],
};
const result = applyThreadDetailEvent(steeringThread, {
...baseEventFields,
sequence: 14,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.reverted",
payload: {
threadId: ThreadId.make("thread-1"),
turnCount: 1,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.messages.map((message) => message.id)).toEqual([
MessageId.make("steering-user-initial"),
MessageId.make("steering-assistant-before"),
MessageId.make("steering-user-target"),
]);
}
});
it("removes every message after the selected user prompt when checkpoints exist", () => {
const threadWithData: OrchestrationThread = {
...baseThread,
messages: [
{
id: MessageId.make("user-hello"),
role: "user",
text: "hello",
turnId: null,
streaming: false,
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
{
id: MessageId.make("assistant-hello"),
role: "assistant",
text: "Hello!",
turnId: TurnId.make("turn-1"),
streaming: false,
createdAt: "2026-04-01T02:00:00.000Z",
updatedAt: "2026-04-01T02:00:00.000Z",
},
{
id: MessageId.make("user-create-file"),
role: "user",
text: "create a file",
turnId: null,
streaming: false,
createdAt: "2026-04-01T03:00:00.000Z",
updatedAt: "2026-04-01T03:00:00.000Z",
},
{
id: MessageId.make("assistant-create-file"),
role: "assistant",
text: "Created it.",
turnId: TurnId.make("turn-2"),
streaming: false,
createdAt: "2026-04-01T04:00:00.000Z",
updatedAt: "2026-04-01T04:00:00.000Z",
},
],
checkpoints: [
{
turnId: TurnId.make("turn-1"),
checkpointTurnCount: 1,
checkpointRef: CheckpointRef.make("ref-1"),
status: "ready",
files: [],
assistantMessageId: MessageId.make("assistant-hello"),
completedAt: "2026-04-01T02:00:00.000Z",
},
{
turnId: TurnId.make("turn-2"),
checkpointTurnCount: 2,
checkpointRef: CheckpointRef.make("ref-2"),
status: "ready",
files: [],
assistantMessageId: MessageId.make("assistant-create-file"),
completedAt: "2026-04-01T04:00:00.000Z",
},
],
};
const result = applyThreadDetailEvent(threadWithData, {
...baseEventFields,
sequence: 14,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.reverted",
payload: {
threadId: ThreadId.make("thread-1"),
turnCount: 0,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.messages.map((message) => message.id)).toEqual([
MessageId.make("user-hello"),
]);
}
});
it("filters entities to retained turns", () => {
const threadWithData: OrchestrationThread = {
...baseThread,
@@ -937,15 +623,6 @@ describe("applyThreadDetailEvent", () => {
},
{
id: MessageId.make("msg-3"),
role: "user",
text: "Second",
turnId: null,
streaming: false,
createdAt: "2026-04-01T02:30:00.000Z",
updatedAt: "2026-04-01T02:30:00.000Z",
},
{
id: MessageId.make("msg-4"),
role: "assistant",
text: "Response 2",
turnId: TurnId.make("turn-2"),
@@ -970,7 +647,7 @@ describe("applyThreadDetailEvent", () => {
checkpointRef: CheckpointRef.make("ref-2"),
status: "ready",
files: [],
assistantMessageId: MessageId.make("msg-4"),
assistantMessageId: MessageId.make("msg-3"),
completedAt: "2026-04-01T03:00:00.000Z",
},
],
@@ -994,94 +671,11 @@ describe("applyThreadDetailEvent", () => {
// turn-2 checkpoint is filtered out (turnCount 2 > revert target 1)
expect(result.thread.checkpoints).toHaveLength(1);
expect(result.thread.checkpoints[0]?.turnId).toBe("turn-1");
// The selected second user prompt remains, but its turn-2 response is removed.
expect(result.thread.messages).toHaveLength(3);
// msg-3 (turn-2) is filtered, msg-1 (no turn) and msg-2 (turn-1) remain
expect(result.thread.messages).toHaveLength(2);
expect(result.thread.latestTurn?.turnId).toBe("turn-1");
}
});
it("retains checkpoint turn entities when no assistant message exists", () => {
const retainedTurnId = TurnId.make("checkpoint-only-turn");
const checkpointOnlyThread: OrchestrationThread = {
...baseThread,
messages: [
{
id: MessageId.make("checkpoint-only-user"),
role: "user",
text: "Start",
turnId: null,
streaming: false,
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
{
id: MessageId.make("checkpoint-only-next-user"),
role: "user",
text: "Try again",
turnId: null,
streaming: false,
createdAt: "2026-04-01T02:00:00.000Z",
updatedAt: "2026-04-01T02:00:00.000Z",
},
],
checkpoints: [
{
turnId: retainedTurnId,
checkpointTurnCount: 1,
checkpointRef: CheckpointRef.make("checkpoint-only-ref"),
status: "ready",
files: [],
assistantMessageId: null,
completedAt: "2026-04-01T01:30:00.000Z",
},
],
proposedPlans: [
{
id: "checkpoint-only-plan",
turnId: retainedTurnId,
planMarkdown: "## Retained plan",
implementedAt: null,
implementationThreadId: null,
createdAt: "2026-04-01T01:10:00.000Z",
updatedAt: "2026-04-01T01:10:00.000Z",
},
],
activities: [
{
id: EventId.make("checkpoint-only-activity"),
tone: "tool",
kind: "command",
summary: "Retained activity",
payload: {},
turnId: retainedTurnId,
createdAt: "2026-04-01T01:20:00.000Z",
},
],
};
const result = applyThreadDetailEvent(checkpointOnlyThread, {
...baseEventFields,
sequence: 14,
occurredAt: "2026-04-01T03:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.reverted",
payload: {
threadId: ThreadId.make("thread-1"),
turnCount: 1,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.proposedPlans.map((plan) => plan.id)).toEqual([
"checkpoint-only-plan",
]);
expect(result.thread.activities.map((activity) => activity.id)).toEqual([
EventId.make("checkpoint-only-activity"),
]);
}
});
});
describe("no-op events", () => {
@@ -417,11 +417,8 @@ export function applyThreadDetailEvent(
Arr.sort(checkpointOrder),
);
const messages = retainMessagesAfterRevert(thread.messages, event.payload.turnCount);
const retainedTurnIds = new Set([
...Arr.map(checkpoints, (checkpoint) => checkpoint.turnId),
...messages.flatMap((message) => (message.turnId === null ? [] : [message.turnId])),
]);
const retainedTurnIds = new Set(Arr.map(checkpoints, (entry) => entry.turnId));
const messages = retainMessagesAfterRevert(thread.messages, retainedTurnIds);
const proposedPlans = pipe(
thread.proposedPlans,
Arr.filter((plan) => plan.turnId === null || retainedTurnIds.has(plan.turnId)),
@@ -534,21 +531,17 @@ function rebindCheckpointAssistantMessage(
function retainMessagesAfterRevert(
messages: ReadonlyArray<OrchestrationMessage>,
turnCount: number,
retainedTurnIds: ReadonlySet<string>,
): OrchestrationMessage[] {
let userMessageIndex = 0;
let reachedTargetUser = false;
// Keep messages that belong to a retained turn, plus system messages and
// messages without a turn binding (pre-turn-0 user messages).
return Arr.filter(messages, (message) => {
if (message.role === "system") {
return true;
}
if (reachedTargetUser) {
return false;
if (message.turnId === null) {
return true;
}
if (message.role === "user") {
reachedTargetUser = userMessageIndex === turnCount;
userMessageIndex += 1;
}
return true;
return retainedTurnIds.has(message.turnId);
});
}