Skip to content

Commit 36ea95d

Browse files
fix(checkpoint): fetched upstream commits no longer show as agent changes
1 parent d2ab13b commit 36ea95d

2 files changed

Lines changed: 140 additions & 13 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,54 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
510510
);
511511
}),
512512
);
513+
514+
it.effect("leaves out recent upstream commits that a fetch brought in", () =>
515+
Effect.gen(function* () {
516+
const tmp = yield* makeTmpDir();
517+
const upstream = yield* makeTmpDir("checkpoint-store-upstream-");
518+
yield* initRepoWithCommit(tmp);
519+
yield* git(upstream, ["clone", tmp, "."]);
520+
yield* git(upstream, ["config", "user.email", "[email protected]"]);
521+
yield* git(upstream, ["config", "user.name", "Test"]);
522+
yield* git(tmp, ["remote", "add", "upstream", upstream]);
523+
yield* git(tmp, ["fetch", "upstream"]);
524+
const checkpointStore = yield* CheckpointStore.CheckpointStore;
525+
const threadId = ThreadId.make("checkpoint-fetched-paths");
526+
const refs = {
527+
cwd: tmp,
528+
fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
529+
toCheckpointRef: checkpointRefForThreadTurn(threadId, 1),
530+
};
531+
yield* checkpointStore.captureCheckpoint({
532+
cwd: tmp,
533+
checkpointRef: refs.fromCheckpointRef,
534+
});
535+
536+
// Dated after the checkpoint, like an upstream commit pushed during the turn.
537+
const afterCheckpoint = "2090-01-01T00:00:00Z";
538+
yield* writeTextFile(NodePath.join(upstream, "upstream.txt"), "upstream\n");
539+
yield* git(upstream, ["add", "."]);
540+
yield* git(upstream, ["commit", "-m", "upstream"], { committedAt: afterCheckpoint });
541+
yield* git(tmp, ["fetch", "upstream"], { committedAt: afterCheckpoint });
542+
yield* git(tmp, ["merge", "--ff-only", "upstream/main"], { committedAt: afterCheckpoint });
543+
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: refs.toCheckpointRef });
544+
expect(yield* git(tmp, ["status", "--porcelain"])).toBe("");
545+
expect(yield* checkpointStore.listAuthoredPaths(refs)).toEqual(new Set());
546+
547+
yield* writeTextFile(NodePath.join(tmp, "own.txt"), "own\n");
548+
yield* git(tmp, ["add", "."]);
549+
yield* git(tmp, ["commit", "-m", "own"], { committedAt: afterCheckpoint });
550+
yield* git(tmp, ["push", "upstream", "HEAD:refs/heads/agent"], {
551+
committedAt: afterCheckpoint,
552+
});
553+
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: refs.toCheckpointRef });
554+
555+
expect(yield* checkpointStore.listAuthoredPaths(refs)).toEqual(new Set(["own.txt"]));
556+
557+
yield* git(upstream, ["branch", "other", "agent"]);
558+
yield* git(tmp, ["fetch", "upstream"], { committedAt: afterCheckpoint });
559+
expect(yield* checkpointStore.listAuthoredPaths(refs)).toEqual(new Set(["own.txt"]));
560+
}),
561+
);
513562
});
514563
});

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,31 +1337,109 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
13371337
return null;
13381338
}
13391339

1340-
const listPaths = (args: ReadonlyArray<string>) =>
1340+
const lines = (args: ReadonlyArray<string>, stdin?: string) =>
1341+
execute({
1342+
operation,
1343+
cwd: input.cwd,
1344+
args,
1345+
...(stdin === undefined ? {} : { stdin }),
1346+
maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
1347+
outputMode: "error",
1348+
}).pipe(Effect.map((result) => result.stdout.split("\n").filter((line) => line !== "")));
1349+
const listPaths = (args: ReadonlyArray<string>, stdin?: string) =>
13411350
execute({
13421351
operation,
13431352
cwd: input.cwd,
13441353
args: [...args, "--name-only", "-z", "--no-renames", "--no-ext-diff"],
1354+
...(stdin === undefined ? {} : { stdin }),
13451355
maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
13461356
outputMode: "error",
13471357
}).pipe(Effect.map((result) => result.stdout.split("\0")));
1358+
1359+
// A commit is an import when a remote-tracking reflog shows a fetch delivering it after the
1360+
// previous checkpoint. Only refs with a tip that recent can hold one, which keeps the walk
1361+
// cheap in repositories with thousands of remote refs.
1362+
const listFetchedCommits = Effect.fn("GitVcsDriver.checkpoints.listFetchedCommits")(
1363+
function* () {
1364+
const since = Number(capturedAt);
1365+
const recentRemoteRefs = (yield* lines([
1366+
"for-each-ref",
1367+
"--format=%(committerdate:unix) %(refname)",
1368+
"refs/remotes",
1369+
])).flatMap((line) => {
1370+
const [committedAt = "", ref = ""] = line.split(" ");
1371+
return Number(committedAt) >= since ? [ref] : [];
1372+
});
1373+
if (recentRemoteRefs.length === 0) return new Set<string>();
1374+
1375+
const entries = (yield* lines(
1376+
["log", "--walk-reflogs", "--stdin", "--date=unix", "--format=%H %gD %gs"],
1377+
`${[...recentRemoteRefs, "HEAD"].join("\n")}\n`,
1378+
)).map((line) => {
1379+
const [commit = "", selector = "", ...subject] = line.split(" ");
1380+
const at = selector.lastIndexOf("@{");
1381+
return {
1382+
commit,
1383+
ref: selector.slice(0, at),
1384+
updatedAt: Number(selector.slice(at + 2, -1)),
1385+
subject: subject.join(" "),
1386+
};
1387+
});
1388+
// Fetching another branch can rediscover a commit made locally during this turn.
1389+
const localCommits = new Set(
1390+
entries
1391+
.filter(
1392+
(entry) =>
1393+
entry.ref === "HEAD" &&
1394+
entry.updatedAt >= since &&
1395+
/^commit(?: \([^)]*\))?: /.test(entry.subject),
1396+
)
1397+
.map((entry) => entry.commit),
1398+
);
1399+
const previousByRef = new Map<string, string>();
1400+
const fetchRanges = entries.toReversed().flatMap((entry) => {
1401+
const previous = previousByRef.get(entry.ref);
1402+
previousByRef.set(entry.ref, entry.commit);
1403+
if (entry.ref === "HEAD") return [];
1404+
if (entry.updatedAt < since || entry.subject === "update by push") return [];
1405+
return [entry.commit, ...(previous === undefined ? [] : [`^${previous}`])];
1406+
});
1407+
if (fetchRanges.length === 0) return new Set<string>();
1408+
return new Set(
1409+
(yield* lines(
1410+
["rev-list", "--stdin"],
1411+
`${[...fetchRanges, `^${startHead}`].join("\n")}\n`,
1412+
)).filter((commit) => !localCommits.has(commit)),
1413+
);
1414+
},
1415+
);
1416+
13481417
// Remerge diffs retain merge conflict fixes while excluding clean upstream changes.
1349-
const pathLists = yield* Effect.all(
1418+
const [pathLists, recentCommits] = yield* Effect.all(
13501419
[
1351-
listPaths(["diff", startHead, `${input.fromCheckpointRef}^{commit}`]),
1352-
listPaths(["diff", endHead, `${input.toCheckpointRef}^{commit}`]),
1353-
listPaths(["log", "--format=", "--diff-merges=remerge", `${endHead}..${startHead}`]),
1354-
listPaths([
1355-
"log",
1356-
"--format=",
1357-
"--diff-merges=remerge",
1358-
`--since=@${capturedAt}`,
1359-
`${startHead}..${endHead}`,
1360-
]),
1420+
Effect.all(
1421+
[
1422+
listPaths(["diff", startHead, `${input.fromCheckpointRef}^{commit}`]),
1423+
listPaths(["diff", endHead, `${input.toCheckpointRef}^{commit}`]),
1424+
listPaths(["log", "--format=", "--diff-merges=remerge", `${endHead}..${startHead}`]),
1425+
],
1426+
{ concurrency: "unbounded" },
1427+
),
1428+
lines(["rev-list", `--since=@${capturedAt}`, `${startHead}..${endHead}`]),
13611429
],
13621430
{ concurrency: "unbounded" },
13631431
);
1364-
return new Set(pathLists.flat().filter((path) => path.length > 0));
1432+
const fetchedCommits =
1433+
recentCommits.length === 0 ? new Set<string>() : yield* listFetchedCommits();
1434+
const ownCommits = recentCommits.filter((commit) => !fetchedCommits.has(commit));
1435+
const ownCommitPaths =
1436+
ownCommits.length === 0
1437+
? []
1438+
: yield* listPaths(
1439+
["log", "--no-walk=unsorted", "--stdin", "--format=", "--diff-merges=remerge"],
1440+
`${ownCommits.join("\n")}\n`,
1441+
);
1442+
return new Set([...pathLists.flat(), ...ownCommitPaths].filter((path) => path.length > 0));
13651443
}),
13661444

13671445
deleteCheckpointRefs: Effect.fn("GitVcsDriver.checkpoints.deleteCheckpointRefs")(

0 commit comments

Comments
 (0)