Skip to content

fix(kap-server): apply agent_config and custom metadata when creating a session - #4778

Merged
wbxl2000 merged 12 commits into
mainfrom
fix/session-params-not-ignored
Nov 10, 2026
Merged

wbxl2000 merged 12 commits into
mainfrom
fix/session-params-not-ignored

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

两个会话接口收下参数却不生效,调用方得不到任何提示:

  • POST /api/v1/sessions 的请求 schema 接受 agent_config 和自定义 metadata,但创建时只用 metadata.cwd,其余都被丢掉。desktop 在用户选了模型后会随创建请求带上 agent_config.model,这个字段不生效:desktop 只能在建好会话后再单独发一次 profile 请求补设模型和各模式,并先在本地把模型补到会话上显示。创建响应和 event.session.created 里的会话都没有模型。
  • profile 更新接口的 schema 接受 agent_config 里的 system_prompt、tools、mcp_servers 和顶层的 permission_rules,从来不应用它们,文档里也写着「schema 接受但不应用」。

Bug Reproduction Steps

  1. POST /api/v1/sessions,body 为 { "metadata": { "cwd": "…", "source": "composer" }, "agent_config": { "plan_mode": true } }。
  2. 返回成功,但会话的 metadata 里没有 source,main agent 也没有进入 Plan 模式。

Root Cause

创建路由只读取 metadata.cwd、title、workspace_id 和 environment 字段,没有像 profile 路由那样应用 agent_config,也没有保存自定义 metadata。profile 接口那四个字段则在实现时就没有接到任何能力上。

这是根本修复:能接上现有能力的接上,接不上的从契约里删掉。

Code Changes

 CreateSessionOptions (agent-core-v2)
+  prepare?: (session) => Promise<void>

 SessionLifecycleService.create
   materialize session, create the main agent when needed, enter default plan mode
+  await opts.prepare?.(session)            # still inside the existing cleanup try
   append the session index entry
   announce created                          # SessionStart hooks, lifecycle events

 POST /sessions
   create({
     ...,
+    prepare: save metadata keys except cwd and the reserved goal / parent_session_id / child_session_kind keys
+             apply agent_config when it has a setting to apply (non-empty model, or any key but tower_base)   # same code as the profile route
   })
   set title
-  facts = { busy: false, ... }
+  facts = resolveSessionFacts(...)          # includes the model just applied
   publish event.session.created

 sessionCreateSchema
-  metadata: { cwd, ... }                   # cwd required whenever metadata is present
+  metadata: { cwd?, ... }                  # the route still requires workspace_id or cwd
+  reject tower_mode: true                 # 40001, before anything is created

 KIMI_TO_PROTOCOL
+  model.config_invalid → 40001             # unsupported thinking level, was 50001

 sessionAgentConfigSchema (request + response)
-  system_prompt, tools, mcp_servers
 updateSessionProfileRequestSchema
-  permission_rules
  • 创建时的设置在引擎宣布会话创建之前应用。应用失败时走引擎现有的创建失败清理:会话目录被删掉,不写索引,不触发 SessionStart / SessionEnd hook,也不发出任何会话事件,接口返回对应的错误。
  • applySessionAgentConfig 拆出 applyAgentConfigToSession(session, config),profile 路由和创建路由共用同一份应用逻辑。
  • agent_config 里没有应用步骤会执行的设置时(只有被删掉的字段、空字符串的 model,或没有 tower_mode 的 tower_base),不调用应用逻辑,不会提前创建 main agent。
  • metadata.goal 是目标功能保留的键,读取时本来就会被隐藏,创建时直接丢弃,不写入。parent_session_id、child_session_kind 是会话索引列出子会话用的键,只有子会话路由会写入,创建普通会话时同样丢弃,避免伪造父子关系。
  • 创建请求里的 tower_mode: true 在校验阶段返回 40001,不登记工作区、不建会话。进入 Tower 会建分支、提交未提交的改动并切换工作树,其中有些步骤在可能失败的步骤之前执行,这些仓库改动无法随创建失败回滚。所以 Tower 只能在会话建好后通过 profile 接口开启,code-app 本来就是这么做的;会话创建这个可回滚的阶段因此完全不碰仓库。
  • profile 接口的错误表复用同一组目标相关错误(创建路由已经声明),与它实际可能返回的错误一致。
  • 错误映射表补上 model.config_invalid → 40001:模型不支持请求的思考强度时,profile 接口、提交输入和创建会话原来都返回 50001,现在与模型未配置时的 config.invalid 一样返回 40001。
  • 只带 workspace_id 时,metadata 可以不含 cwd,自定义 metadata 照样保存;目录仍由「workspace_id 与 metadata.cwd 二选一」的校验保证。
  • 创建路由的错误表加上应用 agent_config 可能返回的目标相关错误(40913–40918、40920),OpenAPI 描述里能看到它们。
  • 请求 schema 仍然是非严格的:删掉的字段如果还有人传,会和其他未知字段一样被忽略,只是不再出现在契约和文档里。
  • server-api.md(中英)同步:创建接口的 agent_config 改为会应用,metadata 写明保留键 goal、parent_session_id、child_session_kind,agent_config 写明创建时不能开启 Tower,错误码列表补上 40001 的新情况和目标相关错误;会话对象 agent_config.model 的说明补上创建响应;删掉 profile 接口「schema 接受但不应用」的说明。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
创建会话时的 agent_config 被丢弃 在会话宣布创建之前应用到 main agent 无。desktop 只传用户选的模型,本来就希望它生效;它建好会话后仍会补发一次 profile 请求,重复设置同一个模型不会触发任何变化 不需要
创建会话时 metadata 里 cwd 和保留键以外的键 被丢弃 保存为会话的自定义 metadata(保留键 goal、parent_session_id、child_session_kind 仍被丢弃) 无 不需要
创建时 agent_config 无法应用 不可能发生(被丢弃) 不创建会话、不触发 hook、不发事件,返回对应错误 — 不传 agent_config
创建时 SessionStart hook 的时机 会话准备好后立即触发 先应用创建设置,再触发 无:hook 运行时会话已应用创建设置;hook 输入里的 model 仍是默认模型(存量行为,未改) —
event.session.created 和创建响应里的 agent_config.model 总是 "" 应用了模型时为该模型 无 —
创建时 tower_mode: true 整体被丢弃,创建成功 返回 40001,不创建会话 无:code-app 在会话建好后用 profile 接口开启 Tower 会话建好后用 profile 接口开启(changeset 已写明)
创建时只带 workspace_id、metadata 不含 cwd 返回 40001 创建成功,自定义 metadata 被保存 无 —
请求的思考强度不被模型支持(profile、提交输入、创建会话) 50001 40001 无:同一请求仍然失败,只是返回码改为客户端错误 —
profile 接口的 system_prompt / tools / mcp_servers / permission_rules 收下但不生效 从契约和文档中删除;带着它们的请求照样成功,字段照样不生效 无:没有任何效果,code-app 也不发送 不需要:运行时行为不变

契约:agent-core-v2 的 sessionLegacy/sessionProtocol.ts(会话请求、响应 schema)、CreateSessionOptions(新增可选的 prepare),以及 kap-server 的创建请求 schema(带 workspace_id 时 metadata.cwd 可省略;拒绝 tower_mode: true)。消费者是 code-app 的 desktop / web,它们不发送、也不读取被删掉的字段。apiSurface 快照没有变化。

测试(test/sessions.test.ts):

  • 创建时应用 agent_config 并保存自定义 metadata。
  • agent_config 无法应用时,不留下会话,也不发出任何 event.session.* 事件。
  • agent_config 只有空字符串的 model 或单独的 tower_base 时,不创建 main agent。
  • 带着已删除字段的 profile 请求仍然成功。
  • 只带 workspace_id 创建时保存自定义 metadata。
  • 创建普通会话时丢弃子会话关联的键,不会出现在父会话的子会话列表里。
  • 创建时传未知模型返回 40001,不留下会话;model.config_invalid 映射为 40001(test/transport-errors.test.ts)。
  • 创建时开启 tower_mode 返回 40001,不留下会话。

「不创建 main agent」和「带着已删除字段的 profile 请求仍然成功」两个用例钉住的是本 PR 不能改变的行为,在改动前的代码上本来就通过;其余用例在改动前的代码上都会失败。

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: a120646

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

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

commit: a120646

@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: ac1bbd582e

ℹ️ 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 thread docs/en/reference/server-api.md Resolved
@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-10T11:11:23.496134Z a120646 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.

@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: 9b5c914b2d

ℹ️ 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 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: 714e52232d

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

…ession

The profile route already treats an empty model as absent, but session
creation counted it as a setting and created the main agent eagerly, so
a no-op request could fail creation. Creation now drops the empty model
before deciding whether to apply the config.

Also pin that profile updates still carrying the fields removed from the
schema keep succeeding.

@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: af5d146084

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

…pply

Session creation decided whether to apply agent_config by its key count,
so a config holding only values the apply step never acts on, such as a
lone tower_base, still created the main agent eagerly. Apply the config
only when it carries a non-empty model or a setting other than
tower_base, which is only read together with tower_mode.

@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: 699366250e

ℹ️ 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
已 resolve 的主题代表我们的判断取舍与当前代码现状,请勿重复提出(其余约定同首轮)。

@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: 699366250e

ℹ️ 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 thread docs/en/reference/server-api.md Resolved
…done

Entering Tower mode creates a branch, commits pending changes, and
switches the worktree, and a goal setting applied after it could still
fail and remove the new session while those changes stayed behind.
Tower mode and goals are mutually exclusive, so creation now rejects
tower_mode: true combined with goal_objective or goal_control during
validation, before anything is created.

The creation schema also required metadata.cwd whenever metadata was
present, so a request addressing a workspace by workspace_id could not
pass custom metadata without repeating the root. metadata.cwd is now
optional there; the route still requires either workspace_id or cwd.
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review
已 resolve 的主题代表我们的判断取舍与当前代码现状,请勿重复提出(其余约定同首轮)。

@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: 8629cf5a5c

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

…t-ignored

# Conflicts:
#	packages/kap-server/test/sessions.test.ts
Profile updates apply agent_config through the same code as session
creation, so they can fail with the same goal errors, but the route
declared only the Tower mode failure. It now reuses the shared goal
error set, and the creation docs list those codes and the new 40001
case.
Entering Tower mode switches the worktree to a new branch before steps
that can still fail, such as the snapshot commit, and session creation
cannot undo those repository changes when it rolls back. Rejecting only
tower_mode combined with goal settings left that path open. Creation now
rejects tower_mode: true during validation, so it never touches the
repository; clients enable Tower mode through the session profile once
the session exists, as code-app already does.

@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: 737f047d99

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

Session creation now keeps custom metadata, and parent_session_id with
child_session_kind are the keys the session index uses to list a
session's children. An ordinary creation request carrying them made the
new session show up as a child of any existing session. Creation drops
them along with cwd and goal; only the child-session route sets them.

@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: 404aecfb1f

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

… error

The profile service rejects a thinking level the model does not support
with model.config_invalid, which the error table did not map, so profile
updates, prompts, and now session creation answered an invalid client
setting with 50001. It now maps to 40001 like config.invalid, which an
unknown model already raises. A test pins that creation with an unknown
model returns 40001 and leaves no session behind.
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review
已 resolve 的主题代表我们的判断取舍与当前代码现状,请勿重复提出(其余约定同首轮)。

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: a120646065

ℹ️ 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 c87878a into main Nov 10, 2026
17 checks passed
@wbxl2000
wbxl2000 deleted the fix/session-params-not-ignored branch November 10, 2026 11:44
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