Skip to content

fix(proxy): 协议代理层放行混合模式官方额度锁定,无需开启前端增强 - #2383

Open
ViceEye wants to merge 8 commits into
BigPizzaV3:mainfrom
ViceEye:codex/fix-hybrid-quota-native
Open

ViceEye wants to merge 8 commits into
BigPizzaV3:mainfrom
ViceEye:codex/fix-hybrid-quota-native

Conversation

@ViceEye

@ViceEye ViceEye commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

问题

在未启用增强模式(enhancementsEnabled: false)的独立场景下,启用「官方登录 + 混入 API Key」时,官方账户订阅额度耗尽仍会导致发送功能被阻塞。
此前 PR #2286 使用的源码定位与条件断点方案,在面对官方客户端打包结构和变量名混淆变动时容易因锚点缺失失效;且之前尝试修改 chatgpt_base_url 的协议层反代会破坏官方 OAuth 2.0 鉴权与 Session Cookie 校验。

修复

复用增强模式成熟的 TanStack Query 缓存拦截机制,并在独立模式下建立轻量、受控的生命周期:

  1. 接入查询缓存原型:
    通过 Fiber 发现 Codex 的 QueryClient,拦截用量查询原型的 Query.setData 与 client.setQueryData。在用量数据通知 React 界面之前完成处理,使已有缓存、后续轮询和推流更新均能生效。
  2. 严格限定混合模式放行:
    仅在启用供应商配置且当前配置为 official + officialMixApiKey 时,放行 rate_limit.allowed = true 与 rate_limit.limit_reached = false。百分比、积分、消费上限保持原样,并显式排除 image-generation 查询。
  3. 保留原始数据与官登恢复:
    使用 WeakMap 保存真实服务端数据快照,兼容缓存的结构共享。切回纯官登、关闭供应商配置或设置超时未响应时,立即还原真实锁定状态。
  4. 断线重连与运行期模式切换:
    在 Rust 侧实现 supervise_isolated_api_quota_gate,内置 500ms~5s 指数退避重试。CDP 会话因页面导航或刷新断开后自动探测并重连;运行期切换 Relay Profile 时通过 Runtime binding 脱敏推送,聚焦窗口即可动态生效。
  5. 退出清理与无轮询开销:
    会话销毁时清理在页面注册的脚本;仅在 focus、visibilitychange 及 DOMContentLoaded 时按需获取设置,空闲时完全静默,不消耗任何定时器轮询。

验证

  • 运行 cargo test -p codex-plus-core --test api_quota_gate --test native_api_quota_gate 全部通过。
  • 覆盖已有用量缓存改写、新数据发布拦截、图片查询排除、切回纯官登状态恢复以及重复注入去重等场景。
  • 已基于最新 upstream/main 完成 Rebase,无代码冲突。

@ViceEye
ViceEye marked this pull request as draft October 3, 2026 13:22
@ViceEye
ViceEye force-pushed the codex/fix-hybrid-quota-native branch 3 times, most recently from 45f36fe to f7f3c5e Compare October 4, 2026 12:25
@BigPizzaV3

Copy link
Copy Markdown
Owner

2026-10-08 当前仍Draft且launcher与c07a337d冲突。正文的协议原生方案与实际CDP独立Query缓存注入不符,请更新正文与实际验证,补断线重连、启动后模式切换、退出清理和官登恢复,再rebase评审。当前head构建通过不能证明无注入或长期持续生效。

@ViceEye
ViceEye force-pushed the codex/fix-hybrid-quota-native branch from f7f3c5e to 6eb87d0 Compare October 8, 2026 11:18
@ViceEye
ViceEye force-pushed the codex/fix-hybrid-quota-native branch from 51ba812 to 31bdafd Compare October 8, 2026 13:20
@ViceEye

ViceEye commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

2026-10-08 当前仍Draft且launcher与c07a337d冲突。正文的协议原生方案与实际CDP独立Query缓存注入不符,请更新正文与实际验证,补断线重连、启动后模式切换、退出清理和官登恢复,再rebase评审。当前head构建通过不能证明无注入或长期持续生效。

已按建议进行完整重构并重新 Rebase:

  1. Rebase & 冲突解决:已变基至上游最新的 main(解决 launcher 重构冲突),保持单一干净 Commit;
  2. 更新正文与真实实现对齐:PR 正文已更新为基于 QueryClient 缓存拦截的轻量独立注入方案说明;
  3. 补齐生命周期:
    • 断线重连:Rust 侧实现了 supervise_isolated_api_quota_gate,带有 500ms~5s 退避重连机制,页面刷新或断连后自动恢复;
    • 运行期模式切换:不再局限于冷启动判断,后台常驻守护,运行期切换 profile 时回到窗口自动同步最新策略;
    • 退出清理:会话结束时通过 remove_registered_scripts 清理新文档注册脚本并关闭 session;
    • 官登恢复:切回纯官登或设置超时 5 秒未收到响应时,立即从 WeakMap 还原真实服务端额度数据;
  4. 验证通过:Docker 内 cargo test -p codex-plus-core --test api_quota_gate --test native_api_quota_gate 全绿。

@ViceEye
ViceEye marked this pull request as ready for review October 8, 2026 13:56

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.

2 participants