Skip to content

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

Open
wbxl2000 wants to merge 2 commits into
mainfrom
fix/error-envelope-cleanup
Open

wbxl2000 wants to merge 2 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. 待确认的 changeset:Stop including server stack traces in error responses, and report missing provider, credential, and model setup with their dedicated error codes.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 4995bca

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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-09T18:00:07.489541Z 4995bca 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.

@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@4995bca
npx https://pkg.pr.new/@moonshot-ai/kimi-code@4995bca

commit: 4995bca

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

Comment on lines +62 to +64
[ErrorCodes.AUTH_PROVISIONING_REQUIRED]: ErrorCode.AUTH_PROVISIONING_REQUIRED,
[ErrorCodes.AUTH_TOKEN_MISSING]: ErrorCode.AUTH_TOKEN_MISSING,
[ErrorCodes.AUTH_MODEL_NOT_RESOLVED]: ErrorCode.AUTH_MODEL_NOT_RESOLVED,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Add a changeset for the user-visible server fixes

These mappings change visible behavior for CLI, web, and remote-control users by replacing generic internal errors with actionable auth codes, while the envelope change removes a previously returned field, yet this commit adds no .changeset entry. Add a patch changeset for @moonshot-ai/kimi-code describing the user-facing fix so it appears in the release notes.

AGENTS.md reference: AGENTS.md:L88-L89

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

changeset 文字需要先确认再提交,建议的文字写在 PR 描述的 Checklist 里,确认后补上。

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.

1 participant