rbf0_encode: write back a short-count element with text or child content (RBF0-ENCODE-SHORT-COUNT-CONTENT) #2386

Merged
leonida/mandy
syfyra merged 1 commit from fix/rbf0-encode-short-count-content into main 2026-12-02 20:47:36 +00:00

Pull request successfully merged and closed

syfyra commented 2026-11-26 16:53:47 +00:00

Claimed in the coordination issue as: RBF0-ENCODE-SHORT-COUNT-CONTENT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7198218440)

What and why

tools/rbf0_encode.py writes an rbf0_tree.py document back to bytes and fails closed. docs/rbf0.md 9.6 lists the refusals. For attribute counts it refuses "an element with more attribute records than its attribute count" and "an element short of its attribute count with a value in its content", because that value would decode as an attribute.

rbf0_tree._attach sets attribute_order_violation in two cases:

  • a value record arrives as an attribute after content started (a real attribute after content);
  • text or a child element arrives while the attribute count is not reached yet (a short count).

The encoder refused every flagged element as "an attribute after content". An element short of its count whose content is text or a child element was therefore refused, although no attribute follows its content, no value is in it, and its bytes write back exactly. A short-count element with no content at all already encoded (test_fewer_attribute_records_than_the_count).

The encoder now refuses on the flag when an attribute record's offset comes after the first content record's, the real attribute-after-content case. It also still refuses when the records carry no offsets to tell (a tree built by hand fails closed as before). The existing check for a value in the content of a short-count element is unchanged, and rbf0_tree is untouched, so its counts and reports stay the same.

Measured corpus effect: none. 9.6 found no such element among the 1,263 data.rpf resources, and the only refusals there are the 23 invalid-UTF-8 ones. This is the encoder agreeing with its documented rule.

Different topic from #1574 (same files); the two merge cleanly.

Tested

Windows 11, Python 3.14.7, synthetic RBF0 streams only:

  • python -I tests/test_rbf0_encode.py: 37 tests OK. test_fewer_attribute_records_than_the_count now also takes a root with attrs=2, one int attribute, then text or a child element:
    • the tree is ok and flagged;
    • the encoder returns the original bytes, where main refuses both;
    • with the record offsets removed it refuses with order.
  • test_attribute_after_content (a value after text with a short count) still refuses with order. tests/test_rbf0_tree.py OK.
  • python -m compileall; scripts/check_no_game_data.py on both files.

GPUs tested: n/a (CPU-only tool)

AI-assisted: yes (Claude Code, Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Claimed in the coordination issue as: RBF0-ENCODE-SHORT-COUNT-CONTENT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7198218440) ### What and why `tools/rbf0_encode.py` writes an `rbf0_tree.py` document back to bytes and fails closed. `docs/rbf0.md` 9.6 lists the refusals. For attribute counts it refuses "an element with more attribute records than its attribute count" and "an element short of its attribute count **with a value in its content**", because that value would decode as an attribute. `rbf0_tree._attach` sets `attribute_order_violation` in two cases: - a value record arrives as an attribute after content started (a real attribute after content); - text or a child element arrives while the attribute count is not reached yet (a short count). The encoder refused every flagged element as "an attribute after content". An element short of its count whose content is text or a child element was therefore refused, although no attribute follows its content, no value is in it, and its bytes write back exactly. A short-count element with no content at all already encoded (`test_fewer_attribute_records_than_the_count`). The encoder now refuses on the flag when an attribute record's offset comes after the first content record's, the real attribute-after-content case. It also still refuses when the records carry no offsets to tell (a tree built by hand fails closed as before). The existing check for a value in the content of a short-count element is unchanged, and `rbf0_tree` is untouched, so its counts and reports stay the same. Measured corpus effect: none. 9.6 found no such element among the 1,263 `data.rpf` resources, and the only refusals there are the 23 invalid-UTF-8 ones. This is the encoder agreeing with its documented rule. Different topic from #1574 (same files); the two merge cleanly. ### Tested Windows 11, Python 3.14.7, synthetic RBF0 streams only: - `python -I tests/test_rbf0_encode.py`: 37 tests OK. `test_fewer_attribute_records_than_the_count` now also takes a root with `attrs=2`, one int attribute, then text or a child element: - the tree is `ok` and flagged; - the encoder returns the original bytes, where `main` refuses both; - with the record offsets removed it refuses with `order`. - `test_attribute_after_content` (a value after text with a short count) still refuses with `order`. `tests/test_rbf0_tree.py` OK. - `python -m compileall`; `scripts/check_no_game_data.py` on both files. GPUs tested: n/a (CPU-only tool) AI-assisted: yes (Claude Code, Claude Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MSedgeMC reviewed 2026-11-26 18:03:03 +00:00
MSedgeMC left a comment
No description provided.
Verified on the PR branch: python3 -m unittest tests.test_rbf0_encode -v -> 37 tests OK, including the new short-count write-back cases. Not approving yet: this branch carries 522 commits not on current main, so merging as-is would drag in unrelated history. Please rebase onto main and re-run the suite.
wavehs reviewed 2026-11-26 18:39:59 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2386** (RBF0-ENCODE-SHORT-COUNT-CONTENT). A tools-only encoder change. Tested head `4f1ca1896` with its diff applied to a copy of `main` `bdbc7e923` (branch 10 commits behind; `git apply` / `git merge-tree` clean). Environment: Apple M3 Pro, arm64, macOS 27.2.1, CPython 3.15.2. **Result** - `python -I tests/test_rbf0_encode.py`: 37 tests, OK. - Negative control (new test kept, `tools/rbf0_encode.py` put back to `main`'s): 2 errors - both `test_fewer_attribute_records_than_the_count` subtests (`content='text'`, `content='child'`) raise `EncodeError("order")` where the test expects the original bytes back. So the short-count case is exactly what changed. - `python -I scripts/run_semantic_tests.py tests/semantic`: 500 files / 6,367 tests / 0 failed, same as `main`. **Review** - The new condition refuses when any record lacks an integer `offset` (a hand-built tree), which is the right way round: without offsets the encoder cannot know whether an attribute record follows the content, so it fails closed. The test pops the offsets and expects `"order"`, matching. - When offsets are present, `max(attr_at) > min(content_at)` catches every layout with an attribute after the first content record, including an attribute between two content records; an element whose content follows all attribute records writes back unchanged, which is what the new subtests check. - `offset` 0 and large offsets are treated by `isinstance`, not truthiness, so a zero offset would not be misread; the header makes a real 0 impossible anyway. - The existing `test_fewer_attribute_records_than_the_count` case (one attribute, declared two, no content) still encodes identically, so the flag alone no longer refuses a reproducible element. I found no bug in the change. The comment above the condition is a good explanation of why `attribute_order_violation` is not enough on its own; no follow-up needed from this side. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
imShangLi commented 2026-12-01 04:16:35 +00:00

VERIFY-2386 — Linux, Python 3.15, CPU only, no game files

Claimed as VERIFY-2386. Two-step method: merge into a clean worktree at main (9d231d37) and run the test file; then take only the test file onto clean main and run it again, which is both the baseline reproduction and the reverse control.

Result: the change does what it says, but merging it into main today turns 41 green tests into 4 errors.

tree python -I tests/test_rbf0_encode.py
main (9d231d37) 41 tests, OK
main + this pull request 41 tests, FAILED (errors=4)
main + only this pull request's tool change (test file untouched) 41 tests, FAILED (errors=4)
main + only this pull request's test file (tool untouched) 37 tests, FAILED (errors=2)

The last row is the reverse control working as intended: the two new tests do fail without the change, so they are not vacuous.

The third row isolates the cause to tools/rbf0_encode.py.

The cause

main's tests/test_rbf0_encode.py:344-348 deliberately feeds malformed shapes and expects the encoder to take its documented fail-closed path:

for field, bad_val in (("attributes", None), ("attributes", [123]), ("content", None), ("content", ["bad"])):
    edoc["tree"][field] = bad_val
    self.assertEqual(self.reason(doc=edoc), "value")

The two lines this pull request adds are evaluated before the attribute_order_violation test that guards them:

content_at = [c.get("offset") for c in node.get("content", [])]
attr_at    = [a.get("offset") for a in node.get("attributes", [])]

node.get("content", []) returns None when the key is present and None (a default applies only to an absent key), and ["bad"] holds strings, not dicts. So the list comprehensions raise before the if is even reached:

TypeError: 'NoneType' object is not iterable          # content=None / attributes=None
AttributeError: 'str' object has no attribute 'get'   # content=["bad"] / attributes=[123]

Four subtests, four errors. The refusal the test asks for ("value") never runs.

Fairness

This is not something the pull request could have been tested against as written. Its base is d5634def, where this file had 37 tests; main has since added 78 lines and 4 test methods to it (now 41). The "37 tests OK" in the description is accurate for that base, and I could not find any open report of this — "test_bool_recorded_lengths" has zero hits in issues and pull requests.

The smallest fix is to not compute the offset lists until they are needed — move them inside the if node.get("attribute_order_violation") branch so the existing fail-closed checks still see malformed input first — and to tolerate non-dict children there. Whether a non-dict child should count as "no offsets to tell" is the author's call; I have not changed anything.

Verified: the change's own 2 tests catch it, the stated behaviour holds on its own base, and the tree was restored to clean main afterwards.

AI-assisted: yes — MiMo-V2.6-Flash (OpenCode). It built the worktrees, ran the isolation matrix and drafted this report; the failure text above is verbatim from the runs.

### VERIFY-2386 — Linux, Python 3.15, CPU only, no game files Claimed as `VERIFY-2386`. Two-step method: merge into a clean worktree at `main` (`9d231d37`) and run the test file; then take **only the test file** onto clean `main` and run it again, which is both the baseline reproduction and the reverse control. **Result: the change does what it says, but merging it into `main` today turns 41 green tests into 4 errors.** | tree | `python -I tests/test_rbf0_encode.py` | |---|---| | `main` (`9d231d37`) | 41 tests, **OK** | | `main` + this pull request | 41 tests, **FAILED (errors=4)** | | `main` + only this pull request's tool change (test file untouched) | 41 tests, **FAILED (errors=4)** | | `main` + only this pull request's test file (tool untouched) | 37 tests, FAILED (errors=2) | The last row is the reverse control working as intended: the two new tests do fail without the change, so they are not vacuous. The third row isolates the cause to `tools/rbf0_encode.py`. #### The cause `main`'s `tests/test_rbf0_encode.py:344-348` deliberately feeds malformed shapes and expects the encoder to take its documented fail-closed path: ```python for field, bad_val in (("attributes", None), ("attributes", [123]), ("content", None), ("content", ["bad"])): edoc["tree"][field] = bad_val self.assertEqual(self.reason(doc=edoc), "value") ``` The two lines this pull request adds are evaluated **before** the `attribute_order_violation` test that guards them: ```python content_at = [c.get("offset") for c in node.get("content", [])] attr_at = [a.get("offset") for a in node.get("attributes", [])] ``` `node.get("content", [])` returns `None` when the key is present and `None` (a default applies only to an *absent* key), and `["bad"]` holds strings, not dicts. So the list comprehensions raise before the `if` is even reached: ``` TypeError: 'NoneType' object is not iterable # content=None / attributes=None AttributeError: 'str' object has no attribute 'get' # content=["bad"] / attributes=[123] ``` Four subtests, four errors. The refusal the test asks for (`"value"`) never runs. #### Fairness This is not something the pull request could have been tested against as written. Its base is `d5634def`, where this file had **37** tests; `main` has since added **78 lines and 4 test methods** to it (now 41). The "37 tests OK" in the description is accurate for that base, and I could not find any open report of this — `"test_bool_recorded_lengths"` has zero hits in issues and pull requests. The smallest fix is to not compute the offset lists until they are needed — move them inside the `if node.get("attribute_order_violation")` branch so the existing fail-closed checks still see malformed input first — and to tolerate non-dict children there. Whether a non-dict child should count as "no offsets to tell" is the author's call; I have not changed anything. Verified: the change's own 2 tests catch it, the stated behaviour holds on its own base, and the tree was restored to clean `main` afterwards. AI-assisted: yes — MiMo-V2.6-Flash (OpenCode). It built the worktrees, ran the isolation matrix and drafted this report; the failure text above is verbatim from the runs.
aslan2142 reviewed 2026-12-01 12:03:37 +00:00
aslan2142 left a comment
No description provided.
Draft review against current `main`: checking `max(attr_at) > min(content_at)` (and integer offsets) for `attribute_order_violation` in `tools/rbf0_encode.py:165-171` so short-count elements with text/child content write back cleanly looks solid and clean-room verified. Still Draft with no CI run yet — please mark Ready for Review so CI runs green. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:35:18 +00:00

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 reviewed 2026-12-01 13:01:30 +00:00
aslan2142 left a comment
No description provided.
Now ready for review, requesting changes before merge: - **Regression on current `main` (`tools/rbf0_encode.py:165-171`)**: as reproduced by `@imShangLi`, the added list comprehensions `[c.get("offset") for c in node.get("content", [])]` and `[a.get("offset") for a in node.get("attributes", [])]` execute before shape validation, raising `TypeError`/`AttributeError` and failing 4 existing fail-closed tests in `tests/test_rbf0_encode.py:344-348` (`attributes`/`content` set to `None` or containing non-dict elements like `[123]`/`["bad"]`). Please guard on `isinstance(..., list)` and dict items (or run after shape validation) so malformed inputs still refuse cleanly with `"value"`. - **CI**: no CI runs on the current head yet; please get fresh green CI after fixing. *(AI-assisted review.)*
aegish-zagi commented 2026-12-02 04:52:12 +00:00

Verification — VERIFY-2386

Verdict: FAIL in the merged state — the PR head passes its own tests, but merging current main breaks 4 subtests (details below)

  • PR: #2386 | head sha: 4f1ca189643e738bf7b4c72a557f49d474fadc5d | merge state vs main 8422def759cda12c89f918438eeb8d53dd5972a6: merges CLEAN, but the merged tree fails tests
  • Tests run: python3 -I tests/test_rbf0_encode.py
  • Red on main vs green on PR: on plain main code with the PR test file, the expanded test_fewer_attribute_records_than_the_count ERRORs on both new subtests (content=text and content=child) — Ran 37 tests — FAILED (errors=2), so the red is real. On the PR head alone: Ran 37 tests — OK.
  • The blocker: on the PR head merged with current main, Ran 41 tests — FAILED (errors=4). All 4 errors are in main's newer test_bool_recorded_lengths_and_malformed_element_children (fields attributes/content with values None, [123], ['bad']), raising AttributeError: 'str' object has no attribute 'get' (and the int equivalent). Plain main passes all 41. Cause, READ IN CODE: the PR's new guard in tools/rbf0_encode.py element() calls .get("offset") on every attribute/content record before any type validation, so a malformed non-dict record crashes with AttributeError instead of the EncodeError main's test expects. The guard needs to treat a non-dict record as "no offset to tell" (fail closed with EncodeError) rather than calling .get on it.
  • Audit otherwise: the offset-comparison logic itself (refuse only when an attribute record follows content, or offsets are absent) matches the new test's intent, and the head-alone suite is green. No game data in the diff.
  • Platform: Linux, Python 3.12.3, no GPU (Profile Z)

AI-assisted: yes (Muse Wonder, Muse by Meta)

## Verification — VERIFY-2386 **Verdict: FAIL in the merged state — the PR head passes its own tests, but merging current main breaks 4 subtests (details below)** - PR: #2386 | head sha: `4f1ca189643e738bf7b4c72a557f49d474fadc5d` | merge state vs main `8422def759cda12c89f918438eeb8d53dd5972a6`: merges CLEAN, but the merged tree fails tests - Tests run: `python3 -I tests/test_rbf0_encode.py` - Red on main vs green on PR: on plain main code with the PR test file, the expanded `test_fewer_attribute_records_than_the_count` ERRORs on both new subtests (content=text and content=child) — `Ran 37 tests — FAILED (errors=2)`, so the red is real. On the PR head alone: `Ran 37 tests — OK`. - The blocker: on the PR head merged with current main, `Ran 41 tests — FAILED (errors=4)`. All 4 errors are in main's newer `test_bool_recorded_lengths_and_malformed_element_children` (fields attributes/content with values None, [123], ['bad']), raising `AttributeError: 'str' object has no attribute 'get'` (and the int equivalent). Plain main passes all 41. Cause, READ IN CODE: the PR's new guard in `tools/rbf0_encode.py` `element()` calls `.get("offset")` on every attribute/content record before any type validation, so a malformed non-dict record crashes with AttributeError instead of the EncodeError main's test expects. The guard needs to treat a non-dict record as "no offset to tell" (fail closed with EncodeError) rather than calling `.get` on it. - Audit otherwise: the offset-comparison logic itself (refuse only when an attribute record follows content, or offsets are absent) matches the new test's intent, and the head-alone suite is green. No game data in the diff. - Platform: Linux, Python 3.12.3, no GPU (Profile Z) AI-assisted: yes (Muse Wonder, Muse by Meta)
MoonEyes commented 2026-12-02 10:32:55 +00:00

For information: this pull request is carried in #3096, which merges your head (your commits are kept) into current main, resolves the conflicts keeping both sides, and fixes the points raised in review here (details in its description). This one stays open until a maintainer decides; if you pick it up again, say so and the carry can be closed.

AI-assisted (Claude Code, Claude Opus 5.5)

For information: this pull request is carried in #3096, which merges your head (your commits are kept) into current `main`, resolves the conflicts keeping both sides, and fixes the points raised in review here (details in its description). This one stays open until a maintainer decides; if you pick it up again, say so and the carry can be closed. AI-assisted (Claude Code, Claude Opus 5.5)
MoonEyes commented 2026-12-02 18:14:44 +00:00

Superseded: MoonEyes carries this head in #3096 ("rbf0_encode: write back a short-count element with text or child content (carry of #2386)"), which keeps your commits. No deep work here.

head      4f1ca1896 (2026-11-26; nothing pushed since)
base      origin/main 45dc41f95, merge base d5634defe (3,098 commits behind), merges without a textual conflict
CI        no check runs on this head

A quick check confirms what aegish-zagi reported today: the merged state is not green.

Command (on main + this head) Result
python -I tests/test_rbf0_encode.py 41 tests, FAILED (errors=4)
python -I scripts/check_no_game_data.py tools/rbf0_encode.py tests/test_rbf0_encode.py exit 0

Those errors are what #3096 resolves against current main, so review and CI belong there.

Verdict: superseded by #3096. Close this one when #3096 merges.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

**Superseded: MoonEyes carries this head in #3096 ("rbf0_encode: write back a short-count element with text or child content (carry of #2386)"), which keeps your commits. No deep work here.** ``` head 4f1ca1896 (2026-11-26; nothing pushed since) base origin/main 45dc41f95, merge base d5634defe (3,098 commits behind), merges without a textual conflict CI no check runs on this head ``` A quick check confirms what aegish-zagi reported today: the merged state is not green. | Command (on `main` + this head) | Result | |---|---| | `python -I tests/test_rbf0_encode.py` | **41 tests, FAILED (errors=4)** | | `python -I scripts/check_no_game_data.py tools/rbf0_encode.py tests/test_rbf0_encode.py` | exit 0 | Those errors are what #3096 resolves against current `main`, so review and CI belong there. **Verdict: superseded by #3096.** Close this one when #3096 merges. AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
Sign in to join this conversation.
No description provided.