Skip to content

chore: eliminate sys.path.insert hacks — migrate to package-relative imports#275

Closed
sheepdestroyer wants to merge 13 commits into
masterfrom
chore/eliminate-sys-path-hacks-565508234636993796
Closed

chore: eliminate sys.path.insert hacks — migrate to package-relative imports#275
sheepdestroyer wants to merge 13 commits into
masterfrom
chore/eliminate-sys-path-hacks-565508234636993796

Conversation

@sheepdestroyer

Copy link
Copy Markdown
Owner

What

This PR removes vestigial sys.path.insert hacks from scripts in scripts/, migrating to standard package-relative imports with ImportError fallbacks. It also adds a missing __init__.py to the router/ directory, normalizes structured content parsing in chat_helpers.py, removes dead sys.path.insert hacks from two router test files flagged by Gemini Code Assist, and moves the classifier api_base from a hardcoded URL to an env-var placeholder.

Fixes #266

Review feedback addressed (since PR #273)

Gemini Code Assist

  • tests/test_a2_verify.py: added try/except ImportError fallback so the script remains runnable both via pytest and directly (python tests/test_a2_verify.py)

CodeRabbit

  • router/main.py: extracted shared _http_limits() helper to deduplicate httpx.Limits construction between get_http_client() and get_classifier_client()
  • router/main.py: replaced hardcoded verify=False with CLASSIFIER_CA_BUNDLE env var resolution (defaults to False — current behavior preserved; set to a PEM path to enable TLS verification)
  • router/main.py: close _classifier_client in lifespan shutdown cleanup alongside _http_client and _redis_client

Env consistency

  • .env.dev: added LLAMA_CLASSIFIER_URL since the llama classifier is shared across dev and prod environments

Rejected review comments (with reasoning)

  • benchmark_tokens.py sys.path.insert: Already uses the try/except ImportError fallback pattern — the same pattern consistently used across ALL 5 verification/benchmark scripts. This is not the old unconditional hack the PR eliminates.
  • verify_canonical_endpoints.py sys.path.insert: Same — standard project pattern.
  • Docstring coverage < 80%: Global repo concern, not introduced or worsened by this PR.
  • Out-of-scope classifier changes: Per project policy, out-of-scope items are tracked as separate issues.

Original Changes (preserved from PR #272)

Import cleanup (9 scripts)

  • Removed sys.path.insert hacks from all scripts in scripts/ and verification/
  • Replaced with from scripts.chat_helpers import parse_chat_response + ImportError fallback
  • Added try/except ImportError pattern to all scripts

Router package

  • Added router/__init__.py to make router a proper Python package
  • Updated router/main.py to use from router.circuit_breaker import get_breaker

chat_helpers.py

  • Added _normalize_chat_content() helper for structured content payloads
  • Updated parse_chat_response() to use the new normalizer

upgrade-prod.sh

  • Self-copy guard, centralized cleanup via trap, non-interactive mode, pre-flight .env validation, split rsync calls

Misc

  • Removed unused imports (asyncio, time) from test files

Related

google-labs-jules Bot and others added 12 commits July 12, 2026 14:04
Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Revert 'from .circuit_breaker' to 'from router.circuit_breaker' in main.py:
  relative imports break when main.py is imported directly as 'main'
  (tests use 'from main import ...' with PYTHONPATH=.:router)
- Add try/except ImportError fallback to 'from scripts.chat_helpers'
  across all 7 scripts (4 in scripts/, 3 in verification/)
  so they remain runnable via both package import and direct execution
…e imports

This commit:
- Removes sys.path.insert hacks from scripts/
- Adds router/__init__.py to make router a package
- Updates router/main.py and router/agy_proxy.py to use package-aware imports
- Updates tests and mocks to use absolute package paths
- Updates CI workflow to use standard PYTHONPATH=.
- Ensures both CLI and package execution modes work correctly

Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Gemini Code Assist correctly identified that the PR's removal of sys.path
hacks broke direct execution of all 9 scripts. When Python runs a script,
it REPLACES sys.path[0] (the CWD) with the script's directory — so cross-
package imports (router.*, scripts.chat_helpers) fail with ModuleNotFoundError.

Two failure patterns, two fixes:

1. Scripts in scripts/ importing scripts.chat_helpers:
   try/except with bare 'from chat_helpers' fallback (found in sys.path[0])

2. Scripts in scripts/verification/ importing scripts.chat_helpers or router.*:
   try/except with sys.path.insert to repo root (parents[2])

Verified: all 9 scripts now run successfully via both 'python scripts/X.py'
(direct) and package imports (from scripts.X import ...)
Gemini Code Assist flagged that test_dashboard_data.py and
test_resolve_external_urls.py still use sys.path.insert(0, router_path)
hacks despite having migrated to absolute package imports
(from router import main).

These hacks were doubly broken:
1. They inserted router/ (the package dir) into sys.path, which doesn't
   help resolve 'from router import main' — Python needs the PARENT dir.
2. router/tests/conftest.py already inserts the correct dir (repo root).

Removed the dead sys/os imports along with the hack blocks.
All 193 tests pass. Scripts import cleanly.
- Add os.environ/ resolution for api_base in router/main.py (same pattern
  as api_key resolution at line 351)
- Add get_classifier_client() with verify=False for internal self-signed TLS
- Change router/config.yaml api_base to os.environ/LLAMA_CLASSIFIER_URL
  placeholder instead of hardcoded URL
- Update test patches to use get_classifier_client
- Add LLAMA_CLASSIFIER_URL to .env pointing at canonical endpoint
  https://x570.vendeuvre.lan/llm-routing/llama/v1

The bot previously hardcoded the canonical URL directly in config.yaml.
Now it's resolved from .env at runtime, keeping config DRY and making
dev/prod environments differ only by their .env files.
The bot previously hardcoded the canonical endpoint in litellm/config.yaml
for two local models (local-qwen-3.6 and nomic-embed-text). Replace with
LLAMA_CLASSIFIER_URL_PLACEHOLDER, resolved at render time by
render_litellm_config() via the same env var used for the router classifier.

The placeholder is substituted during deploy (start-stack.sh) using the
value from .env, keeping config DRY — dev/prod differ only by .env files.
from router.circuit_breaker fails in Docker's flat /app/ layout where
files are not nested in a router/ package directory. Add try/except
ImportError with flat 'from circuit_breaker' fallback — same pattern
used for scripts/chat_helpers imports.
Router waits up to 180s for LiteLLM startup but liveness probe fired
at 20s — on cold starts (postgres from scratch, LiteLLM migrations),
the container was killed before LiteLLM was ready, causing a restart
loop. Readiness probe also bumped from 10s to 30s.

Matches LiteLLM's own 240s liveness probe.
Replace all references to gemma4-26a4b-routing, qwen-0.8b-routing, and
qwen-2b-routing with the canonical qwen-4b-routing classifier model.

Changed files: router/config.yaml, router/main.py, README.md, test
classifier accuracy test, and all 4 batch classification scripts.
- tests/test_a2_verify.py: add try/except ImportError fallback for direct
  script execution (Gemini Code Assist review)
- router/main.py: extract shared _http_limits() helper to deduplicate
  httpx.Limits construction across get_http_client() and
  get_classifier_client() (CodeRabbit review)
- router/main.py: use CLASSIFIER_CA_BUNDLE env var for classifier TLS
  verify (defaults to False — current behavior preserved)
  (CodeRabbit security review)
- router/main.py: close _classifier_client in lifespan shutdown cleanup
  (CodeRabbit stability review)
- .env.dev: add LLAMA_CLASSIFIER_URL (shared classifier across envs)

Rejected review comments:
- benchmark_tokens.py sys.path.insert: already uses try/except ImportError
  fallback — same pattern as all other scripts, not a violation
- verify_canonical_endpoints.py sys.path.insert: same — standard pattern
- docstring coverage < 80%: global repo concern, not PR-specific
- out-of-scope classifier changes: tracked via policy as issue, not
  blocking

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry @sheepdestroyer, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

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

Next review available in: 50 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: a6b38808-a54a-4b16-80d0-449461acaf32

📥 Commits

Reviewing files that changed from the base of the PR and between 3f7f503 and 2894100.

📒 Files selected for processing (30)
  • .env.dev
  • .github/workflows/test.yml
  • README.md
  • litellm/config.yaml
  • pod.yaml
  • router/__init__.py
  • router/agy_proxy.py
  • router/config.yaml
  • router/main.py
  • router/tests/test_dashboard_data.py
  • router/tests/test_detect_active_tool.py
  • router/tests/test_estimate_prompt_tokens.py
  • router/tests/test_get_gemini_oauth_status.py
  • router/tests/test_get_goose_sessions.py
  • router/tests/test_load_persisted_stats.py
  • router/tests/test_resolve_external_urls.py
  • router/tests/test_routing_behavior.py
  • scripts/benchmark_classifier.py
  • scripts/benchmark_tokens.py
  • scripts/classify_direct.py
  • scripts/reclassify_all.py
  • scripts/retry_errors.py
  • scripts/verification/verification_helpers.py
  • scripts/verification/verify_breaker.py
  • scripts/verification/verify_canonical_endpoints.py
  • scripts/verification/verify_ollama_routing.py
  • start-stack.sh
  • tests/test_a2_verify.py
  • tests/test_classifier_accuracy.py
  • tests/test_models_proxy.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/eliminate-sys-path-hacks-565508234636993796

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request transitions the complexity triage classifier to Qwen-4B, introduces the LLAMA_CLASSIFIER_URL environment variable to dynamically configure the classifier endpoint, and adds a dedicated HTTP client singleton for classifier calls with optional TLS verification. Additionally, it adjusts container startup delays and refactors test and script imports to use absolute paths. The review feedback suggests adding a fallback default for LLAMA_CLASSIFIER_URL in start-stack.sh to prevent empty configurations, robustly parsing boolean-like strings for CLASSIFIER_CA_BUNDLE to avoid runtime crashes, and adding a type check on router_api_base to prevent potential AttributeError exceptions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread start-stack.sh Outdated
mkdir -p "$rendered_dir"
sed -e "s/VALKEY_CACHE_PORT_PLACEHOLDER/${VALKEY_CACHE_PORT}/g" \
-e "s/ROUTER_PORT_PLACEHOLDER/${ROUTER_PORT}/g" \
-e "s|LLAMA_CLASSIFIER_URL_PLACEHOLDER|${LLAMA_CLASSIFIER_URL}|g" \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

If LLAMA_CLASSIFIER_URL is not set in the environment (e.g., in a clean production environment where .env has not been manually updated yet), it will be replaced with an empty string. This will silently pass the placeholder validation check but generate an invalid config.yaml with an empty api_base, causing LiteLLM to fail to start or route requests. Providing a sensible default fallback prevents this silent failure.

Suggested change
-e "s|LLAMA_CLASSIFIER_URL_PLACEHOLDER|${LLAMA_CLASSIFIER_URL}|g" \
-e "s|LLAMA_CLASSIFIER_URL_PLACEHOLDER|${LLAMA_CLASSIFIER_URL:-http://127.0.0.1:8080/v1}|g" \\

Comment thread router/main.py
Comment on lines +119 to +123
if _classifier_client is None:
verify = os.getenv("CLASSIFIER_CA_BUNDLE") or False
_classifier_client = httpx.AsyncClient(
limits=_http_limits(), timeout=3600.0, verify=verify
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The CLASSIFIER_CA_BUNDLE environment variable is read as a string. If a user explicitly sets it to "False" or "false" in their environment, os.getenv returns a non-empty string which is truthy, causing verify to be set to the string "False". httpx will then attempt to treat this as a file path and raise a FileNotFoundError. We should robustly parse boolean-like strings to prevent runtime crashes.

Suggested change
if _classifier_client is None:
verify = os.getenv("CLASSIFIER_CA_BUNDLE") or False
_classifier_client = httpx.AsyncClient(
limits=_http_limits(), timeout=3600.0, verify=verify
)
if _classifier_client is None:
ca_bundle = os.getenv("CLASSIFIER_CA_BUNDLE")
if ca_bundle is not None and ca_bundle.lower() in ("false", "0", "off"):
verify = False
elif ca_bundle is not None and ca_bundle.lower() in ("true", "1", "on"):
verify = True
else:
verify = ca_bundle or False
_classifier_client = httpx.AsyncClient(
limits=_http_limits(), timeout=3600.0, verify=verify
)

Comment thread router/main.py Outdated

router_model_conf = config.get("router", {}).get("router_model", {})
router_api_base = router_model_conf.get("api_base", "http://127.0.0.1:8080/v1")
if router_api_base.startswith("os.environ/"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

If api_base is explicitly configured as null or parsed as a non-string type from the YAML configuration, router_api_base could be None or another type. Calling .startswith() directly on it will raise an AttributeError. Adding an explicit type check ensures robust defensive programming.

Suggested change
if router_api_base.startswith("os.environ/"):
if isinstance(router_api_base, str) and router_api_base.startswith("os.environ/"):

- start-stack.sh: fail fast if LLAMA_CLASSIFIER_URL is unset/empty
  using ${VAR:?} bash parameter expansion instead of silently
  producing an invalid litellm config with empty api_base
- router/main.py: parse boolean-like strings for CLASSIFIER_CA_BUNDLE
  ('false'/'0'/'off'/'no' → verify=False, 'true'/'1'/'on'/'yes' →
  verify=True) — prevents httpx FileNotFoundError when env var is
  set to 'False' (Python treats non-empty strings as truthy)
- router/main.py: add isinstance(str) guard on router_api_base before
  calling .startswith() — prevents AttributeError if api_base is
  null/non-string in the YAML config
@sheepdestroyer

Copy link
Copy Markdown
Owner Author

Review Feedback Addressed

Addressed the 3 valid Gemini Code Assist findings in commit 2894100. Closing to open fresh PR.

Addressed:

  • start-stack.sh: ${LLAMA_CLASSIFIER_URL:?...} — fails fast instead of silently producing broken config
  • router/main.py: Boolean string parsing for CLASSIFIER_CA_BUNDLE — prevents httpx crash when set to 'False'
  • router/main.py: isinstance(str) guard on router_api_base before .startswith()

Not actionable:

  • CodeRabbit: rate-limited (no review produced)
  • Sourcery: rate-limited (no review produced)

@sheepdestroyer

Copy link
Copy Markdown
Owner Author

Superseded — review fixes applied, opening fresh PR.

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.

refactor: eliminate sys.path.insert hacks — migrate to package-relative imports

1 participant