Lines
100 %
Functions
96 %
Branches
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use uuid::Uuid;
use wasmtime::Engine;
use crate::lookup_log::{LookupKind, LookupLog};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScriptLimits {
pub fuel: u64,
pub max_memory_pages: u64,
}
impl Default for ScriptLimits {
fn default() -> Self {
Self {
fuel: 1_000_000,
max_memory_pages: 64,
#[derive(Debug, Clone)]
pub struct ScriptCtx {
pub user_id: Uuid,
pub limits: ScriptLimits,
lookups: Option<LookupLog>,
impl ScriptCtx {
#[must_use]
pub fn new(user_id: Uuid) -> Self {
user_id,
limits: ScriptLimits::default(),
lookups: None,
pub fn with_limits(mut self, limits: ScriptLimits) -> Self {
self.limits = limits;
self
/// Records every `get-account` / `get-commodity` argument into `log`.
pub fn with_lookup_log(mut self, log: LookupLog) -> Self {
self.lookups = Some(log);
pub(crate) fn record_lookup(&self, kind: LookupKind, key: &str) {
if let Some(log) = &self.lookups {
log.record(kind, key);
/// Cooperative interrupt signal shared between the SLYNK reader task (which
/// `interrupt()`s on `(:emacs-interrupt)`) and the eval task. Backed by a
/// monotonic generation counter rather than a boolean latch: an interrupt
/// increments the count. The session compares it with its ack watermark
/// (the highest generation already charged to a request); a higher count
/// interrupts the request that sees it, and acking it charges it there.
///
/// The generation design is deliberate (a prior boolean latch was racy): no
/// flag is left set. An interrupt is charged to exactly one request, so once
/// acked it never reaches a later one. An interrupt sent while nothing runs is
/// charged to the next request (design rule 4).
#[derive(Debug, Clone, Default)]
pub struct InterruptHandle {
generation: Arc<AtomicU64>,
impl InterruptHandle {
pub(crate) fn new() -> Self {
Self::default()
/// Signals an interrupt by advancing the generation. Idempotent in effect:
/// any request whose baseline predates this call observes the interrupt.
pub fn interrupt(&self) {
self.generation.fetch_add(1, Ordering::SeqCst);
/// The current interrupt generation. The eval task keeps an "ack"
/// watermark of the highest generation already attributed to a finished
/// request; a new interrupt is one whose generation exceeds that watermark.
pub(crate) fn generation(&self) -> u64 {
self.generation.load(Ordering::SeqCst)
/// Cooperative cancel handle for an in-flight eval. Wraps a clone of
/// the Session's [`wasmtime::Engine`]; the engine's epoch counter is
/// shared internally so a `.bump()` from any task takes effect on the
/// concurrent `func.call_async` running against the same engine.
/// Each Session sets `store.set_epoch_deadline(1)` before invoking
/// `nomi-eval`, so one bump trips the deadline and the eval traps
/// with `wasmtime::Trap::Interrupt` — surfaced upstream as
/// `EngineError::EpochInterrupt` and on the wire as
/// `(:error (:code interrupted ...))`.
#[derive(Clone, Debug)]
pub struct EpochBumper {
engine: Engine,
impl EpochBumper {
pub(crate) fn new(engine: Engine) -> Self {
Self { engine }
/// Increments the engine epoch. Returns immediately (no await).
/// Safe to call before, during, or after an eval; the next epoch
/// check inside any in-flight `call_async` traps.
pub fn bump(&self) {
self.engine.increment_epoch();
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_limits_are_non_zero() {
let limits = ScriptLimits::default();
assert!(limits.fuel > 0);
assert!(limits.max_memory_pages > 0);
fn ctx_carries_user_id() {
let id = Uuid::new_v4();
let ctx = ScriptCtx::new(id);
assert_eq!(ctx.user_id, id);
fn with_limits_overrides_default() {
let limits = ScriptLimits {
fuel: 42,
max_memory_pages: 7,
};
let ctx = ScriptCtx::new(id).with_limits(limits);
assert_eq!(ctx.limits, limits);
fn interrupt_advances_generation_monotonically() {
let h = InterruptHandle::new();
let base = h.generation();
h.interrupt();
assert_eq!(h.generation(), base + 1);
assert_eq!(h.generation(), base + 2);
fn interrupt_handle_clones_share_generation() {
let a = InterruptHandle::new();
let base = a.generation();
let b = a.clone();
b.interrupt();
assert!(a.generation() > base);