Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
feat(meeting): add capture and attribution foundations
argus-vault2834abe99a68
diff --git a/Fluid.xcodeproj/project.pbxproj b/Fluid.xcodeproj/project.pbxprojindex e005dc93..f42e56f5 100644--- a/Fluid.xcodeproj/project.pbxproj+++ b/Fluid.xcodeproj/project.pbxproj@@ -56,6 +56,15 @@803000000000000000000002 /* MediaPlaybackServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803000000000000000000001 /* MediaPlaybackServiceTests.swift */; };0B05F11E000000000000A002 /* SupportedFileExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B05F11E000000000000A001 /* SupportedFileExtensionsTests.swift */; };CD0E00000000000000000002 /* MeetingOverlayModelsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD0E00000000000000000001 /* MeetingOverlayModelsTests.swift */; };+ CD0F00000000000000000002 /* MeetingProcessingConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD0F00000000000000000001 /* MeetingProcessingConfigurationTests.swift */; };+ CD100000000000000000000002 /* MeetingProviderOptionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD100000000000000000000001 /* MeetingProviderOptionsTests.swift */; };+ CD110000000000000000000002 /* MeetingASRPreparationOwnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD110000000000000000000001 /* MeetingASRPreparationOwnerTests.swift */; };+ D2A000000000000000000002 /* MeetingReferenceSynchronizerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2A000000000000000000001 /* MeetingReferenceSynchronizerTests.swift */; };+ CD110000000000000000000003 /* MeetingASRPreparationOwnerTests.swift in Sources (ASR baseline) */ = {isa = PBXBuildFile; fileRef = CD110000000000000000000001 /* MeetingASRPreparationOwnerTests.swift */; };+ CD120000000000000000000002 /* MeetingReferenceAttributionContractsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD120000000000000000000001 /* MeetingReferenceAttributionContractsTests.swift */; };+ E5EC000000000000000000B2 /* MeetingVoiceProcessingProbeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EC000000000000000000B1 /* MeetingVoiceProcessingProbeTests.swift */; };+ E5EC000000000000000000C2 /* MeetingSCKPairedDiagnosticsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EC000000000000000000C1 /* MeetingSCKPairedDiagnosticsTests.swift */; };+ E5EC000000000000000000D2 /* MeetingSCKPairedHardwareTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EC000000000000000000D1 /* MeetingSCKPairedHardwareTests.swift */; };803000000000000000000003 /* MediaRemoteAdapter in Frameworks */ = {isa = PBXBuildFile; productRef = 803000000000000000000004 /* MediaRemoteAdapter */; };B51800000000000000000002 /* SpokenSendTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B51800000000000000000001 /* SpokenSendTests.swift */; };B51900000000000000000002 /* PrivateAIProviderPromptFormatTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B51900000000000000000001 /* PrivateAIProviderPromptFormatTests.swift */; };@@ -65,6 +74,15 @@7CDB0A302F3C4D5600FB7CAD /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7CDB0A2C2F3C4D5600FB7CAD /* XCTest.framework */; };7CE006BD2E80EBE600DDCCD6 /* AppUpdater in Frameworks */ = {isa = PBXBuildFile; productRef = 7CE006BC2E80EBE600DDCCD6 /* AppUpdater */; };86CAA2D4EF18433096185602 /* LLMClientRequestBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 343B29013F4441D6A797D12D /* LLMClientRequestBodyTests.swift */; };+ A5B00000000000000000000B /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B00000000000000000000C /* main.swift */; };+ A5B000000000000000000024 /* MeetingReferenceSynchronizerReplayCLI.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B000000000000000000025 /* MeetingReferenceSynchronizerReplayCLI.swift */; };+ A5B000000000000000000013 /* FluidAudio in Frameworks */ = {isa = PBXBuildFile; productRef = 7C078DA42E3B386A00FB7CAC /* FluidAudio */; };+ A5B000000000000000000014 /* DynamicNotchKit in Frameworks */ = {isa = PBXBuildFile; productRef = 7C3697882ED70F9C005874CE /* DynamicNotchKit */; };+ A5B000000000000000000015 /* MediaRemoteAdapter in Frameworks */ = {isa = PBXBuildFile; productRef = 7C5AF14A2F15041600DE21B0 /* MediaRemoteAdapter */; };+ A5B000000000000000000016 /* TranscribeCpp in Frameworks */ = {isa = PBXBuildFile; productRef = 7C9A71012F58B00000FB7CAF /* TranscribeCpp */; };+ A5B000000000000000000017 /* AppUpdater in Frameworks */ = {isa = PBXBuildFile; productRef = 7CE006BC2E80EBE600DDCCD6 /* AppUpdater */; };+ A5B000000000000000000018 /* MediaRemoteAdapter in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 7C5AF14A2F15041600DE21B0 /* MediaRemoteAdapter */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };+ A5B000000000000000000023 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7CDB0A2C2F3C4D5600FB7CAD /* XCTest.framework */; };/* End PBXBuildFile section *//* Begin PBXContainerItemProxy section */@@ -75,6 +93,13 @@remoteGlobalIDString = 7C078D8E2E3B339200FB7CAC;remoteInfo = fluid;};+ A5B000000000000000000021 /* PBXContainerItemProxy */ = {+ isa = PBXContainerItemProxy;+ containerPortal = 7C078D872E3B339200FB7CAC /* Project object */;+ proxyType = 1;+ remoteGlobalIDString = A5B000000000000000000001;+ remoteInfo = FluidASRBaselineHost;+ };/* End PBXContainerItemProxy section *//* Begin PBXCopyFilesBuildPhase section */@@ -89,6 +114,17 @@name = "Embed Frameworks";runOnlyForDeploymentPostprocessing = 0;};+ A5B000000000000000000008 /* Embed Frameworks */ = {+ isa = PBXCopyFilesBuildPhase;+ buildActionMask = 2147483647;+ dstPath = "";+ dstSubfolderSpec = 10;+ files = (+ A5B000000000000000000018 /* MediaRemoteAdapter in Embed Frameworks */,+ );+ name = "Embed Frameworks";+ runOnlyForDeploymentPostprocessing = 0;+ };/* End PBXCopyFilesBuildPhase section *//* Begin PBXFileReference section */@@ -141,17 +177,50 @@803000000000000000000001 /* MediaPlaybackServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaPlaybackServiceTests.swift; sourceTree = "<group>"; };0B05F11E000000000000A001 /* SupportedFileExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportedFileExtensionsTests.swift; sourceTree = "<group>"; };CD0E00000000000000000001 /* MeetingOverlayModelsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingOverlayModelsTests.swift; sourceTree = "<group>"; };+ CD0F00000000000000000001 /* MeetingProcessingConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingProcessingConfigurationTests.swift; sourceTree = "<group>"; };+ CD100000000000000000000001 /* MeetingProviderOptionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingProviderOptionsTests.swift; sourceTree = "<group>"; };+ CD110000000000000000000001 /* MeetingASRPreparationOwnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingASRPreparationOwnerTests.swift; sourceTree = "<group>"; };+ D2A000000000000000000001 /* MeetingReferenceSynchronizerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingReferenceSynchronizerTests.swift; sourceTree = "<group>"; };+ CD120000000000000000000001 /* MeetingReferenceAttributionContractsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingReferenceAttributionContractsTests.swift; sourceTree = "<group>"; };+ E5EC000000000000000000B1 /* MeetingVoiceProcessingProbeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingVoiceProcessingProbeTests.swift; sourceTree = "<group>"; };+ E5EC000000000000000000C1 /* MeetingSCKPairedDiagnosticsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingSCKPairedDiagnosticsTests.swift; sourceTree = "<group>"; };+ E5EC000000000000000000D1 /* MeetingSCKPairedHardwareTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingSCKPairedHardwareTests.swift; sourceTree = "<group>"; };7CDB0A2A2F3C4D5600FB7CAD /* AudioFixtureLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioFixtureLoader.swift; sourceTree = "<group>"; };7CDB0A2B2F3C4D5600FB7CAD /* dictation_fixture.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = dictation_fixture.wav; sourceTree = "<group>"; };7CDB0A2C2F3C4D5600FB7CAD /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };+ A5B000000000000000000005 /* FluidASRBaselineHost.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FluidASRBaselineHost.app; sourceTree = BUILT_PRODUCTS_DIR; };+ A5B00000000000000000000C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };+ A5B000000000000000000025 /* MeetingReferenceSynchronizerReplayCLI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingReferenceSynchronizerReplayCLI.swift; sourceTree = "<group>"; };+ A5B00000000000000000000D /* FluidASRBaselineHost.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = FluidASRBaselineHost.entitlements; sourceTree = "<group>"; };+ A5B00000000000000000000E /* stage-inputs.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = "stage-inputs.sh"; sourceTree = "<group>"; };+ A5B00000000000000000000F /* placeholder.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = placeholder.json; sourceTree = "<group>"; };+ A5B00000000000000000001D /* FluidASRBaselineTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FluidASRBaselineTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };/* End PBXFileReference section */+/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */+ A5B000000000000000000012 /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = {+ isa = PBXFileSystemSynchronizedBuildFileExceptionSet;+ membershipExceptions = (+ fluidApp.swift,+ );+ target = A5B000000000000000000001 /* FluidASRBaselineHost */;+ };+/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */+/* Begin PBXFileSystemSynchronizedRootGroup section */7C078D912E3B339200FB7CAC /* Sources/Fluid */ = {isa = PBXFileSystemSynchronizedRootGroup;+ exceptions = (+ A5B000000000000000000012 /* PBXFileSystemSynchronizedBuildFileExceptionSet */,+ );path = Sources/Fluid;sourceTree = "<group>";};+ A5B000000000000000000020 /* FluidASRBaselineTests */ = {+ isa = PBXFileSystemSynchronizedRootGroup;+ path = FluidASRBaselineTests;+ sourceTree = "<group>";+ };/* End PBXFileSystemSynchronizedRootGroup section *//* Begin PBXFrameworksBuildPhase section */@@ -176,6 +245,26 @@);runOnlyForDeploymentPostprocessing = 0;};+ A5B000000000000000000007 /* Frameworks */ = {+ isa = PBXFrameworksBuildPhase;+ buildActionMask = 2147483647;+ files = (+ A5B000000000000000000014 /* DynamicNotchKit in Frameworks */,+ A5B000000000000000000015 /* MediaRemoteAdapter in Frameworks */,+ A5B000000000000000000016 /* TranscribeCpp in Frameworks */,+ A5B000000000000000000017 /* AppUpdater in Frameworks */,+ A5B000000000000000000013 /* FluidAudio in Frameworks */,+ );+ runOnlyForDeploymentPostprocessing = 0;+ };+ A5B00000000000000000001F /* Frameworks */ = {+ isa = PBXFrameworksBuildPhase;+ buildActionMask = 2147483647;+ files = (+ A5B000000000000000000023 /* XCTest.framework in Frameworks */,+ );+ runOnlyForDeploymentPostprocessing = 0;+ };/* End PBXFrameworksBuildPhase section *//* Begin PBXGroup section */@@ -193,6 +282,8 @@children = (7C078D8F2E3B339200FB7CAC /* FluidVoice Debug.app */,7CDB0A202F3C4D5600FB7CAD /* FluidDictationIntegrationTests.xctest */,+ A5B000000000000000000005 /* FluidASRBaselineHost.app */,+ A5B00000000000000000001D /* FluidASRBaselineTests.xctest */,);name = Products;sourceTree = "<group>";@@ -201,6 +292,8 @@isa = PBXGroup;children = (7CDB0A252F3C4D5600FB7CAD /* FluidDictationIntegrationTests */,+ A5B000000000000000000010 /* FluidASRBaselineHost */,+ A5B000000000000000000020 /* FluidASRBaselineTests */,);path = Tests;sourceTree = "<group>";@@ -257,6 +350,14 @@CACEC0010000000000000001 /* KeychainServiceCacheTests.swift */,0B05F11E000000000000A001 /* SupportedFileExtensionsTests.swift */,CD0E00000000000000000001 /* MeetingOverlayModelsTests.swift */,+ CD0F00000000000000000001 /* MeetingProcessingConfigurationTests.swift */,+ CD100000000000000000000001 /* MeetingProviderOptionsTests.swift */,+ CD110000000000000000000001 /* MeetingASRPreparationOwnerTests.swift */,+ D2A000000000000000000001 /* MeetingReferenceSynchronizerTests.swift */,+ CD120000000000000000000001 /* MeetingReferenceAttributionContractsTests.swift */,+ E5EC000000000000000000B1 /* MeetingVoiceProcessingProbeTests.swift */,+ E5EC000000000000000000C1 /* MeetingSCKPairedDiagnosticsTests.swift */,+ E5EC000000000000000000D1 /* MeetingSCKPairedHardwareTests.swift */,);path = FluidDictationIntegrationTests;sourceTree = "<group>";@@ -285,6 +386,26 @@name = Frameworks;sourceTree = "<group>";};+ A5B000000000000000000010 /* FluidASRBaselineHost */ = {+ isa = PBXGroup;+ children = (+ A5B00000000000000000000C /* main.swift */,+ A5B000000000000000000025 /* MeetingReferenceSynchronizerReplayCLI.swift */,+ A5B00000000000000000000D /* FluidASRBaselineHost.entitlements */,+ A5B00000000000000000000E /* stage-inputs.sh */,+ A5B000000000000000000011 /* Inputs */,+ );+ path = FluidASRBaselineHost;+ sourceTree = "<group>";+ };+ A5B000000000000000000011 /* Inputs */ = {+ isa = PBXGroup;+ children = (+ A5B00000000000000000000F /* placeholder.json */,+ );+ path = Inputs;+ sourceTree = "<group>";+ };/* End PBXGroup section *//* Begin PBXNativeTarget section */@@ -339,6 +460,57 @@productReference = 7CDB0A202F3C4D5600FB7CAD /* FluidDictationIntegrationTests.xctest */;productType = "com.apple.product-type.bundle.unit-test";};+ A5B000000000000000000001 /* FluidASRBaselineHost */ = {+ isa = PBXNativeTarget;+ buildConfigurationList = A5B000000000000000000002 /* Build configuration list for PBXNativeTarget "FluidASRBaselineHost" */;+ buildPhases = (+ A5B000000000000000000006 /* Sources */,+ A5B000000000000000000007 /* Frameworks */,+ A5B000000000000000000008 /* Embed Frameworks */,+ A5B000000000000000000009 /* Resources */,+ A5B00000000000000000000A /* Stage ASR Baseline Inputs */,+ );+ buildRules = (+ );+ dependencies = (+ );+ fileSystemSynchronizedGroups = (+ 7C078D912E3B339200FB7CAC /* Sources/Fluid */,+ );+ name = FluidASRBaselineHost;+ packageProductDependencies = (+ 7C078DA42E3B386A00FB7CAC /* FluidAudio */,+ 7C3697882ED70F9C005874CE /* DynamicNotchKit */,+ 7CE006BC2E80EBE600DDCCD6 /* AppUpdater */,+ 7C9A71012F58B00000FB7CAF /* TranscribeCpp */,+ 7C5AF14A2F15041600DE21B0 /* MediaRemoteAdapter */,+ );+ productName = FluidASRBaselineHost;+ productReference = A5B000000000000000000005 /* FluidASRBaselineHost.app */;+ productType = "com.apple.product-type.application";+ };+ A5B000000000000000000019 /* FluidASRBaselineTests */ = {+ isa = PBXNativeTarget;+ buildConfigurationList = A5B00000000000000000001A /* Build configuration list for PBXNativeTarget "FluidASRBaselineTests" */;+ buildPhases = (+ A5B00000000000000000001E /* Sources */,+ A5B00000000000000000001F /* Frameworks */,+ );+ buildRules = (+ );+ dependencies = (+ A5B000000000000000000022 /* PBXTargetDependency */,+ );+ fileSystemSynchronizedGroups = (+ A5B000000000000000000020 /* FluidASRBaselineTests */,+ );+ name = FluidASRBaselineTests;+ packageProductDependencies = (+ );+ productName = FluidASRBaselineTests;+ productReference = A5B00000000000000000001D /* FluidASRBaselineTests.xctest */;+ productType = "com.apple.product-type.bundle.unit-test";+ };/* End PBXNativeTarget section *//* Begin PBXProject section */@@ -355,6 +527,12 @@7CDB0A1F2F3C4D5600FB7CAD = {CreatedOnToolsVersion = 26.0;};+ A5B000000000000000000001 = {+ CreatedOnToolsVersion = 26.0;+ };+ A5B000000000000000000019 = {+ CreatedOnToolsVersion = 26.0;+ };};};buildConfigurationList = 7C078D8A2E3B339200FB7CAC /* Build configuration list for PBXProject "Fluid" */;@@ -381,6 +559,8 @@targets = (7C078D8E2E3B339200FB7CAC /* fluid */,7CDB0A1F2F3C4D5600FB7CAD /* FluidDictationIntegrationTests */,+ A5B000000000000000000001 /* FluidASRBaselineHost */,+ A5B000000000000000000019 /* FluidASRBaselineTests */,);};/* End PBXProject section */@@ -401,8 +581,39 @@);runOnlyForDeploymentPostprocessing = 0;};+ A5B000000000000000000009 /* Resources */ = {+ isa = PBXResourcesBuildPhase;+ buildActionMask = 2147483647;+ files = (+ );+ runOnlyForDeploymentPostprocessing = 0;+ };/* End PBXResourcesBuildPhase section */+/* Begin PBXShellScriptBuildPhase section */+ A5B00000000000000000000A /* Stage ASR Baseline Inputs */ = {+ isa = PBXShellScriptBuildPhase;+ buildActionMask = 2147483647;+ files = (+ );+ inputFileListPaths = (+ );+ inputPaths = (+ "$(FLUID_ASR_BASELINE_INPUTS)",+ "$(SRCROOT)/Tests/FluidASRBaselineHost/stage-inputs.sh",+ );+ name = "Stage ASR Baseline Inputs";+ outputFileListPaths = (+ );+ outputPaths = (+ "$(TARGET_BUILD_DIR)/$(UNLOCALIZED_RESOURCES_FOLDER_PATH)/ASRBaselineInputs",+ );+ runOnlyForDeploymentPostprocessing = 0;+ shellPath = /bin/sh;+ shellScript = "\"$SRCROOT/Tests/FluidASRBaselineHost/stage-inputs.sh\"\n";+ };+/* End PBXShellScriptBuildPhase section */+/* Begin PBXSourcesBuildPhase section */7C078D8B2E3B339200FB7CAC /* Sources */ = {isa = PBXSourcesBuildPhase;@@ -463,6 +674,31 @@CACEC0010000000000000002 /* KeychainServiceCacheTests.swift in Sources */,0B05F11E000000000000A002 /* SupportedFileExtensionsTests.swift in Sources */,CD0E00000000000000000002 /* MeetingOverlayModelsTests.swift in Sources */,+ CD0F00000000000000000002 /* MeetingProcessingConfigurationTests.swift in Sources */,+ CD100000000000000000000002 /* MeetingProviderOptionsTests.swift in Sources */,+ CD110000000000000000000002 /* MeetingASRPreparationOwnerTests.swift in Sources */,+ D2A000000000000000000002 /* MeetingReferenceSynchronizerTests.swift in Sources */,+ CD120000000000000000000002 /* MeetingReferenceAttributionContractsTests.swift in Sources */,+ E5EC000000000000000000B2 /* MeetingVoiceProcessingProbeTests.swift in Sources */,+ E5EC000000000000000000C2 /* MeetingSCKPairedDiagnosticsTests.swift in Sources */,+ E5EC000000000000000000D2 /* MeetingSCKPairedHardwareTests.swift in Sources */,+ );+ runOnlyForDeploymentPostprocessing = 0;+ };+ A5B000000000000000000006 /* Sources */ = {+ isa = PBXSourcesBuildPhase;+ buildActionMask = 2147483647;+ files = (+ A5B00000000000000000000B /* main.swift in Sources */,+ A5B000000000000000000024 /* MeetingReferenceSynchronizerReplayCLI.swift in Sources */,+ );+ runOnlyForDeploymentPostprocessing = 0;+ };+ A5B00000000000000000001E /* Sources */ = {+ isa = PBXSourcesBuildPhase;+ buildActionMask = 2147483647;+ files = (+ CD110000000000000000000003 /* MeetingASRPreparationOwnerTests.swift in Sources (ASR baseline) */,);runOnlyForDeploymentPostprocessing = 0;};@@ -474,6 +710,11 @@target = 7C078D8E2E3B339200FB7CAC /* fluid */;targetProxy = 7CDB0A322F3C4D5600FB7CAD /* PBXContainerItemProxy */;};+ A5B000000000000000000022 /* PBXTargetDependency */ = {+ isa = PBXTargetDependency;+ target = A5B000000000000000000001 /* FluidASRBaselineHost */;+ targetProxy = A5B000000000000000000021 /* PBXContainerItemProxy */;+ };/* End PBXTargetDependency section *//* Begin XCBuildConfiguration section */@@ -761,6 +1002,138 @@};name = Release;};+ A5B000000000000000000003 /* Debug */ = {+ isa = XCBuildConfiguration;+ buildSettings = {+ ARCHS = "$(ARCHS_STANDARD)";+ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;+ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;+ CODE_SIGN_ENTITLEMENTS = Tests/FluidASRBaselineHost/FluidASRBaselineHost.entitlements;+ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";+ CODE_SIGN_STYLE = Automatic;+ COMBINE_HIDPI_IMAGES = YES;+ CURRENT_PROJECT_VERSION = 1;+ DEVELOPMENT_TEAM = V4J43B279J;+ ENABLE_APP_SANDBOX = YES;+ ENABLE_CODE_COVERAGE = NO;+ ENABLE_HARDENED_RUNTIME = YES;+ FLUID_ASR_BASELINE_INPUTS = "$(SRCROOT)/Tests/FluidASRBaselineHost/Inputs";+ GENERATE_INFOPLIST_FILE = YES;+ INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";+ INFOPLIST_KEY_NSHumanReadableCopyright = "";+ LD_RUNPATH_SEARCH_PATHS = (+ "$(inherited)",+ "@executable_path/../Frameworks",+ );+ MACOSX_DEPLOYMENT_TARGET = 15.0;+ MARKETING_VERSION = 1.0;+ PRODUCT_BUNDLE_IDENTIFIER = com.FluidApp.ASRBaselineHost;+ PRODUCT_MODULE_NAME = FluidASRBaselineHost;+ PRODUCT_NAME = FluidASRBaselineHost;+ PROVISIONING_PROFILE_SPECIFIER = "";+ STRING_CATALOG_GENERATE_SYMBOLS = YES;+ SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) ARCH_ARM64 FLUID_ASR_BASELINE";+ SWIFT_APPROACHABLE_CONCURRENCY = YES;+ SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor;+ SWIFT_EMIT_LOC_STRINGS = YES;+ SWIFT_OBJC_BRIDGING_HEADER = "Sources/Fluid/Fluid-Bridging-Header.h";+ SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;+ SWIFT_VERSION = 5.0;+ VALID_ARCHS = "$(ARCHS_STANDARD)";+ };+ name = Debug;+ };+ A5B000000000000000000004 /* Release */ = {+ isa = XCBuildConfiguration;+ buildSettings = {+ ARCHS = "$(ARCHS_STANDARD)";+ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;+ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;+ CODE_SIGN_ENTITLEMENTS = Tests/FluidASRBaselineHost/FluidASRBaselineHost.entitlements;+ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";+ CODE_SIGN_STYLE = Automatic;+ COMBINE_HIDPI_IMAGES = YES;+ CURRENT_PROJECT_VERSION = 1;+ DEVELOPMENT_TEAM = V4J43B279J;+ ENABLE_APP_SANDBOX = YES;+ ENABLE_CODE_COVERAGE = NO;+ ENABLE_HARDENED_RUNTIME = YES;+ FLUID_ASR_BASELINE_INPUTS = "$(SRCROOT)/Tests/FluidASRBaselineHost/Inputs";+ GENERATE_INFOPLIST_FILE = YES;+ INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";+ INFOPLIST_KEY_NSHumanReadableCopyright = "";+ LD_RUNPATH_SEARCH_PATHS = (+ "$(inherited)",+ "@executable_path/../Frameworks",+ );+ MACOSX_DEPLOYMENT_TARGET = 15.0;+ MARKETING_VERSION = 1.0;+ PRODUCT_BUNDLE_IDENTIFIER = com.FluidApp.ASRBaselineHost;+ PRODUCT_MODULE_NAME = FluidASRBaselineHost;+ PRODUCT_NAME = FluidASRBaselineHost;+ PROVISIONING_PROFILE_SPECIFIER = "";+ STRING_CATALOG_GENERATE_SYMBOLS = YES;+ SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) ARCH_ARM64 FLUID_ASR_BASELINE";+ SWIFT_APPROACHABLE_CONCURRENCY = YES;+ SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor;+ SWIFT_EMIT_LOC_STRINGS = YES;+ SWIFT_OBJC_BRIDGING_HEADER = "Sources/Fluid/Fluid-Bridging-Header.h";+ SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;+ SWIFT_VERSION = 5.0;+ VALID_ARCHS = "$(ARCHS_STANDARD)";+ };+ name = Release;+ };+ A5B00000000000000000001B /* Debug */ = {+ isa = XCBuildConfiguration;+ buildSettings = {+ ALWAYS_SEARCH_USER_PATHS = NO;+ ARCHS = "$(ARCHS_STANDARD)";+ BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/FluidASRBaselineHost.app/Contents/MacOS/FluidASRBaselineHost";+ CLANG_ENABLE_MODULES = YES;+ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";+ CODE_SIGN_STYLE = Automatic;+ DEVELOPMENT_TEAM = V4J43B279J;+ GENERATE_INFOPLIST_FILE = YES;+ INFOPLIST_KEY_CFBundleDisplayName = FluidASRBaselineTests;+ LD_RUNPATH_SEARCH_PATHS = (+ "$(inherited)",+ "@executable_path/../Frameworks",+ );+ MACOSX_DEPLOYMENT_TARGET = 15.0;+ PRODUCT_BUNDLE_IDENTIFIER = com.FluidApp.FluidASRBaselineTests;+ PRODUCT_NAME = FluidASRBaselineTests;+ SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited) FLUID_ASR_BASELINE_TESTS";+ SWIFT_VERSION = 5.0;+ TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FluidASRBaselineHost.app/Contents/MacOS/FluidASRBaselineHost";+ };+ name = Debug;+ };+ A5B00000000000000000001C /* Release */ = {+ isa = XCBuildConfiguration;+ buildSettings = {+ ALWAYS_SEARCH_USER_PATHS = NO;+ ARCHS = "$(ARCHS_STANDARD)";+ BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/FluidASRBaselineHost.app/Contents/MacOS/FluidASRBaselineHost";+ CLANG_ENABLE_MODULES = YES;+ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";+ CODE_SIGN_STYLE = Automatic;+ DEVELOPMENT_TEAM = V4J43B279J;+ GENERATE_INFOPLIST_FILE = YES;+ INFOPLIST_KEY_CFBundleDisplayName = FluidASRBaselineTests;+ LD_RUNPATH_SEARCH_PATHS = (+ "$(inherited)",+ "@executable_path/../Frameworks",+ );+ MACOSX_DEPLOYMENT_TARGET = 15.0;+ PRODUCT_BUNDLE_IDENTIFIER = com.FluidApp.FluidASRBaselineTests;+ PRODUCT_NAME = FluidASRBaselineTests;+ SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) FLUID_ASR_BASELINE_TESTS";+ SWIFT_VERSION = 5.0;+ TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FluidASRBaselineHost.app/Contents/MacOS/FluidASRBaselineHost";+ };+ name = Release;+ };/* End XCBuildConfiguration section *//* Begin XCConfigurationList section */@@ -791,6 +1164,24 @@defaultConfigurationIsVisible = 0;defaultConfigurationName = Release;};+ A5B000000000000000000002 /* Build configuration list for PBXNativeTarget "FluidASRBaselineHost" */ = {+ isa = XCConfigurationList;+ buildConfigurations = (+ A5B000000000000000000003 /* Debug */,+ A5B000000000000000000004 /* Release */,+ );+ defaultConfigurationIsVisible = 0;+ defaultConfigurationName = Release;+ };+ A5B00000000000000000001A /* Build configuration list for PBXNativeTarget "FluidASRBaselineTests" */ = {+ isa = XCConfigurationList;+ buildConfigurations = (+ A5B00000000000000000001B /* Debug */,+ A5B00000000000000000001C /* Release */,+ );+ defaultConfigurationIsVisible = 0;+ defaultConfigurationName = Release;+ };/* End XCConfigurationList section *//* Begin XCRemoteSwiftPackageReference section */diff --git a/Fluid.xcodeproj/xcshareddata/xcschemes/FluidASRBaseline.xcscheme b/Fluid.xcodeproj/xcshareddata/xcschemes/FluidASRBaseline.xcschemenew file mode 100644index 00000000..65f53c37--- /dev/null+++ b/Fluid.xcodeproj/xcshareddata/xcschemes/FluidASRBaseline.xcscheme@@ -0,0 +1,91 @@+<?xml version="1.0" encoding="UTF-8"?>+<Scheme+ LastUpgradeVersion = "2600"+ version = "1.7">+ <BuildAction+ parallelizeBuildables = "YES"+ buildImplicitDependencies = "YES"+ buildArchitectures = "Automatic">+ <BuildActionEntries>+ <BuildActionEntry+ buildForTesting = "YES"+ buildForRunning = "NO"+ buildForProfiling = "NO"+ buildForArchiving = "NO"+ buildForAnalyzing = "NO">+ <BuildableReference+ BuildableIdentifier = "primary"+ BlueprintIdentifier = "A5B000000000000000000001"+ BuildableName = "FluidASRBaselineHost.app"+ BlueprintName = "FluidASRBaselineHost"+ ReferencedContainer = "container:Fluid.xcodeproj">+ </BuildableReference>+ </BuildActionEntry>+ <BuildActionEntry+ buildForTesting = "YES"+ buildForRunning = "NO"+ buildForProfiling = "NO"+ buildForArchiving = "NO"+ buildForAnalyzing = "NO">+ <BuildableReference+ BuildableIdentifier = "primary"+ BlueprintIdentifier = "A5B000000000000000000019"+ BuildableName = "FluidASRBaselineTests.xctest"+ BlueprintName = "FluidASRBaselineTests"+ ReferencedContainer = "container:Fluid.xcodeproj">+ </BuildableReference>+ </BuildActionEntry>+ </BuildActionEntries>+ </BuildAction>+ <TestAction+ buildConfiguration = "Debug"+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"+ shouldUseLaunchSchemeArgsEnv = "NO"+ shouldAutocreateTestPlan = "YES">+ <Testables>+ <TestableReference+ skipped = "NO">+ <BuildableReference+ BuildableIdentifier = "primary"+ BlueprintIdentifier = "A5B000000000000000000019"+ BuildableName = "FluidASRBaselineTests.xctest"+ BlueprintName = "FluidASRBaselineTests"+ ReferencedContainer = "container:Fluid.xcodeproj">+ </BuildableReference>+ </TestableReference>+ </Testables>+ <EnvironmentVariables>+ <EnvironmentVariable+ key = "FLUID_ASR_RUN_BENCHMARK"+ value = "0"+ isEnabled = "YES">+ </EnvironmentVariable>+ </EnvironmentVariables>+ </TestAction>+ <LaunchAction+ buildConfiguration = "Debug"+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"+ launchStyle = "0"+ useCustomWorkingDirectory = "NO"+ ignoresPersistentStateOnLaunch = "NO"+ debugDocumentVersioning = "YES"+ debugServiceExtension = "internal"+ allowLocationSimulation = "NO">+ </LaunchAction>+ <ProfileAction+ buildConfiguration = "Release"+ shouldUseLaunchSchemeArgsEnv = "YES"+ savedToolIdentifier = ""+ useCustomWorkingDirectory = "NO"+ debugDocumentVersioning = "YES">+ </ProfileAction>+ <AnalyzeAction+ buildConfiguration = "Debug">+ </AnalyzeAction>+ <ArchiveAction+ buildConfiguration = "Release"+ revealArchiveInOrganizer = "YES">+ </ArchiveAction>+</Scheme>diff --git a/Fluid.xcodeproj/xcshareddata/xcschemes/FluidVPIOProbe.xcscheme b/Fluid.xcodeproj/xcshareddata/xcschemes/FluidVPIOProbe.xcschemenew file mode 100644index 00000000..ccebfaa9--- /dev/null+++ b/Fluid.xcodeproj/xcshareddata/xcschemes/FluidVPIOProbe.xcscheme@@ -0,0 +1,93 @@+<?xml version="1.0" encoding="UTF-8"?>+<Scheme+ LastUpgradeVersion = "2600"+ version = "1.7">+ <BuildAction+ parallelizeBuildables = "YES"+ buildImplicitDependencies = "YES"+ buildArchitectures = "Automatic">+ <BuildActionEntries>+ <BuildActionEntry+ buildForTesting = "YES"+ buildForRunning = "YES"+ buildForProfiling = "NO"+ buildForArchiving = "NO"+ buildForAnalyzing = "NO">+ <BuildableReference+ BuildableIdentifier = "primary"+ BlueprintIdentifier = "7C078D8E2E3B339200FB7CAC"+ BuildableName = "FluidVoice.app"+ BlueprintName = "fluid"+ ReferencedContainer = "container:Fluid.xcodeproj">+ </BuildableReference>+ </BuildActionEntry>+ </BuildActionEntries>+ </BuildAction>+ <TestAction+ buildConfiguration = "Debug"+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"+ shouldUseLaunchSchemeArgsEnv = "YES"+ shouldAutocreateTestPlan = "YES">+ <Testables>+ <TestableReference+ skipped = "NO">+ <BuildableReference+ BuildableIdentifier = "primary"+ BlueprintIdentifier = "7CDB0A1F2F3C4D5600FB7CAD"+ BuildableName = "FluidDictationIntegrationTests.xctest"+ BlueprintName = "FluidDictationIntegrationTests"+ ReferencedContainer = "container:Fluid.xcodeproj">+ </BuildableReference>+ </TestableReference>+ </Testables>+ </TestAction>+ <LaunchAction+ buildConfiguration = "Debug"+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"+ launchStyle = "0"+ useCustomWorkingDirectory = "NO"+ ignoresPersistentStateOnLaunch = "YES"+ debugDocumentVersioning = "YES"+ debugServiceExtension = "internal"+ allowLocationSimulation = "NO">+ <EnvironmentVariables>+ <EnvironmentVariable+ key = "FLUIDVOICE_VPIO_ACOUSTIC"+ value = "1"+ isEnabled = "YES">+ </EnvironmentVariable>+ <EnvironmentVariable+ key = "FLUIDVOICE_MIC_PHASE1"+ value = "0.05"+ isEnabled = "YES">+ </EnvironmentVariable>+ </EnvironmentVariables>+ <BuildableProductRunnable+ runnableDebuggingMode = "0">+ <BuildableReference+ BuildableIdentifier = "primary"+ BlueprintIdentifier = "7C078D8E2E3B339200FB7CAC"+ BuildableName = "FluidVoice.app"+ BlueprintName = "fluid"+ ReferencedContainer = "container:Fluid.xcodeproj">+ </BuildableReference>+ </BuildableProductRunnable>+ </LaunchAction>+ <ProfileAction+ buildConfiguration = "Debug"+ shouldUseLaunchSchemeArgsEnv = "YES"+ savedToolIdentifier = ""+ useCustomWorkingDirectory = "NO"+ debugDocumentVersioning = "YES">+ </ProfileAction>+ <AnalyzeAction+ buildConfiguration = "Debug">+ </AnalyzeAction>+ <ArchiveAction+ buildConfiguration = "Debug"+ customArchiveName = "FluidVPIOProbe"+ revealArchiveInOrganizer = "NO">+ </ArchiveAction>+</Scheme>diff --git a/MEETING_AUDIO_PHASE0_RUNBOOK.md b/MEETING_AUDIO_PHASE0_RUNBOOK.mdindex 6220cfad..71190a06 100644--- a/MEETING_AUDIO_PHASE0_RUNBOOK.md+++ b/MEETING_AUDIO_PHASE0_RUNBOOK.md@@ -1,6 +1,6 @@# Meeting Audio Freeze — Phase 0 Evidence Runbook-This runbook classifies the meeting-start freeze before any production behavior is changed. Diagnostics are DEBUG-only and opt-in. They record numeric Core Audio topology events; they do not record audio, device names, meeting titles, window titles, URLs, or transcripts.+This runbook classifies meeting audio freezes before any production behavior is changed, including the post-meeting stop/processing transition. Diagnostics are DEBUG-only and opt-in. They record numeric Core Audio topology events; they do not record audio, device names, meeting titles, window titles, URLs, or transcripts.## Acceptance rule@@ -41,6 +41,18 @@ tools/capture_audio_topology_stall.sh /tmp/fluidvoice-audio-evidenceThe script reads the newest marker, validates its PID, and immediately runs Apple's `sample` tool. If `sample` cannot attach, run it from an administrator terminal. For an unresolved kernel/daemon interaction, capture a sysdiagnose immediately afterward with Control-Option-Command-Shift-Period and note its timestamp. Do not attach sysdiagnose archives to the repository; they may contain unrelated private system data.+## Meeting-end reproduction++1. Launch the installed signed Debug app with diagnostics enabled as above.+2. Start a meeting capture, keep both application and microphone audio active, then stop it normally.+3. Leave the app untouched while capture finalization and transcription finish. If the UI stalls, do not force quit.+4. Immediately run `tools/capture_audio_topology_stall.sh /tmp/fluidvoice-audio-evidence`.+5. Preserve the trace, stall snapshot, process sample, selected input/output transport classes, meeting mode, OS/build identifiers, and exact commit.++Interpret the final unmatched pair using its complete key: event category, owner, object ID, selector, scope, element, phase, and generation. `is_main_thread` reports the actual execution thread for each event; `queue_role` describes the intended delivery/control role. A callback begin without its matching end identifies an in-flight delivery. A main-hop begin without its end identifies work scheduled by a delivery that entered the main actor. Runtime, writer, output-listener release, AV capture stop, ScreenCaptureKit stop, processing, and activity-lease phases locate the owning meeting-end boundary. Always correlate this with the process sample; the trace alone does not prove the other side of a circular wait.++The ring retains 8,192 events. If a device storm overwrites an unmatched begin before the watchdog snapshot, classification may be incomplete; record the observed sequence span and do not interpret absence of an open pair as proof that no operation was blocked.+## Controlled condition matrixKeep every uncontrolled variable fixed. Record successes, freezes, and failures separately.diff --git a/MEETING_DIARIZATION_V2_IMPLEMENTATION_PLAN.md b/MEETING_DIARIZATION_V2_IMPLEMENTATION_PLAN.mdnew file mode 100644index 00000000..5fe328d5--- /dev/null+++ b/MEETING_DIARIZATION_V2_IMPLEMENTATION_PLAN.md@@ -0,0 +1,510 @@+# Meeting Diarization V2 — Evidence-Driven Implementation Plan++Status: **Revised after adversarial review**+Scope: post-meeting remote/application-audio diarization+Constraints: local-first, bounded resources, recoverable processing, no microphone or dictation regression++## 1. Outcome++Improve speaker separation and identity consistency, beginning with the measured failure where an+8:14 five-speaker recording produced four valid remote identities and absorbed a brief recurring+speaker into a dominant speaker.++The implementation is deliberately conditional. The same visible failure can be caused by five+different stages, and each requires a different fix. No long-window or clustering change begins until+a diagnostic pass identifies the stage that lost the speaker.++## 2. Current pipeline++1. Capture writes application and microphone audio into independent 60-second durability chunks.+2. Each application chunk is diarized independently with FluidAudio's offline pipeline.+3. FluidVoice reduces every local chunk label to one centroid and average quality.+4. `MeetingGlobalSpeakerStitcher` merges those label-level observations across chunks.+5. When the ASR provider supports word timings, each chunk is transcribed once and words are assigned+ to diarizer turns in chunk-local time.+6. Final meeting segments retain paragraph/turn time ranges and text, not individual word timings.++This architecture makes one claim certain: once a brief participant has been absorbed into a local+chunk label, the final stitcher cannot reconstruct that participant from a mixed centroid. It does+**not** establish where the absorption happened.++## 3. Verified dependency behavior++These facts were verified against the currently checked-out FluidAudio fork, but must be re-verified+after the actual build revision is pinned:++- The offline diarizer uses 10-second segmentation windows with a 0.2 step ratio, AHC initialization,+ PLDA/VBx refinement, and final timeline reconstruction.+- The URL path converts audio into a memory-mapped disk-backed source.+- Whole-file processing retains segmentation outputs, embeddings, clustering inputs, and+ reconstruction arrays until completion. Audio is disk-backed, but peak memory still grows with+ meeting duration.+- Final embedding assignments are recomputed using nearest-centroid similarity after VBx-derived+ centroids. The plan must not describe the final timeline as a direct HMM-state output.+- Public `TimedSpeakerSegment.embedding` is the resolved cluster centroid repeated on every segment.+ It is not an independent turn embedding.+- Raw timed embeddings and their cluster assignments exist internally. A debug export path can expose+ them for local diagnosis, but it is not an acceptable production API.+- Default reconstruction uses exclusive segments and can trim overlapping speech. Short remnants can+ disappear during sanitization.+- The embedding extractor can still produce evidence for short speech; the configured minimum affects+ clean-mask selection and reconstruction behavior, so the exact failing stage must be inspected.+- `StreamingAudioSampleSource` and `process(audioSource:audioLoadingSeconds:)` are public in the+ inspected checkout and may support a composed source without creating one monolithic scratch WAV.++### Dependency reproducibility blocker++`Package.swift` tracks the `altic-dev/FluidAudio` fork's moving `main` branch. The revision recorded in+`Package.resolved` and the current local checkout do not match, and the resolved object is absent from+the shallow local checkout. Before collecting a baseline:++1. determine the exact revision used by the installed/test build;+2. pin the package to an immutable reviewed revision or release;+3. clean-resolve and verify that the checkout, resolved file, build metadata, and evaluation report all+ carry the same SHA;+4. re-check every API and behavioral claim in this document against that SHA.++Upstream documentation is research input, not proof of behavior in the pinned fork.++## 4. Competing root-cause hypotheses++| ID | Failure stage | What it would look like | Correct intervention |+|---|---|---|---|+| H1 | Local clustering under-merges | Distinct raw embeddings exist, but a 60-second pass assigns the brief speaker to a dominant cluster | Calibrate clustering or evaluate longer/global context |+| H2 | Overlap/reconstruction deletion | The brief speaker exists before reconstruction but is trimmed/dropped from the final timeline | Fix overlap/exclusive/min-duration policy |+| H3 | Embedding contamination | The brief speech's embedding is already acoustically close/mixed with the dominant speaker | Improve segmentation/masking/embedding model; longer context and downstream prototypes will not help |+| H4 | Cross-chunk stitcher merge | Local chunks preserve the fifth identity, but FluidVoice merges it globally | Fix the application-only stitcher; do not add whole-era processing |+| H5 | Word attribution error | Diarizer turns are correct, but ASR words cross the wrong boundary or are dropped | Fix time mapping and word assignment |++Whole-era diarization is the likely fix only for H1. It may conceal H4, and it does not inherently fix+H2, H3, or H5.++## 5. Design principles++1. **Diagnose before selecting architecture.**+2. **Capture chunks remain durability units, never presumed speaker boundaries.**+3. **Application and microphone behavior are isolated in code and tests.**+4. **A false merge is more destructive than a provisional split**, but weak noise must not become a+ confident person.+5. **Acoustic evidence is authoritative.** Language context may flag ambiguity but cannot identify a+ voice by itself.+6. **No repeated centroid may be presented as turn-level evidence.**+7. **No exact speaker count in product behavior.** Exact counts are evaluation-only oracles.+8. **Embeddings remain session-local and absent from exports, analytics, logs, and production debug+ artifacts.**+9. **Word attribution is part of every diarization intervention**, not a later cleanup phase.+10. **No threshold changes without a development/held-out evaluation split.**++## 6. Phase 0 — Reproducible evaluation foundation++### 6.1 Pin inputs++- Pin the exact FluidAudio fork revision.+- Record the diarization model bundle fingerprint and relevant configuration.+- Record ASR provider/model/version, normalization configuration, pipeline version, and fixture hash.+- Add the diarization fingerprint to checkpoint compatibility checks.++### 6.2 Corpus++Create a local, gitignored evaluation manifest supporting:++- audio/session path;+- RTTM or equivalent reference speaker timeline;+- reference transcript with word times where available;+- expected speaker count;+- recurring-speaker relationships;+- overlap regions;+- capture discontinuities and known missing intervals;+- fixture tags such as short speaker, similar voices, overlap, boundary, and long meeting.++The initial development set must contain more than the measured fixture:++1. five speakers with two brief appearances by one person;+2. two clean speakers;+3. one speaker;+4. similar-sounding speakers;+5. short interjection during overlap;+6. a speaker first appearing at a 60-second boundary;+7. paired application/microphone audio with leakage and genuine local speech;+8. at least one 60–90-minute session for resource measurement.++Create a held-out set before tuning begins. User recordings remain local and gitignored. Commit only+synthetic fixtures or audio with explicit repository rights.++### 6.3 Metrics++Primary product metrics:++- speaker merge count;+- speaker split/fragmentation count;+- recovery recall by total speaking-duration buckets (`<1 s`, `1–3 s`, `3–10 s`, `>10 s`);+- recurring-speaker consistency across distant appearances;+- speaker-attributed word error / cpWER;+- word conservation: assigned once, explicitly ambiguous, or explicitly unassigned;+- microphone You/Unknown/echo classification on paired-track fixtures.++Secondary diagnostic metrics:++- DER with miss, false alarm, and confusion components;+- JER;+- speaker-count error;+- purity and coverage;+- plain WER;+- turn-boundary error.++Report both:++- a comparable diagnostic score with 250 ms collar and overlap ignored;+- a strict product score with zero collar and overlap scored, only when the hypothesis format actually+ represents overlap.++Do not use speaker count, DER, or cpWER alone as a gate. A system can recover the requested count by+splitting the wrong person, while a brief merged speaker contributes little duration to DER.++### 6.4 Harness++- Port or wrap the pinned FluidAudio fork's DER/JER metric implementation into a repository-owned+ evaluation tool.+- Extend the existing opt-in retained-meeting replay without ever rewriting source sessions.+- Emit per-stage reports and hypotheses into a user-selected or protected local temporary directory.+- Add deterministic synthetic unit tests for clustering logic independent of real audio.++Exit gate:++- one command produces repeatable baseline metrics and resource reports;+- the five-speaker failure reproduces;+- the held-out manifest already exists;+- dictation and paired-track microphone baselines are recorded;+- no source/model/revision mismatch remains.++## 7. Phase 0.5 — Decisive failure-stage diagnosis++This phase is mandatory and may stop the rest of the plan.++### 7.1 Diagnostic runs++For the failing fixture, run:++1. each original 60-second application chunk independently;+2. the contiguous application capture era as one diagnostic input;+3. the same inputs with `withSpeakers(exactly: 5)` as an evaluation-only oracle;+4. raw timed-embedding export in a debug/test-only build;+5. pre- and post-reconstruction timeline inspection;+6. current word-to-turn assignment inspection.++The debug embedding export must:++- be impossible to enable in production builds;+- write to a newly created `0700` directory and `0600` files;+- contain no participant names;+- be deleted on success/failure/cancellation and by next-launch stale-artifact cleanup;+- never be committed, uploaded, logged, or included in crash reports.++### 7.2 Evidence captured++- raw embedding count, window time, mask quality, and assignment;+- distance of the brief speaker's observations to every resolved centroid;+- cluster membership before and after final nearest-centroid reassignment;+- speaker timeline immediately before exclusive-overlap handling and sanitization;+- duration removed by overlap trimming/minimum-duration filtering;+- per-chunk local labels and the FluidVoice global stitch merge graph;+- diarizer turns versus chunk-local words, including every dropped/ambiguous word;+- NaN/invalid embeddings and any silent cluster-zero assignment;+- result under the exact-five oracle.++### 7.3 Decision tree++```text+Did the brief speech produce distinct raw embeddings?+ no -> H3: segmentation/masking/embedding investigation+ yes -> Did it form a distinct local cluster?+ no -> H1: clustering/global-context experiment+ yes -> Did reconstruction preserve the turn?+ no -> H2: overlap/reconstruction fix+ yes -> Did FluidVoice preserve it globally?+ no -> H4: application stitcher fix+ yes -> H5: word attribution fix+```++Exit gate:++- one hypothesis is supported by stage-local evidence on the target fixture;+- the same diagnostic is run on at least two additional fixtures;+- the selected implementation branch states why the other branches are not being implemented.++## 8. Conditional Branch A — Local clustering/global-context fix (H1)++Choose this branch only if distinct raw evidence exists but local clustering merges it.++### A1. Whole-era experiment++Evaluate one offline pass over each contiguous application-audio era. This is an experiment, not the+default architecture.++Implementation requirements:++- introduce a meeting-only analysis timeline that maps **analysis time ↔ meeting presentation time ↔+ source chunk-local time**;+- extend/reuse existing epoch-boundary logic rather than creating a conflicting `MeetingCaptureEra`+ concept;+- prefer a composed `StreamingAudioSampleSource` over a monolithic decoded scratch file;+- place decoded samples by source PTS, not by assuming encoded AAC frame count equals presentation+ duration;+- measure AAC priming/padding per chunk and prove it cannot accumulate timeline drift;+- derive gaps from adjacent PTS directly, including gaps below the writer's 0.5-second discontinuity+ threshold;+- split around corrupt/unreadable chunks, preserve `skippedChunkIDs`, and process healthy spans;+- never create turns spanning missing or unfinalized audio;+- keep post-meeting-only behavior: live captions remain You/Them and final speaker labels are published+ only after processing completes.++Word attribution is part of A1:++- map global turns back into each chunk's local clock;+- run the current chunk ASR once with word timings;+- assign words to globally resolved turns;+- assert that each word is assigned exactly once or explicitly reported ambiguous/unassigned;+- preserve current zero nearest-turn tolerance on microphone audio.++### A2. Resource experiment before defaulting++Measure 8-minute, 30-minute, 60-minute, and 120-minute sessions:++- peak RSS and memory growth per audio hour;+- segmentation/embedding/clustering/reconstruction allocations;+- AHC and total wall time/real-time factor;+- temporary bytes and stale-artifact behavior;+- cancellation latency and dictation preemption latency;+- crash/retry cost, since the current checkpoint is after the complete application pass.++Whole-era processing may become the default only if resource growth and cancellation are acceptable on+the minimum supported Apple Silicon machine. Disk-backed audio alone is not sufficient evidence.++### A3. Bounded strategy if whole-era fails++If whole-era resource gates fail, design bounded sequential super-windows as the primary architecture,+not as an afterthought:++- select window/overlap duration from measurements rather than assuming 5 minutes/30 seconds;+- process one window at a time;+- reconcile the overlap with acoustic evidence and cannot-link constraints;+- checkpoint after each accepted window;+- freeze resolved IDs deterministically;+- prove no boundary-only label changes or duplicated transcript text;+- demonstrate peak memory independent of meeting duration.++Branch A acceptance:++- brief-speaker recovery improves in its speaking-duration bucket;+- no dominant speaker is merged or fragmented;+- paired-track You/Unknown/echo results are unchanged;+- chunk-local word attribution is correct across every chunk/era boundary;+- corrupt-chunk recovery and retry remain functional;+- dictation can preempt or cancel within a measured product budget;+- resource gates pass on long sessions and minimum hardware.++## 9. Conditional Branch B — Overlap/reconstruction fix (H2)++Choose this branch if clustering finds the speaker but reconstruction deletes or trims the turn.++Tasks:++- measure the effect of `exclusiveSegments`, overlap masks, minimum output duration, onset/offset, and+ gap merging independently;+- preserve overlap when the source model supports multiple active speakers;+- do not lower a minimum globally without measuring false-alarm speakers;+- separate minimum duration for embedding evidence from minimum duration for displayed transcript+ turns;+- preserve a short high-confidence turn even when it cannot yet be promoted to a durable identity;+- attribute overlapping words explicitly rather than forcing all text to one survivor.++Acceptance:++- short-overlap speaker recall improves;+- false-alarm speaker duration and nuisance speaker count do not materially increase;+- strict overlap-scored DER and speaker-attributed WER improve on held-out overlap fixtures.++## 10. Conditional Branch C — Embedding/segmentation fix (H3)++Choose this branch if raw evidence is already contaminated or indistinguishable.++Tasks:++- inspect speech masks and overlap exclusion around the brief turn;+- test shorter/multi-scale embedding windows while retaining longer context for voice quality;+- benchmark an alternative local segmentation/embedding model using the same clustering backend;+- preserve multiple independent observations only after proving they contain distinct information;+- reject long-context clustering and multi-prototype work until acoustic separability improves.++Acceptance:++- same/different-speaker distance distributions separate on the development set and hold out;+- the brief speaker becomes separable before final clustering;+- no material degradation on noisy, similar-voice, and single-speaker fixtures;+- local model, license, memory, and latency remain compatible with the product.++## 11. Conditional Branch D — Application-only global resolver fix (H4)++Choose this branch if local diarization preserves the speaker and FluidVoice merges it later.++Create a new application-only resolver. Do not modify the stitcher instance/type used by microphone+You/Unknown classification in the first change.++Resolver rules:++- consume independent raw observations, never repeated cluster centroids described as turns;+- retain up to four deterministic quality/duration-ranked medoids per speaker;+- different simultaneous local labels are cannot-link;+- labels already distinct in one local diarizer result are cannot-link;+- merge only below a calibrated distance with a first-versus-second ambiguity margin;+- enforce maximum pairwise diameter to prevent centroid chaining;+- weak observations may attach to strong cores but cannot move their prototypes;+- multiple consistent weak appearances may form one provisional speaker;+- unresolved evidence remains explicit rather than being forced onto the nearest dominant speaker;+- output is deterministic under input, chunk, and retry ordering.++Production use requires a stable FluidAudio API exposing raw timed embeddings, assignments, and defined+quality signals. The debug export is diagnostic-only. Decide during Phase 0 whether this is an upstream+change merged into the fork or a maintained pinned-fork patch with contract tests.++Acceptance:++- the fifth identity is preserved without splitting a dominant speaker;+- merge/split metrics improve on held-out sessions;+- a single noisy observation does not create a confident speaker;+- two consistent brief appearances can recover one identity;+- microphone stitcher output is byte-equivalent to baseline fixtures;+- paired-track You/Unknown/echo behavior is unchanged.++## 12. Conditional Branch E — Word-level attribution fix (H5)++Choose this branch if diarizer turns are correct but text is assigned incorrectly.++Tasks:++- preserve processing-time word IDs, text, start/end, and assignment outcome;+- make all three time domains explicit;+- define half-open boundary behavior and overlap ties;+- replace silent word drops with explicit counts and reasons;+- make the allowed dropped-word target relative to measured baseline before setting a stricter target;+- compute speaker-attributed WER/cpWER;+- add boundary tests for encoded-chunk priming, gaps, and diarizer turns spanning a capture boundary;+- decide separately whether product persistence uses `session.json` or a versioned local sidecar.++Acceptance:++- every ASR word is emitted once or explicitly ambiguous/unassigned;+- unexplained word loss improves from baseline;+- speaker-attributed WER improves without material plain-WER regression;+- retry is deterministic for an identical model/configuration fingerprint.++## 13. Shared reliability, privacy, and rollout requirements++### Checkpoints and cancellation++- Bump both processing-pipeline and checkpoint schema versions when compatibility changes.+- Record diarizer model/configuration/dependency fingerprint in checkpoint compatibility.+- Document that the current checkpoint is after the application pass; do not claim finer resume points+ until implemented.+- Add explicit cancellation opportunities around long diarizer phases. Measure worst-case cancellation+ latency because the dependency may not check cancellation internally.+- A failed V2 experiment may fall back only through a typed, recorded technical failure. Never choose a+ result merely because it reports a preferred speaker count.++### Privacy++- Keep remote and microphone tracks separate.+- No cross-meeting profiles or real-name inference.+- No embeddings in session JSON, exports, logs, analytics, crash reports, or UI state.+- Production scratch files, if unavoidable, use protected directories/files, explicit deletion on all+ exits, and stale-artifact cleanup after crashes.+- Debug raw-embedding export remains test-build-only and requires explicit local invocation.+- Any resumable embedding checkpoint requires a separate biometric-adjacent storage review and+ authenticated encryption.++### Dictation and microphone isolation++- Initial implementation changes only post-meeting application-audio processing.+- Do not change capture writers, microphone diarization, echo detection, near-field gating, You+ election, dictation providers, or dictation audio routing.+- Prove isolation with tests rather than relying on type boundaries.+- Run meeting echo, You/Unknown, multi-device dictation, route recovery, deadlock/freeze, and complete+ dictation integration suites for every selected branch.+- Measure ANE/model contention and dictation preemption with a long application diarization pass.++### Rollout++- Put the selected V2 branch behind an internal flag.+- Replay V1 and V2 locally against the same immutable fixture manifest.+- Default new meetings to V2 only after development and held-out gates pass.+- Preserve V1 retry compatibility until retained-meeting replay and dogfooding show no regression.++## 14. Expected file areas++- `Package.swift` and `Package.resolved` for an intentional immutable dependency pin+- `Sources/Fluid/Services/SpeakerDiarizationService.swift`+- `Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift`+- new application-only diagnostic/resolver/time-mapping files under+ `Sources/Fluid/Services/Meeting/`+- `Sources/Fluid/Services/Meeting/MeetingModels.swift` for fingerprint/checkpoint metadata only when+ required+- `Tests/FluidDictationIntegrationTests/SpeakerTurnMergingTests.swift`+- `Tests/FluidDictationIntegrationTests/MeetingExistingAudioReplayTests.swift`+- new local evaluation tooling with no committed user recordings or embeddings+- the pinned FluidAudio fork only if a reviewed public raw-observation API is required++## 15. Explicit non-goals++- cross-meeting voice profiles;+- real participant naming from voice;+- full live diarization during recording;+- changing capture chunk duration;+- changing the dictation ASR path;+- exact speaker-count configuration in normal product behavior;+- language-model-only speaker assignment;+- building all conditional branches.++## 16. Adversarial-review disposition++Three independent reviews agreed on the main correction:++- Kimi K3: **approve with revisions**; whole-era remains plausible after dependency pinning and a+ stage-local diagnostic.+- Grok 4.6: **no-go beyond diagnosis**; the original linear plan overfit one fixture and used gameable+ aggregate gates.+- Claude Opus: **do not start the original Phase 1**; the dependency's debug export can identify the+ actual failure stage first, and whole-era memory/timeline assumptions were incomplete.++The revised plan adopts the shared blocking feedback:++1. pin and verify the actual dependency;+2. run a per-stage diagnostic decision tree;+3. make whole-era processing conditional rather than presumed;+4. treat overlap, raw-embedding availability, time mapping, word attribution, corrupt chunks,+ cancellation, and long-session resources as first-class gates;+5. isolate any new application resolver from microphone behavior;+6. use merge/split and short-speaker recovery as primary metrics, with DER/JER as diagnostics.++## 17. Recommended next action++Implement only Phase 0 and Phase 0.5 first. The immediate deliverable is a local diagnostic report for+the failing five-speaker fixture showing exactly where the fifth speaker disappears.++That report—not architectural preference—selects Branch A, B, C, D, or E. This prevents a large,+technically elegant change from fixing the wrong stage.++## 18. Research sources++- FluidAudio documentation and source for the **pinned fork revision** (authoritative after pinning)+- Upstream FluidAudio offline diarization overview:+ https://github.com/FluidInference/FluidAudio/blob/main/Documentation/Diarization/GettingStarted.md+- VBx theory:+ https://arxiv.org/abs/2012.14952+- Multi-scale diarization decoder:+ https://arxiv.org/abs/2203.15974+- NVIDIA NeMo diarization model design:+ https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/asr/speaker_diarization/models.html+- Reproducible DER and diagnostic metrics:+ https://github.com/pyannote/pyannote-metrics-paper/blob/master/paper.tex+- Overlap-aware diarization fusion:+ https://arxiv.org/abs/2011.01997diff --git a/MEETING_MICROPHONE_ADMISSION_IMPLEMENTATION_PLAN.md b/MEETING_MICROPHONE_ADMISSION_IMPLEMENTATION_PLAN.mdnew file mode 100644index 00000000..e8813010--- /dev/null+++ b/MEETING_MICROPHONE_ADMISSION_IMPLEMENTATION_PLAN.md@@ -0,0 +1,262 @@+# Microphone speech admission and speaker-profile integrity++Date: 2026-09-08 (local)+Status: revision 7 — Stage A and offline B1/B2 plus eight-recording B3 development evaluation implemented. Five controlled recordings were added on 2026-09-09. Scoped Kimi reviews completed and findings addressed/dispositioned; no production approval is claimed. Current evidence still rejects threshold selection. No transcript/profile enforcement, runtime integration or app reinstall is included.++## Objective and relationship to existing work++Prevent playback remnants and unsupported ASR text from becoming accepted microphone speech or microphone speaker profiles, while preserving genuine quiet speech, short replies, overlap, and multiple people sharing a microphone.++This is a scoped execution plan for the microphone-admission P2/P3 work in `MEETING_RELIABILITY_IMPLEMENTATION_PLAN.md`. It does not replace that plan's corpus or release gates. `MEETING_RELIABILITY_P0_BASELINE.md` remains the baseline reference. Live-caption watchdog recovery is a separate workstream; this plan neither fixes nor gates it on microphone filtering.++Non-goals: lower microphone gain by default; add a hard universal volume cutoff; enforce one microphone speaker; restore a “You” election; change dictation or application-track diarization; replace ASR/AEC models; upload recordings or embeddings; automatically rewrite historical meetings.++## 1. Evidence driving the change++| Local fixture | User ground truth | Current output | Evaluation role |+| --- | --- | --- | --- |+| `3774ED4C…` (~100 seconds) | User did not speak | Seven microphone segments, 143 whitespace-delimited words, two represented microphone speaker identities, no echo flags | Development negative regression |+| `88E3BD44…` (~261 seconds) | User spoke only the supplied script near the end | Eight earlier microphone segments / 167 words; scripted speech retained around 208–250 seconds | Development negative intervals plus positive speech |+| `3B9AC1A8…` (~133 seconds) | User-reported playback-only | Prior false microphone text; live-caption blackout also investigated | Additional development regression; separate live issue |++The latest microphone recording has RMS approximately −56.4 dBFS, versus −27.8 dBFS for digital playback. In 32 of 48 two-second windows, the strongest waveform match occurs 55–70 ms after playback; median absolute correlation is only 0.108, versus 0.045 for a mismatched-reference control. These measurements support residual playback leakage but do not establish microphone gain, acoustic source distance, or the origin of every sound. They are not production thresholds.++The source video's captions around 34:05–35:43 corroborate several garbled playback phrases in the microphone transcript. Caption comparison is supporting evidence, not independently annotated audio truth. Do not use source-caption wording as a runtime whitelist or infer speech identity from semantic similarity alone.++All three recordings have already informed diagnosis and belong in development, never held-out acceptance. No claim of quiet-speech recall or statistical reliability follows from these few fixtures.++### Local competitor implementation findings++These are static observations from unpacked client snapshots, not current-service documentation, a runtime activation audit, or evidence that either product passes our fixtures. No competitor code was executed or sent to an external reviewer.++| Snapshot and inspected files | Verified mechanism | Applicability and limits |+| --- | --- | --- |+| Wispr Flow 1.6.492: `/Users/shreeram/Projects/wispr/reverse-engineering/wispr-flow-v1.6.492/extracted/.webpack/main/index.js` | FFT-based microphone/system comparison; repeated lag-stability checks; attack/release hysteresis; timestamped exclusion spans and epochs; conference-room latch; final encoder receives microphone-zeroing spans while original mic PCM is retained | Strong motivation for a temporal duplicate detector. Feature-dependent activation is unverified. Does not prove double-talk safety or successful detection on our low-correlation residuals. |+| Granola 7.498.1: `/Users/shreeram/Projects/granola/Granola - AI Notepad-unpacked/Granola-app-source/dist-electron/audio_process/index.js` and `main/feature-flags.generated-DtBFb_Zf.js` | Native capture wrapper receives echo-cancellation/headphone and gain-compensation options; separate microphone/system buffers and optional capture timestamps | Native cancellation implementation is not exposed here. Bundled defaults are not live feature settings. Do not infer that a flag means effective cancellation. |+| Granola 7.498.1: same source root, `dist-electron/local_diarization_process/index.js` | Segmentation/clustering, adaptive gain, energy-based activity backend, and a minimum voiced-duration check in the fallback embedding path | Supplied embeddings bypass that duration check; the inspected Community-1 path supplies cluster centroids. This is not independent clean-speech validation for every identity. |++Wispr's inspected defaults include an 8 kHz comparison stream, 16,384-point FFT, five consistent lag observations within an eight-observation history, three attack updates and a two-second release hangover. Its lag sign convention must not be assumed to match our analysis. These are reference implementation facts, **not proposed FluidVoice constants**. Its detector has fail-open and processing-budget safeguards; microphone zeroing is refused when preservation/reference checks fail.++Granola's worker applies bounded adaptive gain before its energy-based activity check. That check is not a neural near-end speech detector. The supplied-centroid bypass reinforces our requirement to validate profile provenance on every path rather than rely on the presence of a VAD-named component.++Design decision: adopt the architectural idea of temporal playback-duplicate evidence, independently implemented and calibrated locally. Do not copy proprietary implementation bodies, transplant thresholds, introduce adaptive gain, or zero original microphone audio. Keep the stricter shared speech-admission/profile contract below.++## 2. Verified code gaps++- `MeetingEchoSignalScorer.verdict` returns `containsLocalSpeech` for a sufficiently long low-explanation run. This measures failure to explain the mic with playback, not positive near-end speech evidence. Its rescue precedes the echo coverage gate.+- `MeetingProcessingPipeline.StagedMicrophoneTurn.effectiveEcho` lets that verdict veto both text and signal echo suppression.+- `trustedMicrophoneObservationKeys` excludes only text-echo turns. Global microphone stitching can therefore consume observations whose speech validity was never established.+- An observation key can represent a chunk-level speaker cluster shared by several turns. Accepting one clean-looking sibling cannot make the shared centroid clean.+- At the pinned FluidAudio revision, offline returned segment embeddings repeat cluster centroids; they are not independent interval embeddings.+- `MeetingModels` persists optional `isLikelyEcho`, and `MeetingTranscriptExporter` consumes echo visibility. A boolean cannot represent uncertain speech separately from echo.+- `MeetingFinalProcessingConfiguration` explicitly describes itself as inert. Adding a field there is not runtime enforcement; configuration must actually be captured and applied by the processing run.++Reconfirm call sites, runtime entry points and active consumers before implementation. Legacy local-speaker-election helpers/comments exist; do not reactivate them or assume they describe the current global-stitch path.++## 3. Contracts to implement++### Evidence, decisions and identity are separate++Introduce proposed `MeetingMicrophoneEvidence` and `MeetingMicrophoneAdmissionPolicy` types. Keep policy evaluation pure and deterministic. Evidence is timestamped in the original capture timeline, with explicit mapping to any adjusted display timestamps.++Evidence fields:++- protected capture era, route discontinuity, and interval coverage;+- playback context: `scoreablePlayback`, `verifiedNoPlayback`, `playbackExpectedButUnscoreable`, or `unknown`;+- local speech-detector output, coverage, model/artifact/version and unknown reason;+- playback explanation, delay confidence/coverage and text similarity as separate signals;+- temporal duplicate state, supporting-window count, lag spread, evidence freshness, transition reason and capture epoch; these are correlated with playback explanation and must not be double-counted as independent confidence;+- ASR provider/model and whole-chunk/per-turn/alignment fallback provenance, preserving failed alignment evidence through retries;+- overlap and interval duration;+- embedding provenance/quality, represented separately from speech validity.++Mandatory identity scope: session ID, chunk ID, turn index/key (`microphone:<chunkID>:<index>`), explicit observation reference (`microphone:<chunkID>:<label>` when available, otherwise unavailable), and capture epoch. One observation can have many sibling turns; the evidence contract must preserve that relationship. Never fabricate an observation key for an unassigned fallback. Temporal evidence additionally carries matching identity and interval scope.++Keep `noDelayLock`, `referenceUnavailable`, `unscoredCoverage`, and `scoredInconclusive` distinct; their Stage A compatibility projection may collapse to legacy `.unknown`, but their detailed reason must remain available to shadow evaluation.++Distinguish measured zero from missing data. No invented ASR confidence when the provider exposes none. Do not multiply correlated scores as independent probabilities. An active application track alone does not prove audible speaker playback; a missing reference does not prove silence.++Proposed decision outcomes:++| Outcome | Default transcript/summary/export after enforcement | Speaker-profile contribution |+| --- | --- | --- |+| `acceptedSpeech` | Included; identity may remain unknown | Only independently supported, clean interval evidence |+| `likelyPlaybackDuplicate` | Excluded; available in local review | None |+| `uncertainCandidate` | Excluded from default text; explicitly reviewable | None |+| `noSupportedSpeech` | No accepted text; evidence retained under retention policy | None |++Unknown identity is not uncertain speech. Accepted speech without usable embeddings remains readable under an unknown-identity label. A missing detector/reference must not cause all legitimate microphone speech to disappear.++### Decision rules before calibration++1. Preserve existing capture-era safety checks; record exclusion reasons and retain original audio.+2. Low playback explanation alone never proves local speech and never automatically rescues a likely echo.+3. Reliable playback-duplicate evidence with no independent near-end support favors `likelyPlaybackDuplicate`.+4. Mixed playback/local speech requires interval-level handling. A short local reply must not rescue the entire contaminated turn, nor must playback evidence erase the reply. Split only where timing evidence supports it; otherwise preserve an explicit uncertain candidate.+5. Speech-detector activity alone cannot distinguish leaked speech from near-end speech. In playback conditions, require jointly evaluated evidence; unresolved conflict stays uncertain.+6. Where playback is absent or reference unavailable, evaluate speech support without requiring an impossible echo match. In-room and reference-failure cases need their own calibrated paths.+7. `noSupportedSpeech` requires adequate negative evidence, not merely a missing model, absent confidence, failed ASR, or low amplitude.++These define semantics, not final score thresholds. Feasibility of separating nonlinear residual speech from quiet local speech remains an experimental gate. If available evidence cannot do it reliably, do not enable automatic suppression.++### Temporal playback-duplicate detector++Add proposed `MeetingPlaybackDuplicateDetector` with bounded state, injectable clock/configuration and deterministic paired-audio input. Implement offline shadow evaluation first; reuse of the component in live capture is a separate integration decision.++- Align microphone and reference using original capture PTS, explicit resampling and valid-sample masks. Account for chunk boundaries, discontinuities, AAC padding and clock drift. Define and unit-test the lag sign. Search a bounded lag range instead of hard-coding the observed 60 ms.+- Compute windowed playback similarity and lag evidence, with checks for available reference, usable energy, periodic/ambiguous peaks and match quality relative to deliberately mismatched reference. Low energy or missing coverage means insufficient evidence, not silence or local speech.+- Proposed states: `insufficientEvidence`, `observing`, `duplicateCandidate`, and `duplicateSupported`. Require repeated support with stable delay before advancing; use separately calibrated entry/release criteria. State is evidence, not an immediate instruction to hide or zero audio.+- Track onset uncertainty and a bounded release hangover. Never extend suppression automatically across an unobserved gap or let a conference-room latch hide an entire microphone track. Short local interruptions and simultaneous playback/speech require the shared policy's mixed-interval handling.+- Reset evidence on route/era/sample-rate changes, gaps and reference loss; require fresh support after reacquisition. Do not freeze a stale duplicate decision indefinitely. Record detector-unavailable/over-budget reasons and disable this evidence contribution on repeated failures; do not manufacture `acceptedSpeech` or `noSupportedSpeech` as a fallback.+ Stage B1 binds capture epochs to the intervals between `MeetingProcessingPipeline.epochBoundaries` (either-track discontinuities), plus explicit route/reference discontinuities where no recorded boundary exists. Reuse the current ±`blockSeconds` discontinuity guard exclusions for initial evaluation; changing that guard policy requires separate tests/review. Invalidate prior evidence and delay state at the same boundaries, and keep samples masked by guards unavailable rather than treating the mask's zero values as silence.+- Preserve original audio. Candidate ranges are sidecar evidence mapped into transcript/profile decisions. Any future derived-ASR-input masking is a separate, reviewed experiment requiring positive-speech gates and recoverable originals; it is not in this implementation scope.+- Bound buffers, FFT/window work, pending jobs and evidence history. Run outside capture callbacks; emit no raw audio or transcript in routine metrics. Select latency/memory budgets from local profiling and freeze them before acceptance testing.++## 4. Incremental implementation sequence++### A — Evidence contract and semantics, behavior unchanged++Targets: `MeetingEchoSignalScorer.swift`, `MeetingProcessingPipeline.swift`; new evidence/policy files; `MeetingEchoSignalScorerTests.swift`, `MeetingEchoVetoTests.swift`.++1. Rename the in-memory verdict to `residualNotExplained` (or equivalently precise name), including thresholds/comments/log labels. This enum is not currently a persisted Codable field; do not invent an enum migration.+2. Preserve existing output behavior in a clearly named legacy mapping. Merely renaming must not silently remove the existing rescue and suppress real speech before validation.+ The scorer body is mechanically renamed, not reimplemented: preserve rescue-before-coverage ordering, short-NaN-gap bridging, and the conjunctive trivial-run exclusion. The legacy mapping simply wraps the unchanged `effectiveEcho` boolean expression; characterization tests pin all these edge cases.+3. Add typed evidence and reason codes; represent unmeasured RMS/coverage explicitly rather than interpreting sentinel zero as a measurement.+ Include the temporal detector's evidence contract and a fake implementation; do not load an audio model or enable suppression in Stage A.+4. Implement a pure shadow decision function with injected evidence. Mark uncalibrated outcomes experimental; no user-visible suppression.+ Stage A invocation is **tests only**. No production call site, runtime flag, persistence, logging, model loading or fake detector wiring. Its deliberately conservative output is `uncertainCandidate` with diagnostic reasons; it does not yet produce accepted/no-speech decisions or calibrated would-suppress counts. Stage B introduces an explicit default-off shadow collection configuration; Stage D alone adds enforcement.+5. Add adversarial unit cases: low explanation with noise, garbled playback, short real reply inside echo, missing reference, empty/NaN coverage, identical real repetition of playback, two near-end people and no playback.++Exit: each test explicitly distinguishes the unchanged legacy result from the experimental shadow reason. In particular, characterize the legacy rescue separately from `legacyRescueDisagreement`, and do not assert aspirational filtering as legacy behavior. Tests compare turns/profile eligibility before and after invoking the pure shadow function; static call-site inspection confirms no production invocation. New scorer labels never describe low explanation as confirmed speech. This is not an end-to-end runtime shadow-mode test; that belongs to Stage B.++### B — Local evidence collection and shadow evaluation++Targets: processing pipeline; proposed `MeetingSpeechActivityDetector` adapter, `MeetingPlaybackDuplicateDetector` and tests; effective processing configuration and tests.++1. Audit the pinned local VAD API, available artifacts, licensing, sample rate, padding/context and probability semantics. Prototype with local cached models; no automatic network fallback during evaluation. Add an injected detector for deterministic tests.+2. Capture one immutable effective runtime configuration per attempt. Wire detector, admission mode and versions into the actual processing path; integration-test enforcement rather than only fingerprint equality.+3. Measure playback-conditioned speech support over bounded windows with monotonic timing and bounded buffers. Preserve chunk-edge context without concurrent unbounded model instances. Check cancellation and specify model-load failure behavior.+4. Attach shadow decisions before global stitching and final assembly; retain all current display/profile behavior. Count would-accept/would-suppress/would-exclude-from-profile outcomes separately.+5. Aggregate reason counts and durations locally. Raw audio/text/embeddings are not new default logs. Store any detailed evaluation sidecars separately from original sessions, with bounded retention and exclusive output creation.+6. Run the existing negative and scripted fixtures locally. Add controlled positives/negatives and annotations before threshold selection. Include comparison with detector-disabled baseline.++Deliver Stage B as three reviewable increments:++- **B1: temporal duplicate detector.** Implement timestamp-aligned paired-window measurements and state transitions above; run local negative fixtures with sidecar outputs only. Report lag distributions, lock/hold/release errors and unknown coverage. A consistent lag in our exploratory analysis is a hypothesis to test, not a sufficient acceptance criterion.+- **B2: speech evidence and combined policy.** Add the validated activity adapter and evaluate mixed-speech conflicts. Keep temporal similarity and existing echo explanation separate but acknowledge their statistical dependence. Do not turn weak correlation into positive local-speech evidence.+- **B3: calibration and ablations.** Compare unchanged baseline, existing echo evidence alone, temporal evidence alone, speech activity alone, and the combined policy on identical development recordings. Report false accepted words/identities alongside lost quiet/short/double-talk speech. Freeze the selected policy before held-out evaluation.++Exit: measurable operating-point tradeoffs, complete unknown/fallback reporting, bounded resource use and no shadow-mode change to persisted transcript or profile contents. If local VAD plus current evidence is insufficient, report that result and review a new detector/dependency scope rather than selecting an unsafe threshold.++### C — Independent speaker evidence and shared admission integration++Targets: `trustedMicrophoneObservationKeys`, pending/staged turns, `MeetingGlobalSpeakerStitcher`, `MeetingSpeakerEmbeddingIndex`, final microphone assembly.++1. Carry one decision identifier plus audio interval and evidence/policy fingerprint through every path. Profile eligibility and final text consumption must refer to the same decision, not recompute conflicting rules.+2. Replace the “any non-text-echo turn admits its observation key” rule. Mixed accepted/rejected siblings must not bless a shared cluster centroid.+3. Evaluate compatible clean-interval extraction. The public legacy `EmbeddingExtractor` is not assumed compatible with the offline model/PLDA space. A dependency extension needs its own reviewed API and compatibility tests.+ Explicitly test supplied-embedding paths: a precomputed centroid must not bypass interval admission, minimum supported duration, overlap constraints or contamination checks. Minimum duration gates identity evidence only, not the visibility of otherwise accepted short replies.+4. Until compatible independent extraction is proven, exclude contaminated shared observations from profile updates while retaining accepted text with uncertain identity. Do not drop all accepted speech just because embeddings are unavailable.+5. Count trusted duration using the union of accepted, supported intervals, excluding overlap/duplication as required by the extractor. Do not count repeated centroids as independent observations.+6. Create speaker identities lazily from eligible evidence; remove unused candidate identities only from the new processing result, never destructively from historical sessions. Profile contributions must be traceable for session-local recomputation.++Exit: no rejected/uncertain interval can contribute through a sibling, retry, whole-chunk fallback or resumed run. Genuine multiple near-end speakers remain supported; the system does not pass merely by assigning everyone unknown.++### D — Versioned enforcement and consistent consumers++Targets: `MeetingModels.swift`, `MeetingProcessingPipeline.swift`, effective configuration/checkpoint logic, `MeetingTranscriptExporter.swift`, all discovered transcript UI/search/copy/summary consumers.++1. Add versioned persisted admission status, reason/provenance references and explicit legacy interpretation. Keep speech admission separate from `isLikelyEcho`; uncertain text must not be mislabeled proven echo.+2. Build a shared visibility/eligibility projection used by UI, copy/export, summaries and search. Review uncertain-candidate UX before enabling suppression. Summaries must not silently ingest hidden candidates.+3. Use explicit `off`, `shadow`, and `enforce` modes, default off/shadow until acceptance. Snapshot mode at attempt start, not midway through a run.+4. Bump processing version when final output semantics change (currently 10; choose the next unused value at implementation). Update mirrored defaults/tests and include actual policy/detector/artifact versions in attempt provenance.+5. Audit checkpoint contents and resume paths. Reuse only with matching effective fingerprints; invalidate/rebuild incompatible derived artifacts. Old sessions lacking new evidence remain legacy, not silently accepted under the new policy.+6. Reprocessing is explicit and transactional: prepare a separate result, preserve the original until success, handle cancellation/failure without partial replacement. No automatic historical migration.+7. Rollback must retain the meaning of previously persisted uncertain/rejected evidence; switching the flag off cannot reinterpret it as accepted. Verify compatibility before offering older binaries as rollback.++Exit: all consumers agree, compatibility/resume/cancellation tests pass, and held-out quality gates pass before enforcement is enabled.++## 5. Validation and release gates++Use the parent reliability plan's minimum corpus and statistical gates; this scope does not relax them:++- At least 12 independent negative sessions across three output configurations and two acoustic environments. Include real active-microphone playback, headphones, silence, noise, distorted playback and reference failure; synthetic zero/noise is a control only.+- Separate held-out quiet, brief-reply and double-talk positives: at least 30 annotated minutes across three independent recordings per condition, with at least 100 labeled short replies for that gate. Add multiple near-end speakers, in-room/no reference and real repeated playback phrases.+- Split by recording/device/speaker before tuning. Freeze policy structure, thresholds and splits before held-out evaluation. A failed held-out set becomes development data; obtain fresh held-out data for a subsequent quality claim.+- Zero accepted microphone words and zero represented false microphone identities on the curated playback-only regression set, with exposure and recording-level uncertainty reported. Extend negative screening to at least two hours per condition before release claims.+- At most one percentage point absolute increase in missed-speech duration and speaker-attributed WER per populated quiet/short/double-talk condition; report paired uncertainty and require upper bounds to meet the parent gates or mark evidence insufficient.+- No increase in false confident identities; at most one additional identity fragmentation event per annotated hour on echo-heavy held-out positives. Report unknown-identity coverage so an all-unknown system cannot pass.+- Separately report uncertain-candidate suppression, false words/minute, false identities/session, retained short replies, embedding coverage, latency/memory and capture drops. No new drops or unbounded growth during hour-long checks.++Required integration matrix: normal aligned output; low word/turn agreement and per-turn retry; whole-chunk fallback; silent/empty output; missing/corrupt reference; detector unavailable; protected/unprotected capture eras; chunk/route boundaries; mixed clean/contaminated cluster; stop/cancel; resume with matching/mismatching fingerprint; old-session read; new-result UI/export/summary parity; transactional reprocess failure; rollback interpretation.++Temporal-detector tests additionally cover positive/negative/zero lag conventions, reference timestamps shifted in both directions, wrong reference, music/periodic signals, nonlinear residuals with weak raw correlation, changing delay/drift, insufficient-energy windows, delayed/missing channels, AAC chunk padding, model-independent clock injection, detector budget exhaustion and freshness reset. Measure lock-onset/release latency and near-end speech lost during hold periods. Test a quiet reply while a duplicate state is already supported, not only isolated clean speech. Never compare competitor score thresholds directly with our exploratory normalized-correlation values without proving equivalent definitions.++## 6. Review, safety and delivery++Review after stages A/B and before enforcement: explicitly challenge all-speech-rejected, all-identities-unknown, repeated-centroid contamination, quiet double-talk loss, stale checkpoint and hidden-summary-leak counterexamples. Kimi K3 reviewed revision 2 through OpenCode in read-only plan mode. The six findings and their scope are recorded in `tools/MEETING_MICROPHONE_ADMISSION_PLAN_REVIEW.md`; revision 3 addresses test-only enablement, exact compatibility boundaries, identity keys, explicit test outcomes, epoch/guard alignment and distinct unknown reasons. That review is not sign-off on this amended document or the implementation. Any later external review receives scoped source/diffs only, not meeting recordings, transcripts, embeddings or secrets.++The existing FluidVoice source-review authorization does not automatically authorize sending Wispr/Granola bundles to an external model. Keep competitor inspection local; any separate disclosure requires appropriate authorization. Review temporal detector stale-state/latch behavior, threshold portability, and supplied-embedding bypass before enabling enforcement.++Preserve unrelated dirty-tree work. Do not install during the plan or shadow-data collection by default. Any later authorized Debug installation must follow AGENTS.md: same bundle ID/destination, valid deep/strict signature, matching designated requirement and Team ID, exact-app replacement, relaunch from `/Applications`. Stop before replacing the app if identity cannot be preserved.++Deliver each stage as a separately reviewable change with test evidence. Recommended first implementation unit: **Stage A only—typed evidence, precise naming with a legacy compatibility mapping, pure shadow decisions and regression tests.** No gain change, profile filtering, transcript suppression or app reinstall in that first unit.++## 7. Stage A execution record++Implemented after the user's review-and-proceed request:++- `MeetingEchoSignalScorer.swift`: renamed the verdict and low-explanation threshold identifiers; numeric thresholds and scorer control flow are unchanged. `legacyEffectiveEcho(textEcho:)` preserves the historical display truth table.+- `MeetingProcessingPipeline.swift`: uses the compatibility mapping and precise verdict name in diagnostics. Text-only profile eligibility is deliberately unchanged at this stage.+- `MeetingMicrophoneEvidence.swift`: non-persisted, immutable evidence/identity types; explicit missing-vs-zero and unknown reasons; temporal source protocol. The conservative pure shadow policy returns experimental uncertainty plus reasons, not calibrated admission or profile eligibility.+- `MeetingMicrophoneAdmissionContractTests.swift`: fake temporal source and characterization tests. Existing integration-test enum references were mechanically updated.++Validation: isolated `FluidASRBaseline` build-for-testing succeeded; 15 admission-contract tests and five existing live diagnostic tests passed, with zero failures/skips (`/private/tmp/fv-admission-stage-a-v1.xcresult`). A normalized source comparison confirmed the scorer's `verdict` body matches HEAD after identifier substitutions and comment removal. Static call-site inspection found shadow evaluation and the fake temporal implementation only in tests. The broader app integration suite and a Release build were not run; existing unrelated compiler warnings remain.++The known generated CTranscribe framework packaging defect required repairing only the isolated host's version symlinks, retaining the replaced build artifacts in temporary backups, then signing and deep/strict verifying that test host. The installed Debug app was not changed. No recording was opened, processed, reclassified, or sent to the external reviewer during Stage A.++Next implementation unit: Stage B1, real temporal duplicate measurements on local development fixtures with sidecar-only results. Stage A does not reduce the false microphone words yet, and its all-uncertain proposals cannot satisfy the release gates.++Post-implementation review attempt: Kimi K3 was asked to inspect the new evidence/test files and the two production diffs in read-only mode. That follow-up timed out at 180 seconds without findings. The completed revision-2 plan review and local verification stand; there is **no completed external code sign-off** for Stage A. Complete that code review before Stage B integration/enforcement.++## 8. Initial Stage B1 execution record++Implemented `MeetingPlaybackDuplicateDetector.swift`, the explicitly invoked offline `tools/meeting_temporal_shadow.swift` evaluator and 11 detector tests. No detector result is projected into turn admission or profile eligibility, and no production caller was added. Detailed configuration, implementation limitations, reproducible commands and numeric results are in `tools/MEETING_TEMPORAL_SHADOW_B1.md`.++The three development recordings produced zero supported locks at the initial experimental operating point: 196 of 244 windows were scored but inconclusive; 48 had insufficient coverage/energy. Unwindowed tails remain unmeasured. This does not establish useful weak-residual detection, speech recall, or a false-caption fix. Thresholds were not tuned downward after seeing these outcomes. Do not enable suppression from this result; B2/B3 and potentially a stronger temporal feature remain necessary.++Validation: final isolated baseline-host build succeeded; 31 targeted tests passed with zero failures/skips (11 detector, 15 Stage A contract, five existing live diagnostics), `/private/tmp/fv-temporal-b1-v3.xcresult`. The offline optimized evaluator compiled and ran locally on all three inputs; final sidecars are the v2 files documented in the report. No full app integration suite, Release build, held-out accuracy test or hour-long capture-load test was run. The existing generated framework packaging repair/signing was confined to the isolated test host, with displaced files retained in temporary backups. Installed Debug app unchanged.++Adversarial code review was retried with authorized Kimi K3 (180-second initial call and 300-second continuation) and Grok 4.6 (180 seconds, embedded scoped source only). All attempts timed out without returned findings. No review completion or sign-off is claimed. The completed earlier plan review is not code approval. Keep B1 offline-only until a completed code review and subsequent evidence gates; do not infer permission to integrate/enforce from this implementation record.++## 9. Initial Stage B2 execution record++After the user's “do it”, added a direct local CoreML speech-activity adapter, an injected model seam, bounded recurrent-state evaluation, and a conservative speech/playback diagnostic combination. The standalone CLI explicitly opts in with a local model argument; no production consumer exists. Cached model schema and aggregate-probability semantics were audited against the pinned source and local model graph. There is no model download, gain adjustment, app install or output suppression.++Three local development recordings yielded 1,916 full speech frames, including 153 active diagnostic windows and 24 unknown frames; all combined proposals remain uncertain. Both user-reported playback-only fixtures contain active windows, while temporal matching still has zero supported locks. This is evidence against enabling this simple combination as a near-end admission gate. No speech recall, false-word reduction, independent-person accuracy or held-out acceptance claim follows from these counts.++Kimi K3 completed three scoped reviews covering B1 core, B2 adapter/evaluator and the Stage A contract. Changes include exact integer lag-spread comparison, session-local failure latches, full-coverage temporal joins across mismatched frame grids, explicit unavailable controls and missing-playback-context diagnostics. Exact Stage A snapshot binding and cleared support counts after release are deliberately retained. Full finding dispositions, model audit, hashes, numeric results and verification are in `tools/MEETING_SPEECH_SHADOW_B2.md`.++The earlier review timeouts remain in the historical records above, but no longer describe the current review status. Post-fix code has not been independently re-reviewed in full and is not approved for enforcement. B3 annotated evaluation/calibration, effective app shadow integration, profile provenance, consumer consistency and release gates remain outstanding. The legacy echo score is not measured by this CLI, so the current two-signal comparison is not the complete planned ablation.++Final verification: isolated baseline-host build succeeded; 46 targeted tests passed with no failures/skips in `/private/tmp/fv-speech-b2-v3.xcresult` (11 speech, 13 temporal, 17 admission-contract, five live diagnostics). Final local fixture sidecars are the v2 files in the B2 report. Full app/Release, capture-load and held-out tests remain unrun. Installed app unchanged.++## 10. Existing-data Stage B3 execution record++After the user's “go”, added a versioned development annotation manifest and aggregate-only B3 evaluator with unit tests. The evaluator binds each sidecar to session bytes, pinned detector/model provenance and independently recomputed audio duration. It explicitly counts unknown/boundary/outside evidence and current transcript words without emitting transcript content or identities. All thresholds are descriptive; there is no selected/frozen operating point or production caller.++The available set has two pure-negative recordings, one mixed-label recording containing a negative interval plus only a coarse script region, and zero independently frame-labelled positives. Current saved output contains 324 visible microphone words and four per-session visible speaker slots in known-negative intervals; one saved echo segment accounts for seven additional hidden words. This is current-output inventory, not a projection of the candidate policies. The coarse script region contains 89 visible words whose accuracy is unverified.++At the cached VAD's 0.85 diagnostic threshold, 31/1,691 known-negative measured frames are still accepted while 121/161 coarse-region frames are accepted. A 0.04 threshold accepts 160/161 coarse-region frames but also 549 negative frames. Because frames are correlated and the coarse region contains unlabelled pauses, neither ratio is speech recall or an independent trial estimate. B1 has no supported locks, making temporal-only identical to accept-all and combined identical to speech-only on these inputs. No threshold is safe or selected.++Kimi K3's B3 review found eight issues across two passes: denominator drift, hard-coded frame duration, silent transcript-boundary exclusions, ambiguous overlapping cohort counts, selection-flavored reporting, empty annotations, unpinned model hashes and sidecar-only duration bounds. All were addressed; final post-fix verification returned no findings, and 11 evaluator unit tests pass. Final report and review dispositions: `tools/MEETING_ADMISSION_B3_DEVELOPMENT_EVAL.md`; aggregate artifact: `/private/tmp/fv-admission-b3-dev-v7.json`. No app build/test was required for evaluator-only changes; the last Swift validation remains B2's 46 passing targeted tests. Installed app unchanged.++## 11. Controlled Stage B3 extension++The user completed five separate development recordings with the installed app: playback only; normal speech without playback; quiet speech without playback; three normal-voice repetitions over the same YouTube playback; and two sequences of five quiet replies over that playback. The user confirmed that YouTube playback used speakers, their speech entered through the microphone, no headphones were used, and nobody else was speaking nearby. The runs ranged from 10 to 159 seconds. Every microphone/application track completed with zero recorded dropped samples, discontinuities or failures.++The existing offline B1/B2 CLI created numeric-only sidecars for all five without modifying the sessions. The playback-only run yielded four visible microphone-attributed segments and 49 words despite user-reported silence. B1 found no `duplicateSupported` window in any controlled run; three isolated candidates in the playback-only run did not satisfy stability. At diagnostic speech threshold 0.85, 10/600 controlled playback-only frames were accepted, while 137/445 frames in coarse user-speech regions were accepted. Those coarse regions contain pauses and playback-only time and are not recall denominators.++Across the eight-recording development set, threshold 0.04 accepts 681/2,291 known-negative frames and 507/606 coarse-region frames; threshold 0.85 accepts 41/2,291 and 258/606 respectively. The temporal ablation remains identical to accept-all because there are no supported locks. No operating point is selected: there is still no independently frame-labelled positive recording or held-out set, and transcript-localized overlap intervals are explicitly non-independent. Current aggregate artifact: `/private/tmp/fv-admission-b3-controlled-v3.json`. Full method, limits and hashes are in `tools/MEETING_ADMISSION_B3_DEVELOPMENT_EVAL.md`. The installed app and runtime behavior remain unchanged.++The next evidence gate requires independently timed quiet, normal, short-reply and double-talk recordings. These become development data; separate recordings remain required for held-out evaluation. Do not proceed to runtime shadow integration, threshold freezing, suppression or profile filtering from this result.diff --git a/MEETING_PLAYBACK_ATTRIBUTION_V2_IMPLEMENTATION_PLAN.md b/MEETING_PLAYBACK_ATTRIBUTION_V2_IMPLEMENTATION_PLAN.mdnew file mode 100644index 00000000..878bdedc--- /dev/null+++ b/MEETING_PLAYBACK_ATTRIBUTION_V2_IMPLEMENTATION_PLAN.md@@ -0,0 +1,447 @@+# Reference-aware microphone de-echo and speech preservation++Date: 2026-09-09 (local)+Status: revision 2 after adversarial review. Implementation plan only. No runtime behavior, installed app, recording, transcript, or speaker profile is changed by this document.++## Decision++The recommended solution is a **post-capture, reference-aware echo-cancellation and attribution pipeline** that consumes the selected application's clean-within-scope ScreenCaptureKit audio as a far-end/render reference and produces a derived microphone residual for ASR. Preserve the recorded microphone and application tracks unchanged; speaker-profile enrollment remains limited to verified clean no-playback microphone intervals in the first release.++Do not try to fix this by lowering the existing correlation threshold, adding a louder VAD cutoff, trusting the `voiceProcessingEnabled` flag, or building a new echo canceller from a single transfer-function estimate. The controlled development recordings show that the current waveform detector and current spectral scorer do not identify this residual reliably. A speech VAD also fires on leaked playback and therefore cannot identify near-end speech by itself.++The solution order is: **(1) fix or rule out FluidVoice's current Apple VPIO configuration, (2) evaluate ScreenCaptureKit's already-implemented paired microphone/application stream, then (3) evaluate a pinned, minimal WebRTC Audio Processing Module / AEC3 wrapper on the winning raw topology.** Cascading AEC3 after the current VPIO output is a fallback experiment, not the preferred input: Apple's nonlinear suppressor/AGC may have removed the linear relationship AEC3 needs. A smaller native Swift/Accelerate multi-band attribution detector is the required comparison and an independent enforcement guard, but it is not the place to recreate a production adaptive echo canceller. If no candidate preserves quiet double-talk on FluidVoice's actual streams, enforcement remains off.++This plan supersedes the feature-selection parts of `MEETING_MICROPHONE_ADMISSION_IMPLEMENTATION_PLAN.md`; its evidence contracts and profile-integrity rules remain applicable. The earlier B1/B2/B3 work is retained as a baseline and regression harness, not promoted to production.++Review record: `tools/MEETING_PLAYBACK_ATTRIBUTION_V2_ADVERSARIAL_REVIEW.md` documents the Claude Opus 5, Kimi K3, and Grok 4.6 findings and their revision-2 dispositions.++## 1. What is established, and what is still a hypothesis++### Established locally++- FluidVoice's preferred built-in-speaker online-call path enables voice processing on an `AVAudioEngine` input node, records a tap from that node, and separately captures selected-application audio with ScreenCaptureKit. Its fallback runtime already captures ScreenCaptureKit `.audio` and `.microphone` outputs from one `SCStream`, but marks speaker-route microphone audio unprotected and therefore does not admit it to transcription.+- The selected application's audio does not enter FluidVoice through an app-owned `AVAudioEngine` render source. It arrives through the ScreenCaptureKit `.audio` output.+- On the five controlled recordings made with speakers and no other nearby talker, the playback-only recording still produced four visible microphone-attributed segments and 49 words.+- The existing B1 two-second raw waveform detector produced zero `duplicateSupported` windows across all eight development recordings, including all five controlled recordings.+- The existing `MeetingEchoSignalScorer` already estimates delay with GCC-PHAT and fits a blockwise frequency-domain transfer function. On the controlled playback-only and overlap data, its explained fractions were generally near zero and it frequently returned the legacy low-explanation rescue. Its failure is therefore not fixed by merely adding “adaptive filter” terminology or reducing the B1 NCC threshold.+- Silero VAD activity remains present in playback-only recordings. It is speech activity evidence, not speaker-source evidence.++### Apple behavior that is documented++Apple describes AVAudioEngine voice processing as intended for echo cancellation and VoIP. Enabling it switches both I/O nodes into voice-processing mode, and it is available only when rendering to an audio device. Apple also says AUVoiceIO and AVAudioEngine expose the same voice-processing capabilities and that FaceTime and Phone use these APIs. The processing includes echo cancellation, noise suppression, and AGC and is device-tuned:++- [What's New in AVAudioEngine, WWDC19](https://developer.apple.com/videos/play/wwdc2019/510/)+- [What's new in voice processing, WWDC23](https://developer.apple.com/videos/play/wwdc2023/10235/)+- [`AVAudioIONode`](https://developer.apple.com/documentation/avfaudio/avaudioionode)+- [`kAUVoiceIOProperty_BypassVoiceProcessing`](https://developer.apple.com/documentation/audiotoolbox/kauvoiceioproperty_bypassvoiceprocessing)+- [`AVAudioInputNode.isVoiceProcessingAGCEnabled`](https://developer.apple.com/documentation/avfaudio/avaudioinputnode/isvoiceprocessingagcenabled)++ScreenCaptureKit provides audio sample buffers separately from microphone output and exposes their configured sample format. It does not document a public API for supplying an arbitrary ScreenCaptureKit stream as the far-end reference of AUVoiceIO/AVAudioEngine:++- [`SCStreamOutputType`](https://developer.apple.com/documentation/screencapturekit/scstreamoutputtype)+- [`SCStreamOutputType.audio`](https://developer.apple.com/documentation/screencapturekit/scstreamoutputtype/audio)+- [`SCStreamOutput`](https://developer.apple.com/documentation/screencapturekit/scstreamoutput)+- [`SCStreamConfiguration.capturesAudio`](https://developer.apple.com/documentation/screencapturekit/scstreamconfiguration/capturesaudio)++### Important unresolved point++Apple's public material does **not** specify enough to assert that macOS VPIO either does or does not use the system-wide output mix from other processes as an echo reference. WWDC19's phrase “audio that is coming from the device” is broader than “audio rendered by this engine,” while its same-I/O-path examples are VoIP-oriented. Therefore:++- treat “Chrome never reaches the VPIO reference” as a plausible architectural hypothesis, not a fact;+- do not infer correct AEC from `isVoiceProcessingEnabled == true`;+- measure the actual behavior on the supported Mac/device/route matrix before changing capture architecture.++FaceTime owns its remote-media receive path and is documented to use Apple's voice processing. A conventional WhatsApp-style VoIP client likewise owns both render and capture streams, but WhatsApp's current proprietary audio implementation is not publicly verified here. Do not claim that its internal AEC is identical to FaceTime or to a particular WebRTC revision.++## 2. Why the current approaches are insufficient++### Voice processing is a useful first layer, not proof of clean microphone audio++`MeetingMicrophoneCapture` requests VPIO, configures other-audio ducking, taps processed input, and records settled device metadata. It binds and reads back audio-unit element 1 (input), but does not bind or persist element 0 (output); the source comments already note that element 0 once read back an unrelated AirPods output. It also persists `voiceProcessingEnabled: true` as a literal after the setter succeeds rather than persisting the getter/readback, and it does not persist bypass or AGC readbacks. The engine has no explicit application render source. These are strong configuration hypotheses, not proof of causality. The controlled recording is direct evidence only that the resulting track can still contain ASR-visible playback.++### B1 waveform correlation assumes too much signal similarity++The B1 detector compares two-second, 2 kHz windows using mean-centered normalized correlation over a bounded lag search. Voice processing, speaker/microphone acoustics, room reverberation, non-linear suppression, AGC, and frequency coloration can leave intelligible speech while reducing raw waveform correlation. The observed correlations never reached the exploratory support threshold. Lowering that threshold would also increase matches to mismatched controls and periodic content.++### The current spectral scorer is not a robust AEC++`MeetingEchoSignalScorer` estimates a single delay, learns one complex transfer coefficient per frequency bin over two-second blocks, and measures residual energy. It lacks a continuously adapting partitioned echo path, explicit double-talk adaptation control, render/capture queue jitter handling, clock-drift tracking, nonlinear residual suppression, ERL/ERLE state, and echo-path-change recovery. Its low explained fraction is ambiguous between near-end speech, nonlinear residual echo, bad alignment, insufficient excitation, and a changing acoustic path.++### VAD and ASR text cannot identify the acoustic source++- VAD answers “does this resemble speech?” Leaked playback is speech.+- Text similarity can corroborate playback, but ASR errors, paraphrase-like errors, repeated phrases, and deliberate local repetition make it unsafe as the only suppression signal.+- Speaker clustering can assign stable identities to repeated leakage. A centroid is not proof of near-end speech.++### Competitor lessons are architectural, not thresholds to copy++Static inspection of Wispr Flow 1.6.492 found separate microphone/system PCM, repeated lag-stability evidence, attack/release behavior, timestamped exclusion spans, epoch resets, bounded-work fail-open behavior, and preservation checks before final microphone zeroing. Those are useful design patterns. Its feature activation and performance on FluidVoice recordings are unverified, and its constants are not FluidVoice calibration.++Static inspection of Granola 7.498.1 found a native capture boundary with separate microphone/system buffers and options related to AEC and gain compensation. The native implementation is unavailable in the inspected JavaScript. Its local diarization worker's gain/VAD/centroid path is not independent near-end proof.++## 3. Target architecture++```text+ScreenCaptureKit app audio ──► reference timeline ─┐+ ├─► synchronizer ─► AEC/attribution engine+preserved microphone track ──► capture timeline ──┘ │+ ├─► derived residual PCM+ ├─► delay/ERL/ERLE/echo state+ └─► explicit unknown reasons++derived residual PCM ─► VAD + microphone ASR ─► interval evidence ─► admission/profile policy+preserved original PCM ─────────────────────────────────────────────► local recovery/review only+application ASR ────────────────────────────────────────────────────► supporting text alignment+```++### Capture invariants++1. Preserve the original application and microphone tracks byte-for-byte.+2. Treat every derived residual as reproducible output with a versioned configuration and source hashes.+3. Keep original PTS, valid-sample masks, capture eras, route identity, device identity, discontinuities, and clock-drift observations.+4. Never substitute zeroes for unavailable samples before evidence scoring. Missing coverage stays unknown.+5. Never play ScreenCaptureKit-captured application audio back through FluidVoice during normal operation; doing so would duplicate audible output and create a new latency/feedback path.+6. Persist the reference scope (`selectedWindow`, `selectedApplication`, or an explicitly authorized broader mix). The current selected-application reference is not the acoustic system-output mix: other processes, notifications, volume changes, output DSP, and uncaptured helper processes can be absent or transformed.+7. A present selected-application reference is not automatically a complete reference. Carry `referenceScopeLimited`/`referenceCompletenessUnobservable` separately from absent/gapped reference. Without an explicitly authorized broader reference, the feature can promise suppression only for playback attributable to the selected scope; unrelated playback remains fail-open text and cannot be claimed solved.+8. Do not silently broaden capture to the full system mix. Any diagnostic or product full-mix reference requires explicit user-facing scope, retention, privacy, and deletion rules; it must not be transcribed or persisted by default merely because it is useful to AEC.++### Synchronizer++Add a bounded `MeetingReferenceSynchronizer` that emits fixed 10 ms render/capture frames plus metadata. It must:++- resample through an explicitly versioned converter and account for converter delay;+- define three timelines explicitly: microphone host/sample-count time, ScreenCaptureKit PTS, and the derived session-monotonic frame time. Never assume the first two share a clock merely because both use `CMSampleBuffer`;+- reset at either-track discontinuities, route/device changes, sample-rate changes, decoder gaps, or invalid/non-monotonic PTS;+- expose measured buffer delay and slowly varying drift. Either bound an epoch when drift exceeds a frozen limit or apply explicit, versioned drift-compensating resampling; never silently time-stretch one stream;+- compensate codec encoder priming/remainder, edit-list timing, and every converter's algorithmic delay;+- carry valid masks and an epoch ID into every result;+- distinguish `referenceAbsent`, `referenceGap`, `referenceScopeLimited`, `referenceCompletenessUnobservable`, `captureGap`, `captureTimingSynthesized`, `delayUnresolved`, `clockDriftUnstable`, and `engineUnavailable`;+- freeze adaptation, rather than fabricate aligned samples, across microphone spans whose PTS was synthesized after invalid host time; reset after the recorded resynchronization boundary;+- bound queues and processing time and fail open without blocking capture callbacks.++The initial implementation is offline-only and runs from completed lossless chunk files. No real-time callback may call C++ AEC, FFT, ASR, or file I/O.++### Candidate A: native multi-band attribution++Build a new offline experimental detector rather than tuning B1 in place:++- 16 kHz mono analysis, 20–32 ms Hann frames, 10 ms hop;+- log-mel or perceptual multi-band energies plus band-limited modulation envelopes;+- per-band robust normalization so gain and static coloration do not dominate;+- bounded lag candidates and a monotonic delay path with explicit slope/drift limits;+- coherence/support computed across frequency and time, excluding low-excitation and periodic/ambiguous frames;+- deliberately mismatched reference controls from the same recording;+- attack/release state that resets at the synchronizer epoch and never crosses missing coverage;+- a `duplicateSupported` result only when lag-path continuity, multi-band support, and control margin all pass.++This candidate produces attribution evidence only. It does not manufacture residual audio, call speech activity “near-end,” or suppress text by itself. A chosen enforcement path must retain it, or an equivalently independent reference-attribution guard, rather than trusting residual VAD/ASR alone.++### Candidate B: WebRTC APM/AEC3++Build a minimal offline C++/Objective-C++ wrapper around a **pinned commit** of WebRTC APM/AEC3. Feed the application stream to the render/reverse side and the microphone stream to the capture side in the API's required frame order. Export only:++- both the linear-filter output and the nonlinear/suppressed output, identified separately; treat the linear output as the first ASR/embedding-preservation candidate rather than assuming the listening-optimized suppressor output is best;+- convergence/delay state;+- available echo metrics such as ERL/ERLE or residual-echo likelihood;+- reset/failure reason and processing cost;+- the exact WebRTC commit, build flags, configuration, license, and artifact hash.++AEC3 is a mature reference/capture processor: its current code handles 10 ms frames, delay buffering/estimation, adaptive filtering, render/capture timing, residual echo estimation, and clock drift. Its source is BSD-licensed with a separate patent grant, but a dependency/security/license review is still mandatory:++- [WebRTC Audio Processing Module overview](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/audio_processing/g3doc/audio_processing_module.md)+- [`EchoCanceller3`](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/audio_processing/aec3/echo_canceller3.h)+- [`EchoPathDelayEstimator`](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/audio_processing/aec3/echo_path_delay_estimator.h)+- [`ResidualEchoEstimator`](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/audio_processing/aec3/residual_echo_estimator.cc)+- [WebRTC license](https://webrtc.googlesource.com/src/+/main/LICENSE) and [patent grant](https://webrtc.googlesource.com/src/+/refs/heads/main/PATENTS)++Do not vendor all of WebRTC. First prove a reproducible minimal target and enumerate its transitive source/build dependencies, including Abseil, `rtc_base`, `common_audio`, SIMD/FFT code, generated configuration, and field-trial behavior. Pin the field-trial string and all AEC3 suppressor/comfort-noise/high-pass options as part of engine identity. Do not enable WebRTC AGC or separate noise suppression in the first AEC-only comparison; changing multiple processors at once would make attribution and speech-preservation results uninterpretable.++### Capture-input bake-off++Test Candidate B against three explicitly different capture inputs, in this order:++1. **SCK paired microphone + SCK selected-application audio from one `SCStream`.** This code path already exists. Measure—do not assume—whether the microphone is unprocessed, whether the two outputs share a usable clock, and what latency/jitter it adds.+2. **Exclusive raw diagnostic microphone + SCK reference.** Run with VPIO fully torn down; never make two microphone owners race for the same device. Use only if it adds information beyond the paired SCK path.+3. **Current VPIO output + SCK reference.** This is the smallest product diff but the least identifiable signal model because it cascades AEC after Apple's time-varying nonlinear processing. Reject it if delay/convergence/ERLE are unstable or if most valid coverage remains inert/uncertain; do not tune around non-convergence.++Never silently replace VPIO capture based on one recording. On speaker routes, the current SCK microphone era is `.unprotected`, which blocks both live and offline mic transcription. A production raw/SCK topology therefore needs a separately reviewed capture migration and a distinct state such as `rawWithValidatedOfflineCancellation`; it cannot masquerade as behavior-unchanged shadow mode. That migration must cover exclusive device ownership, common-clock verification, interruption/recovery, permission state, file size, rollback to the whole VPIO runtime, and live-caption consequences.++### Admission and speaker-profile policy++The AEC residual is a candidate analysis/ASR source, not ground truth. Combine evidence without pretending correlated signals are independent:++- `likelyPlaybackOnly`: reliable render reference, converged delay/path, sustained echo attribution, and no supported residual speech interval;+- `acceptedNearEndSpeech`: measurable residual speech with adequate coverage and a preservation-safe AEC state; identity may remain unknown;+- `mixedOrUncertain`: residual speech and playback overlap, conflicting engines, unconverged/path-change state, or coarse timing;+- `unscored`: reference/capture/model/engine unavailable;+- `scopeLimited`: the selected reference is healthy but cannot establish whether other audible processes contributed. This is not equivalent to a complete acoustic reference.++Rules:++1. In the first enforcement release, use a selected linear or suppressed residual as the microphone ASR input only for intervals where the chosen engine is valid and held-out tests show speech preservation. Fall back to the existing microphone ASR input when evidence is unscored; include those fail-open words in the playback-leak metric so abstention cannot make a candidate pass.+2. Never delete or overwrite original microphone PCM.+3. Never drop an entire turn because playback was present somewhere in it. Split only at validated frame boundaries with padding; otherwise retain a recoverable uncertain candidate.+4. Do not admit any microphone-derived speaker embedding from `likelyPlaybackOnly`, `mixedOrUncertain`, `scopeLimited`, invalid, or shared-centroid-contaminated intervals.+5. In the first enforcement release, update speaker profiles only from verified no-playback clean intervals using original microphone PCM restricted to independently admitted speech. Do not enroll from overlap residuals. Separately evaluate embeddings from linear residual, suppressed residual, and clean original PCM for domain shift before any later relaxation.+6. Application/microphone fuzzy transcript alignment is supporting evidence and diagnostic output only until independently ablated. It cannot override strong residual speech or prove identity.+7. Missing reference/model/metrics never means “no speech” or “playback only.”+8. Run original and residual ASR side by side offline. Original words with an empty residual are not automatically rescued—that can be successful echo removal—but if residual-side speech activity, the independent attribution engine, or timing evidence conflicts with the empty residual, degrade the interval to `mixedOrUncertain` rather than silently dropping it.+9. Specify whether interval selection occurs before ASR or from word timestamps after ASR. Do not splice original/residual token streams until boundary padding, duplicate removal, and timestamp accuracy are independently tested.++## 4. Implementation phases++### Phase 0 — close the Apple/capture evidence gap++Deliverable: `tools/MEETING_VPIO_REFERENCE_PROBE.md` plus numeric-only result sidecars.++Create a diagnostic-only, correctly signed build or isolated harness. Use a safe-level, known local stimulus containing an impulse/maximum-length-sequence-style alignment probe plus speech-like material; do not use the current scorer's explained-fraction as the sole Apple-AEC meter. On each supported route, run short exclusive trials:++| Trial | Microphone path | Audible render/reference | Purpose |+| --- | --- | --- | --- |+| A | current VPIO input tap | helper/external process, selected-app SCK reference | Reproduce current architecture |+| B | VPIO input tap | known local PCM through a source node on the same engine; reference from an engine-local tap | Test paired VPIO I/O. Informational only: FluidVoice cannot productize ownership of another app's render path |+| C1 | same VPIO graph, uplink processing bypassed | same external stimulus as A | Quantify voice-processing attenuation without changing capture technology |+| C2 | SCK paired `.microphone` + `.audio` from one stream | same external stimulus | Test the already-implemented paired topology, mic processing, clock relation, latency, and jitter |+| D | VPIO with AGC disabled/enabled; ducking variants reported separately | A and B stimulus where public APIs permit | Separate AGC behavior; do not treat ducking volume change as AEC evidence |+| E | headphones | external process | Negative acoustic-leak control |+| F | exclusive raw/HAL diagnostic, VPIO fully stopped | external process | Only if it adds information beyond C2; never use simultaneous mic owners |+| G | selected app plus a second process and a mid-trial volume change | selected-app reference; optional explicitly authorized full-mix diagnostic reference | Measure incomplete-reference and post-tap gain/DSP failure modes |++Before acoustic conclusions, read and persist the actual input and output `kAudioOutputUnitProperty_CurrentDevice` elements, output render activity/connection, `isVoiceProcessingEnabled`, `isVoiceProcessingBypassed` / `kAUVoiceIOProperty_BypassVoiceProcessing`, `isVoiceProcessingAGCEnabled` / `kAUVoiceIOProperty_VoiceProcessingEnableAGC`, `isVoiceProcessingInputMuted`, and ducking state where supported. The exact property, scope, element, `OSStatus`, and value belong in the probe result. Verify that B's render is audible on the read-back output device. Force-binding element 0 is diagnostic-only until route-change and user-output behavior are understood.++For each trial persist only numeric diagnostics unless the user explicitly authorizes local audio retention: device/property readbacks; graph connection/render state; sample formats; all three timelines; delay sign/distribution; drift; multiband coherence; RMS; attenuation; and uncertainty. SCK-paired timestamps are measured rather than assumed to share a clock. A broader full-system reference in G is opt-in, ephemeral, never transcribed, and cannot become a product default through this probe.++Exit: rank (1) a reproducible Apple VPIO graph/device/property fix, (2) SCK-paired capture quality and clock behavior, and (3) current VPIO residual explainability. If the Apple-only configuration passes later speech-preservation gates, prefer it and stop. If SCK pairing wins, freeze that topology before building AEC3. If the current VPIO residual has unstable delay/coherence, remove VPIO+Aec3 from further work rather than tuning it.++### Phase 1 — freeze corpus and evaluation protocol++Deliverables: versioned annotation manifest, local-only runner, aggregate-only report.++1. Keep all existing eight recordings development-only and fixture-only. They exercise the VPIO topology and cannot calibrate a new SCK/raw topology.+2. Add frame-level timing labels for playback-only, clean normal speech, clean quiet speech, short quiet overlap replies, sustained overlap speech, pauses, route transitions, and reference gaps. Coarse whole-recording labels cannot select thresholds.+3. Build the calibration/held-out corpus only after Phase 0 freezes each evaluated topology. Include other-process speech, window-vs-application scope, helper-process audio, system volume changes, music, silence, periodic sounds, and multiple voices.+4. Reserve separate speakers/sessions/environments as held-out before tuning.+5. Compute recording-level metrics; correlated 10/256 ms frames are not independent samples.+6. The corpus minimums and split rules in `MEETING_RELIABILITY_IMPLEMENTATION_PLAN.md` remain binding: at least 12 independent negative sessions across three output configurations and two acoustic environments; at least 30 annotated minutes across three independent recordings for each populated quiet/short/double-talk positive condition; at least 100 labeled short replies; splits by recording/device/speaker; and fresh held-out data after a failed held-out run.+7. Keep source recordings and transcripts local. Reports contain aggregate numbers, hashes, versions, and failure reasons only. Include label-quality/independent-adjudication reporting for quiet overlap.++Metrics:++- playback-only visible microphone words/minute and speech-seconds/minute;+- percentage reduction from the current pipeline;+- scripted near-end WER or phrase recall, separately for clean, quiet, and overlap;+- false removal of near-end intervals;+- speaker-profile contamination and clean-profile yield;+- unscored/unknown coverage by reason;+- decision/inertness coverage: fractions reaching playback-only, accepted, uncertain, scope-limited, and unscored outcomes for each input topology;+- delay lock time, path-reset recovery, CPU time, peak memory, and derived-file size;+- speaker-embedding domain shift: clean-original versus linear-residual versus suppressed-residual similarity and cross-session verification error, reported separately from ASR quality.++### Phase 2 — synchronizer and deterministic fixtures++Targets: new `MeetingReferenceSynchronizer`, offline CLI, and baseline-host tests.++Required fixtures:++- known impulse/FIR echo with positive and negative delay;+- slowly drifting clocks;+- dropped render and capture blocks;+- codec encoder priming/remainder and edit-list offsets;+- discontinuity/route/sample-rate epochs;+- valid audio with synthesized microphone timing, adaptation freeze, and resynchronization;+- silence and low-excitation references;+- nonlinear coloration/clipping;+- near-end-only, echo-only, and double-talk at multiple echo-to-near-end ratios;+- cancellation-safe exact repetition by the near-end talker;+- deterministic replay and bounded-resource failure.++Exit: frame pairing, lag sign, converter delay, masks, and resets are independently test-pinned before either candidate is scored.++### Phase 3 — offline candidate bake-off++Implement Candidate A and a minimal Candidate B behind a common `MeetingReferenceAttributionEngine` protocol. First run a time-boxed out-of-tree AEC3 build-feasibility spike: require a reproducible macOS arm64/x86_64 artifact, enumerated transitive sources/licenses/field trials, a size report, and a Swift-callable smoke test. If that cannot be produced within a project-owner-approved time box, stop the extraction rather than letting WebRTC vendoring dominate the work; evaluate a maintained standalone WebRTC-audio-processing distribution only after the same provenance/security review, or return to the capture-topology/source-separation decision.++After Phase 0 freezes the input topology, run blinded IDs through the corresponding frozen development set. Do not tune on held-out data. VPIO, SCK-paired, and exclusive-raw recordings are different signal domains; results and thresholds do not transfer silently between them.++A candidate is rejected if it:++- labels leaked playback as accepted near-end speech at an unacceptable recording rate;+- removes or corrupts quiet/overlap phrases beyond the corpus gate;+- relies on a VAD or ASR-text-only source decision;+- carries support across gaps/epochs;+- cannot report unknown rather than fabricate a decision;+- is mostly inert: valid coverage remains unconverged/uncertain or fails to reach actionable states;+- depends on a selected-app reference while silently treating unrelated-process or post-volume/DSP audio as covered;+- cannot meet a bounded local processing/storage budget.++Decision rule:++- choose Apple-only if Phase 0 proves it meets the same output gates;+- otherwise choose SCK-paired microphone plus AEC3 if it materially reduces playback-only microphone words while preserving quiet/overlap speech and has an acceptable vendoring footprint;+- consider current VPIO plus AEC3 only when Phase 0 proves its residual remains delay-locked and learnable and it beats the SCK-paired arm on playback reduction without worse speech preservation;+- require Candidate A, or an equivalently independent reference-attribution guard, for any automatic playback-only/residual-ASR enforcement;+- do not implement a home-grown production adaptive canceller if AEC3 fails. Investigate capture topology or a separately reviewed source-separation model instead.++All Phase 3 comparisons use the binding parent-plan release metrics, including unscored/fail-open output in session totals. “Unacceptable recording rate” is not renegotiated during the bake-off.++### Phase 4 — shadow integration, no behavior change++Targets include `MeetingProcessingConfiguration`, `MeetingProcessingPipeline`, a versioned sidecar schema, and local numeric logging.++1. Add explicit `off` and `shadow` modes; default `off`.+2. Snapshot mode/version at processing start. Never consult a mutable global mid-run.+3. Generate the derived residual and typed evidence after chunk finalization, outside capture callbacks.+4. Run residual ASR/VAD and profile eligibility in parallel with the unchanged legacy outputs.+5. Persist only bounded local evidence needed for comparison; no raw PCM or transcript text in logs.+6. Record would-change counts: words, segments, profile observations, unknowns, processing cost, and candidate/legacy disagreements.+7. Make restart/retry idempotent and key outputs by session, source hashes, epoch, engine version, and configuration hash.++Exit: repeated processing is deterministic for the same architecture, binary, configuration, and inputs; record architecture and build hash rather than requiring cross-architecture floating-point byte identity. `off` is identical to today's behavior; `shadow` never changes UI/export/summary/profile output; injected engine failure falls back cleanly.++This phase applies directly only if the selected topology can preserve today's capture path. If SCK/raw wins, first produce the separate capture-migration design described above. A speaker-route raw era cannot be called “shadow/no behavior change” because today's `.unprotected` gate suppresses its mic transcript; the migration's rollback unit is the entire capture runtime.++### Phase 5 — held-out gate and limited offline enforcement++Before enabling enforcement, freeze:++- the engine revision/configuration and all decision thresholds;+- minimum reference and residual-speech coverage;+- delay convergence and path-change recovery requirements;+- interval padding/splitting rules;+- resource budgets;+- recording-level acceptance thresholds.++The stricter parent-plan release gates apply unchanged; this plan does not replace them with relaxed percentages:++- zero false accepted microphone words and zero visible microphone identities on the curated playback-only regression set, with an exposure-normalized upper confidence bound and session-level paired reporting;+- at most one absolute percentage-point increase in missed-speech duration and one point in speaker-attributed WER on each populated quiet, short-reply, and double-talk held-out condition;+- no increase in false confident microphone identities and no more than one additional identity-fragmentation event per annotated hour on echo-heavy held-out positives;+- zero known instances of cross-gap/route/epoch suppression;+- all missing/incomplete-reference, over-budget, dependency-load, and corrupt-sidecar tests fail open with originals recoverable;+- processing completes within a separately measured and product-approved local CPU/memory/latency budget.++Each gate is evaluated only after the binding corpus minimums above are met. Report recording-level paired uncertainty, and require the upper bound to satisfy each non-inferiority margin; do not use a word-level binomial model for bursty errors. Playback-leak totals include unscored/fail-open intervals and have a separately frozen cap on unscored coverage, so abstention cannot pass. Passing development fixtures alone is prohibited.++First enforcement scope:++- offline final processing only;+- supported built-in speaker/microphone routes with validated device metadata;+- derived residual ASR; speaker-profile updates remain restricted to verified clean no-playback original-microphone intervals until the separate embedding-domain gate passes;+- local recovery path that can show/reprocess from original audio;+- a kill switch returning immediately to legacy processing.++Bluetooth, HDMI, external/aggregate, and unknown output routes remain outside this first enforcement scope; their current unprotected-microphone admission behavior is unchanged until they have topology-specific evidence.++Keep live captions unchanged until offline enforcement has passed a longer soak. This deliberately means live text can differ from the cleaned final transcript; document that product tradeoff rather than silently hiding live mic captions whenever application audio is active.++### Phase 6 — live processing, only if separately justified++Real-time AEC introduces callback scheduling, render/capture ordering, latency, interruption, and state-recovery risks. Treat it as a separate project. A live implementation must use a bounded non-real-time worker, never block ScreenCaptureKit/AVAudioEngine callbacks, and must define behavior for stale/missing render frames. It requires its own stress, interruption, sleep/wake, route-change, CPU-pressure, and long-call tests.++## 5. Dependency and packaging gate for AEC3++Before Candidate B code lands:++1. Pin an immutable WebRTC commit and record the repository/commit in source and SBOM.+2. Build only the transitive APM/AEC3 subset needed for macOS arm64/x86_64; enumerate generated and third-party dependencies.+3. Preserve WebRTC license notices and patent-grant text; obtain project-owner/legal review if required by release policy.+4. Reproducibly build a signed/static artifact from source. Do not download a mutable binary during app execution or testing.+5. Run dependency vulnerability and symbol/export review.+6. Wrap C++ ownership in a narrow Objective-C++ API with explicit frame geometry, resets, and error returns; no exceptions across Swift.+7. Prove no network access, telemetry, audio logging, or debug dump is enabled.+8. Verify binary size, startup time, peak memory, CPU, and notarization/code-signing impact before choosing it.+9. Make feasibility time-boxed with an explicit abort result. A failed minimal/reproducible build is a decision outcome, not permission to vendor the full WebRTC tree or fetch a mutable prebuilt binary.++## 6. Required tests++### Unit/property tests++- PTS mapping, converter latency, lag sign, drift, and epoch reset;+- masks never become measured silence;+- render/capture ordering and exact 10 ms frame counts;+- multi-channel render versus versioned downmix policy, with a measured downmix error budget;+- deterministic AEC reset/replay;+- missing/late/duplicate/non-finite frames;+- double-talk adaptation safety and echo-path changes;+- exact local repetition of playback;+- shared speaker-centroid exclusion;+- no profile output from uncertain/playback-only intervals;+- all reason-code and legacy fallback branches;+- bounded queue, cancellation, timeout, and repeated failure latch reset by new session.++### Integration tests++- complete session reprocess with original hashes unchanged;+- `off`/`shadow` output equality;+- shadow sidecar restart/idempotence;+- app-only, mic-only, overlap, route change, and missing reference;+- selected-window/application reference plus unrelated-process audio and system-volume changes;+- SCK-paired microphone/application PTS comparison and raw-era admission behavior;+- model/dependency unavailable with no download attempt;+- exporter/summary/profile behavior under each admission outcome;+- installed-app capture unchanged until Phase 5.++### Acoustic and adversarial tests++- built-in speaker volume/distance/room sweep;+- male/female and quiet/normal/loud near-end voices;+- fast interjections during loud playback;+- music, applause, silence, noise, and periodic tones;+- speaker movement and device rotation;+- output-route and microphone changes;+- intentionally repeated far-end words by the local user;+- one and multiple nearby local speakers;+- long recordings and thermal/CPU pressure.++## 7. Observability and privacy++Emit numeric/enum diagnostics only:++- engine/version/config/source hashes;+- epoch and aggregate valid coverage;+- delay/convergence/path-reset counts;+- ERL/ERLE or candidate-specific aggregate metrics;+- residual/original energy ratios;+- admission outcome/reason counts;+- would-change segment/word/profile counts;+- CPU, memory, duration, and fallback reason.++Never log transcript text, raw PCM, embeddings, window titles, speaker labels, or absolute recording paths. Reviewer prompts may include relevant source and diffs under the repository authorization, but not recordings, transcripts, model weights, secrets, or unrelated files.++## 8. Rollback and recovery++- Feature modes are `off`, `shadow`, and narrowly scoped `enforce`; `off` remains a one-step kill switch.+- Originals are immutable; derived outputs are versioned and disposable.+- Reprocessing from originals can reproduce either legacy or selected-engine output.+- Unsupported routes, missing reference, failed convergence, corrupt sidecars, dependency load failures, or resource-budget failures use the documented fail-open text path and exclude uncertain audio from speaker-profile learning.+- Never reinstall an ad-hoc/invalidly signed build over `/Applications/FluidVoice Debug.app`. Existing bundle identifier, Team ID, designated requirement, and strict signature checks remain mandatory for any later install.++## 9. Concrete first implementation slice++The first code change after this plan is approved is **the revised Phase 0 plus the synchronizer test harness**, not production AEC and not threshold changes:++1. add VPIO input/output element, graph-render, enabled, bypass, AGC, mute, and ducking property readbacks behind a debug-only probe; replace no production metadata yet;+2. run the A/B/C1/C2/D/E/F/G trials above, including SCK-paired capture, unrelated-process/reference-scope, volume-change, and signed-delay measurement;+3. explicitly rank Apple graph/configuration repair, SCK-paired capture, and current VPIO-residual learnability before any AEC3 recommendation is activated;+4. add an offline `MeetingReferenceSynchronizer` with three-timeline, codec-priming, alignment, drift, synthesized-timing, mask, and gap tests;+5. define the common attribution-engine protocol and aggregate result schema;+6. run the time-boxed out-of-tree AEC3 build/license/size spike only if Phase 0 still requires software cancellation;+7. leave all production transcript/profile behavior unchanged.++Only after that slice establishes what reference Apple receives, whether SCK pairing shares a usable timeline, which reference scope is acceptable, and whether the residual remains learnable should an in-tree AEC3 target be introduced.++## 10. Completion definition++This work is complete only when:++- the Apple reference-path question is answered by repeatable hardware evidence rather than API inference;+- selected-app versus full-mix reference limitations are explicit in product behavior and privacy scope;+- the chosen engine wins a frozen development bake-off and a separate held-out evaluation;+- quiet and overlap speech preservation meets the recording-level gate;+- playback-only microphone text and profile contamination meet the reduction gate;+- dependency, privacy, signing, resource, and recovery reviews pass;+- shadow soak shows no unexplained disagreements or cross-epoch decisions;+- enforcement is reversible and original audio remains recoverable;+- live behavior is either deliberately unchanged or separately validated.++Until then, suppression stays off.diff --git a/MEETING_REFERENCE_SYNCHRONIZER_NOTES.md b/MEETING_REFERENCE_SYNCHRONIZER_NOTES.mdnew file mode 100644index 00000000..45bcd9de--- /dev/null+++ b/MEETING_REFERENCE_SYNCHRONIZER_NOTES.md@@ -0,0 +1,72 @@+# Offline synchronizer first slice++`MeetingReferenceSynchronizer` is a production-inert, deterministic in-memory fixture+API. It converts microphone and ScreenCaptureKit-style reference PCM into 10 ms analysis+frames, preserving valid-sample masks and carrying the microphone source, reference PTS,+and derived session-monotonic timelines.++The slice implements versioned linear conversion metadata, explicit converter delay+compensation, codec priming/remainder/edit-list offsets, bounded input/output budgets,+epoch boundaries, scope/completeness reasons, and synthesized microphone timing freeze and+resynchronization metadata. Missing coverage is represented by masks and unknown reasons;+it is never zero-filled as measured silence.++Frames are consumed in caller arrival order. The first arrival for a sequence number wins;+duplicates and lower-than-the-latest sequence numbers are dropped and counted without adding a+gap or overwriting accepted samples. A forward sequence jump creates an epoch/reset boundary. A+positive interval between the prior accepted end and the current start is marked as unknown gap;+contiguous sample times receive only the boundary marker, so the valid current frame is not+misclassified as missing. Rejected frames consume their sequence number and event insertion is+deduplicated, preventing one malformed frame from manufacturing multiple epochs. Repeated or+overlapping source timestamps are dropped and counted as late/duplicate input. Empty and+pre-origin invalid frames are dropped without claiming a source origin; only a midstream empty+block contributes one inferred unknown interval.++Microphone source origin is established only by the first valid frame. Reference drift maps both+segment starts and segment durations through the same affine scale; output resampling uses the+corresponding session rate so multi-frame placement remains consistent. Each domain is anchored to+its own first valid source coordinate for session placement; the original reference PTS remains in+the source timeline. The exposed lag is configured/mapped alignment (the difference between mapped+first-valid starts), not an observed acoustic delay; `.delayUnresolved` remains present because this+offline fixture has no shared-clock calibration. The value preserves priming, converter, edit-list,+and path-offset coordinates while remaining invariant to unrelated absolute clock origins and+without treating independently selected first samples in a partial output frame as a measured+delay. Synthesized microphone timing freezes adaptation until a real host-time boundary;+sequence/gap boundaries reset state but do not themselves claim measured drift or acoustic delay.++Multichannel PCM is expected to be interleaved and divisible by its declared channel count.+The deterministic fixture policy downmixes by arithmetic channel mean before resampling;+channel-layout-aware mixing and a measured downmix error budget are deferred.++Deferred by design: acoustic delay estimation, adaptive drift estimation, AEC/attribution+engines, file readers, callback integration, and production policy wiring. The configured+reference offset and drift are fixture controls for deterministic replay, not a claim that+those quantities have been measured from a recording.++## Offline replay command++`FluidASRBaselineHost` remains inert in its normal launch path. The replay command is+enabled only by an explicit `--offline-synchronizer-replay` argument or by setting+`FLUID_ASR_OFFLINE_REPLAY=1`; without one of those gates it enters the existing host app+path unchanged. Input is a local JSON fixture supplied on stdin, or with+`--input /absolute/path/to/fixture.json` when the explicit gate is present:++```sh+FluidASRBaselineHost.app/Contents/MacOS/FluidASRBaselineHost \+ --offline-synchronizer-replay --input /absolute/path/to/fixture.json+```++The fixture schema is version `1` and contains only synchronizer configuration, enum+strings, frame metadata, and PCM sample numbers. The CLI rejects unknown keys, unknown+enum values, non-finite numbers, malformed channel arrays, invalid ranges, and inputs+outside both the configured and hard safety budgets before invoking the synchronizer.+The result is one bounded JSON summary containing a canonical SHA-256 replay identity,+frame/mask/timeline ranges, epoch and unknown-reason counts, and numeric diagnostics.+It never emits raw PCM arrays, file paths, transcript text, labels, embeddings, or model+artifacts. Failed gate, validation, and synchronizer-open cases return nonzero status;+normal host behavior and production capture/transcription wiring are unchanged.++The host fixture profile is intentionally bounded to roughly 2,000,000 input samples and+6,000 output frames (about 60 seconds at the default 16 kHz/10 ms grid), with a 64 MiB+encoded-input ceiling and a corresponding output-sample allocation cap. These limits are+applied before decoding/normalization to keep replay memory use finite.diff --git a/MEETING_RELIABILITY_IMPLEMENTATION_PLAN.md b/MEETING_RELIABILITY_IMPLEMENTATION_PLAN.mdnew file mode 100644index 00000000..d0d80c3e--- /dev/null+++ b/MEETING_RELIABILITY_IMPLEMENTATION_PLAN.md@@ -0,0 +1,196 @@+# Meeting reliability: live recovery and evidence-based speaker admission++Date: 2026-09-08+Status: Revision v2 — revised after independent Kimi K3 (OpenCode) and Claude Opus reviews. P0 is ready to scope; P1–P3 behavior changes remain gated on its measurements and the contracts below. Reviewers reviewed v1, not this amended version; this is not a claim of external sign-off.+Scope: implementation plan only; this task does not authorize application changes, deployment, model replacement, or cloud upload of meeting audio.++Execution update (subsequent user authorization, 2026-09-08): the first P0 instrumentation/baseline slice is implemented and locally tested; see `MEETING_RELIABILITY_P0_BASELINE.md`. P0 remains open for controlled acoustic fixtures, annotation and the other documented evidence gaps. No P1–P3 policy change or app reinstall was performed.++## 1. Outcomes and boundaries++Detect sustained live-caption blackouts during audible speech and recover responsive decoder stalls; uninterruptible inference has an explicit degraded/final-reconciliation path, not a live-continuity guarantee. Prevent playback-only microphone artifacts from becoming accepted speech or speaker profiles. Preserve quiet speech, brief acknowledgments, multiple microphone-side people, overlap, local-first processing, and installed macOS privacy grants.++This plan complements MEETING_DIARIZATION_V2_IMPLEMENTATION_PLAN.md. That plan addresses application-track speaker separation and cross-chunk identity. This plan owns live-recognizer health and microphone evidence admission; neither may silently redefine the other's models or thresholds. Existing uncommitted work remains untouched.++Non-goals: restore a "You"/ownership election; equate microphone with one person; force speaker counts; infer acoustic identity from conversational meaning; change dictation; replace AEC or ASR merely on vendor benchmarks; automatically erase ambiguous evidence.++## 2. Evidence and uncertainty++Latest playback-only test: approximately 133 seconds, user reports no microphone-side speech.++- App partials stopped after a forced 20-second utterance reset, at approximately 42 seconds. Count remained 123 while feed advanced; text resumed around 80 seconds. No queue drops were logged. Offline text covers the interval.+- Confirmed source-level recovery defect in the inspected tree: rotateLongUtteranceIfNeeded requires utteranceStartPTS and utteranceTurnID; both are established only by a first partial and cleared on reset. No-first-partial stalls evade it. Binary provenance for this recording remains to be verified.+- Initial decoder failure remains unproven. Other resets recovered quickly. Callback generation races, blank decoder output, reset context loss, conversion effects, and runtime scheduling must be distinguished.+- Live microphone produced no partials. Offline processing retained six microphone phrases, five visible and one marked echo.+- Two microphone chunks had 69/75 and 58/76 words unassigned and fell back to per-turn ASR.+- All six scored turns had 100% scoreable coverage. Five verdicts were unknown; one was containsLocalSpeech. Missing coverage is not the explanation for this fixture.+- The signal scorer's low-explanation run is not a dedicated speech detector. Low correlation can reflect noise, nonlinear residual echo, mismatch, or speech.+- trustedMicrophoneObservationKeys currently excludes text echo only. Observation/profile evidence is chunk-label based; public per-turn embeddings may actually repeat cluster centroids. Do not call those independent clean-turn evidence.++Before claiming causality, pin the exact app build, FluidAudio revision and model artifacts. A currently checked-out source tree is not proof of the binary used in the recording.++## 3. Research basis and limits++The following are public documentation or papers, not independent product benchmarks:++1. Recall.ai separates participant streams when bots/platform support allow it; hybrid processing diarizes within streams. Desktop SDK lacks that separate-stream path: https://docs.recall.ai/docs/diarization+2. Granola uses microphone/system audio and platform speaker tags with shared-device and overlap limitations: https://docs.granola.ai/help-center/taking-notes/speaker-attribution+3. Otter distinguishes generic, known and unknown identity and supports corrections: https://help.otter.ai/hc/en-us/articles/21665587209367-Speaker-Identification-Overview+4. MacWhisper documents local speaker grouping and correction/merging, not perfect identity: https://docs.macwhisper.com/article/32-automatic-speaker-recognition-in-macwhisper+5. Deepgram distinguishes interim text, finalized segments and endpoints. Word-gap endpointing is not an independent first-text watchdog: https://developers.deepgram.com/docs/understand-endpointing-interim-results and https://developers.deepgram.com/docs/utterance-end+6. NN3A jointly models residual echo suppression and near-end speech probability, explicitly balancing suppression and speech distortion: https://arxiv.org/html/2110.08437v1+7. AEC Challenge evaluates single/double talk and recognition quality: https://arxiv.org/abs/2202.13290+8. Coria et al. use overlap-aware embedding weighting and incremental clustering: https://arxiv.org/abs/2109.06483+9. Streaming Sortformer maintains quality-selected speaker memory across chunks; the described four-speaker model is not a drop-in unrestricted desktop solution: https://arxiv.org/html/2507.18446v1+10. WhisperX uses VAD segmentation and alignment; alignment is not proof of speech authenticity: https://arxiv.org/abs/2303.00747+11. Careless Whisper documents non-vocal-duration-associated hallucinations in Whisper, not a measured Parakeet rate: https://arxiv.org/abs/2402.08021+12. pyannote exposes attribution confidence and separate exclusive/overlapping views: https://docs.pyannote.ai/tutorials/confidence-scores and https://docs.pyannote.ai/tutorials/speaker-configuration++Engineering inference: separate capture provenance, speech validity, playback duplication, identity, and word attribution. No cited source proves a competitor solves this exact reset stall or playback fixture.++## 4. Proposed contracts++### 4.1 Live health is separate from utterance formatting++Introduce a testable LiveRecognitionHealth state machine with monotonic clock injection and generation-scoped events. Available wrapper inputs: audio PTS/frames accepted at offer, conversion success, append/process call start/completion, activity-detector output, partial/EOU callback sequence, queue backlog/drop, reset invocation/completion. Decode token/blank progress and reliable reset failure reporting are not public hooks in the inspected dependency; exposing them requires an explicit reviewed dependency patch, not an assumed adapter API. External supervision reads thread-safe progress counters; queue count is available but takeDroppedCount is destructive, so metrics must have one consumer or add a non-destructive snapshot.++States: awaitingSpeech, awaitingFirstText, transcribing, recovering, degraded, stopped. Readiness/loading is explicit and not counted as a post-ready stall. State transitions run in one owner; callbacks carry generation, sequence and originating audio range rather than reading only mutable currentFeedPTS.++Use audio-time for speech exposure and timeline mapping, an explicitly chosen monotonic clock for hangs and user latency. Handle sleep/wake through explicit lifecycle events: invalidate old watchdog deadlines, mark discontinuity, and re-baseline after wake. Verify actual clock semantics; do not infer them from systemUptime's name. Detect capture loss from offer progress versus elapsed time independently of speech. Distinguish no speech, speech without text, slow inference/backlog, callback delivery failure and capture loss. VAD is an imperfect signal: low-confidence activity is not grounds for endless resets or transcript rejection.++Caption segmentation must not automatically imply destruction of the acoustic decoder context. Determine pinned manager capabilities before designing soft-finalize; if unavailable, retain current working endpoint behavior until a tested context-preserving strategy exists.++### 4.2 Evidence-based microphone admission++Introduce per-interval MicrophoneEvidence with typed, independently recorded fields:++- capture protection/era and reference availability;+- playback context: scoreablePlayback, verifiedNoPlayback, playbackExpectedButUnscoreable, or unknownPlaybackContext. In-room mode is not by itself proof of no ambient playback; use the available capture facts and label uncertainty. Missing/zero-filled reference audio must not be interpreted as observed silence;+- speech presence (probability, coverage, detector/version, unknown reason);+- playback explanation (score, confidence, delay/coverage, echo/unknown verdict);+- ASR evidence (timings, alignment agreement, provenance, available model confidence);+- overlap and speaker-embedding quality/provenance;+- admission decision and reason codes.++Do not fabricate confidence for providers that do not expose it. Unknown is distinct from negative evidence. Do not multiply correlated scores as if independent. The same admission function is used before profile construction and final transcript assembly.++Proposed outcomes: acceptedSpeech, likelyPlaybackDuplicate, uncertainCandidate, noSupportedSpeech. Speech confidence and identity confidence remain separate: accepted speech may retain unknown identity. Uncertain text remains locally reviewable with provenance but does not update profiles or silently enter default summaries/exports. Final UI/export semantics require explicit tests and product review; original audio is preserved under existing retention policy.++The policy is not a universal AND gate demanding a valid playback reference: genuine microphone-side speech must remain supportable when reference scoring is unavailable. Conversely, AEC being enabled is not positive proof of speech.++## 5. Phases, deliverables, and exit gates++### P0 — Reproducible baseline and instrumentation (must precede policy tuning)++1. Record installed build identity, code SHA/dirty fingerprint, dependency/model revisions, capture era/route, provider options and processing version. Resolve revision mismatches without silently changing dependencies for the baseline.+2. Preserve original session; use local read-only audio fixtures and separate evaluation outputs. No meeting audio, raw transcript, embeddings or credentials go to reviewers/services without separate authorization.+3. Annotate speech/overlap/speaker truth locally. Preserve the user's report of no speech as evidence; ASR output does not refute it. Label this historical fixture user-reported playback-only, with local audio adjudication and ambiguous intervals flagged separately. Add controlled playback-only captures with the microphone active, known playback and an independently verified silent room, plus synchronized microphone-side speech fixtures. Digital-zero and muted-microphone inputs are ASR controls, not substitutes for acoustic leakage tests. Initial corpus requirement: at least 12 independent negative sessions across three output configurations and two acoustic environments, and separately held-out positive sessions covering quiet/brief/overlap speech; increase coverage if uncertainty remains. Split by recording/device/speaker before policy selection; never reuse held-out data for tuning.+4. Add privacy-safe aggregate metrics to live feed/decode/callback boundaries. Record time-to-first-token, longest speech-without-text interval, decoding latency/backlog, generation changes, dropped frames and recovered audio ranges. Diagnostic raw text is opt-in, local, bounded, and off by default.+5. Add an injectable recognizer adapter, activity detector and clock to permit deterministic state tests without loading CoreML models. Preserve production behavior in this instrumentation change.+6. Measure the pinned ASR on digital silence, low-level noise and controlled AEC residuals, in both whole-chunk and per-turn modes. Begin with bounded 10-minute screening per condition, then at least two hours per negative condition before release claims. Compare detector-gated and ungated processing on development data only. Report false words/phrases per minute; do not infer a Parakeet rate from Whisper research.+7. Verify embedding provenance and supported extraction APIs now, before P3 depends on them. Record independently embeddable accepted-duration coverage by condition, including double-talk. If the necessary independent evidence is unavailable, choose and review a dependency extension or an explicit unknown-identity fallback before P3 implementation; do not discover the limitation at release time. Existing configuration snapshots are inert until runtime enforcement is proven by integration tests.++Exit: reproducible baseline report, exact build provenance or explicit limits, annotated negative/positive fixtures, and deterministic reproduction of the watchdog blind spot. Initial decoder cause must be characterized or explicitly remain open; instrumentation is not a completed fix.++### P1 — Live recovery, independently releasable++Targets: MeetingLiveTrackEngine.swift, MeetingLiveModels.swift, MeetingLiveTranscriptionCoordinator.swift, MeetingLiveBoundedQueue.swift, live test suites; new health/adapter types as needed.++0. Prove reset effectiveness for the reproduced responsive-stall class before selecting reset as the recovery action. The inspected dependency suppresses resetStates errors with try?; expose success/failure via a reviewed patch, or treat reset completion as unverified until a bounded token-progress probe succeeds. A completed reset is never a successful-recovery metric by itself. If reset is ineffective, gate a sequential manager replacement on confirmed retirement, measured load time and memory; do not overlap managers. Cache corruption is a counterexample hypothesis, not the proven incident cause.+1. Implement first-text and in-utterance monitoring from the new state machine; reject stale generation callbacks after resets and stop.+2. Log decode heartbeats independently of callbacks. A watchdog on an actor blocked by inference is insufficient: control/supervision must observe progress without waiting for that actor to finish. Chosen policy: one live manager per track, no quarantined parallel CoreML instance. For responsive decoding without text, perform bounded recovery; for uninterruptible inference, retire outputs by generation, enter degraded state and preserve recording. Full live recovery from a hung backend is explicitly unsupported until a separately reviewed process-isolation design exists. The measured incident had continued consumption, so a 90-second inference hang is an adversarial case, not its established cause.+3. Prototype a timestamped recovery buffer with an initial 60-second cap per track at 16 kHz float mono (3.84 MB per track), enough to retain the observed 38-second interval. P0 may lower or raise the cap based on measured blackout distribution and memory/latency budgets, with a reviewed bounded maximum. Maximum two recovery attempts per minute and at most one live manager per track. Replay work has a wall-time budget and cannot let current audio grow without bound; prioritize current captions and record unreplayed historical ranges. These settings are development proposals, not efficacy claims.+4. Maintain a coverage ledger for original, consumed, committed, replayed, dropped and unresolved audio ranges. Require post-meeting reconciliation of gaps against the existing final-processing result, not blind duplicate ASR work. If final processing does not cover a gap, attempt bounded local catch-up after live model retirement; retain an explicit unresolved marker on cancellation/failure. Report live-only and final coverage separately. A longer ring buffer does not guarantee lossless or fast catch-up.+5. Establish committed versus provisional transcript ranges. Committed live text remains immutable; corrected final text is a separate product. Decode with overlapping audio context, discard only confidently timed replay words wholly before the commit boundary, and retain a provisional reconciliation band for boundary-crossing words. A straddling word must not be blindly discarded. Without reliable word timings, use bounded segment overlap alignment and retain an unresolved boundary rather than asserting lossless deduplication. Test real repeated phrases, changed replay hypotheses and PTS discontinuities. Generation plus source audio range is mandatory; text-only matching is insufficient.+6. Surface recovery/degraded status separately from recording status. Avoid permanent "healthy" based solely on incoming audio.++Exit: deterministic tests for no first partial, token/blank stall, delayed inference, callback reorder, reset completion, stop during recovery, real silence/music and queue loss; replay tests at realistic pacing with no missing/duplicated boundary speech. Proposed target: flag confirmed-speech no-text conditions within 5 seconds after readiness, attempt safe recovery within the next 3 seconds when the backend is responsive. Calibrate and freeze the detector operating point using P0 data; do not promise recovery from an uninterruptible backend.++### P2 — Microphone evidence in shadow mode++Targets: MeetingEchoSignalScorer.swift, MeetingEchoDetector.swift, MeetingProcessingPipeline.swift, MeetingProcessingConfiguration.swift and evidence tests.++1. Add explicit speech-presence evidence using an available local detector after verifying its behavior on AEC-processed residuals. Generic VAD detects leaked speech too; combine with playback-reference evidence and evaluate on transformed/noisy reference cases. A near-end-aware model is an experimental alternative, not a prerequisite or assumed available API.+2. Replace the semantic claim containsLocalSpeech with residual-not-explained evidence internally. The inspected TurnEchoVerdict is Equatable/Sendable in-memory state, not Codable; do not invent an enum migration. Audit all consumers, logs and tests to confirm that scope. Persisted isLikelyEcho and new admission/schema meanings still require versioned compatibility. Revisit its override of text echo only through the new evaluated policy.+3. Carry alignment/fallback provenance and uncertainty across every retry. High word/turn disagreement must not be erased by a successful per-turn text return. Use bounded retries; silence/no-speech remains a valid result.+4. Compute shadow admission alongside existing behavior using the ordered decision contract in section 9. Record aggregate candidate/accepted/uncertain outcomes and reason counts locally; make no user-visible filtering changes yet. Policy structure may be developed on development data only; freeze structure, thresholds and corpus before a single held-out acceptance run. A failed held-out run requires a fresh held-out set for a new quality claim.++Exit: a calibrated policy with false-positive/false-negative trade-offs, including noisy playback, low-volume acknowledgments, no reference, double-talk, route transitions and in-room multi-person capture. No thresholds selected only to remove the five observed phrases.++### P3 — Admission and profile integrity++Targets: trustedMicrophoneObservationKeys, staged/pending microphone turns, MeetingGlobalSpeakerStitcher/embedding index integration, transcript/export/summary consumers, session serialization.++1. Apply evaluated admission before constructing any profile and at transcript finalization. Unit-test all entry paths, including whole-chunk fallback, unreadable reference, word-aligned and per-turn providers, resume/checkpoint and reprocessing.+2. Obtain true independent interval embeddings from validated non-overlap speech. Verify dependency APIs: repeated chunk centroids are not substitutes. If unavailable, conservatively exclude contaminated observations from profile updates and retain accepted text with uncertain identity. Do not gate all valid text on embedding availability.+3. Aggregate trusted duration/quality only over accepted evidence. Profile updates must retain provenance sufficient for session-local recomputation if admission changes; rejected turns must not contaminate siblings through a shared centroid.+4. Lazily create unknown identities; count only identities represented in accepted visible speech. Distinguish unknown person from uncertain speech. Support correcting/merging attribution without overwriting original evidence.+5. Version decision schema and processing cache/checkpoints; old artifacts without evidence are legacy/unknown, never silently trusted. Classification changes bump MeetingProcessingPipeline.pipelineVersion (inspected value 10); coordinate mirrored configuration defaults. Record effective admission-policy/detector versions and flag state in processing-attempt provenance, plus live-policy provenance in the appropriate capture/session record. Checkpoints currently hold the application pass, not microphone evidence; require an exact final-policy fingerprint on resume or explicitly invalidate/rebuild. Existing fingerprints must be proven enforced at runtime. Reprocessing is explicit, transactional, resumable and cancellable. Do not mutate old sessions automatically. Old UI/export reads preserve historical display until explicit reprocessing; rollback cannot reinterpret a new uncertain decision as accepted speech.++Exit: playback-only regression corpus produces zero accepted mic words and zero visible mic identities; genuine short/quiet/overlap speech preservation meets held-out gates. All display/export/summary consumers agree about decision semantics; original evidence remains recoverable.++### P4 — Optional diarization bake-off, not a dependency of P1–P3++Compare current pinned pipeline against overlap-aware alternatives on approved local fixtures. Evaluate pyannote-style segmentation/embedding weighting and streaming cache concepts; any actual model integration requires license, supported speaker count, CoreML/CPU/GPU feasibility, memory and energy checks. Do not add cloud uploads, meeting bots, browser extensions, voice enrollment or persistent voiceprints as an implicit implementation step.++Keep native overlapping timelines separate from exclusive display views. Preserve word attribution uncertainty when multiple speakers overlap; exclusive diarization does not separate voices or recover masked words. Coordinate application-track changes with the existing Diarization V2 plan.++Exit: measured improvement on held-out data justifies cost and migration; otherwise retain current diarizer with P1–P3 controls.++## 6. Evaluation and release gates++Report per-condition results, not only averages:++- Playback-only built-in/external speakers at multiple volumes; headphones; silence; music; fan/keyboard noise; reverberation; nonlinear/distorted playback; missing/wrong reference.+- Quiet microphone-side speech, one-word replies, accents, two nearby people, similar voices, simultaneous playback and speech, real repeated phrases.+- Long continuous speech, onset after silence/reset, hour-long sessions, warmup, system load, queue saturation, route/sample-rate/era changes, stop/resume and cancellation.+- Final metrics: false mic words/minute, false identities/session, missed speech duration, speaker-attributed word errors, speaker confusion/splits/merges and alignment coverage. DER settings must declare overlap handling and boundary collars; silence-only fixtures need false-alarm metrics because DER has no speech denominator.+- Offline live-evaluation metrics: first-text latency from annotated speech onset after readiness, p95/max blackout during annotated speech, omission/duplication at recovery and final coverage reconciliation. Production proxies: time from detector-indicated onset to text, detector-active time without text, decode heartbeat latency/backlog, recovery/degraded duration, CPU/memory/energy and capture drops. Proxies are not ground-truth missed-speech measures.++Proposed gates, to be frozen after baseline and before implementation tuning: zero false accepted mic words/identities on the curated playback-only regression set; at most 1 percentage point absolute increase in missed-speech duration and 1 point speaker-attributed WER on each populated quiet/short/double-talk held-out condition; no new capture drops; no unbounded growth in hour-long runs. Each positive gate condition needs at least 30 minutes annotated audio across three independent recordings, with at least 100 labeled short replies for the short-reply gate; this is a minimum, not sufficient statistical power by itself. Report paired uncertainty intervals and require their upper bounds to meet the non-inferiority margins or report insufficient evidence. Add no increase in false confident mic identities and no more than one additional identity fragmentation event per annotated hour on echo-heavy held-out positives, reporting uncertain-attribution coverage separately so an all-unknown system cannot pass. These are engineering targets, not statistical guarantees.++For the zero-observed-false-positive regression gate, also report an exposure-normalized upper confidence bound and recording-level paired uncertainty; zero on a small corpus is not zero risk. Do not apply a binomial formula directly to words/minute. If using a Poisson rate bound, declare its event/independence assumptions and supplement with session-level results for bursty errors.++Release independently behind liveRecoveryV1 and microphoneAdmissionV1 configuration controls. Test all four flag combinations, not merely all-on/all-off. Roll back behavior independently while retaining compatible evidence records; ensure disabled behavior has regression coverage. Progress from shadow evaluation to opt-in debug to broader rollout only after gates pass. Debug reinstall must preserve com.FluidApp.app, /Applications/FluidVoice Debug.app, verified signature, matching designated requirement and Team ID; stop rather than replace with mismatched signing.++## 7. Work breakdown and review checklist++Suggested implementation slices: (1) provenance/harness, (2) adapter/telemetry, (3) live state tests, (4) bounded live recovery, (5) evidence/shadow policy, (6) independent profile evidence, (7) admission consumers/migration, (8) held-out evaluation/rollout. Each slice has focused tests and a small reversible diff; no broad pipeline rewrite.++Adversarial reviewers should challenge: provenance assumptions; VAD false negatives and leaked-speech false positives; uninterruptible inference/reentrancy; replay boundaries; corrupted sibling embeddings; uncertainty UX; in-room/no-reference policy; migration/rollback; data leakage; hard limits and test feasibility. Demand falsifiable counterexamples and concrete changes. Distinguish confirmed code facts from proposals and unavailable dependency capabilities.++## 8. External review disposition++Both reviewers returned "revise before implementation" on v1. This v2 incorporates adjudicated findings, with empirical questions retained as explicit gates rather than claimed solved. See docs/reviews/meeting-reliability/draft-v1.md, kimi-k3-review.md, claude-opus-review.md and disposition.md. Kimi reviewed relevant source as well as the plan; Claude reviewed only the supplied plan with tools disabled. No reviewer was authorized to implement application changes or access meeting recordings, secrets, credentials or unrelated files.++## 9. Admission decision contract (ordered, shadow-first)++This is a proposed policy structure to freeze on development data before held-out evaluation, not a proven classifier. Numeric operating points remain P2 deliverables. Speech-positive means calibrated acoustic support, not merely ASR text. Speech-negative requires sufficient valid audio and corroborated no-speech evidence, not a single low VAD score. Generic VAD is not independent near-end evidence during playback. ASR conflict includes severe alignment disagreement and uncorroborated fallback; per-turn fallback alone is not automatic rejection when independently supported.++Apply the first matching row; wildcard rows cover remaining combinations:++| Order | Condition | Result/reason |+|---|---|---|+| 1 | Existing online capture-protection admission fails | Excluded by capture policy; preserve evidence. No new AEC-based assumption of speech |+| 2 | No candidate text | No text emitted; if speech evidence exists, record an ASR coverage gap rather than claim silence |+| 3 | Valid corroborated speech-negative audio | noSupportedSpeech; short/quiet duration alone cannot trigger this |+| 4 | Mixed playback/local speech or contradictory evidence | Split only when validated temporal evidence permits; otherwise uncertainCandidate/mixedEvidence, never suppress the whole turn as echo |+| 5 | Strong playback-duplicate evidence, no supported independent speech, no conflict | likelyPlaybackDuplicate; short-text matches alone are insufficient |+| 6 | Speech unknown or ASR conflict remains | uncertainCandidate/insufficientEvidence; no profile update |+| 7 | Speech positive, ASR supported, verifiedNoPlayback | acceptedSpeech; identity may remain unknown |+| 8 | Speech positive, ASR supported, scoreablePlayback AND validated independent microphone-side support | acceptedSpeech; identity evaluated separately |+| 9 | Speech positive during playbackExpectedButUnscoreable or unknownPlaybackContext | uncertainCandidate/referenceUncertain unless separately validated microphone-side evidence satisfies a pre-registered rescue; default policy has no such rescue |+| 10 | Any other combination | uncertainCandidate/unresolved; no profile update |++Unknown context does not silently become accepted. Conversely, in-room/verified-no-playback positives do not require an echo score or an owner voiceprint. This intentionally risks uncertainty for legitimate speech during unscoreable playback; P2 must measure uncertain/missed coverage and cannot ship if positive-speech gates fail. Evaluate the detector's added discrimination within each echo-score/context bucket, not just aggregate AUC. If it adds no useful discrimination, redesign the evidence source instead of counting correlated votes twice.++## 10. Required test artifacts and unresolved decisions++- Annotation format: local interval/word records with recording ID, track, start/end, speaker ID, speech/overlap label and adjudication confidence. Annotator disagreement is retained. Fixture manifests carry provenance and split, not audio in external-review packets.+- Deterministic recovery tests use a fake decoder with exact known tokens/timestamps: each expected token is committed once; overlapping audio may intentionally decode in multiple generations. Audio-range replay across generations is not itself a duplication bug.+- Real replay uses manually verified word-boundary neighborhoods (at least two seconds either side of resets) against annotated speech, not offline ASR as unquestioned ground truth. Report insertions/deletions, genuine repetition preservation and alignment uncertainty. No fixed 0.9 text-containment heuristic is presumed sufficient.+- P0 must settle dependency hooks/reset observability, exact binary/model provenance and independent-embedding feasibility. Missing APIs require a separately scoped patch; their absence is not papered over with fake confidence or repeated centroids.+- P1 must demonstrate responsive-stall recovery and independently demonstrate hang detection/gap reconciliation. Add fake never-returning inference and bounded stop tests; stop/UI return cannot await the hung manager indefinitely. Keep capture ownership and post-meeting handback safe until the worker really retires; mark final processing deferred if model/resource handback is unsafe.+- P2 must validate the proposed decision structure and choose thresholds using development data only. No-reference positives, playback-with-unreadable-reference negatives and mixed-turn short interjections are mandatory adversarial fixtures.+- P3 must settle the clean-embedding API or explicit identity fallback, versioned consumer contract and uncertainty review UI before user-visible activation. Accepted speech must not require a profile; fallback must not create a new person per fragment.+- Product/engineering review must approve uncertain-content visibility/export behavior and revised quantitative gates before implementation activation. No new cloud upload, recording retention expansion, or model download is implied by this planning task.diff --git a/MEETING_RELIABILITY_P0_BASELINE.md b/MEETING_RELIABILITY_P0_BASELINE.mdnew file mode 100644index 00000000..d403586e--- /dev/null+++ b/MEETING_RELIABILITY_P0_BASELINE.md@@ -0,0 +1,94 @@+# Meeting reliability P0 baseline++Date: 2026-09-08 (local). Status: instrumentation and initial measurement slice; **P0 exit gate remains open**. No recovery/admission policy, dependency revision, installed app, privacy permission, or saved recording was changed.++## Execution plan and scope++1. Preserve the existing recording and fingerprint local source, installed binary, processing metadata and staged models.+2. Add an injectable recognizer/clock and opt-in numeric wrapper diagnostics without changing live rotation policy. Reproduce the no-first-partial blind spot deterministically.+3. Measure digital silence and seeded low noise using the pinned offline ASR, in 60-second and 3-second calls. Keep test completion separate from speech-quality verdicts.+4. Obtain an Orchestrate adversarial review, address evidence/reporting defects, and document remaining controlled-corpus gates before P1.++## Evidence and provenance++Local machine-readable inventory: `/private/tmp/fv-reliability-p0-20260908/inventory-v3.json`. Earlier inventory files are superseded.++- Source HEAD: `b701716177bebba97a82d68aa9aa8f765292a8a2`; dirty tree preserved. Inventory records the tracked-diff hash, not an assertion that untracked files are covered.+- Package declaration, both resolved files, and local FluidAudio checkout agree on `3fd63887eef1dc25edea8263ce4b44aa854d898b`.+- Installed `/Applications/FluidVoice Debug.app`: `com.FluidApp.app`, version 1.6.10, build 22, Team `A8467RRA3D`. Deep/strict signature verification succeeded using host trust access. Launcher SHA256 `14948a43786af513c65fb53c0d4b07c0afdf1e77823321b83d9493278f7d5377`; debug dylib SHA256 `f9907c5e5cb623732c9f76627d1cf38803b4a1e370501f848e16e8988be27000`.+- Exact historical binary-to-source match is **unproven**: no embedded build manifest. A present-day hash does not establish recording-time identity.+- Historical session SHA256 `fbccf8fce03fb83a87643192e47e164a5d588726e2ad45f273d6d6383d3de557`; all six chunk hashes match stored values. Inventory contains aggregate metadata only, no transcript or audio.+- User-reported playback-only, approximately 133 seconds. This report preserves that account; no independent listening/annotation adjudication was performed. Application capture was ScreenCaptureKit, 48 kHz stereo; microphone was voice-processing I/O, 48 kHz mono. No stored capture drops/chunk discontinuities or session failures were found.+- Selected log lines 4825–5004 show app partial count 123 unchanged while audio advances 30.1 seconds over 30.071 seconds of sampled wall time. This is a sampled lower bound, not the full blackout duration or proof of audible speech. A silent microphone having no partials is expected.+- Offline staged model manifest SHA256 `b39baf9bf54a0c70e9821989da9cdde32ca3f1f4e74894efa85f982320affad3`; 22 artifacts checked. Inventory reports both hash agreement and exact repository file-set agreement. Hugging Face artifact revision is unavailable; hashes identify local content, not publisher authenticity. Live/diarizer model provenance is not yet complete.++## Instrumentation and deterministic tests++`MeetingLiveTrackEngine` now accepts a recognizer and monotonic clock. Debug diagnostics are opt-in with `FLUIDVOICE_MEETING_LIVE_DIAGNOSTICS=1`; release diagnostics are forced off. New counters contain numbers only. Existing logging behavior outside this change is not a privacy audit.++Tests exercise the actual engine conversion/append/process/rotation path with a fake recognizer, without capture or model loading:++- 40 seconds of feed with no first partial invokes no reset.+- A forced 20-second boundary followed by 38 seconds without partials does not trigger another recovery, despite continued process returns.+- Explicitly disabled diagnostics do not accumulate counters.+- Injected seven-second processing delay is measured without sleeping.+- A thrown process call is not counted as a successful return.++These characterize the existing recovery blind spot; they do not establish why the real decoder first stopped emitting text. `recognizerResetReturned` means wrapper return only. `wrapperGeneration` is a reset ordinal, not a callback-origin epoch. First-partial latency starts after reset returns. Actor-local counters cannot supervise a blocked actor, measure decoder token/blank progress, establish speech activity, or prove recovery. Activity-detector injection and independently readable watchdog progress remain pending.++## Synthetic negative screen++The isolated `com.FluidApp.ASRBaselineHost` requires app sandboxing and rejects microphone/network entitlements at runtime. It uses direct local CoreML constructors, with no model-download API. It never opens the historical recording. The installed Debug app is not used or replaced.++Default exposure is 600 seconds for each of four condition/mode combinations: digital silence and seeded uniform noise at peak 0.001, each in 60-second and 3-second calls. This is 40 minutes of synthetic ASR exposure, not 40 minutes of independent acoustic recordings. No VAD, AEC, diarization, production provider, or live EOU recognizer is exercised. Three-second calls approximate short segmentation; they are not the production fallback pipeline.++Pinned `ASR/Parakeet/AsrManager.swift:524–548` awaits throwing `resetDecoderState()` before returning a successful array transcription. `initialize(models:)` installs supplied local models. This source check supports sequential state-reset semantics, not universal offline safety; host entitlements enforce the screen's network boundary. Errors abort the screen. The between-call wall budget cannot interrupt a stuck CoreML invocation.++Every report has `verdict: measured_only`. XCTest success means execution/schema checks completed, even if all inputs generated text. Nonempty outputs and whitespace-delimited words are reported; text is not attached. No confidence interval or release-quality conclusion is justified by repeated deterministic silence.++Completed measurement in `screen-v3.xcresult`; JSON attachment `/private/tmp/fv-reliability-p0-20260908/attachments-v3/71D9A149-34D2-4880-9039-A1DD95D5E4DA.json`. Model load took 11.654 seconds.++| Input | Call length | Audio exposure | Calls | Nonempty outputs | Words/minute |+| --- | --- | --- | --- | --- | --- |+| Digital silence | 60 seconds | 600 seconds | 10 | 0 | 0 |+| Digital silence | 3 seconds | 600 seconds | 200 | 0 | 0 |+| Seeded low noise | 60 seconds | 600 seconds | 10 | 0 | 0 |+| Seeded low noise | 3 seconds | 600 seconds | 200 | 0 | 0 |++Zero words in these controls does **not** explain or exclude playback-leakage hallucinations. The v3 test bundle had eight passing tests and one failing path-alias fixture test (nonexistent paths cannot reliably resolve aliases); the inference measurement itself completed. That fixture was corrected to create an actual local directory and alias; final rerun status is recorded below.++Final rerun: `screen-v4.xcresult` — **9 passed, 0 failed, 0 skipped**. Final attachment: `/private/tmp/fv-reliability-p0-20260908/attachments-v4/34F372C3-E600-4570-8255-3C4E97491046.json`. Eight Python inventory regression tests also pass; `git diff --check` is clean. Existing unrelated compiler actor-isolation warnings remain; no production-app test/install or release build was performed.++The final rerun again measured 420 calls / 2,400 audio seconds with zero nonempty outputs and zero words; warm model load was 0.132 seconds. Repeated runs are not independent acoustic exposure.++Final tested file SHA256 fingerprints (supplement the tracked-diff inventory):++```text+8e1e5c0634d9aaa18181b18259d0377ead80772cef52764c1f70c67b9d9c37a0 MeetingLiveTrackEngine.swift+e94f7c8a3d7a0234d26ec3ae7c5bb0d4c5c2b5bc4745f0d27d07410d41f5450c MeetingLiveP0DiagnosticsTests.swift+d2d920fd5c2db77a71ca67171084ccf74b79b3f09c57f6bf8107bb6a9e62b8eb MeetingReliabilityNegativeScreenTests.swift+2c35e544313e6a978915fbb75ee26c8e84c571efed04e59a6c346f219e994e06 meeting_reliability_baseline.py+8643d41129b662114a7b626fde122436802b2762d597d6170cf65be928c13abc test_meeting_reliability_baseline.py+```++## Embedding API audit++At the pinned revision, `Diarizer/Offline/Utils/OfflineReconstruction.swift:337–345` assigns the cluster centroid to each returned `TimedSpeakerSegment.embedding`; trimming/merging retains it. These are not independent clean-turn embeddings.++The offline extraction implementation is internal (`Offline/Extraction/OfflineEmbeddingExtractor.swift`). A separate public `EmbeddingExtractor.getEmbeddings(audio:masks:)` exists, with a fixed `[3,160000]` waveform shape and legacy WeSpeaker model interface. Its existence does not establish compatibility with offline feature/PLDA space or current profile similarity thresholds. No extraction compatibility or independent accepted-duration coverage was measured. P3 must review a compatible interval-extraction extension or an explicit uncertain-identity fallback before trusting independent evidence; this slice chooses neither implementation.++## Adversarial review disposition++Orchestrate standard route completed a read-only Grok 4.6 review of the five scoped code/test/tool files (`editedTree=false`). An earlier route failed; the heavy route was blocked by host policy and was not bypassed. No private meeting audio, transcript, embeddings, or credentials were supplied.++Findings addressed: added an explicit measurement-only verdict; renamed reset return/generation counters and moved latency start after reset return; added exact-tree inventory checks; made missing capture-drop schema fail closed and excluded missing speaker IDs from person counts. Removed hard-coded recording-condition inference from the reusable inventory. Verified the pinned offline transcription reset implementation locally in response to the review's uncertainty. Review occurred before these amendments; it is not external sign-off on the amended tree.++## Reproduction and remaining exit gates++Run inventory regressions with `python3 -m unittest discover -s tools -p test_meeting_reliability_baseline.py`. The inventory CLI requires explicit repo/app/session/model-manifest paths; log parsing additionally requires a caller-selected same-session line range. Output creation is exclusive.++Build the existing `FluidASRBaseline` scheme with staged `FLUID_ASR_BASELINE_INPUTS`, using the stable local signing identity. Then run the two P0 XCTest classes with `TEST_RUNNER_FLUID_RELIABILITY_NEGATIVE_SCREEN=1`. Test-run settings accept `FLUID_RELIABILITY_SECONDS` (60–600, default 600) and `FLUID_RELIABILITY_WALL_SECONDS` (60–1800, default 1200); with xcodebuild forward them using `TEST_RUNNER_`. Retain the xcresult and its JSON attachment. Never install this baseline host over the Debug app.++The packaged CTranscribe framework was copied with broken version symlinks by the current build inputs. Only the generated baseline-host copy was repaired, originals preserved in a temporary backup, then re-signed and deep/strict verified. Rebuilds may recreate this packaging defect. This is not a fix to the upstream framework or production packaging.++P0 is not complete until controlled active-microphone playback/AEC-residual and positive speech fixtures are collected and independently annotated: at least 12 independent negative sessions across three output configurations and two acoustic environments, with separate quiet/brief/overlap held-out positives. The human acoustic setup/speech truth cannot be supplied by synthetic samples. Freeze recording/device/speaker splits before tuning; extend negative exposure to the plan's release minimum before claims. Complete effective runtime options/model provenance, activity instrumentation, embedding coverage and detector-gated development comparisons. The original decoder cause remains open.diff --git a/Sources/Fluid/AppDelegate.swift b/Sources/Fluid/AppDelegate.swiftindex dd5b9720..c5683de2 100644--- a/Sources/Fluid/AppDelegate.swift+++ b/Sources/Fluid/AppDelegate.swift@@ -25,9 +25,24 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelefunc applicationDidFinishLaunching(_ notification: Notification) {#if DEBUG+ // Trial A and C2 autoruns must return before Core Audio observers, logging, AppServices, and UI+ // startup. The autorun owns its bounded SCK stream and exits after one report.+ if MeetingExternalReferenceTrialAAutorun.startIfRequested() {+ return+ }+ if MeetingSCKPairedAutorun.startIfRequested() {+ return+ }// Must precede every Core Audio observer. Disabled unless explicitly// requested through the Phase 0 diagnostics environment.AudioTopologyDiagnostics.shared.startIfRequested()+ // App-hosted XCTest otherwise starts the normal UI/audio services alongside the+ // exclusive VPIO hardware probe. Keep that opt-in diagnostic launch isolated.+ if ProcessInfo.processInfo.environment["FLUIDVOICE_MIC_PHASE1"] != nil+ || ProcessInfo.processInfo.environment["FLUIDVOICE_VPIO_ACOUSTIC"] == "1"+ {+ return+ }#endif// Bring up file logging + crash handlers immediately during launch._ = FileLogger.shareddiff --git a/Sources/Fluid/Persistence/KeychainService.swift b/Sources/Fluid/Persistence/KeychainService.swiftindex 8f4256f1..e78e9ab2 100644--- a/Sources/Fluid/Persistence/KeychainService.swift+++ b/Sources/Fluid/Persistence/KeychainService.swift@@ -41,6 +41,11 @@ final class KeychainService {private let testingBackend: TestingBackend?private init() {+ // The ASR baseline host must never touch credentials; any transitive+ // `SettingsStore.shared` initialization reaches this stored-property singleton.+ #if FLUID_ASR_BASELINE+ fatalError("KeychainService is unavailable in the ASR baseline host")+ #endifself.testingBackend = nil}diff --git a/Sources/Fluid/Services/FileLogger.swift b/Sources/Fluid/Services/FileLogger.swiftindex 1430fffe..352e2746 100644--- a/Sources/Fluid/Services/FileLogger.swift+++ b/Sources/Fluid/Services/FileLogger.swift@@ -24,7 +24,13 @@ final nonisolated class FileLogger: @unchecked Sendable {private static let sigUnknownLine = Array("[CRASH] Fatal signal UNKNOWN\n".utf8)private init() {+ #if FLUID_ASR_BASELINE+ // Baseline runs log to an isolated per-process temp root, never user Library.+ let baseDirectory = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)+ .appendingPathComponent("FluidASRBaseline-\(UUID().uuidString)", isDirectory: true)+ #elselet baseDirectory = self.fileManager.urls(for: .libraryDirectory, in: .userDomainMask).first ?? URL(fileURLWithPath: NSTemporaryDirectory())+ #endifself.logDirectory = baseDirectory.appendingPathComponent("Logs/Fluid", isDirectory: true)self.logFileURL = self.logDirectory.appendingPathComponent("Fluid.log", isDirectory: false)self.backupLogURL = self.logDirectory.appendingPathComponent("Fluid.log.1", isDirectory: false)diff --git a/Sources/Fluid/Services/FluidAudioProvider.swift b/Sources/Fluid/Services/FluidAudioProvider.swiftindex ee3837a2..2904aaa4 100644--- a/Sources/Fluid/Services/FluidAudioProvider.swift+++ b/Sources/Fluid/Services/FluidAudioProvider.swift@@ -1,4 +1,63 @@import Foundation++/// Rejection of a meeting ASR configuration outside the fixed supported policy.+nonisolated enum MeetingProviderOptionsError: Error, Equatable {+ case unsupportedASRModel(String)+ case unsupportedLanguageCode(String)+ case unsupportedFeature(String)+}++/// Immutable provider options for the opt-in meeting post-processing path. Only the fixed+/// Parakeet TDT v2 English policy is supported; `resolve` rejects anything else instead of+/// silently coercing it (e.g. labelling a v3 model "v2" or dropping a requested feature).+nonisolated struct MeetingProviderOptions: Equatable, Sendable {+ let model: SettingsStore.SpeechModel+ let vocabularyBoostingEnabled: Bool+ let pronunciationMatchingEnabled: Bool+ let customDictionaryRewritingEnabled: Bool+ let experimentalUnifiedFinalEnabled: Bool++ static func resolve(+ _ configuration: MeetingFinalProcessingConfiguration+ ) throws -> MeetingProviderOptions {+ let model = SettingsStore.SpeechModel(rawValue: configuration.asrModel)+ guard model == .parakeetTDTv2 else {+ throw MeetingProviderOptionsError.unsupportedASRModel(configuration.asrModel)+ }+ guard configuration.languageCode == MeetingFinalProcessingConfiguration.defaultLanguageCode else {+ throw MeetingProviderOptionsError.unsupportedLanguageCode(configuration.languageCode)+ }+ guard !configuration.vocabularyBoostingEnabled else {+ throw MeetingProviderOptionsError.unsupportedFeature("vocabularyBoosting")+ }+ guard !configuration.pronunciationMatchingEnabled else {+ throw MeetingProviderOptionsError.unsupportedFeature("pronunciationMatching")+ }+ guard !configuration.customDictionaryRewritingEnabled else {+ throw MeetingProviderOptionsError.unsupportedFeature("customDictionaryRewriting")+ }+ guard !configuration.experimentalUnifiedFinalEnabled else {+ throw MeetingProviderOptionsError.unsupportedFeature("experimentalUnifiedFinal")+ }+ return MeetingProviderOptions(+ model: .parakeetTDTv2,+ vocabularyBoostingEnabled: false,+ pronunciationMatchingEnabled: false,+ customDictionaryRewritingEnabled: false,+ experimentalUnifiedFinalEnabled: false+ )+ }+}++/// Immutable enhancement options pinned at construction for offline/baseline runs. When+/// provided, these values replace the live `SettingsStore` reads for these enhancements.+/// Full settings isolation also requires an explicit model and disabled vocabulary boosting.+nonisolated struct FluidAudioProviderEnhancementOptions {+ let experimentalUnifiedFinalEnabled: Bool+ let pronunciationMatchingEnabled: Bool+ let customDictionaryEntries: [SettingsStore.CustomDictionaryEntry]+}+#if arch(arm64)import FluidAudio@@ -49,17 +108,71 @@ final class FluidAudioProvider: TranscriptionProvider {/// Optional model override - if set, uses this model instead of the global setting./// Used for downloading specific models without changing the active selection.- var modelOverride: SettingsStore.SpeechModel?+ let modelOverride: SettingsStore.SpeechModel?private let configureWordBoosting: Bool- init(modelOverride: SettingsStore.SpeechModel? = nil, configureWordBoosting: Bool = true) {+ /// Opt-in pinned meeting options. When nil (legacy dictation), enhancement switches and+ /// the custom dictionary are still read live from `SettingsStore` at each call site.+ private let meetingOptions: MeetingProviderOptions?+ /// Explicitly pinned enhancement options for offline/baseline runs. Precedence for each+ /// effective getter: fixed meeting policy, then this, then live `SettingsStore`.+ private let enhancementOptions: FluidAudioProviderEnhancementOptions?++ init(+ modelOverride: SettingsStore.SpeechModel? = nil,+ configureWordBoosting: Bool = true,+ enhancementOptions: FluidAudioProviderEnhancementOptions? = nil+ ) {self.modelOverride = modelOverrideself.configureWordBoosting = configureWordBoosting+ self.meetingOptions = nil+ self.enhancementOptions = enhancementOptions+ }++ /// Opt-in meeting post-processing provider. The model is pinned immutably and every+ /// dictation enhancement (vocabulary store, pronunciation training/matching, custom+ /// dictionary, experimental unified final) is disabled explicitly rather than read from+ /// live settings. Unsupported configurations are rejected, never silently coerced.+ init(meetingConfiguration configuration: MeetingFinalProcessingConfiguration) throws {+ let options = try MeetingProviderOptions.resolve(configuration)+ self.modelOverride = options.model+ self.configureWordBoosting = false+ self.meetingOptions = options+ self.enhancementOptions = nil+ }++ private var effectiveExperimentalUnifiedFinalEnabled: Bool {+ self.meetingOptions?.experimentalUnifiedFinalEnabled+ ?? self.enhancementOptions?.experimentalUnifiedFinalEnabled+ ?? SettingsStore.shared.experimentalParakeetUnifiedFinalEnabled+ }++ private var effectivePronunciationMatchingEnabled: Bool {+ self.meetingOptions?.pronunciationMatchingEnabled+ ?? self.enhancementOptions?.pronunciationMatchingEnabled+ ?? SettingsStore.shared.pronunciationMatchingEnabled+ }++ private var effectiveCustomDictionaryEntries: [SettingsStore.CustomDictionaryEntry] {+ if let meetingOptions = self.meetingOptions, !meetingOptions.customDictionaryRewritingEnabled {+ return []+ }+ return self.enhancementOptions?.customDictionaryEntries+ ?? SettingsStore.shared.customDictionaryEntries}#if DEBUGfunc setWordBoostingActiveForTesting(_ active: Bool) {self.isWordBoostingActive = active}++ /// Read-only snapshot of the actual effective enhancement getters for baseline tests.+ var effectiveEnhancementOptionsForTesting: FluidAudioProviderEnhancementOptions {+ FluidAudioProviderEnhancementOptions(+ experimentalUnifiedFinalEnabled: self.effectiveExperimentalUnifiedFinalEnabled,+ pronunciationMatchingEnabled: self.effectivePronunciationMatchingEnabled,+ customDictionaryEntries: self.effectiveCustomDictionaryEntries+ )+ }#endiffunc prepare(progressHandler: ((ModelPreparationProgress) -> Void)? = nil) async throws {@@ -216,7 +329,7 @@ final class FluidAudioProvider: TranscriptionProvider {let startedAt = Date().timeIntervalSince1970let result: ASRResult- if SettingsStore.shared.experimentalParakeetUnifiedFinalEnabled,+ if self.effectiveExperimentalUnifiedFinalEnabled,samples.count > Self.incrementalChunkingThresholdSamples,let incrementalManager = self.finalAsrManager ?? self.streamingAsrManager{@@ -237,7 +350,7 @@ final class FluidAudioProvider: TranscriptionProvider {result = try await fullPreviewManager.transcribe(samples, source: AudioSource.microphone)}} else {- if !SettingsStore.shared.experimentalParakeetUnifiedFinalEnabled+ if !self.effectiveExperimentalUnifiedFinalEnabled|| samples.count < self.incrementalAcceptedSampleCount{self.resetIncrementalSession()@@ -267,7 +380,7 @@ final class FluidAudioProvider: TranscriptionProvider {/// growing recording on every live-preview tick.func incrementalPreviewDeltaStart(totalSampleCount: Int) -> Int? {Self.incrementalPreviewDeltaRange(- enabled: SettingsStore.shared.experimentalParakeetUnifiedFinalEnabled,+ enabled: self.effectiveExperimentalUnifiedFinalEnabled,hasSession: self.incrementalSession != nil,acceptedSampleCount: self.incrementalAcceptedSampleCount,totalSampleCount: totalSampleCount@@ -339,7 +452,7 @@ final class FluidAudioProvider: TranscriptionProvider {userInfo: [NSLocalizedDescriptionKey: "ASR manager not initialized"])}- let shouldCapture = SettingsStore.shared.pronunciationMatchingEnabled+ let shouldCapture = self.effectivePronunciationMatchingEnabledawait manager.setPronunciationCustomizationEnabled(shouldCapture)do {let result = try await manager.transcribe(samples, source: AudioSource.microphone)@@ -377,7 +490,7 @@ final class FluidAudioProvider: TranscriptionProvider {)}- if SettingsStore.shared.experimentalParakeetUnifiedFinalEnabled,+ if self.effectiveExperimentalUnifiedFinalEnabled,samples.count > Self.incrementalChunkingThresholdSamples,self.incrementalSession != nil{@@ -536,9 +649,9 @@ final class FluidAudioProvider: TranscriptionProvider {private func transcribeFinalResult(_ samples: [Float], manager: AsrManager) async throws -> (result: ASRTranscriptionResult, tokenTimings: [TokenTiming]?, textMayBeCorrected: Bool) {- let matchingEnabled = SettingsStore.shared.pronunciationMatchingEnabled && samples.count <= 16_000 * 15+ let matchingEnabled = self.effectivePronunciationMatchingEnabled && samples.count <= 16_000 * 15let dictionaryLabels = Self.dictionaryLabels(- from: SettingsStore.shared.customDictionaryEntries+ from: self.effectiveCustomDictionaryEntries)let profiles: [PronunciationDictionaryProfile]if matchingEnabled {@@ -854,8 +967,17 @@ final class FluidAudioProvider: TranscriptionProvider {private(set) var isWordBoostingActive: Bool = falseprivate(set) var boostedVocabularyTermsCount: Int = 0- init(modelOverride: SettingsStore.SpeechModel? = nil, configureWordBoosting: Bool = true) {- // Intel stub - parameter ignored+ init(+ modelOverride: SettingsStore.SpeechModel? = nil,+ configureWordBoosting: Bool = true,+ enhancementOptions: FluidAudioProviderEnhancementOptions? = nil+ ) {+ // Intel stub - parameters ignored+ }++ init(meetingConfiguration configuration: MeetingFinalProcessingConfiguration) throws {+ // Intel stub - validates the meeting policy but never loads FluidAudio models+ _ = try MeetingProviderOptions.resolve(configuration)}func prepare(progressHandler: ((ModelPreparationProgress) -> Void)?) async throws {diff --git a/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTrackEngine.swift b/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTrackEngine.swiftindex 1e6434d5..54b7cbc7 100644--- a/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTrackEngine.swift+++ b/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTrackEngine.swift@@ -6,6 +6,41 @@ import Foundationimport FluidAudioimport os+/// Only public dependency operations are represented. A completed reset does NOT prove that+/// decoder state reset succeeded (the dependency currently suppresses internal reset errors).+nonisolated protocol MeetingLiveRecognizer: Sendable {+ func prepareModels() async throws+ func appendAudio(_ buffer: AVAudioPCMBuffer) async throws+ func processBufferedAudio() async throws+ func reset() async+ func setEouCallback(_ callback: @escaping @Sendable (String) -> Void) async+ func setPartialTranscriptCallback(_ callback: @escaping @Sendable (String) -> Void) async+}++extension StreamingEouAsrManager: MeetingLiveRecognizer {+ func prepareModels() async throws { try await self.loadModelsFromHuggingFace() }+}++/// Numeric wrapper observations, not speech truth, token counts, or a recovery verdict.+/// No audio, transcript, embedding, route identifiers or user identifiers are retained here.+nonisolated struct MeetingLiveDiagnosticSnapshot: Sendable {+ var convertedBuffers = 0+ var conversionFailures = 0+ var appendStarts = 0+ var appendCompletions = 0+ var processStarts = 0+ var processCompletions = 0+ var appendOrProcessErrors = 0+ var partialCallbacks = 0+ var eouCallbacks = 0+ var resetInvocations = 0+ var recognizerResetReturned = 0+ var wrapperGeneration = 0+ var maxProcessSeconds: Double = 0+ var firstPartialAfterResetSeconds: Double?+ var feedSeconds: Double = 0+}+/// Drives one `StreamingEouAsrManager` for one capture track. Two instances are required — the/// manager holds a single decoder and endpointing state, so interleaving two audio sources would/// corrupt both transcripts.@@ -30,7 +65,11 @@ actor MeetingLiveTrackEngine {/// The recognizer buffers a full chunk before it can decode it, so the speech behind the first/// token started at least one chunk before the audio we were feeding when that token arrived.private static let decoderLookback = CMTime(value: CMTimeValue(chunkSize.durationMs), timescale: 1000)- private let manager: StreamingEouAsrManager+ private let manager: any MeetingLiveRecognizer+ private let uptime: @Sendable () -> Double+ private let diagnosticsEnabled: Bool+ private var diagnosticSnapshot = MeetingLiveDiagnosticSnapshot()+ private var diagnosticResetUptime: Double?private let targetFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 16_000, channels: 1, interleaved: false)!@@ -68,9 +107,21 @@ actor MeetingLiveTrackEngine {private var onDegraded: DegradedHandler?private var onReady: ReadyHandler?- init(kind: MeetingAudioTrackKind) {+ init(+ kind: MeetingAudioTrackKind,+ recognizer: (any MeetingLiveRecognizer)? = nil,+ uptime: @escaping @Sendable () -> Double = { ProcessInfo.processInfo.systemUptime },+ diagnosticsEnabled: Bool? = nil+ ) {self.kind = kind- self.manager = StreamingEouAsrManager(chunkSize: Self.chunkSize)+ self.manager = recognizer ?? StreamingEouAsrManager(chunkSize: Self.chunkSize)+ self.uptime = uptime+ #if DEBUG+ self.diagnosticsEnabled = diagnosticsEnabled+ ?? (ProcessInfo.processInfo.environment["FLUIDVOICE_MEETING_LIVE_DIAGNOSTICS"] == "1")+ #else+ self.diagnosticsEnabled = false+ #endif}func configure(@@ -97,7 +148,7 @@ actor MeetingLiveTrackEngine {try Task.checkCancellation()// Shares FluidAudio's default cache dir, so a Parakeet Flash download for// dictation already satisfies this — first-run cost is paid at most once.- try await manager.loadModelsFromHuggingFace()+ try await manager.prepareModels()guard !Task.isCancelled else { return }await self.warmUp()guard !Task.isCancelled else { return }@@ -115,7 +166,7 @@ actor MeetingLiveTrackEngine {self.drainTask?.cancel()await self.drainTask?.valueself.drainTask = nil- await self.manager.reset()+ await self.resetRecognizer()}/// Fast, synchronous, non-blocking hand-off from the capture tee. On saturation the queue drops@@ -146,15 +197,15 @@ actor MeetingLiveTrackEngine {if let channel = silence.floatChannelData?[0] {channel.update(repeating: 0, count: Int(frames))}- let started = ProcessInfo.processInfo.systemUptime+ let started = self.uptime()do {try await self.manager.appendAudio(silence)try await self.manager.processBufferedAudio()} catch {self.diag("warmup failed (continuing): \(error)")}- await self.manager.reset()- self.diag(String(format: "warmup complete in %.2fs", ProcessInfo.processInfo.systemUptime - started))+ await self.resetRecognizer()+ self.diag(String(format: "warmup complete in %.2fs", self.uptime() - started))}private func markReady() {@@ -188,6 +239,7 @@ actor MeetingLiveTrackEngine {private func consume(_ sample: MeetingLiveSampleCopy.Sample) async {guard let converted = self.convert(sample.buffer) else {+ if self.diagnosticsEnabled { self.diagnosticSnapshot.conversionFailures += 1 }self.diag("convert FAILED (sr=\(sample.buffer.format.sampleRate) ch=\(sample.buffer.format.channelCount))")return}@@ -195,12 +247,29 @@ actor MeetingLiveTrackEngine {self.lastConsumedPTS = sample.pts + sample.durationself.diagSamplesConsumed += 1self.diagSecondsConsumed += sample.duration.seconds+ if self.diagnosticsEnabled {+ self.diagnosticSnapshot.convertedBuffers += 1+ self.diagnosticSnapshot.feedSeconds += sample.duration.seconds+ }self.logFlowIfDue()do {+ if self.diagnosticsEnabled { self.diagnosticSnapshot.appendStarts += 1 }try await self.manager.appendAudio(converted)+ if self.diagnosticsEnabled {+ self.diagnosticSnapshot.appendCompletions += 1+ self.diagnosticSnapshot.processStarts += 1+ }+ let processStarted = self.diagnosticsEnabled ? self.uptime() : 0try await self.manager.processBufferedAudio()+ if self.diagnosticsEnabled {+ self.diagnosticSnapshot.processCompletions += 1+ self.diagnosticSnapshot.maxProcessSeconds = max(+ self.diagnosticSnapshot.maxProcessSeconds, self.uptime() - processStarted+ )+ }await self.rotateLongUtteranceIfNeeded()} catch {+ if self.diagnosticsEnabled { self.diagnosticSnapshot.appendOrProcessErrors += 1 }self.diag("appendAudio/process error: \(error)")self.onDegraded?(self.kind, "Live captions hit an error and resynced.")await self.resetUtterance()@@ -218,7 +287,7 @@ actor MeetingLiveTrackEngine {let turnID = self.utteranceTurnIDelse { return }let openSeconds = end.seconds - start.seconds- let quietSeconds = self.lastPartialUptime.map { ProcessInfo.processInfo.systemUptime - $0 }+ let quietSeconds = self.lastPartialUptime.map { self.uptime() - $0 }let hitCeiling = openSeconds > Self.maxOpenUtteranceSecondslet decoderQuiet = (quietSeconds ?? 0) > Self.partialStallSeconds && !self.lastPartialText.isEmptyguard hitCeiling || decoderQuiet else { return }@@ -227,7 +296,7 @@ actor MeetingLiveTrackEngine {self.utteranceTurnID = nilself.lastPartialText = ""self.lastPartialUptime = nil- await self.manager.reset()+ await self.resetRecognizer()guard !text.isEmpty else { return }self.lastUtteranceEndPTS = endself.diagUtterances += 1@@ -242,7 +311,7 @@ actor MeetingLiveTrackEngine {/// Once every 5s of wall clock: proves audio is still reaching the recognizer, and how long the/// current utterance has been open without an endpoint.private func logFlowIfDue() {- let now = ProcessInfo.processInfo.systemUptime+ let now = self.uptime()guard now - self.diagLastFlowLog >= 5 else { return }self.diagLastFlowLog = nowlet openFor = self.utteranceStartPTS.map { start in@@ -255,12 +324,25 @@ actor MeetingLiveTrackEngine {self.diagPartials, self.diagUtterances, self.diagDrops,sinceEou.map { String(format: "%.1fs", $0) } ?? "never"))+ if self.diagnosticsEnabled {+ let snapshot = self.diagnosticSnapshot+ // Wrapper counters only: no audio, transcript, names, or identity evidence.+ // Actor-local logging is not an independent hung-executor watchdog.+ self.diag("p0 converted=\(snapshot.convertedBuffers) conversionFailures=\(snapshot.conversionFailures) append=\(snapshot.appendCompletions)/\(snapshot.appendStarts) process=\(snapshot.processCompletions)/\(snapshot.processStarts) errors=\(snapshot.appendOrProcessErrors) partialCallbacks=\(snapshot.partialCallbacks) eouCallbacks=\(snapshot.eouCallbacks) resetReturns=\(snapshot.recognizerResetReturned)/\(snapshot.resetInvocations) resetOrdinal=\(snapshot.wrapperGeneration) maxProcessSeconds=\(snapshot.maxProcessSeconds)")+ }}/// The recognizer only emits tokens once it has decoded speech, so the first partial — not the/// first audio buffer — marks where this utterance actually begins. Anchoring on audio instead/// swallowed every second of preceding silence into the utterance's span.private func handlePartial(_ text: String) {+ if self.diagnosticsEnabled {+ self.diagnosticSnapshot.partialCallbacks += 1+ if self.diagnosticSnapshot.firstPartialAfterResetSeconds == nil,+ let reset = self.diagnosticResetUptime {+ self.diagnosticSnapshot.firstPartialAfterResetSeconds = self.uptime() - reset+ }+ }if self.utteranceStartPTS == nil {guard let feed = self.currentFeedPTS else { return }let backdated = feed - Self.decoderLookback@@ -271,7 +353,7 @@ actor MeetingLiveTrackEngine {}guard let start = self.utteranceStartPTS, let turnID = self.utteranceTurnID else { return }self.lastPartialText = text- self.lastPartialUptime = ProcessInfo.processInfo.systemUptime+ self.lastPartialUptime = self.uptime()let end = self.lastConsumedPTS ?? startself.diagPartials += 1if self.diagPartials % 10 == 1 {@@ -281,17 +363,18 @@ actor MeetingLiveTrackEngine {}private func handleEOU(_ text: String) async {+ if self.diagnosticsEnabled { self.diagnosticSnapshot.eouCallbacks += 1 }let start = self.utteranceStartPTSlet end = self.lastConsumedPTS ?? startlet turnID = self.utteranceTurnID- let now = ProcessInfo.processInfo.systemUptime+ let now = self.uptime()let gap = self.diagLastEouUptime.map { now - $0 }self.diagLastEouUptime = nowself.utteranceStartPTS = nilself.utteranceTurnID = nilself.lastPartialText = ""self.lastPartialUptime = nil- await self.manager.reset()+ await self.resetRecognizer()let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)guard !trimmed.isEmpty else { return }guard let start, let end, let turnID else {@@ -331,9 +414,31 @@ actor MeetingLiveTrackEngine {self.lastPartialText = ""self.lastPartialUptime = nilself.lastConsumedPTS = nil+ await self.resetRecognizer()+ }++ private func resetRecognizer() async {+ if self.diagnosticsEnabled {+ self.diagnosticSnapshot.resetInvocations += 1+ self.diagnosticSnapshot.wrapperGeneration += 1+ self.diagnosticSnapshot.firstPartialAfterResetSeconds = nil+ self.diagnosticResetUptime = nil+ }await self.manager.reset()+ if self.diagnosticsEnabled {+ self.diagnosticSnapshot.recognizerResetReturned += 1+ self.diagnosticResetUptime = self.uptime()+ }}+ #if DEBUG+ /// Deterministic P0 seam: bypasses capture/model loading, but exercises production conversion,+ /// append/process and existing rotation policy. It deliberately does not add recovery policy.+ func diagnosticConsume(_ sample: MeetingLiveSampleCopy.Sample) async { await self.consume(sample) }+ func diagnosticPartial(_ text: String) { self.handlePartial(text) }+ func diagnosticSnapshotValue() -> MeetingLiveDiagnosticSnapshot { self.diagnosticSnapshot }+ #endif+/// Persistent converter reused across calls; FluidAudio's own `appendAudio` takes a fast path/// once the buffer already matches its 16kHz mono target, so this is the only resample per chunk.private func convert(_ buffer: AVAudioPCMBuffer) -> AVAudioPCMBuffer? {diff --git a/Sources/Fluid/Services/Meeting/MeetingASRPreparationOwner.swift b/Sources/Fluid/Services/Meeting/MeetingASRPreparationOwner.swiftnew file mode 100644index 00000000..ea627671--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingASRPreparationOwner.swift@@ -0,0 +1,311 @@+import Foundation++/// Rejection of a meeting ASR preparation request before or during claim.+nonisolated enum MeetingASRPreparationError: LocalizedError, Equatable {+ case invalidActivityLease+ case userModelDownloadInProgress+ case preparationInProgress++ var errorDescription: String? {+ switch self {+ case .invalidActivityLease:+ return "The meeting no longer holds the exclusive audio activity lease."+ case .userModelDownloadInProgress:+ return "Wait for the active voice model download to finish."+ case .preparationInProgress:+ return "Meeting transcription preparation is already in progress."+ }+ }+}++/// Immutable identity for one claimed preparation operation.+nonisolated struct MeetingASRPreparationClaimToken: Equatable, @unchecked Sendable {+ let lease: ASRActivityLease+ let operationID: UUID+}++/// Owns the meeting post-processing ASR provider slot: at most one claimed preparation+/// (provider, owned task, generated operation ID, immutable configuration) exists at a time.+/// The same lease+attempt+configuration joins the in-flight preparation or returns the ready+/// provider; any different request fails busy without cancelling the existing one.+///+/// The owner never acquires or releases the ASR activity lease itself — the caller keeps that+/// responsibility — and it never touches dictation state directly; every side effect goes+/// through the injected production closures. `isClaimed` stays true from the synchronous claim+/// through readiness and teardown; future service guards key on `isClaimed`/`isDraining`+/// rather than the exclusive-activity enum so legacy meetings keep working while unwired.+@MainActor+final class MeetingASRPreparationOwner {+ private struct Slot {+ let lease: ASRActivityLease+ let attemptID: UUID+ let operationID: UUID+ let configuration: MeetingFinalProcessingConfiguration+ var provider: (any TranscriptionProvider)?+ var task: Task<Void, Error>?+ var isReady = false++ var claimToken: MeetingASRPreparationClaimToken {+ MeetingASRPreparationClaimToken(lease: self.lease, operationID: self.operationID)+ }++ func matches(+ lease: ASRActivityLease,+ attemptID: UUID,+ configuration: MeetingFinalProcessingConfiguration+ ) -> Bool {+ self.lease == lease && self.attemptID == attemptID && self.configuration == configuration+ }+ }++ private let isActiveLease: @MainActor (ASRActivityLease) -> Bool+ private let isUserDownloadInProgress: @MainActor () -> Bool+ private let retireDictationResources: @MainActor (ASRActivityLease) async throws -> Void+ private let makeProvider: @MainActor (MeetingFinalProcessingConfiguration) throws -> any TranscriptionProvider+ private let prepareProvider: @MainActor (+ any TranscriptionProvider,+ MeetingFinalProcessingConfiguration,+ @escaping (ModelPreparationProgress) -> Void+ ) async throws -> Void+ private let drainExecutor: @MainActor (ASRActivityLease) async -> Void+#if DEBUG+ var didMarkReadyForTesting: (@MainActor () -> Void)?+ var didBeginWaitingForTesting: (@MainActor () -> Void)?+#endif++ private var slot: Slot?+ private var drainTask: Task<Void, Never>?++ init(+ isActiveLease: @escaping @MainActor (ASRActivityLease) -> Bool,+ isUserDownloadInProgress: @escaping @MainActor () -> Bool,+ retireDictationResources: @escaping @MainActor (ASRActivityLease) async throws -> Void,+ makeProvider: @escaping @MainActor (MeetingFinalProcessingConfiguration) throws -> any TranscriptionProvider,+ prepareProvider: @escaping @MainActor (+ any TranscriptionProvider,+ MeetingFinalProcessingConfiguration,+ @escaping (ModelPreparationProgress) -> Void+ ) async throws -> Void,+ drainExecutor: @escaping @MainActor (ASRActivityLease) async -> Void+ ) {+ self.isActiveLease = isActiveLease+ self.isUserDownloadInProgress = isUserDownloadInProgress+ self.retireDictationResources = retireDictationResources+ self.makeProvider = makeProvider+ self.prepareProvider = prepareProvider+ self.drainExecutor = drainExecutor+ }++ /// True from the synchronous claim through the ready state and the entire teardown.+ var isClaimed: Bool { self.slot != nil }++ /// True while a release drain is in flight, including its final state-clearing tail.+ var isDraining: Bool { self.drainTask != nil }++ /// Snapshot of the currently claimed operation. A caller must capture this on the main+ /// actor before scheduling a later release; cleanup never looks this value up dynamically.+ var currentClaimToken: MeetingASRPreparationClaimToken? { self.slot?.claimToken }++ @discardableResult+ func prepare(+ lease: ASRActivityLease,+ attemptID: UUID,+ configuration: MeetingFinalProcessingConfiguration,+ progress: ((ModelPreparationProgress) -> Void)? = nil+ ) async throws -> any TranscriptionProvider {+ // Validation is pure: no claim, retirement, factory, or hook runs for a rejected request.+ try Task.checkCancellation()+ guard lease.activity == .meeting, self.isActiveLease(lease) else {+ throw MeetingASRPreparationError.invalidActivityLease+ }+ _ = try MeetingProviderOptions.resolve(configuration)+ guard !self.isUserDownloadInProgress() else {+ throw MeetingASRPreparationError.userModelDownloadInProgress+ }++ if self.slot != nil {+ guard self.drainTask == nil,+ self.slot?.matches(+ lease: lease, attemptID: attemptID, configuration: configuration+ ) == true+ else { throw MeetingASRPreparationError.preparationInProgress }+ guard let token = self.slot?.claimToken else {+ throw MeetingASRPreparationError.preparationInProgress+ }+ if self.slot?.isReady == true, let provider = self.slot?.provider {+ try Task.checkCancellation()+ guard self.drainTask == nil,+ self.isActiveLease(lease),+ self.slot?.claimToken == token,+ self.slot?.task?.isCancelled == false else {+ throw CancellationError()+ }+ return provider+ }+ guard let task = self.slot?.task else {+ throw MeetingASRPreparationError.preparationInProgress+ }+ return try await self.awaitPreparation(task, token: token)+ }+ guard self.drainTask == nil else {+ throw MeetingASRPreparationError.preparationInProgress+ }++ // Claim synchronously before the first suspension so a racing request sees the slot.+ let operationID = UUID()+ let progressHandler = self.makeProgressHandler(+ lease: lease, operationID: operationID, progress: progress+ )+ let token = MeetingASRPreparationClaimToken(lease: lease, operationID: operationID)+ let task = Task { @MainActor [weak self] () throws -> Void in+ guard let self else { throw CancellationError() }+ try await self.performPreparation(+ lease: lease,+ operationID: operationID,+ configuration: configuration,+ progressHandler: progressHandler+ )+ }+ self.slot = Slot(+ lease: lease,+ attemptID: attemptID,+ operationID: operationID,+ configuration: configuration,+ task: task+ )+ return try await self.awaitPreparation(task, token: token)+ }++ /// Idempotent teardown for the exact claim token. Joins the single in-flight drain task if one+ /// exists; otherwise cancels and awaits the preparation task (a non-cooperative hook still+ /// runs to completion), drains the executor, then releases the provider and clears the+ /// claim. The ASR activity lease is never released here.+ func release(_ token: MeetingASRPreparationClaimToken) async {+ await self.drain(token: token)+ }++ private func awaitPreparation(+ _ task: Task<Void, Error>,+ token: MeetingASRPreparationClaimToken+ ) async throws -> any TranscriptionProvider {+ do {+ try await withTaskCancellationHandler {+#if DEBUG+ self.didBeginWaitingForTesting?()+#endif+ try await task.value+ } onCancel: {+ task.cancel()+ }+ try Task.checkCancellation()+ guard self.drainTask == nil,+ self.isActiveLease(token.lease),+ self.slot?.claimToken == token,+ self.slot?.isReady == true,+ self.slot?.task?.isCancelled == false,+ let provider = self.slot?.provider+ else {+ throw CancellationError()+ }+ return provider+ } catch {+ await self.drain(token: token)+ if Task.isCancelled { throw CancellationError() }+ throw error+ }+ }++ private func performPreparation(+ lease: ASRActivityLease,+ operationID: UUID,+ configuration: MeetingFinalProcessingConfiguration,+ progressHandler: @escaping (ModelPreparationProgress) -> Void+ ) async throws {+ try self.recheck(lease: lease, operationID: operationID)+ try await self.retireDictationResources(lease)+ try self.recheck(lease: lease, operationID: operationID)+ let provider = try self.makeProvider(configuration)+ self.adoptProvider(provider, operationID: operationID)+ try await self.prepareProvider(provider, configuration, progressHandler)+ try self.recheck(lease: lease, operationID: operationID)+ guard self.slot?.operationID == operationID else { throw CancellationError() }+ self.markReady(operationID: operationID)+#if DEBUG+ self.didMarkReadyForTesting?()+#endif+ }++ private func recheck(lease: ASRActivityLease, operationID: UUID) throws {+ try Task.checkCancellation()+ guard self.slot?.operationID == operationID,+ self.slot?.lease == lease,+ self.drainTask == nil,+ self.isActiveLease(lease)+ else { throw CancellationError() }+ }++ private func makeProgressHandler(+ lease: ASRActivityLease,+ operationID: UUID,+ progress: ((ModelPreparationProgress) -> Void)?+ ) -> (ModelPreparationProgress) -> Void {+ { [weak self] update in+ guard let progress else { return }+ // Providers report from arbitrary queues; hop to the main actor and drop anything+ // stale, cancelled, or draining by the time the hop lands.+ DispatchQueue.main.async { [weak self] in+ MainActor.assumeIsolated {+ guard let self,+ let slot = self.slot,+ slot.operationID == operationID,+ slot.lease == lease,+ !slot.isReady,+ self.drainTask == nil,+ self.isActiveLease(lease),+ slot.task?.isCancelled == false+ else { return }+ progress(update)+ }+ }+ }+ }++ private func adoptProvider(_ provider: any TranscriptionProvider, operationID: UUID) {+ guard self.slot?.operationID == operationID else { return }+ self.slot?.provider = provider+ }++ private func markReady(operationID: UUID) {+ guard self.slot?.operationID == operationID else { return }+ self.slot?.isReady = true+ }++ private func clearSlot(operationID: UUID) {+ guard self.slot?.operationID == operationID else { return }+ self.slot = nil+ }++ /// Creates the one owned drain synchronously on the main actor. The worker only prepares;+ /// this task owns cancellation, executor draining, and the final state-clearing tail.+ private func drain(token: MeetingASRPreparationClaimToken) async {+ guard self.slot?.claimToken == token else { return }+ if let drainTask = self.drainTask {+ await drainTask.value+ return+ }++ let preparationTask = self.slot?.task+ let drainTask = Task { @MainActor [weak self] in+ guard let self else { return }+ preparationTask?.cancel()+ _ = await preparationTask?.result+ // This production hook must only drain executor state; it must not call back into+ // this owner, since that would attempt to await the drain task from itself.+ await self.drainExecutor(token.lease)+ self.clearSlot(operationID: token.operationID)+ self.drainTask = nil+ }+ self.drainTask = drainTask+ await drainTask.value+ }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift b/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swiftindex 4e6aa789..824e832b 100644--- a/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift+++ b/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift@@ -76,18 +76,34 @@ actor MeetingCaptureEngine: MeetingCaptureControlling {else { throw MeetingCaptureError.applicationNotSelected }let outputRoute = Self.currentOutputRouteSnapshot()+#if DEBUG+ let forcePairedScreenCaptureKit = MeetingSCKPairedDiagnosticGate.enabled()+#else+ let forcePairedScreenCaptureKit = false+#endiflet decision = MeetingCapturePathDecider.decide(mode: configuration.mode,microphone: configuration.microphone,- outputRoute: outputRoute+ outputRoute: outputRoute,+ forcePairedScreenCaptureKit: forcePairedScreenCaptureKit)switch decision {case let .screenCaptureKit(reason):let protection = MeetingRawMicrophoneProtection.classify(outputRoute)+#if DEBUG+ let captureDecisionMessage = reason == MeetingCapturePathDecider.diagnosticForceScreenCaptureKitReason+ ? "C2 diagnostic forced paired ScreenCaptureKit; this is not a production fallback."+ : "Meeting voice-processing capture declined: \(reason)"+ DebugLogger.shared.log(+ captureDecisionMessage,+ source: "MeetingCaptureEngine"+ )+#elseDebugLogger.shared.log("Meeting voice-processing capture declined: \(reason)",source: "MeetingCaptureEngine")+#endifeventHandler(.interrupted(kind: .voiceProcessingDeclined, trackID: nil, detail: reason))try await microphoneWriter.updateTrackMetadata { track intrack.captureEras = [MeetingCaptureEra(@@ -459,6 +475,11 @@ private final nonisolated class ScreenCaptureMeetingRuntime: NSObject, MeetingCaprivate let microphoneWriter: MeetingAudioChunkWriterprivate let eventHandler: @Sendable (MeetingCaptureEvent) -> Voidprivate let liveAudioHandler: (@Sendable (MeetingAudioTrackKind, CMSampleBuffer) -> Void)?+#if DEBUG+ private let pairedDiagnosticCollector: MeetingSCKPairedDiagnosticCollector+ private var pairedDiagnosticProvenanceFailureReported = false+ private var pairedDiagnosticReportEmitted = false+#endifprivate let stateLock = NSLock()private var stream: SCStreamprivate var scope: MeetingCaptureScope@@ -500,6 +521,9 @@ private final nonisolated class ScreenCaptureMeetingRuntime: NSObject, MeetingCaself.microphoneWriter = microphoneWriterself.eventHandler = eventHandlerself.liveAudioHandler = liveAudioHandler+#if DEBUG+ self.pairedDiagnosticCollector = MeetingSCKPairedDiagnosticCollector()+#endifsuper.init()}@@ -687,6 +711,9 @@ private final nonisolated class ScreenCaptureMeetingRuntime: NSObject, MeetingCa}func stop() async throws {+#if DEBUG+ defer { self.emitPairedDiagnosticReportIfNeeded() }+#endif#if DEBUGAudioTopologyDiagnostics.record(.phaseBegin, owner: .screenCapture, queueRole: .actorControl, phase: .screenCaptureStop)defer { AudioTopologyDiagnostics.record(.phaseEnd, owner: .screenCapture, queueRole: .actorControl, phase: .screenCaptureStop) }@@ -732,9 +759,15 @@ private final nonisolated class ScreenCaptureMeetingRuntime: NSObject, MeetingCaguard shouldAccept else { return }switch outputType {case .audio:+#if DEBUG+ self.recordPairedDiagnostic(sampleBuffer, output: .applicationAudio)+#endifself.applicationWriter.enqueue(sampleBuffer)self.liveAudioHandler?(.applicationAudio, sampleBuffer)case .microphone:+#if DEBUG+ self.recordPairedDiagnostic(sampleBuffer, output: .microphone)+#endifself.microphoneWriter.enqueue(sampleBuffer)if self.microphoneTranscriptGate.observeAndAdmit(sampleBuffer) {self.liveAudioHandler?(.microphone, sampleBuffer)@@ -746,6 +779,64 @@ private final nonisolated class ScreenCaptureMeetingRuntime: NSObject, MeetingCa}}+#if DEBUG+ /// Captures only callback-boundary numeric metadata. The selected-app scope assertion is+ /// reconstructed from the active SCK filter; no application identity enters the diagnostic.+ private func recordPairedDiagnostic(_ sampleBuffer: CMSampleBuffer, output: MeetingSCKPairedOutput) {+ guard self.pairedDiagnosticCollector.enabled else { return }+ let scope = self.stateLock.withLock { self.scope }+ let diagnosticScope: MeetingSCKPairedDiagnosticScope+ switch scope {+ case .window: diagnosticScope = .selectedApplicationWindow+ case .display: diagnosticScope = .selectedApplicationDisplay+ }+ let provenance = MeetingSCKPairedDiagnosticProvenance(+ scope: diagnosticScope,+ applicationSelectionConfirmed: true+ )+ let accepted = self.pairedDiagnosticCollector.record(+ sampleBuffer: sampleBuffer,+ output: output,+ provenance: provenance+ )+ guard !accepted else { return }+ let shouldReport = self.stateLock.withLock { () -> Bool in+ guard !self.pairedDiagnosticProvenanceFailureReported else { return false }+ self.pairedDiagnosticProvenanceFailureReported = true+ return true+ }+ if shouldReport {+ let message = "SCK paired diagnostic callback rejected due to scope/provenance change."+ Task { @MainActor in+ DebugLogger.shared.warning(message, source: "MeetingSCKPairedDiagnostics")+ }+ }+ }++ private func emitPairedDiagnosticReportIfNeeded() {+ guard self.pairedDiagnosticCollector.enabled else { return }+ let shouldEmit = self.stateLock.withLock { () -> Bool in+ guard !self.pairedDiagnosticReportEmitted else { return false }+ self.pairedDiagnosticReportEmitted = true+ return true+ }+ guard shouldEmit else { return }+ do {+ let data = try self.pairedDiagnosticCollector.report().jsonData()+ let json = String(decoding: data, as: UTF8.self)+ let message = "SCK paired diagnostic report " + json+ Task { @MainActor in+ DebugLogger.shared.log(message, source: "MeetingSCKPairedDiagnostics")+ }+ } catch {+ let message = "SCK paired diagnostic report serialization failed."+ Task { @MainActor in+ DebugLogger.shared.warning(message, source: "MeetingSCKPairedDiagnostics")+ }+ }+ }+#endif+/// A raw ScreenCaptureKit microphone has no producer handoff. Close live admission at the/// observation boundary and persist a new unprotected era from that point forward. Historical/// audio remains classified by the route on which it was actually captured.@@ -2102,10 +2193,14 @@ final nonisolated class MeetingAsyncTaskChain: @unchecked Sendable {/// Pure decision table. Capture safety is independent of semantic speaker ownership.nonisolated enum MeetingCapturePathDecider {+ static let diagnosticForceScreenCaptureKitReason =+ "DEBUG C2 diagnostic forced paired ScreenCaptureKit; not a production fallback."+static func decide(mode: MeetingCaptureMode,microphone: MeetingMicrophoneIdentity,- outputRoute: MeetingOutputRouteSnapshot+ outputRoute: MeetingOutputRouteSnapshot,+ forcePairedScreenCaptureKit: Bool = false) -> MeetingCapturePathDecision {guard mode == .onlineCall else {return .screenCaptureKit(reason: "Voice-processing capture only applies to online-call recordings.")@@ -2116,6 +2211,11 @@ nonisolated enum MeetingCapturePathDecider {if let reason = Self.outputRouteDeclineReason(outputRoute) {return .screenCaptureKit(reason: reason)}+#if DEBUG+ if forcePairedScreenCaptureKit {+ return .screenCaptureKit(reason: Self.diagnosticForceScreenCaptureKitReason)+ }+#endifreturn .voiceProcessing}@@ -2125,6 +2225,9 @@ nonisolated enum MeetingCapturePathDecider {guard !outputRoute.isBluetooth else { return "The output device is Bluetooth." }guard outputRoute.isBuiltIn else { return "The output device is not the built-in speakers." }guard !outputRoute.isHeadphonesDataSource else { return "The output route is headphones." }+ guard !outputRoute.terminalTypes.contains(kAudioStreamTerminalTypeHeadphones) else {+ return "The output stream terminal is headphones."+ }return nil}}diff --git a/Sources/Fluid/Services/Meeting/MeetingEchoSignalScorer.swift b/Sources/Fluid/Services/Meeting/MeetingEchoSignalScorer.swiftindex ae935606..0304fe9a 100644--- a/Sources/Fluid/Services/Meeting/MeetingEchoSignalScorer.swift+++ b/Sources/Fluid/Services/Meeting/MeetingEchoSignalScorer.swift@@ -8,8 +8,15 @@ nonisolated struct DelayEstimate: Equatable, Sendable {nonisolated enum TurnEchoVerdict: Equatable, Sendable {case echo- case containsLocalSpeech+ /// Low playback explanation; not positive evidence of near-end speech.+ case residualNotExplainedcase unknown++ /// Stage A compatibility only: preserve the old truth table, including its unsafe rescue.+ /// Never consult shadow admission here. Changing this rule requires calibrated enforcement.+ func legacyEffectiveEcho(textEcho: Bool) -> Bool {+ (textEcho || self == .echo) && self != .residualNotExplained+ }}nonisolated struct EchoFrameScores: Equatable, Sendable {@@ -17,8 +24,8 @@ nonisolated struct EchoFrameScores: Equatable, Sendable {let hopSeconds: Double}-/// Separates far-end echo from local speech by correlating the mic against what the meeting app-/// played. Calibrated on one 3.2-minute Zoom recording: pure echo scored median 0.62-0.82 with+/// Measures how well playback explains microphone audio; cannot establish near-end speech.+/// Historical calibration on one 3.2-minute Zoom recording: pure echo scored median 0.62-0.82 with/// low-runs of 0.24-0.59 s, local speech median 0.00-0.38 with low-runs of 0.96-10.27 s. Since the/// statistic is `1 - residual/mic`, additive double talk only rescues a turn when local speech/// carries ~1.86x the echo energy - true near the talker, false for a quiet interjection.@@ -26,18 +33,18 @@ nonisolated enum MeetingEchoSignalScorer {static let frameLength = 1024static let hopLength = 256static let blockSeconds = 2.0- static let localSpeechFractionThreshold = 0.35+ static let lowExplanationFractionThreshold = 0.35/// Calibrated against frame-*start* span, which undercounts coverage by 48 ms.- static let minimumLocalSpeechRunSeconds = 0.75+ static let minimumLowExplanationRunSeconds = 0.75static let echoMedianThreshold = 0.5- /// The rescue's `minimumLocalSpeechRunSeconds` is absolute, so on a long turn it fires on a few+ /// The rescue's `minimumLowExplanationRunSeconds` is absolute, so on a long turn it fires on a few/// percent of the span: measured 0.75s inside a 22.4s turn at median 0.91 — indistinguishable/// from a 21.5s turn at median 0.86 whose run happened to stop at 0.48s. A turn is only denied/// the rescue when it is BOTH overwhelmingly explained by the far end AND the low run is a/// trivial share of it; genuine double talk clears the share test (a 1.0s burst in a 3.0s turn/// is 33%), and calibrated mixed turns sit near median 0.38, far under the median test.static let overwhelminglyExplainedMedian = 0.8- static let minimumLocalSpeechRunFraction = 0.15+ static let minimumLowExplanationRunFraction = 0.15static let minimumDelayConfidence = 8.0static let minimumSignalRMS: Float = 1e-4static let minimumScoreableFraction = 0.5@@ -249,20 +256,20 @@ nonisolated enum MeetingEchoSignalScorer {let hopSeconds = scores.hopSecondsguard !explainedFractions.isEmpty else { return .unknown }- // Hiding real speech is the costly failure, so the rescue runs before the coverage gate;- // only the echo verdict requires enough scoreable frames.+ // Stage A preserves historical ordering: low-explanation evidence precedes the coverage+ // gate. The legacy display mapping rescues it, but it does not establish local speech.var longestLowRun = 0var currentRun = 0var gapRun = 0for value in explainedFractions {- if !value.isNaN, value < self.localSpeechFractionThreshold {+ if !value.isNaN, value < self.lowExplanationFractionThreshold {currentRun += 1gapRun = 0longestLowRun = max(longestLowRun, currentRun)} else if value.isNaN {gapRun += 1// A gap long enough to itself be a run means the thread is lost.- if Double(gapRun) * hopSeconds >= self.minimumLocalSpeechRunSeconds {+ if Double(gapRun) * hopSeconds >= self.minimumLowExplanationRunSeconds {currentRun = 0}} else {@@ -280,10 +287,10 @@ nonisolated enum MeetingEchoSignalScorer {let isTrivialRunInExplainedTurn = !median.isNaN&& scoreableFraction >= requiredScoreableFraction&& median >= self.overwhelminglyExplainedMedian- && lowRunFraction < self.minimumLocalSpeechRunFraction+ && lowRunFraction < self.minimumLowExplanationRunFraction- if !isTrivialRunInExplainedTurn, lowRunSeconds >= self.minimumLocalSpeechRunSeconds {- return .containsLocalSpeech+ if !isTrivialRunInExplainedTurn, lowRunSeconds >= self.minimumLowExplanationRunSeconds {+ return .residualNotExplained}guard scoreableFraction >= requiredScoreableFraction else { return .unknown }diff --git a/Sources/Fluid/Services/Meeting/MeetingExternalReferenceTrialA.swift b/Sources/Fluid/Services/Meeting/MeetingExternalReferenceTrialA.swiftnew file mode 100644index 00000000..3cfa055a--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingExternalReferenceTrialA.swift@@ -0,0 +1,806 @@+#if DEBUG++import AppKit+import Accelerate+import AVFoundation+import CoreAudio+import CoreMedia+import Darwin+import Foundation+import ScreenCaptureKit++/// Trial A is intentionally a separate gate from C2 and Trial B. It captures only the selected+/// Chrome audio as a reference and the existing VPIO microphone as capture; it never renders audio,+/// transcribes, persists, or changes the production route.+nonisolated enum MeetingExternalReferenceTrialAGate {+ static let environmentKey = "FLUIDVOICE_TRIAL_A"+ static let targetBundleIDEnvironmentKey = "FLUIDVOICE_TRIAL_A_TARGET_BUNDLE_ID"+ static let autorunEnvironmentKey = "FLUIDVOICE_TRIAL_A_AUTORUN"+ static let requiredTargetBundleID = "com.google.Chrome"+ /// The localhost fixture changes its document title only after the user has clicked Start and+ /// while its AudioContext is active. Checking this title prevents a run from spending its one+ /// audited capture window on a merely-open (or already-complete) browser tab.+ static let stimulusWindowTitleMarker = "FluidVoice C2 Diagnostic Stimulus"+ static let stimulusWindowPlayingMarker = "PLAYING"++ static func enabled(environment: [String: String]) -> Bool {+ environment[Self.environmentKey] == "1"+ && environment[Self.targetBundleIDEnvironmentKey] == Self.requiredTargetBundleID+ }++ static func autorunEnabled(environment: [String: String]) -> Bool {+ self.enabled(environment: environment) && environment[Self.autorunEnvironmentKey] == "1"+ }++ static func stimulusWindowIsPlaying(title: String?) -> Bool {+ guard let title else { return false }+ let prefix = "\(Self.stimulusWindowTitleMarker) — \(Self.stimulusWindowPlayingMarker)"+ guard title.hasPrefix(prefix) else { return false }+ let suffix = title.dropFirst(prefix.count)+ // Chrome may append its product name, but a state token must end at a title boundary;+ // reject strings such as "NOT PLAYING" and "PLAYINGISH".+ return suffix.isEmpty || suffix.hasPrefix(" - ") || suffix.hasPrefix(" — ")+ }+}++nonisolated struct MeetingExternalReferenceTrialADisplayCandidate: Equatable, Sendable {+ let displayID: UInt32+ let frame: CGRect+}++nonisolated struct MeetingExternalReferenceTrialAWindowCandidate: Equatable, Sendable {+ let windowID: UInt32+ let owningBundleIdentifier: String?+ let owningProcessID: Int32+ let title: String?+}++nonisolated enum MeetingExternalReferenceTrialAWindowSelector {+ /// The owning window is the source of truth for readiness. Application lists can contain+ /// duplicate/stale records for one bundle, so selecting an arbitrary bundle record first can+ /// make a real PLAYING window look absent.+ static func selectPlayingWindow(+ from windows: [MeetingExternalReferenceTrialAWindowCandidate],+ targetBundleIdentifier: String+ ) -> MeetingExternalReferenceTrialAWindowCandidate? {+ let matches = windows.filter {+ $0.owningBundleIdentifier == targetBundleIdentifier+ && $0.owningProcessID > 0+ && MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(title: $0.title)+ }+ guard matches.count == 1 else { return nil }+ return matches[0]+ }+}++nonisolated enum MeetingExternalReferenceTrialADisplaySelector {+ /// Selects the display containing the matched stimulus window. A zero-area intersection or a+ /// tie is ambiguous and fails closed; choosing `displays.first` can silently capture the wrong+ /// display on a multi-monitor desktop.+ static func selectDisplayID(+ windowFrame: CGRect,+ displays: [MeetingExternalReferenceTrialADisplayCandidate]+ ) -> UInt32? {+ let intersections = displays.compactMap { display -> (id: UInt32, area: CGFloat)? in+ let intersection = display.frame.intersection(windowFrame)+ let area = intersection.width * intersection.height+ guard area > 0, area.isFinite else { return nil }+ return (display.displayID, area)+ }+ guard let maximum = intersections.map(\.area).max(), maximum > 0 else { return nil }+ let winners = intersections.filter { $0.area == maximum }+ guard winners.count == 1 else { return nil }+ return winners[0].id+ }+}++nonisolated struct MeetingExternalReferenceTrialATrackSample: Equatable, Sendable {+ let presentationSeconds: Double+ let durationSeconds: Double+ let frameCount: Int+ let sampleRateHz: Double+ let channelCount: Int+ let synthesizedTiming: Bool+ let rms: Double+ let peak: Double+ /// Monotonic callback-delivery time. Offline fixtures may omit this; live captures record it+ /// to prove samples belong to the bounded capture-open/close window.+ let arrivalSeconds: Double?++ init(+ presentationSeconds: Double,+ durationSeconds: Double,+ frameCount: Int,+ sampleRateHz: Double,+ channelCount: Int,+ synthesizedTiming: Bool,+ rms: Double,+ peak: Double,+ arrivalSeconds: Double? = nil+ ) {+ self.presentationSeconds = presentationSeconds+ self.durationSeconds = durationSeconds+ self.frameCount = frameCount+ self.sampleRateHz = sampleRateHz+ self.channelCount = channelCount+ self.synthesizedTiming = synthesizedTiming+ self.rms = rms+ self.peak = peak+ self.arrivalSeconds = arrivalSeconds+ }+}++nonisolated struct MeetingExternalReferenceTrialATrackReport: Codable, Equatable, Sendable {+ let callbackCount: Int+ let droppedFrameCount: Int+ let validSampleCount: Int+ let invalidTimestampCount: Int+ let firstPresentationSeconds: Double?+ let lastPresentationEndSeconds: Double?+ let deliveredDurationSeconds: Double+ let coveredDurationSeconds: Double+ let coverageFraction: Double+ let gapCount: Int+ let gapDurationSeconds: Double+ let overlapCount: Int+ let overlapDurationSeconds: Double+ let backwardsTimestampCount: Int+ let synthesizedFrameCount: Int+ let formatChangeCount: Int+ let rms: Double?+ let peak: Double?+ let clippedSampleCount: Int+ let captureOpenArrivalSeconds: Double?+ let captureCloseArrivalSeconds: Double?+ let arrivalBoundaryValid: Bool+ let arrivalCoverageSeconds: Double?+ let timingValid: Bool++ static func analyze(+ _ samples: [MeetingExternalReferenceTrialATrackSample],+ requestedDurationSeconds: Double,+ droppedFrameCount: Int = 0,+ callbackCount: Int? = nil,+ captureOpenArrivalSeconds: Double? = nil,+ captureCloseArrivalSeconds: Double? = nil+ ) -> Self {+ let requested = requestedDurationSeconds.isFinite && requestedDurationSeconds > 0+ ? requestedDurationSeconds : 0+ var invalid = 0+ var clipped = 0+ var valid = [MeetingExternalReferenceTrialATrackSample]()+ for sample in samples {+ if sample.peak.isFinite && sample.peak > 1 { clipped += 1 }+ let validTiming = sample.presentationSeconds.isFinite && sample.presentationSeconds >= 0+ && sample.durationSeconds.isFinite && sample.durationSeconds > 0+ && (sample.presentationSeconds + sample.durationSeconds).isFinite+ && sample.frameCount > 0 && sample.sampleRateHz.isFinite && sample.sampleRateHz > 0+ && sample.channelCount > 0 && sample.rms.isFinite && sample.rms >= 0+ && sample.peak.isFinite && sample.peak >= 0 && sample.peak <= 1+ && abs(Double(sample.frameCount) / sample.sampleRateHz - sample.durationSeconds)+ <= max(1.5 / sample.sampleRateHz, 0.001)+ if validTiming { valid.append(sample) } else { invalid += 1 }+ }+ var first: Double?+ var lastEnd: Double?+ var delivered = 0.0+ var covered = 0.0+ var gaps = 0+ var gapDuration = 0.0+ var overlaps = 0+ var overlapDuration = 0.0+ var backwards = 0+ var synthesized = 0+ var formats = Set<String>()+ var arrivals: [(time: Double, duration: Double)] = []+ var previousStart: Double?+ var previousEnd: Double?+ var previousSampleRate: Double?+ for sample in valid {+ let end = sample.presentationSeconds + sample.durationSeconds+ first = min(first ?? sample.presentationSeconds, sample.presentationSeconds)+ lastEnd = max(lastEnd ?? end, end)+ delivered += sample.durationSeconds+ formats.insert("\(sample.sampleRateHz):\(sample.channelCount)")+ if let arrival = sample.arrivalSeconds, arrival.isFinite {+ arrivals.append((arrival, sample.durationSeconds))+ }+ if sample.synthesizedTiming { synthesized += sample.frameCount }+ if let previousStart, let previousEnd {+ let continuityTolerance = max(+ 1e-9,+ 0.5 / min(previousSampleRate ?? sample.sampleRateHz, sample.sampleRateHz)+ )+ if sample.presentationSeconds < previousStart - continuityTolerance { backwards += 1 }+ if sample.presentationSeconds > previousEnd + continuityTolerance {+ gaps += 1+ gapDuration += sample.presentationSeconds - previousEnd+ } else if sample.presentationSeconds < previousEnd - continuityTolerance {+ overlaps += 1+ overlapDuration += previousEnd - sample.presentationSeconds+ }+ // Union coverage counts audio intervals only; a gap is reported separately and+ // must never inflate the covered fraction.+ covered += sample.presentationSeconds >= previousEnd+ ? sample.durationSeconds+ : max(0, end - previousEnd)+ } else {+ covered += sample.durationSeconds+ }+ previousStart = sample.presentationSeconds+ previousEnd = max(previousEnd ?? end, end)+ previousSampleRate = sample.sampleRateHz+ }+ let coverage = requested > 0 ? min(1, max(0, covered / requested)) : 0+ let oneSourceSampleSeconds = valid.map { 1 / $0.sampleRateHz }.min() ?? 0+ let hasExplicitArrivalWindow = captureOpenArrivalSeconds != nil+ || captureCloseArrivalSeconds != nil+ let hasArrivalEvidence = captureOpenArrivalSeconds?.isFinite == true+ && captureCloseArrivalSeconds?.isFinite == true+ && arrivals.count == valid.count+ let sortedArrivals = arrivals.sorted { $0.time < $1.time }+ let arrivalCoverage: Double? = hasArrivalEvidence+ ? sortedArrivals.first.flatMap { first in+ sortedArrivals.last.map { last in+ max(0, last.time - first.time + last.duration)+ }+ } : nil+ let boundaryTolerance = arrivals.map(\.duration).max() ?? oneSourceSampleSeconds+ let arrivalBoundaryValid = !hasExplicitArrivalWindow || (hasArrivalEvidence && {+ guard let open = captureOpenArrivalSeconds, let close = captureCloseArrivalSeconds,+ let arrivalCoverage else { return false }+ let allWithinWindow = arrivals.allSatisfy {+ $0.time >= open - boundaryTolerance && $0.time <= close + boundaryTolerance+ }+ return allWithinWindow && arrivalCoverage + boundaryTolerance >= requested+ }())+ let rms = valid.isEmpty ? nil : sqrt(valid.reduce(0.0) { $0 + $1.rms * $1.rms } / Double(valid.count))+ let peak = valid.map(\.peak).max()+ let timingValid = !valid.isEmpty && droppedFrameCount == 0 && invalid == 0 && clipped == 0 && gaps == 0 && overlaps == 0+ && backwards == 0 && synthesized == 0 && formats.count <= 1+ // Complete means the requested interval is covered, with only one source-sample of+ // floating-point/frame-quantization tolerance. A broad percentage threshold would+ // incorrectly accept materially truncated captures.+ && coverage + oneSourceSampleSeconds / max(requested, 1) >= 1.0+ && arrivalBoundaryValid+ return Self(+ callbackCount: max(samples.count, callbackCount ?? samples.count),+ droppedFrameCount: max(0, droppedFrameCount),+ validSampleCount: valid.count,+ invalidTimestampCount: invalid,+ firstPresentationSeconds: first,+ lastPresentationEndSeconds: lastEnd,+ deliveredDurationSeconds: delivered,+ coveredDurationSeconds: covered,+ coverageFraction: coverage,+ gapCount: gaps,+ gapDurationSeconds: gapDuration,+ overlapCount: overlaps,+ overlapDurationSeconds: overlapDuration,+ backwardsTimestampCount: backwards,+ synthesizedFrameCount: synthesized,+ formatChangeCount: max(0, formats.count - 1),+ rms: rms,+ peak: peak,+ clippedSampleCount: clipped,+ captureOpenArrivalSeconds: captureOpenArrivalSeconds,+ captureCloseArrivalSeconds: captureCloseArrivalSeconds,+ arrivalBoundaryValid: arrivalBoundaryValid,+ arrivalCoverageSeconds: arrivalCoverage,+ timingValid: timingValid+ )+ }+}++nonisolated struct MeetingExternalReferenceTrialAClockReport: Codable, Equatable, Sendable {+ let sharedClockEstablished: Bool+ let acousticDelaySeconds: Double?+ let relationship: String+ let reason: String+}++nonisolated struct MeetingExternalReferenceTrialAReport: Codable, Equatable, Sendable {+ static let currentSchemaVersion = 2+ let schemaVersion: Int+ let targetBundleID: String+ let targetProcessID: Int32+ let targetProcessStable: Bool+ let selectionConfirmed: Bool+ let microphoneDeviceID: UInt32?+ let defaultInputDeviceID: UInt32?+ let outputDeviceID: UInt32?+ let defaultOutputDeviceID: UInt32?+ let builtInRouteConfirmed: Bool+ let postCaptureRouteConfirmed: Bool+ let outputVolume: Double?+ let postCaptureOutputVolume: Double?+ let outputVolumeReadable: Bool+ let outputVolumeUnchanged: Bool+ let combinedPeakBound: Double?+ let measuredCombinedPeak: Double?+ let referenceExcitationConfirmed: Bool+ let streamErrorCount: Int+ let stopErrorCount: Int+ let reference: MeetingExternalReferenceTrialATrackReport+ let microphone: MeetingExternalReferenceTrialATrackReport+ let clock: MeetingExternalReferenceTrialAClockReport+ let voiceProcessingReadback: MeetingVoiceProcessingProbeSnapshot?+ let appOwnedPlayback: Bool+ let rawPCMRetained: Bool+ let transcriptRetained: Bool+ let persisted: Bool+ let captureValid: Bool+ let acousticMeasurementValid: Bool+ let reasons: [String]++ /// Backward-compatible summary used by the offline diagnostic tests. Trial A can report+ /// capture health while remaining invalid as an acoustic measurement until clock mapping is+ /// established.+ var valid: Bool { acousticMeasurementValid }+}++/// Lock-protected PCM collector. `reportAndRelease` computes numbers and then drops every sample+/// buffer before returning, so only numeric metadata crosses the diagnostic process boundary.+final nonisolated class MeetingExternalReferenceTrialACollector: @unchecked Sendable {+ private let lock = NSLock()+ // Keep a bounded six-second headroom. ScreenCaptureKit may deliver a small trailing tail+ // after a five-second wall-clock capture; treating that tail as a drop made every healthy+ // paired capture invalid while still retaining a strict memory bound.+ private let maximumFrames = 288_000 // 6 seconds at 48 kHz per track+ private var retainedFrames = 0+ private var callbackCount = 0+ private var droppedFrameCount = 0+ private var captureOpenArrivalSeconds: Double?+ private var captureCloseArrivalSeconds: Double?+ private var samples: [MeetingExternalReferenceTrialATrackSample] = []+ private var pcm: [[Float]] = []++ func ingest(_ sampleBuffer: CMSampleBuffer, synthesizedTiming: Bool = false) {+ self.lock.lock()+ self.callbackCount += 1+ self.lock.unlock()+ let arrival = ProcessInfo.processInfo.systemUptime+ let pts = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer))+ let duration = CMTimeGetSeconds(CMSampleBufferGetDuration(sampleBuffer))+ let callbackFrames = CMSampleBufferGetNumSamples(sampleBuffer)+ guard let copied = MeetingLiveSampleCopy.copy(sampleBuffer),+ let channel = copied.buffer.floatChannelData?[0] else {+ self.lock.lock(); defer { self.lock.unlock() }+ self.samples.append(.init(presentationSeconds: pts, durationSeconds: duration,+ frameCount: callbackFrames, sampleRateHz: .nan,+ channelCount: 0, synthesizedTiming: synthesizedTiming,+ rms: .nan, peak: .nan, arrivalSeconds: arrival))+ return+ }+ let frameCount = Int(copied.buffer.frameLength)+ var peak: Float = 0+ vDSP_maxmgv(channel, 1, &peak, vDSP_Length(frameCount))+ var sum: Float = 0+ vDSP_svesq(channel, 1, &sum, vDSP_Length(frameCount))+ let rms = frameCount > 0 ? sqrt(Double(sum) / Double(frameCount)) : .nan+ let sample = MeetingExternalReferenceTrialATrackSample(+ presentationSeconds: pts, durationSeconds: duration, frameCount: frameCount,+ sampleRateHz: copied.buffer.format.sampleRate,+ channelCount: Int(copied.buffer.format.channelCount),+ synthesizedTiming: synthesizedTiming, rms: rms, peak: Double(peak), arrivalSeconds: arrival)+ self.lock.lock(); defer { self.lock.unlock() }+ guard self.retainedFrames + frameCount <= self.maximumFrames else {+ self.droppedFrameCount += frameCount+ return+ }+ self.retainedFrames += frameCount+ self.samples.append(sample)+ self.pcm.append(Array(UnsafeBufferPointer(start: channel, count: frameCount)))+ }++ func reportAndRelease(requestedDurationSeconds: Double) -> MeetingExternalReferenceTrialATrackReport {+ self.lock.lock()+ let open = self.captureOpenArrivalSeconds+ let close = self.captureCloseArrivalSeconds+ let samples = self.samples+ let droppedFrameCount = self.droppedFrameCount+ let callbackCount = self.callbackCount+ self.samples.removeAll(keepingCapacity: false)+ self.pcm.removeAll(keepingCapacity: false)+ self.retainedFrames = 0+ self.droppedFrameCount = 0+ self.callbackCount = 0+ self.captureOpenArrivalSeconds = nil+ self.captureCloseArrivalSeconds = nil+ self.lock.unlock()+ return MeetingExternalReferenceTrialATrackReport.analyze(+ samples, requestedDurationSeconds: requestedDurationSeconds,+ droppedFrameCount: droppedFrameCount, callbackCount: callbackCount,+ captureOpenArrivalSeconds: open, captureCloseArrivalSeconds: close)+ }++ func reset() {+ self.lock.lock()+ self.samples.removeAll(keepingCapacity: false)+ self.pcm.removeAll(keepingCapacity: false)+ self.retainedFrames = 0+ self.droppedFrameCount = 0+ self.callbackCount = 0+ self.captureOpenArrivalSeconds = ProcessInfo.processInfo.systemUptime+ self.captureCloseArrivalSeconds = nil+ self.lock.unlock()+ }++ func markCaptureClosed() {+ self.lock.lock()+ self.captureCloseArrivalSeconds = ProcessInfo.processInfo.systemUptime+ self.lock.unlock()+ }+}++nonisolated enum MeetingExternalReferenceTrialAOfflineHarness {+ static func analyze(+ reference: [MeetingExternalReferenceTrialATrackSample],+ microphone: [MeetingExternalReferenceTrialATrackSample],+ requestedDurationSeconds: Double = 5+ ) -> MeetingExternalReferenceTrialAReport {+ let referenceReport = MeetingExternalReferenceTrialATrackReport.analyze(+ reference, requestedDurationSeconds: requestedDurationSeconds)+ let microphoneReport = MeetingExternalReferenceTrialATrackReport.analyze(+ microphone, requestedDurationSeconds: requestedDurationSeconds)+ let clock = MeetingExternalReferenceTrialAClockReport(+ sharedClockEstablished: false, acousticDelaySeconds: nil,+ relationship: "independent PTS origins; no defensible clock mapping",+ reason: "acoustic delay and AEC measurement are unknown")+ return MeetingExternalReferenceTrialAReport(+ schemaVersion: MeetingExternalReferenceTrialAReport.currentSchemaVersion,+ targetBundleID: MeetingExternalReferenceTrialAGate.requiredTargetBundleID,+ targetProcessID: 0, targetProcessStable: false,+ selectionConfirmed: false,+ microphoneDeviceID: nil, defaultInputDeviceID: nil,+ outputDeviceID: nil, defaultOutputDeviceID: nil,+ builtInRouteConfirmed: false,+ postCaptureRouteConfirmed: false, outputVolume: nil,+ postCaptureOutputVolume: nil,+ outputVolumeReadable: false, outputVolumeUnchanged: false,+ combinedPeakBound: nil, measuredCombinedPeak: nil,+ referenceExcitationConfirmed: false,+ streamErrorCount: 0,+ stopErrorCount: 0,+ reference: referenceReport, microphone: microphoneReport, clock: clock,+ voiceProcessingReadback: nil,+ appOwnedPlayback: false, rawPCMRetained: false,+ transcriptRetained: false, persisted: false,+ captureValid: false, acousticMeasurementValid: false,+ reasons: ["offline harness", "independent clocks; result is unknown"])+ }+}++nonisolated enum MeetingExternalReferenceTrialAAutorun {+ static let captureDurationSeconds = 5.0+ static let maximumRunSeconds = 12.0+ static let hardWatchdogSeconds = 14.0+ private static let operationTimeoutSeconds = 2.0++ @discardableResult+ static func startIfRequested(+ environment: [String: String] = ProcessInfo.processInfo.environment+ ) -> Bool {+ guard MeetingExternalReferenceTrialAGate.autorunEnabled(environment: environment) else {+ return false+ }+ let gate = CompletionGate()+ let watchdog = DispatchWorkItem {+ guard gate.claim() else { return }+ print("[TRIAL_A_AUTORUN] {\"exitStatus\":1,\"reason\":\"hard timeout\",\"status\":\"failure\"}")+ fflush(stdout)+ Darwin.exit(1)+ }+ DispatchQueue.global(qos: .userInitiated).asyncAfter(+ deadline: .now() + Self.hardWatchdogSeconds, execute: watchdog)+ Task { @MainActor in+ let outcome: Outcome+ do {+ outcome = try await Self.withTimeout(seconds: Self.maximumRunSeconds) {+ await Self.run(environment: environment)+ }+ } catch {+ outcome = Self.failure("Trial A timed out or was cancelled")+ }+ watchdog.cancel()+ guard gate.claim() else { return }+ print("[TRIAL_A_AUTORUN] " + outcome.line)+ fflush(stdout)+ Darwin.exit(outcome.exitStatus)+ }+ return true+ }++ private struct Outcome: Sendable { let exitStatus: Int32; let line: String }+ private enum RunError: Error { case timeout }+ private final class CompletionGate: @unchecked Sendable {+ private let lock = NSLock(); private var done = false+ func claim() -> Bool { lock.lock(); defer { lock.unlock() }; guard !done else { return false }; done = true; return true }+ }++ @MainActor+ private static func run(environment: [String: String]) async -> Outcome {+ guard CGPreflightScreenCaptureAccess() else { return Self.failure("screen recording access is not authorized") }+ guard AVCaptureDevice.authorizationStatus(for: .audio) == .authorized else { return Self.failure("microphone access is not authorized") }+ guard let targetBundleID = environment[MeetingExternalReferenceTrialAGate.targetBundleIDEnvironmentKey],+ targetBundleID == MeetingExternalReferenceTrialAGate.requiredTargetBundleID else { return Self.failure("Trial A target must be Chrome") }++ guard let microphoneDevice = try? await MeetingCaptureSourceCatalog.defaultMicrophone(),+ let inputDevice = AudioDevice.listInputDevices().first(where: { $0.uid == microphoneDevice.coreAudioUID }),+ inputDevice.isAlive, inputDevice.isBuiltIn,+ AudioDevice.getDefaultInputDevice()?.uid == microphoneDevice.coreAudioUID else {+ return Self.failure("default live built-in microphone preflight failed")+ }+ let preRoute = MeetingCaptureEngine.currentOutputRouteSnapshot()+ guard MeetingCapturePathDecider.outputRouteDeclineReason(preRoute) == nil,+ let outputDevice = AudioDevice.getDefaultOutputDevice(), outputDevice.isAlive else {+ return Self.failure("default live built-in speaker preflight failed")+ }+ let volume = Self.readOutputVolume(outputDevice.id)+ let combinedPeakBound = volume.map { Double($0) * 0.08 }+ guard let volume, volume.isFinite, volume > 0, volume <= 0.6,+ let combinedPeakBound, combinedPeakBound <= 0.15 else {+ return Self.failure("output volume or combined peak safety preflight failed")+ }++ let content: SCShareableContent+ do { content = try await SCShareableContent.current } catch { return Self.failure("shareable content unavailable") }+ let windowCandidates = content.windows.compactMap { window -> MeetingExternalReferenceTrialAWindowCandidate? in+ guard let owner = window.owningApplication else { return nil }+ return MeetingExternalReferenceTrialAWindowCandidate(+ windowID: window.windowID,+ owningBundleIdentifier: owner.bundleIdentifier,+ owningProcessID: owner.processID,+ title: window.title)+ }+ guard let selectedWindow = MeetingExternalReferenceTrialAWindowSelector.selectPlayingWindow(+ from: windowCandidates, targetBundleIdentifier: targetBundleID),+ let stimulusWindow = content.windows.first(where: { $0.windowID == selectedWindow.windowID }),+ let application = stimulusWindow.owningApplication,+ application.bundleIdentifier == targetBundleID,+ application.processID > 0,+ NSRunningApplication(processIdentifier: application.processID)?.isTerminated == false else {+ return Self.failure("controlled Chrome PLAYING window is absent, stale, or ambiguous")+ }+ let displayCandidates = content.displays.map {+ MeetingExternalReferenceTrialADisplayCandidate(displayID: $0.displayID, frame: $0.frame)+ }+ guard let displayID = MeetingExternalReferenceTrialADisplaySelector.selectDisplayID(+ windowFrame: stimulusWindow.frame, displays: displayCandidates),+ let display = content.displays.first(where: { $0.displayID == displayID }) else {+ return Self.failure("controlled Chrome stimulus window does not intersect exactly one display")+ }+ let referenceCollector = MeetingExternalReferenceTrialACollector()+ let microphoneCollector = MeetingExternalReferenceTrialACollector()+ let capture = MeetingMicrophoneCapture()+ var stream: SCStream?+ var streamOutput: TrialAStreamOutput?+ let streamDelegate = TrialAStreamDelegate()+ var stopErrorCount = 0+ do {+ _ = try await capture.start(+ microphone: microphoneDevice,+ authorizationPreflighted: true,+ onSample: { _ in },+ onSampleMetadata: { sampleBuffer, synthesized, _ in+ microphoneCollector.ingest(sampleBuffer, synthesizedTiming: synthesized)+ })++ let filter = SCContentFilter(display: display, including: [application], exceptingWindows: [])+ let configuration = SCStreamConfiguration()+ configuration.width = 2; configuration.height = 2+ configuration.minimumFrameInterval = CMTime(value: 1, timescale: 1)+ configuration.queueDepth = 3; configuration.showsCursor = false+ configuration.capturesAudio = true; configuration.captureMicrophone = false+ configuration.excludesCurrentProcessAudio = true+ configuration.sampleRate = 48_000; configuration.channelCount = 2+ let newStream = SCStream(filter: filter, configuration: configuration, delegate: streamDelegate)+ // Publish before the async start so timeout/start failures can always stop and clean+ // up the stream that was created.+ stream = newStream+ let output = TrialAStreamOutput(collector: referenceCollector)+ streamOutput = output+ try newStream.addStreamOutput(+ output, type: .audio,+ sampleHandlerQueue: DispatchQueue(label: "fluidvoice.trial-a.reference", qos: .userInteractive))+ try await Self.withTimeout(seconds: Self.operationTimeoutSeconds) { try await newStream.startCapture() }+ // VPIO must start first to establish its hardware graph, but only samples after this+ // shared capture-open boundary belong to Trial A's bounded five-second window.+ referenceCollector.reset()+ microphoneCollector.reset()+ try await Task.sleep(nanoseconds: UInt64(Self.captureDurationSeconds * 1_000_000_000))+ try Task.checkCancellation()+ } catch {+ if let stream {+ do { try await Self.withTimeout(seconds: Self.operationTimeoutSeconds) { try await stream.stopCapture() } }+ catch { stopErrorCount += 1 }+ if let streamOutput { try? await stream.removeStreamOutput(streamOutput, type: .audio) }+ }+ await capture.stopForPhase1Probe()+ _ = referenceCollector.reportAndRelease(requestedDurationSeconds: Self.captureDurationSeconds)+ _ = microphoneCollector.reportAndRelease(requestedDurationSeconds: Self.captureDurationSeconds)+ return Self.failure("Trial A capture failed")+ }+ referenceCollector.markCaptureClosed()+ microphoneCollector.markCaptureClosed()+ let voiceProcessingReadback = await capture.voiceProcessingProbeReadback()+ if let stream {+ do { try await Self.withTimeout(seconds: Self.operationTimeoutSeconds) { try await stream.stopCapture() } }+ catch { stopErrorCount += 1 }+ if let streamOutput {+ do { try await stream.removeStreamOutput(streamOutput, type: .audio) }+ catch { stopErrorCount += 1 }+ }+ }+ // Observe the route and volume while the VPIO generation is still live; teardown may+ // legitimately release the transient I/O unit and would hide a capture-time route change.+ let postCaptureRouteConfirmed = Self.routeIsStillSafe(+ microphoneUID: microphoneDevice.coreAudioUID, outputDeviceID: outputDevice.id)+ let postCaptureOutputVolume = Self.readOutputVolume(+ AudioDevice.getDefaultOutputDevice()?.id ?? kAudioObjectUnknown).map(Double.init)+ await capture.stopForPhase1Probe()+ let reference = referenceCollector.reportAndRelease(requestedDurationSeconds: Self.captureDurationSeconds)+ let microphoneReport = microphoneCollector.reportAndRelease(requestedDurationSeconds: Self.captureDurationSeconds)+ let volumeUnchanged = postCaptureOutputVolume.map {+ abs($0 - Double(volume)) <= 0.0001+ } == true+ let measuredCombinedPeak = postCaptureOutputVolume.map {+ (reference.peak ?? 0) * $0+ }+ let targetProcessStable = NSRunningApplication(processIdentifier: application.processID)?.isTerminated == false+ let voiceProcessingReadbackValid = Self.voiceProcessingReadbackIsValid(+ voiceProcessingReadback, inputDeviceID: inputDevice.id, outputDeviceID: outputDevice.id)+ let captureValid = reference.timingValid && microphoneReport.timingValid+ && postCaptureRouteConfirmed && volumeUnchanged+ && (measuredCombinedPeak ?? .infinity) <= 0.15+ && (reference.peak ?? 0) >= 0.001+ && streamDelegate.errorCount == 0+ && stopErrorCount == 0+ && targetProcessStable+ && voiceProcessingReadbackValid+ let clock = MeetingExternalReferenceTrialAClockReport(+ sharedClockEstablished: false, acousticDelaySeconds: nil,+ relationship: "independent PTS origins; no defensible clock mapping",+ reason: "per-buffer PTS cannot establish an acoustic capture/reference mapping")+ let report = MeetingExternalReferenceTrialAReport(+ schemaVersion: MeetingExternalReferenceTrialAReport.currentSchemaVersion,+ targetBundleID: targetBundleID, targetProcessID: application.processID,+ targetProcessStable: targetProcessStable,+ selectionConfirmed: true,+ microphoneDeviceID: inputDevice.id,+ defaultInputDeviceID: AudioDevice.getDefaultInputDevice()?.id,+ outputDeviceID: outputDevice.id, defaultOutputDeviceID: AudioDevice.getDefaultOutputDevice()?.id,+ builtInRouteConfirmed: true,+ postCaptureRouteConfirmed: postCaptureRouteConfirmed,+ outputVolume: Double(volume), postCaptureOutputVolume: postCaptureOutputVolume,+ outputVolumeReadable: true, outputVolumeUnchanged: volumeUnchanged,+ combinedPeakBound: combinedPeakBound,+ measuredCombinedPeak: measuredCombinedPeak,+ referenceExcitationConfirmed: (reference.peak ?? 0) >= 0.001,+ streamErrorCount: streamDelegate.errorCount,+ stopErrorCount: stopErrorCount,+ reference: reference, microphone: microphoneReport,+ clock: clock, voiceProcessingReadback: voiceProcessingReadback,+ appOwnedPlayback: false, rawPCMRetained: false,+ transcriptRetained: false, persisted: false,+ captureValid: captureValid, acousticMeasurementValid: false,+ reasons: ["diagnostic-only", "no app-owned playback", "independent clocks; acoustic delay unknown"]+ + ((reference.peak ?? 0) >= 0.001 ? [] : ["reference below excitation floor"])+ + (postCaptureRouteConfirmed ? [] : ["capture-time route changed or became unreadable"])+ + (volumeUnchanged ? [] : ["output volume changed during capture"])+ + ((measuredCombinedPeak ?? .infinity) <= 0.15 ? [] : ["measured combined peak exceeded 0.15"])+ + (reference.timingValid && microphoneReport.timingValid ? [] : ["timing invalid or bounded collector dropped audio"])+ + (streamDelegate.errorCount == 0 ? [] : ["ScreenCaptureKit stream stopped with error"])+ + (stopErrorCount == 0 ? [] : ["stream stop or output cleanup failed"])+ + (targetProcessStable ? [] : ["target process became unstable"])+ + (voiceProcessingReadbackValid ? [] : ["VPIO engine/voice-processing device readback was not verified"]))+ do {+ let data = try JSONEncoder.sorted.encode(report)+ return Self.success(String(decoding: data, as: UTF8.self))+ } catch { return Self.failure("numeric Trial A report serialization failed") }+ }++ private static func readOutputVolume(_ deviceID: AudioObjectID) -> Float32? {+ guard deviceID != kAudioObjectUnknown else { return nil }+ for selector in [kAudioHardwareServiceDeviceProperty_VirtualMainVolume, kAudioDevicePropertyVolumeScalar] {+ var address = AudioObjectPropertyAddress(mSelector: selector, mScope: kAudioDevicePropertyScopeOutput, mElement: kAudioObjectPropertyElementMain)+ var value: Float32 = 0; var size = UInt32(MemoryLayout<Float32>.size)+ if AudioObjectGetPropertyData(deviceID, &address, 0, nil, &size, &value) == noErr,+ value.isFinite, (0...1).contains(value) { return value }+ }+ return nil+ }++ private static func routeIsStillSafe(microphoneUID: String?, outputDeviceID: AudioObjectID) -> Bool {+ guard let microphoneUID,+ let input = AudioDevice.listInputDevices().first(where: { $0.uid == microphoneUID }),+ input.isAlive, input.isBuiltIn,+ AudioDevice.getDefaultInputDevice()?.uid == microphoneUID,+ let output = AudioDevice.getDefaultOutputDevice(), output.isAlive,+ output.id == outputDeviceID,+ MeetingCapturePathDecider.outputRouteDeclineReason(+ MeetingCaptureEngine.currentOutputRouteSnapshot()) == nil,+ let volume = Self.readOutputVolume(output.id), volume > 0, volume <= 0.6 else {+ return false+ }+ return true+ }++ private static func voiceProcessingReadbackIsValid(+ _ snapshot: MeetingVoiceProcessingProbeSnapshot?,+ inputDeviceID: AudioObjectID,+ outputDeviceID: AudioObjectID+ ) -> Bool {+ guard let snapshot,+ snapshot.engineRunning,+ snapshot.nodeVoiceProcessingEnabled,+ !snapshot.nodeVoiceProcessingBypassed,+ snapshot.nodeVoiceProcessingAGCEnabled,+ snapshot.inputCurrentDevice.status == noErr,+ snapshot.outputCurrentDevice.status == noErr,+ snapshot.inputCurrentDevice.value == inputDeviceID,+ snapshot.outputCurrentDevice.value == outputDeviceID,+ snapshot.bypassVoiceProcessing.status == noErr,+ snapshot.bypassVoiceProcessing.value == 0,+ snapshot.voiceProcessingAGCEnabled.status == noErr,+ snapshot.voiceProcessingAGCEnabled.value == 1 else {+ return false+ }+ return true+ }++ private static func success(_ reportJSON: String) -> Outcome {+ guard let data = reportJSON.data(using: .utf8),+ let report = try? JSONSerialization.jsonObject(with: data),+ let line = Self.jsonLine(["exitStatus": 0, "report": report, "status": "success"]) else {+ return Self.failure("numeric Trial A report serialization failed")+ }+ return Outcome(exitStatus: 0, line: line)+ }+ private static func failure(_ reason: String) -> Outcome {+ let line = Self.jsonLine(["exitStatus": 1, "reason": reason, "status": "failure"])+ ?? "{\"exitStatus\":1,\"status\":\"failure\"}"+ return Outcome(exitStatus: 1, line: line)+ }+ private static func jsonLine(_ object: [String: Any]) -> String? {+ guard JSONSerialization.isValidJSONObject(object),+ let data = try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys]) else {+ return nil+ }+ return String(decoding: data, as: UTF8.self)+ }+ private static func withTimeout<T: Sendable>(seconds: Double, operation: @escaping @Sendable () async throws -> T) async throws -> T {+ try await withThrowingTaskGroup(of: T.self) { group in+ group.addTask { try await operation() }+ group.addTask { try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000)); throw RunError.timeout }+ defer { group.cancelAll() }+ return try await group.next()!+ }+ }+}++private final class TrialAStreamOutput: NSObject, SCStreamOutput, @unchecked Sendable {+ private let collector: MeetingExternalReferenceTrialACollector+ init(collector: MeetingExternalReferenceTrialACollector) { self.collector = collector }+ func stream(_ stream: SCStream, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, of outputType: SCStreamOutputType) {+ guard outputType == .audio else { return }+ self.collector.ingest(sampleBuffer)+ }+}++private final class TrialAStreamDelegate: NSObject, SCStreamDelegate, @unchecked Sendable {+ private let lock = NSLock()+ private var stoppedWithErrorCount = 0+ var errorCount: Int { lock.lock(); defer { lock.unlock() }; return stoppedWithErrorCount }+ func stream(_ stream: SCStream, didStopWithError error: Error) {+ lock.lock(); stoppedWithErrorCount += 1; lock.unlock()+ }+}++private extension JSONEncoder {+ static var sorted: JSONEncoder {+ let encoder = JSONEncoder(); encoder.outputFormatting = [.sortedKeys]; return encoder+ }+}++#endifdiff --git a/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift b/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swiftindex 8be19fcb..882515ee 100644--- a/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift+++ b/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift@@ -316,6 +316,264 @@ nonisolated struct MeetingMicrophoneSettledConfig: Codable, Equatable, Sendablevar outputChannelDecision: String}+// MARK: - Voice-processing read-back probe++/// One exact `AudioUnitGetProperty` result. A failed read never carries a stale value.+nonisolated struct MeetingAudioUnitUInt32Readback: Codable, Equatable, Sendable {+ var propertyID: UInt32+ var scope: UInt32+ var element: UInt32+ var status: OSStatus+ var value: UInt32?++ init(+ propertyID: UInt32,+ scope: UInt32,+ element: UInt32,+ status: OSStatus,+ value: UInt32?+ ) {+ self.propertyID = propertyID+ self.scope = scope+ self.element = element+ self.status = status+ self.value = status == noErr ? value : nil+ }++ var succeeded: Bool { self.status == noErr }+}++nonisolated struct MeetingAudioUnitDuckingReadback: Codable, Equatable, Sendable {+ var propertyID: UInt32+ var scope: UInt32+ var element: UInt32+ var status: OSStatus+ var advancedDuckingEnabled: Bool?+ var duckingLevelRawValue: UInt32?++ init(+ propertyID: UInt32,+ scope: UInt32,+ element: UInt32,+ status: OSStatus,+ advancedDuckingEnabled: Bool?,+ duckingLevelRawValue: UInt32?+ ) {+ self.propertyID = propertyID+ self.scope = scope+ self.element = element+ self.status = status+ self.advancedDuckingEnabled = status == noErr ? advancedDuckingEnabled : nil+ self.duckingLevelRawValue = status == noErr ? duckingLevelRawValue : nil+ }+}++nonisolated struct MeetingAudioFormatProbeReadback: Codable, Equatable, Sendable {+ var sampleRate: Double+ var channelCount: UInt32+ var commonFormatRawValue: UInt+ var interleaved: Bool++ init(_ format: AVAudioFormat) {+ self.sampleRate = format.sampleRate+ self.channelCount = format.channelCount+ self.commonFormatRawValue = format.commonFormat.rawValue+ self.interleaved = format.isInterleaved+ }+}++nonisolated struct MeetingAudioTimeProbeReadback: Codable, Equatable, Sendable {+ var hostTime: UInt64?+ var sampleTime: Int64?+ var sampleRate: Double?++ init(_ time: AVAudioTime?) {+ self.hostTime = time?.isHostTimeValid == true ? time?.hostTime : nil+ self.sampleTime = time?.isSampleTimeValid == true ? time?.sampleTime : nil+ self.sampleRate = time?.isSampleTimeValid == true ? time?.sampleRate : nil+ }+}++/// Read-only evidence from the already-running VPIO graph. This is probe output, not runtime+/// provenance: the values are intentionally kept out of `MeetingMicrophoneSettledConfig` until+/// the hardware matrix establishes which properties are stable and decision-relevant.+nonisolated struct MeetingVoiceProcessingProbeSnapshot: Codable, Equatable, Sendable {+ static let currentSchemaVersion = 1++ var schemaVersion: Int+ var engineRunning: Bool+ var nodeVoiceProcessingEnabled: Bool+ var nodeVoiceProcessingBypassed: Bool+ var nodeVoiceProcessingAGCEnabled: Bool+ var nodeVoiceProcessingInputMuted: Bool+ var nodeAdvancedDuckingEnabled: Bool+ var nodeDuckingLevelRawValue: Int+ var inputPresentationLatencySeconds: Double+ var outputPresentationLatencySeconds: Double+ var inputNodeInputFormat: MeetingAudioFormatProbeReadback+ var inputNodeOutputFormat: MeetingAudioFormatProbeReadback+ var outputNodeInputFormat: MeetingAudioFormatProbeReadback+ var outputNodeOutputFormat: MeetingAudioFormatProbeReadback+ var inputNodeLastRenderTime: MeetingAudioTimeProbeReadback+ var outputNodeLastRenderTime: MeetingAudioTimeProbeReadback+ var inputNodeOutputConnectionCount: Int+ var inputCurrentDevice: MeetingAudioUnitUInt32Readback+ var outputCurrentDevice: MeetingAudioUnitUInt32Readback+ var bypassVoiceProcessing: MeetingAudioUnitUInt32Readback+ var voiceProcessingAGCEnabled: MeetingAudioUnitUInt32Readback+ var voiceProcessingOutputMuted: MeetingAudioUnitUInt32Readback+ var otherAudioDucking: MeetingAudioUnitDuckingReadback+}++nonisolated enum MeetingVoiceProcessingProbeError: Error, Sendable {+ case disabled+ case captureNotRunning+ case invalidPlaybackFormat+ case playbackTimedOut+}++#if DEBUG+/// Bridges player completion and task cancellation without ever resuming a checked continuation+/// twice. Cancellation resumes the task immediately; the actor's deferred cleanup then owns every+/// engine mutation.+private final nonisolated class MeetingVPIOAcousticPlaybackContinuation: @unchecked Sendable {+ private nonisolated let lock = NSLock()+ private var continuation: CheckedContinuation<Void, Error>?+ private var cancelled = false++ nonisolated func install(_ continuation: CheckedContinuation<Void, Error>) {+ let shouldCancel: Bool+ self.lock.lock()+ shouldCancel = self.cancelled+ if !shouldCancel { self.continuation = continuation }+ self.lock.unlock()+ if shouldCancel { continuation.resume(throwing: CancellationError()) }+ }++ nonisolated func finish() {+ self.resume { $0.resume() }+ }++ nonisolated func cancel() {+ self.lock.lock()+ self.cancelled = true+ let continuation = self.continuation+ self.continuation = nil+ self.lock.unlock()+ continuation?.resume(throwing: CancellationError())+ }++ nonisolated func timeout() {+ self.resume { $0.resume(throwing: MeetingVoiceProcessingProbeError.playbackTimedOut) }+ }++ private nonisolated func resume(_ body: (CheckedContinuation<Void, Error>) -> Void) {+ self.lock.lock()+ let continuation = self.continuation+ let wasCancelled = self.cancelled+ self.continuation = nil+ self.lock.unlock()+ if wasCancelled {+ continuation?.resume(throwing: CancellationError())+ } else if let continuation {+ body(continuation)+ }+ }+}++/// Numeric-only mixer tap accumulator. It records no audio; the sample values are reduced as they+/// arrive so the render confirmation can be emitted without retaining the reference waveform.+final nonisolated class MeetingVPIOAcousticRenderTapCollector: @unchecked Sendable {+ private nonisolated struct State: Sendable {+ var sampleRate = 0.0+ var channelCount = 0+ var frameCount = 0+ var invalidHostTimeCount = 0+ var sumSquares = 0.0+ var peak = 0.0+ var renderStartHostSeconds: Double?+ }++ private nonisolated let lock = OSAllocatedUnfairLock(initialState: State())++ nonisolated func ingest(buffer: AVAudioPCMBuffer, time: AVAudioTime?) {+ guard let channels = buffer.floatChannelData, buffer.frameLength > 0 else { return }+ let frames = Int(buffer.frameLength)+ let channelsCount = max(1, Int(buffer.format.channelCount))+ let rate = buffer.format.sampleRate+ let hostStart = time?.isHostTimeValid == true ? MeetingVPIOAcousticHostClock.seconds(time!.hostTime) : nil+ var sumSquares = 0.0+ var peak = 0.0+ var firstActivityHostSeconds: Double?+ for index in 0..<frames {+ var value = 0.0+ for channel in 0..<channelsCount { value += Double(channels[channel][index]) }+ value /= Double(channelsCount)+ sumSquares += value * value+ peak = max(peak, abs(value))+ if firstActivityHostSeconds == nil, abs(value) > 1e-5, let hostStart {+ firstActivityHostSeconds = hostStart + Double(index) / max(rate, 1)+ }+ }+ let reducedSumSquares = sumSquares+ let reducedPeak = peak+ let reducedFirstActivityHostSeconds = firstActivityHostSeconds+ self.lock.withLock { state in+ state.sampleRate = rate+ state.channelCount = channelsCount+ state.frameCount += frames+ if hostStart == nil { state.invalidHostTimeCount += 1 }+ state.sumSquares += reducedSumSquares+ state.peak = max(state.peak, reducedPeak)+ if state.renderStartHostSeconds == nil {+ state.renderStartHostSeconds = reducedFirstActivityHostSeconds+ }+ }+ }++ nonisolated func report(+ scheduledFrameCount: Int,+ sampleRate: Double,+ volume: Float,+ outputPreflight: MeetingVPIOAcousticOutputPreflight+ ) -> MeetingVPIOAcousticRenderConfirmation {+ let state = self.lock.withLock { $0 }+ let renderRMS = state.frameCount > 0 ? sqrt(state.sumSquares / Double(state.frameCount)) : 0+ var reasons: [MeetingVPIOAcousticReason] = []+ if state.frameCount == 0 || state.peak <= 1e-5 { reasons.append(.renderReadbackSilent) }+ if state.renderStartHostSeconds == nil { reasons.append(.renderStartUnresolved) }+ if state.invalidHostTimeCount > 0 { reasons.append(.renderTimingInvalid) }+ reasons.append(contentsOf: outputPreflight.reasons)+ return MeetingVPIOAcousticRenderConfirmation(+ scheduledFrameCount: scheduledFrameCount,+ sampleRate: sampleRate,+ volume: Double(volume),+ mixerSampleRate: state.sampleRate,+ mixerChannelCount: state.channelCount,+ tapFrameCount: state.frameCount,+ tapRMS: renderRMS,+ tapPeak: state.peak,+ tapInvalidHostTimeCount: state.invalidHostTimeCount,+ renderStartHostSeconds: state.renderStartHostSeconds,+ playerDerivedStartHostSeconds: nil,+ playerRenderedFrameCount: nil,+ renderStartResolved: state.renderStartHostSeconds != nil,+ outputConnectionCount: outputPreflight.outputConnectionCount,+ outputDeviceID: outputPreflight.outputDeviceID,+ defaultOutputDeviceID: outputPreflight.defaultOutputDeviceID,+ outputRouteConfirmed: outputPreflight.outputRouteConfirmed,+ systemVolume: outputPreflight.systemVolume,+ outputVolumeReadable: outputPreflight.outputVolumeReadable,+ combinedPeak: outputPreflight.combinedPeak,+ reasons: {+ var seen = Set<MeetingVPIOAcousticReason>()+ return reasons.filter { seen.insert($0).inserted }.sorted { $0.rawValue < $1.rawValue }+ }()+ )+ }+}+#endif+// MARK: - Capture eventsnonisolated enum MeetingMicrophoneCaptureEvent: Sendable {@@ -441,6 +699,7 @@ private final class MeetingMicrophoneTapState: @unchecked Sendable {private let statsBox: MeetingMicrophoneCaptureStatsprivate let canonicalFormat: AVAudioFormatprivate let onSample: @Sendable (CMSampleBuffer) -> Void+ private let onSampleMetadata: @Sendable (CMSampleBuffer, Bool, Bool) -> Voidprivate var clock = MeetingMicrophonePTSClock()private var converter: AVAudioConverter?@@ -453,11 +712,13 @@ private final class MeetingMicrophoneTapState: @unchecked Sendable {init(statsBox: MeetingMicrophoneCaptureStats,canonicalFormat: AVAudioFormat,- onSample: @escaping @Sendable (CMSampleBuffer) -> Void+ onSample: @escaping @Sendable (CMSampleBuffer) -> Void,+ onSampleMetadata: @escaping @Sendable (CMSampleBuffer, Bool, Bool) -> Void = { _, _, _ in }) {self.statsBox = statsBoxself.canonicalFormat = canonicalFormatself.onSample = onSample+ self.onSampleMetadata = onSampleMetadata}func handle(buffer: AVAudioPCMBuffer, time: AVAudioTime) {@@ -525,6 +786,7 @@ private final class MeetingMicrophoneTapState: @unchecked Sendable {stats.firstValidHostSeconds = clock.firstValidHostSecondsstats.lastValidHostSeconds = clock.lastValidHostSeconds}+ self.onSampleMetadata(sampleBuffer, synthesized, resynced)self.onSample(sampleBuffer)}@@ -637,6 +899,7 @@ actor MeetingMicrophoneCapture {private let generationBox = MeetingMicrophoneGenerationBox()private var engine: AVAudioEngine?+ private var probePlayerNode: AVAudioPlayerNode?private var tapState: MeetingMicrophoneTapState?private var isRunning = false/// Non-nil from the instant a start reserves the actor until that generation is stopped or fails.@@ -652,6 +915,7 @@ actor MeetingMicrophoneCapture {microphone: MeetingMicrophoneIdentity,authorizationPreflighted: Bool = false,onSample: @escaping @Sendable (CMSampleBuffer) -> Void,+ onSampleMetadata: @escaping @Sendable (CMSampleBuffer, Bool, Bool) -> Void = { _, _, _ in },onEvent: @escaping @Sendable (MeetingMicrophoneCaptureEvent) -> Void = { _ in }) async throws -> MeetingMicrophoneBindingOutcome {guard self.activeGeneration == nil else { throw MeetingMicrophoneCaptureError.alreadyStarted }@@ -727,7 +991,12 @@ actor MeetingMicrophoneCapture {guard preStartFormat.sampleRate > 0, preStartFormat.channelCount > 0 else {return self.fail(microphone: microphone, reason: "Input format invalid before start (\(preStartFormat)).", generation: currentGeneration)}- let tapState = MeetingMicrophoneTapState(statsBox: self.statsBox, canonicalFormat: Self.canonicalFormat, onSample: onSample)+ let tapState = MeetingMicrophoneTapState(+ statsBox: self.statsBox,+ canonicalFormat: Self.canonicalFormat,+ onSample: onSample,+ onSampleMetadata: onSampleMetadata+ )self.tapState = tapStatelet generationBox = self.generationBoxinput.installTap(onBus: 0, bufferSize: 4_800, format: preStartFormat) { buffer, time in@@ -779,6 +1048,10 @@ actor MeetingMicrophoneCapture {guard self.ownsGeneration(currentGeneration, engine: engine) else {throw MeetingMicrophoneCaptureError.lifecycleInterrupted}+ if Task.isCancelled {+ await self.abortStartGeneration(currentGeneration, engine: engine)+ throw MeetingMicrophoneCaptureError.lifecycleInterrupted+ }input.removeTap(onBus: 0)engine.stop()self.engine = nil@@ -786,7 +1059,10 @@ actor MeetingMicrophoneCapture {self.isRunning = falsereturn self.fail(microphone: microphone, reason: "Input format never became valid (persistent 0 Hz).", generation: currentGeneration)}- guard self.ownsGeneration(currentGeneration, engine: engine) else {+ guard !Task.isCancelled, self.ownsGeneration(currentGeneration, engine: engine) else {+ if self.ownsGeneration(currentGeneration, engine: engine) {+ await self.abortStartGeneration(currentGeneration, engine: engine)+ }throw MeetingMicrophoneCaptureError.lifecycleInterrupted}if sourceFormat != preStartFormat {@@ -816,7 +1092,10 @@ actor MeetingMicrophoneCapture {generation: currentGeneration,onEvent: onEvent)- guard self.ownsGeneration(currentGeneration, engine: engine) else {+ guard !Task.isCancelled, self.ownsGeneration(currentGeneration, engine: engine) else {+ if self.ownsGeneration(currentGeneration, engine: engine) {+ await self.abortStartGeneration(currentGeneration, engine: engine)+ }throw MeetingMicrophoneCaptureError.lifecycleInterrupted}#if DEBUG@@ -828,6 +1107,19 @@ actor MeetingMicrophoneCapture {}func stop() async {+ await self.stop(teardownVoiceProcessingUnit: true)+ }++ /// DEBUG diagnostic teardown that leaves the VPIO unit to process-boundary cleanup. This is+ /// used only by isolated acoustic probes because explicit unit replacement can crash on some+ /// macOS 26 audio stacks after a live output read-back.+#if DEBUG+ func stopForPhase1Probe() async {+ await self.stop(teardownVoiceProcessingUnit: false)+ }+#endif++ private func stop(teardownVoiceProcessingUnit: Bool) async {guard self.activeGeneration != nil else { return }self.isRunning = falselet stopGeneration = self.generationBox.advance()@@ -836,11 +1128,13 @@ actor MeetingMicrophoneCapture {// Snapshot and detach every generation-owned reference before the first await. A new start// may legally enter while listener removal is suspended and must remain completely isolated.let oldEngine = self.engine+ let oldProbePlayerNode = self.probePlayerNodelet oldConfigObserver = self.configObserverlet oldDefaultInputListenerToken = self.defaultInputListenerTokenlet oldOverloadListenerToken = self.overloadListenerTokenlet oldOverloadListenerDeviceID = self.overloadListenerDeviceIDself.engine = nil+ self.probePlayerNode = nilself.tapState = nilself.configObserver = nilself.defaultInputListenerToken = nil@@ -851,6 +1145,14 @@ actor MeetingMicrophoneCapture {NotificationCenter.default.removeObserver(oldConfigObserver)}if let oldEngine {+ oldProbePlayerNode?.stop()+ if let oldProbePlayerNode {+ // A probe player can still be attached while its playback continuation is+ // unwinding. Remove it from the graph before stopping the old engine so a new+ // generation cannot inherit a stale render connection.+ oldEngine.disconnectNodeOutput(oldProbePlayerNode)+ oldEngine.detach(oldProbePlayerNode)+ }oldEngine.inputNode.removeTap(onBus: 0)#if DEBUGAudioTopologyDiagnostics.record(.engineStopBegin, owner: .meetingMicrophone, queueRole: .actorControl, phase: .engine, generation: stopGeneration)@@ -859,7 +1161,9 @@ actor MeetingMicrophoneCapture {// Explicitly tear down the VPIO Audio Unit. Relying on AVAudioEngine// deallocation leaves device ownership to ARC timing and can overlap a// subsequent AVCaptureSession construction during route recovery.- try? oldEngine.inputNode.setVoiceProcessingEnabled(false)+ if teardownVoiceProcessingUnit {+ try? oldEngine.inputNode.setVoiceProcessingEnabled(false)+ }#if DEBUGAudioTopologyDiagnostics.record(.engineStopEnd, owner: .meetingMicrophone, queueRole: .actorControl, phase: .engine, status: noErr, generation: stopGeneration)#endif@@ -891,6 +1195,352 @@ actor MeetingMicrophoneCapture {self.settled}+ /// Snapshot VPIO state only while the generation is live. Returning `nil` after teardown+ /// prevents a deceptively successful read from an Audio Unit that no longer owns a device.+ func voiceProcessingProbeReadback() -> MeetingVoiceProcessingProbeSnapshot? {+ guard self.isRunning, let engine = self.engine else { return nil }+ let input = engine.inputNode+ let output = engine.outputNode+ guard let audioUnit = input.audioUnit else { return nil }++ let ducking = input.voiceProcessingOtherAudioDuckingConfiguration+ return MeetingVoiceProcessingProbeSnapshot(+ schemaVersion: MeetingVoiceProcessingProbeSnapshot.currentSchemaVersion,+ engineRunning: engine.isRunning,+ nodeVoiceProcessingEnabled: input.isVoiceProcessingEnabled,+ nodeVoiceProcessingBypassed: input.isVoiceProcessingBypassed,+ nodeVoiceProcessingAGCEnabled: input.isVoiceProcessingAGCEnabled,+ nodeVoiceProcessingInputMuted: input.isVoiceProcessingInputMuted,+ nodeAdvancedDuckingEnabled: ducking.enableAdvancedDucking.boolValue,+ nodeDuckingLevelRawValue: ducking.duckingLevel.rawValue,+ inputPresentationLatencySeconds: input.presentationLatency,+ outputPresentationLatencySeconds: output.presentationLatency,+ inputNodeInputFormat: MeetingAudioFormatProbeReadback(input.inputFormat(forBus: 0)),+ inputNodeOutputFormat: MeetingAudioFormatProbeReadback(input.outputFormat(forBus: 0)),+ outputNodeInputFormat: MeetingAudioFormatProbeReadback(output.inputFormat(forBus: 0)),+ outputNodeOutputFormat: MeetingAudioFormatProbeReadback(output.outputFormat(forBus: 0)),+ inputNodeLastRenderTime: MeetingAudioTimeProbeReadback(input.lastRenderTime),+ outputNodeLastRenderTime: MeetingAudioTimeProbeReadback(output.lastRenderTime),+ inputNodeOutputConnectionCount: engine.outputConnectionPoints(for: input, outputBus: 0).count,+ inputCurrentDevice: Self.readUInt32(+ audioUnit,+ propertyID: kAudioOutputUnitProperty_CurrentDevice,+ scope: kAudioUnitScope_Global,+ element: Self.inputElement+ ),+ outputCurrentDevice: Self.readUInt32(+ audioUnit,+ propertyID: kAudioOutputUnitProperty_CurrentDevice,+ scope: kAudioUnitScope_Global,+ element: Self.outputElement+ ),+ bypassVoiceProcessing: Self.readUInt32(+ audioUnit,+ propertyID: kAUVoiceIOProperty_BypassVoiceProcessing,+ scope: kAudioUnitScope_Global,+ element: 0+ ),+ voiceProcessingAGCEnabled: Self.readUInt32(+ audioUnit,+ propertyID: kAUVoiceIOProperty_VoiceProcessingEnableAGC,+ scope: kAudioUnitScope_Global,+ element: 0+ ),+ voiceProcessingOutputMuted: Self.readUInt32(+ audioUnit,+ propertyID: kAUVoiceIOProperty_MuteOutput,+ scope: kAudioUnitScope_Global,+ element: 0+ ),+ otherAudioDucking: Self.readDuckingConfiguration(+ audioUnit,+ propertyID: kAUVoiceIOProperty_OtherAudioDuckingConfiguration,+ scope: kAudioUnitScope_Global,+ element: 0+ )+ )+ }++#if DEBUG+ /// Read-only safety check for the local Trial B render. The system volume is never changed;+ /// unsupported or unexpectedly loud routes are reported as numeric refusal reasons.+ func acousticOutputPreflight(samplePeak: Double, volume: Float) -> MeetingVPIOAcousticOutputPreflight {+ guard self.isRunning, let engine = self.engine else {+ return MeetingVPIOAcousticOutputPreflight(+ outputConnectionCount: 0,+ outputDeviceID: nil,+ defaultOutputDeviceID: nil,+ outputRouteConfirmed: false,+ systemVolume: nil,+ outputVolumeReadable: false,+ combinedPeak: nil,+ reasons: [.captureNotRunning, .outputRouteUnavailable]+ )+ }++ let mixer = engine.mainMixerNode+ let outputConnectionCount = engine.outputConnectionPoints(for: mixer, outputBus: 0).count+ let snapshot = self.voiceProcessingProbeReadback()+ let outputDeviceID = snapshot?.outputCurrentDevice.value+ let defaultOutputDeviceID = AudioDevice.getDefaultOutputDevice()?.id+ let outputFormat = engine.outputNode.outputFormat(forBus: 0)+ let routeSnapshot = MeetingCaptureEngine.currentOutputRouteSnapshot()+ let routePolicyConfirmed = MeetingCapturePathDecider.outputRouteDeclineReason(routeSnapshot) == nil+ let routeConfirmed = engine.isRunning+ && outputConnectionCount > 0+ && outputFormat.sampleRate.isFinite+ && outputFormat.sampleRate > 0+ && outputFormat.channelCount > 0+ && outputDeviceID == defaultOutputDeviceID+ && outputDeviceID.map { $0 != kAudioObjectUnknown } == true+ && outputDeviceID.map(Self.isProbeOutputDeviceAlive) == true+ && routeSnapshot.deviceExists+ && routeSnapshot.isBuiltIn+ && !routeSnapshot.isBluetooth+ && !routeSnapshot.isHeadphonesDataSource+ && !routeSnapshot.terminalTypes.contains(kAudioStreamTerminalTypeHeadphones)+ && routePolicyConfirmed++ var reasons: [MeetingVPIOAcousticReason] = []+ if routeConfirmed == false { reasons.append(.outputRouteUnavailable) }++ let outputVolume = outputDeviceID.flatMap(Self.readProbeOutputVolume)+ let outputVolumeReadable = outputVolume.map { $0.isFinite && (0...1).contains($0) } == true+ if outputVolumeReadable == false { reasons.append(.outputVolumeUnreadable) }++ let safeDigitalPeak = samplePeak.isFinite && samplePeak >= 0 ? samplePeak : 0+ let safePlayerVolume = volume.isFinite ? min(max(Double(volume), 0), Double(MeetingVPIOAcousticStimulus.maximumRenderVolume)) : 0+ let combinedPeak = outputVolume.map { safeDigitalPeak * safePlayerVolume * Double($0) }+ if let outputVolume,+ Double(outputVolume) <= 0 || Double(outputVolume) > MeetingVPIOAcousticGate.maximumSystemVolume {+ reasons.append(.outputLevelUnsafe)+ }+ if let combinedPeak, combinedPeak > MeetingVPIOAcousticGate.maximumCombinedPeak {+ reasons.append(.outputLevelUnsafe)+ }++ return MeetingVPIOAcousticOutputPreflight(+ outputConnectionCount: outputConnectionCount,+ outputDeviceID: outputDeviceID,+ defaultOutputDeviceID: defaultOutputDeviceID,+ outputRouteConfirmed: routeConfirmed,+ systemVolume: outputVolume.map(Double.init),+ outputVolumeReadable: outputVolumeReadable,+ combinedPeak: combinedPeak,+ reasons: reasons.sorted { $0.rawValue < $1.rawValue }+ )+ }++ private static func isProbeOutputDeviceAlive(_ deviceID: AudioObjectID) -> Bool {+ var address = AudioObjectPropertyAddress(+ mSelector: kAudioDevicePropertyDeviceIsAlive,+ mScope: kAudioObjectPropertyScopeGlobal,+ mElement: kAudioObjectPropertyElementMain+ )+ var alive: UInt32 = 0+ var size = UInt32(MemoryLayout<UInt32>.size)+ let status = AudioObjectGetPropertyData(deviceID, &address, 0, nil, &size, &alive)+ return status == noErr && alive != 0+ }++ private static func readProbeOutputVolume(_ deviceID: AudioObjectID) -> Float32? {+ let selectors: [AudioObjectPropertySelector] = [+ kAudioHardwareServiceDeviceProperty_VirtualMainVolume,+ kAudioDevicePropertyVolumeScalar,+ ]+ for selector in selectors {+ var address = AudioObjectPropertyAddress(+ mSelector: selector,+ mScope: kAudioDevicePropertyScopeOutput,+ mElement: kAudioObjectPropertyElementMain+ )+ var volume: Float32 = 0+ var size = UInt32(MemoryLayout<Float32>.size)+ let status = AudioObjectGetPropertyData(deviceID, &address, 0, nil, &size, &volume)+ if status == noErr, volume.isFinite, (0...1).contains(volume) { return volume }+ }+ return nil+ }+#endif++ /// Probe-only mutation. The environment gate prevents production callers from changing VPIO+ /// processing state through this diagnostic surface.+#if DEBUG+ func setVoiceProcessingBypassedForProbe(_ bypassed: Bool) -> Bool? {+ guard MeetingVPIOAcousticGate.isEnabled else {+ return nil+ }+ guard self.isRunning, let input = self.engine?.inputNode else { return nil }+ input.isVoiceProcessingBypassed = bypassed+ return input.isVoiceProcessingBypassed+ }++ /// Probe-only AGC mutation. The getter is public on supported AVFAudio releases; if a+ /// platform rejects the setter, the caller records the read-back and continues the trial as+ /// an unsupported variant rather than changing production defaults.+ func setVoiceProcessingAGCEnabledForProbe(_ enabled: Bool) -> Bool? {+ guard MeetingVPIOAcousticGate.isEnabled else { return nil }+ guard self.isRunning, let input = self.engine?.inputNode else { return nil }+ input.isVoiceProcessingAGCEnabled = enabled+ return input.isVoiceProcessingAGCEnabled+ }+#else+ func setVoiceProcessingBypassedForProbe(_ bypassed: Bool) -> Bool? { nil }+ func setVoiceProcessingAGCEnabledForProbe(_ enabled: Bool) -> Bool? { nil }+#endif++ /// Render known local PCM through this same VPIO engine. This is Trial B only; it cannot route+ /// another application's audio and is never reachable without the acoustic-probe environment.+#if DEBUG+ func playVoiceProcessingProbePCM(+ _ samples: [Float],+ sampleRate: Double,+ volume: Float+ ) async throws -> MeetingVPIOAcousticRenderConfirmation {+ guard MeetingVPIOAcousticGate.isEnabled else {+ throw MeetingVoiceProcessingProbeError.disabled+ }+ guard self.isRunning, let engine = self.engine else {+ throw MeetingVoiceProcessingProbeError.captureNotRunning+ }+ guard !samples.isEmpty, sampleRate.isFinite, sampleRate > 0 else {+ throw MeetingVoiceProcessingProbeError.invalidPlaybackFormat+ }++ var samplePeak = 0.0+ var hasNonFiniteSample = false+ for sample in samples {+ let value = Double(sample)+ if value.isFinite {+ samplePeak = max(samplePeak, abs(value))+ } else {+ hasNonFiniteSample = true+ }+ }++ let safeVolume = min(+ max(volume.isFinite ? volume : 0, 0),+ MeetingVPIOAcousticStimulus.maximumRenderVolume+ )+ var outputPreflight = await self.acousticOutputPreflight(+ samplePeak: samplePeak,+ volume: safeVolume+ )+ if hasNonFiniteSample || samplePeak > Double(MeetingVPIOAcousticStimulus.safetyPeakLimit) || samplePeak <= 0 {+ outputPreflight.reasons.append(.stimulusOutsideSafetyBounds)+ }+ let tapCollector = MeetingVPIOAcousticRenderTapCollector()+ guard outputPreflight.isSafe else {+ return tapCollector.report(+ scheduledFrameCount: samples.count,+ sampleRate: sampleRate,+ volume: safeVolume,+ outputPreflight: outputPreflight+ )+ }+ guard let format = AVAudioFormat(+ commonFormat: .pcmFormatFloat32,+ sampleRate: sampleRate,+ channels: 1,+ interleaved: false+ ),+ let buffer = AVAudioPCMBuffer(+ pcmFormat: format,+ frameCapacity: AVAudioFrameCount(samples.count)+ ),+ let channel = buffer.floatChannelData?[0]+ else {+ throw MeetingVoiceProcessingProbeError.invalidPlaybackFormat+ }+ samples.withUnsafeBufferPointer { source in+ guard let baseAddress = source.baseAddress else { return }+ channel.update(from: baseAddress, count: samples.count)+ }+ buffer.frameLength = AVAudioFrameCount(samples.count)++ let mixer = engine.mainMixerNode+ var tapInstalled = false+ var playerAttached = false+ var playerConnected = false+ var player: AVAudioPlayerNode?+ defer {+ player?.stop()+ if let player {+ if playerConnected { engine.disconnectNodeOutput(player) }+ if playerAttached { engine.detach(player) }+ if self.probePlayerNode === player { self.probePlayerNode = nil }+ }+ if tapInstalled { mixer.removeTap(onBus: 0) }+ }+ mixer.installTap(onBus: 0, bufferSize: 4_800, format: nil) { buffer, time in+ tapCollector.ingest(buffer: buffer, time: time)+ }+ tapInstalled = true+ let newPlayer = AVAudioPlayerNode()+ player = newPlayer+ self.probePlayerNode = newPlayer+ engine.attach(newPlayer)+ playerAttached = true+ engine.connect(newPlayer, to: mixer, format: format)+ playerConnected = true+ newPlayer.volume = safeVolume++ let playbackContinuation = MeetingVPIOAcousticPlaybackContinuation()+ let timeoutTask = Task {+ do {+ try await Task.sleep(nanoseconds: 10_000_000_000)+ } catch {+ return+ }+ playbackContinuation.timeout()+ }+ defer { timeoutTask.cancel() }+ try await withTaskCancellationHandler(operation: {+ try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in+ playbackContinuation.install(continuation)+ guard !Task.isCancelled else {+ playbackContinuation.cancel()+ return+ }+ newPlayer.scheduleBuffer(buffer, completionCallbackType: .dataPlayedBack) { _ in+ playbackContinuation.finish()+ }+ newPlayer.play()+ }+ }, onCancel: {+ playbackContinuation.cancel()+ })++ // Read the output topology while the player is still attached. Detaching first can make a+ // successful render appear disconnected and destroys the evidence needed by the report.+ let outputConnectionCount = engine.outputConnectionPoints(for: mixer, outputBus: 0).count+ outputPreflight.outputConnectionCount = outputConnectionCount+ if outputConnectionCount <= 0, outputPreflight.reasons.contains(.outputRouteUnavailable) == false {+ outputPreflight.outputRouteConfirmed = false+ outputPreflight.reasons.append(.outputRouteUnavailable)+ }+ if tapInstalled {+ mixer.removeTap(onBus: 0)+ tapInstalled = false+ }+ let report = tapCollector.report(+ scheduledFrameCount: samples.count,+ sampleRate: sampleRate,+ volume: safeVolume,+ outputPreflight: outputPreflight+ )+ return report+ }+#else+ func playVoiceProcessingProbePCM(+ _ samples: [Float],+ sampleRate: Double,+ volume: Float+ ) async throws {+ throw MeetingVoiceProcessingProbeError.disabled+ }+#endif+private func ownsGeneration(_ generation: UInt64, engine: AVAudioEngine) -> Bool {self.activeGeneration == generation&& self.generationBox.isCurrent(generation)@@ -898,6 +1548,41 @@ actor MeetingMicrophoneCapture {&& self.isRunning}+ /// Abort only the generation that published this engine. This is used by cancellation and+ /// post-publication startup failures; calling the general stop path here could accidentally+ /// tear down a newer generation after actor reentrancy.+ private func abortStartGeneration(_ generation: UInt64, engine: AVAudioEngine) async {+ guard self.activeGeneration == generation,+ self.generationBox.isCurrent(generation),+ self.engine === engine+ else { return }++ self.isRunning = false+ _ = self.generationBox.advance()+ self.activeGeneration = nil+ let oldConfigObserver = self.configObserver+ let oldDefaultInputListenerToken = self.defaultInputListenerToken+ let oldOverloadListenerToken = self.overloadListenerToken+ let oldOverloadListenerDeviceID = self.overloadListenerDeviceID+ self.engine = nil+ self.probePlayerNode = nil+ self.tapState = nil+ self.configObserver = nil+ self.defaultInputListenerToken = nil+ self.overloadListenerToken = nil+ self.overloadListenerDeviceID = nil++ if let oldConfigObserver { NotificationCenter.default.removeObserver(oldConfigObserver) }+ engine.inputNode.removeTap(onBus: 0)+ engine.stop()+ await Self.removeEventObservers(+ defaultInputToken: oldDefaultInputListenerToken,+ overloadToken: oldOverloadListenerToken,+ overloadDeviceID: oldOverloadListenerDeviceID,+ generation: generation+ )+ }+private func fail(microphone: MeetingMicrophoneIdentity,reason: String,@@ -931,7 +1616,9 @@ actor MeetingMicrophoneCapture {while format.sampleRate == 0 || format.channelCount == 0 {attempt += 1if attempt > Self.formatRetryLimit { return nil }+ if Task.isCancelled { return nil }try? await Task.sleep(nanoseconds: Self.formatRetryDelayNanoseconds)+ if Task.isCancelled { return nil }format = input.outputFormat(forBus: 0)}return format@@ -1115,6 +1802,43 @@ actor MeetingMicrophoneCapture {)return status == noErr ? deviceID : nil}++ private static func readUInt32(+ _ audioUnit: AudioUnit,+ propertyID: AudioUnitPropertyID,+ scope: AudioUnitScope,+ element: AudioUnitElement+ ) -> MeetingAudioUnitUInt32Readback {+ var value: UInt32 = 0+ var size = UInt32(MemoryLayout<UInt32>.size)+ let status = AudioUnitGetProperty(audioUnit, propertyID, scope, element, &value, &size)+ return MeetingAudioUnitUInt32Readback(+ propertyID: propertyID,+ scope: scope,+ element: element,+ status: status,+ value: value+ )+ }++ private static func readDuckingConfiguration(+ _ audioUnit: AudioUnit,+ propertyID: AudioUnitPropertyID,+ scope: AudioUnitScope,+ element: AudioUnitElement+ ) -> MeetingAudioUnitDuckingReadback {+ var value = AUVoiceIOOtherAudioDuckingConfiguration()+ var size = UInt32(MemoryLayout<AUVoiceIOOtherAudioDuckingConfiguration>.size)+ let status = AudioUnitGetProperty(audioUnit, propertyID, scope, element, &value, &size)+ return MeetingAudioUnitDuckingReadback(+ propertyID: propertyID,+ scope: scope,+ element: element,+ status: status,+ advancedDuckingEnabled: value.mEnableAdvancedDucking.boolValue,+ duckingLevelRawValue: UInt32(value.mDuckingLevel.rawValue)+ )+ }}// MARK: - Opt-in hardware probe@@ -1125,6 +1849,10 @@ enum MeetingMicrophonePhase1Probe {var minutes: Doublevar outcome: MeetingMicrophoneBindingOutcomevar settled: MeetingMicrophoneSettledConfig?+ var voiceProcessingReadback: MeetingVoiceProcessingProbeSnapshot?+#if DEBUG+ var acousticTrialB: MeetingVPIOAcousticTrialBReport?+#endifvar stats: MeetingMicrophoneCaptureStats.Snapshotvar writerDiscontinuities: Intvar writerRotationCount: Int@@ -1147,7 +1875,7 @@ enum MeetingMicrophonePhase1Probe {microphone = try await MeetingCaptureSourceCatalog.defaultMicrophone()}- var writerBackpressureEvents = 0+ let writerBackpressureEventsBox = OSAllocatedUnfairLock(initialState: 0)let track = MeetingAudioTrack(id: UUID(),kind: .microphone,@@ -1172,50 +1900,119 @@ enum MeetingMicrophonePhase1Probe {sessionDirectory: sessionDirectory,chunkDuration: MeetingCaptureConfiguration.defaultChunkDuration) { event in- if case .interrupted(.writerBackpressure, _, _) = event { writerBackpressureEvents += 1 }+ if case .interrupted(.writerBackpressure, _, _) = event {+ writerBackpressureEventsBox.withLock { $0 += 1 }+ }}var deliveredSourceFormat = "(none delivered)"let capturedFormat = OSAllocatedUnfairLock<String?>(initialState: nil)let tapTelemetry = OSAllocatedUnfairLock<(nilCopies: Int, peak: Float)>(initialState: (0, 0))+#if DEBUG+ let acousticCollector = MeetingVPIOAcousticCaptureCollector()+#endiflet capture = MeetingMicrophoneCapture()- let outcome = try await capture.start(microphone: microphone) { sampleBuffer in+ let outcome: MeetingMicrophoneBindingOutcome+ do {+ outcome = try await capture.start(+ microphone: microphone,+ onSample: { sampleBuffer inif let description = CMSampleBufferGetFormatDescription(sampleBuffer),let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(description)?.pointee{capturedFormat.withLock { $0 = "\(asbd.mSampleRate)Hz \(asbd.mChannelsPerFrame)ch" }}let copied = MeetingLiveSampleCopy.copy(sampleBuffer)- var bufferPeak: Float = 0- if let channel = copied?.buffer.floatChannelData?[0], let frames = copied.map({ Int($0.buffer.frameLength) }) {- vDSP_maxmgv(channel, 1, &bufferPeak, vDSP_Length(frames))- }+ let bufferPeak: Float = copied.flatMap { sample -> Float? in+ guard let channel = sample.buffer.floatChannelData?[0] else { return nil }+ var peak: Float = 0+ vDSP_maxmgv(channel, 1, &peak, vDSP_Length(sample.buffer.frameLength))+ return peak+ } ?? 0tapTelemetry.withLock {if copied == nil { $0.nilCopies += 1 }$0.peak = max($0.peak, bufferPeak)}writer.enqueue(sampleBuffer)+ },+ onSampleMetadata: { sampleBuffer, synthesized, resynced in+#if DEBUG+ acousticCollector.ingest(sampleBuffer, synthesized: synthesized, resynced: resynced)+ #else+ _ = (sampleBuffer, synthesized, resynced)+ #endif+ }+ )+ } catch {+ _ = await writer.stop()+ throw error}- try await Task.sleep(nanoseconds: UInt64(minutes * 60 * 1_000_000_000))+#if DEBUG+ let acousticTrialB = await MeetingVPIOAcousticTrialB.run(+ capture: capture, collector: acousticCollector, sampleRate: 48_000+ )+#endif++ do {+ try await Task.sleep(nanoseconds: UInt64(minutes * 60 * 1_000_000_000))+ } catch {+ // Cancellation after the engine was published must stop this exact generation and+ // close the writer before the probe task exits; otherwise callbacks can outlive the+ // diagnostic and retain a live input route.+#if DEBUG+ await capture.stopForPhase1Probe()+#else+ await capture.stop()+#endif+ _ = await writer.stop()+ throw error+ }+ let voiceProcessingReadback = await capture.voiceProcessingProbeReadback()+ // The app-hosted diagnostic process exits after this one test. Avoid the explicit VPIO+ // replacement during teardown here: on macOS 26.6 that replacement crashes inside+ // AVAudioIONode after a live output-node read-back. Normal product teardown is unchanged.+#if DEBUG+ await capture.stopForPhase1Probe()+#elseawait capture.stop()+#endiflet finishedTrack = await writer.stop()deliveredSourceFormat = capturedFormat.withLock { $0 } ?? deliveredSourceFormatlet telemetry = tapTelemetry.withLock { $0 }+#if DEBUG+ return Result(+ minutes: minutes,+ outcome: outcome,+ settled: await capture.settledConfiguration(),+ voiceProcessingReadback: voiceProcessingReadback,+ acousticTrialB: acousticTrialB,+ stats: await capture.statistics(),+ writerDiscontinuities: finishedTrack.chunks.reduce(0) { $0 + $1.discontinuities.count },+ writerRotationCount: finishedTrack.chunks.count,+ writerBackpressureEvents: writerBackpressureEventsBox.withLock { $0 },+ finalHealthStatus: finishedTrack.health.status,+ liveCopyNilCount: telemetry.nilCopies,+ deliveredSourceFormat: deliveredSourceFormat,+ peakAmplitude: telemetry.peak+ )+#elsereturn Result(minutes: minutes,outcome: outcome,settled: await capture.settledConfiguration(),+ voiceProcessingReadback: voiceProcessingReadback,stats: await capture.statistics(),writerDiscontinuities: finishedTrack.chunks.reduce(0) { $0 + $1.discontinuities.count },writerRotationCount: finishedTrack.chunks.count,- writerBackpressureEvents: writerBackpressureEvents,+ writerBackpressureEvents: writerBackpressureEventsBox.withLock { $0 },finalHealthStatus: finishedTrack.health.status,liveCopyNilCount: telemetry.nilCopies,deliveredSourceFormat: deliveredSourceFormat,peakAmplitude: telemetry.peak)+#endif}}diff --git a/Sources/Fluid/Services/Meeting/MeetingMicrophoneEvidence.swift b/Sources/Fluid/Services/Meeting/MeetingMicrophoneEvidence.swiftnew file mode 100644index 00000000..e9326791--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingMicrophoneEvidence.swift@@ -0,0 +1,181 @@+import Foundation++/// Stage A value contract only. No production call site evaluates or persists this evidence.+/// All times are seconds in the original session capture timeline, never display-adjusted time.+nonisolated struct MeetingMicrophoneEvidence: Equatable, Sendable {+ enum UnknownReason: String, Equatable, Sendable {+ case notMeasured, referenceUnavailable, noDelayLock, unscoredCoverage+ case scoredInconclusive, detectorUnavailable, overBudget, staleEpoch+ }++ enum Measurement<Value: Equatable & Sendable>: Equatable, Sendable {+ case unavailable(UnknownReason)+ case measured(Value)+ }++ struct Interval: Equatable, Sendable {+ let start: Double+ let end: Double+ init?(start: Double, end: Double) {+ guard start.isFinite, end.isFinite, start >= 0, end > start else { return nil }+ self.start = start+ self.end = end+ }+ }++ struct Fraction: Equatable, Sendable {+ let value: Double+ init?(_ value: Double) {+ guard value.isFinite, (0...1).contains(value) else { return nil }+ self.value = value+ }+ }++ enum Observation: Equatable, Sendable {+ /// Multiple turns can share this key; no individual turn can bless the shared centroid.+ case clusterLabel(String)+ case unavailable+ }++ struct Identity: Equatable, Sendable {+ let sessionID: UUID+ let chunkID: UUID+ let turnIndex: Int+ /// Stage B binds epochs to the pipeline's either-track discontinuity boundaries.+ let captureEpoch: UInt64+ let observation: Observation++ init?(sessionID: UUID, chunkID: UUID, turnIndex: Int, captureEpoch: UInt64, observation: Observation) {+ guard turnIndex >= 0 else { return nil }+ if case .clusterLabel(let label) = observation, label.isEmpty { return nil }+ self.sessionID = sessionID+ self.chunkID = chunkID+ self.turnIndex = turnIndex+ self.captureEpoch = captureEpoch+ self.observation = observation+ }++ var turnKey: String { "microphone:\(chunkID.uuidString):\(turnIndex)" }+ var observationKey: String? {+ guard case .clusterLabel(let label) = observation else { return nil }+ return "microphone:\(chunkID.uuidString):\(label)"+ }+ }++ enum PlaybackContext: Equatable, Sendable {+ case scoreablePlayback, verifiedNoPlayback, playbackExpectedButUnscoreable, unknown+ }++ enum SpeechActivity: Equatable, Sendable {+ /// Activity alone may be leaked playback. It cannot establish near-end speech.+ case detected, notDetected+ }++ enum EmbeddingProvenance: Equatable, Sendable {+ case unavailable, sharedClusterCentroid, independentInterval+ }++ struct TemporalDuplicate: Equatable, Sendable {+ enum State: Equatable, Sendable {+ case insufficientEvidence, observing, duplicateCandidate, duplicateSupported+ }+ let state: State+ let identity: Identity+ let interval: Interval+ let supportingWindows: Int+ /// Convention: positive means microphone follows reference. Not a calibrated threshold.+ let microphoneDelaySeconds: Measurement<Double>+ let lagSpreadSeconds: Measurement<Double>+ let coverage: Fraction+ }++ let identity: Identity+ let interval: Interval+ let playback: PlaybackContext+ let signalVerdict: Measurement<TurnEchoVerdict>+ let textEcho: Measurement<Bool>+ let speechActivity: Measurement<SpeechActivity>+ let speechCoverage: Measurement<Fraction>+ /// Missing and measured zero are distinct. This field is not a speech/no-speech gate.+ let rms: Measurement<Double>+ let temporalDuplicate: Measurement<TemporalDuplicate>+ let embedding: EmbeddingProvenance++ /// Compatibility projection only; detailed unknown reasons survive in the evidence itself.+ var legacySignalVerdict: TurnEchoVerdict {+ guard case .measured(let verdict) = signalVerdict else { return .unknown }+ return verdict+ }+}++/// Stage A seam; the only implementation is a test fake. Stage B owns real paired-audio analysis.+nonisolated protocol MeetingPlaybackDuplicateEvidenceSource: Sendable {+ func evidence(for identity: MeetingMicrophoneEvidence.Identity,+ interval: MeetingMicrophoneEvidence.Interval) -> MeetingMicrophoneEvidence.Measurement<MeetingMicrophoneEvidence.TemporalDuplicate>+}++/// Test-only caller contract: no runtime enablement flag or production invocation in Stage A.+/// Conservative proposals, NOT calibrated admission. No acceptedSpeech/noSupportedSpeech can+/// be inferred from the Stage A activity/echo inputs alone. It cannot hide text or admit profiles.+nonisolated enum MeetingMicrophoneShadowPolicy {+ enum ProposedOutcome: Equatable, Sendable {+ case uncertainCandidate+ }+ enum Reason: Equatable, Sendable {+ case legacyRescueDisagreement, playbackDuplicateCandidate, speechActivityIsNotNearEndProof+ case negativeActivityIsNotSpeechAbsenceProof, insufficientEvidence+ case staleTemporalEvidence, invalidTemporalEvidence, missingPlaybackContext+ }+ struct Evaluation: Equatable, Sendable {+ let outcome: ProposedOutcome = .uncertainCandidate+ let experimental = true+ let reason: Reason+ let identity: MeetingMicrophoneEvidence.Identity+ let interval: MeetingMicrophoneEvidence.Interval+ /// Exposes provenance gaps instead of collapsing them into a generic unknown.+ let signalUnknownReason: MeetingMicrophoneEvidence.UnknownReason?+ }++ static func evaluate(_ evidence: MeetingMicrophoneEvidence) -> Evaluation {+ func result(_ reason: Reason) -> Evaluation {+ let unknown: MeetingMicrophoneEvidence.UnknownReason?+ if case .unavailable(let why) = evidence.signalVerdict { unknown = why } else { unknown = nil }+ return Evaluation(reason: reason, identity: evidence.identity, interval: evidence.interval,+ signalUnknownReason: unknown)+ }+ if case .measured(let temporal) = evidence.temporalDuplicate {+ guard temporal.identity == evidence.identity,+ temporal.interval == evidence.interval else { return result(.staleTemporalEvidence) }+ guard temporal.supportingWindows >= 0 else { return result(.invalidTemporalEvidence) }+ if case .measured(let lag) = temporal.microphoneDelaySeconds, !lag.isFinite {+ return result(.invalidTemporalEvidence)+ }+ if case .measured(let spread) = temporal.lagSpreadSeconds, !spread.isFinite || spread < 0 {+ return result(.invalidTemporalEvidence)+ }+ if temporal.state == .duplicateSupported {+ guard temporal.supportingWindows > 0, temporal.coverage.value > 0,+ case .measured = temporal.microphoneDelaySeconds,+ case .measured = temporal.lagSpreadSeconds else { return result(.invalidTemporalEvidence) }+ if evidence.playback == .unknown { return result(.missingPlaybackContext) }+ guard evidence.playback == .scoreablePlayback else { return result(.invalidTemporalEvidence) }+ }+ }+ // Explicitly distinguish the intentional future removal of the historical rescue.+ // An uncertain shadow proposal is never applied as a hidden-text decision in Stage A.+ if evidence.signalVerdict == .measured(.residualNotExplained), evidence.textEcho == .measured(true) {+ return result(.legacyRescueDisagreement)+ }+ if evidence.signalVerdict == .measured(.echo) || evidence.textEcho == .measured(true) {+ return result(.playbackDuplicateCandidate)+ }+ if case .measured(let temporal) = evidence.temporalDuplicate, temporal.state == .duplicateSupported {+ return result(.playbackDuplicateCandidate)+ }+ switch evidence.speechActivity {+ case .measured(.detected): return result(.speechActivityIsNotNearEndProof)+ case .measured(.notDetected): return result(.negativeActivityIsNotSpeechAbsenceProof)+ case .unavailable: return result(.insufficientEvidence)+ }+ }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingPlaybackDuplicateDetector.swift b/Sources/Fluid/Services/Meeting/MeetingPlaybackDuplicateDetector.swiftnew file mode 100644index 00000000..edccce2f--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingPlaybackDuplicateDetector.swift@@ -0,0 +1,201 @@+import Foundation++/// Offline, experimental sidecar evidence only. No capture, transcript or profile consumers.+/// Input is mono PCM explicitly resampled by the caller to 2 kHz (including anti-aliasing).+/// Invalid samples include decoder padding, missing channels and ±2 s discontinuity guards.+/// Correlation is not a speech detector and cannot rule out quiet double talk.+nonisolated struct MeetingPlaybackDuplicateDetector {+ static let version = "temporal-shadow-b1-v1"+ static let sampleRate = 2_000.0++ struct Configuration: Codable, Sendable {+ // Exploratory engineering constants, not calibrated admission thresholds.+ var minimumCorrelation = 0.25+ var minimumControlMargin = 0.08+ var minimumPeakMargin = 0.025+ var minimumRMS = 0.00001+ var stableLagSeconds = 0.012+ var supportWindows = 3+ var budgetSeconds = 0.5+ var maximumConsecutiveBudgetFailures = 3+ }++ struct PCM: Sendable {+ let start: Double+ let samples: [Float]+ let valid: [Bool]+ }++ struct Window: Sendable {+ let sessionID: UUID+ let epoch: UInt64+ let routeID: String+ let start: Double+ let microphone: PCM+ /// Must include ±0.5 s of reference halo around the two-second mic interval.+ let reference: PCM?+ /// Independent time-shifted window, aligned to this interval solely as a null control.+ let mismatchedReference: PCM?+ }++ enum State: String, Codable, Sendable {+ case insufficientEvidence, observing, duplicateCandidate, duplicateSupported+ }+ enum Reason: String, Codable, Sendable {+ case referenceUnavailable, controlUnavailable, invalidInput, unscoredCoverage, insufficientEnergy+ case ambiguousPeak, scoredInconclusive, accumulating, stableSupport, released+ case overBudget, detectorUnavailable+ }+ struct Result: Codable, Sendable {+ let start: Double+ let end: Double+ let epoch: UInt64+ let state: State+ let reason: Reason+ let supportingWindows: Int+ let correlation: Double?+ let controlCorrelation: Double?+ /// Positive lag means microphone follows reference: mic(t) matches ref(t-lag).+ let microphoneDelaySeconds: Double?+ let lagSpreadSeconds: Double?+ let elapsedSeconds: Double+ }++ private let configuration: Configuration+ private let clock: @Sendable () -> Double+ private var scope: String?+ private var sessionID: UUID?+ private var lastEnd: Double?+ private var lags: [Double] = []+ private var budgetFailures = 0+ private var disabled = false++ init(configuration: Configuration = .init(),+ clock: @escaping @Sendable () -> Double = { ProcessInfo.processInfo.systemUptime }) {+ self.configuration = configuration+ self.clock = clock+ }++ /// Bounded work: exactly 4,000 mic samples, 1,001 lags, two references. No growing PCM/history.+ /// No release hangover in v1: failed support immediately removes supported evidence. This+ /// conservative zero-hold choice must be measured before introducing a nonzero hold period.+ mutating func process(_ window: Window) -> Result {+ let began = clock()+ // Failure latch is attempt-local; a new recording gets a fresh budget allowance.+ if sessionID != window.sessionID { budgetFailures = 0; disabled = false }+ sessionID = window.sessionID+ let newScope = "\(window.sessionID):\(window.epoch):\(window.routeID)"+ if scope != newScope || lastEnd.map({ abs($0 - window.start) > 0.00025 }) != false {+ lags.removeAll()+ }+ scope = newScope+ lastEnd = window.start + 2+ func result(_ reason: Reason, _ state: State = .insufficientEvidence,+ _ correlation: Double? = nil, _ control: Double? = nil,+ _ delay: Double? = nil) -> Result {+ Result(start: window.start.isFinite ? window.start : 0,+ end: window.start.isFinite ? window.start + 2 : 0,+ epoch: window.epoch, state: state, reason: reason,+ supportingWindows: lags.count, correlation: correlation,+ controlCorrelation: control, microphoneDelaySeconds: delay,+ lagSpreadSeconds: lags.isEmpty ? nil : lags.max()! - lags.min()!,+ elapsedSeconds: max(0, clock() - began))+ }+ if disabled { lags.removeAll(); return result(.detectorUnavailable) }+ let c = configuration+ guard window.start.isFinite, window.start >= 0, !window.routeID.isEmpty,+ c.minimumCorrelation.isFinite, (0...1).contains(c.minimumCorrelation),+ c.minimumControlMargin.isFinite, (0...1).contains(c.minimumControlMargin),+ c.minimumPeakMargin.isFinite, (0...1).contains(c.minimumPeakMargin),+ c.minimumRMS.isFinite, c.minimumRMS > 0,+ c.stableLagSeconds.isFinite, (0...0.5).contains(c.stableLagSeconds),+ (2...16).contains(c.supportWindows), c.budgetSeconds.isFinite, c.budgetSeconds > 0,+ (1...10).contains(c.maximumConsecutiveBudgetFailures) else {+ lags.removeAll(); return result(.invalidInput)+ }+ guard let reference = window.reference else {+ lags.removeAll(); budgetFailures = 0; return result(.referenceUnavailable)+ }+ guard let control = window.mismatchedReference else {+ lags.removeAll(); budgetFailures = 0; return result(.controlUnavailable)+ }+ // Validate sizes before allocating aligned arrays. ±0.5s search needs a 3-second halo.+ func aligned(_ pcm: PCM, halo: Int) -> [Double]? {+ guard pcm.start.isFinite, pcm.samples.count == pcm.valid.count,+ pcm.samples.count <= 16_000 else { return nil }+ let offset = (window.start - pcm.start) * Self.sampleRate - Double(halo)+ guard offset.isFinite, offset >= 0, offset <= Double(pcm.samples.count),+ abs(offset - offset.rounded()) < 0.001 else { return nil }+ let first = Int(offset.rounded()), count = 4_000 + 2 * halo+ guard first <= pcm.samples.count - count else { return nil }+ let range = first..<(first + count)+ guard range.allSatisfy({ pcm.valid[$0] && pcm.samples[$0].isFinite }) else { return nil }+ return range.map { Double(pcm.samples[$0]) }+ }+ guard let mic = aligned(window.microphone, halo: 0),+ let ref = aligned(reference, halo: 1_000),+ let null = aligned(control, halo: 1_000) else {+ lags.removeAll(); budgetFailures = 0; return result(.unscoredCoverage)+ }+ let mean = mic.reduce(0, +) / 4_000+ let centered = mic.map { $0 - mean }+ let energy = centered.reduce(0) { $0 + $1 * $1 }+ func usableEnergy(_ samples: [Double]) -> Bool {+ let average = samples.reduce(0, +) / Double(samples.count)+ return samples.reduce(0) { $0 + ($1 - average) * ($1 - average) }+ / Double(samples.count) >= c.minimumRMS * c.minimumRMS+ }+ guard energy / 4_000 >= c.minimumRMS * c.minimumRMS,+ usableEnergy(ref), usableEnergy(null) else {+ lags.removeAll(); budgetFailures = 0; return result(.insufficientEnergy)+ }+ // Search at 1 ms resolution. Peak ambiguity excludes a ±12ms neighborhood.+ func scores(_ samples: [Double]) -> [(lag: Int, score: Double)]? {+ var output: [(Int, Double)] = []+ for lag in stride(from: -1_000, through: 1_000, by: 2) {+ if clock() - began > c.budgetSeconds { return nil }+ let first = 1_000 - lag+ var sum = 0.0, squared = 0.0, cross = 0.0+ for i in 0..<4_000 {+ let value = samples[first + i]+ sum += value; squared += value * value; cross += centered[i] * value+ }+ let refEnergy = max(0, squared - sum * sum / 4_000)+ let score = refEnergy / 4_000 >= c.minimumRMS * c.minimumRMS+ ? min(1, abs(cross) / sqrt(energy * refEnergy)) : 0+ output.append((lag, score))+ }+ return output+ }+ guard let matches = scores(ref), let controls = scores(null) else {+ lags.removeAll(); budgetFailures += 1+ disabled = budgetFailures >= c.maximumConsecutiveBudgetFailures+ return result(.overBudget)+ }+ budgetFailures = 0+ let peak = matches.max { $0.score < $1.score }!+ let nullPeak = controls.map(\.score).max()!+ let secondary = matches.filter { abs($0.lag - peak.lag) > 24 }.map(\.score).max() ?? 0+ let lag = Double(peak.lag) / Self.sampleRate+ let wasSupported = lags.count >= c.supportWindows+ guard peak.score >= c.minimumCorrelation, peak.score - nullPeak >= c.minimumControlMargin else {+ lags.removeAll()+ return result(wasSupported ? .released : .scoredInconclusive, .observing, peak.score, nullPeak, lag)+ }+ guard peak.score - secondary >= c.minimumPeakMargin, abs(peak.lag) < 1_000 else {+ lags.removeAll(); return result(.ambiguousPeak, .observing, peak.score, nullPeak, lag)+ }+ // Compare quantized sample offsets so an exact 12ms spread cannot become+ // 0.012000000000000004 and spuriously reset support.+ if let low = lags.min(), let high = lags.max(),+ Int(((max(high, lag) - min(low, lag)) * Self.sampleRate).rounded())+ > Int((c.stableLagSeconds * Self.sampleRate).rounded(.down)) {+ lags.removeAll()+ }+ lags.append(lag)+ if lags.count > c.supportWindows { lags.removeFirst() }+ return result(lags.count >= c.supportWindows ? .stableSupport : .accumulating,+ lags.count >= c.supportWindows ? .duplicateSupported : .duplicateCandidate,+ peak.score, nullPeak, lag)+ }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingProcessingConfiguration.swift b/Sources/Fluid/Services/Meeting/MeetingProcessingConfiguration.swiftnew file mode 100644index 00000000..129bafa2--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingProcessingConfiguration.swift@@ -0,0 +1,116 @@+import Foundation++/// Versioned, length-framed key/value encoding so arbitrary string fields cannot collide on+/// delimiters (`asr=a@b,lang=c` vs `asr=a,lang=b@c`). Values stay human-readable; no hashing.+/// Strings are canonicalized to precomposed form because Swift `==` treats canonically+/// equivalent Unicode as equal while raw UTF8 lengths differ.+nonisolated enum MeetingConfigFingerprintEncoding {+ static let version = 1++ static func encode(_ pairs: [(key: String, value: String)]) -> String {+ let framed = pairs.map { pair in+ let key = pair.key.precomposedStringWithCanonicalMapping+ let value = pair.value.precomposedStringWithCanonicalMapping+ return "\(key.utf8.count):\(key)=\(value.utf8.count):\(value)"+ }+ return (["v\(Self.version)"] + framed).joined(separator: ";")+ }+}++/// Inert value snapshot of meeting post-processing ASR configuration. No runtime path reads this+/// yet; P1 wiring will build it once per run so pipeline actors stop reading `SettingsStore.shared`+/// mid-flight.+nonisolated struct MeetingFinalProcessingConfiguration: Equatable, Sendable {+ static let defaultASRModel = "parakeet-tdt-v2"+ static let defaultLanguageCode = "en"+ /// Mirrors `MeetingProcessingPipeline.pipelineVersion`; that class is `@MainActor`, so a+ /// MainActor test guards this literal against drift instead of referencing it here.+ static let defaultPipelineVersion = 10++ let asrModel: String+ let languageCode: String+ let vocabularyBoostingEnabled: Bool+ let pronunciationMatchingEnabled: Bool+ let customDictionaryRewritingEnabled: Bool+ let experimentalUnifiedFinalEnabled: Bool+ let diarizationFingerprint: String+ let pipelineVersion: Int++ init(+ asrModel: String = Self.defaultASRModel,+ languageCode: String = Self.defaultLanguageCode,+ vocabularyBoostingEnabled: Bool = false,+ pronunciationMatchingEnabled: Bool = false,+ customDictionaryRewritingEnabled: Bool = false,+ experimentalUnifiedFinalEnabled: Bool = false,+ diarizationFingerprint: String = MeetingProcessingCheckpoint.currentDiarizationFingerprint,+ pipelineVersion: Int = Self.defaultPipelineVersion+ ) {+ self.asrModel = asrModel+ self.languageCode = languageCode+ self.vocabularyBoostingEnabled = vocabularyBoostingEnabled+ self.pronunciationMatchingEnabled = pronunciationMatchingEnabled+ self.customDictionaryRewritingEnabled = customDictionaryRewritingEnabled+ self.experimentalUnifiedFinalEnabled = experimentalUnifiedFinalEnabled+ self.diarizationFingerprint = diarizationFingerprint+ self.pipelineVersion = pipelineVersion+ }++ /// Identity of this inert configuration snapshot only. It does not by itself prove the runtime+ /// enforced these options; resumability may rely on it only once processing fully applies them.+ var identityFingerprint: String {+ MeetingConfigFingerprintEncoding.encode([+ ("asr", self.asrModel),+ ("lang", self.languageCode),+ ("vocabBoost", String(self.vocabularyBoostingEnabled)),+ ("pronunciationMatch", String(self.pronunciationMatchingEnabled)),+ ("dictionaryRewrite", String(self.customDictionaryRewritingEnabled)),+ ("unifiedFinal", String(self.experimentalUnifiedFinalEnabled)),+ ("diarizer", self.diarizationFingerprint),+ ("pipeline", String(self.pipelineVersion)),+ ])+ }+}++/// Inert value snapshot of live-caption ASR configuration. Live captions do no diarization.+nonisolated struct MeetingLiveCaptionsConfiguration: Equatable, Sendable {+ static let defaultModelRepositoryID = "FluidInference/parakeet-realtime-eou-120m-coreml"+ static let defaultModelVariant = "160ms"+ /// Branch selected by the current loader; source pins no resolved HF commit, so this is the+ /// loader default, not a revision pin.+ static let defaultModelRevision = "main"+ static let defaultChunkSizeMilliseconds = 160+ static let defaultLanguageCode = "en"++ let modelRepositoryID: String+ let modelVariant: String+ let modelRevision: String+ let chunkSizeMilliseconds: Int+ let languageCode: String++ init(+ modelRepositoryID: String = Self.defaultModelRepositoryID,+ modelVariant: String = Self.defaultModelVariant,+ modelRevision: String = Self.defaultModelRevision,+ chunkSizeMilliseconds: Int = Self.defaultChunkSizeMilliseconds,+ languageCode: String = Self.defaultLanguageCode+ ) {+ self.modelRepositoryID = modelRepositoryID+ self.modelVariant = modelVariant+ self.modelRevision = modelRevision+ self.chunkSizeMilliseconds = chunkSizeMilliseconds+ self.languageCode = languageCode+ }++ var identityFingerprint: String {+ MeetingConfigFingerprintEncoding.encode([+ ("modelRepo", self.modelRepositoryID),+ ("modelVariant", self.modelVariant),+ ("revision", self.modelRevision),+ ("revisionState", "unresolved"),+ ("chunkMs", String(self.chunkSizeMilliseconds)),+ ("lang", self.languageCode),+ ("diarizer", "none"),+ ])+ }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift b/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swiftindex 7d80406f..ca2d3c03 100644--- a/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift+++ b/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift@@ -913,13 +913,15 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {/// What the UI and exporter hide. Text and signal each get to say "echo", because they fail/// independently: ASR garbles bleed differently from the far end's own transcript, defeating/// text matching, while the signal reads it plainly (measured median 0.91 explained). Either- /// one is then vetoed by evidence of genuine local speech in the same turn.+ /// one is then vetoed by a low-explanation run under the historical compatibility rule.+ /// That run does not prove genuine local speech. Stage A preserves this behavior;+ /// the separately calibrated shadow policy must not influence it.////// `isLikelyEcho` stays text-only and remains the sole input to the local-speaker election —/// signal evidence must never reach it, or a misclassification could elect the far-end/// cluster as "You" through the 1.75x winner rule.var effectiveEcho: Bool {- (self.isLikelyEcho || self.signalVerdict == .echo) && self.signalVerdict != .containsLocalSpeech+ self.signalVerdict.legacyEffectiveEcho(textEcho: self.isLikelyEcho)}}@@ -1084,7 +1086,7 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {var longestLowRun = 0var currentRun = 0for value in scores.fractions {- if !value.isNaN, value < MeetingEchoSignalScorer.localSpeechFractionThreshold {+ if !value.isNaN, value < MeetingEchoSignalScorer.lowExplanationFractionThreshold {currentRun += 1longestLowRun = max(longestLowRun, currentRun)} else if !value.isNaN {@@ -2237,7 +2239,7 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {entry.seconds += max(0, turn.end - turn.start)breakdown[turn.signalVerdict] = entry}- let verdictDescription = [TurnEchoVerdict.echo, .containsLocalSpeech, .unknown].map { verdict -> String in+ let verdictDescription = [TurnEchoVerdict.echo, .residualNotExplained, .unknown].map { verdict -> String inlet entry = breakdown[verdict] ?? (0, 0)return String(format: "%@=%d/%.1fs", String(describing: verdict), entry.count, entry.seconds)}.joined(separator: " ")diff --git a/Sources/Fluid/Services/Meeting/MeetingReferenceAttributionContracts.swift b/Sources/Fluid/Services/Meeting/MeetingReferenceAttributionContracts.swiftnew file mode 100644index 00000000..4f862c63--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingReferenceAttributionContracts.swift@@ -0,0 +1,1183 @@+import CryptoKit+import Foundation++/// Versioned, offline-only contracts for reference-aware microphone attribution.+///+/// These types deliberately contain no transcript text, audio samples, embeddings, labels,+/// window titles, or paths. They are a value seam for candidate engines; defining the seam does+/// not enable an engine or change capture, ASR, export, or speaker-profile behaviour.+nonisolated enum MeetingReferenceAttributionSchema {+ static let protocolVersion = 1+ static let sidecarVersion = 1+}++nonisolated enum MeetingReferenceAttributionOutcome: String, Codable, CaseIterable, Hashable, Sendable {+ case likelyPlaybackOnly+ case acceptedNearEndSpeech+ case mixedOrUncertain+ case unscored+ case scopeLimited+}++/// A typed reason is intentionally more specific than an outcome. In particular, missing data+/// is never represented by `silence`, `noSpeech`, or a playback-only outcome.+nonisolated enum MeetingReferenceAttributionReasonCode: String, Codable, CaseIterable, Hashable, Sendable {+ case referenceAbsent+ case referenceGap+ case referenceScopeLimited+ case referenceCompletenessUnobservable+ case captureGap+ case captureTimingSynthesized+ case delayUnresolved+ case clockDriftUnstable+ case engineUnavailable+ case invalidInput+ case nonFiniteInput+ case unsupportedFormat+ case lowExcitation+ case insufficientCoverage+ case unconverged+ case pathReset+ case resetRequired+ case overBudget+ case cancelled+ case dependencyUnavailable+ case corruptSidecar+ case staleEpoch+ case duplicateFrame+ case nonMonotonicFrame+ case mixedEvidence+ case residualSpeechDetected+ case playbackAttributed+ case silenceMeasured+ case noSpeechMeasured+ case unknownSpeechState+ case noFallback+ case fallbackToOriginal+}++nonisolated enum MeetingReferenceAttributionSpeechState: String, Codable, CaseIterable, Hashable, Sendable {+ case unknown+ case silence+ case noSpeech+ case nearEndSpeech+ case playbackSpeech+ case mixedSpeech+}++nonisolated enum MeetingReferenceAttributionResidualVariant: String, Codable, CaseIterable, Hashable, Sendable {+ case originalMicrophone+ case linearResidual+ case suppressedResidual+}++nonisolated enum MeetingReferenceAttributionReferenceScope: String, Codable, CaseIterable, Hashable, Sendable {+ case selectedWindow+ case selectedApplication+ case authorizedSystemMix+ case unknown+}++nonisolated enum MeetingReferenceAttributionFallback: String, Codable, CaseIterable, Hashable, Sendable {+ case none+ case originalMicrophone+}++nonisolated private enum MeetingReferenceAttributionCanonical {+ // Malformed programmatic values remain visibly invalid rather than becoming plausible evidence.+ static let fallbackLogicalIdentifier = "invalid/"+ static let fallbackOpaqueHash = "invalid"+ static let fallbackConfigurationHash = "invalid"++ static func hash<T: Encodable>(_ value: T) -> String {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]+ // A hash of empty data would look valid while silently erasing provenance when encoding+ // fails (for example, because a corrupt payload contains a non-finite number). Keep an+ // explicit sentinel; sidecar decoding separately rejects corrupt payloads.+ guard let data = try? encoder.encode(value) else { return "invalid" }+ return SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()+ }++ static func normalized(_ value: String) -> String {+ value.precomposedStringWithCanonicalMapping+ }++ static func isLogicalIdentifier(_ value: String) -> Bool {+ let value = normalized(value)+ guard !value.isEmpty, value != ".", value != "..",+ !value.hasPrefix("~"), !value.contains("/"), !value.contains("\\"),+ !value.contains("://") else { return false }+ return value.unicodeScalars.allSatisfy { scalar in+ !CharacterSet.controlCharacters.contains(scalar)+ && !CharacterSet.whitespacesAndNewlines.contains(scalar)+ }+ }++ static func logicalIdentifier(_ value: String) -> String {+ let normalized = normalized(value)+ return isLogicalIdentifier(normalized) ? normalized : Self.fallbackLogicalIdentifier+ }++ static func isOpaqueHash(_ value: String) -> Bool {+ let value = normalized(value)+ guard !value.isEmpty, value != "invalid", !value.contains("/"), !value.contains("\\") else { return false }+ return value.unicodeScalars.allSatisfy { scalar in+ !CharacterSet.controlCharacters.contains(scalar)+ && !CharacterSet.whitespacesAndNewlines.contains(scalar)+ }+ }++ static func opaqueHash(_ value: String) -> String {+ let normalized = normalized(value)+ return isOpaqueHash(normalized) ? normalized : Self.fallbackOpaqueHash+ }++ static func isConfigurationHash(_ value: String) -> Bool {+ let value = normalized(value)+ guard value.count == 64 else { return false }+ return value.unicodeScalars.allSatisfy { scalar in+ (scalar.value >= 48 && scalar.value <= 57)+ || (scalar.value >= 97 && scalar.value <= 102)+ }+ }++ static func configurationHash(_ value: String) -> String {+ let normalized = normalized(value)+ return isConfigurationHash(normalized) ? normalized : Self.fallbackConfigurationHash+ }++ static func string(_ value: String) -> String {+ let normalized = value.precomposedStringWithCanonicalMapping+ // Logical identities may not smuggle local paths or URLs into a sidecar.+ guard isLogicalIdentifier(normalized) else { return Self.fallbackLogicalIdentifier }+ return normalized+ }++ static func finite(_ value: Double?) -> Double? {+ guard let value, value.isFinite else { return nil }+ return value+ }++ static func nonNegative(_ value: Double?) -> Double? {+ guard let value = finite(value), value >= 0 else { return nil }+ return value+ }++ static func fraction(_ value: Double?) -> Double? {+ guard let value = finite(value), (0...1).contains(value) else { return nil }+ return value+ }++ static func nonNegativeInt(_ value: Int) -> Int { max(0, value) }+}++/// A named source revision/artifact digest. `name` is a logical identifier, never a file path.+nonisolated struct MeetingReferenceAttributionSourceHash: Codable, Equatable, Sendable {+ let name: String+ let hash: String++ init(name: String, hash: String) {+ self.name = MeetingReferenceAttributionCanonical.logicalIdentifier(name)+ self.hash = MeetingReferenceAttributionCanonical.opaqueHash(hash)+ }++ private enum CodingKeys: String, CodingKey { case name, hash }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let name = try container.decode(String.self, forKey: .name)+ let hash = try container.decode(String.self, forKey: .hash)+ guard MeetingReferenceAttributionCanonical.isLogicalIdentifier(name),+ MeetingReferenceAttributionCanonical.isOpaqueHash(hash) else {+ throw DecodingError.dataCorruptedError(forKey: .name, in: container,+ debugDescription: "Invalid source hash identity")+ }+ self.name = MeetingReferenceAttributionCanonical.normalized(name)+ self.hash = MeetingReferenceAttributionCanonical.normalized(hash)+ }+}++/// Explicit engine provenance. Hashes are opaque strings so this contract can represent a git+/// revision, source digest, static artifact digest, or a dependency lock digest without guessing+/// its spelling. Callers must provide logical names, not absolute paths.+nonisolated struct MeetingReferenceAttributionEngineIdentity: Codable, Equatable, Sendable {+ let schemaVersion: Int+ let engineID: String+ let engineVersion: String+ let sourceHashes: [MeetingReferenceAttributionSourceHash]+ let buildHash: String?+ let configurationHash: String++ init(+ engineID: String,+ engineVersion: String,+ sourceHashes: [MeetingReferenceAttributionSourceHash] = [],+ buildHash: String? = nil,+ configurationHash: String+ ) {+ self.schemaVersion = MeetingReferenceAttributionSchema.protocolVersion+ self.engineID = MeetingReferenceAttributionCanonical.logicalIdentifier(engineID)+ self.engineVersion = MeetingReferenceAttributionCanonical.logicalIdentifier(engineVersion)+ self.sourceHashes = sourceHashes.sorted {+ $0.name == $1.name ? $0.hash < $1.hash : $0.name < $1.name+ }+ self.buildHash = buildHash.map(MeetingReferenceAttributionCanonical.opaqueHash)+ self.configurationHash = MeetingReferenceAttributionCanonical.configurationHash(configurationHash)+ }++ init(+ engineID: String,+ engineVersion: String,+ sourceHashes: [String: String],+ buildHash: String? = nil,+ configurationHash: String+ ) {+ self.init(engineID: engineID, engineVersion: engineVersion,+ sourceHashes: sourceHashes.map { .init(name: $0.key, hash: $0.value) },+ buildHash: buildHash, configurationHash: configurationHash)+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, engineID, engineVersion, sourceHashes, buildHash, configurationHash+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ let engineID = try container.decode(String.self, forKey: .engineID)+ let engineVersion = try container.decode(String.self, forKey: .engineVersion)+ let sourceHashes = try container.decode([MeetingReferenceAttributionSourceHash].self, forKey: .sourceHashes)+ let buildHash = try container.decodeIfPresent(String.self, forKey: .buildHash)+ let configurationHash = try container.decode(String.self, forKey: .configurationHash)+ guard schemaVersion == MeetingReferenceAttributionSchema.protocolVersion,+ MeetingReferenceAttributionCanonical.isLogicalIdentifier(engineID),+ MeetingReferenceAttributionCanonical.isLogicalIdentifier(engineVersion),+ sourceHashes.map(\.name).count == Set(sourceHashes.map(\.name)).count,+ buildHash == nil || MeetingReferenceAttributionCanonical.isOpaqueHash(buildHash!),+ MeetingReferenceAttributionCanonical.isConfigurationHash(configurationHash) else {+ throw DecodingError.dataCorruptedError(forKey: .schemaVersion, in: container,+ debugDescription: "Invalid engine provenance")+ }+ self.schemaVersion = schemaVersion+ self.engineID = MeetingReferenceAttributionCanonical.normalized(engineID)+ self.engineVersion = MeetingReferenceAttributionCanonical.normalized(engineVersion)+ self.sourceHashes = sourceHashes.sorted { $0.name == $1.name ? $0.hash < $1.hash : $0.name < $1.name }+ self.buildHash = buildHash.map(MeetingReferenceAttributionCanonical.normalized)+ self.configurationHash = MeetingReferenceAttributionCanonical.normalized(configurationHash)+ }++ init(+ engineID: String,+ engineVersion: String,+ sourceHashes: [MeetingReferenceAttributionSourceHash] = [],+ buildHash: String? = nil,+ configuration: MeetingReferenceAttributionConfiguration+ ) {+ self.init(engineID: engineID, engineVersion: engineVersion,+ sourceHashes: sourceHashes, buildHash: buildHash,+ configurationHash: configuration.configurationHash)+ }++ /// Stable across source-hash insertion order and JSON encoder formatting.+ var stableIdentity: String { MeetingReferenceAttributionCanonical.hash(self) }+ var identityHash: String { stableIdentity }+}++/// Configuration for an offline fixed-frame engine. Invalid floating-point values are omitted+/// as `nil`, preserving unknown rather than turning bad input into measured silence.+nonisolated struct MeetingReferenceAttributionConfiguration: Codable, Equatable, Sendable {+ let schemaVersion: Int+ let sampleRateHz: Int+ let frameDurationMilliseconds: Int+ let hopDurationMilliseconds: Int+ let maximumProcessingMillisecondsPerFrame: Double?+ let maximumMemoryBytes: Int?+ let candidateID: String++ init(+ sampleRateHz: Int = 16_000,+ frameDurationMilliseconds: Int = 10,+ hopDurationMilliseconds: Int = 10,+ maximumProcessingMillisecondsPerFrame: Double? = 100,+ maximumMemoryBytes: Int? = nil,+ candidateID: String = "unselected"+ ) {+ self.schemaVersion = MeetingReferenceAttributionSchema.protocolVersion+ self.sampleRateHz = sampleRateHz > 0 ? sampleRateHz : 16_000+ self.frameDurationMilliseconds = frameDurationMilliseconds > 0 ? frameDurationMilliseconds : 10+ self.hopDurationMilliseconds = hopDurationMilliseconds > 0 ? hopDurationMilliseconds : 10+ self.maximumProcessingMillisecondsPerFrame = MeetingReferenceAttributionCanonical.nonNegative(maximumProcessingMillisecondsPerFrame)+ self.maximumMemoryBytes = maximumMemoryBytes.flatMap { $0 >= 0 ? $0 : nil }+ self.candidateID = MeetingReferenceAttributionCanonical.string(candidateID)+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, sampleRateHz, frameDurationMilliseconds, hopDurationMilliseconds,+ maximumProcessingMillisecondsPerFrame, maximumMemoryBytes, candidateID+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ let sampleRateHz = try container.decode(Int.self, forKey: .sampleRateHz)+ let frameDuration = try container.decode(Int.self, forKey: .frameDurationMilliseconds)+ let hopDuration = try container.decode(Int.self, forKey: .hopDurationMilliseconds)+ let maximumProcessing = try container.decodeIfPresent(Double.self, forKey: .maximumProcessingMillisecondsPerFrame)+ let maximumMemory = try container.decodeIfPresent(Int.self, forKey: .maximumMemoryBytes)+ let candidateID = try container.decode(String.self, forKey: .candidateID)+ guard schemaVersion == MeetingReferenceAttributionSchema.protocolVersion,+ sampleRateHz > 0, frameDuration > 0, hopDuration > 0,+ maximumProcessing == nil || (maximumProcessing!.isFinite && maximumProcessing! >= 0),+ maximumMemory == nil || maximumMemory! >= 0,+ MeetingReferenceAttributionCanonical.isLogicalIdentifier(candidateID) else {+ throw DecodingError.dataCorruptedError(forKey: .schemaVersion, in: container,+ debugDescription: "Invalid attribution configuration")+ }+ self.init(sampleRateHz: sampleRateHz, frameDurationMilliseconds: frameDuration,+ hopDurationMilliseconds: hopDuration,+ maximumProcessingMillisecondsPerFrame: maximumProcessing,+ maximumMemoryBytes: maximumMemory, candidateID: candidateID)+ }++ var stableIdentity: String { MeetingReferenceAttributionCanonical.hash(self) }+ var configurationHash: String { stableIdentity }+}++/// A frame input is intentionally not Codable: it may carry PCM to an offline implementation, but+/// PCM never crosses into a Codable result or sidecar. Every frame is fixed-width by the caller.+nonisolated struct MeetingReferenceAttributionPCMFrame: Sendable {+ let samples: [Float]+ let valid: [Bool]++ init(samples: [Float], valid: [Bool]) {+ self.samples = samples+ self.valid = valid+ }++ var hasValidShape: Bool {+ samples.count == valid.count && !samples.isEmpty+ }++ var containsOnlyFiniteSamples: Bool { samples.allSatisfy(\.isFinite) }+}++nonisolated struct MeetingReferenceAttributionFrame: Sendable {+ let frameIndex: Int+ let epoch: UInt64+ let startSeconds: Double+ let durationSeconds: Double+ let microphone: MeetingReferenceAttributionPCMFrame+ let reference: MeetingReferenceAttributionPCMFrame?+ let referenceScope: MeetingReferenceAttributionReferenceScope++ init(+ frameIndex: Int,+ epoch: UInt64,+ startSeconds: Double,+ durationSeconds: Double,+ microphone: MeetingReferenceAttributionPCMFrame,+ reference: MeetingReferenceAttributionPCMFrame?,+ referenceScope: MeetingReferenceAttributionReferenceScope = .unknown+ ) {+ self.frameIndex = frameIndex+ self.epoch = epoch+ self.startSeconds = startSeconds+ self.durationSeconds = durationSeconds+ self.microphone = microphone+ self.reference = reference+ self.referenceScope = referenceScope+ }++ var endSeconds: Double { startSeconds + durationSeconds }+ var hasValidTiming: Bool {+ startSeconds.isFinite && startSeconds >= 0 && durationSeconds.isFinite && durationSeconds > 0+ && endSeconds.isFinite+ }++ /// Validation is explicit because PCM frames are intentionally not Codable. A caller may still+ /// construct an invalid frame to produce a typed fail-open result, but an engine must not+ /// process it as measured audio.+ var validationReasons: [MeetingReferenceAttributionReasonCode] {+ var reasons = [MeetingReferenceAttributionReasonCode]()+ if frameIndex < 0 || !hasValidTiming { reasons.append(.invalidInput) }+ if !microphone.hasValidShape || !microphone.containsOnlyFiniteSamples {+ reasons.append(.invalidInput)+ if !microphone.containsOnlyFiniteSamples { reasons.append(.nonFiniteInput) }+ }+ if let reference {+ if !reference.hasValidShape || !reference.containsOnlyFiniteSamples {+ reasons.append(.invalidInput)+ if !reference.containsOnlyFiniteSamples { reasons.append(.nonFiniteInput) }+ }+ }+ return Array(Set(reasons)).sorted { $0.rawValue < $1.rawValue }+ }++ var isValid: Bool { validationReasons.isEmpty }++ func validate() throws {+ guard isValid else {+ throw MeetingReferenceAttributionFrameValidationError(reasons: validationReasons)+ }+ }+}++nonisolated struct MeetingReferenceAttributionFrameValidationError: Error, Equatable, Sendable {+ let reasons: [MeetingReferenceAttributionReasonCode]+}++nonisolated struct MeetingReferenceAttributionEpoch: Codable, Equatable, Sendable {+ let epoch: UInt64+ let resetReason: MeetingReferenceAttributionReasonCode?++ init(epoch: UInt64, resetReason: MeetingReferenceAttributionReasonCode? = nil) {+ self.epoch = epoch+ self.resetReason = resetReason+ }++ private enum CodingKeys: String, CodingKey { case epoch, resetReason }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ self.epoch = try container.decode(UInt64.self, forKey: .epoch)+ self.resetReason = try container.decodeIfPresent(MeetingReferenceAttributionReasonCode.self, forKey: .resetReason)+ }+}++/// Numeric candidate/quality metrics. Nil means unavailable/unknown; zero is a real measured+/// value. This distinction is important for coverage accounting and fail-open behavior.+nonisolated struct MeetingReferenceAttributionMetrics: Codable, Equatable, Sendable {+ let delaySeconds: Double?+ let delayJitterSeconds: Double?+ let convergence: Double?+ let resetCount: Int+ let erlDB: Double?+ let erleDB: Double?+ let candidateScore: Double?+ let controlMargin: Double?+ let originalEnergy: Double?+ let residualEnergy: Double?+ let residualToOriginalEnergyRatio: Double?+ let processingMilliseconds: Double?+ let peakMemoryBytes: Int?++ init(+ delaySeconds: Double? = nil,+ delayJitterSeconds: Double? = nil,+ convergence: Double? = nil,+ resetCount: Int = 0,+ erlDB: Double? = nil,+ erleDB: Double? = nil,+ candidateScore: Double? = nil,+ controlMargin: Double? = nil,+ originalEnergy: Double? = nil,+ residualEnergy: Double? = nil,+ residualToOriginalEnergyRatio: Double? = nil,+ processingMilliseconds: Double? = nil,+ peakMemoryBytes: Int? = nil+ ) {+ self.delaySeconds = MeetingReferenceAttributionCanonical.finite(delaySeconds)+ self.delayJitterSeconds = MeetingReferenceAttributionCanonical.nonNegative(delayJitterSeconds)+ self.convergence = MeetingReferenceAttributionCanonical.fraction(convergence)+ self.resetCount = MeetingReferenceAttributionCanonical.nonNegativeInt(resetCount)+ self.erlDB = MeetingReferenceAttributionCanonical.finite(erlDB)+ self.erleDB = MeetingReferenceAttributionCanonical.finite(erleDB)+ self.candidateScore = MeetingReferenceAttributionCanonical.fraction(candidateScore)+ self.controlMargin = MeetingReferenceAttributionCanonical.finite(controlMargin)+ self.originalEnergy = MeetingReferenceAttributionCanonical.nonNegative(originalEnergy)+ self.residualEnergy = MeetingReferenceAttributionCanonical.nonNegative(residualEnergy)+ self.residualToOriginalEnergyRatio = MeetingReferenceAttributionCanonical.nonNegative(residualToOriginalEnergyRatio)+ self.processingMilliseconds = MeetingReferenceAttributionCanonical.nonNegative(processingMilliseconds)+ self.peakMemoryBytes = peakMemoryBytes.flatMap { $0 >= 0 ? $0 : nil }+ }++ private enum CodingKeys: String, CodingKey {+ case delaySeconds, delayJitterSeconds, convergence, resetCount, erlDB, erleDB,+ candidateScore, controlMargin, originalEnergy, residualEnergy,+ residualToOriginalEnergyRatio, processingMilliseconds, peakMemoryBytes+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let delay = try container.decodeIfPresent(Double.self, forKey: .delaySeconds)+ let delayJitter = try container.decodeIfPresent(Double.self, forKey: .delayJitterSeconds)+ let convergence = try container.decodeIfPresent(Double.self, forKey: .convergence)+ let resetCount = try container.decode(Int.self, forKey: .resetCount)+ let erl = try container.decodeIfPresent(Double.self, forKey: .erlDB)+ let erle = try container.decodeIfPresent(Double.self, forKey: .erleDB)+ let candidateScore = try container.decodeIfPresent(Double.self, forKey: .candidateScore)+ let controlMargin = try container.decodeIfPresent(Double.self, forKey: .controlMargin)+ let originalEnergy = try container.decodeIfPresent(Double.self, forKey: .originalEnergy)+ let residualEnergy = try container.decodeIfPresent(Double.self, forKey: .residualEnergy)+ let ratio = try container.decodeIfPresent(Double.self, forKey: .residualToOriginalEnergyRatio)+ let processing = try container.decodeIfPresent(Double.self, forKey: .processingMilliseconds)+ let peakMemory = try container.decodeIfPresent(Int.self, forKey: .peakMemoryBytes)+ let finite = [delay, delayJitter, convergence, erl, erle, candidateScore, controlMargin,+ originalEnergy, residualEnergy, ratio, processing].compactMap { $0 }+ guard resetCount >= 0, finite.allSatisfy(\.isFinite),+ delayJitter == nil || delayJitter! >= 0,+ convergence == nil || (0...1).contains(convergence!),+ candidateScore == nil || (0...1).contains(candidateScore!),+ originalEnergy == nil || originalEnergy! >= 0,+ residualEnergy == nil || residualEnergy! >= 0,+ ratio == nil || ratio! >= 0,+ processing == nil || processing! >= 0,+ peakMemory == nil || peakMemory! >= 0 else {+ throw DecodingError.dataCorruptedError(forKey: .resetCount, in: container,+ debugDescription: "Invalid attribution metric")+ }+ self.init(delaySeconds: delay, delayJitterSeconds: delayJitter, convergence: convergence,+ resetCount: resetCount, erlDB: erl, erleDB: erle, candidateScore: candidateScore,+ controlMargin: controlMargin, originalEnergy: originalEnergy,+ residualEnergy: residualEnergy, residualToOriginalEnergyRatio: ratio,+ processingMilliseconds: processing, peakMemoryBytes: peakMemory)+ }++ static let unavailable = Self()++ var residualEnergyRatio: Double? { residualToOriginalEnergyRatio }+ var echoReturnLossDB: Double? { erlDB }+ var echoReturnLossEnhancementDB: Double? { erleDB }+}++nonisolated struct MeetingReferenceAttributionFrameResult: Codable, Equatable, Sendable {+ let schemaVersion: Int+ let frameIndex: Int+ let epoch: UInt64+ let startSeconds: Double+ let durationSeconds: Double+ let outcome: MeetingReferenceAttributionOutcome+ let speechState: MeetingReferenceAttributionSpeechState+ let residualVariant: MeetingReferenceAttributionResidualVariant?+ let reasons: [MeetingReferenceAttributionReasonCode]+ let metrics: MeetingReferenceAttributionMetrics+ let fallback: MeetingReferenceAttributionFallback++ init(+ frameIndex: Int,+ epoch: UInt64,+ startSeconds: Double,+ durationSeconds: Double,+ outcome: MeetingReferenceAttributionOutcome,+ speechState: MeetingReferenceAttributionSpeechState = .unknown,+ residualVariant: MeetingReferenceAttributionResidualVariant? = nil,+ reasons: [MeetingReferenceAttributionReasonCode] = [],+ metrics: MeetingReferenceAttributionMetrics = .unavailable,+ fallback: MeetingReferenceAttributionFallback = .originalMicrophone+ ) {+ var finalOutcome = outcome+ var finalReasons = Set(reasons)+ if speechState == .unknown,+ outcome == .likelyPlaybackOnly || outcome == .acceptedNearEndSpeech {+ finalOutcome = .unscored+ finalReasons.insert(.unknownSpeechState)+ }+ // Playback-only is an exclusion decision. Conflicting positive near-end or mixed+ // speech evidence must degrade to an explicitly uncertain result rather than remain+ // eligible for suppression or profile exclusion as playback-only.+ if outcome == .likelyPlaybackOnly,+ speechState == .nearEndSpeech || speechState == .mixedSpeech {+ finalOutcome = .mixedOrUncertain+ finalReasons.insert(.mixedEvidence)+ }+ let validTiming = frameIndex >= 0 && startSeconds.isFinite && startSeconds >= 0+ && durationSeconds.isFinite && durationSeconds > 0+ && (startSeconds + durationSeconds).isFinite+ if outcome == .acceptedNearEndSpeech && speechState != .nearEndSpeech {+ finalOutcome = .unscored+ finalReasons.insert(.invalidInput)+ }+ if !validTiming {+ finalOutcome = .unscored+ finalReasons.insert(.invalidInput)+ }+ if outcome == .scopeLimited { finalReasons.insert(.referenceScopeLimited) }+ if finalOutcome == .unscored {+ finalReasons.insert(.fallbackToOriginal)+ } else {+ finalReasons.remove(.fallbackToOriginal)+ finalReasons.remove(.unknownSpeechState)+ }+ self.schemaVersion = MeetingReferenceAttributionSchema.sidecarVersion+ self.frameIndex = max(0, frameIndex)+ self.epoch = epoch+ // Invalid timing is represented by one canonical, decodable fail-open shape. In+ // particular, finite-but-overflowing start + duration must not retain a huge duration.+ self.startSeconds = validTiming+ ? startSeconds+ : (startSeconds.isFinite && startSeconds >= 0 ? startSeconds : 0)+ self.durationSeconds = validTiming ? durationSeconds : 0+ self.outcome = finalOutcome+ self.speechState = finalOutcome == .unscored ? .unknown : speechState+ self.residualVariant = finalOutcome == .unscored ? nil : residualVariant+ self.reasons = Array(finalReasons).sorted { $0.rawValue < $1.rawValue }+ self.metrics = metrics+ // A scored result cannot silently carry the legacy/original fallback marker. Normalize a+ // caller that omitted the argument to `.none`; unscored results always remain recoverable.+ self.fallback = finalOutcome == .unscored+ ? .originalMicrophone+ : (fallback == .originalMicrophone ? .none : fallback)+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, frameIndex, epoch, startSeconds, durationSeconds, outcome,+ speechState, residualVariant, reasons, metrics, fallback+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ let frameIndex = try container.decode(Int.self, forKey: .frameIndex)+ let epoch = try container.decode(UInt64.self, forKey: .epoch)+ let startSeconds = try container.decode(Double.self, forKey: .startSeconds)+ let durationSeconds = try container.decode(Double.self, forKey: .durationSeconds)+ let outcome = try container.decode(MeetingReferenceAttributionOutcome.self, forKey: .outcome)+ let speechState = try container.decode(MeetingReferenceAttributionSpeechState.self, forKey: .speechState)+ let residualVariant = try container.decodeIfPresent(MeetingReferenceAttributionResidualVariant.self, forKey: .residualVariant)+ let reasons = try container.decode([MeetingReferenceAttributionReasonCode].self, forKey: .reasons)+ let metrics = try container.decode(MeetingReferenceAttributionMetrics.self, forKey: .metrics)+ let fallback = try container.decode(MeetingReferenceAttributionFallback.self, forKey: .fallback)++ let standardTiming = frameIndex >= 0 && startSeconds.isFinite && startSeconds >= 0+ && durationSeconds.isFinite && durationSeconds > 0+ && (startSeconds + durationSeconds).isFinite+ let invalidFailOpenTiming = outcome == .unscored && frameIndex >= 0+ && startSeconds.isFinite && startSeconds >= 0 && durationSeconds == 0+ && reasons.contains(.invalidInput)+ guard schemaVersion == MeetingReferenceAttributionSchema.sidecarVersion,+ standardTiming || invalidFailOpenTiming else {+ throw DecodingError.dataCorruptedError(forKey: .schemaVersion, in: container,+ debugDescription: "Invalid frame schema or timing")+ }+ guard Set(reasons).count == reasons.count else {+ throw DecodingError.dataCorruptedError(forKey: .reasons, in: container,+ debugDescription: "Duplicate frame reason")+ }+ guard reasons == reasons.sorted(by: { $0.rawValue < $1.rawValue }) else {+ throw DecodingError.dataCorruptedError(forKey: .reasons, in: container,+ debugDescription: "Non-canonical frame reason ordering")+ }+ let knownSpeechState = speechState != .unknown+ guard (outcome != .likelyPlaybackOnly && outcome != .acceptedNearEndSpeech) || knownSpeechState else {+ throw DecodingError.dataCorruptedError(forKey: .speechState, in: container,+ debugDescription: "Scored outcome requires known speech state")+ }+ if outcome == .acceptedNearEndSpeech && speechState != .nearEndSpeech {+ throw DecodingError.dataCorruptedError(forKey: .speechState, in: container,+ debugDescription: "Accepted speech outcome requires near-end state")+ }+ if outcome == .likelyPlaybackOnly+ && (speechState == .nearEndSpeech || speechState == .mixedSpeech) {+ throw DecodingError.dataCorruptedError(forKey: .speechState, in: container,+ debugDescription: "Playback-only outcome conflicts with speech evidence")+ }+ guard outcome == .unscored || !reasons.contains(.unknownSpeechState) else {+ throw DecodingError.dataCorruptedError(forKey: .reasons, in: container,+ debugDescription: "Scored frame cannot carry unknown speech reason")+ }+ if outcome == .scopeLimited && !reasons.contains(.referenceScopeLimited) {+ throw DecodingError.dataCorruptedError(forKey: .reasons, in: container,+ debugDescription: "Scope-limited outcome requires scope reason")+ }+ if outcome == .unscored {+ guard speechState == .unknown else {+ throw DecodingError.dataCorruptedError(forKey: .speechState, in: container,+ debugDescription: "Unscored frame must have unknown speech state")+ }+ guard fallback == .originalMicrophone, reasons.contains(.fallbackToOriginal) else {+ throw DecodingError.dataCorruptedError(forKey: .fallback, in: container,+ debugDescription: "Unscored frame must fall back to original microphone")+ }+ guard residualVariant == nil else {+ throw DecodingError.dataCorruptedError(forKey: .residualVariant, in: container,+ debugDescription: "Unscored frame cannot expose residual variant")+ }+ } else if fallback == .originalMicrophone {+ throw DecodingError.dataCorruptedError(forKey: .fallback, in: container,+ debugDescription: "Scored frame cannot use original fallback")+ }+ self.schemaVersion = schemaVersion+ self.frameIndex = frameIndex+ self.epoch = epoch+ self.startSeconds = startSeconds+ self.durationSeconds = durationSeconds+ self.outcome = outcome+ self.speechState = speechState+ self.residualVariant = residualVariant+ self.reasons = reasons+ self.metrics = metrics+ self.fallback = fallback+ }++ static func failOpen(+ frame: MeetingReferenceAttributionFrame,+ reason: MeetingReferenceAttributionReasonCode+ ) -> Self {+ Self(frameIndex: frame.frameIndex, epoch: frame.epoch,+ startSeconds: frame.startSeconds, durationSeconds: frame.durationSeconds,+ outcome: .unscored, speechState: .unknown, residualVariant: nil,+ reasons: [reason, .unknownSpeechState, .fallbackToOriginal],+ metrics: .unavailable, fallback: .originalMicrophone)+ }++ var endSeconds: Double { startSeconds + durationSeconds }+ var isKnownSpeechState: Bool { speechState != .unknown }+}++nonisolated struct MeetingReferenceAttributionWouldChangeCounts: Codable, Equatable, Sendable {+ let words: Int+ let segments: Int+ let profileObservations: Int+ let unknownIntervals: Int+ let legacyDisagreements: Int++ init(words: Int = 0, segments: Int = 0, profileObservations: Int = 0,+ unknownIntervals: Int = 0, legacyDisagreements: Int = 0) {+ self.words = MeetingReferenceAttributionCanonical.nonNegativeInt(words)+ self.segments = MeetingReferenceAttributionCanonical.nonNegativeInt(segments)+ self.profileObservations = MeetingReferenceAttributionCanonical.nonNegativeInt(profileObservations)+ self.unknownIntervals = MeetingReferenceAttributionCanonical.nonNegativeInt(unknownIntervals)+ self.legacyDisagreements = MeetingReferenceAttributionCanonical.nonNegativeInt(legacyDisagreements)+ }++ static let zero = Self()++ private enum CodingKeys: String, CodingKey {+ case words, segments, profileObservations, unknownIntervals, legacyDisagreements+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let values = try [+ container.decode(Int.self, forKey: .words),+ container.decode(Int.self, forKey: .segments),+ container.decode(Int.self, forKey: .profileObservations),+ container.decode(Int.self, forKey: .unknownIntervals),+ container.decode(Int.self, forKey: .legacyDisagreements)+ ]+ guard values.allSatisfy({ $0 >= 0 }) else {+ throw DecodingError.dataCorruptedError(forKey: .words, in: container,+ debugDescription: "Negative would-change count")+ }+ self.init(words: values[0], segments: values[1], profileObservations: values[2],+ unknownIntervals: values[3], legacyDisagreements: values[4])+ }+}++/// Duration-weighted coverage. Counts are informational; percentages are derived from durations+/// so a 10 ms frame and a 30 ms frame cannot accidentally receive equal weight.+nonisolated struct MeetingReferenceAttributionCoverage: Codable, Equatable, Sendable {+ let totalDurationSeconds: Double+ let scoredDurationSeconds: Double+ let likelyPlaybackOnlyDurationSeconds: Double+ let acceptedNearEndSpeechDurationSeconds: Double+ let mixedOrUncertainDurationSeconds: Double+ let scopeLimitedDurationSeconds: Double+ let unscoredDurationSeconds: Double+ let frameCount: Int++ private init(uncheckedZero: Void) {+ self.totalDurationSeconds = 0+ self.scoredDurationSeconds = 0+ self.likelyPlaybackOnlyDurationSeconds = 0+ self.acceptedNearEndSpeechDurationSeconds = 0+ self.mixedOrUncertainDurationSeconds = 0+ self.scopeLimitedDurationSeconds = 0+ self.unscoredDurationSeconds = 0+ self.frameCount = 0+ }++ static let zero = Self(uncheckedZero: ())++ init?(+ totalDurationSeconds: Double,+ scoredDurationSeconds: Double,+ likelyPlaybackOnlyDurationSeconds: Double,+ acceptedNearEndSpeechDurationSeconds: Double,+ mixedOrUncertainDurationSeconds: Double,+ scopeLimitedDurationSeconds: Double,+ unscoredDurationSeconds: Double,+ frameCount: Int+ ) {+ let values = [totalDurationSeconds, scoredDurationSeconds,+ likelyPlaybackOnlyDurationSeconds, acceptedNearEndSpeechDurationSeconds,+ mixedOrUncertainDurationSeconds, scopeLimitedDurationSeconds,+ unscoredDurationSeconds]+ guard values.allSatisfy({ $0.isFinite && $0 >= 0 }), frameCount >= 0,+ abs(scoredDurationSeconds - (likelyPlaybackOnlyDurationSeconds+ + acceptedNearEndSpeechDurationSeconds + mixedOrUncertainDurationSeconds)) <= 1e-9,+ scoredDurationSeconds + unscoredDurationSeconds <= totalDurationSeconds + 1e-9,+ abs((likelyPlaybackOnlyDurationSeconds + acceptedNearEndSpeechDurationSeconds+ + mixedOrUncertainDurationSeconds + scopeLimitedDurationSeconds+ + unscoredDurationSeconds) - totalDurationSeconds) <= 1e-9 else { return nil }+ self.totalDurationSeconds = totalDurationSeconds+ self.scoredDurationSeconds = scoredDurationSeconds+ self.likelyPlaybackOnlyDurationSeconds = likelyPlaybackOnlyDurationSeconds+ self.acceptedNearEndSpeechDurationSeconds = acceptedNearEndSpeechDurationSeconds+ self.mixedOrUncertainDurationSeconds = mixedOrUncertainDurationSeconds+ self.scopeLimitedDurationSeconds = scopeLimitedDurationSeconds+ self.unscoredDurationSeconds = unscoredDurationSeconds+ self.frameCount = frameCount+ }++ var scoredFraction: Double { totalDurationSeconds > 0 ? scoredDurationSeconds / totalDurationSeconds : 0 }+ var unscoredFraction: Double { totalDurationSeconds > 0 ? unscoredDurationSeconds / totalDurationSeconds : 0 }++ private enum CodingKeys: String, CodingKey {+ case totalDurationSeconds, scoredDurationSeconds, likelyPlaybackOnlyDurationSeconds,+ acceptedNearEndSpeechDurationSeconds, mixedOrUncertainDurationSeconds,+ scopeLimitedDurationSeconds, unscoredDurationSeconds, frameCount+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let total = try container.decode(Double.self, forKey: .totalDurationSeconds)+ let scored = try container.decode(Double.self, forKey: .scoredDurationSeconds)+ let playback = try container.decode(Double.self, forKey: .likelyPlaybackOnlyDurationSeconds)+ let nearEnd = try container.decode(Double.self, forKey: .acceptedNearEndSpeechDurationSeconds)+ let mixed = try container.decode(Double.self, forKey: .mixedOrUncertainDurationSeconds)+ let scope = try container.decode(Double.self, forKey: .scopeLimitedDurationSeconds)+ let unscored = try container.decode(Double.self, forKey: .unscoredDurationSeconds)+ let frameCount = try container.decode(Int.self, forKey: .frameCount)+ guard let value = Self(totalDurationSeconds: total, scoredDurationSeconds: scored,+ likelyPlaybackOnlyDurationSeconds: playback,+ acceptedNearEndSpeechDurationSeconds: nearEnd,+ mixedOrUncertainDurationSeconds: mixed,+ scopeLimitedDurationSeconds: scope,+ unscoredDurationSeconds: unscored, frameCount: frameCount) else {+ throw DecodingError.dataCorruptedError(forKey: .totalDurationSeconds, in: container,+ debugDescription: "Invalid attribution coverage")+ }+ self = value+ }+}++nonisolated struct MeetingReferenceAttributionResourceCost: Codable, Equatable, Sendable {+ let processingMilliseconds: Double?+ let peakMemoryBytes: Int?+ let derivedSidecarBytes: Int?+ let overBudget: Bool+ let fallback: MeetingReferenceAttributionFallback++ init(processingMilliseconds: Double? = nil, peakMemoryBytes: Int? = nil,+ derivedSidecarBytes: Int? = nil, overBudget: Bool = false,+ fallback: MeetingReferenceAttributionFallback = .none) {+ self.processingMilliseconds = MeetingReferenceAttributionCanonical.nonNegative(processingMilliseconds)+ self.peakMemoryBytes = peakMemoryBytes.flatMap { $0 >= 0 ? $0 : nil }+ self.derivedSidecarBytes = derivedSidecarBytes.flatMap { $0 >= 0 ? $0 : nil }+ self.overBudget = overBudget+ self.fallback = overBudget ? .originalMicrophone : fallback+ }++ private enum CodingKeys: String, CodingKey {+ case processingMilliseconds, peakMemoryBytes, derivedSidecarBytes, overBudget, fallback+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let processing = try container.decodeIfPresent(Double.self, forKey: .processingMilliseconds)+ let memory = try container.decodeIfPresent(Int.self, forKey: .peakMemoryBytes)+ let sidecar = try container.decodeIfPresent(Int.self, forKey: .derivedSidecarBytes)+ let overBudget = try container.decode(Bool.self, forKey: .overBudget)+ let fallback = try container.decode(MeetingReferenceAttributionFallback.self, forKey: .fallback)+ guard processing == nil || (processing!.isFinite && processing! >= 0),+ memory == nil || memory! >= 0,+ sidecar == nil || sidecar! >= 0,+ !overBudget || fallback == .originalMicrophone else {+ throw DecodingError.dataCorruptedError(forKey: .overBudget, in: container,+ debugDescription: "Invalid attribution resource cost")+ }+ self.init(processingMilliseconds: processing, peakMemoryBytes: memory,+ derivedSidecarBytes: sidecar, overBudget: overBudget, fallback: fallback)+ }+}++nonisolated private struct MeetingReferenceAttributionOutcomeCountEntry: Codable {+ let outcome: MeetingReferenceAttributionOutcome+ let count: Int+}++nonisolated private struct MeetingReferenceAttributionReasonCountEntry: Codable {+ let reason: MeetingReferenceAttributionReasonCode+ let count: Int+}++/// Aggregate result constructed from fixed-frame results. Invalid/nonfinite frame timing is+/// excluded from measured coverage and represented in `reasonCounts`, never as silence.+nonisolated struct MeetingReferenceAttributionAggregate: Codable, Equatable, Sendable {+ let schemaVersion: Int+ let coverage: MeetingReferenceAttributionCoverage+ let outcomeCounts: [MeetingReferenceAttributionOutcome: Int]+ let reasonCounts: [MeetingReferenceAttributionReasonCode: Int]+ let wouldChange: MeetingReferenceAttributionWouldChangeCounts+ let metrics: MeetingReferenceAttributionMetrics+ let resourceCost: MeetingReferenceAttributionResourceCost++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, coverage, outcomeCounts, reasonCounts, wouldChange, metrics, resourceCost+ }++ init(+ frameResults: [MeetingReferenceAttributionFrameResult],+ wouldChange: MeetingReferenceAttributionWouldChangeCounts = .zero,+ metrics: MeetingReferenceAttributionMetrics = .unavailable,+ resourceCost: MeetingReferenceAttributionResourceCost = .init()+ ) {+ var durations = [MeetingReferenceAttributionOutcome: Double](+ uniqueKeysWithValues: MeetingReferenceAttributionOutcome.allCases.map { ($0, 0) })+ var outcomeCounts = [MeetingReferenceAttributionOutcome: Int](+ uniqueKeysWithValues: MeetingReferenceAttributionOutcome.allCases.map { ($0, 0) })+ var reasonCounts = [MeetingReferenceAttributionReasonCode: Int]()+ var total = 0.0+ var seenFrames = Set<String>()+ var validFrameCount = 0+ for result in frameResults {+ let frameKey = "\(result.epoch):\(result.frameIndex)"+ guard seenFrames.insert(frameKey).inserted else {+ reasonCounts[.duplicateFrame, default: 0] += 1+ continue+ }+ let duration = result.durationSeconds.isFinite && result.durationSeconds > 0 ? result.durationSeconds : 0+ guard duration > 0 else {+ reasonCounts[.invalidInput, default: 0] += 1+ continue+ }+ let nextTotal = total + duration+ let bucket = durations[result.outcome, default: 0]+ let nextBucket = bucket + duration+ guard nextTotal.isFinite, nextBucket.isFinite else {+ reasonCounts[.invalidInput, default: 0] += 1+ continue+ }+ validFrameCount += 1+ total = nextTotal+ durations[result.outcome] = nextBucket+ outcomeCounts[result.outcome, default: 0] += 1+ for reason in result.reasons { reasonCounts[reason, default: 0] += 1 }+ }+ let scored = durations[.likelyPlaybackOnly, default: 0]+ + durations[.acceptedNearEndSpeech, default: 0]+ + durations[.mixedOrUncertain, default: 0]+ let coverage = MeetingReferenceAttributionCoverage(+ totalDurationSeconds: total,+ scoredDurationSeconds: scored,+ likelyPlaybackOnlyDurationSeconds: durations[.likelyPlaybackOnly, default: 0],+ acceptedNearEndSpeechDurationSeconds: durations[.acceptedNearEndSpeech, default: 0],+ mixedOrUncertainDurationSeconds: durations[.mixedOrUncertain, default: 0],+ scopeLimitedDurationSeconds: durations[.scopeLimited, default: 0],+ unscoredDurationSeconds: durations[.unscored, default: 0],+ frameCount: validFrameCount+ ) ?? .zero+ self.schemaVersion = MeetingReferenceAttributionSchema.sidecarVersion+ self.coverage = coverage+ self.outcomeCounts = outcomeCounts+ self.reasonCounts = reasonCounts+ self.wouldChange = wouldChange+ self.metrics = metrics+ self.resourceCost = resourceCost+ }++ /// Count dictionaries use sorted arrays rather than Dictionary's alternating-key encoding.+ /// This makes the sidecar representation explicit and stable across insertion order.+ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let version = try container.decode(Int.self, forKey: .schemaVersion)+ guard version == MeetingReferenceAttributionSchema.sidecarVersion else {+ throw DecodingError.dataCorruptedError(forKey: .schemaVersion, in: container,+ debugDescription: "Unsupported attribution aggregate schema")+ }+ let outcomes = try container.decode([MeetingReferenceAttributionOutcomeCountEntry].self,+ forKey: .outcomeCounts)+ let reasons = try container.decode([MeetingReferenceAttributionReasonCountEntry].self,+ forKey: .reasonCounts)+ var outcomeCounts = [MeetingReferenceAttributionOutcome: Int]()+ for entry in outcomes {+ guard entry.count >= 0, outcomeCounts.updateValue(entry.count, forKey: entry.outcome) == nil else {+ throw DecodingError.dataCorruptedError(forKey: .outcomeCounts, in: container,+ debugDescription: "Duplicate or negative outcome count")+ }+ }+ var reasonCounts = [MeetingReferenceAttributionReasonCode: Int]()+ for entry in reasons {+ guard entry.count >= 0, reasonCounts.updateValue(entry.count, forKey: entry.reason) == nil else {+ throw DecodingError.dataCorruptedError(forKey: .reasonCounts, in: container,+ debugDescription: "Duplicate or negative reason count")+ }+ }+ self.schemaVersion = version+ self.coverage = try container.decode(MeetingReferenceAttributionCoverage.self, forKey: .coverage)+ self.outcomeCounts = outcomeCounts+ self.reasonCounts = reasonCounts+ self.wouldChange = try container.decode(MeetingReferenceAttributionWouldChangeCounts.self, forKey: .wouldChange)+ self.metrics = try container.decode(MeetingReferenceAttributionMetrics.self, forKey: .metrics)+ self.resourceCost = try container.decode(MeetingReferenceAttributionResourceCost.self, forKey: .resourceCost)+ }++ nonisolated func encode(to encoder: Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(schemaVersion, forKey: .schemaVersion)+ try container.encode(coverage, forKey: .coverage)+ let outcomes = outcomeCounts.keys.sorted { $0.rawValue < $1.rawValue }.map {+ MeetingReferenceAttributionOutcomeCountEntry(outcome: $0, count: outcomeCounts[$0] ?? 0)+ }+ let reasons = reasonCounts.keys.sorted { $0.rawValue < $1.rawValue }.map {+ MeetingReferenceAttributionReasonCountEntry(reason: $0, count: reasonCounts[$0] ?? 0)+ }+ try container.encode(outcomes, forKey: .outcomeCounts)+ try container.encode(reasons, forKey: .reasonCounts)+ try container.encode(wouldChange, forKey: .wouldChange)+ try container.encode(metrics, forKey: .metrics)+ try container.encode(resourceCost, forKey: .resourceCost)+ }+}++/// Numeric-only, deterministic sidecar. `sidecarHash` hashes the complete Codable payload and is+/// therefore stable for the same engine/configuration/results, independent of dictionary order.+nonisolated struct MeetingReferenceAttributionSidecar: Codable, Equatable, Sendable {+ let schemaVersion: Int+ let engineIdentity: MeetingReferenceAttributionEngineIdentity+ let configuration: MeetingReferenceAttributionConfiguration+ let epochs: [MeetingReferenceAttributionEpoch]+ let frameResults: [MeetingReferenceAttributionFrameResult]+ let aggregate: MeetingReferenceAttributionAggregate++ nonisolated enum ValidationError: Error, Equatable, Sendable {+ case invalid(String)+ }++ init(+ engineIdentity: MeetingReferenceAttributionEngineIdentity,+ configuration: MeetingReferenceAttributionConfiguration,+ epochs: [MeetingReferenceAttributionEpoch] = [],+ frameResults: [MeetingReferenceAttributionFrameResult],+ aggregate: MeetingReferenceAttributionAggregate? = nil+ ) {+ self.schemaVersion = MeetingReferenceAttributionSchema.sidecarVersion+ self.engineIdentity = engineIdentity+ self.configuration = configuration+ let sortedFrames = frameResults.sorted {+ $0.epoch == $1.epoch ? $0.frameIndex < $1.frameIndex : $0.epoch < $1.epoch+ }+ self.frameResults = sortedFrames+ let frameEpochs = Set(sortedFrames.map(\.epoch)).sorted()+ // Preserve caller-supplied epoch evidence verbatim. Duplicate or incomplete epochs are+ // invalid evidence, but remain representable for validation instead of trapping or being+ // silently repaired. An omitted list gets the convenience defaults for valid construction.+ self.epochs = epochs.isEmpty+ ? frameEpochs.map { .init(epoch: $0) }+ : epochs.sorted { $0.epoch < $1.epoch }+ self.aggregate = aggregate ?? MeetingReferenceAttributionAggregate(frameResults: sortedFrames)+ }++ var sidecarHash: String { MeetingReferenceAttributionCanonical.hash(self) }++ /// Structural validation is exposed for programmatic construction; decoding throws the same+ /// errors so a corrupt sidecar cannot be treated as measured evidence.+ var validationError: ValidationError? {+ let resultEpochs = Set(frameResults.map(\.epoch))+ let epochValues = epochs.map(\.epoch)+ let canonicalFrames = frameResults.sorted {+ $0.epoch == $1.epoch ? $0.frameIndex < $1.frameIndex : $0.epoch < $1.epoch+ }+ guard frameResults.map({ "\($0.epoch):\($0.frameIndex)" })+ == canonicalFrames.map({ "\($0.epoch):\($0.frameIndex)" }) else {+ return .invalid("non-canonical frame ordering")+ }+ var frameKeys = Set<String>()+ for frame in frameResults {+ guard frameKeys.insert("\(frame.epoch):\(frame.frameIndex)").inserted else {+ return .invalid("duplicate epoch/frame key")+ }+ }+ guard epochValues == epochValues.sorted() else {+ return .invalid("non-canonical epoch ordering")+ }+ guard Set(epochValues).count == epochValues.count else { return .invalid("duplicate epoch") }+ guard Set(epochValues) == resultEpochs else { return .invalid("incomplete epoch/frame keys") }+ // Epochs are reset markers, not permission to move backwards in the session timeline.+ // Validate the canonical global order so a later epoch cannot overlap an earlier one.+ guard zip(frameResults, frameResults.dropFirst()).allSatisfy({ previous, next in+ next.startSeconds + 1e-9 >= previous.endSeconds+ }) else { return .invalid("non-monotonic frame timing") }+ guard MeetingReferenceAttributionCanonical.isLogicalIdentifier(engineIdentity.engineID),+ MeetingReferenceAttributionCanonical.isLogicalIdentifier(engineIdentity.engineVersion),+ engineIdentity.sourceHashes.allSatisfy({+ MeetingReferenceAttributionCanonical.isLogicalIdentifier($0.name)+ && MeetingReferenceAttributionCanonical.isOpaqueHash($0.hash)+ }),+ Set(engineIdentity.sourceHashes.map(\.name)).count == engineIdentity.sourceHashes.count,+ engineIdentity.buildHash == nil+ || MeetingReferenceAttributionCanonical.isOpaqueHash(engineIdentity.buildHash!),+ MeetingReferenceAttributionCanonical.isLogicalIdentifier(configuration.candidateID),+ configuration.sampleRateHz > 0,+ configuration.frameDurationMilliseconds > 0,+ configuration.hopDurationMilliseconds > 0,+ configuration.maximumProcessingMillisecondsPerFrame == nil+ || (configuration.maximumProcessingMillisecondsPerFrame!.isFinite+ && configuration.maximumProcessingMillisecondsPerFrame! >= 0),+ configuration.maximumMemoryBytes == nil || configuration.maximumMemoryBytes! >= 0,+ engineIdentity.configurationHash == configuration.configurationHash else {+ return .invalid("engine/configuration hash mismatch")+ }+ guard MeetingReferenceAttributionCanonical.isConfigurationHash(engineIdentity.configurationHash),+ MeetingReferenceAttributionCanonical.isConfigurationHash(configuration.configurationHash) else {+ return .invalid("invalid engine/configuration provenance hash")+ }+ let expected = MeetingReferenceAttributionAggregate(+ frameResults: frameResults,+ wouldChange: aggregate.wouldChange,+ metrics: aggregate.metrics,+ resourceCost: aggregate.resourceCost+ )+ guard expected == aggregate else { return .invalid("aggregate mismatch") }+ return nil+ }++ var isValid: Bool { validationError == nil }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, engineIdentity, configuration, epochs, frameResults, aggregate+ }++ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ let schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ let engineIdentity = try container.decode(MeetingReferenceAttributionEngineIdentity.self, forKey: .engineIdentity)+ let configuration = try container.decode(MeetingReferenceAttributionConfiguration.self, forKey: .configuration)+ let epochs = try container.decode([MeetingReferenceAttributionEpoch].self, forKey: .epochs)+ let frameResults = try container.decode([MeetingReferenceAttributionFrameResult].self, forKey: .frameResults)+ let aggregate = try container.decode(MeetingReferenceAttributionAggregate.self, forKey: .aggregate)+ guard schemaVersion == MeetingReferenceAttributionSchema.sidecarVersion else {+ throw DecodingError.dataCorruptedError(forKey: .schemaVersion, in: container,+ debugDescription: "Unsupported attribution sidecar schema")+ }+ self.schemaVersion = schemaVersion+ self.engineIdentity = engineIdentity+ self.configuration = configuration+ self.epochs = epochs+ self.frameResults = frameResults+ self.aggregate = aggregate+ if let validationError {+ throw DecodingError.dataCorruptedError(forKey: .aggregate, in: container,+ debugDescription: "Invalid attribution sidecar: \(validationError)")+ }+ }+}++/// Common offline seam. Implementations receive one fixed frame at a time and return metadata+/// only. No implementation is installed or called by this contract file.+nonisolated protocol MeetingReferenceAttributionEngine: Sendable {+ var identity: MeetingReferenceAttributionEngineIdentity { get }+ var configuration: MeetingReferenceAttributionConfiguration { get }++ mutating func reset(to epoch: MeetingReferenceAttributionEpoch)+ mutating func process(_ frame: MeetingReferenceAttributionFrame) -> MeetingReferenceAttributionFrameResult+ mutating func finish() -> MeetingReferenceAttributionSidecar+}++nonisolated extension MeetingReferenceAttributionEngine {+ mutating func process(frame: MeetingReferenceAttributionFrame) -> MeetingReferenceAttributionFrameResult {+ process(frame)+ }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingReferenceSynchronizer.swift b/Sources/Fluid/Services/Meeting/MeetingReferenceSynchronizer.swiftnew file mode 100644index 00000000..3a222368--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingReferenceSynchronizer.swift@@ -0,0 +1,1056 @@+import Foundation++/// The source clocks intentionally remain distinct until the synchronizer derives a+/// session timeline. This type is offline-only: it accepts completed in-memory PCM+/// fixtures and performs no callback, file, or engine work.+nonisolated public enum MeetingSynchronizerTimeline: String, Sendable {+ case microphoneSource+ case referencePTS+ case sessionMonotonic+}++nonisolated public enum MeetingReferenceScope: String, Sendable {+ case selectedWindow+ case selectedApplication+ case authorizedFullMix+}++nonisolated public enum MeetingReferenceCompleteness: String, Sendable {+ case measuredComplete+ case unobservable+}++/// These are the only reasons by which a synchronizer result is unknown. In+/// particular, an absent/masked sample is never represented as measured silence.+nonisolated public enum MeetingSynchronizerUnknownReason: String, CaseIterable, Hashable, Sendable {+ case referenceAbsent+ case referenceGap+ case referenceScopeLimited+ case referenceCompletenessUnobservable+ case captureGap+ case captureTimingSynthesized+ case delayUnresolved+ case clockDriftUnstable+ case engineUnavailable+}++nonisolated public struct MeetingMicrophonePCMFrame: Equatable, Sendable {+ public let sequenceNumber: Int+ public let sampleTime: Int64+ public let hostTime: Double?+ public let sampleRate: Double+ public let channelCount: Int+ public let samples: [Float]+ public let routeIdentifier: String+ public let discontinuity: Bool++ public init(+ sequenceNumber: Int,+ sampleTime: Int64,+ hostTime: Double?,+ sampleRate: Double,+ channelCount: Int = 1,+ samples: [Float],+ routeIdentifier: String = "default",+ discontinuity: Bool = false+ ) {+ self.sequenceNumber = sequenceNumber+ self.sampleTime = sampleTime+ self.hostTime = hostTime+ self.sampleRate = sampleRate+ self.channelCount = channelCount+ self.samples = samples+ self.routeIdentifier = routeIdentifier+ self.discontinuity = discontinuity+ }+}++nonisolated public struct MeetingReferencePCMFrame: Equatable, Sendable {+ public let sequenceNumber: Int+ /// PTS in the reference's own clock. It is deliberately not assumed to be host time.+ public let presentationTime: Double+ public let sampleRate: Double+ public let channelCount: Int+ public let samples: [Float]+ public let discontinuity: Bool++ public init(+ sequenceNumber: Int,+ presentationTime: Double,+ sampleRate: Double,+ channelCount: Int = 1,+ samples: [Float],+ discontinuity: Bool = false+ ) {+ self.sequenceNumber = sequenceNumber+ self.presentationTime = presentationTime+ self.sampleRate = sampleRate+ self.channelCount = channelCount+ self.samples = samples+ self.discontinuity = discontinuity+ }+}++nonisolated public struct MeetingSynchronizerConverterConfiguration: Equatable, Sendable {+ public let version: String+ /// Algorithmic delay is removed from the converted timeline, not hidden in the samples.+ public let algorithmicDelaySeconds: Double++ public init(version: String = "linear-v1", algorithmicDelaySeconds: Double = 0) {+ self.version = version+ self.algorithmicDelaySeconds = algorithmicDelaySeconds+ }+}++nonisolated public struct MeetingReferenceSynchronizerConfiguration: Equatable, Sendable {+ public static let defaultFrameDuration: Double = 0.010+ public let analysisSampleRate: Double+ public let frameDuration: Double+ public let microphoneConverter: MeetingSynchronizerConverterConfiguration+ public let referenceConverter: MeetingSynchronizerConverterConfiguration+ /// Positive values place the reference later on the session timeline. The reported+ /// lag sign is `microphoneTime - referenceTime`: positive means microphone lags.+ public let referenceToMicrophoneOffsetSeconds: Double+ /// Reference time is scaled by `1 + clockDriftPPM / 1e6`; this is an explicit fixture+ /// model, not silent time stretching of either captured stream.+ public let referenceClockDriftPPM: Double+ public let maximumClockDriftPPM: Double+ public let referenceScope: MeetingReferenceScope+ public let referenceCompleteness: MeetingReferenceCompleteness+ public let codecPrimingSamples: Int+ public let codecRemainderSamples: Int+ public let editListOffsetSeconds: Double+ public let maximumInputFrameCount: Int+ public let maximumInputSampleCount: Int+ public let maximumOutputFrameCount: Int+ public let engineAvailable: Bool++ public init(+ analysisSampleRate: Double = 16_000,+ frameDuration: Double = MeetingReferenceSynchronizerConfiguration.defaultFrameDuration,+ microphoneConverter: MeetingSynchronizerConverterConfiguration = .init(),+ referenceConverter: MeetingSynchronizerConverterConfiguration = .init(),+ referenceToMicrophoneOffsetSeconds: Double = 0,+ referenceClockDriftPPM: Double = 0,+ maximumClockDriftPPM: Double = 100,+ referenceScope: MeetingReferenceScope = .selectedApplication,+ referenceCompleteness: MeetingReferenceCompleteness = .unobservable,+ codecPrimingSamples: Int = 0,+ codecRemainderSamples: Int = 0,+ editListOffsetSeconds: Double = 0,+ maximumInputFrameCount: Int = 16_384,+ maximumInputSampleCount: Int = 16_000 * 60 * 60,+ maximumOutputFrameCount: Int = 16_000 * 60 * 60 / 160,+ engineAvailable: Bool = true+ ) {+ self.analysisSampleRate = analysisSampleRate+ self.frameDuration = frameDuration+ self.microphoneConverter = microphoneConverter+ self.referenceConverter = referenceConverter+ self.referenceToMicrophoneOffsetSeconds = referenceToMicrophoneOffsetSeconds+ self.referenceClockDriftPPM = referenceClockDriftPPM+ self.maximumClockDriftPPM = maximumClockDriftPPM+ self.referenceScope = referenceScope+ self.referenceCompleteness = referenceCompleteness+ self.codecPrimingSamples = codecPrimingSamples+ self.codecRemainderSamples = codecRemainderSamples+ self.editListOffsetSeconds = editListOffsetSeconds+ self.maximumInputFrameCount = maximumInputFrameCount+ self.maximumInputSampleCount = maximumInputSampleCount+ self.maximumOutputFrameCount = maximumOutputFrameCount+ self.engineAvailable = engineAvailable+ }+}++nonisolated public struct MeetingSynchronizedFrame: Equatable, Sendable {+ public let index: Int+ public let sessionStartTime: Double+ public let sessionEndTime: Double+ public let epochID: Int+ public let renderSamples: [Float]+ public let captureSamples: [Float]+ public let renderValidMask: [Bool]+ public let captureValidMask: [Bool]+ public let unknownReasons: [MeetingSynchronizerUnknownReason]+ public let adaptationFrozen: Bool+ public let resynchronizationBoundary: Bool+ /// Configured/mapped alignment (`captureTime - referenceTime`), not an observed+ /// acoustic delay. A positive value means the mapped capture timeline lags the+ /// mapped reference timeline; callers must retain `.delayUnresolved` unless a+ /// separate shared-clock calibration has been established.+ public let lagSeconds: Double?+ public let timelines: MeetingSynchronizerFrameTimelines++ public init(+ index: Int,+ sessionStartTime: Double,+ sessionEndTime: Double,+ epochID: Int,+ renderSamples: [Float],+ captureSamples: [Float],+ renderValidMask: [Bool],+ captureValidMask: [Bool],+ unknownReasons: [MeetingSynchronizerUnknownReason],+ adaptationFrozen: Bool,+ resynchronizationBoundary: Bool,+ lagSeconds: Double?,+ timelines: MeetingSynchronizerFrameTimelines? = nil+ ) {+ self.index = index+ self.sessionStartTime = sessionStartTime+ self.sessionEndTime = sessionEndTime+ self.epochID = epochID+ self.renderSamples = renderSamples+ self.captureSamples = captureSamples+ self.renderValidMask = renderValidMask+ self.captureValidMask = captureValidMask+ self.unknownReasons = unknownReasons+ self.adaptationFrozen = adaptationFrozen+ self.resynchronizationBoundary = resynchronizationBoundary+ self.lagSeconds = lagSeconds+ self.timelines = timelines ?? MeetingSynchronizerFrameTimelines(+ microphoneSourceTime: nil, referencePTSTime: nil, sessionMonotonicTime: sessionStartTime)+ }+}++nonisolated public struct MeetingSynchronizerDiagnostics: Equatable, Sendable {+ public let inputFrameCount: Int+ public let droppedFrameCount: Int+ public let epochCount: Int+ public let synthesizedMicrophoneFrameCount: Int+ public let duplicateOrLateFrameCount: Int+ public let nonFiniteSampleCount: Int+ public let boundedResourceFailure: Bool+ public let converterVersions: [String]+ public let converterDelays: [Double]+ public let referenceClockDriftPPM: Double?+}++/// The three clock domains are carried with every emitted pair. Values are seconds in+/// their respective domains; `sessionMonotonicTime` is the only timeline consumers may+/// use for pairing. A nil source value means that domain had no valid sample in the pair.+nonisolated public struct MeetingSynchronizerFrameTimelines: Equatable, Sendable {+ /// Sample-count time from the microphone source (seconds, in its own domain).+ public let microphoneSourceTime: Double?+ /// The original reference PTS domain (seconds); codec priming is reflected by the+ /// first valid sample's position, but edit-list/offset/drift are not folded into it.+ public let referencePTSTime: Double?+ public let sessionMonotonicTime: Double+ /// Original microphone sample-count and host-time coordinates, carried independently.+ public let microphoneSampleTime: Int64?+ public let microphoneHostTime: Double?++ public init(microphoneSourceTime: Double?, referencePTSTime: Double?, sessionMonotonicTime: Double) {+ self.init(microphoneSourceTime: microphoneSourceTime, microphoneSampleTime: nil,+ microphoneHostTime: nil, referencePTSTime: referencePTSTime,+ sessionMonotonicTime: sessionMonotonicTime)+ }++ public init(+ microphoneSourceTime: Double?, microphoneSampleTime: Int64?, microphoneHostTime: Double?,+ referencePTSTime: Double?, sessionMonotonicTime: Double+ ) {+ self.microphoneSourceTime = microphoneSourceTime+ self.microphoneSampleTime = microphoneSampleTime+ self.microphoneHostTime = microphoneHostTime+ self.referencePTSTime = referencePTSTime+ self.sessionMonotonicTime = sessionMonotonicTime+ }++}++nonisolated public struct MeetingSynchronizationResult: Equatable, Sendable {+ public let frames: [MeetingSynchronizedFrame]+ public let failedOpen: Bool+ public let failureReason: MeetingSynchronizerUnknownReason?+ public let diagnostics: MeetingSynchronizerDiagnostics++ public init(+ frames: [MeetingSynchronizedFrame],+ failedOpen: Bool,+ failureReason: MeetingSynchronizerUnknownReason?,+ diagnostics: MeetingSynchronizerDiagnostics+ ) {+ self.frames = frames+ self.failedOpen = failedOpen+ self.failureReason = failureReason+ self.diagnostics = diagnostics+ }+}++/// Deterministic first-slice synchronizer. The implementation is intentionally a pure+/// function over bounded arrays so fixtures can be replayed byte-for-byte in tests.+nonisolated public struct MeetingReferenceSynchronizer: Sendable {+ public let configuration: MeetingReferenceSynchronizerConfiguration++ public init(configuration: MeetingReferenceSynchronizerConfiguration = .init()) {+ self.configuration = configuration+ }++ public static func lagSeconds(microphoneTime: Double, referenceTime: Double) -> Double {+ microphoneTime - referenceTime+ }++ public func synchronize(+ microphone: [MeetingMicrophonePCMFrame],+ reference: [MeetingReferencePCMFrame]+ ) -> MeetingSynchronizationResult {+ var totalFrames = 0+ var inputArithmeticOverflow = false+ let (frameSum, frameOverflow) = microphone.count.addingReportingOverflow(reference.count)+ totalFrames = frameOverflow ? Int.max : frameSum+ inputArithmeticOverflow = frameOverflow+ var totalSamples = 0+ for sampleCount in microphone.map(\.samples.count) + reference.map(\.samples.count) {+ let (sum, overflow) = totalSamples.addingReportingOverflow(max(0, sampleCount))+ totalSamples = overflow ? Int.max : sum+ inputArithmeticOverflow = inputArithmeticOverflow || overflow+ }+ let invalidBudgets = configuration.maximumInputFrameCount <= 0+ || configuration.maximumInputSampleCount <= 0+ || configuration.maximumOutputFrameCount <= 0+ let overBudget = !configuration.engineAvailable || invalidBudgets || inputArithmeticOverflow+ || microphone.count > configuration.maximumInputFrameCount+ || reference.count > configuration.maximumInputFrameCount+ || totalFrames > configuration.maximumInputFrameCount+ || totalSamples > configuration.maximumInputSampleCount+ let converterVersions = [configuration.microphoneConverter.version, configuration.referenceConverter.version]+ let converterDelays = [configuration.microphoneConverter.algorithmicDelaySeconds, configuration.referenceConverter.algorithmicDelaySeconds]+ let emptyDiagnostics = MeetingSynchronizerDiagnostics(+ inputFrameCount: totalFrames,+ droppedFrameCount: 0,+ epochCount: 0,+ synthesizedMicrophoneFrameCount: 0,+ duplicateOrLateFrameCount: 0,+ nonFiniteSampleCount: 0,+ boundedResourceFailure: overBudget,+ converterVersions: converterVersions,+ converterDelays: converterDelays,+ referenceClockDriftPPM: finite(configuration.referenceClockDriftPPM) ? configuration.referenceClockDriftPPM : nil+ )+ guard !overBudget else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: emptyDiagnostics)+ }+ guard finite(configuration.maximumClockDriftPPM), configuration.maximumClockDriftPPM >= 0,+ finite(configuration.referenceClockDriftPPM),+ abs(configuration.referenceClockDriftPPM) <= configuration.maximumClockDriftPPM+ else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .clockDriftUnstable, diagnostics: emptyDiagnostics)+ }+ let referenceTimelineScale = 1 + configuration.referenceClockDriftPPM / 1_000_000+ guard finite(referenceTimelineScale), referenceTimelineScale > 0 else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .clockDriftUnstable, diagnostics: emptyDiagnostics)+ }+ guard finite(configuration.analysisSampleRate), configuration.analysisSampleRate > 0,+ finite(configuration.frameDuration), configuration.frameDuration > 0,+ finite(configuration.microphoneConverter.algorithmicDelaySeconds),+ finite(configuration.referenceConverter.algorithmicDelaySeconds),+ finite(configuration.referenceToMicrophoneOffsetSeconds),+ finite(configuration.editListOffsetSeconds),+ configuration.codecPrimingSamples >= 0, configuration.codecRemainderSamples >= 0+ else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: emptyDiagnostics)+ }++ var dropped = 0+ var duplicateOrLate = 0+ var nonFinite = 0+ let micTrack = buildMicrophoneTrack(microphone, dropped: &dropped, duplicateOrLate: &duplicateOrLate, nonFinite: &nonFinite)+ let refTrack = buildReferenceTrack(reference, dropped: &dropped, duplicateOrLate: &duplicateOrLate, nonFinite: &nonFinite)+ let tracks = [micTrack, refTrack]+ guard let firstTime = tracks.compactMap(\.firstTime).min(),+ let lastTime = tracks.compactMap(\.lastTime).max(), lastTime > firstTime+ else {+ let diagnostics = MeetingSynchronizerDiagnostics(+ inputFrameCount: totalFrames, droppedFrameCount: dropped, epochCount: 0,+ synthesizedMicrophoneFrameCount: micTrack.synthesizedFrameCount,+ duplicateOrLateFrameCount: duplicateOrLate, nonFiniteSampleCount: nonFinite,+ boundedResourceFailure: false, converterVersions: converterVersions,+ converterDelays: converterDelays, referenceClockDriftPPM: configuration.referenceClockDriftPPM+ )+ return MeetingSynchronizationResult(frames: [], failedOpen: false, failureReason: nil, diagnostics: diagnostics)+ }++ let frameProduct = configuration.analysisSampleRate * configuration.frameDuration+ guard finite(frameProduct), frameProduct > 0, frameProduct < Double(Int.max) else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: emptyDiagnostics)+ }+ let frameSizeDouble = frameProduct.rounded()+ guard frameSizeDouble >= 1, frameSizeDouble < Double(Int.max) else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: emptyDiagnostics)+ }+ let frameSize = Int(frameSizeDouble)+ let spanSamples = (lastTime - firstTime) * configuration.analysisSampleRate+ guard finite(spanSamples), spanSamples > 0 else {+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: emptyDiagnostics)+ }+ let frameCountDouble = ceil(spanSamples / Double(frameSize) - 1e-10)+ guard finite(frameCountDouble), frameCountDouble >= 1,+ frameCountDouble <= Double(configuration.maximumOutputFrameCount),+ frameCountDouble < Double(Int.max) else {+ let diagnostics = MeetingSynchronizerDiagnostics(+ inputFrameCount: totalFrames, droppedFrameCount: dropped, epochCount: 0,+ synthesizedMicrophoneFrameCount: micTrack.synthesizedFrameCount,+ duplicateOrLateFrameCount: duplicateOrLate, nonFiniteSampleCount: nonFinite,+ boundedResourceFailure: true, converterVersions: converterVersions,+ converterDelays: converterDelays, referenceClockDriftPPM: configuration.referenceClockDriftPPM)+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: diagnostics)+ }+ let frameCount = Int(frameCountDouble)+ guard frameCount <= Int.max / frameSize else {+ let diagnostics = MeetingSynchronizerDiagnostics(+ inputFrameCount: totalFrames, droppedFrameCount: dropped, epochCount: 0,+ synthesizedMicrophoneFrameCount: micTrack.synthesizedFrameCount,+ duplicateOrLateFrameCount: duplicateOrLate, nonFiniteSampleCount: nonFinite,+ boundedResourceFailure: true, converterVersions: converterVersions,+ converterDelays: converterDelays, referenceClockDriftPPM: configuration.referenceClockDriftPPM)+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: diagnostics)+ }+ let outputCount = frameCount * frameSize+ let outputDuration = Double(outputCount) / configuration.analysisSampleRate+ guard outputDuration.isFinite, (firstTime + outputDuration).isFinite else {+ let diagnostics = MeetingSynchronizerDiagnostics(+ inputFrameCount: totalFrames, droppedFrameCount: dropped, epochCount: 0,+ synthesizedMicrophoneFrameCount: micTrack.synthesizedFrameCount,+ duplicateOrLateFrameCount: duplicateOrLate, nonFiniteSampleCount: nonFinite,+ boundedResourceFailure: true, converterVersions: converterVersions,+ converterDelays: converterDelays, referenceClockDriftPPM: configuration.referenceClockDriftPPM)+ return MeetingSynchronizationResult(frames: [], failedOpen: true, failureReason: .engineUnavailable, diagnostics: diagnostics)+ }+ let micGrid = makeGrid(track: micTrack, origin: firstTime, frameSize: frameSize, outputCount: outputCount)+ let refGrid = makeGrid(track: refTrack, origin: firstTime, frameSize: frameSize, outputCount: outputCount)+ let epochEvents = deduplicatedEpochEvents(micTrack.epochEvents + refTrack.epochEvents)+ let resyncEvents = Set((micTrack.resynchronizationEvents + refTrack.resynchronizationEvents).map {+ // Round to the nearest fixed frame index so binary floating-point representation of+ // a 10 ms boundary cannot pin an event to the preceding frame.+ safeRoundedInt(($0 - firstTime) / (Double(frameSize) / configuration.analysisSampleRate))+ }.compactMap { $0 })+ var output: [MeetingSynchronizedFrame] = []+ output.reserveCapacity(frameCount)+ for frameIndex in 0..<frameCount {+ let start = firstTime + Double(frameIndex * frameSize) / configuration.analysisSampleRate+ let end = start + Double(frameSize) / configuration.analysisSampleRate+ let range = frameIndex * frameSize..<(frameIndex + 1) * frameSize+ let micValues = Array(micGrid.samples[range])+ let refValues = Array(refGrid.samples[range])+ let micMask = Array(micGrid.valid[range])+ let refMask = Array(refGrid.valid[range])+ var reasons: [MeetingSynchronizerUnknownReason] = []+ if reference.isEmpty { reasons.append(.referenceAbsent) }+ else if refTrack.hasInternalGap(in: start..<end) { reasons.append(.referenceGap) }+ if !micMask.contains(true) { reasons.append(.captureGap) }+ else if micTrack.hasInternalGap(in: start..<end) { reasons.append(.captureGap) }+ if micTrack.hasSynthesized(in: start..<end) { reasons.append(.captureTimingSynthesized) }+ if !reference.isEmpty {+ if configuration.referenceScope != .authorizedFullMix { reasons.append(.referenceScopeLimited) }+ if configuration.referenceCompleteness == .unobservable { reasons.append(.referenceCompletenessUnobservable) }+ // This offline fixture has no shared-clock/acoustic calibration. Its+ // configured alignment remains useful for deterministic tests, but is+ // never presented as a measured delay.+ reasons.append(.delayUnresolved)+ }+ if !refMask.contains(true) && !reference.isEmpty { reasons.append(.referenceGap) }+ if abs(configuration.referenceClockDriftPPM) > configuration.maximumClockDriftPPM * 0.75 { reasons.append(.clockDriftUnstable) }+ if !micMask.contains(true) || (!reference.isEmpty && !refMask.contains(true)) { reasons.append(.delayUnresolved) }+ reasons = orderedUnique(reasons)+ let anySynth = micTrack.hasSynthesized(in: start..<end)+ let epoch = epochEvents.filter { $0 <= start + 1e-9 }.count+ let micSourceTime = micGrid.sourceTimes[range].compactMap { $0 }.first+ let referenceSourceTime = refGrid.sourceTimes[range].compactMap { $0 }.first+ let microphoneSampleTime = micGrid.microphoneSampleTimes[range].compactMap { $0 }.first+ let microphoneHostTime = micGrid.microphoneHostTimes[range].compactMap { $0 }.first+ let lag: Double? = {+ guard !anySynth else { return nil }+ guard micSourceTime != nil, referenceSourceTime != nil else { return nil }+ // Use each track's mapped first-valid start. This retains codec priming and+ // absolute reference PTS along with converter, edit-list, drift, and path+ // offsets, while avoiding a false lag caused by independently selecting the+ // first covered sample in a partially overlapping output frame.+ guard let micStart = micTrack.firstTime, let referenceStart = refTrack.firstTime else {+ return nil+ }+ return micStart - referenceStart+ }()+ output.append(MeetingSynchronizedFrame(+ index: frameIndex, sessionStartTime: start - firstTime, sessionEndTime: end - firstTime,+ epochID: epoch, renderSamples: refValues, captureSamples: micValues,+ renderValidMask: refMask, captureValidMask: micMask, unknownReasons: reasons,+ adaptationFrozen: anySynth, resynchronizationBoundary: resyncEvents.contains(frameIndex), lagSeconds: lag,+ timelines: MeetingSynchronizerFrameTimelines(+ microphoneSourceTime: micSourceTime,+ microphoneSampleTime: microphoneSampleTime,+ microphoneHostTime: microphoneHostTime,+ referencePTSTime: referenceSourceTime,+ sessionMonotonicTime: start - firstTime)+ ))+ }+ let diagnostics = MeetingSynchronizerDiagnostics(+ inputFrameCount: totalFrames, droppedFrameCount: dropped, epochCount: max(1, epochEvents.count + 1),+ synthesizedMicrophoneFrameCount: micTrack.synthesizedFrameCount,+ duplicateOrLateFrameCount: duplicateOrLate, nonFiniteSampleCount: nonFinite,+ boundedResourceFailure: false, converterVersions: converterVersions,+ converterDelays: converterDelays, referenceClockDriftPPM: configuration.referenceClockDriftPPM+ )+ return MeetingSynchronizationResult(frames: output, failedOpen: false, failureReason: nil, diagnostics: diagnostics)+ }++ // MARK: Track normalization++ nonisolated private struct TrackGrid {+ var segments: [TimedSegment]+ var sourceOrigin: Double?+ var firstTime: Double?+ var lastTime: Double?+ var epochEvents: [Double]+ var resynchronizationEvents: [Double]+ var gapRanges: [Range<Double>]+ var synthesizedFrameCount: Int++ func hasInternalGap(in range: Range<Double>) -> Bool {+ gapRanges.contains { $0.lowerBound < range.upperBound && $0.upperBound > range.lowerBound }+ }++ func hasSynthesized(in range: Range<Double>) -> Bool {+ segments.contains { segment in+ segment.synthesized && segment.start < range.upperBound+ && segment.start + Double(segment.values.count) / segment.sessionSampleRate > range.lowerBound+ }+ }+ }++ nonisolated private struct TimedSegment {+ let start: Double+ let sourceStart: Double+ /// Untransformed source-clock coordinate used for per-frame provenance.+ let sourceTimelineStart: Double+ let sourceSampleTime: Int64?+ let hostTime: Double?+ let sourceSampleRate: Double+ let sessionSampleRate: Double+ let timelineScale: Double+ let values: [Float]+ let synthesized: Bool+ }++ nonisolated private struct Grid {+ var samples: [Float]+ var valid: [Bool]+ var sourceTimes: [Double?]+ var microphoneSampleTimes: [Int64?]+ var microphoneHostTimes: [Double?]+ }++ private func buildMicrophoneTrack(_ frames: [MeetingMicrophonePCMFrame], dropped: inout Int, duplicateOrLate: inout Int, nonFinite: inout Int) -> TrackGrid {+ var seen = Set<Int>()+ var epochEvents: [Double] = []+ var gaps: [Range<Double>] = []+ var synthEvents: [Double] = []+ var synthCount = 0+ var previousSequence: Int?+ var previousSampleTime: Int64?+ var previousHost: Double?+ var previousRate: Double?+ var previousRoute: String?+ var hadSynthesized = false+ var normalized: [(frame: MeetingMicrophonePCMFrame, start: Double, sourceStart: Double, sourceSampleTime: Int64, hostTime: Double?, values: [Float], synthesized: Bool)] = []+ var firstValidSample: Int64?+ for frame in frames {+ let isLate = seen.contains(frame.sequenceNumber)+ || (previousSequence.map { frame.sequenceNumber <= $0 } ?? false)+ guard !isLate else {+ duplicateOrLate += 1; dropped += 1+ continue+ }+ seen.insert(frame.sequenceNumber)+ let nominalStart: Double = {+ guard let firstValidSample, finite(frame.sampleRate), frame.sampleRate > 0 else { return 0 }+ guard let delta = safeSampleDelta(frame.sampleTime, firstValidSample) else { return 0 }+ return delta / frame.sampleRate+ }()+ let sequenceGapDetected = previousSequence.map { frame.sequenceNumber > $0 + 1 } ?? false+ guard frame.channelCount > 0, finite(frame.sampleRate), frame.sampleRate > 0 else {+ dropped += 1+ if normalized.last != nil {+ let gapStart = normalized.last.map { $0.start + Double($0.values.count) / $0.frame.sampleRate } ?? nominalStart+ let gapDuration = frame.sampleRate.isFinite && frame.sampleRate > 0+ ? Double(max(1, frame.samples.count / max(frame.channelCount, 1))) / frame.sampleRate+ : configuration.frameDuration+ appendEpochEvent(gapStart, to: &epochEvents)+ appendGap(gapStart..<gapStart + gapDuration, to: &gaps)+ }+ previousSequence = frame.sequenceNumber+ continue+ }+ if frame.samples.isEmpty {+ dropped += 1+ // An empty pre-origin block cannot establish a measured source origin.+ // Once a valid block exists, its finite timestamp locates one configured+ // hop of unknown coverage without creating a second reset marker.+ if let firstValidSample {+ let start = safeSampleDelta(frame.sampleTime, firstValidSample).map {+ $0 / frame.sampleRate - configuration.microphoneConverter.algorithmicDelaySeconds+ }+ if let start, start.isFinite, normalized.last != nil {+ let duration = normalized.last.map { Double($0.values.count) / $0.frame.sampleRate }+ ?? configuration.frameDuration+ appendGap(start..<start + max(duration, configuration.frameDuration), to: &gaps)+ appendEpochEvent(start, to: &epochEvents)+ }+ }+ if normalized.last != nil { previousSampleTime = frame.sampleTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ guard frame.samples.count % frame.channelCount == 0 else {+ dropped += 1+ // A pre-origin malformed block cannot establish a measured source+ // origin. Once the track is anchored, its finite timing locates the+ // midstream missing interval without using malformed audio as coverage.+ if normalized.last != nil {+ appendEpochEvent(nominalStart, to: &epochEvents)+ appendGap(nominalStart..<(nominalStart + Double(frame.samples.count) / Double(frame.channelCount) / frame.sampleRate), to: &gaps)+ }+ if normalized.last != nil { previousSampleTime = frame.sampleTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ let finiteSamples = frame.samples.allSatisfy { $0.isFinite }+ if !finiteSamples {+ nonFinite += frame.samples.filter { !$0.isFinite }.count; dropped += 1+ let gapStart = normalized.last.map { $0.start + Double($0.values.count) / $0.frame.sampleRate } ?? nominalStart+ let gapDuration = Double(frame.samples.count / frame.channelCount) / frame.sampleRate+ if normalized.last != nil {+ appendGap(gapStart..<gapStart + gapDuration, to: &gaps)+ appendEpochEvent(gapStart, to: &epochEvents)+ }+ if normalized.last != nil { previousSampleTime = frame.sampleTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ // A forward sequence number does not make a frame usable when its source+ // sample time moves backwards (or repeats). Drop it before it can create an+ // overlapping segment or manufacture a second epoch/gap. The sequence+ // cursor is still consumed, while continuity remains anchored to the last+ // accepted sample time.+ if let previousSampleTime, frame.sampleTime <= previousSampleTime {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ if firstValidSample == nil { firstValidSample = frame.sampleTime }+ guard let sampleDelta = safeSampleDelta(frame.sampleTime, firstValidSample ?? frame.sampleTime) else {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ let nominalStartFromOrigin = sampleDelta / frame.sampleRate+ let sourceStart = nominalStartFromOrigin - configuration.microphoneConverter.algorithmicDelaySeconds+ guard sourceStart.isFinite else {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ if let previousEnd = normalized.last.map({ $0.start + Double($0.values.count) / $0.frame.sampleRate }),+ sourceStart < previousEnd - 1e-9 {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ if sequenceGapDetected {+ let previousEnd = normalized.last.map { $0.start + Double($0.values.count) / $0.frame.sampleRate }+ if let previousEnd, sourceStart > previousEnd + 1e-9 {+ appendGap(previousEnd..<sourceStart, to: &gaps)+ }+ appendEpochEvent(sourceStart, to: &epochEvents)+ synthEvents.append(sourceStart)+ }+ let expected = previousSampleTime.flatMap { safeInt64Delta(frame.sampleTime, $0) }+ // A rejected pre-origin frame may supply a timing cursor, but with no+ // accepted segment there is no prior payload length to compare against.+ let expectedSamples: Int64? = normalized.last.map { Int64($0.values.count) }+ if !sequenceGapDetected,+ let expected, expected < 0 || (expectedSamples != nil && expected != expectedSamples!) || frame.discontinuity+ || previousRate != nil && frame.sampleRate != previousRate!+ || previousRoute != nil && frame.routeIdentifier != previousRoute!+ || (previousHost != nil && frame.hostTime != nil && frame.hostTime! <= previousHost!) {+ appendEpochEvent(sourceStart, to: &epochEvents)+ let previousEnd = normalized.last.map { $0.start + Double($0.values.count) / $0.frame.sampleRate } ?? sourceStart+ if sourceStart > previousEnd { appendGap(previousEnd..<sourceStart, to: &gaps) }+ }+ let validHost = frame.hostTime.map(finite) ?? false+ let synthesized = !validHost+ if validHost && hadSynthesized {+ synthEvents.append(sourceStart)+ appendEpochEvent(sourceStart, to: &epochEvents)+ hadSynthesized = false+ }+ if synthesized { synthCount += 1; hadSynthesized = true }+ if !synthesized, let host = frame.hostTime { previousHost = host }+ previousSampleTime = frame.sampleTime+ previousSequence = frame.sequenceNumber+ previousRate = frame.sampleRate+ previousRoute = frame.routeIdentifier+ normalized.append((frame, sourceStart, Double(frame.sampleTime) / frame.sampleRate,+ frame.sampleTime, frame.hostTime,+ downmix(frame.samples, channels: frame.channelCount), synthesized))+ }+ let segments = normalized.map { TimedSegment(start: $0.start, sourceStart: $0.sourceStart,+ sourceTimelineStart: Double($0.frame.sampleTime) / $0.frame.sampleRate,+ sourceSampleTime: $0.sourceSampleTime, hostTime: $0.hostTime,+ sourceSampleRate: $0.frame.sampleRate, sessionSampleRate: $0.frame.sampleRate, timelineScale: 1,+ values: $0.values, synthesized: $0.synthesized) }+ return makeTrackGrid(segments: segments, sourceOrigin: normalized.first?.sourceStart, firstTime: normalized.map { $0.start }.min(), epochEvents: epochEvents, resynchronizationEvents: synthEvents, gaps: gaps, synthesizedFrameCount: synthCount)+ }++ private func buildReferenceTrack(_ frames: [MeetingReferencePCMFrame], dropped: inout Int, duplicateOrLate: inout Int, nonFinite: inout Int) -> TrackGrid {+ var seen = Set<Int>()+ var normalized: [(frame: MeetingReferencePCMFrame, start: Double, sourceStart: Double, sourceSampleTime: Int64?, hostTime: Double?, values: [Float], synthesized: Bool)] = []+ var previousPTS: Double?+ var previousSequence: Int?+ var previousRate: Double?+ var referenceAnchorPTS: Double?+ var previousRejectedEnd: Double?+ var primingRemaining = configuration.codecPrimingSamples+ var epochEvents: [Double] = []+ var resyncEvents: [Double] = []+ var gaps: [Range<Double>] = []+ for frame in frames {+ let isLate = seen.contains(frame.sequenceNumber)+ || (previousSequence.map { frame.sequenceNumber <= $0 } ?? false)+ guard !isLate else {+ duplicateOrLate += 1; dropped += 1+ continue+ }+ seen.insert(frame.sequenceNumber)+ let sequenceGapDetected = previousSequence.map { frame.sequenceNumber > $0 + 1 } ?? false+ guard frame.channelCount > 0, finite(frame.sampleRate), frame.sampleRate > 0,+ finite(frame.presentationTime) else {+ dropped += 1+ if normalized.last != nil || referenceAnchorPTS != nil {+ let gapStart = previousRejectedEnd ?? normalized.last.map {+ $0.start + referenceSessionDuration(Double($0.values.count) / $0.frame.sampleRate)+ } ?? (frame.presentationTime.isFinite+ ? referenceSessionTime(sourceTime: frame.presentationTime - (referenceAnchorPTS ?? frame.presentationTime))+ : 0)+ let gapDuration = frame.sampleRate.isFinite && frame.sampleRate > 0+ ? referenceSessionDuration(Double(max(1, frame.samples.count / max(frame.channelCount, 1))) / frame.sampleRate)+ : configuration.frameDuration+ appendEpochEvent(gapStart, to: &epochEvents)+ appendGap(gapStart..<gapStart + gapDuration, to: &gaps)+ if referenceAnchorPTS != nil { previousRejectedEnd = gapStart + gapDuration }+ }+ if normalized.last != nil || referenceAnchorPTS != nil { previousPTS = frame.presentationTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ if frame.samples.isEmpty {+ dropped += 1+ if let anchor = referenceAnchorPTS {+ let start = referenceSessionTime(sourceTime: frame.presentationTime - anchor)+ if start.isFinite, normalized.last != nil {+ let duration = normalized.last.map {+ referenceSessionDuration(Double($0.values.count) / $0.frame.sampleRate)+ } ?? configuration.frameDuration+ appendEpochEvent(start, to: &epochEvents)+ appendGap(start..<start + max(duration, configuration.frameDuration), to: &gaps)+ previousRejectedEnd = start + max(duration, configuration.frameDuration)+ }+ }+ if normalized.last != nil || referenceAnchorPTS != nil { previousPTS = frame.presentationTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ guard frame.samples.count % frame.channelCount == 0 else {+ dropped += 1+ let malformedStart = referenceSessionTime(+ sourceTime: frame.presentationTime - (referenceAnchorPTS ?? frame.presentationTime)+ )+ if normalized.last != nil || referenceAnchorPTS != nil {+ appendEpochEvent(malformedStart, to: &epochEvents)+ let duration = referenceSessionDuration(Double(frame.samples.count) / Double(frame.channelCount) / frame.sampleRate)+ appendGap(malformedStart..<malformedStart + duration, to: &gaps)+ if referenceAnchorPTS != nil { previousRejectedEnd = malformedStart + duration }+ }+ if normalized.last != nil || referenceAnchorPTS != nil { previousPTS = frame.presentationTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ guard frame.samples.allSatisfy(\.isFinite) else {+ nonFinite += frame.samples.filter { !$0.isFinite }.count; dropped += 1+ let invalidStart = referenceSessionTime(+ sourceTime: frame.presentationTime - (referenceAnchorPTS ?? frame.presentationTime)+ )+ let duration = referenceSessionDuration(Double(frame.samples.count) / Double(frame.channelCount) / frame.sampleRate)+ if normalized.last != nil || referenceAnchorPTS != nil {+ appendGap(invalidStart..<invalidStart + duration, to: &gaps)+ appendEpochEvent(invalidStart, to: &epochEvents)+ if referenceAnchorPTS != nil { previousRejectedEnd = invalidStart + duration }+ }+ if normalized.last != nil || referenceAnchorPTS != nil { previousPTS = frame.presentationTime }+ previousSequence = frame.sequenceNumber+ continue+ }+ // A later-arriving frame with a repeated or backwards PTS cannot be placed+ // deterministically without overlapping accepted reference audio. Drop it+ // and consume its sequence number; the next accepted frame remains compared+ // with the last accepted PTS so any real forward hole is still represented.+ if let previousPTS, frame.presentationTime <= previousPTS {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ var values = downmix(frame.samples, channels: frame.channelCount)+ if referenceAnchorPTS == nil { referenceAnchorPTS = frame.presentationTime }+ let leading = min(primingRemaining, values.count)+ let candidateSourceStart = frame.presentationTime - (referenceAnchorPTS ?? frame.presentationTime)+ + Double(leading) / frame.sampleRate+ let candidateStart = referenceSessionTime(sourceTime: candidateSourceStart)+ guard candidateStart.isFinite else {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ if let previousEnd = previousRejectedEnd ?? normalized.last.map({+ $0.start + referenceSessionDuration(Double($0.values.count) / $0.frame.sampleRate)+ }), candidateStart < previousEnd - 1e-9 {+ dropped += 1+ duplicateOrLate += 1+ previousSequence = frame.sequenceNumber+ continue+ }+ if leading > 0 { values.removeFirst(leading) }+ primingRemaining -= leading+ let sourceStart = frame.presentationTime - (referenceAnchorPTS ?? frame.presentationTime)+ + Double(leading) / frame.sampleRate+ let start = referenceSessionTime(sourceTime: sourceStart)+ if sequenceGapDetected {+ let previousEnd = previousRejectedEnd ?? normalized.last.map {+ $0.start + referenceSessionDuration(Double($0.values.count) / $0.frame.sampleRate)+ }+ if let previousEnd, start > previousEnd + 1e-9 {+ appendGap(previousEnd..<start, to: &gaps)+ }+ appendEpochEvent(start, to: &epochEvents)+ // A sequence boundary is a reset marker even when sample-time coverage is+ // contiguous; it must not turn the valid current frame into a gap.+ // (The actual missing interval, when any, is recorded above.)+ // Keep this separate from measured drift or timing synthesis.+ resyncEvents.append(start)+ }+ // A discontinuity on the first accepted reference frame is metadata for+ // the stream's initial state, not a transition from an earlier epoch.+ if previousPTS != nil {+ let previousEnd = previousRejectedEnd ?? normalized.last.map {+ $0.start + referenceSessionDuration(Double($0.values.count) / $0.frame.sampleRate)+ } ?? start+ if !sequenceGapDetected && (frame.discontinuity || (previousRate != nil && frame.sampleRate != previousRate!)) {+ appendEpochEvent(start, to: &epochEvents)+ }+ // A reset marker and a measured positive timing hole are independent:+ // a route/rate/discontinuity transition can be contiguous (the common+ // case) or can also contain a real missing interval. Preserve the+ // latter instead of hiding it behind the epoch marker.+ if !sequenceGapDetected && start > previousEnd + 1e-9 {+ appendEpochEvent(start, to: &epochEvents)+ appendGap(previousEnd..<start, to: &gaps)+ }+ }+ previousPTS = frame.presentationTime+ previousSequence = frame.sequenceNumber+ previousRate = frame.sampleRate+ // Keep the original PTS (including the retained post-priming position) for+ // provenance; `start` remains the anchored/transformed session coordinate.+ normalized.append((frame, start, frame.presentationTime + Double(leading) / frame.sampleRate,+ nil, nil, values, false))+ previousRejectedEnd = nil+ }+ if configuration.codecRemainderSamples > 0, let last = normalized.indices.last {+ let trim = min(configuration.codecRemainderSamples, normalized[last].values.count)+ if trim > 0 { normalized[last].values.removeLast(trim) }+ }+ let segments = normalized.map { TimedSegment(start: $0.start, sourceStart: $0.sourceStart,+ sourceTimelineStart: $0.sourceStart,+ sourceSampleTime: $0.sourceSampleTime, hostTime: $0.hostTime,+ sourceSampleRate: $0.frame.sampleRate,+ sessionSampleRate: $0.frame.sampleRate / self.referenceTimelineScale,+ timelineScale: self.referenceTimelineScale,+ values: $0.values, synthesized: false) }+ return makeTrackGrid(segments: segments, sourceOrigin: normalized.first?.sourceStart, firstTime: normalized.map { $0.start }.min(), epochEvents: epochEvents, resynchronizationEvents: resyncEvents, gaps: gaps, synthesizedFrameCount: 0)+ }++ private func makeTrackGrid(+ segments: [TimedSegment], sourceOrigin: Double?, firstTime: Double?, epochEvents: [Double], resynchronizationEvents: [Double], gaps: [Range<Double>], synthesizedFrameCount: Int+ ) -> TrackGrid {+ let first = firstTime+ let last = segments.map { $0.start + Double($0.values.count) / $0.sessionSampleRate }.max()+ return TrackGrid(segments: segments, sourceOrigin: sourceOrigin, firstTime: first, lastTime: last, epochEvents: epochEvents, resynchronizationEvents: resynchronizationEvents, gapRanges: gaps, synthesizedFrameCount: synthesizedFrameCount)+ }++ private func makeGrid(track: TrackGrid, origin: Double, frameSize: Int, outputCount: Int) -> Grid {+ guard track.firstTime != nil, track.lastTime != nil, outputCount > 0 else {+ return Grid(samples: [Float](repeating: 0, count: outputCount), valid: [Bool](repeating: false, count: outputCount), sourceTimes: [Double?](repeating: nil, count: outputCount), microphoneSampleTimes: [Int64?](repeating: nil, count: outputCount), microphoneHostTimes: [Double?](repeating: nil, count: outputCount))+ }+ // The caller has already bounded `outputCount` after checking all span+ // arithmetic. Do not derive a second allocation length from untrusted clock+ // deltas here; segments outside the requested output window are clipped.+ var samples = [Float](repeating: 0, count: outputCount)+ var valid = [Bool](repeating: false, count: outputCount)+ var sourceTimes = [Double?](repeating: nil, count: outputCount)+ var microphoneSampleTimes = [Int64?](repeating: nil, count: outputCount)+ var microphoneHostTimes = [Double?](repeating: nil, count: outputCount)+ for segment in track.segments {+ let relativeIndex = (segment.start - origin) * configuration.analysisSampleRate+ guard let roundedIndex = safeRoundedInt(relativeIndex) else { continue }+ let startIndex = max(0, roundedIndex)+ let segmentEnd = segment.start + Double(segment.values.count) / segment.sessionSampleRate+ let relativeEndIndex = (segmentEnd - origin) * configuration.analysisSampleRate+ guard let roundedEndIndex = safeRoundedInt(relativeEndIndex) else { continue }+ let (targetCount, targetOverflow) = roundedEndIndex.subtractingReportingOverflow(roundedIndex)+ guard !targetOverflow, targetCount > 0 else { continue }+ let converted = resample(segment.values, fromRate: segment.sessionSampleRate,+ toRate: configuration.analysisSampleRate, maximumCount: outputCount,+ targetCount: targetCount)+ for (offset, value) in converted.enumerated() {+ let (index, indexOverflow) = startIndex.addingReportingOverflow(offset)+ guard !indexOverflow else { break }+ guard index >= 0, index < samples.count else { continue }+ // A duplicate overlap is a gap/epoch condition; first-arrival order is stable.+ if !valid[index] {+ samples[index] = value+ valid[index] = value.isFinite+ let sourceOffsetSamples = Double(offset) * segment.sourceSampleRate+ / configuration.analysisSampleRate / segment.timelineScale+ sourceTimes[index] = segment.sourceTimelineStart + sourceOffsetSamples / segment.sourceSampleRate+ microphoneSampleTimes[index] = segment.sourceSampleTime.flatMap {+ let offset = sourceOffsetSamples.rounded()+ guard offset.isFinite, offset > Double(Int64.min), offset < Double(Int64.max) else { return nil }+ let offsetInt = Int64(offset)+ let (sum, overflow) = $0.addingReportingOverflow(offsetInt)+ return overflow ? nil : sum+ }+ microphoneHostTimes[index] = segment.hostTime.map { $0 + sourceOffsetSamples / segment.sourceSampleRate }+ }+ }+ }+ _ = frameSize+ return Grid(samples: samples, valid: valid, sourceTimes: sourceTimes,+ microphoneSampleTimes: microphoneSampleTimes, microphoneHostTimes: microphoneHostTimes)+ }++ private func resample(_ values: [Float], fromRate: Double, toRate: Double,+ maximumCount: Int, targetCount: Int? = nil) -> [Float] {+ guard !values.isEmpty, finite(fromRate), fromRate > 0, finite(toRate), toRate > 0 else { return [] }+ guard maximumCount > 0 else { return [] }+ let count: Int+ if let targetCount {+ guard targetCount > 0 else { return [] }+ count = min(maximumCount, targetCount)+ } else {+ let countDouble = (Double(values.count) * toRate / fromRate).rounded()+ guard countDouble.isFinite, countDouble >= 1, countDouble < Double(Int.max) else { return [] }+ count = min(maximumCount, Int(countDouble))+ }+ if count == values.count { return values }+ return (0..<count).map { index in+ let position = Double(index) * fromRate / toRate+ let lower = min(values.count - 1, Int(position.rounded(.down)))+ let upper = min(values.count - 1, lower + 1)+ let fraction = Float(position - Double(lower))+ return values[lower] + (values[upper] - values[lower]) * fraction+ }+ }++ /// Maps a reference-domain PTS into session time. Every caller uses this helper for+ /// both segment starts and epoch/gap events, so edit-list and converter transforms cannot+ /// silently diverge from the frame grid.+ private func referenceSessionTime(sourceTime: Double) -> Double {+ let scale = self.referenceTimelineScale+ return sourceTime * scale + configuration.editListOffsetSeconds * scale+ - configuration.referenceConverter.algorithmicDelaySeconds+ + configuration.referenceToMicrophoneOffsetSeconds+ }++ private func referenceSessionDuration(_ sourceDuration: Double) -> Double {+ sourceDuration * self.referenceTimelineScale+ }++ private var referenceTimelineScale: Double {+ 1 + configuration.referenceClockDriftPPM / 1_000_000+ }++ private func downmix(_ samples: [Float], channels: Int) -> [Float] {+ guard channels > 0 else { return [] }+ let count = samples.count / channels+ // Deterministic first-slice policy: arithmetic mean of interleaved channels. This is+ // not a calibrated perceptual downmix; channel-layout metadata and a measured error+ // budget remain deferred to the multichannel fixture phase.+ return (0..<count).map { index in+ let base = index * channels+ // Accumulate in Double so finite Float samples (for example, two+ // `.greatestFiniteMagnitude` channels) cannot overflow to infinity+ // during the mean and silently become an invalid grid sample.+ let sum = samples[base..<base + channels].reduce(0.0) { partial, sample in+ partial + Double(sample)+ }+ return Float(sum / Double(channels))+ }+ }++ private func orderedUnique(_ reasons: [MeetingSynchronizerUnknownReason]) -> [MeetingSynchronizerUnknownReason] {+ let order = MeetingSynchronizerUnknownReason.allCases+ return order.filter { reasons.contains($0) }+ }++ /// Epoch markers from the two source tracks can describe the same reset boundary.+ /// Merge them in session-time order so a shared microphone/reference reset advances+ /// the output epoch exactly once, independent of which track was visited first.+ private func deduplicatedEpochEvents(_ events: [Double]) -> [Double] {+ events.filter { $0.isFinite }.sorted().reduce(into: [Double]()) { result, event in+ guard !result.contains(where: { abs($0 - event) <= 1e-9 }) else { return }+ result.append(event)+ }+ }++ private func appendEpochEvent(_ time: Double, to events: inout [Double]) {+ guard time.isFinite else { return }+ guard !events.contains(where: { abs($0 - time) <= 1e-9 }) else { return }+ events.append(time)+ }++ private func appendGap(_ range: Range<Double>, to gaps: inout [Range<Double>]) {+ guard range.lowerBound.isFinite, range.upperBound.isFinite,+ range.upperBound > range.lowerBound + 1e-9 else { return }+ guard !gaps.contains(where: {+ abs($0.lowerBound - range.lowerBound) <= 1e-9+ && abs($0.upperBound - range.upperBound) <= 1e-9+ }) else { return }+ gaps.append(range)+ }++ private func safeSampleDelta(_ lhs: Int64, _ rhs: Int64) -> Double? {+ let (delta, overflow) = lhs.subtractingReportingOverflow(rhs)+ guard !overflow else { return nil }+ let value = Double(delta)+ return value.isFinite ? value : nil+ }++ private func safeInt64Delta(_ lhs: Int64, _ rhs: Int64) -> Int64? {+ let (delta, overflow) = lhs.subtractingReportingOverflow(rhs)+ return overflow ? nil : delta+ }++ private func safeRoundedInt(_ value: Double) -> Int? {+ guard value.isFinite else { return nil }+ let rounded = value.rounded()+ guard rounded > Double(Int.min), rounded < Double(Int.max) else { return nil }+ return Int(rounded)+ }++ private func finite(_ value: Double) -> Bool { value.isFinite }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingSCKPairedAutorun.swift b/Sources/Fluid/Services/Meeting/MeetingSCKPairedAutorun.swiftnew file mode 100644index 00000000..78b563b8--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingSCKPairedAutorun.swift@@ -0,0 +1,283 @@+#if DEBUG++import AppKit+import AVFoundation+import CoreGraphics+import CoreMedia+import Darwin+import Foundation+import ScreenCaptureKit++/// DEBUG-only installed-app entry point for the C2 measurement. It is deliberately isolated from+/// AppServices and the meeting coordinator: the only retained state is aggregate numeric metadata.+nonisolated enum MeetingSCKPairedAutorun {+ private static let captureDurationSeconds = 5.0+ static let maximumRunSeconds = 12.0+ static let hardWatchdogSeconds = 14.0+ private static let operationTimeoutSeconds = 2.0+ private static let hardWatchdogFailureLine =+ "[C2_AUTORUN] {\"exitStatus\":1,\"reason\":\"C2 autorun exceeded its hard deadline\",\"status\":\"failure\"}"++ @discardableResult+ static func startIfRequested(+ environment: [String: String] = ProcessInfo.processInfo.environment+ ) -> Bool {+ guard MeetingSCKPairedDiagnosticGate.autorunEnabled(environment: environment) else {+ return false+ }+ let completionGate = CompletionGate()+ let watchdog = DispatchWorkItem {+ guard completionGate.claim() else { return }+ print(Self.hardWatchdogFailureLine)+ fflush(stdout)+ Darwin.exit(1)+ }+ DispatchQueue.global(qos: .userInitiated).asyncAfter(+ deadline: .now() + Self.hardWatchdogSeconds,+ execute: watchdog+ )+ Task { @MainActor in+ let outcome: Outcome+ do {+ outcome = try await Self.withTimeout(seconds: Self.maximumRunSeconds) {+ await Self.run(environment: environment)+ }+ } catch is CancellationError {+ outcome = Self.failure("C2 autorun was cancelled")+ } catch {+ outcome = Self.failure("C2 autorun exceeded its time limit")+ }+ watchdog.cancel()+ guard completionGate.claim() else { return }+ print("[C2_AUTORUN] " + outcome.line)+ fflush(stdout)+ Darwin.exit(outcome.exitStatus)+ }+ return true+ }++ private struct Outcome: Sendable {+ let exitStatus: Int32+ let line: String+ }++ @MainActor+ private static func run(environment: [String: String]) async -> Outcome {+ guard CGPreflightScreenCaptureAccess() else {+ return Self.failure("screen recording access is not preflight-authorized")+ }+ guard AVCaptureDevice.authorizationStatus(for: .audio) == .authorized else {+ return Self.failure("microphone access is not authorized")+ }+ guard let bundleID = environment[MeetingSCKPairedDiagnosticGate.targetBundleIDEnvironmentKey]?+ .trimmingCharacters(in: .whitespacesAndNewlines), !bundleID.isEmpty else {+ return Self.failure("target bundle ID is empty")+ }+ guard let microphone = AVCaptureDevice.default(for: .audio), !microphone.uniqueID.isEmpty else {+ return Self.failure("no default microphone device is available")+ }++ let content: SCShareableContent+ do {+ content = try await SCShareableContent.current+ } catch {+ return Self.failure("shareable content unavailable")+ }+ guard let application = content.applications.first(where: { $0.bundleIdentifier == bundleID }) else {+ return Self.failure("target application is not currently running")+ }+ guard application.processID > 0 else {+ return Self.failure("target application process provenance is unavailable")+ }+ guard let display = content.displays.first else {+ return Self.failure("no display is available")+ }++ let filter = SCContentFilter(display: display, including: [application], exceptingWindows: [])+ let configuration = SCStreamConfiguration()+ configuration.width = 2+ configuration.height = 2+ configuration.minimumFrameInterval = CMTime(value: 1, timescale: 1)+ configuration.queueDepth = 3+ configuration.showsCursor = false+ configuration.capturesAudio = true+ configuration.sampleRate = 48_000+ configuration.channelCount = 2+ configuration.excludesCurrentProcessAudio = true+ configuration.captureMicrophone = true+ configuration.microphoneCaptureDeviceID = microphone.uniqueID++ let collector = MeetingSCKPairedDiagnosticCollector(environment: environment)+ let provenance = MeetingSCKPairedDiagnosticProvenance(+ scope: .selectedApplicationDisplay,+ applicationSelectionConfirmed: application.processID > 0+ )+ guard provenance.isValid else {+ return Self.failure("selected application provenance is invalid")+ }+ let output = MeetingSCKPairedAutorunOutput(collector: collector, provenance: provenance)+ let stream = SCStream(filter: filter, configuration: configuration, delegate: nil)+ do {+ try stream.addStreamOutput(+ output, type: .audio,+ sampleHandlerQueue: DispatchQueue(label: "fluidvoice.c2.autorun.application", qos: .userInteractive)+ )+ try stream.addStreamOutput(+ output, type: .microphone,+ sampleHandlerQueue: DispatchQueue(label: "fluidvoice.c2.autorun.microphone", qos: .userInteractive)+ )+ } catch {+ await Self.stopCaptureBestEffort(stream)+ return Self.failure("paired ScreenCaptureKit outputs could not be attached")+ }++ do {+ try await Self.withTimeout(seconds: Self.operationTimeoutSeconds) {+ try await stream.startCapture()+ }+ try await Task.sleep(nanoseconds: UInt64(Self.captureDurationSeconds * 1_000_000_000))+ try Task.checkCancellation()+ try await Self.withTimeout(seconds: Self.operationTimeoutSeconds) {+ try await stream.stopCapture()+ }+ } catch is CancellationError {+ await Self.stopCaptureBestEffort(stream)+ return Self.failure("C2 autorun was cancelled")+ } catch {+ await Self.stopCaptureBestEffort(stream)+ return Self.failure("paired ScreenCaptureKit capture failed")+ }++ let report = collector.report()+ if let failure = Self.validationFailure(report) {+ return Self.failure(failure)+ }+ do {+ let reportObject = try JSONSerialization.jsonObject(with: report.jsonData())+ return Self.success(reportObject)+ } catch {+ return Self.failure("numeric C2 report serialization failed")+ }+ }++ private final class CompletionGate: @unchecked Sendable {+ private let lock = NSLock()+ private var completed = false++ func claim() -> Bool {+ self.lock.lock()+ defer { self.lock.unlock() }+ guard !self.completed else { return false }+ self.completed = true+ return true+ }+ }++ private static func validationFailure(_ report: MeetingSCKPairedDiagnosticReport) -> String? {+ guard report.enabled else { return "diagnostic gate was not enabled" }+ guard report.rejectedProvenanceCount == 0 else { return "provenance callbacks were rejected" }+ guard report.droppedAfterBoundCount == 0 else { return "collector bound dropped callbacks" }+ guard report.application.sampleCount > 0, report.microphone.sampleCount > 0 else {+ return "one or both paired outputs produced no samples"+ }+ guard report.application.validTimestampCount == report.application.sampleCount,+ report.microphone.validTimestampCount == report.microphone.sampleCount else {+ return "paired output timestamps were malformed"+ }+ guard report.application.firstPresentationSeconds?.isFinite == true,+ report.application.lastPresentationEndSeconds?.isFinite == true,+ report.microphone.firstPresentationSeconds?.isFinite == true,+ report.microphone.lastPresentationEndSeconds?.isFinite == true else {+ return "paired output timestamps were non-finite"+ }+ guard report.application.firstSampleRateHz?.isFinite == true,+ report.microphone.firstSampleRateHz?.isFinite == true,+ (report.application.firstSampleRateHz ?? 0) > 0,+ (report.microphone.firstSampleRateHz ?? 0) > 0,+ (report.application.firstChannelCount ?? 0) > 0,+ (report.microphone.firstChannelCount ?? 0) > 0 else {+ return "paired output formats were invalid"+ }+ guard report.acceptedSampleCount == report.application.sampleCount + report.microphone.sampleCount else {+ return "collector accepted count was inconsistent"+ }+ return nil+ }++ private static func success(_ report: Any) -> Outcome {+ let object: [String: Any] = ["exitStatus": 0, "report": report, "status": "success"]+ guard let line = Self.jsonLine(object) else {+ return Self.failure("numeric C2 report serialization failed")+ }+ return Outcome(exitStatus: 0, line: line)+ }++ private static func failure(_ reason: String) -> Outcome {+ let object: [String: Any] = ["exitStatus": 1, "reason": reason, "status": "failure"]+ return Outcome(exitStatus: 1, line: Self.jsonLine(object) ?? "{\"exitStatus\":1,\"status\":\"failure\"}")+ }++ private static func jsonLine(_ object: [String: Any]) -> String? {+ guard JSONSerialization.isValidJSONObject(object),+ let data = try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys]) else {+ return nil+ }+ return String(decoding: data, as: UTF8.self)+ }++ private enum AutorunError: Error {+ case timeout+ }++ private static func stopCaptureBestEffort(_ stream: SCStream) async {+ try? await Self.withTimeout(seconds: Self.operationTimeoutSeconds) {+ try await stream.stopCapture()+ }+ }++ private static func withTimeout<T: Sendable>(+ seconds: Double,+ operation: @escaping @Sendable () async throws -> T+ ) async throws -> T {+ try await withThrowingTaskGroup(of: T.self) { group in+ group.addTask { try await operation() }+ group.addTask {+ try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))+ throw AutorunError.timeout+ }+ defer { group.cancelAll() }+ return try await group.next()!+ }+ }+}++private final class MeetingSCKPairedAutorunOutput: NSObject, SCStreamOutput, @unchecked Sendable {+ private let collector: MeetingSCKPairedDiagnosticCollector+ private let provenance: MeetingSCKPairedDiagnosticProvenance++ init(+ collector: MeetingSCKPairedDiagnosticCollector,+ provenance: MeetingSCKPairedDiagnosticProvenance+ ) {+ self.collector = collector+ self.provenance = provenance+ }++ func stream(+ _ stream: SCStream,+ didOutputSampleBuffer sampleBuffer: CMSampleBuffer,+ of outputType: SCStreamOutputType+ ) {+ let output: MeetingSCKPairedOutput+ switch outputType {+ case .audio: output = .applicationAudio+ case .microphone: output = .microphone+ default: return+ }+ _ = self.collector.record(+ sampleBuffer: sampleBuffer, output: output, provenance: self.provenance+ )+ }+}++#endifdiff --git a/Sources/Fluid/Services/Meeting/MeetingSCKPairedDiagnostics.swift b/Sources/Fluid/Services/Meeting/MeetingSCKPairedDiagnostics.swiftnew file mode 100644index 00000000..83ee37f5--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingSCKPairedDiagnostics.swift@@ -0,0 +1,485 @@+#if DEBUG++import Foundation+import CoreMedia++/// Phase 0 C2 is deliberately diagnostic-only. The collector receives metadata from the two+/// outputs of one ScreenCaptureKit stream, but never receives (or retains) PCM, transcript text,+/// identities, titles, paths, or URLs. It does not establish a shared clock, acoustic latency,+/// echo cancellation, or a product-quality reference.+nonisolated enum MeetingSCKPairedDiagnosticGate {+ static let environmentKey = "FLUIDVOICE_C2_DIAGNOSTICS"+ static let hardwareEnvironmentKey = "FLUIDVOICE_C2_HARDWARE"+ static let targetBundleIDEnvironmentKey = "FLUIDVOICE_C2_TARGET_BUNDLE_ID"+ static let autorunEnvironmentKey = "FLUIDVOICE_C2_AUTORUN"++ static func enabled(environment: [String: String] = ProcessInfo.processInfo.environment) -> Bool {+ environment[Self.environmentKey] == "1"+ }++ static func hardwareEnabled(environment: [String: String] = ProcessInfo.processInfo.environment) -> Bool {+ guard self.enabled(environment: environment), environment[Self.hardwareEnvironmentKey] == "1" else {+ return false+ }+ return !(environment[Self.targetBundleIDEnvironmentKey] ?? "")+ .trimmingCharacters(in: .whitespacesAndNewlines).isEmpty+ }++ static func autorunEnabled(environment: [String: String] = ProcessInfo.processInfo.environment) -> Bool {+ self.hardwareEnabled(environment: environment)+ && environment[Self.autorunEnvironmentKey] == "1"+ }+}++nonisolated enum MeetingSCKPairedOutput: String, Codable, CaseIterable, Sendable {+ case applicationAudio = "audio"+ case microphone+}++/// Provenance is intentionally reduced to a scope enum and a positive selection assertion. No+/// application identity, window title, URL, or other user data is carried into C2 diagnostics.+nonisolated enum MeetingSCKPairedDiagnosticScope: String, Codable, CaseIterable, Sendable {+ case selectedApplicationWindow+ case selectedApplicationDisplay+}++nonisolated struct MeetingSCKPairedDiagnosticProvenance: Codable, Equatable, Sendable {+ let scope: MeetingSCKPairedDiagnosticScope+ let applicationSelectionConfirmed: Bool+ let captureMethod: String++ init(+ scope: MeetingSCKPairedDiagnosticScope,+ applicationSelectionConfirmed: Bool,+ captureMethod: String = "screenCaptureKit"+ ) {+ self.scope = scope+ self.applicationSelectionConfirmed = applicationSelectionConfirmed+ self.captureMethod = captureMethod+ }++ var isValid: Bool {+ self.applicationSelectionConfirmed && self.captureMethod == "screenCaptureKit"+ }+}++/// Metadata copied synchronously at a ScreenCaptureKit callback boundary. This type intentionally+/// has no buffer or sample payload. `arrivalSeconds` should come from a monotonic clock sampled by+/// the callback owner; it is only used to describe delivery jitter.+nonisolated struct MeetingSCKPairedDiagnosticSample: Sendable {+ let output: MeetingSCKPairedOutput+ let presentationSeconds: Double?+ let durationSeconds: Double?+ let frameCount: Int+ let sampleRateHz: Double+ let channelCount: Int+ let arrivalSeconds: Double?++ init(+ output: MeetingSCKPairedOutput,+ presentationSeconds: Double?,+ durationSeconds: Double?,+ frameCount: Int,+ sampleRateHz: Double,+ channelCount: Int,+ arrivalSeconds: Double? = nil+ ) {+ self.output = output+ self.presentationSeconds = presentationSeconds+ self.durationSeconds = durationSeconds+ self.frameCount = frameCount+ self.sampleRateHz = sampleRateHz+ self.channelCount = channelCount+ self.arrivalSeconds = arrivalSeconds+ }+}++nonisolated struct MeetingSCKPairedDiagnosticConfiguration: Equatable, Sendable {+ static let defaultMaximumSamples = 20_000+ let maximumSamples: Int++ init(maximumSamples: Int = Self.defaultMaximumSamples) {+ self.maximumSamples = max(1, maximumSamples)+ }+}++nonisolated struct MeetingSCKPairedTrackReport: Codable, Equatable, Sendable {+ let sampleCount: Int+ let validTimestampCount: Int+ let invalidTimestampCount: Int+ let deliveredDurationSeconds: Double+ let firstPresentationSeconds: Double?+ let lastPresentationEndSeconds: Double?+ let timestampSpanSeconds: Double?+ let coverageFraction: Double?+ let gapCount: Int+ let gapDurationSeconds: Double+ let overlapCount: Int+ let backwardsTimestampCount: Int+ let formatChangeCount: Int+ let firstSampleRateHz: Double?+ let firstChannelCount: Int?+ let jitterObservationCount: Int+ let meanAbsoluteJitterSeconds: Double?+ let maxAbsoluteJitterSeconds: Double?+ let malformedMetadataCount: Int++ fileprivate init(state: MeetingSCKPairedTrackState) {+ self.sampleCount = state.sampleCount+ self.validTimestampCount = state.validTimestampCount+ self.invalidTimestampCount = state.invalidTimestampCount+ self.deliveredDurationSeconds = state.deliveredDurationSeconds+ self.firstPresentationSeconds = state.firstPresentationSeconds+ self.lastPresentationEndSeconds = state.lastPresentationEndSeconds+ if let first = state.firstPresentationSeconds, let end = state.lastPresentationEndSeconds {+ self.timestampSpanSeconds = max(0, end - first)+ } else {+ self.timestampSpanSeconds = nil+ }+ if let span = self.timestampSpanSeconds, span > 0 {+ self.coverageFraction = min(1, max(0, state.deliveredDurationSeconds / span))+ } else {+ self.coverageFraction = nil+ }+ self.gapCount = state.gapCount+ self.gapDurationSeconds = state.gapDurationSeconds+ self.overlapCount = state.overlapCount+ self.backwardsTimestampCount = state.backwardsTimestampCount+ self.formatChangeCount = state.formatChangeCount+ self.firstSampleRateHz = state.firstSampleRateHz+ self.firstChannelCount = state.firstChannelCount+ self.jitterObservationCount = state.jitterObservationCount+ self.meanAbsoluteJitterSeconds = state.jitterObservationCount > 0+ ? state.jitterAbsoluteSum / Double(state.jitterObservationCount) : nil+ self.maxAbsoluteJitterSeconds = state.maxAbsoluteJitterSeconds+ self.malformedMetadataCount = state.malformedMetadataCount+ }+}++nonisolated struct MeetingSCKPairedCrossTrackReport: Codable, Equatable, Sendable {+ /// This is microphone PTS minus application PTS. It is a timestamp-origin offset, not an+ /// acoustic or processing latency measurement.+ let firstPresentationOffsetSeconds: Double?+ let lastPresentationOffsetSeconds: Double?+ /// Difference between the two observed PTS spans, expressed in parts per million. This is a+ /// delivery-window diagnostic, not a clock-drift estimate; independent PTS epochs and unequal+ /// start/stop boundaries make oscillator drift unidentifiable here.+ let relativeTimestampSpanDifferencePPM: Double?+ let timestampRelationship: String+ let acousticDelaySeconds: Double?+ let sharedClockEstablished: Bool+ let aecEvaluated: Bool++ fileprivate init(application: MeetingSCKPairedTrackReport, microphone: MeetingSCKPairedTrackReport) {+ if let app = application.firstPresentationSeconds, let mic = microphone.firstPresentationSeconds {+ self.firstPresentationOffsetSeconds = mic - app+ } else {+ self.firstPresentationOffsetSeconds = nil+ }+ if let app = application.lastPresentationEndSeconds, let mic = microphone.lastPresentationEndSeconds {+ self.lastPresentationOffsetSeconds = mic - app+ } else {+ self.lastPresentationOffsetSeconds = nil+ }+ if let appSpan = application.timestampSpanSeconds, let micSpan = microphone.timestampSpanSeconds,+ appSpan > 0, micSpan.isFinite {+ self.relativeTimestampSpanDifferencePPM = (micSpan / appSpan - 1) * 1_000_000+ } else {+ self.relativeTimestampSpanDifferencePPM = nil+ }+ self.timestampRelationship = "independent PTS origins; delivery-window span difference; clock drift not identifiable"+ self.acousticDelaySeconds = nil+ self.sharedClockEstablished = false+ self.aecEvaluated = false+ }+}++nonisolated struct MeetingSCKPairedDiagnosticReport: Codable, Equatable, Sendable {+ static let currentSchemaVersion = 3++ let schemaVersion: Int+ let enabled: Bool+ let boundedMaximumSamples: Int+ let acceptedSampleCount: Int+ let droppedAfterBoundCount: Int+ let application: MeetingSCKPairedTrackReport+ let microphone: MeetingSCKPairedTrackReport+ let crossTrack: MeetingSCKPairedCrossTrackReport+ let provenance: MeetingSCKPairedDiagnosticProvenance?+ let rejectedProvenanceCount: Int+ let rawPCMRetained: Bool+ let transcriptRetained: Bool+ let failOpen: Bool+ let reasons: [String]++ fileprivate init(+ enabled: Bool,+ configuration: MeetingSCKPairedDiagnosticConfiguration,+ acceptedSampleCount: Int,+ droppedAfterBoundCount: Int,+ application: MeetingSCKPairedTrackReport,+ microphone: MeetingSCKPairedTrackReport,+ provenance: MeetingSCKPairedDiagnosticProvenance?,+ rejectedProvenanceCount: Int+ ) {+ self.schemaVersion = Self.currentSchemaVersion+ self.enabled = enabled+ self.boundedMaximumSamples = configuration.maximumSamples+ self.acceptedSampleCount = acceptedSampleCount+ self.droppedAfterBoundCount = droppedAfterBoundCount+ self.application = application+ self.microphone = microphone+ self.crossTrack = MeetingSCKPairedCrossTrackReport(application: application, microphone: microphone)+ self.provenance = provenance+ self.rejectedProvenanceCount = rejectedProvenanceCount+ self.rawPCMRetained = false+ self.transcriptRetained = false+ self.failOpen = true+ self.reasons = [+ "diagnostic-only",+ "no raw PCM or transcript retention",+ "shared clock not established",+ "acoustic delay and AEC not evaluated"+ ]+ }++ func jsonData() throws -> Data {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.sortedKeys]+ return try encoder.encode(self)+ }+}++/// Thread-safe, bounded metadata collector suitable for a callback adapter. It is intentionally+/// Metadata is copied synchronously at the SCK callback boundary. The collector never retains a+/// CMSampleBuffer, PCM, transcript, or source identity. It remains inert unless the explicit gate is+/// enabled.+final nonisolated class MeetingSCKPairedDiagnosticCollector: @unchecked Sendable {+ private let lock = NSLock()+ private let configuration: MeetingSCKPairedDiagnosticConfiguration+ private let isEnabled: Bool+ private var acceptedSampleCount = 0+ private var droppedAfterBoundCount = 0+ private var provenance: MeetingSCKPairedDiagnosticProvenance?+ private var rejectedProvenanceCount = 0+ private var applicationState = MeetingSCKPairedTrackState()+ private var microphoneState = MeetingSCKPairedTrackState()++ init(+ configuration: MeetingSCKPairedDiagnosticConfiguration = .init(),+ environment: [String: String] = ProcessInfo.processInfo.environment+ ) {+ self.configuration = configuration+ self.isEnabled = MeetingSCKPairedDiagnosticGate.enabled(environment: environment)+ }++ var enabled: Bool { self.isEnabled }++ func record(_ sample: MeetingSCKPairedDiagnosticSample) {+ guard self.isEnabled else { return }+ self.lock.lock()+ defer { self.lock.unlock() }+ self.recordLocked(sample)+ }++ /// Records a metadata-only callback after requiring stable, explicit selected-app provenance.+ /// A provenance change is rejected rather than silently combining unrelated capture scopes.+ @discardableResult+ func record(+ _ sample: MeetingSCKPairedDiagnosticSample,+ provenance: MeetingSCKPairedDiagnosticProvenance+ ) -> Bool {+ guard self.isEnabled else { return false }+ self.lock.lock()+ defer { self.lock.unlock() }+ guard provenance.isValid else {+ self.rejectedProvenanceCount += 1+ return false+ }+ if let existing = self.provenance, existing != provenance {+ self.rejectedProvenanceCount += 1+ return false+ }+ self.provenance = provenance+ self.recordLocked(sample)+ return true+ }++ /// Extracts only numeric timing and format metadata while the callback is active. The sample+ /// buffer is not passed to, retained by, or stored in the collector.+ @discardableResult+ func record(+ sampleBuffer: CMSampleBuffer,+ output: MeetingSCKPairedOutput,+ provenance: MeetingSCKPairedDiagnosticProvenance+ ) -> Bool {+ guard self.isEnabled else { return false }+ return self.record(+ Self.metadata(from: sampleBuffer, output: output),+ provenance: provenance+ )+ }++ private func recordLocked(_ sample: MeetingSCKPairedDiagnosticSample) {+ guard self.acceptedSampleCount < self.configuration.maximumSamples else {+ self.droppedAfterBoundCount += 1+ return+ }+ self.acceptedSampleCount += 1+ switch sample.output {+ case .applicationAudio: self.applicationState.record(sample)+ case .microphone: self.microphoneState.record(sample)+ }+ }++ func report() -> MeetingSCKPairedDiagnosticReport {+ self.lock.lock()+ defer { self.lock.unlock() }+ return MeetingSCKPairedDiagnosticReport(+ enabled: self.isEnabled,+ configuration: self.configuration,+ acceptedSampleCount: self.acceptedSampleCount,+ droppedAfterBoundCount: self.droppedAfterBoundCount,+ application: MeetingSCKPairedTrackReport(state: self.applicationState),+ microphone: MeetingSCKPairedTrackReport(state: self.microphoneState),+ provenance: self.provenance,+ rejectedProvenanceCount: self.rejectedProvenanceCount+ )+ }++ private static func metadata(+ from sampleBuffer: CMSampleBuffer,+ output: MeetingSCKPairedOutput+ ) -> MeetingSCKPairedDiagnosticSample {+ let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)+ let streamDescription = formatDescription.flatMap {+ CMAudioFormatDescriptionGetStreamBasicDescription($0)?.pointee+ }+ let pts = Self.finiteSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer))+ let duration = Self.finiteSeconds(CMSampleBufferGetDuration(sampleBuffer))+ return MeetingSCKPairedDiagnosticSample(+ output: output,+ presentationSeconds: pts,+ durationSeconds: duration,+ frameCount: CMSampleBufferGetNumSamples(sampleBuffer),+ sampleRateHz: streamDescription?.mSampleRate ?? .nan,+ channelCount: Int(streamDescription?.mChannelsPerFrame ?? 0),+ arrivalSeconds: ProcessInfo.processInfo.systemUptime+ )+ }++ private static func finiteSeconds(_ time: CMTime) -> Double? {+ let seconds = CMTimeGetSeconds(time)+ return seconds.isFinite ? seconds : nil+ }+}++/// Deterministic, non-live harness for unit tests and local metadata fixtures. The fixture is+/// consumed synchronously and is not retained; callers must still provide metadata only.+nonisolated enum MeetingSCKPairedDiagnosticHarness {+ static func run(+ _ samples: [MeetingSCKPairedDiagnosticSample],+ configuration: MeetingSCKPairedDiagnosticConfiguration = .init(),+ environment: [String: String] = ProcessInfo.processInfo.environment+ ) -> MeetingSCKPairedDiagnosticReport {+ let collector = MeetingSCKPairedDiagnosticCollector(configuration: configuration, environment: environment)+ for sample in samples { collector.record(sample) }+ return collector.report()+ }+}++nonisolated private struct MeetingSCKPairedTrackState {+ var sampleCount = 0+ var validTimestampCount = 0+ var invalidTimestampCount = 0+ var deliveredDurationSeconds = 0.0+ var firstPresentationSeconds: Double?+ var lastPresentationEndSeconds: Double?+ var previousPresentationSeconds: Double?+ var previousDurationSeconds: Double?+ var previousSampleRateHz: Double?+ var previousArrivalSeconds: Double?+ var gapCount = 0+ var gapDurationSeconds = 0.0+ var overlapCount = 0+ var backwardsTimestampCount = 0+ var formatChangeCount = 0+ var firstSampleRateHz: Double?+ var firstChannelCount: Int?+ var jitterObservationCount = 0+ var jitterAbsoluteSum = 0.0+ var maxAbsoluteJitterSeconds: Double?+ var malformedMetadataCount = 0++ mutating func record(_ sample: MeetingSCKPairedDiagnosticSample) {+ self.sampleCount += 1+ let validFormat = sample.sampleRateHz.isFinite && sample.sampleRateHz > 0+ && sample.channelCount > 0 && sample.frameCount > 0+ let validDuration = sample.durationSeconds.map { $0.isFinite && $0 > 0 } ?? false+ let validPTS = sample.presentationSeconds.map(\.isFinite) ?? false+ let validFrameGeometry: Bool = {+ guard validFormat, validDuration else { return false }+ let frameDuration = Double(sample.frameCount) / sample.sampleRateHz+ let tolerance = max(1.5 / sample.sampleRateHz, 0.001)+ return frameDuration.isFinite && abs(frameDuration - sample.durationSeconds!) <= tolerance+ }()+ let validEnd: Bool = {+ guard validPTS, validDuration else { return false }+ return (sample.presentationSeconds! + sample.durationSeconds!).isFinite+ }()+ guard validFormat && validDuration && validPTS && validFrameGeometry && validEnd else {+ self.invalidTimestampCount += validPTS ? 0 : 1+ self.malformedMetadataCount += 1+ return+ }++ let pts = sample.presentationSeconds!+ let duration = sample.durationSeconds!+ self.validTimestampCount += 1+ self.deliveredDurationSeconds += duration+ if self.firstSampleRateHz == nil {+ self.firstSampleRateHz = sample.sampleRateHz+ self.firstChannelCount = sample.channelCount+ } else if self.firstSampleRateHz != sample.sampleRateHz || self.firstChannelCount != sample.channelCount {+ self.formatChangeCount += 1+ }++ if let previousPTS = self.previousPresentationSeconds,+ let previousDuration = self.previousDurationSeconds {+ let delta = pts - previousPTS+ // CMTime-to-Double conversion can perturb an otherwise contiguous boundary by a few+ // nanoseconds. Half a sample is a conservative representational tolerance: it ignores+ // sub-sample arithmetic noise while still exposing a genuine one-sample gap/overlap.+ let samplePeriod = max(1.0 / sample.sampleRateHz, 1.0 / (self.previousSampleRateHz ?? sample.sampleRateHz))+ let continuityTolerance = max(1e-9, 0.5 * samplePeriod)+ let deltaError = delta - previousDuration+ if delta < -continuityTolerance {+ self.backwardsTimestampCount += 1+ } else if deltaError > continuityTolerance {+ self.gapCount += 1+ self.gapDurationSeconds += deltaError+ } else if deltaError < -continuityTolerance {+ self.overlapCount += 1+ }+ }+ if let arrival = sample.arrivalSeconds, arrival.isFinite,+ let previousArrival = self.previousArrivalSeconds,+ let previousDuration = self.previousDurationSeconds {+ let deliveryDelta = arrival - previousArrival+ if deliveryDelta.isFinite && deliveryDelta >= 0 {+ let jitter = abs(deliveryDelta - previousDuration)+ self.jitterObservationCount += 1+ self.jitterAbsoluteSum += jitter+ self.maxAbsoluteJitterSeconds = max(self.maxAbsoluteJitterSeconds ?? 0, jitter)+ }+ }+ self.firstPresentationSeconds = self.firstPresentationSeconds ?? pts+ self.lastPresentationEndSeconds = max(self.lastPresentationEndSeconds ?? pts, pts + duration)+ self.previousPresentationSeconds = pts+ self.previousDurationSeconds = duration+ self.previousSampleRateHz = sample.sampleRateHz+ self.previousArrivalSeconds = sample.arrivalSeconds?.isFinite == true ? sample.arrivalSeconds : nil+ }+}++#endifdiff --git a/Sources/Fluid/Services/Meeting/MeetingSpeechActivityDetector.swift b/Sources/Fluid/Services/Meeting/MeetingSpeechActivityDetector.swiftnew file mode 100644index 00000000..0bf896ba--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingSpeechActivityDetector.swift@@ -0,0 +1,161 @@+import Foundation+import CoreML++/// Synchronous, single-owner offline seam. Implementations retain only one recurrent state.+nonisolated protocol MeetingSpeechActivityFrameModel {+ mutating func probability(samples: [Float], reset: Bool) throws -> Float+}++/// Direct adapter for the audited cached Silero 256ms v6 model; never invokes a downloader.+/// Its output is 1-product(1-p_i) across eight internal 32ms outputs, NOT voiced duration,+/// near-end probability, or eight independent observations for policy confidence.+nonisolated struct MeetingLocalSileroActivityModel: MeetingSpeechActivityFrameModel {+ enum Failure: Error { case schema, input, output }+ private let model: MLModel+ private var context = [Float](repeating: 0, count: 64)+ private var hidden = [Float](repeating: 0, count: 128)+ private var cell = [Float](repeating: 0, count: 128)++ init(modelURL: URL) throws {+ let configuration = MLModelConfiguration()+ configuration.computeUnits = .cpuOnly+ model = try MLModel(contentsOf: modelURL, configuration: configuration)+ func matches(_ descriptions: [String: MLFeatureDescription], _ shapes: [String: [Int]]) -> Bool {+ Set(descriptions.keys) == Set(shapes.keys) && shapes.allSatisfy { key, shape in+ guard let constraint = descriptions[key]?.multiArrayConstraint else { return false }+ return constraint.dataType == .float32 && constraint.shape.map(\.intValue) == shape+ }+ }+ guard matches(model.modelDescription.inputDescriptionsByName,+ ["audio_input": [1, 4160], "hidden_state": [1, 128], "cell_state": [1, 128]]),+ matches(model.modelDescription.outputDescriptionsByName,+ ["vad_output": [1, 1, 1], "new_hidden_state": [1, 128], "new_cell_state": [1, 128]])+ else { throw Failure.schema }+ }++ mutating func probability(samples: [Float], reset: Bool) throws -> Float {+ guard samples.count == 4096, samples.allSatisfy(\.isFinite) else { throw Failure.input }+ if reset {+ context = [Float](repeating: 0, count: 64)+ hidden = [Float](repeating: 0, count: 128); cell = hidden+ }+ func array(_ values: [Float]) throws -> MLMultiArray {+ let array = try MLMultiArray(shape: [1, NSNumber(value: values.count)], dataType: .float32)+ for i in values.indices { array[i] = NSNumber(value: values[i]) }+ return array+ }+ let features = try MLDictionaryFeatureProvider(dictionary: [+ "audio_input": array(context + samples), "hidden_state": array(hidden), "cell_state": array(cell)+ ])+ let output = try model.prediction(from: features)+ func values(_ name: String, count: Int) throws -> [Float] {+ guard let array = output.featureValue(for: name)?.multiArrayValue,+ array.dataType == .float32, array.count == count else { throw Failure.output }+ let values = (0..<count).map { array[$0].floatValue }+ guard values.allSatisfy(\.isFinite) else { throw Failure.output }+ return values+ }+ let p = try values("vad_output", count: 1)[0]+ guard (0...1).contains(p) else { throw Failure.output }+ let nextHidden = try values("new_hidden_state", count: 128)+ let nextCell = try values("new_cell_state", count: 128)+ hidden = nextHidden; cell = nextCell; context = Array(samples.suffix(64))+ return p+ }+}++nonisolated struct MeetingSpeechActivityDetector<Model: MeetingSpeechActivityFrameModel> {+ static var version: String { "speech-shadow-b2-v1" }+ struct Frame: Codable, Equatable {+ enum Unknown: String, Codable { case invalidInput, unscoredCoverage, contextWarmup, modelFailure, overBudget, cancelled, disabled }+ let start: Double+ let epoch: UInt64+ let probability: Float?+ let unknown: Unknown?+ /// Window-level activity only; not a voiced-duration annotation or near-end decision.+ var active: Bool? { probability.map { $0 >= 0.85 } }+ }+ private var model: Model+ private var lastEnd: Double?+ private var scope: String?+ private var sessionID: UUID?+ private var needsReset = true+ private var failures = 0+ private let clock: () -> Double+ init(model: Model, clock: @escaping () -> Double = { ProcessInfo.processInfo.systemUptime }) {+ self.model = model; self.clock = clock+ }+ /// Caller supplies exactly 4096 real 16kHz samples and validity flags. No tail padding.+ /// A first frame after reset is model warmup and intentionally unmeasured in the sidecar.+ /// One prediction cannot be preempted; budget/cancellation are checked before AND after it.+ mutating func process(samples: [Float], valid: [Bool], start: Double, session: UUID,+ epoch: UInt64, route: String, cancelled: () -> Bool = { false }) -> Frame {+ if sessionID != session { failures = 0; needsReset = true }+ sessionID = session+ func unknown(_ reason: Frame.Unknown) -> Frame {+ Frame(start: start.isFinite ? start : 0, epoch: epoch, probability: nil, unknown: reason)+ }+ guard start.isFinite, start >= 0, !route.isEmpty,+ samples.count == 4096, valid.count == samples.count,+ samples.allSatisfy(\.isFinite) else {+ needsReset = true; if failures < 3 { failures = 0 }; return unknown(.invalidInput)+ }+ let nextScope = "\(session):\(epoch):\(route)"+ if scope != nextScope || lastEnd.map({ abs($0 - start) > 1.0 / 32_000 }) != false { needsReset = true }+ scope = nextScope; lastEnd = start + 0.256+ guard !cancelled() else { needsReset = true; if failures < 3 { failures = 0 }; return unknown(.cancelled) }+ guard failures < 3 else { needsReset = true; return unknown(.disabled) }+ guard valid.allSatisfy({ $0 }) else { needsReset = true; failures = 0; return unknown(.unscoredCoverage) }+ let reset = needsReset, began = clock()+ do {+ let probability = try model.probability(samples: samples, reset: reset)+ guard !cancelled() else { needsReset = true; failures = 0; return unknown(.cancelled) }+ guard clock() - began <= 0.1 else {+ failures += 1; needsReset = true; return unknown(.overBudget)+ }+ guard probability.isFinite, (0...1).contains(probability) else {+ failures += 1; needsReset = true; return unknown(.modelFailure)+ }+ failures = 0; needsReset = false+ if reset { return unknown(.contextWarmup) }+ return Frame(start: start, epoch: epoch, probability: probability, unknown: nil)+ } catch {+ failures += 1; needsReset = true; return unknown(.modelFailure)+ }+ }+}++/// Offline B2 diagnostic combination. Correlated signals are never multiplied into confidence.+/// All combinations remain uncertain: no calibrated near-end/absence evidence exists yet.+nonisolated enum MeetingSpeechPlaybackShadowPolicy {+ enum TemporalContext: String, Codable { case supported, notSupported, mixed, unavailable }+ enum Reason: String, Codable {+ case missingSpeechEvidence, mixedOrLeakedActivity, activityWithoutReliablePlaybackMatch+ case duplicateWithoutDetectedActivity, negativeActivityIsNotAbsence+ }+ static func reason(activity: Bool?, reliableDuplicate: Bool) -> Reason {+ guard let activity else { return .missingSpeechEvidence }+ if activity { return reliableDuplicate ? .mixedOrLeakedActivity : .activityWithoutReliablePlaybackMatch }+ return reliableDuplicate ? .duplicateWithoutDetectedActivity : .negativeActivityIsNotAbsence+ }++ /// Join by interval overlap, not a single enclosing 2s window: 256ms frames often straddle+ /// that grid. All overlapping evidence must cover the frame in the same epoch. Mixed+ /// windows stay explicit and never borrow a sibling's supported state.+ static func temporalContext(start: Double, epoch: UInt64,+ windows: [MeetingPlaybackDuplicateDetector.Result]) -> TemporalContext {+ guard start.isFinite, start >= 0 else { return .unavailable }+ let end = start + 0.256+ let overlapping = windows.filter { $0.end > start && $0.start < end }.sorted { $0.start < $1.start }+ var cursor = start, supported = false, unsupported = false+ for window in overlapping {+ guard window.epoch == epoch, window.start.isFinite, window.end.isFinite,+ window.end > window.start, window.start <= cursor + 1e-9,+ window.state != .insufficientEvidence else { return .unavailable }+ if window.state == .duplicateSupported { supported = true } else { unsupported = true }+ cursor = max(cursor, min(end, window.end))+ }+ guard cursor >= end - 1e-9 else { return .unavailable }+ return supported ? (unsupported ? .mixed : .supported) : .notSupported+ }+}diff --git a/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticMetrics.swift b/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticMetrics.swiftnew file mode 100644index 00000000..a6b279c3--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticMetrics.swift@@ -0,0 +1,505 @@+#if DEBUG++ import Accelerate+ import Foundation++ // MARK: - Metric records++ nonisolated struct MeetingVPIOAcousticDelayMetrics: Codable, Equatable, Sendable {+ /// Positive means the captured signal lags the rendered reference on the shared host-time+ /// grid. Negative is physically impossible for an acoustic path and therefore evidence of a+ /// timestamp-domain error rather than of a fast echo.+ var signedSeconds: Double?+ var peakToMedianRatio: Double?+ var firstHalfSignedSeconds: Double?+ var secondHalfSignedSeconds: Double?+ /// Half the split-half spread, floored at the sample quantum. Not a confidence interval.+ var uncertaintySeconds: Double?+ var quantizationSeconds: Double+ var maxLagSeconds: Double+ var resolved: Bool+ var reasons: [MeetingVPIOAcousticReason]+ }++ nonisolated struct MeetingVPIOAcousticBandMetrics: Codable, Equatable, Sendable {+ var lowHz: Double+ var highHz: Double+ var binCount: Int+ var frameCount: Int+ var supportedCellCount: Int+ /// Share of (frame, bin) cells where the reference actually excited this band. A band with+ /// no excitation cannot report attenuation, however quiet the capture is.+ var supportFraction: Double+ var coherence: Double?+ /// 10·log10(referenceBandEnergy / capturedBandEnergy). Positive means the captured band is+ /// quieter than the rendered band. Uncalibrated: it mixes the electrical reference with an+ /// acoustic capture, so only differences between variants on one unchanged rig are readable.+ var attenuationDB: Double?+ var referenceEnergyFraction: Double+ var capturedEnergyFraction: Double+ var supported: Bool+ var coherent: Bool+ var reasons: [MeetingVPIOAcousticReason]+ }++ nonisolated struct MeetingVPIOAcousticMeasurement: Codable, Equatable, Sendable {+ static let currentSchemaVersion = 1++ var schemaVersion: Int+ var sampleRate: Double+ var referenceFrameCount: Int+ var capturedFrameCount: Int+ var analysisStartFrame: Int+ var analysisFrameCount: Int+ var referenceRMS: Double+ var referencePeak: Double+ var capturedRMS: Double+ var capturedPeak: Double+ var capturedNoiseFloorRMS: Double?+ var signalToNoiseFloorDB: Double?+ /// Broadband counterpart of `MeetingVPIOAcousticBandMetrics.attenuationDB`, same caveat.+ var attenuationDB: Double?+ var attenuationUncertaintyDB: Double?+ var delay: MeetingVPIOAcousticDelayMetrics+ var bands: [MeetingVPIOAcousticBandMetrics]+ var supportedBandCount: Int+ var coherentBandCount: Int+ /// True only when the delay resolved *and* enough bands were both excited and coherent.+ /// A false `valid` never means "no echo"; it means this measurement says nothing.+ var valid: Bool+ var reasons: [MeetingVPIOAcousticReason]+ }++ // MARK: - Metrics++ nonisolated enum MeetingVPIOAcousticMetrics {+ static let frameLength = 1_024+ static let hopLength = 512+ static let maxLagSeconds = 0.5+ static let minimumAnalysisFrames = 4_096+ static let minimumReferenceRMS: Float = 1e-4+ static let minimumCapturedRMS: Float = 1e-5+ static let minimumDelayConfidence = MeetingEchoSignalScorer.minimumDelayConfidence+ static let maximumSplitHalfDisagreementSeconds = 0.005+ static let minimumBandSupportFraction = 0.25+ static let minimumBandCoherence = 0.30+ static let minimumCoherentBandCount = 2+ static let minimumBandFrameCount = 8+ /// Fraction of the reference peak that still counts as excitation when bounding the+ /// analysis window; below it the segment is the stimulus's own silence.+ static let excitationOnsetFraction: Float = 0.005++ static let bands: [(lowHz: Double, highHz: Double)] = [+ (180, 400), (400, 900), (900, 1_800), (1_800, 3_600), (3_600, 7_800)+ ]++ /// `reference` and `captured` share one index origin: index 0 is the same nominal instant on+ /// the host-time grid. `silencePrefixFrames` marks the leading span the reference is known to+ /// be silent in, used for the captured noise floor.+ static func measure(+ reference: [Float],+ captured: [Float],+ sampleRate: Double,+ silencePrefixFrames: Int+ ) -> MeetingVPIOAcousticMeasurement {+ let quantization = sampleRate > 0 ? 1 / sampleRate : 0+ let emptyDelay = MeetingVPIOAcousticDelayMetrics(+ signedSeconds: nil,+ peakToMedianRatio: nil,+ firstHalfSignedSeconds: nil,+ secondHalfSignedSeconds: nil,+ uncertaintySeconds: nil,+ quantizationSeconds: quantization,+ maxLagSeconds: Self.maxLagSeconds,+ resolved: false,+ reasons: []+ )+ var measurement = MeetingVPIOAcousticMeasurement(+ schemaVersion: MeetingVPIOAcousticMeasurement.currentSchemaVersion,+ sampleRate: sampleRate,+ referenceFrameCount: reference.count,+ capturedFrameCount: captured.count,+ analysisStartFrame: 0,+ analysisFrameCount: 0,+ referenceRMS: Double(MeetingVPIOAcousticStimulus.rms(reference)),+ referencePeak: Double(MeetingVPIOAcousticStimulus.peak(reference)),+ capturedRMS: Double(MeetingVPIOAcousticStimulus.rms(captured)),+ capturedPeak: Double(MeetingVPIOAcousticStimulus.peak(captured)),+ capturedNoiseFloorRMS: nil,+ signalToNoiseFloorDB: nil,+ attenuationDB: nil,+ attenuationUncertaintyDB: nil,+ delay: emptyDelay,+ bands: [],+ supportedBandCount: 0,+ coherentBandCount: 0,+ valid: false,+ reasons: []+ )++ guard sampleRate > 0,+ reference.count >= Self.minimumAnalysisFrames,+ captured.count >= Self.minimumAnalysisFrames+ else {+ measurement.reasons.append(.insufficientFrames)+ return measurement+ }+ if Float(measurement.referenceRMS) < Self.minimumReferenceRMS {+ measurement.reasons.append(.referenceBelowExcitationFloor)+ }+ if Float(measurement.capturedRMS) < Self.minimumCapturedRMS {+ measurement.reasons.append(.capturedBelowNoiseFloor)+ }+ guard measurement.reasons.isEmpty else { return measurement }++ measurement.delay = Self.estimateDelay(reference: reference, captured: captured, sampleRate: sampleRate)+ measurement.reasons.append(contentsOf: measurement.delay.reasons)++ // Unresolved delay still gets band numbers, at zero lag, so the sidecar shows why the+ // run was rejected instead of showing nothing.+ let delayFrames = Int(((measurement.delay.signedSeconds ?? 0) * sampleRate).rounded())+ let aligned = Self.shift(reference, byFrames: delayFrames, targetCount: captured.count)++ guard let analysis = Self.excitedRange(aligned) else {+ measurement.reasons.append(.emptyAnalysisWindow)+ return measurement+ }+ measurement.analysisStartFrame = analysis.lowerBound+ measurement.analysisFrameCount = analysis.count++ let alignedWindow = Array(aligned[analysis])+ let capturedWindow = Array(captured[analysis])+ if let floor = Self.noiseFloorRMS(captured, prefixFrames: min(silencePrefixFrames, analysis.lowerBound)) {+ measurement.capturedNoiseFloorRMS = Double(floor)+ measurement.signalToNoiseFloorDB = Self.decibels(+ numerator: MeetingVPIOAcousticStimulus.rms(capturedWindow),+ denominator: floor+ )+ }+ measurement.attenuationDB = Self.decibels(+ numerator: MeetingVPIOAcousticStimulus.rms(alignedWindow),+ denominator: MeetingVPIOAcousticStimulus.rms(capturedWindow)+ )+ measurement.attenuationUncertaintyDB = Self.splitHalfAttenuationSpreadDB(+ reference: alignedWindow,+ captured: capturedWindow+ )++ measurement.bands = Self.bandMetrics(+ reference: alignedWindow,+ captured: capturedWindow,+ sampleRate: sampleRate+ )+ measurement.supportedBandCount = measurement.bands.filter { $0.supported }.count+ measurement.coherentBandCount = measurement.bands.filter { $0.coherent }.count+ if measurement.coherentBandCount < Self.minimumCoherentBandCount {+ measurement.reasons.append(.tooFewCoherentBands)+ }+ measurement.valid = measurement.delay.resolved && measurement.reasons.isEmpty+ return measurement+ }++ // MARK: Delay++ private static func estimateDelay(+ reference: [Float],+ captured: [Float],+ sampleRate: Double+ ) -> MeetingVPIOAcousticDelayMetrics {+ let quantization = 1 / sampleRate+ var metrics = MeetingVPIOAcousticDelayMetrics(+ signedSeconds: nil,+ peakToMedianRatio: nil,+ firstHalfSignedSeconds: nil,+ secondHalfSignedSeconds: nil,+ uncertaintySeconds: nil,+ quantizationSeconds: quantization,+ maxLagSeconds: Self.maxLagSeconds,+ resolved: false,+ reasons: []+ )++ // Shared with the production echo scorer, including its sign convention: the estimate is+ // the lag of the first argument relative to the second.+ guard let full = MeetingEchoSignalScorer.estimateDelay(+ mic: captured,+ reference: reference,+ sampleRate: sampleRate,+ maxLagSeconds: Self.maxLagSeconds+ ) else {+ metrics.reasons.append(.delayUnresolved)+ return metrics+ }+ metrics.signedSeconds = full.seconds+ metrics.peakToMedianRatio = full.confidence+ if full.seconds < 0 {+ metrics.reasons.append(.negativeDelay)+ }++ let halfCount = min(reference.count, captured.count) / 2+ if halfCount >= MeetingEchoSignalScorer.frameLength {+ let firstHalf = MeetingEchoSignalScorer.estimateDelay(+ mic: Array(captured[0..<halfCount]),+ reference: Array(reference[0..<halfCount]),+ sampleRate: sampleRate,+ maxLagSeconds: Self.maxLagSeconds+ )+ let secondHalf = MeetingEchoSignalScorer.estimateDelay(+ mic: Array(captured[halfCount..<(2 * halfCount)]),+ reference: Array(reference[halfCount..<(2 * halfCount)]),+ sampleRate: sampleRate,+ maxLagSeconds: Self.maxLagSeconds+ )+ metrics.firstHalfSignedSeconds = firstHalf?.seconds+ metrics.secondHalfSignedSeconds = secondHalf?.seconds+ if let first = firstHalf?.seconds, let second = secondHalf?.seconds {+ let spread = abs(first - second) / 2+ metrics.uncertaintySeconds = max(spread, quantization)+ if abs(first - second) > Self.maximumSplitHalfDisagreementSeconds {+ metrics.reasons.append(.splitHalfDelayDisagreement)+ }+ }+ }+ if metrics.uncertaintySeconds == nil {+ metrics.uncertaintySeconds = quantization+ }+ if full.confidence < Self.minimumDelayConfidence {+ metrics.reasons.append(.delayConfidenceBelowThreshold)+ }+ metrics.resolved = metrics.reasons.isEmpty+ return metrics+ }++ // MARK: Bands++ private static func bandMetrics(+ reference: [Float],+ captured: [Float],+ sampleRate: Double+ ) -> [MeetingVPIOAcousticBandMetrics] {+ guard let spectra = Self.crossSpectra(reference: reference, captured: captured) else {+ return Self.bands.map { band in+ MeetingVPIOAcousticBandMetrics(+ lowHz: band.lowHz,+ highHz: band.highHz,+ binCount: 0,+ frameCount: 0,+ supportedCellCount: 0,+ supportFraction: 0,+ coherence: nil,+ attenuationDB: nil,+ referenceEnergyFraction: 0,+ capturedEnergyFraction: 0,+ supported: false,+ coherent: false,+ reasons: [.insufficientFrames]+ )+ }+ }++ let binWidth = sampleRate / Double(Self.frameLength)+ let totalReferenceEnergy = spectra.referencePower.reduce(0, +)+ let totalCapturedEnergy = spectra.capturedPower.reduce(0, +)++ return Self.bands.map { band in+ var referenceEnergy = 0.0+ var capturedEnergy = 0.0+ var weightedCoherence = 0.0+ var coherenceWeight = 0.0+ var binCount = 0+ var supportedCells = 0++ // Bin 0 carries DC and Nyquist packed together in vDSP's real FFT; never used.+ for bin in 1..<spectra.binCount {+ let frequency = Double(bin) * binWidth+ guard frequency >= band.lowHz, frequency < band.highHz else { continue }+ binCount += 1+ let referencePower = spectra.referencePower[bin]+ let capturedPower = spectra.capturedPower[bin]+ referenceEnergy += referencePower+ capturedEnergy += capturedPower+ supportedCells += spectra.supportedCells[bin]+ guard referencePower > 0, capturedPower > 0 else { continue }+ let crossMagnitudeSquared = spectra.crossReal[bin] * spectra.crossReal[bin]+ + spectra.crossImaginary[bin] * spectra.crossImaginary[bin]+ let coherence = min(1, crossMagnitudeSquared / (referencePower * capturedPower))+ weightedCoherence += coherence * referencePower+ coherenceWeight += referencePower+ }++ let cellCount = binCount * spectra.frameCount+ let supportFraction = cellCount > 0 ? Double(supportedCells) / Double(cellCount) : 0+ let coherence = coherenceWeight > 0 ? weightedCoherence / coherenceWeight : nil+ let supported = supportFraction >= Self.minimumBandSupportFraction+ && spectra.frameCount >= Self.minimumBandFrameCount+ let attenuation = supported && referenceEnergy > 0 && capturedEnergy > 0+ ? 10 * log10(referenceEnergy / capturedEnergy)+ : nil+ var reasons: [MeetingVPIOAcousticReason] = []+ if !supported { reasons.append(.bandSupportBelowThreshold) }+ let coherent = supported && (coherence ?? 0) >= Self.minimumBandCoherence+ if supported, !coherent { reasons.append(.bandCoherenceBelowFloor) }++ return MeetingVPIOAcousticBandMetrics(+ lowHz: band.lowHz,+ highHz: band.highHz,+ binCount: binCount,+ frameCount: spectra.frameCount,+ supportedCellCount: supportedCells,+ supportFraction: supportFraction,+ coherence: coherence,+ attenuationDB: attenuation,+ referenceEnergyFraction: totalReferenceEnergy > 0 ? referenceEnergy / totalReferenceEnergy : 0,+ capturedEnergyFraction: totalCapturedEnergy > 0 ? capturedEnergy / totalCapturedEnergy : 0,+ supported: supported,+ coherent: coherent,+ reasons: reasons+ )+ }+ }++ private struct CrossSpectra {+ var binCount: Int+ var frameCount: Int+ var referencePower: [Double]+ var capturedPower: [Double]+ var crossReal: [Double]+ var crossImaginary: [Double]+ var supportedCells: [Int]+ }++ /// Welch-style accumulation over Hann frames. Absolute scaling is deliberately not undone:+ /// every published number here is a ratio, so vDSP's real-FFT factor cancels.+ private static func crossSpectra(reference: [Float], captured: [Float]) -> CrossSpectra? {+ let count = min(reference.count, captured.count)+ guard count >= Self.frameLength else { return nil }+ let frameCount = (count - Self.frameLength) / Self.hopLength + 1+ guard frameCount > 0 else { return nil }++ let log2n = vDSP_Length(log2(Double(Self.frameLength)))+ guard let setup = vDSP_create_fftsetup(log2n, FFTRadix(kFFTRadix2)) else { return nil }+ defer { vDSP_destroy_fftsetup(setup) }++ var window = [Float](repeating: 0, count: Self.frameLength)+ vDSP_hann_window(&window, vDSP_Length(Self.frameLength), Int32(vDSP_HANN_NORM))+ var windowPower: Float = 0+ vDSP_svesq(window, 1, &windowPower, vDSP_Length(window.count))+ // Parseval-consistent floor: the same RMS threshold expressed in accumulated bin units.+ let supportFloor = Double(Self.minimumReferenceRMS * Self.minimumReferenceRMS * windowPower)++ let binCount = Self.frameLength / 2+ var spectra = CrossSpectra(+ binCount: binCount,+ frameCount: frameCount,+ referencePower: [Double](repeating: 0, count: binCount),+ capturedPower: [Double](repeating: 0, count: binCount),+ crossReal: [Double](repeating: 0, count: binCount),+ crossImaginary: [Double](repeating: 0, count: binCount),+ supportedCells: [Int](repeating: 0, count: binCount)+ )++ for frame in 0..<frameCount {+ let start = frame * Self.hopLength+ var referenceFrame = [Float](repeating: 0, count: Self.frameLength)+ var capturedFrame = [Float](repeating: 0, count: Self.frameLength)+ vDSP_vmul(Array(reference[start..<start + Self.frameLength]), 1, window, 1, &referenceFrame, 1, vDSP_Length(Self.frameLength))+ vDSP_vmul(Array(captured[start..<start + Self.frameLength]), 1, window, 1, &capturedFrame, 1, vDSP_Length(Self.frameLength))++ guard let referenceSpectrum = Self.forwardFFT(&referenceFrame, setup: setup, log2n: log2n),+ let capturedSpectrum = Self.forwardFFT(&capturedFrame, setup: setup, log2n: log2n)+ else { return nil }++ for bin in 1..<binCount {+ let rr = Double(referenceSpectrum.real[bin]), ri = Double(referenceSpectrum.imaginary[bin])+ let cr = Double(capturedSpectrum.real[bin]), ci = Double(capturedSpectrum.imaginary[bin])+ let referencePower = rr * rr + ri * ri+ spectra.referencePower[bin] += referencePower+ spectra.capturedPower[bin] += cr * cr + ci * ci+ spectra.crossReal[bin] += cr * rr + ci * ri+ spectra.crossImaginary[bin] += ci * rr - cr * ri+ if referencePower > supportFloor { spectra.supportedCells[bin] += 1 }+ }+ }+ return spectra+ }++ private struct Spectrum {+ var real: [Float]+ var imaginary: [Float]+ }++ private static func forwardFFT(_ signal: inout [Float], setup: FFTSetup, log2n: vDSP_Length) -> Spectrum? {+ let halfCount = Self.frameLength / 2+ var real = [Float](repeating: 0, count: halfCount)+ var imaginary = [Float](repeating: 0, count: halfCount)+ var spectrum: Spectrum?+ real.withUnsafeMutableBufferPointer { realPointer in+ imaginary.withUnsafeMutableBufferPointer { imaginaryPointer in+ guard let realBase = realPointer.baseAddress, let imaginaryBase = imaginaryPointer.baseAddress else { return }+ var split = DSPSplitComplex(realp: realBase, imagp: imaginaryBase)+ signal.withUnsafeBufferPointer { signalPointer in+ guard let signalBase = signalPointer.baseAddress else { return }+ signalBase.withMemoryRebound(to: DSPComplex.self, capacity: halfCount) { complexPointer in+ vDSP_ctoz(complexPointer, 2, &split, 1, vDSP_Length(halfCount))+ }+ }+ vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(FFT_FORWARD))+ spectrum = Spectrum(real: Array(realPointer), imaginary: Array(imaginaryPointer))+ }+ }+ return spectrum+ }++ // MARK: Helpers++ private static func shift(_ signal: [Float], byFrames frames: Int, targetCount: Int) -> [Float] {+ var result = [Float](repeating: 0, count: targetCount)+ for index in 0..<targetCount {+ let source = index - frames+ guard source >= 0, source < signal.count else { continue }+ result[index] = signal[source]+ }+ return result+ }++ /// Span the reference actually excites, so the RMS ratio is not diluted by the stimulus's+ /// own leading and trailing silence.+ private static func excitedRange(_ signal: [Float]) -> Range<Int>? {+ let peak = MeetingVPIOAcousticStimulus.peak(signal)+ guard peak > 0 else { return nil }+ let threshold = peak * Self.excitationOnsetFraction+ guard let first = signal.firstIndex(where: { abs($0) > threshold }),+ let last = signal.lastIndex(where: { abs($0) > threshold }),+ last > first+ else { return nil }+ return first..<(last + 1)+ }++ private static func noiseFloorRMS(_ captured: [Float], prefixFrames: Int) -> Float? {+ guard prefixFrames >= Self.hopLength, prefixFrames <= captured.count else { return nil }+ return MeetingVPIOAcousticStimulus.rms(Array(captured[0..<prefixFrames]))+ }++ private static func decibels(numerator: Float, denominator: Float) -> Double? {+ guard numerator > 0, denominator > 0 else { return nil }+ return 20 * log10(Double(numerator) / Double(denominator))+ }++ /// Spread between the two halves of the analysis window. Widens whenever gain drifted+ /// (AGC) or the acoustic path moved mid-stimulus; it is a stability figure, not a CI.+ private static func splitHalfAttenuationSpreadDB(reference: [Float], captured: [Float]) -> Double? {+ let count = min(reference.count, captured.count)+ let half = count / 2+ guard half >= Self.hopLength else { return nil }+ guard let first = Self.decibels(+ numerator: MeetingVPIOAcousticStimulus.rms(Array(reference[0..<half])),+ denominator: MeetingVPIOAcousticStimulus.rms(Array(captured[0..<half]))+ ), let second = Self.decibels(+ numerator: MeetingVPIOAcousticStimulus.rms(Array(reference[half..<(2 * half)])),+ denominator: MeetingVPIOAcousticStimulus.rms(Array(captured[half..<(2 * half)]))+ ) else { return nil }+ return abs(first - second) / 2+ }+ }++#endifdiff --git a/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticStimulus.swift b/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticStimulus.swiftnew file mode 100644index 00000000..6302eef1--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticStimulus.swift@@ -0,0 +1,301 @@+#if DEBUG++ import Accelerate+ import Foundation++ // MARK: - Reason codes++ /// Machine-stable reasons. Every unusable number carries one; absence of a reason is never+ /// how a caller learns a metric was skipped.+ nonisolated enum MeetingVPIOAcousticReason: String, Codable, Sendable {+ case insufficientFrames+ case referenceBelowExcitationFloor+ case capturedBelowNoiseFloor+ case delayUnresolved+ case delayConfidenceBelowThreshold+ case splitHalfDelayDisagreement+ case emptyAnalysisWindow+ case bandSupportBelowThreshold+ case bandCoherenceBelowFloor+ case tooFewCoherentBands+ case captureWindowIncomplete+ case renderStartUnresolved+ case renderReadbackSilent+ case controlNotApplied+ case stimulusOutsideSafetyBounds+ case captureNotRunning+ case probeDisabled+ case outputRouteUnavailable+ case outputVolumeUnreadable+ case outputLevelUnsafe+ case renderTimingInvalid+ case playbackTimedOut+ case negativeDelay+ case captureGap+ case captureOverlap+ /// The sample values are present, but their PTS was synthesized while host time was+ /// unavailable. They are not valid evidence for a shared-clock delay measurement.+ case captureTimingSynthesized+ /// A synthesized timing run has not yet reached a valid host-time resynchronization.+ case captureTimingResyncRequired+ case probeCancelled+ }++ // MARK: - Stimulus++ /// Numeric description of the rendered stimulus. Records what was actually synthesized, not+ /// what was requested: `measuredPeak`/`measuredRMS` are computed from the emitted samples.+ nonisolated struct MeetingVPIOAcousticStimulusDescriptor: Codable, Equatable, Sendable {+ static let currentSchemaVersion = 1++ var schemaVersion: Int+ var seed: UInt64+ var sampleRate: Double+ var totalFrameCount: Int+ var leadingSilenceFrameCount: Int+ var chirpFrameCount: Int+ var gapFrameCount: Int+ var speechLikeFrameCount: Int+ var trailingSilenceFrameCount: Int+ var chirpStartHz: Double+ var chirpEndHz: Double+ var speechF0StartHz: Double+ var speechF0EndHz: Double+ var targetPeak: Double+ var measuredPeak: Double+ var measuredRMS: Double+ var safetyPeakLimit: Double+ var safetyMinimumRMS: Double+ var safetyMaximumRMS: Double+ var withinSafetyBounds: Bool+ }++ /// Deterministic diagnostic stimulus: a log sweep (alignment-rich, broadband) followed by a+ /// speech-like voiced/fricative segment (representative of what VPIO is tuned for). Identical+ /// bytes for identical `seed` and `sampleRate` so two runs are directly comparable.+ nonisolated enum MeetingVPIOAcousticStimulus {+ static let defaultSeed: UInt64 = 0x5F1D_A00D_C0DE_0001++ /// Bounded so a diagnostic can never drive the speaker into an uncomfortable level. The+ /// runner refuses to render a stimulus that lands outside these.+ static let targetPeak: Float = 0.25+ static let safetyPeakLimit: Float = 0.35+ static let safetyMinimumRMS: Float = 0.02+ static let safetyMaximumRMS: Float = 0.15+ /// Player gain applied on top of the already-bounded stimulus.+ static let maximumRenderVolume: Float = 0.6++ static let leadingSilenceSeconds = 0.20+ static let chirpSeconds = 1.00+ static let gapSeconds = 0.20+ static let speechLikeSeconds = 1.20+ static let trailingSilenceSeconds = 0.30++ static let chirpStartHz = 180.0+ static let chirpEndHz = 7_800.0+ static let speechF0StartHz = 115.0+ static let speechF0EndHz = 145.0++ nonisolated struct Generated: Equatable, Sendable {+ var samples: [Float]+ var descriptor: MeetingVPIOAcousticStimulusDescriptor++ var totalSeconds: Double {+ self.descriptor.sampleRate > 0+ ? Double(self.descriptor.totalFrameCount) / self.descriptor.sampleRate+ : 0+ }+ }++ static func make(sampleRate: Double, seed: UInt64 = MeetingVPIOAcousticStimulus.defaultSeed) -> Generated? {+ guard sampleRate >= 8_000, sampleRate <= 192_000 else { return nil }++ let lead = Self.frames(Self.leadingSilenceSeconds, sampleRate)+ let chirp = Self.frames(Self.chirpSeconds, sampleRate)+ let gap = Self.frames(Self.gapSeconds, sampleRate)+ let speech = Self.frames(Self.speechLikeSeconds, sampleRate)+ let trail = Self.frames(Self.trailingSilenceSeconds, sampleRate)++ var samples = [Float](repeating: 0, count: lead + chirp + gap + speech + trail)+ Self.writeLogSweep(into: &samples, at: lead, count: chirp, sampleRate: sampleRate)+ Self.writeSpeechLike(into: &samples, at: lead + chirp + gap, count: speech, sampleRate: sampleRate, seed: seed)++ var peak: Float = 0+ vDSP_maxmgv(samples, 1, &peak, vDSP_Length(samples.count))+ if peak > 0 {+ var scale = Self.targetPeak / peak+ vDSP_vsmul(samples, 1, &scale, &samples, 1, vDSP_Length(samples.count))+ }+ // Belt-and-braces against a future segment change: never emit past the safety ceiling.+ var lowerLimit = -Self.safetyPeakLimit+ var upperLimit = Self.safetyPeakLimit+ vDSP_vclip(samples, 1, &lowerLimit, &upperLimit, &samples, 1, vDSP_Length(samples.count))++ let measuredPeak = Self.peak(samples)+ let measuredRMS = Self.rms(samples)+ let withinBounds = measuredPeak <= Self.safetyPeakLimit+ && measuredRMS >= Self.safetyMinimumRMS+ && measuredRMS <= Self.safetyMaximumRMS++ return Generated(+ samples: samples,+ descriptor: MeetingVPIOAcousticStimulusDescriptor(+ schemaVersion: MeetingVPIOAcousticStimulusDescriptor.currentSchemaVersion,+ seed: seed,+ sampleRate: sampleRate,+ totalFrameCount: samples.count,+ leadingSilenceFrameCount: lead,+ chirpFrameCount: chirp,+ gapFrameCount: gap,+ speechLikeFrameCount: speech,+ trailingSilenceFrameCount: trail,+ chirpStartHz: Self.chirpStartHz,+ chirpEndHz: Self.chirpEndHz,+ speechF0StartHz: Self.speechF0StartHz,+ speechF0EndHz: Self.speechF0EndHz,+ targetPeak: Double(Self.targetPeak),+ measuredPeak: Double(measuredPeak),+ measuredRMS: Double(measuredRMS),+ safetyPeakLimit: Double(Self.safetyPeakLimit),+ safetyMinimumRMS: Double(Self.safetyMinimumRMS),+ safetyMaximumRMS: Double(Self.safetyMaximumRMS),+ withinSafetyBounds: withinBounds+ )+ )+ }++ private static func frames(_ seconds: Double, _ sampleRate: Double) -> Int {+ max(0, Int((seconds * sampleRate).rounded()))+ }++ /// Exponential sweep: constant energy per octave, so every analysis band gets excitation+ /// and the autocorrelation peak stays narrow enough to time-align on.+ private static func writeLogSweep(into samples: inout [Float], at offset: Int, count: Int, sampleRate: Double) {+ guard count > 1 else { return }+ let duration = Double(count) / sampleRate+ let ratio = Self.chirpEndHz / Self.chirpStartHz+ let logRatio = log(ratio)+ let fadeFrames = min(count / 4, Self.frames(0.010, sampleRate))+ for i in 0..<count {+ let t = Double(i) / sampleRate+ let phase = 2 * Double.pi * Self.chirpStartHz * duration / logRatio * (pow(ratio, t / duration) - 1)+ let value = sin(phase) * Self.fadeEnvelope(index: i, count: count, fadeFrames: fadeFrames)+ samples[offset + i] = Float(value)+ }+ }++ /// Voiced harmonic stack under fixed formants, a syllabic envelope, and two deterministic+ /// fricative bursts. Not speech — a signal whose spectrum and modulation resemble it.+ private static func writeSpeechLike(+ into samples: inout [Float],+ at offset: Int,+ count: Int,+ sampleRate: Double,+ seed: UInt64+ ) {+ guard count > 1 else { return }+ let duration = Double(count) / sampleRate+ let formants: [(center: Double, bandwidth: Double, gain: Double)] = [+ (500, 140, 1.0), (1_500, 220, 0.55), (2_600, 320, 0.30)+ ]+ let fadeFrames = min(count / 4, Self.frames(0.020, sampleRate))+ let nyquist = sampleRate / 2+ var generator = MeetingVPIOAcousticPRNG(seed: seed)+ var previousNoise = 0.0+ var phase = 0.0++ for i in 0..<count {+ let t = Double(i) / sampleRate+ let progress = t / duration+ let f0 = Self.speechF0StartHz + (Self.speechF0EndHz - Self.speechF0StartHz) * progress+ phase += 2 * Double.pi * f0 / sampleRate++ var voiced = 0.0+ var harmonic = 1+ while Double(harmonic) * f0 < min(nyquist, 8_000) {+ let frequency = Double(harmonic) * f0+ var weight = 0.0+ for formant in formants {+ let offsetHz = (frequency - formant.center) / formant.bandwidth+ weight += formant.gain * exp(-offsetHz * offsetHz)+ }+ // 1/h source tilt on top of the formant envelope.+ voiced += weight * sin(phase * Double(harmonic)) / Double(harmonic)+ harmonic += 1+ }++ // First-difference tilt turns flat PRNG noise into a fricative-shaped band.+ let white = generator.nextSymmetricUnit()+ let tilted = white - previousNoise+ previousNoise = white+ let fricative = tilted * Self.fricativeEnvelope(progress: progress)++ let syllabic = 0.35 + 0.65 * (0.5 - 0.5 * cos(2 * Double.pi * 3.5 * t))+ let value = (voiced * syllabic + fricative * 0.5)+ * Self.fadeEnvelope(index: i, count: count, fadeFrames: fadeFrames)+ samples[offset + i] = Float(value)+ }+ }++ /// Two fixed bursts; deterministic in `progress` so the descriptor fully predicts them.+ private static func fricativeEnvelope(progress: Double) -> Double {+ let bursts = [(start: 0.22, end: 0.30), (start: 0.62, end: 0.72)]+ for burst in bursts where progress >= burst.start && progress < burst.end {+ let local = (progress - burst.start) / (burst.end - burst.start)+ return 0.5 - 0.5 * cos(2 * Double.pi * local)+ }+ return 0+ }++ private static func fadeEnvelope(index: Int, count: Int, fadeFrames: Int) -> Double {+ guard fadeFrames > 0 else { return 1 }+ if index < fadeFrames {+ return 0.5 - 0.5 * cos(Double.pi * Double(index) / Double(fadeFrames))+ }+ let tail = count - 1 - index+ if tail < fadeFrames {+ return 0.5 - 0.5 * cos(Double.pi * Double(tail) / Double(fadeFrames))+ }+ return 1+ }++ static func rms(_ signal: [Float]) -> Float {+ guard !signal.isEmpty else { return 0 }+ var meanSquare: Float = 0+ vDSP_measqv(signal, 1, &meanSquare, vDSP_Length(signal.count))+ return sqrt(meanSquare)+ }++ static func peak(_ signal: [Float]) -> Float {+ guard !signal.isEmpty else { return 0 }+ var value: Float = 0+ vDSP_maxmgv(signal, 1, &value, vDSP_Length(signal.count))+ return value+ }+ }++ /// SplitMix64. Foundation's RNG is seeded per-process; a probe stimulus must be reproducible+ /// across processes and machines.+ nonisolated struct MeetingVPIOAcousticPRNG {+ private var state: UInt64++ init(seed: UInt64) {+ self.state = seed+ }++ mutating func next() -> UInt64 {+ self.state &+= 0x9E37_79B9_7F4A_7C15+ var z = self.state+ z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9+ z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB+ return z ^ (z >> 31)+ }++ /// Uniform in [-1, 1].+ mutating func nextSymmetricUnit() -> Double {+ let unit = Double(self.next() >> 11) * (1.0 / 9_007_199_254_740_992.0)+ return unit * 2 - 1+ }+ }++#endifdiff --git a/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticTrialB.swift b/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticTrialB.swiftnew file mode 100644index 00000000..30420913--- /dev/null+++ b/Sources/Fluid/Services/Meeting/MeetingVPIOAcousticTrialB.swift@@ -0,0 +1,565 @@+#if DEBUG++ import AVFoundation+ import CoreMedia+ import Darwin+ import Foundation+ import os++ // MARK: - Gate++ /// Trial B renders audio and mutates VPIO controls. It exists only in DEBUG and only when the+ /// explicit dual hardware-probe gate is present: `FLUIDVOICE_VPIO_ACOUSTIC=1` and a finite+ /// `FLUIDVOICE_MIC_PHASE1` duration in the bounded probe interval. No production path can+ /// reach it.+ nonisolated enum MeetingVPIOAcousticGate {+ static let maximumProbeMinutes = 0.25+ static let maximumSystemVolume = 0.6+ static let maximumCombinedPeak = 0.15++ static var isEnabled: Bool {+ self.isEnabled(environment: ProcessInfo.processInfo.environment)+ }++ static func isEnabled(environment: [String: String]) -> Bool {+ guard environment["FLUIDVOICE_VPIO_ACOUSTIC"] == "1",+ let minutes = environment["FLUIDVOICE_MIC_PHASE1"].flatMap(Double.init),+ minutes > 0,+ minutes <= Self.maximumProbeMinutes,+ minutes.isFinite+ else {+ return false+ }+ return true+ }+ }++ /// mach ticks to seconds, matching `MeetingMicrophonePTSClock`'s conversion so render times and+ /// capture presentation stamps live on one grid.+ nonisolated enum MeetingVPIOAcousticHostClock {+ private static let timebase: mach_timebase_info_data_t = {+ var info = mach_timebase_info_data_t()+ mach_timebase_info(&info)+ return info+ }()++ static func seconds(_ hostTime: UInt64) -> Double {+ guard self.timebase.denom > 0 else { return Double(hostTime) / 1_000_000_000 }+ return Double(hostTime) * Double(self.timebase.numer) / Double(self.timebase.denom) / 1_000_000_000+ }+ }++ // MARK: - Report records++ /// Evidence that the stimulus actually reached the engine's output path, independent of the+ /// microphone: without it a silent capture cannot be told apart from a silent render.+ nonisolated struct MeetingVPIOAcousticRenderConfirmation: Codable, Equatable, Sendable {+ var scheduledFrameCount: Int+ var sampleRate: Double+ var volume: Double+ var mixerSampleRate: Double+ var mixerChannelCount: Int+ var tapFrameCount: Int+ var tapRMS: Double+ var tapPeak: Double+ var tapInvalidHostTimeCount: Int+ /// Host-time seconds of the first mixer sample above the activity floor. This is after the+ /// stimulus's leading silence; Trial B converts it to stimulus sample-zero before windowing.+ var renderStartHostSeconds: Double?+ /// Independent estimate derived from the player's own sample clock; a large disagreement+ /// with `renderStartHostSeconds` invalidates the delay, it does not average with it.+ var playerDerivedStartHostSeconds: Double?+ var playerRenderedFrameCount: Int?+ var renderStartResolved: Bool+ var outputConnectionCount: Int+ var outputDeviceID: UInt32?+ var defaultOutputDeviceID: UInt32?+ var outputRouteConfirmed: Bool+ var systemVolume: Double?+ var outputVolumeReadable: Bool+ var combinedPeak: Double?+ var reasons: [MeetingVPIOAcousticReason]+ }++ /// Read-only safety preflight for a local render. It records only route/volume numbers and+ /// typed refusal reasons; it never changes the system volume or audio-device controls.+ nonisolated struct MeetingVPIOAcousticOutputPreflight: Codable, Equatable, Sendable {+ var outputConnectionCount: Int+ var outputDeviceID: UInt32?+ var defaultOutputDeviceID: UInt32?+ var outputRouteConfirmed: Bool+ var systemVolume: Double?+ var outputVolumeReadable: Bool+ var combinedPeak: Double?+ var reasons: [MeetingVPIOAcousticReason]++ var isSafe: Bool { self.reasons.isEmpty }+ }++ nonisolated struct MeetingVPIOAcousticControlState: Codable, Equatable, Sendable {+ var requestedBypass: Bool+ var appliedBypass: Bool?+ var requestedAGCEnabled: Bool+ var appliedAGCEnabled: Bool?+ var readbackBypass: Bool?+ var readbackAGCEnabled: Bool?+ var readbackBypassRawValue: UInt32?+ var readbackAGCRawValue: UInt32?+ var readbackBypassStatus: OSStatus?+ var readbackAGCStatus: OSStatus?+ var applied: Bool+ }++ nonisolated struct MeetingVPIOAcousticCaptureWindow: Codable, Equatable, Sendable {+ var startHostSeconds: Double+ var frameCount: Int+ var writtenFrameCount: Int+ var coverage: Double+ var contributingSegmentCount: Int+ var overlapFrameCount: Int+ var synthesizedFrameCount: Int+ var resyncedSegmentCount: Int+ }++ nonisolated struct MeetingVPIOAcousticRun: Codable, Equatable, Sendable {+ var variant: String+ var control: MeetingVPIOAcousticControlState+ var render: MeetingVPIOAcousticRenderConfirmation?+ var captureWindow: MeetingVPIOAcousticCaptureWindow?+ var measurement: MeetingVPIOAcousticMeasurement?+ var valid: Bool+ var reasons: [MeetingVPIOAcousticReason]+ }++ nonisolated struct MeetingVPIOAcousticTrialBReport: Codable, Equatable, Sendable {+ static let currentSchemaVersion = 1++ /// Carried in the sidecar so a reader cannot lift a number out of it as an AEC result.+ static let interpretationCaveats = [+ "Trial B renders known local PCM through this process's own VPIO output path; it does not route another application's audio and says nothing about whether Apple's echo reference receives other-process playback.",+ "attenuationDB compares an electrical reference against an acoustic capture and is uncalibrated. Only differences between variants measured back-to-back on one unchanged rig are readable, and even those are confounded by speaker level, room, and AGC.",+ "A single run establishes neither echo cancellation nor its absence. Treat every number as configuration and coupling evidence pending the paired ScreenCaptureKit topology comparison.",+ ]++ var schemaVersion: Int+ var stimulus: MeetingVPIOAcousticStimulusDescriptor+ var renderVolume: Double+ var preRollSeconds: Double+ var postRollSeconds: Double+ var settleSeconds: Double+ var runs: [MeetingVPIOAcousticRun]+ var caveats: [String]+ }++ // MARK: - Capture collector++ /// Holds the probe's already-emitted mono capture in memory for the duration of the trial so+ /// the metrics can be aligned to render time. Nothing here is written to disk, and only numbers+ /// leave it.+ final nonisolated class MeetingVPIOAcousticCaptureCollector: @unchecked Sendable {+ private nonisolated struct Segment: Sendable {+ var startSeconds: Double+ var samples: [Float]+ var synthesized: Bool+ var resynced: Bool+ }++ private nonisolated struct State: Sendable {+ var segments: [Segment] = []+ var frameCount = 0+ }++ /// 20 s at 48 kHz: enough for every variant window, bounded so a long probe cannot grow+ /// without limit.+ private nonisolated static let maximumRetainedFrames = 960_000++ private nonisolated let lock = OSAllocatedUnfairLock(initialState: State())++ nonisolated func ingest(_ sampleBuffer: CMSampleBuffer, synthesized: Bool = false, resynced: Bool = false) {+ guard let sample = MeetingLiveSampleCopy.copy(sampleBuffer),+ let channel = sample.buffer.floatChannelData?[0]+ else { return }+ let frameCount = Int(sample.buffer.frameLength)+ guard frameCount > 0, sample.pts.isNumeric else { return }+ let samples = Array(UnsafeBufferPointer(start: channel, count: frameCount))+ let segment = Segment(+ startSeconds: sample.pts.seconds,+ samples: samples,+ synthesized: synthesized,+ resynced: resynced+ )++ self.lock.withLock { state in+ state.segments.append(segment)+ state.frameCount += frameCount+ while state.frameCount > Self.maximumRetainedFrames, !state.segments.isEmpty {+ state.frameCount -= state.segments.removeFirst().samples.count+ }+ }+ }++ nonisolated func reset() {+ self.lock.withLock { $0 = State() }+ }++ /// Places every retained segment on a host-time grid starting at `startHostSeconds`. Gaps+ /// stay zero and are reported through `coverage`; a partially covered window is never+ /// silently treated as a complete one.+ nonisolated func window(+ startHostSeconds: Double,+ frameCount: Int,+ sampleRate: Double+ ) -> (samples: [Float], window: MeetingVPIOAcousticCaptureWindow) {+ let count = max(0, frameCount)+ var samples = [Float](repeating: 0, count: count)+ var filled = [Bool](repeating: false, count: count)+ var overlap = 0+ var contributing = 0+ var synthesizedFrameCount = 0+ var resyncedSegmentCount = 0+ let segments = self.lock.withLock { $0.segments }++ for segment in segments {+ let offset = Int(((segment.startSeconds - startHostSeconds) * sampleRate).rounded())+ let start = max(0, offset)+ let end = min(count, offset + segment.samples.count)+ guard start < end else { continue }+ contributing += 1+ if segment.resynced { resyncedSegmentCount += 1 }+ for index in start..<end {+ if filled[index] { overlap += 1 }+ let wasFilled = filled[index]+ filled[index] = true+ if segment.synthesized, !wasFilled { synthesizedFrameCount += 1 }+ samples[index] = segment.samples[index - offset]+ }+ }+ let written = filled.filter { $0 }.count++ return (+ samples,+ MeetingVPIOAcousticCaptureWindow(+ startHostSeconds: startHostSeconds,+ frameCount: count,+ writtenFrameCount: written,+ coverage: count > 0 ? Double(written) / Double(count) : 0,+ contributingSegmentCount: contributing,+ overlapFrameCount: overlap,+ synthesizedFrameCount: synthesizedFrameCount,+ resyncedSegmentCount: resyncedSegmentCount+ )+ )+ }+ }++ // MARK: - Runner++ /// Controlled acoustic Trial B: render a deterministic stimulus through the running VPIO+ /// engine's own output and measure what the VPIO input returns, once per control variant.+ nonisolated enum MeetingVPIOAcousticTrialB {+ struct Variant: Sendable {+ var name: String+ var bypassVoiceProcessing: Bool+ var agcEnabled: Bool+ }++ /// Baseline first, then one changed control at a time. Only properties with public setters+ /// are varied; `setVoiceProcessingEnabled` is never touched here (see the teardown note in+ /// `MeetingMicrophoneCapture.stop`).+ static let variants: [Variant] = [+ Variant(name: "vpioActive", bypassVoiceProcessing: false, agcEnabled: true),+ Variant(name: "vpioActiveNoAGC", bypassVoiceProcessing: false, agcEnabled: false),+ Variant(name: "vpioBypassed", bypassVoiceProcessing: true, agcEnabled: true),+ ]++ static let renderVolume: Float = 0.5+ static let settleSeconds = 0.35+ static let preRollSeconds = 0.30+ static let postRollSeconds = 0.60+ static let interVariantSeconds = 0.50+ /// The tap emits 100 ms aggregate buffers. Let a reset establish a complete pre-roll+ /// window, with one buffer-period margin for callback/flush scheduling, before playback.+ static let collectorWarmupSeconds = Self.preRollSeconds + 0.15++ private static func orderedReasons(_ reasons: [MeetingVPIOAcousticReason]) -> [MeetingVPIOAcousticReason] {+ var seen = Set<MeetingVPIOAcousticReason>()+ return reasons.filter { seen.insert($0).inserted }.sorted { $0.rawValue < $1.rawValue }+ }++ /// The mixer tap reports first activity, while the reference array begins at stimulus+ /// sample zero. Keep the conversion explicit so leading silence cannot shift the window+ /// and hide its trailing capture in an otherwise complete run.+ static func stimulusSampleZeroHostSeconds(+ renderActivityStartHostSeconds: Double,+ leadingSilenceFrameCount: Int,+ sampleRate: Double+ ) -> Double? {+ guard renderActivityStartHostSeconds.isFinite,+ leadingSilenceFrameCount >= 0,+ sampleRate.isFinite,+ sampleRate > 0+ else { return nil }+ return renderActivityStartHostSeconds - Double(leadingSilenceFrameCount) / sampleRate+ }++ static func isRequested() -> Bool { MeetingVPIOAcousticGate.isEnabled }++ static func run(+ capture: MeetingMicrophoneCapture,+ collector: MeetingVPIOAcousticCaptureCollector,+ sampleRate: Double = 48_000+ ) async -> MeetingVPIOAcousticTrialBReport? {+ guard MeetingVPIOAcousticGate.isEnabled else { return nil }+ guard let stimulus = MeetingVPIOAcousticStimulus.make(sampleRate: sampleRate) else { return nil }++ var runs: [MeetingVPIOAcousticRun] = []+ guard stimulus.descriptor.withinSafetyBounds else {+ // Refuse to render rather than emit an unbounded level.+ return MeetingVPIOAcousticTrialBReport(+ schemaVersion: MeetingVPIOAcousticTrialBReport.currentSchemaVersion,+ stimulus: stimulus.descriptor,+ renderVolume: Double(Self.renderVolume),+ preRollSeconds: Self.preRollSeconds,+ postRollSeconds: Self.postRollSeconds,+ settleSeconds: Self.settleSeconds,+ runs: [],+ caveats: MeetingVPIOAcousticTrialBReport.interpretationCaveats+ + ["Stimulus fell outside its RMS/peak safety bounds; nothing was rendered."]+ )+ }++ let preRunSnapshot = await capture.voiceProcessingProbeReadback()+ guard let preRunControl = preRunSnapshot.flatMap(Self.validatedControlState) else {+ return MeetingVPIOAcousticTrialBReport(+ schemaVersion: MeetingVPIOAcousticTrialBReport.currentSchemaVersion,+ stimulus: stimulus.descriptor,+ renderVolume: Double(Self.renderVolume),+ preRollSeconds: Self.preRollSeconds,+ postRollSeconds: Self.postRollSeconds,+ settleSeconds: Self.settleSeconds,+ runs: [],+ caveats: MeetingVPIOAcousticTrialBReport.interpretationCaveats+ + ["Pre-run VPIO control getter/raw read-backs were unavailable or inconsistent; no variant was rendered."]+ )+ }+ for variant in Self.variants {+ // Cancellation is cooperative: stop starting new variants, then restore the+ // production controls below before this task returns.+ guard !Task.isCancelled else { break }+ runs.append(+ await Self.runVariant(+ variant,+ capture: capture,+ collector: collector,+ stimulus: stimulus,+ sampleRate: sampleRate+ )+ )+ try? await Task.sleep(nanoseconds: UInt64(Self.interVariantSeconds * 1_000_000_000))+ }++ // Restore the actual pre-run controls. Never assume Apple's defaults and never touch+ // the VPIO enable bit here.+ _ = await capture.setVoiceProcessingBypassedForProbe(preRunControl.bypassed)+ _ = await capture.setVoiceProcessingAGCEnabledForProbe(preRunControl.agcEnabled)++ return MeetingVPIOAcousticTrialBReport(+ schemaVersion: MeetingVPIOAcousticTrialBReport.currentSchemaVersion,+ stimulus: stimulus.descriptor,+ renderVolume: Double(Self.renderVolume),+ preRollSeconds: Self.preRollSeconds,+ postRollSeconds: Self.postRollSeconds,+ settleSeconds: Self.settleSeconds,+ runs: runs,+ caveats: MeetingVPIOAcousticTrialBReport.interpretationCaveats+ )+ }++ private static func runVariant(+ _ variant: Variant,+ capture: MeetingMicrophoneCapture,+ collector: MeetingVPIOAcousticCaptureCollector,+ stimulus: MeetingVPIOAcousticStimulus.Generated,+ sampleRate: Double+ ) async -> MeetingVPIOAcousticRun {+ let appliedBypass = await capture.setVoiceProcessingBypassedForProbe(variant.bypassVoiceProcessing)+ let appliedAGC = await capture.setVoiceProcessingAGCEnabledForProbe(variant.agcEnabled)+ try? await Task.sleep(nanoseconds: UInt64(Self.settleSeconds * 1_000_000_000))++ let snapshot = await capture.voiceProcessingProbeReadback()+ var control = MeetingVPIOAcousticControlState(+ requestedBypass: variant.bypassVoiceProcessing,+ appliedBypass: appliedBypass,+ requestedAGCEnabled: variant.agcEnabled,+ appliedAGCEnabled: appliedAGC,+ readbackBypass: snapshot?.nodeVoiceProcessingBypassed,+ readbackAGCEnabled: snapshot?.nodeVoiceProcessingAGCEnabled,+ readbackBypassRawValue: snapshot?.bypassVoiceProcessing.value,+ readbackAGCRawValue: snapshot?.voiceProcessingAGCEnabled.value,+ readbackBypassStatus: snapshot?.bypassVoiceProcessing.status,+ readbackAGCStatus: snapshot?.voiceProcessingAGCEnabled.status,+ applied: false+ )+ control.applied = snapshot?.bypassVoiceProcessing.succeeded == true+ && snapshot?.voiceProcessingAGCEnabled.succeeded == true+ && snapshot?.bypassVoiceProcessing.value == UInt32(variant.bypassVoiceProcessing ? 1 : 0)+ && snapshot?.voiceProcessingAGCEnabled.value == UInt32(variant.agcEnabled ? 1 : 0)+ && control.readbackBypass == variant.bypassVoiceProcessing+ && control.readbackAGCEnabled == variant.agcEnabled++ var reasons: [MeetingVPIOAcousticReason] = []+ if !control.applied { reasons.append(.controlNotApplied) }+ if Task.isCancelled {+ reasons.append(.probeCancelled)+ return MeetingVPIOAcousticRun(+ variant: variant.name,+ control: control,+ render: nil,+ captureWindow: nil,+ measurement: nil,+ valid: false,+ reasons: Self.orderedReasons(reasons)+ )+ }++ collector.reset()+ try? await Task.sleep(nanoseconds: UInt64(Self.collectorWarmupSeconds * 1_000_000_000))+ guard !Task.isCancelled else {+ reasons.append(.probeCancelled)+ return MeetingVPIOAcousticRun(+ variant: variant.name,+ control: control,+ render: nil,+ captureWindow: nil,+ measurement: nil,+ valid: false,+ reasons: Self.orderedReasons(reasons)+ )+ }+ let render: MeetingVPIOAcousticRenderConfirmation+ do {+ render = try await capture.playVoiceProcessingProbePCM(+ stimulus.samples,+ sampleRate: sampleRate,+ volume: Self.renderVolume+ )+ } catch {+ switch error {+ case is CancellationError: reasons.append(.probeCancelled)+ case MeetingVoiceProcessingProbeError.captureNotRunning: reasons.append(.captureNotRunning)+ case MeetingVoiceProcessingProbeError.invalidPlaybackFormat: reasons.append(.insufficientFrames)+ case MeetingVoiceProcessingProbeError.playbackTimedOut: reasons.append(.playbackTimedOut)+ default: reasons.append(.probeDisabled)+ }+ return MeetingVPIOAcousticRun(+ variant: variant.name,+ control: control,+ render: nil,+ captureWindow: nil,+ measurement: nil,+ valid: false,+ reasons: Self.orderedReasons(reasons)+ )+ }+ try? await Task.sleep(nanoseconds: UInt64(Self.postRollSeconds * 1_000_000_000))++ reasons.append(contentsOf: render.reasons)+ guard let renderActivityStart = render.renderStartHostSeconds,+ let renderStart = Self.stimulusSampleZeroHostSeconds(+ renderActivityStartHostSeconds: renderActivityStart,+ leadingSilenceFrameCount: stimulus.descriptor.leadingSilenceFrameCount,+ sampleRate: sampleRate+ )+ else {+ reasons.append(.renderTimingInvalid)+ return MeetingVPIOAcousticRun(+ variant: variant.name,+ control: control,+ render: render,+ captureWindow: nil,+ measurement: nil,+ valid: false,+ reasons: Self.orderedReasons(reasons)+ )+ }++ let preRollFrames = Int((Self.preRollSeconds * sampleRate).rounded())+ let tailFrames = Int((Self.postRollSeconds * sampleRate).rounded())+ let windowFrames = preRollFrames + stimulus.samples.count + tailFrames+ let captured = collector.window(+ startHostSeconds: renderStart - Self.preRollSeconds,+ frameCount: windowFrames,+ sampleRate: sampleRate+ )+ // A Trial B delay is only meaningful on a complete, non-overlapping shared-clock+ // window. Never turn missing or doubly-written capture into zeroes and then score it.+ if captured.window.coverage < 1.0 {+ reasons.append(.captureGap)+ reasons.append(.captureWindowIncomplete)+ }+ if captured.window.overlapFrameCount > 0 {+ reasons.append(.captureOverlap)+ }+ if captured.window.synthesizedFrameCount > 0 {+ reasons.append(.captureTimingSynthesized)+ if captured.window.resyncedSegmentCount == 0 {+ reasons.append(.captureTimingResyncRequired)+ }+ }+ if captured.window.coverage < 1.0 || captured.window.overlapFrameCount > 0+ || captured.window.synthesizedFrameCount > 0+ {+ return MeetingVPIOAcousticRun(+ variant: variant.name,+ control: control,+ render: render,+ captureWindow: captured.window,+ measurement: nil,+ valid: false,+ reasons: Self.orderedReasons(reasons)+ )+ }++ // The reference is the stimulus as rendered — scaled by the player's gain — placed at+ // the same pre-roll offset as the capture window.+ var reference = [Float](repeating: 0, count: windowFrames)+ let volume = Float(render.volume)+ for index in 0..<stimulus.samples.count where preRollFrames + index < windowFrames {+ reference[preRollFrames + index] = stimulus.samples[index] * volume+ }++ let measurement = MeetingVPIOAcousticMetrics.measure(+ reference: reference,+ captured: captured.samples,+ sampleRate: sampleRate,+ silencePrefixFrames: preRollFrames + stimulus.descriptor.leadingSilenceFrameCount+ )++ return MeetingVPIOAcousticRun(+ variant: variant.name,+ control: control,+ render: render,+ captureWindow: captured.window,+ measurement: measurement,+ valid: measurement.valid && reasons.isEmpty,+ reasons: Self.orderedReasons(reasons + measurement.reasons)+ )+ }++ private static func validatedControlState(+ _ snapshot: MeetingVoiceProcessingProbeSnapshot+ ) -> (bypassed: Bool, agcEnabled: Bool)? {+ guard snapshot.bypassVoiceProcessing.succeeded,+ snapshot.voiceProcessingAGCEnabled.succeeded,+ let rawBypass = snapshot.bypassVoiceProcessing.value,+ let rawAGC = snapshot.voiceProcessingAGCEnabled.value,+ rawBypass <= 1,+ rawAGC <= 1,+ snapshot.nodeVoiceProcessingBypassed == (rawBypass == 1),+ snapshot.nodeVoiceProcessingAGCEnabled == (rawAGC == 1)+ else { return nil }+ return (rawBypass == 1, rawAGC == 1)+ }+ }++#endifdiff --git a/Sources/Fluid/fluidApp.swift b/Sources/Fluid/fluidApp.swiftindex b7524409..bd7f4358 100644--- a/Sources/Fluid/fluidApp.swift+++ b/Sources/Fluid/fluidApp.swift@@ -12,22 +12,24 @@ import SwiftUI@mainstruct FluidApp: App {@StateObject private var menuBarManager = MenuBarManager()- @StateObject private var appServices: AppServices@ObservedObject private var settings = SettingsStore.shared@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate- init() {- // Use the shared singleton instance- _appServices = StateObject(wrappedValue: AppServices.shared)- }-var body: some Scene {WindowGroup(id: "main") {- AdaptiveAppTheme(accent: self.settings.accentColor) {- ContentView()- .environmentObject(self.menuBarManager)- .environmentObject(self.appServices)- }+ #if DEBUG+ if MeetingExternalReferenceTrialAGate.autorunEnabled(environment: ProcessInfo.processInfo.environment)+ || MeetingSCKPairedDiagnosticGate.autorunEnabled()+ || ProcessInfo.processInfo.environment["FLUIDVOICE_MIC_PHASE1"] != nil+ || ProcessInfo.processInfo.environment["FLUIDVOICE_VPIO_ACOUSTIC"] == "1"+ {+ Color.clear+ } else {+ self.applicationContent+ }+ #else+ self.applicationContent+ #endif}.defaultSize(width: 1000, height: 700).commands {@@ -45,4 +47,14 @@ struct FluidApp: App {}}}++ private var applicationContent: some View {+ AdaptiveAppTheme(accent: self.settings.accentColor) {+ ContentView()+ .environmentObject(self.menuBarManager)+ // Resolve the singleton only when the normal application content branch is built.+ // The DEBUG C2 autorun branch returns Color.clear before this view is evaluated.+ .environmentObject(AppServices.shared)+ }+ }}diff --git a/Tests/FluidASRBaselineHost/FluidASRBaselineHost.entitlements b/Tests/FluidASRBaselineHost/FluidASRBaselineHost.entitlementsnew file mode 100644index 00000000..852fa1a4--- /dev/null+++ b/Tests/FluidASRBaselineHost/FluidASRBaselineHost.entitlements@@ -0,0 +1,8 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0">+<dict>+ <key>com.apple.security.app-sandbox</key>+ <true/>+</dict>+</plist>diff --git a/Tests/FluidASRBaselineHost/Inputs/placeholder.json b/Tests/FluidASRBaselineHost/Inputs/placeholder.jsonnew file mode 100644index 00000000..20de46bc--- /dev/null+++ b/Tests/FluidASRBaselineHost/Inputs/placeholder.json@@ -0,0 +1 @@+{"placeholder": true}diff --git a/Tests/FluidASRBaselineHost/MeetingReferenceSynchronizerReplayCLI.swift b/Tests/FluidASRBaselineHost/MeetingReferenceSynchronizerReplayCLI.swiftnew file mode 100644index 00000000..d6d84788--- /dev/null+++ b/Tests/FluidASRBaselineHost/MeetingReferenceSynchronizerReplayCLI.swift@@ -0,0 +1,365 @@+import Foundation+import CryptoKit++/// Codable, local-only input for the offline synchronizer replay command. The schema+/// deliberately contains numeric PCM fixture samples plus timing metadata and enum strings;+/// it never contains transcript text, embeddings, labels, or model artifacts. Raw fixture+/// samples are accepted as input but are never copied into command output.+nonisolated public struct MeetingSynchronizerReplayFixture: Codable, Equatable, Sendable {+ public let schemaVersion: Int+ public let configuration: MeetingSynchronizerReplayConfiguration+ public let microphone: [MeetingSynchronizerReplayMicrophoneFrame]+ public let reference: [MeetingSynchronizerReplayReferenceFrame]++ public init(+ schemaVersion: Int = 1,+ configuration: MeetingSynchronizerReplayConfiguration = .init(),+ microphone: [MeetingSynchronizerReplayMicrophoneFrame] = [],+ reference: [MeetingSynchronizerReplayReferenceFrame] = []+ ) {+ self.schemaVersion = schemaVersion+ self.configuration = configuration+ self.microphone = microphone+ self.reference = reference+ }+}++nonisolated public struct MeetingSynchronizerReplayConfiguration: Codable, Equatable, Sendable {+ public var analysisSampleRate: Double+ public var frameDuration: Double+ public var microphoneAlgorithmicDelaySeconds: Double+ public var referenceAlgorithmicDelaySeconds: Double+ public var referenceToMicrophoneOffsetSeconds: Double+ public var referenceClockDriftPPM: Double+ public var maximumClockDriftPPM: Double+ public var referenceScope: String+ public var referenceCompleteness: String+ public var codecPrimingSamples: Int+ public var codecRemainderSamples: Int+ public var editListOffsetSeconds: Double+ public var maximumInputFrameCount: Int+ public var maximumInputSampleCount: Int+ public var maximumOutputFrameCount: Int+ public var engineAvailable: Bool++ public init(+ analysisSampleRate: Double = 16_000,+ frameDuration: Double = 0.010,+ microphoneAlgorithmicDelaySeconds: Double = 0,+ referenceAlgorithmicDelaySeconds: Double = 0,+ referenceToMicrophoneOffsetSeconds: Double = 0,+ referenceClockDriftPPM: Double = 0,+ maximumClockDriftPPM: Double = 100,+ referenceScope: String = MeetingReferenceScope.selectedApplication.rawValue,+ referenceCompleteness: String = MeetingReferenceCompleteness.unobservable.rawValue,+ codecPrimingSamples: Int = 0,+ codecRemainderSamples: Int = 0,+ editListOffsetSeconds: Double = 0,+ maximumInputFrameCount: Int = 16_384,+ maximumInputSampleCount: Int = 2_000_000,+ maximumOutputFrameCount: Int = 6_000,+ engineAvailable: Bool = true+ ) {+ self.analysisSampleRate = analysisSampleRate+ self.frameDuration = frameDuration+ self.microphoneAlgorithmicDelaySeconds = microphoneAlgorithmicDelaySeconds+ self.referenceAlgorithmicDelaySeconds = referenceAlgorithmicDelaySeconds+ self.referenceToMicrophoneOffsetSeconds = referenceToMicrophoneOffsetSeconds+ self.referenceClockDriftPPM = referenceClockDriftPPM+ self.maximumClockDriftPPM = maximumClockDriftPPM+ self.referenceScope = referenceScope+ self.referenceCompleteness = referenceCompleteness+ self.codecPrimingSamples = codecPrimingSamples+ self.codecRemainderSamples = codecRemainderSamples+ self.editListOffsetSeconds = editListOffsetSeconds+ self.maximumInputFrameCount = maximumInputFrameCount+ self.maximumInputSampleCount = maximumInputSampleCount+ self.maximumOutputFrameCount = maximumOutputFrameCount+ self.engineAvailable = engineAvailable+ }++ fileprivate func makeSynchronizerConfiguration() -> MeetingReferenceSynchronizerConfiguration? {+ guard let scope = MeetingReferenceScope(rawValue: referenceScope),+ let completeness = MeetingReferenceCompleteness(rawValue: referenceCompleteness) else { return nil }+ return MeetingReferenceSynchronizerConfiguration(+ analysisSampleRate: analysisSampleRate, frameDuration: frameDuration,+ microphoneConverter: .init(algorithmicDelaySeconds: microphoneAlgorithmicDelaySeconds),+ referenceConverter: .init(algorithmicDelaySeconds: referenceAlgorithmicDelaySeconds),+ referenceToMicrophoneOffsetSeconds: referenceToMicrophoneOffsetSeconds,+ referenceClockDriftPPM: referenceClockDriftPPM, maximumClockDriftPPM: maximumClockDriftPPM,+ referenceScope: scope, referenceCompleteness: completeness,+ codecPrimingSamples: codecPrimingSamples, codecRemainderSamples: codecRemainderSamples,+ editListOffsetSeconds: editListOffsetSeconds,+ maximumInputFrameCount: maximumInputFrameCount,+ maximumInputSampleCount: maximumInputSampleCount,+ maximumOutputFrameCount: maximumOutputFrameCount, engineAvailable: engineAvailable)+ }+}++nonisolated public struct MeetingSynchronizerReplayMicrophoneFrame: Codable, Equatable, Sendable {+ public let sequenceNumber: Int+ public let sampleTime: Int64+ public let hostTime: Double?+ public let sampleRate: Double+ public let channelCount: Int+ public let samples: [Float]+ public let routeIdentifier: String+ public let discontinuity: Bool++ public init(sequenceNumber: Int, sampleTime: Int64, hostTime: Double?, sampleRate: Double,+ channelCount: Int = 1, samples: [Float], routeIdentifier: String = "default",+ discontinuity: Bool = false) {+ self.sequenceNumber = sequenceNumber; self.sampleTime = sampleTime; self.hostTime = hostTime+ self.sampleRate = sampleRate; self.channelCount = channelCount; self.samples = samples+ self.routeIdentifier = routeIdentifier; self.discontinuity = discontinuity+ }++ fileprivate func makeFrame() -> MeetingMicrophonePCMFrame {+ .init(sequenceNumber: sequenceNumber, sampleTime: sampleTime, hostTime: hostTime,+ sampleRate: sampleRate, channelCount: channelCount, samples: samples,+ routeIdentifier: routeIdentifier, discontinuity: discontinuity)+ }+}++nonisolated public struct MeetingSynchronizerReplayReferenceFrame: Codable, Equatable, Sendable {+ public let sequenceNumber: Int+ public let presentationTime: Double+ public let sampleRate: Double+ public let channelCount: Int+ public let samples: [Float]+ public let discontinuity: Bool++ public init(sequenceNumber: Int, presentationTime: Double, sampleRate: Double,+ channelCount: Int = 1, samples: [Float], discontinuity: Bool = false) {+ self.sequenceNumber = sequenceNumber; self.presentationTime = presentationTime+ self.sampleRate = sampleRate; self.channelCount = channelCount; self.samples = samples+ self.discontinuity = discontinuity+ }++ fileprivate func makeFrame() -> MeetingReferencePCMFrame {+ .init(sequenceNumber: sequenceNumber, presentationTime: presentationTime,+ sampleRate: sampleRate, channelCount: channelCount, samples: samples,+ discontinuity: discontinuity)+ }+}++nonisolated public struct MeetingSynchronizerReplayOutput: Codable, Equatable, Sendable {+ public let schemaVersion: Int+ public let replayIdentity: String+ public let failedOpen: Bool+ public let failureReason: String?+ public let frameCount: Int+ public let captureValidSampleCount: Int+ public let captureInvalidSampleCount: Int+ public let renderValidSampleCount: Int+ public let renderInvalidSampleCount: Int+ public let unknownReasonCounts: [String: Int]+ public let epochFrameCounts: [Int]+ public let sessionStartTime: Double?+ public let sessionEndTime: Double?+ public let microphoneSourceObservedMinimumTime: Double?+ public let microphoneSourceObservedMaximumTime: Double?+ public let referencePTSObservedMinimumTime: Double?+ public let referencePTSObservedMaximumTime: Double?+ public let diagnostics: MeetingSynchronizerReplayDiagnostics+}++nonisolated public struct MeetingSynchronizerReplayDiagnostics: Codable, Equatable, Sendable {+ public let inputFrameCount: Int+ public let droppedFrameCount: Int+ public let duplicateOrLateFrameCount: Int+ public let nonFiniteSampleCount: Int+ public let epochCount: Int+ public let synthesizedMicrophoneFrameCount: Int+ public let boundedResourceFailure: Bool+ public let referenceClockDriftPPM: Double?+}++/// Local-only command implementation. It is inert unless the caller supplies the explicit+/// `--offline-synchronizer-replay` argument or `FLUID_ASR_OFFLINE_REPLAY=1` environment gate.+nonisolated public enum MeetingReferenceSynchronizerReplayCLI {+ public static let argument = "--offline-synchronizer-replay"+ public static let environmentKey = "FLUID_ASR_OFFLINE_REPLAY"++ /// Replay is a fixture tool, not an arbitrary file decoder. Keep the largest+ /// accepted allocation comfortably bounded even if the caller raises config limits.+ public static let maximumInputBytes = 64 * 1024 * 1024+ private static let hardMaximumInputFrames = 50_000+ private static let hardMaximumInputSamples = 2_000_000+ private static let hardMaximumOutputFrames = 6_000+ private static let hardMaximumOutputSamples = 1_000_000+ private static let hardMaximumSourceSampleRate = 384_000.0+ private static let hardMaximumChannelCount = 32++ @discardableResult+ public static func run(arguments: [String], environment: [String: String] = ProcessInfo.processInfo.environment,+ inputData stdinData: Data, output: (Data) -> Void) -> Int32 {+ let gate = arguments.contains(argument) || environment[environmentKey] == "1"+ guard gate else { output(errorData(code: "offlineGateRequired")); return 2 }+ guard let data = inputData(arguments: arguments, stdin: stdinData),+ let fixture = decodeFixture(data), validate(fixture), let config = fixture.configuration.makeSynchronizerConfiguration() else {+ output(errorData(code: "invalidInput")); return 2+ }+ let result = MeetingReferenceSynchronizer(configuration: config).synchronize(+ microphone: fixture.microphone.map { $0.makeFrame() }, reference: fixture.reference.map { $0.makeFrame() })+ guard let canonical = try? canonicalData(fixture) else {+ output(errorData(code: "canonicalEncodingFailed")); return 3+ }+ let identity = "synchronizer-replay-v1-" + SHA256.hash(data: canonical).map { String(format: "%02x", $0) }.joined()+ let summary = makeOutput(result, identity: identity)+ guard let data = try? encoded(summary) else { output(errorData(code: "outputEncodingFailed")); return 3 }+ output(data); return result.failedOpen ? 1 : 0+ }++ /// Read at most `maximumInputBytes + 1` bytes so oversized stdin cannot be+ /// materialized without bound before JSON validation.+ public static func readBoundedStandardInput() -> Data? {+ readBounded(FileHandle.standardInput)+ }++ private static func inputData(arguments: [String], stdin: Data) -> Data? {+ guard let index = arguments.firstIndex(of: "--input") else {+ return stdin.count <= maximumInputBytes ? stdin : nil+ }+ guard arguments.indices.contains(index + 1), !arguments[index + 1].isEmpty else { return nil }+ guard let handle = try? FileHandle(forReadingFrom: URL(fileURLWithPath: arguments[index + 1])) else { return nil }+ defer { try? handle.close() }+ return readBounded(handle)+ }++ private static func readBounded(_ handle: FileHandle) -> Data? {+ var result = Data()+ while true {+ let remaining = maximumInputBytes - result.count+ guard remaining >= 0 else { return nil }+ let chunk: Data+ do {+ chunk = try handle.read(upToCount: min(64 * 1024, remaining + 1)) ?? Data()+ } catch {+ return nil+ }+ guard !chunk.isEmpty else { return result }+ result.append(chunk)+ if result.count > maximumInputBytes { return nil }+ }+ }++ private static func decodeFixture(_ data: Data) -> MeetingSynchronizerReplayFixture? {+ guard let object = try? JSONSerialization.jsonObject(with: data),+ let root = object as? [String: Any], strictKeys(root, ["schemaVersion", "configuration", "microphone", "reference"]) else { return nil }+ guard let configuration = root["configuration"] as? [String: Any],+ strictKeys(configuration, ["analysisSampleRate", "frameDuration", "microphoneAlgorithmicDelaySeconds", "referenceAlgorithmicDelaySeconds", "referenceToMicrophoneOffsetSeconds", "referenceClockDriftPPM", "maximumClockDriftPPM", "referenceScope", "referenceCompleteness", "codecPrimingSamples", "codecRemainderSamples", "editListOffsetSeconds", "maximumInputFrameCount", "maximumInputSampleCount", "maximumOutputFrameCount", "engineAvailable"]),+ let microphone = root["microphone"] as? [[String: Any]],+ let reference = root["reference"] as? [[String: Any]],+ microphone.allSatisfy({ strictKeys($0,+ required: ["sequenceNumber", "sampleTime", "sampleRate", "channelCount", "samples", "routeIdentifier", "discontinuity"],+ allowed: ["sequenceNumber", "sampleTime", "hostTime", "sampleRate", "channelCount", "samples", "routeIdentifier", "discontinuity"]) }),+ reference.allSatisfy({ strictKeys($0, ["sequenceNumber", "presentationTime", "sampleRate", "channelCount", "samples", "discontinuity"]) }) else { return nil }+ return try? JSONDecoder().decode(MeetingSynchronizerReplayFixture.self, from: data)+ }++ private static func validate(_ fixture: MeetingSynchronizerReplayFixture) -> Bool {+ guard fixture.schemaVersion == 1,+ finitePositive(fixture.configuration.analysisSampleRate), fixture.configuration.analysisSampleRate <= 384_000,+ finitePositive(fixture.configuration.frameDuration), fixture.configuration.frameDuration <= 10,+ finite(fixture.configuration.microphoneAlgorithmicDelaySeconds), finite(fixture.configuration.referenceAlgorithmicDelaySeconds),+ finite(fixture.configuration.referenceToMicrophoneOffsetSeconds), finite(fixture.configuration.editListOffsetSeconds),+ finite(fixture.configuration.referenceClockDriftPPM), finite(fixture.configuration.maximumClockDriftPPM),+ fixture.configuration.maximumClockDriftPPM >= 0,+ abs(fixture.configuration.referenceClockDriftPPM) <= fixture.configuration.maximumClockDriftPPM,+ fixture.configuration.codecPrimingSamples >= 0, fixture.configuration.codecRemainderSamples >= 0,+ fixture.configuration.maximumInputFrameCount > 0, fixture.configuration.maximumInputFrameCount <= hardMaximumInputFrames,+ fixture.configuration.maximumInputSampleCount > 0, fixture.configuration.maximumInputSampleCount <= hardMaximumInputSamples,+ fixture.configuration.maximumOutputFrameCount > 0, fixture.configuration.maximumOutputFrameCount <= hardMaximumOutputFrames,+ fixture.microphone.count <= fixture.configuration.maximumInputFrameCount,+ fixture.reference.count <= fixture.configuration.maximumInputFrameCount else { return false }+ let (inputFrames, inputFrameOverflow) = fixture.microphone.count+ .addingReportingOverflow(fixture.reference.count)+ guard !inputFrameOverflow, inputFrames <= fixture.configuration.maximumInputFrameCount else { return false }+ let frameProduct = fixture.configuration.analysisSampleRate * fixture.configuration.frameDuration+ let frameSize = frameProduct.rounded()+ guard frameProduct.isFinite, frameSize >= 1, frameSize <= Double(hardMaximumOutputSamples),+ frameSize < Double(Int.max) else { return false }+ let (outputSamples, outputOverflow) = fixture.configuration.maximumOutputFrameCount+ .multipliedReportingOverflow(by: Int(frameSize))+ guard !outputOverflow, outputSamples <= hardMaximumOutputSamples else { return false }+ var allSamples = 0+ for count in fixture.microphone.map(\.samples.count) + fixture.reference.map(\.samples.count) {+ let (sum, overflow) = allSamples.addingReportingOverflow(max(0, count))+ guard !overflow else { return false }+ allSamples = sum+ }+ guard allSamples <= fixture.configuration.maximumInputSampleCount else { return false }+ guard fixture.microphone.allSatisfy(validate), fixture.reference.allSatisfy(validate) else { return false }+ return true+ }++ private static func validate(_ frame: MeetingSynchronizerReplayMicrophoneFrame) -> Bool {+ !frame.samples.isEmpty && frame.channelCount > 0 && frame.channelCount <= hardMaximumChannelCount+ && finitePositive(frame.sampleRate) && frame.sampleRate <= hardMaximumSourceSampleRate+ && frame.samples.count % frame.channelCount == 0+ && frame.samples.allSatisfy(\.isFinite) && (frame.hostTime.map(finite) ?? true)+ }++ private static func validate(_ frame: MeetingSynchronizerReplayReferenceFrame) -> Bool {+ !frame.samples.isEmpty && frame.channelCount > 0 && frame.channelCount <= hardMaximumChannelCount+ && finitePositive(frame.sampleRate) && frame.sampleRate <= hardMaximumSourceSampleRate+ && finite(frame.presentationTime)+ && frame.samples.count % frame.channelCount == 0 && frame.samples.allSatisfy(\.isFinite)+ }++ private static func makeOutput(_ result: MeetingSynchronizationResult, identity: String) -> MeetingSynchronizerReplayOutput {+ let frames = result.frames+ let captureValid = frames.reduce(0) { $0 + $1.captureValidMask.filter { $0 }.count }+ let renderValid = frames.reduce(0) { $0 + $1.renderValidMask.filter { $0 }.count }+ var reasons: [String: Int] = [:]+ for frame in frames { for reason in frame.unknownReasons { reasons[reason.rawValue, default: 0] += 1 } }+ var epochs = [Int](repeating: 0, count: max(0, (frames.map(\.epochID).max() ?? -1) + 1))+ for frame in frames where frame.epochID >= 0 && frame.epochID < epochs.count { epochs[frame.epochID] += 1 }+ let sessions = frames.map { ($0.sessionStartTime, $0.sessionEndTime) }+ let micTimes = frames.compactMap { $0.timelines.microphoneSourceTime }+ let refTimes = frames.compactMap { $0.timelines.referencePTSTime }+ return .init(schemaVersion: 1, replayIdentity: identity, failedOpen: result.failedOpen,+ failureReason: result.failureReason?.rawValue, frameCount: frames.count,+ captureValidSampleCount: captureValid, captureInvalidSampleCount: frames.reduce(0) { $0 + $1.captureValidMask.filter { !$0 }.count },+ renderValidSampleCount: renderValid, renderInvalidSampleCount: frames.reduce(0) { $0 + $1.renderValidMask.filter { !$0 }.count },+ unknownReasonCounts: reasons, epochFrameCounts: epochs,+ sessionStartTime: sessions.map(\.0).min(), sessionEndTime: sessions.map(\.1).max(),+ microphoneSourceObservedMinimumTime: micTimes.min(), microphoneSourceObservedMaximumTime: micTimes.max(),+ referencePTSObservedMinimumTime: refTimes.min(), referencePTSObservedMaximumTime: refTimes.max(),+ diagnostics: .init(inputFrameCount: result.diagnostics.inputFrameCount,+ droppedFrameCount: result.diagnostics.droppedFrameCount,+ duplicateOrLateFrameCount: result.diagnostics.duplicateOrLateFrameCount,+ nonFiniteSampleCount: result.diagnostics.nonFiniteSampleCount,+ epochCount: result.diagnostics.epochCount,+ synthesizedMicrophoneFrameCount: result.diagnostics.synthesizedMicrophoneFrameCount,+ boundedResourceFailure: result.diagnostics.boundedResourceFailure,+ referenceClockDriftPPM: result.diagnostics.referenceClockDriftPPM))+ }++ private static func canonicalData(_ fixture: MeetingSynchronizerReplayFixture) throws -> Data {+ try encoded(fixture)+ }++ private static func encoded<T: Encodable>(_ value: T) throws -> Data {+ let encoder = JSONEncoder(); encoder.outputFormatting = [.sortedKeys]+ return try encoder.encode(value)+ }++ private static func errorData(code: String) -> Data {+ struct ErrorOutput: Encodable { let schemaVersion: Int; let errorCode: String }+ return (try? encoded(ErrorOutput(schemaVersion: 1, errorCode: code)))+ ?? Data("{\"schemaVersion\":1,\"errorCode\":\"outputEncodingFailed\"}".utf8)+ }++ private static func strictKeys(_ dictionary: [String: Any], _ allowed: Set<String>) -> Bool {+ Set(dictionary.keys) == allowed+ }++ private static func strictKeys(_ dictionary: [String: Any], required: Set<String>, allowed: Set<String>) -> Bool {+ Set(dictionary.keys).isSubset(of: allowed) && required.isSubset(of: Set(dictionary.keys))+ }++ private static func finite(_ value: Double) -> Bool { value.isFinite }+ private static func finitePositive(_ value: Double) -> Bool { value.isFinite && value > 0 }+}diff --git a/Tests/FluidASRBaselineHost/main.swift b/Tests/FluidASRBaselineHost/main.swiftnew file mode 100644index 00000000..c0e99adb--- /dev/null+++ b/Tests/FluidASRBaselineHost/main.swift@@ -0,0 +1,26 @@+import AppKit+import Foundation++if CommandLine.arguments.contains(MeetingReferenceSynchronizerReplayCLI.argument)+ || ProcessInfo.processInfo.environment[MeetingReferenceSynchronizerReplayCLI.environmentKey] == "1" {+ let status = MeetingReferenceSynchronizerReplayCLI.run(+ arguments: CommandLine.arguments,+ // A path-selected replay does not need stdin. Avoid waiting on an attached+ // terminal before the CLI opens the explicitly supplied local fixture.+ inputData: CommandLine.arguments.contains("--input")+ ? Data() : (MeetingReferenceSynchronizerReplayCLI.readBoundedStandardInput() ?? Data()),+ output: { data in+ FileHandle.standardOutput.write(data)+ FileHandle.standardOutput.write(Data([0x0A]))+ })+ exit(status)+}++assert(+ Bundle.main.bundleIdentifier == "com.FluidApp.ASRBaselineHost",+ "FluidASRBaselineHost must run as com.FluidApp.ASRBaselineHost"+)++let app = NSApplication.shared+app.setActivationPolicy(.prohibited)+app.run()diff --git a/Tests/FluidASRBaselineHost/stage-inputs.sh b/Tests/FluidASRBaselineHost/stage-inputs.shnew file mode 100755index 00000000..233f94bd--- /dev/null+++ b/Tests/FluidASRBaselineHost/stage-inputs.sh@@ -0,0 +1,21 @@+#!/bin/sh+set -eu++input_dir="${FLUID_ASR_BASELINE_INPUTS:-${SRCROOT}/Tests/FluidASRBaselineHost/Inputs}"+output_dir="${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/ASRBaselineInputs"++case "$input_dir" in+ /*) ;;+ *)+ echo "error: FLUID_ASR_BASELINE_INPUTS must be an absolute path, got: $input_dir" >&2+ exit 1+ ;;+esac++if [ ! -d "$input_dir" ]; then+ echo "error: ASR baseline inputs directory not found: $input_dir" >&2+ exit 1+fi++mkdir -p "$output_dir"+ditto "$input_dir" "$output_dir"diff --git a/Tests/FluidASRBaselineTests/ASRBaselineHostSmokeTests.swift b/Tests/FluidASRBaselineTests/ASRBaselineHostSmokeTests.swiftnew file mode 100644index 00000000..f6754f65--- /dev/null+++ b/Tests/FluidASRBaselineTests/ASRBaselineHostSmokeTests.swift@@ -0,0 +1,44 @@+import AppKit+import XCTest++@MainActor+final class ASRBaselineHostSmokeTests: XCTestCase {+ private static let hostBundleID = "com.FluidApp.ASRBaselineHost"++ func testHostSandboxSmoke() throws {+ XCTAssertEqual(Bundle.main.bundleIdentifier, Self.hostBundleID)+ XCTAssertNil(NSApplication.shared.delegate)+ XCTAssertTrue(NSApplication.shared.windows.isEmpty)++ let home = URL(fileURLWithPath: NSHomeDirectory()).standardized.resolvingSymlinksInPath()+ let containerSuffix = ["Library", "Containers", Self.hostBundleID, "Data"]+ let homeComponents = home.pathComponents+ XCTAssertTrue(+ homeComponents.count >= containerSuffix.count+ && Array(homeComponents.suffix(containerSuffix.count)) == containerSuffix,+ "home directory is not the host sandbox container: \(home.path)"+ )++ let appSupport = try FileManager.default+ .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)+ .standardized.resolvingSymlinksInPath()+ let appSupportComponents = appSupport.pathComponents+ XCTAssertTrue(+ appSupportComponents.count > homeComponents.count+ && Array(appSupportComponents.prefix(homeComponents.count)) == homeComponents,+ "Application Support is not inside the host sandbox container: \(appSupport.path)"+ )++ let metadata: [String: String] = [+ "applicationSupportDirectory": appSupport.path,+ "bundleIdentifier": Bundle.main.bundleIdentifier ?? "",+ "homeDirectory": home.path,+ "runBenchmark": ProcessInfo.processInfo.environment["FLUID_ASR_RUN_BENCHMARK"] ?? "0",+ ]+ let data = try JSONSerialization.data(withJSONObject: metadata, options: [.sortedKeys])+ let attachment = XCTAttachment(data: data, uniformTypeIdentifier: "public.json")+ attachment.name = "ASRBaselineHostSmoke"+ attachment.lifetime = .keepAlways+ add(attachment)+ }+}diff --git a/Tests/FluidASRBaselineTests/MeetingLiveP0DiagnosticsTests.swift b/Tests/FluidASRBaselineTests/MeetingLiveP0DiagnosticsTests.swiftnew file mode 100644index 00000000..8b42ceb0--- /dev/null+++ b/Tests/FluidASRBaselineTests/MeetingLiveP0DiagnosticsTests.swift@@ -0,0 +1,120 @@+@testable import FluidASRBaselineHost+import AVFoundation+import CoreMedia+import Foundation+import XCTest++#if arch(arm64)+final class MeetingLiveP0DiagnosticsTests: XCTestCase {+ private final class Clock: @unchecked Sendable {+ private let lock = NSLock()+ private var value: Double = 0+ func now() -> Double { lock.lock(); defer { lock.unlock() }; return value }+ func set(_ value: Double) { lock.lock(); defer { lock.unlock() }; self.value = value }+ }++ private actor Recognizer: MeetingLiveRecognizer {+ var resets = 0+ var processed = 0+ private let process: @Sendable () throws -> Void+ init(process: @escaping @Sendable () throws -> Void = {}) { self.process = process }+ func prepareModels() async throws { XCTFail("P0 deterministic tests must not load models") }+ func appendAudio(_ buffer: AVAudioPCMBuffer) async throws {}+ func processBufferedAudio() async throws { processed += 1; try process() }+ func reset() async { resets += 1 }+ func setEouCallback(_ callback: @escaping @Sendable (String) -> Void) async {}+ func setPartialTranscriptCallback(_ callback: @escaping @Sendable (String) -> Void) async {}+ }++ private func sample(at seconds: Double) -> MeetingLiveSampleCopy.Sample {+ let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 16_000,+ channels: 1, interleaved: false)!+ let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 1600)!+ buffer.frameLength = 1600+ buffer.floatChannelData![0].update(repeating: 0, count: 1600)+ return .init(buffer: buffer, pts: CMTime(seconds: seconds, preferredTimescale: 16_000),+ duration: CMTime(seconds: 0.1, preferredTimescale: 16_000))+ }++ func testExistingWatchdogCannotRotateBeforeFirstPartial() async {+ let clock = Clock()+ let recognizer = Recognizer()+ let engine = MeetingLiveTrackEngine(kind: .microphone, recognizer: recognizer,+ uptime: { clock.now() }, diagnosticsEnabled: true)+ for index in 0..<400 {+ clock.set(Double(index) / 10)+ await engine.diagnosticConsume(sample(at: clock.now()))+ }+ let snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.processCompletions, 400)+ XCTAssertEqual(snapshot.feedSeconds, 40, accuracy: 0.0001)+ XCTAssertEqual(snapshot.partialCallbacks, 0)+ XCTAssertEqual(snapshot.resetInvocations, 0, "Characterizes the P0 blind spot; not desired P1 behavior")+ let resets = await recognizer.resets+ XCTAssertEqual(resets, 0)+ }++ func testForcedBoundaryThen38SecondsWithoutPartialDoesNotRecover() async {+ let clock = Clock()+ let recognizer = Recognizer()+ let engine = MeetingLiveTrackEngine(kind: .microphone, recognizer: recognizer,+ uptime: { clock.now() }, diagnosticsEnabled: true)+ await engine.diagnosticConsume(sample(at: 0))+ await engine.diagnosticPartial("synthetic fixture")+ // Advance audio time while keeping wall-clock partial stall below its threshold, so+ // the hard duration boundary (not a quiet timeout) initiates the reset.+ clock.set(1)+ await engine.diagnosticConsume(sample(at: 21))+ var snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.resetInvocations, 1)+ for index in 1...380 {+ clock.set(1 + Double(index) / 10)+ await engine.diagnosticConsume(sample(at: 21 + Double(index) / 10))+ }+ snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.processCompletions, 382)+ XCTAssertEqual(snapshot.resetInvocations, 1, "Continued decode completion does not restart the watchdog")+ XCTAssertNil(snapshot.firstPartialAfterResetSeconds)+ await engine.diagnosticPartial("synthetic resumed fixture")+ snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.firstPartialAfterResetSeconds ?? -1, 38, accuracy: 0.0001)+ XCTAssertEqual(snapshot.wrapperGeneration, 1)+ }++ func testDiagnosticsCanBeExplicitlyDisabled() async {+ let engine = MeetingLiveTrackEngine(kind: .microphone, recognizer: Recognizer(), diagnosticsEnabled: false)+ await engine.diagnosticConsume(sample(at: 0))+ let snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.processStarts, 0)+ XCTAssertEqual(snapshot.convertedBuffers, 0)+ }++ func testProcessLatencyUsesInjectedClockWithoutSleeping() async {+ let clock = Clock()+ let recognizer = Recognizer(process: { clock.set(7) })+ let engine = MeetingLiveTrackEngine(kind: .microphone, recognizer: recognizer,+ uptime: { clock.now() }, diagnosticsEnabled: true)+ await engine.diagnosticConsume(sample(at: 0))+ let snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.appendStarts, 1)+ XCTAssertEqual(snapshot.appendCompletions, 1)+ XCTAssertEqual(snapshot.processStarts, 1)+ XCTAssertEqual(snapshot.processCompletions, 1)+ XCTAssertEqual(snapshot.maxProcessSeconds, 7)+ }++ func testThrownProcessIsNotCountedAsCompletion() async {+ enum FixtureError: Error { case injected }+ let recognizer = Recognizer(process: { throw FixtureError.injected })+ let engine = MeetingLiveTrackEngine(kind: .microphone, recognizer: recognizer,+ diagnosticsEnabled: true)+ await engine.diagnosticConsume(sample(at: 0))+ let snapshot = await engine.diagnosticSnapshotValue()+ XCTAssertEqual(snapshot.processStarts, 1)+ XCTAssertEqual(snapshot.processCompletions, 0)+ XCTAssertEqual(snapshot.appendOrProcessErrors, 1)+ XCTAssertEqual(snapshot.resetInvocations, 1)+ XCTAssertEqual(snapshot.recognizerResetReturned, 1, "Completion records return only, not decoder reset success")+ }+}+#endifdiff --git a/Tests/FluidASRBaselineTests/MeetingMicrophoneAdmissionContractTests.swift b/Tests/FluidASRBaselineTests/MeetingMicrophoneAdmissionContractTests.swiftnew file mode 100644index 00000000..a0e4c022--- /dev/null+++ b/Tests/FluidASRBaselineTests/MeetingMicrophoneAdmissionContractTests.swift@@ -0,0 +1,187 @@+@testable import FluidASRBaselineHost+import Foundation+import XCTest++final class MeetingMicrophoneAdmissionContractTests: XCTestCase {+ private typealias Evidence = MeetingMicrophoneEvidence+ private let sessionID = UUID()+ private let chunkID = UUID()++ private func identity(index: Int = 0, epoch: UInt64 = 0) -> Evidence.Identity {+ Evidence.Identity(sessionID: sessionID, chunkID: chunkID, turnIndex: index,+ captureEpoch: epoch, observation: .clusterLabel("0"))!+ }+ private var interval: Evidence.Interval { .init(start: 0, end: 2)! }++ func testMissingPlaybackContextIsNotInvalidTemporalMeasurement() {+ let temporal = Evidence.TemporalDuplicate(state: .duplicateSupported, identity: identity(), interval: interval,+ supportingWindows: 3, microphoneDelaySeconds: .measured(0.06), lagSpreadSeconds: .measured(0), coverage: .init(1)!)+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(evidence(playback: .unknown, temporal: .measured(temporal))).reason, .missingPlaybackContext)+ }++ func testTemporalBindingDeliberatelyRequiresTheExactEvidenceSnapshot() {+ let changedObservation = Evidence.Identity(sessionID: sessionID, chunkID: chunkID, turnIndex: 0,+ captureEpoch: 0, observation: .unavailable)!+ let scopes: [(Evidence.Identity, Evidence.Interval)] = [+ (changedObservation, interval), (identity(), .init(start: 0, end: 2.0.nextUp)!)+ ]+ for (scope, range) in scopes {+ let temporal = Evidence.TemporalDuplicate(state: .duplicateSupported, identity: scope, interval: range,+ supportingWindows: 3, microphoneDelaySeconds: .measured(0.06), lagSpreadSeconds: .measured(0), coverage: .init(1)!)+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(evidence(playback: .scoreablePlayback, temporal: .measured(temporal))).reason, .staleTemporalEvidence)+ }+ }++ private func evidence(+ signal: Evidence.Measurement<TurnEchoVerdict> = .unavailable(.notMeasured),+ textEcho: Evidence.Measurement<Bool> = .unavailable(.notMeasured),+ activity: Evidence.Measurement<Evidence.SpeechActivity> = .unavailable(.notMeasured),+ playback: Evidence.PlaybackContext = .unknown,+ temporal: Evidence.Measurement<Evidence.TemporalDuplicate> = .unavailable(.notMeasured),+ rms: Evidence.Measurement<Double> = .unavailable(.notMeasured)+ ) -> Evidence {+ Evidence(identity: identity(), interval: interval, playback: playback, signalVerdict: signal,+ textEcho: textEcho, speechActivity: activity, speechCoverage: .unavailable(.notMeasured),+ rms: rms, temporalDuplicate: temporal, embedding: .sharedClusterCentroid)+ }++ private func turn(signal: TurnEchoVerdict, textEcho: Bool, index: Int = 0) -> MeetingProcessingPipeline.StagedMicrophoneTurn {+ .init(chunkID: chunkID, index: index, clusterID: UUID(), clusterLabel: "0",+ diarizationObservationKey: identity(index: index).observationKey,+ start: 0, end: 2, text: "synthetic", overlapsRemote: true,+ isLikelyEcho: textEcho, echoScored: true, signalVerdict: signal)+ }++ func testLegacyTruthTableAndShadowInvocationCannotChangeTurns() {+ let cases: [(TurnEchoVerdict, Bool, Bool)] = [+ (.echo, false, true), (.echo, true, true),+ (.unknown, false, false), (.unknown, true, true),+ (.residualNotExplained, false, false), (.residualNotExplained, true, false)+ ]+ for (signal, textEcho, expected) in cases {+ let original = turn(signal: signal, textEcho: textEcho)+ let keysBefore = MeetingProcessingPipeline.trustedMicrophoneObservationKeys(from: [original])+ XCTAssertEqual(original.effectiveEcho, expected)+ let shadow = MeetingMicrophoneShadowPolicy.evaluate(evidence(signal: .measured(signal), textEcho: .measured(textEcho)))+ XCTAssertTrue(shadow.experimental)+ XCTAssertEqual(shadow.outcome, .uncertainCandidate)+ XCTAssertEqual(original.effectiveEcho, expected)+ XCTAssertEqual(MeetingProcessingPipeline.trustedMicrophoneObservationKeys(from: [original]), keysBefore)+ XCTAssertEqual(keysBefore.isEmpty, textEcho, "Stage A preserves even the known text-only profile gap")+ }+ }++ func testLegacyScorerRescueStillPrecedesCoverageGate() {+ let fractions = [Double](repeating: 0.1, count: 8) + [Double](repeating: .nan, count: 12)+ XCTAssertEqual(MeetingEchoSignalScorer.verdict(.init(fractions: fractions, hopSeconds: 0.1)), .residualNotExplained)+ }++ func testLegacyShortNaNGapStillBridgesLowRun() {+ let fractions = [Double](repeating: 0.1, count: 4) + [Double](repeating: .nan, count: 3)+ + [Double](repeating: 0.1, count: 4) + [Double](repeating: 0.9, count: 20)+ XCTAssertEqual(MeetingEchoSignalScorer.verdict(.init(fractions: fractions, hopSeconds: 0.1)), .residualNotExplained)+ }++ func testLegacyLongNaNGapStillBreaksLowRun() {+ let fractions = [Double](repeating: 0.1, count: 4) + [Double](repeating: .nan, count: 8)+ + [Double](repeating: 0.1, count: 4) + [Double](repeating: 0.9, count: 20)+ XCTAssertEqual(MeetingEchoSignalScorer.verdict(.init(fractions: fractions, hopSeconds: 0.1)), .echo)+ }++ func testLegacyTrivialRunDenialRequiresAllConditions() {+ func verdict(lowCount: Int, other: Double) -> TurnEchoVerdict {+ MeetingEchoSignalScorer.verdict(.init(fractions: [Double](repeating: 0.1, count: lowCount)+ + [Double](repeating: other, count: 100 - lowCount), hopSeconds: 0.1))+ }+ XCTAssertEqual(verdict(lowCount: 8, other: 0.9), .echo)+ XCTAssertEqual(verdict(lowCount: 8, other: 0.7), .residualNotExplained)+ XCTAssertEqual(verdict(lowCount: 16, other: 0.9), .residualNotExplained)+ XCTAssertEqual(MeetingEchoSignalScorer.verdict(.init(fractions: [], hopSeconds: 0.1)), .unknown)+ }++ func testShadowNamesRescueDisagreementWithoutChangingLegacy() {+ let input = evidence(signal: .measured(.residualNotExplained), textEcho: .measured(true))+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(input).reason, .legacyRescueDisagreement)+ XCTAssertFalse(input.legacySignalVerdict.legacyEffectiveEcho(textEcho: true))+ }++ func testActivityDoesNotProveNearEndSpeechEvenWithoutPlayback() {+ for playback in [Evidence.PlaybackContext.scoreablePlayback, .verifiedNoPlayback, .playbackExpectedButUnscoreable, .unknown] {+ let output = MeetingMicrophoneShadowPolicy.evaluate(evidence(activity: .measured(.detected), playback: playback))+ XCTAssertEqual(output.reason, .speechActivityIsNotNearEndProof)+ XCTAssertEqual(output.outcome, .uncertainCandidate)+ }+ }++ func testNegativeActivityAndZeroRMSDoNotProveSpeechAbsence() {+ let missing = evidence(activity: .measured(.notDetected))+ let zero = evidence(activity: .measured(.notDetected), rms: .measured(0))+ XCTAssertNotEqual(missing.rms, zero.rms)+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(zero).reason, .negativeActivityIsNotSpeechAbsenceProof)+ }++ func testUnknownReasonsRemainDistinctDespiteLegacyCollapse() {+ for reason in [Evidence.UnknownReason.noDelayLock, .referenceUnavailable, .unscoredCoverage, .scoredInconclusive] {+ let input = evidence(signal: .unavailable(reason))+ XCTAssertEqual(input.legacySignalVerdict, .unknown)+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(input).signalUnknownReason, reason)+ }+ }++ func testTurnIdentityPreservesSharedObservationAndEpochDistinctions() {+ XCTAssertNotEqual(identity(index: 0).turnKey, identity(index: 1).turnKey)+ XCTAssertEqual(identity(index: 0).observationKey, identity(index: 1).observationKey)+ XCTAssertNotEqual(identity(epoch: 0), identity(epoch: 1))+ XCTAssertNil(Evidence.Identity(sessionID: sessionID, chunkID: chunkID, turnIndex: -1, captureEpoch: 0, observation: .unavailable))+ let fallback = Evidence.Identity(sessionID: sessionID, chunkID: chunkID, turnIndex: 0, captureEpoch: 0, observation: .unavailable)!+ XCTAssertNil(fallback.observationKey)+ }++ func testRangesAndCoverageRejectNonfiniteAndInvalidValues() {+ XCTAssertNil(Evidence.Interval(start: .nan, end: 2))+ XCTAssertNil(Evidence.Interval(start: 0, end: .infinity))+ XCTAssertNil(Evidence.Interval(start: 2, end: 1))+ XCTAssertNil(Evidence.Interval(start: -1, end: 1))+ XCTAssertNil(Evidence.Fraction(.nan))+ XCTAssertNil(Evidence.Fraction(1.1))+ XCTAssertEqual(Evidence.Fraction(0)?.value, 0)+ }++ private struct FakeTemporalDetector: MeetingPlaybackDuplicateEvidenceSource {+ let result: Evidence.Measurement<Evidence.TemporalDuplicate>+ func evidence(for identity: Evidence.Identity, interval: Evidence.Interval) -> Evidence.Measurement<Evidence.TemporalDuplicate> { result }+ }++ func testFakeTemporalSourceIsOnlyEvidenceAndRequiresMatchingScope() {+ let stale = Evidence.TemporalDuplicate(state: .duplicateSupported, identity: identity(epoch: 1), interval: interval,+ supportingWindows: 6, microphoneDelaySeconds: .measured(0.06), lagSpreadSeconds: .measured(0.001), coverage: .init(1)!)+ let fake = FakeTemporalDetector(result: .measured(stale))+ let input = evidence(playback: .scoreablePlayback, temporal: fake.evidence(for: identity(), interval: interval))+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(input).reason, .staleTemporalEvidence)+ }++ func testMalformedTemporalSupportDoesNotBecomeDuplicateEvidence() {+ let invalid = Evidence.TemporalDuplicate(state: .duplicateSupported, identity: identity(), interval: interval,+ supportingWindows: 0, microphoneDelaySeconds: .measured(.nan), lagSpreadSeconds: .measured(0), coverage: .init(0)!)+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(evidence(temporal: .measured(invalid))).reason, .invalidTemporalEvidence)+ }++ func testSupportedDuplicateRemainsExperimentalAndCannotRescueMixedSpeech() {+ let temporal = Evidence.TemporalDuplicate(state: .duplicateSupported, identity: identity(), interval: interval,+ supportingWindows: 6, microphoneDelaySeconds: .measured(0.06), lagSpreadSeconds: .measured(0.001), coverage: .init(1)!)+ let input = evidence(activity: .measured(.detected), playback: .scoreablePlayback, temporal: .measured(temporal))+ let result = MeetingMicrophoneShadowPolicy.evaluate(input)+ XCTAssertEqual(result.reason, .playbackDuplicateCandidate)+ XCTAssertEqual(result.outcome, .uncertainCandidate)+ XCTAssertTrue(result.experimental)+ }++ func testSharedCentroidStillCannotProveSpeechInShadow() {+ let input = evidence(signal: .measured(.residualNotExplained))+ XCTAssertEqual(input.embedding, .sharedClusterCentroid)+ XCTAssertEqual(MeetingMicrophoneShadowPolicy.evaluate(input).reason, .insufficientEvidence)+ // Characterization of the known production gap, not an assertion of desired enforcement.+ let siblings = [turn(signal: .echo, textEcho: true), turn(signal: .unknown, textEcho: false, index: 1)]+ XCTAssertEqual(MeetingProcessingPipeline.trustedMicrophoneObservationKeys(from: siblings), [identity().observationKey!])+ }+}diff --git a/Tests/FluidASRBaselineTests/MeetingPlaybackDuplicateDetectorTests.swift b/Tests/FluidASRBaselineTests/MeetingPlaybackDuplicateDetectorTests.swiftnew file mode 100644index 00000000..089853f8--- /dev/null+++ b/Tests/FluidASRBaselineTests/MeetingPlaybackDuplicateDetectorTests.swift@@ -0,0 +1,172 @@+#if !TEMPORAL_STANDALONE+@testable import FluidASRBaselineHost+#endif+import Foundation+import XCTest++final class MeetingPlaybackDuplicateDetectorTests: XCTestCase {+ private typealias Detector = MeetingPlaybackDuplicateDetector+ private let session = UUID()+ private func noise(_ n: Int, seed: UInt64) -> [Float] {+ var state = seed+ return (0..<n).map { _ in+ state = state &* 6364136223846793005 &+ 1+ return Float(Double(state >> 32) / Double(UInt32.max) - 0.5)+ }+ }+ private func window(start: Double = 1, lag: Int = 120, epoch: UInt64 = 0,+ route: String = "test", mode: String = "echo") -> Detector.Window {+ var reference = noise(6_000, seed: UInt64(start * 100) + 1)+ if mode == "periodic" { reference = (0..<6_000).map { sin(Float($0) * 0.1) } }+ var mic = Array(reference[(1_000 - lag)..<(5_000 - lag)])+ if mode == "wrong" { mic = noise(4_000, seed: 92) }+ if mode == "silence" { mic = [Float](repeating: 0, count: 4_000) }+ if mode == "doubleTalk" {+ let local = noise(4_000, seed: 519)+ mic = zip(mic, local).map { $0 + 0.05 * $1 }+ }+ func pcm(_ samples: [Float], _ pts: Double) -> Detector.PCM {+ .init(start: pts, samples: samples, valid: [Bool](repeating: true, count: samples.count))+ }+ return .init(sessionID: session, epoch: epoch, routeID: route, start: start,+ microphone: pcm(mic, start), reference: pcm(reference, start - 0.5),+ mismatchedReference: pcm(noise(6_000, seed: 982), start - 0.5))+ }+ private func detector() -> Detector {+ var c = Detector.Configuration(); c.budgetSeconds = 10+ return Detector(configuration: c)+ }++ func testLagSignAndRepeatedSupport() {+ for lag in [-120, 0, 120] {+ var d = detector()+ XCTAssertEqual(d.process(window(lag: lag)).state, .duplicateCandidate)+ XCTAssertEqual(d.process(window(start: 3, lag: lag)).state, .duplicateCandidate)+ let result = d.process(window(start: 5, lag: lag))+ XCTAssertEqual(result.state, .duplicateSupported)+ XCTAssertEqual(result.microphoneDelaySeconds!, Double(lag) / 2_000, accuracy: 0.001)+ }+ }+ func testWrongAndPeriodicReferencesDoNotLock() {+ for mode in ["wrong", "periodic", "silence"] {+ var d = detector()+ for start in [1.0, 3, 5, 7] {+ XCTAssertNotEqual(d.process(window(start: start, mode: mode)).state, .duplicateSupported)+ }+ }+ }+ func testGapEpochAndRouteRequireFreshSupport() {+ for reset in ["gap", "epoch", "route"] {+ var d = detector()+ for start in [1.0, 3, 5] { _ = d.process(window(start: start)) }+ let result = d.process(window(start: reset == "gap" ? 9 : 7,+ epoch: reset == "epoch" ? 1 : 0,+ route: reset == "route" ? "new" : "test"))+ XCTAssertEqual(result.state, .duplicateCandidate)+ XCTAssertEqual(result.supportingWindows, 1)+ }+ }+ func testMissingAndMaskedReferenceResetImmediately() {+ for masked in [true, false] {+ var d = detector()+ for start in [1.0, 3, 5] { _ = d.process(window(start: start)) }+ let w = window(start: 7)+ var mask = w.reference!.valid; mask[200] = false+ let bad = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: 7,+ microphone: w.microphone,+ reference: masked ? .init(start: 6.5, samples: w.reference!.samples, valid: mask) : nil,+ mismatchedReference: w.mismatchedReference)+ XCTAssertEqual(d.process(bad).state, .insufficientEvidence)+ XCTAssertEqual(d.process(window(start: 9)).supportingWindows, 1)+ }+ }+ func testReleaseHasNoHoldAndDelayJumpRestarts() {+ var d = detector()+ for start in [1.0, 3, 5] { _ = d.process(window(start: start)) }+ let released = d.process(window(start: 7, mode: "wrong"))+ XCTAssertEqual(released.reason, .released)+ XCTAssertEqual(released.supportingWindows, 0)+ for start in [9.0, 11, 13] { _ = d.process(window(start: start)) }+ XCTAssertEqual(d.process(window(start: 15, lag: -120)).supportingWindows, 1)+ }+ func testQuietDoubleTalkRemainsAConfoundNotSpeechAbsenceProof() {+ var d = detector()+ for start in [1.0, 3, 5] { _ = d.process(window(start: start)) }+ XCTAssertEqual(d.process(window(start: 7, mode: "doubleTalk")).state, .duplicateSupported,+ "Temporal match survives quiet local audio: NEVER use this alone to suppress speech")+ }+ func testControlMatchRejectsEvenPerfectCorrelation() {+ var d = detector(); let w = window()+ let matchingControl = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: 1,+ microphone: w.microphone, reference: w.reference,+ mismatchedReference: w.reference)+ XCTAssertEqual(d.process(matchingControl).reason, .scoredInconclusive)+ }+ func testBudgetDisablesAfterBoundedFailures() {+ final class Clock: @unchecked Sendable {+ let lock = NSLock(); var value = 0.0+ func now() -> Double { lock.lock(); defer { lock.unlock() }; value += 1; return value }+ }+ let clock = Clock()+ var d = Detector(clock: { clock.now() })+ for start in [1.0, 3, 5] { XCTAssertEqual(d.process(window(start: start)).reason, .overBudget) }+ XCTAssertEqual(d.process(window(start: 7)).reason, .detectorUnavailable)+ let w = window()+ let newSession = Detector.Window(sessionID: UUID(), epoch: 0, routeID: "test", start: 1,+ microphone: w.microphone, reference: w.reference, mismatchedReference: w.mismatchedReference)+ XCTAssertEqual(d.process(newSession).reason, .overBudget)+ }+ func testMalformedSamplesAndTimestampsDoNotTrap() {+ let w = window()+ for pts in [Double.nan, .infinity, -1, 1e100] {+ var d = detector()+ let bad = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: pts,+ microphone: w.microphone, reference: w.reference,+ mismatchedReference: w.mismatchedReference)+ XCTAssertEqual(d.process(bad).state, .insufficientEvidence)+ }+ var d = detector(); var samples = w.microphone.samples; samples[4] = .nan+ let bad = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: 1,+ microphone: .init(start: 1, samples: samples, valid: w.microphone.valid),+ reference: w.reference, mismatchedReference: w.mismatchedReference)+ XCTAssertEqual(d.process(bad).reason, .unscoredCoverage)+ }++ func testReferenceSilenceIsInsufficientNotNegativeEvidence() {+ var d = detector(); let w = window()+ let silent = Detector.PCM(start: 0.5, samples: [Float](repeating: 0, count: 6_000),+ valid: [Bool](repeating: true, count: 6_000))+ let input = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: 1,+ microphone: w.microphone, reference: silent,+ mismatchedReference: w.mismatchedReference)+ XCTAssertEqual(d.process(input).reason, .insufficientEnergy)+ }++ func testReferencePTSMovesEstimatedLagInBothDirections() {+ for shift in [-0.02, 0.02] {+ var d = detector(); let w = window()+ let padded = [Float](repeating: 0, count: 200) + w.reference!.samples+ + [Float](repeating: 0, count: 200)+ let ref = Detector.PCM(start: 0.4 + shift, samples: padded,+ valid: [Bool](repeating: true, count: padded.count))+ let input = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: 1,+ microphone: w.microphone, reference: ref,+ mismatchedReference: w.mismatchedReference)+ XCTAssertEqual(d.process(input).microphoneDelaySeconds!, 0.06 - shift, accuracy: 0.001)+ }+ }++ func testExactLagSpreadBoundaryRetainsSupport() {+ var d = detector()+ _ = d.process(window(start: 1, lag: 200))+ _ = d.process(window(start: 3, lag: 224))+ XCTAssertEqual(d.process(window(start: 5, lag: 200)).state, .duplicateSupported)+ }++ func testUnavailableControlIsDistinctFromMissingPlayback() {+ var d = detector(); let w = window()+ let input = Detector.Window(sessionID: session, epoch: 0, routeID: "test", start: 1,+ microphone: w.microphone, reference: w.reference, mismatchedReference: nil)+ XCTAssertEqual(d.process(input).reason, .controlUnavailable)+ }+}diff --git a/Tests/FluidASRBaselineTests/MeetingReferenceSynchronizerReplayCLITests.swift b/Tests/FluidASRBaselineTests/MeetingReferenceSynchronizerReplayCLITests.swiftnew file mode 100644index 00000000..25cad95c--- /dev/null+++ b/Tests/FluidASRBaselineTests/MeetingReferenceSynchronizerReplayCLITests.swift@@ -0,0 +1,222 @@+@testable import FluidASRBaselineHost+import Foundation+import XCTest++@MainActor+final class MeetingReferenceSynchronizerReplayCLITests: XCTestCase {+ private func fixture(+ microphone: [MeetingSynchronizerReplayMicrophoneFrame] = [],+ reference: [MeetingSynchronizerReplayReferenceFrame] = [],+ configuration: MeetingSynchronizerReplayConfiguration = .init()+ ) -> MeetingSynchronizerReplayFixture {+ .init(configuration: configuration, microphone: microphone, reference: reference)+ }++ private func frameFixture(+ hostTime: Double? = 1,+ configuration: MeetingSynchronizerReplayConfiguration = .init()+ ) -> MeetingSynchronizerReplayFixture {+ fixture(+ microphone: (0..<3).map {+ let index = $0+ return .init(sequenceNumber: index, sampleTime: Int64(index * 160), hostTime: hostTime.map { base in base + Double(index) * 0.01 },+ sampleRate: 16_000, samples: [Float](repeating: Float(index + 1), count: 160))+ },+ reference: (0..<3).map {+ .init(sequenceNumber: $0, presentationTime: Double($0) * 0.01,+ sampleRate: 16_000, samples: [Float](repeating: 2, count: 160))+ },+ configuration: configuration+ )+ }++ private func encoded(_ fixture: MeetingSynchronizerReplayFixture) throws -> Data {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.sortedKeys]+ return try encoder.encode(fixture)+ }++ private func run(_ fixture: MeetingSynchronizerReplayFixture, environment: [String: String] = [:]) throws -> (Int32, Data) {+ var output = Data()+ let status = MeetingReferenceSynchronizerReplayCLI.run(+ arguments: ["replay", MeetingReferenceSynchronizerReplayCLI.argument],+ environment: environment, inputData: try encoded(fixture), output: { output = $0 })+ return (status, output)+ }++ func testReplayIsDeterministicAndOutputContainsNoPCM() throws {+ let fixture = frameFixture()+ let first = try run(fixture)+ let second = try run(fixture)+ XCTAssertEqual(first.0, 0)+ XCTAssertEqual(first.1, second.1)+ let object = try XCTUnwrap(JSONSerialization.jsonObject(with: first.1) as? [String: Any])+ XCTAssertNil(object["samples"])+ XCTAssertNil(object["transcript"])+ XCTAssertNotNil(object["replayIdentity"])+ XCTAssertNotNil(object["captureValidSampleCount"])+ }++ func testUnknownMasksGapsEpochsAndSynthTimingAreSummarized() throws {+ let mic = [+ MeetingSynchronizerReplayMicrophoneFrame(sequenceNumber: 0, sampleTime: 0, hostTime: nil,+ sampleRate: 16_000, samples: [Float](repeating: 1, count: 160)),+ MeetingSynchronizerReplayMicrophoneFrame(sequenceNumber: 2, sampleTime: 320, hostTime: 1.02,+ sampleRate: 16_000, samples: [Float](repeating: 1, count: 160))+ ]+ let (status, data) = try run(fixture(microphone: mic, reference: frameFixture().reference))+ XCTAssertEqual(status, 0)+ let object = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])+ let reasons = try XCTUnwrap(object["unknownReasonCounts"] as? [String: Int])+ XCTAssertGreaterThan(reasons[MeetingSynchronizerUnknownReason.captureGap.rawValue] ?? 0, 0)+ XCTAssertGreaterThan(reasons[MeetingSynchronizerUnknownReason.captureTimingSynthesized.rawValue] ?? 0, 0)+ XCTAssertGreaterThan((object["frameCount"] as? Int) ?? 0, 0)+ }++ func testMalformedSchemaAndNonFiniteInputFailClosedWithoutPathLeak() throws {+ var json = try XCTUnwrap(JSONSerialization.jsonObject(with: encoded(frameFixture())) as? [String: Any])+ json["unexpected"] = "rejected"+ let data = try JSONSerialization.data(withJSONObject: json)+ var output = Data()+ let status = MeetingReferenceSynchronizerReplayCLI.run(+ arguments: ["replay", MeetingReferenceSynchronizerReplayCLI.argument],+ inputData: data, output: { output = $0 })+ XCTAssertEqual(status, 2)+ let error = try XCTUnwrap(JSONSerialization.jsonObject(with: output) as? [String: Any])+ XCTAssertEqual(error["errorCode"] as? String, "invalidInput")+ XCTAssertNil(error["path"])+ }++ func testResourceBoundsAndOfflineGateAreExplicit() throws {+ var output = Data()+ let noGate = MeetingReferenceSynchronizerReplayCLI.run(+ arguments: ["replay"], inputData: try encoded(frameFixture()), output: { output = $0 })+ XCTAssertEqual(noGate, 2)+ XCTAssertEqual((try JSONSerialization.jsonObject(with: output) as? [String: Any])?["errorCode"] as? String, "offlineGateRequired")++ let limited = MeetingSynchronizerReplayConfiguration(maximumOutputFrameCount: 1)+ let (status, boundedOutput) = try run(frameFixture(configuration: limited))+ XCTAssertEqual(status, 1)+ let bounded = try XCTUnwrap(JSONSerialization.jsonObject(with: boundedOutput) as? [String: Any])+ XCTAssertEqual(bounded["failedOpen"] as? Bool, true)+ XCTAssertEqual((bounded["diagnostics"] as? [String: Any])?["boundedResourceFailure"] as? Bool, true)++ let totalFrameLimit = MeetingSynchronizerReplayConfiguration(maximumInputFrameCount: 3)+ let (totalFrameStatus, totalFrameData) = try run(frameFixture(configuration: totalFrameLimit))+ XCTAssertEqual(totalFrameStatus, 2)+ XCTAssertEqual((try JSONSerialization.jsonObject(with: totalFrameData) as? [String: Any])?["errorCode"] as? String, "invalidInput")+ }++ func testNilHostTimeAndEmptySamplesAreHandledStrictly() throws {+ let nilHost = frameFixture(hostTime: nil)+ let (nilStatus, nilData) = try run(nilHost)+ XCTAssertEqual(nilStatus, 0)+ XCTAssertNotNil(try JSONSerialization.jsonObject(with: nilData))++ let emptyMic = MeetingSynchronizerReplayMicrophoneFrame(+ sequenceNumber: 0, sampleTime: 0, hostTime: 1, sampleRate: 16_000, samples: [])+ let (emptyStatus, emptyData) = try run(fixture(microphone: [emptyMic], reference: frameFixture().reference))+ XCTAssertEqual(emptyStatus, 2)+ XCTAssertEqual((try JSONSerialization.jsonObject(with: emptyData) as? [String: Any])?["errorCode"] as? String, "invalidInput")+ }++ func testOversizedInputIsRejectedBeforeJSONDecode() throws {+ var output = Data()+ let oversized = Data(repeating: 0x20, count: MeetingReferenceSynchronizerReplayCLI.maximumInputBytes + 1)+ let status = MeetingReferenceSynchronizerReplayCLI.run(+ arguments: ["replay", MeetingReferenceSynchronizerReplayCLI.argument], inputData: oversized,+ output: { output = $0 })+ XCTAssertEqual(status, 2)+ XCTAssertEqual((try JSONSerialization.jsonObject(with: output) as? [String: Any])?["errorCode"] as? String, "invalidInput")+ }++ func testOutputAllocationBudgetHasAnExactFrameCap() throws {+ let accepted = MeetingSynchronizerReplayConfiguration(maximumOutputFrameCount: 6_000)+ let (acceptedStatus, _) = try run(frameFixture(configuration: accepted))+ XCTAssertEqual(acceptedStatus, 0)++ let tooManyFrames = MeetingSynchronizerReplayConfiguration(maximumOutputFrameCount: 6_001)+ let (frameStatus, frameData) = try run(frameFixture(configuration: tooManyFrames))+ XCTAssertEqual(frameStatus, 2)+ XCTAssertEqual((try JSONSerialization.jsonObject(with: frameData) as? [String: Any])?["errorCode"] as? String, "invalidInput")++ let tooLargeFrame = MeetingSynchronizerReplayConfiguration(+ analysisSampleRate: 384_000, frameDuration: 10, maximumOutputFrameCount: 1)+ let (sampleStatus, sampleData) = try run(frameFixture(configuration: tooLargeFrame))+ XCTAssertEqual(sampleStatus, 2)+ XCTAssertEqual((try JSONSerialization.jsonObject(with: sampleData) as? [String: Any])?["errorCode"] as? String, "invalidInput")+ }++ func testConfiguredFixtureFamiliesReplayWithoutClassificationClaims() throws {+ let sampleCount = 3 * 160+ let broadbandReference = (0..<sampleCount).map { index -> Float in+ index == 8 ? 1 : Float(sin(Double(index) * 0.071) * 0.4)+ }+ let impulseReference = (0..<sampleCount).map { $0 == 8 ? Float(1) : 0 }+ let nearSignal = (0..<sampleCount).map { index -> Float in+ Float(sin(Double(index) * 0.113 + 0.4) * 0.25)+ }+ func firEcho(_ reference: [Float]) -> [Float] {+ let taps: [(delay: Int, gain: Float)] = [(16, 0.7), (31, 0.2), (47, -0.1)]+ return (0..<reference.count).map { index in+ taps.reduce(Float(0)) { partial, tap in+ index >= tap.delay ? partial + tap.gain * reference[index - tap.delay] : partial+ }+ }+ }+ let impulseFIR = firEcho(impulseReference)+ let broadbandEcho = firEcho(broadbandReference)+ let silent = [Float](repeating: 0, count: sampleCount)+ let lowReference = broadbandReference.map { $0 * 0.00001 }+ let families: [(name: String, capture: [Float], reference: [Float])] = [+ ("impulseFIR", impulseFIR, impulseReference),+ ("echoOnly", broadbandEcho, broadbandReference),+ ("nearEndOnly", nearSignal, silent),+ ("doubleTalkLowEcho", zip(nearSignal, broadbandEcho).map { $0 + 0.25 * $1 }, broadbandReference),+ ("doubleTalkUnity", zip(nearSignal, broadbandEcho).map(+), broadbandReference),+ ("doubleTalkHighEcho", zip(nearSignal, broadbandEcho).map { $0 + 4 * $1 }, broadbandReference),+ ("nonlinearClippedEcho", broadbandEcho.map { max(-0.3, min(0.3, $0 * 3)) }, broadbandReference),+ ("lowExcitation", firEcho(lowReference), lowReference),+ ("silence", silent, silent),+ ("exactLocalRepetition", broadbandReference, broadbandReference),+ ]+ for family in families {+ let mic = (0..<3).map { index in+ let slice = Array(family.capture[(index * 160)..<((index + 1) * 160)])+ return MeetingSynchronizerReplayMicrophoneFrame(sequenceNumber: index, sampleTime: Int64(index * 160),+ hostTime: 1 + Double(index) * 0.01, sampleRate: 16_000, samples: slice)+ }+ let ref = (0..<3).map { index in+ let slice = Array(family.reference[(index * 160)..<((index + 1) * 160)])+ return MeetingSynchronizerReplayReferenceFrame(sequenceNumber: index, presentationTime: Double(index) * 0.01,+ sampleRate: 16_000, samples: slice)+ }+ let replay = fixture(microphone: mic, reference: ref)+ let (status, data) = try run(replay)+ XCTAssertEqual(status, 0)+ let object = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])+ XCTAssertEqual(object["frameCount"] as? Int, 3)++ // The family inputs are signal-shape controls only. Assert pairing and+ // coverage invariants directly; do not make an attribution/classification claim.+ let synchronizer = MeetingReferenceSynchronizer()+ let result = synchronizer.synchronize(+ microphone: mic.map {+ MeetingMicrophonePCMFrame(sequenceNumber: $0.sequenceNumber, sampleTime: $0.sampleTime,+ hostTime: $0.hostTime, sampleRate: $0.sampleRate, channelCount: $0.channelCount,+ samples: $0.samples, routeIdentifier: $0.routeIdentifier, discontinuity: $0.discontinuity)+ },+ reference: ref.map {+ MeetingReferencePCMFrame(sequenceNumber: $0.sequenceNumber, presentationTime: $0.presentationTime,+ sampleRate: $0.sampleRate, channelCount: $0.channelCount, samples: $0.samples,+ discontinuity: $0.discontinuity)+ })+ XCTAssertFalse(result.failedOpen)+ XCTAssertEqual(result.frames.count, 3)+ XCTAssertTrue(result.frames.allSatisfy { $0.captureValidMask.count == 160 && $0.renderValidMask.count == 160 })+ XCTAssertTrue(result.frames.allSatisfy { $0.captureValidMask.allSatisfy { $0 } && $0.renderValidMask.allSatisfy { $0 } })+ XCTAssertEqual(result.frames.flatMap(\.captureSamples), family.capture, family.name)+ XCTAssertEqual(result.frames.flatMap(\.renderSamples), family.reference, family.name)+ }+ }+}diff --git a/Tests/FluidASRBaselineTests/MeetingReliabilityNegativeScreenTests.swift b/Tests/FluidASRBaselineTests/MeetingReliabilityNegativeScreenTests.swiftnew file mode 100644index 00000000..c18f9c9d--- /dev/null+++ b/Tests/FluidASRBaselineTests/MeetingReliabilityNegativeScreenTests.swift@@ -0,0 +1,256 @@+import CoreML+import Foundation+import Security+import XCTest+#if arch(arm64)+import FluidAudio+#endif++/// Diagnostic, not an accuracy gate: nonempty ASR output is measured, never asserted away.+/// Synthetic input only; no fixture audio, devices, downloader, or production pipeline used.+@MainActor+final class MeetingReliabilityNegativeScreenTests: XCTestCase {+ func testSyntheticNoiseIsDeterministicBoundedAndNonzero() {+ let a = ReliabilitySyntheticInput.samples(count: 16_000, noise: true, seed: 42)+ XCTAssertEqual(a, ReliabilitySyntheticInput.samples(count: 16_000, noise: true, seed: 42))+ XCTAssertNotEqual(a, ReliabilitySyntheticInput.samples(count: 16_000, noise: true, seed: 43))+ XCTAssertTrue(a.allSatisfy { $0.isFinite && abs($0) <= 0.001 })+ XCTAssertGreaterThan(a.reduce(0.0) { $0 + Double($1 * $1) }, 0)+ XCTAssertEqual(ReliabilitySyntheticInput.samples(count: 100, noise: false, seed: 42), [Float](repeating: 0, count: 100))+ }++ func testOutputCountingAndExposureAccounting() {+ XCTAssertEqual(ReliabilitySyntheticInput.wordCount(" \n\t"), 0)+ XCTAssertEqual(ReliabilitySyntheticInput.wordCount("Okay, I\nget it."), 4)+ XCTAssertEqual(ReliabilitySyntheticInput.segmentLengths(totalSeconds: 61, segmentSeconds: 60), [60, 1])+ XCTAssertEqual(ReliabilitySyntheticInput.segmentLengths(totalSeconds: 600, segmentSeconds: 3).reduce(0, +), 600)+ }++ func testCanonicalArtifactRelativePathHandlesTemporaryDirectoryAliases() throws {+ let fm = FileManager.default+ let fixture = fm.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)+ try fm.createDirectory(at: fixture, withIntermediateDirectories: true)+ defer { try? fm.removeItem(at: fixture) }+ let root = fixture.appendingPathComponent("model", isDirectory: true)+ try fm.createDirectory(at: root, withIntermediateDirectories: true)+ let child = root.appendingPathComponent("weight.bin")+ try Data([0]).write(to: child)+ let alias = fixture.appendingPathComponent("alias", isDirectory: true)+ try fm.createSymbolicLink(at: alias, withDestinationURL: root)+ XCTAssertEqual(try ReliabilitySyntheticInput.relativeArtifactPath(child, under: alias), "weight.bin")+ XCTAssertThrowsError(try ReliabilitySyntheticInput.relativeArtifactPath(fixture.appendingPathComponent("outside"), under: root))+ }++ func testSyntheticSilenceAndLowNoiseNegativeScreen() async throws {+ guard ProcessInfo.processInfo.environment["FLUID_RELIABILITY_NEGATIVE_SCREEN"] == "1" else {+ throw XCTSkip("opt-in: FLUID_RELIABILITY_NEGATIVE_SCREEN=1; default 600 audio seconds per condition/mode")+ }+ #if arch(arm64)+ try validateHost()+ let env = ProcessInfo.processInfo.environment+ let seconds = try boundedInteger(env["FLUID_RELIABILITY_SECONDS"], default: 600, range: 60...600)+ let budgetSeconds = try boundedInteger(env["FLUID_RELIABILITY_WALL_SECONDS"], default: 1200, range: 60...1800)+ guard let root = BaselineInputLoader.inputsRoot() else { throw ScreenError.invalidInputs }+ let manifest = try BaselineInputLoader.loadManifest(from: root)+ let manifestHash = try BaselineHashing.sha256Hex(ofFileAt: root.appendingPathComponent("manifest.json"))+ var report = NegativeScreenReport(manifestSHA256: manifestHash, modelRepository: manifest.modelRepository,+ requestedAudioSecondsPerConditionMode: seconds, wallBudgetSeconds: budgetSeconds)+ defer { attach(report) }+ var activeManager: AsrManager?+ do {+ report.phase = "artifact_validation"+ let repository = try validateArtifacts(root: root, manifest: manifest)+ // No AsrModels.load/download API: even load(from:) can enter DownloadUtils.+ // Direct CoreML loading has no model retrieval fallback.+ let config = MLModelConfiguration()+ config.computeUnits = .cpuAndNeuralEngine+ let preprocessorConfig = MLModelConfiguration()+ preprocessorConfig.computeUnits = .cpuOnly+ report.phase = "vocabulary_validation"+ let vocabData = try Data(contentsOf: repository.appendingPathComponent("parakeet_vocab.json"))+ let rawVocab = try JSONDecoder().decode([String: String].self, from: vocabData)+ var vocabulary: [Int: String] = [:]+ for (key, value) in rawVocab {+ guard let index = Int(key), vocabulary[index] == nil else { throw ScreenError.invalidVocabulary }+ vocabulary[index] = value+ }+ guard !vocabulary.isEmpty else { throw ScreenError.invalidVocabulary }+ report.phase = "local_model_load"+ let loadStart = Date()+ let models = AsrModels(+ encoder: try MLModel(contentsOf: repository.appendingPathComponent("Encoder.mlmodelc"), configuration: config),+ preprocessor: try MLModel(contentsOf: repository.appendingPathComponent("Preprocessor.mlmodelc"), configuration: preprocessorConfig),+ decoder: try MLModel(contentsOf: repository.appendingPathComponent("Decoder.mlmodelc"), configuration: config),+ joint: try MLModel(contentsOf: repository.appendingPathComponent("JointDecision.mlmodelc"), configuration: config),+ configuration: config, vocabulary: vocabulary, version: .v2)+ let manager = AsrManager()+ activeManager = manager+ try await manager.initialize(models: models)+ report.modelLoadSeconds = Date().timeIntervalSince(loadStart)+ report.phase = "synthetic_inference"+ let clock = ContinuousClock()+ let deadline = clock.now + .seconds(budgetSeconds)+ // One manager, strictly sequential awaits. Pinned AsrManager.transcribe([Float])+ // awaits throwing resetDecoderState before returning (3fd6388, lines 524–548).+ // Noise is the same continuous seeded sequence under either segmentation mode.+ for noise in [false, true] {+ let allSamples = ReliabilitySyntheticInput.samples(count: seconds * 16_000, noise: noise, seed: 0x46565030)+ for segmentSeconds in [60, 3] {+ var offset = 0+ for length in ReliabilitySyntheticInput.segmentLengths(totalSeconds: seconds, segmentSeconds: segmentSeconds) {+ try Task.checkCancellation()+ guard clock.now < deadline else { throw ScreenError.wallBudgetExceeded }+ let count = length * 16_000+ let samples = Array(allSamples[offset..<(offset + count)])+ let start = clock.now+ let result = try await manager.transcribe(samples, source: .microphone)+ let elapsed = start.duration(to: clock.now).components+ report.runs.append(.init(condition: noise ? "synthetic_uniform_noise_peak_0.001" : "digital_silence",+ mode: segmentSeconds == 60 ? "whole_chunk_60s" : "short_turn_3s",+ startSample: offset, sampleCount: count,+ nonempty: !result.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,+ whitespaceWordCount: ReliabilitySyntheticInput.wordCount(result.text),+ latencySeconds: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18))+ offset += count+ }+ }+ }+ await manager.cleanup()+ activeManager = nil+ report.status = "complete"+ report.phase = "complete"+ } catch {+ if let activeManager { await activeManager.cleanup() }+ // Only error type, never ASR text or potentially sensitive file/error payload.+ report.status = "aborted_\(String(describing: type(of: error)))"+ if let safeError = error as? ScreenError { report.failureCode = String(describing: safeError) }+ throw error+ }+ #else+ throw XCTSkip("requires Apple Silicon")+ #endif+ }++ private func boundedInteger(_ raw: String?, default fallback: Int, range: ClosedRange<Int>) throws -> Int {+ guard let raw else { return fallback }+ guard let value = Int(raw), range.contains(value) else { throw ScreenError.invalidConfiguration }+ return value+ }++ private func validateHost() throws {+ guard Bundle.main.bundleIdentifier == "com.FluidApp.ASRBaselineHost",+ let task = SecTaskCreateFromSelf(nil),+ (SecTaskCopyValueForEntitlement(task, "com.apple.security.app-sandbox" as CFString, nil) as? Bool) == true+ else { throw ScreenError.unsafeHost }+ for key in ["com.apple.security.network.client", "com.apple.security.network.server", "com.apple.security.device.audio-input"] {+ guard (SecTaskCopyValueForEntitlement(task, key as CFString, nil) as? Bool) != true else { throw ScreenError.unsafeHost }+ }+ }++ private func validateArtifacts(root: URL, manifest: ASRBaselineManifest) throws -> URL {+ guard manifest.modelRepository == "parakeet-tdt-0.6b-v2-coreml", !manifest.artifacts.isEmpty else { throw ScreenError.invalidRepository }+ let paths = manifest.artifacts.map(\.path)+ guard Set(paths).count == paths.count else { throw ScreenError.duplicateArtifactPaths }+ let repository = try BaselinePathSafety.validatedURL(forRelativePath: manifest.modelRepository, under: root)+ for artifact in manifest.artifacts {+ guard artifact.path.hasPrefix(manifest.modelRepository + "/") else { throw ScreenError.invalidArtifactPath }+ let url = try BaselinePathSafety.validatedURL(forRelativePath: artifact.path, under: root)+ guard try BaselineHashing.sha256Hex(ofFileAt: url) == artifact.sha256 else { throw ScreenError.artifactHashMismatch }+ }+ // Reject unmanifested files too: a partial manifest cannot bless an unverified model.+ guard let enumerator = FileManager.default.enumerator(at: repository, includingPropertiesForKeys: [.isRegularFileKey, .isSymbolicLinkKey]) else { throw ScreenError.artifactEnumerationFailed }+ var actual = Set<String>()+ for case let url as URL in enumerator {+ let values = try url.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey])+ guard values.isSymbolicLink != true else { throw ScreenError.symbolicLinkArtifact }+ if values.isRegularFile == true {+ actual.insert(manifest.modelRepository + "/" + (try ReliabilitySyntheticInput.relativeArtifactPath(url, under: repository)))+ }+ }+ guard actual == Set(paths) else { throw ScreenError.artifactSetMismatch }+ return repository+ }++ private func attach(_ report: NegativeScreenReport) {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ guard let data = try? encoder.encode(report) else { XCTFail("negative screen report encoding failed"); return }+ let attachment = XCTAttachment(data: data, uniformTypeIdentifier: "public.json")+ attachment.name = "MeetingReliabilityNegativeScreenReport"+ attachment.lifetime = .keepAlways+ add(attachment)+ }+}++nonisolated private enum ScreenError: Error {+ case invalidInputs, invalidConfiguration, unsafeHost, wallBudgetExceeded+ case invalidRepository, duplicateArtifactPaths, invalidArtifactPath, artifactHashMismatch+ case artifactEnumerationFailed, symbolicLinkArtifact, artifactSetMismatch, invalidVocabulary+}++nonisolated enum ReliabilitySyntheticInput {+ static func relativeArtifactPath(_ child: URL, under root: URL) throws -> String {+ // FileManager may enumerate /private/tmp while resolvingSymlinksInPath returns+ // /tmp. Never trim using the character count of a differently normalized URL.+ let rootParts = BaselinePathSafety.canonical(root).pathComponents+ let childParts = BaselinePathSafety.canonical(child).pathComponents+ guard childParts.count > rootParts.count,+ Array(childParts.prefix(rootParts.count)) == rootParts else { throw ScreenError.invalidArtifactPath }+ return childParts.dropFirst(rootParts.count).joined(separator: "/")+ }+ static func samples(count: Int, noise: Bool, seed: UInt64) -> [Float] {+ guard noise else { return [Float](repeating: 0, count: count) }+ var state = seed+ return (0..<count).map { _ in+ state = state &* 6364136223846793005 &+ 1442695040888963407+ return (Float(state >> 40) / Float(0xFFFFFF) * 2 - 1) * 0.001+ }+ }+ static func wordCount(_ text: String) -> Int { text.split(whereSeparator: { $0.isWhitespace }).count }+ static func segmentLengths(totalSeconds: Int, segmentSeconds: Int) -> [Int] {+ precondition(totalSeconds >= 0 && segmentSeconds > 0)+ return stride(from: 0, to: totalSeconds, by: segmentSeconds).map { min(segmentSeconds, totalSeconds - $0) }+ }+}++nonisolated private struct NegativeScreenReport: Encodable {+ let schemaVersion = 1+ let kind = "synthetic_negative_asr_screen_not_meeting_pipeline"+ let verdict = "measured_only"+ let resetSemantics = "Pinned AsrManager array transcribe awaits resetDecoderState before successful return; errors abort the screen. Not the streaming EOU decoder."+ let timestamp = ISO8601DateFormatter().string(from: Date())+ let sampleRateHz = 16_000+ let channels = 1+ let seed = "0x46565030; LCG64; top24-bit uniform; peak=0.001 (~-60dBFS)"+ let modelProvenance = "Preexisting host-bundled ASRBaselineInputs; exact artifact-set SHA256 verification against manifest; manifest is integrity provenance, not independent publisher authentication"+ let networkPolicy = "Direct local CoreML constructors; no downloader; sandbox required and network/audio-input entitlements forbidden"+ let scope = "No recorded audio read. No VAD, AEC, diarization, speaker profiles, production provider, or live recognizer. Noise is not AEC residual. Whitespace-delimited word counts, not linguistic token accuracy. Repeated silence is deterministic exposure, not independent statistical trials. No emptiness assertion. Wall budget checked between calls; a non-cooperative CoreML call may exceed it."+ let notTested = ["real AEC residual", "quiet speech recall", "short spoken acknowledgments", "double talk", "playback leakage", "caption reset recovery"]+ let manifestSHA256: String+ let modelRepository: String+ let requestedAudioSecondsPerConditionMode: Int+ let wallBudgetSeconds: Int+ var modelLoadSeconds: Double?+ var status = "in_progress"+ var phase = "initialization"+ var failureCode: String?+ var runs: [Run] = []+ struct Run: Encodable {+ let condition: String+ let mode: String+ let startSample: Int+ let sampleCount: Int+ let nonempty: Bool+ let whitespaceWordCount: Int+ let latencySeconds: Double+ var audioSeconds: Double { Double(sampleCount) / 16_000 }+ enum CodingKeys: CodingKey { case condition, mode, startSample, sampleCount, nonempty, whitespaceWordCount, latencySeconds, audioSeconds }+ func encode(to encoder: Encoder) throws {+ var c = encoder.container(keyedBy: CodingKeys.self)+ try c.encode(condition, forKey: .condition); try c.encode(mode, forKey: .mode)+ try c.encode(startSample, forKey: .startSample); try c.encode(sampleCount, forKey: .sampleCount)+ try c.encode(nonempty, forKey: .nonempty); try c.encode(whitespaceWordCount, forKey: .whitespaceWordCount)+ try c.encode(latencySeconds, forKey: .latencySeconds); try c.encode(audioSeconds, forKey: .audioSeconds)+ }+ }+}diff --git a/Tests/FluidASRBaselineTests/MeetingSpeechActivityDetectorTests.swift b/Tests/FluidASRBaselineTests/MeetingSpeechActivityDetectorTests.swiftnew file mode 100644index 00000000..2a21afce--- /dev/null+++ b/Tests/FluidASRBaselineTests/MeetingSpeechActivityDetectorTests.swift@@ -0,0 +1,97 @@+@testable import FluidASRBaselineHost+import Foundation+import XCTest++final class MeetingSpeechActivityDetectorTests: XCTestCase {+ private struct Fake: MeetingSpeechActivityFrameModel {+ var p: Float+ var probabilityOnReset: Float = 0.1+ mutating func probability(samples: [Float], reset: Bool) throws -> Float {+ reset ? probabilityOnReset : p+ }+ }+ private let session = UUID()+ private let samples = [Float](repeating: 0.1, count: 4096)+ private let valid = [Bool](repeating: true, count: 4096)++ func testWarmupThenMeasuredActivity() {+ var detector = MeetingSpeechActivityDetector(model: Fake(p: 0.9))+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0, session: session, epoch: 0, route: "a").unknown, .contextWarmup)+ let result = detector.process(samples: samples, valid: valid, start: 0.256, session: session, epoch: 0, route: "a")+ XCTAssertEqual(result.active, true); XCTAssertEqual(result.probability, 0.9)+ }+ func testMasksAndGapRequireWarmupAgain() {+ var detector = MeetingSpeechActivityDetector(model: Fake(p: 0.9))+ _ = detector.process(samples: samples, valid: valid, start: 0, session: session, epoch: 0, route: "a")+ var masked = valid; masked[10] = false+ XCTAssertEqual(detector.process(samples: samples, valid: masked, start: 0.256, session: session, epoch: 0, route: "a").unknown, .unscoredCoverage)+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0.512, session: session, epoch: 0, route: "a").unknown, .contextWarmup)+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 2, session: session, epoch: 0, route: "a").unknown, .contextWarmup)+ }+ func testEpochAndRouteReset() {+ var detector = MeetingSpeechActivityDetector(model: Fake(p: 0.9))+ _ = detector.process(samples: samples, valid: valid, start: 0, session: session, epoch: 0, route: "a")+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0.256, session: session, epoch: 1, route: "a").unknown, .contextWarmup)+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0.512, session: session, epoch: 1, route: "b").unknown, .contextWarmup)+ }+ func testNaNAndPartialFramesAreUnavailable() {+ var detector = MeetingSpeechActivityDetector(model: Fake(p: .nan, probabilityOnReset: .nan))+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0, session: session, epoch: 0, route: "a").unknown, .modelFailure)+ XCTAssertEqual(detector.process(samples: [0.1], valid: [true], start: 0.256, session: session, epoch: 0, route: "a").unknown, .invalidInput)+ }+ func testCancellationBeforeAndAfterPrediction() {+ var detector = MeetingSpeechActivityDetector(model: Fake(p: 0.9))+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0, session: session, epoch: 0, route: "a", cancelled: { true }).unknown, .cancelled)+ var calls = 0+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0.256, session: session, epoch: 0, route: "a", cancelled: { calls += 1; return calls > 1 }).unknown, .cancelled)+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0.512, session: session, epoch: 0, route: "a").unknown, .contextWarmup)+ }+ func testBudgetFailuresDisableContribution() {+ var time = 0.0+ var detector = MeetingSpeechActivityDetector(model: Fake(p: 0.9), clock: { time += 1; return time })+ for i in 0..<3 {+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: Double(i) * 0.256, session: session, epoch: 0, route: "a").unknown, .overBudget)+ }+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0.768, session: session, epoch: 0, route: "a").unknown, .disabled)+ }+ func testCombinedPolicyNeverTreatsActivityAsNearEndOrAbsenceProof() {+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.reason(activity: true, reliableDuplicate: true), .mixedOrLeakedActivity)+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.reason(activity: true, reliableDuplicate: false), .activityWithoutReliablePlaybackMatch)+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.reason(activity: false, reliableDuplicate: true), .duplicateWithoutDetectedActivity)+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.reason(activity: false, reliableDuplicate: false), .negativeActivityIsNotAbsence)+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.reason(activity: nil, reliableDuplicate: true), .missingSpeechEvidence)+ }+ func testUnavailableModelDoesNotDownload() {+ XCTAssertThrowsError(try MeetingLocalSileroActivityModel(modelURL: URL(fileURLWithPath: "/private/tmp/fv-missing-vad-artifact.mlmodelc")))+ }++ func testNewSessionAndUnknownFramesResetFailureAllowance() {+ var time = 0.0+ var detector = MeetingSpeechActivityDetector(model: Fake(p: 0.9), clock: { time += 1; return time })+ for i in 0..<2 { _ = detector.process(samples: samples, valid: valid, start: Double(i) * 0.256, session: session, epoch: 0, route: "a") }+ var masked = valid; masked[0] = false+ _ = detector.process(samples: samples, valid: masked, start: 0.512, session: session, epoch: 0, route: "a")+ for i in 3..<6 { XCTAssertEqual(detector.process(samples: samples, valid: valid, start: Double(i) * 0.256, session: session, epoch: 0, route: "a").unknown, .overBudget) }+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 1.536, session: session, epoch: 0, route: "a").unknown, .disabled)+ XCTAssertEqual(detector.process(samples: samples, valid: valid, start: 0, session: UUID(), epoch: 0, route: "a").unknown, .overBudget)+ }++ private func temporal(_ start: Double, supported: Bool, epoch: UInt64 = 0) -> MeetingPlaybackDuplicateDetector.Result {+ .init(start: start, end: start + 2, epoch: epoch,+ state: supported ? .duplicateSupported : .observing,+ reason: supported ? .stableSupport : .scoredInconclusive,+ supportingWindows: supported ? 3 : 0, correlation: 0.9, controlCorrelation: 0.1,+ microphoneDelaySeconds: 0.06, lagSpreadSeconds: 0, elapsedSeconds: 0)+ }+ func testCrossGridFrameUsesBothWindowsWithoutDroppingCoverage() {+ let windows = [temporal(0, supported: true), temporal(2, supported: true)]+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.temporalContext(start: 1.792, epoch: 0, windows: windows), .supported)+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.temporalContext(start: 1.792, epoch: 0, windows: [windows[0]]), .unavailable)+ }+ func testCrossGridMixedOrStaleEvidenceCannotBorrowSupport() {+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.temporalContext(start: 1.792, epoch: 0,+ windows: [temporal(0, supported: true), temporal(2, supported: false)]), .mixed)+ XCTAssertEqual(MeetingSpeechPlaybackShadowPolicy.temporalContext(start: 1.792, epoch: 0,+ windows: [temporal(0, supported: true), temporal(2, supported: true, epoch: 1)]), .unavailable)+ }+}diff --git a/Tests/FluidASRBaselineTests/PairedASRBaselineTests.swift b/Tests/FluidASRBaselineTests/PairedASRBaselineTests.swiftnew file mode 100644index 00000000..7734b949--- /dev/null+++ b/Tests/FluidASRBaselineTests/PairedASRBaselineTests.swift@@ -0,0 +1,856 @@+#if arch(arm64)+import FluidAudio+#endif+@testable import FluidASRBaselineHost+import AppKit+import AVFoundation+import CryptoKit+import Darwin+import XCTest++/// Opt-in paired ASR provider baseline: legacy dictation config vs meeting config, both pinned+/// to Parakeet TDT v2 with every enhancement explicitly disabled. This is a synthetic+/// provider-only comparison of two equivalent explicit configurations — it is NOT the real+/// meeting diarization pipeline, NOT the S2-ownership memory gate, and NOT legacy default+/// (settings-driven) behavior. Runs only with FLUID_ASR_RUN_BENCHMARK=1 on arm64 under the+/// sandboxed `com.FluidApp.ASRBaselineHost` host; reports attach to the xcresult as+/// `ASRBaselineReport` (public.json, keepAlways) after every completed run and on abort.+@MainActor+final class PairedASRBaselineTests: XCTestCase {+ private static let pinnedFixtureSHA256 = "9fad783fa5e6398d095bf94883b5cf6ecb133c0c38480b790ce8c2903a818dde"+ private static let hostBundleIdentifier = "com.FluidApp.ASRBaselineHost"+ private static let pairRoundCount = 5+ private static let deallocBudget = Duration.seconds(2)++ private var partialReport: ASRBaselineReport?++ // MARK: - Always-on pure validation tests++ func testUnsafeRelativePathsAreRejected() {+ let root = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)+ let rejected = ["/etc/passwd", "../escape", "a/../b", "a//b", "./x", "~/x", "a\\b", ""]+ for path in rejected {+ XCTAssertThrowsError(+ try BaselinePathSafety.validatedURL(forRelativePath: path, under: root),+ "expected rejection for \(path.debugDescription)"+ )+ }+ XCTAssertNoThrow(+ try BaselinePathSafety.validatedURL(+ forRelativePath: "parakeet-tdt-0.6b-v2-coreml/vocabulary.json",+ under: root+ )+ )+ }++ func testSymlinkEscapeIsRejected() throws {+ let fileManager = FileManager.default+ let root = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)+ .appendingPathComponent("baseline-symlink-\(UUID().uuidString)", isDirectory: true)+ try fileManager.createDirectory(at: root, withIntermediateDirectories: true)+ defer { try? fileManager.removeItem(at: root) }+ try fileManager.createSymbolicLink(+ at: root.appendingPathComponent("link", isDirectory: true),+ withDestinationURL: root.deletingLastPathComponent()+ )+ XCTAssertThrowsError(+ try BaselinePathSafety.validatedURL(forRelativePath: "link/x.bin", under: root)+ )+ }++ func testBundledFixtureMatchesPinnedHashWhenAvailable() throws {+ guard let root = BaselineInputLoader.inputsRoot(),+ FileManager.default.fileExists(atPath: root.appendingPathComponent("manifest.json").path)+ else {+ throw XCTSkip("ASRBaselineInputs is not bundled with this host")+ }+ let canonicalRoot = BaselinePathSafety.canonical(root)+ let manifest = try BaselineInputLoader.loadManifest(from: canonicalRoot)+ let fixtureURL = try BaselinePathSafety.validatedURL(+ forRelativePath: manifest.fixture.path,+ under: canonicalRoot+ )+ let hash = try BaselineHashing.sha256Hex(ofFileAt: fixtureURL)+ XCTAssertEqual(hash, manifest.fixture.sha256)+ XCTAssertEqual(hash, Self.pinnedFixtureSHA256)+ }++ func testBundledFixtureDecodesToPinnedPCMWhenAvailable() throws {+ guard let root = BaselineInputLoader.inputsRoot(),+ FileManager.default.fileExists(atPath: root.appendingPathComponent("manifest.json").path)+ else {+ throw XCTSkip("ASRBaselineInputs is not bundled with this host")+ }+ let canonicalRoot = BaselinePathSafety.canonical(root)+ let manifest = try BaselineInputLoader.loadManifest(from: canonicalRoot)+ let fixtureURL = try BaselinePathSafety.validatedURL(+ forRelativePath: manifest.fixture.path,+ under: canonicalRoot+ )+ let hash = try BaselineHashing.sha256Hex(ofFileAt: fixtureURL)+ XCTAssertEqual(hash, Self.pinnedFixtureSHA256)+ let samples = try BaselineAudioLoader.loadMono16kInt16Samples(from: fixtureURL)+ XCTAssertEqual(samples.count, 1_200_000, "75s at 16kHz must decode to exactly 1.2M samples")+ XCTAssertTrue(samples.allSatisfy(\.isFinite))+ let energy = samples.reduce(0.0) { $0 + Double($1) * Double($1) }+ XCTAssertGreaterThan(energy, 0, "pinned fixture must contain non-silent audio")+ }++ // MARK: - Opt-in paired benchmark++ func testPairedFluidAudioProviderBaseline() async throws {+ #if arch(arm64)+ guard ProcessInfo.processInfo.environment["FLUID_ASR_RUN_BENCHMARK"] == "1" else {+ throw XCTSkip("opt-in: set FLUID_ASR_RUN_BENCHMARK=1 to run the paired ASR baseline")+ }+ do {+ try await self.runPairedBenchmark()+ } catch {+ // A cancel mid-await finishes the current non-cooperative await, unwinds the run+ // frame (releasing the provider), then lands here; attach whatever was recorded.+ if var report = self.partialReport {+ report.note = "aborted: \(error)"+ self.attachReport(report)+ } else {+ self.attachDiagnosticJSON(note: "aborted before report initialization: \(error)")+ }+ throw error+ }+ #else+ throw XCTSkip("paired ASR baseline requires Apple Silicon")+ #endif+ }++ #if arch(arm64)+ private func runPairedBenchmark() async throws {+ try self.validateSandboxEnvironment()+ let canonicalHome = BaselinePathSafety.canonical(+ URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)+ )+ let cacheDirectory = AsrModels.defaultCacheDirectory(for: .v2)+ guard BaselinePathSafety.isStrictDescendant(cacheDirectory, of: canonicalHome) else {+ throw BaselineInputError.sandboxEnvironment("model cache directory escapes container home")+ }++ guard let inputsRoot = BaselineInputLoader.inputsRoot(),+ FileManager.default.fileExists(atPath: inputsRoot.path)+ else {+ throw BaselineInputError.missingInputsRoot+ }+ let canonicalInputsRoot = BaselinePathSafety.canonical(inputsRoot)+ let manifest = try BaselineInputLoader.loadManifest(from: canonicalInputsRoot)+ // Attach diagnostics from this point on; earlier failures fall back to a minimal note.+ self.partialReport = ASRBaselineReport(+ schemaVersion: 1,+ testKind: "paired-fluidaudio-provider-baseline",+ syntheticScope: "synthetic provider-only run of two explicit equivalent configs; not meeting diarization, not the S2-ownership memory gate, not legacy default settings behavior",+ policy: "legacy=modelOverride:parakeetTDTv2,wordBoosting:off,unifiedFinal:off,pronunciation:off,customDictionary:[]; meeting=MeetingFinalProcessingConfiguration() fixed all-disabled policy",+ timestamp: ISO8601DateFormatter().string(from: Date()),+ modelRepository: manifest.modelRepository,+ fixtureSHA256: manifest.fixture.sha256,+ fixtureSampleRateHz: manifest.fixture.sampleRateHz,+ fixtureChannels: manifest.fixture.channels,+ fixtureDurationSeconds: manifest.fixture.durationSeconds,+ runs: [],+ comparisons: [],+ note: "in-progress"+ )+ guard manifest.modelRepository == "parakeet-tdt-0.6b-v2-coreml",+ manifest.modelRepository == cacheDirectory.lastPathComponent+ else {+ throw BaselineInputError.invalidManifest("modelRepository=\(manifest.modelRepository)")+ }+ guard !manifest.artifacts.isEmpty else {+ throw BaselineInputError.invalidManifest("artifacts empty")+ }+ let artifactPaths = manifest.artifacts.map(\.path)+ guard Set(artifactPaths).count == artifactPaths.count else {+ throw BaselineInputError.invalidManifest("duplicate artifact paths")+ }+ guard manifest.artifacts.allSatisfy({ Self.isHex64($0.sha256) }) else {+ throw BaselineInputError.invalidManifest("artifact sha256 must be 64 hex characters")+ }++ // Fixture hash first: nothing is copied or prepared before the pinned audio is proven.+ let fixtureURL = try BaselinePathSafety.validatedURL(+ forRelativePath: manifest.fixture.path,+ under: canonicalInputsRoot+ )+ let fixtureHash = try BaselineHashing.sha256Hex(ofFileAt: fixtureURL)+ guard fixtureHash == manifest.fixture.sha256, fixtureHash == Self.pinnedFixtureSHA256 else {+ throw BaselineInputError.hashMismatch("fixture \(manifest.fixture.path)")+ }+ guard manifest.fixture.sampleRateHz == 16_000,+ manifest.fixture.channels == 1,+ abs(manifest.fixture.durationSeconds - 75.0) < 0.5+ else {+ throw BaselineInputError.fixtureMetadataMismatch(+ "rate=\(manifest.fixture.sampleRateHz) channels=\(manifest.fixture.channels) duration=\(manifest.fixture.durationSeconds)"+ )+ }++ var sourceArtifacts: [(relativePath: String, url: URL, sha256: String)] = []+ for artifact in manifest.artifacts {+ let firstComponent = artifact.path.split(separator: "/").first.map(String.init)+ guard firstComponent == manifest.modelRepository else {+ throw BaselineInputError.unsafeRelativePath(artifact.path)+ }+ let url = try BaselinePathSafety.validatedURL(forRelativePath: artifact.path, under: canonicalInputsRoot)+ sourceArtifacts.append((artifact.path, url, artifact.sha256))+ }+ for artifact in sourceArtifacts {+ let hash = try BaselineHashing.sha256Hex(ofFileAt: artifact.url)+ guard hash == artifact.sha256 else {+ throw BaselineInputError.hashMismatch("source \(artifact.relativePath)")+ }+ }++ // Verify-or-copy into the container cache. A mismatched existing copy fails loudly and+ // is never deleted by this test; extra files in an existing copy are tolerated.+ let fileManager = FileManager.default+ let modelsRoot = cacheDirectory.deletingLastPathComponent()+ if !fileManager.fileExists(atPath: cacheDirectory.path) {+ try fileManager.createDirectory(at: modelsRoot, withIntermediateDirectories: true)+ let bundledRepository = canonicalInputsRoot.appendingPathComponent(+ manifest.modelRepository,+ isDirectory: true+ )+ guard BaselinePathSafety.isStrictDescendant(bundledRepository, of: canonicalInputsRoot) else {+ throw BaselineInputError.unsafeRelativePath(manifest.modelRepository)+ }+ try fileManager.copyItem(at: bundledRepository, to: cacheDirectory)+ }+ for artifact in manifest.artifacts {+ let cachedURL = try BaselinePathSafety.validatedURL(forRelativePath: artifact.path, under: modelsRoot)+ guard fileManager.fileExists(atPath: cachedURL.path) else {+ throw BaselineInputError.hashMismatch("cache missing \(artifact.path)")+ }+ let hash = try BaselineHashing.sha256Hex(ofFileAt: cachedURL)+ guard hash == artifact.sha256 else {+ throw BaselineInputError.hashMismatch("cache \(artifact.path)")+ }+ }++ let samples = try BaselineAudioLoader.loadMono16kInt16Samples(from: fixtureURL)+ let decodedSeconds = Double(samples.count) / 16_000+ guard !samples.isEmpty, abs(decodedSeconds - manifest.fixture.durationSeconds) < 1.0 else {+ throw BaselineInputError.fixtureMetadataMismatch("decoded \(decodedSeconds)s")+ }++ for round in 0..<Self.pairRoundCount {+ try Task.checkCancellation()+ let order: [BaselineArm] = round.isMultiple(of: 2) ? [.legacy, .meeting] : [.meeting, .legacy]+ var roundRecords: [BaselineArm: ASRBaselineReport.RunRecord] = [:]+ for (orderIndex, arm) in order.enumerated() {+ var (record, box) = try await self.runArm(+ arm,+ orderIndex: orderIndex,+ round: round,+ samples: samples+ )+ // Providers must never overlap: gate the next arm on this provider's dealloc.+ record.providerDeallocated = try await self.waitForProviderDealloc(box, budget: Self.deallocBudget)+ record.memoryAfterDeallocWait = try TaskMemorySnapshot.capture()+ self.partialReport?.runs.append(record)+ if let report = self.partialReport {+ self.attachReport(report)+ }+ print(Self.progressLine(for: record))+ guard record.providerDeallocated else {+ throw BaselineInputError.providerDidNotDeallocate+ }+ roundRecords[arm] = record+ try Task.checkCancellation()+ }+ if let legacy = roundRecords[.legacy], let meeting = roundRecords[.meeting] {+ self.partialReport?.comparisons.append(+ Self.compare(legacy: legacy, meeting: meeting, round: round)+ )+ }+ }+ self.partialReport?.note = "complete"+ if let report = self.partialReport {+ self.attachReport(report)+ }+ }++ /// Run-scoped: the provider is created, used, and left to die inside this frame; only plain+ /// value copies (no provider or ASR-library references) cross the return boundary.+ private func runArm(+ _ arm: BaselineArm,+ orderIndex: Int,+ round: Int,+ samples: [Float]+ ) async throws -> (ASRBaselineReport.RunRecord, WeakProviderBox) {+ try Task.checkCancellation()+ let box = WeakProviderBox()+ let sampler = MemoryPeakSampler()+ let before = try TaskMemorySnapshot.capture()+ try sampler.start()+ defer { sampler.stopAndJoin() }++ let provider: FluidAudioProvider+ switch arm {+ case .legacy:+ provider = FluidAudioProvider(+ modelOverride: .parakeetTDTv2,+ configureWordBoosting: false,+ enhancementOptions: FluidAudioProviderEnhancementOptions(+ experimentalUnifiedFinalEnabled: false,+ pronunciationMatchingEnabled: false,+ customDictionaryEntries: []+ )+ )+ case .meeting:+ provider = try FluidAudioProvider(meetingConfiguration: MeetingFinalProcessingConfiguration())+ }+ box.provider = provider+ guard provider.modelsExistOnDisk() else {+ throw BaselineInputError.modelsMissingOnDisk+ }++ let clock = ContinuousClock()+ let prepareStart = clock.now+ try await provider.prepare()+ try Task.checkCancellation()+ let inferenceStart = clock.now+ let outcome = try await provider.transcribeWithWordTimings(samples)+ try Task.checkCancellation()+ let inferenceEnd = clock.now++ guard sampler.stopAndJoin() else {+ throw BaselineInputError.memorySamplerDidNotStop+ }+ try sampler.checkCaptureError()+ let after = try TaskMemorySnapshot.capture()++ let transcript = outcome.result.text+ let words = outcome.words+ guard !transcript.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, !words.isEmpty else {+ throw BaselineInputError.emptyTranscription(arm.rawValue)+ }+ var previousStart = -Double.infinity+ for word in words {+ guard word.start.isFinite, word.end.isFinite,+ word.start <= word.end, word.start >= previousStart+ else {+ throw BaselineInputError.invalidWordTimings(arm.rawValue)+ }+ previousStart = word.start+ }++ let record = ASRBaselineReport.RunRecord(+ round: round,+ orderInRound: orderIndex,+ arm: arm.rawValue,+ prepareSeconds: Self.seconds(from: prepareStart, to: inferenceStart),+ inferenceSeconds: Self.seconds(from: inferenceStart, to: inferenceEnd),+ memoryBefore: before,+ memoryPeakDuringRun: sampler.peak,+ memoryAfterReturn: after,+ memoryAfterDeallocWait: nil,+ providerDeallocated: false,+ transcript: transcript,+ words: words.map {+ ASRBaselineReport.WordRecord(text: $0.text, start: $0.start, end: $0.end)+ }+ )+ return (record, box)+ }+ #endif++ private func validateSandboxEnvironment() throws {+ guard Bundle.main.bundleIdentifier == Self.hostBundleIdentifier else {+ throw BaselineInputError.sandboxEnvironment(+ "host bundle id=\(Bundle.main.bundleIdentifier ?? "nil")"+ )+ }+ if let application = NSApp {+ guard application.delegate == nil else {+ throw BaselineInputError.sandboxEnvironment("host must not install an app delegate")+ }+ guard application.windows.isEmpty else {+ throw BaselineInputError.sandboxEnvironment("host must not create windows")+ }+ }+ let canonicalHome = BaselinePathSafety.canonical(+ URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)+ )+ let containerMarker = ["Library", "Containers", Self.hostBundleIdentifier, "Data"]+ guard Self.containsContiguous(canonicalHome.pathComponents, marker: containerMarker) else {+ throw BaselineInputError.sandboxEnvironment(+ "home is not the \(Self.hostBundleIdentifier) container Data directory"+ )+ }+ }++ /// Bounded poll on an unstructured (uncancelled) task; caller re-checks cancellation after cleanup, before any leak error.+ private func waitForProviderDealloc(_ box: WeakProviderBox, budget: Duration) async throws -> Bool {+ let deallocated = await Task { @MainActor in+ let clock = ContinuousClock()+ let deadline = clock.now + budget+ while box.provider != nil, clock.now < deadline {+ try? await Task.sleep(for: .milliseconds(10))+ }+ return box.provider == nil+ }.value+ try Task.checkCancellation()+ return deallocated+ }++ private static func compare(+ legacy: ASRBaselineReport.RunRecord,+ meeting: ASRBaselineReport.RunRecord,+ round: Int+ ) -> ASRBaselineReport.PairComparison {+ var maxStartDelta = 0.0+ var maxEndDelta = 0.0+ for (legacyWord, meetingWord) in zip(legacy.words, meeting.words) {+ maxStartDelta = max(maxStartDelta, abs(legacyWord.start - meetingWord.start))+ maxEndDelta = max(maxEndDelta, abs(legacyWord.end - meetingWord.end))+ }+ return ASRBaselineReport.PairComparison(+ round: round,+ transcriptMatches: legacy.transcript == meeting.transcript,+ wordTextsMatch: legacy.words.map(\.text) == meeting.words.map(\.text),+ legacyWordCount: legacy.words.count,+ meetingWordCount: meeting.words.count,+ maxWordStartDeltaSeconds: maxStartDelta,+ maxWordEndDeltaSeconds: maxEndDelta+ )+ }++ private static func progressLine(for record: ASRBaselineReport.RunRecord) -> String {+ let peakResidentMB = Double(record.memoryPeakDuringRun.residentBytes) / 1_048_576+ let peakFootprintMB = Double(record.memoryPeakDuringRun.footprintBytes) / 1_048_576+ return String(+ format: "ASR_BASELINE round=%d/%d arm=%@ order=%d prepare=%.3fs inference=%.3fs peakRSS=%.0fMB peakFootprint=%.0fMB deallocated=%@",+ record.round + 1,+ Self.pairRoundCount,+ record.arm,+ record.orderInRound,+ record.prepareSeconds,+ record.inferenceSeconds,+ peakResidentMB,+ peakFootprintMB,+ record.providerDeallocated ? "yes" : "no"+ )+ }++ private static func seconds(from start: ContinuousClock.Instant, to end: ContinuousClock.Instant) -> Double {+ let duration = end - start+ return Double(duration.components.seconds) + Double(duration.components.attoseconds) / 1e18+ }++ private static func containsContiguous(_ components: [String], marker: [String]) -> Bool {+ guard !marker.isEmpty, marker.count <= components.count else { return false }+ for start in 0...(components.count - marker.count)+ where components[start..<(start + marker.count)].elementsEqual(marker) {+ return true+ }+ return false+ }++ private static func isHex64(_ string: String) -> Bool {+ string.count == 64 && string.allSatisfy(\.isHexDigit)+ }++ private func attachReport(_ report: ASRBaselineReport) {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ guard let data = try? encoder.encode(report) else { return }+ let attachment = XCTAttachment(data: data, uniformTypeIdentifier: "public.json")+ attachment.lifetime = .keepAlways+ attachment.name = "ASRBaselineReport"+ self.add(attachment)+ }++ /// Fallback attachment for failures before `partialReport` exists.+ private func attachDiagnosticJSON(note: String) {+ guard let data = try? JSONSerialization.data(+ withJSONObject: ["note": note],+ options: [.prettyPrinted, .sortedKeys]+ ) else { return }+ let attachment = XCTAttachment(data: data, uniformTypeIdentifier: "public.json")+ attachment.lifetime = .keepAlways+ attachment.name = "ASRBaselineReport"+ self.add(attachment)+ }+}++nonisolated enum BaselineArm: String, Sendable {+ case legacy+ case meeting+}++/// Local to the benchmark driver; proves the provider frame released before the next arm starts.+final class WeakProviderBox {+ weak var provider: AnyObject?+}++nonisolated enum BaselineInputError: Error, Equatable {+ case missingInputsRoot+ case invalidManifest(String)+ case unsafeRelativePath(String)+ case hashMismatch(String)+ case fixtureMetadataMismatch(String)+ case sandboxEnvironment(String)+ case unsupportedFixtureFormat(String)+ case modelsMissingOnDisk+ case providerDidNotDeallocate+ case memoryQueryFailed(String)+ case memorySamplerDidNotStop+ case emptyTranscription(String)+ case invalidWordTimings(String)+}++extension BaselineInputError: LocalizedError {+ var errorDescription: String? {+ switch self {+ case .missingInputsRoot:+ return "ASRBaselineInputs not found in host bundle resources"+ case .invalidManifest(let detail):+ return "invalid manifest.json: \(detail)"+ case .unsafeRelativePath(let path):+ return "unsafe relative path: \(path)"+ case .hashMismatch(let label):+ return "sha256 mismatch: \(label)"+ case .fixtureMetadataMismatch(let detail):+ return "fixture metadata mismatch: \(detail)"+ case .sandboxEnvironment(let detail):+ return "sandbox environment check failed: \(detail)"+ case .unsupportedFixtureFormat(let detail):+ return "unsupported fixture format: \(detail)"+ case .modelsMissingOnDisk:+ return "model cache incomplete before prepare"+ case .providerDidNotDeallocate:+ return "provider still alive after the 2s dealloc budget"+ case .memoryQueryFailed(let detail):+ return "memory query failed: \(detail)"+ case .memorySamplerDidNotStop:+ return "memory peak sampler failed to join within the 2s deadline"+ case .emptyTranscription(let arm):+ return "empty transcript or word timings: \(arm)"+ case .invalidWordTimings(let arm):+ return "non-finite or unordered word timings: \(arm)"+ }+ }+}++nonisolated struct ASRBaselineManifest: Codable, Sendable, Equatable {+ struct Fixture: Codable, Sendable, Equatable {+ var path: String+ var sha256: String+ var sampleRateHz: Int+ var channels: Int+ var durationSeconds: Double+ }++ struct Artifact: Codable, Sendable, Equatable {+ var path: String+ var sha256: String+ }++ var schemaVersion: Int+ var modelRepository: String+ var fixture: Fixture+ var artifacts: [Artifact]+}++nonisolated struct TaskMemorySnapshot: Codable, Sendable {+ var residentBytes: UInt64+ var footprintBytes: UInt64++ /// Throws on task_info failure: a silently zeroed snapshot reads as a valid 0.+ static func capture() throws -> TaskMemorySnapshot {+ var basicInfo = mach_task_basic_info()+ var basicCount = mach_msg_type_number_t(+ MemoryLayout<mach_task_basic_info>.stride / MemoryLayout<integer_t>.stride+ )+ let basicResult = withUnsafeMutablePointer(to: &basicInfo) { pointer in+ pointer.withMemoryRebound(to: integer_t.self, capacity: Int(basicCount)) { rebound in+ task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), rebound, &basicCount)+ }+ }+ guard basicResult == KERN_SUCCESS else {+ throw BaselineInputError.memoryQueryFailed("MACH_TASK_BASIC_INFO kern_return=\(basicResult)")+ }++ var vmInfo = task_vm_info_data_t()+ var vmCount = mach_msg_type_number_t(+ MemoryLayout<task_vm_info_data_t>.stride / MemoryLayout<integer_t>.stride+ )+ let vmResult = withUnsafeMutablePointer(to: &vmInfo) { pointer in+ pointer.withMemoryRebound(to: integer_t.self, capacity: Int(vmCount)) { rebound in+ task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), rebound, &vmCount)+ }+ }+ guard vmResult == KERN_SUCCESS else {+ throw BaselineInputError.memoryQueryFailed("TASK_VM_INFO kern_return=\(vmResult)")+ }+ return TaskMemorySnapshot(residentBytes: basicInfo.resident_size, footprintBytes: vmInfo.phys_footprint)+ }+}++nonisolated struct ASRBaselineReport: Codable, Sendable {+ struct WordRecord: Codable, Sendable, Equatable {+ var text: String+ var start: Double+ var end: Double+ }++ struct RunRecord: Codable, Sendable {+ var round: Int+ var orderInRound: Int+ var arm: String+ var prepareSeconds: Double+ var inferenceSeconds: Double+ var memoryBefore: TaskMemorySnapshot+ var memoryPeakDuringRun: TaskMemorySnapshot+ var memoryAfterReturn: TaskMemorySnapshot+ var memoryAfterDeallocWait: TaskMemorySnapshot?+ var providerDeallocated: Bool+ var transcript: String+ var words: [WordRecord]+ }++ struct PairComparison: Codable, Sendable {+ var round: Int+ var transcriptMatches: Bool+ var wordTextsMatch: Bool+ var legacyWordCount: Int+ var meetingWordCount: Int+ var maxWordStartDeltaSeconds: Double+ var maxWordEndDeltaSeconds: Double+ }++ var schemaVersion: Int+ var testKind: String+ var syntheticScope: String+ var policy: String+ var timestamp: String+ var modelRepository: String+ var fixtureSHA256: String+ var fixtureSampleRateHz: Int+ var fixtureChannels: Int+ var fixtureDurationSeconds: Double+ var runs: [RunRecord]+ var comparisons: [PairComparison]+ var note: String+}++/// NSLock + dedicated Thread so peak sampling never depends on the cooperative pool. Peak+/// between 5ms polls is approximate; Core ML caches may outlive the provider without leaking.+final class MemoryPeakSampler: @unchecked Sendable {+ private let lock = NSLock()+ private var peakResidentBytes: UInt64 = 0+ private var peakFootprintBytes: UInt64 = 0+ private var stopRequested = false+ private var firstCaptureError: Error?+ private var worker: Thread?+ private let completion = DispatchGroup()+ private static let joinTimeout: DispatchTimeInterval = .seconds(2)++ /// Seeds the peak with a valid snapshot so a pre-first-poll read never reports a fake 0.+ func start() throws {+ let seed = try TaskMemorySnapshot.capture()+ self.lock.lock()+ self.peakResidentBytes = seed.residentBytes+ self.peakFootprintBytes = seed.footprintBytes+ self.stopRequested = false+ self.firstCaptureError = nil+ self.lock.unlock()+ self.completion.enter()+ let thread = Thread { [self] in+ defer { self.completion.leave() }+ while !self.consumeStopRequest() {+ do {+ let sample = try TaskMemorySnapshot.capture()+ self.lock.lock()+ self.peakResidentBytes = max(self.peakResidentBytes, sample.residentBytes)+ self.peakFootprintBytes = max(self.peakFootprintBytes, sample.footprintBytes)+ self.lock.unlock()+ } catch {+ self.lock.lock()+ if self.firstCaptureError == nil {+ self.firstCaptureError = error+ }+ self.lock.unlock()+ break+ }+ Thread.sleep(forTimeInterval: 0.005)+ }+ }+ thread.name = "asr-baseline-memory-peak"+ thread.qualityOfService = .utility+ self.lock.lock()+ self.worker = thread+ self.lock.unlock()+ thread.start()+ }++ /// Real-deadline join on the worker's completion group; idempotent. False = caller must fail the run.+ @discardableResult+ func stopAndJoin() -> Bool {+ self.lock.lock()+ self.stopRequested = true+ let worker = self.worker+ self.lock.unlock()+ guard worker != nil else { return true }+ let joined = self.completion.wait(timeout: .now() + Self.joinTimeout) == .success+ guard joined else { return false }+ self.lock.lock()+ self.worker = nil+ self.lock.unlock()+ return true+ }++ /// Rethrows the first poll error recorded by the worker, if any; call after a joined stop.+ func checkCaptureError() throws {+ self.lock.lock()+ let error = self.firstCaptureError+ self.lock.unlock()+ if let error {+ throw error+ }+ }++ var peak: TaskMemorySnapshot {+ self.lock.lock()+ defer { self.lock.unlock() }+ return TaskMemorySnapshot(+ residentBytes: self.peakResidentBytes,+ footprintBytes: self.peakFootprintBytes+ )+ }++ private func consumeStopRequest() -> Bool {+ self.lock.lock()+ defer { self.lock.unlock() }+ return self.stopRequested+ }+}++nonisolated enum BaselinePathSafety {+ static func canonical(_ url: URL) -> URL {+ url.standardizedFileURL.resolvingSymlinksInPath()+ }++ /// Component-wise containment: raw string prefixes accept sibling names like `X-evil`.+ static func isStrictDescendant(_ child: URL, of ancestor: URL) -> Bool {+ let ancestorComponents = self.canonical(ancestor).pathComponents+ let childComponents = self.canonical(child).pathComponents+ guard ancestorComponents.count < childComponents.count else { return false }+ return Array(childComponents.prefix(ancestorComponents.count)) == ancestorComponents+ }++ static func validatedURL(forRelativePath relativePath: String, under root: URL) throws -> URL {+ guard !relativePath.isEmpty,+ !relativePath.hasPrefix("/"),+ !relativePath.hasPrefix("~"),+ !relativePath.contains("\\")+ else {+ throw BaselineInputError.unsafeRelativePath(relativePath)+ }+ let parts = relativePath.split(separator: "/", omittingEmptySubsequences: false)+ guard parts.allSatisfy({ !$0.isEmpty && $0 != "." && $0 != ".." }) else {+ throw BaselineInputError.unsafeRelativePath(relativePath)+ }+ let canonicalRoot = self.canonical(root)+ let rootPath = canonicalRoot.path+ var currentPath = rootPath+ for part in parts {+ currentPath = (currentPath as NSString).appendingPathComponent(String(part))+ if (try? FileManager.default.destinationOfSymbolicLink(atPath: currentPath)) != nil {+ throw BaselineInputError.unsafeRelativePath(relativePath)+ }+ currentPath = (currentPath as NSString).standardizingPath+ guard currentPath == rootPath || currentPath.hasPrefix(rootPath + "/") else {+ throw BaselineInputError.unsafeRelativePath(relativePath)+ }+ }+ let resolved = self.canonical(URL(fileURLWithPath: currentPath))+ guard self.isStrictDescendant(resolved, of: canonicalRoot) else {+ throw BaselineInputError.unsafeRelativePath(relativePath)+ }+ return resolved+ }+}++nonisolated enum BaselineHashing {+ /// Streams in 1 MiB chunks; the artifact set includes multi-hundred-MB model files.+ static func sha256Hex(ofFileAt url: URL) throws -> String {+ let handle = try FileHandle(forReadingFrom: url)+ defer { try? handle.close() }+ var hasher = SHA256()+ while let chunk = try handle.read(upToCount: 1 << 20), !chunk.isEmpty {+ hasher.update(data: chunk)+ }+ return hasher.finalize().map { String(format: "%02x", $0) }.joined()+ }+}++nonisolated enum BaselineInputLoader {+ static func inputsRoot() -> URL? {+ Bundle.main.resourceURL?.appendingPathComponent("ASRBaselineInputs", isDirectory: true)+ }++ static func loadManifest(from root: URL) throws -> ASRBaselineManifest {+ let manifestURL = root.appendingPathComponent("manifest.json", isDirectory: false)+ let data = try Data(contentsOf: manifestURL)+ let manifest = try JSONDecoder().decode(ASRBaselineManifest.self, from: data)+ guard manifest.schemaVersion == 1 else {+ throw BaselineInputError.invalidManifest("schemaVersion=\(manifest.schemaVersion)")+ }+ return manifest+ }+}++nonisolated enum BaselineAudioLoader {+ static func loadMono16kInt16Samples(from url: URL) throws -> [Float] {+ // forReading: alone defaults to float32 processing even for an int16 WAV.+ let file = try AVAudioFile(forReading: url, commonFormat: .pcmFormatInt16, interleaved: false)+ let format = file.processingFormat+ guard format.channelCount == 1,+ format.sampleRate == 16_000,+ format.commonFormat == .pcmFormatInt16+ else {+ throw BaselineInputError.unsupportedFixtureFormat("\(format)")+ }+ let declaredFrames = file.length+ guard declaredFrames > 0 else {+ throw BaselineInputError.unsupportedFixtureFormat("empty audio file")+ }+ var samples: [Float] = []+ samples.reserveCapacity(Int(declaredFrames))+ let scale = Float(1.0 / 32768.0)+ var consumed: AVAudioFramePosition = 0+ while consumed < declaredFrames {+ let remaining = declaredFrames - consumed+ let chunkSize = min(AVAudioFrameCount(remaining), 1_048_576)+ guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: chunkSize) else {+ throw BaselineInputError.unsupportedFixtureFormat("buffer allocation failed")+ }+ try file.read(into: buffer)+ let framesRead = AVAudioFramePosition(buffer.frameLength)+ guard framesRead > 0 else {+ throw BaselineInputError.unsupportedFixtureFormat(+ "premature EOF at \(consumed)/\(declaredFrames) frames"+ )+ }+ guard let channelData = buffer.int16ChannelData else {+ throw BaselineInputError.unsupportedFixtureFormat("no int16 channel data")+ }+ for index in 0..<Int(framesRead) {+ samples.append(Float(channelData[0][index]) * scale)+ }+ consumed += framesRead+ }+ guard consumed == declaredFrames, samples.count == declaredFrames else {+ throw BaselineInputError.fixtureMetadataMismatch(+ "decoded \(consumed) frames, expected \(declaredFrames)"+ )+ }+ return samples+ }+}diff --git a/Tests/FluidASRBaselineTests/ProviderEnhancementOptionsTests.swift b/Tests/FluidASRBaselineTests/ProviderEnhancementOptionsTests.swiftnew file mode 100644index 00000000..3125306a--- /dev/null+++ b/Tests/FluidASRBaselineTests/ProviderEnhancementOptionsTests.swift@@ -0,0 +1,57 @@+@testable import FluidASRBaselineHost+import XCTest++/// Pins the explicit `enhancementOptions` path of `FluidAudioProvider`. These tests never+/// touch `SettingsStore.shared` and never prepare models; under the baseline host any+/// transitive settings/credentials access would trip the `KeychainService` fatalError guard.+@MainActor+final class ProviderEnhancementOptionsTests: XCTestCase {+ private func makeEntry() -> SettingsStore.CustomDictionaryEntry {+ SettingsStore.CustomDictionaryEntry(+ id: UUID(uuidString: "11111111-2222-3333-4444-555555555555")!,+ triggers: ["fluid voice", "fluid boys"],+ replacement: "FluidVoice"+ )+ }++ func testExplicitEnhancementOptionsDriveEffectiveGetters() {+ let entry = self.makeEntry()+ let options = FluidAudioProviderEnhancementOptions(+ experimentalUnifiedFinalEnabled: true,+ pronunciationMatchingEnabled: true,+ customDictionaryEntries: [entry]+ )+ let provider = FluidAudioProvider(configureWordBoosting: false, enhancementOptions: options)+ #if arch(arm64) && DEBUG+ let snapshot = provider.effectiveEnhancementOptionsForTesting+ XCTAssertTrue(snapshot.experimentalUnifiedFinalEnabled)+ XCTAssertTrue(snapshot.pronunciationMatchingEnabled)+ XCTAssertEqual(snapshot.customDictionaryEntries, [entry])+ #endif+ }++ func testExplicitlyDisabledOptionsAreNotTreatedAsNil() {+ let options = FluidAudioProviderEnhancementOptions(+ experimentalUnifiedFinalEnabled: false,+ pronunciationMatchingEnabled: false,+ customDictionaryEntries: []+ )+ let provider = FluidAudioProvider(enhancementOptions: options)+ #if arch(arm64) && DEBUG+ let snapshot = provider.effectiveEnhancementOptionsForTesting+ XCTAssertFalse(snapshot.experimentalUnifiedFinalEnabled)+ XCTAssertFalse(snapshot.pronunciationMatchingEnabled)+ XCTAssertTrue(snapshot.customDictionaryEntries.isEmpty)+ #endif+ }++ func testMeetingProviderKeepsFixedAllDisabledPolicy() throws {+ let provider = try FluidAudioProvider(meetingConfiguration: MeetingFinalProcessingConfiguration())+ #if arch(arm64) && DEBUG+ let snapshot = provider.effectiveEnhancementOptionsForTesting+ XCTAssertFalse(snapshot.experimentalUnifiedFinalEnabled)+ XCTAssertFalse(snapshot.pronunciationMatchingEnabled)+ XCTAssertTrue(snapshot.customDictionaryEntries.isEmpty)+ #endif+ }+}diff --git a/Tests/FluidDictationIntegrationTests/MeetingASRPreparationOwnerTests.swift b/Tests/FluidDictationIntegrationTests/MeetingASRPreparationOwnerTests.swiftnew file mode 100644index 00000000..6acb87b4--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingASRPreparationOwnerTests.swift@@ -0,0 +1,1174 @@+#if FLUID_ASR_BASELINE_TESTS+@testable import FluidASRBaselineHost+#else+@testable import FluidVoice_Debug+#endif+import Foundation+import XCTest++private enum PreparationTestError: Error {+ case prepareFailed+ case retireFailed+}++/// Bounded rendezvous for orchestration tests. Waiters park on a continuation until `open()`;+/// cancellation of a waiter never resumes it, which is exactly how a non-cooperative+/// provider hook behaves under task cancellation.+private final class ContinuationGate: @unchecked Sendable {+ private let lock = NSLock()+ private var isOpen: Bool+ private var waiters: [CheckedContinuation<Void, Never>] = []++ init(open: Bool = false) {+ self.isOpen = open+ }++ var opened: Bool {+ self.lock.withLock { self.isOpen }+ }++ func wait() async {+ await withCheckedContinuation { continuation in+ let resumeNow = self.lock.withLock { () -> Bool in+ if self.isOpen { return true }+ self.waiters.append(continuation)+ return false+ }+ if resumeNow { continuation.resume() }+ }+ }++ func open() {+ let pending = self.lock.withLock { () -> [CheckedContinuation<Void, Never>] in+ self.isOpen = true+ let pending = self.waiters+ self.waiters.removeAll()+ return pending+ }+ pending.forEach { $0.resume() }+ }++ func reset() {+ self.lock.withLock { self.isOpen = false }+ }+}++private final class OrchestrationRecorder: @unchecked Sendable {+ private let lock = NSLock()+ private var recordedEvents: [String] = []++ var events: [String] {+ self.lock.withLock { self.recordedEvents }+ }++ func record(_ event: String) {+ self.lock.withLock { self.recordedEvents.append(event) }+ }++ func count(of event: String) -> Int {+ self.lock.withLock { self.recordedEvents.filter { $0 == event }.count }+ }+}++private final class TestFlag: @unchecked Sendable {+ private let lock = NSLock()+ private var flag = false++ var value: Bool {+ self.lock.withLock { self.flag }+ }++ func set(_ value: Bool) {+ self.lock.withLock { self.flag = value }+ }+}++private final class LeaseRegistry: @unchecked Sendable {+ private let lock = NSLock()+ private var activeLeases: [ASRActivityLease] = []++ func activate(_ lease: ASRActivityLease) {+ self.lock.withLock { self.activeLeases.append(lease) }+ }++ func deactivateAll() {+ self.lock.withLock { self.activeLeases.removeAll() }+ }++ func isActive(_ lease: ASRActivityLease) -> Bool {+ self.lock.withLock { self.activeLeases.contains(lease) }+ }+}++private final class DrainHook: @unchecked Sendable {+ private let lock = NSLock()+ private var handler: (() -> Void)?++ func set(_ handler: (() -> Void)?) {+ self.lock.withLock { self.handler = handler }+ }++ func fire() {+ self.lock.withLock { self.handler }?()+ }+}++/// Proves orchestration only: call order, call counts, gating. It says nothing about real+/// CoreML model loading or deallocation, which remain the production closures' job.+private final class FakeMeetingTranscriptionProvider: TranscriptionProvider, @unchecked Sendable {+ let name = "FakeMeetingProvider"++ private let lock = NSLock()+ private var ready = false+ private var prepareCallCount = 0+ private var gate: ContinuationGate?+ private var error: Error?+ private var enteredHook: (@Sendable () -> Void)?+ private var progressHandler: ((ModelPreparationProgress) -> Void)?++ var isAvailable: Bool { true }++ var isReady: Bool {+ self.lock.withLock { self.ready }+ }++ var prepareCalls: Int {+ self.lock.withLock { self.prepareCallCount }+ }++ var lastProgressHandler: ((ModelPreparationProgress) -> Void)? {+ self.lock.withLock { self.progressHandler }+ }++ func blockPrepare(on gate: ContinuationGate) {+ self.lock.withLock { self.gate = gate }+ }++ func failPrepare(with error: Error?) {+ self.lock.withLock { self.error = error }+ }++ func onPrepareEntered(_ hook: (@Sendable () -> Void)?) {+ self.lock.withLock { self.enteredHook = hook }+ }++ func openPrepareGate() {+ self.lock.withLock { self.gate }?.open()+ }++ func prepare(progressHandler: ((ModelPreparationProgress) -> Void)?) async throws {+ let entered = self.lock.withLock { () -> (@Sendable () -> Void)? in+ self.prepareCallCount += 1+ self.progressHandler = progressHandler+ return self.enteredHook+ }+ entered?()+ let gate = self.lock.withLock { self.gate }+ if let gate { await gate.wait() }+ if let error = self.lock.withLock({ self.error }) { throw error }+ self.lock.withLock { self.ready = true }+ }++ func transcribe(_ samples: [Float]) async throws -> ASRTranscriptionResult {+ ASRTranscriptionResult(text: "")+ }+}++private final class DeallocationProvider: TranscriptionProvider, @unchecked Sendable {+ let name = "DeallocationProvider"+ let probe: TestFlag++ init(probe: TestFlag) {+ self.probe = probe+ }++ deinit {+ self.probe.set(true)+ }++ var isAvailable: Bool { true }+ var isReady: Bool { true }++ func prepare(progressHandler: ((ModelPreparationProgress) -> Void)?) async throws {}++ func transcribe(_ samples: [Float]) async throws -> ASRTranscriptionResult {+ ASRTranscriptionResult(text: "")+ }+}++@MainActor+final class MeetingASRPreparationOwnerTests: XCTestCase {+ @MainActor private final class Harness {+ let recorder = OrchestrationRecorder()+ let registry = LeaseRegistry()+ let provider = FakeMeetingTranscriptionProvider()+ let userDownloadInProgress = TestFlag()+ let retireGate = ContinuationGate(open: true)+ let retireEntered = ContinuationGate()+ let failRetire = TestFlag()+ let drainGate = ContinuationGate(open: true)+ let drainHook = DrainHook()+ let owner: MeetingASRPreparationOwner++ init() {+ let recorder = self.recorder+ let registry = self.registry+ let provider = self.provider+ let userDownloadInProgress = self.userDownloadInProgress+ let retireGate = self.retireGate+ let retireEntered = self.retireEntered+ let failRetire = self.failRetire+ let drainGate = self.drainGate+ let drainHook = self.drainHook+ self.owner = MeetingASRPreparationOwner(+ isActiveLease: { lease in registry.isActive(lease) },+ isUserDownloadInProgress: { userDownloadInProgress.value },+ retireDictationResources: { _ in+ recorder.record("retire")+ retireEntered.open()+ await retireGate.wait()+ if failRetire.value { throw PreparationTestError.retireFailed }+ },+ makeProvider: { _ in+ recorder.record("factory")+ return provider+ },+ prepareProvider: { preparedProvider, _, progressHandler in+ let identity = preparedProvider as AnyObject === provider+ recorder.record(identity ? "prepare" : "prepare:wrongProvider")+ try await provider.prepare(progressHandler: progressHandler)+ },+ drainExecutor: { _ in+ recorder.record("drain")+ drainHook.fire()+ await drainGate.wait()+ }+ )+ }++ func openAllGates() {+ self.retireGate.open()+ self.provider.openPrepareGate()+ self.drainGate.open()+ }+ }++ private func makeHarness() -> Harness {+ let harness = Harness()+ self.addTeardownBlock { @MainActor in+ harness.openAllGates()+ if let token = harness.owner.currentClaimToken {+ await harness.owner.release(token)+ }+ }+ return harness+ }++ private func makeLease(activity: ASRExclusiveActivity = .meeting) -> ASRActivityLease {+ ASRActivityLease(id: UUID(), activity: activity)+ }++ private func installSecondWaiterAcknowledgement(+ on owner: MeetingASRPreparationOwner+ ) -> ContinuationGate {+ let secondWaiterEntered = ContinuationGate()+#if DEBUG+ let waiters = OrchestrationRecorder()+ owner.didBeginWaitingForTesting = {+ waiters.record("waiting")+ if waiters.count(of: "waiting") == 2 {+ secondWaiterEntered.open()+ }+ }+#endif+ return secondWaiterEntered+ }++ /// Bounded observation that never owns or cancels the operation being observed. Tests open+ /// their gates and join their task handles after observation, so non-cooperative hooks cannot+ /// strand a throwing timeout task-group child.+ private func waitUntil(+ timeoutNanoseconds: UInt64 = 5_000_000_000,+ condition: @escaping @MainActor () -> Bool+ ) async -> Bool {+ let deadline = DispatchTime.now().uptimeNanoseconds + timeoutNanoseconds+ while !condition() {+ if DispatchTime.now().uptimeNanoseconds >= deadline { return false }+ try? await Task.sleep(nanoseconds: 1_000_000)+ }+ return true+ }++ func testRejectsInvalidLeaseBeforeAnyMutationOrDependencyCall() async {+ let harness = self.makeHarness()+ let lease = self.makeLease()++ do {+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ XCTFail("Expected invalid lease rejection")+ } catch let error as MeetingASRPreparationError {+ XCTAssertEqual(error, .invalidActivityLease)+ } catch {+ XCTFail("Expected MeetingASRPreparationError, got \(error)")+ }++ // A lease the registry accepts but for the wrong activity is also rejected.+ let dictationLease = self.makeLease(activity: .dictation)+ harness.registry.activate(dictationLease)+ do {+ _ = try await harness.owner.prepare(+ lease: dictationLease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ XCTFail("Expected invalid lease rejection for non-meeting activity")+ } catch let error as MeetingASRPreparationError {+ XCTAssertEqual(error, .invalidActivityLease)+ } catch {+ XCTFail("Expected MeetingASRPreparationError, got \(error)")+ }++ XCTAssertTrue(harness.recorder.events.isEmpty)+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertFalse(harness.owner.isDraining)+ XCTAssertEqual(harness.provider.prepareCalls, 0)+ }++ func testRejectsUnsupportedFixedPolicyBeforeAnyMutationOrDependencyCall() async {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)++ do {+ _ = try await harness.owner.prepare(+ lease: lease,+ attemptID: UUID(),+ configuration: MeetingFinalProcessingConfiguration(languageCode: "de")+ )+ XCTFail("Expected unsupported policy rejection")+ } catch let error as MeetingProviderOptionsError {+ XCTAssertEqual(error, .unsupportedLanguageCode("de"))+ } catch {+ XCTFail("Expected MeetingProviderOptionsError, got \(error)")+ }++ do {+ _ = try await harness.owner.prepare(+ lease: lease,+ attemptID: UUID(),+ configuration: MeetingFinalProcessingConfiguration(vocabularyBoostingEnabled: true)+ )+ XCTFail("Expected unsupported policy rejection")+ } catch let error as MeetingProviderOptionsError {+ XCTAssertEqual(error, .unsupportedFeature("vocabularyBoosting"))+ } catch {+ XCTFail("Expected MeetingProviderOptionsError, got \(error)")+ }++ XCTAssertTrue(harness.recorder.events.isEmpty)+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertEqual(harness.provider.prepareCalls, 0)+ }++ func testRejectsUserDownloadInProgressBeforeAnyMutationOrDependencyCall() async {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ harness.userDownloadInProgress.set(true)++ do {+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ XCTFail("Expected user-download rejection")+ } catch let error as MeetingASRPreparationError {+ XCTAssertEqual(error, .userModelDownloadInProgress)+ } catch {+ XCTFail("Expected MeetingASRPreparationError, got \(error)")+ }++ XCTAssertTrue(harness.recorder.events.isEmpty)+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertEqual(harness.provider.prepareCalls, 0)+ }++ func testPreCancelledPrepareRejectsWithoutMutationOrDependencyCall() async {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)++ let task = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ }+ task.cancel()+ do {+ _ = try await task.value+ XCTFail("Expected pre-cancelled prepare to throw")+ } catch is CancellationError {+ } catch {+ XCTFail("Expected CancellationError, got \(error)")+ }++ XCTAssertTrue(harness.recorder.events.isEmpty)+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertEqual(harness.provider.prepareCalls, 0)+ }++ func testSuccessfulPrepareRetiresBuildsPreparesInOrderAndStaysClaimed() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attempt = UUID()+ let configuration = MeetingFinalProcessingConfiguration()++ let first = try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: configuration+ )++ XCTAssertEqual(harness.recorder.events, ["retire", "factory", "prepare"])+ XCTAssertTrue(harness.owner.isClaimed)+ XCTAssertFalse(harness.owner.isDraining)+ XCTAssertTrue(harness.provider.isReady)++ // A repeated identical request returns the ready slot without redoing any work.+ let second = try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: configuration+ )+ XCTAssertTrue(first as AnyObject === second as AnyObject)+ XCTAssertEqual(harness.recorder.events, ["retire", "factory", "prepare"])+ XCTAssertEqual(harness.provider.prepareCalls, 1)+ XCTAssertTrue(harness.owner.isClaimed)+ }++ func testSameLeaseAttemptAndConfigurationJoinsSingleFlight() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attempt = UUID()+ let configuration = MeetingFinalProcessingConfiguration()+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }++ let first = Task { @MainActor in+ let provider = try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: configuration+ )+ return ObjectIdentifier(provider as AnyObject)+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)++ let second = Task { @MainActor in+ let provider = try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: configuration+ )+ return ObjectIdentifier(provider as AnyObject)+ }+ XCTAssertTrue(harness.owner.isClaimed)++ providerGate.open()+ let firstID = try await first.value+ let secondID = try await second.value++ XCTAssertEqual(firstID, secondID)+ XCTAssertEqual(harness.provider.prepareCalls, 1)+ XCTAssertEqual(harness.recorder.count(of: "retire"), 1)+ XCTAssertEqual(harness.recorder.count(of: "factory"), 1)+ XCTAssertTrue(harness.owner.isClaimed)+ }++ func testDifferentRequestFailsBusyWithoutCancellingInFlightPreparation() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attempt = UUID()+ let configuration = MeetingFinalProcessingConfiguration()+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }++ let first = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: configuration+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)++ let otherLease = self.makeLease()+ harness.registry.activate(otherLease)+ let busyAttempts: [(ASRActivityLease, UUID, MeetingFinalProcessingConfiguration)] = [+ (lease, UUID(), configuration),+ (lease, attempt, MeetingFinalProcessingConfiguration(pipelineVersion: configuration.pipelineVersion + 1)),+ (otherLease, attempt, configuration),+ ]+ for (busyLease, busyAttempt, busyConfiguration) in busyAttempts {+ do {+ _ = try await harness.owner.prepare(+ lease: busyLease, attemptID: busyAttempt, configuration: busyConfiguration+ )+ XCTFail("Expected busy rejection for a different request")+ } catch let error as MeetingASRPreparationError {+ XCTAssertEqual(error, .preparationInProgress)+ } catch {+ XCTFail("Expected MeetingASRPreparationError, got \(error)")+ }+ }++ // The rejections must not have disturbed the in-flight preparation.+ providerGate.open()+ _ = try await first.value+ XCTAssertEqual(harness.provider.prepareCalls, 1)+ XCTAssertEqual(harness.recorder.events, ["retire", "factory", "prepare"])+ XCTAssertTrue(harness.owner.isClaimed)++ // A different request is still busy against the ready slot; the identical request+ // returns it without new work.+ do {+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: configuration+ )+ XCTFail("Expected busy rejection against the ready slot")+ } catch let error as MeetingASRPreparationError {+ XCTAssertEqual(error, .preparationInProgress)+ } catch {+ XCTFail("Expected MeetingASRPreparationError, got \(error)")+ }+ _ = try await harness.owner.prepare(lease: lease, attemptID: attempt, configuration: configuration)+ XCTAssertEqual(harness.provider.prepareCalls, 1)+ XCTAssertTrue(harness.owner.isClaimed)+ }++ func testCancellationStaysClaimedUntilNonCooperativePrepareAndDrainFinish() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ let attemptID = UUID()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }++ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)++ preparation.cancel()+ // The fake hook ignores cancellation, so until it returns the claim must stay and+ // the executor drain must not have run.+ XCTAssertTrue(harness.owner.isClaimed)+ XCTAssertFalse(harness.owner.isDraining)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 0)++ providerGate.open()+ do {+ _ = try await preparation.value+ XCTFail("Expected cancelled preparation to throw")+ } catch is CancellationError {+ } catch {+ XCTFail("Expected CancellationError, got \(error)")+ }++ XCTAssertEqual(harness.recorder.events, ["retire", "factory", "prepare", "drain"])+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertFalse(harness.owner.isDraining)+ }++ func testFailedPrepareDrainsExecutorBeforeClearingClaimAndAllowsRetry() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let configuration = MeetingFinalProcessingConfiguration()+ harness.provider.failPrepare(with: PreparationTestError.prepareFailed)++ do {+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: configuration+ )+ XCTFail("Expected preparation failure")+ } catch PreparationTestError.prepareFailed {+ } catch {+ XCTFail("Expected PreparationTestError, got \(error)")+ }++ XCTAssertEqual(harness.recorder.events, ["retire", "factory", "prepare", "drain"])+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertFalse(harness.owner.isDraining)++ harness.provider.failPrepare(with: nil)+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: configuration+ )+ XCTAssertEqual(+ harness.recorder.events,+ ["retire", "factory", "prepare", "drain", "retire", "factory", "prepare"]+ )+ XCTAssertEqual(harness.provider.prepareCalls, 2)+ XCTAssertTrue(harness.owner.isClaimed)+ }++ func testRetirementCancellationInvalidationAndFailureNeverReachFactory() async throws {+ do {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ harness.retireGate.reset()+ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ }+ let observed = await self.waitUntil { harness.retireEntered.opened }+ XCTAssertTrue(observed)+ preparation.cancel()+ XCTAssertTrue(harness.owner.isClaimed)+ harness.retireGate.open()+ do {+ _ = try await preparation.value+ XCTFail("Expected retirement cancellation")+ } catch is CancellationError {+ }+ XCTAssertEqual(harness.recorder.count(of: "factory"), 0)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ XCTAssertFalse(harness.owner.isClaimed)+ }++ do {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ harness.retireGate.reset()+ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ }+ let observed = await self.waitUntil { harness.retireEntered.opened }+ XCTAssertTrue(observed)+ harness.registry.deactivateAll()+ harness.retireGate.open()+ do {+ _ = try await preparation.value+ XCTFail("Expected invalid-lease cancellation")+ } catch is CancellationError {+ }+ XCTAssertEqual(harness.recorder.count(of: "factory"), 0)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ XCTAssertFalse(harness.owner.isClaimed)+ }++ do {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ harness.failRetire.set(true)+ do {+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ XCTFail("Expected retirement failure")+ } catch PreparationTestError.retireFailed {+ }+ XCTAssertEqual(harness.recorder.events, ["retire", "drain"])+ XCTAssertFalse(harness.owner.isClaimed)+ }+ }++ func testReleaseAndPreparationFailureShareExactlyOneDrain() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let entered = ContinuationGate()+ let providerGate = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }+ harness.provider.failPrepare(with: PreparationTestError.prepareFailed)+ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)+ let token = try XCTUnwrap(harness.owner.currentClaimToken)+ harness.drainGate.reset()+ let release = Task { @MainActor in await harness.owner.release(token) }+ providerGate.open()+ let drainObserved = await self.waitUntil { harness.recorder.count(of: "drain") == 1 }+ XCTAssertTrue(drainObserved)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ harness.drainGate.open()+ do {+ _ = try await preparation.value+ XCTFail("Expected preparation failure")+ } catch PreparationTestError.prepareFailed {+ }+ await release.value+ XCTAssertFalse(harness.owner.isClaimed)+ }++ func testOldTokenCannotReleaseRepeatedSameLeaseAttempt() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attempt = UUID()+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: MeetingFinalProcessingConfiguration()+ )+ let oldToken = try XCTUnwrap(harness.owner.currentClaimToken)+ await harness.owner.release(oldToken)+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: attempt, configuration: MeetingFinalProcessingConfiguration()+ )+ let newToken = try XCTUnwrap(harness.owner.currentClaimToken)+ await harness.owner.release(oldToken)+ XCTAssertTrue(harness.owner.isClaimed)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ await harness.owner.release(newToken)+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 2)+ }++ func testJoinedWaiterCancellationCancelsSingleFlightAndSharesDrain() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attemptID = UUID()+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }+ let secondWaiterEntered = self.installSecondWaiterAcknowledgement(on: harness.owner)+#if !DEBUG+ let secondStarted = ContinuationGate()+#endif+ let first = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)+ let second = Task { @MainActor in+#if !DEBUG+ secondStarted.open()+#endif+ return try await harness.owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ }+ // Both waiters join the same exact operation; cancellation of one cancels the shared worker.+#if DEBUG+ let secondObserved = await self.waitUntil { secondWaiterEntered.opened }+#else+ let secondObserved = await self.waitUntil { secondStarted.opened }+#endif+ XCTAssertTrue(secondObserved)+ second.cancel()+ providerGate.open()+ do {+ _ = try await first.value+ XCTFail("Expected the first joined waiter to observe cancellation")+ } catch is CancellationError {+ } catch {+ XCTFail("Expected CancellationError from first waiter, got \(error)")+ }+ do {+ _ = try await second.value+ XCTFail("Expected the cancelled joined waiter to throw")+ } catch is CancellationError {+ } catch {+ XCTFail("Expected CancellationError from second waiter, got \(error)")+ }+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ XCTAssertFalse(harness.owner.isClaimed)+ }++#if DEBUG+ func testReadyTransitionCancelsJoinedWaiterBeforeEitherCanReturn() async throws {+ var joinedWaiter: Task<any TranscriptionProvider, Error>?+ let harness = self.makeHarness()+ harness.owner.didMarkReadyForTesting = {+ joinedWaiter?.cancel()+ }+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attemptID = UUID()+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }+ let secondWaiterEntered = self.installSecondWaiterAcknowledgement(on: harness.owner)+#if !DEBUG+ let secondStarted = ContinuationGate()+#endif++ let first = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)+ let second = Task { @MainActor in+#if !DEBUG+ secondStarted.open()+#endif+ return try await harness.owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ }+ joinedWaiter = second+#if DEBUG+ let secondObserved = await self.waitUntil { secondWaiterEntered.opened }+#else+ let secondObserved = await self.waitUntil { secondStarted.opened }+#endif+ XCTAssertTrue(secondObserved)+ providerGate.open()++ do {+ _ = try await first.value+ XCTFail("Expected first waiter to observe shared-worker cancellation")+ } catch is CancellationError {+ } catch {+ XCTFail("Expected CancellationError from first waiter, got \(error)")+ }+ do {+ _ = try await second.value+ XCTFail("Expected joined waiter cancellation")+ } catch is CancellationError {+ } catch {+ XCTFail("Expected CancellationError from joined waiter, got \(error)")+ }+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ XCTAssertFalse(harness.owner.isClaimed)+ joinedWaiter = nil+ }+#endif++ func testProgressHopsToMainActorAndStaleEventsAreDroppedAfterReleaseAndLeaseChange() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }++ let deliveries = OrchestrationRecorder()+ let expectedProgress = [+ "progress:preparingDownload:-1.0",+ "progress:downloading:0.5",+ "progress:optimizing:-1.0",+ "progress:loading:-1.0",+ ]+ let allProgressDelivered = self.expectation(description: "all progress phases delivered on the main actor")+ let progress: (ModelPreparationProgress) -> Void = { update in+ XCTAssertTrue(Thread.isMainThread)+ deliveries.record("progress:\(update.phase):\(update.fractionCompleted ?? -1)")+ if deliveries.events.count == expectedProgress.count {+ allProgressDelivered.fulfill()+ }+ }++ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease,+ attemptID: UUID(),+ configuration: MeetingFinalProcessingConfiguration(),+ progress: progress+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)++ let handler = try XCTUnwrap(harness.provider.lastProgressHandler)+ DispatchQueue.global().async {+ handler(.preparingDownload)+ handler(.downloading(0.5))+ handler(.optimizing)+ handler(.loading)+ }+ await fulfillment(of: [allProgressDelivered], timeout: 2)+ XCTAssertEqual(deliveries.events, expectedProgress)++ providerGate.open()+ _ = try await preparation.value+ let token = try XCTUnwrap(harness.owner.currentClaimToken)++ // A late provider callback after readiness is stale even before release begins.+ let staleAfterReady = self.expectation(description: "no delivery after ready")+ staleAfterReady.isInverted = true+ DispatchQueue.global().async { handler(.downloading(0.6)) }+ await fulfillment(of: [staleAfterReady], timeout: 0.3)++ // Draining suppresses callbacks while the exact claim remains visible.+ harness.drainGate.reset()+ let drainEntered = ContinuationGate()+ harness.drainHook.set { drainEntered.open() }+ let release = Task { @MainActor in await harness.owner.release(token) }+ let drainObserved = await self.waitUntil { drainEntered.opened }+ XCTAssertTrue(drainObserved)+ let staleDuringDrain = self.expectation(description: "no delivery while draining")+ staleDuringDrain.isInverted = true+ DispatchQueue.global().async { handler(.downloading(0.7)) }+ await fulfillment(of: [staleDuringDrain], timeout: 0.3)+ harness.drainGate.open()+ await release.value+ XCTAssertFalse(harness.owner.isClaimed)++ // After release the old handler is stale and must be dropped.+ let staleAfterRelease = self.expectation(description: "no delivery after release")+ staleAfterRelease.isInverted = true+ DispatchQueue.global().async { handler(.downloading(0.75)) }+ await fulfillment(of: [staleAfterRelease], timeout: 0.3)+ XCTAssertEqual(deliveries.events, expectedProgress)++ // After a different lease owns a fresh slot the old handler is still stale.+ let newLease = self.makeLease()+ harness.registry.activate(newLease)+ _ = try await harness.owner.prepare(+ lease: newLease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )+ let staleAfterLeaseChange = self.expectation(description: "no delivery after lease change")+ staleAfterLeaseChange.isInverted = true+ DispatchQueue.global().async { handler(.downloading(0.9)) }+ await fulfillment(of: [staleAfterLeaseChange], timeout: 0.3)+ XCTAssertEqual(deliveries.events, expectedProgress)+ }++ func testProgressIsSuppressedAfterCancellationAndLeaseInvalidation() async throws {+ do {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }+ let deliveries = OrchestrationRecorder()+ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease,+ attemptID: UUID(),+ configuration: MeetingFinalProcessingConfiguration(),+ progress: { _ in deliveries.record("cancelled") }+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)+ let handler = try XCTUnwrap(harness.provider.lastProgressHandler)+ preparation.cancel()+ let stale = self.expectation(description: "no progress after cancellation")+ stale.isInverted = true+ DispatchQueue.global().async { handler(.downloading(0.8)) }+ await fulfillment(of: [stale], timeout: 0.3)+ providerGate.open()+ do {+ _ = try await preparation.value+ XCTFail("Expected cancelled preparation")+ } catch is CancellationError {+ }+ XCTAssertTrue(deliveries.events.isEmpty)+ }++ do {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let providerGate = ContinuationGate()+ let entered = ContinuationGate()+ harness.provider.blockPrepare(on: providerGate)+ harness.provider.onPrepareEntered { entered.open() }+ let deliveries = OrchestrationRecorder()+ let preparation = Task { @MainActor in+ try await harness.owner.prepare(+ lease: lease,+ attemptID: UUID(),+ configuration: MeetingFinalProcessingConfiguration(),+ progress: { _ in deliveries.record("invalid") }+ )+ }+ let enteredObserved = await self.waitUntil { entered.opened }+ XCTAssertTrue(enteredObserved)+ let handler = try XCTUnwrap(harness.provider.lastProgressHandler)+ harness.registry.deactivateAll()+ let stale = self.expectation(description: "no progress after invalidation")+ stale.isInverted = true+ DispatchQueue.global().async { handler(.downloading(0.9)) }+ await fulfillment(of: [stale], timeout: 0.3)+ providerGate.open()+ do {+ _ = try await preparation.value+ XCTFail("Expected invalidated preparation")+ } catch is CancellationError {+ }+ XCTAssertTrue(deliveries.events.isEmpty)+ }+ }++ func testReleaseJoinsSingleDrainAndRejectsNewPreparesUntilDrainCompletes() async throws {+ let harness = self.makeHarness()+ let lease = self.makeLease()+ harness.registry.activate(lease)+ let attempt = UUID()+ let configuration = MeetingFinalProcessingConfiguration()+ _ = try await harness.owner.prepare(lease: lease, attemptID: attempt, configuration: configuration)+ XCTAssertTrue(harness.owner.isClaimed)++ harness.drainGate.reset()+ let drainEntered = ContinuationGate()+ harness.drainHook.set { drainEntered.open() }++ let token = try XCTUnwrap(harness.owner.currentClaimToken)+ let firstRelease = Task { @MainActor in+ await harness.owner.release(token)+ }+ let drainObserved = await self.waitUntil { drainEntered.opened }+ XCTAssertTrue(drainObserved)+ XCTAssertTrue(harness.owner.isClaimed)+ XCTAssertTrue(harness.owner.isDraining)++ // No new prepare may start while the drain is blocked, not even the same request.+ for blockedAttempt in [attempt, UUID()] {+ do {+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: blockedAttempt, configuration: configuration+ )+ XCTFail("Expected busy rejection during drain")+ } catch let error as MeetingASRPreparationError {+ XCTAssertEqual(error, .preparationInProgress)+ } catch {+ XCTFail("Expected MeetingASRPreparationError, got \(error)")+ }+ }++ // A second release joins the same drain instead of starting another one.+ let secondRelease = Task { @MainActor in+ await harness.owner.release(token)+ }+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)++ harness.drainGate.open()+ await firstRelease.value+ await secondRelease.value+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertFalse(harness.owner.isDraining)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)++ // Once the drain finished, a fresh preparation may claim the slot again.+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: configuration+ )+ XCTAssertTrue(harness.owner.isClaimed)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ }++ func testReleaseWithoutMatchingClaimIsNoOp() async throws {+ let harness = self.makeHarness()++ await harness.owner.release(+ MeetingASRPreparationClaimToken(lease: self.makeLease(), operationID: UUID())+ )+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertTrue(harness.recorder.events.isEmpty)++ let lease = self.makeLease()+ harness.registry.activate(lease)+ _ = try await harness.owner.prepare(+ lease: lease, attemptID: UUID(), configuration: MeetingFinalProcessingConfiguration()+ )++ await harness.owner.release(+ MeetingASRPreparationClaimToken(lease: self.makeLease(), operationID: UUID())+ )+ XCTAssertTrue(harness.owner.isClaimed)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 0)++ await harness.owner.release(try XCTUnwrap(harness.owner.currentClaimToken))+ XCTAssertFalse(harness.owner.isClaimed)+ XCTAssertEqual(harness.recorder.count(of: "drain"), 1)+ }++ func testSuccessfulWorkerTaskDoesNotRetainProviderResultAfterRelease() async throws {+ let probe = TestFlag()+ let lease = self.makeLease()+ let registry = LeaseRegistry()+ registry.activate(lease)+ let providerGate = ContinuationGate()+ let providerEntered = ContinuationGate()+ let owner = MeetingASRPreparationOwner(+ isActiveLease: { registry.isActive($0) },+ isUserDownloadInProgress: { false },+ retireDictationResources: { _ in },+ makeProvider: { _ in DeallocationProvider(probe: probe) },+ prepareProvider: { provider, _, progress in+ providerEntered.open()+ await providerGate.wait()+ try await provider.prepare(progressHandler: progress)+ },+ drainExecutor: { _ in }+ )+ let secondWaiterEntered = self.installSecondWaiterAcknowledgement(on: owner)+#if !DEBUG+ let secondStarted = ContinuationGate()+#endif+ self.addTeardownBlock { @MainActor in+ providerGate.open()+ if let token = owner.currentClaimToken {+ await owner.release(token)+ }+ }++ let attemptID = UUID()+ let first = Task { @MainActor in+ do {+ _ = try await owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ return true+ } catch {+ return false+ }+ }+ let enteredObserved = await self.waitUntil { providerEntered.opened }+ XCTAssertTrue(enteredObserved)+ let second = Task { @MainActor in+#if !DEBUG+ secondStarted.open()+#endif+ do {+ _ = try await owner.prepare(+ lease: lease, attemptID: attemptID, configuration: MeetingFinalProcessingConfiguration()+ )+ return true+ } catch {+ return false+ }+ }+#if DEBUG+ let secondObserved = await self.waitUntil { secondWaiterEntered.opened }+#else+ let secondObserved = await self.waitUntil { secondStarted.opened }+#endif+ XCTAssertTrue(secondObserved)+ providerGate.open()+ let firstSucceeded = await first.value+ let secondSucceeded = await second.value+ XCTAssertTrue(firstSucceeded)+ XCTAssertTrue(secondSucceeded)+ let token = try XCTUnwrap(owner.currentClaimToken)+ await owner.release(token)+ XCTAssertTrue(probe.value)+ }+}diff --git a/Tests/FluidDictationIntegrationTests/MeetingEchoSignalScorerTests.swift b/Tests/FluidDictationIntegrationTests/MeetingEchoSignalScorerTests.swiftindex 780ca87a..d4541655 100644--- a/Tests/FluidDictationIntegrationTests/MeetingEchoSignalScorerTests.swift+++ b/Tests/FluidDictationIntegrationTests/MeetingEchoSignalScorerTests.swift@@ -146,7 +146,7 @@ final class MeetingEchoSignalScorerTests: XCTestCase {mic: mic, reference: reference, sampleRate: self.sampleRate, delaySeconds: 0)let verdict = MeetingEchoSignalScorer.verdict(scores)- XCTAssertEqual(verdict, .containsLocalSpeech)+ XCTAssertEqual(verdict, .residualNotExplained)}func testDoubleTalkBurstRescuesTurnAsLocalSpeech() {@@ -171,7 +171,7 @@ final class MeetingEchoSignalScorerTests: XCTestCase {mic: mic, reference: reference, sampleRate: self.sampleRate, delaySeconds: Double(delaySamples) / self.sampleRate)let verdict = MeetingEchoSignalScorer.verdict(scores)- XCTAssertEqual(verdict, .containsLocalSpeech)+ XCTAssertEqual(verdict, .residualNotExplained)}/// A long turn of pure bleed picks up a brief low-scoring run from estimation noise. Measured on@@ -220,7 +220,7 @@ final class MeetingEchoSignalScorerTests: XCTestCase {let scores = MeetingEchoSignalScorer.explainedFractions(mic: mic, reference: reference, sampleRate: self.sampleRate, delaySeconds: Double(delaySamples) / self.sampleRate)- XCTAssertEqual(MeetingEchoSignalScorer.verdict(scores), .containsLocalSpeech)+ XCTAssertEqual(MeetingEchoSignalScorer.verdict(scores), .residualNotExplained)}func testQuietAdditiveInterjectionDoesNotRescueTurn() {@@ -294,7 +294,7 @@ final class MeetingEchoSignalScorerTests: XCTestCase {}let scores = EchoFrameScores(fractions: fractions, hopSeconds: self.hopSeconds)let verdict = MeetingEchoSignalScorer.verdict(scores)- XCTAssertEqual(verdict, .containsLocalSpeech)+ XCTAssertEqual(verdict, .residualNotExplained)}func testExplainedFractionsAreClampedToUnitRange() {diff --git a/Tests/FluidDictationIntegrationTests/MeetingEchoVetoTests.swift b/Tests/FluidDictationIntegrationTests/MeetingEchoVetoTests.swiftindex 72440ae1..b21da701 100644--- a/Tests/FluidDictationIntegrationTests/MeetingEchoVetoTests.swift+++ b/Tests/FluidDictationIntegrationTests/MeetingEchoVetoTests.swift@@ -218,10 +218,10 @@ final class MeetingEchoVetoTests: XCTestCase {isLikelyEcho: isLikelyEcho, echoScored: true, signalVerdict: signalVerdict)}- XCTAssertFalse(turn(isLikelyEcho: true, signalVerdict: .containsLocalSpeech).effectiveEcho)+ XCTAssertFalse(turn(isLikelyEcho: true, signalVerdict: .residualNotExplained).effectiveEcho)XCTAssertTrue(turn(isLikelyEcho: true, signalVerdict: .echo).effectiveEcho)XCTAssertTrue(turn(isLikelyEcho: true, signalVerdict: .unknown).effectiveEcho)- XCTAssertFalse(turn(isLikelyEcho: false, signalVerdict: .containsLocalSpeech).effectiveEcho)+ XCTAssertFalse(turn(isLikelyEcho: false, signalVerdict: .residualNotExplained).effectiveEcho)// Signal-only echo: ASR garbles bleed differently from the far end's own transcript, so// text matching misses it while the signal reads it plainly (measured median 0.91).XCTAssertTrue(turn(isLikelyEcho: false, signalVerdict: .echo).effectiveEcho)@@ -270,7 +270,7 @@ final class MeetingEchoVetoTests: XCTestCase {from: turns(signalVerdict: .unknown), prototypeSpeakerIDs: prototypeSpeakerIDs)let localSpeechResult = MeetingProcessingPipeline.selectLocalCluster(- from: turns(signalVerdict: .containsLocalSpeech), prototypeSpeakerIDs: prototypeSpeakerIDs+ from: turns(signalVerdict: .residualNotExplained), prototypeSpeakerIDs: prototypeSpeakerIDs)let echoResult = MeetingProcessingPipeline.selectLocalCluster(from: turns(signalVerdict: .echo), prototypeSpeakerIDs: prototypeSpeakerIDsdiff --git a/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift b/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swiftindex b9035aec..ae79b130 100644--- a/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift+++ b/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift@@ -60,6 +60,25 @@ final class MeetingMicrophoneCaptureTests: XCTestCase {[phase1] chunks=\(result.writerRotationCount) discontinuities=\(result.writerDiscontinuities) \backpressure=\(result.writerBackpressureEvents) health=\(result.finalHealthStatus) liveCopyNils=\(result.liveCopyNilCount)""")+ if let readback = result.voiceProcessingReadback {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ let json = String(decoding: try encoder.encode(readback), as: UTF8.self)+ print("[phase1] voice-processing-readback=\n\(json)")+ } else {+ XCTFail("running VPIO capture did not produce a read-back snapshot")+ }+#if DEBUG+ if let acoustic = result.acousticTrialB {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ let json = String(decoding: try encoder.encode(acoustic), as: UTF8.self)+ print("[phase0-trial-b] numeric-sidecar=\n\(json)")+ XCTAssertEqual(acoustic.schemaVersion, MeetingVPIOAcousticTrialBReport.currentSchemaVersion)+ XCTAssertFalse(acoustic.runs.isEmpty)+ XCTAssertTrue(acoustic.stimulus.withinSafetyBounds)+ }+#endifXCTAssertGreaterThanOrEqual(validRate, 0.99)XCTAssertEqual(result.writerDiscontinuities, 0)XCTAssertEqual(result.writerBackpressureEvents, 0)@@ -753,12 +772,19 @@ extension MeetingMicrophoneCaptureTests {MeetingMicrophoneIdentity(captureDeviceID: id, coreAudioUID: uid, displayName: name, role: role)}- func testElectionRoleTransfersOnlyWhenElectedDeviceEqualsOriginal() {+ func testElectionSameDevicePreservesIdentityAndNeutralizesRole() {let original = self.micIdentity(id: "dev-1", uid: "uid-1", name: "MacBook Mic", role: .personal)let catalog = [original]let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: catalog, defaultInputUID: "uid-1")+ var expected = original+ expected.role = .unknown+ XCTAssertEqual(elected.identity, expected)XCTAssertEqual(elected.identity.captureDeviceID, "dev-1")- XCTAssertEqual(elected.role, .personal)+ XCTAssertEqual(elected.identity.coreAudioUID, "uid-1")+ XCTAssertEqual(elected.identity.role, .unknown)+ XCTAssertEqual(elected.role, .unknown)+ XCTAssertNotEqual(elected.role, .personal)+ XCTAssertNotEqual(elected.role, .shared)}func testElectionDifferentDeviceReturnsUnknown() {@@ -770,11 +796,56 @@ extension MeetingMicrophoneCaptureTests {XCTAssertEqual(elected.role, .unknown)}- func testElectionFallsBackToOriginalWhenNoSystemDefault() {+ func testElectionFallsBackToOriginalIdentityWithUnknownRoleWhenNoSystemDefault() {let original = self.micIdentity(id: "dev-1", uid: "uid-1", name: "MacBook Mic", role: .personal)let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: [original], defaultInputUID: nil)- XCTAssertEqual(elected.identity, original)- XCTAssertEqual(elected.role, .personal)+ var expected = original+ expected.role = .unknown+ XCTAssertEqual(elected.identity, expected)+ XCTAssertEqual(elected.identity.captureDeviceID, "dev-1")+ XCTAssertEqual(elected.identity.coreAudioUID, "uid-1")+ XCTAssertEqual(elected.identity.role, .unknown)+ XCTAssertEqual(elected.role, .unknown)+ XCTAssertNotEqual(elected.role, .personal)+ XCTAssertNotEqual(elected.role, .shared)+ }++ func testElectionAndPreselectionNeverEmitLegacyPersonalOrSharedRoles() {+ for legacyRole in [MeetingMicrophoneRole.personal, .shared] {+ let original = self.micIdentity(id: "dev-1", uid: "uid-1", name: "MacBook Mic", role: legacyRole)+ let sameDevice = MeetingCaptureDeviceElection.decide(+ original: original, catalog: [original], defaultInputUID: "uid-1"+ )+ XCTAssertEqual(sameDevice.identity.captureDeviceID, "dev-1")+ XCTAssertEqual(sameDevice.identity.coreAudioUID, "uid-1")+ XCTAssertEqual(sameDevice.identity.role, .unknown)+ XCTAssertEqual(sameDevice.role, .unknown)+ XCTAssertNotEqual(sameDevice.role, .personal)+ XCTAssertNotEqual(sameDevice.role, .shared)++ let unmatchedDefault = MeetingCaptureDeviceElection.decide(+ original: original, catalog: [original], defaultInputUID: "missing-uid"+ )+ var expectedFallback = original+ expectedFallback.role = .unknown+ XCTAssertEqual(unmatchedDefault.identity, expectedFallback)+ XCTAssertEqual(unmatchedDefault.identity.captureDeviceID, "dev-1")+ XCTAssertEqual(unmatchedDefault.identity.coreAudioUID, "uid-1")+ XCTAssertEqual(unmatchedDefault.role, .unknown)++ let selection = MeetingMicrophonePreselection.select(+ identities: [original],+ savedDeviceID: "dev-1",+ savedRole: legacyRole,+ systemDefaultUID: "uid-1",+ preferredInputUID: nil,+ systemDefaultCaptureID: nil+ )+ XCTAssertEqual(selection.deviceID, "dev-1")+ XCTAssertEqual(selection.role, .unknown)+ XCTAssertNotEqual(selection.role, .personal)+ XCTAssertNotEqual(selection.role, .shared)+ }}// MARK: Codable: captureEras@@ -926,21 +997,29 @@ extension MeetingMicrophoneCaptureTests {// MARK: - Not covered here: phase-machine transitions (file-private, hardware-driven —// needs the Phase1 probe) and writer finalizationSlots saturation (no test seam exposed).- func testPreselectionBuiltInMicDefaultsToPersonalRole() {+ func testPreselectionBuiltInMicDefaultsToUnknownRole() {let builtIn = MeetingMicrophoneIdentity(captureDeviceID: "built-in", coreAudioUID: "BuiltInMicrophoneDevice", displayName: "MacBook Pro Microphone")let selection = MeetingMicrophonePreselection.select(identities: [builtIn], savedDeviceID: "other-device", savedRole: .personal,systemDefaultUID: "BuiltInMicrophoneDevice", preferredInputUID: nil, systemDefaultCaptureID: nil)XCTAssertEqual(selection.deviceID, "built-in")- XCTAssertEqual(selection.role, .personal)+ XCTAssertEqual(selection.role, .unknown)+ XCTAssertNotEqual(selection.role, .personal)+ XCTAssertNotEqual(selection.role, .shared)}- func testElectionBuiltInMicDefaultsToPersonalRole() {+ func testElectionBuiltInMicDefaultsToUnknownRole() {let original = MeetingMicrophoneIdentity(captureDeviceID: "airpods", coreAudioUID: "EC:input", displayName: "AirPods", role: .personal)let builtIn = MeetingMicrophoneIdentity(captureDeviceID: "built-in", coreAudioUID: "BuiltInMicrophoneDevice", displayName: "MacBook Pro Microphone")let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: [builtIn], defaultInputUID: "BuiltInMicrophoneDevice")- XCTAssertEqual(elected.role, .personal)+ XCTAssertEqual(elected.identity.captureDeviceID, "built-in")+ XCTAssertEqual(elected.identity.coreAudioUID, "BuiltInMicrophoneDevice")+ XCTAssertEqual(elected.identity.displayName, "MacBook Pro Microphone")+ XCTAssertEqual(elected.identity.role, .unknown)+ XCTAssertEqual(elected.role, .unknown)+ XCTAssertNotEqual(elected.role, .personal)+ XCTAssertNotEqual(elected.role, .shared)}}diff --git a/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift b/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swiftindex 4c06263e..68dd1a2e 100644--- a/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift+++ b/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift@@ -82,12 +82,12 @@ final class MeetingPhase4ClassificationTests: XCTestCase {func testContainsLocalSpeechRescueStillWinsUnderVoiceProcessing() {var vpioTurn = self.turn(start: 0, end: 3, text: "some text", overlapsRemote: false)- vpioTurn.signalVerdict = .containsLocalSpeech+ vpioTurn.signalVerdict = .residualNotExplainedvpioTurn.isLikelyEcho = truelet (classified, _) = MeetingProcessingPipeline.classifyMicrophoneTurns([vpioTurn], eras: self.singleEra(.voiceProcessing), applicationTrackID: nil, segments: [], fallbackSpeakerID: nil)- XCTAssertFalse(classified[0].effectiveEcho, "the rescue rule is untouched: containsLocalSpeech always wins")+ XCTAssertFalse(classified[0].effectiveEcho, "the rescue rule is untouched: residualNotExplained always wins")}// MARK: - 3. Two-voice election via localSpeakerEvidence + selectLocalCluster (VPIO turns, all scored)diff --git a/Tests/FluidDictationIntegrationTests/MeetingProcessingConfigurationTests.swift b/Tests/FluidDictationIntegrationTests/MeetingProcessingConfigurationTests.swiftnew file mode 100644index 00000000..5c0a0fd0--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingProcessingConfigurationTests.swift@@ -0,0 +1,134 @@+@testable import FluidVoice_Debug+import XCTest++final class MeetingProcessingConfigurationTests: XCTestCase {+ func testFinalConfigurationDefaultsAreParakeetTDTv2EnglishWithoutPostProcessing() {+ let config = MeetingFinalProcessingConfiguration()+ XCTAssertEqual(config.asrModel, "parakeet-tdt-v2")+ XCTAssertEqual(config.languageCode, "en")+ XCTAssertFalse(config.vocabularyBoostingEnabled)+ XCTAssertFalse(config.pronunciationMatchingEnabled)+ XCTAssertFalse(config.customDictionaryRewritingEnabled)+ XCTAssertFalse(config.experimentalUnifiedFinalEnabled)+ XCTAssertEqual(config.pipelineVersion, MeetingFinalProcessingConfiguration.defaultPipelineVersion)+ XCTAssertEqual(config.diarizationFingerprint, MeetingProcessingCheckpoint.currentDiarizationFingerprint)+ }++ @MainActor+ func testFinalConfigurationDefaultPipelineVersionMatchesPipelineConstant() {+ XCTAssertEqual(+ MeetingFinalProcessingConfiguration.defaultPipelineVersion,+ MeetingProcessingPipeline.pipelineVersion+ )+ }++ func testFinalIdentityFingerprintIsStableAndCoversEveryEffectiveField() {+ let base = MeetingFinalProcessingConfiguration()+ XCTAssertEqual(base.identityFingerprint, MeetingFinalProcessingConfiguration().identityFingerprint)+ XCTAssertTrue(base.identityFingerprint.hasPrefix("v\(MeetingConfigFingerprintEncoding.version);"))++ let variants: [MeetingFinalProcessingConfiguration] = [+ MeetingFinalProcessingConfiguration(asrModel: "parakeet-tdt-v3"),+ MeetingFinalProcessingConfiguration(languageCode: "de"),+ MeetingFinalProcessingConfiguration(vocabularyBoostingEnabled: true),+ MeetingFinalProcessingConfiguration(pronunciationMatchingEnabled: true),+ MeetingFinalProcessingConfiguration(customDictionaryRewritingEnabled: true),+ MeetingFinalProcessingConfiguration(experimentalUnifiedFinalEnabled: true),+ MeetingFinalProcessingConfiguration(diarizationFingerprint: "other"),+ MeetingFinalProcessingConfiguration(pipelineVersion: base.pipelineVersion + 1),+ ]+ for variant in variants {+ XCTAssertNotEqual(variant.identityFingerprint, base.identityFingerprint)+ }+ XCTAssertTrue(base.identityFingerprint.contains(MeetingProcessingCheckpoint.currentDiarizationFingerprint))+ XCTAssertTrue(base.identityFingerprint.contains("pipeline"))+ XCTAssertTrue(base.identityFingerprint.contains(String(base.pipelineVersion)))+ }++ func testFinalIdentityFingerprintHasNoDelimiterCollisions() {+ let splitAt = MeetingFinalProcessingConfiguration(asrModel: "a@b", languageCode: "c")+ let splitAtLanguage = MeetingFinalProcessingConfiguration(asrModel: "a", languageCode: "b@c")+ XCTAssertNotEqual(splitAt.identityFingerprint, splitAtLanguage.identityFingerprint)++ let semicolonKey = MeetingFinalProcessingConfiguration(asrModel: "x;lang=de")+ let plain = MeetingFinalProcessingConfiguration(languageCode: "de", vocabularyBoostingEnabled: false)+ XCTAssertNotEqual(semicolonKey.identityFingerprint, plain.identityFingerprint)+ }++ func testLiveCaptionsDefaultsMatchStreamingEouIdentity() {+ let config = MeetingLiveCaptionsConfiguration()+ XCTAssertEqual(config.modelRepositoryID, "FluidInference/parakeet-realtime-eou-120m-coreml")+ XCTAssertEqual(config.modelVariant, "160ms")+ XCTAssertEqual(config.chunkSizeMilliseconds, 160)+ XCTAssertEqual(config.languageCode, "en")+ XCTAssertEqual(config.modelRevision, "main")+ }++ func testLiveCaptionsFingerprintIsStableAndDoesNotClaimResolvedRevision() {+ let base = MeetingLiveCaptionsConfiguration()+ XCTAssertEqual(base.identityFingerprint, MeetingLiveCaptionsConfiguration().identityFingerprint)+ XCTAssertTrue(base.identityFingerprint.hasPrefix("v\(MeetingConfigFingerprintEncoding.version);"))+ XCTAssertTrue(base.identityFingerprint.contains("FluidInference/parakeet-realtime-eou-120m-coreml"))+ XCTAssertTrue(base.identityFingerprint.contains("160ms"))+ XCTAssertTrue(base.identityFingerprint.contains("unresolved"))+ // A resolved HF pin would be a full commit SHA; the loader only selects the branch.+ let hex = CharacterSet(charactersIn: "0123456789abcdef")+ XCTAssertFalse(+ base.modelRevision.count == 40+ && base.modelRevision.unicodeScalars.allSatisfy(hex.contains)+ )+ }++ func testLiveCaptionsFingerprintChangesForEveryField() {+ let base = MeetingLiveCaptionsConfiguration()+ let variants: [MeetingLiveCaptionsConfiguration] = [+ MeetingLiveCaptionsConfiguration(modelRepositoryID: "FluidInference/parakeet-realtime-eou-200m-coreml"),+ MeetingLiveCaptionsConfiguration(modelVariant: "320ms"),+ MeetingLiveCaptionsConfiguration(modelRevision: "release"),+ MeetingLiveCaptionsConfiguration(chunkSizeMilliseconds: 320),+ MeetingLiveCaptionsConfiguration(languageCode: "de"),+ ]+ for variant in variants {+ XCTAssertNotEqual(variant.identityFingerprint, base.identityFingerprint)+ }+ }++ func testLiveCaptionsFingerprintHasNoDelimiterCollisions() {+ let splitRepo = MeetingLiveCaptionsConfiguration(modelRepositoryID: "x/y", modelVariant: "z")+ let splitVariant = MeetingLiveCaptionsConfiguration(modelRepositoryID: "x", modelVariant: "y/z")+ XCTAssertNotEqual(splitRepo.identityFingerprint, splitVariant.identityFingerprint)+ }++ func testConfigurationsBehaveAsEquatableValueSnapshots() {+ let final = MeetingFinalProcessingConfiguration()+ let finalCopy = final+ XCTAssertEqual(final, finalCopy)+ XCTAssertEqual(final, MeetingFinalProcessingConfiguration())+ XCTAssertNotEqual(final, MeetingFinalProcessingConfiguration(vocabularyBoostingEnabled: true))+ XCTAssertNotEqual(final, MeetingFinalProcessingConfiguration(pipelineVersion: final.pipelineVersion + 1))++ let captions = MeetingLiveCaptionsConfiguration()+ XCTAssertEqual(captions, MeetingLiveCaptionsConfiguration())+ XCTAssertNotEqual(captions, MeetingLiveCaptionsConfiguration(modelRevision: "abc123"))+ }++ func testFingerprintsMatchForCanonicallyEquivalentUnicode() {+ let composedFinal = MeetingFinalProcessingConfiguration(languageCode: "\u{E9}")+ let decomposedFinal = MeetingFinalProcessingConfiguration(languageCode: "e\u{301}")+ XCTAssertEqual(composedFinal, decomposedFinal)+ XCTAssertEqual(composedFinal.identityFingerprint, decomposedFinal.identityFingerprint)++ let composedCaptions = MeetingLiveCaptionsConfiguration(modelRepositoryID: "Caf\u{E9}")+ let decomposedCaptions = MeetingLiveCaptionsConfiguration(modelRepositoryID: "Cafe\u{301}")+ XCTAssertEqual(composedCaptions, decomposedCaptions)+ XCTAssertEqual(composedCaptions.identityFingerprint, decomposedCaptions.identityFingerprint)+ }++ func testFingerprintGoldenEncodingLocksFramingStability() {+ let encoded = MeetingConfigFingerprintEncoding.encode([+ ("asr", "a@b"),+ ("lang", "e\u{301}"),+ ])+ XCTAssertEqual(encoded, "v1;3:asr=3:a@b;4:lang=2:\u{E9}")+ }+}diff --git a/Tests/FluidDictationIntegrationTests/MeetingProviderOptionsTests.swift b/Tests/FluidDictationIntegrationTests/MeetingProviderOptionsTests.swiftnew file mode 100644index 00000000..4e524369--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingProviderOptionsTests.swift@@ -0,0 +1,97 @@+@testable import FluidVoice_Debug+import XCTest++final class MeetingProviderOptionsTests: XCTestCase {+ func testDefaultMeetingConfigurationResolvesToPinnedParakeetTDTv2EnglishPolicy() throws {+ let options = try MeetingProviderOptions.resolve(MeetingFinalProcessingConfiguration())+ XCTAssertEqual(options.model, .parakeetTDTv2)+ XCTAssertFalse(options.vocabularyBoostingEnabled)+ XCTAssertFalse(options.pronunciationMatchingEnabled)+ XCTAssertFalse(options.customDictionaryRewritingEnabled)+ XCTAssertFalse(options.experimentalUnifiedFinalEnabled)+ }++ func testP1DefaultASRModelMatchesSpeechModelRawValue() {+ XCTAssertEqual(+ MeetingFinalProcessingConfiguration.defaultASRModel,+ SettingsStore.SpeechModel.parakeetTDTv2.rawValue+ )+ }++ func testResolveRejectsUnsupportedModelsWithoutRelabeling() {+ let unsupportedModels = [+ "parakeet-tdt", // raw value of the v3 multilingual model+ "parakeet-tdt-v3",+ "parakeet-tdt-v99",+ "whisper-tiny",+ "",+ ]+ for asrModel in unsupportedModels {+ let configuration = MeetingFinalProcessingConfiguration(asrModel: asrModel)+ XCTAssertThrowsError(try MeetingProviderOptions.resolve(configuration)) { error in+ XCTAssertEqual(+ error as? MeetingProviderOptionsError,+ .unsupportedASRModel(asrModel)+ )+ }+ }+ }++ func testResolveRejectsNonEnglishLanguageCode() {+ let configuration = MeetingFinalProcessingConfiguration(languageCode: "de")+ XCTAssertThrowsError(try MeetingProviderOptions.resolve(configuration)) { error in+ XCTAssertEqual(+ error as? MeetingProviderOptionsError,+ .unsupportedLanguageCode("de")+ )+ }+ }++ func testResolveRejectsEveryEnhancementFlag() {+ let cases: [(MeetingFinalProcessingConfiguration, String)] = [+ (MeetingFinalProcessingConfiguration(vocabularyBoostingEnabled: true), "vocabularyBoosting"),+ (MeetingFinalProcessingConfiguration(pronunciationMatchingEnabled: true), "pronunciationMatching"),+ (MeetingFinalProcessingConfiguration(customDictionaryRewritingEnabled: true), "customDictionaryRewriting"),+ (MeetingFinalProcessingConfiguration(experimentalUnifiedFinalEnabled: true), "experimentalUnifiedFinal"),+ ]+ for (configuration, feature) in cases {+ XCTAssertThrowsError(try MeetingProviderOptions.resolve(configuration)) { error in+ XCTAssertEqual(+ error as? MeetingProviderOptionsError,+ .unsupportedFeature(feature)+ )+ }+ }+ }++ func testMeetingConstructorPinsModelOverrideImmutably() throws {+ let provider = try FluidAudioProvider(meetingConfiguration: MeetingFinalProcessingConfiguration())+ // The Intel stub stores no modelOverride; the pinning assertion is arm64-only.+ #if arch(arm64)+ XCTAssertEqual(provider.modelOverride, .parakeetTDTv2)+ #endif+ XCTAssertFalse(provider.isWordBoostingActive)+ XCTAssertEqual(provider.boostedVocabularyTermsCount, 0)+ }++ func testMeetingConstructorRejectsUnsupportedConfiguration() {+ let configuration = MeetingFinalProcessingConfiguration(asrModel: "parakeet-tdt-v3")+ XCTAssertThrowsError(try FluidAudioProvider(meetingConfiguration: configuration)) { error in+ XCTAssertEqual(+ error as? MeetingProviderOptionsError,+ .unsupportedASRModel("parakeet-tdt-v3")+ )+ }+ }++ #if arch(arm64)+ func testLegacyConstructorsKeepDynamicModelResolution() {+ XCTAssertNil(FluidAudioProvider().modelOverride)+ XCTAssertNil(FluidAudioProvider(configureWordBoosting: false).modelOverride)+ XCTAssertEqual(+ FluidAudioProvider(modelOverride: .parakeetTDT, configureWordBoosting: false).modelOverride,+ .parakeetTDT+ )+ }+ #endif+}diff --git a/Tests/FluidDictationIntegrationTests/MeetingReferenceAttributionContractsTests.swift b/Tests/FluidDictationIntegrationTests/MeetingReferenceAttributionContractsTests.swiftnew file mode 100644index 00000000..9f0cd886--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingReferenceAttributionContractsTests.swift@@ -0,0 +1,501 @@+import Foundation+import XCTest+@testable import FluidVoice_Debug++final class MeetingReferenceAttributionContractsTests: XCTestCase {+ private func result(+ _ outcome: MeetingReferenceAttributionOutcome,+ duration: Double,+ epoch: UInt64 = 0,+ index: Int = 0,+ speech: MeetingReferenceAttributionSpeechState = .unknown,+ residual: MeetingReferenceAttributionResidualVariant? = nil,+ reasons: [MeetingReferenceAttributionReasonCode] = []+ ) -> MeetingReferenceAttributionFrameResult {+ MeetingReferenceAttributionFrameResult(+ frameIndex: index, epoch: epoch, startSeconds: Double(index) * duration,+ durationSeconds: duration, outcome: outcome, speechState: speech,+ residualVariant: residual, reasons: reasons,+ metrics: MeetingReferenceAttributionMetrics(+ delaySeconds: 0.04, convergence: 0.9, erlDB: 5, erleDB: 12,+ originalEnergy: 1, residualEnergy: 0.25,+ residualToOriginalEnergyRatio: 0.25),+ fallback: outcome == .unscored ? .originalMicrophone : .none)+ }++ func testSidecarJSONRoundTripIsNumericOnlyAndPreservesResidualIdentity() throws {+ let configuration = MeetingReferenceAttributionConfiguration(candidateID: "native-multiband")+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "native-multiband", engineVersion: "candidate-a-v1",+ sourceHashes: [+ .init(name: "accelerate", hash: "sha256:abc"),+ .init(name: "source", hash: "sha256:def")+ ], buildHash: "sha256:build", configurationHash: configuration.configurationHash)+ let frames = [+ result(.acceptedNearEndSpeech, duration: 0.1, speech: .nearEndSpeech,+ residual: .linearResidual, reasons: [.residualSpeechDetected]),+ result(.likelyPlaybackOnly, duration: 0.1, index: 1, speech: .noSpeech,+ residual: .suppressedResidual, reasons: [.playbackAttributed])+ ]+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ epochs: [.init(epoch: 0)], frameResults: frames)+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.sortedKeys]+ let data = try encoder.encode(sidecar)+ let decoded = try JSONDecoder().decode(MeetingReferenceAttributionSidecar.self, from: data)++ XCTAssertEqual(decoded, sidecar)+ XCTAssertEqual(sidecar.sidecarHash, MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ epochs: [.init(epoch: 0)], frameResults: frames).sidecarHash)+ let json = String(decoding: data, as: UTF8.self)+ for forbidden in ["samples", "transcript", "embedding", "windowTitle", "absolutePath"] {+ XCTAssertFalse(json.contains(forbidden), "sidecar leaked (forbidden)")+ }+ XCTAssertNotEqual(frames[0].residualVariant, frames[1].residualVariant)+ }++ func testCanonicalEncodingSortsCountEntriesAndInputOrderDoesNotAffectHash() throws {+ let configuration = MeetingReferenceAttributionConfiguration(candidateID: "canonical")+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let first = result(.acceptedNearEndSpeech, duration: 0.1, index: 0,+ speech: .nearEndSpeech,+ reasons: [.residualSpeechDetected, .mixedEvidence])+ let second = result(.unscored, duration: 0.2, index: 1,+ reasons: [.referenceGap, .unknownSpeechState])+ let left = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [first, second])+ let right = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [second, first])+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.sortedKeys]+ let leftData = try encoder.encode(left)+ let rightData = try encoder.encode(right)++ XCTAssertEqual(leftData, rightData)+ XCTAssertEqual(left.sidecarHash, right.sidecarHash)+ let object = try XCTUnwrap(JSONSerialization.jsonObject(with: leftData) as? [String: Any])+ let aggregate = try XCTUnwrap(object["aggregate"] as? [String: Any])+ XCTAssertTrue(aggregate["outcomeCounts"] is [[String: Any]])+ XCTAssertTrue(aggregate["reasonCounts"] is [[String: Any]])+ let entries = try XCTUnwrap(aggregate["reasonCounts"] as? [[String: Any]])+ XCTAssertEqual(entries.compactMap { $0["reason"] as? String },+ entries.compactMap { $0["reason"] as? String }.sorted())+ }++ func testFailOpenPreservesUnknownAndOriginalFallback() {+ let frame = MeetingReferenceAttributionFrame(+ frameIndex: 4, epoch: 2, startSeconds: .nan, durationSeconds: .nan,+ microphone: .init(samples: [0], valid: [true]), reference: nil)+ let output = MeetingReferenceAttributionFrameResult.failOpen(frame: frame, reason: .referenceAbsent)++ XCTAssertEqual(output.outcome, .unscored)+ XCTAssertEqual(output.speechState, .unknown)+ XCTAssertNil(output.residualVariant)+ XCTAssertEqual(output.fallback, .originalMicrophone)+ XCTAssertTrue(output.reasons.contains(.referenceAbsent))+ XCTAssertTrue(output.reasons.contains(.unknownSpeechState))+ XCTAssertFalse(output.reasons.contains(.silenceMeasured))+ XCTAssertFalse(output.reasons.contains(.noSpeechMeasured))++ let unsafe = MeetingReferenceAttributionFrameResult(+ frameIndex: 0, epoch: 0, startSeconds: 0, durationSeconds: 0.01,+ outcome: .acceptedNearEndSpeech, speechState: .unknown)+ XCTAssertEqual(unsafe.outcome, .unscored)+ XCTAssertEqual(unsafe.fallback, .originalMicrophone)+ }++ func testAggregateCoverageUsesDurationWeightsAndCounts() {+ let frames = [+ result(.likelyPlaybackOnly, duration: 0.1, speech: .noSpeech),+ result(.acceptedNearEndSpeech, duration: 0.3, index: 1, speech: .nearEndSpeech),+ result(.mixedOrUncertain, duration: 0.2, index: 2, speech: .mixedSpeech),+ result(.scopeLimited, duration: 0.1, index: 3, reasons: [.referenceScopeLimited]),+ result(.unscored, duration: 0.3, index: 4, reasons: [.referenceGap, .unknownSpeechState])+ ]+ let aggregate = MeetingReferenceAttributionAggregate(frameResults: frames)++ XCTAssertEqual(aggregate.coverage.totalDurationSeconds, 1, accuracy: 0.000001)+ XCTAssertEqual(aggregate.coverage.scoredDurationSeconds, 0.6, accuracy: 0.000001)+ XCTAssertEqual(aggregate.coverage.likelyPlaybackOnlyDurationSeconds, 0.1, accuracy: 0.000001)+ XCTAssertEqual(aggregate.coverage.acceptedNearEndSpeechDurationSeconds, 0.3, accuracy: 0.000001)+ XCTAssertEqual(aggregate.coverage.unscoredFraction, 0.3, accuracy: 0.000001)+ XCTAssertEqual(aggregate.outcomeCounts[.acceptedNearEndSpeech], 1)+ XCTAssertEqual(aggregate.outcomeCounts[.unscored], 1)+ XCTAssertEqual(aggregate.reasonCounts[.referenceGap], 1)+ }++ func testScopeLimitedAndUnknownRemainDistinctFromMeasuredNoSpeech() {+ let scopeLimited = result(.scopeLimited, duration: 0.1, reasons: [.referenceScopeLimited])+ let unknown = result(.unscored, duration: 0.1, index: 1, reasons: [.referenceAbsent, .unknownSpeechState])+ let silence = result(.likelyPlaybackOnly, duration: 0.1, index: 2, speech: .silence,+ residual: .suppressedResidual, reasons: [.silenceMeasured])+ let aggregate = MeetingReferenceAttributionAggregate(frameResults: [scopeLimited, unknown, silence])++ XCTAssertEqual(aggregate.outcomeCounts[.scopeLimited], 1)+ XCTAssertEqual(aggregate.outcomeCounts[.unscored], 1)+ XCTAssertEqual(aggregate.outcomeCounts[.likelyPlaybackOnly], 1)+ XCTAssertEqual(aggregate.reasonCounts[.referenceAbsent], 1)+ XCTAssertEqual(silence.speechState, .silence)+ XCTAssertNotEqual(unknown.speechState, silence.speechState)+ }++ func testIdentityAndConfigurationAreDeterministicAcrossHashOrder() {+ let first = MeetingReferenceAttributionConfiguration(candidateID: "a")+ let second = MeetingReferenceAttributionConfiguration(candidateID: "a")+ XCTAssertEqual(first.configurationHash, second.configurationHash)++ let one = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1",+ sourceHashes: [.init(name: "z", hash: "2"), .init(name: "a", hash: "1")],+ configurationHash: first.configurationHash)+ let two = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1",+ sourceHashes: [.init(name: "a", hash: "1"), .init(name: "z", hash: "2")],+ configurationHash: second.configurationHash)+ XCTAssertEqual(one.stableIdentity, two.stableIdentity)+ XCTAssertNotEqual(one.stableIdentity, MeetingReferenceAttributionEngineIdentity(+ engineID: "different", engineVersion: "1", configurationHash: first.configurationHash).stableIdentity)+ }++ func testInvalidNonFiniteMetricsAndCoverageDoNotBecomeMeasuredValues() {+ let metrics = MeetingReferenceAttributionMetrics(+ delaySeconds: .infinity, delayJitterSeconds: -.nan, convergence: 2,+ erlDB: .nan, erleDB: -.infinity, candidateScore: -0.1,+ originalEnergy: -.infinity, residualEnergy: .nan,+ residualToOriginalEnergyRatio: -0.1, processingMilliseconds: .nan,+ peakMemoryBytes: -1)+ XCTAssertNil(metrics.delaySeconds)+ XCTAssertNil(metrics.delayJitterSeconds)+ XCTAssertNil(metrics.convergence)+ XCTAssertNil(metrics.erlDB)+ XCTAssertNil(metrics.candidateScore)+ XCTAssertNil(metrics.originalEnergy)+ XCTAssertNil(metrics.residualEnergy)+ XCTAssertNil(metrics.residualToOriginalEnergyRatio)+ XCTAssertNil(metrics.processingMilliseconds)+ XCTAssertNil(metrics.peakMemoryBytes)++ XCTAssertNil(MeetingReferenceAttributionCoverage(+ totalDurationSeconds: 1, scoredDurationSeconds: 1.1,+ likelyPlaybackOnlyDurationSeconds: 0, acceptedNearEndSpeechDurationSeconds: 0,+ mixedOrUncertainDurationSeconds: 0, scopeLimitedDurationSeconds: 0,+ unscoredDurationSeconds: 0, frameCount: 1))+ }++ func testFrameValidationRejectsBadShapeNonFinitePCMAndOverflowTiming() {+ let badShape = MeetingReferenceAttributionFrame(+ frameIndex: 0, epoch: 0, startSeconds: 0, durationSeconds: 0.01,+ microphone: .init(samples: [0, 1], valid: [true]), reference: nil)+ XCTAssertFalse(badShape.isValid)+ XCTAssertTrue(badShape.validationReasons.contains(.invalidInput))+ XCTAssertThrowsError(try badShape.validate())++ let badPCM = MeetingReferenceAttributionFrame(+ frameIndex: 0, epoch: 0, startSeconds: 0, durationSeconds: 0.01,+ microphone: .init(samples: [.nan], valid: [true]), reference: nil)+ XCTAssertTrue(badPCM.validationReasons.contains(.nonFiniteInput))++ let overflow = MeetingReferenceAttributionFrame(+ frameIndex: 0, epoch: 0, startSeconds: .greatestFiniteMagnitude,+ durationSeconds: .greatestFiniteMagnitude,+ microphone: .init(samples: [0], valid: [true]), reference: nil)+ XCTAssertFalse(overflow.hasValidTiming)+ }++ func testFrameResultDecodeRejectsOutcomeSpeechAndFallbackContradictions() throws {+ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [result(.acceptedNearEndSpeech, duration: 0.1, speech: .nearEndSpeech)])+ var object = try XCTUnwrap(JSONSerialization.jsonObject(+ with: JSONEncoder().encode(sidecar)) as? [String: Any])+ var frames = try XCTUnwrap(object["frameResults"] as? [[String: Any]])+ frames[0]["speechState"] = MeetingReferenceAttributionSpeechState.unknown.rawValue+ object["frameResults"] = frames+ let corrupt = try JSONSerialization.data(withJSONObject: object)+ XCTAssertThrowsError(try JSONDecoder().decode(MeetingReferenceAttributionSidecar.self, from: corrupt))+ }++ func testPlaybackOnlyConflictingSpeechDegradesAndNoncanonicalReasonsAreRejected() throws {+ let conflicting = result(+ .likelyPlaybackOnly, duration: 0.1, speech: .nearEndSpeech,+ reasons: [.playbackAttributed]+ )+ XCTAssertEqual(conflicting.outcome, .mixedOrUncertain)+ XCTAssertEqual(conflicting.speechState, .nearEndSpeech)+ XCTAssertTrue(conflicting.reasons.contains(.mixedEvidence))++ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [result(+ .likelyPlaybackOnly, duration: 0.1, speech: .noSpeech,+ reasons: [.playbackAttributed, .noSpeechMeasured])])+ var object = try XCTUnwrap(JSONSerialization.jsonObject(+ with: JSONEncoder().encode(sidecar)) as? [String: Any])+ var frames = try XCTUnwrap(object["frameResults"] as? [[String: Any]])+ let canonicalReasons = try XCTUnwrap(frames[0]["reasons"] as? [String])+ frames[0]["reasons"] = Array(canonicalReasons.reversed())+ object["frameResults"] = frames+ let noncanonical = try JSONSerialization.data(withJSONObject: object)+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionSidecar.self, from: noncanonical))+ }++ func testSidecarDecodeRejectsAggregateEpochAndConfigurationMismatches() throws {+ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [result(.acceptedNearEndSpeech, duration: 0.1, speech: .nearEndSpeech)])+ let encoder = JSONEncoder()+ var object = try XCTUnwrap(JSONSerialization.jsonObject(+ with: encoder.encode(sidecar)) as? [String: Any])++ var identityObject = try XCTUnwrap(object["engineIdentity"] as? [String: Any])+ identityObject["configurationHash"] = "mismatch"+ object["engineIdentity"] = identityObject+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionSidecar.self,+ from: JSONSerialization.data(withJSONObject: object)))++ object = try XCTUnwrap(JSONSerialization.jsonObject(+ with: encoder.encode(sidecar)) as? [String: Any])+ var epochs = try XCTUnwrap(object["epochs"] as? [[String: Any]])+ epochs.append(epochs[0])+ object["epochs"] = epochs+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionSidecar.self,+ from: JSONSerialization.data(withJSONObject: object)))++ object = try XCTUnwrap(JSONSerialization.jsonObject(+ with: encoder.encode(sidecar)) as? [String: Any])+ var aggregate = try XCTUnwrap(object["aggregate"] as? [String: Any])+ var coverage = try XCTUnwrap(aggregate["coverage"] as? [String: Any])+ coverage["totalDurationSeconds"] = 0.2+ aggregate["coverage"] = coverage+ object["aggregate"] = aggregate+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionSidecar.self,+ from: JSONSerialization.data(withJSONObject: object)))+ }++ func testProgrammaticSidecarExposesDuplicateFrameKeyAsInvalid() {+ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let frame = result(.acceptedNearEndSpeech, duration: 0.1, speech: .nearEndSpeech)+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [frame, frame])+ XCTAssertEqual(sidecar.validationError, .invalid("duplicate epoch/frame key"))+ XCTAssertFalse(sidecar.isValid)+ }++ func testProgrammaticDuplicateEpochsDoNotTrapAndRemainInvalid() {+ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let frame = result(.acceptedNearEndSpeech, duration: 0.1, speech: .nearEndSpeech)+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ epochs: [.init(epoch: 0), .init(epoch: 0)], frameResults: [frame])++ XCTAssertEqual(sidecar.epochs.count, 2)+ XCTAssertEqual(sidecar.validationError, .invalid("duplicate epoch"))+ XCTAssertFalse(sidecar.isValid)+ }++ func testContradictoryFrameResultsNormalizeToDecodableFailOpenValues() throws {+ let cases: [MeetingReferenceAttributionFrameResult] = [+ result(.acceptedNearEndSpeech, duration: 0.1, speech: .playbackSpeech,+ residual: .linearResidual, reasons: [.residualSpeechDetected]),+ result(.likelyPlaybackOnly, duration: 0.1, index: 1, speech: .unknown,+ residual: .suppressedResidual),+ MeetingReferenceAttributionFrameResult(+ frameIndex: 2, epoch: 0, startSeconds: 0.2, durationSeconds: 0.1,+ outcome: .unscored, speechState: .nearEndSpeech,+ residualVariant: .linearResidual, fallback: .none),+ MeetingReferenceAttributionFrameResult(+ frameIndex: 3, epoch: 0, startSeconds: 0.3, durationSeconds: 0.1,+ outcome: .scopeLimited, speechState: .unknown, fallback: .originalMicrophone),+ MeetingReferenceAttributionFrameResult(+ frameIndex: -1, epoch: 0, startSeconds: 0.4, durationSeconds: 0.1,+ outcome: .mixedOrUncertain, speechState: .mixedSpeech),+ MeetingReferenceAttributionFrameResult(+ frameIndex: 5, epoch: 0, startSeconds: .greatestFiniteMagnitude,+ durationSeconds: .greatestFiniteMagnitude, outcome: .mixedOrUncertain,+ speechState: .mixedSpeech)+ ]++ for (offset, value) in cases.enumerated() {+ let data = try JSONEncoder().encode(value)+ let decoded = try JSONDecoder().decode(+ MeetingReferenceAttributionFrameResult.self, from: data)+ XCTAssertEqual(decoded, value)+ XCTAssertEqual(decoded.fallback, decoded.outcome == .unscored+ ? .originalMicrophone : .none)+ if decoded.outcome == .unscored {+ XCTAssertEqual(decoded.speechState, .unknown)+ XCTAssertNil(decoded.residualVariant)+ XCTAssertEqual(decoded.durationSeconds, offset >= 4 ? 0 : 0.1)+ }+ }+ }++ func testSidecarDecodeRejectsOverlapAndAdversarialContractPayloads() throws {+ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1",+ sourceHashes: [.init(name: "source", hash: "sha256:1")],+ configuration: configuration)+ let frames = [+ result(.acceptedNearEndSpeech, duration: 0.1, speech: .nearEndSpeech),+ result(.likelyPlaybackOnly, duration: 0.1, index: 1, speech: .noSpeech)+ ]+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: frames)+ let encoder = JSONEncoder()++ func object() throws -> [String: Any] {+ try XCTUnwrap(JSONSerialization.jsonObject(with: encoder.encode(sidecar))+ as? [String: Any])+ }+ func assertRejects(_ object: [String: Any], _ label: String = "") throws {+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionSidecar.self,+ from: JSONSerialization.data(withJSONObject: object)), label)+ }++ var value = try object()+ value["schemaVersion"] = 999+ try assertRejects(value, "schema")++ value = try object()+ var badIdentity = try XCTUnwrap(value["engineIdentity"] as? [String: Any])+ badIdentity["engineID"] = "../leak"+ value["engineIdentity"] = badIdentity+ try assertRejects(value, "logical identity")++ value = try object()+ var duplicateIdentity = try XCTUnwrap(value["engineIdentity"] as? [String: Any])+ duplicateIdentity["sourceHashes"] = [+ ["name": "source", "hash": "sha256:1"],+ ["name": "source", "hash": "sha256:2"]+ ]+ value["engineIdentity"] = duplicateIdentity+ try assertRejects(value, "duplicate source")++ value = try object()+ var aggregate = try XCTUnwrap(value["aggregate"] as? [String: Any])+ var wouldChange = try XCTUnwrap(aggregate["wouldChange"] as? [String: Any])+ wouldChange["words"] = -1+ aggregate["wouldChange"] = wouldChange+ value["aggregate"] = aggregate+ try assertRejects(value, "negative would-change")++ value = try object()+ aggregate = try XCTUnwrap(value["aggregate"] as? [String: Any])+ var coverage = try XCTUnwrap(aggregate["coverage"] as? [String: Any])+ coverage["scoredDurationSeconds"] = 0+ aggregate["coverage"] = coverage+ value["aggregate"] = aggregate+ try assertRejects(value, "inconsistent scored coverage")++ value = try object()+ aggregate = try XCTUnwrap(value["aggregate"] as? [String: Any])+ var resourceCost = try XCTUnwrap(aggregate["resourceCost"] as? [String: Any])+ resourceCost["processingMilliseconds"] = -1+ aggregate["resourceCost"] = resourceCost+ value["aggregate"] = aggregate+ try assertRejects(value, "negative resource cost")++ value = try object()+ aggregate = try XCTUnwrap(value["aggregate"] as? [String: Any])+ resourceCost = try XCTUnwrap(aggregate["resourceCost"] as? [String: Any])+ resourceCost["overBudget"] = true+ resourceCost["fallback"] = MeetingReferenceAttributionFallback.none.rawValue+ aggregate["resourceCost"] = resourceCost+ value["aggregate"] = aggregate+ try assertRejects(value, "over-budget fallback")++ value = try object()+ var frameObjects = try XCTUnwrap(value["frameResults"] as? [[String: Any]])+ frameObjects[1]["startSeconds"] = 0.05+ value["frameResults"] = frameObjects+ try assertRejects(value, "overlapping frames")+ }++ func testResourceCostRejectsNonFiniteJSON() {+ let data = Data(#"{"processingMilliseconds":1e400,"peakMemoryBytes":null,"derivedSidecarBytes":null,"overBudget":false,"fallback":"none"}"#.utf8)+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionResourceCost.self, from: data))+ }++ func testProgrammaticProvenanceAndEpochsRemainCanonicalAndRejectDuplicates() {+ let configuration = MeetingReferenceAttributionConfiguration()+ let duplicateIdentity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1",+ sourceHashes: [+ .init(name: "source", hash: "sha256:1"),+ .init(name: "source", hash: "sha256:2")+ ], configuration: configuration)+ let frames = [+ result(.acceptedNearEndSpeech, duration: 0.1, epoch: 0, speech: .nearEndSpeech),+ result(.acceptedNearEndSpeech, duration: 0.1, epoch: 1, index: 1,+ speech: .nearEndSpeech)+ ]+ let duplicateSourceSidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: duplicateIdentity, configuration: configuration,+ epochs: [.init(epoch: 0), .init(epoch: 1)], frameResults: frames)+ XCTAssertFalse(duplicateSourceSidecar.isValid)++ let validIdentity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let duplicateEpochSidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: validIdentity, configuration: configuration,+ epochs: [.init(epoch: 2), .init(epoch: 0), .init(epoch: 2)], frameResults: frames)++ XCTAssertEqual(duplicateEpochSidecar.epochs.map(\.epoch), [0, 2, 2])+ XCTAssertEqual(duplicateEpochSidecar.validationError, .invalid("duplicate epoch"))+ XCTAssertFalse(duplicateEpochSidecar.isValid)+ }++ func testSidecarRejectsUnsortedEpochsAndCrossEpochOverlap() throws {+ let configuration = MeetingReferenceAttributionConfiguration()+ let identity = MeetingReferenceAttributionEngineIdentity(+ engineID: "engine", engineVersion: "1", configuration: configuration)+ let first = result(.acceptedNearEndSpeech, duration: 0.1, epoch: 0,+ speech: .nearEndSpeech)+ let second = result(.likelyPlaybackOnly, duration: 0.1, epoch: 1, speech: .noSpeech)+ let sidecar = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [first, second])+ XCTAssertEqual(sidecar.epochs.map(\.epoch), [0, 1])++ var object = try XCTUnwrap(JSONSerialization.jsonObject(+ with: JSONEncoder().encode(sidecar)) as? [String: Any])+ object["epochs"] = [["epoch": 1], ["epoch": 0]]+ XCTAssertThrowsError(try JSONDecoder().decode(+ MeetingReferenceAttributionSidecar.self,+ from: JSONSerialization.data(withJSONObject: object)))++ let overlapping = MeetingReferenceAttributionSidecar(+ engineIdentity: identity, configuration: configuration,+ frameResults: [first, MeetingReferenceAttributionFrameResult(+ frameIndex: 0, epoch: 1, startSeconds: 0.05, durationSeconds: 0.1,+ outcome: .likelyPlaybackOnly, speechState: .noSpeech)])+ XCTAssertEqual(overlapping.validationError, .invalid("non-monotonic frame timing"))+ }+}diff --git a/Tests/FluidDictationIntegrationTests/MeetingReferenceSynchronizerTests.swift b/Tests/FluidDictationIntegrationTests/MeetingReferenceSynchronizerTests.swiftnew file mode 100644index 00000000..cd290b15--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingReferenceSynchronizerTests.swift@@ -0,0 +1,603 @@+@testable import FluidVoice_Debug+import XCTest++final class MeetingReferenceSynchronizerTests: XCTestCase {+ private let sampleRate = 16_000.0++ private func mic(+ _ sequence: Int,+ start: Int64,+ host: Double? = nil,+ value: Float = 1,+ count: Int = 160,+ route: String = "route-a",+ discontinuity: Bool = false+ ) -> MeetingMicrophonePCMFrame {+ MeetingMicrophonePCMFrame(sequenceNumber: sequence, sampleTime: start, hostTime: host,+ sampleRate: sampleRate, samples: [Float](repeating: value, count: count), routeIdentifier: route,+ discontinuity: discontinuity)+ }++ private func ref(+ _ sequence: Int,+ pts: Double,+ value: Float = 2,+ count: Int = 160,+ rate: Double? = nil,+ discontinuity: Bool = false+ ) -> MeetingReferencePCMFrame {+ MeetingReferencePCMFrame(sequenceNumber: sequence, presentationTime: pts,+ sampleRate: rate ?? sampleRate, samples: [Float](repeating: value, count: count), discontinuity: discontinuity)+ }++ func testExactTenMillisecondFramesAndMasks() {+ let input = MeetingReferenceSynchronizer()+ let result = input.synchronize(+ microphone: (0..<3).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) },+ reference: (0..<3).map { ref($0, pts: Double($0) * 0.01) }+ )+ XCTAssertFalse(result.failedOpen)+ XCTAssertEqual(result.frames.count, 3)+ XCTAssertTrue(result.frames.allSatisfy { $0.captureSamples.count == 160 && $0.renderSamples.count == 160 })+ XCTAssertTrue(result.frames.allSatisfy { $0.captureValidMask.allSatisfy { $0 } && $0.renderValidMask.allSatisfy { $0 } })+ XCTAssertEqual(result.frames.map(\.epochID), [0, 0, 0])+ }++ func testPositiveAndNegativeOffsetHaveExplicitLagSign() {+ let positive = MeetingReferenceSynchronizer(configuration: .init(referenceToMicrophoneOffsetSeconds: 0.02))+ .synchronize(microphone: (0..<5).map { mic($0, start: Int64($0 * 160)) }, reference: (0..<5).map { ref($0, pts: Double($0) * 0.01) })+ let negative = MeetingReferenceSynchronizer(configuration: .init(referenceToMicrophoneOffsetSeconds: -0.02))+ .synchronize(microphone: (0..<5).map { mic($0, start: Int64($0 * 160)) }, reference: (0..<5).map { ref($0, pts: Double($0) * 0.01) })+ XCTAssertTrue(positive.frames.compactMap(\.lagSeconds).allSatisfy { $0 == -0.02 })+ XCTAssertTrue(negative.frames.compactMap(\.lagSeconds).allSatisfy { $0 == 0.02 })+ }++ func testSlowDriftIsReportedAndLargeDriftIsUnknown() {+ let stable = MeetingReferenceSynchronizer(configuration: .init(referenceClockDriftPPM: 25))+ .synchronize(microphone: [mic(0, start: 0)], reference: [ref(0, pts: 0)])+ XCTAssertEqual(stable.diagnostics.referenceClockDriftPPM, 25)+ let unstable = MeetingReferenceSynchronizer(configuration: .init(referenceClockDriftPPM: 90, maximumClockDriftPPM: 100))+ .synchronize(microphone: [mic(0, start: 0)], reference: [ref(0, pts: 0)])+ XCTAssertTrue(unstable.frames[0].unknownReasons.contains(.clockDriftUnstable))+ }++ func testDroppedBlocksCreateGapMaskAndEpoch() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0), mic(2, start: 320)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01), ref(2, pts: 0.02)]+ )+ XCTAssertTrue(result.frames.contains { $0.unknownReasons.contains(.captureGap) && $0.captureValidMask.contains(false) })+ XCTAssertGreaterThan(result.diagnostics.epochCount, 1)+ XCTAssertEqual(result.frames.map(\.epochID).max(), 1)+ }++ func testCodecPrimingRemainderAndEditListAreAccountedFor() {+ let configuration = MeetingReferenceSynchronizerConfiguration(+ referenceScope: .authorizedFullMix, referenceCompleteness: .measuredComplete,+ codecPrimingSamples: 32, codecRemainderSamples: 16, editListOffsetSeconds: 0.01+ )+ let result = MeetingReferenceSynchronizer(configuration: configuration).synchronize(+ microphone: [mic(0, start: 0), mic(1, start: 160)],+ reference: [ref(0, pts: 0, count: 160), ref(1, pts: 0.01, count: 160)]+ )+ XCTAssertEqual(result.frames.count, 3) // edit-list offset shifts reference one hop+ XCTAssertEqual(result.diagnostics.converterVersions, ["linear-v1", "linear-v1"])+ XCTAssertEqual(result.diagnostics.converterDelays, [0, 0])+ XCTAssertTrue(result.frames.dropFirst().contains { $0.renderValidMask.contains(true) })+ }++ func testDiscontinuityRouteAndSampleRateChangesResetEpoch() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0), mic(1, start: 160, route: "route-b", discontinuity: true), mic(2, start: 320, route: "route-b")],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01, rate: 8_000), ref(2, pts: 0.03, rate: 8_000)]+ )+ // Both tracks reset at the same session boundary, so the merged epoch advances once.+ XCTAssertEqual(+ result.diagnostics.epochCount,+ 2,+ "epochIDs=\(result.frames.map(\.epochID)) reasons=\(result.frames.map(\.unknownReasons))"+ )+ XCTAssertGreaterThan(result.frames.map(\.epochID).max() ?? 0, 0)+ }++ func testSynthesizedMicrophoneTimingFreezesUntilResynchronizationBoundary() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [+ mic(0, start: 0, host: nil),+ mic(1, start: 160, host: nil),+ mic(2, start: 320, host: 0.02),+ mic(3, start: 480, host: 0.03)+ ],+ reference: (0..<4).map { ref($0, pts: Double($0) * 0.01) }+ )+ XCTAssertTrue(result.frames.prefix(2).allSatisfy { $0.adaptationFrozen })+ guard let boundary = result.frames.firstIndex(where: { $0.resynchronizationBoundary }) else {+ return XCTFail("expected a recorded resynchronization boundary")+ }+ XCTAssertTrue(result.frames.contains { $0.unknownReasons.contains(.captureTimingSynthesized) })+ XCTAssertGreaterThan(boundary, 0)+ XCTAssertTrue(result.frames.dropFirst(boundary + 1).contains { !$0.adaptationFrozen })+ }++ func testSilenceIsValidAudioAndNotMissingCoverage() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, value: 0)], reference: [ref(0, pts: 0, value: 0)]+ )+ XCTAssertTrue(result.frames[0].captureValidMask.allSatisfy { $0 })+ XCTAssertTrue(result.frames[0].renderValidMask.allSatisfy { $0 })+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.captureGap))+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.referenceGap))+ }++ func testReferenceScopeAndCompletenessRemainSeparateReasons() {+ let result = MeetingReferenceSynchronizer().synchronize(microphone: [mic(0, start: 0)], reference: [ref(0, pts: 0)])+ XCTAssertTrue(result.frames[0].unknownReasons.contains(.referenceScopeLimited))+ XCTAssertTrue(result.frames[0].unknownReasons.contains(.referenceCompletenessUnobservable))+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.referenceAbsent))+ }++ func testMissingReferenceAndMissingCaptureAreUnknownNotSilence() {+ let noReference = MeetingReferenceSynchronizer().synchronize(microphone: [mic(0, start: 0)], reference: [])+ XCTAssertTrue(noReference.frames[0].unknownReasons.contains(.referenceAbsent))+ XCTAssertTrue(noReference.frames[0].renderValidMask.allSatisfy { !$0 })+ let noCapture = MeetingReferenceSynchronizer().synchronize(microphone: [], reference: [ref(0, pts: 0)])+ XCTAssertTrue(noCapture.frames[0].unknownReasons.contains(.captureGap))+ XCTAssertTrue(noCapture.frames[0].captureValidMask.allSatisfy { !$0 })+ }++ func testDeterministicReplayAndBoundedResourceFailOpen() {+ let micFrames = (0..<4).map { mic($0, start: Int64($0 * 160), value: Float($0)) }+ let refs = (0..<4).map { ref($0, pts: Double($0) * 0.01, value: Float($0 + 1)) }+ let synchronizer = MeetingReferenceSynchronizer()+ XCTAssertEqual(synchronizer.synchronize(microphone: micFrames, reference: refs), synchronizer.synchronize(microphone: micFrames, reference: refs))+ let bounded = MeetingReferenceSynchronizer(configuration: .init(maximumInputFrameCount: 1))+ .synchronize(microphone: micFrames, reference: refs)+ XCTAssertTrue(bounded.failedOpen)+ XCTAssertEqual(bounded.failureReason, .engineUnavailable)+ XCTAssertTrue(bounded.diagnostics.boundedResourceFailure)+ }++ func testNonFiniteDuplicateAndLateFramesAreDroppedDeterministically() {+ var invalid = mic(1, start: 160)+ invalid = MeetingMicrophonePCMFrame(sequenceNumber: 1, sampleTime: 160, hostTime: 0.01, sampleRate: sampleRate, samples: [Float.nan] + Array(repeating: 1, count: 159))+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0), invalid, mic(1, start: 320), mic(3, start: 160)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01), ref(2, pts: 0.02)]+ )+ XCTAssertGreaterThanOrEqual(result.diagnostics.nonFiniteSampleCount, 1)+ XCTAssertGreaterThanOrEqual(result.diagnostics.duplicateOrLateFrameCount, 1)+ XCTAssertGreaterThanOrEqual(result.diagnostics.droppedFrameCount, 2)+ XCTAssertTrue(result.frames.contains { $0.unknownReasons.contains(.captureGap) })+ }++ func testStereoContinuityUsesDownmixedCountAndMalformedArrayIsGap() {+ let stereo = (0..<2).map { index in+ MeetingMicrophonePCMFrame(sequenceNumber: index, sampleTime: Int64(index * 160), hostTime: Double(index) * 0.01,+ sampleRate: sampleRate, channelCount: 2, samples: [Float](repeating: Float(index + 1), count: 320))+ }+ let valid = MeetingReferenceSynchronizer().synchronize(microphone: stereo, reference: [ref(0, pts: 0), ref(1, pts: 0.01)])+ XCTAssertEqual(valid.diagnostics.epochCount, 1)+ XCTAssertEqual(valid.frames.map { $0.captureSamples[0] }, [1, 2])+ XCTAssertFalse(valid.frames.contains { $0.unknownReasons.contains(.captureGap) })++ let malformed = MeetingMicrophonePCMFrame(sequenceNumber: 0, sampleTime: 0, hostTime: 0,+ sampleRate: sampleRate, channelCount: 2, samples: [Float](repeating: 1, count: 319))+ let result = MeetingReferenceSynchronizer().synchronize(microphone: [malformed, stereo[1]], reference: [ref(0, pts: 0), ref(1, pts: 0.01)])+ XCTAssertEqual(result.diagnostics.droppedFrameCount, 1)+ // A malformed pre-origin block cannot define a source gap; the first accepted+ // block establishes the microphone origin and is fully covered.+ XCTAssertTrue(result.frames[0].captureValidMask.allSatisfy { $0 })+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.captureGap))+ }++ func testDriftBeyondConfiguredMaximumFailsOpenAsClockDriftUnstable() {+ let result = MeetingReferenceSynchronizer(configuration: .init(referenceClockDriftPPM: 101, maximumClockDriftPPM: 100))+ .synchronize(microphone: [mic(0, start: 0, host: 0)], reference: [ref(0, pts: 0)])+ XCTAssertTrue(result.failedOpen)+ XCTAssertEqual(result.failureReason, MeetingSynchronizerUnknownReason.clockDriftUnstable)+ XCTAssertFalse(result.diagnostics.boundedResourceFailure)+ }++ func testReferenceEpochAndGapUseTransformedSessionCoordinates() {+ let config = MeetingReferenceSynchronizerConfiguration(+ referenceConverter: .init(algorithmicDelaySeconds: 0.004), referenceToMicrophoneOffsetSeconds: 0.03,+ editListOffsetSeconds: 0.02)+ let refs = [ref(0, pts: 0), ref(1, pts: 0.03, discontinuity: true)]+ + (2..<10).map { ref($0, pts: Double($0 + 2) * 0.01) }+ let result = MeetingReferenceSynchronizer(configuration: config).synchronize(+ microphone: (0..<10).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) }, reference: refs)+ let firstReset = result.frames.firstIndex { $0.epochID > 0 }+ XCTAssertNotNil(firstReset)+ XCTAssertGreaterThanOrEqual(firstReset ?? 0, 5) // transformed event is after 0.05 s, not raw PTS 0.01+ XCTAssertTrue(result.frames.contains { $0.unknownReasons.contains(.referenceGap) && $0.renderValidMask.contains(false) })+ }++ func testFrameTimelinesExposeActualSourceTimesAndMappedLag() {+ let config = MeetingReferenceSynchronizerConfiguration(+ microphoneConverter: .init(algorithmicDelaySeconds: 0.003),+ referenceConverter: .init(algorithmicDelaySeconds: 0.005),+ referenceToMicrophoneOffsetSeconds: 0.007,+ editListOffsetSeconds: 0.011)+ let result = MeetingReferenceSynchronizer(configuration: config).synchronize(+ microphone: (0..<5).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) },+ reference: (0..<5).map { ref($0, pts: Double($0) * 0.01) })+ guard let frame = result.frames.first(where: { $0.lagSeconds != nil }) else { return XCTFail("expected overlap") }+ XCTAssertEqual(frame.timelines.microphoneSourceTime ?? .nan, 0.01, accuracy: 0.000001)+ XCTAssertEqual(frame.timelines.referencePTSTime ?? .nan, 0, accuracy: 0.000001)+ XCTAssertEqual(frame.lagSeconds ?? .nan, -0.016, accuracy: 0.000001)+ }++ func testSynthesizedTimingMakesLagUnknown() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0), mic(1, start: 160)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01)]+ )++ XCTAssertEqual(result.frames.count, 2)+ XCTAssertTrue(result.frames.allSatisfy { $0.lagSeconds == nil })+ XCTAssertTrue(result.frames.allSatisfy { $0.unknownReasons.contains(.captureTimingSynthesized) })+ }++ func testFrameTimelinesCarryMicrophoneSampleAndHostTimes() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 8_000, host: 42), mic(1, start: 8_160, host: 42.01)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01)]+ )++ XCTAssertEqual(result.frames.count, 2)+ XCTAssertEqual(result.frames[0].timelines.microphoneSampleTime, 8_000)+ XCTAssertEqual(result.frames[0].timelines.microphoneHostTime ?? .nan, 42, accuracy: 0.000001)+ XCTAssertEqual(result.frames[1].timelines.microphoneSampleTime, 8_160)+ XCTAssertEqual(result.frames[1].timelines.microphoneHostTime ?? .nan, 42.01, accuracy: 0.000001)+ }++ func testSequenceGapWithContiguousSampleTimesCreatesBoundaryWithoutGap() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), mic(2, start: 160, host: 1.01)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01)]+ )++ XCTAssertEqual(result.frames.count, 2)+ XCTAssertFalse(result.frames[1].unknownReasons.contains(.captureGap))+ XCTAssertTrue(result.frames[1].captureValidMask.allSatisfy { $0 })+ XCTAssertEqual(result.frames[1].epochID, 1)+ XCTAssertTrue(result.frames[1].resynchronizationBoundary)+ }++ func testInvalidReferencePTSAndRateCreateReferenceGapEpoch() {+ let invalidPTS = MeetingReferencePCMFrame(sequenceNumber: 1, presentationTime: .nan,+ sampleRate: sampleRate, samples: [Float](repeating: 2, count: 160))+ let ptsResult = MeetingReferenceSynchronizer().synchronize(+ microphone: (0..<3).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) },+ reference: [ref(0, pts: 0), invalidPTS, ref(2, pts: 0.02)]+ )+ XCTAssertTrue(ptsResult.frames.contains { $0.unknownReasons.contains(.referenceGap) })+ XCTAssertGreaterThan(ptsResult.diagnostics.epochCount, 1)++ let invalidRate = MeetingReferencePCMFrame(sequenceNumber: 1, presentationTime: 0.01,+ sampleRate: 0, samples: [Float](repeating: 2, count: 160))+ let rateResult = MeetingReferenceSynchronizer().synchronize(+ microphone: (0..<3).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) },+ reference: [ref(0, pts: 0), invalidRate, ref(2, pts: 0.02)]+ )+ XCTAssertTrue(rateResult.frames.contains { $0.unknownReasons.contains(.referenceGap) })+ XCTAssertGreaterThan(rateResult.diagnostics.epochCount, 1)+ }++ func testRateChangePreservesReferenceSourcePTSInTimelines() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), mic(1, start: 160, host: 1.01)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01, count: 80, rate: 8_000)]+ )++ XCTAssertEqual(result.frames.count, 2)+ XCTAssertEqual(result.frames[1].timelines.referencePTSTime ?? .nan, 0.01, accuracy: 0.000001)+ }++ func testArrivalOrderWinsAndLateFramesCannotOverwriteAcceptedAudio() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [+ mic(1, start: 160, host: 1.01, value: 11),+ mic(0, start: 0, host: 1.00, value: 22),+ mic(1, start: 160, host: 1.02, value: 33),+ mic(2, start: 320, host: 1.03, value: 44)+ ],+ reference: (0..<3).map { ref($0, pts: Double($0) * 0.01) }+ )++ XCTAssertEqual(result.diagnostics.duplicateOrLateFrameCount, 2)+ XCTAssertEqual(result.diagnostics.droppedFrameCount, 2)+ XCTAssertEqual(result.frames.prefix(2).map { $0.captureSamples[0] }, [11, 44])+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.captureGap))+ }++ func testRejectedSequenceIsConsumedWithoutDoubleEpochOrGap() {+ let invalid = MeetingMicrophonePCMFrame(+ sequenceNumber: 1, sampleTime: 160, hostTime: 1.01, sampleRate: sampleRate,+ samples: [Float.nan] + Array(repeating: 1, count: 159)+ )+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), invalid, mic(2, start: 320, host: 1.02)],+ reference: (0..<3).map { ref($0, pts: Double($0) * 0.01) }+ )+ let micOnly = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), invalid, mic(2, start: 320, host: 1.02)],+ reference: []+ )+ let refOnly = MeetingReferenceSynchronizer().synchronize(+ microphone: [], reference: (0..<3).map { ref($0, pts: Double($0) * 0.01) }+ )++ XCTAssertEqual(result.diagnostics.nonFiniteSampleCount, 1)+ XCTAssertEqual(+ result.diagnostics.epochCount,+ 2,+ "epochIDs=\(result.frames.map(\.epochID)) micOnly=\(micOnly.diagnostics.epochCount) refOnly=\(refOnly.diagnostics.epochCount) reasons=\(result.frames.map(\.unknownReasons))"+ )+ XCTAssertEqual(result.frames.filter { $0.unknownReasons.contains(.captureGap) }.count, 1)+ }++ func testInvalidFirstMicrophoneFrameDoesNotBecomeTheSourceOrigin() {+ let invalid = MeetingMicrophonePCMFrame(+ sequenceNumber: 0, sampleTime: 8_000, hostTime: 40, sampleRate: 0,+ samples: [Float](repeating: 1, count: 160)+ )+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [invalid, mic(1, start: 8_160, host: 40.01)],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01)]+ )++ XCTAssertFalse(result.failedOpen)+ XCTAssertEqual(result.diagnostics.droppedFrameCount, 1)+ XCTAssertEqual(result.frames.first?.timelines.microphoneSampleTime, 8_160)+ XCTAssertTrue(result.frames.contains { $0.captureValidMask.contains(true) })+ }++ func testReferenceDriftScalesSegmentPlacementAndDuration() {+ let count = 10+ let sourceFrameSamples = 1_600+ let config = MeetingReferenceSynchronizerConfiguration(+ referenceClockDriftPPM: 5_000, maximumClockDriftPPM: 10_000,+ referenceScope: .authorizedFullMix, referenceCompleteness: .measuredComplete+ )+ let result = MeetingReferenceSynchronizer(configuration: config).synchronize(+ microphone: (0..<count).map {+ mic($0, start: Int64($0 * sourceFrameSamples), host: Double($0) * 0.1, count: sourceFrameSamples)+ },+ reference: (0..<count).map {+ ref($0, pts: Double($0) * 0.1, count: sourceFrameSamples)+ }+ )++ XCTAssertEqual(result.frames.count, 101)+ XCTAssertTrue(result.frames.dropLast().allSatisfy { $0.renderValidMask.contains(true) })+ XCTAssertEqual(result.frames[10].timelines.referencePTSTime ?? .nan, 0.1, accuracy: 0.002)+ }++ func testLagKeepsCodecPrimingAndAbsoluteReferencePTSCoordinates() {+ let config = MeetingReferenceSynchronizerConfiguration(+ referenceConverter: .init(algorithmicDelaySeconds: 0.004),+ referenceToMicrophoneOffsetSeconds: 0.007,+ referenceScope: .authorizedFullMix,+ referenceCompleteness: .measuredComplete,+ codecPrimingSamples: 32,+ editListOffsetSeconds: 0.01+ )+ let result = MeetingReferenceSynchronizer(configuration: config).synchronize(+ microphone: (0..<5).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) },+ reference: (0..<5).map { ref($0, pts: Double($0) * 0.01) }+ )++ guard let frame = result.frames.first(where: { $0.lagSeconds != nil }) else {+ return XCTFail("expected an overlapping frame")+ }+ XCTAssertEqual(frame.timelines.referencePTSTime ?? .nan, 0.002, accuracy: 0.001)+ // The 2 ms priming shift is retained in the source coordinate and is not removed by+ // subtracting the first reference segment origin before applying the affine mapping.+ XCTAssertEqual(frame.lagSeconds ?? .nan, -0.015, accuracy: 0.001)+ }++ func testLagIsInvariantToIndependentAbsoluteClockOrigins() {+ let config = MeetingReferenceSynchronizerConfiguration(+ microphoneConverter: .init(algorithmicDelaySeconds: 0.003),+ referenceConverter: .init(algorithmicDelaySeconds: 0.004),+ referenceToMicrophoneOffsetSeconds: 0.007,+ referenceScope: .authorizedFullMix,+ referenceCompleteness: .measuredComplete,+ codecPrimingSamples: 32,+ editListOffsetSeconds: 0.01+ )+ let synchronizer = MeetingReferenceSynchronizer(configuration: config)+ let relative = synchronizer.synchronize(+ microphone: (0..<5).map { mic($0, start: Int64($0 * 160), host: Double($0) * 0.01) },+ reference: (0..<5).map { ref($0, pts: Double($0) * 0.01) }+ )+ let offset = synchronizer.synchronize(+ microphone: (0..<5).map { mic($0, start: 8_000 + Int64($0 * 160), host: Double($0) * 0.01) },+ reference: (0..<5).map { ref($0, pts: 1_234 + Double($0) * 0.01) }+ )++ let relativeLag = relative.frames.compactMap(\.lagSeconds).first+ let offsetLag = offset.frames.compactMap(\.lagSeconds).first+ XCTAssertEqual(relativeLag ?? .nan, offsetLag ?? .nan, accuracy: 0.000001)+ XCTAssertEqual(offset.frames.compactMap { $0.timelines.referencePTSTime }.first ?? .nan,+ 1_234.002, accuracy: 0.001)+ }++ func testForwardSequenceWithNonMonotonicMicrophoneSampleTimeIsDropped() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [+ mic(0, start: 0, value: 10),+ mic(1, start: 80, value: 20), // overlaps the accepted source cursor+ mic(2, start: 160, value: 30)+ ],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01)]+ )++ XCTAssertEqual(result.diagnostics.droppedFrameCount, 1)+ XCTAssertEqual(result.diagnostics.duplicateOrLateFrameCount, 1)+ XCTAssertEqual(result.frames.count, 2)+ XCTAssertEqual(result.frames.map { $0.captureSamples[0] }, [10, 30])+ XCTAssertTrue(result.frames.allSatisfy { $0.captureValidMask.allSatisfy { $0 } })+ XCTAssertFalse(result.frames.contains { $0.unknownReasons.contains(.captureGap) })+ }++ func testForwardSequenceWithNonMonotonicReferencePTSIsDropped() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0), mic(1, start: 160)],+ reference: [+ ref(0, pts: 0, value: 10),+ ref(1, pts: 0.005, value: 20), // overlaps the accepted reference cursor+ ref(2, pts: 0.01, value: 30)+ ]+ )++ XCTAssertEqual(result.diagnostics.droppedFrameCount, 1)+ XCTAssertEqual(result.diagnostics.duplicateOrLateFrameCount, 1)+ XCTAssertEqual(result.frames.count, 2)+ XCTAssertEqual(result.frames.map { $0.renderSamples[0] }, [10, 30])+ XCTAssertTrue(result.frames.allSatisfy { $0.renderValidMask.allSatisfy { $0 } })+ XCTAssertFalse(result.frames.contains { $0.unknownReasons.contains(.referenceGap) })+ }++ func testContiguousRouteRateAndDiscontinuityResetsDoNotCreateGaps() {+ let changedMic = MeetingMicrophonePCMFrame(+ sequenceNumber: 1, sampleTime: 160, hostTime: 1.01, sampleRate: sampleRate,+ samples: [Float](repeating: 2, count: 160), routeIdentifier: "route-b", discontinuity: true)+ let changedRef = ref(1, pts: 0.01, count: 80, rate: 8_000, discontinuity: true)+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), changedMic],+ reference: [ref(0, pts: 0), changedRef]+ )++ XCTAssertEqual(result.frames.count, 2)+ XCTAssertGreaterThan(result.diagnostics.epochCount, 1)+ XCTAssertTrue(result.frames[1].captureValidMask.allSatisfy { $0 })+ XCTAssertTrue(result.frames[1].renderValidMask.allSatisfy { $0 })+ XCTAssertFalse(result.frames[1].unknownReasons.contains(.captureGap))+ XCTAssertFalse(result.frames[1].unknownReasons.contains(.referenceGap))+ }++ func testSharedResetEventAcrossTracksAdvancesEpochOnlyOnce() {+ let changedMic = mic(1, start: 160, host: 1.01, route: "route-b", discontinuity: true)+ let changedRef = ref(1, pts: 0.01, discontinuity: true)+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), changedMic],+ reference: [ref(0, pts: 0), changedRef]+ )++ XCTAssertEqual(result.diagnostics.epochCount, 2)+ XCTAssertEqual(result.frames.map(\.epochID), [0, 1])+ }++ func testExtremeInt64SampleTimesAreDroppedWithoutOverflowTrap() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [+ mic(0, start: .min, host: 1),+ mic(1, start: .max, host: 1.01)+ ],+ reference: [ref(0, pts: 0), ref(1, pts: 0.01)]+ )++ XCTAssertFalse(result.failedOpen)+ XCTAssertEqual(result.diagnostics.droppedFrameCount, 1)+ XCTAssertEqual(result.diagnostics.duplicateOrLateFrameCount, 1)+ }++ func testPreOriginInvalidFramesDoNotCreateFalseOverlapGaps() {+ let invalidMic = MeetingMicrophonePCMFrame(+ sequenceNumber: 0, sampleTime: 8_000, hostTime: 40, sampleRate: 0,+ samples: [Float](repeating: 1, count: 160))+ let invalidReference = MeetingReferencePCMFrame(+ sequenceNumber: 0, presentationTime: .nan, sampleRate: sampleRate,+ samples: [Float](repeating: 2, count: 160))+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [invalidMic, mic(1, start: 8_160, host: 40.01)],+ reference: [invalidReference, ref(1, pts: 0.01)]+ )++ XCTAssertFalse(result.failedOpen)+ XCTAssertEqual(result.diagnostics.droppedFrameCount, 2)+ XCTAssertTrue(result.frames[0].captureValidMask.allSatisfy { $0 })+ XCTAssertTrue(result.frames[0].renderValidMask.allSatisfy { $0 })+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.captureGap))+ XCTAssertFalse(result.frames[0].unknownReasons.contains(.referenceGap))+ }++ func testMidstreamEmptyBlockIsOneUnknownGapAndAllEmptyInputIsDropped() {+ let emptyMic = MeetingMicrophonePCMFrame(+ sequenceNumber: 1, sampleTime: 160, hostTime: 1.01, sampleRate: sampleRate, samples: [])+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1), emptyMic, mic(2, start: 320, host: 1.02)],+ reference: (0..<3).map { ref($0, pts: Double($0) * 0.01) }+ )+ XCTAssertEqual(result.diagnostics.droppedFrameCount, 1)+ XCTAssertEqual(result.diagnostics.epochCount, 2)+ XCTAssertTrue(result.frames[1].unknownReasons.contains(.captureGap))+ XCTAssertTrue(result.frames[2].captureValidMask.allSatisfy { $0 })++ let allEmpty = MeetingReferenceSynchronizer().synchronize(+ microphone: [emptyMic], reference: [MeetingReferencePCMFrame(+ sequenceNumber: 0, presentationTime: 0, sampleRate: sampleRate, samples: [])])+ XCTAssertEqual(allEmpty.diagnostics.droppedFrameCount, 2)+ XCTAssertTrue(allEmpty.frames.isEmpty)+ }++ func testOutputBudgetFailureRetainsTimingDiagnostics() {+ let config = MeetingReferenceSynchronizerConfiguration(maximumOutputFrameCount: 1)+ let result = MeetingReferenceSynchronizer(configuration: config).synchronize(+ microphone: (0..<3).map { mic($0, start: Int64($0 * 160), host: nil) },+ reference: (0..<3).map { ref($0, pts: Double($0) * 0.01) }+ )++ XCTAssertTrue(result.failedOpen)+ XCTAssertTrue(result.diagnostics.boundedResourceFailure)+ XCTAssertEqual(result.diagnostics.synthesizedMicrophoneFrameCount, 3)+ }++ func testFirstFrameDiscontinuityIsInitialMetadataNotAnEpochTransition() {+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [mic(0, start: 0, host: 1, discontinuity: true), mic(1, start: 160, host: 1.01)],+ reference: [ref(0, pts: 0, discontinuity: true), ref(1, pts: 0.01)]+ )++ XCTAssertEqual(result.diagnostics.epochCount, 1)+ XCTAssertEqual(result.frames.map(\.epochID), [0, 0])+ }++ func test44100HzContiguousShortSegmentsHaveNoResamplingSeamHoles() {+ let result = MeetingReferenceSynchronizer().synchronize(+ // 100 samples at 44.1 kHz maps to 36.28 analysis samples. Independent+ // per-segment rounding would leave alternating seam holes/overlaps.+ microphone: (0..<10).map {+ MeetingMicrophonePCMFrame(sequenceNumber: $0, sampleTime: Int64($0 * 100),+ hostTime: Double($0 * 100) / 44_100, sampleRate: 44_100,+ samples: [Float](repeating: 1, count: 100)+ )+ },+ reference: (0..<10).map {+ MeetingReferencePCMFrame(sequenceNumber: $0, presentationTime: Double($0 * 100) / 44_100,+ sampleRate: 44_100, samples: [Float](repeating: 2, count: 100)+ )+ }+ )++ XCTAssertEqual(result.frames.count, 3)+ XCTAssertTrue(result.frames.dropLast().allSatisfy { $0.captureValidMask.allSatisfy { $0 } })+ XCTAssertTrue(result.frames.dropLast().allSatisfy { $0.renderValidMask.allSatisfy { $0 } })+ XCTAssertFalse(result.frames.contains { $0.unknownReasons.contains(.captureGap) })+ XCTAssertFalse(result.frames.contains { $0.unknownReasons.contains(.referenceGap) })+ }++ func testFiniteExtremeStereoSamplesRemainFiniteAfterDownmix() {+ let extreme = MeetingMicrophonePCMFrame(+ sequenceNumber: 0, sampleTime: 0, hostTime: 1, sampleRate: sampleRate,+ channelCount: 2, samples: Array(repeating: .greatestFiniteMagnitude, count: 320))+ let result = MeetingReferenceSynchronizer().synchronize(+ microphone: [extreme], reference: [ref(0, pts: 0)])++ XCTAssertEqual(result.diagnostics.nonFiniteSampleCount, 0)+ XCTAssertTrue(result.frames[0].captureValidMask.allSatisfy { $0 })+ XCTAssertTrue(result.frames[0].captureSamples.allSatisfy { $0.isFinite })+ }+}diff --git a/Tests/FluidDictationIntegrationTests/MeetingSCKPairedDiagnosticsTests.swift b/Tests/FluidDictationIntegrationTests/MeetingSCKPairedDiagnosticsTests.swiftnew file mode 100644index 00000000..370499b8--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingSCKPairedDiagnosticsTests.swift@@ -0,0 +1,398 @@+#if DEBUG++@testable import FluidVoice_Debug+import AVFoundation+import CoreMedia+import Foundation+import XCTest++final class MeetingSCKPairedDiagnosticsTests: XCTestCase {+ private let enabledEnvironment = [MeetingSCKPairedDiagnosticGate.environmentKey: "1"]++ private func sample(+ _ output: MeetingSCKPairedOutput,+ pts: Double,+ duration: Double = 0.02,+ arrival: Double? = nil,+ rate: Double = 48_000,+ channels: Int = 2+ ) -> MeetingSCKPairedDiagnosticSample {+ .init(output: output, presentationSeconds: pts, durationSeconds: duration,+ frameCount: Int((duration * rate).rounded()), sampleRateHz: rate,+ channelCount: channels, arrivalSeconds: arrival)+ }++ func testGateIsExplicitAndDisabledReportsNoSamples() throws {+ let collector = MeetingSCKPairedDiagnosticCollector(environment: [:])+ XCTAssertFalse(collector.enabled)+ collector.record(sample(.applicationAudio, pts: 1))+ let report = collector.report()+ XCTAssertFalse(report.enabled)+ XCTAssertEqual(report.acceptedSampleCount, 0)+ XCTAssertFalse(report.rawPCMRetained)+ XCTAssertFalse(report.transcriptRetained)+ XCTAssertTrue(report.failOpen)+ XCTAssertNotNil(try JSONSerialization.jsonObject(with: report.jsonData()) as? [String: Any])+ }++ func testTracksFormatContinuityGapJitterAndOffsetWithoutClockClaim() throws {+ let collector = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)+ collector.record(sample(.applicationAudio, pts: 100, arrival: 0))+ collector.record(sample(.applicationAudio, pts: 100.02, arrival: 0.025))+ collector.record(sample(.applicationAudio, pts: 100.04, arrival: 0.04, channels: 1))+ collector.record(sample(.microphone, pts: 100.08, arrival: 0.01))+ collector.record(sample(.microphone, pts: 100.10, arrival: 0.03))+ collector.record(sample(.microphone, pts: 100.14, arrival: 0.08))++ let report = collector.report()+ XCTAssertEqual(report.acceptedSampleCount, 6)+ XCTAssertEqual(report.schemaVersion, 3)+ XCTAssertEqual(report.application.sampleCount, 3)+ XCTAssertEqual(report.application.formatChangeCount, 1)+ XCTAssertEqual(report.application.jitterObservationCount, 2)+ XCTAssertEqual(report.application.meanAbsoluteJitterSeconds!, 0.005, accuracy: 0.000_001)+ XCTAssertEqual(report.microphone.gapCount, 1)+ XCTAssertEqual(report.microphone.gapDurationSeconds, 0.02, accuracy: 0.000_001)+ XCTAssertEqual(report.crossTrack.firstPresentationOffsetSeconds!, 0.08, accuracy: 0.000_001)+ XCTAssertEqual(report.crossTrack.relativeTimestampSpanDifferencePPM!,+ ((report.microphone.timestampSpanSeconds! / report.application.timestampSpanSeconds!) - 1) * 1_000_000,+ accuracy: 0.001)+ XCTAssertFalse(report.crossTrack.sharedClockEstablished)+ XCTAssertNil(report.crossTrack.acousticDelaySeconds)+ XCTAssertFalse(report.crossTrack.aecEvaluated)+ XCTAssertTrue(report.crossTrack.timestampRelationship.contains("delivery-window"))+ XCTAssertTrue(report.crossTrack.timestampRelationship.contains("not identifiable"))+ }++ func testTimestampContinuityToleranceIgnoresNanosecondNoiseButDetectsOneSample() {+ let rate = 48_000.0+ let epsilon = 1e-9+ let contiguous = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)+ contiguous.record(sample(.microphone, pts: 0, rate: rate, channels: 1))+ contiguous.record(sample(.microphone, pts: 0.02 + epsilon, rate: rate, channels: 1))+ contiguous.record(sample(.microphone, pts: 0.04 - epsilon, rate: rate, channels: 1))+ let contiguousReport = contiguous.report().microphone+ XCTAssertEqual(contiguousReport.gapCount, 0)+ XCTAssertEqual(contiguousReport.overlapCount, 0)+ XCTAssertEqual(contiguousReport.backwardsTimestampCount, 0)++ let oneSampleGap = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)+ oneSampleGap.record(sample(.microphone, pts: 0, rate: rate, channels: 1))+ oneSampleGap.record(sample(.microphone, pts: 0.02 + 1 / rate, rate: rate, channels: 1))+ XCTAssertEqual(oneSampleGap.report().microphone.gapCount, 1)+ XCTAssertEqual(oneSampleGap.report().microphone.gapDurationSeconds, 1 / rate, accuracy: 1e-12)++ let oneSampleOverlap = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)+ oneSampleOverlap.record(sample(.microphone, pts: 0, rate: rate, channels: 1))+ oneSampleOverlap.record(sample(.microphone, pts: 0.02 - 1 / rate, rate: rate, channels: 1))+ XCTAssertEqual(oneSampleOverlap.report().microphone.overlapCount, 1)+ }++ func testMalformedMetadataAndSampleBoundAreNumericAndBounded() {+ let collector = MeetingSCKPairedDiagnosticCollector(+ configuration: .init(maximumSamples: 2), environment: enabledEnvironment+ )+ collector.record(sample(.applicationAudio, pts: 1))+ collector.record(.init(output: .applicationAudio, presentationSeconds: .nan, durationSeconds: 0.02,+ frameCount: 960, sampleRateHz: 48_000, channelCount: 2))+ collector.record(sample(.microphone, pts: 1))+ collector.record(sample(.microphone, pts: 2))++ let report = collector.report()+ XCTAssertEqual(report.acceptedSampleCount, 2)+ XCTAssertEqual(report.droppedAfterBoundCount, 2)+ XCTAssertEqual(report.application.malformedMetadataCount, 1)+ XCTAssertEqual(report.application.invalidTimestampCount, 1)+ XCTAssertEqual(report.microphone.sampleCount, 0)+ XCTAssertEqual(report.boundedMaximumSamples, 2)+ }++ func testReportIsNumericAndDoesNotContainRawAudioOrTextFields() throws {+ let collector = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)+ collector.record(sample(.microphone, pts: 3))+ let data = try collector.report().jsonData()+ let json = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])+ XCTAssertNil(json["samples"])+ XCTAssertNil(json["pcm"])+ XCTAssertNil(json["transcript"])+ XCTAssertEqual(json["rawPCMRetained"] as? Bool, false)+ XCTAssertEqual(json["transcriptRetained"] as? Bool, false)+ }++ func testDeterministicHarnessConsumesMetadataOnly() {+ let report = MeetingSCKPairedDiagnosticHarness.run(+ [sample(.applicationAudio, pts: 2), sample(.microphone, pts: 2.1, channels: 1)],+ environment: enabledEnvironment+ )+ XCTAssertEqual(report.acceptedSampleCount, 2)+ XCTAssertEqual(report.application.sampleCount, 1)+ XCTAssertEqual(report.microphone.sampleCount, 1)+ XCTAssertTrue(report.failOpen)+ }++ func testCallbackSeamRequiresStableSelectedApplicationProvenance() {+ let collector = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)+ let sample = sample(.applicationAudio, pts: 4)+ let unconfirmed = MeetingSCKPairedDiagnosticProvenance(+ scope: .selectedApplicationWindow, applicationSelectionConfirmed: false+ )+ XCTAssertFalse(collector.record(sample, provenance: unconfirmed))+ XCTAssertEqual(collector.report().acceptedSampleCount, 0)++ let confirmed = MeetingSCKPairedDiagnosticProvenance(+ scope: .selectedApplicationWindow, applicationSelectionConfirmed: true+ )+ XCTAssertTrue(collector.record(sample, provenance: confirmed))+ XCTAssertFalse(collector.record(sample, provenance: .init(+ scope: .selectedApplicationDisplay, applicationSelectionConfirmed: true+ )))+ let report = collector.report()+ XCTAssertEqual(report.acceptedSampleCount, 1)+ XCTAssertEqual(report.provenance, confirmed)+ XCTAssertEqual(report.rejectedProvenanceCount, 2)+ XCTAssertFalse(report.rawPCMRetained)+ XCTAssertFalse(report.transcriptRetained)+ }++ func testDisabledCallbackSeamDoesNotAdmitMetadata() {+ let collector = MeetingSCKPairedDiagnosticCollector(environment: [:])+ let provenance = MeetingSCKPairedDiagnosticProvenance(+ scope: .selectedApplicationDisplay, applicationSelectionConfirmed: true+ )+ XCTAssertFalse(collector.record(+ sample(.microphone, pts: 5), provenance: provenance+ ))+ XCTAssertEqual(collector.report().acceptedSampleCount, 0)+ XCTAssertNil(collector.report().provenance)+ XCTAssertEqual(collector.report().rejectedProvenanceCount, 0)+ }++ func testHardwareGateRequiresBothFlagsAndTargetBundleID() {+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.hardwareEnabled(environment: [:]))+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.hardwareEnabled(environment: [+ MeetingSCKPairedDiagnosticGate.environmentKey: "1",+ MeetingSCKPairedDiagnosticGate.hardwareEnvironmentKey: "1"+ ]))+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.hardwareEnabled(environment: [+ MeetingSCKPairedDiagnosticGate.environmentKey: "1",+ MeetingSCKPairedDiagnosticGate.targetBundleIDEnvironmentKey: "com.example.Meeting"+ ]))+ XCTAssertTrue(MeetingSCKPairedDiagnosticGate.hardwareEnabled(environment: [+ MeetingSCKPairedDiagnosticGate.environmentKey: "1",+ MeetingSCKPairedDiagnosticGate.hardwareEnvironmentKey: "1",+ MeetingSCKPairedDiagnosticGate.targetBundleIDEnvironmentKey: "com.example.Meeting"+ ]))+ }++ func testAutorunGateRequiresAdditionalExactFlag() {+ let hardware = [+ MeetingSCKPairedDiagnosticGate.environmentKey: "1",+ MeetingSCKPairedDiagnosticGate.hardwareEnvironmentKey: "1",+ MeetingSCKPairedDiagnosticGate.targetBundleIDEnvironmentKey: "com.example.Meeting"+ ]+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.autorunEnabled(environment: hardware))+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.autorunEnabled(environment: hardware.merging([+ MeetingSCKPairedDiagnosticGate.autorunEnvironmentKey: "true"+ ]) { _, new in new }))+ XCTAssertTrue(MeetingSCKPairedDiagnosticGate.autorunEnabled(environment: hardware.merging([+ MeetingSCKPairedDiagnosticGate.autorunEnvironmentKey: "1"+ ]) { _, new in new }))+ XCTAssertFalse(MeetingSCKPairedAutorun.startIfRequested(environment: [:]))+ }++ func testAutorunTimeoutBudgetHasHardWatchdogHeadroom() {+ XCTAssertGreaterThan(MeetingSCKPairedAutorun.hardWatchdogSeconds,+ MeetingSCKPairedAutorun.maximumRunSeconds)+ XCTAssertLessThanOrEqual(MeetingSCKPairedAutorun.hardWatchdogSeconds, 15)+ }++ func testSampleBufferCallbackCopiesNumericMetadataOnly() throws {+ let format = try XCTUnwrap(AVAudioFormat(+ commonFormat: .pcmFormatFloat32, sampleRate: 48_000, channels: 1, interleaved: false+ ))+ let pcm = try XCTUnwrap(AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 960))+ pcm.frameLength = 960+ let buffer = try XCTUnwrap(meetingMicrophoneSynthesizeSampleBuffer(+ from: pcm, presentationTime: CMTime(value: 12, timescale: 1)+ ))+ let provenance = MeetingSCKPairedDiagnosticProvenance(+ scope: .selectedApplicationWindow, applicationSelectionConfirmed: true+ )+ let collector = MeetingSCKPairedDiagnosticCollector(environment: enabledEnvironment)++ XCTAssertTrue(collector.record(sampleBuffer: buffer, output: .microphone, provenance: provenance))+ let report = collector.report()+ XCTAssertEqual(report.microphone.sampleCount, 1)+ XCTAssertEqual(report.microphone.validTimestampCount, 1)+ XCTAssertEqual(report.microphone.firstSampleRateHz ?? .nan, 48_000, accuracy: 0.001)+ XCTAssertEqual(report.microphone.firstChannelCount, 1)+ XCTAssertEqual(report.provenance, provenance)+ XCTAssertFalse(report.rawPCMRetained)+ XCTAssertFalse(report.transcriptRetained)+ }++ func testTrialAGateIsSeparateAndChromeOnly() {+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.autorunEnabled(environment: [+ MeetingExternalReferenceTrialAGate.environmentKey: "1",+ MeetingExternalReferenceTrialAGate.autorunEnvironmentKey: "1",+ MeetingExternalReferenceTrialAGate.targetBundleIDEnvironmentKey: "com.example.App"+ ]))+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.autorunEnabled(environment: [+ MeetingExternalReferenceTrialAGate.environmentKey: "1",+ MeetingExternalReferenceTrialAGate.autorunEnvironmentKey: "1",+ MeetingSCKPairedDiagnosticGate.targetBundleIDEnvironmentKey: "com.google.Chrome"+ ]))+ XCTAssertTrue(MeetingExternalReferenceTrialAGate.autorunEnabled(environment: [+ MeetingExternalReferenceTrialAGate.environmentKey: "1",+ MeetingExternalReferenceTrialAGate.autorunEnvironmentKey: "1",+ MeetingExternalReferenceTrialAGate.targetBundleIDEnvironmentKey: "com.google.Chrome"+ ]))+ }++ func testTrialARequiresTheControlledStimulusToBePlaying() {+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(title: nil))+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(+ title: "FluidVoice C2 Diagnostic Stimulus — Ready"))+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(+ title: "FluidVoice C2 Diagnostic Stimulus — Complete"))+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(+ title: "FluidVoice C2 Diagnostic Stimulus — NOT PLAYING - Google Chrome"))+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(+ title: "FluidVoice C2 Diagnostic Stimulus — PLAYINGISH - Google Chrome"))+ XCTAssertTrue(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(+ title: "FluidVoice C2 Diagnostic Stimulus — PLAYING - Google Chrome"))+ XCTAssertFalse(MeetingExternalReferenceTrialAGate.stimulusWindowIsPlaying(+ title: "Unrelated PLAYING - Google Chrome"))+ }++ func testTrialASelectsUniqueDisplayWithMaximumWindowIntersection() {+ let left = MeetingExternalReferenceTrialADisplayCandidate(+ displayID: 1, frame: CGRect(x: 0, y: 0, width: 1_000, height: 800))+ let right = MeetingExternalReferenceTrialADisplayCandidate(+ displayID: 2, frame: CGRect(x: 1_000, y: 0, width: 1_000, height: 800))+ let window = CGRect(x: 800, y: 100, width: 500, height: 400)+ XCTAssertEqual(+ MeetingExternalReferenceTrialADisplaySelector.selectDisplayID(+ windowFrame: window, displays: [left, right]), 2)+ }++ func testTrialADisplaySelectionFailsClosedForNoIntersectionAndTie() {+ let first = MeetingExternalReferenceTrialADisplayCandidate(+ displayID: 1, frame: CGRect(x: 0, y: 0, width: 1_000, height: 800))+ let second = MeetingExternalReferenceTrialADisplayCandidate(+ displayID: 2, frame: CGRect(x: 1_000, y: 0, width: 1_000, height: 800))+ XCTAssertNil(MeetingExternalReferenceTrialADisplaySelector.selectDisplayID(+ windowFrame: CGRect(x: 3_000, y: 0, width: 100, height: 100), displays: [first, second]))+ XCTAssertNil(MeetingExternalReferenceTrialADisplaySelector.selectDisplayID(+ windowFrame: CGRect(x: 800, y: 100, width: 400, height: 400), displays: [first, second]))+ }++ func testTrialAWindowSelectionUsesOwningWindowAuthorityAcrossDuplicateAppRecords() {+ let staleRecordWindow = MeetingExternalReferenceTrialAWindowCandidate(+ windowID: 1, owningBundleIdentifier: "com.google.Chrome", owningProcessID: 101,+ title: "FluidVoice C2 Diagnostic Stimulus — READY")+ let playingWindow = MeetingExternalReferenceTrialAWindowCandidate(+ windowID: 2, owningBundleIdentifier: "com.google.Chrome", owningProcessID: 202,+ title: "FluidVoice C2 Diagnostic Stimulus — PLAYING")+ let selected = MeetingExternalReferenceTrialAWindowSelector.selectPlayingWindow(+ from: [staleRecordWindow, playingWindow], targetBundleIdentifier: "com.google.Chrome")+ XCTAssertEqual(selected?.windowID, 2)+ XCTAssertEqual(selected?.owningProcessID, 202)+ }++ func testTrialAWindowSelectionFailsClosedForDuplicatePlayingWindowsOrWrongOwner() {+ let first = MeetingExternalReferenceTrialAWindowCandidate(+ windowID: 1, owningBundleIdentifier: "com.google.Chrome", owningProcessID: 101,+ title: "FluidVoice C2 Diagnostic Stimulus — PLAYING")+ let second = MeetingExternalReferenceTrialAWindowCandidate(+ windowID: 2, owningBundleIdentifier: "com.google.Chrome", owningProcessID: 202,+ title: "FluidVoice C2 Diagnostic Stimulus — PLAYING")+ XCTAssertNil(MeetingExternalReferenceTrialAWindowSelector.selectPlayingWindow(+ from: [first, second], targetBundleIdentifier: "com.google.Chrome"))+ let wrongOwner = MeetingExternalReferenceTrialAWindowCandidate(+ windowID: 3, owningBundleIdentifier: "com.apple.Safari", owningProcessID: 303,+ title: "FluidVoice C2 Diagnostic Stimulus — PLAYING")+ XCTAssertNil(MeetingExternalReferenceTrialAWindowSelector.selectPlayingWindow(+ from: [wrongOwner], targetBundleIdentifier: "com.google.Chrome"))+ }++ func testTrialAOfflineAnalyzerRejectsGapsOverlapsSynthesizedAndBadGeometry() throws {+ func sample(_ pts: Double, _ duration: Double = 0.1, synthesized: Bool = false,+ frames: Int = 4_800, arrival: Double? = nil) -> MeetingExternalReferenceTrialATrackSample {+ .init(presentationSeconds: pts, durationSeconds: duration, frameCount: frames,+ sampleRateHz: 48_000, channelCount: 1, synthesizedTiming: synthesized,+ rms: 0.05, peak: 0.08, arrivalSeconds: arrival)+ }+ let clean = MeetingExternalReferenceTrialAOfflineHarness.analyze(+ reference: [sample(0), sample(0.1), sample(0.2)],+ microphone: [sample(0), sample(0.1), sample(0.2)])+ XCTAssertFalse(clean.valid, "clock mapping is intentionally still unknown")+ XCTAssertFalse(clean.reference.timingValid, "three tenths of audio cannot cover a five-second request")+ XCTAssertEqual(clean.reference.coverageFraction, 0.06, accuracy: 0.000_001)+ XCTAssertFalse(clean.clock.sharedClockEstablished)+ XCTAssertNil(clean.clock.acousticDelaySeconds)++ let completeTiming = MeetingExternalReferenceTrialATrackReport.analyze(+ [sample(0), sample(0.1), sample(0.2)], requestedDurationSeconds: 0.3)+ XCTAssertTrue(completeTiming.timingValid)++ let ninetyFivePercent = MeetingExternalReferenceTrialATrackReport.analyze(+ (0..<19).map { sample(Double($0) * 0.1) }, requestedDurationSeconds: 2.0)+ XCTAssertEqual(ninetyFivePercent.coverageFraction, 0.95, accuracy: 0.000_001)+ XCTAssertFalse(ninetyFivePercent.timingValid, "95% coverage is not complete capture")++ let shiftedArrival = MeetingExternalReferenceTrialATrackReport.analyze(+ [sample(0, arrival: 10), sample(0.1, arrival: 10.1), sample(0.2, arrival: 10.2)],+ requestedDurationSeconds: 0.3,+ captureOpenArrivalSeconds: 0,+ captureCloseArrivalSeconds: 0.3)+ XCTAssertFalse(shiftedArrival.arrivalBoundaryValid)+ XCTAssertFalse(shiftedArrival.timingValid)++ let missingArrival = MeetingExternalReferenceTrialATrackReport.analyze(+ [sample(0), sample(0.1), sample(0.2)],+ requestedDurationSeconds: 0.3,+ captureOpenArrivalSeconds: 0,+ captureCloseArrivalSeconds: 0.3)+ XCTAssertFalse(missingArrival.arrivalBoundaryValid)+ XCTAssertFalse(missingArrival.timingValid)++ let boundedArrival = MeetingExternalReferenceTrialATrackReport.analyze(+ [sample(0, arrival: 0.01), sample(0.1, arrival: 0.11), sample(0.2, arrival: 0.21)],+ requestedDurationSeconds: 0.3,+ captureOpenArrivalSeconds: 0,+ captureCloseArrivalSeconds: 0.3)+ XCTAssertTrue(boundedArrival.arrivalBoundaryValid)+ XCTAssertTrue(boundedArrival.timingValid)++ let invalid = MeetingExternalReferenceTrialATrackReport.analyze(+ [sample(0), sample(0.2), sample(0.15, synthesized: true), sample(.nan), sample(0.3, frames: 1)],+ requestedDurationSeconds: 0.3)+ XCTAssertEqual(invalid.gapCount, 1)+ XCTAssertEqual(invalid.overlapCount, 1)+ XCTAssertEqual(invalid.synthesizedFrameCount, 4_800)+ XCTAssertEqual(invalid.invalidTimestampCount, 2)+ XCTAssertFalse(invalid.timingValid)+ }++ func testTrialAReportIsNumericOnlyAndNoPlaybackOrRetention() throws {+ let sample = MeetingExternalReferenceTrialATrackSample(+ presentationSeconds: 0, durationSeconds: 0.1, frameCount: 4_800,+ sampleRateHz: 48_000, channelCount: 1, synthesizedTiming: false,+ rms: 0.05, peak: 0.08)+ let report = MeetingExternalReferenceTrialAOfflineHarness.analyze(+ reference: [sample], microphone: [sample])+ let data = try JSONEncoder().encode(report)+ let object = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])+ XCTAssertEqual(object["appOwnedPlayback"] as? Bool, false)+ XCTAssertEqual(object["rawPCMRetained"] as? Bool, false)+ XCTAssertEqual(object["transcriptRetained"] as? Bool, false)+ XCTAssertEqual(object["persisted"] as? Bool, false)+ XCTAssertEqual(object["captureValid"] as? Bool, false)+ XCTAssertEqual(object["acousticMeasurementValid"] as? Bool, false)+ XCTAssertNil(object["samples"])+ XCTAssertNil(object["pcm"])+ }+}++#endifdiff --git a/Tests/FluidDictationIntegrationTests/MeetingSCKPairedHardwareTests.swift b/Tests/FluidDictationIntegrationTests/MeetingSCKPairedHardwareTests.swiftnew file mode 100644index 00000000..2de7e5de--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingSCKPairedHardwareTests.swift@@ -0,0 +1,145 @@+#if DEBUG++@testable import FluidVoice_Debug+import AVFoundation+import CoreMedia+import Foundation+import ScreenCaptureKit+import XCTest++/// One-shot, opt-in C2 topology probe. It is intentionally skipped in the normal suite and never+/// launches or identifies an application beyond the explicitly supplied target bundle ID.+final class MeetingSCKPairedHardwareTests: XCTestCase {+ private static let maximumDurationSeconds = 15.0+ private static let captureDurationSeconds = 5.0++ func testSelectedApplicationPairedSCKMetadataOnly() async throws {+ let environment = ProcessInfo.processInfo.environment+ guard MeetingSCKPairedDiagnosticGate.hardwareEnabled(environment: environment) else {+ throw XCTSkip(+ "Set FLUIDVOICE_C2_DIAGNOSTICS=1, FLUIDVOICE_C2_HARDWARE=1, and "+ + "FLUIDVOICE_C2_TARGET_BUNDLE_ID to run the one-shot C2 probe."+ )+ }+ guard let bundleID = environment[MeetingSCKPairedDiagnosticGate.targetBundleIDEnvironmentKey]?+ .trimmingCharacters(in: .whitespacesAndNewlines), !bundleID.isEmpty else {+ throw XCTSkip("FLUIDVOICE_C2_TARGET_BUNDLE_ID is empty")+ }+ guard AVCaptureDevice.authorizationStatus(for: .audio) == .authorized else {+ throw XCTSkip("microphone permission is not authorized")+ }+ guard let microphone = AVCaptureDevice.default(for: .audio) else {+ throw XCTSkip("no default microphone is available")+ }++ let content: SCShareableContent+ do {+ content = try await SCShareableContent.current+ } catch {+ throw XCTSkip("ScreenCaptureKit shareable content unavailable: \(error.localizedDescription)")+ }+ guard let application = content.applications.first(where: { $0.bundleIdentifier == bundleID }) else {+ throw XCTSkip("target application is not currently running")+ }+ guard let display = content.displays.first else {+ throw XCTSkip("no display is available for the selected-application filter")+ }++ let filter = SCContentFilter(display: display, including: [application], exceptingWindows: [])+ let configuration = SCStreamConfiguration()+ configuration.width = 2+ configuration.height = 2+ configuration.minimumFrameInterval = CMTime(value: 1, timescale: 1)+ configuration.queueDepth = 3+ configuration.showsCursor = false+ configuration.capturesAudio = true+ configuration.sampleRate = 48_000+ configuration.channelCount = 2+ configuration.excludesCurrentProcessAudio = true+ configuration.captureMicrophone = true+ configuration.microphoneCaptureDeviceID = microphone.uniqueID++ let collector = MeetingSCKPairedDiagnosticCollector(environment: environment)+ let provenance = MeetingSCKPairedDiagnosticProvenance(+ scope: .selectedApplicationDisplay,+ applicationSelectionConfirmed: application.processID > 0+ )+ guard provenance.isValid else {+ throw XCTSkip("selected application process provenance is unavailable")+ }+ let output = C2HardwareMetadataOutput(collector: collector, provenance: provenance)+ let stream = SCStream(filter: filter, configuration: configuration, delegate: nil)+ try stream.addStreamOutput(output, type: .audio, sampleHandlerQueue: DispatchQueue(+ label: "fluidvoice.c2.hardware.application", qos: .userInteractive+ ))+ try stream.addStreamOutput(output, type: .microphone, sampleHandlerQueue: DispatchQueue(+ label: "fluidvoice.c2.hardware.microphone", qos: .userInteractive+ ))++ do {+ try await stream.startCapture()+ try await Task.sleep(nanoseconds: UInt64(Self.captureDurationSeconds * 1_000_000_000))+ try await stream.stopCapture()+ } catch {+ try? await stream.stopCapture()+ throw error+ }++ let report = collector.report()+ let data = try report.jsonData()+ print("[C2] sorted report JSON: \(String(decoding: data, as: UTF8.self))")+ XCTAssertTrue(report.enabled)+ XCTAssertEqual(report.provenance, provenance)+ XCTAssertEqual(report.rejectedProvenanceCount, 0)+ XCTAssertEqual(report.droppedAfterBoundCount, 0)+ XCTAssertGreaterThan(report.application.sampleCount, 0)+ XCTAssertGreaterThan(report.microphone.sampleCount, 0)+ XCTAssertEqual(report.acceptedSampleCount, report.application.sampleCount + report.microphone.sampleCount)+ XCTAssertEqual(report.application.validTimestampCount, report.application.sampleCount)+ XCTAssertEqual(report.microphone.validTimestampCount, report.microphone.sampleCount)+ XCTAssertTrue(report.application.firstPresentationSeconds?.isFinite == true)+ XCTAssertTrue(report.application.lastPresentationEndSeconds?.isFinite == true)+ XCTAssertTrue(report.microphone.firstPresentationSeconds?.isFinite == true)+ XCTAssertTrue(report.microphone.lastPresentationEndSeconds?.isFinite == true)+ XCTAssertTrue(report.application.firstSampleRateHz?.isFinite == true)+ XCTAssertTrue(report.microphone.firstSampleRateHz?.isFinite == true)+ XCTAssertGreaterThan(report.application.firstSampleRateHz ?? 0, 0)+ XCTAssertGreaterThan(report.microphone.firstSampleRateHz ?? 0, 0)+ XCTAssertGreaterThan(report.application.firstChannelCount ?? 0, 0)+ XCTAssertGreaterThan(report.microphone.firstChannelCount ?? 0, 0)+ XCTAssertLessThanOrEqual(Self.captureDurationSeconds, Self.maximumDurationSeconds)+ XCTAssertFalse(report.rawPCMRetained)+ XCTAssertFalse(report.transcriptRetained)+ }+}++private final class C2HardwareMetadataOutput: NSObject, SCStreamOutput, @unchecked Sendable {+ private let collector: MeetingSCKPairedDiagnosticCollector+ private let provenance: MeetingSCKPairedDiagnosticProvenance++ init(+ collector: MeetingSCKPairedDiagnosticCollector,+ provenance: MeetingSCKPairedDiagnosticProvenance+ ) {+ self.collector = collector+ self.provenance = provenance+ }++ func stream(+ _ stream: SCStream,+ didOutputSampleBuffer sampleBuffer: CMSampleBuffer,+ of outputType: SCStreamOutputType+ ) {+ let output: MeetingSCKPairedOutput+ switch outputType {+ case .audio: output = .applicationAudio+ case .microphone: output = .microphone+ default: return+ }+ _ = self.collector.record(+ sampleBuffer: sampleBuffer, output: output, provenance: self.provenance+ )+ }+}++#endifdiff --git a/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift b/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swiftindex 2df04d13..c0bfb9d0 100644--- a/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift+++ b/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift@@ -32,6 +32,36 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {XCTAssertEqual(decision, .voiceProcessing)}+#if DEBUG+ func testExplicitC2GateCanForcePairedScreenCaptureKitWithoutChangingDefault() {+ let defaultDecision = MeetingCapturePathDecider.decide(+ mode: .onlineCall, microphone: self.mic(), outputRoute: self.viableRoute()+ )+ let forcedDecision = MeetingCapturePathDecider.decide(+ mode: .onlineCall, microphone: self.mic(), outputRoute: self.viableRoute(),+ forcePairedScreenCaptureKit: true+ )+ XCTAssertEqual(defaultDecision, .voiceProcessing)+ XCTAssertEqual(+ forcedDecision,+ .screenCaptureKit(reason: MeetingCapturePathDecider.diagnosticForceScreenCaptureKitReason)+ )+ guard case let .screenCaptureKit(reason) = forcedDecision else { return }+ XCTAssertTrue(reason.contains("C2 diagnostic"))+ XCTAssertTrue(reason.contains("not a production fallback"))+ }++ func testC2GateIsOnlyEnabledByExplicitEnvironmentValue() {+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.enabled(environment: [:]))+ XCTAssertFalse(MeetingSCKPairedDiagnosticGate.enabled(environment: [+ MeetingSCKPairedDiagnosticGate.environmentKey: "true"+ ]))+ XCTAssertTrue(MeetingSCKPairedDiagnosticGate.enabled(environment: [+ MeetingSCKPairedDiagnosticGate.environmentKey: "1"+ ]))+ }+#endif+func testDecisionDeclinesForInRoomMode() {let decision = MeetingCapturePathDecider.decide(mode: .inRoom, microphone: self.mic(), outputRoute: self.viableRoute())guard case .screenCaptureKit = decision else { return XCTFail("expected decline") }@@ -71,6 +101,19 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {XCTAssertTrue(reason.contains("headphones"))}+ func testDecisionDeclinesForHeadphoneTerminalEvenWhenDataSourceIsUnknown() {+ let route = MeetingOutputRouteSnapshot(+ deviceExists: true,+ isBluetooth: false,+ isBuiltIn: true,+ isHeadphonesDataSource: false,+ terminalTypes: [kAudioStreamTerminalTypeHeadphones]+ )+ let decision = MeetingCapturePathDecider.decide(mode: .onlineCall, microphone: self.mic(), outputRoute: route)+ guard case let .screenCaptureKit(reason) = decision else { return XCTFail("expected decline") }+ XCTAssertTrue(reason.contains("headphones"))+ }+func testDecisionSucceedsForPersonalRole() {let decision = MeetingCapturePathDecider.decide(mode: .onlineCall, microphone: self.mic(role: .personal), outputRoute: self.viableRoute()diff --git a/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingProbeTests.swift b/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingProbeTests.swiftnew file mode 100644index 00000000..6974ada3--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingProbeTests.swift@@ -0,0 +1,222 @@+#if DEBUG+@testable import FluidVoice_Debug+import AudioToolbox+import AVFoundation+import XCTest++final class MeetingVoiceProcessingProbeTests: XCTestCase {+ func testVPIOAcousticGateRequiresBothFlagsAndBoundedDuration() {+ XCTAssertFalse(MeetingVPIOAcousticGate.isEnabled(environment: [:]))+ XCTAssertFalse(MeetingVPIOAcousticGate.isEnabled(environment: ["FLUIDVOICE_MIC_PHASE1": "0.05"]))+ XCTAssertFalse(MeetingVPIOAcousticGate.isEnabled(environment: ["FLUIDVOICE_VPIO_ACOUSTIC": "1", "FLUIDVOICE_MIC_PHASE1": "0.250001"]))+ XCTAssertFalse(MeetingVPIOAcousticGate.isEnabled(environment: ["FLUIDVOICE_VPIO_ACOUSTIC": "1", "FLUIDVOICE_MIC_PHASE1": "nan"]))+ XCTAssertTrue(MeetingVPIOAcousticGate.isEnabled(environment: ["FLUIDVOICE_VPIO_ACOUSTIC": "1", "FLUIDVOICE_MIC_PHASE1": "0.25"]))+ }++ func testTrialBConvertsFirstActivityToStimulusSampleZero() {+ XCTAssertEqual(+ MeetingVPIOAcousticTrialB.stimulusSampleZeroHostSeconds(+ renderActivityStartHostSeconds: 10.2,+ leadingSilenceFrameCount: 9_600,+ sampleRate: 48_000+ )!,+ 10.0,+ accuracy: 1e-12+ )+ XCTAssertNil(MeetingVPIOAcousticTrialB.stimulusSampleZeroHostSeconds(+ renderActivityStartHostSeconds: .infinity,+ leadingSilenceFrameCount: 9_600,+ sampleRate: 48_000+ ))+ }++ func testAcousticCollectorReportsGapOverlapAndSynthesizedTiming() throws {+ let collector = MeetingVPIOAcousticCaptureCollector()+ let empty = collector.window(startHostSeconds: 0, frameCount: 4, sampleRate: 48_000)+ XCTAssertLessThan(empty.window.coverage, 1)++ let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48_000, channels: 1, interleaved: false)!+ func sampleBuffer() throws -> CMSampleBuffer {+ let pcm = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 4)!+ pcm.frameLength = 4+ pcm.floatChannelData![0].update(repeating: 0.1, count: 4)+ return try XCTUnwrap(meetingMicrophoneSynthesizeSampleBuffer(+ from: pcm, presentationTime: CMTime.zero+ ))+ }+ let first = try sampleBuffer()+ collector.ingest(first, synthesized: true, resynced: false)+ collector.ingest(try sampleBuffer(), synthesized: false, resynced: true)+ let window = collector.window(startHostSeconds: 0, frameCount: 4, sampleRate: 48_000)+ XCTAssertGreaterThan(window.window.overlapFrameCount, 0)+ XCTAssertGreaterThan(window.window.synthesizedFrameCount, 0)+ XCTAssertGreaterThan(window.window.resyncedSegmentCount, 0)+ }++ func testVPIOStimulusIsDeterministicAndWithinSafetyBounds() throws {+ let first = try XCTUnwrap(MeetingVPIOAcousticStimulus.make(sampleRate: 16_000))+ let second = try XCTUnwrap(MeetingVPIOAcousticStimulus.make(sampleRate: 16_000))+ XCTAssertEqual(first.samples, second.samples)+ XCTAssertTrue(first.descriptor.withinSafetyBounds)+ XCTAssertLessThanOrEqual(first.descriptor.measuredPeak, first.descriptor.safetyPeakLimit)+ XCTAssertGreaterThan(first.descriptor.measuredRMS, first.descriptor.safetyMinimumRMS)+ }++ func testVPIOMetricsReportsPositiveSignedDelayWhenCaptureLags() throws {+ let sampleRate = 16_000.0+ // Use the same bounded, alignment-rich stimulus family as the hardware trial. A pair of+ // steady tones is intentionally avoided: its repeated phase makes split-half delay+ // agreement ambiguous and should be reported as uncertainty, not treated as a pass.+ let stimulus = try XCTUnwrap(MeetingVPIOAcousticStimulus.make(sampleRate: sampleRate))+ let reference = stimulus.samples+ let delay = 240+ var captured = Array(repeating: Float.zero, count: reference.count)+ captured.replaceSubrange(delay..<reference.count, with: reference[0..<(reference.count - delay)])+ let measurement = MeetingVPIOAcousticMetrics.measure(+ reference: reference, captured: captured, sampleRate: sampleRate, silencePrefixFrames: 0+ )+ XCTAssertTrue(measurement.delay.resolved, "reasons=\(measurement.delay.reasons)")+ XCTAssertEqual(measurement.delay.signedSeconds!, Double(delay) / sampleRate, accuracy: 2 / sampleRate)+ XCTAssertGreaterThanOrEqual(measurement.bands.count, 4)+ }++ func testVPIOMetricsRejectsNegativeSignedDelay() throws {+ let sampleRate = 16_000.0+ let stimulus = try XCTUnwrap(MeetingVPIOAcousticStimulus.make(sampleRate: sampleRate))+ let advance = 240+ var captured = Array(repeating: Float.zero, count: stimulus.samples.count)+ captured.replaceSubrange(+ 0..<(stimulus.samples.count - advance),+ with: stimulus.samples[advance..<stimulus.samples.count]+ )++ let measurement = MeetingVPIOAcousticMetrics.measure(+ reference: stimulus.samples,+ captured: captured,+ sampleRate: sampleRate,+ silencePrefixFrames: 0+ )+ XCTAssertLessThan(measurement.delay.signedSeconds ?? 0, 0)+ XCTAssertFalse(measurement.delay.resolved)+ XCTAssertTrue(measurement.delay.reasons.contains(.negativeDelay))+ }++ func testVPIOMetricsMarksSilenceLowExcitationAndMismatchedControlUnknown() {+ let silence = Array(repeating: Float.zero, count: 5_000)+ let silentMeasurement = MeetingVPIOAcousticMetrics.measure(+ reference: silence, captured: silence, sampleRate: 16_000, silencePrefixFrames: 1_024+ )+ XCTAssertFalse(silentMeasurement.valid)+ XCTAssertTrue(silentMeasurement.reasons.contains(.referenceBelowExcitationFloor))++ let reference = (0..<12_000).map { Float(sin(Double($0) * 0.173)) }+ let control = (0..<12_000).map { Float(cos(Double($0) * 0.093 + 0.8)) }+ let controlMeasurement = MeetingVPIOAcousticMetrics.measure(+ reference: reference, captured: control, sampleRate: 16_000, silencePrefixFrames: 0+ )+ XCTAssertFalse(controlMeasurement.valid)+ XCTAssertTrue(+ controlMeasurement.reasons.contains(.delayConfidenceBelowThreshold)+ || controlMeasurement.reasons.contains(.delayUnresolved)+ || controlMeasurement.reasons.contains(.tooFewCoherentBands)+ )+ }++ func testVPIOTrialBReportJSONRoundTripHasNoRawSamples() throws {+ let stimulus = try XCTUnwrap(MeetingVPIOAcousticStimulus.make(sampleRate: 16_000))+ let report = MeetingVPIOAcousticTrialBReport(+ schemaVersion: MeetingVPIOAcousticTrialBReport.currentSchemaVersion,+ stimulus: stimulus.descriptor, renderVolume: 0.5, preRollSeconds: 0.3,+ postRollSeconds: 0.6, settleSeconds: 0.35, runs: [],+ caveats: MeetingVPIOAcousticTrialBReport.interpretationCaveats+ )+ let data = try JSONEncoder().encode(report)+ XCTAssertEqual(try JSONDecoder().decode(MeetingVPIOAcousticTrialBReport.self, from: data), report)+ let json = String(decoding: data, as: UTF8.self)+ XCTAssertFalse(json.contains("samples"))+ XCTAssertFalse(json.contains("transcript"))+ }++ func testFailedUInt32ReadbackNeverPublishesValue() {+ let readback = MeetingAudioUnitUInt32Readback(+ propertyID: kAUVoiceIOProperty_BypassVoiceProcessing,+ scope: kAudioUnitScope_Global,+ element: 0,+ status: kAudioUnitErr_InvalidProperty,+ value: 42+ )++ XCTAssertFalse(readback.succeeded)+ XCTAssertNil(readback.value)+ }++ func testFailedDuckingReadbackNeverPublishesFields() {+ let readback = MeetingAudioUnitDuckingReadback(+ propertyID: kAUVoiceIOProperty_OtherAudioDuckingConfiguration,+ scope: kAudioUnitScope_Global,+ element: 0,+ status: kAudioUnitErr_InvalidProperty,+ advancedDuckingEnabled: true,+ duckingLevelRawValue: 30+ )++ XCTAssertNil(readback.advancedDuckingEnabled)+ XCTAssertNil(readback.duckingLevelRawValue)+ }++ func testProbeSnapshotJSONRoundTripPreservesRawEvidence() throws {+ let format = AVAudioFormat(+ commonFormat: .pcmFormatFloat32,+ sampleRate: 48_000,+ channels: 1,+ interleaved: false+ )!+ let successfulRead = MeetingAudioUnitUInt32Readback(+ propertyID: kAUVoiceIOProperty_VoiceProcessingEnableAGC,+ scope: kAudioUnitScope_Global,+ element: 0,+ status: noErr,+ value: 1+ )+ let snapshot = MeetingVoiceProcessingProbeSnapshot(+ schemaVersion: MeetingVoiceProcessingProbeSnapshot.currentSchemaVersion,+ engineRunning: true,+ nodeVoiceProcessingEnabled: true,+ nodeVoiceProcessingBypassed: false,+ nodeVoiceProcessingAGCEnabled: true,+ nodeVoiceProcessingInputMuted: false,+ nodeAdvancedDuckingEnabled: false,+ nodeDuckingLevelRawValue: 10,+ inputPresentationLatencySeconds: 0.01,+ outputPresentationLatencySeconds: 0.02,+ inputNodeInputFormat: MeetingAudioFormatProbeReadback(format),+ inputNodeOutputFormat: MeetingAudioFormatProbeReadback(format),+ outputNodeInputFormat: MeetingAudioFormatProbeReadback(format),+ outputNodeOutputFormat: MeetingAudioFormatProbeReadback(format),+ inputNodeLastRenderTime: MeetingAudioTimeProbeReadback(nil),+ outputNodeLastRenderTime: MeetingAudioTimeProbeReadback(nil),+ inputNodeOutputConnectionCount: 0,+ inputCurrentDevice: successfulRead,+ outputCurrentDevice: successfulRead,+ bypassVoiceProcessing: successfulRead,+ voiceProcessingAGCEnabled: successfulRead,+ voiceProcessingOutputMuted: successfulRead,+ otherAudioDucking: MeetingAudioUnitDuckingReadback(+ propertyID: kAUVoiceIOProperty_OtherAudioDuckingConfiguration,+ scope: kAudioUnitScope_Global,+ element: 0,+ status: noErr,+ advancedDuckingEnabled: false,+ duckingLevelRawValue: 10+ )+ )++ let data = try JSONEncoder().encode(snapshot)+ let decoded = try JSONDecoder().decode(MeetingVoiceProcessingProbeSnapshot.self, from: data)++ XCTAssertEqual(decoded, snapshot)+ XCTAssertEqual(decoded.voiceProcessingAGCEnabled.value, 1)+ XCTAssertEqual(decoded.otherAudioDucking.duckingLevelRawValue, 10)+ }+}+#endifdiff --git a/tools/MEETING_ADMISSION_B3_DEVELOPMENT_EVAL.md b/tools/MEETING_ADMISSION_B3_DEVELOPMENT_EVAL.mdnew file mode 100644index 00000000..16cdc9aa--- /dev/null+++ b/tools/MEETING_ADMISSION_B3_DEVELOPMENT_EVAL.md@@ -0,0 +1,118 @@+# B3 development ablation — existing recordings++Date: 2026-09-08 (local), updated 2026-09-09 with five controlled recordings. Status: **insufficient for threshold selection**.++## Outcome++The available evidence does not support enabling microphone admission. The current temporal detector produces no supported duplicate windows, so its ablation is identical to accepting every measured frame. Speech activity shows a tradeoff but cannot establish near-end speech: settings that preserve nearly all frames in the coarse scripted region admit hundreds of known-negative frames, while stricter settings still admit negative frames and reject many frames inside that coarse region.++No threshold was selected or frozen. The installed app, runtime configuration, original recordings, transcripts and speaker profiles were not changed. This evaluator reads existing saved text locally only to count words and never emits text, speaker identifiers, segment identifiers, audio or embeddings.++## Controlled-recording update++Five user-directed development recordings were captured with the installed app and analyzed locally: playback only; normal and quiet speech without playback; normal speech over playback; and two sequences of quiet replies over playback. The user confirmed that playback used speakers, their speech entered through the microphone, and nobody else was speaking nearby. No headphones were used. All ten audio tracks completed without a dropped sample, discontinuity or recorded failure. The longer playback-only recording produced four visible microphone-attributed segments containing 49 words even though the user reported staying silent. This is a controlled acoustic-playback failure example; it does not by itself identify the exact hardware/software leakage path.++The B1 temporal detector found no `duplicateSupported` window in any controlled recording. The playback-only recording produced three isolated `duplicateCandidate` windows, but never the required stable three-window sequence. Temporal-only admission therefore still accepts every measured negative frame.++At the existing diagnostic speech threshold of 0.85, the controlled playback-only recording admitted 10/600 measured negative frames. The coarse positive regions admitted 137/445 measured frames: 24/42 in clean normal speech, 16/36 in clean quiet speech, 63/135 in normal speech over playback, and 34/232 in the whole-recording quiet-replies-over-playback region. These positive denominators include pauses and, for the overlap recordings, playback-only time; their acceptance is not recall. The transcript localized the three normal-over-playback repetitions into coarse regions, but this is not independent ground truth. The quiet-over-playback recording remains recording-level coarse truth.++Across all eight development recordings, threshold 0.04 admitted 681/2,291 known-negative frames and 507/606 coarse-region frames. Threshold 0.85 admitted 41/2,291 known-negative frames and 258/606 coarse-region frames. This is a real tradeoff, not a calibration result: frames are correlated, coarse regions contain pauses, and there are still zero independently frame-labelled positive recordings. No threshold is selected and suppression remains off.++Current aggregate result after recording the corrected speaker/microphone provenance: `/private/tmp/fv-admission-b3-controlled-v3.json`. The five numeric-only sidecars are `/private/tmp/fv-speech-control-{playback,normal,quiet,normal-overlap,quiet-overlap}-v1.json`. Audio and transcript content stayed local and no installed app was changed.++## Ground truth that is actually available++The development annotation manifest is `meeting_admission_b3_annotations.json`:++- `3774ED4C…`: user reported no near-end speech for the recording.+- `3B9AC1A8…`: user reported playback-only for the recording.+- `88E3BD44…`: user reported speaking only the supplied script near the end. The 0–208 s interval is treated as known-negative. The 208–250 s interval is only a **coarse script region**; its individual speech and pause frames are not labelled. The remaining tail is unlabelled.++These are failure-selected, post-hoc development recordings. There are two pure-negative recordings, one recording containing both a negative interval and a coarse script region, and zero independently frame-labelled positive recordings. The cohort counts intentionally overlap where one recording supplies both kinds of interval.++## Current saved transcript inventory++For microphone segments fully contained in annotated intervals:++| Annotation | Segments | All words | Currently visible words | Visible speaker slots per session |+| --- | ---: | ---: | ---: | ---: |+| Known-negative intervals | 21 | 331 | 324 | 4 |+| Coarse script region | 3 | 89 | 89 | 1 |++“Visible” reuses the saved pipeline's own `isLikelyEcho` output and is reported only as current-output inventory, not as independent validation. The 89 coarse-region words are not verified for correctness. Full-containment is used to avoid assigning a boundary-crossing segment wholesale; boundary/outside exclusions are separately counted in the JSON result. This dataset happened to have no excluded microphone transcript segments.++## Frame evidence and descriptive sweep++There are 1,691 measured 256 ms frames in known-negative intervals and 161 measured frames in the coarse script region. Another 21 labelled frames have explicit unknown speech evidence; two frames straddle annotation boundaries and 39 fall outside annotations. All denominators and exclusions are present in the result.++| Diagnostic VAD threshold | Known-negative frames accepted | Negative acceptance | Coarse-region frames accepted | Coarse-region acceptance |+| ---: | ---: | ---: | ---: | ---: |+| 0.01 | 1,078 / 1,691 | 63.7% | 161 / 161 | 100% |+| 0.04 | 549 / 1,691 | 32.5% | 160 / 161 | 99.4% |+| 0.05 | 464 / 1,691 | 27.4% | 158 / 161 | 98.1% |+| 0.10 | 282 / 1,691 | 16.7% | 145 / 161 | 90.1% |+| 0.50 | 66 / 1,691 | 3.9% | 123 / 161 | 76.4% |+| 0.85 | 31 / 1,691 | 1.8% | 121 / 161 | 75.2% |+| 0.95 | 20 / 1,691 | 1.2% | 121 / 161 | 75.2% |++Coarse-region acceptance is **not speech recall**: some rejected frames can be pauses, and some accepted frames can be leaked playback. Frame observations are recurrent and temporally correlated, so 1,691 frames are not 1,691 independent trials. Recording-level uncertainty cannot be meaningfully estimated from two pure-negative recordings and no independently labelled positive recording.++The “speech plus no supported duplicate” ablation equals speech-only on these inputs because B1 produced zero supported locks. The accept-all control and current temporal-only policy both accept all 1,691 measured negative frames. The full sweep is included for diagnosis only; it does not rank candidates or publish a selection gate. It does not project frame decisions into word removal, WER or speaker identity changes.++## Evaluator integrity++`meeting_admission_b3_eval.py` validates:++- canonical session IDs and safe sidecar basenames;+- session-byte hash agreement with each sidecar;+- pinned B1/B2 versions, 4,096/16 kHz frame geometry, 0.85 diagnostic threshold and identical cached model file hashes;+- finite probabilities, explicit reasons for every unknown probability, known temporal states and annotation ends within recorded duration;+- non-overlapping, finite annotation intervals and exclusive output creation.++It reports measured/unknown/boundary/outside frames explicitly. Transcript segments crossing boundaries or outside annotation are counted separately rather than silently disappearing. Empty sessions/annotations are rejected. The cached model artifact dictionary is pinned to fingerprint `259aec29878cbca1118f4e4fa1db126676286c90b65903266545f50e9ac4512d`, and sidecar duration must agree with an independent calculation from session audio-chunk PTS. Eleven unit tests cover interval containment, overlap/traversal/empty-input rejection, confusion counts, privacy, threshold inventory, unknown probability handling, duration calculation and transcript boundary accounting.++The original three-recording aggregate was `/private/tmp/fv-admission-b3-dev-v7.json`. The controlled eight-recording aggregate `/private/tmp/fv-admission-b3-controlled-v3.json` supersedes it for current development analysis. Reproduce from the repository root:++```sh+python3 tools/meeting_admission_b3_eval.py \+ --annotations tools/meeting_admission_b3_annotations.json \+ --sessions-root "$HOME/Library/Application Support/FluidVoice/Meetings" \+ --sidecar-root /private/tmp \+ --output /private/tmp/fv-admission-b3-dev-new.json+```++The command above is documentation for a human shell; the evaluator itself never expands or emits the sessions path. Use a fresh output filename because overwrite is refused.++## Next evidence gate++The next useful input is an independently timed controlled recording with frame-level labels for:++1. clean normal and quiet speech without playback;+2. playback-only intervals;+3. short quiet replies during playback;+4. sustained normal speech during playback;+5. deliberate pauses and missing/reference-transition intervals.++This first positive must remain development data. After any feature/threshold changes, separate recordings by speaker/device/environment are required for held-out evaluation. Until then, suppression and profile filtering remain off. If a stronger temporal feature or model dependency is proposed, that is a separate reviewed scope.++## Review and artifact identity++Kimi K3 adversarially reviewed the annotation/evaluator/test source in read-only mode. Its first pass found silent denominator drift, hard-coded frame duration, unreported boundary exclusions, overlapping cohort-count ambiguity and selection-flavored output. All were fixed: unknowns/exclusions are counted, frame geometry is schema-derived and pinned, cohort fields explicitly describe overlap, and candidate lists were removed. A second pass found empty annotations, consistency-only model hashes and sidecar-only duration bounds; all were fixed with nonempty-input checks, a pinned model fingerprint and independent session-duration verification. The final post-fix verification returned **no findings**. No audio, transcript content, model weights or secrets were sent to the reviewer.++Original three-recording SHA-256 values are retained below for audit history:++```text+annotations 95a0a11bf12400e22ec8d00de36397d491e90520fcc5b7e61dc3135fa0b4e955+evaluator 886187f58085207802cae8f713d72206f70272d767a9dbb3d15fe16ec7e301b1+tests 93dfb7673a9dbf60775e8e351d5781f4cd940b1a5a09f88eb2e202430d58b5a5+result 26d01f04a558ee88c26bb105451e42a74987048f7b3c923dc3635f98d5ef0c9e+```++Controlled-update SHA-256:++```text+annotations 0837a3d5935a7ca945217c2fe869863ad39268a2ac7a1ce9ea0fbe8b400280cb+evaluator cc4bfa8647a60c5d7cd3422f0defe8959ffe9fd59a24485fc6f10d0b80d2a108+tests 93dfb7673a9dbf60775e8e351d5781f4cd940b1a5a09f88eb2e202430d58b5a5+result 875437729970fbc763d589a49961cd2045a30153f1a0dc9ea8d72df368e7655d+```diff --git a/tools/MEETING_ADMISSION_CONTROLLED_RECORDING.md b/tools/MEETING_ADMISSION_CONTROLLED_RECORDING.mdnew file mode 100644index 00000000..e1b6f7f4--- /dev/null+++ b/tools/MEETING_ADMISSION_CONTROLLED_RECORDING.md@@ -0,0 +1,63 @@+# Simple controlled microphone-admission recordings++Make five separate FluidVoice meeting recordings. Each should last roughly 20–30 seconds and test only one condition. Exact timing is unnecessary.++Use the currently installed app and your normal microphone settings. Use speakers if that is how the false captions occurred. For playback tests, use the same speech-heavy podcast or video each time.++## Recording 1 — playback only++1. Start the podcast or video through your speakers.+2. Start a new FluidVoice meeting recording.+3. Stay completely silent for about 20–30 seconds.+4. Stop the recording.++## Recording 2 — normal speech, no playback++1. Stop all playback.+2. Start a new recording.+3. In your normal voice, read this twice:++ “The blue lantern is beside the window. I will review the document after lunch.”++4. Stop the recording.++## Recording 3 — quiet speech, no playback++1. Keep playback stopped.+2. Start a new recording.+3. In a naturally quiet meeting voice, read this twice:++ “A small silver key rests under the green notebook.”++4. Stop the recording.++## Recording 4 — normal speech over playback++1. Start the podcast or video through your speakers.+2. Start a new recording.+3. In your normal voice, read this twice:++ “The blue lantern is beside the window. I will review the document after lunch.”++4. Stop the recording, then stop playback.++## Recording 5 — quiet replies over playback++1. Start the podcast or video through your speakers.+2. Start a new recording.+3. In a naturally quiet voice, say these phrases with a short pause between them:++ “Yes.” “Not yet.” “I agree.” “Please continue.” “That is correct.”++4. Repeat the five phrases once, then stop the recording and playback.++## When finished++Tell Codex:++- approximately when you completed the five recordings;+- whether you used speakers or headphones;+- whether anyone else spoke nearby;+- whether any recording differed from the instructions.++Codex will locate the five local sessions and analyze them offline. No recording will be sent to an external reviewer. These are development recordings; separate held-out recordings are still required before enabling suppression.diff --git a/tools/MEETING_DIARIZATION_EVAL.md b/tools/MEETING_DIARIZATION_EVAL.mdnew file mode 100644index 00000000..a70974f2--- /dev/null+++ b/tools/MEETING_DIARIZATION_EVAL.md@@ -0,0 +1,41 @@+# Meeting diarization evaluation++This Phase 0 tool establishes a repeatable local baseline before diarization behavior changes. It+never uploads, rewrites, or copies meeting recordings. User audio, annotations, hypotheses, and+generated reports must stay under the gitignored `benchmark_reports/` directory.++## Set up the corpus++Copy `tools/meeting_diarization_eval_manifest.example.json` to+`benchmark_reports/meeting-diarization/manifest.json`. Add development and held-out fixtures before+tuning. `inventory` fixtures reserve the split without claiming that they are ground truth; change a+fixture to `annotated` only after adding both `referenceRTTM` and `hypothesisRTTM`. An inventory+fixture may record `expectedSpeakerCount`, `observedRemoteSpeakerCount`, a generated+`hypothesisRTTM`, and `runReport`; these reproduce a count defect and fingerprint its artifacts but+remain explicitly unscored until a human reference timeline exists.++RTTM rows use the standard form:++```text+SPEAKER meeting 1 12.340 1.250 <NA> <NA> speaker-1 <NA> <NA>+```++Optional `referenceSpeakerText` and `hypothesisSpeakerText` files are JSON objects mapping speaker+labels to transcript text. When present, the report includes cpWER.++## Run++```bash+python3 tools/meeting_diarization_eval.py self-test+python3 tools/meeting_diarization_eval.py validate benchmark_reports/meeting-diarization/manifest.json+python3 tools/meeting_diarization_eval.py score benchmark_reports/meeting-diarization/manifest.json \+ --output benchmark_reports/meeting-diarization/baseline.json+```++Each scored fixture reports primary merge/split and speaking-duration recovery metrics plus DER, JER,+speaker-count error, coverage, and purity. The `comparable` score uses a 250 ms collar and ignores+reference overlap; the `strict` score uses no collar and scores overlap.++Reports contain aggregate metrics and SHA-256 fingerprints of RTTM inputs, not audio or transcript+content. Record ASR provider/model, pipeline version, and normalization configuration in the manifest+for every baseline run.diff --git a/tools/MEETING_MICROPHONE_ADMISSION_PLAN_REVIEW.md b/tools/MEETING_MICROPHONE_ADMISSION_PLAN_REVIEW.mdnew file mode 100644index 00000000..13a1f204--- /dev/null+++ b/tools/MEETING_MICROPHONE_ADMISSION_PLAN_REVIEW.md@@ -0,0 +1,75 @@+# Kimi K3 review of microphone-admission plan revision 2++Disposition: revision 3 addresses the six plan findings. A subsequent read-only Stage A code-review request in the same OpenCode session timed out at 180 seconds without findings; do not interpret it as approval. Local Stage A validation passed 20 tests.++Read-only review through OpenCode; amended implementation has not been signed off by this review. The tool's dirty-tree listing includes pre-existing work, not a claim the reviewer authored it.++status: success+sessionID: ses_f7bec653effeM3RqWBphVhxcRv+model: fireworks-ai/accounts/fireworks/models/kimi-k3+agent: plan+elapsed: 211.0s+tokens: {"total":74870,"input":1633,"output":1374,"reasoning":8383,"cache":{"write":0,"read":63480}}+cost: $0.448019+changed files:+Fluid.xcodeproj/project.pbxproj | 367 ++++++++++++++++++++++ MEETING_AUDIO_PHASE0_RUNBOOK.md | 14 +-+ Sources/Fluid/Persistence/KeychainService.swift | 8 +-+ Sources/Fluid/Services/FileLogger.swift | 6 ++ Sources/Fluid/Services/FluidAudioProvider.swift | 144 +++++++-+ .../LiveTranscription/MeetingLiveTrackEngine.swift | 133 +++++++-+ .../MeetingMicrophoneCaptureTests.swift | 78 ++++-+ 7 files changed, 714 insertions(+), 36 deletions(-)+?? Fluid.xcodeproj/xcshareddata/xcschemes/FluidASRBaseline.xcscheme+?? MEETING_DIARIZATION_V2_IMPLEMENTATION_PLAN.md+?? MEETING_MICROPHONE_ADMISSION_IMPLEMENTATION_PLAN.md+?? MEETING_RELIABILITY_IMPLEMENTATION_PLAN.md+?? MEETING_RELIABILITY_P0_BASELINE.md+?? Sources/Fluid/Services/Meeting/MeetingASRPreparationOwner.swift+?? Sources/Fluid/Services/Meeting/MeetingProcessingConfiguration.swift+?? Tests/FluidASRBaselineHost/+?? Tests/FluidASRBaselineTests/+?? Tests/FluidDictationIntegrationTests/MeetingASRPreparationOwnerTests.swift+?? Tests/FluidDictationIntegrationTests/MeetingProcessingConfigurationTests.swift+?? Tests/FluidDictationIntegrationTests/MeetingProviderOptionsTests.swift+?? fluid_pico_cutB_wingbeat_51s.mp4+?? fluid_pico_cutB_wingbeat_53s.mp4+?? fluid_pico_false_start_10s_prompt.md+?? fluid_pico_false_start_74s.mp4+?? fluid_pico_false_start_prompt.md+?? fluid_pico_false_start_v3.mp4+?? fluid_pico_hero_76s.mp4+?? fluid_pico_sprinter.mp4+?? render_pico.py+?? render_pico_split.py+?? render_pico_v2.py+?? tools/MEETING_DIARIZATION_EVAL.md+?? tools/MEETING_RELIABILITY_P0_ADVERSARIAL_REVIEW.md+?? tools/meeting_reliability_baseline.py+?? tools/test_meeting_reliability_baseline.py++Review complete. Plan rev 2 read against the five permitted source files; findings below, Stage A blockers first.++## Stage A blockers++**1. [High] "Shadow disabled AND enabled" is untestable as written — Stage A defines no enablement mechanism or call site.**+Stage A item 4 specifies a pure shadow decision function with injected evidence, and the exit gate requires behavior unchanged "with shadow disabled AND enabled" — but the `off`/`shadow`/`enforce` mode flag is explicitly Stage D (item 3), and no Stage A item names where the shadow function is invoked (tests only? a debug gate? unconditionally inside `processMicrophoneChunk`?). An implementer must invent the switch, and the cheapest invention is evaluating unconditionally in the live per-chunk path — adding per-turn work and a new failure surface in production alongside the echo scoring `do/catch` at pipeline :1972-2002. Same gap covers the fake temporal-detector implementation (A3): "fake" is undefined, so it could end up wired into the real path returning constants. Minimal correction: state in Stage A that the shadow function and fake detector are invoked only from tests (or a named debug-only gate), are side-effect-free, never persisted, never logged with transcript content, and that any runtime mode flag is out of scope until Stage D.++**2. [High] The legacy-mapping boundary is ambiguous exactly where the dangerous semantics live: the rescue inside `verdict()`.**+A1/A2 say "rename" plus "a clearly named legacy mapping" without stating whether the mapping wraps the existing `verdict()` output or reimplements the decision. The behavior that must survive is not just the `effectiveEcho` truth table (pinned by `testEffectiveEchoMatrix`); it is the scorer-internal rescue, which (a) runs *before* the coverage gate, so it fires even when `scoreableFraction < 0.5` (scorer :285-289); (b) lets NaN gaps under 0.75 s accumulate through without breaking a low run (:262-267); and (c) is denied only by the conjunctive trivial-run test (scoreable coverage AND median ≥ 0.8 AND lowRunFraction < 0.15, :280-283). A mapping written at the segment/`effectiveEcho` level cannot reproduce (a)-(c); a reimplementation can silently drop one. Minimal correction: mandate that Stage A is a mechanical rename of the enum case with zero logic edits to `verdict()`/`effectiveEcho`, and add characterization tests pinning (a), (b), and (c) before the rename lands.++**3. [Medium] The evidence contract omits the identity keys Stage C's shared-centroid exclusion needs.**+The evidence field list (§3) has interval coverage and embedding provenance but no turn/segment key and no diarization observation key. The contamination unit is the chunk-level label cluster: one `Observation` per `microphone:<chunkID>:<label>`, one centroid, shared by every turn with that label (pipeline :1937-1964, :2027), admitted today by text-only `isLikelyEcho` (:720-726). Turn-granular evidence without the observation key cannot express "rejected sibling ⇒ shared centroid ineligible" (C2), and since the contract is Stage A's deliverable, omitting the keys means reworking a test-pinned contract later. Minimal correction: add mandatory identity fields to the Stage A contract — turn key (`microphone:<chunkID>:<index>`), observation key (`microphone:<chunkID>:<label>`), and capture-epoch identifier — noting the one-to-many observation→turn relationship.++**4. [Medium] Adversarial unit cases (A5) have unspecified expected outcomes.**+"Identical real repetition of playback" is scored `echo` and hidden by the *current* code — that is today's behavior, which Stage A must preserve. If the new tests assert the aspirational outcome, they change behavior; if they assert the current outcome as the policy target, they entrench it. Rule 2 ("low explanation never rescues") deliberately inverts the live rescue, so shadow and legacy disagree by construction on every rescued turn. Minimal correction: each A5 case asserts two explicit expectations — legacy-mapping output equals pre-change output, shadow output marked experimental with its own reason code — and the rescue-removal delta gets a dedicated reason code so Stage B's would-suppress accounting isn't dominated by the deliberate inversion.++## Later-stage concerns++**5. [Medium] The temporal detector's "capture epoch" is undefined relative to existing staleness machinery.**+The plan requires freshness/reset on discontinuities but never binds "capture epoch" to `epochBoundaries` (either-track discontinuities, pipeline :1110-1118) or to the ±`blockSeconds` guard intervals the scorer zeroes/masks (:998-1047). If the detector's epoch is narrower than the delay consensus's, duplicate state can survive the clock jump that invalidated the delay estimate — cross-jump correlation presented as fresh support. Minimal correction (Stage B1): define the detector epoch as the intervals between `epochBoundaries`, mandate state reset plus evidence invalidation at those boundaries, and state whether the scorer's guard-interval policy is reused or replaced.++**6. [Low] "Never scored" and "scored but inconclusive" are collapsed today and will pollute Stage B's unknown reporting.**+`computeEchoVerdicts` returns an empty verdicts dict when no delay resolves (:1199-1201), and the call site folds absence into `.unknown` (`signalVerdictByIndex[index] ?? .unknown`, :2041) — indistinguishable from a fully scored inconclusive turn. Stage A's typed evidence can carry the distinguished values in parallel with the legacy-collapsed one, but the plan doesn't require them as separate reason codes; without that, Stage B's "complete unknown/fallback reporting" exit cannot tell a blind scorer (no lock, no reference) from an inconclusive one. Minimal correction: name distinct reason codes for `noDelayLock`, `referenceUnavailable`, `unscoredCoverage`, and `scoredInconclusive` in the Stage A contract, with the legacy mapping defined as their collapse into `.unknown`.++No findings on the evidence calibration claims themselves; per scope, nothing here asserts the detector will work — finding 5's note that the observed 55-70 ms lag is "a hypothesis to test" is the correct posture.diff --git a/tools/MEETING_PLAYBACK_ATTRIBUTION_V2_ADVERSARIAL_REVIEW.md b/tools/MEETING_PLAYBACK_ATTRIBUTION_V2_ADVERSARIAL_REVIEW.mdnew file mode 100644index 00000000..92d329cf--- /dev/null+++ b/tools/MEETING_PLAYBACK_ATTRIBUTION_V2_ADVERSARIAL_REVIEW.md@@ -0,0 +1,87 @@+# Adversarial review — reference-aware playback attribution plan++Date: 2026-09-09 (local)+Reviewed artifact: `MEETING_PLAYBACK_ATTRIBUTION_V2_IMPLEMENTATION_PLAN.md` revision 1+Result: revision 2 incorporates the supported findings below. No implementation or production approval is claimed.++## Review execution++All reviewers were instructed to operate read-only and were limited to the plan and relevant FluidVoice capture/scoring source. No recordings, transcript content, logs, model weights, credentials, secrets, or unrelated files were included. The dirty-tree summaries printed by the tools describe the pre-existing shared worktree; all successful review results reported no reviewer edits.++| Requested reviewer | Successful result | Notes |+| --- | --- | --- |+| Claude Opus 5 | Orchestrate heavy route reported `claude · opus`; the response identified `claude-opus-5`; 294.5 s; `editedTree=false` | The first broad call hit the orchestration transport's 300 s timeout. A narrower retry completed. |+| Kimi K3 | `fireworks-ai/accounts/fireworks/models/kimi-k3`; 219.7 s; OpenCode plan mode | Completed on the first call. |+| Grok 4.6 | `grok-4.6`; xAI Grok CLI session `9e315fa8-069f-4ef7-9713-a985e2e01a9c` | Two bounded turns ended at their tool-turn limits; a read-only continuation in the same session returned the final review. |++Timeouts and turn-limit exits contained no review findings and were not counted as approval. The completed responses were reconciled against local source and Apple/WebRTC primary documentation.++## Convergent blockers and dispositions++### 1. Test the VPIO render side before adding WebRTC — accepted++Claude and Grok identified that `MeetingMicrophoneCapture` binds/read-backs audio-unit element 1 (input) but not element 0 (output), has no explicit application render source, and persists `voiceProcessingEnabled: true` as a literal after the setter rather than a readback. Kimi independently asked for exact public property IDs before the AGC/bypass trial.++Revision 2 now makes these the first probe: input/output current-device properties, graph/render activity, `isVoiceProcessingEnabled`, bypass, AGC, mute, and ducking state with exact property/scope/element/`OSStatus`. Element-0 force binding is diagnostic-only until route behavior is understood. The plan still treats missing render reference as a hypothesis until hardware results exist.++### 2. Evaluate the existing SCK paired microphone path before AEC3 — accepted++All three reviewers found that the repository already implements one `SCStream` producing selected-application `.audio` plus `.microphone`. This can provide a rawer microphone input and potentially a simpler clock relationship, but neither property is guaranteed by the public API.++Revision 2 adds this as Phase 0 trial C2 and the leading software-AEC input. It requires measurement of processing, clock relationship, latency, and jitter. It also records the critical product constraint: on speaker routes this path is currently `.unprotected`, so it cannot be called behavior-unchanged shadow mode and needs a separately reviewed capture migration before production.++### 3. A selected-application reference is incomplete — accepted with a privacy boundary++All reviewers noted that the microphone hears the full acoustic output while the current reference contains only the selected window/application and excludes FluidVoice. Other processes, notifications, helper processes, volume changes, and output DSP may be missing or transformed. AEC cannot cancel an absent source.++Revision 2 adds explicit reference scope and `referenceScopeLimited` / `referenceCompletenessUnobservable` outcomes, concurrent-other-process and volume-change trials, corpus cases, and fail-open semantics. It also refuses to make a broader full-system reference an automatic product default: full-mix diagnostic capture must be explicitly authorized, ephemeral, not transcribed, and separately reviewed for scope/retention/privacy. Without it, the feature promises only selected-scope playback suppression.++### 4. Do not prefer VPIO output plus AEC3 — accepted++All reviewers agreed that cascading AEC3 after Apple's nonlinear AEC/NS/AGC is the least identifiable path. Low-level intelligible leakage can have little stable linear coherence, so AEC3 may remain inert or damage quiet overlap.++Revision 2 orders the candidates as Apple configuration repair, SCK-paired mic plus AEC3, exclusive raw diagnostic if it adds information, and VPIO plus AEC3 only as a fallback. It adds convergence/ERLE/inertness rejection criteria and forbids tuning around a non-learnable VPIO residual.++### 5. Export both linear and suppressed AEC3 outputs — accepted++Claude and Grok noted that listening-optimized nonlinear suppression can damage quiet double-talk and that `EchoCanceller3` can expose a linear-filter output separately. Revision 2 requires both outputs, pins suppressor/comfort-noise/high-pass/field-trial configuration, and makes the linear output the first ASR-preservation candidate. Neither output is assumed safe until the bake-off.++### 6. Residual speech is not automatically near-end speech — accepted++Grok required the independent multi-band attribution candidate (or equivalent guard) for enforcement rather than making it optional. Revision 2 adopts that requirement. VAD, residual energy, AEC validity, or ASR text alone cannot produce a playback-only/near-end conclusion.++### 7. Restore the parent reliability gates — accepted++Kimi and Claude found that revision 1's suggested 95% reduction and five-point WER margin weakened the existing reliability plan. Revision 2 restores the binding parent gates: zero false accepted mic words/identities on the curated negative regression set, one-point missed-speech/WER non-inferiority margins, 12 negative sessions, 30 annotated minutes per populated positive condition, 100 short replies, recording/device/speaker splits, and recording-level uncertainty. Unscored/fail-open intervals remain in leak totals so abstention cannot pass.++### 8. Time-box and front-load AEC3 build feasibility — accepted++All reviewers warned that a “minimal AEC3 subset” still pulls substantial WebRTC infrastructure. Revision 2 names expected dependency families, requires an out-of-tree reproducible two-architecture smoke artifact before in-tree code, pins field-trial behavior, and defines failure to produce it within an approved time box as an abort outcome rather than permission to vendor the full tree or fetch a mutable binary.++### 9. Tighten synchronizer semantics — accepted++The reviews called out three distinct clocks/timelines, signed acoustic delay, ppm drift, codec priming/remainder, converter delay, multi-channel render policy, and valid samples with synthesized timing. Revision 2 explicitly models those, freezes adaptation across synthesized-timing spans, preserves masks instead of zero-fill, and scopes deterministic equality to the same binary/architecture/configuration.++### 10. Avoid residual-embedding domain drift — accepted with a safer first-release rule++Claude noted that AEC residual embeddings may be out-of-domain relative to existing natural-speech profiles. Revision 2 adds an embedding-domain metric, but does not make a residual embedding the first-release default. Profile learning is limited to independently admitted, verified no-playback intervals using original microphone PCM; overlap residuals do not enroll profiles.++## Findings adopted with modification++### Original-versus-residual ASR disagreement++Kimi proposed retaining an interval when original ASR has words and residual ASR is empty. Applied literally, that would restore the exact playback words a successful canceller removed. Revision 2 instead degrades to `mixedOrUncertain` only when the empty residual conflicts with residual-side speech activity, independent attribution, or timing evidence. Original/residual ASR remains a diagnostic pair; neither wins solely because it emitted words.++### Full-system reference++Claude suggested a second full-system SCK stream as the better AEC reference. Technically it may improve completeness, but silently capturing unrelated applications would expand user-authorized scope. Revision 2 includes an opt-in diagnostic comparison and requires a separate privacy/product decision before any production use. The selected-app-only limitation remains visible rather than being hidden by implementation convenience.++### SCK paired streams and clock sharing++Reviewers described the paired stream as the topology most likely to share a useful clock. Revision 2 does not promote that expectation to fact; Phase 0 measures PTS relationship, latency, drift, and jitter.++## Overall disposition++The three reviews agree on the durable design principles: preserve originals, run post-capture first, fail open, never use VAD/text as source identity, do not lower the B1 correlation threshold, keep live processing out of the first release, and gate speaker-profile learning more strictly than transcript fallback.++They also agree that revision 1 moved to AEC3 too quickly. Revision 2 therefore approves only the Apple/SCK reference probe, synchronizer harness, and conditional out-of-tree AEC3 feasibility spike. It does **not** approve an AEC3 dependency, raw-microphone capture migration, production suppression, profile enforcement, or app reinstall.diff --git a/tools/MEETING_RELIABILITY_P0_ADVERSARIAL_REVIEW.md b/tools/MEETING_RELIABILITY_P0_ADVERSARIAL_REVIEW.mdnew file mode 100644index 00000000..3b41381e--- /dev/null+++ b/tools/MEETING_RELIABILITY_P0_ADVERSARIAL_REVIEW.md@@ -0,0 +1,53 @@+# Orchestrate P0 adversarial review++Captured before amendments; see MEETING_RELIABILITY_P0_BASELINE.md for disposition. Reviewer output is analysis, not implementation instructions or final sign-off.++served-by: standard (grok · grok-4.6)+attempt: standard · ok · 274.8s · editedTree=false++I'll review only the five named files and produce an adversarial pass on production behavior, decoder-proof claims, model-loading network safety, and report schema accuracy.**Verdict:** RELEASE live policy looks unchanged (diagnostics compile out / force off). These files do **not** prove decoder reset, live-pipeline recovery, or a passed negative screen.++### 1. High — Green XCTest is not a negative-screen pass+`Tests/FluidASRBaselineTests/MeetingReliabilityNegativeScreenTests.swift` (`testSyntheticSilenceAndLowNoiseNegativeScreen`, `NegativeScreenReport.status`, class comment).++The opt-in test never asserts on `nonempty` / `whitespaceWordCount`. Any finished run sets `status = "complete"` and the test succeeds.++**Counterexample:** Silence/noise both emit captions every segment → `runs[].nonempty = true`, `status = "complete"`, XCTest green. That is the hallucination case, not a pass.++**Correction:** Keep measurements non-gating, but add an explicit `verdict: "measured_only"` (never `pass`) and refuse to treat XCTest success as evidence. Gate only on schema/host/artifact checks.++### 2. High — Reset/process KPIs are proof-shaped, not decoder-true+`Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTrackEngine.swift` `resetRecognizer()` (generation / `firstPartialAfterResetSeconds` / `diagnosticResetUptime` **before** `await manager.reset()`, `resetCompletions` **after** return). `MeetingLiveP0DiagnosticsTests.testThrownProcessIsNotCountedAsCompletion` still asserts `resetCompletions == 1`.++**Counterexample:** FluidAudio swallows a failed internal decoder reset (the protocol comment already says this) → `resetInvocations == resetCompletions`, `generation` bumped, later `processCompletions` keep rising (`testForcedBoundaryThen38SecondsWithoutPartialDoesNotRecover`). That is wrapper return, not a new decoder epoch. `firstPartialAfterResetSeconds` also includes reset wait: a 2s hung `reset()` then an immediate partial reports `2`, not `0`.++**Correction:** Rename to `recognizerResetReturned` / `wrapperGeneration`. Start the partial-after-reset clock after `reset()` returns. Never export `resetCompletions` as health.++### 3. Medium — Same model tree, two hash semantics+Python `model_manifest_inventory()` (`tools/meeting_reliability_baseline.py`) vs Swift `validateArtifacts` (`MeetingReliabilityNegativeScreenTests.swift`).++Swift: enumerator, reject symlinks, `actual == Set(paths)`. Python: hash listed artifacts only; extra files do not flip `allHashesMatch`.++**Counterexample:** Manifest-listed files match, plus `Decoder.mlmodelc` sibling `Extra.mlmodelc` → inventory `allHashesMatch: true`; negative screen `ScreenError.invalidInputs`.++**Correction:** Inventory should fail (or set `exactTreeMatch: false`) unless the repository file set equals the manifest, same as the screen.++### 4. Medium — “Local-only” is sandbox + constructors, not a FluidAudio offline proof; live load is still HF+Screen: `MLModel(contentsOf:)` + `AsrManager.initialize` / `transcribe` (`MeetingReliabilityNegativeScreenTests.swift` ~47–90, `networkPolicy`). Host check only rejects `network.client/server` and mic entitlements. Live: `StreamingEouAsrManager.prepareModels()` → `loadModelsFromHuggingFace()` (`MeetingLiveTrackEngine.swift`).++**Counterexample:** Opt-in screen stays sandbox-offline while production live still downloads on cache miss. Comment “Each transcribe resets decoder state” is unverified here; one `AsrManager` is reused for 60s then 3s over the **same** samples — if `transcribe` does not reset, short-turn rows are not independent exposures. `AsrManager` is also not `MeetingLiveTrackEngine` / `StreamingEouAsrManager`.++**Correction:** State in the report that FluidAudio initialize/transcribe internals were not inspected; recreate or explicitly `reset` per segment; never cite this screen for live HF load or live EOU stalls.++### 5. Medium — Session inventory is fail-open on drop schema+`session_inventory()` in `tools/meeting_reliability_baseline.py`: `captureDrops` is `health.droppedSampleCount` via `.get`; missing key → `null`. `sourceTrackID` is fail-closed. `visibleSpeakerCount` is `len({speakerID})`, so a missing id counts as one speaker (`None`).++**Counterexample:** Drops stored as `droppedFrames` (or omitted) → `captureDrops: null` next to real chunk hashes; readers treat it as “unknown/zero drops.” Two visible segments with no `speakerID` → `visibleSpeakerCount: 1`.++**Correction:** Fail if `health.droppedSampleCount` is absent (same as `sourceTrackID`). Count only non-null `speakerID`, or emit `speakerIdPresentCount` separately.++---++**Production preservation (this file):** `#else diagnosticsEnabled = false`; diagnostic APIs are `#if DEBUG`. `consume` / rotate / HF load / reset policy are not altered when diagnostics are off. `uptime` is injectable in RELEASE (default still `systemUptime`) — watchdog clock is no longer a closed production constant.++**Unverified (out of allowed files):** `AsrManager.initialize`/`transcribe` network and reset; `DebugLogger` timestamp vs `FLOW` regex; `BaselinePathSafety` / `ASRBaselineHost` entitlements at runtime.diff --git a/tools/MEETING_SCK_PAIRED_C2_DIAGNOSTICS.md b/tools/MEETING_SCK_PAIRED_C2_DIAGNOSTICS.mdnew file mode 100644index 00000000..8299e48f--- /dev/null+++ b/tools/MEETING_SCK_PAIRED_C2_DIAGNOSTICS.md@@ -0,0 +1,81 @@+# Phase 0 C2 — ScreenCaptureKit paired-output diagnostics++This slice characterizes the existing one-`SCStream` topology that emits selected-application+`.audio` and `.microphone` outputs. It is DEBUG-only and opt-in with:++```text+FLUIDVOICE_C2_DIAGNOSTICS=1+```++In a DEBUG build, this explicit gate also forces the online-call path to use the paired+ScreenCaptureKit topology when the output route is otherwise eligible for voice processing. The+decision detail and debug log identify this as `C2 diagnostic forced paired ScreenCaptureKit`; it+is not a production fallback. Release builds and DEBUG runs without the exact `=1` value retain the+normal voice-processing decision.++The one-shot signed hardware test additionally requires `FLUIDVOICE_C2_HARDWARE=1` and+`FLUIDVOICE_C2_TARGET_BUNDLE_ID=<currently-running-bundle>`. It creates one selected-application+display filter with `.audio` and `.microphone` outputs, runs for five seconds (bounded below the+15-second ceiling), stops cleanly, and prints the sorted numeric report JSON. It skips without all+three explicit values and never launches an application or writes PCM/transcript data.++For an installed DEBUG app, set `FLUIDVOICE_C2_AUTORUN=1` as the fourth exact gate. The app-launch+hook returns before normal topology observers, logging, AppServices, or UI startup, performs the+same no-prompt Screen Recording and microphone preflight, runs the five-second metadata-only stream,+prints one sorted `[C2_AUTORUN]` success/failure JSON line, and exits with status 0/1. All other+launches, including Release builds or any missing/alternate gate value, follow the normal app path.++`MeetingSCKPairedDiagnosticCollector` accepts only callback-boundary metadata: output kind,+presentation timestamp, duration, frame count, sample rate, channel count, and an optional monotonic+arrival timestamp. It retains aggregate numbers in a lock-protected bounded collector. It never+accepts or stores PCM, transcript text, device names, window titles, paths, URLs, or identities.++The report measures per-output sample counts, malformed/non-finite timestamps, format changes,+timestamp gaps/overlaps/backwards movement, delivery jitter, coverage, first/last PTS offset, and+the relative observed timestamp-span difference. The cross-output offset is explicitly+`microphone PTS - application PTS`; it is not an acoustic or processing latency. Independent+ScreenCaptureKit PTS origins mean the report does not establish a shared clock, estimate oscillator+drift, evaluate AEC, or authorize suppression. Timestamp continuity uses a sample-rate-derived+half-sample tolerance, so sub-sample floating-point noise is not reported as a gap or overlap;+missing or malformed data is fail-open and remains numeric.++The DEBUG ScreenCaptureKit runtime now calls the collector at the callback boundary for the selected+application `.audio` and paired `.microphone` outputs. The callback path first checks the explicit+environment gate, copies only numeric metadata, and requires a scope/provenance assertion derived+from the active selected-application filter. At deterministic runtime stop, the bounded report is+serialized with sorted JSON keys and emitted through the DEBUG logger. A provenance/scope mismatch+is rejected, counted in the report, and logged once; it is never silently combined with another+capture scope. The unit tests retain a deterministic harness for synthetic metadata and exercise+the callback seam without hardware.++No production capture, transcription, persistence, profile learning, or installed app behavior is+changed by C2.++## 2026-09-10 built-in-route attempt++The signed installed-app autorun reached both no-prompt privacy preflights, but+`SCShareableContent.current` returned no display. It therefore exited before creating or starting+an `SCStream`; no browser stimulus played and no PCM or transcript was retained. The numeric result+is frozen in `meeting_sck_paired_c2_builtin_2026-09-10.json` as invalid evidence. It neither supports+nor rejects the paired topology. A later rerun requires resolving the macOS ScreenCaptureKit display+availability/permission state; the diagnostic must not request or bypass that permission itself.++## 2026-09-10 unlocked rerun++After the interactive display session was unlocked, the same signed installed-app autorun completed+one bounded five-second capture against the controlled Chrome stimulus. Both selected-application+audio and microphone outputs produced samples; every callback had valid timestamps and stable+formats, with zero collector-bound drops, malformed metadata, backwards timestamps, or provenance+rejections. The application and microphone spans each reported full coverage. The exact reduced+report is frozen in `meeting_sck_paired_c2_builtin_unlocked_2026-09-10.json`; it retains no PCM or+transcript. That sidecar predates the continuity-tolerance and report-field correction (schema 3),+so its nanosecond-scale gap/overlap counts and `relativeSpanDriftPPM` key are historical artifacts,+not current measurements.++This establishes that the selected-application paired topology is viable on this host when a display+session is available. It does not establish a common capture clock or evaluate AEC: ScreenCaptureKit+reported independent PTS origins, a first-PTS offset of approximately 154 ms, a last-PTS offset of+approximately 42 ms, and a pre-correction observed-span difference of approximately -21,538 ppm over+this short window. The unequal start/stop delivery boundaries account for that span difference, so it+must not be interpreted as oscillator drift. These values are diagnostic timing evidence, not acoustic+delay or cancellation scores.diff --git a/tools/MEETING_SPEECH_SHADOW_B2.md b/tools/MEETING_SPEECH_SHADOW_B2.mdnew file mode 100644index 00000000..a745b882--- /dev/null+++ b/tools/MEETING_SPEECH_SHADOW_B2.md@@ -0,0 +1,97 @@+# B2 offline speech activity and playback comparison++Date: 2026-09-08 (local). Experimental evidence collection, not production admission.++## Result++The cached speech-activity model runs locally and produces useful diagnostic variation, but activity still occurs in user-reported playback-only recordings. The B1 matcher still produces no supported locks on these fixtures. **This combination does not establish a safe local-speech classifier.** Every combined proposal remains explicitly uncertain; nothing hides text or filters speaker profiles.++No installed app, original recording, saved transcript or speaker profile was changed. No model was downloaded and no audio/text/embedding was sent to a reviewer. The default CLI path has speech collection off; an explicit fourth argument selects a local compiled model for this standalone run. No app runtime enablement was introduced.++## Model/API audit++The pinned FluidAudio source (`3fd63887eef1dc25edea8263ce4b44aa854d898b`) uses Silero v6's unified 256 ms model: 4,096 new mono 16 kHz samples plus 64 context samples; hidden/cell states each have 128 values. `VadManager`'s normal and directory initializers call download helpers, while its preloaded-MLModel initializer avoids them. Its whole-array helper pads short final chunks by repeating the final sample.++This evaluator instead uses a small direct CoreML adapter with the audited exact feature names/shapes and CPU-only execution. It cannot invoke FluidAudio download helpers. It submits only complete valid frames, with no fabricated tail padding. It keeps the 64-sample context and recurrent state across contiguous valid frames, and resets after invalid coverage, gaps, epoch/route/session changes or model failure. The first valid frame after reset is marked warmup/unknown; this one-frame warmup is an explicit experimental convention, not a validated convergence guarantee.++The cached `model.mil` combines eight internal 32 ms probabilities as `1 - product(1 - p_i)`. Consequently the exported scalar is **not** mean speech probability, voiced duration, near-end confidence or eight independent pieces of evidence. The dependency's 0.85 default is retained only as a diagnostic activity threshold. No gain normalization, energy rescue, segmentation padding or speech suppression was added.++The local FluidAudio repository declares Apache-2.0. The model publisher declares MIT in its [model card](https://huggingface.co/FluidInference/silero-vad-coreml/blob/main/README.md); no new model redistribution/package is performed here. Cached artifact identity is separately fingerprinted rather than assuming the current remote card establishes byte identity.++Cached model: `silero-vad-unified-256ms-v6.0.0.mlmodelc`. Sidecars record hashes of all five files; hashes are checked again after inference. Key SHA-256 values:++```text+model.mil 4f93e2b5920e851fbc0be1c21a2a76e170124467ed7b01125190aa32e795f8af+weights/weight.bin 853cf34740d3f5061f977ebe2976f7c921b064261c9c4753b3a1196f2dba42b4+```++## Evidence, coverage and safety++- The existing B1 paired-audio analysis remains a separate signal. Speech frames join its windows by full interval overlap in the same capture epoch; 256 ms frames crossing the two-second grid are not dropped. Mixed support remains explicit. Missing temporal coverage is not a reliable negative match.+- New combined reason categories distinguish activity with a supported duplicate, activity without a reliable match, negative activity with/without duplicate evidence, and unavailable speech evidence. None establishes near-end speech or absence. The legacy echo score is explicitly `notMeasured` in this CLI; this is not the full three-signal/B3 ablation.+- Original capture PTS, overlapping-chunk rejection, 50 ms conservative decoded edges and ±2 s either-track recorded discontinuity guards are retained. Invalid samples remain unavailable, not silence. Unrecorded route changes and accurate AAC priming remain limitations from B1.+- One model instance and synchronous frame inference; cancellation checked before/after prediction. A CoreML prediction cannot be forcibly preempted. A 100 ms per-frame engineering budget discards over-budget results; three consecutive failures disable the contribution within the session. Unknown frames clear pre-disable strikes; a new session resets the latch. Model load is synchronous and is not covered by the per-frame inference budget.+- The offline runner is capped at 600 seconds/32 chunks and keeps a bounded session raster in memory; it is not a streaming capture implementation. A missing model emits explicit unavailable status and full unmeasured duration, with no network fallback.+- Detailed reports are numeric-only, exclusively created outside original sessions under temporary storage. They are not application logs or persisted admission decisions. Retain only for development review; no automatic deletion/retention automation was created.++## Local measurements++Final reports: `/private/tmp/fv-speech-{3774,88e3,3b9a}-v2.json`. V1 reports predate the improved temporal-grid join and are superseded. All counts below are **256 ms windows**, not speech duration.++| Development recording | Full frames | Measured | Activity ≥0.85 | Unknown full frames | Unknown duration including tail |+| --- | ---: | ---: | ---: | ---: | ---: |+| `3774ED4C…` user-reported no speech | 385 | 380 | 6 | 5 | 1.476 s |+| `88E3BD44…` scripted speech near end | 1,016 | 1,004 | 141 | 12 | 3.299 s |+| `3B9AC1A8…` user-reported playback-only | 515 | 508 | 6 | 7 | 1.822 s |++In the previously identified coarse 208–250 s scripted interval, 121 of 161 measured windows were active (163 total). There were also 19 active windows wholly before 208 s. These boundaries come from prior diagnosis, not independent sample-level annotations; pauses and exact speech onset are unlabelled. The ratio is **not speech recall**, and earlier windows cannot all be labelled individually from this coarse boundary.++The playback-only recordings demonstrate that activity can be leaked speech rather than near-end speech. Conversely, a negative activity window cannot establish absence of a quiet reply. Synthetic tests explicitly retain this distinction. All 1,916 combined frame proposals stay uncertain: 153 activity-without-reliable-match, 1,739 negative-activity-not-absence, and 24 missing-speech-evidence. There are zero supported temporal locks.++Final end-to-end local CPU-only runs took 1.10, 1.48 and 0.77 seconds, with maximum resident sizes about 62.3, 80.9 and 64.7 MB. No model, budget or inference failures occurred in these runs. These are development profiles, not capture-load or held-out acceptance results.++`/private/tmp/fv-speech-3774-off.json` confirms off mode has the same input fingerprint and temporal state counts. `/private/tmp/fv-speech-3774-unavailable.json` confirms a missing model reports all 98.756 seconds unavailable without a fallback download. This does not replace a full baseline/echo/VAD/combined end-to-end admission ablation.++## Adversarial review and dispositions++Three scoped Kimi K3 reviews completed through OpenCode in read-only mode: B1 core, B2 adapter/evaluator, and Stage A evidence seam. Earlier timeouts are historical, not the current review result. Findings were addressed as follows:++1. B1 floating-point lag-spread boundary: fixed using integer-sample comparison; exact 12 ms spread regression added.+2. B1 failure latch/counter scope: new sessions reset the latch; unavailable windows clear pre-disable consecutive strikes. Within-session disabled state intentionally remains latched.+3. B1 release counters: no code change. `released` records the transition; zero supporting windows accurately represents current evidence. Retaining former support in those fields would be misleading.+4. B2 failure reset issues: fixed consistently with B1 and covered by session/unknown-frame tests.+5. B2 256 ms / two-second grid mismatch: replaced single-containing-window lookup with overlap/coverage join and explicit mixed/unavailable state, with boundary tests. The reviewer incorrectly attributed the old nil temporal match to the missing-*speech* reason; that reason depends on missing VAD activity. The temporal coverage problem itself was real.+6. Short-session shifted controls: explicitly report `controlUnavailable` when neither ±8 s control fits. No fabricated fallback. Some later short-session windows can use the backward control, contrary to the review's broader claim; missing control remains an acknowledged measurement limit.+7. Stage A unknown playback context: added `missingPlaybackContext` instead of labelling a valid temporal measurement invalid, with a regression test.+8. Stage A exact interval/observation equality: retained intentionally. This test-only contract binds to an immutable evidence snapshot, not an approximate audio match; callers must reuse/rebind that snapshot explicitly. Two cases pin rejection of a one-ULP interval change and observation change. A future runtime projection must define its own timeline conversion rather than silently relaxing binding. `staleTemporalEvidence` currently includes snapshot mismatch, not just expired epochs.++The scopes above were reviewed; post-fix implementation was locally verified, not independently re-reviewed in full. No claim of production approval, calibration approval or enforcement sign-off is made.++## Verification++Final isolated `FluidASRBaseline` build succeeded and **46 targeted tests passed**, zero failures/skips: 11 speech tests, 13 temporal tests, 17 Stage A contract tests and five existing live diagnostic tests. Result bundle: `/private/tmp/fv-speech-b2-v3.xcresult`. The earlier v1 test attempt failed before tests could launch because the generated CTranscribe framework had an invalid version layout/signature; displaced framework files were retained in temporary backups, the isolated host was repaired/re-signed and deep/strict verified, then v2 (44 tests) and final v3 passed. No installed-app replacement occurred.++The optimized CLI compiled and ran all three local fixtures, speech-off mode and missing-model mode. Source inspection confirms no production caller of the speech adapter or either shadow policy. `git diff --check` passed. The full application integration suite, Release build, hour-long capture load, independently annotated positives, calibrated recall and held-out gates were not run.++## Reproduce / next gate++From the repository root:++```sh+swiftc -module-cache-path /private/tmp/fv-reliability-swift-cache -O \+ Sources/Fluid/Services/Meeting/MeetingPlaybackDuplicateDetector.swift \+ Sources/Fluid/Services/Meeting/MeetingSpeechActivityDetector.swift \+ tools/meeting_temporal_shadow.swift -o /private/tmp/fv-speech-shadow+```++Run with `session.json`, a fresh output path outside the session directory, and the explicitly chosen cached `.mlmodelc` directory. Omit the model argument for speech-off mode. Native codec/CoreML access may require host execution approval; this does not upload audio or grant new application privacy permissions.++Next is annotated development evaluation and feature/calibration work, not enabling suppression. Stronger temporal evidence may be needed for weak nonlinear residuals. Quiet replies, double talk, no-reference speech, multiple near-end speakers and held-out quality remain unvalidated. Any new detector/model dependency needs a separately reviewed scope. B3, app shadow integration and enforcement remain outstanding.++Source SHA-256 for the final fixture runs:++```text+speech adapter 751dea98435493b2c2c3792a6965ed2e3d7eb301634bcd92a71728ec4cba12bb+temporal 429111cce7a90ffec42e84b7f9e7bd9fece6b0c21c8790616de52fc180389d95+evaluator d254f1e0c309f485d50e93ab146c245ea3457245881a4ff2a1f772ded7465e07+```diff --git a/tools/MEETING_TEMPORAL_SHADOW_B1.md b/tools/MEETING_TEMPORAL_SHADOW_B1.mdnew file mode 100644index 00000000..bcb09b3d--- /dev/null+++ b/tools/MEETING_TEMPORAL_SHADOW_B1.md@@ -0,0 +1,70 @@+# Temporal shadow detector B1 — development evaluation++Date: 2026-09-08 (local). Offline experimental implementation, not a caption fix or release gate pass.++Follow-up: B2 completed the previously pending scoped reviews, fixed temporal lag/budget issues, and added local activity comparison. See `MEETING_SPEECH_SHADOW_B2.md` for current review dispositions and results; historical B1 hashes/measurements below identify the earlier run.++## Outcome++The first normalized-waveform operating point produced **zero duplicate-supported windows** on all three development recordings. This is a negative feasibility result for this particular feature/operating point, not proof that the microphone contains no playback or that speech is safe to accept. Thresholds were not lowered to fit these recordings.++The installed app, legacy admission behavior, saved transcripts, speaker profiles and original audio were not modified. The detector has no production caller; the only executable caller outside tests is the explicitly invoked local evaluator. No new runtime flag, model, network call or persistence schema was added.++## Implementation and limits++- `MeetingPlaybackDuplicateDetector.swift` consumes explicitly resampled mono 2 kHz windows with original PTS and validity masks. Positive lag means microphone follows playback. It searches ±0.5 seconds in 1 ms increments, measures mean-centered absolute normalized correlation and compares its peak with an eight-second-shifted reference control and separated competing peaks.+- Support requires three consecutive two-second windows whose lag spread is at most 12 ms. History is capped at three windows by default (configuration capped at 16). Epoch/session/route changes, gaps, unavailable coverage and failed support clear accumulated evidence. No stale supported state survives reference loss.+- There is no release hangover in this first experiment. Updates still have two-second resolution; zero hold does **not** mean zero detection/release latency. Synthetic perfect-copy lock requires six seconds of observations. Reports do not backdate support into earlier windows or claim sub-window onset precision.+- The 0.25 correlation, 0.08 control margin, 0.025 competing-peak margin, 1e-5 RMS floor and timing settings are experimental engineering constants. Configuration is captured in sidecars. They are neither probabilities nor calibrated speech-admission thresholds.+- The caller uses AVAudioConverter anti-aliased resampling, clips decoded samples to recorded chunk end, linearly maps fractional PTS to a common grid and invalidates overlapping chunk coverage. It masks either-track recorded discontinuities by ±2 seconds, matching the existing pipeline guard. It additionally masks 50 ms at decoded/chunk edges conservatively; this is not an independently verified AAC priming model. Missing-PTS discontinuities fail the offline run rather than invent an epoch.+- Only recorded discontinuities can be reconstructed. Unrecorded route/sample-clock changes remain a limitation; lag instability can reset support but is not a verified clock-drift correction. No live reuse or source-rate-change guarantees are claimed.+- Low-energy mic/reference/control windows remain insufficient evidence. A missing control is not a zero-valued negative control. Periodic signals and equal control matches cannot lock in the synthetic tests.+- A synthetic quiet local signal added while playback is already supported **still leaves a supported match**. This explicitly demonstrates why temporal evidence alone must never hide speech. Nonlinear residual separation, near-end activity and interval admission are not implemented here.+- The evaluator is capped at 600 seconds, 32 chunks, two tracks, 65 seconds per decoded chunk, bounded formats/file sizes and one synchronous job. Per-window scoring has an injectable 0.5-second budget, checked each lag; three repeated budget failures disable the contribution for that detector instance. These local measurements do not constitute an hour-long capture-load test or a hard real-time deadline guarantee.++## Local numeric results++Final sidecars are `/private/tmp/fv-temporal-{3774,88e3,3b9a}-v2.json`; earlier v1 sidecars precede the explicit silent-reference/control check and are superseded. Input session and audio hashes are in each sidecar; no text, embeddings or raw audio is emitted. Durations below are recorded audio PTS spans, not UI session durations.++| Development session | Audio span | Windows | Scored/inconclusive | Invalid coverage | Low energy | Supported | Median diagnostic lag | Median peak correlation |+| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |+| `3774ED4C…` | 98.756 s | 49 | 44 | 5 | 0 | 0 | +70 ms | 0.104 |+| `88E3BD44…` | 260.323 s | 130 | 98 | 18 | 14 | 0 | +86 ms | 0.129 |+| `3B9AC1A8…` | 131.870 s | 65 | 54 | 11 | 0 | 0 | +62 ms | 0.075 |++Unwindowed tails are respectively 0.756, 0.323 and 1.870 seconds and remain unmeasured. Invalid/low-energy windows contribute another 10, 64 and 22 seconds of unknown coverage. Even the scored windows are inconclusive, not negative speech evidence. Diagnostic best-lag ranges are respectively −500…+350, −491…+479 and −491…+469 ms; their medians are **not delay locks**. Peak correlations never reached 0.25 (maxima 0.190, 0.2495 and 0.175).++Maximum measured optimized scoring time per window was 4.72 ms. End-to-end local runs took 0.67, 0.58 and 0.31 seconds, with maximum resident sizes about 40.5, 42.7 and 41.1 MB. These are single-machine development profiles, not frozen acceptance budgets.++No real lock-onset, hold/release error, missed-speech or false-word rate can be estimated from these runs: there were no locks and no independent interval annotations. The scripted portion remains development material. Detector-disabled comparison is structurally unchanged because no production code consumes the reports; an end-to-end policy ablation belongs to B3.++## Reproduce safely++Compile from the repository root:++```sh+swiftc -module-cache-path /private/tmp/fv-reliability-swift-cache -O \+ Sources/Fluid/Services/Meeting/MeetingPlaybackDuplicateDetector.swift \+ Sources/Fluid/Services/Meeting/MeetingSpeechActivityDetector.swift \+ tools/meeting_temporal_shadow.swift -o /private/tmp/fv-temporal-shadow+```++Invoke with an explicitly selected `session.json` and a new output path outside that session directory. Output creation is exclusive. Local macOS codec access may require the normal host execution permission; no network permission or audio upload is involved. Sidecars are temporary development artifacts, not new session data; keep only for the review and remove explicitly when no longer needed (there is no automatic cleanup job).++## Next gate++Do not connect this operating point to suppression or speaker-profile eligibility. B2 needs independently validated speech activity and combined/mixed-interval evidence; B3 needs annotated controls and calibration/ablations. A more robust temporal feature may be needed for nonlinear low-correlation residuals. This document does not broaden implementation scope to a new model or dependency.++## Verification and review status++Final isolated `FluidASRBaseline` build succeeded. All 31 targeted tests passed with zero failures/skips: 11 new detector tests, 15 Stage A contract tests and five existing live diagnostic tests. Result: `/private/tmp/fv-temporal-b1-v3.xcresult`. Tests cover lag sign/shift, repeated support, periodic/wrong/control references, quiet-double-talk confounding, epoch/route/gap resets, masked/missing reference, no-hold release, delay jumps, silent reference, invalid inputs and injected budget exhaustion. Broader app/Release and held-out evaluations were not run.++Kimi K3's code-review call and continuation timed out (180/300 seconds); the scoped Grok 4.6 fallback also timed out (180 seconds). No findings were returned and **external code sign-off is still pending**. Only authorized FluidVoice source was sent; no audio, transcript, embedding, secret or competitor code was sent. The local checks above are not a substitute for the outstanding review.++Source SHA-256 at evaluation:++```text+detector f580cb5857d7b1917e1569f15b10efafc2637740fbe3f72b4f922d0046dcc166+evaluator c607e67e408eb89532ea69f072dd5eec7c64591b2d595795847a8b0502020f48+tests d8ca22c8a759924dc7be6430a424c900ad5a0f4cf9e7540370c5d30a40710520+```diff --git a/tools/MEETING_TRIAL_A_EXTERNAL_REFERENCE.md b/tools/MEETING_TRIAL_A_EXTERNAL_REFERENCE.mdnew file mode 100644index 00000000..c916870d--- /dev/null+++ b/tools/MEETING_TRIAL_A_EXTERNAL_REFERENCE.md@@ -0,0 +1,77 @@+# Phase 0 Trial A — external Chrome reference++Trial A is a DEBUG-only, one-shot diagnostic. It captures selected `com.google.Chrome` audio from+one ScreenCaptureKit stream as a reference while the existing VPIO microphone capture runs in the+same short-lived signed process. It does not play audio, start ASR, retain transcripts, write+sessions, or change production capture behavior.++All three exact gates are required:++```text+FLUIDVOICE_TRIAL_A=1+FLUIDVOICE_TRIAL_A_AUTORUN=1+FLUIDVOICE_TRIAL_A_TARGET_BUNDLE_ID=com.google.Chrome+```++The launch hook performs Screen Recording and microphone permission checks, confirms the live+default input is built-in and the live default output is built-in speakers, reads output volume,+and applies the controlled fixture's 0.08 peak bound. It captures for at most five seconds, has a+12-second operation timeout and a 14-second process watchdog, stops the stream and VPIO on every+path, emits one sorted numeric `[TRIAL_A_AUTORUN]` line, and exits.++The report retains PCM only in memory during the bounded run and releases it immediately after RMS,+peak, timestamp, coverage, gap, overlap, format, and synthesized-timing reduction. It records the+selected Chrome PID and scope. ScreenCaptureKit PTS and the VPIO host-derived PTS are not treated as+a shared clock: acoustic delay, AEC, and attribution remain `unknown` unless a separately validated+clock bridge is added. No C2 or Trial B gate is consulted or triggered.++The deterministic offline harness tests exact gating, numeric-only output, and fail-closed timing+for gaps, overlaps, synthesized timing, malformed geometry, and unknown clock mapping.++## Stimulus readiness and launch order++The browser fixture is now a required readiness handshake. Its window title is `... — READY` until+the Start button is clicked and the `AudioContext` is successfully resumed; it changes to+`... — PLAYING` only while the 12-second fixture is active, then to `... — COMPLETE`. Trial A+requires a selected Chrome window whose title contains the PLAYING marker before it starts VPIO or+ScreenCaptureKit. It also resolves the display from that exact window by maximum positive frame+intersection and fails closed for no-intersection or equal-area multi-display ties. This makes a+not-yet-clicked or already-finished tab, or a window whose audio would be associated with the wrong+display, fail immediately instead of consuming the one-shot capture window. The PLAYING window's+owning `SCRunningApplication` is authoritative; duplicate/stale same-bundle application records are+not consulted for readiness or filtering.++Use this order for any separately authorized future run: open the localhost fixture, click Start,+confirm the browser title says PLAYING, then launch the signed DEBUG app promptly. Launching the app+first and clicking later is intentionally rejected.++## Unlocked built-in-route run — 2026-09-10++One safety-audited signed run completed on the built-in microphone and speakers at system volume+0.25. Route, volume, VPIO readback, selection, process stability, callback timing, and full interval+coverage all passed. The microphone delivered 53 valid callbacks with RMS 0.001411 and peak+0.007458. ScreenCaptureKit delivered 264 structurally valid reference callbacks, but every sample+was silent (RMS and peak 0). Therefore reference excitation was not confirmed and `captureValid`+and `acousticMeasurementValid` are both false. This result establishes microphone-side capture+health only; it provides no AEC, attenuation, delay, or topology-ranking evidence. Do not rerun+Trial A without a fresh safety review. The exact report is preserved in+`meeting_external_reference_trial_a_builtin_unlocked_2026-09-10.json`.++The zero-valued reference in that run is most consistent with fixture readiness/order, not a+ScreenCaptureKit timing or filtering failure: the selected Chrome stream delivered 264 callbacks+with complete, gap-free timing and no stream errors, but every sample was numerically zero. The+filter explicitly enables application audio, selects Chrome, and excludes only FluidVoice's own+process audio. Because the page click occurred after app launch and the old harness had no+cross-process PLAYING handshake, the run did not establish that Chrome was actively rendering the+fixture during the five-second window. A future zero reference would still be an invalid result,+but the readiness gate now prevents spending a run when that precondition is visibly absent.++Run only from the signed DEBUG app/process boundary after Chrome is already showing the controlled+0.08 localhost stimulus:++```sh+FLUIDVOICE_TRIAL_A=1 \+FLUIDVOICE_TRIAL_A_AUTORUN=1 \+FLUIDVOICE_TRIAL_A_TARGET_BUNDLE_ID=com.google.Chrome \+"/Applications/FluidVoice Debug.app/Contents/MacOS/FluidVoice Debug"+```diff --git a/tools/MEETING_VPIO_REFERENCE_PROBE.md b/tools/MEETING_VPIO_REFERENCE_PROBE.mdnew file mode 100644index 00000000..a151f01c--- /dev/null+++ b/tools/MEETING_VPIO_REFERENCE_PROBE.md@@ -0,0 +1,195 @@+# Meeting VPIO reference probe++Status: read-back slice implemented and exercised on 2026-09-10. This is configuration evidence,+not an acoustic AEC result.++## Scope and routing boundary++The normal meeting architecture is unchanged. Selected-application audio remains captured by+ScreenCaptureKit and is not rendered through VPIO. The probe only inspects the already-running+microphone VPIO. A later, explicitly diagnostic Trial B may render known local PCM through the+same engine; that will not reroute arbitrary application audio and is not part of this slice.++No production capture metadata, transcription, suppression, scoring, or speaker-profile policy is+changed by this probe. The installed app was not replaced.++## Read-back schema++`MeetingVoiceProcessingProbeSnapshot` records:++- input and output `kAudioOutputUnitProperty_CurrentDevice` values;+- each Audio Unit property ID, scope, element, `OSStatus`, and value for VPIO bypass, AGC, output+ mute, and other-audio ducking;+- the corresponding `AVAudioInputNode` voice-processing getters;+- input/output node formats, presentation latencies, last-render host/sample times, engine running+ state, and the input node's explicit output-connection count.++An unsuccessful Audio Unit read serializes its nonzero status and omits its value, preventing a+zero-initialized buffer from masquerading as a successful read. The JSON schema is versioned and+covered by a round-trip test.++## First built-in-route observation++Environment:++- MacBook Pro `Mac17,9`, macOS 26.6.2 (25G83), arm64;+- signed Debug test host using the existing stable Apple Development identity;+- built-in microphone to built-in speakers;+- 3-second capture, no deliberate playback stimulus.++Numeric sidecar:+[`meeting_vpio_reference_probe_builtin_2026-09-10.json`](meeting_vpio_reference_probe_builtin_2026-09-10.json)++Observed configuration:++- CurrentDevice input element 1 read back device 82; output element 0 read back device 87. Both+ reads returned `OSStatus == 0`. The requested and settled UIDs independently identified those as+ the built-in microphone and built-in speaker.+- Voice processing was enabled and not bypassed. AGC was enabled, input/output mute was off, and+ advanced ducking was disabled at the minimum level (raw 10). The AVFoundation getters and raw+ Audio Unit reads agreed.+- The VPIO input-side node format was 48 kHz, 9 channels; the capture tap delivered canonical+ 48 kHz mono. The output-side node format was 48 kHz stereo.+- Both I/O nodes had valid last-render host and sample times. The input node had zero explicit+ output connections, consistent with the current graph having no app-owned render source.+- Capture health for the short run was clean: 30 emitted buffers, 100% valid host time, zero+ synthesized timestamps, conversion failures, continuity breaks, writer discontinuities,+ backpressure events, or live-copy failures.++The system VoiceProcessor also logged repeated downlink errors saying its audio timestamp lacked a+valid sample time, followed by downlink DSP I/O faults, before the final snapshot showed valid node+render times. This is actionable configuration evidence, but it does not prove whether other-process+speaker audio is or is not available to Apple's echo reference. The acoustic A/B trials remain+required.++## Isolated harness and teardown finding++The `FluidVPIOProbe` scheme sets `FLUIDVOICE_MIC_PHASE1=0.05`. In that environment only, Debug+startup avoids normal FluidVoice UI/audio-service initialization so the app-hosted XCTest does not+create competing audio activity.++Reading the live output node exposed a macOS 26.6 AVFAudio teardown crash when the diagnostic then+called `setVoiceProcessingEnabled(false)` after stopping the engine. The crash was in+`AVAudioIONodeImpl::AUI()` during that explicit replacement. The probe now skips only that explicit+replacement and lets process exit release the diagnostic VPIO. Ordinary product `stop()` keeps the+existing explicit teardown behavior unchanged.++## Reproduce++Run the hardware-free schema tests:++```sh+xcodebuild -project Fluid.xcodeproj -scheme Fluid -configuration Debug \+ -destination 'platform=macOS' -derivedDataPath /private/tmp/fv-vpio-probe test \+ -only-testing:FluidDictationIntegrationTests/MeetingVoiceProcessingProbeTests+```++Run the 3-second signed built-in-route probe:++```sh+xcodebuild -project Fluid.xcodeproj -scheme FluidVPIOProbe -configuration Debug \+ -destination 'platform=macOS' -derivedDataPath /private/tmp/fv-vpio-probe test \+ -only-testing:FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests/testPhase1HardwareGate+```++Use the repository's stable signing settings for the hardware command. Changing the value of+`FLUIDVOICE_MIC_PHASE1` in the diagnostic scheme changes the duration in minutes. Do not install the+test product over `/Applications/FluidVoice Debug.app`.++## Remaining Phase 0 work++This result establishes that VPIO is enabled, which devices its two elements use, and that an output+side is rendering despite no explicit app source. It does not establish echo attenuation or a usable+reference path. Next work is:++1. run the controlled external-stimulus Trial A and compare it with the engine-local Trial B below;+2. persist the printed numeric timing, delay, band-coherence, RMS, attenuation, and uncertainty sidecar;+3. compare bypass/AGC variants without changing production defaults;+4. only then compare the paired ScreenCaptureKit topology and decide whether an Apple-only graph fix+ is sufficient.++## Controlled engine-local Trial B++The DEBUG-only `MeetingVPIOAcousticTrialB` renders a deterministic log-sweep plus speech-like+stimulus through an `AVAudioPlayerNode` attached to the existing VPIO engine mixer. A mixer tap is+the engine-local render reference; the microphone tap is the VPIO capture. The probe runs three+short, exclusive variants: default processing, AGC disabled, and voice processing bypassed (a+variant is marked invalid if a public setter/read-back cannot be applied). The signed delay+convention is `capture time - render time`: positive means the VPIO capture lags the local render.++Run it only from the isolated signed Debug scheme. Both explicit environment gates are mandatory:+`FLUIDVOICE_VPIO_ACOUSTIC=1` and a finite `FLUIDVOICE_MIC_PHASE1` duration strictly above zero and+at most 0.25 minutes (15 seconds); the process must be built with `DEBUG`:++```sh+xcodebuild -project Fluid.xcodeproj -scheme FluidVPIOProbe -configuration Debug \+ -destination 'platform=macOS' -derivedDataPath /private/tmp/fv-vpio-probe test \+ -only-testing:FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests/testPhase1HardwareGate+```++The test prints one JSON result containing only the stimulus descriptor and, per variant, property+read-backs plus render/capture valid counts, overlap, signed delay, correlation, render/capture RMS,+attenuation in dB, five frequency-band coherence/support values, output-render confirmation, output+connection count, and explicit uncertainty reasons. PCM arrays are not Codable and are released after+reduction; the Trial B collector does not write audio, invoke ASR, or retain a transcript. The current+process does not automatically persist this output, so redirect only the numeric test output to a+local sidecar after inspecting it.++Limits and caveats:++- The stimulus is generated from fixed seed and segment durations, has a peak limit of 0.35, RMS+ bounds of 0.02–0.15, and the player volume is limited to 0.6. These are safe diagnostic bounds,+ not a calibrated speaker level.+- Correlation searches at most ±0.5 seconds and reports split-half delay uncertainty; low excitation,+ mismatched controls, incomplete capture coverage, and unsupported properties carry explicit reason+ codes. Frequency coherence/support is evidence about coupling, not an echo-cancellation score.+- A successful mixer tap/read-back confirms an engine-local render, not that an arbitrary other+ process or selected-app ScreenCaptureKit stream reaches VPIO. Trial B is informational and cannot+ productize ownership of another app's render path.+- The acoustic probe requires DEBUG plus both explicit environment gates: `FLUIDVOICE_VPIO_ACOUSTIC=1`+ and finite `FLUIDVOICE_MIC_PHASE1` with `0 < minutes <= 0.25`. Before rendering, the read-only+ output preflight requires a live output route whose actual device matches the current default,+ a readable nonzero system volume no higher than 0.6, and a combined digital/output peak no higher+ than 0.15; an unsafe or unreadable route refuses the render.+ It intentionally avoids `setVoiceProcessingEnabled(false)` during teardown because macOS 26.6+ can crash in `AVAudioIONode` after a live output read-back; ordinary product teardown is unchanged.+- This slice makes no AEC claim and does not alter normal capture, transcription, profile, export,+ or installed-app behavior. A positive local correlation is evidence about this controlled graph+ only; it is not proof that Apple's VPIO reference contains system-wide output.++### Built-in-device run on 2026-09-10++The first signed, one-shot Trial B run completed without installing an app. The built-in microphone+and built-in output route were confirmed, system volume was 0.25, the bounded combined peak was+0.03125, and the mixer tap confirmed all 139,200 scheduled frames with valid host timestamps. All+three VPIO control variants also matched their raw and high-level read-backs.++Every acoustic result is nevertheless **invalid/unknown**. The capture collector covered only+93.29–94.74% of each requested shared-clock window, with no overlap and no synthesized timing, so+the strict completeness gate correctly withheld delay, correlation, band, and attenuation values.+Apple's VPIO also logged repeated downlink DSP I/O/state faults. This run is evidence of a probe+window-alignment defect and an unstable VPIO graph, not evidence for or against echo cancellation.+The exact reduced report is in `meeting_vpio_acoustic_trial_b_builtin_2026-09-10.json`; it contains+no PCM or transcript.++An audited rerun after adding collector warm-up remained invalid/unknown, with 92.76–93.18%+coverage. That ruled out insufficient pre-render accumulation and exposed a separate 200 ms origin+error: the capture window used the first non-silent mixer sample as stimulus sample zero even though+the generated stimulus begins with 9,600 silent frames. The rerun is preserved in+`meeting_vpio_acoustic_trial_b_builtin_rerun1_2026-09-10.json`. The window-origin correction is a+code change only until it receives a fresh safety review; no additional acoustic result is implied.++After that correction passed a fresh safety review, one final signed rerun improved the two active+VPIO windows to 98.45% and 97.82% coverage, but both still failed the strict gap gate while Apple+logged repeated downlink DSP I/O/state faults. The bypassed VPIO variant reached exactly 100%+coverage with no overlap or synthesized timing, yet its acoustic measurement was also invalid: the+two delay halves disagreed (approximately 0.00002 s versus 0.05285 s), only one frequency band was+coherent, and captured RMS was only 1.20 dB above the measured noise floor. Consequently none of+the three variants provides a valid AEC result. The exact report is+`meeting_vpio_acoustic_trial_b_builtin_rerun2_2026-09-10.json`.++This is enough to stop repeating Trial B on this host: active VPIO is associated with incomplete+capture windows and framework DSP faults, while the complete bypass arm is not sufficiently+delay-locked or broadband-coherent to support a cancellation claim. The remaining Phase 0 evidence+must come from the external-process Trial A and paired ScreenCaptureKit Trial C2 rather than further+engine-local playback.diff --git a/tools/meeting_admission_b3_annotations.json b/tools/meeting_admission_b3_annotations.jsonnew file mode 100644index 00000000..fb39475d--- /dev/null+++ b/tools/meeting_admission_b3_annotations.json@@ -0,0 +1,66 @@+{+ "schemaVersion": 1,+ "annotationSet": "development-only-v1",+ "warning": "User-reported recording-level/coarse interval truth; not sample-level annotation and never held out.",+ "sessions": [+ {+ "sessionID": "3774ED4C-7267-4838-A9A2-71DACE246CF9",+ "sidecar": "fv-speech-3774-v2.json",+ "intervals": [+ {"start": 0.0, "end": 98.75583824932983, "label": "noNearEndSpeech", "provenance": "user reported no speech for whole recording"}+ ]+ },+ {+ "sessionID": "88E3BD44-6935-44AB-B139-1DED1002E8E0",+ "sidecar": "fv-speech-88e3-v2.json",+ "intervals": [+ {"start": 0.0, "end": 208.0, "label": "noNearEndSpeech", "provenance": "user reported speaking only supplied script near end"},+ {"start": 208.0, "end": 250.0, "label": "coarseNearEndSpeechRegion", "provenance": "script occurs somewhere in this coarse region; individual frames and pauses are not labelled"}+ ]+ },+ {+ "sessionID": "3B9AC1A8-EB78-4C52-976D-7BF45427F423",+ "sidecar": "fv-speech-3b9a-v2.json",+ "intervals": [+ {"start": 0.0, "end": 131.8697643330088, "label": "noNearEndSpeech", "provenance": "user reported playback-only for whole recording"}+ ]+ },+ {+ "sessionID": "720AB8CD-842E-4D25-97A1-9CAAFBC28E74",+ "sidecar": "fv-speech-control-playback-v1.json",+ "intervals": [+ {"start": 0.0, "end": 155.2994252916542, "label": "noNearEndSpeech", "provenance": "controlled recording 1: user reported YouTube playback through speakers, no near-end speech, and nobody else nearby for the whole recording"}+ ]+ },+ {+ "sessionID": "C102F9B6-C61F-48EB-8F8B-A36451C712B8",+ "sidecar": "fv-speech-control-normal-v1.json",+ "intervals": [+ {"start": 0.0, "end": 11.663354208343662, "label": "coarseNearEndSpeechRegion", "provenance": "controlled recording 2: user reported normal speech into the microphone with playback stopped and nobody else nearby; individual speech and pause frames are not labelled"}+ ]+ },+ {+ "sessionID": "4734B958-A92F-4AEB-954C-7A9AC91DE457",+ "sidecar": "fv-speech-control-quiet-v1.json",+ "intervals": [+ {"start": 0.0, "end": 10.022595125017688, "label": "coarseNearEndSpeechRegion", "provenance": "controlled recording 3: user reported quiet speech into the microphone with playback stopped and nobody else nearby; individual speech and pause frames are not labelled"}+ ]+ },+ {+ "sessionID": "51507DB8-FD6B-48CD-9996-70AED789BDF5",+ "sidecar": "fv-speech-control-normal-overlap-v1.json",+ "intervals": [+ {"start": 18.0, "end": 31.0, "label": "coarseNearEndSpeechRegion", "provenance": "controlled recording 4: user reported normal phrase into the microphone over YouTube playback through speakers with nobody else nearby; interval coarsely localized from the saved transcript and is not independent frame truth"},+ {"start": 97.0, "end": 109.0, "label": "coarseNearEndSpeechRegion", "provenance": "controlled recording 4: user reported normal phrase into the microphone over YouTube playback through speakers with nobody else nearby; interval coarsely localized from the saved transcript and is not independent frame truth"},+ {"start": 147.0, "end": 157.2600000000093, "label": "coarseNearEndSpeechRegion", "provenance": "controlled recording 4: user reported normal phrase into the microphone over YouTube playback through speakers with nobody else nearby; interval coarsely localized from the saved transcript and is not independent frame truth"}+ ]+ },+ {+ "sessionID": "990EDF30-4BDD-420F-B78C-4CB5D430F998",+ "sidecar": "fv-speech-control-quiet-overlap-v1.json",+ "intervals": [+ {"start": 0.0, "end": 60.92128191667143, "label": "coarseNearEndSpeechRegion", "provenance": "controlled recording 5: user reported two sequences of five quiet replies into the microphone over YouTube playback through speakers with nobody else nearby; individual speech and pause frames are not labelled"}+ ]+ }+ ]+}diff --git a/tools/meeting_admission_b3_eval.py b/tools/meeting_admission_b3_eval.pynew file mode 100644index 00000000..4a7c077e--- /dev/null+++ b/tools/meeting_admission_b3_eval.py@@ -0,0 +1,302 @@+#!/usr/bin/env python3+"""Development-only B3 ablation over numeric sidecars and coarse user labels.++Reads transcript text locally only to count whitespace-delimited words. Output never contains+text, speaker IDs, paths, or segment IDs. It does not mutate sessions or choose a threshold.+"""+import argparse+import hashlib+import json+import math+import uuid+from pathlib import Path+++LABELS = {"noNearEndSpeech", "coarseNearEndSpeechRegion"}+THRESHOLDS = [0.0] + [round(i / 100, 2) for i in range(1, 11)] + [round(i / 20, 2) for i in range(3, 20)]+REPORT_THRESHOLDS = [0.01, 0.05, 0.10, 0.50, 0.85, 0.95]+EXPECTED_SPEECH_MODEL_FINGERPRINT = "259aec29878cbca1118f4e4fa1db126676286c90b65903266545f50e9ac4512d"+++def sha256(path):+ return hashlib.sha256(Path(path).read_bytes()).hexdigest()+++def seconds(stamp):+ if not isinstance(stamp, dict) or not isinstance(stamp.get("value"), (int, float)):+ raise ValueError("invalid timestamp")+ scale = stamp.get("timescale")+ if not isinstance(scale, (int, float)) or scale <= 0:+ raise ValueError("invalid timescale")+ value = stamp["value"] / scale+ if not math.isfinite(value):+ raise ValueError("non-finite timestamp")+ return value+++def label_for(start, end, intervals):+ matches = [item["label"] for item in intervals+ if start >= item["start"] and end <= item["end"]]+ return matches[0] if len(matches) == 1 else None+++def validate_annotations(payload):+ if payload.get("schemaVersion") != 1 or not isinstance(payload.get("sessions"), list):+ raise ValueError("unsupported annotation schema")+ seen = set()+ for session in payload["sessions"]:+ session_id = session.get("sessionID")+ try:+ canonical_id = str(uuid.UUID(session_id)).upper()+ except (ValueError, TypeError, AttributeError):+ raise ValueError("invalid session ID")+ sidecar = session.get("sidecar")+ if (canonical_id != session_id or session_id in seen or not isinstance(sidecar, str)+ or Path(sidecar).name != sidecar or "/" in sidecar or "\\" in sidecar):+ raise ValueError("invalid or duplicate session")+ seen.add(session_id)+ intervals = session.get("intervals")+ if not isinstance(intervals, list) or not intervals:+ raise ValueError("session requires at least one annotation interval")+ previous_end = -1.0+ for interval in intervals:+ start, end = interval.get("start"), interval.get("end")+ if (not isinstance(start, (int, float)) or not isinstance(end, (int, float))+ or not math.isfinite(start) or not math.isfinite(end)+ or start < 0 or end <= start or start < previous_end+ or interval.get("label") not in LABELS+ or not isinstance(interval.get("provenance"), str)+ or not interval["provenance"]):+ raise ValueError("invalid annotation interval")+ previous_end = end+ return payload+++def confusion(rows, accept):+ negative = [r for r in rows if r["label"] == "noNearEndSpeech"]+ coarse = [r for r in rows if r["label"] == "coarseNearEndSpeechRegion"]+ false_accept = sum(accept(r) for r in negative)+ accepted_coarse = sum(accept(r) for r in coarse)+ return {+ "negativeMeasuredFrames": len(negative),+ "falseAcceptedNegativeFrames": false_accept,+ "falseAcceptanceFraction": false_accept / len(negative) if negative else None,+ "coarseScriptRegionMeasuredFrames": len(coarse),+ "acceptedCoarseScriptRegionFrames": accepted_coarse,+ "acceptedCoarseScriptRegionFraction": accepted_coarse / len(coarse) if coarse else None,+ "warning": "Coarse-region acceptance is not speech recall; individual speech/pause frames are unlabelled.",+ }+++def session_rows(sidecar, intervals):+ samples, rate = sidecar.get("speechFrameSamples"), sidecar.get("speechFrameSampleRate")+ if samples != 4096 or rate != 16000:+ raise ValueError("unexpected speech frame geometry")+ duration = samples / rate+ rows = []+ coverage = {label: {"labelledFrames": 0, "measuredFrames": 0, "unknownFrames": 0}+ for label in LABELS}+ excluded = {"boundaryStraddlingFrames": 0, "outsideAnnotationFrames": 0}+ frames = sidecar.get("speechFrames")+ if not isinstance(frames, list) or not frames:+ raise ValueError("missing speech frames")+ for record in frames:+ frame = record.get("frame", {})+ probability = frame.get("probability")+ unknown = frame.get("unknown")+ start = frame.get("start")+ if not isinstance(start, (int, float)) or not math.isfinite(start):+ raise ValueError("invalid frame start")+ end = start + duration+ label = label_for(start, end, intervals)+ if label is None:+ if any(end > item["start"] and start < item["end"] for item in intervals):+ excluded["boundaryStraddlingFrames"] += 1+ else:+ excluded["outsideAnnotationFrames"] += 1+ continue+ coverage[label]["labelledFrames"] += 1+ if probability is None:+ if not isinstance(unknown, str) or not unknown:+ raise ValueError("missing probability without unknown reason")+ coverage[label]["unknownFrames"] += 1+ continue+ if unknown is not None or not isinstance(probability, (int, float)) or not math.isfinite(probability):+ raise ValueError("invalid measured probability")+ if not 0 <= probability <= 1:+ raise ValueError("probability outside [0,1]")+ temporal = record.get("temporalState")+ if temporal not in {"supported", "notSupported", "mixed", "unavailable"}:+ raise ValueError("unknown temporal state")+ coverage[label]["measuredFrames"] += 1+ rows.append({"label": label, "probability": probability, "temporal": temporal})+ return rows, {"byLabel": coverage, **excluded, "frameDurationSeconds": duration}+++def transcript_counts(session, intervals):+ tracks = session.get("audioTracks")+ if not isinstance(tracks, list):+ raise ValueError("invalid tracks")+ mic_ids = [t.get("id") for t in tracks if t.get("kind") == "microphone"]+ if len(mic_ids) != 1:+ raise ValueError("expected one microphone track")+ counts = {label: {"segments": 0, "words": 0, "visibleSegments": 0,+ "visibleWords": 0, "representedVisibleSpeakers": set()}+ for label in LABELS}+ excluded = {"boundaryStraddlingSegments": 0, "boundaryStraddlingWords": 0,+ "outsideAnnotationSegments": 0, "outsideAnnotationWords": 0}+ for segment in session.get("transcriptSegments", []):+ if segment.get("sourceTrackID") != mic_ids[0]:+ continue+ start, end = seconds(segment.get("start")), seconds(segment.get("end"))+ text = segment.get("text")+ if not isinstance(text, str):+ raise ValueError("invalid transcript text")+ words = len(text.split())+ label = label_for(start, end, intervals)+ if label is None:+ key = "boundaryStraddling" if any(end > item["start"] and start < item["end"] for item in intervals) else "outsideAnnotation"+ excluded[key + "Segments"] += 1+ excluded[key + "Words"] += words+ continue+ item = counts[label]+ item["segments"] += 1+ item["words"] += words+ if not segment.get("isLikelyEcho", False):+ item["visibleSegments"] += 1+ item["visibleWords"] += words+ if segment.get("speakerID") is not None:+ item["representedVisibleSpeakers"].add(segment["speakerID"])+ return {"byLabel": {label: {**values, "representedVisibleSpeakers": len(values["representedVisibleSpeakers"])}+ for label, values in counts.items()}, "excluded": excluded,+ "warning": "Counts inventory current saved output; coarse-region words are not independently verified and visibility reuses current isLikelyEcho."}+++def session_duration(session):+ chunks = [chunk for track in session.get("audioTracks", []) for chunk in track.get("chunks", [])]+ if not chunks:+ raise ValueError("session has no audio chunks")+ starts, ends = [], []+ for chunk in chunks:+ start, end = seconds(chunk.get("presentationStart")), seconds(chunk.get("presentationEnd"))+ if end <= start:+ raise ValueError("invalid chunk interval")+ starts.append(start); ends.append(end)+ return max(ends) - min(starts)+++def evaluate(annotation_path, sessions_root, sidecar_root):+ annotations = validate_annotations(json.loads(Path(annotation_path).read_text()))+ all_rows, session_reports = [], []+ model_identity = None+ for item in annotations["sessions"]:+ session_path = Path(sessions_root) / item["sessionID"] / "session.json"+ sidecar_path = Path(sidecar_root) / item["sidecar"]+ session_bytes, sidecar_bytes = session_path.read_bytes(), sidecar_path.read_bytes()+ session, sidecar = json.loads(session_bytes), json.loads(sidecar_bytes)+ if session.get("id") != item["sessionID"]:+ raise ValueError("session ID mismatch")+ session_hash = hashlib.sha256(session_bytes).hexdigest()+ if sidecar.get("sessionSHA256") != session_hash:+ raise ValueError("sidecar/session hash mismatch")+ if sidecar.get("speechAdapterVersion") != "speech-shadow-b2-v1":+ raise ValueError("unexpected speech adapter")+ hashes = sidecar.get("speechModelHashes")+ if (sidecar.get("version") != "temporal-shadow-b1-v1"+ or sidecar.get("speechModelStatus") != "local-cpu-only-silero-256ms-v6; threshold=0.85; warmup=one-frame"+ or sidecar.get("speechDiagnosticThreshold") != 0.85+ or not isinstance(hashes, dict) or not hashes+ or any(not isinstance(key, str) or not key or not isinstance(value, str)+ or len(value) != 64 or any(c not in "0123456789abcdef" for c in value)+ for key, value in hashes.items())):+ raise ValueError("unexpected evidence provenance")+ identity = json.dumps(hashes, sort_keys=True, separators=(",", ":"))+ identity_hash = hashlib.sha256(identity.encode()).hexdigest()+ if identity_hash != EXPECTED_SPEECH_MODEL_FINGERPRINT:+ raise ValueError("unpinned speech model artifact")+ if model_identity is not None and identity != model_identity:+ raise ValueError("mixed speech model artifacts")+ model_identity = identity+ duration = sidecar.get("durationSeconds")+ if not isinstance(duration, (int, float)) or not math.isfinite(duration) or duration <= 0:+ raise ValueError("invalid sidecar duration")+ if abs(duration - session_duration(session)) > 1e-6:+ raise ValueError("sidecar/session duration mismatch")+ if any(interval["end"] > duration + 1e-6 for interval in item["intervals"]):+ raise ValueError("annotation exceeds recording")+ rows, coverage = session_rows(sidecar, item["intervals"])+ all_rows.extend(rows)+ frame_ablation = {str(threshold): confusion(rows, lambda row, t=threshold: row["probability"] >= t)+ for threshold in REPORT_THRESHOLDS}+ session_reports.append({+ "sessionIDPrefix": item["sessionID"].split("-")[0] + "…",+ "sessionSHA256": session_hash,+ "sidecarSHA256": hashlib.sha256(sidecar_bytes).hexdigest(),+ "frameCoverage": coverage,+ "currentTranscript": transcript_counts(session, item["intervals"]),+ "speechOnlyFrameAblation": frame_ablation,+ })+ baseline = confusion(all_rows, lambda _: True)+ temporal = confusion(all_rows, lambda row: row["temporal"] != "supported")+ sweeps = []+ for threshold in THRESHOLDS:+ speech = confusion(all_rows, lambda row, t=threshold: row["probability"] >= t)+ # Current data has no supported duplicate. Keep the ablation explicit rather than+ # multiplying correlated scores or inventing a combined confidence.+ combined = confusion(all_rows, lambda row, t=threshold:+ row["probability"] >= t and row["temporal"] != "supported")+ sweeps.append({"speechThreshold": threshold, "speechOnly": speech,+ "speechAndNoSupportedDuplicate": combined})+ transcript_aggregate = {label: {"segments": 0, "words": 0, "visibleSegments": 0,+ "visibleWords": 0, "representedVisibleSpeakerSlots": 0}+ for label in sorted(LABELS)}+ for session in session_reports:+ for label, values in session["currentTranscript"]["byLabel"].items():+ for key in ("segments", "words", "visibleSegments", "visibleWords"):+ transcript_aggregate[label][key] += values[key]+ transcript_aggregate[label]["representedVisibleSpeakerSlots"] += values["representedVisibleSpeakers"]+ return {+ "schemaVersion": 1,+ "status": "development-insufficient-for-selection",+ "warning": "Development recordings, correlated 256ms frames, post-hoc user reports, and coarse speech regions; not word accuracy, recall, WER, or held-out evidence.",+ "annotationSHA256": sha256(annotation_path),+ "speechModelFingerprintSHA256": EXPECTED_SPEECH_MODEL_FINGERPRINT,+ "sessionCount": len(session_reports),+ "independentlyFrameLabelledPositiveRecordingCount": 0,+ "recordingsWithCoarseScriptRegion": sum(any(i["label"] == "coarseNearEndSpeechRegion" for i in s["intervals"])+ for s in annotations["sessions"]),+ "recordingsWithNegativeIntervals": sum(any(i["label"] == "noNearEndSpeech" for i in s["intervals"])+ for s in annotations["sessions"]),+ "pureNegativeRecordingCount": sum(all(i["label"] == "noNearEndSpeech" for i in s["intervals"])+ for s in annotations["sessions"]),+ "mixedLabelRecordingCount": sum(len({i["label"] for i in s["intervals"]}) > 1+ for s in annotations["sessions"]),+ "baselineAcceptAllMeasuredFrames": baseline,+ "currentTemporalOnly": temporal,+ "thresholdSweeps": sweeps,+ "thresholdSweepPurpose": "Descriptive development tradeoff only; no threshold ranking, candidate list, or selection gate.",+ "selection": None,+ "selectionReason": "No independently frame-labelled positive audio or held-out set; controlled speech regions remain coarse, and there are no supported temporal windows.",+ "sessions": session_reports,+ "currentTranscriptAggregate": transcript_aggregate,+ "privacy": "Output contains hashes and aggregate counts only; no text, speaker IDs, paths, segment IDs, audio, or embeddings.",+ }+++def main():+ parser = argparse.ArgumentParser()+ parser.add_argument("--annotations", required=True)+ parser.add_argument("--sessions-root", required=True)+ parser.add_argument("--sidecar-root", required=True)+ parser.add_argument("--output", required=True)+ args = parser.parse_args()+ output = Path(args.output)+ if output.exists():+ raise SystemExit("refusing to overwrite output")+ result = evaluate(args.annotations, args.sessions_root, args.sidecar_root)+ output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")+ print(f"Created aggregate-only development evaluation: {output}")+++if __name__ == "__main__":+ main()diff --git a/tools/meeting_c2_browser_stimulus.html b/tools/meeting_c2_browser_stimulus.htmlnew file mode 100644index 00000000..63001b1f--- /dev/null+++ b/tools/meeting_c2_browser_stimulus.html@@ -0,0 +1,80 @@+<!doctype html>+<meta charset="utf-8">+<title>FluidVoice C2 Diagnostic Stimulus — READY</title>+<style>+ :root { color-scheme: dark; font-family: system-ui, sans-serif; }+ body { max-width: 46rem; margin: 5rem auto; padding: 0 2rem; background: #11151b; color: #e9eef6; }+ button { font: inherit; font-weight: 650; padding: .8rem 1.1rem; border: 0; border-radius: .65rem;+ color: #07111f; background: #7cc4ff; cursor: pointer; }+ button:disabled { opacity: .55; cursor: default; }+ code { color: #9bd3ff; }+ #status { margin-top: 1.25rem; min-height: 1.5rem; }+</style>+<h1>FluidVoice C2 diagnostic stimulus</h1>+<p>+ A deterministic 12-second, low-level sweep and speech-like tone for the selected-application+ ScreenCaptureKit probe. No microphone or user data is read by this page.+</p>+<p>Browser master gain: <code>0.08</code>. FluidVoice still performs its own route checks.</p>+<button id="start" type="button">Start 12-second diagnostic</button>+<div id="status" role="status">Ready.</div>+<script>+ const button = document.querySelector('#start');+ const status = document.querySelector('#status');++ button.addEventListener('click', async () => {+ button.disabled = true;+ // Trial A uses the window title as a cross-process readiness handshake. The title is+ // intentionally PLAYING only after this user gesture has resumed the AudioContext.+ const context = new AudioContext({ sampleRate: 48000 });+ await context.resume();+ document.title = 'FluidVoice C2 Diagnostic Stimulus — PLAYING';+ const t0 = context.currentTime + 0.20;+ const master = context.createGain();+ master.gain.setValueAtTime(0.08, t0);+ master.connect(context.destination);++ const sweep = context.createOscillator();+ const sweepGain = context.createGain();+ sweep.type = 'sine';+ sweep.frequency.setValueAtTime(180, t0 + 0.50);+ sweep.frequency.exponentialRampToValueAtTime(7800, t0 + 3.50);+ sweepGain.gain.setValueAtTime(0, t0);+ sweepGain.gain.setValueAtTime(0, t0 + 0.50);+ sweepGain.gain.linearRampToValueAtTime(0.45, t0 + 0.55);+ sweepGain.gain.setValueAtTime(0.45, t0 + 3.45);+ sweepGain.gain.linearRampToValueAtTime(0, t0 + 3.50);+ sweep.connect(sweepGain).connect(master);+ sweep.start(t0);+ sweep.stop(t0 + 3.55);++ const voiced = context.createOscillator();+ const voicedGain = context.createGain();+ voiced.type = 'sawtooth';+ voiced.frequency.setValueAtTime(115, t0 + 4.00);+ voiced.frequency.linearRampToValueAtTime(145, t0 + 10.50);+ voicedGain.gain.setValueAtTime(0, t0);+ voicedGain.gain.setValueAtTime(0, t0 + 4.00);+ for (let i = 0; i < 26; i++) {+ const start = t0 + 4.00 + i * 0.25;+ voicedGain.gain.linearRampToValueAtTime(0.28, start + 0.04);+ voicedGain.gain.linearRampToValueAtTime(0.10, start + 0.19);+ voicedGain.gain.linearRampToValueAtTime(0, start + 0.24);+ }+ voiced.connect(voicedGain).connect(master);+ voiced.start(t0);+ voiced.stop(t0 + 10.55);++ const ticks = [];+ for (let remaining = 12; remaining >= 1; remaining--) {+ ticks.push(setTimeout(() => { status.textContent = `Playing: ${remaining}s remaining`; },+ (12 - remaining) * 1000));+ }+ setTimeout(async () => {+ status.textContent = 'Complete.';+ document.title = 'FluidVoice C2 Diagnostic Stimulus — COMPLETE';+ await context.close();+ button.disabled = false;+ }, 12000);+ });+</script>diff --git a/tools/meeting_external_reference_trial_a_builtin_unlocked_2026-09-10.json b/tools/meeting_external_reference_trial_a_builtin_unlocked_2026-09-10.jsonnew file mode 100644index 00000000..46f8ca0e--- /dev/null+++ b/tools/meeting_external_reference_trial_a_builtin_unlocked_2026-09-10.json@@ -0,0 +1,185 @@+{+ "exitStatus": 0,+ "report": {+ "acousticMeasurementValid": false,+ "appOwnedPlayback": false,+ "builtInRouteConfirmed": true,+ "captureValid": false,+ "clock": {+ "reason": "per-buffer PTS cannot establish an acoustic capture/reference mapping",+ "relationship": "independent PTS origins; no defensible clock mapping",+ "sharedClockEstablished": false+ },+ "combinedPeakBound": 0.02,+ "defaultInputDeviceID": 82,+ "defaultOutputDeviceID": 87,+ "measuredCombinedPeak": 0,+ "microphone": {+ "arrivalBoundaryValid": true,+ "arrivalCoverageSeconds": 5.3049835416546554,+ "backwardsTimestampCount": 0,+ "callbackCount": 53,+ "captureCloseArrivalSeconds": 173138.78600870835,+ "captureOpenArrivalSeconds": 173133.49417354167,+ "clippedSampleCount": 0,+ "coverageFraction": 1,+ "coveredDurationSeconds": 5.2999999997438847,+ "deliveredDurationSeconds": 5.2999999999999972,+ "droppedFrameCount": 0,+ "firstPresentationSeconds": 173133.48014583334,+ "formatChangeCount": 0,+ "gapCount": 0,+ "gapDurationSeconds": 0,+ "invalidTimestampCount": 0,+ "lastPresentationEndSeconds": 173138.78014583333,+ "overlapCount": 0,+ "overlapDurationSeconds": 0,+ "peak": 0.007458198349922895,+ "rms": 0.0014110774349462589,+ "synthesizedFrameCount": 0,+ "timingValid": true,+ "validSampleCount": 53+ },+ "microphoneDeviceID": 82,+ "outputDeviceID": 87,+ "outputVolume": 0.25,+ "outputVolumeReadable": true,+ "outputVolumeUnchanged": true,+ "persisted": false,+ "postCaptureOutputVolume": 0.25,+ "postCaptureRouteConfirmed": true,+ "rawPCMRetained": false,+ "reasons": [+ "diagnostic-only",+ "no app-owned playback",+ "independent clocks; acoustic delay unknown",+ "reference below excitation floor"+ ],+ "reference": {+ "arrivalBoundaryValid": true,+ "arrivalCoverageSeconds": 5.2999272083211686,+ "backwardsTimestampCount": 0,+ "callbackCount": 264,+ "captureCloseArrivalSeconds": 173138.78600783335,+ "captureOpenArrivalSeconds": 173133.49417179168,+ "clippedSampleCount": 0,+ "coverageFraction": 1,+ "coveredDurationSeconds": 5.2799999999999754,+ "deliveredDurationSeconds": 5.2799999999999754,+ "droppedFrameCount": 0,+ "firstPresentationSeconds": 173133.455253708,+ "formatChangeCount": 0,+ "gapCount": 0,+ "gapDurationSeconds": 0,+ "invalidTimestampCount": 0,+ "lastPresentationEndSeconds": 173138.735253708,+ "overlapCount": 0,+ "overlapDurationSeconds": 0,+ "peak": 0,+ "rms": 0,+ "synthesizedFrameCount": 0,+ "timingValid": true,+ "validSampleCount": 264+ },+ "referenceExcitationConfirmed": false,+ "schemaVersion": 2,+ "selectionConfirmed": true,+ "stopErrorCount": 0,+ "streamErrorCount": 0,+ "targetBundleID": "com.google.Chrome",+ "targetProcessID": 1513,+ "targetProcessStable": true,+ "transcriptRetained": false,+ "voiceProcessingReadback": {+ "bypassVoiceProcessing": {+ "element": 0,+ "propertyID": 2100,+ "scope": 0,+ "status": 0,+ "value": 0+ },+ "engineRunning": true,+ "inputCurrentDevice": {+ "element": 1,+ "propertyID": 2000,+ "scope": 0,+ "status": 0,+ "value": 82+ },+ "inputNodeInputFormat": {+ "channelCount": 9,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "inputNodeLastRenderTime": {+ "hostTime": 4155331206753,+ "sampleRate": 48000,+ "sampleTime": 260768+ },+ "inputNodeOutputConnectionCount": 0,+ "inputNodeOutputFormat": {+ "channelCount": 9,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "inputPresentationLatencySeconds": 0.0005416666666666666,+ "nodeAdvancedDuckingEnabled": false,+ "nodeDuckingLevelRawValue": 10,+ "nodeVoiceProcessingAGCEnabled": true,+ "nodeVoiceProcessingBypassed": false,+ "nodeVoiceProcessingEnabled": true,+ "nodeVoiceProcessingInputMuted": false,+ "otherAudioDucking": {+ "advancedDuckingEnabled": false,+ "duckingLevelRawValue": 10,+ "element": 0,+ "propertyID": 2108,+ "scope": 0,+ "status": 0+ },+ "outputCurrentDevice": {+ "element": 0,+ "propertyID": 2000,+ "scope": 0,+ "status": 0,+ "value": 87+ },+ "outputNodeInputFormat": {+ "channelCount": 2,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "outputNodeLastRenderTime": {+ "hostTime": 4155330914755,+ "sampleRate": 48000,+ "sampleTime": 260184+ },+ "outputNodeOutputFormat": {+ "channelCount": 2,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "outputPresentationLatencySeconds": 2.0833333333333333e-05,+ "schemaVersion": 1,+ "voiceProcessingAGCEnabled": {+ "element": 0,+ "propertyID": 2101,+ "scope": 0,+ "status": 0,+ "value": 1+ },+ "voiceProcessingOutputMuted": {+ "element": 0,+ "propertyID": 2104,+ "scope": 0,+ "status": 0,+ "value": 0+ }+ }+ },+ "status": "success"+}diff --git a/tools/meeting_reliability_baseline.py b/tools/meeting_reliability_baseline.pynew file mode 100644index 00000000..de7e4ece--- /dev/null+++ b/tools/meeting_reliability_baseline.py@@ -0,0 +1,257 @@+#!/usr/bin/env python3+"""Local P0 inventory. Read-only inputs; emits no transcript, names, or raw logs.++This is measurement tooling, not speech ground truth or an ASR-quality test.+Output creation is exclusive so an old baseline cannot be silently overwritten.+"""+import argparse+import hashlib+import json+import math+import plistlib+import re+import subprocess+from datetime import datetime, timezone+from pathlib import Path+++def sha256(path):+ digest = hashlib.sha256()+ with Path(path).open("rb") as stream:+ for block in iter(lambda: stream.read(1024 * 1024), b""):+ digest.update(block)+ return digest.hexdigest()+++def command(*args, cwd=None):+ result = subprocess.run(args, cwd=cwd, capture_output=True, text=True,+ timeout=30, check=False)+ if result.returncode:+ raise ValueError(f"{args[0]} failed (exit {result.returncode}); no raw output retained")+ return result.stdout.strip()+++def dependency_revision(path):+ payload = json.loads(Path(path).read_text())+ matches = [pin["state"]["revision"] for pin in payload["pins"]+ if pin.get("identity", "").lower() == "fluidaudio"]+ if len(matches) != 1:+ raise ValueError("expected exactly one FluidAudio pin")+ return matches[0]+++def source_inventory(repo):+ """Hash tracked dirty diff, never emit diff contents or untracked private data."""+ repo = Path(repo).resolve(strict=True)+ diff = subprocess.run(["git", "diff", "HEAD", "--binary"], cwd=repo,+ capture_output=True, timeout=30, check=True).stdout+ swift_revision = dependency_revision(repo / "Package.resolved")+ xcode_revision = dependency_revision(+ repo / "Fluid.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved")+ checkout = command("git", "rev-parse", "HEAD", cwd=repo / ".build/checkouts/FluidAudio")+ declaration = re.search(r'FluidAudio\.git"\s*,\s*revision:\s*"([0-9a-f]{40})"',+ (repo / "Package.swift").read_text())+ declared = declaration.group(1) if declaration else None+ return {+ "head": command("git", "rev-parse", "HEAD", cwd=repo),+ "trackedDirtyDiffSHA256": hashlib.sha256(diff).hexdigest(),+ "trackedDirty": bool(diff),+ "dirtyFingerprintScope": "tracked diff only; explicit tooling hashes recorded separately",+ "fluidAudio": {"declared": declared, "swiftResolved": swift_revision,+ "xcodeResolved": xcode_revision, "localCheckout": checkout,+ "allMatch": declared == swift_revision == xcode_revision == checkout},+ "installedBinaryMatchesSource": "unproven",+ }+++def installed_inventory(app):+ app = Path(app).resolve(strict=True)+ with (app / "Contents/Info.plist").open("rb") as stream:+ info = plistlib.load(stream)+ executable = info.get("CFBundleExecutable")+ if not isinstance(executable, str) or Path(executable).name != executable:+ raise ValueError("unsafe bundle executable name")+ files = []+ for file in sorted((app / "Contents/MacOS").iterdir()):+ if file.is_file() and not file.is_symlink():+ files.append({"name": file.name, "sha256": sha256(file), "bytes": file.stat().st_size})+ # Caller may perform codesign verification with the host trust store separately.+ return {"bundleIdentifier": info.get("CFBundleIdentifier"),+ "version": info.get("CFBundleShortVersionString"),+ "build": info.get("CFBundleVersion"), "executables": files,+ "signatureVerification": "not performed by this inventory",+ "recordingTimeBuildMatch": "unproven without embedded build manifest"}+++def safe_child(root, relative):+ if not isinstance(relative, str) or not relative or "\\" in relative:+ raise ValueError("invalid relative path")+ if relative.startswith("/") or any(p in ("", ".", "..") for p in relative.split("/")):+ raise ValueError("unsafe relative path")+ root = Path(root).resolve(strict=True)+ candidate = (root / relative).resolve(strict=True)+ if not candidate.is_relative_to(root) or candidate == root:+ raise ValueError("path escapes input root")+ return candidate+++def session_inventory(session_path):+ path = Path(session_path)+ # Snapshot bytes once so the fingerprint identifies exactly what was analyzed.+ raw = path.read_bytes()+ session = json.loads(raw)+ tracks = []+ for track in session.get("audioTracks", []):+ kind = track.get("kind")+ if kind not in ("microphone", "applicationAudio"):+ raise ValueError("unknown track kind")+ chunks = []+ for chunk in track.get("chunks", []):+ file = safe_child(path.parent, chunk["relativeFilePath"])+ actual = sha256(file)+ chunks.append({"sequence": chunk.get("sequence"), "bytes": file.stat().st_size,+ "sha256": actual, "storedSHA256Matches": actual == chunk.get("sha256"),+ "discontinuityCount": len(chunk.get("discontinuities", []))})+ all_segments = session.get("transcriptSegments", [])+ if any("sourceTrackID" not in s for s in all_segments):+ raise ValueError("unknown segment schema: sourceTrackID is required")+ segments = [s for s in all_segments if s["sourceTrackID"] == track["id"]]+ visible = [s for s in segments if not s.get("isLikelyEcho", False)]+ health = track.get("health", {})+ if "droppedSampleCount" not in health:+ raise ValueError("unknown health schema: droppedSampleCount is required")+ tracks.append({"kind": kind, "captureMethod": track.get("captureMethod"),+ "format": {k: track.get("format", {}).get(k) for k in+ ("sampleRate", "channelCount", "codec", "bitRate")},+ "captureDrops": health["droppedSampleCount"],+ "chunks": chunks, "segmentCount": len(segments),+ "visibleSegmentCount": len(visible),+ "visibleSpeakerCount": len({s["speakerID"] for s in visible if s.get("speakerID") is not None}),+ "missingVisibleSpeakerIDCount": sum(s.get("speakerID") is None for s in visible),+ "echoSegmentCount": len(segments) - len(visible)})+ attempts = [{k: a.get(k) for k in ("asrModel", "asrProvider", "diarizationModel",+ "pipelineVersion", "languageCode", "stage")}+ for a in session.get("processingAttempts", [])]+ return {"sessionSHA256": hashlib.sha256(raw).hexdigest(), "schemaVersion": session.get("schemaVersion"),+ "mode": session.get("mode"), "tracks": tracks, "processingAttempts": attempts,+ "eventCount": len(session.get("events", [])), "failureCount": len(session.get("failures", [])),+ "groundTruth": "not independently annotated; inventory does not infer recording conditions",+ "warning": "segment counts describe saved output, not verified speech or person counts"}+++FLOW = re.compile(r'\[(\d\d):(\d\d):(\d\d\.\d+)\].*?\[live/(APP|MIC)\] flow '+ r'chunks=(\d+) audio=([\d.]+)s openUtterance=([\d.]+)s '+ r'partials=(\d+) utterances=(\d+) drops=(\d+)')+++def summarize_flows(lines):+ rows = {"APP": [], "MIC": []}+ for line in lines:+ match = FLOW.search(line)+ if not match:+ continue+ h, m, s, kind, chunks, audio, open_seconds, partials, utterances, drops = match.groups()+ time = int(h) * 3600 + int(m) * 60 + float(s)+ row = dict(wallSeconds=time, chunks=int(chunks), audioSeconds=float(audio),+ openSeconds=float(open_seconds), partials=int(partials),+ utterances=int(utterances), drops=int(drops))+ if not all(math.isfinite(v) for v in row.values()):+ raise ValueError("nonfinite flow data")+ if rows[kind] and any(row[key] < rows[kind][-1][key] for key in+ ("wallSeconds", "chunks", "audioSeconds", "partials", "utterances", "drops")):+ raise ValueError("flow counters/time regressed: select exactly one same-day session")+ rows[kind].append(row)+ result = {}+ for kind, samples in rows.items():+ runs = []+ first = None+ previous = None+ for row in samples:+ if previous and row["partials"] == previous["partials"] and row["chunks"] > previous["chunks"]:+ if first is None:+ first = previous+ runs.append({"observedNoPartialSpanSeconds": round(row["wallSeconds"] - first["wallSeconds"], 3),+ "audioAdvancedSeconds": round(row["audioSeconds"] - first["audioSeconds"], 3),+ "partialCount": row["partials"],+ "dropIncrease": row["drops"] - first["drops"]})+ else:+ first = None+ previous = row+ result[kind] = {"flowSamples": len(samples),+ "lastCounters": ({k: v for k, v in samples[-1].items() if k != "wallSeconds"} if samples else None),+ "longestSampledNoPartialRun": max(runs, key=lambda r: r["observedNoPartialSpanSeconds"]) if runs else None,+ "interpretation": "sampled no-text lower bound, not proof of audible-speech blackout; mic silence is expected in this test"}+ return result+++def model_manifest_inventory(manifest_path):+ path = Path(manifest_path)+ raw = path.read_bytes()+ manifest = json.loads(raw)+ records = []+ seen = set()+ repository_name = manifest["modelRepository"]+ repository = safe_child(path.parent, repository_name)+ actual_paths = set()+ for child in repository.rglob("*"):+ if child.is_symlink():+ raise ValueError("symbolic link model artifact")+ if child.is_file():+ actual_paths.add(repository_name + "/" + child.relative_to(repository).as_posix())+ for artifact in manifest.get("artifacts", []):+ relative = artifact["path"]+ if not relative.startswith(repository_name + "/"):+ raise ValueError("artifact outside model repository")+ if relative in seen:+ raise ValueError("duplicate model artifact")+ seen.add(relative)+ file = safe_child(path.parent, relative)+ actual = sha256(file)+ records.append({"path": relative, "sha256": actual,+ "matchesManifest": actual == artifact.get("sha256")})+ if not records:+ raise ValueError("empty artifact manifest")+ return {"manifestSHA256": hashlib.sha256(raw).hexdigest(),+ "repository": manifest.get("modelRepository"), "artifacts": records,+ "allHashesMatch": all(r["matchesManifest"] for r in records),+ "exactTreeMatch": actual_paths == seen,+ "huggingFaceRevision": "not recorded; content hashes identify local artifacts only"}+++def main():+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("--repo", type=Path, required=True)+ parser.add_argument("--app", type=Path, required=True)+ parser.add_argument("--session", type=Path, required=True)+ parser.add_argument("--model-manifest", type=Path, required=True)+ parser.add_argument("--log", type=Path)+ parser.add_argument("--log-first-line", type=int)+ parser.add_argument("--log-last-line", type=int)+ parser.add_argument("--output", type=Path, required=True)+ args = parser.parse_args()+ report = {"schemaVersion": 1, "createdAt": datetime.now(timezone.utc).isoformat(),+ "scope": "local P0 inventory; no ASR inference, no ground-truth verification",+ "toolSHA256": sha256(Path(__file__)), "source": source_inventory(args.repo),+ "installed": installed_inventory(args.app), "session": session_inventory(args.session),+ "offlineASRArtifacts": model_manifest_inventory(args.model_manifest)}+ if args.log:+ if not args.log_first_line or not args.log_last_line or not 1 <= args.log_first_line <= args.log_last_line:+ parser.error("log requires explicit ordered positive line bounds")+ raw = args.log.read_bytes()+ lines = raw.decode("utf-8", errors="replace").splitlines()+ if args.log_last_line > len(lines):+ parser.error("log range exceeds input length")+ selected = lines[args.log_first_line - 1:args.log_last_line]+ report["historicalFlow"] = {"logSnapshotSHA256": hashlib.sha256(raw).hexdigest(),+ "firstLine": args.log_first_line, "lastLine": args.log_last_line,+ "selection": "caller-selected same-session range; logs contain no date binding",+ "tracks": summarize_flows(selected)}+ args.output.parent.mkdir(parents=True, exist_ok=True)+ with args.output.open("x") as stream:+ json.dump(report, stream, indent=2, sort_keys=True, allow_nan=False)+ stream.write("\n")+ print("P0 inventory written; no input changed and no audio/transcript emitted")+++if __name__ == "__main__":+ main()diff --git a/tools/meeting_sck_paired_c2_builtin_2026-09-10.json b/tools/meeting_sck_paired_c2_builtin_2026-09-10.jsonnew file mode 100644index 00000000..d859d57f--- /dev/null+++ b/tools/meeting_sck_paired_c2_builtin_2026-09-10.json@@ -0,0 +1,15 @@+{+ "captureDurationSeconds": 0,+ "captureStarted": false,+ "displayCount": 0,+ "exitStatus": 1,+ "microphoneAuthorizationPassed": true,+ "rawPCMRetained": false,+ "reason": "no display is available",+ "schemaVersion": 1,+ "screenRecordingPreflightPassed": true,+ "status": "invalid",+ "transcriptRetained": false,+ "trial": "C2",+ "valid": false+}diff --git a/tools/meeting_sck_paired_c2_builtin_unlocked_2026-09-10.json b/tools/meeting_sck_paired_c2_builtin_unlocked_2026-09-10.jsonnew file mode 100644index 00000000..083bae47--- /dev/null+++ b/tools/meeting_sck_paired_c2_builtin_unlocked_2026-09-10.json@@ -0,0 +1,76 @@+{+ "exitStatus": 0,+ "report": {+ "acceptedSampleCount": 737,+ "application": {+ "backwardsTimestampCount": 0,+ "coverageFraction": 1,+ "deliveredDurationSeconds": 5.1999999999999771,+ "firstChannelCount": 2,+ "firstPresentationSeconds": 169340.14785104099,+ "firstSampleRateHz": 48000,+ "formatChangeCount": 0,+ "gapCount": 93,+ "gapDurationSeconds": 1.7322599500657621e-09,+ "invalidTimestampCount": 0,+ "jitterObservationCount": 259,+ "lastPresentationEndSeconds": 169345.34785104098,+ "malformedMetadataCount": 0,+ "maxAbsoluteJitterSeconds": 0.01998691666405648,+ "meanAbsoluteJitterSeconds": 0.002437802284724938,+ "overlapCount": 166,+ "sampleCount": 260,+ "timestampSpanSeconds": 5.1999999999825377,+ "validTimestampCount": 260+ },+ "boundedMaximumSamples": 20000,+ "crossTrack": {+ "aecEvaluated": false,+ "firstPresentationOffsetSeconds": 0.15400312567362562,+ "lastPresentationOffsetSeconds": 0.042003125679912046,+ "relativeSpanDriftPPM": -21538.461537324973,+ "sharedClockEstablished": false,+ "timestampRelationship": "independent PTS origins; measured offset/drift only"+ },+ "droppedAfterBoundCount": 0,+ "enabled": true,+ "failOpen": true,+ "microphone": {+ "backwardsTimestampCount": 0,+ "coverageFraction": 1,+ "deliveredDurationSeconds": 5.0879999999999956,+ "firstChannelCount": 1,+ "firstPresentationSeconds": 169340.30185416667,+ "firstSampleRateHz": 48000,+ "formatChangeCount": 0,+ "gapCount": 440,+ "gapDurationSeconds": 9.5615799999837492e-10,+ "invalidTimestampCount": 0,+ "jitterObservationCount": 476,+ "lastPresentationEndSeconds": 169345.38985416666,+ "malformedMetadataCount": 0,+ "maxAbsoluteJitterSeconds": 0.0019029583178150154,+ "meanAbsoluteJitterSeconds": 8.0990108485100049e-05,+ "overlapCount": 36,+ "sampleCount": 477,+ "timestampSpanSeconds": 5.0879999999888241,+ "validTimestampCount": 477+ },+ "provenance": {+ "applicationSelectionConfirmed": true,+ "captureMethod": "screenCaptureKit",+ "scope": "selectedApplicationDisplay"+ },+ "rawPCMRetained": false,+ "reasons": [+ "diagnostic-only",+ "no raw PCM or transcript retention",+ "shared clock not established",+ "acoustic delay and AEC not evaluated"+ ],+ "rejectedProvenanceCount": 0,+ "schemaVersion": 2,+ "transcriptRetained": false+ },+ "status": "success"+}diff --git a/tools/meeting_temporal_shadow.swift b/tools/meeting_temporal_shadow.swiftnew file mode 100644index 00000000..6be2026c--- /dev/null+++ b/tools/meeting_temporal_shadow.swift@@ -0,0 +1,274 @@+import AVFoundation+import CryptoKit+import Foundation++/// Local offline evaluator. Compile with MeetingPlaybackDuplicateDetector.swift. No ASR/model+/// loading, networking, original-session writes, text/embedding output or app installation.+@main+struct MeetingTemporalShadow {+ struct Stamp: Decodable {+ let value: Double+ let timescale: Double+ var seconds: Double { value / timescale }+ }+ struct Discontinuity: Decodable { let presentationTime: Stamp? }+ struct Chunk: Decodable {+ let id: UUID+ let relativeFilePath: String+ let presentationStart: Stamp+ let presentationEnd: Stamp+ let discontinuities: [Discontinuity]+ }+ struct Track: Decodable { let kind: String; let chunks: [Chunk] }+ struct Session: Decodable { let id: UUID; let audioTracks: [Track] }+ struct InputFingerprint: Codable { let chunkID: UUID; let sha256: String }+ struct SpeechRecord: Encodable {+ let frame: MeetingSpeechActivityDetector<MeetingLocalSileroActivityModel>.Frame+ let temporalState: MeetingSpeechPlaybackShadowPolicy.TemporalContext+ let combinedReason: MeetingSpeechPlaybackShadowPolicy.Reason+ let proposedAdmission = "uncertainCandidate"+ let legacyEchoEvidence = "notMeasured"+ }+ struct Report: Encodable {+ let version = MeetingPlaybackDuplicateDetector.version+ let experimental = true+ let scope = "Offline temporal evidence only; no speech absence/admission/identity conclusions. No enforcement."+ let sessionSHA256: String+ let inputs: [InputFingerprint]+ let configuration: MeetingPlaybackDuplicateDetector.Configuration+ let sampleRate = MeetingPlaybackDuplicateDetector.sampleRate+ let boundaryGuardSeconds = 2.0+ let decoderEdgeGuardSeconds = 0.05+ let controlOffsetSeconds = 8.0+ let minimumCorrelation = 0.25+ let minimumControlMargin = 0.08+ let minimumPeakMargin = 0.025+ let supportWindows = 3+ let holdSeconds = 0+ let durationSeconds: Double+ let unwindowedTailSeconds: Double+ let maximumWindowWallSeconds: Double+ let stateCounts: [String: Int]+ let reasonCounts: [String: Int]+ let windows: [MeetingPlaybackDuplicateDetector.Result]+ let speechModelHashes: [String: String]?+ let speechModelStatus: String+ let speechAdapterVersion = "speech-shadow-b2-v1"+ let speechFrameSampleRate = 16_000+ let speechFrameSamples = 4096+ let speechDiagnosticThreshold = 0.85+ let speechFrames: [SpeechRecord]?+ let speechUnwindowedTailSeconds: Double?+ let speechUnavailableSeconds: Double?+ }+ enum Failure: Error { case invalidInput, unsafePath, decoder, outputExists }++ static func decode(_ url: URL, rate: Double = 2_000) throws -> [Float] {+ let file = try AVAudioFile(forReading: url)+ let format = file.processingFormat+ guard format.sampleRate.isFinite, format.sampleRate >= 2_000,+ format.sampleRate <= 192_000, (1...8).contains(format.channelCount),+ file.length > 0, file.length <= Int64(format.sampleRate * 65),+ let input = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(file.length)),+ let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: rate,+ channels: 1, interleaved: false),+ let converter = AVAudioConverter(from: format, to: outputFormat),+ let output = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(rate * 66)) else {+ throw Failure.decoder+ }+ try file.read(into: input)+ converter.sampleRateConverterQuality = AVAudioQuality.max.rawValue+ var supplied = false+ var error: NSError?+ let status = converter.convert(to: output, error: &error) { _, status in+ if supplied { status.pointee = .endOfStream; return nil }+ supplied = true; status.pointee = .haveData; return input+ }+ guard error == nil, status != .error, output.frameLength > 0,+ let samples = output.floatChannelData?[0] else { throw Failure.decoder }+ return Array(UnsafeBufferPointer(start: samples, count: Int(output.frameLength)))+ }++ static func main() throws {+ guard (3...4).contains(CommandLine.arguments.count) else { throw Failure.invalidInput }+ let speechEnabled = CommandLine.arguments.count == 4+ var speechModel: MeetingLocalSileroActivityModel?+ var modelHashes: [String: String]?+ var modelFiles: [String: URL] = [:]+ var modelStatus = "off"+ if speechEnabled {+ // Only explicitly selected local compiled models. No default model/download API.+ let modelURL = URL(fileURLWithPath: CommandLine.arguments[3]).resolvingSymlinksInPath()+ do {+ guard modelURL.pathExtension == "mlmodelc",+ let enumerator = FileManager.default.enumerator(at: modelURL,+ includingPropertiesForKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey])+ else { throw Failure.invalidInput }+ var hashes: [String: String] = [:], totalBytes = 0+ for case let file as URL in enumerator {+ let info = try file.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey])+ guard info.isSymbolicLink != true else { throw Failure.unsafePath }+ if info.isRegularFile == true {+ totalBytes += info.fileSize ?? 0+ guard totalBytes <= 32_000_000, hashes.count < 64 else { throw Failure.invalidInput }+ let name = String(file.path.dropFirst(modelURL.path.count + 1))+ hashes[name] = SHA256.hash(data: try Data(contentsOf: file)).map { String(format: "%02x", $0) }.joined()+ modelFiles[name] = file+ }+ }+ guard !hashes.isEmpty else { throw Failure.invalidInput }+ speechModel = try MeetingLocalSileroActivityModel(modelURL: modelURL)+ modelHashes = hashes; modelStatus = "local-cpu-only-silero-256ms-v6; threshold=0.85; warmup=one-frame"+ } catch { modelStatus = "unavailable-no-download" }+ }+ let input = URL(fileURLWithPath: CommandLine.arguments[1]).resolvingSymlinksInPath()+ let output = URL(fileURLWithPath: CommandLine.arguments[2]).standardizedFileURL+ let root = input.deletingLastPathComponent()+ // Never write a sidecar into the original session directory, even via symlinks.+ let resolvedOutput = output.deletingLastPathComponent().resolvingSymlinksInPath()+ .appendingPathComponent(output.lastPathComponent)+ guard !resolvedOutput.path.hasPrefix(root.path + "/"), resolvedOutput != input else { throw Failure.unsafePath }+ guard !FileManager.default.fileExists(atPath: output.path) else { throw Failure.outputExists }+ let inputAttributes = try FileManager.default.attributesOfItem(atPath: input.path)+ guard let inputBytes = inputAttributes[.size] as? NSNumber,+ inputBytes.intValue <= 16_000_000 else { throw Failure.invalidInput }+ let data = try Data(contentsOf: input)+ let session = try JSONDecoder().decode(Session.self, from: data)+ let chunks = session.audioTracks.flatMap(\.chunks)+ guard !chunks.isEmpty, chunks.count <= 32,+ session.audioTracks.count <= 2,+ Set(session.audioTracks.map(\.kind)).count == session.audioTracks.count,+ session.audioTracks.allSatisfy({ ["microphone", "applicationAudio"].contains($0.kind) }),+ chunks.allSatisfy({ $0.presentationStart.seconds.isFinite && $0.presentationEnd.seconds.isFinite+ && $0.presentationEnd.seconds > $0.presentationStart.seconds+ && $0.presentationEnd.seconds - $0.presentationStart.seconds <= 65 }) else { throw Failure.invalidInput }+ let origin = chunks.map { $0.presentationStart.seconds }.min()!+ let duration = chunks.map { $0.presentationEnd.seconds - origin }.max()!+ guard duration > 0, duration <= 600 else { throw Failure.invalidInput }+ // Same either-track boundary definition and ±blockSeconds=2 guard as the pipeline.+ // Missing-PTS discontinuities cannot be placed: fail closed for this offline input.+ let discontinuities = chunks.flatMap(\.discontinuities)+ guard discontinuities.allSatisfy({ $0.presentationTime?.seconds.isFinite == true }) else { throw Failure.invalidInput }+ let boundaries = discontinuities.map { $0.presentationTime!.seconds - origin }.sorted()+ let count = Int(ceil(duration * 2_000))+ var tracks: [String: MeetingPlaybackDuplicateDetector.PCM] = [:]+ var fingerprints: [InputFingerprint] = []+ let speechCount = speechEnabled ? Int(ceil(duration * 16_000)) : 0+ var speechSamples = [Float](repeating: 0, count: speechCount)+ var speechValid = [Bool](repeating: false, count: speechCount)+ var speechOccupied = [Bool](repeating: false, count: speechCount)+ for track in session.audioTracks {+ var samples = [Float](repeating: 0, count: count)+ var valid = [Bool](repeating: false, count: count)+ var occupied = [Bool](repeating: false, count: count)+ for chunk in track.chunks {+ let parts = chunk.relativeFilePath.split(separator: "/", omittingEmptySubsequences: false)+ guard !parts.isEmpty, parts.allSatisfy({ !$0.isEmpty && $0 != "." && $0 != ".." }),+ !chunk.relativeFilePath.contains("\\") else { throw Failure.unsafePath }+ let url = root.appendingPathComponent(chunk.relativeFilePath).resolvingSymlinksInPath()+ guard url.path.hasPrefix(root.path + "/") else { throw Failure.unsafePath }+ let attributes = try FileManager.default.attributesOfItem(atPath: url.path)+ guard let bytes = attributes[.size] as? NSNumber, bytes.intValue <= 32_000_000 else { throw Failure.invalidInput }+ let before = SHA256.hash(data: try Data(contentsOf: url)).map { String(format: "%02x", $0) }.joined()+ let decoded = try decode(url)+ let decodedSpeech = speechEnabled && track.kind == "microphone" ? try decode(url, rate: 16_000) : nil+ let after = SHA256.hash(data: try Data(contentsOf: url)).map { String(format: "%02x", $0) }.joined()+ guard before == after else { throw Failure.invalidInput }+ fingerprints.append(.init(chunkID: chunk.id, sha256: before))+ let start = chunk.presentationStart.seconds - origin+ let end = chunk.presentationEnd.seconds - origin+ if let decodedSpeech {+ let first = max(0, Int(ceil(start * 16_000))), last = min(speechCount, Int(floor(end * 16_000)))+ for index in first..<last {+ let time = Double(index) / 16_000, position = (time - start) * 16_000+ let lower = Int(floor(position)), alpha = Float(position - floor(position))+ let usable = time >= start + 0.05 && time < end - 0.05+ && lower >= 800 && lower + 801 < decodedSpeech.count+ && !boundaries.contains(where: { abs(time - $0) <= 2 })+ if speechOccupied[index] { speechValid[index] = false; continue }+ speechOccupied[index] = true+ if usable {+ speechSamples[index] = decodedSpeech[lower] * (1 - alpha) + decodedSpeech[lower + 1] * alpha+ speechValid[index] = speechSamples[index].isFinite+ }+ }+ }+ let first = max(0, Int(ceil(start * 2_000)))+ let last = min(count, Int(floor(end * 2_000)))+ for index in first..<last {+ let time = Double(index) / 2_000+ let position = (time - start) * 2_000+ let lower = Int(floor(position)), alpha = Float(position - floor(position))+ // Trim declared end, decoded end and conservative codec/converter edges.+ let usable = time >= start + 0.05 && time < end - 0.05+ && lower >= 100 && lower + 101 < decoded.count+ && !boundaries.contains(where: { abs(time - $0) <= 2 })+ if occupied[index] { valid[index] = false; continue }+ occupied[index] = true+ if usable {+ samples[index] = decoded[lower] * (1 - alpha) + decoded[lower + 1] * alpha+ valid[index] = samples[index].isFinite+ }+ }+ }+ tracks[track.kind] = .init(start: 0, samples: samples, valid: valid)+ }+ func slice(_ track: MeetingPlaybackDuplicateDetector.PCM?, start: Double, count: Int,+ mappedStart: Double? = nil) -> MeetingPlaybackDuplicateDetector.PCM? {+ guard let track else { return nil }+ let first = Int((start * 2_000).rounded())+ var samples = [Float](repeating: 0, count: count), valid = [Bool](repeating: false, count: count)+ for i in 0..<count where first + i >= 0 && first + i < track.samples.count {+ samples[i] = track.samples[first + i]; valid[i] = track.valid[first + i]+ }+ return .init(start: mappedStart ?? start, samples: samples, valid: valid)+ }+ var detector = MeetingPlaybackDuplicateDetector()+ var results: [MeetingPlaybackDuplicateDetector.Result] = []+ for step in 0..<Int(duration / 2) {+ let start = Double(step * 2)+ let epoch = UInt64(boundaries.filter { $0 <= start }.count)+ let referenceStart = start - 0.5+ let nullStart = start + 10.5 <= duration ? referenceStart + 8 : referenceStart - 8+ let nullControl = nullStart >= 0 && nullStart + 3 <= duration+ ? slice(tracks["applicationAudio"], start: nullStart, count: 6_000, mappedStart: referenceStart) : nil+ let mic = slice(tracks["microphone"], start: start, count: 4_000)+ ?? .init(start: start, samples: [], valid: [])+ results.append(detector.process(.init(sessionID: session.id, epoch: epoch, routeID: "recorded",+ start: start, microphone: mic,+ reference: slice(tracks["applicationAudio"], start: referenceStart, count: 6_000),+ mismatchedReference: nullControl)))+ }+ var speechRecords: [SpeechRecord] = []+ if let speechModel {+ var detector = MeetingSpeechActivityDetector(model: speechModel)+ for first in stride(from: 0, through: max(-1, speechCount - 4096), by: 4096) {+ let start = Double(first) / 16_000, epoch = UInt64(boundaries.filter { $0 <= start }.count)+ let frame = detector.process(samples: Array(speechSamples[first..<(first + 4096)]),+ valid: Array(speechValid[first..<(first + 4096)]), start: start, session: session.id, epoch: epoch, route: "recorded")+ let temporal = MeetingSpeechPlaybackShadowPolicy.temporalContext(start: start, epoch: epoch, windows: results)+ speechRecords.append(.init(frame: frame, temporalState: temporal,+ combinedReason: MeetingSpeechPlaybackShadowPolicy.reason(activity: frame.active,+ reliableDuplicate: temporal == .supported)))+ }+ }+ if let modelHashes {+ for (name, file) in modelFiles {+ guard SHA256.hash(data: try Data(contentsOf: file)).map({ String(format: "%02x", $0) }).joined() == modelHashes[name]+ else { throw Failure.invalidInput }+ }+ }+ let report = Report(sessionSHA256: SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined(),+ inputs: fingerprints, configuration: .init(), durationSeconds: duration,+ unwindowedTailSeconds: duration - Double(results.count * 2),+ maximumWindowWallSeconds: results.map(\.elapsedSeconds).max() ?? 0,+ stateCounts: Dictionary(grouping: results, by: { $0.state.rawValue }).mapValues(\.count),+ reasonCounts: Dictionary(grouping: results, by: { $0.reason.rawValue }).mapValues(\.count), windows: results,+ speechModelHashes: modelHashes, speechModelStatus: modelStatus, speechFrames: speechEnabled ? speechRecords : nil,+ speechUnwindowedTailSeconds: speechModel == nil ? nil : duration - Double(speechRecords.count) * 0.256,+ speechUnavailableSeconds: speechEnabled ? duration - Double(speechRecords.filter { $0.frame.probability != nil }.count) * 0.256 : nil)+ let encoder = JSONEncoder(); encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ try encoder.encode(report).write(to: output, options: [.withoutOverwriting])+ print("Created numeric-only shadow sidecar: \(results.count) windows; no session changes.")+ }+}diff --git a/tools/meeting_vpio_acoustic_trial_b_builtin_2026-09-10.json b/tools/meeting_vpio_acoustic_trial_b_builtin_2026-09-10.jsonnew file mode 100644index 00000000..05e9f8a2--- /dev/null+++ b/tools/meeting_vpio_acoustic_trial_b_builtin_2026-09-10.json@@ -0,0 +1,198 @@+{+ "caveats" : [+ "Trial B renders known local PCM through this process's own VPIO output path; it does not route another application's audio and says nothing about whether Apple's echo reference receives other-process playback.",+ "attenuationDB compares an electrical reference against an acoustic capture and is uncalibrated. Only differences between variants measured back-to-back on one unchanged rig are readable, and even those are confounded by speaker level, room, and AGC.",+ "A single run establishes neither echo cancellation nor its absence. Treat every number as configuration and coupling evidence pending the paired ScreenCaptureKit topology comparison."+ ],+ "postRollSeconds" : 0.6,+ "preRollSeconds" : 0.3,+ "renderVolume" : 0.5,+ "runs" : [+ {+ "captureWindow" : {+ "contributingSegmentCount" : 36,+ "coverage" : 0.9472752192982457,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 149569.71863929168,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 172783+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : true,+ "appliedBypass" : false,+ "readbackAGCEnabled" : true,+ "readbackAGCRawValue" : 1,+ "readbackAGCStatus" : 0,+ "readbackBypass" : false,+ "readbackBypassRawValue" : 0,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : true,+ "requestedBypass" : false+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 149570.01863929167,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioActive"+ },+ {+ "captureWindow" : {+ "contributingSegmentCount" : 36,+ "coverage" : 0.9329002192982456,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 149574.2732747917,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 170161+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : false,+ "appliedBypass" : false,+ "readbackAGCEnabled" : false,+ "readbackAGCRawValue" : 0,+ "readbackAGCStatus" : 0,+ "readbackBypass" : false,+ "readbackBypassRawValue" : 0,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : false,+ "requestedBypass" : false+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 149574.57327479168,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioActiveNoAGC"+ },+ {+ "captureWindow" : {+ "contributingSegmentCount" : 36,+ "coverage" : 0.9473684210526315,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 149578.81724337503,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 172800+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : true,+ "appliedBypass" : true,+ "readbackAGCEnabled" : true,+ "readbackAGCRawValue" : 1,+ "readbackAGCStatus" : 0,+ "readbackBypass" : true,+ "readbackBypassRawValue" : 1,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : true,+ "requestedBypass" : true+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 149579.11724337502,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioBypassed"+ }+ ],+ "schemaVersion" : 1,+ "settleSeconds" : 0.35,+ "stimulus" : {+ "chirpEndHz" : 7800,+ "chirpFrameCount" : 48000,+ "chirpStartHz" : 180,+ "gapFrameCount" : 9600,+ "leadingSilenceFrameCount" : 9600,+ "measuredPeak" : 0.25,+ "measuredRMS" : 0.09035877883434296,+ "safetyMaximumRMS" : 0.15000000596046448,+ "safetyMinimumRMS" : 0.019999999552965164,+ "safetyPeakLimit" : 0.3499999940395355,+ "sampleRate" : 48000,+ "schemaVersion" : 1,+ "seed" : 6853810188858556417,+ "speechF0EndHz" : 145,+ "speechF0StartHz" : 115,+ "speechLikeFrameCount" : 57600,+ "targetPeak" : 0.25,+ "totalFrameCount" : 139200,+ "trailingSilenceFrameCount" : 14400,+ "withinSafetyBounds" : true+ }+}diff --git a/tools/meeting_vpio_acoustic_trial_b_builtin_rerun1_2026-09-10.json b/tools/meeting_vpio_acoustic_trial_b_builtin_rerun1_2026-09-10.jsonnew file mode 100644index 00000000..ec66993a--- /dev/null+++ b/tools/meeting_vpio_acoustic_trial_b_builtin_rerun1_2026-09-10.json@@ -0,0 +1,198 @@+{+ "caveats" : [+ "Trial B renders known local PCM through this process's own VPIO output path; it does not route another application's audio and says nothing about whether Apple's echo reference receives other-process playback.",+ "attenuationDB compares an electrical reference against an acoustic capture and is uncalibrated. Only differences between variants measured back-to-back on one unchanged rig are readable, and even those are confounded by speaker level, room, and AGC.",+ "A single run establishes neither echo cancellation nor its absence. Treat every number as configuration and coupling evidence pending the paired ScreenCaptureKit topology comparison."+ ],+ "postRollSeconds" : 0.6,+ "preRollSeconds" : 0.3,+ "renderVolume" : 0.5,+ "runs" : [+ {+ "captureWindow" : {+ "contributingSegmentCount" : 36,+ "coverage" : 0.9318366228070175,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 150064.272264875,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 169967+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : true,+ "appliedBypass" : false,+ "readbackAGCEnabled" : true,+ "readbackAGCRawValue" : 1,+ "readbackAGCStatus" : 0,+ "readbackBypass" : false,+ "readbackBypassRawValue" : 0,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : true,+ "requestedBypass" : false+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 150064.572264875,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioActive"+ },+ {+ "captureWindow" : {+ "contributingSegmentCount" : 36,+ "coverage" : 0.9311458333333333,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 150069.27489570837,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 169841+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : false,+ "appliedBypass" : false,+ "readbackAGCEnabled" : false,+ "readbackAGCRawValue" : 0,+ "readbackAGCStatus" : 0,+ "readbackBypass" : false,+ "readbackBypassRawValue" : 0,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : false,+ "requestedBypass" : false+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 150069.57489570836,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioActiveNoAGC"+ },+ {+ "captureWindow" : {+ "contributingSegmentCount" : 36,+ "coverage" : 0.9276480263157895,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 150074.2881927917,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 169203+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : true,+ "appliedBypass" : true,+ "readbackAGCEnabled" : true,+ "readbackAGCRawValue" : 1,+ "readbackAGCStatus" : 0,+ "readbackBypass" : true,+ "readbackBypassRawValue" : 1,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : true,+ "requestedBypass" : true+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 150074.58819279168,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioBypassed"+ }+ ],+ "schemaVersion" : 1,+ "settleSeconds" : 0.35,+ "stimulus" : {+ "chirpEndHz" : 7800,+ "chirpFrameCount" : 48000,+ "chirpStartHz" : 180,+ "gapFrameCount" : 9600,+ "leadingSilenceFrameCount" : 9600,+ "measuredPeak" : 0.25,+ "measuredRMS" : 0.09035877883434296,+ "safetyMaximumRMS" : 0.15000000596046448,+ "safetyMinimumRMS" : 0.019999999552965164,+ "safetyPeakLimit" : 0.3499999940395355,+ "sampleRate" : 48000,+ "schemaVersion" : 1,+ "seed" : 6853810188858556417,+ "speechF0EndHz" : 145,+ "speechF0StartHz" : 115,+ "speechLikeFrameCount" : 57600,+ "targetPeak" : 0.25,+ "totalFrameCount" : 139200,+ "trailingSilenceFrameCount" : 14400,+ "withinSafetyBounds" : true+ }+}diff --git a/tools/meeting_vpio_acoustic_trial_b_builtin_rerun2_2026-09-10.json b/tools/meeting_vpio_acoustic_trial_b_builtin_rerun2_2026-09-10.jsonnew file mode 100644index 00000000..72d888b4--- /dev/null+++ b/tools/meeting_vpio_acoustic_trial_b_builtin_rerun2_2026-09-10.json@@ -0,0 +1,320 @@+{+ "caveats" : [+ "Trial B renders known local PCM through this process's own VPIO output path; it does not route another application's audio and says nothing about whether Apple's echo reference receives other-process playback.",+ "attenuationDB compares an electrical reference against an acoustic capture and is uncalibrated. Only differences between variants measured back-to-back on one unchanged rig are readable, and even those are confounded by speaker level, room, and AGC.",+ "A single run establishes neither echo cancellation nor its absence. Treat every number as configuration and coupling evidence pending the paired ScreenCaptureKit topology comparison."+ ],+ "postRollSeconds" : 0.6,+ "preRollSeconds" : 0.3,+ "renderVolume" : 0.5,+ "runs" : [+ {+ "captureWindow" : {+ "contributingSegmentCount" : 38,+ "coverage" : 0.984468201754386,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 150637.13146808336,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 179567+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : true,+ "appliedBypass" : false,+ "readbackAGCEnabled" : true,+ "readbackAGCRawValue" : 1,+ "readbackAGCStatus" : 0,+ "readbackBypass" : false,+ "readbackBypassRawValue" : 0,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : true,+ "requestedBypass" : false+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 150637.63146808336,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioActive"+ },+ {+ "captureWindow" : {+ "contributingSegmentCount" : 38,+ "coverage" : 0.9781578947368421,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 150642.155443125,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 178416+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : false,+ "appliedBypass" : false,+ "readbackAGCEnabled" : false,+ "readbackAGCRawValue" : 0,+ "readbackAGCStatus" : 0,+ "readbackBypass" : false,+ "readbackBypassRawValue" : 0,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : false,+ "requestedBypass" : false+ },+ "reasons" : [+ "captureGap",+ "captureWindowIncomplete"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 150642.655443125,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioActiveNoAGC"+ },+ {+ "captureWindow" : {+ "contributingSegmentCount" : 39,+ "coverage" : 1,+ "frameCount" : 182400,+ "overlapFrameCount" : 0,+ "resyncedSegmentCount" : 0,+ "startHostSeconds" : 150647.158084375,+ "synthesizedFrameCount" : 0,+ "writtenFrameCount" : 182400+ },+ "control" : {+ "applied" : true,+ "appliedAGCEnabled" : true,+ "appliedBypass" : true,+ "readbackAGCEnabled" : true,+ "readbackAGCRawValue" : 1,+ "readbackAGCStatus" : 0,+ "readbackBypass" : true,+ "readbackBypassRawValue" : 1,+ "readbackBypassStatus" : 0,+ "requestedAGCEnabled" : true,+ "requestedBypass" : true+ },+ "measurement" : {+ "analysisFrameCount" : 114970,+ "analysisStartFrame" : 26570,+ "attenuationDB" : 51.67431417015087,+ "attenuationUncertaintyDB" : 4.129742150956794,+ "bands" : [+ {+ "attenuationDB" : 51.72175227690992,+ "binCount" : 5,+ "capturedEnergyFraction" : 0.2118404211751727,+ "coherence" : 0.07052537638508652,+ "coherent" : false,+ "frameCount" : 223,+ "highHz" : 400,+ "lowHz" : 180,+ "reasons" : [+ "bandCoherenceBelowFloor"+ ],+ "referenceEnergyFraction" : 0.19497064849912396,+ "supportFraction" : 0.7130044843049327,+ "supported" : true,+ "supportedCellCount" : 795+ },+ {+ "attenuationDB" : 54.44255103269122,+ "binCount" : 11,+ "capturedEnergyFraction" : 0.14465508528394677,+ "coherence" : 0.17504443829361027,+ "coherent" : false,+ "frameCount" : 223,+ "highHz" : 900,+ "lowHz" : 400,+ "reasons" : [+ "bandCoherenceBelowFloor"+ ],+ "referenceEnergyFraction" : 0.24910014839465808,+ "supportFraction" : 0.7276803913575214,+ "supported" : true,+ "supportedCellCount" : 1785+ },+ {+ "attenuationDB" : 55.857771433088324,+ "binCount" : 19,+ "capturedEnergyFraction" : 0.07163047360368306,+ "coherence" : 0.2851466393895651,+ "coherent" : false,+ "frameCount" : 223,+ "highHz" : 1800,+ "lowHz" : 900,+ "reasons" : [+ "bandCoherenceBelowFloor"+ ],+ "referenceEnergyFraction" : 0.1708677667574621,+ "supportFraction" : 0.6676894028793958,+ "supported" : true,+ "supportedCellCount" : 2829+ },+ {+ "attenuationDB" : 54.705440476719,+ "binCount" : 38,+ "capturedEnergyFraction" : 0.09306935749061682,+ "coherence" : 0.8934269210718394,+ "coherent" : true,+ "frameCount" : 223,+ "highHz" : 3600,+ "lowHz" : 1800,+ "reasons" : [++ ],+ "referenceEnergyFraction" : 0.17026913930966597,+ "supportFraction" : 0.5168751475100307,+ "supported" : true,+ "supportedCellCount" : 4380+ },+ {+ "binCount" : 90,+ "capturedEnergyFraction" : 0.09997534815634505,+ "coherence" : 0.911874959696126,+ "coherent" : false,+ "frameCount" : 223,+ "highHz" : 7800,+ "lowHz" : 3600,+ "reasons" : [+ "bandSupportBelowThreshold"+ ],+ "referenceEnergyFraction" : 0.18694681849886505,+ "supportFraction" : 0.13477827603388143,+ "supported" : false,+ "supportedCellCount" : 2705+ }+ ],+ "capturedFrameCount" : 182400,+ "capturedNoiseFloorRMS" : 0.0001129639713326469,+ "capturedPeak" : 0.0005520954146049917,+ "capturedRMS" : 0.00012377530219964683,+ "coherentBandCount" : 1,+ "delay" : {+ "firstHalfSignedSeconds" : 2.0833333333333333e-05,+ "maxLagSeconds" : 0.5,+ "peakToMedianRatio" : 255.57161520341677,+ "quantizationSeconds" : 2.0833333333333333e-05,+ "reasons" : [+ "splitHalfDelayDisagreement"+ ],+ "resolved" : false,+ "secondHalfSignedSeconds" : 0.05285416666666667,+ "signedSeconds" : 0.05291666666666667,+ "uncertaintySeconds" : 0.026416666666666668+ },+ "reasons" : [+ "splitHalfDelayDisagreement",+ "tooFewCoherentBands"+ ],+ "referenceFrameCount" : 182400,+ "referencePeak" : 0.125,+ "referenceRMS" : 0.03946822136640549,+ "sampleRate" : 48000,+ "schemaVersion" : 1,+ "signalToNoiseFloorDB" : 1.196240784447919,+ "supportedBandCount" : 4,+ "valid" : false+ },+ "reasons" : [+ "splitHalfDelayDisagreement",+ "tooFewCoherentBands"+ ],+ "render" : {+ "combinedPeak" : 0.03125,+ "defaultOutputDeviceID" : 87,+ "mixerChannelCount" : 2,+ "mixerSampleRate" : 48000,+ "outputConnectionCount" : 1,+ "outputDeviceID" : 87,+ "outputRouteConfirmed" : true,+ "outputVolumeReadable" : true,+ "reasons" : [++ ],+ "renderStartHostSeconds" : 150647.658084375,+ "renderStartResolved" : true,+ "sampleRate" : 48000,+ "scheduledFrameCount" : 139200,+ "systemVolume" : 0.25,+ "tapFrameCount" : 139200,+ "tapInvalidHostTimeCount" : 0,+ "tapPeak" : 0.0883883461356163,+ "tapRMS" : 0.03194666943073746,+ "volume" : 0.5+ },+ "valid" : false,+ "variant" : "vpioBypassed"+ }+ ],+ "schemaVersion" : 1,+ "settleSeconds" : 0.35,+ "stimulus" : {+ "chirpEndHz" : 7800,+ "chirpFrameCount" : 48000,+ "chirpStartHz" : 180,+ "gapFrameCount" : 9600,+ "leadingSilenceFrameCount" : 9600,+ "measuredPeak" : 0.25,+ "measuredRMS" : 0.09035877883434296,+ "safetyMaximumRMS" : 0.15000000596046448,+ "safetyMinimumRMS" : 0.019999999552965164,+ "safetyPeakLimit" : 0.3499999940395355,+ "sampleRate" : 48000,+ "schemaVersion" : 1,+ "seed" : 6853810188858556417,+ "speechF0EndHz" : 145,+ "speechF0StartHz" : 115,+ "speechLikeFrameCount" : 57600,+ "targetPeak" : 0.25,+ "totalFrameCount" : 139200,+ "trailingSilenceFrameCount" : 14400,+ "withinSafetyBounds" : true+ }+}diff --git a/tools/meeting_vpio_reference_probe_builtin_2026-09-10.json b/tools/meeting_vpio_reference_probe_builtin_2026-09-10.jsonnew file mode 100644index 00000000..e4e12ecd--- /dev/null+++ b/tools/meeting_vpio_reference_probe_builtin_2026-09-10.json@@ -0,0 +1,90 @@+{+ "bypassVoiceProcessing": {+ "element": 0,+ "propertyID": 2100,+ "scope": 0,+ "status": 0,+ "value": 0+ },+ "engineRunning": true,+ "inputCurrentDevice": {+ "element": 1,+ "propertyID": 2000,+ "scope": 0,+ "status": 0,+ "value": 82+ },+ "inputNodeInputFormat": {+ "channelCount": 9,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "inputNodeLastRenderTime": {+ "hostTime": 3459905607007,+ "sampleRate": 48000,+ "sampleTime": 147066+ },+ "inputNodeOutputConnectionCount": 0,+ "inputNodeOutputFormat": {+ "channelCount": 9,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "inputPresentationLatencySeconds": 0.0005416666666666666,+ "nodeAdvancedDuckingEnabled": false,+ "nodeDuckingLevelRawValue": 10,+ "nodeVoiceProcessingAGCEnabled": true,+ "nodeVoiceProcessingBypassed": false,+ "nodeVoiceProcessingEnabled": true,+ "nodeVoiceProcessingInputMuted": false,+ "otherAudioDucking": {+ "advancedDuckingEnabled": false,+ "duckingLevelRawValue": 10,+ "element": 0,+ "propertyID": 2108,+ "scope": 0,+ "status": 0+ },+ "outputCurrentDevice": {+ "element": 0,+ "propertyID": 2000,+ "scope": 0,+ "status": 0,+ "value": 87+ },+ "outputNodeInputFormat": {+ "channelCount": 2,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "outputNodeLastRenderTime": {+ "hostTime": 3459905315008,+ "sampleRate": 48000,+ "sampleTime": 146482+ },+ "outputNodeOutputFormat": {+ "channelCount": 2,+ "commonFormatRawValue": 1,+ "interleaved": false,+ "sampleRate": 48000+ },+ "outputPresentationLatencySeconds": 0.000020833333333333333,+ "schemaVersion": 1,+ "voiceProcessingAGCEnabled": {+ "element": 0,+ "propertyID": 2101,+ "scope": 0,+ "status": 0,+ "value": 1+ },+ "voiceProcessingOutputMuted": {+ "element": 0,+ "propertyID": 2104,+ "scope": 0,+ "status": 0,+ "value": 0+ }+}diff --git a/tools/test_meeting_admission_b3_eval.py b/tools/test_meeting_admission_b3_eval.pynew file mode 100644index 00000000..5d65889e--- /dev/null+++ b/tools/test_meeting_admission_b3_eval.py@@ -0,0 +1,94 @@+import json+import tempfile+import unittest+from pathlib import Path++import meeting_admission_b3_eval as subject+++class B3EvalTests(unittest.TestCase):+ def test_label_requires_full_containment(self):+ labels = [{"start": 0, "end": 2, "label": "noNearEndSpeech"}]+ self.assertEqual(subject.label_for(0, 2, labels), "noNearEndSpeech")+ self.assertIsNone(subject.label_for(1.9, 2.1, labels))++ def test_confusion_keeps_positive_and_negative_separate(self):+ rows = [{"label": "noNearEndSpeech", "x": True},+ {"label": "noNearEndSpeech", "x": False},+ {"label": "coarseNearEndSpeechRegion", "x": True}]+ result = subject.confusion(rows, lambda row: row["x"])+ self.assertEqual(result["falseAcceptedNegativeFrames"], 1)+ self.assertEqual(result["acceptedCoarseScriptRegionFrames"], 1)++ def test_annotation_rejects_overlap(self):+ payload = {"schemaVersion": 1, "sessions": [{"sessionID": "00000000-0000-0000-0000-000000000001", "sidecar": "a.json", "intervals": [+ {"start": 0, "end": 2, "label": "noNearEndSpeech", "provenance": "a"},+ {"start": 1, "end": 3, "label": "coarseNearEndSpeechRegion", "provenance": "b"}]}]}+ with self.assertRaises(ValueError):+ subject.validate_annotations(payload)++ def test_annotation_rejects_sidecar_traversal(self):+ payload = {"schemaVersion": 1, "sessions": [{"sessionID": "00000000-0000-0000-0000-000000000001",+ "sidecar": "../outside.json", "intervals": []}]}+ with self.assertRaises(ValueError):+ subject.validate_annotations(payload)++ def test_annotation_rejects_empty_intervals(self):+ payload = {"schemaVersion": 1, "sessions": [{"sessionID": "00000000-0000-0000-0000-000000000001",+ "sidecar": "a.json", "intervals": []}]}+ with self.assertRaises(ValueError):+ subject.validate_annotations(payload)++ def test_output_never_contains_transcript_text_or_identifiers(self):+ # Exercise the counter directly: values are aggregates and sets are collapsed.+ session = {"audioTracks": [{"id": "mic-secret", "kind": "microphone"}],+ "transcriptSegments": [{"sourceTrackID": "mic-secret", "start": {"value": 0, "timescale": 1},+ "end": {"value": 1, "timescale": 1}, "text": "private words", "speakerID": "private-speaker"}]}+ result = subject.transcript_counts(session, [{"start": 0, "end": 2, "label": "noNearEndSpeech"}])+ serialized = json.dumps(result)+ self.assertNotIn("private words", serialized)+ self.assertNotIn("private-speaker", serialized)+ self.assertEqual(result["byLabel"]["noNearEndSpeech"]["visibleWords"], 2)++ def test_sweep_includes_accept_all_control_and_unique_thresholds(self):+ self.assertEqual(subject.THRESHOLDS[0], 0.0)+ self.assertEqual(len(subject.THRESHOLDS), len(set(subject.THRESHOLDS)))+ self.assertIn(0.85, subject.THRESHOLDS)+ self.assertTrue(set(subject.REPORT_THRESHOLDS).issubset(subject.THRESHOLDS))++ def test_unknown_frame_is_counted_not_silently_dropped(self):+ sidecar = {"speechFrameSamples": 4096, "speechFrameSampleRate": 16000,+ "speechFrames": [{"frame": {"start": 0, "probability": None, "unknown": "contextWarmup"},+ "temporalState": "unavailable"}]}+ rows, coverage = subject.session_rows(sidecar, [{"start": 0, "end": 1, "label": "noNearEndSpeech"}])+ self.assertEqual(rows, [])+ self.assertEqual(coverage["byLabel"]["noNearEndSpeech"]["unknownFrames"], 1)++ def test_invalid_missing_probability_is_rejected(self):+ sidecar = {"speechFrameSamples": 4096, "speechFrameSampleRate": 16000,+ "speechFrames": [{"frame": {"start": 0, "probability": None, "unknown": None},+ "temporalState": "unavailable"}]}+ with self.assertRaises(ValueError):+ subject.session_rows(sidecar, [{"start": 0, "end": 1, "label": "noNearEndSpeech"}])++ def test_straddling_transcript_is_reported(self):+ session = {"audioTracks": [{"id": "mic", "kind": "microphone"}], "transcriptSegments": [+ {"sourceTrackID": "mic", "start": {"value": 15, "timescale": 10},+ "end": {"value": 25, "timescale": 10}, "text": "two words"}]}+ result = subject.transcript_counts(session, [+ {"start": 0, "end": 2, "label": "noNearEndSpeech"},+ {"start": 2, "end": 3, "label": "coarseNearEndSpeechRegion"}])+ self.assertEqual(result["excluded"]["boundaryStraddlingSegments"], 1)+ self.assertEqual(result["excluded"]["boundaryStraddlingWords"], 2)++ def test_session_duration_uses_either_track_origin_and_latest_end(self):+ session = {"audioTracks": [{"chunks": [+ {"presentationStart": {"value": 100, "timescale": 10},+ "presentationEnd": {"value": 200, "timescale": 10}}]}, {"chunks": [+ {"presentationStart": {"value": 90, "timescale": 10},+ "presentationEnd": {"value": 210, "timescale": 10}}]}]}+ self.assertEqual(subject.session_duration(session), 12)+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/test_meeting_reliability_baseline.py b/tools/test_meeting_reliability_baseline.pynew file mode 100644index 00000000..9ceb2351--- /dev/null+++ b/tools/test_meeting_reliability_baseline.py@@ -0,0 +1,88 @@+import json+import tempfile+import unittest+from pathlib import Path++import meeting_reliability_baseline as baseline+++class BaselineTests(unittest.TestCase):+ def test_flow_plateau_is_only_sampled_lower_bound(self):+ rows = [f"[07:18:{s:02d}.000] [INFO] [MeetingLive] [live/APP] flow chunks={c} audio={a}s openUtterance=0.0s partials={p} utterances=2 drops=0 SECRET_TRANSCRIPT"+ for s, c, a, p in [(7, 2255, 45.1, 123), (12, 2506, 50.1, 123), (37, 3758, 75.2, 123), (42, 4009, 80.2, 125)]]+ result = baseline.summarize_flows(rows)+ run = result["APP"]["longestSampledNoPartialRun"]+ self.assertEqual(run["observedNoPartialSpanSeconds"], 30)+ self.assertEqual(run["partialCount"], 123)+ self.assertNotIn("SECRET", json.dumps(result))++ def test_regressed_counter_rejects_multiple_sessions(self):+ line = "[07:18:07.000] [live/APP] flow chunks={} audio=1.0s openUtterance=0.0s partials=0 utterances=0 drops=0"+ with self.assertRaises(ValueError):+ baseline.summarize_flows([line.format(10), line.format(1)])++ def test_missing_flow_is_not_zero_drops_evidence(self):+ result = baseline.summarize_flows(["private unrelated log"])+ self.assertIsNone(result["APP"]["lastCounters"])+ self.assertIsNone(result["APP"]["longestSampledNoPartialRun"])++ def test_path_traversal_and_symlink_escape(self):+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory) / "root"+ root.mkdir()+ (Path(directory) / "outside").write_text("private")+ (root / "escape").symlink_to(Path(directory) / "outside")+ for value in ("../outside", "/etc/passwd", "a//b", "./x", "a\\b", "escape"):+ with self.assertRaises(ValueError):+ baseline.safe_child(root, value)++ def test_model_hash_mismatch_remains_false(self):+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory)+ (root / "example").mkdir()+ (root / "example/model.bin").write_bytes(b"model")+ manifest = root / "manifest.json"+ manifest.write_text(json.dumps({"modelRepository": "example", "artifacts": [+ {"path": "example/model.bin", "sha256": "0" * 64}]}))+ self.assertFalse(baseline.model_manifest_inventory(manifest)["allHashesMatch"])+ self.assertTrue(baseline.model_manifest_inventory(manifest)["exactTreeMatch"])+ (root / "example/extra.bin").write_bytes(b"extra")+ self.assertFalse(baseline.model_manifest_inventory(manifest)["exactTreeMatch"])++ def test_saved_source_track_id_counts_without_emitting_text(self):+ with tempfile.TemporaryDirectory() as directory:+ path = Path(directory) / "session.json"+ path.write_text(json.dumps({"audioTracks": [{"id": "mic", "kind": "microphone", "chunks": [], "health": {"droppedSampleCount": 0}}],+ "transcriptSegments": [+ {"sourceTrackID": "mic", "speakerID": "one", "text": "PRIVATE", "isLikelyEcho": False},+ {"sourceTrackID": "mic", "speakerID": "two", "text": "PRIVATE", "isLikelyEcho": True}]}))+ result = baseline.session_inventory(path)+ self.assertEqual(result["tracks"][0]["visibleSegmentCount"], 1)+ self.assertEqual(result["tracks"][0]["echoSegmentCount"], 1)+ self.assertNotIn("PRIVATE", json.dumps(result))++ def test_missing_health_fails_closed_and_missing_speaker_is_not_a_person(self):+ with tempfile.TemporaryDirectory() as directory:+ path = Path(directory) / "session.json"+ payload = {"audioTracks": [{"id": "mic", "kind": "microphone", "chunks": []}],+ "transcriptSegments": [{"sourceTrackID": "mic"}]}+ path.write_text(json.dumps(payload))+ with self.assertRaises(ValueError):+ baseline.session_inventory(path)+ payload["audioTracks"][0]["health"] = {"droppedSampleCount": 0}+ path.write_text(json.dumps(payload))+ track = baseline.session_inventory(path)["tracks"][0]+ self.assertEqual(track["visibleSpeakerCount"], 0)+ self.assertEqual(track["missingVisibleSpeakerIDCount"], 1)++ def test_unknown_segment_schema_fails_instead_of_zero(self):+ with tempfile.TemporaryDirectory() as directory:+ path = Path(directory) / "session.json"+ path.write_text(json.dumps({"audioTracks": [{"id": "mic", "kind": "microphone", "chunks": []}],+ "transcriptSegments": [{"trackID": "mic"}]}))+ with self.assertRaises(ValueError):+ baseline.session_inventory(path)+++if __name__ == "__main__":+ unittest.main()