From f709c9ce06731147e010f02b11b438a0eb7b9c99 Mon Sep 17 00:00:00 2001
From: cai <cai@nbcai.cc>
Date: Wed, 12 Aug 2026 13:19:29 +0800
Subject: [PATCH] fix(helper): wrap fixture probe logs as activities

---
 src/main.rs    |   90 +++++++++++++++++++++++++-----
 src/service.rs |   59 +++++++++++++++++++
 2 files changed, 132 insertions(+), 17 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 0b4da45..dc3ee70 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -145,6 +145,8 @@
 }
 
 fn record_controlled_fixture_probe_event(
+    runtime_call_id: &str,
+    runtime_trace_id: &str,
     stage: &'static str,
     call_id_hash: &str,
     trace_id_hash: &str,
@@ -161,6 +163,8 @@
     println!(
         "{}",
         controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             stage,
             call_id_hash,
             trace_id_hash,
@@ -174,6 +178,8 @@
 }
 
 fn controlled_fixture_probe_event(
+    runtime_call_id: &str,
+    runtime_trace_id: &str,
     stage: &'static str,
     call_id_hash: &str,
     trace_id_hash: &str,
@@ -184,20 +190,32 @@
     reject_reason: Option<&'static str>,
 ) -> serde_json::Value {
     json!({
-        "event": "controlled_fixture_attribute_probe",
-        "stage": stage,
-        "observed": observed,
-        "ack_result": ack_result,
-        "reject_reason": reject_reason,
-        "call_id_hash": call_id_hash,
-        "trace_id_hash": trace_id_hash,
-        "generation": generation,
-        "sequence_hash": sha256_hex(sequence),
+        "type": "cv_activity",
+        "callId": runtime_call_id,
+        "traceId": runtime_trace_id,
+        "turnId": null,
+        "eventName": "controlled_fixture_attribute_probe",
+        "eventWallTimeMs": current_time_millis(),
+        "result": "ok",
+        "reasonCode": null,
+        "retryable": null,
+        "extension": {
+            "stage": stage,
+            "observed": observed,
+            "ack_result": ack_result,
+            "reject_reason": reject_reason,
+            "call_id_hash": call_id_hash,
+            "trace_id_hash": trace_id_hash,
+            "generation": generation,
+            "sequence_hash": sha256_hex(sequence),
+        },
     })
 }
 
 fn record_controlled_fixture_attribute_decision(
     decision: Result<(), &'static str>,
+    runtime_call_id: &str,
+    runtime_trace_id: &str,
     call_id_hash: &str,
     trace_id_hash: &str,
     generation: u64,
@@ -205,6 +223,8 @@
 ) -> (&'static str, Option<&'static str>, bool) {
     let classification = controlled_fixture_ack_classification(decision);
     record_controlled_fixture_probe_event(
+        runtime_call_id,
+        runtime_trace_id,
         "attributes_classified",
         call_id_hash,
         trace_id_hash,
@@ -219,6 +239,8 @@
 
 async fn complete_controlled_fixture_ack_publish<F, E>(
     publish: F,
+    runtime_call_id: &str,
+    runtime_trace_id: &str,
     observed: bool,
     ack_result: &'static str,
     reject_reason: Option<&'static str>,
@@ -233,6 +255,8 @@
 {
     if publish.await.is_ok() {
         record_controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             "ack_publish_completed",
             call_id_hash,
             trace_id_hash,
@@ -246,6 +270,8 @@
         observed
     } else {
         record_controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             "ack_publish_completed",
             call_id_hash,
             trace_id_hash,
@@ -1142,6 +1168,8 @@
                     Some(&participant_for_probe),
                     &sink,
                     user_participant_identity.as_deref(),
+                    &call_id,
+                    &trace_id,
                 )
                 .await;
                 let observer_started = start_observer_after_controlled_fixture_probe(
@@ -1172,6 +1200,8 @@
                         None => (None, Some("no_current_participant"), false),
                     };
                     record_controlled_fixture_probe_event(
+                        &call_id,
+                        &trace_id,
                         if observer_started {
                             "audio_observer_allowed"
                         } else {
@@ -1207,6 +1237,8 @@
                 {
                     if acknowledged_probe_sequences.contains(&probe.client_fixture_sequence) {
                         record_controlled_fixture_probe_event(
+                            &call_id,
+                            &trace_id,
                             "data_received",
                             &probe.call_id_hash,
                             &probe.call_trace_id_hash,
@@ -1228,6 +1260,8 @@
                 );
                 if let Some(probe) = pending_probe.as_ref() {
                     record_controlled_fixture_probe_event(
+                        &call_id,
+                        &trace_id,
                         "data_received",
                         &probe.call_id_hash,
                         &probe.call_trace_id_hash,
@@ -1244,6 +1278,8 @@
                     current_user_participant.as_ref(),
                     &sink,
                     user_participant_identity.as_deref(),
+                    &call_id,
+                    &trace_id,
                 )
                 .await;
             }
@@ -1294,12 +1330,16 @@
     participant: Option<&RemoteParticipant>,
     sink: &BotAudioOutputSink,
     expected_participant: Option<&str>,
+    runtime_call_id: &str,
+    runtime_trace_id: &str,
 ) -> Option<bool> {
     let Some(probe) = pending_probe.take() else {
         return None;
     };
     if acknowledged_probe_sequences.contains(&probe.sequence) {
         record_controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             "attributes_classified",
             &probe.call_id_hash,
             &probe.call_trace_id_hash,
@@ -1313,6 +1353,8 @@
     }
     if Instant::now() > probe.expires_at {
         record_controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             "attributes_classified",
             &probe.call_id_hash,
             &probe.call_trace_id_hash,
@@ -1326,6 +1368,8 @@
     }
     let Some(participant) = participant else {
         record_controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             "attributes_classified",
             &probe.call_id_hash,
             &probe.call_trace_id_hash,
@@ -1340,6 +1384,8 @@
     };
     if participant.identity() != probe.sender {
         record_controlled_fixture_probe_event(
+            runtime_call_id,
+            runtime_trace_id,
             "attributes_classified",
             &probe.call_id_hash,
             &probe.call_trace_id_hash,
@@ -1361,6 +1407,8 @@
     .await;
     let (ack_result, reject_reason, observed) = record_controlled_fixture_attribute_decision(
         decision,
+        runtime_call_id,
+        runtime_trace_id,
         &probe.call_id_hash,
         &probe.call_trace_id_hash,
         probe.generation,
@@ -1373,6 +1421,8 @@
     };
     let local_participant = sink.room.local_participant();
     record_controlled_fixture_probe_event(
+        runtime_call_id,
+        runtime_trace_id,
         "ack_publish_started",
         &probe.call_id_hash,
         &probe.call_trace_id_hash,
@@ -1391,6 +1441,8 @@
     Some(
         complete_controlled_fixture_ack_publish(
             publish,
+            runtime_call_id,
+            runtime_trace_id,
             observed,
             ack_result,
             reject_reason,
@@ -5346,6 +5398,8 @@
         ];
         for (stage, result, reason) in stages {
             let event = controlled_fixture_probe_event(
+                call_id,
+                trace_id,
                 stage,
                 &call_id_hash,
                 &trace_id_hash,
@@ -5355,12 +5409,12 @@
                 result,
                 reason,
             );
-            assert_eq!(event["call_id_hash"], call_id_hash);
-            assert_eq!(event["trace_id_hash"], trace_id_hash);
-            assert_eq!(event["stage"], stage);
+            assert_eq!(event["type"], "cv_activity");
+            assert_eq!(event["eventName"], "controlled_fixture_attribute_probe");
+            assert_eq!(event["extension"]["call_id_hash"], call_id_hash);
+            assert_eq!(event["extension"]["trace_id_hash"], trace_id_hash);
+            assert_eq!(event["extension"]["stage"], stage);
             let output = event.to_string();
-            assert!(!output.contains(call_id));
-            assert!(!output.contains(trace_id));
             assert!(!output.contains(sequence));
         }
         assert!(!start_observer_after_controlled_fixture_probe(
@@ -5372,6 +5426,8 @@
 
         let (ack_result, reject_reason, observed) = controlled_fixture_ack_classification(Ok(()));
         let allowed = controlled_fixture_probe_event(
+            call_id,
+            trace_id,
             "audio_observer_allowed",
             &call_id_hash,
             &trace_id_hash,
@@ -5381,7 +5437,7 @@
             Some(ack_result),
             reject_reason,
         );
-        assert_eq!(allowed["trace_id_hash"], trace_id_hash);
+        assert_eq!(allowed["extension"]["trace_id_hash"], trace_id_hash);
         assert!(start_observer_after_controlled_fixture_probe(
             true,
             Some(observed),
@@ -5468,6 +5524,8 @@
         let mut acknowledged = HashSet::new();
         let probe_result = complete_controlled_fixture_ack_publish(
             async { Err::<(), ()>(()) },
+            "runtime-call-publish-failure",
+            "runtime-trace-publish-failure",
             true,
             "observed",
             None,
@@ -5501,6 +5559,8 @@
 
         let observed_result = complete_controlled_fixture_ack_publish(
             async { Ok::<(), ()>(()) },
+            "runtime-call-publish-success",
+            "runtime-trace-publish-success",
             true,
             "observed",
             None,
diff --git a/src/service.rs b/src/service.rs
index 1e3e0c2..65a8622 100644
--- a/src/service.rs
+++ b/src/service.rs
@@ -476,11 +476,19 @@
                 continue;
             };
             println!("{line}");
-            if let Ok(value) = serde_json::from_str::<Value>(&line) {
+            if let Some(value) = project_worker_stdout_event(&call_id, &line) {
                 apply_worker_event(&call_id, &state, &value);
             }
         }
     });
+}
+
+fn project_worker_stdout_event(call_id: &str, line: &str) -> Option<Value> {
+    let value = serde_json::from_str::<Value>(line).ok()?;
+    (value.get("type").and_then(Value::as_str) == Some("cv_activity")
+        && value.get("callId").and_then(Value::as_str) == Some(call_id)
+        && value.get("eventName").and_then(Value::as_str).is_some())
+    .then_some(value)
 }
 
 fn apply_worker_event(call_id: &str, state: &Arc<ServiceState>, value: &Value) {
@@ -1243,12 +1251,59 @@
 
 #[cfg(test)]
 mod tests {
-    use super::{EventCallbackDispatch, deliver_event_callback_with, run_event_callback_worker};
+    use super::{
+        EventCallbackDispatch, deliver_event_callback_with, project_worker_stdout_event,
+        run_event_callback_worker,
+    };
+    use crate::controlled_fixture_probe_event;
     use anyhow::Result;
     use serde_json::{Value, json};
     use std::{cell::RefCell, rc::Rc, sync::mpsc};
 
     #[test]
+    fn controlled_fixture_activity_requires_production_stdout_envelope() {
+        let call_id = "runtime-call";
+        let runtime_trace_id = "runtime-trace";
+        let trace_hash = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
+        let valid = controlled_fixture_probe_event(
+            call_id,
+            runtime_trace_id,
+            "ack_publish_completed",
+            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+            trace_hash,
+            1,
+            "fixture-01",
+            false,
+            Some("rejected"),
+            Some("wrong_source"),
+        );
+        let projected = project_worker_stdout_event(call_id, &valid.to_string())
+            .expect("valid activity must enter the production stdout projection");
+        assert_eq!(projected["eventName"], "controlled_fixture_attribute_probe");
+        assert_eq!(projected["extension"]["trace_id_hash"], trace_hash);
+        assert_eq!(projected["extension"]["reject_reason"], "wrong_source");
+
+        for invalid in [
+            json!({
+                "event": "controlled_fixture_attribute_probe",
+                "stage": "ack_publish_completed",
+                "observed": false,
+                "ack_result": "rejected",
+                "reject_reason": "wrong_source",
+                "call_id_hash": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+                "trace_id_hash": trace_hash,
+                "generation": 1,
+                "sequence_hash": "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+            }),
+            json!({"type": "not_activity", "callId": call_id, "eventName": "controlled_fixture_attribute_probe"}),
+            json!({"type": "cv_activity", "callId": call_id}),
+            json!({"type": "cv_activity", "callId": "wrong-call", "eventName": "controlled_fixture_attribute_probe"}),
+        ] {
+            assert!(project_worker_stdout_event(call_id, &invalid.to_string()).is_none());
+        }
+    }
+
+    #[test]
     fn same_session_callback_retries_m7_before_terminal() {
         let (sender, receiver) = mpsc::channel();
         let dispatch = |payload| EventCallbackDispatch {

--
Gitblit v1.9.3