249 lines
29 KiB
JavaScript
249 lines
29 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import http from 'node:http';
|
|
import { completionBody, requestHistory, actualRequest } from '../src/shared/contracts/model-request.mjs';
|
|
import { messagesBody, messagesHistory } from '../src/shared/contracts/anthropic-messages.mjs';
|
|
import { normalizeProtocolResponse } from '../src/core/models/protocols.mjs';
|
|
import { providerStateAudit, providerStateCompatibility } from '../src/shared/contracts/model-provider-state.mjs';
|
|
import { fixture, completion, call } from './helpers.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { Store } from '../src/server/store.mjs';
|
|
import { Runtime } from '../src/server/runtime.mjs';
|
|
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
|
|
|
|
const open = { mode: 'compatible', executionMode: 'unrestricted', approvalMode: 'full', capacityMode: 'passthrough', historyPolicy: 'preserve', maxSteps: 0, maxDurationMs: 0, maxTokens: 0, maxTaskTokens: 0 };
|
|
const messagesConfig = { ...open, protocol: 'anthropic-messages', model: 'claude-opus-5-5', baseUrl: 'https://api.anthropic.com', modelConnectionId: 'a', modelCredentialRevision: 2 };
|
|
const cache = { type: 'ephemeral', ttl: '1h', vendor_unknown: { keep: true } };
|
|
const tool = { type: 'function', cache_control: cache, defer_loading: true, function: { name: 'owned', description: '原工具描述', strict: true, parameters: { type: 'object', properties: {} } } };
|
|
const response = protocol => protocol === 'anthropic-messages'
|
|
? { id: 'owned-message', type: 'message', role: 'assistant', model: 'claude-opus-5-5', content: [{ type: 'thinking', thinking: 'private', signature: 'owned-signature' }, { type: 'text', text: '完成', cache_control: cache }], stop_reason: 'end_turn', usage: { input_tokens: 1, output_tokens: 1 } }
|
|
: { id: 'owned-response', status: 'completed', output: [{ type: 'reasoning', id: 'owned-reasoning', encrypted_content: 'owned-encrypted' }, { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: '完成' }] }], usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 } };
|
|
|
|
test('Messages preserves system/user text cache annotations and complete native tool metadata without mutating the canonical inputs', () => {
|
|
const messages = [{ role: 'system', content: [{ type: 'text', text: '固定规则', cache_control: cache, citations: [] }] }, { role: 'user', content: [{ type: 'input_text', text: '问题', cache_control: cache, source: { kind: 'owned' } }] }];
|
|
const original = structuredClone({ messages, tool });
|
|
const body = completionBody(messagesConfig, messages, [tool]);
|
|
assert.deepEqual(body.system[0], messages[0].content[0]); assert.deepEqual(body.messages[0].content[0], { ...messages[1].content[0], type: 'text' });
|
|
assert.deepEqual(body.tools[0], { name: 'owned', description: '原工具描述', strict: true, defer_loading: true, cache_control: cache, input_schema: tool.function.parameters });
|
|
assert.deepEqual({ messages, tool }, original); assert.equal(body.cache_control, undefined);
|
|
});
|
|
|
|
test('Messages request history round-trip preserves native block order, tool_result metadata and independent TTLs', () => {
|
|
const body = { ...completionBody(messagesConfig, [], []), system: [{ type: 'text', text: '规则', cache_control: cache }], tools: [{ name: 'owned', input_schema: { type: 'object' }, cache_control: cache }],
|
|
messages: [{ role: 'assistant', content: [{ type: 'thinking', thinking: 'opaque', signature: 'sig' }, { type: 'tool_use', id: 'call-own', name: 'owned', input: {}, cache_control: { type: 'ephemeral', ttl: '5m' } }] },
|
|
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'call-own', content: [{ type: 'text', text: '原结果', cache_control: cache }], is_error: true, cache_control: cache }, { type: 'text', text: '补充', cache_control: { type: 'ephemeral', ttl: '5m' } }] }] };
|
|
const history = messagesHistory(body), original = structuredClone(history);
|
|
const restored = messagesBody(messagesConfig, history, body.tools, { nativeParameters: {} });
|
|
assert.deepEqual(restored.system, body.system); assert.deepEqual(restored.messages, body.messages); assert.deepEqual(restored.tools, body.tools); assert.deepEqual(history, original);
|
|
const chat = completionBody({ ...messagesConfig, protocol: 'chat-completions' }, history, []);
|
|
assert.ok(chat.messages.every(value => value.providerInput === undefined && value.providerState === undefined)); assert.ok(!JSON.stringify(chat).includes('opaque'));
|
|
});
|
|
|
|
test('Responses explicit cache breakpoints, tool metadata and top-level controls keep independent upstream fields', () => {
|
|
const config = { ...open, model: 'gpt-6.1-sol', protocol: 'responses', baseUrl: 'https://api.openai.com/v1', nativeParameters: { prompt_cache_options: { mode: 'explicit', ttl: '30m' }, prompt_cache_key: 'owned-session', store: false } };
|
|
const messages = [{ role: 'developer', content: [{ type: 'input_text', text: '固定规则', prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' } }] }];
|
|
const body = completionBody(config, messages, [tool]); assert.deepEqual(body.input[0], messages[0]); assert.deepEqual(body.prompt_cache_options, config.nativeParameters.prompt_cache_options);
|
|
assert.equal(body.tools[0].strict, true); assert.deepEqual(body.tools[0].cache_control, cache); assert.equal(body.tools[0].defer_loading, true); assert.equal(body.max_output_tokens, undefined);
|
|
});
|
|
|
|
test('plain Chat pool members with the same model project opaque history by their own credential and request scope', () => {
|
|
const config = { ...open, protocol: 'chat-completions', model: 'same-model', baseUrl: 'https://owned.invalid/v1', modelConnectionId: 'a', modelCredentialRevision: 2 };
|
|
const members = [{ id: 'a', model: config.model, protocol: config.protocol, baseUrl: config.baseUrl, credentialRevision: 2 },
|
|
{ id: 'b', model: config.model, protocol: config.protocol, baseUrl: config.baseUrl, credentialRevision: 3 }];
|
|
const message = normalizeProtocolResponse(config, { choices: [{ message: { role: 'assistant', content: '原答复', reasoning_content: 'opaque-owned-first' }, finish_reason: 'stop' }] }).choices[0].message;
|
|
const original = structuredClone(message), body = completionBody({ ...config, modelRouting: { members } }, [message], []);
|
|
assert.equal(actualRequest(body, members[0]).messages[0].reasoning_content, 'opaque-owned-first');
|
|
assert.equal(actualRequest(body, members[1]).messages[0].reasoning_content, undefined);
|
|
assert.equal(actualRequest(body, members[1]).messages[0].content, '原答复'); assert.deepEqual(message, original);
|
|
});
|
|
|
|
for (const protocol of ['anthropic-messages', 'responses', 'chat-completions']) test(protocol + ' opaque output state binds actual URL, version/headers, model, connection and credential without discarding historical data', () => {
|
|
const config = { ...messagesConfig, protocol, requestContract: { schemaVersion: 1, defaultsVersion: '2026-11-07.1', anthropicVersion: '2023-06-01', anthropicBetas: [], headers: { 'OpenAI-Project': 'owned-original' }, auth: 'default', completionPath: null, modelsPath: null, versionSource: 'explicit' } };
|
|
const raw = protocol === 'chat-completions' ? { ...completion([], '完成'), choices: [{ message: { role: 'assistant', content: '完成', reasoning_content: 'owned-private' }, finish_reason: 'stop' }] } : response(protocol);
|
|
const message = normalizeProtocolResponse(config, raw).choices[0].message, original = structuredClone(message);
|
|
assert.equal(message.providerState.schemaVersion, 2); assert.equal(providerStateCompatibility(config, message.providerState).applicable, true);
|
|
const same = completionBody(config, [message], []); assert.ok(JSON.stringify(same).includes(protocol === 'anthropic-messages' ? 'owned-signature' : protocol === 'responses' ? 'owned-encrypted' : 'owned-private'));
|
|
for (const patch of [{ model: 'different' }, { modelConnectionId: 'b' }, { modelCredentialRevision: 3 }, { baseUrl: 'https://other.invalid/v1' },
|
|
{ requestContract: { ...config.requestContract, completionPath: '/different/messages' } }, { requestContract: { ...config.requestContract, headers: { 'OpenAI-Project': 'different' } } },
|
|
...(protocol === 'anthropic-messages' ? [{ requestContract: { ...config.requestContract, anthropicVersion: '2026-10-21' } }] : [])]) {
|
|
const changed = { ...config, ...patch }; assert.equal(providerStateCompatibility(changed, message.providerState).applicable, false);
|
|
const body = completionBody(changed, [message], []); assert.ok(!/owned-(signature|encrypted|private)/.test(JSON.stringify(body)));
|
|
assert.equal(providerStateAudit(changed, [message]).variants[0].retainedOnly, 1);
|
|
}
|
|
assert.deepEqual(message, original); assert.deepEqual(raw, protocol === 'chat-completions' ? raw : response(protocol));
|
|
});
|
|
|
|
const untilIdle = async (runtime, session) => { const deadline = Date.now() + 25000; while (runtime.isActive(session.id)) { if (Date.now() > deadline) throw Error('Owned runtime did not finish'); await new Promise(resolve => setTimeout(resolve, 25)); } };
|
|
async function ownedServer(t, fn) {
|
|
const requests = [], server = http.createServer(async (req, res) => { const chunks=[]; for await (const chunk of req) chunks.push(chunk); const body=JSON.parse(Buffer.concat(chunks)); requests.push({ path:req.url,headers:req.headers,body }); await fn(body,res,requests.length); });
|
|
await new Promise(resolve => server.listen(0,'127.0.0.1',resolve)); t.after(()=>new Promise(resolve=>{server.closeAllConnections();server.close(resolve);}));
|
|
return { requests, baseUrl:'http://127.0.0.1:'+server.address().port+'/v1' };
|
|
}
|
|
|
|
test('session affinity survives actual independent Runs and Store/Provider restart while raw reasoning and completed tool IDs remain exact', async t => {
|
|
const f=fixture(t); f.store.saveConfig(open);
|
|
let firstCall=true;
|
|
const a=await ownedServer(t,(body,res)=>{const output=completion(firstCall?[call('owned-original-call','list_files',{path:'',depth:1})]:[],'完成'); firstCall=false; output.choices[0].message.reasoning_content='owned-reasoning-'+body.messages.length; res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(output));});
|
|
const b=await ownedServer(t,(body,res)=>{res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(completion([],'备用完成')));});
|
|
const provider=new Provider(),hub=provider.modelHub(f.store),memberA=hub.upsert({name:'Owned A',model:'owned-a',baseUrl:a.baseUrl,requiresKey:false}),memberB=hub.upsert({name:'Owned B',model:'owned-b',baseUrl:b.baseUrl,requiresKey:false});
|
|
const pool=hub.pool({name:'Owned session affinity',members:[{connectionId:memberA.id,weight:1},{connectionId:memberB.id,weight:1}],sticky:true}); assert.equal(pool.affinityScope,'session'); hub.select({type:'pool',id:pool.id});
|
|
const runtime=new Runtime(f.store,provider); t.after(()=>runtime.stopAll()); runtime.start(f.session.id,'读取工程'); await untilIdle(runtime,f.session); assert.equal(f.session.status,'completed',JSON.stringify(f.session.failure));
|
|
assert.equal(a.requests.length,2); assert.equal(b.requests.length,0); const toolCount=f.store.core.tool.size,history=structuredClone(f.session.providerMessages),firstRun=f.session.runId;
|
|
runtime.start(f.session.id,'继续检查原结果'); await untilIdle(runtime,f.session); assert.equal(a.requests.length,3); assert.equal(b.requests.length,0); assert.notEqual(f.session.runId,firstRun); assert.equal(f.store.core.tool.size,toolCount);
|
|
assert.deepEqual(f.session.providerMessages.slice(0,history.length),history);
|
|
for(const plan of f.store.core.context_plan.values()) assert.doesNotThrow(()=>verifyPlanEvidence(f.store,f.session,plan));
|
|
const affinity=structuredClone(f.session.modelAffinity),storedHistory=structuredClone(f.session.providerMessages);f.store.close();const reopened=new Store(f.data);f.onCleanup(()=>reopened.close()); const session=reopened.session(f.session.id); assert.deepEqual(session.providerMessages,storedHistory);assert.deepEqual(session.modelAffinity,affinity);
|
|
const restoredProvider=new Provider(),restoredHub=restoredProvider.modelHub(reopened),restoredRuntime=new Runtime(reopened,restoredProvider);t.after(()=>restoredRuntime.stopAll());
|
|
restoredHub.cursors.set(pool.id,1);restoredRuntime.start(session.id,'继续核对原记录');await untilIdle(restoredRuntime,session);assert.equal(a.requests.length,4);assert.equal(b.requests.length,0);assert.equal(reopened.core.tool.size,toolCount);
|
|
const latest=restoredHub.view().attempts.find(value=>value.runId===session.runId);assert.equal(latest.routingDecision.affinity.source,'saved-completed-response');assert.equal(latest.routingDecision.affinity.continued,true);
|
|
for(const plan of reopened.core.context_plan.values()) assert.doesNotThrow(()=>verifyPlanEvidence(reopened,session,plan));
|
|
});
|
|
|
|
test('task affinity ends with its Task; explicit manual selection wins and old pools keep their original Run scope', async t => {
|
|
const f=fixture(t);f.store.saveConfig(open);const model=await ownedServer(t,(body,res)=>{res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(completion([],'完成')));});
|
|
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'A',model:'a',baseUrl:model.baseUrl,requiresKey:false}),b=hub.upsert({name:'B',model:'b',baseUrl:model.baseUrl,requiresKey:false});
|
|
const pool=hub.pool({name:'Task',members:[{connectionId:a.id},{connectionId:b.id}],affinityScope:'task'});hub.select({type:'pool',id:pool.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
|
|
runtime.start(f.session.id,'完成第一个独立任务');await untilIdle(runtime,f.session);const oldTask=f.session.taskId;
|
|
runtime.start(f.session.id,'完成另一个独立目标');await untilIdle(runtime,f.session);assert.notEqual(f.session.taskId,oldTask);assert.deepEqual(model.requests.map(value=>value.body.model),['a','b']);
|
|
hub.select({type:'connection',id:a.id});runtime.start(f.session.id,'继续原检查');await untilIdle(runtime,f.session);assert.equal(model.requests.at(-1).body.model,'a');
|
|
const legacy=hub.state.pools.find(value=>value.id===pool.id);delete legacy.affinityScope;hub.save();const prepared=hub.prepare(f.store.config,{type:'pool',id:pool.id});assert.equal(prepared.modelRouting.affinityScope,'run');
|
|
const before=structuredClone(hub.state.pools);assert.throws(()=>hub.pool({...legacy,affinityScope:'invalid'}),/保持范围/);assert.deepEqual(hub.state.pools,before);
|
|
});
|
|
|
|
test('native state sidecars are created by the adapter; raw extensions and unknown schema cannot impersonate a saved provider scope', () => {
|
|
const config={...open,protocol:'chat-completions',model:'owned',baseUrl:'https://owned.invalid/v1',modelConnectionId:'owned',modelCredentialRevision:1};
|
|
const native=normalizeProtocolResponse(config,{choices:[{message:{role:'assistant',content:'OK',reasoning_content:'owned-private'},finish_reason:'stop'}]}).choices[0].message;
|
|
const raw={choices:[{message:{role:'assistant',content:'OK',providerState:native.providerState,providerInput:{protocol:'anthropic-messages',content:[]}},finish_reason:'stop'}]};
|
|
const normalized=normalizeProtocolResponse(config,raw);assert.equal(normalized.choices[0].message.providerState,undefined);assert.equal(normalized.choices[0].message.providerInput,undefined);assert.ok(raw.choices[0].message.providerState);
|
|
assert.equal(providerStateCompatibility(config,{...native.providerState,schemaVersion:999}).reason,'unknown-state-schema');
|
|
});
|
|
|
|
test('disabled remembered members and changed request scopes select current eligible routes while old opaque history stays saved', async t => {
|
|
const f=fixture(t);f.store.saveConfig(open);
|
|
const model=await ownedServer(t,(body,res)=>{const output=completion([],'完成');output.choices[0].message.reasoning_content='opaque-reasoning-for-'+body.model;res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(output));});
|
|
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'A',model:'a',baseUrl:model.baseUrl,requiresKey:false}),b=hub.upsert({name:'B',model:'b',baseUrl:model.baseUrl,requiresKey:false});
|
|
const pool=hub.pool({name:'Session',members:[{connectionId:a.id},{connectionId:b.id}],affinityScope:'session'});hub.select({type:'pool',id:pool.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
|
|
runtime.start(f.session.id,'记录第一个状态');await untilIdle(runtime,f.session);const old=structuredClone(f.session.providerMessages);assert.equal(model.requests[0].body.model,'a');
|
|
hub.toggle(a.id,false);runtime.start(f.session.id,'继续已有任务');await untilIdle(runtime,f.session);assert.equal(model.requests[1].body.model,'b');assert.ok(model.requests[1].body.messages.every(message=>message.reasoning_content===undefined));
|
|
assert.deepEqual(f.session.providerMessages.slice(0,old.length),old);
|
|
const switched=hub.view().attempts.find(value=>value.runId===f.session.runId);assert.equal(switched.routingDecision.affinity.reason,'saved-member-not-eligible');assert.equal(switched.nativeStateAudit.retainedOnly,1);
|
|
const previous=hub.connection(b.id);hub.upsert({...previous,requestContract:{...previous.requestContract,headers:{'OpenAI-Project':'new-owned'}}});
|
|
runtime.start(f.session.id,'继续在新项目头下核对');await untilIdle(runtime,f.session);const last=model.requests.at(-1);assert.equal(last.headers['openai-project'],'new-owned');assert.ok(last.body.messages.every(message=>message.reasoning_content===undefined));
|
|
const changed=hub.view().attempts.find(value=>value.runId===f.session.runId);assert.equal(changed.routingDecision.affinity.reason,'saved-provider-scope-changed');assert.equal(changed.nativeStateAudit.retainedOnly,2);
|
|
for(const plan of f.store.core.context_plan.values())assert.doesNotThrow(()=>verifyPlanEvidence(f.store,f.session,plan));
|
|
});
|
|
|
|
test('failed and unknown upstream requests keep a single original attempt and do not overwrite completed session affinity or dispatch tools', async t => {
|
|
const f=fixture(t);f.store.saveConfig(open);let fail=false;
|
|
const model=await ownedServer(t,(body,res)=>{if(fail){res.writeHead(400,{'Content-Type':'application/json','x-request-id':'owned-original-400'});res.end(JSON.stringify({error:{type:'invalid_request_error',message:'owned-original-rejection'}}));}else{res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(completion([],'完成')));}});
|
|
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'Owned',model:'owned',baseUrl:model.baseUrl,requiresKey:false}),pool=hub.pool({name:'Session',members:[{connectionId:a.id}],affinityScope:'session'});hub.select({type:'pool',id:pool.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
|
|
runtime.start(f.session.id,'取得原完成结果');await untilIdle(runtime,f.session);const saved=structuredClone(f.session.modelAffinity),history=structuredClone(f.session.providerMessages),tools=f.store.core.tool.size;
|
|
fail=true;runtime.start(f.session.id,'继续观察上游错误');await untilIdle(runtime,f.session);assert.equal(model.requests.length,2);assert.deepEqual(f.session.modelAffinity,saved);assert.equal(f.store.core.tool.size,tools);assert.deepEqual(f.session.providerMessages.slice(0,history.length),history);
|
|
const attempt=hub.view().attempts.find(value=>value.runId===f.session.runId);assert.equal(attempt.status,'failed');assert.equal(attempt.httpStatus,400);assert.match(JSON.stringify(attempt.diagnostic),/owned-original-rejection/);
|
|
assert.equal(attempt.routingDecision.affinity.continued,true);
|
|
});
|
|
|
|
test('unknown saved affinity is retained and optional affinity persistence cannot change a completed upstream response into an error', async t => {
|
|
const f = fixture(t); f.store.saveConfig(open);
|
|
const model = await ownedServer(t, (body, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(completion([], '完成'))); });
|
|
const provider = new Provider(), hub = provider.modelHub(f.store);
|
|
const member = hub.upsert({ name: 'Owned', model: 'owned', baseUrl: model.baseUrl, requiresKey: false });
|
|
const pool = hub.pool({ name: 'Session', members: [{ connectionId: member.id }], affinityScope: 'session' }); hub.select({ type: 'pool', id: pool.id });
|
|
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
|
const unknown = { schemaVersion: 999, bindings: { original: true }, extension: ['keep-original'] };
|
|
f.session.modelAffinity = structuredClone(unknown); f.store.save(f.session);
|
|
runtime.start(f.session.id, '完成独立请求'); await untilIdle(runtime, f.session);
|
|
assert.equal(f.session.status, 'completed'); assert.deepEqual(f.session.modelAffinity, unknown); assert.equal(model.requests.length, 1);
|
|
let attempt = hub.view().attempts.find(value => value.runId === f.session.runId);
|
|
assert.equal(attempt.routingDecision.affinity.reason, 'unknown-affinity-state'); assert.equal(attempt.affinityPersistence.status, 'retained-unknown');
|
|
delete f.session.modelAffinity; f.store.save(f.session);
|
|
const save = f.store.save.bind(f.store);
|
|
f.store.save = (session, event) => { if (event?.type === 'model.affinity_recorded') throw new Error('owned-optional-affinity-write-failed'); return save(session, event); };
|
|
runtime.start(f.session.id, '继续实际请求'); await untilIdle(runtime, f.session); f.store.save = save;
|
|
attempt = hub.view().attempts.find(value => value.runId === f.session.runId);
|
|
assert.equal(f.session.status, 'completed'); assert.equal(attempt.status, 'completed'); assert.equal(attempt.affinityPersistence.status, 'unknown');
|
|
assert.match(attempt.affinityPersistence.reason, /owned-optional-affinity-write-failed/); assert.equal(model.requests.length, 2);
|
|
});
|
|
|
|
test('task affinity survives a stopped response and explicit same-Task continuation without dispatching the interrupted request again', async t => {
|
|
const f = fixture(t); f.store.saveConfig(open); let block = false;
|
|
const a = await ownedServer(t, (body, res) => {
|
|
if (block) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.write('{'); return; }
|
|
res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(completion([], '完成')));
|
|
});
|
|
const b = await ownedServer(t, (body, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(completion([], '备用完成'))); });
|
|
const provider = new Provider(), hub = provider.modelHub(f.store);
|
|
const memberA = hub.upsert({ name: 'A', model: 'a', baseUrl: a.baseUrl, requiresKey: false });
|
|
const memberB = hub.upsert({ name: 'B', model: 'b', baseUrl: b.baseUrl, requiresKey: false });
|
|
const pool = hub.pool({ name: 'Task', members: [{ connectionId: memberA.id }, { connectionId: memberB.id }], affinityScope: 'task' });
|
|
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
|
runtime.start(f.session.id, '完成任务'); await untilIdle(runtime, f.session);
|
|
const taskId = f.session.taskId, original = structuredClone(f.session.modelAffinity), tools = f.store.core.tool.size;
|
|
block = true; runtime.start(f.session.id, '', true);
|
|
const deadline = Date.now() + 20000; while (a.requests.length < 2) { assert.ok(Date.now() < deadline); await new Promise(resolve => setTimeout(resolve, 25)); }
|
|
await runtime.stop(f.session.id); await untilIdle(runtime, f.session);
|
|
assert.equal(f.session.taskId, taskId); assert.deepEqual(f.session.modelAffinity, original); assert.equal(b.requests.length, 0);
|
|
const interrupted = hub.view().attempts.find(value => value.runId === f.session.runId); assert.equal(interrupted.status, 'interrupted');
|
|
block = false; runtime.resume(f.session.id); await untilIdle(runtime, f.session);
|
|
assert.equal(f.session.taskId, taskId); assert.equal(a.requests.length, 3); assert.equal(b.requests.length, 0); assert.equal(f.store.core.tool.size, tools);
|
|
const resumed = hub.view().attempts.find(value => value.runId === f.session.runId);
|
|
assert.equal(resumed.routingDecision.affinity.source, 'saved-completed-response');
|
|
});
|
|
|
|
for (const protocol of ['anthropic-messages', 'responses']) test(protocol + ' real owned SSE retains native cache blocks and opaque output across turns, then projects ordinary history after a header change', async t => {
|
|
const f = fixture(t); f.store.saveConfig(open);
|
|
const model = await ownedServer(t, (body, res, index) => {
|
|
const tools = index === 1;
|
|
const value = protocol === 'anthropic-messages'
|
|
? { id: 'owned-native-' + index, type: 'message', role: 'assistant', model: body.model,
|
|
content: [{ type: 'thinking', thinking: 'opaque-native', signature: 'owned-native-signature-' + index },
|
|
{ type: 'text', text: '完成', cache_control: cache },
|
|
...(tools ? [{ type: 'tool_use', id: 'owned-native-tool', name: 'list_files', input: { path: '', depth: 1 }, cache_control: { type: 'ephemeral', ttl: '5m' } }] : [])],
|
|
stop_reason: tools ? 'tool_use' : 'end_turn', usage: { input_tokens: 10, output_tokens: 3, cache_read_input_tokens: 2, cache_creation_input_tokens: 1 } }
|
|
: { id: 'owned-native-' + index, model: body.model, status: 'completed',
|
|
output: [{ type: 'reasoning', id: 'owned-reasoning-' + index, encrypted_content: 'owned-encrypted-' + index, summary: [] },
|
|
{ type: 'message', id: 'owned-message-' + index, role: 'assistant', phase: 'final_answer', content: [{ type: 'output_text', text: '完成', annotations: [] }] },
|
|
...(tools ? [{ type: 'function_call', id: 'owned-call-item', call_id: 'owned-native-tool', name: 'list_files', arguments: JSON.stringify({ path: '', depth: 1 }), status: 'completed' }] : [])],
|
|
usage: { input_tokens: 10, output_tokens: 3, input_tokens_details: { cached_tokens: 2 } } };
|
|
const event = value => 'data: ' + JSON.stringify(value) + '\n\n';
|
|
res.writeHead(200, { 'Content-Type': 'text/event-stream' });
|
|
if (protocol === 'anthropic-messages') {
|
|
res.write(event({ type: 'message_start', message: { ...value, content: [], stop_reason: null } }));
|
|
value.content.forEach((block, index) => { res.write(event({ type: 'content_block_start', index, content_block: block })); res.write(event({ type: 'content_block_stop', index })); });
|
|
res.write(event({ type: 'message_delta', delta: { stop_reason: value.stop_reason }, usage: value.usage })); res.end(event({ type: 'message_stop' }));
|
|
} else {
|
|
res.write(event({ type: 'response.created', sequence_number: 0, response: { ...value, status: 'in_progress', output: [] } }));
|
|
res.end(event({ type: 'response.completed', sequence_number: 1, response: value }));
|
|
}
|
|
});
|
|
const provider = new Provider(), hub = provider.modelHub(f.store);
|
|
const member = hub.upsert({ name: 'Owned native', model: protocol === 'anthropic-messages' ? 'claude-opus-5-5' : 'gpt-6.1-sol',
|
|
protocol, streaming: true, baseUrl: model.baseUrl, requiresKey: false });
|
|
const pool = hub.pool({ name: 'Native session', members: [{ connectionId: member.id }], affinityScope: 'session' }); hub.select({ type: 'pool', id: pool.id });
|
|
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
|
runtime.start(f.session.id, '读取工程然后结束'); await untilIdle(runtime, f.session);
|
|
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.failure)); assert.equal(model.requests.length, 2);
|
|
const second = model.requests[1].body;
|
|
if (protocol === 'anthropic-messages') {
|
|
const blocks = second.messages.filter(message => message.role === 'assistant').flatMap(message => message.content);
|
|
assert.ok(blocks.some(block => block.signature === 'owned-native-signature-1'));
|
|
assert.deepEqual(blocks.find(block => block.type === 'text' && block.text === '完成').cache_control, cache);
|
|
assert.equal(blocks.find(block => block.type === 'tool_use').cache_control.ttl, '5m');
|
|
} else {
|
|
assert.ok(second.input.some(item => item.encrypted_content === 'owned-encrypted-1'));
|
|
assert.equal(second.input.find(item => item.type === 'message' && item.role === 'assistant').phase, 'final_answer');
|
|
assert.ok(second.input.some(item => item.type === 'function_call_output' && item.call_id === 'owned-native-tool'));
|
|
assert.equal(second.max_output_tokens, undefined);
|
|
}
|
|
const old = structuredClone(f.session.providerMessages), tools = f.store.core.tool.size;
|
|
hub.upsert({ ...hub.connection(member.id), requestContract: { ...hub.connection(member.id).requestContract, headers: { 'X-Owned-Scope': 'changed' } } });
|
|
runtime.start(f.session.id, '追加说明然后结束'); await untilIdle(runtime, f.session);
|
|
assert.equal(model.requests.length, 3); assert.equal(f.store.core.tool.size, tools); assert.deepEqual(f.session.providerMessages.slice(0, old.length), old);
|
|
const changed = model.requests.at(-1).body;
|
|
if (protocol === 'anthropic-messages') assert.ok(changed.messages.flatMap(message => message.content).every(block => !['thinking', 'redacted_thinking'].includes(block.type)));
|
|
else assert.ok(changed.input.every(item => item.type !== 'reasoning'));
|
|
const attempt = hub.view().attempts.find(value => value.runId === f.session.runId); assert.equal(attempt.nativeStateAudit.retainedOnly, 2);
|
|
for (const plan of f.store.core.context_plan.values()) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
|
|
});
|