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): tolerate malformed history in result association

zqchris04e1b83ff9ca
diff --git a/apps/desktop/src/main/__tests__/botTaskReplyResults.test.ts b/apps/desktop/src/main/__tests__/botTaskReplyResults.test.ts
index f899a13f7e..9c7c25f586 100644
--- a/apps/desktop/src/main/__tests__/botTaskReplyResults.test.ts
+++ b/apps/desktop/src/main/__tests__/botTaskReplyResults.test.ts
@@ -73,3 +73,15 @@ it('does not lose earlier attachments when a repeated seal sees only part of the
row('final', 'assistant', 'Both ready', { turnCompleted: true, botTaskResults: [card('a'), card('b')] });
expect(await bind([ids.completionRun('b', 1)])).toEqual([card('a'), card('b')]);
});
+
+it('ignores corrupt historic metadata without losing new results or valid duplicate ownership', async () => {
+ row('corrupt', 'assistant', 'Old reply');
+ sqlite.prepare('UPDATE messages SET agent_meta=? WHERE client_id=?').run('{truncated', 'corrupt');
+ row('old-shape', 'assistant', 'Old reply', { turnCompleted: true, botTaskResults: ['legacy', null, 7] });
+ receipt(); row('final', 'assistant', 'Ready');
+ expect(await bind()).toEqual([card()]);
+ row('valid-owner', 'assistant', 'Already delivered', { turnCompleted: true, botTaskResults: [card()] });
+ expect(await bind()).toEqual([]);
+ expect(sqlite.prepare('SELECT agent_meta FROM messages WHERE client_id=?').get('corrupt'))
+ .toEqual({ agent_meta: '{truncated' });
+});
diff --git a/apps/desktop/src/main/botTaskReplyResults.ts b/apps/desktop/src/main/botTaskReplyResults.ts
index 4d002f2643..5b6d417cb1 100644
--- a/apps/desktop/src/main/botTaskReplyResults.ts
+++ b/apps/desktop/src/main/botTaskReplyResults.ts
@@ -43,11 +43,18 @@ export async function readTaskResultsForReply(
if (card?.role !== 'delegation-result' || !card.result || card.parentSessionId !== sessionId
|| card.fromBotId !== reply.botId || botTaskResultKey(card) !== receiptId) continue;
// Retry/replayed wakes cannot attach the same execution to a second unrelated reply.
+ // Guard JSON at the function argument: WHERE evaluation order must not let an unrelated
+ // corrupt history row (or a legacy scalar attachment) abort this new reply's association.
const existing = await db.queryOne<{ found: number }>(`
- SELECT 1 AS found FROM messages m, json_each(m.agent_meta, '$.botTaskResults') r
- WHERE m.session_id = ? AND m.client_id != ? AND m.role = 'assistant' AND m.rewind_at IS NULL
- AND json_extract(m.agent_meta, '$.turnCompleted') = 1
- AND json_extract(r.value, '$.delegationId') = ? AND json_extract(r.value, '$.result.runSequence') = ?
+ WITH historic_replies AS (
+ SELECT CASE WHEN json_valid(agent_meta) THEN agent_meta ELSE '{}' END AS meta
+ FROM messages
+ WHERE session_id = ? AND client_id != ? AND role = 'assistant' AND rewind_at IS NULL
+ )
+ SELECT 1 AS found FROM historic_replies, json_each(meta, '$.botTaskResults') r
+ WHERE json_extract(meta, '$.turnCompleted') = 1
+ AND json_extract(CASE WHEN r.type = 'object' THEN r.value ELSE '{}' END, '$.delegationId') = ?
+ AND json_extract(CASE WHEN r.type = 'object' THEN r.value ELSE '{}' END, '$.result.runSequence') = ?
LIMIT 1
`, [sessionId, replyClientId, card.delegationId, card.result.runSequence]);
if (!existing && !attached.has(receiptId)) {