Skip to content

fix(desktop): 抑制可重试 5xx 与非当前前台会话的供应商上游错误全局 toast - #15926

Open
dashhuang wants to merge 2 commits into
mainfrom
cindy/dazzling-thompson
Open

dashhuang wants to merge 2 commits into
mainfrom
cindy/dazzling-thompson

Conversation

@dashhuang

@dashhuang dashhuang commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

这次改了什么

摘要

修复自定义(含组织下发 BYOK)供应商遇到上游 5xx 或后台任务出错时,向用户当前正在看的窗口误弹全局 toast 的问题:

  1. 抑制可重试 5xx 全局 toast:代理层捕获到可重试 5xx(500/502/503/504 UPSTREAM_ERROR 与 529 RATE_LIMITED)时,CLI/proxy 会自动退避重试;若最终耗尽重试,失败会正常落到对应会话内的 ErrorBanner,不再每次单次抖动都弹全局 warning toast。
  2. 仅对当前可见窗口的前台会话弹 toast:
    • Main 侧在 ProviderUpstreamErrorEvent 中补齐 sessionId(覆盖 Claude Code 代理、Codex HTTP 代理及 Codex Chat / Anthropic 桥接路径),保持 resolveUserSessionId 对 observeHealth / observeVerifiedResponse 的原有语义不变。
    • Main 侧 30s 节流改为按 (sessionId, providerId, code, 5xx/non5xx) 有界分桶隔离(上限 256 条,过期清理 + 最旧淘汰),避免后台会话或被 renderer 静默的 529 占掉节流槽而饿死前台会话随后的 401 / 429 提示。
    • Renderer 侧在 CCAgentSessionView 挂载且 viewVisible 为真时通过引用计数登记当前窗口的前台会话;handleProviderUpstreamError 仅在「非可重试 5xx + 归属当前窗口前台可见会话 + 窗口对用户可见(document.visibilityState === 'visible' && !isWindowHidden())」时弹 toast,后台 Worker、定时任务、未打开会话、折叠 rail/侧栏或隐藏窗口一律静默。

变更类型

  • feat 新功能
  • fix 缺陷修复
  • refactor / perf 重构或性能优化
  • docs / test / chore 文档、测试或工程维护
  • 其他:

范围

  • 关联 Issue / 需求:上游过载触发网关 504 时,后台 Worker 持续向用户当前前台会话弹出「供应商请求失败:上游服务错误(5xx)」全局 toast。
  • 本 PR 包含:
    • apps/desktop/src/shared/providerErrors.ts:新增共享 isRetryableUpstream5xx 判定。
    • apps/desktop/src/main/maker-host/provider-upstream-error-observer.ts、anthropicCompatProxyHostCore.ts、codex-proxy-host.ts、maker-ipc/channels.ts:事件透传 sessionId,按会话与 5xx/non5xx 分桶有界节流。
    • apps/desktop/src/renderer/lib/providerUpstreamErrorToast.ts、CCAgentSessionView.tsx、vite-env.d.ts:前景可见会话引用计数登记、窗口可见性与可重试 5xx 过滤。
    • providerUpstreamErrorObserver.test.ts、providerUpstreamErrorToast.test.ts:补充回归单测。
  • 明确不包含:不改 observeHealth 对 401/402/429/529 的账号健康度捕获,不改 ErrorBanner 会话内最终失败展示,不改服务端网关超时策略。
  • 用户可见变化:上游瞬时 5xx 与后台/隐藏会话的自定义供应商错误不再弹出全局 toast 干扰当前工作;当前前台可见会话遇到不可重试配置/鉴权/额度错误或 429 限流时照常提示。
  • 是否存在 breaking change:无

UI 变化

不涉及(未修改组件布局、样式或文案)。

  • 引用的设计规范:不涉及:仅调整供应商上游错误 toast 的触发过滤条件与前台会话归属,未改动视觉样式、组件布局或提示文案。

怎么验证的

自动验证

pnpm --dir apps/desktop exec vitest run \
  src/main/maker-host/__tests__/providerUpstreamErrorObserver.test.ts \
  src/renderer/lib/providerUpstreamErrorToast.test.ts \
  src/main/maker-host/__tests__/providerDiagnostics.test.ts \
  src/main/maker-host/__tests__/codexProxyHost.test.ts
结果:4 passed (681 tests),其中 providerUpstreamErrorObserver.test.ts (19 tests, ~8ms)、providerUpstreamErrorToast.test.ts (7 tests, ~3ms)

pnpm --filter desktop typecheck
结果:PASS(tsconfig.main.json + tsconfig.main-tests.json + tsconfig.renderer.json,0 errors)

pnpm --filter @cindy/host typecheck && pnpm --filter @cindy/host exec node --import ./src/loaders/test-region.mjs --import tsx --import ./src/loaders/register-raw.mjs --test src/agent/claude-proxy-runtime.test.mjs
结果:PASS(8/8 passed)

pnpm check:engineering-guards && pnpm check:test-hygiene
结果:PASS(12 strict rules 0 violations,472 test files within baseline)

pnpm --dir apps/desktop exec eslint \
  src/shared/providerErrors.ts \
  src/main/maker-host/provider-upstream-error-observer.ts \
  src/main/maker-host/__tests__/providerUpstreamErrorObserver.test.ts \
  src/main/maker-ipc/channels.ts \
  src/renderer/lib/providerUpstreamErrorToast.ts \
  src/renderer/lib/providerUpstreamErrorToast.test.ts
结果:PASS(0 errors)

新增/改写测试价值说明(engineering-conventions.md §3.2):

  • apps/desktop/src/main/maker-host/__tests__/providerUpstreamErrorObserver.test.ts(unit,单跑 ~8ms):经真实 createProviderUpstreamErrorObserver 与 reportProviderUpstreamError 入口 + 注入时钟验证;补齐「后台会话错误不压制前台会话」「resolveSessionId 与 resolveUserSessionId 分离归属」「529 不占 429 节流槽」三类缺口(最小失败输入:30s 内同 provider 先后触发 bg-session/fg-session 401 或同会话先 529 后 429)。
  • apps/desktop/src/renderer/lib/providerUpstreamErrorToast.test.ts(unit / jsdom,单跑 ~3ms):经真实 handleProviderUpstreamError、registerForegroundSession、installProviderUpstreamErrorToastListener 与 hiddenAnimationGate 入口验证;补齐「可重试 5xx(500/502/503/504/529)静默」「非前台/无 sessionId 静默」「429 与非可重试 500 保留」「重叠挂载引用计数与幂等注销」「visibilityState 与主进程隐藏闸门静默」五类缺口(最小失败输入:前台登记 fg-session 后投递 bg-worker 401 或 fg-session 504)。

通知与读写开销核查:

  • 新增逻辑纯内存操作,每次上游错误事件 0 次磁盘/DB 读写、0 次额外网络/list 请求;Main 节流表有界(上限 256 条),CCAgentSessionView 卸载或 viewVisible=false 时立即注销引用计数,隐藏窗口经 isWindowHidden() 静默。

手工验证

不涉及(核心过滤、跨进程透传、节流隔离与隐藏闸门已由确定性单测覆盖)。

未执行的验证

  • 未跑整仓全量测试(按仓库规范已跑相关单测、Desktop 全量 typecheck、Host typecheck 与 proxy 回归、工程门禁与测试卫生检查,其余由 CI 保障)。

风险

风险分类

  • 无已知风险
  • SQLite / migration
  • system prompt
  • 协议兼容
  • 权限 / 安全 / 用户数据
  • 存量插件兼容(批准状态 / 指纹 / manifest 校验 / 安装布局 / 包格式)
  • 插件引擎(ACP)与插件模型影响面(新任务 / 模型选择器 / 权限 / 多端 / 引擎枚举分支)
  • 原生层 / fingerprint / OTA
  • 跨平台差异
  • 其他:

影响与回滚

  • 影响范围:仅影响桌面端自定义(含组织 BYOK)供应商上游错误全局 toast 的触发过滤,以及 PROVIDER_UPSTREAM_ERROR 事件新增可选 sessionId 字段(向后兼容)。
  • 回滚 / 降级方式:直接回退本 PR 即可恢复原全局弹窗行为,无数据或协议迁移依赖。

提交前检查

  • 已 review 完整 diff
  • 每个 commit 都带 DCO 签名(git commit -s,见 DCO)
  • UI 改动已在「UI 变化」注明引用的设计规范章节(不涉及 UI 则跳过)
  • 未提交凭证、令牌或授权文件
  • 已核对受影响的文档,行为变化涉及的旧结论已同步修订(不涉及则无需修改文档)
  • 已确认测试结果或说明未执行原因
  • 涉及数据读取、通知后刷新、界面频繁写入或常驻组件时,已在「怎么验证的」写明:每个用户动作
    和每次通知触发几次 list/扫描/IPC/写事务(读远程数据再写远程调用次数和回包字节),以及隐藏或
    卸载后是否停止(见 docs/dev-rules/engineering-conventions.md §8;远程读取同时见
    docs/dev-rules/server-channel-review.md)
  • 如用户操作、界面入口或报错发生变化,已同步 apps/docs 或说明自动同步会从哪些来源更新。

Assisted-by: Cindy (https://cindy.app)


Note

Low Risk
Changes only error-toast gating and optional event metadata; no auth, persistence, or protocol breaking changes.

Overview
Stops mis-timed global toasts when custom/BYOK provider failures happen in background work or during transient retryable 5xx, while keeping real signals (401, 429, non-retryable errors) for the chat the user is actually viewing.

Main now tags PROVIDER_UPSTREAM_ERROR with optional sessionId (Codex HTTP + bridge paths, Claude compat proxy via resolveSessionId separate from resolveUserSessionId). The 30s broadcast throttle keys on (sessionId, providerId, code) with 5xx / non-5xx buckets and a capped throttle map so a background session—or a renderer-silent 529—does not swallow a later 429 or auth toast for the foreground session.

Renderer adds isRetryableUpstream5xx filtering (no toast for retryable 500–529; session ErrorBanner still owns final failure). registerForegroundSession in CCAgentSessionView (ref-counted, visibility-aware) gates toasts to the current visible window and visible chat shell; hidden document / hidden-window animation gate stays silent.

IPC/channel typings document the new field; observer and toast tests cover session isolation, 529 vs 429 bucketing, and foreground suppression.

Reviewed by Cursor Bugbot for commit 0f9c753. Bugbot is set up for automated code reviews on this repo. Configure here.

…asts

Carry sessionId on PROVIDER_UPSTREAM_ERROR events across Claude Code,
Codex proxy, and Codex bridge paths, isolate main-process throttling by
session and retryable-5xx bucket so background or 529 errors never
starve foreground 4xx alerts, and restrict renderer provider error
toasts to visible foreground sessions while suppressing retryable 5xx
responses that CLI retries automatically.

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
@dashhuang
dashhuang requested a review from a team as a code owner December 5, 2026 23:35
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T00:33:50.936956Z 0f9c753 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Auto-approved by Chris’s trusted-author rule for makecindy/cindy. This is an automated approval.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The PR changes provider-error notifications in the desktop app.

本次修改整体可合并,但工具面板撑满时,隐藏主会话仍弹提示的问题值得补齐。

Findings

  1. P2 隐藏会话仍弹提示 ▶
Fix with agent prompt
### Issue 1
apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx:1177-1180
用户将右侧栏或其他工具面板撑满后,主会话虽然已经看不见,却仍被登记为前台会话。`viewVisible` 默认是 `true`;撑满操作只通过 CSS 隐藏聊天区或将其宽度设为零,没有改变这个值,也没有卸载组件。因此,该会话随后出现的 401、429 等错误仍会弹出全局提示,打扰正在查看其他面板的用户。

建议让登记条件同时跟随聊天区的实际可见状态,并补充撑满、还原的回归测试。

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

本次修改按会话来源和窗口可见性过滤供应商错误提示。

  • Provider error events stay tied to the session that caused them.
  • Only visible foreground sessions can trigger provider error toasts.

本次仅做静态检查,未运行测试或界面验证。

Diagram

%%{init: {'theme': 'neutral', 'htmlLabels': false}}%%
flowchart TD
  A[供应商错误] --> B[解析会话并分类]
  B --> C[按会话和错误类型有界节流]
  C --> D[广播到窗口]
  D --> E{可重试 5xx?}
  E -- 是 --> F[静默]
  E -- 否 --> G{会话已登记且窗口可见?}
  G -- 否 --> F
  G -- 是 --> H[显示全局提示]
Loading

Reviews (1) · Last reviewed commit: "fix(desktop): suppress retryable 5xx and..." · Reviewed by Greptile

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

3 participants