fix: filter embedding API key errors and harden beforeSend patterns#477
Merged
Conversation
- Validate API keys in pickRemoteFallback() before using them for real
API calls. Keys shorter than 20 characters (e.g. 'nokey' placeholder
set by tools like Codex) are now rejected, falling through to FTS-only
search instead of producing 401 errors against OpenAI.
(Fixes LOREAI-GATEWAY-1G, LOREAI-GATEWAY-1H)
- Add beforeSend patterns for:
- 'Incorrect API key' (defense-in-depth for embedding 401s)
- 'onnxruntime' / 'onnxruntime-node' / 'LoadLibrary failed' /
'Protobuf parsing failed' (ONNX init failures on various platforms)
- 'getSystemErrorMap' (Bun doesn't implement node:util's
getSystemErrorMap, which crashes the Sentry SDK during error
processing — LOREAI-GATEWAY-1A)
BYK
added a commit
that referenced
this pull request
May 27, 2026
## Summary Addresses findings from the adversarial self-review of PR #475 and #477. ### Changes **C1 — Unknown block types silently dropped in `parseAnthropicResponseJSON`** Added a `default` case to the content block switch that preserves unknown block types as serialized JSON text. This matches the pattern used in `toGatewayBlock()` for request parsing and prevents silent data loss if Anthropic adds new content block types. **C2 — Invalid scope values accepted by `filterOps()`** Changed scope validation from `typeof o.scope === "string"` to `o.scope === "project" || o.scope === "global"`. Previously, an LLM producing an invalid scope like `"session"` or `""` would pass validation and create all entries with `projectPath: undefined` (global scope) regardless of intent. **M2 — `looksLikeApiKey()` doesn't trim or reject whitespace** Added `key.trim()` and a `/\s/` check. Environment variables can contain trailing newlines which would pass the length check but fail at the API. **M4 — `/onnxruntime/i` pattern too broad** Removed the overly broad `/onnxruntime/i` pattern from `TRANSIENT_ERROR_PATTERNS`. The three specific patterns that follow it (`Cannot find package`, `LoadLibrary failed`, `Protobuf parsing failed`) already cover the known ONNX init failures without risking silencing real bugs. **M5 — `/Incorrect API key/i` could match unrelated auth errors** Narrowed to `/Incorrect API key provided/i` which is the specific error message format returned by the OpenAI SDK, avoiding false positives from other auth error formats. ## Test Plan - All 1915 tests pass - Typecheck passes across all 4 packages
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
Fix three more Sentry issues and harden the beforeSend filter to suppress additional non-actionable errors.
LOREAI-GATEWAY-1G / 1H — OpenAI API 401 with placeholder key
Root cause:
pickRemoteFallback()treated any truthyOPENAI_API_KEYas valid. Tools like Codex setOPENAI_API_KEY=nokeyas a placeholder when routing through a customOPENAI_BASE_URL. When the local embedding provider fails, the fallback used this placeholder key to make real requests tohttps://api.openai.com/v1/embeddings, producing 401 errors.Fix: Added
looksLikeApiKey()validation — keys shorter than 20 characters are rejected as placeholders, falling through to FTS-only search. Also added/Incorrect API key/ito the beforeSend filter as defense-in-depth.LOREAI-GATEWAY-1A — getSystemErrorMap crash in Sentry SDK on Bun
Root cause: Bun doesn't implement
getSystemErrorMap()fromnode:util. The Sentry SDK calls this during error processing, creating an infinite crash loop (original error → captureException → SDK internal processing → getSystemErrorMap crash → unhandled error).Fix: Added
/getSystemErrorMap/to the beforeSend filter to suppress this Sentry SDK internal failure.Additional beforeSend hardening
Added patterns for ONNX-related init failures that were slipping through on various platforms:
/onnxruntime/i— general ONNX runtime errors/Cannot find package 'onnxruntime-node'/— missing ONNX package (LOREAI-GATEWAY-18)/LoadLibrary failed/— Windows ONNX DLL load failure (LOREAI-GATEWAY-15)/Protobuf parsing failed/— corrupt/incompatible ONNX model file (LOREAI-GATEWAY-10)Test Plan
Closes LOREAI-GATEWAY-1G, LOREAI-GATEWAY-1H, LOREAI-GATEWAY-1A