Skip to content

fix(chat): a saved key that can't be read stops Chat instead of moving it to the AI limit - #1939

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-unreadable-key-stops
Nov 1, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-unreadable-key-stops

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

AGENT-OWN-KEY, PR 7: a saved key that can't be read stops Chat instead of moving it to the AI limit. #1928 (key health) is merged, and this branch has current main merged in. No migration.

This was a quiet cost and data leak (spec §3.1). When the workspace's active own key couldn't be decrypted, Chat fell back to Ansvisor's key and the workspace's AI budget. Meanwhile chatRunsOnOwnKey still said "own key".

  • A customer who keeps data with their own provider (e.g. their Azure) would have had it sent to Anthropic.
  • The shared budget would have paid for it.

What changes

  • readActiveKey (lib/agent/key.ts) tells three cases apart: none, unreadable and ready. resolveAgentModelConfig still returns null for none and unreadable, so today's Agent Chat behaves as before.
  • resolveChatModelKey throws OwnKeyUnreadableError on an unreadable own key and marks it unreadable (PR 2's health).
    • It never falls back to Ansvisor's key.
    • It stops even if the marking fails.
  • aiLimitReached counts an unreadable key as an own key, so the AI limit doesn't apply.
  • The agent form keeps saying it runs on the provider's key.
  • Help: ai-providers.md, "Add a key", gets one sentence.

Left to PR 3: the run still ends with "Something went wrong". The key-class words come in PR 3, to keep the engine file out of this PR.

Related issue

None. AGENT-OWN-KEY spec (owner decision D6: no automatic move to Ansvisor's budget).

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

Unit tests across 14 files, 175 tests:

  • key and agent/key;
  • agent-form-setup, agents, agent-runs, agent-action, agent-driver and agent-sweep;
  • engine-model;
  • the AI Providers route, help, subscription-guard coverage and demo-gate routes.

An unreadable key throws and marks it, and is never Ansvisor's key. Reproducing it on dev would mean corrupting a stored envelope, which wasn't done.

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)

Full checks on this branch (8d01832): web 805 files / 8393 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

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

Copy link
Copy Markdown
Collaborator Author

Merged current main after #1928 was merged, so the diff now shows only this PR's own changes. Checks after the merge: typecheck passes on #1939 (bf033c5), #1940 (11c3165) and #1941 (3105778). On #1941's tip, format:check and lint pass (0 errors), and the Chat, agents and help tests pass (132 files / 1,838 tests).

@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 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 a8b4765 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