Skip to content

fix(topic): /th /tw 后紧跟技能或斜杠命令时照常转给 CLI - #1975

Closed
wzono wants to merge 4 commits into
deepcoldy:masterfrom
wzono:topic/topic-header-passthrough
Closed

wzono wants to merge 4 commits into
deepcoldy:masterfrom
wzono:topic/topic-header-passthrough

Conversation

@wzono

@wzono wzono commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

改了什么

/th(等同 /t here)和 /tw 后面紧跟技能或斜杠命令时(例如 /th /teach 讲讲实现),原先会被话题头部解析器当作「已写头部」,然后对未知的 /xxx fail closed,消息被拒。这和 /t /goal 干活 的既有用法不一致。

  1. core/topic-header.ts:生命周期变体本身不算「用户写了头部」。只有标题、worktree 或指令出现时,才对未知 /xxx fail closed。紧跟的技能或命令照常落进正文,转给 CLI。
  2. daemon.ts:冷启动透传只用于 forceTopicMode === 'default'。/th 和 /tw 的目录(当前群会话目录或先建的 worktree)只在常规建会话路径里解析,冷启动透传会直接在 bot 默认目录 fork,所以两者都改走常规路径。原来只排除了 /tw。

影响面

  • 范围:只影响新话题的 slash 入口。/t 及不带生命周期变体的写法行为不变。变体之外又写了标题或指令时,未知 /xxx 仍然 fail closed。
  • 会话类型:话题会话(here / worktree 两种生命周期)。

测试验证

bun run build                      # 通过
npx vitest run --project unit test/daemon-rename-route.test.ts test/topic-header.test.ts
# Test Files 2 passed, Tests 198 passed

新增用例:

  • 只写变体时,紧跟的 /技能、/命令 原样落进正文(与 /t /goal 一致);
  • 变体加标题或指令时,未知 /xxx 仍 fail closed;
  • /th /<passthrough> 走常规建会话路径并转发原文。

🤖 Generated with Claude Code

生命周期变体不算「写了头部」:`/th /teach 讲讲实现` 与 `/t /goal 干活`
同形,紧跟的技能/命令必须照样转给 CLI;`/th` 与 `/tw` 都不走冷启动透传。

Co-Authored-By: Claude Code <[email protected]>
@wzono
wzono force-pushed the topic/topic-header-passthrough branch from 6a41f7e to aa2d0b6 Compare November 3, 2026 04:42
@wzono
wzono marked this pull request as ready for review November 3, 2026 04:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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 ⚠️ Failed 2026-11-03T05:54:01.796542Z
We couldn’t complete this review. Comment @codex review to try again.
df13911 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.

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

ℹ️ 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 src/daemon.ts Outdated
if (forceTopicMode !== 'worktree' && slashDecision.kind === 'passthrough') {
// `/th|/tw /<passthrough>`:生命周期变体的目录(当前群会话目录 / 先建的 worktree)只在
// 下面的常规建会话路径解析,冷启动透传会直接在 bot 默认目录 fork,所以两者都不走它。
if (forceTopicMode === 'default' && slashDecision.kind === 'passthrough') {

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 Preserve raw delivery for lifecycle-prefixed passthrough commands

With default inline injection on a Codex or Claude PTY bot, /th /goal ... now falls through to the ordinary opening path, which calls buildNewTopicCliInput and wraps /goal in <user_message> plus other context. It never sets pendingRawInput, unlike /t /goal ..., so the native command becomes model text rather than a literal slash-command invocation; /tw has the same issue after worktree setup. This contradicts the documented “forwarded verbatim” contract in src/core/passthrough-commands.ts:17-21. Resolve the lifecycle cwd/worktree first, but retain raw-opening delivery afterward.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in df13911. Lifecycle-prefixed native commands retain raw delivery; daemon-command precreate now waits for lifecycle cwd/worktree setup; copy-pinned assertions were replaced with behavior checks. Focused suites: 231/231; build and diff-check pass.

Comment thread src/core/topic-header.ts
const claimed = title !== undefined || worktree !== undefined || lifecycle !== undefined || Object.keys(directives).length > 0;
// 生命周期变体不算「写了头部」:`/th` 就是 `/t here`,`/th /teach 讲讲实现`
// 与 `/t /goal 干活` 同形,紧跟的技能/命令必须照样转给 CLI。
const claimed = title !== undefined || worktree !== undefined || Object.keys(directives).length > 0;

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 Apply lifecycle setup to newly admitted daemon-command sessions

When the current chat cwd differs from a configured defaultWorkingDir and automatic worktrees are disabled, the newly accepted /tw /status or /th /status reaches executeNewTopicSlash's daemon-precreate branch. That branch persists the ordinary configured cwd and returns before the lifecycle resolution/worktree setup at daemon.ts:25384-25480. The following task reuses that session and launches directly in the configured cwd; /tw therefore silently loses its promised worktree isolation. Either reject unsupported lifecycle/daemon-command combinations or apply the lifecycle setup before precreating their sessions.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in df13911. Lifecycle-prefixed native commands retain raw delivery; daemon-command precreate now waits for lifecycle cwd/worktree setup; copy-pinned assertions were replaced with behavior checks. Focused suites: 231/231; build and diff-check pass.

expect(ds?.pendingRepo).toBe(false);
expect(mocks.forkWorker).toHaveBeenCalledTimes(1);
expect(JSON.stringify(mocks.forkWorker.mock.calls[0])).toContain(forwarded);
expect(JSON.stringify(mocks.replyMessage.mock.calls)).not.toContain('未知');

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 Replace the localized reply-copy assertion with a behavior check

This new assertion inspects the Chinese word 未知 in serialized Lark replies rather than a routing or delivery outcome. A harmless confirmation-copy change containing that word can fail a successful route, while an English unknown-directive response evades this particular assertion. The root AGENTS.md (which points to CLAUDE.md) explicitly prohibits tests pinned to copy. Rely on the existing session/fork checks or assert the structured delivery payload instead. CLAUDE.mdL149-L149

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in df13911. Lifecycle-prefixed native commands retain raw delivery; daemon-command precreate now waits for lifecycle cwd/worktree setup; copy-pinned assertions were replaced with behavior checks. Focused suites: 231/231; build and diff-check pass.

zhongweijie.x and others added 2 commits November 3, 2026 13:33
dashboard IPC 测试运行时相邻端口被其它进程占用;空提交仅重触发 CI。

Co-Authored-By: Claude Code <[email protected]>
生命周期头后的原生斜杠命令继续走 raw_input,并让预建型 daemon 命令在 cwd/worktree 准备完成后执行。补充不依赖本地化文案的行为测试。

Co-Authored-By: Claude Code <[email protected]>
@wzono

wzono commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

源码切片行为测试显式提供新增的生命周期开场依赖,避免与真实路由无关的 ReferenceError。

Co-Authored-By: Claude Code <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner

你好,这是自动评审(Claude)的首审初步意见,最终结论以维护者复审为准。

本地已把本 PR 三个提交 rebase 到最新 master(4d3b54cba,无冲突,patch-id 与原提交恒等)。验证:tsc 净;test/topic-header.test.ts bun/vitest 双腿绿(并对 claimed 行做变异,新用例咬红);test/daemon-rename-route.test.ts vitest 176 全绿。topic-header 解析器的改动方向和测试都没问题;daemon 侧的延迟派发发现以下问题:

P1:/th <预建型 daemon 命令> 之后,整个话题永久卡死

src/daemon.ts pinned 分支(约 25976 行)里 lifecycleDaemonCommand 调完 handleCommand 后直接 markIngressAdmitted + return:会话保持 worker=null 且 initialStartPending=true,既没有 fork 也没有清标志。之后该话题的第一条真实任务(以及随后每一条)在 handleThreadReplyAdmitted 恒走 initialStartPending 缓冲分支,被 park 进 queuedActivationTail:没有 CLI 被拉起、没有任何错误提示、也没有 release 定时器,只有 daemon 重启时 session-manager 的 tail 提升能救活。

真实夹具探针(seed 一个带 workingDir 的群会话 → handleNewTopic('/th /status') → handleThreadReply('修一下登录')):

  • /th /status 后:worker=false, initialStartPending=true;
  • 续轮:forkWorker 0 次,queuedActivationTail=["修一下登录"],用户侧零反馈;
  • 对照组默认 /status(无 /th,走既有预建路径)后续轮正常 fork 1 次。

影响面:所有 sessionPolicy: 'default' 的预建命令——/status /help /insight /close /restart /retry /detach /cd /role /pair /login /adopt /oncall /relay /subscribe-lark-doc /vc /cli(/th /repo 会在 topic-spec 相斥校验被拒,到不了这里;/card /cot /term /sessions 是前置特判,直接执行,不受影响)。

建议修法:延迟 daemon 命令的情形把 ds 建成与既有默认预建路径一致的 command scratch——initialStartPending 从建会话起就是 false(handleCommand 前),且不播种任何开场缓冲(pendingCodexAppText/messageContext 置空,见 P2)。最小验证补丁(仅在 handleCommand 后置 ds.initialStartPending=false)我在本地探针验证过:续轮恢复 1 次 fork、tail 清空。

P2:/tw <同组命令> 的命令文本泄漏为 worktree 会话的幽灵首轮

建 ds 时 daemon 命令情形把 pendingPrompt 置成了 '',但 pendingCodexAppText 仍是命令文本(/status)。auto-worktree 分支先 stageClaimedPendingRepoSetup,journal 里落成 {prompt:'', codexAppText:'/status'};worktree 建好后 commitRepoSelection 据此判 hasBufferedInput=true(card-handler.ts 约 735-741 行,判据含 pendingCodexAppText !== undefined),于是不走 empty start,而是 fork 出一个真实首轮。

实证:/tw /status 后直接驱动真实 commitRepoSelection,forkWorker 收到的开场是包着空 <user_message></user_message> 的完整信封、initialUserTurnPending=false(即不被标记为「等首轮」)、并绑定了真实 turnId;codex-app 干净输入侧可见的 UserMessage 文本就是 "/status"。用户视角:worktree 建好后 CLI 白跑一轮,daemon 命令既没有按命令语义生效在新会话里,也没有作为任务被用户发出。

建议:延迟 daemon 命令情形与 P1 同一处处理——pendingPrompt='' 的同时把 pendingCodexAppText 及两个 context 字段置空,让 journal 为纯 empty start(/status 的回复仍由前置的 handleCommand 正常发出)。

另请注意命令执行时机:auto-worktree 分支里命令在 worktree 创建之前对 base 目录的 pending 会话执行(/status 报的是 base;/tw /close 会先关掉会话,随后 worktree 仍照建、在 pendingRepo 护栏处丢弃,白建一个目录)。如果期望命令作用于新 worktree,需要把执行点挪到 commit 完成之后;至少建议在 PR 描述里写明当前语义。

P2(配额):/th /tw + 纯管理命令现在扣 1 条 message quota

enforceMessageQuotaForCliInput 对 /th /status 照常扣费(isBareForceTopic 因正文非空为 false,skipCharge 不成立);默认 /status 在配额闸之前执行、零扣费。配额受限的用户每发一条这类纯管理命令就少一个额度。建议延迟 daemon 分支带 skipCharge: true(evaluateTalk 与 prompt.submit 闸照跑,只是不扣额度)。

P3

  1. /th 下非 cold-start 的 passthrough 命令绕过了 delivery === 'cold_start' 判定被原样 raw 输入新 CLI(默认 /t 同形会回「需要已有会话」),两条入口行为出现分歧;raw 文本本身无害,但是否刻意请确认。
  2. 新增的 /th /status 用例只断言「没有 fork」,且夹具里生命周期目录解析器与默认 inherit 层都能命中同一个 seeded 群会话——revert 路由改动该用例仍绿(我本地验证过)。建议补两条真正的回归测试:① P1:命令之后第一条真实任务正常 fork 且 tail 清空;② P2:/tw 命令后 journal 不含命令文本、commit 后走 empty start。

@deepcoldy

Copy link
Copy Markdown
Owner

复审判定:#1975 建议作者修改,暂不可合

在 2d512f8bf(当前 head,已含 df13911c8 与 2d512f8bf)上复核,本地把四个提交 cherry-pick 到 origin/master(6a400d93a,无冲突,三个实代码提交 patch-id 与原提交逐字相同:dd93244 / 48e77a2 / a6d0d8e)。tsc 净;test/topic-header.test.ts + test/daemon-rename-route.test.ts + test/new-topic-lark-caller.test.ts 共 243 用例全绿;CI 在 2d512f8bf 上 11 项全 pass。

parseTopicHeader 那一侧的改动方向与测试我认同(把 claimed 行改回去,新用例立刻咬红)。问题全在 daemon 侧 deferred 的落地收口:两条 deferred 分支都没有把「这一轮没有 AI 回合」这件事讲清楚——一批分支说它还在开场(P1a/P2a),另一批说它是开场内容(P2b/P2c)。

以下每条结论都由真实路由 + 真实 card-handler/commitRepoSelection 夹具跑出(charts 见下),不是读码推断。


🔴 P1-a /th <预建型 daemon 命令> 之后整个话题静默失效

src/daemon.ts pinned 分支调完 handleCommand 直接 markIngressAdmitted + return,ds 仍是 worker=null + initialStartPending=true。

探针(seed 带 workingDir 的群会话 → handleNewTopic('/th /status') → handleThreadReply('修一下登录')):

/th /status 之后: {"worker":false,"initialStartPending":true,"pendingPrompt":"","pendingCodexAppText":"/status"}
后续真实任务:      {"forks":0,"worker":false, tail:["...修一下登录 的信封..."]}
对照(默认 /status): {"forks":1,"initialStartPending":false,"worker":true}

用户侧零反馈,只有 daemon 重启时 promoteQueuedActivationTail 能救。补最小修复(handleCommand 后 initialStartPending=false + 清三个 codexApp 缓冲)后探针恢复 forks=1。

🔴 P1-b 同一批命令还会撞上「开场还在进行」的护栏,回一句假原因

/th /restart、/th /cd <dir> 在 handleCommand 里被 hasProtectedSessionMutationOwnership(ds)(initial_start 腿)拒掉,且文案固定说是 Codex App:

/th /restart: "当前 Codex App 仍有未结算消息,暂不能重启;请等待本轮完成或关闭会话。" initialStartPending=true worker=false
/th /cd X:    "当前 Codex App 仍有未结算消息,暂不能切换工作目录;…"                initialStartPending=true worker=false

bot 的 CLI 根本不是 codex-app,用户看到的是错误原因;/restart 这类命令在这条路径上永远不可能成功。修 P1-a 时把 initialStartPending 从建会话起就置 false(与既有「默认预建 command scratch」路径一致),这两条自然痊愈。

🟠 P2-a /tw <daemon 命令>:journal 里带着命令文本,commit 后白烧一个开场

pendingPrompt='' 但 pendingCodexAppText 仍是 /status,stageClaimedPendingRepoSetup 把它一起落盘:

呈递给 runAutoWorktreeCommit 的 prompt: "/status"
journal: { mode: "auto_worktree", prompt: "", codexAppText: "/status", ... }

驱动真实 runAutoWorktreeCommit/commitRepoSelection 后:hasBufferedInput 因 pendingCodexAppText !== undefined 为 true ⇒ 不走 empty start,fork 出一个 <user_message> 为空的完整信封;codex-app 侧 codexAppInput.text 就是 "/status",会成为一条真实用户消息。

🟠 P2-b /tw <passthrough>:除了正确的 raw 命令,还会多提交一条空正文的幽灵轮次

同一个 pendingCodexAppText 泄漏在 passthrough 上也发作,但形状不同(这条不在前一轮评审里):

/tw /goal 修登录(codex,真实 runAutoWorktreeCommit):
  fork prompt = ""                      ← 正确:raw 走 prompt_ready
  pendingRawInput = "/goal 修登录"      ← 正确
  pendingFollowUpInput = { userPrompt:"/goal 修登录", cliInput: "<…3169 字的信封…>\n<user_message>\n\n</user_message>\n<sender …/>" }

即 raw 命令落地之后,worker 还会按 followUpContent 再提交一条 user_message 为空、只带路由提示的回合。同夹具去掉泄漏的 pendingCodexAppText(对照组)后 buildNewTopicCliInput 调用 0 次、无 follow-up。

🟠 P2-c /tw /cd、/tw /close 的作用对象与最终落地目录不一致

  • /tw /cd <别的目录>:命令打在建 worktree 之前的 pending 会话上(被 P1-b 的护栏拒掉,回一句假原因),随后 worktree 照建、workingDir 被 worktree 覆盖,用户以为切了目录其实没有。
  • /tw /close:关掉 pending 会话后 startAutoWorktreePending 仍然跑,探针里 runAutoWorktreeCommit 调用 1 次、会话已从 activeSessions 消失 ⇒ 白建一个 worktree 目录后丢弃。

建议至少在 PR 描述里写明「生命周期头之后的 daemon 命令作用于 worktree 之前的会话」这一语义;/close、/cd、/repo 这三种建议在 deferred 分支直接拒绝并说明,而不是执行后又被覆盖。

(/tw /repo … 不会到这里:topic-spec.ts 的相斥校验先拒。)

🟠 P2-d /th /tw + 纯管理命令会扣 1 条 message quota,额度耗尽时命令直接发不出去

/th /status: consumeQuota 调用 1 次
/status:     consumeQuota 调用 0 次(命令在闸之前执行)

配额耗尽(allow=false)时 /th /status 的实测回复只有 ⚠️ 这条消息没有送达 CLI,请重发一次,/status 输出根本没出来——不只是「少一格」,是纯管理命令被配额挡死且提示误导。建议 deferred daemon 分支带 skipCharge: true(evaluateTalk 与 prompt.submit 闸照跑)。

⚪ P3-1 /th 下非 cold-start 的 passthrough 绕过了 delivery 闸

/th /compact: forks=1, raw="/compact", 回复 "↪️ 已转入新话题,正在处理…"
/t  /compact: forks=0, 回复 "/compact 需要在已有会话内使用(先发一条普通消息启动 CLI)。"

即同一个 passthrough 命令在 /t 下是「需要已有会话」,加个 here 变成「冷启动起一个 CLI 再打字」。若是有意的请写进描述;若不是,把 lifecycle_passthrough 也按 slashDecision.delivery 分叉。

⚪ P3-2 新增用例咬不住本次的两个真实缺陷

我对新增用例做了三组变异:

变异 daemon-rename-route 新增 /th /status 用例
删掉 lifecycle_daemon deferred 返回 咬红(workingDir 断言)
整个 src/daemon.ts 回退到 master 咬红
只补 P1-a 的最小修复(initialStartPending=false) 仍绿 ← 缺陷仍在时也绿

所以它只钉住了「命令要在钉定目录之后执行」,钉不住「之后这个话题还能用」。建议补两条:
① P1-a:/th /status 后发一条真实任务 → forkWorker 1 次、queuedActivationTail 为空、initialStartPending=false(这条在我加最小修复前必须红);
② P2-a/b:/tw <命令> 后驱动真实 commitRepoSelection → pendingRepoSetup 里不含命令文本、buildNewTopicCliInput 未被调用(empty start),或 raw 场景下 pendingFollowUpInput 为 undefined。

⚪ P3-3 2d512f8bf 揭示的夹具脆弱性

test/new-topic-lark-caller.test.ts 切片执行 daemon 的 ds 字面量,新增的 lifecycleRawOpening 是自由变量 ⇒ 不补 deps 时该文件 10 条用例全红(我实测过)。2d512f8bf 补上了 deps,方向可接受;但更根本的是这段字面量不该读闭包变量——按「测试原则」这条属于需要 seam 的读源码用例,长期应把 ds 构建抽成可导入的函数。至少请在文件里保留一句说明,别让后来人以为这是随手补的 fixture。


建议修法(一处收口)

deferred daemon 命令的情形,让 ds 与既有「默认 /status 预建路径」同形:initialStartPending 从建会话起 false、pendingPrompt/pendingCodexAppText/两个 context 全部为空、skipCharge: true;命令仍在目录钉定(/th)或 journal 落盘(/tw)之后执行,或干脆按 P2-c 对 /cd /close /repo 直接拒绝。这样 P1、P2-a/b/c/d 一起消失,且 handleCommand 看到的是与默认路径一模一样的 command scratch。

影响面

改动只在新话题 slash 入口;/t 与不带变体的写法不变。happy path(/th /teach、/t /goal)我实测正常。但本次泄漏的 pendingCodexAppText 会穿过 pending-repo-journal → commitRepoSelection → codex-app sidecar 开场三处共用路径,属公共层,修完请连同 test/card-handler-repo-select.test.ts 的 raw 冷启动用例一起看一遍。

@deepcoldy

Copy link
Copy Markdown
Owner

感谢贡献!这个 PR 已由维护者另开 #3184 接管落地:保留了你的四个原提交和署名,在其上同步了最新 master,并按评审意见重做了 daemon 侧的收口(/th /tw 后接命令改走既有的冷启动透传和命令会话路径,/tw 后接 daemon 命令直接提示)。解析器那一侧保持你的原样。

#3184 合入后会关闭本 PR。如果你本地已经改了这部分,请在这里说一声,我们改用你的版本。

@deepcoldy

Copy link
Copy Markdown
Owner

本 PR 的改动已经随 #3184 合入 master(0d156c8ab),你的四个原提交内容都在,合入提交带有你的 Co-authored-by 署名。解析器一侧保持你的原样,daemon 侧按评审意见做了收口。

再次感谢贡献!这个 PR 先关闭,后续有问题欢迎在 #3184 或新 issue 里继续讨论。

@deepcoldy deepcoldy closed this Nov 8, 2026
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