Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): sanitize routine plugin errors and await read effects
Chrisd096f0051f84
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.tsindex 549d3cb517..a3e5bd4c7a 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/routineSlot.test.ts@@ -1,8 +1,12 @@-import { describe, it, expect, vi } from 'vitest';+import { afterEach, describe, it, expect, vi } from 'vitest';import { RoutineEngine } from '@cindy/maker-scheduler';import type { InstalledGhost } from '../../../shared/ghost.js';import { handleRoutineRequest } from '../routineSlot.js';+const warn = vi.hoisted(() => vi.fn());+vi.mock('../../logger.js', () => ({ createLogger: () => ({ warn }) }));+afterEach(() => { warn.mockClear(); });+const ghost = {enabled: true,manifest: {@@ -145,3 +149,49 @@ it('caps aggregate status reports across plugin identities', async () => {expect(f.changed).toHaveBeenCalledTimes(5);await f.engine.stop();});++it.each([+ new Error("EACCES: permission denied, open '/Users/private-user/Library/Application Support/Cindy/routines/routines.json'"),+ new Error("EBUSY: resource busy, rename 'C:\\Users\\private-user\\AppData\\Roaming\\Cindy\\routines\\private.tmp'"),+ new SyntaxError('Unexpected token in private routine instructions'),+ new Error('Routine request rate limit reached; retry after 60 seconds /private/internal-path'),+ 'Unexpected failure at /private/internal-path',+])('keeps unexpected startup failure details in Main logs, not plugin replies: %s', async (error) => {+ const result = await handleRoutineRequest(+ ghost, { action: 'status', status: 'listening' }, async () => { throw error; }, () => true,+ );+ expect(result).toEqual({ ok: false, message: 'Routine request failed; please retry later' });+ expect(warn).toHaveBeenCalledWith('routine plugin request failed', {+ ghostId: 'mail', error: error instanceof Error ? error.message : error,+ });+});++it.each(['write', 'rename'])('hides %s failures and keeps the same event retryable until durable acceptance', async (operation) => {+ const save = vi.fn(async () => {});+ const engine = new RoutineEngine({+ load: async () => null, save, execute: vi.fn(async () => ({})),+ id: () => 'id', now: () => 1, changed: vi.fn(), onError: vi.fn(),+ });+ await engine.start();+ const request = (payload: unknown) => handleRoutineRequest(ghost, payload, async () => engine, () => true);+ await request({ action: 'status', status: 'listening' });+ const payload = { action: 'publish', event: { id: 'retry-me', type: 'new', occurredAt: 1, data: { label: 'private-event-data' } } };+ const error = new Error(`EACCES: ${operation} '/Users/private-user/Cindy/routines/private.tmp'`);+ save.mockRejectedValueOnce(error);+ expect(await request(payload)).toEqual({ ok: false, message: 'Routine request failed; please retry later' });+ expect(warn).toHaveBeenCalledWith('routine plugin request failed', { ghostId: 'mail', error: error.message });+ expect(JSON.stringify(warn.mock.calls)).not.toContain('private-event-data');+ expect(await request(payload)).toEqual({ ok: true, accepted: 0, duplicate: false });+ expect(await request(payload)).toEqual({ ok: true, accepted: 0, duplicate: true });+ await engine.stop();+});++it('keeps fixed validation and retry guidance without logging repeated expected rejections', async () => {+ const f = await statusFixture();+ await f.request({ action: 'status', status: 'listening' });+ expect(await f.request({ action: 'publish', event: { id: 'bad', type: 'new', occurredAt: -1, data: {} } }))+ .toEqual({ ok: false, message: 'Invalid event timestamp' });+ for (let i = 0; i < 80; i++) await f.request({ action: 'status', status: 'listening' });+ expect(warn).not.toHaveBeenCalled();+ await f.engine.stop();+});diff --git a/apps/desktop/src/main/cindy-brain/forge.ts b/apps/desktop/src/main/cindy-brain/forge.tsindex 8e890d29e6..4751fec879 100644--- a/apps/desktop/src/main/cindy-brain/forge.ts+++ b/apps/desktop/src/main/cindy-brain/forge.ts@@ -1482,6 +1482,7 @@ export const FORGE_GUIDE = `# 意识(Ghost)编写手册应保留原 event.id 稍后重试,不要改 ID 或紧密循环重试。容量满时不会驱逐窗口内回执;旧版过量回执在启动时保留最新的有界部分。事件运行历史不受回执清理影响。状态上报另有独立的每插件 60 次/分钟、全账号 240 次/分钟额度,重复状态同样计数;相同状态和声明不会触发界面刷新,也不会清空最近事件时间。请只在状态变化或重连后上报,超限时退避重试。+失败回执只包含固定公开文案;未知宿主错误统一返回 Routine request failed; please retry later,底层路径和异常仅留在 Main 日志。保留原投递 ID,按退避重试。意识是 Cindy 的第三方能力包,文件形态是 \`.cindy\`(zip 包)。装入后可给主机叠加:AI 可调用的工具、常驻界面面板、模型代办能力。本手册教你(agent)替用户diff --git a/apps/desktop/src/main/cindy-brain/routineSlot.ts b/apps/desktop/src/main/cindy-brain/routineSlot.tsindex 220714b1d8..7e15b03ba0 100644--- a/apps/desktop/src/main/cindy-brain/routineSlot.ts+++ b/apps/desktop/src/main/cindy-brain/routineSlot.ts@@ -1,5 +1,30 @@import type { InstalledGhost } from '../../shared/ghost.js';import type { RoutineEngine } from '@cindy/maker-scheduler';+import { createLogger } from '../logger.js';++const log = createLogger('routines:plugin');++// Exact host-authored rejections only. Never echo arbitrary storage errors across the plugin boundary.+const PUBLIC_REJECTIONS = [+ 'Routine request rate limit reached; retry after 60 seconds',+ 'Routine request intake is busy; retry later',+ 'Routine receipt storage is full; retry after receipts expire (24 hours)',+ 'Routine queue is full; retry this event later',+ 'Routine service is stopped',+ 'Event source is not listening',+ 'Event type is undeclared',+ 'Event publisher is no longer active',+ 'Expected an object',+ 'Too many event fields',+ 'Event fields must be strings, finite numbers or booleans',+ 'Invalid event field',+ 'Event payload is too large',+ 'Invalid event timestamp',+ 'Expected nonempty text of at most 128 characters',+ 'Expected nonempty text of at most 200 characters',+ 'Expected nonempty text of at most 256 characters',+ 'Expected nonempty text of at most 1000 characters',+] as const;/** Host-authenticated publisher: a plugin may only publish its own declared event types. */export async function handleRoutineRequest(@@ -31,9 +56,17 @@ export async function handleRoutineRequest(if (request.action !== 'publish') return { ok: false, message: 'Unknown routine operation' };return { ok: true, ...(await engine.publish(sourceId, request.event, isCurrent)) };} catch (error) {+ const publicMessage = error instanceof Error+ ? PUBLIC_REJECTIONS.find((message) => message === error.message)+ : undefined;+ if (publicMessage) return { ok: false, message: publicMessage };+ log.warn('routine plugin request failed', {+ ghostId: ghost.manifest.id,+ error: error instanceof Error ? error.message : String(error),+ });return {ok: false,- message: error instanceof Error ? error.message : 'Routine event rejected',+ message: 'Routine request failed; please retry later',};}}diff --git a/apps/desktop/src/main/routines/__tests__/service.test.ts b/apps/desktop/src/main/routines/__tests__/service.test.tsindex ebb38c2cec..1d9b08da38 100644--- a/apps/desktop/src/main/routines/__tests__/service.test.ts+++ b/apps/desktop/src/main/routines/__tests__/service.test.ts@@ -449,6 +449,6 @@ it('reports real database failures instead of waiting indefinitely and allows aconst f = await restoreEventRoutine();mock.readProfiles.mockRejectedValueOnce(new Error('database query failed'));await expect(f.request({ action: 'status', status: 'listening' }))- .resolves.toMatchObject({ ok: false, message: 'database query failed' });+ .resolves.toMatchObject({ ok: false, message: 'Routine request failed; please retry later' });await expect(f.request({ action: 'status', status: 'listening' })).resolves.toEqual({ ok: true });});diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botSessionRead.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/botSessionRead.test.tsxindex 89a248161d..b97d2d5ba5 100644--- a/apps/desktop/src/renderer/features/bots/__tests__/botSessionRead.test.tsx+++ b/apps/desktop/src/renderer/features/bots/__tests__/botSessionRead.test.tsx@@ -93,8 +93,11 @@ describe('Bot conversation read position', () => {const view = render(<BotSessionView />);- await waitFor(() => expect(view.getByTestId('chat').dataset.unreadBoundary).toBe('5000'));- expect(getBotLastReadAt('bot-1')).toBe(10_000);+ // Rendering the divider can precede the passive effect that advances the read position.+ await waitFor(() => {+ expect(view.getByTestId('chat').dataset.unreadBoundary).toBe('5000');+ expect(getBotLastReadAt('bot-1')).toBe(10_000);+ });});it('keeps advancing the read position while the user is watching the chat', async () => {diff --git a/docs/product-rules/bot-routines.md b/docs/product-rules/bot-routines.mdindex 68d85b1495..9bbdbb138c 100644--- a/docs/product-rules/bot-routines.md+++ b/docs/product-rules/bot-routines.md@@ -90,6 +90,8 @@ const receipt = await cindy.routines.request({事件源状态上报使用独立限额:单来源每 60 秒最多 60 次、全账号最多 240 次,重复状态也计入频率。名称、状态及事件声明均未变时直接返回,不广播或清空最近事件时间;真实变化才通知界面。宿主发现插件断开不受插件上报额度限制,重复断开也不广播。+插件失败回执只返回固定公开文案:校验和容量/频率拒绝按精确白名单保留提示;未知异常统一回复通用重试提示,详细异常只进入 Main 日志,不携带文件路径或事件数据返回插件。写盘失败后的原事件 ID 仍可重试,成功持久化后才进入去重窗口。+本协议不要求第三方连接器的固定组合:飞书长连接、Gmail 本地采集和 GitHub 事件适配器都可报告相同形态的事件。本次交付的是宿主接入与执行能力,不会自动给现有飞书/Gmail 插件增加第三方订阅;适配器需按上述协议接入。## 验证与边界