Four findings by @HQasper on tools/rbf0_encode.py:
- `--check` exited 0 when resources were refused, so a round trip that verified nothing read as success.
It now exits 0 only when every resource is byte-identical, as rbf0_tree.py does for a resource that is
not ok.
- The element header count came from `attrs_declared` while every node of `attributes` was written, so an
attribute appended by hand encoded as the element's first content value: a different tree with status
ok. encode() now refuses more attribute records than the count (value), and an element short of its
count that has a value in its content, which would decode as an attribute (order). A parsed element
short of its count with no such value still round-trips exactly.
- `blob[:doc["size"]]` in resources() could not trim anything. The slice is gone and check() compares
the joined length with the recorded size; two carved streams of one name are refused instead of the
last one silently winning.
- docs/rbf0.md 9.6 lists the `value` refusals and says what `--check` exits with.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Harden tools/rbf0_encode.py so edited/malformed trees and JSON reports raise clean EncodeError / SystemExit messages instead of tracebacks: reject bool for descriptor/attrs_declared, catch direct math.nan and struct.pack OverflowError in float_bits(), reject non-dict documents and non-str text, and validate report JSON/shape/offsets/OSError in write_all(). Synthetic tests added in tests/test_rbf0_encode.py.
docs/rbf0.md called the tree output "a rendering, not a round-trip" and
nothing wrote RBF0. encode(doc) rebuilds the record stream from the tree
rbf0_tree.parse_resource returns, in both layouts: descriptor names at
first use, element words 0, 0, attribute count, attributes then content
in tree order, text and strings with their recorded NUL.
It fails closed, with a reason, when the tree cannot hold the original:
an incomplete parse, invalid UTF-8 the decoder replaced, a NaN float
(infinities are exact), an attribute after content, a descriptor out of
order, or a string or text whose byte length changed.
--check decodes, encodes and compares every resource of a carve and
prints counts only. A report-and-directory form writes the resources of
an rbf0_tree.py JSON report (possibly edited) as .rbf files.
Measured on the 1,263 data.rpf resources: 1,240 byte-identical, 0
different, 23 refused, all for invalid UTF-8. They are the resources
holding the 26 invalid payloads docs/rbf0.md already lists. Counts are
in docs/rbf0.md 9.6.
tests/test_rbf0_encode.py: synthetic streams only. Every record type in
both widths, infinities, narrow float bits, the largest integers, the
JSON report form, an edited tree, each refusal reason, and the CLI with
a resource split over two 64 KiB streams.