Files
Agent-Workbench/tests/reminder-queue.test.mjs
2026-11-06 15:38:50 +08:00

134 lines
11 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { observeTurn } from '../src/core/context/turn-progress.mjs';
import { observeReminders, selectReminder, recordReminderDispatch, REMINDER_POLICY } from '../src/core/context/reminder-queue.mjs';
import { recordTurnProgress, verifyTurnProgressReceipt } from '../src/core/context/turn-progress-recorder.mjs';
import { fixture } from './helpers.mjs';
import { beginPlanTask, completeRead } from './fixtures/task-plan-fixture.mjs';
const hash = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
const scope = { projectId: 'p', sessionId: 's', taskId: 't', runId: 'r' };
const read = (start = 0, end = 4, name = 'read_file') => ({ id: 'read', name, argsSha256: hash(['read', start, end]),
success: true, read: { key: 'file', sha256: hash('original source'), unit: 'utf16_in_original_range', start, end } });
const failure = (category = 'invalid_input', signature = 'same error') => ({ id: 'failure', name: 'read_file', argsSha256: hash('same bad arguments'), failure: { category, signature } });
function scenario() {
let shadow, state, ordinal = 0, at = 1000;
return {
turn(tools = [], extras = {}, policy = {}) {
const result = observeTurn(shadow, { ...scope, tools, phase: 'settled', ...extras }); shadow = result.state;
const receipt = { observation: result.observation, tools, ref: { artifactId: 'original-' + (++ordinal), sha256: hash(result.observation), ordinal } };
state = observeReminders(state, { scope, enabled: true, at: ++at, receipt, ...policy });
return receipt;
},
get state() { return state; },
set state(value) { state = value; },
get at() { return at; },
select(extras = {}) { const selected = selectReminder(state, { scope, enabled: true, at, visibleTools: ['read_file'], ...extras }); state = selected.state; return selected.message; },
dispatch(message, requestId = 'request-1') { state = recordReminderDispatch(state, { scope, at, message, requestId, requestSha256: hash(requestId) }); },
};
}
function repeated(s, tool = read()) { s.turn([tool]); s.turn([tool]); s.turn([tool]); }
test('off by default, two confirmed repeated turns, stable dedup, no task limits or model reasoning', () => {
const s = scenario(); s.turn([read()]); s.turn([read()]); assert.equal(s.state.items.length, 0);
s.turn([read()]); assert.equal(s.state.items.length, 1); const key = s.state.items[0].key;
s.turn([read()], { modelText: '明白,已经解决', privateReasoning: 'PRIVATE_SENTINEL' });
assert.equal(s.state.items.length, 1); assert.equal(s.state.items[0].key, key); assert.equal(s.state.items[0].status, 'pending');
assert.ok(!JSON.stringify(s.state).includes('PRIVATE_SENTINEL'));
const off = observeReminders(null, { scope, at: 1000 }); assert.equal(off.enabled, false); assert.equal(off.items.length, 0);
assert.equal(selectReminder(off, { scope, at: 1001 }).message, null);
for (const key of ['maxTokens', 'maxSteps', 'shouldAbort', 'approvalMode', 'history']) assert.equal(key in s.state, false);
});
test('normal pagination, new SHA and legal research never ask for changes', () => {
const s = scenario(); s.turn([read(0, 4)]); s.turn([read(4, 8)]); s.turn([read(8, 12)]);
const changed = read(); changed.read.sha256 = hash('changed source'); s.turn([changed]); assert.equal(s.state.items.length, 0);
repeated(s, read(20, 24)); assert.equal(s.state.items.length, 1);
s.turn([read(20, 24)], {}, { research: true }); assert.equal(s.state.items[0].exitReason, 'research'); assert.equal(s.select(), null);
const research = scenario(); for (let i = 0; i < 8; i++) research.turn([read()], {}, { research: true });
assert.equal(research.state.items.length, 0);
});
test('new failures, network and environment repetition, user boundaries and waits stay in their actual classes', () => {
for (const category of ['permission', 'network', 'authentication', 'tool_or_environment', 'requires_verification']) {
const s = scenario(); for (let i = 0; i < 6; i++) s.turn([failure(category)]); assert.equal(s.state.items.length, 0, category);
}
for (const kind of ['approval', 'check_job', 'review']) {
const s = scenario(); repeated(s); s.turn([], { waiting: [{ kind, id: 'owned-job' }] });
assert.equal(s.state.items[0].status, 'expired'); assert.equal(s.state.items[0].exitReason, 'waiting'); assert.equal(s.select(), null);
}
const changed = scenario(); repeated(changed); changed.turn([], { userBoundaries: [['new-user', hash('changed goal')]] });
assert.equal(changed.state.items[0].exitReason, 'user_boundary');
const broken = scenario(); repeated(broken); broken.turn([failure('network')]); assert.equal(broken.state.items[0].exitReason, 'new_failure');
});
test('sent is separate from resolved, acknowledgement is ignored and real readback keeps causality unknown', () => {
const s = scenario(); repeated(s); const message = s.select(); assert.ok(message); assert.equal(s.state.items[0].deliveries.length, 0);
assert.ok(Buffer.byteLength(message.content) <= REMINDER_POLICY.messageBytes); s.dispatch(message);
assert.equal(s.state.items[0].status, 'delivered'); s.dispatch(message); assert.equal(s.state.items[0].deliveries.length, 1);
s.turn([], { modelText: '明白,我已采纳并完成所有工作。' }); assert.equal(s.state.items[0].status, 'delivered');
const original = structuredClone(s.state.items[0].source); const receipt = s.turn([read(4, 8)]);
assert.equal(s.state.items[0].status, 'resolved'); assert.equal(s.state.items[0].resolution.source.artifactId, receipt.ref.artifactId);
assert.equal(s.state.items[0].resolution.relation, 'observed_after_dispatch'); assert.equal(s.state.items[0].resolution.causality, 'unproven');
assert.deepEqual(s.state.items[0].source, original); assert.equal(s.select(), null);
});
test('pending advice can resolve naturally; invalid inputs need the same tool to actually succeed', () => {
const natural = scenario(); repeated(natural); natural.turn([read(4, 8)]);
assert.equal(natural.state.items[0].resolution.relation, 'naturally_resolved'); assert.equal(natural.state.items[0].deliveries.length, 0);
const s = scenario(); repeated(s, failure()); assert.equal(s.state.items[0].category, 'repeated_invalid_input');
s.dispatch(s.select()); s.turn([{ id: 'other', name: 'list_files', success: true }]); assert.equal(s.state.items[0].status, 'delivered');
s.turn([{ ...failure(), success: false }]); assert.equal(s.state.items[0].status, 'delivered');
s.turn([read()]); assert.equal(s.state.items[0].status, 'resolved'); assert.equal(s.state.items[0].resolution.kind, 'corrected_tool_result');
});
test('ownership, late observations and modified dispatch cannot mutate the original queue', () => {
const s = scenario(); repeated(s); const original = structuredClone(s.state), message = s.select();
for (const field of Object.keys(scope)) assert.throws(() => observeReminders(s.state, { scope: { ...scope, [field]: 'foreign' }, enabled: true, at: 1005 }), /another Task\/Run/);
assert.throws(() => observeReminders(s.state, { scope, enabled: true, at: 1005, receipt: { observation: { taskId: 't', runId: 'other' }, tools: [], ref: { ordinal: 5 } } }), /source is invalid/);
assert.throws(() => observeReminders(s.state, { scope, enabled: true, at: 1005, receipt: { observation: { taskId: 't', runId: 'r' }, tools: [], ref: { ordinal: 1, artifactId: 'old', sha256: hash('old') } } }), /Late/);
assert.throws(() => s.dispatch({ ...message, content: 'different instructions' }), /differs/);
assert.throws(() => s.dispatch({ ...message, scope: { ...scope, taskId: 'foreign' } }), /ownership/);
assert.deepEqual(s.state, original);
});
test('disabled, cancelled, expired and unavailable-tool advice cannot dispatch or resolve later', () => {
for (const reason of ['disabled', 'cancelled', 'ttl']) {
const s = scenario(); repeated(s); const message = s.select();
s.state = observeReminders(s.state, { scope, enabled: reason !== 'disabled', at: reason === 'ttl' ? s.at + REMINDER_POLICY.ttlMs : s.at, endReason: reason === 'cancelled' ? reason : null });
s.dispatch(message); s.turn([read(4, 8)]); assert.equal(s.state.items[0].status, 'expired'); assert.equal(s.state.items[0].exitReason, reason);
assert.equal(s.state.items[0].resolution, undefined); assert.equal(s.state.items[0].deliveries.length, 0);
}
const absent = scenario(); repeated(absent); assert.equal(absent.select({ visibleTools: ['propose_file'] }), null);
assert.equal(absent.state.items[0].exitReason, 'tool_unavailable_or_advice_too_large');
});
test('ineffective suggestions cool down, reduce frequency and expire without pausing execution', () => {
const s = scenario(); repeated(s); s.dispatch(s.select(), 'first'); assert.equal(s.select(), null);
for (let i = 0; i < REMINDER_POLICY.cooldownTurns; i++) s.turn([read()]);
s.dispatch(s.select(), 'second');
for (let i = 0; i < REMINDER_POLICY.cooldownTurns; i++) s.turn([read()]); assert.equal(s.select(), null);
for (let i = 0; i < REMINDER_POLICY.cooldownTurns; i++) s.turn([read()]); s.dispatch(s.select(), 'third');
assert.equal(s.select(), null); assert.equal(s.state.items[0].exitReason, 'ineffective');
for (let i = 0; i < 30; i++) s.turn([read()]); assert.equal(s.state.items.length, 1); assert.equal(s.select(), null);
});
test('bounded shadow uncertainty never turns retained unknown data into advice', () => {
const s = scenario(); for (let i = 0; i < 45; i++) s.turn([]); repeated(s); assert.equal(s.state.items.length, 0);
});
test('immutable receipt readback verifies original ownership, arguments and result bytes without rewriting B', t => {
const f = fixture(t), run = beginPlanTask(f); const step = f.store.beginStep(f.session, run);
const record = completeRead(f); step.progressToolIds = [record.id]; recordTurnProgress(f.store, f.session, run, step);
const id = run.progressShadowArtifactIds.at(-1), original = structuredClone(run.progressShadow);
const verified = verifyTurnProgressReceipt(f.store, f.session, run, id);
assert.equal(verified.tools[0].read.sha256.length, 64); assert.equal(verified.tools[0].success, true);
const argsSha = record.argsSha256; record.argsSha256 = hash('tampered');
assert.throws(() => verifyTurnProgressReceipt(f.store, f.session, run, id), /来源变化/); record.argsSha256 = argsSha;
const artifact = f.store.core.artifact.get(record.resultArtifactId), old = artifact.sha256; artifact.sha256 = hash('tampered result');
assert.throws(() => verifyTurnProgressReceipt(f.store, f.session, run, id), /来源变化/); artifact.sha256 = old;
assert.throws(() => verifyTurnProgressReceipt(f.store, { ...f.session, projectId: 'foreign' }, run, id), /归属不一致/);
assert.deepEqual(run.progressShadow, original);
});