Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 回忆指引的自有会话清单按归属分层,旧对话不被后台任务挤掉
zqchrisf487482af1ec
diff --git a/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.ts b/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.tsindex 4f1aad9d51..37924ea6c9 100644--- a/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.ts+++ b/apps/desktop/src/main/localDb/__tests__/botHistoryScope.test.ts@@ -10,7 +10,7 @@ vi.mock('../client/current.js', () => ({}),}));-import { resolveBotHistorySessionIds } from '../botHistoryScope.js';+import { listBotRecallSessionIds, resolveBotHistorySessionIds } from '../botHistoryScope.js';beforeEach(() => {h.sqlite = new Database(':memory:');@@ -35,3 +35,22 @@ describe('resolveBotHistorySessionIds', () => {expect(await resolveBotHistorySessionIds('missing', undefined)).toEqual([]);});});++describe('listBotRecallSessionIds', () => {+ it('keeps every chat session ahead of background tasks within the bound, newest first', async () => {+ // 恢复前的旧对话(更早)+ 当前对话,再加一堆比它们都新的后台任务。+ 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')}+ `);+ const ids = await listBotRecallSessionIds('bot-main', 4);+ expect(ids).toEqual(['bot-main', 'bot-main-old', 'task-5', 'task-4']);+ // 名额够时全部带上;非伙伴 / 未知调用方 / 零名额都为空。+ expect(await listBotRecallSessionIds('bot-main', 50)).toEqual([+ 'bot-main', 'bot-main-old', 'task-5', 'task-4', 'task-3', 'task-2', 'task-1', 'task-0', 'child-1',+ ]);+ expect(await listBotRecallSessionIds('owner-task', 4)).toEqual([]);+ expect(await listBotRecallSessionIds('missing', 4)).toEqual([]);+ expect(await listBotRecallSessionIds('bot-main', 0)).toEqual([]);+ });+});diff --git a/apps/desktop/src/main/localDb/botHistoryScope.ts b/apps/desktop/src/main/localDb/botHistoryScope.tsindex c10af9632d..7ebad366ae 100644--- a/apps/desktop/src/main/localDb/botHistoryScope.ts+++ b/apps/desktop/src/main/localDb/botHistoryScope.ts@@ -63,6 +63,35 @@ export async function resolveBotHistorySessionIds(return [...new Set(rows.map((row) => row.sessionId))];}+/**+ * 换窗交接回忆指引用的伙伴自有会话清单(有界,最多 `limit` 条)。按归属分层而不是按时间混排:+ * 伙伴自己的对话会话(当前主对话 + 恢复前的 history Session,最新在前)整体优先,剩余名额再+ * 给最新的后台任务——后台任务再多,结果也早已按回执进过主对话;而旧对话一旦被挤出清单,+ * 用户追问恢复前的事就只能得到"没有记录"。非伙伴会话或归属损坏返回 []。+ */+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],+ );+ const ids = new Set(chats.map((row) => row.sessionId));+ const remaining = limit - ids.size;+ if (remaining > 0) {+ const tasks = await db.query<{ sessionId: string }>(+ `SELECT child_session_id AS sessionId FROM bot_delegations+ WHERE requesting_bot_id = ? AND child_session_id IS NOT NULL+ ORDER BY created_at DESC LIMIT ?`,+ [scope.botId, remaining],+ );+ for (const row of tasks) ids.add(row.sessionId);+ }+ return [...ids];+}+/*** 这个伙伴的队友们 —— 除它自己之外、还启用着的伙伴。*diff --git a/apps/desktop/src/main/maker-ipc/agentHandoff.ts b/apps/desktop/src/main/maker-ipc/agentHandoff.tsindex a3ad4490fc..7e52dc2c0c 100644--- a/apps/desktop/src/main/maker-ipc/agentHandoff.ts+++ b/apps/desktop/src/main/maker-ipc/agentHandoff.ts@@ -87,10 +87,11 @@ export interface BuildHandoffOptions {*/companion?: boolean;/**- * 伙伴自己的会话 id(宿主按 resolveBotHistorySessionIds 解析:当前对话、恢复前的旧- * 对话、自己开的后台任务;最新在前)。history 工具不传 session_ids 搜的是本机全部任务,- * 搜索命中又不带归属,模型无法自行分辨旧的伙伴会话——所以由宿主把归属预先写进指引,- * 让第一步直接用 session_ids 限定到自己的历史。缺省只认当前对话。+ * 伙伴自己的会话 id(宿主按 botHistoryScope.listBotRecallSessionIds 解析:当前对话与+ * 恢复前的旧对话整体优先,剩余名额给最新的后台任务;总数不超过 COMPANION_SESSION_IDS_LIMIT)。+ * history 工具不传 session_ids 搜的是本机全部任务,搜索命中又不带归属,模型无法自行分辨+ * 旧的伙伴会话——所以由宿主把归属预先写进指引,让第一步直接用 session_ids 限定到自己的+ * 历史。缺省只认当前对话。*/companionSessionIds?: readonly string[];/**@@ -650,8 +651,11 @@ function splitTrailingTerminator(text: string): [body: string, tail: string] {const RETRIEVAL_SECTION_HEADING = '== Retrieving earlier verbatim history';/** 伙伴主对话的检索指引标题(按需回忆口径),同样属于必须整段保住的尾部。 */const RECALL_SECTION_HEADING = '== Recalling earlier history';-/** 回忆指引里列出的伙伴自有会话 id 上限(最新在前);再多就靠搜索命中的 sessionId 补查。 */-const COMPANION_SESSION_IDS_LIMIT = 20;+/**+ * 回忆指引里列出的伙伴自有会话 id 上限。宿主(listBotRecallSessionIds)按同一上限分层取数:+ * 对话会话整体优先、后台任务补足,旧对话不会被后台任务挤掉;这里的截断只是防御性兜底。+ */+export const COMPANION_SESSION_IDS_LIMIT = 20;function splitMandatoryHandoffTail(text: string, opts: BuildHandoffOptions): {body: string;@@ -846,7 +850,7 @@ function assembleHandoffText(`== Recalling earlier history (use when needed) ==\n` +`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, your earlier chats before a restart, and the background tasks you started; newest first) — 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` ++ `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` +`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.`,diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.tsindex f687172511..6b0afee4c2 100644--- a/apps/desktop/src/main/maker-ipc/register.ts+++ b/apps/desktop/src/main/maker-ipc/register.ts@@ -602,7 +602,7 @@ import {pruneWorkingMemoryForSession,readWorkingMemoryNoteForSession,} from './botWorkingMemory.js';-import { resolveBotHistorySessionIds } from '../localDb/botHistoryScope.js';+import { listBotRecallSessionIds } from '../localDb/botHistoryScope.js';import { createWireNoteMemory } from './wireNoteMemory.js';import { isBotCanonicalReplacementBusy } from './botCanonicalReplacementGuard.js';import { configureBotCanonicalReplacementCoordinator } from './botCanonicalReplacementCoordinator.js';@@ -997,6 +997,7 @@ import {} from './sessionAgentSwitchHandler.js';import { pendingHarnessRuntimeMutation, setSessionRuntimeHarness } from './sessionRuntimeHarnessSelection.js';import {+ COMPANION_SESSION_IDS_LIMIT,extractAgentIslandPromptText,extractPlainText,prependNoteToWireUserMessage,@@ -16441,8 +16442,8 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)},listMessages: (sessionId) => listMessagesForAgentHandoff(sessionId, 400),readWorkingMemoryNote,- // 伙伴自有会话(当前 + 恢复前旧对话 + 自己开的后台任务),写进回忆指引限定 history 工具范围。- listCompanionSessionIds: async (sessionId) => (await resolveBotHistorySessionIds(sessionId, undefined)) ?? [],+ // 伙伴自有会话(当前 + 恢复前旧对话整体优先,后台任务补足),写进回忆指引限定 history 工具范围。+ listCompanionSessionIds: (sessionId) => listBotRecallSessionIds(sessionId, COMPANION_SESSION_IDS_LIMIT),findLatestUser: findLatestUserMessageForRebuild,findLatestRebuildMeta: findLatestContextRebuildMeta,getLiveSession: (sessionId) => maker.getSession(sessionId),