diff --git a/tests/approved_native_m2_contract.test.cjs b/tests/approved_native_m2_contract.test.cjs index 31e225bc1..bc019fad3 100644 --- a/tests/approved_native_m2_contract.test.cjs +++ b/tests/approved_native_m2_contract.test.cjs @@ -3433,3 +3433,58 @@ test("GUI-2 M4 rack pending selector rejects revisions and unaccepted authority" assert.ok(source.includes("fxRackVM.m4RackSettlement(")); assert.doesNotMatch(source,/const actual=p\.kind==="rack"\?/); }); + +test("MV-7 deferred SEND gain requires unique exact installed EdgeId",()=>{ + const source="9007199254740993",edgeId="9007199254740997"; + const snap={admitted:true,retained:false,identity:"A|17",graph:{edges:[ + {enabled:true,primaryOutput:false,from:source,edgeId,to:"2",gainLinear:0.5}]}}; + const edit={identity:snap.identity,source,gain:0.75}; + const before=JSON.stringify(snap); + assert.deepEqual(wireVM.gainDispatchPlan(snap,edgeId,edit), + {kind:"DISPATCH",source,edgeId,gainLinear:0.75}); + assert.equal(wireVM.gainDispatchPlan(snap,edgeId,{...edit,gain:0.5}).kind,"CONFIRMED"); + const fail=(s=snap,e=edgeId,d=edit)=>assert.equal( + wireVM.gainDispatchPlan(s,e,d).kind,"ROLLBACK"); + fail({...snap,admitted:false});fail({...snap,retained:true}); + fail({...snap,identity:"B|17"});fail(snap,edgeId,{...edit,identity:"B|17"}); + fail(snap,Number(edgeId));fail(snap,"0");fail(snap,edgeId,{...edit,source:Number(source)}); + for(const g of [NaN,Infinity,-1,4.01])fail(snap,edgeId,{...edit,gain:g}); + fail({...snap,graph:{edges:[...snap.graph.edges,...snap.graph.edges]}}); + fail({...snap,graph:{edges:[{...snap.graph.edges[0],primaryOutput:true}]}}); + fail({...snap,graph:{edges:[{...snap.graph.edges[0],gainLinear:NaN}]}}); + assert.equal(JSON.stringify(snap),before); +}); +test("MV-7 WIRE status only retries one same-epoch initial CONNECT",()=>{ + const state={pending:null,lastError:"STALE_NATIVE_WIRE_GRAPH",current:{device:{epoch:"71"}}}; + const attempt={epoch:"71",retries:0,ops:[{type:"CONNECT",sourceId:1,destId:11}]}; + const plan=(s=state,a=attempt,start=null)=> + wireVM.nativeRecoveryPlan(s,a,start).kind; + assert.equal(plan(),"REBASE_CONNECTION"); + assert.equal(plan({...state,lastError:"STALE_NATIVE_CONTROL_REVISION"}),"REBASE_CONNECTION"); + assert.equal(plan({...state,pending:{requestId:"r"}}),"PENDING"); + assert.equal(plan({...state,lastError:"ACCEPTED_READBACK"}),"ACCEPTED"); + assert.equal(plan({...state,lastError:"NONE"}),"IDLE"); + assert.equal(plan({...state,lastError:"NATIVE_NOT_SUBSCRIBED"}),"IDLE"); + for(const err of ["INVALID_NATIVE_ACK","UNKNOWN_CHANNEL_ID", + "NATIVE_ACK_OR_READBACK_TIMEOUT","ACK_READBACK_EFFECT_MISMATCH"]) + assert.equal(plan({...state,lastError:err}),"ROLLBACK"); + assert.equal(plan({...state,current:{device:{epoch:"72"}}}),"ROLLBACK"); + assert.equal(plan(state,{...attempt,epoch:"72"}),"ROLLBACK"); + assert.equal(plan(state,{...attempt,epoch:71}),"ROLLBACK"); + assert.equal(plan(state,{...attempt,retries:1}),"ROLLBACK"); + assert.equal(plan(state,{...attempt,ops:[{type:"REMOVE"}]}),"ROLLBACK"); + assert.equal(plan(state,{...attempt,ops:[{type:"RETARGET"}]}),"ROLLBACK"); + assert.equal(plan(state,{...attempt,ops:[{type:"CONNECT"},{type:"CONNECT"}]}),"ROLLBACK"); + assert.equal(plan(state,null,{retries:1}),"FLUSH_STARTUP_RETRY"); + assert.equal(plan({...state,lastError:"INVALID_NATIVE_ACK"},null,{retries:1}),"ROLLBACK"); +}); +test("MV-7 bridge delegates WIRE decisions, preserves only one native controller",()=>{ + const bridge=read("web/approved-native-bridge.js"); + assert.match(bridge,/wireVM\.gainDispatchPlan\(/); + assert.match(bridge,/wireVM\.nativeRecoveryPlan\(/); + assert.match(bridge,/controller\.intent\("SET_WIRE_GAIN",plan\.source/); + assert.match(bridge,/controller = protocol\.createController\(/); + assert.doesNotMatch(bridge,/const failed=!state\.pending/); + assert.doesNotMatch(read("web/approved-wire-view-model.js"), + /backend\.emitEvent|window\.setTimeout|document\.querySelector/); +}); diff --git a/web/approved-native-bridge.js b/web/approved-native-bridge.js index 60a3b284a..87d874dd9 100644 --- a/web/approved-native-bridge.js +++ b/web/approved-native-bridge.js @@ -369,24 +369,18 @@ if(state?.pending||!current?.admitted)return; for(const [edgeId,edit] of pendingWireGains){ pendingWireGains.delete(edgeId); - if(current.identity!==edit.identity){ - // A changed device/graph invalidates a deferred edit, but must not - // strand its LCD while waiting for a nonexistent ACK. - scope?.rollbackNativeWireInspector?.(nativeWireGainReadback(current)); - continue; // never replay into a new graph/device - } - const edge=current.graph.edges.find(e=>e.enabled&&e.edgeId===edgeId&&e.from===edit.source); - if(!edge||edge.primaryOutput!==false){ + const plan=wireVM.gainDispatchPlan(current,edgeId,edit); + if(plan.kind==="ROLLBACK"){ + // Stale identity, malformed gain, or duplicate EdgeId: fail closed. scope?.rollbackNativeWireInspector?.(nativeWireGainReadback(current)); continue; } - if(Math.abs(edge.gainLinear-edit.gain)<.00001){ - // The accepted PCM gain already equals the requested scalar. Do not - // emit a redundant native intent, but DO settle the pending display. + if(plan.kind==="CONFIRMED"){ scope?.syncNativeWireGains?.(nativeWireGainReadback(current)); continue; } - const result=controller.intent("SET_WIRE_GAIN",edit.source,{edgeId,gainLinear:edit.gain}); + const result=controller.intent("SET_WIRE_GAIN",plan.source,{ + edgeId:plan.edgeId,gainLinear:plan.gainLinear}); if(result.ok)window.setTimeout(()=>controller?.expire(result.requestId),8000); else status("WIRE gain rejected ยท "+result.error); return; // next coalesced target follows ACK + independent readback @@ -2901,27 +2895,23 @@ pendingNativeControl.size)queueLiveControlFlush(); if(!state.pending&&state.lastError==="ACCEPTED_READBACK"&&pendingWireGains.size) queueWireGainFlush(); - // Rejection cleanup and native rollback must BOTH run. - // Former else-if was unreachable after the cleanup branch matched. - const failed=!state.pending && - state.lastError!=="ACCEPTED_READBACK" && - state.lastError!=="NONE" && - state.lastError!=="NATIVE_NOT_SUBSCRIBED"; - if(!state.pending&&state.lastError==="ACCEPTED_READBACK"&&startupWire) + // Pure ViewModel decides retry/rollback; one native controller. + const recovery=wireVM.nativeRecoveryPlan( + state,activeWireAttempt,startupWire); + if(recovery.kind==="ACCEPTED"&&startupWire) window.queueMicrotask(flushStartupWire); - if(!state.pending&&state.lastError==="ACCEPTED_READBACK")activeWireAttempt=null; - if(failed&&startupWire?.retries===1&& - ["STALE_NATIVE_CONTROL_REVISION","STALE_NATIVE_WIRE_GRAPH"].includes(state.lastError)) { + if(recovery.kind==="ACCEPTED")activeWireAttempt=null; + if(recovery.kind==="FLUSH_STARTUP_RETRY"){ window.queueMicrotask(flushStartupWire);return; } - if(failed){ + if(recovery.kind==="REBASE_CONNECTION"){ const wire=activeWireAttempt;activeWireAttempt=null; - // One stale-revision rebase of a new connection only. A fresh - // independent snapshot must still prove device/endpoints. Never - // replay retarget/remove, a changed epoch, or a rejected cycle. - if(wire&&wire.retries===0&& - ["STALE_NATIVE_CONTROL_REVISION","STALE_NATIVE_WIRE_GRAPH"].includes(state.lastError)&& - state.current?.device?.epoch===wire.epoch&&deferStartupWire(wire.ops,1))return; + // Existing bridge rechecks exact admission before one retry. + if(deferStartupWire(wire.ops,1))return; + } + if(recovery.kind==="ROLLBACK"|| + recovery.kind==="REBASE_CONNECTION"){ + activeWireAttempt=null; startupWire=null; pendingWireGains.clear(); // The controller deliberately freezes current on a rejected ACK. diff --git a/web/approved-wire-view-model.js b/web/approved-wire-view-model.js index 0e55c1045..9cdc16de2 100644 --- a/web/approved-wire-view-model.js +++ b/web/approved-wire-view-model.js @@ -153,7 +153,44 @@ return {ok:true,source,changes}; } + + // MV-7: pure deferred WIRE gain projection. Installed PCM alone decides. + function gainDispatchPlan(snapshot,edgeId,edit) { + if(snapshot?.admitted!==true||snapshot.retained===true|| + !edit||snapshot.identity!==edit.identity|| + !nativeId(edgeId)||!nativeId(edit.source)|| + typeof edit.gain!=="number"||!Number.isFinite(edit.gain)|| + edit.gain<0||edit.gain>4)return {kind:"ROLLBACK"}; + const edge=parallelEdge(snapshot.graph,edit.source,edgeId); + if(!edge||!Number.isFinite(edge.gainLinear))return {kind:"ROLLBACK"}; + if(Math.abs(edge.gainLinear-edit.gain)<0.00001) + return {kind:"CONFIRMED"}; + return {kind:"DISPATCH",source:edit.source,edgeId, + gainLinear:edit.gain}; + } + + // MV-7: only a same-device initial CONNECT may rebase once. + // The original controller remains the sole native ACK/readback authority. + const STALE_WIRE_ERRORS=new Set([ + "STALE_NATIVE_CONTROL_REVISION","STALE_NATIVE_WIRE_GRAPH"]); + function nativeRecoveryPlan(state,activeAttempt,startupAttempt) { + if(state?.pending)return {kind:"PENDING"}; + const error=state?.lastError; + if(error==="ACCEPTED_READBACK")return {kind:"ACCEPTED"}; + if(error==="NONE"||error==="NATIVE_NOT_SUBSCRIBED"||!error) + return {kind:"IDLE"}; + if(startupAttempt?.retries===1&&STALE_WIRE_ERRORS.has(error)) + return {kind:"FLUSH_STARTUP_RETRY"}; + if(activeAttempt?.retries===0&&nativeId(activeAttempt.epoch)&& + state?.current?.device?.epoch===activeAttempt.epoch&& + STALE_WIRE_ERRORS.has(error)&& + Array.isArray(activeAttempt.ops)&&activeAttempt.ops.length===1&& + activeAttempt.ops[0]?.type==="CONNECT") + return {kind:"REBASE_CONNECTION"}; + return {kind:"ROLLBACK"}; + } + return Object.freeze({gainDb,edgePresentation,parallelEdge, acceptedParallelEdge,gainReadback,canEditTap,gestureTap,sofAvailable, - compactSendGesture,nativeTransaction}); + compactSendGesture,nativeTransaction,gainDispatchPlan,nativeRecoveryPlan}); });