docs(release): seal and deliver task agreement 1.25 with retained production state

This commit is contained in:
Agent Workbench Dev
2026-11-04 15:34:29 +08:00
parent 78976ac775
commit ddefe5f108
1444 changed files with 2161122 additions and 17 deletions
@@ -0,0 +1,238 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { fixture, compatibleFixture, until, assertToolPairs, completion } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { completionBody, actualRequest, requestHistory, requestMetrics, requestImages } from '../src/shared/contracts/model-request.mjs';
import { modelRequestUrl } from '../src/shared/contracts/model-endpoint.mjs';
import { normalizeProtocolResponse, reportedUsage } from '../src/core/models/protocols.mjs';
import { nativeParameterSettings } from '../src/shared/contracts/native-parameters.mjs';
import { readModelStream } from '../src/core/models/model-stream.mjs';
import { visionProbe } from '../src/core/models/vision-probe.mjs';
import { ConfigImporter } from '../src/core/models/config-import.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
const protocol = 'anthropic-messages', model = 'claude-owned';
const usage = { input_tokens: 100, output_tokens: 12, cache_creation_input_tokens: 20, cache_read_input_tokens: 30 };
const config = baseUrl => ({ mode: 'compatible', baseUrl, protocol, model, maxOutputTokens: 256 });
const message = (content = [{ type: 'text', text: '完成。' }], extra = {}) => ({ id: 'msg_owned', type: 'message', role: 'assistant', model, content, stop_reason: content.some(b => b.type === 'tool_use') ? 'tool_use' : 'end_turn', stop_sequence: null, usage, ...extra });
const tool = { type: 'tool_use', id: 'call_owned', name: 'list_files', input: { path: '', depth: 1 } };
const definition = { type: 'function', function: { name: 'list_files', description: 'owned fixture only', parameters: { type: 'object', properties: { path: { type: 'string' } } } } };
const event = (type, fields = {}) => `event: ${type}\r\ndata: ${JSON.stringify({ type, ...fields })}\r\n\r\n`;
function stream(content = [{ type: 'text', text: '完成。' }], extra = {}) {
const start = message([], { usage: { ...usage, output_tokens: 1 }, stop_reason: null });
const result = [event('message_start', { message: start }), event('ping')];
content.forEach((block, index) => {
const initial = block.type === 'tool_use' ? { ...block, input: {} } : block.type === 'text' ? { ...block, text: '' }
: block.type === 'thinking' ? { ...block, thinking: '', signature: '' } : block;
result.push(event('content_block_start', { index, content_block: initial }));
if (block.type === 'tool_use') {
const json = JSON.stringify(block.input), at = Math.floor(json.length / 2);
for (const partial_json of [json.slice(0, at), json.slice(at)]) result.push(event('content_block_delta', { index, delta: { type: 'input_json_delta', partial_json } }));
} else if (block.type === 'text') result.push(event('content_block_delta', { index, delta: { type: 'text_delta', text: block.text } }));
else if (block.type === 'thinking') {
result.push(event('content_block_delta', { index, delta: { type: 'thinking_delta', thinking: block.thinking } }));
result.push(event('content_block_delta', { index, delta: { type: 'signature_delta', signature: block.signature } }));
}
result.push(event('content_block_stop', { index }));
});
result.push(event('message_delta', { delta: { stop_reason: content.some(b => b.type === 'tool_use') ? 'tool_use' : 'end_turn', stop_sequence: null, ...extra }, usage: { output_tokens: usage.output_tokens } }));
result.push(event('message_stop'));
return result.join('');
}
async function streamingServer(t, action) {
const requests = [], sockets = new Set();
const server = http.createServer(async (req, res) => {
const parts = []; for await (const chunk of req) parts.push(chunk);
const body = JSON.parse(Buffer.concat(parts)); requests.push({ url: req.url, body, headers: req.headers });
res.writeHead(200, { 'Content-Type': 'text/event-stream', 'request-id': 'req_owned_messages' });
try { await action(res, body); } catch { res.destroy(); }
});
server.on('connection', socket => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { for (const socket of sockets) socket.destroy(); server.close(resolve); }));
return { baseUrl: 'http://127.0.0.1:' + server.address().port, requests };
}
test('Messages builds native system/tool history, bounds images, and seals a distinct mixed-pool variant', () => {
const image = visionProbe().image, cfg = { ...config('https://example.test'), inputImages: [image] };
const history = [{ role: 'system', content: '系统一' }, { role: 'system', content: '系统二' }, { role: 'user', content: '读取' },
{ role: 'assistant', content: '核对', tool_calls: [{ id: tool.id, type: 'function', function: { name: tool.name, arguments: JSON.stringify(tool.input) } }] },
{ role: 'tool', tool_call_id: tool.id, content: '{"files":[]}' }, { role: 'user', content: '继续' }];
const body = completionBody(cfg, history, [definition]);
assert.deepEqual(body.system, [{ type: 'text', text: '系统一' }, { type: 'text', text: '系统二' }]);
assert.equal(body.messages[1].content[1].type, 'tool_use'); assert.equal(body.messages[2].content[0].tool_use_id, tool.id);
assert.equal(body.messages[2].content[1].text, '继续'); assert.equal(body.max_tokens, 256); assert.equal(body.tool_choice.type, 'auto');
assert.deepEqual(body.tools[0].input_schema, definition.function.parameters); assert.equal(body.tools[0].function, undefined);
assert.equal(requestImages(body)[0], image.dataUrl); assert.equal(requestMetrics(body).imageReservationTokens, 4096);
assert.equal(requestHistory(body).filter(m => m.role === 'tool')[0].tool_call_id, tool.id);
assert.deepEqual(requestHistory(body).filter(m => m.role === 'system').map(m => m.content), ['系统一', '系统二']);
assert.equal(completionBody(config('https://example.test'), [{ role: 'user', content: 'OK' }], []).tool_choice, undefined);
const members = [{ id: 'chat', protocol: 'chat-completions', model: 'gpt-owned' }, { id: 'messages', protocol, model, baseUrl: cfg.baseUrl }];
const mixed = completionBody({ ...cfg, modelRouting: { members } }, history, [definition]);
assert.equal(mixed.kind, 'model-route-plan'); assert.deepEqual(actualRequest(mixed, members[1]), body);
assert.ok(actualRequest(mixed, members[0]).messages.some(m => m.role === 'system'));
});
test('Messages URL and native parameter contracts preserve other protocol paths and hard output budget', () => {
for (const root of ['https://example.test', 'https://example.test/v1', 'https://example.test/v1/']) assert.equal(modelRequestUrl(config(root)), 'https://example.test/v1/messages');
assert.equal(modelRequestUrl(config('https://example.test/proxy')), 'https://example.test/proxy/v1/messages');
assert.equal(modelRequestUrl({ ...config('https://example.test'), protocol: 'chat-completions' }), 'https://example.test/chat/completions');
const native = { temperature: 0.5, top_k: 10, stop_sequences: ['END'], thinking: { type: 'enabled', budget_tokens: 1024 } };
assert.deepEqual(completionBody({ ...config('https://example.test'), maxOutputTokens: 2048, nativeParameters: native }, [{ role: 'user', content: 'x' }], []).thinking, native.thinking);
assert.throws(() => completionBody({ ...config('https://example.test'), nativeParameters: native }, [], []), /小于/);
for (const invalid of [{ max_tokens: 9999 }, { temperature: 2 }, { parallel_tool_calls: true }, { thinking: { type: 'enabled', budget_tokens: 1 } }]) assert.throws(() => nativeParameterSettings(protocol, invalid));
assert.throws(() => nativeParameterSettings(protocol, {}, 'max_completion_tokens'));
});
test('native usage includes both cache buckets once, retains raw and rejects invalid counters', () => {
assert.equal(reportedUsage({ protocol }, { usage }).prompt_tokens, 150);
assert.equal(reportedUsage({ protocol }, { usage }).total_tokens, 162);
assert.deepEqual(reportedUsage({ protocol }, { usage }).raw, usage);
assert.equal(reportedUsage({ protocol }, { usage: { ...usage, cache_read_input_tokens: -1 } }), null);
assert.equal(reportedUsage({ protocol }, { usage: { input_tokens: 1 } }), null);
});
for (const streaming of [false, true]) test(`real HTTP Messages ${streaming ? 'SSE' : 'JSON'} executes one tool, retains opaque history, actual URL and cache accounting`, async t => {
const f = fixture(t), thinking = { type: 'thinking', thinking: 'owned fixture reasoning', signature: 'owned-signature' };
const content = [thinking, { type: 'text', text: '开始读取。' }, tool];
const server = streaming ? await streamingServer(t, async (res, body) => {
const raw = Buffer.from(stream(body.messages.some(m => m.content.some(b => b.type === 'tool_result')) ? undefined : content));
for (let at = 0; at < raw.length; at += 7) res.write(raw.subarray(at, at + 7)); res.end();
}) : await compatibleFixture(t, (_url, body) => ({ body: message(body.messages.some(m => m.content.some(b => b.type === 'tool_result')) ? undefined : content), headers: { 'request-id': 'req_owned_messages' } }));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = hub.upsert({ ...config(server.baseUrl), name: 'Owned Messages', requiresKey: false,
streaming, capabilityDeclarations: { streaming: 'supported' }, modelIdentityPolicy: 'require-match' });
hub.select({ type: 'connection', id: connection.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Read the project files once.'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(server.requests.length, 2);
const second = server.requests[1].input ?? server.requests[1].body;
assert.deepEqual(second.messages.find(m => m.role === 'assistant').content[0], thinking); assertToolPairs(f.session.providerMessages);
assert.equal(f.session.events.filter(e => e.tool === 'list_files' && e.phase === 'completed').length, 1);
const run = f.store.core.run.get(f.session.runId); assert.equal(run.usage.inputTokens, 300); assert.equal(run.usage.outputTokens, 24);
assert.equal(run.consumption.length, 2); assert.equal(run.consumption[0].requestUrl, modelRequestUrl(config(server.baseUrl)));
for (const attempt of hub.view().attempts) {
assert.equal(attempt.modelIdentity.status, 'matched'); assert.equal(attempt.upstreamResponse.responseUrl, attempt.requestUrl);
assert.deepEqual(attempt.usage.raw, usage); assert.equal(attempt.usage.total_tokens, 162);
const artifact = f.store.core.artifact.get(attempt.responseArtifactId); assert.equal(attempt.responseSha256, artifact.sha256);
assert.equal(attempt.modelIdentity.responseSource.sha256, artifact.sha256);
}
for (const plan of f.store.core.context_plan.values()) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
for (const messagesAuth of ['api-key', 'bearer']) test('Messages ' + messagesAuth + ' authentication is explicit; mismatched model remains blocked with original usage', async t => {
const server = await compatibleFixture(t, () => ({ body: message(undefined, { model: 'gpt-other' }) }));
const provider = new Provider(), progress = [];
await assert.rejects(provider.complete({ ...config(server.baseUrl), messagesAuth, modelIdentityPolicy: 'require-match' }, [{ role: 'user', content: 'OK' }], null, undefined, e => progress.push(e), null, [], { attempts: 1 }), cause => cause.diagnostic?.modelIdentity?.status === 'mismatch');
assert.equal(server.requests.length, 1); const headers = server.requests[0].headers;
assert.equal(headers['anthropic-version'], '2023-06-01');
const received = progress.find(e => e.consumption).consumption;
assert.equal(received.usage.total_tokens, 162); assert.equal(received.modelIdentity.reportedModels[0], 'gpt-other');
// Use only a synthetic test credential, never any user's saved connection.
provider.setKey('owned-key');
await assert.rejects(provider.complete({ ...config(server.baseUrl), messagesAuth, modelIdentityPolicy: 'require-match' }, [{ role: 'user', content: 'OK' }], null, undefined, undefined, null, [], { attempts: 1 }));
const keyed = server.requests[1].headers;
assert.equal(keyed['x-api-key'], messagesAuth === 'api-key' ? 'owned-key' : undefined);
assert.equal(keyed.authorization, messagesAuth === 'bearer' ? 'Bearer owned-key' : undefined);
});
test('Messages redirect is not followed, either credential stays at the original server', async t => {
const target = await compatibleFixture(t, () => ({ body: message() })), origin = await compatibleFixture(t, () => ({ status: 307, headers: { location: target.baseUrl + '/messages' }, body: {} }));
const provider = new Provider(); provider.setKey('owned-redirect-key');
await assert.rejects(provider.complete(config(origin.baseUrl), [{ role: 'user', content: 'OK' }], null, undefined, undefined, null, [], { attempts: 1 }));
assert.equal(origin.requests.length, 1); assert.equal(target.requests.length, 0);
assert.equal(provider.lastRequest.requestUrl, modelRequestUrl(config(origin.baseUrl))); assert.equal(provider.lastRequest.redirectMode, 'manual');
});
test('strict native SSE reads the nested start model and retains mismatch source without retries', async t => {
const raw = stream().replace('"model":"claude-owned"', '"model":"gpt-wrong"');
const server = await streamingServer(t, res => res.end(raw)), provider = new Provider(), progress = [];
await assert.rejects(provider.complete({ ...config(server.baseUrl), streaming: true, modelIdentityPolicy: 'require-match' }, [{ role: 'user', content: 'OK' }], null, undefined, e => progress.push(e), null, []), cause => cause.diagnostic?.modelIdentity?.status === 'mismatch');
assert.equal(server.requests.length, 1); assert.equal(progress.find(e => e.streamCapture).streamCapture.toString(), raw);
assert.equal(progress.find(e => e.consumption).consumption.usage.total_tokens, 162);
});
test('a closed native tool block cannot execute before message_stop, then executes exactly once', async t => {
const f = fixture(t); let release, ready = false;
const gate = new Promise(resolve => { release = resolve; });
const server = await streamingServer(t, async (res, body) => {
if (body.messages.some(m => m.content.some(b => b.type === 'tool_result'))) { res.end(stream()); return; }
res.write(stream([tool]).replace(event('message_stop'), '')); ready = true;
await gate; res.end(event('message_stop'));
});
const provider = new Provider(), hub = provider.modelHub(f.store), connection = hub.upsert({ ...config(server.baseUrl), name: 'Owned held terminal', requiresKey: false, streaming: true, capabilityDeclarations: { streaming: 'supported' } });
hub.select({ type: 'connection', id: connection.id }); const runtime = new Runtime(f.store, provider); t.after(() => { release(); runtime.stopAll(); });
runtime.start(f.session.id, 'Read once.'); await until(() => ready); await delay(150);
assert.equal(f.session.events.filter(e => e.tool === 'list_files').length, 0); release();
await until(() => !runtime.isActive(f.session.id), 15000); assert.equal(f.session.status, 'completed');
assert.equal(f.session.events.filter(e => e.tool === 'list_files' && e.phase === 'completed').length, 1);
});
test('mixed Chat/Messages pool seals both variants and preserves one completed tool on failover', async t => {
const f = fixture(t), first = await compatibleFixture(t, () => ({ status: 429, body: { error: { message: 'owned throttling' } } }));
const second = await compatibleFixture(t, (_url, body) => ({ body: message(body.messages.some(m => m.content.some(b => b.type === 'tool_result')) ? undefined : [tool]) }));
const provider = new Provider(), hub = provider.modelHub(f.store);
const chat = hub.upsert({ ...config(first.baseUrl), protocol: 'chat-completions', name: 'Owned Chat', requiresKey: false });
const native = hub.upsert({ ...config(second.baseUrl), name: 'Owned Messages', requiresKey: false });
const pool = hub.pool({ name: 'Owned mixed', members: [{ connectionId: chat.id }, { connectionId: native.id }], maxAttempts: 2 });
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Read the project files once.'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed'); assert.equal(first.requests.length, 1); assert.equal(second.requests.length, 2);
assert.equal(f.session.events.filter(e => e.tool === 'list_files' && e.phase === 'completed').length, 1);
});
test('native JSON rejects incomplete stops, duplicate tools and hosted content', () => {
for (const value of [message([tool], { stop_reason: 'max_tokens' }), message([tool], { stop_reason: 'pause_turn' }), message([tool], { stop_reason: 'end_turn' }),
message([{ type: 'server_tool_use', name: 'bash' }]), message([{ type: 'tool_use', id: 'x', name: 'list_files', input: [] }]), message([{ type: 'thinking', thinking: 'x', signature: '' }])]) {
assert.throws(() => normalizeProtocolResponse({ protocol }, value));
}
});
test('strict SSE validates all blocks, terminal, event names, UTF8 and tool JSON before dispatch', async t => {
const original = stream([tool]), start = event('message_start', { message: message([], { stop_reason: null }) });
const cases = [original.replace(event('message_stop'), ''), original.replace('"partial_json":"{\\"path', '"partial_json":"!{\\"path'),
original.replace('event: content_block_delta', 'event: message_delta'), original + event('ping'),
original.replace('"index":0', '"index":3'), original.replace('"stop_reason":"tool_use"', '"stop_reason":"max_tokens"'),
start + event('message_stop'), start + event('content_block_delta', { index: 0, delta: { type: 'text_delta', text: 'orphan' } }),
original.replace('event: message_stop\r\ndata: {"type":"message_stop"}', 'data: [DONE]')];
for (let i = 0; i < cases.length; i++) await t.test('reject native stream ' + i, async () => {
assert.notEqual(cases[i], original, 'fixture mutation must change actual bytes');
await assert.rejects(readModelStream(new Response(cases[i], { headers: { 'content-type': 'text/event-stream' } }), { protocol }, undefined, undefined, [definition]));
});
});
test('upstream SSE error retains original type/request ID and initial usage, never retries', async t => {
const raw = event('message_start', { message: message([], { stop_reason: null }) }) + event('error', { error: { type: 'overloaded_error', message: 'owned overload' } });
const server = await streamingServer(t, res => res.end(raw)), provider = new Provider(), progress = [];
await assert.rejects(provider.complete({ ...config(server.baseUrl), streaming: true }, [{ role: 'user', content: 'OK' }], null, undefined, e => progress.push(e), null, []), cause => cause.diagnostic?.upstream?.type === 'overloaded_error');
assert.equal(server.requests.length, 1); assert.equal(progress.find(e => e.streamCapture).streamCapture.toString(), raw);
assert.equal(progress.find(e => e.consumption).consumption.usage.total_tokens, 162);
});
test('stop cancels the owned stream and retains partial consumption without running tool or replaying', async t => {
let closed = false;
const server = await streamingServer(t, res => { res.on('close', () => { closed = true; }); res.write(event('message_start', { message: message([], { stop_reason: null }) })
+ event('content_block_start', { index: 0, content_block: { ...tool, input: {} } })
+ event('content_block_delta', { index: 0, delta: { type: 'input_json_delta', partial_json: '{"path":' } })); });
const provider = new Provider(), controller = new AbortController(), progress = [];
const pending = provider.complete({ ...config(server.baseUrl), streaming: true }, [{ role: 'user', content: 'OK' }], null, controller.signal, e => progress.push(e), null, []);
const rejected = assert.rejects(pending); await until(() => progress.some(e => e.streaming)); controller.abort(); await rejected;
await until(() => closed); assert.equal(server.requests.length, 1);
assert.equal(progress.find(e => e.consumption).consumption.outcome, 'interrupted');
assert.equal(progress.find(e => e.streamCapture).stream.terminal, false);
});
test('saved Messages import preserves auth choice, rejects endpoint duplication, and keeps source file untouched', t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store), importer = new ConfigImporter(hub);
assert.throws(() => hub.upsert({ ...config('https://example.test/v1/messages'), name: 'invalid root' }), /根地址/);
assert.throws(() => hub.upsert({ ...config('https://example.test'), messagesAuth: 'guess', name: 'invalid auth' }), /鉴权/);
const filename = path.join(f.root, 'connections.json'), input = { schema: 1, connections: [{ ...config('https://example.test'), name: 'Owned import', messagesAuth: 'bearer', requiresKey: false }] };
fs.writeFileSync(filename, JSON.stringify(input)); const before = fs.readFileSync(filename);
const preview = importer.preview('workbench-json', filename), result = importer.apply(preview.id);
assert.equal(hub.connection(result.imported[0]).messagesAuth, 'bearer'); assert.deepEqual(fs.readFileSync(filename), before);
});
@@ -0,0 +1,170 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { fixture, launchWorkbench, compatibleFixture, until, assertToolPairs } from './helpers.mjs';
import { createSessionPublisher } from '../src/server/app.mjs';
test('large source receipts use cursor-preserving invalidation without filling the event stream or changing the session', () => {
const session = { id: 'owned-session', projectId: 'owned-project', revision: 10, eventSeq: 42, source: '汉'.repeat(24000) };
let queued = 0, destroyed = false; const chunks = [];
const response = { get writableLength() { return queued; }, write(value) { queued += Buffer.byteLength(value); chunks.push(value); }, destroy() { destroyed = true; } };
const publish = createSessionPublisher(new Set([response]));
for (let i = 0; i < 100; i++) publish(session);
assert.equal(destroyed, false); assert.ok(queued < 64 * 1024);
assert.ok(chunks.every(value => value.startsWith('id: 42\ndata: ')));
assert.deepEqual(JSON.parse(chunks[0].split('\ndata: ')[1]), { kind: 'session_changed', seq: 42, sessionId: session.id, projectId: session.projectId, revision: 10 });
assert.equal(session.source, '汉'.repeat(24000)); assert.equal(session.eventSeq, 42);
});
test('small event updates retain the current session and actual cursor', () => {
const session = { id: 'owned-small', projectId: 'owned-project', revision: 3, eventSeq: 7, status: 'awaiting_approval' }, chunks = [];
createSessionPublisher(new Set([{ write(value) { chunks.push(value); } }]))(session);
assert.deepEqual(JSON.parse(chunks[0].split('\ndata: ')[1]), { kind: 'session', seq: 7, session });
assert.ok(chunks[0].startsWith('id: 7\n'));
});
test('the bundled local service enforces host, origin, token and JSON object contracts', async t => {
const f = fixture(t), server = await launchWorkbench(t, f.data); f.onCleanup(server.close);
assert.equal((await fetch(server.url + '/api/health')).status, 200);
assert.equal((await fetch(server.url + '/api/sessions')).status, 403);
assert.equal((await fetch(server.url + '/api/bootstrap', { headers: { Origin: 'https://example.invalid' } })).status, 403);
const badHostStatus = await new Promise((resolve, reject) => {
http.get(server.url + '/api/health', { headers: { Host: 'example.invalid' } }, response => { response.resume(); resolve(response.statusCode); }).on('error', reject);
});
assert.equal(badHostStatus, 421);
await assert.rejects(server.api('/sessions', null), { status: 400 });
assert.equal(server.bootstrap.capabilities.commands, 'per-call-approval');
const page = await fetch(server.url); assert.equal(page.status, 200); assert.match(await page.text(), /Agent Workbench/);
assert.equal(page.headers.get('x-frame-options'), 'DENY');
assert.equal(page.headers.get('referrer-policy'), 'no-referrer');
assert.match(page.headers.get('content-security-policy'), /script-src 'self'/);
assert.match(page.headers.get('content-security-policy'), /frame-ancestors 'none'/);
await server.close();
});
test('an in-flight connection probe prevents configuration changes and another probe until its actual result returns', async t => {
const f = fixture(t), host = await launchWorkbench(t, f.data); f.onCleanup(host.close);
let release;
const model = await compatibleFixture(t, async () => { await new Promise(resolve => { release = resolve; }); return { choices: [{ message: { role: 'assistant', content: 'OK' } }] }; });
await host.api('/config', { mode: 'compatible', model: 'owned-probe-model', baseUrl: model.baseUrl, apiKey: 'owned-probe-fixture' });
const probing = host.api('/config/test', {});
await until(() => !!release);
await assert.rejects(host.api('/config', { mode: 'demo' }), { status: 409 });
await assert.rejects(host.api('/config/test', {}), { status: 409 });
await assert.rejects(host.api('/model-center/connections', { name: 'Blocked', model: 'owned-model', baseUrl: model.baseUrl }), { status: 409 });
assert.equal(model.requests.length, 1); assert.equal((await host.api('/bootstrap')).config.model, 'owned-probe-model');
release(); assert.equal((await probing).ok, true);
assert.equal((await host.api('/config', { mode: 'demo', model: 'owned-probe-model', baseUrl: model.baseUrl })).mode, 'demo');
});
test('API review applies verified content, detects conflicts, exports sessions and keeps keys in memory', async t => {
const f = fixture(t), server = await launchWorkbench(t, f.data); f.onCleanup(server.close);
const project = await server.api('/projects', { root: f.projectRoot });
const session = await server.api('/sessions', { projectId: project.id });
const file = await server.api('/projects/' + project.id + '/file?path=README.md');
const proposed = await server.api('/sessions/' + session.id + '/proposals', { path: 'README.md', content: '# API suggestion\n', base_sha256: file.sha256 });
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'README.md'), 'utf8'), file.content);
const changed = await server.api('/sessions/' + session.id + '/changes/' + proposed.changes[0].id + '/apply', {});
assert.equal(changed.changes[0].status, 'applied'); assert.ok(changed.revision > proposed.revision);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'README.md'), 'utf8'), '# API suggestion\n');
const next = await server.api('/sessions/' + session.id + '/proposals', { path: 'README.md', content: '# Another suggestion\n' });
fs.writeFileSync(path.join(f.projectRoot, 'README.md'), '# External edit\n');
await assert.rejects(server.api('/sessions/' + session.id + '/changes/' + next.changes.at(-1).id + '/apply', {}), { status: 409 });
const conflicted = await server.api('/sessions/' + session.id);
assert.equal(conflicted.changes.at(-1).status, 'conflict');
await server.api('/sessions/' + session.id + '/changes/' + next.changes.at(-1).id + '/reject', {});
const model = await compatibleFixture(t, () => ({ data: [{ id: 'fixture-model' }] }));
await server.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'fixture-model', apiKey: 'fixture-api-saved-key' });
await server.api('/config/models', { baseUrl: model.baseUrl, apiKey: 'fixture-tentative-key' });
assert.equal(model.requests[0].headers.authorization, 'Bearer fixture-tentative-key');
const bootstrap = await server.api('/bootstrap'); assert.equal(bootstrap.config.hasKey, true);
assert.ok(!fs.readFileSync(path.join(f.data, 'config.json'), 'utf8').includes('fixture-api-saved-key'));
const exported = await server.api('/sessions/' + session.id + '/export');
assert.equal(exported.schemaVersion, 1); assert.equal(exported.session.id, session.id);
assert.ok(!JSON.stringify(exported).includes('fixture-api-saved-key'));
assertToolPairs(exported.session.providerMessages);
await server.close();
const restarted = await launchWorkbench(t, f.data); f.onCleanup(restarted.close);
assert.equal(restarted.bootstrap.config.hasKey, false);
assert.equal(restarted.bootstrap.config.model, 'fixture-model');
assert.equal((await restarted.api('/sessions/' + session.id)).changes[0].status, 'applied');
await restarted.close();
});
test('a real bundle restart preserves proposals, interrupts old approvals and requests fresh consent on resume', async t => {
const f = fixture(t), server = await launchWorkbench(t, f.data); f.onCleanup(server.close);
const session = await server.api('/sessions', { projectId: f.project.id });
await server.api('/sessions/' + session.id + '/messages', { content: 'Run the offline workflow on this fixture.' });
let waiting;
await until(async () => { waiting = await server.api('/sessions/' + session.id); return waiting.status === 'awaiting_approval'; });
const oldApproval = waiting.approvals[0], proposal = waiting.changes[0];
assert.equal(fs.existsSync(path.join(f.projectRoot, proposal.path)), false);
assert.equal(waiting.events.find(event => event.tool === 'run_command').executedAt, undefined);
server.child.kill('SIGKILL');
await until(() => server.child.exitCode !== null || server.child.signalCode !== null);
const restarted = await launchWorkbench(t, f.data); f.onCleanup(restarted.close);
const recovered = await restarted.api('/sessions/' + session.id);
assert.equal(recovered.status, 'paused'); assert.ok(recovered.recoveryNotice);
assert.equal(recovered.approvals[0].status, 'interrupted');
assert.equal(recovered.changes[0].id, proposal.id); assert.equal(recovered.changes[0].status, 'pending');
assert.equal(recovered.events.find(event => event.tool === 'run_command').phase, 'interrupted');
await assert.rejects(restarted.api('/sessions/' + session.id + '/approvals/' + oldApproval.id, { allow: true }), { status: 409 });
await restarted.api('/sessions/' + session.id + '/resume', {});
await until(async () => { waiting = await restarted.api('/sessions/' + session.id); return waiting.status === 'awaiting_approval'; });
const freshApproval = waiting.approvals.find(item => item.status === 'pending');
assert.notEqual(freshApproval.id, oldApproval.id);
assert.equal(waiting.approvals.find(item => item.id === oldApproval.id).status, 'interrupted');
await restarted.api('/sessions/' + session.id + '/approvals/' + freshApproval.id, { allow: false });
await until(async () => { const current = await restarted.api('/sessions/' + session.id); return current.status === 'needs_review'; });
assert.equal(fs.existsSync(path.join(f.projectRoot, proposal.path)), false);
await restarted.close();
});
test('runtime diagnostics and model-response validation use actual records and preserve the saved connection', async t => {
const f = fixture(t), server = await launchWorkbench(t, f.data); f.onCleanup(server.close);
const model = await compatibleFixture(t, (url, input) => url === '/v1/models'
? { data: [{ id: 'saved-model' }] }
: { choices: [{ message: { role: 'assistant', content: input.model === 'tentative-model' ? 'OK' : 'Unexpected model' }, finish_reason: 'stop' }] });
await server.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'saved-model', apiKey: 'fixture-saved-connection' });
const testResult = await server.api('/config/test', { model: 'tentative-model', apiKey: 'fixture-tentative-connection' });
assert.equal(testResult.ok, true); assert.equal(model.requests[0].headers.authorization, 'Bearer fixture-tentative-connection');
const bootstrap = await (await fetch(server.url + '/api/bootstrap')).json();
assert.equal(bootstrap.config.model, 'saved-model'); assert.equal(bootstrap.config.hasKey, true);
const diagnostic = await server.api('/diagnostics?session=' + f.session.id);
assert.equal(diagnostic.version, bootstrap.version); assert.equal(diagnostic.session.project.root, f.project.root);
assert.equal(diagnostic.environment.encoding, 'UTF-8'); assert.equal(diagnostic.connection.lastRequest.status, 'completed');
assert.ok(!JSON.stringify(diagnostic).includes('fixture-saved-connection')); assert.ok(!JSON.stringify(diagnostic).includes('fixture-tentative-connection'));
await server.api('/config/models', {}); assert.equal(model.requests[1].headers.authorization, 'Bearer fixture-saved-connection');
});
test('persistent event replay is read-only and keeps run/artifact identity across reconnects', async t => {
const f = fixture(t), server = await launchWorkbench(t, f.data); f.onCleanup(server.close);
const cursor = server.bootstrap.eventCursor;
await server.api('/sessions/' + f.session.id + '/messages', { content: 'Use the controlled offline fixture' });
let waiting;
await until(async () => { waiting = await server.api('/sessions/' + f.session.id); return waiting.status === 'awaiting_approval'; }, 12000);
await server.api('/sessions/' + f.session.id + '/approvals/' + waiting.approvals[0].id, { allow: false });
let ended;
await until(async () => { ended = await server.api('/sessions/' + f.session.id); return ended.status === 'needs_review'; }, 12000);
const history = await server.api('/events/history?after=' + cursor + '&session=' + f.session.id + '&limit=3');
assert.equal(history.events.length, 3); assert.equal(history.hasMore, true);
assert.ok(history.events.every(value => value.taskId === ended.execution.task.id && value.runId === ended.execution.run.id));
const artifact = ended.execution.artifacts.find(value => value.kind === 'tool-result');
const result = await server.api('/artifacts/' + artifact.id + '?session=' + f.session.id);
assert.equal(result.sha256, artifact.sha256); assert.ok(result.content);
const other = await server.api('/sessions', { projectId: f.project.id });
await assert.rejects(server.api('/artifacts/' + artifact.id + '?session=' + other.id), { status: 404 });
const stop = new AbortController();
const stream = await fetch(server.url + '/api/events?token=' + encodeURIComponent(server.bootstrap.token) + '&after=' + cursor,
{ headers: { 'Last-Event-ID': String(history.nextCursor) }, signal: stop.signal });
const reader = stream.body.getReader(); const bytes = await reader.read(); const text = Buffer.from(bytes.value).toString('utf8');
stop.abort(); await reader.cancel().catch(() => {});
assert.match(text, /session_changed/); assert.match(text, /id: \d+/);
const replayedIds = [...text.matchAll(/^id: (\d+)$/gm)].map(match => Number(match[1]));
assert.ok(replayedIds.every(value => value > history.nextCursor));
const after = await server.api('/sessions/' + f.session.id);
assert.equal(after.runId, ended.runId); assert.equal(after.approvals.length, ended.approvals.length);
assert.deepEqual(after.changes, ended.changes); assert.equal(after.execution.run.toolCalls, ended.execution.run.toolCalls);
});
@@ -0,0 +1,96 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { fixture, launchWorkbench, until, compatibleFixture, call, completion, assertToolPairs, workspace } from './helpers.mjs';
import { createBrowserProject, browserSteps } from './fixtures/browser-project.mjs';
import { BrowserManager } from '../src/core/browser/browser-manager.mjs';
import { Store } from '../src/server/store.mjs';
test('real browser HTTP plans, original field pages and PNG bytes preserve ownership and survive host restart', async t => {
const f = createBrowserProject(fixture(t)); f.store.close(); let host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
assert.equal(host.bootstrap.capabilities.browserChecks, 'sealed-static-build-and-real-edge-assertions');
assert.equal((await host.api(route + '/browser')).availability.status, 'available');
let session = await host.api(route + '/browser-plans', { steps: browserSteps, viewport: { width: 390, height: 844 } });
const plan = session.execution.browserPlans.at(-1), other = await host.api('/sessions', { projectId: f.project.id });
assert.equal(plan.build.files, 2); assert.equal(session.execution.browserJobs.length, 0);
await assert.rejects(host.api('/sessions/' + other.id + '/browser-jobs', { planId: plan.id }), /当前工程Task|不属于/);
await host.api(route + '/browser-jobs', { planId: plan.id });
await until(async () => { session = await host.api(route); return session.execution.browserJobs.at(-1)?.finishedAt; }, 20000);
const job = session.execution.browserJobs.at(-1); assert.equal(job.status, 'passed', job.summary); assert.equal(job.cleanup.exited, true);
let text = '', offset = 0, page;
do { page = await host.api(route + '/browser-jobs/' + job.id + '/record?offset=' + offset + '&limit=1700'); text += page.content; offset += page.content.length; } while (page.truncated);
assert.equal(createHash('sha256').update(text).digest('hex'), job.resultSha256); const original = JSON.parse(text);
assert.equal(original.pages.at(-1).viewport.width, 390); assert.equal(original.pages.at(-1).viewport.scrollWidth <= 390, true);
const image = job.screenshots.at(-1), imageRoute = '/api' + route + '/browser-jobs/' + job.id + '/images/' + image.artifactId;
const response = await fetch(host.url + imageRoute, { headers: { 'X-Workbench-Token': host.bootstrap.token } });
assert.equal(response.headers.get('content-type'), 'image/png'); const bytes = Buffer.from(await response.arrayBuffer());
assert.equal(bytes.length, image.bytes); assert.equal(createHash('sha256').update(bytes).digest('hex'), image.sha256);
assert.equal((await fetch(host.url + imageRoute)).status, 403);
await assert.rejects(host.api('/sessions/' + other.id + '/browser-jobs/' + job.id + '/record'), /不属于/);
const duplicate = await host.api(route + '/browser-jobs', { planId: plan.id }); assert.equal(duplicate.execution.browserJobs.length, 1);
await host.close(); host = await launchWorkbench(t, f.data);
assert.deepEqual((await host.api(route)).execution.browserJobs.at(-1), job);
assert.equal((await host.api(route + '/browser-jobs/' + job.id + '/record?limit=1700')).content, text.slice(0, 1700));
assert.equal((await host.api(route)).execution.task.usage.requests, 0);
});
test('self harness browser tools require approval and feed verified actual page fields back to the original Task', async t => {
const f = createBrowserProject(fixture(t)); f.store.close(); let phase = 0;
const model = await compatibleFixture(t, (url, input) => {
if (!input?.messages) return { data: [{ id: 'browser-protocol-fixture' }] };
assert.ok(input.tools.some(tool => tool.function.name === 'run_browser'));
const last = input.messages.filter(value => value.role === 'tool').at(-1);
const latest = last ? JSON.parse(last.content) : null;
if (phase++ === 0) return completion([call('browser-fixture-plan', 'plan_browser', { steps: browserSteps })]);
if (phase === 2) return completion([call('browser-fixture-run', 'run_browser', { plan_id: latest.id })]);
if (phase === 3) { assert.equal(latest.browser_status, 'passed'); assert.equal(latest.evidence_verified, true); return completion([call('browser-fixture-read', 'read_browser_result', { browser_job_id: latest.browser_job_id, section: 'page', limit: 6000 })]); }
assert.ok(latest.content.includes('已完成')); return completion([], '已核对当前Task实际页面和原断言。');
});
const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'browser-protocol-fixture', apiKey: 'SYNTHETIC-BROWSER-QA-ONLY' });
await host.api(route + '/messages', { content: '请验证当前工程的填写、选择和状态切换,读取真实页面结果。' });
let session;
await until(async () => { session = await host.api(route); return session.status === 'awaiting_approval'; }, 15000);
const taskId = session.execution.task.id, approval = session.approvals.at(-1);
assert.ok(approval.browserPlanId); assert.equal(approval.checkPlanId, undefined); assert.equal(session.execution.browserJobs.length, 0);
assert.equal(approval.browserSummary.steps.length, browserSteps.length);
await host.api(route + '/approvals/' + approval.id, { allow: true });
await until(async () => { session = await host.api(route); return ['completed', 'failed', 'paused'].includes(session.status); }, 25000);
assert.equal(session.status, 'completed', session.failure?.message); assert.equal(session.execution.task.id, taskId);
assert.equal(session.execution.browserJobs.at(-1).status, 'passed'); assert.equal(session.execution.browserJobs.at(-1).taskId, taskId);
assert.equal(session.events.filter(value => value.kind === 'tool' && value.phase === 'completed').length, 3);
assertToolPairs((await host.api(route + '/export')).session.providerMessages);
});
test('browser host recovery retains unknown root outcome, blocks replay, and requires an explicit owned acknowledgement', async t => {
const f = createBrowserProject(fixture(t)), manager = new BrowserManager(f.store, { appDirectory: workspace });
const plan = await manager.plan(f.session, { steps: browserSteps }), now = new Date().toISOString();
const job = { id: '42424242-4242-4242-4242-424242424242', sessionId: f.session.id, projectId: f.project.id, taskId: f.session.taskId, runId: f.session.runId,
planId: plan.id, planFingerprint: plan.fingerprint, sourceFingerprint: plan.sourceFingerprint, buildSha256: plan.build.sha256,
status: 'running', browserDispatched: true, requiresVerification: false, startedAt: now, finishedAt: null, steps: [{ index: 0, status: 'passed', actual: 'before host exit' }], screenshots: [] };
f.store.putRecord('browser_job', f.session, job, 'browser.test_crash'); f.store.close();
const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); const session = reopened.session(f.session.id), restored = reopened.core.browser_job.get(job.id);
assert.equal(restored.status, 'unknown'); assert.equal(restored.requiresVerification, true); assert.equal(restored.steps[0].actual, 'before host exit');
const next = new BrowserManager(reopened, { appDirectory: workspace }); f.onCleanup(() => next.stopAll());
const newPlan = await next.plan(session, { steps: browserSteps }); assert.throws(() => next.start(session, newPlan.id), /原记录/);
assert.throws(() => next.acknowledge(session, restored.id, '0'.repeat(64)), /已经变化/);
next.acknowledge(session, restored.id, restored.planFingerprint); assert.equal(restored.status, 'unknown'); assert.equal(restored.requiresVerification, false);
const actual = await next.start(session, newPlan.id).promise; assert.equal(actual.status, 'passed', actual.summary); assert.equal(restored.status, 'unknown');
});
test('late HTTP stop of a finished browser job cannot interrupt a later job in the same Task', async t => {
const f = createBrowserProject(fixture(t)); f.store.close(); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
const planned = await host.api(route + '/browser-plans', { steps: [{ action: 'assert_count', selector: 'button', count: 1 }] });
await host.api(route + '/browser-jobs', { planId: planned.execution.browserPlans.at(-1).id }); let first;
await until(async () => { first = (await host.api(route)).execution.browserJobs.at(-1); return !!first.finishedAt; }, 20000); assert.equal(first.status, 'passed');
const next = await host.api(route + '/browser-plans', { steps: [{ action: 'assert_visible', selector: '#state' }, { action: 'assert_count', selector: '.not-present', count: 1 }] });
const started = await host.api(route + '/browser-jobs', { planId: next.execution.browserPlans.at(-1).id }), jobId = started.execution.browserJobs.at(-1).id;
await until(async () => (await host.api(route)).execution.browserJobs.at(-1).steps.length === 1, 15000);
const late = await host.api(route + '/browser-jobs/' + first.id + '/stop', {});
assert.notEqual(late.execution.browserJobs.at(-1).status, 'interrupted', 'Stale stop affected a later job.');
assert.equal(late.execution.browserJobs[0].status, 'passed');
await host.api(route + '/browser-jobs/' + jobId + '/stop', {});
const finished = (await host.api(route)).execution.browserJobs.at(-1);
assert.equal(finished.status, 'interrupted'); assert.equal(finished.cleanup.exited, true);
assert.equal((await host.api(route)).execution.task.usage.requests, 0);
});
@@ -0,0 +1,46 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, workspace } from './helpers.mjs';
import { createBrowserProject, browserSteps } from './fixtures/browser-project.mjs';
import { checkedDelivery, deliveryGit as git } from './fixtures/delivery-project.mjs';
import { BrowserManager } from '../src/core/browser/browser-manager.mjs';
import { DeliveryReporter } from '../src/core/delivery-report.mjs';
test('actual failed page blocks source-passed delivery; a new passing page permits a bound commit and preserves both original records', async t => {
const f = createBrowserProject(fixture(t), { beginTask: false }), { check, delivery } = await checkedDelivery(f);
const browser = new BrowserManager(f.store, { appDirectory: workspace }); f.onCleanup(() => browser.stopAll());
assert.equal(check.status, 'passed'); const parent = git(f, ['rev-parse', 'HEAD']);
const failedPlan = await browser.plan(f.session, { steps: [{ action: 'assert_count', selector: 'button', count: 2 }] });
await assert.rejects(delivery.plan(f.session), /实际浏览器断言通过/);
const failed = await browser.start(f.session, failedPlan.id).promise; assert.equal(failed.status, 'failed');
const reporter = new DeliveryReporter(f.store), initial = await reporter.create(f.session);
assert.equal(initial.report.status, 'needs_browser_checks'); assert.equal(initial.report.verifiedCheckJobId, check.id);
await assert.rejects(delivery.plan(f.session), /实际浏览器断言通过/); assert.equal(git(f, ['rev-parse', 'HEAD']), parent);
const passedPlan = await browser.plan(f.session, { steps: browserSteps }), passed = await browser.start(f.session, passedPlan.id).promise;
assert.equal(passed.status, 'passed', passed.summary); const commitPlan = await delivery.plan(f.session);
assert.equal(commitPlan.browserJobId, passed.id); assert.equal(commitPlan.browserBuildSha256, passed.buildSha256);
const committed = await (await delivery.start(f.session, commitPlan.id)).promise;
assert.equal(committed.status, 'completed', committed.summary); assert.equal(committed.indexPreserved, true);
const report = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(committed.reportArtifactId)));
assert.equal(report.status, 'verified_committed'); assert.equal(report.browserVerification.jobId, passed.id);
assert.deepEqual(report.browserChecks.map(value => value.status), ['failed', 'passed']);
assert.ok(report.browserChecks.every(value => value.evidenceVerified && value.cleanup.exited));
assert.match(f.store.artifactStore.read(f.store.core.artifact.get(committed.reportMarkdownArtifactId)), /页面验收/);
});
test('a build-only change after Git review invalidates the actual browser evidence and leaves HEAD and the protected index intact', async t => {
const f = createBrowserProject(fixture(t), { beginTask: false }), { delivery } = await checkedDelivery(f);
const browser = new BrowserManager(f.store, { appDirectory: workspace }); f.onCleanup(() => browser.stopAll());
const plan = await browser.plan(f.session, { steps: browserSteps }), passed = await browser.start(f.session, plan.id).promise;
assert.equal(passed.status, 'passed', passed.summary); const commitPlan = await delivery.plan(f.session);
const head = git(f, ['rev-parse', 'HEAD']), index = git(f, ['ls-files', '--stage', '-z']);
fs.appendFileSync(path.join(f.projectRoot, 'dist', 'index.html'), '\n<!-- build changed after review -->');
const job = await (await delivery.start(f.session, commitPlan.id)).promise;
assert.equal(job.status, 'stale', job.summary); assert.equal(job.commit, null);
assert.equal(git(f, ['rev-parse', 'HEAD']), head); assert.equal(git(f, ['ls-files', '--stage', '-z']), index);
const report = await new DeliveryReporter(f.store).create(f.session);
assert.equal(report.report.status, 'needs_browser_checks'); assert.equal(report.report.browserVerification.status, 'stale');
assert.equal(f.store.core.browser_job.get(passed.id).status, 'passed');
});
@@ -0,0 +1,54 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, call, assertToolPairs } from './helpers.mjs';
import { createBrowserProject } from './fixtures/browser-project.mjs';
import { BrowserManager } from '../src/core/browser/browser-manager.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { recordBrowserRead } from '../src/core/context/read-budget.mjs';
test('actual DOM controls include validated unique CSS selectors across repeated parent buttons and escaped labels', async t => {
const f = createBrowserProject(fixture(t)), manager = new BrowserManager(f.store); f.onCleanup(() => manager.stopAll());
const html = path.join(f.projectRoot, 'dist/index.html');
fs.appendFileSync(html, '<section><button>同类按钮一</button></section><section><button>同类按钮二</button></section><input aria-label="特殊&amp;标签&quot;一" id="field:special">');
const observation = await manager.plan(f.session, { steps: [] }), observed = await manager.start(f.session, observation.id).promise;
assert.equal(observed.status, 'observed');
const original = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(observed.resultArtifactId))), controls = original.pages.at(-1).controls;
assert.ok(controls.length >= 6); assert.ok(controls.every(control => control.selectorEvidence === 'observed-dom-query' && control.selectorMatches === 1 && control.selector.length <= 300));
assert.equal(new Set(controls.map(control => control.selector)).size, controls.length);
const exact = await manager.plan(f.session, { steps: controls.map(control => ({ action: 'assert_count', selector: control.selector, count: 1 })) });
assert.equal((await manager.start(f.session, exact.id).promise).status, 'passed');
const ambiguous = await manager.plan(f.session, { steps: [{ action: 'assert_visible', selector: 'button:nth-of-type(1)' }] });
const rejected = await manager.start(f.session, ambiguous.id).promise;
assert.equal(rejected.status, 'failed'); assert.match(rejected.summary, /strict mode violation/);
});
test('browser field coverage distinguishes sections and Run windows while retaining original source identity', () => {
const task = { activeRunId: 'run-one' }, page = { source_artifact_id: 'original-source', source_sha256: 'a'.repeat(64), view_sha256: 'b'.repeat(64), section: 'page', content: '原字段正文', offset: 0, next_offset: 5 };
for (let count = 0; count < 4; count++) recordBrowserRead(task, { ...page }, 'inv-' + count, 'run-one');
assert.equal(task.readProgress.repeated, 3); assert.equal(task.readProgress.pause, true);
recordBrowserRead(task, { ...page, section: 'steps' }, 'other-section', 'run-one'); assert.equal(task.readProgress.repeated, 0);
recordBrowserRead(task, { ...page }, 'new-run', 'run-two'); assert.equal(task.readProgress.repeated, 0);
assert.equal(task.readProgress.last.artifactId, 'original-source'); assert.equal(task.readProgress.last.sourceSha256, 'a'.repeat(64));
assert.equal(task.readProgress.reads.length, 2);
});
test('repeated original browser pages pause a live Task without re-opening the browser or breaking tool pairing', async t => {
const f = createBrowserProject(fixture(t)), browser = new BrowserManager(f.store); f.onCleanup(() => browser.stopAll());
const plan = await browser.plan(f.session, { steps: [{ action: 'assert_count', selector: 'button', count: 1 }] }), original = await browser.start(f.session, plan.id).promise;
assert.equal(original.status, 'passed');
const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
const execute = runtime.executeTool.bind(runtime);
runtime.executeTool = (session, invocation, run) => { run.readMessageBytes = 4096; return execute(session, invocation, run); };
let requests = 0;
provider.complete = async () => ({ message: { role: 'assistant', content: '', tool_calls: [call('browser-repeat-' + requests++, 'read_browser_result', { browser_job_id: original.id, section: 'page', limit: 1000 })] }, usage: { prompt_tokens: 100, completion_tokens: 20 } });
runtime.start(f.session.id, '读取本任务原页面字段'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'no_progress'); assert.equal(requests, 4);
assert.equal(f.store.core.browser_job.size, 1); assert.equal(f.store.core.browser_job.get(original.id).status, 'passed');
for (const message of f.session.providerMessages.filter(value => value.role === 'tool')) {
assert.ok(Buffer.byteLength(JSON.stringify(message), 'utf8') <= 4096, 'Original browser result plus repeat metadata must fit the actual next tool message.');
}
assertToolPairs(f.session.providerMessages);
});
@@ -0,0 +1,83 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { fixture, until, workspace } from './helpers.mjs';
import { Store } from '../src/server/store.mjs';
import { BrowserManager, verifyBrowserJob, currentBrowserVerification } from '../src/core/browser/browser-manager.mjs';
import { browserPlanInput } from '../src/shared/contracts/browser.mjs';
import { createBrowserProject, browserSteps } from './fixtures/browser-project.mjs';
const managerFor = f => { const manager = new BrowserManager(f.store, { appDirectory: workspace }); f.onCleanup(() => manager.stopAll()); return manager; };
test('browser plan rejects outside builds, invalid interaction scope and unbounded steps before execution', () => {
for (const input of [{ build_directory: '../project' }, { build_directory: 'D:/project' }, { path: '//example.com/' }, { viewport: { width: 200, height: 900 } },
{ steps: [{ action: 'evaluate', selector: 'body', value: 'process.exit()' }] }, { steps: [{ action: 'click', selector: 'body >> iframe' }] },
{ steps: [{ action: 'assert_count', selector: 'body', count: -1 }] }, { steps: Array.from({ length: 21 }, () => ({ action: 'click', selector: 'body' })) }]) assert.throws(() => browserPlanInput(input));
});
test('real owned Edge interactions, assertions, raw PNG and stopped process survive exact result reuse and host reopen', async t => {
const f = createBrowserProject(fixture(t)), manager = managerFor(f), plan = await manager.plan(f.session, { steps: browserSteps });
const before = fs.readFileSync(path.join(f.projectRoot, 'src', 'app.js'));
const first = manager.start(f.session, plan.id), job = await first.promise;
assert.equal(job.status, 'passed', job.summary); assert.equal(job.assertions.total, 3); assert.equal(job.assertions.passed, 3);
assert.equal(job.cleanup.exited, true); assert.equal(job.cleanup.snapshotDisposed, true); assert.ok(job.connection.pid > 0);
const original = verifyBrowserJob(f.store, f.session, job); assert.equal(original.steps[0].actual, '真实输入');
assert.equal(original.pages.at(-1).controls.find(control => control.id === 'filter').value, 'pending');
assert.ok(original.pages.at(-1).text.includes('已完成')); assert.equal(original.screenshots.length, 2); assert.deepEqual(original.pageErrors, []);
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'src', 'app.js')), before);
const repeated = manager.start(f.session, plan.id); assert.equal(repeated.reused, true); assert.equal((await repeated.promise).id, job.id); assert.equal(f.store.core.browser_job.size, 1);
assert.equal((await currentBrowserVerification(f.store, f.session)).status, 'passed');
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
const stored = reopened.session(f.session.id); assert.equal(verifyBrowserJob(reopened, stored, reopened.core.browser_job.get(job.id)).connection.pid, job.connection.pid);
assert.equal(reopened.core.browser_job.size, 1); assert.equal(stored.status, 'paused');
const image = reopened.owned('artifact', job.screenshots[0].artifactId, stored.id); assert.equal(reopened.artifactStore.readBytes(image).subarray(1, 4).toString(), 'PNG');
assert.throws(() => reopened.artifactStore.read(image), /图片工件/);
});
test('failed real page assertion preserves actual value and forbids silent retry of the original plan', async t => {
const f = createBrowserProject(fixture(t)), manager = managerFor(f), plan = await manager.plan(f.session, { steps: [{ action: 'assert_count', selector: 'button', count: 2 }] });
const job = await manager.start(f.session, plan.id).promise; assert.equal(job.status, 'failed', job.summary); assert.equal(job.cleanup.exited, true);
const original = verifyBrowserJob(f.store, f.session, job); assert.equal(original.steps[0].actual, 1); assert.equal(original.assertions.failed, 1);
assert.throws(() => manager.start(f.session, plan.id), /失败或中断/);
const repairPlan = await manager.plan(f.session, { steps: [{ action: 'assert_count', selector: 'button', count: 1 }] });
assert.equal((await manager.start(f.session, repairPlan.id).promise).status, 'passed');
assert.equal((await currentBrowserVerification(f.store, f.session)).status, 'passed'); assert.equal(f.store.core.browser_job.get(job.id).status, 'failed');
});
test('build/source mutation and sealed step tampering reject before browser dispatch; earlier task cannot execute plan', async t => {
const f = createBrowserProject(fixture(t)), manager = managerFor(f), plan = await manager.plan(f.session, { steps: browserSteps });
fs.appendFileSync(path.join(f.projectRoot, 'dist', 'app.js'), '\n// changed build');
const job = await manager.start(f.session, plan.id).promise; assert.equal(job.status, 'stale'); assert.equal(job.browserDispatched, false); assert.equal(job.connection, undefined);
const second = await manager.plan(f.session, { steps: browserSteps }); second.steps[0].value = 'tampered';
assert.throws(() => manager.start(f.session, second.id), /封存/);
f.session.status = 'completed'; f.store.save(f.session); f.store.beginRun(f.session, 'new task', false, f.store.config);
assert.throws(() => manager.planFor(f.session, plan.id), /此前Task/);
});
test('outside network never reaches another local service, and original JavaScript/console failures remain failures', async t => {
let requests = 0; const server = http.createServer((request, response) => { requests++; response.end('outside'); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); t.after(() => new Promise(resolve => server.close(resolve)));
const outside = 'http://127.0.0.1:' + server.address().port + '/private';
const f = createBrowserProject(fixture(t), { extra: `;console.error('真实页面错误');fetch(${JSON.stringify(outside)}).catch(()=>{});setTimeout(()=>{throw new Error('原脚本失败')},0);` });
const manager = managerFor(f), plan = await manager.plan(f.session, { steps: [{ action: 'assert_count', selector: 'button', count: 1 }] });
const job = await manager.start(f.session, plan.id).promise, original = verifyBrowserJob(f.store, f.session, job);
assert.equal(job.status, 'failed'); assert.equal(requests, 0); assert.equal(original.blockedRequests.length, 1);
assert.ok(original.console.some(item => item.text.includes('真实页面错误'))); assert.ok(original.pageErrors.some(item => item.message.includes('原脚本失败'))); assert.equal(original.assertions.passed, 1);
});
test('stopping a real in-flight browser preserves finished steps, closes its owned root and never replays', async t => {
const f = createBrowserProject(fixture(t)), manager = managerFor(f), plan = await manager.plan(f.session, { steps: [browserSteps[0], { action: 'assert_count', selector: '.absent', count: 1 }] });
const started = manager.start(f.session, plan.id); await until(() => started.job.steps.length === 1, 15000); await manager.stop(f.session.id);
const job = await started.promise; assert.equal(job.status, 'interrupted', job.summary); assert.equal(job.steps[0].actual, '真实输入'); assert.equal(job.cleanup.exited, true);
assert.equal(job.requiresVerification, false); assert.throws(() => manager.start(f.session, plan.id), /失败或中断/);
});
test('原PNG损坏使原结果校验失败,不能以已有通过状态复用', async t => {
const f = createBrowserProject(fixture(t)), manager = managerFor(f), plan = await manager.plan(f.session, { steps: [browserSteps[2]] });
const job = await manager.start(f.session, plan.id).promise; assert.equal(job.status, 'passed', job.summary);
const image = f.store.core.artifact.get(job.screenshots[0].artifactId); fs.writeFileSync(path.join(f.store.artifactStore.root, image.relative), 'corrupt');
assert.throws(() => verifyBrowserJob(f.store, f.session, job), /工件校验失败/); assert.equal((await currentBrowserVerification(f.store, f.session)).status, 'unverified');
await assert.rejects(manager.start(f.session, plan.id).promise, /工件校验失败/);
});
@@ -0,0 +1,159 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { PNG } from 'pngjs';
import { fixture, until, compatibleFixture, completion, call, assertToolPairs } from './helpers.mjs';
import { BrowsingManager } from '../src/core/browser/browsing-manager.mjs';
import { browsingUrl, browsingAction } from '../src/shared/contracts/browsing.mjs';
import { Store } from '../src/server/store.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
const image = PNG.sync.write(new PNG({ width: 2, height: 2 })).toString('base64');
const page = (revision = 'document:1', text = '原页面') => ({ observation: { revision, url: 'https://example.com/', title: '所属站点', text,
controls: [{ ref: 'e1', tag: 'button', type: 'submit', label: '确认保存', disabled: false }], scope: { complete: true } }, png: image });
function setup(t, options = {}) {
const f = fixture(t), run = f.store.beginRun(f.session, '核对网页操作', false, f.store.config); f.session.status = 'paused'; f.store.finishRun(f.session, run);
const m = new BrowsingManager(f.store, options), windowId = randomUUID(); m.poll(windowId); f.onCleanup(() => m.stopAll()); return { ...f, m, windowId };
}
async function respond(f, promise, result = page()) {
await until(() => f.m.active.size > 0); const command = f.m.poll(f.windowId).commands[0]; assert.ok(command);
assert.equal(f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result }).accepted, true);
return { result: await promise, command };
}
async function open(f, options = {}) { return (await respond(f, f.m.open(f.session, { url: 'https://example.com/', interact: true, approved: true, ...options }))).result; }
const operation = (f, tab, extra = {}) => ({ action: 'click', observationId: tab.observationId, revision: tab.revision, ref: 'e1', ...extra });
test('fixed browsing contracts reject local host APIs, credential URLs, protocols, cross-origin and arbitrary execution', () => {
for (const value of ['http://example.com','https://user:password@example.com','file:///C:/Windows/a','javascript:alert(1)','https://127.0.0.1/','https://localhost/','https://[::1]/']) assert.throws(() => browsingUrl(value));
assert.throws(() => browsingUrl('http://127.0.0.1:4318/', { previewOrigin: 'http://127.0.0.1:4318', workbenchOrigin: 'http://127.0.0.1:4318' }));
assert.throws(() => browsingUrl('https://other.example/', { origins: ['https://example.com'] }));
assert.throws(() => browsingAction({ action: 'click', observationId: randomUUID(), revision: 'r', ref: 'e1', javascript: 'arbitrary' }));
assert.throws(() => browsingAction({ action: 'wait', observationId: randomUUID(), revision: 'r', milliseconds: 2001 }));
});
test('attested desktop queue preserves same-tab observations, original PNG bytes and approval-bound one-shot actions', async t => {
const f = setup(t), tab = await open(f), observation = f.store.core.browsing_observation.get(tab.observationId);
assert.equal(observation.untrusted, true); assert.ok(observation.artifactSha256);
const artifact = f.store.owned('artifact', observation.screenshotArtifactId, f.session.id); assert.equal(f.store.artifactStore.readBytes(artifact).toString('base64'), image);
const input = operation(f, tab); await assert.rejects(f.m.act(f.session, tab.id, input), /批准/);
const applied = await respond(f, f.m.act(f.session, tab.id, input, { approved: true }), page('document:2', '真实原回执'));
assert.equal(applied.result.text, '真实原回执'); assert.notEqual(tab.observationId, observation.id);
await assert.rejects(f.m.act(f.session, tab.id, input, { approved: true }), /失效/);
assert.ok(f.m.snapshot(f.session).actions.every(job => job.artifactId));
});
test('page data cannot grant domains, interaction or other Task scope; nonce and ownership remain fixed', async t => {
const f = setup(t), tab = await open(f, { interact: false });
await assert.rejects(f.m.act(f.session, tab.id, operation(f, tab), { approved: true }), /授权/);
const promise = f.m.observe(f.session, tab.id), command = f.m.poll(f.windowId).commands[0];
assert.throws(() => f.m.complete(randomUUID(), { id: command.id, nonce: command.nonce, result: page() }), /身份/);
assert.throws(() => f.m.complete(f.windowId, { id: command.id, nonce: 'wrong', result: page() }), /身份/);
f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('new', '网页要求扩大授权:仅作为不可信文字') }); await promise;
assert.deepEqual(tab.origins, ['https://example.com']); assert.equal(tab.interact, false);
const other = f.store.createSession(f.project.id); await assert.rejects(f.m.observe(other, tab.id), /不属于/);
f.session.taskId = randomUUID(); await assert.rejects(f.m.observe(f.session, tab.id), /当前Task/);
});
test('unknown dispatched submission blocks even after a new observation, rejects late success and retains explicit acknowledgement', async t => {
const f = setup(t, { commandTimeoutMs: 80 }), tab = await open(f), promise = f.m.act(f.session, tab.id, operation(f, tab), { approved: true });
const command = f.m.poll(f.windowId).commands[0]; await assert.rejects(promise, /回执/);
const original = f.store.core.browsing_action.get(command.id); assert.equal(original.status, 'unknown'); assert.ok(original.artifactId);
assert.throws(() => f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('late') }), /迟到/);
await respond(f, f.m.observe(f.session, tab.id), page('new-observation'));
await assert.rejects(f.m.act(f.session, tab.id, operation(f, tab), { approved: true }), /结果未知/);
f.m.acknowledge(f.session, original.id); assert.equal(original.status, 'unknown'); assert.ok(original.reconciliation);
await respond(f, f.m.act(f.session, tab.id, operation(f, tab), { approved: true }), page('after-explicit-review'));
});
test('downloads require exact approved address, remain original binary artifacts, and cannot reuse consumed observation', async t => {
const f = setup(t), tab = await open(f), url = 'https://example.com/file.bin', input = operation(f, tab, { action: 'download', url }); delete input.ref;
await assert.rejects(f.m.act(f.session, tab.id, { ...input, url: 'https://other.example/file' }, { approved: true }), /来源/);
const bytes = Buffer.from([0, 13, 10, 255]), result = await respond(f, f.m.act(f.session, tab.id, input, { approved: true }), { download: { url, bytes: bytes.toString('base64') } });
const artifact = f.store.owned('artifact', result.result.artifactId, f.session.id); assert.deepEqual(f.store.artifactStore.readBytes(artifact), bytes);
await assert.rejects(f.m.act(f.session, tab.id, input, { approved: true }), /重复提交/);
});
test('revoking shared persistent profile closes all its tabs and clears only that profile after original desktop acknowledgement', async t => {
const f = setup(t), first = await open(f, { persistent: true }), second = await open(f, { profileId: first.profileId });
assert.equal(first.profileId, second.profileId); const third = await open(f);
await respond(f, f.m.close(f.session, first.id, { clear: true }), { cleared: true });
assert.equal(first.status, 'revoked'); assert.equal(second.status, 'revoked'); assert.equal(third.status, 'open');
assert.equal(f.store.core.browsing_profile.get(first.profileId).status, 'cleared');
await assert.rejects(f.m.observe(f.session, second.id), /撤销/);
});
test('service recovery never reopens old tabs or replays dispatched actions and keeps unknown original receipts', async t => {
const f = setup(t), tab = await open(f), job = { id: randomUUID(), sessionId: f.session.id, projectId: f.project.id, taskId: f.session.taskId, runId: f.session.runId,
tabId: tab.id, kind: 'act', status: 'dispatched', dispatchedAt: new Date().toISOString(), input: operation(f, tab) };
f.store.putRecord('browsing_action', f.session, job, 'browsing.fixture'); f.store.close();
const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
assert.equal(recovered.core.browsing_tab.get(tab.id).status, 'unavailable'); assert.equal(recovered.core.browsing_action.get(job.id).status, 'unknown');
const next = new BrowsingManager(recovered); assert.equal(next.active.size, 0); assert.equal(next.poll(randomUUID()).commands.length, 0);
});
test('agent browser flow waits for original Task/Run approval, dispatches once, and returns paired original results', async t => {
const f = setup(t), tab = await open(f), taskId = f.session.taskId;
const upstream = await compatibleFixture(t, (_route, input, requests) => {
if (requests.length === 1) return completion([call('browse-observe', 'observe_browser_tab', { tab_id: tab.id })]);
if (requests.length === 2) {
const original = JSON.parse(input.messages.findLast(value => value.tool_call_id === 'browse-observe').content);
return completion([call('browse-act', 'act_browser_tab', { tab_id: tab.id, action: 'click', observationId: original.id, revision: original.revision, ref: 'e1' })]);
}
return completion([], '原页面已保存,结果来自本次原动作回执。');
});
f.store.config = { ...f.store.config, mode: 'compatible', baseUrl: upstream.baseUrl, model: 'declared-browser-protocol-fixture' };
const runtime = new Runtime(f.store, new Provider()); runtime.browsing = f.m; f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, '继续核对本任务页面,操作需要原审批', true);
const running = runtime.active.get(f.session.id).promise;
await until(() => [...f.m.active.values()].some(value => value.job.kind === 'observe'));
const observe = f.m.poll(f.windowId).commands[0];
f.m.complete(f.windowId, { id: observe.id, nonce: observe.nonce, result: page('agent:1', '模型读取的原页面') });
await until(() => f.session.approvals.some(value => value.browsingTabId === tab.id && value.status === 'pending'));
const approval = f.session.approvals.find(value => value.browsingTabId === tab.id && value.status === 'pending');
assert.equal(f.session.taskId, taskId); assert.equal(approval.taskId, taskId); assert.equal(approval.runId, f.session.runId);
assert.equal([...f.store.core.browsing_action.values()].filter(value => value.kind === 'act').length, 0);
assert.equal(f.m.poll(f.windowId).commands.length, 0);
runtime.decide(f.session.id, approval.id, true);
await until(() => [...f.m.active.values()].some(value => value.job.kind === 'act'));
const command = f.m.poll(f.windowId).commands[0]; assert.deepEqual(command.input, approval.browsingInput);
assert.equal(f.m.poll(f.windowId).commands.length, 0);
f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('agent:2', '来自原点击的保存结果') });
await running;
const action = f.store.core.browsing_action.get(command.id);
assert.equal(action.approvalId, approval.id); assert.equal(action.source, 'agent'); assert.equal(action.status, 'completed');
assert.equal(action.taskId, taskId); assert.equal(action.runId, approval.runId); assert.ok(action.artifactId);
assertToolPairs(f.session.providerMessages); assert.equal(upstream.requests.length, 3);
assert.throws(() => runtime.decide(f.session.id, approval.id, true), /过期/);
});
test('changed approved input and stopped dispatched agent action cannot be accepted or replayed', async t => {
const f = setup(t), tab = await open(f), input = operation(f, tab);
const approval = { id: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, browsingTabId: tab.id, browsingInput: { ...input }, status: 'approved' };
f.session.approvals.push(approval);
await assert.rejects(f.m.act(f.session, tab.id, { ...input, action: 'reload' }, { approved: true, approvalId: approval.id }), /审批/);
const controller = new AbortController(), promise = f.m.act(f.session, tab.id, input, { approved: true, approvalId: approval.id, signal: controller.signal });
const command = f.m.poll(f.windowId).commands[0]; controller.abort(); await assert.rejects(promise, /停止/);
assert.equal(f.store.core.browsing_action.get(command.id).status, 'unknown');
assert.throws(() => f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('late') }), /迟到/);
await assert.rejects(f.m.act(f.session, tab.id, input, { approved: true, approvalId: approval.id }), /结果未知/);
});
test('browser inventory gives bounded metadata and original references instead of repeating full website histories', async t => {
const f = setup(t), tab = await open(f);
await respond(f, f.m.observe(f.session, tab.id), page('long:1', '网站原文'.repeat(3500)));
const value = f.m.toolSnapshot(f.session), observation = value.observations.at(-1);
assert.equal(observation.artifactId, f.store.core.browsing_observation.get(tab.observationId).artifactId);
assert.equal(observation.text, undefined); assert.equal(value.profiles, undefined); assert.equal(value.tabs[0].windowId, undefined);
assert.ok(JSON.stringify(value).length < 8000);
});
test('receipt storage failure settles the original dispatched action as unknown and never hangs or accepts late success', async t => {
const f=setup(t),tab=await open(f),promise=f.m.act(f.session,tab.id,operation(f,tab),{approved:true});
const command=f.m.poll(f.windowId).commands[0],write=f.m.artifact.bind(f.m);
f.m.artifact=(...args)=>{if(args[3]==='browsing-action-receipt')throw new Error('declared browser receipt storage failure');return write(...args);};
assert.equal(f.m.complete(f.windowId,{id:command.id,nonce:command.nonce,result:page('storage:2')}).accepted,false);
await assert.rejects(promise,/receipt storage failure/);const original=f.store.core.browsing_action.get(command.id);
assert.equal(original.status,'unknown');assert.equal(original.requiresVerification,true);assert.match(original.evidenceError,/storage failure/);assert.equal(f.m.active.size,0);
assert.throws(()=>f.m.complete(f.windowId,{id:command.id,nonce:command.nonce,result:page('late')}),/迟到/);
});
@@ -0,0 +1,31 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { budgetPausePresentation } from '../src/shared/budget-presentation.mjs';
test('input that fits the engine window but exceeds remaining credit diagnoses the credit limit', () => {
const pause = { reason: 'engine_input_budget', limitingReason: 'remaining_token_budget', requiredInputTokens: 27424,
inputBudget: 27552, remainingTokens: 28339, tokenReservation: { reservedTokens: 29600 } };
const diagnosis = budgetPausePresentation(pause);
assert.equal(diagnosis.title, '原生引擎剩余预算不足');
assert.match(diagnosis.guidance, /输入可容纳/); assert.match(diagnosis.guidance, /预留超过剩余预算/);
assert.ok(pause.requiredInputTokens < pause.inputBudget); assert.ok(pause.tokenReservation.reservedTokens > pause.remainingTokens);
});
test('window overflow and historical pauses retain distinct and honest diagnoses', () => {
assert.equal(budgetPausePresentation({ reason: 'engine_input_budget', limitingReason: 'context_limit' }).title, '原生引擎上下文窗口不足');
assert.equal(budgetPausePresentation({ reason: 'engine_input_budget' }).title, '原生引擎输入或剩余预算不足');
assert.equal(budgetPausePresentation(undefined, 'task_token_limit').title, '任务累计预算不足');
assert.equal(budgetPausePresentation(undefined, 'token_limit').title, '本次运行预算不足');
assert.equal(budgetPausePresentation(undefined, 'change_set_review'), null);
});
test('R observed reservation shortage keeps the window-fit diagnosis separate from Run/Task credit', () => {
const pause = { reason: 'token_limit', requiredInputTokens: 12352, inputBudget: 12352,
remainingTokens: 17429, tokenReservation: { reservedTokens: 29623 } };
assert.equal(budgetPausePresentation(pause).title, '下一次请求超出运行余额');
assert.match(budgetPausePresentation(pause).guidance, /输入可容纳/);
assert.equal(budgetPausePresentation({ ...pause, reason: 'task_token_limit' }).title, '下一次请求超出任务余额');
const withoutWindow = budgetPausePresentation({ reason: 'token_limit', pauseKind: 'request_reservation' });
assert.match(withoutWindow.guidance, /不补造输入结论/);
assert.equal(budgetPausePresentation({ reason: 'token_limit', requiredInputTokens: 100, inputBudget: 100 }).title, '本次运行预算不足');
});
@@ -0,0 +1,63 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { fixture,compatibleFixture,completion,until,call } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
async function paused(t){
const f=fixture(t),server=await compatibleFixture(t,(_url,body)=>body.messages.some(row=>row.role==='tool')?completion([],'Finished from the original result'):completion([call('original-files','list_files',{path:'',depth:1})]));
const provider=new Provider(),hub=provider.modelHub(f.store),connection=hub.upsert({name:'Owned capacity',baseUrl:server.baseUrl,model:'owned',contextLimit:64000,requiresKey:false});
hub.select({type:'connection',id:connection.id});f.store.saveConfig({...f.store.config,maxSteps:1});
const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());runtime.start(f.session.id,'Inspect the project, preserving all requirements.');await until(()=>!runtime.isActive(f.session.id));
assert.equal(f.session.status,'paused');assert.equal(f.store.core.tool.size,1);const taskId=f.session.taskId;
f.store.saveConfig({...f.store.config,contextLimit:1000});runtime.resume(f.session.id,'');await until(()=>!runtime.isActive(f.session.id));
assert.equal(f.session.taskId,taskId);assert.equal(f.session.status,'paused');assert.equal(server.requests.length,1);
return {...f,server,provider,hub,connection,runtime};
}
const prepare=f=>f.runtime.budgetResumes().prepare(f.session,{contextLimit:64000,maxRunTokens:200000,maxSteps:3});
test('a budget pause persists concrete text/tool/history/image breakdown and never dispatches an oversized request',async t=>{
const f=await paused(t),run=f.store.core.run.get(f.session.runId),pause=run.budgetPause;
assert.equal(pause.reason,'context_limit');assert.equal(pause.contextLimit,1000);assert.ok(pause.requiredInputTokens>pause.inputBudget);
assert.equal(Object.values(pause.breakdown).reduce((sum,value)=>sum+value,0),pause.requiredInputTokens);
assert.ok(pause.breakdown.toolDefinitionTokens>0);assert.equal(pause.imageReservationTokens,0);assert.equal(f.server.requests.length,1);
const rows=f.store.storage.load().records.filter(row=>row.kind==='run');assert.equal(rows.at(-1).value.budgetPause.requiredInputTokens,pause.requiredInputTokens);
});
test('explicit frozen preview resumes the same Task in a new Run, preserves completed tools and does not change global config or selected model',async t=>{
const f=await paused(t),before=structuredClone(f.store.config),selected=structuredClone(f.hub.state.selected),taskId=f.session.taskId,oldRun=f.session.runId;
const plan=prepare(f);assert.equal(f.server.requests.length,1);assert.equal(plan.completedToolIds.length,1);assert.equal(plan.taskId,taskId);
f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256});await until(()=>!f.runtime.isActive(f.session.id));
assert.equal(f.session.status,'completed',JSON.stringify(f.session.messages));assert.equal(f.session.taskId,taskId);assert.notEqual(f.session.runId,oldRun);
assert.equal(f.store.core.run.get(f.session.runId).resumeOf,oldRun);assert.equal(f.store.core.run.get(f.session.runId).resumePlanId,plan.id);
assert.equal(f.store.core.tool.size,1);assert.equal(f.server.requests.length,2);assert.equal(f.store.core.resume_plan.get(plan.id).status,'consumed');
assert.deepEqual(f.store.config,before);assert.deepEqual(f.hub.state.selected,selected);
assert.throws(()=>f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256}));
});
test('insufficient budgets, silently clamped capacity and arbitrary config fields reject without a physical request or mutating task budget',async t=>{
const f=await paused(t),before=structuredClone(f.store.core.task.get(f.session.taskId).budget);
for(const input of [{contextLimit:200000},{maxRunTokens:1000},{maxTaskTokens:1000},{apiKey:'forbidden'},{contextLimit:64000.5},{selection:{type:'connection',id:'missing'}}])assert.throws(()=>f.runtime.budgetResumes().prepare(f.session,input));
assert.equal(f.server.requests.length,1);assert.equal(f.store.core.resume_plan.size,0);assert.deepEqual(f.store.core.task.get(f.session.taskId).budget,before);
});
test('changed session, changed registry, expiry and altered frozen preview all require a new explicit preview',async t=>{
const f=await paused(t);let plan=prepare(f);f.store.message(f.session,'system','Owned review changed the revision');
assert.throws(()=>f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256}),/已变化/);
plan=prepare(f);f.hub.upsert({...f.hub.connection(f.connection.id),name:'Revised'});
assert.throws(()=>f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256}),/已变化/);
plan=prepare(f);f.store.core.resume_plan.get(plan.id).config.contextLimit=200000;
assert.throws(()=>f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256}),/已变化/);
plan=prepare(f);f.store.core.resume_plan.get(plan.id).expiresAt=new Date(0).toISOString();
assert.throws(()=>f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256}),/已变化/);
assert.equal(f.server.requests.length,1);
});
test('preview preserves unknown invocation status and increasing Task allowance is committed only with confirmed new Run',async t=>{
const f=await paused(t),tool=[...f.store.core.tool.values()][0];tool.requiresVerification=true;f.store.save(f.session);
const task=f.store.core.task.get(f.session.taskId),before=task.budget.maxTokens;
const plan=f.runtime.budgetResumes().prepare(f.session,{contextLimit:64000,maxTaskTokens:before+100000,maxSteps:3});
assert.deepEqual(plan.unknownToolIds,[tool.id]);assert.equal(task.budget.maxTokens,before);
f.runtime.resumeBudget(f.session.id,{planId:plan.id,sha256:plan.sha256});await until(()=>!f.runtime.isActive(f.session.id));
assert.equal(task.budget.maxTokens,before+100000);assert.equal(tool.requiresVerification,true);assert.equal(f.store.core.tool.size,1);
});
@@ -0,0 +1,148 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, launchWorkbench, until, compatibleFixture, call, completion, assertToolPairs } from './helpers.mjs';
import { batchFixture, assertBatchBytes } from './fixtures/change-set-project.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
test('batch APIs enforce ownership and review fingerprints, apply once, and persist the completed batch across restart', { timeout: 30000 }, async t => {
const f = fixture(t), b = batchFixture(f); f.store.close();
const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
const first = await host.api(route + '/change-sets', { changeIds: b.ids }), set = first.execution.changeSets.at(-1);
assert.equal((await host.api(route + '/change-sets', { changeIds: b.ids })).execution.changeSets.at(-1).id, set.id);
assertBatchBytes(assert, f, b.before);
await assert.rejects(host.api(route + '/change-sets/' + set.id + '/apply', {}), /指纹/);
await assert.rejects(host.api(route + '/changes/' + b.ids[0] + '/apply', {}), /封存/);
await assert.rejects(host.api(route + '/messages', { content: 'different task' }), /变更集/);
const other = await host.api('/sessions', { projectId: f.project.id });
await assert.rejects(host.api('/sessions/' + other.id + '/change-sets/' + set.id), /不属于/);
await host.api(route + '/change-sets/' + set.id + '/apply', { fingerprint: set.fingerprint });
let applied;
await until(async () => { applied = await host.api(route); return applied.execution.changeSets.at(-1).status === 'completed'; });
assertBatchBytes(assert, f, b.after);
await assert.rejects(host.api(route + '/change-sets/' + set.id + '/apply', { fingerprint: set.fingerprint }), /重复应用/);
await host.close();
const restarted = await launchWorkbench(t, f.data), restored = await restarted.api(route);
assert.deepEqual(restored.execution.changeSets, applied.execution.changeSets); assertBatchBytes(assert, f, b.after);
});
test('delivery reports bind actual batch identity and reject corrupted snapshot evidence without changing project files', { timeout: 30000 }, async t => {
const f = fixture(t), b = batchFixture(f), set = await b.manager.prepare(f.session, b.ids);
await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
const snapshot = f.store.owned('artifact', set.files[1].snapshotArtifactId, f.session.id), snapshotPath = path.join(f.store.artifactStore.root, snapshot.relative);
f.store.close(); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
const valid = await host.api(route + '/delivery-reports', {}), jsonArtifact = valid.execution.artifacts.slice().reverse().find(item => item.kind === 'delivery-report');
const preview = await host.api('/artifacts/' + jsonArtifact.id + '?session=' + f.session.id + '&limit=16000'), report = JSON.parse(preview.content);
assert.equal(report.changeSets[0].id, set.id); assert.equal(report.changeSets[0].snapshotVerified, true);
assert.ok(report.changes.every(change => change.changeSetId === set.id)); assert.equal(report.status, 'needs_checks');
fs.writeFileSync(snapshotPath, 'Corrupt owned QA snapshot');
const invalid = await host.api(route + '/delivery-reports', {}), artifact = invalid.execution.artifacts.slice().reverse().find(item => item.kind === 'delivery-report');
const invalidReport = JSON.parse((await host.api('/artifacts/' + artifact.id + '?session=' + f.session.id + '&limit=16000')).content);
assert.equal(invalidReport.status, 'needs_verification'); assert.equal(invalidReport.changeSets[0].snapshotVerified, false); assertBatchBytes(assert, f, b.after);
});
test('a compatible model can seal proposals through the real HTTP loop while application stays behind user review', { timeout: 60000 }, async t => {
const f = fixture(t); f.store.close(); let count = 0;
const provider = await compatibleFixture(t, (url, input) => {
if (!input?.messages) return { data: [{ id: 'fixture-model' }] };
count++;
if (count === 1) return completion([
call('first-file', 'propose_file', { path: 'src/new.mjs', content: 'export const newValue = 1;\n', reason: '新增模块' }),
call('second-file', 'propose_file', { path: 'docs/new.md', content: '# New\n', reason: '补充说明' }),
], '准备两个文件提案。');
if (count === 2) {
const ids = input.messages.filter(message => message.role === 'tool').map(message => JSON.parse(message.content).change_id).filter(Boolean);
return completion([call('seal-batch', 'prepare_change_set', { change_ids: ids })], '封存整批审查范围。');
}
return completion([], '文件批次已封存,等待用户审查应用。');
});
const host = await launchWorkbench(t, f.data);
await host.api('/config', { mode: 'compatible', baseUrl: provider.baseUrl, model: 'fixture-model', apiKey: 'SYNTHETIC-QA-ONLY' });
await host.api('/sessions/' + f.session.id + '/messages', { content: '创建两个文件并组合审查' });
let session;
await until(async () => { session = await host.api('/sessions/' + f.session.id); return session.status === 'needs_review'; }, 30000);
assert.equal(session.execution.changeSets.length, 1); assert.equal(session.execution.changeSets[0].status, 'prepared');
assert.equal(session.execution.changeSets[0].files.length, 2); assert.equal(fs.existsSync(path.join(f.projectRoot, 'src')), false);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'docs')), false);
assert.equal(session.events.find(event => event.tool === 'prepare_change_set').phase, 'completed');
assert.equal(count, 2); assert.equal(session.execution.run.stopReason, 'change_set_review');
const tools = provider.requests.find(request => request.input?.tools)?.input.tools.map(tool => tool.function.name);
assert.ok(tools.includes('prepare_change_set')); assert.ok(!tools.includes('apply_change_set')); assert.ok(!tools.includes('rollback_change_set'));
});
test('a sealed batch at the step limit hands off and continues the same Task into actual checks without replaying saved tools', { timeout: 60000 }, async t => {
const f = fixture(t);
fs.mkdirSync(path.join(f.projectRoot, 'test'));
const acceptance = "import test from 'node:test'; import assert from 'node:assert/strict'; import { left } from '../src/left.mjs'; import { right } from '../src/right.mjs';\ntest('left value', () => assert.equal(left, 1)); test('right value', () => assert.equal(right, 2));\n";
fs.writeFileSync(path.join(f.projectRoot, 'test/values.test.mjs'), acceptance);
fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ type: 'module', scripts: { test: 'node --test test/*.test.mjs' } }));
f.store.close(); let count = 0;
const model = await compatibleFixture(t, (url, input) => {
if (!input?.messages) return { data: [{ id: 'batch-continuation-fixture' }] };
count++;
if (count === 1) return completion([
call('paused-left', 'propose_file', { path: 'src/left.mjs', content: 'export const left = 1;\n', reason: '第一份受控模块' }),
call('paused-right', 'propose_file', { path: 'src/right.mjs', content: 'export const right = 2;\n', reason: '第二份受控模块' }),
]);
const results = input.messages.filter(message => message.role === 'tool').map(message => JSON.parse(message.content));
if (count === 2) return completion([call('paused-seal', 'prepare_change_set', { change_ids: results.map(result => result.change_id).filter(Boolean) })]);
if (count === 3) return completion([call('continued-check-plan', 'plan_checks', { names: ['test'] })]);
if (count === 4) return completion([call('continued-actual-check', 'run_checks', { plan_id: results.findLast(result => result.checks && result.fingerprint).id })]);
return completion([], '原任务的两份文件已通过真实工程检查。');
});
const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'batch-continuation-fixture', maxSteps: 2 });
await host.api(route + '/messages', { content: '完成原始双模块工程目标并运行测试' });
let paused;
await until(async () => { paused = await host.api(route); return paused.status === 'needs_review'; }, 30000);
assert.equal(paused.execution.run.stopReason, 'change_set_review'); assert.equal(count, 2);
const set = paused.execution.changeSets.at(-1), firstRun = structuredClone(paused.execution.run);
assert.equal(set.status, 'prepared'); assert.equal(fs.existsSync(path.join(f.projectRoot, 'src')), false);
const savedMessages = (await host.api(route + '/export')).session.providerMessages;
const savedResults = savedMessages.filter(message => message.role === 'tool'); assert.equal(savedResults.length, 3);
await assert.rejects(host.api(route + '/resume', {}), { status: 409, message: /请先审查待处理改动/ });
await assert.rejects(host.api(route + '/continue-reviewed', {}), { status: 409 });
await host.api(route + '/change-sets/' + set.id + '/apply', { fingerprint: set.fingerprint });
await until(async () => (await host.api(route)).execution.changeSets.at(-1).status === 'completed', 30000);
await host.api('/config', { ...(await host.api('/bootstrap')).config, maxSteps: 5 });
await host.api(route + '/continue-reviewed', {});
let waiting;
await until(async () => { waiting = await host.api(route); return waiting.approvals.some(approval => approval.status === 'pending'); }, 30000);
await host.api(route + '/approvals/' + waiting.approvals.find(approval => approval.status === 'pending').id, { allow: true });
let completed;
await until(async () => { completed = await host.api(route); return completed.status === 'completed'; }, 30000);
assert.equal(completed.execution.task.id, paused.execution.task.id);
assert.equal(completed.execution.task.goal, '完成原始双模块工程目标并运行测试');
assert.equal(completed.execution.run.resumeOf, firstRun.id); assert.equal(completed.execution.task.verification.status, 'passed');
assert.equal(completed.execution.checks.length, 1); assert.equal(completed.execution.checks[0].results[0].tests.passed, 2);
assert.equal(completed.execution.runs.find(run => run.id === firstRun.id).status, 'needs_review');
assert.deepEqual(completed.execution.runs.find(run => run.id === firstRun.id).usage, firstRun.usage);
assert.equal(completed.execution.changeSets[0].runId, firstRun.id); assert.equal(count, 5);
const exported = (await host.api(route + '/export')).session.providerMessages;
assert.deepEqual(exported.filter(message => message.role === 'tool').slice(0, 3), savedResults); assertToolPairs(exported);
assert.equal(completed.events.filter(event => event.kind === 'tool' && ['propose_file', 'prepare_change_set'].includes(event.tool)).length, 3);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'test/values.test.mjs'), 'utf8'), acceptance);
await assert.rejects(host.api(route + '/continue-reviewed', {}), { status: 409 });
});
test('applied batches cannot turn cancellation, other budgets, restart or unknown commands into a review continuation', async t => {
const f = fixture(t), b = batchFixture(f), set = await b.manager.prepare(f.session, b.ids);
await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
const runtime = new Runtime(f.store, new Provider()), run = f.store.core.run.get(f.session.runId);
for (const reason of ['cancelled', 'time_limit', 'token_limit', 'task_token_limit', 'restart']) {
run.status = 'paused'; run.stopReason = reason;
const before = JSON.stringify(f.store.publicSession(f.session));
assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 });
assert.equal(JSON.stringify(f.store.publicSession(f.session)), before); assertBatchBytes(assert, f, b.after);
}
const invocation = call('unknown-command', 'run_command', { command: 'echo owned-fixture', reason: 'Unknown result must be reviewed' });
const event = f.store.event(f.session, { kind: 'tool', tool: 'run_command', toolCallId: invocation.id, phase: 'running' });
f.store.beginInvocation(f.session, invocation, run, event); f.store.invocationState(f.session, event, 'running');
f.store.interruptPending(f.session, run); f.session.status = 'paused'; f.store.finishRun(f.session, run, 'step_limit');
const before = JSON.stringify(f.store.publicSession(f.session));
assert.equal(f.store.publicSession(f.session).execution.progress.requiresVerification.length, 1);
assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 });
assert.equal(JSON.stringify(f.store.publicSession(f.session)), before); assertBatchBytes(assert, f, b.after);
});
@@ -0,0 +1,71 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { fixture, call, assertToolPairs } from './helpers.mjs';
async function exercise(t, mode) {
const f = fixture(t), provider = new Provider(), runtime = new Runtime(f.store, provider);
f.onCleanup(() => runtime.stopAll());
f.store.saveConfig({ maxSteps: 2 });
let requests = 0, stopped, stopScheduled = false;
provider.complete = async () => {
requests++;
const calls = requests === 1 ? [
call('left', 'propose_file', { path: 'left.mjs', content: 'export const left = 1;\n', reason: 'Left module' }),
call('right', 'propose_file', { path: 'right.mjs', content: 'export const right = 2;\n', reason: 'Right module' }),
] : [call('seal', 'prepare_change_set', { change_ids: mode === 'failed' ? ['missing']
: f.session.changes.slice(0, mode === 'partial' ? 1 : 2).map(change => change.id) }),
...(mode === 'new-proposal' ? [call('third', 'propose_file', { path: 'third.mjs', content: 'export const third = 3;\n', reason: 'Later same-turn proposal' })]
: [call('after-seal', 'inspect_session', {})]),
...(mode === 'later-error' ? [call('bad-read', 'read_file', { path: 'missing.mjs' })] : []),
];
return { message: { role: 'assistant', content: 'Declared model fixture.', tool_calls: calls }, usage: null };
};
f.store.on('update', () => {
if (mode === 'cancelled' && !stopScheduled && f.session.events.some(event => event.toolCallId === 'seal' && event.phase === 'completed')) {
stopScheduled = true; setImmediate(() => { stopped = runtime.stop(f.session.id); });
}
if (mode === 'conflict' && f.session.events.some(event => event.toolCallId === 'after-seal' && event.phase === 'completed')) {
fs.writeFileSync(path.join(f.projectRoot, 'left.mjs'), 'Concurrent owner bytes\n');
}
if (mode === 'unknown') {
const event = f.session.events.find(event => event.toolCallId === 'after-seal' && event.phase === 'completed');
if (event) f.store.core.tool.get(event.invocationId).requiresVerification = true;
}
if (mode === 'tampered') {
const set = [...f.store.core.change_set.values()][0];
if (set) set.fingerprint = '0'.repeat(64);
}
});
runtime.start(f.session.id, 'Create and review two modules; do not apply them automatically.');
await runtime.active.get(f.session.id).promise;
if (mode === 'cancelled') { await new Promise(resolve => setImmediate(resolve)); await stopped; }
const run = f.store.core.run.get(f.session.runId);
assert.equal(requests, 2); assertToolPairs(f.session.providerMessages);
assert.equal([...f.store.core.tool.values()].filter(tool => tool.sessionId === f.session.id).length, mode === 'later-error' ? 5 : 4);
assert.ok(f.session.changes.every(change => change.status === 'pending'));
assert.equal(fs.existsSync(path.join(f.projectRoot, 'right.mjs')), false);
if (mode === 'conflict') assert.equal(fs.readFileSync(path.join(f.projectRoot, 'left.mjs'), 'utf8'), 'Concurrent owner bytes\n');
else assert.equal(fs.existsSync(path.join(f.projectRoot, 'left.mjs')), false);
return { ...f, run };
}
test('complete sealed batch hands off after all tools settle with no next model request', async t => {
for (const mode of ['complete', 'later-error']) await t.test(mode, async t => {
const f = await exercise(t, mode);
assert.equal(f.session.status, 'needs_review'); assert.equal(f.run.stopReason, 'change_set_review');
assert.equal(f.session.events.find(event => event.toolCallId === 'after-seal').phase, 'completed');
if (mode === 'later-error') assert.equal(f.session.events.find(event => event.toolCallId === 'bad-read').phase, 'failed');
});
});
test('partial, changed, failed or cancelled batches cannot claim complete review handoff', async t => {
for (const mode of ['partial', 'new-proposal', 'failed', 'conflict', 'cancelled', 'unknown', 'tampered']) await t.test(mode, async t => {
const f = await exercise(t, mode);
assert.equal(f.session.status, 'paused'); assert.equal(f.run.stopReason, mode === 'cancelled' ? 'cancelled' : 'step_limit');
if (mode !== 'failed' && mode !== 'cancelled') assert.equal([...f.store.core.change_set.values()][0].status, 'prepared');
});
});
@@ -0,0 +1,42 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { fixture, until, workspace } from './helpers.mjs';
import { batchFixture, assertBatchBytes } from './fixtures/change-set-project.mjs';
import { Store } from '../src/server/store.mjs';
import { ChangeSetManager } from '../src/core/workspaces/change-sets.mjs';
for (const phase of ['prepared', 'application_started', 'write_intent', 'staged', 'replaced', 'file_recorded', 'finished',
'rollback_started', 'restore_intent', 'restore_staged', 'restored', 'restore_recorded', 'rollback_finished']) {
test('actual process exit at file-batch ' + phase + ' preserves durable identity and never replays writes', { timeout: 30000 }, async t => {
const f = fixture(t), b = batchFixture(f);
let set = phase === 'prepared' ? null : await b.manager.prepare(f.session, b.ids);
const direction = phase === 'prepared' ? 'prepare' : phase.startsWith('restore') || phase.startsWith('rollback') ? 'rollback' : 'apply';
if (direction === 'rollback') await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
f.store.close();
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/change-set-crash-worker.mjs'), f.data, f.session.id,
set?.id ?? '-', phase, direction, JSON.stringify(b.ids)], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null, 25000); assert.equal(child.exitCode, 73, errors);
const cut = JSON.parse(output.trim()), beforeRestart = {};
for (const relative of Object.keys(b.before)) { const target = path.join(f.projectRoot, relative); beforeRestart[relative] = fs.existsSync(target) ? fs.readFileSync(target) : null; }
const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
const session = recovered.session(f.session.id), manager = new ChangeSetManager(recovered); set = recovered.owned('change_set', cut.setId, session.id);
await new Promise(resolve => setTimeout(resolve, 30)); assertBatchBytes(assert, f, beforeRestart);
assert.equal(set.taskId, session.taskId); assert.equal(set.runId, f.session.runId);
if (phase === 'prepared') {
assert.equal(set.status, 'prepared'); assert.equal(set.requiresVerification, false);
manager.discard(session, set.id, set.fingerprint); assertBatchBytes(assert, f, b.before); return;
}
if (!['finished', 'rollback_finished'].includes(phase)) { assert.equal(set.status, 'unknown'); assert.equal(set.requiresVerification, true); }
await assert.rejects(manager.start(session, set.id, set.fingerprint), { status: 409 });
const result = await manager.reconcile(session, set.id);
if (result.status === 'partial') { assert.equal(result.requiresVerification, true); await manager.rollback(session, set.id, set.fingerprint); }
assert.equal(set.requiresVerification, false);
assertBatchBytes(assert, f, phase === 'finished' ? b.after : b.before);
assert.equal(recovered.publicSession(session).execution.changeSets[0].id, cut.setId);
});
}
@@ -0,0 +1,172 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { fixture, until } from './helpers.mjs';
import { batchFixture, assertBatchBytes } from './fixtures/change-set-project.mjs';
import { ChangeSetManager } from '../src/core/workspaces/change-sets.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { GitDelivery } from '../src/core/git-delivery.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
import { assertChangeSetsResolved } from '../src/shared/contracts/change-sets.mjs';
test('sealed batches preserve bytes until review, then apply and explicitly restore BOM/CRLF/new files', async t => {
const f = fixture(t), b = batchFixture(f), owner = fs.readFileSync(path.join(f.projectRoot, 'README.md'));
const set = await b.manager.prepare(f.session, b.ids);
assert.equal((await b.manager.prepare(f.session, b.ids)).id, set.id);
assertBatchBytes(assert, f, b.before); assert.equal(fs.existsSync(path.join(f.projectRoot, 'docs')), false);
assert.equal(set.taskId, f.session.taskId); assert.equal(f.store.publicSession(f.session).execution.changeSets.length, 1);
for (let index = 0; index < set.files.length; index++) {
const snapshot = JSON.parse(f.store.artifactStore.read(f.store.owned('artifact', set.files[index].snapshotArtifactId, f.session.id)));
assert.deepEqual(snapshot.before === null ? null : Buffer.from(snapshot.before, 'base64'), b.before[set.files[index].path]);
}
assert.throws(() => applyChange(f.store, f.session, b.ids[0]), /封存/);
const job = await b.manager.start(f.session, set.id, set.fingerprint);
assert.equal((await job.promise).status, 'completed'); assertBatchBytes(assert, f, b.after);
assert.ok(f.session.changes.every(change => change.status === 'applied' && change.changeSetId === set.id));
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'README.md')), owner);
assert.equal((await b.manager.rollback(f.session, set.id, set.fingerprint)).status, 'rolled_back');
assertBatchBytes(assert, f, b.before); assert.ok(f.session.changes.every(change => change.status === 'rolled_back'));
await assert.rejects(b.manager.start(f.session, set.id, set.fingerprint), { status: 409 });
});
test('a conflict in the last file or Git branch prevents writing every file in the batch', async t => {
const f = fixture(t), b = batchFixture(f);
const git = args => execFileSync('git', args, { cwd: f.projectRoot, encoding: 'utf8', windowsHide: true });
git(['init', '-b', 'main']); git(['add', '--', 'README.md', 'src']);
git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '-m', 'baseline']);
const set = await b.manager.prepare(f.session, b.ids);
fs.mkdirSync(path.join(f.projectRoot, 'docs')); fs.writeFileSync(path.join(f.projectRoot, 'docs/new.md'), 'Owner file');
await assert.rejects(b.manager.start(f.session, set.id, set.fingerprint), /整批/);
assertBatchBytes(assert, f, { ...b.before, 'docs/new.md': Buffer.from('Owner file') }); assert.equal(set.status, 'stale');
b.manager.discard(f.session, set.id, set.fingerprint); fs.unlinkSync(path.join(f.projectRoot, 'docs/new.md'));
const fresh = await b.manager.prepare(f.session, b.ids); git(['switch', '-c', 'owner-branch']);
await assert.rejects(b.manager.start(f.session, fresh.id, fresh.fingerprint), /Git HEAD/); assertBatchBytes(assert, f, b.before);
});
test('duplicate canonical paths, ancestor collisions, links, dependencies and Windows aliases never form a batch', async t => {
const f = fixture(t), b = batchFixture(f);
const alias = proposeFile(f.store, f.session, { path: './src/one.mjs', content: 'alias\n', reason: 'alias' });
await assert.rejects(b.manager.prepare(f.session, [b.ids[0], alias.change_id]), /实际路径/);
const parent = proposeFile(f.store, f.session, { path: 'fresh', content: 'file', reason: 'parent' });
const child = proposeFile(f.store, f.session, { path: 'fresh/nested.md', content: 'child', reason: 'child' });
await assert.rejects(b.manager.prepare(f.session, [parent.change_id, child.change_id]), /子路径/);
fs.symlinkSync(path.join(f.projectRoot, 'src'), path.join(f.projectRoot, 'internal-link'), process.platform === 'win32' ? 'junction' : 'dir');
const linked = proposeFile(f.store, f.session, { path: 'internal-link/one.mjs', content: 'linked', reason: 'link' });
await assert.rejects(b.manager.prepare(f.session, [linked.change_id]), /链接/);
const dependency = proposeFile(f.store, f.session, { path: 'node_modules/owned.txt', content: 'dependency', reason: 'excluded' });
await assert.rejects(b.manager.prepare(f.session, [dependency.change_id]), /普通源码/);
if (process.platform === 'win32') {
assert.throws(() => proposeFile(f.store, f.session, { path: 'src/one.mjs.', content: 'alias', reason: 'windows alias' }), /可读取的项目范围/);
// A proposal retained from an older host must still be refused by batch
// validation even though the current proposal API now rejects it earlier.
const retained = f.session.changes.find(value => value.id === alias.change_id), originalPath = retained.path;
retained.path = 'src/one.mjs.';
try { await assert.rejects(b.manager.prepare(f.session, [retained.id]), /可读取的项目范围/); }
finally { retained.path = originalPath; }
}
assertBatchBytes(assert, f, b.before);
});
test('fingerprints, changed proposals and corrupt snapshot artifacts cannot write or restore any target', async t => {
const f = fixture(t), b = batchFixture(f), set = await b.manager.prepare(f.session, b.ids);
await assert.rejects(b.manager.start(f.session, set.id, '0'.repeat(64)), /指纹/); assertBatchBytes(assert, f, b.before);
f.session.changes[1].content = 'tampered proposal';
await assert.rejects(b.manager.start(f.session, set.id, set.fingerprint), /原提案/); assertBatchBytes(assert, f, b.before);
f.session.changes[1].content = b.after['src/two.mjs'].toString('utf8'); b.manager.discard(f.session, set.id, set.fingerprint);
const fresh = await b.manager.prepare(f.session, b.ids); await (await b.manager.start(f.session, fresh.id, fresh.fingerprint)).promise;
const artifact = f.store.owned('artifact', fresh.files[1].snapshotArtifactId, f.session.id);
fs.writeFileSync(path.join(f.store.artifactStore.root, artifact.relative), 'corrupt snapshot');
await assert.rejects(b.manager.rollback(f.session, fresh.id, fresh.fingerprint), /工件校验/); assertBatchBytes(assert, f, b.after);
});
test('a failure after replacement restores only batch bytes and never retries an old application', async t => {
const f = fixture(t), b = batchFixture(f, { faultInjector: (phase, set, index) => { if (phase === 'replaced' && index === 0) throw new Error('lost result after first replacement'); } });
const set = await b.manager.prepare(f.session, b.ids), result = await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
assert.equal(result.status, 'rolled_back', result.summary); assertBatchBytes(assert, f, b.before);
await assert.rejects(b.manager.start(f.session, set.id, set.fingerprint), { status: 409 });
});
test('rollback preserves owner edits, blocks other project writers, and explicit acknowledgement releases recovery', async t => {
const f = fixture(t), b = batchFixture(f, { faultInjector: (phase, set, index) => {
if (phase === 'replaced' && index === 1) { fs.writeFileSync(path.join(f.projectRoot, 'src/one.mjs'), 'Owner changed this file\n'); throw new Error('owner edit and lost result'); }
} });
const set = await b.manager.prepare(f.session, b.ids), result = await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
assert.equal(result.status, 'partial'); assert.equal(result.requiresVerification, true);
assertBatchBytes(assert, f, { ...b.before, 'src/one.mjs': Buffer.from('Owner changed this file\n') });
const runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
assert.throws(() => runtime.start(f.session.id, '新任务'), /变更集/);
assert.throws(() => proposeFile(f.store, f.session, { path: 'other.md', content: 'no' }), /变更集/);
assert.throws(() => applyChange(f.store, f.session, b.ids[2]), /变更集/);
await assert.rejects(new CheckRunner(f.store).start(f.session, 'unknown'), /变更集/);
await assert.rejects(new GitDelivery(f.store).plan(f.session), /变更集/);
const verified = await b.manager.reconcile(f.session, set.id), oldObservation = verified.observationFingerprint;
fs.writeFileSync(path.join(f.projectRoot, 'src/one.mjs'), 'Owner newer edit\n');
assert.throws(() => b.manager.acknowledge(f.session, set.id, set.fingerprint, oldObservation), /当前文件变化/);
await b.manager.reconcile(f.session, set.id);
const acknowledged = b.manager.acknowledge(f.session, set.id, set.fingerprint, set.observationFingerprint);
assert.equal(acknowledged.status, 'resolved_external'); assert.equal(acknowledged.requiresVerification, false);
assert.doesNotThrow(() => assertChangeSetsResolved(f.store, f.project.id));
assert.equal(f.session.changes[0].status, 'external_preserved'); assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/one.mjs'), 'utf8'), 'Owner newer edit\n');
});
test('an unattempted owner-created file identical to a suggestion remains external and is never removed', async t => {
const f = fixture(t), b = batchFixture(f, { faultInjector: (phase, set, index) => {
if (phase === 'write_intent' && index === 0) { fs.mkdirSync(path.join(f.projectRoot, 'docs')); fs.writeFileSync(path.join(f.projectRoot, 'docs/new.md'), '# 新文件\n'); throw new Error('not written by this batch'); }
} });
const set = await b.manager.prepare(f.session, b.ids), result = await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
assert.equal(result.status, 'partial'); assert.equal(result.observations[2].state, 'external');
const restored = await b.manager.rollback(f.session, set.id, set.fingerprint);
assert.equal(restored.status, 'partial'); assertBatchBytes(assert, f, { ...b.before, 'docs/new.md': b.after['docs/new.md'] });
b.manager.acknowledge(f.session, set.id, set.fingerprint, set.observationFingerprint);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'docs/new.md'), 'utf8'), '# 新文件\n');
});
test('active batch requests are serialized and cancellation leaves a verifiable ended batch', async t => {
const f = fixture(t); let release, entered = false;
const b = batchFixture(f, { faultInjector: async (phase, set, index) => {
if (phase === 'write_intent' && index === 0) { entered = true; await new Promise(resolve => { release = resolve; }); }
} });
const set = await b.manager.prepare(f.session, b.ids), started = await b.manager.start(f.session, set.id, set.fingerprint);
await until(() => entered); await assert.rejects(b.manager.start(f.session, set.id, set.fingerprint), { status: 409 });
await assert.rejects(b.manager.reconcile(f.session, set.id), { status: 409 });
const stopped = b.manager.stop(f.session.id); release(); await stopped;
assert.equal((await started.promise).status, 'rolled_back'); assertBatchBytes(assert, f, b.before); assert.equal(b.manager.projects.size, 0);
});
test('batch application and rollback preserve unrelated Git index and worktree bytes', async t => {
const f = fixture(t), b = batchFixture(f), git = args => execFileSync('git', args, { cwd: f.projectRoot, windowsHide: true, stdio: 'pipe' });
git(['init', '-b', 'main']); git(['add', '--', 'README.md', 'src']); git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '-m', 'baseline']);
fs.writeFileSync(path.join(f.projectRoot, 'README.md'), 'Owner staged\n'); git(['add', '--', 'README.md']);
fs.writeFileSync(path.join(f.projectRoot, 'README.md'), 'Owner unstaged after staging\n');
const index = fs.readFileSync(path.join(f.projectRoot, '.git/index')), owner = fs.readFileSync(path.join(f.projectRoot, 'README.md'));
const set = await b.manager.prepare(f.session, b.ids); await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, '.git/index')), index); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'README.md')), owner);
await b.manager.rollback(f.session, set.id, set.fingerprint);
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, '.git/index')), index); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'README.md')), owner);
assertBatchBytes(assert, f, b.before);
});
test('recovery reuses exact staged bytes but retains a conflicting temporary file for owner inspection', async t => {
const f = fixture(t), b = batchFixture(f), set = await b.manager.prepare(f.session, b.ids);
await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
const temporary = path.join(f.projectRoot, 'src', '.awb-' + set.id + '-1-rollback.tmp');
fs.writeFileSync(temporary, 'Owner temporary file');
const blocked = await b.manager.rollback(f.session, set.id, set.fingerprint);
assert.equal(blocked.status, 'partial'); assert.equal(fs.readFileSync(temporary, 'utf8'), 'Owner temporary file');
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'src/two.mjs')), b.after['src/two.mjs']);
fs.writeFileSync(temporary, b.before['src/two.mjs']);
const restored = await b.manager.rollback(f.session, set.id, set.fingerprint);
assert.equal(restored.status, 'rolled_back'); assert.equal(fs.existsSync(temporary), false); assertBatchBytes(assert, f, b.before);
});
test('one Task can seal pending proposals from earlier Runs while snapshots retain each source Run', async t => {
const f = fixture(t), b = batchFixture(f), firstRunId = f.session.runId;
const next = f.store.beginRun(f.session, '', true, f.store.config); f.session.status = 'needs_review'; f.store.finishRun(f.session, next);
const set = await b.manager.prepare(f.session, b.ids);
assert.equal(set.runId, next.id); assert.ok(set.files.every(file => file.sourceRunId === firstRunId));
assert.equal((await (await b.manager.start(f.session, set.id, set.fingerprint)).promise).status, 'completed'); assertBatchBytes(assert, f, b.after);
});
@@ -0,0 +1,265 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { execFileSync, spawn } from 'node:child_process';
import { fixture, call, until, launchWorkbench, compatibleFixture, completion, workspace, assertToolPairs } from './helpers.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Store } from '../src/server/store.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
function write(f, relative, value) { const file = path.join(f.projectRoot, relative); fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, value); }
function nodeProject(f, { source = 'export const add = (a, b) => a + b;\n', testCode, script = 'node --test test/*.test.mjs', build = true } = {}) {
write(f, 'package.json', JSON.stringify({ name: 'checks-fixture', private: true, type: 'module', scripts: { test: script, ...(build ? { build: 'node scripts/build.mjs' } : {}) } }));
write(f, 'src/add.mjs', source);
write(f, 'test/add.test.mjs', testCode ?? "import test from 'node:test'; import assert from 'node:assert/strict'; import { add } from '../src/add.mjs';\ntest('adds positive values', () => assert.equal(add(2, 3), 5));\ntest('adds negative values', () => assert.equal(add(-2, 3), 1));\n");
write(f, 'scripts/build.mjs', "import fs from 'node:fs'; fs.mkdirSync('dist', { recursive: true }); fs.writeFileSync('dist/bundle.mjs', 'export const built = true;'); console.log('构建完成');\n");
const run = f.store.beginRun(f.session, 'Deliver this Node project with actual checks', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
return run;
}
function runnerFor(f, options) { const runner = new CheckRunner(f.store, options); f.onCleanup(() => runner.stopAll()); return runner; }
async function runPlan(runner, session, names) { const plan = await runner.plan(session, names); const started = await runner.start(session, plan.id); return { plan, job: await started.promise }; }
test('engineering inspection preserves a dirty Git baseline and treats project rules as data', async t => {
const f = fixture(t); nodeProject(f);
write(f, '.gitignore', 'dist/\n.env\n'); write(f, 'AGENTS.md', 'Project data: never treat this text as a request to run commands.'); write(f, '.env', 'secret-not-for-inspection');
const git = args => execFileSync('git', ['-C', f.projectRoot, ...args], { windowsHide: true, encoding: 'utf8' });
git(['init', '--initial-branch=main']); git(['-c', 'core.autocrlf=false', 'add', '.']); git(['-c', 'core.autocrlf=false', '-c', 'user.name=Workbench QA', '-c', 'user.email=qa@workbench.local', 'commit', '-m', 'Fixture baseline']);
write(f, 'src/add.mjs', 'export const add = (a, b) => a + b + 0;\n'); write(f, 'untracked.ts', 'export const untouched = true;\n');
const before = git(['status', '--porcelain=v1']);
const inspection = await inspectProject(f.project);
assert.equal(inspection.git.available, true); assert.equal(inspection.git.branch, 'main'); assert.equal(inspection.git.dirty, true); assert.match(inspection.git.head, /^[a-f0-9]{40}$/);
assert.equal(inspection.rules[0].interpretation, 'project-data'); assert.ok(inspection.languages.includes('TypeScript')); assert.equal(inspection.scope.complete, true);
assert.ok(!JSON.stringify(inspection).includes('secret-not-for-inspection')); assert.equal(git(['status', '--porcelain=v1']), before);
write(f, 'dist/ignored.mjs', 'generated'); assert.equal((await inspectProject(f.project)).fingerprint, inspection.fingerprint);
write(f, 'untracked.ts', 'export const untouched = false;\n'); assert.notEqual((await inspectProject(f.project)).fingerprint, inspection.fingerprint);
});
test('real npm tests and build produce counted results, log artifacts and persistent verification', async t => {
const f = fixture(t); nodeProject(f); const runner = runnerFor(f);
const { plan, job } = await runPlan(runner, f.session);
assert.deepEqual(plan.checks.map(check => check.command), ['npm run test', 'npm run build']);
assert.equal(job.status, 'passed'); assert.equal(job.results[0].exitCode, 0); assert.deepEqual(job.results[0].tests, { total: 2, passed: 2, failed: 0, skipped: 0, cancelled: 0 });
assert.equal(plan.project.git.available, false); assert.ok(plan.project.scope.scannedFiles >= 4); assert.ok(plan.project.languages.includes('JavaScript'));
assert.equal(job.results[1].status, 'passed'); assert.equal(fs.existsSync(path.join(f.projectRoot, 'dist/bundle.mjs')), true);
const log = f.store.owned('artifact', job.results[1].logArtifactId, f.session.id); assert.match(f.store.artifactStore.read(log), /构建完成/);
assert.equal(f.store.executionSnapshot(f.session).task.verification.status, 'passed');
f.store.close(); const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
assert.equal(recovered.owned('check_job', job.id, f.session.id).status, 'passed'); assert.equal(recovered.executionSnapshot(recovered.session(f.session.id)).task.verification.status, 'passed');
});
test('an actual failing test retains the nonzero exit code and failed count', async t => {
const f = fixture(t); nodeProject(f, { source: 'export const add = (a, b) => a - b;\n' }); const runner = runnerFor(f);
const { job } = await runPlan(runner, f.session);
assert.equal(job.status, 'failed'); assert.notEqual(job.results[0].exitCode, 0); assert.equal(job.results[0].tests.failed, 2);
assert.match(f.store.artifactStore.read(f.store.owned('artifact', job.results[0].logArtifactId, f.session.id)), /AssertionError|ERR_ASSERTION/);
assert.equal(job.results[1].status, 'passed'); assert.equal(f.store.executionSnapshot(f.session).task.verification.status, 'failed');
});
test('unchanged automatic checks return original failure evidence, while explicit user reruns and changed source still execute', async t => {
const f = fixture(t); nodeProject(f, { source: 'export const add = (a, b) => a - b;\n' }); const runner = runnerFor(f);
let executions = 0; const execute = runner.executeCheck.bind(runner);
runner.executeCheck = async (...args) => { executions++; return execute(...args); };
const { job } = await runPlan(runner, f.session, ['test']);
const second = await runner.plan(f.session, ['test']);
assert.notEqual(second.id, job.planId); assert.equal((await runner.unchangedResult(f.session, second.id)).id, job.id);
assert.equal(executions, 1); assert.equal(f.store.core.check_job.size, 1);
const rerun = await runner.start(f.session, second.id, { source: 'user' }); await rerun.promise;
assert.equal(executions, 2); assert.equal(f.store.core.check_job.size, 2); assert.notEqual(rerun.job.id, job.id);
write(f, 'src/add.mjs', 'export const add = (a, b) => a + b;\n');
const changed = await runner.plan(f.session, ['test']); assert.equal(await runner.unchangedResult(f.session, changed.id), null);
const repaired = await runner.start(f.session, changed.id); assert.equal((await repaired.promise).status, 'passed'); assert.equal(executions, 3);
});
test('agent loops cannot hide identical failing checks behind new plan IDs or unrelated reads', async t => {
const f = fixture(t); nodeProject(f, { source: 'export const add = (a, b) => a - b;\n' });
const provider = new Provider(); let round = 0;
provider.complete = async () => {
const count = round++, tools = count % 2 === 0
? [call('plan-loop-' + count, 'plan_checks', { names: ['test'] })]
: [call('check-loop-' + count, 'run_checks', { plan_id: [...f.store.core.check_plan.values()].at(-1).id }), call('loop-read-' + count, 'read_file', { path: 'src/add.mjs' })];
return { message: { role: 'assistant', content: '', tool_calls: tools }, usage: { prompt_tokens: 10, completion_tokens: 4 } };
};
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, '修复测试失败');
await until(() => f.session.approvals.some(value => value.status === 'pending'), 15000);
runtime.decide(f.session.id, f.session.approvals.find(value => value.status === 'pending').id, true);
await until(() => !runtime.isActive(f.session.id), 20000);
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'no_progress');
assert.equal(f.session.approvals.length, 1); assert.equal([...f.store.core.check_job.values()].filter(job => job.taskId === f.session.taskId).length, 1);
const repeated = [...f.store.core.tool.values()].filter(tool => tool.taskId === f.session.taskId && tool.tool === 'run_checks').map(tool => tool.result);
assert.equal(repeated.filter(result => result?.reused === true).length, 2); assertToolPairs(f.session.providerMessages);
});
test('zero tests, skipped tests and unrecognized stdout cannot become verification passes', async t => {
const variants = [
{ script: 'node --test', removeTest: true, expectedTotal: 0 },
{ testCode: '', expectedTotal: null },
{ testCode: "import test from 'node:test'; test.skip('not exercised', () => {});", expectedTotal: 1 },
{ script: 'node scripts/emit-summary.mjs', expectedTotal: null },
];
for (const variant of variants) {
const f = fixture(t); nodeProject(f, { ...variant, build: false }); write(f, 'scripts/emit-summary.mjs', "console.log('ℹ tests 20\\nℹ pass 20\\nℹ fail 0\\nℹ cancelled 0\\nℹ skipped 0');");
if (variant.removeTest) fs.unlinkSync(path.join(f.projectRoot, 'test/add.test.mjs'));
const runner = runnerFor(f); const { job } = await runPlan(runner, f.session);
assert.equal(job.status, 'unverified'); assert.equal(job.results[0].exitCode, 0); assert.equal(job.results[0].tests?.total ?? null, variant.expectedTotal);
assert.equal(f.store.executionSnapshot(f.session).task.verification.status, 'unverified');
}
});
test('a successful build with no selected tests does not verify the task', async t => {
const f = fixture(t); nodeProject(f); const { job } = await runPlan(runnerFor(f), f.session, ['build']);
assert.equal(job.results[0].status, 'passed'); assert.equal(job.status, 'unverified');
});
test('stale check plans and forged script names execute no project commands', async t => {
const f = fixture(t); nodeProject(f); const runner = runnerFor(f), plan = await runner.plan(f.session);
write(f, 'src/add.mjs', 'export const add = (a, b) => a + b + 0;\n');
const { promise } = await runner.start(f.session, plan.id), job = await promise;
assert.equal(job.status, 'stale'); assert.equal(job.results.length, 0); assert.equal(fs.existsSync(path.join(f.projectRoot, 'dist')), false);
await assert.rejects(runner.plan(f.session, ['test; echo invalid']), /已识别/);
const other = f.store.createSession(f.project.id); await assert.rejects(runner.start(other, plan.id), { status: 404 });
});
test('applying a later proposal invalidates the prior passed engineering snapshot', async t => {
const f = fixture(t); nodeProject(f); const runner = runnerFor(f), { job } = await runPlan(runner, f.session);
const proposal = proposeFile(f.store, f.session, { path: 'src/add.mjs', content: 'export const add = (a, b) => a + b + 1;\n', reason: 'Controlled change after verification' });
await assert.rejects(runner.start(f.session, (await runner.plan(f.session)).id), /待审查/);
applyChange(f.store, f.session, proposal.change_id);
assert.equal(job.stale, true); assert.equal(f.store.executionSnapshot(f.session).task.verification.status, 'stale');
});
test('stopping an actual check terminates its child process and blocks concurrent jobs', async t => {
const f = fixture(t); nodeProject(f, { testCode: "import test from 'node:test'; import fs from 'node:fs'; test('long running check', async () => { fs.writeFileSync('.awb-check-entered', 'entered'); await new Promise(resolve => setTimeout(resolve, 30000)); });\n", build: false });
const runner = runnerFor(f), plan = await runner.plan(f.session), started = await runner.start(f.session, plan.id);
await until(() => fs.existsSync(path.join(f.projectRoot, '.awb-check-entered')), 10000);
await assert.rejects(runner.start(f.session, plan.id), { status: 409 });
await runner.stop(f.session.id, started.job.id);
const job = await started.promise; assert.equal(job.status, 'cancelled'); assert.equal(job.results[0].status, 'cancelled'); assert.equal(runner.active.size, 0);
});
test('check timeout records a failure with its actual termination result', async t => {
const f = fixture(t); nodeProject(f, { testCode: "import test from 'node:test'; test('timeout', async () => { await new Promise(resolve => setTimeout(resolve, 30000)); });", build: false });
const { job } = await runPlan(runnerFor(f, { timeoutMs: 400 }), f.session);
assert.equal(job.status, 'failed'); assert.equal(job.results[0].timedOut, true); assert.match(job.results[0].reason, /时间上限/);
});
test('source edits during a successful check make the result stale', async t => {
const f = fixture(t); nodeProject(f, { testCode: "import test from 'node:test'; import fs from 'node:fs'; test('check snapshot', async () => { fs.writeFileSync('.awb-check-entered', 'entered'); await new Promise(resolve => setTimeout(resolve, 1200)); });", build: false });
const runner = runnerFor(f), plan = await runner.plan(f.session), started = await runner.start(f.session, plan.id);
await until(() => fs.existsSync(path.join(f.projectRoot, '.awb-check-entered')), 10000); write(f, 'src/add.mjs', 'export const add = (a, b) => a + b + 0;');
const job = await started.promise; assert.equal(job.results[0].status, 'passed'); assert.equal(job.status, 'stale'); assert.equal(job.stale, true);
});
test('bounded large logs retain the test footer and readable artifact instead of inventing counts', async t => {
const f = fixture(t); nodeProject(f, { testCode: "import test from 'node:test'; test('large output', () => { console.log('中'.repeat(600000)); });", build: false });
const { job } = await runPlan(runnerFor(f), f.session);
assert.equal(job.status, 'passed'); assert.equal(job.results[0].truncated, true); assert.equal(job.results[0].tests.total, 1);
const artifact = f.store.owned('artifact', job.results[0].logArtifactId, f.session.id), text = f.store.artifactStore.read(artifact);
assert.match(text, /中间日志超出保存上限/); assert.ok(artifact.bytes < 2 * 1024 * 1024); assert.match(text, /tests 1/);
});
test('model-selected checks require a run-bound decision and denial creates no check job', async t => {
const f = fixture(t); nodeProject(f); const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let round = 0;
provider.complete = async (_config, messages) => {
const planResult = messages.filter(message => message.role === 'tool').map(message => { try { return JSON.parse(message.content); } catch { return {}; } }).find(value => value.checks && value.fingerprint);
const calls = round++ === 0 ? [call('plan-for-model', 'plan_checks', {})] : round === 2 ? [call('run-for-model', 'run_checks', { plan_id: planResult.id })] : [];
return { message: { role: 'assistant', content: calls.length ? '' : 'Checks were declined; task has no engineering pass.', tool_calls: calls }, usage: { prompt_tokens: 10, completion_tokens: 5 } };
};
runtime.start(f.session.id, 'Validate this engineering project'); const promise = runtime.active.get(f.session.id).promise;
await until(() => f.session.approvals.some(value => value.status === 'pending'), 8000);
assert.equal(f.store.core.check_job.size, 0); const approval = f.session.approvals.at(-1);
assert.equal(approval.runId, f.session.runId); runtime.decide(f.session.id, approval.id, false); await promise;
assert.equal(f.store.core.check_job.size, 0); assert.equal(f.session.events.find(event => event.tool === 'run_checks').phase, 'denied'); assertToolPairs(f.session.providerMessages);
});
test('check APIs expose owned plans and jobs and preserve the authoritative results across restart', async t => {
const f = fixture(t); nodeProject(f); f.store.close(); const app = await launchWorkbench(t, f.data);
const planned = await app.api('/sessions/' + f.session.id + '/check-plans', {}), plan = planned.execution.plans.at(-1);
const accepted = await app.api('/sessions/' + f.session.id + '/check-jobs', { planId: plan.id }); const jobId = accepted.execution.checks.at(-1).id;
await until(async () => !['queued', 'running'].includes((await app.api('/sessions/' + f.session.id + '/check-jobs/' + jobId)).status), 12000);
const finished = await app.api('/sessions/' + f.session.id + '/check-jobs/' + jobId); assert.equal(finished.status, 'passed');
const other = await app.api('/sessions', { projectId: f.project.id }); await assert.rejects(app.api('/sessions/' + other.id + '/check-jobs/' + jobId), { status: 404 });
const log = await app.api('/artifacts/' + finished.results[0].logArtifactId + '?session=' + f.session.id); assert.match(log.content, /tests 2/);
const exported = await app.api('/sessions/' + f.session.id + '/export'); assert.equal(exported.execution.checks.at(-1).id, jobId);
await app.close(); const resumed = await launchWorkbench(t, f.data); assert.equal((await resumed.api('/sessions/' + f.session.id)).execution.task.verification.status, 'passed');
});
test('reviewed changes continue the original task without reopening a generic completed run', async t => {
const f = fixture(t); nodeProject(f); const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let round = 0;
provider.complete = async () => ({ message: { role: 'assistant', content: round++ === 0 ? '' : 'Reviewed changes need actual verification.',
...(round === 1 ? { tool_calls: [call('reviewed-proposal', 'propose_file', { path: 'src/add.mjs', content: 'export const add = (a, b) => a + b + 0;\n', reason: 'Verify the reviewed-task continuation' })] } : {}) }, usage: { prompt_tokens: 5, completion_tokens: 5 } });
runtime.start(f.session.id, 'Keep this original engineering goal'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'needs_review'); const taskId = f.session.taskId, firstRun = f.session.runId;
assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 });
applyChange(f.store, f.session, f.session.changes.at(-1).id); assert.equal(f.session.status, 'completed');
assert.throws(() => runtime.resume(f.session.id), { status: 409 });
const before = JSON.stringify(f.store.publicSession(f.session)), config = f.store.config;
f.store.config = { ...config, mode: 'compatible', model: '' };
assert.throws(() => runtime.continueReviewed(f.session.id), /模型名称/);
assert.equal(JSON.stringify(f.store.publicSession(f.session)), before, 'rejected continuation must not append an instruction or run');
f.store.config = config;
runtime.continueReviewed(f.session.id); assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 });
await runtime.active.get(f.session.id).promise;
assert.equal(f.session.taskId, taskId); assert.equal(f.store.core.task.get(taskId).goal, 'Keep this original engineering goal');
assert.equal(f.store.core.run.get(f.session.runId).resumeOf, firstRun); assert.equal(f.store.core.task.get(taskId).instructions.length, 0);
assert.equal(f.store.core.run.get(f.session.runId).source, 'review-action');
assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 }); assertToolPairs(f.session.providerMessages);
});
test('concurrent review-continuation API requests accept one run and retain the goal after restart', async t => {
const f = fixture(t); nodeProject(f);
let requests = 0;
const model = await compatibleFixture(t, async () => {
if (requests++ === 0) return completion([call('api-review-proposal', 'propose_file', { path: 'src/add.mjs', content: 'export const add = (a, b) => a + b + 0;\n', reason: 'A reviewable API fixture change' })]);
await new Promise(resolve => setTimeout(resolve, 120));
return completion([], 'Continue the same delivery goal with the saved results.');
});
const app = await launchWorkbench(t, f.data);
await app.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'review-fixture' });
await app.api('/sessions/' + f.session.id + '/messages', { content: 'Retain this delivery goal' });
let reviewed; await until(async () => { reviewed = await app.api('/sessions/' + f.session.id); return reviewed.status === 'needs_review'; });
await app.api('/sessions/' + f.session.id + '/changes/' + reviewed.changes.at(-1).id + '/apply', {});
const responses = await Promise.allSettled([app.api('/sessions/' + f.session.id + '/continue-reviewed', {}), app.api('/sessions/' + f.session.id + '/continue-reviewed', {})]);
assert.equal(responses.filter(value => value.status === 'fulfilled').length, 1);
assert.equal(responses.find(value => value.status === 'rejected').reason.status, 409);
let delivered; await until(async () => { delivered = await app.api('/sessions/' + f.session.id); return delivered.status === 'completed'; });
assert.equal(delivered.execution.task.id, reviewed.execution.task.id); assert.equal(delivered.execution.task.goal, 'Retain this delivery goal');
assert.equal(delivered.execution.run.resumeOf, reviewed.execution.run.id);
assert.equal(delivered.execution.task.instructions.filter(value => value.source === 'review-action').length, 0);
assert.equal(delivered.execution.run.source, 'review-action');
await app.close(); const restarted = await launchWorkbench(t, f.data);
const saved = await restarted.api('/sessions/' + f.session.id);
assert.equal(saved.execution.task.id, delivered.execution.task.id); assert.equal(saved.execution.run.id, delivered.execution.run.id);
assert.equal(saved.execution.task.instructions.filter(value => value.source === 'review-action').length, 0);
assert.equal(saved.execution.run.source, 'review-action');
assertToolPairs((await restarted.api('/sessions/' + f.session.id + '/export')).session.providerMessages);
});
test('the model reads owned failure logs without running the check again', async t => {
const f = fixture(t); nodeProject(f, { source: 'export const add = (a, b) => a - b;\n' });
const { job } = await runPlan(runnerFor(f), f.session); const artifactId = job.results[0].logArtifactId;
assert.match(job.results[0].logExcerpt, /AssertionError|ERR_ASSERTION/);
const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let round = 0; provider.complete = async () => ({ message: round++ === 0 ? { role: 'assistant', content: '', tool_calls: [call('read-check-log', 'read_artifact', { artifact_id: artifactId, offset: 0, limit: 16000 })] } : { role: 'assistant', content: 'Failure log read without replay.' }, usage: { prompt_tokens: 5, completion_tokens: 5 } });
runtime.start(f.session.id, 'Explain the saved check failure'); await runtime.active.get(f.session.id).promise;
assert.equal(f.store.core.check_job.size, 1); const result = f.store.savedToolResult(f.session.id, 'read-check-log'); assert.match(result.content, /AssertionError|ERR_ASSERTION/); assert.equal(result.artifact_id, artifactId);
const other = f.store.createSession(f.project.id); assert.throws(() => f.store.owned('artifact', artifactId, other.id), { status: 404 });
});
for (const phase of ['queued', 'result_committed']) test('process exit at check ' + phase + ' preserves results and never replays commands', async t => {
const f = fixture(t); nodeProject(f, { testCode: "import test from 'node:test'; import fs from 'node:fs'; test('one side effect', () => fs.writeFileSync('.awb-effect', '1'));" }); f.store.close();
const worker = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/check-crash-worker.mjs'), f.data, f.session.id, phase], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let error = ''; worker.stderr.on('data', bytes => { error += bytes.toString(); }); const code = await new Promise(resolve => worker.once('exit', resolve)); assert.equal(code, 69, error);
const recovered = new Store(f.data); f.onCleanup(() => recovered.close()); const session = recovered.session(f.session.id), job = recovered.executionSnapshot(session).checks.at(-1);
assert.equal(job.status, phase === 'queued' ? 'interrupted' : 'unknown'); assert.equal(session.execution, undefined);
if (phase === 'result_committed') { assert.equal(job.results.length, 1); assert.equal(job.results[0].tests.total, 1); assert.equal(fs.readFileSync(path.join(f.projectRoot, '.awb-effect'), 'utf8'), '1'); }
else assert.equal(fs.existsSync(path.join(f.projectRoot, '.awb-effect')), false);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'dist')), false); assert.equal(recovered.executionSnapshot(session).task.verification.status, job.status);
});
@@ -0,0 +1,75 @@
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, call, until, assertToolPairs, launchWorkbench } from './helpers.mjs';
import { Runtime, validateToolArgs } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Store } from '../src/server/store.mjs';
import { applyChange, hash } from '../src/server/files.mjs';
test('model code tools keep Task/Run ownership, index evidence and exact proposals across storage recovery', async t => {
const f = fixture(t), target = path.join(f.projectRoot, 'compute.ts'), original = '\uFEFF// protected banner\r\nexport function compute() { return 1; }\r\n';
fs.writeFileSync(target, original); let rounds = 0, fileSha = '';
const model = await compatibleFixture(t, (_url, input) => {
assertToolPairs(input.messages); rounds++;
const output = id => JSON.parse(input.messages.find(value => value.tool_call_id === id).content);
if (rounds === 1) {
for (const name of ['index_project', 'query_code', 'propose_patch']) assert.ok(input.tools.some(value => value.function.name === name));
return completion([call('index', 'index_project', {}), call('query', 'query_code', { query: 'compute', kind: 'symbols', limit: 5 })]);
}
if (rounds === 2) {
assert.equal(output('index').projectId, f.project.id); assert.equal(output('query').results[0].line, 2);
assert.ok(output('index').artifact_id); assert.equal(output('query').results[0].evidenceMethod, 'javascript-typescript-ast');
return completion([call('read', 'read_file', { path: 'compute.ts', start_line: 2, end_line: 2 })]);
}
if (rounds === 3) {
fileSha = output('read').sha256; assert.equal(output('read').bom, true);
return completion([call('patch', 'propose_patch', { path: 'compute.ts', base_sha256: fileSha, reason: 'Repair precise implementation', edits: [{ old_text: 'return 1;', new_text: 'return 2;' }] })]);
}
assert.equal(output('patch').status, 'awaiting_review'); assert.equal(output('patch').patch.lineEnding, 'crlf');
return completion([], '精确补丁已准备,请审核当前项目的差异。');
});
f.store.saveConfig({ ...f.store.config, mode: 'compatible', baseUrl: model.baseUrl, model: 'index-tool-fixture' });
const provider = new Provider(); provider.setKey('synthetic-index-key'); const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Use project index and exact patches'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(rounds, 4); assert.equal(f.session.changes.length, 1); assert.equal(fs.readFileSync(target, 'utf8'), original);
const change = f.session.changes[0], run = f.store.executionSnapshot(f.session).run;
assert.equal(change.editKind, 'exact-patch'); assert.equal(change.runId, run.id); assert.equal(change.taskId, run.taskId); assert.equal(change.baseSha256, fileSha);
const artifact = f.store.executionSnapshot(f.session).artifacts.find(value => value.kind === 'project-index');
const owned = f.store.owned('artifact', artifact.id, f.session.id), content = f.store.artifactStore.read(owned), index = JSON.parse(content);
assert.equal(owned.runId, run.id); assert.equal(owned.taskId, run.taskId); assert.equal(owned.sha256, hash(Buffer.from(content))); assert.equal(index.projectId, f.project.id);
assert.ok(!content.includes('synthetic-index-key')); assert.equal(index.symbols.find(value => value.name === 'compute').sha256, fileSha);
const other = f.store.createSession(f.project.id); assert.throws(() => f.store.owned('artifact', artifact.id, other.id));
applyChange(f.store, f.session, change.id); assert.equal(fs.readFileSync(target, 'utf8'), original.replace('return 1;', 'return 2;'));
f.store.close(); const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
assert.deepEqual(recovered.session(f.session.id).changes[0].patch, change.patch);
assert.equal(recovered.artifactStore.read(recovered.owned('artifact', artifact.id, f.session.id)), content);
});
test('bundled code APIs isolate project results, validate queries and create review-only patches with conflicts', async t => {
const f = fixture(t); fs.writeFileSync(path.join(f.projectRoot, 'code.mjs'), 'export const currentOnly = 1;\n');
const otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); fs.writeFileSync(path.join(otherRoot, 'code.mjs'), 'export const otherOnly = 1;\n');
const other = f.store.addProject(otherRoot), server = await launchWorkbench(t, f.data);
assert.equal(server.bootstrap.capabilities.codeIndex, 'bounded-ast-and-candidates'); assert.equal(server.bootstrap.capabilities.exactPatches, 'sha256-unique-anchors');
assert.equal((await fetch(server.url + '/api/projects/' + f.project.id + '/code-index')).status, 403);
const index = await server.api('/projects/' + f.project.id + '/code-index'); assert.equal(index.projectId, f.project.id); assert.equal(index.coverage.astFiles, 1);
const own = await server.api('/projects/' + f.project.id + '/code-query?query=currentOnly&kind=symbols'); assert.equal(own.results[0].path, 'code.mjs'); assert.equal(own.results[0].line, 1);
const separate = await server.api('/projects/' + other.id + '/code-query?query=currentOnly'); assert.equal(separate.results.length, 0); assert.equal(separate.index.projectId, other.id);
for (const query of ['kind=references', 'limit=0', 'limit=61', 'limit=abc', 'query=' + 'a'.repeat(201)]) await assert.rejects(server.api('/projects/' + f.project.id + '/code-query?' + query), { status: 400 });
const args = { path: 'code.mjs', base_sha256: own.results[0].sha256, reason: 'Exact API edit', edits: [{ old_text: 'currentOnly = 1', new_text: 'currentOnly = 2' }] };
const proposed = await server.api('/sessions/' + f.session.id + '/patch-proposals', args); assert.equal(proposed.changes[0].editKind, 'exact-patch');
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'code.mjs'), 'utf8'), 'export const currentOnly = 1;\n');
fs.writeFileSync(path.join(f.projectRoot, 'code.mjs'), 'export const owner = 3;\n');
await assert.rejects(server.api('/sessions/' + f.session.id + '/patch-proposals', args), { status: 409 });
await assert.rejects(server.api('/sessions/' + f.session.id + '/changes/' + proposed.changes[0].id + '/apply', {}), { status: 409 });
const refreshed = await server.api('/projects/' + f.project.id + '/code-query?query=owner'); assert.equal(refreshed.results[0].name, 'owner'); assert.notEqual(refreshed.index.indexFingerprint, own.index.indexFingerprint);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'code.mjs'), 'utf8'), 'export const owner = 3;\n');
});
test('code tool arguments reject malformed ownership-independent inputs before side effects', () => {
for (const [name, args] of [['query_code', { query: 'q', kind: 'references' }], ['query_code', { query: [] }], ['query_code', { query: 'q', limit: 1.5 }],
['propose_patch', { path: 'a', base_sha256: 'invalid', reason: 'x', edits: [] }], ['propose_patch', { path: 'a', base_sha256: 'a'.repeat(64), reason: 'x', edits: [] }]]) assert.throws(() => validateToolArgs(name, args));
validateToolArgs('query_code', { query: 'compute', kind: 'symbols', limit: 5 });
validateToolArgs('propose_patch', { path: 'a', base_sha256: 'a'.repeat(64), reason: 'x', edits: [{ old_text: 'a', new_text: 'b' }] });
});
@@ -0,0 +1,401 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import https from 'node:https';
import fs from 'node:fs';
import path from 'node:path';
import { randomBytes, randomUUID, createCipheriv, createDecipheriv, X509Certificate } from 'node:crypto';
import { fixture, call, until, workspace } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { installUpgrade } from '../src/server/hot-upgrade.mjs';
import { createTask, createRun } from '../src/shared/contracts/execution.mjs';
import http from 'node:http';
import { CompanionGateway } from '../src/server/companion-gateway.mjs';
import { DeviceRegistry } from '../src/core/companion/device-registry.mjs';
import { CompanionTLS } from '../src/core/companion/tls-authority.mjs';
import { approvalBinding, companionHash } from '../src/shared/contracts/companion.mjs';
import { InputAttachments } from '../src/core/context/input-attachments.mjs';
import { PNG } from 'pngjs';
function setup(t) {
const value = fixture(t), host = { instanceId: randomUUID(), ownerId: randomUUID(), userSid: 'fixture-current-user', pid: process.pid, createdAt: new Date().toISOString() };
let clock = Date.now();
const registry = new DeviceRegistry(value.store, () => host, { clock: () => clock });
const grant = { projectIds: [value.project.id], permissions: { observe: true, approve: false, stop: false }, expiresInHours: 24 };
registry.initialize();
return { ...value, registry, grant, host, advance: ms => { clock += ms; } };
}
function testSealing() {
// This explicit dependency is confined to tests. Product code always uses
// Windows CurrentUser DPAPI and has no plaintext/non-Windows fallback.
const key = randomBytes(32);
return { protect: (bytes, entropy) => {
const iv = randomBytes(12), cipher = createCipheriv('aes-256-gcm', key, iv); cipher.setAAD(entropy);
const encrypted = Buffer.concat([cipher.update(bytes), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), encrypted]);
}, unprotect: (bytes, entropy) => {
const decipher = createDecipheriv('aes-256-gcm', key, bytes.subarray(0, 12)); decipher.setAuthTag(bytes.subarray(12, 28)); decipher.setAAD(entropy);
return Buffer.concat([decipher.update(bytes.subarray(28)), decipher.final()]);
} };
}
async function running(t) {
const f = setup(t), runtime = { waiters: new Map(), decide() { throw new Error('No approval'); }, async stop() {} };
const appDirectory = path.join(f.root, 'app'); fs.mkdirSync(path.join(appDirectory, 'dist-client'), { recursive: true });
fs.writeFileSync(path.join(appDirectory, 'dist-client', 'mobile.html'), '<!doctype html><title>Test mobile entry</title>');
fs.writeFileSync(path.join(appDirectory, 'dist-client', 'mobile-assets.json'), '[]');
const gateway = new CompanionGateway({ store: f.store, runtime, appDirectory, identity: () => f.host, redact: value => String(value).replaceAll('fixture-secret', '[REDACTED]') },
{ tlsOptions: testSealing(), allowLoopback: true });
f.onCleanup(() => gateway.close());
assert.equal(gateway.status().enabled, false);
await gateway.start({ address: '127.0.0.1', port: 0 });
const ca = gateway.registry.read().tls.certificate;
const request = (route, data, { cookie = '', origin = gateway.origin, headers = {} } = {}) => new Promise((resolve, reject) => {
const req = https.request(gateway.origin + route, { ca, servername: '', method: data === undefined ? 'GET' : 'POST', headers: {
...(cookie ? { Cookie: cookie } : {}), ...(origin ? { Origin: origin } : {}), ...(data === undefined ? {} : { 'Content-Type': 'application/json' }), Connection: 'close', ...headers } }, res => {
const chunks = []; res.on('data', chunk => chunks.push(chunk)); res.on('end', () => {
const text = Buffer.concat(chunks).toString('utf8'); let body; try { body = JSON.parse(text); } catch { body = text; }
resolve({ status: res.statusCode, headers: res.headers, body,bytes:Buffer.concat(chunks) });
});
}); req.on('error', reject); req.end(data === undefined ? undefined : JSON.stringify(data));
});
async function pair(grant = f.grant) {
const ticket = await gateway.pairing(grant), token = new URLSearchParams(new URL(ticket.url).hash.slice(1)).get('pair');
const result = await request('/mobile-api/pair', { token, name: '测试iPhone' });
assert.equal(result.status, 201, JSON.stringify(result.body));
const cookie = result.headers['set-cookie'][0].split(';')[0]; return { ticket, token, cookie, device: result.body.device };
}
return { ...f, gateway, runtime, request, pair };
}
test('observe-only device authority cannot read, configure or clear project parsing caches', async t => {
const f = await running(t), paired = await f.pair();
assert.equal((await f.request('/mobile-api/projects', undefined, { cookie: paired.cookie })).status, 200);
for (const route of ['/api/project-cache', '/api/projects/' + f.project.id + '/index-cache', '/mobile-api/project-cache', '/mobile-api/projects/' + f.project.id + '/index-cache']) {
for (const data of [undefined, { enabled: false }]) assert.equal((await f.request(route, data, { cookie: paired.cookie })).status, 404);
}
assert.equal(fs.existsSync(path.join(f.data, 'cache', 'project-index')), false);
});
test('pairing is short lived, single-use, generation-bound and validates exact permissions', t => {
const f = setup(t), pair = f.registry.pair(f.grant, 'generation');
assert.match(pair.token, /^[A-Za-z0-9_-]{43}$/);
assert.throws(() => f.registry.consume(pair.token, 'phone', 'other'), /无效/);
const accepted = f.registry.consume(pair.token, 'phone', 'generation');
assert.equal(f.registry.authenticate(accepted.token).id, accepted.device.id);
assert.throws(() => f.registry.consume(pair.token, 'phone', 'generation'), /无效/);
const next = f.registry.pair(f.grant, 'generation'); f.advance(120001);
assert.throws(() => f.registry.consume(next.token, 'phone', 'generation'), /过期/);
assert.throws(() => f.registry.pair({ ...f.grant, permissions: { ...f.grant.permissions, execute: true } }, 'g'), /字段/);
assert.throws(() => f.registry.pair({ ...f.grant, projectIds: [randomUUID()] }, 'g'), /项目/);
});
test('device transaction failure neither consumes the ticket nor leaves a credential', t => {
const f = setup(t), pair = f.registry.pair(f.grant, 'g');
f.store.storage.faultInjector = () => { throw new Error('disk fixture'); };
assert.throws(() => f.registry.consume(pair.token, 'phone', 'g'), /disk fixture/);
assert.equal(f.registry.view().devices.length, 0);
f.store.storage.faultInjector = null;
assert.equal(f.registry.consume(pair.token, 'phone', 'g').device.name, 'phone');
});
test('devices have project and action boundaries; rotation, expiration, revocation and host changes reject old authority', t => {
const f = setup(t), pair = f.registry.pair(f.grant, 'g'), accepted = f.registry.consume(pair.token, 'phone', 'g');
const device = f.registry.authenticate(accepted.token);
assert.throws(() => f.registry.authorize(device, randomUUID()), /权限/);
assert.throws(() => f.registry.authorize(device, f.project.id, 'stop'), /权限/);
const rotated = f.registry.rotate(device);
assert.throws(() => f.registry.authenticate(accepted.token), /轮换/);
const current = f.registry.authenticate(rotated); f.registry.revoke(current.id, current.revision);
assert.throws(() => f.registry.authenticate(rotated), /撤销/);
const fresh = f.registry.consume(f.registry.pair(f.grant, 'g').token, 'other', 'g'); f.advance(25 * 3600000);
assert.throws(() => f.registry.authenticate(fresh.token), /到期/);
f.host.instanceId = randomUUID(); assert.throws(() => f.registry.authenticate(fresh.token), /宿主/);
});
test('persistent registry stores hashes and a bounded audit, never raw credentials', t => {
const f = setup(t), ticket = f.registry.pair(f.grant, 'g'), accepted = f.registry.consume(ticket.token, 'iPhone', 'g');
const text = JSON.stringify(f.store.storage.meta('mobileCompanion'));
assert.ok(!text.includes(ticket.token) && !text.includes(accepted.token));
const nextHost = { ...f.host, instanceId: randomUUID() };
const registry = new DeviceRegistry(f.store, () => nextHost);
assert.equal(registry.authenticate(accepted.token).name, 'iPhone');
assert.ok(!JSON.stringify(registry.view()).includes('credentialHash'));
});
test('real TLS verifies IP SAN, CA chain, encrypted restart and public fingerprint', async t => {
const f = await running(t), first = f.gateway.tls.publicCertificate();
assert.equal(new X509Certificate(first.bytes).ca, true);
assert.equal(companionHash(first.bytes), first.sha256);
assert.equal((await f.request('/')).status, 200);
const record = f.gateway.registry.read().tls;
assert.ok(!Buffer.from(record.ciphertext, 'base64').toString('utf8').includes('pkcs8'));
const origin = f.gateway.origin; await f.gateway.close();
await f.gateway.start({ address: '127.0.0.1', port: Number(new URL(origin).port) });
assert.equal(f.gateway.tls.publicCertificate().sha256, first.sha256);
assert.equal((await f.request('/')).status, 200);
});
test('TLS sealing failure leaves listener closed and no plaintext or successful TLS registration', async t => {
const f = setup(t), tls = new CompanionTLS(f.registry, { protect() { throw new Error('fixture encryption failure'); } });
await assert.rejects(tls.material('127.0.0.1'), /encryption/);
assert.equal(f.registry.read().tls, undefined);
});
test('HTTP pairing is concurrent single-use; secure cookie and strict origin/host checks', async t => {
const f = await running(t), ticket = await f.gateway.pairing(f.grant), token = new URLSearchParams(new URL(ticket.url).hash.slice(1)).get('pair');
assert.match(ticket.qr, /^data:image\/png;base64,/);
const attempts = await Promise.all([f.request('/mobile-api/pair', { token, name: 'first' }), f.request('/mobile-api/pair', { token, name: 'second' })]);
assert.deepEqual(attempts.map(value => value.status).sort(), [201, 401]);
const accepted = attempts.find(value => value.status === 201);
assert.match(accepted.headers['set-cookie'][0], /; Secure; HttpOnly; SameSite=Strict/);
assert.equal((await f.request('/mobile-api/pair', { token, name: 'third' }, { origin: 'https://unrelated.invalid' })).status, 403);
assert.equal((await f.request('/', undefined, { headers: { Host: 'localhost:443' } })).status, 421);
});
test('gateway never exposes management routes, arbitrary assets or unpaired project data', async t => {
const f = await running(t);
assert.equal((await f.request('/mobile-api/projects')).status, 401);
const { cookie } = await f.pair();
for (const route of ['/api/bootstrap', '/api/config', '/index.html', '/assets/desktop.js', '/src/server/index.mjs', '/mobile-api/config', '/mobile-api/projects?expand=all']) {
const result = await f.request(route, undefined, { cookie }); assert.ok(result.status >= 400, route);
assert.ok(!JSON.stringify(result.body).includes('Workbench-Token'));
}
for (const action of ['requirements', 'requirement-proofs']) {
const route = '/mobile-api/sessions/' + f.session.id + '/' + action;
assert.equal((await f.request(route, undefined, { cookie })).status, 404);
assert.equal((await f.request(route, { expected_revision: 0, note: 'attempted remote acceptance' }, { cookie })).status, 404);
}
});
test('authorized project events paginate from original seq without leaking other project events or payloads', async t => {
const f = await running(t), otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot);
const otherProject = f.store.addProject(otherRoot), other = f.store.createSession(otherProject.id);
for (let i = 0; i < 7; i++) { f.store.save(other, { detail: { secret: 'fixture-secret' } }); f.store.save(f.session); }
const { cookie } = await f.pair(); let after = 0, events = [];
for (let i = 0; i < 20; i++) {
const page = await f.request('/mobile-api/events?after=' + after + '&limit=2', undefined, { cookie }); assert.equal(page.status, 200);
events.push(...page.body.events); after = page.body.nextCursor;
if (!page.body.hasMore) break;
}
assert.ok(events.length >= 8); assert.equal(new Set(events.map(event => event.seq)).size, events.length);
assert.ok(events.every(event => event.projectId === f.project.id && !Object.hasOwn(event, 'payload')));
assert.equal((await f.request('/mobile-api/sessions/' + other.id, undefined, { cookie })).status, 403);
const projects = await f.request('/mobile-api/projects', undefined, { cookie });
assert.equal(projects.body.length, 1); assert.equal(projects.body[0].root, undefined);
});
test('remote task projection excludes raw model history, host configuration, tool input and credentials', async t => {
const f = await running(t); f.session.providerMessages.push({ role: 'system', content: 'private-model-history' });
f.session.events.push({ id: randomUUID(), label: 'check', input: { token: 'private-input' }, output: 'fixture-secret' }); f.store.save(f.session);
const { cookie } = await f.pair(), result = await f.request('/mobile-api/sessions/' + f.session.id, undefined, { cookie });
assert.equal(result.status, 200); const text = JSON.stringify(result.body);
assert.ok(!text.includes('private-model-history') && !text.includes('private-input') && !text.includes('fixture-secret'));
assert.equal(result.body.events.at(-1).output, '[REDACTED]');
});
test('permission checks apply to direct remote mutation, not only UI controls', async t => {
const f = await running(t), { cookie } = await f.pair();
f.session.taskId = randomUUID(); f.session.runId = randomUUID(); f.store.save(f.session);
const result = await f.request('/mobile-api/sessions/' + f.session.id + '/stop', { requestId: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, revision: f.session.revision }, { cookie });
assert.equal(result.status, 403);
});
test('approval fingerprint and exact task/revision are bound; duplicate requests never execute twice', async t => {
const f = await running(t), grant = { ...f.grant, permissions: { observe: true, approve: true, stop: true } }, { cookie } = await f.pair(grant);
f.session.taskId = randomUUID(); f.session.runId = randomUUID();
const approval = { id: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, projectId: f.project.id, command: 'echo owned', reason: 'fixture', status: 'pending' };
f.session.approvals.push(approval); f.runtime.waiters.set(approval.id, { sessionId: f.session.id, runId: f.session.runId }); f.store.save(f.session);
let calls = 0;
f.runtime.decide = (_session, _approval, allow) => { calls++; assert.equal(allow, true); approval.status = 'approved'; f.runtime.waiters.delete(approval.id); f.store.save(f.session); };
const value = { requestId: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, revision: f.session.revision, approvalId: approval.id, fingerprint: approvalBinding(f.session, approval), allow: true };
const route = '/mobile-api/sessions/' + f.session.id + '/approve';
assert.equal((await f.request(route, { ...value, fingerprint: '0'.repeat(64) }, { cookie })).status, 409);
const results = await Promise.all([f.request(route, value, { cookie }), f.request(route, value, { cookie })]);
assert.ok(results.every(result => result.status === 200)); assert.equal(calls, 1);
assert.equal((await f.request(route, { ...value, allow: false }, { cookie })).status, 409);
});
test('unknown original operation is recorded and never replayed after response failure', async t => {
const f = await running(t), { cookie } = await f.pair({ ...f.grant, permissions: { observe: true, approve: false, stop: true } });
f.session.taskId = randomUUID(); f.session.runId = randomUUID(); f.store.save(f.session);
let calls = 0; f.runtime.stop = async () => { calls++; throw new Error('fixture interrupted receipt'); };
const value = { requestId: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, revision: f.session.revision }, route = '/mobile-api/sessions/' + f.session.id + '/stop';
assert.equal((await f.request(route, value, { cookie })).status, 409);
const retry = await f.request(route, value, { cookie }); assert.equal(retry.body.status, 'unknown'); assert.equal(calls, 1);
const me = await f.request('/mobile-api/me', undefined, { cookie });
assert.equal(me.body.operations.at(-1).status, 'unknown'); assert.equal(me.body.operations.at(-1).requestId, value.requestId);
assert.ok(!JSON.stringify(me.body).includes('credentialHash') && !JSON.stringify(me.body).includes('"hash"'));
});
test('revocation and gateway closure reject old access without invoking runtime stop', async t => {
const f = await running(t), { cookie, device } = await f.pair(); let stopped = 0; f.runtime.stop = () => { stopped++; };
f.gateway.revoke(device.id, device.revision);
assert.equal((await f.request('/mobile-api/projects', undefined, { cookie })).status, 401);
await f.gateway.close(); assert.equal(stopped, 0); assert.equal(f.gateway.status().enabled, false);
});
test('rate limits reject pairing brute force and do not persist candidate secrets', async t => {
const f = await running(t); let last;
for (let i = 0; i < 9; i++) last = await f.request('/mobile-api/pair', { token: randomBytes(32).toString('base64url'), name: 'phone' });
assert.equal(last.status, 429); assert.equal(f.gateway.registry.view().devices.length, 0);
});
test('real Windows CurrentUser DPAPI persists only a sealed CA and reopens it', { skip: process.platform !== 'win32' }, async t => {
const f = setup(t), tls = new CompanionTLS(f.registry);
await tls.material('127.0.0.1'); const hash = tls.publicCertificate().sha256;
await new CompanionTLS(f.registry).material('127.0.0.1');
assert.equal(tls.publicCertificate().sha256, hash);
});
function realRuntime(f, command) {
const provider = new Provider(), runtime = new Runtime(f.store, provider);
provider.complete = async (_config, messages) => ({ message: messages.some(value => value.role === 'tool') ? { role: 'assistant', content: '本地夹具完成。' }
: { role: 'assistant', content: '', tool_calls: [call('owned-mobile-command', 'run_command', { command, reason: '原Runtime手机审批夹具' })] }, usage: null });
f.gateway.runtime = runtime; f.onCleanup(() => runtime.stopAll()); return runtime;
}
const fixtureCommand = source => (process.platform === 'win32' ? '& ' : '') + '"' + process.execPath + '" -e "' + source + '"';
test('mobile approval executes the original Runtime command exactly once and rejects the later desktop decision', async t => {
const f = await running(t), runtime = realRuntime(f, fixtureCommand("require('node:fs').appendFileSync('phone-approved.txt','one\\n')"));
const { cookie } = await f.pair({ ...f.grant, permissions: { observe: true, approve: true, stop: true } });
runtime.start(f.session.id, '只运行这个自有夹具命令。');
await until(() => f.session.status === 'awaiting_approval');
const before = await f.request('/mobile-api/sessions/' + f.session.id, undefined, { cookie }), approval = before.body.approvals[0];
const value = { requestId: randomUUID(), taskId: before.body.taskId, runId: before.body.runId, revision: before.body.revision,
approvalId: approval.id, fingerprint: approval.fingerprint, allow: true };
const route = '/mobile-api/sessions/' + f.session.id + '/approve';
const results = await Promise.all([f.request(route, value, { cookie }), f.request(route, value, { cookie })]);
assert.ok(results.every(value => value.status === 200));
assert.throws(() => runtime.decide(f.session.id, approval.id, true), /过期/);
await until(() => !runtime.active.has(f.session.id));
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'phone-approved.txt'), 'utf8'), 'one\n');
assert.equal(f.session.events.filter(value => value.tool === 'run_command').length, 1);
assert.equal((await f.request(route, value, { cookie })).body.duplicate, true);
});
test('desktop rejection wins a concurrent mobile request and original command remains unexecuted', async t => {
const f = await running(t), runtime = realRuntime(f, fixtureCommand("require('node:fs').writeFileSync('should-not-exist.txt','no')"));
const { cookie } = await f.pair({ ...f.grant, permissions: { observe: true, approve: true, stop: true } });
runtime.start(f.session.id, '原桌面和手机审批竞争夹具。'); await until(() => f.session.status === 'awaiting_approval');
const before = await f.request('/mobile-api/sessions/' + f.session.id, undefined, { cookie }), approval = before.body.approvals[0];
const pending = f.request('/mobile-api/sessions/' + f.session.id + '/approve', { requestId: randomUUID(), taskId: before.body.taskId,
runId: before.body.runId, revision: before.body.revision, approvalId: approval.id, fingerprint: approval.fingerprint, allow: true }, { cookie });
runtime.decide(f.session.id, approval.id, false);
assert.equal((await pending).status, 409); await until(() => !runtime.active.has(f.session.id));
assert.equal(fs.existsSync(path.join(f.projectRoot, 'should-not-exist.txt')), false);
});
test('phone stop waits for the original process exit and prevents later heartbeats and command replay', async t => {
const f = await running(t), runtime = realRuntime(f, fixtureCommand("setInterval(()=>require('node:fs').appendFileSync('heartbeat.txt','x'),80)"));
const { cookie } = await f.pair({ ...f.grant, permissions: { observe: true, approve: true, stop: true } });
runtime.start(f.session.id, '手机停止自有心跳进程。'); await until(() => f.session.status === 'awaiting_approval');
runtime.decide(f.session.id, f.session.approvals.at(-1).id, true);
await until(() => fs.existsSync(path.join(f.projectRoot, 'heartbeat.txt')));
const before = await f.request('/mobile-api/sessions/' + f.session.id, undefined, { cookie });
const value = { requestId: randomUUID(), taskId: before.body.taskId, runId: before.body.runId, revision: before.body.revision };
const route = '/mobile-api/sessions/' + f.session.id + '/stop', result = await f.request(route, value, { cookie });
assert.equal(result.status, 200); assert.equal(runtime.active.has(f.session.id), false);
const bytes = fs.readFileSync(path.join(f.projectRoot, 'heartbeat.txt'));
await new Promise(resolve => setTimeout(resolve, 250));
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'heartbeat.txt')), bytes);
assert.equal((await f.request(route, value, { cookie })).body.duplicate, true);
assert.equal(f.session.events.filter(value => value.tool === 'run_command').length, 1);
});
test('Run replacement during parent-stop wait is rejected rather than stopping the new Run', async t => {
const f = setup(t), runtime = new Runtime(f.store, new Provider());
f.session.taskId = randomUUID(); f.session.runId = randomUUID(); const expected = { taskId: f.session.taskId, runId: f.session.runId };
let stopped = 0;
runtime.workers = { async stopParent() { f.session.runId = randomUUID(); runtime.active.set(f.session.id, { controller: { abort() { stopped++; } }, promise: Promise.resolve() }); } };
await assert.rejects(runtime.stop(f.session.id, expected), /已变化/); assert.equal(stopped, 0);
runtime.active.clear();
});
test('diff pages preserve source SHA; report reads verify blobs and exclude unrelated artifact kinds', async t => {
const f = await running(t), task = createTask(f.session, '原差异及工件夹具', f.store.config);
const run = createRun(f.session, task, f.store.config);
f.store.core.task.set(task.id, task); f.store.core.run.set(run.id, run); f.session.taskId = task.id; f.session.runId = run.id;
const text = '+line\\n'.repeat(17000), change = { id: randomUUID(), taskId: f.session.taskId, sha256: companionHash('new file'), baseSha256: companionHash('old'), path: 'long/path.ts', diff: [{ value: text, added: true }] };
f.session.changes.push(change); f.store.save(f.session);
const report = f.store.artifactStore.write(f.session, null, '# 原报告\nfixture-secret\n', { kind: 'delivery-report-markdown', mediaType: 'text/markdown' });
const raw = f.store.artifactStore.write(f.session, null, 'never expose tool input', { kind: 'tool-result' });
f.store.core.artifact.set(report.id, report); f.store.core.artifact.set(raw.id, raw); f.store.save(f.session);
const { cookie } = await f.pair(); let assembled = '', offset = 0, hash;
for (let page = 0; page < 20; page++) {
const value = await f.request('/mobile-api/sessions/' + f.session.id + '/changes/' + change.id + '?offset=' + offset, undefined, { cookie }); assert.equal(value.status, 200);
assert.equal(value.body.sourceSha256, change.sha256); if (hash) assert.equal(value.body.sha256, hash); hash = value.body.sha256;
assembled += value.body.text; offset = value.body.nextOffset; if (!value.body.hasMore) break;
}
assert.equal(companionHash(assembled), hash); assert.ok(assembled.length > 100000);
const route = '/mobile-api/sessions/' + f.session.id + '/reports/';
const delivered = await f.request(route + report.id, undefined, { cookie }); assert.equal(delivered.status, 200);
assert.equal(delivered.body.sourceSha256, report.sha256); assert.equal(delivered.body.sha256, companionHash(delivered.body.text)); assert.equal(delivered.body.redacted, true);
assert.equal((await f.request(route + raw.id, undefined, { cookie })).status, 404);
fs.appendFileSync(path.join(f.store.artifactStore.root, report.relative), 'corrupt');
assert.equal((await f.request(route + report.id, undefined, { cookie })).status, 500);
});
test('hot upgrade refuses an active gateway and restores closed gateway ownership on failure', async t => {
const f = setup(t), provider = new Provider(), runtime = new Runtime(f.store, provider), server = http.createServer((_request, response) => response.end('old'));
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
f.onCleanup(() => new Promise(resolve => server.close(resolve)));
const context = { server, store: f.store, provider, runtime, token: 'test-upgrade-owned', clients: new Set(), started: Date.now() };
const gateway = { server: { listening: true }, busy: false }; server.workbenchCompanion = gateway;
assert.throws(() => installUpgrade(context, { appDirectory: workspace, updateServiceInfo: false }), /Close the mobile/);
gateway.server = null;
f.store.claimRuntime = () => { throw new Error('fixture upgrade failure'); };
assert.throws(() => installUpgrade(context, { appDirectory: workspace, updateServiceInfo: false }), /fixture upgrade failure/);
assert.equal(server.workbenchCompanion, gateway);
});
test('damaged device permissions or expiry fail closed and never overwrite the original registry', t => {
const f = setup(t), accepted = f.registry.consume(f.registry.pair(f.grant, 'g').token, 'phone', 'g');
const original = f.store.storage.meta('mobileCompanion');
for (const damage of [value => { value.devices[accepted.device.id].permissions.stop = 'true'; }, value => { value.devices[accepted.device.id].expiresAt = 'invalid'; }]) {
const changed = structuredClone(original); damage(changed); f.store.storage.setMeta('mobileCompanion', changed);
assert.throws(() => f.registry.authenticate(accepted.token), /原手机登记无法核对/);
assert.throws(() => f.registry.initialize(), /原手机登记无法核对/);
assert.deepEqual(f.store.storage.meta('mobileCompanion'), changed);
}
});
test('device revocation destroys its actual authenticated TLS connection', async t => {
const f = await running(t), { cookie, device } = await f.pair();
const agent = new https.Agent({ keepAlive: true, ca: f.gateway.registry.read().tls.certificate });
let socket;
await new Promise((resolve, reject) => {
const request = https.get(f.gateway.origin + '/mobile-api/me', { agent, servername: '', headers: { Cookie: cookie } }, response => {
socket = response.socket; response.resume(); response.on('end', resolve);
}); request.on('error', reject);
});
assert.ok(socket && !socket.destroyed); f.gateway.revoke(device.id, device.revision);
await until(() => socket.destroyed); agent.destroy();
});
test('companion observes only current Task screenshots, verifies original PNG bytes and loses access immediately on revoke',async t=>{
const f=await running(t),manager=new InputAttachments(f.store),png=new PNG({width:20,height:10});png.data.fill(255);
const row=manager.upload(f.session,{name:'工程截图.png',mediaType:'image/png',base64:PNG.sync.write(png).toString('base64')});
const run=f.store.beginRun(f.session,'Inspect',false,f.store.config,{inputAttachments:manager.validateSelection(f.session,[row.id])});
f.session.status='paused';f.store.finishRun(f.session,run,'context_limit');const id=f.store.core.task.get(run.taskId).attachmentIds[0];
const {cookie,device}=await f.pair(),route='/mobile-api/sessions/'+f.session.id+'/input-attachments/'+id;
const image=await f.request(route+'/image',undefined,{cookie});assert.equal(image.status,200);assert.equal(companionHash(image.bytes),row.sentSha256);
const original=await f.request(route+'/original',undefined,{cookie});assert.equal(original.status,200);assert.equal(companionHash(original.bytes),row.sha256);
const projection=await f.request('/mobile-api/sessions/'+f.session.id,undefined,{cookie});assert.equal(projection.body.inputAttachments[0].id,id);
assert.equal(projection.body.inputAttachments[0].originalArtifactId,undefined);assert.equal(projection.body.providerMessages,undefined);
const draft=manager.upload(f.session,{name:'draft.png',mediaType:'image/png',base64:PNG.sync.write(png).toString('base64')});
assert.equal((await f.request('/mobile-api/sessions/'+f.session.id+'/input-attachments/'+draft.id+'/image',undefined,{cookie})).status,404);
const other=f.store.createSession(f.project.id);assert.notEqual((await f.request('/mobile-api/sessions/'+other.id+'/input-attachments/'+id+'/image',undefined,{cookie})).status,200);
assert.equal((await f.request(route+'/image',{}, {cookie})).status,404);
f.registry.revoke(device.id,device.revision);assert.equal((await f.request(route+'/image',undefined,{cookie})).status,401);
});
test('mobile browser observation is current Task/project scoped, preserves original PNG and exposes no browser control or profile data',async t=>{
const f=await running(t),run=f.store.beginRun(f.session,'核对网页原观察',false,f.store.config);f.session.status='paused';f.store.finishRun(f.session,run);
const png=PNG.sync.write(new PNG({width:4,height:4})),artifact=f.store.artifactStore.write(f.session,run,png,{kind:'browsing-screenshot',mediaType:'image/png'});f.store.core.artifact.set(artifact.id,artifact);
const row={id:randomUUID(),sessionId:f.session.id,projectId:f.project.id,taskId:f.session.taskId,runId:f.session.runId,tabId:randomUUID(),url:'https://example.com/',title:'所属原页面',createdAt:new Date().toISOString(),revision:'original:1',screenshotArtifactId:artifact.id,screenshotSha256:artifact.sha256,scope:{complete:true}};
f.store.putRecord('browsing_observation',f.session,row,'browsing.fixture');const{cookie,device}=await f.pair(),route='/mobile-api/sessions/'+f.session.id+'/browser-observations/'+row.id+'/image';
const image=await f.request(route,undefined,{cookie});assert.equal(image.status,200);assert.deepEqual(image.bytes,png);assert.equal(image.headers['x-content-sha256'],artifact.sha256);
const projected=(await f.request('/mobile-api/sessions/'+f.session.id,undefined,{cookie})).body;assert.equal(projected.browserObservations[0].id,row.id);assert.equal(projected.browserObservations[0].screenshotArtifactId,undefined);assert.equal(projected.browsingProfiles,undefined);
assert.equal((await f.request(route,{}, {cookie})).status,404);assert.equal((await f.request('/mobile-api/sessions/'+f.session.id+'/browsing/tabs',{}, {cookie})).status,404);
const other=f.store.createSession(f.project.id);assert.notEqual((await f.request('/mobile-api/sessions/'+other.id+'/browser-observations/'+row.id+'/image',undefined,{cookie})).status,200);
f.registry.revoke(device.id,device.revision);assert.equal((await f.request(route,undefined,{cookie})).status,401);
});
@@ -0,0 +1,40 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { fixture, compatibleFixture } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { createRequestHandler } from '../src/server/app.mjs';
test('actual config HTTP failure preserves the committed selection, config and volatile credential before a successful retry', async t => {
const f = fixture(t), provider = new Provider(), runtime = new Runtime(f.store, provider), server = http.createServer();
const token = 'owned-config-transaction-token', clients = new Set();
const context = { appDirectory: process.cwd(), store: f.store, provider, runtime, server, token, clients,
started: Date.now(), exampleProject: f.project, getPort: () => server.address().port, shutdown: () => {} };
server.on('request', createRequestHandler(context));
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
f.onCleanup(async () => { await runtime.stopAll(); server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
const api = async (route, input) => {
const response = await fetch('http://127.0.0.1:' + server.address().port + '/api' + route, { method: input === undefined ? 'GET' : 'POST',
headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': token, Connection: 'close' }, ...(input === undefined ? {} : { body: JSON.stringify(input) }) });
const value = await response.json(); if (!response.ok) throw Object.assign(new Error(value.error), { status: response.status }); return value;
};
const model = await compatibleFixture(t, () => ({ data: [{ id: 'owned-config-model' }] }));
const config = { mode: 'compatible', model: 'owned-config-model', baseUrl: model.baseUrl };
await api('/config', { ...config, apiKey: 'owned-original-config-key' });
const connection = await api('/model-center/connections', { name: 'Owned selected connection', model: 'owned-reserve', baseUrl: model.baseUrl, apiKey: 'owned-reserve-config-key' });
await api('/model-center/select', { type: 'connection', id: connection.id });
const before = structuredClone(f.store.config), registry = structuredClone(provider.hub.state);
f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('owned config disk failure'); };
await assert.rejects(api('/config', { ...config, mode: 'demo', apiKey: 'owned-new-config-key' }), /owned config disk failure/);
assert.deepEqual(f.store.config, before); assert.deepEqual(provider.hub.state, registry);
assert.deepEqual(f.store.storage.meta('config'), before); assert.deepEqual(f.store.storage.meta('modelRegistry'), registry);
assert.equal(provider.apiKey, 'owned-original-config-key');
f.store.storage.faultInjector = null;
await api('/config/models', {}); assert.equal(model.requests.at(-1).headers.authorization, 'Bearer owned-original-config-key');
await assert.rejects(api('/config', { ...config, apiKey: 123 }), /凭据格式无效/);
assert.deepEqual(f.store.config, before); assert.deepEqual(provider.hub.state, registry);
await api('/config', { ...config, apiKey: 'owned-new-config-key' });
assert.equal(provider.hub.state.selected.type, 'legacy'); assert.equal(f.store.config.configRevision, before.configRevision + 1);
await api('/config/models', {}); assert.equal(model.requests.at(-1).headers.authorization, 'Bearer owned-new-config-key');
});
@@ -0,0 +1,111 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { randomUUID } from 'node:crypto';
import { fixture } from './helpers.mjs';
import { createEngineFixture } from '../scripts/engine-fixtures.mjs';
import { CONTROLLED_CRITICAL_FILES, resolveControlledInstallation, assertControlledInstallation,
controlledToolCatalogue, controlledDigest, controlledPatch } from '../src/core/engines/controlled-installation.mjs';
import { CONTROLLED_DISABLED_PLUGINS } from '../src/core/engines/controlled-plugin.mjs';
import { ControlledDeepSeekSession } from '../src/core/engines/controlled-session.mjs';
const tools = [{ name: 'read_file', description: 'Read the owned project file.', parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] } }];
function installation(t) {
const f = fixture(t), root = createEngineFixture(path.join(f.root, 'harness'));
for (const file of CONTROLLED_CRITICAL_FILES) {
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'export const fixture = true;\n');
}
return { ...f, root, installation: resolveControlledInstallation(f.root, root) };
}
test('controlled installation pins the native kernel and bridge independently of the existing connection-probe identity', t => {
const f = installation(t), original = f.installation;
assert.equal(original.controlledStatus, 'available'); assert.equal(original.controlledInputs.length, CONTROLLED_CRITICAL_FILES.length);
assert.equal(original.controlledCapabilities.nativeModelCredentials, false);
assert.equal(original.controlledCapabilities.singleSessionCancel, false);
fs.appendFileSync(path.join(f.root, CONTROLLED_CRITICAL_FILES[0]), '// changed runtime\n');
const current = resolveControlledInstallation(f.root, f.root);
assert.equal(current.identitySha256, original.identitySha256); assert.notEqual(current.controlledSha256, original.controlledSha256);
assert.throws(() => assertControlledInstallation(original), /工具内核或叠加插件已变化/);
});
test('controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract', () => {
const patch = controlledPatch('owned-plugin.mjs', { fixture: true });
assert.deepEqual(patch.slice(0, -1), CONTROLLED_DISABLED_PLUGINS.map(id => ({ id, disabled: true })));
assert.deepEqual(patch.at(-1).insert[0], { id: 'agent-workbench-controlled-bridge', name: 'owned-plugin.mjs', config: { fixture: true } });
assert.ok(CONTROLLED_DISABLED_PLUGINS.includes('persistent-pwsh')); assert.ok(CONTROLLED_DISABLED_PLUGINS.includes('tool-fs'));
});
test('owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations', () => {
assert.equal(controlledDigest(controlledToolCatalogue(tools)), controlledDigest(controlledToolCatalogue([{ function: tools[0] }])));
assert.throws(() => controlledToolCatalogue([...tools, tools[0]]), /重复/);
assert.throws(() => controlledToolCatalogue([{ ...tools[0], parameters: { type: 'array' } }]), /格式无效/);
});
async function bridge(t, { onTool = async () => ({ observed: true }), onModel = async () => ({ observed: true }), maximumFrameBytes = 4096 } = {}) {
const f = installation(t), driver = new ControlledDeepSeekSession(f.installation, { maximumFrameBytes });
const owner = Object.fromEntries(['sessionId', 'projectId', 'taskId', 'runId', 'jobId'].map(key => [key, randomUUID()]));
driver.owner = owner; driver.sdkSessionId = randomUUID(); driver.client = { pid: 11717 };
driver.catalogueNames = new Set(['read_file']); driver.callIds = new Set(); driver.catalogueSha256 = controlledDigest(tools);
driver.config = { model: 'fixture-model', maxOutputTokens: 512 }; driver.verifyLaunch = () => {};
driver.report = { bridgeRequests: [], modelRequests: 0, toolRequests: 0 };
const controller = new AbortController(), authentication = 'Bearer owned-test-token';
const server = http.createServer((request, response) => {
driver.handleBridge(request, response, { authentication, onTool, onModel, signal: controller.signal }).catch(error => {
response.writeHead(error.status ?? 409, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: error.message }));
});
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
f.onCleanup(() => new Promise(resolve => { controller.abort(); server.closeAllConnections(); server.close(resolve); }));
const post = (route, data, overrides = {}) => fetch('http://127.0.0.1:' + server.address().port + route, {
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: authentication, ...overrides.headers },
body: JSON.stringify({ owner, sessionId: driver.sdkSessionId, pid: driver.client.pid, data, ...overrides.body }),
});
return { driver, post, owner, controller };
}
test('bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch', async t => {
let calls = 0; const b = await bridge(t, { onTool: async () => { calls++; return {}; } });
const data = { callId: 'owned-call', name: 'read_file', arguments: { path: 'README.md' } };
for (const overrides of [{ headers: { Authorization: 'Bearer foreign' } }, { headers: { Origin: 'http://example.invalid' } },
{ body: { owner: { ...b.owner, runId: randomUUID() } } }, { body: { sessionId: randomUUID() } }, { body: { pid: 31313 } }]) {
assert.equal((await b.post('/tool', data, overrides)).status, 403);
}
assert.equal(calls, 0); assert.equal(b.driver.report.toolRequests, 0);
b.driver.client.exited = true;
assert.equal((await b.post('/tool', data)).status, 403); assert.equal(calls, 0);
});
test('bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect', async t => {
let calls = 0; const b = await bridge(t, { onTool: async () => { calls++; return { read: true }; } });
const data = { callId: 'call-1', name: 'read_file', arguments: { path: 'README.md' } };
assert.equal((await b.post('/tool', data)).status, 200);
assert.equal((await b.post('/tool', data)).status, 409);
assert.equal((await b.post('/tool', { ...data, callId: 'call-2', name: 'pwsh' })).status, 400);
assert.equal((await b.post('/tool', { ...data, callId: 'call-3', arguments: [] })).status, 400);
b.driver.verifyLaunch = () => { throw Object.assign(new Error('source changed'), { status: 409 }); };
assert.equal((await b.post('/tool', { ...data, callId: 'call-4' })).status, 409);
assert.equal(calls, 1); assert.equal(b.driver.report.bridgeRequests.length, 1);
});
test('bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity', async t => {
let calls = 0; const b = await bridge(t, { onModel: async () => { calls++; return { message: { role: 'assistant', content: 'owned' } }; } });
const data = { messages: [], system: null, tools, toolHistory: null, model: 'fixture-model', maxTokens: 256 };
assert.equal((await b.post('/model', { ...data, maxTokens: 513 })).status, 400);
assert.equal((await b.post('/model', { ...data, model: 'other' })).status, 400);
assert.equal((await b.post('/model', { ...data, tools: [{ ...tools[0], description: 'changed' }] })).status, 400);
const response = await b.post('/model', data); assert.equal(response.status, 200);
assert.equal(calls, 1); assert.equal(b.driver.report.modelRequests, 1);
const trace = b.driver.report.bridgeRequests[0]; assert.equal(trace.status, 'completed');
assert.equal(trace.resultSha256, controlledDigest(await response.json())); assert.deepEqual(trace.data, data);
});
test('stopped and oversized bridge requests never reach a local model or tool callback', async t => {
let calls = 0; const b = await bridge(t, { maximumFrameBytes: 512, onTool: async () => { calls++; return {}; } });
const data = { callId: 'large', name: 'read_file', arguments: { path: 'x'.repeat(1000) } };
assert.equal((await b.post('/tool', data)).status, 413);
b.controller.abort(); assert.equal((await b.post('/tool', { ...data, arguments: {} })).status, 403);
assert.equal(calls, 0);
});
@@ -0,0 +1,123 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { randomBytes } from 'node:crypto';
import { fixture } from './helpers.mjs';
import { Store } from '../src/server/store.mjs';
import { Provider } from '../src/server/provider.mjs';
import { ConfigImporter } from '../src/core/models/config-import.mjs';
import { protectCredential, unprotectCredential } from '../src/core/desktop/windows-dpapi.mjs';
const create = f => { const provider = new Provider(); provider.setKey(''); return { provider, hub: provider.modelHub(f.store) }; };
const add = (hub, name, extra = {}) => hub.upsert({ name, model: name, baseUrl: 'http://127.0.0.1:12001/v1', apiKey: 'owned-vault-' + name, ...extra });
const reopen = f => { f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); const provider = new Provider(); provider.setKey(''); return { store, provider, hub: provider.modelHub(store) }; };
test('a Windows virtual data-directory name binds new storage and credentials to its actual physical root', t => {
const f = fixture(t), logical = path.join(f.root, 'virtual-local-data'), physical = path.join(f.root, 'physical-local-data');
fs.mkdirSync(logical); fs.mkdirSync(physical);
const realpath = fs.realpathSync.native;
t.mock.method(fs.realpathSync, 'native', (file, ...options) => path.resolve(file) === logical ? physical : realpath(file, ...options));
const store = new Store(logical); f.onCleanup(() => store.close());
const provider = new Provider(); provider.setKey(''); const hub = provider.modelHub(store);
assert.equal(store.directory, physical); assert.equal(store.storage.filename, path.join(physical, 'workbench.sqlite'));
assert.equal(hub.vault().view(hub).unavailable, false);
assert.equal(fs.existsSync(path.join(physical, 'workbench.sqlite')), true); assert.deepEqual(fs.readdirSync(logical), []);
});
test('physical data-root adoption still rejects explicit junctions at the directory or any ancestor', t => {
const f = fixture(t), direct = path.join(f.root, 'direct-data-link'), parent = path.join(f.root, 'parent-link');
const target = path.join(f.root, 'parent-target'); fs.mkdirSync(path.join(target, 'data'), { recursive: true });
fs.symlinkSync(f.data, direct, 'junction'); fs.symlinkSync(target, parent, 'junction');
const database = f.store.storage.database, original = f.store.directory;
for (const alias of [direct, path.join(parent, 'data')]) {
assert.throws(() => new Store(alias), /符号链接或目录联接/);
f.store.directory = alias; assert.throws(() => f.store.ensureStorage(), /符号链接或目录联接/);
assert.equal(f.store.directory, alias); assert.equal(f.store.storage.database, database);
}
f.store.directory = original; assert.deepEqual(fs.readdirSync(path.join(target, 'data')), []);
});
test('actual Windows CurrentUser DPAPI roundtrips owned UTF-8 and rejects damaged bytes and wrong entropy', () => {
const plain = Buffer.from('自有测试凭据 owned-key-only', 'utf8'), entropy = randomBytes(32), encrypted = protectCredential(plain, entropy);
assert.notDeepEqual(encrypted, plain); assert.deepEqual(unprotectCredential(encrypted, entropy), plain);
assert.throws(() => unprotectCredential(encrypted, randomBytes(32)), /无法处理/);
encrypted[encrypted.length - 1] ^= 255; assert.throws(() => unprotectCredential(encrypted, entropy), /无法处理/);
});
test('only explicitly supplied keys persist; new provider restores original revisions and public views contain no key', t => {
const f = fixture(t), { provider, hub } = create(f); provider.setKey('owned-unsaved-legacy');
const volatile = add(hub, 'volatile'), saved = add(hub, 'saved', { persistKey: true });
const ledger = f.store.storage.meta('modelCredentials'); assert.deepEqual(Object.keys(ledger.entries), [saved.id]);
assert.equal(hub.credential(saved).revision, ledger.entries[saved.id].credentialRevision);
assert.equal(JSON.stringify(ledger).includes('owned-vault-'), false); assert.equal(JSON.stringify(hub.vault().view(hub)).includes('owned-vault-'), false);
assert.equal(provider.apiKey, 'owned-unsaved-legacy');
const fresh = reopen(f); assert.equal(fresh.provider.hasKey(), false);
assert.equal(fresh.hub.status(fresh.hub.connection(volatile.id)).hasKey, false);
assert.equal(fresh.hub.credential(fresh.hub.connection(saved.id)).key, 'owned-vault-saved');
assert.equal(fresh.hub.credential(fresh.hub.connection(saved.id)).revision, hub.credential(saved).revision);
assert.equal(fresh.hub.vault().status(saved).status, 'saved');
});
test('registry and cipher commit failure restores original routing, keys, block and every database row', t => {
const f = fixture(t), { provider, hub } = create(f), saved = add(hub, 'before', { persistKey: true });
const originalState = structuredClone(hub.state), keyReference = provider.credentials.get(saved.id), block = f.store.storage.meta('modelCredentials');
const snapshot = () => ['metadata', 'projects', 'sessions', 'records', 'events', 'outbox'].map(table => f.store.storage.database.prepare('SELECT * FROM ' + table + ' ORDER BY rowid').all());
const originalRows = snapshot();
f.store.storage.faultInjector = point => { if (point === 'before_commit') throw new Error('owned vault commit failure'); };
assert.throws(() => hub.upsert({ ...saved, model: 'after', apiKey: 'owned-after-key', persistKey: true }), /owned vault commit failure/);
assert.deepEqual(hub.state, originalState); assert.equal(provider.credentials.get(saved.id), keyReference);
assert.deepEqual(hub.vault().state, block); assert.deepEqual(snapshot(), originalRows);
f.store.storage.faultInjector = null;
});
test('a two-key import with a protection failure rolls back all registry and cipher entries and retains its preview', t => {
const f = fixture(t), { hub } = create(f), vault = hub.vault(), importer = new ConfigImporter(hub), filename = path.join(f.root, 'owned-import.json');
fs.writeFileSync(filename, JSON.stringify({ schema: 1, connections: ['one', 'two'].map(name => ({ name, model: name, baseUrl: 'http://127.0.0.1:12002/v1', apiKey: 'owned-import-' + name })) }));
const preview = importer.preview('workbench-json', filename), state = structuredClone(hub.state); let calls = 0;
vault.protect = (bytes, entropy) => { if (++calls === 2) throw new Error('owned second protect failure'); return protectCredential(bytes, entropy); };
assert.throws(() => importer.apply(preview.id, false, true), /owned second protect failure/);
assert.deepEqual(hub.state, state); assert.deepEqual(vault.state.entries, {}); assert.equal(hub.provider.credentials.size, 0); assert.equal(importer.previews.has(preview.id), true);
vault.protect = protectCredential; const accepted = importer.apply(preview.id, false, true);
assert.equal(accepted.imported.length, 2); assert.equal(Object.keys(vault.state.entries).length, 2);
});
test('connection revision changes retain the original saved block and report a source mismatch on restart', t => {
const f = fixture(t), { hub } = create(f), original = add(hub, 'original', { persistKey: true }), block = structuredClone(hub.vault().state.entries[original.id]);
const changed = hub.upsert({ ...original, model: 'changed' }); assert.equal(hub.vault().status(changed).status, 'source_changed');
assert.deepEqual(hub.vault().state.entries[original.id], block);
const fresh = reopen(f); assert.equal(fresh.hub.status(fresh.hub.connection(original.id)).hasKey, false); assert.equal(fresh.hub.vault().status(fresh.hub.connection(original.id)).status, 'source_changed');
});
test('disable skips real restart loading, explicit enable restores, delete preserves memory and forget removes both', t => {
const f = fixture(t), { hub } = create(f), saved = add(hub, 'toggle', { persistKey: true });
hub.manageCredential({ id: saved.id, revision: saved.revision, action: 'disable' }); assert.equal(hub.status(saved).hasKey, true);
const fresh = reopen(f), connection = fresh.hub.connection(saved.id); assert.equal(fresh.hub.status(connection).hasKey, false); assert.equal(fresh.hub.vault().status(connection).status, 'disabled');
fresh.hub.manageCredential({ id: saved.id, revision: saved.revision, action: 'enable' }); assert.equal(fresh.hub.status(connection).hasKey, true);
fresh.hub.manageCredential({ id: saved.id, revision: saved.revision, action: 'delete' }); assert.equal(fresh.hub.status(connection).hasKey, true); assert.equal(fresh.hub.vault().status(connection).status, 'not_saved');
fresh.hub.manageCredential({ id: saved.id, revision: saved.revision, action: 'forget' }); assert.equal(fresh.hub.status(connection).hasKey, false);
});
test('corrupt saved block and another SID do not load; failures are visible without returning ciphertext or plaintext', t => {
const f = fixture(t), { hub } = create(f), saved = add(hub, 'corrupt', { persistKey: true });
const state = f.store.storage.meta('modelCredentials'); state.entries[saved.id].ciphertext = 'AAAA'; f.store.storage.transaction(() => f.store.storage.setMeta('modelCredentials', state));
const fresh = reopen(f), connection = fresh.hub.connection(saved.id); assert.equal(fresh.hub.status(connection).hasKey, false); assert.equal(fresh.hub.vault().status(connection).status, 'decrypt_failed');
const publicView = fresh.hub.vault().view(fresh.hub); assert.equal(JSON.stringify(publicView).includes('AAAA'), false); assert.equal(JSON.stringify(publicView).includes('owned-vault-corrupt'), false);
fresh.hub.vault().state.entries[saved.id].userSid = 'S-1-5-21-1-2-3-1000'; assert.equal(fresh.hub.vault().status(connection).status, 'user_mismatch');
});
test('legacy persistence requires a new explicit key and binds config and credential revisions; stale requests cannot save', t => {
const f = fixture(t), { provider, hub } = create(f), legacy = hub.connection('legacy-default'); provider.setKey('owned-original-memory');
assert.throws(() => hub.manageCredential({ id: legacy.id, revision: legacy.revision, action: 'save' }), /重新填写/); assert.equal(provider.apiKey, 'owned-original-memory');
assert.throws(() => hub.manageCredential({ id: legacy.id, revision: legacy.revision - 1, action: 'save', apiKey: 'owned-explicit' }), /刷新/);
hub.manageCredential({ id: legacy.id, revision: legacy.revision, action: 'save', apiKey: 'owned-explicit' });
const fresh = reopen(f); assert.equal(fresh.provider.apiKey, 'owned-explicit'); assert.equal(fresh.provider.legacyCredentialRevision, 2);
fresh.store.saveConfig({ model: 'new-model' }); assert.equal(fresh.hub.vault().status(fresh.hub.connection(legacy.id)).status, 'source_changed');
});
test('owned plaintext keys never reach SQLite pages, WAL, config or public execution exports', t => {
const f = fixture(t), { hub } = create(f), key = 'OWNED_VAULT_PLAINTEXT_SENTINEL_42fdbedc67b9420f'; add(hub, 'leak-check', { apiKey: key, persistKey: true });
const needles = [Buffer.from(key), Buffer.from(key, 'utf16le'), Buffer.from(Buffer.from(key).toString('base64'))];
for (const file of fs.readdirSync(f.data)) if (fs.statSync(path.join(f.data, file)).isFile()) { const bytes = fs.readFileSync(path.join(f.data, file)); for (const needle of needles) assert.equal(bytes.includes(needle), false, file); }
assert.equal(JSON.stringify(f.store.publicSession(f.session)).includes(key), false); assert.equal(JSON.stringify(hub.view()).includes(key), false);
});
@@ -0,0 +1,61 @@
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} from './helpers.mjs';
import {createDeliveryProject,applyDeliveryFix,deliveryGit} from './fixtures/delivery-project.mjs';
import {CheckRunner} from '../src/core/check-runner.mjs';
import {taskDeliveryProgress} from '../src/core/context/delivery-progress.mjs';
test('reviewed application invalidates old checks, names current SHA and advances through a new approved check to Git preview',async t=>{
const f=fixture(t),run=createDeliveryProject(f),checks=new CheckRunner(f.store);f.onCleanup(()=>checks.stopAll());
const snapshot=()=>taskDeliveryProgress(f.store,f.session,run,{checks});
assert.equal(await snapshot(),null);const before=deliveryGit(f,['rev-parse','HEAD']);
const original=await checks.plan(f.session);await(await checks.start(f.session,original.id)).promise;
applyDeliveryFix(f);let progress=await snapshot();assert.equal(progress.stage,'needs_current_check_plan');
assert.equal(progress.latest_check.stale,true);assert.ok(progress.files.every(file=>file.current&&file.current_sha256===file.applied_sha256));
assert.deepEqual(progress.next_action,{tool:'plan_checks',arguments:{names:['test','build']}});
const plan=await checks.plan(f.session);progress=await snapshot();assert.equal(progress.stage,'ready_for_checks');
assert.deepEqual(progress.next_action,{tool:'run_checks',arguments:{plan_id:plan.id}});assert.equal(f.store.core.check_job.size,1,'snapshot never executes a check');
const passed=await(await checks.start(f.session,plan.id)).promise;assert.equal(passed.status,'passed');
progress=await snapshot();assert.equal(progress.stage,'ready_for_git_preview');assert.equal(progress.latest_check.current_source,true);
assert.equal(progress.next_action.tool,'plan_delivery');assert.equal(deliveryGit(f,['rev-parse','HEAD']),before,'snapshot cannot publish a commit');
fs.appendFileSync(path.join(f.projectRoot,'src/math.mjs'),'// owner changed\n');progress=await snapshot();
assert.equal(progress.stage,'applied_files_need_review');assert.equal(progress.next_action,null);assert.equal(progress.files.find(file=>file.path==='src/math.mjs').current,false);
});
test('skip-tests, foreign task, unknown operations, diagnostic and parent review retain their boundaries',async t=>{
const f=fixture(t),run=createDeliveryProject(f),checks=new CheckRunner(f.store);f.onCleanup(()=>checks.stopAll());applyDeliveryFix(f);
const task=f.store.core.task.get(f.session.taskId);task.contract={...task.contract,checks:'skip'};
const snapshot=options=>taskDeliveryProgress(f.store,f.session,run,{checks,...options});
assert.equal((await snapshot()).stage,'checks_not_requested');assert.equal((await snapshot()).next_action.tool,'delivery_report');
assert.equal((await snapshot({worker:true})).next_action,null);assert.equal(f.store.core.check_plan.size,0);
assert.equal(await taskDeliveryProgress(f.store,f.session,{...run,purpose:'diagnostic'},{checks}),null);
f.session.changes[0].taskId='foreign-task';assert.equal((await snapshot()).files.length,1);
const id='owned-unknown';f.store.core.tool.set(id,{id,sessionId:f.session.id,projectId:f.project.id,taskId:task.id,requiresVerification:true});
assert.equal((await snapshot()).stage,'awaiting_review_or_verification');assert.equal((await snapshot()).next_action,null);
});
test('before the first change, real baseline failures persist across later reads; passed baselines and changed sources cannot certify delivery',async t=>{
const f=fixture(t),run=createDeliveryProject(f),checks=new CheckRunner(f.store);f.onCleanup(()=>checks.stopAll());
const snapshot=()=>taskDeliveryProgress(f.store,f.session,run,{checks});
assert.equal(await snapshot(),null);
const plan=await checks.plan(f.session),job=await(await checks.start(f.session,plan.id)).promise;
assert.equal(job.status,'failed');
let current=await snapshot();assert.equal(current.has_applied_changes,false);assert.equal(current.stage,'checks_need_repair');
assert.equal(current.latest_check.id,job.id);assert.equal(current.latest_check.current_source,true);
assert.ok(current.latest_check.results.every(result=>result.log_artifact_id&&result.log_sha256));
const later=randomUUID();f.store.core.tool.set(later,{schemaVersion:1,id:later,taskId:run.taskId,runId:run.id,sessionId:f.session.id,projectId:f.project.id,tool:'read_file',state:'completed'});
assert.deepEqual(await snapshot(),current);
fs.writeFileSync(path.join(f.projectRoot,'src/add.mjs'),'export const add = (a,b) => a+b;\n');
current=await snapshot();assert.equal(current.latest_check.current_source,false);assert.notEqual(current.stage,'ready_for_git_preview');
// An actual current passed baseline still has no reviewed implementation.
const updated=await checks.plan(f.session),passed=await(await checks.start(f.session,updated.id)).promise;
assert.equal(passed.status,'passed');current=await snapshot();assert.equal(current.stage,'baseline_passed_without_changes');assert.equal(current.next_action,null);
const artifact=f.store.core.artifact.get(passed.results[0].logArtifactId),log=path.join(f.data,'artifacts',artifact.relative);
fs.writeFileSync(log,'corrupted external evidence');current=await snapshot();
assert.equal(current.latest_check.current_source,false);assert.equal(current.stage,'requires_current_evidence_verification');assert.equal(current.next_action,null);
f.store.core.tool.set('unknown',{id:'unknown',taskId:run.taskId,sessionId:f.session.id,projectId:f.project.id,requiresVerification:true});
assert.equal((await snapshot()).stage,'awaiting_review_or_verification');
});
@@ -0,0 +1,127 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { fixture, until, workspace } from './helpers.mjs';
import { checkedDelivery, deliveryGit as git, writeDeliveryFile as write } from './fixtures/delivery-project.mjs';
import { Store } from '../src/server/store.mjs';
import { GitDelivery } from '../src/core/git-delivery.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
for (const phase of ['queued', 'preparing', 'index_locked', 'prepared', 'ref_prepared', 'publishing', 'committed', 'index_published', 'finished']) {
test('actual process exit at Git ' + phase + ' never replays a commit and can reconcile the durable result', { timeout: 60000 }, async t => {
const f = fixture(t), { delivery } = await checkedDelivery(f), plan = await delivery.plan(f.session);
const parent = git(f, ['rev-parse', 'HEAD']), stage = git(f, ['show', ':owner-staged.txt']), source = (await inspectProject(f.project)).sourceFingerprint;
const owner = fs.readFileSync(path.join(f.projectRoot, 'owner-staged.txt'));
f.store.close();
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/delivery-crash-worker.mjs'), f.data, f.session.id, plan.id, phase],
{ windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null, 25000); assert.equal(child.exitCode, 73, errors);
const cut = JSON.parse(output.trim()), recovered = new Store(f.data); f.onCleanup(() => recovered.close());
const session = recovered.session(f.session.id), job = recovered.owned('delivery_job', cut.jobId, session.id), adapter = new GitDelivery(recovered);
f.onCleanup(() => adapter.stopAll());
const published = ['committed', 'index_published', 'finished'].includes(phase);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), published ? '2' : '1');
if (!published) assert.equal(git(f, ['rev-parse', 'HEAD']), parent);
if (['queued', 'preparing'].includes(phase)) assert.ok(['interrupted', 'unknown'].includes(job.status));
else if (phase !== 'finished') assert.equal(job.status, 'unknown');
else assert.equal(job.status, 'completed');
await new Promise(resolve => setTimeout(resolve, 80)); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), published ? '2' : '1');
await assert.rejects(adapter.start(session, plan.id), { status: 409 });
if (job.refProcessPid) await until(() => !alive(job.refProcessPid), 10000);
const reconciled = await adapter.reconcile(session, job.id);
assert.equal(reconciled.status, published ? 'completed' : 'interrupted', reconciled.summary); assert.equal(reconciled.requiresVerification, false);
if (published) assert.equal(reconciled.commit, cut.expectedCommit);
assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
assert.equal(git(f, ['show', ':owner-staged.txt']), stage); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'owner-staged.txt')), owner);
assert.equal((await inspectProject(f.project)).sourceFingerprint, source);
const report = await adapter.report(session); assert.equal(report.report.status, published ? 'verified_committed' : 'verified_uncommitted');
if (!published) {
const fresh = await adapter.plan(session), final = await (await adapter.start(session, fresh.id)).promise;
assert.equal(final.status, 'completed', final.summary); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
}
});
}
test('an unknown Git result cannot overwrite an externally changed index or delete an unrelated lock', async t => {
const f = fixture(t); await checkedDelivery(f);
const adapter = new GitDelivery(f.store, { faultInjector: phase => { if (phase === 'committed') throw new Error('Synthetic lost result after actual publication'); } });
const plan = await adapter.plan(f.session), job = await (await adapter.start(f.session, plan.id)).promise;
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, true); assert.ok(job.expectedCommit);
const index = path.join(f.projectRoot, '.git/index'), lock = index + '.lock';
const original = fs.readFileSync(index); fs.appendFileSync(index, 'Owner changed index');
await assert.rejects(adapter.reconcile(f.session, job.id), /索引或遗留锁/);
assert.equal(fs.existsSync(lock), true); assert.ok(fs.readFileSync(index).includes(Buffer.from('Owner changed index')));
fs.writeFileSync(index, original); const ours = fs.readFileSync(lock); fs.writeFileSync(lock, 'Other operation owns this lock');
await assert.rejects(adapter.reconcile(f.session, job.id), /索引或遗留锁/); assert.equal(fs.readFileSync(lock, 'utf8'), 'Other operation owns this lock');
fs.writeFileSync(lock, ours);
const result = await adapter.reconcile(f.session, job.id); assert.equal(result.status, 'completed');
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('known publication with a later source edit retains the commit and marks the delivery for inspection', async t => {
const f = fixture(t); await checkedDelivery(f);
let changed = false;
const adapter = new GitDelivery(f.store, { faultInjector: phase => { if (phase === 'index_published' && !changed) { changed = true; write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b + 0;\n'); } } });
const job = await (await adapter.start(f.session, (await adapter.plan(f.session)).id)).promise;
assert.equal(job.status, 'unknown'); assert.equal(job.indexPreserved, true); assert.equal(job.workspacePreserved, false);
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), job.expectedCommit); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/math.mjs'), 'utf8'), 'export const sum = (a, b) => a + b + 0;\n');
const runtime = new Runtime(f.store, new Provider());
assert.throws(() => runtime.start(f.session.id, 'Start another task'), /结果未知/);
assert.equal((await adapter.report(f.session)).report.status, 'needs_verification');
write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b;\n');
assert.equal((await adapter.reconcile(f.session, job.id)).status, 'completed');
});
test('CRLF normalization, multiple applied revisions and unrelated special index flags preserve exact bytes and ownership', async t => {
const f = fixture(t); await checkedDelivery(f);
git(f, ['config', 'core.autocrlf', 'true']);
git(f, ['update-index', '--assume-unchanged', '--', 'owner-unstaged.txt']);
const beforeFlags = git(f, ['ls-files', '-v', '--', 'owner-unstaged.txt']);
const latest = proposeFile(f.store, f.session, { path: 'src/math.mjs', content: 'export const sum = (a, b) => a + b + 0;\r\n', reason: 'Reviewed CRLF revision' });
applyChange(f.store, f.session, latest.change_id);
// This later applied revision intentionally invalidates old checks.
const { CheckRunner } = await import('../src/core/check-runner.mjs');
const checks = new CheckRunner(f.store); assert.equal((await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise).status, 'passed');
const adapter = new GitDelivery(f.store), plan = await adapter.plan(f.session), job = await (await adapter.start(f.session, plan.id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(git(f, ['ls-files', '-v', '--', 'owner-unstaged.txt']), beforeFlags);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/math.mjs'), 'utf8'), 'export const sum = (a, b) => a + b + 0;\r\n');
assert.deepEqual(plan.files.find(file => file.path === 'src/math.mjs').changeIds, f.session.changes.filter(change => change.path === 'src/math.mjs').map(change => change.id));
});
test('adding new durable record kinds to an existing hot Store preserves all existing Maps and records', async t => {
const f = fixture(t); await checkedDelivery(f);
const oldCore = { ...f.store.core }; delete oldCore.delivery_plan; delete oldCore.delivery_job;
f.store.core = oldCore; const tasks = f.store.core.task, sessions = f.store.sessions;
f.store.ensureStorage();
assert.notEqual(f.store.core, oldCore); assert.equal(f.store.core.task, tasks); assert.equal(f.store.sessions, sessions);
assert.ok(f.store.core.delivery_plan instanceof Map); assert.ok(f.store.core.delivery_job instanceof Map);
const adapter = new GitDelivery(f.store), plan = await adapter.plan(f.session);
assert.equal((await (await adapter.start(f.session, plan.id)).promise).status, 'completed');
});
test('an unrelated owner deletion is fingerprinted and preserved while linked task paths are rejected', async t => {
const f = fixture(t); await checkedDelivery(f);
fs.unlinkSync(path.join(f.projectRoot, 'owner-unstaged.txt'));
const { CheckRunner } = await import('../src/core/check-runner.mjs'), checks = new CheckRunner(f.store);
const inspection = await inspectProject(f.project); assert.equal(inspection.scope.complete, true);
assert.equal((await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise).status, 'passed');
const adapter = new GitDelivery(f.store), job = await (await adapter.start(f.session, (await adapter.plan(f.session)).id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(fs.existsSync(path.join(f.projectRoot, 'owner-unstaged.txt')), false);
assert.match(git(f, ['status', '--porcelain=v1']), / D owner-unstaged.txt/);
fs.symlinkSync(path.join(f.projectRoot, 'src'), path.join(f.projectRoot, 'linked-src'), process.platform === 'win32' ? 'junction' : 'dir');
const proposal = proposeFile(f.store, f.session, { path: 'linked-src/math.mjs', content: 'export const sum = (a, b) => a + b + 0;\n', reason: 'Linked path requires an explicit canonical review' });
applyChange(f.store, f.session, proposal.change_id);
await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
await assert.rejects(adapter.plan(f.session), /经过链接/);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
@@ -0,0 +1,266 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, launchWorkbench, until, call, assertToolPairs, compatibleFixture, completion } from './helpers.mjs';
import { createDeliveryProject, applyDeliveryFix, checkedDelivery, deliveryGit as git, writeDeliveryFile as write } from './fixtures/delivery-project.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { GitDelivery } from '../src/core/git-delivery.mjs';
import { gitEnvironment } from '../src/core/git-process.mjs';
import { DeliveryReporter } from '../src/core/delivery-report.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
import { Store } from '../src/server/store.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
test('actual failed tests become a reviewed local commit and evidence report while preserving unrelated index and files', async t => {
const f = fixture(t); createDeliveryProject(f);
const parent = git(f, ['rev-parse', 'HEAD']).trim(), protectedStage = git(f, ['show', ':owner-staged.txt']);
const ownerFiles = ['owner-staged.txt', 'owner-unstaged.txt', 'owner-untracked.txt'].map(name => [name, fs.readFileSync(path.join(f.projectRoot, name))]);
const checks = new CheckRunner(f.store), delivery = new GitDelivery(f.store); f.onCleanup(() => delivery.stopAll()); f.onCleanup(() => checks.stopAll());
let started = await checks.start(f.session, (await checks.plan(f.session)).id), failed = await started.promise;
assert.equal(failed.status, 'failed'); assert.equal(failed.results[0].tests.failed, 3);
await assert.rejects(delivery.plan(f.session), /通过记录/);
applyDeliveryFix(f);
started = await checks.start(f.session, (await checks.plan(f.session)).id); const passed = await started.promise;
assert.equal(passed.status, 'passed'); assert.equal(passed.results[0].tests.passed, 3);
const before = await inspectProject(f.project), plan = await delivery.plan(f.session, { message: 'feat: deliver reviewed addition repair' });
assert.deepEqual(plan.paths, ['src/add.mjs', 'src/math.mjs']); assert.equal(plan.author.name, 'Workbench QA'); assert.equal(plan.policy.hooks, 'disabled');
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), parent); assert.match(plan.files[1].diff, /sum/);
const accepted = await delivery.start(f.session, plan.id), job = await accepted.promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(job.requiresVerification, false); assert.equal(job.indexPreserved, true); assert.equal(job.workspacePreserved, true);
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), job.commit); assert.equal(git(f, ['rev-parse', 'HEAD^']).trim(), parent);
assert.deepEqual(git(f, ['diff-tree', '--no-commit-id', '--name-only', '-r', 'HEAD']).trim().split('\n'), ['src/add.mjs', 'src/math.mjs']);
assert.equal(git(f, ['show', ':owner-staged.txt']), protectedStage);
for (const [name, bytes] of ownerFiles) assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, name)), bytes);
assert.match(git(f, ['status', '--porcelain=v1']), /MM owner-staged.txt/); assert.match(git(f, ['status', '--porcelain=v1']), /\?\? owner-untracked.txt/);
const after = await inspectProject(f.project);
assert.notEqual(before.fingerprint, after.fingerprint); assert.equal(before.sourceFingerprint, after.sourceFingerprint);
assert.equal(f.store.core.task.get(f.session.taskId).verification.status, 'passed');
const report = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(job.reportArtifactId)));
assert.equal(report.status, 'verified_committed'); assert.equal(report.verifiedCheckJobId, passed.id); assert.equal(report.task.requirementsAcceptance, 'not_recorded');
assert.equal(report.checks[0].results[0].tests.failed, 3); assert.equal(report.checks[1].results[0].tests.passed, 3); assert.equal(report.checks[1].stale, false);
assert.equal(report.deliveries[0].commit, job.commit); assert.equal(report.usage.amount, null);
assert.match(f.store.artifactStore.read(f.store.core.artifact.get(job.reportMarkdownArtifactId)), /检查通过并已提交/);
await assert.rejects(delivery.start(f.session, plan.id), { status: 409 });
await assert.rejects(delivery.plan(f.session), /没有新的/);
f.store.close(); const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
assert.equal(recovered.owned('delivery_job', job.id, f.session.id).commit, job.commit);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('selected preexisting unstaged, staged and untracked owner edits cannot enter a task commit', async t => {
for (const kind of ['unstaged', 'staged', 'untracked']) {
const f = fixture(t); createDeliveryProject(f, { broken: false });
const relative = kind === 'untracked' ? 'owner-new.mjs' : 'src/add.mjs';
write(f, relative, kind === 'untracked' ? 'export const owner = true;\n' : 'export const add = (a, b) => a + b + 0;\n');
if (kind === 'staged') git(f, ['add', '--', relative]);
const proposal = proposeFile(f.store, f.session, { path: relative, content: kind === 'untracked' ? 'export const owner = true; export const reviewed = true;\n' : 'export const add = (a, b) => a + b + 0 + 0;\n', reason: 'Task starts after an owner edit' });
applyChange(f.store, f.session, proposal.change_id);
const checks = new CheckRunner(f.store), plan = await checks.plan(f.session);
assert.equal((await (await checks.start(f.session, plan.id)).promise).status, 'passed');
const before = git(f, ['status', '--porcelain=v1']), head = git(f, ['rev-parse', 'HEAD']);
await assert.rejects(new GitDelivery(f.store).plan(f.session), /其他工作区修改|暂存修改|任务之前已经存在/);
assert.equal(git(f, ['status', '--porcelain=v1']), before); assert.equal(git(f, ['rev-parse', 'HEAD']), head);
}
});
test('source, unrelated stage and branch changes after review invalidate a commit plan without changing HEAD', async t => {
for (const kind of ['source', 'stage', 'branch']) {
const f = fixture(t), { delivery } = await checkedDelivery(f), plan = await delivery.plan(f.session);
if (kind === 'source') write(f, 'src/add.mjs', "import { sum } from './math.mjs'; export const add = sum;\n");
if (kind === 'stage') { write(f, 'owner-unstaged.txt', 'Owner staged after review\n'); git(f, ['add', '--', 'owner-unstaged.txt']); }
if (kind === 'branch') git(f, ['switch', '-c', 'owner-branch']);
const head = git(f, ['rev-parse', 'HEAD']), stage = git(f, ['ls-files', '--stage', '-z']);
const job = await (await delivery.start(f.session, plan.id)).promise;
assert.equal(job.status, 'stale', job.summary); assert.equal(job.commit, null); assert.equal(git(f, ['rev-parse', 'HEAD']), head);
assert.equal(git(f, ['ls-files', '--stage', '-z']), stage); assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
await assert.rejects(delivery.start(f.session, plan.id), { status: 409 });
}
});
test('a later Git metadata change keeps matching source evidence valid but a real source edit requires checks', async t => {
const f = fixture(t), { delivery, check } = await checkedDelivery(f), source = (await inspectProject(f.project)).sourceFingerprint;
git(f, ['switch', '-c', 'delivery-branch']);
const plan = await delivery.plan(f.session), job = await (await delivery.start(f.session, plan.id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(plan.snapshot.branch, 'delivery-branch');
assert.equal((await inspectProject(f.project)).sourceFingerprint, source);
const reporter = new DeliveryReporter(f.store); assert.equal((await reporter.create(f.session)).report.verifiedCheckJobId, check.id);
write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b + 1;\n');
const stale = await reporter.create(f.session);
assert.equal(stale.report.status, 'needs_checks'); assert.equal(stale.report.verifiedCheckJobId, null); assert.equal(stale.report.checks[0].stale, true);
});
test('a corrupt passed-check log and a pending proposal block Git delivery instead of inventing evidence', async t => {
const f = fixture(t), { delivery, check } = await checkedDelivery(f);
const proposal = proposeFile(f.store, f.session, { path: 'review.md', content: 'Still pending\n', reason: 'Pending item' });
await assert.rejects(delivery.plan(f.session), /待审查/);
f.session.changes.find(change => change.id === proposal.change_id).status = 'rejected'; f.session.status = 'completed'; f.store.save(f.session);
const log = f.store.core.artifact.get(check.results[0].logArtifactId);
fs.writeFileSync(path.join(f.store.artifactStore.root, log.relative), 'Corrupted result');
await assert.rejects(delivery.plan(f.session), /日志完整/);
assert.equal((await new DeliveryReporter(f.store).create(f.session)).report.status, 'needs_checks');
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '1');
});
test('Git filters, merge state, detached HEAD and old task ownership are explicitly rejected', async t => {
for (const kind of ['filter', 'merge', 'detached', 'old-task']) {
const f = fixture(t), { delivery } = await checkedDelivery(f);
if (kind === 'filter') {
write(f, '.gitattributes', '*.mjs filter=owner-filter\n');
git(f, ['config', 'filter.owner-filter.clean', 'node scripts/filter-marker.mjs']);
write(f, 'scripts/filter-marker.mjs', "import fs from 'node:fs'; fs.writeFileSync('filter-was-run.txt', 'unexpected');\n");
}
if (kind === 'merge') fs.writeFileSync(path.join(f.projectRoot, '.git/MERGE_HEAD'), git(f, ['rev-parse', 'HEAD']));
if (kind === 'detached') git(f, ['switch', '--detach']);
if (kind === 'old-task') {
const run = f.store.beginRun(f.session, 'A different task with no reviewed changes', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
const checks = new CheckRunner(f.store); await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
}
await assert.rejects(delivery.plan(f.session), /过滤|合并|命名分支|归属明确/);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'filter-was-run.txt')), false);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '1');
}
});
test('local snapshot commits disable repository hooks and signing and isolate the Git child environment', async t => {
const f = fixture(t), { delivery } = await checkedDelivery(f);
const hook = path.join(f.projectRoot, '.git/hooks/reference-transaction');
fs.writeFileSync(hook, '#!/bin/sh\nprintf unexpected > hook-was-run.txt\n', { mode: 0o755 });
git(f, ['config', 'commit.gpgSign', 'true']); git(f, ['config', 'user.signingKey', 'a-key-that-is-not-present']);
const previous = process.env.EXTRA_MODEL_CONNECTION_SECRET;
process.env.EXTRA_MODEL_CONNECTION_SECRET = 'synthetic-runtime-only-secret';
try {
assert.equal(gitEnvironment().EXTRA_MODEL_CONNECTION_SECRET, undefined); assert.equal(gitEnvironment().NODE_TEST_CONTEXT, undefined);
const job = await (await delivery.start(f.session, (await delivery.plan(f.session, { authorName: 'Local Reviewer', authorEmail: 'reviewer@local.invalid', message: 'fix: reviewed delivery' })).id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(fs.existsSync(path.join(f.projectRoot, 'hook-was-run.txt')), false);
assert.match(git(f, ['cat-file', 'commit', 'HEAD']), /author Local Reviewer <reviewer@local.invalid>/);
assert.ok(!git(f, ['cat-file', 'commit', 'HEAD']).includes('gpgsig'));
assert.ok(!fs.readFileSync(path.join(f.data, 'workbench.sqlite')).includes(Buffer.from('synthetic-runtime-only-secret')));
} finally { if (previous === undefined) delete process.env.EXTRA_MODEL_CONNECTION_SECRET; else process.env.EXTRA_MODEL_CONNECTION_SECRET = previous; }
});
test('stopping before publication cancels safely; stopping after publication preserves the actual completed commit', async t => {
for (const phase of ['prepared', 'committed']) {
const f = fixture(t); await checkedDelivery(f);
let reached = false, release;
const gate = new Promise(resolve => { release = resolve; });
const delivery = new GitDelivery(f.store, { faultInjector: async current => { if (current === phase) { reached = true; await gate; } } });
f.onCleanup(() => { release(); return delivery.stopAll(); });
const runtime = new Runtime(f.store, new Provider()); runtime.deliveries = delivery;
delivery.projectBusy = projectId => [...runtime.active.values()].some(run => run.project.id === projectId);
const plan = await delivery.plan(f.session), started = await delivery.start(f.session, plan.id);
await until(() => reached, 12000); assert.equal(runtime.hasActivity(), true); assert.equal(runtime.projectHasActivity(f.project.id), true);
await assert.rejects(delivery.start(f.session, plan.id), { status: 409 });
assert.throws(() => runtime.start(f.store.createSession(f.project.id).id, 'Concurrent work'), { status: 409 });
const stop = runtime.stop(f.session.id); release(); await stop; const job = await started.promise;
assert.equal(job.status, phase === 'prepared' ? 'cancelled' : 'completed', job.summary);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), phase === 'prepared' ? '1' : '2');
assert.equal(job.stopRequested, true); assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
assert.equal(runtime.hasActivity(), false);
}
});
test('delivery API confirms exactly one owned plan, serves verified reports and preserves results across restart', async t => {
const f = fixture(t); await checkedDelivery(f); f.store.close(); const app = await launchWorkbench(t, f.data);
const planned = await app.api('/sessions/' + f.session.id + '/delivery-plans', { message: 'API reviewed delivery' }), plan = planned.execution.deliveryPlans.at(-1);
assert.deepEqual(plan.paths, ['src/add.mjs', 'src/math.mjs']); assert.equal(plan.snapshot, undefined); assert.match(plan.baseline.head, /^[a-f0-9]{40}$/);
const other = await app.api('/sessions', { projectId: f.project.id });
await assert.rejects(app.api('/sessions/' + other.id + '/delivery-jobs', { planId: plan.id }), { status: 409 });
const responses = await Promise.allSettled([app.api('/sessions/' + f.session.id + '/delivery-jobs', { planId: plan.id }), app.api('/sessions/' + f.session.id + '/delivery-jobs', { planId: plan.id })]);
assert.equal(responses.filter(value => value.status === 'fulfilled').length, 1);
const accepted = responses.find(value => value.status === 'fulfilled').value, jobId = accepted.execution.deliveries.at(-1).id;
let job; await until(async () => { job = await app.api('/sessions/' + f.session.id + '/delivery-jobs/' + jobId); return !['queued', 'preparing', 'prepared', 'publishing', 'committed', 'reporting'].includes(job.status); }, 20000);
assert.equal(job.status, 'completed', job.summary);
await assert.rejects(app.api('/sessions/' + other.id + '/delivery-jobs/' + jobId), { status: 404 });
const preview = await app.api('/artifacts/' + job.reportArtifactId + '?session=' + f.session.id + '&limit=64000');
assert.equal(JSON.parse(preview.content).status, 'verified_committed');
await assert.rejects(app.api('/artifacts/' + job.reportArtifactId + '?session=' + other.id), { status: 404 });
const download = await fetch(app.url + '/api/artifacts/' + job.reportMarkdownArtifactId + '?session=' + f.session.id + '&download=1', { headers: { 'X-Workbench-Token': app.bootstrap.token } });
assert.equal(download.status, 200); assert.match(download.headers.get('content-disposition'), /\.md/); assert.match(await download.text(), /检查通过并已提交/);
const unknown = await fetch(app.url + '/api/artifacts/' + job.reportArtifactId + '?session=' + f.session.id + '&download=1');
assert.equal(unknown.status, 403);
await app.close(); const resumed = await launchWorkbench(t, f.data);
assert.equal((await resumed.api('/sessions/' + f.session.id + '/delivery-jobs/' + jobId)).commit, job.commit);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('the model can prepare an owned Git review and evidence report while publishing remains a separate user action', async t => {
const f = fixture(t); await checkedDelivery(f);
f.session.status = 'paused'; f.store.save(f.session);
const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let round = 0;
provider.complete = async () => ({ message: { role: 'assistant', content: round > 1 ? 'Git预览已准备,等待用户确认。' : '',
tool_calls: round++ === 0 ? [call('model-git-plan', 'plan_delivery', { message: 'fix: model-prepared reviewed changes' })]
: round === 2 ? [call('model-evidence-report', 'delivery_report', {})] : [] }, usage: { prompt_tokens: 10, completion_tokens: 5 } });
const head = git(f, ['rev-parse', 'HEAD']);
runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
const result = f.store.publicSession(f.session);
assert.equal(result.execution.deliveryPlans.length, 1); assert.equal(result.execution.deliveryPlans[0].preparedBy, 'agent');
assert.equal(result.execution.deliveries.length, 0); assert.equal(git(f, ['rev-parse', 'HEAD']), head);
assert.equal(result.events.find(event => event.tool === 'plan_delivery').phase, 'completed');
assert.equal(result.events.some(event => event.tool === 'delivery_report'), false);
assert.equal(round, 1); assert.equal(result.status, 'needs_review'); assert.equal(result.execution.run.stopReason, 'git_review');
assert.throws(() => runtime.resume(f.session.id), { status: 409 });
assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 });
assertToolPairs(f.session.providerMessages);
assert.equal((await runtime.gitDelivery().report(f.session)).report.status, 'needs_review');
const accepted = await runtime.gitDelivery().start(f.session, result.execution.deliveryPlans[0].id), job = await accepted.promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
assert.equal(f.session.status, 'completed'); assert.equal(f.store.core.run.get(f.session.runId).review.status, 'committed');
assert.equal(JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(job.reportArtifactId))).status, 'verified_committed');
assert.equal(round, 1);
});
test('HTTP Git review pairs deferred calls, survives reload and never requests more work until the saved preview is resolved', async t => {
const f = fixture(t); await checkedDelivery(f); f.session.status = 'paused'; f.store.save(f.session);
const model = await compatibleFixture(t, () => completion([
call('sealed-http-git', 'plan_delivery', { message: 'fix: HTTP review boundary' }),
call('deferred-after-preview', 'run_command', { command: 'node -e "require(\'node:fs\').writeFileSync(\'should-not-run.txt\',\'no\')"', reason: 'This must wait after Git preview' }),
]));
f.store.saveConfig({ mode: 'compatible', baseUrl: model.baseUrl, model: 'review-fixture' });
const provider = new Provider(); provider.setKey('synthetic-review-key');
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
assert.equal(model.requests.length, 1); assert.equal(f.session.status, 'needs_review');
assert.equal(f.session.approvals.length, 0); assert.equal(fs.existsSync(path.join(f.projectRoot, 'should-not-run.txt')), false);
const invocation = [...f.store.core.tool.values()].find(tool => tool.callId === 'deferred-after-preview');
assert.equal(invocation.state, 'interrupted'); assert.equal(invocation.requiresVerification, false);
assert.equal(JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(invocation.resultArtifactId))).reason, 'git_review');
assertToolPairs(f.session.providerMessages);
const task = f.session.taskId, plan = f.store.executionSnapshot(f.session).deliveryPlans.at(-1);
f.store.close(); const restored = new Store(f.data); f.onCleanup(() => restored.close());
const resumed = new Runtime(restored, provider); f.onCleanup(() => resumed.stopAll());
assert.throws(() => resumed.resume(f.session.id), { status: 409 }); assert.equal(model.requests.length, 1);
const original = restored.session(f.session.id);
const job = await (await resumed.gitDelivery().start(original, plan.id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(original.taskId, task);
assert.equal(original.status, 'completed'); assert.equal(model.requests.length, 1);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('a reviewed Git preview can be revised or withdrawn without publishing or losing the current Task', async t => {
const f = fixture(t); await checkedDelivery(f); f.session.status = 'paused'; f.store.save(f.session);
const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let requests = 0;
provider.complete = async () => ({ message: { role: 'assistant', content: '', tool_calls: requests++ === 0
? [call('withdraw-git-plan', 'plan_delivery', { message: 'First preview' })] : [] }, usage: null });
runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
const task = f.session.taskId, run = f.session.runId, delivery = runtime.gitDelivery(), original = f.store.executionSnapshot(f.session).deliveryPlans.at(-1);
const revised = await delivery.plan(f.session, { message: 'Revised explicit review' });
assert.equal(f.store.core.delivery_plan.get(original.id).status, 'superseded');
assert.equal(f.store.core.run.get(run).review.planId, revised.id);
const other = f.store.createSession(f.project.id);
assert.throws(() => delivery.discardPlan(other, revised.id), { status: 404 });
const before = git(f, ['status', '--porcelain=v1']);
delivery.discardPlan(f.session, revised.id);
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(run).stopReason, 'git_review_declined');
assert.equal(f.session.taskId, task); assert.equal(git(f, ['status', '--porcelain=v1']), before);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '1');
assert.equal(f.store.executionSnapshot(f.session).deliveries.length, 0);
runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.taskId, task); assert.equal(f.session.status, 'completed'); assert.equal(requests, 2);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '1');
});
@@ -0,0 +1,108 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID, createHash } from 'node:crypto';
import { fixture } from './helpers.mjs';
import { DesktopBridge } from '../src/core/desktop/desktop-bridge.mjs';
import { desktopInput } from '../src/shared/contracts/desktop-bridge.mjs';
import { readText, listFiles } from '../src/server/files.mjs';
import { HostAuthority } from '../src/core/desktop/host-authority.mjs';
import { desktopLaunchOptions } from '../src/core/desktop/launch-options.mjs';
test('actual Electron debug argument ordering retains exactly the compiled entry and rejects extra or repeated actions', () => {
const mainFile = path.resolve('dist-desktop/main.cjs'), argv = ['electron.exe', '--inspect=0', '--remote-debugging-port=0', mainFile, '--port', '4319', '--hidden-window'];
assert.deepEqual(desktopLaunchOptions(argv, { defaultApp: true, mainFile }), { '--port': '4319', hidden: true });
assert.deepEqual(desktopLaunchOptions(['product.exe', '--port', '4319'], { defaultApp: false }), { '--port': '4319' });
assert.throws(() => desktopLaunchOptions(argv, { defaultApp: true, mainFile: path.resolve('different.cjs') }));
assert.throws(() => desktopLaunchOptions([...argv, '--port', '4320'], { defaultApp: true, mainFile }));
assert.throws(() => desktopLaunchOptions([...argv, '--command', 'arbitrary'], { defaultApp: true, mainFile }));
});
function setup(t) {
const f = fixture(t), frame = { url: 'http://127.0.0.1:4319/' }, contents = { mainFrame: frame };
const window = { webContents: contents, isDestroyed: () => false }, event = { sender: contents, senderFrame: frame };
const scope = { projectId: f.project.id, sessionId: f.session.id, taskId: randomUUID() }, calls = [];
const saved = { id: f.session.id, projectId: f.project.id, execution: { task: { id: scope.taskId } }, eventSeq: 19 };
const identity = { instanceId: randomUUID(), runtimeIdentity: 'original' };
const controller = { url: 'http://127.0.0.1:4319', connect: async () => ({ identity }), status: async () => ({ identity }),
api: async (route, options) => { calls.push({ route, options }); if (route === '/bootstrap') return { projects: [f.project] }; if (route.startsWith('/sessions/')) return saved; return f.project; },
stop: async () => { calls.push({ stop: true }); return { processExited: true }; } };
const shell = { openPath: async value => { calls.push({ folder: value }); return ''; }, showItemInFolder: value => calls.push({ reveal: value }) };
const dialog = { showOpenDialog: async () => ({ canceled: true, filePaths: [] }), showMessageBox: async () => ({ response: 0 }) };
const bridge = new DesktopBridge(controller, { shell, dialog, getWindow: () => window });
return { ...f, frame, contents, window, event, scope, calls, saved, identity, controller, shell, dialog, bridge };
}
test('native bridge refuses other origins, subframes, other windows and unsupported inputs before connecting', async t => {
const f = setup(t);
for (const event of [{ ...f.event, sender: {} }, { ...f.event, senderFrame: { url: f.frame.url } }]) await assert.rejects(f.bridge.execute(event, 'pickProject'));
f.frame.url = 'https://example.invalid/'; await assert.rejects(f.bridge.execute(f.event, 'status')); assert.equal(f.calls.length, 0);
for (const input of [{ projectId: f.project.id, command: 'arbitrary' }, { projectId: '../project' }, ['project']]) assert.throws(() => desktopInput('projectFolder', input));
assert.throws(() => desktopInput('arbitrary', {}));
});
test('desktop preview opening requires a currently registered project preview and rejects iframe callers', async t => {
const f = setup(t), original = f.controller.api;
const url = 'http://127.0.0.1:45000/' + 'a'.repeat(48) + '/index.html';
let allowed = false;
f.controller.api = async (route, options) => route.startsWith('/local-file-preview-access?') ? { allowed, projectId: f.project.id } : original(route, options);
f.shell.openExternal = async value => f.calls.push({ external: value });
await assert.rejects(f.bridge.execute(f.event, 'openFilePreview', { projectId: f.project.id, url }));
allowed = true;
await assert.rejects(f.bridge.execute({ ...f.event, senderFrame: { url } }, 'openFilePreview', { projectId: f.project.id, url }));
await assert.rejects(f.bridge.execute(f.event, 'openFilePreview', { projectId: randomUUID(), url }));
assert.deepEqual(await f.bridge.execute(f.event, 'openFilePreview', { projectId: f.project.id, url }), { opened: true });
assert.deepEqual(f.calls.filter(value => value.external), [{ external: url }]);
});
test('file reveal binds the current project, task, path, UTF-8 bytes and original SHA without executing the file', async t => {
const f = setup(t), file = readText(f.project, 'README.md'), input = { ...f.scope, path: file.path, sha256: file.sha256 };
await f.bridge.execute(f.event, 'revealFile', input); assert.equal(f.calls.filter(value => value.reveal).length, 1); assert.equal(f.calls.filter(value => value.folder).length, 0);
for (const changed of [{ taskId: randomUUID() }, { projectId: randomUUID() }, { sha256: '0'.repeat(64) }, { path: '../outside' }, { path: 'C:\\outside' }]) await assert.rejects(f.bridge.execute(f.event, 'revealFile', { ...input, ...changed }));
assert.equal(f.calls.filter(value => value.reveal).length, 1);
const invalid = Buffer.from([255, 254, 255]); fs.writeFileSync(path.join(f.projectRoot, 'bad.txt'), invalid);
await assert.rejects(f.bridge.execute(f.event, 'revealFile', { ...input, path: 'bad.txt', sha256: createHash('sha256').update(invalid).digest('hex') }));
});
test('file changed during asynchronous task recheck is not revealed as the old bytes', async t => {
const f = setup(t), file = readText(f.project, 'README.md'), original = f.controller.api; let requests = 0;
f.controller.api = async (...args) => { if (args[0].startsWith('/sessions/') && ++requests === 2) fs.appendFileSync(path.join(f.projectRoot, file.path), 'changed during original check'); return original(...args); };
await assert.rejects(f.bridge.execute(f.event, 'revealFile', { ...f.scope, path: file.path, sha256: file.sha256 }), /变化/);
assert.equal(f.calls.filter(value => value.reveal).length, 0);
});
test('desktop browser profile and aliases stay outside project inventory and file access', async t => {
const f = setup(t), profile = path.join(f.projectRoot, '.desktop-profile'); fs.mkdirSync(profile); fs.writeFileSync(path.join(profile, 'Cookies'), 'owned private profile');
fs.symlinkSync(profile, path.join(f.projectRoot, 'profile-alias'), 'junction');
assert.throws(() => readText(f.project, '.desktop-profile/Cookies')); assert.throws(() => readText(f.project, 'profile-alias/Cookies'));
assert.equal(listFiles(f.project).files.some(value => /profile|Cookies/.test(value.path)), false);
});
test('native picker cancel and origin change after dialog publish no project registration', async t => {
const f = setup(t); assert.equal(await f.bridge.execute(f.event, 'pickProject'), null);
f.dialog.showOpenDialog = async () => { f.frame.url = 'http://127.0.0.1:4320/'; return { canceled: false, filePaths: [f.projectRoot] }; };
await assert.rejects(f.bridge.execute(f.event, 'pickProject')); assert.equal(f.calls.some(value => value.route === '/projects'), false);
});
test('pending native action is not dispatched twice, and current task review returns its original seq', async t => {
const f = setup(t); let finish; f.dialog.showOpenDialog = () => new Promise(resolve => { finish = resolve; });
const first = f.bridge.execute(f.event, 'pickProject'); await new Promise(resolve => setImmediate(resolve));
await assert.rejects(f.bridge.execute(f.event, 'pickProject'), /正在处理/); finish({ canceled: true, filePaths: [] }); await first;
assert.deepEqual(await f.bridge.execute(f.event, 'reviewTask', f.scope), { ...f.scope, eventSeq: 19 });
});
test('native stop cancellation and changed original host never stop a replacement process', async t => {
const f = setup(t); assert.deepEqual(await f.bridge.execute(f.event, 'stopHost'), { cancelled: true }); assert.equal(f.calls.some(value => value.stop), false);
f.dialog.showMessageBox = async () => ({ response: 1 }); let connects = 0;
f.controller.connect = async () => ({ identity: ++connects === 1 ? f.identity : { ...f.identity, runtimeIdentity: 'replacement' } });
await assert.rejects(f.bridge.execute(f.event, 'stopHost'), /已变化/); assert.equal(f.calls.some(value => value.stop), false);
f.controller.connect = async () => ({ identity: f.identity }); assert.equal((await f.bridge.execute(f.event, 'stopHost')).processExited, true); assert.equal(f.calls.filter(value => value.stop).length, 1);
});
test('window register rejects host PID, stale installation, extra fields and missing creation identity', () => {
const authority = new HostAuthority({}), identity = { pid: process.pid, instanceId: randomUUID(), runtimeIdentity: 'original' }; authority.identity = () => identity;
const input = { windowId: randomUUID(), pid: 1234, createdAt: '2026-10-31T00:00:00.000Z', instanceId: identity.instanceId, runtimeIdentity: identity.runtimeIdentity };
assert.equal(authority.assertWindowInput(input), identity);
for (const changed of [{ pid: process.pid }, { createdAt: '' }, { runtimeIdentity: 'replacement' }, { program: 'foreign' }]) assert.throws(() => authority.assertWindowInput({ ...input, ...changed }));
});
@@ -0,0 +1,90 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { randomBytes, createHash } from 'node:crypto';
import { fixture, workspace, until } from './helpers.mjs';
import { hostProof, verifyHostProof, assertHostStop } from '../src/shared/contracts/desktop-host.mjs';
import { ownedChild, writeState, readState } from '../src/core/desktop/file-state.mjs';
import { HostAuthority } from '../src/core/desktop/host-authority.mjs';
import { Provider } from '../src/server/provider.mjs';
import { inspectHostInstallation } from '../src/core/desktop/app-installation.mjs';
test('a rebuilt source archive binds actual generated client assets and retains its original running identity after disk changes', t => {
const f = fixture(t), root = path.join(f.root, 'owned-source-build'); fs.mkdirSync(root);
for (const directory of ['dist-client', 'dist-server', 'dist-desktop']) fs.cpSync(path.join(workspace, directory), path.join(root, directory), { recursive: true });
const packageBytes = fs.readFileSync(path.join(workspace, 'package.json')); fs.writeFileSync(path.join(root, 'package.json'), packageBytes);
fs.writeFileSync(path.join(root, 'MANIFEST.json'), JSON.stringify({ app: 'agent-workbench', kind: 'source', version: JSON.parse(packageBytes).version,
files: [{ path: 'package.json', bytes: packageBytes.length, sha256: createHash('sha256').update(packageBytes).digest('hex') }] }));
const authority = new HostAuthority({ appDirectory: root, store: f.store, provider: new Provider(), runtime: { appDirectory: root, active: new Map(), waiters: new Map() }, server: { address: () => ({ port: 49998 }) } });
const running = authority.identity(), asset = 'dist-client/assets/' + fs.readdirSync(path.join(root, 'dist-client/assets')).find(name => name.endsWith('.js'));
assert.ok(inspectHostInstallation(root).inputs.some(value => value.path === asset));
fs.appendFileSync(path.join(root, asset), '\n// owned source-archive disk change\n');
assert.equal(authority.identity().runtimeIdentity, running.runtimeIdentity);
assert.notEqual(inspectHostInstallation(root).identitySha256, running.runtimeIdentity);
});
test('host challenge proof rejects a stale challenge, changed identity and changed signature', () => {
const identity = { instanceId: 'owned', pid: 123, createdAt: 'fixed', runtimeIdentity: 'fixed-input' }, secret = randomBytes(32), challenge = randomBytes(32).toString('hex');
const reply = { identity, challenge, proof: hostProof(identity, challenge, secret) };
assert.deepEqual(verifyHostProof(reply, challenge, secret), identity);
assert.throws(() => verifyHostProof(reply, randomBytes(32).toString('hex'), secret));
assert.throws(() => verifyHostProof({ ...reply, identity: { ...identity, pid: 124 } }, challenge, secret));
assert.throws(() => verifyHostProof(reply, challenge, randomBytes(32)));
assert.throws(() => hostProof(identity, 'short', secret));
});
test('stop confirmation binds the original instance, creation and loaded runtime', () => {
const identity = { instanceId: 'owned', createdAt: 'fixed', runtimeIdentity: 'fixed-input' }, input = { ...identity, confirmation: 'stop-owned-host' };
assertHostStop(input, identity);
for (const key of ['instanceId', 'createdAt', 'runtimeIdentity', 'confirmation']) assert.throws(() => assertHostStop({ ...input, [key]: 'changed' }, identity));
});
test('state paths reject escaping, linked ancestors, ambiguous hardlinks and damaged JSON', t => {
const f = fixture(t), owned = path.join(f.root, 'owned'); fs.mkdirSync(owned);
assert.throws(() => ownedChild(owned, '../outside', { missing: true }));
assert.throws(() => ownedChild(owned, 'file:stream', { missing: true }));
fs.symlinkSync(f.projectRoot, path.join(owned, 'linked'), 'junction');
assert.throws(() => ownedChild(owned, 'linked/README.md'));
const original = path.join(owned, 'original.json'); writeState(original, { original: true });
fs.linkSync(original, path.join(owned, 'second.json')); assert.throws(() => readState(original));
const damaged = path.join(owned, 'damaged.json'); fs.writeFileSync(damaged, '{'); assert.throws(() => writeState(damaged, {}));
const valid = path.join(owned, 'valid.json'); writeState(valid, { revision: 1 }); writeState(valid, { revision: 2 }); assert.deepEqual(readState(valid), { revision: 2 });
});
test('failed registration publishes no initialized memory and can complete after its actual inputs are supplied', { timeout: 30000 }, t => {
const f = fixture(t); f.store.claimRuntime();
const app = path.join(f.root, 'app'); fs.mkdirSync(app); fs.writeFileSync(path.join(app, 'package.json'), fs.readFileSync(path.join(workspace, 'package.json')));
const authority = new HostAuthority({ appDirectory: app, server: { address: () => ({ port: 12345 }) }, store: f.store, runtime: { appDirectory: app } });
assert.throws(() => authority.initialize()); assert.equal(authority.registration, undefined);
for (const directory of ['dist-server', 'dist-client']) fs.cpSync(path.join(workspace, directory), path.join(app, directory), { recursive: true });
authority.initialize(); const identity = authority.identity();
assert.equal(identity.pid, process.pid); assert.equal(identity.ownerId, f.store.ownerId);
assert.equal(readState(path.join(f.data, 'service-info.json')).instanceId, identity.instanceId);
assert.equal(JSON.stringify(authority.attest(randomBytes(32).toString('hex'))).includes(authority.registration.secret), false);
});
test('host shutdown cancels an un-signalled HTTP model request and refuses later requests until recovered', async () => {
let received;
const provider = new Provider({ fetch: async (_url, options) => new Promise((_resolve, reject) => {
received = options; options.signal.addEventListener('abort', () => reject(options.signal.reason), { once: true });
}) }); provider.setKey('owned-request-key');
const request = provider.models({ baseUrl: 'http://127.0.0.1:1/v1', model: 'owned' }).catch(error => error);
await until(() => received);
await provider.abortRequests(); const error = await request;
assert.equal(error instanceof Error, true); assert.equal(received.signal.aborted, true);
assert.equal(provider.activeRequests, 0); assert.equal(provider.requestControllers.size, 0); assert.equal(provider.lastRequest.status, 'interrupted');
await assert.rejects(provider.models({ baseUrl: 'http://127.0.0.1:1/v1' }), /停止/);
provider.resumeRequests(); assert.equal(provider.requestsClosing, false);
});
test('host shutdown cancels model-pool acquisition and releases queue ownership without inventing attempts', async t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store);
const connection = hub.upsert({ name: 'owned', model: 'owned', baseUrl: 'http://127.0.0.1:1/v1', apiKey: 'owned-pool-key' });
hub.select({ type: 'connection', id: connection.id }); const config = hub.prepare(f.store.config);
hub.leases.set(connection.id, 1);
const request = hub.complete(config, { messages: [] }, undefined, null, null, () => { throw new Error('Queued request must not dispatch.'); }).catch(error => error);
await until(() => hub.waiting === 1); await provider.abortRequests(); const failure = await request;
assert.equal(failure instanceof Error, true); assert.equal(hub.waiting, 0); assert.equal(hub.attempts.length, 0); assert.equal(hub.leases.get(connection.id), 1);
hub.leases.clear();
});
@@ -0,0 +1,90 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import { createHash } from 'node:crypto';
import { fixture, workspace } from './helpers.mjs';
import { InstallationManager, validateInstalledVersion, snapshotPersistentData } from '../src/core/desktop/install-manager.mjs';
import { inspectHostInstallation } from '../src/core/desktop/app-installation.mjs';
const hash = value => createHash('sha256').update(value).digest('hex');
function candidate(f, name = 'owned-a', bootstrapBytes = 'declared-static-unit-fixture') {
const root = path.join(f.root, 'install', 'versions', name); fs.mkdirSync(root, { recursive: true });
for (const directory of ['dist-client', 'dist-server', 'dist-desktop']) fs.cpSync(path.join(workspace, directory), path.join(root, directory), { recursive: true });
for (const file of ['AgentWorkbench.exe', 'runtime/node.exe', 'desktop/AgentWorkbench.exe', 'desktop/resources/app/main.cjs', 'desktop/resources/app/package.json']) { fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'declared-static-unit-fixture'); }
fs.writeFileSync(path.join(root, 'AgentWorkbench.exe'), bootstrapBytes);
const walk = (directory, prefix = '') => fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
const relative = path.posix.join(prefix, entry.name), file = path.join(directory, entry.name);
return entry.isDirectory() ? walk(file, relative) : [{ path: relative, bytes: fs.statSync(file).size, sha256: hash(fs.readFileSync(file)) }];
});
fs.writeFileSync(path.join(root, 'MANIFEST.json'), JSON.stringify({ app: 'agent-workbench', kind: 'portable', version: JSON.parse(fs.readFileSync(path.join(workspace, 'package.json'))).version, platform: 'win32-x64', files: walk(root) }));
return root;
}
const manager = (f, root) => new InstallationManager({ installRoot: path.join(f.root, 'install'), dataRoot: f.data, candidateRoot: root, port: 49999 });
test('full candidate validation returns the exact loaded-input host identity and rejects undeclared or altered files', t => {
const f = fixture(t), root = candidate(f);
assert.deepEqual(validateInstalledVersion(root), inspectHostInstallation(root));
fs.writeFileSync(path.join(root, 'extra.txt'), 'unknown'); assert.throws(() => validateInstalledVersion(root)); fs.unlinkSync(path.join(root, 'extra.txt'));
fs.appendFileSync(path.join(root, 'dist-server/server.mjs'), '\nchanged'); assert.throws(() => validateInstalledVersion(root));
});
test('installation requires separate absolute program/data directories and a candidate in its own versions namespace', t => {
const f = fixture(t), root = candidate(f);
assert.throws(() => new InstallationManager({ installRoot: f.root, dataRoot: f.data }));
assert.throws(() => new InstallationManager({ installRoot: 'relative', dataRoot: f.data }));
const m = manager(f, root); m.candidateRoot = f.projectRoot; assert.throws(() => m.candidate());
});
test('staging retains the original data and pointer without querying or restarting a host', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root), before = snapshotPersistentData(f.data);
m.host = () => { throw Error('Staging must not contact the host'); };
const result = await m.activate({ stageOnly: true }); assert.equal(result.activated, false); assert.equal(m.current(), null); assert.deepEqual(snapshotPersistentData(f.data), before);
});
test('a post-pointer failure restores every original pointer byte and persistent row; a first-install failure removes only its published pointer', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); m.host = async () => null;
await assert.rejects(m.activate({ afterPointer: () => { throw Error('owned-first-failure'); } }), /owned-first-failure/); assert.equal(m.current(), null);
await m.activate(); const pointer = fs.readFileSync(m.recordFile), before = snapshotPersistentData(f.data);
m.candidateRoot = candidate(f, 'owned-b');
await assert.rejects(m.activate({ afterPointer: () => { throw Error('owned-post-pointer-failure'); } }), /owned-post-pointer-failure/);
assert.deepEqual(fs.readFileSync(m.recordFile), pointer); assert.deepEqual(snapshotPersistentData(f.data), before);
});
test('changed active manifest and concurrent pointer changes are rejected without overwriting the unknown record', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); m.host = async () => null; await m.activate();
fs.appendFileSync(path.join(root, 'MANIFEST.json'), ' '); assert.throws(() => m.current());
});
test('an unregistered non-HTTP listener blocks preflight even when service-info is absent', async t => {
const f = fixture(t), root = candidate(f), server = net.createServer(socket => socket.on('error', () => {}));
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); f.onCleanup(() => new Promise(resolve => server.close(resolve)));
const m = new InstallationManager({ installRoot: path.join(f.root, 'install'), dataRoot: f.data, candidateRoot: root, port: server.address().port });
await assert.rejects(m.preflight(), /端口/); assert.equal(m.current(), null);
});
test('uninstall archives its original pointer and preserves every data row/artifact instead of recursively deleting data', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); m.host = async () => null;
await m.activate(); const before = snapshotPersistentData(f.data), pointer = fs.readFileSync(m.recordFile);
const result = await m.prepareUninstall(); assert.equal(result.dataPolicy, 'preserve-original-data'); assert.equal(m.current(), null); assert.deepEqual(snapshotPersistentData(f.data), before);
const archive = fs.readdirSync(m.stateRoot).find(name => name.startsWith('uninstalled-')); assert.deepEqual(fs.readFileSync(path.join(m.stateRoot, archive)), pointer);
});
test('preflight rejects an unrelated program root or changed native bootstrap while preserving its original bytes', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); m.host = async () => null;
const unknown = path.join(m.installRoot, 'unrelated.exe'); fs.writeFileSync(unknown, 'owned-unknown-original');
await assert.rejects(m.preflight(), /未登记/); assert.equal(fs.readFileSync(unknown, 'utf8'), 'owned-unknown-original'); fs.unlinkSync(unknown);
const launcher = path.join(m.installRoot, 'AgentWorkbench.exe'); fs.writeFileSync(launcher, 'owned-altered-bootstrap');
await assert.rejects(m.preflight(), /启动器/); assert.equal(fs.readFileSync(launcher, 'utf8'), 'owned-altered-bootstrap');
});
test('a changed owned bootstrap follows its candidate and a post-pointer fault restores both original bytes', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); m.host = async () => null;
await m.activate(); const launcher = path.join(m.installRoot, 'AgentWorkbench.exe'), original = fs.readFileSync(launcher), pointer = fs.readFileSync(m.recordFile);
m.candidateRoot = candidate(f, 'owned-bootstrap-b', 'owned-new-native-bootstrap');
await assert.rejects(m.activate({ afterPointer: () => { assert.equal(fs.readFileSync(launcher, 'utf8'), 'owned-new-native-bootstrap'); throw Error('owned-bootstrap-published-fault'); } }), /owned-bootstrap-published-fault/);
assert.deepEqual(fs.readFileSync(launcher), original); assert.deepEqual(fs.readFileSync(m.recordFile), pointer); await m.preflight();
await m.activate(); assert.equal(fs.readFileSync(launcher, 'utf8'), 'owned-new-native-bootstrap'); await m.preflight();
});
test('an external bootstrap change after preflight is preserved and never overwritten as an update or rollback', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); m.host = async () => null; await m.activate();
const launcher = path.join(m.installRoot, 'AgentWorkbench.exe'), pointer = fs.readFileSync(m.recordFile);
m.candidateRoot = candidate(f, 'owned-bootstrap-b', 'owned-new-native-bootstrap');
await assert.rejects(m.activate({ beforePointer: () => fs.writeFileSync(launcher, 'owned-external-bootstrap') }), /回退尚待核对/);
assert.equal(fs.readFileSync(launcher, 'utf8'), 'owned-external-bootstrap'); assert.deepEqual(fs.readFileSync(m.recordFile), pointer);
});
@@ -0,0 +1,50 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';
import { fixture, workspace } from './helpers.mjs';
import { HostController } from '../src/core/desktop/host-controller.mjs';
import { observeWindowsProcess } from '../src/core/desktop/process-identity.mjs';
function ownedStartup(t, timeoutMs = 10000) {
const f = fixture(t), appRoot = path.join(f.root, 'owned-startup-app'), dataRoot = path.join(f.root, 'owned-startup-data');
for (const directory of ['dist-client', 'dist-server', 'dist-desktop']) fs.cpSync(path.join(workspace, directory), path.join(appRoot, directory), { recursive: true });
fs.copyFileSync(path.join(workspace, 'package.json'), path.join(appRoot, 'package.json'));
const source = "import fs from 'node:fs';import path from 'node:path';const stop=path.join(process.env.AGENT_WORKBENCH_DATA_DIR,'exit-owned-startup');setInterval(()=>{if(fs.existsSync(stop))process.exit(0)},25);";
fs.writeFileSync(path.join(appRoot, 'dist-server/server.mjs'), source);
const controller = new HostController({ appRoot, dataRoot, nodePath: process.execPath, port: 49997, timeoutMs });
const intent = () => JSON.parse(fs.readFileSync(path.join(dataRoot, 'desktop-launch-state.json'), 'utf8'));
f.onCleanup(async () => {
fs.writeFileSync(path.join(dataRoot, 'exit-owned-startup'), 'exit through own fixture');
const pid = intent().pid, deadline = Date.now() + 10000;
while (observeWindowsProcess(pid)) { assert.ok(Date.now() < deadline, 'Owned fixture must exit through its own sentinel.'); await delay(50); }
});
return { controller, intent };
}
test('owned startup retries only read-only observation after a slow health response and attests the same original child', { timeout: 30000 }, async t => {
const { controller, intent } = ownedStartup(t); let connects = 0, observations = 0;
controller.connect = async () => ++connects === 1 ? null : { identity: { pid: intent().pid, instanceId: 'owned-attested-result' } };
controller.health = async () => {
if (++observations === 1) throw Object.assign(new Error('controlled registration delay'), { code: 'HOST_PORT_UNVERIFIED' });
return { pid: intent().pid };
};
const result = await controller.start();
assert.equal(result.reused, false); assert.equal(connects, 2); assert.equal(observations, 2);
assert.equal(result.identity.pid, intent().pid); assert.equal(intent().state, 'ready');
assert.equal(observeWindowsProcess(intent().pid).createdAt, intent().createdAt);
});
test('unverified startup expires with its original live child and never treats timeout or an unrelated health PID as proof', { timeout: 30000 }, async t => {
const { controller, intent } = ownedStartup(t, 1000); let connects = 0;
controller.connect = async () => { connects++; return null; };
controller.health = async () => { throw Object.assign(new Error('controlled unknown response'), { code: 'HOST_PORT_UNVERIFIED' }); };
await assert.rejects(controller.start(), error => error.code === 'HOST_START_UNVERIFIED');
const original = intent(); assert.equal(connects, 1); assert.equal(original.state, 'starting');
assert.equal(observeWindowsProcess(original.pid).createdAt, original.createdAt);
controller.health = async () => ({ pid: original.pid + 1 });
// The original unresolved launch prevents a second spawn, even after timeout.
await assert.rejects(controller.start(), error => error.code === 'HOST_START_UNVERIFIED');
assert.equal(intent().pid, original.pid);
});
@@ -0,0 +1,114 @@
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, launchWorkbench } from './helpers.mjs';
import { beginPlanTask, completeRead } from './fixtures/task-plan-fixture.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Store } from '../src/server/store.mjs';
import { RequirementReview } from '../src/core/tasks/requirement-review.mjs';
import { inspectDiagnosticContext } from '../src/core/tasks/diagnostic-context.mjs';
import { DeliveryReporter, renderDeliveryMarkdown } from '../src/core/delivery-report.mjs';
const answer = { message: { role: 'assistant', content: '只读解释已完成,原任务仍待处理。' }, usage: { prompt_tokens: 10, completion_tokens: 4 } };
function originalFailure(f) {
const original = beginPlanTask(f, '修复原工程并通过测试');
f.session.status = 'failed';
const diagnostic = { id: randomUUID(), source: 'model_connection', kind: 'network', stage: 'model_request', title: '原连接失败', message: '原POST结果未知,不得重放。', runId: original.id };
const event = f.store.event(f.session, { kind: 'error', phase: 'failed', diagnostic });
f.session.lastFailure = diagnostic; f.store.save(f.session);
return { original, diagnostic, event };
}
const requirement = (f, extra = {}) => ({ key: 'research', role: 'requirement', title: '解释原工程', criterion: '核对原读取', kind: 'analysis', tools: ['read_file'], method: 'evidence',
source: { kind: 'goal', id: f.session.taskId, start: 0, end: f.store.core.task.get(f.session.taskId).goal.length }, ...extra });
test('diagnostic failures and repeated questions retain the original execution error, task, checks and read-only authority', async t => {
const f = fixture(t), origin = originalFailure(f), provider = new Provider(); let round = 0;
provider.complete = async (_config, _messages, _project, _signal, _progress, _plan, tools) => {
assert.ok(tools.every(tool => !['propose_file', 'run_command', 'run_checks'].includes(tool.function.name)));
if (round++ === 0) throw new Error('本次诊断自身的独立错误');
return answer;
};
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
const task = f.store.core.task.get(f.session.taskId); task.verification.status = 'failed';
const instructions = structuredClone(task.instructions);
runtime.start(f.session.id, '前面的错误是怎么回事?'); await runtime.active.get(f.session.id).promise;
const first = f.store.core.run.get(f.session.runId), context = inspectDiagnosticContext(f.store, f.session, first);
assert.equal(first.status, 'failed'); assert.equal(context.evidenceVerified, true);
assert.equal(context.originalRunId, origin.original.id); assert.equal(context.failure.id, origin.diagnostic.id);
assert.deepEqual(context.failure.value, origin.diagnostic); assert.equal(f.session.lastFailure.runId, first.id);
runtime.start(f.session.id, '请解释刚才执行的错误'); await runtime.active.get(f.session.id).promise;
const second = f.store.core.run.get(f.session.runId), next = inspectDiagnosticContext(f.store, f.session, second);
assert.equal(second.status, 'completed'); assert.equal(second.stopReason, 'diagnostic_answered');
assert.equal(next.failure.sha256, context.failure.sha256); assert.equal(next.originalRunId, origin.original.id);
assert.equal(f.session.status, 'failed'); assert.equal(task.status, 'failed'); assert.equal(task.verification.status, 'failed');
assert.deepEqual(task.instructions, instructions); assert.equal(f.session.changes.length, 0); assert.equal(f.store.core.check_job.size, 0);
const report = await new DeliveryReporter(f.store).inspect(f.session);
assert.equal(report.status, 'needs_checks'); assert.equal(report.diagnostics.length, 2); assert.match(renderDeliveryMarkdown(report), /原POST结果未知/);
assert.match(report.diagnostics[0].failure.message, /诊断自身的独立错误/);
});
test('diagnostic binding survives reopen, rejects foreign Task references and detects original error or artifact tampering', t => {
const f = fixture(t), origin = originalFailure(f), run = f.store.beginRun(f.session, '解释下这个错误', false, f.store.config);
f.session.status = 'failed'; f.store.finishRun(f.session, run, 'diagnostic_answered', { status: 'completed' }); f.store.close();
const restored = new Store(f.data); f.onCleanup(() => restored.close()); const session = restored.session(f.session.id), saved = restored.core.run.get(run.id);
assert.equal(inspectDiagnosticContext(restored, session, saved).failure.id, origin.diagnostic.id);
assert.equal(inspectDiagnosticContext(restored, { ...session, taskId: randomUUID() }, saved).evidenceVerified, false);
const event = session.events.find(value => value.id === origin.event.id); event.diagnostic.message += 'changed';
assert.match(inspectDiagnosticContext(restored, session, saved).error, /原失败/); event.diagnostic.message = origin.diagnostic.message;
const artifact = restored.core.artifact.get(saved.diagnosticContext.artifactId); fs.writeFileSync(path.join(restored.artifactStore.root, artifact.relative), 'corrupt');
assert.match(inspectDiagnosticContext(restored, session, saved).error, /校验/);
});
test('a new Task never adopts a previous Task error from session.lastFailure', t => {
const f = fixture(t); originalFailure(f);
f.store.core.task.get(f.session.taskId).status = 'completed'; const current = beginPlanTask(f, '只解释当前新工程');
const run = f.store.beginRun(f.session, '前面的错误是怎么回事?', false, f.store.config), context = inspectDiagnosticContext(f.store, f.session, run);
assert.equal(context.evidenceVerified, true); assert.equal(context.failure, null); assert.equal(context.originalRunId, current.id);
assert.equal(context.taskId, current.taskId);
});
test('verified research and explicit no-test scopes do not demand an unrelated test job or claim engineering success', async t => {
for (const skip of [false, true]) {
const f = fixture(t); beginPlanTask(f, '只研究原README.md,不修改。' + (skip ? '无需测试。' : '')); const read = completeRead(f);
const manager = new RequirementReview(f.store), rows = [requirement(f), requirement(f, { key: 'ref', role: 'reference', method: 'scope' }),
requirement(f, { key: 'boundary', role: 'prohibition', method: 'scope' })].map(value => { if (value.method === 'scope') delete value.tools; return value; });
await manager.save(f.session, { expected_revision: 0, requirements: rows });
await manager.verify(f.session, { expected_revision: 1, key: 'research', evidence_id: read.id });
const reporter = new DeliveryReporter(f.store), report = await reporter.inspect(f.session);
assert.equal(report.status, skip ? 'scope_verified' : 'research_verified'); assert.equal(report.verifiedCheckJobId, null);
assert.equal(f.store.core.check_job.size, 0); assert.equal(report.followUp.gaps.length, 0);
const text = renderDeliveryMarkdown(report); assert.match(text, /不自动要求修改/); assert.match(text, /是否遵守仍需实际证据/);
if (!skip) { f.store.core.task.get(f.session.taskId).verification.status = 'failed'; assert.equal((await reporter.inspect(f.session)).status, 'needs_checks'); }
}
});
test('unknown scope and unknown operations remain actionable even after a diagnostic answer or read proof', async t => {
const f = fixture(t); beginPlanTask(f, '研究原工程,无需测试。'); const read = completeRead(f), manager = new RequirementReview(f.store);
const unknownScope = requirement(f, { key: 'unknown', role: 'unknown', method: 'scope' }); delete unknownScope.tools;
await manager.save(f.session, { expected_revision: 0, requirements: [requirement(f), unknownScope] });
await manager.verify(f.session, { expected_revision: 1, key: 'research', evidence_id: read.id });
const reporter = new DeliveryReporter(f.store), report = await reporter.inspect(f.session);
assert.equal(report.status, 'needs_requirements'); assert.match(report.followUp.nextStep, /澄清原文用途/);
f.store.core.tool.get(read.id).requiresVerification = true;
const unknown = await reporter.inspect(f.session); assert.equal(unknown.status, 'needs_verification'); assert.match(unknown.followUp.nextStep, /不要重放/);
});
test('delivery status HTTP is read-only, selection-bound and equal to the saved report after host restart', async t => {
const f = fixture(t); const origin = originalFailure(f);
const diagnostic = f.store.beginRun(f.session, '这个报错是什么意思', false, f.store.config);
f.session.status = 'failed'; f.store.finishRun(f.session, diagnostic, 'diagnostic_answered', { status: 'completed' }); f.store.close();
let host = await launchWorkbench(t, f.data); const route = '/sessions/' + f.session.id;
const url = route + '/delivery-status?taskId=' + f.session.taskId + '&runId=' + diagnostic.id;
const before = await host.api(route + '/export'), view = await host.api(url), after = await host.api(route + '/export');
const { exportedAt: beforeAt, ...beforeData } = before, { exportedAt: afterAt, ...afterData } = after;
assert.ok(beforeAt && afterAt); assert.deepEqual(afterData, beforeData); assert.equal(view.diagnostics[0].context.failure.id, origin.diagnostic.id);
await assert.rejects(host.api(url.replace(diagnostic.id, origin.original.id)), /所选原任务或运行/);
await host.api(route + '/delivery-reports', {});
const saved = await host.api(route), reportArtifact = saved.execution.artifacts.findLast(row => row.kind === 'delivery-report');
const report = JSON.parse((await host.api('/artifacts/' + reportArtifact.id + '?session=' + f.session.id + '&limit=64000')).content);
assert.deepEqual(report.followUp, view.followUp);
await host.close(); host = await launchWorkbench(t, f.data); assert.deepEqual((await host.api(url)).followUp, view.followUp);
});
@@ -0,0 +1,208 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { Provider, toolDefinitions, projectContext } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Store } from '../src/server/store.mjs';
import { sessionSnapshot, sessionContext } from '../src/server/diagnostics.mjs';
import { fixture, compatibleFixture, call, completion, until, assertToolPairs } from './helpers.mjs';
const policy = { attempts: 3, delays: [1, 2], timeoutMs: 1000, totalTimeoutMs: 3000 };
const response = result => new Response(JSON.stringify(result), { headers: { 'Content-Type': 'application/json' } });
const disconnected = () => new TypeError('fetch failed', { cause: Object.assign(new Error('connection reset'), { code: 'ECONNRESET' }) });
const refused = () => new TypeError('fetch failed', { cause: Object.assign(new Error('connection refused'), { code: 'ECONNREFUSED' }) });
const configure = f => f.store.saveConfig({ mode: 'compatible', baseUrl: 'http://127.0.0.1:1/v1', model: 'fixture-agent' });
test('routine engineering facts remove duplicate success excerpts while complete diagnostics and errors remain available', t => {
const f=fixture(t);
f.store.event(f.session,{kind:'tool',tool:'read_file',phase:'completed',input:{path:'src/a.mjs'},output:'successful source excerpt'});
f.store.event(f.session,{kind:'tool',tool:'read_file',phase:'failed',input:{path:'src/missing.mjs'},output:'original missing file error'});
f.store.event(f.session,{kind:'error',phase:'failed',label:'Original error',output:'fetch failed'});
const full=sessionSnapshot(f.session,f.project,f.store.config),context=sessionContext(f.session,f.project,f.store.config,String,{executionFactsIncluded:true});
assert.ok(full.recentTools.some(tool=>tool.outputExcerpt==='successful source excerpt'));
assert.ok(!context.includes('successful source excerpt'));assert.ok(context.includes('original missing file error'));
assert.ok(context.includes('model_connection'));assert.ok(context.includes('inspect_session'));assert.ok(context.includes('fetch failed'));
assert.deepEqual(sessionSnapshot(f.session,f.project,f.store.config),full);
});
test('a question about the previous workbench error receives local error context and current change states', async t => {
const f = fixture(t);
f.session.changes.push({ id: 'impl', path: 'src/catalog.js', status: 'applied' }, { id: 'tests', path: 'tests/catalog.test.mjs', status: 'applied' });
f.store.message(f.session, 'system', 'fetch failed');
f.store.event(f.session, { kind: 'error', label: '任务需要处理', phase: 'failed', output: 'fetch failed' });
const provider = new Provider();
let round = 0;
provider.complete = async (_config, messages) => {
assert.match(messages[0].content, /model_connection/);
assert.match(messages[0].content, /inspect_session/);
assert.match(messages[0].content, /"status":"applied"/);
if (++round === 1) return { message: completion([call('inspect', 'inspect_session', {})]).choices[0].message };
const result = JSON.parse(messages.findLast(message => message.role === 'tool').content);
assert.equal(result.lastFailure.source, 'model_connection');
assert.equal(result.lastFailure.stage, 'model_request');
assert.equal(result.lastFailure.historical, true);
assert.deepEqual(result.changes.map(change => change.status), ['applied', 'applied']);
assert.ok(result.environment.shell); assert.equal(result.environment.encoding, 'UTF-8');
return { message: { role: 'assistant', content: 'The recorded failure is a model request failure.' } };
};
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, '刚才工作台的 fetch failed 怎么回事?'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'completed'); assert.equal(round, 2);
assert.equal(f.session.events.filter(event => event.tool === 'search_files').length, 0);
assertToolPairs(f.session.providerMessages);
assert.ok(toolDefinitions.some(tool => tool.function.name === 'inspect_session'));
});
test('an unknown model request stops, and explicit resume never replays the approved command', async t => {
const f = fixture(t); configure(f);
fs.writeFileSync(path.join(f.projectRoot, 'counter.mjs'), "import fs from 'node:fs'; const previous = fs.existsSync('count.txt') ? Number(fs.readFileSync('count.txt','utf8')) : 0; fs.writeFileSync('count.txt', String(previous + 1));\n");
let requests = 0;
const provider = new Provider({ retryPolicy: policy, fetch: async (_url, input) => {
const messages = JSON.parse(input.body).messages; assertToolPairs(messages);
if (++requests === 1) return response(completion([call('command', 'run_command', { command: 'node counter.mjs', reason: 'Controlled one-time command' })]));
if (requests === 2) throw disconnected();
return response(completion([], 'Done'));
} });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Run the controlled counter.'); const done = runtime.active.get(f.session.id).promise;
await until(() => f.session.status === 'awaiting_approval'); runtime.decide(f.session.id, f.session.approvals[0].id, true);
await done;
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'count.txt'), 'utf8'), '1');
assert.equal(requests, 2); assert.equal(f.session.status, 'failed');
assert.equal(f.session.failure.outcome, 'unknown');
assert.equal(f.session.failure.retryable, false);
assert.equal(f.session.failure.failoverAllowed, false);
assert.equal(f.session.failure.attempts, 1);
const failedEvent = f.session.events.filter(event => event.kind === 'model').at(-1);
assert.equal(failedEvent.phase, 'failed'); assert.equal(failedEvent.diagnostic.outcome, 'unknown');
const userTurns = f.session.providerMessages.filter(message => message.role === 'user').length;
runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
assert.equal(requests, 3); assert.equal(f.session.status, 'completed');
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'count.txt'), 'utf8'), '1');
assert.equal(f.session.providerMessages.filter(message => message.role === 'user').length, userTurns);
assert.equal(f.session.approvals.length, 1);
assert.equal(f.session.events.filter(event => event.tool === 'run_command').length, 1);
assert.equal(f.session.events.filter(event => event.kind === 'model').at(-1).attempts, 1);
assert.ok(f.session.events.includes(failedEvent));
assertToolPairs(f.session.providerMessages);
});
test('exhausted refused connections preserve tool results, and explicit resume adds no new user turn or replay', async t => {
const f = fixture(t); configure(f);
let requests = 0, fail = true;
const provider = new Provider({ retryPolicy: { ...policy, attempts: 2 }, fetch: async (_url, input) => {
const messages = JSON.parse(input.body).messages; assertToolPairs(messages);
if (++requests === 1) return response(completion([call('read', 'read_file', { path: 'README.md' })]));
if (fail) throw refused();
assert.equal(messages.at(-1).role, 'tool'); return response(completion([], 'Recovered'));
} });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Read the fixture.'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'failed');
assert.equal(f.session.failure.source, 'model_connection'); assert.equal(f.session.failure.code, 'ECONNREFUSED');
assert.equal(f.session.failure.attempts, 2);
const userTurns = f.session.providerMessages.filter(message => message.role === 'user').length;
fail = false; runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'completed');
assert.equal(f.session.providerMessages.filter(message => message.role === 'user').length, userTurns);
assert.equal(f.session.events.filter(event => event.tool === 'read_file').length, 1);
assertToolPairs(f.session.providerMessages);
});
test('stopping during model backoff cancels further requests and clears the running request state', async t => {
const f = fixture(t); configure(f); let requests = 0;
const provider = new Provider({ retryPolicy: { ...policy, delays: [500, 500] }, fetch: async () => { requests++; throw refused(); } });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'test'); await until(() => f.session.events.some(event => event.phase === 'retrying'));
await runtime.stop(f.session.id);
assert.equal(requests, 1); assert.equal(f.session.status, 'paused');
assert.equal(f.session.events.find(event => event.kind === 'model').phase, 'interrupted');
assert.equal(provider.lastRequest.status, 'interrupted');
assert.equal(provider.lastRequest.attempts, 1);
assert.equal(Object.hasOwn(provider.lastRequest, 'kind'), false);
});
test('HTTP authentication errors are classified, redacted and never automatically retried', async t => {
const model = await compatibleFixture(t, () => ({ status: 401, body: { error: { message: 'Rejected fixture-private-key' } } }));
const provider = new Provider({ retryPolicy: policy }); provider.setKey('fixture-private-key');
await assert.rejects(provider.complete({ mode: 'compatible', baseUrl: model.baseUrl, model: 'fixture' }, [], null), error => {
assert.equal(error.diagnostic.kind, 'authentication'); assert.equal(error.diagnostic.httpStatus, 401);
assert.equal(error.diagnostic.attempts, 1); assert.equal(error.diagnostic.retryable, false);
assert.ok(error.message.includes('[已隐藏]')); assert.ok(!JSON.stringify(error.diagnostic).includes('fixture-private-key')); return true;
});
assert.equal(model.requests.length, 1);
});
test('temporary HTTP service errors retry and recover with recorded attempt counts', async t => {
const model = await compatibleFixture(t, (_url, _input, requests) => requests.length === 1
? { status: 503, body: { error: { message: 'Temporary fixture outage' } } } : completion([], 'Recovered'));
const provider = new Provider({ retryPolicy: policy }), progress = [];
const result = await provider.complete({ mode: 'compatible', baseUrl: model.baseUrl, model: 'fixture' }, [], null, undefined, value => progress.push(value));
assert.equal(result.message.content, 'Recovered'); assert.equal(model.requests.length, 2);
assert.ok(progress.some(item => item.phase === 'retrying')); assert.equal(provider.lastRequest.attempts, 2);
});
test('invalid and oversized protocol responses fail once with a useful response-format diagnosis', async () => {
for (const content of ['not JSON', 'x'.repeat(4 * 1024 * 1024 + 1)]) {
let requests = 0;
const provider = new Provider({ retryPolicy: policy, fetch: async () => { requests++; return new Response(content); } });
await assert.rejects(provider.complete({ mode: 'compatible', baseUrl: 'http://127.0.0.1:1/v1', model: 'fixture' }, [], null), error => error.diagnostic.kind === 'protocol' && !error.diagnostic.retryable);
assert.equal(requests, 1);
}
});
test('HTTP rate limits expose their source and obey the service retry interval before recovery', async t => {
const timestamps = [];
const model = await compatibleFixture(t, () => {
timestamps.push(Date.now());
return timestamps.length === 1 ? { status: 429, headers: { 'Retry-After': '1' }, body: { error: { message: 'Controlled rate limit' } } }
: completion([], 'Limit recovered');
});
const provider = new Provider({ retryPolicy: policy }), progress = [];
const result = await provider.complete({ mode: 'compatible', baseUrl: model.baseUrl, model: 'fixture' }, [], null, undefined, value => progress.push(value));
assert.equal(result.message.content, 'Limit recovered'); assert.equal(model.requests.length, 2);
assert.ok(timestamps[1] - timestamps[0] >= 950, 'The service retry interval must be respected.');
const retry = progress.find(value => value.phase === 'retrying');
assert.equal(retry.diagnostic.kind, 'rate_limit'); assert.equal(retry.diagnostic.httpStatus, 429);
assert.equal(retry.waitMs, 1000); assert.equal(provider.lastRequest.status, 'completed');
});
test('Windows commands preserve Chinese through a PowerShell pipeline into Node', { skip: process.platform !== 'win32' }, async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
const command = "@'\nconsole.log(JSON.stringify({ message: '工具·中文完成', value: 2 + 3 }));\n'@ | node --input-type=module\nexit $LASTEXITCODE";
const result = await runtime.executeCommand(command, f.projectRoot, new AbortController().signal, () => {});
assert.equal(result.status, 'completed', result.stderr); assert.equal(result.exit_code, 0);
assert.deepEqual(JSON.parse(result.stdout), { message: '工具·中文完成', value: 5 });
assert.ok(!result.stdout.includes('�')); assert.match(projectContext(f.project), /PowerShell 5\.1/);
});
test('Windows shell syntax errors stay readable and native command exit codes are retained', { skip: process.platform !== 'win32' }, async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
const invalid = await runtime.executeCommand("Write-Output '未执行的中文' && Write-Output 'ok'", f.projectRoot, new AbortController().signal, () => {});
assert.equal(invalid.status, 'failed'); assert.equal(invalid.stdout, ''); assert.match(invalid.stderr, /&&/);
assert.ok(!invalid.stderr.includes('�'));
const failed = await runtime.executeCommand("node -e 'process.exit(7)'", f.projectRoot, new AbortController().signal, () => {});
assert.equal(failed.status, 'failed'); assert.equal(failed.exit_code, 7);
});
test('review and completed states do not turn a generic continue click into a new model request', async t => {
const f = fixture(t), provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let requests = 0; provider.complete = async () => { requests++; throw new Error('Should not be requested'); };
for (const status of ['needs_review', 'completed']) {
f.session.status = status; assert.throws(() => runtime.resume(f.session.id), { status: 409 });
}
assert.equal(requests, 0); assert.equal(f.session.providerMessages.length, 0);
});
test('old session data receives a truthful historical connection diagnosis without rewriting history', async t => {
const f = fixture(t);
f.store.message(f.session, 'system', 'fetch failed'); f.store.event(f.session, { kind: 'error', phase: 'failed', output: 'fetch failed' });
const originalMessages = structuredClone(f.session.messages), reloaded = new Store(f.data);
f.onCleanup(() => reloaded.close());
const session = reloaded.session(f.session.id), visible = reloaded.publicSession(session);
assert.equal(visible.lastFailure.source, 'model_connection'); assert.equal(visible.lastFailure.historical, true);
assert.deepEqual(session.messages, originalMessages); assert.equal(Object.hasOwn(visible, 'providerMessages'), false);
const snapshot = sessionSnapshot(session, f.project, f.store.config);
assert.match(snapshot.lastFailure.detail, /没有保存底层/);
});
@@ -0,0 +1,60 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { fixture, launchWorkbench, until } from './helpers.mjs';
import { createEngineFixture } from '../scripts/engine-fixtures.mjs';
test('engine HTTP checks bind the detected installation and current task, page original bytes and preserve them across host restart', async t => {
const f = fixture(t), root = createEngineFixture(path.join(f.root, 'sdk 源码 #'));
f.store.close(); let host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: root });
const route = '/sessions/' + f.session.id, installation = await host.api('/engines/deepseek/availability');
assert.equal(installation.status, 'available'); assert.equal(installation.capabilities.projectExecution, false);
await assert.rejects(host.api(route + '/engines/jobs', { root, identitySha256: '0'.repeat(64) }), /检测结果不一致/);
assert.equal((await host.api(route)).execution.engineJobs.length, 0);
const started = await host.api(route + '/engines/jobs', { root, identitySha256: installation.identitySha256 });
const jobRoute = route + '/engines/jobs/' + started.job.id;
await until(async () => (await host.api(route)).execution.engineJobs.at(-1).reportArtifactId, 15000);
const job = await host.api(jobRoute), session = await host.api(route);
assert.equal(job.status, 'completed'); assert.equal(job.report, undefined); assert.equal(job.reportSummary.modelRequests, 0);
assert.equal(job.taskId, session.execution.task.id); assert.equal(job.runId, session.execution.run.id);
assert.equal((await host.api(route + '/engines')).jobs[0].id, job.id);
const response = await fetch(host.url + '/api' + jobRoute + '/download', { headers: { 'X-Workbench-Token': host.bootstrap.token } });
assert.equal(response.status, 200); assert.match(response.headers.get('content-disposition'), /attachment/);
const text = await response.text(), original = JSON.parse(text);
assert.equal(createHash('sha256').update(text).digest('hex'), job.reportSha256); assert.equal(original.owner.sessionId, f.session.id);
assert.equal(original.report.responses.length, 6); assert.equal(original.report.cleanup.rootExited, true);
let content = '', offset = 0, page;
do {
page = await host.api(jobRoute + '/report?offset=' + offset + '&limit=1700'); assert.equal(page.sha256, job.reportSha256);
content += page.content; offset += page.content.length;
} while (page.truncated);
assert.equal(content, text); await assert.rejects(host.api(jobRoute + '/report?limit=0'), /读取位置或页长/);
const other = await host.api('/sessions', { projectId: f.project.id });
await assert.rejects(host.api('/sessions/' + other.id + '/engines/jobs/' + job.id + '/report'), /不属于/);
const wire = path.join(root, 'wire.jsonl'), before = fs.readFileSync(wire);
await host.close(); host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: root });
const reopened = await host.api(jobRoute); assert.equal(reopened.status, 'completed'); assert.equal(reopened.reportSha256, job.reportSha256);
assert.equal((await host.api(jobRoute + '/report?limit=1700')).content, text.slice(0, 1700));
assert.deepEqual(fs.readFileSync(wire), before); assert.equal((await host.api(route)).execution.task.usage.requests, 0);
});
test('HTTP stop seals a pending SDK call, rejects automatic replay and keeps failures attached to the original session', async t => {
const f = fixture(t), root = createEngineFixture(path.join(f.root, 'sdk'), { mode: 'hang-init' }); f.store.close();
const host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: root }), route = '/sessions/' + f.session.id;
const started = await host.api(route + '/engines/jobs', { root });
await until(async () => (await host.api(route)).execution.engineJobs.at(-1).status === 'initializing', 15000);
const other = await host.api('/sessions', { projectId: f.project.id });
await assert.rejects(host.api('/sessions/' + other.id + '/messages', { content: 'owned request' }), /在执行/);
await host.api(route + '/engines/stop', {});
const job = await host.api(route + '/engines/jobs/' + started.job.id);
assert.equal(job.status, 'cancelled'); assert.equal(job.reportSummary.rootExited, true); assert.equal(job.taskId, started.job.taskId);
await assert.rejects(host.api(route + '/engines/jobs', { root }), /重新验证/);
const text = fs.readFileSync(path.join(root, 'wire.jsonl'), 'utf8'); assert.equal(text.includes('notifications/cancelled'), false);
fs.writeFileSync(path.join(root, 'fixture-options.json'), '{}');
const retry = await host.api(route + '/engines/jobs', { root, retry: true });
await until(async () => (await host.api(route)).execution.engineJobs.at(-1).reportArtifactId, 15000);
assert.equal((await host.api(route + '/engines/jobs/' + retry.job.id)).status, 'completed');
assert.notEqual(retry.job.id, job.id); assert.equal((await host.api(route)).execution.engineJobs.length, 2);
});
@@ -0,0 +1,90 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { engineCanonicalDigest } from '../src/shared/contracts/engines.mjs';
import { nativeMessagesToChat } from '../src/shared/contracts/engine-execution.mjs';
import { engineHistoryGroups,engineResultView,engineContextIndex,engineContextView } from '../src/shared/contracts/engine-context.mjs';
const root=path.resolve('docs/live-model-purge-1.24-evidence'),read=file=>JSON.parse(fs.readFileSync(file,'utf8'));
const manifest=read(path.join(root,'manifest.json'));
const artifacts=new Map(manifest.artifacts.map(x=>[x.id,x]));
function replay(name){
const result=read(path.join(root,'samples',name,'result.json'));
const report=read(path.join(root,'samples',name,'engine-'+result.engineRuns[0].id+'.json'));
const session=read(path.join(root,'samples',name,'original-child-session.json'));
const input=report.native.bridgeRequests.filter(x=>x.route==='/model').at(-1).data;
const messages=nativeMessagesToChat(input),evidence=report.calls.map(call=>({
callId:call.sourceCallId,name:call.name,argumentsSha256:call.argumentsSha256,
content:fs.readFileSync(path.join(root,artifacts.get(call.outputArtifactId).file),'utf8'),
artifactId:call.outputArtifactId,sha256:call.outputSha256,invocationId:call.invocationId,state:call.status,requiresVerification:false,
}));
const index=engineContextIndex(messages,evidence,{artifact_id:'original-native-input',sha256:engineCanonicalDigest(input)});
const config={...session.execution.run.model,modelRouting:session.execution.run.model.routing,maxOutputTokens:input.maxTokens};
const projected=engineContextView(messages,input.tools.map(x=>({type:'function',function:x})),config,index,{id:'original-index',sha256:engineCanonicalDigest(index)});
return {input,messages,index,config,projected,evidence};
}
for(const name of ['r1-off','r1-on','r2-on','r2-off'])test('sealed '+name+' failure fits its original 16K config with authentic latest status and readable source',()=>{
const {input,messages,index,config,projected}=replay(name);
assert.equal(config.contextLimit,16000);assert.equal(projected.mode,'bounded');
assert.ok(projected.before.estimatedInputTokens+input.maxTokens+128>16000);
assert.ok(projected.metrics.estimatedInputTokens<=projected.inputBudget);
assert.ok(projected.metrics.estimatedInputTokens+input.maxTokens+projected.safetyTokens<=16000);
assert.deepEqual(projected.messages.filter(x=>['system','developer','user'].includes(x.role)).slice(1),messages.filter(x=>['system','developer','user'].includes(x.role)));
assert.doesNotThrow(()=>engineHistoryGroups(projected.messages));
const text=JSON.stringify(projected.messages),latest=index.groups.filter(x=>x.results.length).at(-1);
for(const result of latest.results)assert.ok(text.includes(result.call_id)||text.includes(result.result_artifact_id));
if(name!=='r1-on'){assert.ok(text.includes('failed'));assert.ok(text.includes('logArtifactId'));assert.ok(text.includes('"failed":9')||text.includes('\\"failed\\":9'));}
assert.ok(text.includes('original-index'));assert.ok(projected.archivedGroups.length>0);
});
test('archived native call pairs require exact saved arguments/results rather than model-written summaries',()=>{
const {messages,evidence}=replay('r1-off');
const changed=structuredClone(evidence);changed[0].content+='changed';
assert.throws(()=>engineContextIndex(messages,changed,{}),/参数或结果/);
assert.throws(()=>engineContextIndex(messages,evidence.slice(1),{}),/缺少所属/);
const malformed=structuredClone(messages);malformed.find(x=>x.role==='tool').tool_call_id='foreign';
assert.throws(()=>engineHistoryGroups(malformed),/缺少或错配/);
});
test('full system/user requirements and mandatory current failure can refuse rather than be trimmed',()=>{
const {messages,index,config}=replay('r1-off');
const required={role:'user',content:'Do not change protected files. 原要求。'.repeat(2000)};
const original=structuredClone([...messages,required]);
const view=engineContextView(original,[],{...config,contextLimit:1600},index,{id:'idx',sha256:'original'});
assert.ok(view.metrics.estimatedInputTokens>view.inputBudget);assert.ok(view.messages.some(x=>x.content===required.content));
assert.deepEqual(original,[...messages,required]);
});
test('small native input is byte-preserving; developer instructions remain mandatory after compaction',()=>{
const messages=[{role:'developer',content:'Bounded host policy'},{role:'user',content:'Fix original code'}];
const config={model:'fixture',contextLimit:16000,maxOutputTokens:512};
const view=engineContextView(messages,[],config,null,{id:null,sha256:null});
assert.equal(view.mode,'full');assert.deepEqual(view.body.messages,messages);assert.deepEqual(view.archivedGroups,[]);
});
test('older failing checks and pending proposals remain visible after a later large file read',()=>{
const {messages,evidence,config}=replay('r1-off');
const changes={role:'assistant',content:'',tool_calls:[{id:'proposal',type:'function',function:{name:'propose_file',arguments:'{"path":"src/amount.mjs"}'}}]};
const source={role:'assistant',content:'',tool_calls:[{id:'read',type:'function',function:{name:'read_file',arguments:'{"path":"test/expense.test.mjs"}'}}]};
const returned=[{role:'tool',tool_call_id:'proposal',content:JSON.stringify({status:'pending',change_id:'pending-amount',path:'src/amount.mjs'})},
{role:'tool',tool_call_id:'read',content:JSON.stringify({path:'test/expense.test.mjs',content:'actual old file content\n'.repeat(2000)})}];
const original=[...messages,changes,returned[0],source,returned[1]];
const proof=[...evidence,...[changes,source].map((item,i)=>({callId:item.tool_calls[0].id,name:item.tool_calls[0].function.name,
argumentsSha256:engineCanonicalDigest(JSON.parse(item.tool_calls[0].function.arguments)),content:returned[i].content,
artifactId:'owned-'+i,sha256:'saved-'+i,invocationId:'tool-'+i,state:'completed',requiresVerification:false}))];
const index=engineContextIndex(original,proof,{}),view=engineContextView(original,[],config,index,{id:'history',sha256:'original'});
const text=JSON.stringify(view.messages);assert.equal(view.mode,'bounded');assert.ok(view.metrics.estimatedInputTokens<=view.inputBudget);
assert.ok(text.includes('pending-amount'));assert.ok(text.includes('failed'));assert.ok(text.includes('logArtifactId'));
assert.ok(text.includes('recorded_operations'));assert.ok(text.includes('view_content_truncated'));assert.doesNotThrow(()=>engineHistoryGroups(view.messages));
});
test('bounded result keeps every check status and cuts Unicode only on whole characters',()=>{
const results=Array.from({length:9},(_,index)=>({id:'check-'+index,status:index===8?'failed':'passed',logArtifactId:'log-'+index,
tests:{total:1,passed:index===8?0:1,failed:index===8?1:0},logExcerpt:'a'.repeat(1199)+'😀diagnostic'}));
const view=engineResultView(JSON.stringify({results,content:'a'.repeat(2399)+'😀file'}));
assert.equal(view.results.length,9);assert.equal(view.results[8].status,'failed');assert.equal(view.results[8].logArtifactId,'log-8');
assert.equal(view.results[0].logExcerpt,undefined);assert.equal(view.content_excerpt,'a'.repeat(2399));assert.equal(view.view_content_truncated,true);
assert.equal(engineResultView(JSON.stringify({results:[{...results[0],status:'failed'}]})).results[0].logExcerpt,'a'.repeat(1199));
assert.throws(()=>engineContextView([{role:'user',content:'x'.repeat(10000)}],[],{model:'fixture',contextLimit:2000,maxOutputTokens:512},null,null),/归档索引/);
});
@@ -0,0 +1,44 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, until } from './helpers.mjs';
import { workerHttp } from './fixtures/workers-http.mjs';
import { ControlledWorkerFixture } from './fixtures/controlled-worker.mjs';
import { createEngineFixture } from '../scripts/engine-fixtures.mjs';
import { EngineRunner } from '../src/core/engines/engine-runner.mjs';
import { resolveControlledInstallation, CONTROLLED_CRITICAL_FILES } from '../src/core/engines/controlled-installation.mjs';
test('controlled engine HTTP freezes one child, pages original owned input/report, rejects foreign reads and prevents changed-source reuse', async t => {
const f = fixture(t), host = await workerHttp(f), root = createEngineFixture(path.join(f.root, 'harness'));
for (const file of CONTROLLED_CRITICAL_FILES) { fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'export const fixture = true;\n'); }
host.runtime.controlledEngines = new EngineRunner(f.store, host.provider, { directoryRoot: path.join(f.root, 'engines'), sessionFactory: () => new ControlledWorkerFixture() });
const installation = await host.api('/engines/deepseek/controlled-availability?root=' + encodeURIComponent(root));
assert.equal(installation.controlledStatus, 'available');
const input = { goal: 'HTTP controlled engine', engine: { id: 'deepseek-harness', root, controlledSha256: installation.controlledSha256 },
children: [{ label: 'engine-1', goal: 'Repair and check one child', paths: ['src/a.mjs'] }] };
const route = '/sessions/' + f.session.id, group = (await host.api(route + '/collaborations', input)).group;
assert.equal(group.children.length, 1); const action = operation => host.api(route + '/collaborations/' + group.id + '/' + operation, { fingerprint: group.fingerprint });
await action('prepare'); await until(() => f.store.core.collaboration.get(group.id).status === 'ready', 20000);
await action('start'); await until(() => f.store.core.collaboration.get(group.id).status === 'awaiting_review', 20000);
const childRoute = '/sessions/' + group.children[0].sessionId, view = await host.api(childRoute + '/engine-runs'), job = view[0];
assert.equal(job.status, 'completed'); assert.equal(job.nativeSummary.treeConfirmed, true); assert.equal(job.modelRequests, 2); assert.equal(job.toolRequests, 1);
const original = f.store.artifactStore.read(f.store.core.artifact.get(job.reportArtifactId));
const first = await host.api(childRoute + '/engine-runs/' + job.id + '/report?offset=0&limit=200');
assert.equal(first.content, original.slice(0, 200)); assert.equal(first.sha256, job.reportSha256); assert.equal(first.truncated, true);
const second = await host.api(childRoute + '/engine-runs/' + job.id + '/report?offset=200&limit=200'); assert.equal(second.content, original.slice(200, 400));
const raw = await fetch(host.url + '/api' + childRoute + '/engine-runs/' + job.id + '/download', { headers: { 'X-Workbench-Token': host.token } });
assert.equal(await raw.text(), original); assert.match(raw.headers.get('content-disposition'), /attachment/);
assert.equal((await host.api(childRoute + '/engine-runs/' + job.id + '/input?limit=200')).sha256, job.inputSha256);
assert.equal((await host.raw(childRoute + '/engine-runs/' + job.id + '/report?offset=999999')).status, 400);
assert.equal((await host.raw(childRoute + '/engine-runs', undefined, 'foreign')).status, 403);
assert.ok((await host.raw(route + '/engine-runs/' + job.id + '/report')).status >= 400);
assert.equal((await host.raw(childRoute + '/engines/jobs', { root })).status, 409);
assert.equal((await host.api(childRoute + '/execution')).engineRuns[0].id, job.id);
const artifact = f.store.core.artifact.get(job.reportArtifactId); fs.appendFileSync(path.join(f.store.artifactStore.root, artifact.relative), 'damage');
assert.ok((await host.raw(childRoute + '/engine-runs/' + job.id + '/report')).status >= 400);
const before = f.store.core.task.size; fs.appendFileSync(resolveControlledInstallation(f.root, root).controlledInputs[0].path, '// changed\n');
const another = f.store.createSession(f.project.id);
assert.equal((await host.raw('/sessions/' + another.id + '/collaborations', input)).status, 409);
assert.equal(f.store.core.task.size, before); assert.equal(host.requests.length, 2);
});
@@ -0,0 +1,270 @@
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, assertToolPairs } from './helpers.mjs';
import { controlledWorker, ControlledWorkerFixture } from './fixtures/controlled-worker.mjs';
import { applyWorkerProposals, approveWorkerChecks, ownerBytes } from './fixtures/workers-project.mjs';
import { ControlledDeepSeekSession } from '../src/core/engines/controlled-session.mjs';
import { Store } from '../src/server/store.mjs';
import { nativeMessagesToChat, originalEngineRun, verifyEngineModelInput } from '../src/shared/contracts/engine-execution.mjs';
const usage = { total_tokens: 168, prompt_tokens: 123, completion_tokens: 45 };
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;
}
const jobs = (f, group) => [...f.store.core.engine_run.values()].filter(job => job.collaborationId === group.id);
test('a sealed whole-batch review pauses the owned native session without certifying engine completion or losing the saved host result',async t=>{
const f=fixture(t);let requests=0;
const context=controlledWorker(f,{respond:({project})=>{
requests++;const session=[...f.store.sessions.values()].find(value=>value.projectId===project.id);
const calls=requests===1?[call('proposal','propose_file',{path:'src/a.mjs',content:'export const value=1;\n',reason:'owned review'})]
:[call('review','prepare_change_set',{change_ids:session.changes.filter(value=>value.status==='pending').map(value=>value.id)})];
return {message:{role:'assistant',content:'',tool_calls:calls},usage};
}}),group=await prepare(f,context);
await(await context.manager.start(f.session,group.id,group.fingerprint)).promise;
const session=f.store.session(group.children[0].sessionId),job=jobs(f,group)[0];
assert.equal(requests,2);assert.equal(session.status,'needs_review');assert.equal(group.status,'awaiting_review');
assert.equal(job.status,'paused');assert.equal(job.verified,false);assert.equal(job.requiresVerification,false);assert.equal(job.error,null);
assert.equal(job.native.cleanup.rootExited,true);assert.equal(job.reviewHandoff.tools.length,1);
const result=job.reviewHandoff.tools[0];assert.equal(result.resultArtifactId,f.store.core.tool.get(result.invocationId).resultArtifactId);
assert.equal(f.store.savedToolResult(session.id,result.callId).change_set_id,job.reviewHandoff.changeSetId);
assert.ok(job.calls.some(value=>value.status!=='returned'),'saved host result is not claimed as delivered to the native engine');
originalEngineRun(f.store,session,job);
});
test('a changed review seal still closes the owned native process and cannot claim a verified review handoff',async t=>{
const f=fixture(t);let requests=0,changed=false;
const save=f.store.save.bind(f.store);f.store.save=(session,event)=>{
const result=save(session,event);
if(event?.type==='change_set.awaiting_review'){
const set=f.store.core.change_set.get(event.detail.changeSetId);set.fingerprint='changed-after-review-ready';changed=true;
}
return result;
};
const context=controlledWorker(f,{respond:({project})=>{
requests++;const session=[...f.store.sessions.values()].find(value=>value.projectId===project.id);
const calls=requests===1?[call('proposal','propose_file',{path:'src/a.mjs',content:'export const value=1;\n',reason:'owned review'})]
:[call('review','prepare_change_set',{change_ids:session.changes.filter(value=>value.status==='pending').map(value=>value.id)})];
return {message:{role:'assistant',content:'',tool_calls:calls},usage};
}}),group=await prepare(f,context);
await(await context.manager.start(f.session,group.id,group.fingerprint)).promise;
const session=f.store.session(group.children[0].sessionId),job=jobs(f,group)[0];
assert.equal(changed,true);assert.equal(requests,2);assert.equal(job.status,'failed');assert.equal(job.verified,false);
assert.equal(job.reviewHandoff,undefined);assert.equal(job.native.cleanup.rootExited,true);
assert.equal(context.runtime.controlledEngines.isActive(session.id),false);originalEngineRun(f.store,session,job);
});
test('native text and call IDs preserve original result pairing while unsupported content fails closed', () => {
const input = { system: 'fixed', messages: [{ role: 'assistant', content: [{ type: 'reasoning', text: 'private' }, { type: 'tool-call', id: 'native', name: 'read_file', arguments: '{"path":"README.md"}' }] },
{ role: 'tool', toolCallId: 'native', content: [{ type: 'text', text: 'result' }] }] };
const mapped = nativeMessagesToChat(input); assert.equal(mapped[1].content, ''); assert.equal(mapped[1].tool_calls[0].id, 'native'); assert.equal(mapped[2].tool_call_id, 'native');
assert.throws(() => nativeMessagesToChat({ messages: [{ role: 'user', content: [{ type: 'image', url: 'invalid' }] }] }), /尚未验证/);
assert.throws(() => nativeMessagesToChat({ messages: [{ role: 'tool', content: [] }] }), /toolCallId/);
});
test('one engine child freezes real installation identity before creating tasks and rejects a changed source', async t => {
const f = fixture(t), context = controlledWorker(f);
const group = await context.manager.plan(f.session, context.definition); assert.equal(group.children.length, 1);
assert.equal(group.children[0].engine.controlledSha256, context.installation.controlledSha256);
fs.appendFileSync(context.installation.controlledInputs[0].path, '// changed\n');
assert.throws(() => context.runtime.engineRunner().selected(context.definition.engine), /不一致/);
await (await context.manager.prepare(f.session, group.id, group.fingerprint)).promise;
const worker = f.store.core.worker.get(group.children[0].id); group.deadline = Date.now() + 60000;
assert.throws(() => context.manager.configFor(group, worker), /已变化/);
assert.equal(context.requests.length, 0);
});
test('owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs', async t => {
const f = fixture(t), context = controlledWorker(f), original = ownerBytes(f), group = await prepare(f, context);
await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
assert.equal(group.status, 'awaiting_review', group.summary); assert.equal(jobs(f, group)[0].status, 'completed');
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 child = group.children[0], session = f.store.session(child.sessionId), worker = f.store.core.worker.get(child.id);
assert.equal(worker.status, 'completed', worker.summary); assert.equal(session.approvals.filter(value => value.status === 'approved').length, 1);
const all = jobs(f, group), requestCount = all.reduce((sum, job) => sum + job.requests.length, 0);
assert.equal(all.length, 2); assert.equal(f.store.core.task.get(worker.taskId).usage.requests, requestCount);
assert.equal(f.store.core.task.get(worker.taskId).usage.totalTokens, requestCount * 168);
assert.ok(all.every(job => job.status === 'completed' && job.requests.every(request => request.accountedAttempts === 1)));
for (const job of all) {
originalEngineRun(f.store, session, job);
for (const request of job.requests) verifyEngineModelInput(f.store, session, job, request);
for (const entry of job.calls) {
assert.equal(entry.callId, job.id + ':' + entry.sourceCallId);
assert.equal(f.store.core.tool.get(entry.invocationId).callId, entry.callId); assert.ok(entry.outputArtifactId);
}
}
assertToolPairs(session.providerMessages);
assert.ok(context.requests.some(request => request.messages.some(message => message.role === 'tool'
&& JSON.parse(message.content).status === 'completed' && JSON.parse(message.content).check_status === 'passed')));
assert.equal([...f.store.core.context_plan.values()].filter(plan => plan.taskId === worker.taskId).some(plan => plan.workerDispatchedAt), false);
assert.equal(context.runtime.controlledEngines.drivers.size, 0);
const finalReport = f.store.core.artifact.get(all.at(-1).reportArtifactId);
fs.appendFileSync(path.join(f.store.artifactStore.root, finalReport.relative), 'damaged');
await assert.rejects(context.manager.output(session, worker), /校验失败/);
});
test('forged native scope cannot change a parent file and a final completion claim cannot replace worker proof', async t => {
const f = fixture(t), context = controlledWorker(f, { respond: ({ messages }) => messages.some(message => message.role === 'tool')
? { message: { role: 'assistant', content: 'claimed complete' }, usage }
: { message: { role: 'assistant', content: '', tool_calls: [call('outside', 'propose_file', { path: 'README.md', content: 'forged', reason: 'forged' })] }, usage } });
const original = ownerBytes(f), group = await prepare(f, context);
await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
const session = f.store.session(group.children[0].sessionId); assert.deepEqual(ownerBytes(f), original);
assert.equal(session.changes.length, 0); assert.equal(group.status, 'paused'); assert.equal(f.store.core.run.get(session.runId).stopReason, 'worker_incomplete');
assert.equal(context.native[0].received[0].result.status, 'error'); assert.match(context.native[0].received[0].result.message, /范围/);
});
test('duplicate native IDs are rejected without replaying a saved tool', async t => {
const f = fixture(t), context = controlledWorker(f, { respond: () => ({ message: { role: 'assistant', content: '', tool_calls: [call('same', 'read_file', { path: 'README.md' })] }, usage }) });
const group = await prepare(f, context); await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
const job = jobs(f, group)[0]; assert.equal(job.calls.length, 1); assert.equal(f.store.core.tool.size, 1); assert.equal(job.status, 'failed');
assert.equal(context.requests.length, 2); assert.match(job.error, /已经登记/);
});
test('native input exceeding the context budget is retained and sends no model request', async t => {
class Oversized extends ControlledWorkerFixture {
async wait() { const { onModel, tools, config, signal } = this.options; return onModel({ model: config.model, maxTokens: config.maxOutputTokens,
tools: tools.map(value => value.function), messages: [{ role: 'user', content: [{ type: 'text', text: 'x'.repeat(2000000) }] }] }, { id: randomUUID() }, signal); }
}
const f = fixture(t), context = controlledWorker(f, { sessionFactory: () => new Oversized() }), 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, group)[0].requests.length, 0);
assert.equal(f.store.core.task.get(group.children[0].taskId).usage.requests, 0); assert.match(jobs(f, group)[0].error, /上下文窗口可用容量/);
const job=jobs(f,group)[0],rejection=job.inputRejections[0],artifact=f.store.owned('artifact',rejection.artifactId,job.sessionId);
const receipt=JSON.parse(f.store.artifactStore.read(artifact));
assert.equal(artifact.kind,'engine-model-rejection');assert.equal(artifact.sha256,rejection.sha256);assert.equal(receipt.dispatched,false);
assert.equal(receipt.budget.limitingReason,'context_limit');assert.equal(receipt.view.mode,'bounded');
assert.equal(Object.values(receipt.budget.breakdown).reduce((sum,value)=>sum+value,0),receipt.budget.requiredInputTokens);
assert.ok(receipt.body.messages.some(message=>message.content==='x'.repeat(2000000)));
const source=f.store.owned('artifact',receipt.view.sourceArtifactId,job.sessionId);
assert.equal(source.kind,'engine-native-input');assert.equal(source.sha256,receipt.view.sourceSha256);
});
test('bounded native sends verify original tool provenance and reject forged views or missing versions',async t=>{
const f=fixture(t);fs.writeFileSync(path.join(f.projectRoot,'README.md'),'Owned source observation\n'.repeat(1200));
const context=controlledWorker(f,{respond:({messages})=>({message:{role:'assistant',content:messages.some(message=>message.role==='tool')||messages.some(message=>message.content?.includes('latest_results'))?'':'Owned protocol-fixture history.\n'.repeat(1600),
...(messages.some(message=>message.role==='tool')||messages.some(message=>message.content?.includes('latest_results'))?{}:
{tool_calls:[call('read-large','read_file',{path:'README.md',limit:16000})]})},usage})});
f.store.saveConfig({contextLimit:16000,maxOutputTokens:2048});
const group=await prepare(f,context);await (await context.manager.start(f.session,group.id,group.fingerprint)).promise;
const job=jobs(f,group)[0],session=f.store.session(job.sessionId),request=job.requests.at(-1);
assert.equal(context.requests.length,2,job.error);assert.equal(job.status,'completed',job.error);
const original=verifyEngineModelInput(f.store,session,job,request);assert.equal(original.view.mode,'bounded');
assert.ok(original.view.beforeInputTokens>original.view.inputBudget);assert.ok(request.estimatedInputTokens<=original.view.inputBudget);
const forged=structuredClone(original);delete forged.view;
const record=f.store.artifactStore.write(session,f.store.core.run.get(job.runId),forged,{kind:'engine-model-input'});f.store.core.artifact.set(record.id,record);
assert.throws(()=>verifyEngineModelInput(f.store,session,job,{...request,inputArtifactId:record.id,inputSha256:record.sha256}),/视图版本/);
const invocation=f.store.core.tool.get(job.calls[0].invocationId);invocation.requiresVerification=true;
assert.throws(()=>verifyEngineModelInput(f.store,session,job,request),/尚未核对/);invocation.requiresVerification=false;
const previous=invocation.argsSha256;invocation.argsSha256='changed';assert.throws(()=>verifyEngineModelInput(f.store,session,job,request),/尚未核对/);invocation.argsSha256=previous;
const source=f.store.core.artifact.get(original.view.indexArtifactId),index=JSON.parse(f.store.artifactStore.read(source));
index.groups.find(group=>group.results.length).results[0].result.status='forged';
const changed=f.store.artifactStore.write(session,f.store.core.run.get(job.runId),index,{kind:'engine-history-index'});f.store.core.artifact.set(changed.id,changed);
const projected=structuredClone(original);projected.view.indexArtifactId=changed.id;projected.view.indexSha256=changed.sha256;
const replaced=f.store.artifactStore.write(session,f.store.core.run.get(job.runId),projected,{kind:'engine-model-input'});f.store.core.artifact.set(replaced.id,replaced);
assert.throws(()=>verifyEngineModelInput(f.store,session,job,{...request,inputArtifactId:replaced.id,inputSha256:replaced.sha256}),/索引/);
assert.doesNotThrow(()=>verifyEngineModelInput(f.store,session,job,request));assert.equal(context.requests.length,2);
});
test('stop retains a late reported model usage once and executes no tool from that output', async t => {
let release; const waiting = new Promise(resolve => { release = resolve; });
const f = fixture(t), context = controlledWorker(f, { respond: async () => { await waiting; return { message: { role: 'assistant', content: '', tool_calls: [call('late', 'propose_file', { path: 'src/a.mjs', content: 'late', reason: 'late' })] }, usage }; } });
const group = await prepare(f, context), started = context.manager.start(f.session, group.id, group.fingerprint);
await until(() => context.requests.length === 1); const stopping = context.runtime.stop(f.session.id); release();
await stopping; await started.promise;
const task = f.store.core.task.get(group.children[0].taskId), session = f.store.session(group.children[0].sessionId);
assert.equal(task.usage.requests, 1); assert.equal(task.usage.totalTokens, 168); assert.equal(session.changes.length, 0);
assert.equal(jobs(f, group)[0].calls.length, 0); assert.equal(jobs(f, group)[0].status, 'cancelled');
assert.ok(jobs(f, group)[0].requests[0].responseArtifactId); assert.equal(context.runtime.controlledEngines.drivers.size, 0);
});
test('a source change after the original model intent stops before native tools execute', async t => {
const f = fixture(t); let context;
context = controlledWorker(f, { respond: () => { fs.appendFileSync(context.installation.controlledInputs[0].path, '// runtime changed\n');
return { message: { role: 'assistant', content: '', tool_calls: [call('changed', 'propose_file', { path: 'src/a.mjs', content: 'bad', reason: 'bad' })] }, usage }; } });
const 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, group)[0].calls.length, 0);
assert.equal(jobs(f, group)[0].requiresVerification, false); assert.match(jobs(f, group)[0].error, /已变化/);
});
test('an engine source change while approval is pending invalidates the old permission before any check starts', async t => {
const f = fixture(t), context = controlledWorker(f, { 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);
const session = f.store.session(group.children[0].sessionId);
await until(() => session.approvals.some(approval => approval.status === 'pending'), 15000);
fs.appendFileSync(context.installation.controlledInputs[0].path, '// changed while awaiting approval\n');
context.runtime.decide(session.id, session.approvals.find(approval => approval.status === 'pending').id, true);
await started.promise; assert.equal(f.store.core.check_job.size, 0); assert.equal(jobs(f, group)[0].status, 'failed');
assert.equal(context.requests.length, 2); assert.match(jobs(f, group)[0].error, /已变化/);
});
test('missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text', async () => {
for (const kind of [null, 'failed', 'cancelled']) {
const native = new ControlledDeepSeekSession({}); native.request = async () => ({}); native.report = { receipt: { messageId: 'owned' }, notifications: [
{ method: 'session.event', params: { event: { type: 'agent/inbox/spliced', data: { inserted: [{ id: 'owned' }] } } } },
{ method: 'session.event', params: { event: { type: 'assistant/message', data: { message: { content: [{ type: 'text', text: 'claim' }] } } } } },
...(kind ? [{ method: 'session.event', params: { event: { type: 'turn/end', data: { reason: { kind } } } } }] : [])] };
await assert.rejects(native.wait(), /正常结束/);
}
});
test('closed root with an original live descendant remains unknown and retains its process tree', async () => {
const native = new ControlledDeepSeekSession({}, { processTreeReader: async () => ({ supported: true, processes: [{ pid: 91, createdAt: '2026-10-31T00:00:00Z' }] }) });
native.report = { process: { pid: 90, startedAt: new Date().toISOString() }, notifications: [] };
native.client = { exited: true, close: async () => ({ rootExited: true, exit: { code: 0 } }), trace: [], stderr: '' };
const result = await native.close({ force: true }); assert.equal(result.status, 'unknown'); assert.equal(result.cleanup.treeConfirmed, false);
assert.equal(result.process.descendants[0].pid, 91);
});
async function pendingRecovery(f, context) {
const group = await prepare(f, context); await (await context.manager.start(f.session, group.id, group.fingerprint)).promise;
const job = jobs(f, group)[0], session = f.store.session(job.sessionId), old = job.requests[0];
const input = verifyEngineModelInput(f.store, session, job, old);
const request = { id: randomUUID(), bridgeTraceId: randomUUID(), sourceSha256: old.sourceSha256, requestSha256: old.requestSha256,
requestBytes: old.requestBytes, estimatedInputTokens: old.estimatedInputTokens, reservedPerAttempt: old.reservedPerAttempt,
maxAttempts: 2, accountedAttempts: 0, observedAttempts: 1, status: 'dispatched', startedAt: new Date().toISOString(), dispatchedAt: new Date().toISOString() };
const artifact = f.store.artifactStore.write(session, f.store.core.run.get(job.runId), { schemaVersion: 1, owner: input.owner,
native: input.native, body: input.body, plan: { ...request, ...input.owner } }, { kind: 'engine-model-input' });
f.store.core.artifact.set(artifact.id, artifact); request.inputArtifactId = artifact.id; request.inputSha256 = artifact.sha256;
job.requests.push(request); job.status = 'running'; job.finishedAt = null; job.native = null; job.reportArtifactId = null; job.reportSha256 = null;
job.originalOutcome = null; job.process = { pid: 2147483000, startedAt: new Date().toISOString(), rootCreatedAt: '2026-10-31T00:00:00.000Z', descendants: [] };
group.status = 'running'; f.store.save(f.session);
const worker = f.store.core.worker.get(group.children[0].id); worker.status = 'running'; worker.ownerPid = null;
session.status = 'running'; const run = f.store.core.run.get(job.runId); run.status = 'running'; run.finishedAt = null;
f.store.save(session); return { job, request, worker, group, before: structuredClone(f.store.core.task.get(worker.taskId).usage) };
}
test('host recovery reserves original pending attempts exactly once and explicit review preserves unknown outcomes', async t => {
const f = fixture(t), context = controlledWorker(f), pending = await pendingRecovery(f, context); f.store.close();
const reopened = new Store(f.data), task = reopened.core.task.get(pending.worker.taskId);
assert.equal(task.usage.requests, pending.before.requests + 2); assert.equal(task.usage.unknownRequests, pending.before.unknownRequests + 2);
assert.equal(task.usage.estimatedUnknownTokens, pending.before.estimatedUnknownTokens + 2 * pending.request.reservedPerAttempt);
const firstUsage = structuredClone(task.usage); reopened.close();
const second = new Store(f.data); f.onCleanup(() => second.close()); assert.deepEqual(second.core.task.get(task.id).usage, firstUsage);
const { Runtime } = await import('../src/server/runtime.mjs'); const runtime = new Runtime(second, context.provider); f.onCleanup(() => runtime.stopAll());
const manager = runtime.workerManager(), parent = second.session(f.session.id), group = second.core.collaboration.get(pending.group.id);
assert.equal(group.status, 'unknown'); assert.throws(() => manager.start(parent, group.id, group.fingerprint), /核对/);
await (await manager.reconcile(parent, group.id, group.fingerprint)).promise;
assert.ok(group.evidence, group.summary); const original = structuredClone(second.core.engine_run.get(pending.job.id));
await manager.acknowledge(parent, group.id, group.fingerprint, group.evidence.sha256);
const reviewed = second.core.engine_run.get(pending.job.id); assert.equal(reviewed.status, 'resolved_external'); assert.equal(reviewed.verified, false);
assert.deepEqual(reviewed.originalOutcome, original.originalOutcome); assert.deepEqual(reviewed.requests, original.requests);
assert.equal(context.requests.length, 2); assert.equal(reviewed.review.result, 'unknown-preserved');
});
test('corrupted model reservation cannot reduce recovered budget or permit a new engine request', async t => {
const f = fixture(t), context = controlledWorker(f), pending = await pendingRecovery(f, context);
pending.request.reservedPerAttempt = 1; f.store.save(f.store.session(pending.worker.sessionId)); f.store.close();
const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); const job = reopened.core.engine_run.get(pending.job.id);
assert.equal(job.requiresVerification, true); assert.match(job.requests.at(-1).verificationError, /预算|不一致/);
assert.equal(reopened.executionSnapshot(reopened.session(pending.worker.sessionId)).accountedTaskTokens, pending.worker.budget.maxTokens);
assert.equal(job.requests.at(-1).recoveryConservativeCharge, true); assert.equal(context.requests.length, 2);
});
@@ -0,0 +1,186 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { EngineManager } from '../src/core/engine-manager.mjs';
import { DeepSeekAdapter } from '../src/core/engines/deepseek-adapter.mjs';
import { resolveDeepSeekInstallation } from '../src/core/engines/deepseek-installation.mjs';
import { Store } from '../src/server/store.mjs';
import { Runtime, validateToolArgs } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { createEngineFixture } from '../scripts/engine-fixtures.mjs';
import { fixture, until } from './helpers.mjs';
function setup(t, options = {}) {
const f = fixture(t), root = createEngineFixture(path.join(f.root, 'harness 源码 #'), options);
const manager = new EngineManager(f.store, { adapterFactory: installation => new DeepSeekAdapter(installation, { timeoutMs: 3000 }) });
f.onCleanup(async () => {
await manager.stopAll();
for (const job of manager.jobs(f.session)) {
const directory = path.resolve(job.directory), base = path.resolve(os.tmpdir(), 'AgentWorkbench-Engines');
assert.equal(path.dirname(directory), base); assert.equal(path.basename(directory), job.id);
fs.rmSync(directory, { recursive: true, force: true, maxRetries: 3 });
}
});
const audit = () => fs.existsSync(path.join(root, 'wire.jsonl')) ? fs.readFileSync(path.join(root, 'wire.jsonl'), 'utf8').trim().split('\n').map(line => JSON.parse(line)) : [];
return { ...f, root, manager, audit };
}
test('engine detection seals the actual CLI, SDK, profile and Node and rejects mixed versions without launch', t => {
const f = setup(t), current = resolveDeepSeekInstallation(process.cwd(), f.root);
assert.equal(current.status, 'available'); assert.equal(current.version, '0.2.1-rc.2'); assert.equal(current.inputs.length, 13);
const pkg = path.join(f.root, 'packages/sdk/server/package.json');
fs.writeFileSync(pkg, JSON.stringify({ version: 'another-version' }));
assert.equal(f.manager.availability(f.root).status, 'unsupported'); assert.throws(() => f.manager.start(f.session, { root: f.root }), /版本不一致/);
assert.equal(f.audit().length, 0); assert.equal(f.store.core.engine_job.size, 0);
fs.writeFileSync(pkg, JSON.stringify({ version: '0.2.1-rc.2' }));
const detected = f.manager.availability(f.root); fs.appendFileSync(path.join(f.root, 'apps/cli/lib/bin.js'), '\n// changed after detection\n');
assert.throws(() => f.manager.start(f.session, { root: f.root, identitySha256: detected.identitySha256 }), /检测结果不一致/);
assert.equal(f.audit().length, 0); assert.equal(f.store.core.engine_job.size, 0);
});
test('owned engine probe waits for real ready identity, verifies six native responses and saves immutable task evidence', async t => {
const f = setup(t, { readyDelayMs: 150 });
const originalBytes = fs.readFileSync(path.join(f.projectRoot, 'README.md'));
const started = f.manager.start(f.session, { root: f.root });
assert.throws(() => f.manager.start(f.session, { root: f.root }), /在执行/);
const job = await started.promise;
assert.equal(job.status, 'completed'); assert.equal(job.verification.verified, true); assert.equal(job.report.responses.length, 6);
assert.equal(job.report.modelRequests, 0); assert.equal(job.report.cleanup.rootExited, true); assert.equal(job.report.cleanup.exit.code, 0);
assert.equal(job.report.readiness.pid, job.pid); assert.equal(job.sdkSessionId, job.report.sdkSessionId);
assert.equal(f.store.executionSnapshot(f.session).engineJobs[0].report, undefined);
assert.equal(f.store.executionSnapshot(f.session).engineJobs[0].reportSummary.observedResponses, 6);
const original = JSON.parse(f.manager.originalReport(f.session, job));
assert.equal(original.owner.taskId, f.session.taskId); assert.equal(original.inputSha256, job.contractSha256);
assert.ok(original.report.responses[2].error.message.includes(original.report.responses[2].parameters.sessionId));
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'README.md')), originalBytes);
assert.equal(f.store.core.task.get(f.session.taskId).usage.requests, 0); assert.equal(f.session.status, 'paused');
assert.equal(f.audit().filter(value => value.type === 'wire' && value.value.method === 'session/prompt').length, 0);
});
test('an invalid startup nonce is rejected before initialize and preserves the process close and partial report', async t => {
const f = setup(t, { mode: 'bad-ready' }), job = await f.manager.start(f.session, { root: f.root }).promise;
assert.equal(job.status, 'failed'); assert.match(job.summary, /启动完成信号/); assert.equal(job.report.trace.length, 0);
assert.equal(job.report.cleanup.rootExited, true); assert.equal(f.audit().filter(value => value.type === 'wire').length, 0);
assert.equal(f.manager.summary(f.session, job).evidence_verified, false);
});
test('SDK identity and session directory mismatches retain raw responses and never become verified', async t => {
for (const mode of ['wrong-version', 'wrong-cwd', 'malformed']) {
const f = setup(t, { mode }), job = await f.manager.start(f.session, { root: f.root }).promise;
assert.equal(job.status, 'failed', mode); assert.equal(job.verification, null); assert.ok(job.reportArtifactId);
assert.equal(job.report.cleanup.rootExited, true); assert.equal(f.manager.summary(f.session, job).evidence_verified, false);
if (mode !== 'malformed') assert.ok(job.report.responses.length);
}
});
test('cancel during a pending initialize closes only the owned process without sending an unsupported MCP cancellation', async t => {
const f = setup(t, { mode: 'hang-init' }), started = f.manager.start(f.session, { root: f.root });
await until(() => started.job.status === 'initializing' && f.audit().some(value => value.value?.method === 'initialize'));
await f.manager.stop(f.session.id);
const job = await started.promise;
assert.equal(job.status, 'cancelled'); assert.equal(job.report.cleanup.rootExited, true);
assert.equal(job.report.cleanup.pid, job.pid); assert.equal(job.requiresVerification, false);
assert.equal(f.audit().filter(value => value.value?.method === 'notifications/cancelled').length, 0);
assert.throws(() => f.manager.start(f.session, { root: f.root }), /重新验证/);
});
test('cancel before readiness sends no protocol request and a user retry creates a different process and record', async t => {
const f = setup(t, { mode: 'no-ready' }), started = f.manager.start(f.session, { root: f.root });
await until(() => started.job.pid && f.audit().length);
await f.manager.stop(f.session.id); assert.equal((await started.promise).status, 'cancelled');
assert.equal(f.audit().filter(value => value.type === 'wire').length, 0);
fs.writeFileSync(path.join(f.root, 'fixture-options.json'), '{}');
const retry = await f.manager.start(f.session, { root: f.root, retry: true }).promise;
assert.equal(retry.status, 'completed'); assert.notEqual(retry.id, started.job.id); assert.notEqual(retry.pid, started.job.pid);
});
test('source or scratch changes and unexpected model traffic fail after preserving the actual observed result', async t => {
for (const mode of ['change-input', 'change-workspace', 'model-request']) {
const f = setup(t, { mode }), job = await f.manager.start(f.session, { root: f.root }).promise;
assert.equal(job.status, 'failed', mode); assert.equal(job.report.cleanup.rootExited, true); assert.equal(job.verification, null);
if (mode === 'model-request') { assert.equal(job.report.modelRequests, 1); assert.equal(job.report.providerRequests, 0); }
assert.equal(f.session.changes.length, 0);
}
});
test('unowned idle notifications stay quarantined and server requests receive a rejection without granting operations', async t => {
const f = setup(t, { mode: 'unowned-event' }), job = await f.manager.start(f.session, { root: f.root }).promise;
assert.equal(job.status, 'completed'); assert.equal(job.report.notifications.length, 0); assert.equal(job.report.unownedNotifications.length, 1);
assert.equal(f.store.core.task.get(f.session.taskId).status, 'paused');
const reverse = setup(t, { mode: 'reverse-request' }), response = await reverse.manager.start(reverse.session, { root: reverse.root }).promise;
assert.equal(response.status, 'completed'); assert.equal(response.report.rejectedServerRequests.length, 1);
assert.ok(reverse.audit().some(value => value.value?.id === 'reverse-control' && value.value?.error?.code === -32601));
assert.equal(reverse.session.approvals.length, 0); assert.equal(reverse.session.changes.length, 0);
});
test('engine probe environment excludes parent secrets and NODE_OPTIONS while preserving operating system paths', async t => {
const f = setup(t), previousSecret = process.env.OWNED_ENGINE_TEST_SECRET, previousOptions = process.env.NODE_OPTIONS;
process.env.OWNED_ENGINE_TEST_SECRET = 'owned-non-credential-test'; process.env.NODE_OPTIONS = '--no-warnings';
try {
const job = await f.manager.start(f.session, { root: f.root }).promise; assert.equal(job.status, 'completed');
const launch = f.audit()[0]; assert.equal(launch.secretInherited, false); assert.equal(launch.nodeOptionsInherited, false);
} finally {
if (previousSecret === undefined) delete process.env.OWNED_ENGINE_TEST_SECRET; else process.env.OWNED_ENGINE_TEST_SECRET = previousSecret;
if (previousOptions === undefined) delete process.env.NODE_OPTIONS; else process.env.NODE_OPTIONS = previousOptions;
}
});
test('foreign sessions and altered reports or task bindings cannot reuse verified engine evidence', async t => {
const f = setup(t), job = await f.manager.start(f.session, { root: f.root }).promise, other = f.store.createSession(f.project.id);
assert.throws(() => f.manager.originalReport(other, job), /不属于/);
const original = job.sdkSessionId; job.sdkSessionId = other.id;
assert.equal(f.manager.summary(f.session, job).evidence_verified, false); assert.throws(() => f.manager.originalReport(f.session, job), /不一致/); job.sdkSessionId = original;
const artifact = f.store.core.artifact.get(job.reportArtifactId), filename = path.join(f.store.artifactStore.root, artifact.relative);
fs.appendFileSync(filename, 'tampered'); assert.equal(f.manager.summary(f.session, job).evidence_verified, false);
assert.throws(() => f.manager.originalReport(f.session, job), /校验失败/);
});
test('restart never replays an active engine check or stops a historical PID, and keeps prior task state', async t => {
const f = setup(t), job = await f.manager.start(f.session, { root: f.root }).promise;
const calls = f.audit().length; job.status = 'starting'; job.pid = process.pid; job.verification = null;
f.store.putRecord('engine_job', f.session, job, 'fixture.crash_window'); f.store.close();
const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
const saved = reopened.core.engine_job.get(job.id);
assert.equal(saved.status, 'unknown'); assert.equal(saved.requiresVerification, true); assert.equal(saved.pid, process.pid);
assert.equal(f.audit().length, calls); assert.equal(reopened.session(f.session.id).taskId, f.session.taskId);
});
test('model argument validation and runtime activity do not grant external engine prompt or project execution', t => {
const f = setup(t), runtime = new Runtime(f.store, new Provider());
assert.doesNotThrow(() => validateToolArgs('inspect_engine_checks', {}));
assert.throws(() => validateToolArgs('inspect_engine_checks', { root: f.root }), /当前任务/);
assert.throws(() => validateToolArgs('engine_prompt', {}), /没有工具/);
assert.throws(() => f.manager.start(f.session, { root: f.root, prompt: 'not accepted' }), /只接受/);
runtime.engines = { active: new Map([[f.session.id, {}]]), isActive: id => id === f.session.id, projectActive: id => id === f.project.id };
assert.equal(runtime.hasActivity(), true); assert.equal(runtime.isActive(f.session.id), true); assert.equal(runtime.projectHasActivity(f.project.id), true);
assert.throws(() => runtime.start(f.session.id, 'fixture request'), /在执行/);
});
test('a follow-up question receives the actual failed engine check from its original Task and keeps its report unchanged', async t => {
const f = setup(t, { mode: 'bad-ready' }), job = await f.manager.start(f.session, { root: f.root }).promise;
assert.equal(job.status, 'failed'); const originalReport = f.manager.originalReport(f.session, job), taskId = job.taskId;
const originalWireCount = f.audit().length, originalGoal = f.store.core.task.get(taskId).goal;
const provider = new Provider(); let requests = 0;
provider.complete = async (_config, messages, _project, _signal, _progress, _plan, tools) => {
requests++;
assert.ok(tools.some(tool => tool.function.name === 'inspect_engine_checks'));
if (requests === 1) return { message: { role: 'assistant', content: '', tool_calls: [
{ id: 'owned-engine-question', type: 'function', function: { name: 'inspect_engine_checks', arguments: '{}' } },
] }, usage: null };
const result = JSON.parse(messages.filter(message => message.role === 'tool').at(-1).content), check = result.engine_checks[0];
assert.equal(check.engine_job_id, job.id); assert.equal(check.task_id, taskId); assert.equal(check.engine_status, 'failed');
assert.match(check.message, /启动完成信号/); assert.equal(check.report_sha256, job.reportSha256);
return { message: { role: 'assistant', content: '当前任务的引擎启动完成信号与所属进程不一致,检查未通过;原进程已经关闭,原记录仍保留。' }, usage: null };
};
const runtime = new Runtime(f.store, provider); runtime.engines = f.manager;
f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, '刚才的引擎连接检查为什么报错?'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.taskId, taskId); assert.equal(f.session.status, 'paused'); assert.equal(requests, 2);
const diagnostic = f.store.core.run.get(f.session.runId);
assert.equal(diagnostic.purpose, 'diagnostic'); assert.equal(diagnostic.status, 'completed'); assert.equal(diagnostic.stopReason, 'diagnostic_answered');
assert.equal(f.store.core.task.get(taskId).status, 'paused'); assert.equal(f.store.core.task.get(taskId).goal, originalGoal);
assert.equal(f.audit().length, originalWireCount, 'Reading the failed original report must not start or prompt the external engine again.');
assert.equal(f.manager.originalReport(f.session, job), originalReport); assert.match(f.session.messages.at(-1).content, /当前任务/);
});
@@ -0,0 +1,167 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, until, launchWorkbench, compatibleFixture,completion,call,assertToolPairs } from './helpers.mjs';
import { environmentProject } from './fixtures/environment-project.mjs';
import { EnvironmentManager } from '../src/core/environment/environment-manager.mjs';
import { diagnoseEnvironment,inspectNodeEnvironment,dependencyTree,nodeEnvironmentBinding,verifyNodeBinding } from '../src/core/environment/environment-inspector.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { verifiedCheck } from '../src/core/delivery-report.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { Store } from '../src/server/store.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { assertEnvironmentsResolved } from '../src/shared/contracts/environment.mjs';
import { installUpgrade } from '../src/server/hot-upgrade.mjs';
function manager(f,options={}){const value=new EnvironmentManager(f.store,{allowHttpLoopback:true,...options});f.onCleanup(()=>value.stopAll());return value;}
const write=(project,name,value)=>fs.writeFileSync(path.join(project.root,name),value);
const read=(project,name)=>fs.readFileSync(path.join(project.root,name),'utf8');
test('unfixed dependency URLs and invalid integrity are reported without exposing URL credentials or allowing a plan',t=>{
const f=fixture(t);write(f.project,'package.json',JSON.stringify({dependencies:{a:'1.0.0'}}));
write(f.project,'package-lock.json',JSON.stringify({lockfileVersion:3,packages:{'':{dependencies:{a:'1.0.0'}},'node_modules/a':{version:'1.0.0',resolved:'https://private-token@example.invalid/a.tgz',integrity:'sha1-invalid'}}}));
const node=inspectNodeEnvironment(f.project);assert.equal(node.preparable,false);assert.match(node.issues.join(),/来源/);assert.match(node.issues.join(),/sha512/);assert.ok(!JSON.stringify(node).includes('private-token'));
});
test('hot upgrade refuses an active dependency operation or evaluation before changing runtime objects',()=>{
for(const name of ['environments','evaluations']){const runtime={[name]:{active:new Map([['owned',{}]])}},context={server:{listening:true},store:{},provider:{},runtime,token:'owned-token',clients:new Set()};
assert.throws(()=>installUpgrade(context,{appDirectory:process.cwd()}),/Wait for active/);assert.ok(runtime[name].active.has('owned'));}
});
test('read-only environment diagnosis identifies actual runtime, missing dependencies and sealed inputs without installing or running scripts',async t=>{
const f=fixture(t),p=await environmentProject(f),m=manager(f),before=read(p.project,'package-lock.json');
const diagnostic=await m.diagnose(p.session);
assert.equal(diagnostic.node.status,'needs_preparation');assert.deepEqual(diagnostic.node.missing,['awb-env-fixture']);
assert.match(diagnostic.node.runtime.node.sha256,/^[a-f0-9]{64}$/);assert.ok(diagnostic.node.runtime.npm.version);
assert.equal(diagnostic.node.lifecycle[0].name,'postinstall');assert.equal(p.requests(),0);
assert.equal(fs.existsSync(path.join(p.root,'node_modules')),false);assert.equal(read(p.project,'package-lock.json'),before);
assert.equal(f.store.artifactStore.read(f.store.owned('artifact',diagnostic.artifactId,p.session.id)),JSON.stringify((({artifactId,artifactSha256,...data})=>data)(diagnostic)));
});
test('diagnosis rejects unsupported managers, lock integrity and credential configuration without reading config content',async t=>{
const f=fixture(t),p=await environmentProject(f,{dependency:false});
write(p.project,'.npmrc','_authToken=diagnostic-must-not-read-this');
let node=inspectNodeEnvironment(p.project);assert.equal(node.preparable,false);assert.match(node.issues.join(),/npmrc/);assert.ok(!JSON.stringify(node).includes('diagnostic-must-not-read-this'));
fs.unlinkSync(path.join(p.root,'.npmrc'));
const manifest=JSON.parse(read(p.project,'package.json'));manifest.packageManager='pnpm@10.0.0';write(p.project,'package.json',JSON.stringify(manifest));
node=inspectNodeEnvironment(p.project);assert.equal(node.status,'unsupported');assert.equal(node.preparable,false);
});
test('approved real npm ci runs only in owned worktree, skips scripts, retains logs and binds later actual tests', {timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f),m=manager(f);
await assert.rejects(m.plan(f.session,{}),/独立工作区/);
const plan=await m.plan(p.session,{registry:p.registry});assert.equal(plan.scripts,'skip');
await assert.rejects(m.start(p.session,plan.id,plan.fingerprint),/批准/);
const started=await m.start(p.session,plan.id,plan.fingerprint,{approved:true}),job=await started.promise;
assert.equal(job.status,'prepared',job.summary+'\n'+job.logExcerpt);assert.equal(job.result.exitCode,0);assert.ok(job.process.pid);assert.equal(job.process.rootExited,true);
assert.equal(fs.existsSync(path.join(p.root,'node_modules/lifecycle.txt')),false);assert.equal(fs.existsSync(path.join(p.sourceRoot,'node_modules')),false);
assert.equal(f.store.core.task.get(p.session.taskId).verification.status,'not_configured');assert.ok(p.requests()>0);
assert.match(f.store.artifactStore.read(f.store.owned('artifact',job.logArtifactId,p.session.id)),/added 1 package/);
await assert.rejects(m.start(p.session,plan.id,plan.fingerprint,{approved:true}),/已经使用/);
const checks=new CheckRunner(f.store);const checkPlan=await checks.plan(p.session,['test']);const check=await(await checks.start(p.session,checkPlan.id)).promise;
assert.equal(check.status,'passed',check.summary);assert.equal(check.results[0].tests.passed,1);assert.equal(check.environment.identity,job.after.identity);
assert.ok(verifiedCheck(f.store,p.session,await inspectProject(p.project)));
fs.appendFileSync(path.join(p.root,'node_modules/awb-env-fixture/index.cjs'),'\n// external change');
assert.equal(verifiedCheck(f.store,p.session,await inspectProject(p.project)),null);
const stale=await(await checks.start(p.session,checkPlan.id)).promise;assert.equal(stale.status,'stale');assert.equal(stale.results.length,0);
});
test('explicitly allowing lifecycle scripts executes sealed root script and preserves the separate preparation result', {timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f),m=manager(f),plan=await m.plan(p.session,{registry:p.registry,scripts:'allow'});
const job=await(await m.start(p.session,plan.id,plan.fingerprint,{approved:true})).promise;
assert.equal(job.status,'prepared',job.logExcerpt);assert.equal(read(p.project,'node_modules/lifecycle.txt'),'approved');
assert.equal(f.store.core.task.get(p.session.taskId).verification.status,'not_configured');
});
test('stale lockfile, tampered plan/config, cross-session identities and mismatched download sources never dispatch npm',async t=>{
const f=fixture(t),p=await environmentProject(f),m=manager(f);
await assert.rejects(m.plan(p.session,{}),/来源/);
const plan=await m.plan(p.session,{registry:p.registry}),original=read(p.project,'package-lock.json');
await assert.rejects(m.start(f.session,plan.id,plan.fingerprint,{approved:true}),/不属于/);
write(p.project,'package-lock.json',original+'\n');await assert.rejects(m.start(p.session,plan.id,plan.fingerprint,{approved:true}),/变化/);
write(p.project,'package-lock.json',original);fs.writeFileSync(plan.configFiles[0].path,'ignore-scripts=false');await assert.rejects(m.start(p.session,plan.id,plan.fingerprint,{approved:true}),/配置/);
fs.writeFileSync(plan.configFiles[0].path,'');plan.invocation.args.push('--ignore-scripts=false');await assert.rejects(m.start(p.session,plan.id,plan.fingerprint,{approved:true}),/范围/);
assert.equal(p.requests(),0);assert.equal(f.store.core.environment_job.size,0);
});
test('registry failure keeps original npm exit/log and performs no automatic fallback or replay', {timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f,{response:'error'}),m=manager(f),plan=await m.plan(p.session,{registry:p.registry});
const job=await(await m.start(p.session,plan.id,plan.fingerprint,{approved:true})).promise;
assert.equal(job.status,'failed',job.summary);assert.notEqual(job.result.exitCode,0);assert.match(job.logExcerpt,/503|E503/);assert.equal(p.requests(),1);
assert.equal(fs.existsSync(path.join(p.sourceRoot,'node_modules')),false);
});
test('stopping a hanging dependency download records only its owned process and preserves stopped logs', {timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f,{response:'hang'}),m=manager(f),plan=await m.plan(p.session,{registry:p.registry});
const {job,promise}=await m.start(p.session,plan.id,plan.fingerprint,{approved:true});await until(()=>p.requests()>0);
assert.equal(m.projectActive(p.project.id),true);assert.throws(()=>assertEnvironmentsResolved(f.store,p.project.id),/准备/);
await m.stop(p.session.id,job.id);await promise;assert.ok(['cancelled','unknown'].includes(job.status),job.summary);
assert.ok(job.logArtifactId);assert.equal(job.result.process.treeTermination,job.status==='unknown'?'unknown':'confirmed');assert.equal(m.active.size,0);
});
test('restart retains unknown preparation, blocks writes and checks, and requires explicit read-only reconciliation',async t=>{
const f=fixture(t),p=await environmentProject(f,{dependency:false}),m=manager(f),plan=await m.plan(p.session,{offline:true});
const job={id:crypto.randomUUID(),sessionId:p.session.id,projectId:p.project.id,taskId:p.session.taskId,runId:p.session.runId,planId:plan.id,planFingerprint:plan.fingerprint,status:'queued',process:null};
f.store.putRecord('environment_job',p.session,job,'environment.queued');f.store.close();
const recovered=new Store(f.data);f.onCleanup(()=>recovered.close());const session=recovered.session(p.session.id),original=recovered.core.environment_job.get(job.id);
assert.equal(original.status,'unknown');assert.equal(original.requiresVerification,true);
const runtime=new Runtime(recovered,new Provider());assert.throws(()=>runtime.start(session.id,'new task'),/环境|依赖/);
await assert.rejects(new CheckRunner(recovered).plan(session),/依赖/);
const resumedManager=new EnvironmentManager(recovered);await resumedManager.reconcile(session,job.id,plan.fingerprint);
assert.equal(original.status,'unknown');assert.equal(original.requiresVerification,false);assert.ok(original.reconciliation.sha256);assert.doesNotThrow(()=>assertEnvironmentsResolved(recovered,p.project.id));
});
test('HTTP environment flow survives refresh, requires explicit approval and preserves task and artifact ownership', {timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f,{dependency:false}),id=p.session.id;f.store.close();const host=await launchWorkbench(t,f.data),base='/sessions/'+id;
let session=await host.api(base+'/environment-diagnostics',{});assert.equal(session.execution.environmentDiagnostics.at(-1).node.status,'ready');
session=await host.api(base+'/environment-plans',{offline:true});const plan=session.execution.environmentPlans.at(-1);
await assert.rejects(host.api(base+'/environment-jobs',{planId:plan.id,fingerprint:plan.fingerprint}),/批准/);
await host.api(base+'/environment-jobs',{planId:plan.id,fingerprint:plan.fingerprint,approved:true});
await until(async()=>{session=await host.api(base);return session.execution.environmentJobs.at(-1)?.finishedAt;},30000);
assert.equal(session.execution.environmentJobs.at(-1).status,'prepared',session.execution.environmentJobs.at(-1).logExcerpt);
assert.equal(session.execution.task.id,p.session.taskId);assert.equal(session.approvals.at(-1).environmentPlanId,plan.id);
await host.close();const next=await launchWorkbench(t,f.data),again=await next.api(base);assert.equal(again.execution.environmentJobs.at(-1).status,'prepared');
});
test('Python environment diagnosis reads declarations and runtime identity without executing project code or installing packages',async t=>{
const f=fixture(t);write(f.project,'pyproject.toml','[tool.pytest.ini_options]\ntestpaths=["tests"]\n');
write(f.project,'conftest.py',"raise RuntimeError('must never import during diagnosis')");
const diagnostic=await diagnoseEnvironment(f.project,{pythonExecutable:process.env.AGENT_WORKBENCH_TEST_PYTHON});
assert.equal(diagnostic.node,null);assert.ok(diagnostic.python);assert.equal(diagnostic.python.preparation,'manual');assert.match(diagnostic.python.runtime.sha256,/^[a-f0-9]{64}$/);
assert.equal(fs.existsSync(path.join(f.projectRoot,'__pycache__')),false);
});
test('empty build cache directories keep dependency identity while added executable files invalidate it',t=>{
const f=fixture(t);write(f.project,'package.json','{"name":"dependency-identity","private":true}');
fs.mkdirSync(path.join(f.projectRoot,'node_modules/a'),{recursive:true});write(f.project,'node_modules/a/package.json','{"name":"a","version":"1.0.0"}');
const before=nodeEnvironmentBinding(f.project);
fs.mkdirSync(path.join(f.projectRoot,'node_modules/.vite-temp'));
fs.mkdirSync(path.join(f.projectRoot,'node_modules/.cache/empty'),{recursive:true});
assert.deepEqual(nodeEnvironmentBinding(f.project),before);verifyNodeBinding(f.project,before);
write(f.project,'node_modules/.vite-temp/config.mjs','export default {plugins:[]}');
assert.throws(()=>verifyNodeBinding(f.project,before),/依赖已变化/);
fs.unlinkSync(path.join(f.projectRoot,'node_modules/.vite-temp/config.mjs'));verifyNodeBinding(f.project,before);
write(f.project,'node_modules/a/package.json','{"name":"a","version":"2.0.0"}');
assert.throws(()=>verifyNodeBinding(f.project,before),/依赖已变化/);
});
test('dependency snapshots reject external links and incomplete budgets instead of claiming environment equivalence',async t=>{
const f=fixture(t);fs.mkdirSync(path.join(f.projectRoot,'node_modules/a'),{recursive:true});write(f.project,'node_modules/a/package.json','{}');
assert.equal(dependencyTree(f.project,{maximumFiles:1}).status,'unverified');const before=nodeEnvironmentBinding(f.project);assert.equal(before,null);
const external=path.join(f.root,'external');fs.mkdirSync(external);fs.symlinkSync(external,path.join(f.projectRoot,'node_modules/external'),process.platform==='win32'?'junction':'dir');
assert.equal(dependencyTree(f.project).status,'unverified');
});
test('model-requested environment preparation uses the original approval boundary and never treats preparation as passed engineering checks',{timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f,{dependency:false}),upstream=await compatibleFixture(t,(route,input,requests)=>{
if(requests.length===1)return completion([call('env-diagnose','diagnose_environment',{})]);
if(requests.length===2)return completion([call('env-plan','plan_environment',{offline:true})]);
if(requests.length===3){const plan=JSON.parse(input.messages.findLast(value=>value.tool_call_id==='env-plan').content);return completion([call('env-run','prepare_environment',{plan_id:plan.id,fingerprint:plan.fingerprint})]);}
return completion([],'依赖准备已完成,工程检查尚未执行。');
});
f.store.config={...f.store.config,mode:'compatible',baseUrl:upstream.baseUrl,model:'owned-environment-fixture'};
const runtime=new Runtime(f.store,new Provider());f.onCleanup(()=>runtime.stopAll());runtime.start(p.session.id,'仅准备这个独立工作区的依赖,保留原记录,不需要测试');const running=runtime.active.get(p.session.id).promise;
await until(()=>p.session.approvals.some(row=>row.environmentPlanId&&row.status==='pending'),30000);
const approval=p.session.approvals.find(row=>row.environmentPlanId&&row.status==='pending');assert.equal(f.store.core.environment_job.size,0);assert.equal(approval.environmentSummary.scripts,'skip');
runtime.decide(p.session.id,approval.id,true);await running;
const snapshot=f.store.executionSnapshot(p.session),job=snapshot.environmentJobs.at(-1);assert.equal(job.status,'prepared',job.summary);assert.equal(f.store.core.task.get(p.session.taskId).verification.status,'not_configured');
assert.equal(snapshot.environmentDiagnostics.at(-1).node.status,'ready');assert.equal(snapshot.environmentDiagnostics.at(-1).runId,job.runId);
assert.equal(f.store.core.environment_job.get(job.id).approvalId,approval.id);assertToolPairs(p.session.providerMessages);
});
test('timeout and offline cache miss retain actual failures and new attempts require newly sealed plans',{timeout:60000},async t=>{
const f=fixture(t),p=await environmentProject(f,{response:'hang'}),m=manager(f,{timeoutMs:1200});
const offline=await m.plan(p.session,{registry:p.registry,offline:true}),missing=await(await m.start(p.session,offline.id,offline.fingerprint,{approved:true})).promise;
assert.equal(missing.status,'failed');assert.match(missing.logExcerpt,/ENOTCACHED|cache/i);assert.equal(p.requests(),0);
const plan=await m.plan(p.session,{registry:p.registry}),job=await(await m.start(p.session,plan.id,plan.fingerprint,{approved:true})).promise;
assert.equal(job.result.timedOut,true);assert.ok(['failed','unknown'].includes(job.status));assert.ok(job.logArtifactId);assert.notEqual(job.id,missing.id);
});
@@ -0,0 +1,157 @@
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 { createDeliveryProject, deliveryGit } from './fixtures/delivery-project.mjs';
import { ownerBytes, applyWorkerProposals, approveWorkerChecks } from './fixtures/workers-project.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Store } from '../src/server/store.mjs';
import { proposeFile } from '../src/server/files.mjs';
import { evaluationInput, evaluationSourcePath, evaluationSummary } from '../src/shared/contracts/evaluations.mjs';
import { verifyEvaluationSources } from '../src/core/evaluation/provenance.mjs';
import { workerHttp } from './fixtures/workers-http.mjs';
export const definition = (count=1) => ({title:'修复求和并核对完整原测试',evidenceMode:'local-fixture',samples:Array.from({length:count},(_,index)=>({label:'加法样本'+index,goal:'修复加法运算,保留已有功能',criteria:'原3项加法测试及构建通过;不修改原测试',paths:['src/add.mjs'],checks:['test','build']}))});
test('both evaluation worktrees can approve fixed dependency preparation and different actual dependencies reject candidate execution',{timeout:90000},async t=>{
const f=fixture(t);createDeliveryProject(f,{ownerChanges:false});
const manifest=JSON.parse(fs.readFileSync(path.join(f.projectRoot,'package.json'),'utf8'));
fs.writeFileSync(path.join(f.projectRoot,'package-lock.json'),JSON.stringify({name:manifest.name,version:manifest.version,lockfileVersion:3,requires:true,packages:{'':{name:manifest.name,version:manifest.version}}}));
deliveryGit(f,['add','--','package-lock.json']);deliveryGit(f,['-c','user.name=Workbench QA','-c','user.email=qa@workbench.local','commit','-m','Seal owned lockfile']);
const provider=new Provider();let requests=0;provider.complete=async()=>{requests++;throw new Error('Unexpected model call');};
const runtime=new Runtime(f.store,provider),manager=runtime.evaluationManager();f.onCleanup(()=>runtime.stopAll());
const batch=await manager.plan(f.session,definition()),sample=batch.samples[0];await manager.prepare(f.session,batch.id,batch.fingerprint,sample.id).promise;
assert.equal(sample.status,'ready',batch.message);assert.ok(sample.environmentInputs.dependencyInputs);
for(const child of sample.children){const owner=f.store.session(child.sessionId),environment=runtime.environmentManager(),plan=await environment.plan(owner,{offline:true});
const job=await(await environment.start(owner,plan.id,plan.fingerprint,{approved:true})).promise;assert.equal(job.status,'prepared',job.summary+'\n'+job.logExcerpt);}
await manager.baseline(f.session,batch.id,batch.fingerprint,sample.id).promise;assert.equal(sample.status,'baseline_complete',batch.message);
const candidate=sample.children.find(child=>child.role==='candidate'),root=f.store.project(candidate.projectId).root;
fs.mkdirSync(path.join(root,'node_modules','external'),{recursive:true});fs.writeFileSync(path.join(root,'node_modules','external','package.json'),'{}');
await manager.start(f.session,batch.id,batch.fingerprint,sample.id).promise;assert.equal(sample.status,'failed');assert.match(sample.error,/依赖环境不同/);assert.equal(requests,0);
await manager.seal(f.session,batch.id,batch.fingerprint,sample.id).promise;const report=manager.readReport(f.session,batch,sample);
assert.equal(report.inputVerified,false);assert.equal(report.environmentComparison.matched,false);assert.equal(report.summary.candidate,'unknown');
});
test('evaluation denominator distinguishes attempted unsealed candidates from unexecuted samples',()=>{
for(const status of ['candidate_running','candidate_complete','awaiting_review','paused','collecting','adopting','sealed']){
const summary=evaluationSummary({evidenceMode:'local-fixture',samples:[{status}]});
assert.equal(summary.samples,1,status);assert.equal(summary.candidate.unknown,1,status);assert.equal(summary.candidate.pending,0,status);assert.equal(summary.inferenceValidated,false);
}
for(const status of ['pending','ready','baseline_running','baseline_complete'])assert.equal(evaluationSummary({samples:[{status}]}).candidate.pending,1,status);
});
test('evaluation summary retains sealed outcomes and stopped or failed original attempts',()=>{
const summary=evaluationSummary({evidenceMode:'local-fixture',samples:[{status:'failed'},{status:'stopped'},{status:'unknown'},{status:'sealed',report:{summary:{original:'failed',candidate:'passed'}}}]});
assert.equal(summary.samples,4);assert.equal(summary.original.failed,1);assert.deepEqual(summary.candidate,{passed:1,failed:1,stopped:1,unknown:1,pending:0});
});
function setup(t,{respond=null,usage=true}={}) {
const f=fixture(t);createDeliveryProject(f);const provider=new Provider(),requests=[];
provider.complete=async(config,messages,project,signal)=>{
requests.push(project.id);if(respond)return respond({project,signal,f});
const owner=[...f.store.sessions.values()].find(session=>session.projectId===project.id),taskId=owner.taskId;
const plan=[...f.store.core.check_plan.values()].find(plan=>plan.taskId===taskId),job=[...f.store.core.check_job.values()].find(job=>job.taskId===taskId);
let calls=[];
if(!owner.changes.length)calls=[call(randomUUID(),'propose_file',{path:'src/add.mjs',content:'export const add = (a,b) => a + b;\n',reason:'本地固定响应中的加法修复'})];
else if(owner.changes.some(change=>change.status==='pending'))calls=[];
else if(!plan)calls=[call(randomUUID(),'plan_checks',{names:['test','build']})];
else if(!job)calls=[call(randomUUID(),'run_checks',{plan_id:plan.id})];
return {message:{role:'assistant',content:calls.length?'':'固定响应声称完成,仍需实际证据',...(calls.length?{tool_calls:calls}:{})},usage:usage?{total_tokens:168,prompt_tokens:123,completion_tokens:45}:null};
};
const runtime=new Runtime(f.store,provider),manager=runtime.evaluationManager();f.onCleanup(()=>runtime.stopAll());return {...f,runtime,manager,requests};
}
async function prepared(t,options={}) {
const f=setup(t,options),original=ownerBytes(f),batch=await f.manager.plan(f.session,definition()),sample=batch.samples[0];
await f.manager.prepare(f.session,batch.id,batch.fingerprint,sample.id).promise;
assert.equal(sample.status,'ready',batch.message);return {...f,batch,sample,original,group:f.store.core.collaboration.get(sample.groupId)};
}
async function baseline(f){await f.manager.baseline(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;assert.equal(f.sample.status,'baseline_complete',f.batch.message);}
async function complete(f){await baseline(f);await f.manager.start(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;assert.equal(f.sample.status,'awaiting_review',f.batch.message);await applyWorkerProposals(f,f.runtime,f.group);await approveWorkerChecks(f,f.runtime,f.group,f.manager.start(f.session,f.batch.id,f.batch.fingerprint,f.sample.id));assert.equal(f.sample.status,'candidate_complete',f.batch.message);}
async function sealed(f){await complete(f);await f.manager.seal(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;assert.ok(f.sample.report,f.batch.message);return f.manager.readReport(f.session,f.batch,f.sample);}
test('evaluation contract fixes bounded samples, source paths, checks and aggregate budget',t=>{
const f=fixture(t),valid=evaluationInput(definition(3),f.store.config);assert.equal(valid.samples.length,3);
for(const name of ['test/a.mjs','src/a.test.ts','src/config.ts','package.json','scripts/check.mjs','src','../evil.mjs'])assert.throws(()=>evaluationSourcePath(name));
for(const input of [{...definition(),command:'bypass'},{...definition(),samples:[]},{...definition(3),samples:[...definition(3).samples,definition().samples[0]]},{...definition(),budget:{maxSteps:1}},{...definition(),samples:[{...definition().samples[0],checks:['test','test']}]}])assert.throws(()=>evaluationInput(input,f.store.config));
});
test('same-source workspaces seal real commands before execution and preserve original staged and unstaged bytes',async t=>{
const f=await prepared(t);assert.deepEqual(ownerBytes(f),f.original);assert.equal(f.requests.length,0);
assert.equal(f.sample.commands[0].script,'node --test test/*.test.mjs');assert.ok(f.sample.contractSha256);
for(const child of f.group.children){const owner=f.store.session(child.sessionId);assert.throws(()=>proposeFile(f.store,owner,{path:'test/add.test.mjs',content:'pass',reason:'weaken test'}),/原版|范围/);assert.throws(()=>f.runtime.start(owner.id,'replace'),/受限|父协作/);}
const original=f.group.children[0],owner=f.store.session(original.sessionId);
assert.throws(()=>proposeFile(f.store,owner,{path:'src/add.mjs',content:'change',reason:'original'}),/原版/);
assert.throws(()=>f.runtime.workerManager().start(f.session,f.group.id,f.group.fingerprint,original.id),/原版/);
await assert.rejects(f.runtime.checkRunner().plan(f.store.session(f.group.children[1].sessionId),['test']),/完整检查/);
});
test('actual original failures and candidate success are both preserved, then adoption requires parent review, checks and Git',async t=>{
const f=await prepared(t),report=await sealed(f);assert.deepEqual(report.summary,{original:'failed',candidate:'passed'});
assert.equal(report.original.attempts[0].results[0].tests.failed,3);assert.equal(report.candidate.attempts[0].results[0].tests.passed,3);
assert.equal(report.original.usage.requests,0);assert.ok(report.candidate.usage.requests>0);assert.equal(report.inferenceValidated,false);assert.ok(report.candidate.approvals.some(approval=>approval.status==='approved'));
assert.deepEqual(ownerBytes(f),f.original);const source=fs.readFileSync(path.join(f.projectRoot,'src/add.mjs'));
await f.manager.adopt(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;assert.equal(f.batch.status,'awaiting_delivery',f.batch.message);assert.deepEqual(fs.readFileSync(path.join(f.projectRoot,'src/add.mjs')),source);
await assert.rejects(f.manager.finalize(f.session,f.batch.id,f.batch.fingerprint),/应用|变更集/);
const set=f.store.core.change_set.get(f.group.merge.changeSetId);await (await f.runtime.changeSetManager().start(f.session,set.id,set.fingerprint)).promise;assert.equal(set.status,'completed',set.error?.message);
const plan=await f.runtime.checkRunner().plan(f.session,['test','build']);assert.equal((await (await f.runtime.checkRunner().start(f.session,plan.id)).promise).status,'passed');
const delivery=await f.runtime.gitDelivery().plan(f.session,{message:'Apply evaluated addition candidate'});const result=await (await f.runtime.gitDelivery().start(f.session,delivery.id)).promise;assert.equal(result.status,'completed',result.error?.message);
await f.manager.finalize(f.session,f.batch.id,f.batch.fingerprint);assert.equal(f.batch.adoption.status,'completed');
const after=ownerBytes(f);assert.deepEqual(after.files,f.original.files);assert.notEqual(after.head,f.original.head);assert.match(deliveryGit(f,['diff','--cached','--name-only']),/owner-staged/);
});
test('candidate cannot shrink tests and changing original test bytes rejects execution before any model request',async t=>{
const f=await prepared(t);await baseline(f);const child=f.group.children[1],owner=f.store.session(child.sessionId);
fs.appendFileSync(path.join(f.store.project(child.projectId).root,'test/add.test.mjs'),'\n// changed\n');
await assert.rejects(verifyEvaluationSources(f.store,owner),/原测试/);
await f.manager.start(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;assert.equal(f.requests.length,0);assert.equal(f.sample.status,'failed');
await f.manager.seal(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;const report=f.manager.readReport(f.session,f.batch,f.sample);assert.equal(report.inputVerified,false);assert.equal(report.summary.candidate,'unknown');
assert.throws(()=>f.manager.adopt(f.session,f.batch.id,f.batch.fingerprint,f.sample.id),/通过/);
});
test('missing upstream usage remains unknown and does not become a free run or invented cost',async t=>{
const f=await prepared(t,{usage:false}),report=await sealed(f);assert.ok(report.candidate.usage.unknownRequests>0);assert.equal(report.candidate.usage.reported,false);assert.equal(report.candidate.usage.cost,null);assert.ok(report.candidate.usage.estimatedUnknownTokens>0);
});
test('missing raw check logs preserve unknown result and original sample denominator',async t=>{
const f=await prepared(t);await complete(f);const job=f.store.core.check_job.get(f.sample.baselineJobId);f.store.core.artifact.delete(job.results[0].logArtifactId);
await f.manager.seal(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;const report=f.manager.readReport(f.session,f.batch,f.sample);assert.equal(report.summary.original,'unknown');assert.ok(report.original.evidenceError);assert.equal(evaluationSummary(f.batch).samples,1);
assert.throws(()=>f.manager.adopt(f.session,f.batch.id,f.batch.fingerprint,f.sample.id),/原版结果完整/);
});
test('source and model changes never silently replace an existing batch input',async t=>{
const f=setup(t),batch=await f.manager.plan(f.session,definition());fs.appendFileSync(path.join(f.projectRoot,'README.md'),'owner later');
await f.manager.prepare(f.session,batch.id,batch.fingerprint,batch.samples[0].id).promise;assert.equal(batch.samples[0].status,'failed');assert.equal(f.store.core.worker.size,0);assert.equal(f.requests.length,0);
});
test('model revision change before preparing a fixed sample cannot silently use the replacement connection',async t=>{
const f=setup(t),batch=await f.manager.plan(f.session,definition());f.store.saveConfig({model:'replacement-connection'});
await f.manager.prepare(f.session,batch.id,batch.fingerprint,batch.samples[0].id).promise;assert.equal(batch.samples[0].status,'failed');assert.equal(f.requests.length,0);assert.equal(f.store.core.worker.size,0);
});
test('HTTP evaluations require host token, original fingerprint and session ownership; worker API cannot bypass evaluation flow',async t=>{
const f=fixture(t),host=await workerHttp(f);assert.equal((await host.raw('/sessions/'+f.session.id+'/evaluations',definition(),'wrong-token')).status,403);
const {batch}=await host.api('/sessions/'+f.session.id+'/evaluations',definition()),sample=batch.samples[0];
assert.equal((await host.raw('/sessions/'+f.session.id+'/evaluations/'+batch.id+'/prepare',{sampleId:sample.id})).status,409);
assert.equal((await host.raw('/sessions/'+f.session.id+'/evaluations/'+batch.id+'/prepare',{fingerprint:batch.fingerprint,sampleId:sample.id,command:'extra'})).status,409);
const other=f.store.createSession(f.project.id);assert.equal((await host.raw('/sessions/'+other.id+'/evaluations/'+batch.id+'/prepare',{fingerprint:batch.fingerprint,sampleId:sample.id})).status,404);
await host.api('/sessions/'+f.session.id+'/evaluations/'+batch.id+'/prepare',{fingerprint:batch.fingerprint,sampleId:sample.id});await until(()=>!host.runtime.evaluationManager().isActive(f.session.id),20000);
const saved=f.store.core.evaluation.get(batch.id);assert.equal(saved.samples[0].status,'ready',saved.message);const group=f.store.core.collaboration.get(saved.samples[0].groupId);
assert.equal((await host.raw('/sessions/'+group.children[1].sessionId+'/evaluations',definition())).status,409);
assert.equal((await host.raw('/sessions/'+f.session.id+'/collaborations/'+group.id+'/start',{fingerprint:group.fingerprint,workerId:group.children[1].id})).status,409);
assert.equal(host.requests.length,0);
});
test('sample queue cannot skip or reset attempted samples, and reports freeze further writes',async t=>{
const f=setup(t),batch=await f.manager.plan(f.session,definition(2));assert.throws(()=>f.manager.prepare(f.session,batch.id,batch.fingerprint,batch.samples[1].id),/顺序/);
const sample=batch.samples[0];await f.manager.prepare(f.session,batch.id,batch.fingerprint,sample.id).promise;assert.equal(sample.status,'ready',batch.message);
assert.throws(()=>f.manager.prepare(f.session,batch.id,batch.fingerprint,sample.id),/尝试/);
await f.manager.seal(f.session,batch.id,batch.fingerprint,sample.id).promise;const child=sample.children[1],owner=f.store.session(child.sessionId);
assert.throws(()=>proposeFile(f.store,owner,{path:'src/add.mjs',content:'change',reason:'sealed'}),/封存/);
await f.manager.prepare(f.session,batch.id,batch.fingerprint,batch.samples[1].id).promise;assert.equal(batch.samples[1].status,'ready',batch.message);
f.manager.retain(f.session,batch.id,batch.fingerprint);assert.equal(evaluationSummary(batch).samples,2);assert.equal(evaluationSummary(batch).finished,1);
});
test('stop cancels only current candidate and preserves partial results instead of replaying a request',async t=>{
const f=await prepared(t,{respond:({signal})=>new Promise((resolve,reject)=>signal.addEventListener('abort',()=>reject(signal.reason),{once:true}))});await baseline(f);
const started=f.manager.start(f.session,f.batch.id,f.batch.fingerprint,f.sample.id);await until(()=>f.requests.length===1);await f.runtime.stop(f.session.id);await started.promise;
assert.equal(f.requests.length,1);assert.equal(f.runtime.hasActivity(),false);assert.notEqual(f.sample.status,'candidate_complete');assert.deepEqual(ownerBytes(f),f.original);
await f.manager.seal(f.session,f.batch.id,f.batch.fingerprint,f.sample.id).promise;
const report=f.manager.readReport(f.session,f.batch,f.sample);
assert.equal(report.summary.candidate,'stopped');assert.equal(report.inputVerified,false);
assert.equal(report.environmentComparison.matched,false);assert.equal(report.original.attempts[0].status,'failed');assert.equal(f.requests.length,1);
});
test('restart recovers interrupted evaluations as unknown and keeps completed checks and plan identity',async t=>{
const f=await prepared(t);await baseline(f);const fingerprint=f.batch.fingerprint,jobId=f.sample.baselineJobId;
f.batch.status='candidate_running';f.sample.status='candidate_running';f.store.save(f.session);f.store.close();
const reopened=new Store(f.data);f.onCleanup(()=>reopened.close());const saved=reopened.core.evaluation.get(f.batch.id);assert.equal(saved.status,'unknown');assert.equal(saved.samples[0].status,'unknown');assert.equal(saved.fingerprint,fingerprint);assert.equal(saved.samples[0].baselineJobId,jobId);assert.equal(reopened.core.check_job.get(jobId).status,'failed');
});
@@ -0,0 +1,183 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { Store, repairToolPairs } from '../src/server/store.mjs';
import { StateDatabase } from '../src/core/storage/state-database.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { fixture, workspace, call, until, assertToolPairs } from './helpers.mjs';
function legacyCase(f, name) {
f.store.message(f.session, 'user', 'Original fixture requirement');
fs.writeFileSync(path.join(f.projectRoot, 'worker-effect.mjs'), "import fs from 'node:fs'; const file='executions.txt'; const count=fs.existsSync(file)?Number(fs.readFileSync(file,'utf8')):0; fs.writeFileSync(file,String(count+1)); console.log('fixture-result');\n");
const directory = path.join(f.root, name); f.store.exportLegacyData(directory);
return directory;
}
async function crash(t, directory, phase) {
const env = { ...process.env }; delete env.AGENT_WORKBENCH_API_KEY;
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/core-crash-worker.mjs'), directory, phase], { env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = ''; child.stdout.on('data', bytes => output += bytes.toString('utf8')); child.stderr.on('data', bytes => output += bytes.toString('utf8'));
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null || child.signalCode !== null, 15000);
assert.equal(child.exitCode, 66, output);
}
function reopen(f, directory) { const store = new Store(directory); f.onCleanup(() => store.close()); return store; }
test('JSON migration survives process exit before commit and retains exact legacy backups', { timeout: 30000 }, async t => {
const f = fixture(t), directory = legacyCase(f, 'migration');
const sessionFile = path.join(directory, 'sessions', f.session.id + '.json');
const before = fs.readFileSync(sessionFile);
await crash(t, directory, 'migration');
const raw = new StateDatabase(directory);
assert.equal(raw.initialized, false); assert.equal(raw.cursor, 0); assert.equal(raw.load().sessions.length, 0); raw.close();
assert.deepEqual(fs.readFileSync(sessionFile), before);
const recovered = reopen(f, directory), session = recovered.session(f.session.id);
assert.equal(session.messages[0].content, 'Original fixture requirement');
assert.equal(recovered.core.task.size, 1);
const migration = recovered.storage.meta('migration');
assert.deepEqual(fs.readFileSync(path.join(directory, migration.backupRelative, 'sessions', f.session.id + '.json')), before);
const expected = structuredClone(session); recovered.close();
fs.writeFileSync(sessionFile, '{}');
const again = reopen(f, directory);
assert.deepEqual(again.session(f.session.id), expected, 'A stale JSON mirror cannot overwrite migrated SQLite state');
const exported = path.join(f.root, 'rollback-export'); again.exportLegacyData(exported);
assert.deepEqual(JSON.parse(fs.readFileSync(path.join(exported, 'sessions', session.id + '.json'), 'utf8')), expected);
});
for (const point of ['after_state', 'after_event']) test('process exit ' + point + ' rolls back state, event and outbox together', { timeout: 30000 }, async t => {
const f = fixture(t), directory = legacyCase(f, point), initial = reopen(f, directory);
const before = structuredClone(initial.session(f.session.id)), cursor = initial.eventCursor;
initial.close(); await crash(t, directory, point);
const recovered = reopen(f, directory);
assert.deepEqual(recovered.session(f.session.id), before); assert.equal(recovered.eventCursor, cursor);
assert.equal(recovered.eventsAfter(cursor).events.length, 0);
});
for (const phase of ['accepted', 'queued', 'approval_wait', 'approved', 'effect_unrecorded', 'result_committed', 'before_next_model']) {
test('crash recovery at ' + phase + ' retains execution identity and never replays side effects', { timeout: 30000 }, async t => {
const f = fixture(t), directory = legacyCase(f, phase);
await crash(t, directory, phase);
const recovered = reopen(f, directory), session = recovered.session(f.session.id), execution = recovered.executionSnapshot(session);
assert.equal(session.status, 'paused'); assert.equal(execution.task.goal, 'Crash fixture task');
assert.equal(execution.run.status, 'paused'); assert.equal(execution.run.stopReason, 'restart');
const marker = path.join(f.projectRoot, 'executions.txt');
const effectOccurred = ['effect_unrecorded', 'result_committed', 'before_next_model'].includes(phase);
assert.equal(fs.existsSync(marker), effectOccurred);
if (effectOccurred) assert.equal(fs.readFileSync(marker, 'utf8'), '1');
assertToolPairs(session.providerMessages);
const invocation = [...recovered.core.tool.values()].find(value => value.callId === 'crash-' + phase);
if (phase !== 'accepted') {
assert.ok(invocation); assert.equal(invocation.runId, execution.run.id); assert.equal(invocation.taskId, execution.task.id);
if (['result_committed', 'before_next_model'].includes(phase)) {
assert.equal(invocation.state, 'completed'); assert.equal(invocation.requiresVerification, false);
const result = JSON.parse(session.providerMessages.find(value => value.tool_call_id === invocation.callId).content);
assert.equal(result.status, 'completed'); assert.match(result.stdout, /fixture-result/);
} else if (['approved', 'effect_unrecorded'].includes(phase)) {
assert.equal(invocation.state, 'unknown'); assert.equal(invocation.requiresVerification, true);
assert.ok(execution.run.checkpoint.requiresVerification.includes(invocation.id));
} else { assert.equal(invocation.state, 'interrupted'); assert.equal(invocation.requiresVerification, false); }
}
const oldApproval = session.approvals[0]?.id;
const provider = new Provider(); let requests = 0;
provider.complete = async () => { requests++; return { message: { role: 'assistant', content: 'Recovered records inspected.' }, usage: null }; };
const runtime = new Runtime(recovered, provider); f.onCleanup(() => runtime.stopAll());
if (oldApproval) assert.throws(() => runtime.decide(session.id, oldApproval, true), { status: 409 });
if (phase !== 'accepted') {
const previousRunId = session.runId, taskId = session.taskId;
runtime.resume(session.id); await runtime.active.get(session.id).promise;
assert.equal(requests, 1); assert.equal(session.taskId, taskId); assert.notEqual(session.runId, previousRunId);
assert.equal(recovered.core.run.get(session.runId).resumeOf, previousRunId);
if (oldApproval) assert.throws(() => runtime.decide(session.id, oldApproval, true), { status: 409 });
assert.equal(fs.existsSync(marker), effectOccurred);
if (effectOccurred) assert.equal(fs.readFileSync(marker, 'utf8'), '1');
}
});
}
test('state and event failures publish no success and preserve the last committed memory snapshot', t => {
const f = fixture(t), before = structuredClone(f.session), cursor = f.store.eventCursor;
let notifications = 0; f.store.on('update', () => notifications++);
f.store.storage.faultInjector = point => { if (point === 'after_event') throw new Error('Controlled transaction failure'); };
assert.throws(() => f.store.message(f.session, 'user', 'must not publish'), /Controlled transaction failure/);
assert.deepEqual(f.session, before); assert.equal(f.store.eventCursor, cursor); assert.equal(notifications, 0);
f.store.storage.faultInjector = null;
f.store.message(f.session, 'user', 'committed'); assert.equal(notifications, 1);
});
test('tool result artifacts and event cursors retain project ownership across restart', async t => {
const f = fixture(t), provider = new Provider(); let round = 0;
provider.complete = async () => ({ message: round++ === 0 ? { role: 'assistant', content: '', tool_calls: [call('owned-read', 'read_file', { path: 'README.md' })] }
: { role: 'assistant', content: 'Read complete.' }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
const initial = f.store.eventCursor;
runtime.start(f.session.id, 'Read the fixture'); await runtime.active.get(f.session.id).promise;
const snapshot = f.store.executionSnapshot(f.session);
assert.equal(snapshot.run.usage.requests, 2); assert.equal(snapshot.run.usage.totalTokens, 28);
const artifact = snapshot.artifacts.find(value => value.kind === 'tool-result'); assert.equal(artifact.projectId, f.project.id);
assert.match(JSON.parse(f.store.artifactStore.read(artifact)).content, /Fixture project/);
const other = f.store.createSession(f.project.id);
assert.throws(() => f.store.owned('artifact', artifact.id, other.id), { status: 404 });
const page = f.store.eventsAfter(initial, { sessionId: f.session.id, limit: 3 });
assert.equal(page.events.length, 3); assert.equal(page.hasMore, true);
assert.ok(page.events.every(event => event.projectId === f.project.id && event.sessionId === f.session.id));
const next = f.store.eventsAfter(page.nextCursor, { sessionId: f.session.id });
assert.ok(next.events.every(event => event.seq > page.nextCursor));
const all = [...page.events, ...next.events]; assert.equal(new Set(all.map(event => event.seq)).size, all.length);
const expectedCursor = f.store.eventCursor;
f.store.close(); const recovered = reopen(f, f.data);
assert.equal(recovered.eventCursor, expectedCursor);
assert.deepEqual(recovered.owned('artifact', artifact.id, f.session.id), artifact);
});
test('token budgets pause before tools execute and continuation keeps original task and accumulated usage', async t => {
const f = fixture(t), provider = new Provider(); let round = 0;
f.store.saveConfig({ maxRunTokens: 12000 });
provider.complete = async () => ({ message: { role: 'assistant', content: '', tool_calls: [call('budget-' + round++, 'run_command', { command: 'node --version', reason: 'must not execute over budget' })] },
usage: { prompt_tokens: 12020, completion_tokens: 10 } });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Original goal'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'paused'); assert.equal(f.session.approvals.length, 0);
const originalTaskId = f.session.taskId;
assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'token_limit');
assert.equal(f.store.core.task.get(originalTaskId).usage.totalTokens, 12030); assertToolPairs(f.session.providerMessages);
f.store.saveConfig({ maxRunTokens: 16000 });
provider.complete = async () => ({ message: { role: 'assistant', content: 'Finished.' }, usage: { prompt_tokens: 5, completion_tokens: 5 } });
runtime.start(f.session.id, 'Also preserve the public API.'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.taskId, originalTaskId); assert.equal(f.store.core.task.get(originalTaskId).goal, 'Original goal');
assert.equal(f.store.core.task.get(originalTaskId).instructions[0].content, 'Also preserve the public API.');
assert.equal(f.store.core.task.get(originalTaskId).usage.totalTokens, 12040);
assert.equal(f.store.core.run.get(f.session.runId).budget.maxTokens, 16000);
});
test('a time budget interrupts an unresponsive model and persists a resumable run', async t => {
const f = fixture(t), provider = new Provider(); f.store.saveConfig({ maxRunDurationMs: 80 });
provider.complete = async (_config, _messages, _project, signal) => new Promise((resolve, reject) => {
signal.addEventListener('abort', () => reject(signal.reason), { once: true });
if (signal.aborted) reject(signal.reason);
});
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Keep this goal after timeout'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'time_limit');
assert.match(f.session.messages.at(-1).content, /时间预算/); assert.equal(runtime.isActive(f.session.id), false);
});
test('a corrupted large result artifact cannot become a recovered success', t => {
const f = fixture(t), run = f.store.beginRun(f.session, 'Read a large fixture', false, f.store.config);
const tool = call('large-result', 'read_file', { path: 'README.md' });
f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [tool] });
const event = { id: crypto.randomUUID(), kind: 'tool', tool: 'read_file', phase: 'queued', toolCallId: tool.id, runId: run.id, createdAt: new Date().toISOString() };
f.session.events.push(event); f.store.registerInvocation(f.session, tool, run, event);
f.store.beginInvocation(f.session, tool, run, event); f.store.invocationState(f.session, event, 'running');
event.phase = 'completed';
const artifact = f.store.finishInvocation(f.session, event, { status: 'completed', content: 'x'.repeat(20000) });
fs.writeFileSync(path.join(f.data, 'artifacts', artifact.relative), 'corrupted fixture result');
f.store.close(); const recovered = reopen(f, f.data), session = recovered.session(f.session.id);
const result = JSON.parse(session.providerMessages.find(value => value.tool_call_id === tool.id).content);
assert.equal(result.status, 'unknown'); assert.equal(result.requires_verification, true);
assert.equal(recovered.executionSnapshot(session).progress.requiresVerification.length, 1);
assert.equal(session.events.find(value => value.id === event.id).phase, 'unknown');
assert.equal(recovered.core.run.get(run.id).checkpoint.completedToolIds.includes(event.invocationId), false);
assert.throws(() => recovered.artifactStore.write(session, recovered.core.run.get(run.id), { status: 'completed', content: 'x'.repeat(20000) }), /已有执行工件校验失败/);
});
@@ -0,0 +1,34 @@
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 { readText, listFiles, proposeFile } from '../src/server/files.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
test('hard-link aliases cannot expose protected bytes through reads, proposals, inventory or automatic project rules', async t => {
const f = fixture(t);
fs.mkdirSync(path.join(f.projectRoot, '.git')); fs.mkdirSync(path.join(f.projectRoot, '.secrets'));
fs.writeFileSync(path.join(f.projectRoot, '.git/config'), '[core]\nrepositoryformatversion = 0\n');
fs.writeFileSync(path.join(f.projectRoot, '.secrets/owned.txt'), 'owned-protected-rule-marker');
fs.linkSync(path.join(f.projectRoot, '.git/config'), path.join(f.projectRoot, 'public-config.txt'));
fs.linkSync(path.join(f.projectRoot, '.secrets/owned.txt'), path.join(f.projectRoot, 'AGENTS.md'));
for (const filename of ['public-config.txt', 'AGENTS.md']) {
assert.throws(() => readText(f.project, filename), /硬链接/);
assert.throws(() => proposeFile(f.store, f.session, { path: filename, content: 'new bytes' }), /硬链接/);
}
const inventory = listFiles(f.project); assert.ok(!inventory.files.some(value => ['public-config.txt', 'AGENTS.md'].includes(value.path)));
const inspection = await inspectProject(f.project); assert.equal(inspection.rules.length, 0);
assert.equal(JSON.stringify(inspection).includes('owned-protected-rule-marker'), false);
assert.equal(fs.readFileSync(path.join(f.projectRoot, '.secrets/owned.txt'), 'utf8'), 'owned-protected-rule-marker');
});
test('ambiguous project hard links are explicit and ordinary independent file copies remain readable', t => {
const f = fixture(t), original = path.join(f.projectRoot, 'README.md'), alias = path.join(f.projectRoot, 'alias.md');
fs.linkSync(original, alias);
assert.throws(() => readText(f.project, 'README.md'), /独立文件副本/); assert.throws(() => readText(f.project, 'alias.md'), /硬链接/);
fs.unlinkSync(alias); fs.copyFileSync(original, alias);
assert.match(readText(f.project, 'README.md').content, /Fixture project/);
assert.match(readText(f.project, 'alias.md').content, /Fixture project/);
assert.ok(listFiles(f.project).files.some(value => value.path === 'alias.md'));
});
@@ -0,0 +1,35 @@
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 { beginPlanTask } from './fixtures/task-plan-fixture.mjs';
import { readText } from '../src/server/files.mjs';
test('current-file HTTP receipts bind Task/Run and bytes, persist on restart and end without altering original runs or user Git data', async t => {
const f = fixture(t); beginPlanTask(f, '创建 README.md 文件,沿用已有内容,无需测试');
const taskId = f.session.taskId, runId = f.session.runId, sha256 = readText(f.project, 'README.md').sha256;
const originalRun = structuredClone(f.store.core.run.get(runId)); f.store.close();
let host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
const input = { taskId, runId, files: [{ path: 'README.md', sha256 }], reason: '实际读取现成说明,符合当前文件交付范围。' };
await assert.rejects(host.api(route + '/file-results', { ...input, taskId: crypto.randomUUID() }), /切换/);
await assert.rejects(host.api(route + '/file-results', { ...input, runId: crypto.randomUUID() }), /切换/);
await assert.rejects(host.api(route + '/file-results', { ...input, status: 'completed' }), /核对需要/);
await assert.rejects(host.api(route + '/file-results', { ...input, files: [{ path: '../outside.md', sha256 }] }), /项目内文件/);
const saved = await host.api(route + '/file-results', input);
assert.equal(saved.results.length, 1); assert.equal(saved.results[0].verification, 'current');
const again = await host.api(route + '/file-results', input); assert.equal(again.results.length, 1);
await host.close(); host = await launchWorkbench(t, f.data);
const restored = await host.api(route + '/file-results'); assert.deepEqual(restored.results, saved.results);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'User edit\n');
assert.equal((await host.api(route + '/file-results')).results[0].verification, 'stale');
await assert.rejects(host.api(route + '/file-results/complete', { taskId, runId }), /当前有效成果/);
await assert.rejects(host.api(route + '/file-results', input), /变化/);
const file = await host.api('/projects/' + f.project.id + '/file?path=README.md');
await host.api(route + '/file-results', { ...input, files: [{ path: file.path, sha256: file.sha256 }], reason: '重新核对用户编辑后的完整说明。' });
const completed = await host.api(route + '/file-results/complete', { taskId, runId });
assert.equal(completed.session.status, 'completed'); assert.equal(completed.session.execution.task.delivery.status, 'existing_verified');
assert.deepEqual(completed.session.execution.run, originalRun); assert.equal(completed.session.changes.length, 0);
assert.equal(completed.session.execution.checks.length, 0); assert.equal(completed.session.execution.deliveries.length, 0);
assert.match(fs.readFileSync(path.join(f.projectRoot, 'README.md'), 'utf8'), /User edit/);
});
@@ -0,0 +1,199 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { FileResults } from '../src/core/tasks/file-results.mjs';
import { TaskPlanManager } from '../src/core/tasks/task-plan-manager.mjs';
import { DeliveryReporter, renderDeliveryMarkdown } from '../src/core/delivery-report.mjs';
import { mobileSession } from '../src/core/companion/projection.mjs';
import { readText, readLines, proposeFile, applyChange } from '../src/server/files.mjs';
import { filePage } from '../src/core/context/read-budget.mjs';
import { requestedFilePaths } from '../src/shared/contracts/file-intent.mjs';
import { fixture, call, assertToolPairs } from './helpers.mjs';
const answer = content => ({ message: { role: 'assistant', content }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
const calls = (...tools) => ({ ...answer(''), message: { role: 'assistant', content: '', tool_calls: tools } });
function harness(t, respond) {
const f = fixture(t), provider = new Provider(); let round = 0;
provider.complete = async (...args) => respond(round++, f, args);
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
f.runtime = runtime; f.manager = runtime.fileResults(); f.wait = () => runtime.active.get(f.session.id).promise; return f;
}
function begin(f, goal = '创建 README.md 文件,已有成果满足时采用,无需测试。', forceNewTask = false) {
const run = f.store.beginRun(f.session, goal, false, f.store.config, { forceNewTask });
f.session.status = 'paused'; f.store.finishRun(f.session, run, 'fixture_ready'); return run;
}
function read(f, args) {
const run = f.store.core.run.get(f.session.runId), invocation = call('read-' + crypto.randomUUID(), 'read_file', args);
f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [invocation] });
const event = f.store.event(f.session, { kind: 'tool', phase: 'queued', tool: 'read_file', toolCallId: invocation.id, label: 'Fixture actual read' });
const record = f.store.beginInvocation(f.session, invocation, run, event); f.store.invocationState(f.session, event, 'running');
const result = filePage(readLines(f.project, args), args, { callId: invocation.id });
event.phase = 'completed'; f.store.finishInvocation(f.session, event, result);
f.session.providerMessages.push({ role: 'tool', tool_call_id: invocation.id, content: JSON.stringify(result) });
return invocation.id;
}
function input(f, extra = {}) { return { files: [{ path: 'README.md', sha256: readText(f.project, 'README.md').sha256, ...extra }], reason: '已按当前目标核对现有文件内容。' }; }
test('file targets exclude referenced inputs and prohibited subsequent modifications', () => {
for (const goal of ['create result.html using template.html', '创建 result.html 参考 README.md', '创建 result.html 不要修改 README.md']) {
assert.deepEqual(requestedFilePaths(goal), ['result.html']);
}
assert.deepEqual(requestedFilePaths('创建 result.html 和 style.css,参考 README.md'), ['result.html', 'style.css']);
assert.deepEqual(requestedFilePaths('创建基于参考 README.md 的说明'), []);
});
test('full actual read and existing receipt complete a no-test file task without fabricating changes, checks or commits', async t => {
const f = harness(t, (round, f) => round === 0 ? calls(call('existing-read', 'read_file', { path: 'README.md' }))
: round === 1 ? calls(call('existing-result', 'verify_existing_files', input(f, { read_call_ids: ['existing-read'] }))) : answer('已核对已有成果,无新增修改。'));
const bytes = fs.readFileSync(path.join(f.projectRoot, 'README.md'));
f.runtime.start(f.session.id, '创建 README.md 文件,已有成果满足时采用,无需测试。'); await f.wait();
assert.equal(f.session.status, 'completed');
const task = f.store.core.task.get(f.session.taskId);
assert.equal(task.delivery.status, 'existing_verified'); assert.equal(task.delivery.newChanges, 0); assert.equal(task.delivery.checks, 'not_requested');
assert.equal(f.session.changes.length, 0); assert.equal(f.store.core.check_job.size, 0); assert.equal(f.store.core.delivery_job.size, 0);
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'README.md')), bytes);
const row = f.manager.records(f.session)[0]; assert.equal(f.manager.verify(f.session, row.id).id, row.id);
assert.equal(row.files[0].reads[0].callId, 'existing-read'); assertToolPairs(f.session.providerMessages);
f.runtime.start(f.session.id, '继续完成'); await f.wait();
assert.equal(f.session.taskId, task.id); assert.equal(f.manager.inspect(f.session).results[0].verification, 'current'); assert.equal(f.session.status, 'completed');
});
test('partial pages, partial lines, wrong hashes and failed reads cannot be adopted; contiguous full paging can', t => {
const f = fixture(t); begin(f); const manager = new FileResults(f.store);
const a = read(f, { path: 'README.md', start_line: 1, end_line: 1 });
assert.throws(() => manager.record(f.session, input(f, { read_call_ids: [a] })), /完整读取/);
const b = read(f, { path: 'README.md', start_line: 2, end_line: 4, limit: 8 });
assert.throws(() => manager.record(f.session, input(f, { read_call_ids: [a, b] })), /完整读取/);
const c = read(f, { path: 'README.md', start_line: 2, end_line: 4, offset: 8 });
const result = manager.record(f.session, input(f, { read_call_ids: [a, b, c] }));
assert.equal(manager.verify(f.session, result.result_id).files[0].reads.length, 3);
const invocation = [...f.store.core.tool.values()].find(row => row.callId === c); invocation.state = 'unknown'; invocation.requiresVerification = true;
assert.throws(() => manager.verify(f.session, result.result_id), /读取调用/);
});
test('user edits and new requirements invalidate receipts, and a second task must explicitly adopt a source in its own project/session', t => {
const f = fixture(t); begin(f); const manager = new FileResults(f.store);
const old = manager.record(f.session, input(f), { source: 'user' }), oldTask = f.session.taskId;
assert.throws(() => manager.record(f.session, input(f, { source_result_id: old.result_id }), { source: 'user' }), /不重复采用自身成果/);
assert.equal(manager.records(f.session).length, 1);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nUser contribution\n');
assert.equal(manager.inspect(f.session).results[0].verification, 'stale');
assert.throws(() => manager.record(f.session, { ...input(f), files: [{ path: 'README.md', sha256: manager.records(f.session)[0].files[0].sha256 }] }, { source: 'user' }), /变化/);
begin(f, '创建 README.md 文件,沿用已有内容,无需测试。', true);
assert.notEqual(f.session.taskId, oldTask);
assert.throws(() => manager.record(f.session, input(f), { source: 'user' }), /显式选择/);
const adopted = manager.record(f.session, input(f, { source_result_id: old.result_id }), { source: 'user' });
const origin = manager.verify(f.session, adopted.result_id).files[0].origin;
assert.equal(origin.taskId, oldTask); assert.notEqual(origin.sha256, manager.records(f.session)[0].files[0].sha256);
const otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); fs.writeFileSync(path.join(otherRoot, 'README.md'), 'other');
const otherProject = f.store.addProject(otherRoot), other = f.store.createSession(otherProject.id);
f.store.beginRun(other, '创建 README.md 文件', false, f.store.config);
assert.throws(() => manager.verify(other, adopted.result_id, { current: false }), /不属于/);
begin(f, '补充需求:标题必须更清晰');
assert.equal(manager.inspect(f.session).results[0].verification, 'stale');
});
test('adopting an unchanged file never satisfies an explicit modification or a new modification instruction', async t => {
const f = fixture(t); const runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f, '修改 README.md 文件标题,无需测试'); const manager = runtime.fileResults();
manager.record(f.session, input(f), { source: 'user' });
assert.equal(manager.completion(f.session).requiresChange, true);
await assert.rejects(runtime.completeFileResults(f.session), /修改/);
const proposed = proposeFile(f.store, f.session, { path: 'README.md', content: '# Correct title\n', reason: '修改标题' });
applyChange(f.store, f.session, f.session.changes.at(-1).id);
assert.equal(manager.completion(f.session).satisfied, true);
await new Promise(resolve => setTimeout(resolve, 10));
begin(f, '再修改 README.md 文件,增加结果说明', true);
manager.record(f.session, input(f, { source_result_id: manager.records({ ...f.session, taskId: f.store.core.task.get(f.session.taskId).previousTaskId })[0].id }), { source: 'user' });
assert.equal(manager.completion(f.session).requiresChange, true);
});
test('required checks and stale passing flags cannot be bypassed by current file receipts', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f, '创建 README.md 文件并运行测试');
runtime.fileResults().record(f.session, input(f), { source: 'user' });
await assert.rejects(runtime.completeFileResults(f.session), /工程检查/);
f.store.core.task.get(f.session.taskId).verification = { status: 'passed', checkJobId: crypto.randomUUID() };
await assert.rejects(runtime.completeFileResults(f.session), /工程检查/);
assert.equal(f.session.status, 'paused');
});
test('results plan proofs bind current file receipts while changes proofs still demand real applied change sets', async t => {
const f = fixture(t); begin(f); const manager = new FileResults(f.store), planner = new TaskPlanManager(f.store);
const result = manager.record(f.session, input(f), { source: 'user' });
const plan = await planner.plan(f.session, { expected_revision: 0, steps: [{ key: 'existing', title: '核对已有说明', criterion: '当前说明原文件SHA与完整成果凭据一致', kind: 'results', paths: ['README.md'] }] });
const proof = await planner.verify(f.session, { plan_id: plan.id, expected_revision: 1, step_key: 'existing', evidence_id: result.result_id });
assert.equal(proof.status, 'completed'); assert.equal((await planner.inspect(f.session)).all_verified, true);
await assert.rejects(planner.evidence(f.session, { kind: 'changes', paths: ['README.md'] }, result.result_id), /不属于/);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'new owner text');
assert.equal((await planner.inspect(f.session)).all_verified, false);
});
test('duplicate receipts cannot create progress indefinitely or reset no-progress reads', async t => {
const f = harness(t, (round, f) => round === 0 ? calls(call('loop-read', 'read_file', { path: 'README.md' })) : calls(call('loop-' + round, 'verify_existing_files', input(f, { read_call_ids: ['loop-read'] }))));
f.runtime.start(f.session.id, '创建 README.md 文件,无需测试'); await f.wait();
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'no_progress');
assert.equal(f.manager.records(f.session).length, 1); assert.ok(f.store.core.run.get(f.session.runId).steps < 10);
});
test('manual completion preserves the failed original run and refuses unknown operations', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
const run = begin(f); runtime.fileResults().record(f.session, input(f), { source: 'user' });
const original = structuredClone(run);
await runtime.completeFileResults(f.session);
assert.equal(f.session.status, 'completed'); assert.equal(f.store.core.task.get(f.session.taskId).status, 'completed');
assert.deepEqual(run, original); assert.equal(f.session.changes.length, 0);
f.store.core.tool.set('unknown', { id: 'unknown', taskId: f.session.taskId, requiresVerification: true });
await assert.rejects(runtime.completeFileResults(f.session), /未知结果/);
});
test('completion refuses active work, files changed during inspection and a newly selected task', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f); runtime.fileResults().record(f.session, input(f), { source: 'user' });
runtime.active.set(f.session.id, { project: f.project });
try { await assert.rejects(runtime.completeFileResults(f.session), /仍有活动/); } finally { runtime.active.delete(f.session.id); }
const planner = runtime.taskPlanner();
planner.inspect = async () => { fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'Edit while inspecting\n'); return { configured: false }; };
await assert.rejects(runtime.completeFileResults(f.session), /核对期间/);
assert.equal(f.session.status, 'paused');
runtime.fileResults().record(f.session, input(f), { source: 'user' });
planner.inspect = async () => { begin(f, '创建 README.md 文件,无需测试', true); return { configured: false }; };
await assert.rejects(runtime.completeFileResults(f.session), /核对期间/);
assert.equal(f.session.status, 'paused');
assert.equal(f.store.core.task.get(f.session.taskId).delivery, undefined);
});
test('unrelated files cannot stand in for named output paths, nor can a late apply satisfy newer instructions with an old proposal', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f, '创建 result.html 文件,无需测试'); const manager = runtime.fileResults();
manager.record(f.session, input(f), { source: 'user' });
assert.deepEqual(manager.completion(f.session).missingPaths, ['result.html']); assert.equal(manager.completion(f.session).satisfied, false);
begin(f, '修改 README.md 文件标题,无需测试', true);
proposeFile(f.store, f.session, { path: 'README.md', content: '# Early proposal\n', reason: '早于追加要求' });
await new Promise(resolve => setTimeout(resolve, 10));
begin(f, '再修改 README.md 文件,加入新要求');
applyChange(f.store, f.session, f.session.changes.at(-1).id);
assert.equal(manager.completion(f.session).requiresChange, true);
assert.equal(f.session.status, 'paused');
await assert.rejects(runtime.completeFileResults(f.session), /修改/);
});
test('delivery and mobile projections distinguish adopted bytes, new changes, no tests and no commit; mutations cannot rewrite receipts', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f); const manager = runtime.fileResults(), saved = manager.record(f.session, input(f), { source: 'user' });
await runtime.completeFileResults(f.session);
const reporter = new DeliveryReporter(f.store), { report } = await reporter.create(f.session);
assert.equal(report.status, 'existing_verified'); assert.equal(report.checkPolicy, 'not_requested'); assert.equal(report.gitOutcome.status, 'no_new_changes');
assert.equal(report.verifiedCheckJobId, null); assert.equal(report.deliveries.length, 0); assert.match(renderDeliveryMarkdown(report), /未创建提交/);
const projection = mobileSession(f.store, runtime, f.session);
assert.equal(projection.fileResults[0].verification, 'current'); assert.equal(projection.fileResults[0].files[0].sha256, report.fileResults[0].files[0].sha256);
assert.equal(JSON.stringify(projection).includes(f.projectRoot.replaceAll('\\', '\\\\')), false);
const row = manager.records(f.session)[0]; row.reason = 'tampered';
assert.throws(() => manager.verify(f.session, saved.result_id), /封存原凭据/); row.reason = '已按当前目标核对现有文件内容。';
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'changed');
assert.equal((await reporter.create(f.session)).report.status, 'needs_results');
assert.equal(mobileSession(f.store, runtime, f.session).fileResults[0].verification, 'stale');
});
@@ -0,0 +1,103 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { safePath, readText, readLines, listFiles, searchFiles, proposeFile, applyChange, hash } from '../src/server/files.mjs';
import { fixture } from './helpers.mjs';
test('proposals preserve project bytes until review; apply backs up the exact original bytes', t => {
const { store, session, projectRoot, project, data } = fixture(t);
const original = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from('原文\r\n第二行\r\n')]);
fs.writeFileSync(path.join(projectRoot, 'notes.md'), original);
const file = readText(project, 'notes.md');
const proposed = proposeFile(store, session, { path: 'notes.md', content: '修改后\n第二行\n', reason: 'Test review', base_sha256: file.sha256 });
assert.equal(proposed.status, 'awaiting_review');
assert.deepEqual(fs.readFileSync(path.join(projectRoot, 'notes.md')), original);
session.status = 'needs_review';
const applied = applyChange(store, session, proposed.change_id);
assert.equal(applied.status, 'applied');
assert.equal(session.status, 'completed');
assert.equal(fs.readFileSync(path.join(projectRoot, 'notes.md'), 'utf8'), '修改后\n第二行\n');
assert.deepEqual(fs.readFileSync(path.join(data, 'backups', session.id, proposed.change_id + '.txt')), original);
assert.throws(() => applyChange(store, session, proposed.change_id));
});
test('new nested files are created only on apply and unchanged content creates no proposal', t => {
const { store, session, projectRoot, project } = fixture(t);
const current = readText(project, 'README.md');
assert.equal(proposeFile(store, session, { path: current.path, content: current.content }).status, 'unchanged');
const proposed = proposeFile(store, session, { path: 'docs/new.md', content: '# New\n', base_sha256: null });
assert.equal(fs.existsSync(path.join(projectRoot, 'docs')), false);
applyChange(store, session, proposed.change_id);
assert.equal(fs.readFileSync(path.join(projectRoot, 'docs', 'new.md'), 'utf8'), '# New\n');
});
test('stale reads and changed baselines are rejected without overwriting external edits', t => {
const { store, session, projectRoot, project } = fixture(t);
const original = readText(project, 'README.md');
const change = proposeFile(store, session, { path: 'README.md', content: '# Suggestion\n', base_sha256: original.sha256 });
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# External change\n');
assert.throws(() => proposeFile(store, session, { path: 'README.md', content: 'replacement', base_sha256: original.sha256 }), { status: 409 });
assert.throws(() => applyChange(store, session, change.change_id), { status: 409 });
assert.equal(session.changes[0].status, 'conflict');
assert.equal(fs.readFileSync(path.join(projectRoot, 'README.md'), 'utf8'), '# External change\n');
});
test('file paths reject traversal, absolute paths, credentials and outside junctions', t => {
const { store, session, project, projectRoot, root } = fixture(t);
for (const target of ['../outside.txt', 'a/../../outside.txt', 'C:\\Windows\\win.ini', '/etc/passwd', '.env', '.env.local', '.GiT/config', 'data:stream', 'a\0b']) {
assert.throws(() => safePath(project, target, true), target);
}
const outside = path.join(root, 'outside'); fs.mkdirSync(outside);
fs.writeFileSync(path.join(outside, 'outside.txt'), 'outside');
fs.symlinkSync(outside, path.join(projectRoot, 'link'), process.platform === 'win32' ? 'junction' : 'dir');
assert.throws(() => readText(project, 'link/outside.txt'));
assert.throws(() => proposeFile(store, session, { path: 'link/new.txt', content: 'must not write' }));
assert.ok(!listFiles(project).files.some(file => file.path.startsWith('link')));
assert.equal(fs.readFileSync(path.join(outside, 'outside.txt'), 'utf8'), 'outside');
});
test('read and search expose bounded text scopes and reject non UTF-8 / oversized files', t => {
const { projectRoot, project } = fixture(t);
fs.writeFileSync(path.join(projectRoot, 'lines.txt'), Array.from({ length: 500 }, (_, index) => 'TODO line ' + (index + 1)).join('\n'));
const lines = readLines(project, { path: 'lines.txt', start_line: 2, end_line: 500 });
assert.equal(lines.start_line, 2); assert.equal(lines.end_line, 301); assert.equal(lines.truncated, true);
const results = searchFiles(project, { query: 'TODO' });
assert.equal(results.matches.length, 80); assert.equal(results.truncated, true);
fs.writeFileSync(path.join(projectRoot, 'binary.bin'), Buffer.from([0, 1]));
fs.writeFileSync(path.join(projectRoot, 'invalid.txt'), Buffer.from([0xff, 0xfe]));
fs.writeFileSync(path.join(projectRoot, 'large.txt'), 'a'.repeat(1024 * 1024 + 1));
for (const file of ['binary.bin', 'invalid.txt', 'large.txt']) assert.throws(() => readText(project, file));
assert.equal(hash(Buffer.from('abc')), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
});
test('project-local junctions cannot expose or create protected Git and credential files', t => {
const { store, session, project, projectRoot } = fixture(t);
for (const name of ['.git', 'credentials', '.secrets']) {
fs.mkdirSync(path.join(projectRoot, name));
fs.writeFileSync(path.join(projectRoot, name, 'config.txt'), 'protected fixture bytes');
const alias = name.replaceAll('.', '') + '-alias';
fs.symlinkSync(path.join(projectRoot, name), path.join(projectRoot, alias), process.platform === 'win32' ? 'junction' : 'dir');
for (const relative of [name + '/config.txt', alias + '/config.txt']) {
assert.throws(() => readText(project, relative), relative);
assert.throws(() => listFiles(project, relative.split('/')[0]), relative);
}
assert.throws(() => proposeFile(store, session, { path: alias + '/new.txt', content: 'forbidden' }));
assert.equal(fs.existsSync(path.join(projectRoot, name, 'new.txt')), false);
assert.equal(fs.readFileSync(path.join(projectRoot, name, 'config.txt'), 'utf8'), 'protected fixture bytes');
}
assert.equal(session.changes.length, 0);
assert.ok(!listFiles(project).files.some(item => /alias|credentials|secrets|\.git/.test(item.path)));
fs.mkdirSync(path.join(projectRoot, 'public-docs'));
fs.writeFileSync(path.join(projectRoot, 'public-docs', 'info.txt'), 'public fixture');
fs.symlinkSync(path.join(projectRoot, 'public-docs'), path.join(projectRoot, 'docs-alias'), process.platform === 'win32' ? 'junction' : 'dir');
assert.equal(readText(project, 'docs-alias/info.txt').content, 'public fixture');
});
test('Windows trailing-dot and trailing-space path aliases cannot bypass protected paths', { skip: process.platform !== 'win32' }, t => {
const { project } = fixture(t);
assert.equal(safePath(project, '.'), project.root);
for (const relative of ['.git./config', '.git /config', '.env ', 'credentials /config', '.secrets./new.txt']) {
assert.throws(() => safePath(project, relative, true), relative);
}
});
@@ -0,0 +1,26 @@
import fs from 'node:fs';
import path from 'node:path';
export function createBrowserProject(f, { extra = '', beginTask = true } = {}) {
const source = 'let done = false; document.querySelector("#toggle").addEventListener("click", () => { done = !done; document.querySelector("#state").textContent = done ? "已完成" : "待办"; });';
fs.mkdirSync(path.join(f.projectRoot, 'src'), { recursive: true }); fs.mkdirSync(path.join(f.projectRoot, 'dist'), { recursive: true });
fs.writeFileSync(path.join(f.projectRoot, 'src', 'app.js'), source + extra);
fs.writeFileSync(path.join(f.projectRoot, 'dist', 'app.js'), source + extra);
fs.writeFileSync(path.join(f.projectRoot, 'dist', 'index.html'), '<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>工程页面</title>'
+ '<style>body{font:16px sans-serif;max-width:600px;margin:20px}input,select,button{display:block;margin:12px 0}</style>'
+ '<h1>实际浏览器样本</h1><label for="query">搜索任务</label><input id="query" aria-label="搜索任务"><select id="filter" aria-label="状态筛选"><option value="all">全部</option><option value="pending">待办</option></select>'
+ '<button id="toggle">切换状态</button><p id="state">待办</p><script src="/app.js"></script></html>');
if (beginTask) {
const run = f.store.beginRun(f.session, '检查这个工程实际页面', false, f.store.config);
f.session.status = 'paused'; f.store.finishRun(f.session, run, 'browser_ready');
}
return f;
}
export const browserSteps = [
{ action: 'fill', selector: '#query', value: '真实输入' },
{ action: 'select', selector: '#filter', value: 'pending' },
{ action: 'assert_count', selector: 'button', count: 1 },
{ action: 'assert_text', selector: '#state', text: '待办' },
{ action: 'click', selector: '#toggle' },
{ action: 'assert_text', selector: '#state', text: '已完成' },
];
@@ -0,0 +1,15 @@
import { Store } from '../../src/server/store.mjs';
import { ChangeSetManager } from '../../src/core/workspaces/change-sets.mjs';
const [data, sessionId, setId, phase, direction, ids] = process.argv.slice(2);
const store = new Store(data), manager = new ChangeSetManager(store, { faultInjector: (actual, set) => {
if (actual !== phase) return;
process.stdout.write(JSON.stringify({ phase, setId: set.id, direction }) + '\n'); process.exit(73);
} });
const session = store.session(sessionId);
if (direction === 'prepare') await manager.prepare(session, JSON.parse(ids));
else {
const set = store.owned('change_set', setId, sessionId);
if (direction === 'rollback') await manager.rollback(session, setId, set.fingerprint);
else await (await manager.start(session, setId, set.fingerprint)).promise;
}
process.stderr.write('Did not reach crash phase: ' + phase + '\n'); store.close(); process.exit(74);
@@ -0,0 +1,33 @@
import fs from 'node:fs';
import path from 'node:path';
import { proposeFile } from '../../src/server/files.mjs';
import { ChangeSetManager } from '../../src/core/workspaces/change-sets.mjs';
export function batchFixture(f, options = {}) {
const before = {
'src/one.mjs': Buffer.from('\uFEFFexport const one = 1;\r\n// 保留原文🙂\r\n'),
'src/two.mjs': Buffer.from('export const two = 2;\n'),
'docs/new.md': null,
};
const after = {
'src/one.mjs': Buffer.from('\uFEFFexport const one = 11;\r\n// 保留原文🙂\r\n'),
'src/two.mjs': Buffer.from('export const two = 22;\n'),
'docs/new.md': Buffer.from('# 新文件\n'),
};
for (const [relative, bytes] of Object.entries(before)) if (bytes) {
fs.mkdirSync(path.dirname(path.join(f.projectRoot, relative)), { recursive: true }); fs.writeFileSync(path.join(f.projectRoot, relative), bytes);
}
const run = f.store.beginRun(f.session, '修复两个模块并新增说明,保留已有工作。', false, f.store.config);
const ids = Object.entries(after).map(([relative, bytes]) => proposeFile(f.store, f.session,
{ path: relative, content: bytes.toString('utf8'), reason: '受控跨文件任务' }).change_id);
f.session.status = 'needs_review'; f.store.finishRun(f.session, run);
return { before, after, ids, manager: new ChangeSetManager(f.store, options) };
}
export function assertBatchBytes(assert, f, files) {
for (const [relative, bytes] of Object.entries(files)) {
const target = path.join(f.projectRoot, relative);
if (bytes === null) assert.equal(fs.existsSync(target), false, relative);
else assert.deepEqual(fs.readFileSync(target), bytes, relative);
}
}
@@ -0,0 +1,14 @@
import { Store } from '../../src/server/store.mjs';
import { CheckRunner } from '../../src/core/check-runner.mjs';
const [directory, sessionId, phase] = process.argv.slice(2);
const store = new Store(directory), session = store.session(sessionId), runner = new CheckRunner(store);
const plan = await runner.plan(session);
const original = store.save.bind(store);
store.save = (value, options) => {
const result = original(value, options);
if (options?.type === (phase === 'queued' ? 'check.queued' : 'check.result')) process.exit(69);
return result;
};
const started = await runner.start(session, plan.id); await started.promise;
process.exit(1);
@@ -0,0 +1,7 @@
import { Store } from '../../src/server/store.mjs';
import { RequestPlanner } from '../../src/core/context/request-plan.mjs';
const [directory, id] = process.argv.slice(2), store = new Store(directory), session = store.session(id);
const run = store.beginRun(session, 'Crash after durable context planning.', false, store.config);
session.providerMessages.push({ role: 'user', content: 'Crash after durable context planning.' });
new RequestPlanner(store).prepare(session, run, store.config, { system: 'Crash fixture system', tools: [] });
process.exit(67);
@@ -0,0 +1,55 @@
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { createEngineFixture } from '../../scripts/engine-fixtures.mjs';
import { CONTROLLED_CRITICAL_FILES, resolveControlledInstallation, controlledDigest } from '../../src/core/engines/controlled-installation.mjs';
import { EngineRunner } from '../../src/core/engines/engine-runner.mjs';
import { prepareWorkerFixture } from './workers-project.mjs';
// A protocol fixture, not the native Harness. Native acceptance is independent.
export class ControlledWorkerFixture {
constructor() { this.report = { notifications: [], bridgeRequests: [] }; this.received = []; }
async open(options) {
this.options = options; this.report.owner = options.owner; this.report.sdkSessionId = options.sdkSessionId;
options.onState('ready', {}); return this.report;
}
async prompt(content) { this.content = content; this.report.receipt = { messageId: randomUUID() }; return this.report.receipt; }
async wait() {
const { onModel, onTool, tools, config, signal } = this.options;
const messages = [{ role: 'user', content: [{ type: 'text', text: this.content }] }];
for (let step = 0; step < 100; step++) {
signal.throwIfAborted();
const input = { messages: [{ role: 'system', content: [{ type: 'text', text: 'Protocol fixture for the owned task.' + (this.options.strategy ? '\n\n' + this.options.strategy.section.text : '') }], source: { kind: 'system-prompt' } }, ...structuredClone(messages)], system: null,
tools: tools.map(value => value.function ?? value), toolHistory: null, model: config.model, maxTokens: config.maxOutputTokens };
const result = await onModel(input, { id: randomUUID() }, signal);
signal.throwIfAborted();
const message = result.message, content = [{ type: 'text', text: message.content }];
for (const call of message.tool_calls ?? []) content.push({ type: 'tool-call', id: call.id, name: call.function.name, arguments: call.function.arguments });
messages.push({ role: 'assistant', content });
if (!message.tool_calls?.length) return message.content;
for (const call of message.tool_calls) {
const input = { callId: call.id, name: call.function.name, arguments: JSON.parse(call.function.arguments) };
const result = await onTool(input, { id: randomUUID() }, signal);
this.received.push({ input, result, resultSha256: controlledDigest(result) });
messages.push({ role: 'tool', toolCallId: call.id, content: [{ type: 'text', text: JSON.stringify(result) }] });
}
}
throw new Error('Fixture exceeded its step bound.');
}
async close({ force } = {}) {
return Object.assign(this.report, { status: force ? 'cancelled' : 'closed', cleanup: { rootExited: true, treeConfirmed: true, remainingProcesses: [] } });
}
}
export function controlledWorker(f, { respond = null, sessionFactory = () => new ControlledWorkerFixture() } = {}) {
const context = prepareWorkerFixture(f, { respond });
const root = createEngineFixture(path.join(f.root, 'controlled-harness'));
for (const file of CONTROLLED_CRITICAL_FILES) {
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'export const fixture = true;\n');
}
const installation = resolveControlledInstallation(f.root, root), native = [];
context.runtime.controlledEngines = new EngineRunner(f.store, context.provider, { directoryRoot: path.join(f.root, 'owned-engines'),
sessionFactory: () => { const value = sessionFactory(); native.push(value); return value; } });
const definition = { goal: '交付受控引擎的独立工程成果', engine: { id: 'deepseek-harness', root, controlledSha256: installation.controlledSha256 },
budget: { maxSteps: 30 }, children: [{ label: 'engine-1', goal: '实现独立文件并通过当前检查', paths: ['src/a.mjs'] }] };
return { ...context, installation, root, native, definition };
}
@@ -0,0 +1,54 @@
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { Store } from '../../src/server/store.mjs';
import { Runtime } from '../../src/server/runtime.mjs';
import { Provider } from '../../src/server/provider.mjs';
const [directory, phase] = process.argv.slice(2);
const store = new Store(directory, { faultInjector: point => { if (phase === 'migration' && point === 'migration_records') process.exit(66); } });
const session = [...store.sessions.values()][0];
const project = store.project(session.projectId);
const call = { id: 'crash-' + phase, type: 'function', function: { name: 'run_command', arguments: JSON.stringify({ command: 'node worker-effect.mjs', reason: 'Controlled crash fixture' }) } };
if (phase === 'after_state' || phase === 'after_event') {
store.storage.faultInjector = point => { if (point === phase) process.exit(66); };
store.message(session, 'user', 'This transaction must roll back.');
throw new Error('Transaction crash point was not reached.');
}
if (phase === 'accepted' || phase === 'queued') {
const run = store.beginRun(session, 'Crash fixture task', false, store.config);
if (phase === 'queued') {
session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [call] });
const event = { id: randomUUID(), kind: 'tool', phase: 'queued', tool: 'run_command', toolCallId: call.id, runId: run.id, taskId: run.taskId, label: 'Controlled fixture', createdAt: new Date().toISOString() };
session.events.push(event); store.registerInvocation(session, call, run, event); store.save(session);
}
process.exit(66);
}
const provider = new Provider();
let requests = 0;
provider.complete = async () => {
requests++;
if (requests > 1 && phase === 'before_next_model') process.exit(66);
return { message: requests === 1 ? { role: 'assistant', content: '', tool_calls: [call] } : { role: 'assistant', content: 'Done.' }, usage: { prompt_tokens: 20, completion_tokens: 10 } };
};
const runtime = new Runtime(store, provider);
if (phase === 'approved') runtime.executeCommand = () => process.exit(66);
const finish = store.finishInvocation.bind(store);
store.finishInvocation = (...args) => {
if (phase === 'effect_unrecorded') process.exit(66);
const result = finish(...args);
if (phase === 'result_committed') process.exit(66);
return result;
};
runtime.start(session.id, 'Crash fixture task');
const poll = setInterval(() => {
const approval = session.approvals.find(value => value.status === 'pending');
if (!approval) return;
if (phase === 'approval_wait') process.exit(66);
runtime.decide(session.id, approval.id, true);
}, 10);
await runtime.active.get(session.id).promise;
clearInterval(poll); store.close();
throw new Error('Runtime crash point was not reached: ' + phase + ' at ' + path.basename(project.root));
@@ -0,0 +1,15 @@
import { Store } from '../../src/server/store.mjs';
import { GitDelivery } from '../../src/core/git-delivery.mjs';
const [data, sessionId, planId, cut] = process.argv.slice(2);
const store = new Store(data);
const delivery = new GitDelivery(store, {
faultInjector: (phase, job) => {
if (phase !== cut) return;
process.stdout.write(JSON.stringify({ phase, jobId: job.id, sessionId, expectedCommit: job.expectedCommit }) + '\n');
process.exit(73);
},
});
const started = await delivery.start(store.session(sessionId), planId);
const job = await started.promise;
process.stderr.write('Did not reach crash phase: ' + cut + ' / ' + job.status + ' / ' + job.summary + '\n');
await delivery.stopAll(); store.close(); process.exit(74);
@@ -0,0 +1,55 @@
import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { CheckRunner } from '../../src/core/check-runner.mjs';
import { GitDelivery } from '../../src/core/git-delivery.mjs';
import { proposeFile, applyChange } from '../../src/server/files.mjs';
import { gitEnvironment } from '../../src/core/git-process.mjs';
export function writeDeliveryFile(f, relative, contents) {
const target = path.join(f.projectRoot, relative); fs.mkdirSync(path.dirname(target), { recursive: true }); fs.writeFileSync(target, contents);
}
export function deliveryGit(f, args, input) {
return execFileSync('git', ['--no-pager', '--no-optional-locks', '-C', f.projectRoot, ...args],
{ windowsHide: true, encoding: 'utf8', env: gitEnvironment(), input });
}
export function createDeliveryProject(f, { broken = true, ownerChanges = true } = {}) {
writeDeliveryFile(f, '.gitignore', 'dist/\n.env\n.awb-*\n');
writeDeliveryFile(f, 'package.json', JSON.stringify({ name: 'git-delivery-fixture', type: 'module', private: true, scripts: { test: 'node --test test/*.test.mjs', build: 'node scripts/build.mjs' } }, null, 2));
writeDeliveryFile(f, 'src/add.mjs', 'export const add = (a, b) => a ' + (broken ? '-' : '+') + ' b;\n');
writeDeliveryFile(f, 'test/add.test.mjs', "import test from 'node:test'; import assert from 'node:assert/strict'; import { add } from '../src/add.mjs';\ntest('adds positive values', () => assert.equal(add(2, 3), 5));\ntest('adds negative values', () => assert.equal(add(-2, 3), 1));\ntest('preserves integer addition', () => assert.equal(add(10, 1), 11));\n");
writeDeliveryFile(f, 'scripts/build.mjs', "import fs from 'node:fs'; fs.cpSync('src', 'dist', { recursive: true }); console.log('Delivery fixture built.');\n");
writeDeliveryFile(f, 'owner-staged.txt', 'Owner baseline staged file\n');
writeDeliveryFile(f, 'owner-unstaged.txt', 'Owner baseline unstaged file\n');
deliveryGit(f, ['init', '--initial-branch=main']);
deliveryGit(f, ['config', 'core.autocrlf', 'false']);
deliveryGit(f, ['config', 'user.name', 'Workbench QA']);
deliveryGit(f, ['config', 'user.email', 'qa@workbench.invalid']);
const files = ['README.md', '.gitignore', 'package.json', 'src/add.mjs', 'test/add.test.mjs', 'scripts/build.mjs', 'owner-staged.txt', 'owner-unstaged.txt'];
deliveryGit(f, ['add', '--', ...files]);
deliveryGit(f, ['commit', '--no-gpg-sign', '-m', 'Sealed delivery fixture baseline']);
if (ownerChanges) {
writeDeliveryFile(f, 'owner-staged.txt', 'Owner staged change: keep this exact staged content\n');
deliveryGit(f, ['add', '--', 'owner-staged.txt']);
writeDeliveryFile(f, 'owner-staged.txt', 'Owner working copy after staging: keep both versions\n');
writeDeliveryFile(f, 'owner-unstaged.txt', 'Owner unstaged change: keep this content\n');
writeDeliveryFile(f, 'owner-untracked.txt', 'Owner untracked content: keep this file untracked\n');
}
const run = f.store.beginRun(f.session, 'Repair addition, verify the project and deliver only reviewed files', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
return run;
}
export function applyDeliveryFix(f) {
for (const [relative, content] of [['src/add.mjs', "import { sum } from './math.mjs';\nexport const add = sum;\n"], ['src/math.mjs', 'export const sum = (a, b) => a + b;\n']]) {
const proposed = proposeFile(f.store, f.session, { path: relative, content, reason: 'Reviewed addition repair' });
applyChange(f.store, f.session, proposed.change_id);
}
}
export async function checkedDelivery(f, { broken = true, ownerChanges = true } = {}) {
createDeliveryProject(f, { broken, ownerChanges }); if (broken) applyDeliveryFix(f);
const checks = new CheckRunner(f.store), delivery = new GitDelivery(f.store);
f.onCleanup(() => checks.stopAll()); f.onCleanup(() => delivery.stopAll());
const plan = await checks.plan(f.session), started = await checks.start(f.session, plan.id);
const check = await started.promise;
return { checks, check, delivery };
}
@@ -0,0 +1,28 @@
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
export function specification(kind, root) {
if (kind === 'node') return { tests: 8, checks: ['test', 'build'], initial: null, fixes: {
'src/add.mjs': 'export function add(left, right) {\n if (!Number.isFinite(left) || !Number.isFinite(right)) throw new TypeError("finite numeric inputs required");\n return left + right;\n}\n',
'src/range.mjs': 'export function inclusiveRange(start, end) {\n if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || end < start || end - start >= 10000) throw new RangeError("bounded ascending integer range required");\n return Array.from({ length: end - start + 1 }, (_, index) => start + index);\n}\n',
} };
if (kind === 'react') {
const app = fs.readFileSync(path.join(root, 'src/App.tsx'), 'utf8'); assert.ok(app.includes("toggleTask(current, 'missing')"));
return { tests: 9, checks: ['test', 'typecheck', 'build'], initial: null, fixes: {
'src/query.ts': "import type { Task } from './types.ts';\nexport function selectTasks(tasks: Task[], filter: string, query: string): Task[] {\n const text = query.trim().toLocaleLowerCase();\n return tasks.filter(task => (filter === 'done' ? task.done : filter === 'pending' ? !task.done : true) && task.title.toLocaleLowerCase().includes(text));\n}\n",
'src/summary.ts': "import type { Task } from './types.ts';\nexport function summarizeTasks(tasks: Task[]) {\n return { total: tasks.length, completed: tasks.filter(task => task.done).length, pendingHours: tasks.filter(task => !task.done).reduce((sum, task) => sum + task.hours, 0) };\n}\n",
'src/transition.ts': "import type { Task } from './types.ts';\nexport function toggleTask(tasks: Task[], id: string): Task[] {\n return tasks.map(task => task.id === id ? { ...task, done: !task.done } : task);\n}\n",
'src/App.tsx': app.replace("toggleTask(current, 'missing')", 'toggleTask(current, task.id)'),
} };
}
const test = file => fs.readFileSync(path.join('benchmarks/workers-delivery-v1/python-tests', file), 'utf8');
return { tests: 10, checks: ['python:test', 'python:compile'], initial: {
'money.py': 'def to_cents(value):\n return int(float(value) * 100)\n',
'schedule.py': 'def normalize_schedule(values):\n return list(values)\n',
'tests/test_money.py': test('test_money.py'), 'tests/test_schedule.py': test('test_schedule.py'),
}, fixes: {
'money.py': 'from decimal import Decimal, InvalidOperation, ROUND_HALF_UP\n\ndef to_cents(value):\n try:\n amount = Decimal(str(value))\n if not amount.is_finite():\n raise ValueError("finite amount required")\n return int((amount * 100).quantize(Decimal("1"), rounding=ROUND_HALF_UP))\n except (InvalidOperation, OverflowError, TypeError) as error:\n raise ValueError("valid finite amount required") from error\n',
'schedule.py': 'import re\nfrom datetime import date\n\ndef normalize_schedule(values):\n dates = []\n for value in values:\n if not isinstance(value, str) or not re.fullmatch(r"\\d{4}-\\d{2}-\\d{2}", value):\n raise ValueError("ISO calendar date required")\n dates.append(date.fromisoformat(value))\n return [value.isoformat() for value in sorted(set(dates))]\n',
} };
}
@@ -0,0 +1,37 @@
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { WorkspaceManager } from '../../src/core/workspaces/git-workspaces.mjs';
import { deliveryGit, writeDeliveryFile as write } from './delivery-project.mjs';
export async function environmentProject(f,{dependency=true,postinstall="node -e \"require('fs').writeFileSync('node_modules/lifecycle.txt','approved')\"",response='ok'}={}) {
let registry='https://registry.npmjs.org/',requests=0,server=null,bytes;
if(dependency) {
const pack=path.join(f.root,'tar-content');fs.mkdirSync(path.join(pack,'package'),{recursive:true});
fs.writeFileSync(path.join(pack,'package','package.json'),JSON.stringify({name:'awb-env-fixture',version:'1.0.0',main:'index.cjs'}));
fs.writeFileSync(path.join(pack,'package','index.cjs'),'module.exports = { value: 42 };');
const tar=path.join(f.root,'fixture.tgz');execFileSync(process.platform==='win32'?'tar.exe':'tar',['-czf',tar,'-C',pack,'package'],{windowsHide:true});bytes=fs.readFileSync(tar);
server=http.createServer((request,result)=>{requests++;if(response==='hang'){request.on('close',()=>result.destroy());return;}
if(response!=='ok'){result.writeHead(503,{'Content-Type':'text/plain'});result.end('fixture registry unavailable');return;}
result.writeHead(200,{'Content-Type':'application/octet-stream'});result.end(bytes);
});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));registry='http://127.0.0.1:'+server.address().port+'/';
f.onCleanup(()=>{server.closeAllConnections();return new Promise(resolve=>server.close(resolve));});
}
const manifest={name:'environment-fixture',version:'1.0.0',private:true,type:'module',scripts:{test:'node --test test.test.mjs',...(postinstall?{postinstall}: {})},
...(dependency?{dependencies:{'awb-env-fixture':'1.0.0'}}:{})};
write(f,'package.json',JSON.stringify(manifest));write(f,'.gitignore','node_modules/\ndist/\n');
write(f,'package-lock.json',JSON.stringify({name:manifest.name,version:manifest.version,lockfileVersion:3,requires:true,packages:{'':{name:manifest.name,version:manifest.version,...(dependency?{dependencies:manifest.dependencies}:{})},
...(dependency?{'node_modules/awb-env-fixture':{version:'1.0.0',resolved:registry+'awb-env-fixture/-/awb-env-fixture-1.0.0.tgz',integrity:'sha512-'+createHash('sha512').update(bytes).digest('base64')}}:{})}}));
write(f,'test.test.mjs',dependency?"import test from 'node:test';import assert from 'node:assert/strict';import fixture from 'awb-env-fixture';test('uses installed dependency',()=>assert.equal(fixture.value,42));":"import test from 'node:test';import assert from 'node:assert/strict';test('builtin check',()=>assert.equal(6*7,42));");
deliveryGit(f,['init','--initial-branch=main']);deliveryGit(f,['-c','core.autocrlf=false','add','.']);deliveryGit(f,['-c','core.autocrlf=false','-c','user.name=Workbench QA','-c','user.email=qa@workbench.local','commit','-m','Owned dependency fixture']);
const run=f.store.beginRun(f.session,'核对依赖环境并执行工程检查',false,f.store.config);f.session.status='paused';f.store.finishRun(f.session,run);
const workspaces=new WorkspaceManager(f.store);f.onCleanup(()=>workspaces.stopAll());
const workspace=await workspaces.prepare(f.session,'环境准备验收');const {promise}=await workspaces.start(f.session,workspace.id,workspace.fingerprint);
const ready=await promise;if(ready.status!=='ready')throw new Error(ready.summary);
const project=f.store.project(workspace.childProjectId),session=f.store.session(workspace.childSessionId),childRun=f.store.beginRun(session,'核对独立环境并检查',false,f.store.config);
session.status='paused';f.store.finishRun(session,childRun);
return {project,session,workspace,registry,requests:()=>requests,sourceRoot:f.projectRoot,root:project.root};
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,6 @@
# Owned 1.9.0 artifact storage fixture
`artifact-store.mjs` is the unchanged source at commit `91981c1`. It retains the
pre-browser text-only artifact API. Upgrade regressions use its actual object
and existing blobs to verify a new binary API after the host upgrades, and
rollback to the old object if installing the new request handler fails.
@@ -0,0 +1,40 @@
import fs from 'node:fs';
import path from 'node:path';
import { createHash, randomUUID } from 'node:crypto';
export class ArtifactStore {
constructor(directory) {
this.root = path.join(directory, 'artifacts'); fs.mkdirSync(this.root, { recursive: true });
if (fs.lstatSync(this.root).isSymbolicLink()) throw new Error('执行工件目录不能是链接。');
}
write(session, run, value, { kind = 'tool-result', mediaType = 'application/json' } = {}) {
const bytes = Buffer.from(typeof value === 'string' ? value : JSON.stringify(value), 'utf8');
if (bytes.length > 8 * 1024 * 1024) throw new Error('单项执行工件超过8MB,请缩小输出范围。');
const sha256 = createHash('sha256').update(bytes).digest('hex');
const relative = sha256.slice(0, 2) + '/' + sha256 + '.blob';
const file = path.join(this.root, relative);
fs.mkdirSync(path.dirname(file), { recursive: true });
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.existsSync(file) && fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能通过链接写入。');
if (fs.existsSync(file)) {
const existing = fs.readFileSync(file);
if (existing.length !== bytes.length || createHash('sha256').update(existing).digest('hex') !== sha256) {
throw new Error('已有执行工件校验失败,实际结果需要核对。');
}
} else {
const temporary = file + '.' + randomUUID() + '.tmp';
const handle = fs.openSync(temporary, 'wx', 0o600);
try { fs.writeFileSync(handle, bytes); fs.fsyncSync(handle); } finally { fs.closeSync(handle); }
try { fs.renameSync(temporary, file); } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
}
return { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run?.taskId ?? session.taskId ?? null,
runId: run?.id ?? session.runId ?? null, kind, mediaType, sha256, bytes: bytes.length, relative, createdAt: new Date().toISOString(), status: 'stored' };
}
read(artifact) {
if (!/^[a-f0-9]{2}\/[a-f0-9]{64}\.blob$/.test(artifact.relative)) throw new Error('执行工件路径无效。');
const file = path.join(this.root, artifact.relative);
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能是链接。');
const bytes = fs.readFileSync(file);
if (bytes.length !== artifact.bytes || createHash('sha256').update(bytes).digest('hex') !== artifact.sha256) throw new Error('执行工件校验失败,实际结果需要核对。');
return bytes.toString('utf8');
}
}
@@ -0,0 +1,5 @@
# 自有 1.0.0 热升级工件
从本产品已交付的 `releases/Agent-Workbench-1.0.0-win-x64/dist-server/hot-upgrade.mjs` 保留原字节,用于验证运行过一次旧热升级的进程继续升级到SQLite内核。
这里仅包含Agent Workbench自有升级实现与原捆绑的公开依赖,没有用户数据或连接凭据。测试为它创建独立数据/端口和界面目录,验证旧handler闭包恢复、本地进程身份、原对象中的合成内存连接、连续升级及数据库所有权释放。
@@ -0,0 +1,5 @@
# Legacy Workbench fixture
`dist-server/server.mjs` is the original Agent Workbench 0.1.0 server bundle created in this project. It is retained as a standalone compatibility fixture so the in-place upgrade test can run from an extracted source package.
It contains the project's own original implementation and bundled dependencies whose license texts are included in the source release. It contains no user sessions, configuration, API keys, or third-party client recovery artifacts. Tests launch it with an isolated data directory.
@@ -0,0 +1,21 @@
import fs from 'node:fs';
import path from 'node:path';
import { Store } from '../../src/server/store.mjs';
import { Provider } from '../../src/server/provider.mjs';
import { Runtime } from '../../src/server/runtime.mjs';
const [data, sessionId] = process.argv.slice(2);
const store = new Store(data), provider = new Provider();
provider.setKey('owned-mcp-key-canary');
const runtime = new Runtime(store, provider), session = store.session(sessionId);
runtime.start(sessionId, 'Owned MCP crash fixture: invoke exactly the registered tool.');
const timer = setInterval(() => {
const approval = session.approvals.find(value => value.status === 'pending');
if (approval) runtime.decide(session.id, approval.id, true);
const job = [...store.core.mcp_job.values()].find(value => value.sessionId === session.id && value.operation === 'call' && value.status === 'calling');
if (job?.directory && fs.existsSync(path.join(job.directory, 'owned-call-dispatched.txt'))) {
console.log(JSON.stringify({ crashAfterDispatched: true, jobId: job.id, invocationId: job.invocationId }));
process.exit(85);
}
}, 10);
setTimeout(() => { clearInterval(timer); console.error('Owned MCP crash fixture deadline.'); process.exit(86); }, 12000);
@@ -0,0 +1,38 @@
// Deterministic response fixture for bridge acceptance, never a real-model
// solution or a modification to the sealed pilot's original project.
export const nativeContextSpecification = {
tests: 9, checks: ['test', 'build'], initial: null, fixes: {
'src/amount.mjs': `export function amountToCents(value) {
if (typeof value !== 'string' && (typeof value !== 'number' || !Number.isFinite(value))) throw new TypeError('Decimal amount required');
const match = /^([+-]?)(\\d+)(?:\\.(\\d{1,2}))?$/.exec(String(value).trim());
if (!match) throw new TypeError('At most two decimal places required');
const cents = (BigInt(match[2]) * 100n + BigInt((match[3] ?? '').padEnd(2, '0'))) * (match[1] === '-' ? -1n : 1n);
if (cents > BigInt(Number.MAX_SAFE_INTEGER) || cents < BigInt(Number.MIN_SAFE_INTEGER)) throw new TypeError('Safe cents required');
return Number(cents);
}
`,
'src/summary.mjs': `import { amountToCents } from './amount.mjs';
export function summarize(rows) {
if (!Array.isArray(rows)) throw new TypeError('Rows required');
const totals = new Map();
for (const row of rows) {
if (!row || typeof row.currency !== 'string' || !/^[A-Za-z]{3}$/.test(row.currency.trim())) throw new TypeError('Three-letter currency required');
const currency = row.currency.trim().toUpperCase(), previous = totals.get(currency) ?? { currency, cents: 0, count: 0 };
const cents = previous.cents + amountToCents(row.amount);
if (!Number.isSafeInteger(cents)) throw new TypeError('Safe aggregate required');
totals.set(currency, { currency, cents, count: previous.count + 1 });
}
return [...totals.values()].sort((left, right) => left.currency.localeCompare(right.currency));
}
`,
'src/report.mjs': `export function formatReport(groups) {
if (!Array.isArray(groups)) throw new TypeError('Groups required');
return groups.map(({ currency, cents, count }) => {
if (!/^[A-Z]{3}$/.test(currency) || !Number.isSafeInteger(cents) || !Number.isSafeInteger(count) || count < 0) throw new TypeError('Valid report group required');
const absolute = Math.abs(cents);
return currency + ' ' + (cents < 0 ? '-' : '') + Math.trunc(absolute / 100) + '.' + String(absolute % 100).padStart(2, '0') + ' (' + count + ')';
}).join('\\n');
}
`,
},
};
@@ -0,0 +1,80 @@
{
"schemaVersion": 1,
"provenance": {
"kind": "owned-real-model-failure-replay",
"sessionId": "4d085dd0-aa21-4d9f-a968-1b1117618b42",
"exportSha256": "84a754362c8ffdf4fd656290b0a156c2cc19162505a194925ede479bcf81f3bd",
"note": "Original two model replies reproduce context pressure only. The baseline checks step is a historical model proposal, not an endorsed plan or passed evidence."
},
"goal": "修复这个Node费用报表工程的amount、summary、report三个模块。金额用精确分单位处理,接受测试约定的十进制字符串/有限数字、退款、0及空白;拒绝格式错误、超精度和不安全金额。按独立币种规范化/排序汇总并保留原数组,计数准确、安全求和,报表两位小数含负分金额。以原九项测试为完整契约。先inspect_project,再plan_checks选择[\"test\",\"build\"]并run_checks保留真实基线失败,读取原测试和相关源码。只允许修改src/amount.mjs、src/summary.mjs、src/report.mjs;不改测试、配置、README、构建脚本、owner文件或原暂存,不使用run_command,不直接提交Git。propose_file/propose_patch准备完整三个模块后,用prepare_change_set一次封存全部三文件,等待审查应用。应用后继续同一个Task,重新plan_checks/run_checks,通过原9项测试和build后,用plan_delivery为三个文件准备本地Git预览并等待确认。模型必须自己实现,禁止用口头完成替代检查。预算最多8次模型请求、90000tokens,输出2048,上下文16K,请合并独立工具调用并避免重复观察。",
"replies": [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_CSpL0Z0taDo3JSvSsUM9cX1b",
"type": "function",
"function": {
"name": "inspect_project",
"arguments": "{}"
}
}
]
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_y57eUlmWs6kQosUucfBCmDJz",
"type": "function",
"function": {
"name": "plan_checks",
"arguments": "{\"names\":[\"test\",\"build\"]}"
}
},
{
"id": "call_adOQYrTfGG7la2wZU2tjctIr",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\":\"test/expense.test.mjs\"}"
}
},
{
"id": "call_Dtu2oZXnYjE7nfwqWjtKFLr1",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\":\"src/amount.mjs\"}"
}
},
{
"id": "call_yIjGWvDQx96grZKungT5LCSD",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\":\"src/summary.mjs\"}"
}
},
{
"id": "call_eTBhRkzY8eupvG0FAYRGlQSq",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\":\"src/report.mjs\"}"
}
},
{
"id": "call_6qZDMzBIlfG8WqSclbh6ceV0",
"type": "function",
"function": {
"name": "plan_task",
"arguments": "{\"expected_revision\":0,\"steps\":[{\"key\":\"baseline\",\"kind\":\"checks\",\"title\":\"保留原测试与构建的真实基线结果\",\"checks\":[\"test\",\"build\"],\"criterion\":\"执行原test和build并保留实际退出码与日志;基线失败如实记录\"},{\"key\":\"repair\",\"kind\":\"changes\",\"title\":\"修复三个费用模块\",\"paths\":[\"src/amount.mjs\",\"src/summary.mjs\",\"src/report.mjs\"],\"criterion\":\"仅三个模块的完整批次经用户审核应用\"},{\"key\":\"validation\",\"kind\":\"checks\",\"title\":\"验证原九项测试和构建\",\"depends_on\":[\"repair\"],\"checks\":[\"test\",\"build\"],\"criterion\":\"原9项测试全部通过且build退出码为0\"}]}"
}
}
]
}
]
}
@@ -0,0 +1,29 @@
import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import { environmentProject } from './environment-project.mjs';
import { workspace } from '../helpers.mjs';
export async function freePreviewPort() {
const server = net.createServer(); await new Promise((resolve, reject) => { server.once('error', reject); server.listen(0, '127.0.0.1', resolve); });
const port = server.address().port; await new Promise(resolve => server.close(resolve)); return port;
}
export async function previewProject(f) {
const p = await environmentProject(f, { dependency: false, postinstall: null }), dependencies = new Set();
const copy = name => {
if (dependencies.has(name)) return; const source = path.join(workspace, 'node_modules', name);
if (!fs.existsSync(source)) return;
dependencies.add(name); const info = JSON.parse(fs.readFileSync(path.join(source, 'package.json')));
fs.cpSync(source, path.join(p.root, 'node_modules', name), { recursive: true });
for (const name of Object.keys({ ...info.dependencies, ...info.optionalDependencies })) copy(name);
};
copy('vite'); copy('react'); copy('react-dom');
const versions = Object.fromEntries(['vite','react','react-dom'].map(name => [name, JSON.parse(fs.readFileSync(path.join(p.root,'node_modules',name,'package.json'))).version]));
const original = JSON.parse(fs.readFileSync(path.join(workspace, 'package-lock.json'))), packages = {};
for (const name of dependencies) if (original.packages['node_modules/' + name]) packages['node_modules/' + name] = original.packages['node_modules/' + name];
fs.writeFileSync(path.join(p.root, 'package.json'), JSON.stringify({ name: 'owned-vite-preview', version: '1.0.0', type: 'module', scripts: { dev: 'vite' }, devDependencies: versions }));
fs.writeFileSync(path.join(p.root, 'package-lock.json'), JSON.stringify({ lockfileVersion: 3, packages: { '': { devDependencies: versions }, ...packages } }));
fs.writeFileSync(path.join(p.root, 'index.html'), '<!doctype html><html><body><h1>所属预览初版</h1><script type="module" src="/src.js"></script></body></html>');
fs.writeFileSync(path.join(p.root, 'src.js'), "document.body.dataset.ready = 'yes';");
return { ...p, port: await freePreviewPort() };
}
@@ -0,0 +1,20 @@
import fs from 'node:fs';
import path from 'node:path';
import { PURGE_SOURCE_FILES, resolvePurgeSource, PURGE_PROMPT_KIND, PURGE_DEFAULT_PROMPT } from '../../src/core/engines/purge-strategy.mjs';
// Source-identity fixture only. Its JavaScript would throw if imported.
export function createPurgeSource(directory, version = '1.1.78') {
fs.mkdirSync(directory, { recursive: true });
for (const relative of PURGE_SOURCE_FILES) {
const file = path.join(directory, relative); fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, relative === 'package.json' ? JSON.stringify({ name: 'dsh-purge', version, dshTarget: '0.2.1-rc.2',
type: 'module', main: 'lib/index.js', license: 'Apache-2.0', scripts: { postinstall: 'throw-if-run' } })
: relative.endsWith('.js') ? 'throw new Error("Third-party Purge code must not run in this fixture.");\n' : 'Declared source identity fixture.\n');
}
return directory;
}
export function purgeInput(root, prompt = PURGE_DEFAULT_PROMPT) {
const source = resolvePurgeSource(process.cwd(), root);
if (source.status !== 'available') throw new Error(source.message);
return { kind: PURGE_PROMPT_KIND, root, sourceSha256: source.sourceSha256, prompt };
}
@@ -0,0 +1,41 @@
import fs from 'node:fs';
import path from 'node:path';
import { createDeliveryProject, deliveryGit, writeDeliveryFile } from './delivery-project.mjs';
export const pythonFixes = {
'src/workflow/normalize.py': 'def normalize(value):\n return max(0, float(value))\n',
'src/workflow/summary.py': 'def summarize(values):\n return {"count": len(values), "total": sum(values)}\n',
'src/workflow/render.py': 'def render(value):\n return f"共 {value[\'count\']} 项 · 合计 {value[\'total\']:g}"\n',
};
export const pythonProtected = ['pyproject.toml', 'tests/__init__.py', 'tests/test_workflow.py', 'src/workflow/__init__.py'];
export function createPythonDeliveryProject(f, { ownerChanges = true } = {}) {
createDeliveryProject(f, { broken: false, ownerChanges: false });
writeDeliveryFile(f, '.gitignore', 'dist/\n.env\n.awb-*\n.venv/\nvenv/\n__pycache__/\n.pytest_cache/\n');
writeDeliveryFile(f, 'pyproject.toml', '[project]\nname = "python-delivery-fixture"\nversion = "0.1.0"\n\n[tool.agent-workbench.python]\nframework = "unittest"\ntest-paths = ["tests"]\n');
writeDeliveryFile(f, 'tests/__init__.py', '');
writeDeliveryFile(f, 'tests/test_workflow.py', 'import unittest\nfrom workflow.normalize import normalize\nfrom workflow.summary import summarize\nfrom workflow.render import render\n'
+ 'class Workflow(unittest.TestCase):\n def test_number(self): self.assertEqual(normalize("3"), 3)\n'
+ ' def test_negative(self): self.assertEqual(normalize(-2), 0)\n def test_empty(self): self.assertEqual(summarize([]), {"count": 0, "total": 0})\n'
+ ' def test_summary(self): self.assertEqual(summarize([2, 3]), {"count": 2, "total": 5})\n'
+ ' def test_render(self): self.assertEqual(render({"count": 2, "total": 5}), "共 2 项 · 合计 5")\n'
+ ' def test_zero(self): self.assertEqual(render({"count": 0, "total": 0}), "共 0 项 · 合计 0")\n');
writeDeliveryFile(f, 'src/workflow/__init__.py', '');
writeDeliveryFile(f, 'src/workflow/normalize.py', 'def normalize(value):\n return -float(value)\n');
writeDeliveryFile(f, 'src/workflow/summary.py', 'def summarize(values):\n return {"count": len(values), "total": 0}\n');
writeDeliveryFile(f, 'src/workflow/render.py', 'def render(value):\n return ""\n');
deliveryGit(f, ['add', '--', '.gitignore', ...pythonProtected, ...Object.keys(pythonFixes)]);
deliveryGit(f, ['commit', '--no-gpg-sign', '-m', 'Seal three-module Python failure']);
if (ownerChanges) {
writeDeliveryFile(f, 'owner-staged.txt', 'Owner staged change: preserve the index\n');
deliveryGit(f, ['add', '--', 'owner-staged.txt']);
writeDeliveryFile(f, 'owner-staged.txt', 'Owner working copy: preserve this separately\n');
writeDeliveryFile(f, 'owner-unstaged.txt', 'Owner unstaged change: preserve these bytes\n');
writeDeliveryFile(f, 'owner-untracked.txt', 'Owner untracked file: preserve these bytes\n');
}
return Object.fromEntries(pythonProtected.map(relative => [relative, fs.readFileSync(path.join(f.projectRoot, relative))]));
}
export function assertPythonProtected(assert, root, original) {
for (const [relative, bytes] of Object.entries(original)) assert.ok(fs.readFileSync(path.join(root, relative)).equals(bytes), relative);
}
@@ -0,0 +1,24 @@
import fs from 'node:fs';
import path from 'node:path';
import readline from 'node:readline';
const mode = 'normal';
const schema = { type: 'object', additionalProperties: false, required: ['text'], properties: { text: { type: 'string', minLength: 1, maxLength: 15000 }, count: { type: 'integer', minimum: 1, maximum: 3 } } };
const send = value => process.stdout.write(JSON.stringify(value) + '\n');
for await (const line of readline.createInterface({ input: process.stdin })) {
let message; try { message = JSON.parse(line); } catch { continue; }
if (message.id === undefined) continue;
if (message.method === 'initialize') {
send({ jsonrpc: '2.0', id: message.id, result: { protocolVersion: '2025-03-26', capabilities: { tools: {} }, serverInfo: { name: 'owned-registered-mcp', version: mode === 'drift' ? '2.0' : '1.0' } } });
send({ jsonrpc: '2.0', id: 'server-model-request', method: 'sampling/createMessage', params: { messages: [] } });
} else if (message.method === 'tools/list') {
send({ jsonrpc: '2.0', id: message.id, result: { tools: [
{ name: 'echo', description: 'Owned fixture echo; annotations are not an authorization.', inputSchema: schema, annotations: { readOnlyHint: true } },
{ name: 'hold', description: 'Owned side effect followed by lost result.', inputSchema: schema },
{ name: 'unsupported', description: 'Unsupported schema must not become callable.', inputSchema: { type: 'object', properties: { n: { type: 'string', pattern: '^a$' } } } },
] } });
} else if (message.method === 'tools/call') {
if (message.params.name === 'hold') { fs.writeFileSync(path.join(process.cwd(), 'owned-call-dispatched.txt'), JSON.stringify(message.params)); continue; }
send({ jsonrpc: '2.0', id: message.id, result: { content: [{ type: 'text', text: message.params.arguments.text.repeat(message.params.arguments.count ?? 1) }],
structuredContent: { environmentNames: Object.keys(process.env).filter(name => /key|token|secret|credential/i.test(name)), cwd: process.cwd(), value: message.params.arguments } } });
} else send({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Unknown owned fixture method.' } });
}
@@ -0,0 +1,24 @@
const readline = require('node:readline');
const mode = process.argv[2] || 'echo';
const write = value => process.stdout.write(JSON.stringify(value) + '\n');
let serverRequest;
readline.createInterface({ input: process.stdin }).on('line', text => {
const message = JSON.parse(text);
if (message.id === 'server-operation') { write({ jsonrpc: '2.0', id: serverRequest, result: { denied: message.error?.code === -32601 } }); return; }
if (message.id === undefined) return;
if (mode === 'hang') return;
if (mode === 'invalid') { process.stdout.write('invalid protocol frame\n'); return; }
if (mode === 'oversized') { process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: 'x'.repeat(20000) }) + '\n'); return; }
if (message.method === 'initialize') {
write({ jsonrpc: '2.0', id: message.id, result: { protocolVersion: '2025-03-26', serverInfo: { name: 'rea', version: '6.8.0' },
fixtureCanaryForwarded: !!process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY } }); return;
}
if (message.method === 'tools/list') { write({ jsonrpc: '2.0', id: message.id, result: { tools: [] } }); return; }
if (mode === 'server-request') {
serverRequest = message.id; write({ jsonrpc: '2.0', id: 'server-operation', method: 'sampling/createMessage', params: { messages: [] } }); return;
}
const bytes = Buffer.from(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: { text: '原字节🙂', received: message.params } }) + '\n');
if (mode === 'exit-result') { process.stdout.write(bytes, () => process.exit(0)); return; }
const middle = bytes.indexOf(Buffer.from('🙂')) + 1;
process.stdout.write(bytes.subarray(0, middle)); setTimeout(() => process.stdout.write(bytes.subarray(middle)), 5);
});
@@ -0,0 +1,33 @@
import http from 'node:http';
import assert from 'node:assert/strict';
import { requestHistory } from '../../src/shared/contracts/model-request.mjs';
// An owned wire fixture only: the original engineering algorithm receives the
// same canonical history. Runtime/Harness/checks/Git still execute for real.
export async function responsesEngineeringHttp(t, algorithm) {
const requests = [], errors = [], sockets = new Set();
const server = http.createServer(async (request, response) => {
try {
const parts = []; for await (const part of request) parts.push(part);
const wire = JSON.parse(Buffer.concat(parts)); assert.equal(request.url, '/v1/responses'); assert.equal(wire.stream, true);
requests.push(wire);
const input = { ...wire, messages: requestHistory(wire) }, result = await algorithm('/v1/chat/completions', input);
const message = result.choices[0].message;
const output = [...(message.content ? [{ id: 'msg_' + requests.length, type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: message.content }] }] : []),
...(message.tool_calls ?? []).map((call, index) => ({ type: 'function_call', id: 'fc_' + requests.length + '_' + index, call_id: call.id, name: call.function.name, arguments: call.function.arguments, status: 'completed' }))];
const final = { id: 'resp_' + requests.length, status: 'completed', output, usage: { input_tokens: result.usage.prompt_tokens, output_tokens: result.usage.completion_tokens, total_tokens: result.usage.total_tokens } };
response.writeHead(200, { 'Content-Type': 'text/event-stream' }); let sequence = 0;
const send = (type, data) => response.write('data: ' + JSON.stringify({ type, sequence_number: sequence++, ...data }) + '\n\n');
send('response.created', { response: { id: final.id } });
for (const item of output) {
if (item.type === 'message') send('response.output_text.delta', { delta: item.content[0].text });
else { send('response.output_item.added', { item: { ...item, arguments: '', status: 'in_progress' } }); send('response.function_call_arguments.delta', { item_id: item.id, delta: item.arguments }); send('response.function_call_arguments.done', { item_id: item.id, arguments: item.arguments }); }
}
send('response.completed', { response: final }); response.end();
} catch (error) { errors.push({message:error.message,stack:error.stack}); response.destroy(); }
});
server.on('connection', socket => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { for (const socket of sockets) socket.destroy(); server.close(resolve); }));
return { baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1', requests, errors };
}
@@ -0,0 +1,17 @@
import fs from 'node:fs';
import { Store } from '../../src/server/store.mjs';
import { TaskPlanManager } from '../../src/core/tasks/task-plan-manager.mjs';
import { TaskCheckpointManager } from '../../src/core/tasks/task-checkpoint-manager.mjs';
const input = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
const store = new Store(input.data), session = store.session(input.sessionId);
const manager = new TaskCheckpointManager(store, { taskPlans: new TaskPlanManager(store) });
const put = store.putRecord.bind(store);
store.putRecord = (kind, ...args) => {
if (kind === 'task_checkpoint' && input.point === 'before') process.exit(71);
const result = put(kind, ...args);
if (kind === 'task_checkpoint' && input.point === 'after') process.exit(71);
return result;
};
await manager.create(session);
throw new Error('Declared checkpoint process exit was not reached.');
@@ -0,0 +1,18 @@
import fs from 'node:fs';
import { Store } from '../../src/server/store.mjs';
import { TaskPlanManager } from '../../src/core/tasks/task-plan-manager.mjs';
import { readingStep } from './task-plan-fixture.mjs';
const [inputFile] = process.argv.slice(2), input = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
const store = new Store(input.data), session = store.session(input.sessionId), manager = new TaskPlanManager(store);
const put = store.putRecord.bind(store);
store.putRecord = (kind, ...args) => {
const selected = kind === (input.operation === 'plan' ? 'task_plan' : 'task_proof');
if (selected && input.point === 'before') process.exit(71);
const result = put(kind, ...args);
if (selected && input.point === 'after') process.exit(71);
return result;
};
if (input.operation === 'plan') await manager.plan(session, { expected_revision: 0, steps: [readingStep()] });
else await manager.verify(session, { plan_id: input.planId, expected_revision: 1, step_key: 'read', evidence_id: input.evidenceId });
throw new Error('The declared process exit point was not reached.');
@@ -0,0 +1,21 @@
import { randomUUID } from 'node:crypto';
import { call } from '../helpers.mjs';
import { readText } from '../../src/server/files.mjs';
export const readingStep = (key = 'read', depends_on = []) => ({ key, title: '读取实际工程', criterion: '实际读取本工程文件并保留原结果', kind: 'analysis', tools: ['read_file'], depends_on });
export const checksStep = (key = 'check', depends_on = []) => ({ key, title: '检查工程', criterion: '所选测试与构建实际通过', kind: 'checks', checks: ['test', 'build'], depends_on });
export function beginPlanTask(f, goal = '检查工程并按真实证据交付') {
const run = f.store.beginRun(f.session, goal, false, f.store.config);
f.session.status = 'paused'; f.store.finishRun(f.session, run, 'plan_ready'); return run;
}
export function completeRead(f, relative = 'README.md') {
const run = f.store.core.run.get(f.session.runId), invocation = call('plan-read-' + randomUUID(), 'read_file', { path: relative });
f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [invocation] });
const event = f.store.event(f.session, { kind: 'tool', toolCallId: invocation.id, tool: 'read_file', phase: 'queued', label: '实际文件读取' });
const record = f.store.beginInvocation(f.session, invocation, run, event);
f.store.invocationState(f.session, event, 'running');
const result = { ...readText(f.project, relative), tool_invocation_id: record.id };
event.phase = 'completed'; f.store.finishInvocation(f.session, event, result);
f.session.providerMessages.push({ role: 'tool', tool_call_id: invocation.id, content: JSON.stringify(result) }); f.store.save(f.session);
return record;
}
@@ -0,0 +1,19 @@
import { randomUUID } from 'node:crypto';
import { Store } from '../../src/server/store.mjs';
import { Runtime } from '../../src/server/runtime.mjs';
import { Provider } from '../../src/server/provider.mjs';
const [data, groupId] = process.argv.slice(2), store = new Store(data), provider = new Provider(), runtime = new Runtime(store, provider);
provider.complete = async (config, messages, project) => {
const session = [...store.sessions.values()].find(value => value.projectId === project.id);
const plan = [...store.core.check_plan.values()].find(value => value.taskId === session.taskId);
return { message: { role: 'assistant', tool_calls: [{ id: randomUUID(), type: 'function', function: { name: 'run_checks', arguments: JSON.stringify({ plan_id: plan.id }) } }] }, usage: { total_tokens: 123 } };
};
const save = store.save;
store.save = function(session, event) { const result = save.call(this, session, event); if (event?.type === 'worker.check_process_owned') {
const info = [...store.core.check_job.values()].find(job => job.id === event.detail.jobId)?.currentCheck?.process;
process.send?.({ kind: 'owned-check', jobId: event.detail.jobId, process: info, descendants: info?.descendants?.length ?? 0 });
} return result; };
const group = store.core.collaboration.get(groupId), parent = store.session(group.sessionId);
const started = runtime.workerManager().start(parent, group.id, group.fingerprint);
const timer = setInterval(() => { for (const child of group.children) for (const approval of store.session(child.sessionId).approvals.filter(value => value.status === 'pending')) runtime.decide(child.sessionId, approval.id, true); }, 20);
await started.promise; clearInterval(timer); store.close();
@@ -0,0 +1,24 @@
import http from 'node:http';
import { randomUUID } from 'node:crypto';
import { createRequestHandler, createSessionPublisher } from '../../src/server/app.mjs';
import { prepareWorkerFixture } from './workers-project.mjs';
import { workspace } from '../helpers.mjs';
export async function workerHttp(f, options = {}) {
const context = prepareWorkerFixture(f, options);
f.store.saveConfig({ mode: 'compatible', model: 'owned-worker-model', baseUrl: 'http://127.0.0.1:1/v1' });
context.provider.setKey('self-owned-local-worker-fixture');
const token = randomUUID(), clients = new Set(), server = http.createServer(), started = Date.now();
f.store.on('update', createSessionPublisher(clients));
server.on('request', createRequestHandler({ appDirectory: workspace, server, store: f.store, provider: context.provider, runtime: context.runtime,
token, clients, started, getPort: () => server.address().port, exampleProject: f.project.id, shutdown: () => {} }));
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const url = 'http://127.0.0.1:' + server.address().port;
f.onCleanup(async () => { await context.runtime.stopAll(); for (const response of clients) response.destroy(); server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
const raw = async (route, input, supplied = token) => {
const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': supplied, Connection: 'close' }, ...(input === undefined ? {} : { body: JSON.stringify(input) }) });
return { status: response.status, value: await response.json() };
};
const api = async (route, input) => { const result = await raw(route, input); if (result.status >= 400) throw Object.assign(new Error(result.value.error ?? JSON.stringify(result.value)), { status: result.status }); return result.value; };
return { ...context, url, token, raw, api };
}
@@ -0,0 +1,66 @@
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { Provider } from '../../src/server/provider.mjs';
import { Runtime } from '../../src/server/runtime.mjs';
import { createDeliveryProject, writeDeliveryFile, deliveryGit } from './delivery-project.mjs';
import { call, until } from '../helpers.mjs';
export const workerDefinition = (paths = ['src/a.mjs', 'src/b.mjs']) => ({ goal: '交付两个独立改进并核对父工程',
children: paths.map((relative, index) => ({ label: 'worker-' + (index + 1), goal: '实现子任务' + (index + 1), paths: [relative] })) });
export function prepareWorkerFixture(f, { ownerChanges = true, respond = null, broken = false } = {}) {
createDeliveryProject(f, { broken, ownerChanges });
const provider = new Provider(), requests = [];
provider.complete = async (config, messages, project, signal, progress, planned, tools) => {
requests.push({ config, messages, projectId: project.id, tools: tools.map(value => value.function.name) });
if (respond) return respond({ config, messages, project, signal, progress, planned, tools });
const session = [...f.store.sessions.values()].find(value => value.projectId === project.id), task = f.store.core.task.get(session.taskId);
const worker = [...f.store.core.worker.values()].find(value => value.taskId === task.id);
const plan = [...f.store.core.check_plan.values()].filter(value => value.taskId === task.id).at(-1);
const job = [...f.store.core.check_job.values()].filter(value => value.taskId === task.id).at(-1);
let calls = [];
if (!session.changes.some(change => change.taskId === task.id)) calls = [call(randomUUID(), 'propose_file', {
path: worker.paths[0], content: 'export const value = ' + (worker.label.endsWith('1') ? '1' : '2') + ';\n', reason: '自有夹具中的独立修改' })];
else if (session.changes.some(change => change.status === 'pending')) calls = [];
else if (!plan) calls = [call(randomUUID(), 'plan_checks', { names: ['test', 'build'] })];
else if (!job) calls = [call(randomUUID(), 'run_checks', { plan_id: plan.id })];
return { message: { role: 'assistant', content: calls.length ? '' : '夹具声称完成,宿主仍需核验', ...(calls.length ? { tool_calls: calls } : {}) }, usage: { total_tokens: 168, prompt_tokens: 123, completion_tokens: 45 } };
};
const runtime = new Runtime(f.store, provider), manager = runtime.workerManager();
f.onCleanup(() => runtime.stopAll());
return { runtime, manager, provider, requests };
}
export async function applyWorkerProposals(f, runtime, group) {
for (const child of group.children) {
const session = f.store.session(child.sessionId), ids = session.changes.filter(change => change.status === 'pending').map(change => change.id);
if (!ids.length) continue;
const set = await runtime.changeSetManager().prepare(session, ids);
await (await runtime.changeSetManager().start(session, set.id, set.fingerprint)).promise;
if (set.status !== 'completed') throw new Error(set.summary);
}
}
export async function approveWorkerChecks(f, runtime, group, started) {
let done = false, error;
started.promise.then(() => { done = true; }, failure => { error = failure; done = true; });
while (!done) {
for (const child of group.children) {
const session = f.store.session(child.sessionId);
for (const approval of session.approvals.filter(value => value.status === 'pending')) runtime.decide(session.id, approval.id, true);
}
await new Promise(resolve => setTimeout(resolve, 20));
}
if (error) throw error;
return started.promise;
}
export const ownerBytes = f => ({ head: deliveryGit(f, ['rev-parse', 'HEAD']).trim(), index: fs.readFileSync(path.join(f.projectRoot, '.git/index')).toString('base64'),
files: Object.fromEntries(['owner-staged.txt', 'owner-unstaged.txt', 'owner-untracked.txt'].filter(name => fs.existsSync(path.join(f.projectRoot, name))).map(name => [name, fs.readFileSync(path.join(f.projectRoot, name)).toString('base64')])) });
export async function completedWorkers(f, options = {}) {
const context = prepareWorkerFixture(f, options), original = ownerBytes(f);
const group = await context.manager.plan(f.session, workerDefinition(options.paths));
await (await context.manager.prepare(f.session, group.id, group.fingerprint)).promise;
if (group.status !== 'ready') throw new Error(group.summary);
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));
return { ...context, group, original };
}
@@ -0,0 +1,137 @@
{
"scope": "Frozen T goal, first three original assistant turns and original unchanged project inputs. Offline fixture only; no model implementation.",
"goal": "修复这个Node费用报表工程的amount、summary、report三个模块。金额用精确分单位处理,接受测试约定的十进制字符串/有限数字、退款、0及空白;拒绝格式错误、超精度和不安全金额。按独立币种规范化/排序汇总并保留原数组,计数准确、安全求和,报表两位小数含负分金额。以原九项测试为完整契约。先inspect_project,再plan_checks选择[\"test\",\"build\"]并run_checks保留真实基线失败,读取原测试和相关源码。只允许修改src/amount.mjs、src/summary.mjs、src/report.mjs;不改测试、配置、README、构建脚本、owner文件或原暂存,不使用run_command,不直接提交Git。propose_file/propose_patch准备完整三个模块后,用prepare_change_set一次封存全部三文件,等待审查应用。应用后继续同一个Task,重新plan_checks/run_checks,通过原9项测试和build后,用plan_delivery为三个文件准备本地Git预览并等待确认。模型必须自己实现,禁止用口头完成替代检查。预算最多8次模型请求、90000tokens,输出2048,上下文16K,请合并独立工具调用并避免重复观察。",
"replies": [
{
"role": "assistant",
"content": "我会先读取项目识别结果并按你指定的顺序封存 `test`、`build` 基线计划;随后读取九项测试及三个目标模块,依据实际契约准备完整改动提案。",
"tool_calls": [
{
"id": "call_5eb7405a2e6a4fb2bd2e2acf4d775dcb",
"type": "function",
"function": {
"name": "inspect_project",
"arguments": "{}"
}
}
]
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_56fb7fe30f7a48c0a466eedca7cb1c33",
"type": "function",
"function": {
"name": "plan_checks",
"arguments": "{\"names\":[\"test\",\"build\"]}"
}
}
]
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_12f66bde19464121a617329f20484c0d",
"type": "function",
"function": {
"name": "run_checks",
"arguments": "{\"plan_id\": \"1c46ffc1-3b24-4fb8-b207-3305ccd14fd1\"}"
}
},
{
"id": "call_40d0588adc6b4abb8352fcd04c9abfbb",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\": \"test/expense.test.mjs\"}"
}
},
{
"id": "call_7ca0aaf713a34fb6b06af412a0806774",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\": \"src/amount.mjs\"}"
}
},
{
"id": "call_bba7c0bbdc874767a61a735efda0f601",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\": \"src/summary.mjs\"}"
}
},
{
"id": "call_dbec88442f9f43469ad0cef99c0534f6",
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\": \"src/report.mjs\"}"
}
}
]
}
],
"files": [
{
"path": ".gitignore",
"content": "dist/\n.awb-*\nnode_modules/\n",
"sha256": "cab0bc87cad1b1214d71b023eee742986044093ff4b4331cd6a51098af6bdce6"
},
{
"path": "owner-staged.txt",
"content": "Owner working copy differs from staging\n",
"sha256": "56b0dda452eaf9aead80f0e4ad971d7befa3cf152c3553923f8fde46e1fd336a"
},
{
"path": "owner-unstaged.txt",
"content": "Owner unstaged change\n",
"sha256": "468bfb2956a4ac040508fc5ffd915cf8057fb7420e22c41a3cc2d094785f0d3e"
},
{
"path": "owner-untracked.txt",
"content": "Owner untracked change\n",
"sha256": "e32ab29a12ed13d72c2ed603b2360e1b4e35afb0fe4280db2e523ad2c6c5bc8d"
},
{
"path": "package.json",
"content": "{\n \"name\": \"owned-live-expense-report\",\n \"private\": true,\n \"type\": \"module\",\n \"scripts\": {\n \"test\": \"node --test test/expense.test.mjs\",\n \"build\": \"node scripts/build.mjs\"\n }\n}\n",
"sha256": "6ad63026b2fe9de7513a4f6704c372a86108ee3d018a1f04883dc0a409ea4c24"
},
{
"path": "README.md",
"content": "# Expense report acceptance\nRepair the three source modules using the immutable original nine tests; do not edit checks or owner files.\n",
"sha256": "5e35a4dace9537e7c4cc815ce7054f73c171f053f1cccb9ff141f49ecb3c013e"
},
{
"path": "scripts/build.mjs",
"content": "import { spawnSync } from 'node:child_process';\nimport { readFileSync } from 'node:fs';\nfor (const file of ['src/amount.mjs','src/summary.mjs','src/report.mjs']) {\n const result=spawnSync(process.execPath,['--check',file],{encoding:'utf8'});\n if(result.status!==0) { process.stderr.write(result.stderr);process.exit(result.status??1); }\n if(!readFileSync(file,'utf8').trim()) throw new Error('Empty business module');\n}\nconsole.log('All three business modules compile.');\n",
"sha256": "7279bc7c707c0f3ccbae63b14f0abed248ad3755be14f9e37128124ae9267526"
},
{
"path": "src/amount.mjs",
"content": "export function amountToCents(value) { return 0; }\n",
"sha256": "84cb647328ec52dad27d288f3a6127ff3e1ad87274ae2d227ad44daa49cbb2ed"
},
{
"path": "src/report.mjs",
"content": "export function formatReport(groups) { return ''; }\n",
"sha256": "ba0a014e6e9df710d66e255a3a6f970f4e1c8af48accc81f33627029dcded7c0"
},
{
"path": "src/summary.mjs",
"content": "import { amountToCents } from './amount.mjs';\nexport function summarize(rows) { return []; }\n",
"sha256": "48829850c11ca0cd0ab5e86bbac69a0c5f90297292168c1ba6dc338e2eae6ee7"
},
{
"path": "test/expense.test.mjs",
"content": "import test from 'node:test';\nimport assert from 'node:assert/strict';\nimport { amountToCents } from '../src/amount.mjs';\nimport { summarize } from '../src/summary.mjs';\nimport { formatReport } from '../src/report.mjs';\ntest('decimal strings become exact cents including zero and refunds',()=>{\n for(const [input,expected] of [['0',0],[' 12.34 ',1234],['+2.5',250],['-1.05',-105],['-0.00',0],['0009.10',910]]) assert.equal(amountToCents(input),expected);\n});\ntest('finite decimal numbers use the same cents contract',()=>{\n for(const [input,expected] of [[0,0],[1.01,101],[2.5,250],[-4.32,-432]]) assert.equal(amountToCents(input),expected);\n});\ntest('malformed overprecision nonfinite and unsafe amounts are rejected',()=>{\n for(const input of ['',null,undefined,true,{},'1e2','1,000','.50','1.234',1.234,NaN,Infinity,'900719925474099.99']) assert.throws(()=>amountToCents(input),TypeError,String(input));\n});\ntest('independent currencies are sorted and summed without floating point loss',()=>{\n assert.deepEqual(summarize([{currency:'usd',amount:'0.10'},{currency:' CNY ',amount:3},{currency:'USD',amount:'0.20'}]),[\n {currency:'CNY',cents:300,count:1},{currency:'USD',cents:30,count:2}]);\n});\ntest('refunds cancel totals while counting every original record',()=>{\n assert.deepEqual(summarize([{currency:'eur',amount:'1.20'},{currency:'EUR',amount:'-1.20'}]),[{currency:'EUR',cents:0,count:2}]);\n});\ntest('summarizing retains caller rows and handles an empty list',()=>{\n const rows=Object.freeze([Object.freeze({currency:'usd',amount:'2.00'})]);\n assert.deepEqual(summarize(rows),[{currency:'USD',cents:200,count:1}]);assert.deepEqual(summarize([]),[]);\n});\ntest('invalid rows currency and unsafe aggregate totals are rejected',()=>{\n for(const rows of [null,{},[null],[{currency:'US',amount:'1'}],[{currency:'U1D',amount:'1'}],[{currency:'USD',amount:'1.234'}],\n [{currency:'USD',amount:'90071992547409.91'},{currency:'USD',amount:'0.01'}]]) assert.throws(()=>summarize(rows),TypeError);\n});\ntest('reports use two decimals and render negative subunit amounts',()=>{\n assert.equal(formatReport([{currency:'CNY',cents:300,count:2},{currency:'USD',cents:-105,count:1}]),'CNY 3.00 (2)\\nUSD -1.05 (1)');\n assert.equal(formatReport([{currency:'EUR',cents:-5,count:1}]),'EUR -0.05 (1)');assert.equal(formatReport([]),'');\n});\ntest('complete expense flow preserves exact decimal sums',()=>{\n assert.equal(formatReport(summarize([{currency:'usd',amount:'0.29'},{currency:'USD',amount:'-0.30'},{currency:'CNY',amount:'10.50'}])),\n 'CNY 10.50 (1)\\nUSD -0.01 (2)');\n});\n",
"sha256": "0bcda551dbaeafad9a6e3df8024492ab67d7c09956f4676c393b78249ab44839"
}
]
}
@@ -0,0 +1,14 @@
import { Store } from '../../src/server/store.mjs';
import { WorkspaceManager } from '../../src/core/workspaces/git-workspaces.mjs';
const [data, sessionId, workspaceId, cut] = process.argv.slice(2), store = new Store(data);
const manager = new WorkspaceManager(store, { initialize: false, faultInjector: (phase, record) => {
if (phase !== cut) return;
process.stdout.write(JSON.stringify({ phase, workspaceId: record.id, root: record.root, branch: record.branch, gitPid: record.gitPid }) + '\n');
process.exit(73);
} });
const record = store.owned('workspace', workspaceId, sessionId);
await (await manager.start(store.session(sessionId), record.id, record.fingerprint)).promise;
process.stderr.write('Workspace did not reach crash cut: ' + cut + ' / ' + record.status + ' / ' + record.summary
+ '\nGit result: ' + JSON.stringify(record.gitResult ?? null) + '\nObservation: ' + JSON.stringify(record.observation?.problems ?? []) + '\n');
await manager.stopAll(); store.close(); process.exit(74);
@@ -0,0 +1,15 @@
import fs from 'node:fs';
import path from 'node:path';
import { deliveryGit } from './delivery-project.mjs';
export function captureWorkspaceSource(f) {
const index = deliveryGit(f, ['rev-parse', '--path-format=absolute', '--git-path', 'index']).trim();
return { head: deliveryGit(f, ['rev-parse', 'HEAD']), branch: deliveryGit(f, ['symbolic-ref', 'HEAD']),
refs: deliveryGit(f, ['for-each-ref', '--format=%(refname) %(objectname)']).split('\n').filter(Boolean),
index: fs.readFileSync(index), files: ['owner-staged.txt', 'owner-unstaged.txt', 'owner-untracked.txt', 'src/add.mjs']
.map(relative => [relative, fs.existsSync(path.join(f.projectRoot, relative)) ? fs.readFileSync(path.join(f.projectRoot, relative)) : null]) };
}
export function assertWorkspaceSource(assert, f, original, branch) {
const current = captureWorkspaceSource(f);
assert.deepEqual({ ...current, refs: current.refs.filter(value => !value.startsWith('refs/heads/' + branch + ' ')) }, original);
}
@@ -0,0 +1,117 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { Store } from '../src/server/store.mjs';
export const workspace = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const fixtureRoot = path.join(workspace, '.qa');
const disposalLists = new WeakMap();
export function fixture(t) {
fs.mkdirSync(fixtureRoot, { recursive: true });
const root = fs.mkdtempSync(path.join(fixtureRoot, 'test-'));
const projectRoot = path.join(root, 'project');
fs.mkdirSync(projectRoot);
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Fixture project\n\nA local test project with TODO work.\n');
const data = path.join(root, 'data');
const store = new Store(data);
const project = store.addProject(projectRoot);
const session = store.createSession(project.id);
const disposers = [() => store.close()];
disposalLists.set(t, disposers);
t.after(async () => {
for (const dispose of disposers.reverse()) await dispose();
const resolved = path.resolve(root);
assert.ok(resolved.startsWith(path.resolve(fixtureRoot) + path.sep) && path.basename(resolved).startsWith('test-'));
// Yield while Windows releases handles from owned child processes. A
// persistent lock still fails teardown; resource disposal must run first.
await fs.promises.rm(resolved, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
});
return { root, projectRoot, data, store, project, session, onCleanup: dispose => disposers.push(dispose) };
}
export async function until(predicate, timeout = 8000) {
const deadline = Date.now() + timeout;
while (!(await predicate())) {
if (Date.now() > deadline) throw new Error('Timed out waiting for test state.');
await new Promise(resolve => setTimeout(resolve, 20));
}
}
export async function compatibleFixture(t, respond) {
const requests = [];
const server = http.createServer(async (request, response) => {
try {
const chunks = [];
for await (const chunk of request) chunks.push(chunk);
const input = chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : null;
requests.push({ url: request.url, headers: request.headers, input });
const result = await respond(request.url, input, requests);
response.writeHead(result.status ?? 200, { 'Content-Type': 'application/json', ...result.headers });
response.end(JSON.stringify(result.body ?? result));
} catch (error) {
response.writeHead(500, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ error: { message: error.message } }));
}
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
return { baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1', requests };
}
export const call = (id, name, args) => ({ id, type: 'function', function: { name, arguments: typeof args === 'string' ? args : JSON.stringify(args) } });
export const completion = (calls = [], content = '') => ({ choices: [{ message: { role: 'assistant', content, ...(calls.length ? { tool_calls: calls } : {}) }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage: { prompt_tokens: 123, completion_tokens: 45 } });
export function assertToolPairs(messages) {
for (let index = 0; index < messages.length; index++) {
const message = messages[index];
assert.notEqual(message.role, 'tool', 'Orphan tool message');
if (!message.tool_calls?.length) continue;
const expected = new Set(message.tool_calls.map(call => call.id));
for (let count = 0; count < message.tool_calls.length; count++) {
const result = messages[++index];
assert.equal(result?.role, 'tool');
assert.ok(expected.delete(result.tool_call_id), 'Tool result ID mismatch or duplicate');
}
assert.equal(expected.size, 0);
}
}
export async function launchWorkbench(t, data, extraEnv = {}, { appRoot = workspace, nodePath = process.execPath } = {}) {
const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: appRoot, AGENT_WORKBENCH_DATA_DIR: data, AGENT_WORKBENCH_PORT: '0', ...extraEnv };
delete env.AGENT_WORKBENCH_API_KEY;
const child = spawn(nodePath, [path.join(appRoot, 'dist-server', 'server.mjs')], { cwd: appRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = '', url = '';
child.stdout.on('data', bytes => {
output += bytes.toString('utf8');
for (const line of output.split('\n')) { try { const parsed = JSON.parse(line); if (parsed.ready) url = parsed.url; } catch {} }
});
child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
await until(() => url || child.exitCode !== null);
assert.ok(url, 'Workbench launch failed: ' + errors);
const bootstrap = await (await fetch(url + '/api/bootstrap', { headers: { Connection: 'close' } })).json();
const api = async (route, input) => {
const method = input === undefined ? 'GET' : 'POST';
let response;
try {
response = await fetch(url + '/api' + route, { method,
headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' },
...(input === undefined ? {} : { body: JSON.stringify(input) }) });
} catch (cause) { throw Object.assign(new Error('QA API transport failed before response headers: ' + method + ' ' + route, { cause }), { stage: 'response_headers', method, route }); }
let result;
try { result = await response.json(); }
catch (cause) { throw Object.assign(new Error('QA API transport failed while reading response: ' + method + ' ' + route, { cause }), { stage: 'response_body', method, route }); }
if (!response.ok) throw Object.assign(new Error(result.error), { status: response.status });
return result;
};
const close = async () => {
if (child.exitCode !== null || child.signalCode !== null) return;
try { await api('/shutdown', {}); await until(() => child.exitCode !== null || child.signalCode !== null, 5000); }
finally { if (child.exitCode === null && child.signalCode === null) child.kill(); }
};
if (disposalLists.has(t)) disposalLists.get(t).push(close); else t.after(close);
return { child, url, bootstrap, api, close };
}
@@ -0,0 +1,20 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, workspace } from './helpers.mjs';
import { Store } from '../src/server/store.mjs';
import { initializeExampleProject } from '../src/core/initial-example-project.mjs';
import { snapshotPersistentData } from '../src/core/desktop/install-manager.mjs';
test('a starter is copied into user data once; restarting from another program version preserves existing projects and editable bytes', t => {
const f = fixture(t), store = new Store(path.join(f.root, 'empty-data')); f.onCleanup(() => store.close());
const source = fs.readFileSync(path.join(workspace, 'examples/starter-project/README.md'));
const example = initializeExampleProject(store, workspace); assert.ok(example.root.startsWith(store.directory + path.sep)); assert.equal(store.projects.length, 1);
const file = path.join(example.root, 'README.md'); fs.appendFileSync(file, '\nOwned user change\n'); const edited = fs.readFileSync(file), snapshot = snapshotPersistentData(store.directory);
assert.equal(initializeExampleProject(store, path.join(f.root, 'different-version')).id, example.id); assert.deepEqual(snapshotPersistentData(store.directory), snapshot); assert.deepEqual(fs.readFileSync(file), edited);
assert.deepEqual(fs.readFileSync(path.join(workspace, 'examples/starter-project/README.md')), source);
});
test('startup does not insert a demo or rewrite/reorder an existing user project registry', t => {
const f = fixture(t), before = snapshotPersistentData(f.data); assert.equal(initializeExampleProject(f.store, workspace), null); assert.deepEqual(snapshotPersistentData(f.data), before);
});
@@ -0,0 +1,152 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { createHash } from 'node:crypto';
import { PNG } from 'pngjs';
import jpeg from 'jpeg-js';
import { fixture,compatibleFixture,completion,until,assertToolPairs } from './helpers.mjs';
import { Store } from '../src/server/store.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { createRequestHandler } from '../src/server/app.mjs';
import { InputAttachments,validateImage,IMAGE_LIMITS } from '../src/core/context/input-attachments.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
import { completionBody,requestMetrics,requestImages } from '../src/shared/contracts/model-request.mjs';
export function pngInput(width=96,height=64) {
const png=new PNG({width,height});
for(let p=0;p<png.data.length;p+=4)png.data.set([34,110,180,255],p);
return {name:'工程截图.png',mediaType:'image/png',base64:PNG.sync.write(png).toString('base64')};
}
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
const withBytes=(bytes,mediaType='image/png')=>({name:'image.'+(mediaType==='image/png'?'png':'jpg'),mediaType,base64:bytes.toString('base64')});
test('PNG bytes are fully decoded, metadata-free and bounded; JPEG EXIF orientation is applied to the sent PNG',()=>{
const value=validateImage(pngInput(2048,512));
assert.equal(value.sentWidth,1024);assert.equal(value.sentHeight,256);assert.ok(value.sent.length<=IMAGE_LIMITS.sentBytes);
const rgba=Buffer.alloc(24*48*4);for(let p=0;p<rgba.length;p+=4)rgba.set([255,0,0,255],p);
const original=jpeg.encode({width:24,height:48,data:rgba},90).data;
const exif=Buffer.from('45786966000049492a0008000000010012010300010000000600000000000000','hex');
const segment=Buffer.alloc(4);segment[0]=255;segment[1]=225;segment.writeUInt16BE(exif.length+2,2);
const rotated=validateImage(withBytes(Buffer.concat([original.subarray(0,2),segment,exif,original.subarray(2)]),'image/jpeg'));
assert.equal(rotated.orientation,6);assert.equal(rotated.sentWidth,48);assert.equal(rotated.sentHeight,24);
assert.equal(rotated.sent.includes(Buffer.from('Exif')),false);assert.equal(rotated.width,24);assert.equal(rotated.height,48);
});
test('forged type, short headers, CRC damage, incomplete chunks, trailing payloads, animation and dimension bombs reject',()=>{
const bytes=Buffer.from(pngInput().base64,'base64');
for(const length of [0,1,8,16,22,24,32,bytes.length-1])assert.throws(()=>validateImage(withBytes(bytes.subarray(0,length))));
assert.throws(()=>validateImage(withBytes(bytes,'image/jpeg')),/类型不符/);
const corrupt=Buffer.from(bytes);corrupt[29]^=1;assert.throws(()=>validateImage(withBytes(corrupt)),/解码/);
assert.throws(()=>validateImage(withBytes(Buffer.concat([bytes,Buffer.from('payload')]))),/尾部/);
const animation=Buffer.from(bytes);animation.write('acTL',37,'ascii');assert.throws(()=>validateImage(withBytes(animation)),/静态/);
const huge=Buffer.from(bytes);huge.writeUInt32BE(9000,16);assert.throws(()=>validateImage(withBytes(huge)),error=>error.status===413);
assert.throws(()=>validateImage({...pngInput(),name:'../secret.png'}),/文件名/);
assert.throws(()=>validateImage({...pngInput(),base64:'%%%'}),/编码/);
assert.throws(()=>validateImage(withBytes(Buffer.alloc(IMAGE_LIMITS.bytes+1))),error=>error.status===413);
});
test('draft image ownership, withdrawal, reload and atomic Task binding retain original SHA and original bytes',t=>{
const f=fixture(t),manager=new InputAttachments(f.store),input=pngInput(),row=manager.upload(f.session,input);
assert.equal(row.taskId,null);assert.equal(row.runId,null);assert.equal(row.sha256,sha(Buffer.from(input.base64,'base64')));
assert.deepEqual(manager.read(f.session,row.id,{original:true}).bytes,Buffer.from(input.base64,'base64'));
const other=f.store.createSession(f.project.id);assert.throws(()=>manager.read(other,row.id),/归属|所属|不存在|不属于/);
const selected=manager.validateSelection(f.session,[row.id]),run=f.store.beginRun(f.session,'Inspect screenshot',false,f.store.config,{inputAttachments:selected});
const boundId=manager.get(f.session,row.id).boundAttachmentId;
assert.equal(manager.get(f.session,row.id).status,'consumed');assert.equal(manager.get(f.session,boundId).taskId,run.taskId);assert.equal(manager.images(f.session,f.store.core.task.get(run.taskId)).length,1);
assert.throws(()=>manager.remove(f.session,row.id),/已发送/);
const draft=manager.upload(f.session,pngInput(64,64));manager.remove(f.session,draft.id);assert.throws(()=>manager.read(f.session,draft.id),error=>error.status===410);
f.store.close();const restored=new Store(f.data);f.onCleanup(()=>restored.close());const reloaded=new InputAttachments(restored),session=restored.session(f.session.id);
assert.equal(reloaded.images(session,restored.core.task.get(run.taskId))[0].sha256,row.sentSha256);
assert.equal(reloaded.list(session).length,1);
});
test('a failed session transaction restores draft status and cannot leave a Run consuming the attachment',t=>{
const f=fixture(t),manager=new InputAttachments(f.store),row=manager.upload(f.session,pngInput()),save=f.store.storage.save;
f.store.storage.save=()=>{throw new Error('owned transaction failure');};
assert.throws(()=>f.store.beginRun(f.session,'Inspect',false,f.store.config,{inputAttachments:manager.validateSelection(f.session,[row.id])}),/transaction failure/);
f.store.storage.save=save;assert.equal(manager.get(f.session,row.id).status,'draft');assert.equal(f.store.core.run.size,0);assert.equal(f.session.runId,undefined);
});
test('draft limits, duplicate selection, source changes and cross-Task association reject before dispatch',t=>{
const f=fixture(t),manager=new InputAttachments(f.store),row=manager.upload(f.session,pngInput());
assert.throws(()=>manager.validateSelection(f.session,[row.id,row.id]),/一张/);
const artifact=f.store.core.artifact.get(row.sentArtifactId),file=path.join(f.store.artifactStore.root,artifact.relative);
fs.appendFileSync(file,'tampered');assert.throws(()=>manager.validateSelection(f.session,[row.id]),/校验失败/);
for(let i=1;i<8;i++)manager.upload(f.session,pngInput(i+10,10));
assert.throws(()=>manager.upload(f.session,pngInput(20,20)),/已满/);
});
test('demo and legacy paths refuse image dispatch without creating a Task or consuming the draft',async t=>{
const f=fixture(t),provider=new Provider(),runtime=new Runtime(f.store,provider),row=runtime.inputAttachments().upload(f.session,pngInput());
assert.throws(()=>runtime.start(f.session.id,'Inspect',false,{attachmentIds:[row.id]}),/演示/);
assert.equal(f.store.core.run.size,0);assert.equal(runtime.inputAttachments().get(f.session,row.id).status,'draft');
await assert.rejects(()=>provider.complete({...f.store.config,inputImages:[{dataUrl:'not-sent'}]},[],null),/视觉能力/);
});
for(const protocol of ['chat-completions','responses'])test(protocol+' sends one owned image per actual request, pairs original tools and verifies exact sealed bytes',async t=>{
const f=fixture(t),server=await compatibleFixture(t,(_url,body,requests)=>{
assert.equal(requestImages(body).length,1);
if(protocol==='responses')return {body:{status:'completed',output:requests.length===1?[{type:'function_call',call_id:'image_files',name:'list_files',arguments:'{"path":"","depth":1}'}]
:[{type:'message',role:'assistant',content:[{type:'output_text',text:'截图与工程已核对'}]}],usage:{input_tokens:300,output_tokens:20,total_tokens:320}}};
return completion(requests.length===1?[{id:'image_files',type:'function',function:{name:'list_files',arguments:'{"path":"","depth":1}'}}]:[],requests.length===1?'':'截图与工程已核对');
});
const provider=new Provider(),hub=provider.modelHub(f.store),connection=hub.upsert({name:'Owned visual',model:'fixture',baseUrl:server.baseUrl,protocol,requiresKey:false,contextLimit:64000,capabilityDeclarations:{vision:'supported'}});
hub.select({type:'connection',id:connection.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
const row=runtime.inputAttachments().upload(f.session,pngInput());runtime.start(f.session.id,'只根据此图核对本工程',false,{attachmentIds:[row.id]});await until(()=>!runtime.isActive(f.session.id),15000);
assert.equal(f.session.status,'completed',JSON.stringify(f.session.messages));assert.equal(server.requests.length,2);assertToolPairs(f.session.providerMessages);
assert.equal(JSON.stringify(f.session.providerMessages).includes('data:image'),false);assert.deepEqual(f.session.messages.find(value=>value.role==='user').attachmentIds,f.store.core.task.get(f.session.taskId).attachmentIds);
for(const plan of f.store.core.context_plan.values()) {
assert.equal(plan.imageCount,1);assert.ok(plan.imageReservationTokens>=4096);assert.doesNotThrow(()=>verifyPlanEvidence(f.store,f.session,plan));
plan.inputImages[0].sha256='0'.repeat(64);assert.throws(()=>verifyPlanEvidence(f.store,f.session,plan),/图片/);plan.inputImages[0].sha256=row.sentSha256;
}
});
test('mixed pool excludes unknown vision before lease and sends zero HTTP to the incompatible member',async t=>{
const f=fixture(t),bad=await compatibleFixture(t,()=>{throw new Error('must not dispatch');}),good=await compatibleFixture(t,()=>completion([],'Done'));
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'Text',model:'text',baseUrl:bad.baseUrl,requiresKey:false,contextLimit:64000}),
b=hub.upsert({name:'Vision',model:'vision',baseUrl:good.baseUrl,requiresKey:false,contextLimit:64000,capabilityDeclarations:{vision:'supported'}});
const pool=hub.pool({name:'Mixed',members:[{connectionId:a.id},{connectionId:b.id}]});hub.select({type:'pool',id:pool.id});
const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());const row=runtime.inputAttachments().upload(f.session,pngInput());
runtime.start(f.session.id,'Inspect',false,{attachmentIds:[row.id]});await until(()=>!runtime.isActive(f.session.id));
assert.equal(f.session.status,'completed');assert.equal(bad.requests.length,0);assert.equal(good.requests.length,1);
const attempt=hub.view().attempts[0];assert.ok(attempt.requirements.includes('vision'));assert.ok(attempt.rejections.some(item=>item.connectionId===a.id&&item.capability==='vision'));
});
test('vision probe requires correct randomized pixel order and evidence expires when the connection revision changes',async t=>{
const f=fixture(t),server=await compatibleFixture(t,(_url,body)=>{
const png=PNG.sync.read(Buffer.from(requestImages(body)[0].split(',')[1],'base64'));
const names=[[230,0,0,'RED'],[0,180,0,'GREEN'],[0,0,230,'BLUE'],[240,220,0,'YELLOW']];
const result=[[10,10],[100,10],[10,100],[100,100]].map(([x,y])=>names.find(rgb=>rgb.slice(0,3).every((value,c)=>png.data[(y*128+x)*4+c]===value))[3]);
return completion([],result.join(','));
});
const provider=new Provider(),hub=provider.modelHub(f.store),connection=hub.upsert({name:'Probe',model:'owned',baseUrl:server.baseUrl,requiresKey:false});
await hub.probe(connection.id,'vision');assert.equal(hub.view().connections.find(value=>value.id===connection.id).capabilities.vision,'verified');
hub.upsert({...hub.connection(connection.id),name:'Changed'});assert.equal(hub.view().connections.find(value=>value.id===connection.id).capabilities.vision,'unknown');
const failed=await compatibleFixture(t,()=>completion([],'OK')),other=hub.upsert({name:'Blind',model:'owned',baseUrl:failed.baseUrl,requiresKey:false});
await assert.rejects(()=>hub.probe(other.id,'vision'),/未正确识别/);assert.equal(hub.view().connections.find(value=>value.id===other.id).capabilities.vision,'unknown');
});
test('HTTP upload/read/withdraw uses session authorization, returns byte SHA and refuses unknown ownership',async t=>{
const f=fixture(t),provider=new Provider(),runtime=new Runtime(f.store,provider),server=http.createServer();
server.on('request',createRequestHandler({appDirectory:process.cwd(),server,store:f.store,provider,runtime,token:'owned',clients:new Set(),started:Date.now(),getPort:()=>server.address().port}));
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));t.after(()=>new Promise(resolve=>{server.closeAllConnections();server.close(resolve);}));
const root='http://127.0.0.1:'+server.address().port,base='/api/sessions/'+f.session.id+'/input-attachments';
const request=(route,method='GET',body,token='owned')=>fetch(root+route,{method,headers:{'X-Workbench-Token':token,'Content-Type':'application/json'},...(body?{body:JSON.stringify(body)}:{})});
assert.equal((await request(base,'POST',pngInput(),'invalid')).status,403);
const uploaded=await request(base,'POST',pngInput());assert.equal(uploaded.status,201);const row=await uploaded.json();
const image=await request(base+'/'+row.id+'/image');assert.equal(image.headers.get('X-Content-SHA256'),row.sentSha256);assert.equal(sha(Buffer.from(await image.arrayBuffer())),row.sentSha256);
const other=f.store.createSession(f.project.id);assert.equal((await request('/api/sessions/'+other.id+'/input-attachments/'+row.id+'/image')).status,404);
assert.equal((await request(base+'/'+row.id,'DELETE',{})).status,200);assert.equal((await request(base+'/'+row.id+'/image')).status,410);
});
test('image accounting excludes Base64 as text but seals every original request byte and reserves image input separately',()=>{
const value=validateImage(pngInput()),image={id:'owned',sha256:sha(value.sent),width:value.sentWidth,height:value.sentHeight,dataUrl:'data:image/png;base64,'+value.sent.toString('base64')};
const config={protocol:'responses',model:'owned',maxOutputTokens:512,inputImages:[image]};
const body=completionBody(config,[{role:'user',content:'Inspect'}],[]),metrics=requestMetrics(body);
assert.equal(metrics.bytes,Buffer.byteLength(JSON.stringify(body)));assert.equal(metrics.sha256,sha(JSON.stringify(body)));assert.equal(metrics.imageCount,1);
assert.equal(metrics.imageEstimate,'local-reservation-not-provider-billing');assert.equal(metrics.imageReservationTokens,4096);
assert.ok(metrics.estimatedInputTokens>4096);assert.equal(requestImages(body).length,1);
});
@@ -0,0 +1,49 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { observedTaskModels } from '../scripts/live-model-observation.mjs';
const receipt=(attemptId,model='actual-claude',extra={})=>({attemptId,model,protocol:'chat-completions',connectionId:'owned-connection',
outcome:'completed',usageStatus:'reported',usage:{prompt_tokens:23,completion_tokens:7,total_tokens:30},...extra});
const state=(requests,runs)=>({config:{model:'planned-gpt'},execution:{task:{id:'owned-task',usage:{requests}},runs,run:runs.at(-1)}});
const run=(id,receipts,extra={})=>({id,taskId:'owned-task',consumption:receipts,usage:{requests:receipts.length},...extra});
test('actual model comes from owned consumption, excludes other Tasks and deduplicates the current Run',()=>{
const value=state(2,[run('first',[receipt('one')]),run('second',[receipt('two')]),run('foreign',[receipt('foreign','other')],{taskId:'other-task'})]);
value.execution.run=value.execution.runs[1];const original=structuredClone(value);
const report=observedTaskModels(value,'owned-task');
assert.equal(report.status,'observed');assert.equal(report.model,'actual-claude');assert.equal(report.observedAttempts,2);
assert.deepEqual(report.models,['actual-claude']);assert.equal(report.routes[0].requests,2);assert.deepEqual(value,original);
});
test('mixed routes retain each original model/protocol/outcome and unknown use is never reported as zero',()=>{
const value=state(2,[run('mixed',[receipt('one','actual-gpt',{outcome:'unknown',usageStatus:'unknown',usage:null}),
receipt('two','actual-claude',{protocol:'responses',connectionId:'second'})])]);
const report=observedTaskModels(value);
assert.equal(report.status,'mixed');assert.equal(report.model,null);assert.equal(report.routes.length,2);
assert.equal(report.attempts[0].usage,null);assert.equal(report.routes[0].unknownUsageRequests,1);
assert.equal(report.attempts[1].protocol,'responses');assert.deepEqual(report.attempts[1].usage,value.execution.run.consumption[1].usage);
});
test('zero dispatch, missing ledger, missing model and truncated Run history never borrow configured model identity',()=>{
assert.equal(observedTaskModels(state(0,[])).status,'not_observed');
assert.equal(observedTaskModels({}).status,'incomplete');
for(const value of [state(3,[run('old',[])]),state(1,[run('missing',[receipt('one',null)])]),state(2,[run('recent',[receipt('last')])])]){
const report=observedTaskModels(value);assert.equal(report.status,'incomplete');assert.equal(report.model,null);assert.ok(report.issues.length);
}
});
test('repeated attempt identities across Runs and contradictory usage stay explicit instead of inflating successes',()=>{
const report=observedTaskModels(state(2,[run('one',[receipt('same')]),run('two',[receipt('same','changed',{usage:null})])]));
assert.equal(report.status,'incomplete');assert.equal(report.observedAttempts,1);assert.equal(report.model,null);
assert.ok(report.issues.some(issue=>issue.kind==='conflicting_attempt'));assert.ok(report.issues.some(issue=>issue.kind==='request_coverage_gap'));
});
test('observed route never certifies returned identity and retains mismatches and original source',()=>{
const legacy=observedTaskModels(state(1,[run('old',[receipt('one')])]));
assert.equal(legacy.status,'observed');assert.equal(legacy.responseIdentity.allAccepted,false);
assert.equal(legacy.responseIdentity.counts.historical_unchecked,1);
const modelIdentity={requestedModel:'requested',reportedModels:['other'],status:'mismatch',complete:true,responseSource:{artifactId:'source',sha256:'a'.repeat(64)}};
const report=observedTaskModels(state(1,[run('new',[receipt('two','requested',{modelIdentity})])]));
assert.equal(report.status,'observed');assert.equal(report.model,'requested');assert.equal(report.responseIdentity.allAccepted,false);
assert.deepEqual(report.responseIdentity.reportedModels,['other']);assert.equal(report.attempts[0].modelIdentity.responseSource.artifactId,'source');
});
@@ -0,0 +1,98 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { fixture, launchWorkbench, until } from './helpers.mjs';
import { localDirectories } from '../src/server/local-directories.mjs';
import { LocalFilePreviews } from '../src/core/preview/local-file-preview.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
function html(f) {
fs.mkdirSync(path.join(f.projectRoot, 'pages'));
fs.mkdirSync(path.join(f.projectRoot, 'assets'));
fs.writeFileSync(path.join(f.projectRoot, 'pages', '页面.html'), '<!doctype html><html><script type="module" src="../assets/app.mjs"></script><body>Preview</body></html>');
fs.writeFileSync(path.join(f.projectRoot, 'assets', 'app.mjs'), 'document.body.dataset.ready="yes";');
fs.writeFileSync(path.join(f.projectRoot, '.env'), 'TEST_SECRET=never-served');
}
test('directory picker reads one level, preserves empty folders, skips credential and linked folders', async t => {
const f = fixture(t);
for (const name of ['empty', '中文 工程', '.ssh', '.git']) fs.mkdirSync(path.join(f.projectRoot, name));
fs.symlinkSync(f.root, path.join(f.projectRoot, 'external-link'), process.platform === 'win32' ? 'junction' : 'dir');
const result = await localDirectories(f.projectRoot);
assert.equal(result.current, fs.realpathSync.native(f.projectRoot));
assert.equal(result.parent, f.root);
assert.deepEqual(result.directories.map(item => item.name).sort(), ['empty', '中文 工程'].sort());
assert.ok(result.bookmarks.length);
await assert.rejects(localDirectories('relative-folder'));
await assert.rejects(localDirectories(path.join(f.root, 'missing')), /不存在/);
await assert.rejects(localDirectories(path.join(f.projectRoot, 'README.md')), /无法访问/);
});
test('HTML preview serves encoded names and relative modules on an isolated loopback origin; closing revokes it', async t => {
const f = fixture(t); html(f);
const manager = new LocalFilePreviews(f.store); f.onCleanup(() => manager.stopAll());
const result = await manager.start(f.project, 'pages/页面.html');
assert.equal(manager.access(result.url).projectId, f.project.id);
const response = await fetch(result.url);
assert.equal(response.status, 200); assert.match(await response.text(), /Preview/);
assert.match(response.headers.get('content-security-policy'), /sandbox allow-scripts/);
assert.equal(response.headers.get('access-control-allow-origin'), 'null');
const script = await fetch(new URL('../assets/app.mjs', result.url));
assert.equal(script.status, 200); assert.match(await script.text(), /dataset.ready/);
const wrong = new URL(result.url); wrong.pathname = '/invalid/pages/页面.html';
assert.equal(manager.access(wrong.href).allowed, false);
assert.equal((await fetch(wrong)).status, 404);
assert.equal((await fetch(result.url, { method: 'POST' })).status, 405);
await assert.rejects(manager.close('other-project', result.id), /不属于/);
await manager.close(f.project.id, result.id);
assert.equal(manager.access(result.url).allowed, false);
await assert.rejects(fetch(result.url));
});
test('HTML preview refuses credentials, hidden inventories, traversal, external junctions, hardlinks and unsupported file types', async t => {
const f = fixture(t); html(f);
fs.writeFileSync(path.join(f.root, 'outside.html'), '<h1>Outside project</h1>');
fs.symlinkSync(f.root, path.join(f.projectRoot, 'escape'), process.platform === 'win32' ? 'junction' : 'dir');
fs.linkSync(path.join(f.projectRoot, 'pages', '页面.html'), path.join(f.projectRoot, 'aliased.html'));
const manager = new LocalFilePreviews(f.store); f.onCleanup(() => manager.stopAll());
await assert.rejects(manager.start(f.project, 'aliased.html'), /硬链接/);
fs.unlinkSync(path.join(f.projectRoot, 'aliased.html'));
const result = await manager.start(f.project, 'pages/页面.html'), url = new URL(result.url), base = url.origin + url.pathname.split('/').slice(0, 2).join('/') + '/';
for (const relative of ['.env', '.git/config', 'README.md', 'escape/outside.html', '%2e%2e%2fproject/pages/页面.html', '%2eenv.json', 'pages/%00.html']) {
const response = await fetch(base + relative); assert.ok(response.status >= 400, relative);
}
const wrongHostStatus = await new Promise((resolve, reject) => {
http.get(result.url, { headers: { Host: 'evil.example' } }, response => { response.resume(); resolve(response.statusCode); }).on('error', reject);
});
assert.equal(wrongHostStatus, 421);
});
test('runtime shutdown closes local preview ports and pending starts cannot escape shutdown', async t => {
const f = fixture(t); html(f);
const runtime = new Runtime(f.store, new Provider());
const result = await runtime.localFilePreviews().start(f.project, 'pages/页面.html');
await runtime.stopAll(); assert.equal(runtime.filePreviews.active.size, 0);
await assert.rejects(fetch(result.url));
await assert.rejects(runtime.localFilePreviews().start(f.project, 'pages/页面.html'), /已关闭/);
});
test('idle file previews expire and release their loopback servers', async t => {
const f = fixture(t); html(f);
const manager = new LocalFilePreviews(f.store, { ttl: 60 }); f.onCleanup(() => manager.stopAll());
const result = await manager.start(f.project, 'pages/页面.html');
await until(() => !manager.access(result.url).allowed);
await assert.rejects(fetch(result.url));
});
test('directory and preview HTTP controls require workbench authentication; project pages cannot read bootstrap', async t => {
const f = fixture(t); html(f);
const server = await launchWorkbench(t, f.data);
assert.equal((await fetch(server.url + '/api/local-directories')).status, 403);
const before = (await server.api('/bootstrap')).projects.length;
assert.equal((await server.api('/local-directories?path=' + encodeURIComponent(f.projectRoot))).current, f.projectRoot);
assert.equal((await server.api('/bootstrap')).projects.length, before);
const result = await server.api('/projects/' + f.project.id + '/file-previews', { path: 'pages/页面.html' });
assert.notEqual(new URL(result.url).origin, server.url);
assert.equal((await fetch(server.url + '/api/bootstrap', { headers: { Origin: 'null' } })).status, 403);
assert.equal((await server.api('/local-file-preview-access?url=' + encodeURIComponent(result.url))).allowed, true);
await assert.rejects(server.api('/projects/' + f.project.id + '/file-previews', { path: '../escape.html' }));
const response = await fetch(server.url + '/api/projects/' + f.project.id + '/file-previews/' + result.id, { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': server.bootstrap.token }, body: '{}' });
assert.equal(response.status, 200); await assert.rejects(fetch(result.url));
});
@@ -0,0 +1,226 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { fixture, compatibleFixture, completion, call, until, assertToolPairs, launchWorkbench } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Store } from '../src/server/store.mjs';
import { validateMcpArguments, checkMcpSchema } from '../src/core/tools/mcp-contract.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
import { gitEnvironment } from '../src/core/git-process.mjs';
function entryFor(f) { const entry = path.join(f.root, 'owned-mcp.mjs'); fs.copyFileSync(path.resolve('tests/fixtures/registered-mcp-server.mjs'), entry); return entry; }
async function setup(t, respond = () => completion([], 'Owned MCP local fixture response.')) {
const f = fixture(t), provider = new Provider(); provider.setKey('owned-mcp-key-canary');
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
const manager = runtime.mcpRegistry(), entry = entryFor(f);
const saved = manager.register(f.project.id, { name: 'Owned stdio MCP', executable: process.execPath, entry, timeoutMs: 800 });
return { ...f, provider, runtime, manager, entry, saved, respond,
async probe() { const result = await manager.start(f.session, { id: saved.id, configurationRevision: manager.find(f.project.id, saved.id).configurationRevision, confirm: true }).promise;
assert.equal(result.status, 'completed', result.error?.message); return result; },
async model() { const server = await compatibleFixture(t, respond); f.store.saveConfig({ mode: 'compatible', model: 'owned-mcp', baseUrl: server.baseUrl, contextLimit: 24000 }); return server; },
};
}
function enable(f, tools = ['echo']) { const value = f.manager.find(f.project.id, f.saved.id); return f.manager.configure(f.project.id, { id: value.id, configurationRevision: value.configurationRevision, enabled: true, allowedTools: tools }); }
function toolInput(f, tool = 'echo', args = { text: 'Owned echo', count: 2 }) { const value = f.manager.find(f.project.id, f.saved.id); return { registration_id: value.id, configuration_revision: value.configurationRevision, contract_sha256: value.contract.sha256, tool, arguments: args }; }
test('MCP registration stays disabled until an explicit real stdio probe and schema-based tool selection, with actual process cleanup', async t => {
const f = await setup(t); assert.equal(f.saved.enabled, false); assert.equal(f.saved.contract, null);
assert.throws(() => f.manager.start(f.session, { id: f.saved.id, configurationRevision: 1, confirm: false }), /确认/);
const job = await f.probe(), report = f.manager.report(f.session, job.id);
assert.equal(report.initialize.serverInfo.version, '1.0'); assert.equal(report.tools.length, 3); assert.equal(job.cleanup.rootExited, true);
assert.ok(report.tools.find(value => value.name === 'unsupported').unsupported);
assert.deepEqual(report.trace.map(value => value.method), ['initialize', 'tools/list']); assert.equal(report.result, null);
assert.throws(() => enable(f, ['unsupported']), /参数契约/); const enabled = enable(f); assert.equal(enabled.enabled, true);
assert.equal(f.manager.catalog(f.project.id).entries.length, 1);
assert.throws(() => f.manager.configure(f.project.id, { id: enabled.id, configurationRevision: 2, enabled: false, allowedTools: [] }), /配置已变化/);
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
const next = new Runtime(reopened, new Provider()); assert.equal(next.mcpRegistry().view(f.project.id).registrations[0].status, 'verified');
});
test('MCP probe holds the project operation lock until its owned process and report are settled', async t => {
const f = await setup(t), entry = path.join(f.root, 'second-mcp.mjs');
fs.copyFileSync(path.resolve('tests/fixtures/registered-mcp-server.mjs'), entry);
const probe = f.manager.start(f.session, { id: f.saved.id, configurationRevision: 1, confirm: true });
assert.throws(() => f.manager.register(f.project.id, { name: 'Concurrent MCP', executable: process.execPath, entry }), /项目正在执行/);
const result = await probe.promise;
assert.equal(result.status, 'completed'); assert.equal(result.cleanup.rootExited, true);
const saved = f.manager.register(f.project.id, { name: 'After MCP', executable: process.execPath, entry });
assert.equal(saved.enabled, false);
});
test('MCP validates the declared JSON schema subset and rejects unsupported schemas before external dispatch', () => {
const schema = { type: 'object', properties: { count: { type: 'integer', minimum: 1, maximum: 3 }, name: { type: 'string', enum: ['alpha', 'beta'] } }, required: ['count'], additionalProperties: false };
assert.equal(validateMcpArguments(schema, { count: 2, name: 'alpha' }), true);
for (const value of [{}, { count: 4 }, { count: '2' }, { count: 2, other: true }, { count: 2, name: 'other' }]) assert.throws(() => validateMcpArguments(schema, value));
assert.throws(() => checkMcpSchema({ type: 'object', $ref: '#/other' }), /schema/);
assert.throws(() => checkMcpSchema({ type: 'object', properties: { x: { type: 'string', pattern: '.*' } } }), /schema/);
});
test('actual model MCP call approves long schema-valid arguments, validates the sent catalog and stores one original result without leaking environment secrets', async t => {
let input;
const f = await setup(t, (_url, body, requests) => requests.length === 1 ? completion([call('owned-external-call', 'call_registered_mcp', input)]) : completion([], 'One owned MCP call finished.'));
await f.probe(); enable(f); input = toolInput(f, 'echo', { text: 'Owned echo '.repeat(1200), count: 2 });
const server = await f.model(); f.runtime.start(f.session.id, 'Use the registered echo tool for the current project.');
await until(() => f.session.status === 'awaiting_approval'); assert.equal(f.manager.jobs(f.session).length, 1);
const approval = f.session.approvals.find(value => value.status === 'pending'); assert.match(approval.command, /工具 echo/);
assert.ok(approval.command.length > 6000); assert.ok(approval.command.includes(JSON.stringify(input.arguments)));
assert.equal(approval.mcpRegistrationId, f.saved.id); assert.equal(approval.taskId, f.session.taskId);
f.runtime.decide(f.session.id, approval.id, true); await f.runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'completed', f.session.lastFailure?.message); assert.equal(server.requests.length, 2); assertToolPairs(f.session.providerMessages);
const job = f.manager.jobs(f.session).find(value => value.operation === 'call'); assert.equal(job.status, 'completed'); assert.equal(job.dispatched, true);
const report = f.manager.report(f.session, job.id); assert.equal(report.result.content[0].text, input.arguments.text.repeat(2));
assert.deepEqual(report.result.structuredContent.environmentNames, []); assert.equal(report.cleanup.rootExited, true);
assert.equal(JSON.stringify(report).includes('owned-mcp-key-canary'), false);
for (const plan of f.store.core.context_plan.values()) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('denying an MCP call never starts another process or executes the external tool', async t => {
let input; const f = await setup(t, (_url, _body, requests) => requests.length === 1 ? completion([call('denied-external', 'call_registered_mcp', input)]) : completion([], 'Denied.'));
await f.probe(); enable(f); input = toolInput(f); await f.model();
f.runtime.start(f.session.id, 'Request the registered tool.'); await until(() => f.session.status === 'awaiting_approval');
f.runtime.decide(f.session.id, f.session.approvals.find(value => value.status === 'pending').id, false); await f.runtime.active.get(f.session.id).promise;
assert.equal(f.manager.jobs(f.session).length, 1); assert.equal([...f.store.core.tool.values()][0].state, 'denied');
});
test('actual handshake drift rejects tools/call even if program entry hash is unchanged', async t => {
let input; const f = await setup(t, (_url, _body, requests) => requests.length === 1 ? completion([call('drift-external', 'call_registered_mcp', input)]) : completion([], 'Version drift rejected.'));
await f.probe(); enable(f); input = toolInput(f);
// Simulate a dynamic dependency by changing the stored original contract's actual server version.
const state = f.manager.state(), registration = state.registrations[0], original = f.manager.contract(registration);
original.serverInfo.version = 'unmatched-version';
const contract = f.store.artifactStore.write(f.session, f.store.core.run.get(f.session.runId), original, { kind: 'mcp-contract' });
f.store.core.artifact.set(contract.id, contract); f.store.save(f.session); registration.contract = { ...registration.contract, artifactId: contract.id, sha256: contract.sha256 }; f.manager.save(state);
input = toolInput(f); await f.model(); f.runtime.start(f.session.id, 'Call with fixed actual schema.'); await until(() => f.session.status === 'awaiting_approval');
f.runtime.decide(f.session.id, f.session.approvals.find(value => value.status === 'pending').id, true); await f.runtime.active.get(f.session.id).promise;
const job = f.manager.jobs(f.session).find(value => value.operation === 'call'), report = f.manager.report(f.session, job.id);
assert.equal(job.status, 'failed'); assert.equal(job.dispatched, false); assert.match(job.error.message, /实际版本/);
assert.equal(report.trace.some(value => value.method === 'tools/call'), false);
});
test('cancelled dispatched external side effect stays unknown, closes its owned process and cannot replay on reopen', async t => {
let input; const f = await setup(t, (_url, _body, requests) => requests.length === 1 ? completion([call('held-external', 'call_registered_mcp', input)]) : completion([], 'Stopped.'));
await f.probe(); enable(f, ['hold']); input = toolInput(f, 'hold', { text: 'unknown-effect' }); await f.model();
f.runtime.start(f.session.id, 'Use the external held operation.'); await until(() => f.session.status === 'awaiting_approval');
f.runtime.decide(f.session.id, f.session.approvals.find(value => value.status === 'pending').id, true);
await until(() => f.manager.jobs(f.session).some(value => value.status === 'calling'));
const job = f.manager.jobs(f.session).find(value => value.operation === 'call');
await until(() => fs.existsSync(path.join(job.directory, 'owned-call-dispatched.txt'))); await f.runtime.stop(f.session.id);
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, true); assert.equal(job.cleanup.rootExited, true);
const before = f.manager.jobs(f.session).length; f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
assert.equal(reopened.core.mcp_job.get(job.id).status, 'unknown'); assert.equal([...reopened.core.mcp_job.values()].length, before);
assert.equal(fs.readFileSync(path.join(job.directory, 'owned-call-dispatched.txt'), 'utf8').includes('unknown-effect'), true);
});
test('changed executable input, foreign project IDs and corrupted original contracts block enable and dispatch', async t => {
const f = await setup(t); await f.probe(); const value = f.manager.find(f.project.id, f.saved.id);
const otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); const other = f.store.addProject(otherRoot);
assert.throws(() => f.manager.find(other.id, value.id), /不属于当前项目/);
fs.appendFileSync(f.entry, '\n// Changed executable input.\n'); assert.throws(() => enable(f), /程序文件已变化/);
assert.equal(f.manager.view(f.project.id).registrations[0].status, 'changed');
fs.copyFileSync(path.resolve('tests/fixtures/registered-mcp-server.mjs'), f.entry);
const artifact = f.store.core.artifact.get(value.contract.artifactId); fs.appendFileSync(path.join(f.store.artifactStore.root, artifact.relative), '\nChanged.');
assert.throws(() => enable(f), /工件校验失败/);
});
test('registered MCP HTTP API uses fixed execution contracts and project-owned probe records with no model requests', async t => {
const f = fixture(t), entry = entryFor(f); f.store.close(); const host = await launchWorkbench(t, f.data); f.onCleanup(host.close);
const saved = await host.api('/projects/' + f.project.id + '/mcp', { name: 'HTTP MCP', executable: process.execPath, entry, timeoutMs: 2000 });
assert.equal(saved.enabled, false);
const started = await host.api('/sessions/' + f.session.id + '/mcp/probe', { id: saved.id, configurationRevision: 1, confirm: true });
await until(async () => (await host.api('/sessions/' + f.session.id + '/mcp/jobs')).some(value => value.id === started.job.id && value.status === 'completed'));
const report = await host.api('/sessions/' + f.session.id + '/mcp/jobs/' + started.job.id + '/report'); assert.equal(report.cleanup.rootExited, true);
const input = await host.api('/sessions/' + f.session.id + '/mcp/jobs/' + started.job.id + '/input');
const sealed = (await host.api('/sessions/' + f.session.id + '/mcp/jobs')).find(value => value.id === started.job.id);
assert.equal(input.taskId, started.job.taskId); assert.equal(input.runId, started.job.runId); assert.equal(input.sha256, sealed.inputSha256);
const view = await host.api('/projects/' + f.project.id + '/mcp'); assert.equal(view.registrations[0].tools.length, 3);
await assert.rejects(host.api('/projects/' + f.project.id + '/mcp', { name: 'Arbitrary command', executable: process.execPath, args: ['-e', 'do not execute'] }), /不支持|未支持/);
await host.close();
});
test('unknown MCP pauses later tool calls and all project writers; fingerprinted acknowledgement clears only the review gates and explicit resume does not replay', async t => {
let input;
const f = await setup(t, (_url, _body, requests) => requests.length === 1 ? completion([
call('unknown-before-writer', 'call_registered_mcp', input),
call('must-not-execute-later', 'run_command', { command: 'Set-Content must-not-exist.txt unexpected', reason: 'Owned later call must never execute.' }),
]) : completion([], 'Explicit resumed task.'));
await f.probe(); enable(f, ['hold']); input = toolInput(f, 'hold', { text: 'timeout-original-effect' }); const server = await f.model();
f.runtime.start(f.session.id, 'Call the external tool before doing any further work.');
await until(() => f.session.status === 'awaiting_approval');
f.runtime.decide(f.session.id, f.session.approvals.find(value => value.status === 'pending').id, true);
await f.runtime.active.get(f.session.id).promise;
const job = f.manager.jobs(f.session).find(value => value.operation === 'call'), tool = f.store.owned('tool', job.invocationId, f.session.id);
assert.equal(job.status, 'unknown'); assert.equal(tool.requiresVerification, true); assert.equal(f.session.status, 'paused');
assert.equal(f.store.core.run.get(job.runId).stopReason, 'mcp_review'); assert.equal(server.requests.length, 1);
const later = [...f.store.core.tool.values()].find(value => value.callId === 'must-not-execute-later');
assert.equal(later.state, 'interrupted'); assert.equal(later.result.reason, 'mcp_review'); assertToolPairs(f.session.providerMessages);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'must-not-exist.txt')), false);
const original = f.store.artifactStore.readBytes(f.store.owned('artifact', tool.resultArtifactId, f.session.id));
const transcript = JSON.stringify(f.session.providerMessages), other = f.store.createSession(f.project.id);
assert.throws(() => f.runtime.start(other.id, 'Cannot evade project-wide unknown results.'), /MCP.*待核对/);
assert.throws(() => proposeFile(f.store, other, { path: 'new.txt', content: 'blocked' }), /MCP.*待核对/);
assert.throws(() => applyChange(f.store, f.session, 'no-change'), /MCP.*待核对/);
await assert.rejects(f.runtime.checkRunner().start(f.session, 'no-plan'), /MCP.*待核对/);
assert.throws(() => f.runtime.gitDelivery().assertTask(f.session), /MCP.*待核对/);
assert.throws(() => f.runtime.changeSetManager().requireIdle(f.session), /MCP.*待核对/);
assert.throws(() => f.runtime.workspaceManager().assertIdle(f.session), /MCP.*待核对/);
assert.throws(() => f.runtime.browserManager().assertReady(f.session), /MCP.*待核对/);
const evidence = f.manager.evidence(f.session, job.id);
assert.throws(() => f.manager.acknowledge(f.session, job.id, '0'.repeat(64)), /指纹/);
const acknowledged = f.manager.acknowledge(f.session, job.id, evidence.evidenceSha256);
assert.equal(acknowledged.status, 'unknown'); assert.equal(acknowledged.requiresVerification, false);
const reviewed = f.store.core.tool.get(tool.id); assert.equal(reviewed.state, 'unknown'); assert.equal(reviewed.requiresVerification, false);
assert.equal(f.store.core.run.get(job.runId).checkpoint.requiresVerification.includes(tool.id), false);
assert.deepEqual(f.store.artifactStore.readBytes(f.store.owned('artifact', tool.resultArtifactId, f.session.id)), original);
assert.equal(JSON.stringify(f.session.providerMessages), transcript); assert.equal(server.requests.length, 1);
f.runtime.resume(f.session.id); await f.runtime.active.get(f.session.id).promise;
assert.equal(server.requests.length, 2); assert.equal(f.manager.jobs(f.session).filter(value => value.operation === 'call').length, 1);
assert.equal(f.session.status, 'completed');
});
test('MCP acknowledgement persistence failure restores both job and tool review gates; corrupt evidence cannot be acknowledged', async t => {
let input; const f = await setup(t, () => completion([call('rollback-review', 'call_registered_mcp', input)]));
await f.probe(); enable(f, ['hold']); input = toolInput(f, 'hold', { text: 'review-rollback' }); await f.model();
f.runtime.start(f.session.id, 'Preserve unresolved state if acknowledgement cannot persist.'); await until(() => f.session.status === 'awaiting_approval');
f.runtime.decide(f.session.id, f.session.approvals.find(value => value.status === 'pending').id, true); await f.runtime.active.get(f.session.id).promise;
const job = f.manager.jobs(f.session).find(value => value.operation === 'call'), evidence = f.manager.evidence(f.session, job.id), save = f.store.save;
f.store.save = function(session, event) { if (event?.type === 'mcp.acknowledged') throw new Error('Owned acknowledgement persistence failure'); return save.call(this, session, event); };
try { assert.throws(() => f.manager.acknowledge(f.session, job.id, evidence.evidenceSha256), /persistence failure/); }
finally { f.store.save = save; }
assert.equal(f.store.core.mcp_job.get(job.id).requiresVerification, true); assert.equal(f.store.core.tool.get(job.invocationId).requiresVerification, true);
assert.equal(f.store.core.run.get(job.runId).checkpoint.requiresVerification.includes(job.invocationId), true);
const tool = f.store.core.tool.get(job.invocationId), originalHash = tool.argsSha256; tool.argsSha256 = '0'.repeat(64);
assert.throws(() => f.manager.evidence(f.session, job.id), /原调用输入/); tool.argsSha256 = originalHash;
const artifact = f.store.core.artifact.get(job.inputArtifactId); fs.appendFileSync(path.join(f.store.artifactStore.root, artifact.relative), '\ncorrupted');
assert.throws(() => f.manager.acknowledge(f.session, job.id, evidence.evidenceSha256), /工件校验失败/);
assert.equal(f.store.core.mcp_job.get(job.id).requiresVerification, true);
});
test('actual host exit after an external effect restores unknown jobs without a fabricated report, allows source-bound review and never replays the call', async t => {
let input; const f = await setup(t, () => completion([call('crashed-mcp-host', 'call_registered_mcp', input)]));
await f.probe(); enable(f, ['hold']); input = toolInput(f, 'hold', { text: 'crash-original-effect' }); const server = await f.model(); f.store.close();
const executed = await promisify(execFile)(process.execPath, [path.resolve('tests/fixtures/mcp-host-crash.mjs'), f.data, f.session.id], { env: gitEnvironment(), windowsHide: true, timeout: 18000 })
.then(() => assert.fail('Owned crash worker must exit after the dispatched effect.'), error => { assert.equal(error.code, 85, error.stderr); return JSON.parse(error.stdout.trim().split('\n').at(-1)); });
assert.equal(executed.crashAfterDispatched, true);
const store = new Store(f.data); f.onCleanup(() => store.close()); const runtime = new Runtime(store, new Provider()); f.onCleanup(() => runtime.stopAll());
const session = store.session(f.session.id), manager = runtime.mcpRegistry(), job = store.core.mcp_job.get(executed.jobId);
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, true); assert.equal(job.reportArtifactId, undefined);
assert.equal(store.core.tool.get(job.invocationId).state, 'unknown'); assert.equal(server.requests.length, 1);
const evidence = manager.evidence(session, job.id); assert.equal(evidence.report, null); assert.match(evidence.note, /没有完整原报告/);
assert.equal(evidence.input.input.call.arguments.text, 'crash-original-effect');
assert.throws(() => runtime.resume(session.id), /MCP.*待核对/);
const reviewed = manager.acknowledge(session, job.id, evidence.evidenceSha256);
assert.equal(reviewed.status, 'unknown'); assert.equal(reviewed.reportArtifactId, undefined); assert.equal(store.core.tool.get(job.invocationId).requiresVerification, false);
assert.equal(manager.jobs(session).filter(value => value.operation === 'call').length, 1); assert.equal(server.requests.length, 1);
});
test('changed enabled MCP programs can be explicitly disabled without launching or trusting their changed source', async t => {
const f = await setup(t); await f.probe(); const enabled = enable(f);
fs.appendFileSync(f.entry, '\n// Changed dependency entry.\n'); assert.equal(f.manager.view(f.project.id).registrations[0].status, 'changed');
const disabled = f.manager.configure(f.project.id, { id: enabled.id, configurationRevision: enabled.configurationRevision, enabled: false, allowedTools: [] });
assert.equal(disabled.enabled, false); assert.deepEqual(f.manager.catalog(f.project.id).entries, []); assert.equal(f.manager.jobs(f.session).length, 1);
});
@@ -0,0 +1,67 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { McpStdio } from '../src/core/tools/mcp-stdio.mjs';
import { ReaAdapter, resolveReaInstallation } from '../src/core/tools/rea-adapter.mjs';
import { fixture, workspace } from './helpers.mjs';
const script = path.join(workspace, 'tests', 'fixtures', 'research-mcp.cjs');
function clientFixture(t, mode, options = {}) {
const f = fixture(t), client = new McpStdio({ executable: process.execPath, args: [script, mode], cwd: f.root, env: { ...process.env } }, options);
f.onCleanup(() => client.close({ force: true })); return { ...f, client };
}
test('stdio preserves split UTF-8 frames and final replies drained after process exit', async t => {
for (const mode of ['echo', 'exit-result']) {
const { client } = clientFixture(t, mode), result = await client.request('echo', { source: '用户工件🙂' });
assert.equal(result.text, '原字节🙂'); assert.equal(result.received.source, '用户工件🙂');
assert.equal(client.trace.length, 1); assert.equal(client.trace[0].status, 'completed');
assert.match(client.trace[0].resultSha256, /^[a-f0-9]{64}$/);
assert.equal((await client.close({ force: true })).rootExited, true);
}
});
test('server-initiated model requests are refused instead of inheriting provider privileges', async t => {
const { client } = clientFixture(t, 'server-request'); assert.deepEqual(await client.request('echo'), { denied: true });
assert.equal(client.trace.length, 1);
});
for (const [mode, code] of [['invalid', 'MCP_INVALID_FRAME'], ['oversized', 'MCP_FRAME_LIMIT']]) {
test('invalid or over-budget protocol ' + mode + ' closes only its owned process and is not retried', async t => {
const { client } = clientFixture(t, mode, { maximumFrameBytes: 1024 });
await assert.rejects(client.request('echo'), error => error.code === code && error.dispatched === true);
assert.equal(client.trace.length, 1); assert.equal(client.trace[0].status, 'failed');
assert.equal((await client.close({ force: true })).rootExited, true);
});
}
test('cancel and timeout do not resend a dispatched request and pre-cancel has no request trace', async t => {
const { client } = clientFixture(t, 'hang', { timeoutMs: 100 });
await assert.rejects(client.request('echo'), error => error.code === 'MCP_TIMEOUT' && error.dispatched);
assert.equal(client.trace.length, 1);
const controller = new AbortController(), pending = client.request('echo', {}, { signal: controller.signal, timeoutMs: 3000 }); controller.abort();
await assert.rejects(pending, error => error.code === 'MCP_CANCELLED' && error.dispatched);
await assert.rejects(client.request('echo', {}, { signal: controller.signal }), error => error.code === 'MCP_CANCELLED' && !error.dispatched);
assert.equal(client.trace.length, 2); assert.equal((await client.close({ force: true })).rootExited, true);
});
test('startup failure remains undispatched and has no process PID to terminate', async t => {
const f = fixture(t), client = new McpStdio({ executable: path.join(f.root, 'missing-node.exe'), args: [], cwd: f.root });
f.onCleanup(() => client.close()); await assert.rejects(client.request('initialize'), error => error.code === 'MCP_START_FAILED' && !error.dispatched);
assert.equal(client.trace[0].dispatched, false); const closed = await client.close(); assert.equal(closed.pid, null); assert.equal(closed.rootExited, true);
});
test('REA launch filters a fixture credential canary and rejects changed entry identity before a new connection', async t => {
const f = fixture(t), packageRoot = path.join(f.root, 'package'); fs.mkdirSync(path.join(packageRoot, 'scripts'), { recursive: true });
fs.writeFileSync(path.join(packageRoot, 'package.json'), JSON.stringify({ name: 'rea-agents', version: '6.8.0' }));
fs.copyFileSync(script, path.join(packageRoot, 'scripts', 'rea.mjs'));
// The fixture entry uses CommonJS; its local declaration intentionally selects
// CommonJS so the same protocol fixture can be launched through REA's profile.
fs.writeFileSync(path.join(packageRoot, 'package.json'), JSON.stringify({ name: 'rea-agents', version: '6.8.0', type: 'commonjs' }));
fs.copyFileSync(script, path.join(packageRoot, 'scripts', 'rea.cjs'));
fs.writeFileSync(path.join(packageRoot, 'scripts', 'rea.mjs'), "import './rea.cjs';\n");
const installation = resolveReaInstallation(f.root, { packageRoot });
const previous = process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY; process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY = 'fixture-only-canary';
const adapter = new ReaAdapter(installation, { cwd: f.root }); f.onCleanup(() => adapter.close({ force: true }));
try {
const contract = await adapter.connect(); assert.equal(contract.initialize.fixtureCanaryForwarded, false);
assert.equal(contract.advertisedTools, 0); assert.equal((await adapter.close({ force: true })).rootExited, true);
fs.appendFileSync(installation.entry.path, '// changed entry\n');
const changed = new ReaAdapter(installation, { cwd: f.root }); await assert.rejects(changed.connect(), /入口身份/); assert.equal(changed.client, undefined);
} finally { if (previous === undefined) delete process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY; else process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY = previous; }
});
@@ -0,0 +1,50 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { fixture } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Store } from '../src/server/store.mjs';
import { capabilityView, capabilityRejections } from '../src/shared/contracts/model-capabilities.mjs';
const input = { name: 'Owned Responses', model: 'owned-model', protocol: 'responses', baseUrl: 'http://127.0.0.1:12345/v1', requiresKey: false };
test('schema1 connections remain readable without inventing observed capabilities or migrating original state', t => {
const f = fixture(t), provider = new Provider();
const original = { schema: 1, revision: 4, selected: { type: 'connection', id: 'owned-old' }, pools: [], imports: {},
connections: [{ ...input, protocol: 'chat-completions', id: 'owned-old', revision: 1, enabled: true }] };
f.store.storage.setMeta('modelRegistry', original);
const hub = provider.modelHub(f.store), old = hub.view().connections.find(item => item.id === 'owned-old');
assert.equal(old.capabilities.tools, 'protocol_declared'); assert.equal(old.capabilities.streaming, 'unknown');
assert.equal(old.capabilities.vision, 'unknown'); assert.equal(old.capabilities.audio, 'unknown');
assert.deepEqual(f.store.storage.meta('modelRegistry'), original);
});
test('protocol, unsupported capabilities and stream choice persist; client claims cannot forge verified evidence', t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store);
const connection = hub.upsert({ ...input, streaming: true, apiKey: 'owned-volatile', capabilityDeclarations: { tools: 'unsupported', streaming: 'supported' },
capabilityEvidence: { results: { tools: 'verified' } } });
assert.equal(connection.protocol, 'responses'); assert.equal(connection.capabilities.tools, 'unsupported');
assert.equal(connection.capabilityEvidence, undefined); assert.equal(connection.capabilities.streaming, 'protocol_declared');
assert.equal(JSON.stringify(f.store.storage.meta('modelRegistry')).includes('owned-volatile'), false);
assert.throws(() => hub.upsert({ ...input, protocol: 'unknown' }), /协议/);
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
const restored = new Provider().modelHub(reopened).connection(connection.id);
assert.equal(restored.streaming, true); assert.equal(restored.capabilityDeclarations.tools, 'unsupported');
assert.equal(restored.protocol, 'responses');
});
test('ability evidence belongs to exact connection and credential revision; unknown and unsupported reject required routing', () => {
const connection = { ...input, id: 'owned', revision: 3, capabilityDeclarations: { text: 'supported', tools: 'unknown', streaming: 'unsupported' },
capabilityEvidence: { connectionRevision: 3, credentialRevision: 7, results: { tools: 'verified', text: 'verified' } } };
assert.equal(capabilityView(connection, 7).tools, 'verified');
assert.equal(capabilityView(connection, 8).tools, 'unknown');
assert.deepEqual(capabilityRejections(connection, ['text', 'tools', 'streaming'], 7).map(item => item.capability), ['streaming']);
assert.deepEqual(capabilityRejections({ ...connection, revision: 4 }, ['text', 'tools'], 7).map(item => item.capability), ['tools']);
});
test('invalid declarations and inconsistent stream settings reject before registry, revision and credentials mutate', t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store), connection = hub.upsert(input);
const before = structuredClone(hub.state);
for (const change of [{ capabilityDeclarations: { tools: 'verified' } }, { capabilityDeclarations: { invented: 'supported' } }, { streaming: true }, { streaming: 'yes' }]) {
assert.throws(() => hub.upsert({ ...connection, ...change, apiKey: 'owned-should-not-save' }));
assert.deepEqual(hub.state, before); assert.equal(provider.credentials.has(connection.id), false);
}
});
@@ -0,0 +1,199 @@
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, call, until, launchWorkbench, assertToolPairs } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Store } from '../src/server/store.mjs';
import { ConfigImporter } from '../src/core/models/config-import.mjs';
const add = (hub, server, name, extra = {}) => hub.upsert({ name, model: name, baseUrl: server.baseUrl, requiresKey: true, apiKey: 'owned-' + name, ...extra });
test('model registry keeps original credentials/endpoint intact, rejects stale edits, and forgets only volatile keys on restart', t => {
const f = fixture(t), provider = new Provider(); provider.setKey('owned-legacy');
f.store.saveConfig({ baseUrl: 'http://127.0.0.1:12000/v1', model: 'old' });
const hub = provider.modelHub(f.store), connection = add(hub, { baseUrl: 'http://127.0.0.1:13000/v1' }, 'new');
hub.select({ type: 'connection', id: connection.id });
assert.equal(f.store.config.baseUrl, 'http://127.0.0.1:12000/v1'); assert.equal(provider.apiKey, 'owned-legacy');
assert.equal(provider.prepareConfig(f.store.config, f.store).baseUrl, 'http://127.0.0.1:13000/v1');
assert.throws(() => hub.upsert({ ...connection, revision: 0 }), /刷新/);
assert.equal(JSON.stringify(hub.view()).includes('owned-new'), false);
const stored = JSON.stringify(f.store.storage.meta('modelRegistry')); assert.equal(stored.includes('owned-'), false);
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
const fresh = new Provider(); fresh.setKey(''); const restored = fresh.modelHub(reopened).view();
assert.equal(restored.connections.find(value => value.id === connection.id).status, 'needs_key');
assert.equal(restored.selected.id, connection.id);
});
test('config import previews exact revisions, keeps secrets volatile, syncs only when enabled and preserves manual edits on conflicts', t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store), importer = new ConfigImporter(hub);
const filename = path.join(f.root, 'connections.json');
const write = (model, name = 'Owned import') => fs.writeFileSync(filename, JSON.stringify({ schema: 1, connections: [{ name, model,
baseUrl: 'http://127.0.0.1:12345/v1', apiKey: 'import-owned-secret' }] }));
write('fixture-one');
const preview = importer.preview('workbench-json', filename);
assert.equal(JSON.stringify(preview).includes('import-owned-secret'), false);
write('fixture-changed'); assert.throws(() => importer.apply(preview.id), /来源配置已变化/);
write('fixture-one');
const accepted = importer.preview('workbench-json', filename), result = importer.apply(accepted.id);
assert.equal(result.imported.length, 1);
const sourceId = Object.keys(hub.state.imports)[0], initial = hub.connection(result.imported[0]);
assert.equal(initial.model, 'fixture-one'); assert.equal(importer.read('workbench-json', filename).entries.length, 1);
assert.equal(JSON.stringify(f.store.storage.meta('modelRegistry')).includes('import-owned-secret'), false);
write('fixture-two'); assert.deepEqual(importer.sync(), []); assert.equal(hub.connection(initial.id).model, 'fixture-one');
importer.toggle(sourceId, true); const synced = importer.sync(); assert.equal(synced[0].imported.length, 1);
assert.equal(hub.connection(initial.id).model, 'fixture-two');
const current = hub.connection(initial.id); hub.upsert({ ...current, name: 'Manual owner edit' });
write('fixture-three'); const conflicted = importer.sync(); assert.deepEqual(conflicted[0].conflicts, ['Manual owner edit']);
assert.equal(hub.connection(initial.id).name, 'Manual owner edit'); assert.equal(hub.connection(initial.id).model, 'fixture-two');
assert.ok(hub.state.imports[sourceId].lastCheckedAt);
importer.toggle(sourceId, false); write('fixture-four'); assert.deepEqual(importer.sync(), []);
assert.equal(hub.connection(initial.id).model, 'fixture-two');
const workbuddy = importer.discover().sources.find(value => value.source === 'workbuddy');
assert.equal(workbuddy.supported, false); assert.equal(workbuddy.available, false);
});
test('real HTTP pool fails over 429 to another model and never repeats completed tools; attempts seal actual request bytes and Run identity', async t => {
const f = fixture(t), bad = await compatibleFixture(t, () => ({ status: 429, headers: { 'retry-after': '2' }, body: { error: { message: 'owned rate limit' } } }));
const good = await compatibleFixture(t, (_url, body) => body.messages.some(value => value.role === 'tool') ? completion([], 'Done') : completion([call('pool_read', 'list_files', { path: '', depth: 1 })]));
const provider = new Provider(), hub = provider.modelHub(f.store), first = add(hub, bad, 'rate-limited'), second = add(hub, good, 'working');
const pool = hub.pool({ name: 'Owned pool', members: [{ connectionId: first.id }, { connectionId: second.id }], sticky: true });
hub.select({ type: 'pool', id: pool.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'List files and finish.');
await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed'); assert.equal(bad.requests.length, 1); assert.equal(good.requests.length, 2);
assert.equal(f.session.events.filter(value => value.tool === 'list_files' && value.phase === 'completed').length, 1);
assertToolPairs(f.session.providerMessages);
const attempts = hub.view().attempts; assert.equal(attempts.length, 3); assert.equal(attempts.filter(value => value.status === 'failed').length, 1);
const usage = f.store.core.run.get(f.session.runId).usage;
assert.equal(usage.requests, 3); assert.equal(usage.unknownRequests, 1); assert.equal(usage.totalTokens, 336);
assert.equal(f.store.core.run.get(f.session.runId).consumption.length, 3);
assert.ok(attempts.every(value => value.taskId === f.session.taskId && value.runId === f.session.runId));
for (const attempt of attempts) {
const artifact = f.store.owned('artifact', attempt.requestArtifactId, f.session.id);
const body = JSON.parse(f.store.artifactStore.read(artifact));
assert.equal(body.model, attempt.model); assert.equal(artifact.sha256, attempt.requestSha256);
}
const rateLimit = hub.view().connections.find(value => value.id === first.id).health;
assert.equal(Date.parse(rateLimit.lastFailureAt) + 2000, rateLimit.cooldownUntil, 'Retry-After is reflected in the recorded cooldown even if the test finishes after it expires');
assert.equal(good.requests[0].headers.authorization, 'Bearer owned-working');
assert.equal(bad.requests[0].headers.authorization, 'Bearer owned-rate-limited');
});
test('pool rotation and weights select members on new Runs; disabled and missing credentials are excluded without requests', async t => {
const f = fixture(t), a = await compatibleFixture(t, () => completion()), b = await compatibleFixture(t, () => completion());
const provider = new Provider(), hub = provider.modelHub(f.store), first = add(hub, a, 'A'), second = add(hub, b, 'B');
const pool = hub.pool({ name: 'Weighted', strategy: 'weighted', members: [{ connectionId: first.id, weight: 2 }, { connectionId: second.id, weight: 1 }] });
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
for (let index = 0; index < 3; index++) { runtime.start(f.session.id, 'Finish ' + index); await until(() => !runtime.isActive(f.session.id)); }
assert.equal(a.requests.length, 2); assert.equal(b.requests.length, 1);
hub.toggle(first.id, false);
runtime.start(f.session.id, 'Use enabled member'); await until(() => !runtime.isActive(f.session.id));
assert.equal(a.requests.length, 2); assert.equal(b.requests.length, 2);
hub.toggle(second.id, false); assert.throws(() => runtime.start(f.session.id, 'Nothing available'), /没有可用/);
});
test('401 blocks the credential revision; updating the key restores availability without leaking error echoes', async t => {
const f = fixture(t), server = await compatibleFixture(t, () => ({ status: 401, body: { error: { message: 'invalid owned-secret' } } }));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'secret', { apiKey: 'owned-secret' });
hub.select({ type: 'connection', id: connection.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Try'); await until(() => !runtime.isActive(f.session.id));
assert.equal(server.requests.length, 1); assert.equal(hub.view().connections.find(value => value.id === connection.id).status, 'needs_attention');
assert.equal(JSON.stringify(f.session).includes('owned-secret'), false); assert.equal(JSON.stringify(hub.view()).includes('owned-secret'), false);
const changed = hub.upsert({ ...connection, apiKey: 'replacement' }); assert.equal(changed.status, 'configured');
});
test('queued request cancellation and in-flight disable release only owned leases and do not dispatch another HTTP request', async t => {
const f = fixture(t); let release;
const server = await compatibleFixture(t, async () => { await new Promise(resolve => { release = resolve; }); return completion(); });
const provider = new Provider(), hub = provider.modelHub(f.store), value = add(hub, server, 'queue'); hub.select({ type: 'connection', id: value.id });
const config = hub.prepare(f.store.config), first = new AbortController(), second = new AbortController();
const payload = { model: value.model, messages: [] };
const execute = (selected, key, body) => provider.request(selected, '/chat/completions', body, first.signal, key, undefined, { attempts: 1 });
const pending = hub.complete(config, payload, first.signal, null, null, execute);
await until(() => !!release);
const queued = hub.complete(config, payload, second.signal, null, null, execute);
await until(() => hub.waiting === 1); second.abort(); await assert.rejects(queued);
hub.toggle(value.id, false); first.abort(); await assert.rejects(pending); release();
assert.equal(server.requests.length, 1); assert.equal(hub.leases.get(value.id), 0); assert.equal(hub.waiting, 0);
const attempt = hub.view().attempts.at(-1);
assert.equal(attempt.status, 'interrupted');
const health = hub.health.get(value.id + ':' + attempt.connectionRevision + ':' + attempt.credentialRevision);
assert.equal(health.state, 'interrupted'); assert.equal(health.cooldownUntil, undefined);
});
test('compiled model-center HTTP API persists public profiles, blocks forged origins, and local operations never invoke a model', async t => {
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
const saved = await host.api('/model-center/connections', { name: 'HTTP own profile', model: 'owned-model', baseUrl: 'http://127.0.0.1:12000/v1', apiKey: 'owned-http-key' });
assert.equal(saved.hasKey, true); assert.equal(saved.apiKey, undefined);
const view = await host.api('/model-center'); assert.ok(view.connections.some(value => value.id === saved.id));
const operations = await host.api('/operations'); assert.equal(operations.modelRequests, 0); assert.ok(operations.checks.some(value => value.id === 'database' && value.status === 'passed'));
const response = await fetch(host.url + '/api/model-center/connections', { method: 'POST', headers: { 'Content-Type': 'application/json', Origin: 'https://unowned.example', 'X-Workbench-Token': host.bootstrap.token }, body: '{}' });
assert.equal(response.status, 403);
assert.equal(fs.readFileSync(path.join(f.data, 'workbench.sqlite')).includes(Buffer.from('owned-http-key')), false);
});
test('model registry disk failures restore routing, pool profiles and credential references without partial updates', t => {
const f = fixture(t), provider = new Provider(); provider.setKey('owned-original-fixture');
const hub = provider.modelHub(f.store), connection = add(hub, { baseUrl: 'http://127.0.0.1:12000/v1' }, 'atomic');
const state = structuredClone(hub.state), credential = provider.credentials.get(connection.id);
const fail = () => { f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('fixture failed commit'); }; };
for (const action of [
() => hub.toggle(connection.id, false),
() => hub.upsert({ ...connection, name: 'Uncommitted rename', apiKey: 'uncommitted-fixture-key' }),
() => hub.pool({ name: 'Uncommitted pool', members: [{ connectionId: connection.id }] }),
]) {
fail(); assert.throws(action, /fixture failed commit/);
assert.deepEqual(hub.state, state); assert.deepEqual(f.store.storage.meta('modelRegistry'), state);
assert.equal(provider.credentials.get(connection.id), credential);
assert.equal(provider.apiKey, 'owned-original-fixture');
}
f.store.storage.faultInjector = null;
const disabled = hub.toggle(connection.id, false); assert.equal(disabled.enabled, false);
assert.deepEqual(f.store.storage.meta('modelRegistry'), hub.state);
});
test('model selection and saved configuration commit together and failures preserve the original mode and endpoint', t => {
const f = fixture(t), provider = new Provider(); provider.setKey('owned-legacy-fixture');
const hub = provider.modelHub(f.store), connection = add(hub, { baseUrl: 'http://127.0.0.1:12000/v1' }, 'selected');
const state = structuredClone(hub.state), config = structuredClone(f.store.config);
f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('fixture selection failure'); };
assert.throws(() => hub.select({ type: 'connection', id: connection.id }), /fixture selection failure/);
assert.deepEqual(hub.state, state); assert.deepEqual(f.store.config, config);
assert.deepEqual(f.store.storage.meta('modelRegistry'), state); assert.deepEqual(f.store.storage.meta('config'), config);
f.store.storage.faultInjector = null;
hub.select({ type: 'connection', id: connection.id });
const selected = structuredClone(hub.state), selectedConfig = structuredClone(f.store.config);
f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('fixture original config failure'); };
assert.throws(() => hub.select({ type: 'legacy' }, { mode: 'demo', model: 'uncommitted-model' }), /fixture original config failure/);
assert.deepEqual(hub.state, selected); assert.deepEqual(f.store.config, selectedConfig);
assert.equal(provider.apiKey, 'owned-legacy-fixture');
});
test('configuration imports roll back the entire batch and retain one usable preview after validation or commit failure', t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store), importer = new ConfigImporter(hub);
const existing = add(hub, { baseUrl: 'http://127.0.0.1:12000/v1' }, 'preexisting');
const before = structuredClone(hub.state), credential = provider.credentials.get(existing.id), filename = path.join(f.root, 'atomic-import.json');
const entries = [1, 2].map(index => ({ id: index, name: 'Fixture import ' + index, model: 'fixture-model',
baseUrl: 'http://127.0.0.1:12345/v1', apiKey: 'owned-import-key-' + index }));
fs.writeFileSync(filename, JSON.stringify({ schema: 1, connections: entries }));
const preview = importer.preview('workbench-json', filename);
f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('fixture import failure'); };
assert.throws(() => importer.apply(preview.id, true), /fixture import failure/);
assert.deepEqual(hub.state, before); assert.deepEqual(f.store.storage.meta('modelRegistry'), before);
assert.equal(provider.credentials.size, 1); assert.equal(provider.credentials.get(existing.id), credential);
assert.equal(importer.previews.has(preview.id), true);
f.store.storage.faultInjector = null;
const imported = importer.apply(preview.id, true); assert.equal(imported.imported.length, 2);
assert.equal(importer.previews.has(preview.id), false);
assert.deepEqual(f.store.storage.meta('modelRegistry'), hub.state);
const validState = structuredClone(hub.state), validCredentials = new Map(provider.credentials);
entries[1].apiKey = 'x'.repeat(16001);
fs.writeFileSync(filename, JSON.stringify({ schema: 1, connections: entries }));
const invalid = importer.preview('workbench-json', filename);
assert.throws(() => importer.apply(invalid.id), /凭据格式无效/);
assert.deepEqual(hub.state, validState); assert.deepEqual(provider.credentials, validCredentials);
assert.deepEqual(f.store.storage.meta('modelRegistry'), validState);
});
@@ -0,0 +1,227 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import http from 'node:http';
import { createHash } from 'node:crypto';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Store } from '../src/server/store.mjs';
import { fixture, compatibleFixture, completion, launchWorkbench, until } from './helpers.mjs';
import { completionBody, actualRequest, outputReservation } from '../src/shared/contracts/model-request.mjs';
import { nativeParameterSettings } from '../src/shared/contracts/native-parameters.mjs';
import { retryAfterMilliseconds, ERROR_CAPTURE_LIMIT } from '../src/core/models/upstream-error.mjs';
const policy = { attempts: 3, delays: [1, 1], timeoutMs: 1000, totalTimeoutMs: 2000 };
const config = { mode: 'compatible', baseUrl: 'http://127.0.0.1:1/v1', model: 'owned-fidelity', maxOutputTokens: 512 };
const sha = value => createHash('sha256').update(value).digest('hex');
const sse = value => 'data: ' + JSON.stringify(value) + '\n\n';
function configure(f, provider, url, extra = {}) {
const hub = provider.modelHub(f.store), connection = hub.upsert({ name: 'Owned fidelity', model: 'owned-fidelity', baseUrl: url,
requiresKey: false, ...extra });
hub.select({ type: 'connection', id: connection.id });
return { hub, connection };
}
async function execute(f, provider) {
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, '读取当前项目;不要修改或执行检查');
await runtime.active.get(f.session.id).promise;
return f.session;
}
async function streamServer(t, bytes, headers = {}) {
let requests = 0;
const server = http.createServer(async (req, res) => { for await (const _ of req) {} requests++;
res.writeHead(200, { 'Content-Type': 'text/event-stream', ...headers }); res.end(bytes); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
return { url: 'http://127.0.0.1:' + server.address().port + '/v1', count: () => requests };
}
test('HTTP quota retains native code/type/param/request id, bounded original and allowlisted headers, never repeats that connection', async t => {
const f = fixture(t), original = { error: { code: 'insufficient_quota', type: 'billing_error', param: 'account',
message: 'Owned account quota. Echo owned-private-fidelity-key', api_key: 'unregistered-secret-value' }, request_id: 'body-request', usage: { prompt_tokens: 11, completion_tokens: 7 } };
const model = await compatibleFixture(t, () => ({ status: 429, headers: { 'X-Request-ID': 'upstream-owned-quota', 'Retry-After': '120',
'Set-Cookie': 'private-session=never-keep', 'X-Internal-Secret': 'never-keep' }, body: original }));
const provider = new Provider({ retryPolicy: policy }), { hub, connection } = configure(f, provider, model.baseUrl, { apiKey: 'owned-private-fidelity-key' });
await execute(f, provider);
assert.equal(model.requests.length, 1); assert.equal(f.session.status, 'failed');
const failure = f.session.failure, attempt = hub.view().attempts[0];
assert.equal(failure.origin, 'upstream'); assert.equal(failure.kind, 'quota'); assert.equal(failure.code, 'insufficient_quota');
assert.equal(failure.upstream.type, 'billing_error'); assert.equal(failure.upstream.param, 'account');
assert.equal(failure.upstream.requestId, 'upstream-owned-quota'); assert.notEqual(failure.wireAttemptId, failure.upstream.requestId);
assert.equal(failure.retryAfterMs, 120000); assert.equal(failure.retryable, false);
assert.equal(attempt.diagnostic.upstream.artifactId, attempt.responseArtifactId); assert.equal(attempt.status, 'failed');
const raw = f.store.artifactStore.read(f.store.owned('artifact', attempt.responseArtifactId, f.session.id));
assert.ok(raw.includes('insufficient_quota')); assert.ok(raw.includes('[已隐藏]'));
const publicData = JSON.stringify({ failure, attempt });
for (const secret of ['owned-private-fidelity-key', 'unregistered-secret-value', 'never-keep']) assert.ok(!publicData.includes(secret) && !raw.includes(secret));
assert.equal(hub.status(hub.connection(connection.id)).health.blocked, true);
assert.equal(f.store.core.tool.size, 0); assert.equal(f.store.core.run.get(f.session.runId).usage.unknownRequests, 0);
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 18);
f.store.close(); const reopened = new Store(f.data); try { assert.deepEqual(reopened.session(f.session.id).failure, failure); } finally { reopened.close(); }
});
test('Retry-After above 90 seconds remains exact and a short local budget cannot trigger an early retry', async () => {
let requests = 0;
const provider = new Provider({ retryPolicy: policy, fetch: async () => { requests++; return new Response(JSON.stringify({ error: { code: 'rate_limit_exceeded', message: 'slow down' } }),
{ status: 429, headers: { 'Retry-After': '180', 'X-Request-ID': 'owned-long-wait' } }); } });
await assert.rejects(provider.complete(config, [], null), error => {
assert.equal(error.diagnostic.retryAfterMs, 180000); assert.equal(error.diagnostic.retryDecision, 'wait_budget');
assert.equal(error.diagnostic.limitReason, 'rate'); assert.equal(error.diagnostic.upstream.retryAfter, '180'); return true;
});
assert.equal(requests, 1);
});
test('Retry-After date, expired date, huge value and invalid header retain explicit semantics', async () => {
const now = Date.parse('2026-11-01T00:00:00Z');
assert.equal(retryAfterMilliseconds('Sun, 01 Nov 2026 00:02:00 GMT', now), 120000);
assert.equal(retryAfterMilliseconds('Sat, 31 Oct 2026 00:00:00 GMT', now), 0);
for (const value of ['999999999999999999999999999999999', 'not-a-date']) {
let requests = 0;
const provider = new Provider({ retryPolicy: policy, fetch: async () => { requests++; return new Response('{"error":{"message":"limited"}}', { status: 429, headers: { 'Retry-After': value } }); } });
await assert.rejects(provider.complete(config, [], null), error => { assert.equal(error.diagnostic.upstream.retryAfter, value);
assert.equal(error.diagnostic.upstream.retryAfterInvalid, true); assert.equal(error.diagnostic.retryable, false); return true; });
assert.equal(requests, 1);
}
});
test('unclassified 429 remains unspecified and every bounded actual retry retains its own original error', async t => {
const f = fixture(t), provider = new Provider({ retryPolicy: { ...policy, attempts: 2 }, fetch: async () => new Response('{"error":{"message":"unspecified limit"}}',
{ status: 429, headers: { 'retry-after': '0' } }) });
f.store.saveConfig({ ...config }); await execute(f, provider);
assert.equal(f.session.failure.limitReason, 'unspecified'); assert.equal(f.session.failure.attempts, 2);
const artifacts = [...f.store.core.artifact.values()].filter(value => value.kind === 'model-http-response');
assert.equal(artifacts.length, 2); assert.equal(f.store.core.run.get(f.session.runId).usage.requests, 2);
});
test('oversized HTTP error saves exactly the bounded prefix and labels its incomplete range', async t => {
const f = fixture(t), original = Buffer.from('owned-error\n' + 'x'.repeat(ERROR_CAPTURE_LIMIT * 3));
const provider = new Provider({ retryPolicy: { ...policy, attempts: 1 }, fetch: async () => new Response(original, { status: 502, headers: { 'X-Request-ID': 'owned-large' } }) });
const { hub } = configure(f, provider, config.baseUrl); await execute(f, provider);
const attempt = hub.view().attempts[0], artifact = f.store.owned('artifact', attempt.responseArtifactId, f.session.id);
assert.equal(artifact.bytes, ERROR_CAPTURE_LIMIT); assert.equal(artifact.sha256, sha(original.subarray(0, ERROR_CAPTURE_LIMIT)));
assert.equal(attempt.upstream.capture.complete, false); assert.equal(attempt.upstream.capture.truncated, true);
assert.deepEqual(attempt.upstream.capture.capturedRange, [0, ERROR_CAPTURE_LIMIT]);
assert.equal(attempt.upstream.rawPreview.length, 8000); assert.equal(attempt.upstream.previewTruncated, true);
});
test('Chat SSE error preserves upstream fields, original failed event, partial tool and reported usage without executing or retrying', async t => {
const f = fixture(t), original = sse({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'partial', type: 'function', function: { name: 'propose_file', arguments: '{"path":' } }] } }] })
+ sse({ error: { code: 'rate_limit_exceeded', type: 'rate_limit_error', message: 'Owned error owned-sse-secret' }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
const model = await streamServer(t, original, { 'x-request-id': 'owned-sse-id' });
const provider = new Provider({ retryPolicy: policy }), { hub } = configure(f, provider, model.url, { apiKey: 'owned-sse-secret', streaming: true, capabilityDeclarations: { streaming: 'supported' } });
await execute(f, provider);
assert.equal(model.count(), 1); assert.equal(f.store.core.tool.size, 0); assert.equal(f.session.changes.length, 0);
assert.equal(f.session.failure.kind, 'rate_limit'); assert.equal(f.session.failure.origin, 'upstream'); assert.equal(f.session.failure.retryDecision, 'stream_not_replayed');
assert.equal(f.session.failure.model, 'owned-fidelity'); assert.equal(f.session.failure.endpoint, model.url); assert.equal(f.session.failure.route, '/chat/completions');
assert.equal(f.session.failure.upstream.requestId, 'owned-sse-id'); assert.ok(!JSON.stringify(f.session.failure).includes('owned-sse-secret'));
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'failed'); assert.equal(attempt.stream.terminal, false);
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 14);
assert.equal(f.session.failure.upstream.artifactId, attempt.responseArtifactId);
assert.equal(f.session.failure.upstream.artifactScope, 'sse-stream');
assert.ok(f.store.artifactStore.read(f.store.owned('artifact', attempt.responseArtifactId, f.session.id)).includes('"id":"partial"'));
});
for (const status of ['failed', 'incomplete']) test('Responses ' + status + ' SSE retains original event/response identity and usage', async t => {
const f = fixture(t), terminal = { id: 'resp-owned', status, usage: { input_tokens: 12, output_tokens: 5 },
...(status === 'failed' ? { error: { code: 'insufficient_quota', type: 'billing_error', message: 'Owned exhausted' } } : { incomplete_details: { reason: 'max_output_tokens' } }) };
const model = await streamServer(t, sse({ type: 'response.created', response: { id: 'resp-owned' }, sequence_number: 1 }) + sse({ type: 'response.' + status, response: terminal, sequence_number: 2 }));
const provider = new Provider({ retryPolicy: policy }), { hub } = configure(f, provider, model.url, { protocol: 'responses', streaming: true, capabilityDeclarations: { streaming: 'supported' } });
await execute(f, provider);
assert.equal(model.count(), 1); assert.equal(f.session.failure.origin, 'upstream'); assert.equal(f.session.failure.upstream.responseId, 'resp-owned');
assert.equal(f.session.failure.upstream.eventType, 'response.' + status); assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 17);
assert.equal(hub.view().attempts[0].status, 'failed'); assert.equal(f.store.core.tool.size, 0);
if (status === 'incomplete') assert.equal(f.session.failure.upstream.reason, 'max_output_tokens');
});
test('unsupported successful Responses output retains original JSON and usage with a local protocol origin', async t => {
const f = fixture(t), original = { id: 'resp-audio', object: 'response', status: 'completed', output: [{ type: 'audio', data: 'owned-data' }], usage: { input_tokens: 21, output_tokens: 3 } };
const model = await compatibleFixture(t, () => ({ body: original })), provider = new Provider({ retryPolicy: policy }), { hub } = configure(f, provider, model.baseUrl, { protocol: 'responses' });
await execute(f, provider);
assert.equal(f.session.failure.origin, 'local'); assert.equal(f.session.failure.kind, 'protocol'); assert.equal(model.requests.length, 1);
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'failed');
assert.equal(f.store.artifactStore.read(f.store.owned('artifact', attempt.responseArtifactId, f.session.id)), JSON.stringify(original));
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 24); assert.equal(f.store.core.tool.size, 0);
});
test('HTTP 200 native error is still an upstream failure rather than a fabricated successful answer', async () => {
const provider = new Provider({ retryPolicy: policy, fetch: async () => new Response('{"error":{"code":"insufficient_quota","message":"owned"}}') });
await assert.rejects(provider.complete(config, [], null), error => error.diagnostic.origin === 'upstream' && error.diagnostic.kind === 'quota' && error.diagnostic.httpStatus === 200);
});
test('JSON-escaped credential echoes are redacted in the original preview and capture as well as parsed fields', async () => {
const key = 'owned-quote-"-and-backslash-\\-key', progress = [];
const provider = new Provider({ retryPolicy: policy, fetch: async () => new Response(JSON.stringify({ error: { message: 'Echo ' + key, code: 'invalid_api_key' } }), { status: 401 }) });
provider.setKey(key);
await assert.rejects(provider.complete(config, [], null, undefined, value => progress.push(value)), error => {
assert.ok(!JSON.stringify(error.diagnostic).includes(JSON.stringify(key).slice(1, -1)));
assert.ok(error.diagnostic.upstream.rawPreview.includes('[已隐藏]')); return true;
});
assert.ok(progress.find(value => value.responseCapture).responseCapture.toString('utf8').includes('[已隐藏]'));
});
test('quota failover uses another independent pool member and never replays a failed stream', async t => {
const f = fixture(t), quota = await compatibleFixture(t, () => ({ status: 429, body: { error: { code: 'insufficient_quota', message: 'Owned empty' } } }));
const ready = await compatibleFixture(t, () => completion([], 'Owned completed'));
const provider = new Provider(), { hub, connection } = configure(f, provider, quota.baseUrl);
const other = hub.upsert({ name: 'Owned other', model: 'owned-other', baseUrl: ready.baseUrl, requiresKey: false });
const pool = hub.pool({ name: 'Owned failover', members: [{ connectionId: connection.id }, { connectionId: other.id }], maxAttempts: 3 }); hub.select({ type: 'pool', id: pool.id });
await execute(f, provider);
assert.equal(quota.requests.length, 1); assert.equal(ready.requests.length, 1); assert.equal(hub.view().attempts.length, 2);
assert.deepEqual(hub.view().attempts.map(value => value.status), ['completed', 'failed']);
assert.equal(f.store.core.run.get(f.session.runId).usage.requests, 2);
});
test('native parameters are isolated by pool member, preserve Responses state and bind the original output reservation', () => {
const a = { id: 'a', model: 'a-model', protocol: 'chat-completions', nativeParameters: { reasoning_effort: 'high', temperature: 0.4, seed: 3 }, outputTokenField: 'max_completion_tokens' };
const b = { id: 'b', model: 'b-model', protocol: 'responses', nativeParameters: { reasoning: { effort: 'low' }, text: { verbosity: 'high' }, parallel_tool_calls: false } };
const c = { id: 'c', model: 'c-model', protocol: 'chat-completions' };
const body = completionBody({ ...config, ...a, modelRouting: { members: [a, b, c] } }, [{ role: 'user', content: 'owned' }], []);
const chat = actualRequest(body, a), responses = actualRequest(body, b), plain = actualRequest(body, c);
assert.equal(chat.max_completion_tokens, 512); assert.equal(chat.max_tokens, undefined); assert.equal(outputReservation(chat), 512);
assert.equal(chat.reasoning_effort, 'high'); assert.equal(chat.seed, 3); assert.deepEqual(responses.reasoning, { effort: 'low' });
assert.equal(responses.temperature, undefined); assert.equal(responses.seed, undefined); assert.deepEqual(responses.include, ['reasoning.encrypted_content']);
assert.equal(responses.store, false); assert.equal(plain.reasoning_effort, undefined); assert.equal(plain.max_tokens, 512);
});
test('unsupported native fields and invalid values reject before dispatch instead of silently dropping or overriding constraints', () => {
for (const [protocol, params, field] of [['chat-completions', { model: 'override' }], ['responses', { tools: [] }], ['responses', { reasoning: { effort: 'high', extra: 1 } }],
['responses', { text: { format: { type: 'json' } } }], ['chat-completions', { temperature: '1' }], ['chat-completions', { top_p: 2 }], ['chat-completions', { seed: 1.5 }],
['chat-completions', { parallel_tool_calls: 'true' }], ['chat-completions', { reasoning_effort: 'invented' }], ['chat-completions', {}, 'max_output_tokens'],
['responses', {}, 'max_tokens'], ['responses', []], ['responses', { unknown: 'x'.repeat(8200) }]]) {
assert.throws(() => nativeParameterSettings(protocol, params, field), error => error.status === 400);
}
});
test('native settings for legacy and registered profiles persist without changing credentials or old records and reject stale edits', async t => {
const f = fixture(t), provider = new Provider(); provider.setKey('owned-legacy-key');
const { hub, connection } = configure(f, provider, config.baseUrl, { apiKey: 'owned-profile-key' });
const oldMessages = JSON.stringify(f.session), credential = provider.credentials.get(connection.id), oldPool = JSON.stringify(hub.state.pools);
hub.setNativeParameters({ id: connection.id, revision: connection.revision, nativeParameters: { reasoning_effort: 'medium' }, outputTokenField: 'max_completion_tokens' });
assert.equal(provider.credentials.get(connection.id), credential); assert.equal(JSON.stringify(f.session), oldMessages); assert.equal(JSON.stringify(hub.state.pools), oldPool);
assert.throws(() => hub.setNativeParameters({ id: connection.id, revision: connection.revision, nativeParameters: {} }), error => error.status === 409);
const legacy = hub.connection('legacy-default'); hub.setNativeParameters({ id: legacy.id, revision: legacy.revision, nativeParameters: { temperature: 0.5 } });
assert.equal(provider.apiKey, 'owned-legacy-key'); assert.deepEqual(f.store.config.nativeParameters, { temperature: 0.5 });
f.store.close(); const reopened = new Store(f.data); try { assert.deepEqual(reopened.config.nativeParameters, { temperature: 0.5 });
assert.deepEqual(new Provider().modelHub(reopened).connection(connection.id).nativeParameters, { reasoning_effort: 'medium' }); } finally { reopened.close(); }
});
test('compiled native settings API saves/restores exact parameters and rejects reserved fields without mutating the profile', async t => {
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
const member = await host.api('/model-center/connections', { name: 'Owned API', model: 'owned-api', protocol: 'responses', baseUrl: config.baseUrl, requiresKey: false });
await host.api('/model-center/native-parameters', { id: member.id, revision: member.revision, nativeParameters: { reasoning: { effort: 'high' }, parallel_tool_calls: false } });
const saved = (await host.api('/model-center')).connections.find(value => value.id === member.id);
assert.deepEqual(saved.nativeParameters, { reasoning: { effort: 'high' }, parallel_tool_calls: false });
await assert.rejects(host.api('/model-center/native-parameters', { id: member.id, revision: saved.revision, nativeParameters: { max_output_tokens: 99999 } }), error => error.status === 400);
assert.deepEqual((await host.api('/model-center')).connections.find(value => value.id === member.id), saved);
});
test('legacy connection test sends its saved native fields and selected output limit field without changing the configuration', async t => {
const f = fixture(t), model = await compatibleFixture(t, () => completion([], 'OK')); f.store.close();
const host = await launchWorkbench(t, f.data), original = (await host.api('/model-center')).connections.find(value => value.id === 'legacy-default');
await host.api('/model-center/native-parameters', { id: original.id, revision: original.revision, nativeParameters: { reasoning_effort: 'low' }, outputTokenField: 'max_completion_tokens' });
const before = await host.api('/model-center');
await host.api('/config/test', { baseUrl: model.baseUrl, model: 'owned-probe' });
assert.equal(model.requests.length, 1); assert.equal(model.requests[0].input.reasoning_effort, 'low');
assert.equal(model.requests[0].input.max_completion_tokens, 64); assert.equal(model.requests[0].input.max_tokens, undefined);
assert.deepEqual(await host.api('/model-center'), before);
});
@@ -0,0 +1,119 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {Provider} from '../src/server/provider.mjs';
import {Runtime} from '../src/server/runtime.mjs';
import {Store} from '../src/server/store.mjs';
import {fixture} from './helpers.mjs';
import {modelIdentityObserver,modelIdentitySettings} from '../src/shared/contracts/model-identity.mjs';
import {modelSnapshot} from '../src/shared/contracts/execution.mjs';
import {controlledWorker} from './fixtures/controlled-worker.mjs';
import {createDeliveryProject} from './fixtures/delivery-project.mjs';
import {DeliveryReporter} from '../src/core/delivery-report.mjs';
const config={mode:'compatible',baseUrl:'http://127.0.0.1:1/v1',model:'requested',maxOutputTokens:512};
const usage={prompt_tokens:13,completion_tokens:7,total_tokens:20};
const reply=model=>({model,choices:[{message:{role:'assistant',content:'已收到。'}}],usage});
const frame=value=>'data: '+JSON.stringify(value)+'\n\n';
const stream=(models,protocol)=>protocol==='responses'
? frame({type:'response.created',response:{id:'r',model:models[0]}})+frame({type:'response.completed',response:{id:'r',model:models.at(-1),status:'completed',output:[{type:'message',role:'assistant',content:[{type:'output_text',text:'已收到。'}]}],usage:{input_tokens:13,output_tokens:7,total_tokens:20}}})
: models.map((model,i)=>frame({model,choices:[{index:0,delta:{content:i?'':'已收到。'},finish_reason:i===models.length-1?'stop':null}],...(i===models.length-1?{usage}:{})})).join('')+'data: [DONE]\n\n';
test('identity distinguishes missing, invalid, mixed, exact and explicitly declared aliases without guessing',()=>{
assert.deepEqual(modelIdentitySettings({}),{});
for(const [values,status]of [[[], 'missing'],[['requested'],'matched'],[['other'],'mismatch'],[['requested','other'],'mixed'],[[null],'invalid']]){
const o=modelIdentityObserver('requested');for(const model of values)o.observe({model});assert.equal(o.snapshot(true).status,status);
}
assert.throws(()=>modelIdentitySettings({modelIdentityAliases:['snapshot']}),/依据/);
assert.throws(()=>modelIdentitySettings({modelIdentityAliases:['a','a'],modelIdentityAliasSource:'owned'}),/不重复/);
const o=modelIdentityObserver('requested',{modelIdentityAliases:['snapshot'],modelIdentityAliasSource:'Owned supplier mapping, revision 1'});
o.observe({model:'snapshot'});assert.equal(o.snapshot(true).status,'mapped');assert.equal(o.snapshot(false).status,'incomplete');
assert.equal(o.snapshot(true).mapping.kind,'user-declared');
});
for(const protocol of ['chat-completions','responses'])for(const streaming of [false,true]){
test(protocol+' '+(streaming?'SSE':'JSON')+' rejects a different model before tool dispatch, retaining source and reported usage',async t=>{
const f=fixture(t);let calls=0;
const provider=new Provider({fetch:async()=>{calls++;return new Response(streaming?stream(['other'],protocol):JSON.stringify(protocol==='responses'
? {model:'other',status:'completed',output:[{type:'message',role:'assistant',content:[{type:'output_text',text:'已收到。'}]}],usage:{input_tokens:13,output_tokens:7,total_tokens:20}}:reply('other')),
{headers:{'Content-Type':streaming?'text/event-stream':'application/json','x-request-id':'owned-identity'}});}});
const hub=provider.modelHub(f.store),c=hub.upsert({name:'Identity',model:'requested',baseUrl:config.baseUrl,requiresKey:false,protocol,streaming,
modelIdentityPolicy:'require-match',capabilityDeclarations:{text:'supported',tools:'supported',streaming:'supported'}});hub.select({type:'connection',id:c.id});
const runtime=new Runtime(f.store,provider);f.onCleanup(()=>runtime.stopAll());runtime.start(f.session.id,'读取当前项目。');await runtime.active.get(f.session.id).promise;
assert.equal(calls,1);assert.equal(f.session.status,'paused');assert.equal(f.store.core.run.get(f.session.runId).stopReason,'model_identity');
assert.equal(f.store.core.tool.size,0);assert.equal(f.session.failure.kind,'model_identity');
const attempt=hub.view().attempts[0],run=f.store.core.run.get(f.session.runId),identity=run.consumption[0].modelIdentity;
assert.equal(attempt.modelIdentity.status,'mismatch');assert.equal(identity.status,'mismatch');assert.equal(identity.requestedModel,'requested');
assert.deepEqual(identity.reportedModels,['other']);assert.equal(run.usage.totalTokens,20);assert.equal(run.usage.unknownRequests,0);
assert.equal(identity.responseSource.artifactId,attempt.responseArtifactId);assert.equal(identity.responseSource.sha256,attempt.responseSha256);
assert.ok(f.store.artifactStore.read(f.store.owned('artifact',attempt.responseArtifactId,f.session.id)).includes('other'));
assert.equal(hub.view().connections.find(v=>v.id===c.id).health.blocked,true);
f.store.close();const reopened=new Store(f.data);try{assert.deepEqual(reopened.core.run.get(run.id).consumption,run.consumption);}finally{reopened.close();}
});
}
test('record policy accepts missing identity but keeps it explicit, and strict declared mapping is frozen in the consumption',async()=>{
for(const settings of [{},{modelIdentityPolicy:'require-match',modelIdentityAliases:['snapshot'],modelIdentityAliasSource:'Owned supplier mapping'}]){
const events=[];const provider=new Provider({fetch:async()=>new Response(JSON.stringify(reply(settings.modelIdentityPolicy?'snapshot':undefined)))});
await provider.complete({...config,...settings},[],null,undefined,e=>events.push(e));
const r=events.find(e=>e.consumption).consumption;assert.equal(r.modelIdentity.status,settings.modelIdentityPolicy?'mapped':'missing');assert.equal(r.usage.total_tokens,20);
}
});
test('mixed streamed identities cannot pass strict mode, and interrupted streams retain observed identifiers without authenticating them',async()=>{
for(const interrupted of [false,true]){
const events=[];let calls=0;
const provider=new Provider({fetch:async()=>{calls++;return new Response(interrupted?frame({model:'requested',choices:[{index:0,delta:{content:'partial'}}]}):stream(['requested','other']),{headers:{'Content-Type':'text/event-stream'}});}});
await assert.rejects(provider.complete({...config,streaming:true,modelIdentityPolicy:'require-match'},[],null,undefined,e=>events.push(e)));
const r=events.find(e=>e.consumption).consumption;assert.equal(calls,1);assert.equal(r.modelIdentity.status,interrupted?'incomplete':'mixed');
assert.equal(r.modelIdentity.complete,!interrupted);assert.ok(r.modelIdentity.reportedModels.includes('requested'));
}
});
test('identity refusal never fails over to another pool member and raw response model cannot leak known credentials',async t=>{
const f=fixture(t),secret='owned-identity-private-key';let calls=0;
const provider=new Provider({fetch:async()=>{calls++;return new Response(JSON.stringify(reply(secret)));}}),hub=provider.modelHub(f.store);
const a=hub.upsert({name:'A',model:'requested',baseUrl:config.baseUrl,apiKey:secret,modelIdentityPolicy:'require-match'});
const b=hub.upsert({name:'B',model:'requested',baseUrl:config.baseUrl,requiresKey:false});
const pool=hub.pool({name:'Identity pool',members:[{connectionId:a.id},{connectionId:b.id}],maxAttempts:2});hub.select({type:'pool',id:pool.id});
const runtime=new Runtime(f.store,provider);f.onCleanup(()=>runtime.stopAll());runtime.start(f.session.id,'读取当前项目。');await runtime.active.get(f.session.id).promise;
assert.equal(calls,1);assert.equal(f.session.status,'paused');assert.ok(!JSON.stringify({view:hub.view(),failure:f.session.failure,run:f.store.core.run.get(f.session.runId)}).includes(secret));
});
test('strict refusal remains visible even when the same original response exhausts the budget',async t=>{
const f=fixture(t),provider=new Provider({fetch:async()=>new Response(JSON.stringify({...reply('other'),usage:{total_tokens:9999999}}))});
const hub=provider.modelHub(f.store),c=hub.upsert({name:'Strict',model:'requested',baseUrl:config.baseUrl,requiresKey:false,modelIdentityPolicy:'require-match'});
hub.select({type:'connection',id:c.id});const runtime=new Runtime(f.store,provider);f.onCleanup(()=>runtime.stopAll());
runtime.start(f.session.id,'读取项目。');await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status,'paused');assert.equal(f.session.failure.kind,'model_identity');
assert.equal(f.store.core.run.get(f.session.runId).stopReason,'model_identity');assert.equal(f.session.failure.budgetStopReason,'token_limit');
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens,9999999);
});
test('strict identity crosses the controlled engine bridge with its original source and zero tool dispatch',async t=>{
const f=fixture(t),context=controlledWorker(f);let calls=0;
context.provider.complete=Provider.prototype.complete.bind(context.provider);
context.provider.fetcher=async()=>{calls++;return new Response(JSON.stringify(reply('other')));};
const hub=context.provider.modelHub(f.store),c=hub.upsert({name:'Strict engine',model:'requested',baseUrl:config.baseUrl,requiresKey:false,modelIdentityPolicy:'require-match'});
hub.select({type:'connection',id:c.id});
const group=await context.manager.plan(f.session,context.definition);
await(await context.manager.prepare(f.session,group.id,group.fingerprint)).promise;
await(await context.manager.start(f.session,group.id,group.fingerprint)).promise;
const session=f.store.session(group.children[0].sessionId),run=f.store.core.run.get(session.runId);
assert.equal(calls,1);assert.equal(session.status,'paused');assert.equal(run.stopReason,'model_identity');
assert.equal(run.usage.totalTokens,20);assert.equal(run.usage.unknownRequests,0);
const job=[...f.store.core.engine_run.values()].find(v=>v.runId===run.id);
assert.equal(job.calls.length,0);assert.equal(job.requests[0].modelIdentity.status,'mismatch');
assert.equal(job.requests[0].modelIdentity.responseSource.sha256,run.consumption[0].modelIdentity.responseSource.sha256);
assert.equal(job.native.cleanup.rootExited,true);assert.equal(job.requiresVerification,false);
});
test('frozen direct connection policy and delivery reports preserve response identity without filling historical gaps',async t=>{
const settings={modelIdentityPolicy:'require-match',modelIdentityAliases:['snapshot'],modelIdentityAliasSource:'Owned revision 1'};
const frozen=modelSnapshot({...config,...settings});settings.modelIdentityAliases.push('later');assert.deepEqual(frozen.modelIdentityAliases,['snapshot']);
const f=fixture(t);createDeliveryProject(f);const run=f.store.core.run.get(f.session.runId),observer=modelIdentityObserver('requested');observer.observe({model:'other'});
run.consumption=[{attemptId:'owned',model:'requested',modelIdentity:observer.snapshot(true),usageStatus:'reported',usage}];
let report=await new DeliveryReporter(f.store).create(f.session);
assert.equal(report.report.runs.at(-1).consumption[0].modelIdentity.status,'mismatch');
assert.match(f.store.artifactStore.read(f.store.core.artifact.get(report.markdownArtifactId)),/请求与返回型号不一致/);
delete run.consumption;run.usage.requests=1;report=await new DeliveryReporter(f.store).create(f.session);
assert.match(f.store.artifactStore.read(f.store.core.artifact.get(report.markdownArtifactId)),/历史记录未保存返回型号核对/);
});
@@ -0,0 +1,147 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
import { fixture, compatibleFixture, completion, until, assertToolPairs } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
import { completionBody, requestMetrics } from '../src/shared/contracts/model-request.mjs';
import { ConfigImporter } from '../src/core/models/config-import.mjs';
import { verifyEngineModelInput } from '../src/shared/contracts/engine-execution.mjs';
import { engineCanonicalDigest } from '../src/shared/contracts/engines.mjs';
test('saved engine budgets verify all Chat/Responses route variants and reject a mismatched output allowance', t => {
const f = fixture(t), run = f.store.beginRun(f.session, 'Owned engine input proof', false, f.store.config);
const owner = { sessionId: f.session.id, projectId: f.project.id, taskId: run.taskId, runId: run.id };
const native = { messages: [], maxTokens: 512 };
const verify = body => {
const metrics = requestMetrics(body), request = { id: 'owned', bridgeTraceId: 'owned-trace', sourceSha256: engineCanonicalDigest(native),
requestSha256: metrics.sha256, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens,
reservedPerAttempt: metrics.estimatedInputTokens + 128 + native.maxTokens, maxAttempts: 1 };
const artifact = f.store.artifactStore.write(f.session, run, { owner, native, body, plan: request }, { kind: 'engine-model-input' });
f.store.core.artifact.set(artifact.id, artifact); request.inputArtifactId = artifact.id; request.inputSha256 = artifact.sha256;
return () => verifyEngineModelInput(f.store, f.session, owner, request);
};
const config = { model: 'owned', maxOutputTokens: 512 };
for (const protocol of ['chat-completions', 'responses']) assert.doesNotThrow(verify(completionBody({ ...config, protocol }, [], [])));
const mixed = completionBody({ ...config, modelRouting: { members: [{ id: 'chat', model: 'owned', protocol: 'chat-completions' },
{ id: 'responses', model: 'owned', protocol: 'responses' }] } }, [], []);
assert.doesNotThrow(verify(mixed)); mixed.variants[1].body.max_output_tokens++;
assert.throws(verify(mixed), /预算核对失败/);
});
test('owned CC Switch SQLite imports the selected TOML Responses wire_api and leaves unknown clients untouched', t => {
const f = fixture(t), file = path.join(f.root, 'owned-switch.db'), db = new DatabaseSync(file);
db.exec('CREATE TABLE providers (id TEXT,app_type TEXT,name TEXT,settings_config TEXT)');
const insert = db.prepare('INSERT INTO providers VALUES(?,?,?,?)');
insert.run('responses', 'codex', 'Owned Responses', JSON.stringify({ config: 'model = "owned"\nmodel_provider = "own"\n[model_providers.own]\nbase_url = "http://127.0.0.1:12345/v1"\nwire_api = "responses"', auth: { OPENAI_API_KEY: 'owned-import-key' } }));
insert.run('unknown', 'workbuddy', 'Unknown format', '{}'); db.close();
const original = fs.readFileSync(file), hub = new Provider().modelHub(f.store), importer = new ConfigImporter(hub);
const preview = importer.preview('ccswitch', file);
assert.equal(preview.entries.length, 1); assert.equal(preview.entries[0].protocol, 'responses');
assert.equal(JSON.stringify(preview).includes('owned-import-key'), false); assert.equal(preview.notices.length, 1);
const result = importer.apply(preview.id); assert.equal(hub.connection(result.imported[0]).protocol, 'responses');
assert.deepEqual(fs.readFileSync(file), original);
});
const response = (output, extra = {}) => ({ body: { id: 'resp_owned', status: 'completed', output, usage: { input_tokens: 137, output_tokens: 29, total_tokens: 166 }, ...extra } });
const message = text => ({ type: 'message', id: 'msg_owned', status: 'completed', role: 'assistant', content: [{ type: 'output_text', text }] });
const fn = (id, name = 'list_files', args = '{"path":"","depth":1}') => ({ type: 'function_call', id: 'fc_' + id, call_id: id, status: 'completed', name, arguments: args });
const connect = (hub, server, protocol = 'responses', extra = {}) => hub.upsert({ name: 'Owned ' + protocol, model: 'owned-model', protocol,
baseUrl: server.baseUrl, requiresKey: false, ...extra });
const runUntilIdle = async (runtime, session) => { runtime.start(session.id, 'Inspect files and finish.'); await until(() => !runtime.isActive(session.id), 15000); };
test('Responses executes one original tool through Runtime, carries encrypted Items, reports raw usage and seals exact wire bytes', async t => {
const f = fixture(t), reasoning = { type: 'reasoning', id: 'rs_owned', summary: [], encrypted_content: 'owned-encrypted-reasoning' };
const server = await compatibleFixture(t, (_url, body) => body.input.some(item => item.type === 'function_call_output')
? response([message('工程读取完成')]) : response([reasoning, fn('owned_read')]));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = connect(hub, server);
hub.select({ type: 'connection', id: connection.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(server.requests.length, 2); assertToolPairs(f.session.providerMessages);
assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
assert.deepEqual(server.requests[1].input.input.find(item => item.type === 'reasoning'), reasoning);
assert.equal(server.requests[1].input.input.find(item => item.type === 'function_call_output').call_id, 'owned_read');
const usage = f.store.core.run.get(f.session.runId).usage;
assert.equal(usage.inputTokens, 274); assert.equal(usage.outputTokens, 58);
for (const attempt of hub.view().attempts) {
const sent = server.requests.find(item => createHash('sha256').update(JSON.stringify(item.input)).digest('hex') === attempt.requestSha256);
assert.ok(sent); assert.equal(sent.url, '/v1/responses'); assert.equal(sent.input.store, false);
assert.equal(sent.input.tools[0].type, 'function'); assert.equal(sent.input.tools[0].strict, false);
assert.equal(sent.input.tools[0].function, undefined); assert.equal(attempt.protocol, 'responses');
assert.equal(attempt.usage.raw.input_tokens, 137); assert.equal(attempt.runId, f.session.runId);
const artifact = f.store.owned('artifact', attempt.requestArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.requestSha256); assert.equal(attempt.requestBytes, Buffer.byteLength(JSON.stringify(sent.input)));
}
for (const plan of [...f.store.core.context_plan.values()]) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('mixed-protocol pool keeps both actual variants and changes route on 429 without re-executing completed tools', async t => {
const f = fixture(t), limited = await compatibleFixture(t, () => ({ status: 429, body: { error: { message: 'owned rate limit' } } }));
const good = await compatibleFixture(t, (_url, body) => body.input.some(item => item.type === 'function_call_output') ? response([message('Done')]) : response([fn('mixed_read')]));
const provider = new Provider(), hub = provider.modelHub(f.store), first = connect(hub, limited, 'chat-completions'), second = connect(hub, good);
const pool = hub.pool({ name: 'Mixed protocol pool', members: [{ connectionId: first.id }, { connectionId: second.id }] });
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(limited.requests.length, 1); assert.equal(good.requests.length, 2);
assert.ok(limited.requests[0].input.messages); assert.ok(good.requests[0].input.input);
assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
const plan = [...f.store.core.context_plan.values()][0], body = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(plan.requestArtifactId)));
assert.equal(body.kind, 'model-route-plan'); assert.equal(body.variants.length, 2);
assert.equal(plan.estimatedInputTokens, Math.max(...body.variants.map(item => requestMetrics(item.body).estimatedInputTokens)));
assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool', async t => {
for (const value of [response([fn('partial')], { status: 'incomplete' }), response([fn('failed')], { status: 'failed' }),
response([{ ...fn('bad'), call_id: '' }]), response([{ type: 'computer_call', id: 'not-supported' }])]) {
await t.test('Reject ' + JSON.stringify(value).slice(0, 100), async sub => {
const f = fixture(sub), server = await compatibleFixture(sub, () => value), provider = new Provider(), hub = provider.modelHub(f.store);
const connection = connect(hub, server); hub.select({ type: 'connection', id: connection.id });
const runtime = new Runtime(f.store, provider); sub.after(() => runtime.stopAll()); await runUntilIdle(runtime, f.session);
assert.notEqual(f.session.status, 'completed'); assert.equal(server.requests.length, 1);
assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
assert.equal(hub.view().attempts[0].status, 'failed');
});
}
});
test('invalid JSON in a nonstream Chat or Responses tool group prevents every tool and pool replay', async t => {
for (const protocol of ['chat-completions', 'responses']) await t.test(protocol, async sub => {
const f = fixture(sub), value = protocol === 'responses' ? response([fn('valid'), fn('broken', 'search_files', '{invalid')])
: { choices: [{ message: { role: 'assistant', content: '', tool_calls: [
{ id: 'valid', type: 'function', function: { name: 'list_files', arguments: '{"path":""}' } },
{ id: 'broken', type: 'function', function: { name: 'search_files', arguments: '{invalid' } },
] }, finish_reason: 'tool_calls' }] };
const server = await compatibleFixture(sub, () => value), unused = await compatibleFixture(sub, () => { throw new Error('Must not replay'); });
const provider = new Provider(), hub = provider.modelHub(f.store), first = connect(hub, server, protocol), second = connect(hub, unused, protocol);
const pool = hub.pool({ name: 'Owned validation boundary', members: [{ connectionId: first.id }, { connectionId: second.id }] });
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); sub.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(server.requests.length, 1); assert.equal(unused.requests.length, 0); assert.equal(f.store.core.tool.size, 0);
assert.notEqual(f.session.status, 'completed'); assert.equal(hub.view().attempts[0].status, 'failed');
});
});
test('wire adapter preserves tool IDs but never passes Responses-private Items to Chat or another endpoint', () => {
const messages = [{ role: 'user', content: 'Inspect' }, { role: 'assistant', content: '', tool_calls: [{ id: 'call_own', type: 'function', function: { name: 'read_file', arguments: '{"path":"README.md"}' } }],
providerState: { protocol: 'responses', endpoint: 'https://owned.invalid/v1', model: 'm', items: [{ type: 'reasoning', encrypted_content: 'owned-private' }] } },
{ role: 'tool', tool_call_id: 'call_own', content: '{"result":"owned"}' }];
const config = { model: 'm', baseUrl: 'https://other.invalid/v1', maxOutputTokens: 512 };
const chat = completionBody(config, messages, []); assert.equal(chat.messages[1].providerState, undefined);
const native = completionBody({ ...config, protocol: 'responses' }, messages, []);
assert.equal(JSON.stringify(native).includes('owned-private'), false);
assert.equal(native.input.find(item => item.type === 'function_call').call_id, 'call_own');
assert.equal(native.input.find(item => item.type === 'function_call_output').call_id, 'call_own');
});
test('connection JSON import accepts explicit Responses protocol and continues to reject unknown protocols without reading client sessions', t => {
const f = fixture(t), hub = new Provider().modelHub(f.store), importer = new ConfigImporter(hub), file = path.join(f.root, 'owned-connections.json');
fs.writeFileSync(file, JSON.stringify({ schema: 1, connections: [{ ...connectInput(), protocol: 'responses' }, { ...connectInput(), protocol: 'unknown' }] }));
const preview = importer.preview('workbench-json', file); assert.equal(preview.entries.length, 1); assert.equal(preview.entries[0].protocol, 'responses'); assert.equal(preview.notices.length, 1);
const result = importer.apply(preview.id); assert.equal(hub.connection(result.imported[0]).protocol, 'responses');
});
function connectInput() { return { name: 'Owned import', model: 'owned-model', baseUrl: 'http://127.0.0.1:12345/v1', requiresKey: false }; }
@@ -0,0 +1,114 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {modelReservation,localModelEstimate,verifyModelReservation} from '../src/shared/contracts/model-reservation.mjs';
import {completionBody} from '../src/shared/contracts/model-request.mjs';
import {Provider} from '../src/server/provider.mjs';
import {Runtime} from '../src/server/runtime.mjs';
import {fixture,compatibleFixture,completion,call} from './helpers.mjs';
import {controlledWorker} from './fixtures/controlled-worker.mjs';
import {verifyEngineModelInput} from '../src/shared/contracts/engine-execution.mjs';
const config={mode:'compatible',model:'owned-A',protocol:'chat-completions',baseUrl:'http://127.0.0.1:1/v1',maxOutputTokens:512,contextLimit:16000};
const body=completionBody(config,[{role:'user',content:'Owned bounded input.'}],[]);
const observed=(extra={})=>({attemptId:'owned-attempt',runId:'owned-run',model:config.model,protocol:config.protocol,endpoint:config.baseUrl,
connectionId:null,outcome:'completed',usageStatus:'reported',localEstimate:localModelEstimate(body),
usage:{prompt_tokens:4000,completion_tokens:800,total_tokens:4800},...extra});
test('reservation uses actual known usage from the same route, never lowers estimates or rewrites original receipts',()=>{
const receipts=[observed(),observed({attemptId:'unknown',usageStatus:'unknown',usage:{total_tokens:9999999}}),
observed({attemptId:'other',model:'other-model',usage:{total_tokens:9999999}}),observed({attemptId:'legacy',localEstimate:undefined,usage:{total_tokens:9999999}})];
const original=structuredClone(receipts),reservation=modelReservation(config,body,receipts,128);
assert.equal(reservation.reservedTokens,4928);assert.equal(reservation.routes[0].sources.length,1);
assert.ok(reservation.routes[0].inputRatio>1);assert.deepEqual(receipts,original);
const small=modelReservation(config,body,[observed({usage:{prompt_tokens:1,completion_tokens:1,total_tokens:2}})]);
assert.equal(small.reservedTokens,localModelEstimate(body).inputTokens+512);
const pool={...config,modelRouting:{members:[{id:'A',model:config.model,protocol:config.protocol,baseUrl:config.baseUrl},
{id:'B',model:'owned-B',protocol:config.protocol,baseUrl:'http://127.0.0.1:2/v1'}]}};
const mixed=modelReservation(pool,completionBody(pool,[{role:'user',content:'Owned bounded input.'}],[]),[observed({connectionId:'A'})]);
assert.equal(mixed.routes[0].sources.length,1);assert.equal(mixed.routes[1].sources.length,0);assert.equal(mixed.routes[1].inputRatio,1);
});
test('reservation verification binds original Task/Run consumption and rejects changed usage or foreign provenance',()=>{
const receipt=observed(),run={id:'owned-run',taskId:'owned-task',sessionId:'owned-session',consumption:[receipt]},session={id:'owned-session'};
const store={owned(kind,id,sessionId){assert.equal(kind,'run');assert.equal(id,run.id);assert.equal(sessionId,run.sessionId);return run;}};
const reservation=modelReservation(config,body,[receipt],128);
assert.deepEqual(verifyModelReservation(store,session,'owned-task',config,body,reservation),reservation);
const altered=structuredClone(reservation);altered.routes[0].inputRatio=1;
assert.throws(()=>verifyModelReservation(store,session,'owned-task',config,body,altered),/不一致/);
assert.throws(()=>verifyModelReservation(store,session,'other-task',config,body,reservation),/当前Task/);
run.consumption[0]={...receipt,usage:{...receipt.usage,total_tokens:4900}};
assert.throws(()=>verifyModelReservation(store,session,'owned-task',config,body,reservation),/不一致/);
});
test('known high input calibrates the next request and stops before dispatch while preserving the completed read',async t=>{
const f=fixture(t),model=await compatibleFixture(t,()=>({...completion([call('owned-read','read_file',{path:'README.md'})]),
usage:{prompt_tokens:45000,completion_tokens:100,total_tokens:45100}}));
f.store.saveConfig({...config,baseUrl:model.baseUrl,maxRunTokens:70000,maxTaskTokens:1000000});
const provider=new Provider(),runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
runtime.start(f.session.id,'Read README. Do not modify files or run checks.');await runtime.active.get(f.session.id).promise;
const run=f.store.core.run.get(f.session.runId),first=structuredClone(run.consumption[0]);
assert.equal(model.requests.length,1);assert.equal(f.session.status,'paused');assert.equal(run.stopReason,'token_limit');
assert.equal(run.usage.totalTokens,45100);assert.equal(run.usage.unknownRequests,0);
assert.equal(f.session.events.filter(event=>event.tool==='read_file'&&event.phase==='completed').length,1);
assert.ok(run.budgetPause.tokenReservation.reservedTokens>run.budgetPause.remainingTokens);
assert.equal(run.budgetPause.tokenReservation.routes[0].sources[0].attemptId,first.attemptId);
assert.deepEqual(run.consumption[0],first);assert.ok(first.localEstimate.requestSha256);
});
for(const mode of ['legacy','pool','connection'])test('one reported overrun stops '+mode+' requests and keeps actual use and excess',async t=>{
const f=fixture(t),model=await compatibleFixture(t,()=>({status:503,body:{error:{message:'owned failed request'},usage:{prompt_tokens:55000,completion_tokens:17,total_tokens:55017}}}));
f.store.saveConfig({...config,baseUrl:model.baseUrl,maxRunTokens:35000,maxTaskTokens:1000000});
const provider=new Provider({retryPolicy:{delays:[1,1]}});
if(mode!=='legacy'){const hub=provider.modelHub(f.store),one=hub.upsert({name:'owned first',model:config.model,baseUrl:model.baseUrl,requiresKey:false});
if(mode==='connection')hub.select({type:'connection',id:one.id});
else{
const two=hub.upsert({name:'owned unused second',model:'owned-B',baseUrl:model.baseUrl,requiresKey:false});
const pool=hub.pool({name:'owned overrun',members:[{connectionId:one.id},{connectionId:two.id}]});hub.select({type:'pool',id:pool.id});}}
const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
runtime.start(f.session.id,'Read README without changing files.');await runtime.active.get(f.session.id).promise;
const run=f.store.core.run.get(f.session.runId);
assert.equal(model.requests.length,1);assert.equal(f.session.status,'paused');assert.equal(run.stopReason,'token_limit');
assert.equal(run.consumption.length,1);assert.equal(run.usage.totalTokens,55017);assert.equal(run.usage.unknownRequests,0);
assert.equal(run.budgetPause.tokenAccounting.run.exceededTokens,20017);assert.equal(f.store.core.tool.size,0);
if(mode!=='legacy')assert.equal(provider.modelHub(f.store).view().attempts.length,1);
if(mode==='connection')assert.equal(run.budgetPause.modelFailure.httpStatus,503);
});
for(const mode of ['legacy','pool'])test('reported usage below the limit prevents an unaffordable '+mode+' retry before another dispatch',async t=>{
const f=fixture(t),model=await compatibleFixture(t,()=>({status:503,body:{error:{message:'owned response below maximum'},
usage:{prompt_tokens:45000,completion_tokens:100,total_tokens:45100}}}));
f.store.saveConfig({...config,baseUrl:model.baseUrl,maxRunTokens:70000,maxTaskTokens:1000000});
const provider=new Provider({retryPolicy:{delays:[1,1]}});
if(mode==='pool'){
const hub=provider.modelHub(f.store),one=hub.upsert({name:'owned charged route',model:config.model,baseUrl:model.baseUrl,requiresKey:false}),
two=hub.upsert({name:'owned unused route',model:'owned-B',baseUrl:model.baseUrl,requiresKey:false});
const pool=hub.pool({name:'owned remaining budget',members:[{connectionId:one.id},{connectionId:two.id}]});hub.select({type:'pool',id:pool.id});
}
const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
runtime.start(f.session.id,'Read README without changing files.');await runtime.active.get(f.session.id).promise;
const run=f.store.core.run.get(f.session.runId);
assert.equal(model.requests.length,1);assert.equal(f.session.status,'paused');assert.equal(run.stopReason,'token_limit');
assert.equal(run.usage.totalTokens,45100);assert.equal(run.usage.unknownRequests,0);assert.equal(run.consumption.length,1);
assert.equal(run.budgetPause.remainingTokens,24900);assert.ok(run.budgetPause.tokenReservation.reservedTokens>24900);
assert.equal(run.budgetPause.modelFailure.httpStatus,503);assert.equal(run.budgetPause.tokenAccounting.run.exceededTokens,0);
if(mode==='pool')assert.equal(provider.modelHub(f.store).view().attempts.length,1);
});
test('native bridge seals calibrated refusal with original consumption and preserves versioned request verification',async t=>{
const f=fixture(t),model=await compatibleFixture(t,()=>({...completion([call('native-owned-read','read_file',{path:'README.md'})]),
usage:{prompt_tokens:45000,completion_tokens:100,total_tokens:45100}})),context=controlledWorker(f);
context.provider.complete=Provider.prototype.complete.bind(context.provider);
f.store.saveConfig({...config,baseUrl:model.baseUrl,maxRunTokens:70000,maxTaskTokens:1000000});
context.definition.budget.maxTokens=70000;
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);
await (await context.manager.start(f.session,group.id,group.fingerprint)).promise;
const job=[...f.store.core.engine_run.values()].find(value=>value.collaborationId===group.id),session=f.store.session(job.sessionId),run=f.store.core.run.get(job.runId);
assert.equal(model.requests.length,1);assert.equal(group.status,'paused');assert.equal(run.stopReason,'engine_input_budget',JSON.stringify({budget:run.budget,usage:run.usage,
task:f.store.core.task.get(run.taskId).usage,requests:job.requests.map(value=>({accounted:value.accountedAttempts,observed:value.observedAttempts,max:value.maxAttempts,estimate:value.reservedPerAttempt,status:value.status})),rejections:job.inputRejections?.map(value=>({remaining:value.remainingTokens,reserved:value.tokenReservation?.reservedTokens})),error:job.error}));
assert.equal(job.calls.length,1);assert.equal(job.calls[0].status,'returned');assert.equal(job.requests[0].reservationVersion,1);
assert.doesNotThrow(()=>verifyEngineModelInput(f.store,session,job,job.requests[0]));
const refusal=job.inputRejections[0];assert.ok(refusal.tokenReservation.reservedTokens>refusal.remainingTokens);
assert.equal(refusal.tokenReservation.routes[0].sources[0].attemptId,run.consumption[0].attemptId);
assert.equal(run.usage.totalTokens,45100);assert.equal(run.usage.requests,1);assert.equal(run.usage.unknownRequests,0);
});
@@ -0,0 +1,57 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { fixture, compatibleFixture, completion, call, until } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
const add = (hub, server, name, declarations) => hub.upsert({ name, model: 'owned-model', baseUrl: server.baseUrl, requiresKey: false, capabilityDeclarations: declarations });
test('capability routing skips text-only members before acquiring a lease and records exact refusal in the original Run', async t => {
const f = fixture(t), text = await compatibleFixture(t, () => completion([], 'Text only')), tools = await compatibleFixture(t, () => completion([], 'Done'));
const provider = new Provider(), hub = provider.modelHub(f.store), a = add(hub, text, 'Text only', { tools: 'unsupported' }), b = add(hub, tools, 'Code tools', { tools: 'supported' });
const pool = hub.pool({ name: 'Ability route', members: [{ connectionId: a.id, weight: 10 }, { connectionId: b.id }] }); hub.select({ type: 'pool', id: pool.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
assert.equal(f.session.status, 'completed'); assert.equal(text.requests.length, 0); assert.equal(tools.requests.length, 1);
const attempt = hub.view().attempts[0]; assert.equal(attempt.runId, f.session.runId); assert.equal(attempt.rejections[0].connectionId, a.id); assert.equal(attempt.rejections[0].capability, 'tools');
});
test('all incompatible members reject with no network request, no lease, preserved Task and explicit non-dispatched ledger', async t => {
const f = fixture(t), server = await compatibleFixture(t, () => completion());
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Unknown tools', { tools: 'unknown' }); hub.select({ type: 'connection', id: connection.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
assert.equal(server.requests.length, 0); assert.equal(f.session.status, 'failed');
assert.equal(hub.view().attempts[0].status, 'rejected'); assert.equal(hub.view().attempts[0].usageStatus, 'not_dispatched');
assert.equal(hub.view().attempts[0].runId, f.session.runId); assert.equal([...hub.leases.values()].reduce((a, b) => a + b, 0), 0);
});
test('explicit synthetic tool probe verifies only actual matching call, never executes it and loses verification on revision changes', async t => {
const f = fixture(t), server = await compatibleFixture(t, (_url, body) => {
const nonce = body.messages.at(-1).content.match(/nonce ([a-f0-9-]+)/)?.[1];
return completion([call('probe_owned', 'workbench_capability_echo', { nonce })]);
});
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Probe owned', { tools: 'unknown' });
const result = await hub.probe(connection.id, 'tools'); assert.equal(result.ok, true); assert.equal(server.requests.length, 1);
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'verified');
assert.equal(f.session.events.length, 0); assert.equal(f.store.core.tool.size, 0);
const current = hub.connection(connection.id); hub.upsert({ ...current, name: 'Changed owned' });
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
});
test('plain text probe cannot verify tools; wrong tool nonce and client supplied evidence cannot claim verified support', async t => {
const f = fixture(t), server = await compatibleFixture(t, () => completion([], 'OK'));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Text probe', { tools: 'unknown' });
await hub.probe(connection.id); assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.text, 'verified');
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
await assert.rejects(hub.probe(connection.id, 'tools'), /未标为已验证/);
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
});
test('a disabled connection invalidates an in-flight probe even when toggle mutates its original registry object', async t => {
const f = fixture(t); let release;
const server = await compatibleFixture(t, async () => { await new Promise(resolve => { release = resolve; }); return completion([], 'OK'); });
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Changing probe', {});
const pending = hub.probe(connection.id), rejected = assert.rejects(pending, /检测期间连接已变化/);
await until(() => !!release); hub.toggle(connection.id, false); release(); await rejected;
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.text, 'protocol_declared');
assert.equal(hub.state.capabilityResults?.[connection.id], undefined);
assert.equal(hub.view().attempts[0].status, 'completed');
});
@@ -0,0 +1,77 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { fixture, compatibleFixture, completion, until, launchWorkbench } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { validateRuntimeBudgets } from '../src/core/models/runtime-settings.mjs';
test('selected connection owns the actual endpoint, credential, wire model and mismatch even when legacy is configured for GPT', async t => {
const f = fixture(t);
const legacy = await compatibleFixture(t, () => ({ ...completion([], 'Legacy response'), model: 'owned-gpt' }));
const chosen = await compatibleFixture(t, () => ({ ...completion([], 'Chosen response'), model: 'owned-gpt' }));
f.store.saveConfig({ mode: 'compatible', baseUrl: legacy.baseUrl, model: 'owned-gpt', maxSteps: 1 });
const provider = new Provider(); provider.setKey('owned-legacy-key');
const hub = provider.modelHub(f.store), connection = hub.upsert({ name: 'Chosen Claude route', model: 'owned-claude',
baseUrl: chosen.baseUrl, apiKey: 'owned-chosen-key', modelIdentityPolicy: 'require-match' });
hub.select({ type: 'connection', id: connection.id });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Reply only; no tools.'); await runtime.active.get(f.session.id).promise;
assert.equal(legacy.requests.length, 0); assert.equal(chosen.requests.length, 1);
assert.equal(chosen.requests[0].headers.authorization, 'Bearer owned-chosen-key');
assert.equal(chosen.requests[0].input.model, 'owned-claude');
const attempt = hub.view().attempts[0], run = f.store.core.run.get(f.session.runId);
assert.equal(attempt.connectionId, connection.id); assert.equal(attempt.poolId, null);
assert.equal(attempt.requestSha256, createHash('sha256').update(JSON.stringify(chosen.requests[0].input)).digest('hex'));
assert.equal(run.stopReason, 'model_identity'); assert.equal(run.model.model, 'owned-claude');
assert.equal(f.store.core.tool.size, 0); assert.deepEqual(attempt.modelIdentity.reportedModels, ['owned-gpt']);
const sealedRun = structuredClone(run);
hub.select({ type: 'legacy' }, { mode: 'compatible' });
const next = f.store.createSession(f.project.id); runtime.start(next.id, 'Reply only; no tools.'); await runtime.active.get(next.id).promise;
assert.equal(legacy.requests.length, 1); assert.equal(chosen.requests.length, 1);
assert.equal(legacy.requests[0].headers.authorization, 'Bearer owned-legacy-key');
assert.equal(legacy.requests[0].input.model, 'owned-gpt'); assert.equal(next.status, 'completed'); assert.deepEqual(run, sealedRun);
});
test('budget-only API and demo/real selection preserve routes, reject injected endpoint fields and publish authoritative selection without keys', async t => {
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
const connection = await host.api('/model-center/connections', { name: 'Owned model', model: 'owned-real',
baseUrl: 'http://127.0.0.1:9/v1', apiKey: 'owned-private-key' });
await host.api('/model-center/select', { type: 'connection', id: connection.id });
const before = await host.api('/model-center'), original = await host.api('/model-center/selection');
const result = await host.api('/model-center/runtime-budgets', { contextLimit: 32000, maxSteps: 8, maxRunTokens: 90000 });
assert.deepEqual(result.modelSelection, { type: 'connection', id: connection.id, name: 'Owned model' });
assert.equal(result.config.contextLimit, 32000); assert.equal(result.config.baseUrl, original.config.baseUrl);
assert.deepEqual((await host.api('/model-center')).connections.find(c => c.id === connection.id), before.connections.find(c => c.id === connection.id));
await assert.rejects(host.api('/model-center/runtime-budgets', { baseUrl: 'http://127.0.0.1:1' }), /只接受运行预算/);
await assert.rejects(host.api('/model-center/runtime-budgets', { maxSteps: 0 }), /范围内的整数/);
await host.api('/model-center/mode', { mode: 'demo' });
assert.deepEqual((await host.api('/model-center')).selected, before.selected);
await host.api('/model-center/select', { type: 'legacy', id: 'legacy-default' });
const legacy = await host.api('/model-center/selection'); assert.equal(legacy.config.mode, 'compatible');
assert.equal(legacy.modelSelection.type, 'legacy');
await host.api('/model-center/select', { type: 'connection', id: connection.id });
const current = await host.api('/model-center/selection'); assert.equal(current.modelSelection.name, 'Owned model');
assert.equal(JSON.stringify(current).includes('owned-private-key'), false);
assert.equal((await host.api('/model-center')).attempts.length, 0);
for (const input of [null, [], {}, { maxSteps: '8' }, { maxTaskTokens: Infinity }, { apiKey: 'never-save' }]) {
assert.throws(() => validateRuntimeBudgets(input));
}
});
test('another session cannot change default model, mode or budgets while a request is active', async t => {
let release; const pending = new Promise(resolve => { release = resolve; });
const upstream = await compatibleFixture(t, async () => { await pending; return { ...completion([], 'Done'), model: 'owned-running' }; });
t.after(() => release());
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
const connection = await host.api('/model-center/connections', { name: 'Active model', model: 'owned-running', baseUrl: upstream.baseUrl, requiresKey: false });
await host.api('/model-center/select', { type: 'connection', id: connection.id });
await host.api('/sessions/' + f.session.id + '/messages', { content: 'Reply only; no tools.' });
await until(() => upstream.requests.length === 1);
const before = await host.api('/model-center/selection');
for (const [route, input] of [['runtime-budgets', { maxSteps: 8 }], ['mode', { mode: 'demo' }], ['select', { type: 'legacy' }]]) {
await assert.rejects(host.api('/model-center/' + route, input), error => error.status === 409);
}
assert.deepEqual(await host.api('/model-center/selection'), before);
release(); await until(async () => (await host.api('/sessions/' + f.session.id)).status !== 'running');
});
@@ -0,0 +1,240 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { createHash } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { fixture, until, assertToolPairs, launchWorkbench } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { readModelStream } from '../src/core/models/model-stream.mjs';
const sse = event => 'data: ' + (typeof event === 'string' ? event : JSON.stringify(event)) + '\r\n\r\n';
const chat = (delta, finish_reason = null) => sse({ choices: [{ index: 0, delta, finish_reason }] });
const chatEnd = (reason = 'stop') => chat({}, reason) + sse({ choices: [], usage: { prompt_tokens: 20, completion_tokens: 10 } }) + sse('[DONE]');
const native = (type, value, sequence_number) => sse({ type, sequence_number, ...value });
test('HTTP download preserves original malformed SSE bytes and artifact SHA instead of decoding invalid UTF8', async t => {
const f = fixture(t), bytes = Buffer.from([100, 97, 116, 97, 58, 32, 0xff, 10, 10]);
const artifact = f.store.artifactStore.write(f.session, null, bytes, { kind: 'model-stream-response', mediaType: 'text/event-stream' });
f.store.core.artifact.set(artifact.id, artifact); f.store.save(f.session); f.store.close();
const host = await launchWorkbench(t, f.data);
const response = await fetch(host.url + '/api/artifacts/' + artifact.id + '?session=' + f.session.id + '&download=1', { headers: { 'X-Workbench-Token': host.bootstrap.token } });
assert.equal(response.status, 200); const downloaded = Buffer.from(await response.arrayBuffer());
assert.deepEqual(downloaded, bytes); assert.equal(createHash('sha256').update(downloaded).digest('hex'), artifact.sha256);
});
async function streamingServer(t, handler) {
const requests = [], sockets = new Set();
const server = http.createServer(async (request, response) => {
const parts = []; for await (const part of request) parts.push(part);
const body = JSON.parse(Buffer.concat(parts)); requests.push({ body, url: request.url });
response.writeHead(200, { 'Content-Type': 'text/event-stream' });
try { await handler(response, body, requests); } catch { response.destroy(); }
});
server.on('connection', socket => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { for (const socket of sockets) socket.destroy(); server.close(resolve); }));
return { requests, baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1' };
}
function configure(f, server, protocol = 'chat-completions') {
const provider = new Provider(), hub = provider.modelHub(f.store);
const connection = hub.upsert({ name: 'Owned stream', baseUrl: server.baseUrl, model: 'owned-stream', protocol, requiresKey: false,
streaming: true, capabilityDeclarations: { streaming: 'supported' } });
hub.select({ type: 'connection', id: connection.id });
return { provider, hub, connection };
}
test('terminated SSE adapts empty optional tool arguments, retains the exact wire bytes and executes the validated batch once', async t => {
const f = fixture(t);
const original = chat({ tool_calls: [
{ index: 0, id: 'empty-inspection', type: 'function', function: { name: 'inspect_project', arguments: '' } },
{ index: 1, id: 'valid-file', type: 'function', function: { name: 'read_file', arguments: '{"path":"README.md"}' } },
] }) + chatEnd('tool_calls');
const server = await streamingServer(t, (res, body) => res.end(body.messages.some(message => message.role === 'tool') ? chat({ content: '读取已完成。' }) + chatEnd() : original));
const { provider, hub } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, '读取项目与 README'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed'); assert.equal(server.requests.length, 2);
assert.equal(f.store.core.tool.size, 2); assertToolPairs(f.session.providerMessages);
const first = hub.view().attempts.find(attempt => attempt.compatibility?.some(value => value.callId === 'empty-inspection')), artifact = f.store.owned('artifact', first.responseArtifactId, f.session.id);
assert.equal(f.store.artifactStore.read(artifact), original);
assert.deepEqual(first.compatibility, [{ kind: 'empty_object_arguments', callId: 'empty-inspection', tool: 'inspect_project', originalArguments: '', arguments: '{}' }]);
const run = f.store.core.run.get(f.session.runId);
assert.equal(run.usage.requests, 2); assert.equal(run.usage.totalTokens, 60); assert.equal(run.consumption.length, 2);
});
for (const malformed of ['', '{"path":', 'null']) test('reported SSE usage survives rejected required arguments ' + JSON.stringify(malformed) + ' without dispatching any tool', async t => {
const f = fixture(t), original = chat({ tool_calls: [
{ index: 0, id: 'valid-inspect', type: 'function', function: { name: 'inspect_project', arguments: '{}' } },
{ index: 1, id: 'invalid-read', type: 'function', function: { name: 'read_file', arguments: malformed } },
] }) + chatEnd('tool_calls');
const server = await streamingServer(t, res => res.end(original));
const { provider, hub } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, '读取项目'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'failed'); assert.equal(server.requests.length, 1); assert.equal(f.store.core.tool.size, 0);
assert.match(f.session.failure.message, /read_file.*invalid-read/);
const run = f.store.core.run.get(f.session.runId), task = f.store.core.task.get(f.session.taskId);
assert.equal(run.usage.totalTokens, 30); assert.equal(task.usage.totalTokens, 30); assert.equal(run.usage.requests, 1); assert.equal(run.usage.unknownRequests, 0);
assert.equal(hub.view().attempts[0].usageStatus, 'reported');
assert.equal(hub.view().attempts[0].status, 'failed');
assert.equal(f.store.artifactStore.read(f.store.owned('artifact', hub.view().attempts[0].responseArtifactId, f.session.id)), original);
assert.equal(f.store.recordConsumption(f.session, run, run.consumption[0], 999), false);
assert.equal(run.usage.requests, 1);
});
test('a stream without DONE retains reported usage but cannot adapt or execute even an optional tool', async t => {
const f = fixture(t), server = await streamingServer(t, res => res.end(chat({ tool_calls: [
{ index: 0, id: 'no-done', type: 'function', function: { name: 'inspect_project', arguments: '' } },
] }) + chat({}, 'tool_calls') + sse({ choices: [], usage: { prompt_tokens: 31, completion_tokens: 7 } })));
const { provider } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, '读取项目'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.store.core.tool.size, 0); assert.equal(f.session.status, 'failed');
const run = f.store.core.run.get(f.session.runId); assert.equal(run.usage.requests, 1); assert.equal(run.usage.totalTokens, 38);
});
test('a timeout after reasoning-only fragments creates one unknown receipt and never invents reported tokens', async t => {
const f = fixture(t), server = await streamingServer(t, res => { res.write(chat({ reasoning_content: 'pending reasoning' })); });
const { provider } = configure(f, server); provider.retryPolicy = { timeoutMs: 400, totalTimeoutMs: 1000 };
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, '读取项目'); await until(() => !runtime.isActive(f.session.id), 15000);
const run = f.store.core.run.get(f.session.runId);
assert.equal(server.requests.length, 1); assert.equal(run.usage.requests, 1); assert.equal(run.usage.unknownRequests, 1);
assert.equal(run.usage.totalTokens, 0); assert.ok(run.usage.estimatedUnknownTokens > 0); assert.equal(run.consumption.length, 1);
assert.equal(f.store.core.tool.size, 0);
});
test('real Chat SSE carries UTF8 and split tool JSON; Runtime executes only after DONE, stores partial progress and original usage', async t => {
const f = fixture(t); let runtime, observedBeforeEnd = false;
const server = await streamingServer(t, async (res, body) => {
if (body.messages.some(item => item.role === 'tool')) { res.end(chat({ content: '读取完成。' }) + chatEnd()); return; }
const bytes = Buffer.from(chat({ content: '先读取项目。' }));
for (let at = 0; at < bytes.length; at += 7) res.write(bytes.subarray(at, at + 7));
res.write(chat({ tool_calls: [{ index: 0, id: 'stream_read', type: 'function', function: { name: 'list_files', arguments: '{"path":' } }] }));
await delay(150); observedBeforeEnd = f.session.events.some(item => item.stream?.textPreview === '先读取项目。') && !f.session.events.some(item => item.kind === 'tool');
res.end(chat({ tool_calls: [{ index: 0, function: { arguments: '"","depth":1}' } }] }) + chatEnd('tool_calls'));
});
const { provider, hub } = configure(f, server); runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect original project'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-2))); assert.equal(observedBeforeEnd, true);
assert.equal(server.requests.length, 2); assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
assertToolPairs(f.session.providerMessages); assert.equal(f.store.core.run.get(f.session.runId).usage.inputTokens, 40);
assert.ok(hub.view().attempts.every(item => item.stream.terminal && item.stream.sha256.length === 64));
for (const attempt of hub.view().attempts) {
const artifact = f.store.owned('artifact', attempt.responseArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.stream.sha256); assert.equal(artifact.bytes, attempt.stream.bytes);
assert.equal(artifact.kind, 'model-stream-response'); assert.equal(attempt.responseRedacted, false);
}
});
test('Responses typed SSE validates original item identity, terminal data, raw usage and tool result pair', async t => {
const f = fixture(t), call = { type: 'function_call', id: 'fc_stream', call_id: 'call_stream', name: 'list_files', arguments: '', status: 'in_progress' };
const server = await streamingServer(t, async (res, body) => {
const done = body.input.some(item => item.type === 'function_call_output');
let seq = 0; res.write(native('response.created', { response: { id: 'resp_stream' } }, seq++));
let output;
if (done) {
res.write(native('response.output_text.delta', { delta: '完成' }, seq++));
output = [{ type: 'message', id: 'msg_stream', status: 'completed', role: 'assistant', content: [{ type: 'output_text', text: '完成' }] }];
} else {
res.write(native('response.output_item.added', { output_index: 0, item: call }, seq++));
res.write(native('response.function_call_arguments.delta', { item_id: call.id, delta: '{"path":"",' }, seq++));
res.write(native('response.function_call_arguments.delta', { item_id: call.id, delta: '"depth":1}' }, seq++));
output = [{ ...call, status: 'completed', arguments: '{"path":"","depth":1}' }];
res.write(native('response.function_call_arguments.done', { item_id: call.id, arguments: output[0].arguments }, seq++));
}
res.end(native('response.completed', { response: { id: 'resp_stream', status: 'completed', output, usage: { input_tokens: 33, output_tokens: 9, total_tokens: 42 } } }, seq++));
});
const { provider, hub } = configure(f, server, 'responses'), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-2))); assertToolPairs(f.session.providerMessages);
assert.equal(server.requests.length, 2); assert.equal(server.requests[1].body.input.find(item => item.type === 'function_call_output').call_id, 'call_stream');
assert.equal(hub.view().attempts[0].usage.raw.total_tokens, 42);
});
test('incomplete network stream retains unknown attempt and never automatically switches pool or dispatches fragment tools', async t => {
const f = fixture(t), server = await streamingServer(t, async res => res.end(chat({ content: 'partial' }) + chat({ tool_calls: [{ index: 0, id: 'partial_tool', function: { name: 'list_files', arguments: '{"path":' } }] })));
const { provider, hub, connection } = configure(f, server), other = hub.upsert({ ...connection, id: undefined, name: 'Other', revision: undefined });
const pool = hub.pool({ name: 'No replay', members: [{ connectionId: connection.id }, { connectionId: other.id }] }); hub.select({ type: 'pool', id: pool.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
assert.equal(server.requests.length, 1); assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'unknown'); assert.equal(attempt.partial.textPreview, 'partial'); assert.equal(attempt.usageStatus, 'unknown');
const artifact = f.store.owned('artifact', attempt.responseArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.stream.sha256); assert.equal(artifact.bytes, attempt.stream.capturedBytes); assert.equal(attempt.stream.terminal, false);
assert.equal(f.session.events.find(item => item.stream)?.stream.status, 'incomplete');
});
test('stopping an active HTTP stream closes only original request and keeps partial text without executing tools', async t => {
const f = fixture(t); let closed = false;
const server = await streamingServer(t, async res => { res.on('close', () => { closed = true; }); res.write(chat({ content: '尚未完成' })); });
const { provider, hub } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect'); await until(() => f.session.events.some(item => item.stream?.textCharacters > 0));
runtime.stop(f.session.id); await until(() => !runtime.isActive(f.session.id) && closed);
assert.equal(server.requests.length, 1); assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
assert.equal(hub.view().attempts[0].status, 'interrupted'); assert.equal(f.session.events.find(item => item.stream)?.stream.status, 'interrupted');
const attempt = hub.view().attempts[0]; assert.equal(f.store.owned('artifact', attempt.responseArtifactId, f.session.id).sha256, attempt.stream.sha256);
assert.equal([...hub.leases.values()].reduce((a, b) => a + b, 0), 0);
});
test('explicit streaming probe uses actual SSE despite unknown declaration and binds its own original attempt', async t => {
const f = fixture(t), server = await streamingServer(t, async res => res.end(chat({ content: 'OK' }) + chatEnd()));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = hub.upsert({ name: 'Owned probe', model: 'm', baseUrl: server.baseUrl, requiresKey: false });
await hub.probe(connection.id, 'streaming');
const view = hub.view().connections.find(item => item.id === connection.id);
assert.equal(view.capabilities.streaming, 'verified'); assert.equal(view.streaming, false); assert.equal(server.requests[0].body.stream, true);
assert.equal(view.capabilityEvidence.attemptId, hub.view().attempts[0].id); assert.equal(f.session.events.length, 0);
});
test('coalesced initial SSE events publish the latest partial state even when the upstream stays idle', async t => {
const f = fixture(t), server = await streamingServer(t, async res => {
res.write(native('response.created', { response: { id: 'resp_idle' } }, 0) + native('response.output_text.delta', { delta: '等待中的实际片段' }, 1));
});
const { provider } = configure(f, server, 'responses'), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect'); await until(() => f.session.events.some(item => item.stream?.textPreview === '等待中的实际片段'));
assert.equal(runtime.isActive(f.session.id), true); runtime.stop(f.session.id); await until(() => !runtime.isActive(f.session.id));
});
test('stream timeout closes the original HTTP response, preserves redacted capture and never retries', async t => {
const f = fixture(t); let closed = false;
const server = await streamingServer(t, async res => { res.on('close', () => { closed = true; }); res.write(chat({ content: 'owned-secret partial' })); });
const { provider, hub } = configure(f, server); provider.setKey('owned-secret');
await assert.rejects(provider.complete(provider.prepareConfig(f.store.config, f.store), [{ role: 'user', content: 'Inspect' }], f.project,
undefined, undefined, null, [], { timeoutMs: 100, totalTimeoutMs: 300, attempts: 3 }), error => error.diagnostic.kind === 'timeout' && !error.diagnostic.retryable);
await until(() => closed); assert.equal(server.requests.length, 1); assert.equal(hub.view().attempts[0].status, 'unknown');
assert.equal(hub.view().attempts[0].partial.textPreview.includes('owned-secret'), false);
});
test('stream idle timeout preserves incomplete tool arguments, records unknown use and never fails over', async t => {
const f = fixture(t); let closed = false;
const original = chat({ tool_calls: [{ index: 0, id: 'idle-original', type: 'function', function: { name: 'propose_file', arguments: '{"path":"incomplete.html",' } }] });
const server = await streamingServer(t, res => { res.on('close', () => { closed = true; }); res.write(original); });
const { provider, hub, connection } = configure(f, server), other = hub.upsert({ ...connection, id: undefined, name: 'No failover', revision: undefined });
const pool = hub.pool({ name: 'Idle capture', members: [{ connectionId: connection.id }, { connectionId: other.id }] }); hub.select({ type: 'pool', id: pool.id });
const progress = [];
await assert.rejects(provider.complete(provider.prepareConfig(f.store.config, f.store), [{ role: 'user', content: 'owned timeout' }], f.project,
undefined, event => progress.push(event), null, [], { timeoutMs: 5000, totalTimeoutMs: 5000, idleTimeoutMs: 150 }), error => {
assert.equal(error.diagnostic.timeout.kind, 'idle'); assert.equal(error.diagnostic.timeout.receivedBytes, Buffer.byteLength(original));
assert.equal(error.diagnostic.retryable, false); return true;
});
await until(() => closed); assert.equal(server.requests.length, 1); assert.equal(f.session.changes.length, 0);
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'unknown'); assert.equal(attempt.usageStatus, 'unknown');
assert.equal(attempt.stream.terminal, false); assert.equal(attempt.stream.bytes, Buffer.byteLength(original));
const capture = progress.find(event => event.consumption)?.consumption; assert.equal(capture.usageStatus, 'unknown');
});
test('SSE accepts CR-only final event boundaries and retains exact invalid UTF8 bytes without dispatch', async () => {
const valid = (chat({ content: 'OK' }) + chatEnd()).replaceAll('\r\n', '\r');
const result = await readModelStream(new Response(valid, { headers: { 'Content-Type': 'text/event-stream' } }), {}, undefined);
assert.equal(result.value.choices[0].message.content, 'OK');
const invalid = Buffer.from([100, 97, 116, 97, 58, 32, 0xff, 10, 10]); let capture;
await assert.rejects(readModelStream(new Response(invalid, { headers: { 'Content-Type': 'text/event-stream' } }), {}, undefined, event => { if (event.streamCapture) capture = event; }));
assert.deepEqual(capture.streamCapture, invalid); assert.equal(capture.stream.sha256, createHash('sha256').update(invalid).digest('hex')); assert.equal(capture.stream.terminal, false);
});
test('stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data', async t => {
const cases = [
['chat-completions', chat({ content: 'partial' })], ['chat-completions', chat({ content: 'partial' }) + chatEnd('length')],
['chat-completions', 'data: {broken}\n\n'], ['chat-completions', 'data: ' + 'x'.repeat(4 * 1024 * 1024) + '\n\n'],
['responses', native('response.created', { response: { id: 'r' } }, 0) + native('response.output_text.delta', { delta: 'x' }, 0)],
['responses', native('response.output_text.delta', { delta: 'x' }, 0) + native('response.completed', { response: { status: 'completed', output: [{ type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'changed' }] }] } }, 1)],
];
for (const [index, [protocol, body]] of cases.entries()) await t.test('reject ' + index, async () => {
await assert.rejects(readModelStream(new Response(body, { headers: { 'content-type': 'text/event-stream' } }), { protocol }, undefined));
});
});
@@ -0,0 +1,39 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { fixture,launchWorkbench,completion,call,until } from './helpers.mjs';
for(const status of [200,503])test('compiled host retains interrupted HTTP '+status+' and explicitly resumes after the recorded cooldown',async t=>{
const f=fixture(t);let requests=0,recovered=false;
const server=http.createServer(async(request,response)=>{
const parts=[];for await(const chunk of request)parts.push(chunk);
const body=JSON.parse(Buffer.concat(parts));requests++;
response.writeHead(recovered?200:status,{'Content-Type':'application/json','X-Request-ID':'owned-compiled-partial-'+status});
if(!recovered){response.write('{"error":{"message":"owned interrupted');setTimeout(()=>response.destroy(),20);return;}
response.end(JSON.stringify(body.messages.some(message=>message.role==='tool')?completion([],'README已读取,文件保持。')
:completion([call('owned-read','read_file',{path:'README.md'})])));
});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
t.after(()=>new Promise(resolve=>{server.closeAllConnections();server.close(resolve);}));
f.store.close();const host=await launchWorkbench(t,f.data);
const connection=await host.api('/model-center/connections',{name:'Owned compiled transport',model:'owned-compiled',
baseUrl:'http://127.0.0.1:'+server.address().port+'/v1',requiresKey:false});
await host.api('/model-center/select',{type:'connection',id:connection.id});
const route='/sessions/'+f.session.id;let state;
await host.api(route+'/messages',{content:'分析README,不修改文件,不检查。'});
await until(async()=>{state=await host.api(route);return state.status==='failed';},15000);
assert.equal(requests,1);assert.equal(state.failure.outcome,'unknown');assert.equal(state.failure.retryable,false);
assert.equal(state.failure.upstreamRequestId,'owned-compiled-partial-'+status);
assert.equal(state.failure.upstream.capture.interrupted,true);assert.equal(state.execution.run.consumption.length,1);
assert.equal(state.execution.run.consumption[0].usageStatus,'unknown');assert.equal(state.execution.task.usage.requests,1);
const taskId=state.taskId,userTurns=state.messages.filter(message=>message.role==='user').length;
const center=await host.api('/model-center'),cooldown=center.connections.find(value=>value.id===connection.id).health.cooldownUntil;
assert.ok(Number.isFinite(cooldown));await until(()=>Date.now()>=cooldown);assert.equal(requests,1);
recovered=true;await host.api(route+'/resume',{});
await until(async()=>{state=await host.api(route);return !['running','awaiting_approval','stopping'].includes(state.status);},15000);
assert.equal(state.status,'completed',JSON.stringify({status:state.status,failure:state.failure?.message,stopReason:state.execution.run.stopReason,requests}));
assert.equal(state.taskId,taskId);assert.equal(requests,3);
assert.equal(state.messages.filter(message=>message.role==='user').length,userTurns);
assert.equal(state.events.filter(event=>event.tool==='read_file'&&event.phase==='completed').length,1);
assert.equal(state.execution.task.usage.requests,3);assert.equal(state.execution.task.usage.unknownRequests,1);
});
@@ -0,0 +1,123 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import net from 'node:net';
import { createHash } from 'node:crypto';
import { ModelTransport } from '../src/core/models/transport.mjs';
import { Provider } from '../src/server/provider.mjs';
import { connectionFailure } from '../src/server/diagnostics.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { fixture,compatibleFixture,completion,until } from './helpers.mjs';
const config=baseUrl=>({mode:'compatible',baseUrl,model:'owned-transport',maxOutputTokens:128});
const policy={attempts:3,delays:[1,1],timeoutMs:1500,totalTimeoutMs:4000};
const lookup=(_hostname,options,callback)=>{
assert.equal(options.all,true);callback(null,[{address:'::1',family:6},{address:'127.0.0.1',family:4}]);
};
test('owned IPv4 service works after a failed IPv6 candidate without a second model POST or global DNS change',async t=>{
const model=await compatibleFixture(t,()=>completion([],'Owned dual-stack result'));
const url=new URL(model.baseUrl);url.hostname='owned-transport.invalid';
const transport=new ModelTransport({environment:{},lookup}),provider=new Provider({transport,retryPolicy:policy}),events=[];
t.after(()=>provider.abortRequests());const original=net.getDefaultAutoSelectFamily();
const result=await provider.complete(config(url.href),[],null,null,event=>events.push(event));
assert.equal(result.message.content,'Owned dual-stack result');assert.equal(model.requests.length,1);
assert.equal(net.getDefaultAutoSelectFamily(),original);assert.ok(events.some(event=>event.transport?.autoSelectFamily));
assert.equal(provider.lastRequest.attempts,1);
});
test('environment HTTP proxy tunnels the owned request and NO_PROXY sends a separate direct request',async t=>{
const model=await compatibleFixture(t,()=>completion([],'Owned proxy result')),target=new URL(model.baseUrl),sockets=new Set();let tunnels=0;
const proxy=http.createServer();proxy.on('connection',socket=>{sockets.add(socket);socket.on('close',()=>sockets.delete(socket));});
proxy.on('connect',(request,socket,head)=>{
assert.equal(request.url,target.host);tunnels++;
const upstream=net.connect({host:target.hostname,port:Number(target.port)},()=>{socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');if(head.length)upstream.write(head);socket.pipe(upstream);upstream.pipe(socket);});
upstream.on('error',()=>socket.destroy());socket.on('error',()=>upstream.destroy());socket.on('close',()=>upstream.destroy());
});
await new Promise(resolve=>proxy.listen(0,'127.0.0.1',resolve));
t.after(async()=>{for(const socket of sockets)socket.destroy();await new Promise(resolve=>proxy.close(resolve));});
const environment={HTTP_PROXY:'http://owned-proxy.invalid:'+proxy.address().port};
const through=new Provider({transport:new ModelTransport({environment,lookup}),retryPolicy:policy});t.after(()=>through.abortRequests());
assert.equal((await through.complete(config(model.baseUrl),[],null)).message.content,'Owned proxy result');assert.equal(tunnels,1);
const bypass=new Provider({transport:new ModelTransport({environment:{...environment,NO_PROXY:'127.0.0.1'}}),retryPolicy:policy});t.after(()=>bypass.abortRequests());
await bypass.complete(config(model.baseUrl),[],null);assert.equal(tunnels,1);assert.equal(model.requests.length,2);
assert.equal(JSON.stringify(through.transport.profile).includes(environment.HTTP_PROXY),false);
});
test('original connect timeout has a bounded nested cause and does not masquerade as response generation timeout',()=>{
const error=new TypeError('fetch failed',{cause:Object.assign(new Error('socket'),{code:'UND_ERR_CONNECT_TIMEOUT'})});
const value=connectionFailure(error,{model:'owned',attempt:1});
assert.equal(value.kind,'timeout');assert.equal(value.network.stage,'connect');assert.equal(value.network.replaySafety,'before_model_send');
assert.equal(value.retryable,true);assert.equal(value.outcome,'failed');assert.equal(value.title,'模型服务连接超时');
assert.match(value.suggestion,/增加模型响应等待时限不能修复连接/);
const unreachable=Object.assign(new Error('owned'),{code:'ENETUNREACH',family:6,syscall:'connect'});
const aggregate=new TypeError('fetch',{cause:new AggregateError([unreachable,Object.assign(new Error('owned'),{code:'ECONNREFUSED',family:4})])});
const bounded=connectionFailure(aggregate,{attempt:1});assert.equal(bounded.retryable,true);assert.equal(bounded.network.causes.length,2);
unreachable.cause=unreachable;
assert.equal(connectionFailure(aggregate,{attempt:1}).retryable,false,'Incomplete or cyclic causes cannot establish safe replay.');
for(const errors of [[Object.assign(new Error('refused'),{code:'ECONNREFUSED'}),new Error('unknown failure')],
[...Array.from({length:8},()=>Object.assign(new Error('refused'),{code:'ECONNREFUSED'})),Object.assign(new Error('reset'),{code:'ECONNRESET'})]]){
const unknown=connectionFailure(new AggregateError(errors),{attempt:1});
assert.equal(unknown.outcome,'unknown');assert.equal(unknown.retryable,false);
}
});
test('POST accepted then reset is unknown, is charged once and never automatically retried',async t=>{
const sockets=new Set();let posts=0;
const server=http.createServer(async(request,response)=>{for await(const _chunk of request){}posts++;response.socket.destroy();});
server.on('connection',socket=>{sockets.add(socket);socket.on('close',()=>sockets.delete(socket));});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
t.after(async()=>{for(const socket of sockets)socket.destroy();await new Promise(resolve=>server.close(resolve));});
const provider=new Provider({transport:new ModelTransport({environment:{}}),retryPolicy:policy}),events=[];t.after(()=>provider.abortRequests());
await assert.rejects(provider.complete(config('http://127.0.0.1:'+server.address().port+'/v1'),[],null,null,event=>events.push(event)),error=>{
assert.equal(error.diagnostic.outcome,'unknown');assert.equal(error.diagnostic.retryable,false);assert.equal(error.diagnostic.failoverAllowed,false);return true;
});
assert.equal(posts,1);const receipts=events.filter(event=>event.consumption);assert.equal(receipts.length,1);assert.equal(receipts[0].consumption.usageStatus,'unknown');
assert.equal(receipts[0].consumption.outcome,'unknown');
});
test('partial HTTP bodies retain original request id and bytes, and an unknown pool attempt never switches model',async t=>{
const f=fixture(t),prefix=Buffer.from('{"choices":[{"message":{"content":"owned partial 中文');let posts=0;
const server=http.createServer(async(request,response)=>{
for await(const _chunk of request){}posts++;
response.writeHead(200,{'Content-Type':'application/json','X-Request-ID':'owned-partial-http'});response.write(prefix);
setTimeout(()=>response.destroy(),20);
});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
t.after(()=>new Promise(resolve=>{server.closeAllConnections();server.close(resolve);}));
const backup=await compatibleFixture(t,()=>completion([],'Must not be called'));
const provider=new Provider({transport:new ModelTransport({environment:{}}),retryPolicy:policy}),hub=provider.modelHub(f.store);
const first=hub.upsert({name:'owned partial',model:'actual-partial',baseUrl:'http://127.0.0.1:'+server.address().port+'/v1',requiresKey:false});
const second=hub.upsert({name:'owned backup',model:'actual-backup',baseUrl:backup.baseUrl,requiresKey:false});
const pool=hub.pool({name:'owned unknown pool',members:[{connectionId:first.id},{connectionId:second.id}]});hub.select({type:'pool',id:pool.id});
const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
runtime.start(f.session.id,'读取项目,不修改、不检查');await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status,'failed');assert.equal(posts,1);assert.equal(backup.requests.length,0);
const failure=f.session.failure,attempt=hub.view().attempts[0],run=f.store.core.run.get(f.session.runId);
assert.equal(hub.view().attempts.length,1);assert.equal(attempt.status,'unknown');
assert.equal(failure.network.stage,'response_body');assert.equal(failure.outcome,'unknown');assert.equal(failure.upstreamRequestId,'owned-partial-http');
assert.equal(failure.upstream.capture.complete,false);assert.equal(failure.upstream.capture.interrupted,true);
const captured=f.store.artifactStore.read(f.store.owned('artifact',attempt.responseArtifactId,f.session.id));
assert.equal(captured,prefix.toString('utf8'));assert.equal(attempt.responseSha256,createHash('sha256').update(prefix).digest('hex'));
assert.equal(run.consumption.length,1);assert.equal(run.consumption[0].outcome,'unknown');assert.equal(run.consumption[0].usageStatus,'unknown');
assert.equal(run.usage.requests,1);assert.equal(run.usage.unknownRequests,1);assert.equal(f.store.core.tool.size,0);
});
test('response wait timeout after an accepted POST is unknown; cancellation closes the owned socket and permits explicit reuse',async t=>{
let posts=0,closed=0;const sockets=new Set();
const server=http.createServer(async(request,_response)=>{for await(const _chunk of request){}posts++;request.socket.once('close',()=>closed++);});
server.on('connection',socket=>{sockets.add(socket);socket.on('close',()=>sockets.delete(socket));});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
t.after(async()=>{for(const socket of sockets)socket.destroy();await new Promise(resolve=>server.close(resolve));});
const provider=new Provider({transport:new ModelTransport({environment:{}}),retryPolicy:{...policy,timeoutMs:100,totalTimeoutMs:100}});t.after(()=>provider.abortRequests());
const endpoint=config('http://127.0.0.1:'+server.address().port+'/v1');
await assert.rejects(provider.complete(endpoint,[],null),error=>error.diagnostic.outcome==='unknown'&&error.diagnostic.network.stage==='waiting_response');
assert.equal(posts,1);assert.equal(provider.lastRequest.attempts,1);
provider.retryPolicy=policy;const controller=new AbortController(),pending=provider.complete(endpoint,[],null,controller.signal);
await until(()=>posts===2);controller.abort(new Error('Owned user cancellation'));await assert.rejects(pending,/Owned user cancellation/);
await provider.abortRequests();assert.equal(provider.activeRequests,0);assert.ok(closed>=1);assert.equal(provider.transport,null);provider.resumeRequests();
assert.equal(posts,2);
const working=await compatibleFixture(t,()=>completion([],'Explicit reuse works'));
assert.equal((await provider.complete(config(working.baseUrl),[],null)).message.content,'Explicit reuse works');
assert.equal(working.requests.length,1);
});
@@ -0,0 +1,63 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { modelWaitSettings, validateModelWaitSettings, requestDeadline } from '../src/core/models/request-deadline.mjs';
import { Provider } from '../src/server/provider.mjs';
import { modelSnapshot } from '../src/shared/contracts/execution.mjs';
import { fixture } from './helpers.mjs';
import { Store } from '../src/server/store.mjs';
import { connectionFailure } from '../src/server/diagnostics.mjs';
test('older configurations receive bounded wait defaults without changing the original object', () => {
const legacy = { model: 'legacy' }, original = structuredClone(legacy);
assert.deepEqual(modelWaitSettings(legacy), { modelResponseTimeoutMs: 300000, modelStreamIdleTimeoutMs: 120000 });
assert.deepEqual(legacy, original);
for (const value of [null, '', -1, Infinity, 0, 2 ** 32, 60000.5]) assert.equal(modelWaitSettings({ modelResponseTimeoutMs: value }).modelResponseTimeoutMs, 300000);
assert.equal(modelWaitSettings({ modelResponseTimeoutMs: 900000 }).modelResponseTimeoutMs, 900000);
assert.throws(() => validateModelWaitSettings({ modelResponseTimeoutMs: '60000' }), /整数/);
assert.throws(() => validateModelWaitSettings({ modelStreamIdleTimeoutMs: 0 }), /整数/);
assert.equal(validateModelWaitSettings({ modelStreamIdleTimeoutMs: 15000 }, { modelResponseTimeoutMs: 600000 }).modelResponseTimeoutMs, 600000);
});
test('active stream survives sixty seconds; byte activity resets idle but cannot extend total deadline', t => {
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: 1000 });
const deadline = requestDeadline({ timeoutMs: 300000, idleTimeoutMs: 120000 }); t.after(() => deadline.close());
deadline.beginStream();
for (let index = 0; index < 4; index++) { t.mock.timers.tick(70000); assert.equal(deadline.signal.aborted, false); deadline.receive(10); }
t.mock.timers.tick(19999); assert.equal(deadline.signal.aborted, false);
t.mock.timers.tick(1); assert.equal(deadline.signal.aborted, true);
assert.deepEqual(deadline.timeout, { kind: 'total', stage: 'stream', limitMs: 300000, elapsedMs: 300000, receivedBytes: 40, idleMs: 20000 });
});
test('silent stream has its own accurate deadline; caller cancellation and cleanup preserve their cause', t => {
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: 1000 });
const deadline = requestDeadline({ timeoutMs: 300000, idleTimeoutMs: 120000 }); t.after(() => deadline.close());
deadline.beginStream(); t.mock.timers.tick(100000); deadline.receive(7);
t.mock.timers.tick(119999); assert.equal(deadline.signal.aborted, false);
t.mock.timers.tick(1); assert.equal(deadline.timeout.kind, 'idle'); assert.equal(deadline.timeout.receivedBytes, 7);
const diagnosis = connectionFailure(deadline.signal.reason, { timedOut: true, timeout: deadline.timeout });
assert.match(diagnosis.message, /连续 120 秒未收到新数据/); assert.equal(diagnosis.timeout.elapsedMs, 220000);
const stop = new AbortController(), user = requestDeadline({ signal: stop.signal, timeoutMs: 300000, idleTimeoutMs: 120000 });
const reason = new Error('owned user stop'); stop.abort(reason); assert.equal(user.signal.reason, reason); assert.equal(user.timeout, null); user.close();
const closed = requestDeadline({ timeoutMs: 300000, idleTimeoutMs: 120000 }); closed.beginStream(); closed.close();
t.mock.timers.tick(900000); assert.equal(closed.signal.aborted, false);
});
test('waiting for headers is bounded independently of stream-idle time', t => {
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: 1000 });
const deadline = requestDeadline({ timeoutMs: 300000, idleTimeoutMs: 120000 }); t.after(() => deadline.close());
t.mock.timers.tick(120001); assert.equal(deadline.signal.aborted, false);
t.mock.timers.tick(179999); assert.equal(deadline.timeout.stage, 'response'); assert.equal(deadline.timeout.kind, 'total');
});
test('wait settings persist, pin new runs and leave model pool, credentials and old run snapshots intact', t => {
const f = fixture(t), provider = new Provider(), hub = provider.modelHub(f.store);
const connection = hub.upsert({ name: 'Owned wait', model: 'm', baseUrl: 'http://127.0.0.1:1/v1', requiresKey: false });
const pool = hub.pool({ name: 'Original pool', members: [{ connectionId: connection.id }] }); hub.select({ type: 'pool', id: pool.id });
const before = structuredClone(hub.state), previous = modelSnapshot(f.store.config);
f.store.saveConfig(validateModelWaitSettings({ modelResponseTimeoutMs: 600000, modelStreamIdleTimeoutMs: 180000 }, f.store.config));
assert.deepEqual(hub.state, before); assert.equal(previous.modelResponseTimeoutMs, 300000);
assert.equal(hub.prepare(f.store.config).modelResponseTimeoutMs, 600000);
assert.equal(hub.view().waitSettings.modelStreamIdleTimeoutMs, 180000);
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
assert.equal(reopened.config.modelResponseTimeoutMs, 600000); assert.equal(reopened.storage.meta('modelRegistry').selected.id, pool.id);
});
@@ -0,0 +1,165 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import http from 'node:http';
import net from 'node:net';
import tls from 'node:tls';
import { fixture, until, workspace } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { ModelTransport } from '../src/core/models/transport.mjs';
import { NetworkDiagnostics } from '../src/core/models/network-diagnostics.mjs';
import { CompanionTLS } from '../src/core/companion/tls-authority.mjs';
import { createRequestHandler } from '../src/server/app.mjs';
import { hostAuthority } from '../src/core/desktop/host-authority.mjs';
import { installUpgrade } from '../src/server/hot-upgrade.mjs';
async function listener(f, server) {
const sockets = new Set(); server.on('connection', socket => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
f.onCleanup(async () => { for (const socket of sockets) socket.destroy(); await new Promise(resolve => server.close(resolve)); });
return server.address().port;
}
function setup(t, options = {}) {
const f = fixture(t), provider = new Provider({ transport: options.transport ?? new ModelTransport({ environment: {} }) });
const hub = provider.modelHub(f.store); hub.networkChecks = new NetworkDiagnostics(hub, options);
f.onCleanup(() => provider.abortRequests());
const add = baseUrl => hub.upsert({ name: 'Owned network check', model: 'owned-no-model', baseUrl, requiresKey: true });
const run = async connection => { const { job } = hub.networkChecks.start({ id: connection.id, revision: connection.revision });
await hub.networkChecks.active.get(job.id).promise; return hub.networkChecks.view().jobs.find(row => row.id === job.id); };
return { ...f, provider, hub, add, run, manager: hub.networkChecks };
}
async function material() {
const state = { gatewayId: 'owned-network-test' }, registry = { binding: 'owned-network-test', read: () => state, mutate: action => action(state) };
return new CompanionTLS(registry, { protect: value => Buffer.from(value), unprotect: value => Buffer.from(value) }).material('127.0.0.1');
}
test('plain endpoint checks only TCP, records family coverage and never reads model credentials or writes routing/health/attempts', async t => {
const f = setup(t); let bytes = 0;
const port = await listener(f, net.createServer(socket => socket.on('data', chunk => { bytes += chunk.length; })));
const connection = f.add('http://127.0.0.1:' + port + '/v1'), state = structuredClone(f.hub.state), config = structuredClone(f.store.config);
f.hub.credential = () => { throw Error('Credential access forbidden.'); };
f.provider.redact = () => { throw Error('Redaction must not enumerate saved keys.'); };
const job = await f.run(connection);
assert.equal(job.status, 'passed'); assert.equal(job.stages.dns.status, 'passed');
assert.equal(job.trials[0].tls, 'not_applicable'); assert.equal(job.trials[2].status, 'skipped');
assert.equal(job.trials[2].code, 'ADDRESS_FAMILY_UNAVAILABLE');
assert.equal(bytes, 0); assert.deepEqual(f.hub.state, state); assert.deepEqual(f.store.config, config);
assert.deepEqual(f.hub.attempts, []); assert.equal(f.hub.health.size, 0); assert.equal(job.credentialAccess, false);
assert.equal(job.httpRequests, 0); assert.equal(job.modelRequests, 0);
});
test('local DNS failure is separate from TCP/TLS and arbitrary input, stale revision and TLS overrides are rejected', async t => {
let lookups = 0;
const f = setup(t, { lookup: (_name, _options, callback) => { lookups++; callback(Object.assign(new Error('owned lookup'), { code: 'ENOTFOUND' })); } });
const connection = f.add('https://owned-dns.invalid/v1');
for (const extra of [{ revision: 99 }, { rejectUnauthorized: false }, { apiKey: 'must-not-read' }, { port: 1 }]) assert.throws(() => f.manager.start({ id: connection.id, revision: connection.revision, ...extra }));
const job = await f.run(connection); assert.equal(lookups, 1); assert.equal(job.status, 'failed');
assert.equal(job.stages.dns.code, 'ENOTFOUND'); assert.ok(job.trials.every(row => row.status === 'skipped'));
assert.equal(job.target.host, 'owned-dns.invalid');
});
test('refused TCP socket preserves its stage and cannot masquerade as a TLS or API response', async t => {
const f = setup(t), server = net.createServer(), port = await listener(f, server);
await new Promise(resolve => server.close(resolve));
const job = await f.run(f.add('https://127.0.0.1:' + port + '/v1'));
assert.equal(job.status, 'failed'); assert.equal(job.trials[0].stage, 'tcp'); assert.equal(job.trials[0].code, 'ECONNREFUSED');
assert.equal(job.trials[0].tlsProtocol, undefined); assert.equal(job.modelRequests, 0);
});
test('TLS validation succeeds for a trusted owned certificate and rejects the same untrusted certificate', async t => {
const cert = await material(), f = setup(t, { ca: cert.ca });
const server = tls.createServer(cert); server.on('tlsClientError', () => {});
const port = await listener(f, server), connection = f.add('https://127.0.0.1:' + port + '/v1');
const good = await f.run(connection); assert.equal(good.status, 'passed'); assert.equal(good.trials[0].certificateAuthorized, true);
assert.ok(good.trials[0].certificateFingerprint256); assert.ok(good.trials[0].tlsMs >= 0);
delete f.manager.options.ca;
const bad = await f.run(connection); assert.equal(bad.status, 'failed'); assert.equal(bad.trials[0].stage, 'tls');
assert.ok(['SELF_SIGNED_CERT_IN_CHAIN', 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'].includes(bad.trials[0].code), bad.trials[0].code);
assert.notEqual(bad.trials[0].certificateAuthorized, true);
});
test('a TCP peer that never answers TLS is bounded, and cancellation waits for owned sockets to close', async t => {
let open = 0; const f = setup(t, { stageTimeoutMs: 80 });
const server = net.createServer(socket => { open++; socket.on('data', () => {}); socket.on('close', () => open--); });
const port = await listener(f, server), connection = f.add('https://127.0.0.1:' + port + '/v1');
const timed = await f.run(connection); assert.equal(timed.trials[0].stage, 'tls'); assert.equal(timed.trials[0].code, 'TLS_HANDSHAKE_TIMEOUT');
await until(() => open === 0); f.manager.options.stageTimeoutMs = 10000;
const { job } = f.manager.start({ id: connection.id, revision: connection.revision }); await until(() => open > 0);
assert.throws(() => f.manager.start({ id: connection.id, revision: connection.revision }), /等待/);
const stopped = await f.manager.stop(job.id, {}); assert.equal(stopped.job.status, 'cancelled'); assert.equal(f.manager.active.size, 0);
await until(() => open === 0); const repeated = await f.manager.stop(job.id, {}); assert.deepEqual(repeated, stopped);
});
test('a stalled lookup is cancellable and the overall deadline bounds DNS without late socket work', async t => {
let callback; const f = setup(t, { budgetMs: 65, lookup: (_name, _options, done) => { callback = done; } });
const connection = f.add('https://owned-stalled.invalid/v1'), job = await f.run(connection);
assert.equal(job.status, 'failed'); assert.equal(job.code, 'DIAGNOSTIC_DEADLINE'); assert.equal(job.stages.dns.status, 'cancelled');
const frozen = JSON.stringify(f.manager.view()); callback(null, [{ address: '127.0.0.1', family: 4 }]);
await new Promise(resolve => setImmediate(resolve)); assert.equal(JSON.stringify(f.manager.view()), frozen); assert.equal(f.manager.active.size, 0);
});
test('dual-stack fallback and explicit family results remain separate and never change global DNS defaults', async t => {
const lookup = (_name, options, callback) => callback(null, [{ address: '::1', family: 6 }, { address: '127.0.0.1', family: 4 }]);
const f = setup(t, { transport: new ModelTransport({ environment: {}, lookup }) });
const port = await listener(f, net.createServer()), previous = net.getDefaultAutoSelectFamily();
const job = await f.run(f.add('http://owned-dual.invalid:' + port + '/v1'));
assert.equal(job.status, 'partial'); assert.equal(job.trials[0].status, 'passed'); assert.equal(job.trials[0].family, 'IPv4');
assert.equal(job.trials[2].status, 'failed'); assert.equal(job.trials[2].code, 'ECONNREFUSED');
assert.equal(net.getDefaultAutoSelectFamily(), previous); assert.equal(f.hub.attempts.length, 0);
});
test('proxy settings are reported as scope only; diagnostics do not contact the proxy or leak its credentials', async t => {
let contacts = 0; const f = setup(t), proxy = net.createServer(() => { contacts++; }), proxyPort = await listener(f, proxy);
f.provider.transport = new ModelTransport({ environment: { HTTPS_PROXY: 'http://owned-user:owned-secret@127.0.0.1:' + proxyPort, NO_PROXY: '127.0.0.1' } });
const port = await listener(f, net.createServer()), job = await f.run(f.add('http://127.0.0.1:' + port + '/v1'));
assert.equal(job.route.proxyConfigured, true); assert.equal(job.route.noProxyConfigured, true); assert.equal(job.route.kind, 'direct-endpoint-only');
assert.equal(job.route.modelApiRouteVerified, false); assert.equal(contacts, 0); assert.equal(JSON.stringify(job).includes('owned-secret'), false);
});
test('history is bounded, survives reopening, marks interrupted work and reports changed revisions without rewriting frozen targets', async t => {
const f = setup(t), connection = f.add('http://127.0.0.1:9/v1');
assert.equal(f.store.storage.meta('modelNetworkDiagnostics', null), null, 'Reading an empty history does not create metadata.');
const job = await f.run(connection); f.hub.toggle(connection.id, false);
assert.equal(f.manager.view().jobs[0].stale, true); assert.equal(job.connectionRevision, connection.revision);
const rows = f.store.storage.meta('modelNetworkDiagnostics', []); rows[0].status = 'running'; rows[0].stages.dns.status = 'running';
f.store.storage.setMeta('modelNetworkDiagnostics', rows); const reopened = new NetworkDiagnostics(f.hub);
assert.equal(reopened.view().jobs[0].status, 'interrupted'); assert.equal(reopened.active.size, 0);
assert.equal(reopened.view().jobs[0].stages.dns.status, 'interrupted');
reopened.jobs = Array.from({ length: 30 }, (_, index) => ({ ...job, id: 'retained-' + index })); f.hub.networkChecks = reopened;
const next = f.hub.connection(connection.id); const result = reopened.start({ id: next.id, revision: next.revision });
await reopened.active.get(result.job.id).promise; assert.equal(reopened.view().jobs.length, 30); assert.equal(reopened.view().jobs.at(-1).id, 'retained-1');
});
test('HTTP API, host activity, configuration edits, model start and upgrade honor the same active diagnostic and stop operation', async t => {
const f = setup(t), port = await listener(f, net.createServer(socket => socket.on('data', () => {})));
const connection = f.add('https://127.0.0.1:' + port + '/v1'), runtime = new Runtime(f.store, f.provider), server = http.createServer();
await listener(f, server); const token = 'owned-network-token';
const context = { appDirectory: workspace, server, store: f.store, provider: f.provider, runtime, token, clients: new Set(), started: Date.now(),
getPort: () => server.address().port, exampleProject: f.project, shutdown: () => {} };
server.on('request', createRequestHandler(context));
const api = async (route, input, auth = token) => { const response = await fetch('http://127.0.0.1:' + server.address().port + '/api' + route,
{ method: input === undefined ? 'GET' : 'POST', headers: { 'X-Workbench-Token': auth, 'Content-Type': 'application/json', Connection: 'close' },
...(input === undefined ? {} : { body: JSON.stringify(input) }) }); return { status: response.status, value: await response.json() }; };
assert.equal((await api('/model-center/network-diagnostics', undefined, '')).status, 403);
const started = await api('/model-center/network-diagnostics', { id: connection.id, revision: connection.revision }); assert.equal(started.status, 201);
assert.equal(runtime.hasActivity(), true); assert.equal(hostAuthority(context).activity().counts.networkDiagnostics, 1);
assert.equal((await api('/model-center/connections', { name: 'blocked' })).status, 409);
assert.equal((await api('/sessions/' + f.session.id + '/messages', { content: 'Do not start.' })).status, 409);
assert.throws(() => installUpgrade(context, { appDirectory: workspace, updateServiceInfo: false }), /connection diagnostics/);
const stopped = await api('/model-center/network-diagnostics/' + started.value.job.id + '/stop', {}); assert.equal(stopped.value.job.status, 'cancelled');
assert.equal(runtime.hasActivity(), false); assert.equal(hostAuthority(context).activity().active, false);
const network = f.hub.networkChecks, before = JSON.stringify(network.jobs);
installUpgrade(context, { appDirectory: workspace, updateServiceInfo: false }); assert.equal(f.hub.networkChecks, network); assert.equal(JSON.stringify(network.jobs), before);
assert.equal((await api('/model-center/network-diagnostics')).value.jobs[0].id, started.value.job.id);
});
test('provider shutdown cancels diagnostics and permits explicit reuse after a failed host stop', async t => {
const f = setup(t), port = await listener(f, net.createServer(socket => socket.on('data', () => {}))), connection = f.add('https://127.0.0.1:' + port + '/v1');
const { job } = f.manager.start({ id: connection.id, revision: connection.revision });
await until(() => f.manager.jobs[0].trials.some(row => row.stage === 'tls'));
await f.provider.abortRequests(); assert.equal(f.manager.active.size, 0); assert.equal(f.manager.view().jobs[0].status, 'cancelled');
assert.equal(f.manager.view().jobs[0].id, job.id); assert.throws(() => f.manager.start({ id: connection.id, revision: connection.revision }), /停止/);
f.provider.resumeRequests(); const next = f.manager.start({ id: connection.id, revision: connection.revision });
await f.manager.stop(next.job.id, {}); assert.equal(f.manager.view().jobs[0].status, 'cancelled');
});
@@ -0,0 +1,48 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import {fixture,compatibleFixture,call,completion,assertToolPairs} from './helpers.mjs';
import {Runtime} from '../src/server/runtime.mjs';
import {Provider} from '../src/server/provider.mjs';
import {TaskPlanManager} from '../src/core/tasks/task-plan-manager.mjs';
import {readingStep,beginPlanTask,completeRead} from './fixtures/task-plan-fixture.mjs';
import {artifactPage,recordArtifactRead,readCoverageContext} from '../src/core/context/read-budget.mjs';
test('unchanged proposals cannot hide repeated original-log reads or identical session inspections',async t=>{
const f=fixture(t),original=fs.readFileSync(path.join(f.projectRoot,'README.md'),'utf8');
const artifact=f.store.artifactStore.write(f.session,null,'Actual immutable failure evidence',{kind:'check-log'});f.store.core.artifact.set(artifact.id,artifact);
let requests=0;
const model=await compatibleFixture(t,(route,body)=>{
requests++;if(requests>1){assert.ok(body.messages[0].content.includes(artifact.sha256));assert.match(body.messages[0].content,/ranges_returned_by_tools_in_this_run/);}
return {body:completion([call('read-'+requests,'read_artifact',{artifact_id:artifact.id}),
call('noop-'+requests,'propose_file',{path:'README.md',content:original,reason:'Same content, no write'}),call('inspect-'+requests,'inspect_session',{})])};
});
f.store.saveConfig({mode:'compatible',model:'owned-loop',baseUrl:model.baseUrl,maxSteps:12});
const runtime=new Runtime(f.store,new Provider());f.onCleanup(()=>runtime.stopAll());runtime.start(f.session.id,'核对原结果后推进,不要重放工具。');
await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status,'paused');assert.equal(f.store.core.run.get(f.session.runId).stopReason,'no_progress');assert.equal(requests,3);
assert.equal(f.session.changes.length,0);assert.equal(fs.readFileSync(path.join(f.projectRoot,'README.md'),'utf8'),original);
assert.equal(f.store.artifactStore.read(artifact),'Actual immutable failure evidence');assertToolPairs(f.session.providerMessages);
});
test('reusing a verified proof never resets identical-state detection or creates a new proof',async t=>{
const f=fixture(t);beginPlanTask(f,'读取当前工程依据,不修改文件。');const read=completeRead(f),planner=new TaskPlanManager(f.store);
const plan=await planner.plan(f.session,{expected_revision:0,steps:[readingStep()]});
const input={plan_id:plan.id,expected_revision:plan.revision,step_key:'read',evidence_id:read.id};
const proof=await planner.verify(f.session,input);assert.equal(proof.status,'completed');let requests=0;
const model=await compatibleFixture(t,()=>({body:completion([call('inspect-'+(++requests),'inspect_session',{}),call('proof-'+requests,'verify_task_step',input)])}));
f.store.saveConfig({mode:'compatible',model:'owned-proof-loop',baseUrl:model.baseUrl,maxSteps:12});
const runtime=new Runtime(f.store,new Provider());f.onCleanup(()=>runtime.stopAll());runtime.start(f.session.id,'',true);await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status,'paused');assert.equal(f.store.core.run.get(f.session.runId).stopReason,'no_progress');assert.equal(requests,3);
assert.equal(f.store.core.task_proof.size,1);assert.equal(f.store.savedToolResult(f.session.id,'proof-3').proof_id,proof.proof_id);
assert.equal(f.store.savedToolResult(f.session.id,'proof-3').unchanged_action_count,3);assertToolPairs(f.session.providerMessages);
});
test('coverage merges adjacent pages, separates changed sources and never claims old Run coverage in a continuation',()=>{
const task={activeRunId:'first'},artifact={id:'owned-log',sha256:'a'.repeat(64),kind:'check-log'},text='abcd';
for(const offset of [0,2])recordArtifactRead(task,artifact,artifactPage(artifact,text,{offset,limit:2},{maximumMessageBytes:4000}),'page-'+offset);
assert.deepEqual(readCoverageContext(task,'first').covered[0].ranges,[[0,4]]);assert.equal(readCoverageContext(task,'second'),null);
recordArtifactRead(task,{...artifact,sha256:'b'.repeat(64)},artifactPage(artifact,text,{offset:0,limit:2},{maximumMessageBytes:4000}),'changed','first');
assert.equal(readCoverageContext(task,'first').covered.length,2);assert.equal(task.readProgress.pause,false);
});
@@ -0,0 +1,27 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, workspace } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { inspectOperations } from '../src/server/operations.mjs';
test('paged inspection finds a corrupted old artifact outside the recent fifty and retains original evidence without model/tool execution', t => {
const f = fixture(t), provider = new Provider(), runtime = new Runtime(f.store, provider); let modelCalls = 0;
provider.request = () => { modelCalls++; throw Error('Inspection cannot request a model'); };
for (let i = 0; i < 65; i++) { const value = f.store.artifactStore.write(f.session, null, { owned: i }); f.store.core.artifact.set(value.id, value); }
const first = [...f.store.core.artifact.values()][0], corrupt = path.join(f.store.artifactStore.root, first.relative); fs.appendFileSync(corrupt, 'original-corruption'); const bytes = fs.readFileSync(corrupt);
const context = { store: f.store, provider, runtime, appDirectory: workspace, version: 'owned' };
const recent = inspectOperations(context); assert.equal(recent.artifacts.offset, 15); assert.equal(recent.artifacts.checked, 50); assert.equal(recent.artifacts.remaining, 15); assert.equal(recent.checks.find(value => value.id === 'artifacts').status, 'passed');
const older = inspectOperations({ ...context, parameters: new URLSearchParams('artifactOffset=0&artifactLimit=15') });
assert.equal(older.artifacts.checked, 15); assert.equal(older.artifacts.newerOffset, 15); assert.equal(older.artifacts.results[0].passed, false); assert.equal(older.checks.find(value => value.id === 'artifacts').status, 'failed');
assert.deepEqual(fs.readFileSync(corrupt), bytes); assert.equal(modelCalls, 0); assert.equal(runtime.checks, undefined); assert.equal(older.modelRequests, 0);
});
test('inspection rejects ambiguous/out-of-range pagination and reports unknown tool/task identity without resolving it', t => {
const f = fixture(t), provider = new Provider(), runtime = new Runtime(f.store, provider), context = { store: f.store, provider, runtime, appDirectory: workspace, version: 'owned' };
for (const value of ['artifactLimit=101', 'artifactOffset=-1', 'artifactOffset=1', 'artifactOffset=0&artifactOffset=0', 'extra=0']) assert.throws(() => inspectOperations({ ...context, parameters: new URLSearchParams(value) }));
const original = { id: 'owned-unknown-tool', sessionId: f.session.id, taskId: 'owned-task', runId: 'owned-run', state: 'unknown', requiresVerification: true }; f.store.core.tool.set(original.id, original);
const result = inspectOperations(context); assert.equal(result.unknown.total, 1); assert.equal(result.unknown.items[0].taskId, original.taskId); assert.equal(result.checks.find(value => value.id === 'unknown').status, 'attention'); assert.equal(original.requiresVerification, true);
assert.equal(result.disk.length, 2); assert.ok(result.disk.every(value => /^\d+$/.test(value.freeBytes))); assert.equal(result.sources.length, 0);
});
@@ -0,0 +1,62 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import {spawnSync} from 'node:child_process';
import {fixture} from './helpers.mjs';
import {originalPurgeSource,ORIGINAL_PURGE_KIND} from '../src/core/engines/original-purge-source.mjs';
import {resolveOriginalPurgeSource,selectPurgeStrategy,assertPurgeModelInput} from '../src/core/engines/purge-strategy.mjs';
import {originalPurgeReadRoots,OriginalPurgeRuntime} from '../src/core/engines/original-purge-runtime.mjs';
import lock from '../src/core/engines/original-purge-lock.json' with {type:'json'};
function source(f){const root=path.join(f.root,'original');fs.mkdirSync(root);
for(const input of lock.original){const target=path.join(root,input.relative);fs.mkdirSync(path.dirname(target),{recursive:true});
fs.copyFileSync(path.resolve('docs/original-purge-load-evidence/upstream',input.relative),target);}
return root;
}
test('original source locks all 60 byte-identical files including modules outside the nine-file prompt experiment',t=>{
const f=fixture(t),root=source(f),before=originalPurgeSource(root);assert.equal(before.inputs.length,60);assert.equal(before.version,'1.1.78');
const file=before.inputs.find(input=>input.relative==='lib/host.js');assert.ok(file);
fs.appendFileSync(file.path,'\n// changed outside old prompt lock\n');assert.throws(()=>originalPurgeSource(root),/固定原版输入已变化/);
});
test('a new original executable or missing license cannot silently join the fixed trusted-source profile',t=>{
const f=fixture(t),root=source(f);fs.writeFileSync(path.join(root,'lib/unsealed.js'),'export const added=true;');
assert.throws(()=>originalPurgeSource(root),/文件集合已变化/);fs.unlinkSync(path.join(root,'lib/unsealed.js'));fs.unlinkSync(path.join(root,'NOTICE'));
assert.throws(()=>originalPurgeSource(root));
});
test('original mode has a separate source identity and does not accept retry or privilege fields',t=>{
const f=fixture(t),root=source(f),available=resolveOriginalPurgeSource(f.root,root,{controlledStatus:'unavailable'});
assert.equal(available.kind,ORIGINAL_PURGE_KIND);assert.equal(available.status,'unavailable');assert.equal(available.sourceSha256,undefined);
assert.match(available.message,/Harness/);
for(const field of ['automaticRetry','hooksDenyBypass','hostModification'])assert.throws(()=>selectPurgeStrategy({kind:ORIGINAL_PURGE_KIND,root,prompt:'owned task',[field]:true}),/不接受/);
});
test('actual original model input requires the complete postPrompt once in system context and cannot borrow a user or tool copy',()=>{
const strategy={kind:ORIGINAL_PURGE_KIND,originalConfig:{postPrompt:'[sealed original engineering prompt]'}};
const system=text=>({system:text,messages:[]});assertPurgeModelInput(system(strategy.originalConfig.postPrompt),strategy);
assert.throws(()=>assertPurgeModelInput(system(strategy.originalConfig.postPrompt.repeat(2)),strategy),/唯一完整/);
assert.throws(()=>assertPurgeModelInput({messages:[{role:'user',content:[{type:'text',text:strategy.originalConfig.postPrompt}]}]},strategy),/唯一完整/);
});
test('Windows directory permissions normalize overlapping grants without allowing an unrelated sibling',t=>{
const f=fixture(t),workspace=path.join(f.root,'workspace'),native=path.join(workspace,'native'),sibling=path.join(f.root,'unrelated');
fs.mkdirSync(native,{recursive:true});fs.mkdirSync(sibling);fs.writeFileSync(path.join(sibling,'sentinel.txt'),'protected');
const roots=originalPurgeReadRoots([native,workspace],[]);assert.deepEqual(roots,[fs.realpathSync.native(workspace)]);
const script='const fs=require("node:fs");fs.realpath.native(process.argv[1],(error,result)=>{if(error)throw error;try{fs.readFileSync(process.argv[2]);process.exitCode=2;}catch(error){if(error.code!=="ERR_ACCESS_DENIED")throw error;process.stdout.write("allowed-directory-and-denied-sibling");}});';
const result=spawnSync(process.execPath,['--permission',...roots.flatMap(root=>['--allow-fs-read='+root,'--allow-fs-read='+path.toNamespacedPath(root)]),'-e',script,workspace,path.join(sibling,'sentinel.txt')],{encoding:'utf8',windowsHide:true});
assert.equal(result.status,0,result.stderr);assert.equal(result.stdout,'allowed-directory-and-denied-sibling');
});
test('durability broker rejects foreign authentication, outside paths and wrong file identity before real fsync',async t=>{
const f=fixture(t),directory=path.join(f.root,'run');fs.mkdirSync(directory);const file=path.join(directory,'owned.jsonl');fs.writeFileSync(file,'owned\n');
// Exercise the public broker handler with an explicitly declared object;
// original-source loading is verified separately by the actual Harness driver.
const broker=Object.assign(Object.create(OriginalPurgeRuntime.prototype),{directory,report:{durabilityReceipts:[],durabilityRejections:[]}});
const {Readable}=await import('node:stream');
const run=async(value,authorization='Bearer owned')=>{
const request=Readable.from([Buffer.from(JSON.stringify(value))]);Object.assign(request,{method:'POST',url:'/fsync',headers:{'content-type':'application/json',authorization}});
const response={writeHead(status){this.status=status;},end(body){this.body=body;}};
await broker.handleDurability(request,response,'owned');return response;
};
const stat=fs.statSync(file,{bigint:true}),value={path:file,dev:String(stat.dev),ino:String(stat.ino),size:String(stat.size)};
assert.equal((await run(value,'Bearer foreign')).status,409);assert.equal((await run({...value,path:path.join(f.root,'outside')})).status,409);
assert.equal((await run({...value,ino:'0'})).status,409);assert.equal(broker.report.durabilityReceipts.length,0);
assert.equal((await run(value)).status,200);assert.equal(broker.report.durabilityReceipts.length,1);assert.equal(broker.report.durabilityReceipts[0].operation,'actual-parent-fsync');
});
@@ -0,0 +1,69 @@
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 { proposePatch, proposeFile, readText, applyChange, hash } from '../src/server/files.mjs';
test('exact patches preserve BOM/CRLF, unicode and all unselected bytes and back up the original on apply', t => {
const f = fixture(t), target = path.join(f.projectRoot, 'code.ts');
const before = Buffer.from('\uFEFF// 保留🙂\r\nexport const value = 1;\r\n\r\nexport function keep() { return "原文"; }\r\n'); fs.writeFileSync(target, before);
const file = readText(f.project, 'code.ts'); assert.equal(file.bom, true);
const result = proposePatch(f.store, f.session, { path: file.path, base_sha256: file.sha256.toUpperCase(), reason: 'precise replacement',
edits: [{ old_text: 'export const value = 1;\n', new_text: 'export const value = 2;\nexport const added = true;\n' }] });
assert.equal(result.status, 'awaiting_review'); assert.equal(result.patch.lineEnding, 'crlf'); assert.equal(result.patch.preservedBom, true);
assert.deepEqual(fs.readFileSync(target), before);
const change = f.session.changes[0]; assert.equal(change.editKind, 'exact-patch'); assert.equal(change.baseContent, before.toString('utf8')); assert.equal(change.baseSha256, hash(before));
assert.equal(change.patch.ranges[0].startLine, 2); assert.equal(change.patch.ranges[0].anchorSha256, hash(Buffer.from('export const value = 1;\r\n')));
applyChange(f.store, f.session, result.change_id);
const expected = Buffer.from(before.toString('utf8').replace('export const value = 1;\r\n', 'export const value = 2;\r\nexport const added = true;\r\n'));
assert.deepEqual(fs.readFileSync(target), expected); assert.deepEqual(fs.readFileSync(path.join(f.data, 'backups', f.session.id, change.id + '.txt')), before);
});
test('several patches are matched against one original snapshot in any input order and keep LF', t => {
const f = fixture(t), target = path.join(f.projectRoot, 'code.js'); fs.writeFileSync(target, 'const first = 1;\nconst second = 2;\n// trailing\n');
const file = readText(f.project, 'code.js');
const result = proposePatch(f.store, f.session, { path: file.path, base_sha256: file.sha256, reason: 'two edits', edits: [
{ old_text: 'const second = 2;\r\n', new_text: 'const second = 20;\r\n' }, { old_text: 'const first = 1;', new_text: 'const first = 10;' }] });
assert.equal(result.patch.edits, 2); assert.deepEqual(result.patch.ranges.map(value => value.startLine), [1, 2]);
applyChange(f.store, f.session, result.change_id); assert.equal(fs.readFileSync(target, 'utf8'), 'const first = 10;\nconst second = 20;\n// trailing\n');
});
test('mixed line endings require exact anchors and preserve untouched formatting', t => {
const f = fixture(t), target = path.join(f.projectRoot, 'mixed.txt'); fs.writeFileSync(target, 'first\r\nsecond\nthird\r\n');
const file = readText(f.project, 'mixed.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'mixed' };
assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'first\nsecond', new_text: 'changed' }] }), { status: 409 });
const result = proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'second\n', new_text: 'replacement\n' }] });
assert.equal(result.patch.lineEnding, 'mixed'); applyChange(f.store, f.session, result.change_id);
assert.equal(fs.readFileSync(target, 'utf8'), 'first\r\nreplacement\nthird\r\n');
});
test('ambiguous, overlapping, missing and malformed patch anchors never create a proposal or write', t => {
const f = fixture(t), target = path.join(f.projectRoot, 'anchors.txt'); const original = 'repeat repeat\nabcdef\n'; fs.writeFileSync(target, original);
const file = readText(f.project, 'anchors.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'reject invalid' };
const invalid = [[], [{ old_text: '', new_text: 'x' }], [{ old_text: 'repeat', new_text: 'x' }], [{ old_text: 'missing', new_text: 'x' }],
[{ old_text: 'abc', new_text: 'x' }, { old_text: 'bcd', new_text: 'y' }], [null], [{ old_text: 1, new_text: 'x' }],
[{ old_text: 'abc', new_text: '\0' }], [{ old_text: 'abc', new_text: 42 }], Array.from({ length: 33 }, () => ({ old_text: 'abc', new_text: 'x' }))];
for (const edits of invalid) assert.throws(() => proposePatch(f.store, f.session, { ...args, edits }));
assert.throws(() => proposePatch(f.store, f.session, { ...args, base_sha256: 'invalid', edits: [{ old_text: 'abc', new_text: 'x' }] }));
assert.throws(() => proposePatch(f.store, f.session, { ...args, path: '../anchors.txt', edits: [{ old_text: 'abc', new_text: 'x' }] }));
assert.equal(f.session.changes.length, 0); assert.equal(fs.readFileSync(target, 'utf8'), original);
});
test('stale original hashes and changes before apply preserve owner edits and mark conflicts', t => {
const f = fixture(t), target = path.join(f.projectRoot, 'value.txt'); fs.writeFileSync(target, 'value = 1\n');
const file = readText(f.project, 'value.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'fix', edits: [{ old_text: 'value = 1', new_text: 'value = 2' }] };
const result = proposePatch(f.store, f.session, args); fs.writeFileSync(target, 'owner = 3\n');
assert.throws(() => proposePatch(f.store, f.session, args), { status: 409 });
assert.throws(() => applyChange(f.store, f.session, result.change_id), { status: 409 }); assert.equal(f.session.changes[0].status, 'conflict'); assert.equal(fs.readFileSync(target, 'utf8'), 'owner = 3\n');
});
test('no-op BOM patches/full content produce no change and replacements cannot split Unicode bytes', t => {
const f = fixture(t), target = path.join(f.projectRoot, 'unicode.txt'); const contents = '\uFEFFkeep🙂\n'; fs.writeFileSync(target, contents);
const file = readText(f.project, 'unicode.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'noop' };
assert.equal(proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'keep', new_text: 'keep' }] }).status, 'unchanged');
assert.equal(proposeFile(f.store, f.session, { path: file.path, content: contents }).status, 'unchanged');
assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: '\ud83d', new_text: 'x' }] }));
assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'keep', new_text: '\ud83d' }] }));
assert.equal(f.session.changes.length, 0); assert.equal(fs.readFileSync(target, 'utf8'), contents);
});

Some files were not shown because too many files have changed in this diff Show More