Skip to content

fix(kap-server): bind the main agent when a session is created - #4839

Merged
wbxl2000 merged 4 commits into
release/2026-11-12from
fix/kap-bind-model-on-create
Nov 11, 2026
Merged

wbxl2000 merged 4 commits into
release/2026-11-12from
fix/kap-bind-model-on-create

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

通过服务端 API 使用会话时,如果先设置思考强度、后设置模型,思考强度会在第一次绑定模型时被丢掉。这个 PR 让会话在创建时就确定 Agent profile、模型和思考强度,所有设置都针对一个确定的模型生效。

Bug Reproduction Steps

  1. 配置一个支持 low / high 的默认模型。
  2. POST /api/v1/sessions,agent_config 只带 { "thinking": "high" }(或创建后先用 profile 接口只设 thinking: "high")。
  3. 再用 profile 接口或提交输入设置模型。
  4. 主 agent 的 AgentState 中 thinking 不是 high,而是模型的初始强度。

Root Cause

服务端创建会话时不绑定主 agent。没有绑定时,setThinking 只把强度存在未绑定的状态里;之后第一次设置模型会走 bindProfile,而绑定只沿用「已绑定」状态里的强度,未绑定时存下的值被忽略。

这是根本修复:会话创建时就完成绑定,之后不存在「未绑定时设置思考强度」的路径。只有服务端完全没有可用模型时会话才保持未绑定,这时设置思考强度直接返回 40113,不再悄悄丢失。

Code Changes

创建会话的流程:

 POST /sessions
+  model = agent_config.model ?? 当前默认模型(已配置且能在模型目录解析时)
+  没有 model 时:agent_config 带了 profile 或 thinking → 40113;否则创建无模型会话
   ISessionManager.create
+    mainAgentBinding = { profile: agent_config.profile ?? "agent", model, thinking, strictThinking }
     prepare
-      model / thinking / 其他设置都在这里逐项应用
+      只应用其余设置(权限、plan、swarm、goal)

其余改动:

  • agent_config 在创建请求里新增 profile,只在创建时生效;Agent profile 绑定后本来就不能更换。
  • 提交输入(含 steer)删除「第一条输入时绑定 profile」的逻辑;请求带 profile 时返回 40001,不静默忽略。
  • profile 接口和提交输入设置 thinking 时,如果主 agent 还没有模型、且同一请求没带 model,返回 40113(applyThinking)。
  • 判断默认模型是否可用时,只看「已配置并能在模型目录解析」,不检查登录凭证:登录过期但已配置模型的用户照常拿到有模型的会话,发消息时再提示登录。
  • profile.unknown 映射为 40001。
  • server-api.md 中英文同步。

从 packages/kap-server/src/routes/sessionAgentConfig.ts 的 creationMainAgentBinding 开始读。

Behavior Changes and Affected Users

行为 之前 之后 谁依赖旧行为 退路
创建会话不带 agent_config.model 不绑定主 agent,第一条输入时才用当时的默认模型 创建时绑定当时的默认模型,创建响应和 AgentState 就带模型 直接调用服务端 API 且期望「发消息时才取默认模型」的客户端;desktop / web 创建时总是带模型,不受影响 创建时传 model,或之后用 profile 接口换模型
创建时只带 thinking 被悄悄存下,绑定模型时丢失 和模型一起绑定,生效 无(旧行为是 bug) 不需要
修改默认模型之后 还没发过消息的会话会在第一条输入时用新默认模型 已创建的会话保持创建时的模型 直接调用服务端 API、创建后才改默认模型的客户端 profile 接口换模型
没有可用模型时创建并带 profile 或 thinking 创建成功,thinking 悄悄存下 40113,不创建会话 无 先配置模型
会话没有模型时只设 thinking(profile 接口 / 提交输入) 悄悄存下,之后丢失 40113 旧版本服务端创建、且从未成功发过消息的无模型会话(desktop 按 profile 失败处理,恢复设置) 同一请求带 model
提交输入的 profile 字段 第一条输入时绑定指定的 Agent profile 返回 40001,提示改用创建时的 agent_config.profile 在第一条输入里选自定义 Agent profile 的服务端 API 客户端;desktop / web 不使用这个字段 创建会话时用 agent_config.profile;不保留旧用法已与维护者确认
创建会话的 agent_config.profile 不存在 指定 Agent profile,未知名称返回 40001 — —
profile 接口的 agent_config.profile 不存在,被忽略 返回 40001,提示只能在创建时指定 无 —
远程环境(environment_id 非 local)创建 创建未绑定的主 agent 同样按上述规则在创建时绑定 远程环境客户端 同第一行
profile.unknown 错误 输入接口外会落成 50001 40001 无 —

desktop / web 的实际流程对比(按 code-app main 的客户端代码核对):创建会话时带 agent_config.model → 立即用 profile 接口写思考强度、权限等 → 提交输入时带 model / thinking / permission_mode,不带 profile。之前的服务端收到 model 时同样在创建时绑定主 agent,所以这三步的结果和之前一致。

测试覆盖(packages/kap-server/test/):

  • sessions.test.ts:
    • 创建时绑定默认模型;
    • 只带 thinking 创建不丢;
    • profile / model / thinking 一起绑定;
    • 未知 profile 返回 40001 且不创建;
    • 创建后用 profile 接口改 profile 返回 40001,profile 不变;
    • 修改默认模型不影响已有会话;
    • desktop 流程(创建带模型 → profile 写思考强度和权限);
    • 没有可用模型时创建无模型会话,profile 接口设 thinking 返回 40113;
    • 没有可用模型时带 profile 或 thinking 创建返回 40113。
  • prompts.test.ts:
    • 删除测输入 profile 绑定的用例,新增「输入带 profile 返回 40001」;
    • 依赖「新会话没有模型」的用例改为在无默认模型的配置下运行,断言不变。

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 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f864272

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 11, 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-11T12:18:04.701180Z f864272 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 11, 2026 •

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

commit: f864272

@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: 06e3b4e8fb

ℹ️ 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

@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: 331f495e69

ℹ️ 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 afadca2 into release/2026-11-12 Nov 11, 2026
14 checks passed
@wbxl2000
wbxl2000 deleted the fix/kap-bind-model-on-create branch November 11, 2026 12:19
@github-actions github-actions Bot mentioned this pull request Nov 11, 2026
Grapedge added a commit that referenced this pull request Nov 12, 2026
* fix(agent-core-v2): truncate oversized skill content (#4834)

* fix(kap-server): bind the main agent when a session is created (#4839)

* fix(kap-server): bind the main agent when a session is created

* fix(kap-server): reject a profile on prompt submissions

* fix(kap-server): reject a profile change through the session profile

* fix(kap-server): wait for model configuration before binding a new session

* fix(agent-core-v2): deduplicate restored task notifications (#4836)

* fix(agent-core-v2): deduplicate restored task notifications

* chore: add notification replay fix changeset

* fix(agent-core-v2): preserve task notifications after prompt-only crashes

* fix(agent-core-v2): complete prompts before ending turns

* test: align lifecycle snapshots with prompt completion order

* fix(agent-core-v2): settle all prompts during turn finalization

* fix(kap-server): drop reserved metadata keys from session profile updates (#4842)

* fix(kap-server): drop reserved metadata keys from session profile updates

* fix(kap-server): keep stored reserved metadata keys on profile updates

* docs: split and rewrite the server API reference (#4841)

* docs: split and rewrite the server API reference

* docs: redirect old server API anchors to the split pages

* docs: describe profile metadata as a replacement

* docs: mark question responses as answered-only

* fix(kap-server): avoid heartbeat timeout after scheduling stalls (#4823)

* fix(kap-server): avoid heartbeat timeout reaping after stalls or clock jumps

* fix(kap-server): grace heartbeat window for short scheduling stalls

* fix(kap-server): treat only clearly late heartbeats as scheduling stalls

* fix(kap-server): time heartbeats on a monotonic clock and drain queued input before reaping

---------

Co-authored-by: Kimi Code (Grok Bot) <[email protected]>

* fix(kap-server): undo a turn only once when requests race (#4846)

* docs: describe heartbeat stall handling (#4849)

* docs: describe heartbeat stall handling

* docs: explain the monotonic clock in the heartbeat notes

* fix(agent-core-v2): acquire session lock before loop restoration (#4848)

* fix(agent-core-v2): acquire session lock before loop restoration

* chore: add session lock lifecycle changeset

* fix(agent-core-v2): hold session lock through loop restoration

- add an operation-scoped lease to the session run gate
- hold it across loop initialization, reset, and pending drains
- cover idle timeout and release ordering

* fix(agent-core-v2): keep the output of stopped tool calls (#4854)

* fix(kap-server): drop the partial reply of a retried step (#4855)

* fix(agent-core-v2): dedupe historical tool schemas (#4862)

* fix(agent-core-v2): dedupe historical tool schemas

* chore: add duplicate tool schema changeset

* fix(agent-core-v2): preserve first tool schema declaration

* fix(agent-core-v2): remind on changed tool schemas

* fix(agent-core-v2): block stale tool schema calls

* fix(agent-core-v2): normalize registry schemas in pending checks

* fix(agent-core-v2): import registry tool metadata type

* fix(agent-core-v2): preserve abort handling during validation

* fix(agent-core-v2): refine validation hook preparation

* fix(agent-core-v2): make validation hooks synchronous

* fix(agent-core-v2): preserve tool result notes on fork (#4867)

* fix(agent-core-v2): preserve tool result notes on fork

* chore: add fork tool result note changeset

* fix(agent-core-v2): scope tool result note restoration to forks

* fix(agent-core-v2): normalize inherited tool result notes

* fix(agent-core-v2): isolate fork note restoration

* feat(tui): sweep running titles toward text and add KIMI_CODE_TUI_ANIMATION_FPS (#4843) (#4870)

* chore: sync web dist from code-app (#4871)

code-app: 8ab17294e031323186d357d121c5360b8353900e

Co-authored-by: kimi-agent-bot <[email protected]>

* ci: release packages (#4835)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: sync web dist from code-app

code-app: 5835391dac3a2b9e0c65ffbd418f0d6042a7adb2

---------

Co-authored-by: Haozhe <[email protected]>
Co-authored-by: qer <[email protected]>
Co-authored-by: Kimi Code (Grok Bot) <[email protected]>
Co-authored-by: grapedge <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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