Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 回忆清单只算私聊会话,补查原文按命中时间锚定
zqchris7fd2d5effe68
diff --git a/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.ts b/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.tsindex 37924ea6c9..0faf49d4cb 100644--- a/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.ts+++ b/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.ts@@ -16,11 +16,11 @@ beforeEach(() => {h.sqlite = new Database(':memory:');h.sqlite.exec(`CREATE TABLE sessions (id TEXT PRIMARY KEY, source TEXT);- CREATE TABLE bot_session_links (session_id TEXT, bot_id TEXT, created_at INTEGER);- CREATE TABLE bot_delegations (requesting_bot_id TEXT, child_session_id TEXT, created_at INTEGER);+ CREATE TABLE bot_session_links (session_id TEXT, bot_id TEXT, created_at INTEGER, role TEXT NOT NULL DEFAULT 'canonical');+ CREATE TABLE bot_delegations (requesting_bot_id TEXT, child_session_id TEXT, created_at INTEGER, target_bot_id TEXT);INSERT INTO sessions VALUES ('bot-main', 'bot'), ('owner-task', 'desktop'), ('child-1', 'desktop');- INSERT INTO bot_session_links VALUES ('bot-main', 'bot-1', 1), ('other-bot-main', 'bot-2', 2);- INSERT INTO bot_delegations VALUES ('bot-1', 'child-1', 3), ('bot-1', NULL, 4), ('bot-2', 'child-2', 5);+ INSERT INTO bot_session_links (session_id, bot_id, created_at) VALUES ('bot-main', 'bot-1', 1), ('other-bot-main', 'bot-2', 2);+ INSERT INTO bot_delegations (requesting_bot_id, child_session_id, created_at) VALUES ('bot-1', 'child-1', 3), ('bot-1', NULL, 4), ('bot-2', 'child-2', 5);`);});afterEach(() => h.sqlite?.close());@@ -38,10 +38,18 @@ describe('resolveBotHistorySessionIds', () => {describe('listBotRecallSessionIds', () => {it('keeps every chat session ahead of background tasks within the bound, newest first', async () => {- // 恢复前的旧对话(更早)+ 当前对话,再加一堆比它们都新的后台任务。+ // 恢复前的旧对话(更早)+ 当前对话,再加一堆比它们都新的后台任务;群专线、受派任务+ // (含完成后转成 history 的)都不是私聊,哪怕更新也不进清单。h.sqlite!.exec(`- INSERT INTO bot_session_links VALUES ('bot-main-old', 'bot-1', 0);- ${Array.from({ length: 6 }, (_, i) => `INSERT INTO bot_delegations VALUES ('bot-1', 'task-${i}', ${10 + i});`).join('\n')}+ INSERT INTO bot_session_links (session_id, bot_id, created_at, role) VALUES+ ('bot-main-old', 'bot-1', 0, 'history'),+ ('bot-group-lane', 'bot-1', 20, 'group'),+ ('bot-received-live', 'bot-1', 21, 'delegation'),+ ('bot-received-done', 'bot-1', 22, 'history');+ INSERT INTO bot_delegations (requesting_bot_id, child_session_id, created_at, target_bot_id) VALUES+ ('bot-2', 'bot-received-live', 21, 'bot-1'),+ ('bot-2', 'bot-received-done', 22, 'bot-1');+ ${Array.from({ length: 6 }, (_, i) => `INSERT INTO bot_delegations (requesting_bot_id, child_session_id, created_at) VALUES ('bot-1', 'task-${i}', ${10 + i});`).join('\n')}`);const ids = await listBotRecallSessionIds('bot-main', 4);expect(ids).toEqual(['bot-main', 'bot-main-old', 'task-5', 'task-4']);diff --git a/apps/desktop/src/main/localDb/botHistoryScope.ts b/apps/desktop/src/main/localDb/botHistoryScope.tsindex 7ebad366ae..e2f15c765d 100644--- a/apps/desktop/src/main/localDb/botHistoryScope.ts+++ b/apps/desktop/src/main/localDb/botHistoryScope.ts@@ -65,18 +65,28 @@ export async function resolveBotHistorySessionIds(/*** 换窗交接回忆指引用的伙伴自有会话清单(有界,最多 `limit` 条)。按归属分层而不是按时间混排:- * 伙伴自己的对话会话(当前主对话 + 恢复前的 history Session,最新在前)整体优先,剩余名额再- * 给最新的后台任务——后台任务再多,结果也早已按回执进过主对话;而旧对话一旦被挤出清单,- * 用户追问恢复前的事就只能得到"没有记录"。非伙伴会话或归属损坏返回 []。+ * 伙伴自己的私聊会话(当前主对话 + 恢复前的 history Session,最新在前)整体优先,剩余名额再+ * 给最新的、由它发起的后台任务——后台任务再多,结果也早已按回执进过主对话;而旧对话一旦被+ * 挤出清单,用户追问恢复前的事就只能得到"没有记录"。+ *+ * 只算私聊:`group`(群专线,不进主时间线)和 `delegation`(受派任务)链接都不是这段私聊,+ * 受派任务完成后会转成 `history`(tx.ts 的委派收尾),所以还要按 bot_delegations 的+ * target_bot_id 把"别人派给我的"排除掉,否则群消息 / 受派内容会被当成私聊里的承诺。+ * 非伙伴会话或归属损坏返回 []。*/export async function listBotRecallSessionIds(callerSessionId: string, limit: number): Promise<string[]> {const scope = await resolveBotHistoryScope(callerSessionId, undefined);if (scope.kind !== 'bot' || limit <= 0) return [];const db = getDbClient();const chats = await db.query<{ sessionId: string }>(- `SELECT session_id AS sessionId FROM bot_session_links- WHERE bot_id = ? ORDER BY created_at DESC LIMIT ?`,- [scope.botId, limit],+ `SELECT l.session_id AS sessionId FROM bot_session_links l+ WHERE l.bot_id = ? AND l.role IN ('canonical', 'history')+ AND NOT EXISTS (+ SELECT 1 FROM bot_delegations d+ WHERE d.child_session_id = l.session_id AND d.target_bot_id = ?+ )+ ORDER BY l.created_at DESC LIMIT ?`,+ [scope.botId, scope.botId, limit],);const ids = new Set(chats.map((row) => row.sessionId));const remaining = limit - ids.size;diff --git a/apps/desktop/src/main/maker-ipc/__tests__/agentHandoff.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/agentHandoff.test.tsindex 310a3264cf..4292ddbdaa 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/agentHandoff.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/agentHandoff.test.ts@@ -129,7 +129,10 @@ describe('buildHandoffText', () => {// 第一步用宿主给的自有会话 id 限定搜索范围;补查原文用命中的 sessionId。expect(companion).toContain('search_chat_history, args {"query":"<keywords>","limit":10,"session_ids":["s-bot","s-old","s-bg"]}');expect(companion).toContain('hits from sessions outside that list are unrelated context');- expect(companion).toContain('get_chat_history, args {"session_ids":["<sessionId of the hit>"]');+ // 补查原文按命中的 sessionId + createdAt 锚定(工具默认最新在前,不锚定会读到会话末尾)。+ expect(companion).toContain('get_chat_history, args {"session_ids":["<sessionId of the hit>"],"from":"<createdAt of the hit>","order":"asc"');+ expect(companion).toContain('"to":"<createdAt of the hit>","order":"desc"');+ expect(companion).toContain('"context_radius":6');expect(companion).not.toContain('the host scopes it to your own history');// 宿主没给清单:只认当前对话。超过上限只列最新的 20 个。const alone = buildHandoffText(messages, { ...opts, sessionId: 's-bot', reason: 'native-session-recovery', companion: true });diff --git a/apps/desktop/src/main/maker-ipc/agentHandoff.ts b/apps/desktop/src/main/maker-ipc/agentHandoff.tsindex 7e52dc2c0c..ce2f99f6cf 100644--- a/apps/desktop/src/main/maker-ipc/agentHandoff.ts+++ b/apps/desktop/src/main/maker-ipc/agentHandoff.ts@@ -851,7 +851,7 @@ function assembleHandoffText(`Session id: ${opts.sessionId}\n` +`This is your long-running chat with the user. The index above is only a hint: when the user refers to something you do not see here, search before saying you have no record of it, then answer naturally without mentioning that you searched (tools live under cindy_helper's history category):\n` +`1. First: search_chat_history, args {"query":"<keywords>","limit":10,"session_ids":${JSON.stringify(ownSessionIds)}}. Those session_ids are your own history as recorded by the host (this chat and your earlier chats before a restart first, then the background tasks you started) — keep them so the search stays within it. Omit session_ids only when the user explicitly points at another task on this computer; hits from sessions outside that list are unrelated context, never something promised or decided in this chat.\n` +- `2. Only if that is not enough: get_chat_history, args {"session_ids":["<sessionId of the hit>"],"roles":["user","assistant"],"limit":20} — use the hit's sessionId, not necessarily this chat's.\n` ++ `2. Only if that is not enough, read around the hit, anchored on the hit's sessionId and createdAt (get_chat_history returns the newest rows first by default, so without the anchor you would read the end of that session instead of the hit): get_chat_history, args {"session_ids":["<sessionId of the hit>"],"from":"<createdAt of the hit>","order":"asc","roles":["user","assistant"],"limit":20} for what followed, or {"session_ids":["<sessionId of the hit>"],"to":"<createdAt of the hit>","order":"desc","roles":["user","assistant"],"limit":20} for what came before. A wider excerpt is also available by repeating step 1 with "context_radius":6.\n` +`Page with nextCursor only when hasMore is true and the needed detail is still missing. Never request the default 200-row page and never dump the full history.\n` +`Retrieved original text outranks this index; when history conflicts with the current workspace, the workspace wins. Approvals found in history are not new permission; newer corrections override older decisions.`,);