Skip to content

feat(host): 阶段 4 Batch 4F-1 由 Host 受理和续跑 Goal - #15924

Open
dashhuang wants to merge 2 commits into
mainfrom
feat/host-stage4-batch4f1-goal-runtime
Open

dashhuang wants to merge 2 commits into
mainfrom
feat/host-stage4-batch4f1-goal-runtime

Conversation

@dashhuang

@dashhuang dashhuang commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

这次改了什么

摘要

这是阶段 4(本机桌面界面与后台分离)的 Batch 4F-1。桌面以客户端模式连接本机 cindy-host 时,桌面和 Host 都不运行 Goal 控制器,Goal 完全用不了。本批让 Host 成为 Goal 唯一的运行端,客户端模式下可以设置目标并查看状态,窗口关掉后已受理的 Goal 由 Host 继续推进。

  • Host 运行 Goal:Host 装配一份 Goal 控制器,复用桌面零 Electron 的控制器和裁决逻辑(apps/host/src/agent/goal-runtime.ts)。session_goals 只由 Host 写入。
    • 派发走 Host 输入协调器原有的 command → route lock 顺序。首轮真正受理后才写 active,终态按实际受理的回合身份关联。
    • 续轮要等持久结算完成,再由输入协调器安排。排队或结算期间不会抢发下一轮。
    • 普通 Stop 在 abort 之前先撤掉 Goal 的续跑资格,迟到的终态不会再续一轮。
  • 两条通道和一条推送:
    • maker:goal:get-status:只读,不调用 resumeOnOpen。冷启动和打开窗口都只展示已存状态,不认领旧存档、不发轮。
    • maker:goal:set:显式设置目标,是取得续跑资格的唯一入口。参数、回执和限额语义与桌面一致。
    • maker:goal:status-changed:带账号戳。桌面只转发给当前账号的可信窗口,配对 Web 只看到隐藏事件。
  • 写入准入:session_goals 的新建、更新、清除共用同一个写入口。构造完 SQL 后复核原 owner、DB、hold 代次和有效期,再在同一同步段执行 .run()。ended 写入前还要确认输入运行时没有开始排空或已关闭。切号或 hold 期间的旧回调写不进去,也不发推送。
  • 未结算的 Goal 不会被覆盖:Goal 还欠一次持久化 ended 时,同一任务保留它的标记。IM、普通发送和重放打开回合前都先检查这一点,不会抢在结算前开新回合。用户 resume 先重试原来的结算,协调器仍拒绝时返回 409,保留待恢复状态。
  • 自动恢复:按已定口径,窗口关掉后只继续已经开始的 Goal,不自动启动暂停或未批准的 Goal。
  • 默认桌面模式(嵌入)继续由桌面持有 profile,运行原来的 Goal 控制器,行为不变。

变更类型

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

范围

  • 关联 Issue / 需求:阶段 4 Host 客户端模式路线图 Batch 4F-1(Goal 运行时迁到 Host 的第一步)。
  • 本 PR 包含:Host Goal 运行时与写入准入,get-status / set 两条通道和状态推送,Stop、hold、切号与退役的接线,桌面端在客户端模式下的推送转发,以及通道覆盖清单同步。
  • 明确不包含:
    • clear / pause / resume / update 四条写口和 /goal 命令的客户端子集,留给后续批次。
    • 冷启动后自动恢复存档 Goal:保持观测,等用户显式操作重新授权。
    • 既有问题:模型组切换说明的实际 INSERT 仍缺提交前复核。本批只在 runtime-selection.ts 入口加了复核,完整修复需要把守卫一路传到 messageWrite.ts 去重查询之后的 INSERT,留后续。
    • 后续补偿:Host 已提交 started、尚未进入 SDK 派发时进入 hold,本次请求返回 409、不写 Goal 行和消息,但共享 SQLite 的 started 标记可能残留到 hold 交还之后。补偿需要交还后重新绑定原 owner 和确切的 started 值再做条件写,避免误收尾桌面或新一代已开始的回合,留后续。
  • 用户可见变化:
    • 客户端模式下可以设置 Goal、查看 Goal 状态,状态变化实时刷新。
    • 关掉窗口后已受理的 Goal 继续推进,重新打开后看到最新状态。
    • 默认桌面模式不变。
  • 是否存在 breaking change:无。

UI 变化

不涉及。复用现有的 Goal 界面和 payload,没有新增文案或组件。

怎么验证的

自动验证

全部使用隔离 HOME、系统默认 TMPDIR、LANG=zh_CN.UTF-8,取消了 LC_ALL、LC_CTYPE 和 XDT_OFFICIAL_SHARED_PROFILE。HTTP 只走本机回环,使用临时 SQLite 和替身 SDK,没有启动真实 Electron 窗口。

作者在 8b9f570a8cdc(基于 11fd88a5d3d3)上跑的全量门禁:

maker-shared build + vitest
结果:通过;135 files / 3360 通过
pnpm --filter @cindy/host typecheck;输入、Stop、IM、Goal 定向;test:ipc-route
结果:通过;471 pass / 1 基点已有 skip;2 PASS
apps/desktop 四个 tsconfig 类型检查;Goal、handoff、DB 定向
结果:均通过;11 files / 548 通过
通道覆盖清单 --check / check:engineering-guards / check:test-hygiene / git diff --check
结果:unresolved=0 / 12 strict 0 违规 / PASS / 通过

变基到 f4ed831f8ba9(6A 合入后)之后重跑:

冲突:apps/host/src/main.ts(6A 与本批各加一行 binding,两行并存)、通道覆盖清单(用脚本重新生成)
结果:contract=266,client=2,unresolved=0,--check 通过
maker-shared build;hostIpcRegistry 指纹测试
结果:通过;10/10 通过
pnpm --filter @cindy/host typecheck;batch4f-goal + input-queue 定向;test:ipc-route
结果:通过;471 pass / 1 基点已有 skip;2 PASS
apps/desktop:tsconfig.main / renderer / main-tests / main-tests-b 类型检查
结果:均通过
apps/desktop 定向 vitest(goal-host、sessionActiveTurn、goalHandlers、额度与中断自动恢复、sessionsUpdate)
结果:12 files / 853 通过
check:engineering-guards / check:test-hygiene / check-i18n / git diff --check / DCO
结果:12 strict 0 违规,8 ratcheted 在基线内 / PASS(472 files)/ 通过 / 通过 / 通过

新增或改写的测试(工程约定 §3.2),主要在 apps/host/src/ipc-bindings/batch4f-goal.test.mjs,走生产 Host HTTP、生产运行时选路和输入协调器锁序:

  • 首轮受理后断开窗口,Host 继续推进;打开连接和查询旧存档都不派发。
  • 排队和持久结算期间不抢发;Stop 后迟到的终态不续轮。
  • 用 SQLite TEMP trigger 拒绝 ended:IM 和普通发送都在 SDK 派发前被拒,started/ended 原值保留;resume 完成旧 Goal 后 IM 正常发送、只派发一次;协调器持续拒绝结算时 resume 返回 409。
  • 在 ended 写等待中、UPDATE builder 内以及重试写 .run() 之前触发 hold、Stop 或真实切换 DB owner:旧库 ended 保持 NULL,turnsUsed 为 0,零 SDK 派发、零 Goal 推送。普通 Stop 的合法 ended 写入正常。
  • 新增回环另外在未设置 TMPDIR 的环境复跑 16/16。

变异:三轮返修的每处修复都做过真实变异,撤掉后对应用例变红,逐字节复原后再变绿(首轮受理前写 active、Stop 前撤续跑资格、等持久结算、同步 SQL 改回 thenable、派发前代次复核、绕过 route lock、marker 保护、resume 拒绝、drain/scope 守卫等)。

独立复审共 3 轮:

  • 前 2 轮提出的 P1 和 P2-1/P2-3 均已修复,并复验关闭。
  • 第 3 轮结论 APPROVE,0 P0/P1/P2。复审方在固定副本上跑原回环 16/16,并自补 4 条反例(原 SDK Session 关闭后持锁重放、重试写 .run() 前触发 hold 和真实切换 DB owner),均通过。

读写开销:

  • get-status:每次调用读一次 Goal 行,零写入。
  • set:一次 route lock 内的派发,写一次 Goal 行和一条目标消息。
  • 每轮终态:写一次计数和状态;完成时写完成消息并清理 Goal 行。每次状态变化发一条推送。
  • 没有新增轮询或常驻订阅;Goal 的 timer 和监听都绑定 owner 生命周期,切号或关停即撤销。

手工验证

不涉及。日常登录会话里不拉起真实窗口,客户端模式的真机走查统一放在 Batch 7。

未执行的验证

  • 没有在真实 Electron 窗口里验证,放到 Batch 7。
  • 范围一节列出的两项既有/后续问题本批未处理。

风险

风险分类

  • 无已知风险
  • SQLite / migration
  • system prompt
  • 协议兼容
  • 权限 / 安全 / 用户数据
  • 存量插件兼容(批准状态 / 指纹 / manifest 校验 / 安装布局 / 包格式)
  • 插件引擎(ACP)与插件模型影响面(新任务 / 模型选择器 / 权限 / 多端 / 引擎枚举分支)
  • 原生层 / fingerprint / OTA
  • 跨平台差异
  • 其他:

影响与回滚

  • 影响范围:客户端模式下的 Goal 设置、状态查询、自动续轮和状态推送。默认桌面模式不变。
  • 协议:新增 maker:goal:get-status、maker:goal:set 两条 Host 合同通道,参数和回执沿用桌面原有 payload。状态推送对配对 Web 隐藏。Device Link 和其他 wire 合同不变。
  • 数据:没有 schema 改动。客户端模式下 session_goals 只由持有 profile 的 Host 写,嵌入模式仍由桌面写。
  • 回滚 / 降级方式:直接回滚本提交,客户端模式回到 Goal 不可用的状态,已存的 Goal 行保持原样,没有需要清理的数据。

提交前检查

  • 已 review 完整 diff
  • 每个 commit 都带 DCO 签名(git commit -s,见 DCO)
  • UI 改动已在「UI 变化」注明引用的设计规范章节(不涉及 UI 则跳过)
  • 未提交凭证、令牌或授权文件
  • 已核对受影响的文档,行为变化涉及的旧结论已同步修订(不涉及则无需修改文档)
  • 已确认测试结果或说明未执行原因
  • 涉及数据读取、通知后刷新、界面频繁写入或常驻组件时,已在「怎么验证的」写明:每个用户动作
    和每次通知触发几次 list/扫描/IPC/写事务(读远程数据再写远程调用次数和回包字节),以及隐藏或
    卸载后是否停止(见 docs/dev-rules/engineering-conventions.md §8;远程读取同时见
    docs/dev-rules/server-channel-review.md)
  • 如用户操作、界面入口或报错发生变化,已同步 apps/docs 或说明自动同步会从哪些来源更新。(没有新增入口或文案,只让客户端模式下原有的 Goal 入口可用)

Assisted-by: Cindy (https://cindy.app)


Note

High Risk
Touches session input dispatch, durable SQLite writes, and account/hold boundaries for autonomous multi-turn goals—errors could corrupt goal state or allow stale owners to write or continue runs.

Overview
Moves Goal execution to the Host in desktop client mode: new createHostGoalRuntime wires the shared GoalController through the input coordinator’s route lock, turn markers, and owner-scoped synchronous SQL commits. maker:goal:get-status is read-only observation; maker:goal:set is the only path that starts a goal via new startGoal, which persists an active row only after the first send is accepted and settles terminals through withGoalTurn / turn reservations.

Refactors the controller for Host admission: injectable onIntervention and pendingHandoff, settleTerminal / afterTurnPersistence, event filtering by session instance and turn generation, and managesGoal for Stop-time pause. Storage and local DB writes gain optional assertCommit (.run() under the same lease) for goals, messages, handoff consumption, and durable turn end markers.

Desktop bootstrap now forwards maker:goal:status-changed pushes with owner-stamp checks; IPC registry adds the maker-goal contract family. Goal limit defaults move to goal-host/settings.ts. Extensive Host HTTP integration tests cover hold, account switch, settlement retries, and marker ownership.

Reviewed by Cursor Bugbot for commit e34068b. Bugbot is set up for automated code reviews on this repo. Configure here.

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
@dashhuang
dashhuang requested a review from a team as a code owner December 5, 2026 23:22
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T23:25:34.772260Z e34068b 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.

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

ℹ️ 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 on lines +3004 to +3006
await this.trackPersistence(
dispatchBoundary, this.deps.storage.upsert(acceptedState),
() => this.deps.persistUserMessage?.(sessionId, state.objective, { goalObjective: { updated: false } }),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the objective message's pre-dispatch ordering

When the provider emits output or finishes before session.send() returns, the Host can persist the assistant response before this objective message is created. This is the fast-terminal scenario already exercised by batch4f-goal.test.mjs. Because goal-runtime.ts supplies no createdAt, writeMessageRow() timestamps the objective at insertion time, and chatHistoryReader.ts orders messages by creation time and rowid. Consequently, reopening the conversation displays the goal-setting user message after the response it prompted. Preserve a pre-dispatch timestamp or reserve the message's ordering before dispatch while still deferring its publication until acceptance.

Useful? React with 👍 / 👎.

@zqchris zqchris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Auto-approved by Chris’s trusted-author rule for makecindy/cindy. This is an automated approval.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Confidence Score: 3/5RetriggerView in Greptile

[Medium impact] The PR expands Goal execution to client mode on the local Host.

合并前需要修复首轮保存失败后的 Goal 归属丢失,以及输入解锁后目标不再推进的问题。

Findings

  1. P1 保存失败后模型仍在运行 ▶
  2. P1 解锁后目标不再推进 ▶
Fix with agent prompt
### Issue 1
apps/desktop/src/main/goal-host/controller.ts:3004-3008
首轮已被模型受理后,如果 `session_goals` 或目标消息写入失败,`firstAccepted` 仍为 false,`startGoal` 会通过 `stopSession` 删除监听和续跑状态。但输入协调器已收到 `noteAccepted`,不会取消模型回合。用户会看到设置失败,模型却继续工作;若只有目标消息写入失败,还会留下无人推进的 active Goal。

请分开记录“模型已受理”和“保存完成”。保存失败时保留可重试的写入及 Goal 归属,或明确停止已受理的回合。

### Issue 2
apps/host/src/agent/input-queue.mjs:269-271
Host 只在回合结算后调用 `goalInputIdle`,没有在输入锁解除时唤醒 Goal。如果续跑计时器触发时仍有交互锁,`ensureInputReady` 返回 false,这次续跑机会就被消耗掉。之后 `setInteractionLock(..., false)` 只更新队列,没有新回合触发唤醒,Goal 会一直停在 active。

桌面原实现会在队列不再阻塞时通知 Goal。请为 Host 补上同样的解锁唤醒,并保留未结算回合的检查。

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

本 PR 让 Host 受理并继续推进显式设置的 Goal。

  • The Host keeps accepted Goals running after the desktop window closes.
  • Goal turns wait for saved endings before another input takes over.

已知范围:dashhuang 明确将模型组切换说明 INSERT 的提交前检查,以及 hold 后残留 started 标记的补偿留待后续,本次不重复提出。

验证方式:静态阅读,未运行测试或真实窗口。

Diagram

%%{init: {'theme': 'neutral', 'htmlLabels': false}}%%
flowchart TD
  A[显式设置 Goal] --> B[取得命令锁和路由锁]
  B --> C[模型受理首轮]
  C --> D[保存 Goal 和目标消息]
  D --> E[接收对应回合终态]
  E --> F[等待历史和结束标记保存]
  F --> G[等待输入协调器结算]
  G --> H[更新 Goal 状态]
  H --> I{是否继续}
  I -->|是| B
  I -->|否| J[停止或完成]
  K[用户 Stop] --> L[先撤销续跑资格]
  L --> M[取消模型回合]
Loading

Reviews (1) · Last reviewed commit: "feat(host): 阶段 4 Batch 4F-1 由 Host 受理和续跑..." · Reviewed by Greptile

Comment on lines +3004 to +3008
await this.trackPersistence(
dispatchBoundary, this.deps.storage.upsert(acceptedState),
() => this.deps.persistUserMessage?.(sessionId, state.objective, { goalObjective: { updated: false } }),
);
firstAccepted = true;

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 保存失败后模型仍在运行

首轮已被模型受理后,如果 session_goals 或目标消息写入失败,firstAccepted 仍为 false,startGoal 会通过 stopSession 删除监听和续跑状态。但输入协调器已收到 noteAccepted,不会取消模型回合。用户会看到设置失败,模型却继续工作;若只有目标消息写入失败,还会留下无人推进的 active Goal。

请分开记录“模型已受理”和“保存完成”。保存失败时保留可重试的写入及 Goal 归属,或明确停止已受理的回合。

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop/src/main/goal-host/controller.ts
Line: 3004-3008

Comment:
**保存失败后模型仍在运行**

首轮已被模型受理后,如果 `session_goals` 或目标消息写入失败,`firstAccepted` 仍为 false,`startGoal` 会通过 `stopSession` 删除监听和续跑状态。但输入协调器已收到 `noteAccepted`,不会取消模型回合。用户会看到设置失败,模型却继续工作;若只有目标消息写入失败,还会留下无人推进的 active Goal。

请分开记录“模型已受理”和“保存完成”。保存失败时保留可重试的写入及 Goal 归属,或明确停止已受理的回合。

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment on lines +269 to +271
if (settled && !closing && !profileHold && isCurrent()) {
if (goalTurn) executor.confirmGoalSettlement(id, goalTurn);
else if (terminal) deps.goalInputIdle?.(id);

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 解锁后目标不再推进

Host 只在回合结算后调用 goalInputIdle,没有在输入锁解除时唤醒 Goal。如果续跑计时器触发时仍有交互锁,ensureInputReady 返回 false,这次续跑机会就被消耗掉。之后 setInteractionLock(..., false) 只更新队列,没有新回合触发唤醒,Goal 会一直停在 active。

桌面原实现会在队列不再阻塞时通知 Goal。请为 Host 补上同样的解锁唤醒,并保留未结算回合的检查。

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/host/src/agent/input-queue.mjs
Line: 269-271

Comment:
**解锁后目标不再推进**

Host 只在回合结算后调用 `goalInputIdle`,没有在输入锁解除时唤醒 Goal。如果续跑计时器触发时仍有交互锁,`ensureInputReady` 返回 false,这次续跑机会就被消耗掉。之后 `setInteractionLock(..., false)` 只更新队列,没有新回合触发唤醒,Goal 会一直停在 active。

桌面原实现会在队列不再阻塞时通知 Goal。请为 Host 补上同样的解锁唤醒,并保留未结算回合的检查。

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit e34068b. Configure here.

if (isCurrentDispatch()) {
this.emit(acceptedState);
opts.first.publishTerminal();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Failed set leaves an active Goal

High Severity

The provider send is marked accepted before the Goal row and objective message finish committing. If that later write fails, startGoal treats the attempt as unsuccessful and stopSession only detaches the listener. A committed session_goals row stays active, and the running turn is not abandoned, so the Goal never advances.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e34068b. Configure here.

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>

This branch has not been deployed

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