Skip to content

feat: import Claude Code and Codex sessions - #4342

Merged
RealKai42 merged 18 commits into
mainfrom
feat/session-import-conversion
Oct 26, 2026
Merged

RealKai42 merged 18 commits into
mainfrom
feat/session-import-conversion

Conversation

@RealKai42

@RealKai42 RealKai42 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

把最近在 Claude Code 和 Codex 里的会话导入 Kimi Code,让用户接着之前的工作继续。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

packages/session-import          外部会话 → 一条导入消息 + 完整历史附件;不依赖引擎、SDK 或 UI
├── discover.ts      findSessions:列出最近的 Claude Code / Codex 交互会话
├── claude-code.ts   Claude Code 读取:沿 parentUuid 选当前分支,处理压缩保留段、回退、并行工具结果和晚写入的父记录;跳过 API 请求日志
├── rollout.ts       Codex 读取:JSONL 和 zstd,处理压缩替换、回滚和事件去重
├── images.ts        嵌入图片按内容保存并换成路径引用;附件原子发布,不支持硬链接时改用独占复制
├── archive.ts       保存完整有效历史附件和源文件副本
├── render.ts        渲染导入消息,按预算从最新一轮往前整轮保留
└── convert.ts       convertSession:读取 → 附件 → 渲染 → 返回状态
packages/migration-legacy
└── imported-session.ts   writeImportedSession:复用 kimi-cli 迁移的写法,写会话目录、轮次记录和会话索引
apps/kimi-code
├── session-import/                  扫描、导入预算、逐个转换写入、导入页
├── tui/commands/import-sessions.ts  /import-sessions(仅空闲时可用)
├── cli/commands.ts                  kimi import-sessions(需要交互终端)
└── migration/badge.ts               /sessions 里导入的会话显示 [Claude Code] / [Codex]

另外更新了中英文的会话指南、kimi 命令参考和斜杠命令参考,同步了工作区、lockfile、Nix 清单和项目地图。

导入后的会话以一条普通、可见的用户消息开头:

<system>导入说明:这是从 Claude Code / Codex 导入的记录,不是新指令</system>
<imported_conversation>
<session_info>来源会话与源文件、标题、工作目录与 git 分支、活动时间、带入了哪些轮</session_info>
<attachments>完整历史和源文件副本的位置及各自包含的内容(完整历史第 N 轮在第 N + 1 行)、图片索引</attachments>
<known_limitations>已知限制(有才出现)</known_limitations>
<source_summary>来源的压缩摘要(有才出现)</source_summary>
<turn index="N" time="…"><user>原话</user><assistant>回复与工具记录</assistant></turn>
…
</imported_conversation>
<system>结尾说明:对方的工具在这里不可用,调用记录不是待执行的动作;文件可能已变,编辑前先重读;等用户发话再继续</system>

转换规则

  • 工具调用写成 [external_agent_tool_call: 名称] 加来源记录的输入前 2000 字符,结果写成 [external_agent_tool_result] 加前 4000 字符,来源标记出错时加 : error,被截断的地方带指向完整历史的标记。所有工具同样处理,格式参照 Codex 0.163.0 的 /import。
  • 思考、来源事件、注入的来源上下文和 API 错误记录不进消息,只留在完整历史附件里。来源文本不转义。
  • 完整历史附件保存源 agent 最后看到的对话(当前分支、压缩之后)的全部内容,包括思考、完整的工具输入输出和来源记录。
  • 源文件副本:源会话文件按字节逐行原样保存,只去掉 Claude Code 的 API 请求日志(它只是对话的重复副本),Codex 的 zstd 解压后保存;Claude Code 连同同名会话目录(子代理会话、外置的工具输出)一起保存,相对路径不变。完整历史里没有的压缩前历史、放弃的分支、子代理会话和外置工具输出都在这里,源文件以后被清理也不受影响。复制完会再核对源文件,期间有变化则返回 SOURCE_CHANGED。
  • 嵌入图片按原字节保存、按 sha256 去重,在原位置换成路径引用;本地路径和 URL 只保留引用,不检查是否存在,也不下载。
  • 标题:来源里用户命名的标题优先,其次是生成的标题。都没有时取首条提问里第一行有字母或数字的文字,跳过控制标签、IDE 附带的上下文(## My request: 之前的部分)和图片引用行,最多 120 字符。会话列表里的“最后一次提问”按同样规则取自来源。
  • 状态:complete;partial 带具体原因(预算省略、未识别内容、加密压缩等);unreliable 带错误码,不写入会话。没有任何提问的会话返回 EMPTY_SOURCE_HISTORY;Codex 里经过 fork 或从之前消息重新开始的会话返回 UNSUPPORTED_HISTORY_BASE。

导入预算:转换包默认用窗口的 80%,调用方可以传入 tokenBudget。Kimi Code 按默认模型计算:

  • 窗口取输入上限,没有就用上下文窗口。
  • 预算 =(自动压缩阈值 − 3 万)÷ 1.25,且不超过窗口的 80%。阈值按引擎规则取“窗口 × 触发比例”和“窗口 − 预留”中较小的一个,并读取 loopControl 配置。
  • 目的是给续聊留出余量,避免一开始就把导入内容压缩掉,也不超出输入上限。除以 1.25 是因为估算会把代码和 JSON 少算约四分之一(用本地会话实测)。
  • 例如 262,144 的窗口预算为 145,715;400k 窗口、272k 输入为 153,600;没有模型配置时为 40,857。

导入流程

  • /import-sessions 和 kimi import-sessions 打开同一个页面:默认选最近 15 天,另有最近 30 天和取消;页面显示各来源的数量,以及不计入的已导入数量和项目目录已不存在的数量。
  • 每次最多导入最近的 100 个。项目目录已不存在的会话在扫描时就排除,不占名额。
  • 会话 ID 为 ses_<来源会话 ID>,重跑会跳过已导入的会话。
  • 导入的会话写进原项目目录对应的工作区,保留标题和最后活动时间,并带合成的轮次记录,续聊时轮次编号不会冲突;不改引擎、SDK 和会话格式。
  • 正在写入、没有提问和不支持的会话会被跳过,结果页按原因列出;失败或跳过的会话不留下半成品目录。
  • 每个导入的会话都带源文件副本,例如本机 143.6 MB 的源会话(大部分是 API 请求日志),附件合计约 24 MB。
  • 导入在本地完成,不调用模型,也不修改源文件。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
导入入口 无 新增 /import-sessions(仅空闲时可用)和 kimi import-sessions(需要交互终端) 无,新增功能 不运行就不读不写
会话列表标记 只有 kimi-cli 迁移的会话带 [imported] 导入的会话显示 [Claude Code] / [Codex],其他会话不变 无 不适用
导入消息里的说明文字 无 只出现在导入会话的第一条消息里:告诉模型这是导入的记录、对方的工具不可用、编辑前先重读文件 无,只影响导入的会话 不导入就不会出现
导入预算 无 按默认模型的输入上限和自动压缩阈值计算,最多为窗口的 80% 无,新增功能 不适用
会话转换 API 无独立转换包 新增 convertSession() 和 findSessions() 后续接入该包的内部调用方 可传入 tokenBudget;不调用就不读不写
现有 CLI 子命令与选项、其他斜杠命令、print、desktop/web、VS Code、ACP 和 SDK 现有入口、配置与会话格式 不变:apps/kimi-code/src/cli 只新增一个子命令,已有子命令、选项和输出格式不变;未改引擎、SDK 和会话格式 现有脚本和客户端 不适用

测试覆盖

  • 来源读取:分支与回退、压缩保留段、并行工具结果、晚写入的父记录、镜像消息、旧版本格式、读取时源文件变化、未知与损坏的记录、API 请求日志。
  • 转换与附件:导入消息的完整结构、工具记录与截断、截断不切开图片引用、标题与最后一次提问、预算与 Unicode 边界、图片字节与去重、原子发布与不支持硬链接时的回退、源文件副本(去掉 API 请求日志、按字节保留、会话目录的路径映射、zstd 解压)、三种状态。
  • 应用:扫描规则、导入预算、导入与重跑去重、跳过原因、批量上限与中途停止、导入页各状态与按键、两个入口的接线;写出的会话能被引擎读取,标题、时间和轮次正确。
  • session-import 186 项、migration-legacy 266 项、kimi-code 全量 4089 项测试通过;类型检查、类型感知 lint、构建和 Nix 工作区同步检查通过。

真实会话验证

  • 本机 19 个真实会话、两档窗口(262144 / 131072)共 38 次转换:18 次 complete,16 次 partial(预算省略、Claude Code 的 tool_reference 块、Codex 加密压缩),2 次 EMPTY_SOURCE_HISTORY,2 次 Codex fork 会话的预期拒绝。每个结果都是一条用户消息,首尾说明完整,不超预算;图片哈希与索引一致,完整历史中没有重复的工具调用,重复转换的结果一致。
  • 端到端:在伪终端里用打包产物运行 kimi import-sessions,写入临时主目录。导入了 12 个会话,每个都带源文件副本;再次运行时全部显示为已导入;kimi session list --all 列出 12 个。临时目录已删除。
  • 另一台会话更多的机器上,用同一套验证脚本测过加入源文件副本之前的版本:51 个 Claude Code 会话(2.1.229–2.1.296)和 1513 个 Codex 会话(0.75.0–0.163.0-alpha,其中 1422 个为 zstd),共 907.7 MB。没有崩溃、超时或违规,确定性抽查一致;和更早一轮的结果逐会话对比,除被修复的会话外完全一致;端到端导入 64 个会话,数量全部对得上,最大的导入消息不超过预算。源文件副本是之后加入的,尚未在那台机器上验证。

已知限制

  • 转换包的 80% 只是默认上限;Kimi Code 按压缩阈值传入预算,其他宿主需要自行计算。
  • 不复制的内容:引用的本地图片文件(不在来源工具的目录里,被删除后也不会另外提示)、Codex 的子线程,以及 Claude Code 的编辑前文件备份(file-history)和任务工作目录里的文件。
  • 完整历史里的 Codex 投影事件按原样保存,最多比未压缩的源文件大约 15%。
  • 未做实际的模型续聊验证。

新增 changeset(@moonshot-ai/kimi-code minor):“Add /import-sessions and kimi import-sessions to import Claude Code and Codex sessions from the last 15 or 30 days. Run either one and choose a time range.”

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).(无关联 issue;需求见上文。)
  • 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.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 12:10

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-10-26T05:27:04.871126Z fcd4e2b New commits
🔒 Security Review ✅ Completed 2026-10-22T12:16:32.027694Z d7ef1c7 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 Oct 22, 2026 •

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

commit: fcd4e2b

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

ℹ️ 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 thread packages/session-import/src/rollout.ts Resolved
Comment thread packages/session-import/src/images.ts Resolved
Comment thread packages/session-import/src/rollout.ts Resolved
Comment thread packages/session-import/src/rollout.ts Resolved
- Extend a rollback cutoff only over records that share a defined turn id.
- Keep tool-call arguments verbatim instead of rewriting image-shaped values.
- Keep media from an assistant projection that its matching response lacks.
- Count unknown event and projection kinds as unrecognized source content.
@changeset-bot

changeset-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fcd4e2b

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

Follow the Codex /import format: each source turn becomes an ordinary user
message and one assistant message, tool calls and results become tagged text
notes clipped to 2,000 and 4,000 characters, and thinking, source events and
injected context stay only in the full history attachment. A final user notice
records the source session, included turns, attachment paths and known
limitations.

Prefer a user-set title over later generated ones and fall back to the first
prompt line, treat agent-name rows as metadata, stop reporting missing
reasoning text, return EMPTY_SOURCE_HISTORY for sources without a prompt, and
explain why forked or restarted Codex sessions are rejected.

@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: 201dae53c0

ℹ️ 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 thread packages/session-import/src/rollout.ts Resolved
Add /import-sessions and `kimi import-sessions`, which open the same
page: scan recent Claude Code and Codex sessions, choose the last 15
days (the default) or 30 days, import the newest 100 with progress, and
summarize what was imported and skipped. Reruns skip sessions imported
before.

Imported sessions are written into the workspace of their original
folder with their title, last activity time, and synthesized turn
records, using the migration-legacy session writer, and show a
[Claude Code] or [Codex] badge in /sessions. Discovery lives in
session-import and leaves out subagent, non-interactive, and prompt-less
sessions. Sessions still being written are reported as in use, and
fallback titles skip lines without letters or digits.
@RealKai42 RealKai42 changed the title feat: add standalone session import conversion package feat: import Claude Code and Codex sessions Oct 22, 2026

@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: 51ec78679c

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

/import-sessions writes sessions straight to disk, which the running
session index only noticed on its next periodic reconcile, so /sessions
missed them until a restart or up to a minute later. Look each imported
session up by id after the run so the index records it immediately.
Records without text of their own, such as titles, mode changes, and
request dumps, were kept three times in the full history attachment: in
a header list, as the step text, and again in the step's source
metadata. Keep them only as the step text with the source line in the
metadata, and do the same for unknown content blocks and unknown Codex
response items. On local sessions the attachments shrink from 346 MB to
141 MB for 240 MB of source files.
Render the imported history as a single user message: a system note
before and after it, session info, attachment locations, known
limitations, and the included turns. Every tool call keeps the first
2,000 characters of its recorded input instead of three picked fields.
The session list shows the last real prompt from the source.
Claude Code 2.1.300 and later write API request records into the
session file. Their message copies were read as conversation records,
which duplicated tool calls and results and wrote the request log into
the full history attachment.

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

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

Size the imported message from the default model's input limit and keep it
below the automatic compaction threshold, so continuing an imported session
does not start with a compaction or an over-limit request. Sessions whose
project folder no longer exists are left out when scanning, so they no
longer take the newest-100 slots of every run.
Link a Claude Code record to its parent even when the parent was written
after it, placing the parent first, instead of cutting the history there.
Titles and last prompts skip IDE context ahead of the request and image
reference lines. Codex tool search results and three recent Claude Code
metadata records are no longer reported as unrecognized content.

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

ℹ️ 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 thread packages/session-import/src/images.ts Resolved
Take the last record that no other record names as its parent as the end
of a Claude Code conversation, so a parent written after its child at the
end of the file no longer hides the final prompt. On filesystems without
hard links, publish attachments with an exclusive copy instead of failing
the import.

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

ℹ️ 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 thread packages/session-import/src/rollout.ts Resolved
Resolve the default model through its overrides, as the engine does,
before taking its input limit, so an override that lowers the context
size also lowers the import budget.
@RealKai42

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 447dc10433

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

Save the source session file in the import's attachments, minus Claude
Code's API request log, together with its Claude Code folder of subagent
transcripts and saved tool output, so history from before compaction and
abandoned branches outlive the source files. The imported message says
what the full history and the copy each hold, and clipped tool notes point
to the full history.
- Copy the rollouts of the subagent threads a Codex session spawned under source/subagents/.
- Show what the user did during a turn (interruptions, slash commands, messages sent while
  the agent worked, Codex shell commands) as [user_event] notes.
- Show the work recorded after a source compaction with its summary instead of dropping it.
- Title Codex sessions with the thread name from Codex's state database.
- Point omitted media and injected source context to the attachments that keep them.
…ts turn

- Show work and commands recorded before a turn's prompt at the start of that turn,
  including Codex work before the first prompt, instead of dropping them without a summary.
- Wait up to 3 seconds for a Codex state database lock before falling back to the
  first prompt as the title.

@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: 77ab5346a3

ℹ️ 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 thread packages/session-import/src/rollout.ts Resolved
- When a turn starts, claim only the records written before it that can open a turn
  (user input and context), so a rollback no longer drops the previous turn's
  answer and tool work in sessions that mix records with and without turn ids.
@RealKai42

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: de7f6bb9f2

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

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.

3 participants