Skip to content

feat(chat): sort AI provider errors and keep the own key's health (00440) - #1928

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:feature/chat-own-key-health
Nov 1, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:feature/chat-own-key-health

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AGENT-OWN-KEY, PR 2: sort AI provider errors, and keep the own key's health. Nothing visible yet; PRs 7, 3, 4, 5 and 6 build on it. From main. Migration 00440.

What changes

  • classifyProviderError (web/src/lib/agent/key-errors.ts) sorts a provider failure as:

    • invalid: the key was refused (401/403);
    • no_credit: no credit or quota left;
    • model: a model the key can't reach;
    • transient: 5xx, rate limit or timeout;
    • request: our request.

    Each provider's documented error bodies are in the tests: Anthropic, OpenAI, Google, Azure and OpenRouter. OpenRouter's 403 is a moderation flag, so it is request, not the key. The AI SDK's RetryError wrapper, network errors and timeouts are covered too.

  • key-health.ts:

    • ownKeyFailure;
    • markKeyFailing: one failure episode, with a conditional update, so runs that fail together mark it once;
    • markKeyWorking: writes only when the key is marked, then resumes own_key agents;
    • resumeOwnKeyAgents.
  • 00440:

    • organization_ai_keys gains checked_at, failing_since, failure_class, failure_status and notified_at, with a consistency check (failing_since and failure_class set together).
    • A BEFORE UPDATE OF key_encrypted trigger clears all five when the key string changes. A replaced key starts unchecked whatever the route sends, so it's safe even if the web ships before the migration.
    • chat_agents.paused_reason and chat_agent_runs.stop_reason gain own_key.
    • It ends with notify pgrst. schema.sql and supabase.ts are updated.

Agents that the system resumes by itself get no ledger line, as with today's cap resume.

For @gkhngyk: apply 00440 on prod (applied on dev). It changes constraints on small tables only, with no rewrite.

Related issue

None. AGENT-OWN-KEY spec §6.1 (owner decisions 2026-11-01).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00440. Update a row's key_encrypted: the five health columns clear.
  2. Unit tests:
    • key-errors: each provider's error bodies, the wrapper, network and timeout;
    • key-health: one episode, working clears, resume;
    • subscription-guard coverage and demo-gate routes.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)
  • Migration added (00440) and supabase/schema.sql rebuilt

Full checks on this branch (c96e53a): web 805 files / 8391 tests pass, and yarn build passes. Server format:check and lint are clean, and 352 files / 4994 tests pass (3 skipped).

🤖 Generated with Claude Code

…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]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 1, 2026

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants