Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): bound canonical reply tool scans to the next input
nana8d1838aeb895
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.tsindex 8702345b33..b2ceccf846 100644--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts@@ -4103,6 +4103,72 @@ 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.+ // 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 () => {+ 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', {+ botId: 'bot-1', rootSessionId: sessionId, rootClientId: 'root',+ });+ 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 });+ }+ for (let i = 0; i < 105; i++) {+ insertMessage(id, { id: `${id}-empty-${i}`, role: 'assistant', content: '', createdAt: 10000 + i });+ }+ }+ })();++ 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 [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(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<\?\)/);+ }+ });++ // The bound must preserve the former strict next_input < tool comparison, including+ // ties, absent boundaries and inputs that do not start a new turn (#6958).+ it.each([+ { label: 'next input before tool', inputAt: 2000, toolAt: 2001, visible: true },+ { label: 'next input tied with tool', inputAt: 2000, toolAt: 2000, visible: false },+ { label: 'input tied with reply', inputAt: 1000, toolAt: 2000, visible: false },+ { label: 'tool tied with reply', inputAt: 2000, toolAt: 1000, visible: true },+ { label: 'rewound input', inputAt: 2000, toolAt: 3000, rewindAt: 4000, visible: false },+ { label: 'auto-resume input', inputAt: 2000, toolAt: 3000, agentMeta: { autoResume: true }, visible: false },+ { label: 'steer input', inputAt: 2000, toolAt: 3000, agentMeta: { delivery: 'steer' }, visible: false },+ { label: 'rewound tool', inputAt: 3000, toolAt: 2000, toolRewindAt: 4000, visible: true },+ ])('preserves reply visibility at the turn boundary: $label', async ({ inputAt, toolAt, rewindAt, agentMeta, toolRewindAt, visible }) => {+ const sessionId = await canonicalFor('bot-1');+ insertMessage(sessionId, { id: 'reply', role: 'assistant', content: 'Reply', createdAt: 1000 });+ insertMessage(sessionId, { id: 'input', role: 'user', content: '', createdAt: inputAt, rewindAt, agentMeta });+ insertMessage(sessionId, { id: 'tool', role: 'tool_use', content: {}, createdAt: toolAt, rewindAt: toolRewindAt });+ expect(await unreadFor('bot-1', { 'bot-1': 1 })).toBe(visible ? 1 : 0);+ expect(await getBotRemoteResourceSource('bot-1')).toMatchObject({+ lastMessagePreview: visible ? 'Reply' : null,+ lastReplyAt: visible ? 1000 : 0,+ });+ });+it('reports zero when the caller has no read position for that Bot', async () => {const sessionId = await canonicalFor('bot-1');insertMessage(sessionId, {diff --git a/apps/desktop/src/main/localDb/ipc/bots.ts b/apps/desktop/src/main/localDb/ipc/bots.tsindex 9f67bd9943..88f716da45 100644--- a/apps/desktop/src/main/localDb/ipc/bots.ts+++ b/apps/desktop/src/main/localDb/ipc/bots.ts@@ -1002,20 +1002,21 @@ 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.- // Bound the lookup to this user turn so a later task cannot hide an old reply.+ // 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.sql`(${canonicalReplyCompleted()} OR NOT EXISTS (SELECT 1 FROM messages AS subsequent_toolWHERE subsequent_tool.session_id = ${messages.sessionId}AND subsequent_tool.created_at > ${messages.createdAt}AND subsequent_tool.rewind_at IS NULLAND subsequent_tool.role IN ('tool_use', 'tool_result', 'thinking')- AND NOT EXISTS (SELECT 1 FROM messages AS next_input+ AND subsequent_tool.created_at <= COALESCE((SELECT MIN(next_input.created_at) FROM messages AS next_inputWHERE next_input.session_id = ${messages.sessionId}AND next_input.role = 'user' AND next_input.rewind_at IS NULLAND next_input.created_at > ${messages.createdAt}- AND next_input.created_at < subsequent_tool.created_atAND (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'))+ AND (next_input.agent_meta IS NULL OR json_extract(next_input.agent_meta, '$.delivery') IS NOT 'steer')), 9223372036854775807)))`,];}