Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(chat): keep media settlement off the message write queue
xd94a73e7e0fb9
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/assistantFinalRetraction.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/assistantFinalRetraction.test.tsindex ad12ce065b..54a19e964b 100644--- a/apps/desktop/src/main/localDb/ipc/__tests__/assistantFinalRetraction.test.ts+++ b/apps/desktop/src/main/localDb/ipc/__tests__/assistantFinalRetraction.test.ts@@ -315,8 +315,9 @@ it.each(['Corrected without a link', 'https://github.com/o/r/pull/3'])(},);-it.each([{ shared: false, clear: false }, { shared: true, clear: false }, { shared: false, clear: true }])(- 'settles pending media publication before reconciling withdrawn references (%j)', async ({ shared, clear }) => {+it.each([{ shared: false, clear: false }, { shared: true, clear: false }, { shared: false, clear: true }]+ .flatMap(options => ['retraction', 'correction'].map(mode => ({ ...options, mode }))))(+ 'settles media cleanup without blocking another task (%j)', async ({ shared, clear, mode }) => {const hash = 'a'.repeat(64);const url = `cindy-media://blobs/${hash}.png`;h.sqlite!.prepare("INSERT INTO media_blobs VALUES (?, '.png', 'image/png', 8, 0, 1, 1)").run(hash);@@ -335,27 +336,33 @@ it.each([{ shared: false, clear: false }, { shared: true, clear: false }, { sharconst originalTx = h.client!.tx;h.client!.tx = async (name, args) => {const result = await originalTx(name, args);- if ((args as { publication?: string }).publication === 'rollback') deleted();+ if ((args as { publication?: string }).publication === 'rollback' || name === 'message.updateContent') deleted();return result;};try {onAssistantTextEvent('s1', { text: url, agentMessageId: 'withdraw-media' }, null);flushAssistantBlock('s1');await drainPersistQueue();- onAssistantTextEvent('s1', { text: '', agentMessageId: 'withdraw-media', isFinal: true, isFullText: true }, null);+ onAssistantTextEvent('s1', { text: mode === 'retraction' ? '' : 'Corrected without media', agentMessageId: 'withdraw-media', isFinal: true, isFullText: true }, null);await rollback;if (clear) {const boundary = Date.now() + 1000;h.sqlite!.prepare('UPDATE sessions SET cleared_at = ? WHERE id = ?').run(boundary, 's1');noteSessionClearBoundary('s1', boundary);}+ onAssistantTextEvent('s2', { text: 'Unrelated task', agentMessageId: 'other-b', isFinal: true, isFullText: true }, null);+ await vi.waitFor(() => {+ expect(h.sqlite!.prepare("SELECT count(*) AS n FROM messages WHERE session_id='s2'").get()).toEqual({ n: 1 });+ });release();await drainPersistQueue();await vi.mocked(commitMessageMediaRefs).mock.results[0].value;- expect(h.sqlite!.prepare("SELECT count(*) AS n FROM media_refs WHERE ref_kind = 'session-attachment'").get())- .toEqual({ n: shared ? 1 : 0 });+ await vi.waitFor(() => {+ expect(h.sqlite!.prepare("SELECT count(*) AS n FROM media_refs WHERE ref_kind = 'session-attachment'").get())+ .toEqual({ n: shared ? 1 : 0 });+ });expect(h.sqlite!.prepare('SELECT count(*) AS n FROM media_blobs').get()).toEqual({ n: 1 });- } finally { release(); }+ } finally { release(); await drainPersistQueue(); clearSessionPersistState('s2'); }});@@ -425,7 +432,9 @@ it.each(['after-update', 'during-preview', 'row-deleted'] as const)(onAssistantTextEvent('s1', { text: 'Corrected without media', agentMessageId: 'correct-clear-media', isFinal: true, isFullText: true }, null);await drainPersistQueue();await vi.mocked(commitMessageMediaRefs).mock.results[0].value;- expect(h.sqlite!.prepare("SELECT count(*) AS n FROM media_refs WHERE ref_kind = 'session-attachment'").get()).toEqual({ n: 0 });+ await vi.waitFor(() => {+ expect(h.sqlite!.prepare("SELECT count(*) AS n FROM media_refs WHERE ref_kind = 'session-attachment'").get()).toEqual({ n: 0 });+ });expect(h.sqlite!.prepare('SELECT count(*) AS n FROM media_blobs').get()).toEqual({ n: 1 });expect(h.broadcast.mock.calls.filter(([channel]) => channel === 'local-db:sessions:patched')).toHaveLength(0);} finally { release(); preview.mockRestore(); }diff --git a/apps/desktop/src/main/localDb/ipc/messages.ts b/apps/desktop/src/main/localDb/ipc/messages.tsindex bf187f7511..35257789e2 100644--- a/apps/desktop/src/main/localDb/ipc/messages.ts+++ b/apps/desktop/src/main/localDb/ipc/messages.ts@@ -1488,26 +1488,9 @@ export async function supersedeRetriedUserTurn(return clientIds;}-/** Reconcile only the corrected row's derived state against current live history. */-async function reconcileAssistantDerivedState(- row: MessageRow,- dbClient: DbClient,- isCurrent: () => boolean,- ownerScope: DataOwnerBroadcastScope | null,- refreshPreview: boolean,-): Promise<void> {- await Promise.all([- pendingPrRefWrites.get(dbClient)?.get(row.id),- ...(pendingMediaWrites.get(dbClient)?.get(row.id) ?? []),- ]);- try {- if (isCurrent()) await recomputePrRefsForSession(row.sessionId, { dbClient, isCurrent });- } catch (error) {- log.warn('assistant correction PR reference reconciliation failed', {- sessionId: row.sessionId, clientId: row.clientId,- error: error instanceof Error ? error.message : String(error),- });- }+/** Settle media publication off the global message queue, using its captured DB. */+async function reconcileAssistantMediaRefs(row: MessageRow, dbClient: DbClient): Promise<void> {+ await Promise.all([...(pendingMediaWrites.get(dbClient)?.get(row.id) ?? [])]);// Cleanup belongs to the committed mutation even if clear/owner changes while// publication settles. Use its captured DB and atomic live-message predicate;// never republish derived state into the replacement scope.@@ -1524,6 +1507,33 @@ async function reconcileAssistantDerivedState(});}}+}++/** Reconcile only the corrected row's derived state against current live history. */+async function reconcileAssistantDerivedState(+ row: MessageRow,+ dbClient: DbClient,+ isCurrent: () => boolean,+ ownerScope: DataOwnerBroadcastScope | null,+ refreshPreview: boolean,+): Promise<void> {+ // Slow media publication must not hold every task's message write queue.+ // Cleanup still waits for this row's hooks and retains the live-reference check.+ void reconcileAssistantMediaRefs(row, dbClient).catch((error) => {+ log.warn('assistant correction media reconciliation failed', {+ sessionId: row.sessionId, clientId: row.clientId,+ error: error instanceof Error ? error.message : String(error),+ });+ });+ await pendingPrRefWrites.get(dbClient)?.get(row.id);+ try {+ if (isCurrent()) await recomputePrRefsForSession(row.sessionId, { dbClient, isCurrent });+ } catch (error) {+ log.warn('assistant correction PR reference reconciliation failed', {+ sessionId: row.sessionId, clientId: row.clientId,+ error: error instanceof Error ? error.message : String(error),+ });+ }if (!refreshPreview || !isCurrent()) return;try {const latest = await latestVisiblePreviewRow(row.sessionId);