Skip to main content

rpc/
session.rs

1use std::cell::RefCell;
2use std::sync::{Arc, Mutex};
3
4use nomiscript::{
5    Compiler, Error as NomiError, Expr, HostFnSpec, Program, Reader, SymbolTable, Value,
6};
7use scripting::runtime::{
8    EngineError, EngineOpts, ModuleCache, build_engine, classify_runtime_error, decode_eval_result,
9};
10use thiserror::Error;
11use tracing::debug;
12use wasmtime::{AnyRef, Engine, Linker, Rooted, Store, Val};
13
14use crate::ctx::{EpochBumper, InterruptHandle, ScriptCtx};
15use crate::envelope::{
16    EnvelopeError, ErrorCode, Request, RequestId, Response, ResponsePayload, format_response,
17    parse_request,
18};
19
20const EPOCH_DEADLINE_TICKS: u64 = 1;
21
22/// Wasmtime Store data type for the rpc eval channel. Carries the
23/// per-session user context (`ScriptCtx`) so native fns reach
24/// `caller.data().ctx().user_id` directly. The legacy
25/// `EvalContext` capture-protocol companion field retired alongside
26/// the capture imports in P4 A6.c — `nomi-eval` now returns its
27/// final value via the function's `(ref null any)` return slot.
28///
29/// Native fns are async (`Linker::func_wrap_async`) — they `.await`
30/// `server::command::*` futures directly on whatever runtime drives the
31/// surrounding `Session::handle_form` call. No owned runtime, no
32/// `spawn_blocking`, no thread-local pool concerns.
33pub struct SessionData {
34    ctx: ScriptCtx,
35    /// Per-request stdout sink. `env.log` (PRINT/DISPLAY/NEWLINE) appends here so
36    /// the mREPL (`nms --slynk-port`) can surface script output as
37    /// `:write-string` separately from the final value. Shared with the owning
38    /// `Session`, which drains it per request. The non-mrepl paths (sshd / the
39    /// rpc text REPL) ignore it; `env.log` still tees to `tracing` for them.
40    output: Arc<Mutex<String>>,
41    /// Render-only draft accumulator. `Some` only on the restricted template
42    /// render path ([`crate::template`]); the draft natives mutate it and the
43    /// render entry point reads it back via `store.into_data()`. `None` on the
44    /// normal eval channel, where the draft natives are not even linked.
45    draft: Option<RefCell<crate::draft::TransactionDraft>>,
46}
47
48impl SessionData {
49    pub(crate) fn new(ctx: ScriptCtx, output: Arc<Mutex<String>>) -> Self {
50        Self {
51            ctx,
52            output,
53            draft: None,
54        }
55    }
56
57    /// Builds session data with a draft accumulator armed — the render path's
58    /// constructor. Draft natives require `draft` to be `Some`.
59    pub(crate) fn for_render(ctx: ScriptCtx, output: Arc<Mutex<String>>) -> Self {
60        Self {
61            ctx,
62            output,
63            draft: Some(RefCell::new(crate::draft::TransactionDraft::new())),
64        }
65    }
66
67    #[must_use]
68    pub fn ctx(&self) -> &ScriptCtx {
69        &self.ctx
70    }
71
72    /// Mutates the draft accumulator if armed (render path). Returns an error
73    /// on the normal eval path where no draft is present — a draft native
74    /// reaching a non-render Store is a wiring bug, surfaced as a trap.
75    pub fn with_draft<F>(&self, f: F) -> wasmtime::Result<()>
76    where
77        F: FnOnce(&mut crate::draft::TransactionDraft),
78    {
79        let cell = self
80            .draft
81            .as_ref()
82            .ok_or_else(|| wasmtime::Error::msg("draft native invoked outside render mode"))?;
83        f(&mut cell.borrow_mut());
84        Ok(())
85    }
86
87    /// Consumes the accumulated draft, if any. Called after a render run via
88    /// `store.into_data()`.
89    #[must_use]
90    pub fn into_draft(self) -> Option<crate::draft::TransactionDraft> {
91        self.draft.map(RefCell::into_inner)
92    }
93
94    /// Appends a script-output line to the per-request buffer. Called by the
95    /// `env.log` host fn. A poisoned lock is swallowed (output capture is
96    /// best-effort telemetry, never a reason to fail an eval).
97    pub fn push_output(&self, msg: &str) {
98        if let Ok(mut buf) = self.output.lock() {
99            buf.push_str(msg);
100        }
101    }
102}
103
104/// Per-channel evaluator. Owns mutable state across forms (defun-defined symbols
105/// persist between requests in the same session via `SymbolTable`; the wasm
106/// `ModuleCache` reuses compilations of structurally identical forms) and an
107/// interrupt handle that cooperatively short-circuits the next form on demand.
108///
109/// Eval pipeline: `nomiscript::Compiler::compile_with_mode(Eval)` emits a
110/// module exporting `nomi-eval`; the form's final value rides the function's
111/// `(ref null any)` return slot and the host walks it into an `EvalValue`
112/// via [`decode_eval_result`].
113pub struct Session {
114    ctx: ScriptCtx,
115    engine: Engine,
116    compiler: Compiler,
117    cache: ModuleCache,
118    symbols: SymbolTable,
119    interrupt: InterruptHandle,
120    /// Watermark of the highest interrupt generation already attributed to a
121    /// finished request (see [`Session::check_interrupt`] / [`Session::ack_interrupt`]).
122    /// A `C-g` counts only while `interrupt.generation() > interrupt_ack`, so one
123    /// signal aborts exactly one request and can never linger to poison a later
124    /// form.
125    interrupt_ack: u64,
126    /// Shared with each per-request `SessionData` so `env.log` output lands
127    /// where [`Session::handle_request`] can drain it.
128    output: Arc<Mutex<String>>,
129}
130
131/// A structured eval result: the captured script output plus the typed
132/// value/error payload. The SLYNK mREPL maps `output` → `:write-string` and
133/// `payload` → `:write-values` / `:evaluation-aborted`. `handle_form` (the text
134/// wire path) does not use this — it formats the payload alone.
135#[derive(Debug, Clone, PartialEq)]
136pub struct EvalOutcome {
137    pub output: String,
138    pub payload: ResponsePayload,
139}
140
141#[derive(Debug, Error)]
142pub enum SessionError {
143    #[error("engine init failed: {0}")]
144    Engine(#[from] EngineError),
145}
146
147impl Session {
148    pub fn new(ctx: ScriptCtx) -> Result<Self, SessionError> {
149        let engine = build_engine(EngineOpts::baseline().with_fuel())?;
150        let host_fns = crate::natives::all_compiler_specs();
151        let mut symbols = SymbolTable::with_builtins();
152        symbols.register_host_fns(&host_fns);
153        // Host-dependent prelude (ADR-0029): loaded only here, after the RPC
154        // host fns it calls are registered. The universal prelude already rode
155        // in via `with_builtins`.
156        crate::host_prelude::load(&mut symbols);
157        let mut session = Self {
158            ctx,
159            engine,
160            compiler: Compiler::with_host_fns(host_fns.clone()),
161            cache: ModuleCache::new(),
162            symbols,
163            interrupt: InterruptHandle::new(),
164            interrupt_ack: 0,
165            output: Arc::new(Mutex::new(String::new())),
166        };
167        // Phase 4: pre-warm the per-Session ModuleCache with the
168        // bare-call wasm for every zero-arg host fn that has a
169        // return type. The cache is keyed by full bytecode bytes so
170        // when a request like `(:id N :form (rpc-protocol-version))`
171        // lands, `cache.get_or_compile` finds the pre-compiled
172        // module and `handle_form`'s critical path is
173        // instantiate-only. Composed forms / arg-bearing calls /
174        // unseen forms still hit the cold compile path.
175        session.warm_bare_call_cache(&host_fns);
176        Ok(session)
177    }
178
179    /// Pre-compile and cache the bare-call wasm for every zero-arg
180    /// host fn whose result type is non-None. Skips no-arg fns with
181    /// `result: None` (their bare call would error at value position)
182    /// and skips arg-bearing fns (their wasm embeds the literal
183    /// args, so pre-warming would be wrong-keyed).
184    ///
185    /// Failures are silently ignored — pre-warming is an optimization,
186    /// not a correctness step. If a spec fails to compile here, the
187    /// runtime path will surface the same error when the form lands.
188    fn warm_bare_call_cache(&mut self, host_fns: &[HostFnSpec]) {
189        for spec in host_fns {
190            if !spec.params.is_empty() || spec.result.is_none() {
191                continue;
192            }
193            let form = Expr::List(vec![Expr::Symbol(spec.nomi_name.clone())]);
194            let program = Program::new(vec![form]);
195            let Ok((bytes, _ty)) = self
196                .compiler
197                .compile_eval_with_type(&program, &mut self.symbols)
198            else {
199                continue;
200            };
201            let _ = self.cache.get_or_compile(&self.engine, &bytes);
202        }
203    }
204
205    #[must_use]
206    pub fn ctx(&self) -> &ScriptCtx {
207        &self.ctx
208    }
209
210    #[must_use]
211    pub fn interrupt_handle(&self) -> InterruptHandle {
212        self.interrupt.clone()
213    }
214
215    /// Symbol names completing `prefix`, sorted and deduplicated, for the SLYNK
216    /// completion rex (`M-x sly-complete-symbol` / mREPL TAB). The reader folds
217    /// symbols with `make_ascii_uppercase` at read time, so the match uppercases
218    /// the prefix the SAME way (ASCII-only — matching the reader, not full
219    /// Unicode) and returns the canonical name (it re-reads identically). Skips
220    /// `(SETF …)` setf-place names and compiler-internal `$…` / `__…` symbols —
221    /// none are head symbols a user types. An empty `prefix` lists every
222    /// completable symbol.
223    #[must_use]
224    pub fn completions(&self, prefix: &str) -> Vec<String> {
225        let needle = prefix.to_ascii_uppercase();
226        let mut names: Vec<String> = self
227            .symbols
228            .iter()
229            .map(|(name, _)| name.as_str())
230            .filter(|name| {
231                !name.starts_with('$') && !name.starts_with("__") && !name.starts_with("(SETF")
232            })
233            .filter(|name| name.starts_with(&needle))
234            .map(str::to_owned)
235            .collect();
236        names.sort_unstable();
237        names.dedup();
238        names
239    }
240
241    /// Cooperative cancel handle for an in-flight `nomi-eval`. Clone
242    /// and hand to the transport layer: when the client sends a
243    /// cancel signal (e.g. emacs `C-g`), call `.bump()` and the
244    /// awaiting evaluation traps with `EngineError::EpochInterrupt`,
245    /// surfacing on the wire as `(:error (:code interrupted ...))`.
246    #[must_use]
247    pub fn epoch_bumper(&self) -> EpochBumper {
248        EpochBumper::new(self.engine.clone())
249    }
250
251    /// Number of pre-compiled wasm modules currently cached.
252    /// Used by tests to confirm the phase-4 pre-warm step ran and
253    /// that subsequent `handle_form` calls of cached forms don't
254    /// trigger a cold compile.
255    pub fn cache_size(&self) -> Result<usize, EngineError> {
256        self.cache.len()
257    }
258
259    pub async fn handle_form(&mut self, frame: &str) -> String {
260        let response = match self.evaluate(frame).await {
261            Ok(resp) => resp,
262            Err(err) => err.into_response(),
263        };
264        format_response(&response)
265    }
266
267    /// Structured eval of a single bare `form` (not an envelope frame): clears
268    /// the output buffer, runs the form, and returns the captured script output
269    /// alongside the typed value/error payload. The SLYNK mREPL uses this so it
270    /// can render `:write-string` (output) and `:write-values` /
271    /// `:evaluation-aborted` (payload) separately. `handle_form` is unchanged.
272    pub async fn handle_request(&mut self, source: &str) -> EvalOutcome {
273        if let Ok(mut buf) = self.output.lock() {
274            buf.clear();
275        }
276        let payload = match self.eval_source(source).await {
277            Ok(value) => ResponsePayload::Value(value),
278            Err(err) => err.into_response().payload,
279        };
280        let output = self
281            .output
282            .lock()
283            .map(|buf| buf.clone())
284            .unwrap_or_default();
285        EvalOutcome { output, payload }
286    }
287
288    /// Parses `source` to a single top-level form and evaluates it. Unlike
289    /// `handle_form`, the source is parsed to an AST directly (NOT interpolated
290    /// into an envelope string), so plist-shaped input can't hijack the
291    /// envelope: `1 :form (+ 2 3)` is rejected as "more than one form", not
292    /// silently re-read as a `(:id 1 :form …)` plist. The fixed `RequestId::Int(0)`
293    /// is unused downstream — the SLYNK layer tracks its own channel ids.
294    async fn eval_source(&mut self, source: &str) -> Result<Value, EvalFailure> {
295        let id = RequestId::Int(0);
296        let program = Reader::parse(source).map_err(|err| EvalFailure::Eval(id.clone(), err))?;
297        let mut exprs = program.exprs;
298        let form = match exprs.len() {
299            0 => return Ok(Value::Nil),
300            1 => exprs.remove(0),
301            _ => {
302                return Err(EvalFailure::Eval(
303                    id,
304                    NomiError::Compile("expected a single form".to_string()),
305                ));
306            }
307        };
308        self.eval_one_form(form).await
309    }
310
311    /// Reads `path`, evaluates every top-level form in source order (state —
312    /// `defun`s etc. — accumulates across forms, like the sshd channel), and
313    /// returns a short summary. Powers SLY's `M-x sly-load-file`
314    /// (`slynk:load-file`). Captured output across all forms is returned for the
315    /// caller to surface; a failing form aborts the load at that point.
316    pub async fn handle_file(&mut self, path: &str) -> EvalOutcome {
317        if let Ok(mut buf) = self.output.lock() {
318            buf.clear();
319        }
320        let payload = match self.load_path(path).await {
321            Ok(summary) => ResponsePayload::Value(Value::String(summary)),
322            Err(err) => err.into_response().payload,
323        };
324        let output = self
325            .output
326            .lock()
327            .map(|buf| buf.clone())
328            .unwrap_or_default();
329        EvalOutcome { output, payload }
330    }
331
332    /// Attributes all interrupts up to `observed` to the current request, so
333    /// they aren't counted again. Crucially the caller passes the SAME
334    /// generation it used to decide an interrupt is pending — never a fresh
335    /// reload — so a `C-g` that lands strictly after that observation point is
336    /// NOT folded into this request; it stays pending for the next form. The
337    /// guard keeps the watermark monotonic.
338    ///
339    /// Coalescing is intended: several `C-g`s observed together (generation
340    /// jumped by >1) cancel the one in-flight form, they do not pre-arm
341    /// cancellation of distinct future forms — that is the REPL `C-g` contract.
342    fn ack_interrupt(&mut self, observed: u64) {
343        if observed > self.interrupt_ack {
344            self.interrupt_ack = observed;
345        }
346    }
347
348    /// If an interrupt is pending, ack it (using the one generation snapshot
349    /// that decided it) and yield the interrupted error; otherwise `None`. The
350    /// single checkpoint used everywhere a form can abort on a `C-g` before the
351    /// Wasm call.
352    fn check_interrupt(&mut self, id: &RequestId) -> Option<EvalFailure> {
353        let observed = self.interrupt.generation();
354        (observed > self.interrupt_ack).then(|| {
355            self.ack_interrupt(observed);
356            EvalFailure::Interrupted(id.clone())
357        })
358    }
359
360    async fn load_path(&mut self, path: &str) -> Result<String, EvalFailure> {
361        let id = RequestId::Int(0);
362        // The synchronous file read + parse aren't covered by `run`'s checks, so
363        // guard them explicitly; a `C-g` during a later form is caught by that
364        // form's `run`, and the failing form aborts the whole load via `?`.
365        if let Some(err) = self.check_interrupt(&id) {
366            return Err(err);
367        }
368        let source = std::fs::read_to_string(path).map_err(|err| {
369            EvalFailure::Eval(
370                id.clone(),
371                NomiError::Compile(format!("cannot read {path}: {err}")),
372            )
373        })?;
374        if let Some(err) = self.check_interrupt(&id) {
375            return Err(err);
376        }
377        let program = Reader::parse(&source).map_err(|err| EvalFailure::Eval(id.clone(), err))?;
378        let count = program.exprs.len();
379        for form in program.exprs {
380            self.run(&Request {
381                id: id.clone(),
382                form,
383            })
384            .await?;
385        }
386        Ok(format!("loaded {path} ({count} forms)"))
387    }
388
389    /// Evaluates a single already-parsed form (mREPL input). The interrupt
390    /// pre-start check lives in `run`.
391    async fn eval_one_form(&mut self, form: Expr) -> Result<Value, EvalFailure> {
392        self.run(&Request {
393            id: RequestId::Int(0),
394            form,
395        })
396        .await
397    }
398
399    async fn evaluate(&mut self, frame: &str) -> Result<Response, EvalFailure> {
400        let request = parse_request(frame).map_err(EvalFailure::Envelope)?;
401        let value = self.run(&request).await?;
402        Ok(Response {
403            id: request.id,
404            payload: ResponsePayload::Value(value),
405        })
406    }
407
408    async fn run(&mut self, request: &Request) -> Result<Value, EvalFailure> {
409        debug!(user_id = %self.ctx.user_id, "evaluating form");
410        // A `C-g` that landed before this form started (pre-armed, or during an
411        // earlier form of a load) aborts here.
412        if let Some(err) = self.check_interrupt(&request.id) {
413            return Err(err);
414        }
415        let program = Program::new(vec![request.form.clone()]);
416        let (bytes, result_ty) = self
417            .compiler
418            .compile_eval_with_type(&program, &mut self.symbols)
419            .map_err(|err| EvalFailure::Eval(request.id.clone(), err))?;
420        let module = self
421            .cache
422            .get_or_compile(&self.engine, &bytes)
423            .map_err(|err| EvalFailure::Engine(request.id.clone(), err))?;
424
425        let mut linker: Linker<SessionData> = Linker::new(&self.engine);
426        crate::natives::link(&mut linker).map_err(|err| {
427            EvalFailure::Engine(
428                request.id.clone(),
429                EngineError::Instantiate(err.to_string()),
430            )
431        })?;
432
433        let mut store: Store<SessionData> = Store::new(
434            &self.engine,
435            SessionData::new(self.ctx.clone(), Arc::clone(&self.output)),
436        );
437        store.set_fuel(self.ctx.limits.fuel).map_err(|err| {
438            EvalFailure::Engine(request.id.clone(), EngineError::Fuel(err.to_string()))
439        })?;
440        store.set_epoch_deadline(EPOCH_DEADLINE_TICKS);
441
442        let instance = linker
443            .instantiate_async(&mut store, &module)
444            .await
445            .map_err(|err| EvalFailure::Engine(request.id.clone(), classify_runtime_error(&err)))?;
446        let func = instance.get_func(&mut store, "nomi-eval").ok_or_else(|| {
447            EvalFailure::Engine(
448                request.id.clone(),
449                EngineError::MissingExport("nomi-eval".into()),
450            )
451        })?;
452        // An interrupt that arrived during compile/link (the epoch bump only
453        // cancels a running call) — abort before entering Wasm.
454        if let Some(err) = self.check_interrupt(&request.id) {
455            return Err(err);
456        }
457        let mut results = [Val::AnyRef(None)];
458        let call_result = func.call_async(&mut store, &[], &mut results).await;
459        // The reader bumps the epoch AND signals the interrupt together on
460        // `(:emacs-interrupt)`. If a C-g arrived during THIS call it cancelled it
461        // (epoch trap → `call_result` is `Err`); attribute it to this request so
462        // it can't also abort the next one. We ack only on the error path, so a
463        // C-g just after a clean success stays pending for the next form. This
464        // covers every terminal exit uniformly (epoch cancel, the
465        // out-of-fuel/runtime-trap-wins-the-race case) with no per-exit cleanup.
466        //
467        // Attribution cutoff (deliberate): the ack snapshot is taken AFTER the
468        // call returns, so a C-g landing in the sub-µs window between the call
469        // returning `Err` and this load is attributed to THIS (failing) form,
470        // not the next one. That is correct REPL semantics — at that instant the
471        // failing form is still the in-flight request (its error hasn't reached
472        // the client), so the user can't yet have meant the C-g for a later
473        // form. A C-g pressed after the error is surfaced necessarily arrives
474        // after `run` returns, advancing the generation again, and the next
475        // form's pre-start `check_interrupt` catches it. The exact instant of
476        // call return is unobservable, so this boundary is irreducible, not a
477        // lost interrupt.
478        if call_result.is_err() {
479            let observed = self.interrupt.generation();
480            self.ack_interrupt(observed);
481        }
482        call_result
483            .map_err(|err| EvalFailure::Engine(request.id.clone(), classify_runtime_error(&err)))?;
484
485        let any: Option<Rooted<AnyRef>> = match &results[0] {
486            Val::AnyRef(a) => *a,
487            _ => {
488                return Err(EvalFailure::Engine(
489                    request.id.clone(),
490                    EngineError::Trap("nomi-eval did not return anyref".into()),
491                ));
492            }
493        };
494        let captured = decode_eval_result(&mut store, any, result_ty).map_err(|err| {
495            EvalFailure::Engine(
496                request.id.clone(),
497                EngineError::Trap(format!("decoding nomi-eval result: {err}")),
498            )
499        })?;
500        Ok(Value::from(captured))
501    }
502}
503
504enum EvalFailure {
505    Envelope(EnvelopeError),
506    Eval(RequestId, NomiError),
507    Engine(RequestId, EngineError),
508    Interrupted(RequestId),
509}
510
511impl EvalFailure {
512    fn into_response(self) -> Response {
513        match self {
514            EvalFailure::Envelope(err) => Response {
515                id: RequestId::Int(0),
516                payload: ResponsePayload::Error {
517                    code: envelope_error_code(&err),
518                    message: err.to_string(),
519                    detail: Some(format!("{err:?}")),
520                },
521            },
522            EvalFailure::Eval(id, err) => Response {
523                id,
524                payload: ResponsePayload::Error {
525                    code: nomi_error_code(&err),
526                    message: err.to_string(),
527                    detail: Some(format!("{err:?}")),
528                },
529            },
530            EvalFailure::Engine(id, err) => Response {
531                id,
532                payload: ResponsePayload::Error {
533                    code: engine_error_code(&err),
534                    message: err.to_string(),
535                    detail: Some(format!("{err:?}")),
536                },
537            },
538            EvalFailure::Interrupted(id) => Response {
539                id,
540                payload: ResponsePayload::Error {
541                    code: ErrorCode::new(ErrorCode::INTERRUPTED),
542                    message: "evaluation interrupted before start".into(),
543                    detail: None,
544                },
545            },
546        }
547    }
548}
549
550fn envelope_error_code(err: &EnvelopeError) -> ErrorCode {
551    let symbol = match err {
552        EnvelopeError::Parse(_) => ErrorCode::PARSE,
553        EnvelopeError::NotSingleExpr
554        | EnvelopeError::NotPlist
555        | EnvelopeError::MissingKey(_)
556        | EnvelopeError::InvalidValue(_, _) => ErrorCode::ARGS,
557    };
558    ErrorCode::new(symbol)
559}
560
561fn nomi_error_code(err: &NomiError) -> ErrorCode {
562    let symbol = match err {
563        NomiError::Parse(_) => ErrorCode::PARSE,
564        NomiError::Compile(_) | NomiError::UndefinedSymbol(_) => ErrorCode::COMPILE,
565        NomiError::Runtime(_) => ErrorCode::RUNTIME,
566        NomiError::Type { .. } | NomiError::Arity { .. } => ErrorCode::ARGS,
567    };
568    ErrorCode::new(symbol)
569}
570
571fn engine_error_code(err: &EngineError) -> ErrorCode {
572    match err {
573        EngineError::Compile(_) => ErrorCode::new(ErrorCode::COMPILE),
574        EngineError::OutOfFuel | EngineError::Trap(_) => ErrorCode::new(ErrorCode::RUNTIME),
575        EngineError::EpochInterrupt => ErrorCode::new(ErrorCode::INTERRUPTED),
576        EngineError::Instantiate(_) | EngineError::MissingExport(_) => {
577            ErrorCode::new(ErrorCode::SERVER)
578        }
579        EngineError::Fuel(_) | EngineError::Config(_) | EngineError::CachePoisoned => {
580            ErrorCode::new(ErrorCode::SERVER)
581        }
582        EngineError::NoConversion(_) => ErrorCode::new(ErrorCode::NO_CONVERSION),
583        // Commodity mismatch now arrives as a `ScriptRaised` carrying the
584        // reader-folded symbol `COMMODITY-MISMATCH` (ADR-0026): it `throw`s
585        // `$nomi_error` in-guest and the boundary wrapper bridges the uncaught
586        // throw to `__nomi_raise`. The code flows through verbatim like any
587        // script raise; the wire `:code` is the upper-cased symbol form.
588        EngineError::ScriptRaised { code, .. } => ErrorCode::new(code.clone()),
589    }
590}
591
592#[cfg(test)]
593mod tests {
594    use super::*;
595    use crate::ctx::ScriptLimits;
596    use nomiscript::{Fraction, Reader};
597
598    async fn handle_form_smoke(frame: &str) -> String {
599        let ctx = ScriptCtx::new(uuid::Uuid::nil());
600        let mut session = Session::new(ctx).expect("Session::new");
601        session.handle_form(frame).await
602    }
603
604    fn parse_to_value(input: &str) -> Result<Value, NomiError> {
605        let program = Reader::parse(input)?;
606        let mut symbols = SymbolTable::with_builtins();
607        nomiscript::eval_program(&mut symbols, &program)
608    }
609
610    #[tokio::test]
611    async fn evaluates_arithmetic_and_returns_value_envelope() {
612        let response = handle_form_smoke("(:id 1 :form (+ 1 2))").await;
613        assert_eq!(response, "(:id 1 :value 3)");
614    }
615
616    #[tokio::test]
617    async fn evaluates_nested_arithmetic() {
618        let response = handle_form_smoke("(:id 5 :form (* (+ 1 2) (- 10 4)))").await;
619        assert_eq!(response, "(:id 5 :value 18)");
620    }
621
622    #[tokio::test]
623    async fn print_in_eval_mode_does_not_panic() {
624        // Regression: PRINT / DISPLAY / NEWLINE / DEBUG lower to `env.log`,
625        // which was script-mode-only. In eval mode the missing func index used
626        // to SIGABRT the compiler (`registry.rs` `HashMap[key]`). The eval-mode
627        // `log` import + the rpc `env.log` host fn now make it compile + run.
628        let response = handle_form_smoke("(:id 1 :form (print \"hi\"))").await;
629        assert!(response.contains(":id 1"), "got: {response}");
630        assert!(!response.contains(":code"), "must not error: {response}");
631    }
632
633    #[tokio::test]
634    async fn dolist_with_print_in_eval_mode_runs() {
635        // The exact shape from the Metro script that first surfaced the panic.
636        let response = handle_form_smoke("(:id 2 :form (dolist (x (list 1 2 3)) (print x)))").await;
637        assert!(response.contains(":id 2"), "got: {response}");
638        assert!(!response.contains(":code"), "must not error: {response}");
639    }
640
641    #[tokio::test]
642    async fn handle_request_captures_output_and_value() {
643        // `(print "hi")` writes "hi" to the per-request buffer (via env.log) and
644        // its own return value is nil; the structured outcome must carry the
645        // captured text in `output` AND the value payload — the SLYNK mREPL
646        // renders these as `:write-string` + `:write-values` respectively.
647        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
648        let outcome = session.handle_request("(print \"hi\")").await;
649        assert!(
650            outcome.output.contains("hi"),
651            "captured output should contain the printed text, got: {:?}",
652            outcome.output
653        );
654        assert!(
655            matches!(outcome.payload, ResponsePayload::Value(_)),
656            "payload should be a Value, got: {:?}",
657            outcome.payload
658        );
659    }
660
661    #[tokio::test]
662    async fn handle_request_value_only_has_empty_output() {
663        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
664        let outcome = session.handle_request("(+ 1 2)").await;
665        assert!(outcome.output.is_empty(), "got: {:?}", outcome.output);
666        assert_eq!(
667            outcome.payload,
668            ResponsePayload::Value(Value::Number(Fraction::from_integer(3)))
669        );
670    }
671
672    #[tokio::test]
673    async fn handle_request_rejects_plist_shaped_injection() {
674        // Adversarial review: the source must be parsed as a standalone AST, not
675        // interpolated into a `(:id 0 :form …)` envelope string — otherwise
676        // `1 :form (+ 2 3)` would re-read as a plist and eval to `1`. It must
677        // instead error (more than one top-level form), never silently return 1.
678        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
679        let outcome = session.handle_request("1 :form (+ 2 3)").await;
680        assert!(
681            matches!(outcome.payload, ResponsePayload::Error { .. }),
682            "plist-shaped input must error, got: {:?}",
683            outcome.payload
684        );
685    }
686
687    #[tokio::test]
688    async fn handle_request_interrupt_latch_aborts_before_eval() {
689        // An interrupt latched before the request (the SLYNK reader arms it on
690        // `(:emacs-interrupt)`) must abort the eval at the pre-start check, even
691        // for a form that would otherwise compile fine.
692        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
693        session.interrupt_handle().interrupt();
694        let outcome = session.handle_request("(+ 1 2)").await;
695        match outcome.payload {
696            ResponsePayload::Error { code, .. } => {
697                assert_eq!(code.as_symbol(), ErrorCode::INTERRUPTED);
698            }
699            other => panic!("expected interrupted error, got: {other:?}"),
700        }
701    }
702
703    #[tokio::test]
704    async fn handle_file_evaluates_all_forms_and_persists_state() {
705        // A file with a defun + a call to it: state accumulates across forms,
706        // and the load returns a summary value (not the last form's value).
707        let dir = std::env::temp_dir();
708        let path = dir.join(format!("nms_load_test_{}.nms", std::process::id()));
709        std::fs::write(&path, "(defun dbl (x) (* x 2))\n(dbl 21)\n").unwrap();
710        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
711        let outcome = session.handle_file(path.to_str().unwrap()).await;
712        std::fs::remove_file(&path).ok();
713        match outcome.payload {
714            ResponsePayload::Value(Value::String(s)) => {
715                assert!(s.contains("loaded"), "summary: {s}");
716                assert!(s.contains("2 forms"), "summary: {s}");
717            }
718            other => panic!("expected a load summary string, got: {other:?}"),
719        }
720    }
721
722    #[tokio::test]
723    async fn handle_file_missing_path_errors() {
724        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
725        let outcome = session.handle_file("/no/such/nms/file.nms").await;
726        assert!(
727            matches!(outcome.payload, ResponsePayload::Error { .. }),
728            "got: {:?}",
729            outcome.payload
730        );
731    }
732
733    #[tokio::test]
734    async fn handle_file_aborts_on_a_bad_form() {
735        let dir = std::env::temp_dir();
736        let path = dir.join(format!("nms_load_bad_{}.nms", std::process::id()));
737        std::fs::write(&path, "(+ 1 2)\n(undefined-symbol-here)\n").unwrap();
738        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
739        let outcome = session.handle_file(path.to_str().unwrap()).await;
740        std::fs::remove_file(&path).ok();
741        assert!(
742            matches!(outcome.payload, ResponsePayload::Error { .. }),
743            "a bad form must abort the load, got: {:?}",
744            outcome.payload
745        );
746    }
747
748    #[tokio::test]
749    async fn handle_file_honours_interrupt_armed_before_load() {
750        // A `C-g` that lands before/during the synchronous read+parse must abort
751        // the load (the pre-read latch check), not be ignored until the first
752        // form reaches the Wasm call.
753        let dir = std::env::temp_dir();
754        let path = dir.join(format!("nms_load_intr_{}.nms", std::process::id()));
755        std::fs::write(&path, "(+ 1 2)\n(+ 3 4)\n").unwrap();
756        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
757        session.interrupt_handle().interrupt();
758        let outcome = session.handle_file(path.to_str().unwrap()).await;
759        std::fs::remove_file(&path).ok();
760        match outcome.payload {
761            ResponsePayload::Error { code, .. } => {
762                assert_eq!(code.as_symbol(), ErrorCode::INTERRUPTED, "got: {code:?}");
763            }
764            other => panic!("interrupt should abort the load, got: {other:?}"),
765        }
766    }
767
768    #[tokio::test]
769    async fn handle_request_surfaces_error_payload() {
770        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
771        let outcome = session.handle_request("does-not-exist").await;
772        assert!(
773            matches!(outcome.payload, ResponsePayload::Error { .. }),
774            "payload should be an Error, got: {:?}",
775            outcome.payload
776        );
777    }
778
779    #[tokio::test]
780    async fn handle_request_clears_output_between_calls() {
781        // The buffer must not leak across requests: a print then a pure value.
782        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
783        let _ = session.handle_request("(print \"first\")").await;
784        let second = session.handle_request("(+ 1 1)").await;
785        assert!(
786            second.output.is_empty(),
787            "output leaked from prior request: {:?}",
788            second.output
789        );
790    }
791
792    #[tokio::test]
793    async fn returns_value_for_literal_form() {
794        let response = handle_form_smoke("(:id 9 :form 42)").await;
795        assert_eq!(response, "(:id 9 :value 42)");
796    }
797
798    #[tokio::test]
799    async fn returns_value_for_string_literal() {
800        let response = handle_form_smoke("(:id 9 :form \"hello\")").await;
801        assert_eq!(response, "(:id 9 :value \"hello\")");
802    }
803
804    #[test]
805    fn round_trips_bytes_through_eval() {
806        let value = parse_to_value("'#u8(1 2 3)").unwrap();
807        assert_eq!(value, Value::Bytes(vec![1, 2, 3]));
808    }
809
810    #[tokio::test]
811    async fn bad_envelope_emits_envelope_error() {
812        let response = handle_form_smoke("(:form (+ 1 2))").await;
813        assert!(response.contains(":code args"));
814        assert!(response.contains(":id 0"));
815    }
816
817    #[tokio::test]
818    async fn malformed_envelope_emits_parse_error() {
819        let response = handle_form_smoke("(((((").await;
820        assert!(response.contains(":code parse"));
821    }
822
823    #[tokio::test]
824    async fn undefined_symbol_emits_compile_error() {
825        let response = handle_form_smoke("(:id 7 :form does-not-exist)").await;
826        assert!(response.contains(":id 7"));
827        assert!(response.contains(":code compile"));
828    }
829
830    #[tokio::test]
831    async fn user_function_arity_violation_emits_args_error() {
832        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
833        let _ = session
834            .handle_form("(:id 1 :form (defun id-fn (x) x))")
835            .await;
836        let response = session.handle_form("(:id 2 :form (id-fn))").await;
837        assert!(response.contains(":id 2"));
838        assert!(response.contains(":code args"));
839    }
840
841    #[test]
842    fn completions_match_case_insensitively_and_skip_internal() {
843        let session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
844        // Lower-case input matches the upper-case folded symbol; the canonical
845        // upper-case name is returned.
846        let defuns = session.completions("def");
847        assert!(defuns.contains(&"DEFUN".to_string()), "got: {defuns:?}");
848        assert!(
849            defuns.iter().all(|n| n.starts_with("DEF")),
850            "got: {defuns:?}"
851        );
852        // Sorted; no internal `$`/`__` or `(SETF …)` place names.
853        let all = session.completions("");
854        assert!(all.windows(2).all(|w| w[0] <= w[1]), "must be sorted");
855        assert!(
856            all.iter()
857                .all(|n| !n.starts_with('$') && !n.starts_with("__") && !n.starts_with("(SETF")),
858            "internal/setf symbols must be filtered: {all:?}"
859        );
860    }
861
862    #[tokio::test]
863    async fn completions_include_a_user_defined_symbol() {
864        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
865        let _ = session
866            .handle_form("(:id 1 :form (defun my-helper (x) x))")
867            .await;
868        // The defun's name is folded to upper-case; a lower-case prefix finds it.
869        let hits = session.completions("my-");
870        assert!(hits.contains(&"MY-HELPER".to_string()), "got: {hits:?}");
871    }
872
873    #[test]
874    fn completions_unknown_prefix_is_empty() {
875        let session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
876        assert!(session.completions("zzz-no-such-symbol-").is_empty());
877    }
878
879    #[tokio::test]
880    async fn interrupt_before_form_short_circuits_with_interrupted() {
881        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
882        let handle = session.interrupt_handle();
883        handle.interrupt();
884        let response = session.handle_form("(:id 11 :form (+ 1 2))").await;
885        assert!(response.contains(":id 11"));
886        assert!(response.contains(":code interrupted"));
887    }
888
889    #[tokio::test]
890    async fn coalesced_interrupts_abort_one_form_each_in_order() {
891        // Two `C-g`s observed together before a form cancel THAT form and are
892        // coalesced (they do not pre-arm cancellation of a later one); a fresh,
893        // distinct `C-g` later still cancels the next form. This pins the REPL
894        // contract against both a "lost interrupt" and an "over-eager next-form
895        // abort" regression.
896        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
897        let handle = session.interrupt_handle();
898        handle.interrupt();
899        handle.interrupt(); // two presses before the form
900        let first = session.handle_form("(:id 60 :form (+ 1 2))").await;
901        assert!(
902            first.contains(":code interrupted"),
903            "first form must abort: {first}"
904        );
905        // Both presses were coalesced into the one abort — the next form is clean.
906        let second = session.handle_form("(:id 61 :form (+ 1 2))").await;
907        assert_eq!(
908            second, "(:id 61 :value 3)",
909            "next form coalesced-poisoned: {second}"
910        );
911        // A fresh, distinct press still aborts the following form.
912        handle.interrupt();
913        let third = session.handle_form("(:id 62 :form (+ 1 2))").await;
914        assert!(
915            third.contains(":code interrupted"),
916            "a distinct later interrupt must still abort: {third}"
917        );
918    }
919
920    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
921    async fn inflight_interrupt_does_not_poison_next_form() {
922        // The SLYNK reader arms BOTH the epoch bump and the interrupt signal on
923        // `(:emacs-interrupt)` (mod.rs). When that lands mid-eval the epoch trap
924        // cancels the running call; that interrupt generation must then be acked
925        // so the SAME `C-g` can't also abort the next request.
926        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
927        let bumper = session.epoch_bumper();
928        let interrupt = session.interrupt_handle();
929        let cancel_task = tokio::spawn(async move {
930            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
931            bumper.bump();
932            interrupt.interrupt();
933        });
934        let cancelled = session
935            .handle_form("(:id 30 :form (do ((i 0 (+ i 1))) ((>= i 1000000) i)))")
936            .await;
937        cancel_task.await.unwrap();
938        assert!(
939            cancelled.contains(":code interrupted") || cancelled.contains(":code runtime"),
940            "in-flight eval should have been cancelled: {cancelled}"
941        );
942        // The next form must evaluate normally — the acked interrupt must not abort it.
943        let next = session.handle_form("(:id 31 :form (+ 1 2))").await;
944        assert_eq!(next, "(:id 31 :value 3)", "next form was poisoned: {next}");
945    }
946
947    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
948    async fn non_interrupt_failure_still_consumes_a_concurrent_interrupt() {
949        // Race: an interrupt is signalled WHILE a form is in flight, but the
950        // form loses to its own terminal error (out-of-fuel) before any epoch
951        // bump could win. The in-flight form is finished, so the interrupt must
952        // be acked on the error path too — otherwise it poisons the next form.
953        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
954        let interrupt = session.interrupt_handle();
955        let latch_task = tokio::spawn(async move {
956            // Latch only (no epoch bump): the form trips out-of-fuel on its own.
957            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
958            interrupt.interrupt();
959        });
960        let failed = session
961            .handle_form("(:id 50 :form (do ((i 0 (+ i 1))) ((>= i 100000000) i)))")
962            .await;
963        latch_task.await.unwrap();
964        assert!(
965            failed.contains(":code runtime") || failed.contains(":code interrupted"),
966            "in-flight form should fail terminally: {failed}"
967        );
968        let next = session.handle_form("(:id 51 :form (+ 1 2))").await;
969        assert_eq!(next, "(:id 51 :value 3)", "next form was poisoned: {next}");
970    }
971
972    #[tokio::test]
973    async fn interrupt_after_clean_eval_aborts_next_form() {
974        // The mirror of the above: after a form completes NORMALLY, an interrupt
975        // armed before the next form must still abort it. The in-flight cleanup
976        // only consumes the latch on an actual epoch-interrupt trap, so a `C-g`
977        // that lands post-completion is NOT swallowed.
978        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
979        let clean = session.handle_form("(:id 40 :form (+ 1 2))").await;
980        assert_eq!(clean, "(:id 40 :value 3)");
981        session.interrupt_handle().interrupt();
982        let interrupted = session.handle_form("(:id 41 :form (+ 4 5))").await;
983        assert!(
984            interrupted.contains(":code interrupted"),
985            "post-completion interrupt must abort the next form: {interrupted}"
986        );
987    }
988
989    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
990    async fn epoch_bumper_cancels_inflight_long_eval() {
991        // The one test that pins the EPOCH-TRAP cancel path specifically.
992        //
993        // Two things can end an in-flight eval with `:code interrupted`: the
994        // pre-start `check_interrupt` latch, and the epoch trap inside Wasm.
995        // They are easy to confuse, and a test that accepts either silently
996        // stops covering the trap the moment timing drifts — which is exactly
997        // what a coverage-instrumented build does to it.
998        //
999        // This isolates the trap structurally rather than by timing:
1000        //   - the interrupt handle is never touched, so `check_interrupt` can
1001        //     not fire at all and the pre-start path is unreachable;
1002        //   - fuel is effectively unbounded and the loop bound is just under
1003        //     i32::MAX, so the form can neither finish nor exhaust fuel, ruling
1004        //     out `:code runtime`;
1005        //   - the bumper runs in a loop rather than after a fixed sleep, so a
1006        //     slow host widens the window instead of missing it.
1007        // The assertions then pin the trap by its wire signature: EpochInterrupt
1008        // carries a `:detail`, whereas the pre-start latch reports
1009        // "evaluation interrupted before start" with none.
1010        let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
1011            fuel: u64::MAX,
1012            ..ScriptLimits::default()
1013        });
1014        let mut session = Session::new(ctx).expect("Session::new");
1015        let bumper = session.epoch_bumper();
1016        let done = Arc::new(std::sync::atomic::AtomicBool::new(false));
1017        let bump_until_done = done.clone();
1018        let bump_task = tokio::spawn(async move {
1019            while !bump_until_done.load(std::sync::atomic::Ordering::Relaxed) {
1020                bumper.bump();
1021                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
1022            }
1023        });
1024        // Bounded: if epoch interruption regresses, the form runs to a bound just
1025        // under i32::MAX with unbounded fuel and never returns. Without this the
1026        // test would stop being a test and become a job that burns until the k8s
1027        // deadline kills it — silently, since it produces no output meanwhile.
1028        let response = tokio::time::timeout(
1029            std::time::Duration::from_secs(60),
1030            session.handle_form("(:id 22 :form (do ((i 0 (+ i 1))) ((>= i 2000000000) i)))"),
1031        )
1032        .await
1033        .expect("epoch bump did not cancel the eval within 60s — interruption has regressed");
1034        done.store(true, std::sync::atomic::Ordering::Relaxed);
1035        bump_task.await.unwrap();
1036
1037        assert!(response.contains(":id 22"), "{response}");
1038        assert!(
1039            response.contains(":code interrupted"),
1040            "epoch bump must cancel the in-flight eval: {response}"
1041        );
1042        assert!(
1043            !response.contains("evaluation interrupted before start"),
1044            "took the pre-start latch path, so the epoch trap went untested: {response}"
1045        );
1046        assert!(
1047            response.contains(":detail"),
1048            "EpochInterrupt must surface its detail; got: {response}"
1049        );
1050    }
1051
1052    #[test]
1053    fn host_prelude_helper_is_loaded_and_compiles() {
1054        // ADR-0029 host-dependent prelude: split:list-for-transaction is loaded
1055        // on the Session path (after register_host_fns) and is callable. We
1056        // compile a form referencing it — proving load + name resolution + the
1057        // qualified native dispatch wire up — WITHOUT running it (it bottoms out
1058        // in the DB-backed list-splits-by-transaction native; execution is
1059        // covered by the db-gated integration test).
1060        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1061        assert!(
1062            session.symbols.contains("SPLIT:LIST-FOR-TRANSACTION"),
1063            "host prelude helper not registered"
1064        );
1065        // Compile (not run) a real call: lowering emits the qualified-name
1066        // dispatch + the native import, proving resolution wires up. Host fns
1067        // never execute at compile time, so no DB is touched.
1068        let program = Reader::parse("(split:list-for-transaction (car (list-transactions \"\")))")
1069            .expect("parse");
1070        if let Err(e) = session
1071            .compiler
1072            .compile_eval_with_type(&program, &mut session.symbols)
1073        {
1074            panic!("host prelude helper failed to compile: {e:?}");
1075        }
1076    }
1077
1078    #[tokio::test]
1079    async fn car_of_quoted_constant_list_compiles_on_eval_path() {
1080        // Regression: CAR/CDR of a quoted CONSTANT list folds on the codegen
1081        // path but used to trap on the eval-with-type (Session) path because
1082        // the stack handler called compile_for_stack on the quoted arg, which
1083        // has no Quote arm. The stack handlers now const-fold first.
1084        let response = handle_form_smoke("(:id 1 :form (car '(1 2 3)))").await;
1085        assert_eq!(response, "(:id 1 :value 1)");
1086    }
1087
1088    #[tokio::test]
1089    async fn car_of_quoted_heterogeneous_list_compiles_on_eval_path() {
1090        // Mixed number + string quoted list — the element is extracted by fold.
1091        let response = handle_form_smoke("(:id 1 :form (car '(7 \"x\")))").await;
1092        assert_eq!(response, "(:id 1 :value 7)");
1093    }
1094
1095    #[tokio::test]
1096    async fn car_of_cdr_of_quoted_constant_compiles_on_eval_path() {
1097        // CDR folds to the quoted tail, then CAR extracts its head — exercises
1098        // the cdr fold-first path feeding car.
1099        let response = handle_form_smoke("(:id 1 :form (car (cdr '(1 2 3))))").await;
1100        assert_eq!(response, "(:id 1 :value 2)");
1101    }
1102
1103    #[tokio::test]
1104    async fn cdr_of_quoted_constant_renders_tail_on_eval_path() {
1105        // The bare-CDR case (no enclosing CAR): folds to a quoted tail and
1106        // renders to its printed form rather than trapping.
1107        assert_eq!(
1108            handle_form_smoke("(:id 1 :form (cdr '(1 2 3)))").await,
1109            "(:id 1 :value \"(2 3)\")"
1110        );
1111        assert_eq!(
1112            handle_form_smoke("(:id 1 :form (cdr '(1)))").await,
1113            "(:id 1 :value NIL)"
1114        );
1115    }
1116
1117    #[tokio::test]
1118    async fn car_of_quoted_compound_and_symbol_heads_render_as_data() {
1119        // A compound or symbol head is quoted DATA, not code — it renders to
1120        // its printed form (not resolved as a call / variable).
1121        assert_eq!(
1122            handle_form_smoke("(:id 1 :form (car '((1 2) 3)))").await,
1123            "(:id 1 :value \"(1 2)\")"
1124        );
1125        assert_eq!(
1126            handle_form_smoke("(:id 1 :form (car '(x y)))").await,
1127            "(:id 1 :value \"X\")"
1128        );
1129    }
1130
1131    #[tokio::test]
1132    async fn reverse_of_constant_list_renders_on_eval_path() {
1133        // Regression (same class as CAR/CDR): REVERSE of a constant or
1134        // runtime-builder list folded but the stack handler rejected the
1135        // non-runtime-pair result on the eval-with-type path. Now it renders
1136        // the reversed datum on both surfaces.
1137        assert_eq!(
1138            handle_form_smoke("(:id 1 :form (reverse '(1 2 3)))").await,
1139            "(:id 1 :value \"(3 2 1)\")"
1140        );
1141        assert_eq!(
1142            handle_form_smoke("(:id 1 :form (reverse (list 1 2 3)))").await,
1143            "(:id 1 :value \"(3 2 1)\")"
1144        );
1145        // Composition still folds through to the element.
1146        assert_eq!(
1147            handle_form_smoke("(:id 1 :form (car (reverse '(1 2 3))))").await,
1148            "(:id 1 :value 3)"
1149        );
1150    }
1151
1152    #[tokio::test]
1153    async fn cons_onto_constant_list_renders_on_eval_path() {
1154        // Regression: CONS with a constant / runtime-builder list cdr trapped
1155        // in push_pair_cdr on the eval-with-type path. A fully-constant cons
1156        // now folds and renders the list datum; a dotted pair renders too.
1157        assert_eq!(
1158            handle_form_smoke("(:id 1 :form (cons 0 '(1 2 3)))").await,
1159            "(:id 1 :value \"(0 1 2 3)\")"
1160        );
1161        assert_eq!(
1162            handle_form_smoke("(:id 1 :form (cons 0 (list 1 2 3)))").await,
1163            "(:id 1 :value \"(0 1 2 3)\")"
1164        );
1165        assert_eq!(
1166            handle_form_smoke("(:id 1 :form (cons 1 2))").await,
1167            "(:id 1 :value \"(1 . 2)\")"
1168        );
1169    }
1170
1171    #[tokio::test]
1172    async fn append_of_constant_lists_renders_on_eval_path() {
1173        // Regression: all-constant APPEND folds to a quoted list; the stack
1174        // handler used to force it through runtime materialization (which can't
1175        // represent symbols) instead of rendering the folded datum.
1176        assert_eq!(
1177            handle_form_smoke("(:id 1 :form (append '(1 2) '(3)))").await,
1178            "(:id 1 :value \"(1 2 3)\")"
1179        );
1180        assert_eq!(
1181            handle_form_smoke("(:id 1 :form (append '(a b) '(c)))").await,
1182            "(:id 1 :value \"(A B C)\")"
1183        );
1184    }
1185
1186    #[tokio::test]
1187    async fn universal_prelude_helper_runs_end_to_end() {
1188        // The universal prelude is loaded on the Session path too; a math:*
1189        // helper executes through wasm and returns its value. No DB.
1190        let response = handle_form_smoke("(:id 9 :form (math:square 9))").await;
1191        assert_eq!(response, "(:id 9 :value 81)");
1192    }
1193
1194    #[tokio::test]
1195    async fn pp_form_at_value_position_returns_string() {
1196        // Exercises compile_pp_for_stack — the path nms / emacs see
1197        // when `(pp 42)` is the request form.
1198        let resp = handle_form_smoke("(:id 7 :form (pp 42))").await;
1199        assert!(resp.contains(":id 7"), "{resp}");
1200        assert!(resp.contains("\"42\""), "{resp}");
1201    }
1202
1203    #[tokio::test]
1204    async fn describe_form_at_value_position_returns_doc() {
1205        // compile_describe_for_stack — same shape.
1206        let resp = handle_form_smoke("(:id 8 :form (describe '+))").await;
1207        assert!(resp.contains(":id 8"), "{resp}");
1208        assert!(!resp.contains(":error"), "{resp}");
1209    }
1210
1211    #[tokio::test]
1212    async fn apropos_form_at_value_position_returns_list() {
1213        let resp = handle_form_smoke("(:id 9 :form (apropos \"entity\"))").await;
1214        assert!(resp.contains(":id 9"), "{resp}");
1215        assert!(!resp.contains(":error"), "{resp}");
1216    }
1217
1218    #[tokio::test]
1219    async fn deftest_form_at_value_position_returns_quoted_name() {
1220        let resp = handle_form_smoke("(:id 10 :form (deftest sanity (assert-equal 1 1)))").await;
1221        assert!(resp.contains(":id 10"), "{resp}");
1222        assert!(!resp.contains(":error"), "{resp}");
1223    }
1224
1225    #[tokio::test]
1226    async fn assert_equal_pass_form_at_value_position() {
1227        let resp = handle_form_smoke("(:id 11 :form (assert-equal 2 2))").await;
1228        assert!(resp.contains(":id 11"), "{resp}");
1229        assert!(!resp.contains(":error"), "{resp}");
1230    }
1231
1232    #[tokio::test]
1233    async fn assert_equal_fail_surfaces_as_error() {
1234        // assert_equal returns Err on mismatch; Session maps to
1235        // :code compile error envelope (it's a NomiError::Compile).
1236        let resp = handle_form_smoke("(:id 12 :form (assert-equal 1 2))").await;
1237        assert!(resp.contains(":id 12"), "{resp}");
1238        assert!(resp.contains(":error"), "{resp}");
1239    }
1240
1241    #[tokio::test]
1242    async fn coverage_dump_lists_called_natives() {
1243        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1244        let _ = session
1245            .handle_form("(:id 1 :form (rpc-protocol-version))")
1246            .await;
1247        let dump = session.handle_form("(:id 2 :form (coverage-dump))").await;
1248        assert!(dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
1249        assert!(dump.contains(":id 2"), "{dump}");
1250    }
1251
1252    #[tokio::test]
1253    async fn coverage_dump_reports_pre_warmed_natives() {
1254        // Session::new pre-compiles every zero-arg native fn (phase 4
1255        // fast-path stubs), so coverage-dump is non-empty even before
1256        // a user form lands. This is the desired semantic: it
1257        // reflects compile-time reference counts, including pre-warm
1258        // compilations, which is what the parity contract gates on.
1259        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1260        let dump = session.handle_form("(:id 1 :form (coverage-dump))").await;
1261        assert!(dump.contains(":id 1"), "{dump}");
1262        assert!(dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
1263    }
1264
1265    #[tokio::test]
1266    async fn interrupt_does_not_persist_across_forms() {
1267        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1268        session.interrupt_handle().interrupt();
1269        let _ = session.handle_form("(:id 11 :form (+ 1 2))").await;
1270        let response = session.handle_form("(:id 12 :form (+ 1 2))").await;
1271        assert_eq!(response, "(:id 12 :value 3)");
1272    }
1273
1274    #[tokio::test]
1275    async fn session_state_persists_across_forms() {
1276        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1277        let defun = session
1278            .handle_form("(:id 1 :form (defun double (x) (* 2 x)))")
1279            .await;
1280        assert!(defun.contains(":id 1"));
1281        let call = session.handle_form("(:id 2 :form (double 21))").await;
1282        assert_eq!(call, "(:id 2 :value 42)");
1283    }
1284
1285    #[tokio::test]
1286    async fn fraction_results_format_canonically() {
1287        // A fractional (Scalar) literal renders canonically as `n/d`. (Integer
1288        // `(/ 1 4)` is now Index division → 0 per ADR-0028; the canonical
1289        // fraction idiom is the `1/4` Scalar literal.)
1290        let response = handle_form_smoke("(:id 3 :form 1/4)").await;
1291        assert_eq!(response, "(:id 3 :value 1/4)");
1292    }
1293
1294    #[test]
1295    fn nomi_runtime_value_carries_through() {
1296        let value = parse_to_value("(+ 0.5 0.25)").unwrap();
1297        assert_eq!(value, Value::Number(Fraction::new(3, 4)));
1298    }
1299
1300    #[tokio::test]
1301    async fn calls_meta_native_from_nomiscript_source() {
1302        let response = handle_form_smoke("(:id 1 :form (rpc-protocol-version))").await;
1303        let expected_version = crate::natives::meta::PROTOCOL_VERSION;
1304        assert_eq!(response, format!("(:id 1 :value {expected_version})"));
1305    }
1306
1307    #[tokio::test]
1308    async fn calls_server_get_version_from_nomiscript_source() {
1309        let response = handle_form_smoke("(:id 1 :form (get-version))").await;
1310        // GIT_HASH is baked at server crate build time via env!. We don't
1311        // assert its exact value (changes per build) — just that the
1312        // envelope round-trips a non-empty :value string.
1313        assert!(
1314            response.starts_with("(:id 1 :value \""),
1315            "expected string response, got: {response}"
1316        );
1317        assert!(response.ends_with("\")"));
1318    }
1319
1320    #[tokio::test]
1321    async fn calls_server_get_build_date_from_nomiscript_source() {
1322        let response = handle_form_smoke("(:id 2 :form (get-build-date))").await;
1323        assert!(
1324            response.starts_with("(:id 2 :value \""),
1325            "expected string response, got: {response}"
1326        );
1327        assert!(response.ends_with("\")"));
1328    }
1329
1330    #[tokio::test]
1331    async fn cons_list_surfaces_as_printable_string() {
1332        // First WasmGC sub-slice: eval-mode `(cons ...)` chains now
1333        // capture through pending_string instead of erroring at compile
1334        // time. The result is a textual `(1 2 3)` value the emacs client
1335        // can (read) back into a real list. Heterogeneous car types ride
1336        // a follow-up slice once Pair/Vector/Closure/Struct share a
1337        // tagged union — today's cons cell stores i32 payloads only.
1338        let response = handle_form_smoke("(:id 12 :form (cons 1 (cons 2 (cons 3 nil))))").await;
1339        assert!(
1340            response.contains(":value \"(1 2 3)\""),
1341            "expected :value \"(1 2 3)\", got: {response}"
1342        );
1343    }
1344
1345    #[tokio::test]
1346    async fn count_native_cannot_mix_with_ratio_arithmetic() {
1347        // account-count returns i32 (a count / Index, not a Scalar). Mixing it
1348        // with a fractional Scalar literal must fail to compile — the design
1349        // forbids accidental arithmetic across the Index/Scalar strata. (An
1350        // integer literal like `10` is itself an Index now (ADR-0028), so
1351        // `(+ 10 (account-count))` is valid Index arithmetic; the genuine
1352        // stratum clash needs a fractional `1/2` Scalar operand.) The explicit
1353        // `index->scalar` bridge is the only legal crossing.
1354        let response = handle_form_smoke("(:id 11 :form (+ 1/2 (account-count)))").await;
1355        assert!(response.contains(":code compile"), "got: {response}");
1356        assert!(
1357            response.contains("scalar") && response.contains("index"),
1358            "expected Index/Scalar stratum-separation error, got: {response}"
1359        );
1360    }
1361
1362    #[tokio::test]
1363    async fn get_commodity_with_non_uuid_arg_falls_back_to_symbol_lookup() {
1364        // get-commodity now accepts a uuid OR a symbol (mirroring get-account's
1365        // name fallback), so a non-uuid arg is NO LONGER an "invalid uuid"
1366        // error — it's treated as a symbol and routed to a DB lookup. On this
1367        // no-DB smoke harness that lookup surfaces a runtime DB-access error
1368        // (not a parse error); a DB-backed test
1369        // (`get_commodity_resolves_by_symbol` in tests-integration) covers the
1370        // successful resolution.
1371        let response = handle_form_smoke("(:id 9 :form (get-commodity \"USD\"))").await;
1372        assert!(response.contains(":id 9"), "got: {response}");
1373        assert!(
1374            response.contains(":code runtime") && response.contains("get-commodity"),
1375            "expected a get-commodity runtime error (symbol path hits the DB), got: {response}"
1376        );
1377        assert!(
1378            !response.contains("invalid uuid"),
1379            "a non-uuid arg must no longer short-circuit as an invalid-uuid error: {response}"
1380        );
1381    }
1382
1383    #[test]
1384    fn meta_native_unknown_in_script_mode_compile() {
1385        // host_fns are only registered in eval-mode contexts; the compiler
1386        // built without with_host_fns shouldn't see them. Sanity that the
1387        // mode flag actually gates the registration.
1388        use nomiscript::CompileMode;
1389        let mut compiler = Compiler::new();
1390        let mut symbols = SymbolTable::with_builtins();
1391        let program = nomiscript::Reader::parse("(rpc-protocol-version)").unwrap();
1392        let result = compiler.compile_with_mode(&program, &mut symbols, CompileMode::Script);
1393        assert!(
1394            result.is_err(),
1395            "host fn should not be callable when compiler has no specs"
1396        );
1397    }
1398
1399    #[tokio::test]
1400    async fn get_config_missing_name_yields_error_envelope() {
1401        let resp = handle_form_smoke("(:id 20 :form (get-config \"\"))").await;
1402        assert!(resp.contains(":id 20"), "{resp}");
1403        assert!(resp.contains(":error"), "{resp}");
1404        assert!(!resp.contains(":value"), "{resp}");
1405    }
1406
1407    #[tokio::test]
1408    async fn get_config_success_shape_is_config_value() {
1409        // Passes a non-empty name; the DB call fails (no pool) and surfaces
1410        // as an :error envelope — we verify the SUCCESS wire shape is only
1411        // emitted when the command actually succeeds. Here we just confirm
1412        // the argument-validation path (empty string) always errors.
1413        let resp = handle_form_smoke("(:id 21 :form (get-config \"\"))").await;
1414        assert!(resp.contains(":id 21"), "{resp}");
1415        assert!(!resp.contains(":value"), "{resp}");
1416    }
1417}