Repository navigation
feat(chat): stop a run when the own key fails, and say why - #1940
Merged
Merged
Conversation
…440) AGENT-OWN-KEY PR 2. When the workspace's own AI key fails, every run ends "Something went wrong" and nothing names the key. This adds what the next PRs use to stop runs, pause agents and tell Admins. Nothing is visible yet. - lib/agent/key-errors.ts: classifyProviderError(provider, error) unwraps the AI SDK's RetryError and reads the APICallError's status and body: invalid (401, 403, Google API_KEY_INVALID), no_credit (insufficient_quota, OpenRouter 402, Anthropic's low credit balance or billing_error), model (404, Azure DeploymentNotFound), transient (408/409/429/5xx/529, Google RESOURCE_EXHAUSTED, a dropped connection, a timeout) or request (anything else, and OpenRouter's 403 moderation flag). invalid, no_credit, model and unreadable (a key that can't be decrypted, ours) are the key classes. - lib/chat/key-health.ts: ownKeyFailure reads the active key's failure without decrypting; markKeyFailing starts one episode however many runs fail at once (a conditional update on failing_since) and keeps the latest class; markKeyWorking clears it only when set and resumes the agents paused own_key; resumeOwnKeyAgents for a key that went out of use. - 00440: organization_ai_keys gets checked_at, failing_since, failure_class, failure_status and notified_at, failing_since set exactly while failure_class is. A new key string clears all five in a trigger, so a key replaced in Settings starts unchecked whatever the route sends, and the web may deploy ahead of the migration. chat_agents.paused_reason and chat_agent_runs.stop_reason gain own_key. schema.sql and the Supabase types are regenerated. Agents resumed by the system aren't logged, as the cap resume isn't. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g it to the AI limit AGENT-OWN-KEY PR 7 (§3.1 point 2, §13 item 1). When the workspace's active own key couldn't be decrypted (the master key rotated without a re-save, a malformed row), resolveAgentModelConfig answered null and Chat silently ran on Ansvisor's key and the workspace's AI budget, while chatRunsOnOwnKey still said "own". A workspace that keeps its data with its own provider would have had it sent through Ansvisor's key. - lib/agent/key.ts: readActiveKey tells none saved, unreadable (with its provider) and ready apart. resolveAgentModelConfig keeps answering null for both, so today's Agent Chat still asks for a key. - lib/chat/key.ts: resolveChatModelKey throws OwnKeyUnreadableError on an unreadable own key, never Ansvisor's key, and marks the key failing unreadable (key-health, best effort). The run ends in error; PR 3 gives it its own words. chatRunsOnOwnKey already reads it as own. - Agents: aiLimitReached reads an unreadable key as the workspace's own (no AI limit), and the agent form still says the agent runs on that provider's key. - Help "Use your own AI keys": a key the server can no longer read stops Chat; save it again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
AGENT-OWN-KEY PR 3 (§5.1, §5.2). A revoked key, an account out of credit or
a model the key can't reach ended every run "Something went wrong", the
reading model's failure was tried again on the same key, and a triggered
agent kept firing until 3 failed runs in a row paused it.
- Engine: a failed model call on the workspace's own key is sorted by
classifyProviderError (engine-key.ts stepFailure). A key class ends the
run at once with code own_key: no read→write second call, and a Help
answer stops too. The key is marked failing (one episode) and the run's
progress keeps keyFailure. The words name the provider and the class
("Anthropic turned down your workspace's key." / "…out of credit." /
"…can't find the model {model}…" / "…can't be read any more.") plus "So
Chat stopped here. What's done so far is kept."
- A saved key that can't be read (PR 7) ends the run the same way before
any call.
- A provider that didn't answer after the SDK's retries ends provider_down:
"{provider} didn't answer, so Chat stopped here… Try again in a moment."
The key isn't marked.
- Ansvisor's own key failing keeps today's path: it isn't the workspace's
to fix.
- ChatModels carries the key's provider and its health at the run's start;
the first answered call on a key marked failing marks it working, which
resumes the agents it paused (key-health).
- The run's error box: Admins get Open AI Providers, others read "An Admin
can fix it in Settings › AI Providers." (run.error code own_key,
kept on reload).
- Agents: an error run with keyFailure is stopped own_key ("Stopped · AI
key not working"), not failed, so it doesn't count toward 3 failed runs in
a row. A triggered agent pauses own_key ("Your AI key isn't working"); a
person's Run now or Run agent doesn't pause it.
- Help: chat.md "When your AI key fails"; agents.md "When your AI key fails".
Co-Authored-By: Claude Opus 5.5 <[email protected]>
8 of 13 tasks
Collaborator
Author
|
Merged current main after #1928 was merged, so the diff now shows only this PR's own changes. Checks after the merge: |
This was referenced Nov 1, 2026
Merged
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
AGENT-OWN-KEY, PR 3: stop a run when the own key fails, and say why. Stacked on #1939 (#1928 is merged; current main is merged in). Merge #1939 first. No migration.
Before, a key that had been refused, had run out of credit or couldn't reach the model ended every run with "Something went wrong". A triggered agent kept firing until 3 runs in a row had failed.
What changes
engine-key.tsstepFailure, with PR 2'sclassifyProviderError):error: 'own_key', the key is marked, andprogress.keyFailureis kept;provider_down("{provider} didn't answer … Try again in a moment."), and the key isn't marked.ChatModelscarriesproviderandkeyFailing.markKeyWorkingand resumesown_keyagents.run.errorgainscode: own_keyandkeyFailure.keyFailedon the thread).agentStateOfturnserror+keyFailureintostopped/own_key("Stopped · AI key not working").own_keyat once ("Your AI key isn't working"). Run now doesn't pause it.fail_count.AGENT_PAUSED_REASONSgainsown_key.chat.mdandagents.md.Related issue
None. AGENT-OWN-KEY spec §5.2 (owner decisions 2026-11-01).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Unit tests:
engine.test163/163, with 5 new cases;engine-model;engine-key,agent-driver,run-answer,run-state,thread,chat-thread-view,agent-rulesandevents;The error box's line and link, and the agent's reason, are tested for an Admin and a Manager. Making a key fail on dev needs a real own key, so it wasn't done.
Checklist
yarn lintpasses (0 errors; 7 old warnings in files this PR doesn't touch)yarn typecheckpassesyarn format:checkpasseshelp/(or no user-visible change)Full checks on this branch (5f627af): web 806 files / 8405 tests pass, and
yarn buildpasses. Serverformat:checkand lint are clean, and 352 files / 4994 tests pass (3 skipped).🤖 Generated with Claude Code