fix(models): retain upstream identity and pause strict mismatches
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// This reports observed Task consumption, never the model selected in bootstrap.
|
||||
// Legacy model fields describe the dispatched route, not authenticated model weights.
|
||||
// Public execution views may contain only the most recent Runs; gaps stay explicit.
|
||||
export function observedTaskModels(state,taskId=state?.execution?.task?.id){
|
||||
const execution=state?.execution??{},runs=new Map(),attempts=new Map(),issues=[];
|
||||
@@ -16,6 +16,8 @@ export function observedTaskModels(state,taskId=state?.execution?.task?.id){
|
||||
model:typeof receipt.model==='string'&&receipt.model.trim()?receipt.model:null,
|
||||
protocol:typeof receipt.protocol==='string'&&receipt.protocol.trim()?receipt.protocol:null,
|
||||
connectionId:receipt.connectionId??null,outcome:receipt.outcome??null,
|
||||
requestedModel:receipt.modelIdentity?.requestedModel??receipt.model??null,
|
||||
modelIdentity:receipt.modelIdentity?structuredClone(receipt.modelIdentity):null,
|
||||
usageStatus:receipt.usageStatus??'unknown',usage:receipt.usage??null};
|
||||
const previous=attempts.get(attempt.attemptId);
|
||||
if(previous){
|
||||
@@ -36,7 +38,12 @@ export function observedTaskModels(state,taskId=state?.execution?.task?.id){
|
||||
if(!taskId)issues.push({kind:'missing_task_identity'});
|
||||
if(declared!==null&&declared!==observed.length)issues.push({kind:'request_coverage_gap',recordedRequests:declared,observedAttempts:observed.length});
|
||||
const status=issues.length?'incomplete':!observed.length?declared===0?'not_observed':'unavailable':models.length>1?'mixed':'observed';
|
||||
const identities=observed.map(attempt=>attempt.modelIdentity),identityCounts={};
|
||||
for(const identity of identities){const key=identity?.status??'historical_unchecked';identityCounts[key]=(identityCounts[key]??0)+1;}
|
||||
return {schemaVersion:1,source:'task_run_consumption',taskId:taskId??null,status,
|
||||
identityScope:'request_routes_and_upstream_reported_identifiers_not_model_weights',
|
||||
responseIdentity:{counts:identityCounts,reportedModels:[...new Set(identities.flatMap(value=>value?.reportedModels??[]))],
|
||||
allAccepted:observed.length>0&&issues.length===0&&identities.every(value=>value?.complete===true&&['matched','mapped'].includes(value.status))},
|
||||
model:['observed','mixed'].includes(status)&&models.length===1?models[0]:null,models,
|
||||
observedAttempts:observed.length,recordedRequests:declared,routes:[...routes.values()],attempts:observed,issues};
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ try {
|
||||
const editor = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await editor.getByLabel('连接名称', { exact: true }).fill('Fixture Primary');
|
||||
await editor.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:9/v1');
|
||||
await editor.getByLabel('实际模型名称', { exact: true }).fill('fixture-primary');
|
||||
await editor.getByLabel('请求模型名称', { exact: true }).fill('fixture-primary');
|
||||
await editor.getByLabel('临时 API Key', { exact: true }).fill('ui-owned-primary-key');
|
||||
await editor.getByRole('button', { name: '保存模型连接', exact: true }).click();
|
||||
await expect(dialog.locator('.mc-connection').filter({ hasText: 'Fixture Primary' })).toContainText('已设置,重启需重新填写');
|
||||
@@ -42,7 +42,7 @@ try {
|
||||
const second = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await second.getByLabel('连接名称', { exact: true }).fill('Fixture Reserve');
|
||||
await second.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:9/v1');
|
||||
await second.getByLabel('实际模型名称', { exact: true }).fill('fixture-reserve');
|
||||
await second.getByLabel('请求模型名称', { exact: true }).fill('fixture-reserve');
|
||||
await second.getByRole('button', { name: '保存模型连接', exact: true }).click();
|
||||
await dialog.getByRole('button', { name: '模型池', exact: true }).click();
|
||||
await dialog.getByRole('button', { name: '新建模型池', exact: true }).click();
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import assert from 'node:assert/strict';
|
||||
import {createHash,randomUUID} from 'node:crypto';
|
||||
import {chromium,expect} from 'playwright/test';
|
||||
import {fixture,launchWorkbench,until} from '../tests/helpers.mjs';
|
||||
import {Provider} from '../src/server/provider.mjs';
|
||||
|
||||
const directory=path.resolve('.qa','model-identity-ui-'+randomUUID()),disposers=[],checks=[],screenshots=[],errors=[],requests=[];
|
||||
fs.mkdirSync(directory,{recursive:true});const t={after:fn=>disposers.push(fn)},f=fixture(t);
|
||||
const upstream=http.createServer(async(req,res)=>{
|
||||
const chunks=[];for await(const chunk of req)chunks.push(chunk);requests.push(JSON.parse(Buffer.concat(chunks)));
|
||||
res.writeHead(200,{'Content-Type':'application/json'});
|
||||
res.end(JSON.stringify({model:'owned-upstream-snapshot',choices:[{message:{role:'assistant',content:'',tool_calls:[
|
||||
{id:'owned-ui-call',type:'function',function:{name:'propose_file',arguments:JSON.stringify({path:'unsafe.txt',content:'must not execute',reason:'fixture'})}}]}}],
|
||||
usage:{prompt_tokens:13,completion_tokens:7,total_tokens:20}}));
|
||||
});
|
||||
await new Promise(resolve=>upstream.listen(0,'127.0.0.1',resolve));
|
||||
const provider=new Provider(),hub=provider.modelHub(f.store),connection=hub.upsert({name:'身份核对 · 自有接口夹具',model:'owned-requested-model',
|
||||
baseUrl:'http://127.0.0.1:'+upstream.address().port+'/v1',requiresKey:false});
|
||||
hub.select({type:'connection',id:connection.id});f.store.close();
|
||||
const host=await launchWorkbench(t,f.data);assert.notEqual(new URL(host.url).port,'4318');
|
||||
let browser,page,result;
|
||||
const capture=async name=>{const file=path.join(directory,name+'.png');await page.screenshot({path:file});screenshots.push({path:file,sha256:createHash('sha256').update(fs.readFileSync(file)).digest('hex')});};
|
||||
const mark=name=>{checks.push(name);console.log('PASS '+name);};
|
||||
const openCenter=async()=>{await page.getByRole('button',{name:'打开模型中心',exact:true}).click();return page.getByRole('dialog',{name:'模型中心',exact:true});};
|
||||
try{
|
||||
browser=await chromium.launch({channel:'msedge',headless:true});page=await browser.newPage({viewport:{width:1440,height:1000}});
|
||||
page.on('pageerror',e=>errors.push(e.message));await page.goto(host.url);await page.waitForLoadState('networkidle');
|
||||
await page.getByLabel('当前项目').selectOption(f.project.id);
|
||||
let center=await openCenter();const card=()=>center.locator('.mc-connection').filter({has:page.getByRole('heading',{name:connection.name,exact:true})});
|
||||
await card().getByRole('button',{name:'编辑',exact:true}).click();const form=()=>page.getByRole('form',{name:'编辑模型连接',exact:true});
|
||||
await form().getByLabel('型号核对',{exact:true}).selectOption('require-match');
|
||||
await form().getByLabel('允许的返回型号',{exact:false}).fill('snapshot-a, snapshot-b');
|
||||
await form().getByLabel('型号映射依据',{exact:true}).fill('自有接口映射约定 revision 1');
|
||||
await capture('settings-desktop');await page.setViewportSize({width:390,height:844});
|
||||
await form().getByRole('button',{name:'保存模型连接',exact:true}).scrollIntoViewIfNeeded();
|
||||
assert.ok(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth));await capture('settings-390');
|
||||
await form().getByRole('button',{name:'保存模型连接',exact:true}).click();await expect(form()).toHaveCount(0);
|
||||
await page.reload();center=await openCenter();await card().getByRole('button',{name:'编辑',exact:true}).click();
|
||||
await expect(form().getByLabel('型号核对',{exact:true})).toHaveValue('require-match');
|
||||
await expect(form().getByLabel('允许的返回型号',{exact:false})).toHaveValue('snapshot-a, snapshot-b');
|
||||
await expect(form().getByLabel('型号映射依据',{exact:true})).toHaveValue('自有接口映射约定 revision 1');
|
||||
mark('strict policy and multiple declared aliases save and reload at desktop and 390px without a model call');assert.equal(requests.length,0);
|
||||
await form().getByLabel('允许的返回型号',{exact:false}).fill('');await form().getByLabel('型号映射依据',{exact:true}).fill('');
|
||||
await form().getByRole('button',{name:'保存模型连接',exact:true}).click();await expect(form()).toHaveCount(0);await page.keyboard.press('Escape');
|
||||
await page.setViewportSize({width:1440,height:1000});const prompt='核对当前项目,身份不一致应暂停';
|
||||
await page.getByLabel('任务内容').fill(prompt);await page.getByRole('button',{name:'发送任务',exact:true}).click();let session;
|
||||
await until(async()=>{const id=(await host.api('/sessions')).find(v=>v.title===prompt)?.id;session=id&&await host.api('/sessions/'+id);return session?.status==='paused';},15000);
|
||||
assert.equal(requests.length,1);assert.equal(session.changes.length,0);assert.equal(session.execution.run.stopReason,'model_identity');
|
||||
await expect(page.locator('.model-identity-details').first()).toContainText('请求与返回型号不一致');await page.reload();
|
||||
const failure=page.locator('.failure-card').last();await failure.locator('.model-identity-details summary').click();
|
||||
await expect(failure).toContainText('owned-requested-model');await expect(failure).toContainText('owned-upstream-snapshot');
|
||||
await expect(failure).toContainText('不一致或无法核对时暂停');await capture('paused-desktop');
|
||||
await page.setViewportSize({width:390,height:844});await failure.locator('.model-identity-details').scrollIntoViewIfNeeded();await capture('paused-390');
|
||||
assert.ok(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth));
|
||||
const downloadPromise=page.waitForEvent('download');await failure.getByRole('button',{name:'下载原响应',exact:true}).click();
|
||||
const download=await downloadPromise,saved=path.join(directory,'original-response.json');await download.saveAs(saved);
|
||||
const original=fs.readFileSync(saved),identity=session.execution.run.consumption[0].modelIdentity;
|
||||
assert.equal(createHash('sha256').update(original).digest('hex'),identity.responseSource.sha256);assert.equal(JSON.parse(original).model,'owned-upstream-snapshot');
|
||||
mark('strict mismatch pauses before any proposal, retains reported usage, persists on refresh and downloads SHA-verified original');
|
||||
center=await openCenter();await center.getByRole('button',{name:'请求账本',exact:true}).click();
|
||||
const attempt=center.locator('.mc-attempts article').first();await attempt.locator('.model-identity-details').first().locator('summary').click();
|
||||
await expect(attempt).toContainText('owned-upstream-snapshot');await expect(attempt).toContainText(identity.responseSource.sha256);
|
||||
await capture('ledger-390');assert.deepEqual(errors,[]);mark('physical request ledger retains the same identity and source without page errors');
|
||||
result={success:true,checks,screenshots,errors,modelRequests:requests.length,realModelRequests:0};
|
||||
}catch(error){if(page)await capture('failure').catch(()=>{});result={success:false,error:error.stack,checks,screenshots,errors,realModelRequests:0};throw error;}
|
||||
finally{
|
||||
fs.writeFileSync(path.join(directory,'result.json'),JSON.stringify(result,null,2));console.log(JSON.stringify({success:result.success,directory,checks:checks.length}));
|
||||
await browser?.close();upstream.closeAllConnections();await new Promise(resolve=>upstream.close(resolve));for(const action of disposers.reverse())await action();
|
||||
}
|
||||
Reference in New Issue
Block a user