fix(dsh): recognize configured native models and pin diagnosis selection
Core regression / Python 3.12 core, intervention UI and release configuration (push) Failing after 20s
Core regression / JavaScript syntax (push) Successful in 4s
Core regression / Python 3.12 core, intervention UI and release configuration (pull_request) Failing after 20s
Core regression / JavaScript syntax (pull_request) Successful in 3s

This commit is contained in:
阿白叔 committed 2026-11-05 17:40:50 +08:00
1 parent eba935e710
commit 8837587ce9
9 files changed
+149 -36

No files matched your search

@@ -40,3 +40,11 @@ node tools/verify_dsh_model_results.mjs
## 验证边界
本次仅验证新增能力及直接受影响的回归,不运行旧功能全量测试,不重新打包 EXE。真实模型时延、远端工单和 CAD 替换结果不由上述离线结果证明。
## 原生模型设置后的状态灯修正
手动验证发现:原生模型设置会保存自定义提供商及凭据,但仅添加提供商时没有更新 `agent-default-model`。只检查内置官方默认模型会误报缺少凭据。现在没有显式默认配置且内置默认不可用时,从已保存的提供商中选择有凭据的模型,返回经过校验的选择元数据;自动诊断会话使用同一选择及推理强度,不改写用户全局默认配置。有显式默认配置时仍按该配置检查。
定向回归 **55 passed**(与上面的验收范围有重叠,不累计):`tests/dsh/test_availability.py`、`tests/ui/test_dsh_status_indicator.py`、`tests/ui/test_dsh_diagnosis_lifecycle.py`、`tests/ui/test_dsh_ai_nonblocking.py`。原生模型离线检查 **23 场景通过**,配置文件字节不变;原生诊断取消与会话选模检查通过,包含禁止自动诊断保存全局默认的断言。
使用当前预览中实际保存的模型配置,通过 Windows UI Automation 开启自动介入,状态由灰色静默切换为绿色“AI 自动介入:待机”,前后事件记录均为 0。没有发送真实模型请求、操作 CAD 或提交工单。
+42 -28
View File
@@ -8,7 +8,7 @@ import {overlay,refreshApplicationModels} from './start.mjs';
const require=createRequire(import.meta.url);
const result=code=>({available:code==='available',code});
const ready=(source,ref)=>({...result('available'),source,environment_refs:ref?[ref]:[]});
const ready=(source,ref,selection)=>({...result('available'),source,environment_refs:ref?[ref]:[],model_selection:selection});
async function optional(filename,parse) {
try{return parse(await readFile(filename,'utf8'));}
@@ -63,35 +63,49 @@ export async function checkModelAvailability(home) {
const refreshed=refreshApplicationModels(rows,application,{legacy,storedHostKey,previousApplication,boundDx});
const effective=composeEntries([...layers,refreshed]);
const selection=entry(effective,'agent-default-model')?.config;
if(selection?.provider==='deepseek-official'){
const official=entry(effective,'llm-deepseek');
if(official?.name!=='@deepseek-ai/dsh-llm-deepseek-api-key')return result('invalid_configuration');
const ref=official.config?.apiKeyEnv??'DEEPSEEK_API_KEY';
if(typeof ref!=='string'||!/^[A-Za-z_][A-Za-z0-9_]*$/.test(ref))return result('invalid_configuration');
const key=refs[ref]??process.env[ref];
return typeof key==='string'&&key.trim()?ready('native',refs[ref]===undefined?ref:undefined):result('missing_credential');
}
const adapter=entry(effective,'llm-pi-ai');
if(adapter&&adapter.name!=='@deepseek-ai/dsh-llm-pi-ai')return result('invalid_configuration');
const profile=adapter?.config?.providers?.[selection?.provider];
if(!profile)return result('missing_configuration');
if(profile.apiKeyEnv==='DSH_API_KEY'){
const current=application.find(row=>row.id==='llm-pi-ai')?.config.providers.dx;
if(storedHostKey&&(!boundDx||!isDeepStrictEqual(profile,boundDx)))return result('invalid_configuration');
if(!storedHostKey&&!isDeepStrictEqual(profile,current))return result('invalid_configuration');
}
if(!selection.model||profile.models&&!profile.models.some(model=>model.id===selection.model))
return result('invalid_configuration');
if(profile.apiKeyEnv!==undefined){
if(typeof profile.apiKeyEnv!=='string'||!/^[A-Za-z_][A-Za-z0-9_]*$/.test(profile.apiKeyEnv))
function inspect(selection){
if(selection?.provider==='deepseek-official'){
const official=entry(effective,'llm-deepseek');
if(official?.name!=='@deepseek-ai/dsh-llm-deepseek-api-key')return result('invalid_configuration');
const ref=official.config?.apiKeyEnv??'DEEPSEEK_API_KEY';
if(typeof ref!=='string'||!/^[A-Za-z_][A-Za-z0-9_]*$/.test(ref))return result('invalid_configuration');
const key=refs[ref]??process.env[ref];
return typeof key==='string'&&key.trim()?ready('native',refs[ref]===undefined?ref:undefined,selection):result('missing_credential');
}
const adapter=entry(effective,'llm-pi-ai');
if(adapter&&adapter.name!=='@deepseek-ai/dsh-llm-pi-ai')return result('invalid_configuration');
const profile=adapter?.config?.providers?.[selection?.provider];
if(!profile)return result('missing_configuration');
if(profile.apiKeyEnv==='DSH_API_KEY'){
const current=application.find(row=>row.id==='llm-pi-ai')?.config.providers.dx;
if(storedHostKey&&(!boundDx||!isDeepStrictEqual(profile,boundDx)))return result('invalid_configuration');
if(!storedHostKey&&!isDeepStrictEqual(profile,current))return result('invalid_configuration');
}
if(!selection.model||profile.models&&!profile.models.some(model=>model.id===selection.model))
return result('invalid_configuration');
const key=refs[profile.apiKeyEnv]??process.env[profile.apiKeyEnv];
const source=selection.provider==='dx'&&!storedHostKey&&profile.apiKeyEnv==='DSH_API_KEY'?'host':'native';
return typeof key==='string'&&key.trim()
?ready(source,refs[profile.apiKeyEnv]===undefined?profile.apiKeyEnv:undefined):result('missing_credential');
if(profile.apiKeyEnv!==undefined){
if(typeof profile.apiKeyEnv!=='string'||!/^[A-Za-z_][A-Za-z0-9_]*$/.test(profile.apiKeyEnv))
return result('invalid_configuration');
const key=refs[profile.apiKeyEnv]??process.env[profile.apiKeyEnv];
const source=selection.provider==='dx'&&!storedHostKey&&profile.apiKeyEnv==='DSH_API_KEY'?'host':'native';
return typeof key==='string'&&key.trim()
?ready(source,refs[profile.apiKeyEnv]===undefined?profile.apiKeyEnv:undefined,selection):result('missing_credential');
}
// Native routes may explicitly omit credentials for local/public endpoints.
return ready('native',undefined,selection);
}
// Native routes may explicitly omit credentials for local/public endpoints.
return ready('native');
const primary=inspect(selection);
if(primary.available||rows.some(row=>row.id==='agent-default-model'))return primary;
// Native provider settings do not change the bundled official default.
// Choose a saved, credentialed route and pin it on automatic sessions too.
const configured=rows.find(row=>row.id==='llm-pi-ai')?.config?.providers??{};
for(const [provider,profile] of Object.entries(configured)){
for(const model of profile.models??[]){
const candidate=inspect({provider,model:model.id});
if(candidate.available)return candidate;
}
}
return primary;
}catch{return result('invalid_configuration');}
}
+17
View File
@@ -148,6 +148,12 @@ export async function apply(ctx){
if(request.model_budget_ms!==undefined&&(!Number.isFinite(request.model_budget_ms)||request.model_budget_ms<=0||request.model_budget_ms>300000)){
emit({status:'failed',error:'invalid_incident_request'});return;
}
const selection=request.model_selection;
if(selection!==undefined&&(!selection||typeof selection!=='object'||Array.isArray(selection)||
Object.keys(selection).some(key=>!['provider','model','reasoningEffort'].includes(key))||
['provider','model',...selection.reasoningEffort===undefined?[]:['reasoningEffort']].some(key=>typeof selection[key]!=='string'||!selection[key].length||selection[key].length>256||/[\x00-\x1f]/.test(selection[key])))){
emit({status:'failed',error:'invalid_incident_request'});return;
}
let cancel;
const cancelled=new Promise(resolve=>{cancel=resolve;});
const active={incidentId:request.incident_id,sessionId:null,cancelled:false,stopReason:null,cancel};
@@ -160,6 +166,17 @@ export async function apply(ctx){
active.sessionId=sessionId;
incidentSessions.set(request.incident_id,sessionId);
checkCancelled();
if(selection){
const agent=ctx.agents.get(sessionId);
// The public selectModel API also changes the user's global default.
await ctx.agents.serializeImageAdmission(agent,async()=>{
const resolved=await ctx.llm.resolveCallConfig(selection);
checkCancelled();
ctx.agents.selectForNextRequest(agent,{provider:resolved.provider,model:resolved.model,
...resolved.reasoningEffort===undefined?{}:{reasoningEffort:resolved.reasoningEffort}});
});
checkCancelled();
}
await ctx.sessionController.rename({sessionId,title:('DSH自动诊断 · '+(request.snapshot.task_name||request.snapshot.issue_code)).slice(0,128)});
}
active.sessionId=sessionId;
+14 -2
View File
@@ -50,7 +50,8 @@ def check_availability(data_root: Path) -> dict:
except Exception:
return _result('runtime_unavailable')
if not native:
return _result('available') | {'source': 'host', 'environment_refs': ['DSH_API_KEY']}
return _result('available') | {'source': 'host', 'environment_refs': ['DSH_API_KEY'],
'model_selection': {'provider': 'dx', 'model': env['DSH_MODEL']}}
env.update(DSH_HOME=str(home), DX_DSH_HOME=str(home), DSH_TELEMETRY_DISABLED='1')
try:
completed = subprocess.run([node, str(root / 'model_availability.mjs'), str(home)],
@@ -65,7 +66,18 @@ def check_availability(data_root: Path) -> dict:
if source not in {'native', 'host'} or not isinstance(refs, list) or len(refs) > 1 or any(
not isinstance(ref, str) or not re.fullmatch(r'[A-Za-z_][A-Za-z0-9_]*', ref) for ref in refs):
return _result('runtime_unavailable')
return _result(code) | {'source': source, 'environment_refs': refs}
checked = _result(code) | {'source': source, 'environment_refs': refs}
selection = value.get('model_selection')
if selection is not None:
if not isinstance(selection, dict):
return _result('invalid_configuration')
fields = ('provider', 'model', 'reasoningEffort') if 'reasoningEffort' in selection else ('provider', 'model')
if (not {'provider', 'model'} <= selection.keys()
or any(not isinstance(selection[key], str) or not 1 <= len(selection[key]) <= 256
or any(ord(char) < 32 for char in selection[key]) for key in fields)):
return _result('invalid_configuration')
checked['model_selection'] = {key: selection[key] for key in fields}
return checked
return _result(code)
except Exception:
# Parser/OS errors can include paths or text containing credentials.
@@ -155,6 +155,7 @@ class DshAutomation(QObject):
self._uses_cad_controls = False
self._admitted_action = None
self._observed_run_id = w._dsh_run_id
self._analysis_model_selection = deepcopy(w._dsh_status.availability.get('model_selection')) if w._dsh_status else None
self.cancel_event = Event()
# Start the shared model budget when the Harness session is ready;
# startup/session creation must not consume diagnosis time.
@@ -364,6 +365,9 @@ class DshAutomation(QObject):
if view is None or not view.is_running:
self._diagnosis_event({'request_id': request['request_id'], 'status': 'failed', 'error': 'native_harness_unavailable'})
else:
selection = getattr(self, '_analysis_model_selection', None)
if selection is not None:
request = dict(request, model_selection=selection)
view.request_diagnosis(request)
def _runtime_stopped(self):
+33 -3
View File
@@ -90,16 +90,18 @@ def test_dpapi_host_configuration_is_available_without_writes(tmp_path):
assert {path.name: path.read_bytes() for path in directory.iterdir()} == before
def test_real_native_route_survives_corrupt_unused_host_metadata(tmp_path):
@pytest.mark.parametrize('explicit_default', [True, False])
def test_real_native_route_survives_corrupt_unused_host_metadata(tmp_path, explicit_default):
directory = tmp_path / 'dsh'
patch = directory / 'harness-home/profiles/dx-task/cordis.patch.yml'
patch.parent.mkdir(parents=True)
(directory / 'provider.json').write_text('{invalid-unused-host', encoding='utf8')
patch.write_text(json.dumps([
rows = [
{'id': 'llm-pi-ai', 'config': {'providers': {'native': {'api': 'openai-completions',
'baseURL': 'https://example.invalid/v1', 'apiKeyEnv': 'FIXTURE_NATIVE_KEY',
'models': [{'id': 'fixture-model'}]}}}},
{'id': 'agent-default-model', 'config': {'provider': 'native', 'model': 'fixture-model'}}]), encoding='utf8')
{'id': 'agent-default-model', 'config': {'provider': 'native', 'model': 'fixture-model'}}]
patch.write_text(json.dumps(rows if explicit_default else rows[:1]), encoding='utf8')
credentials = directory / 'harness-home/.credentials.yaml'
credentials.write_text(json.dumps({'version': 1, 'refs': {'FIXTURE_NATIVE_KEY': 'fake-native-key'}}), encoding='utf8')
before = {path: path.read_bytes() for path in directory.rglob('*') if path.is_file()}
@@ -107,6 +109,7 @@ def test_real_native_route_survives_corrupt_unused_host_metadata(tmp_path):
assert result['available'] is True, result
assert result['source'] == 'native'
assert result['environment_refs'] == []
assert result['model_selection'] == {'provider': 'native', 'model': 'fixture-model'}
assert 'fake-native-key' not in json.dumps(result)
assert {path: path.read_bytes() for path in directory.rglob('*') if path.is_file()} == before
@@ -131,6 +134,33 @@ def test_native_extra_child_fields_are_discarded(tmp_path, monkeypatch):
assert 'fake-do-not-return' not in json.dumps(availability.check_availability(tmp_path))
@pytest.mark.parametrize('selection', [None, [], 7, {'provider': 'p'},
{'provider': 'p', 'model': 'm', 'reasoningEffort': None}, {'provider': 'p', 'model': '\n'}])
def test_invalid_native_model_route_is_rejected(tmp_path, monkeypatch, selection):
credentials = tmp_path / 'dsh/harness-home/.credentials.yaml'
credentials.parent.mkdir(parents=True)
credentials.write_text('{}', encoding='utf8')
output = {'available': True, 'code': 'available', 'source': 'native',
'environment_refs': [], 'model_selection': selection}
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k:
SimpleNamespace(returncode=0, stdout=json.dumps(output), stderr=''))
result = availability.check_availability(tmp_path)
assert result['available'] is (selection is None)
def test_native_reasoning_selection_is_preserved_without_extra_fields(tmp_path, monkeypatch):
credentials = tmp_path / 'dsh/harness-home/.credentials.yaml'
credentials.parent.mkdir(parents=True)
credentials.write_text('{}', encoding='utf8')
output = {'available': True, 'code': 'available', 'source': 'native', 'environment_refs': [],
'model_selection': {'provider': 'p', 'model': 'm', 'reasoningEffort': 'high', 'secret': 'do-not-return'}}
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k:
SimpleNamespace(returncode=0, stdout=json.dumps(output), stderr=''))
result = availability.check_availability(tmp_path)
assert result['model_selection'] == {'provider': 'p', 'model': 'm', 'reasoningEffort': 'high'}
assert 'do-not-return' not in json.dumps(result)
@pytest.mark.parametrize('output', ['secret not json', '{"available":true,"code":"secret"}'])
def test_native_invalid_output_is_fixed_safe_message(tmp_path, monkeypatch, output):
patch = tmp_path / 'dsh/harness-home/profiles/dx-task/cordis.patch.yml'
+11
View File
@@ -145,6 +145,17 @@ def test_model_budget_starts_when_harness_session_is_ready(slow_diagnosis, monke
assert automation._analysis_deadline == 100.0 + automation._diagnosis_budget_seconds()
def test_diagnosis_pins_the_same_route_as_readiness(slow_diagnosis):
window, automation, _, _ = slow_diagnosis
window._open_dsh()
selection = {'provider': 'configured', 'model': 'saved-model'}
automation._analysis_model_selection = selection
request = {'request_id': 'controlled-model-route'}
automation._request_model(request)
assert window._dsh_view.requests[-1]['model_selection'] == selection
assert 'model_selection' not in request
def test_cold_session_wait_does_not_consume_shared_model_budget(slow_diagnosis, monkeypatch, ui):
window, automation, _, _ = slow_diagnosis
window._open_dsh()
+14 -3
View File
@@ -7,7 +7,7 @@ import {randomUUID,createHash} from 'node:crypto';
const deferred=()=>{let resolve;const promise=new Promise(yes=>{resolve=yes;});return {promise,resolve};};
const tick=()=>new Promise(resolve=>setImmediate(resolve));
const events=[],handlers=new Map(),routes=new Map(),sessions=new Map(),prompts=[],cancelled=[];
const events=[],handlers=new Map(),routes=new Map(),sessions=new Map(),prompts=[],cancelled=[],selected=[];
let nextCreate=null,nextPrompt=null,latestTimeout,now=1000000;
const timeouts=[];
const context={task_id:'',revision:'current'};
@@ -16,6 +16,7 @@ const ctx={on:(name,fn)=>handlers.set(name,fn),effect:()=>{},
webServer:{register:route=>routes.set(route.path,route.handler)},
workspaceRegistry:{create:async()=>({id:'workspace'})},
sessionController:{
selectModel:async()=>{assert.fail('Automatic diagnosis must not save the global default');},
create:async()=>{
if(nextCreate){const wait=nextCreate;nextCreate=null;await wait.promise;}
const sessionId=randomUUID();
@@ -34,7 +35,12 @@ const ctx={on:(name,fn)=>handlers.set(name,fn),effect:()=>{},
sessions.get(sessionId).pending.push('diagnosis');
sessions.get(sessionId).wakeRequested=true;
}}
},agents:{get:id=>sessions.get(id)}};
},llm:{resolveCallConfig:async selection=>selection},agents:{get:id=>sessions.get(id),
serializeImageAdmission:async(_agent,action)=>action(),
selectForNextRequest:(agent,selection)=>{
const sessionId=[...sessions].find(([_id,item])=>item===agent)[0];
assert.ok(!prompts.includes(sessionId));selected.push({sessionId,...selection});
}}};
let source=await readFile(new URL('../integration/dsh/task-plugin/host.mjs',import.meta.url),'utf8');
source=source.replace(/^import .*;\r?\n/gm,'').replace(/export /g,'');
const sandbox={path,randomUUID,createHash,Date:class extends Date{static now(){return now;}},
@@ -53,10 +59,15 @@ const start=(id,options={})=>handlers.get('dx/diagnose-request')({request_id:id,
const waitFor=async predicate=>{for(let i=0;i<100&&!predicate();i++)await tick();assert.ok(predicate(),'expected lifecycle event');};
const terminal=id=>events.find(event=>event.request_id===id&&['completed','cancelled','failed'].includes(event.status));
start('cancel-active');start('keep-active');
start('cancel-active');start('keep-active',{model_selection:{provider:'configured',model:'saved-model',reasoningEffort:'high'}});
await waitFor(()=>prompts.length===2);
const first=events.find(event=>event.request_id==='cancel-active'&&event.status==='started').session_id;
const other=events.find(event=>event.request_id==='keep-active'&&event.status==='started').session_id;
assert.equal(selected.length,1,'automatic session must use the same configured model as readiness');
assert.equal(selected[0].sessionId,other);
assert.equal(selected[0].provider,'configured');
assert.equal(selected[0].model,'saved-model');
assert.equal(selected[0].reasoningEffort,'high');
handlers.get('dx/diagnose-cancel')({request_id:'unknown'});
assert.deepEqual(cancelled,[]);
handlers.get('dx/diagnose-cancel')({request_id:'cancel-active'});
+6
View File
@@ -55,6 +55,12 @@ try{
{version:1,refs:{FIXTURE_NATIVE_KEY:'fake-custom-official-key'}},'available',[]);
await verify(rows(route),{},'missing_credential');
await verify(rows(route),{version:1,refs:{FIXTURE_NATIVE_KEY:'fake-offline-key'}},'available',[]);
// Native settings save a provider without editing agent-default-model.
await verify(rows(route).slice(0,1),{version:1,refs:{FIXTURE_NATIVE_KEY:'fake-offline-key'}},'available',[]);
assert.deepEqual((await checkModelAvailability(home)).model_selection,{provider:'custom',model:'fixture-model'});
await verify(rows(route).slice(0,1),{},'missing_credential');
await verify([{id:'llm-pi-ai',config:{providers:{custom:route,other:{...route,apiKeyEnv:undefined}}}},
rows(route)[1]],{},'missing_credential');
await verify(rows(route),{version:1,refs:{FIXTURE_NATIVE_KEY:'fake-offline-key'},records:[]},'invalid_configuration');
await verify(rows(route),{FIXTURE_NATIVE_KEY:'fake-flat-offline-key'},'available',[]);
await verify(rows(route),'%YAML 1.2\n---\nFIXTURE_NATIVE_KEY: fake-flat-offline-key\n','invalid_configuration');