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): bound and deduplicate routine source status reports

Chrisec2c59c3ccb4
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts
index b5d7a641dc..549d3cb517 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts
@@ -73,3 +73,75 @@ describe('Plugin routine publisher', () => {
).toBe(false);
});
});
+
+async function statusFixture() {
+ let now = 1000;
+ const changed = vi.fn();
+ const engine = new RoutineEngine({
+ load: async () => null, save: vi.fn(async () => {}), execute: vi.fn(async () => ({})),
+ id: () => 'id', now: () => now, changed, onError: vi.fn(),
+ });
+ await engine.start();
+ return {
+ engine, changed,
+ advance: () => { now += 60_000; },
+ request: (payload: unknown, plugin = ghost) => handleRoutineRequest(plugin, payload, async () => engine, () => true),
+ };
+}
+
+it('does not broadcast unchanged source reports or erase runtime event metadata', async () => {
+ const f = await statusFixture();
+ await f.request({ action: 'status', status: 'listening' });
+ await f.request({ action: 'publish', event: { id: 'first', type: 'new', occurredAt: 1, data: {} } });
+ f.changed.mockClear();
+ f.advance();
+ expect(await f.request({ action: 'status', status: 'listening' })).toEqual({ ok: true });
+ expect(f.changed).not.toHaveBeenCalled();
+ expect(f.engine.listSources()[0].lastEventAt).toBe(1000);
+ const renamed = { ...ghost, manifest: { ...ghost.manifest, name: 'Renamed Mail' } };
+ expect(await f.request({ action: 'status', status: 'listening' }, renamed)).toEqual({ ok: true });
+ expect(f.changed).toHaveBeenCalledOnce();
+ expect(f.engine.listSources()[0]).toMatchObject({ name: 'Renamed Mail', lastEventAt: 1000 });
+ await f.request({ action: 'status', status: 'disconnected' }, renamed);
+ await f.request({ action: 'status', status: 'disconnected' }, renamed);
+ expect(f.changed).toHaveBeenCalledTimes(2);
+ await f.engine.stop();
+});
+
+it('limits repeated status requests without starving events or blocking host disconnects', async () => {
+ const f = await statusFixture();
+ for (let i = 0; i < 60; i++) expect(await f.request({ action: 'status', status: 'listening' })).toEqual({ ok: true });
+ expect(f.changed).toHaveBeenCalledOnce();
+ expect(await f.request({ action: 'status', status: 'error' })).toMatchObject({ ok: false, message: expect.stringContaining('rate limit') });
+ expect(f.changed).toHaveBeenCalledOnce();
+ expect(f.engine.listSources()[0].status).toBe('listening');
+ expect(await f.request({ action: 'publish', event: { id: 'first', type: 'new', occurredAt: 1, data: {} } })).toMatchObject({ ok: true });
+ f.changed.mockClear();
+ f.engine.removeSource('plugin:mail');
+ f.engine.removeSource('plugin:mail');
+ f.engine.removeSource('plugin:unknown');
+ expect(f.changed).toHaveBeenCalledOnce();
+ expect(f.engine.listSources()[0].status).toBe('disconnected');
+ expect(await f.request({ action: 'status', status: 'listening' })).toMatchObject({ ok: false });
+ const other = { ...ghost, manifest: { ...ghost.manifest, id: 'other' } };
+ expect(await f.request({ action: 'status', status: 'listening' }, other)).toEqual({ ok: true });
+ f.advance();
+ expect(await f.request({ action: 'status', status: 'listening' })).toEqual({ ok: true });
+ expect(f.engine.listSources()[0]).toMatchObject({ status: 'listening', lastEventAt: 1000 });
+ await f.engine.stop();
+});
+
+it('caps aggregate status reports across plugin identities', async () => {
+ const f = await statusFixture();
+ for (let i = 0; i < 240; i++) {
+ const plugin = { ...ghost, manifest: { ...ghost.manifest, id: `plugin-${Math.floor(i / 60)}` } };
+ expect(await f.request({ action: 'status', status: 'listening' }, plugin)).toEqual({ ok: true });
+ }
+ expect(f.changed).toHaveBeenCalledTimes(4);
+ expect(await f.request({ action: 'status', status: 'listening' })).toMatchObject({ ok: false, message: expect.stringContaining('rate limit') });
+ expect(f.changed).toHaveBeenCalledTimes(4);
+ f.advance();
+ expect(await f.request({ action: 'status', status: 'listening' })).toEqual({ ok: true });
+ expect(f.changed).toHaveBeenCalledTimes(5);
+ await f.engine.stop();
+});
diff --git a/apps/desktop/src/main/cindy-brain/forge.ts b/apps/desktop/src/main/cindy-brain/forge.ts
index 9b66514e7e..8e890d29e6 100644
--- a/apps/desktop/src/main/cindy-brain/forge.ts
+++ b/apps/desktop/src/main/cindy-brain/forge.ts
@@ -1481,6 +1481,7 @@ export const FORGE_GUIDE = `# 意识(Ghost)编写手册
去重窗口为 24 小时,回执按 UTF-8 JSON 限制为单插件 256 KiB、账号 2 MiB;超限返回 ok:false,
应保留原 event.id 稍后重试,不要改 ID 或紧密循环重试。容量满时不会驱逐窗口内回执;旧版过量
回执在启动时保留最新的有界部分。事件运行历史不受回执清理影响。
+状态上报另有独立的每插件 60 次/分钟、全账号 240 次/分钟额度,重复状态同样计数;相同状态和声明不会触发界面刷新,也不会清空最近事件时间。请只在状态变化或重连后上报,超限时退避重试。
意识是 Cindy 的第三方能力包,文件形态是 \`.cindy\`(zip 包)。装入后可给
主机叠加:AI 可调用的工具、常驻界面面板、模型代办能力。本手册教你(agent)替用户
diff --git a/docs/product-rules/bot-routines.md b/docs/product-rules/bot-routines.md
index c5eba9d5fe..aede706f6d 100644
--- a/docs/product-rules/bot-routines.md
+++ b/docs/product-rules/bot-routines.md
@@ -86,6 +86,8 @@ const receipt = await cindy.routines.request({
宿主限制事件发布频率:单来源每 60 秒最多 60 次,全账号最多 240 次;待处理发布请求上限分别为 8 和 32,限速在解析负载和进入持久写入队列之前执行,状态重报不重置额度。去重回执保留 24 小时(过期项在后续接收或启动时清理),按 UTF-8 JSON 字节数限制为单来源 256 KiB、全账号 2 MiB。容量满时拒绝新事件并返回可重试错误,不驱逐窗口内回执;相同 ID 的已确认重投不重新写盘。启动时将旧版过量回执按时间从新到旧裁入上限,不改运行历史。
+事件源状态上报使用独立限额:单来源每 60 秒最多 60 次、全账号最多 240 次,重复状态也计入频率。名称、状态及事件声明均未变时直接返回,不广播或清空最近事件时间;真实变化才通知界面。宿主发现插件断开不受插件上报额度限制,重复断开也不广播。
+
本协议不要求第三方连接器的固定组合:飞书长连接、Gmail 本地采集和 GitHub 事件适配器都可报告相同形态的事件。本次交付的是宿主接入与执行能力,不会自动给现有飞书/Gmail 插件增加第三方订阅;适配器需按上述协议接入。
## 验证与边界
diff --git a/packages/maker-scheduler/src/routine-engine.ts b/packages/maker-scheduler/src/routine-engine.ts
index 195c3a3af1..ea5efcecc3 100644
--- a/packages/maker-scheduler/src/routine-engine.ts
+++ b/packages/maker-scheduler/src/routine-engine.ts
@@ -69,6 +69,7 @@ export class RoutineEngine {
};
private readonly sources = new Map<string, RoutineSource>();
private readonly eventAdmission = new RoutineEventAdmission();
+ private readonly sourceAdmission = new RoutineEventAdmission();
private readonly active = new Map<string, AbortController>();
private readonly activeTasks = new Map<string, Promise<void>>();
private readonly blockedBots = new Set<string>();
@@ -157,13 +158,29 @@ export class RoutineEngine {
}
registerSource(source: RoutineSource): void {
- this.sources.set(source.id, structuredClone(source));
- this.notifyChanged();
+ // A separate quota prevents status chatter from exhausting event publication capacity.
+ const release = this.sourceAdmission.acquire(source.id, this.deps.now());
+ try {
+ const existing = this.sources.get(source.id);
+ if (
+ existing?.name === source.name && existing.status === source.status &&
+ JSON.stringify(existing.events) === JSON.stringify(source.events)
+ ) return;
+ const next = structuredClone(source);
+ // Status reports do not own the runtime's last successful event timestamp.
+ if (existing?.lastEventAt !== undefined) next.lastEventAt = existing.lastEventAt;
+ this.sources.set(source.id, next);
+ this.notifyChanged();
+ } finally {
+ release();
+ }
}
removeSource(sourceId: string): void {
const source = this.sources.get(sourceId);
- if (source) source.status = "disconnected";
+ if (!source || source.status === "disconnected") return;
+ // Host-observed disconnects must still take effect when a plugin has exhausted its quota.
+ source.status = "disconnected";
this.notifyChanged();
}
diff --git a/packages/maker-scheduler/src/routine-event-admission.ts b/packages/maker-scheduler/src/routine-event-admission.ts
index cde50e6c16..e06d309a91 100644
--- a/packages/maker-scheduler/src/routine-event-admission.ts
+++ b/packages/maker-scheduler/src/routine-event-admission.ts
@@ -12,7 +12,7 @@ export const ROUTINE_EVENT_LIMITS = {
receiptBytesGlobal: 2 * 1024 * 1024,
} as const;
-/** Reserve before parsing or joining the durable write queue, including duplicate requests. */
+/** Reserve before cloning/queuing plugin requests; separate instances isolate event and status quotas. */
export class RoutineEventAdmission {
private readonly windows = new Map<string, { start: number; count: number }>();
private global = { start: 0, count: 0 };
@@ -26,10 +26,10 @@ export class RoutineEventAdmission {
if (now - this.global.start >= limits.windowMs) this.global = { start: now, count: 0 };
const window = this.windows.get(sourceId) ?? { start: now, count: 0 };
if (window.count >= limits.perSource || this.global.count >= limits.global)
- throw new Error('Routine event rate limit reached; retry after 60 seconds');
+ throw new Error('Routine request rate limit reached; retry after 60 seconds');
const pending = this.pending.get(sourceId) ?? 0;
if (pending >= limits.pendingPerSource || this.totalPending >= limits.pendingGlobal)
- throw new Error('Routine event intake is busy; retry later');
+ throw new Error('Routine request intake is busy; retry later');
window.count += 1;
this.global.count += 1;
this.windows.set(sourceId, window);