Files
Agent-Workbench/tests/project-memory.test.mjs
T
2026-10-30 17:54:11 +08:00

93 lines
7.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, launchWorkbench } from './helpers.mjs';
import { ProjectMemoryManager } from '../src/core/memory/project-memory.mjs';
test('project memories persist across restart, keep source provenance, and are visible across tasks', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store);
const created = await manager.create(f.session, { title: '接口约束', content: '本地服务只接受带工作台令牌的请求。', tags: ['架构', '安全'], source: { kind: 'file', path: 'README.md' } });
assert.equal(created.status, 'current'); assert.equal(created.projectId, f.project.id); assert.equal(created.source.kind, 'file');
assert.equal(created.source.path, 'README.md'); assert.match(created.source.sha256, /^[a-f0-9]{64}$/);
const before = await manager.list(f.project.id); assert.equal(before.summary.current, 1); assert.equal(before.memories[0].content, created.content);
f.store.close(); const reopened = new (f.store.constructor)(f.data); f.onCleanup(() => reopened.close());
const restored = await new ProjectMemoryManager(reopened).list(f.project.id);
assert.equal(restored.summary.current, 1); assert.equal(restored.memories[0].id, created.id);
const second = reopened.createSession(f.project.id, '另一个任务'); f.onCleanup(() => reopened.close());
const crossTask = await new ProjectMemoryManager(reopened).list(second.projectId);
assert.equal(crossTask.memories.some(value => value.id === created.id), true);
});
test('source changes expire current project memories and keep the original evidence', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store);
const created = await manager.create(f.session, { title: '读取范围', content: 'README 是当前项目的说明入口。' });
const cursor = f.store.eventCursor;
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nChanged outside the workbench.\n');
const after = await manager.list(f.project.id);
const expired = after.memories.find(value => value.id === created.id);
assert.equal(expired?.status, 'expired'); assert.match(expired?.expiredReason ?? '', /源码指纹/);
assert.equal(expired?.sourceFingerprint, created.sourceFingerprint);
assert.ok(f.store.eventCursor > cursor);
assert.ok(f.store.eventsAfter(cursor).events.some(event => event.type === 'memory.expired' && event.payload?.recordId === created.id));
assert.equal((await manager.list(f.project.id, { includeExpired: false })).memories.length, 0);
});
test('incomplete source scans mark current memories unverified without expiring or reusing them', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store);
const created = await manager.create(f.session, { title: '项目事实', content: '这条记忆绑定完整的工程快照。' });
const oversized = path.join(f.projectRoot, 'oversized-source.mjs'); fs.writeFileSync(oversized, Buffer.alloc(1024 * 1024 + 1));
const partial = await manager.list(f.project.id);
assert.equal(partial.scope.complete, false); assert.equal(partial.summary.current, 0); assert.equal(partial.summary.unverified, 1);
assert.equal(partial.memories.find(value => value.id === created.id)?.status, 'unverified');
assert.equal(f.store.core.memory.get(created.id)?.status, 'current');
assert.equal((await manager.list(f.project.id, { includeExpired: false })).memories.length, 0);
await assert.rejects(manager.create(f.session, { content: '不能绑定不完整的工程快照。' }), /扫描不完整/);
fs.rmSync(oversized);
const restored = await manager.list(f.project.id);
assert.equal(restored.scope.complete, true); assert.equal(restored.memories.find(value => value.id === created.id)?.status, 'current');
});
test('explicit file provenance is rechecked even when the inventory excludes that path', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store);
const filename = path.join(f.projectRoot, 'node_modules', 'owned-fixture', 'entry.mjs');
fs.mkdirSync(path.dirname(filename), { recursive: true }); fs.writeFileSync(filename, 'export const value = 1;\n');
const created = await manager.create(f.session, { title: '依赖入口', content: '这条记忆明确引用了文件内容。', source: { kind: 'file', path: 'node_modules/owned-fixture/entry.mjs' } });
fs.writeFileSync(filename, 'export const value = 2;\n');
const after = await manager.list(f.project.id);
const expired = after.memories.find(value => value.id === created.id);
assert.equal(after.scope.complete, true); assert.equal(expired?.status, 'expired'); assert.match(expired?.expiredReason ?? '', /来源文件已变化/);
});
test('execution snapshots do not expose project memories before their source scope is validated', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store);
const created = await manager.create(f.session, { title: '仅在项目页可见', content: '未验证前不能进入模型运行上下文。' });
assert.equal(Object.hasOwn(f.store.executionSnapshot(f.session), 'memories'), false);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nExternal source change.\n');
assert.equal(Object.hasOwn(f.store.executionSnapshot(f.session), 'memories'), false);
const view = await manager.list(f.project.id);
assert.equal(view.memories.find(value => value.id === created.id)?.status, 'expired');
});
test('project memory records stay isolated and archive requires the owning session', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store);
const otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); fs.writeFileSync(path.join(otherRoot, 'README.md'), '# Other\n');
const other = f.store.addProject(otherRoot), otherSession = f.store.createSession(other.id);
const created = await manager.create(f.session, { title: '仅当前项目', content: '只属于第一个项目。' });
assert.equal((await manager.list(other.id)).memories.length, 0);
assert.throws(() => manager.archive(otherSession, created.id), /项目记忆不存在/);
const archived = manager.archive(f.session, created.id); assert.equal(archived.status, 'archived');
assert.equal((await manager.list(f.project.id)).memories[0].status, 'archived');
});
test('project memory HTTP API exposes read-only source status and explicit writes', async t => {
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data); f.onCleanup(host.close);
const project = await host.api('/projects', { root: f.projectRoot });
const session = await host.api('/sessions', { projectId: project.id });
const created = await host.api('/sessions/' + session.id + '/memories', { title: 'HTTP memory', content: 'Stored through the owned API.', source: { kind: 'project' } });
assert.equal(created.status, 'current'); assert.equal(created.sourceFingerprint.length, 64); assert.equal(JSON.stringify(created).includes('apiKey'), false);
const listed = await host.api('/projects/' + project.id + '/memories'); assert.equal(listed.summary.current, 1); assert.equal(listed.memories[0].id, created.id);
const archived = await host.api('/sessions/' + session.id + '/memories/' + created.id + '/archive', {}); assert.equal(archived.status, 'archived');
await host.close();
});