Skip to content

Fix section numbers being paired with the wrong section body - #45

Merged
nickvido merged 1 commit into
mainfrom
fix/section-number-pairing
Nov 21, 2026
Merged

nickvido merged 1 commit into
mainfrom
fix/section-number-pairing

Conversation

@nickvido

Copy link
Copy Markdown
Owner

Problem

In the generated us-code corpus, many sections carry the wrong number. The statute text is real, but the ## § N heading and section-N anchor belong to a neighbouring section. Examples at annual/2025:

  • The section labelled 42 U.S.C. § 12132 contains § 12134 (Regulations).
  • The section labelled 50 U.S.C. § 3035 contains § 3033 (Inspector General of the Intelligence Community).
  • The section labelled 12 U.S.C. § 5495 contains § 5491 (Establishment of the CFPB).

Cause

parseUslmToIr (src/transforms/uslm-to-ir.ts) parses the XML twice:

  • The non-ordered parse supplies each section's num, identifier, and metadata.
  • The order-preserving parse supplies heading, content, and notes.

The two section lists were paired by index. However, collectSectionNodesRecursive visits a node's subtitle/part/chapter/subchapter children before its direct <section> children, because the non-ordered tree groups children by tag. collectOrderedSectionNodesRecursive follows document order. So in any chapter that has sections before (or between) its subchapters or parts, every following section is shifted by one or more positions.

Fix

Before parsing, each <section start tag is stamped with its document position (data-us-code-tools-section-seq). The two views are then paired by that marker instead of by list index. Output is sorted downstream, so iteration order is unchanged. A test also asserts that the marker does not leak into the IR.

Verification

I compared each parsed (identifier, heading) with the <num>/<heading> pair taken straight from the raw XML at OLRC release point 119-73:

Title Sections Mismatched before After
7 2926 766 (26%) 0
12 1996 899 (45%) 0*
19 1111 394 (36%) 0*
25 2397 473 (20%) 0*
42 8461 3044 (36%) 0*
50 1360 436 (32%) 0

* A handful of residual differences are checker artifacts: bracketed xref text in headings, and a form inside a note. They are not pairing errors.

Spot checks now match official headings. For example, 42 § 12132 is "Discrimination", 50 § 3003 is "Definitions", 12 § 5491 is "Establishment of the Bureau of Consumer Financial Protection", and 12 § 1701q is "Supportive housing for the elderly".

  • I added tests/unit/transforms/section-pairing.test.ts. It fails on main and passes with this change.
  • Full suite: 216 passed, 17 failed. The same 17 fail on main in my environment because zip isn't installed; this change adds no new failures.

Follow-up

The us-code corpus needs to be regenerated (re-transform and backfill) to pick up the fix. Every vintage produced so far is affected.

🤖 Generated with Claude Code

parseUslmToIr reads each section's number from the non-ordered parse and
its heading/content/notes from the order-preserving parse, pairing the two
by list index. The non-ordered walk visits a container's hierarchy children
(subchapter, part, ...) before its direct <section> children, while the
ordered walk follows document order, so any chapter that mixes the two
shifted every following section onto its neighbour's number.

Stamp each <section> start tag with its document position before parsing
and pair the two views by that marker instead of by index.

On release point 119-73 this took heading/number mismatches from
26-45% of sections (titles 7, 12, 19, 25, 42, 50) to zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nickvido
nickvido merged commit d3de96e into main Nov 21, 2026
1 check failed
@nickvido
nickvido deleted the fix/section-number-pairing branch November 21, 2026 04:22
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.

1 participant