Skip to content

Import reads the WAV header, records the facts, asks on mismatch - #107

Merged
ninjudd merged 3 commits into
mainfrom
audio/import-reads-headers
Aug 9, 2026
Merged

ninjudd merged 3 commits into
mainfrom
audio/import-reads-headers

Conversation

@ninjudd

@ninjudd ninjudd commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

multi-rate.md step 1, decision 5 as recorded: field import gains the first content validation it has ever had. It reads the WAV header before any write, records sample_rate, bit_depth, channels, and frames in the ledger row — stored derived values in project-core.md §3.1's sense — and check verifies the recorded facts against the file it already hashes, so a hand-edited row that lies about its rate goes red. A rate that differs from the project's must be acknowledged: --allow-mismatch in scripts, a y/N prompt interactively, and the refusal happens before the copy, naming the flag. Refusing outright stays rejected, per the decision — with decision 4, a mismatched asset is a conversion cost, not an error, and the warning exists so nobody pays it unknowingly.

The WAV signature is sniffed by content, so the extension neither gates the safeguard nor fools it; a file whose header will not parse (or declares an impossible rate) records nothing and reports why — import has always been willing to carry bytes it does not understand, and several test fixtures rely on exactly that, so unparseable stays legal rather than becoming a new refusal. The fields are optional in the schema, so every ledger written before this rule stays valid; old rows simply carry no facts (the same forward-compat stance as #104's path rule). The normalize property tests import off-rate sources deliberately — the resampler is the thing under test — so their fixtures now acknowledge the mismatch, which reads as documentation of what they do.

The multi-rate status line moves to In progress with step 1 done, and the Multi-rate line moves from next.md to now.md.

Testing

From the repo root, pnpm build && pnpm typecheck && pnpm test — 557 tests pass (8 new: facts recorded and check-verified, mismatch refused before any write then acknowledged both ways, junk .wav recording nothing, a hand-edited sample_rate going check-red, and a partial row — the facts stripped down to a subset — refused by the schema so the verification gate cannot be dodged, a real WAV named .wave sniffed by content so the acknowledgement cannot be bypassed by filename, and a header declaring a zero sample rate recorded as nothing instead of as a row check rejects). The recording and the verification are both mutation-proven: neutralizing either turns its test red.

By hand, in a throwaway project (needs pnpm build so the field on your PATH is current):

cd "$(mktemp -d)"
field init --name demo
field song create creek
python3 -c "import struct,wave; w=wave.open('water.wav','wb'); w.setnchannels(2); w.setsampwidth(2); w.setframerate(48000); w.writeframes(struct.pack('<9600h', *[0]*9600)); w.close()"
python3 -c "import struct,wave; w=wave.open('tape.wav','wb'); w.setnchannels(2); w.setsampwidth(2); w.setframerate(44100); w.writeframes(struct.pack('<8820h', *[0]*8820)); w.close()"
field import water.wav --song creek
field import tape.wav --song creek < /dev/null
field import tape.wav --song creek --allow-mismatch
cat songs/creek/assets.yaml
field check

The first import prints imported water (audio/water/water.wav, 19244 bytes, 48000 Hz 16-bit 2ch, 4800 frames) — before this change the parenthetical stopped at the byte count and the ledger row at bytes:, which is where wrong would show. The second exits 1 with tape.wav is 44100 Hz against the project's 48000 — placements will pay a conversion (multi-rate.md §2.4); import it anyway with --allow-mismatch and leaves no file and no row behind. The third succeeds and records sample_rate: 44100. check says ok; edit sample_rate: 44100 to another value by hand and it says asset "tape" records sample_rate ... but the file says 44100. Run interactively (a real terminal, no < /dev/null) and the second import asks import anyway? [y/N] instead of exiting. State left behind: just the throwaway directory.

multi-rate step 1 (decision 5): the ledger row gains sample_rate,
bit_depth, channels, and frames — stored derived values in
project-core §3.1's sense, verified by check against the file it
already hashes. A rate differing from the project's must be
acknowledged (--allow-mismatch in scripts, y/N interactively) because
a mismatch is a conversion cost, not an error, and the warning fires
where the mistake is cheap to fix. A .wav whose header will not parse
records nothing and says so; import still carries bytes it does not
understand.
@minjudd

minjudd commented Aug 9, 2026

Copy link
Copy Markdown

Starting review of 047353d.

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed 047353d. The by-hand block runs verbatim and prints exactly what it says it will — imported water (audio/water/water.wav, 19244 bytes, 48000 Hz 16-bit 2ch, 4800 frames), then the refusal tape.wav is 44100 Hz against the project's 48000 … import it anyway with --allow-mismatch with exit 1 — and I checked the part that is easy to claim and easy to get wrong: after the refusal songs/creek/audio/ holds only water, so nothing was copied and no row was written. The --allow-mismatch retry records sample_rate: 44100, and hand-editing it to 96000 gives asset "tape" records sample_rate 96000 but the file says 44100.

The rest of the surface behaves as documented: a junk .wav imports with note: WAV header unreadable, nothing recorded — junk.wav: not a RIFF/WAVE file and no fields; a non-.wav extension records nothing even when the bytes are a WAV, which is what "reads the WAV header" scopes it to; and the continue after a sha mismatch keeps a drifted file from also being accused of lying about its rate — one mistake, one message.

The forward-compat stance holds where it counts: the fields are optional, so every pre-existing ledger stays valid, and check on rows without them is silent. That is also where the finding lives — the silence is broader than intended.

Docs match the code: the plan's status line moves to In progress naming step 1 and what is left, the list line moves next → now with blocks reworded to unblocks, and project-model.md describes the recorded facts and the acknowledgement. 554 tests, 455 pass, 41 skip, 58 fail — the same 58 as main, all field-audiod is not built. pnpm typecheck clean.

Comment thread src/model/project.ts Resolved
The verification gate keys on sample_rate, so a row that omitted it
was never checked at all — channels could say 7 against a 2-channel
file and check stayed ok. The refine makes the together-or-not-at-all
rule the schema's (the voice_ref/voice_ref_sha256 precedent), which
makes the gate correct by construction and turns a partial row into a
check-time message instead of a silent pass.
@minjudd

minjudd commented Aug 9, 2026

Copy link
Copy Markdown

Starting review of 7673e2e.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 047353df38

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/schema/assets.ts Resolved
Comment thread src/commands/import.ts Resolved
Comment thread src/commands/import.ts Resolved
minjudd
minjudd previously approved these changes Aug 9, 2026

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving 7673e2e. The schema refine is the fix that makes the gate correct rather than the fix that patches around it — sample_rate can now stand for all four because the format guarantees they arrive together, so the continue below it is no longer an assumption.

The three cases, on the ledger from your own Testing block:

full rows                              ok
no sample_rate, channels: 7            WAV facts travel together — record sample_rate, bit_depth,
                                       channels, and frames, or none (multi-rate.md decision 5) at assets.1
all four present, channels: 7          asset "tape" records channels 7 but the file says 2
no facts at all (a pre-rule ledger)     ok

The row that dodged verification is now refused at load, the lie it was hiding is caught when the row is complete, and forward compatibility is intact — a ledger written before this rule still checks green, which is the promise the optional fields exist for. at assets.1 locates the row, which matters in a file that is a list.

The comment is worth noting for the next reader: it now says the refine is what makes "recorded together or not at all" true "rather than merely written down", and names the gate that depends on it. That is the part that keeps someone from relaxing the refine later without noticing what it holds up.

555 tests, 456 pass, 41 skip, 58 fail — the same 58 as main. pnpm typecheck clean, and assetSchema has no .shape/.extend() callers that the ZodObject → refined change could have broken.

No findings.

Two holes in the new safeguard, both review-found: a real WAV named
.wave (or nothing) bypassed the acknowledgement by filename alone —
the signature is now probed wherever the extension does not claim it
— and a structurally parseable header declaring a zero sample rate
sailed through the mismatch prompt into a ledger row the schema
refuses, reporting success on an import check immediately rejected.
Facts that cannot be true are a header problem now, recorded as
nothing.
@minjudd

minjudd commented Aug 9, 2026

Copy link
Copy Markdown

Starting review of baea8f9.

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving baea8f9. Both hardenings are real, and the second is precisely scoped rather than accidentally so — I checked whether it was the whole of the problem.

The spelling bypass is closed. A 44.1k WAV renamed .wave, and one with no extension at all, both refuse now: b.wave is 44100 Hz against the project's 48000 …, noext is 44100 Hz …. Before this, either would have imported silently at the wrong rate — the acknowledgement was only as strong as the filename, which is the one thing about a file nobody has to mean.

sampleRate really was the only one of the four that could land an impossible value, which is what makes the narrow fix correct rather than a first patch. readHeaderFrom computes bytesPerFrame = (bitsPerSample / 8) * channels and throws on a falsy one, so channels: 0 and bitsPerSample: 0 never reach the ledger — confirmed against hand-corrupted fmt chunks, both missing fmt or data chunk, nothing recorded. frames is floor-divided and the schema allows 0. Rate alone slips through the arithmetic, and now it does not: a zeroed rate imports the bytes, records nothing, and check is ok — where before it would have prompted for an acknowledgement whose reward was a row the schema refuses.

Sniffing is 12 bytes on a descriptor already known to be a file, so the cost lands only on non-.wav imports, and a genuinely non-audio file (plain.bin) sniffs false and imports as it always did.

557 tests, 458 pass, 41 skip, 58 fail — the same 58 as main. pnpm typecheck clean.

One cosmetic thing: the zero-rate case prints note: WAV header unreadable, nothing recorded — header declares a sample rate of 0 — nothing recorded. The header was perfectly readable, and "nothing recorded" lands twice. The headerProblem string carries a full sentence where the CLI's prefix already supplies one, so header declares a sample rate of 0 alone would read right in both spots.

No findings.

@ninjudd
ninjudd merged commit e3f3db0 into main Aug 9, 2026
1 check passed
@ninjudd
ninjudd deleted the audio/import-reads-headers branch August 9, 2026 23:17
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.

2 participants