Skip to content

feat(settings): test an AI key, show its health, and restart its paused agents - #1943

Merged
gkhngyk merged 11 commits into
ansvisor:mainfrom
cihan-gyk:feature/settings-own-key-test
Nov 1, 2026
Merged

gkhngyk merged 11 commits into
ansvisor:mainfrom
cihan-gyk:feature/settings-own-key-test

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AGENT-OWN-KEY, PR 5: test an AI key, show its health, and restart its paused agents, in Settings › AI Providers. Stacked on #1941 (and its parents #1940 and #1939; #1928 is merged). Merge those first. No migration; it uses #1928's health columns (00440).

What changes

  • Status under each key:
    • red "Not working since {date}: {why}";
    • green "Working · checked {time} ago";
    • grey "Not checked yet".
  • Test, beside Replace:
    • It reads "Testing…" while it runs, and the other Test buttons wait.
    • A pass shows a toast: "Your {provider} key works." It adds "{n} paused agents started again." when any were paused own_key.
  • Save and Use for Chat test the key first:
    • invalid, model or unreadable: the key isn't saved. The field says "{provider} turned down this key." or "{provider} can't find the model {model} for this key."
    • no_credit: the key is saved, with a warning: "Saved. Your {provider} account is out of credit, so Chat and agents stop on it until it has credit."
    • transient: the key is saved: "Saved. Couldn't check the key just now."
  • Coming back by itself: a key that works again (on Test, Save, or the first answered call, from PR 3) resumes the agents paused own_key. A sweep is the backstop.
  • Remove dialog: it says what Chat moves to, the most recently saved other key or the plan's monthly AI limit. It also says that agents paused because this key failed start again.
  • Help: help/settings/ai-providers.md.

The design was approved by the owner as a mockup, and the screens below are from this code. At 1024px, the Azure row's "Key saved" badge drops to a second line beside three buttons and the delete icon. It still reads fine, and is left for a follow-up.

Related issue

None. AGENT-OWN-KEY spec §5.2 item 7 and §5.4 (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. On an Enterprise workspace with saved keys, open Settings › AI Providers. Each key shows its status line.
  2. Click Test on a key: "Testing…", then the toast and the status line updated.
  3. Replace a key with a wrong one: the field error, and nothing is saved.
  4. Unit tests:
    • the AI Providers route, with the new health;
    • key-test;
    • subscription-guard coverage and demo-gate routes;
    • help.

Checked in the browser at 1024px, light and dark. The page's own requests were answered in the browser, so no key reached a provider. The screens:

  • Testing…;
  • the toast;
  • the refused key's field error.

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)

CI passed on this branch (46ae1f7), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41332904851): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 10 commits November 1, 2026 12:08
…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]>
AGENT-OWN-KEY PR 4 (§5.2 items 4–6). While the workspace's own AI key was
failing, Run now and Run agent queued runs that then failed, Resume turned
a paused agent back on into the same failure, and nothing outside a run
said the key was the cause.

- runChecks refuses own_key_failed (409) when Chat runs on the workspace's
  own key and it is marked failing (failingOwnKey): Run now and Run agent
  queue nothing, on any brand; Activate, Resume and Take over are refused
  the same way. The words: "{why} An Admin can fix it in Settings › AI
  Providers, then run the agent again."
- The runner ends a queued run before any model call: stopped own_key; a
  triggered agent pauses own_key, a person's run doesn't.
- Agent Center: a banner while the key fails, "Your {provider} key isn't
  working · agents are paused" and why; Admins get Fix in AI Providers,
  others read that an Admin can fix it there. The list's answer carries
  the key's class and provider (keyFailure), no dates.
- The composer, on Chat's pages and in the side panel: one line over the
  box, "{why} Chat stops on it until it's fixed.", with Fix in AI Providers
  for Admins. Sending stays on. Read only where Chat runs on the own key.
- Help: agents.md and chat.md "When your AI key fails".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed agents

Settings › AI Providers gets Test on each saved key and on the Replace form:
one call with no retries, sorted into working, out of credit, refused or
unchecked. What it finds is kept on the key (00440): a failing key starts an
episode, a working one ends it and restarts the agents it paused. The list
shows each key's health. The agent sweep restarts agents paused for the own
key once it works again, or once Chat no longer runs on it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fuse

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/chat/_chat-page-access.ts
#	web/src/components/chat/agents/agents-list.tsx
gkhngyk
gkhngyk previously approved these changes 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

# Conflicts:
#	web/src/lib/agent/key.ts
#	web/src/lib/chat/agent-sweep.test.ts

@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

@gkhngyk
gkhngyk merged commit b7eb4ae into ansvisor:main Nov 1, 2026
5 checks passed
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