Files
Agent-Workbench/tests/experience-drafts.test.mjs
T

179 lines
14 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 } from './helpers.mjs';
import { beginPlanTask, completeRead } from './fixtures/task-plan-fixture.mjs';
import { createDeliveryProject, applyDeliveryFix } from './fixtures/delivery-project.mjs';
import { ProjectMemoryManager } from '../src/core/memory/project-memory.mjs';
import { DeliveryReporter } from '../src/core/delivery-report.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { Store } from '../src/server/store.mjs';
const cursor = session => ({ taskId: session.taskId, runId: session.runId });
async function setup(t) {
const f = fixture(t, { modernDefaults: true }), manager = new ProjectMemoryManager(f.store);
const run = beginPlanTask(f, '解释原README,不修改,无需测试。');
run.usage = { requests: 1, inputTokens: 0, outputTokens: 0, totalTokens: 0, unknownRequests: 1, estimatedUnknownTokens: 121 };
run.stopReason = 'network_error'; f.store.save(f.session);
const report = await new DeliveryReporter(f.store).create(f.session);
const source = manager.experiences.sources(f.session).sources.find(value => value.id === report.artifactId);
return { ...f, manager, run, report, source,
create: () => manager.experiences.create(f.session, { cursor: cursor(f.session), source }),
change: (memory, action, fields = {}) => manager.experiences.change(f.session, memory.id, action,
{ cursor: cursor(f.session), revision: memory.experience.revision, ...fields }) };
}
test('experience drafts preserve original failures and unknown usage, deduplicate, persist and remain out of context even with full access', async t => {
const f = await setup(t), { memory } = await f.create();
assert.equal(f.store.config.approvalMode, 'ask'); f.store.saveConfig({ approvalMode: 'full' });
assert.equal(memory.origin, 'experience'); assert.equal(memory.experience.adoption, 'draft');
assert.deepEqual(memory.experience.stages, ['observed', 'source_checked']);
assert.equal(memory.experience.causalStatus, 'unproven'); assert.equal(memory.experience.engineering.verified, false);
assert.equal(memory.experience.provenance.facts.runs[0].usage.unknownRequests, 1);
assert.equal(memory.experience.provenance.facts.runs[0].stopReason, 'network_error');
assert.equal(memory.experience.provenance.scope.runId, f.run.id);
assert.equal(memory.experience.provenance.selection.sha256, f.source.sha256);
assert.equal(await f.manager.context(f.session), null);
const duplicate = await f.create(); assert.equal(duplicate.duplicate, true); assert.equal(duplicate.memory.id, memory.id);
assert.equal(f.store.core.memory.size, 1); assert.equal(f.store.core.artifact.get(memory.experience.artifactId).kind, 'experience-revision');
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
const manager = new ProjectMemoryManager(reopened), view = await manager.list(f.project.id);
assert.equal(view.summary.drafts, 1); assert.equal(view.summary.current, 0);
assert.equal(view.memories[0].experience.revisions.length, 1); assert.equal(await manager.context(reopened.session(f.session.id)), null);
});
test('user adoption is independent from engineering verification; edits withdraw the active text and preserve every revision', async t => {
const f = await setup(t), { memory } = await f.create();
const active = await f.change(memory, 'adopt'); assert.equal(active.experience.adoption, 'active');
assert.ok(active.experience.stages.includes('user_confirmed')); assert.ok(!active.experience.stages.includes('engineering_verified'));
const sent = await f.manager.context(f.session); assert.equal(sent.entries[0].experience.revision, 2);
const edited = await f.change(active, 'edit', { title: '网络观测', content: '网络失败的原因尚未确定。', applicability: '本项目所选报告', invalidWhen: '新网络或源码证据出现' });
assert.equal(edited.experience.adoption, 'draft'); assert.equal(await f.manager.context(f.session), null);
assert.equal(edited.experience.revisions.length, 3); f.manager.experiences.verifySnapshot(edited);
const original = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(memory.experience.artifactId)));
assert.equal(original.content, memory.content); assert.equal(original.experience.adoption, 'draft');
const readopted = await f.change(edited, 'adopt'), withdrawn = await f.change(readopted, 'withdraw');
assert.equal(withdrawn.experience.adoption, 'withdrawn'); assert.equal(await f.manager.context(f.session), null);
assert.equal(sent.entries[0].experience.adoption, 'active', 'past snapshots remain facts of their send time');
await assert.rejects(f.change(edited, 'adopt'), /已更新/);
});
test('counterexamples expire adoption, preserve the original failure and cannot claim engineering verification', async t => {
const f = await setup(t), active = await f.change((await f.create()).memory, 'adopt');
const expired = await f.change(active, 'counterexample', { note: '后续同条件仍失败,旧归因不成立。' });
assert.equal(expired.status, 'expired'); assert.equal(expired.experience.adoption, 'withdrawn');
assert.equal(expired.experience.counterexamples[0].engineeringVerified, false);
assert.equal(expired.experience.provenance.facts.runs[0].stopReason, 'network_error');
assert.equal(await f.manager.context(f.session), null); await assert.rejects(f.change(expired, 'adopt'), /已失效/);
});
test('foreign identities, stale source SHA, late cursor and stale revisions cannot change another experience', async t => {
const f = await setup(t), { memory } = await f.create();
const other = f.store.createSession(f.project.id, 'Another task');
await assert.rejects(f.manager.experiences.create(other, { source: f.source, cursor: cursor(other) }), /不属于/);
await assert.rejects(f.manager.experiences.change(other, memory.id, 'adopt', { cursor: cursor(other), revision: 1 }), /来源会话/);
await assert.rejects(f.manager.experiences.create(f.session, { cursor: cursor(f.session), source: { ...f.source, sha256: '0'.repeat(64) } }), /来源已变化/);
await assert.rejects(f.manager.experiences.change(f.session, memory.id, 'adopt', { cursor: { taskId: null, runId: null }, revision: 1 }), /已切换/);
const root = path.join(f.root, 'foreign'); fs.mkdirSync(root); fs.writeFileSync(path.join(root, 'README.md'), '# Other');
const foreign = f.store.createSession(f.store.addProject(root).id);
await assert.rejects(f.manager.experiences.create(foreign, { cursor: cursor(foreign), source: f.source }), /不属于/);
assert.equal(f.store.core.memory.get(memory.id).experience.revision, 1);
});
test('source changes, missing original bytes and changed source ownership exclude adopted experience', async t => {
for (const scenario of ['source', 'bytes', 'ownership', 'missing']) await t.test(scenario, async t => {
const f = await setup(t), active = await f.change((await f.create()).memory, 'adopt');
const artifact = f.store.core.artifact.get(f.source.id);
if (scenario === 'source') fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nchanged');
if (scenario === 'bytes') fs.writeFileSync(path.join(f.store.artifactStore.root, artifact.relative), 'tampered');
if (scenario === 'ownership') artifact.sessionId = f.store.createSession(f.project.id).id;
if (scenario === 'missing') fs.unlinkSync(path.join(f.store.artifactStore.root, artifact.relative));
assert.equal(await f.manager.context(f.session), null);
const view = await f.manager.list(f.project.id), expired = view.memories.find(value => value.id === active.id);
assert.equal(expired.status, 'expired'); assert.equal(expired.experience.provenance.selection.sha256, f.source.sha256);
assert.equal(expired.experience.revisions[0].artifactId, active.experience.revisions[0].artifactId);
});
});
test('a changed immutable artifact Run is excluded for the request while the storage ownership guard restores its persisted identity', async t => {
const f = await setup(t), active = await f.change((await f.create()).memory, 'adopt');
const originalRunId = active.runId, artifact = f.store.core.artifact.get(f.source.id);
const other = f.store.beginRun(f.session, '同任务另一运行', true, f.store.config);
f.session.status = 'paused'; f.store.finishRun(f.session, other, 'owned_scope_fixture');
artifact.runId = other.id;
const view = await f.manager.list(f.project.id);
assert.equal(view.memories[0].status, 'unverified', 'optional experience is unavailable on the failed persistence attempt');
assert.equal(f.store.core.artifact.get(f.source.id).runId, originalRunId, 'the existing storage guard restores the immutable row');
const restored = await f.manager.list(f.project.id); assert.equal(restored.memories[0].status, 'current');
assert.equal(restored.memories[0].experience.revision, active.experience.revision);
});
test('changed current or historical revision bytes and materialized text cannot enter requests', async t => {
for (const scenario of ['current', 'history', 'text']) await t.test(scenario, async t => {
const f = await setup(t), active = await f.change((await f.create()).memory, 'adopt');
if (scenario === 'text') f.store.core.memory.get(active.id).content = 'Forged lesson';
else {
const id = scenario === 'current' ? active.experience.artifactId : active.experience.revisions[0].artifactId;
const artifact = f.store.core.artifact.get(id); fs.writeFileSync(path.join(f.store.artifactStore.root, artifact.relative), '{}');
}
assert.equal(await f.manager.context(f.session), null);
assert.notEqual((await f.manager.list(f.project.id)).memories[0].status, 'current');
});
});
test('experience persistence failure excludes optional content while manual memory compatibility remains intact', async t => {
const f = await setup(t), active = await f.change((await f.create()).memory, 'adopt');
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nchanged');
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 experience persistence failure');
return save(session, records, type, ...rest);
};
assert.equal(await f.manager.context(f.session), null);
assert.equal(f.store.core.memory.get(active.id).status, 'current', 'failed saves cannot silently rewrite stored history');
f.store.storage.save = save;
const manual = await f.manager.create(f.session, { title: '手动约定', content: '保留当前项目约定。' });
const context = await f.manager.context(f.session); assert.deepEqual(context.entries.map(value => value.id), [manual.id]);
assert.equal(manual.origin, 'user'); assert.equal(manual.schemaVersion, 1);
});
test('rejected and archived experience is not silently reactivated by duplicate generation', async t => {
const f = await setup(t), rejected = await f.change((await f.create()).memory, 'reject');
assert.ok(rejected.experience.stages.includes('rejected')); assert.equal((await f.create()).memory.id, rejected.id);
await assert.rejects(f.change(rejected, 'adopt'), /拒绝/);
const archived = await f.change(rejected, 'archive'); assert.equal(archived.status, 'archived');
assert.equal((await f.create()).memory.experience.adoption, 'withdrawn'); assert.equal(await f.manager.context(f.session), null);
});
test('tools without source fingerprints remain historical drafts and do not manufacture checked engineering outcomes', async t => {
const f = await setup(t), tool = completeRead(f);
const source = f.manager.experiences.sources(f.session).sources.find(value => value.kind === 'tool' && value.id === tool.id);
const { memory } = await f.manager.experiences.create(f.session, { cursor: cursor(f.session), source });
assert.equal(memory.experience.provenance.facts.tool, 'read_file'); assert.equal(memory.status, 'expired');
assert.equal(memory.experience.engineering.verified, false); assert.equal(await f.manager.context(f.session), null);
});
test('actual original failed and passing check jobs remain distinct; engineering verification never certifies edited conclusions', async t => {
const f = fixture(t), manager = new ProjectMemoryManager(f.store), checks = new CheckRunner(f.store);
f.onCleanup(() => checks.stopAll()); createDeliveryProject(f);
const failed = await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
assert.equal(failed.status, 'failed');
const source = manager.experiences.sources(f.session).sources.find(value => value.id === failed.id);
const original = (await manager.experiences.create(f.session, { source, cursor: cursor(f.session) })).memory;
assert.equal(original.experience.provenance.facts.check.results[0].tests.failed, 3);
assert.equal(original.experience.engineering.verified, false);
applyDeliveryFix(f);
const passed = await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
assert.equal(passed.status, 'passed');
const passedSource = manager.experiences.sources(f.session).sources.find(value => value.id === passed.id);
const fresh = (await manager.experiences.create(f.session, { source: passedSource, cursor: cursor(f.session) })).memory;
assert.equal(fresh.experience.engineering.verified, true); assert.equal(fresh.experience.engineering.conclusionVerified, false);
assert.ok(fresh.experience.stages.includes('engineering_verified'));
const report = await new DeliveryReporter(f.store).create(f.session);
const reportSource = manager.experiences.sources(f.session).sources.find(value => value.id === report.artifactId);
const reportDraft = (await manager.experiences.create(f.session, { source: reportSource, cursor: cursor(f.session) })).memory;
assert.deepEqual(reportDraft.experience.provenance.facts.checks.map(value => value.status), ['failed', 'passed']);
assert.equal(f.store.core.memory.get(original.id).content, original.content);
});