From 6d877bae4ce90a3660a1c4a6b88ec5c931b81014 Mon Sep 17 00:00:00 2001 From: John Doe <123843277+scuttletoby@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:09:40 +0100 Subject: [PATCH 1/2] seam guard: fork-seams.json is a gate file; a seam file may get lost upstream lines back (#968) - .github/fork-pr-queue.json: the gate rule lists .github/fork-seams.json, so a change to the upstream-seam list needs the merge-path and state lenses (and the code lens with other files), as docs/fork/updates.md says ("reviewed like any gate change"). - fork-seam-check.cjs: a pull request that lowers a seam file's "lost" entry in its own list (neither count raised) may put those upstream lines back: one added line per "gone" lowered, one lost line of main's file per "lines" lowered beyond those. The run from upstream holds the lowered entry exact, so a fork line in place of upstream's is still refused there. With "lost" left as main's, the refusal says to lower it. Co-Authored-By: Claude Opus 5.5 --- .github/fork-pr-queue.json | 1 + .github/scripts/fork-seam-check.cjs | 66 +++++++++-- .github/scripts/fork-seam-check.test.cjs | 108 ++++++++++++++++++ docs/fork/updates.md | 11 +- fork-ci/actions/head-check/head_check.test.py | 29 +++-- fork-ci/actions/pr-queue/review_judge.test.py | 27 +++-- 6 files changed, 213 insertions(+), 29 deletions(-) diff --git a/.github/fork-pr-queue.json b/.github/fork-pr-queue.json index 9e74fac4ad122..4fc63e4edd8e8 100644 --- a/.github/fork-pr-queue.json +++ b/.github/fork-pr-queue.json @@ -35,6 +35,7 @@ ".github/fork-removed-workflows.txt", ".github/fork-review-kit.json", ".github/fork-secret-sweep-gone-tips.txt", + ".github/fork-seams.json", ".github/fork-tests-upstream.json", ".github/fork-upstream.json", "docs/fork/gate-trust.md", diff --git a/.github/scripts/fork-seam-check.cjs b/.github/scripts/fork-seam-check.cjs index 09de37461f842..1f278dca36037 100644 --- a/.github/scripts/fork-seam-check.cjs +++ b/.github/scripts/fork-seam-check.cjs @@ -165,7 +165,10 @@ function parseRemoved(text = "") { * in-date copies go to `late`. `atTip` (a PR's run: the base tip) spends the allowance too, so two branches * from one merge-base cannot each add the one listed copy once the first has merged. */ -function findSeamBreaches(files, { atBase, atHead, atTip, upstream = false, today, late }) { +function findSeamBreaches( + files, + { atBase, atHead, atTip, upstream = false, today, late, restorable, lost }, +) { const reasons = []; const lateReasons = late ?? reasons; for (const [file, seam] of Object.entries(files)) { @@ -199,6 +202,18 @@ function findSeamBreaches(files, { atBase, atHead, atTip, upstream = false, toda const allowance = counts.get(line) ?? 0; return upstream ? allowance : Math.max(0, allowance - spent(line)); }; + // `restorable` (restorableLost): lines put back for the own "lost" entry's lowered `gone`. With + // none, main's "lost" (`lost`) names the way out for a line that may be one put back. + let restore = upstream ? 0 : (restorable?.get(file)?.gone ?? 0); + const putBack = + !upstream && + restore === 0 && + lost !== undefined && + Object.hasOwn(lost, file) && + lost[file].gone > 0 + ? ` (if it puts back an upstream line counted in "lost", lower that entry in this change's ${CONFIG})` + : ""; + // The hint only where the base branch's newer copies alone make these too many: judged from // the merge-base (the copies the announcement allows in date), the branch would pass. // `counts` is the allowance the copies are judged by: in date for `late`, else `allowed` (an @@ -209,10 +224,12 @@ function findSeamBreaches(files, { atBase, atHead, atTip, upstream = false, toda added <= (counts.get(line) ?? 0) - (baseCounts.get(line) ?? 0) ? " (the base branch has more copies than this branch's merge-base: update the branch from its base)" : "" - }`; + }${putBack}`; for (const [line, added] of addedLines(base, head)) { const left = leftOf(allowed, line); - const extra = added - left; + const back = Math.min(Math.max(added - left, 0), restore); + restore -= back; + const extra = added - left - back; if (extra <= 0) continue; // Only the copies the announcement would allow in date are its to fix; the rest are refused // either way. @@ -475,7 +492,10 @@ function findUnlistedChanges( * `rises`. On a PR only "added" lines may go: an announced line in main's file is upstream's. * `lowered.lost` gets each entry a run from upstream finds too high: its counts ({0, 0}: remove it). */ -function findLostLines(config, { atBase, atHead, upstream, afterRebase = false, lowered }) { +function findLostLines( + config, + { atBase, atHead, upstream, afterRebase = false, lowered, restorable }, +) { const reasons = []; const ceilings = []; const rises = []; @@ -504,7 +524,8 @@ function findLostLines(config, { atBase, atHead, upstream, afterRebase = false, (sum, [line, copies]) => sum + Math.min(copies, listed.get(line) ?? 0), 0, ); - const count = lines - dropped; + // And lines moved or re-indented back for the own "lost" entry (restorableLost). + const count = lines - dropped - (restorable?.get(file)?.lines ?? 0); if (count > 0) { reasons.push( `${file}: ${say(count, what.lines)} by this change (only its listed seam lines may go, and none may move: a moved copy looks like upstream's line).`, @@ -610,6 +631,27 @@ function findSpareSeamLines( * The "pending" files this change's own list moves to "files" (main's list has them in "pending", * this change's in "files" and not in "pending"): main's list cannot tell which lines they hold. */ +/** + * Main's seam files whose "lost" entry the own list lowers (neither raised), among `counted` (held exact + * from upstream): `gone` lowered allows that many lines put back; `lines` lowered beyond it, that many + * moved or re-indented back. A fork line in their place lowers neither count, so upstream's run refuses it. + */ +function restorableLost(config, ownLost, counted) { + const restorable = new Map(); + if (ownLost === undefined) return restorable; + const of = (lost, file) => + lost !== undefined && Object.hasOwn(lost, file) ? lost[file] : { lines: 0, gone: 0 }; + for (const file of counted) { + if (!Object.hasOwn(config.files, file)) continue; + const [main, own] = [of(config.lost, file), of(ownLost, file)]; + if (own.lines > main.lines || own.gone > main.gone) continue; + const gone = main.gone - own.gone; + const lines = Math.max(0, main.lines - own.lines - gone); + if (gone > 0 || lines > 0) restorable.set(file, { gone, lines }); + } + return restorable; +} + const promotedFiles = (config, ownFiles = {}, ownPending = {}) => Object.keys(config.pending ?? {}).filter( (file) => Object.hasOwn(ownFiles, file) && !Object.hasOwn(ownPending, file), @@ -1556,12 +1598,19 @@ function main(argv) { ownLost, lowered, }); + // The own list's edits that only upstream can check, counted below. + const edits = + upstream || ownConfig === undefined + ? { files: [], pending: [], lost: [], forkFiles: false } + : { files: listEditedFiles(baseFiles, ownFiles), ...listEditedCounts(baseList, ownConfig) }; + const restorable = restorableLost(config, ownLost, edits.lost); const lost = findLostLines(config, { atBase, atHead, upstream, afterRebase: parsed.afterRebase, lowered, + restorable, }); const spare = findSpareSeamLines(config.files, { atBase, @@ -1576,10 +1625,6 @@ function main(argv) { // and the pending files' text changes main's list cannot check: only upstream can. Every way the count cannot run refuses (fail // closed); fork-tests' run on the merge commit passes --no-upstream, as fork-checks' run counts. const promoted = upstream ? [] : promotedFiles(config, ownFiles, ownPending); - const edits = - upstream || ownConfig === undefined - ? { files: [], pending: [], lost: [], forkFiles: false } - : { files: listEditedFiles(baseFiles, ownFiles), ...listEditedCounts(baseList, ownConfig) }; // The "pending" files whose text this change edits. Main's tree cannot tell which line it drops // is upstream's: a swapped upstream line (one out, one in) keeps every count it sees, and // fork-follow then refuses main. Counted from upstream as fork-follow counts them: fork @@ -1711,6 +1756,8 @@ function main(argv) { today, late, atTip: upstream ? undefined : (file) => textAt(base, file), + restorable, + lost: config.lost, }), ...lost.reasons, ...unlisted.reasons, @@ -1867,6 +1914,7 @@ module.exports = { parseConfig, parseRemoved, promotedFiles, + restorableLost, sameLines, UPSTREAM_FILE, UPSTREAM_REF, diff --git a/.github/scripts/fork-seam-check.test.cjs b/.github/scripts/fork-seam-check.test.cjs index da0ce16965906..631410dca840d 100644 --- a/.github/scripts/fork-seam-check.test.cjs +++ b/.github/scripts/fork-seam-check.test.cjs @@ -4281,3 +4281,111 @@ test("a change that drops another change's in-date announcement is refused, unle assert.doesNotMatch(result.stderr, /drops the announced line/); }); }); + +// A seam file with a "lost" entry may get upstream's lines back. Each line the pull +// request's own "lost" lowers allows one put-back line, and the run from upstream holds that entry +// to the head's lost lines exactly, so a fork line added in its place is refused there. +test('a pull request that lowers a seam file\'s "lost" entry may put those upstream lines back', () => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { + const UPSTREAM_P = "a();\nb(x);\nc();\nif (y) {\n d();\n}\n"; + const upstream = commit({ "p.ts": UPSTREAM_P }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const entry = (...added) => ({ "p.ts": { why: "w", added } }); + const list = (added, lost) => ({ files: entry(...added), pending: {}, lost }); + // The fork changed b(x) (1 line gone) and re-indented d() (moved, its text kept). + const main = commit( + { + ...seams(list(["b(x, fork);", "forkHook();"], { "p.ts": { lines: 2, gone: 1 } })), + "p.ts": "a();\nb(x, fork);\nc();\nif (y) {\n d();\n}\nforkHook();\n", + }, + "main", + ); + publish(upstream); + assert.equal(fromUpstream(upstream, main).status, 0); + const pr = (name, own, text) => { + git("checkout", "-q", "-f", "-B", name, main); + return commit({ ...seams(own), "p.ts": text }, name); + }; + const passes = (head) => { + for (const args of [ + [main, head], + ["--no-upstream", main, head], + ]) { + const result = run(...args); + assert.equal(result.status, 0, `${args.join(" ")}: ${result.stderr}`); + } + assert.equal(fromUpstream(upstream, head).status, 0); + }; + // Both lines back, "lost" dropped; only b(x) back, "lost" lowered to the re-indented line. + passes(pr("both", list(["forkHook();"], {}), `${UPSTREAM_P}forkHook();\n`)); + passes( + pr( + "gone", + list(["forkHook();"], { "p.ts": { lines: 1, gone: 0 } }), + "a();\nb(x);\nc();\nif (y) {\n d();\n}\nforkHook();\n", + ), + ); + // Half-way: the lines are back but "lost" is main's. Refused as before, naming the way out. + const halfway = run( + main, + pr( + "halfway", + list(["forkHook();"], { "p.ts": { lines: 2, gone: 1 } }), + `${UPSTREAM_P}forkHook();\n`, + ), + ); + assert.equal(halfway.status, 1); + assert.match( + halfway.stderr, + /- p\.ts: adds `b\(x\);` \(if it puts back an upstream line counted in "lost", lower that entry in this change's \.github\/fork-seams\.json\)/, + ); + assert.match( + halfway.stderr, + /- p\.ts: 1 upstream line deleted, moved or re-indented by this change/, + ); + // "lost" lowered, but a fork line in place of upstream's: main's tree cannot tell, upstream can. + const swap = run( + main, + pr( + "swap", + list(["forkHook();"], {}), + "a();\nforkZ();\nc();\nif (y) {\n d();\n}\nforkHook();\n", + ), + ); + assert.equal(swap.status, 1, swap.stdout); + assert.match( + swap.stderr, + /"added" lists `forkZ\(\);` not at all, but the fork adds it over upstream/, + ); + assert.match( + swap.stderr, + /- p\.ts: dropped from "lost" .*\(no entry\), but the fork loses 1 upstream line over upstream \([0-9a-f]{12}\) .*1 of them gone/, + ); + // A line put back in place lowers both counts: no room to move a line too (a moved seam line + // looks like upstream's, so upstream's count would not see it). + const moved = run( + main, + pr( + "moved", + list(["forkHook();"], { "p.ts": { lines: 1, gone: 0 } }), + "forkHook();\na();\nb(x);\nc();\nif (y) {\n d();\n}\n", + ), + ); + assert.equal(moved.status, 1, moved.stdout); + assert.match( + moved.stderr, + /- p\.ts: 1 upstream line deleted, moved or re-indented by this change/, + ); + // Raising one count while lowering the other allows nothing. + const raised = run( + main, + pr( + "raised", + list(["forkHook();"], { "p.ts": { lines: 3, gone: 0 } }), + "a();\nb(x);\nc();\nif (y) {\n d();\n}\nforkHook();\n", + ), + ); + assert.equal(raised.status, 1); + assert.match(raised.stderr, /- p\.ts: adds `b\(x\);`/); + }); +}); diff --git a/docs/fork/updates.md b/docs/fork/updates.md index e536403450a12..0c457108dd15a 100644 --- a/docs/fork/updates.md +++ b/docs/fork/updates.md @@ -128,6 +128,12 @@ watches; it changes nothing on the machine, and `0` turns it off. (fork-follow lowers a count after a rebase, but never raises one). A pull request may drop a seam file's listed seam lines (no more copies than listed) but lose no other line, and may not move a seam line: a moved copy of `}` looks the same as upstream's (move one with two pull requests). + A seam file with a `lost` entry may get upstream's lines back (#968): a pull request that lowers + that entry in its own list (neither count raised) may add one line per `gone` it lowers, and lose + one line of main's file per `lines` it lowers beyond those (a line moved or re-indented back; a + line put back in place lowers both). Its run counts the lowered entry from upstream exactly, so a + fork line in place of upstream's is refused there. With `lost` left as main's, a line put back is + refused as an unlisted one, and the refusal says to lower the entry. A seam's `added` lists only the copies the fork adds: the run from upstream refuses a listed copy the fork does not add over upstream (a spare one would let a later change add that line unannounced), and right after a rebase that took the line word for word fork-follow drops it in @@ -195,7 +201,10 @@ watches; it changes nothing on the machine, and `0` turns it off. A pull request is judged by main's list, so a change that adds a line to an upstream file and its seam entry together is refused. It takes two pull requests, each green: -1. **Announce** (list only, reviewed like any gate change): add the lines under `announced` in the +1. **Announce** (list only, reviewed like any gate change: the `gate` rule of + [`.github/fork-pr-queue.json`](../../.github/fork-pr-queue.json) lists `.github/fork-seams.json`, + so a change to the list needs the merge-path and state lenses, and the code lens too when it + changes other files): add the lines under `announced` in the file's entry, with the day: `{"since": "YYYY-MM-DD", "added": [lines]}`. Set `since` to the day this pull request merges (refresh it if the reviews took days): its run warns when the announcement it adds would be refused within 3 days. A file with no entry yet gets one with diff --git a/fork-ci/actions/head-check/head_check.test.py b/fork-ci/actions/head-check/head_check.test.py index 8e7c4c68d52c1..d04a74a7a37cf 100644 --- a/fork-ci/actions/head-check/head_check.test.py +++ b/fork-ci/actions/head-check/head_check.test.py @@ -1707,16 +1707,25 @@ def test_the_rollback_to_shadow_needs_the_gate_lenses(self): # it changes the code, out = self.check(base, head, [self.said('merge-path', head), self.said('state', head)]) self.assertEqual(code, 0, out) - def test_an_app_change_that_adds_a_seam_or_a_test_line_needs_the_code_lens(self): # the dry run's wrong refusals - for extra in ('.github/fork-seams.json', '.github/fork-ci-tests.txt'): - with self.subTest(extra): - base, head = self.pair('apps/server/src/x.ts', extra) - code, out = self.check(base, head, [self.said('code', head)]) - self.assertEqual(code, 0, out) - self.assertIn('path rules (enforce): the path rules decide: normal, lenses code (', out) - code, out = self.check(base, head, [self.said('merge-path', head), self.said('state', head)]) - self.assertEqual(code, 1, out) # the gate lenses alone never read app code - self.assertIn('required lens without a read: code', out) + def test_an_app_change_that_adds_a_test_line_needs_the_code_lens(self): # the dry run's wrong refusals + base, head = self.pair('apps/server/src/x.ts', '.github/fork-ci-tests.txt') + code, out = self.check(base, head, [self.said('code', head)]) + self.assertEqual(code, 0, out) + self.assertIn('path rules (enforce): the path rules decide: normal, lenses code (', out) + code, out = self.check(base, head, [self.said('merge-path', head), self.said('state', head)]) + self.assertEqual(code, 1, out) # the gate lenses alone never read app code + self.assertIn('required lens without a read: code', out) + + def test_an_app_change_that_adds_a_seam_needs_the_gate_lenses_and_the_code_lens(self): # #968 item 1 + base, head = self.pair('apps/server/src/x.ts', '.github/fork-seams.json') + code, out = self.check(base, head, [self.said('code', head)]) + self.assertEqual(code, 1, out) # the seam guard's list is a gate setting: one code lens is not enough + self.assertIn('required lens without a read: merge-path, state', out) + code, out = self.check(base, head, [self.said('merge-path', head), self.said('state', head)]) + self.assertEqual(code, 1, out) # the gate lenses alone never read app code + self.assertIn('required lens without a read: code', out) + code, out = self.check(base, head, [self.said(x, head) for x in ('merge-path', 'state', 'code')]) + self.assertEqual(code, 0, out) def test_a_blocker_or_major_fails_and_the_owners_login_does_not_clear_it(self): # #99, #78 base, head = self.pair() diff --git a/fork-ci/actions/pr-queue/review_judge.test.py b/fork-ci/actions/pr-queue/review_judge.test.py index ed63b7bdc3de5..8043b23d9d9f3 100644 --- a/fork-ci/actions/pr-queue/review_judge.test.py +++ b/fork-ci/actions/pr-queue/review_judge.test.py @@ -537,12 +537,14 @@ def test_a_wrong_list_is_refused(self): # --- the fork's own config (issue #730): in enforce, workflow, queue, lens and other CI files critical (#82) --------- ROOT = os.path.join(HERE, '..', '..', '..') FORK_CONFIG = '.github/fork-pr-queue.json' -# The two .github files that are not workflow files or gate settings (#82): per-pull-request bookkeeping lists a CI -# check compares with the tree (fork-seam-check.cjs: every fork line in an upstream file is announced; fork-ci-tests.cjs: -# every test file is listed). Most fork pull requests add a line to one of them; as critical files they made the gate -# lenses (merge-path, state) the only required ones on plain app changes, and no lens read the code (the dry run of -# the switch, issue #82). Every other file under .github stays critical: a new one fails this test until it is sorted. -NOT_GATE = ('.github/fork-seams.json', '.github/fork-ci-tests.txt') +# The one .github file that is not a workflow file or gate setting (#82): a per-pull-request bookkeeping list a CI check +# compares with the tree (fork-ci-tests.cjs: every test file is listed). Most fork pull requests add a line to it; as a +# critical file alone it made the gate lenses (merge-path, state) the only required ones on plain app changes, and no +# lens read the code (the dry run of the switch, issue #82). .github/fork-seams.json was such a list too, but it is +# the upstream-seam guard's list (which fork lines upstream files may hold): #968 makes it critical, and an app change +# with it needs the code lens and the gate lenses (one extra review, below). Every other file under .github stays +# critical: a new one fails this test until it is sorted. +NOT_GATE = ('.github/fork-ci-tests.txt',) # fork-kit folders that are gate settings (#82): review is the lens-format checker and the lens skill (what a lens # report must say to count), gh-apps keeps the gate App's key and mints its tokens (the gate-trust design). The # other kits run machines and agents, and the merge gate reads none of them. @@ -579,7 +581,7 @@ def test_workflow_queue_gate_config_and_lens_files_are_critical(self): # #82 '.github/fork-actions-policies.json', '.github/scripts/fork-contracts.test.py', '.github/workflows/new.yml', '.github/new-folder/x.json', '.github/new.md', 'fork-ci/new.txt', 'fork-kit/review/lib/review_kit.py', 'fork-kit/gh-apps/libexec/x', 'fork-kit/review/SKILL.md', - 'docs/fork/gate-trust.md'): + 'docs/fork/gate-trust.md', '.github/fork-seams.json'): with self.subTest(path): j = self.judge(path) self.assertEqual((j.weight, j.lenses), ('critical', self.critical.lenses)) @@ -587,7 +589,7 @@ def test_workflow_queue_gate_config_and_lens_files_are_critical(self): # #82 j = self.judge('apps/server/src/x.ts') self.assertEqual((j.weight, j.lenses), ('normal', ('code',))) - def test_every_github_file_is_critical_but_the_two_bookkeeping_lists(self): # a new one must be sorted + def test_every_github_file_is_critical_but_the_bookkeeping_list(self): # a new one must be sorted top = sorted(os.listdir(os.path.join(ROOT, '.github'))) self.assertTrue(set(NOT_GATE) <= {f'.github/{x}' for x in top}, 'a bookkeeping list moved: update NOT_GATE') for name in top: @@ -630,12 +632,19 @@ def test_every_fork_kit_folder_is_sorted(self): # a new kit must be sorted too 'OTHER_KITS with why') self.assertEqual(weight, 'normal', f'{path} is in OTHER_KITS but judged {weight}') - def test_an_app_change_that_announces_a_seam_or_lists_a_test_needs_the_code_lens(self): # the dry run's refusals + def test_an_app_change_that_lists_a_test_needs_the_code_lens(self): # the dry run's refusals for extra in NOT_GATE: with self.subTest(extra): j = rj.judge(self.review, [('M', ('apps/server/src/x.ts',)), ('M', (extra,))], FORK_CONFIG) self.assertEqual((j.weight, j.lenses), ('normal', ('code',))) + def test_a_seam_list_change_needs_the_gate_lenses_and_with_code_the_code_lens(self): # #968 item 1 + seams = '.github/fork-seams.json' + j = self.judge(seams) # an announcement alone (a list-only pull request) + self.assertEqual((j.weight, j.lenses, j.hits[0].rule), ('critical', ('merge-path', 'state'), self.critical.name)) + j = rj.judge(self.review, [('M', ('apps/server/src/x.ts',)), ('M', (seams,))], FORK_CONFIG) + self.assertEqual((j.weight, j.lenses), ('critical', ('merge-path', 'state', 'code'))) + def test_an_app_change_plus_a_gate_file_needs_the_code_lens_and_the_gate_lenses(self): # #82: one EXTRA review for gate in ('.github/workflows/ci.yml', '.github/SECURITY.md', 'fork-ci/actions/x.py', 'fork-kit/review/lib/review_kit.py'): From 410cbe6d6551febfc0552eafdabe435f1f495ddd Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:33:21 +0000 Subject: [PATCH 2/2] seam guard: a line put back must return in place; hints name the way out (#968) A lowered "lost" `gone` now allows lines back only when `lines` drops at least as much: a line put back where upstream has it is in order again, so it lowers both. One put back out of order (upstream `lock(); work(); unlock();` as `lock(); unlock(); work();`) lowers `gone` alone and is refused, with a hint to put it where upstream has it. The refusal for a line moved or re-indented back now says to lower the entry, and refusals name updating the branch when main's "lost" entry changed since the merge-base. Each doc comment sits above its own function; the docs say what the gate rule does exactly. New tests: the out-of-place case, the counted-only rule, the stale-branch hints, the moved-back hint. Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-seam-check.cjs | 77 ++++++++--- .github/scripts/fork-seam-check.test.cjs | 160 +++++++++++++++++++++++ docs/fork/updates.md | 13 +- 3 files changed, 227 insertions(+), 23 deletions(-) diff --git a/.github/scripts/fork-seam-check.cjs b/.github/scripts/fork-seam-check.cjs index 1f278dca36037..002e0a5a20a12 100644 --- a/.github/scripts/fork-seam-check.cjs +++ b/.github/scripts/fork-seam-check.cjs @@ -167,7 +167,7 @@ function parseRemoved(text = "") { */ function findSeamBreaches( files, - { atBase, atHead, atTip, upstream = false, today, late, restorable, lost }, + { atBase, atHead, atTip, upstream = false, today, late, restorable, lost, staleLost }, ) { const reasons = []; const lateReasons = late ?? reasons; @@ -206,13 +206,7 @@ function findSeamBreaches( // none, main's "lost" (`lost`) names the way out for a line that may be one put back. let restore = upstream ? 0 : (restorable?.get(file)?.gone ?? 0); const putBack = - !upstream && - restore === 0 && - lost !== undefined && - Object.hasOwn(lost, file) && - lost[file].gone > 0 - ? ` (if it puts back an upstream line counted in "lost", lower that entry in this change's ${CONFIG})` - : ""; + !upstream && restore === 0 ? lostHint(file, "gone", { lost, restorable, staleLost }) : ""; // The hint only where the base branch's newer copies alone make these too many: judged from // the merge-base (the copies the announcement allows in date), the branch would pass. @@ -494,7 +488,7 @@ function findUnlistedChanges( */ function findLostLines( config, - { atBase, atHead, upstream, afterRebase = false, lowered, restorable }, + { atBase, atHead, upstream, afterRebase = false, lowered, restorable, staleLost }, ) { const reasons = []; const ceilings = []; @@ -527,8 +521,12 @@ function findLostLines( // And lines moved or re-indented back for the own "lost" entry (restorableLost). const count = lines - dropped - (restorable?.get(file)?.lines ?? 0); if (count > 0) { + // The way out for a line moved back where the own entry allows none (restorableLost). + const back = restorable?.get(file)?.lines + ? "" + : lostHint(file, "lines", { lost, restorable, staleLost }); reasons.push( - `${file}: ${say(count, what.lines)} by this change (only its listed seam lines may go, and none may move: a moved copy looks like upstream's line).`, + `${file}: ${say(count, what.lines)} by this change (only its listed seam lines may go, and none may move: a moved copy looks like upstream's line)${back}.`, ); } continue; @@ -628,13 +626,32 @@ function findSpareSeamLines( } /** - * The "pending" files this change's own list moves to "files" (main's list has them in "pending", - * this change's in "files" and not in "pending"): main's list cannot tell which lines they hold. + * The way out for a refused line that may be upstream's back (`key` of main's "lost" entry counts + * it): lower that entry, or put it where upstream has it when the own entry is `misplaced` + * (restorableLost). `staleLost`: files whose entry main changed since this branch's merge-base. */ +function lostHint(file, key, { lost, restorable, staleLost }) { + const misplaced = restorable?.get(file)?.misplaced; + const held = lost !== undefined && Object.hasOwn(lost, file) ? lost[file][key] : 0; + const hint = + misplaced !== undefined + ? ` (this change's ${CONFIG} lowers its "lost" gone by ${misplaced.gone} but lines by only ${misplaced.lines}: put each upstream line back where upstream has it, in order, which lowers both)` + : held > 0 + ? ` (if it ${key === "gone" ? "puts back" : "moves or re-indents back"} an upstream line counted in "lost", lower that entry in this change's ${CONFIG})` + : ""; + return hint === "" ? "" : `${hint}${staleHint(file, staleLost)}`; +} + +/** The update hint for a file whose "lost" entry main changed since this branch's merge-base. */ +const staleHint = (file, staleLost) => + staleLost?.has(file) + ? ` (this branch's merge-base has another "lost" entry for it than main: update the branch from main, whose list changed since)` + : ""; + /** - * Main's seam files whose "lost" entry the own list lowers (neither raised), among `counted` (held exact - * from upstream): `gone` lowered allows that many lines put back; `lines` lowered beyond it, that many - * moved or re-indented back. A fork line in their place lowers neither count, so upstream's run refuses it. + * Main's seam files whose "lost" entry the own list lowers (none raised), among `counted` (exact from + * upstream): lowered `gone` allows that many lines put back, `lines` beyond it that many moved back. + * A line put back in place lowers `lines` too; lowering `gone` more puts one out of order: `misplaced`. */ function restorableLost(config, ownLost, counted) { const restorable = new Map(); @@ -646,12 +663,21 @@ function restorableLost(config, ownLost, counted) { const [main, own] = [of(config.lost, file), of(ownLost, file)]; if (own.lines > main.lines || own.gone > main.gone) continue; const gone = main.gone - own.gone; - const lines = Math.max(0, main.lines - own.lines - gone); + const fewer = main.lines - own.lines; + if (fewer < gone) { + restorable.set(file, { gone: 0, lines: 0, misplaced: { gone, lines: fewer } }); + continue; + } + const lines = fewer - gone; if (gone > 0 || lines > 0) restorable.set(file, { gone, lines }); } return restorable; } +/** + * The "pending" files this change's own list moves to "files" (main's list has them in "pending", + * this change's in "files" and not in "pending"): main's list cannot tell which lines they hold. + */ const promotedFiles = (config, ownFiles = {}, ownPending = {}) => Object.keys(config.pending ?? {}).filter( (file) => Object.hasOwn(ownFiles, file) && !Object.hasOwn(ownPending, file), @@ -783,7 +809,7 @@ function findListedNotFork( * `atUpstream` (`name`), as fork-follow counts them: a count set too high lets a later change swap * an upstream line for a fork line unseen; too low, or dropped, stops fork-follow. */ -function findLostNotUpstream(files, ownLost, { atUpstream, atHead, name, wasLost }) { +function findLostNotUpstream(files, ownLost, { atUpstream, atHead, name, wasLost, staleLost }) { const ceilings = []; for (const file of files) { const own = Object.hasOwn(ownLost, file) ? ownLost[file] : undefined; @@ -813,7 +839,7 @@ function findLostNotUpstream(files, ownLost, { atUpstream, atHead, name, wasLost if (held.lines === lines && held.gone === gone) continue; const says = own === undefined ? "no entry" : `{"lines": ${own.lines}, "gone": ${own.gone}}`; ceilings.push( - `${file}: ${where} (${says}), but the fork loses ${plural(lines, "upstream line")} over upstream (${name}) (deleted, moved or re-indented), ${gone} of them gone: ${lines === 0 ? 'remove it from "lost"' : `set it to {"lines": ${lines}, "gone": ${gone}}`} (a count above the lines lost is room to swap one for a fork line later).`, + `${file}: ${where} (${says}), but the fork loses ${plural(lines, "upstream line")} over upstream (${name}) (deleted, moved or re-indented), ${gone} of them gone: ${lines === 0 ? 'remove it from "lost"' : `set it to {"lines": ${lines}, "gone": ${gone}}`} (a count above the lines lost is room to swap one for a fork line later)${staleHint(file, staleLost)}.`, ); } return ceilings; @@ -1604,6 +1630,18 @@ function main(argv) { ? { files: [], pending: [], lost: [], forkFiles: false } : { files: listEditedFiles(baseFiles, ownFiles), ...listEditedCounts(baseList, ownConfig) }; const restorable = restorableLost(config, ownLost, edits.lost); + // Main's "lost" entries that differ from the merge-base's: the branch's own may be stale. + const entryOf = (lost, file) => (Object.hasOwn(lost, file) ? lost[file] : { lines: 0, gone: 0 }); + const staleLost = new Set( + baseList === undefined + ? [] + : [...new Set([...Object.keys(config.lost), ...Object.keys(baseList.lost)])].filter( + (file) => { + const [a, b] = [entryOf(config.lost, file), entryOf(baseList.lost, file)]; + return a.lines !== b.lines || a.gone !== b.gone; + }, + ), + ); const lost = findLostLines(config, { atBase, atHead, @@ -1611,6 +1649,7 @@ function main(argv) { afterRebase: parsed.afterRebase, lowered, restorable, + staleLost, }); const spare = findSpareSeamLines(config.files, { atBase, @@ -1718,6 +1757,7 @@ function main(argv) { baseList === undefined ? undefined : new Set([...Object.keys(config.lost), ...Object.keys(baseList.lost)]), + staleLost, }), ...(edits.forkFiles ? findOwnHiddenFiles( @@ -1758,6 +1798,7 @@ function main(argv) { atTip: upstream ? undefined : (file) => textAt(base, file), restorable, lost: config.lost, + staleLost, }), ...lost.reasons, ...unlisted.reasons, diff --git a/.github/scripts/fork-seam-check.test.cjs b/.github/scripts/fork-seam-check.test.cjs index 631410dca840d..0d3253785e5ea 100644 --- a/.github/scripts/fork-seam-check.test.cjs +++ b/.github/scripts/fork-seam-check.test.cjs @@ -4389,3 +4389,163 @@ test('a pull request that lowers a seam file\'s "lost" entry may put those upstr assert.match(raised.stderr, /- p\.ts: adds `b\(x\);`/); }); }); + +// A line put back where upstream has it is in order again, so it lowers "lost" `lines` with `gone`. +// One put back elsewhere lowers `gone` alone: a fork change to the order of upstream's lines, which +// the count from upstream (exact either way) cannot see, so the pull request's run refuses it. +test('a line put back out of place is refused, though "lost" is lowered to its exact count', () => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { + const UPSTREAM_Q = "lock();\nwork();\nunlock();\n"; + const upstream = commit({ "q.ts": UPSTREAM_Q }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const list = (lost) => ({ files: { "q.ts": { why: "w", added: [] } }, pending: {}, lost }); + // The fork dropped unlock(): 1 line not in order, its text gone. + const main = commit( + { ...seams(list({ "q.ts": { lines: 1, gone: 1 } })), "q.ts": "lock();\nwork();\n" }, + "main", + ); + publish(upstream); + assert.equal(fromUpstream(upstream, main).status, 0); + const pr = (name, own, text) => { + git("checkout", "-q", "-f", "-B", name, main); + return commit({ ...seams(own), "q.ts": text }, name); + }; + const runs = (head) => [run(main, head), run("--no-upstream", main, head)]; + // In place: "lost" dropped, all three runs pass. + const inPlace = pr("in-place", list({}), UPSTREAM_Q); + for (const result of runs(inPlace)) assert.equal(result.status, 0, result.stderr); + assert.equal(fromUpstream(upstream, inPlace).status, 0); + // Out of place: {"lines": 1, "gone": 0} is the head's exact count (fork-follow passes it), but + // unlock() now runs before work(). + const outOfPlace = pr( + "out-of-place", + list({ "q.ts": { lines: 1, gone: 0 } }), + "lock();\nunlock();\nwork();\n", + ); + assert.equal(fromUpstream(upstream, outOfPlace).status, 0); + for (const result of runs(outOfPlace)) { + assert.equal(result.status, 1, result.stdout); + assert.match( + result.stderr, + /- q\.ts: adds `unlock\(\);` \(this change's \.github\/fork-seams\.json lowers its "lost" gone by 1 but lines by only 0: put each upstream line back where upstream has it, in order, which lowers both\)/, + ); + } + }); +}); + +// Only an own "lost" entry the pull request edits from its merge-base's list is counted from +// upstream, so only that one allows lines back: an entry left as the merge-base's, below main's +// newer one, is not checked by anyone and allows nothing. +test("an own \"lost\" entry left as the merge-base's allows no line back, though main's is higher", () => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { + const upstream = commit({ "p.ts": "a();\nb();\nc();\n" }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const list = (lost) => ({ + files: { "p.ts": { why: "w", added: ["b(fork);"] } }, + pending: {}, + lost, + }); + const before = commit( + { ...seams(list({ "p.ts": { lines: 1, gone: 1 } })), "p.ts": "a();\nb(fork);\nc();\n" }, + "fork changes b", + ); + // Main drops c() after the branch's merge-base: its entry rises to {2, 2}. + const main = commit( + { ...seams(list({ "p.ts": { lines: 2, gone: 2 } })), "p.ts": "a();\nb(fork);\n" }, + "main", + ); + publish(upstream); + assert.equal(fromUpstream(upstream, main).status, 0); + // The branch keeps its merge-base's list ({1, 1}, below main's) and adds only a fork line. + git("checkout", "-q", "-f", "-B", "unedited", before); + const head = commit({ "p.ts": "a();\nb(fork);\nc();\nforkZ();\n" }, "unedited"); + for (const args of [ + [main, head], + ["--no-upstream", main, head], + ]) { + const result = run(...args); + assert.equal(result.status, 1, `${args.join(" ")}: ${result.stdout}`); + assert.match( + result.stderr, + /- p\.ts: adds `forkZ\(\);` \(if it puts back an upstream line counted in "lost", lower that entry in this change's \.github\/fork-seams\.json\) \(this branch's merge-base has another "lost" entry for it than main: update the branch from main, whose list changed since\)/, + ); + } + }); +}); + +// Main lowered a "lost" entry since the branch's merge-base (another change put a line back): the +// branch's own entry, counted from its merge-base's, gets no room or no longer matches its head over +// upstream. Both refusals name the way out: update the branch from main. +test('a "lost" entry main changed since the merge-base names updating the branch', () => { + withRepo(({ git, commit, run, publish }) => { + const upstream = commit({ "p.ts": "a();\nb();\nc();\nd();\n" }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const list = (lost) => ({ + files: { "p.ts": { why: "w", added: ["b(fork);"] } }, + pending: {}, + lost, + }); + const before = commit( + { ...seams(list({ "p.ts": { lines: 3, gone: 3 } })), "p.ts": "a();\nb(fork);\n" }, + "fork changes b, drops c and d", + ); + const main = commit( + { ...seams(list({ "p.ts": { lines: 2, gone: 2 } })), "p.ts": "a();\nb(fork);\nc();\n" }, + "main puts c back", + ); + publish(upstream); + const update = + / \(this branch's merge-base has another "lost" entry for it than main: update the branch from main, whose list changed since\)/; + const pr = (name, lost) => { + git("checkout", "-q", "-f", "-B", name, before); + return commit({ ...seams(list(lost)), "p.ts": "a();\nb(fork);\nd();\n" }, name); + }; + // The branch puts d() back and lowers its merge-base's {3, 3} to main's {2, 2}: no room. + const same = run(main, pr("as-main", { "p.ts": { lines: 2, gone: 2 } })); + assert.equal(same.status, 1, same.stdout); + assert.match( + same.stderr, + new RegExp( + `- p\\.ts: adds \`d\\(\\);\` \\(if it puts back an upstream line counted in "lost", lower that entry in this change's \\.github/fork-seams\\.json\\)${update.source}`, + ), + ); + // It lowers main's {2, 2} by one: room for d(), but its head loses 2 over upstream (c() too). + const lowered = run(main, pr("below-main", { "p.ts": { lines: 1, gone: 1 } })); + assert.equal(lowered.status, 1, lowered.stdout); + assert.doesNotMatch(lowered.stderr, /adds `d\(\);`/); + assert.match( + lowered.stderr, + new RegExp( + `- p\\.ts: set in "lost" .*set it to \\{"lines": 2, "gone": 2\\} \\(a count above the lines lost is room to swap one for a fork line later\\)${update.source}`, + ), + ); + }); +}); + +// A line moved or re-indented back with "lost" left as main's is refused, naming the way out. +test('a line re-indented back with "lost" left as main\'s is refused, naming the entry to lower', () => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { + const UPSTREAM_R = "if (y) {\n d();\n}\n"; + const upstream = commit({ "r.ts": UPSTREAM_R }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const list = (lost) => ({ files: { "r.ts": { why: "w", added: [] } }, pending: {}, lost }); + const main = commit( + { ...seams(list({ "r.ts": { lines: 1, gone: 0 } })), "r.ts": "if (y) {\n d();\n}\n" }, + "main", + ); + publish(upstream); + assert.equal(fromUpstream(upstream, main).status, 0); + const pr = (name, own) => { + git("checkout", "-q", "-f", "-B", name, main); + return commit({ ...seams(own), "r.ts": UPSTREAM_R }, name); + }; + const kept = run(main, pr("kept", list({ "r.ts": { lines: 1, gone: 0 } }))); + assert.equal(kept.status, 1, kept.stdout); + assert.match( + kept.stderr, + /- r\.ts: 1 upstream line deleted, moved or re-indented by this change \(.*\) \(if it moves or re-indents back an upstream line counted in "lost", lower that entry in this change's \.github\/fork-seams\.json\)\./, + ); + const dropped = run(main, pr("dropped", list({}))); + assert.equal(dropped.status, 0, dropped.stderr); + }); +}); diff --git a/docs/fork/updates.md b/docs/fork/updates.md index 0c457108dd15a..95c26d90d52e1 100644 --- a/docs/fork/updates.md +++ b/docs/fork/updates.md @@ -130,10 +130,13 @@ watches; it changes nothing on the machine, and `0` turns it off. seam line: a moved copy of `}` looks the same as upstream's (move one with two pull requests). A seam file with a `lost` entry may get upstream's lines back (#968): a pull request that lowers that entry in its own list (neither count raised) may add one line per `gone` it lowers, and lose - one line of main's file per `lines` it lowers beyond those (a line moved or re-indented back; a - line put back in place lowers both). Its run counts the lowered entry from upstream exactly, so a - fork line in place of upstream's is refused there. With `lost` left as main's, a line put back is - refused as an unlisted one, and the refusal says to lower the entry. + one line of main's file per `lines` it lowers beyond those (a line moved or re-indented back). A + line put back where upstream has it is in order again, so it lowers both: an entry that lowers + `gone` more than `lines` puts a line back out of place (a fork change to upstream's order) and + allows nothing. Its run counts the lowered entry from upstream exactly, so a fork line in place of + upstream's is refused there. With `lost` left as main's, a line put back or moved back is refused + as an unlisted one, and the refusal says to lower the entry (or, when main's entry changed since + the branch's merge-base, to update the branch from main). A seam's `added` lists only the copies the fork adds: the run from upstream refuses a listed copy the fork does not add over upstream (a spare one would let a later change add that line unannounced), and right after a rebase that took the line word for word fork-follow drops it in @@ -204,7 +207,7 @@ seam entry together is refused. It takes two pull requests, each green: 1. **Announce** (list only, reviewed like any gate change: the `gate` rule of [`.github/fork-pr-queue.json`](../../.github/fork-pr-queue.json) lists `.github/fork-seams.json`, so a change to the list needs the merge-path and state lenses, and the code lens too when it - changes other files): add the lines under `announced` in the + also changes a file that no `gate` path matches): add the lines under `announced` in the file's entry, with the day: `{"since": "YYYY-MM-DD", "added": [lines]}`. Set `since` to the day this pull request merges (refresh it if the reviews took days): its run warns when the announcement it adds would be refused within 3 days. A file with no entry yet gets one with