Repository navigation
refactor(adapters): CLI 特判下沉为适配器 bridgeSpec,codex-app 判定收敛 - #1898
Merged
Merged
Conversation
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 13, 2026 21:38
deepcoldy
force-pushed
the
refactor/cli-capability-sinking
branch
from
October 13, 2026 22:24
b402f94 to
23e926a
Compare
动机:worker.ts / worker-pool.ts / daemon.ts 里「是不是 Codex App」散落 45 处字面量比较 (`=== 'codex-app'`、`!== 'codex-app'`、`codex || codex-app` 各种写法),取值优先级也不一致, 新增/改动判定时没有单一落点,也看不出哪些差异是有意的。 改动: - adapters/cli/types.ts 新增 `isCodexAppCliId(cliId)`(codex-app 字面量唯一出口)和 `isCodexFamilyCliId(cliId)`(codex || codex-app)。 - worker.ts:33 处 `lastInitConfig?.cliId / cfg.cliId / msg.cliId / session?.cliId` 比较改走谓词; `codex || codex-app` 的 4 处改走 isCodexFamilyCliId。 - worker-pool.ts:13 处改走谓词,只换写法不换取值来源——需要完整三级优先级的仍用 `sessionCliId(ds, botCfg)`(cliLaunchSnapshot -> session.cliId -> bot),另外 3 处历史上就是 `session.cliId ?? bot.cliId`(不含 snapshot),原样保留并在注释里写明(见下「差异」)。 - daemon.ts:/api/send 与 hook 校验两处、streaming 卡片 follow-up 一处、新建话题一处、 queuedActivation 恢复一处改走谓词,同样保持原取值来源并加注释。 严格保持行为不变。有差异的判定点没有强行统一: 1. worker-pool `codexAppCleanInputAcceptedForSession` / `acceptCodexAppDispatch` 用 `session.cliId ?? bot.cliId`,不含 cliLaunchSnapshot;而同文件 `sendToWorker` 用含 snapshot 的 sessionCliId。`/cli use` 之后、首次 fork 之前(snapshot 仍 pending)两者结论可能不同。 疑似漂移,保持原样,加注释,并用测试钉住。 2. daemon 流式卡片 follow-up 的 `session.cliId === 'codex-app' || bot.cliId === 'codex-app'` 是 「任一为真」的 OR,而不是 session ?? bot 的优先级:session 已冻结为别的 CLI、bot 仍是 codex-app 时也为 true。疑似漂移,保持原样,加注释。 3. worker.ts `codexAppManagedOrigin` 与 daemon 两处 `validateCodexAppManagedSendOrigin` 实参 刻意取 initConfig/lastInitConfig 与 session.cliId 的 OR(fail-closed),有意保留并加注释。 4. worker-pool `updateCodexAppEffort` 用 `snapshot ?? session.cliId`(不回落 bot),此处只对已有 live worker 的会话生效,回落分支不可达,保留并加注释。 影响面:只有 worker / worker-pool / daemon 三个共用文件里的谓词写法变化,运行时语义不变; codex、codex-app、traex 及其它 CLI 的会话路径都经过同一份谓词。 测试: - 新增 test/codex-app-cli-predicate.test.ts:谓词语义;三个文件内不再出现 `=== 'codex-app'` 字面量 比较;上述有意保留的 OR / 无 snapshot 优先级被钉住(改动它们会红)。 - 4 个读 worker.ts 源码文本的断言(api-only-mode-wiring、critical-control-key、 worker-app-runner-control-wiring、worker-pipe-initial-screen-order)同步成谓词写法——只更新 钉实现写法的断言;worker-exact-interrupt 与 worker-screenshot-upload-hardening 用 vm/new Function 执行 worker.ts 片段,补注入 isCodexAppCliId。 - tsc 三套零错误。
动机:src/adapters/cli/ 下有几组逐字重复的实现,改一处要记得改 N 处,历史上已经出现过漂移。
改动(均为逐字等价搬移,行为不变):
- 新增 tui-input.ts:`BRACKETED_PASTE_START/END`、`wrapBracketedPaste()`、`writeTextThenEnter()`。
- `writeInput` 逐字相同的 4 份(aiden / copilot / gemini / mtr)改为 `writeInput: writeTextThenEnter`。
- `BRACKETED_PASTE_*` 常量 4 份(dsh-tui / ebsd-input / kimi / oh-my-pi)改为引用共享模块;
另外 9 处内联的 `'\x1b[200~' + … + '\x1b[201~'`(claude-code / coco / codex / opencode /
opencode2 / grok / hermes / pi / traex)统一走 `wrapBracketedPaste()`,字节序列不变。
- 新增 runner-path.ts:`resolveRunnerPath(file, { testOverrideEnv?, fallback? })`,替换 codex-app / dsh /
mira / mir / minimax 五份 `runnerPath()`。五份只在文件名、测试覆盖 env、末位兜底三处不同,
都参数化保留:codex-app/dsh 仍只在 NODE_ENV=test 下认各自的 BOTMUX_TEST_*_RUNNER_PATH;
minimax 保持「两个候选都不存在时返回 dist/ 路径」(fallback: 'dist'),其余返回编译产物同层路径。
文件仍在 adapters/cli/ 下,`import.meta.url` 相对位置与原先一致。
- model-catalog-json.ts 新增 `detectModelsViaExec()`(lazy promisify + execFile + 8s + 16MB + fail-soft)
与 `capabilitySlugs()`。coco / traex / opencode 的 detectModels 改为一行调用(含逐字相同的 lazy promisify
注释上提到共享函数里);codex / codex-app 的 detectModels 改为 capabilitySlugs。
child_process 的 mock 点没变(仍是 node:child_process 的 execFile,调用时才 promisify)。
- 删除 32 个适配器文件末尾的 `export const create = createXxxAdapter`:逐个核实
src / test / scripts / *.mjs 均无引用(write-input-all-cli.e2e.ts 的 `adapterDef.create` 是其本地
ADAPTERS 数组的字段,不是模块导出);registry.ts 一直走具名 import。
影响面:aiden / copilot / gemini / mtr(writeInput);codex-app / dsh / mira / mir / minimax
(runner 定位,含编译态 runnerArgv0 的入口选择不受影响);coco / traex / opencode / mimocode /
codex / codex-app(detectModels);dsh-tui / ebsd / kimi / oh-my-pi 及上列 9 个 CLI(粘贴包装)。
没动 worker / daemon / backend。
测试:新增 test/adapter-shared-primitives.test.ts(共享原语行为;aiden / copilot / gemini / mtr 四个
CLI 经共享序列提交;resolveRunnerPath 的 test-only override、两种兜底;capabilitySlugs)。
write-input / cli-adapters / *-detect-models / model-catalog(-json) / cli-runner-compiled-entries
共 9 个文件 743 passed。tsc 三套零错误。
…(cliId) 动机:worker.ts 里有 10 个 `lastInitConfig?.cliId === 'xxx'` 的一行函数(codex / traex / hermes / mtr / pi / oh-my-pi / ebsd / grok / cursor(还叫 codexBridgeIsCursor)/ antigravity),约 64 处调用, 每接入一个结构化桥接 CLI 就要再抄一个函数。 改动:只保留一个 `structuredBridgeIs(cliId)`,id 类型取自 services/structured-bridge-clis.ts 的 ALWAYS / ZERO_PROMPT 清单(拼错 id 会编译失败);调用点机械替换。不改任何判定语义。 测试:5 个按 worker.ts 源码文本断言的测试同步成新写法(codex-active-runtime-wiring、 codex-adopt-recovery、codex-worker-bridge-wiring、traex-worker-bridge-wiring、 worker-structured-lifecycle-status),仅更新钉写法的断言,断言意图不变; functionSlice 的结束标记从被删除的 structuredBridgeIsCodex 换成同位置的 structuredBridgeIs。 上述 5 文件 + structured-bridge-clis.test.ts 共 86 passed;tsc 零错误。
… / 挂载由适配器声明驱动
动机:worker.ts 的 transcript 桥接是按 CLI 名硬编码的 if 链——adopt 挂载(codex / traex / coco /
mtr / cursor / pi / grok 七个分支,其中 traex 与 cursor 的骨架与已有的 setupAdoptFileBridge
完全相同却各抄一份)、spawn 挂载(hermes / codex / traex / coco / mtr / cursor+antigravity /
pi+grok+omp+ebsd)、drain 分派(structuredBridgeIngestPath 9 路 if)。接入一个新 CLI 要在
worker 里改多处,且各处差异无处登记。
改动:
- types.ts:`CliAdapter.bridgeSpec?: StructuredBridgeSpec`(能力描述,全部是数据或纯函数,需要
worker 活状态的仍留在 worker)。字段:
- `drain(path, offset, ctx{adoptMode,state,flushTrailingFinal})` -> events/newOffset/pendingTail/state。
- `stateCommit`: 'drain' | 'ingest' | 缺省。驱动自带增量状态(codex drain state、omp/ebsd/antigravity
的 provisional 终态)由 worker 的单个不透明槽位保存;'drain' = 每次 drain(含 attach 时的一次性 drain)
都落槽(codex/omp/ebsd),'ingest' = 只在稳态 ingest 落槽(antigravity 的 attach 期 restore drain
刻意不保留状态),缺省 = 无状态。
- `locate.{bySessionId, byPid?}`:transcript 发现(omp/ebsd 只按 id,无 pid)。
- `spawn?`:`sessionId`('cli' | 'cli-or-adapter')、`baseline`('resume-aware' | 'birth-aware')、
`zeroPromptOnly`、`appServerShare`。baseline 的参数化保留了 codex / traex / antigravity 的
细微差异:codex 在 existingAppServerEndpoint 下走 split-live 并开启 App 侧本地 turn 转发,
traex 没有;antigravity 的 transcript 是 submit 后懒创建,birth-aware(晚于 spawn 创建的文件视为
live turn,不是历史)。
- `adopt?`:`attach`('split-live' | 'baseline-skip-tail')、`persistsPidProbedSessionId`。
cursor 的 transcript 无逐事件时间戳,只能按字节偏移基线化;traex 经 pid 探测到 rollout 时顺带
持久化原生 session id。
- 九个适配器声明各自的 bridgeSpec:codex / traex / coco / cursor / antigravity / pi / oh-my-pi / ebsd /
grok。registry.ts 新增 `bridgeSpecFor(cliId)` 静态查表(pi / omp 的工厂会同步 resolveCommand,
worker 每个桥接 tick 都要问,不能为了查能力去构造适配器);
test 钉住「实例上的 bridgeSpec 与查表同一对象」。
- services/file-bridge-path.ts 的 resolveFileBridgePath 改为委托 spec.locate(sid 优先、再 pid 的
顺序不变),不再自带 9 个 CLI 的 switch。
- worker.ts:
- structuredBridgeIngestPath 按 spec.drain 分派;原先分散的 codexBridgeDrainState / ompBridgeState /
ebsdBridgeState / antigravityBridgeState 四个变量并成 structuredBridgeDriverState 一个槽位。
- spawn 挂载抽成 attachStructuredBridgeAtSpawn(spec.spawn),覆盖 codex / traex / cursor /
antigravity / pi / grok / oh-my-pi / ebsd 八个分支;zeroPromptOnly(cursor/antigravity)仍受
codexBridgeFallbackActive() 约束,默认注入模式下为 no-op。
- adopt 挂载:traex / cursor 两个分支并入 setupAdoptFileBridge(由 spec.adopt 参数化),与 pi / grok
共用 sid -> pid -> split-live | pending 骨架。
未迁移(差异无法用字段表达,保留原分支):
- spawn:hermes(SQLite 库,baseline 的是整库偏移)、mtr(SQLite + mtrSessionIdForBotmuxSession 推导
id + 专用 attach)、coco(路径由 botmux sessionId 确定,文件未出现也要 attach)。
- adopt:codex(需先刷新 terminal session 才有 thread id)、coco(session 目录已消失时要发提示并禁用桥接)、
mtr(SQLite,按目录找最新会话)。
- late-attach 轮询(codexBridgeStartTimer)、codexBridgeNotifyCliSessionId 里的 codex/traex 所有权闸门与
grok/traex/antigravity 的会话轮换逻辑依赖 worker 活状态(pid、旧路径 drain-before-detach),保持原样。
影响面:worker.ts 的桥接路径(所有走结构化 transcript 桥接的 CLI:codex、traex、coco、cursor、
antigravity、pi、oh-my-pi、ebsd、grok 及 hermes/mtr 不变路径);不含 claude-code 桥接、codex-app。
测试:
- 新增 structured-bridge-spec.test.ts(spec 与适配器实例一致、白名单与 spec 覆盖面一致、逐 CLI 差异
被钉住、drain 状态契约、resolveFileBridgePath sid 优先 / pid 校验)与
structured-bridge-attach-wiring.test.ts(spawn / adopt 流程的顺序与参数来源)。
- 钉实现细节的源码文本断言同步:traex-worker-bridge-wiring、codex-worker-bridge-wiring、
existing-app-server-bridge-wiring、worker-structured-lifecycle-status(断言意图不变,改为检查
helper + 适配器声明)。
- tsc 三套零错误。
rebase 到最新 master 后与 #1854(识别恢复会话的分段 rollout)的语义交叉: 本分支把 traex adopt 并入共享的 setupAdoptFileBridge,其 pid 探测分支用 codexSessionIdFromRolloutPath 从路径解析原生会话 id。#1854 之前 traex 与 codex 的 rollout 命名逐字相同,两者等价;#1854 引入分段命名 `rollout-<ts>-<sid>_<resumeTurnId>.jsonl` 后,codex 解析器读不出 id, pid 探测命中的分段 rollout 会静默不持久化会话 id(master 上 traex 分支用的是 findTraexRolloutByPid 自带的段感知解析,不受影响)。 - adopt spec 的布尔 persistsPidProbedSessionId 改为由适配器提供解析函数 pidProbedSessionId(path);traex 传入 traexSessionIdFromRolloutPath(箭头函数, 调用期才读取导入)。共享骨架不再引用任何 CLI 专属解析器 - 测试:spec 用例以真实分段文件名断言 traex 解析出 sid、codex 解析器读不出; 接线用例断言共享骨架只走 adopt.pidProbedSessionId(path)。已负向验证: 适配器改回 codex 正则后用例失败
deepcoldy
force-pushed
the
refactor/cli-capability-sinking
branch
from
October 14, 2026 09:19
23e926a to
52e3a64
Compare
|
🚀 Released in v3.44.0 |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么
把按 CLI 名硬编码的特判下沉到适配器能力字段或统一的判定函数,严格保持行为不变。共 4 个提交:
isCodexAppCliId/isCodexFamilyCliId(定义在adapters/cli/types.ts)test/codex-app-cli-predicate.test.ts,确保这三个文件里不再出现裸=== 'codex-app'。isCodexAppSession(ds)。疑点另行跟进,见下文。structuredBridgeIsXxx()收敛为structuredBridgeIs(cliId):参数类型取自结构化桥接的 CLI 清单,拼错会编译失败。CliAdapter.bridgeSpecdrain、stateCommit(drain/ingest)、locate.{bySessionId,byPid}、spawn.{sessionId,baseline,zeroPromptOnly,appServerShare}、adopt.{attach,persistsPidProbedSessionId}。setupAdoptFileBridge。src/services/file-bridge-path.ts的resolveFileBridgePath改为委托各适配器的spec.locate。writeTextThenEnter、wrapBracketedPaste、resolveRunnerPath、detectModelsViaExec/capabilitySlugs收进共享模块。export const create。影响面
adapters/cli/**。待跟进的疑点(未改,单独跟进)
codexAppCleanInputAcceptedForSession和acceptCodexAppDispatch用的是session.cliId ?? bot.cliId,没看cliLaunchSnapshot;同文件的sendToWorker则用了带 snapshot 的sessionCliId。/cli use之后、下次 fork 之前可能会判错。这一点要先弄清 snapshot 的语义,再另行修复。验证
codex-app-cli-predicate、adapter-shared-primitives、structured-bridge-spec、structured-bridge-attach-wiring。*-detect-models、worker-pi-cot、worker-agy-delivery、worker-argv-reaction-status(含 pi / ebsd / traex)均通过。src/adapters/cli/codex.ts的 import 行有一处冲突,取两边 import 的并集即可。集成验证时已按此解决:合并整树三套 tsconfig 零错误,bun run build通过。