Thread cancel_event explicitly through the parameter chain
(sync_run closure → download_book() → streaming calls) instead of
stashing it on reporter objects as shared state. Remove the
cancel_event attribute from both PlainTextReporter and
ProgressReporter — cancellation is not a reporting concern.
Closes#39
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move entire download pipeline from cli.run() into standalone
sync_run() in new sync_run.py module. Introduce SyncRunResult
frozen dataclass as public return type with fatal_error field
for exit-code translation. Slim cli.py from 252→65 lines to a
thin argparse→exit adapter that only calls sync_run().
Migrate 35 pipeline tests from test_cli.py to test_sync_run.py
(43 total). CLI tests now only patch sync_run (no internal
collaborators). Add select_mode stub for ADR #6 branch point.
Update test_progress.py patch targets to sync_run module.
374 tests pass.
start_download(book) now returns a per-book bound callable
instead of raw task_id. Task identity is fully internal to
progress.py — no caller ever sees or passes task_id.
- Orchestrator: removed closure pattern, now threads
progress_cb = start_download(book) directly
- downloader: download_book() accepts optional progress= kwarg,
uses it when provided (falls back to reporter.update)
- cli.py: forwards progress kwarg through _download_fn
- Public update() drops task_id param; bound callable calls
_progress.update(task_id_internal) directly
382 tests pass (+11 new), 6/6 AC met.
Replace 9-parameter signature (book, client, output_base, history,
format_strategy, config, transport, progress, cancel_event) with
4 domain-aligned parameters (book, session, plan, reporter) that
return a frozen DownloadResult dataclass.
Key changes:
- Add DownloadResult(status, path, format, error) frozen dataclass
- Add format_strategy field to OutputPlan
- Move history writing from download_book() to caller (cli closure)
- Remove config param from download_accompanying_files()
- Wire cancel_event through reporter.cancel_event attribute
- Extract transport from session._client._transport
Closes#29
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Relocate API client from client.py to session.py with constructor
injection. Transport is now passed once at construction time
instead of per-method, eliminating 5 redundant httpx.Client
creations per session lifecycle.
- New session.py: LibroFmSession with shared _client, transport
injected at construction, auth headers set once after
authenticate()
- Old client.py: deprecated re-export shim (LibroFmClient alias)
- All endpoint signatures slimmed to ≤3 params (removed
transport= from authenticate/fetch_library/fetch_m4b_url/
fetch_download_manifest/fetch_pdf_extra_url)
- Updated all source imports (cli.py, downloader.py) and test
files (test_client, test_downloader, test_cli, test_progress)
- 17 new TDD tests in test_session.py
Closes#27
Move Book dataclass + from_library_row() into dedicated book.py.
Extract sanitize/resolve_path/needs_subdirectory into path.py.
Hard-cut import migration across 7 source+test files. 79 new tests.
Closes#26
In parallel download mode, all progress updates went to the same
(most recently started) bar because task_id was never threaded from
start_download() to the update callback. Create per-book closure in
orchestrator that binds task_id, pass through cli._download_fn as
optional kwarg. 3 new regression tests. 259 total.
Python ThreadPoolExecutor workers cannot be killed when blocked in HTTP
I/O. Previous approach of waiting for graceful cancellation via
shutdown(wait=True) caused infinite hangs, and re-raising KeyboardInterrupt
still triggered Python's _python_exit thread-join cleanup on exit.
Replace with hard-exit strategy: first Ctrl+C marks all unfinished books
(active + pending) as cancelled, prints summary, returns partial result
with interrupted=True. CLI then calls os._exit(130) to bypass thread
cleanup entirely. Second Ctrl+C hard-exits immediately.
Also fixes: books_with_index[idx][0] returning int index instead of
Book object (AttributeError on summary), empty CancelledError reason
strings, missing cancel_event wiring through download_book->download_m4b
call chain, and corrupted test body from earlier edit collision.
256 tests pass.
Replace singleton _current_task/_current_book state with bidirectional
_tasks (task_id→book) / _book_ids (id(book)→task_id) dicts, enabling N
simultaneous Rich progress bars for parallel downloads.
- start_download() now returns task_id (int)
- update() accepts optional task_id kwarg; falls back to most-recent
task when omitted for backward compat with existing callers
- complete()/fail() look up task by id(book) and clean up both mappings
- Safe on unknown books (no-op)
- PlainTextReporter unchanged — already stateless per-call design
10 new tests across TestMultiBarProgress (9) and TestMultiBookPlainText (1).
All 194 tests pass, zero regressions. Closes#14
Add parallel download worker-count configuration through all three
config layers: Config dataclass (workers=3 default), YAML parsing
with InvalidWorkersError validation, and -w/--workers CLI flag with
three-layer resolution (CLI > config > hardcoded 3). 6 new tests.
Closes#12
Summary output now lists each failed book (ISBN + title + reason)
and each skipped book (ISBN + title) in both plain-text and rich/TTY
formats. Ctrl+C produces a clean shutdown message with exit code 130
instead of a Python traceback. Three integration tests exercise the
full run() pipeline: happy path, mixed results, and fatal auth failure.
12 new tests (168 total), 15/15 acceptance criteria met.
Rich progress bars with live %/ETA/speed/file size for interactive
terminals; plain log lines for pipes/cron. Reads Content-Length from
HTTP response so percentage works from first chunk onward. Per-book
failure isolation prevents one bad book from stopping the batch.
18 new tests, 156 total passing.