Skip to content

refactor(incremental): concentrate incremental orchestration in the shared module - #55

Merged
ElbertePlinio merged 4 commits into
mainfrom
refactor/shared-incremental-orchestration
Jul 19, 2026
Merged

ElbertePlinio merged 4 commits into
mainfrom
refactor/shared-incremental-orchestration

Conversation

@ElbertePlinio

Copy link
Copy Markdown
Member

CAND-1 (#47) — PR 1: Incremental orchestration

Concentrates CLI and desktop incremental scheduling/fallback/finalization
policy into src/engine/incremental.rs. Closes out the "PR 1 — Incremental
orchestration" item from #47's dependency-ordered plan; #47 completes when
this merges (the other CAND items are separate PRs).

What changed

  1. src/engine/incremental.rs (b0e8c13, prior commit on this branch) grew
    from a tests-only IncrementalRunner nobody constructed in production
    into the real deep module: SchedulingConfig (target/max/overlap/backlog
    clamps), the pure decision functions (next_step, choose_segment_end,
    after_boundary, classify_slice), and the IncrementalHost trait +
    run() driver that owns the scheduling loop, progress state, and the
    fallback/completion decision.
  2. src/bin/pickscribe.rs's run_incremental_worker_loop now drives
    through incremental::run via CliIncrementalHost, an adapter over the
    CLI's file-based control signals (stop/stopping/cancel paths, parent
    recorder pid liveness) and subprocess STT execution.
  3. src-tauri/src/engine.rs's run_incremental_worker now drives through
    the same incremental::run via TauriIncrementalHost, an adapter over
    the desktop worker's CancelToken/session-current signals and in-process
    STT execution.
  4. Deleted the now-dead duplicate helpers from both adapters
    (growing_audio_duration_ms, choose_segment_end × 2) now that the
    module owns the equivalent logic (audio_segments::duration_ms,
    incremental::choose_segment_end).

Preserved exactly

  • CLI: process files (stop/stopping/cancel/worker-output.json),
    exit codes, and orphan/parent-death handling. CliIncrementalHost::control()
    reproduces the same precedence the inline loop had: cancel > stop >
    stopping-grace > orphan-with-startup-deadline.
  • Desktop: Tauri events (publish() still calls emit_incremental_session
    at the same points), stage transitions (run_pipeline/incremental_result
    untouched), the IncrementalResult::Partial salvage path from fix: unblock the UI thread and stop tray subprocess churn #49
    (segments::salvage_completed_prefix untouched), and keep_audio
    retention. Desktop's extra TOCTOU guard around complete (re-checking
    cancellation/supersession after the module returns, since a cancel can
    race in during the last segment's STT call) is kept in the
    run_incremental_worker wrapper rather than folded into the shared
    module, since it's specific to how desktop's session-current check works.

Where CLI and desktop diverged, and why the CLI's behavior won

Before this refactor, live backlog overflow (buffered-but-unsegmented audio
exceeding the backlog limit before a final drain was requested) was
handled differently by each adapter:

  • CLI stopped scheduling immediately with a fallback outcome.
  • Desktop set fallback_required and kept scheduling more segments
    until the final drain or cancellation — segments a fallback would end up
    discarding anyway.

next_step() in the shared module now always stops immediately (the CLI's
prior behavior), for two reasons: it stops burning CPU/STT time on segments
a fallback will discard, and it maximizes the contiguous finished prefix
available to segments::salvage_completed_prefix (#49) by not letting more
segments race the fallback decision. This is a real, intentional behavior
change for desktop — see next_step()'s doc comment for the full
rationale, and run_stops_immediately_on_live_backlog_overflow_without_transcribing_more
in incremental.rs's test suite for the characterization.

Final backlog overflow (after a final drain was requested) already stopped
both loops immediately; that policy is unchanged, just centralized.

Validation

  • cargo test --workspace --locked --all-targets: 100 lib + 10 CLI-bin +
    7 pickscribe-cleanup-bin + 29 src-tauri-lib tests, all green, run after
    each of the three routing/cleanup commits.
  • bun run build (Vite/Tauri frontend build) and bun run test
    (install-script smoke + vitest) both green.
  • Manual CLI smoke: pickscribe start --incremental / stop against real
    pw-record + whisper-cli — segments scheduled at the configured
    target/max boundaries, worker-output.json progressed through
    transcribing → rawReady, final output reported
    complete: true, fallback_required: false, and keep_audio retained
    segment WAVs. pickscribe start --incremental / cancel mid-recording
    left no residual worker/temp-dir artifacts.
  • No new warnings: both adapters compile with zero dead-code warnings now
    that the duplicate helpers are gone.

Refs #47.

…al scheduling policy module

CAND-1 (#47) step 1/3: build the deep module both worker loops will
route through. src/engine/incremental.rs now owns:

- SchedulingConfig: target/max/overlap/backlog clamps (target >= 1000ms,
  overlap <= half target, backlog = max_ms * max_queue), centralized from
  two duplicated copies.
- next_step/choose_segment_end/after_boundary/classify_slice: the pure
  segment-cut decisions (250ms residual tail, stop-before-first-segment,
  empty/failed slice handling) previously duplicated verbatim in
  src/bin/pickscribe.rs and src-tauri/src/engine.rs.
- IncrementalHost + run(): the driver loop, generic over an adapter trait
  for STT execution, live segment cleanup, and progress publication. run()
  owns progress state (the RecordingSession and when to publish it), the
  fallback/completion decision, and the final drain.
- Unifies the one live policy divergence between the two production
  loops: live backlog overflow now always stops scheduling immediately
  (the CLI's existing behavior) instead of desktop's prior behavior of
  flagging fallback_required and continuing to schedule segments a
  fallback will discard. Documented on next_step().

CancelToken and cleanup_session_dir (used outside incremental scheduling)
are unchanged. IncrementalRunner/SegmentTranscriber/EnqueueResult/
IncrementalSession are removed: grep confirms nothing outside this
module's own tests ever constructed them, so they characterized an
orchestration path production never ran.

Neither production loop routes through this module yet (next commits).
…emental

CAND-1 (#47) step 2/3: src/bin/pickscribe.rs's run_incremental_worker_loop
now drives segment scheduling through engine::incremental::run instead of
its own copy of the state machine. CliIncrementalHost adapts the module's
IncrementalHost trait to the CLI's file-based control signals (stop/
stopping/cancel paths, parent recorder pid liveness) and subprocess STT
execution (transcribe_incremental_segment, still polling the same
is-cancelled condition via Control::Abandoned | Control::Cancelled).

Process files, exit codes, worker-output.json shape, and orphan/parent-
death handling are unchanged: CliIncrementalHost::control() reproduces the
exact precedence (cancel > stop > stopping-grace > orphan-with-startup-
deadline) that run_incremental_worker_loop had inline. The one intentional
behavior shift is upstream: live backlog overflow now stops immediately
rather than flagging-and-continuing, which was already the CLI's own
behavior pre-refactor (see b0e8c13) and only changes desktop's loop
(next commit).

queue_segment_cleanup/drain_segment_cleanup_results are folded into
CliIncrementalHost::try_queue_cleanup/drain_cleanup, which is what the
module's run() calls instead. growing_audio_duration_ms and the CLI's
choose_segment_end are now dead code (equivalent functions live in the
shared module); left in place for the step 4 cleanup commit alongside
desktop's duplicates so each adapter's dedup lands together.

Next: route the Tauri worker loop in src-tauri/src/engine.rs through the
same module.
…cremental

CAND-1 (#47) step 3/3: src-tauri/src/engine.rs's run_incremental_worker now
drives segment scheduling through engine::incremental::run instead of its
own copy of the state machine. TauriIncrementalHost adapts the module's
IncrementalHost trait to the desktop worker's in-process signals
(worker_cancel_token, the recording session's CancelToken via
is_session_current, stop_requested) and in-process STT execution
(stt::transcribe_with_cancel).

Preserved exactly:
- Tauri events: publish() still calls emit_incremental_session, so segment
  status transitions (Transcribing/RawReady/Cleaning/Failed) reach the
  frontend at the same points.
- Stage transitions: unchanged, driven by run_pipeline/incremental_result
  which don't change here.
- The IncrementalResult::Partial salvage path (PR #49): incremental_result
  and segments::salvage_completed_prefix are untouched; run_incremental_worker
  still returns an IncrementalDone with the same session/complete/
  fallback_required shape.
- keep_audio retention: host.keep_audio() and cleanup_session_dir calls use
  the same worker.cfg.general.keep_audio.
- The extra TOCTOU guard desktop had around `complete` (re-checking
  worker_cancel_token/is_session_current after the module returns, since a
  cancel/supersession can race in during the last segment's STT call) is
  kept in the run_incremental_worker wrapper rather than the shared module.

Behavior change (intentional, per b0e8c13): live backlog overflow now stops
scheduling immediately instead of flagging fallback_required and continuing
to schedule segments a fallback would discard. This was already the CLI's
behavior; desktop's loop now matches it. See engine::incremental::next_step
docs for the full rationale.

choose_segment_end here is now dead code (the module owns the equivalent
logic); left in place for the step 4 cleanup commit.

Next: delete the now-duplicate loop internals/helpers from both adapters.
…adapters

CAND-1 (#47) step 4/4: growing_audio_duration_ms and choose_segment_end in
src/bin/pickscribe.rs, and choose_segment_end in src-tauri/src/engine.rs,
were left as dead code by the previous two commits once each worker loop
routed through engine::incremental (audio_segments::duration_ms and
incremental::choose_segment_end are the equivalents the module now owns).
Removed, along with the now-unused audio_segments import in the CLI binary
(src-tauri/src/engine.rs still uses audio_segments::slice_wav directly for
its fallback-tail slicing, so its import stays).

Both worker loops now contain zero scheduling policy of their own; all of
it lives in src/engine/incremental.rs, exercised by that module's own test
suite plus each adapter's existing integration coverage.
@ElbertePlinio
ElbertePlinio merged commit 379a9d1 into main Jul 19, 2026
1 check passed
@ElbertePlinio
ElbertePlinio deleted the refactor/shared-incremental-orchestration branch July 19, 2026 23:48
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