b118d79f09
The experimental-overhead case pushes complete input past the context window, so the engine input guard reports context_limit, not the reservation-branch budget wording. Assert the actual message and limitingReason; the guard still blocks dispatch with zero requests.
160 lines
14 KiB
JavaScript
160 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 { randomUUID } from 'node:crypto';
|
|
import { fixture, call, until } from './helpers.mjs';
|
|
import { controlledWorker, ControlledWorkerFixture } from './fixtures/controlled-worker.mjs';
|
|
import { applyWorkerProposals, approveWorkerChecks, ownerBytes } from './fixtures/workers-project.mjs';
|
|
import { createPurgeSource, purgeInput } from './fixtures/purge-source.mjs';
|
|
import { resolvePurgeSource, selectPurgeStrategy, assertPurgeStrategy, assertPurgeModelInput, PURGE_SOURCE_FILES } from '../src/core/engines/purge-strategy.mjs';
|
|
import { originalEngineRun, verifyEngineModelInput, nativeMessagesToChat } from '../src/shared/contracts/engine-execution.mjs';
|
|
import { completionBody, requestMetrics } from '../src/shared/contracts/model-request.mjs';
|
|
import { workerHttp } from './fixtures/workers-http.mjs';
|
|
|
|
const usage = { total_tokens: 168, prompt_tokens: 123, completion_tokens: 45 };
|
|
const jobs = f => [...f.store.core.engine_run.values()];
|
|
function setup(t, options = {}, prompt) {
|
|
const f = fixture(t), context = controlledWorker(f, options), sourceRoot = createPurgeSource(path.join(f.root, 'purge-source'));
|
|
context.definition.engine.strategy = purgeInput(sourceRoot, prompt);
|
|
return { f, context, sourceRoot };
|
|
}
|
|
async function prepare(f, context) {
|
|
const group = await context.manager.plan(f.session, context.definition);
|
|
await (await context.manager.prepare(f.session, group.id, group.fingerprint)).promise;
|
|
assert.equal(group.status, 'ready', group.summary); return group;
|
|
}
|
|
|
|
test('two Purge source versions lock nine original inputs without executing JavaScript or postinstall and remain disabled by default', t => {
|
|
const f = fixture(t);
|
|
for (const version of ['1.1.78', '1.1.81']) {
|
|
const root = createPurgeSource(path.join(f.root, version), version), source = resolvePurgeSource(f.root, root);
|
|
assert.equal(source.status, 'available', source.message); assert.equal(source.version, version); assert.equal(source.defaultEnabled, false);
|
|
assert.deepEqual(source.inputs.map(input => input.relative), [...PURGE_SOURCE_FILES]); assert.equal(source.inputs.every(input => /^[a-f0-9]{64}$/.test(input.sha256)), true);
|
|
const strategy = selectPurgeStrategy(purgeInput(root)); assertPurgeStrategy(strategy);
|
|
assert.equal(strategy.upstreamPluginLoaded, false); assert.equal(strategy.automaticRetry, false); assert.equal(strategy.hostModification, false);
|
|
}
|
|
});
|
|
test('incomplete, unsupported, relative and oversized sources cannot enable the experiment', t => {
|
|
const f = fixture(t), root = createPurgeSource(path.join(f.root, 'purge'));
|
|
assert.throws(() => resolvePurgeSource(f.root, 'relative'), /绝对目录/);
|
|
fs.unlinkSync(path.join(root, 'LICENSE')); assert.equal(resolvePurgeSource(f.root, root).status, 'unavailable');
|
|
createPurgeSource(root, '1.1.99'); assert.equal(resolvePurgeSource(f.root, root).status, 'unsupported');
|
|
createPurgeSource(root); fs.writeFileSync(path.join(root, 'lib/core.js'), Buffer.alloc(2 * 1024 * 1024 + 1));
|
|
assert.equal(resolvePurgeSource(f.root, root).status, 'unavailable');
|
|
});
|
|
test('invalid source fingerprints and invented privilege or retry fields fail before any parent Task is created', async t => {
|
|
const { f, context } = setup(t), before = f.store.core.task.size;
|
|
for (const raw of [{ ...context.definition.engine.strategy, sourceSha256: '0'.repeat(64) },
|
|
{ ...context.definition.engine.strategy, hooksDenyBypass: true }, { ...context.definition.engine.strategy, automaticRetry: true },
|
|
{ ...context.definition.engine.strategy, prompt: '\x00bad' }, { ...context.definition.engine.strategy, prompt: '字'.repeat(8001) }]) {
|
|
await assert.rejects(context.manager.plan(f.session, { ...context.definition, engine: { ...context.definition.engine, strategy: raw } }));
|
|
assert.equal(f.store.core.task.size, before); assert.equal(context.requests.length, 0);
|
|
}
|
|
});
|
|
test('source edits between detection and plan are rejected without creating a parent Task', async t => {
|
|
const { f, context, sourceRoot } = setup(t), before = f.store.core.task.size;
|
|
fs.appendFileSync(path.join(sourceRoot, 'lib/identity.js'), '// changed\n');
|
|
await assert.rejects(context.manager.plan(f.session, context.definition), /检测不一致/);
|
|
assert.equal(f.store.core.task.size, before); assert.equal(context.requests.length, 0);
|
|
});
|
|
test('source edits after sealing cannot launch a child and do not reset its cumulative budget', async t => {
|
|
const { f, context, sourceRoot } = setup(t), group = await prepare(f, context), task = f.store.core.task.get(group.children[0].taskId), before = structuredClone(task.usage);
|
|
fs.appendFileSync(path.join(sourceRoot, 'lib/core.js'), '// changed\n');
|
|
await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
|
|
assert.match(group.summary, /来源已变化/); assert.equal(context.native.length, 0); assert.equal(context.requests.length, 0); assert.deepEqual(task.usage, before);
|
|
});
|
|
test('literal experiment text is sealed into the parent, worker and each new Run while actual host approvals and saved results remain required', async t => {
|
|
const prompt = 'Preserve {{unknown_literal}} and 原字面值; repair only the sealed source paths.', { f, context } = setup(t, {}, prompt), original = ownerBytes(f), group = await prepare(f, context);
|
|
const bound = group.children[0].engine.strategy;
|
|
assert.equal(bound.prompt, prompt); assert.equal(bound.section.interpolate, false);
|
|
await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
|
|
await applyWorkerProposals(f, context.runtime, group);
|
|
await approveWorkerChecks(f, context.runtime, group, context.manager.start(f.session, group.id, group.fingerprint));
|
|
assert.equal(group.status, 'ready_to_collect', group.summary); assert.deepEqual(ownerBytes(f), original);
|
|
const session = f.store.session(group.children[0].sessionId), worker = f.store.core.worker.get(group.children[0].id);
|
|
assert.deepEqual(worker.engine.strategy, bound); assert.equal(session.approvals.filter(value => value.status === 'approved').length, 1);
|
|
assert.equal(jobs(f).length, 2);
|
|
for (const job of jobs(f)) {
|
|
assert.deepEqual(job.installation.strategy, bound); originalEngineRun(f.store, session, job);
|
|
for (const request of job.requests) { const saved = verifyEngineModelInput(f.store, session, job, request); assertPurgeModelInput(saved.native, bound); assert.ok(saved.body.messages[0].content.includes(prompt)); }
|
|
}
|
|
assert.equal(f.store.core.task.get(worker.taskId).usage.requests, context.requests.length); assert.equal(f.store.core.task.get(worker.taskId).usage.totalTokens, context.requests.length * 168);
|
|
});
|
|
test('source changes after a model response stop before any native tool is accepted, retaining the actual usage once', async t => {
|
|
let sourceRoot;
|
|
const state = setup(t, { respond: () => { fs.appendFileSync(path.join(sourceRoot, 'lib/identity.js'), '// changed after response\n');
|
|
return { message: { role: 'assistant', content: '', tool_calls: [call('blocked', 'propose_file', { path: 'src/a.mjs', content: 'bad', reason: 'bad' })] }, usage }; } });
|
|
sourceRoot = state.sourceRoot; const { f, context } = state, group = await prepare(f, context);
|
|
await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
|
|
assert.equal(f.store.session(group.children[0].sessionId).changes.length, 0); assert.equal(jobs(f)[0].calls.length, 0);
|
|
assert.equal(context.requests.length, 1); assert.equal(f.store.core.task.get(group.children[0].taskId).usage.totalTokens, 168); assert.match(jobs(f)[0].error, /来源已变化/);
|
|
});
|
|
test('source changes while approval is pending invalidate the old permission before an engineering check starts', async t => {
|
|
const { f, context, sourceRoot } = setup(t, { respond: ({ messages }) => {
|
|
const result = messages.filter(message => message.role === 'tool').at(-1);
|
|
return { message: { role: 'assistant', content: '', tool_calls: [result ? call('check', 'run_checks', { plan_id: JSON.parse(result.content).id }) : call('plan', 'plan_checks', { names: ['test', 'build'] })] }, usage };
|
|
} }), group = await prepare(f, context), started = context.manager.start(f.session, group.id, group.fingerprint), session = f.store.session(group.children[0].sessionId);
|
|
await until(() => session.approvals.some(value => value.status === 'pending'), 15000);
|
|
fs.appendFileSync(path.join(sourceRoot, 'cordis.patch.yml'), '# changed while awaiting approval\n');
|
|
context.runtime.decide(session.id, session.approvals.find(value => value.status === 'pending').id, true);
|
|
await started.promise; assert.equal(f.store.core.check_job.size, 0); assert.equal(jobs(f)[0].status, 'failed'); assert.equal(context.requests.length, 2); assert.match(jobs(f)[0].error, /来源已变化/);
|
|
});
|
|
test('altered text, interpolation, section order and policy flags cannot reuse a sealed experiment', t => {
|
|
const { context } = setup(t), strategy = selectPurgeStrategy(context.definition.engine.strategy);
|
|
for (const altered of [{ ...strategy, prompt: 'changed' }, { ...strategy, section: { ...strategy.section, interpolate: true } },
|
|
{ ...strategy, section: { ...strategy.section, order: 0 } }, { ...strategy, automaticRetry: true }]) assert.throws(() => assertPurgeStrategy(altered), /已变化/);
|
|
});
|
|
test('the observed SDK system-message layout and the explicit system field both require exactly one effective section, never a user or tool copy', t => {
|
|
const { context } = setup(t), strategy = selectPurgeStrategy(context.definition.engine.strategy), text = strategy.section.text;
|
|
const message = role => ({ role, content: [{ type: 'text', text: 'Original identity\n\n' + text }] });
|
|
assert.doesNotThrow(() => assertPurgeModelInput({ system: null, messages: [message('system')] }, strategy));
|
|
assert.doesNotThrow(() => assertPurgeModelInput({ system: text, messages: [message('user')] }, strategy));
|
|
assert.throws(() => assertPurgeModelInput({ system: text, messages: [message('system')] }, strategy), /唯一完整/);
|
|
assert.throws(() => assertPurgeModelInput({ system: null, messages: [message('system'), message('system')] }, strategy), /唯一完整/);
|
|
for (const role of ['user', 'assistant', 'tool']) assert.throws(() => assertPurgeModelInput({ system: null, messages: [message(role)] }, strategy), /唯一完整/);
|
|
});
|
|
test('a missing or duplicated experimental section in the actual input is rejected before any model dispatch', async t => {
|
|
for (const copies of [0, 2]) {
|
|
class InvalidPrompt extends ControlledWorkerFixture {
|
|
async wait() { const { onModel, tools, config, signal, strategy } = this.options;
|
|
return onModel({ system: strategy.section.text.repeat(copies), model: config.model, maxTokens: config.maxOutputTokens,
|
|
messages: [{ role: 'user', content: [{ type: 'text', text: 'owned task' }] }], tools: tools.map(value => value.function) }, { id: randomUUID() }, signal); }
|
|
}
|
|
const { f, context } = setup(t, { sessionFactory: () => new InvalidPrompt() }), group = await prepare(f, context);
|
|
await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
|
|
assert.equal(context.requests.length, 0); assert.equal(jobs(f)[0].requests.length, 0); assert.match(jobs(f)[0].error, /唯一完整/);
|
|
}
|
|
});
|
|
test('experimental prompt overhead still participates in the original complete-input budget guard', async t => {
|
|
class ThresholdInput extends ControlledWorkerFixture {
|
|
async wait() {
|
|
const { onModel, tools, config, signal, strategy } = this.options;
|
|
const input = { system: '', model: config.model, maxTokens: config.maxOutputTokens,
|
|
messages: [{ role: 'user', content: [{ type: 'text', text: 'owned task' }] }], tools: tools.map(value => value.function) };
|
|
const estimate = text => requestMetrics(completionBody(config, nativeMessagesToChat({ ...input, system: text }), tools)).estimatedInputTokens + 128 + config.maxOutputTokens;
|
|
let low = 0, high = config.contextLimit * 4;
|
|
while (low + 1 < high) { const middle = Math.floor((low + high) / 2); if (estimate('a'.repeat(middle)) <= config.contextLimit) low = middle; else high = middle; }
|
|
const baseline = 'a'.repeat(low); this.baselineEstimate = estimate(baseline); this.experimentalEstimate = estimate(baseline + '\n\n' + strategy.section.text);
|
|
assert.ok(this.baselineEstimate <= config.contextLimit); assert.ok(this.experimentalEstimate > config.contextLimit);
|
|
return onModel({ ...input, system: baseline + '\n\n' + strategy.section.text }, { id: randomUUID() }, signal);
|
|
}
|
|
}
|
|
const { f, context } = setup(t, { sessionFactory: () => new ThresholdInput() }, '工程'.repeat(3900)); f.store.saveConfig({ contextLimit: 64000, maxOutputTokens: 256 });
|
|
const group = await prepare(f, context); await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
|
|
// The overhead pushes the complete input past the context window, so the guard
|
|
// reports context_limit; the original budget wording belongs to the reservation branch.
|
|
assert.equal(context.requests.length, 0); assert.equal(jobs(f)[0].requests.length, 0); assert.match(jobs(f)[0].error, /上下文窗口可用容量/);
|
|
assert.equal(jobs(f)[0].inputRejections[0].limitingReason, 'context_limit');
|
|
assert.equal(f.store.core.task.get(group.children[0].taskId).usage.requests, 0);
|
|
assert.ok(context.native[0].baselineEstimate <= 64000); assert.ok(context.native[0].experimentalEstimate > 64000);
|
|
});
|
|
test('source detection HTTP remains local-authenticated and read-only with no configured model requests', async t => {
|
|
const f = fixture(t), host = await workerHttp(f), root = createPurgeSource(path.join(f.root, 'purge'), '1.1.81'), before = f.store.core.task.size;
|
|
const route = '/engines/purge/source?root=' + encodeURIComponent(root), source = await host.api(route);
|
|
assert.equal(source.status, 'available'); assert.equal(source.defaultEnabled, false); assert.equal(source.inputs.length, 9);
|
|
assert.equal((await host.raw(route, undefined, 'foreign')).status, 403);
|
|
assert.equal((await host.raw('/engines/purge/source?root=relative')).status, 400);
|
|
assert.equal(f.store.core.task.size, before); assert.equal(host.requests.length, 0);
|
|
});
|