Repository navigation
perf(ui): defer questionary/pygments imports off the CLI status path - #3321
Conversation
Problem: polylogue-8s70 found that polylogue status/agents status pay a command-body import tax that --help never reaches (help short-circuits before the callback runs). Prior work (PR #3166) cut status from ~2.05s to ~1.47s by localizing storage.repair/sources.dispatch imports, but a fresh python -X importtime pass on `polylogue status --format json` against an empty archive root (POLYLOGUE_DAEMON_URL pointed at a closed port to force the direct-fallback path) showed status regressed back to ~1.84-1.90s and traced a distinct, previously unnoticed 300-400ms branch: polylogue.ui -> polylogue.ui.facade -> questionary -> prompt_toolkit, plus a smaller pygments/rich.markdown/rich.syntax branch in facade_rendering.py. Both are imported unconditionally at module top level even though: - questionary is only called inside ConsoleFacade.confirm/choose/input's non-plain (interactive TTY) branches -- `env.ui.console.print(json...)` for --format json output never reaches them. - pygments/rich.markdown.Markdown/rich.syntax.Syntax are only used inside render_markdown/render_code/render_diff, not by plain status printing. What changed: moved both import sets from module top level into the specific methods/functions that use them, matching the repo's existing local-import convention (grep other cli/ modules for the same pattern). No behavior change -- confirmed via tests/unit/ui (which monkeypatches questionary attributes directly on the shared module object, still works since imports are idempotent). Verification: - `python -X importtime -m polylogue status --format json`: polylogue.ui cumulative import cost 408ms -> 92ms; questionary/prompt_toolkit/ pygments/rich.markdown/rich.syntax now show 0 occurrences on this command's trace (994 -> 716 modules imported total). - `time polylogue status --format json` (empty archive root, cold subprocess, POLYLOGUE_DAEMON_URL forced to a closed port, 3+ runs): ~1.84-1.90s -> ~1.52-1.54s. - `polylogue --help`: unaffected, ~0.45-0.46s; `devtools bench help-latency`: all 14 required targets still green. - `polylogue agents status --format json`: unaffected (~1.48-1.50s) -- that command's import trace never touched questionary/prompt_toolkit, confirming this is a targeted fix, not a blanket sweep. - ruff check/format --check clean; mypy --strict clean on both files. - devtools test tests/unit/ui: 115 passed. - devtools test tests/unit/cli/test_status.py tests/unit/cli/commands/test_status.py tests/unit/cli/test_status_diagnostics.py tests/unit/cli/test_embed_status_fast.py: 1 pre-existing failure (test_archive_facade_route_catalog_covers_public_async_facade), confirmed identical on baseline via git stash -- unrelated async facade route-catalog coverage gap, not touched by this change. AC honesty: the bead's original AC (<700ms cold-CLI budget) is still not met. Remaining ~1.1s is dominated by the readiness/capability -> storage.repair and insights.archive/archive_rollups chains, which prior investigation (recorded on this bead) already found to be a genuine, non-trivially-fixable pydantic-model-construction cost rather than an eager-import artifact -- unchanged by this PR. This PR closes a previously undiscovered, cleanly avoidable slice (the interactive UI stack) found via a fresh importtime pass. Ref polylogue-8s70 Co-Authored-By: Claude <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
Next review available in: 42 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes a real, measured slice of
polylogue-8s70's command-body import tax.Problem
polylogue status/agents statuspay an import tax thatpolylogue --helpnever reaches (--helpshort-circuits before the command callback runs). Prior work (PR #3166) cutstatusfrom ~2.05s to ~1.47s by localizingstorage.repair/sources.dispatchimports, but a freshpython -X importtimepass onpolylogue status --format jsonagainst an empty archive root showed it had regressed back to ~1.84-1.90s, tracing a distinct, previously unnoticed 300-400ms branch:polylogue.ui→polylogue.ui.facade→questionary→prompt_toolkit, plus a smallerpygments/rich.markdown/rich.syntaxbranch infacade_rendering.py.Both are imported unconditionally at module top level even though:
questionaryis only called insideConsoleFacade.confirm/choose/input's non-plain (interactive TTY) branches —--format jsonoutput never reaches them.pygments/rich.markdown.Markdown/rich.syntax.Syntaxare only used insiderender_markdown/render_code/render_diff, not by plain status printing.Solution
Moved both import sets from module top level into the specific methods/functions that use them, matching this repo's existing local-import convention. No behavior change — confirmed via
tests/unit/ui(which monkeypatchesquestionaryattributes directly on the shared module object; imports are idempotent).Verification
python -X importtime -m polylogue status --format json:polylogue.uicumulative import cost 408ms → 92ms; questionary/prompt_toolkit/pygments/rich.markdown/rich.syntax now show 0 occurrences on this command's trace (994 → 716 modules imported total).time polylogue status --format json(empty archive root, cold subprocess,POLYLOGUE_DAEMON_URLforced to a closed port, 3+ runs): ~1.84-1.90s → ~1.52-1.54s.polylogue --help: unaffected, ~0.45-0.46s;devtools bench help-latency: all 14 required targets still green.polylogue agents status --format json: unaffected (~1.48-1.50s) — that command's import trace never touched questionary/prompt_toolkit, confirming this is a targeted fix, not a blanket sweep.ruff check/format --checkclean;mypy --strictclean on both files.devtools test tests/unit/ui: 115 passed.devtools test tests/unit/cli/test_status.py tests/unit/cli/commands/test_status.py tests/unit/cli/test_status_diagnostics.py tests/unit/cli/test_embed_status_fast.py: 1 pre-existing failure (test_archive_facade_route_catalog_covers_public_async_facade), independently re-confirmed identical onorigin/masterbefore merging this PR — unrelated async facade route-catalog coverage gap, not touched by this change.AC honesty
The bead's original AC (<700ms cold-CLI budget) is still not met. Remaining ~1.1s is dominated by the readiness/capability →
storage.repairandinsights.archive/archive_rollupschains, which prior investigation (recorded on the bead) already found to be a genuine, non-trivially-fixable pydantic-model-construction cost rather than an eager-import artifact — unchanged by this PR. This PR closes a previously undiscovered, cleanly avoidable slice (the interactive UI stack) found via a fresh importtime pass.Ref polylogue-8s70