Repository navigation
fix(mcp): isolate OAuth authorization cancellation per waiter - #4643
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
7Sageer
approved these changes
Nov 3, 2026
Closed
7723qqq
pushed a commit
to 7723qqq/kimi-code
that referenced
this pull request
Nov 4, 2026
Port of upstream MoonshotAI/kimi-code 42cb675 (MoonshotAI#4643), adapted to this fork's authorization flow. Each attach() now gets its own waitId and abort controller, registered in an authorizationWaits map with a cancelAuthorizationWait(waitId) lookup. Cancelling one waiter only aborts that waiter; the shared flow's controller is aborted (and its callback listener closed) when the last attached handle detaches. A joined caller that aborts while another is still completing no longer tears the flow down under it. The per-waiter deadline also stops leaking into the exchange: the callback deadline is created from the waiter's signal, cleared as soon as the callback settles, and the code exchange no longer carries the caller's timeout. Local adaptation: upstream's version hangs the cancellation off the configuration-source registry this fork removed (it uses a credentials coordinator instead), so cancelAuthorizationWait is the only new public entry point; the begin signature passes the flow's AbortController instead of its signal so the flow can abort with a reason. The auth tool's URL update and the kap-server wire schema carry the new waitId, and the TUI parser narrows its return type to the two fields it reads. Verified: MCP suites 378 pass (mcpCore, agent/mcp, app/mcpManagement), oauth service 42, callback-server 11, TUI mcp-oauth 5; engine/app/kap-server typechecks clean; oxlint 0 errors, check:no-comments green. Reverse check: dropping the attachedHandles guard reddens 'keeps a joined completion alive when the first completing caller aborts', restoring it green. Two lifecycle cases (joins concurrent begins, detaches a joined caller) failed intermittently with ECONNREFUSED to the local fake auth server; the same failure reproduces with service.ts reverted, so it is an existing environment flake, not this change.
7723qqq
pushed a commit
to 7723qqq/kimi-code
that referenced
this pull request
Nov 4, 2026
This was referenced Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement or Bug
同一 MCP 服务器的多个授权调用会共用浏览器回调和 token exchange。当前第一个调用的 signal 和超时直接控制共享流程,取消它可能同时中断其他仍在等待的调用;宿主也无法通过工具更新中的标识,只取消选中的一次授权等待。
本次为每个等待提供独立取消入口,让对应工具返回取消结果,其他等待可以继续完成。这里提供 Core 能力,宿主授权卡按钮与接线由消费方单独完成。
Bug Reproduction Steps
beginAuthorization(),两者开始complete()。Root Cause
startCompletion()复用同一个 Promise,却把首个调用者的 signal 和 timeout 交给共享回调与 token exchange。原有cancel()只释放句柄,没有独立终止该句柄正在进行的等待。Code Changes
共享授权继续由原 OAuth service 负责;每个调用者独立等待其结果,并分别处理取消、signal 和回调超时。
oauth/service.ts:返回waitId,增加cancelAuthorizationWait(waitId);完成或取消后清理标识,迟到取消幂等。取消等待不撤销已有 token。mcp/tools/auth.ts:在现有 OAuth URL 更新中附带waitId。取消仍走工具已有错误返回,不中止整轮对话。waitId仍为必填。Behavior Changes and Affected Users
cancel()仅释放句柄,无法独立结束其等待BeginAuthorizationResult的调用方waitId和对应 service 方法timeoutMs的作用范围聚焦验证通过:取消首个工具等待后,第二个等待仍能授权成功;工具取消不设置
stopTurn,不 abort 原 turn signal、不 reconnect;迟到取消不撤销 token。未执行真实账号授权或宿主 UI 联调。验证详情
在
packages/agent-core-v2执行:pnpm exec vitest run --config vitest.config.ts test/mcpCore/oauth/service.test.ts test/agent/mcp/tools/auth.test.ts pnpm typecheckOAuth service/auth 53 项测试通过;callback-server 10 项与 TUI OAuth 5 项测试通过。Core package 和 CLI 类型检查通过;改动文件 lint 无错误,Core 有 9 条既有警告;comment-free 与
git diff --check通过。CLI 类型检查命令(仓库根目录):
pnpm exec tsc -p apps/kimi-code/tsconfig.json --noEmit。原有取消断言按单等待者取消结果更新,callback 关闭与资源释放测试保留。两个其他既有测试文件仅补齐 3 个 mock 的
waitId字段。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.No CLI command or configuration documentation changes are required. No related issue was provided.