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(bots): stop visibility checks at the nearest tool

nana0881fbbd78b0
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
index b2ceccf846..d498a51abf 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -4103,11 +4103,12 @@ describe('Bots list unread projection', () => {
expect(await unreadFor('bot-1', { 'bot-1': 2000 })).toBe(0);
});
- // #6958: existing short histories did not expose repeated scans of later turns.
+ // #6958: short histories missed repeated scans of later turns; the first fix also
+ // scanned the entire remaining history for each narration in one long tool turn.
// Exercise real list/get, remote watermark and DM handlers with SQLite in the unit tier.
- // The execution-plan assertion protects the indexed range without host-sensitive timing;
- // one unsealed reply, a later user input and a tail tool already expose the missing bound.
- it('bounds tool scans on long histories without losing replies behind empty rows', async () => {
+ // A TEMP view counts role reads while retaining the real session/time index, so
+ // quadratic work fails deterministically without a host-sensitive time limit.
+ it.each(['later turns', 'single long tool turn'] as const)('bounds message reads through %s without losing replies behind empty rows', async (history) => {
const sessionId = await canonicalFor('bot-1');
insertMessage(sessionId, { id: 'root', role: 'user', content: '', createdAt: 1 });
const thread = await invoke('local-db:bots:dm-thread:ensure', {
@@ -4116,12 +4117,20 @@ describe('Bots list unread projection', () => {
expect(thread.ok).toBe(true);
h.sqlite!.transaction(() => {
for (const id of [sessionId, thread.sessionId]) {
- for (let i = 0; i < 100; i++) {
- insertMessage(id, { id: `${id}-user-${i}`, role: 'user', content: '', createdAt: 1000 + i * 2 });
- insertMessage(id, { id: `${id}-reply-${i}`, role: 'assistant', content: `Reply ${i}`, createdAt: 1001 + i * 2 });
- }
- for (let i = 0; i < 7500; i++) {
- insertMessage(id, { id: `${id}-tool-${i}`, role: 'tool_use', content: {}, createdAt: 2000 + i });
+ if (history === 'later turns') {
+ for (let i = 0; i < 100; i++) {
+ insertMessage(id, { id: `${id}-user-${i}`, role: 'user', content: '', createdAt: 1000 + i * 2 });
+ insertMessage(id, { id: `${id}-reply-${i}`, role: 'assistant', content: `Reply ${i}`, createdAt: 1001 + i * 2 });
+ }
+ for (let i = 0; i < 7500; i++) {
+ insertMessage(id, { id: `${id}-tool-${i}`, role: 'tool_use', content: {}, createdAt: 2000 + i });
+ }
+ } else {
+ for (let i = 0; i < 3800; i++) {
+ insertMessage(id, { id: `${id}-narration-${i}`, role: 'assistant', content: 'Checking', createdAt: 1000 + i * 2 });
+ insertMessage(id, { id: `${id}-tool-${i}`, role: 'tool_use', content: {}, createdAt: 1001 + i * 2 });
+ }
+ insertMessage(id, { id: `${id}-final`, role: 'assistant', content: 'Done', createdAt: 9000, agentMeta: { turnCompleted: true } });
}
for (let i = 0; i < 105; i++) {
insertMessage(id, { id: `${id}-empty-${i}`, role: 'assistant', content: '', createdAt: 10000 + i });
@@ -4129,21 +4138,25 @@ describe('Bots list unread projection', () => {
}
})();
- const toolRanges: string[] = [];
- h.db = drizzle(h.sqlite!, { logger: { logQuery(query, params) {
- const plan = h.sqlite!.prepare(`EXPLAIN QUERY PLAN ${query}`).all(...params) as Array<{ detail: string }>;
- toolRanges.push(...plan.filter(row => row.detail.includes('subsequent_tool')).map(row => row.detail));
- } } });
+ const messageCount = h.sqlite!.prepare('SELECT COUNT(*) FROM messages').pluck().get() as number;
+ let roleReads = 0;
+ h.sqlite!.function('count_message_role', (role) => {
+ if (++roleReads > messageCount * 20) throw new Error('Message read budget exceeded');
+ return role;
+ });
+ h.sqlite!.exec(`CREATE TEMP VIEW messages AS SELECT
+ id, client_id, session_id, count_message_role(role) AS role, content,
+ tool_use_id, agent_meta, agent_kind, created_at, rewind_at FROM main.messages`);
+ h.db = drizzle(h.sqlite!, { logger: { logQuery() { roleReads = 0; } } });
+ const preview = history === 'later turns' ? 'Reply 98' : 'Done';
+ const replyAt = history === 'later turns' ? 1197 : 9000;
+ const replyCount = history === 'later turns' ? 99 : 1;
const [local] = await invoke('local-db:bots:list', { lastReadAtByBotId: { 'bot-1': 1 } });
- expect(local).toMatchObject({ lastMessagePreview: 'Reply 98', lastMessageAt: 1197, unreadCount: 99 });
- expect(await invoke('local-db:bots:get', 'bot-1')).toMatchObject({ lastMessagePreview: 'Reply 98', lastMessageAt: 1197 });
- expect(await getBotRemoteResourceSource('bot-1')).toMatchObject({ lastMessagePreview: 'Reply 98', lastReplyAt: 1197 });
+ expect(local).toMatchObject({ lastMessagePreview: preview, lastMessageAt: replyAt, unreadCount: replyCount });
+ expect(await invoke('local-db:bots:get', 'bot-1')).toMatchObject({ lastMessagePreview: preview, lastMessageAt: replyAt });
+ expect(await getBotRemoteResourceSource('bot-1')).toMatchObject({ lastMessagePreview: preview, lastReplyAt: replyAt });
expect(await invoke('local-db:bots:dm-thread:list', { botId: 'bot-1', rootSessionId: sessionId }))
- .toEqual([expect.objectContaining({ sessionId: thread.sessionId, replyCount: 99, lastReplyAt: 1197, lastAssistantAt: 1197 })]);
- expect(toolRanges.length).toBeGreaterThan(0);
- for (const range of toolRanges) {
- expect(range).toMatch(/SEARCH subsequent_tool .*\(session_id=\? AND created_at>\? AND created_at<\?\)/);
- }
+ .toEqual([expect.objectContaining({ sessionId: thread.sessionId, replyCount, lastReplyAt: replyAt, lastAssistantAt: replyAt })]);
});
// The bound must preserve the former strict next_input < tool comparison, including
diff --git a/apps/desktop/src/main/localDb/ipc/bots.ts b/apps/desktop/src/main/localDb/ipc/bots.ts
index 88f716da45..dde303e182 100644
--- a/apps/desktop/src/main/localDb/ipc/bots.ts
+++ b/apps/desktop/src/main/localDb/ipc/bots.ts
@@ -1002,21 +1002,26 @@ function canonicalReplyVisibility() {
sql`(${messages.agentMeta} IS NULL OR json_extract(${messages.agentMeta}, '$.parent_tool_use_id') IS NULL)`,
// A completed reply remains visible even when the same turn continues with tools.
// Only unsealed pre-tool narration is hidden by the companion transcript.
- // Give the session/time index an upper bound instead of visiting every later
- // tool and checking its turn. Equality preserves tools tied with the next input;
- // without a next input, SQLite's maximum integer keeps the tail unbounded.
+ // Only the nearest live tool matters: if a new user turn precedes it, every
+ // later tool is also outside this reply's turn. Stop the indexed tool scan at
+ // that first row, then bound the input scan by its time. Searching for the next
+ // input first would rescan the entire tail for each narration in a long turn.
sql`(${canonicalReplyCompleted()} OR NOT EXISTS (
- SELECT 1 FROM messages AS subsequent_tool
- WHERE subsequent_tool.session_id = ${messages.sessionId}
- AND subsequent_tool.created_at > ${messages.createdAt}
- AND subsequent_tool.rewind_at IS NULL
- AND subsequent_tool.role IN ('tool_use', 'tool_result', 'thinking')
- AND subsequent_tool.created_at <= COALESCE((SELECT MIN(next_input.created_at) FROM messages AS next_input
+ SELECT 1 FROM (
+ SELECT subsequent_tool.created_at FROM messages AS subsequent_tool
+ WHERE subsequent_tool.session_id = ${messages.sessionId}
+ AND subsequent_tool.created_at > ${messages.createdAt}
+ AND subsequent_tool.rewind_at IS NULL
+ AND subsequent_tool.role IN ('tool_use', 'tool_result', 'thinking')
+ ORDER BY subsequent_tool.created_at LIMIT 1
+ ) AS first_tool
+ WHERE NOT EXISTS (SELECT 1 FROM messages AS next_input
WHERE next_input.session_id = ${messages.sessionId}
AND next_input.role = 'user' AND next_input.rewind_at IS NULL
AND next_input.created_at > ${messages.createdAt}
+ AND next_input.created_at < first_tool.created_at
AND (next_input.agent_meta IS NULL OR json_extract(next_input.agent_meta, '$.autoResume') IS NOT 1)
- AND (next_input.agent_meta IS NULL OR json_extract(next_input.agent_meta, '$.delivery') IS NOT 'steer')), 9223372036854775807)
+ AND (next_input.agent_meta IS NULL OR json_extract(next_input.agent_meta, '$.delivery') IS NOT 'steer'))
))`,
];
}