fix(763): round 6 — measure the partial-mark fallback under the REAL ordering

A fourth cold review (Opus, isolated worktree, tests/double/docs focus)
reported no correctness bugs in shipped behaviour but two coverage defects on
exactly the two things this change advertises. Both are closed.

The partial-mark fallback's safety is a claim ABOUT THE ORDERING — page 1 holds
the newest rows, so a walk that fails later still saw the true maximum. The
fixture pinning it served ASCENDING ids, i.e. the arrangement the design calls
unsafe, and passed anyway because the raced row's id sat above even the partial
mark. It could not distinguish safe from unsafe.

The stub now HONOURS the sort parameter: order-faithful modes serve DESC by
default and ASC when the request asks. The new fixture holds a PRE-EXISTING
base-mismatched verdict at id 7055 among 60 rows. Under DESC the salvaged mark
is 7059 and that row is below it — the exemption correctly stands. Under ASC
the mark would be 7049 and that untouched row tests as NEWER, a sticky repair
on a head nothing raced. So re-adding `sort=highestindex` now reddens by
BEHAVIOUR, not only by the structural assertion added in round 5. Measured:
re-adding it reds both tests.

Most modes stay ordering-blind on purpose and now say so: they test walk
COMPLETENESS, which is order-independent, and insertion order is what lets a
fixture place a row beyond page 1.

Also fixed:
  - `null` is accepted as an empty page. An array-only gate is the exact shape
    of #751 — `count_retargets` had one, the timeline really did return `null`
    past the end, and the fence withheld EVERY exemption from the day it
    shipped. The same narrowing here is worse, because this walk's failure is
    the STICKY sentinel: every exempt PR would need a hand-posted verdict, per
    head. Tolerating `null` cannot misread `[]`. Proved by fixture.
  - the fail-closed comment said "past the 1000-row page cap"; the bound is 950,
    as the walk's own comment and both docs already said.
  - the docs claimed "only a read returning no rows at all abandons the mark".
    False: a VALIDATED empty history yields a mark of 0 and is not abandoned —
    that is the normal first run. What abandons it is a read that both FAILED
    and returned nothing. Corrected in ci-cd.md and the record `rule:`.
  - a comment pointed at the page-2 probe "a few lines further down"; it was
    deleted, so the deixis pointed at nothing.
  - the stub claimed its logical-read counter "is only reached on a SUCCESSFUL
    page-1 serve" — measured false; it counts page-1 requests, retries included.
  - five `(round N)` markers removed. A round number is session chronology and
    does not parse for a reader who never saw it (`docs.no-session-narrative`);
    an issue number does. The four that remain predate this change.

Verification: `scripts/tests` 1096 passed, 2 skipped. Thirteen executed
mutations across rounds 2-6. The reviewer independently re-ran the earlier
matrix and confirmed it, with one correction carried here: two of those
mutations redden MORE than their named test, so "each reddening exactly its
named test" was wrong — they redden at least it.

refs #763

Decisions-Edit: yes
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-28 17:34:08 +02:00
co-authored by Claude Opus 5
parent 53ee44cbc1
commit 89db6022d9
5 changed files with 131 additions and 16 deletions
+21 -8
View File
@@ -793,7 +793,7 @@ jobs:
# separate "the body is null" from "the predicate is false".
kind=$(printf '%s' "$raw" | jq -r 'type' 2>/dev/null) || kind=""
fi
if [ "$kind" = array ]; then break; fi
if [ "$kind" = array ] || [ "$kind" = null ]; then break; fi
# A SECOND IMMEDIATE REQUEST BARELY COUNTS AS A RETRY. One second does not survive a
# Gitea restart either, and saying so is the point: this absorbs a momentary blip, not
# an outage. An outage still lands on `ph_ok=no`, which is the safe direction.
@@ -801,7 +801,19 @@ jobs:
# waiting on, twice per walk.
if [ "$try" -eq 1 ]; then sleep 1; fi
done
if [ "$kind" != array ]; then ph_rows=$acc; return 0; fi
# `null` COUNTS AS AN EMPTY PAGE, though this endpoint returns `[]` today. An
# array-only gate is the exact shape of #751: `count_retargets` had one, the timeline
# really did return `null` past the end, so the walk never reached a validated empty
# page and the fence withheld EVERY exemption. Re-adopting that narrowing on a second
# endpoint would be worse, because this walk's failure is the STICKY sentinel — every
# exempt PR would need a human verdict, per head. Tolerating `null` costs nothing and
# cannot misread `[]`; asserting the endpoint will never change its empty shape is a
# bet with no upside.
case "$kind" in
null) ph_ok=yes; ph_rows=$acc; return 0 ;;
array) ;;
*) ph_rows=$acc; return 0 ;;
esac
n=$(printf '%s' "$raw" | jq -r 'length' 2>/dev/null) || n=""
case "$n" in ''|*[!0-9]*) ph_rows=$acc; return 0 ;; esac
if [ "$n" -eq 0 ]; then ph_ok=yes; ph_rows=$acc; return 0; fi
@@ -1105,7 +1117,7 @@ jobs:
# raced — but it is a stall with no diagnosis attached, and it is the same read the
# post-write check depends on being complete.
page_statuses
# A PARTIAL LIST STILL YIELDS A USABLE MARK, and refusing one is a FAIL-OPEN (round 2).
# A PARTIAL LIST STILL YIELDS A USABLE MARK, and refusing one is a FAIL-OPEN (ersatztv#763).
# This depends on the DESC default above: the newest row — the one carrying the maximum id,
# ids being monotonic with `created_at` (measured) — is on page 1, so a walk that fails
# later still saw it. Under an ASCENDING sort this reasoning inverts and the fallback
@@ -1317,7 +1329,7 @@ jobs:
raced_why=human
if [ "$ph_ok" != yes ]; then
# FAIL CLOSED, AND THAT IS A BEHAVIOUR CHANGE (ersatztv#763). This used to warn and leave
# the exemption green, while #751's page-2 probe a few lines further down repaired on
# the exemption green, while #751's page-2 probe (since deleted) repaired on
# exactly the same uncertainty — the two halves of one check disagreed about which way
# "I could not read it" resolves. On the one path whose failure direction is toward
# SUCCESS it has to be `pending`: an unreadable history is not evidence that nothing
@@ -1326,7 +1338,8 @@ jobs:
# This is affordable ONLY because the walk above is real paging. Under the old probe the
# common trigger was an ordinary head carrying more than 50 rows, so failing closed then
# would have cost the exemption on every busy PR. Now "could not establish" means a
# genuine API failure that survived a retry, or a history past the 1000-row page cap.
# genuine API failure that survived a retry, or a history past the page cap (950 rows,
# since the twentieth request must be the empty terminator).
echo "::warning::Could not establish a complete status history for ${SHA:0:7} after posting, so a human verdict landing during the write window could not be ruled out. Repairing ${CONTEXT} to pending rather than leaving an exemption green on an unverified head."
raced_why="the status history could not be read completely"
raced=1
@@ -1378,7 +1391,7 @@ jobs:
# on a row whose `creator` is any non-object, jq exits 5, and under `set -e` the
# assignment takes the step down — AFTER the exemption `success` is already posted and
# with the repair never attempted, leaving a forged green on a head that may carry a
# rejection. Reproduced (round 2). `(.creator | type) == "object"` short-circuits jq's
# rejection. Reproduced (ersatztv#763). `(.creator | type) == "object"` short-circuits jq's
# `and` before the index, so a malformed row is dropped from the count instead of
# killing the job, and a well-formed verdict beside it is still counted.
raced=$(printf '%s' "$ph_rows" | jq -r --arg c "$CONTEXT" --argjson since "$max_id_before" --arg rd "$REPAIR_DESC" \
@@ -1390,7 +1403,7 @@ jobs:
and (((.description // "") | startswith("Review-verdict:"))))
or ((.creator == null) and ((.description // "") == $rd))
)] | length') || raced=""
# NO "CURRENCY WITNESS" HERE, AND THAT IS A DELIBERATE WITHDRAWAL (round 2).
# NO "CURRENCY WITNESS" HERE, AND THAT IS A DELIBERATE WITHDRAWAL (ersatztv#763).
# A draft of this change also asserted that the post-write read must show at least one
# row above the mark, on the grounds that reaching a validated empty page proves the
# walk finished but not that it saw the POST just made. Two independent defects came out
@@ -1413,7 +1426,7 @@ jobs:
fi
case "$raced" in
''|*[!0-9]*)
# FAILS CLOSED (round 2). "Not acting on it" left the exemption green on a head whose
# FAILS CLOSED (ersatztv#763). "Not acting on it" left the exemption green on a head whose
# write window could not be inspected at all — the same uncertainty every other branch
# here resolves to `pending`, resolved the opposite way purely because it arrived as a
# malformed count rather than a failed read.
+5 -3
View File
@@ -1607,10 +1607,12 @@ The two directions are **not** symmetric, and the asymmetry is deliberate:
disagreeing with itself.
- **Pre-write, a partial list still yields a mark.** The mark gates the post-write check entirely, so
refusing one *skips* the check and is itself a fail-open. This rests on the DESC default above: the
newest row, carrying the maximum id, is on page 1, so a walk that fails later still saw it. Only a
read returning no rows at all abandons the mark — the pre-existing gap tracked as ersatztv#849,
newest row, carrying the maximum id, is on page 1, so a walk that fails later still saw it. A
*validated* empty history is a different case and is not abandoned: it yields a mark of 0, which is
correct, since every later row is newer — that is the normal first run. What abandons the mark is a
read that both **failed** and returned nothing, the pre-existing gap tracked as ersatztv#849 and
unchanged here. A non-empty history carrying no numeric id is reported as unusable rather than
collapsed to a mark of 0.
collapsed to 0.
Two id comparisons are **numeric-only**, both closing a fail-open: jq orders strings above every
number, so one `"id": "99999"` would inflate the high-water mark until nothing looked newer, and
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+103 -3
View File
@@ -1052,6 +1052,14 @@ if "/statuses/" in url:
#
# Counter branches are confined to the page-1 arm below, so a later page can never advance them.
HIST_PAGE_SIZE = 50
# ORDER-FAITHFUL MODES HONOUR `sort`. Most modes here are ordering-blind on purpose (see above):
# they test walk COMPLETENESS, which is an order-independent property, and serving insertion order
# lets a fixture place a row beyond page 1. But the partial-mark fallback's safety is a claim ABOUT
# the ordering, so the mode that pins it serves the real arrangement — DESC by default, ASC when
# the request asks for `sort=highestindex`. That is what lets re-adding the withdrawn parameter be
# caught by BEHAVIOUR rather than only by a structural assertion on the request line.
want_asc = "sort=highestindex" in url
order_faithful = False
hist_page = 1
for part in url.split("?", 1)[-1].split("&"):
if part.startswith("page="):
@@ -1119,6 +1127,36 @@ if "/statuses/" in url:
# row, so it is above the high-water mark too. A job that reads only page 1 cannot
# see it — the fail-toward-SUCCESS hole #763 closes.
rows.insert(55, dict(raced_row))
elif mode == "premark-page2-error-desc":
# THE PARTIAL-MARK FALLBACK, MEASURED UNDER THE REAL ORDERING. 60 rows, one of them a
# PRE-EXISTING base-mismatched human verdict at id 7055 — genuinely older than the newest
# row and therefore not something that raced this write.
#
# Under the server default (DESC) page 1 carries 7059..7010, so a walk that fails on page 2
# still saw the true maximum: the mark is 7059, the verdict at 7055 is below it, and the
# exemption correctly STANDS. Under ASC page 1 carries 7000..7049, the salvaged mark is
# 7049, and that same pre-existing verdict tests as NEWER than the mark — a spurious sticky
# repair on a head nothing raced, which is the #761 failure. This fixture is what makes
# that difference observable.
order_faithful = True
ctr = out / "history_reads.txt"
seen = int(ctr.read_text()) if ctr.exists() else 0
ctr.write_text(str(seen + 1))
rows = [{"id": 7000 + i, "context": "ci/other", "status": "success",
"creator": None, "description": "unrelated"}
for i in range(60) if 7000 + i != 7055]
rows.append({"id": 7055, "context": "review-verdict/h10", "status": "success",
"creator": {"login": "timothy"},
"description": "Review-verdict: MERGEABLE @ a9e3e23 (base: other)"})
rows.sort(key=lambda r: r["id"])
elif mode == "null-terminator":
# This endpoint returns `[]` past the end today. `null` is what `/issues/{n}/timeline`
# returns, and an array-only gate on THAT endpoint was ersatztv#751 — the walk never
# reached a validated empty page and every exemption was withheld. Same shape, second
# endpoint, worse blast radius (the sticky sentinel), so `null` is tolerated and this
# fixture proves it rather than asserting it.
rows = [{"id": 7000 + i, "context": "ci/other", "status": "success",
"creator": None, "description": "unrelated"} for i in range(60)]
elif mode == "string-id-preexisting-row":
# THE TWIN of `string-id-inflates-mark`, on the COMPARISON rather than the maximum. A
# PRE-EXISTING row (a base-mismatched verdict `read_existing_verdict` declines to honour)
@@ -1180,6 +1218,8 @@ if "/statuses/" in url:
seen = int(ctr.read_text()) if ctr.exists() else 0
ctr.write_text(str(seen + 1))
pages = [ordinary[:50], ordinary[50:], [dict(raced_row)] if seen > 0 else []]
if order_faithful and not want_asc:
rows = list(reversed(rows))
if pages is None:
pages = [rows[i:i + HIST_PAGE_SIZE] for i in range(0, len(rows), HIST_PAGE_SIZE)] or [[]]
@@ -1216,7 +1256,7 @@ if "/statuses/" in url:
if hist_page == 1 and mode == "premark-page1-error":
# PAGE 1 ITSELF FAILS, for the whole first logical read (both attempts), so the walk returns
# nothing at all and the mark must be abandoned. Counted in its own file because the logical
# read counter below is only reached on a SUCCESSFUL page-1 serve.
# read counter below counts page-1 REQUESTS, retries included — not logical reads.
ctr = out / "p1_attempts.txt"
seen = int(ctr.read_text()) if ctr.exists() else 0
ctr.write_text(str(seen + 1))
@@ -1236,7 +1276,10 @@ if "/statuses/" in url:
sys.exit(0)
if hist_page > 1 and mode == "second-page-error":
sys.exit(22)
if hist_page > 1 and mode == "premark-page2-error" and n_logical == 1:
if hist_page > 1 and mode == "null-terminator" and hist_page > len(pages):
print("null")
sys.exit(0)
if hist_page > 1 and mode in ("premark-page2-error", "premark-page2-error-desc") and n_logical == 1:
# Fails ONLY on the PRE-write read, so the high-water mark must be salvaged from the partial
# list; the post-write read then pages cleanly and must still catch the raced verdict.
# `n_logical` is incremented on every page-1 REQUEST, including retried ones — which is fine
@@ -3749,6 +3792,63 @@ A verdict sits BEYOND the first page — 60 ordinary rows with it inserted at in
)
def test_a_partial_mark_is_SAFE_because_the_newest_rows_are_on_page_1(tmp_path):
"""The partial-mark fallback's safety is a claim ABOUT THE ORDERING, so it is measured under the
real one — this is the only order-faithful fixture here.
The fallback exists because refusing a mark skips the post-write check entirely, which is a
fail-open. It is safe only because the server serves `created_unix DESC` and ids are monotonic
with `created_at`: page 1 therefore carries the true maximum, so a walk that fails on page 2 has
still seen it.
The history holds a PRE-EXISTING base-mismatched verdict at id 7055 — older than the newest row,
so nothing it does raced this write. Under DESC the salvaged mark is 7059 and that row is below
it: the exemption correctly STANDS. Under ASC (`sort=highestindex`, withdrawn in round 4) page 1
would be 7000..7049, the mark 7049, and that same untouched row would test as NEWER than the mark
— a sticky repair on a head nothing raced, which is exactly #761.
So re-adding the sort parameter reddens this test by BEHAVIOUR, not merely by the structural
assertion in `test_the_walk_does_not_request_a_SORT_order`.
"""
posted, r = _run_classify(tmp_path, _emitting("docs/a.md"), history_mode="premark-page2-error-desc")
assert r.returncode == 0, r.stderr
log = r.stdout + r.stderr
assert "taking the high-water mark over the" in log, (
f"the partial-list fallback did not run, so this test is not measuring it:\n{r.stdout[-900:]}"
)
seq = _posted_sequence(tmp_path)
assert len(seq) == 1, (
"a PRE-EXISTING verdict was read as newer than the salvaged mark, so the exemption was "
f"repaired away on a head nothing raced. Posts: {seq}\n{r.stdout[-900:]}"
)
assert seq[0]["state"] == "success", f"expected the exemption to stand; got {seq}"
assert "was overwritten" not in log, (
f"nothing raced this write, but the log claims a verdict was overwritten:\n{r.stdout[-900:]}"
)
def test_a_NULL_page_terminates_the_walk_as_an_empty_one(tmp_path):
"""`/statuses/{sha}` returns `[]` past the end today; `/issues/{n}/timeline` returns bare `null`.
An array-only gate on the timeline was ersatztv#751: the walk never reached a validated empty
page, `rt_ok` was never `yes` for any PR, and the fence withheld EVERY exemption `success` from
the day it shipped. Re-adopting that narrowing on this endpoint would be worse, because this
walk's failure mode is the STICKY sentinel — every exempt PR would need a human verdict, per head,
clearable only by hand.
Tolerating `null` cannot misread `[]`, so it costs nothing. This asserts it rather than trusting
that the endpoint will never change its empty shape.
"""
posted, r = _run_classify(tmp_path, _emitting("docs/a.md"), history_mode="null-terminator")
assert r.returncode == 0, r.stderr
seq = _posted_sequence(tmp_path)
assert len(seq) == 1, (
"a `null` page past the end was read as unreadable rather than as exhaustion, so the walk "
f"never validated and the exemption was repaired away. Posts: {seq}\n{r.stdout[-900:]}"
)
assert seq[0]["state"] == "success", f"expected the exemption to stand; got {seq}"
def test_a_STRING_id_on_a_PRE_EXISTING_row_is_not_read_as_raced(tmp_path):
"""The twin of `test_a_STRING_id_cannot_inflate_the_high_water_mark`, on the COMPARISON rather
than the maximum — and the twin was live on `main`, one expression away from the fix.
@@ -3987,7 +4087,7 @@ def test_a_verdict_BEYOND_A_SHORT_PAGE_is_still_found(tmp_path):
def test_a_PRE_WRITE_paging_failure_still_yields_a_usable_high_water_mark(tmp_path):
"""The fail-open a paging change can introduce while fixing one (round-2 Blocker).
"""The fail-open a paging change can introduce while fixing one (ersatztv#763).
The high-water mark gates the post-write race check entirely: `max_id_before=-1` skips it, so a
human rejection landing in the write window is neither detected nor repaired. Before paging, only