Skip to content

Commit 5c28770

Browse files
committed
fix(repair): retry state-repo pushes on concurrent-writer rejection
A shallow state checkout fetches a racing writer's push with --depth=1, so the new remote head shares no local ancestry. pushCommit's recovery then ran git merge-base through runGit, which threw and escaped every publish retry loop (spam scanner runs 29701094002 and 29702697623). Probe the merge base non-fatally and defer disjoint-history races to the caller's bounded rebuild-on-remote-head retry path. Also safelist log and merge-base in redacted command display so future failures name the action.
1 parent d7e01ce commit 5c28770

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎src/repair/git-publish.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ function safeGitDisplayAction(action: string | undefined): string {
174174
case "checkout":
175175
case "cat-file":
176176
case "clean":
177+
case "log":
177178
case "ls-files":
178179
case "ls-tree":
180+
case "merge-base":
179181
case "mktree":
180182
case "push":
181183
case "read-tree":
@@ -1256,8 +1258,20 @@ export function pushCommit(options: {
12561258
continue;
12571259
}
12581260
const remoteCommit = runGit(["rev-parse", `${remote}/${branch}`], { quiet: true }).trim();
1259-
const statusMerges = planSweepStatusMerges({ localCommit, remoteCommit });
1260-
const ledgerMerge = planCommentRouterLedgerMerge({ localCommit, remoteCommit });
1261+
const mergeBase = spawnGit(["merge-base", localCommit, remoteCommit], { quiet: true });
1262+
if (mergeBase.status !== 0) {
1263+
// A shallow state checkout fetches racing pushes with --depth=1, so the
1264+
// new remote head can share no local ancestry. Rebasing is impossible;
1265+
// hand the race back to the caller, whose rebuild path re-applies this
1266+
// publish directly on the fetched remote head.
1267+
console.log(
1268+
`No common Git base with ${remote}/${branch} after a lost push race; deferring to a remote-head rebuild`,
1269+
);
1270+
return false;
1271+
}
1272+
const baseCommit = mergeBase.stdout.trim();
1273+
const statusMerges = planSweepStatusMerges({ baseCommit, localCommit, remoteCommit });
1274+
const ledgerMerge = planCommentRouterLedgerMerge({ baseCommit, localCommit, remoteCommit });
12611275
const rebaseArgs =
12621276
rebaseStrategy === "theirs" || rebaseStrategy === "apply-records"
12631277
? ["rebase", "-X", "theirs", `${remote}/${branch}`]

‎test/repair/git-publish.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,57 @@ test("publishMainCommit preserves latest health when a second race forces commit
436436
assert.deepEqual(merged.last_close_apply_health, secondCloseHealth);
437437
});
438438

439+
test("publishMainCommit survives a shallow-checkout push race without a common Git base", () => {
440+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-publish-"));
441+
const origin = path.join(root, "origin.git");
442+
const seed = path.join(root, "seed");
443+
const work = path.join(root, "work");
444+
const other = path.join(root, "other");
445+
run("git", ["init", "--bare", origin], root);
446+
run("git", ["clone", origin, seed], root);
447+
configureUser(seed);
448+
write(path.join(seed, "results/spam-scanner.json"), '{"scans":0}\n');
449+
write(path.join(seed, "results/other.txt"), "remote\n");
450+
run("git", ["add", "."], seed);
451+
run("git", ["commit", "-m", "initial"], seed);
452+
run("git", ["push", "origin", "HEAD:main"], seed);
453+
run("git", ["--git-dir", origin, "symbolic-ref", "HEAD", "refs/heads/main"], root);
454+
455+
// Production state checkouts are shallow, so the racing writer's commits
456+
// arrive through a --depth=1 fetch that shares no ancestry with local HEAD.
457+
run("git", ["clone", "--depth", "1", `file://${origin}`, work], root);
458+
configureUser(work);
459+
460+
run("git", ["clone", origin, other], root);
461+
configureUser(other);
462+
write(path.join(other, "results/other.txt"), "remote update one\n");
463+
run("git", ["commit", "-am", "remote update one"], other);
464+
write(path.join(other, "results/other.txt"), "remote update two\n");
465+
run("git", ["commit", "-am", "remote update two"], other);
466+
run("git", ["push", "origin", "HEAD:main"], other);
467+
468+
write(path.join(work, "results/spam-scanner.json"), '{"scans":1}\n');
469+
const result = withCwd(work, () =>
470+
publishMainCommit({
471+
message: "chore: record ClawSweeper spam scan",
472+
paths: ["results/spam-scanner.json"],
473+
maxAttempts: 1,
474+
pushAttempts: 1,
475+
rebaseStrategy: "theirs",
476+
}),
477+
);
478+
479+
assert.equal(result, "committed");
480+
assert.equal(
481+
run("git", ["--git-dir", origin, "show", "main:results/spam-scanner.json"], root),
482+
'{"scans":1}\n',
483+
);
484+
assert.equal(
485+
run("git", ["--git-dir", origin, "show", "main:results/other.txt"], root),
486+
"remote update two\n",
487+
);
488+
});
489+
439490
test("publishMainCommit fails closed when a racing sweep status is malformed", () => {
440491
const root = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-publish-"));
441492
const origin = path.join(root, "origin.git");
@@ -1231,7 +1282,7 @@ test("reconcile-records fails closed when state and remote have no common base",
12311282
),
12321283
lines,
12331284
),
1234-
/git command <redacted-args> exited 1/,
1285+
/git merge-base <redacted-args> exited 1/,
12351286
);
12361287
assert.equal(
12371288
lines.some((line) => line.includes("Git publish failure: phase=prepare")),

0 commit comments

Comments
 (0)