harn_vm/vm/async_builtin.rs
1use std::future::Future;
2use std::sync::Arc;
3
4use super::Vm;
5
6/// Explicit handle to the parent VM's execution context for the duration of one
7/// async-builtin call. Threaded into every async builtin by the dispatch loop
8/// (and the `#[harn_builtin]` macro), so context can no longer be "lost across a
9/// spawn boundary": a handler that needs VM access receives or clones this
10/// handle deliberately instead of reading ambient state.
11///
12/// Holds the "template" child VM that closure-invoking host helpers clone via
13/// [`AsyncBuiltinCtx::child_vm`], and whose `output` buffer collects text
14/// forwarded from VM-side closures via [`AsyncBuiltinCtx::forward_output`]. The
15/// dispatch loop drains that buffer back to the original parent VM after the
16/// async builtin returns. Cheap to clone: it is an `Arc` handle and everything
17/// heavy inside the `Vm` is shared.
18#[derive(Clone)]
19pub struct AsyncBuiltinCtx {
20 child: Arc<parking_lot::Mutex<Vm>>,
21}
22
23impl AsyncBuiltinCtx {
24 fn new(vm: Vm) -> Self {
25 Self {
26 child: Arc::new(parking_lot::Mutex::new(vm)),
27 }
28 }
29
30 /// Construct a context around `vm` for host adapters that are not themselves
31 /// async builtins but need to run VM-side closures.
32 pub fn from_vm(vm: Vm) -> Self {
33 Self::new(vm)
34 }
35
36 /// Construct a standalone ctx around `vm` for unit tests that drive an async
37 /// builtin handler directly (outside the dispatch loop). Production code
38 /// receives its ctx from the dispatch path, never this.
39 #[cfg(test)]
40 pub fn for_test(vm: Vm) -> Self {
41 Self::new(vm)
42 }
43
44 /// Clone a fresh child VM from this context. The returned `Vm` shares the
45 /// parent's heavy state, so each closure-invoking handler gets its own
46 /// cheap execution context.
47 ///
48 /// Uses the *inline* clone: this child runs while the original parent is
49 /// parked awaiting the builtin, so it inherits the parent's held-lock keys
50 /// for cross-context self-deadlock detection (HARN-ORC-011). Long-lived /
51 /// detached contexts use [`AsyncBuiltinCtx::child_ctx`] instead, which does
52 /// not inherit, since the parent keeps running there.
53 pub fn child_vm(&self) -> Vm {
54 self.child.lock().child_vm_inline()
55 }
56
57 /// Pool tasks may execute on any Tokio worker thread, so pool lookup state
58 /// is shared through the VM context rather than thread-local storage.
59 pub(crate) fn pool_registry(&self) -> Arc<crate::stdlib::pool::PoolRegistry> {
60 self.child.lock().pool_registry.clone()
61 }
62
63 pub(crate) fn wait_for_graph(&self) -> Arc<crate::wait_for_graph::VmWaitForGraph> {
64 self.child.lock().wait_for_graph.clone()
65 }
66
67 /// Create an independent context rooted at a fresh child VM. Long-lived
68 /// local tasks use this instead of sharing the parent builtin's output
69 /// buffer after the parent future has returned.
70 ///
71 /// This is a *detached* context: the new task runs independently of the
72 /// original parent, so it must NOT inherit the parent's held-lock keys
73 /// (blocking on a parent-held lock is legitimately resolvable here). Uses
74 /// the plain, non-inheriting `child_vm()` rather than `Self::child_vm`.
75 pub fn child_ctx(&self) -> Self {
76 Self::new(self.child.lock().child_vm())
77 }
78
79 /// Forward captured output from a transient child VM (typically created via
80 /// [`AsyncBuiltinCtx::child_vm`] and used to invoke a closure) back into this
81 /// context's output buffer. The dispatch loop drains that buffer back to the
82 /// original parent VM after the async builtin returns.
83 ///
84 /// Without this hook, `log()`/`__io_print()` calls inside
85 /// `post_turn_callback` closures, tool handlers, and other VM-side closures
86 /// invoked from async builtins would silently disappear because the transient
87 /// child VM's output buffer is dropped on scope exit.
88 pub fn forward_output(&self, text: &str) {
89 if text.is_empty() {
90 return;
91 }
92 self.child.lock().append_output(text);
93 }
94}
95
96/// Run an async builtin's future with `child` installed as its explicit
97/// [`AsyncBuiltinCtx`]. `make_fut` receives the ctx handle and returns the
98/// handler's future; the ctx is moved into the future, so it lives exactly as
99/// long as the call. Returns the future's output plus any output that VM-side
100/// closures forwarded into the context, which the dispatch loop appends to the
101/// real parent VM. Cancel-safe: if the returned future is dropped, the ctx +
102/// child `Vm` are dropped with it.
103pub(crate) fn run_async_builtin_with<F, M>(
104 child: Vm,
105 make_fut: M,
106) -> impl Future<Output = (F::Output, String)>
107where
108 F: Future + Send,
109 M: FnOnce(AsyncBuiltinCtx) -> F,
110{
111 // Build the context + scope synchronously so the by-value `child: Vm` moves
112 // onto the heap *before* any async state machine exists. If this were
113 // an `async fn`, the future would reserve a Vm-sized slot for `child` up to
114 // its first await, and that bloat propagates into every caller's stack
115 // frame, which can trip clippy::large_stack_frames in large dispatch
116 // functions.
117 let ctx = AsyncBuiltinCtx::new(child);
118 let registry = ctx.pool_registry();
119 let sink = Arc::clone(&ctx.child);
120 let fut = make_fut(ctx);
121 async move {
122 let output = crate::stdlib::pool::with_pool_registry_scope(registry, fut).await;
123 let captured = sink.lock().take_output();
124 (output, captured)
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131 use crate::Vm;
132
133 #[tokio::test]
134 async fn explicit_ctx_mints_child_and_captures_forwarded_output() {
135 let (present, captured) = run_async_builtin_with(Vm::new(), |ctx| async move {
136 // The handler holds the explicit ctx — no ambient lookup needed.
137 let _child = ctx.child_vm();
138 ctx.forward_output("hello ");
139 ctx.forward_output("world");
140 true
141 })
142 .await;
143 assert!(present);
144 // `forward_output` appends into the same buffer the dispatch loop drains.
145 assert_eq!(captured, "hello world");
146 }
147
148 #[tokio::test]
149 async fn child_context_has_independent_output_buffer() {
150 let (_result, captured) = run_async_builtin_with(Vm::new(), |ctx| async move {
151 let child = ctx.child_ctx();
152 child.forward_output("child");
153 ctx.forward_output("parent");
154 })
155 .await;
156 assert_eq!(captured, "parent");
157 }
158
159 #[tokio::test]
160 async fn cancelled_scope_strands_nothing() {
161 use std::future::pending;
162 // Build a future that never completes, then drop it without polling to
163 // completion. The ctx is owned by that future, so dropping it releases
164 // the child VM without any ambient cleanup.
165 let never = run_async_builtin_with(Vm::new(), |_ctx| pending::<()>());
166 drop(never);
167 }
168}