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): resolve the ledger cursor only against the visible timeline

zqchrisd6192ec63a8c
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 9dec5465c6..741a93437e 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
@@ -31,7 +31,7 @@ vi.mock('../../appSessionState.js', () => ({
}));
vi.mock('../../localDb/client/current.js', () => {
const db = {
- queryOne: async (sql: string) =>
+ queryOne: vi.fn(async (sql: string) =>
sql.includes('prev-completed')
? h.prevCompleted ? { id: 'prev', rowid: h.prevCompleted } : undefined
: sql.includes('ledger-floor')
@@ -44,7 +44,7 @@ vi.mock('../../localDb/client/current.js', () => {
rowid: (h.prevCompleted ?? 0) + 2,
meta: JSON.stringify(h.meta),
}
- : undefined,
+ : undefined),
query: vi.fn(async (sql: string) =>
sql.includes('ORDER BY rowid ASC')
? h.gapRows
@@ -367,6 +367,12 @@ it('resolves the ledger floor by client_id so a VACUUM-renumbered rowid cannot f
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]);
+ // 游标解析只认可见时间线上的行:被 rewind / 删成墓碑 / 落在 /clear 之前的都算解析不到。
+ const queryOne = (getDbClient() as unknown as { queryOne: ReturnType<typeof vi.fn> }).queryOne;
+ const floorSql = String(queryOne.mock.calls.find((call) => String(call[0]).includes('ledger-floor'))![0]);
+ expect(floorSql).toContain('rewind_at IS NULL');
+ expect(floorSql).toContain("role != 'message_tombstone'");
+ expect(floorSql).toContain('cleared_at');
// 没有账本:从对话开头起算,整段历史按缺口分批回补,而不是从上一条完成回复直接跳过去。
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 cf98d8e2eb..82fd908e37 100644
--- a/apps/desktop/src/main/maker-ipc/botLearningReview.ts
+++ b/apps/desktop/src/main/maker-ipc/botLearningReview.ts
@@ -179,11 +179,14 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)
// 开头(/clear 之后)起算——整段历史按缺口分批回补,补完之前不算新鲜,不跳过任何一轮。
// 水位可以停在一条长消息中间(offset = 已覆盖的字符数):下一次从同一条消息的这个偏移接着取。
type Watermark = { rowid: number; id: string | undefined; offset?: number };
+ // 只认仍在可见时间线上的行:被 rewind 软删、被删成墓碑、/clear 之前的水位消息都按
+ // 解析不到——否则一条已撤回的回复会把下界撑在上一条完成回复之上,跳过幸存历史的回补。
const resolvedFloor = workingBefore?.coveredThroughMessageId
? (
await db.queryOne<{ rowid: number }>(
- `SELECT rowid FROM messages WHERE session_id=? AND client_id=? /* ledger-floor */`,
- [sessionId, workingBefore.coveredThroughMessageId],
+ `SELECT rowid FROM messages WHERE session_id=? AND client_id=? AND rewind_at IS NULL
+ AND role != 'message_tombstone' ${clearedAtFilter} /* ledger-floor */`,
+ [sessionId, workingBefore.coveredThroughMessageId, sessionId],
)
)?.rowid
: undefined;
diff --git a/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts b/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts
index 872d780c7c..fd9abe0213 100644
--- a/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts
+++ b/apps/desktop/src/main/maker-ipc/botWorkingMemory.ts
@@ -17,7 +17,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
-import { and, eq, inArray, isNull, sql } from 'drizzle-orm';
+import { and, eq, inArray, isNull, ne, sql } from 'drizzle-orm';
import { getDbClient } from '../localDb/client/current.js';
import { botDelegations, botProfiles, botSessionLinks, messages, sessions } from '../localDb/schema.js';
@@ -681,10 +681,19 @@ export async function isWorkingMemoryFreshForSession(sessionId: string, userData
// 没有 client_id(旧账本)或水位消息只覆盖了一部分(长消息分块中):都不算新鲜。
if (!stored?.coveredThroughMessageId || stored.coveredThroughOffset !== undefined) return false;
const db = getDbClient().drizzle;
+ // 水位消息必须仍在可见时间线上(未 rewind、非墓碑、在 /clear 之后),否则按不新鲜。
const [covered] = await db
.select({ rowid: sql<number | null>`rowid` })
.from(messages)
- .where(and(eq(messages.sessionId, sessionId), eq(messages.clientId, stored.coveredThroughMessageId)))
+ .where(
+ and(
+ eq(messages.sessionId, sessionId),
+ eq(messages.clientId, stored.coveredThroughMessageId),
+ isNull(messages.rewindAt),
+ ne(messages.role, 'message_tombstone'),
+ sql`${messages.createdAt} > COALESCE((SELECT cleared_at FROM sessions WHERE id = ${sessionId}), 0)`,
+ ),
+ )
.limit(1);
if (typeof covered?.rowid !== 'number') return false;
const [row] = await db