Repository navigation
Conversation
🦋 Changeset detectedLatest commit: c336848 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 92200f7ff8
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c336848f6f
ℹ️ 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".
`bun scripts/upstream-ledger.mjs --refresh` at upstream/main 4eb2919 added 126 rows, so every open upstream commit and every PR it references now has a ledger row: 0/596, down from 125 at 8bf8ba3 and 126 at the fetched tip. The refresh only reads `upstream/main ^ main`, so the three release-2.6.2 commits named by the mission (MoonshotAI#4875/MoonshotAI#4905/MoonshotAI#4922) gained rows with the refresh's row shape. Settled with live probes in this tree: - ported: MoonshotAI#5533 (docs 2.8.1 changelog), MoonshotAI#4875 (registry search_url and feedback_url plus the feedback service), MoonshotAI#4905 (Kimi upload status validation in packages/core), MoonshotAI#4922 (swarm task events in sessionTaskFeed), MoonshotAI#5438 (unload only declared dynamic tools), MoonshotAI#5592 (dynamic tool replacement order), MoonshotAI#5595 (append-log known length), MoonshotAI#5573 (skill roots for every work dir), MoonshotAI#5576 (date reminder cwd comparison), MoonshotAI#5618 (session titles for skill openers), MoonshotAI#5638 (frozen loop tool baseline), MoonshotAI#5626 (legacy turn-input origins) - partial: MoonshotAI#5505 (processLiveness source and consumers present; the upstream unit test file was never carried), MoonshotAI#5571 (program-resume and OSC 7577 present; the upstream test helper was never carried), MoonshotAI#5532 (gitService half present; fsService still leaks the environment lease at fsService.ts:1133 and :1267), MoonshotAI#5557 (app-client icon registry kept; the move to app-ui was never carried) - not-applicable: MoonshotAI#4655 (upstream reverted auto review in MoonshotAI#5649), MoonshotAI#5333 (PLAYBOOK:111 deliberate flat experimental flags) M39's "likely present" list was checked, not trusted: MoonshotAI#5438 is present (upstream test passes), MoonshotAI#5487 is not (nativeStartSettleMs/leg.nativeSettled absent), MoonshotAI#5635 is not (ToolsSettled absent), MoonshotAI#5429 needs a visual probe. The remaining 194 pending rows stay open with their reasons; no feature was ported and no upstream-drift allowlist entry was added. Verification: verify -> 332 closed rows, 332 carry every file of their PR; 317 probes pass, 2 foreign-runner skips, 0 fail; a second verify is byte-identical (ledger sha256 cbf53638...). Probe tests 29 pass; bun run lint and node scripts/checks/ratchet.mjs green.
Requirement or Bug
用户在支持 OSC 7577(Program Resume Protocol,程序恢复协议)的终端里重启终端或系统后,原先运行 Kimi Code 的面板会落回普通 shell,需要手动查找并重输恢复命令。本 PR 同步
@moonshot-ai/pi-tui上游的 OSC 7577 编码工具,并在交互式 TUI 中上报带 SHA-256 签名的恢复命令kimi -r <sessionId>与工作目录cwd。支持该协议的终端重开面板时会自动回到原会话与目录,用户主动退出或会话清空时则会清除记录。Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
核心思路
终端通过环境变量
OSC7577_TOKEN为每个面板注入独立密钥。CLI 在跨过原生安装包热更新重拉起之后,立即取走并从process.env中删除该令牌,只通过内存参数传给交互式 TUI,隔离所有子进程与非交互子命令。TUI 新增ProgramResumeController状态机,仅当会话同时具备sessionId和有效对话记录时向终端写出签名的kimi -r <sessionId>与本机workDir,并在主动退出或会话变空时写出clear=1。未设置OSC7577_TOKEN的终端与无界面模式不产生任何额外输出。结构图
flowchart LR Term["终端 (注入 OSC7577_TOKEN)"] --> Swap["原生热更新检查 (native-swap)"] Swap -- "重拉起保留令牌,冒烟子进程剥离" --> Main["CLI 入口 (cli/main)"] Main -- "takeProgramResumeToken 删除环境变量" --> Shell["交互宿主 (run-shell 与 KimiTUI)"] Main -. "无令牌环境" .-> Sub["非交互命令与全部子进程"] Shell --> Ctrl["恢复状态机 (ProgramResumeController)"] Ctrl -- "签名上报 kimi -r <id> 或 clear=1" --> PiTui["协议编码器 (@moonshot-ai/pi-tui)"] PiTui --> Term因为
OSC7577_TOKEN代表向当前终端面板注册启动命令的权限,所以 CLI 在解析子命令之前就从process.env剥离令牌,而不是留到 TUI 启动后再清理。这样后续派生的 shell 工具、!命令、外部编辑器和 MCP 子进程都无法伪造恢复序列覆盖当前会话。关键决策
maybeRelaunchWithStagedNativeUpdate之后、bootstrap()首行剥离process.env.OSC7577_TOKEN,热更新--version冒烟检查在子进程环境副本中单独剥离main()首行剥离,或留到KimiTUI构造时剥离reexec需要把原令牌传给替换后的新二进制,而冒烟子进程与后续子进程都不应拿到令牌ProgramResumeController提供可重入词法锁hold()与runHeld(),包裹启动、新建、恢复、切换、重载与删除会话流程await分步修改workDir、sessionId和transcriptEntries,加锁后只在最终状态收敛时发送一次序列,避免泄露中间状态或产生clear=1闪烁/exit、Ctrl+C、Ctrl+D、/web、/remote-control及关停期二次Ctrl+C)发clear=1;SIGTERM、SIGHUP与终端断连调用preserveForSignal()保留上报stop()里发clear=1SIGTERM或SIGHUP结束前台进程,若此时发送clear=1会擦除终端刚保存的恢复记录/undo判定hasResumableTranscriptContent过滤空会话/undo提示条目undo-limit-status,且从未上报过时退出不发clear=1transcriptEntries非空就上报,退出时无条件发clear=1/undo只会产生提示文本,不应触发恢复上报;从未上报过却发clear=1还会误删同面板挂起程序留下的恢复记录阅读指南
packages/pi-tui/src/program-resume.ts定义 OSC 7577 序列格式、参数数量与总字节上限(最多 64 个参数、总长 2048 字节、绝对路径cwd)以及 SHA-256mac计算。apps/kimi-code/src/tui/controllers/program-resume.ts实现disabled | active | preserved | cleared状态机、(sessionId, cwd)去重、hold()合并与信号保留逻辑。apps/kimi-code/src/main.ts、apps/kimi-code/src/cli/main.ts与apps/kimi-code/src/cli/update/native-swap.ts划定OSC7577_TOKEN在快速入口、热更新、冒烟检查与交互入口之间的隔离边界。apps/kimi-code/src/tui/kimi-tui.ts与apps/kimi-code/src/tui/commands/undo.ts负责会话生命周期、外部编辑器返回重报、信号处理与/undo清空时的接线。其余参数透传(
run-shell.ts、dispatch.ts)、tsconfig.json的ES2024库声明、changeset 与测试夹具可略读。状态迁移大纲
flowchart LR Start(("构造")) -- "无令牌" --> Disabled["disabled (静默)"] Start -- "非空 OSC7577_TOKEN" --> Active["active (同步恢复序列与 clear=1)"] Active -- "SIGTERM / SIGHUP / 启动异常" --> Preserved["preserved (冻结已上报记录)"] Active -- "主动退出 clearForExit()" --> Cleared["cleared (按需发送单次 clear=1)"] Preserved -- "二次 Ctrl+C clearForExit(force)" --> Cleared状态机把“静默”“正常同步”“信号冻结”和“已清除”拆成互斥分支。正常退出最多发送一次
clear=1,信号关停期间即使后续清理逻辑清空界面也不会覆盖终端已保存的恢复命令。Behavior Changes and Affected Users
OSC7577_TOKEN时的交互式 TUI 终端输出OSC7577_TOKEN,不输出 OSC 7577 序列OSC 7577恢复序列(kimi -r <sessionId>与workDir),会话清空或用户主动退出时输出clear=1,SIGTERM和SIGHUP退出时保留记录OSC7577_TOKEN的终端中使用kimi交互式 TUI 的用户OSC7577_TOKENprocess.env.OSC7577_TOKENprocess.env中并继承给子进程reexec保留该变量;热更新--version冒烟子进程及bootstrap()之后的所有入口(TUI 的!命令、Bash 工具、外部编辑器、MCP stdio 子进程、kimi -pprint 模式及其他 CLI 子命令)均从环境中剥离OSC7577_TOKENkimi内部通过!、Bash 工具或插件子进程直接读取顶层终端OSC7577_TOKEN的脚本/undo后的KimiTUI.hasSessionContent()Nothing to undo.状态条目后返回trueundo-limit-status提示条目后仍返回false,退出时不打印无内容的kimi -r <sessionId>提示/undo后直接退出 TUI 的交互用户受影响模块与测试覆盖
@moonshot-ai/pi-tui(packages/pi-tui/src/program-resume.ts)新增formatProgramResume、formatProgramResumeClear与takeProgramResumeToken,由packages/pi-tui/test/program-resume.test.ts覆盖协议测试向量、SHA-256 签名与不可携带输入的回退。@moonshot-ai/kimi-codeCLI 入口与热更新(apps/kimi-code/src/main.ts、src/cli/main.ts、src/cli/run-shell.ts、src/cli/update/native-swap.ts)负责令牌剥离、热更新重拉起保留与交互宿主透传,由apps/kimi-code/test/cli/main.test.ts、exec-server.test.ts、run-shell.test.ts与update/native-swap.test.ts覆盖。@moonshot-ai/kimi-codeTUI 控制器与会话接线(apps/kimi-code/src/tui/controllers/program-resume.ts、kimi-tui.ts、commands/undo.ts)负责会话上报、防抖合并、外部编辑器返回重报、/undo清空清除与信号保留,由apps/kimi-code/test/tui/controllers/program-resume.test.ts、kimi-tui-startup.test.ts、signal-handlers.test.ts与commands/undo.test.ts覆盖。验证结论
macOS 环境下已通过单元测试、TUI 集成测试与真实 POSIX PTY 双阶段端到端演练,覆盖无会话启动静默、子进程环境隔离、首轮发言签名上报、
kimi -r恢复历史回放、SIGTERM保留记录以及/exit清除记录。Windows 下的信号与路径校验已由单测覆盖,未在真实 Windows 终端模拟器上实机验证。执行的验证命令与结果
pnpm --filter @moonshot-ai/pi-tui test通过(1257 个通过,1 个既有跳过)。pnpm --filter @moonshot-ai/pi-tui run typecheck通过。pnpm exec tsc -p apps/kimi-code/tsconfig.json --noEmit通过。pnpm exec vitest run --reporter=agent apps/kimi-code/test/tui/controllers/program-resume.test.ts apps/kimi-code/test/tui/commands/undo.test.ts apps/kimi-code/test/cli/main.test.ts apps/kimi-code/test/cli/run-shell.test.ts apps/kimi-code/test/cli/exec-server.test.ts apps/kimi-code/test/cli/update/native-swap.test.ts apps/kimi-code/test/tui/signal-handlers.test.ts apps/kimi-code/test/tui/kimi-tui-startup.test.ts通过。pnpm lint通过。pnpm run check:duplication <base-sha>通过。pty.openpty()加本地假 OpenAI SSE 服务)通过。阶段一验证无会话启动 0 条 OSC 7577、!子进程OSC7577_TOKEN为空、首轮对话后发出 1 条合法 SHA-256 签名的['kimi', '-r', sessionId]与cwd、/exit发出clear=1。阶段二验证kimi -r <sessionId>恢复历史后立即上报、SIGTERM退出不发clear=1。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.