Skip to content

fix(tables): resolve a block's name through its BLOCK sentinel, and gate the class census in CI - #75

Merged
DrunkOnJava merged 3 commits into
mainfrom
fix/block-name-slot-and-census-gate
Aug 30, 2026
Merged

DrunkOnJava merged 3 commits into
mainfrom
fix/block-name-slot-and-census-gate

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two issues, one PR.

#70 — neither reader was on the wrong slot. BLOCK_RECORD decoded *D / *T / *Paper_Space where the adjacent BLOCK entity of the same definition decoded *D3 / *T9 / *Paper_Space0. Both closed their own boundary, so nothing errored — eight BLOCK_HEADERs of sample_AC1032.dwg simply came back named *D, which no symbol table can hold. The file itself carries two different strings: the BLOCK_HEADER stores only the stem of an auto-generated name and the numeric suffix lives solely on the BLOCK sentinel. BLOCK_RECORD is the reader that was wrong, and the fix is a join, not a slot change.

#56 — the class census is now a CI gate, the 916 unclaimed object-stream bytes are fully explained (they are not padding), and TABLECONTENT / TABLEGEOMETRY is settled as far as this corpus permits.

Evidence — #70

The BLOCK_HEADER string stream genuinely holds the stem

examples/probe_block_names.rs (new) reads both records' string streams positionally, from the bit string_stream::locate reports, every slot in order.

On arc_2013.dwg, BLOCK_HEADER 0x6C and BLOCK_HEADER 0x74 are byte-identical apart from their own handle field and their handle streams:

0x6C  4d 0c 40 5b 2a 81 54 01 50 04 30 a8 01 40 01 84 …
0x74  4d 0c 40 5d 2a 81 54 01 50 04 30 a8 01 40 01 84 …

Both string streams are (76, 282) — 206 bits, which the §19.1 trailer declares literally (RS = 0x00CE = 206) — and both parse to exactly ["*Paper_Space", "", ""]: a 10-bit BS length, 192 bits of UTF-16, and two empty TV slots at 2 bits each. A 13-unit *Paper_Space0 needs 222 bits. Reading 16 bits earlier does not parse (the BS at that offset lands on a 00 control and claims a 340-unit string), so the located start is the only self-consistent one.

AutoCAD's own DXF twin says which one is the real name

arc_2013.dxf from the public nextgis/dwg_samples repository is the same drawing exported by the producer (an Autodesk application — AcDb:AppInfo on sample_AC1032.dwg reads "Trusted DWG last saved by an Autodesk", version 25.0). Its BLOCK_RECORD table:

DXF handle DXF group 2 dwg-rs BLOCK_HEADER dwg-rs BLOCK sentinel
6C *Paper_Space *Paper_Space ✓ *Paper_Space
70 *Model_Space *Model_Space ✓ *Model_Space
74 *Paper_Space0 *Paper_Space ✗ *Paper_Space0 ✓

The sentinel is the authority. On sample_AC1032.dwg the stored stem is a prefix of the sentinel name in 27 of 27 definitions and the remainder is decimal digits every time; the 27 sentinel names are distinct where the 27 stored stems collapse to 20 (eight records read *D, three *Paper_Space, three *U, two *T).

Not an artefact of the R2007+ string stream

After merging #72, arc_R14.dwg, arc_2000.dwg and arc_2004.dwg walk too. They store their names inline — no string stream involved — and show exactly the same disagreement: BLOCK_HEADER 0x74 reads *Paper_Space, its BLOCK sentinel 0x75 reads *Paper_Space0. The split is a property of the record, not of §19.1.

The join

tables::block_record::block_sentinel_handle_of reads the BLOCK handle out of a BLOCK_HEADER's own handle stream. §20.4.52 puts a NULL reference immediately before it, and that NULL is a hard pointer with a zero-byte counter (code 5, counter 0) — self-identifying, so the scan needs no reactor count and no xdictionary flag:

BLOCK_HEADER 108: 4.1:1(BlockControl) 5.0:0 3.1:109(Block) 3.1:110(EndBlk)
BLOCK_HEADER 1778: 4.1:1(BlockControl) 3.2:1779(Dictionary) 5.0:0 3.2:1780(Block) …

27 of 27 on sample_AC1032.dwg, 3 of 3 on every R2000-R2018 corpus file, resolving to a record the walker independently classifies as BLOCK in every case. graph::resolve_block_names performs the join and is what callers should use for a block table. R13/R14 records carry neither the §19.1 trailer nor the RL object-data size, so they stay unresolved rather than guessed — pinned as a known limitation by r14_block_names_stay_unresolved_for_want_of_a_handle_stream.

The decoder it replaces

The R2007+ BLOCK_HEADER path was the one table entry not on the shared tables::modern::SplitStream machinery. It parsed its fields, then — if the resulting position did not read as a "plausible block name" — scanned every bit of the record for the highest-scoring string that looked like one (find_block_name_string_start / block_name_score, scoring *Model_Space at 1000, *-prefixed at 600, and so on). That is now gone. The decoder uses open_table_entry + SplitStream::finish, so a mis-read field is an error instead of a plausible-looking name; all 27 records close on delta 0. Two further fixes fell out: the record's description was read and discarded (now surfaced), and its entry flags used the inline B, BS, B order rather than the measured B, B, BS, which had is_xref_dependent and is_xref_resolved swapped.

Evidence — #56

The census is wired into CI

  • .github/workflows/ci.yml (coverage-smoke job) runs probe_class_census --strict over every canonical fixture. --strict disables the allowlist; the fixtures are self-generated so their census must be exact.
  • tests/canonical_corpus.rs::fixture_class_census_is_exact asserts walked == declared (was >=), with the fixed-type crediting the probe uses.
  • tests/class_census.rs (new) ratchets the real corpus when present: every under-walk that exists today is pinned by file, class and count, so a new one fails and a fixed one also fails, forcing the list to shrink rather than rot.

The 916 unclaimed bytes are not padding — the stream is 100 % accounted for

One measured sentence, as asked: the 916 unclaimed bytes are each record's own leading MC handle-stream-size field, which its MS object size does not count — 772 one-byte fields, 70 two-byte fields and a 4-byte section prologue, summing to exactly 916.

They are emphatically not alignment padding: only 8 of the 916 bytes are zero. Each inter-record run equals the width of the preceding record's MC field in 841 of 841 cases, against 734 of 841 for the "following record" reading — and 100 % on arc_2010.dwg, arc_2013.dwg and line_2013.dwg as well. That settles the open question in src/string_stream.rs, which noted that "either the record's MS byte count excludes the MC field, or the recorded handle-stream size counts the MC field itself": it is the first.

The probe now reports this as an agreement count rather than a byte total, so a regression surfaces as a disagreement:

object stream coverage    : 1191935 of 1192851 bytes; 916 unclaimed (largest run 4)
unclaimed bytes explained : 916 of 916 (841 inter-record runs equal the preceding
                            record's MC width, 0 do not; 4 leading, 1 trailing)

TABLECONTENT / TABLEGEOMETRY: not decidable from this corpus

sample_AC1032.dwg declares 5 of each against 2 ACAD_TABLE entities and contains 2 of each. Three independent measurements place the missing six records outside the drawing:

  1. the 842 walked records cover 1,191,935 of 1,192,851 stream bytes and the longest unclaimed run is 4 bytes — with the accounting above, there is no byte left for a hidden record;
  2. decoding all 842 records' trailing handle streams yields no reference to a handle the walk cannot answer (the only 6 unresolved references in the file are insert handles on one BLOCK_HEADER, a separate mis-parse of that list, and none of them are table objects) — nothing in the drawing points at a missing content or geometry object;
  3. ACAD_TABLE itself declares 2 and walks 2, so the drawing holds exactly two tables, and TABLECONTENT / TABLEGEOMETRY are 1:1 with a table — which cannot yield 2.5 instances each.

Against the embedded-sub-record hypothesis: CELLSTYLEMAP (AcDbCellStyleMap) declares 0 on this file while a TABLESTYLE exists, which is what a class the writer embeds rather than counts looks like — TABLECONTENT declares 5, not 0. Against the stale-counter hypothesis: ACAD_TABLE would have declared 5 too. Neither survives, and a one-file, two-table corpus cannot separate them. The gate therefore takes an explicit allowlist with that reason (ALLOWLIST in examples/probe_class_census.rs), tolerating a shortfall on exactly those two classes and never a surplus, and --strict keeps the question measurable.

One thing the census surfaced that is not mine

The R2004, R2007 and R2010 files each under-walk DICTIONARYVAR and CELLSTYLEMAP (e.g. arc_2010.dwg: declared 10 / walked 5, and 1 / 0). This is pre-existing on main — verified by running main's own probe_class_census at 3d3d443, which exits 1 with the same numbers. It is an open walker gap, unexplained, and is pinned per file and per count in KNOWN_SHORTFALL so it cannot grow. Follow-up below.

Measured results

Merged with origin/main at 3d3d443 before measuring.

cargo run --release --example coverage_report -- <19-file corpus>:

Files Decoded Skipped Errored Rate
main 3d3d443 19 3695 741 9 83.1 %
this branch 19 3695 741 9 83.1 %

Unchanged by construction: this PR fixes which name a record reports, not how many records decode. The block-name defect was silent — both readers returned well-formed strings — so no coverage figure ever moved with it, which is exactly why it needed an external oracle to find.

Block names on sample_AC1032.dwg, before → after:

Definitions Distinct names
BLOCK_HEADER stored string 27 20
resolved through the sentinel 27 27

Changes

  • src/tables/block_record.rs — the R2007+ path moves onto tables::modern::SplitStream with finish() boundary enforcement; the ~110-line name-scoring heuristic (find_block_name_string_start, plausible_tv_at, is_plausible_block_name, block_name_score, a stale copy of the object prefix skipper) is deleted. New description and block_sentinel_handle fields, new block_sentinel_handle / block_sentinel_handle_at / block_sentinel_handle_of / handle_stream_start. Five new unit tests including a misalignment negative control and three handle-stream anchor cases built from bytes.
  • src/graph.rs — new resolve_block_names.
  • examples/probe_block_names.rs — new; positional dump of both records' string streams plus the resolved join.
  • examples/probe_class_census.rs — --strict, the documented ALLOWLIST, the MC attribution of the unclaimed bytes, and a fix so a file with no AcDb:Classes section is "nothing to census" rather than a parse failure (which is what the canonical fixtures are).
  • tests/block_names.rs — new; 5 tests, corpus-gated.
  • tests/class_census.rs — new; the corpus ratchet.
  • tests/canonical_corpus.rs — the class census is now exact.
  • .github/workflows/ci.yml — the census gate.
  • CHANGELOG.md, STATUS.md, CLEANROOM.md.

Spec sections

§19.1 (R2007+ string stream), §20.4.52 (BLOCK_HEADER), §2.13 (handle references), §19.4.3 (BLOCK).

Source provenance

Clean-room declaration. All code and tests in this PR were written from scratch. I consulted only sources listed as allowed in CLEANROOM.md — the freely-redistributable ODA Open Design Specification for .dwg files v5.4.1, the corpus bytes themselves via this repository's probes, and the two public sources disclosed in the new CLEANROOM.md section: arc_2013.dxf from the public nextgis/dwg_samples repository (read only as an oracle, not vendored, contributing no structure or layout), and AutoCAD's anonymous-block naming convention as recalled from Autodesk's public DXF Reference (published documentation, vocabulary only, and independently corroborated by 27 of 27 measured records). I did not consult Autodesk SDK source, ODA SDK source, LibreDWG source, decompilations, or any copyleft-licensed DWG implementation. The §19.5.51 citation block_record.rs previously carried is withdrawn in favour of §20.4.52.

Gate

cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings, cargo test --release (19 suites, 0 failures), cargo deny check all, RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features, and cargo +1.85.0 test --no-run all pass.

Follow-ups

  • DICTIONARYVAR / CELLSTYLEMAP under-walk on R2004 / R2007 / R2010 (18 pinned entries across 9 files). Pre-existing on main; the census now makes it visible and bounded. Worth its own issue.
  • R13/R14 block names stay unresolved — those records carry no locatable handle stream, so arc_R14.dwg still reports two blocks named *PAPER_SPACE. Pinned by a test that fails the day the R14 handle stream becomes locatable.
  • The canonical fixtures carry no block definitions, so the block-name gate can only run on the real corpus. Teaching examples/build_fixtures.rs to emit a BLOCK_HEADER + BLOCK pair would put it in CI; canonical_fixtures_carry_no_block_definitions fails the moment that happens, so the gate cannot silently stay corpus-only.
  • TABLECONTENT / TABLEGEOMETRY needs a second table-bearing file to separate "stale registration count" from "embedded copy". The allowlist carries the reason until then.
  • RawObject::raw may be one to two bytes short. If MS excludes the MC field, as measured above, the walker's raw slice ends mc_width bytes early — harmless today because those bytes are the tail of the handle stream, which nothing decodes yet, but it will matter to whoever decodes it. Container-layer work, deliberately not touched here.

Closes #70
Closes #56

https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C


Note

Medium Risk
Changes core BLOCK_HEADER decoding and public graph APIs (resolve_block_names); mistakes could mis-name blocks in exports. CI and extensive corpus tests mitigate regression risk.

Overview
Fixes #70 by treating BLOCK_HEADER names as stems for auto-generated blocks (*D, *Paper_Space, etc.) and joining each header to its BLOCK sentinel for the authoritative full name. Adds graph::resolve_block_names, block_sentinel_handle_of (NULL-handle anchor in §20.4.52), surfaces description, and replaces the R2007+ BLOCK_HEADER heuristic name scanner with tables::modern::SplitStream plus boundary checks. New probe_block_names and tests/block_names.rs pin the behavior.

Closes #56 by making declared-vs-walked class counts a CI gate: probe_class_census --strict on canonical fixtures, exact equality in canonical_corpus.rs, and tests/class_census.rs ratcheting known shortfalls on the sample corpus. The probe gains an documented ALLOWLIST (TABLECONTENT/TABLEGEOMETRY, DICTIONARYVAR/CELLSTYLEMAP) and reports how unclaimed object-stream bytes map to preceding records’ leading MC fields.

Reviewed by Cursor Bugbot for commit 1a565d4. Configure here.

…nd-census-gate

# Conflicts:
#	src/tables/block_record.rs
…ate the class census in CI

BLOCK_RECORD decoded *D / *T / *Paper_Space where the adjacent BLOCK
entity of the same definition decoded *D3 / *T9 / *Paper_Space0. Both
closed their own boundary, so nothing errored — eight BLOCK_HEADERs of
sample_AC1032.dwg came back named *D, which no symbol table can hold.

Neither reader was on the wrong slot: the BLOCK_HEADER stores only the
stem of an auto-generated name and the numeric suffix lives solely on
the BLOCK sentinel. AutoCAD's own DXF twin of arc_2013.dwg names the
two paper-space BLOCK_RECORDs *Paper_Space and *Paper_Space0 where both
DWG records store *Paper_Space.

- block_sentinel_handle_of reads the BLOCK handle out of a
  BLOCK_HEADER's handle stream via the §20.4.52 NULL anchor.
- graph::resolve_block_names performs the join: 27 of 27 definitions on
  sample_AC1032.dwg, 3 of 3 on every R2000-R2018 corpus file.
- The R2007+ BLOCK_HEADER decoder moves onto tables::modern::SplitStream
  and is held to its string-stream boundary, replacing a name-scoring
  heuristic.
- probe_class_census gains --strict and a documented allowlist; CI runs
  it over every canonical fixture, canonical_corpus.rs asserts the
  census exactly, and class_census.rs ratchets the real corpus.
- The 916 unclaimed object-stream bytes are each record's leading MC
  field, not padding: 841 of 841 agreements. The stream is 100 %
  accounted for.

Closes #70
Closes #56

Claude-Session: https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C
@cursor

cursor Bot commented Aug 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_abbc8975-8040-4ebe-8a74-de34a355be31)

@DrunkOnJava
DrunkOnJava merged commit 6cc9b13 into main Aug 30, 2026
20 checks passed
@DrunkOnJava
DrunkOnJava deleted the fix/block-name-slot-and-census-gate branch August 30, 2026 22:52
DrunkOnJava added a commit that referenced this pull request Aug 30, 2026
…e is not always a live census (#80)

* fix(container): a record's MS excludes its own MC, and the class table is not always a live census

#77 — the walker's raw slice ended one MC width short on every R2010+
record. #75 measured that each inter-record run in AcDb:AcDbObjects
equals the width of the *preceding* record's leading MC handle-stream-
size field (841/841 on sample_AC1032.dwg, 100% on three more files,
against 734/841 for the following-record reading, with only 8 of the 916
bytes zero). A record is framed `MS | MC | MS bytes | CRC`, so
RawObject::raw is now sized mc_width + size_bytes; size_bytes still
reports the MS verbatim.

string_stream::data_section_end drops its `+ mc_field_bits` correction,
which existed only to compensate for the short slice — the rule is now
`payload_bits - handle_bits` and the two edits cancel to the same bit
offset. Coverage over the 19-file corpus is byte-identical before and
after: 3695 / 741 / 9 / 83.1%.

Two things the short slice hid: the sequential (handle-map-less) walk
drifted by one MC width per record on R2010+, and the object stream
looked gappy. Every R2004+ corpus file now tiles exactly — sample_
AC1032.dwg goes from 916 unclaimed bytes to the 4-byte 0x0dca prologue,
with zero bytes between records.

examples/build_fixtures writes the same rule, and now also emits the
§19.1 "strings present" trailer bit with the MC recording the padding
that stands in for the handle stream; declaring a zero-bit handle stream
put the data boundary past the end of the record, silently disabling
every decoder's boundary check on the three R2010+ fixtures. Those are
regenerated.

#76 — DICTIONARYVAR / CELLSTYLEMAP are not under-walked. The new
examples/probe_reference_closure runs three measurements that never
consult the class table, and all three are closed on all 19 files:
records tile the section with zero bytes between them; every hard
handle reference (§2.13 codes 3 and 5) resolves, the only unanswered
references in the corpus being six code-4 soft pointers from
BLOCK_HEADER 0xA0B of sample_AC1032.dwg to erased INSERTs; and the
AcDbVariableDictionary, sole owner of a drawing's DICTIONARYVAR
objects, holds exactly as many keys as the walk finds records on every
file — 10/6/5 on the R2004/R2007/R2010 bands that declare 16/11/10, and
5 and 11 on the two files whose class tables agree with the walk.
arc_2010.dwg settles CELLSTYLEMAP: it declares one, contains none, and
has no ACAD_ROUNDTRIP_2008_TABLESTYLE_CELLSTYLEMAP key for one to hang
from. The class parse is not in doubt — arc_2004.dwg's ten records
consume 5161 of the declared 5168 bits and eight of ten counts match
the walk exactly.

The KNOWN_SHORTFALL pins keep their counts (nothing was under-walked to
recover) and are re-documented as files that over-declare rather than as
an open walker gap; a drop in any walked figure still fails the ratchet.
CI gains the closure probe over the canonical fixtures.

Closes #76
Closes #77

* docs(changelog): state combined-tree coverage after merging main (3911/525/9 = 88.0%)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant