Skip to content

fix: HAProxy routing, env vars, + canonical endpoint verification#261

Closed
sheepdestroyer wants to merge 15 commits into
masterfrom
test/canonical-endpoint-verification
Closed

fix: HAProxy routing, env vars, + canonical endpoint verification#261
sheepdestroyer wants to merge 15 commits into
masterfrom
test/canonical-endpoint-verification

Conversation

@sheepdestroyer

@sheepdestroyer sheepdestroyer commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes HAProxy routing, container env vars, and adds comprehensive endpoint verification.

HAProxy fixes (5 issues)

  • Dev subpath routing hit prod backends — fixed by moving dev rules first
  • Added missing dev-specific backends for LiteLLM, Langfuse, router
  • Langfuse empty-path 400 on HTTP/1.1 — fixed with / fallback
  • Langfuse /_next assets 404 — moved SPA rules before catch-all
  • LiteLLM /ui/login 404 — added backend rule before catch-all

Container env fixes (3 issues)

  • LiteLLM UI password now defaults to LITELLM_MASTER_KEY when UI_PASSWORD is unset (security fix)
  • Langfuse NEXTAUTH_URL derived from PUBLIC_BASE_URL
  • Prod .env gets PUBLIC_BASE_URL

Verification script (scripts/verification/verify_canonical_endpoints.py)

Comprehensive endpoint test suite: Router API, LiteLLM, Langfuse, infrastructure, E2E chat, direct LiteLLM, canonical HTTPS URLs.

Shared response parser (new: scripts/chat_helpers.py)

parse_chat_response() extracted to a shared module, used by all 4 classifier scripts + the verification script. Eliminates duplicated defensive parsing logic.

Review fixes on this PR (from #260)

  • Gemini Code Assist: UI_PASSWORD defaults to LITELLM_MASTER_KEY (not 'admin')
  • Gemini Code Assist: PUBLIC_BASE_URL .rstrip('/') prevents double-slash URLs
  • Sourcery: parse_chat_response() extracted to shared scripts/chat_helpers.py
  • Sourcery: Skipped canonical URL tests show '⚠ SKIP' instead of ✓
  • Sourcery: Removed unused base_url from load_env() return dict

Test results

  • 92 unit tests: all passing
  • Prod verification: 24/24 passed
  • Dev verification: 24/24 passed

Summary by Sourcery

Add a canonical endpoint verification script and centralize chat response parsing while tightening container environment configuration for LiteLLM and Langfuse.

New Features:

  • Introduce a canonical endpoint verification script that exercises router, LiteLLM, Langfuse, infrastructure services, E2E chat flows, and public HTTPS URLs for both prod and dev.
  • Add shared chat completion response parsing helpers reused by verification and classifier scripts.

Enhancements:

  • Update classifier and retry scripts to use shared defensive chat response parsing and return explicit error markers on malformed responses.
  • Derive Langfuse NEXTAUTH_URL from PUBLIC_BASE_URL to align OAuth redirects with the public routing domain.
  • Document canonical endpoint verification usage and coverage in the README.

Chores:

  • Export LiteLLM UI credentials into the pod manifest with sensible defaults derived from LITELLM_MASTER_KEY and new UI env vars.
  • Ensure PUBLIC_BASE_URL is propagated into prod .env and pod configuration for canonical URL checks.

boy added 11 commits July 11, 2026 22:47
Reads ports/URLs from .env (and .env.dev overlay), validates:
- Router: /v1/models, /metrics, /dashboard, /api/dashboard-stats
- LiteLLM: /health/liveness, /health/readiness, /v1/models
- Langfuse: /api/public/health
- E2E: 3 chat completions through triage router
- Canonical HTTPS URLs (graceful skip on DNS failure)

Usage:
  python scripts/verification/verify_canonical_endpoints.py         # prod
  python scripts/verification/verify_canonical_endpoints.py --dev   # dev
- Pass UI_USERNAME/UI_PASSWORD from .env to LiteLLM container as
  LITELLM_UI_USERNAME/LITELLM_UI_PASSWORD (LiteLLM's expected env vars)
- Derive NEXTAUTH_URL from PUBLIC_BASE_URL instead of hardcoding
  localhost — fixes Langfuse post-login redirect to broken URL
- LiteLLM: /llm-routing/litellm/ui/ (SERVER_ROOT_PATH prefix required)
- Langfuse: / (web UI root)
- Canonical URLs: /litellm/ui/ and /langfuse
…cation

- Router: /visualizer endpoint
- Infrastructure: MinIO /minio/health/live, ClickHouse /ping
- LiteLLM: direct chat completion (bypasses triage router)
- Canonical URLs: /visualizer
- POST agent-simple-core through public URL (dev.vendeuvre.lan)
- Validates full HTTPS path: HAProxy → dev router → LiteLLM → model
- Graceful DNS skip when host unreachable
- All three chat completion parsers now use .get() on choices/message
  instead of direct indexing (Gemini Code Assist review)
- Canonical URL skipped checks are tracked separately and shown
  in summary as 'X skipped' (Sourcery review)
… ports, .env logging

- Extract parse_chat_response() helper to deduplicate chat completion parsing
  across test_e2e_chat, test_litellm_direct_chat, and test_canonical_urls
- Add isinstance guards for defensive JSON response parsing (Gemini suggestion)
- Load MinIO and ClickHouse ports from .env (MINIO_S3_PORT, CLICKHOUSE_HTTP_PORT)
  instead of hardcoding 9002/8123 (Sourcery suggestion)
- Log which .env files were loaded and warn on missing files (Sourcery suggestion)
- Handle export prefix and inline comments in .env parser (Gemini suggestion)
Apply isinstance guards on choices/message across benchmark_classifier.py,
retry_errors.py, and reclassify_all.py — same pattern as the verification
script's parse_chat_response() helper. Prevents unhandled exceptions when
the API returns unexpected response structures.
- Drop fragile inline-comment stripping from env parser (Sourcery + Gemini):
  heuristic truncated '#' in passwords/URLs — only full-line comments now
- Add encoding='utf-8' to open() call (Gemini)
- Add isinstance(data, dict) guards before .get() calls in all 4 classifier
  scripts: benchmark_classifier, reclassify_all, retry_errors, classify_direct
- Use 'or' fallback for UI_USERNAME/UI_PASSWORD (Gemini):
  os.environ.get('X') or 'admin' handles empty-string exports
- Send auth headers on canonical GET endpoints (Sourcery):
  defense-in-depth even though router public endpoints don't require auth
- Fix exit condition: skipped DNS-unreachable endpoints no longer cause
  non-zero exit code (Sourcery high-level)
- Security: UI_PASSWORD defaults to LITELLM_MASTER_KEY (not 'admin')
  when UI_PASSWORD env var is unset (Gemini Code Assist)
- Robustness: strip trailing slash from PUBLIC_BASE_URL to prevent
  double-slash URLs (Gemini Code Assist)
- Deduplication: extract parse_chat_response() into shared
  scripts/chat_helpers.py, imported by all 4 classifier scripts +
  verification script (Sourcery)
- UX: skipped canonical URL tests show '⚠ SKIP' instead of ✓
  (Sourcery)
- Cleanup: remove unused 'base_url' from load_env() return dict
  (Sourcery)
@sourcery-ai

sourcery-ai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Fixes HAProxy routing and environment configuration for LiteLLM/Langfuse, introduces a canonical endpoint verification script, and centralizes chat response parsing used across verification and classifier scripts.

File-Level Changes

Change Details Files
Add canonical endpoint verification script that exercises router, LiteLLM, Langfuse, infra services, E2E chat, LiteLLM direct chat, and public HTTPS URLs using .env/.env.dev-derived config.
  • Implement verify_canonical_endpoints.py with env loader that reads .env and optional .env.dev, normalizes PUBLIC_BASE_URL, and constructs service ports and credentials.
  • Add test suites for router API endpoints, LiteLLM health and models, Langfuse health and UI, MinIO and ClickHouse health checks, E2E chat via router, direct LiteLLM chat, and canonical HTTPS URLs including graceful DNS-unreachable skips.
  • Aggregate per-section pass/total/skip counts and exit non‑zero on failures, with clear console reporting of per-endpoint status and summary.
scripts/verification/verify_canonical_endpoints.py
Introduce shared defensive chat completion response parser and refactor classifier/utility scripts to use it instead of ad-hoc JSON traversal.
  • Create chat_helpers.parse_chat_response() to safely extract content and reasoning_content from OpenAI-compatible responses with robust type/shape checks.
  • Update retry_errors.py to import parse_chat_response, return ERROR when no content, and keep tier normalization logic on top of parsed content.
  • Update benchmark_classifier.py, reclassify_all.py, and classify_direct.py to import and use parse_chat_response, standardizing error handling and content extraction across scripts.
scripts/chat_helpers.py
scripts/retry_errors.py
scripts/benchmark_classifier.py
scripts/reclassify_all.py
scripts/classify_direct.py
Adjust pod startup script and Kubernetes pod manifest to correctly wire LiteLLM UI and Langfuse NEXTAUTH_URL from environment, improving security and canonical URL handling.
  • Extend start-stack.sh render_pod_yaml env exports to include UI_USERNAME/UI_PASSWORD and NEXTAUTH_URL, and add replacements for LiteLLM UI credentials and Langfuse NEXTAUTH_URL placeholders.
  • Default LiteLLM UI username to admin and UI password to UI_PASSWORD or LITELLM_MASTER_KEY (falling back to admin), ensuring secure behavior when UI_PASSWORD is unset.
  • Derive NEXTAUTH_URL from PUBLIC_BASE_URL/langfuse instead of localhost+Langfuse port, and normalize PUBLIC_BASE_URL with rstrip('/') to avoid double-slash URLs.
  • Update pod.yaml to inject LITELLM_UI_USERNAME and LITELLM_UI_PASSWORD env vars, and to use NEXTAUTH_URL_PLACEHOLDER for Langfuse NEXTAUTH_URL.
start-stack.sh
pod.yaml
Document canonical endpoint verification flow and PUBLIC_BASE_URL requirement in README for both prod and dev environments.
  • Add README section describing canonical endpoint verification usage for prod and dev, listing covered endpoint categories and specific paths.
  • Document requirement for PUBLIC_BASE_URL in prod .env and show example URL; clarify that dev .env.dev already contains PUBLIC_BASE_URL.
  • Explain that canonical URL tests hit router, LiteLLM UI, Langfuse, infra endpoints, E2E chat, direct LiteLLM, and HTTPS canonical URLs.
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Jul 11, 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: 2 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: 33f68f83-8aeb-4603-9181-df855810704f

📥 Commits

Reviewing files that changed from the base of the PR and between 6d064bb and 010d8a9.

📒 Files selected for processing (15)
  • README.md
  • pod.yaml
  • scripts/README.md
  • scripts/__init__.py
  • scripts/benchmark_classifier.py
  • scripts/chat_helpers.py
  • scripts/classify_direct.py
  • scripts/reclassify_all.py
  • scripts/retry_errors.py
  • scripts/upgrade-prod.sh
  • scripts/verification/__init__.py
  • scripts/verification/verification_helpers.py
  • scripts/verification/verify_canonical_endpoints.py
  • scripts/verification/verify_ollama_routing.py
  • start-stack.sh
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/canonical-endpoint-verification

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.

@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.

Hey - I've left some high level feedback:

  • The various scripts manipulate sys.path to import chat_helpers; consider turning scripts into a proper package or centralizing helper imports to avoid per-script path hacks and make module resolution more robust.
  • In verify_canonical_endpoints.load_env, the manual .env parsing strips quotes but doesn’t handle escaped characters or whitespace around values; aligning this with the existing config/env loading approach (or a small shared loader) would reduce the risk of subtle parsing differences.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The various scripts manipulate `sys.path` to import `chat_helpers`; consider turning `scripts` into a proper package or centralizing helper imports to avoid per-script path hacks and make module resolution more robust.
- In `verify_canonical_endpoints.load_env`, the manual .env parsing strips quotes but doesn’t handle escaped characters or whitespace around values; aligning this with the existing config/env loading approach (or a small shared loader) would reduce the risk of subtle parsing differences.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@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 introduces a canonical endpoint verification script to validate services across environments, extracts a shared chat response parsing helper to improve code reuse across benchmark scripts, and configures UI credentials and NextAuth URL placeholders in the pod deployment. The review feedback suggests enhancing the environment loader to respect shell environment overrides, printing a clear skip message when the public base URL is missing, and defensively checking response types in the shared parser to prevent potential attribute errors.

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 on lines +61 to +70
return {
"router_port": env.get("ROUTER_PORT", "5000"),
"litellm_port": env.get("LITELLM_PORT", "4000"),
"langfuse_web_port": env.get("LANGFUSE_WEB_PORT", "3001"),
"litellm_master_key": env.get("LITELLM_MASTER_KEY", "gateway-pass"),
"router_api_key": env.get("ROUTER_API_KEY", "gateway-pass"),
"public_base_url": env.get("PUBLIC_BASE_URL", "").rstrip("/"),
"minio_s3_port": env.get("MINIO_S3_PORT", "9002"),
"clickhouse_http_port": env.get("CLICKHOUSE_HTTP_PORT", "8123"),
}

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 load_env function currently only resolves configuration values from the parsed .env and .env.dev files, completely ignoring any environment variables set in the shell/process environment (os.environ). To align with standard environment variable behavior where shell-defined variables can override file-defined defaults, consider falling back to os.environ when resolving the configuration keys.

Suggested change
return {
"router_port": env.get("ROUTER_PORT", "5000"),
"litellm_port": env.get("LITELLM_PORT", "4000"),
"langfuse_web_port": env.get("LANGFUSE_WEB_PORT", "3001"),
"litellm_master_key": env.get("LITELLM_MASTER_KEY", "gateway-pass"),
"router_api_key": env.get("ROUTER_API_KEY", "gateway-pass"),
"public_base_url": env.get("PUBLIC_BASE_URL", "").rstrip("/"),
"minio_s3_port": env.get("MINIO_S3_PORT", "9002"),
"clickhouse_http_port": env.get("CLICKHOUSE_HTTP_PORT", "8123"),
}
return {
"router_port": os.environ.get("ROUTER_PORT") or env.get("ROUTER_PORT", "5000"),
"litellm_port": os.environ.get("LITELLM_PORT") or env.get("LITELLM_PORT", "4000"),
"langfuse_web_port": os.environ.get("LANGFUSE_WEB_PORT") or env.get("LANGFUSE_WEB_PORT", "3001"),
"litellm_master_key": os.environ.get("LITELLM_MASTER_KEY") or env.get("LITELLM_MASTER_KEY", "gateway-pass"),
"router_api_key": os.environ.get("ROUTER_API_KEY") or env.get("ROUTER_API_KEY", "gateway-pass"),
"public_base_url": (os.environ.get("PUBLIC_BASE_URL") or env.get("PUBLIC_BASE_URL", "")).rstrip("/"),
"minio_s3_port": os.environ.get("MINIO_S3_PORT") or env.get("MINIO_S3_PORT", "9002"),
"clickhouse_http_port": os.environ.get("CLICKHOUSE_HTTP_PORT") or env.get("CLICKHOUSE_HTTP_PORT", "8123"),
}

Comment on lines +362 to +364
public = cfg["public_base_url"]
if not public:
return 0, 0, 0

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

When PUBLIC_BASE_URL is not set, the canonical URL tests are silently skipped without any console output. Adding a clear skip message makes the test execution output much more informative and user-friendly.

Suggested change
public = cfg["public_base_url"]
if not public:
return 0, 0, 0
public = cfg["public_base_url"]
if not public:
print("\n── Canonical URLs — SKIPPED (PUBLIC_BASE_URL not set) ──")
return 0, 0, 0

Comment thread scripts/chat_helpers.py Outdated
Comment on lines +30 to +31
content = (message.get("content") or "").strip()
reasoning = (message.get("reasoning_content") or "").strip()

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

To ensure the parsing helper is fully defensive, verify that the retrieved content and reasoning_content values are actually strings before calling .strip(). Some APIs or unexpected error responses might return non-string types (such as integers, lists of content blocks, or dictionaries), which would raise an AttributeError.

Suggested change
content = (message.get("content") or "").strip()
reasoning = (message.get("reasoning_content") or "").strip()
content_val = message.get("content")
content = content_val.strip() if isinstance(content_val, str) else ""
reasoning_val = message.get("reasoning_content")
reasoning = reasoning_val.strip() if isinstance(reasoning_val, str) else ""

boy added 4 commits July 12, 2026 01:11
- LiteLLM liveness: /ping (404) → /health/liveness (200)
- Langfuse liveness+readiness: /api/health (404) → /api/public/health (200)

Both containers were crash-looping because Podman's
HealthcheckOnFailureAction=restart kept killing them when the health
checks hit the wrong endpoints (404). RestartCount was 22+ on both
dev and prod before the fix; now 0 and healthy.
Pulls the latest GitHub release tag, clones it, rsyncs runtime files
(pod.yaml, start-stack.sh, litellm/, router/, scripts/) into ~/prod/
without touching .env or data/, then redeploys via start-stack.sh --pull.

Prevents temptation to patch prod directly — all config changes flow
through git releases.
- LiteLLM liveness: /ping → /health/liveness
- Langfuse liveness+readiness: /api/health → /api/public/health
- Add upgrade-prod.sh to directory layout
## Gemini Code Assist fixes
- load_env() now checks os.environ before .env values for config resolution
- test_canonical_urls() prints skip message when PUBLIC_BASE_URL not set
- parse_chat_response() uses isinstance guards before .strip() to prevent
  AttributeError on non-string content/reasoning_content values

## Sourcery fixes
- Created scripts/__init__.py and scripts/verification/__init__.py to
  turn scripts/ into a proper Python package
- All scripts now import via
  with a single WORKDIR sys.path.insert instead of per-script hacks
- .env value parser now strips trailing whitespace after quote removal

## Global consistency
- verification_helpers.py: replaced unsafe inline content extraction
  with shared parse_chat_response()
- verify_ollama_routing.py: same — uses parse_chat_response() instead
  of direct choices[0]["message"].get("content") indexing
- scripts/README.md: added chat_helpers.py module documentation
@sheepdestroyer

Copy link
Copy Markdown
Owner Author

All review comments from Gemini Code Assist and Sourcery have been addressed. Closing in favor of a fresh PR. See #262.

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