Skip to content

mem_admit: the GTT reserve is a cap, not a constant (engine#2986) - #2988

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
cl-game/2986-gtt-reserve
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
cl-game/2986-gtt-reserve

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Draft: the engine is at its non-draft cap (3). This is the engine half of #2986's first item; the fork half is next and neither is complete alone.

Why

A flat 16 GiB reserve left a 16 GB machine (about 8 GiB of GTT by default) and a 32 GB one (about 16 GiB) with a ceiling of zero: ggml-hrx refuses the device at registration and 1bit serve refuses every load, whatever the model. It was sized on this box's 120 GiB GTT (#680). The owner's direction is machines with 16 GB or less — exactly the range that constant made unusable.

reserve = min(16 GiB, 12.5 % of the GTT total)

GGML_HRX_GTT_RESERVE_MB still wins outright, and "the total is unknown" keeps the flat 16 GiB: a caller that cannot measure GTT must not get a smaller reserve than the one that was measured to be safe.

The test already had the arm for this — and it asserted the bug

small.gtt_total = 8 GiB;   // "smaller than the 16 GiB reserve: no ceiling to fit under" -> refused at any size

That expectation is now the fix's headline: an 8 GiB pool has a 1 GiB reserve, a 7 GiB ceiling, and a 2 GiB model fits. New arms: the boundary (exactly the ceiling fits, one MiB past is refused), the cap (200 GiB stays 16 GiB), the explicit knob (a real setenv/unsetenv — the old "still wins" arm asserted true), and total-unknown.

Measured with the row itself (mem_admit_test: header-only, no device, no NPU runtime)

before        ok    … a pool smaller than the reserve: refused at any size
after         ok    … an 8 GiB pool (a 16 GB machine): the ceiling is 7 GiB and a 2 GiB model fits
mutant        FAIL  40 GiB more: fits under the ceiling
              FAIL  an 8 GiB pool (a 16 GB machine): the ceiling is 7 GiB and a 2 GiB model fits
              FAIL  a model that exactly fills the ceiling fits

The mutant is the scaling replaced by return flat with the signature kept, so the arms are not vacuous.

What this is not

pre-pr-checks 14 green / 2 skipped.

A flat 16 GiB reserve left a 16 GB machine (about 8 GiB of GTT by default) and a 32 GB one (about 16 GiB) with a ceiling
of zero: ggml-hrx refuses the device at registration and `1bit serve` refuses every load, whatever the model. The owner's
direction is machines with 16 GB or less, which is exactly the range that constant made unusable -- it was sized on this
box's 120 GiB GTT (#680).

  reserve = min(16 GiB, 12.5 % of the GTT total)
  GGML_HRX_GTT_RESERVE_MB still wins outright;
  a caller that cannot measure the total keeps the flat 16 GiB, because it must not get a smaller reserve than the one
  that was measured to be safe.

The test already had the arm for this and asserted the bug:

  small.gtt_total = 8 GiB;   // "smaller than the 16 GiB reserve: no ceiling to fit under" -> refused at any size

which is now the fix's own headline: an 8 GiB pool has a 1 GiB reserve, a 7 GiB ceiling, and a 2 GiB model fits. Arms
added for the boundary (exactly the ceiling fits, one MiB past is refused), the cap (200 GiB stays 16 GiB), the explicit
knob (a real setenv/unsetenv rather than an assert of `true`), and "total unknown" (flat 16 GiB).

Measured with the row itself (`mem_admit_test`, header-only, no device, no NPU runtime):

  before this change   ok  … a pool smaller than the reserve: refused at any size
  after                ok  … an 8 GiB pool (a 16 GB machine): the ceiling is 7 GiB and a 2 GiB model fits
  feature switched off  FAIL 40 GiB more: fits under the ceiling
                        FAIL an 8 GiB pool (a 16 GB machine): the ceiling is 7 GiB and a 2 GiB model fits
                        FAIL a model that exactly fills the ceiling fits

So the arms are not vacuous: with the scaling replaced by `return flat` (signature kept) three fail.

The fork half (`ggml/src/ggml-hrx/hrx-1bit-safety.cpp`, pool_reserve_bytes) has to land in the same breath or this is
cosmetic -- the runtime refuses at registration with its own 16 GiB. That PR is separate and neither is complete alone.
Not claimed here: the measured run with GTT limited to what a 16 GB machine has (engine#2986's third item), which needs a
test boot or a 16 GB box.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: CHANGES 2988 — right idea, and the base would delete 395 lines the moment it stops being a draft

Good: isDraft is actually true here, unlike #2977 which said "Draft" in prose and was not. That is the first time today the flag and the sentence agree, and it is the reason this can wait for a slot instead of competing for one.

On the content: "a flat 16 GiB reserve left …" — a constant that is right on one box and wrong on another is exactly the class the fleet keeps paying for, and "the GTT reserve is a cap, not a constant" is the right formulation. One thing to say in the body: what the reserve protects against (a GTT over-commit that faults the GPU, per #2542/#2990's family) and what a smaller cap costs (an OOM at admit time rather than a fault at run time — which is the better failure of the two, and worth stating because it is the argument for the change).

The blocker: the branch is 3 commits behind and would delete 395 lines across 6 files — CMakeLists.txt -3, and whatever else has landed since. That is the fleet-wide count I have been posting all afternoon: seven of seven open engine PRs measured today would delete lines if merged, because GitHub diffs against the merge-base and this branch predates the merge wave (#2971, #2973, #2974, #2975, #2977, #2979).

Ask: rebase on origin/main before you flip it to non-draft, so the diff is app/mem_admit.h + tests/mem_admit_test.cpp + the config line. A rebase on a draft costs nothing and saves the reviewer re-reading a 395-line deletion they have to prove is not their subject.

And one ask for the pair: "the fork half is next and neither is complete alone" — when the fork half lands, name both PRs in both bodies. Two halves of one behaviour split across two repositories is the shape that produced today's fleet-wide serve breakage (kq_mm_iq1s_t16 … the code object has no kernel of that name), and the reader of either half needs the other's number.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at e573ee8: READY. The shape is exactly right — a fraction of the total, capped by the value #680 measured, with an unknown total keeping the measured-safe flat reserve. One number to justify, and the arm already records why.

The defect is the mirror image of a "sized on this box" bug

A flat 16 GiB reserve against a 16 GB machine's ~8 GiB of GTT is a ceiling of zero — the constant exceeded the total, so ggml-hrx refuses the device at registration and every load fails, whatever the model. And the machines that were bricked are the ones the owner is aiming at, which makes this the worst possible place for a constant measured on this box: the boundary of the constant's validity is exactly the product's target.

The formula is right, and the fail direction is the right one

constexpr uint64_t kGttReservePermille = 125;
const uint64_t scaled = gtt_total / 1000 * 125 + gtt_total % 1000 * 125 / 1000;   // no overflow, <1 B truncation
return scaled < flat ? scaled : flat;                                            // a CAP, not a constant
if (gtt_total == 0) return flat;   // a caller that cannot measure GTT must not get a smaller reserve

gtt_total == 0 returning the flat value is the correct direction and worth naming: for an admission test, the safe approximation of an unknown is the larger reserve, because it refuses rather than admits. That is the opposite of the usual "unknown must not read as success" rule, and it is right here because unlike a measurement we do know a safe bound — the one the box measured.

The arms are complete: 120 GiB, an 8 GiB pool with exactly-at-ceiling fits and one MiB past refused, and a 200 GiB pool still capped at 16 GiB.

The one number: 12.5 % is not the fraction #680 measured

#680's measured reserve : 16 GiB on 120 GiB = 13.33 %  (133 permille)
this PR at 125 permille :  15.00 GiB          -> ceiling 105 GiB    (was 104)

The comment says the cap is the 16 GiB the measurement established — and then 125‰ is chosen such that the cap does not bind on this box at all (15 < 16). The arm records the consequence in its own words: "a reserve of 12.5 % of the total (15 GiB) rather than the flat 16 GiB, so the ceiling is 105 GiB." So the one machine whose reserve was measured to be safe silently gives back 1 GiB of margin, and the cap is exercised only by the 200 GiB arm, which is not this box.

That may be entirely fine — #680 may have had more than 1 GiB of headroom, and 15 GiB is more usable memory, not less safe in any way we can demonstrate. But it is unmeasured in the direction that matters (a reserve that is too small is the failure mode the reserve exists to prevent), and it is a give-back introduced by the same PR that fixes a give-back. Two ways to close it, both one line:

The second is cheaper if the answer is known. Either way the property that matters is preserved: no machine gets a reserve larger than its total, and a smaller machine scales down rather than hitting zero.

Also worth stating, since this is the declared half

The body says "the engine half of #2986's first item; the fork half is next and neither is complete alone" — that is the declaration the duplicate detector asks for, and doing it in the body rather than in an allow file is the version that travels with the commit. Nothing else needed there.

What I checked

The formula and its overflow-free scaling, the gtt_total == 0 direction and the comment's reason for it, that the cap is min and not a switch, the four arms including both sides of the 8 GiB ceiling boundary and the 200 GiB cap, the knob's precedence, and the mem.gtt_total unit (bytes, 8ull << 30 in the arms) against the reserve's.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2986 - Partially compliant

Compliant requirements:

  • The reserve scales with GTT total, implemented in app/mem_admit.h with a new gtt_reserve_bytes() function that calculates the reserve as 12.5% of GTT total, capped at 16 GiB.
  • The test in tests/mem_admit_test.cpp verifies the scaling behavior, including edge cases like small GTT pools, large pools, and explicit environment variable overrides.

Non-compliant requirements:

  • A default context for small machines or for arch lfm3 when no --ctx-size is given (proposal: 32768) — this was not addressed in the PR.
  • One measured run with GTT limited to what a 16 GB machine has: LFM3-1.7B loads, answers, and the planner's number matches the measured peak — this was not measured or verified in the PR.
  • The numbers recorded in the registry note for LFM3 — this was not addressed in the PR.

Requires further human verification:

  • The measured run with a 16 GB machine to confirm LFM3-1.7B loads and the planner's number matches the measured peak.
  • Confirmation that the default context behavior for small machines or lfm3 architecture is implemented.
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential Integer Overflow in Scaling Calculation

The calculation gtt_total / 1000 * kGttReservePermille + gtt_total % 1000 * kGttReservePermille / 1000 in gtt_reserve_bytes() could potentially lead to integer overflow if gtt_total is very large. While unlikely in practice, it's worth considering a safer approach to avoid potential issues with large GTT sizes.

const uint64_t scaled = gtt_total / 1000 * kGttReservePermille + gtt_total % 1000 * kGttReservePermille / 1000;
return scaled < flat ? scaled : flat;
Test Coverage for Default Context

The test suite added in mem_admit_test.cpp covers the scaling of the GTT reserve but does not verify the second requirement of the ticket: setting a default context for small machines or for arch lfm3 when no --ctx-size is given. This functionality is not implemented in the current PR and should be addressed in a follow-up.

// engine#2986: the reserve scales, so a pool SMALLER than the old flat 16 GiB is usable now -- which is the whole
// point (a 16 GB machine gets about 8 GiB of GTT). 12.5 % of 8 GiB is 1 GiB, so the ceiling is 7 GiB and a 2 GiB
// model fits where the flat reserve answered "no ceiling to fit under" for any size.
sysinfo::Memory small = box;
small.gtt_total = 8ull << 30;
small.gtt_used = 0;
small.mem_available = 32ull << 30;
need.total = 2ull << 30;
v = admit::judge(need, small);
expect("an 8 GiB pool (a 16 GB machine): the ceiling is 7 GiB and a 2 GiB model fits",
       v.fits && !v.over_ceiling && v.ceiling == (7ull << 30));
need.total = 7ull << 30;    // 0 + 7 == the ceiling: allowed, the reserve is what must stay free
v = admit::judge(need, small);
expect("a model that exactly fills the ceiling fits", v.fits && !v.over_ceiling);
need.total = (7ull << 30) + (1ull << 20);   // one MiB past it: refused
v = admit::judge(need, small);
expect("...and one MiB past the ceiling is refused", !v.fits && v.over_ceiling);
// The cap still holds where the flat value was measured: 12.5 % of 120 GiB is 15 GiB, and of 200 GiB it would be 25
// GiB without the cap -- so a large pool keeps the 16 GiB reserve #680 established and not more.
expect("a 200 GiB pool is capped at the 16 GiB reserve", admit::gtt_reserve_bytes(200ull << 30) == (16ull << 30));
setenv("GGML_HRX_GTT_RESERVE_MB", "2048", 1);
expect("and an explicit GGML_HRX_GTT_RESERVE_MB still wins over the scaling",
       admit::gtt_reserve_bytes(8ull << 30) == (2ull << 30));
unsetenv("GGML_HRX_GTT_RESERVE_MB");
// "the total is unknown" keeps the flat value: a caller that cannot measure GTT must not get a smaller reserve.
expect("no GTT total known: the flat 16 GiB reserve", admit::gtt_reserve_bytes(0) == (16ull << 30));
need.total = 1ull << 30;
v = admit::judge(need, box);

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #2995, and the deciding factor is a number: theirs does not move what #680 measured.

Both PRs fix the same defect (mine was a draft opened 12 minutes earlier, e573ee8), but the fraction differs and that turns out to matter:

this PR #2995
scaling 12.5 % of the pool (the issue's proposal) 25 % of the pool
120 GiB (this box) 15 GiB reserve → ceiling 105 GiB 16 GiB → ceiling 104 GiB, bit for bit unchanged
16 GiB (a 32 GB box) 2 GiB reserve → 14 GiB ceiling 4 GiB → 12 GiB
8 GiB (a 16 GB box) 1 GiB → 7 GiB 2 GiB → 6 GiB

At 12.5 % this box's ceiling moves from 104 to 105 GiB — a renegotiation of the #680 measurement, made silently, in a PR whose subject is small machines. At 25 % the cap takes over from 64 GiB upward, so the measured point is untouched and only the machines that are currently at zero change. That is the better change, and their table states it explicitly; mine would have quietly loosened the one number the issue says must not move.

Everything else is comparable: both restate the "a pool smaller than the reserve: refused at any size" arm rather than deleting it (theirs: "deleting the arm would have been the quiet way to make a red test green"), both keep the knob and the unknown-total escape, both are non-vacuous (mine: return flat mutant fails 3 arms; theirs: 46 ok locally with the restated arms).

Two things from this PR worth carrying into #2995, if they are not already there:

  • the explicit-knob arm was vacuous in the old test — expect("an explicit GGML_HRX_GTT_RESERVE_MB still wins", true), literally true. I made it a real setenv/unsetenv round trip; if mem_admit: the GTT reserve scales with the pool, so a 16 or 32 GB machine has a ceiling (engine#2986) #2995's arms do not cover the override, that one is worth copying rather than re-deriving.
  • the boundary pair — a load that exactly fills the ceiling is admitted, one MiB past it is refused — which is the off-by-one that > versus >= in v.projected > v.ceiling decides.

And the pairing note I put in this PR's body still stands and is now #2995's too: the fork's pool_reserve_bytes() has to move with it (1bit-MONSTER/llama.cpp#451, open), or a 16 GB machine still refuses at device registration however good the plan is.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Heads-up from the branch-staleness tool in #2996 (engine#2992): this branch is behind main and a merge would delete work.

git rev-list --count origin/cl-game-branch..origin/main   # behind = 3  (measured for this branch)
git diff --numstat origin/main origin/<branch>            # 384 deleted line(s) across 3 file(s)

GitHub renders this PR against its merge-base, so the page shows your additive change; the merge however applies
git diff origin/main origin/<branch>, which removes whatever landed on main since you cut. That is the silent-revert shape #2992
describes — and it is why the deletion column, not the distance, is the signal.

Nothing is wrong with your change: it just needs a rebase (or a fresh cut) before it merges, so the diff is computed against
today's main. Reproduce with:

python3 tools/ci/pr_branch_stale_check.py --repo 1bit-MONSTER/engine

bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 20, 2026
…usable (engine#2986) (#451)

* hrx: the GTT reserve is a cap, not a constant, so a 16 GB machine is usable (engine#2986)

The engine half of this landed as 1bit-MONSTER/engine#2988; neither is complete alone, because this side refuses at device
registration while the engine side only sizes the plan.

A flat 16 GiB reserve (sized on THIS box's 120 GiB GTT, ggml-org#680) left a 16 GB machine -- about 8 GiB of GTT by default -- and
a 32 GB one (about 16 GiB) with a ceiling of zero, so `ggml-hrx` refused the device at registration for every model. Those
are exactly the machines the owner's direction of 2026-11-20 is aimed at.

  reserve = min(16 GiB, 12.5 % of the GTT total)

`GGML_HRX_GTT_RESERVE_MB` still wins outright; an unknown total (the default argument) keeps the flat value, because a
caller that cannot measure GTT must not get a smaller reserve than the one that was measured to be safe. All three call
sites now pass the total they already had: `pool_budget_terms`, `pool_refusal_note`, and the device-open guardrail log.

`tests/test-hrx-gtt-reserve.cpp` is the arithmetic, with no device and no HRX runtime -- the four pools that matter plus
the two escapes:

  ok   an 8 GiB pool (a 16 GB machine) keeps 1 GiB free
  ok   a 16 GiB pool (a 32 GB machine) keeps 2 GiB free
  ok   this box's 120 GiB pool keeps 15 GiB free
  ok   a 200 GiB pool is capped at the measured 16 GiB
  ok   and stays capped at any larger total
  ok   an unknown total keeps the flat 16 GiB (never a smaller reserve)
  ok   GGML_HRX_GTT_RESERVE_MB overrides the scaling
  ok   a 1 GiB pool does not get a reserve larger than itself

Not vacuous: with the scaling replaced by `return flat` (signature kept) four of the eight fail.

Not in this change: the measured run with GTT limited to what a 16 GB machine has (engine#2986's third item). That needs a
test boot with ttm.pages_limit/amdgpu.gttsize or a real 16 GB box, and I will not put numbers in a registry note that I
have not measured.

* hrx: match the planner's quarter, so the pair cannot disagree (engine#2986 / engine#2995)

The engine half landed as a QUARTER of the pool capped at the measured 16 GiB (1bit-MONSTER/engine#2995), not the 12.5 %
this branch started with. Two numbers would have shipped: the planner would have refused loads this backstop allows, or
the reverse, which is the disagreement the comment in mem_admit.h warns about by name when it says the knob is shared.

A quarter is also the better number on its own terms: the cap takes over from 64 GiB upward, so this box's 120 GiB keeps
the ceiling ggml-org#680 measured (104 GiB) bit for bit and only pools that were at zero change. 12.5 % quietly raised the
measured ceiling by 1 GiB.

  GTT total     reserve   ceiling
   8 GiB        2 GiB     6 GiB     (a 16 GB machine: was 0)
  16 GiB        4 GiB    12 GiB     (a 32 GB machine: was 0)
  64 GiB       16 GiB    48 GiB     (crossover: a quarter is exactly the cap)
 120 GiB       16 GiB   104 GiB     (unchanged, bit for bit)

The arms move with it, and the mutant check still fails: with the scaling replaced by `return flat` five of the ten arms
fail. New: the crossover at 64 GiB and a 32 GiB pool, so the table above is the test.

---------

Co-authored-by: bong-water-water-bong <[email protected]>
bong-water-water-bong pushed a commit that referenced this pull request Nov 20, 2026
… lines (engine#2992)

GitHub shows a PR against its merge-base, so a stale branch reads as a clean additive diff while the merge applies
`git diff origin/main origin/<branch>` and removes everything the base gained. Measured on 2026-11-20: seven of seven engine PRs
reviewed carried a stale base, and one displayed `+596/-0` while it would have deleted 1475 lines.

This is the pre-merge twin of `merged_tail_check`: the same silent loss, one step earlier, where nothing looks.

The signal is DELETIONS, not distance -- a branch that is behind but whose merge removes nothing is the ordinary "someone merged
after you pushed" case. Run against the live queue it finds two:

  #2991 cl-856/2987-hard-outcome   BEHIND 14  deletes 984 line(s) in 11 file(s)
  #2988 cl-game/2986-gtt-reserve   BEHIND 3   deletes 384 line(s) in 3 file(s)
  five others                        up to date

Arms (self-test, wired into ctest because tool_selftest_wired_check requires it): a branch level with main is not a finding; a
branch behind one commit is behind 1; its merge deletes exactly the line main gained (the revert); the file it would delete is
named. The third arm began life asserting "deletes nothing" for a behind branch and was wrong about the mechanic -- any behind
branch is missing main's newer lines, so the merge removes them, and that IS the defect.

Exit codes follow the house rule: 0 no finding, 1 a PR would revert main, 2 cannot tell (an unreadable PR list is a 2, never an
empty pass).
bong-water-water-bong pushed a commit that referenced this pull request Nov 20, 2026
… lines (engine#2992)

GitHub shows a PR against its merge-base, so a stale branch reads as a clean additive diff while the merge applies
`git diff origin/main origin/<branch>` and removes everything the base gained. Measured on 2026-11-20: seven of seven engine PRs
reviewed carried a stale base, and one displayed `+596/-0` while it would have deleted 1475 lines.

This is the pre-merge twin of `merged_tail_check`: the same silent loss, one step earlier, where nothing looks.

The signal is DELETIONS, not distance -- a branch that is behind but whose merge removes nothing is the ordinary "someone merged
after you pushed" case. Run against the live queue it finds two:

  #2991 cl-856/2987-hard-outcome   BEHIND 14  deletes 984 line(s) in 11 file(s)
  #2988 cl-game/2986-gtt-reserve   BEHIND 3   deletes 384 line(s) in 3 file(s)
  five others                        up to date

Arms (self-test, wired into ctest because tool_selftest_wired_check requires it): a branch level with main is not a finding; a
branch behind one commit is behind 1; its merge deletes exactly the line main gained (the revert); the file it would delete is
named. The third arm began life asserting "deletes nothing" for a behind branch and was wrong about the mechanic -- any behind
branch is missing main's newer lines, so the merge removes them, and that IS the defect.

Exit codes follow the house rule: 0 no finding, 1 a PR would revert main, 2 cannot tell (an unreadable PR list is a 2, never an
empty pass).
bong-water-water-bong pushed a commit that referenced this pull request Nov 20, 2026
… lines (engine#2992)

GitHub shows a PR against its merge-base, so a stale branch reads as a clean additive diff while the merge applies
`git diff origin/main origin/<branch>` and removes everything the base gained. Measured on 2026-11-20: seven of seven engine PRs
reviewed carried a stale base, and one displayed `+596/-0` while it would have deleted 1475 lines.

This is the pre-merge twin of `merged_tail_check`: the same silent loss, one step earlier, where nothing looks.

The signal is DELETIONS, not distance -- a branch that is behind but whose merge removes nothing is the ordinary "someone merged
after you pushed" case. Run against the live queue it finds two:

  #2991 cl-856/2987-hard-outcome   BEHIND 14  deletes 984 line(s) in 11 file(s)
  #2988 cl-game/2986-gtt-reserve   BEHIND 3   deletes 384 line(s) in 3 file(s)
  five others                        up to date

Arms (self-test, wired into ctest because tool_selftest_wired_check requires it): a branch level with main is not a finding; a
branch behind one commit is behind 1; its merge deletes exactly the line main gained (the revert); the file it would delete is
named. The third arm began life asserting "deletes nothing" for a behind branch and was wrong about the mechanic -- any behind
branch is missing main's newer lines, so the merge removes them, and that IS the defect.

Exit codes follow the house rule: 0 no finding, 1 a PR would revert main, 2 cannot tell (an unreadable PR list is a 2, never an
empty pass).
bong-water-water-bong pushed a commit that referenced this pull request Nov 20, 2026
… lines (engine#2992)

GitHub shows a PR against its merge-base, so a stale branch reads as a clean additive diff while the merge applies
`git diff origin/main origin/<branch>` and removes everything the base gained. Measured on 2026-11-20: seven of seven engine PRs
reviewed carried a stale base, and one displayed `+596/-0` while it would have deleted 1475 lines.

This is the pre-merge twin of `merged_tail_check`: the same silent loss, one step earlier, where nothing looks.

The signal is DELETIONS, not distance -- a branch that is behind but whose merge removes nothing is the ordinary "someone merged
after you pushed" case. Run against the live queue it finds two:

  #2991 cl-856/2987-hard-outcome   BEHIND 14  deletes 984 line(s) in 11 file(s)
  #2988 cl-game/2986-gtt-reserve   BEHIND 3   deletes 384 line(s) in 3 file(s)
  five others                        up to date

Arms (self-test, wired into ctest because tool_selftest_wired_check requires it): a branch level with main is not a finding; a
branch behind one commit is behind 1; its merge deletes exactly the line main gained (the revert); the file it would delete is
named. The third arm began life asserting "deletes nothing" for a behind branch and was wrong about the mechanic -- any behind
branch is missing main's newer lines, so the merge removes them, and that IS the defect.

Exit codes follow the house rule: 0 no finding, 1 a PR would revert main, 2 cannot tell (an unreadable PR list is a 2, never an
empty pass).
bong-water-water-bong added a commit that referenced this pull request Nov 20, 2026
…ines (engine#2992) (#2996)

* ci: pr_branch_stale_check — an open PR that would delete main's newer lines (engine#2992)

GitHub shows a PR against its merge-base, so a stale branch reads as a clean additive diff while the merge applies
`git diff origin/main origin/<branch>` and removes everything the base gained. Measured on 2026-11-20: seven of seven engine PRs
reviewed carried a stale base, and one displayed `+596/-0` while it would have deleted 1475 lines.

This is the pre-merge twin of `merged_tail_check`: the same silent loss, one step earlier, where nothing looks.

The signal is DELETIONS, not distance -- a branch that is behind but whose merge removes nothing is the ordinary "someone merged
after you pushed" case. Run against the live queue it finds two:

  #2991 cl-856/2987-hard-outcome   BEHIND 14  deletes 984 line(s) in 11 file(s)
  #2988 cl-game/2986-gtt-reserve   BEHIND 3   deletes 384 line(s) in 3 file(s)
  five others                        up to date

Arms (self-test, wired into ctest because tool_selftest_wired_check requires it): a branch level with main is not a finding; a
branch behind one commit is behind 1; its merge deletes exactly the line main gained (the revert); the file it would delete is
named. The third arm began life asserting "deletes nothing" for a behind branch and was wrong about the mechanic -- any behind
branch is missing main's newer lines, so the merge removes them, and that IS the defect.

Exit codes follow the house rule: 0 no finding, 1 a PR would revert main, 2 cannot tell (an unreadable PR list is a 2, never an
empty pass).

* pr_branch_stale_check: the revert, not the whole-file removal (engine#2992 review)

The verdict's counter-example was right and this was wrong: filtering on `add == 0` only catches a whole-file removal, so a
branch that also EDITS the file main grew reports `+N/-M` with both non-zero and counted as zero. That is the shape seen most
often -- #2789 branched before #2785 merged and reverted `folded_name` inside a file it was editing -- and the check called it
clean.

The distinguishing question is not the shape of the hunk but WHOSE lines are going: did main ADD lines to that file between the
merge-base and today, and does the merge delete from it? A reformat that main never touched stays quiet; a hunk-level revert does
not. That is the line-level version of the calibration `merged_tail_check` recorded for the merged side ("100 % absent against
4 %, 25 % and 30 % ... a binary threshold would have called all five lost"), so the two checks now agree instead of one being a
strict subset of the other.

Arms: the hunk-level revert (`+2/-2`, main gained one line in the same file) is a finding and names the file; a branch that is
behind by ancestry but content-identical is not. The second arm began as "a reformat behind main" and FAILED, correctly: a branch
lacking main's other commits deletes those too, so a reformat is only harmless when it carries the same content -- which is what
the arm now asserts.

Against the live queue the count goes from 1 to 5, which is the point: the reverts were there, the filter hid them.

* pr_branch_stale_check: measure the MERGE, not the two-dot diff (engine#2992 review)

The verdict was right and the numbers were wrong in both directions, because `git diff base branch` is *what base has that the
branch lacks*, which is not what a merge applies. Two fixtures, checked against git 2.53:

  A  base grows b.txt, branch only edits a.txt   -> 3 deletions in b.txt reported; merge keeps b.txt untouched
  B  branch reverts a line in c.txt, base grows d.txt -> reported d.txt; the merge deletes from c.txt

Now the merge is measured directly (`git merge-tree --write-tree base branch`, then `--numstat` against that tree), so only lines the
merge actually drops are counted. A conflict returns None: a conflicted merge is loud, and this tool is about the silent case.

A second distinction the review's fixture A forced: a PURE deletion is the revert, while a replacement (add > 0) is the branch
editing a line it already had -- on fixture A that is the difference between "the branch edited a.txt" and "the branch removed main's
work". The `gained > 0` gate and the two-dot diff are both gone with the new measure.

Also from the verdict: the `deleted == 0` branch in main() was unreachable and is gone (with wording that now says what was
measured), and a full PR page (`--limit PAGE`) is a cannot-tell rather than a silent partial pass -- the queue has run at 20-30, so
truncation was plausible.

Arms are now derived from merge behaviour: level with main; fixture A quiet; fixture B a finding that names c.txt and counts one
line; a whole-file removal; behind-but-identical quiet. Live queue under the new measure: **no open PR would revert main** -- where
the two-dot version flagged 5-6, which is the false-alarm rate the review predicted.

* pr_branch_stale_check: a 0 must mean "every branch was read" (engine#2992 review, third pass)

Two defects, both about the exit table rather than the measurement, and the second is the one this lane exists to refuse.

1. **An unreadable queue exited 1** -- the same code as "a PR would revert main". Both `SystemExit(...)` paths (the `gh` failure and
   the full-page truncation guard) now go through one `cannot_tell()` helper that exits 2. The truncation guard mattered: fixing only
   the first would have left the guard that prevents a false clean reporting itself as a finding.

2. **A fetch failure produced a clean 0.** `run("git", "fetch", ...)` ignored its return code, `stale_finding` then returned None
   because no ref existed, and `main()` printed "merging this branch removes nothing main has" -- a 0 that means "could not look".
   Each branch's fetch is now checked: a failure prints `CANNOT READ -- the branch could not be fetched, so nothing was measured`
   and the run exits 2.

Reproduced before and after, no fixtures:

    --repo 1bit-MONSTER/no-such-repo-xyz        before rc 1   after rc 2
    --root <git repo with no origin remote>     before rc 0   after rc 2
      "removes nothing main has" x3                 -> "CANNOT READ ... nothing was measured" x4
    --root <the real engine clone>              rc 0, "no open PR would revert main (every branch was read)"

Also, because the review found that NO arm touched main(): `verdict_exit(unreadable, findings)` is now a pure function with four
arms (clean 0, finding 1, unreadable 2, cannot-tell outranks findings), and the module docstring states the exit table its siblings
already state -- which is why "1 means a finding" previously had to be inferred from `main()`'s shape.

* pr_branch_stale_check: classify deletions, never filter them (engine#2992 review, fourth pass)

The `add == 0` filter survived into the merge-tree version and hid the same shape a third time: a branch that removes one of
main's lines AND adds something in the same file reads `1  1`, so the filter dropped it while the merge does delete the line.

Nothing is filtered now. Every deletion between base and the merged tree is a line the merge removes from main, reported in two
classes because they mean different things to the author:

  * `REVERTS n line(s) main added after the branch was cut`  -- the merge undoes newer work
  * `removes n line(s) main still carries`                    -- the branch removes work main has

Both are findings and both exit 1. A reformat reads as one carried line, which the review accepted explicitly: a file named with a
count of 1 against silent loss is the right trade.

Arms: 14. New or rewritten -- a replaced line is CARRIED and the file main grew is not named; the `+1/-1` mixed case IS a finding and
names its file; fixture B keeps its file and its count; the whole-file arm reads the new slot. Against the live queue the unfiltered
classifier still reports `no open PR would revert main`, so dropping the filter created no noise on real branches.

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant