Repository navigation
chore(agents): let a rebased branch push without verifying again #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
scottlovegrove
merged 2 commits into
scottl/agent-guard-pi
from
scottl/agent-push-rebase-exemption
Nov 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { chmodSync, existsSync } from 'node:fs' | ||
| import { join } from 'node:path' | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
| import { | ||
| branchPatchId, | ||
| forgetPassedChanges, | ||
| hasPassedPatch, | ||
| readPassedPatches, | ||
| recordPassedPatch, | ||
| } from './patches.ts' | ||
| import { createTempRepo, GIT_HAS_VERBATIM_PATCH_ID, type TempRepo } from './test-repo.ts' | ||
|
|
||
| let repo: TempRepo | ||
|
|
||
| const rev = (ref: string) => repo.git(['rev-parse', ref]).stdout.trim() | ||
| const base = (ref = 'HEAD') => repo.git(['merge-base', ref, 'main']).stdout.trim() | ||
|
|
||
| const LINES = Array.from({ length: 30 }, (_, i) => `export const line${i} = ${i}`) | ||
|
|
||
| function commit(path: string, content: string, message = `change ${path}`): void { | ||
| repo.write(path, content) | ||
| repo.git(['add', path]) | ||
| repo.git(['commit', '--quiet', '-m', message]) | ||
| } | ||
|
|
||
| /** Moves `main` on with `path`, then rebases `branch` onto it and returns to `branch`. */ | ||
| function moveMainAndRebase(branch: string, path: string, content: string): void { | ||
| repo.git(['switch', '--quiet', 'main']) | ||
| commit(path, content, 'main moves on') | ||
| repo.git(['switch', '--quiet', branch]) | ||
| const rebase = repo.git(['rebase', '--quiet', 'main']) | ||
| if (rebase.status !== 0) throw new Error(rebase.stderr) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| repo = createTempRepo() | ||
| repo.write('src/other.ts', 'export const other = 1\n') | ||
| repo.git(['add', 'src/other.ts']) | ||
| commit('src/a.ts', `${LINES.join('\n')}\n`, 'initial') | ||
| repo.git(['switch', '--quiet', '-c', 'feature']) | ||
| commit('src/a.ts', `${LINES.join('\n').replace('line15 = 15', 'line15 = 150')}\n`) | ||
| }) | ||
|
|
||
| afterEach(() => repo.cleanup()) | ||
|
|
||
| // Without `--verbatim` there is no exemption, and every rebase needs verify again. | ||
| describe.skipIf(!GIT_HAS_VERBATIM_PATCH_ID)('branchPatchId', () => { | ||
| it('keeps the id when a rebase moved the base under unchanged changes', () => { | ||
| const before = branchPatchId(repo.dir, base(), 'HEAD') | ||
| moveMainAndRebase('feature', 'src/other.ts', 'export const other = 2\n') | ||
| expect(rev('HEAD')).not.toBe(rev('ORIG_HEAD')) | ||
| expect(branchPatchId(repo.dir, base(), 'HEAD')).toBe(before) | ||
|
|
||
| // Lines added far above the change only shift its line numbers | ||
| const shifted = ['// a comment', ...LINES].join('\n') | ||
| repo.git(['switch', '--quiet', 'main']) | ||
| commit('src/a.ts', `${shifted}\n`, 'main adds a comment at the top') | ||
| repo.git(['switch', '--quiet', 'feature']) | ||
| repo.git(['rebase', '--quiet', 'main']) | ||
| expect(branchPatchId(repo.dir, base(), 'HEAD')).toBe(before) | ||
| }) | ||
|
|
||
| it('changes the id when the base changed the lines around the change', () => { | ||
| // Whatever context the person's git config asks diffs for | ||
| repo.git(['config', 'diff.context', '0']) | ||
| const before = branchPatchId(repo.dir, base(), 'HEAD') | ||
| moveMainAndRebase( | ||
| 'feature', | ||
| 'src/a.ts', | ||
| `${LINES.join('\n').replace('line13 = 13', 'line13 = 130')}\n`, | ||
| ) | ||
| expect(branchPatchId(repo.dir, base(), 'HEAD')).not.toBe(before) | ||
| }) | ||
|
|
||
| it('changes the id for a change only in whitespace, or only in binary content', () => { | ||
| const before = branchPatchId(repo.dir, base(), 'HEAD') | ||
| repo.git(['switch', '--quiet', '-c', 'spaced', 'main']) | ||
| commit('src/a.ts', `${LINES.join('\n').replace('line15 = 15', 'line15 = 150')}\n`) | ||
| expect(branchPatchId(repo.dir, base(), 'HEAD')).not.toBe(before) | ||
|
|
||
| const ids = ['one', 'two'].map((name) => { | ||
| repo.git(['switch', '--quiet', '-c', `binary-${name}`, 'main']) | ||
| commit('icon.png', `\u0000binary ${name}`) | ||
| return branchPatchId(repo.dir, base(), 'HEAD') | ||
| }) | ||
| expect(ids[0]).toBeDefined() | ||
| expect(ids[0]).not.toBe(ids[1]) | ||
| }) | ||
|
|
||
| it('has no id for a commit that changes nothing since the base', () => { | ||
| expect(branchPatchId(repo.dir, rev('main'), 'main')).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe.skipIf(!GIT_HAS_VERBATIM_PATCH_ID)('passed patches', () => { | ||
| it('finds a recorded patch on a newer base, and only there', () => { | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| expect(hasPassedPatch(repo.dir, base(), 'HEAD')).toBe(true) | ||
| moveMainAndRebase('feature', 'src/other.ts', 'export const other = 2\n') | ||
| expect(hasPassedPatch(repo.dir, base(), 'HEAD')).toBe(true) | ||
|
|
||
| // The same changes on a base the recorded one isn't part of | ||
| repo.git(['switch', '--quiet', '--orphan', 'unrelated']) | ||
| commit('src/a.ts', `${LINES.join('\n')}\n`, 'unrelated history') | ||
| const elsewhere = rev('HEAD') | ||
| commit('src/a.ts', `${LINES.join('\n').replace('line15 = 15', 'line15 = 150')}\n`) | ||
| expect(branchPatchId(repo.dir, elsewhere, 'HEAD')).toBe( | ||
| readPassedPatches(repo.dir)[0]?.patchId, | ||
| ) | ||
| expect(hasPassedPatch(repo.dir, elsewhere, 'HEAD')).toBe(false) | ||
| }) | ||
|
|
||
| it('forgets a patch, whatever base it passed on', () => { | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| moveMainAndRebase('feature', 'src/other.ts', 'export const other = 2\n') | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| commit('src/b.ts', 'export const b = 1\n') | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| repo.git(['reset', '--quiet', '--hard', 'HEAD~1']) | ||
|
|
||
| forgetPassedChanges(repo.dir, base()) | ||
| expect(hasPassedPatch(repo.dir, base(), 'HEAD')).toBe(false) | ||
| // Other changes keep theirs | ||
| expect(readPassedPatches(repo.dir)).toHaveLength(1) | ||
| }) | ||
|
|
||
| // root can write a read-only file, so there is no record it fails to rewrite. | ||
| it.skipIf(process.getuid?.() === 0)( | ||
| "deletes the record when it can't rewrite it, and throws when it can't delete it", | ||
| () => { | ||
| const record = join(repo.dir, '.tmp/agents/verified-patches.json') | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| chmodSync(record, 0o444) | ||
| forgetPassedChanges(repo.dir, base()) | ||
| expect(existsSync(record)).toBe(false) | ||
|
|
||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| chmodSync(record, 0o444) | ||
| chmodSync(join(repo.dir, '.tmp/agents'), 0o555) | ||
| try { | ||
| expect(() => forgetPassedChanges(repo.dir, base())).toThrow(/EACCES/) | ||
| } finally { | ||
| chmodSync(join(repo.dir, '.tmp/agents'), 0o755) | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| it('does not find changes that never passed', () => { | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| commit('src/a.ts', 'export const changed = true\n') | ||
| expect(hasPassedPatch(repo.dir, base(), 'HEAD')).toBe(false) | ||
| }) | ||
|
|
||
| it('keeps one entry per patch and base, and the 50 newest', () => { | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| expect(readPassedPatches(repo.dir)).toHaveLength(1) | ||
| for (let i = 0; i < 55; i++) { | ||
| commit('src/b.ts', `export const b = ${i}\n`) | ||
| recordPassedPatch(repo.dir, base(), 'HEAD') | ||
| } | ||
| const patches = readPassedPatches(repo.dir) | ||
| expect(patches).toHaveLength(50) | ||
| expect(patches.at(-1)?.patchId).toBe(branchPatchId(repo.dir, base(), 'HEAD')) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // The changes that passed `npm run verify`, recorded by patch rather than by tree, so the push | ||
| // check can let a branch through when it holds the same changes on a newer default branch: a | ||
| // rebase that only moved the base. Without this, every rebase of a verified branch, and of every | ||
| // branch of a stack, would need verify again before it could push. | ||
|
|
||
| import { copyFileSync, existsSync, mkdtempSync, rmSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { git } from './git.ts' | ||
| import { readJson, stateDir, writeJson } from './state.ts' | ||
|
|
||
| /** A branch's changes that passed verify, and where the branch left the default branch then. */ | ||
| export type PassedPatch = { patchId: string; base: string; at: string } | ||
|
|
||
| /** Enough for every branch of a long stack, and a few rebases of each. */ | ||
| const MAX_RECORDED = 50 | ||
|
|
||
| function patchesPath(root: string): string { | ||
| return join(stateDir(root), 'verified-patches.json') | ||
| } | ||
|
|
||
| export function readPassedPatches(root: string): PassedPatch[] { | ||
| return readJson<{ patches?: PassedPatch[] }>(patchesPath(root))?.patches ?? [] | ||
| } | ||
|
|
||
| /** | ||
| * An id for everything a commit changes since `base`, the same wherever the changes sit: git's | ||
| * patch id ignores line numbers and file order, but not the three lines around each change, which | ||
| * the diff sets itself whatever `diff.context` says. So a rebase that changed nothing near the | ||
| * branch's changes keeps the id, and one that did, by a conflict or by new code next to them, | ||
| * doesn't. `--verbatim` keeps whitespace, which the default strips, so a change inside a string or | ||
| * to formatting changes the id. Binary files count by their full blob ids. A git older than 2.39 | ||
| * has no `--verbatim`, so every rebase there needs verify again. | ||
| */ | ||
| export function branchPatchId(root: string, base: string, commit: string): string | undefined { | ||
| return patchIdOf(root, [base, commit]) | ||
| } | ||
|
|
||
| /** The patch id of `git diff <compared>`, with the diff settings `branchPatchId` describes. */ | ||
| function patchIdOf( | ||
| root: string, | ||
| compared: string[], | ||
| env?: Record<string, string>, | ||
| ): string | undefined { | ||
| const diff = git( | ||
| root, | ||
| [ | ||
| 'diff', | ||
| '--unified=3', | ||
| '--diff-algorithm=myers', | ||
| '--src-prefix=a/', | ||
| '--dst-prefix=b/', | ||
| '--full-index', | ||
| '--no-color', | ||
| '--no-ext-diff', | ||
| '--no-textconv', | ||
| '--no-renames', | ||
| ...compared, | ||
| ], | ||
| { env }, | ||
| ) | ||
| if (!diff.ok || !diff.stdout.trim()) return undefined | ||
| const id = git(root, ['patch-id', '--verbatim'], { input: diff.stdout }) | ||
| return id.ok ? id.stdout.split(' ')[0] || undefined : undefined | ||
| } | ||
|
|
||
| /** | ||
| * The patch ids of what could be committed next, from `base`: HEAD's changes, what's staged, and | ||
| * the whole working tree, untracked files included. The working tree is read through a copy of the | ||
| * index with everything added, so the real index is left as it is. | ||
| */ | ||
| function pendingPatchIds(root: string, base: string): Set<string> { | ||
| const ids = [branchPatchId(root, base, 'HEAD'), patchIdOf(root, ['--cached', base])] | ||
| const scratch = mkdtempSync(join(tmpdir(), 'agent-index-')) | ||
| try { | ||
| const index = git(root, ['rev-parse', '--path-format=absolute', '--git-path', 'index']) | ||
| const copy = join(scratch, 'index') | ||
| const real = index.stdout.trim() | ||
| if (index.ok && existsSync(real)) copyFileSync(real, copy) | ||
| const env = { GIT_INDEX_FILE: copy } | ||
| if (git(root, ['add', '--all'], { env }).ok) | ||
| ids.push(patchIdOf(root, ['--cached', base], env)) | ||
| } finally { | ||
| rmSync(scratch, { recursive: true, force: true }) | ||
| } | ||
| return new Set(ids.filter((id) => id !== undefined)) | ||
| } | ||
|
|
||
| /** | ||
| * Records that the changes `commit` makes since `base` passed verify. Best effort: a record that | ||
| * can't be written only means the next rebase needs verify again. | ||
| */ | ||
| export function recordPassedPatch(root: string, base: string, commit: string): void { | ||
| try { | ||
| const patchId = branchPatchId(root, base, commit) | ||
| if (patchId === undefined) return | ||
| const others = readPassedPatches(root).filter( | ||
| (patch) => patch.patchId !== patchId || patch.base !== base, | ||
| ) | ||
| const patches = [...others, { patchId, base, at: new Date().toISOString() }] | ||
| writeJson(patchesPath(root), { patches: patches.slice(-MAX_RECORDED) }) | ||
| } catch { | ||
| // Nothing to undo: the push or the verify run it follows already succeeded. | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Forgets that the changes verify is about to check passed, whatever base they passed on, so a run | ||
| * that doesn't pass can't leave them counted as passed: HEAD's changes since `base`, and the ones a | ||
| * commit could hold next, staged or not, as after `git reset HEAD~1`. When the record can't be | ||
| * rewritten it is deleted, and when it can't be deleted either, this throws. | ||
| */ | ||
| export function forgetPassedChanges(root: string, base: string): void { | ||
| const patches = readPassedPatches(root) | ||
| if (patches.length === 0) return | ||
| const pending = pendingPatchIds(root, base) | ||
| const kept = patches.filter((patch) => !pending.has(patch.patchId)) | ||
| if (kept.length === patches.length) return | ||
| try { | ||
| writeJson(patchesPath(root), { patches: kept }) | ||
| } catch { | ||
| // Losing the other branches' records only means they need verify again after a rebase. | ||
| rmSync(patchesPath(root), { force: true }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Whether `commit` holds changes that passed verify, from `base` or from an earlier point of the | ||
| * default branch that `base` has moved on from. | ||
| */ | ||
| export function hasPassedPatch(root: string, base: string, commit: string): boolean { | ||
| const patches = readPassedPatches(root) | ||
| if (patches.length === 0) return false | ||
| const patchId = branchPatchId(root, base, commit) | ||
| if (patchId === undefined) return false | ||
| return patches.some( | ||
| (patch) => | ||
| patch.patchId === patchId && | ||
| git(root, ['merge-base', '--is-ancestor', patch.base, base]).ok, | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.