Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(refresh): bind the diverged publish to the upstream the refresh checked (#7686)

garrytan32e6c7ec8912
diff --git a/src/commands/git.ts b/src/commands/git.ts
index 11d14053d9..453d427fa7 100644
--- a/src/commands/git.ts
+++ b/src/commands/git.ts
@@ -153,7 +153,7 @@ export async function runGit(args: string[]): Promise<number> {
throw opError('commit_paths_invalid', 'These flags do not combine.',
'Use one mode: -m <msg> -- <paths> (commit + push), --pending [-m <msg>], --push-only [--keep-checkout] [--branch <b>] [--expect-remote <sha> --resolve ...], or --pull [--require-clean].');
}
- if (Boolean(p.resolve.length) !== Boolean(p.expectRemote) || (p.expectRemote && !/^[0-9a-f]{7,64}$/i.test(p.expectRemote))) {
+ if ((p.resolve.length && !p.expectRemote) || (p.expectRemote && !/^[0-9a-f]{7,64}$/i.test(p.expectRemote))) {
throw opError('commit_paths_invalid', '--resolve and --expect-remote <sha> go together.',
'Copy both from the integrate_conflict report: --expect-remote names the remote commit your choice was made against.');
}
@@ -182,7 +182,8 @@ export async function runGit(args: string[]): Promise<number> {
...(p.allowOutside ? { allowOutsideSourceRoot: true } : {}),
...(p.keepCheckout ? { keepCheckout: true } : {}),
...(p.branch ? { branch: p.branch } : {}),
- ...(p.resolve.length ? { resolutions: parseResolutions(p.resolve), expectRemote: p.expectRemote } : {}),
+ ...(p.resolve.length ? { resolutions: parseResolutions(p.resolve) } : {}),
+ ...(p.expectRemote ? { expectRemote: p.expectRemote } : {}),
});
if (result.outcome === 'push_deferred') {
emit(json, { ...result, repo }, `push deferred: another push is running in ${repo} and will publish HEAD too`);
diff --git a/src/core/git/commit-push.ts b/src/core/git/commit-push.ts
index 1f14441991..a844378f9a 100644
--- a/src/core/git/commit-push.ts
+++ b/src/core/git/commit-push.ts
@@ -100,7 +100,11 @@ export interface CommitPushOptions {
lockWaitMs?: number;
/** `--resolve`: blob choice per conflicted path for the in-memory merge. */
resolutions?: Map<string, Resolution>;
- /** `--expect-remote`: the remote commit the resolutions were chosen against (required with resolutions). */
+ /**
+ * `--expect-remote`: the remote commit the resolutions were chosen against (required with resolutions). Without
+ * resolutions it binds the publish (#7686): HEAD is merged with that commit, pushed with a lease on it, and a remote that
+ * moved anywhere else refuses, so a rewrite made after the caller checked the remote is never merged back.
+ */
expectRemote?: string;
/** How long a foreground publish waits for another publish to finish. */
publishWaitMs?: number;
@@ -891,6 +895,7 @@ export function readQuarantine(repo: string): QuarantineEntry[] {
* a merge built over it earlier) is returned so a lagging checkout catches up.
*/
function publish(ctx: Ctx, start: string, resolutions: Map<string, Resolution> | undefined, expectRemote: string | undefined, cwd: string): Published {
+ const bound = expectRemote && !resolutions?.size ? gitOk(ctx.root, ['rev-parse', '--verify', `${expectRemote}^{commit}`], 'rev-parse --expect-remote') : undefined;
let candidate = start;
let integrated = 0;
let lastError = '';
@@ -904,15 +909,22 @@ function publish(ctx: Ctx, start: string, resolutions: Map<string, Resolution> |
};
for (let attempt = 0; attempt < MAX_PUSH_ATTEMPTS; attempt++) {
if (attempt) Atomics.wait(sleeper, 0, 0, Math.min(2000, 50 * 2 ** attempt) + Math.floor(Math.random() * 100));
- const push = remoteGit(ctx.root, 'push', [...(ctx.keepCheckout ? ['--no-verify'] : []), '--', ctx.remote, `${candidate}:${ctx.mergeRef}`]);
- if (push.code === 0) return { sha: candidate, integrated, confirmed: 'push', ...notes() };
- lastError = push.stderr.trim();
- if (remoteContains(ctx, candidate)) return onRemote();
- const remoteTip = fetchTracking(ctx);
- if (!remoteTip) break;
- if (isAncestor(ctx.root, remoteTip, candidate)) {
- if (++stalled > 1) break;
- continue;
+ let remoteTip = bound && !isAncestor(ctx.root, bound, candidate) ? bound : null;
+ if (!remoteTip) {
+ const push = remoteGit(ctx.root, 'push', [...(ctx.keepCheckout ? ['--no-verify'] : []), ...(bound ? [`--force-with-lease=${ctx.mergeRef}:${bound}`] : []), '--', ctx.remote, `${candidate}:${ctx.mergeRef}`]);
+ if (push.code === 0) return { sha: candidate, integrated, confirmed: 'push', ...notes() };
+ lastError = push.stderr.trim();
+ if (remoteContains(ctx, candidate)) return onRemote();
+ remoteTip = fetchTracking(ctx);
+ if (!remoteTip) break;
+ if (bound && remoteTip !== bound) {
+ throw opError('push_unconfirmed', `The remote moved to ${remoteTip.slice(0, 12)} after --expect-remote ${bound.slice(0, 12)} was checked, so nothing was pushed: publishing now could merge back commits a rewrite removed.`,
+ 'Check the remote history first (gbrain sources refresh checks it for a force-push), then rerun.', { detail: candidate, retryable: true });
+ }
+ if (isAncestor(ctx.root, remoteTip, candidate)) {
+ if (++stalled > 1) break;
+ continue;
+ }
}
const raw = integrate(ctx.root, candidate, remoteTip);
const kept = supersededPaths(ctx.root, candidate, remoteTip, raw, resolutions);
@@ -1148,14 +1160,14 @@ export function commitAndPush(opts: CommitPushOptions): CommitPushResult {
{ detail: committed ?? '', retryable: true });
}
- const cycle = (onlyWhenClean: boolean, start: string | null, resolutions?: Map<string, Resolution>) => {
+ const cycle = (onlyWhenClean: boolean, start: string | null, resolutions?: Map<string, Resolution>, expectRemote?: string) => {
const requested = existsSync(requestPath);
rmSync(requestPath, { force: true });
try {
const head = gitOk(root, ['rev-parse', 'HEAD'], 'rev-parse HEAD');
const target = start && !isAncestor(root, start, head) ? start : head;
if (!opts.allowOutsideSourceRoot) assertLocalCommitsInsideRoots(ctx, target, roots);
- const published = publish(ctx, target, resolutions, opts.expectRemote, cwd);
+ const published = publish(ctx, target, resolutions, expectRemote, cwd);
if (!elsewhere) rmSync(absGitPath(root, CONFLICT_STATE_NAME), { force: true });
const moved = published.sha === head || elsewhere || opts.keepCheckout ? { checkout: 'unchanged' as const } : advanceWithLock(root, published.sha, head, onlyWhenClean, opts.lockWaitMs);
return { published, moved };
@@ -1168,7 +1180,7 @@ export function commitAndPush(opts: CommitPushOptions): CommitPushResult {
let first: ReturnType<typeof cycle>;
try {
- first = cycle(unattended, committed, opts.resolutions);
+ first = cycle(unattended, committed, opts.resolutions, opts.expectRemote);
} finally {
pub.release();
}
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 6141608d74..08ec0c5603 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -328,7 +328,7 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTi
const incoming = await dirtyOverlap(git, root, base, targetHead);
if (incoming.overlap.length) throw dirtyRefusal(sourceId, incoming.overlap);
const localCommits = Number((await read(['rev-list', '--count', `${targetHead}..${oldHead}`])).stdout.trim()) || 0;
- const pushed = await publishInChild(root, sourceId);
+ const pushed = await publishInChild(root, sourceId, targetHead);
const published = { sha: pushed.sha ?? targetHead, local_commits: localCommits, integrated: pushed.integrated,
...(pushed.quarantined ? { quarantined: pushed.quarantined } : {}), ...(pushed.superseded_remote_paths ? { superseded_remote_paths: pushed.superseded_remote_paths } : {}) };
const plan = await precheck(engine, git, sourceId, fetchTimeoutMs).catch(error => { throw notePublished(error, published); });
@@ -342,16 +342,17 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTi
const PUBLISH_TIMEOUT_MS = 600_000;
/**
- * #7686 (#7010's rule): run the keep-checkout publish in a bounded child `gbrain git commit-push --push-only --keep-checkout`.
+ * #7686 (#7010's rule): run the keep-checkout publish in a bounded child `gbrain git commit-push --push-only --keep-checkout`,
+ * bound with `--expect-remote` to the upstream this refresh checked for a rewrite, so a force-push after the check refuses.
* The shared publish is synchronous (fetches, pushes and lock waits block for minutes), and a refresh delegated to a
* PGLite owner runs inside the owner, where blocking would stall lease renewals; a busy publish lock fails at once.
*/
-async function publishInChild(root: string, sourceId: string): Promise<{ sha: string | null; integrated: number; quarantined?: QuarantineEntry; superseded_remote_paths?: string[] }> {
+async function publishInChild(root: string, sourceId: string, checkedUpstream: string): Promise<{ sha: string | null; integrated: number; quarantined?: QuarantineEntry; superseded_remote_paths?: string[] }> {
const { CHILD_ENV, resolveChildCliInvocation } = await import('../minions/job-isolation.ts');
const cli = resolveChildCliInvocation(process.env, process.execPath, process.argv[1], () => null);
if (!cli) throw opError('git_push_unavailable', `This process could not locate its own CLI executable to publish the diverged checkout ${root}; nothing was pushed.`,
`Set ${CHILD_ENV.childCliOverride} to the path of the CLI executable and retry gbrain sources refresh ${sourceId}, or publish by hand: gbrain git commit-push --push-only --keep-checkout --path ${q(root)}.`);
- const child = await execFileBounded(cli.cmd, [...cli.argsPrefix, 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', root, '--json'], {
+ const child = await execFileBounded(cli.cmd, [...cli.argsPrefix, 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', root, '--expect-remote', checkedUpstream, '--json'], {
timeout: PUBLISH_TIMEOUT_MS, termGraceMs: 10_000, maxBuffer: 1024 * 1024,
env: { ...process.env, GBRAIN_PUBLISH_LOCK_WAIT_SECONDS: '0', GBRAIN_PUSH_LOCK_WAIT_SECONDS: '0' },
});
diff --git a/test/refresh-publish-diverged-7686.test.ts b/test/refresh-publish-diverged-7686.test.ts
index 15c0948688..85b5adc15c 100644
--- a/test/refresh-publish-diverged-7686.test.ts
+++ b/test/refresh-publish-diverged-7686.test.ts
@@ -10,7 +10,9 @@
* refuses `integrate_conflict` naming the path and changes nothing; an uncommitted
* edit on an incoming path refuses `refresh_dirty` before anything is pushed; a dry
* run never publishes; with gbrain.quarantine on, the refresh names the parked
- * local version; a refusal after the publish says the remote already holds it.
+ * local version; a refusal after the publish says the remote already holds it; a
+ * force-push that lands between the refresh's check and the publish's push is never
+ * merged back (the publish is bound to the upstream the refresh checked).
* Regression it catches: a refresh that pushes local commits without the
* setting, one that moves the checkout or drops the local commits while
* publishing, and one that pushes or fences anything when the merge conflicts or
@@ -230,6 +232,24 @@ for (const [label, forget] of [['', false], [' even when the first fetch to see
}), 120_000);
}
+test('setting on: a force-push that lands after the refresh checked the upstream and before the publish pushes is never merged back; the publish refuses and the remote keeps the rewrite', () => each(async f => {
+ f.push('alpha/purged.md', page('Purged', 'A page the upstream later removed by rewriting history.'));
+ expect(await refreshWorktree(f.engine, f.alpha)).toMatchObject({ status: 'completed' });
+ const purged = f.head();
+ f.push('alpha/three.md', page('Alpha three', 'A normal upstream commit after the purged one.'));
+ git(f.upstream, 'checkout', '-q', '--detach', `${purged}~1`);
+ commitFile(f.upstream, 'alpha/two.md', page('Alpha two', 'Rewritten upstream.'));
+ const rewrite = git(f.upstream, 'rev-parse', 'HEAD').trim();
+ writeFileSync(join(f.root, 'beta/local.md'), page('Local', 'A local commit nobody pushed.'));
+ git(f.root, 'add', 'beta/local.md'); git(f.root, 'commit', '-qm', 'local only');
+ const racing = join(bin, `gbrain-race-${f.engine.kind}`);
+ writeFileSync(racing, `#!/bin/sh\ngit -C '${f.upstream}' push -q --force origin ${rewrite}:refs/heads/main || exit 97\nexec '${childCli}' "$@"\n`);
+ chmodSync(racing, 0o755);
+ await f.engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, 'true');
+ await withEnv({ GBRAIN_JOB_CHILD_CLI: racing }, () => refusedWith(refreshWorktree(f.engine, f.alpha), 'push_unconfirmed'));
+ expect(remoteHead(f)).toBe(rewrite);
+}), 120_000);
+
test('setting on, reflog off and no completed refresh: the refusal\'s own fetch records the rewrite, so a second refresh refuses too, and with no record at all it refuses', () => each(async f => {
const reflog = git(f.root, 'rev-parse', '--path-format=absolute', '--git-path', 'logs/refs/remotes/origin/main').trim();
git(f.root, 'config', 'core.logAllRefUpdates', 'false');