Skip to content

perf(ui): defer questionary/pygments imports off the CLI status path - #3321

Merged
Sinity merged 1 commit into
masterfrom
feature/perf/lazy-import-ui-questionary
Jul 27, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/perf/lazy-import-ui-questionary

Conversation

@Sinity

@Sinity Sinity commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes a real, measured slice of polylogue-8s70's command-body import tax.

Problem

polylogue status/agents status pay an import tax that polylogue --help never reaches (--help short-circuits before the command 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 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 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 — --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.

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 monkeypatches questionary attributes directly on the shared module object; 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), independently re-confirmed identical on origin/master before 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.repair and insights.archive/archive_rollups chains, 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

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]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 42 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 93cda1f9-c701-4452-809a-83373b121da4

📥 Commits

Reviewing files that changed from the base of the PR and between cd56bcc and ac7ebd1.

📒 Files selected for processing (2)
  • polylogue/ui/facade.py
  • polylogue/ui/facade_rendering.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/perf/lazy-import-ui-questionary

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sinity
Sinity merged commit 8d576c5 into master Jul 27, 2026
3 checks passed
@Sinity
Sinity deleted the feature/perf/lazy-import-ui-questionary branch July 27, 2026 12:24
Sinity added a commit that referenced this pull request Jul 27, 2026
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