Skip to content

feat(jobs): add single-worker jobs manager with cancellation and atomic writes - #13

Merged
prateekchaplot merged 2 commits into
mainfrom
arch/tts/t1.6
Nov 20, 2026
Merged

prateekchaplot merged 2 commits into
mainfrom
arch/tts/t1.6

Conversation

@prateekchaplot

Copy link
Copy Markdown
Owner

Task T1.6 from docs/ARCHITECTURE.md section 14.

What was built

Implements the jobs component of ARCHITECTURE.md section 7.8:

  • src/tts/jobs/manager.py — JobManager(dal, settings) with submit, cancel, subscribe and the module-level bootstrap(settings, dal). A ThreadPoolExecutor(max_workers=1) runs one generation at a time, so two submissions never synthesize concurrently (ADR-8).
  • src/tts/jobs/sse.py — in-memory subscriber registry, the retry: 3000 line and the event:/data: wire format of section 9.2. A new subscriber is replayed the current state, so a reconnect is stateless-safe.
  • src/tts/jobs/__init__.py — package marker (a package needs one; the timing and subtitles packages ship the same).

Key behaviours

  • Atomic output. PCM frames are streamed per sentence into {id}.wav.part; the file is closed and os.replaced to {id}.wav only after the timings and cues are derived, and the row is set succeeded only after that rename (section 1, section 10.1, NFR-03). A failure or a cancellation removes the .part.
  • Cancellation. threading.Event checked after every sentence. A job that never started is cancelled outright; a running job reports cancelling until the worker confirms it.
  • Honest failure. Any worker exception removes the partial audio, sets failed with an ENGINE_ERROR/INTERNAL_ERROR code, and publishes a failed event. A generation that renders no audio at all fails instead of succeeding with an unplayable empty WAV.
  • TTS_FAULT=engine injects a mid-run engine failure for the AC11/AC12 end-to-end specs (section 11, section 15). A cancel requested first wins over the injected fault.
  • Downstream contracts honoured: per-sentence first_word_idx triplets for build_cues (T1.3's contract), evict_to_limit called with every in-flight id (T1.5's contract).

Decisions

  • Sentence segmentation lives in jobs/manager.py (split_sentences): section 7.4 forbids the engine from splitting text and no other component owns a splitter. A terminator ends a sentence only when followed by whitespace or end-of-text; decimals and abbreviations/initials do not split.
  • bootstrap(settings, dal) is implemented here because section 7.8 lists it in the JobManager interface; T2.3 owns the wiring and the lazy settings seeding.
  • TTS_FAULT is read in jobs/manager.py rather than config.py: section 14.2 assigns the implementation to T1.6 while config.py belongs to T0.2/T4.3, so the file-ownership guardrail wins over section 7.1. T4.3 owns the follow-up that folds it into Settings.
  • cancel() returns cancelling for a job that was already cancelling. Section 9.2 literally reads "already cancelling" as a 200 with status: cancelled; jobs reports the truthful state and T2.2 owns the HTTP mapping.

The full list of decisions and downstream contracts is recorded in docs/TASKS.md (Notes).

Verification

  • ruff check src/tts tests — clean
  • ruff format --check src/tts tests — clean
  • mypy src/tts — clean (strict, 25 source files)
  • pytest tests/unit — 140 passed (37 new)
  • pytest tests/integration — 13 passed (7 new) against the real en_US-lessac-medium voice
  • git diff --check — clean

Acceptance criteria (section 14.2, T1.6)

Criterion Covered by
submit, subscribe, progress at least 1, succeeded integration: test_real_voice_job_reports_progress_and_succeeds
engine failure mid-run: no .wav, status failed, .part removed integration: test_injected_engine_fault_leaves_no_audio; unit: test_engine_failure_leaves_no_audio_and_marks_failed
cancel mid-run: status cancelled, no file integration: test_cancel_mid_run_leaves_no_audio; unit: test_cancel_mid_run_cancels_the_job_and_removes_the_output
two submits run strictly sequentially integration: test_two_jobs_run_strictly_sequentially, test_a_second_job_does_not_start_before_the_first_finishes; unit: test_two_jobs_run_strictly_sequentially

Manual checks

  • The rendered WAV was inspected with the wave module: mono, 16-bit, 22050 Hz, non-zero frame count.
  • The .part file is absent after both success and failure; the final .wav exists only after success.
  • A real-voice run produces one cue per sentence for a two-sentence text, confirming the per-sentence first_word_idx contract.

Environment note

The integration suite renders with the downloaded voice but keeps its database and audio in a tmp_path. On this Windows clone pytest needs --basetemp .venv/ptmp-* because the default system temp directory is inaccessible (pre-existing, see the T0.2 note). prettier --check fails locally only because core.autocrlf=true gives the working tree CRLF endings; the committed blobs are LF and CI's lint job is green.

Suggested merge method: squash merge with branch deletion.

…ic writes

Implements ARCHITECTURE.md section 7.8: one generation at a time on a
single-worker executor, atomic .part -> .wav rename before the row is
marked succeeded, cancel events checked at every sentence boundary, and
an in-memory SSE subscriber registry that replays the current state on
connect.

Also adds TTS_FAULT=engine mid-run failure injection for the AC11/AC12
end-to-end specs, and records the T1.6 decisions and downstream contracts
for T2.2 and T4.3 in docs/TASKS.md.
@prateekchaplot
prateekchaplot merged commit 3c30df7 into main Nov 20, 2026
5 checks passed
@prateekchaplot
prateekchaplot deleted the arch/tts/t1.6 branch November 20, 2026 17:24
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