1use std::ffi::c_void;
38use std::collections::{HashMap, VecDeque};
39use std::sync::atomic::{AtomicBool, Ordering};
40use std::sync::{mpsc, Arc, Mutex};
41
42use rpi_plugin_sdk::{RuntimeActionId, StbString, StbStringRef};
43use tokio::runtime::Handle;
44
45#[derive(Clone, Debug, PartialEq)]
52pub struct UiDialogRequest {
53 pub request_id: String,
54 pub tool_call_id: Option<String>,
55 pub ui: serde_json::Value,
56 pub raw: serde_json::Value,
57}
58
59struct UiDialogEntry {
60 request: UiDialogRequest,
61 answer: Option<serde_json::Value>,
62 cancelled: bool,
63}
64
65struct UiDialogState {
66 attached: bool,
67 pending: VecDeque<String>,
68 active: HashMap<String, UiDialogEntry>,
69}
70
71#[derive(Clone)]
75pub struct UiDialogMailbox {
76 state: Arc<Mutex<UiDialogState>>,
77}
78
79impl Default for UiDialogMailbox {
80 fn default() -> Self {
81 Self::new()
82 }
83}
84
85impl UiDialogMailbox {
86 pub fn new() -> Self {
87 Self {
88 state: Arc::new(Mutex::new(UiDialogState {
89 attached: false,
90 pending: VecDeque::new(),
91 active: HashMap::new(),
92 })),
93 }
94 }
95
96 pub fn attach(&self) {
98 self.state.lock().expect("ui dialog mailbox poisoned").attached = true;
99 }
100
101 pub fn detach(&self) {
104 let mut state = self.state.lock().expect("ui dialog mailbox poisoned");
105 state.attached = false;
106 state.pending.clear();
107 for entry in state.active.values_mut() {
108 entry.cancelled = true;
109 entry.answer = None;
110 }
111 }
112
113 pub fn is_attached(&self) -> bool {
114 self.state
115 .lock()
116 .expect("ui dialog mailbox poisoned")
117 .attached
118 }
119
120 pub fn take_pending(&self) -> Option<UiDialogRequest> {
124 let mut state = self.state.lock().expect("ui dialog mailbox poisoned");
125 while let Some(request_id) = state.pending.pop_front() {
126 if let Some(entry) = state.active.get(&request_id) {
127 if !entry.cancelled && entry.answer.is_none() {
128 return Some(entry.request.clone());
129 }
130 }
131 }
132 None
133 }
134
135 pub fn pending_len(&self) -> usize {
136 self.state
137 .lock()
138 .expect("ui dialog mailbox poisoned")
139 .pending
140 .len()
141 }
142
143 pub fn respond(&self, request_id: &str, answer: serde_json::Value) -> Result<(), String> {
146 let mut state = self.state.lock().map_err(|_| "ui dialog mailbox poisoned".to_string())?;
147 let entry = state
148 .active
149 .get_mut(request_id)
150 .ok_or_else(|| format!("unknown UI request `{request_id}`"))?;
151 if entry.cancelled {
152 return Err(format!("UI request `{request_id}` was cancelled"));
153 }
154 if entry.answer.is_none() {
155 entry.answer = Some(answer);
156 }
157 Ok(())
158 }
159
160 pub fn cancel(&self, request_id: &str) -> Result<(), String> {
163 let mut state = self.state.lock().map_err(|_| "ui dialog mailbox poisoned".to_string())?;
164 let entry = state
165 .active
166 .get_mut(request_id)
167 .ok_or_else(|| format!("unknown UI request `{request_id}`"))?;
168 entry.cancelled = true;
169 entry.answer = None;
170 state.pending.retain(|id| id != request_id);
171 Ok(())
172 }
173
174 pub fn cancel_all(&self) {
176 let mut state = self.state.lock().expect("ui dialog mailbox poisoned");
177 state.pending.clear();
178 for entry in state.active.values_mut() {
179 entry.cancelled = true;
180 entry.answer = None;
181 }
182 }
183
184 pub fn poll(&self, request_id: &str) -> Result<serde_json::Value, String> {
187 let mut state = self.state.lock().map_err(|_| "ui dialog mailbox poisoned".to_string())?;
188 let terminal = match state.active.get(request_id) {
189 Some(entry) if entry.cancelled => Some(serde_json::json!({
190 "status": "cancelled",
191 "requestId": request_id,
192 "toolCallId": entry.request.tool_call_id,
193 })),
194 Some(entry) if entry.answer.is_some() => Some(serde_json::json!({
195 "status": "answered",
196 "requestId": request_id,
197 "toolCallId": entry.request.tool_call_id,
198 "answer": entry.answer.clone().unwrap_or(serde_json::Value::Null),
199 })),
200 Some(entry) => Some(serde_json::json!({
201 "status": "pending",
202 "requestId": request_id,
203 "toolCallId": entry.request.tool_call_id,
204 })),
205 None => None,
206 };
207 let Some(result) = terminal else {
208 return Err(format!("unknown UI request `{request_id}`"));
209 };
210 if result.get("status").and_then(serde_json::Value::as_str) != Some("pending") {
211 state.active.remove(request_id);
212 }
213 Ok(result)
214 }
215
216 pub fn handle(&self, args: serde_json::Value) -> Result<serde_json::Value, String> {
220 let op = args.get("op").and_then(serde_json::Value::as_str).unwrap_or("open");
221 let request_id = || {
222 args.get("requestId")
223 .or_else(|| args.get("request_id"))
224 .and_then(serde_json::Value::as_str)
225 .map(str::to_owned)
226 .filter(|id| !id.trim().is_empty())
227 .ok_or_else(|| "missing non-empty `requestId`".to_string())
228 };
229 match op {
230 "open" => {
231 let id = request_id()?;
232 let mut state = self.state.lock().map_err(|_| "ui dialog mailbox poisoned".to_string())?;
233 if !state.attached {
234 return Err("ask_user requires an interactive UI".to_string());
235 }
236 if let Some(entry) = state.active.get(&id) {
237 return Ok(if entry.cancelled {
238 serde_json::json!({"status":"cancelled","requestId":id})
239 } else if let Some(answer) = &entry.answer {
240 serde_json::json!({"status":"answered","requestId":id,"answer":answer})
241 } else {
242 serde_json::json!({"status":"pending","requestId":id})
243 });
244 }
245 let tool_call_id = args
246 .get("toolCallId")
247 .or_else(|| args.get("tool_call_id"))
248 .and_then(serde_json::Value::as_str)
249 .map(str::to_owned);
250 let ui = args.get("ui").cloned().unwrap_or_else(|| args.clone());
251 state.active.insert(
252 id.clone(),
253 UiDialogEntry {
254 request: UiDialogRequest {
255 request_id: id.clone(),
256 tool_call_id: tool_call_id.clone(),
257 ui,
258 raw: args,
259 },
260 answer: None,
261 cancelled: false,
262 },
263 );
264 state.pending.push_back(id.clone());
265 Ok(serde_json::json!({"status":"pending","requestId":id,"toolCallId":tool_call_id}))
266 }
267 "poll" => self.poll(&request_id()?),
268 "cancel" => {
269 let id = request_id()?;
270 self.cancel(&id)?;
271 Ok(serde_json::json!({"status":"cancelled","requestId":id}))
272 }
273 "response" | "respond" => {
274 let id = request_id()?;
275 let answer = args
276 .get("answer")
277 .or_else(|| args.get("value"))
278 .cloned()
279 .unwrap_or(serde_json::Value::Null);
280 self.respond(&id, answer)?;
281 Ok(serde_json::json!({"status":"answered","requestId":id}))
282 }
283 other => Err(format!("unknown UI dialog operation `{other}`")),
284 }
285 }
286}
287
288#[async_trait::async_trait]
303pub trait RuntimeActionHost: Send + Sync {
304 async fn send_message(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
306 async fn send_user_message(&self, args: serde_json::Value)
308 -> Result<serde_json::Value, String>;
309 async fn append_entry(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
311 async fn set_session_name(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
313 async fn get_active_tools(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
315 async fn set_active_tools(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
317 async fn set_model(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
319 async fn get_thinking_level(
321 &self,
322 args: serde_json::Value,
323 ) -> Result<serde_json::Value, String>;
324 async fn set_thinking_level(
326 &self,
327 args: serde_json::Value,
328 ) -> Result<serde_json::Value, String>;
329 async fn compact(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
331 async fn get_system_prompt(&self, args: serde_json::Value)
333 -> Result<serde_json::Value, String>;
334 async fn new_session(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
336 async fn fork(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
338 async fn navigate_tree(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
340 async fn switch_session(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
342 async fn reload(&self, args: serde_json::Value) -> Result<serde_json::Value, String>;
346
347 async fn get_cli_flag(&self, _args: serde_json::Value) -> Result<serde_json::Value, String> {
350 Err("CLI flag lookup is not configured".to_string())
351 }
352
353 async fn ui_dialog(&self, _args: serde_json::Value) -> Result<serde_json::Value, String> {
357 Err("ask_user requires an interactive UI".to_string())
358 }
359}
360
361pub struct ActionBridge {
390 pub(crate) runtime: Handle,
392 pub(crate) host: Arc<dyn RuntimeActionHost>,
394 pub(crate) reload:
396 Option<Arc<dyn Fn() -> futures::future::BoxFuture<'static, ()> + Send + Sync>>,
397 ui_dialog: UiDialogMailbox,
400 active: Arc<AtomicBool>,
403}
404
405impl ActionBridge {
406 pub fn new(runtime: Handle, host: Arc<dyn RuntimeActionHost>) -> Arc<Self> {
409 Arc::new(Self {
410 runtime,
411 host,
412 reload: None,
413 ui_dialog: UiDialogMailbox::new(),
414 active: Arc::new(AtomicBool::new(true)),
415 })
416 }
417
418 pub fn with_reload(
421 runtime: Handle,
422 host: Arc<dyn RuntimeActionHost>,
423 reload: Arc<dyn Fn() -> futures::future::BoxFuture<'static, ()> + Send + Sync>,
424 ) -> Arc<Self> {
425 Self::with_reload_and_ui(runtime, host, reload, UiDialogMailbox::new())
426 }
427
428 pub fn with_ui_dialog(
430 runtime: Handle,
431 host: Arc<dyn RuntimeActionHost>,
432 ui_dialog: UiDialogMailbox,
433 ) -> Arc<Self> {
434 Arc::new(Self {
435 runtime,
436 host,
437 reload: None,
438 ui_dialog,
439 active: Arc::new(AtomicBool::new(true)),
440 })
441 }
442
443 pub fn with_reload_and_ui(
445 runtime: Handle,
446 host: Arc<dyn RuntimeActionHost>,
447 reload: Arc<dyn Fn() -> futures::future::BoxFuture<'static, ()> + Send + Sync>,
448 ui_dialog: UiDialogMailbox,
449 ) -> Arc<Self> {
450 Arc::new(Self {
451 runtime,
452 host,
453 reload: Some(reload),
454 ui_dialog,
455 active: Arc::new(AtomicBool::new(true)),
456 })
457 }
458
459 pub fn ui_dialog_mailbox(&self) -> UiDialogMailbox {
461 self.ui_dialog.clone()
462 }
463
464 pub fn invalidate(&self) {
469 self.active.store(false, Ordering::SeqCst);
470 }
471
472 pub fn is_active(&self) -> bool {
474 self.active.load(Ordering::SeqCst)
475 }
476
477 pub fn clone_host(&self) -> Arc<dyn RuntimeActionHost> {
484 Arc::clone(&self.host)
485 }
486}
487
488#[derive(Clone)]
501pub struct ReloadMailbox {
502 tx: Arc<std::sync::Mutex<Option<tokio::sync::mpsc::UnboundedSender<()>>>>,
503}
504
505impl Default for ReloadMailbox {
506 fn default() -> Self {
507 Self {
508 tx: Arc::new(std::sync::Mutex::new(None)),
509 }
510 }
511}
512
513impl ReloadMailbox {
514 pub fn new() -> Self {
516 Self::default()
517 }
518
519 pub fn install(&self, tx: tokio::sync::mpsc::UnboundedSender<()>) {
523 *self.tx.lock().unwrap() = Some(tx);
524 }
525
526 pub fn signal(&self) -> Result<(), ()> {
531 let g = self.tx.lock().unwrap();
532 match &*g {
533 Some(tx) => {
534 let _ = tx.send(());
535 Ok(())
536 }
537 None => Err(()),
538 }
539 }
540
541 pub fn clear(&self) {
543 *self.tx.lock().unwrap() = None;
544 }
545}
546
547pub fn reload_callback_from_mailbox(
554 mailbox: ReloadMailbox,
555) -> Arc<dyn Fn() -> futures::future::BoxFuture<'static, ()> + Send + Sync> {
556 Arc::new(move || {
557 let m = mailbox.clone();
558 Box::pin(async move {
559 let _ = m.signal();
560 })
561 })
562}
563
564async fn dispatch(
575 host: &Arc<dyn RuntimeActionHost>,
576 ui_dialog: &UiDialogMailbox,
577 action: RuntimeActionId,
578 args: serde_json::Value,
579) -> Result<serde_json::Value, String> {
580 match action {
581 RuntimeActionId::SendMessage => host.send_message(args).await,
582 RuntimeActionId::SendUserMessage => host.send_user_message(args).await,
583 RuntimeActionId::AppendEntry => host.append_entry(args).await,
584 RuntimeActionId::SetSessionName => host.set_session_name(args).await,
585 RuntimeActionId::GetActiveTools => host.get_active_tools(args).await,
586 RuntimeActionId::SetActiveTools => host.set_active_tools(args).await,
587 RuntimeActionId::SetModel => host.set_model(args).await,
588 RuntimeActionId::GetThinkingLevel => host.get_thinking_level(args).await,
589 RuntimeActionId::SetThinkingLevel => host.set_thinking_level(args).await,
590 RuntimeActionId::Compact => host.compact(args).await,
591 RuntimeActionId::GetSystemPrompt => host.get_system_prompt(args).await,
592 RuntimeActionId::NewSession => host.new_session(args).await,
593 RuntimeActionId::Fork => host.fork(args).await,
594 RuntimeActionId::NavigateTree => host.navigate_tree(args).await,
595 RuntimeActionId::SwitchSession => host.switch_session(args).await,
596 RuntimeActionId::Reload => host.reload(args).await,
597 RuntimeActionId::GetCliFlag => host.get_cli_flag(args).await,
598 RuntimeActionId::UiDialog => ui_dialog.handle(args),
599 }
600}
601
602pub extern "C" fn trampoline_runtime_action(
625 action_id: u32,
626 args_json: StbStringRef,
627 out: *mut StbString,
628 user_data: *mut c_void,
629) -> i32 {
630 let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
631 run_action(
632 action_id,
633 u32::from(RuntimeActionId::UiDialog),
634 args_json,
635 out,
636 user_data,
637 )
638 }));
639 match outcome {
640 Ok(rc) => rc,
641 Err(_) => {
642 tracing::error!("runtime_action trampoline panicked — reporting host error");
646 if !out.is_null() {
647 unsafe {
648 *out = StbString::from_string(
649 r#"{"error":"runtime_action panicked"}"#.to_string(),
650 );
651 }
652 }
653 1
654 }
655 }
656}
657
658pub extern "C" fn trampoline_runtime_action_v1(
661 action_id: u32,
662 args_json: StbStringRef,
663 out: *mut StbString,
664 user_data: *mut c_void,
665) -> i32 {
666 let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
667 run_action(
668 action_id,
669 u32::from(RuntimeActionId::Reload),
670 args_json,
671 out,
672 user_data,
673 )
674 }));
675 match outcome {
676 Ok(rc) => rc,
677 Err(_) => {
678 tracing::error!("ABI v1 runtime_action trampoline panicked - reporting host error");
679 if !out.is_null() {
680 unsafe {
681 *out = StbString::from_string(
682 r#"{"error":"runtime_action panicked"}"#.to_string(),
683 );
684 }
685 }
686 1
687 }
688 }
689}
690
691fn run_action(
693 action_id: u32,
694 max_action_id: u32,
695 args_json: StbStringRef,
696 out: *mut StbString,
697 user_data: *mut c_void,
698) -> i32 {
699 let action = match RuntimeActionId::try_from(action_id) {
703 Ok(action) if action_id <= max_action_id => action,
704 Ok(_) | Err(_) => {
705 if !out.is_null() {
706 let json = serde_json::json!({
707 "error": format!("unknown runtime action id {action_id}")
708 })
709 .to_string();
710 unsafe {
714 *out = StbString::from_string(json);
715 }
716 }
717 return 2;
718 }
719 };
720
721 if user_data.is_null() {
722 return -1;
723 }
724 let bridge: &ActionBridge = unsafe { &*(user_data as *const ActionBridge) };
729
730 if !bridge.is_active() {
738 if out.is_null() {
739 return 1;
740 }
741 let json = serde_json::json!({
742 "error": "runtime_action on a stale ActionBridge (session reloaded/swapped)"
743 })
744 .to_string();
745 unsafe {
746 *out = StbString::from_string(json);
747 }
748 return 1;
749 }
750
751 let args_str = unsafe { args_json.as_str() };
754 let args: serde_json::Value = if args_str.is_empty() {
755 serde_json::Value::Object(serde_json::Map::new())
756 } else {
757 serde_json::from_str(args_str)
758 .unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::new()))
759 };
760
761 let (tx, rx) = mpsc::sync_channel::<Result<serde_json::Value, String>>(1);
766 let host = Arc::clone(&bridge.host);
770 let ui_dialog = bridge.ui_dialog.clone();
771 let reload_cb = bridge.reload.clone();
772 if action == RuntimeActionId::UiDialog {
773 std::thread::spawn(move || {
779 let _ = tx.send(ui_dialog.handle(args));
780 });
781 } else {
782 bridge.runtime.spawn(async move {
783 let r = if action == RuntimeActionId::Reload {
784 if let Some(cb) = reload_cb {
785 cb().await;
786 Ok(serde_json::Value::Null)
787 } else {
788 host.reload(args).await
789 }
790 } else {
791 dispatch(&host, &ui_dialog, action, args).await
792 };
793 let _ = tx.send(r);
796 });
797 }
798
799 let result = match rx.recv() {
800 Ok(r) => r,
801 Err(_) => {
802 return -2;
807 }
808 };
809
810 if out.is_null() {
813 return match result {
815 Ok(_) => 0,
816 Err(_) => 1,
817 };
818 }
819
820 let (rc, payload) = match result {
821 Ok(value) => {
822 let json = serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string());
823 (0, json)
824 }
825 Err(msg) => {
826 let json = serde_json::json!({ "error": msg }).to_string();
827 (1, json)
828 }
829 };
830 unsafe {
836 *out = StbString::from_string(payload);
837 }
838 rc
839}
840
841#[cfg(test)]
850mod tests {
851 use super::*;
852 use std::sync::Mutex;
853
854 struct MockHost {
859 prompt: String,
860 saw: Mutex<Vec<RuntimeActionId>>,
861 }
862
863 #[async_trait::async_trait]
864 impl RuntimeActionHost for MockHost {
865 async fn send_message(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
866 unreachable!("not under test")
867 }
868 async fn send_user_message(
869 &self,
870 _: serde_json::Value,
871 ) -> Result<serde_json::Value, String> {
872 unreachable!("not under test")
873 }
874 async fn append_entry(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
875 unreachable!("not under test")
876 }
877 async fn set_session_name(
878 &self,
879 _: serde_json::Value,
880 ) -> Result<serde_json::Value, String> {
881 unreachable!("not under test")
882 }
883 async fn get_active_tools(
884 &self,
885 _: serde_json::Value,
886 ) -> Result<serde_json::Value, String> {
887 unreachable!("not under test")
888 }
889 async fn set_active_tools(
890 &self,
891 _: serde_json::Value,
892 ) -> Result<serde_json::Value, String> {
893 unreachable!("not under test")
894 }
895 async fn set_model(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
896 unreachable!("not under test")
897 }
898 async fn get_thinking_level(
899 &self,
900 _: serde_json::Value,
901 ) -> Result<serde_json::Value, String> {
902 unreachable!("not under test")
903 }
904 async fn set_thinking_level(
905 &self,
906 _: serde_json::Value,
907 ) -> Result<serde_json::Value, String> {
908 unreachable!("not under test")
909 }
910 async fn compact(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
911 unreachable!("not under test")
912 }
913 async fn get_system_prompt(
914 &self,
915 _: serde_json::Value,
916 ) -> Result<serde_json::Value, String> {
917 self.saw
918 .lock()
919 .unwrap()
920 .push(RuntimeActionId::GetSystemPrompt);
921 Ok(serde_json::json!({ "prompt": self.prompt }))
922 }
923 async fn new_session(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
924 unreachable!("not under test")
925 }
926 async fn fork(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
927 unreachable!("not under test")
928 }
929 async fn navigate_tree(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
930 unreachable!("not under test")
931 }
932 async fn switch_session(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
933 unreachable!("not under test")
934 }
935 async fn reload(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
936 self.saw.lock().unwrap().push(RuntimeActionId::Reload);
937 Ok(serde_json::Value::Null)
938 }
939 async fn get_cli_flag(&self, _: serde_json::Value) -> Result<serde_json::Value, String> {
940 self.saw.lock().unwrap().push(RuntimeActionId::GetCliFlag);
941 Ok(serde_json::json!({ "value": true }))
942 }
943 }
944
945 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
954 async fn trampoline_round_trips_get_system_prompt() {
955 let host = Arc::new(MockHost {
956 prompt: "hello from host".to_string(),
957 saw: Mutex::new(Vec::new()),
958 });
959 let host_for_assert = Arc::clone(&host);
960 let host_dyn: Arc<dyn RuntimeActionHost> = host;
961 let runtime = tokio::runtime::Handle::current();
962 let bridge = ActionBridge::new(runtime, host_dyn);
963 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
964
965 let args_str = "{}";
967 let args_ref = StbStringRef::from_str(args_str);
968
969 let mut out = StbString::empty();
970 let rc = trampoline_runtime_action(
971 RuntimeActionId::GetSystemPrompt.into(),
972 args_ref,
973 &mut out as *mut StbString,
974 user_data,
975 );
976 assert_eq!(rc, 0, "success return code");
977
978 let json_text = out.to_string_lossy();
980 let parsed: serde_json::Value = serde_json::from_str(&json_text).expect("valid json");
981 assert_eq!(parsed["prompt"], "hello from host");
982 crate::host_free_string(out);
983
984 let saw = host_for_assert.saw.lock().unwrap().clone();
987 assert_eq!(saw, vec![RuntimeActionId::GetSystemPrompt]);
988 }
989
990 #[tokio::test]
991 async fn trampoline_null_user_data_returns_minus_one() {
992 let args_ref = StbStringRef::from_str("{}");
993 let mut out = StbString::empty();
994 let rc = trampoline_runtime_action(
995 RuntimeActionId::GetSystemPrompt.into(),
996 args_ref,
997 &mut out as *mut StbString,
998 std::ptr::null_mut(),
999 );
1000 assert_eq!(rc, -1, "null user_data ⇒ no bridge");
1001 }
1002
1003 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1004 async fn trampoline_intercepts_reload_when_bridge_has_callback() {
1005 use std::sync::atomic::{AtomicUsize, Ordering};
1009 let reload_calls = Arc::new(AtomicUsize::new(0));
1010 let reload_calls_for_cb = Arc::clone(&reload_calls);
1011 let reload: Arc<dyn Fn() -> futures::future::BoxFuture<'static, ()> + Send + Sync> =
1012 Arc::new(move || {
1013 let c = Arc::clone(&reload_calls_for_cb);
1014 Box::pin(async move {
1015 c.fetch_add(1, Ordering::SeqCst);
1016 })
1017 });
1018 let host = Arc::new(MockHost {
1019 prompt: String::new(),
1020 saw: Mutex::new(Vec::new()),
1021 });
1022 let host_for_assert = Arc::clone(&host);
1023 let host_dyn: Arc<dyn RuntimeActionHost> = host;
1024 let runtime = tokio::runtime::Handle::current();
1025 let bridge = ActionBridge::with_reload(runtime, host_dyn, reload);
1026 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
1027
1028 let args_ref = StbStringRef::from_str("{}");
1029 let mut out = StbString::empty();
1030 let rc = trampoline_runtime_action(
1031 RuntimeActionId::Reload.into(),
1032 args_ref,
1033 &mut out as *mut StbString,
1034 user_data,
1035 );
1036 assert_eq!(rc, 0);
1037 assert_eq!(reload_calls.load(Ordering::SeqCst), 1);
1039 assert!(host_for_assert.saw.lock().unwrap().is_empty());
1040 crate::host_free_string(out);
1041 }
1042
1043 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1048 async fn trampoline_rejects_stale_bridge_with_invalidate() {
1049 let host = Arc::new(MockHost {
1050 prompt: String::new(),
1051 saw: Mutex::new(Vec::new()),
1052 });
1053 let host_for_assert = Arc::clone(&host);
1054 let host_dyn: Arc<dyn RuntimeActionHost> = host;
1055 let runtime = tokio::runtime::Handle::current();
1056 let bridge = ActionBridge::new(runtime, host_dyn);
1057 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
1058
1059 bridge.invalidate();
1061 assert!(!bridge.is_active());
1062
1063 let args_ref = StbStringRef::from_str("{}");
1064 let mut out = StbString::empty();
1065 let rc = trampoline_runtime_action(
1066 RuntimeActionId::GetSystemPrompt.into(),
1067 args_ref,
1068 &mut out as *mut StbString,
1069 user_data,
1070 );
1071 assert_eq!(rc, 1, "stale bridge ⇒ error return code");
1073 let json_text = out.to_string_lossy();
1074 assert!(
1075 json_text.contains("stale"),
1076 "stale-bridge error payload: {json_text}"
1077 );
1078 crate::host_free_string(out);
1079 assert!(
1080 host_for_assert.saw.lock().unwrap().is_empty(),
1081 "host dispatch must NOT run on a stale bridge"
1082 );
1083 }
1084
1085 #[tokio::test]
1086 async fn trampoline_rejects_unknown_numeric_id_without_dispatch() {
1087 let host = Arc::new(MockHost {
1088 prompt: String::new(),
1089 saw: Mutex::new(Vec::new()),
1090 });
1091 let host_for_assert = Arc::clone(&host);
1092 let host_dyn: Arc<dyn RuntimeActionHost> = host;
1093 let bridge = ActionBridge::new(tokio::runtime::Handle::current(), host_dyn);
1094 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
1095
1096 let mut out = StbString::empty();
1097 let rc = trampoline_runtime_action(
1098 0xFFFF_FFFE,
1099 StbStringRef::from_str("{}"),
1100 &mut out,
1101 user_data,
1102 );
1103
1104 assert_eq!(rc, 2, "unknown action id must be a protocol error");
1105 let payload: serde_json::Value =
1106 serde_json::from_str(&out.to_string_lossy()).expect("structured error JSON");
1107 assert_eq!(payload["error"], "unknown runtime action id 4294967294");
1108 crate::host_free_string(out);
1109 assert!(
1110 host_for_assert.saw.lock().unwrap().is_empty(),
1111 "unknown action id must not reach host dispatch"
1112 );
1113 }
1114
1115 #[tokio::test]
1116 async fn legacy_trampoline_rejects_v2_action_without_dispatch() {
1117 let host = Arc::new(MockHost {
1118 prompt: String::new(),
1119 saw: Mutex::new(Vec::new()),
1120 });
1121 let host_for_assert = Arc::clone(&host);
1122 let host_dyn: Arc<dyn RuntimeActionHost> = host;
1123 let bridge = ActionBridge::new(tokio::runtime::Handle::current(), host_dyn);
1124 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
1125
1126 let mut out = StbString::empty();
1127 let rc = trampoline_runtime_action_v1(
1128 RuntimeActionId::GetCliFlag.into(),
1129 StbStringRef::from_str("{}"),
1130 &mut out,
1131 user_data,
1132 );
1133
1134 assert_eq!(rc, 2);
1135 let payload: serde_json::Value =
1136 serde_json::from_str(&out.to_string_lossy()).expect("structured error JSON");
1137 assert_eq!(payload["error"], "unknown runtime action id 16");
1138 crate::host_free_string(out);
1139 assert!(host_for_assert.saw.lock().unwrap().is_empty());
1140 }
1141
1142 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1143 async fn legacy_accepts_v1_action_and_v2_accepts_new_action() {
1144 let host = Arc::new(MockHost {
1145 prompt: "legacy".to_string(),
1146 saw: Mutex::new(Vec::new()),
1147 });
1148 let host_for_assert = Arc::clone(&host);
1149 let host_dyn: Arc<dyn RuntimeActionHost> = host;
1150 let bridge = ActionBridge::new(tokio::runtime::Handle::current(), host_dyn);
1151 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
1152
1153 let mut legacy_out = StbString::empty();
1154 assert_eq!(
1155 trampoline_runtime_action_v1(
1156 RuntimeActionId::GetSystemPrompt.into(),
1157 StbStringRef::from_str("{}"),
1158 &mut legacy_out,
1159 user_data,
1160 ),
1161 0
1162 );
1163 crate::host_free_string(legacy_out);
1164
1165 let mut v2_out = StbString::empty();
1166 assert_eq!(
1167 trampoline_runtime_action(
1168 RuntimeActionId::GetCliFlag.into(),
1169 StbStringRef::from_str(r#"{"name":"flag"}"#),
1170 &mut v2_out,
1171 user_data,
1172 ),
1173 0
1174 );
1175 crate::host_free_string(v2_out);
1176
1177 assert_eq!(
1178 *host_for_assert.saw.lock().unwrap(),
1179 vec![
1180 RuntimeActionId::GetSystemPrompt,
1181 RuntimeActionId::GetCliFlag
1182 ]
1183 );
1184 }
1185
1186 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1191 async fn reload_mailbox_signals_installed_receiver() {
1192 let mailbox = ReloadMailbox::new();
1193 assert!(matches!(mailbox.signal(), Err(())));
1195
1196 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
1197 mailbox.install(tx);
1198 let cb = reload_callback_from_mailbox(mailbox.clone());
1199 cb().await;
1200 assert_eq!(
1201 rx.recv().await,
1202 Some(()),
1203 "installed receiver saw the signal"
1204 );
1205 mailbox.clear();
1206 }
1207
1208 fn open_args(id: &str, tool_call: &str) -> serde_json::Value {
1211 serde_json::json!({
1212 "op": "open",
1213 "requestId": id,
1214 "toolCallId": tool_call,
1215 "ui": {"kind": "selector", "questions": [
1216 {"id": "q1", "question": "Which target?", "options": ["Linux"]}
1217 ]},
1218 })
1219 }
1220
1221 #[test]
1224 fn ui_dialog_open_requires_an_attached_ui() {
1225 let mailbox = UiDialogMailbox::new();
1226 let error = mailbox.handle(open_args("r1", "c1")).unwrap_err();
1227 assert!(error.contains("interactive UI"), "got: {error}");
1228 }
1229
1230 #[test]
1231 fn ui_dialog_open_poll_answer_round_trip() {
1232 let mailbox = UiDialogMailbox::new();
1233 mailbox.attach();
1234
1235 let opened = mailbox.handle(open_args("r1", "c1")).unwrap();
1236 assert_eq!(opened["status"], "pending");
1237 assert_eq!(opened["toolCallId"], "c1");
1238
1239 let request = mailbox.take_pending().expect("request visible to TUI");
1240 assert_eq!(request.request_id, "r1");
1241 assert_eq!(request.tool_call_id.as_deref(), Some("c1"));
1242 assert_eq!(mailbox.poll("r1").unwrap()["status"], "pending");
1244 assert_eq!(mailbox.poll("r1").unwrap()["status"], "pending");
1245
1246 mailbox
1247 .respond("r1", serde_json::json!("7890"))
1248 .expect("respond");
1249 let answered = mailbox.poll("r1").unwrap();
1250 assert_eq!(answered["status"], "answered");
1251 assert_eq!(answered["answer"], "7890");
1252 assert!(mailbox.poll("r1").is_err());
1254 }
1255
1256 #[test]
1257 fn ui_dialog_cancel_is_reported_once() {
1258 let mailbox = UiDialogMailbox::new();
1259 mailbox.attach();
1260 mailbox.handle(open_args("r1", "c1")).unwrap();
1261 mailbox.cancel("r1").unwrap();
1262 let cancelled = mailbox.poll("r1").unwrap();
1263 assert_eq!(cancelled["status"], "cancelled");
1264 assert!(mailbox.poll("r1").is_err());
1265 }
1266
1267 #[test]
1268 fn ui_dialog_concurrent_requests_do_not_cross_answers() {
1269 let mailbox = UiDialogMailbox::new();
1270 mailbox.attach();
1271 mailbox.handle(open_args("r1", "c1")).unwrap();
1272 mailbox.handle(open_args("r2", "c2")).unwrap();
1273
1274 let first = mailbox.take_pending().unwrap().request_id;
1275 let second = mailbox.take_pending().unwrap().request_id;
1276 assert_ne!(first, second);
1277
1278 mailbox.respond(&second, serde_json::json!("second")).unwrap();
1280 assert_eq!(mailbox.poll(&first).unwrap()["status"], "pending");
1281 let second_answer = mailbox.poll(&second).unwrap();
1282 assert_eq!(second_answer["answer"], "second");
1283
1284 mailbox.respond(&first, serde_json::json!("first")).unwrap();
1285 assert_eq!(mailbox.poll(&first).unwrap()["answer"], "first");
1286 }
1287
1288 #[test]
1289 fn ui_dialog_detach_cancels_outstanding() {
1290 let mailbox = UiDialogMailbox::new();
1291 mailbox.attach();
1292 mailbox.handle(open_args("r1", "c1")).unwrap();
1293 mailbox.detach();
1294 assert!(!mailbox.is_attached());
1295 assert_eq!(mailbox.pending_len(), 0);
1296 assert!(mailbox.handle(open_args("r2", "c2")).is_err());
1298 assert_eq!(mailbox.poll("r1").unwrap()["status"], "cancelled");
1299 }
1300
1301 #[test]
1302 fn ui_dialog_duplicate_open_does_not_reenqueue() {
1303 let mailbox = UiDialogMailbox::new();
1304 mailbox.attach();
1305 mailbox.handle(open_args("r1", "c1")).unwrap();
1306 let again = mailbox.handle(open_args("r1", "c1")).unwrap();
1307 assert_eq!(again["status"], "pending");
1308 assert_eq!(mailbox.pending_len(), 1);
1309 mailbox.take_pending().unwrap();
1310 assert!(mailbox.take_pending().is_none());
1311 }
1312
1313 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1317 async fn v2_dispatch_routes_ui_dialog_action_17() {
1318 let host: Arc<dyn RuntimeActionHost> = Arc::new(MockHost {
1319 prompt: String::new(),
1320 saw: Mutex::new(Vec::new()),
1321 });
1322 let bridge = ActionBridge::with_ui_dialog(
1323 tokio::runtime::Handle::current(),
1324 host,
1325 UiDialogMailbox::new(),
1326 );
1327 let user_data = Arc::as_ptr(&bridge) as *mut c_void;
1328
1329 let mut out = StbString::empty();
1330 let rc = trampoline_runtime_action(
1331 17,
1332 StbStringRef::from_str(r#"{"op":"open","requestId":"r1","ui":{}}"#),
1333 &mut out,
1334 user_data,
1335 );
1336 assert_eq!(rc, 1);
1337 let payload: serde_json::Value =
1338 serde_json::from_str(&out.to_string_lossy()).expect("structured error JSON");
1339 assert!(
1340 payload["error"]
1341 .as_str()
1342 .is_some_and(|error| error.contains("interactive UI")),
1343 "unexpected payload: {payload}"
1344 );
1345 crate::host_free_string(out);
1346
1347 let mut out = StbString::empty();
1349 assert_eq!(
1350 trampoline_runtime_action(18, StbStringRef::from_str("{}"), &mut out, user_data),
1351 2
1352 );
1353 crate::host_free_string(out);
1354 }
1355}