Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(workspace): count a run's commits from its recorded base alone, a…
…nd never rewind a tracking ref another provision moved (FIX-1927)

- readRunCommits uses the recorded baseCommit alone when there is one; the
  named refs, which a run's worktree shares, are only a fallback for older
  records.
- After a fetch, the tracking ref moves only if it still holds the commit
  the fetch started from, and the comparison uses the fetched commit rather
  than re-reading the shared ref.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GJ9UL2RHLrxJsiGFnkuxjt
  • Loading branch information
claude committed Oct 13, 2026
commit 181cfe5dc255a628907da652e4ec85ba435886db
26 changes: 14 additions & 12 deletions packages/harness-manager/src/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ async function git(cwd: string, args: string[]): Promise<string> {
}

/**
* The commits on `cwd`'s HEAD that the base does not have: not the commit the
* run's branch was cut at (`baseCommit`), nor the remote's copy of `baseRef`
* (`origin/<baseRef>`), nor the local branch. A local checkout cuts the run's
* branch from the newer of the local branch and its upstream, and a clone from
* the remote's, so the cut commit is the one copy every host has. It is read
* from the run's record, never from the checkout's config: a run shares that
* config and could repoint an upstream at its own work. No base, or no
* checkout git can read, answers none.
* The commits on `cwd`'s HEAD that the base does not have.
*
* With `baseCommit`, the commit the run's branch was cut at, that commit alone
* is the base. It comes from the run's record, never from the checkout: a run
* shares the checkout's refs and config, so any named copy of the base is one
* it could point at its own work. A record without it falls back to the named
* copies: the remote's (`origin/<baseRef>`) and the local branch. No base, or
* no checkout git can read, answers none.
*/
export async function readRunCommits(
cwd: string,
Expand All @@ -45,10 +45,12 @@ export async function readRunCommits(
if (baseRef === null) return { count: 0, subjects: [] };
const bases: string[] = [];
// In the run's own checkout, a bare `HEAD` is the run's branch, never its base.
const copies = [
...(baseCommit !== null ? [baseCommit] : []),
...(baseRef === "HEAD" ? [`refs/remotes/origin/HEAD`] : [`refs/remotes/origin/${baseRef}`, baseRef]),
];
const copies =
baseCommit !== null
? [baseCommit]
: baseRef === "HEAD"
? [`refs/remotes/origin/HEAD`]
: [`refs/remotes/origin/${baseRef}`, baseRef];
for (const base of copies) {
try {
await git(cwd, ["rev-parse", "--verify", "--quiet", `${base}^{commit}`]);
Expand Down
11 changes: 11 additions & 0 deletions packages/harness-manager/test/commits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ describe("readRunCommits", () => {
expect(await readRunCommits(dir, "main", cut)).toEqual({ count: 1, subjects: ["the run's change"] });
});

it("still counts a run's commits after the run points the base's own refs at its work", async () => {
// Refs are shared with the run's worktree too; only the recorded cut commit is not.
const dir = repo("local");
git(dir, "checkout", "-q", "-b", "run", "main");
const cut = git(dir, "rev-parse", "HEAD").trim();
commit(dir, "the run's change");
git(dir, "update-ref", "refs/remotes/origin/main", "HEAD");
git(dir, "update-ref", "refs/heads/main", "HEAD");
expect(await readRunCommits(dir, "main", cut)).toEqual({ count: 1, subjects: ["the run's change"] });
});

it("answers none for a base the checkout doesn't have", async () => {
const dir = repo("local");
expect(await readRunCommits(dir, "no-such-branch")).toEqual({ count: 0, subjects: [] });
Expand Down
6 changes: 5 additions & 1 deletion packages/workspace/src/local-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,11 @@ export function localWorkspaceHost(options: LocalWorkspaceHostOptions): Workspac
},
);
const fetched = (await run("git", ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`], { cwd: scratch, timeoutMs: left(), env: isolated })).stdout.trim();
await git(repo, ["-c", "core.hooksPath=/dev/null", "update-ref", ref, fetched], left());
// Only over the copy this fetch started from: a provision running
// beside this one may have fetched something newer since.
await git(repo, ["-c", "core.hooksPath=/dev/null", "update-ref", ref, fetched, previous], left()).catch(() => undefined);
Comment thread
jhoffner marked this conversation as resolved.
Resolved
// What was fetched, not what the shared ref says now.
return { ref, commit: fetched };
} catch (error) {
skip(`could not fetch "${redactRemote(judged.url)}" (${firstLine(error)})`);
} finally {
Expand Down
Loading