test_dossier_batch.py

tests/test_dossier_batch.py

Tests for the BATCHED dossier — several symbols, one call, one shared byte budget.

test_a_batch_returns_the_same_payload_per_symbol_as_the_single_calls

None test_dossier_batch.test_a_batch_returns_the_same_payload_per_symbol_as_the_single_calls(Path rich_db)

Each batch entry matches its single-call dossier.

tests/test_dossier_batch.py:69

Show on the map →

Called by none

Calls _tools, list

Checks

  • assert batch["kind"] == "dossiers"
  • assert batch["count"] == len(INDEXED)
  • assert batch["found"] == len(INDEXED)
  • assert [e["name"] for e in batch["results"]] == list(INDEXED)
  • assert entry.get(panel) == single.get(panel), panel
  • assert entry.get(ident) == single.get(ident), ident

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measured
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-002measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-004measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-009measured
REQ-DDB-QUERY-010measured
REQ-DDB-QUERY-012measured
REQ-DDB-QUERY-014measured
REQ-DDB-SCHEMA-011measured
Source · 28 lines
def test_a_batch_returns_the_same_payload_per_symbol_as_the_single_calls(rich_db: Path) -> None:
    """THE WHOLE PREMISE. A batch is only a saving if it is the same answer in fewer
    turns; if the batched entry differs from the single-call payload the saving is
    bought with data the caller then has to go back for, which is a net loss of a turn
    rather than a gain of four.

    Compared field by field over the panels that carry rows, because comparing whole
    dicts would fail on the envelope keys a single reply carries and a batch entry
    deliberately does not (`target`, stamped once for the batch).

    @brief Each batch entry matches its single-call dossier.
    @version 1
    """
    tools = _tools(rich_db)
    singles = [tools.dossier(n) for n in INDEXED]
    batch = tools.dossier(list(INDEXED))

    assert batch["kind"] == "dossiers"
    assert batch["count"] == len(INDEXED)
    assert batch["found"] == len(INDEXED)
    assert [e["name"] for e in batch["results"]] == list(INDEXED)
    for single, entry in zip(singles, batch["results"], strict=True):
        for panel in ("callers", "callees", "writes", "reads", "requirements"):
            assert entry.get(panel) == single.get(panel), panel
        for ident in ("name", "signature", "file", "brief", "liveness"):
            assert entry.get(ident) == single.get(ident), ident

Activity · 12 steps
lines 82–82tools = _tools(rich_db)lines 83–83singles = [tools.dossier(n) for n in INDEXED]lines 84–84batch = tools.dossier(list(INDEXED))lines 86–86assert batch["kind"] == "dossiers"lines 87–87assert batch["count"] == len(INDEXED)lines 88–88assert batch["found"] == len(INDEXED)lines 89–89assert [e["name"] for e in batch["results"]] == list(INDEXED)lines 90–90◇ for single, entry in zip(singles, batch["results"], strict=True)lines 91–91◇ for panel in ("callers", "callees", "writes", "reads", "requirements")lines 92–92assert entry.get(panel) == single.get(panel), panellines 93–93◇ for ident in ("name", "signature", "file", "brief", "liveness")lines 94–94assert entry.get(ident) == single.get(ident), ident

test_a_qualified_argument_beside_a_list_is_refused_not_ignored

None test_dossier_batch.test_a_qualified_argument_beside_a_list_is_refused_not_ignored(Path rich_db)

A list plus `qualified` raises.

tests/test_dossier_batch.py:123

Show on the map →

Called by none

Calls _tools, list

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-010measured
Source · 13 lines
def test_a_qualified_argument_beside_a_list_is_refused_not_ignored(rich_db: Path) -> None:
    """`qualified` names ONE identity among namesakes. Applied to a list it would scope
    four dossiers by a key belonging to the fifth; ignored, the caller who asked for a
    specific identity would receive an arbitrary one and no indication of it. Both silent
    options produce a confidently mislabelled answer, so the call is refused.

    @brief A list plus `qualified` raises.
    @version 1
    """
    with pytest.raises(ValueError, match="cannot be combined with a list"):
        _tools(rich_db).dossier(list(INDEXED), qualified="some::identity")

Activity · 2 steps
lines 132–132with pytest.raises(ValueError, match="cannot be combined with a list")lines 133–133_tools(rich_db).dossier(list(INDEXED), qualified="some::identity")

test_a_repeated_name_is_answered_once

None test_dossier_batch.test_a_repeated_name_is_answered_once(Path rich_db)

Duplicates collapse to one entry.

tests/test_dossier_batch.py:170

Show on the map →

Called by none

Calls _tools

Checks

  • assert [e["name"] for e in batch["results"]] == [INDEXED[0], INDEXED[1]]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measured
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-002measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-004measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-009measured
REQ-DDB-QUERY-010measured
REQ-DDB-QUERY-012measured
REQ-DDB-QUERY-014measured
REQ-DDB-SCHEMA-011measured
Source · 11 lines
def test_a_repeated_name_is_answered_once(rich_db: Path) -> None:
    """A duplicate costs a whole dossier of the shared budget to say the same thing
    twice, and the entry it occupies is better spent on a symbol that was asked about.

    @brief Duplicates collapse to one entry.
    @version 1
    """
    batch = _tools(rich_db).dossier([INDEXED[0], INDEXED[0], INDEXED[1]])
    assert [e["name"] for e in batch["results"]] == [INDEXED[0], INDEXED[1]]

Activity · 2 steps
lines 177–177batch = _tools(rich_db).dossier([INDEXED[0], INDEXED[0], INDEXED[1]])lines 178–178assert [e["name"] for e in batch["results"]] == [INDEXED[0], INDEXED[1]]

test_a_single_string_argument_is_unchanged_by_the_batch_path

None test_dossier_batch.test_a_single_string_argument_is_unchanged_by_the_batch_path(Path rich_db)

The single-symbol form still returns a bare dossier.

tests/test_dossier_batch.py:181

Show on the map →

Called by none

Calls _tools

Checks

  • assert single["name"] == INDEXED[0]
  • assert single["kind"] != "dossiers"
  • assert "results" not in single

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measured
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-002measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-004measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-009measured
REQ-DDB-QUERY-010measured
REQ-DDB-QUERY-012measured
REQ-DDB-QUERY-014measured
REQ-DDB-SCHEMA-011measured
Source · 13 lines
def test_a_single_string_argument_is_unchanged_by_the_batch_path(rich_db: Path) -> None:
    """The common case and every existing caller. A polymorphic first argument is only
    safe if the string branch is byte-identical to what it always returned.

    @brief The single-symbol form still returns a bare dossier.
    @version 1
    """
    single = _tools(rich_db).dossier(INDEXED[0])
    assert single["name"] == INDEXED[0]
    assert single["kind"] != "dossiers"
    assert "results" not in single

Activity · 4 steps
lines 188–188single = _tools(rich_db).dossier(INDEXED[0])lines 189–189assert single["name"] == INDEXED[0]lines 190–190assert single["kind"] != "dossiers"lines 191–191assert "results" not in single

test_an_empty_list_is_refused_rather_than_answered_with_zero_results

None test_dossier_batch.test_an_empty_list_is_refused_rather_than_answered_with_zero_results(Path rich_db)

An empty list raises.

tests/test_dossier_batch.py:157

Show on the map →

Called by none

Calls _tools

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-010measured
Source · 13 lines
def test_an_empty_list_is_refused_rather_than_answered_with_zero_results(
    rich_db: Path,
) -> None:
    """`count: 0` to a request that asked for nothing reads as "none of your symbols is
    indexed", which is a measurement the database never made.

    @brief An empty list raises.
    @version 1
    """
    with pytest.raises(ValueError, match="at least one"):
        _tools(rich_db).dossier([])

Activity · 2 steps
lines 166–166with pytest.raises(ValueError, match="at least one")lines 167–167_tools(rich_db).dossier([])

test_n_dossiers_share_the_budget_instead_of_each_claiming_it

None test_dossier_batch.test_n_dossiers_share_the_budget_instead_of_each_claiming_it()

A batch of oversized dossiers still fits the single-response cap.

tests/test_dossier_batch.py:194

Show on the map →

Called by none

Calls _budget_batch, _size, all

Checks

  • assert sum(_size(e) for e in entries) > RESPONSE_BUDGET_BYTES * 4
  • assert sum(_size(e) for e in entries) + 200 <= RESPONSE_BUDGET_BYTES
  • assert limited is not None
  • assert set(limited["adjusted"]) == {f"fat_{i}.callers" for i in range(5)}
  • assert all(f"{rows} ->" in v for v in limited["adjusted"].values())

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-WIKI-054inferredtest_n_dossiers_share_the_budget_instead_of_each_claiming_it → all
Source · 39 lines
def test_n_dossiers_share_the_budget_instead_of_each_claiming_it() -> None:
    """THE FAILURE THIS PREVENTS IS ARITHMETIC. Trimming each entry to the whole-response
    ceiling would produce a reply that respects `RESPONSE_BUDGET_BYTES` N times over and
    exceeds it once — and nothing downstream would notice, because every individual
    payload passed its own check.

    Driven through `_budget_batch` with synthetic entries rather than a real index,
    because the fixture repository is too small to overflow the cap and a mechanism only
    exercised on a target nobody runs in CI is a mechanism nobody tests.

    THE FIXTURE IS DERIVED FROM THE CAP, NOT HARDCODED. A literal row count silently stops
    testing the mechanism the moment the cap moves: at 400 rows this test overflowed a
    32,768-byte budget and merely fitted a 65,536-byte one, so raising the cap turned the
    oversize PRECONDITION into the failure — the assertion that fired was the guard, not
    the property. The cap is documented as PROVISIONAL and expected to move again, so the
    fixture scales with it and the guard below stays meaningful at any value.

    @brief A batch of oversized dossiers still fits the single-response cap.
    @version 2
    """
    ## Sized so ONE entry alone overflows the whole budget, which makes five of them
    ## comfortably clear the 4x guard however the serialiser's per-row overhead drifts.
    rows = RESPONSE_BUDGET_BYTES // 64
    entries = [
        {"name": f"fat_{i}", "callers": [{"name": f"c{j}" * 12, "line": j} for j in range(rows)]}
        for i in range(5)
    ]
    assert sum(_size(e) for e in entries) > RESPONSE_BUDGET_BYTES * 4

    limited = _budget_batch(entries, overhead=200)

    assert sum(_size(e) for e in entries) + 200 <= RESPONSE_BUDGET_BYTES
    assert limited is not None
    ## NAMES THE SYMBOL, not just the field: in a one-symbol reply `_limited: {callers}`
    ## is unambiguous, in a batch it is useless.
    assert set(limited["adjusted"]) == {f"fat_{i}.callers" for i in range(5)}
    assert all(f"{rows} ->" in v for v in limited["adjusted"].values())

Activity · 8 steps
lines 216–216rows = RESPONSE_BUDGET_BYTES // 64lines 217–220entries = [ {"name": f"fat_{i}", "callers": [{"name": f"c{j}" * 12, "line": j} for j in range(rows)]} for i in range(5) ]lines 221–221assert sum(_size(e) for e in entries) > RESPONSE_BUDGET_BYTES * 4lines 223–223limited = _budget_batch(entries, overhead=200)lines 225–225assert sum(_size(e) for e in entries) + 200 <= RESPONSE_BUDGET_BYTESlines 226–226assert limited is not Nonelines 229–229assert set(limited["adjusted"]) == {f"fat_{i}.callers" for i in range(5)}lines 230–230assert all(f"{rows} ->" in v for v in limited["adjusted"].values())

test_one_unresolvable_name_misses_in_its_own_slot_and_the_rest_still_answer

None test_dossier_batch.test_one_unresolvable_name_misses_in_its_own_slot_and_the_rest_still_answer(Path rich_db)

A miss occupies its own slot and does not fail the batch.

tests/test_dossier_batch.py:97

Show on the map →

Called by none

Calls _tools

Checks

  • assert [e["name"] for e in batch["results"]] == names
  • assert batch["count"] == 3
  • assert batch["found"] == 2
  • assert batch["results"][1]["found"] is False
  • assert "not an error" in batch["results"][1]["note"]
  • assert batch["results"][0].get("found") is None
  • assert batch["results"][2]["signature"]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measured
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-002measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-004measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-009measured
REQ-DDB-QUERY-010measured
REQ-DDB-QUERY-012measured
REQ-DDB-QUERY-013measured
REQ-DDB-QUERY-014measured
REQ-DDB-SCHEMA-011measured
Source · 26 lines
def test_one_unresolvable_name_misses_in_its_own_slot_and_the_rest_still_answer(
    rich_db: Path,
) -> None:
    """POSITIONAL, INCLUDING THE MISSES. Dropping the miss would silently re-align every
    entry after it, so a reader pairing names with answers by position would attribute
    the third symbol's dossier to the second name — a wrong answer delivered with full
    confidence, which is worse than the miss it hides.

    The miss is placed in the MIDDLE deliberately: a miss at the end is caught by a
    length check alone, and a length check is what a dropped-and-shifted list passes.

    @brief A miss occupies its own slot and does not fail the batch.
    @version 1
    """
    names = [INDEXED[0], MISSING, INDEXED[1]]
    batch = _tools(rich_db).dossier(names)

    assert [e["name"] for e in batch["results"]] == names
    assert batch["count"] == 3
    assert batch["found"] == 2
    assert batch["results"][1]["found"] is False
    assert "not an error" in batch["results"][1]["note"]
    assert batch["results"][0].get("found") is None
    assert batch["results"][2]["signature"]

Activity · 9 steps
lines 111–111names = [INDEXED[0], MISSING, INDEXED[1]]lines 112–112batch = _tools(rich_db).dossier(names)lines 114–114assert [e["name"] for e in batch["results"]] == nameslines 115–115assert batch["count"] == 3lines 116–116assert batch["found"] == 2lines 117–117assert batch["results"][1]["found"] is Falselines 118–118assert "not an error" in batch["results"][1]["note"]lines 119–119assert batch["results"][0].get("found") is Nonelines 120–120assert batch["results"][2]["signature"]

test_over_the_batch_cap_answers_the_first_n_and_names_the_rest

None test_dossier_batch.test_over_the_batch_cap_answers_the_first_n_and_names_the_rest(Path rich_db)

Over the cap answers the first N and names the rest; at the cap names nothing.

tests/test_dossier_batch.py:136

Show on the map →

Called by none

Calls _tools

Checks

  • assert reply["count"] == q.MAX_BATCH_SYMBOLS
  • assert reply["subject"] == over[: q.MAX_BATCH_SYMBOLS]
  • assert reply["not_answered"] == over[q.MAX_BATCH_SYMBOLS :]
  • assert "re-ask" in reply["not_answered_means"]
  • assert at_cap["count"] == q.MAX_BATCH_SYMBOLS and "not_answered" not in at_cap

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-003measured
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-004measured
REQ-DDB-QUERY-010measured
REQ-DDB-QUERY-012measured
REQ-DDB-QUERY-013measured
REQ-DDB-QUERY-014measured
Source · 21 lines
def test_over_the_batch_cap_answers_the_first_n_and_names_the_rest(rich_db: Path) -> None:
    """THE RULE CHANGED BY OWNER RULING (2026-11-13, 1.3.10): an over-cap batch used to be
    REFUSED, which cost the agent a second call every time (2 of 188 mbedtls calls in the 1.3.0
    grid). It now answers the first `MAX_BATCH_SYMBOLS` and names every subject it did not answer
    in `not_answered` — the disclosure that kept the old rule's purpose: a truncated request must
    never read as a complete one. At the cap nothing is named.

    @brief Over the cap answers the first N and names the rest; at the cap names nothing.
    @version 2
    """
    tools = _tools(rich_db)
    over = [f"{INDEXED[0]}_{i}" for i in range(q.MAX_BATCH_SYMBOLS + 2)]
    reply = tools.dossier(over)
    assert reply["count"] == q.MAX_BATCH_SYMBOLS
    assert reply["subject"] == over[: q.MAX_BATCH_SYMBOLS]
    assert reply["not_answered"] == over[q.MAX_BATCH_SYMBOLS :]
    assert "re-ask" in reply["not_answered_means"]
    at_cap = tools.dossier(over[: q.MAX_BATCH_SYMBOLS])
    assert at_cap["count"] == q.MAX_BATCH_SYMBOLS and "not_answered" not in at_cap

Activity · 9 steps
lines 146–146tools = _tools(rich_db)lines 147–147over = [f"{INDEXED[0]}_{i}" for i in range(q.MAX_BATCH_SYMBOLS + 2)]lines 148–148reply = tools.dossier(over)lines 149–149assert reply["count"] == q.MAX_BATCH_SYMBOLSlines 150–150assert reply["subject"] == over[: q.MAX_BATCH_SYMBOLS]lines 151–151assert reply["not_answered"] == over[q.MAX_BATCH_SYMBOLS :]lines 152–152assert "re-ask" in reply["not_answered_means"]lines 153–153at_cap = tools.dossier(over[: q.MAX_BATCH_SYMBOLS])lines 154–154assert at_cap["count"] == q.MAX_BATCH_SYMBOLS and "not_answered" not in at_cap

test_r2_dossiers_is_positional_and_matches_the_single_symbol_function

None test_dossier_batch.test_r2_dossiers_is_positional_and_matches_the_single_symbol_function(Path rich_db)

R2 `dossiers` is positional and agrees with `dossier`.

tests/test_dossier_batch.py:273

Checks

  • assert len(built) == len(names)
  • assert built[1] is None
  • assert doss == q.function_dossier(rich_db, name)

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → symbol_provenance
REQ-DDB-PIPE-003inferredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → _body_and_external → external_callees → _harvest_file → _ast_harvest_calls → is_python_tree
REQ-DDB-PIPE-011inferredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → _body_and_external → external_callees → _harvest_file → _ast_parse_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → _body_and_external → external_callees → _ts_language_for → _grammar_available → has_language
REQ-DDB-QUERY-001measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → connect
REQ-DDB-QUERY-002measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → unresolved_inbound_for_rowids
REQ-DDB-QUERY-003measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → function_candidates
REQ-DDB-QUERY-004measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier
REQ-DDB-QUERY-006measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → gates_covering
REQ-DDB-QUERY-009measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier
REQ-DDB-QUERY-010measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier
REQ-DDB-QUERY-012measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → _macro_dossier
REQ-DDB-QUERY-013inferredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → config_defaults_conn
REQ-DDB-SCHEMA-011measuredtest_r2_dossiers_is_positional_and_matches_the_single_symbol_function → function_dossier → _dossier_conn → locks_held_for_rowids
Source · 23 lines
def test_r2_dossiers_is_positional_and_matches_the_single_symbol_function(
    rich_db: Path,
) -> None:
    """The library contract underneath the tool: one entry per requested name, in
    request order, `None` where a name resolves to nothing — and each one equal to what
    the single-symbol `dossier` would have built for it.

    Guards the split that makes the batch cheap. `_dossier_conn` was extracted so five
    symbols share one connection; if that extraction changed an answer, the saving would
    have been bought with correctness.

    @brief R2 `dossiers` is positional and agrees with `dossier`.
    @version 1
    """
    names = [INDEXED[0], MISSING, INDEXED[1]]
    built = q.function_dossiers(rich_db, names)

    assert len(built) == len(names)
    assert built[1] is None
    for name, doss in zip(names, built, strict=True):
        assert doss == q.function_dossier(rich_db, name)

Activity · 6 steps
lines 287–287names = [INDEXED[0], MISSING, INDEXED[1]]lines 288–288built = q.function_dossiers(rich_db, names)lines 290–290assert len(built) == len(names)lines 291–291assert built[1] is Nonelines 292–292◇ for name, doss in zip(names, built, strict=True)lines 293–293assert doss == q.function_dossier(rich_db, name)

test_the_chain_loses_its_outermost_ring_before_a_neighbour_list_is_trimmed

None test_dossier_batch.test_the_chain_loses_its_outermost_ring_before_a_neighbour_list_is_trimmed()

Depth is spent before rows, and hops leave with their nodes.

tests/test_dossier_batch.py:296

Show on the map →

Called by none

Calls _shrink_to_budget, _size, all

Checks

  • assert len(payload["callers"]) == 8, ("the immediate neighbours are the DEAREST rows and must survive a chain that " "could have been dropped instead" )
  • assert cut.get("chain_depth") == 3, "the dropped depth must be reported as a depth"
  • assert all(n["depth"] < 3 for n in payload["chain"]["nodes"])
  • assert payload["chain"]["hops"] == []
  • assert _size(payload) <= RESPONSE_BUDGET_BYTES

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-WIKI-054inferredtest_the_chain_loses_its_outermost_ring_before_a_neighbour_list_is_trimmed → all
Source · 45 lines
def test_the_chain_loses_its_outermost_ring_before_a_neighbour_list_is_trimmed() -> None:
    """gh#383, and the defect is sharper than "step depth before halving". `_DOSSIER_LISTS`
    holds TOP-LEVEL keys and `payload["chain"]` is a NESTED dict of nodes/hops, so
    `_shrink_to_budget` could not see the chain at all: an oversized traversal was carried in
    full while `callers` and `callees` — the highest-value rows in the reply — were halved to
    pay for it. Exactly backwards. Depth 1 is adjacency, which the populated section already
    carries; depth 2+ is traversal, and fan-out is roughly geometric, so the outermost ring is
    both the cheapest thing to lose and usually the largest.

    THE ORDERING IS THE CLAIM, so it is what is asserted: with a chain big enough to blow the
    budget on its own, the neighbour list must come through UNTOUCHED. A test that only
    checked "the payload fits" would pass on the old behaviour too.

    HOPS ARE PRUNED WITH THEIR NODES. A hop whose endpoint left would assert an edge into
    nothing, which is a worse answer than a smaller chain.

    @brief Depth is spent before rows, and hops leave with their nodes.
    @version 1
    """
    payload: dict = {
        "subject": "seed",
        "callers": [{"name": f"caller_{i}"} for i in range(8)],
        "chain": {
            "seed": "seed",
            "nodes": [{"name": "seed", "depth": 1}]
            + [{"name": f"far_{i}" * 30, "depth": 3} for i in range(400)],
            "hops": [
                {"edge_class": "call", "from_name": "seed", "to_name": f"far_{i}" * 30}
                for i in range(400)
            ],
        },
    }
    cut = _shrink_to_budget(payload, ("callers",))

    assert len(payload["callers"]) == 8, (
        "the immediate neighbours are the DEAREST rows and must survive a chain that "
        "could have been dropped instead"
    )
    assert cut.get("chain_depth") == 3, "the dropped depth must be reported as a depth"
    assert all(n["depth"] < 3 for n in payload["chain"]["nodes"])
    ## Every hop pointed at a depth-3 node, so all of them must go with it.
    assert payload["chain"]["hops"] == []
    assert _size(payload) <= RESPONSE_BUDGET_BYTES

Activity · 7 steps
lines 315–327payload: dict = { "subject": "seed", "callers": [{"name": f"caller_{i}"} for i in range(8)], "chain": { "seed": "seed", "nodes": [{"name": "seed", "depth": 1}] + [{"name": f"far_{i}" * 30, "depth": 3} for i in range(400)], "hops": [ {"edge_class": "call", "from_name": "seed", "to_name": f"far_{i}" * 30} for i in range(400) ], }, }lines 328–328cut = _shrink_to_budget(payload, ("callers",))lines 330–333assert len(payload["callers"]) == 8, ( "the immediate neighbours are the DEAREST rows and must survive a chain that " "could have been dropped instead" )lines 334–334assert cut.get("chain_depth") == 3, "the dropped depth must be reported as a depth"lines 335–335assert all(n["depth"] < 3 for n in payload["chain"]["nodes"])lines 337–337assert payload["chain"]["hops"] == []lines 338–338assert _size(payload) <= RESPONSE_BUDGET_BYTES