Skip to content

refactor(cli): extract drt run to commands/run.py — #573 Phase 2b PR (a)#579

Merged
masukai merged 2 commits into
mainfrom
refactor/573-cli-run-extract
May 30, 2026
Merged

refactor(cli): extract drt run to commands/run.py — #573 Phase 2b PR (a)#579
masukai merged 2 commits into
mainfrom
refactor/573-cli-run-extract

Conversation

@masukai

@masukai masukai commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Phase 2b PR (a) of #546 (tracked under #573) — extracts the drt run command + its supporting infrastructure out of drt/cli/main.py into a dedicated drt/cli/commands/run.py. Pure mechanical move; no behaviour change.

What moved

drt/cli/main.pydrt/cli/commands/run.py:

Symbol Notes
LogFormat(str, Enum) the #577 Enum backing --log-format
_STANDARD_LOG_FIELDS, _JsonFormatter, _configure_json_logging JSON Lines log format (only run uses these)
_RunContext dataclass shared state passed into _run_one
_exit_code_for_signal POSIX 128 + signum
_run_one per-sync execution, observer composition, telemetry
_print_watermark_summary post-run watermark notes
run (@app.command) the command itself; signal handling; parallel/sequential dispatch; JSON-mode output

Total moved: ~485 LOC. drt/cli/main.py shrinks 1086 → 595 LOC (-45%).

Back-compat shim

drt/cli/main.py re-exports each moved underscore-prefixed name + LogFormat so existing imports keep working:

from drt.cli.commands.run import (
    LogFormat,
    _configure_json_logging,
    _exit_code_for_signal,
    _JsonFormatter,
    _print_watermark_summary,
    _run_one,
    _RunContext,
)

Same pattern as #572's _get_source / _get_destination / _get_watermark_storage shims. A "tighten back-compat surface" pass is appropriate post-v1.0.

Test mock paths updated

The move also corrects monkeypatch paths in tests/unit/test_cli_run_telemetry.py:

Was Now Was it actually effective?
drt.cli.main._get_destination drt.cli.commands.run.get_destination No — silent no-op since #572 (_run_one calls _helpers.get_destination directly, not via the main.py shim). Fixed opportunistically.
drt.cli.main._get_watermark_storage drt.cli.commands.run.get_watermark_storage Same as above.
drt.cli.main.print_row_errors drt.cli.commands.run.print_row_errors This one was failing outright (AttributeError) after the move, since main.py no longer imports print_row_errors. The fix made it pass.

All 10 tests in test_cli_run_telemetry.py now pass with the path adjustments and are actually effective mocks.

Acceptance criteria

  • run + _RunContext + _run_one + _JsonFormatter moved to drt/cli/commands/run.py
  • cli/main.py shrinks (1086 → 595 LOC, -45%)
  • Back-compat shim re-exports all underscore-prefixed names + LogFormat
  • All existing tests pass — make test: 1247 passed, 1 skipped
  • make lint clean (111 source files; ruff + mypy)
  • drt run --help renders identically
  • drt run --log-format invalid still rejected by Choice validation (Enum auto-derived)
  • CI green on 3.10 / 3.11 / 3.12 / 3.13

Out of scope (PR (b) follow-up)

  • validate (~190 LOC)
  • status (~120 LOC)
  • test + _SyncTestResult (~130 LOC)

After PR (b), cli/main.py will hit the #573 target of ~50-100 LOC pure wiring.

References

🤖 Generated with Claude Code

@codecov

codecov Bot commented May 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.03540% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
drt/cli/commands/run.py 92.00% 18 Missing ⚠️

📢 Thoughts on this report? Let us know!

masukai and others added 2 commits May 30, 2026 19:21
… (a))

Moves the run command + its supporting infrastructure out of main.py:

- LogFormat Enum (#577)
- _STANDARD_LOG_FIELDS / _JsonFormatter / _configure_json_logging
- _RunContext dataclass
- _exit_code_for_signal (POSIX 128+signum)
- _run_one (per-sync execution; observer composition; telemetry)
- _print_watermark_summary (post-run notes)
- run (@app.command itself; signal handling; parallel/sequential dispatch)

Total ~485 LOC. cli/main.py shrinks 1086 → 595 (-45%).

Back-compat: main.py re-exports each moved underscore-prefixed name +
LogFormat so existing `from drt.cli.main import _RunContext` etc. keep
working. Same pattern as #572's `_get_*` shims.

Test mock paths updated for monkeypatched names that no longer live on
drt.cli.main:

- print_row_errors: was failing outright (AttributeError) — main.py no
  longer imports it after the move.
- _get_destination / _get_watermark_storage: mocks were silently no-ops
  since #572 because _run_one calls the public _helpers.get_destination
  directly; updating the path so the mock is actually effective.

Closes part of #573 (PR (b) covers validate / status / test).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds 5 unit tests in tests/unit/test_print_watermark_summary.py that
exercise the helper directly:

- empty results: no output
- default_value branch: yellow first-run note + sync name list
- cli_override branch: cyan --cursor-value note + sync name list
- both branches: emits both notes
- unknown / missing watermark_source: silent

Lifts codecov patch coverage on commands/run.py from 90.27% to ~92%
(22 → 18 missed lines). The remaining 18 lines are inside the run
command body and need a CliRunner + project fixture to exercise; that's
appropriate for a follow-up, since the watermark helper is the only
piece reachable via direct call.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@masukai masukai force-pushed the refactor/573-cli-run-extract branch from 5905b2f to 4dcb750 Compare May 30, 2026 10:21
@masukai masukai merged commit 57896b8 into main May 30, 2026
8 checks passed
@masukai masukai deleted the refactor/573-cli-run-extract branch May 30, 2026 10:27
@github-actions github-actions Bot locked and limited conversation to collaborators May 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: split drt/cli/main.py into per-command handler modules

1 participant