Skip to content

fix(kap-server): map auth and Tower errors to their codes and stop sending stack traces - #4777

Merged
wbxl2000 merged 3 commits into
mainfrom
fix/error-envelope-cleanup
Nov 10, 2026
Merged

wbxl2000 merged 3 commits into
mainfrom
fix/error-envelope-cleanup

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

HTTP 错误响应有三个问题:

  1. 每个错误信封都带着服务端的调用栈 stack,生产环境也是。Remote Control 和局域网客户端因此能看到本机的文件路径,而没有任何客户端读这个字段。
  2. 引擎里几类明确的可预期错误没有映射,统一落成 50001(服务端内部错误),还按服务端故障记 error 日志:
    • 未配置供应商、凭证缺失、模型无法解析(auth.*):文档和客户端约定的是 40110 / 40111 / 40113。客户端的登录与模型门禁就是按这几个码弹出对应引导的。
    • 无法进入 Tower 模式(session.tower_mode_invalid)。
  3. 40002(REQUEST_MALFORMED)和 40416(TOOL_CALL_NOT_FOUND)定义了,但服务端从未返回。

Bug Reproduction Steps

  1. 在没有配置任何供应商的环境里打开侧边对话(POST /api/v1/sessions/{id}:btw):返回 50001,错误信封里带着完整调用栈。
  2. 在 Tower 实验未开启时,通过 profile 接口打开 tower_mode:返回 50001,服务端日志里出现一条 session request failed 的 error。

Root Cause

  • errEnvelope 把 stack 作为信封字段,四十来个调用点都把 err.stack 传了进去。
  • kap-server 的 KIMI_TO_PROTOCOL 映射表里没有 auth.* 和 session.tower_mode_invalid,未映射的错误按兜底规则变成 INTERNAL_ERROR。
  • 这是根本修复。

Code Changes

 Envelope
-  stack?: string
 errEnvelope(code, msg, requestId,
-            stack?,
             details?)

 mapError                       # public HTTP / WS errors: no stack
+mapDebugError = mapError + stack   # /api/v1/debug RPC and snapshots only

 KIMI_TO_PROTOCOL
+  session.tower_mode_invalid → 40935 TOWER_MODE_INVALID (new)
+  auth.provisioning_required → 40110
+  auth.token_missing         → 40111  (details.provider_id kept)
+  auth.model_not_resolved    → 40113  (details.model_id / provider_id kept)

 ErrorCode
-  REQUEST_MALFORMED 40002
-  TOOL_CALL_NOT_FOUND 40416
+  TOWER_MODE_INVALID 40935
  • debug RPC(--debug-endpoints,只绑 loopback、需要 bearer)仍然返回 stack,开发排查不受影响。
  • 认证错误的 details 加入公开名单,客户端按 provider_id 区分「登录」还是「配置供应商」,按 model_id 区分「选模型」。
  • 40112(凭证被拒绝)引擎里没有抛出点,这次不加映射。auth.login_required 没有文档约定的码,保持现状。
  • server-api.md(中英)的错误码分段表补上 40935;profile 路由的错误表声明 TOWER_MODE_INVALID,OpenAPI 描述里也有这个响应。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
所有 HTTP / WS 错误信封的 stack 带服务端调用栈 不带 无:desktop、web、VS Code、klient 都不读这个字段 debug RPC 仍带 stack
未配置供应商 / 凭证缺失 / 模型无法解析 50001,无 details 40110 / 40111 / 40113,带 details 无:客户端的门禁本来就按这几个码处理,50001 只会弹通用错误 不需要
无法进入 Tower 模式 50001,记 error 日志 40935,msg 不变 无:客户端不按 50001 区分处理,提示文字照常显示 不需要
40002、40416 已定义,从未返回 删除 无 —

契约文件:protocol/envelope.ts(去掉 stack)与 protocol/error-codes.ts。消费者是 code-app 的 desktop / web:不读 stack,认证码和 details 正是它的 chatGate 期望的形状。apiSurface 快照没有变化。

测试:

  • test/transport-errors.test.ts:新增 Tower 与三种认证错误的映射、认证错误保留 details、公开信封不带 stack 而 debug 信封带、兜底错误不带 stack。
  • test/sessions.test.ts:Tower 不可用时由 50001 改为断言 40935。
  • test/rpc.test.ts 的「debug RPC 返回调用栈」保持通过。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 799b708

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 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-11-10T08:38:03.502936Z 799b708 Manual request
ℹ️ 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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@799b708
npx https://pkg.pr.new/@moonshot-ai/kimi-code@799b708

commit: 799b708

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1077292ff2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review
评审约定:

  1. 只审本 PR 相对 base 的 diff;base 存量问题请单列为 note,不作阻塞意见。
  2. 同类问题请一次性列出 diff 内全部位置,不逐轮分批提出。
  3. 纯风格/个人偏好意见不发;每条意见标注严重度(P0 阻断 / P1 应修 / P2 建议)。
  4. 已 resolve 的主题代表我们的判断取舍与当前代码现状,请勿重复提出。

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 799b708508

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@wbxl2000
wbxl2000 merged commit b9c1897 into main Nov 10, 2026
28 of 29 checks passed
@wbxl2000
wbxl2000 deleted the fix/error-envelope-cleanup branch November 10, 2026 10:05
This was referenced Nov 11, 2026
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