Files
Agent-Workbench/tests/experience-context.test.mjs

148 lines
12 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, compatibleFixture, completion, launchWorkbench } from './helpers.mjs';
import { beginPlanTask } from './fixtures/task-plan-fixture.mjs';
import { ProjectMemoryManager } from '../src/core/memory/project-memory.mjs';
import { DeliveryReporter } from '../src/core/delivery-report.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
const documents = body => body.messages.filter(value => ['system', 'user'].includes(value.role) && value.content.includes('"project_memory":'))
.map(value => JSON.parse(value.content.slice(value.content.lastIndexOf('\n') + 1)).project_memory);
async function setup(t) {
const f = fixture(t, { modernDefaults: true }), provider = new Provider(); provider.setKey('owned-experience-key');
const model = await compatibleFixture(t, () => completion([], '本机固定HTTP结果,不修改工程。'));
f.store.saveConfig({ mode: 'compatible', model: 'owned-experience-model', baseUrl: model.baseUrl, approvalMode: 'full' });
const run = beginPlanTask(f, '只核对README,无需测试。');
run.usage.unknownRequests = 1; run.usage.estimatedUnknownTokens = 19; run.stopReason = 'network_error'; f.store.save(f.session);
const report = await new DeliveryReporter(f.store).create(f.session);
const manager = new ProjectMemoryManager(f.store, { redact: value => provider.redact(value) });
const sources = manager.experiences.sources(f.session), source = sources.sources.find(value => value.id === report.artifactId);
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
return { ...f, provider, model, manager, sources, source, runtime,
change: (memory, action, fields = {}) => manager.experiences.change(f.session, memory.id, action, { cursor: sources.cursor, revision: memory.experience.revision, ...fields }),
async send(session) {
runtime.start(session.id, '说明当前工程可核对的经验;无需测试,不修改文件。'); await runtime.active.get(session.id).promise;
assert.equal(session.status, 'completed', session.lastFailure?.message); return model.requests.at(-1).input;
} };
}
test('actual HTTP distinguishes draft, redacted adoption, withdrawal and immutable past send snapshots without limiting model tokens', async t => {
const f = await setup(t), consumer = f.store.createSession(f.project.id, 'Experience consumer');
const { memory } = await f.manager.experiences.create(f.session, { cursor: f.sources.cursor, source: f.source });
assert.equal(documents(await f.send(consumer)).length, 0);
const edited = await f.change(memory, 'edit', { title: '原网络观测', content: '仅记录失败,原因未知。owned-experience-key', applicability: '本工程原来源', invalidWhen: '源码或证据变化' });
const active = await f.change(edited, 'adopt');
const body = await f.send(consumer), [sent] = documents(body);
assert.ok(sent, JSON.stringify({ memoryView: await f.manager.list(f.project.id), systemMessages: body.messages.filter(value => value.role === 'system').map(value => value.content.slice(0, 180)) }, null, 2));
assert.equal(sent.entries[0].experience.adoption, 'active'); assert.match(sent.entries[0].content, /已隐藏/);
assert.equal(JSON.stringify(body).includes('owned-experience-key'), false);
assert.equal(sent.entries[0].experience.provenance.facts.runs[0].usage.unknownRequests, 1);
assert.equal(sent.entries[0].experience.engineering.verified, false);
assert.equal(body.max_tokens, undefined); assert.equal(body.max_completion_tokens, undefined);
const plan = [...f.store.core.context_plan.values()].filter(value => value.sessionId === consumer.id).at(-1);
const snapshot = f.store.artifactStore.read(f.store.core.artifact.get(plan.projectMemoryArtifactId));
assert.doesNotThrow(() => verifyPlanEvidence(f.store, consumer, plan));
await f.change(active, 'withdraw'); assert.equal(documents(await f.send(consumer)).length, 0);
assert.equal(f.store.artifactStore.read(f.store.core.artifact.get(plan.projectMemoryArtifactId)), snapshot);
assert.doesNotThrow(() => verifyPlanEvidence(f.store, consumer, plan));
assert.equal(f.model.requests.length, 3); assert.equal(f.store.config.approvalMode, 'full');
});
test('unavailable experience and a failed expiration save omit only optional content while actual HTTP engineering continues', async t => {
const f = await setup(t), consumer = f.store.createSession(f.project.id, 'Repair consumer');
const created = (await f.manager.experiences.create(f.session, { cursor: f.sources.cursor, source: f.source })).memory;
await f.change(created, 'adopt');
const artifact = f.store.core.artifact.get(f.source.id); fs.unlinkSync(path.join(f.store.artifactStore.root, artifact.relative));
const save = f.store.storage.save.bind(f.store.storage);
f.store.storage.save = (session, records, type, ...rest) => {
if (type === 'memory.experience.expired') throw Error('Owned optional save failure');
return save(session, records, type, ...rest);
};
assert.equal(documents(await f.send(consumer)).length, 0); assert.equal(f.model.requests.length, 1);
assert.equal(f.store.core.memory.get(created.id).status, 'current');
});
test('new local API persists explicit source selection, duplicate identity and revision guards, preserves raw bytes and does not request a model', async t => {
const f = await setup(t), foreign = f.store.createSession(f.project.id, 'Foreign session');
await f.runtime.stopAll(); f.store.close();
const host = await launchWorkbench(t, f.data), path = '/sessions/' + f.session.id + '/memories/experiences';
const sources = await host.api(path), source = sources.sources.find(value => value.id === f.source.id);
const [first, duplicate] = await Promise.all([host.api(path, { source, cursor: sources.cursor }), host.api(path, { source, cursor: sources.cursor })]);
assert.equal(first.memory.id, duplicate.memory.id); assert.equal(Number(first.duplicate) + Number(duplicate.duplicate), 1);
const memory = first.memory;
const active = await host.api(path + '/' + memory.id + '/adopt', { revision: memory.experience.revision, cursor: sources.cursor });
assert.equal(active.experience.adoption, 'active');
await assert.rejects(host.api(path + '/' + memory.id + '/withdraw', { revision: 1, cursor: sources.cursor }), /已更新/);
await assert.rejects(host.api('/sessions/' + foreign.id + '/memories/experiences/' + memory.id + '/adopt', { revision: 2, cursor: { taskId: null, runId: null } }), /来源会话/);
const raw = await host.api('/artifacts/' + source.id + '?session=' + f.session.id);
assert.equal(raw.sha256, source.sha256); assert.equal(JSON.parse(raw.content).runId, source.runId);
const withdrawn = await host.api(path + '/' + memory.id + '/withdraw', { revision: 2, cursor: sources.cursor });
assert.equal(withdrawn.experience.adoption, 'withdrawn');
assert.equal((await host.api('/projects/' + f.project.id + '/memories')).summary.current, 0);
assert.equal(f.model.requests.length, 0);
});
test('a task switch while asynchronous creation or adoption is inspecting cannot save a late mutation', async t => {
const f = await setup(t);
const pending = f.manager.experiences.create(f.session, { cursor: f.sources.cursor, source: f.source });
beginPlanTask(f, '新的用户任务,无需测试。');
await assert.rejects(pending, /已切换/); assert.equal(f.store.core.memory.size, 0);
const currentSources = f.manager.experiences.sources(f.session);
const created = (await f.manager.experiences.create(f.session, { cursor: currentSources.cursor, source: f.source })).memory;
const adoption = f.manager.experiences.change(f.session, created.id, 'adopt', { cursor: currentSources.cursor, revision: 1 });
beginPlanTask(f, '再一个用户任务,无需测试。');
await assert.rejects(adoption, /已切换/); assert.equal(f.store.core.memory.get(created.id).experience.adoption, 'draft');
});
test('a failed optional experience send snapshot is recorded and does not block the original actual HTTP request', async t => {
const f = await setup(t), consumer = f.store.createSession(f.project.id, 'Optional snapshot failure');
const created = (await f.manager.experiences.create(f.session, { cursor: f.sources.cursor, source: f.source })).memory;
await f.change(created, 'adopt');
const write = f.store.artifactStore.write.bind(f.store.artifactStore);
f.store.artifactStore.write = (session, run, value, options) => {
if (options?.kind === 'context-project-memory') throw Error('Owned experience snapshot unavailable');
return write(session, run, value, options);
};
assert.equal(documents(await f.send(consumer)).length, 0);
const plan = [...f.store.core.context_plan.values()].find(value => value.sessionId === consumer.id);
assert.equal(plan.projectMemoryIssue.kind, 'optional_experience_unavailable');
assert.equal(f.model.requests.length, 1); assert.doesNotThrow(() => verifyPlanEvidence(f.store, consumer, plan));
});
test('optional experience snapshot failure retains compatible manual memory in the actual HTTP request', async t => {
const f = await setup(t), consumer = f.store.createSession(f.project.id, 'Mixed memory snapshot');
const created = (await f.manager.experiences.create(f.session, { cursor: f.sources.cursor, source: f.source })).memory;
await f.change(created, 'adopt');
const manual = await f.manager.create(f.session, { content: '原有手写项目约定,不随经验故障丢失。' });
const write = f.store.artifactStore.write.bind(f.store.artifactStore);
f.store.artifactStore.write = (session, run, value, options) => {
if (options?.kind === 'context-project-memory' && (typeof value === 'string' ? JSON.parse(value) : value).entries.some(entry => entry.experience)) throw Error('Owned experience-specific snapshot failure');
return write(session, run, value, options);
};
const [sent] = documents(await f.send(consumer)); assert.deepEqual(sent.entries.map(value => value.id), [manual.id]);
const plan = [...f.store.core.context_plan.values()].find(value => value.sessionId === consumer.id);
assert.equal(plan.projectMemoryEntries, 1); assert.equal(plan.projectMemoryIssue.kind, 'optional_experience_unavailable');
assert.doesNotThrow(() => verifyPlanEvidence(f.store, consumer, plan));
});
test('malformed optional experience metadata remains unavailable while manual memory and actual HTTP continue', async t => {
const f = await setup(t), consumer = f.store.createSession(f.project.id, 'Malformed optional metadata');
const created = (await f.manager.experiences.create(f.session, { cursor: f.sources.cursor, source: f.source })).memory;
const active = await f.change(created, 'adopt');
const manual = await f.manager.create(f.session, { content: '手写记忆不受经验结构错误影响。' });
f.store.core.memory.get(active.id).experience = null;
const view = await f.manager.list(f.project.id);
assert.equal(view.memories.find(row => row.id === active.id).status, 'unverified');
assert.match(view.memories.find(row => row.id === active.id).experienceError, /结构不完整/);
const stored = f.store.core.memory.get(active.id); stored.status = 'archived'; stored.experience = {};
const archived = (await f.manager.list(f.project.id)).memories.find(row => row.id === active.id);
assert.equal(archived.status, 'archived'); assert.equal(archived.experience, null);
assert.match(archived.experienceError, /结构不完整/);
const [sent] = documents(await f.send(consumer)); assert.deepEqual(sent.entries.map(row => row.id), [manual.id]);
assert.equal(f.model.requests.length, 1);
});