Skip to content

vdf: derive results via direct record map and decoded forward link - #508

Merged
bpowers merged 3 commits into
mainfrom
vdf-direct-record-map
May 9, 2026
Merged

bpowers merged 3 commits into
mainfrom
vdf-direct-record-map

Conversation

@bpowers

@bpowers bpowers commented May 9, 2026

Copy link
Copy Markdown
Owner

Summary

Refactors to_results_via_records (Rust) and extract_named_results (Python xray) to use the deterministic record-to-OT chain that Vensim's writer produces, replacing the previous overlap-selection DP and within-candidate lookup-name filter. Section-1 records carry the direct map (f[2] -> name, f[6] -> shape, f[11] -> OT-start), and graphical-function descriptors are removed via the decoded forward link into the section-6 lookup-record array (case-insensitive alphabetical order of the lookup-def names).

The f[11] owner/descriptor union is the only on-disk ambiguity; the field-by-field analysis in docs/design/vdf.md confirms no byte distinguishes the two. Descriptor identification therefore proceeds by overlap-component analysis: spans that share OT slots form connected components, and within each component the lexical lookup-def name match (or, in Ref.vdf's case, the highest-f[10] fallback) selects the descriptor record.

This brings xray and Rust into agreement on the result set, replacing the slot-alignment-derived hide-set that made xray's result count depend on slot-table layout noise. Closes #500.

Notable changes

Engine (src/simlin-engine/src/vdf.rs, src/simlin-engine/src/vdf/record_results.rs)

  • to_results_via_records becomes a thin wrapper around build_record_result_columns, which composes decoded_record_spans -> identify_descriptor_records -> name-partitioned emission.
  • New OT class-code constants (0x05/0x16/0x17/0x18 alongside 0x08/0x11) and an is_owner_ot_class_code predicate so the class-code guard rejects records whose f[11]-as-OT-start would land on a non-owner slot.
  • The previous DP-based overlap selector and the within-candidate lookup-name filter are removed.
  • is_lookupish_name's docstring is updated to frame it as a model-free reader's lexical approximation of the decoded forward link, not a heuristic.
  • New unit test test_identify_descriptor_records_uses_f10_fallback_on_ref_vdf pins the Ref.vdf fallback case and confirms small-model fixtures don't need it.

xray (tools/vdf_xray.py, tools/test_vdf_xray.py)

  • Same refactor: extract_named_results_with_diagnostics uses decoded_record_spans + identify_descriptor_records as the primary path, with a used_descriptor_f10_fallback diagnostic.
  • Class-code guard added to decoded_record_spans.
  • Corpus precision moves from 31 exact-by-xray / 9 not-proven to 39 / 1 (only Ref.vdf, exact via fallback but flagged).

Docs and notes

  • docs/design/vdf.md updates the pinned-facts section to document the direct map as the primary path, the SMOOTH/macro structure, the zeroed-record decode, the absence of a version marker, the axis_ref = 60 + 16*k formula, and the owner/descriptor reframe-confirmed status.
  • New analysis notes under tools/vdf_xray_notes/ document the corpus survey, the heuristic audit, the helper-signature region, the model-editing diff sequence, the owner/descriptor field analysis, and the synthesis plan that drove this refactor.

Pre-existing test fix (separate commit)

  • serve: gate headless-linux open_browser test on xdg-open behavior -- the strict assertion was false-positive on Linux distros that ship an xdg-open returning 0 even without $DISPLAY (Fedora's Asahi spin); probe xdg-open and skip the assertion when it succeeds. Pre-existing failure on main, surfaced by the pre-commit hook on this branch's first commit.

Related issues

Test plan

  • cargo test -p simlin-engine (3199 pass)
  • python3 -m unittest tools.test_vdf_xray (95 pass)
  • Pre-commit hook clean on both commits
  • New test_identify_descriptor_records_uses_f10_fallback_on_ref_vdf validates the Ref.vdf fallback fires and small models don't need it

bpowers added 2 commits May 9, 2026 06:23
Fedora's Asahi spin (and presumably other distros) ships an `xdg-open`
that returns 0 even when `$DISPLAY` is unset, so the strict assertion
"open_browser must return false on headless Linux" is false-positive on
those hosts. Probe `xdg-open` directly and skip the assertion when it
succeeds; the relaxed `open_browser_does_not_panic` test continues to
exercise the bool-flow contract on every Linux host.
Refactor `to_results_via_records` (Rust) and `extract_named_results`
(Python xray) to use the deterministic record-to-OT chain that Vensim's
writer produces, replacing the previous overlap-selection DP and
within-candidate lookup-name filter. Section-1 records carry the direct
map (`f[2]` -> name, `f[6]` -> shape, `f[11]` -> OT-start), and
graphical-function descriptors are removed via the decoded forward link
into the section-6 lookup-record array (case-insensitive alphabetical
order of the lookup-def names).

The `f[11]` owner/descriptor union is the only on-disk ambiguity; the
field-by-field analysis in `docs/design/vdf.md` confirms no byte
distinguishes the two. Descriptor identification therefore proceeds by
overlap-component analysis: spans that share OT slots form connected
components, and within each component the lexical lookup-def name match
(or, in `Ref.vdf`'s case, the highest-`f[10]` fallback) selects the
descriptor record. This avoids the slot-alignment-derived hide-set that
made the xray result count depend on slot-table layout noise (#500), and
the xray and Rust paths now agree on the result set.

The change adds class-code constants for `0x05`/`0x16`/`0x17`/`0x18` OT
codes (alongside `0x08`/`0x11`) so the class-code guard rejects records
whose `f[11]`-as-OT-start would land on a non-owner slot, ports the
descriptor identification to `record_results.rs`, drops the now-unused
overlap-selection DP, and updates `docs/design/vdf.md` to document the
direct map as the primary path. The corpus precision report moves from
31 exact-by-xray / 9 not-proven to 39 / 1 (only `Ref.vdf`, where the
`f[10]` fallback is exact but flagged).
@claude

claude Bot commented May 9, 2026

Copy link
Copy Markdown

Review summary

The PR replaces the overlap-selection DP and within-candidate lookup-name filter with a deterministic record-to-OT chain, plus a class-code guard and overlap-component descriptor identification. The corpus precision improves from 31/9 to 39/1, and Rust/Python paths now agree. Below are a few correctness-adjacent observations.

[P3] tools/test_vdf_xray.py — test name and docstring reference fixtures that are no longer "not-proven"

The new test_decoded_record_spans_overlap_count_pinned_on_not_proven_fixtures test and the NOT_PROVEN_OVERLAP_PAIRS dict reference 8 fixtures (lookup_ex, econ x5, WRLD3 x2) that the same PR moves to exact-by-xray (per the corpus-precision assertion status_counts["not-proven"] == 1, only Ref.vdf). The test still passes since it pins a structural property of decoded_record_spans before descriptor identification, but the test name, the dict name, and the docstring phrasing "On not-proven fixtures, the overlap count..." are misleading post-PR. Worth renaming/rewording.

[P3] tools/vdf_xray.py — used_system_variable_fallback semantics changed

In extract_named_results_with_diagnostics, the new branch sets diagnostics.used_system_variable_fallback = True whenever a system variable lacks a direct record, then continues (silently dropping it). The previous code only set this flag when a fallback path actually recovered an OT for a missing system variable. The flag name still reads as "we used a fallback to recover" but its post-PR meaning is "this system variable is missing and was dropped from results." The docstring acknowledges the flag is dead on the corpus, but if it ever fires, the name will misreport what happened — consider renaming to e.g. system_variable_record_missing.

[P3] Class-code guard diverges on out-of-bounds OT indices between Rust and Python

In src/simlin-engine/src/vdf/record_results.rs, the Rust class-code guard uses code_vec.get(ot_idx).copied().unwrap_or(0) and is_owner_ot_class_code(0) returns false, so an OT index past the end of codes causes the span to be rejected. Python (tools/vdf_xray.py decoded_record_spans) uses ot_idx < len(codes) and codes[ot_idx] not in _OWNER_OT_CLASS_CODES, where the short-circuit False for out-of-bounds slots causes the span to be accepted. Because section6_ot_class_codes() typically returns one entry per OT slot and decoded_record_spans already gates on end > offset_table_count, this divergence does not affect any corpus fixture today, but it is a behavioral asymmetry between the two readers that the PR otherwise works hard to align.

Overall correctness verdict

Correct. The corpus precision results (39/1) and Rust/Python parity goals are met, and the descriptor identification logic mirrors symmetrically across the two implementations. The findings above are doc/clarity nits rather than blocking issues.

Three doc/clarity nits flagged on PR #508:

1. Rename `NOT_PROVEN_OVERLAP_PAIRS` and the corresponding xray test to
   `DESCRIPTOR_CONFLICT_PAIRS` /
   `..._pinned_on_descriptor_conflict_fixtures`. The dict and test pin
   the raw descriptor/owner overlap shape produced by
   `decoded_record_spans` *before* `identify_descriptor_records` runs;
   after that step these fixtures all classify as `exact-by-xray` in
   the precision report, so the previous "not-proven" framing was
   misleading post-PR.

2. Rename `used_system_variable_fallback` to
   `system_variable_record_missing` (and the matching reason string to
   `system-variable-record-missing`). The post-PR meaning is "this
   system variable lacks a direct record and was silently dropped from
   the result set", not "we used a fallback to recover it"; the
   previous name still implied recovery.

3. Align the Rust class-code guard's out-of-bounds behaviour with the
   Python xray. Previously the Rust path treated a missing class code
   as `0` (which `is_owner_ot_class_code` rejects) while Python
   short-circuited OOB indices into "no rejection signal". Both pass
   today on every corpus fixture because the upstream
   `end > offset_table_count` gate already covers the realistic OOB
   case, but the symmetry goal of the PR motivates aligning them.
   Pick the Python rule: only in-bounds non-owner codes reject the
   span, and a short class-code array is treated as a parser defect
   rather than a span-level signal.
@claude

claude Bot commented May 9, 2026

Copy link
Copy Markdown

Review summary

Looked through the three commits: the serve xdg-open probe, the direct-record-map refactor (decoded_record_spans + identify_descriptor_records + build_record_result_columns), and the rename/symmetry follow-up. The pipeline-style decomposition is clean, the docstrings and vdf.md updates carry the rationale well, and the corpus tests pin both the post-resolution exact-by-xray shape and the pre-resolution per-fixture descriptor-conflict counts.

Specifics I checked end-to-end:

  • Rust↔Python class-code guard symmetry (record_results.rs:147-169, vdf_xray.py:3279-3287): both now short-circuit OOB indices into "no rejection signal" and accept short codes arrays as a parser-defect tolerance. The upstream end > offset_table_count gate plus the actual section6_ot_class_codes() length (always offset_table_count on the primary path) makes the gap purely defensive on the corpus.
  • Determinism of identify_descriptor_records (record_results.rs:252-364): although by_slot.values() and components.values() iterate in non-deterministic HashMap order, every component is processed independently, the descriptor membership is collected into a HashSet, and per-component span ordering inherits the deterministic for (i, _) in spans.iter().enumerate() push order. The max_by_key((sort_key, rec_idx)) tie-break is total, so the f10 fallback is deterministic.
  • build_record_result_columns dedup (record_results.rs:518-538): keeping the lowest-start span per name with Some(prev) if prev.start <= span.start => {} matches the Python if prev is None or span.start < prev.start behavior — first-iterated wins on ties in both.
  • Lookup descriptor handling: descriptors with f[11]=0 (e.g. #inline lookup table# in lookup_ex) are filtered out by start <= 0 in decoded_record_spans rather than reaching identify_descriptor_records, so they correctly never overlap with anything; the regression test test_to_results_via_records_lookup_ex_separates_lookup_definition_from_output doesn't require the descriptor name to appear as a result column, only that it not steal another OT.
  • System variable diagnostic asymmetry: Rust silently drops missing system records while Python flags system_variable_record_missing. Python is the only side with a precision report, so this is consistent with the ownership of the diagnostic surface.

Findings

No correctness, performance, or security issues that meet the bar for filing.

A couple of low-priority observations not worth blocking on:

  • CorpusDecodedRecordSpanCoverageTests.EXACT_BY_XRAY (tools/test_vdf_xray.py:2009-2040) is "fixtures whose raw decoded_record_spans has no overlaps", which is now distinct from the precision report's exact-by-xray (which counts post-identify_descriptor_records). The names happen to coincide on those 31 fixtures but the 8 in DESCRIPTOR_CONFLICT_PAIRS are also exact-by-xray per the report. A docstring renaming to e.g. RAW_NON_OVERLAPPING would prevent confusion in the next round of edits, but the tests themselves are correct.
  • The iterative comp_by_slot rebuild in identify_descriptor_records is O(D · K · avg_span) per component (D descriptors peeled, K active spans). Tractable on the current corpus (Ref.vdf's 35 descriptors over ~849 spans is fine) but worth noting if the corpus grows.

Overall correctness verdict

Correct. The patch preserves existing behavior on every clean fixture, resolves the documented field[11] owner/descriptor union via the decoded forward link, and is well-pinned by both the existing Rust tests and the new corpus-coverage test classes. The pre-existing xdg-open probe is appropriately scoped to skip rather than weaken the headless assertion.

@bpowers
bpowers merged commit 479569e into main May 9, 2026
12 of 13 checks passed
@bpowers
bpowers deleted the vdf-direct-record-map branch May 9, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tools: vdf_xray hidden-block rule keys off slot-alignment noise; xray and Rust disagree on result set

1 participant