diff --git a/brokkr-daemon/src/main.rs b/brokkr-daemon/src/main.rs index 48600c5..be9fbc0 100644 --- a/brokkr-daemon/src/main.rs +++ b/brokkr-daemon/src/main.rs @@ -767,7 +767,7 @@ fn run_sync_once(args: &SyncArgs) -> Result { &source.local_position, ); if !args.dry_run { - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.command_intent.v0", &format!("blender/commands/{command_id}"), &command, @@ -843,7 +843,7 @@ fn run_sync_once(args: &SyncArgs) -> Result { "scale": source.local_scale, }); if !args.dry_run { - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.command_intent.v0", &format!( "blender/commands/{}", @@ -874,7 +874,7 @@ fn run_sync_once(args: &SyncArgs) -> Result { "visible": source.active_self, }); if !args.dry_run { - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.command_intent.v0", &format!( "blender/commands/{}", @@ -924,7 +924,7 @@ fn run_sync_once(args: &SyncArgs) -> Result { &parent_name, ); if !args.dry_run { - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.command_intent.v0", &format!("blender/commands/{command_id}"), &command, @@ -961,7 +961,7 @@ fn run_sync_once(args: &SyncArgs) -> Result { material, ); if !args.dry_run { - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.command_intent.v0", &format!("blender/commands/{command_id}"), &command, @@ -1013,7 +1013,7 @@ fn run_sync_once(args: &SyncArgs) -> Result { "value": property.value, }); if !args.dry_run { - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.command_intent.v0", &format!( "blender/commands/{}", @@ -1368,8 +1368,8 @@ fn run_sync_once(args: &SyncArgs) -> Result { "sync/receipts/{}", receipt["receiptId"].as_str().unwrap_or("sync-receipt") ); - unity_store.put_json_document("brokkr.sync.receipt.v0", &receipt_key, &receipt)?; - blender_store.put_json_document("brokkr.sync.receipt.v0", &receipt_key, &receipt)?; + unity_store.put_messagepack_document("brokkr.sync.receipt.v0", &receipt_key, &receipt)?; + blender_store.put_messagepack_document("brokkr.sync.receipt.v0", &receipt_key, &receipt)?; report.receipts_written += 2; } @@ -1401,17 +1401,6 @@ impl MirrorStore { Ok(documents) } - fn put_json_document(&mut self, schema: &str, key: &str, value: &Value) -> Result<()> { - let payload = serde_json::to_vec(value)?; - self.store.push(&CultCacheEnvelope { - key: key.to_string(), - r#type: schema.to_string(), - payload, - stored_at: chrono_like_now(), - schema_id: Some(schema.to_string()), - }) - } - fn put_messagepack_document(&mut self, schema: &str, key: &str, value: &Value) -> Result<()> { let payload = rmp_serde::to_vec(value)?; self.store.push(&CultCacheEnvelope { @@ -1425,14 +1414,18 @@ impl MirrorStore { } fn decode_payload(envelope: &CultCacheEnvelope) -> Result { - serde_json::from_slice(&envelope.payload) - .or_else(|_| rmp_serde::from_slice(&envelope.payload)) - .with_context(|| { - format!( - "payload is neither MessagePack nor JSON for {}", - envelope.key - ) - }) + let mut cursor = io::Cursor::new(envelope.payload.as_slice()); + let value: Value = rmp_serde::decode::from_read(&mut cursor) + .with_context(|| format!("payload is not a MessagePack value for {}", envelope.key))?; + let trailing = envelope.payload.len() as u64 - cursor.position(); + if trailing != 0 { + return Err(anyhow!( + "payload for {} is not exactly one MessagePack value: {} trailing byte(s)", + envelope.key, + trailing + )); + } + Ok(value) } fn parse_object_binding(value: &Value) -> Option { @@ -2699,7 +2692,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -2714,7 +2707,7 @@ mod tests { }] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -2729,7 +2722,7 @@ mod tests { "authority": authority }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.var.v0", "sync/vars/var-cube-transform", &json!({ @@ -2746,7 +2739,7 @@ mod tests { }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -2772,7 +2765,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -2787,7 +2780,7 @@ mod tests { }] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -2802,7 +2795,7 @@ mod tests { "authority": "unity-to-blender" }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ "objects": [] }), @@ -2815,12 +2808,12 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ "sceneObjects": [] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -2834,7 +2827,7 @@ mod tests { "authority": "blender-to-unity" }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -2856,7 +2849,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -2883,7 +2876,7 @@ mod tests { ] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-rig", &json!({ @@ -2898,7 +2891,7 @@ mod tests { "authority": authority }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -2913,7 +2906,7 @@ mod tests { "authority": authority }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.var.v0", "sync/vars/var-cube-parent", &json!({ @@ -2929,7 +2922,7 @@ mod tests { "enabled": true }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -2963,7 +2956,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -2979,7 +2972,7 @@ mod tests { }] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -2994,7 +2987,7 @@ mod tests { "authority": authority }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.var.v0", "sync/vars/var-cube-material", &json!({ @@ -3010,7 +3003,7 @@ mod tests { "enabled": true }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -3033,7 +3026,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -3048,7 +3041,7 @@ mod tests { }] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -3063,7 +3056,7 @@ mod tests { "authority": "blender-to-unity" }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.var.v0", "sync/vars/var-cube-active", &json!({ @@ -3079,7 +3072,7 @@ mod tests { "enabled": true }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -3106,7 +3099,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -3131,7 +3124,7 @@ mod tests { }] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -3146,7 +3139,7 @@ mod tests { "authority": "unity-to-blender" }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.var.v0", "sync/vars/var-cube-component-speed", &json!({ @@ -3162,7 +3155,7 @@ mod tests { "enabled": true }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -3189,7 +3182,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.unity.host_snapshot.v0", "unity/host/current", &json!({ @@ -3204,7 +3197,7 @@ mod tests { }] }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.object_binding.v0", "sync/bindings/objects/binding-cube", &json!({ @@ -3219,7 +3212,7 @@ mod tests { "authority": "blender-to-unity" }), )?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.var.v0", "sync/vars/var-cube-custom-intensity", &json!({ @@ -3235,7 +3228,7 @@ mod tests { "enabled": true }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -3261,7 +3254,7 @@ mod tests { let mut unity_store = MirrorStore::open(unity_path)?; let mut blender_store = MirrorStore::open(blender_path)?; - unity_store.put_json_document( + unity_store.put_messagepack_document( "brokkr.sync.timeline_binding.v0", "sync/bindings/timelines/timeline-camera", &json!({ @@ -3280,7 +3273,7 @@ mod tests { "enabled": true }), )?; - blender_store.put_json_document( + blender_store.put_messagepack_document( "brokkr.blender.host_snapshot.v0", "blender/host/current", &json!({ @@ -3329,4 +3322,73 @@ mod tests { ); Ok(()) } + + #[test] + fn decode_payload_refuses_trailing_bytes() -> Result<()> { + let mut payload = rmp_serde::to_vec(&json!({"a": 1}))?; + let clean = CultCacheEnvelope { + key: "k/clean".to_string(), + r#type: "t".to_string(), + payload: payload.clone(), + stored_at: String::new(), + schema_id: None, + }; + assert_eq!(decode_payload(&clean)?, json!({"a": 1})); + + payload.push(0xc0); + let dirty = CultCacheEnvelope { + key: "k/dirty".to_string(), + payload, + ..clean + }; + let message = format!("{:#}", decode_payload(&dirty).unwrap_err()); + assert!(message.contains("k/dirty"), "{message}"); + assert!(message.contains("1 trailing byte"), "{message}"); + assert!(!message.contains("c0"), "{message}"); + Ok(()) + } + + #[test] + fn decode_payload_refuses_json_payload() -> Result<()> { + let receipt = json!({"schema": "brokkr.sync.receipt.v0", "status": "ok"}); + let envelope = CultCacheEnvelope { + key: "sync/receipts/json".to_string(), + r#type: "brokkr.sync.receipt.v0".to_string(), + payload: serde_json::to_vec(&receipt)?, + stored_at: String::new(), + schema_id: None, + }; + assert!(decode_payload(&envelope).is_err()); + Ok(()) + } + + #[test] + fn sync_once_writes_messagepack_receipts() -> Result<()> { + let temp = tempfile::tempdir()?; + let unity_path = temp.path().join("unity.ccmp"); + let blender_path = temp.path().join("blender.ccmp"); + seed_sync_stores(&unity_path, &blender_path, "unity-to-blender")?; + run_sync_once(&SyncArgs { + unity_cache: unity_path.clone(), + blender_cache: blender_path.clone(), + dry_run: false, + })?; + + for path in [&unity_path, &blender_path] { + let store = SingleFileMessagePackBackingStore::new(path); + let envelopes = store.pull_all()?; + let receipt = envelopes + .iter() + .find(|envelope| envelope.r#type == "brokkr.sync.receipt.v0") + .expect("store should hold the daemon sync receipt"); + let lead = receipt.payload.first().copied().unwrap_or(0); + assert!( + (0x80..=0x8f).contains(&lead) || lead == 0xde || lead == 0xdf, + "receipt payload should be a MessagePack map, led by {lead:#04x}" + ); + assert!(serde_json::from_slice::(&receipt.payload).is_err()); + assert!(decode_payload(receipt)?.is_object()); + } + Ok(()) + } }