Skip to content

fix(desktop): 用当前 generation 终态证据回收残留 activeTurn - #3383

Open
zqchris wants to merge 10 commits into
mainfrom
pi-stale-active-turn-terminal-proof
Open

fix(desktop): 用当前 generation 终态证据回收残留 activeTurn#3383
zqchris wants to merge 10 commits into
mainfrom
pi-stale-active-turn-terminal-proof

Conversation

@zqchris

@zqchris zqchris commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

这次改了什么

摘要

PI 任务正常结束后,后续 send_to_session / HANDOFF 会被错判成目标仍忙,消息只进队列,队列最终停在 paused。根因不是模型异常中断,而是 coordinator 残留了一份已派发的 activeTurn:live Session 和 desktop tracker 都已 idle,但 #3240 只覆盖「Session 已卸载」和「tracker 假忙」,漏了「只有 coordinator 残留」这一支。本 PR 让 Session 暴露当前 generation 是否已观察到产品终态;coordinator 只在这份证据为真时回收残留并继续派发。

变更类型

  • fix 缺陷修复
  • feat 新功能
  • refactor / perf 重构或性能优化
  • docs / test / chore 文档、测试或工程维护
  • 其他:

范围

  • 关联 Issue / 需求:内部反馈(PI 任务自己停下后,插队/排队消息进不去;fix(desktop): drain queued input after a stale idle turn #3240 修过后仍复现)
  • 本 PR 包含:用当前 generation 的终态观察事实回收 coordinator-only leftover activeTurn
  • 明确不包含:不猜更长 timeout;不改模型主动收口 / PI compaction / handoff「成功后结束当前 turn」;不自动 unpause crash-snapshot 恢复后的队列;不追查第一道 onTurnEvent(done) 为何被漏的全部入口
  • 用户可见变化:已结束的任务不再把后续 HANDOFF 永久卡在「队列暂停」;真正还在跑、或 PI agent_start 空窗中的排队语义不变
  • 是否存在 breaking change:无

UI 变化

  • 引用的设计规范:不涉及

怎么验证的

自动验证

pnpm --filter desktop run typecheck
pnpm --filter @cindy/maker-core run typecheck
结果:TYPECHECK_OK

pnpm --filter desktop exec vitest run src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
结果:315 passed

pnpm --filter @cindy/maker-core exec vitest run src/session-control.test.ts
结果:13 passed

pnpm test:unit:related
结果:desktop / maker-core / 相关依赖 workspace 全 PASS

当前 head checks(与本 PR 目标无关)

head 7bb441a9 当前 Windows unit tests (1/2) / 汇总 Windows unit tests 为 FAILURE。标注指向 packages/maker-core/src/agents/pi/__tests__/pi-subagent-runs.test.ts 的 launch fence 5s 超时。

这不是 generation 终态回收问题:

  • 本 PR diff 只动 7 个文件(coordinator / Session / register / 对应测试),不包含 pi-subagent-runs.test.ts
  • 同一 head 的 Linux unit tests (1/2)(2/2)、Windows unit tests (2/2)、Desktop Git integration、typecheck/verify、DCO、Greptile 均已通过
  • 按意图契约不修 PI launch fence / 5s timeout;若该红灯稳定复现,应独立 issue/独立 PR

手工验证

不涉及。本改动在 main 进程状态机,当前宿主无法用这份 worktree 热更正式包。

用户可执行验证:更新到含本 PR 的包后,等一个 PI 任务正常结束后立刻 send_to_session / 投递 HANDOFF,应直接开新 turn,而不是停在「任务已停止,队列暂停」。

未执行的验证

  • 未在正式包上复现并验证「已经 paused 的历史队列」会自动恢复。本 PR 只防新发生的永久冻结;已经按 crash-snapshot 保护策略 paused 的队列仍需用户点继续。
  • 未验证第一道 onTurnEvent(done) 为何没进 coordinator。那是另一条漏,本 PR 用 Session 终态证据做回收兜底。

风险

风险分类

  • 无已知风险
  • SQLite / migration
  • system prompt
  • 协议兼容
  • 权限 / 安全 / 用户数据
  • 存量插件兼容(批准状态 / 指纹 / manifest 校验 / 安装布局 / 包格式)
  • 原生层 / fingerprint / OTA
  • 跨平台差异
  • 其他:命中 maker-core 架构门(session.ts 只读终态观察 API)。该改动服务于回收判据,不是顺手重构。

影响与回滚

  • 影响范围:Desktop coordinator 回收 leftover activeTurn 的第三条路径;maker-core Session 新增只读方法。
  • 回滚 / 降级方式:整 PR revert。无 migration、无协议、无插件兼容变化。
  • 失效契约:账号 / 工作区 / 会话切换不新增缓存。新 Session incarnation 的 generation 从 0 起,旧终态观察不会跨 instance 复用。

提交前检查

  • 已 review 完整 diff
  • 每个 commit 都带 DCO 签名(git commit -s,见 DCO
  • UI 改动已在「UI 变化」注明引用的设计规范章节(不涉及 UI 则跳过)
  • 未提交凭证、令牌或授权文件
  • 已补充必要文档
  • 已确认测试结果或说明未执行原因

远程连接 / 手机版适配

不涉及。本 PR 只改本机 coordinator / Session 终态回收,不改 device-link wire protocol、不改 mobile runtime。

意图契约

  • 目标:在 live Session 已 idle、desktop tracker 也已 idle、但 coordinator 仍残留已派发 activeTurn 时,仅当当前 turn generation 已观察到终态事件,才回收该残留并继续派发队列。
  • 非目标:不猜 timeout;不改模型收口;不自动 unpause crash 恢复队列;不修第一道 done 漏投的全部入口;不改 PI agent_start 空窗。
  • 验收:有终态证据则回收并派发;无证据 / probe 不可用则 fail-closed;既有 Session gone 与 tracker 假忙路径保持。

…minal proof

When a PI turn already observed its product terminal, the live Session and
desktop tracker can both be idle while coordinator still holds a dispatched
activeTurn. send_to_session then keeps queueing, and no retry timer is armed.

Reclaim that leftover only when Session reports the current generation has
already fanned out a terminal. The PI prompt-accepted to agent_start gap stays
fail-closed.

Signed-off-by: zqchris <chrisz83@gmail.com>
@zqchris
zqchris requested a review from a team as a code owner August 25, 2026 07:58
@greptile-apps

greptile-apps Bot commented Aug 25, 2026

Copy link
Copy Markdown

Greptile Summary

本 PR 为 maker-core Session 增加按 generation 记录的终态证据,并让 Desktop coordinator 使用该证据回收已结束 turn 遗留的 activeTurn

  • 在 turn reservation 时将 vendor generation 绑定到 coordinator 的 active turn
  • 区分 done、terminal error 与无终态证据,并对错误继续走恢复流程
  • 对计划内 daemon upgrade 的关闭错误保留静默语义
  • 补充 generation、拒绝回滚、配对 done、探针不可用及错误恢复测试

Confidence Score: 5/5

当前未发现仍需阻止合并的故障,PR 看起来可以安全合并。

未发现仍然存在的阻塞性故障。

Important Files Changed

Filename Overview
packages/maker-core/src/session.ts 新增 generation 级终态快照、reservation 回调及拒绝派发时的终态证据恢复逻辑。
apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts 将 active turn 绑定到 vendor generation,并依据匹配的 done/error 证据执行残留回收或错误恢复。
apps/desktop/src/main/maker-ipc/register.ts 接入 Session 终态探针,并标记计划内 daemon upgrade 产生的静默 terminal error。
apps/desktop/src/main/maker-ipc/makerSendTransaction.ts 将 coordinator 的 reservation-generation 回调透传到 Session.send。
apps/desktop/src/main/maker-ipc/tests/agent-input-coordinator.test.ts 覆盖 generation 匹配、探针失败、配对 done、计划内关闭和错误恢复等 coordinator 场景。
packages/maker-core/src/session-control.test.ts 覆盖终态快照在连续派发、拒绝回滚和 reservation 窗口中的行为。

Sequence Diagram

sequenceDiagram
  participant C as Desktop Coordinator
  participant S as maker-core Session
  participant P as Provider
  C->>S: send(message, onTurnReserved)
  S->>S: reserve generation N
  S-->>C: onTurnReserved(N)
  S->>P: dispatch turn
  P-->>S: done / terminal error
  S->>S: record terminal evidence for N
  Note over C,S: coordinator callback may be missed
  C->>S: probe live state and terminal evidence
  alt done matches bound generation N
    C->>C: reclaim leftover activeTurn
    C->>C: continue draining queue
  else error matches bound generation N
    C->>C: enter recovery / retry state
  else evidence unavailable or mismatched
    C->>C: fail closed and retain activeTurn
  end
Loading

Reviews (10): Last reviewed commit: "fix: address review — keep planned-upgra..." | Re-trigger Greptile

@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: 1d50ffa354

ℹ️ 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 apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts Outdated
Observed terminal probes now carry done vs error. Successful leftover
reclaim still drains the queue; a current-generation terminal error keeps
active-turn recovery and Retry. A paired done tail cannot rewrite that error.

Signed-off-by: zqchris <chrisz83@gmail.com>

@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: 6b542024d4

ℹ️ 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/maker-core/src/session.ts Outdated
@MagicLizi

Copy link
Copy Markdown
Contributor

@zqchris 👋 这个 PR 还有 1 条 review conversation 没 resolve(packages/maker-core/src/session.ts),auto-review 因此暂时跳过、没法继续审查 / 合并。

如果你已经按评论改完或回应了,请到对应 thread 上点 Resolve conversation;全部 resolve 后,下一轮 auto-review 会自动重新审查这个 PR。

Cache the listener-redacted error text, and redact again when exposing the
snapshot or synthesizing onTurnEvent('error'). Recovery/Retry stays on the
error path; Authorization secrets must not reach the input projection.

Signed-off-by: zqchris <chrisz83@gmail.com>
@zqchris

zqchris commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Windows CI

Windows unit tests (2/2) 在 head 6b542024 红了,annotation 是:

  • packages/maker-core/src/agents/pi/__tests__/pi-subagent-runs.test.ts
  • launch fence > overlapping boundaries ... start in the same tick 两组
  • Test timed out in 5000ms(每组 16 次并发写 fence 文件)

这不是本 PR 回归:

  • 本 PR 只动 leftover activeTurn 终态回收(session.ts 终态观察 + coordinator 回收),没有改 PI launch fence 或该测试。
  • Linux 两片、Windows 1/2、Desktop Git integration 全绿。
  • 汇总 job Windows unit tests 3 秒失败,是因为 2/2 已经失败。

按范围纪律不把无关 Windows 超时并进本 PR。新 head 会再跑一轮 CI。

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

ℹ️ 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 apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts Outdated
Comment thread packages/maker-core/src/session.ts Outdated
@MagicLizi MagicLizi added the touches:core 改动碰到架构核心路径(review-pr 自动维护,仅展示) label Aug 25, 2026
@MagicLizi

Copy link
Copy Markdown
Contributor

@zqchris 👋 这个 PR 还有 2 条 review conversation 没 resolve(apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts / packages/maker-core/src/session.ts),auto-review 因此暂时跳过、没法继续审查 / 合并。

如果你已经按评论改完或回应了,请到对应 thread 上点 Resolve conversation;全部 resolve 后,下一轮 auto-review 会自动重新审查这个 PR。

Capture the dispatched maker-core generation on activeTurn and only accept
matching terminal evidence. Error snapshots now keep reason/sdkError/status
so empty-response can still auto-resume, without leaking Authorization text.

Signed-off-by: zqchris <chrisz83@gmail.com>

@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: 69a77e21cc

ℹ️ 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/maker-core/src/session.ts Outdated
Comment thread apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
Comment thread packages/maker-core/src/session.ts
Ignore background terminals, bind vendor generation at Session reservation,
and restore the snapshot when a later send rolls back. Reclaim still requires
a matching foreground done/error; Authorization text stays redacted.

Signed-off-by: zqchris <chrisz83@gmail.com>

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

ℹ️ 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/maker-core/src/session.ts Outdated
A prior-generation done/error can arrive while a later send is reserved and
then rejected. Promote that window snapshot on rollback so leftover
activeTurn can still reclaim by terminal type.

Signed-off-by: zqchris <chrisz83@gmail.com>

@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: 2d152b6731

ℹ️ 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 apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts Outdated
@MagicLizi MagicLizi added the status:awaiting-bot-review 等外部审查机器人表态(review-pr 自动维护,仅展示) label Aug 25, 2026
If the coordinator misses a terminal error, the paired done must not clear
leftover activeTurn as success. Same-generation error snapshots go through
existing recovery/Retry; mismatched generations stay fail-closed.

Signed-off-by: zqchris <chrisz83@gmail.com>
@zqchris

zqchris commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Windows pi-subagent-runs launch fence 超时仍按原判断:不是本 PR 改动路径,不并入这次修复。当前 head 的 checks 让它自己收口即可。

@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: 8997ebcde5

ℹ️ 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 apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts Outdated
Comment thread apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@MagicLizi MagicLizi removed the status:awaiting-bot-review 等外部审查机器人表态(review-pr 自动维护,仅展示) label Aug 25, 2026
Write the steer-start vendor generation onto accepted activeTurn instead of
re-reading after ACK. Treat a missing or throwing terminal probe as
unavailable and fail-closed, distinct from an explicit none snapshot.

Signed-off-by: zqchris <chrisz83@gmail.com>

@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: 2ce6d76d33

ℹ️ 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/maker-core/src/session.ts
Do not adopt a reserved generation until provider dispatch, and keep
error-over-done scoped to the same generation. A settled turn late
terminal cannot become N+1 leftover error evidence.

Signed-off-by: zqchris <chrisz83@gmail.com>

@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: 3f3218bc46

ℹ️ 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 apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
Record the host-suppressed remote_daemon_closed disposition when register
skips onTurnEvent(error). Paired done and leftover reclaim must not
re-synthesize that snapshot as a Retry banner.

Signed-off-by: zqchris <chrisz83@gmail.com>

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

ℹ️ 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 apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@zqchris

zqchris commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

当前 head 7bb441a9Windows unit tests (1/2) 红灯仍按原判断:pi-subagent-runs launch fence 超时,不在本 PR 改动路径(diff 不含该测试)。Linux 两分片、Windows (2/2)、Desktop Git integration 已通过。不把这次红 check 当成 generation 回收问题,也不改 PI launch fence / 5s timeout。live 线程 unresolved=0。

@MagicLizi MagicLizi added the status:ci-failed CI 失败(review-pr 自动维护,仅展示) label Aug 25, 2026
@zqchris

zqchris commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

stall 再核对(live head 仍是 7bb441a9):

  • Windows unit tests (1/2) / 汇总 Windows unit tests 仍 FAILURE;Linux 两分片、Windows (2/2)、Desktop Git integration、DCO、Greptile、verify 已通过。
  • 本 PR diff 仍是 7 个文件(coordinator / Session / register / 对应测试),不含 packages/maker-core/src/agents/pi/__tests__/pi-subagent-runs.test.ts
  • live unresolved=0。

结论不变:红灯是 launch fence 并发超时,不属于 generation 终态回收目标,不并入本 PR。若该超时稳定复现,应独立 issue/PR。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:ci-failed CI 失败(review-pr 自动维护,仅展示) touches:core 改动碰到架构核心路径(review-pr 自动维护,仅展示)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants