Skip to content

fix(server): warn at startup when only half of the LLM gateway is configured - #236

Merged
camielvs merged 2 commits into
mainfrom
warn-half-configured-gateway
Dec 3, 2026
Merged

camielvs merged 2 commits into
mainfrom
warn-half-configured-gateway

Conversation

@camielvs

Copy link
Copy Markdown
Contributor

Description

The shell routes models through an LLM gateway only when both PI_PROXY_URL and PI_PROXY_API_KEY are set. If only one is set, agents silently call each provider directly, and you only find out when an agent turn fails.

The server now logs one warning at startup (not on every agent spawn) when exactly one of the two is set. The warning says which variable is missing and what happens instead, for example:

[pi] PI_PROXY_URL is set but PI_PROXY_API_KEY is not. The LLM gateway is used only when both are set, so agents will call each provider directly with its own key (e.g. OPENAI_API_KEY or ANTHROPIC_API_KEY). Set PI_PROXY_API_KEY to use the gateway, or unset PI_PROXY_URL.

With a model catalog configured, every model goes through the gateway anyway, so in that case the warning says requests will carry no API key.

Startup is never blocked: direct provider keys may be exactly what you want. The check follows the startup-check pattern from #223 (a catalog with no gateway). The README config table and docs/server/orchestrator.md each gain one sentence about the both-or-neither rule.

Screenshots

N/A

How to test

  1. cd apps/server && pnpm exec tsx --test src/pi/gatewayEnv.test.ts: 5 tests pass.
  2. Start the server with PI_PROXY_URL=https://gateway.example.com and no PI_PROXY_API_KEY. One warning naming PI_PROXY_API_KEY is printed before [server] listening …, and the server keeps running.
  3. Swap them (only PI_PROXY_API_KEY set). The warning names PI_PROXY_URL.
  4. Set both, or neither. No warning.
  5. pnpm typecheck, pnpm lint and cd apps/server && pnpm test pass.

Notes

  • The existing per-spawn "No LLM credentials found" warning is unchanged. It covers no credentials at all; this one covers a gateway that is only half set, including when direct provider keys are also present.
  • The "smooth the first local run" PR edits the README config table two lines above the row this PR changes; whichever merges second may need a quick rebase.

🤖 Generated with Claude Code

…figured

The gateway is used only when both PI_PROXY_URL and PI_PROXY_API_KEY are set.
With just one, agents silently call providers directly and the mistake only
shows up as a failed turn. Log one warning at server start naming the missing
variable and what happens instead; with a model catalog, warn that gateway
requests will carry no key. Startup is never blocked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@camielvs
camielvs marked this pull request as ready for review December 1, 2026 16:33
@camielvs
camielvs requested a review from a team as a code owner December 1, 2026 16:33
@EdwardGallant

Copy link
Copy Markdown

🤖 AI-assisted review (worry pass + test validation). Generated by my review agents and read by me before posting. Nothing here is a blocker unless it says so.

Hey @camielvs, I did a pre-review pass over the diff and the code it relies on: the catalog startup assertion in modelCatalog.ts, the proxy-provider extension, the per-spawn warnMissingProxyEnv line and the session-env merge in piAgentManager.ts. I didn't tophat it, so I never started the server with only some of the env set. Nothing here blocks the PR, and I'd take it with a grain of salt.

Worth acting on

  1. (Optional) No test checks that catalog + only PI_PROXY_API_KEY prints no warning.
    gatewayEnv.ts:33, return hasKey ? undefined : CATALOG_WITHOUT_KEY_MESSAGE, is quiet on purpose. That's correct, because assertCatalogSettings in modelCatalog.ts:77 already throws when a catalog has no PI_PROXY_URL. But when I changed that line to an unconditional return CATALOG_WITHOUT_KEY_MESSAGE, all 5 tests still passed. A later refactor could start printing "requests will carry no API key" for a setup that has the key and no URL, and no test would fail. The catalog test in gatewayEnv.test.ts:47 only covers URL-without-key.
    Suggest: add a { ...catalog, PI_PROXY_API_KEY: "k" } case to that test's loop and expect [].
Checked and fine · what was run · not checked

What was run

  • cd apps/server && pnpm exec tsx --test src/pi/gatewayEnv.test.ts: 5 of 5 passed at baseline. I didn't re-run the rest of the server suite for this PR. CI's Tests job is green.
  • Mutation testing on gatewayEnv.ts: 8 mutations, 7 caught, 1 survived. The tests caught all of these:
    • swapping the both/neither early return
    • dropping either catalog variable from configuresCatalog
    • making configuresCatalog always true
    • swapping the two fallback messages
    • changing Boolean(key) to key !== undefined
    • bypassing the injected warn
  • The one survivor is the claim no test checks: "with a catalog and only the key set, no warning is printed." That's item 1 above.

Checked and fine

  • Catalog + key-only / catalog + neither stay silent. Fine: assertCatalogSettings (modelCatalog.ts:77) throws "set PI_PROXY_URL" in both cases when the catalog loads, so the hasKey ? undefined branch never hides a broken setup.
  • The new startup line contradicting the per-spawn one. Fine: warnMissingProxyEnv (piAgentManager.ts:533) only says "PI_PROXY_URL is not set…". It mentions no placeholder or "will default to" URL (config.ts:160 leaves it undefined). Key-only does log both lines, but the overlap is only in wording.

Good to know

  • A session can half-configure the gateway without the boot warning seeing it. No shipped path sets PI_PROXY_* per session, and the local-agent host sets both together (packages/local-agent-host/src/piAdapter.ts:104-105). But the REST create-session env field passes them through sanitizeSessionEnv, which only filters keys (piAgentManager.ts:647). So PI_PROXY_API_KEY: "" there would override the server value for each spawn. A boot-time warning isn't meant to catch that. I'm noting it only so the docs don't promise more than the warning covers.

Not checked

  • Base tree mismatch (assertPiVersion / the fix(server): refuse to start with a model catalog but no LLM gateway #223 assertion missing at base). Not compared against base. Both exist on head (index.ts:97, modelCatalog.ts:72).
  • What pi actually does with an empty $PI_PROXY_API_KEY in catalog mode (sends the literal string, falls back to auth.json, or refuses). Not checked, so "its requests will carry no API key" is unverified.
  • TANGENT_MODEL_CATALOG_JSON set directly in the server env. configuresCatalog only looks at _FILE / _URL (gatewayEnv.ts:22-26).
  • Coverage of the default process.env / console.warn wiring and the call in index.ts:98. Not checked beyond the mutation run.
  • The docs sentence "Pi otherwise falls back to calling providers directly" leaving out the catalog case.
  • Edge values: PI_PROXY_URL=/ or whitespace. The extension strips trailing slashes before it tests for an empty value (proxyProvider.ts:27), so / would count as set in the new check and as unset in the extension.
  • The catalog fetch (top-level await) running before the warning. If it throws, the operator would see the catalog error and not this hint.
  • Whether one boot-time console.warn reaches anyone (lost in turbo pnpm dev scrollback, or in deployed log collection).
  • The README table rebase against the "smooth the first local run" PR. Your Notes already call it out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@camielvs

camielvs commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

🤖 This is an AI-generated response.

Thanks @EdwardGallant. Added the case in 8f9cdbf: with a catalog (file or URL) and only PI_PROXY_API_KEY set, no warning is printed. The test now fails under the mutation you used (always returning the catalog message).

I also checked whether configuresCatalog should look at TANGENT_MODEL_CATALOG_JSON. It shouldn't: spawnEnv strips that variable from the server environment, adds it back only when the server loaded a catalog from a file or URL, and session env can't set any TANGENT_* key. So file/URL is exactly what puts Pi in catalog mode.

@camielvs
camielvs merged commit d68be18 into main Dec 3, 2026
10 checks passed
@camielvs
camielvs deleted the warn-half-configured-gateway branch December 3, 2026 17:24
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.

2 participants