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): preserve completion before delegation refresh

zqchris9682343edd39
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 c02f75d518..d1a5966332 100644
--- a/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
+++ b/apps/desktop/src/main/bot-autonomy/__tests__/runtime.test.ts
@@ -538,6 +538,50 @@ it('keeps opted-in tasks visible and reminds the owner when the scheduler teamma
}
});
+// Unit / #6675: activity can run through completion before the debounced ownership
+// refresh. Previous recovery coverage kept the task active until ownership loaded.
+// Only a witnessed completion may publish/count; stopped, unrelated and stale
+// completed tasks must remain silent. Fake time also covers an ownership retry.
+it.each([false, true])('keeps a fast delegation completion across delayed ownership (read failure: %s)', async (failFirstRead) => {
+ 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 completed = (id: string) => ({ ...running(id), phase: 'completed', lastActivityAtMs: t.now() } as SessionActivitySnapshot);
+ try {
+ await runtime.start();
+ options.creators.child = options.creators.stopped = options.creators.stale = 'a';
+ for (const id of ['child', 'unrelated', 'stopped']) runtime.watch.applyActivity(id, running(id));
+ for (const id of ['child', 'unrelated', 'stale']) runtime.watch.applyActivity(id, completed(id));
+ runtime.watch.applyActivity('stopped', null);
+ await runtime.watch.settled();
+ expect(publish).not.toHaveBeenCalled();
+ expect(runtime.watch.needsSweep()).toBe(false);
+ failRead = failFirstRead;
+ await runtime.refreshFollowers();
+ if (failFirstRead) {
+ expect(publish).not.toHaveBeenCalled();
+ failRead = false;
+ 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.glance('a').doneToday).toBe(1);
+ expect(runtime.watch.needsSweep()).toBe(false);
+ await runtime.refreshFollowers();
+ await vi.advanceTimersByTimeAsync(120_000);
+ expect(publish).toHaveBeenCalledOnce();
+ } finally {
+ runtime.stop();
+ vi.useRealTimers();
+ }
+});
+
// 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) => {
diff --git a/apps/desktop/src/main/bot-autonomy/taskWatch.ts b/apps/desktop/src/main/bot-autonomy/taskWatch.ts
index 0c9a04709b..97474652dc 100644
--- a/apps/desktop/src/main/bot-autonomy/taskWatch.ts
+++ b/apps/desktop/src/main/bot-autonomy/taskWatch.ts
@@ -136,6 +136,8 @@ function inputProjectionErrorReason(projection: BotAutonomyInputProjection): str
interface LocalFacts {
activity: SessionActivitySnapshot | null;
+ /** Live activity reached completion before ownership/metadata could create a row. */
+ observedCompletion: boolean;
inputError: string | null;
/** 出错横幅背后的稳定原因(输入协调器投影的 errorReason / 工具循环),没有就是 null。 */
inputErrorReason: string | null;
@@ -270,7 +272,11 @@ export class TaskWatch {
applyActivity(sessionId: string, current: SessionActivitySnapshot | null): void {
if (this.stopped) return;
- const facts = { ...this.factsFor(sessionId), activity: current };
+ const previous = this.factsFor(sessionId);
+ const observedCompletion = current?.phase === 'completed' && previous.activity !== null
+ && previous.activity.turnGeneration === current.turnGeneration
+ && (previous.activity.phase !== 'completed' || previous.observedCompletion);
+ const facts = { ...previous, activity: current, observedCompletion };
if (!current && !facts.inputError && !facts.usageLimitWait) this.local.delete(sessionId);
else this.local.set(sessionId, facts);
if (this.observesLocal(sessionId) || !this.ownershipLoaded) this.queue(sessionId);
@@ -345,7 +351,7 @@ export class TaskWatch {
}
private factsFor(sessionId: string): LocalFacts {
- return this.local.get(sessionId) ?? { activity: null, inputError: null, inputErrorReason: null, usageLimitWait: false };
+ return this.local.get(sessionId) ?? { activity: null, observedCompletion: false, inputError: null, inputErrorReason: null, usageLimitWait: false };
}
private queue(sessionId: string): void {
@@ -436,8 +442,8 @@ export class TaskWatch {
usageLimitWait: facts.usageLimitWait,
});
if (activity) this.interrupted.delete(sessionId);
- // 第一次看到就已经做完(例如启动时灵动岛里还挂着的未读完成):没有可观察的跃迁,不发「做完」。
- if (phase === 'finished' && !previous) return { changed: false, unowned: false };
+ // 启动时残留的完成不发事件;归属/元数据读取前已见过同一轮的完成跃迁则保留。
+ if (phase === 'finished' && !previous && !facts.observedCompletion) return { changed: false, unowned: false };
if (phase === 'idle') {
if (previous && this.interrupted.has(sessionId)) return { changed: false, unowned: false };
if (previous?.phase === 'finished') return { changed: false, unowned: false };