Visitar URL original
feat(webhooks): per-account connection.auth_failed event when an account's login is revoked · Issue #21130 · cheaper-inference/OmniRoute · GitHub
Skip to content

feat(webhooks): per-account connection.auth_failed event when an account's login is revoked #21130

Backlog

Description

@xiaoyaner0201

TL;DR: When one account in a provider pool gets its login revoked, OmniRoute quietly switches to another account and the client still gets a 200, so the operator has no signal that the account is dead. This proposes a per-account webhook event, connection.auth_failed, that names the account. Implementation PR: #21131

Problem / Use Case

We run a pool of a few hundred Codex subscription accounts behind OmniRoute. Over a few days the pool logged thousands of upstream 401s. Most came from logins that had been revoked upstream (refresh_token_reused, invalid_grant). OmniRoute handled every one of them correctly: it marked the account, moved the request to the next account, and the client got its answer. The cost was that nobody knew. A dead account was only found later, by scanning the database by hand, and until then the pool was smaller than everyone thought.

What the webhook surface sends today:

  • request.failed fires only when the whole request fails after every retry and fallback. Its payload has the combo and the reason, not the account. A request that succeeded on the second account sends nothing.
  • provider.exhausted fires only when every account of a provider is cooling down at the same time. One dead account out of many never gets there.
  • Log export (OTLP) carries status and an error summary, but no connection id or account. It is batched, and a background token refresh that fails without any request is not in it at all.

So nothing answers "which account, when, and what kind of auth failure".

Proposed Solution

connection.auth_failed names the failed account

Add one webhook event, connection.auth_failed, sent through the existing notifyWebhookEvent path (same delivery, retries, HMAC signing, Discord/Slack/Telegram formatting). Two sources feed it:

  1. A request saw an auth failure for this account. In markAccountUnavailable (src/sse/services/auth.ts), after the verdict is persisted: status 401/403 classified as unauthorized, account_deactivated, oauth_invalid_token or forbidden. At most once per account every 10 minutes, the same dedupe pattern provider.exhausted uses. A 401 that the reactive refresh fixes before the retry never reaches markAccountUnavailable, so a request that recovers and returns 200 sends nothing.
  2. The account now needs re-authentication. The moment a connection's testStatus moves to expired. This state is written from several places (the reactive refresh in the streaming path, the token health check, the quota sync, the connection test, markAccountUnavailable), so the check lives in one shared place, updateProviderConnection in src/lib/db/providers.ts, which compares the row before and after the write. It fires once per episode and is never deduped. A retired provider (provider_deprecated) is not an auth failure and is skipped. The proactive refresh, which on purpose never marks an account expired, stays untouched.

Payload (no tokens, no refresh tokens, no upstream response bodies):

{
  "provider": "codex",
  "connectionId": "conn-a",
  "account": "ali*******@*******com",
  "stage": "request",
  "status": 401,
  "code": "refresh_token_reused",
  "needsReauth": true
}
  • account: the connection's display name, or its masked email (pickMaskedDisplayValue, already used in the token health check logs).
  • stage: "request" when a live request reached the verdict, "refresh" for background refreshes and health checks.
  • code: a short machine code only (an upstream error code or OmniRoute's own error class). Free text is dropped.

Alternatives Considered

  • Put the connection id on request.failed. That still only fires when the whole request fails. The case here is that the request succeeds on another account.
  • Add the connection id to the OTLP log export. Useful, but batched and request-only: a failed background refresh never appears, and alerting on logs needs a separate pipeline.
  • One hook at each place that writes expired. Several call sites today, and the next one would be missed. The before/after check in the shared write covers them all.

Acceptance Criteria

  • connection.auth_failed appears in WEBHOOK_EVENT_VALUES, EVENT_DESCRIPTIONS, the dashboard event checklist and docs/frameworks/WEBHOOKS.md.
  • A 401 whose refresh succeeds and whose retry returns 200 sends no event.
  • A 401 whose refresh token is rejected sends exactly one event with needsReauth: true.
  • A request-side 401 that the router rotates away from sends one event per account per 10 minutes.
  • 429s, 5xx, and a 401 that names an unsupported model send nothing.
  • The payload never contains tokens, refresh tokens, raw emails or upstream response bodies.

Area

Proxy / Routing, OAuth / Authentication

Related Provider(s)

Any OAuth or API-key provider with a multi-account pool. Seen most on Codex.

Additional Context

Neighbouring work: #14755 added proxy.set_aside / proxy.pool.exhausted (pool members, not accounts), and #19566 added provider.exhausted (whole provider). This fills the per-account gap between them.

I know features from 3.8.55 on are held with the v4-feature label; the PR targets the active release branch as CONTRIBUTING asks, and is fine to wait for develop.

Expected Test Plan

  • A new unit test drives the real request path (handleChat → markAccountUnavailable) and the real connection write (updateProviderConnection) with an offline upstream, and captures what reaches the webhook sink.
  • Update the event catalogue tests (webhooks-ghost-events, characterization-webhooks).
  • Re-run the neighbouring webhook, 401-handling and token-refresh suites.
中文说明

一句话:号池里某个号的登录被上游作废时,OmniRoute 会悄悄换下一个号,客户端照样拿到 200,运营方完全不知道这个号已经坏了。这个提案加一个按账号的 Webhook 事件 connection.auth_failed,直接点名是哪个号。实现 PR:#21131

connection.auth_failed 点名坏掉的账号

现象:我们在 OmniRoute 后面跑着几百个 Codex 订阅号。几天里号池积累了几千次上游 401,大多是登录被上游作废(refresh_token_reused、invalid_grant)。OmniRoute 每次都处理对了:标记这个号,把请求转给下一个号,客户端拿到了结果。代价是没人知道。坏号要等事后人工扫库才发现,在那之前号池实际比大家以为的小。

现有的 Webhook 为什么不够:

  • request.failed 只在整个请求重试、降级都失败后才发,payload 只有 combo 和原因,不带账号。换号成功的请求什么都不发。
  • provider.exhausted 要一个 provider 的所有号同时冷却才发,几百个里坏一个永远到不了。
  • 日志导出(OTLP)有状态码和错误摘要,但没有号 id,而且是定时批量;没有请求的后台刷新失败根本不在里面。

改法:新增一个事件 connection.auth_failed,走现有的 notifyWebhookEvent(投递、重试、签名、Discord/Slack/Telegram 格式都复用)。两个来源:

  1. 请求遇到这个号的鉴权失败:在 markAccountUnavailable 落库之后,401/403 且分类是 unauthorized / account_deactivated / oauth_invalid_token / forbidden 时报一次。同一个号 10 分钟最多一次,和 provider.exhausted 的去重写法一样。当场刷新成功、重试拿到 200 的那次根本走不到这里,所以不会报。
  2. 这个号需要重新登录:号的 testStatus 变成 expired 的那一刻报一次。写 expired 的地方有好几处,所以检查放在统一的写入口 updateProviderConnection,比较写入前后的状态。每次只报一次,不去重。provider 下线(provider_deprecated)不算鉴权失败,跳过。提前刷新(proactive)故意不标 expired,这一点不动。

payload 不带 token、refresh token,也不带上游原始报文。account 是号的显示名,没有显示名就用脱敏后的邮箱;code 只留短的机器码,自由文本丢掉。

为什么不用其他做法:给 request.failed 加号 id 没用,因为我们的场景是请求换号后成功了;给 OTLP 加号 id 有用但是批量的,后台刷新失败也看不到;在每处写 expired 的地方各埋一个点,下一个新写入点就会漏。

验收:刷新成功后 200 的那次不发;refresh token 被拒发一次 needsReauth: true;请求侧 401 换号同号 10 分钟一次;429、5xx、"模型不支持"的 401 都不发;payload 里没有 token、原始邮箱和上游报文。

另外:知道 3.8.55 起新功能 PR 会打 v4-feature 标签暂缓,PR 按 CONTRIBUTING 指向当前 release 分支,等 develop 开了再合也没问题。

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions