From 7268054c06259b183c3838519c73846667fe20e0 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 05:47:24 +0000 Subject: [PATCH 01/29] A review brief names at most five tests to run first and counts the test files it leaves out The tests section's first row, kind run_first, holds the first five tests code.test_gate lists for the PR's diff, in the gate's order (must_run first), one per test: rows that share a run command, or a command-less file beside a same-named one, are one test. Each carries why, evidence, run and must_run; left_out and must_run_left_out count the test files under none of them. The row is advice no verdict rests on. Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 11 +++ docs/features/code-index/operations.md | 4 + server/code/app/index/review_brief.py | 8 +- server/code/app/index/review_brief_build.py | 61 +++++++++++++-- server/code/app/review_brief.py | 8 +- server/code/domain/index/test_gate.py | 74 ++++++++++++++++++ server/transport/mcp_descriptions.py | 6 +- .../unit/test_code_review_brief_no_groups.py | 2 + tests/unit/test_code_review_brief_sections.py | 75 ++++++++++++++++--- tests/unit/test_code_test_gate.py | 58 ++++++++++++++ 10 files changed, 284 insertions(+), 23 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 8794a107f1e..2fd5375ce14 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -409,6 +409,17 @@ at most eight sites, outside the methods of its own class (the its head's, which the index rows are not at, so its brief collapses nothing. The section's `note` gives both counts. +The first `tests` row, `kind: "run_first"`, names the tests to run first: the first five +tests `code.test_gate` lists for the PR's diff, in its order, so every test that says +`must_run` comes before a lead and the strongest evidence comes first. Files that share a +run command, or a directory and a name up to the first dot (a case's script and its +expected output), are one test, listed once with every file in `test_paths`. Each test +carries the gate's `why`, `evidence`, `run` and `must_run` for its first file. `left_out` +counts the test files the gate holds under none of the five, the leads it cut included, and +`must_run_left_out` how many of them say `must_run`; `code.test_gate` lists them all. The +row is advice no verdict rests on: a read of the gate that raised keeps the brief unstored, +and a gate that lists no test leaves the row out. + Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier by repository path, and `tests_at_tier`, how many tests of that tier the claim listed (it diff --git a/docs/features/code-index/operations.md b/docs/features/code-index/operations.md index 8b9d512440d..daaa9a4259a 100644 --- a/docs/features/code-index/operations.md +++ b/docs/features/code-index/operations.md @@ -771,6 +771,10 @@ the nested one), or an override or a subclass, as a test double is. The other `s rows, and the `test_map` and `file_neighbor` rows, say `false` too. Those rows may not run the changed code, and a caller with a budget can leave them out. +A review brief names the first five tests of this list, one per test, in its `tests` +section's `run_first` row, with the count of the test files it leaves out ([review +briefs](history-and-prs.md#review-briefs-and-conventions)). + The gate reads a test file (one under a test directory such as `tests/`, `test/` or `spec/`, or a test-named file such as `foo_test.go`) as a test, except a definition there that other tests call or take as a fixture (a `conftest.py` fixture, a builder): that is a helper. A diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index dee741e831e..7dc8c3cb3ac 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -24,9 +24,11 @@ weekly fan-in; the wiring the PR adds that nothing but a registry or its own class reaches (`WIRING_KINDS`) is a count per kind in `collapsed`, not a row, and is claimed like any other row; -- `tests` — a coverage claim per touched function and setting, with the - claim's strongest evidence tier and only the tests of that tier, - and the test file this PR's fingerprint belongs in; +- `tests` — the first tests code.test_gate lists for the diff, one per test + with the reason it is there and how many were left out; a coverage + claim per touched function and setting, with the claim's strongest + evidence tier and only the tests of that tier; and the test file this + PR's fingerprint belongs in; - `settings` — the facts of every setting the diff declares, changes in the history file or uses in a new stateless test; - `history` — each touched function's regression record; diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 56ea5fea290..f4957f00d38 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -64,7 +64,7 @@ from server.code.app.symbol.diff import _parse_unified_diff_full, is_blank from server.code.app.symbol.resolve import _parse_file_symbol from server.code.domain.index.epistemic import CAUSE_NEW_SIDE_FAILED, CAUSE_UNMAPPED -from server.code.domain.index.test_gate import is_test_path +from server.code.domain.index.test_gate import is_test_path, run_first from server.observability import degradations logger = logging.getLogger(__name__) @@ -149,7 +149,9 @@ async def build_brief( ranges = deleted_ranges(facts, src_only=is_graphed_path) - tests_t = _tests_section(call, inp, names[:MAX_COVERAGE_SYMBOLS], setting_names, stems) + tests_t = _tests_section( + call, inp, names[:MAX_COVERAGE_SYMBOLS], setting_names, stems, paths=paths + ) settings_t = _settings_section(call, inp, facts.settings[:MAX_SETTINGS]) defs = list(dict.fromkeys((str(r["qualified_name"]), str(r["path"])) for r in ranked)) history_t = _history_section(call, inp, defs[:MAX_HISTORY_SYMBOLS]) @@ -730,6 +732,8 @@ async def _tests_section( names: list[str], setting_names: list[str], stems: list[str], + *, + paths: list[str], ) -> dict[str, Any]: base = {"org": inp.org, "namespace": inp.ns} subjects = [("symbol", n) for n in names] + [ @@ -754,8 +758,12 @@ async def _tests_section( }, ) ) - results = await asyncio.gather(*tasks, return_exceptions=True) - rows: list[dict[str, Any]] = [] + (results, (first, advice_failed)) = await asyncio.gather( + asyncio.gather(*tasks, return_exceptions=True), _run_first_row(call, inp, paths) + ) + # The tests to run first are advice no verdict rests on, first so a token budget trims + # them last + rows: list[dict[str, Any]] = [first] if first else [] causes: list[str] = [] left = 0 # claims an op refused to read: a cap epistemic = "lower_bound" @@ -785,13 +793,12 @@ async def _tests_section( ) # The home is advice no verdict rests on: a read of it that raised only keeps the # brief unstored, and a refused one leaves the row out - home_failed: list[str] = [] if home_wanted: res = results[-1] if isinstance(res, BaseException): if not refused(res): degradations.partial("code.review_brief", "test_home_failed", res) - home_failed.append(failure_cause("test_home", res)) + advice_failed.append(failure_cause("test_home", res)) else: rows.append( { @@ -805,7 +812,8 @@ async def _tests_section( "next_free_stateless_number": int(res.get("next_free_stateless_number") or 0), } ) - if rows and all(r.get("epistemic") == "exact" for r in rows if r.get("kind") != "home"): + claims = [r for r in rows if r.get("kind") in ("symbol", "setting")] + if rows and all(r.get("epistemic") == "exact" for r in claims): epistemic = "exact" out = section( SERVED, @@ -813,12 +821,49 @@ async def _tests_section( epistemic=epistemic, causes=causes, rows=rows, - failed=[*causes, *home_failed], # every cause here is a read that raised + failed=[*causes, *advice_failed], # every cause here is a read that raised ) _cut(out, NOT_CLAIMED, left) return out +async def _run_first_row( + call: CallOp, inp: BriefInputs, paths: list[str] +) -> tuple[dict[str, Any] | None, list[str]]: + """(the `run_first` row of the tests section, the read of it that raised): the first + tests code.test_gate lists for the PR's diff (`run_first`), None when it lists none. The + diff is read as written: a merged PR's new side is the merge, which the index holds. A + read that raised only keeps the brief unstored, and a refused one leaves the row out.""" + if not (paths or inp.diff): + return None, [] + try: + gate = await call( + "code.test_gate", + { + "org": inp.org, + "namespace": inp.ns, + "files": paths[:IMPACT_SEED_FILES], # code.test_gate's cap too + "diff": inp.diff[:MAX_DIFF_CHARS], + }, + ) + except Exception as e: # noqa: BLE001 — the tests to run first are advice + if refused(e): + return None, [] + degradations.partial("code.review_brief", "test_gate_failed", e) + return None, [failure_cause("test_gate", e)] + tests = list(gate.get("tests") or []) + unnamed = int(gate.get("tests_unnamed") or 0) + if not (tests or unnamed): + return None, [] + picked = run_first( + tests, + tests_total=int(gate.get("tests_total") or 0), + must_run_total=int(gate.get("must_run_total") or 0), + tests_unnamed=unnamed, + ) + return {"kind": "run_first", **picked}, [] + + async def _settings_section( call: CallOp, inp: BriefInputs, diff --git a/server/code/app/review_brief.py b/server/code/app/review_brief.py index 6b677570912..fe757c07a9e 100644 --- a/server/code/app/review_brief.py +++ b/server/code/app/review_brief.py @@ -20,7 +20,11 @@ from server.code.api.models_history import CodeBlameRangeInput, CodeSymbolHistoryInput from server.code.api.models_review import CodeReviewBriefOutput from server.code.api.models_symbol import CodeImpactInput -from server.code.api.models_testing import CodeCoverageClaimInput, CodeTestHomeInput +from server.code.api.models_testing import ( + CodeCoverageClaimInput, + CodeTestGateInput, + CodeTestHomeInput, +) from server.code.app.common import ( new_trace_run_id, require_code_intel, @@ -52,12 +56,14 @@ async def call_code_op(op: str, body: dict[str, Any]) -> dict[str, Any]: from server.code.app.coverage import handle_code_coverage_claim, handle_code_test_home from server.code.app.settings import handle_code_setting_facts from server.code.app.symbol import handle_code_impact + from server.code.app.symbol.gate import handle_code_test_gate from server.code.app.symbol_history import handle_code_symbol_history table: dict[str, tuple[Any, Any]] = { "code.impact": (CodeImpactInput, handle_code_impact), "code.coverage_claim": (CodeCoverageClaimInput, handle_code_coverage_claim), "code.test_home": (CodeTestHomeInput, handle_code_test_home), + "code.test_gate": (CodeTestGateInput, handle_code_test_gate), "code.setting_facts": (CodeSettingFactsInput, handle_code_setting_facts), "code.symbol_history": (CodeSymbolHistoryInput, handle_code_symbol_history), "code.blame_range": (CodeBlameRangeInput, handle_code_blame_range), diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index 75c7247227e..b1ea740e53f 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -247,6 +247,80 @@ def _links_to(roots: set[str], names: dict[str, str]) -> list[str]: return sorted(names[r] for r in roots if r in names)[:_LINKS_SHOWN] +# Tests a brief names to run first (`run_first`). +# not a setting: the length of the list the brief's readers asked for +RUN_FIRST = 5 + + +def _stem_key(path: str) -> str: + """A test file's directory and its name up to the first dot: `a.sql` and `a.expected`.""" + p = PurePosixPath(path) + return f"{p.parent}/{p.name.split('.', 1)[0]}" + + +def _test_groups(tests: list[dict[str, Any]]) -> list[dict[str, Any]]: + """The gate's rows as tests, in their order: rows that share a run command are one + test, and a row with none joins a test of its directory and name (`_stem_key`, a case's + expected output beside its script), as one with a command joins such rows that have + none. Rows with different commands are never one test.""" + groups: list[dict[str, Any]] = [] + for r in tests: + run, stem = str(r.get("run") or ""), _stem_key(str(r["test_path"])) + group = next( + ( + g + for g in groups + if (run and g["run"] == run) or (stem in g["stems"] and not (run and g["run"])) + ), + None, + ) + if group is None: + groups.append({"run": run, "stems": {stem}, "rows": [r]}) + continue + group["run"] = group["run"] or run + group["stems"].add(stem) + group["rows"].append(r) + return groups + + +def run_first( + tests: list[dict[str, Any]], + *, + tests_total: int, + must_run_total: int, + tests_unnamed: int, + limit: int = RUN_FIRST, +) -> dict[str, Any]: + """The first `limit` tests of a gate's `tests` rows (`_test_groups`), in the gate's + order: every `must_run` row first, the strongest evidence first. A test carries its + first row's path, `why`, `evidence` and `must_run`, and its run command. `left_out` + counts the gate's test files under no listed test, the leads it cut included, and + `must_run_left_out` those of them that say `must_run`; `tests_total`, `must_run_total` + and `tests_unnamed` are the gate's counts of the rows `tests` was cut from.""" + listed = _test_groups(tests)[: max(0, limit)] + shown = [r for g in listed for r in g["rows"]] + return { + "tests": [ + { + "test_path": g["rows"][0]["test_path"], + "run": g["run"], + "run_unknown": not g["run"], + "why": g["rows"][0]["why"], + "evidence": g["rows"][0].get("evidence") or "", + "must_run": bool(g["rows"][0].get("must_run")), + **( + {"test_paths": [r["test_path"] for r in g["rows"]]} + if len(g["rows"]) > 1 + else {} + ), + } + for g in listed + ], + "left_out": max(0, tests_total + tests_unnamed - len(shown)), + "must_run_left_out": max(0, must_run_total - sum(1 for r in shown if r.get("must_run"))), + } + + def link_state(run: dict[str, Any] | None, linked: bool) -> str: """What the index knows of one definition's tests: what a verified coverage run measured of it when there is one (`executed`, `unexecuted`), else whether a call diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index 69143fd3781..c0d9c0000b1 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1657,7 +1657,11 @@ def revision_policy(update: str) -> str: "callers and fan-in; the registrations, factories, constructors and " "one-line definitions the PR adds that nothing else calls are counts " "in `collapsed`), " - "tests (coverage_claim per function and setting, its strongest tier " + "tests (first a run_first row: the first five tests code.test_gate " + "lists for the diff, must_run ones first, one per test with its why and " + "evidence, and `left_out` / `must_run_left_out` counting the test files " + "under none of them, which code.test_gate lists in full; then " + "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " "plus the test file this change belongs in), settings (facts of " diff --git a/tests/unit/test_code_review_brief_no_groups.py b/tests/unit/test_code_review_brief_no_groups.py index 9a6095f76bb..6bd351d3d70 100644 --- a/tests/unit/test_code_review_brief_no_groups.py +++ b/tests/unit/test_code_review_brief_no_groups.py @@ -225,6 +225,8 @@ async def _call(op: str, body: dict[str, Any]) -> dict[str, Any]: "tests_total": 2 if covered else 0, "epistemic": "lower_bound", } + if op == "code.test_gate": + return {"tests": [], "tests_total": 0, "must_run_total": 0, "tests_unnamed": 0} if op == "code.test_home": return { "verdict": "add_case_to", diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index ca28bdc2b54..3c54e7640b3 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -583,6 +583,30 @@ async def __call__(self, op: str, body: dict[str, Any]) -> dict[str, Any]: "tests_total": 4523 if covered else 0, "epistemic": "lower_bound", } + if op == "code.test_gate": + return { + "tests": [ + { + "test_path": "tests/integration/test_parts/test.py", + "why": "static_call", + "evidence": "test_load reaches the change in 1 hop(s)", + "run": "pytest tests/integration/test_parts", + "run_unknown": False, + "must_run": True, + }, + { + "test_path": "tests/integration/test_parts/__init__.py", + "why": "test_map", + "evidence": "test map (pr_cooccurrence), confidence 0.70", + "run": "pytest tests/integration/test_parts", + "run_unknown": False, + "must_run": False, + }, + ], + "tests_total": 2, + "must_run_total": 1, + "tests_unnamed": 6, + } if op == "code.test_home": return { "verdict": "add_case_to", @@ -781,8 +805,31 @@ async def test_build_assembles_every_section_with_verdicts_and_decisions() -> No assert secs["symbols"]["verdict"] == "touched" and secs["symbols"]["causes"] == [ "treesitter_only" ] - # tests: a claim per symbol and per setting, then the home; Settings::dump is uncovered - kinds = [(r["kind"], r["subject"]) for r in secs["tests"]["rows"]] + # tests: the tests to run first, a claim per symbol and per setting, then the home; + # Settings::dump is uncovered + first = secs["tests"]["rows"][0] + assert first == { + "kind": "run_first", + "tests": [ + { + "test_path": "tests/integration/test_parts/test.py", + "run": "pytest tests/integration/test_parts", + "run_unknown": False, + "why": "static_call", + "evidence": "test_load reaches the change in 1 hop(s)", + "must_run": True, + "test_paths": [ + "tests/integration/test_parts/test.py", + "tests/integration/test_parts/__init__.py", + ], + } + ], + "left_out": 6, + "must_run_left_out": 0, + } + gate_body = next(b for op, b in calls.calls if op == "code.test_gate") + assert gate_body["diff"] == _inputs().diff and "src/Core/Settings.cpp" in gate_body["files"] + kinds = [(r["kind"], r.get("subject")) for r in secs["tests"]["rows"][1:]] assert ("symbol", "DB::MergeTreeData::loadDataParts") in kinds and ( "setting", "new_flag", @@ -791,7 +838,7 @@ async def test_build_assembles_every_section_with_verdicts_and_decisions() -> No assert secs["tests"]["verdict"] == "uncovered" # a claim row keeps its strongest tier and that tier's tests by path; the grep total # of every tier is not carried - claim = secs["tests"]["rows"][kinds.index(("symbol", "DB::MergeTreeData::loadDataParts"))] + claim = secs["tests"]["rows"][1:][kinds.index(("symbol", "DB::MergeTreeData::loadDataParts"))] assert claim["strongest_tier"] == "exact_scoped" and claim["tests_at_tier"] == 2 assert claim["tests"] == [ "tests/queries/0_stateless/02994_parts.sql", @@ -1004,9 +1051,16 @@ async def test_a_failing_op_fails_its_section_open_and_the_rest_stand() -> None: @pytest.mark.asyncio -async def test_the_test_home_decides_no_verdict() -> None: - """The test home is advice: a read of it that raised or was refused leaves a settled - brief settled, and one that raised keeps the brief unstored.""" +@pytest.mark.parametrize( + ("op", "kind", "cause"), + [("code.test_home", "home", "test_home"), ("code.test_gate", "run_first", "test_gate")], +) +async def test_the_test_home_and_the_tests_to_run_first_decide_no_verdict( + op: str, kind: str, cause: str +) -> None: + """The test home and the tests to run first are advice: a read of one that raised or + was refused leaves a settled brief settled, and one that raised keeps the brief + unstored.""" from server.core.errors import ValidationError async def build(fail: set[str], error: type[Exception] = RuntimeError) -> dict[str, Any]: @@ -1020,12 +1074,12 @@ async def build(fail: set[str], error: type[Exception] = RuntimeError) -> dict[s base = await build(set()) assert rb.brief_verdict(base) == ("covered_no_gaps", []) - assert any(r["kind"] == "home" for r in base["tests"]["rows"]) - for error, failed in ((RuntimeError, ["tests:test_home:RuntimeError"]), (ValidationError, [])): - secs = await build({"code.test_home"}, error) + assert any(r["kind"] == kind for r in base["tests"]["rows"]) + for error, failed in ((RuntimeError, [f"tests:{cause}:RuntimeError"]), (ValidationError, [])): + secs = await build({op}, error) assert rb.brief_verdict(secs) == ("covered_no_gaps", []) assert rb.failed_reads(secs) == failed - assert not any(r["kind"] == "home" for r in secs["tests"]["rows"]) + assert not any(r["kind"] == kind for r in secs["tests"]["rows"]) @pytest.mark.asyncio @@ -3544,6 +3598,7 @@ async def run(git_dir: str, *args: str) -> str: ("symbol_stats", "code_index: fan_in_unread (TimeoutError)"), ("code.coverage_claim", "code.review_brief: coverage_claim_failed (RuntimeError)"), ("code.test_home", "code.review_brief: test_home_failed (RuntimeError)"), + ("code.test_gate", "code.review_brief: test_gate_failed (RuntimeError)"), ("code.setting_facts", "code.review_brief: setting_facts_failed (RuntimeError)"), ("code.symbol_history", "code.review_brief: symbol_history_failed (RuntimeError)"), ("code.blame_range", "code.review_brief: blame_range_failed (RuntimeError)"), diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index ae9031cc422..34ba0451fe8 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -219,6 +219,64 @@ def test_each_row_says_whether_its_test_is_tied_to_the_change_or_to_its_file() - assert [(r["why"], r["must_run"]) for r in few.tests] == [("file_neighbor", False)] +def _gate_row(path: str, why: str, must_run: bool) -> dict[str, Any]: + run, unknown = tg.run_command_for(path) + return { + "test_path": path, + "why": why, + "evidence": f"{why} evidence", + "run": run, + "run_unknown": unknown, + "must_run": must_run, + } + + +def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: + """A case's script and its expected output are one test, as are the files of one run + command, under the stronger row; the list stops at `limit` and counts the test files it + leaves out.""" + rows = [ + _gate_row("tests/cases/a.expected", "changed_test", True), + {**_gate_row("tests/cases/a.sh", "changed_test", True), "run": "sh tests/cases/a.sh"}, + _gate_row("src/x/tests/check_x.cpp", "static_call", True), + _gate_row("pkg/store/store_test.go", "coverage", True), + _gate_row("pkg/store/load_test.go", "test_map", False), + _gate_row("tests/unit/test_b.py", "stem_partner", False), + _gate_row("tests/unit/test_c.py", "test_map", False), + ] + out = tg.run_first(rows, tests_total=7, must_run_total=4, tests_unnamed=3, limit=3) + assert [(t["test_path"], t["why"], t["must_run"]) for t in out["tests"]] == [ + ("tests/cases/a.expected", "changed_test", True), + ("src/x/tests/check_x.cpp", "static_call", True), + ("pkg/store/store_test.go", "coverage", True), + ] + # the expected output's test is run by its script's command + assert out["tests"][0]["test_paths"] == ["tests/cases/a.expected", "tests/cases/a.sh"] + assert out["tests"][0]["run"] == "sh tests/cases/a.sh" and not out["tests"][0]["run_unknown"] + assert out["tests"][2]["run"] == "go test ./pkg/store/" and out["tests"][2]["test_paths"] == [ + "pkg/store/store_test.go", + "pkg/store/load_test.go", + ] + # a test of one file names no `test_paths` + assert "test_paths" not in out["tests"][1] and out["tests"][1]["run_unknown"] + # 5 of the 7 rows are listed; the 3 cut leads are left out too + assert out["left_out"] == 5 and out["must_run_left_out"] == 0 + # with the list shorter than the must-run tests, the rest are counted + one = tg.run_first(rows, tests_total=7, must_run_total=4, tests_unnamed=0, limit=1) + assert one["left_out"] == 5 and one["must_run_left_out"] == 2 + # two tests of one name with different commands stay two + pair = [ + _gate_row("web/a.test.ts", "coverage", True), + _gate_row("web/a.spec.ts", "coverage", True), + ] + assert len(tg.run_first(pair, tests_total=2, must_run_total=2, tests_unnamed=0)["tests"]) == 2 + assert tg.run_first([], tests_total=0, must_run_total=0, tests_unnamed=4) == { + "tests": [], + "left_out": 4, + "must_run_left_out": 0, + } + + def test_a_one_hop_row_whose_test_uses_no_touched_definition_is_a_lead() -> None: """A test double that overrides the change and a call of the function a touched nested function is defined in are one hop away and not `direct`: they come after the direct From 1154438f315e967a3af149ef432602bb251f8915 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 05:47:37 +0000 Subject: [PATCH 02/29] The brief's docs state the one-test rule as built Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 2fd5375ce14..c6db41787ad 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -412,13 +412,14 @@ section's `note` gives both counts. The first `tests` row, `kind: "run_first"`, names the tests to run first: the first five tests `code.test_gate` lists for the PR's diff, in its order, so every test that says `must_run` comes before a lead and the strongest evidence comes first. Files that share a -run command, or a directory and a name up to the first dot (a case's script and its -expected output), are one test, listed once with every file in `test_paths`. Each test -carries the gate's `why`, `evidence`, `run` and `must_run` for its first file. `left_out` -counts the test files the gate holds under none of the five, the leads it cut included, and -`must_run_left_out` how many of them say `must_run`; `code.test_gate` lists them all. The -row is advice no verdict rests on: a read of the gate that raised keeps the brief unstored, -and a gate that lists no test leaves the row out. +run command are one test, and a file with no run command joins the test of its directory and +name up to the first dot (a case's expected output beside its script); files with different +run commands are never one test. A test of several files lists them in `test_paths`. Each +test carries its run command and the gate's `why`, `evidence` and `must_run` for its first +file. `left_out` counts the test files the gate holds under none of the five, the leads it +cut included, and `must_run_left_out` how many of them say `must_run`; `code.test_gate` +lists them all. The row is advice no verdict rests on: a read of the gate that raised keeps +the brief unstored, and a gate that lists no test leaves the row out. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier From ce2c82ef174bffbd5fe6196166e9b83a6ceed566 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 06:03:48 +0000 Subject: [PATCH 03/29] The gate reads a merged PR's diff from the merge commit; one run command is one test code.test_gate reads a diff's old side as the revision the index holds, so a merged PR's brief sends it the reversed diff, as it sends code.impact. A row joins the test of its run command before a test of its name. A section of the tests to run first alone stays lower_bound. left_out is documented as a lower bound. Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 8 +++--- server/code/app/index/review_brief.py | 4 +-- server/code/app/index/review_brief_build.py | 23 ++++++++++------- server/code/domain/index/test_gate.py | 25 ++++++++----------- server/transport/mcp_descriptions.py | 4 +-- tests/unit/test_code_review_brief_sections.py | 20 ++++++++++++++- tests/unit/test_code_test_gate.py | 14 +++++++++++ 7 files changed, 66 insertions(+), 32 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index c6db41787ad..11a8ec39bd3 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -416,10 +416,10 @@ run command are one test, and a file with no run command joins the test of its d name up to the first dot (a case's expected output beside its script); files with different run commands are never one test. A test of several files lists them in `test_paths`. Each test carries its run command and the gate's `why`, `evidence` and `must_run` for its first -file. `left_out` counts the test files the gate holds under none of the five, the leads it -cut included, and `must_run_left_out` how many of them say `must_run`; `code.test_gate` -lists them all. The row is advice no verdict rests on: a read of the gate that raised keeps -the brief unstored, and a gate that lists no test leaves the row out. +file. `left_out` counts at least the test files the gate holds under none of the five, the +leads it cut included, and `must_run_left_out` how many of them say `must_run`; +`code.test_gate` lists them all. The row is advice no verdict rests on: a read of the gate +that raised keeps the brief unstored, and a gate that holds no test leaves the row out. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index 7dc8c3cb3ac..8ebb14d8967 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -552,8 +552,8 @@ def section( failed: list[str] | None = None, ) -> dict[str, Any]: """One section. `failed` names the reads of it that raised (`failure_cause`), which are - among its `causes` too unless no verdict rests on the read (`code.test_home`); the key - rides only when there are any.""" + among its `causes` too unless no verdict rests on the read (`code.test_home`, + `code.test_gate`); the key rides only when there are any.""" rows = rows or [] out: dict[str, Any] = { "status": status if rows or status != SERVED else EMPTY, diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index f4957f00d38..a618b4ef491 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -150,7 +150,7 @@ async def build_brief( ranges = deleted_ranges(facts, src_only=is_graphed_path) tests_t = _tests_section( - call, inp, names[:MAX_COVERAGE_SYMBOLS], setting_names, stems, paths=paths + call, inp, names[:MAX_COVERAGE_SYMBOLS], setting_names, stems, paths=paths, diff=diff ) settings_t = _settings_section(call, inp, facts.settings[:MAX_SETTINGS]) defs = list(dict.fromkeys((str(r["qualified_name"]), str(r["path"])) for r in ranked)) @@ -734,6 +734,7 @@ async def _tests_section( stems: list[str], *, paths: list[str], + diff: str, ) -> dict[str, Any]: base = {"org": inp.org, "namespace": inp.ns} subjects = [("symbol", n) for n in names] + [ @@ -759,7 +760,7 @@ async def _tests_section( ) ) (results, (first, advice_failed)) = await asyncio.gather( - asyncio.gather(*tasks, return_exceptions=True), _run_first_row(call, inp, paths) + asyncio.gather(*tasks, return_exceptions=True), _run_first_row(call, inp, paths, diff) ) # The tests to run first are advice no verdict rests on, first so a token budget trims # them last @@ -812,8 +813,11 @@ async def _tests_section( "next_free_stateless_number": int(res.get("next_free_stateless_number") or 0), } ) + # the advice rows say nothing of what the claims read: a section of the home alone is + # exact, one of the tests to run first alone is not claims = [r for r in rows if r.get("kind") in ("symbol", "setting")] - if rows and all(r.get("epistemic") == "exact" for r in claims): + others = [r for r in rows if r.get("kind") != "run_first"] + if others and all(r.get("epistemic") == "exact" for r in claims): epistemic = "exact" out = section( SERVED, @@ -828,13 +832,14 @@ async def _tests_section( async def _run_first_row( - call: CallOp, inp: BriefInputs, paths: list[str] + call: CallOp, inp: BriefInputs, paths: list[str], diff: str ) -> tuple[dict[str, Any] | None, list[str]]: """(the `run_first` row of the tests section, the read of it that raised): the first - tests code.test_gate lists for the PR's diff (`run_first`), None when it lists none. The - diff is read as written: a merged PR's new side is the merge, which the index holds. A - read that raised only keeps the brief unstored, and a refused one leaves the row out.""" - if not (paths or inp.diff): + tests code.test_gate lists for the PR's diff (`run_first`), None when it holds none. The + gate reads a diff's old side as the revision the index holds, so `diff` is the one + code.impact reads: a merged PR's reversed, an open PR's as written. A read that raised + only keeps the brief unstored, and a refused one leaves the row out.""" + if not (paths or diff): return None, [] try: gate = await call( @@ -843,7 +848,7 @@ async def _run_first_row( "org": inp.org, "namespace": inp.ns, "files": paths[:IMPACT_SEED_FILES], # code.test_gate's cap too - "diff": inp.diff[:MAX_DIFF_CHARS], + "diff": diff[:MAX_DIFF_CHARS], }, ) except Exception as e: # noqa: BLE001 — the tests to run first are advice diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index b1ea740e53f..e89a3a4d794 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -261,18 +261,14 @@ def _stem_key(path: str) -> str: def _test_groups(tests: list[dict[str, Any]]) -> list[dict[str, Any]]: """The gate's rows as tests, in their order: rows that share a run command are one test, and a row with none joins a test of its directory and name (`_stem_key`, a case's - expected output beside its script), as one with a command joins such rows that have - none. Rows with different commands are never one test.""" + expected output beside its script), as one with a command joins such a test that has + none yet. Rows with different commands are never one test.""" groups: list[dict[str, Any]] = [] for r in tests: run, stem = str(r.get("run") or ""), _stem_key(str(r["test_path"])) - group = next( - ( - g - for g in groups - if (run and g["run"] == run) or (stem in g["stems"] and not (run and g["run"])) - ), - None, + same_run = next((g for g in groups if run and g["run"] == run), None) + group = same_run or next( + (g for g in groups if stem in g["stems"] and not (run and g["run"])), None ) if group is None: groups.append({"run": run, "stems": {stem}, "rows": [r]}) @@ -294,10 +290,11 @@ def run_first( """The first `limit` tests of a gate's `tests` rows (`_test_groups`), in the gate's order: every `must_run` row first, the strongest evidence first. A test carries its first row's path, `why`, `evidence` and `must_run`, and its run command. `left_out` - counts the gate's test files under no listed test, the leads it cut included, and - `must_run_left_out` those of them that say `must_run`; `tests_total`, `must_run_total` + counts the gate's test files under no listed test, the leads it cut included (at + least: `tests_unnamed` is a lower bound), and `must_run_left_out` those of them that + say `must_run`; `tests_total`, `must_run_total` and `tests_unnamed` are the gate's counts of the rows `tests` was cut from.""" - listed = _test_groups(tests)[: max(0, limit)] + listed = _test_groups(tests)[:limit] shown = [r for g in listed for r in g["rows"]] return { "tests": [ @@ -316,8 +313,8 @@ def run_first( } for g in listed ], - "left_out": max(0, tests_total + tests_unnamed - len(shown)), - "must_run_left_out": max(0, must_run_total - sum(1 for r in shown if r.get("must_run"))), + "left_out": tests_total + tests_unnamed - len(shown), + "must_run_left_out": must_run_total - sum(1 for r in shown if r.get("must_run")), } diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index c0d9c0000b1..ace82092096 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1659,8 +1659,8 @@ def revision_policy(update: str) -> str: "in `collapsed`), " "tests (first a run_first row: the first five tests code.test_gate " "lists for the diff, must_run ones first, one per test with its why and " - "evidence, and `left_out` / `must_run_left_out` counting the test files " - "under none of them, which code.test_gate lists in full; then " + "evidence, and `left_out` / `must_run_left_out` counting at least the " + "test files under none of them, which code.test_gate lists in full; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 3c54e7640b3..cc807a15336 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -828,7 +828,11 @@ async def test_build_assembles_every_section_with_verdicts_and_decisions() -> No "must_run_left_out": 0, } gate_body = next(b for op, b in calls.calls if op == "code.test_gate") - assert gate_body["diff"] == _inputs().diff and "src/Core/Settings.cpp" in gate_body["files"] + # read from the merge commit, which the index holds, as impact reads it + assert ( + gate_body["diff"] == rb.reversed_diff(DIFF) + and "src/Core/Settings.cpp" in gate_body["files"] + ) kinds = [(r["kind"], r.get("subject")) for r in secs["tests"]["rows"][1:]] assert ("symbol", "DB::MergeTreeData::loadDataParts") in kinds and ( "setting", @@ -1082,6 +1086,20 @@ async def build(fail: set[str], error: type[Exception] = RuntimeError) -> dict[s assert not any(r["kind"] == kind for r in secs["tests"]["rows"]) +@pytest.mark.asyncio +async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> None: + """A PR with no claim and no home still names its tests to run first; the row says + nothing of what a claim read, so the section stays `lower_bound` with no verdict.""" + calls = _Calls() + inp = dataclasses.replace(_inputs(), open=True, head_sha="a" * 40) + out = await review_brief_build._tests_section( + calls, inp, [], [], [], paths=["tests/unit/test_x.py"], diff="" + ) + assert [r["kind"] for r in out["rows"]] == ["run_first"] + assert out["epistemic"] == "lower_bound" and out["verdict"] == "no_subjects" + assert next(b for op, b in calls.calls if op == "code.test_gate")["diff"] == "" + + @pytest.mark.asyncio async def test_a_compiler_tier_name_past_a_field_limit_is_refused_not_failed() -> None: """The brief's op seam builds each op's input model: a name past `symbol`'s 300 diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index 34ba0451fe8..12e1e38ac4e 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -264,6 +264,20 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: # with the list shorter than the must-run tests, the rest are counted one = tg.run_first(rows, tests_total=7, must_run_total=4, tests_unnamed=0, limit=1) assert one["left_out"] == 5 and one["must_run_left_out"] == 2 + # a gate cut by its own limit: the rows it did not return are left out too + cut = tg.run_first(rows[:2], tests_total=7, must_run_total=4, tests_unnamed=3, limit=3) + assert len(cut["tests"]) == 1 and cut["left_out"] == 8 and cut["must_run_left_out"] == 2 + # a row joins the test of its run command before a test of its name + order = [ + _gate_row("tests/cases/a.expected", "changed_test", True), + {**_gate_row("tests/cases/b.sh", "coverage", True), "run": "run-cases tests/cases"}, + {**_gate_row("tests/cases/a.sh", "static_call", True), "run": "run-cases tests/cases"}, + ] + grouped = tg.run_first(order, tests_total=3, must_run_total=3, tests_unnamed=0)["tests"] + assert [t.get("test_paths", [t["test_path"]]) for t in grouped] == [ + ["tests/cases/a.expected"], + ["tests/cases/b.sh", "tests/cases/a.sh"], + ] # two tests of one name with different commands stay two pair = [ _gate_row("web/a.test.ts", "coverage", True), From 3eeb504f7f06645618ff7af8cb759a07549b32fc Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 06:14:31 +0000 Subject: [PATCH 04/29] The brief groups the gate's first 200 rows and a command-less file joins its script's test wherever it stands Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 18 +++-- server/code/app/index/review_brief_build.py | 3 +- server/code/domain/index/test_gate.py | 74 ++++++++++--------- server/transport/mcp_descriptions.py | 4 +- tests/unit/test_code_review_brief_sections.py | 10 ++- tests/unit/test_code_test_gate.py | 17 ++++- 6 files changed, 73 insertions(+), 53 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 11a8ec39bd3..51c7f03cca1 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -412,14 +412,16 @@ section's `note` gives both counts. The first `tests` row, `kind: "run_first"`, names the tests to run first: the first five tests `code.test_gate` lists for the PR's diff, in its order, so every test that says `must_run` comes before a lead and the strongest evidence comes first. Files that share a -run command are one test, and a file with no run command joins the test of its directory and -name up to the first dot (a case's expected output beside its script); files with different -run commands are never one test. A test of several files lists them in `test_paths`. Each -test carries its run command and the gate's `why`, `evidence` and `must_run` for its first -file. `left_out` counts at least the test files the gate holds under none of the five, the -leads it cut included, and `must_run_left_out` how many of them say `must_run`; -`code.test_gate` lists them all. The row is advice no verdict rests on: a read of the gate -that raised keeps the brief unstored, and a gate that holds no test leaves the row out. +run command are one test, and a file with no run command joins the first test that holds a +file of its directory and name up to the first dot (a case's expected output beside its +script); files with different run commands are never one test. A test of several files lists +them in `test_paths`. Each test carries its run command and the gate's `why`, `evidence` and +`must_run` for its first file. The brief reads the gate's first 200 rows. `left_out` counts +the test files the gate holds under none of the five, and `must_run_left_out` how many of +them say `must_run`. A file past those 200 rows and a lead the gate cut are counted whatever +their run command, and the gate's count of cut leads is a lower bound; `code.test_gate` +lists them. The row is advice no verdict rests on: a read of the gate that raised keeps the +brief unstored, and a gate that holds no test leaves the row out. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index a618b4ef491..b7100092826 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -64,7 +64,7 @@ from server.code.app.symbol.diff import _parse_unified_diff_full, is_blank from server.code.app.symbol.resolve import _parse_file_symbol from server.code.domain.index.epistemic import CAUSE_NEW_SIDE_FAILED, CAUSE_UNMAPPED -from server.code.domain.index.test_gate import is_test_path, run_first +from server.code.domain.index.test_gate import GATE_ROWS, is_test_path, run_first from server.observability import degradations logger = logging.getLogger(__name__) @@ -849,6 +849,7 @@ async def _run_first_row( "namespace": inp.ns, "files": paths[:IMPACT_SEED_FILES], # code.test_gate's cap too "diff": diff[:MAX_DIFF_CHARS], + "limit": GATE_ROWS, }, ) except Exception as e: # noqa: BLE001 — the tests to run first are advice diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index e89a3a4d794..6cfd7134c30 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -258,25 +258,33 @@ def _stem_key(path: str) -> str: return f"{p.parent}/{p.name.split('.', 1)[0]}" -def _test_groups(tests: list[dict[str, Any]]) -> list[dict[str, Any]]: - """The gate's rows as tests, in their order: rows that share a run command are one - test, and a row with none joins a test of its directory and name (`_stem_key`, a case's - expected output beside its script), as one with a command joins such a test that has - none yet. Rows with different commands are never one test.""" - groups: list[dict[str, Any]] = [] - for r in tests: - run, stem = str(r.get("run") or ""), _stem_key(str(r["test_path"])) - same_run = next((g for g in groups if run and g["run"] == run), None) - group = same_run or next( - (g for g in groups if stem in g["stems"] and not (run and g["run"])), None - ) - if group is None: - groups.append({"run": run, "stems": {stem}, "rows": [r]}) +def _test_groups(tests: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: + """The gate's rows as tests, each in the gate's order and the tests in the order of + their first rows: rows that share a run command are one test, and a row with none joins + the first test that holds a file of its directory and name (`_stem_key`, a case's + expected output beside its script), else the test of the command-less rows of that + name. Rows with different commands are never one test.""" + by_run: dict[str, list[int]] = {} + for i, r in enumerate(tests): + if r.get("run"): + by_run.setdefault(str(r["run"]), []).append(i) + groups = list(by_run.values()) + stems = [{_stem_key(str(tests[i]["test_path"])) for i in g} for g in groups] + for i, r in enumerate(tests): + if r.get("run"): continue - group["run"] = group["run"] or run - group["stems"].add(stem) - group["rows"].append(r) - return groups + stem = _stem_key(str(r["test_path"])) + at = next((k for k, held in enumerate(stems) if stem in held), None) + if at is None: + groups.append([i]) + stems.append({stem}) + else: + groups[at].append(i) + return [[tests[i] for i in sorted(g)] for g in sorted(groups, key=min)] + + +# not a setting: code.test_gate's `limit` maximum, the rows the brief groups +GATE_ROWS = 200 def run_first( @@ -290,28 +298,24 @@ def run_first( """The first `limit` tests of a gate's `tests` rows (`_test_groups`), in the gate's order: every `must_run` row first, the strongest evidence first. A test carries its first row's path, `why`, `evidence` and `must_run`, and its run command. `left_out` - counts the gate's test files under no listed test, the leads it cut included (at - least: `tests_unnamed` is a lower bound), and `must_run_left_out` those of them that - say `must_run`; `tests_total`, `must_run_total` - and `tests_unnamed` are the gate's counts of the rows `tests` was cut from.""" + counts the gate's test files under no listed test, and `must_run_left_out` those of + them that say `must_run`; `tests_total`, `must_run_total` and `tests_unnamed` are the + gate's counts of the rows `tests` was cut from. A row past `tests` and a lead the gate + cut are counted whatever their run command, and the cut leads are a lower bound.""" listed = _test_groups(tests)[:limit] - shown = [r for g in listed for r in g["rows"]] + shown = [r for rows in listed for r in rows] return { "tests": [ { - "test_path": g["rows"][0]["test_path"], - "run": g["run"], - "run_unknown": not g["run"], - "why": g["rows"][0]["why"], - "evidence": g["rows"][0].get("evidence") or "", - "must_run": bool(g["rows"][0].get("must_run")), - **( - {"test_paths": [r["test_path"] for r in g["rows"]]} - if len(g["rows"]) > 1 - else {} - ), + "test_path": rows[0]["test_path"], + "run": next((str(r["run"]) for r in rows if r.get("run")), ""), + "run_unknown": not any(r.get("run") for r in rows), + "why": rows[0]["why"], + "evidence": rows[0].get("evidence") or "", + "must_run": bool(rows[0].get("must_run")), + **({"test_paths": [r["test_path"] for r in rows]} if len(rows) > 1 else {}), } - for g in listed + for rows in listed ], "left_out": tests_total + tests_unnamed - len(shown), "must_run_left_out": must_run_total - sum(1 for r in shown if r.get("must_run")), diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index ace82092096..c05a628275b 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1659,8 +1659,8 @@ def revision_policy(update: str) -> str: "in `collapsed`), " "tests (first a run_first row: the first five tests code.test_gate " "lists for the diff, must_run ones first, one per test with its why and " - "evidence, and `left_out` / `must_run_left_out` counting at least the " - "test files under none of them, which code.test_gate lists in full; then " + "evidence, and `left_out` / `must_run_left_out` counting the test " + "files under none of them, which code.test_gate lists in full; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index cc807a15336..a3a8e46c7c1 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1089,15 +1089,19 @@ async def build(fail: set[str], error: type[Exception] = RuntimeError) -> dict[s @pytest.mark.asyncio async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> None: """A PR with no claim and no home still names its tests to run first; the row says - nothing of what a claim read, so the section stays `lower_bound` with no verdict.""" + nothing of what a claim read, so the section stays `lower_bound` with no verdict. An + open PR's diff reaches the gate as written.""" calls = _Calls() inp = dataclasses.replace(_inputs(), open=True, head_sha="a" * 40) + diff = "--- a/tests/unit/test_x.py\n+++ b/tests/unit/test_x.py\n@@ -1 +1 @@\n-a\n+b\n" out = await review_brief_build._tests_section( - calls, inp, [], [], [], paths=["tests/unit/test_x.py"], diff="" + calls, inp, [], [], [], paths=["tests/unit/test_x.py"], diff=diff ) assert [r["kind"] for r in out["rows"]] == ["run_first"] assert out["epistemic"] == "lower_bound" and out["verdict"] == "no_subjects" - assert next(b for op, b in calls.calls if op == "code.test_gate")["diff"] == "" + # the gate reads the diff it is handed (an open PR's as written) and all its rows + body = next(b for op, b in calls.calls if op == "code.test_gate") + assert body["diff"] == diff and body["limit"] == 200 @pytest.mark.asyncio diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index 12e1e38ac4e..7215293c608 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -267,17 +267,26 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: # a gate cut by its own limit: the rows it did not return are left out too cut = tg.run_first(rows[:2], tests_total=7, must_run_total=4, tests_unnamed=3, limit=3) assert len(cut["tests"]) == 1 and cut["left_out"] == 8 and cut["must_run_left_out"] == 2 - # a row joins the test of its run command before a test of its name + # a command-less row joins the test that runs a file of its name, wherever that file + # stands, and a row joins the test of its run command before one of its name order = [ _gate_row("tests/cases/a.expected", "changed_test", True), {**_gate_row("tests/cases/b.sh", "coverage", True), "run": "run-cases tests/cases"}, {**_gate_row("tests/cases/a.sh", "static_call", True), "run": "run-cases tests/cases"}, ] grouped = tg.run_first(order, tests_total=3, must_run_total=3, tests_unnamed=0)["tests"] - assert [t.get("test_paths", [t["test_path"]]) for t in grouped] == [ - ["tests/cases/a.expected"], - ["tests/cases/b.sh", "tests/cases/a.sh"], + assert [(t["test_paths"], t["run"], t["why"]) for t in grouped] == [ + ( + ["tests/cases/a.expected", "tests/cases/b.sh", "tests/cases/a.sh"], + "run-cases tests/cases", + "changed_test", + ) ] + # a row past the gate's `tests` is counted whatever its command: the gate's counts are + # all the brief has of it + pkg = [_gate_row(f"pkg/a/f{i}_test.go", "changed_test", True) for i in range(3)] + past = tg.run_first(pkg, tests_total=5, must_run_total=5, tests_unnamed=0) + assert len(past["tests"]) == 1 and past["left_out"] == 2 and past["must_run_left_out"] == 2 # two tests of one name with different commands stay two pair = [ _gate_row("web/a.test.ts", "coverage", True), From a5c80515f6cb244627368608b6c214af8653601c Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 06:27:45 +0000 Subject: [PATCH 05/29] The run_first row caps a test's paths, says when the gate read a cut change, and its docs say what code.test_gate lists Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 20 +++--- server/code/app/index/review_brief_build.py | 9 ++- server/code/domain/index/test_gate.py | 14 ++++- server/transport/mcp_descriptions.py | 3 +- tests/unit/test_code_review_brief_sections.py | 62 ++++++++++++------- tests/unit/test_code_test_gate.py | 14 +++++ 6 files changed, 88 insertions(+), 34 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 51c7f03cca1..52f00f6ca01 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -414,14 +414,18 @@ tests `code.test_gate` lists for the PR's diff, in its order, so every test that `must_run` comes before a lead and the strongest evidence comes first. Files that share a run command are one test, and a file with no run command joins the first test that holds a file of its directory and name up to the first dot (a case's expected output beside its -script); files with different run commands are never one test. A test of several files lists -them in `test_paths`. Each test carries its run command and the gate's `why`, `evidence` and -`must_run` for its first file. The brief reads the gate's first 200 rows. `left_out` counts -the test files the gate holds under none of the five, and `must_run_left_out` how many of -them say `must_run`. A file past those 200 rows and a lead the gate cut are counted whatever -their run command, and the gate's count of cut leads is a lower bound; `code.test_gate` -lists them. The row is advice no verdict rests on: a read of the gate that raised keeps the -brief unstored, and a gate that holds no test leaves the row out. +script); files with different run commands are never one test. A test of several files names +its first five in `test_paths` and counts them in `test_paths_total`. Each test carries its +run command and the gate's `why`, `evidence` and `must_run` for its first file. The brief +reads the gate's first 200 rows. `left_out` counts the test files the gate holds under none +of the five, and `must_run_left_out` how many of them say `must_run`. A file past those 200 +rows and a lead the gate cut are counted whatever their run command, and the gate's count of +cut leads is a lower bound. `code.test_gate` lists up to 200 rows and counts the leads it +cuts. The row says `cut: true` when the gate read a cut change (the first 50 files, or the +first 200,000 characters of the diff) or held more rows than it returned: its counts are +then of what it read. The row is advice no verdict rests on: a read of the gate that raised +keeps the brief unstored, and a refused read or a gate that holds no test leaves the row +out. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index b7100092826..fce1cb54bd6 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -867,7 +867,14 @@ async def _run_first_row( must_run_total=int(gate.get("must_run_total") or 0), tests_unnamed=unnamed, ) - return {"kind": "run_first", **picked}, [] + # the gate read a cut change, or held more rows than it returned: the counts are of + # what it read + cut = ( + len(paths) > IMPACT_SEED_FILES + or len(diff) > MAX_DIFF_CHARS + or bool(gate.get("tests_capped")) + ) + return {"kind": "run_first", **picked, **({"cut": True} if cut else {})}, [] async def _settings_section( diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index 6cfd7134c30..33411fa70b4 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -285,6 +285,8 @@ def _test_groups(tests: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: # not a setting: code.test_gate's `limit` maximum, the rows the brief groups GATE_ROWS = 200 +# Paths a listed test names in `test_paths`: one run command can hold a whole directory. +_PATHS_SHOWN = 5 def run_first( @@ -297,7 +299,8 @@ def run_first( ) -> dict[str, Any]: """The first `limit` tests of a gate's `tests` rows (`_test_groups`), in the gate's order: every `must_run` row first, the strongest evidence first. A test carries its - first row's path, `why`, `evidence` and `must_run`, and its run command. `left_out` + first row's path, `why`, `evidence` and `must_run`, and its run command; a test of + several files names the first `_PATHS_SHOWN` and counts them all. `left_out` counts the gate's test files under no listed test, and `must_run_left_out` those of them that say `must_run`; `tests_total`, `must_run_total` and `tests_unnamed` are the gate's counts of the rows `tests` was cut from. A row past `tests` and a lead the gate @@ -313,7 +316,14 @@ def run_first( "why": rows[0]["why"], "evidence": rows[0].get("evidence") or "", "must_run": bool(rows[0].get("must_run")), - **({"test_paths": [r["test_path"] for r in rows]} if len(rows) > 1 else {}), + **( + { + "test_paths": [r["test_path"] for r in rows[:_PATHS_SHOWN]], + "test_paths_total": len(rows), + } + if len(rows) > 1 + else {} + ), } for rows in listed ], diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index c05a628275b..0456dd7e4b4 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1660,7 +1660,8 @@ def revision_policy(update: str) -> str: "tests (first a run_first row: the first five tests code.test_gate " "lists for the diff, must_run ones first, one per test with its why and " "evidence, and `left_out` / `must_run_left_out` counting the test " - "files under none of them, which code.test_gate lists in full; then " + "files under none of them (code.test_gate lists up to 200 and counts " + "the leads it cuts); then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index a3a8e46c7c1..808cb331b70 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -584,6 +584,8 @@ async def __call__(self, op: str, body: dict[str, Any]) -> dict[str, Any]: "epistemic": "lower_bound", } if op == "code.test_gate": + # the gate's shape: the must_run row first, four leads, six more cut + lead = {"why": "test_map", "evidence": "test map", "run_unknown": False} return { "tests": [ { @@ -595,17 +597,25 @@ async def __call__(self, op: str, body: dict[str, Any]) -> dict[str, Any]: "must_run": True, }, { - "test_path": "tests/integration/test_parts/__init__.py", - "why": "test_map", - "evidence": "test map (pr_cooccurrence), confidence 0.70", + **lead, + "test_path": "tests/integration/test_parts/configs/parts.xml", "run": "pytest tests/integration/test_parts", - "run_unknown": False, "must_run": False, }, + *( + { + **lead, + "test_path": f"tests/unit/test_{n}.py", + "run": f"pytest tests/unit/test_{n}.py", + "must_run": False, + } + for n in ("a", "b", "c") + ), ], - "tests_total": 2, + "tests_total": 5, "must_run_total": 1, "tests_unnamed": 6, + "tests_capped": False, } if op == "code.test_home": return { @@ -808,25 +818,28 @@ async def test_build_assembles_every_section_with_verdicts_and_decisions() -> No # tests: the tests to run first, a claim per symbol and per setting, then the home; # Settings::dump is uncovered first = secs["tests"]["rows"][0] - assert first == { - "kind": "run_first", - "tests": [ - { - "test_path": "tests/integration/test_parts/test.py", - "run": "pytest tests/integration/test_parts", - "run_unknown": False, - "why": "static_call", - "evidence": "test_load reaches the change in 1 hop(s)", - "must_run": True, - "test_paths": [ - "tests/integration/test_parts/test.py", - "tests/integration/test_parts/__init__.py", - ], - } + assert first["kind"] == "run_first" and "cut" not in first + # the integration test's config is under its test: four tests, the six cut leads left out + assert [t["test_path"] for t in first["tests"]] == [ + "tests/integration/test_parts/test.py", + "tests/unit/test_a.py", + "tests/unit/test_b.py", + "tests/unit/test_c.py", + ] + assert first["tests"][0] == { + "test_path": "tests/integration/test_parts/test.py", + "run": "pytest tests/integration/test_parts", + "run_unknown": False, + "why": "static_call", + "evidence": "test_load reaches the change in 1 hop(s)", + "must_run": True, + "test_paths": [ + "tests/integration/test_parts/test.py", + "tests/integration/test_parts/configs/parts.xml", ], - "left_out": 6, - "must_run_left_out": 0, + "test_paths_total": 2, } + assert first["left_out"] == 6 and first["must_run_left_out"] == 0 gate_body = next(b for op, b in calls.calls if op == "code.test_gate") # read from the merge commit, which the index holds, as impact reads it assert ( @@ -1102,6 +1115,11 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> # the gate reads the diff it is handed (an open PR's as written) and all its rows body = next(b for op, b in calls.calls if op == "code.test_gate") assert body["diff"] == diff and body["limit"] == 200 + assert "cut" not in out["rows"][0] + # a change past the gate's file cap is read cut, and the row says so + wide = [f"tests/unit/test_{i}.py" for i in range(51)] + cut = await review_brief_build._tests_section(calls, inp, [], [], [], paths=wide, diff=diff) + assert cut["rows"][0]["cut"] is True @pytest.mark.asyncio diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index 7215293c608..2549d6e8bb7 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -252,6 +252,7 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: ] # the expected output's test is run by its script's command assert out["tests"][0]["test_paths"] == ["tests/cases/a.expected", "tests/cases/a.sh"] + assert out["tests"][0]["test_paths_total"] == 2 assert out["tests"][0]["run"] == "sh tests/cases/a.sh" and not out["tests"][0]["run_unknown"] assert out["tests"][2]["run"] == "go test ./pkg/store/" and out["tests"][2]["test_paths"] == [ "pkg/store/store_test.go", @@ -287,6 +288,19 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: pkg = [_gate_row(f"pkg/a/f{i}_test.go", "changed_test", True) for i in range(3)] past = tg.run_first(pkg, tests_total=5, must_run_total=5, tests_unnamed=0) assert len(past["tests"]) == 1 and past["left_out"] == 2 and past["must_run_left_out"] == 2 + # command-less files of one name with no test of a command are one test of their own + pair = [ + _gate_row("tests/cases/c.in", "test_map", False), + _gate_row("tests/cases/c.out", "test_map", False), + ] + alone = tg.run_first(pair, tests_total=2, must_run_total=0, tests_unnamed=0)["tests"] + assert [(t["test_paths"], t["run_unknown"]) for t in alone] == [ + (["tests/cases/c.in", "tests/cases/c.out"], True) + ] + # a test of many files names the first five and counts them all + many = [_gate_row(f"pkg/b/f{i}_test.go", "coverage", True) for i in range(7)] + one_test = tg.run_first(many, tests_total=7, must_run_total=7, tests_unnamed=0)["tests"] + assert len(one_test[0]["test_paths"]) == 5 and one_test[0]["test_paths_total"] == 7 # two tests of one name with different commands stay two pair = [ _gate_row("web/a.test.ts", "coverage", True), From 4abe1d5caf0a26e875a26ceb7909c5c97c18995c Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 06:34:38 +0000 Subject: [PATCH 06/29] The run_first row says cut only for a change the gate did not read in full, and every cut is tested GATE_ROWS sits beside the brief's other caps of the op inputs it mirrors. A gate that lists no test holds none, so the row needs no cut-leads arm. Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 10 +++++----- server/code/app/index/review_brief.py | 5 +++++ server/code/app/index/review_brief_build.py | 18 ++++++++++------- server/code/domain/index/test_gate.py | 2 -- tests/unit/test_code_review_brief_sections.py | 20 ++++++++++++++++--- tests/unit/test_code_test_gate.py | 5 ----- 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 52f00f6ca01..850c1ac447e 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -421,11 +421,11 @@ reads the gate's first 200 rows. `left_out` counts the test files the gate holds of the five, and `must_run_left_out` how many of them say `must_run`. A file past those 200 rows and a lead the gate cut are counted whatever their run command, and the gate's count of cut leads is a lower bound. `code.test_gate` lists up to 200 rows and counts the leads it -cuts. The row says `cut: true` when the gate read a cut change (the first 50 files, or the -first 200,000 characters of the diff) or held more rows than it returned: its counts are -then of what it read. The row is advice no verdict rests on: a read of the gate that raised -keeps the brief unstored, and a refused read or a gate that holds no test leaves the row -out. +cuts. The row says `cut: true` when the gate read a cut change or held more rows than it +returned: a changed file in neither the 50 files it is sent nor the first 200,000 characters +of the diff, a diff longer than that, or more than 200 changed files. Its counts are then of +what it read. The row is advice no verdict rests on: a read of the gate that raised keeps +the brief unstored, and a refused read or a gate that holds no test leaves the row out. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index 8ebb14d8967..aa22530ed48 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -134,6 +134,10 @@ # The changed files the brief's impact read seeds from. # not a setting: CodeImpactInput.files' max_length IMPACT_SEED_FILES = 50 +# The rows of code.test_gate the tests to run first are picked from, and the changed +# paths it reads. +# not a setting: CodeTestGateInput.limit's maximum, and the gate's own path cut +GATE_ROWS = 200 # The paths section stores the first this many changed paths as rows (impact's # `paths_seen` cut); `rows_total` counts them all, and its verdict and causes read all. # not a setting: it bounds a stored row list @@ -1193,6 +1197,7 @@ def serve( "DRIFTED", "EMPTY", "FAILED", + "GATE_ROWS", "IMPACT_SEED_FILES", "INDEX_ERROR", "NOT_BOUND", diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index fce1cb54bd6..3f9ede0cbfe 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -15,6 +15,7 @@ DRIFTED, EMPTY, FAILED, + GATE_ROWS, IMPACT_SEED_FILES, INDEX_ERROR, MAX_COVERAGE_SETTINGS, @@ -64,7 +65,7 @@ from server.code.app.symbol.diff import _parse_unified_diff_full, is_blank from server.code.app.symbol.resolve import _parse_file_symbol from server.code.domain.index.epistemic import CAUSE_NEW_SIDE_FAILED, CAUSE_UNMAPPED -from server.code.domain.index.test_gate import GATE_ROWS, is_test_path, run_first +from server.code.domain.index.test_gate import is_test_path, run_first from server.observability import degradations logger = logging.getLogger(__name__) @@ -857,20 +858,23 @@ async def _run_first_row( return None, [] degradations.partial("code.review_brief", "test_gate_failed", e) return None, [failure_cause("test_gate", e)] + # a gate that cuts leads keeps four of them, so one that lists no test holds none tests = list(gate.get("tests") or []) - unnamed = int(gate.get("tests_unnamed") or 0) - if not (tests or unnamed): + if not tests: return None, [] picked = run_first( tests, tests_total=int(gate.get("tests_total") or 0), must_run_total=int(gate.get("must_run_total") or 0), - tests_unnamed=unnamed, + tests_unnamed=int(gate.get("tests_unnamed") or 0), ) - # the gate read a cut change, or held more rows than it returned: the counts are of - # what it read + # the gate read a cut change (a changed file in neither the files it was sent nor the + # diff it read, or past the 200 changed paths it reads), or held more rows than it + # returned: the counts are of what it read + read = {*paths[:IMPACT_SEED_FILES], *parse_diff(diff[:MAX_DIFF_CHARS]).paths} cut = ( - len(paths) > IMPACT_SEED_FILES + bool(set(paths) - read) + or len(read) > GATE_ROWS or len(diff) > MAX_DIFF_CHARS or bool(gate.get("tests_capped")) ) diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index 33411fa70b4..6cddb005fba 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -283,8 +283,6 @@ def _test_groups(tests: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: return [[tests[i] for i in sorted(g)] for g in sorted(groups, key=min)] -# not a setting: code.test_gate's `limit` maximum, the rows the brief groups -GATE_ROWS = 200 # Paths a listed test names in `test_paths`: one run command can hold a whole directory. _PATHS_SHOWN = 5 diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 808cb331b70..00802c9d828 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1116,10 +1116,24 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> body = next(b for op, b in calls.calls if op == "code.test_gate") assert body["diff"] == diff and body["limit"] == 200 assert "cut" not in out["rows"][0] - # a change past the gate's file cap is read cut, and the row says so + # a changed file past the gate's file cap and outside the diff, a diff past its cap and + # a gate that held more rows than it returned each say the counts are of a cut read wide = [f"tests/unit/test_{i}.py" for i in range(51)] - cut = await review_brief_build._tests_section(calls, inp, [], [], [], paths=wide, diff=diff) - assert cut["rows"][0]["cut"] is True + long_diff = diff + "+" * rb.MAX_DIFF_CHARS + for kw in ({"paths": wide, "diff": diff}, {"paths": [], "diff": long_diff}): + sec = await review_brief_build._tests_section(calls, inp, [], [], [], **kw) + assert sec["rows"][0]["cut"] is True + + async def capped(op: str, body: dict[str, Any]) -> dict[str, Any]: + return {**await calls(op, body), "tests_capped": True} + + sec = await review_brief_build._tests_section(capped, inp, [], [], [], paths=[], diff=diff) + assert sec["rows"][0]["cut"] is True + # a file past the cap whose hunks the diff carries is read + covered = await review_brief_build._tests_section( + calls, inp, [], [], [], paths=[*wide[:50], "tests/unit/test_x.py"], diff=diff + ) + assert "cut" not in covered["rows"][0] @pytest.mark.asyncio diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index 2549d6e8bb7..c6d434b550c 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -307,11 +307,6 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: _gate_row("web/a.spec.ts", "coverage", True), ] assert len(tg.run_first(pair, tests_total=2, must_run_total=2, tests_unnamed=0)["tests"]) == 2 - assert tg.run_first([], tests_total=0, must_run_total=0, tests_unnamed=4) == { - "tests": [], - "left_out": 4, - "must_run_left_out": 0, - } def test_a_one_hop_row_whose_test_uses_no_touched_definition_is_a_lead() -> None: From 45ea1537115b114ff05778c37aa299f95ad87844 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 06:44:03 +0000 Subject: [PATCH 07/29] The run_first row reads cut off the files the gate read and its truncated cause Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 16 +++++---- server/code/app/index/review_brief.py | 5 ++- server/code/app/index/review_brief_build.py | 25 +++++++------ tests/unit/test_code_review_brief_sections.py | 35 +++++++++++++++---- 4 files changed, 53 insertions(+), 28 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 850c1ac447e..d917cd06107 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -281,8 +281,8 @@ serve it (the trivial tier, or `sections` without it) answers `gaps`, `unsettled `rationale:not_requested`, or `no_source_change`: `covered_no_gaps` needs `rationale` in `sections` (or `sections` empty) on the standard or full tier. A brief one of whose reads raised is served and not stored, and the next read builds it again (the test home -is advice: a read of it that raised or was refused decides no verdict, though one that -raised still keeps the brief unstored); so is an open PR's brief built without its diff, +and the tests to run first are advice: a read of one that raised or was refused decides no +verdict, though one that raised still keeps the brief unstored); so is an open PR's brief built without its diff, or from an overlay that was off (`overlay:error` when git could not read its diff), left a changed file unreached when its parse time was spent or its parse could not run here, or parsed another head. A parse of a file that raised or ran past its limits does the @@ -421,11 +421,13 @@ reads the gate's first 200 rows. `left_out` counts the test files the gate holds of the five, and `must_run_left_out` how many of them say `must_run`. A file past those 200 rows and a lead the gate cut are counted whatever their run command, and the gate's count of cut leads is a lower bound. `code.test_gate` lists up to 200 rows and counts the leads it -cuts. The row says `cut: true` when the gate read a cut change or held more rows than it -returned: a changed file in neither the 50 files it is sent nor the first 200,000 characters -of the diff, a diff longer than that, or more than 200 changed files. Its counts are then of -what it read. The row is advice no verdict rests on: a read of the gate that raised keeps -the brief unstored, and a refused read or a gate that holds no test leaves the row out. +cuts. The row says `cut: true` when the gate did not read the whole change or all its tests: +a changed file is missing from the gate's `changed_files` (past the 50 files it is sent and +outside the first 200,000 characters of the diff, or past the 200 changed paths it reads), +the diff is longer than that, the gate's causes say `truncated` (its call-graph walk or +coverage read was cut), or it held more rows than it returned. Its counts are then of what +it read. The row is advice no verdict rests on: a read of the gate that raised keeps the +brief unstored, and a refused read or a gate that holds no test leaves the row out. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index aa22530ed48..2a1192e2cbb 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -134,9 +134,8 @@ # The changed files the brief's impact read seeds from. # not a setting: CodeImpactInput.files' max_length IMPACT_SEED_FILES = 50 -# The rows of code.test_gate the tests to run first are picked from, and the changed -# paths it reads. -# not a setting: CodeTestGateInput.limit's maximum, and the gate's own path cut +# The rows of code.test_gate the tests to run first are picked from. +# not a setting: CodeTestGateInput.limit's maximum GATE_ROWS = 200 # The paths section stores the first this many changed paths as rows (impact's # `paths_seen` cut); `rows_total` counts them all, and its verdict and causes read all. diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 3f9ede0cbfe..07c0ce042c9 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -64,7 +64,11 @@ ) from server.code.app.symbol.diff import _parse_unified_diff_full, is_blank from server.code.app.symbol.resolve import _parse_file_symbol -from server.code.domain.index.epistemic import CAUSE_NEW_SIDE_FAILED, CAUSE_UNMAPPED +from server.code.domain.index.epistemic import ( + CAUSE_NEW_SIDE_FAILED, + CAUSE_TRUNCATED, + CAUSE_UNMAPPED, +) from server.code.domain.index.test_gate import is_test_path, run_first from server.observability import degradations @@ -814,11 +818,12 @@ async def _tests_section( "next_free_stateless_number": int(res.get("next_free_stateless_number") or 0), } ) - # the advice rows say nothing of what the claims read: a section of the home alone is - # exact, one of the tests to run first alone is not + # the tests to run first say nothing of what the claims read: a section of the home + # alone is exact, one of the tests to run first alone is not claims = [r for r in rows if r.get("kind") in ("symbol", "setting")] - others = [r for r in rows if r.get("kind") != "run_first"] - if others and all(r.get("epistemic") == "exact" for r in claims): + if any(r.get("kind") != "run_first" for r in rows) and all( + r.get("epistemic") == "exact" for r in claims + ): epistemic = "exact" out = section( SERVED, @@ -868,14 +873,12 @@ async def _run_first_row( must_run_total=int(gate.get("must_run_total") or 0), tests_unnamed=int(gate.get("tests_unnamed") or 0), ) - # the gate read a cut change (a changed file in neither the files it was sent nor the - # diff it read, or past the 200 changed paths it reads), or held more rows than it - # returned: the counts are of what it read - read = {*paths[:IMPACT_SEED_FILES], *parse_diff(diff[:MAX_DIFF_CHARS]).paths} + # the gate read a cut change (a changed file it did not read), cut its own walk or + # coverage read, or held more rows than it returned: the counts are of what it read cut = ( - bool(set(paths) - read) - or len(read) > GATE_ROWS + bool(set(paths) - set(gate.get("changed_files") or [])) or len(diff) > MAX_DIFF_CHARS + or CAUSE_TRUNCATED in (gate.get("causes") or []) or bool(gate.get("tests_capped")) ) return {"kind": "run_first", **picked, **({"cut": True} if cut else {})}, [] diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 00802c9d828..7bef965c6d6 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -9,6 +9,7 @@ import asyncio import dataclasses import json +import re from datetime import UTC, datetime from typing import Any, ClassVar @@ -616,6 +617,16 @@ async def __call__(self, op: str, body: dict[str, Any]) -> dict[str, Any]: "must_run_total": 1, "tests_unnamed": 6, "tests_capped": False, + # as the gate reads them: the files sent, then the files the diff changes + # on its new side (a deleted file has none) + "changed_files": list( + dict.fromkeys( + [ + *body["files"], + *re.findall(r"^\+\+\+ b/(\S+)$", body["diff"], flags=re.M), + ] + ) + )[:200], } if op == "code.test_home": return { @@ -1116,19 +1127,29 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> body = next(b for op, b in calls.calls if op == "code.test_gate") assert body["diff"] == diff and body["limit"] == 200 assert "cut" not in out["rows"][0] - # a changed file past the gate's file cap and outside the diff, a diff past its cap and - # a gate that held more rows than it returned each say the counts are of a cut read + # a changed file the gate did not read (past its file cap and outside the diff, a file + # the diff deletes there, or past the 200 paths it reads), a diff past its cap, a gate that cut its own walk and + # one that held more rows than it returned each say the counts are of a cut read wide = [f"tests/unit/test_{i}.py" for i in range(51)] + gone = "--- a/src/a.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-x\n" long_diff = diff + "+" * rb.MAX_DIFF_CHARS - for kw in ({"paths": wide, "diff": diff}, {"paths": [], "diff": long_diff}): + many = [f"tests/unit/test_{i}.py" for i in range(201)] + every = "".join(f"--- a/{p}\n+++ b/{p}\n@@ -1 +1 @@\n-a\n+b\n" for p in many) + for kw in ( + {"paths": wide, "diff": diff}, + {"paths": many, "diff": every}, + {"paths": [*wide[:50], "src/a.py"], "diff": diff + gone}, + {"paths": [], "diff": long_diff}, + ): sec = await review_brief_build._tests_section(calls, inp, [], [], [], **kw) assert sec["rows"][0]["cut"] is True + for extra in ({"tests_capped": True}, {"causes": ["truncated"]}): - async def capped(op: str, body: dict[str, Any]) -> dict[str, Any]: - return {**await calls(op, body), "tests_capped": True} + async def answer(op: str, body: dict[str, Any], extra: dict[str, Any] = extra) -> Any: + return {**await calls(op, body), **extra} - sec = await review_brief_build._tests_section(capped, inp, [], [], [], paths=[], diff=diff) - assert sec["rows"][0]["cut"] is True + sec = await review_brief_build._tests_section(answer, inp, [], [], [], paths=[], diff=diff) + assert sec["rows"][0]["cut"] is True # a file past the cap whose hunks the diff carries is read covered = await review_brief_build._tests_section( calls, inp, [], [], [], paths=[*wide[:50], "tests/unit/test_x.py"], diff=diff From 8e5aa7a6d3ab81c34d8df7e9b61df48fb40f3d51 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 06:58:57 +0000 Subject: [PATCH 08/29] The run_first row leaves out the test files the PR deletes and keeps the count of cut leads when the gate names none Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 57 ++++++++++--------- server/code/app/index/review_brief_build.py | 28 +++++---- server/transport/mcp_descriptions.py | 3 +- tests/unit/test_code_review_brief_sections.py | 32 ++++++++++- 4 files changed, 79 insertions(+), 41 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index d917cd06107..a44d6b7f4ef 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -270,27 +270,27 @@ values are tried in this order: - `""`: no brief was found (`source` is `missing`). The brief caps its fan-out: coverage claims for the first six touched functions and four -settings, regression records for the first six touched definitions (a name in two files -is two), facts for the first eight settings, and the first 30 touched functions. Each -cap that leaves a subject out is a cause of its section, so the verdict stays -`unsettled`; so is a subject an op refuses to read, such as a compiler-tier name past a -field's 300 characters, since every build refuses it the same way. The paths section -stores its first 200 changed paths as rows; its `rows_total`, verdict and causes count -every changed path. The `rationale` section is never stored, so a read that does not -serve it (the trivial tier, or `sections` without it) answers `gaps`, `unsettled` with -`rationale:not_requested`, or `no_source_change`: `covered_no_gaps` needs `rationale` in -`sections` (or `sections` empty) on the standard or full tier. A brief one of whose -reads raised is served and not stored, and the next read builds it again (the test home -and the tests to run first are advice: a read of one that raised or was refused decides no -verdict, though one that raised still keeps the brief unstored); so is an open PR's brief built without its diff, -or from an overlay that was off (`overlay:error` when git could not read its diff), left -a changed file unreached when its parse time was spent or its parse could not run here, -or parsed another head. A parse of a file that raised or ran past its limits does the -same on every build of that file, at an open PR's head and at a merged PR's parent -alike, so that brief is stored, `unsettled`: `code.impact` names `new_side_unread` for -the file, and an open PR's `overlay_note` counts it. A stored brief is served as it was -built: one of an older payload version (`v` in the payload) records no cap or failed -read, so its verdict is `gaps` or `unsettled`. +settings, regression records for the first six touched definitions (a name in two files is +two), facts for the first eight settings, and the first 30 touched functions. Each cap that +leaves a subject out is a cause of its section, so the verdict stays `unsettled`; so is a +subject an op refuses to read, such as a compiler-tier name past a field's 300 characters, +since every build refuses it the same way. The paths section stores its first 200 changed +paths as rows; its `rows_total`, verdict and causes count every changed path. The +`rationale` section is never stored, so a read that does not serve it (the trivial tier, or +`sections` without it) answers `gaps`, `unsettled` with `rationale:not_requested`, or +`no_source_change`: `covered_no_gaps` needs `rationale` in `sections` (or `sections` empty) +on the standard or full tier. A brief one of whose reads raised is served and not stored, +and the next read builds it again (the test home and the tests to run first are advice: a +read of one that raised or was refused decides no verdict, though one that raised still +keeps the brief unstored); so is an open PR's brief built without its diff, or from an +overlay that was off (`overlay:error` when git could not read its diff), left a changed file +unreached when its parse time was spent or its parse could not run here, or parsed another +head. A parse of a file that raised or ran past its limits does the same on every build of +that file, at an open PR's head and at a merged PR's parent alike, so that brief is stored, +`unsettled`: `code.impact` names `new_side_unread` for the file, and an open PR's +`overlay_note` counts it. A stored brief is served as it was built: one of an older payload +version (`v` in the payload) records no cap or failed read, so its verdict is `gaps` or +`unsettled`. `code.impact` maps a diff on its old side onto the index rows, which are at the indexed head (at the merge commit when the ingest builds a merged PR's brief, before it moves @@ -422,12 +422,15 @@ of the five, and `must_run_left_out` how many of them say `must_run`. A file pas rows and a lead the gate cut are counted whatever their run command, and the gate's count of cut leads is a lower bound. `code.test_gate` lists up to 200 rows and counts the leads it cuts. The row says `cut: true` when the gate did not read the whole change or all its tests: -a changed file is missing from the gate's `changed_files` (past the 50 files it is sent and -outside the first 200,000 characters of the diff, or past the 200 changed paths it reads), -the diff is longer than that, the gate's causes say `truncated` (its call-graph walk or -coverage read was cut), or it held more rows than it returned. Its counts are then of what -it read. The row is advice no verdict rests on: a read of the gate that raised keeps the -brief unstored, and a refused read or a gate that holds no test leaves the row out. +a changed file is missing from the gate's `changed_files` (one it was not sent, past the +first 50, whose hunks the diff it reads does not carry, or one past the 200 changed paths it +reads), the diff is longer than that, the gate's causes say `truncated` (its call-graph walk +or coverage read was cut), or it held more rows than it returned. Its counts are then of +what it read. A test file the PR deletes is no test to run: its row is neither listed nor +counted. The row is advice no verdict rests on: a read of the gate that raised keeps the +brief unstored, and a refused read, or a gate that lists and counts no test, leaves the row +out. A gate that names no test but counts the leads it cut gives a row with no tests and +their count in `left_out`. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 07c0ce042c9..266a3267bc5 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -841,10 +841,12 @@ async def _run_first_row( call: CallOp, inp: BriefInputs, paths: list[str], diff: str ) -> tuple[dict[str, Any] | None, list[str]]: """(the `run_first` row of the tests section, the read of it that raised): the first - tests code.test_gate lists for the PR's diff (`run_first`), None when it holds none. The - gate reads a diff's old side as the revision the index holds, so `diff` is the one - code.impact reads: a merged PR's reversed, an open PR's as written. A read that raised - only keeps the brief unstored, and a refused one leaves the row out.""" + tests code.test_gate lists for the PR's diff (`run_first`), None when it lists and + counts none. The gate reads a diff's old side as the revision the index holds, so + `diff` is the one code.impact reads: a merged PR's reversed, an open PR's as written. A + test file the PR deletes is no test to run, and its row is neither listed nor counted. + A read that raised only keeps the brief unstored, and a refused one leaves the row + out.""" if not (paths or diff): return None, [] try: @@ -863,15 +865,21 @@ async def _run_first_row( return None, [] degradations.partial("code.review_brief", "test_gate_failed", e) return None, [failure_cause("test_gate", e)] - # a gate that cuts leads keeps four of them, so one that lists no test holds none - tests = list(gate.get("tests") or []) - if not tests: + deleted = set(whole_files(inp.diff)) + every = list(gate.get("tests") or []) + tests = [t for t in every if t.get("test_path") not in deleted] + # a gate can list none and still count the leads it cut (file neighbours it could not + # tell apart): the row then names no test and counts them + unnamed = int(gate.get("tests_unnamed") or 0) + if not (tests or unnamed): return None, [] + gone = [t for t in every if t.get("test_path") in deleted] picked = run_first( tests, - tests_total=int(gate.get("tests_total") or 0), - must_run_total=int(gate.get("must_run_total") or 0), - tests_unnamed=int(gate.get("tests_unnamed") or 0), + tests_total=int(gate.get("tests_total") or 0) - len(gone), + must_run_total=int(gate.get("must_run_total") or 0) + - sum(1 for t in gone if t.get("must_run")), + tests_unnamed=unnamed, ) # the gate read a cut change (a changed file it did not read), cut its own walk or # coverage read, or held more rows than it returned: the counts are of what it read diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index 0456dd7e4b4..728404f0353 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1661,7 +1661,8 @@ def revision_policy(update: str) -> str: "lists for the diff, must_run ones first, one per test with its why and " "evidence, and `left_out` / `must_run_left_out` counting the test " "files under none of them (code.test_gate lists up to 200 and counts " - "the leads it cuts); then " + "the leads it cuts), and `cut: true` when the gate did not read the " + "whole change, its counts then floors; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 7bef965c6d6..e6cd882e149 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -830,7 +830,8 @@ async def test_build_assembles_every_section_with_verdicts_and_decisions() -> No # Settings::dump is uncovered first = secs["tests"]["rows"][0] assert first["kind"] == "run_first" and "cut" not in first - # the integration test's config is under its test: four tests, the six cut leads left out + # the integration test's config is under its test: four tests, the six cut leads are + # left out assert [t["test_path"] for t in first["tests"]] == [ "tests/integration/test_parts/test.py", "tests/unit/test_a.py", @@ -1128,8 +1129,9 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> assert body["diff"] == diff and body["limit"] == 200 assert "cut" not in out["rows"][0] # a changed file the gate did not read (past its file cap and outside the diff, a file - # the diff deletes there, or past the 200 paths it reads), a diff past its cap, a gate that cut its own walk and - # one that held more rows than it returned each say the counts are of a cut read + # the diff deletes there, or past the 200 paths it reads), a diff past its cap, a gate + # that cut its own walk and one that held more rows than it returned each say the + # counts are of a cut read wide = [f"tests/unit/test_{i}.py" for i in range(51)] gone = "--- a/src/a.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-x\n" long_diff = diff + "+" * rb.MAX_DIFF_CHARS @@ -1150,6 +1152,30 @@ async def answer(op: str, body: dict[str, Any], extra: dict[str, Any] = extra) - sec = await review_brief_build._tests_section(answer, inp, [], [], [], paths=[], diff=diff) assert sec["rows"][0]["cut"] is True + # a test file the PR deletes is neither listed nor counted + deleting = "--- a/tests/unit/test_x.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-a\n" + + async def with_deleted(op: str, body: dict[str, Any]) -> Any: + out = await calls(op, body) + gone = {"test_path": "tests/unit/test_x.py", "why": "changed_test", "must_run": True} + return {**out, "tests": [gone, *out["tests"]], "tests_total": 6, "must_run_total": 2} + + sec = await review_brief_build._tests_section( + with_deleted, dataclasses.replace(inp, diff=deleting), [], [], [], paths=[], diff=diff + ) + row = sec["rows"][0] + assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] + assert row["left_out"] == 6 and row["must_run_left_out"] == 0 + + # a gate that names no test but counts the leads it cut gives a row of the count + async def unnamed_only(op: str, body: dict[str, Any]) -> Any: + out = await calls(op, body) + return {**out, "tests": [], "tests_total": 0, "must_run_total": 0, "tests_unnamed": 7} + + sec = await review_brief_build._tests_section( + unnamed_only, inp, [], [], [], paths=[], diff=diff + ) + assert sec["rows"][0]["tests"] == [] and sec["rows"][0]["left_out"] == 7 # a file past the cap whose hunks the diff carries is read covered = await review_brief_build._tests_section( calls, inp, [], [], [], paths=[*wide[:50], "tests/unit/test_x.py"], diff=diff From 9ffe7e203a86e27e9374d7f89955909f5856cbe0 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 07:14:09 +0000 Subject: [PATCH 09/29] The run_first row treats a deletion with no diff sides as deleted and counts the tests left when every row it read is deleted Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 12 +++++------ server/code/app/index/review_brief_build.py | 21 +++++++++++++------ tests/unit/test_code_review_brief_sections.py | 20 ++++++++++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index a44d6b7f4ef..1fe45f3a851 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -424,13 +424,13 @@ cut leads is a lower bound. `code.test_gate` lists up to 200 rows and counts the cuts. The row says `cut: true` when the gate did not read the whole change or all its tests: a changed file is missing from the gate's `changed_files` (one it was not sent, past the first 50, whose hunks the diff it reads does not carry, or one past the 200 changed paths it -reads), the diff is longer than that, the gate's causes say `truncated` (its call-graph walk -or coverage read was cut), or it held more rows than it returned. Its counts are then of -what it read. A test file the PR deletes is no test to run: its row is neither listed nor -counted. The row is advice no verdict rests on: a read of the gate that raised keeps the +reads), the diff is longer than 200,000 characters, the gate's causes say `truncated` (its +call-graph walk or coverage read was cut), or it held more rows than it returned. Its counts +are then of what it read. A test file the PR deletes is no test to run: its row is neither +listed nor counted. The row is advice no verdict rests on: a read of the gate that raised keeps the brief unstored, and a refused read, or a gate that lists and counts no test, leaves the row -out. A gate that names no test but counts the leads it cut gives a row with no tests and -their count in `left_out`. +out. A gate that names no test, or only deleted ones, but counts other tests or the leads +it cut gives a row with no tests and their count in `left_out`. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 266a3267bc5..380e2d9be5c 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -64,6 +64,7 @@ ) from server.code.app.symbol.diff import _parse_unified_diff_full, is_blank from server.code.app.symbol.resolve import _parse_file_symbol +from server.code.domain.index.conventions import parse_unified_diff from server.code.domain.index.epistemic import ( CAUSE_NEW_SIDE_FAILED, CAUSE_TRUNCATED, @@ -865,18 +866,19 @@ async def _run_first_row( return None, [] degradations.partial("code.review_brief", "test_gate_failed", e) return None, [failure_cause("test_gate", e)] - deleted = set(whole_files(inp.diff)) + deleted = _deleted_paths(inp.diff) every = list(gate.get("tests") or []) tests = [t for t in every if t.get("test_path") not in deleted] - # a gate can list none and still count the leads it cut (file neighbours it could not - # tell apart): the row then names no test and counts them + gone = [t for t in every if t.get("test_path") in deleted] + total = int(gate.get("tests_total") or 0) - len(gone) + # a gate can list none (or only deleted files) and still count tests past its rows or + # the leads it cut: the row then names no test and counts them unnamed = int(gate.get("tests_unnamed") or 0) - if not (tests or unnamed): + if not (tests or unnamed or total): return None, [] - gone = [t for t in every if t.get("test_path") in deleted] picked = run_first( tests, - tests_total=int(gate.get("tests_total") or 0) - len(gone), + tests_total=total, must_run_total=int(gate.get("must_run_total") or 0) - sum(1 for t in gone if t.get("must_run")), tests_unnamed=unnamed, @@ -892,6 +894,13 @@ async def _run_first_row( return {"kind": "run_first", **picked, **({"cut": True} if cut else {})}, [] +def _deleted_paths(diff: str) -> set[str]: + """The paths of the files a git diff deletes: a `+++ /dev/null` side, or a `deleted + file mode` line for a deletion git prints with no sides (a binary or an empty file).""" + marked = {p for p, f in parse_unified_diff(diff).files.items() if f.status == "D"} + return marked | set(whole_files(diff)) + + async def _settings_section( call: CallOp, inp: BriefInputs, diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index e6cd882e149..04bf53026d7 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1166,6 +1166,26 @@ async def with_deleted(op: str, body: dict[str, Any]) -> Any: row = sec["rows"][0] assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] assert row["left_out"] == 6 and row["must_run_left_out"] == 0 + # git prints a deleted binary or empty file with no sides, only `deleted file mode` + bare = "diff --git a/tests/unit/test_x.py b/tests/unit/test_x.py\ndeleted file mode 100644\n" + sec = await review_brief_build._tests_section( + with_deleted, dataclasses.replace(inp, diff=bare), [], [], [], paths=[], diff=diff + ) + assert "tests/unit/test_x.py" not in [t["test_path"] for t in sec["rows"][0]["tests"]] + + # rows that are all deleted files still leave the gate's other tests counted + async def only_deleted(op: str, body: dict[str, Any]) -> Any: + out = await calls(op, body) + gone = {"test_path": "tests/unit/test_x.py", "why": "changed_test", "must_run": True} + extra = {"tests_total": 4, "must_run_total": 2, "tests_unnamed": 0, "tests_capped": True} + return {**out, "tests": [gone], **extra} + + sec = await review_brief_build._tests_section( + only_deleted, dataclasses.replace(inp, diff=deleting), [], [], [], paths=[], diff=diff + ) + row = sec["rows"][0] + assert row["tests"] == [] and row["left_out"] == 3 and row["must_run_left_out"] == 1 + assert row["cut"] is True # a gate that names no test but counts the leads it cut gives a row of the count async def unnamed_only(op: str, body: dict[str, Any]) -> Any: From 78c98526f16840dfb1f48375b213d3618723f7cc Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 07:23:52 +0000 Subject: [PATCH 10/29] A drifted changed file marks the run_first row cut, and a gate holding more rows than it returned does not Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 23 ++++++++------- server/code/app/index/review_brief_build.py | 5 ++-- server/transport/mcp_descriptions.py | 3 +- tests/unit/test_code_review_brief_sections.py | 29 ++++++++++++++----- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 1fe45f3a851..3029406adc3 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -420,17 +420,18 @@ run command and the gate's `why`, `evidence` and `must_run` for its first file. reads the gate's first 200 rows. `left_out` counts the test files the gate holds under none of the five, and `must_run_left_out` how many of them say `must_run`. A file past those 200 rows and a lead the gate cut are counted whatever their run command, and the gate's count of -cut leads is a lower bound. `code.test_gate` lists up to 200 rows and counts the leads it -cuts. The row says `cut: true` when the gate did not read the whole change or all its tests: -a changed file is missing from the gate's `changed_files` (one it was not sent, past the -first 50, whose hunks the diff it reads does not carry, or one past the 200 changed paths it -reads), the diff is longer than 200,000 characters, the gate's causes say `truncated` (its -call-graph walk or coverage read was cut), or it held more rows than it returned. Its counts -are then of what it read. A test file the PR deletes is no test to run: its row is neither -listed nor counted. The row is advice no verdict rests on: a read of the gate that raised keeps the -brief unstored, and a refused read, or a gate that lists and counts no test, leaves the row -out. A gate that names no test, or only deleted ones, but counts other tests or the leads -it cut gives a row with no tests and their count in `left_out`. +cut leads is a lower bound. The row says `cut: true` when the gate did not read the whole +change, or read it on another revision: a changed file is missing from the gate's +`changed_files` (one it was not sent, past the first 50, whose hunks the diff it reads does +not carry, or one past the 200 changed paths it reads), the diff is longer than 200,000 +characters, the gate's causes say `truncated` (its call-graph walk or coverage read was +cut), or a changed file is drifted (the index holds it at another revision than the diff's +old side, so its lines map onto other definitions). Its list and counts are then of what it +read. A test file the PR deletes is no test to run: its row is neither listed nor counted. +The row is advice no verdict rests on: a read of the gate that raised keeps the brief +unstored, and a refused read, or a gate that lists and counts no test, leaves the row out. A +gate that names no test, or only deleted ones, but counts other tests or the leads it cut +gives a row with no tests and their count in `left_out`. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 380e2d9be5c..fd5fe18caf7 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -884,12 +884,13 @@ async def _run_first_row( tests_unnamed=unnamed, ) # the gate read a cut change (a changed file it did not read), cut its own walk or - # coverage read, or held more rows than it returned: the counts are of what it read + # coverage read, or mapped a file's lines onto rows of another revision (`drifted`): + # the list and counts are of what it read cut = ( bool(set(paths) - set(gate.get("changed_files") or [])) or len(diff) > MAX_DIFF_CHARS or CAUSE_TRUNCATED in (gate.get("causes") or []) - or bool(gate.get("tests_capped")) + or bool(set(inp.drifted) & set(paths)) ) return {"kind": "run_first", **picked, **({"cut": True} if cut else {})}, [] diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index 728404f0353..29ef992a82e 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1662,7 +1662,8 @@ def revision_policy(update: str) -> str: "evidence, and `left_out` / `must_run_left_out` counting the test " "files under none of them (code.test_gate lists up to 200 and counts " "the leads it cuts), and `cut: true` when the gate did not read the " - "whole change, its counts then floors; then " + "whole change or read a file the index holds at another revision, its " + "list and counts then of what it read; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 04bf53026d7..e52ca76a775 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1145,13 +1145,25 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> ): sec = await review_brief_build._tests_section(calls, inp, [], [], [], **kw) assert sec["rows"][0]["cut"] is True - for extra in ({"tests_capped": True}, {"causes": ["truncated"]}): - async def answer(op: str, body: dict[str, Any], extra: dict[str, Any] = extra) -> Any: - return {**await calls(op, body), **extra} + async def truncated(op: str, body: dict[str, Any]) -> Any: + return {**await calls(op, body), "causes": ["truncated"]} - sec = await review_brief_build._tests_section(answer, inp, [], [], [], paths=[], diff=diff) - assert sec["rows"][0]["cut"] is True + sec = await review_brief_build._tests_section(truncated, inp, [], [], [], paths=[], diff=diff) + assert sec["rows"][0]["cut"] is True + # a changed file the index holds at another revision maps onto other rows + moved = dataclasses.replace(inp, drifted=("tests/unit/test_x.py",)) + sec = await review_brief_build._tests_section( + calls, moved, [], [], [], paths=["tests/unit/test_x.py"], diff=diff + ) + assert sec["rows"][0]["cut"] is True + + # a gate that held more rows than it returned counts them all: no cut + async def capped(op: str, body: dict[str, Any]) -> Any: + return {**await calls(op, body), "tests_capped": True} + + sec = await review_brief_build._tests_section(capped, inp, [], [], [], paths=[], diff=diff) + assert "cut" not in sec["rows"][0] # a test file the PR deletes is neither listed nor counted deleting = "--- a/tests/unit/test_x.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-a\n" @@ -1171,13 +1183,15 @@ async def with_deleted(op: str, body: dict[str, Any]) -> Any: sec = await review_brief_build._tests_section( with_deleted, dataclasses.replace(inp, diff=bare), [], [], [], paths=[], diff=diff ) - assert "tests/unit/test_x.py" not in [t["test_path"] for t in sec["rows"][0]["tests"]] + row = sec["rows"][0] + assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] + assert row["left_out"] == 6 and row["must_run_left_out"] == 0 # rows that are all deleted files still leave the gate's other tests counted async def only_deleted(op: str, body: dict[str, Any]) -> Any: out = await calls(op, body) gone = {"test_path": "tests/unit/test_x.py", "why": "changed_test", "must_run": True} - extra = {"tests_total": 4, "must_run_total": 2, "tests_unnamed": 0, "tests_capped": True} + extra = {"tests_total": 4, "must_run_total": 2, "tests_unnamed": 0} return {**out, "tests": [gone], **extra} sec = await review_brief_build._tests_section( @@ -1185,7 +1199,6 @@ async def only_deleted(op: str, body: dict[str, Any]) -> Any: ) row = sec["rows"][0] assert row["tests"] == [] and row["left_out"] == 3 and row["must_run_left_out"] == 1 - assert row["cut"] is True # a gate that names no test but counts the leads it cut gives a row of the count async def unnamed_only(op: str, body: dict[str, Any]) -> Any: From 9b888ac5175eb65a567c690fc511eabe09e76c7d Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 07:38:15 +0000 Subject: [PATCH 11/29] Only a drifted source file or a failed drift read marks the run_first row cut Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 14 ++-- docs/reference/operations/code.md | 2 +- server/code/api/models_review.py | 8 +- server/code/app/index/review_brief_build.py | 34 +++++--- server/code/domain/index/test_gate.py | 2 +- server/transport/mcp_descriptions.py | 4 +- tests/unit/test_code_review_brief_sections.py | 77 +++++++++++++++---- 7 files changed, 100 insertions(+), 41 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 3029406adc3..957e081f77c 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -425,13 +425,13 @@ change, or read it on another revision: a changed file is missing from the gate' `changed_files` (one it was not sent, past the first 50, whose hunks the diff it reads does not carry, or one past the 200 changed paths it reads), the diff is longer than 200,000 characters, the gate's causes say `truncated` (its call-graph walk or coverage read was -cut), or a changed file is drifted (the index holds it at another revision than the diff's -old side, so its lines map onto other definitions). Its list and counts are then of what it -read. A test file the PR deletes is no test to run: its row is neither listed nor counted. -The row is advice no verdict rests on: a read of the gate that raised keeps the brief -unstored, and a refused read, or a gate that lists and counts no test, leaves the row out. A -gate that names no test, or only deleted ones, but counts other tests or the leads it cut -gives a row with no tests and their count in `left_out`. +cut), or a changed source file is drifted (the index holds it at another revision than the +diff's old side, so its lines may map onto other definitions) or the drift read failed. Its +list and counts are then of what it read. A test file the PR deletes is no test to run: its +row is neither listed nor counted. The row is advice no verdict rests on: a read of the gate +that raised keeps the brief unstored, and a refused read, or a gate that lists and counts no +test, leaves the row out. A gate that names no test, or only deleted ones, but counts other +tests or the leads it cut gives a row with no tests and their count in `left_out`. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/docs/reference/operations/code.md b/docs/reference/operations/code.md index d271d547248..612b693b970 100644 --- a/docs/reference/operations/code.md +++ b/docs/reference/operations/code.md @@ -1473,7 +1473,7 @@ A merged PR's review brief: the point read of what the ingest built at merge — | `tokens_used` | `int` | Estimated tokens of the sections served. | | `token_budget` | `int` | The budget the sections were cut to: the request's, else the tier's. | | `decisions` | `dict[str, bool]` | one-bit answers read off the section verdicts: test_only, symbol_with_regressions, uncovered_symbol, randomized_setting, deleted_lines_blamed | -| `sections` | `dict[str, dict[str, Any]]` | name -> {status, verdict, epistemic, causes, rows, rows_total, tokens, truncated}; `rows_total` counts every row, of which `paths` stores the first 200; `failed` names the reads of a section that raised, among its causes; `symbols` adds… | +| `sections` | `dict[str, dict[str, Any]]` | name -> {status, verdict, epistemic, causes, rows, rows_total, tokens, truncated}; `rows_total` counts every row, of which `paths` stores the first 200; `failed` names the reads of a section that raised, among its causes except the advice… | | `trace_run_id` | `str` | Id of this call's trace row; pass it to `code.outcome`. | ## `code.scip_ingest` diff --git a/server/code/api/models_review.py b/server/code/api/models_review.py index eb56c773ad9..8ba09adbd63 100644 --- a/server/code/api/models_review.py +++ b/server/code/api/models_review.py @@ -186,10 +186,10 @@ def _drop_unset(self, handler: SerializerFunctionWrapHandler) -> dict[str, Any]: decisions: dict[str, bool] = Field(default_factory=dict) # name -> {status, verdict, epistemic, causes, rows, rows_total, tokens, truncated}; # `rows_total` counts every row, of which `paths` stores the first 200; `failed` names - # the reads of a section that raised, among its causes; `symbols` adds `collapsed` - # ({registration | factory | constructor | one_line: count}) for the wiring the PR - # adds that it lists as counts, not rows, and marks a function an open PR's overlay - # adds `via: added`. + # the reads of a section that raised, among its causes except the advice reads + # (`code.test_home`, `code.test_gate`); `symbols` adds `collapsed` ({registration | + # factory | constructor | one_line: count}) for the wiring the PR adds that it lists as + # counts, not rows, and marks a function an open PR's overlay adds `via: added`. sections: dict[str, dict[str, Any]] = Field(default_factory=dict) # Id of this call's trace row; pass it to `code.outcome`. trace_run_id: str = "" diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index fd5fe18caf7..7ad2977e883 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -155,8 +155,18 @@ async def build_brief( ranges = deleted_ranges(facts, src_only=is_graphed_path) + # a changed source file the index holds at another revision, or a drift read that + # failed: the gate may have mapped the diff's lines onto other definitions + moved = bool(inp.drift_failed) or any(kinds.get(p) for p in inp.drifted) tests_t = _tests_section( - call, inp, names[:MAX_COVERAGE_SYMBOLS], setting_names, stems, paths=paths, diff=diff + call, + inp, + names[:MAX_COVERAGE_SYMBOLS], + setting_names, + stems, + paths=paths, + diff=diff, + moved=moved, ) settings_t = _settings_section(call, inp, facts.settings[:MAX_SETTINGS]) defs = list(dict.fromkeys((str(r["qualified_name"]), str(r["path"])) for r in ranked)) @@ -741,6 +751,7 @@ async def _tests_section( *, paths: list[str], diff: str, + moved: bool, ) -> dict[str, Any]: base = {"org": inp.org, "namespace": inp.ns} subjects = [("symbol", n) for n in names] + [ @@ -766,7 +777,8 @@ async def _tests_section( ) ) (results, (first, advice_failed)) = await asyncio.gather( - asyncio.gather(*tasks, return_exceptions=True), _run_first_row(call, inp, paths, diff) + asyncio.gather(*tasks, return_exceptions=True), + _run_first_row(call, inp, paths, diff, moved=moved), ) # The tests to run first are advice no verdict rests on, first so a token budget trims # them last @@ -839,15 +851,15 @@ async def _tests_section( async def _run_first_row( - call: CallOp, inp: BriefInputs, paths: list[str], diff: str + call: CallOp, inp: BriefInputs, paths: list[str], diff: str, *, moved: bool ) -> tuple[dict[str, Any] | None, list[str]]: """(the `run_first` row of the tests section, the read of it that raised): the first tests code.test_gate lists for the PR's diff (`run_first`), None when it lists and - counts none. The gate reads a diff's old side as the revision the index holds, so - `diff` is the one code.impact reads: a merged PR's reversed, an open PR's as written. A - test file the PR deletes is no test to run, and its row is neither listed nor counted. - A read that raised only keeps the brief unstored, and a refused one leaves the row - out.""" + counts none. The gate reads a diff's old side as the revision the index holds, so `diff` + is the one code.impact reads: a merged PR's reversed, an open PR's as written; `moved` + says it may have mapped a source file's lines onto rows of another revision. A test file + the PR deletes is no test to run, and its row is neither listed nor counted. A read that + raised only keeps the brief unstored, and a refused one leaves the row out.""" if not (paths or diff): return None, [] try: @@ -884,13 +896,13 @@ async def _run_first_row( tests_unnamed=unnamed, ) # the gate read a cut change (a changed file it did not read), cut its own walk or - # coverage read, or mapped a file's lines onto rows of another revision (`drifted`): - # the list and counts are of what it read + # coverage read, or may have mapped a source file's lines onto rows of another revision + # (`moved`): the list and counts are of what it read cut = ( bool(set(paths) - set(gate.get("changed_files") or [])) or len(diff) > MAX_DIFF_CHARS or CAUSE_TRUNCATED in (gate.get("causes") or []) - or bool(set(inp.drifted) & set(paths)) + or moved ) return {"kind": "run_first", **picked, **({"cut": True} if cut else {})}, [] diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index 6cddb005fba..644628b1945 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -253,7 +253,7 @@ def _links_to(roots: set[str], names: dict[str, str]) -> list[str]: def _stem_key(path: str) -> str: - """A test file's directory and its name up to the first dot: `a.sql` and `a.expected`.""" + """A test file's directory and its name up to the first dot (`a.sql`, `a.expected`).""" p = PurePosixPath(path) return f"{p.parent}/{p.name.split('.', 1)[0]}" diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index 29ef992a82e..328db3ffe6a 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1662,8 +1662,8 @@ def revision_policy(update: str) -> str: "evidence, and `left_out` / `must_run_left_out` counting the test " "files under none of them (code.test_gate lists up to 200 and counts " "the leads it cuts), and `cut: true` when the gate did not read the " - "whole change or read a file the index holds at another revision, its " - "list and counts then of what it read; then " + "whole change or read a source file the index holds at another " + "revision, its list and counts then of what it read; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index e52ca76a775..b6f37c519aa 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1111,6 +1111,29 @@ async def build(fail: set[str], error: type[Exception] = RuntimeError) -> dict[s assert not any(r["kind"] == kind for r in secs["tests"]["rows"]) +@pytest.mark.asyncio +async def test_a_drifted_source_file_or_a_failed_drift_read_marks_the_run_first_row_cut() -> None: + """The gate maps the diff's lines onto the rows the index holds: a changed source file + the index holds at another revision, or a drift read that failed, makes its list a cut + one; a drifted file of no source kind maps no line.""" + + async def first(inp: rb.BriefInputs) -> dict[str, Any]: + payload = await review_brief_build.build_brief( + inp, call=_Calls(), repo=_Repo(), tracker_rows=_tracker_served, now=_fixed_now + ) + return payload["sections"]["tests"]["rows"][0] + + assert "cut" not in await first(_inputs()) + src = dataclasses.replace(_inputs(), drifted=("src/Core/Settings.cpp",)) + assert (await first(src))["cut"] is True + failed = dataclasses.replace(_inputs(), drift_failed="drift:TimeoutError") + assert (await first(failed))["cut"] is True + doc = dataclasses.replace( + _inputs(), changed_paths=[*PATHS, "docs/x.md"], drifted=("docs/x.md",) + ) + assert "cut" not in await first(doc) + + @pytest.mark.asyncio async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> None: """A PR with no claim and no home still names its tests to run first; the row says @@ -1120,7 +1143,7 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> inp = dataclasses.replace(_inputs(), open=True, head_sha="a" * 40) diff = "--- a/tests/unit/test_x.py\n+++ b/tests/unit/test_x.py\n@@ -1 +1 @@\n-a\n+b\n" out = await review_brief_build._tests_section( - calls, inp, [], [], [], paths=["tests/unit/test_x.py"], diff=diff + calls, inp, [], [], [], paths=["tests/unit/test_x.py"], diff=diff, moved=False ) assert [r["kind"] for r in out["rows"]] == ["run_first"] assert out["epistemic"] == "lower_bound" and out["verdict"] == "no_subjects" @@ -1129,9 +1152,8 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> assert body["diff"] == diff and body["limit"] == 200 assert "cut" not in out["rows"][0] # a changed file the gate did not read (past its file cap and outside the diff, a file - # the diff deletes there, or past the 200 paths it reads), a diff past its cap, a gate - # that cut its own walk and one that held more rows than it returned each say the - # counts are of a cut read + # the diff deletes there, or past the 200 paths it reads), a diff past its cap and a + # gate that cut its own walk each say the counts are of a cut read wide = [f"tests/unit/test_{i}.py" for i in range(51)] gone = "--- a/src/a.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-x\n" long_diff = diff + "+" * rb.MAX_DIFF_CHARS @@ -1143,18 +1165,20 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> {"paths": [*wide[:50], "src/a.py"], "diff": diff + gone}, {"paths": [], "diff": long_diff}, ): - sec = await review_brief_build._tests_section(calls, inp, [], [], [], **kw) + sec = await review_brief_build._tests_section(calls, inp, [], [], [], **kw, moved=False) assert sec["rows"][0]["cut"] is True async def truncated(op: str, body: dict[str, Any]) -> Any: return {**await calls(op, body), "causes": ["truncated"]} - sec = await review_brief_build._tests_section(truncated, inp, [], [], [], paths=[], diff=diff) + sec = await review_brief_build._tests_section( + truncated, inp, [], [], [], paths=[], diff=diff, moved=False + ) assert sec["rows"][0]["cut"] is True - # a changed file the index holds at another revision maps onto other rows - moved = dataclasses.replace(inp, drifted=("tests/unit/test_x.py",)) + # a changed source file the index holds at another revision, or a drift read that + # failed, may map the diff's lines onto other definitions sec = await review_brief_build._tests_section( - calls, moved, [], [], [], paths=["tests/unit/test_x.py"], diff=diff + calls, inp, [], [], [], paths=[], diff=diff, moved=True ) assert sec["rows"][0]["cut"] is True @@ -1162,7 +1186,9 @@ async def truncated(op: str, body: dict[str, Any]) -> Any: async def capped(op: str, body: dict[str, Any]) -> Any: return {**await calls(op, body), "tests_capped": True} - sec = await review_brief_build._tests_section(capped, inp, [], [], [], paths=[], diff=diff) + sec = await review_brief_build._tests_section( + capped, inp, [], [], [], paths=[], diff=diff, moved=False + ) assert "cut" not in sec["rows"][0] # a test file the PR deletes is neither listed nor counted deleting = "--- a/tests/unit/test_x.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-a\n" @@ -1173,7 +1199,14 @@ async def with_deleted(op: str, body: dict[str, Any]) -> Any: return {**out, "tests": [gone, *out["tests"]], "tests_total": 6, "must_run_total": 2} sec = await review_brief_build._tests_section( - with_deleted, dataclasses.replace(inp, diff=deleting), [], [], [], paths=[], diff=diff + with_deleted, + dataclasses.replace(inp, diff=deleting), + [], + [], + [], + paths=[], + diff=diff, + moved=False, ) row = sec["rows"][0] assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] @@ -1181,7 +1214,14 @@ async def with_deleted(op: str, body: dict[str, Any]) -> Any: # git prints a deleted binary or empty file with no sides, only `deleted file mode` bare = "diff --git a/tests/unit/test_x.py b/tests/unit/test_x.py\ndeleted file mode 100644\n" sec = await review_brief_build._tests_section( - with_deleted, dataclasses.replace(inp, diff=bare), [], [], [], paths=[], diff=diff + with_deleted, + dataclasses.replace(inp, diff=bare), + [], + [], + [], + paths=[], + diff=diff, + moved=False, ) row = sec["rows"][0] assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] @@ -1195,7 +1235,14 @@ async def only_deleted(op: str, body: dict[str, Any]) -> Any: return {**out, "tests": [gone], **extra} sec = await review_brief_build._tests_section( - only_deleted, dataclasses.replace(inp, diff=deleting), [], [], [], paths=[], diff=diff + only_deleted, + dataclasses.replace(inp, diff=deleting), + [], + [], + [], + paths=[], + diff=diff, + moved=False, ) row = sec["rows"][0] assert row["tests"] == [] and row["left_out"] == 3 and row["must_run_left_out"] == 1 @@ -1206,12 +1253,12 @@ async def unnamed_only(op: str, body: dict[str, Any]) -> Any: return {**out, "tests": [], "tests_total": 0, "must_run_total": 0, "tests_unnamed": 7} sec = await review_brief_build._tests_section( - unnamed_only, inp, [], [], [], paths=[], diff=diff + unnamed_only, inp, [], [], [], paths=[], diff=diff, moved=False ) assert sec["rows"][0]["tests"] == [] and sec["rows"][0]["left_out"] == 7 # a file past the cap whose hunks the diff carries is read covered = await review_brief_build._tests_section( - calls, inp, [], [], [], paths=[*wide[:50], "tests/unit/test_x.py"], diff=diff + calls, inp, [], [], [], paths=[*wide[:50], "tests/unit/test_x.py"], diff=diff, moved=False ) assert "cut" not in covered["rows"][0] From 576c1ae74cd0739ca98a8f2baff245958c118ac2 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 07:43:33 +0000 Subject: [PATCH 12/29] The run_first drift test drifts a non-source file inside the indexed prefixes, and the MCP text names a failed drift read Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- server/transport/mcp_descriptions.py | 5 +++-- tests/unit/test_code_review_brief_sections.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index 328db3ffe6a..8494b8e1902 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1662,8 +1662,9 @@ def revision_policy(update: str) -> str: "evidence, and `left_out` / `must_run_left_out` counting the test " "files under none of them (code.test_gate lists up to 200 and counts " "the leads it cuts), and `cut: true` when the gate did not read the " - "whole change or read a source file the index holds at another " - "revision, its list and counts then of what it read; then " + "whole change, read a source file the index holds at another " + "revision or the drift read failed, its list and counts then of what " + "it read; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index b6f37c519aa..be7c2e4b7cc 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1129,7 +1129,7 @@ async def first(inp: rb.BriefInputs) -> dict[str, Any]: failed = dataclasses.replace(_inputs(), drift_failed="drift:TimeoutError") assert (await first(failed))["cut"] is True doc = dataclasses.replace( - _inputs(), changed_paths=[*PATHS, "docs/x.md"], drifted=("docs/x.md",) + _inputs(), changed_paths=[*PATHS, "src/README.md"], drifted=("src/README.md",) ) assert "cut" not in await first(doc) From 802311247ea5b67391f91dbd696d32065cc3480f Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 07:50:14 +0000 Subject: [PATCH 13/29] A test builds an open PR's brief and checks the gate reads its diff as written Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- tests/unit/test_code_review_brief_sections.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index be7c2e4b7cc..0379d03c8dc 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1150,6 +1150,15 @@ async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> # the gate reads the diff it is handed (an open PR's as written) and all its rows body = next(b for op, b in calls.calls if op == "code.test_gate") assert body["diff"] == diff and body["limit"] == 200 + opened = _Calls() + await review_brief_build.build_brief( + dataclasses.replace(_inputs(), open=True, head_sha="a" * 40), + call=opened, + repo=_Repo(), + tracker_rows=_tracker_served, + now=_fixed_now, + ) + assert next(b for op, b in opened.calls if op == "code.test_gate")["diff"] == DIFF assert "cut" not in out["rows"][0] # a changed file the gate did not read (past its file cap and outside the diff, a file # the diff deletes there, or past the 200 paths it reads), a diff past its cap and a From f6f829d6b586cac345d18213c63dd76853eafb54 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 08:08:26 +0000 Subject: [PATCH 14/29] The run_first tests split by behaviour, a dotfile is its own test, and the docs name run_unknown and where the gate reads its links Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 40 ++-- server/code/app/index/review_brief_build.py | 4 +- server/code/domain/index/test_gate.py | 19 +- tests/unit/test_code_review_brief_sections.py | 220 +++++++++--------- tests/unit/test_code_test_gate.py | 6 + 5 files changed, 145 insertions(+), 144 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 957e081f77c..7dd15dbd173 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -414,24 +414,28 @@ tests `code.test_gate` lists for the PR's diff, in its order, so every test that `must_run` comes before a lead and the strongest evidence comes first. Files that share a run command are one test, and a file with no run command joins the first test that holds a file of its directory and name up to the first dot (a case's expected output beside its -script); files with different run commands are never one test. A test of several files names -its first five in `test_paths` and counts them in `test_paths_total`. Each test carries its -run command and the gate's `why`, `evidence` and `must_run` for its first file. The brief -reads the gate's first 200 rows. `left_out` counts the test files the gate holds under none -of the five, and `must_run_left_out` how many of them say `must_run`. A file past those 200 -rows and a lead the gate cut are counted whatever their run command, and the gate's count of -cut leads is a lower bound. The row says `cut: true` when the gate did not read the whole -change, or read it on another revision: a changed file is missing from the gate's -`changed_files` (one it was not sent, past the first 50, whose hunks the diff it reads does -not carry, or one past the 200 changed paths it reads), the diff is longer than 200,000 -characters, the gate's causes say `truncated` (its call-graph walk or coverage read was -cut), or a changed source file is drifted (the index holds it at another revision than the -diff's old side, so its lines may map onto other definitions) or the drift read failed. Its -list and counts are then of what it read. A test file the PR deletes is no test to run: its -row is neither listed nor counted. The row is advice no verdict rests on: a read of the gate -that raised keeps the brief unstored, and a refused read, or a gate that lists and counts no -test, leaves the row out. A gate that names no test, or only deleted ones, but counts other -tests or the leads it cut gives a row with no tests and their count in `left_out`. +script; a dotfile's whole name); files with different run commands are never one test. A +test of several files names its first five in `test_paths` and counts them in +`test_paths_total`. Each test carries its run command (`run`, with `run_unknown: true` when +the gate knows none) and the gate's `why`, `evidence` and `must_run` for its first file. The +brief reads the gate's first 200 rows. `left_out` counts the test files the gate holds under +none of the five, and `must_run_left_out` how many of them say `must_run`. A file past those +200 rows and a lead the gate cut are counted whatever their run command, and the gate's +count of cut leads is a lower bound. The row says `cut: true` when the gate did not read the +whole change, or read a changed source file on another revision: a changed file is missing +from the gate's `changed_files` (one it was not sent, past the first 50, whose hunks the +diff it reads does not carry, or one past the 200 changed paths it reads), the diff is +longer than 200,000 characters, the gate's causes say `truncated` (its call-graph walk or +coverage read was cut), or a changed source file is drifted (the index holds it at another +revision than the diff's old side, so its lines may map onto other definitions) or the drift +read failed. Its list and counts are then of what it read. The gate reads test links at the +revision the index holds, as the claim rows do, so the brief of a merged PR built after the +index moved past it can name a test added since. A test file the PR deletes is no test to +run: its row is neither listed nor counted. The row is advice no verdict rests on: a read of +the gate that raised keeps the brief unstored, and a refused read, or a gate that lists and +counts no test, leaves the row out. A gate that names no test, or only deleted ones, but +counts other tests or the leads it cut gives a row with no tests and their count in +`left_out`. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 7ad2977e883..54563001e14 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -908,8 +908,8 @@ async def _run_first_row( def _deleted_paths(diff: str) -> set[str]: - """The paths of the files a git diff deletes: a `+++ /dev/null` side, or a `deleted - file mode` line for a deletion git prints with no sides (a binary or an empty file).""" + """The paths of the files a git diff deletes: a `+++ /dev/null` side, or a `deleted file + mode` line for a deletion git prints with no sides (a binary or an empty file).""" marked = {p for p, f in parse_unified_diff(diff).files.items() if f.status == "D"} return marked | set(whole_files(diff)) diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index 644628b1945..daa44979e59 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -253,17 +253,18 @@ def _links_to(roots: set[str], names: dict[str, str]) -> list[str]: def _stem_key(path: str) -> str: - """A test file's directory and its name up to the first dot (`a.sql`, `a.expected`).""" + """A test file's directory and its name up to the first dot (`a.sql`, `a.expected`), a + dotfile's whole name (`.env`).""" p = PurePosixPath(path) - return f"{p.parent}/{p.name.split('.', 1)[0]}" + return f"{p.parent}/{p.name.split('.', 1)[0] or p.name}" def _test_groups(tests: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: """The gate's rows as tests, each in the gate's order and the tests in the order of their first rows: rows that share a run command are one test, and a row with none joins the first test that holds a file of its directory and name (`_stem_key`, a case's - expected output beside its script), else the test of the command-less rows of that - name. Rows with different commands are never one test.""" + expected output beside its script), else the test of the command-less rows of that name. + Rows with different commands are never one test.""" by_run: dict[str, list[int]] = {} for i, r in enumerate(tests): if r.get("run"): @@ -298,11 +299,11 @@ def run_first( """The first `limit` tests of a gate's `tests` rows (`_test_groups`), in the gate's order: every `must_run` row first, the strongest evidence first. A test carries its first row's path, `why`, `evidence` and `must_run`, and its run command; a test of - several files names the first `_PATHS_SHOWN` and counts them all. `left_out` - counts the gate's test files under no listed test, and `must_run_left_out` those of - them that say `must_run`; `tests_total`, `must_run_total` and `tests_unnamed` are the - gate's counts of the rows `tests` was cut from. A row past `tests` and a lead the gate - cut are counted whatever their run command, and the cut leads are a lower bound.""" + several files names the first `_PATHS_SHOWN` and counts them all. `left_out` counts the + gate's test files under no listed test, and `must_run_left_out` those of them that say + `must_run`; `tests_total`, `must_run_total` and `tests_unnamed` are the gate's counts of + the rows `tests` was cut from. A row past `tests` and a lead the gate cut are counted + whatever their run command, and the cut leads are a lower bound.""" listed = _test_groups(tests)[:limit] shown = [r for rows in listed for r in rows] return { diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 0379d03c8dc..0e3010bc079 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -617,8 +617,8 @@ async def __call__(self, op: str, body: dict[str, Any]) -> dict[str, Any]: "must_run_total": 1, "tests_unnamed": 6, "tests_capped": False, - # as the gate reads them: the files sent, then the files the diff changes - # on its new side (a deleted file has none) + # as the gate reads them: the files sent, then the files the diff changes on + # its new side (a deleted file has none) "changed_files": list( dict.fromkeys( [ @@ -1087,9 +1087,8 @@ async def test_a_failing_op_fails_its_section_open_and_the_rest_stand() -> None: async def test_the_test_home_and_the_tests_to_run_first_decide_no_verdict( op: str, kind: str, cause: str ) -> None: - """The test home and the tests to run first are advice: a read of one that raised or - was refused leaves a settled brief settled, and one that raised keeps the brief - unstored.""" + """The test home and the tests to run first are advice: a read of one that raised or was + refused leaves a settled brief settled, and one that raised keeps the brief unstored.""" from server.core.errors import ValidationError async def build(fail: set[str], error: type[Exception] = RuntimeError) -> dict[str, Any]: @@ -1134,142 +1133,133 @@ async def first(inp: rb.BriefInputs) -> dict[str, Any]: assert "cut" not in await first(doc) +_OPEN = dataclasses.replace(_inputs(), open=True, head_sha="a" * 40) +_TEST_DIFF = "--- a/tests/unit/test_x.py\n+++ b/tests/unit/test_x.py\n@@ -1 +1 @@\n-a\n+b\n" +_DELETING = "--- a/tests/unit/test_x.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-a\n" +_DELETED_ROW = {"test_path": "tests/unit/test_x.py", "why": "changed_test", "must_run": True} + + +async def _run_first_of( + call: Any, + inp: rb.BriefInputs = _OPEN, + *, + paths: list[str] | None = None, + diff: str = _TEST_DIFF, + moved: bool = False, +) -> dict[str, Any]: + """The `run_first` row of a tests section with no claim and no home.""" + sec = await review_brief_build._tests_section( + call, inp, [], [], [], paths=paths or [], diff=diff, moved=moved + ) + return dict(sec["rows"][0]) + + +def _answering(**extra: Any) -> Any: + """`_Calls` whose code.test_gate answer carries `extra`, and `ahead` the rows to put + ahead of its own `tests`.""" + calls = _Calls() + + async def answer(op: str, body: dict[str, Any]) -> Any: + out = await calls(op, body) + if op != "code.test_gate": + return out + ahead = extra.get("ahead", []) + rest = {k: v for k, v in extra.items() if k != "ahead"} + return {**out, "tests": [*ahead, *out["tests"]], **rest} + + return answer + + @pytest.mark.asyncio async def test_the_tests_to_run_first_alone_leave_the_section_a_lower_bound() -> None: """A PR with no claim and no home still names its tests to run first; the row says - nothing of what a claim read, so the section stays `lower_bound` with no verdict. An - open PR's diff reaches the gate as written.""" + nothing of what a claim read, so the section stays `lower_bound` with no verdict.""" calls = _Calls() - inp = dataclasses.replace(_inputs(), open=True, head_sha="a" * 40) - diff = "--- a/tests/unit/test_x.py\n+++ b/tests/unit/test_x.py\n@@ -1 +1 @@\n-a\n+b\n" out = await review_brief_build._tests_section( - calls, inp, [], [], [], paths=["tests/unit/test_x.py"], diff=diff, moved=False + calls, _OPEN, [], [], [], paths=["tests/unit/test_x.py"], diff=_TEST_DIFF, moved=False ) assert [r["kind"] for r in out["rows"]] == ["run_first"] assert out["epistemic"] == "lower_bound" and out["verdict"] == "no_subjects" - # the gate reads the diff it is handed (an open PR's as written) and all its rows + assert "cut" not in out["rows"][0] + # the gate reads the diff it is handed and all its rows body = next(b for op, b in calls.calls if op == "code.test_gate") - assert body["diff"] == diff and body["limit"] == 200 - opened = _Calls() + assert body["diff"] == _TEST_DIFF and body["limit"] == 200 + + +@pytest.mark.asyncio +async def test_an_open_prs_diff_reaches_the_gate_as_written() -> None: + """The index holds an open PR's base, the diff's old side: the gate reads the diff as + written, where a merged PR's is reversed.""" + calls = _Calls() await review_brief_build.build_brief( - dataclasses.replace(_inputs(), open=True, head_sha="a" * 40), - call=opened, - repo=_Repo(), - tracker_rows=_tracker_served, - now=_fixed_now, + _OPEN, call=calls, repo=_Repo(), tracker_rows=_tracker_served, now=_fixed_now ) - assert next(b for op, b in opened.calls if op == "code.test_gate")["diff"] == DIFF - assert "cut" not in out["rows"][0] - # a changed file the gate did not read (past its file cap and outside the diff, a file - # the diff deletes there, or past the 200 paths it reads), a diff past its cap and a - # gate that cut its own walk each say the counts are of a cut read + assert next(b for op, b in calls.calls if op == "code.test_gate")["diff"] == DIFF + + +@pytest.mark.asyncio +async def test_the_tests_to_run_first_say_cut_when_the_gate_read_a_cut_change() -> None: + """A changed file the gate did not read (past its file cap and outside the diff, a file + the diff deletes there, or past the 200 paths it reads), a diff past its cap, a gate + that cut its own walk, and a changed source file read on another revision each say the + list and counts are of a cut read. A gate that held more rows than it returned counts + them all, and a file past the cap whose hunks the diff carries is read.""" + calls = _Calls() wide = [f"tests/unit/test_{i}.py" for i in range(51)] gone = "--- a/src/a.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-x\n" - long_diff = diff + "+" * rb.MAX_DIFF_CHARS many = [f"tests/unit/test_{i}.py" for i in range(201)] every = "".join(f"--- a/{p}\n+++ b/{p}\n@@ -1 +1 @@\n-a\n+b\n" for p in many) - for kw in ( - {"paths": wide, "diff": diff}, - {"paths": many, "diff": every}, - {"paths": [*wide[:50], "src/a.py"], "diff": diff + gone}, - {"paths": [], "diff": long_diff}, + for paths, diff in ( + (wide, _TEST_DIFF), + (many, every), + ([*wide[:50], "src/a.py"], _TEST_DIFF + gone), + ([], _TEST_DIFF + "+" * rb.MAX_DIFF_CHARS), ): - sec = await review_brief_build._tests_section(calls, inp, [], [], [], **kw, moved=False) - assert sec["rows"][0]["cut"] is True - - async def truncated(op: str, body: dict[str, Any]) -> Any: - return {**await calls(op, body), "causes": ["truncated"]} - - sec = await review_brief_build._tests_section( - truncated, inp, [], [], [], paths=[], diff=diff, moved=False - ) - assert sec["rows"][0]["cut"] is True - # a changed source file the index holds at another revision, or a drift read that - # failed, may map the diff's lines onto other definitions - sec = await review_brief_build._tests_section( - calls, inp, [], [], [], paths=[], diff=diff, moved=True - ) - assert sec["rows"][0]["cut"] is True - - # a gate that held more rows than it returned counts them all: no cut - async def capped(op: str, body: dict[str, Any]) -> Any: - return {**await calls(op, body), "tests_capped": True} + assert (await _run_first_of(calls, paths=paths, diff=diff))["cut"] is True + assert (await _run_first_of(_answering(causes=["truncated"])))["cut"] is True + assert (await _run_first_of(calls, moved=True))["cut"] is True + assert "cut" not in await _run_first_of(_answering(tests_capped=True)) + read = await _run_first_of(calls, paths=[*wide[:50], "tests/unit/test_x.py"]) + assert "cut" not in read - sec = await review_brief_build._tests_section( - capped, inp, [], [], [], paths=[], diff=diff, moved=False - ) - assert "cut" not in sec["rows"][0] - # a test file the PR deletes is neither listed nor counted - deleting = "--- a/tests/unit/test_x.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-a\n" - - async def with_deleted(op: str, body: dict[str, Any]) -> Any: - out = await calls(op, body) - gone = {"test_path": "tests/unit/test_x.py", "why": "changed_test", "must_run": True} - return {**out, "tests": [gone, *out["tests"]], "tests_total": 6, "must_run_total": 2} - sec = await review_brief_build._tests_section( - with_deleted, - dataclasses.replace(inp, diff=deleting), - [], - [], - [], - paths=[], - diff=diff, - moved=False, - ) - row = sec["rows"][0] - assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] - assert row["left_out"] == 6 and row["must_run_left_out"] == 0 - # git prints a deleted binary or empty file with no sides, only `deleted file mode` +@pytest.mark.asyncio +async def test_a_test_file_the_pr_deletes_is_neither_listed_nor_counted() -> None: + """Whether git prints the deletion with a `+++ /dev/null` side or, for a binary or an + empty file, with only `deleted file mode`; a gate whose rows are all deleted files + still counts its other tests.""" bare = "diff --git a/tests/unit/test_x.py b/tests/unit/test_x.py\ndeleted file mode 100644\n" - sec = await review_brief_build._tests_section( - with_deleted, - dataclasses.replace(inp, diff=bare), - [], - [], - [], - paths=[], - diff=diff, - moved=False, - ) - row = sec["rows"][0] - assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] - assert row["left_out"] == 6 and row["must_run_left_out"] == 0 + with_deleted = _answering(ahead=[_DELETED_ROW], tests_total=6, must_run_total=2) + for deletion in (_DELETING, bare): + row = await _run_first_of(with_deleted, dataclasses.replace(_OPEN, diff=deletion)) + assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] + assert row["left_out"] == 6 and row["must_run_left_out"] == 0 + only_deleted = _answering( + tests=[_DELETED_ROW], tests_total=4, must_run_total=2, tests_unnamed=0 + ) + row = await _run_first_of(only_deleted, dataclasses.replace(_OPEN, diff=_DELETING)) + assert row["tests"] == [] and row["left_out"] == 3 and row["must_run_left_out"] == 1 - # rows that are all deleted files still leave the gate's other tests counted - async def only_deleted(op: str, body: dict[str, Any]) -> Any: - out = await calls(op, body) - gone = {"test_path": "tests/unit/test_x.py", "why": "changed_test", "must_run": True} - extra = {"tests_total": 4, "must_run_total": 2, "tests_unnamed": 0} - return {**out, "tests": [gone], **extra} - sec = await review_brief_build._tests_section( - only_deleted, - dataclasses.replace(inp, diff=deleting), - [], - [], - [], - paths=[], - diff=diff, - moved=False, +@pytest.mark.asyncio +async def test_a_gate_that_names_no_test_but_counts_cut_leads_gives_a_row_of_the_count() -> None: + row = await _run_first_of( + _answering(tests=[], tests_total=0, must_run_total=0, tests_unnamed=7) ) - row = sec["rows"][0] - assert row["tests"] == [] and row["left_out"] == 3 and row["must_run_left_out"] == 1 + assert row["tests"] == [] and row["left_out"] == 7 - # a gate that names no test but counts the leads it cut gives a row of the count - async def unnamed_only(op: str, body: dict[str, Any]) -> Any: - out = await calls(op, body) - return {**out, "tests": [], "tests_total": 0, "must_run_total": 0, "tests_unnamed": 7} +@pytest.mark.asyncio +async def test_a_token_budget_trims_the_tests_to_run_first_last() -> None: + """`apply_budget` drops a section's rows from its end, so the first row goes last.""" + calls = _Calls() sec = await review_brief_build._tests_section( - unnamed_only, inp, [], [], [], paths=[], diff=diff, moved=False - ) - assert sec["rows"][0]["tests"] == [] and sec["rows"][0]["left_out"] == 7 - # a file past the cap whose hunks the diff carries is read - covered = await review_brief_build._tests_section( - calls, inp, [], [], [], paths=[*wide[:50], "tests/unit/test_x.py"], diff=diff, moved=False + calls, _OPEN, ["DB::f", "DB::g"], [], [], paths=[], diff=_TEST_DIFF, moved=False ) - assert "cut" not in covered["rows"][0] + assert sec["rows"][0]["kind"] == "run_first" and len(sec["rows"]) > 1 + first = rb.estimate_tokens(sec["rows"][:1]) + trimmed, _ = rb.apply_budget({"tests": sec}, ["tests"], first) + assert [r["kind"] for r in trimmed["tests"]["rows"]] == ["run_first"] @pytest.mark.asyncio diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index c6d434b550c..db81b6ef413 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -307,6 +307,12 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: _gate_row("web/a.spec.ts", "coverage", True), ] assert len(tg.run_first(pair, tests_total=2, must_run_total=2, tests_unnamed=0)["tests"]) == 2 + # dotfiles of one directory share no name: each is its own test + dots = [ + {"test_path": "fixtures/.env", "why": "changed_test", "must_run": True, "run": ""}, + {"test_path": "fixtures/.npmrc", "why": "changed_test", "must_run": True, "run": ""}, + ] + assert len(tg.run_first(dots, tests_total=2, must_run_total=2, tests_unnamed=0)["tests"]) == 2 def test_a_one_hop_row_whose_test_uses_no_touched_definition_is_a_lead() -> None: From 00c58a5db209787ba24a963c1d20042c0cd302bf Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 08:18:20 +0000 Subject: [PATCH 15/29] The run_first domain tests split by behaviour and the brief's gate fake takes its rows ahead by name Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- tests/unit/test_code_review_brief_sections.py | 16 ++++----- tests/unit/test_code_test_gate.py | 36 ++++++++++++------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 0e3010bc079..c04c369a94d 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1154,18 +1154,16 @@ async def _run_first_of( return dict(sec["rows"][0]) -def _answering(**extra: Any) -> Any: - """`_Calls` whose code.test_gate answer carries `extra`, and `ahead` the rows to put - ahead of its own `tests`.""" +def _answering(*, ahead: tuple[dict[str, Any], ...] = (), **gate: Any) -> Any: + """`_Calls` whose code.test_gate answer puts the rows `ahead` before its own `tests` + and then carries the fields `gate`.""" calls = _Calls() async def answer(op: str, body: dict[str, Any]) -> Any: out = await calls(op, body) if op != "code.test_gate": return out - ahead = extra.get("ahead", []) - rest = {k: v for k, v in extra.items() if k != "ahead"} - return {**out, "tests": [*ahead, *out["tests"]], **rest} + return {**out, "tests": [*ahead, *out["tests"]], **gate} return answer @@ -1202,8 +1200,8 @@ async def test_the_tests_to_run_first_say_cut_when_the_gate_read_a_cut_change() """A changed file the gate did not read (past its file cap and outside the diff, a file the diff deletes there, or past the 200 paths it reads), a diff past its cap, a gate that cut its own walk, and a changed source file read on another revision each say the - list and counts are of a cut read. A gate that held more rows than it returned counts - them all, and a file past the cap whose hunks the diff carries is read.""" + list and counts are of a cut read. A gate that held more rows than it returned is no + cut read, and a file past the cap whose hunks the diff carries is read.""" calls = _Calls() wide = [f"tests/unit/test_{i}.py" for i in range(51)] gone = "--- a/src/a.py\n+++ /dev/null\n@@ -1 +0,0 @@\n-x\n" @@ -1229,7 +1227,7 @@ async def test_a_test_file_the_pr_deletes_is_neither_listed_nor_counted() -> Non empty file, with only `deleted file mode`; a gate whose rows are all deleted files still counts its other tests.""" bare = "diff --git a/tests/unit/test_x.py b/tests/unit/test_x.py\ndeleted file mode 100644\n" - with_deleted = _answering(ahead=[_DELETED_ROW], tests_total=6, must_run_total=2) + with_deleted = _answering(ahead=(_DELETED_ROW,), tests_total=6, must_run_total=2) for deletion in (_DELETING, bare): row = await _run_first_of(with_deleted, dataclasses.replace(_OPEN, diff=deletion)) assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index db81b6ef413..a6734a05c05 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -234,7 +234,7 @@ def _gate_row(path: str, why: str, must_run: bool) -> dict[str, Any]: def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: """A case's script and its expected output are one test, as are the files of one run command, under the stronger row; the list stops at `limit` and counts the test files it - leaves out.""" + leaves out, the gate's cut leads and the rows it did not return among them.""" rows = [ _gate_row("tests/cases/a.expected", "changed_test", True), {**_gate_row("tests/cases/a.sh", "changed_test", True), "run": "sh tests/cases/a.sh"}, @@ -268,8 +268,12 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: # a gate cut by its own limit: the rows it did not return are left out too cut = tg.run_first(rows[:2], tests_total=7, must_run_total=4, tests_unnamed=3, limit=3) assert len(cut["tests"]) == 1 and cut["left_out"] == 8 and cut["must_run_left_out"] == 2 - # a command-less row joins the test that runs a file of its name, wherever that file - # stands, and a row joins the test of its run command before one of its name + + +def test_run_first_joins_a_command_less_file_to_the_test_of_its_name() -> None: + """A command-less row joins the test that runs a file of its name, wherever that file + stands, and a row joins the test of its run command before one of its name; + command-less files of one name with no test of a command are one test of their own.""" order = [ _gate_row("tests/cases/a.expected", "changed_test", True), {**_gate_row("tests/cases/b.sh", "coverage", True), "run": "run-cases tests/cases"}, @@ -283,12 +287,6 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: "changed_test", ) ] - # a row past the gate's `tests` is counted whatever its command: the gate's counts are - # all the brief has of it - pkg = [_gate_row(f"pkg/a/f{i}_test.go", "changed_test", True) for i in range(3)] - past = tg.run_first(pkg, tests_total=5, must_run_total=5, tests_unnamed=0) - assert len(past["tests"]) == 1 and past["left_out"] == 2 and past["must_run_left_out"] == 2 - # command-less files of one name with no test of a command are one test of their own pair = [ _gate_row("tests/cases/c.in", "test_map", False), _gate_row("tests/cases/c.out", "test_map", False), @@ -297,17 +295,31 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: assert [(t["test_paths"], t["run_unknown"]) for t in alone] == [ (["tests/cases/c.in", "tests/cases/c.out"], True) ] - # a test of many files names the first five and counts them all + + +def test_run_first_counts_a_row_past_the_gates_tests_whatever_its_command() -> None: + """The gate's counts are all the brief has of a row it did not return.""" + pkg = [_gate_row(f"pkg/a/f{i}_test.go", "changed_test", True) for i in range(3)] + past = tg.run_first(pkg, tests_total=5, must_run_total=5, tests_unnamed=0) + assert len(past["tests"]) == 1 and past["left_out"] == 2 and past["must_run_left_out"] == 2 + + +def test_run_first_names_five_paths_of_a_test_and_counts_all() -> None: many = [_gate_row(f"pkg/b/f{i}_test.go", "coverage", True) for i in range(7)] one_test = tg.run_first(many, tests_total=7, must_run_total=7, tests_unnamed=0)["tests"] assert len(one_test[0]["test_paths"]) == 5 and one_test[0]["test_paths_total"] == 7 - # two tests of one name with different commands stay two + + +def test_run_first_keeps_tests_of_one_name_with_different_commands_apart() -> None: pair = [ _gate_row("web/a.test.ts", "coverage", True), _gate_row("web/a.spec.ts", "coverage", True), ] assert len(tg.run_first(pair, tests_total=2, must_run_total=2, tests_unnamed=0)["tests"]) == 2 - # dotfiles of one directory share no name: each is its own test + + +def test_run_first_keeps_dotfiles_of_one_directory_apart() -> None: + """A dotfile's name is its whole name, so `.env` and `.npmrc` share none.""" dots = [ {"test_path": "fixtures/.env", "why": "changed_test", "must_run": True, "run": ""}, {"test_path": "fixtures/.npmrc", "why": "changed_test", "must_run": True, "run": ""}, From 6ca21877e938e5e5f39314f3c113f6d05fe6dedc Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 08:32:02 +0000 Subject: [PATCH 16/29] A merged PR's test reads its deleted tests off its own diff, and the docs say a brief with no run_first row names none Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 3 ++- server/transport/mcp_descriptions.py | 2 +- tests/unit/test_code_review_brief_sections.py | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 7dd15dbd173..166f145721d 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -435,7 +435,8 @@ run: its row is neither listed nor counted. The row is advice no verdict rests o the gate that raised keeps the brief unstored, and a refused read, or a gate that lists and counts no test, leaves the row out. A gate that names no test, or only deleted ones, but counts other tests or the leads it cut gives a row with no tests and their count in -`left_out`. +`left_out`. A brief stored with no `run_first` row says nothing of the tests to run first: +`code.test_gate` lists them. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index 8494b8e1902..d49a7c565c7 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1664,7 +1664,7 @@ def revision_policy(update: str) -> str: "the leads it cuts), and `cut: true` when the gate did not read the " "whole change, read a source file the index holds at another " "revision or the drift read failed, its list and counts then of what " - "it read; then " + "it read; a brief with no run_first row says nothing of them; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index c04c369a94d..250d40b5fc9 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1239,6 +1239,28 @@ async def test_a_test_file_the_pr_deletes_is_neither_listed_nor_counted() -> Non assert row["tests"] == [] and row["left_out"] == 3 and row["must_run_left_out"] == 1 +@pytest.mark.asyncio +async def test_a_merged_prs_deleted_tests_are_read_off_its_own_diff() -> None: + """A merged PR's gate reads the reversed diff, where a test file the PR adds looks + deleted: the deleted files come from the PR's own diff, so an added test is listed and + a deleted one is not.""" + added = { + "test_path": "tests/queries/0_stateless/03999_new_flag.sql", + "why": "changed_test", + "must_run": True, + } + row = await _run_first_of(_answering(ahead=(added,)), _inputs(), diff=rb.reversed_diff(DIFF)) + assert row["tests"][0]["test_path"] == added["test_path"] + merged = dataclasses.replace(_inputs(), diff=_DELETING) + row = await _run_first_of( + _answering(ahead=(_DELETED_ROW,), tests_total=6, must_run_total=2), + merged, + diff=rb.reversed_diff(_DELETING), + ) + assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] + assert row["left_out"] == 6 and row["must_run_left_out"] == 0 + + @pytest.mark.asyncio async def test_a_gate_that_names_no_test_but_counts_cut_leads_gives_a_row_of_the_count() -> None: row = await _run_first_of( From c6281014f2b5d9ecd55d2a55c61b2912611aeb85 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 08:41:31 +0000 Subject: [PATCH 17/29] The merged-PR deletion test keeps only the case it can fail on and says why the PR's own diff is read Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- tests/unit/test_code_review_brief_sections.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 250d40b5fc9..a8312ce7877 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1241,16 +1241,9 @@ async def test_a_test_file_the_pr_deletes_is_neither_listed_nor_counted() -> Non @pytest.mark.asyncio async def test_a_merged_prs_deleted_tests_are_read_off_its_own_diff() -> None: - """A merged PR's gate reads the reversed diff, where a test file the PR adds looks - deleted: the deleted files come from the PR's own diff, so an added test is listed and - a deleted one is not.""" - added = { - "test_path": "tests/queries/0_stateless/03999_new_flag.sql", - "why": "changed_test", - "must_run": True, - } - row = await _run_first_of(_answering(ahead=(added,)), _inputs(), diff=rb.reversed_diff(DIFF)) - assert row["tests"][0]["test_path"] == added["test_path"] + """A merged PR's gate reads the reversed diff, where a deletion git printed with no + `deleted file mode` line reads as a change: the deleted files come from the PR's own + diff, so its deleted test is neither listed nor counted.""" merged = dataclasses.replace(_inputs(), diff=_DELETING) row = await _run_first_of( _answering(ahead=(_DELETED_ROW,), tests_total=6, must_run_total=2), From 453a7d334d40121aa255bccd7c59410ce91d6a77 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 09:22:12 +0000 Subject: [PATCH 18/29] run_first lists RUN_FIRST tests with no limit parameter, and the brief's diff cap names the gate's Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- server/code/app/index/review_brief.py | 2 +- server/code/domain/index/test_gate.py | 5 ++--- tests/unit/test_code_test_gate.py | 23 ++++++++++++++--------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index 2a1192e2cbb..dd6008e94e2 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -130,7 +130,7 @@ MAX_SETTINGS = 8 MAX_COVERAGE_SETTINGS = 4 MAX_BLAME_RANGES = 5 -MAX_DIFF_CHARS = 200_000 # CodeImpactInput.diff's cap +MAX_DIFF_CHARS = 200_000 # CodeImpactInput.diff's and CodeTestGateInput.diff's cap # The changed files the brief's impact read seeds from. # not a setting: CodeImpactInput.files' max_length IMPACT_SEED_FILES = 50 diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index daa44979e59..8366af2e01b 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -294,9 +294,8 @@ def run_first( tests_total: int, must_run_total: int, tests_unnamed: int, - limit: int = RUN_FIRST, ) -> dict[str, Any]: - """The first `limit` tests of a gate's `tests` rows (`_test_groups`), in the gate's + """The first `RUN_FIRST` tests of a gate's `tests` rows (`_test_groups`), in the gate's order: every `must_run` row first, the strongest evidence first. A test carries its first row's path, `why`, `evidence` and `must_run`, and its run command; a test of several files names the first `_PATHS_SHOWN` and counts them all. `left_out` counts the @@ -304,7 +303,7 @@ def run_first( `must_run`; `tests_total`, `must_run_total` and `tests_unnamed` are the gate's counts of the rows `tests` was cut from. A row past `tests` and a lead the gate cut are counted whatever their run command, and the cut leads are a lower bound.""" - listed = _test_groups(tests)[:limit] + listed = _test_groups(tests)[:RUN_FIRST] shown = [r for rows in listed for r in rows] return { "tests": [ diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index a6734a05c05..c7fbd39077d 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -233,8 +233,8 @@ def _gate_row(path: str, why: str, must_run: bool) -> dict[str, Any]: def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: """A case's script and its expected output are one test, as are the files of one run - command, under the stronger row; the list stops at `limit` and counts the test files it - leaves out, the gate's cut leads and the rows it did not return among them.""" + command, under the stronger row; the list stops at `RUN_FIRST` and counts the test + files it leaves out, the gate's cut leads and the rows it did not return among them.""" rows = [ _gate_row("tests/cases/a.expected", "changed_test", True), {**_gate_row("tests/cases/a.sh", "changed_test", True), "run": "sh tests/cases/a.sh"}, @@ -243,12 +243,16 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: _gate_row("pkg/store/load_test.go", "test_map", False), _gate_row("tests/unit/test_b.py", "stem_partner", False), _gate_row("tests/unit/test_c.py", "test_map", False), + _gate_row("tests/unit/test_d.py", "test_map", False), + _gate_row("tests/unit/test_e.py", "test_map", False), ] - out = tg.run_first(rows, tests_total=7, must_run_total=4, tests_unnamed=3, limit=3) + out = tg.run_first(rows, tests_total=9, must_run_total=4, tests_unnamed=3) assert [(t["test_path"], t["why"], t["must_run"]) for t in out["tests"]] == [ ("tests/cases/a.expected", "changed_test", True), ("src/x/tests/check_x.cpp", "static_call", True), ("pkg/store/store_test.go", "coverage", True), + ("tests/unit/test_b.py", "stem_partner", False), + ("tests/unit/test_c.py", "test_map", False), ] # the expected output's test is run by its script's command assert out["tests"][0]["test_paths"] == ["tests/cases/a.expected", "tests/cases/a.sh"] @@ -260,14 +264,15 @@ def test_run_first_lists_one_entry_per_test_in_the_gates_order() -> None: ] # a test of one file names no `test_paths` assert "test_paths" not in out["tests"][1] and out["tests"][1]["run_unknown"] - # 5 of the 7 rows are listed; the 3 cut leads are left out too + # 7 of the 9 rows are listed; the 3 cut leads are left out too assert out["left_out"] == 5 and out["must_run_left_out"] == 0 - # with the list shorter than the must-run tests, the rest are counted - one = tg.run_first(rows, tests_total=7, must_run_total=4, tests_unnamed=0, limit=1) - assert one["left_out"] == 5 and one["must_run_left_out"] == 2 + # with more must-run tests than the list holds, the rest are counted + must = [_gate_row(f"pkg/m{i}/m_test.go", "coverage", True) for i in range(7)] + many = tg.run_first(must, tests_total=7, must_run_total=7, tests_unnamed=0) + assert many["left_out"] == 2 and many["must_run_left_out"] == 2 # a gate cut by its own limit: the rows it did not return are left out too - cut = tg.run_first(rows[:2], tests_total=7, must_run_total=4, tests_unnamed=3, limit=3) - assert len(cut["tests"]) == 1 and cut["left_out"] == 8 and cut["must_run_left_out"] == 2 + cut = tg.run_first(rows[:2], tests_total=9, must_run_total=4, tests_unnamed=3) + assert len(cut["tests"]) == 1 and cut["left_out"] == 10 and cut["must_run_left_out"] == 2 def test_run_first_joins_a_command_less_file_to_the_test_of_its_name() -> None: From 062db2518c2c8f8c2c44cad38608fc73f6067f50 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 09:29:38 +0000 Subject: [PATCH 19/29] IMPACT_SEED_FILES names the test gate's file cap at its definition Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- server/code/app/index/review_brief.py | 4 ++-- server/code/app/index/review_brief_build.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index dd6008e94e2..fd7f7b73005 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -131,8 +131,8 @@ MAX_COVERAGE_SETTINGS = 4 MAX_BLAME_RANGES = 5 MAX_DIFF_CHARS = 200_000 # CodeImpactInput.diff's and CodeTestGateInput.diff's cap -# The changed files the brief's impact read seeds from. -# not a setting: CodeImpactInput.files' max_length +# The changed files the brief's impact and test-gate reads seed from. +# not a setting: CodeImpactInput.files' and CodeTestGateInput.files' max_length IMPACT_SEED_FILES = 50 # The rows of code.test_gate the tests to run first are picked from. # not a setting: CodeTestGateInput.limit's maximum diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index 54563001e14..e4d93b8ced8 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -868,7 +868,7 @@ async def _run_first_row( { "org": inp.org, "namespace": inp.ns, - "files": paths[:IMPACT_SEED_FILES], # code.test_gate's cap too + "files": paths[:IMPACT_SEED_FILES], "diff": diff[:MAX_DIFF_CHARS], "limit": GATE_ROWS, }, From 437866a60f01e4654251e605cf452ddcb1185676 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 09:42:56 +0000 Subject: [PATCH 20/29] SEED_FILES names the cap both reads share, deleted tests are read off the diff's headers alone, and a test pins the omitted row Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- server/code/app/index/review_brief.py | 4 +-- server/code/app/index/review_brief_build.py | 30 ++++++++++++++----- server/code/app/review_brief.py | 2 +- server/code/domain/index/test_gate.py | 2 +- tests/unit/test_code_review_brief_sections.py | 14 ++++++++- tests/unit/test_review_brief_rationale.py | 2 +- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index fd7f7b73005..b6faa803012 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -133,7 +133,7 @@ MAX_DIFF_CHARS = 200_000 # CodeImpactInput.diff's and CodeTestGateInput.diff's cap # The changed files the brief's impact and test-gate reads seed from. # not a setting: CodeImpactInput.files' and CodeTestGateInput.files' max_length -IMPACT_SEED_FILES = 50 +SEED_FILES = 50 # The rows of code.test_gate the tests to run first are picked from. # not a setting: CodeTestGateInput.limit's maximum GATE_ROWS = 200 @@ -1197,7 +1197,7 @@ def serve( "EMPTY", "FAILED", "GATE_ROWS", - "IMPACT_SEED_FILES", + "SEED_FILES", "INDEX_ERROR", "NOT_BOUND", "NOT_BUILT", diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index e4d93b8ced8..bbd89b9c4c6 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -16,7 +16,6 @@ EMPTY, FAILED, GATE_ROWS, - IMPACT_SEED_FILES, INDEX_ERROR, MAX_COVERAGE_SETTINGS, MAX_COVERAGE_SYMBOLS, @@ -30,6 +29,7 @@ PATHS_ROWS, PAYLOAD_VERSION, SECTION_ORDER, + SEED_FILES, SERVED, UNDECIDED, UNGRAPHED, @@ -41,6 +41,8 @@ BriefInputs, CallOp, DiffFacts, + _git_header_paths, + _side_path, blame_verdict, canonical, class_member, @@ -64,7 +66,6 @@ ) from server.code.app.symbol.diff import _parse_unified_diff_full, is_blank from server.code.app.symbol.resolve import _parse_file_symbol -from server.code.domain.index.conventions import parse_unified_diff from server.code.domain.index.epistemic import ( CAUSE_NEW_SIDE_FAILED, CAUSE_TRUNCATED, @@ -104,7 +105,7 @@ async def build_brief( { "org": inp.org, "namespace": inp.ns, - "files": paths[:IMPACT_SEED_FILES], + "files": paths[:SEED_FILES], "diff": diff[:MAX_DIFF_CHARS], "depth": 1, "per_hop_limit": 200, @@ -868,7 +869,7 @@ async def _run_first_row( { "org": inp.org, "namespace": inp.ns, - "files": paths[:IMPACT_SEED_FILES], + "files": paths[:SEED_FILES], "diff": diff[:MAX_DIFF_CHARS], "limit": GATE_ROWS, }, @@ -908,10 +909,23 @@ async def _run_first_row( def _deleted_paths(diff: str) -> set[str]: - """The paths of the files a git diff deletes: a `+++ /dev/null` side, or a `deleted file - mode` line for a deletion git prints with no sides (a binary or an empty file).""" - marked = {p for p, f in parse_unified_diff(diff).files.items() if f.status == "D"} - return marked | set(whole_files(diff)) + """The paths of the files a git diff deletes, read off its headers alone: a `+++ + /dev/null` side, or a `deleted file mode` line for a deletion git prints with no sides + (a binary or an empty file).""" + out: set[str] = set() + header = old = "" + for line in diff.split("\n"): + line = line.removesuffix("\r") + if line.startswith("diff --git "): + header, old = line, "" + elif line.startswith("deleted file mode") and header: + out.add(_git_header_paths(header)[0]) + elif line.startswith("--- "): + old = _side_path(line, "a/") + elif line.startswith("+++ ") and old and not _side_path(line, "b/"): + out.add(old) + out.discard("") + return out async def _settings_section( diff --git a/server/code/app/review_brief.py b/server/code/app/review_brief.py index fe757c07a9e..db172f89a38 100644 --- a/server/code/app/review_brief.py +++ b/server/code/app/review_brief.py @@ -364,7 +364,7 @@ async def _rationale_section( # the stored rows are the first `PATHS_ROWS` of `rows_total`; a brief of payload # version 1 stored impact's list, which may stop at its seed files paths_maybe_cut=len(rows) < int(paths_sec.get("rows_total") or 0) - or (int(payload.get("v") or 0) < 2 and len(rows) >= rb.IMPACT_SEED_FILES), + or (int(payload.get("v") or 0) < 2 and len(rows) >= rb.SEED_FILES), # a brief stored without a diff or a file list says nothing of the PR's paths no_paths=not rows, budget_tokens=settings.code_review_brief_rationale_tokens, diff --git a/server/code/domain/index/test_gate.py b/server/code/domain/index/test_gate.py index 8366af2e01b..10e57fe4e3a 100644 --- a/server/code/domain/index/test_gate.py +++ b/server/code/domain/index/test_gate.py @@ -248,7 +248,7 @@ def _links_to(roots: set[str], names: dict[str, str]) -> list[str]: # Tests a brief names to run first (`run_first`). -# not a setting: the length of the list the brief's readers asked for +# not a setting: a list short enough to run before the rest RUN_FIRST = 5 diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index a8312ce7877..7bbe8d49917 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1262,6 +1262,18 @@ async def test_a_gate_that_names_no_test_but_counts_cut_leads_gives_a_row_of_the assert row["tests"] == [] and row["left_out"] == 7 +@pytest.mark.asyncio +async def test_a_gate_that_lists_and_counts_no_test_leaves_the_row_out() -> None: + """Nor does a gate whose only rows are files the PR deletes and that counts no other.""" + empty = _answering(tests=[], tests_total=0, must_run_total=0, tests_unnamed=0) + only_gone = _answering(tests=[_DELETED_ROW], tests_total=1, must_run_total=1, tests_unnamed=0) + for call, inp in ((empty, _OPEN), (only_gone, dataclasses.replace(_OPEN, diff=_DELETING))): + sec = await review_brief_build._tests_section( + call, inp, [], [], [], paths=[], diff=_TEST_DIFF, moved=False + ) + assert [r for r in sec["rows"] if r.get("kind") == "run_first"] == [] + + @pytest.mark.asyncio async def test_a_token_budget_trims_the_tests_to_run_first_last() -> None: """`apply_budget` drops a section's rows from its end, so the first row goes last.""" @@ -1336,7 +1348,7 @@ async def __call__(self, op: str, body: dict[str, Any]) -> dict[str, Any]: return {"verdict": "covered_by", "strongest_tier": "exact", "tests": []} if op != "code.impact": return await super().__call__(op, body) - assert len(body["files"]) == rb.IMPACT_SEED_FILES + assert len(body["files"]) == rb.SEED_FILES assert len(body["diff"]) == rb.MAX_DIFF_CHARS return { "touched_symbols": [], diff --git a/tests/unit/test_review_brief_rationale.py b/tests/unit/test_review_brief_rationale.py index 61a250a4768..952da905564 100644 --- a/tests/unit/test_review_brief_rationale.py +++ b/tests/unit/test_review_brief_rationale.py @@ -772,7 +772,7 @@ def test_the_impact_seed_count_is_code_impacts_own_file_cap() -> None: for m in CodeImpactInput.model_fields["files"].metadata if hasattr(m, "max_length") ] - assert caps == [rb.IMPACT_SEED_FILES] + assert caps == [rb.SEED_FILES] @pytest.mark.asyncio From 1485d68d9f6c35f50ddbd3539a631c6ffe8f5cd9 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 09:54:15 +0000 Subject: [PATCH 21/29] deleted_paths sits with the diff parsers, a brief with no diff marks its run_first row cut, and a test pins the brief's caps to both reads' inputs Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 19 +++++++------- server/code/app/index/review_brief.py | 21 +++++++++++++++ server/code/app/index/review_brief_build.py | 26 +++---------------- tests/unit/test_code_review_brief_sections.py | 3 +++ tests/unit/test_review_brief_rationale.py | 19 +++++++++----- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 166f145721d..3bc1c3247df 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -428,15 +428,16 @@ diff it reads does not carry, or one past the 200 changed paths it reads), the d longer than 200,000 characters, the gate's causes say `truncated` (its call-graph walk or coverage read was cut), or a changed source file is drifted (the index holds it at another revision than the diff's old side, so its lines may map onto other definitions) or the drift -read failed. Its list and counts are then of what it read. The gate reads test links at the -revision the index holds, as the claim rows do, so the brief of a merged PR built after the -index moved past it can name a test added since. A test file the PR deletes is no test to -run: its row is neither listed nor counted. The row is advice no verdict rests on: a read of -the gate that raised keeps the brief unstored, and a refused read, or a gate that lists and -counts no test, leaves the row out. A gate that names no test, or only deleted ones, but -counts other tests or the leads it cut gives a row with no tests and their count in -`left_out`. A brief stored with no `run_first` row says nothing of the tests to run first: -`code.test_gate` lists them. +read failed, or the brief was built with no diff (from its changed paths alone, when it +cannot tell the files the PR deletes). Its list and counts are then of what it read. The +gate reads test links at the revision the index holds, as the claim rows do, so the brief of +a merged PR built after the index moved past it can name a test added since. A test file the +PR deletes is no test to run: its row is neither listed nor counted. The row is advice no +verdict rests on: a read of the gate that raised keeps the brief unstored, and a refused +read, or a gate that lists and counts no test, leaves the row out. A gate that names no +test, or only deleted ones, but counts other tests or the leads it cut gives a row with no +tests and their count in `left_out`. A brief stored with no `run_first` row says nothing of +the tests to run first: `code.test_gate` lists them. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index b6faa803012..cb2b3eb7348 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -861,6 +861,26 @@ def wiring_kind(qualified_name: str, *, lines: int, path: str) -> str: return WIRING_ONE_LINE if lines == 1 else "" +def deleted_paths(diff: str) -> set[str]: + """The paths of the files a git diff deletes, read off its `diff --git`, `---` and + `+++` lines: a `+++ /dev/null` side, or a `deleted file mode` line for a deletion git + prints with no sides (a binary or an empty file).""" + out: set[str] = set() + header = old = "" + for line in diff.split("\n"): + line = line.removesuffix("\r") + if line.startswith("diff --git "): + header, old = line, "" + elif line.startswith("deleted file mode") and header: + out.add(_git_header_paths(header)[0]) + elif line.startswith("--- "): + old = _side_path(line, "a/") + elif line.startswith("+++ ") and old and not _side_path(line, "b/"): + out.add(old) + out.discard("") + return out + + def whole_files(diff: str, *, added: bool = False) -> dict[str, list[str]]: """The lines of each file the diff deletes (`+++ /dev/null`), by its old path, or with `added` of each file it adds (`--- /dev/null`), by its new path.""" @@ -1237,5 +1257,6 @@ def serve( "source_stems", "tier_sections", "whole_files", + "deleted_paths", "wiring_kind", ] diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index bbd89b9c4c6..b76fb32e2e2 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -41,12 +41,11 @@ BriefInputs, CallOp, DiffFacts, - _git_header_paths, - _side_path, blame_verdict, canonical, class_member, decisions_of, + deleted_paths, deleted_ranges, failed_reads, failed_section, @@ -879,7 +878,7 @@ async def _run_first_row( return None, [] degradations.partial("code.review_brief", "test_gate_failed", e) return None, [failure_cause("test_gate", e)] - deleted = _deleted_paths(inp.diff) + deleted = deleted_paths(inp.diff) every = list(gate.get("tests") or []) tests = [t for t in every if t.get("test_path") not in deleted] gone = [t for t in every if t.get("test_path") in deleted] @@ -904,30 +903,11 @@ async def _run_first_row( or len(diff) > MAX_DIFF_CHARS or CAUSE_TRUNCATED in (gate.get("causes") or []) or moved + or not inp.diff ) return {"kind": "run_first", **picked, **({"cut": True} if cut else {})}, [] -def _deleted_paths(diff: str) -> set[str]: - """The paths of the files a git diff deletes, read off its headers alone: a `+++ - /dev/null` side, or a `deleted file mode` line for a deletion git prints with no sides - (a binary or an empty file).""" - out: set[str] = set() - header = old = "" - for line in diff.split("\n"): - line = line.removesuffix("\r") - if line.startswith("diff --git "): - header, old = line, "" - elif line.startswith("deleted file mode") and header: - out.add(_git_header_paths(header)[0]) - elif line.startswith("--- "): - old = _side_path(line, "a/") - elif line.startswith("+++ ") and old and not _side_path(line, "b/"): - out.add(old) - out.discard("") - return out - - async def _settings_section( call: CallOp, inp: BriefInputs, diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 7bbe8d49917..d8c751381d9 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1216,6 +1216,9 @@ async def test_the_tests_to_run_first_say_cut_when_the_gate_read_a_cut_change() assert (await _run_first_of(calls, paths=paths, diff=diff))["cut"] is True assert (await _run_first_of(_answering(causes=["truncated"])))["cut"] is True assert (await _run_first_of(calls, moved=True))["cut"] is True + # a brief built from its changed paths alone cannot tell the files the PR deletes + unread = dataclasses.replace(_inputs(), diff="") + assert (await _run_first_of(calls, unread, paths=["src/a.py"], diff=""))["cut"] is True assert "cut" not in await _run_first_of(_answering(tests_capped=True)) read = await _run_first_of(calls, paths=[*wide[:50], "tests/unit/test_x.py"]) assert "cut" not in read diff --git a/tests/unit/test_review_brief_rationale.py b/tests/unit/test_review_brief_rationale.py index 952da905564..9e062109fd0 100644 --- a/tests/unit/test_review_brief_rationale.py +++ b/tests/unit/test_review_brief_rationale.py @@ -764,15 +764,20 @@ async def _failing_git(*args: Any, **kw: Any) -> Any: assert await gitrepo.run_bounded("/m.git", "diff", max_bytes=100) == ("+partial", False) -def test_the_impact_seed_count_is_code_impacts_own_file_cap() -> None: +def test_the_seed_and_gate_caps_are_code_impacts_and_code_test_gates_own() -> None: + """The brief cuts what it sends to the reads' own input caps: past one, the read is + refused and the brief loses what it would have named.""" from server.code.api.models_symbol import CodeImpactInput + from server.code.api.models_testing import CodeTestGateInput - caps = [ - m.max_length - for m in CodeImpactInput.model_fields["files"].metadata - if hasattr(m, "max_length") - ] - assert caps == [rb.SEED_FILES] + def bound(model: Any, field: str, attr: str) -> list[Any]: + return [getattr(m, attr) for m in model.model_fields[field].metadata if hasattr(m, attr)] + + assert bound(CodeImpactInput, "files", "max_length") == [rb.SEED_FILES] + assert bound(CodeImpactInput, "diff", "max_length") == [rb.MAX_DIFF_CHARS] + assert bound(CodeTestGateInput, "files", "max_length") == [rb.SEED_FILES] + assert bound(CodeTestGateInput, "diff", "max_length") == [rb.MAX_DIFF_CHARS] + assert bound(CodeTestGateInput, "limit", "le") == [rb.GATE_ROWS] @pytest.mark.asyncio From c19c1f473fc57250fe45aa1e22892c3e1b3618d0 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 10:04:27 +0000 Subject: [PATCH 22/29] deleted_paths adds the deleted-file-mode paths to whole_files', and the docs and MCP text say a brief with no diff may list a deleted test Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 20 ++++++++-------- server/code/app/index/review_brief.py | 26 +++++++++------------ server/transport/mcp_descriptions.py | 5 ++-- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 3bc1c3247df..55f1cf8ed88 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -428,16 +428,16 @@ diff it reads does not carry, or one past the 200 changed paths it reads), the d longer than 200,000 characters, the gate's causes say `truncated` (its call-graph walk or coverage read was cut), or a changed source file is drifted (the index holds it at another revision than the diff's old side, so its lines may map onto other definitions) or the drift -read failed, or the brief was built with no diff (from its changed paths alone, when it -cannot tell the files the PR deletes). Its list and counts are then of what it read. The -gate reads test links at the revision the index holds, as the claim rows do, so the brief of -a merged PR built after the index moved past it can name a test added since. A test file the -PR deletes is no test to run: its row is neither listed nor counted. The row is advice no -verdict rests on: a read of the gate that raised keeps the brief unstored, and a refused -read, or a gate that lists and counts no test, leaves the row out. A gate that names no -test, or only deleted ones, but counts other tests or the leads it cut gives a row with no -tests and their count in `left_out`. A brief stored with no `run_first` row says nothing of -the tests to run first: `code.test_gate` lists them. +read failed, or the brief was built with no diff (from its changed paths alone), when its +list may name a file the PR deletes. Its list and counts are then of what it read. The gate +reads test links at the revision the index holds, as the claim rows do, so the brief of a +merged PR built after the index moved past it can name a test added since. A test file the +PR deletes is no test to run: with the diff, its row is neither listed nor counted. The row +is advice no verdict rests on: a read of the gate that raised keeps the brief unstored, and +a refused read, or a gate that lists and counts no test, leaves the row out. A gate that +names no test, or only deleted ones, but counts other tests or the leads it cut gives a row +with no tests and their count in `left_out`. A brief stored with no `run_first` row says +nothing of the tests to run first: `code.test_gate` lists them. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index cb2b3eb7348..675d23debdd 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -862,23 +862,19 @@ def wiring_kind(qualified_name: str, *, lines: int, path: str) -> str: def deleted_paths(diff: str) -> set[str]: - """The paths of the files a git diff deletes, read off its `diff --git`, `---` and - `+++` lines: a `+++ /dev/null` side, or a `deleted file mode` line for a deletion git - prints with no sides (a binary or an empty file).""" - out: set[str] = set() - header = old = "" + """The paths of the files a git diff deletes: a `+++ /dev/null` side (`whole_files`), or + a `deleted file mode` line for a deletion git prints with no sides (a binary or an + empty file).""" + marked: set[str] = set() + header = "" for line in diff.split("\n"): line = line.removesuffix("\r") if line.startswith("diff --git "): - header, old = line, "" + header = line elif line.startswith("deleted file mode") and header: - out.add(_git_header_paths(header)[0]) - elif line.startswith("--- "): - old = _side_path(line, "a/") - elif line.startswith("+++ ") and old and not _side_path(line, "b/"): - out.add(old) - out.discard("") - return out + marked.add(_git_header_paths(header)[0]) + marked.discard("") + return marked | set(whole_files(diff)) def whole_files(diff: str, *, added: bool = False) -> dict[str, list[str]]: @@ -1217,7 +1213,6 @@ def serve( "EMPTY", "FAILED", "GATE_ROWS", - "SEED_FILES", "INDEX_ERROR", "NOT_BOUND", "NOT_BUILT", @@ -1228,6 +1223,7 @@ def serve( "RATIONALE", "RESTRICTED", "SECTION_ORDER", + "SEED_FILES", "SERVED", "SERVED_ORDER", "TIERS", @@ -1243,6 +1239,7 @@ def serve( "checked_names", "class_member", "decisions_of", + "deleted_paths", "deleted_ranges", "failed_reads", "failure_cause", @@ -1257,6 +1254,5 @@ def serve( "source_stems", "tier_sections", "whole_files", - "deleted_paths", "wiring_kind", ] diff --git a/server/transport/mcp_descriptions.py b/server/transport/mcp_descriptions.py index d49a7c565c7..af72b2e7c4f 100644 --- a/server/transport/mcp_descriptions.py +++ b/server/transport/mcp_descriptions.py @@ -1663,8 +1663,9 @@ def revision_policy(update: str) -> str: "files under none of them (code.test_gate lists up to 200 and counts " "the leads it cuts), and `cut: true` when the gate did not read the " "whole change, read a source file the index holds at another " - "revision or the drift read failed, its list and counts then of what " - "it read; a brief with no run_first row says nothing of them; then " + "revision, the drift read failed or the brief had no diff (it then " + "cannot tell deleted tests), its list and counts then of what it " + "read; a brief with no run_first row says nothing of them; then " "coverage_claim per function and setting, its strongest tier " "and that tier's test paths — verdict uncovered | covered | " "partly_covered | unknown — " From 4e114e1ab352653975256e3fe84777da0cf032cf Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 10:13:39 +0000 Subject: [PATCH 23/29] A test file the PR renames away is no test to run first either Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 12 ++++++------ server/code/app/index/review_brief.py | 8 +++++--- tests/unit/test_code_review_brief_sections.py | 12 ++++++++---- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 55f1cf8ed88..460d8906d42 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -432,12 +432,12 @@ read failed, or the brief was built with no diff (from its changed paths alone), list may name a file the PR deletes. Its list and counts are then of what it read. The gate reads test links at the revision the index holds, as the claim rows do, so the brief of a merged PR built after the index moved past it can name a test added since. A test file the -PR deletes is no test to run: with the diff, its row is neither listed nor counted. The row -is advice no verdict rests on: a read of the gate that raised keeps the brief unstored, and -a refused read, or a gate that lists and counts no test, leaves the row out. A gate that -names no test, or only deleted ones, but counts other tests or the leads it cut gives a row -with no tests and their count in `left_out`. A brief stored with no `run_first` row says -nothing of the tests to run first: `code.test_gate` lists them. +PR deletes or renames away is no test to run: with the diff, its row is neither listed nor +counted. The row is advice no verdict rests on: a read of the gate that raised keeps the +brief unstored, and a refused read, or a gate that lists and counts no test, leaves the row +out. A gate that names no test, or only deleted ones, but counts other tests or the leads it +cut gives a row with no tests and their count in `left_out`. A brief stored with no +`run_first` row says nothing of the tests to run first: `code.test_gate` lists them. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/server/code/app/index/review_brief.py b/server/code/app/index/review_brief.py index 675d23debdd..dd3d5ecbbe2 100644 --- a/server/code/app/index/review_brief.py +++ b/server/code/app/index/review_brief.py @@ -862,9 +862,9 @@ def wiring_kind(qualified_name: str, *, lines: int, path: str) -> str: def deleted_paths(diff: str) -> set[str]: - """The paths of the files a git diff deletes: a `+++ /dev/null` side (`whole_files`), or - a `deleted file mode` line for a deletion git prints with no sides (a binary or an - empty file).""" + """The paths of the files a git diff deletes or moves away: a `+++ /dev/null` side + (`whole_files`), a `deleted file mode` line for a deletion git prints with no sides (a + binary or an empty file), or a rename's `rename from` path.""" marked: set[str] = set() header = "" for line in diff.split("\n"): @@ -873,6 +873,8 @@ def deleted_paths(diff: str) -> set[str]: header = line elif line.startswith("deleted file mode") and header: marked.add(_git_header_paths(header)[0]) + elif line.startswith("rename from "): + marked.add(line[len("rename from ") :]) marked.discard("") return marked | set(whole_files(diff)) diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index d8c751381d9..3c8417855e3 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1226,12 +1226,16 @@ async def test_the_tests_to_run_first_say_cut_when_the_gate_read_a_cut_change() @pytest.mark.asyncio async def test_a_test_file_the_pr_deletes_is_neither_listed_nor_counted() -> None: - """Whether git prints the deletion with a `+++ /dev/null` side or, for a binary or an - empty file, with only `deleted file mode`; a gate whose rows are all deleted files - still counts its other tests.""" + """Whether git prints the deletion with a `+++ /dev/null` side, with only `deleted file + mode` (a binary or an empty file), or as a rename away from the path; a gate whose rows + are all deleted files still counts its other tests.""" bare = "diff --git a/tests/unit/test_x.py b/tests/unit/test_x.py\ndeleted file mode 100644\n" + moved = ( + "diff --git a/tests/unit/test_x.py b/tests/unit/test_y.py\nsimilarity index 100%\n" + "rename from tests/unit/test_x.py\nrename to tests/unit/test_y.py\n" + ) with_deleted = _answering(ahead=(_DELETED_ROW,), tests_total=6, must_run_total=2) - for deletion in (_DELETING, bare): + for deletion in (_DELETING, bare, moved): row = await _run_first_of(with_deleted, dataclasses.replace(_OPEN, diff=deletion)) assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] assert row["left_out"] == 6 and row["must_run_left_out"] == 0 From 78d526a3b69bbdde1f825af38b3c51b7b227e44a Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 10:22:59 +0000 Subject: [PATCH 24/29] The run_first docstring and cut comment name renames and a brief with no diff Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- server/code/app/index/review_brief_build.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/code/app/index/review_brief_build.py b/server/code/app/index/review_brief_build.py index b76fb32e2e2..cfcc0c25d74 100644 --- a/server/code/app/index/review_brief_build.py +++ b/server/code/app/index/review_brief_build.py @@ -858,8 +858,9 @@ async def _run_first_row( counts none. The gate reads a diff's old side as the revision the index holds, so `diff` is the one code.impact reads: a merged PR's reversed, an open PR's as written; `moved` says it may have mapped a source file's lines onto rows of another revision. A test file - the PR deletes is no test to run, and its row is neither listed nor counted. A read that - raised only keeps the brief unstored, and a refused one leaves the row out.""" + the PR deletes or renames away is no test to run, and its row is neither listed nor + counted. A read that raised only keeps the brief unstored, and a refused one leaves the + row out.""" if not (paths or diff): return None, [] try: @@ -897,7 +898,8 @@ async def _run_first_row( ) # the gate read a cut change (a changed file it did not read), cut its own walk or # coverage read, or may have mapped a source file's lines onto rows of another revision - # (`moved`): the list and counts are of what it read + # (`moved`), or the brief has no diff to tell the files the PR deletes: the list and + # counts are of what it read cut = ( bool(set(paths) - set(gate.get("changed_files") or [])) or len(diff) > MAX_DIFF_CHARS From 54195b94eb95aa972a4dc5a30b4b3b5b69d6bb2d Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 10:32:01 +0000 Subject: [PATCH 25/29] The docs limit the deleted-test rule to the rows the brief reads Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 13 +++++++------ tests/unit/test_code_review_brief_sections.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 460d8906d42..dee3062f356 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -432,12 +432,13 @@ read failed, or the brief was built with no diff (from its changed paths alone), list may name a file the PR deletes. Its list and counts are then of what it read. The gate reads test links at the revision the index holds, as the claim rows do, so the brief of a merged PR built after the index moved past it can name a test added since. A test file the -PR deletes or renames away is no test to run: with the diff, its row is neither listed nor -counted. The row is advice no verdict rests on: a read of the gate that raised keeps the -brief unstored, and a refused read, or a gate that lists and counts no test, leaves the row -out. A gate that names no test, or only deleted ones, but counts other tests or the leads it -cut gives a row with no tests and their count in `left_out`. A brief stored with no -`run_first` row says nothing of the tests to run first: `code.test_gate` lists them. +PR deletes or renames away is no test to run: with the diff, its row among those the brief +reads is neither listed nor counted. The row is advice no verdict rests on: a read of the +gate that raised keeps the brief unstored, and a refused read, or a gate that lists and +counts no test, leaves the row out. A gate that names no test, or only deleted ones, but +counts other tests or the leads it cut gives a row with no tests and their count in +`left_out`. A brief stored with no `run_first` row says nothing of the tests to run first: +`code.test_gate` lists them. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier diff --git a/tests/unit/test_code_review_brief_sections.py b/tests/unit/test_code_review_brief_sections.py index 3c8417855e3..080436f5a56 100644 --- a/tests/unit/test_code_review_brief_sections.py +++ b/tests/unit/test_code_review_brief_sections.py @@ -1230,12 +1230,12 @@ async def test_a_test_file_the_pr_deletes_is_neither_listed_nor_counted() -> Non mode` (a binary or an empty file), or as a rename away from the path; a gate whose rows are all deleted files still counts its other tests.""" bare = "diff --git a/tests/unit/test_x.py b/tests/unit/test_x.py\ndeleted file mode 100644\n" - moved = ( + renamed = ( "diff --git a/tests/unit/test_x.py b/tests/unit/test_y.py\nsimilarity index 100%\n" "rename from tests/unit/test_x.py\nrename to tests/unit/test_y.py\n" ) with_deleted = _answering(ahead=(_DELETED_ROW,), tests_total=6, must_run_total=2) - for deletion in (_DELETING, bare, moved): + for deletion in (_DELETING, bare, renamed): row = await _run_first_of(with_deleted, dataclasses.replace(_OPEN, diff=deletion)) assert "tests/unit/test_x.py" not in [t["test_path"] for t in row["tests"]] assert row["left_out"] == 6 and row["must_run_left_out"] == 0 From 17d193c144e70d89e10e17320b714ba41b3212ba Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 10:48:43 +0000 Subject: [PATCH 26/29] The fan-out paragraph wraps at 92 columns after the advice clause Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 922a534f63d..7cd1c0f421b 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -303,17 +303,17 @@ stores its first 200 changed paths as rows; its `rows_total`, verdict and causes every changed path. The `rationale` section is never stored, so a read that does not serve it (the trivial tier, or `sections` without it) answers `gaps`, `unsettled` with `rationale:not_requested`, or `no_source_change`: `covered_no_gaps` needs `rationale` in -`sections` (or `sections` empty) on the standard or full tier. A brief one of whose -reads raised is served and not stored, and the next read builds it again (the test home -and the tests to run first are advice: a read of one that raised or was refused decides no -verdict, though one that raised still keeps the brief unstored); so is an open PR's brief built without its diff, -or from an overlay that was off (`overlay:error` when git could not read its diff), left -a changed file unreached when its parse time was spent or its parse could not run here, -or parsed another head. A parse of a file that raised or ran past its limits does the -same on every build of that file, at an open PR's head and at a merged PR's parent -alike, so that brief is stored, `unsettled`: `code.impact` names `new_side_unread` for -the file, and an open PR's `overlay_note` counts it. A build that replaces a brief of an -older version is tried once (below). +`sections` (or `sections` empty) on the standard or full tier. A brief one of whose reads +raised is served and not stored, and the next read builds it again (the test home and the +tests to run first are advice: a read of one that raised or was refused decides no verdict, +though one that raised still keeps the brief unstored); so is an open PR's brief built +without its diff, or from an overlay that was off (`overlay:error` when git could not read +its diff), left a changed file unreached when its parse time was spent or its parse could +not run here, or parsed another head. A parse of a file that raised or ran past its limits +does the same on every build of that file, at an open PR's head and at a merged PR's parent +alike, so that brief is stored, `unsettled`: `code.impact` names `new_side_unread` for the +file, and an open PR's `overlay_note` counts it. A build that replaces a brief of an older +version is tried once (below). A change to what a brief stores or how its verdict reads it raises the payload version (`v` in the payload). A stored brief of the current version is served as it was built. From 1f888d177dc8aade322c68aab71eaecae66b3fbc Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 10:56:56 +0000 Subject: [PATCH 27/29] The docs say a changed file past the first 50 is a cut only when the gate's diff lacks its hunks Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 32 ++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 7cd1c0f421b..00e71b56643 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -473,22 +473,22 @@ none of the five, and `must_run_left_out` how many of them say `must_run`. A fil 200 rows and a lead the gate cut are counted whatever their run command, and the gate's count of cut leads is a lower bound. The row says `cut: true` when the gate did not read the whole change, or read a changed source file on another revision: a changed file is missing -from the gate's `changed_files` (one it was not sent, past the first 50, whose hunks the -diff it reads does not carry, or one past the 200 changed paths it reads), the diff is -longer than 200,000 characters, the gate's causes say `truncated` (its call-graph walk or -coverage read was cut), or a changed source file is drifted (the index holds it at another -revision than the diff's old side, so its lines may map onto other definitions) or the drift -read failed, or the brief was built with no diff (from its changed paths alone), when its -list may name a file the PR deletes. Its list and counts are then of what it read. The gate -reads test links at the revision the index holds, as the claim rows do, so the brief of a -merged PR built after the index moved past it can name a test added since. A test file the -PR deletes or renames away is no test to run: with the diff, its row among those the brief -reads is neither listed nor counted. The row is advice no verdict rests on: a read of the -gate that raised keeps the brief unstored, and a refused read, or a gate that lists and -counts no test, leaves the row out. A gate that names no test, or only deleted ones, but -counts other tests or the leads it cut gives a row with no tests and their count in -`left_out`. A brief stored with no `run_first` row says nothing of the tests to run first: -`code.test_gate` lists them. +from the gate's `changed_files` (one past the first 50 it is sent whose hunks the diff it +reads does not carry, or one past the 200 changed paths it reads), the diff is longer than +200,000 characters, the gate's causes say `truncated` (its call-graph walk or coverage read +was cut), or a changed source file is drifted (the index holds it at another revision than +the diff's old side, so its lines may map onto other definitions) or the drift read failed, +or the brief was built with no diff (from its changed paths alone), when its list may name a +file the PR deletes. Its list and counts are then of what it read. The gate reads test links +at the revision the index holds, as the claim rows do, so the brief of a merged PR built +after the index moved past it can name a test added since. A test file the PR deletes or +renames away is no test to run: with the diff, its row among those the brief reads is +neither listed nor counted. The row is advice no verdict rests on: a read of the gate that +raised keeps the brief unstored, and a refused read, or a gate that lists and counts no +test, leaves the row out. A gate that names no test, or only deleted ones, but counts other +tests or the leads it cut gives a row with no tests and their count in `left_out`. A brief +stored with no `run_first` row says nothing of the tests to run first: `code.test_gate` +lists them. Each `tests` row of a symbol or setting carries the coverage claim's `verdict`, its `strongest_tier` (`code.coverage_claim`'s evidence tiers), up to two `tests` of that tier From 6bbac07e7e1b6e94114ae6e52fda931d240d89e1 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 11:05:59 +0000 Subject: [PATCH 28/29] The docs state the changed-files cut as one rule: a changed file the gate did not read Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 00e71b56643..963d145c509 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -473,12 +473,11 @@ none of the five, and `must_run_left_out` how many of them say `must_run`. A fil 200 rows and a lead the gate cut are counted whatever their run command, and the gate's count of cut leads is a lower bound. The row says `cut: true` when the gate did not read the whole change, or read a changed source file on another revision: a changed file is missing -from the gate's `changed_files` (one past the first 50 it is sent whose hunks the diff it -reads does not carry, or one past the 200 changed paths it reads), the diff is longer than -200,000 characters, the gate's causes say `truncated` (its call-graph walk or coverage read -was cut), or a changed source file is drifted (the index holds it at another revision than -the diff's old side, so its lines may map onto other definitions) or the drift read failed, -or the brief was built with no diff (from its changed paths alone), when its list may name a +from the gate's `changed_files`, the files it read, the diff is longer than 200,000 +characters, the gate's causes say `truncated` (its call-graph walk or coverage read was +cut), or a changed source file is drifted (the index holds it at another revision than the +diff's old side, so its lines may map onto other definitions) or the drift read failed, or +the brief was built with no diff (from its changed paths alone), when its list may name a file the PR deletes. Its list and counts are then of what it read. The gate reads test links at the revision the index holds, as the claim rows do, so the brief of a merged PR built after the index moved past it can name a test added since. A test file the PR deletes or From a5f5e77f904ac4d1dc48aed6b8cec0fe73cb11ea Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sun, 6 Dec 2026 11:06:29 +0000 Subject: [PATCH 29/29] The changed-files cut names the gate's read set in parentheses Co-Authored-By: Claude Opus 5.5 Signed-off-by: zlareb1 --- docs/features/code-index/history-and-prs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/code-index/history-and-prs.md b/docs/features/code-index/history-and-prs.md index 963d145c509..c447765e465 100644 --- a/docs/features/code-index/history-and-prs.md +++ b/docs/features/code-index/history-and-prs.md @@ -473,7 +473,7 @@ none of the five, and `must_run_left_out` how many of them say `must_run`. A fil 200 rows and a lead the gate cut are counted whatever their run command, and the gate's count of cut leads is a lower bound. The row says `cut: true` when the gate did not read the whole change, or read a changed source file on another revision: a changed file is missing -from the gate's `changed_files`, the files it read, the diff is longer than 200,000 +from the gate's `changed_files` (the files it read), the diff is longer than 200,000 characters, the gate's causes say `truncated` (its call-graph walk or coverage read was cut), or a changed source file is drifted (the index holds it at another revision than the diff's old side, so its lines may map onto other definitions) or the drift read failed, or