Skip to content

Keep a loop take running when the heartbeat falls behind its wraps (recording.md §15.4) - #204

Merged
ninjudd merged 10 commits into
mainfrom
recording/loop-take-race
Oct 11, 2026
Merged

ninjudd merged 10 commits into
mainfrom
recording/loop-take-race

Conversation

@ninjudd

@ninjudd ninjudd commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

A loop take (field record --loop, or record with loop over the socket) lands one take per pass of its region. At each wrap the audio callback ends the pass's take and takes over the standby, a transaction the heartbeat on the message thread opened ahead of the wrap. The heartbeat then finalizes the ended take and opens the next standby. The heartbeat worked out what the callback had done from a done flag and the standby slot, and when it fell behind the wraps, that guess went wrong in the two ways #203 read from the code. Both races reproduce every time with the tests below, and the second is the crash.

  • A wrap before the heartbeat serviced the last pass stopped the loop. The callback found the done flag still set. It cleared activeCapture and counted a missed pass, but it never asked for a new start, so no later block took a standby over and no further take landed. A region shorter than the heartbeat's 250 ms tick does this within the first few wraps.
  • A wrap during a finalize left the daemon with no take. serviceCapture cleared the flag, finalized the take, and only then opened the next standby, so a wrap in between found none. The callback took the late standby over a block after it opened, in the middle of the pass, which recording.md §15.4 rules out. After a slow tick, JUCE runs the heartbeat again at once, so that tick usually came before the block. It saw the standby still in the slot, left capture null, and at the next pass called finalizeTake (*finished) on null. The macOS crash report shows EXC_BAD_ACCESS at 0x128 in Transaction::finalize, called from Player::serviceCapture. A null capture also drops loop_takes from status, reports the record state as armed, and lets through every request the daemon refuses while recording.

The fix stops the heartbeat from guessing. The done flag becomes capturesEnded, a count of the takes the callback has ended. The callback begins and ends a loop's takes in the order the heartbeat opened them, so the heartbeat finalizes that many and moves each next take up into capture. That take is either recording or waiting for the wrap that begins it, so capture stays set for as long as a loop runs, and the heartbeat never reads the slot to decide who owns what. The callback takes a standby over only at a wrap. A pass that begins with none ready is therefore skipped whole and counted in record.missed, as §15.4 decided, and every take starts at at and runs the whole region. While the next take waits for its wrap, status reports it as pending. The heartbeat now opens the next standby before it finalizes the last pass, so a slow finalize costs no pass.

Stop changes to match. It discards a take still waiting for its wrap, as it already discarded an unused standby. It finalizes a standby that a wrap began just before the stop, which it used to discard with its samples. It also detaches a second time, because the block in flight can take the standby over just before the first detach and store it in activeCapture just after. On the audio thread the change only loads, stores, exchanges, and increments atomics, so the callback still takes no lock and allocates nothing.

A session swap, which field play --watch sends after an edit and so after a landed take changes the chosen one, now carries the take's region onto the incoming session. Without it the clock ran on past until, and with the callback skipping between passes, every block ended a pass and landed a 512-frame take. loadSession sets the region before the pre-roll, so the chain warms on it, and again under the message thread's lock, where it refuses a session the region does not fit. With a region on the incoming session, the swap carries the position unwrapped and lets the region place it, where a looping session's length would otherwise wrap it.

A pass also ends in a block that ends exactly on until. Blocks start on the grid the locate to at sets, and the block after one that ends on until already begins at at, so the callback's old test, until strictly inside the block, never saw that wrap and the take ran on into the next pass. A region a whole number of blocks long never ended a pass, and a one-bar loop at 120 bpm merged every other pass. That predates #203, but the docs here promise whole passes. A punch-out still ends on the same sample, a block sooner.

Four new tests in src/commands/punch-daemon.test.ts reproduce the defects. One records a loop a tenth of a second long, so at least two wraps fall between any two heartbeat ticks. The other sets FIELD_AUDIOD_SERVICE_STALL_MS=600, a test seam the daemon honours only with --null-output. It holds the heartbeat between taking over a finished take and finalizing it, so a wrap lands inside every finalize. Both tests poll status throughout and require each poll to report the loop with its take count. Both require each take to be a whole, gapless pass from the region's top. The first commit adds only the seam and the tests to main's handoff. Against it, the short loop timed out in 5 of 5 runs and the stall lost the daemon in 5 of 5. The third swaps the session mid-loop, first to a song too short for the region, which must be refused, then to the same song, after which the region must still be in force and the passes whole. Against 786e25d the short song's swap was accepted. The fourth loops over a region 48 blocks long, and against cb48e36 no take ever landed. docs/audio-engine.md now describes the skipped pass, the pending take, and the carried region, and an amendment to recording.md §15.4 records what the first build did.

Fixes #203.

The review's carried-over suggestion is fixed too. On a bed already rolling, record jumped to at, then opened the take's transactions, then armed the start, so the blocks the bed played while the transactions opened carried it past at, and the first take started late. record now opens the transactions first. It then jumps to at and arms the start in one hold of liveLock. The callback only tries that lock, so no block runs between the two. locate keeps its refusal under a take and calls a new seek, which record uses. A second seam, FIELD_AUDIOD_OPEN_STALL_MS, is honoured only with null output like the first. It stalls the open, so a fifth test can make it outlast 200 ms of blocks, on a live bed and on a file bed, and require each take to start at 24000. With the old order, that test starts the take at 33728. Because the transactions now open before the jump, a record refused for its at or its play discards them, rather than leaving incomplete takes for recordings to list. The same test sends an at past the bed's end and requires the refusal to leave none.

Testing

From the root of a checkout of recording/loop-take-race, after pnpm install --frozen-lockfile:

cmake -S native/audiod -B native/audiod/build -DCMAKE_BUILD_TYPE=Release
cmake --build native/audiod/build --parallel 8 --config Release
export FIELD_AUDIOD=$PWD/native/audiod/build/field-audiod_artefacts/Release/field-audiod
pnpm typecheck
pnpm exec vitest run src/commands/punch-daemon.test.ts
for i in $(seq 1 25); do pnpm exec vitest run src/commands/punch-daemon.test.ts > /dev/null || echo "run $i failed"; done
pnpm test
project check

tsc --noEmit prints nothing. punch-daemon.test.ts reports Tests 11 passed (11), including "a take at at on a bed already playing starts there, however long its transactions take to open", "a loop shorter than the heartbeat tick goes on landing whole passes, and counts the ones it skips", "a heartbeat that stalls across a wrap keeps the loop, its count, and whole passes", "a session swap mid-loop carries the take region, and one it does not fit is refused", and "a region a whole number of blocks long ends every pass on its until". The loop prints nothing: I ran it 25 times in a row on macOS with no failure, and 20 more times four at a time, to load the machine, also with no failure. The suite reports Test Files 83 passed (83) and Tests 1084 passed | 1 skipped (1085), where the skip is "refuses to arm for MIDI where there is no MIDI source", which runs only without a MIDI service, and project check prints "Project plans are valid."

Each failure names what is still wrong. "timed out waiting for 4 passes to land" from the short loop means two wraps between ticks still stop the loop. "field-audiod connection closed" from the stalled heartbeat means the daemon still dereferences a null take. A followLoop assertion that the record is armed rather than recording, or that loop_takes is missing, means the daemon lost the loop's take. A started_at_samples other than 24000 means a take began mid-pass, or, from the rolling-bed test, that a block ran between the jump to at and the start. "promise resolved ... instead of rejecting" or a missing loop_from from the swap test means a swap still drops the region, and a take shorter than 24000 frames means passes are ending on every block again. "timed out waiting for 3 passes to land" from the 48-block region means a block that ends on until still hides the wrap. To see the first two failures, build commit cd088df and run punch-daemon.test.ts again. To see the swap test fail, build the daemon at 786e25d and run this branch's punch-daemon.test.ts against it.

The commands leave the daemon build in native/audiod/build. The tests remove their temp projects and quit the daemons they spawn. The windows CI job is the check that matters most for #203, because its runner is slow enough to have shown every symptom: its pnpm test step must pass punch-daemon.test.ts with no loop take failure.

📽️ Projector summary of b4363fa: https://field.amnes.ac/reviews/204

Two tests make the message thread fall behind a loop's wraps on
purpose. A region a tenth of a second long puts at least two wraps
between heartbeat ticks, and FIELD_AUDIOD_SERVICE_STALL_MS, a test seam
the daemon honours only with null output, holds the heartbeat between
taking over a finished take and finalizing it, so a wrap lands inside
the finalize. Against this commit's handoff the first times out after
the loop stops landing takes, and the second loses the daemon mid-loop.
)

The callback ends a loop's passes and takes over the standby on its
own clock, and the heartbeat took ownership after the fact by reading
a done flag and the standby slot. Both could disagree with what the
callback had done. A wrap that came before the heartbeat had serviced
the last pass found the flag still set and stopped the loop without
asking for a new start. A wrap during a finalize found no standby, so
the callback took the late one over a block after it opened, mid-pass,
and a heartbeat that ran before that block saw the slot still full,
left `capture` null, and dereferenced it at the next pass.

The done flag is now a count of the takes the callback has ended, and
the heartbeat finalizes that many in the order they were opened,
moving each next take up into `capture`, so ownership follows what the
callback did rather than a slot it can empty at any moment. The
callback takes a standby over only at a wrap, so a pass that began
with none ready is skipped whole and counted, as recording.md §15.4
decided. The heartbeat opens the next standby before it finalizes the
last pass. Stop discards a take still waiting for its wrap, finalizes
a standby a wrap began just before it, and detaches a second time in
case the block in flight took the standby over across the first.

@ninjudd ninjudd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

📽️ Projector review · 0.7.4 · model claude-opus-5-5 · effort xhigh · CHANGES REQUESTED · took 22m 38s

This change fixes #203, two races in the daemon's loop take where the heartbeat on the message thread falls behind the audio callback's wraps. A wrap before the heartbeat had serviced the last pass stopped the loop for good, and a wrap during a finalize left capture null, so the next pass crashed the daemon. The fix replaces the done flag with capturesEnded, a count of the takes the callback has ended, so the heartbeat hands ownership over by counting instead of reading the standby slot. The callback now takes a standby over only at a wrap, so a pass with none ready is skipped whole and counted, and the heartbeat opens the next standby before it finalizes. Stop discards a take still waiting for its wrap and finalizes a standby that a wrap began just before it. Two new tests in punch-daemon.test.ts reproduce both races, one with a region shorter than the heartbeat tick and one with FIELD_AUDIOD_SERVICE_STALL_MS, a null-output test seam that stalls the heartbeat inside each finalize.

1 finding thread: 0 resolved, 1 open

Covered 5 of 5 changed files; none generated.

The handoff itself holds up. I walked two and three wraps between ticks, a wrap between capturesEnded.exchange and the standby open, a wrap between the open's two stores, and a stop racing a wrap with the loop recording and with it skipping, and each take was finalized or discarded exactly once. The callback adds only atomic loads, stores, an exchange, and increments, so it still takes no lock and allocates nothing. The one finding is a session swap mid-loop, which the base already mishandled by stalling the loop. The new skip path turns that stall into a stream of one-block takes landing in the project.

The recording plan is completed, so it makes no readiness claim, and its §15.4 amendment matches the code. docs/audio-engine.md describes the skipped pass and the pending take as built.

Disclosures:

  • MIDI tests not run. record-midi-daemon.test.ts and record-tracks-daemon.test.ts arm MIDI tracks, which opens every MIDI source on the machine, and a live field keys session is running on this one, so I skipped both. CI skips most of their tests too, for want of a MIDI service. The change touches the MIDI paths only where recordMidiBlock and recordMidiCompanions count an ended take instead of raising the done flag, and I read those by hand.
  • Windows CI. The first attempt of run 38160071695 failed only in push.test.ts, "Test timed out in 5000ms" on "pushes, then offers every entry beneath main for HEAD", a test that never starts the daemon. Its punch-daemon.test.ts passed 8 of 8, and pull.test.ts, its neighbor, timed out in a hook on the windows job of #202's run 38160235808 in the same window. A rerun of the windows job, on the merge commit f136215 of this head and main at 9e0fc79, passed every test file, punch-daemon.test.ts 8 of 8 and push.test.ts 8 of 8, and linux passed on the first attempt.

Suggestions

  • src/audiod/protocol.ts:72: the pending comment still reads "a pre-rolled take waiting for its position". Add that it now also marks a loop's next take waiting for its wrap.

What I ran

  • Setup and gates. pnpm install --frozen-lockfile, then pnpm typecheck, which printed no errors, and project check, which printed "Project plans are valid." beside the existing site-workflow warning.
  • Builds. field-audiod at this head and at cd088df, the first commit, which adds only the stall seam and the tests to the base handoff, both against JUCE 8.0.4.
  • Head. punch-daemon.test.ts passed 8 of 8 in 11 runs in a row and in 4 runs at once. record-daemon.test.ts passed 10 of 10.
  • Base. Against cd088df, the two new tests failed in 3 of 3 runs, with "timed out waiting for 4 passes to land" and "field-audiod connection closed", and the other 6 passed. Both tests reproduce #203 deterministically.
  • Probes, not committed. A session sent mid-loop, and recordTake with loop against playLiveSession with watch, each at this head and at cd088df. The thread's numbers come from the second.

Comment thread native/audiod/src/player.cpp Resolved
A `session` swap installed a session without the take's region, so the
clock ran on past `until` and every later block ended a pass. With the
callback skipping between passes, each standby the heartbeat opened was
taken over for one block and ended at the next, landing a 512-frame
take on every tick. `field play --watch` beside a loop take sends such
a swap whenever a landed take changes the chosen one.

loadSession now carries the take's region onto the incoming session,
before the pre-roll so the chain warms on it, and again under the
message thread's lock, where it refuses a session the region does not
fit. With a region on the incoming session, the swap keeps the
position unwrapped, because the region wraps it, where the session's
own loop and end would wrap or clamp it at the length. Stop restores
the cached loop flag that converted delivery reads.

@ninjudd ninjudd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

📽️ Projector review · 0.7.4 · model claude-opus-5-5 · effort xhigh · CHANGES REQUESTED · took 16m 29s

This head answers the review of 786e25d. A session swap during a loop take now carries the take's region onto the incoming session, both before the pre-roll and again under the message thread's lock, where the daemon refuses a session that the region does not fit. With a region carried, the swap keeps the transport position unwrapped so the region wraps it. Stop restores the cached loop flag that converted delivery reads. A new test swaps the session mid-loop and checks that a session too short for the region is refused. The pending comment in protocol.ts now names the loop case, and a merge of main brings in #185, which touches none of this pull request's files.

2 finding threads: 1 resolved, 1 open

Covered 6 of 6 changed files; none generated.

The swap finding is fixed at this head, and its thread stays resolved. I reran both probes from the last review against a build of this head. A session sent mid-loop kept loop_from and loop_to and landed seven whole 24000-frame passes. field record --loop beside field play --watch landed no one-block take and counted no missed pass. A third probe edited the song mid-take so its content ended before the region. field play --watch printed watch audiod session: a loop take is recording, and its region does not fit this session (stop first): loop region ends past the session's content (36000 samples), the session already playing kept the region, and two more passes landed whole.

The defect behind the new thread predates this pull request. The probes turned up passes merged in pairs, and the base handoff merges them the same way. It is filed now because this change's docs promise whole passes and its skip path depends on the same wrap test.

Disclosures:

  • MIDI tests not run. record-midi-daemon.test.ts and record-tracks-daemon.test.ts arm MIDI tracks, which opens every MIDI source on the machine, and a live field keys session is running on this one, so I skipped both again. This head does not touch the MIDI paths.
  • Probes not committed. The swap, watch, and region probes ran from temporary test files, which I removed from the scratch worktree afterward.

Suggestions

  • native/audiod/src/player.cpp:1805: on a bed already rolling, record locates to at and then opens the transactions before it arms the latch, so blocks that pass meanwhile start the first take after at. Two of the watch probes saw it begin at 96512 for an at of 96000. This predates the change. Locating after the transactions open, just before the arming, would shrink that window to a few stores.

What I ran

  • Setup and gates. pnpm install --frozen-lockfile, then pnpm typecheck, which printed no errors, and project check, which printed "Project plans are valid." beside the existing site-workflow warning.
  • Builds. field-audiod at this head, and a scratch copy of this head with only here + n >= untilAt changed, both against JUCE 8.0.4. The earlier cd088df build served as the base.
  • This head. punch-daemon.test.ts, now 9 tests, passed 9 of 9 in 10 runs in a row and in 4 runs at once. record-daemon.test.ts passed 10 of 10.
  • Merged passes. Loops over [96000, 192000), [96000, 288000), and [96000, 144000) landed merged passes at this head and at cd088df. The patched copy landed whole passes for all three and passed punch-daemon.test.ts and record-daemon.test.ts, 19 of 19.
  • CI. Run 38163469751 passed on its first attempt: windows ran punch-daemon.test.ts 9 of 9 among 78 passed files, and linux passed 82 files.

Comment thread native/audiod/src/player.cpp Resolved
recordAudioBlock ended a pass only when `until` fell inside a block,
`here + n > untilAt`. A block that ended exactly on `until` pushed the
whole pass, and the block after it already began at `at`, where the
region had wrapped it, so no block ever saw that wrap and the take ran
on through the next pass. Blocks start on the grid the locate to `at`
sets, so a region a whole number of blocks long never ended a pass,
and a one-bar loop at 120 bpm merged every other pass. The test is now
`here + n >= untilAt`, which ends the pass with nothing left over for
the standby. A punch-out ends on the same sample as before, a block
sooner.

@ninjudd ninjudd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

📽️ Projector review · 0.7.7 · model claude-opus-5-5 · effort xhigh · CLEAN · took 0m 16s

This head answers the review of cb48e36. The audio callback now ends a loop pass in the block that reaches until, here + n >= untilAt. Before, a block that ended exactly on until hid the wrap and the take ran on through the next pass. A new test loops a region exactly 48 null-clock blocks long, where every pass ends on a block boundary, and requires each landed take to be one whole pass. The recording plan's §15.4 amendment records the change. A merge of main brings in #202, a plan that touches none of this pull request's files.

2 finding threads: 2 resolved, 0 open

Covered 6 of 6 changed files; none generated.

Disclosure: the project CLI moved from 0.7.4 to 0.7.7 while this review ran, and 0.7.7 does not read a 0.7.4 setup, so the review was set up again at this same head under 0.7.7 after the first start comment was deleted. The checks below ran against 85b6da2 before the re-setup, and the head has not moved since.

Both finding threads are fixed at this head, and the author had resolved each. The session swap carries the take's region, and passes that end on a block boundary now end there.

Suggestions

  • native/audiod/src/player.cpp:1807: carried over from the last review and still open. On a bed already rolling, record locates to at and then opens the transactions before it arms the latch, so blocks that pass meanwhile start the first take after at. This predates the change.

What was checked

  • The wrap fix. Against a build of this head, the three regions from the last review landed one whole pass per take, with only the stop's partial pass shorter. A one-bar loop at 120 bpm, [96000, 192000), landed four 96000-frame takes. A two-bar loop landed two 192000-frame takes. --at 2.1 --until 2.3 landed six 48000-frame takes. At cb48e36 the same loops landed 192000-, 432640-, and 96000-frame merged takes.
  • Punch-out unchanged. A punch-out over exactly 48 blocks, [24000, 48576), landed 24576 frames both at this head and at cb48e36. The take now ends in the block that reaches until instead of the block after it, on the same sample.
  • The new test. "a region a whole number of blocks long ends every pass on its until" failed against the cb48e36 build with "timed out waiting for 3 passes to land", because no pass ever ended, and passes at this head.
  • The swap probes. A session sent mid-loop kept the region and landed seven whole passes. field record --loop beside field play --watch landed eight takes, each 48000 frames but the stop's partial one, and counted no missed pass. An edit that ended the song before the region was refused, field play --watch printed its "does not fit this session (stop first)" message, and three more whole passes landed.
  • Suites. punch-daemon.test.ts, now 10 tests, passed 10 of 10 in 10 runs in a row and in 4 runs at once. record-daemon.test.ts passed 10 of 10. pnpm install --frozen-lockfile and pnpm typecheck passed, and project check printed "Project plans are valid."
  • CI. Run 38165813132 passed on its first attempt. Windows ran punch-daemon.test.ts 10 of 10 among 78 passed files, and linux passed 82 files.
  • Not run. record-midi-daemon.test.ts and record-tracks-daemon.test.ts, which arm MIDI tracks and so open every MIDI source, because a live field keys session is running on this machine. This head does not touch the MIDI paths. Reading recordMidiCompanions, a MIDI companion still ends a block after the audio when until falls on a block boundary, which is the order it expects.

…actions take to open

record located the bed to `at`, then opened the take's transactions, then armed the start latch, so the blocks a rolling bed played while the transactions opened carried it past `at` and the first take started late. record now opens the transactions first, then jumps to `at` and arms the latch in one hold of liveLock, which the callback only tries, so no block runs between them. locate keeps its refusal under a take and calls a new seek, which record uses. FIELD_AUDIOD_OPEN_STALL_MS, honoured only with null output like FIELD_AUDIOD_SERVICE_STALL_MS, stalls the open so punch-daemon.test.ts can make it outlast 200 ms of blocks; with the old order that test starts the take at 33728 instead of 24000.

@ninjudd ninjudd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

📽️ Projector review · 0.7.7 · model claude-opus-5-5 · effort xhigh · CHANGES REQUESTED · took 9m 59s

This head answers the suggestion left on 85b6da2. On a bed already rolling, record used to jump to at before it opened the take's transactions, so the blocks that played meanwhile started the first take after at. It now opens the transactions first, then jumps and arms the start inside one hold of liveLock, the lock the callback only tries, so no block runs between them. locate splits into seek, which record calls without the refusal a running take raises, and the cleanup after a refused jump or play moves into an abandon lambda. A new null-output seam, FIELD_AUDIOD_OPEN_STALL_MS, stalls the open, and a new test starts a take at at on a live bed and on a file bed that are both already playing. docs/audio-engine.md and the §15.4 amendment describe the new order. The merge base is unchanged at d94b734.

3 finding threads: 2 resolved, 1 open

Covered 6 of 6 changed files; none generated.

The late start is fixed. The new test failed against a build of 85b6da2 with only the stall seam added, "expected 33728 to be 24000", and passes at this head. In the watch probe below, the first take began at 96000 in three runs out of three. Holding liveLock for the jump adds only a few stores to the hold that the jump already took, and the callback still only tries that lock, so a contended block plays silence as before. The new finding is what the reorder does to a refused record.

Disclosures:

  • MIDI tests not run. record-midi-daemon.test.ts and record-tracks-daemon.test.ts arm MIDI tracks, which opens every MIDI source on the machine, and a live field keys session is running on this one, so I skipped both. The reorder also covers a MIDI take's transaction, which I read by hand.
  • Probes not committed. The refused-at, region, and swap probes ran from temporary test files, which I removed from the scratch worktree afterward.

What I ran

  • Setup and gates. pnpm install --frozen-lockfile, then pnpm typecheck, which printed no errors, and project check, which printed "Project plans are valid."
  • Builds. field-audiod at this head, and 85b6da2 with only FIELD_AUDIOD_OPEN_STALL_MS added, both against JUCE 8.0.4.
  • Suites. punch-daemon.test.ts, now 11 tests, passed 11 of 11 in 10 runs in a row and in 4 runs at once. record-daemon.test.ts passed 10 of 10.
  • Regions. A one-bar loop at 120 bpm landed 96000-frame takes, a two-bar loop 192000-frame takes, and --at 2.1 --until 2.3 48000-frame takes, one whole pass each but the stop's partial one.
  • Swaps. A session sent mid-loop kept the region and landed seven whole passes. field record --loop beside field play --watch landed eight 48000-frame takes but the stop's partial one, with no missed pass. An edit that ended the song before the region was refused with the "does not fit this session (stop first)" message, and three more whole passes landed.
  • Refused at. record at sample 4800000 against the 12-second live session and the same file bed failed with "locate out of range" at both heads. At this head each refusal left an incomplete transaction in recordings; at 85b6da2 neither did.
  • CI. Run 38172035023 passed on its first attempt. Windows ran punch-daemon.test.ts 11 of 11 among 80 passed files, and linux passed 86 files.

Comment thread native/audiod/src/player.cpp Resolved
@ninjudd
ninjudd marked this pull request as draft October 11, 2026 20:35
Opening the transactions before the jump to `at` meant a record refused for its `at`, or for a play that failed, reset takes that were already on disk, and recordings then offered each one back as an incomplete take. abandon now discards every transaction it holds, the primary's, the standby, and each companion's, before resetting it. punch-daemon.test.ts sends an `at` past the bed's end, on a live bed and on a file bed, and requires the refusal to leave no transaction behind; without the discard it leaves one per refusal.

@ninjudd ninjudd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

📽️ Projector review · 0.7.8 · model claude-opus-5-5 · effort xhigh · CHANGES REQUESTED · took 15m 41s

This head answers the thread on f381bde. abandon, the cleanup after a refused jump, a refused play, or a failed open in record, now calls discard() on the primary take, the standby, and each companion take before it drops them, so a refused record leaves nothing in .field/recording/. The late-start test now also asks for an at past the bed's end, live and from a file, and requires the list of recordings to stay the same length.

4 finding threads: 3 resolved, 1 open

Covered 6 of 6 changed files; none generated.

The refused-at thread is fixed. Against a build of this head, record at sample 4800000 was refused with "locate out of range" for the live session and for the file bed and left no transaction. A pre-rolled loop take with start at -1, which opens the standby as well before seek refuses it, left none either. At f381bde the same three refusals left four incomplete transactions. A refused play cannot be reached: record calls play only with a bed loaded, and play fails only with none.

The new thread is about the hold that f381bde added and this head keeps. One of 30 full runs of punch-daemon.test.ts here failed the late-start test with a take begun at 24099 for an at of 24000, and 30 runs of that test alone passed. A probe build that widens the gap between a block's position read and its arming check made it fail half the time, so the race is real, and the thread says how it misplaces a take.

Disclosures:

  • MIDI tests not run. record-midi-daemon.test.ts and record-tracks-daemon.test.ts arm MIDI tracks, which opens every MIDI source on the machine, and a live field keys session is running on this one, so I skipped both. abandon discards a MIDI take the same way, which I read by hand.
  • Probes not committed. The refused-at, region, swap, and widened-gap probes ran from temporary test files and scratch builds outside the worktree, which I left clean.

Suggestions

  • native/audiod/src/player.cpp:1904: a loop take refused at the jump still leaves the session looping the region record set before it opened anything; a probe saw loop_from stay at 24000 after the refusal. This predates the change, since cd088df does the same, and abandon could clear the region as stop does.

What I ran

  • Setup and gates. pnpm install --frozen-lockfile, then pnpm typecheck, which printed no errors, and project check, which printed "Project plans are valid."
  • Builds. field-audiod at this head, and a probe copy of it that sleeps 3 ms after pullAudio in null mode, both against JUCE 8.0.4.
  • Suites. punch-daemon.test.ts, 11 tests, passed in 29 of 30 full runs: 10 in a row, 4 at once, and 16 more two at a time. The one failure is the late-start test above. The late-start test alone passed 30 of 30. record-daemon.test.ts passed 10 of 10.
  • Regions. A one-bar loop at 120 bpm landed 96000-frame takes, a two-bar loop 192000-frame takes, and --at 2.1 --until 2.3 48000-frame takes, one whole pass each but the stop's partial one.
  • Swaps. A session sent mid-loop kept the region and landed seven whole passes. field record --loop beside field play --watch landed eight 48000-frame takes but the stop's partial one, with no missed pass and the first take at 96000. An edit that ended the song before the region was refused with the "does not fit this session (stop first)" message, and three more whole passes landed.
  • CI. Run 38173040488 passed on its first attempt. Windows ran punch-daemon.test.ts 11 of 11 among 80 passed files, and linux passed 86 files.

Comment thread native/audiod/src/player.cpp Resolved
…efused loop's region

record's jump to `at` and its latch shared one hold of liveLock, but a block reads its position inside that lock and checks the latch after it, and a file bed read its position and pulled outside the lock altogether, so a hold landing between the two latched the position from before the jump. pullAudio now records whether its pull saw captureStartRequested under the lock, the file bed's pull runs under it too, and both latches wait for a block that saw the request. A loop take refused at its jump clears the region record set, as stop does. FIELD_AUDIOD_LATCH_STALL_MS, a null-output seam like the others, waits between a block's pull and its latch, so punch-daemon.test.ts can land the jump there: without the guards a take latched 160896 instead of 24000, and without the clearing a refused loop left the session looping.

@ninjudd ninjudd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

📽️ Projector review · 0.7.8 · model claude-opus-5-5 · effort xhigh · CLEAN · took 11m 03s

This head answers the thread on ffa3fe5. pullAudio now records in blockSawRequest whether its pull saw captureStartRequested set under liveLock, and a file bed's position read and pull move inside that lock as the live branch's are. Both latches, audio and MIDI, wait for a block whose pull saw the request, so a block that read its position before record's jump can no longer start the take. A loop take refused at the jump now clears the region record set, as a stop does. A third null-output seam, FIELD_AUDIOD_LATCH_STALL_MS, waits between a block's pull and its latch, and the late-start test now uses it for four takes each on a live and a file bed rolling from three seconds in. A new test checks that a refused loop take leaves the session unlooped.

4 finding threads: 4 resolved, 0 open

Covered 6 of 6 changed files; none generated.

All four finding threads are fixed at this head, and the author had resolved each. Nothing new needs a thread.

What was checked

  • The latch race. The new tests fail against ffa3fe5 with a 3 ms gap built in: the late-start test with "expected 160896 to be 24000", a take latched at the bed's position before the jump, and the refused-loop test with the session still looping. At this head the late-start test passed 30 of 30 runs, three at a time, each run starting eight takes. A probe with an 8 ms latch stall started 20 takes on each bed from three seconds in, and all 40 began at 24000.
  • Real-time safety. The audio thread still only tries liveLock, and the code that moved inside it is the file bed's position read and pull, which already took the transport's own lock, plus one plain store to a flag only the audio thread reads. Nothing allocates. With a file bed, the message thread holds liveLock only to load, to make record's jump, and on a device start, so a file bed misses a block only then.
  • Refused takes. record at sample 4800000, live and from a file, and a pre-rolled loop take with start at -1 were all refused with "locate out of range" and left no transaction. The loop's refusal also left loop_from unset, where ffa3fe5 left the session looping.
  • Regions. A one-bar loop at 120 bpm landed 96000-frame takes, a two-bar loop 192000-frame takes, and --at 2.1 --until 2.3 48000-frame takes, one whole pass each but the stop's partial one.
  • Swaps. A session sent mid-loop kept the region and landed seven whole passes. field record --loop beside field play --watch landed eight 48000-frame takes but the stop's partial one, with no missed pass and the first at 96000. An edit that ended the song before the region was refused with the "does not fit this session (stop first)" message, and three more whole passes landed.
  • Suites. punch-daemon.test.ts, now 12 tests, passed 12 of 12 in 10 runs in a row and in 16 runs four at a time. record-daemon.test.ts passed 10 of 10. pnpm install --frozen-lockfile and pnpm typecheck passed, and project check printed "Project plans are valid."
  • CI. Run 38174398708 passed on its first attempt. Windows ran punch-daemon.test.ts 12 of 12 among 80 passed files, so the 4 ms stall holds up on its timer, and linux passed 86 files.
  • Not run. record-midi-daemon.test.ts and record-tracks-daemon.test.ts, which arm MIDI tracks and so open every MIDI source, because a live field keys session is running on this machine. I read the MIDI latch's new guard by hand: a block that did not see the request discards its arrivals, as a block before the start already did.

@ninjudd
ninjudd marked this pull request as ready for review October 11, 2026 21:11
@ninjudd
ninjudd merged commit 5cd6b9f into main Oct 11, 2026
7 checks passed
@ninjudd
ninjudd deleted the recording/loop-take-race branch October 11, 2026 21:13
ninjudd added a commit that referenced this pull request Oct 11, 2026
…s pull

#204 added a branch to recordMidiBlock for a block whose pull ran before
record's request: that block does not start the take, and it drained
its MIDI through a discard lambda. This change replaces that lambda with
chaseEach, so on main the stack no longer compiled ("use of undeclared
identifier 'discard'"). What the branch drains arrived before the take
starts, which the chase keeps, so it now drains through chaseEach too.

A daemon test sets #204's FIELD_AUDIOD_LATCH_STALL_MS seam to 8 ms,
which holds each block between its pull and its latch, and sends a
controller just before each of ten records on one arm. With a
discarding drain in that branch, a controller was missing from a take
in 4 of 4 runs; with chaseEach, every take had it in 6 of 6 runs.
ninjudd added a commit that referenced this pull request Oct 11, 2026
…ent, and the daemon chases it to the take's first sample (instrument-knobs.md chunk 3) (#212)

A MIDI take records only the messages that arrive once it has started.
So a knob you turned before pressing record, such as `juno`'s cutoff
dialed down for a darker pad, played live but rendered at its default in
the take until you happened to turn it again. The same gap hit the
modulation wheel, and keyboard-instrument.md §14.5 left it open. This
third chunk of the instrument-knobs plan (#202) closes it in the daemon,
with MIDI alone, so it works for any source.

There are three parts:

- **`field keys` publishes what it has sent.** Run inside a project, it
keeps every controller from 0 to 119 it has sent, by channel, in
`.field/keys.json`, written to a temporary file and renamed over the
last on each control change, and removed when it quits. A `field keys`
removes the file only if it wrote it, so two in one project do not
delete each other's. One that cannot write the file keeps playing and
names the error on the status line, and a take then starts without its
knobs.
- **`field record` passes them in `arm`.** When it arms a MIDI take, in
the one-track form or with `tracks`, and the `field keys` that wrote the
file is still running, it adds `controllers` to `arm`. It does so only
for a daemon that lists the new `midi-chase` feature, so an older daemon
never sees the field.
- **The daemon chases controllers.** From the arm, it keeps the latest
value of each controller from 0 to 119 on each channel: the ones `arm`
passed, then every control change that arrives, a count-in included,
whether before the take, during it, or past its `until`. Before this
change it dropped those that arrived before the start. At the take's
first sample it writes one line per channel and controller at `at: 0`,
ahead of every other line, in the order the values last arrived, in each
MIDI take of the pass. Controllers 120 to 127 are channel mode messages
and are never chased. A hardware knob moved after the arm counts too. A
change the take records enters the table only after those lines, so a
second take on one arm, which arming.md chunk 3's lasting arm will make,
starts each controller where the first take left it.

On the audio thread, the chase is a fixed table of 16 channels by 120
controllers, updated in constant time per message wherever the callback
used to discard arrivals before the start. `arm` hands its seeds to the
callback under a spin lock that the callback only ever tries, so it
never blocks, and each arm starts the table over. The lines go in just
before the take's first stamped message. So an arrival in the start
block that falls before the start sample is chased before the lines are
written, and the lines still lead the file. `arm` refuses `controllers`
out of range, as it refuses a bad input channel. A block whose pull ran
before `record`'s request, which #204 lets wait for the next block to
start the take, chases what it drains too, because that arrived before
the start.

#209 and #210, chunks 1 and 2, are on `main`, and this branch is rebased
onto it, #213's host monitoring included. `armSet` takes the chase's
seeds and then #213's connection `owner`, and the `tracks` form of `arm`
passes both. With this, every chunk of the plan is built, and what
remains is the criteria to check by hand. §11 of the plan records what
implementation settled.

## Testing

I ran these on macOS, from the root of a checkout of this branch, after
`pnpm install`, `pnpm build`, and `pnpm build:native`. `pnpm
build:native` matters here, since this change is in the daemon. Against
a daemon built before it, the `midi-chase` tests fail on
`client.supports('midi-chase')`.

1. `pnpm typecheck` prints nothing and exits 0.
2. `pnpm vitest run` reports 91 test files, 1216 tests passed and 1
skipped.
3. `pnpm vitest run src/commands/record-midi-daemon.test.ts
src/commands/record-plan.test.ts` passes 16 tests and skips 1, which
runs only where there is no MIDI service. The daemon tests publish a
CoreMIDI source of their own, `Field Keys Capture <pid> …`, for a few
seconds, and arm a daemon of their own in null-output mode; they touch
no running daemon. The chase test seeds `cc 110 = 10` and `cc 111 = 20`
on channel 16 at the arm. Before the start, it sends `cc 112 = 30` on
channel 15, `cc 110 = 40` on 16, and CC 120; after the start, `cc 111 =
50`. The take's lines at `at: 0` must be `cc 111 = 20`, `cc 112 = 30`,
`cc 110 = 40`, in that order, with no CC 120, and `cc 111 = 50` must
land later at its own time. Lines in another order mean the chase no
longer orders by last arrival. A `cc 110 = 10` line means a seed was not
replaced. The `field record` test publishes `.field/keys.json` as its
own process and checks that the landed notes file has `{ at: 0, cc: 113,
value: 23, channel: 16 }`. The latch test sets
`FIELD_AUDIOD_LATCH_STALL_MS=8` and sends a controller just before each
of ten `record`s on one arm. "the controller sent before take N:
expected [] to have a length of 1" means a block whose pull ran before
`record`'s request dropped what it drained instead of chasing it. The
carry test records twice on one arm, with `cc 114 = 20` before the first
take and `cc 114 = 50` during it, and the second take's line at `at: 0`
must be `cc 114 = 50`. `cc 114 = 20` there means a change the take
recorded never reached the chase.

What I could not run is criteria 20 and 21 by hand, which need a kitty
terminal and ears. From `examples/bloom`, give `creek` a track `keys`
with `instrument: juno`. In one terminal, run `node ../../dist/cli.js
keys juno` and turn `cutoff` down. In another, run `node
../../dist/cli.js record creek/track:keys --at 17`, play without
touching a knob, stop, build, and play. The take should render dark from
its first note, and its notes file's first `controls` row should be `{
at: 0, cc: 74, value: … }`. Both terminals need this branch's `dist` and
daemon: a daemon left running from another build answers without
`midi-chase`, so run `field transport quit` first. With `--count-in 1`,
turning `res` during the count-in should start the take with that `res`.

<!-- projector-summary v=1 sha=68d929f49bdbec524a494779a2cfc85fb8ff5fb9
-->
📽️ **Projector summary** of 68d929f: https://field.amnes.ac/reviews/212

This branch was previously deployed

1 inactive deployment
github-pages — b4363fae Deployed Oct 11, 2026 by ninjudd via publish #145
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.

Loop takes stop, lose their count, or crash the daemon when the message thread falls behind

1 participant