diff --git a/.github/workflows/ui-smoke.yml b/.github/workflows/ui-smoke.yml index 9e9438823..d4c1673fc 100644 --- a/.github/workflows/ui-smoke.yml +++ b/.github/workflows/ui-smoke.yml @@ -31,6 +31,7 @@ on: - 'tests/approved_native_m2_contract.test.cjs' - 'tests/approved_device_pin_identity_contract.test.cjs' - 'tests/approved_scalar_view_model_contract.test.cjs' + - 'tests/approved_scalar_scheduler_contract.test.cjs' - 'scripts/build_ui_css.py' # Only user-facing GUI/bridge files are relevant. No PR metadata API gate. pull_request: @@ -57,6 +58,7 @@ on: - 'tests/approved_native_m2_contract.test.cjs' - 'tests/approved_device_pin_identity_contract.test.cjs' - 'tests/approved_scalar_view_model_contract.test.cjs' + - 'tests/approved_scalar_scheduler_contract.test.cjs' - 'scripts/build_ui_css.py' - '.github/workflows/ui-smoke.yml' - '.github/workflows/pages-publish.yml' @@ -135,6 +137,7 @@ jobs: run: | node --test tests/approved_device_pin_identity_contract.test.cjs node --test tests/approved_scalar_view_model_contract.test.cjs + node --test tests/approved_scalar_scheduler_contract.test.cjs node --test tests/floating_window_workspace_contract.test.mjs node --test tests/rc_operator_workflow_contract.test.mjs node --test tests/workstream_coordination_contract.test.mjs tests/chat_ci_dispatch_contract.test.mjs tests/gui_workflow_contract_test.mjs tests/plugin_ui_contract_test.mjs tests/ci_workflow_efficiency_test.mjs tests/fx_drag_reorder_contract_test.mjs tests/mixer_io_operator_contract_test.mjs tests/mixer_shell_contract_test.mjs tests/approved_native_m2_contract.test.cjs @@ -501,6 +504,7 @@ jobs: node --test tests/workstream_coordination_contract.test.mjs tests/chat_ci_dispatch_contract.test.mjs tests/gui_workflow_contract_test.mjs tests/plugin_ui_contract_test.mjs tests/ci_workflow_efficiency_test.mjs tests/fx_drag_reorder_contract_test.mjs tests/mixer_io_operator_contract_test.mjs tests/mixer_shell_contract_test.mjs tests/approved_native_m2_contract.test.cjs node --test tests/approved_device_pin_identity_contract.test.cjs node --test tests/approved_scalar_view_model_contract.test.cjs + node --test tests/approved_scalar_scheduler_contract.test.cjs - name: Validate approved GUI checkpoint and single publisher run: python tests/gui_baseline_lock_test.py python tests/mixer_physical_keys_contract.py diff --git a/tests/approved_scalar_scheduler_contract.test.cjs b/tests/approved_scalar_scheduler_contract.test.cjs new file mode 100644 index 000000000..d93aa3198 --- /dev/null +++ b/tests/approved_scalar_scheduler_contract.test.cjs @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// MV-10: deterministic scalar scheduler with no DOM, clocks, timers, PCM or extra store. +const test=require("node:test"); +const assert=require("node:assert/strict"); +const {readFileSync}=require("node:fs"); +const vm=require("../web/approved-channel-strip-view-model.js"); + +const identity={epoch:"18446744073709551610",authority:"CHANNEL_ID", + channelId:"9007199254740993"}; +const edit={parameter:"faderDb",value:-14.5,t:{kind:"audio",id:3}, + ...identity}; +const lease=at=>vm.scalarDispatchLease(edit,at); +const ready=(overrides={})=>({ + identity,ready:true,logical:false,logicalPending:false,m4Owner:false, + nativePending:false,...overrides +}); +const gesture=(overrides={})=>({ + final:false,logical:false,now:135,lastSent:lease(120), + identity:edit,...overrides +}); + +test("MV-10 throttle lease binds exact uint64 identity, not recycled strip ordinal",()=>{ + assert.deepEqual(lease(120),{at:120,epoch:identity.epoch, + authority:"CHANNEL_ID",channelId:identity.channelId}); + assert.equal(vm.scalarGestureFlushPlan(gesture()),"QUEUE"); + assert.equal(vm.scalarGestureFlushPlan(gesture({now:149})),"QUEUE"); + assert.equal(vm.scalarGestureFlushPlan(gesture({now:150})),"FLUSH"); + assert.equal(vm.scalarGestureFlushPlan(gesture({ + identity:{...edit,channelId:"9007199254740994"}})),"FLUSH"); + assert.equal(vm.scalarGestureFlushPlan(gesture({ + identity:{...edit,epoch:"18446744073709551611"}})),"FLUSH"); + assert.equal(vm.scalarGestureFlushPlan(gesture({ + identity:{...edit,authority:"M4"}})),"FLUSH"); + assert.equal(vm.scalarGestureFlushPlan(gesture({ + identity:{...edit,channelId:Number(identity.channelId)}})),"FLUSH"); +}); + +test("MV-10 pointer release is never swallowed by throttling or offline edit",()=>{ + assert.equal(vm.scalarGestureFlushPlan(gesture({final:true,now:120})),"FLUSH"); + assert.equal(vm.scalarGestureFlushPlan(gesture({final:true,now:121,logical:true})),"FLUSH"); + assert.equal(vm.scalarGestureFlushPlan(gesture({logical:true,lastSent:null})),"QUEUE"); + assert.equal(vm.scalarGestureFlushPlan(gesture({logical:true,now:900})),"QUEUE", + "offline mousemove should not emit one session generation per frame"); +}); + +test("MV-10 clock rollover, bad timestamp and first gesture cannot be delayed",()=>{ + for(const g of [ + {lastSent:null},{now:0},{lastSent:lease(Infinity)}, + {lastSent:lease(NaN)},{now:NaN},{now:Infinity}, + {lastSent:{at:120,epoch:"0",authority:"CHANNEL_ID",channelId:identity.channelId}} + ])assert.equal(vm.scalarGestureFlushPlan(gesture(g)),"FLUSH", + "invalid/older clock cannot block last target"); +}); + +test("MV-10 one native ChannelId fader operation from accepted edit",()=>{ + const before=JSON.stringify(edit); + const plan=vm.scalarDispatchPlan(edit,ready()); + assert.deepEqual(plan,{kind:"INTENT",operation:"SET_FADER", + payload:{valueDb:-14.5}}); + assert.equal(JSON.stringify(edit),before); + const pan=vm.scalarDispatchPlan({...edit,parameter:"pan",value:-36},ready()); + assert.deepEqual(pan,{kind:"INTENT",operation:"SET_PAN",payload:{value:-.36}}); + const main=vm.scalarDispatchPlan({...edit,parameter:"masterGain",value:2.5},ready()); + assert.deepEqual(main,{kind:"INTENT",operation:"SET_FADER",payload:{valueDb:2.5}}); +}); + +test("MV-10 ChannelId readiness and exact identity fail closed before emit",()=>{ + for(const opts of [ + {identity:{...identity,epoch:"18446744073709551611"}}, + {identity:{...identity,channelId:"9007199254740994"}}, + {identity:{...identity,authority:"LOGICAL"}}, + {identity:null},{ready:false} + ])assert.deepEqual(vm.scalarDispatchPlan(edit,ready(opts)),{kind:"REJECT"}); + for(const bad of [ + {...edit,value:NaN},{...edit,value:Infinity},{...edit,value:"-14.5"}, + {...edit,parameter:"mute"},{...edit,parameter:"foo"} + ])assert.deepEqual(vm.scalarDispatchPlan(bad,ready()),{kind:"REJECT"}); +}); + +test("MV-10 native pending controller holds latest target, never reports rejection",()=>{ + assert.equal(vm.scalarDispatchPlan(edit,ready({nativePending:true})).kind,"WAIT"); + assert.equal(vm.scalarDispatchPlan({...edit,parameter:"pan"},ready({nativePending:true})).kind,"WAIT"); + assert.equal(vm.scalarDispatchPlan(edit,ready({nativePending:false})).kind,"INTENT"); + // A stalled logical edit is also deferred; native does not supersede it. + assert.equal(vm.scalarDispatchPlan(edit,ready({ + logical:true,logicalPending:true})).kind,"WAIT"); + assert.equal(vm.scalarDispatchPlan(edit,ready({ + logical:true,logicalPending:false})).kind,"INTENT"); +}); + +test("MV-10 M4/MAIN continue to use the one existing routing transport",()=>{ + assert.deepEqual(vm.scalarDispatchPlan(edit,ready({m4Owner:true})),{kind:"M4"}); + assert.deepEqual(vm.scalarDispatchPlan(edit,ready({ + m4Owner:true,nativePending:true})),{kind:"M4"}, + "MAIN/M4 must not be blocked by unrelated ChannelId controller"); + assert.deepEqual(vm.scalarDispatchPlan(edit,ready({ + logical:true,m4Owner:true})),{kind:"INTENT",operation:"SET_FADER", + payload:{valueDb:-14.5}}, "logical .smix edit never emits native M4"); + assert.deepEqual(vm.scalarDispatchPlan(edit,ready({ + m4Owner:true,ready:false})),{kind:"REJECT"}); +}); + +test("MV-10 two-strip burst stays isolated, pointerup releases last writer",()=>{ + const targets=new Map(),sent=new Map(); + const a={...edit,channelId:"9007199254740993",value:-18}; + const b={...edit,channelId:"9007199254740994",value:-10}; + function queue(key,item,at,final=false){ + targets.set(key,item); + return vm.scalarGestureFlushPlan({ + identity:item,now:at,final,lastSent:sent.get(key)}); + } + assert.equal(queue("fader:3",a,0),"FLUSH"); + sent.set("fader:3",vm.scalarDispatchLease(a,0)); + assert.equal(queue("fader:3",{...a,value:-16},8),"QUEUE"); + assert.equal(queue("fader:4",b,9),"FLUSH"); + sent.set("fader:4",vm.scalarDispatchLease(b,9)); + assert.equal(queue("fader:3",{...a,value:-12},10,true),"FLUSH"); + assert.equal(targets.get("fader:3").value,-12); + assert.equal(targets.get("fader:4").value,-10); + assert.equal(queue("fader:3",{...b,value:-3},11),"FLUSH", + "reused local fader ordinal cannot inherit old channel time lease"); +}); + +test("MV-10 bridge has ONE scalar queue/transport and immutable ViewModel policy",()=>{ + const bridge=readFileSync("web/approved-native-bridge.js","utf8"); + const source=readFileSync("web/approved-channel-strip-view-model.js","utf8"); + assert.equal((bridge.match(/const liveFaderVisualTargets=new Map\(\)/g)||[]).length,1); + assert.equal((bridge.match(/pendingNativeControl=new Map\(\)/g)||[]).length,1); + assert.equal((bridge.match(/function queueLiveControlFlush\(/g)||[]).length,1); + assert.equal((bridge.match(/function flushLiveScalars\(/g)||[]).length,1); + assert.match(bridge,/stripVM\.scalarGestureFlushPlan\(/); + assert.match(bridge,/stripVM\.scalarDispatchPlan\(/); + assert.match(bridge,/stripVM\.scalarDispatchLease\(edit,performance\.now\(\)\)/); + assert.match(bridge,/lastNativeControlSent\.clear\(\)/); + assert.equal((bridge.match(/action:"routingM4Control"/g)||[]).length,1); + assert.doesNotMatch(source,/window\.|document\.|setTimeout|AudioContext|backend\.emitEvent/); +}); diff --git a/web/approved-channel-strip-view-model.js b/web/approved-channel-strip-view-model.js index 50c711c44..406c167cd 100644 --- a/web/approved-channel-strip-view-model.js +++ b/web/approved-channel-strip-view-model.js @@ -365,10 +365,42 @@ return pending.acked===true&&matches?"CONFIRMED":"WAIT"; } + // MV-10: pure scalar dispatch/coalescing policy. The bridge alone owns + // timers, one pending Map, native transport and the accepted PCM model. + // The throttle lease belongs to the EXACT native identity, not a recycled + // local strip key, so reconnect/roster replacement can send immediately. + function scalarDispatchLease(edit,at) { + return {at,epoch:edit.epoch,authority:edit.authority, + channelId:edit.channelId}; + } + function scalarGestureFlushPlan({logical=false,final=false,now, + lastSent=null,identity=null}={}) { + if(final)return "FLUSH"; // never defer pointerup/release + if(logical)return "QUEUE"; // offline .smix: one edit on release + if(!lastSent || !scalarIdentityMatches(lastSent,identity) || + !Number.isFinite(lastSent.at) || !Number.isFinite(now) || + now=30)return "FLUSH"; + return "QUEUE"; // keep latest target; existing one-shot timer owns retry + } + function scalarDispatchPlan(edit,{identity=null,ready=false, + logical=false,logicalPending=false,m4Owner=false,nativePending=false}={}) { + if(!scalarIdentityMatches(edit,identity) || ready!==true || + !Number.isFinite(edit.value) || + !["faderDb","masterGain","pan"].includes(edit.parameter)) + return {kind:"REJECT"}; + if(logical && logicalPending)return {kind:"WAIT"}; + if(!logical && m4Owner)return {kind:"M4"}; + if(!logical && nativePending)return {kind:"WAIT"}; + return edit.parameter==="pan"? + {kind:"INTENT",operation:"SET_PAN",payload:{value:edit.value/100}}: + {kind:"INTENT",operation:"SET_FADER",payload:{valueDb:edit.value}}; + } + return Object.freeze({outputTargets,inputChoices,externalChoices, primaryChoices,resolvePrimarySelection,readbackBindings,externalPending, primaryOutMutation,externalOutputPlan,sourceQueueSelection, sourceDispatchPlan,sourceAckResult,sourceReadbackSettlement, scalarIdentityMatches,scalarVisualSettlement,m4ScalarAckResult, - m4ScalarReadbackSettlement}); + m4ScalarReadbackSettlement,scalarDispatchLease, + scalarGestureFlushPlan,scalarDispatchPlan}); }); diff --git a/web/approved-native-bridge.js b/web/approved-native-bridge.js index 11b93e1bc..8013f8f9e 100644 --- a/web/approved-native-bridge.js +++ b/web/approved-native-bridge.js @@ -1524,22 +1524,25 @@ // null means an admitted ChannelId intent is already awaiting its ACK / // producer readback. Keep ONLY the newest dB target, never drop release. function dispatchLiveScalar(edit) { - if(!sameNativeEditIdentity(edit) || - !liveScalarReady(edit.parameter,edit.t))return false; - if(logicalView&&pendingLogical)return null; - if(!logicalView&&(edit.t?.kind==="master" || m4ControlIsOwner())) - return sendM4Control(edit.parameter,edit.t,edit.value); - if(!logicalView&&controller?.state()?.pending)return null; - return intent(edit.parameter==="faderDb"||edit.parameter==="masterGain"?"SET_FADER":"SET_PAN", - edit.t,edit.parameter==="faderDb"||edit.parameter==="masterGain"? - {valueDb:edit.value}:{value:edit.value/100}); + const plan=stripVM.scalarDispatchPlan(edit,{ + identity:nativeEditIdentity(edit.t), + ready:liveScalarReady(edit.parameter,edit.t), + logical:!!logicalView,logicalPending:!!pendingLogical, + m4Owner:edit.t?.kind==="master"||m4ControlIsOwner(), + nativePending:!!controller?.state()?.pending + }); + if(plan.kind==="REJECT")return false; + if(plan.kind==="WAIT")return null; + if(plan.kind==="M4")return sendM4Control(edit.parameter,edit.t,edit.value); + return intent(plan.operation,edit.t,plan.payload); } function flushLiveScalars() { for(const [key,edit] of pendingNativeControl) { const result=dispatchLiveScalar(edit); if(result===null)continue; pendingNativeControl.delete(key); - if(result)lastNativeControlSent.set(key,performance.now()); + if(result)lastNativeControlSent.set(key, + stripVM.scalarDispatchLease(edit,performance.now())); else { if(liveFaderVisualTargets.get(key)===edit) liveFaderVisualTargets.delete(key); @@ -1554,15 +1557,14 @@ pendingNativeControl.set(key,edit); // coalesce: last writer always wins if(parameter==="faderDb"||parameter==="masterGain") liveFaderVisualTargets.set(key,edit); - // Offline document changes are not realtime PCM. Persist only the - // released target, not one .smix generation per pointer-move frame. - if(logicalView){ - if(final)flushLiveScalars(); - return true; - } - const last=lastNativeControlSent.get(key); - if(final||last===undefined||now-last>=30) - flushLiveScalars(); // pointermove publishes BEFORE pointerup + // One pure selector preserves last-writer-wins, pointerup release and + // offline .smix persistence, while ignoring old channel/epoch throttles. + const flush=stripVM.scalarGestureFlushPlan({ + logical:!!logicalView,final:!!final,now, + lastSent:lastNativeControlSent.get(key),identity:edit + }); + if(flush==="FLUSH")flushLiveScalars(); // pointermove BEFORE pointerup + if(logicalView)return true; if(pendingNativeControl.has(key))queueLiveControlFlush(); return true; } @@ -2777,6 +2779,7 @@ pendingOutputPatch=null;pendingNativeFxPower=null;pendingBusCreate=null; pendingFxInsert=null;pendingTrackDelete=null;startupWire=null;activeWireAttempt=null; pendingWireGains.clear();pendingNativeControl.clear();liveFaderVisualTargets.clear(); + lastNativeControlSent.clear(); const signature="retained|"+next.identity+"|"+appearanceRevision; if(lastPaintedSignature!==signature && projectNativeGraphWithoutFocusLoss(next)){ lastPaintedSignature=signature; @@ -2899,6 +2902,9 @@ if(edit.authority==="CHANNEL_ID")pendingNativeControl.delete(key); for(const [key,edit] of liveFaderVisualTargets) if(edit.authority==="CHANNEL_ID")liveFaderVisualTargets.delete(key); + // A rejected native graph invalidates its old send-time leases. + // M4 and offline remain readback-owned; next gesture can send now. + lastNativeControlSent.clear(); lastNativeError=state.lastError; // Never destroy an active pointer capture. Once the user releases, // restore only the last independently accepted native controls.