Skip to content

feat(agent-core-v2): expose session and workspace IDs in Bash tool env - #5053

Merged
wbxl2000 merged 1 commit into
mainfrom
feat/bash-session-workspace-env
Nov 18, 2026
Merged

wbxl2000 merged 1 commit into
mainfrom
feat/bash-session-workspace-env

Conversation

@wbxl2000

Copy link
Copy Markdown
Collaborator

Requirement or Bug

当 Kimi Code 会话内运行的脚本、Hooks 或 Agent Skill(例如通过本地 KAP 服务派生或交接并行会话的 use-kimi-code)在 Bash 工具子进程中执行时,此前子进程环境变量中没有当前会话 ID 与所属工作区 ID。若当前会话位于显式创建的 managed 工作区(wm_*),子进程仅凭 $PWD(cwd)调用 POST /api/v1/sessions 会落入隐式 wd_* 桶而无法归入当前工作区。本 PR 在 Bash 工具子进程环境中注入 KIMI_CODE_SESSION_ID 与 KIMI_CODE_WORKSPACE_ID,让会话内执行的命令和自动化脚本能够直接获知当前会话与所属工作区标识。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

BashTool 已注入 @ISessionContext(携带 sessionId 与 workspaceId),在 BashTool.spawn 构造子进程 noninteractiveEnv 时直接写入这两个只读上下文环境变量:

     const noninteractiveEnv: Record<string, string> = {
       NO_COLOR: '1',
       TERM: 'dumb',
       GIT_TERMINAL_PROMPT: process.env['GIT_TERMINAL_PROMPT'] ?? '0',
       SHELL: env.shellPath,
+      KIMI_CODE_SESSION_ID: this.ctx.sessionId,
+      KIMI_CODE_WORKSPACE_ID: this.ctx.workspaceId,
     };
  • packages/agent-core-v2/src/agent/tools/os/bash/bashTool.ts:向 Bash 工具启动的子进程环境注入 KIMI_CODE_SESSION_ID 与 KIMI_CODE_WORKSPACE_ID。
  • packages/agent-core-v2/test/os/backends/node-local/tools/bash.test.ts:断言子进程 env 包含当前会话与工作区 ID。
  • docs/{zh,en}/reference/tools.md:在 Bash 工具说明中补充这两个子进程环境变量。

Behavior Changes and Affected Users

行为 改动前 改动后 依赖旧行为的人群 退路
Bash 工具子进程环境变量 仅设置 NO_COLOR、TERM、GIT_TERMINAL_PROMPT、SHELL(叠加宿主进程环境) 额外注入 KIMI_CODE_SESSION_ID(当前会话 ID)与 KIMI_CODE_WORKSPACE_ID(当前工作区 ID) 在 TUI、print 模式、Desktop、Web、ACP、SDK 会话中通过 Bash 工具运行的命令与脚本 若外层进程原本设置了同名变量,子进程内将反映当前实际执行该命令的会话与工作区 ID;命令中可按需显式覆盖
  • 受影响模块:packages/agent-core-v2(BashTool)、docs/{zh,en}/reference/tools.md
  • 测试覆盖:packages/agent-core-v2/test/os/backends/node-local/tools/bash.test.ts(77 项测试全部通过)。

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T11:42:54.079533Z 5acfb21 PR opened
ℹ️ 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 18, 2026

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

commit: 5acfb21

@wbxl2000
wbxl2000 merged commit 50ec3c3 into main Nov 18, 2026
17 checks passed
@wbxl2000
wbxl2000 deleted the feat/bash-session-workspace-env branch November 18, 2026 11:54
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Route file download and session media URL calls through a sessionSync
route that answers from the current owner without a session load (MoonshotAI#5336).
Add thoughtsTokenCount to the Google GenAI output usage with thinking and
zero-thinking fixtures (MoonshotAI#5334), update the desktop composer e2e specs for
the label-only slash menu (MoonshotAI#5274), and document the Bash session/workspace
env vars (MoonshotAI#5053).
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