feat(boot): self-test OpenRouter at startup — fail fast on env typos#308
Merged
Conversation
Frontier_sip_17 caught two stacked env-var bugs that took 100% of prod chat
traffic offline silently for an unknown window:
1. SIPHER_MODEL set to hyphen-form (claude-sonnet-4-6) instead of
dot-form. getSipherModel() throws on bad config, but only fires when
a chat actually happens — meaning the outage manifested as empty
assistant responses with no error log, not a boot failure.
2. OPENROUTER_API_KEY pointed at a stale/revoked key. OpenRouter
returned 401; pi-ai swallowed it and emitted empty content.
This PR adds a 2-token ping to OpenRouter as the third boot step (after
loadNetworkConfig + before getDb). Both failure modes now throw at boot
and Docker restarts the container with the same loud error until env is
fixed — a recoverable container restart loop instead of a silent
production outage.
New module: packages/agent/src/boot/self-test.ts
selfTestOpenRouter({ timeoutMs }?):
- returns early if SIPHER_SKIP_BOOT_SELF_TEST=true
- calls getSipherModel() which throws synchronously on a bad
SIPHER_MODEL — the existing dot-vs-hyphen guard. No fetch yet.
- validates OPENROUTER_API_KEY is set (throws if empty).
- POST https://openrouter.ai/api/v1/chat/completions with
model.id + max_tokens:2 + a 1-message body. AbortController
caps the budget at 5000ms (overridable in tests).
- on non-2xx, throws with HTTP status + body slice (no secret
leakage — OpenRouter doesn't echo keys in error bodies, and
we cap the body slice at 300 chars).
Wiring: packages/agent/src/index.ts adds \`await selfTestOpenRouter()\`
between loadNetworkConfig() and getDb(), with a one-line "self-test
pass (Nms)" success log. Top-level await is already used elsewhere in
the file (e.g. restorePendingActions), so no module-system change.
Skipped automatically in:
- vitest (vitest.config.ts env block — tests don't load index.ts
directly today, but defensive against future integration tests).
- playwright (playwright.config.ts webServer env block — e2e has
no real OPENROUTER key, chat is mocked at the network layer).
- any environment that opts in via SIPHER_SKIP_BOOT_SELF_TEST=true.
.env.example documents this for operators.
Tests: 8 cases in tests/boot/self-test.test.ts — 200 success, 401
HTTP error, 5xx HTTP error, missing API key, invalid SIPHER_MODEL,
abort-on-timeout, skip flag respected, skip flag NOT respected on
non-"true" values. Suite total: 1652 → 1662 (+10) after merge of
prior #295/#294.
Closes #293.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #293. Adds a 2-token ping to OpenRouter as the third boot step (after
loadNetworkConfig, beforegetDb). Catches the two silent-outage modes fromfrontier_sip_17at boot instead of letting them manifest as empty assistant responses on every chat turn:SIPHER_MODELtypo —getSipherModel()throws synchronously when pi-ai's registry doesn't know the model (e.g. the hyphen-formclaude-sonnet-4-6instead of dot-form).OPENROUTER_API_KEY— the ping returns 401 / 403 and we surface that here.Both now produce a Docker restart-loop with a loud error in the container logs instead of accepting traffic and degrading silently.
New module
packages/agent/src/boot/self-test.ts:SIPHER_SKIP_BOOT_SELF_TEST=true.getSipherModel()throws on invalidSIPHER_MODELbefore any fetch.OPENROUTER_API_KEYis set.https://openrouter.ai/api/v1/chat/completionswithmax_tokens: 2.AbortControllercaps budget at 5000ms.OpenRouter self-test failed: HTTP <status> - <body slice>. Body slice capped at 300 chars (no secret leakage; OpenRouter doesn't echo keys in error envelopes).Wiring
packages/agent/src/index.tsaddsawait selfTestOpenRouter()betweenloadNetworkConfig()andgetDb(). Top-level await is already in use elsewhere in the file (e.g.restorePendingActions).Skipped automatically in
vitest.config.tsenv block) — tests don't currently loadindex.ts, but defensive against future integration tests.playwright.config.tswebServer env block) — e2e has no realOPENROUTER_API_KEYand chat is mocked at the network layer.SIPHER_SKIP_BOOT_SELF_TEST=true(documented in.env.example).Test plan
pnpm vitest run tests/boot/self-test.test.ts— 8 passed.pnpm test -- --run(root) — 1662 passed (was 1652 baseline; +10 net = 8 new self-test + 2 from the2 skippedtorque integrations being recounted).pnpm typecheckclean across root + sdk + app + agent.OpenRouter: self-test pass (~Nms)line ~200-500ms afterNetwork: devnet ...line. IfOPENROUTER_API_KEYis wrong, container should restart-loop with the clearOpenRouter self-test failed: HTTP 401 ...error.New tests
tests/boot/self-test.test.ts(8 cases):OPENROUTER_API_KEYis unset (no fetch attempted)getSipherModelerror whenSIPHER_MODELis invalid (no fetch attempted)SIPHER_SKIP_BOOT_SELF_TEST=true"false"or other truthy stringsFollow-ups (not in this PR)
SIPHER_SELF_TEST_TIMEOUT_MSenv var if 5s default ever turns out to be too tight for some operator network. Punt until needed.