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

Open
github-bot wants to merge 1 commit from github-bot/gh-2386 into main AGit
Owner

@⁠syfyra on GitHub · 2026-11-26 16:53 UTC · original
syfyra:fix/rbf0-encode-short-count-content → main · +23 −1 · 2 files · 1 commits · draft

Claimed in the coordination issue as: RBF0-ENCODE-SHORT-COUNT-CONTENT (#1608)

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

**@⁠syfyra** on GitHub · 2026-11-26 16:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2386) `syfyra:fix/rbf0-encode-short-count-content` → `main` · +23 −1 · 2 files · 1 commits · draft Claimed in the coordination issue as: RBF0-ENCODE-SHORT-COUNT-CONTENT (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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)
docs/rbf0.md 9.6: an element short of its attribute count is refused
only when it has a value in its content, because that value would
decode as an attribute. rbf0_tree also sets attribute_order_violation
when text or a child element arrives before the count is reached, and
the encoder refused every flagged element as "an attribute after
content", so such an element was refused although its bytes write back
exactly (an element short of its count with no content already encoded,
as tested).

Refuse on the flag only when an attribute record's offset follows the
first content record's, or when records carry no offsets to tell (a
hand-built tree fails closed as before). The short-count check for a
value in the content is unchanged.

Tested: test_fewer_attribute_records_than_the_count now also encodes a
short-count element with text and with a child element exactly (both
refused before) and refuses them once the offsets are removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:58:57 +00:00
Author
Owner

@⁠MSedgeMC on GitHub · 2026-11-26 18:03 UTC · original · reviewed

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.

**@⁠MSedgeMC** on GitHub · 2026-11-26 18:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2386#pullrequestreview-6078739195) · **reviewed** 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.
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 18:39 UTC · original · reviewed

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.

**@⁠wavehs** on GitHub · 2026-11-26 18:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2386#pullrequestreview-6079087431) · **reviewed** **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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2386/head:github-bot/gh-2386'
git switch 'github-bot/gh-2386'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2386'
git switch 'github-bot/gh-2386'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2386'
git switch 'github-bot/gh-2386'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2386'
git switch 'main'
git merge --squash 'github-bot/gh-2386'
git switch 'main'
git merge --ff-only 'github-bot/gh-2386'
git switch 'main'
git merge 'github-bot/gh-2386'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.