<!-- informative -->

# Re-review 2: Bounded Recursive-Planning Pilot — the Model Producer at `ac26235`

Second verification pass, of the corrective commit `ac26235` ("Clear the producer
re-review residuals R1-R4") against the residuals R1-R4 in the
[producer re-review](phase4-recursive-planning-pilot-producer-re-review.md).
Reviewed revision `ac2623525796f14d432366a85360acfd63f65ea6` in a detached worktree
at `/tmp/kilo/pilot-fix2`. No source edits; scratch discarded.

## Verdict

**R1 (node_id / criterion_ids), R2, and R3 are closed. The R4 decline is correct and
well-justified. One residual of the same R1 class remains** — `contract.budget_id`
is provider-controlled, used as a ledger key, and is still not validated — so the
seam's "what the guard admits is something the reducer can evaluate" promise is not
yet total. Conditional GO is unchanged; close this last identifier-shaped gap before
wiring.

Baseline reproduced on `ac26235`: `scripts/check-tests --fast` **866 passed, 30
deselected**; full `pytest -q` **896 passed**; `scripts/check-docs` **256 links OK**;
`scripts/check-static` rc 0.

## R1 — closed for `node_id` and `criterion_ids`

`_check_child_identifiers` (`planning_pilot.py:98`) runs on the deep-copied children
inside `admit_proposal` and requires a non-empty string `node_id` and non-empty
string criterion ids. Verified refused at stage `GUARD` for the full shape set:
`node_id` as list, dict, `None`, empty string, `int`, tuple, set; and criterion ids
as list element, dict, `int`, `None`, empty string, tuple, set, plus a bare-string
`criterion_ids`. A well-formed child is still admitted, and the
omitted-`criterion_ids` division of labour (admitted with empty coverage, refused by
the reducer) still holds.

The stage move `COVERAGE` → `GUARD` for the malformed-`criterion_ids` case is
correct: the boundary now catches it, so the honest stage is the guard, not the
derivation that never runs. The test expectation was moved with exact equality
(`== "GUARD"`), not loosened to a set. Confirmed.

The original symptom is gone: a payload with an unhashable `node_id` can no longer
be produced, so the reducer no longer sees the `TypeError: unhashable type: 'list'`.
(The earlier `UnknownTransition` seen in the probe is an artefact of the synthetic
state's lifecycle, not a producer issue.)

## R2 — closed

`parent_depth` is derived from the live state before the provider runs, and children
are counted as they normalise. Verified:

| provider fault | before | after |
| --- | --- | --- |
| generator yields 2 then raises | `proposed_children=0`, `depth_reached=0` | `proposed_children=2`, `depth_reached=1` |
| `[valid, non-mapping]` | `proposed_children=0` | `proposed_children=1` |
| returns a `Mapping` / string | iterated keys / per character | refused `PROVIDER`, `proposed_children=0` |

The `str`/`bytes`/`Mapping` rejection before the loop closes the per-character
iteration shape; the style gate's earlier catch is reflected in the code, not just
the tests. `check-static` rc 0.

## R3 — closed

The spec's `## Residual and inherited surface` now carries F4's wiring constraint
beside the single-layer containment entry, and also records the `clone()` rationale.

## R4 — decline verified correct

The claim was that `RunState.clone()` would share "precisely the structures an
untrusted caller reaches for". Verified directly:

```
clone.nodes["p"].contract is s.nodes["p"].contract        -> True
clone.nodes["p"].contract["owned_scope"][0] = "src/evil/x.py"
# live s.nodes["p"].contract["owned_scope"][0] == "src/evil/x.py"  -> True
```

`clone()` unshares the top-level mappings, the record objects, the ledger internals,
and the reducer-mutated lists, but it deliberately shares each record's nested
containers (the docstring scopes the contract to "every container the reducer can
mutate", and the reducer assigns record scalar fields rather than mutating nested
dicts). A provider handed a clone could therefore widen its parent's `owned_scope`
in the live contract and be admitted against the widened scope — the P0 exactly. The
decline is sound; `deepcopy` is the right primitive for an untrusted boundary, and
the reasoning is now recorded so the cheaper primitive is not adopted by a later
reader seeing only "it is cheaper".

## Residual finding

### R5 (P2) — `contract.budget_id` is provider-controlled, unvalidated, and used as a ledger key

Same class as R1, different field. `_check_child_identifiers` validates `node_id` and
criterion ids but not `contract.budget_id`. The reducer's `_on_decomposition_admitted`
passes it to `Ledger.register_budget`, which keys `budgets` by it
(`model.py:244-246`); an unhashable value raises a bare `TypeError` that the
handler's `except decomposition.DecompositionError` does not catch.

```python
kid = {**child(), "contract": {**child()["contract"], "budget_id": ["b"]}}
payload, record = pilot.propose_decomposition(
    state(), "p", lambda s, p: [kid], proposal_id="P")
# guard ADMITTED; validate_decomposition ADMITTED
state().ledger.register_budget(
    BudgetRecord(budget_id=["b"], node_id="c1", limits={"MODEL_COST": 1.0}))
# -> TypeError: unhashable type: 'list'
```

`validate_decomposition` only truthiness-checks `budget_id`, so the crash surfaces
later at `register_budget`. **Proposed fix:** require a non-empty string
`budget_id` in `_check_child_identifiers` (the child contract is provider-supplied),
or make `register_budget`/the reducer raise `DecompositionError`. **Regression
test:** a provider returning `contract.budget_id` as a list/dict is refused by the
guard.

### R6 (P2) — malformed `dependencies` entries pass the guard

Caller-supplied rather than provider-controlled, so lower priority, but
`propose_decomposition` forwards `dependencies` into the payload where
`validate_decomposition` does `dependency["from_node_id"]` / `["to_node_id"]`
without a shape check:

```
dependencies=[{}]                                  -> reducer KeyError: 'from_node_id'
dependencies=[{"from_node_id": ["c1"], ...}]       -> reducer TypeError: unhashable type: 'list'
dependencies=[7]                                   -> guard refused ASSEMBLY (non-mapping)
```

Neither `KeyError` nor `TypeError` is a `DecompositionError`, so the reducer handler
does not convert them. The model cannot choose `dependencies`, so this is a caller
contract gap, not a model-decision bypass; still worth a validator shape check or a
`DecompositionError` at the authority.

## Reproduce

```
git worktree add --detach /tmp/kilo/pilot-fix2 ac26235
ln -s <repo>/.venv /tmp/kilo/pilot-fix2/.venv
cd /tmp/kilo/pilot-fix2
PYTHONPATH=src <repo>/.venv/bin/python /tmp/kilo/probe_a3.py
# R1/R2/R4/R5/R6 inline probes as above
./scripts/check-tests --fast && ./scripts/check-docs && ./scripts/check-static
```

## Implementer's response (2026-10-01)

R5 and R6 closed. **922 passed**; `scripts/check`, `scripts/check-static` and
`scripts/check-docs` all rc 0 (257 links).

**R5 closed in the guard.** `contract.budget_id` is provider-controlled and becomes a
ledger key via `Ledger.register_budget`, so an unhashable value reached the reducer
as a bare `TypeError` its `DecompositionError` handler did not catch. The re-review
is right that this is R1's defect on a field the guard had not reached, and the
right way to describe the whole class is: **every identifier the reducer will key is
validated at the guard**. That now covers `node_id`, criterion ids,
`contract.budget_id`, and the `acceptance_criteria` entries' ids — and the contract
itself must be a mapping carrying a non-empty string budget id and non-empty
acceptance criteria with string ids.

**R6 closed.** Caller-supplied `dependencies` entries are shape-checked for both
endpoints before assembly, since `_ensure_acyclic` reads `from_node_id` and
`to_node_id` with no shape check, so a missing key reached the authority as a bare
`KeyError` and an unhashable one as a bare `TypeError`. The model cannot choose these,
but a caller contract gap that crashes the authority is still a gap.

**A shared normaliser, because the pattern recurred a fifth time.** R6's first
implementation used `for entry in dependencies or ():` and the style gate caught it —
the same per-character/Mapping-iteration shape it has now caught five times across
this module. Five local guards is not a fix; a habit is not a fix. `_as_sequence` is
one normaliser that refuses the shapes that iterate wrongly, and the two remaining
`contract.get("owned_scope") or ()` sites were checked by hand — both handle a string
explicitly, so neither is the hazard.

The pattern is worth naming for whoever reads this next: `x or ()` reads as
"empty means nothing" and is correct for `None` and for empty containers, and wrong
for a non-empty string or a Mapping. In a module whose entire job is normalising
untrusted input, that distinction is the work.
