Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ enso config get providers.<id> # apiKey、Authorization 等显示为

`memoryReconcileEnabled` 只控制写入新记忆后是否自动复核,写入后立即生效;设置页的「立即复核」不受它限制。它是普通布尔开关,不是密钥、路径或执行入口,也不在启动时一次性读取,所以 `policy.ts` 不需要为它加规则。

`memoryDispatchRecallEnabled`(缺省开)控制秘书派发会话时是否按任务内容检索本机记忆库并把相关经验注入首条 prompt。写入后立即生效(在派发时读取);注入失败或超时静默降级,不阻塞派发。它只作用于本机记忆库,不参与配置同步;同样是普通布尔开关,`policy.ts` 不需要为它加规则。
`memoryDispatchRecallEnabled`(缺省关)控制秘书派发会话时是否按任务内容检索本机记忆库并把相关经验注入首条 prompt。写入后立即生效(在派发时读取);注入失败或超时静默降级,不阻塞派发。它只作用于本机记忆库,不参与配置同步;同样是普通布尔开关,`policy.ts` 不需要为它加规则。

`memoryDeepDreamEnabled`(布尔,缺省 `false`)控制自动睡眠整理是否启用多轮工具调用与只读代码核对的深度 Agentic Dream 模式(开启后按独立的 24 小时 / 10 个待整理会话节律触发;手动触发不受此开关限制)。只接受布尔值,热生效,无需重启,不参与配置同步。

Expand Down
2 changes: 2 additions & 0 deletions src/agent/microcompact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const READ_TOOLS = new Set([
'task_output',
'memory_search',
'session_search',
'transcript_list',
'transcript_read',
]);
const WRITE_TOOLS = new Set(['bash', 'edit', 'write', 'apply_patch', 'codemode']);

Expand Down
2 changes: 2 additions & 0 deletions src/agent/supervisor.agentDispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,8 @@ describe('SessionSupervisor deterministic child lifecycle', () => {
'memory_search',
'memory_write',
'session_search',
'transcript_list',
'transcript_read',
'image_view',
'computer',
];
Expand Down
41 changes: 38 additions & 3 deletions src/agent/tools/memory.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SECRETARY_TOOL_ALLOWLIST } from '@shared/secretary/tools';
import { describe, expect, it, vi } from 'vitest';
import { ActionTimeoutError, createMemoryTools, MemoryInvoker } from './memory';

Expand Down Expand Up @@ -25,14 +26,16 @@ describe('Memory v2 agent tools', () => {
}
});

it('暴露 4 个极简工具(memory_read / memory_search / memory_write / session_search)', () => {
it('暴露 6 个工具(memory_read / memory_search / memory_write / session_search / transcript_list / transcript_read)', () => {
const invoker = new MemoryInvoker(IDENTITY, () => {});
const tools = createMemoryTools(invoker);
expect(tools.map((t) => t.name)).toEqual([
'memory_read',
'memory_search',
'memory_write',
'session_search',
'transcript_list',
'transcript_read',
]);
const byName = Object.fromEntries(tools.map((t) => [t.name, t]));
expect(byName.memory_search.description).toContain('no note body');
Expand Down Expand Up @@ -86,7 +89,21 @@ describe('Memory v2 agent tools', () => {
);
await byName.session_search.execute(
'5',
{ q: 'Apple ID 公证' } as never,
{ q: 'Apple ID 公证', after: '2026-01-01', session_id: 'sess-1' } as never,
undefined,
undefined,
CTX
);
await byName.transcript_list.execute(
'5b',
{ limit: 10, after: '2026-01-01' } as never,
undefined,
undefined,
CTX
);
await byName.transcript_read.execute(
'5c',
{ session_id: 'sess-1', entry_id: 'e-42', window: 2 } as never,
undefined,
undefined,
CTX
Expand All @@ -106,7 +123,12 @@ describe('Memory v2 agent tools', () => {
},
},
{ op: 'write', params: { action: 'delete', path: 'decisions/old.md' } },
{ op: 'session_search', params: { query: 'Apple ID 公证' } },
{
op: 'session_search',
params: { query: 'Apple ID 公证', after: '2026-01-01', sessionId: 'sess-1' },
},
{ op: 'transcript_list', params: { limit: 10, after: '2026-01-01' } },
{ op: 'transcript_read', params: { sessionId: 'sess-1', entryId: 'e-42', window: 2 } },
]);

await expect(
Expand All @@ -133,6 +155,19 @@ describe('Memory v2 agent tools', () => {
expect(searchProps).toHaveProperty('projectId');
});

it('secretary 模式只产出秘书白名单内的记忆工具(transcript_* 由 session_list / session_read 承担)', () => {
const tools = createMemoryTools(new MemoryInvoker(IDENTITY, vi.fn()), { secretary: true });
expect(tools.map((t) => t.name)).toEqual([
'memory_read',
'memory_search',
'memory_write',
'session_search',
]);
for (const tool of tools) expect(SECRETARY_TOOL_ALLOWLIST.has(tool.name), tool.name).toBe(true);
const sessionSearch = tools.find((t) => t.name === 'session_search');
expect(sessionSearch?.description).not.toContain('transcript_read');
});

it('prepareArguments 严格满足各工具 JSON Schema(additionalProperties: false、枚举与类型校验)', () => {
const invoker = new MemoryInvoker(IDENTITY, vi.fn());
const regularTools = Object.fromEntries(createMemoryTools(invoker).map((t) => [t.name, t]));
Expand Down
139 changes: 137 additions & 2 deletions src/agent/tools/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ const normalizeSearchParams = (raw: unknown, secretary: boolean): Record<string,
};
};

const MEMORY_WRITE_SOURCES = ['observed', 'user_stated'] as const;

const normalizeWriteParams = (raw: unknown, secretary: boolean): Record<string, unknown> => {
const o = asRecord(raw);
const action = o.action === 'delete' ? 'delete' : 'upsert';
Expand All @@ -241,6 +243,7 @@ const normalizeWriteParams = (raw: unknown, secretary: boolean): Record<string,
const projectId = secretary ? trimStr(o.projectId) : undefined;
const keywords = asStringArray(o.keywords);
const aliases = asStringArray(o.aliases);
const source = inEnum(trimStr(o.source)?.toLowerCase(), MEMORY_WRITE_SOURCES);
return {
action,
...(path ? { path } : {}),
Expand All @@ -251,16 +254,51 @@ const normalizeWriteParams = (raw: unknown, secretary: boolean): Record<string,
...(projectId ? { projectId } : {}),
...(keywords ? { keywords } : {}),
...(aliases ? { aliases } : {}),
...(source ? { source } : {}),
};
};

const normalizeSessionSearchParams = (raw: unknown): Record<string, unknown> => {
const o = asRecord(raw);
const query = trimStr(o.query) ?? trimStr(o.q);
const limit = asClampedInt(o.limit, 1, 15);
const after = trimStr(o.after);
const before = trimStr(o.before);
const sessionId = trimStr(o.sessionId) ?? trimStr(o.session_id);
return {
...(query ? { query } : {}),
...(limit !== undefined ? { limit } : {}),
...(after ? { after } : {}),
...(before ? { before } : {}),
...(sessionId ? { sessionId } : {}),
};
};

const normalizeSessionListParams = (raw: unknown): Record<string, unknown> => {
const o = asRecord(raw);
const limit = asClampedInt(o.limit, 1, 50);
const after = trimStr(o.after);
const before = trimStr(o.before);
return {
...(limit !== undefined ? { limit } : {}),
...(after ? { after } : {}),
...(before ? { before } : {}),
};
};

const normalizeSessionReadParams = (raw: unknown): Record<string, unknown> => {
const o = asRecord(raw);
const sessionId = trimStr(o.sessionId) ?? trimStr(o.session_id);
const entryId = trimStr(o.entryId) ?? trimStr(o.entry_id);
const fromSeq = asClampedInt(o.fromSeq ?? o.from_seq, 0, 1_000_000);
const limit = asClampedInt(o.limit, 1, 20);
const window = asClampedInt(o.window, 0, 10);
return {
...(sessionId ? { sessionId } : {}),
...(entryId ? { entryId } : {}),
...(fromSeq !== undefined ? { fromSeq } : {}),
...(limit !== undefined ? { limit } : {}),
...(window !== undefined ? { window } : {}),
};
};

Expand Down Expand Up @@ -420,6 +458,12 @@ export function createMemoryTools(
items: { type: 'string' },
description: 'Optional synonym, bilingual, or abbreviation aliases for search recall',
},
source: {
type: 'string',
enum: [...MEMORY_WRITE_SOURCES],
description:
"Optional authority source: pass 'user_stated' ONLY when the user explicitly stated a preference/rule or asked to remember this; omit or pass 'observed' for agent-inferred findings (omitting preserves existing source on update and defaults to 'observed' on create)",
},
spaceId: {
type: 'string',
enum: [...MEMORY_CAPTURE_SPACES],
Expand All @@ -441,8 +485,11 @@ export function createMemoryTools(
define(
'session_search',
'Session search',
'Search raw past conversation transcripts (L2 evidence) when you need exact error outputs, ' +
'original wording, or details not captured in distilled memory files.',
'Search raw past conversation transcripts and session summaries (L2 evidence) when you need exact error outputs, ' +
'original wording, or details not captured in distilled memory files.' +
(secretary
? ''
: ' Use transcript_read with the returned sessionId/entryId to inspect surrounding turns.'),
schema(
{
query: { type: 'string', description: 'Specific keywords, error snippet, or identifier' },
Expand All @@ -452,6 +499,18 @@ export function createMemoryTools(
maximum: 15,
description: 'Max matching snippets (default 6)',
},
after: {
type: 'string',
description: 'Optional inclusive lower bound ISO date or timestamp (e.g. 2026-01-01)',
},
before: {
type: 'string',
description: 'Optional inclusive upper bound ISO date or timestamp (e.g. 2026-12-31)',
},
sessionId: {
type: 'string',
description: 'Optional sessionId to restrict search to a single past session',
},
},
['query']
),
Expand All @@ -461,5 +520,81 @@ export function createMemoryTools(
if (!p.query) throw new Error('query is required');
}
),
// 秘书白名单已有 session_list / session_read,不重复暴露 transcript_*
...(secretary
? []
: [
define(
'transcript_list',
'Transcript list',
'List recent past sessions in the current visible memory space with their timestamps and rollout summaries (L2 directory).',
schema(
{
limit: {
type: 'integer',
minimum: 1,
maximum: 50,
description: 'Max sessions to return (default 20)',
},
after: {
type: 'string',
description: 'Optional inclusive lower bound ISO date or timestamp',
},
before: {
type: 'string',
description: 'Optional inclusive upper bound ISO date or timestamp',
},
},
[]
),
'transcript_list',
normalizeSessionListParams,
() => {}
),

define(
'transcript_read',
'Transcript read',
'Read a bounded window of verbatim user/assistant turns from a past session transcript (L2). ' +
'Pass entryId (from session_search) to inspect turns around a match, or fromSeq + limit to page through a session (or derivedFrom sessionId from memory_read).',
schema(
{
sessionId: {
type: 'string',
description:
'Session ID from transcript_list, session_search, or memory_read derivedFrom',
},
entryId: {
type: 'string',
description: 'Optional entryId to center the read window around',
},
fromSeq: {
type: 'integer',
minimum: 0,
description:
'Optional 0-based turn index to start reading from (omit for latest turns)',
},
limit: {
type: 'integer',
minimum: 1,
maximum: 20,
description: 'Max turns when reading by fromSeq or tail (default 8)',
},
window: {
type: 'integer',
minimum: 0,
maximum: 10,
description: 'Turns before and after entryId to include (default 3)',
},
},
['sessionId']
),
'transcript_read',
normalizeSessionReadParams,
(p) => {
if (!p.sessionId) throw new Error('sessionId is required');
}
),
]),
];
}
2 changes: 2 additions & 0 deletions src/main/ipc/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ import { type Complete, redactSecrets } from '../services/memory/distill';
import { memorySpaceContext, secretaryProjectIds } from '../services/memory/space';
import {
configureMemoryDistill,
forgetMemorySession,
invokeMemory,
rootSessionId,
scheduleMemoryDistill,
Expand Down Expand Up @@ -810,6 +811,7 @@ function removeConversationFiles(conversation: {
const previousSessionFiles = conversation.previousSessionFiles ?? authority?.previousSessionFiles;
peekExtensionPlugins()?.forgetSession(conversation.conversationId);
peekEventClaimStore()?.releaseSession(conversation.conversationId);
forgetMemorySession(conversation.conversationId);
getSecretaryImageStore()?.remove(conversation.conversationId);
getSecretaryReportStore()?.remove(conversation.conversationId);
removeConversationSessionFiles({
Expand Down
3 changes: 3 additions & 0 deletions src/main/ipc/bots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ function createRuntime(botsRoot: string): BotRuntimePort {
conversationId: conversation.conversationId,
...(conversation.sessionFile ? { sessionFile: conversation.sessionFile } : {}),
});
void import('../services/memoryHost')
.then((m) => m.forgetMemorySession(conversation.conversationId))
.catch(() => {});
},
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/ipc/btw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ export function registerBtwHandlers(): void {
conversationId: parsed.sessionId,
...(file ? { sessionFile: file } : {}),
});
void import('../services/memoryHost')
.then((m) => m.forgetMemorySession(parsed.sessionId))
.catch(() => {});
return { ok: true };
});
}
3 changes: 3 additions & 0 deletions src/main/ipc/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ export function registerProjectHandlers(): void {
conversationId: conversation.conversationId,
...(conversation.sessionFile ? { sessionFile: conversation.sessionFile } : {}),
});
void import('../services/memoryHost')
.then((m) => m.forgetMemorySession(conversation.conversationId))
.catch(() => {});
// 项目删了,会话也不在了:手机关注里的这些一并移除
void unfollowTargetEverywhere(conversation.conversationId);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/services/memoryHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ export function notifyMemoryChanged(): void {
} catch {}
}

/** 删会话时同步清理 Memory v2 的 session_cursors 与搜索缓存(best-effort,不建空库) */
export function forgetMemorySession(sessionId: string): void {
try {
getMemoryV2Service().forgetSession(sessionId);
} catch {}
}

/** 子会话(coworker `${parentId}::cw-x` / enso child)归属父会话所在项目。 */
export function rootSessionId(identity: SessionIdentity | ChildSessionIdentity): string {
const parent = (identity as ChildSessionIdentity).parent?.sessionId;
Expand Down
Loading