Skip to content

Commit 6c1f5e6

Browse files
committed
fix(server,web): keep existing checkpoints when a baseline fills in new repositories
- A turn's baseline now captures only in the repositories that lack the ref. Before, one repository missing it (cloned in mid-thread, or after a partial capture) re-captured every repository and overwrote their good checkpoints, so edits made between turns leaked into earlier turns' diffs and rewinds. - Switching the diff panel to another repository goes back to the automatic base, since a base branch picked in one repository may not exist in another.
1 parent bd6eb11 commit 6c1f5e6

5 files changed

Lines changed: 75 additions & 19 deletions

File tree

‎apps/server/src/checkpointing/CheckpointStore.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,37 @@ it.layer(TestLayer)("CheckpointStore.layer", (it) => {
220220
);
221221
}),
222222
);
223+
it.effect("fills in a new repository without overwriting existing checkpoints", () =>
224+
Effect.gen(function* () {
225+
const workspace = yield* makeWorkspace;
226+
const fileSystem = yield* FileSystem.FileSystem;
227+
const checkpointStore = yield* CheckpointStore.CheckpointStore;
228+
const threadId = ThreadId.make("thread-multi-repo-fill-in");
229+
const checkpointRef = checkpointRefForThreadTurn(threadId, 0);
230+
231+
yield* checkpointStore.captureCheckpoint({ cwd: workspace, checkpointRef });
232+
const late = NodePath.join(workspace, "late");
233+
yield* fileSystem.makeDirectory(late);
234+
yield* initRepoWithCommit(late);
235+
yield* writeTextFile(NodePath.join(workspace, "api", "README.md"), "# edited\n");
236+
237+
yield* checkpointStore.captureCheckpoint({
238+
cwd: workspace,
239+
checkpointRef,
240+
missingOnly: true,
241+
});
242+
243+
expect(yield* checkpointStore.hasCheckpointRef({ cwd: workspace, checkpointRef })).toBe(
244+
true,
245+
);
246+
expect(yield* checkpointStore.restoreCheckpoint({ cwd: workspace, checkpointRef })).toBe(
247+
true,
248+
);
249+
expect(yield* fileSystem.readFileString(NodePath.join(workspace, "api", "README.md"))).toBe(
250+
"# test\n",
251+
);
252+
}),
253+
);
223254
});
224255

225256
describe("diffCheckpoints", () => {

‎apps/server/src/checkpointing/CheckpointStore.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { prefixNumstatPaths, prefixPatchPaths } from "./Diffs.ts";
3333
export interface CaptureCheckpointInput {
3434
readonly cwd: string;
3535
readonly checkpointRef: CheckpointRef;
36+
/** Capture only in repositories without the ref, keeping checkpoints already taken. */
37+
readonly missingOnly?: boolean;
3638
}
3739

3840
export interface RestoreCheckpointInput {
@@ -175,11 +177,17 @@ export const make = Effect.gen(function* () {
175177
"captureCheckpoint",
176178
)(function* (input) {
177179
yield* forEachTarget(yield* resolveTargets(input.cwd), (target) =>
178-
resolveCheckpoints("CheckpointStore.captureCheckpoint", target.cwd).pipe(
179-
Effect.flatMap((checkpoints) =>
180-
checkpoints.captureCheckpoint({ ...input, cwd: target.cwd }),
181-
),
182-
),
180+
Effect.gen(function* () {
181+
if (input.missingOnly && (yield* hasRefIn(target, input.checkpointRef))) return;
182+
const checkpoints = yield* resolveCheckpoints(
183+
"CheckpointStore.captureCheckpoint",
184+
target.cwd,
185+
);
186+
yield* checkpoints.captureCheckpoint({
187+
cwd: target.cwd,
188+
checkpointRef: input.checkpointRef,
189+
});
190+
}),
183191
);
184192
});
185193

‎apps/server/src/orchestration-v2/CheckpointService.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,10 @@ export const layer: Layer.Layer<
269269
scopeId: input.scope.id,
270270
ordinalWithinScope: input.ordinalWithinScope,
271271
});
272-
const exists = yield* checkpointStore.hasCheckpointRef({
273-
cwd: input.scope.cwd,
274-
checkpointRef,
275-
});
276-
if (exists) {
277-
return;
278-
}
279-
280272
yield* checkpointStore.captureCheckpoint({
281273
cwd: input.scope.cwd,
282274
checkpointRef,
275+
missingOnly: true,
283276
});
284277
}),
285278
).pipe(

‎apps/web/src/diffPanelStore.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe("diffPanelStore", () => {
1111
useDiffPanelStore.setState({
1212
byThreadKey: {},
1313
branchBaseRefByThreadKey: {},
14+
repositoryByThreadKey: {},
1415
}),
1516
);
1617

@@ -107,6 +108,20 @@ describe("diffPanelStore", () => {
107108
).toEqual({ kind: "branch", baseRef: "origin/main" });
108109
});
109110

111+
it("goes back to the automatic base when switching repositories", () => {
112+
useDiffPanelStore.getState().selectRepository(THREAD_REF, "api");
113+
useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/develop");
114+
useDiffPanelStore.getState().selectRepository(THREAD_REF, "api");
115+
expect(
116+
selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
117+
).toEqual({ kind: "branch", baseRef: "origin/develop" });
118+
119+
useDiffPanelStore.getState().selectRepository(THREAD_REF, "web");
120+
expect(
121+
selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
122+
).toEqual({ kind: "branch", baseRef: null });
123+
});
124+
110125
it("reconciles a missing turn selection to the latest available turn", () => {
111126
const missingTurnId = RunId.make("turn-missing");
112127
const latestTurnId = RunId.make("turn-latest");

‎apps/web/src/diffPanelStore.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,22 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
7474
},
7575
};
7676
}),
77+
// A base branch picked in one repository may not exist in another, so switching
78+
// repositories goes back to the automatic base.
7779
selectRepository: (ref, relativePath) =>
78-
set((state) => ({
79-
repositoryByThreadKey: {
80-
...state.repositoryByThreadKey,
81-
[scopedThreadKey(ref)]: relativePath,
82-
},
83-
})),
80+
set((state) => {
81+
const threadKey = scopedThreadKey(ref);
82+
if (state.repositoryByThreadKey[threadKey] === relativePath) return state;
83+
const previous = state.byThreadKey[threadKey];
84+
return {
85+
repositoryByThreadKey: { ...state.repositoryByThreadKey, [threadKey]: relativePath },
86+
byThreadKey:
87+
previous?.kind === "branch"
88+
? { ...state.byThreadKey, [threadKey]: { kind: "branch", baseRef: null } }
89+
: state.byThreadKey,
90+
branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [threadKey]: null },
91+
};
92+
}),
8493
selectTurn: (ref, turnId, filePath) =>
8594
set((state) => {
8695
const threadKey = scopedThreadKey(ref);

0 commit comments

Comments
 (0)