feat(models): execute Responses and validated streams with capability routing

This commit is contained in:
Agent Workbench Dev
2026-10-31 22:27:53 +08:00
parent 578cf88eca
commit eeb11d6a76
24 changed files with 856 additions and 52 deletions
+108
View File
@@ -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 { 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';
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('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 }; }
+57
View File
@@ -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');
});
+145
View File
@@ -0,0 +1,145 @@
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 } 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 });
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('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('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('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));
});
});