diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a6982611..e89f3138b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1396,6 +1396,19 @@ if(BUILD_TESTING) add_executable(ARMixRecoveryTests tests/direct_restore_backoff_test.cpp) target_compile_features(ARMixRecoveryTests PRIVATE cxx_std_20) add_test(NAME BoundedDirectDeviceRecovery COMMAND ARMixRecoveryTests) + # N-MOD-19: native JUCE CONTROL coordinator tested with fake device/driver + # status and real diagnostics, but never opening hardware or rendering PCM. + juce_add_console_app(ARMixNativeDirectRestoreTests + PRODUCT_NAME "ArSonMix Native Direct Restore Coordinator Contract") + target_sources(ARMixNativeDirectRestoreTests PRIVATE + tests/native_direct_restore_coordinator_test.cpp + src/NativeDirectRestoreCoordinator.h) + target_compile_features(ARMixNativeDirectRestoreTests PRIVATE cxx_std_20) + target_link_libraries(ARMixNativeDirectRestoreTests PRIVATE + juce::juce_core juce::juce_recommended_config_flags) + add_test(NAME NativeDirectRestoreBoundedRetryAndFaultEvidence + COMMAND ARMixNativeDirectRestoreTests) + add_dependencies(ARMixMixerRoutingTests ARMixNativeDirectRestoreTests) add_executable(ARMixClockSizingTests tests/clock_bridge_sizing_test.cpp) target_compile_features(ARMixClockSizingTests PRIVATE cxx_std_20) add_test(NAME AdaptiveClockPeriodHeadroom COMMAND ARMixClockSizingTests) diff --git a/src/Main.cpp b/src/Main.cpp index 73d9369f3..597765489 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -19,6 +19,7 @@ #include "../engine/core/DearVrPolarPolicy.h" #include "DiagnosticsRecorder.h" #include "../engine/device/DirectRestoreBackoff.h" +#include "NativeDirectRestoreCoordinator.h" #include "../engine/device/AudioSetupRecoveryPolicy.h" #include "../engine/device/AsioRestartRecoveryGate.h" #include "../engine/device/PeriodCapabilityEvidence.h" @@ -928,95 +929,24 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr return report; } - void annotateRecovery (juce::var& state) const - { - auto* object = state.getDynamicObject(); - if (object == nullptr) return; - const auto snapshot = directRestore_.snapshot(); - using State = armix::device::DirectRestoreBackoff::State; - const char* label = snapshot.state == State::idle ? "IDLE" - : snapshot.state == State::waiting ? "WAITING" : "EXHAUSTED"; - object->setProperty ("directRestoreState", label); - object->setProperty ("directRestoreAttempts", static_cast (snapshot.attempts)); - object->setProperty ("directRestoreNextMs", - static_cast (directRestore_.remainingMs (monotonicMilliseconds()))); - object->setProperty ("directRestoreGeneration", static_cast (snapshot.generation)); - object->setProperty ("directRestoreError", lastDirectRestoreError_); - object->setProperty ("directRestorePending", engine_.hasPendingDirectRestore()); + void annotateRecovery (juce::var& state) const { + armix::device::NativeDirectRestoreCoordinator::annotate( + state,directRestore_,lastDirectRestoreError_, + engine_.hasPendingDirectRestore(),monotonicMilliseconds()); } - // Device reopens happen on the JUCE message thread, never the RT callback. - // Restoring DIRECT never silently re-enables a faulting bridged route. - void serviceDirectRestore() - { - using Gate = armix::device::DirectRestoreBackoff; - const auto now = monotonicMilliseconds(); - // Independent drivers can stop callbacks without emitting JUCE error. - // Read existing atomic progress on message thread before failback gate. - engine_.pollIndependentClockHealth (now); - auto snapshot = directRestore_.snapshot(); - if (snapshot.state == Gate::State::idle && - (engine_.independentClockFaulted() || - (!engine_.independentClockActive() && engine_.hasPendingDirectRestore()))) - { - const auto generation = directRestore_.begin (now); - diagnostics_.record ("DIRECT_RESTORE_BEGIN", - juce::String ("Generation ") + juce::String (static_cast (generation))); - snapshot = directRestore_.snapshot(); - } - if (!directRestore_.due (snapshot.generation, now)) return; - - const bool wasIndependent = engine_.independentClockActive(); - const auto stateBeforeRestore = engine_.status(); - const auto faultStatus = juce::JSON::toString (stateBeforeRestore, false); - // DiagnosticsRecorder bounds event details to 950 characters. Keep - // the independent stream fault source and durations FIRST, before the - // potentially long full status JSON, so field reports retain evidence - // after closing the adapter resets its watchdog. - const auto watchdogSummary = juce::String ("format=") - + stateBeforeRestore.getProperty ("bridgeNegotiationFault", "NONE").toString() - + " watchdog=" - + stateBeforeRestore.getProperty ("bridgeWatchdogFault", "NONE").toString() - + " captureSilentMs=" - + stateBeforeRestore.getProperty ("bridgeCaptureSilentMs", 0).toString() - + " renderSilentMs=" - + stateBeforeRestore.getProperty ("bridgeRenderSilentMs", 0).toString() - + " continuity=" - + stateBeforeRestore.getProperty ("bridgeContinuityFault", "NONE").toString() - + " lossBp=" - + stateBeforeRestore.getProperty ("bridgeContinuityLossBp", 0).toString() - + " badWindows=" - + stateBeforeRestore.getProperty ("bridgeContinuityBadWindows", 0).toString(); - const auto error = engine_.setIndependentClockEnabled (false); - if (error.isEmpty()) - { - directRestore_.recovered (snapshot.generation); - lastDirectRestoreError_.clear(); - deviceSettingsDirty_ = true; - diagnostics_.record ("SMART_CLOCK_RECOVERY", - juce::String ("DIRECT_RESTORED after ") + juce::String (snapshot.attempts + 1) + - " attempt(s) | " + watchdogSummary + " | " + faultStatus); - if (engine_.isOutputMuted()) - emitNotice ("DIRECT device reopened. Output remains muted for safety; unmute only when ready.", true); - else - emitNotice (wasIndependent - ? "Independent device fault: safe DIRECT audio restored." - : "DIRECT audio device restored after a bounded retry.", true); - return; - } - - directRestore_.failed (snapshot.generation, now); - lastDirectRestoreError_ = error; - const auto next = directRestore_.snapshot(); - diagnostics_.record ("SMART_CLOCK_RECOVERY", - juce::String ("RESTORE_FAIL ") + juce::String (next.attempts) + "/" + - juce::String (static_cast (Gate::maxAttempts)) + ": " + error + - " | " + watchdogSummary + " | " + faultStatus); - if (next.state == Gate::State::exhausted) - emitNotice ("Automatic DIRECT recovery exhausted; audio remains safe/muted. " - "Check AUDIO SETUP or retry DIRECT manually. " + error, true); - else if (next.attempts == 1) - emitNotice ("Device interrupted. DIRECT restore is retrying with bounded backoff.", true); + // One control owner; recovery policy, telemetry and bounded retries live + // in the dedicated Native coordinator. No change to ASIO callback path. + void serviceDirectRestore() { + armix::device::NativeDirectRestoreCoordinator::service( + directRestore_,lastDirectRestoreError_,deviceSettingsDirty_, + monotonicMilliseconds(),engine_, + [this](const juce::String& code,const juce::String& detail) { + diagnostics_.record(code,detail); + }, + [this](const juce::String& message,bool error) { + emitNotice(message,error); + }); } void emitNotice (const juce::String& message, bool error = false) diff --git a/src/NativeDirectRestoreCoordinator.h b/src/NativeDirectRestoreCoordinator.h new file mode 100644 index 000000000..366cbd5f3 --- /dev/null +++ b/src/NativeDirectRestoreCoordinator.h @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include "../engine/device/DirectRestoreBackoff.h" +#include + +// N-MOD-19: ONE control-thread DIRECT restore coordinator. Does not own a +// device, callback, PCM renderer, driver binding, or recovery timer. The +// existing LiveAudioEngine still exclusively owns JUCE device transactions. +// The caller supplies diagnostics and operator notification sinks. +namespace armix::device { +class NativeDirectRestoreCoordinator final { +public: + using Gate=DirectRestoreBackoff; + static void annotate(juce::var& state,const Gate& gate, + const juce::String& previousError, + bool pending,std::uint64_t nowMs) { + auto* obj=state.getDynamicObject(); + if(!obj)return; + const auto snapshot=gate.snapshot(); + const char* stateName=snapshot.state==Gate::State::idle?"IDLE": + snapshot.state==Gate::State::waiting?"WAITING":"EXHAUSTED"; + obj->setProperty("directRestoreState",stateName); + obj->setProperty("directRestoreAttempts", + static_cast(snapshot.attempts)); + obj->setProperty("directRestoreNextMs", + static_cast(gate.remainingMs(nowMs))); + obj->setProperty("directRestoreGeneration", + static_cast(snapshot.generation)); + obj->setProperty("directRestoreError",previousError); + obj->setProperty("directRestorePending",pending); + } + + template + static void service(Gate& gate,juce::String& previousError, + bool& settingsDirty,std::uint64_t nowMs, + Engine& engine,Diagnostic&& record,Notice&& notify) { + // Control thread only. Polling observes independent callback atomic + // progress but never executes or blocks inside either audio callback. + engine.pollIndependentClockHealth(nowMs); + auto snapshot=gate.snapshot(); + if(snapshot.state==Gate::State::idle && + (engine.independentClockFaulted() || + (!engine.independentClockActive() && + engine.hasPendingDirectRestore()))) { + const auto generation=gate.begin(nowMs); + record("DIRECT_RESTORE_BEGIN", + juce::String("Generation ") + + juce::String(static_cast(generation))); + snapshot=gate.snapshot(); + } + if(!gate.due(snapshot.generation,nowMs))return; + + const bool wasIndependent=engine.independentClockActive(); + const auto stateBeforeRestore=engine.status(); + const auto faultStatus=juce::JSON::toString(stateBeforeRestore,false); + // Keep field diagnostics FIRST: recorder truncation must not erase + // stream watchdog evidence when the device is disposed. + const auto watchdogSummary=juce::String("format=") + + stateBeforeRestore.getProperty("bridgeNegotiationFault","NONE").toString() + + " watchdog=" + + stateBeforeRestore.getProperty("bridgeWatchdogFault","NONE").toString() + + " captureSilentMs=" + + stateBeforeRestore.getProperty("bridgeCaptureSilentMs",0).toString() + + " renderSilentMs=" + + stateBeforeRestore.getProperty("bridgeRenderSilentMs",0).toString() + + " continuity=" + + stateBeforeRestore.getProperty("bridgeContinuityFault","NONE").toString() + + " lossBp=" + + stateBeforeRestore.getProperty("bridgeContinuityLossBp",0).toString() + + " badWindows=" + + stateBeforeRestore.getProperty("bridgeContinuityBadWindows",0).toString(); + // The actual restore call remains on the one existing engine owner. + const auto error=engine.setIndependentClockEnabled(false); + if(error.isEmpty()) { + gate.recovered(snapshot.generation); + previousError.clear(); + settingsDirty=true; + record("SMART_CLOCK_RECOVERY", + juce::String("DIRECT_RESTORED after ") + + juce::String(snapshot.attempts+1) + + " attempt(s) | " +watchdogSummary+" | "+faultStatus); + if(engine.isOutputMuted()) + notify("DIRECT device reopened. Output remains muted for safety; unmute only when ready.",true); + else + notify(wasIndependent? + "Independent device fault: safe DIRECT audio restored.": + "DIRECT audio device restored after a bounded retry.",true); + return; + } + gate.failed(snapshot.generation,nowMs); + previousError=error; + const auto next=gate.snapshot(); + record("SMART_CLOCK_RECOVERY", + juce::String("RESTORE_FAIL ") + + juce::String(next.attempts) + "/" + + juce::String(static_cast(Gate::maxAttempts)) + + ": " +error+" | "+watchdogSummary+" | "+faultStatus); + if(next.state==Gate::State::exhausted) + notify("Automatic DIRECT recovery exhausted; audio remains safe/muted. " + "Check AUDIO SETUP or retry DIRECT manually. "+error,true); + else if(next.attempts==1) + notify("Device interrupted. DIRECT restore is retrying with bounded backoff.",true); + } +}; +} // namespace armix::device diff --git a/tests/direct_restore_recovery_contract_test.mjs b/tests/direct_restore_recovery_contract_test.mjs index 25bc60235..5f7d97c9f 100644 --- a/tests/direct_restore_recovery_contract_test.mjs +++ b/tests/direct_restore_recovery_contract_test.mjs @@ -7,6 +7,7 @@ const read=p=>readFileSync(p,'utf8'); const main=read('src/Main.cpp'); const engine=read('src/LiveAudioEngine.h'); const gate=read('engine/device/DirectRestoreBackoff.h'); +const coordinator=read('src/NativeDirectRestoreCoordinator.h'); const cmake=read('CMakeLists.txt'); const workflow=read('.github/workflows/p1-windows.yml'); @@ -21,8 +22,10 @@ test('actual device recoveries occur on message thread, not inside audio callbac assert.ok(graphAck>timer); assert.match(main.slice(timer,graphAck),/serviceDirectRestore\(\)/); assert.match(main.slice(runner,main.indexOf('void emitNotice',runner)), - /engine_\.setIndependentClockEnabled \(false\)/); - assert.match(main,/engine_\.independentClockFaulted\(\)/); + /NativeDirectRestoreCoordinator::service/); + assert.match(coordinator,/engine\.setIndependentClockEnabled\(false\)/); + assert.match(coordinator,/engine\.independentClockFaulted\(\)/); + assert.doesNotMatch(coordinator,/audioDeviceIOCallbackWithContext/); assert.match(engine,/hasPendingDirectRestore\(\) const noexcept/); assert.match(engine,/verifyRestoredDirectDevice \(\*savedDirectSetup_\)/); assert.match(engine,/verifyRestoredDirectDevice \(original\)/); @@ -49,18 +52,23 @@ test('output-only SAME-device WASAPI recovery never invents or requires micropho assert.match(main,/No verified stereo playback device; audio remains gated/); }); test('restore retry is bounded and disabled on success, explicit direct and setup paths',()=>{ - assert.match(main,/directRestore_\.failed \(snapshot\.generation, now\)/); - assert.match(main,/directRestore_\.recovered \(snapshot\.generation\)/); + assert.match(coordinator,/gate\.failed\(snapshot\.generation,nowMs\)/); + assert.match(coordinator,/gate\.recovered\(snapshot\.generation\)/); assert.match(main,/directRestore_\.cancel\(\)/); - assert.match(main,/independentClockActive\(\) \|\| engine_\.hasPendingDirectRestore\(\)/); - assert.match(main,/directRestoreAttempts/); - assert.match(main,/directRestoreNextMs/); - assert.match(main,/directRestoreError/); - assert.match(main,/DIRECT_RESTORE_BEGIN/); - assert.match(main,/SMART_CLOCK_RECOVERY/); + assert.match(coordinator,/engine\.independentClockActive\(\)/); + assert.match(coordinator,/engine\.hasPendingDirectRestore\(\)/); + assert.match(coordinator,/directRestoreAttempts/); + assert.match(coordinator,/directRestoreNextMs/); + assert.match(coordinator,/directRestoreError/); + assert.match(coordinator,/DIRECT_RESTORE_BEGIN/); + assert.match(coordinator,/SMART_CLOCK_RECOVERY/); + assert.match(coordinator,/bridgeCaptureSilentMs/); + assert.match(coordinator,/bridgeWatchdogFault/); assert.match(main,/annotateRecovery \(status\)/); assert.match(main,/annotateRecovery \(state\)/); assert.match(cmake,/add_test\(NAME BoundedDirectDeviceRecovery COMMAND ARMixRecoveryTests\)/); + assert.match(cmake,/add_test\(NAME NativeDirectRestoreBoundedRetryAndFaultEvidence/); + assert.match(main,/NativeDirectRestoreCoordinator::annotate/); assert.match(workflow,/direct_restore_recovery_contract_test.mjs/); }); test('verified native FAT package preserves actual beta live-bridge provenance, not older offline-only claim',()=>{ @@ -77,7 +85,9 @@ test('verified native FAT package preserves actual beta live-bridge provenance, }); test('healthy independent sessions never arm restoration; setup can replace missing devices',()=>{ - assert.match(main,/\(!engine_\.independentClockActive\(\) && engine_\.hasPendingDirectRestore\(\)\)/); + assert.match(coordinator,/!engine\.independentClockActive\(\) &&/); + assert.match(coordinator,/engine\.hasPendingDirectRestore\(\)/); + assert.doesNotMatch(coordinator,/setIndependentClockEnabled\(true\)/); assert.match(engine,/bool abandonPendingDirectRestoreForSetup\(\)/); assert.match(engine,/deviceManager_\.addAudioCallback \(this\)/); assert.match(main,/DIRECT_RESTORE_ABANDON/); diff --git a/tests/independent_clock_adapter_contract_test.mjs b/tests/independent_clock_adapter_contract_test.mjs index 705cb1be9..17a8cc65c 100644 --- a/tests/independent_clock_adapter_contract_test.mjs +++ b/tests/independent_clock_adapter_contract_test.mjs @@ -6,6 +6,7 @@ const r=p=>readFileSync(p,'utf8'); const device=r('engine/device/JuceIndependentClockDevice.h'); const engine=r('src/LiveAudioEngine.h'); const host=r('src/Main.cpp'); +const coordinator=r('src/NativeDirectRestoreCoordinator.h'); const sizing=r('engine/device/ClockBridgeSizing.h'); const watchdog=r('engine/device/IndependentStreamWatchdog.h'); const ui=r('web/monitor.js'); @@ -43,7 +44,7 @@ test('P0 immutable independent stream format fence blocks silent device/period/m assert.match(device,/formatFence_\.observeCallbackChannels/); assert.match(device,/formatFence_\.resetStopped\(\)/); assert.match(engine,/bridgeNegotiationFault/); - assert.match(host,/getProperty \("bridgeNegotiationFault", "NONE"\)/); + assert.match(coordinator,/getProperty\("bridgeNegotiationFault","NONE"\)/); assert.match(cmake,/add_test\(NAME IndependentStreamNegotiatedFormatFence COMMAND ARMixIndependentFormatTests\)/); }); @@ -58,10 +59,11 @@ test('live deployment preserves direct ASIO and bounded rollback semantics',()=> assert.match(engine,/bridgeUnderflowFrames/); assert.match(engine,/bridgeCorrectionPpm/); assert.match(engine,/WASAPI_CAPTURE_DISCONTINUITIES_ONLY/); - assert.match(host,/engine_\.independentClockFaulted\(\)/); + assert.match(coordinator,/engine\.independentClockFaulted\(\)/); assert.match(host,/serviceDirectRestore\(\)/); - assert.match(host,/SMART_CLOCK_RECOVERY/); - assert.match(host,/!engine_\.independentClockActive\(\)/); + assert.match(coordinator,/SMART_CLOCK_RECOVERY/); + assert.match(host,/NativeDirectRestoreCoordinator::service/); + assert.match(coordinator,/!engine\.independentClockActive\(\)/); assert.match(host,/editing VST3 graph/); assert.match(host,/emitFxAddResult \(requestId, name, error, 0\)/); }); @@ -79,18 +81,18 @@ test('silent JUCE callback watchdog routes through control timer and DIRECT fail assert.match(watchdog,/renderCount != renderCount_/); assert.match(device,/streamWatchdog_\.observe/); assert.match(engine,/pollIndependentClockHealth/); - assert.match(host,/engine_\.pollIndependentClockHealth \(now\)/); + assert.match(coordinator,/engine\.pollIndependentClockHealth\(nowMs\)/); assert.match(engine,/bridgeWatchdogFault/); assert.match(engine,/bridgeCaptureSilentMs/); assert.match(engine,/bridgeRenderSilentMs/); }); test('watchdog fault cause precedes bounded diagnostics JSON on both recovery outcomes',()=>{ - assert.match(host,/const auto watchdogSummary =/); - assert.match(host,/getProperty \("bridgeWatchdogFault", "NONE"\)/); - assert.match(host,/getProperty \("bridgeCaptureSilentMs", 0\)/); - assert.match(host,/getProperty \("bridgeRenderSilentMs", 0\)/); - assert.equal((host.match(/watchdogSummary \+ " \| " \+ faultStatus/g)||[]).length,2); + assert.match(coordinator,/const auto watchdogSummary=/); + assert.match(coordinator,/getProperty\("bridgeWatchdogFault","NONE"\)/); + assert.match(coordinator,/getProperty\("bridgeCaptureSilentMs",0\)/); + assert.match(coordinator,/getProperty\("bridgeRenderSilentMs",0\)/); + assert.equal((coordinator.match(/watchdogSummary\s*\+\s*" \| "\s*\+\s*faultStatus/g)||[]).length,2); }); test('sustained live FIFO loss feeds bounded DIRECT recovery without audio-callback policy work',()=>{ @@ -104,9 +106,9 @@ test('sustained live FIFO loss feeds bounded DIRECT recovery without audio-callb assert.match(device,/pcm\.overflowFrames/); assert.match(engine,/bridgeContinuityFault/); assert.match(engine,/bridgeContinuityLossBp/); - assert.match(host,/getProperty \("bridgeContinuityFault", "NONE"\)/); - assert.match(host,/getProperty \("bridgeContinuityLossBp", 0\)/); - assert.match(host,/engine_\.pollIndependentClockHealth \(now\)/); + assert.match(coordinator,/getProperty\("bridgeContinuityFault","NONE"\)/); + assert.match(coordinator,/getProperty\("bridgeContinuityLossBp",0\)/); + assert.match(coordinator,/engine\.pollIndependentClockHealth\(nowMs\)/); }); test('dual-WASAPI opening is guarded by unique native endpoint IDs without GUI or RT work',()=>{ diff --git a/tests/native_direct_restore_coordinator_test.cpp b/tests/native_direct_restore_coordinator_test.cpp new file mode 100644 index 000000000..e547ac335 --- /dev/null +++ b/tests/native_direct_restore_coordinator_test.cpp @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#ifdef NDEBUG +#undef NDEBUG +#endif +#include +#include +#include +#include "../src/NativeDirectRestoreCoordinator.h" + +using Coordinator=armix::device::NativeDirectRestoreCoordinator; +using Gate=armix::device::DirectRestoreBackoff; +struct Engine { + bool active=false,fault=false,pending=false,muted=false; + juce::String result; + int polls=0,attempts=0; + void pollIndependentClockHealth(std::uint64_t) {++polls;} + bool independentClockFaulted() const {return fault;} + bool independentClockActive() const {return active;} + bool hasPendingDirectRestore() const {return pending;} + bool isOutputMuted() const {return muted;} + juce::var status() const { + auto state=juce::var(new juce::DynamicObject()); + auto* obj=state.getDynamicObject(); + obj->setProperty("bridgeNegotiationFault","ASIO_CLOCK"); + obj->setProperty("bridgeWatchdogFault","CAPTURE_STALLED"); + obj->setProperty("bridgeCaptureSilentMs",234); + obj->setProperty("bridgeRenderSilentMs",12); + obj->setProperty("bridgeContinuityFault","NONE"); + obj->setProperty("bridgeContinuityLossBp",2); + obj->setProperty("bridgeContinuityBadWindows",1); + return state; + } + juce::String setIndependentClockEnabled(bool enable) { + assert(!enable); + ++attempts; + if(result.isEmpty()) { + fault=false;pending=false;active=false; + } + return result; + } +}; +int main() { + Gate gate; + Engine engine; + juce::String lastError; + bool dirty=false; + std::vector> events; + std::vector notices; + auto record=[&](const juce::String& key,const juce::String& value) { + events.emplace_back(key,value); + }; + auto notify=[&](const juce::String& message,bool isError) { + assert(isError);notices.push_back(message); + }; + auto tick=[&](std::uint64_t now) { + Coordinator::service(gate,lastError,dirty,now,engine,record,notify); + }; + tick(100); + assert(engine.polls==1 && engine.attempts==0); + engine.active=true;engine.fault=true;engine.result="driver cannot reopen"; + tick(200); // immediate first attempt, never in callback + assert(engine.attempts==1 && !dirty); + assert(lastError=="driver cannot reopen"); + assert(gate.snapshot().state==Gate::State::waiting); + assert(events.size()==2 && notices.size()==1); + assert(events[1].second.startsWith("RESTORE_FAIL 1/5")); + assert(events[1].second.contains("captureSilentMs=234")); + juce::var state(new juce::DynamicObject()); + Coordinator::annotate(state,gate,lastError,true,300); + assert(state.getProperty("directRestoreState","")=="WAITING"); + assert(static_cast(state.getProperty("directRestoreNextMs",0))==150); + assert(state.getProperty("directRestorePending",false)==true); + tick(449); + assert(engine.attempts==1); + engine.result.clear(); + engine.muted=true; + tick(450); + assert(engine.attempts==2 && dirty); + assert(lastError.isEmpty() && gate.snapshot().state==Gate::State::idle); + assert(notices.back().contains("remains muted")); + assert(events.back().second.contains("DIRECT_RESTORED after 2 attempt")); + assert(engine.active==false); + tick(451); + assert(engine.attempts==2); // recovery cannot flap/auto-arm + + // Lost DIRECT while no independent endpoint is active uses the same + // bounded policy; exhausted retries never reopen a faulting bridge. + engine.pending=true; + engine.result="physical output missing"; + gate.cancel(); + dirty=false;notices.clear(); + tick(1000); + assert(engine.attempts==3 && notices.size()==1); + tick(1249);assert(engine.attempts==3); + tick(1250);assert(engine.attempts==4); + tick(2250);assert(engine.attempts==5); + tick(5250);assert(engine.attempts==6); + tick(13250);assert(engine.attempts==7); + assert(gate.snapshot().state==Gate::State::exhausted); + assert(notices.back().contains("exhausted")); + tick(30000);assert(engine.attempts==7 && !dirty); + juce::var exhausted(new juce::DynamicObject()); + Coordinator::annotate(exhausted,gate,lastError,true,30000); + assert(exhausted.getProperty("directRestoreState","")=="EXHAUSTED"); + assert(static_cast(exhausted.getProperty("directRestoreAttempts",0))==5); + gate.cancel();engine.pending=false; + tick(31000);assert(engine.attempts==7); + juce::var invalid;Coordinator::annotate(invalid,gate,lastError,false,0); + assert(!invalid.isObject()); +}