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。

perf(desktop): bound history outline stamp scan

ficowang1d3bc870af5d
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/historyViewOutline.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/historyViewOutline.test.ts
index 32ab761004..990086b0bf 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/historyViewOutline.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/historyViewOutline.test.ts
@@ -14,13 +14,22 @@ function outline(role: string, content: unknown, agentMeta: unknown = null, raw
} finally { db.close(); }
}
-function stamp(content: string) {
+function stamp(content: string | null) {
const db = new DatabaseSync(':memory:');
try {
db.exec('CREATE TABLE messages (content TEXT)');
db.prepare('INSERT INTO messages VALUES (?)').run(content);
const query = new SQLiteSyncDialect().sqlToQuery(historyOutlineContentStamp());
- return db.prepare(`SELECT ${query.sql} AS stamp FROM messages`).get(...query.params as string[])!.stamp as string;
+ return db.prepare(`SELECT ${query.sql} AS stamp FROM messages`).get(...query.params as string[])!.stamp as string | null;
+ } finally { db.close(); }
+}
+
+function legacyStamp(content: string | null): string | null {
+ const db = new DatabaseSync(':memory:');
+ try {
+ db.exec('CREATE TABLE messages (content TEXT)');
+ db.prepare('INSERT INTO messages VALUES (?)').run(content);
+ return db.prepare("SELECT length(content) || ':' || substr(content, -24) AS stamp FROM messages").get()!.stamp as string | null;
} finally { db.close(); }
}
@@ -44,9 +53,32 @@ describe('SQLite history outline', () => {
const summary = JSON.stringify('build summary');
const full = JSON.stringify(`build summary\n${'full log line\n'.repeat(50000)}`);
expect(stamp(summary)).not.toBe(stamp(full));
- expect(stamp(full).length).toBeLessThan(64);
+ expect(stamp(full)?.length).toBeLessThan(256);
expect(stamp(JSON.stringify('结果:完成'))).toBe(stamp(JSON.stringify('结果:完成')));
});
+ // The byte-length prototype missed the first pair: its character count changes
+ // while its UTF-8 byte count and suffix stay equal. Exercise the production SQL,
+ // including SQLite's embedded-NUL behavior, rather than duplicating it in JS.
+ it('does not weaken the character-based invalidation contract across UTF-8 and text boundaries', () => {
+ const tail = 'q'.repeat(120);
+ const pairs = [
+ [`ab${tail}`, `é${tail}`],
+ [`${tail}中文`, `${tail}中义`],
+ [`${'😀'.repeat(23)}a`, `${'😀'.repeat(24)}`],
+ [`${'😀'.repeat(24)}`, `${'😀'.repeat(23)}😁`],
+ [`${'e\u0301'.repeat(30)}${tail}`, `${'e\u0301'.repeat(29)}e${tail}`],
+ [JSON.stringify({ result: '完成😀', detail: `${tail}a` }), JSON.stringify({ result: '完成😀', detail: `${tail}b` })],
+ [`plain text ${tail}a`, `plain text ${tail}b`],
+ ];
+ for (const [before, after] of pairs) {
+ expect(legacyStamp(before)).not.toBe(legacyStamp(after));
+ expect(stamp(before)).not.toBe(stamp(after));
+ }
+ // SQLite's text functions stop at NUL, so these retain the exact legacy path.
+ for (const content of [null, '', `prefix\u0000${'😀'.repeat(30)}`]) {
+ expect(stamp(content)).toBe(legacyStamp(content));
+ }
+ });
it('does not return folded thinking, tool outputs or file bodies to the main thread', () => {
const body = 'private heavy body '.repeat(100000);
expect(JSON.stringify(outline('thinking', { text: body, durationMs: 1000 })).length).toBeLessThan(100);
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/messagesListCursor.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/messagesListCursor.test.ts
index 86a5b5faae..382ea64630 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/messagesListCursor.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/messagesListCursor.test.ts
@@ -6,7 +6,7 @@ import { messages, sessions } from '../../schema';
import * as taskImages from '../../../cindy-media/taskImageDelivery';
import * as currentDb from '../../client/current';
import { runDeviceLinkInvokeContext } from '../../../device-link/invoke-context';
-import { MAX_HISTORY_SCAN_ROWS } from '../historyViewReader';
+import { MAX_HISTORY_SCAN_ROWS, type HistoryPageShape } from '../historyViewReader';
import { historyViewLeaves, type HistoryViewPage, type HistoryDetailPage, type HistoryMessageSource } from '@cindy/maker-shared/message-window';
import type { DbClient } from '../../client/DbClient';
import { captureMediaUserTurn, refreshMediaUserTurnCost } from '../../../usage/mediaTurnCost';
@@ -189,11 +189,23 @@ describe('local-db:messages:list cursor', () => {
parent ? JSON.stringify({ parentUuid: parent, model: 'actual-child-model' }) : null, Number(id));
add('0', 'user', 'Visible question');
add('1', 'tool_use', { toolName: 'Agent', toolUseId: 'agent', input: { description: 'Inspect' } });
- for (let n = 2; n < 103; n++) add(String(n), 'thinking', { text: 'hidden '.repeat(1000), durationMs: 10 }, 'agent');
+ const unchangedByteTail = 'q'.repeat(200);
+ for (let n = 2; n < 103; n++) add(String(n), 'thinking', {
+ text: n === 2 ? `ab${unchangedByteTail}` : 'hidden '.repeat(1000), durationMs: 10,
+ }, 'agent');
add('103', 'assistant', 'Visible answer');
registerMessageIpc();
- const invoke = (channel: string, ...args: unknown[]) => runDeviceLinkInvokeContext({ controllerDeviceId: 'd', channel }, () => h.handlers.get(channel)!({}, 's1', ...args));
- const page = await invoke('local-db:messages:view', { lazyDetails: true }) as HistoryViewPage<HistoryMessageSource>;
+ const shapes: HistoryPageShape[] = [];
+ const historyView = {
+ disable() {}, setExpanded() {},
+ update(_keys: readonly string[], latest?: { shape?: HistoryPageShape }) {
+ if (latest?.shape) shapes.push(latest.shape);
+ },
+ };
+ const invoke = (channel: string, ...args: unknown[]) => runDeviceLinkInvokeContext({
+ controllerDeviceId: 'd', channel, historyView,
+ }, () => h.handlers.get(channel)!({}, 's1', ...args));
+ const page = await invoke('local-db:messages:view', { lazyDetails: true, workLines: true }) as HistoryViewPage<HistoryMessageSource>;
expect(page.hasMore).toBe(false);
expect(JSON.stringify(page)).not.toContain('hidden ');
expect(JSON.stringify(page)).toContain('Visible answer');
@@ -204,9 +216,14 @@ describe('local-db:messages:list cursor', () => {
expect(detail.messages.length).toBeGreaterThan(0);
expect(detail.messages.every((row) => (row.agentMeta as { parentUuid?: string })?.parentUuid === 'agent')).toBe(true);
expect(JSON.stringify(detail)).toContain('hidden ');
- expect(await invoke('local-db:messages:view', { lazyDetails: true })).toEqual(page);
- sqlite.prepare('UPDATE messages SET content = ? WHERE id = ?').run(JSON.stringify({ text: 'edited '.repeat(1000), durationMs: 10 }), '2');
- expect(await invoke('local-db:messages:view', { lazyDetails: true })).not.toEqual(page);
+ expect(await invoke('local-db:messages:view', { lazyDetails: true, workLines: true })).toEqual(page);
+ expect(shapes.at(-1)?.fingerprints).toEqual(shapes.at(-2)?.fingerprints);
+ // Same UTF-8 byte length and unchanged last 96 bytes, but a different character
+ // count ("ab" → "é"): the production UPDATE → outline entry must invalidate.
+ sqlite.prepare('UPDATE messages SET content = ? WHERE id = ?')
+ .run(JSON.stringify({ text: `é${unchangedByteTail}`, durationMs: 10 }), '2');
+ expect(await invoke('local-db:messages:view', { lazyDetails: true, workLines: true })).not.toEqual(page);
+ expect(shapes.at(-1)?.fingerprints).not.toEqual(shapes.at(-2)?.fingerprints);
});
it('keeps a command larger than the scan budget folded without losing its trailing artifact or full details', async () => {
const sqlite = createDb();
diff --git a/apps/desktop/src/main/localDb/ipc/historyViewOutline.ts b/apps/desktop/src/main/localDb/ipc/historyViewOutline.ts
index 6693b148a7..d9da363760 100644
--- a/apps/desktop/src/main/localDb/ipc/historyViewOutline.ts
+++ b/apps/desktop/src/main/localDb/ipc/historyViewOutline.ts
@@ -112,9 +112,20 @@ export function resetCommandArtifactCache(): void {
* tool result, a background completion). The outline already reads every body, so length
* and tail add CPU but no IO; grouping revisions hash it, and it never reaches the wire.
* A rewrite with the same length and the same last 24 characters is not detected.
+ * Keep that character-length contract: byte length would miss edits whose UTF-8 byte
+ * count stays constant while their character count changes. A 96-byte suffix covers
+ * the maximum UTF-8 span of the legacy 24-character suffix; hex keeps a truncated
+ * leading code point opaque but comparable. Embedded NUL needs the legacy path because
+ * SQLite text functions deliberately stop there.
*/
export function historyOutlineContentStamp(): SQL<string> {
- return sql<string>`length(${messages.content}) || ':' || substr(${messages.content}, -24)`;
+ return sql<string>`CASE
+ WHEN typeof(${messages.content}) <> 'text'
+ OR instr(CAST(${messages.content} AS BLOB), x'00') > 0
+ THEN length(${messages.content}) || ':' || substr(${messages.content}, -24)
+ ELSE length(${messages.content}) || ':' || hex(
+ substr(CAST(${messages.content} AS BLOB), -96))
+ END`;
}
/** File content/patches never cross the outline query; commands yield only paths on the wire. */