Skip to content

perf: load the HTTP stack only when a role command runs - #53

Merged
rabesss merged 2 commits into
mainfrom
perf/lazy-role-imports
Oct 2, 2026
Merged

rabesss merged 2 commits into
mainfrom
perf/lazy-role-imports

Conversation

@rabesss

@rabesss rabesss commented Oct 2, 2026

Copy link
Copy Markdown
Owner

What changed

lighthouse instructor --help and lighthouse student --help imported the whole HTTP stack (lighthouse_cli.api, requests, config), because assessment_commands.py imported api and assessment_api at the top of the file. Those imports now happen inside the functions that use them (_run, _create, quiz-create, assignment-create). course_read_commands.py only needed AssessmentAPI for type hints, so it now imports it under TYPE_CHECKING.

This replaces #29. That PR took the same idea further, with a 316-line lazy-export layer (__getattr__/globals()) over the legacy commands module, but that module already loads lazily from cli.py, so the role groups were the only slow path left.

Measured

Median wall time of a fresh process, 20 runs after 3 warm-ups. The machine was under load, so the numbers are noisy but consistent across rounds.

command main this branch
lighthouse --help 68–83 ms 70–74 ms
lighthouse instructor --help 169–174 ms 75–84 ms
lighthouse student --help 161–165 ms 69–72 ms

Tests

  • New test_role_help_does_not_load_http[instructor|student] runs role help in a subprocess and asserts that neither requests nor lighthouse_cli.api was imported. Putting the top-level api import back makes both cases fail.
  • The 8 tests that patched lighthouse_cli.assessment_commands.LighthouseClient now patch lighthouse_cli.api.LighthouseClient, which the function-level import reads at call time.
  • Full gate: ruff, mypy, lint-imports, deptry, xenon, the secret check, and pytest (1599 passed).

Closes #27

instructor/student --help imported lighthouse_cli.api (requests, config)
through assessment_commands. Import the API and assessment helpers inside
the functions that use them instead; role help now loads like root help.

Replaces the lazy-export layer in #29.
@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, add credits to your account and enable them for code reviews in your settings.

@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because your trial has ended. Ask your workspace admin to add credits to resume reviews. Manage billing

@factory-droid

factory-droid Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Droid finished @rabesss's task —— View job


Security Review

The lazy-import refactor matches the stated perf goal and the new subprocess test usefully guards against unintended HTTP stack imports on role help. I only see a small robustness issue in the new test around verifying the Click exit code.

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

[Medium risk] Defers HTTP client imports to reduce startup time.

The PR appears safe to merge; the help-test exit-status check is a non-blocking improvement.

Summary

The PR moves assessment HTTP imports from role-group loading to command execution and updates the associated mocks and help test.

  • Role help no longer loads requests or the API module through the assessment command imports.
  • The new help test should preserve the help command’s exit status.

Reviews (1) · Last reviewed commit: "perf: load the HTTP stack only when a ro..."

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — verified the lazy-import mechanism end-to-end: read all changed files plus the _AssessmentGroup wiring in cli.py, grepped for stale references to the moved symbols, ran ruff + lint-imports + 188 tests locally, mutation-tested the new test (restoring the top-level api import makes both cases fail), and confirmed CI green on the head SHA.

  • Deferred the HTTP-stack imports in assessment_commands.py to call time — _run, the _create dry-run branch, quiz-create, and assignment-create now import LighthouseClient / assessment_api helpers inside the function, so resolving role groups no longer pulls requests; every lazy import is used at its site.
  • Moved the annotation-only AssessmentAPI import under TYPE_CHECKING in both assessment_commands.py and course_read_commands.py, guarded by from __future__ import annotations (same pattern as ms_auth.py).
  • Retargeted 8 mock patches to lighthouse_cli.api.LighthouseClient — correct, since _run now resolves the attribute from lighthouse_cli.api at call time — and added a subprocess test asserting role --help imports neither requests nor lighthouse_cli.api.

Pullfrog  | View workflow run | Using glm-5.3 | 𝕏

Comment thread tests/test_assessment_workflows.py Resolved
@factory-droid

factory-droid Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Droid finished @rabesss's task —— View job


Security Review

No high-confidence, actionable issues found in the lazy-import refactor or its test updates. No additional inline comments to add for this head SHA.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — re-reviewed the delta since the prior pullfrog review (9bfb331): one test-only commit, re-ran the tightened test at head, and re-checked the full diff for drift.

  • Tightened the role-help subprocess test to require a zero exit code — except SystemExit: pass became except SystemExit as exc: assert exc.code == 0, so the test now catches a regression where role --help renders via an error path (e.g. Click's SystemExit(2)) even if no HTTP imports leaked; both parametrized cases pass at 8cdf3e3.

Pullfrog  | View workflow run | Using glm-5.3 | 𝕏


def _run(course_id: int, json_output: bool, action: Callable[[AssessmentAPI], Any]) -> None:
# Imported here, not at module level, so role --help skips the HTTP stack.
from .api import LighthouseClient

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: These function-level imports run before the try block, so an ImportError (broken install/missing dependency) escapes _run's format_user_error handling and surfaces as a raw Click traceback instead of the repo's safe JSON/human error path. Before this change the same failure crashed at module import, so this is not a regression, but the failure is now deferred later in the flow — in _create's non-dry-run path the user answers the click.confirm prompt first and only then hits the crash. If you want the error contract to hold even in broken-environment scenarios, consider placing these imports inside the guarded region or catching ImportError explicitly at this boundary.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.


def _create(course_id: int, resource: str, payload: dict[str, Any], yes: bool, dry_run: bool, json_output: bool) -> None:
if dry_run:
from .assessment_api import project

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Worth being aware that this dry-run path still transitively loads the full HTTP stack: assessment_api imports requests and .api at its own module level, so from .assessment_api import project here pulls both in at command execution time. The lazy-import win is therefore limited to --help/role-group resolution (which the new subprocess test correctly guards), not to dry-run execution. That is fine for the stated perf goal, but a future reader may assume --dry-run is HTTP-stack-free; a brief note here (or moving project to a dependency-free module later) would prevent that misconception.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@kilo-code-bot

kilo-code-bot Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: 2 Issues Found | Recommendation: Merge (non-blocking suggestions only)

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 2
Issue Details (click to expand)

SUGGESTION

File Line Issue
lighthouse_cli/assessment_commands.py 30 Function-level imports in _run sit outside the try, so an ImportError in a broken environment escapes as a raw Click traceback instead of the format_user_error/JSON-safe error path; in _create's non-dry-run flow the failure now surfaces only after the user answers the confirmation prompt.
lighthouse_cli/assessment_commands.py 154 The --dry-run branch imports project from assessment_api, which imports requests and lighthouse_cli.api at its own module level, so dry-run execution still transitively loads the full HTTP stack; the lazy-import benefit is limited to --help/role-group resolution (which the new subprocess test correctly guards).
Files Reviewed (3 files)
  • lighthouse_cli/assessment_commands.py - 2 issues
  • lighthouse_cli/course_read_commands.py - 0 issues
  • tests/test_assessment_workflows.py - 0 issues (prior exit-code finding verified fixed at HEAD 8cdf3e3; all 8 mock-patch retargets to lighthouse_cli.api.LighthouseClient are correct for the function-level import pattern)

Fix these issues in Kilo Cloud


Reviewed by glm-5.3-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

@rabesss
rabesss merged commit f7b5dce into main Oct 2, 2026
15 checks passed
@rabesss
rabesss deleted the perf/lazy-role-imports branch October 2, 2026 07:36
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.

Reduce CLI startup latency by deferring legacy command imports

1 participant