Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(bots): stop waking the follower on every intermediate turn end

Chris46743a16c261
diff --git a/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts b/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
index d1a5966332..3adae3010b 100644
--- a/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
+++ b/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
@@ -14,7 +14,8 @@
* 停止被当成做完叫醒伙伴;首次安装时事件赶在 ensure 之前发布、accepted=0 后永久丢失;伙伴主任务的欢迎轮出错
* 后宿主把 errored 发给它自己,排在正在失败的主任务后面打转;启动时第一次读归属失败,补快照里已出错的任务被按
* 「没人跟」结清、之后归属读到也不再发;内置规则落地失败仍发布(accepted=0 + 回执)后永久丢失;
- * 120 字符的合法伙伴 id 拼出超过 128 字符的规则 id、永远没有内置规则。
+ * 120 字符的合法伙伴 id 拼出超过 128 字符的规则 id、永远没有内置规则;PR 任务每处理一轮评审就发一次
+ * finished、逐轮叫醒伙伴模型(实测 6 小时 57 次唤醒里 49 次是 finished)。
* - tier:unit(真实引擎 + 内存 load/save;状态表 IO 为内存 fake;时间由 now() 推进)。
*/
import type { SessionActivitySnapshot } from '@cindy/maker-shared/session-activity';
@@ -568,6 +569,10 @@ it.each([false, true])('keeps a fast delegation completion across delayed owners
t.advance(30_000);
await vi.advanceTimersByTimeAsync(30_000);
}
+ // finished 等安静期满才发:期满前不叫醒,期满后由扫描发出一次。
+ expect(publish).not.toHaveBeenCalled();
+ t.advance(AUTONOMY_THRESHOLDS.finishedQuietMs);
+ await vi.advanceTimersByTimeAsync(30_000);
expect(publish).toHaveBeenCalledOnce();
expect(publish).toHaveBeenCalledWith(HOST_SESSIONS_SOURCE_ID,
expect.objectContaining({ type: 'finished', subject: 'child' }), expect.any(Function));
@@ -603,7 +608,8 @@ it.each(['ensure', 'publish'] as const)('retries the last completion after %s fa
await runtime.watch.settled();
runtime.watch.applyActivity('child', { ...running('child'), phase: 'completed' } as SessionActivitySnapshot);
await runtime.watch.settled();
- await vi.advanceTimersByTimeAsync(0);
+ t.advance(AUTONOMY_THRESHOLDS.finishedQuietMs);
+ await vi.advanceTimersByTimeAsync(30_000);
expect(runtime.watch.needsSweep()).toBe(false);
failing = false;
publish.mockClear();
@@ -629,3 +635,39 @@ it.each(['ensure', 'publish'] as const)('retries the last completion after %s fa
vi.useRealTimers();
}
});
+
+// Unit / bot follow-up wake storm: a PR task handles one review round per turn, and every
+// turn end used to publish `finished` and wake the teammate's model (49 of 57 wakes in 6h).
+// Rounds that resume within the quiet period are superseded; the final round still wakes once.
+it('wakes the follower once for the final round, not for every intermediate turn end', async () => {
+ vi.useFakeTimers();
+ const t = await setup({ schedulerOff: true, creators: { child: 'a' } });
+ const publish = vi.fn<AutonomyEngine['publish']>(async () => ({ accepted: 1, duplicate: false }));
+ const runtime = t.create({ registerSource: vi.fn(), publish, ensureBuiltin: vi.fn(async () => ({} as never)) });
+ const turn = (phase: 'running' | 'completed') => ({
+ ...running('child'), phase, startedAtMs: t.now(), lastActivityAtMs: t.now(),
+ } as SessionActivitySnapshot);
+ try {
+ await runtime.start();
+ for (let round = 0; round < 3; round += 1) {
+ runtime.watch.applyActivity('child', turn('running'));
+ t.advance(60_000);
+ await vi.advanceTimersByTimeAsync(1_000);
+ runtime.watch.applyActivity('child', turn('completed'));
+ t.advance(AUTONOMY_THRESHOLDS.finishedQuietMs / 2);
+ await vi.advanceTimersByTimeAsync(30_000);
+ }
+ expect(publish).not.toHaveBeenCalled();
+ const finalSince = t.now() - AUTONOMY_THRESHOLDS.finishedQuietMs / 2;
+ t.advance(AUTONOMY_THRESHOLDS.finishedQuietMs / 2);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(publish).toHaveBeenCalledOnce();
+ expect(publish).toHaveBeenCalledWith(HOST_SESSIONS_SOURCE_ID,
+ expect.objectContaining({ type: 'finished', id: `finished:child:${finalSince}` }), expect.any(Function));
+ await vi.advanceTimersByTimeAsync(120_000);
+ expect(publish).toHaveBeenCalledOnce();
+ } finally {
+ runtime.stop();
+ vi.useRealTimers();
+ }
+});
diff --git a/apps/desktop/src/main/bot-autonomy/__tests__/taskWatch.test.ts b/apps/desktop/src/main/bot-autonomy/__tests__/taskWatch.test.ts
index 6d57d877cf..e42bd42941 100644
--- a/apps/desktop/src/main/bot-autonomy/__tests__/taskWatch.test.ts
+++ b/apps/desktop/src/main/bot-autonomy/__tests__/taskWatch.test.ts
@@ -494,9 +494,10 @@ it('captures completion execution before asynchronous reads or a later turn', as
h.watch.applyActivity('child', activity('child', 'completed', { turnGeneration: null }));
execution = { instanceId: 'native-2', generation: 2 };
await h.watch.settled();
- const [candidate] = new AutonomyEventLedger().evaluate(h.watch.list(), h.now()).due;
+ const quietAt = h.now() + AUTONOMY_THRESHOLDS.finishedQuietMs;
+ const [candidate] = new AutonomyEventLedger().evaluate(h.watch.list(), quietAt).due;
expect(candidate).toBeDefined();
- const saved = JSON.parse(JSON.stringify(toRoutineEvent(candidate, h.now())));
+ const saved = JSON.parse(JSON.stringify(toRoutineEvent(candidate, quietAt)));
expect(saved.data).toMatchObject({ executionInstanceId: 'native-1', executionGeneration: 1 });
h.watch.applyActivity('child', activity('child', 'running', { turnGeneration: 2 }));
await h.watch.settled();
diff --git a/apps/desktop/src/main/bot-autonomy/events.ts b/apps/desktop/src/main/bot-autonomy/events.ts
index 18dbb3703a..21823bbea3 100644
--- a/apps/desktop/src/main/bot-autonomy/events.ts
+++ b/apps/desktop/src/main/bot-autonomy/events.ts
@@ -1,7 +1,7 @@
/**
* 伙伴自主跟进 · 事件判定(程序级,不经模型)。
*
- * 从状态表判定 stuck / waiting / errored / usage-limit / finished,发布到例行任务引擎的宿主
+ * 从状态表判定 stuck / waiting / errored / usage-limit / finished(做完后安静满 finishedQuietMs),发布到例行任务引擎的宿主
* 内置事件源 `host:sessions`。去重:同一任务同一 (类型, phaseSince) 只发一次(事件 id 天然唯一,
* 引擎另有 24h 回执去重);防打转:同一任务同一类事件 24h 内最多
* `AUTONOMY_THRESHOLDS.maxEventsPerSessionPerDay` 次,超出只记日志。
@@ -77,14 +77,30 @@ export function dueEventType(row: WatchedSession, now: number): AutonomyEventTyp
case 'usage-limit':
return 'usage-limit';
case 'finished':
- // 自动化运行的任务有自己的结果通知,「做完」不再叫醒伙伴;卡住 / 等你 / 出错照常。
- // 伙伴主任务的一轮回复做完不是「任务做完」:否则别的伙伴每回一句话都会叫醒总调度。
- return row.source === 'routine' || row.canonicalOfBotId ? null : 'finished';
+ return emitsFinished(row) && now - row.phaseSince >= AUTONOMY_THRESHOLDS.finishedQuietMs ? 'finished' : null;
default:
return null;
}
}
+/**
+ * 自动化运行的任务有自己的结果通知,「做完」不再叫醒伙伴;卡住 / 等你 / 出错照常。
+ * 伙伴主任务的一轮回复做完不是「任务做完」:否则别的伙伴每回一句话都会叫醒总调度。
+ */
+function emitsFinished(row: WatchedSession): boolean {
+ return row.source !== 'routine' && !row.canonicalOfBotId;
+}
+
+/** 本机任务做完、finished 还没发:这一行由扫描到点判定(发出或结清后即离表)。 */
+export function awaitsFinishedEvent(row: WatchedSession): boolean {
+ return row.online && row.deviceId === null && row.phase === 'finished' && emitsFinished(row);
+}
+
+/** 还在安静期内:这一行要留在表里,期满再判定。 */
+export function awaitsFinishedQuiet(row: WatchedSession, now: number): boolean {
+ return awaitsFinishedEvent(row) && now - row.phaseSince < AUTONOMY_THRESHOLDS.finishedQuietMs;
+}
+
export interface AutonomyEventCandidate {
id: string;
type: AutonomyEventType;
diff --git a/apps/desktop/src/main/bot-autonomy/runtime.ts b/apps/desktop/src/main/bot-autonomy/runtime.ts
index 9b6e272ebf..fb0495d4a2 100644
--- a/apps/desktop/src/main/bot-autonomy/runtime.ts
+++ b/apps/desktop/src/main/bot-autonomy/runtime.ts
@@ -22,6 +22,8 @@ import {
import { FOLLOW_UP_BUILTIN_VERSION, buildFollowUpRoutineInput, builtinFollowUpRoutineId } from './builtinRoutine.js';
import {
AutonomyEventLedger,
+ awaitsFinishedEvent,
+ awaitsFinishedQuiet,
hostSessionsSource,
isPendingForFallback,
sweepEventId,
@@ -123,7 +125,8 @@ export class BotAutonomyRuntime {
}
private needsSweep(): boolean {
- return this.watch.needsSweep() || this.deferredEvents.size > 0;
+ return this.watch.needsSweep() || this.deferredEvents.size > 0
+ || this.watch.list().some(awaitsFinishedEvent);
}
stop(): void {
@@ -299,7 +302,9 @@ export class BotAutonomyRuntime {
}
const unsettled = new Set(due.filter(candidate => !this.ledger.isSettled(candidate.id)).map(candidate => candidate.row.sessionId));
for (const row of rows) {
- if (row.phase === 'finished' && !unsettled.has(row.sessionId)) this.watch.retireFinished(row.sessionId, row.phaseSince);
+ if (row.phase === 'finished' && !unsettled.has(row.sessionId) && !awaitsFinishedQuiet(row, now)) {
+ this.watch.retireFinished(row.sessionId, row.phaseSince);
+ }
}
this.pruneReminded(now);
this.syncSweepTimer();
diff --git a/apps/desktop/src/main/bot-autonomy/taskWatch.ts b/apps/desktop/src/main/bot-autonomy/taskWatch.ts
index 182e379cb1..d4e76647b4 100644
--- a/apps/desktop/src/main/bot-autonomy/taskWatch.ts
+++ b/apps/desktop/src/main/bot-autonomy/taskWatch.ts
@@ -36,8 +36,8 @@ import {
truncateSummary,
} from './phase.js';
-/** 做完的行在表里停留多久(之后只留在「今天做完」计数里)。 */
-export const FINISHED_ROW_RETENTION_MS = 15 * 60_000;
+/** 做完的行在表里停留多久(之后只留在「今天做完」计数里);须长于 finished 的安静期,否则事件来不及判定。 */
+export const FINISHED_ROW_RETENTION_MS = AUTONOMY_THRESHOLDS.finishedQuietMs + 15 * 60_000;
/** 伙伴、委派与接手关系的重读间隔。 */
export const OWNERSHIP_REFRESH_MS = 60_000;
/** 设备目录(服务端 HTTP)重读间隔;轮询本身按 remotePollMs。 */
diff --git a/apps/desktop/src/shared/botAutonomy.ts b/apps/desktop/src/shared/botAutonomy.ts
index d67358036d..fa9abe4bf9 100644
--- a/apps/desktop/src/shared/botAutonomy.ts
+++ b/apps/desktop/src/shared/botAutonomy.ts
@@ -65,6 +65,11 @@ export const AUTONOMY_THRESHOLDS = {
stuckInToolAfterMs: 45 * 60_000,
/** 等权限 / 等回答达到这么久 → waiting(主人不在时伙伴代答或提醒)。 */
waitingAfterMs: 5 * 60_000,
+ /**
+ * 做完后安静满这么久、仍停在做完才发 finished。期间任务又开始下一轮(例如 PR 每来一轮评审处理一轮),
+ * 这一轮的做完被下一轮取代、不叫醒伙伴;最后一轮停下后照常发,最终交付不会漏。
+ */
+ finishedQuietMs: 20 * 60_000,
/** 同一任务同一类事件 24h 内最多发布次数,防止伙伴与任务互相推着打转。 */
maxEventsPerSessionPerDay: 6,
/** 远程电脑状态轮询间隔(仅有在线且已授权的电脑时才轮询)。 */
diff --git a/docs/product-rules/cindy-bots-runtime.md b/docs/product-rules/cindy-bots-runtime.md
index 7e6ea66f1b..60d098caa1 100644
--- a/docs/product-rules/cindy-bots-runtime.md
+++ b/docs/product-rules/cindy-bots-runtime.md
@@ -876,7 +876,7 @@ Session 任务遵守同一套机制与呈现契约:
| `waiting` | 等权限 / 等回答满 5 分钟 |
| `errored` | 出错,或启动时发现上一轮被打断 |
| `usage-limit` | 撞上用量上限、宿主正在等重置后自动继续 |
-| `finished` | 观察到任务完成(灵动岛完成态);主人点停止 / 中止的任务静默离表,不算做完、不发事件。自动化的运行任务不发,它有自己的结果通知 |
+| `finished` | 观察到任务完成(灵动岛完成态)后安静满 20 分钟仍停在做完;期间任务又开始下一轮(如 PR 每来一轮评审处理一轮),这一轮的做完被下一轮取代、不发,最后一轮停下后照常发。主人点停止 / 中止的任务静默离表,不算做完、不发事件。自动化的运行任务不发,它有自己的结果通知 |
| `sweep` | 条件兜底:某跟进伙伴还有 ≥1 件等你 / 出错 / 用量上限 / 卡住的本机任务,且距它上次被叫醒(任一事件被它的规则接收)满 2 小时 |
- 去重:事件 id = `类型:任务:进入该状态的时刻`,同一状态只发一次;引擎另有 24 小时回执去重,重启后同一次状态不重发。
@@ -1088,7 +1088,7 @@ Codex 切模型重建时,线程索引里的路径不等于文件已经落盘
父任务异常恢复后仍投递到当前主任务;check / message / stop 都只能控制它创建的 Session 任务。
伙伴负责的任务经 6.3 的工作台工具查看、继续;越权(不在负责范围、伙伴隐藏任务、非主任务调用、别的伙伴在跟)
一律被宿主拒绝。
-- 自主跟进:任务卡住 10 分钟 / 等你 5 分钟 / 出错 / 用量上限 / 做完时,只有跟进者的内置「跟进」规则被叫醒一次;
+- 自主跟进:任务卡住 10 分钟 / 等你 5 分钟 / 出错 / 用量上限 / 做完后安静 20 分钟时,只有跟进者的内置「跟进」规则被叫醒一次;
例行任务引擎重建后 `host:sessions` 事件源重新登记;内置规则不能删除、不能改内容、只能暂停;没人跟的本机「等你」
只提醒主人一次;「自动跟进所有任务」默认关闭,恢复默认删除 override 并回到关闭;无总调度、无待跟进委派时不起扫描定时器;
总调度的「一眼」看全设备任务,其它伙伴只看自己开的或接手的任务;已授权的远程控制端读「一眼」时,与远程任务列表同一口径,隐藏 / 归档伙伴开的、它的主任务与它在跟的任务一律不返回;远程任务只进「一眼」、不叫醒伙伴也不提醒;没有待处理