Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Merge batch 11-06 k0520: #3279 (#3281)

0xken15b61d6c494da6
diff --git a/backend/src/github/train.test.ts b/backend/src/github/train.test.ts
index d486ace27..8640995c5 100644
--- a/backend/src/github/train.test.ts
+++ b/backend/src/github/train.test.ts
@@ -103,6 +103,13 @@ describe('the merge train on GitHub', () => {
expect(calls.map(c => c.method)).toEqual(['PUT', 'POST', 'PATCH', 'DELETE'])
})
+ it('reads a pull request whose base or head branch name hides a control or format character as unreadable', async () => {
+ for (const bad of [{ base: { ref: 'main\u200b' } }, { base: { ref: 'ma\u202ein' } }, { head: { sha: SHA, ref: 'feature\u0007', repo: { full_name: 'acme/app' } } }]) {
+ const { train } = stub({ 'GET /repos/acme/app/pulls/7': { status: 200, body: pullBody(7, bad) } })
+ await expect(train.pull(7)).rejects.toBeInstanceOf(TrainReadError)
+ }
+ })
+
it('reads a fork\'s pull request as from another repository', async () => {
const { train } = stub({ 'GET /repos/acme/app/pulls/7': { status: 200, body: pullBody(7, { head: { sha: SHA, ref: 'harness-train/p1/1', repo: { full_name: 'outsider/app' } } }) } })
expect(train.repository).toBe('acme/app')
diff --git a/backend/src/github/train.ts b/backend/src/github/train.ts
index c0a075088..9cf1ac9ba 100644
--- a/backend/src/github/train.ts
+++ b/backend/src/github/train.ts
@@ -73,6 +73,9 @@ const PASSED = new Set(['success', 'neutral'])
type Json = Record<string, unknown>
const object = (value: unknown): Json => value && typeof value === 'object' && !Array.isArray(value) ? value as Json : {}
const text = (value: unknown): string | null => typeof value === 'string' ? value : null
+/** A branch name with no control or format character: one with a zero-width space or a bidi override can read as "main" on
+ * screen and be another branch (task #3493), so the train reads such a pull request as unreadable. */
+const plainRef = (value: unknown): value is string => typeof value === 'string' && !!value && !/[\p{Cc}\p{Cf}]/u.test(value)
/** The train's calls on `owner/name` [repository] through [call]. */
export function trainGitHub(call: TrainCall, repository: string): TrainGitHub {
@@ -99,7 +102,7 @@ export function trainGitHub(call: TrainCall, repository: string): TrainGitHub {
const body = object(value), head = object(body.head), into = object(body.base)
const state = body.state
if (typeof body.number !== 'number' || (number !== undefined && body.number !== number) || (state !== 'open' && state !== 'closed')
- || typeof body.draft !== 'boolean' || !text(body.html_url) || !text(head.ref) || !text(into.ref)) throw new TrainReadError(what, 200)
+ || typeof body.draft !== 'boolean' || !text(body.html_url) || !plainRef(head.ref) || !plainRef(into.ref)) throw new TrainReadError(what, 200)
const mergeable = body.mergeable === true || body.mergeable === false ? body.mergeable : null
return { number: body.number, url: text(body.html_url)!, state, merged: body.merged === true, draft: body.draft, title: text(body.title) ?? '',
body: text(body.body) ?? '', mergeable, head: sha(head.sha, what), headRef: text(head.ref)!,
diff --git a/backend/src/superTabs/mergeTrain/plan.test.ts b/backend/src/superTabs/mergeTrain/plan.test.ts
index 231534792..58817ce50 100644
--- a/backend/src/superTabs/mergeTrain/plan.test.ts
+++ b/backend/src/superTabs/mergeTrain/plan.test.ts
@@ -6,16 +6,16 @@ import { requiredReads, SEED_RULES } from '../policy.js'
const URL = 'https://github.com/acme/app/pull/7'
const HEAD = 'a'.repeat(40), OLD = 'b'.repeat(40)
-const check = (verdicts: Array<{ passed: boolean; head?: string; patch?: string | null; stale?: unknown; pullRequest?: string }>): Check => ({
+const check = (verdicts: Array<{ passed: boolean; head?: string; patch?: string | null; stale?: unknown; pullRequest?: string; base?: string }>): Check => ({
id: 'c1', description: 'Review', independent: true, evidence: null, passedBy: null,
- verdicts: verdicts.map((v, i) => ({ by: `agent:r${i}`, evidence: 'read', at: 1, pullRequest: URL, ...v })) as Check['verdicts'],
+ verdicts: verdicts.map((v, i) => ({ by: `agent:r${i}`, evidence: 'read', at: 1, pullRequest: URL, base: 'main', ...v })) as Check['verdicts'],
})
const step = (checks: Check[], over: Partial<Conversation> = {}): TrainTask['steps'][number] => ({
step: 'Review', title: 'Review', checks, archived: false, assignment: { status: 'done' } as Conversation['assignment'], ...over,
})
const task = (over: Partial<TrainTask> = {}): TrainTask => ({
checks: [check([{ passed: true, head: HEAD }])],
- pullRequestHeads: { [URL]: { head: HEAD, patch: 'p1', at: 1 } },
+ pullRequestHeads: { [URL]: { head: HEAD, patch: 'p1', at: 1, base: 'main' } },
steps: [step([check([{ passed: true, head: HEAD }])])],
...over,
})
@@ -26,66 +26,100 @@ const candidate = (over: Partial<TrainCandidate> = {}): TrainCandidate => ({
describe('headReads', () => {
it('counts passes recorded at the head GitHub shows now', () => {
- expect(headReads(task(), URL, HEAD)).toBe(1)
+ expect(headReads(task(), URL, HEAD, 'main')).toBe(1)
})
it('refuses a pass at an older head with another patch', () => {
const t = task({ steps: [step([check([{ passed: true, head: OLD, patch: 'p0' }])])] })
- expect(headReads(t, URL, HEAD)).toBe(null)
+ expect(headReads(t, URL, HEAD, 'main')).toBe(null)
})
it('keeps a pass at an older head whose patch is the same (a plain rebase)', () => {
const t = task({ steps: [step([check([{ passed: true, head: OLD, patch: 'p1' }])])] })
- expect(headReads(t, URL, HEAD)).toBe(1)
+ expect(headReads(t, URL, HEAD, 'main')).toBe(1)
})
it('never counts a pass with no head, though old clients still count it', () => {
const t = task({ steps: [step([check([{ passed: true }])])] })
- expect(headReads(t, URL, HEAD)).toBe(null)
+ expect(headReads(t, URL, HEAD, 'main')).toBe(null)
})
it('never counts a stale pass, or one on another pull request', () => {
- expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, stale: { head: OLD } }])])] }), URL, HEAD)).toBe(null)
- expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, pullRequest: 'https://github.com/acme/app/pull/8' }])])] }), URL, HEAD)).toBe(null)
+ expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, stale: { head: OLD } }])])] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, pullRequest: 'https://github.com/acme/app/pull/8' }])])] }), URL, HEAD, 'main')).toBe(null)
})
it('refuses when the watch has not seen this head yet', () => {
- expect(headReads(task({ pullRequestHeads: { [URL]: { head: OLD, patch: 'p1', at: 1 } } }), URL, HEAD)).toBe(null)
- expect(headReads(task({ pullRequestHeads: undefined }), URL, HEAD)).toBe(null)
+ expect(headReads(task({ pullRequestHeads: { [URL]: { head: OLD, patch: 'p1', at: 1, base: 'main' } } }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ pullRequestHeads: undefined }), URL, HEAD, 'main')).toBe(null)
})
it('refuses a failure at the head next to a pass', () => {
const t = task({ steps: [step([check([{ passed: true, head: HEAD }, { passed: false, head: HEAD }])])] })
- expect(headReads(t, URL, HEAD)).toBe(null)
+ expect(headReads(t, URL, HEAD, 'main')).toBe(null)
})
it('needs a review step, every open step done, and the task checks too', () => {
- expect(headReads(task({ steps: [] }), URL, HEAD)).toBe(null)
- expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD }])], { step: 'Test', title: 'Test' })] }), URL, HEAD)).toBe(null)
- expect(headReads(task({ steps: [...task().steps, step([check([{ passed: true, head: HEAD }])], { step: 'Test', title: 'Test', assignment: { status: 'working' } as Conversation['assignment'] })] }), URL, HEAD)).toBe(null)
- expect(headReads(task({ steps: [...task().steps, step([], { step: 'Test', title: 'Test' })] }), URL, HEAD)).toBe(null)
- expect(headReads(task({ checks: [check([{ passed: true, head: OLD, patch: 'p0' }])] }), URL, HEAD)).toBe(null)
+ expect(headReads(task({ steps: [] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD }])], { step: 'Test', title: 'Test' })] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ steps: [...task().steps, step([check([{ passed: true, head: HEAD }])], { step: 'Test', title: 'Test', assignment: { status: 'working' } as Conversation['assignment'] })] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ steps: [...task().steps, step([], { step: 'Test', title: 'Test' })] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ checks: [check([{ passed: true, head: OLD, patch: 'p0' }])] }), URL, HEAD, 'main')).toBe(null)
})
it('counts each reviewer once, over every review step passed at the head', () => {
const two = step([check([{ passed: true, head: HEAD }])], { step: 'Security review 2', title: 'Security review 2' })
two.checks[0]!.verdicts![0]!.by = 'agent:other'
- expect(headReads(task({ steps: [...task().steps, two] }), URL, HEAD)).toBe(2)
+ expect(headReads(task({ steps: [...task().steps, two] }), URL, HEAD, 'main')).toBe(2)
const same = step([check([{ passed: true, head: HEAD }])], { step: 'Security review 2', title: 'Security review 2' })
- expect(headReads(task({ steps: [...task().steps, same] }), URL, HEAD)).toBe(1)
+ expect(headReads(task({ steps: [...task().steps, same] }), URL, HEAD, 'main')).toBe(1)
})
it('never counts the task\'s own builders as reads: its assignee or an earlier attempt\'s agent', () => {
// The review step is passed by agent:r0 (the step helper's checker): as the builder, its pass counts for nothing, so the
// step has no pass at all and the pull request may not merge, however many other steps passed.
- expect(headReads(task({ builders: ['agent:r0'] }), URL, HEAD)).toBe(null)
+ expect(headReads(task({ builders: ['agent:r0'] }), URL, HEAD, 'main')).toBe(null)
const two = step([check([{ passed: true, head: HEAD }])], { step: 'Security review 2', title: 'Security review 2' })
two.checks[0]!.verdicts![0]!.by = 'agent:other'
- expect(headReads(task({ steps: [...task().steps, two], builders: ['agent:r0'] }), URL, HEAD)).toBe(null)
- expect(headReads(task({ steps: [...task().steps, two], builders: ['agent:someone-else'] }), URL, HEAD)).toBe(2)
+ expect(headReads(task({ steps: [...task().steps, two], builders: ['agent:r0'] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ steps: [...task().steps, two], builders: ['agent:someone-else'] }), URL, HEAD, 'main')).toBe(2)
})
it('never counts a builder\'s pass of a test step, but keeps the builder\'s own task checks', () => {
const tested = step([check([{ passed: true, head: HEAD }])], { step: 'Test', title: 'Test' })
tested.checks[0]!.verdicts![0]!.by = 'agent:builder'
- expect(headReads(task({ steps: [...task().steps, tested], builders: ['agent:builder'] }), URL, HEAD)).toBe(null)
+ expect(headReads(task({ steps: [...task().steps, tested], builders: ['agent:builder'] }), URL, HEAD, 'main')).toBe(null)
const own = task({ builders: ['agent:r0'] })
own.steps[0]!.checks[0]!.verdicts![0]!.by = 'agent:reviewer'
- expect(headReads(own, URL, HEAD)).toBe(1)
+ expect(headReads(own, URL, HEAD, 'main')).toBe(1)
})
it('ignores a cancelled step', () => {
const t = task({ steps: [...task().steps, step([], { step: 'Test', assignment: { status: 'cancelled' } as Conversation['assignment'] })] })
- expect(headReads(t, URL, HEAD)).toBe(1)
+ expect(headReads(t, URL, HEAD, 'main')).toBe(1)
+ })
+})
+
+describe('headReads at the base it read (task #3493)', () => {
+ it('counts a verdict at the exact head on the base GitHub shows now', () => {
+ expect(headReads(task(), URL, HEAD, 'main')).toBe(1)
+ })
+ it('never counts the same patch on a new base: a patch id names its base', () => {
+ const t = task({ pullRequestHeads: { [URL]: { head: HEAD, patch: 'p-main', at: 2, base: 'main' } },
+ steps: [step([check([{ passed: true, head: OLD, patch: 'p-release', base: 'release' }])])] })
+ expect(headReads(t, URL, HEAD, 'main')).toBe(null)
+ })
+ it('never counts a verdict once the pull request was retargeted: the live base differs from the watched one', () => {
+ // Ledger's case on #3218: passed at this head against another branch, retargeted to main before the watch's next pass.
+ const t = task({ pullRequestHeads: { [URL]: { head: HEAD, patch: 'p-release', at: 1, base: 'release' } },
+ checks: [check([{ passed: true, head: HEAD, base: 'release' }])], steps: [step([check([{ passed: true, head: HEAD, base: 'release' }])])] })
+ expect(headReads(t, URL, HEAD, 'main')).toBe(null)
+ })
+ it('never counts a verdict naming no base, or another base, at the same head', () => {
+ expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, base: undefined }])])] }), URL, HEAD, 'main')).toBe(null)
+ expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, base: 'release' }])])] }), URL, HEAD, 'main')).toBe(null)
+ })
+ it('never counts a verdict naming no pull request, which the shared check reads against the latest watched head', () => {
+ expect(headReads(task({ steps: [step([check([{ passed: true, head: HEAD, pullRequest: undefined }])])] }), URL, HEAD, 'main')).toBe(null)
+ })
+ it('reads nothing from a head recorded before bases', () => {
+ expect(headReads(task({ pullRequestHeads: { [URL]: { head: HEAD, patch: 'p1', at: 1 } } }), URL, HEAD, 'main')).toBe(null)
+ })
+ it('reads nothing when a base holds a control or format character, however it reads on screen', () => {
+ const hidden = 'main\u200b'
+ const t = task({ pullRequestHeads: { [URL]: { head: HEAD, patch: 'p1', at: 1, base: hidden } },
+ checks: [check([{ passed: true, head: HEAD, base: hidden }])], steps: [step([check([{ passed: true, head: HEAD, base: hidden }])])] })
+ expect(headReads(t, URL, HEAD, hidden)).toBe(null)
+ expect(headReads(task(), URL, HEAD, 'main\u202e')).toBe(null)
})
})
diff --git a/backend/src/superTabs/mergeTrain/plan.ts b/backend/src/superTabs/mergeTrain/plan.ts
index 83f85eebe..02ad82488 100644
--- a/backend/src/superTabs/mergeTrain/plan.ts
+++ b/backend/src/superTabs/mergeTrain/plan.ts
@@ -3,7 +3,8 @@
* Swarm and hands the answers here. Every unknown reads as "not yet": a pull request merged with too little evidence cannot
* be taken back, and one that waits a pass costs ten minutes.
*/
-import type { Check, CheckVerdict, Conversation } from '../model.js'
+import { verdictCounts, type PullRequestHeads } from '../checkVerdicts.js'
+import type { Check, CheckVerdict, Conversation, PullRequestHead } from '../model.js'
import { FAST_LANE, TRAIN_HOLD_LABELS } from './config.js'
/** A pull request as the train reads it from GitHub this pass. */
@@ -22,10 +23,6 @@ export interface TrainCandidate {
ciRequired: 'success' | 'failure' | 'pending' | null
}
-/** The verdict fields task #3394 adds (checkVerdicts.ts there): the pull request and head it was given at, the head's patch
- * id, and where it stopped counting. Optional here so this reads verdicts written before them, which never count. */
-type HeadVerdict = CheckVerdict & { pullRequest?: string; head?: string; patch?: string | null; stale?: unknown }
-interface PullRequestHead { head: string; patch: string | null; at: number }
/** The parts of a task, and of its steps, the train reads. */
export interface TrainTask {
@@ -39,20 +36,21 @@ export interface TrainTask {
}
const REVIEW_STEP = /review/i
-const MIN_PREFIX = 7
+/** A branch name with a control or format character (a zero-width space, a bidi override) can read as "main" on screen and
+ * be another branch: the train reads nothing from one (task #3493). */
+const HIDDEN = /[\p{Cc}\p{Cf}]/u
+const plainBase = (base: string | undefined): base is string => !!base && !HIDDEN.test(base)
-/** Whether [verdict] counts at [url]'s current head [now]: given at that head (a 7+ character prefix of it, as #3394 reads
- * one), or at a head whose patch is the same. A verdict with no head never counts here, though old clients still count it:
- * merging needs the head it read. */
-function countsAt(verdict: HeadVerdict, url: string, now: PullRequestHead): boolean {
- if (!verdict.head || verdict.stale || verdict.pullRequest !== url) return false
- if (verdict.head.length >= MIN_PREFIX && now.head.startsWith(verdict.head)) return true
- return !!verdict.patch && verdict.patch === now.patch
-}
+/** Whether [verdict] counts for [url]: the shared verdict check (checkVerdicts.ts, #3394: its head on its base, or the same
+ * patch, whose id names its base), on a verdict naming this pull request and the head it read, and not marked stale (an
+ * empty mark too: its head not read yet). Never a verdict without them, though the shared check reads one of those
+ * against the task's latest watched head. */
+const countsFor = (verdict: CheckVerdict, url: string, heads: PullRequestHeads): boolean =>
+ verdict.pullRequest === url && !!verdict.head && verdict.stale === undefined && verdictCounts(verdict, heads)
/** The checkers whose passes count on [check], or null when it has no pass that counts or a failure that does. */
-function passers(check: Check, url: string, now: PullRequestHead, ignored: ReadonlySet<string> = new Set()): string[] | null {
- const counting = ((check.verdicts ?? []) as HeadVerdict[]).filter(v => !ignored.has(v.by) && countsAt(v, url, now))
+function passers(check: Check, url: string, heads: PullRequestHeads, ignored: ReadonlySet<string> = new Set()): string[] | null {
+ const counting = (check.verdicts ?? []).filter(v => !ignored.has(v.by) && countsFor(v, url, heads))
if (!counting.length || counting.some(v => !v.passed)) return null
return counting.map(v => v.by)
}
@@ -63,17 +61,19 @@ function passers(check: Check, url: string, now: PullRequestHead, ignored: Reado
* review step passed, or the task's own checks or another step (a test) did not pass at this head. A cancelled step does not
* count either way; any other step must be done.
*/
-export function headReads(task: TrainTask, url: string, head: string): number | null {
- const now = task.pullRequestHeads?.[url]
- if (!now || now.head !== head) return null
- if (!task.checks.every(check => passers(check, url, now))) return null
+export function headReads(task: TrainTask, url: string, head: string, base: string): number | null {
+ const heads = task.pullRequestHeads, now = heads?.[url]
+ // GitHub's live head and base must be the ones the watch read (task #3493, Ledger's case on #3218): a pull request retargeted
+ // since then carries verdicts read against another branch, which the shared check still counts until the watch reads it.
+ if (!heads || !now || now.head.toLowerCase() !== head.toLowerCase() || !plainBase(now.base) || !plainBase(base) || now.base !== base) return null
+ if (!task.checks.every(check => passers(check, url, heads))) return null
const readers = new Set<string>(), builders = new Set(task.builders ?? [])
let reviewed = false
for (const step of task.steps) {
const status = step.assignment?.status
if (status === 'cancelled') continue
if (status !== 'done' || !step.checks.length) return null
- const found = step.checks.map(check => passers(check, url, now, builders))
+ const found = step.checks.map(check => passers(check, url, heads, builders))
if (found.some(list => !list)) return null
if (REVIEW_STEP.test(step.step ?? step.title)) {
reviewed = true
diff --git a/backend/src/superTabs/mergeTrain/rulesRecord.test.ts b/backend/src/superTabs/mergeTrain/rulesRecord.test.ts
index 8268e731e..8c54b2ee6 100644
--- a/backend/src/superTabs/mergeTrain/rulesRecord.test.ts
+++ b/backend/src/superTabs/mergeTrain/rulesRecord.test.ts
@@ -17,10 +17,10 @@ const MINUTE = 60_000
/** A task whose review step passed at [head] of [url], once by each of [reviewers]. */
function approvedTask(url: string, head: string, reviewers: string[]): TrainTask {
const check = (by: string): Check => ({ id: `c-${by}`, description: 'Review', independent: true, evidence: 'ok', passedBy: by,
- verdicts: [{ by, passed: true, evidence: 'ok', at: 1, pullRequest: url, head, patch: `patch-${head}` }] as unknown as Check['verdicts'] })
+ verdicts: [{ by, passed: true, evidence: 'ok', at: 1, pullRequest: url, head, patch: `patch-${head}`, base: 'main' }] as unknown as Check['verdicts'] })
return {
checks: [],
- pullRequestHeads: { [url]: { head, patch: `patch-${head}`, at: 1 } },
+ pullRequestHeads: { [url]: { head, patch: `patch-${head}`, at: 1, base: 'main' } },
steps: reviewers.map(by => ({ step: 'Review', title: 'Review', archived: false, checks: [check(by)], assignment: { status: 'done' } as Conversation['assignment'] })),
}
}
diff --git a/backend/src/superTabs/mergeTrain/train.test.ts b/backend/src/superTabs/mergeTrain/train.test.ts
index 9ca70dde2..d581d3041 100644
--- a/backend/src/superTabs/mergeTrain/train.test.ts
+++ b/backend/src/superTabs/mergeTrain/train.test.ts
@@ -11,10 +11,10 @@ const ON: TrainRules = { enabled: true, rules: SEED_RULES }
/** A task whose one review step passed at [head] of [url]. */
function approvedTask(url: string, head: string, reviewers = ['agent:reviewer']): TrainTask {
const check = (by: string): Check => ({ id: `c-${by}`, description: 'Review', independent: true, evidence: 'ok', passedBy: by,
- verdicts: [{ by, passed: true, evidence: 'ok', at: 1, pullRequest: url, head, patch: `patch-${head}` }] as unknown as Check['verdicts'] })
+ verdicts: [{ by, passed: true, evidence: 'ok', at: 1, pullRequest: url, head, patch: `patch-${head}`, base: 'main' }] as unknown as Check['verdicts'] })
return {
checks: [],
- pullRequestHeads: { [url]: { head, patch: `patch-${head}`, at: 1 } },
+ pullRequestHeads: { [url]: { head, patch: `patch-${head}`, at: 1, base: 'main' } },
steps: reviewers.map(by => ({ step: 'Review', title: 'Review', archived: false, checks: [check(by)], assignment: { status: 'done' } as Conversation['assignment'] })),
}
}
@@ -70,6 +70,19 @@ describe('the merge train builds batches', () => {
expect(repo.batches().map(p => repo.state.contents.get(p.head))).toEqual([[b.head]])
})
+ it('never batches a pull request retargeted to main after its review was read against another branch (Ledger, #3218)', async () => {
+ const { repo, swarm, pass } = setup()
+ const a = repo.addPull(7, ['backend/src/a.ts'])
+ swarm.approve(a)
+ // The review passed at this head against "release", and the watch last read it there; GitHub now shows it into main.
+ const task = swarm.tasks.get('task-7')!.task
+ task.pullRequestHeads![a.url] = { ...task.pullRequestHeads![a.url]!, base: 'release' }
+ for (const step of task.steps) for (const check of step.checks) for (const verdict of check.verdicts!) verdict.base = 'release'
+ await pass()
+ expect(repo.batches()).toEqual([])
+ expect(repo.state.squashes).toEqual([])
+ })
+
it('never batches a pull request whose verdict is not at its head', async () => {
const { repo, swarm, pass } = setup()
const a = repo.addPull(7, ['backend/src/a.ts'])
diff --git a/backend/src/superTabs/mergeTrain/train.ts b/backend/src/superTabs/mergeTrain/train.ts
index ec5ec2e1a..a13d4795e 100644
--- a/backend/src/superTabs/mergeTrain/train.ts
+++ b/backend/src/superTabs/mergeTrain/train.ts
@@ -75,7 +75,7 @@ export async function trainPass(input: { projectId: string; github: TrainGitHub;
const pull = await github.pull(number)
if (pull.state !== 'open' || pull.url.toLowerCase() !== linked.url.toLowerCase() || pull.headRef.startsWith(TRAIN_BRANCH_PREFIX)) return null
const files = await github.files(number), ciRequired = await github.ciRequired(pull.head)
- const reads = headReads(linked.task, linked.url, pull.head), required = requiredReads(rules.rules, files)
+ const reads = headReads(linked.task, linked.url, pull.head, pull.base), required = requiredReads(rules.rules, files)
const verdict = eligible({ url: linked.url, number, head: pull.head, state: pull.state, draft: pull.draft, base: pull.base, mergeable: pull.mergeable,
labels: pull.labels, files, ciRequired }, reads, required)
return { member: { number, url: linked.url, head: pull.head, title: pull.title, files, taskId: linked.taskId }, verdict, reads, required, lane: laneOf(files) }
diff --git a/docs/plans/2026-11-06-train-verdicts-at-base-plan.md b/docs/plans/2026-11-06-train-verdicts-at-base-plan.md
new file mode 100644
index 000000000..9f634a666
--- /dev/null
+++ b/docs/plans/2026-11-06-train-verdicts-at-base-plan.md
@@ -0,0 +1,62 @@
+# The merge train counts a review only at the version and base it read: implementation plan
+
+> Written by the project's Engineer way of working (project instructions, Engineer): plan first, then each task test first,
+> the failing test committed before its code. No Execution Handoff.
+
+**Goal:** The merge train counts a review or test verdict only through the shared verdict check (head and base, #3394), and
+only while GitHub's live base for the pull request is the base the PR watch recorded, so a verdict read against another
+branch never merges anything (task #3493, requested by Dee through Harness).
+
+**Architecture:** `mergeTrain/plan.ts` drops its own head/patch match (`countsAt`) and calls `verdictCounts` from
+`superTabs/checkVerdicts.ts` on the task's `pullRequestHeads`. `headReads` gains the live base: it reads nothing when the
+watched head entry is missing, names another head, has no base, names another base than GitHub's live one, or either base
+holds a control or format character. `train.ts` passes the live base it read. `github/train.ts` refuses a pull request whose
+base or head branch name holds such a character.
+
+**Tech Stack:** TypeScript backend, vitest, the fake GitHub of `mergeTrain/fakeTrainGitHub.ts`.
+
+**Spec:** task #3493's brief; Verdict's notes in #3394 (base recorded per verdict; a patch id hashes its base) and Ledger's
+retarget reproduction on #3218.
+
+## Global Constraints
+
+- PR body first line: "Requested by Dee through Harness."
+- Owns `backend/src/superTabs/mergeTrain/` and `backend/src/github/train.ts`.
+- Fail closed: `verdictCounts` counts any verdict on a task never watched; the train requires a watched entry first, and a
+ verdict naming its pull request.
+- The train stays off for every project (project rules) until a project switches it on.
+
+## Design
+
+- growth, concurrency, read cost, scale, migration: N/A because nothing new is stored or read: the same verdicts and heads,
+ compared differently.
+- compatibility: a head recorded before bases (no `base`) no longer lets the train merge until the watch reads it again.
+- audience, identity: N/A because nothing new is exposed and nothing acts as a person.
+- failure: a refused pull request is "not eligible" for that pass, as any other unverified one.
+- platforms: backend only.
+
+## Review Focus
+
+- A retarget between the watch's last read and the train's pass: GitHub's live base differs from the watched one.
+- The same patch on another base: the patch id differs, so it never counts.
+- A base name with a zero-width or bidi character that reads as "main".
+
+### Task 1: the shared check, at the live base
+
+**Files:** `backend/src/superTabs/mergeTrain/plan.ts`, `plan.test.ts`
+
+**Interfaces:** `headReads(task: TrainTask, url: string, head: string, base: string): number | null`.
+
+- [ ] Test: a verdict at the same head and base counts; the same patch on a new base does not; a watched base other than
+ the live base (retarget) does not; a verdict with no head never counts; a watched entry with no base does not; a base with
+ a format character does not.
+- [ ] Code: call `verdictCounts`, require `verdict.pullRequest === url`, delete `countsAt` and `MIN_PREFIX`.
+
+### Task 2: the train reads at the live base
+
+**Files:** `backend/src/superTabs/mergeTrain/train.ts`, `train.test.ts`, `backend/src/github/train.ts`, `github/train.test.ts`
+
+- [ ] Test (train): Ledger's case: a review passed at head A against another branch, the PR retargeted to main before the
+ watch's next pass: never batched.
+- [ ] Test (GitHub): a pull request whose base or head branch name holds a control or format character reads as unreadable.
+- [ ] Code.