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): retain observation and delivery recovery demand

zqchrisfe06860645f2
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 3b558596eb..c02f75d518 100644
--- a/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
+++ b/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
@@ -116,7 +116,7 @@ async function setup(options: {
await created.start();
return created;
};
- return { runtime, create, engine, execute, remind, push, now: () => now, advance: (ms: number) => { now += ms; } };
+ return { runtime, create, engine, execute, remind, push, bots, now: () => now, advance: (ms: number) => { now += ms; } };
}
describe('BotAutonomyRuntime with the routine engine', () => {
@@ -476,3 +476,112 @@ it('starts the scan timer only for observed work and releases it when that work
vi.useRealTimers();
}
});
+
+// Unit / PR #6675: a delegation arrives after the watcher went idle, then its one
+// ownership refresh fails. Earlier recovery coverage only exercised initial startup.
+it('retries a failed delegation ownership refresh while idle and admits the cached activity', async () => {
+ vi.useFakeTimers();
+ let failRead = false;
+ const options = { schedulerOff: true, creators: {} as Record<string, string>,
+ beforeOwnershipRead: () => { if (failRead) throw new Error('busy'); } };
+ const t = await setup(options);
+ 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 sweep = vi.spyOn(runtime.watch, 'sweep');
+ try {
+ await runtime.start();
+ options.creators.child = 'a';
+ runtime.watch.applyActivity('child', errored('child'));
+ failRead = true;
+ await runtime.refreshFollowers();
+ expect(runtime.watch.get('child')).toBeUndefined();
+ expect(publish).not.toHaveBeenCalled();
+ failRead = false;
+ t.advance(30_000);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(sweep).toHaveBeenCalledOnce();
+ expect(runtime.watch.get('child')).toMatchObject({ followerBotId: 'a', phase: 'errored' });
+ expect(publish).toHaveBeenCalledWith(HOST_SESSIONS_SOURCE_ID,
+ expect.objectContaining({ type: 'errored', subject: 'child' }), expect.any(Function));
+ runtime.watch.applyActivity('child', null);
+ await runtime.watch.settled();
+ sweep.mockClear();
+ await vi.advanceTimersByTimeAsync(120_000);
+ expect(sweep).not.toHaveBeenCalled();
+ } finally {
+ runtime.stop();
+ vi.useRealTimers();
+ }
+});
+
+// Unit / PR #6675: pausing the selected scheduler is not opting out of observation.
+// Check the existing owner reminder through the real runtime and routine engine.
+it('keeps opted-in tasks visible and reminds the owner when the scheduler teammate is paused', async () => {
+ const t = await setup();
+ const engine = await t.engine();
+ const runtime = await t.runtime(engine);
+ try {
+ runtime.watch.applyActivity('mine', waiting('mine', t.now()));
+ await runtime.watch.settled();
+ t.bots.get('sched')!.status = 'paused';
+ await runtime.refreshFollowers();
+ expect(runtime.watch.needsSweep()).toBe(true);
+ expect(runtime.glance('sched').waiting.map(row => row.sessionId)).toEqual(['mine']);
+ expect(runtime.watch.get('mine')?.followerBotId).toBeNull();
+ t.advance(AUTONOMY_THRESHOLDS.waitingAfterMs);
+ await runtime.sweep();
+ expect(t.remind).toHaveBeenCalledOnce();
+ expect(t.execute).not.toHaveBeenCalled();
+ } finally {
+ runtime.stop();
+ await engine.stop();
+ }
+});
+
+// Unit / PR #6675: a finished last delegation no longer needs state scans, but a
+// failed ensure/publish must retain event delivery and stop retrying after success.
+it.each(['ensure', 'publish'] as const)('retries the last completion after %s fails and then releases the timer', async (failure) => {
+ vi.useFakeTimers();
+ let failing = true;
+ const t = await setup({ schedulerOff: true, creators: { child: 'a' } });
+ const publish = vi.fn<AutonomyEngine['publish']>(async () => {
+ if (failure === 'publish' && failing) throw new Error('publish unavailable');
+ return { accepted: 1, duplicate: false };
+ });
+ const runtime = t.create({ registerSource: vi.fn(), publish, ensureBuiltin: vi.fn(async () => {
+ if (failure === 'ensure' && failing) throw new Error('storage unavailable');
+ return {} as never;
+ }) });
+ const sweep = vi.spyOn(runtime.watch, 'sweep');
+ try {
+ await runtime.start();
+ runtime.watch.applyActivity('child', running('child'));
+ await runtime.watch.settled();
+ runtime.watch.applyActivity('child', { ...running('child'), phase: 'completed' } as SessionActivitySnapshot);
+ await runtime.watch.settled();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(runtime.watch.needsSweep()).toBe(false);
+ failing = false;
+ publish.mockClear();
+ t.advance(30_000);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(publish).toHaveBeenCalledOnce();
+ expect(publish).toHaveBeenCalledWith(HOST_SESSIONS_SOURCE_ID,
+ expect.objectContaining({ type: 'finished', subject: 'child' }), expect.any(Function));
+ expect(runtime.watch.get('child')).toBeUndefined();
+ expect(runtime.watch.finished().map(row => row.sessionId)).toEqual(['child']);
+ sweep.mockClear();
+ await vi.advanceTimersByTimeAsync(120_000);
+ expect(sweep).not.toHaveBeenCalled();
+ expect(publish).toHaveBeenCalledOnce();
+ // The event ledger expires after 24h. A later settings/activity refresh must not replay it.
+ t.advance(25 * 60 * 60_000);
+ runtime.watch.applyActivity('child', { ...running('child'), phase: 'completed' } as SessionActivitySnapshot);
+ await runtime.refreshFollowers();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(publish).toHaveBeenCalledOnce();
+ } finally {
+ runtime.stop();
+ vi.useRealTimers();
+ }
+});
diff --git a/apps/desktop/src/main/bot-autonomy/runtime.ts b/apps/desktop/src/main/bot-autonomy/runtime.ts
index e288b83612..ac86a80305 100644
--- a/apps/desktop/src/main/bot-autonomy/runtime.ts
+++ b/apps/desktop/src/main/bot-autonomy/runtime.ts
@@ -33,7 +33,7 @@ import { isFollowerAvailable } from './follower.js';
import { projectBotGlance } from './glance.js';
import { errorCode, type TaskWatch, type TaskWatchLogger } from './taskWatch.js';
-/** 程序级心跳:刷新归属 / 标题、清理做完的行、判定阈值、按需轮询远程电脑。 */
+/** 按需检查本机状态,或重试尚未成功发布的事件;不轮询远程电脑。 */
export const SWEEP_INTERVAL_MS = 30_000;
/** 界面推送节流。 */
export const PUSH_THROTTLE_MS = 1_000;
@@ -67,6 +67,7 @@ export class BotAutonomyRuntime {
readonly watch: TaskWatch;
private readonly ledger = new AutonomyEventLedger();
private readonly inflight = new Set<string>();
+ private readonly deferredEvents = new Set<string>();
private readonly reminded = new Map<string, number>();
private readonly ensured = new Set<string>();
/** 正在对齐的内置规则:并发的 ensure 与发布共用同一次写入。 */
@@ -109,7 +110,7 @@ export class BotAutonomyRuntime {
}
private syncSweepTimer(): void {
- const needed = this.started && !this.stopped && this.timersEnabled && this.watch.needsSweep();
+ const needed = this.started && !this.stopped && this.timersEnabled && this.needsSweep();
if (!needed && this.sweepTimer) {
clearInterval(this.sweepTimer);
this.sweepTimer = null;
@@ -119,6 +120,10 @@ export class BotAutonomyRuntime {
}
}
+ private needsSweep(): boolean {
+ return this.watch.needsSweep() || this.deferredEvents.size > 0;
+ }
+
stop(): void {
if (this.stopped) return;
this.stopped = true;
@@ -138,7 +143,7 @@ export class BotAutonomyRuntime {
/** 一次程序级扫描;定时器与单测共用。 */
async sweep(): Promise<void> {
if (this.stopped || this.sweeping || !this.deps.isCurrent()) return;
- if (!this.watch.needsSweep()) {
+ if (!this.needsSweep()) {
this.syncSweepTimer();
return;
}
@@ -266,7 +271,11 @@ export class BotAutonomyRuntime {
async evaluate(): Promise<void> {
if (!this.started || this.stopped || !this.deps.isCurrent()) return;
const now = this.deps.now();
- const { due, capped } = this.ledger.evaluate(this.watch.list(), now);
+ const rows = this.watch.list();
+ const { due, capped } = this.ledger.evaluate(rows, now);
+ const dueIds = new Set(due.map(candidate => candidate.id));
+ // Removed/out-of-scope tasks must not retain a retry timer or replay stale events.
+ for (const id of this.deferredEvents) if (!dueIds.has(id)) this.deferredEvents.delete(id);
for (const candidate of capped) {
this.deps.log.info('autonomy event capped', { type: candidate.type, occurrence: candidate.occurrence });
}
@@ -277,10 +286,17 @@ export class BotAutonomyRuntime {
await this.dispatch(candidate, now);
} finally {
this.inflight.delete(candidate.id);
+ if (this.ledger.isSettled(candidate.id)) this.deferredEvents.delete(candidate.id);
+ else this.deferredEvents.add(candidate.id);
}
if (this.stopped) return;
}
+ 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);
+ }
this.pruneReminded(now);
+ this.syncSweepTimer();
}
private async dispatch(candidate: AutonomyEventCandidate, now: number): Promise<void> {
diff --git a/apps/desktop/src/main/bot-autonomy/taskWatch.ts b/apps/desktop/src/main/bot-autonomy/taskWatch.ts
index 7d279c95a7..f980ca44d2 100644
--- a/apps/desktop/src/main/bot-autonomy/taskWatch.ts
+++ b/apps/desktop/src/main/bot-autonomy/taskWatch.ts
@@ -21,7 +21,6 @@ import {
} from '../../shared/botAutonomy.js';
import {
deriveFollowerBotId,
- isFollowerAvailable,
resolveSchedulerBotId,
type FollowerBotDirectory,
type FollowerBotState,
@@ -228,6 +227,20 @@ export class TaskWatch {
return this.finishedLog;
}
+ /** Completed event was settled (or intentionally has no event); keep only the completion counter. */
+ retireFinished(sessionId: string, phaseSince: number): void {
+ const row = this.rows.get(sessionId);
+ if (!row || row.deviceId !== null || row.phase !== 'finished' || row.phaseSince !== phaseSince) return;
+ this.rows.delete(sessionId);
+ this.meta.delete(sessionId);
+ this.interrupted.delete(sessionId);
+ const facts = this.local.get(sessionId);
+ // New activity may already be queued while the previous completion publish was awaiting IO.
+ if (facts && (!facts.activity || facts.activity.phase === 'completed') && !facts.inputError && !facts.usageLimitWait) {
+ this.local.delete(sessionId);
+ }
+ }
+
offlineDevices(): string[] {
return [...this.remote.values()].filter((device) => !device.online).map((device) => device.name);
}
@@ -302,14 +315,16 @@ export class TaskWatch {
needsSweep(): boolean {
if (this.stopped) return false;
if (!this.ownershipLoaded) return this.ownershipReadFailures <= 3 || this.pending.size > 0;
- return this.hasScheduler()
+ return this.ownershipReadFailures > 0
+ || this.hasScheduler()
|| (!this.interruptedSeeded && this.ownership.creatorBySession.size > 0)
|| [...this.pending].some(id => this.observesLocal(id))
|| [...this.rows.values()].some(row => row.deviceId === null && row.phase !== 'finished');
}
private hasScheduler(): boolean {
- return this.schedulerBotId !== null && isFollowerAvailable(this.ownership.bots.get(this.schedulerBotId));
+ // Observation consent survives pausing the selected teammate; followerFor controls execution.
+ return this.schedulerBotId !== null;
}
private observesLocal(id: string): boolean {
@@ -574,7 +589,6 @@ export class TaskWatch {
}
private async refreshOwnership(force = false): Promise<void> {
- const wasLoaded = this.ownershipLoaded;
const previousScope = JSON.stringify([this.hasScheduler(), [...this.ownership.creatorBySession.keys()]]);
if (this.ownershipRefresh) {
await this.ownershipRefresh;
@@ -599,6 +613,8 @@ export class TaskWatch {
this.schedulerBotId = resolveSchedulerBotId(this.readOverride(), this.ownership.bots);
if (previousScope !== JSON.stringify([this.hasScheduler(), [...this.ownership.creatorBySession.keys()]])) {
this.interruptedSeeded = false;
+ // A recovered ownership read can discover delegations whose activity arrived while idle.
+ for (const id of this.local.keys()) if (this.observesLocal(id)) this.pending.add(id);
}
for (const row of this.rows.values()) {
if (row.deviceId === null && !this.observesLocal(row.sessionId)) {
@@ -622,8 +638,8 @@ export class TaskWatch {
} finally {
if (this.ownershipRefresh === run) this.ownershipRefresh = null;
}
- // 首次读到归属(含启动时读失败、之后扫描才读到):把期间累积的变化补算一次。
- if (!wasLoaded && this.ownershipLoaded && this.pending.size && !this.stopped) this.scheduleFlush();
+ // 初次加载或变化后恢复归属:把期间累积的变化补算一次。
+ if (this.ownershipLoaded && this.pending.size && !this.stopped) this.scheduleFlush();
}
private readOverride(): { customized: boolean; schedulerBotId: string | null } {
@@ -641,7 +657,7 @@ export class TaskWatch {
async sweep(): Promise<void> {
if (this.stopped || !this.needsSweep()) return;
const now = this.deps.now();
- if (now - this.ownershipAt >= OWNERSHIP_REFRESH_MS) await this.refreshOwnership();
+ if (this.ownershipReadFailures > 0 || now - this.ownershipAt >= OWNERSHIP_REFRESH_MS) await this.refreshOwnership();
await this.refreshLocalMeta();
// 启动兜底读失败(如数据库一时忙)或启动时归属还没读到:按扫描节奏重试,读成功一次后不再读。
if (!this.interruptedSeeded && this.ownershipLoaded && !this.stopped) await this.seedInterrupted();
diff --git a/docs/product-rules/cindy-bots-runtime.md b/docs/product-rules/cindy-bots-runtime.md
index 1d8b22847c..8c2a84aa16 100644
--- a/docs/product-rules/cindy-bots-runtime.md
+++ b/docs/product-rules/cindy-bots-runtime.md
@@ -668,6 +668,8 @@ Session 任务遵守同一套机制与呈现契约:
订阅(运行 / 等交互 / 完成 / 出错)、输入协调器投影(出错、撞上用量上限且宿主已排好自动继续)、挂起交互(等你的
那句话)与数据库兜底(标题、隐藏 / 归档、启动时上一轮被打断的任务)。本机每 30 秒检查只在有待跟进的委派工作或已开启总调度时运行;没有需求则释放定时器,新委派和设置变化通过事件重新启动。启动归属读取失败可有限重试,有待处理事件时保留恢复机会。
全局跟进关闭时,启动恢复也只查询明确委派的任务,不读无关任务的元数据和挂起交互。普通任务自身的通知链保持不变。
+ 归属变化读取失败时保留重试需求,成功后补入期间收到的活动;已选总调度的暂停只影响模型执行,不撤销观察与等待提醒。完成事件因规则未就绪或发布失败而未结清时,继续按原间隔重试事件,成功或任务移出范围后退出,不为此扩大状态扫描范围。
+ 完成事件结清(或该完成本就不发事件)后移除对应本机状态行,完成统计单独保留;不能依赖已停止的扫描清理旧行,避免去重过期后重发旧完成事件。
远程只在查看总调度工作台时,经既有授权后台只读链路读 `maker:list-active` v2,再按需读标题与挂起交互;没有常驻远程轮询。
同时读取合并、每台电脑至少间隔 60 秒,失败按电脑指数退避(上限 15 分钟);离线电脑的行保留最后状态并标离线,下次读取时清理超过 24 小时的离线行。手机不进查询目标。
隐藏或收起的工作台不读取;自动打开判断只读已有投影,不触发远程查询。