Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bot): no ledger when task status fails; truncated turns do not advance the watermark
zqchris6df9952ff4e9
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.tsindex 292702f275..c4df83873f 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts@@ -19,6 +19,8 @@ const h = vi.hoisted(() => ({prevCompleted: 3 as number | null,/** 缺口回补查询(ORDER BY rowid ASC)返回的行。 */gapRows: [] as any[],+ /** 本轮取材查询返回的行;null = 默认一条用户消息。 */+ turnRows: null as any[] | null,}));vi.mock('../../appSessionState.js', () => ({activeOwnerScopeKey: () => h.owner,@@ -42,7 +44,7 @@ vi.mock('../../localDb/client/current.js', () => {query: vi.fn(async (sql: string) =>sql.includes('ORDER BY rowid ASC')? h.gapRows- : [+ : h.turnRows ?? [{id: 'user',role: 'user',@@ -284,3 +286,16 @@ it('advances the ledger watermark only through the contiguous backfilled prefixh.prevCompleted = 3;h.gapRows = [];});++it('does not advance the watermark past the previous completed reply when the current turn itself was truncated', async () => {+ // 本轮取材取满 80 条 = 可能被 LIMIT 截断:水位停在上一条完成回复(3),留给下一次的缺口回补。+ h.working = null;+ h.turnRows = [+ { id: 'user', role: 'user', content: JSON.stringify('Please always summarize the conclusion first.'), order: 4 },+ ...Array.from({ length: 79 }, (_, i) => ({ id: `t${i}`, role: 'tool_result', content: JSON.stringify(`result ${i}`), order: 4 + i })),+ ];+ h.model.mockResolvedValueOnce({ ok: true, text: JSON.stringify({ memories: [], skills: [], working: { commitments: [] } }) });+ await reviewTeammateLearning('session', 'reply');+ expect(h.writeWorking).toHaveBeenLastCalledWith('/test-owner', 'bot', expect.objectContaining({ coveredThroughRowid: 3 }), 0);+ h.turnRows = null;+});diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botWorkingMemory.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botWorkingMemory.test.tsindex 125bcd32f6..aeeeb43c72 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/botWorkingMemory.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/botWorkingMemory.test.ts@@ -321,6 +321,9 @@ describe('store', () => {// 来源校验查询失败:不退回"未过滤",按没有账本返回 null(提前换窗也随之取消)。db.queue.push([{ botId: 'bot-1' }], new Error('db down'));expect(await readWorkingMemoryNoteForSession('s-bot', root)).toBeNull();+ // 后台任务状态查询失败:不渲染"没有任务区"的账本(新上下文会以为旧任务不在跑),同样按 null。+ db.queue.push([{ botId: 'bot-1' }], [{ clearedAt: null }], dbRows, new Error('db down'));+ expect(await readWorkingMemoryNoteForSession('s-bot', root)).toBeNull();});it('treats items whose source message predates /clear as withdrawn, and prunes them after the clear is persisted', async () => {diff --git a/apps/desktop/src/main/maker-ipc/botLearningReview.ts b/apps/desktop/src/main/maker-ipc/botLearningReview.tsindex cc18a119a1..8c8bb668f5 100644--- a/apps/desktop/src/main/maker-ipc/botLearningReview.ts+++ b/apps/desktop/src/main/maker-ipc/botLearningReview.ts@@ -26,6 +26,8 @@ import {} from './botWorkingMemory.js';const running = new Map<string, Promise<void>>();+/** 本轮取材最多取多少条消息;超出即视为截断,水位不推进到当前回复。 */+const TURN_REVIEW_ROWS = 80;/** 缺口回补每批最多取多少条消息 / 多少字符;单条正文已截到 6000 字,所以每批至少前进一条。 */const GAP_BACKFILL_ROWS = 40;const GAP_BACKFILL_CHARS = 16000;@@ -103,18 +105,21 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)`SELECT client_id AS id, role, content, rowid AS 'order' FROM messagesWHERE session_id=? AND rewind_at IS NULL AND rowid <= ? AND rowid > ? ${clearedAtFilter}- AND ${REVIEW_ROLES} ORDER BY CASE WHEN role='user' THEN 0 ELSE 1 END, rowid DESC LIMIT 80+ AND ${REVIEW_ROLES} ORDER BY CASE WHEN role='user' THEN 0 ELSE 1 END, rowid DESC LIMIT ${TURN_REVIEW_ROWS}`,[sessionId, row.rowid, prevCompleted, sessionId],);const turnMessages = learningSnapshotBudget(records.map(toSnapshot), 40000);+ // 本轮本身被 LIMIT / 预算截断(工具密集的大轮次)时,漏掉的中间承诺 / 工具结果同样不能+ // 算"已覆盖":水位停在上一条完成回复,下一次复盘由缺口回补路径按序补齐。+ const turnTruncated = records.length >= TURN_REVIEW_ROWS || turnMessages.length < records.length;// 缺口回补:账本上次覆盖到的行落后于上一条完成回复(某轮复盘 claim 后失败、CAS 冲突、// 模型漏回 working),那段内容不会再被单独复盘;按 rowid 升序分批补进本次取材,覆盖水位// 只推进到连续处理过的前缀末尾——补不完就不推进到当前回复,账本在补完前不算新鲜,换窗线// 不会因为一份有缺口的账本提前。每批有界且至少前进一条,几轮即收敛。没有账本时从上一条// 完成回复接起,不回溯整段历史。const ledgerFloor = workingBefore ? workingBefore.coveredThroughRowid : prevCompleted;- let coveredThroughRowid = row.rowid;+ let coveredThroughRowid = turnTruncated ? prevCompleted : row.rowid;let gapMessages: ReturnType<typeof toSnapshot>[] = [];if (ledgerFloor < prevCompleted) {const gapRows = await db.query<Record_>(diff --git a/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts b/apps/desktop/src/main/maker-ipc/botWorkingMemory.tsindex 1c88d59c27..cec4ba07fc 100644--- a/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts+++ b/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts@@ -573,9 +573,10 @@ export async function listWithdrawnSourceMessageIds(/*** 换窗交接 / 压缩后刷新用的渲染段;非伙伴主对话或没有账本时返回 null。* 任何读取失败都按"没有账本"处理——账本是增强,不能挡住换窗。来源校验(rewind / 删除 /- * clear 对账)失败同样按没有账本:退回"未过滤"会让已撤回的承诺 / 纠正借交接回到新上下文,- * 70% 提前换窗也只接受校验成功的快照。`excludeMessageIds` 是正在删除、还没落库的那批- * (持久清理在删除提交后另做,见 pruneWorkingMemoryForSession)。+ * clear 对账)和后台任务状态读取失败同样按没有账本:退回"未过滤"会让已撤回的承诺 / 纠正+ * 借交接回到新上下文,少了任务区会让新上下文不知道旧任务还在跑而重复派发;70% 提前换窗也+ * 只接受完整渲染成功的快照。`excludeMessageIds` 是正在删除、还没落库的那批(持久清理在+ * 删除提交后另做,见 pruneWorkingMemoryForSession)。*/export async function readWorkingMemoryNoteForSession(sessionId: string,@@ -590,7 +591,7 @@ export async function readWorkingMemoryNoteForSession(const withdrawn = await listWithdrawnSourceMessageIds(sessionId, stored);for (const id of opts.excludeMessageIds ?? []) withdrawn.add(id);const memory = withdrawn.size > 0 ? pruneWorkingMemoryForMessages(stored, withdrawn).memory : stored;- const openTasks = await listOpenTasks(botId).catch(() => []);+ const openTasks = await listOpenTasks(botId);return renderWorkingMemory(memory, { openTasks });} catch {return null;diff --git a/docs/product-rules/cindy-bots-runtime.md b/docs/product-rules/cindy-bots-runtime.mdindex 2d17d1510e..05336ed226 100644--- a/docs/product-rules/cindy-bots-runtime.md+++ b/docs/product-rules/cindy-bots-runtime.md@@ -81,7 +81,8 @@ tool 副作用时自动重放。原生后台工作尚未结束时不自动关闭宿主另为伙伴主对话维护一份**工作记忆账本**(Bot Home 下的 `working-memory.json`):进行中线索、伙伴答应过的事、在等谁、用户最新纠正与决定、近期完成,每条带来源消息位置。账本由回复后的学习复盘顺手更新,不新增模型调用;复盘取材从账本上次覆盖到的位置接上,某轮复盘失败漏掉的内容-由后续复盘按序分批补进账本(每批有界),覆盖水位只推进到连续补完的位置,补完之前账本不算新鲜;+由后续复盘按序分批补进账本(每批有界),覆盖水位只推进到连续补完的位置;本轮取材本身被截断时+同样不推进水位,补完之前账本不算新鲜;新条目必须有本轮用户或伙伴原话证据,漏写的旧条目保留,删除同样要证据。上述所有换窗交接(重启、换引擎、缩窗、容量到线、删消息重建)都带上账本;原生自动压缩之后的下一次发送再补一次,交接首条已带时不重复。账本是摘要,不是授权:历史授权不是新的