Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions tests/approved_native_m2_contract.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,134 @@ test("MV-7 native SOURCE requires ACKed graph revision AND independently install
"Channel Strip readback remains pure; bridge holds one transient source queue");
});

test("G-MV23 ChannelId SOURCE queue skips an already installed choice after timeout or ACK race",()=>{
const epoch="18446744073709551610",id="9007199254740993";
const config=protocol.nativeHardwareConfiguration({sampleRate:48000,bufferFrames:256,
enabledInputs:[0,1,2,3],enabledOutputs:[0,1]});
const choices=[{label:"None",mode:"NONE",left:-1,right:-1},
{label:"Mic 1",mode:"MONO",left:0,right:-1},
{label:"Mic 2",mode:"MONO",left:1,right:-1},
{label:"Stereo 3+4",mode:"STEREO",left:2,right:3}];
const choice=choices[1];
const queued=stripVM.sourceQueueSelection({choice,choices,epoch,
acceptedLabel:"None",activeSameChannel:true,
hardwareConfiguration:config}).entry;
const hardware={device:{epoch},hardwareConfiguration:config,
raw:{mixerSession:{authority:"CHANNEL_ID",deviceEpoch:epoch}}};
const installed={admitted:true,retained:false,device:{epoch},
hardwareConfiguration:config,
graph:{revision:"70",rosterRevision:"80",channels:[{
id,role:"INPUT",source:{sourceFormat:"MONO",portId:"in:0"}}]}};
const q=new Map([[id,queued]]);
const check=(snapshot=installed,h=hardware,entry=queued,active=null)=>
stripVM.sourceQueueHead({authority:"CHANNEL_ID",queue:new Map([[id,entry]]),
active,hardware:h,native:snapshot,choices,deviceReady:true});
assert.deepEqual(check(),{kind:"SKIP_INSTALLED",key:id},
"native source already installed after first in-flight patch: do not send twice");
assert.equal(q.size,1,"pure VM must not mutate the bridge-owned pending queue");
assert.deepEqual(check(installed,hardware,queued,{requestId:"source-native-7"}),
{kind:"IDLE"},"never skip while a source request is still in flight");
const firstRejected={...installed,graph:{...installed.graph,revision:"71",
channels:[{id,role:"INPUT",source:null}]}};
assert.equal(check(firstRejected).kind,"SEND",
"after a rejected first request, latest choice must remain dispatchable");
assert.equal(check(firstRejected).payload.graphRevision,"71",
"failed first request is retried only using fresh accepted graph revision");
const changedChoice={...queued,choice:choices[2]};
assert.equal(check(installed,hardware,changedChoice).kind,"SEND",
"different latest choice must not be dropped just because first ACK succeeded");
const otherPhysical={...hardware,hardwareConfiguration:config+"-different"};
assert.equal(check(installed,otherPhysical).kind,"DROP",
"a no-op must NEVER mask same-epoch hardware identity mismatch");
assert.deepEqual(check({...installed,admitted:false}),{kind:"WAIT"});
assert.deepEqual(check(installed,{...hardware,raw:{
mixerSession:{authority:"M4",deviceEpoch:epoch}}}),{kind:"WAIT"});
assert.equal(check({...installed,graph:{...installed.graph,channels:[{
id,role:"INPUT",source:{sourceFormat:"MONO",portId:"in:0",
bindingState:"NEEDS_REMAP"}}]}}).kind,"SEND",
"a retained physical input mapping is never verified installed PCM");
assert.equal(check({...installed,graph:{...installed.graph,channels:[{
id,role:"GROUP",source:{sourceFormat:"MONO",portId:"in:0"}}]}}).kind,
"DROP","invalid graph ownership cannot be skipped");
const none=stripVM.sourceQueueSelection({choice:choices[0],choices,epoch,
acceptedLabel:"Mic 1",hardwareConfiguration:config}).entry;
assert.deepEqual(check(firstRejected,hardware,none),
{kind:"SKIP_INSTALLED",key:id},
"SOURCE NONE is already installed only when the real ChannelId source is absent");
assert.equal(check(installed,hardware,none).kind,"SEND",
"SOURCE NONE must remain a real patch when an input is still bound");
const stereo=stripVM.sourceQueueSelection({choice:choices[3],choices,epoch,
acceptedLabel:"None",hardwareConfiguration:config}).entry;
assert.equal(check({...installed,graph:{...installed.graph,channels:[{
id,role:"INPUT",source:{sourceFormat:"STEREO",
portId:"in:2",portIdRight:"in:3"}}]}},hardware,stereo).kind,"SKIP_INSTALLED");
assert.equal(check({...installed,graph:{...installed.graph,channels:[{
id,role:"INPUT",source:{sourceFormat:"STEREO",
portId:"in:2",portIdRight:"in:1"}}]}},hardware,stereo).kind,"SEND",
"right-channel pin mismatch must not be treated as already installed");
assert.strictEqual(stripVM.sourceQueueAlreadyInstalled({authority:"CHANNEL_ID",
key:id,entry:queued,hardware,native:installed}),true);
assert.deepEqual([...q],[[id,queued]],"SOURCE VM cannot mutate queued drafts");
});

test("G-MV23 M4 accepted SOURCE no-op avoids duplicate patch but preserves first sound",()=>{
const epoch="71",config=protocol.nativeHardwareConfiguration({
sampleRate:48000,bufferFrames:128,enabledInputs:[0,1],
enabledOutputs:[0,1]});
const choices=[{label:"None",mode:"NONE",left:-1,right:-1},
{label:"Mic 1",mode:"MONO",left:0,right:-1},
{label:"Mic 2",mode:"MONO",left:1,right:-1}];
const q=choice=>new Map([[4,{epoch,hardwareConfiguration:config,choice}]]);
const session={authority:"M4",deviceEpoch:epoch,routeRevision:"29",
channels:Array.from({length:10},(_,i)=>({role:"INPUT",
source:i===4?{mode:"MONO",left:0,right:-1}:
{mode:"NONE",left:-1,right:-1}}))};
const legacyM4={active:true,routeAccepted:true,routingRevision:"29"};
const hardware={admitted:false,retained:false,device:{epoch},
hardwareConfiguration:config,raw:{mixerSession:session,legacyM4}};
const native={admitted:false,retained:false,device:{epoch},
hardwareConfiguration:config};
const check=(hardwareValue=hardware,choice=choices[1])=>
stripVM.sourceQueueHead({authority:"M4",queue:q(choice),
hardware:hardwareValue,native,choices,deviceReady:true});
assert.deepEqual(check(),{kind:"SKIP_INSTALLED",key:4},
"M4 matching accepted producer readback after timeout skips redundant PCM switch");
assert.equal(check(hardware,choices[2]).kind,"SEND",
"a new physical selection must use the latest M4 routing revision");
assert.equal(check(hardware,choices[0]).kind,"SEND",
"SOURCE NONE requires real patch when old M4 route is still installed");
const cleared={...hardware,raw:{...hardware.raw,mixerSession:{
...session,channels:session.channels.map((ch,i)=>i===4?
{...ch,source:{mode:"NONE",left:-1,right:-1}}:ch)}}};
assert.deepEqual(check(cleared,choices[0]),{kind:"SKIP_INSTALLED",key:4});
const preparing={...hardware,raw:{legacyM4:{...legacyM4,routeAccepted:false},
mixerSession:{...session,authority:"NONE"}}};
assert.equal(check(preparing).kind,"SEND",
"UNADMITTED to M4 first sound must never be mistaken for verified PCM");
const inconsistent={...hardware,raw:{legacyM4:{...legacyM4,routingRevision:"30"},
mixerSession:session}};
assert.equal(check(inconsistent).kind,"SEND",
"inconsistent route revisions cannot prove installed M4 source");
assert.equal(check({...hardware,admitted:true}).kind,"SEND");
assert.equal(check({...hardware,retained:true}).kind,"SEND");
assert.equal(check({...hardware,hardwareConfiguration:config+"-old"}).kind,"DROP");
const brokenSource={...hardware,raw:{legacyM4,mixerSession:{
...session,channels:session.channels.map((ch,i)=>i===4?
{...ch,source:{mode:"STEREO",left:0,right:1}}:ch)}}};
assert.equal(check(brokenSource).kind,"SEND");
const bridge=read("web/approved-native-bridge.js");
const body=bridge.slice(bridge.indexOf(" function dispatchSourceQueue(authority){"),
bridge.indexOf(" function dispatchNativeSource()"));
assert.match(body,/if\(plan\.kind==="SKIP_INSTALLED"\)continue;/);
assert.ok(body.indexOf('if(plan.kind==="SKIP_INSTALLED")continue;')<
body.indexOf('backend.emitEvent("arMixAction"'),
"verified no-op must exit BEFORE the sole native SOURCE dispatcher");
assert.equal((body.match(/backend\.emitEvent\("arMixAction"/g)||[]).length,1,
"single dispatcher and no second PCM owner");
assert.doesNotMatch(stripVM.sourceQueueAlreadyInstalled.toString(),
/backend\.|document\.|window\.|setTimeout|AudioContext|localStorage/);
});

test("G-MV22 physical SOURCE leases reject same-epoch hardware remaps for ChannelId and M4",()=>{
const epoch="91",id="9007199254740993";
const d={sampleRate:48000,bufferFrames:256,enabledInputs:[0,1],enabledOutputs:[0,1]};
Expand Down
91 changes: 69 additions & 22 deletions web/approved-channel-strip-view-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,65 @@
}
return unavailable;
}
// G-MV23: ONE pure accepted-source comparator reused by the normal
// correlated ACK/readback settlement and the queue's no-op optimization.
// No GUI label, ACK payload, or pending request may prove installed PCM.
function installedSourceMatches(authority,choice,source) {
if(!choice||!["NONE","MONO","STEREO"].includes(choice.mode))return false;
if(authority==="CHANNEL_ID"){
if(choice.mode==="NONE")return !source;
if(!source||source.bindingState==="NEEDS_REMAP"||
source.portId!=="in:"+choice.left)return false;
if(choice.mode==="MONO")
return source.sourceFormat==="MONO"&&source.portIdRight===undefined;
return source.sourceFormat==="STEREO"&&
source.portIdRight==="in:"+choice.right;
}
return authority==="M4"&&!!source&&
source.mode===choice.mode&&source.left===choice.left&&
source.right===choice.right;
}
// Never skip a native SOURCE patch merely because its pin numbers match.
// An independently installed, admitted ChannelId graph (or fully
// accepted M4 producer) with the exact current physical lease is required.
// A stale/NEEDS_REMAP/unadmitted session must still use normal native
// preflight rather than be optimistically accepted as already installed.
function sourceQueueAlreadyInstalled({authority,key,entry,hardware,native}={}) {
if(!entry?.choice||!decimalId(entry.epoch)||
hardware?.device?.epoch!==entry.epoch||
(hardware?.hardwareConfiguration!==undefined&&
entry.hardwareConfiguration!==hardware.hardwareConfiguration)||
(native?.hardwareConfiguration!==undefined&&
entry.hardwareConfiguration!==native.hardwareConfiguration))
return false;
if(authority==="CHANNEL_ID"){
const session=hardware?.raw?.mixerSession;
if(!decimalId(key)||native?.admitted!==true||
native?.retained===true||session?.authority!=="CHANNEL_ID"||
session.deviceEpoch!==entry.epoch||
native?.device?.epoch!==entry.epoch)return false;
const row=native.graph?.channels?.find(ch=>ch.id===key);
return row?.role==="INPUT"&&installedSourceMatches(
authority,entry.choice,row.source);
}
if(authority==="M4"){
const m=hardware?.raw?.legacyM4,session=hardware?.raw?.mixerSession;
if(hardware?.admitted!==false||hardware?.retained===true||
m?.active!==true||m.routeAccepted!==true||
session?.authority!=="M4"||
session.deviceEpoch!==entry.epoch||
!positiveCounter(session.routeRevision)||
session.routeRevision!==m.routingRevision||
!Number.isInteger(key)||key<0||key>=10||
!Array.isArray(session.channels)||session.channels.length!==10)
return false;
const row=session.channels[key];
return row?.role==="INPUT"&&installedSourceMatches(
authority,entry.choice,row.source);
}
return false;
}

// MV-14: pure FIFO head shared by ChannelId and legacy M4 INPUT.
// The bridge alone owns queue mutation, native events, timeouts and
// correlated ACK/readback. Never replay pin drafts from an old device.
Expand All @@ -296,9 +355,10 @@
const plan=sourceDispatchPlan({authority,entry,hardware,native,
channelId:authority==="CHANNEL_ID"?key:undefined,
channel:authority==="M4"?key:undefined,choices,deviceReady});
return plan.kind==="READY"?
{kind:"SEND",key,entry,action:plan.action,payload:plan.payload}:
{kind:"DROP",key};
if(plan.kind!=="READY")return {kind:"DROP",key};
if(sourceQueueAlreadyInstalled({authority,key,entry,hardware,native}))
return {kind:"SKIP_INSTALLED",key};
return {kind:"SEND",key,entry,action:plan.action,payload:plan.payload};
}

// G-MV22: one pure request-lease factory for both native SOURCE paths.
Expand Down Expand Up @@ -398,18 +458,8 @@
BigInt(pending.ackRevision)<=BigInt(pending.beforeRevision)||
BigInt(next.graph.revision)<BigInt(pending.ackRevision))
return "PENDING";
const choice=pending.choice;
if(!choice||!["NONE","MONO","STEREO"].includes(choice.mode))
return "PENDING";
const source=channel.source;
if(choice.mode==="NONE")return !source?"CONFIRMED":"PENDING";
if(!source||source.bindingState==="NEEDS_REMAP"||
source.portId!=="in:"+choice.left)return "PENDING";
if(choice.mode==="MONO")
return source.sourceFormat==="MONO" &&
source.portIdRight===undefined?"CONFIRMED":"PENDING";
return source.sourceFormat==="STEREO" &&
source.portIdRight==="in:"+choice.right?"CONFIRMED":"PENDING";
return installedSourceMatches("CHANNEL_ID",pending.choice,channel.source)?
"CONFIRMED":"PENDING";
}


Expand Down Expand Up @@ -445,12 +495,8 @@
!Number.isSafeInteger(pending.revision)||
BigInt(ackRevision)<=BigInt(pending.revision)||
BigInt(revision)<BigInt(ackRevision))return "PENDING";
const desired=pending.choice,source=channels[pending.channel].source;
if(!desired||!["NONE","MONO","STEREO"].includes(desired.mode)||
!source||source.mode!==desired.mode||
source.left!==desired.left||source.right!==desired.right)
return "PENDING";
return "CONFIRMED";
return installedSourceMatches("M4",pending.choice,
channels[pending.channel].source)?"CONFIRMED":"PENDING";
}

// MV-15: a single readback decision for both native SOURCE authorities.
Expand Down Expand Up @@ -666,7 +712,8 @@
return Object.freeze({outputTargets,inputChoices,externalChoices,
primaryChoices,resolvePrimarySelection,readbackBindings,externalPending,
primaryOutMutation,externalOutputPlan,sourceQueueSelection,
sourceDispatchPlan,sourceQueueHead,sourceQueueRequestLease,
sourceDispatchPlan,sourceQueueHead,sourceQueueAlreadyInstalled,
sourceQueueRequestLease,
sourceAckResult,sourceReadbackSettlement,
m4SourceReadbackSettlement,sourceQueueReadback,sourceQueueAckTransition,
sourceQueueTimeoutDecision,
Expand Down
5 changes: 5 additions & 0 deletions web/approved-native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,11 @@
if(plan.kind==="IDLE"||plan.kind==="WAIT")return;
if(plan.kind==="DROP_ALL"){queue.clear();return;}
queue.delete(plan.key);
// G-MV23: after ACK/readback races or timeout, skip a last-choice
// draft that the accepted native PCM has ALREADY installed. Avoid
// reissuing the same routing switch and risking needless audio churn.
// The pure VM allows this only with the exact hardware/PCM lease.
if(plan.kind==="SKIP_INSTALLED")continue;
if(plan.kind==="DROP"){
if(channelIdOwner)
nativeToast("Source identity or device pins changed · refresh native mixer");
Expand Down
Loading