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(bot): never trust a stored rowid as the backfill floor

zqchrisb53efe83c8d0
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
index 38f2faff01..0144a29a33 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
@@ -239,12 +239,13 @@ it('feeds the working memory to the review and writes an evidence-filtered ledge
await reviewTeammateLearning('session', 'reply');
const input = JSON.parse(h.model.mock.calls[0]![1] as string);
expect(input.workingMemory).toEqual({ threads: [], commitments: [], waiting: [], corrections: [], recentlyDone: [] });
- // 本轮取材从上一条完成回复(3)接起;账本只覆盖到 1,缺口 (1,3] 另按升序分批回补;都不取 /clear 之前的消息。
+ // 本轮取材从上一条完成回复(3)接起;旧账本没有 client_id,存下的 rowid(1)不作数,缺口从对话开头(0)
+ // 按升序分批回补;都不取 /clear 之前的消息。
const query = (getDbClient() as unknown as { query: ReturnType<typeof vi.fn> }).query;
const recent = query.mock.calls.slice(-2);
expect(recent.map((call) => call[1])).toEqual([
['session', 5, 3, 'session'],
- ['session', 1, 3, 'session', 41],
+ ['session', 0, 3, 'session', 41],
]);
expect(recent.every((call) => String(call[0]).includes('cleared_at'))).toBe(true);
expect(h.writeWorking).toHaveBeenCalledWith(
@@ -304,6 +305,12 @@ it('resolves the ledger floor by client_id so a VACUUM-renumbered rowid cannot f
await reviewTeammateLearning('session', 'reply');
expect(query.mock.calls.at(-1)![1]).toEqual(['session', 1, 3, 'session', 41]);
expect(h.writeWorking).toHaveBeenLastCalledWith('/test-owner', 'bot', expect.objectContaining({ coveredThroughRowid: 5, coveredThroughMessageId: 'reply' }), 4);
+ // 有 client_id 但解析不到(水位消息已被删 / 撤回):同样不信存下的 500,从对话开头回补。
+ h.meta = { turnCompleted: true };
+ h.floorRowid = null;
+ h.model.mockResolvedValueOnce({ ok: true, text: JSON.stringify({ memories: [], skills: [], working: { commitments: [] } }) });
+ await reviewTeammateLearning('session', 'reply-gone');
+ expect(query.mock.calls.at(-1)![1]).toEqual(['session', 0, 3, 'session', 41]);
// 没有账本:从对话开头起算,整段历史按缺口分批回补,而不是从上一条完成回复直接跳过去。
h.meta = { turnCompleted: true };
h.working = null;
diff --git a/apps/desktop/src/main/maker-ipc/botLearningReview.ts b/apps/desktop/src/main/maker-ipc/botLearningReview.ts
index 475c7c2ab7..778f6cf61f 100644
--- a/apps/desktop/src/main/maker-ipc/botLearningReview.ts
+++ b/apps/desktop/src/main/maker-ipc/botLearningReview.ts
@@ -117,25 +117,23 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)
// 只推进到连续处理过的前缀末尾——补不完就不推进到当前回复,账本在补完前不算新鲜,换窗线
// 不会因为一份有缺口的账本提前。每批有界且至少前进一条,几轮即收敛。
//
- // 水位的持久判据是消息 client_id(rowid 在瘦身 VACUUM 后可能重排),按它解析回当前 rowid;
- // 解析不到(水位消息被删 / 撤回)退回存下的 rowid。没有账本(老对话第一次建账本、或首轮
- // 复盘 claim 后失败)时从对话开头(/clear 之后)起算:整段历史按缺口分批回补,补完之前
- // 不算新鲜,不跳过任何一轮。
+ // 水位的持久判据是消息 client_id(rowid 在瘦身 VACUUM 后可能重排),按它解析回当前 rowid。
+ // 存下的 rowid 数字在任何情况下都不拿来当下界:旧账本没有 client_id、或水位消息已被删 /
+ // 撤回解析不到,以及根本没有账本(老对话第一次建账本、首轮复盘 claim 后失败),一律从对话
+ // 开头(/clear 之后)起算——整段历史按缺口分批回补,补完之前不算新鲜,不跳过任何一轮。
type Watermark = { rowid: number; id: string | undefined };
- const floor: Watermark = workingBefore
- ? {
- rowid:
- (workingBefore.coveredThroughMessageId
- ? (
- await db.queryOne<{ rowid: number }>(
- `SELECT rowid FROM messages WHERE session_id=? AND client_id=? /* ledger-floor */`,
- [sessionId, workingBefore.coveredThroughMessageId],
- )
- )?.rowid
- : undefined) ?? workingBefore.coveredThroughRowid,
- id: workingBefore.coveredThroughMessageId,
- }
- : { rowid: 0, id: undefined };
+ const resolvedFloor = workingBefore?.coveredThroughMessageId
+ ? (
+ await db.queryOne<{ rowid: number }>(
+ `SELECT rowid FROM messages WHERE session_id=? AND client_id=? /* ledger-floor */`,
+ [sessionId, workingBefore.coveredThroughMessageId],
+ )
+ )?.rowid
+ : undefined;
+ const floor: Watermark =
+ resolvedFloor !== undefined
+ ? { rowid: resolvedFloor, id: workingBefore!.coveredThroughMessageId }
+ : { rowid: 0, id: undefined };
let covered: Watermark = turnTruncated ? { rowid: prevCompleted, id: prev?.id } : { rowid: row.rowid, id: replyId };
let gapMessages: ReturnType<typeof toSnapshot>[] = [];
if (floor.rowid < prevCompleted) {