Skip to content

feat(artifacts): inspect NES 2.0 container headers and regions - #10674

Merged
morluto merged 14 commits into
morluto:mainfrom
Davmunrey:claude/nes2-header
Nov 19, 2026
Merged

morluto merged 14 commits into
morluto:mainfrom
Davmunrey:claude/nes2-header

Conversation

@Davmunrey

@Davmunrey Davmunrey commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Refs #2072.

The iNES reader added in #2816 refused NES 2.0 headers. This change parses the NES 2.0 variant and inventories the declared trainer, PRG-ROM, CHR-ROM, and remaining regions. The region scan and reconstruction keep original bytes and source offsets.

  • ROM sizes include the exponent-multiplier form. The parser checks the exact combined extent before computing file offsets.
  • Byte 15 carries a seven-bit expansion-device code. The highest bit remains reserved.
  • NES 2.0 reports the two raw nametable-layout flags as declarations. The effective layout can depend on the mapper.
  • Classic iNES preserves its existing header, regions and inventory limitation. It retains the same Evidence for the same input and policy.

Validation

  • All 100 focused parser, inventory, extraction, cancellation and compiled CLI/MCP acceptance tests passed on the refreshed head. The positive 0x40 expansion-device case and raw nametable flag assertions failed on the original contributor head and passed after the repair.
  • The audited size implementation agreed with an independent BigInt calculation for all 65,536 paired exponent encodings, with exact oversized diagnostics.
  • Compiled CLI inspection of the same classic ROM returned byte-identical JSON against the audited main build, both with the explicit fail integrity policy and with the default arguments on the refreshed head. Documentation, cold static checks and available catalog validators passed on the repair.
  • Validation used authored fixtures. Real cartridge images, emulator behavior, Windows execution, and NES 2.0 extraction through MCP were outside this run.

Author readiness

Human owner: @Davmunrey

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

Maintainer update

Refreshed against main and resolved the raw-read baseline conflict. The feature source and tests retain the contributor revision. The focused parser, artifact recognition, filesystem, and cancellation suites passed all 131 cases on the refreshed head. The architecture guard passed. The previous contributor head passed hosted CI. Fresh hosted checks are pending.

Problem: the iNES lane refused every header whose byte 7 bits 2-3 select
NES 2.0, so NES 2.0 cartridges could not be inventoried, extracted or
reconstructed even though their file layout extends classic iNES.

Fix: the single iNES-family header parser now reads NES 2.0 alongside
classic iNES. It reports the 12-bit mapper, submapper, 12-bit and
exponent-multiplier PRG/CHR sizes, declared PRG/CHR RAM and NVRAM sizes,
CPU/PPU timing, console type with Vs. System and extended console codes,
miscellaneous ROM count and default expansion device in a "nes-2.0"
header variant; classic headers keep their exact serialization. Regions
add one prg.bin/chr.bin for exponent sizes and misc-rom.bin for the bytes
after CHR-ROM when miscellaneous ROMs are declared. Oversized
declarations (with exact totals, including exponent sizes beyond safe
integers), nonzero unused header bits, empty PRG-ROM and declared but
absent miscellaneous ROM bytes are refused through the existing format
error. Nested byte classification now admits NES 2.0 through the same
parser. The reader limitation, tool description, guide and skill
reference now state that UNIF remains unsupported and that RAM, timing
and console values are declarations.

Validation: new synthetic NES 2.0 unit cases fail on the base (15 of 24)
and pass after the change; focused boundary and acceptance iNES suites
(75 tests) pass, including NES 2.0 inventory, extraction, reconstruction
and compiled CLI inspection; npm run check:fast passes.

Refs morluto#2072
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-14T20:59:38.772021Z
We couldn’t complete this review. Comment @codex review to try again.
3abd74b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5 Tier: plus

[Medium impact] The PR extends a bounded artifact-inspection and extraction workflow for NES cartridge files.

Satisfy the shared-reader and feature-documentation requirements before merging.

Findings

  1. P2 Header reads bypass required reader ▶
  2. P2 Required feature records are missing ▶
Fix with agent prompt
### Issue 1
src/domain/inesContainer.ts:28-30
The new `headerByte` wrapper still reads fields with `Buffer.readUInt8`. The parser guide requires bounded fields to use `BinaryReader`. Existing length checks protect these calls today, but moving raw reads into a helper does not satisfy that requirement. Use `BinaryReader.u8` as the shared owner of checked header reads. This repository requirement must be satisfied before merging.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

### Issue 2
docs/ines-containers.md:27-36
NES 2.0 support adds new header fields, size rules and region behavior, but no matching feature specification, plan or tasks are tracked under `docs/specs`. The documentation guide requires those three records for a feature and distinguishes them from a usage guide. Add the feature records and link this guide to them so the intended behavior and acceptance checks have a clear home. This repository requirement must be satisfied before merging.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds NES 2.0 header inspection and preserves declared ROM regions, original offsets and reconstruction facts.

  • NES 2.0 headers now describe their ROM areas and cartridge declarations.
  • NES 2.0 regions now appear in artifact inventories and extraction.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Selected file] --> B[Read signature and header]
  B --> C{Header variant}
  C --> D[Classic iNES declarations]
  C --> E[NES 2.0 declarations]
  D --> F[Check combined extent]
  E --> F
  F -->|Does not fit| G[Typed parse failure]
  F -->|Fits| H[Build ordered file regions]
  H --> I[Hash regions and retain source offsets]
  I --> J[Verify order, byte count and whole-file digest]
  J --> K[Inspection Evidence or extraction]
Loading

Reviews (12) · Last reviewed commit: "Merge main into nes2-header" · Reviewed by Greptile

Comment thread src/domain/inesContainer.test.ts Resolved
Review follow-up for NES 2.0 headers.

The exponent-multiplier case stopped at parseInesLayout, and the
extraction test used counted ROM units, so prg.bin and chr.bin from
an exponent-sized area never went through the reader or extraction.
A boundary case now builds a NES 2.0 file with a 2^10 * 3 byte PRG-ROM
area beside one counted 8 KiB CHR-ROM unit. It checks the header omits
the PRG unit count, the source offsets of prg.bin and chr/000.bin,
the verified reconstruction order, and that the extracted regions
match the original bytes exactly.

Tests: tests/boundary/filesystem/inesArtifact.test.ts passes (38).
The NES 2.0 declaration added a sixth raw Buffer read to
inesContainer.ts. On current main the architecture guard allows five
there, so the merged tree failed Static checks. iNES and NES 2.0
declarations and the format probe now read header bytes through one
headerByte helper. The file has one raw read, and its counted baseline
drops from 5 to 1 through --shrink-baselines.

Tests: inesContainer domain tests (24) and the NES boundary and
acceptance tests (76) pass. architecture-guards, format:check and
check:ci:fast pass on the branch merged with main.
Comment thread src/domain/inesContainer.ts Resolved
@greptile-apps

greptile-apps Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

Main now recognizes artifact formats through one registry, which already admits NES 2.0 through parseInesLayout; the old classifier chain is dropped in favour of it.
Review follow-up: the NES 2.0 declaration added plain-string failures.
The iNES-family parser now returns Result<_, ParseFailure> with its
kind and where it was found:
- malformed: a missing signature, a dirty legacy header, an empty
  PRG-ROM area, reserved NES 2.0 bits or an invalid file extent;
- truncated: a short header, a short payload, or declared
  miscellaneous ROM with no bytes;
- unsupported: UNIF or another header variant, with undecoded content.
The detail text is unchanged.

The format registry refuses with the detail, and the iNES reader and
root classifier translate the failure at their boundary into the
existing format ArtifactReaderFailure, with the ParseFailureError as
its cause.

Tests: the refusal table now asserts each failure's kind and format
as well as its detail. iNES domain and recognition tests, NES boundary
and acceptance tests (76), architecture-guards, format:check and
check:ci:fast pass on the branch merged with main.
Main imports the artifact reader helpers from src/resources, gives inspect_artifact explicit tool effects, and keeps each counted guard's baseline in its own file. The merge keeps the branch's ParseFailureError imports and its NES 2.0 contract wording, and lowers the raw-buffer-read baseline for inesContainer.ts from 5 to 1, as the branch did.
@morluto
morluto enabled auto-merge (squash) November 19, 2026 20:38
@morluto
morluto merged commit cd3590c into morluto:main Nov 19, 2026
51 checks passed
Comment on lines +28 to +30
/** The one reader of header bytes; callers index inside the admitted prefix. */
const headerByte = (prefix: Buffer, index: number): number =>
prefix.readUInt8(index);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Header reads bypass required reader

The new headerByte wrapper still reads fields with Buffer.readUInt8. The parser guide requires bounded fields to use BinaryReader. Existing length checks protect these calls today, but moving raw reads into a helper does not satisfy that requirement. Use BinaryReader.u8 as the shared owner of checked header reads. This repository requirement must be satisfied before merging.

Context Used: AGENTS.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/domain/inesContainer.ts
Line: 28-30

Comment:
**Header reads bypass required reader**

The new `headerByte` wrapper still reads fields with `Buffer.readUInt8`. The parser guide requires bounded fields to use `BinaryReader`. Existing length checks protect these calls today, but moving raw reads into a helper does not satisfy that requirement. Use `BinaryReader.u8` as the shared owner of checked header reads. This repository requirement must be satisfied before merging.

**Context Used:** AGENTS.md ([source](https://github.com/morluto/rea/blob/main/AGENTS.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread docs/ines-containers.md
Comment on lines +27 to +36
- NES 2.0 headers add `prg_rom_bytes`/`chr_rom_bytes`, the 12-bit mapper and
`submapper`, `console_type`, `cpu_ppu_timing`, declared PRG-RAM,
PRG-NVRAM, CHR-RAM and CHR-NVRAM byte sizes (`0` when none is declared),
`misc_rom_count` and `default_expansion_device`. Vs. System headers add
`vs_ppu_type` and `vs_hardware_type`; extended console headers add
`extended_console_type`. These codes are reported as numbers. When the
exponent-multiplier form declares a ROM size, its unit count is omitted
rather than reported as zero. `nametable_layout_flags` preserves byte 6's
`arrangement_bit` (bit 0) and `alternative_layout_bit` (bit 3) as header
flags. Their effective behavior can depend on the mapper.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Required feature records are missing

NES 2.0 support adds new header fields, size rules and region behavior, but no matching feature specification, plan or tasks are tracked under docs/specs. The documentation guide requires those three records for a feature and distinguishes them from a usage guide. Add the feature records and link this guide to them so the intended behavior and acceptance checks have a clear home. This repository requirement must be satisfied before merging.

Context Used: Documentation (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: docs/ines-containers.md
Line: 27-36

Comment:
**Required feature records are missing**

NES 2.0 support adds new header fields, size rules and region behavior, but no matching feature specification, plan or tasks are tracked under `docs/specs`. The documentation guide requires those three records for a feature and distinguishes them from a usage guide. Add the feature records and link this guide to them so the intended behavior and acceptance checks have a clear home. This repository requirement must be satisfied before merging.

**Context Used:** Documentation ([source](https://github.com/morluto/rea/blob/main/docs))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@morluto morluto added the area: evidence Evidence, artifacts, and reconstruction label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: evidence Evidence, artifacts, and reconstruction enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants