Skip to content

fix: route session status and git data through bound environments - #4698

Merged
7Sageer merged 3 commits into
mainfrom
fix/remote-session-surface-routing
Nov 5, 2026
Merged

7Sageer merged 3 commits into
mainfrom
fix/remote-session-surface-routing

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Follow-up to #4649。远程会话的欢迎面板和 footer 已经能读取实际绑定环境,但 /status、Markdown 导出元数据和 kap-server 的 v2 sessions include=git 仍读取本地启动目录,导致同一 session 在不同表面显示不一致。本 PR 把这三处剩余路径也接到绑定环境。

Bug Reproduction Steps

  1. 用 kimi --environment dev-box --environment-cwd /home/example/project 启动远程会话,本地启动目录与远程目录不同。
  2. 执行 /status 或 /export-md /tmp/session.md,可见 Directory / frontmatter 仍显示本地启动目录。
  3. 对包含远程 session 的列表请求 POST /api/v2/sessions?include=git,其 git.branch / pull_request 来自本地 cwd,无法表示远程仓库。

Root Cause

这三处仍沿用“本地工作区即 session 工作目录”的假设:TUI 直接传 appState.workDir,v2 GitDomainResolver 则只用 cwd 调 App 级 IGitService。此外,SessionSummary 在索引和镜像过程中丢弃了 environmentId / environmentCwd,所以即使要按环境路由,冷会话也没有足够信息。

本 PR 保留原有本地路径作为旧会话的兼容回退,并为远程会话补齐绑定元数据和按环境执行的查询路径,是根本修复。

Code Changes

核心思路是让“展示用工作目录”和“git 查询目标”都由 session 绑定决定,同时不改变本地 workspace 的归属和导出文件保存位置。

 TUI /status, /export-md
-  appState.workDir
+  remote environment.cwd, fallback to appState.workDir

 SessionSummary / index mirror
+  preserve environmentId + environmentCwd across cold reads, fork, archive and metadata writes

 v2 sessions include=git
-  cwd -> App IGitService -> local repository
+  (environmentId, cwd) -> local App IGitService or remote GitService
+  cache and deduplicate by (environmentId, cwd)

远程 GitService 复用环境自己的 fs/process。缺少环境信息的旧会话继续按 local 处理;远程绑定缺少 cwd 或环境不可用时返回 null git 域,不回落到本地仓库。workspace.cwd 仍用于本地 workspace 分组,Markdown 文件仍默认保存在本地启动目录。

阅读顺序:

  1. packages/kap-server/src/routes/v2/sessions.ts:按环境解析、查询和缓存 git 域。
  2. packages/agent-core-v2/src/app/sessionIndex/sessionIndexSource.ts:环境字段在冷读取、镜像和相等比较中的保留。
  3. apps/kimi-code/src/tui/commands/info.ts 与 session.ts:/status 和导出元数据的目录选择。

可略读:三个 summary 写入点的字段透传、回归测试、中英文文档和 changeset。

Screenshots / Recordings

新增测试直接渲染 /status 并读取实际导出的 Markdown,关键输出如下(省略其余面板内容):

Surface Before After
/status Directory /tmp/proj-a Directory /remote/project
/export-md frontmatter work_dir: /tmp/proj-a work_dir: /remote/project

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
远程 session 的 /status Directory 本地启动目录 绑定的远程工作目录 远程 TUI 用户;旧值与实际执行目录不符 无(修正错误显示)
/export-md 的 work_dir 本地启动目录 绑定的远程工作目录 导出远程 session 的用户;导出文件仍保存在本地 无
v2 sessions include=git 按本地 cwd 查询 App 级 git 按 session 的 environmentId + cwd 查询 desktop、web 和 API 客户端中的远程 session 无
无环境元数据的旧会话与本地 session 本地 cwd 不变 旧版本写入的 session、所有本地用户 继续使用 local 回退

受影响模块与测试覆盖:

  • TUI message-flow:覆盖远程 /status 的实际渲染和远程导出 frontmatter;原有本地 /status 用例保留。
  • session index:覆盖冷读取恢复 environmentId / environmentCwd;metadata、fork、archive 和 mirror 测试通过。
  • kap-server v2 sessions:覆盖真实远程环境分支读取、本地与远程同 cwd 的结果隔离、原有本地去重缓存和 null 降级。
验证记录与未覆盖范围
  • agent-core-v2、kap-server、apps/kimi-code 类型检查通过。
  • kap-server v2Sessions.test.ts:49 个测试全部通过。
  • core 相关 119 个测试中 118 个通过;旧 legacy 映射断言在未修改的基线上同样失败,未纳入本 PR。
  • TUI message-flow 447 个测试中 444 个通过,包含两条新增用例;两条 fork 测试在未修改基线上同样失败,视频上传用例在基线单独复跑通过,属于时序波动。
  • 仓库 lint 通过(0 errors,既有 warnings);文档构建通过;git diff --check 通过。
  • gen-docs 所需的 docs/scripts/sync-changelog.mjs 在当前 release 不存在,因此未生成 changelog;相关指南与 API 参考已手工双语同步。
  • 未验证真实 SSH 远程环境;远程路径由 FakeEnvironment 驱动真实 git 进程的集成测试覆盖。

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

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8f79e9c

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/kimi-code Patch
@moonshot-ai/pi-tui Minor

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

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T07:00:45.739531Z 8f79e9c 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 5, 2026 •

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

commit: 2c24f42

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

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

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

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +404 to +405
const environmentId = summary.environmentId ?? LOCAL_ENVIRONMENT_ID;
const cwd = environmentId === LOCAL_ENVIRONMENT_ID ? (workspaceCwd ?? undefined) : summary.environmentCwd;

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 Rebuild old session summaries before routing git

On an existing install with a session-index generation built before these fields were added, stored summaries lack environmentId; the unchanged schema version and freshness checks let that generation continue serving them. This fallback then treats a remote session as local and probes the host path, so /api/v2/sessions?include=git can silently return missing or incorrect git details until the summary is reprojected or rewritten. Invalidate or migrate older summaries before relying on these fields.

AGENTS.md reference: AGENTS.md:L110-L112

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.

已在 8f79e9c 修复:同时提升 session 索引 schema 版本(2 → 3)和扫描缓存版本(1 → 2)。只提升前者不够,重建时仍会从扫描缓存复用缺少环境字段的旧 summary。新增测试模拟升级前遗留的索引与扫描缓存,验证重建后能恢复 environmentId / environmentCwd;只提升其中任一版本时该测试都会失败。

status = await this.core.accessor.get(IGitService).status(target.cwd);
} else {
const environments = this.core.accessor.get(IEnvironmentService);
const lease = environments.acquire({ environmentId: target.environmentId }, ['fs']);

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 Connect pending environments before reading remote git

When a listed remote session is cold and its declared environment has not connected in this server process, DeclaredRemoteEnvironment has no fs capability yet. This acquire() therefore throws, and the catch silently returns null git details instead of opening the connection; unlike session creation, this path never uses acquireWhenReady. Acquire the environment when ready so include=git can work for cold sessions.

Useful? React with 👍 / 👎.

@7Hanrui

7Hanrui commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2c24f42a74

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

stdrc and others added 3 commits November 5, 2026 14:48
Derive the displayed session directory from one TUI helper, and stop the
v2 session list from caching environment-unavailable git misses. Remote
lookups now time out after a short budget and share one in-flight request
per environment and cwd.
Bump the session index schema and scan cache versions so summaries
projected before environmentId and environmentCwd were recorded are
re-read from session metadata instead of routing remote sessions as local.
@7Sageer
7Sageer force-pushed the fix/remote-session-surface-routing branch from 2c24f42 to 8f79e9c Compare November 5, 2026 06:55
@7Sageer
7Sageer changed the base branch from release/2026-11-04 to main November 5, 2026 06:56
@7Hanrui

7Hanrui commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@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: 8f79e9c7f5

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

if (value !== undefined) this.cache.set(key, { value, fetchedAt });
return value;
})
.finally(() => this.inflight.delete(key));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Bound stalled remote git lookups

When a remote process.spawn() never settles—as modeled by the new stalled-process test—GitService.runCommand() never reaches its own command timer, so this promise never settles and the .finally() cannot remove it from inflight. Every later include=git request for the same environment and cwd therefore reuses the poisoned promise and silently returns null after three seconds, even after that environment reconnects or is replaced because the key contains no generation. Give the underlying lookup a finite lifetime or invalidate it when the environment generation changes while retaining bounded deduplication.

AGENTS.md reference: AGENTS.md:L130-L132

Useful? React with 👍 / 👎.

@7Sageer
7Sageer merged commit 2be062e into main Nov 5, 2026
1 check passed
@7Sageer
7Sageer deleted the fix/remote-session-surface-routing branch November 5, 2026 09:56
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.

4 participants