diff --git a/tests/approved_device_pin_identity_contract.test.cjs b/tests/approved_device_pin_identity_contract.test.cjs index 3361d2ffc..bb7af4d71 100644 --- a/tests/approved_device_pin_identity_contract.test.cjs +++ b/tests/approved_device_pin_identity_contract.test.cjs @@ -342,10 +342,12 @@ test("MV-18 physical device swap cancels output and FX leases even at unchanged old),{kind:"M4_FIRST_SOUND",expirePending:false}, "first M4 input patch must preserve its own pending source"); const bridge=readFileSync("web/approved-native-bridge.js","utf8"); - const a=bridge.indexOf(" function expireTransientNativeUi(reason) {"); + const a=bridge.indexOf(" function discardTransientNativeUi({preserveRetainedIdentity=false}={}) {"); const b=bridge.indexOf(" function onNativeReadback(next,confirmed)",a); const cleanup=bridge.slice(a,b); assert.match(cleanup,/pendingExternal\.clear\(\);pendingSlotPowers\.clear\(\)/); + assert.match(cleanup,/function expireTransientNativeUi\(reason\)\s*\{\s*discardTransientNativeUi\(\)/, + "physical change must use the same complete GUI lease discard"); assert.doesNotMatch(cleanup,/backend\.emitEvent|audioDeviceApply|routingExternalOutput|routingFxBypass/, "UI cleanup cannot send native commands, reset FX, or reopen devices"); }); @@ -432,9 +434,80 @@ test("MV-19 invalid read-model and malformed device identity cannot mutate hardw "read-model M4 evidence must not use the prior hardware cache"); }); +test("G-MV24 readback policy quarantines retained PCM without starving first M4 input",()=>{ + const policy=vm.nativeTransientReadbackPolicy; + for(const [kind,expected] of [ + ["NEEDS_REMAP",{discardRetained:true,drainM4Sources:false, + drainNativeSources:false}], + ["CHANNEL_ID",{discardRetained:false,drainM4Sources:false, + drainNativeSources:true}], + ["M4",{discardRetained:false,drainM4Sources:true, + drainNativeSources:false}], + ["UNADMITTED",{discardRetained:false,drainM4Sources:true, + drainNativeSources:false}], + ["LOGICAL_ONLY",{discardRetained:false,drainM4Sources:false, + drainNativeSources:false}], + ["STALE",{discardRetained:false,drainM4Sources:false, + drainNativeSources:false}], + ["INVALID",{discardRetained:false,drainM4Sources:false, + drainNativeSources:false}], + [null,{discardRetained:false,drainM4Sources:false, + drainNativeSources:false}] + ]){ + const result=policy(kind); + assert.deepEqual(result,expected,String(kind)); + assert.equal(Object.isFrozen(result),true); + } + assert.doesNotMatch(policy.toString(),/document|window|backend|AudioContext|setTimeout/, + "readback quarantine decision belongs to the pure Device ViewModel"); + const bridge=readFileSync("web/approved-native-bridge.js","utf8"); + const start=bridge.indexOf(" function discardTransientNativeUi({preserveRetainedIdentity=false}={}) {"); + const expire=bridge.indexOf(" function expireTransientNativeUi(reason) {",start); + const readback=bridge.indexOf(" function onNativeReadback(next,confirmed)",expire); + assert.ok(start>0&&expire>start&&readback>expire); + const discard=bridge.slice(start,expire); + const expiration=bridge.slice(expire,readback); + const region=bridge.slice(readback, + bridge.indexOf(" if (connected) {\n controller = protocol.createController",readback)); + assert.match(expiration,/discardTransientNativeUi\(\)/, + "DEVICE_CHANGED and AUTHORITY_CHANGED reuse the sole GUI discard"); + assert.match(region,/if\s*\(transientPlan\.discardRetained\)\s*\{/); + const retained=region.slice(region.indexOf("if (transientPlan.discardRetained)"), + region.indexOf("if (!nativeAdmitted)",region.indexOf("if (transientPlan.discardRetained)"))); + assert.match(retained,/discardTransientNativeUi\(\{preserveRetainedIdentity:true\}\)/, + "NEEDS_REMAP must discard physical leases but retain legal cosmetic edits"); + assert.match(discard,/if\(pendingIdentity&&!preserveRetainedIdentity\)/, + "retained ChannelId name/color/icon edits must finish on native ACK/readback"); + assert.match(expiration,/discardTransientNativeUi\(\)/, + "real authority or physical device change still cancels cosmetic edit"); + assert.doesNotMatch(retained,/pendingNativeSources\.clear|pendingNativeFxPower=null|pendingWireGains\.clear|activeNativeSource=null/, + "there must be no second partial cleanup list to drift from reconnect"); + for(const token of ["pendingNativeSources.clear()","pendingSourceQueue.clear()", + "pendingM4RackPowers.clear()","pendingExternal.clear()", + "pendingSlotPowers.clear()","pendingGlobalFx=null", + "pendingNativeFxPower=null","pendingWireGains.clear()", + "pendingTrackDelete=null","pendingFxInsert=null","pendingIdentity"]){ + assert.ok(discard.includes(token),"retained cleanup must cover "+token); + } + assert.equal((bridge.match(/pendingSourceQueue\.clear\(\)/g)||[]).length,1, + "source queue has one complete cleanup owner, including reconnect and retention"); + assert.match(region,/transientPlan\.drainM4Sources\s*&&\s*!nativeUiSourcePending/); + assert.match(region,/transientPlan\.drainNativeSources\s*&&\s*!activeNativeSource/); + assert.ok(region.indexOf("if (transientPlan.drainM4Sources")< + region.indexOf("if (transientPlan.discardRetained)")); + assert.doesNotMatch(region.slice(region.indexOf("if (transientPlan.discardRetained)"), + region.indexOf("if (!nativeAdmitted)",region.indexOf("if (transientPlan.discardRetained)"))), + /window\.queueMicrotask|backend\.emitEvent|controller\?\.intent/, + "quarantined retained graph cannot create routing operations"); + assert.doesNotMatch(discard,/lastPaintedSignature=|lastNativeTopologySignature=|backend\.emitEvent|localStorage/, + "repeated retained readbacks must preserve paint signature and never change PCM"); + assert.match(expiration,/lastPaintedSignature="";lastNativeTopologySignature="";/, + "only a real authority/physical device change invalidates full GUI paint"); +}); + test("MV-12 approved bridge invalidates transient UI leases, not durable JUCE state",()=>{ const source=readFileSync("web/approved-native-bridge.js","utf8"); - const start=source.indexOf(" function expireTransientNativeUi(reason) {"); + const start=source.indexOf(" function discardTransientNativeUi({preserveRetainedIdentity=false}={}) {"); const end=source.indexOf(" function onNativeReadback(next,confirmed)",start); assert.ok(start>0&&end>start); const cleanup=source.slice(start,end); diff --git a/web/approved-device-view-model.js b/web/approved-device-view-model.js index 9478887a4..d2068c015 100644 --- a/web/approved-device-view-model.js +++ b/web/approved-device-view-model.js @@ -388,6 +388,16 @@ captureDevice:d.captureDevice,playbackDevice:d.playbackDevice, hardwareConfiguration,authority:readModelKind}); } + // G-MV24: pure readback scheduling/quarantine policy. The accepted native + // read-model is the only authority; old SOURCE timers and microtasks may + // never be scheduled from a retained or stale PCM snapshot. + function nativeTransientReadbackPolicy(kind) { + return Object.freeze({ + discardRetained:kind==="NEEDS_REMAP", + drainM4Sources:kind==="M4"||kind==="UNADMITTED", + drainNativeSources:kind==="CHANNEL_ID" + }); + } function nativeLifecycleDecision(previous,next) { if(!next || typeof next.epoch!=="string" || typeof next.authority!=="string") @@ -415,5 +425,6 @@ return Object.freeze({activeInputPins,inputChoices,inputLabel,outputChoices, externalPresentation,setupDraft,setupPresentation,setupEditDraft, setupInitial,setupBegin,setupAck,setupReadback, - setupTimeout,setupClearResult,nativeLifecycleLease,nativeLifecycleDecision}); + setupTimeout,setupClearResult,nativeLifecycleLease,nativeLifecycleDecision, + nativeTransientReadbackPolicy}); }); diff --git a/web/approved-native-bridge.js b/web/approved-native-bridge.js index 9716ba50d..249ae16f3 100644 --- a/web/approved-native-bridge.js +++ b/web/approved-native-bridge.js @@ -2676,7 +2676,11 @@ // changed PCM authority; the native session, .smix, FX instances and // approved controls remain owned by JUCE and will be repainted by readback. let lastNativeLifecycleLease=null; - function expireTransientNativeUi(reason) { + // G-MV24: ONE complete GUI-only discard path for both physical/authority + // switches and repeated NEEDS_REMAP readbacks. No second PCM ownership. + // Keep cosmetic readback signatures separate so retained-mixer telemetry + // does not force pointless full DOM repaint on every callback. + function discardTransientNativeUi({preserveRetainedIdentity=false}={}) { pendingNativeSources.clear();pendingSourceQueue.clear(); activeNativeSource=null;nativeUiSourcePending=null;pendingSourceId=0; pendingOutputPatch=null;pendingNativeFxPower=null;pendingGlobalFx=null; @@ -2690,10 +2694,16 @@ // must not strand EXTERNAL OUT selects or slot-bypass edits behind // an old ACK/timer. Never touch persisted PCM/FX state here. pendingExternal.clear();pendingSlotPowers.clear(); - if(pendingIdentity){ + // Cosmetic ChannelId edits remain legal while native playback is + // NEEDS_REMAP. They still require their own native ACK/readback proof. + // Only physical/authority changes must invalidate those old identities. + if(pendingIdentity&&!preserveRetainedIdentity){ if(pendingIdentity.migration)legacyDeferred.add(pendingIdentity.channelId); rejectIdentity("Channel identity expired after audio connection change"); } + } + function expireTransientNativeUi(reason) { + discardTransientNativeUi(); lastPaintedSignature="";lastNativeTopologySignature=""; status(reason==="DEVICE_CHANGED"? "Audio connection changed ยท old control gestures discarded": @@ -2839,8 +2849,11 @@ } // A second channel's IN can be selected while the first transaction is // in flight. Always dispatch its latest choice with a FRESH native route - // revision, not stale tokens captured before the previous ACK. - if(!nativeUiSourcePending && pendingSourceQueue.size) + // revision, not stale tokens captured before the previous ACK. An old + // M4 microtask must never be scheduled from retained hardware state. + const transientPlan=deviceVM.nativeTransientReadbackPolicy(readModel.kind); + if(transientPlan.drainM4Sources && + !nativeUiSourcePending && pendingSourceQueue.size) window.queueMicrotask(()=>dispatchQueuedSource()); const caps=next?.graph?.capabilities; const nativeAdmitted=readModel.kind==="CHANNEL_ID"; @@ -2855,20 +2868,20 @@ !caps?.supportedOps?.includes("CREATE_GROUP"); addBus.disabled=!!pendingBusCreate||!!pendingTrackDelete; } - if (nativeAdmitted && !activeNativeSource && pendingNativeSources.size) { + if (transientPlan.drainNativeSources && + !activeNativeSource && pendingNativeSources.size) { // A rejected/timeout source command may leave the graph revision // unchanged. Drain the next distinct channel after this authoritative // snapshot even if the GUI signature is otherwise identical. window.queueMicrotask(dispatchNativeSource); } - if (next.retained && !nativeAdmitted) { + if (transientPlan.discardRetained) { flags.productionCallbackLive=false;flags.acceptedGraphConnected=false; - pendingNativeSources.clear();pendingSourceQueue.clear(); - activeNativeSource=null;nativeUiSourcePending=null; - pendingOutputPatch=null;pendingNativeFxPower=null;pendingBusCreate=null; - pendingFxInsert=null;pendingTrackDelete=null;startupWire=null;activeWireAttempt=null; - pendingWireGains.clear();pendingNativeControl.clear();liveFaderVisualTargets.clear(); - lastNativeControlSent.clear(); + // Also catches stray same-lease transient edits after reconnect. This + // silently discards ALL source, FX, WIRE and output gesture leases via + // the same method used for authority/endpoint changes. Unlike expire, + // it does not reset the accepted retained-graph paint signature. + discardTransientNativeUi({preserveRetainedIdentity:true}); const signature="retained|"+next.identity+"|"+appearanceRevision; if(lastPaintedSignature!==signature && projectNativeGraphWithoutFocusLoss(next)){ lastPaintedSignature=signature;