From 091ab50d7c7fd0be6c22f5c8826b3e7317dc4632 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 11 Jun 2026 20:01:01 +0100 Subject: [PATCH] Map Muninn Move evidence into native tracking --- docs/implementation-plan.md | 4 + docs/perfect-machine-module-library.md | 3 +- src/Mimir.BufferSmoke/Program.cs | 92 ++++++++ .../MimirMuninnMoveEvidence.cs | 210 ++++++++++++++++++ state/evidence.jsonl | 1 + state/map.yaml | 4 + 6 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 src/Mimir.Runtime/Synchronization/MimirMuninnMoveEvidence.cs diff --git a/docs/implementation-plan.md b/docs/implementation-plan.md index e8f5cba..a93c741 100644 --- a/docs/implementation-plan.md +++ b/docs/implementation-plan.md @@ -45,6 +45,10 @@ a named invariant that the native runtime cannot protect yet. - `MimirNativeReservoirRuntime` is the managed owner for that native runtime boundary. It pins Move evidence sample/descriptor batches so native reservoir handles remain valid while Mimir prepares fusion input. +- `MimirMuninnMoveEvidenceAdapter` mirrors Muninn's marker-candidate and + controller-state document schemas and normalizes them into native Move + evidence samples. It does not associate markers to controllers or synthesize + pose. - `mimir.move_controller_pose.v1` is Mimir's resolved wand pose contract. Mimir owns calibration, association, triangulation, IMU fusion, prediction, confidence, and latency accounting for those poses before Fensalir consumes diff --git a/docs/perfect-machine-module-library.md b/docs/perfect-machine-module-library.md index aa14bbb..c632593 100644 --- a/docs/perfect-machine-module-library.md +++ b/docs/perfect-machine-module-library.md @@ -14,7 +14,7 @@ job-interview answer for why it belongs in the body of the machine. | Alignment actuator | `Mimir.Runtime` + Faust | Converts sync state into bounded fractional-delay, gain, and sample-rate-offset controls; Faust moves samples. | `src/Mimir.Runtime/Synchronization/MimirAudioAlignmentActuator.cs` | | Audio actuator strategies | `Mimir.Runtime` + Faust | Integer baseline, Farrow fractional delay, variable ASRC, and hybrid delay/ASRC strategy profiles. | `src/Mimir.Runtime/Synchronization/MimirAudioActuatorConfiguration.cs` | | CultMesh contracts | `Mimir.Runtime` | Typed codebook, decoder, acoustic path, actuator, program, Move evidence, and Mimir-fused Move controller pose documents for distributed Mimir nodes. | `src/Mimir.Runtime/Synchronization/MimirCultMeshContracts.cs`, `src/Mimir.Runtime/Synchronization/MimirTrackingObservation.cs` | -| Move controller fusion | `Mimir.Runtime` + native reservoir + Fensalir | Mimir admits Muninn marker candidates plus controller/IMU/button feeds into native `move_evidence` buffers, fuses them into `mimir.move_controller_pose.v1`, and Fensalir consumes the resolved pose stream for interactive environments. | `src/Mimir.Runtime/Synchronization/MimirTrackingObservation.cs`, `src/Mimir.Runtime/Synchronization/MimirNativeReservoirRuntime.cs`, `native/reservoir/src/lib.rs` | +| Move controller fusion | `Mimir.Runtime` + native reservoir + Fensalir | Mimir admits Muninn marker candidates plus controller/IMU/button feeds into native `move_evidence` buffers, fuses them into `mimir.move_controller_pose.v1`, and Fensalir consumes the resolved pose stream for interactive environments. | `src/Mimir.Runtime/Synchronization/MimirTrackingObservation.cs`, `src/Mimir.Runtime/Synchronization/MimirMuninnMoveEvidence.cs`, `src/Mimir.Runtime/Synchronization/MimirNativeReservoirRuntime.cs`, `native/reservoir/src/lib.rs` | | Acoustic path learning | `Mimir.Runtime` | Calibration stages for usable bands, confusion, global delay, group delay, and codebook adaptation. | `src/Mimir.Runtime/Synchronization/MimirAcousticPathLearningConfiguration.cs` | | Acoustic localization | `Mimir.Runtime` + Fensalir | Pairwise TDOA, SRP-PHAT grid, sparse source, and visual-constrained localization profiles plus a small SRP/TDOA scorer. | `src/Mimir.Runtime/Synchronization/MimirAcousticLocalizationConfiguration.cs`, `src/Mimir.Runtime/Synchronization/MimirAcousticLocalizationSolver.cs` | | Benchmark panels | `Mimir.Runtime` | Decoder golf and meatspace acceptance degradation panels with receipt roots and thresholds. | `src/Mimir.Runtime/Synchronization/MimirBenchmarkPanelConfiguration.cs` | @@ -63,6 +63,7 @@ dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-contract-smoke dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --move-tracking-contract-smoke dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --move-native-reservoir-smoke --native-reservoir .\native\reservoir\target\debug\localcast_reservoir.dll +dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --muninn-move-evidence-smoke --native-reservoir .\native\reservoir\target\debug\localcast_reservoir.dll dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-manifest dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --bioacoustic-actuator-self-test ``` diff --git a/src/Mimir.BufferSmoke/Program.cs b/src/Mimir.BufferSmoke/Program.cs index c571a6d..fc3d535 100644 --- a/src/Mimir.BufferSmoke/Program.cs +++ b/src/Mimir.BufferSmoke/Program.cs @@ -102,6 +102,12 @@ if (args.Any(arg => string.Equals(arg, "--move-native-reservoir-smoke", StringCo ParseStringOption(args, "--native-reservoir", DefaultNativeReservoirPath())); } +if (args.Any(arg => string.Equals(arg, "--muninn-move-evidence-smoke", StringComparison.OrdinalIgnoreCase))) +{ + return RunMuninnMoveEvidenceSmoke( + ParseStringOption(args, "--native-reservoir", DefaultNativeReservoirPath())); +} + if (args.Any(arg => string.Equals(arg, "--import-obs-program-scene", StringComparison.OrdinalIgnoreCase))) { return await ImportObsProgramSceneAsync( @@ -1526,6 +1532,92 @@ static int RunMoveNativeReservoirSmoke(string nativeReservoirPath) : 1; } +static int RunMuninnMoveEvidenceSmoke(string nativeReservoirPath) +{ + if (!File.Exists(nativeReservoirPath)) + { + Console.Error.WriteLine($"muninn-move-evidence-smoke missing-native path={nativeReservoirPath}"); + return 1; + } + + var markers = new[] + { + new MuninnMoveMarkerCandidateDocument( + StreamId: "nightwing:ps3-eye-0:move-marker-candidates", + HostId: "nightwing", + CameraId: "ps3-eye-0", + FrameSequence: 9001, + SourceIdHash: 0xA11CE, + TileX: 19, + TileY: 7, + CenterXPx: 313.25f, + CenterYPx: 118.75f, + RadiusPx: 12.5f, + AreaPx: 491, + MeanLuma: 0.62f, + PeakLuma: 252, + Score: 0.88f, + ObservedAt: "2026-06-11T18:30:00.0010000Z"), + new MuninnMoveMarkerCandidateDocument( + StreamId: "starfire:ps3-eye-1:move-marker-candidates", + HostId: "starfire", + CameraId: "ps3-eye-1", + FrameSequence: 9002, + SourceIdHash: 0xB0B, + TileX: 11, + TileY: 10, + CenterXPx: 201.5f, + CenterYPx: 160.25f, + RadiusPx: 10.75f, + AreaPx: 363, + MeanLuma: 0.58f, + PeakLuma: 246, + Score: 0.79f, + ObservedAt: "2026-06-11T18:30:00.0020000Z") + }; + var controllers = new[] + { + new MuninnMoveControllerStateDocument( + StreamId: "nightwing:move-usb:move-controller-state", + HostId: "nightwing", + MoveId: "move-usb", + Sequence: 77, + SourceTimestampNs: 1_781_194_200_003_000_000, + AccelerometerXyz: [-0.02f, 0.11f, 0.98f], + GyroscopeXyz: [0.01f, -0.02f, 0.03f], + MagnetometerXyz: [0.0f, 0.0f, 0.0f], + TriggerValue: 0.42f, + Buttons: ["move", "trigger"], + Battery01: float.NaN, + ObservedAt: "2026-06-11T18:30:00.0030000Z") + }; + + var samples = MimirMuninnMoveEvidenceAdapter.BuildNativeSamples(markers, controllers); + using var runtime = new MimirNativeReservoirRuntime(nativeReservoirPath); + var handle = runtime.AdmitMoveEvidence( + "mimir:muninn-move-evidence-smoke", + samples, + "mimir-move-stage-calibration-v1", + "mimir-stage-space"); + var status = runtime.Status; + var optical = samples.Count(sample => sample.EvidenceKind == (uint)MimirNativeMoveEvidenceKind.OpticalMarker); + var controller = samples.Count(sample => sample.EvidenceKind == (uint)MimirNativeMoveEvidenceKind.ControllerState); + var buttonMask = samples.Single(sample => sample.EvidenceKind == (uint)MimirNativeMoveEvidenceKind.ControllerState).ButtonsMask; + + Console.WriteLine( + $"muninn-move-evidence-smoke native={nativeReservoirPath} samples={samples.Count} optical={optical} controller={controller} buttonMask=0x{buttonMask:X} payload=0x{handle.PayloadHandle:X} total={status.TotalSampleCount} moveEvidence={status.MoveEvidenceCount} edgeNs={status.EdgeNs}"); + + return samples.Count == 3 && + optical == 2 && + controller == 1 && + buttonMask == ((1u << 17) | (1u << 18)) && + handle.PayloadHandle != 0 && + status.TotalSampleCount.ToUInt64() == 1 && + status.MoveEvidenceCount.ToUInt64() == 1 + ? 0 + : 1; +} + static MoveTrackingSmokeContext BuildMoveTrackingSmokeHub() { var starfireDescriptor = new MimirStreamDescriptor( diff --git a/src/Mimir.Runtime/Synchronization/MimirMuninnMoveEvidence.cs b/src/Mimir.Runtime/Synchronization/MimirMuninnMoveEvidence.cs new file mode 100644 index 0000000..e0b3483 --- /dev/null +++ b/src/Mimir.Runtime/Synchronization/MimirMuninnMoveEvidence.cs @@ -0,0 +1,210 @@ +using GameCult.Caching; +using MessagePack; + +namespace Mimir.Runtime.Synchronization; + +[CultDocument("muninn.move_marker_candidate", "muninn.move_marker_candidate.v1")] +[MessagePackObject] +public sealed record MuninnMoveMarkerCandidateDocument( + [property: Key(0)] string StreamId, + [property: Key(1)] string HostId, + [property: Key(2)] string CameraId, + [property: Key(3)] ulong FrameSequence, + [property: Key(4)] ulong SourceIdHash, + [property: Key(5)] uint TileX, + [property: Key(6)] uint TileY, + [property: Key(7)] float CenterXPx, + [property: Key(8)] float CenterYPx, + [property: Key(9)] float RadiusPx, + [property: Key(10)] uint AreaPx, + [property: Key(11)] float MeanLuma, + [property: Key(12)] uint PeakLuma, + [property: Key(13)] float Score, + [property: Key(14)] string ObservedAt); + +[CultDocument("muninn.move_controller_state", "muninn.move_controller_state.v1")] +[MessagePackObject] +public sealed record MuninnMoveControllerStateDocument( + [property: Key(0)] string StreamId, + [property: Key(1)] string HostId, + [property: Key(2)] string MoveId, + [property: Key(3)] ulong Sequence, + [property: Key(4)] long SourceTimestampNs, + [property: Key(5)] float[] AccelerometerXyz, + [property: Key(6)] float[] GyroscopeXyz, + [property: Key(7)] float[] MagnetometerXyz, + [property: Key(8)] float TriggerValue, + [property: Key(9)] string[] Buttons, + [property: Key(10)] float Battery01, + [property: Key(11)] string ObservedAt); + +public static class MimirMuninnMoveEvidenceAdapter +{ + public static IReadOnlyList BuildNativeSamples( + IEnumerable markerCandidates, + IEnumerable controllerStates) + { + ArgumentNullException.ThrowIfNull(markerCandidates); + ArgumentNullException.ThrowIfNull(controllerStates); + var samples = new List(); + samples.AddRange(markerCandidates.Select(ToNativeSample)); + samples.AddRange(controllerStates.Select(ToNativeSample)); + return samples + .OrderBy(sample => sample.SourceTimestampNs) + .ThenBy(sample => sample.ArrivalNs) + .ThenBy(sample => sample.Sequence) + .ToArray(); + } + + public static MimirNativeMoveEvidenceSample ToNativeSample(MuninnMoveMarkerCandidateDocument marker) + { + ArgumentNullException.ThrowIfNull(marker); + var observedNs = ObservedAtToUnixNs(marker.ObservedAt); + var timestampNs = observedNs == 0 + ? checked((ulong)marker.FrameSequence) + : observedNs; + var witnessHash = marker.SourceIdHash != 0 + ? marker.SourceIdHash + : Fnva64($"{marker.HostId}:{marker.CameraId}:{marker.StreamId}"); + + return new MimirNativeMoveEvidenceSample( + WitnessIdHash: witnessHash, + ControllerIdHash: 0, + SourceTimestampNs: timestampNs, + ArrivalNs: timestampNs, + Sequence: marker.FrameSequence, + EvidenceKind: (uint)MimirNativeMoveEvidenceKind.OpticalMarker, + Flags: 0, + ImageX: marker.CenterXPx, + ImageY: marker.CenterYPx, + RadiusPx: marker.RadiusPx, + Confidence: Math.Clamp(marker.Score, 0.0f, 1.0f), + AccelX: 0.0f, + AccelY: 0.0f, + AccelZ: 0.0f, + GyroX: 0.0f, + GyroY: 0.0f, + GyroZ: 0.0f, + Trigger: 0.0f, + ButtonsMask: 0, + Reserved: 0, + Battery01: float.NaN, + Reserved1: marker.TileX, + Reserved2: marker.TileY); + } + + public static MimirNativeMoveEvidenceSample ToNativeSample(MuninnMoveControllerStateDocument controller) + { + ArgumentNullException.ThrowIfNull(controller); + var observedNs = ObservedAtToUnixNs(controller.ObservedAt); + var sourceNs = controller.SourceTimestampNs > 0 + ? checked((ulong)controller.SourceTimestampNs) + : observedNs; + var vector = ExtractVectors(controller); + + return new MimirNativeMoveEvidenceSample( + WitnessIdHash: Fnva64(controller.StreamId), + ControllerIdHash: Fnva64($"{controller.HostId}:{controller.MoveId}"), + SourceTimestampNs: sourceNs, + ArrivalNs: observedNs == 0 ? sourceNs : observedNs, + Sequence: controller.Sequence, + EvidenceKind: (uint)MimirNativeMoveEvidenceKind.ControllerState, + Flags: 0, + ImageX: float.NaN, + ImageY: float.NaN, + RadiusPx: float.NaN, + Confidence: 1.0f, + AccelX: vector.AccelX, + AccelY: vector.AccelY, + AccelZ: vector.AccelZ, + GyroX: vector.GyroX, + GyroY: vector.GyroY, + GyroZ: vector.GyroZ, + Trigger: Math.Clamp(controller.TriggerValue, 0.0f, 1.0f), + ButtonsMask: ButtonMask(controller.Buttons), + Reserved: 0, + Battery01: float.IsFinite(controller.Battery01) + ? Math.Clamp(controller.Battery01, 0.0f, 1.0f) + : float.NaN, + Reserved1: 0, + Reserved2: 0); + } + + public static ulong Fnva64(string value) + { + var hash = 14_695_981_039_346_656_037UL; + foreach (var b in System.Text.Encoding.UTF8.GetBytes(value)) + { + hash ^= b; + hash *= 1_099_511_628_211UL; + } + + return hash == 0 ? 1 : hash; + } + + private static ulong ObservedAtToUnixNs(string observedAt) + { + if (!DateTimeOffset.TryParse(observedAt, out var parsed)) + { + return 0; + } + + var secondsNs = checked((ulong)parsed.ToUnixTimeSeconds() * 1_000_000_000UL); + var tickRemainderNs = checked((ulong)(parsed.Ticks % TimeSpan.TicksPerSecond) * 100UL); + return checked(secondsNs + tickRemainderNs); + } + + private static (float AccelX, float AccelY, float AccelZ, float GyroX, float GyroY, float GyroZ) ExtractVectors( + MuninnMoveControllerStateDocument controller) + { + var accel = controller.AccelerometerXyz ?? []; + var gyro = controller.GyroscopeXyz ?? []; + return ( + Component(accel, 0), + Component(accel, 1), + Component(accel, 2), + Component(gyro, 0), + Component(gyro, 1), + Component(gyro, 2)); + } + + private static float Component(IReadOnlyList values, int index) => + index < values.Count && float.IsFinite(values[index]) ? values[index] : 0.0f; + + private static uint ButtonMask(IEnumerable? buttons) + { + var mask = 0u; + foreach (var button in buttons ?? []) + { + var bit = button.ToLowerInvariant() switch + { + "select" => 0, + "l3" => 1, + "r3" => 2, + "start" => 3, + "up" => 4, + "right" => 5, + "down" => 6, + "left" => 7, + "l2" => 8, + "r2" => 9, + "l1" => 10, + "r1" => 11, + "triangle" => 12, + "circle" => 13, + "cross" => 14, + "square" => 15, + "ps" => 16, + "move" => 17, + "trigger" => 18, + _ => -1 + }; + if (bit >= 0) + { + mask |= 1u << bit; + } + } + + return mask; + } +} diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 0ec30e9..330c4f1 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -132,3 +132,4 @@ {"ts": "2026-06-11T16:59:31+01:00", "type": "decision", "status": "ok", "note": "Move sensor fusion authority moved to Mimir: Muninn publishes source-local marker candidates and controller/IMU/button state, Odin discovers/projects schemas, Mimir owns calibration, association, triangulation, IMU fusion, prediction, confidence, latency, and mimir.move_controller_pose.v1 for Fensalir interaction."} {"ts": "2026-06-11T18:47:43+01:00", "type": "implementation", "status": "ok", "note": "Added native reservoir Move evidence admission: LocalcastMoveEvidenceSample and LocalcastMoveEvidenceBufferDescriptor define a fixed compute-upload ABI, SampleKind move_evidence enters the shared rolling window, malformed buffers are rejected before live admission, and Mimir docs/maps now route Muninn marker/controller witnesses through native buffers before Mimir pose fusion and Fensalir consumption."} {"ts": "2026-06-11T19:27:48+01:00", "type": "implementation", "status": "ok", "note": "Connected Mimir.Runtime to the native Move evidence reservoir: MimirNativeReservoirRuntime loads localcast_reservoir, pins Move evidence sample/descriptor batches, admits them through localcast_producer_push_move_evidence_buffer, and BufferSmoke --move-native-reservoir-smoke proves a marker plus controller-state batch enters the native move_evidence view."} +{"ts": "2026-06-11T19:58:56+01:00", "type": "implementation", "status": "ok", "note": "Added Mimir-side Muninn Move evidence normalization: muninn.move_marker_candidate.v1 and muninn.move_controller_state.v1 documents now map into native Move evidence samples, and BufferSmoke --muninn-move-evidence-smoke proves two optical candidates plus one controller-state record enter the native move_evidence reservoir view."} diff --git a/state/map.yaml b/state/map.yaml index 6f4fc56..51e2007 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -94,6 +94,10 @@ accepted_design: Muninn optical marker and controller-state witnesses before Mimir fusion. - `MimirNativeReservoirRuntime` loads the native reservoir DLL and pins Move evidence sample/descriptor batches so admitted native handles remain valid. + - `MimirMuninnMoveEvidenceAdapter` mirrors Muninn's + `muninn.move_marker_candidate.v1` and `muninn.move_controller_state.v1` + document shapes and maps them into native Move evidence samples without + claiming association or pose authority. - `mimir.move_controller_pose.v1` is Mimir's resolved wand pose contract for Fensalir-facing interaction. Mimir owns calibration, association, triangulation, IMU fusion, prediction, confidence, and latency accounting.