diff --git a/tests/approved_device_pin_identity_contract.test.cjs b/tests/approved_device_pin_identity_contract.test.cjs index 986b93824..3361d2ffc 100644 --- a/tests/approved_device_pin_identity_contract.test.cjs +++ b/tests/approved_device_pin_identity_contract.test.cjs @@ -68,6 +68,75 @@ test("MV-8 same-device Apply remembers explicit physical pin sets, not callback "DeviceVM cannot edit authoritative JUCE inventory"); }); +test("G-MV21 Device Setup first ACK wins even if native sends contradictory duplicates",()=>{ + const f=fixture(),begun=f.begin("ack-once"); + assert.ok(begun); + const accepted=f.ack("ack-once"),verifying=vm.setupAck(begun.state,accepted); + assert.equal(verifying.phase,"VERIFYING"); + const original=JSON.stringify(verifying); + for(const duplicate of [accepted, + {...accepted,ok:false,state:"REJECTED",error:"Late failed driver"}, + {...accepted,active:{...accepted.active,playbackDevice:"Wrong ASIO"}}, + {...accepted,active:{...accepted.active,enabledOutputs:[0,1]}}, + {...accepted,active:{...accepted.active,deviceGeneration:999}}]){ + assert.strictEqual(vm.setupAck(verifying,duplicate),verifying); + assert.equal(JSON.stringify(verifying),original); + } + assert.equal(vm.setupReadback(verifying,f.readback).phase,"ACCEPTED"); + const expired=vm.setupTimeout(verifying,"ack-once"); + assert.equal(expired.phase,"TIMEOUT"); + assert.strictEqual(vm.setupAck(expired,accepted),expired); +}); + +test("G-MV21 only verified device readback refreshes Audio I/O inventory",()=>{ + const bridge=readFileSync("web/approved-native-bridge.js","utf8"); + const a=bridge.indexOf('backend.addEventListener("arMixAudioDeviceApplyAck",ack=>'); + const b=bridge.indexOf(" function installFirstSoundMenu()",a); + assert.ok(a>0&&b>a); + const ack=bridge.slice(a,b); + assert.match(ack,/deviceVM\.setupAck\(deviceState,ack\)/); + assert.match(ack,/if\(deviceBusy\(\)\)[\s\S]*?else\{[\s\S]*?finishDeviceApplyUi\(\);[\s\S]*?action:"audioDeviceInventory"/, + "even rejected/NEEDS_REMAP outcomes must refresh native inventory for recovery"); + assert.doesNotMatch(ack,/deviceInventory\s*=|inventory\.active\s*=|updateHardwarePanel/, + "unverified or rejected ACK cannot mutate native hardware inventory"); + const s=bridge.indexOf(" function onNativeReadback(next,confirmed) {"); + const e=bridge.indexOf(" if (connected) {\n controller = protocol.createController",s); + assert.ok(s>0&&e>s); + const readback=bridge.slice(s,e); + assert.match(readback,/deviceVM\.setupReadback\(deviceState,next\)/); + assert.match(readback,/finishDeviceApplyUi\(next\)/); + assert.match(readback,/deviceState\.phase==="ACCEPTED"&&connected[\s\S]*?action:"audioDeviceInventory"/); + const finish=bridge.slice(bridge.indexOf(" function finishDeviceApplyUi("),a); + assert.match(finish,/verifiedSnapshot\?\.device/); + assert.match(finish,/active\?\.sampleRate/); + assert.doesNotMatch(finish,/deviceInventory\?\.active/); +}); +test("G-MV21 NEEDS_REMAP native inventory refresh restores reuse-pins recovery",()=>{ + const f=fixture(),begin=f.begin("needs-remap"); + const newActive={...f.active,deviceGeneration:42,enabledInputs:[0,1], + enabledOutputs:[0,1]}; + const state=vm.setupAck(begin.state,{requestId:"needs-remap", + ok:false,state:"NEEDS_REMAP",error:"NEEDS_REMAP: Confirm physical pins", + canReusePins:true,active:newActive}); + assert.equal(state.phase,"REJECTED"); + assert.equal(state.remapGeneration,42); + assert.equal(vm.setupPresentation(f.inventory,f.draft,state).canReusePins,false, + "stale old endpoint generation cannot authorize a remap"); + const refreshed={...f.inventory,active:newActive}; + assert.equal(vm.setupPresentation(refreshed,f.draft,state).canReusePins,true, + "fresh native inventory unlocks explicit recovery, not ack.active"); + const retry=vm.setupBegin(state,{inventory:refreshed,draft:f.draft, + requestId:"needs-remap-retry",baselineEpoch:"42", + confirmRetainedPinMapping:true}); + assert.ok(retry,"recovery request must use fresh endpoint generation"); + assert.equal(retry.request.expectedDeviceGeneration,42); + assert.equal(retry.request.config.confirmRetainedPinMapping,true); + assert.equal(retry.request.config.sampleRate,newActive.rate); + assert.equal(retry.request.config.bufferSize,newActive.buffer); + assert.deepEqual(f.inventory.active.enabledOutputs,[0,1], + "negative Apply ACK cannot mutate old installed inventory"); +}); + test("MV-8 native accepted ACK with changed output pins never claims selected hardware",()=>{ const f=fixture(),begun=f.begin("pin-2"); const acceptedDifferent=f.ack("pin-2",{active:{ diff --git a/tests/approved_native_m2_contract.test.cjs b/tests/approved_native_m2_contract.test.cjs index ce85b2011..29ef4dec3 100644 --- a/tests/approved_native_m2_contract.test.cjs +++ b/tests/approved_native_m2_contract.test.cjs @@ -2125,6 +2125,36 @@ test("SET_FADER: one exact string request, ACK then LATER same-revision native r assert.equal(c.state().pending,null); assert.equal(visible.length,2); }); +test("G-MV21 controller ACK lease is immutable until installed v6 readback",()=>{ + const snap=acceptedFixture(),events=[],paints=[]; + const c=protocol.createController({emit:(name,payload)=>events.push({name,payload}), + onAccepted:(next,confirmed)=>paints.push({next,confirmed})}); + assert.equal(c.onSnapshot(snap),true); + const issued=c.intent("SET_FADER","9007199254740993",{valueDb:-12}); + assert.equal(issued.ok,true); + const ack={requestId:issued.requestId,status:"ACCEPTED",errorCode:"NONE", + deviceEpoch:snap.device.epoch,graphRevision:"6",rosterRevision:"7"}; + assert.equal(c.onAck(ack),true); + const n=events.length; + for(const late of [ack, + {...ack,status:"REJECTED",errorCode:"STALE_GRAPH"}, + {...ack,graphRevision:"99"},{...ack,graphRevision:"broken"}, + {...ack,deviceEpoch:"18446744073709551609"}]){ + assert.equal(c.onAck(late),false); + assert.equal(c.state().pending?.requestId,issued.requestId); + assert.equal(c.state().lastError,"PENDING_ACCEPTED_SNAPSHOT"); + assert.equal(events.length,n,"duplicate ACK cannot resubscribe"); + } + assert.equal(paints.length,1,"ACK alone cannot repaint"); + const installed=clone(snap); + installed.graph.revision="6"; + installed.graph.channels[0].controls.faderDb=-12; + assert.equal(c.onSnapshot(installed),true); + assert.equal(c.state().pending,null); + assert.equal(paints.length,2); + assert.equal(paints[1].confirmed.requestId,issued.requestId); + assert.equal(c.onAck(ack),false); +}); test("STALE/REJECTED/timeout leave accepted GUI untouched and request fresh native readback",()=>{ const sent=[],c=protocol.createController({emit:(name,payload)=>sent.push({name,payload})}); const s=acceptedFixture();c.onSnapshot(s); diff --git a/web/approved-device-view-model.js b/web/approved-device-view-model.js index 615dbf2af..9478887a4 100644 --- a/web/approved-device-view-model.js +++ b/web/approved-device-view-model.js @@ -258,7 +258,11 @@ remapGeneration:null}}; } function setupAck(state,ack) { - if(!state.pending||ack?.requestId!==state.pending.requestId)return state; + // G-MV21: first ACK wins while APPLYING. After a matching hardware + // acceptance, the immutable ACK lease is verified only by an installed + // v6 readback, never replaced by duplicate positive/negative ACKs. + if(state.phase!=="APPLYING"||!state.pending|| + state.pending.ack||ack?.requestId!==state.pending.requestId)return state; const active=ack.active; const accepted=ack.ok===true && (ack.state==="ACCEPTED_LIVE"||ack.state==="ACCEPTED_MUTED") && diff --git a/web/approved-native-bridge.js b/web/approved-native-bridge.js index 77d238377..8998a82de 100644 --- a/web/approved-native-bridge.js +++ b/web/approved-native-bridge.js @@ -2281,7 +2281,7 @@ backend.emitEvent("arMixAction",{action:"audioDeviceInventory"}); },12000); } - function finishDeviceApplyUi(){ + function finishDeviceApplyUi(verifiedSnapshot=null){ window.clearTimeout(deviceApplyTimer); deviceApplyTimer=0; if(!devicePanel)return; @@ -2289,9 +2289,12 @@ if(deviceState.phase==="ACCEPTED"){ deviceChoice=null; devicePanel.hidden=true; - const active=deviceInventory?.active; + // The confirmed schema-v6 readback, not the preceding Apply ACK, + // is the only source for the device-ready status shown to operators. + const active=verifiedSnapshot?.device; status("DEVICE READY · "+String(active?.backend||"Audio")+ - " · "+String(active?.rate||0)+" Hz / "+String(active?.buffer||0)+" samples"); + " · "+String(active?.sampleRate||0)+" Hz / "+ + String(active?.bufferFrames||0)+" samples"); nativeToast(deviceState.message); }else{ deviceChoice=null; @@ -2313,12 +2316,19 @@ const next=deviceVM.setupAck(deviceState,ack); if(next===deviceState)return; // delayed or unrelated ACK deviceState=next; - if(ack?.active && deviceInventory) - deviceInventory={...deviceInventory,active:ack.active}; + // ACK.active is unverified hardware evidence. Do not allow either + // accepted or rejected ACKs to overwrite the installed I/O inventory. if(deviceBusy()){ // ACK alone NEVER closes the dialog or announces LIVE. controller?.subscribe(); - }else finishDeviceApplyUi(); + }else{ + finishDeviceApplyUi(); + // G-MV21: a negative ACK can still leave a NEW native endpoint in + // NEEDS_REMAP. Only a freshly queried native inventory may unblock + // retained-pin recovery or supply the next device generation. + // Never commit ack.active as installed hardware. + backend.emitEvent("arMixAction",{action:"audioDeviceInventory"}); + } }); } @@ -2715,7 +2725,14 @@ scope?.syncNativeWireGains?.([]); // flush accepted paint after pointer release const previousSetup=deviceState; deviceState=deviceVM.setupReadback(deviceState,next); - if(deviceState!==previousSetup && !deviceState.pending)finishDeviceApplyUi(); + if(deviceState!==previousSetup && !deviceState.pending){ + finishDeviceApplyUi(next); + // G-MV21: only after installed device verification may the UI + // request a fresh authoritative native inventory. A late ACK must + // neither poison the operator's pin choices nor close Audio I/O. + if(deviceState.phase==="ACCEPTED"&&connected) + backend.emitEvent("arMixAction",{action:"audioDeviceInventory"}); + } updateHardwarePanel(next); // The MV-18 external-output repaint must stay after the same fence. paintExternalReadback(next); diff --git a/web/approved-native-contract.js b/web/approved-native-contract.js index e5f1bbc5c..fa8553f3c 100644 --- a/web/approved-native-contract.js +++ b/web/approved-native-contract.js @@ -743,6 +743,11 @@ emit("arMixConsoleIntent",msg);notify();return {ok:true,requestId}; } function onAck(raw) { + // G-MV21: the first correlated native ACK owns this request's proof + // lease. A duplicate positive, negative or malformed ACK cannot + // overwrite its graph revision, demand another subscribe, or reject + // a command already waiting for independent installed PCM readback. + if (pending?.ack) return false; if (!pending || !fields(raw,["requestId","status","deviceEpoch","graphRevision","rosterRevision","errorCode"]) || typeof raw.requestId !== "string" || raw.requestId !== pending.requestId) return false; if ([raw.deviceEpoch,raw.graphRevision,raw.rosterRevision].some(x=>decimal(x)===null)) {