Skip to main content

EffectHandler

Trait EffectHandler 

Source
pub trait EffectHandler {
    // Required method
    fn dispatch(
        &mut self,
        kind: &str,
        op: &str,
        args: Vec<Value>,
    ) -> Result<Value, String>;

    // Provided methods
    fn note_call_budget(&mut self, _budget_cost: u64) -> Result<(), String> { ... }
    fn spawn_for_worker(&self) -> Option<Box<dyn EffectHandler + Send>> { ... }
}
Expand description

Host-side effect dispatch. Implementors decide what kind/op mean and how arguments map to side effects.

Required Methods§

Source

fn dispatch( &mut self, kind: &str, op: &str, args: Vec<Value>, ) -> Result<Value, String>

Provided Methods§

Source

fn note_call_budget(&mut self, _budget_cost: u64) -> Result<(), String>

Hook called by the VM at every function call so handlers can enforce per-call budget consumption (#225). The argument is the sum of [budget(N)] declared on the callee’s signature; the handler returns Err to refuse the call (the VM converts to VmError::Effect). Default impl is a no-op so legacy handlers and pure-only runs are unaffected.

Source

fn spawn_for_worker(&self) -> Option<Box<dyn EffectHandler + Send>>

list.par_map worker-handler factory (#305 slice 2).

Each parallel worker thread runs its own Vm and therefore needs its own effect handler. The parent handler may opt in to per-worker dispatch by returning Some(handler) here; returning None (the default) keeps slice-1 behavior: the worker runs DenyAllEffects and any effect call inside the closure fails with VmError::Effect.

The returned handler must be Send so the worker can take ownership across a thread boundary. Shared state (budget pool, chat registry, etc.) is wired up by the implementer. Per-worker independence (MCP client cache, output sink) is intentional — the alternative is mutex-serialization of the whole effect dispatch, which would defeat the parallelism.

Implementors§