1
use std::cell::RefCell;
2
use std::sync::{Arc, Mutex};
3

            
4
use nomiscript::{
5
    Compiler, Error as NomiError, Expr, HostFnSpec, Program, Reader, SymbolTable, Value,
6
};
7
use scripting::runtime::{
8
    EngineError, EngineOpts, ModuleCache, build_engine, classify_runtime_error, decode_eval_result,
9
};
10
use thiserror::Error;
11
use tracing::debug;
12
use wasmtime::{AnyRef, Engine, Linker, Rooted, Store, Val};
13

            
14
use crate::ctx::{EpochBumper, InterruptHandle, ScriptCtx};
15
use crate::envelope::{
16
    EnvelopeError, ErrorCode, Request, RequestId, Response, ResponsePayload, format_response,
17
    parse_request,
18
};
19

            
20
const 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.
33
pub 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

            
48
impl SessionData {
49
2558
    pub(crate) fn new(ctx: ScriptCtx, output: Arc<Mutex<String>>) -> Self {
50
2558
        Self {
51
2558
            ctx,
52
2558
            output,
53
2558
            draft: None,
54
2558
        }
55
2558
    }
56

            
57
    /// Builds session data with a draft accumulator armed — the render path's
58
    /// constructor. Draft natives require `draft` to be `Some`.
59
101
    pub(crate) fn for_render(ctx: ScriptCtx, output: Arc<Mutex<String>>) -> Self {
60
101
        Self {
61
101
            ctx,
62
101
            output,
63
101
            draft: Some(RefCell::new(crate::draft::TransactionDraft::new())),
64
101
        }
65
101
    }
66

            
67
    #[must_use]
68
2503
    pub fn ctx(&self) -> &ScriptCtx {
69
2503
        &self.ctx
70
2503
    }
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
162
    pub fn with_draft<F>(&self, f: F) -> wasmtime::Result<()>
76
162
    where
77
162
        F: FnOnce(&mut crate::draft::TransactionDraft),
78
    {
79
162
        let cell = self
80
162
            .draft
81
162
            .as_ref()
82
162
            .ok_or_else(|| wasmtime::Error::msg("draft native invoked outside render mode"))?;
83
162
        f(&mut cell.borrow_mut());
84
162
        Ok(())
85
162
    }
86

            
87
    /// Consumes the accumulated draft, if any. Called after a render run via
88
    /// `store.into_data()`.
89
    #[must_use]
90
54
    pub fn into_draft(self) -> Option<crate::draft::TransactionDraft> {
91
54
        self.draft.map(RefCell::into_inner)
92
54
    }
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
6
    pub fn push_output(&self, msg: &str) {
98
6
        if let Ok(mut buf) = self.output.lock() {
99
6
            buf.push_str(msg);
100
6
        }
101
6
    }
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`].
113
pub 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)]
136
pub struct EvalOutcome {
137
    pub output: String,
138
    pub payload: ResponsePayload,
139
}
140

            
141
#[derive(Debug, Error)]
142
pub enum SessionError {
143
    #[error("engine init failed: {0}")]
144
    Engine(#[from] EngineError),
145
}
146

            
147
impl Session {
148
1539
    pub fn new(ctx: ScriptCtx) -> Result<Self, SessionError> {
149
1539
        let engine = build_engine(EngineOpts::baseline().with_fuel())?;
150
1539
        let host_fns = crate::natives::all_compiler_specs();
151
1539
        let mut symbols = SymbolTable::with_builtins();
152
1539
        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
1539
        crate::host_prelude::load(&mut symbols);
157
1539
        let mut session = Self {
158
1539
            ctx,
159
1539
            engine,
160
1539
            compiler: Compiler::with_host_fns(host_fns.clone()),
161
1539
            cache: ModuleCache::new(),
162
1539
            symbols,
163
1539
            interrupt: InterruptHandle::new(),
164
1539
            interrupt_ack: 0,
165
1539
            output: Arc::new(Mutex::new(String::new())),
166
1539
        };
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
1539
        session.warm_bare_call_cache(&host_fns);
176
1539
        Ok(session)
177
1539
    }
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
1539
    fn warm_bare_call_cache(&mut self, host_fns: &[HostFnSpec]) {
189
67716
        for spec in host_fns {
190
67716
            if !spec.params.is_empty() || spec.result.is_none() {
191
49248
                continue;
192
18468
            }
193
18468
            let form = Expr::List(vec![Expr::Symbol(spec.nomi_name.clone())]);
194
18468
            let program = Program::new(vec![form]);
195
18468
            let Ok((bytes, _ty)) = self
196
18468
                .compiler
197
18468
                .compile_eval_with_type(&program, &mut self.symbols)
198
            else {
199
                continue;
200
            };
201
18468
            let _ = self.cache.get_or_compile(&self.engine, &bytes);
202
        }
203
1539
    }
204

            
205
    #[must_use]
206
    pub fn ctx(&self) -> &ScriptCtx {
207
        &self.ctx
208
    }
209

            
210
    #[must_use]
211
206
    pub fn interrupt_handle(&self) -> InterruptHandle {
212
206
        self.interrupt.clone()
213
206
    }
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
4
    pub fn completions(&self, prefix: &str) -> Vec<String> {
225
4
        let needle = prefix.to_ascii_uppercase();
226
4
        let mut names: Vec<String> = self
227
4
            .symbols
228
4
            .iter()
229
1001
            .map(|(name, _)| name.as_str())
230
1001
            .filter(|name| {
231
1001
                !name.starts_with('$') && !name.starts_with("__") && !name.starts_with("(SETF")
232
1001
            })
233
881
            .filter(|name| name.starts_with(&needle))
234
4
            .map(str::to_owned)
235
4
            .collect();
236
4
        names.sort_unstable();
237
4
        names.dedup();
238
4
        names
239
4
    }
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
200
    pub fn epoch_bumper(&self) -> EpochBumper {
248
200
        EpochBumper::new(self.engine.clone())
249
200
    }
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
126
    pub fn cache_size(&self) -> Result<usize, EngineError> {
256
126
        self.cache.len()
257
126
    }
258

            
259
2633
    pub async fn handle_form(&mut self, frame: &str) -> String {
260
202
        let response = match self.evaluate(frame).await {
261
169
            Ok(resp) => resp,
262
33
            Err(err) => err.into_response(),
263
        };
264
202
        format_response(&response)
265
202
    }
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
7
    pub async fn handle_request(&mut self, source: &str) -> EvalOutcome {
273
7
        if let Ok(mut buf) = self.output.lock() {
274
7
            buf.clear();
275
7
        }
276
7
        let payload = match self.eval_source(source).await {
277
4
            Ok(value) => ResponsePayload::Value(value),
278
3
            Err(err) => err.into_response().payload,
279
        };
280
7
        let output = self
281
7
            .output
282
7
            .lock()
283
7
            .map(|buf| buf.clone())
284
7
            .unwrap_or_default();
285
7
        EvalOutcome { output, payload }
286
7
    }
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
7
    async fn eval_source(&mut self, source: &str) -> Result<Value, EvalFailure> {
295
7
        let id = RequestId::Int(0);
296
7
        let program = Reader::parse(source).map_err(|err| EvalFailure::Eval(id.clone(), err))?;
297
7
        let mut exprs = program.exprs;
298
7
        let form = match exprs.len() {
299
            0 => return Ok(Value::Nil),
300
6
            1 => exprs.remove(0),
301
            _ => {
302
1
                return Err(EvalFailure::Eval(
303
1
                    id,
304
1
                    NomiError::Compile("expected a single form".to_string()),
305
1
                ));
306
            }
307
        };
308
6
        self.eval_one_form(form).await
309
7
    }
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
4
    pub async fn handle_file(&mut self, path: &str) -> EvalOutcome {
317
4
        if let Ok(mut buf) = self.output.lock() {
318
4
            buf.clear();
319
4
        }
320
4
        let payload = match self.load_path(path).await {
321
1
            Ok(summary) => ResponsePayload::Value(Value::String(summary)),
322
3
            Err(err) => err.into_response().payload,
323
        };
324
4
        let output = self
325
4
            .output
326
4
            .lock()
327
4
            .map(|buf| buf.clone())
328
4
            .unwrap_or_default();
329
4
        EvalOutcome { output, payload }
330
4
    }
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
281
    fn ack_interrupt(&mut self, observed: u64) {
343
281
        if observed > self.interrupt_ack {
344
81
            self.interrupt_ack = observed;
345
207
        }
346
281
    }
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
5186
    fn check_interrupt(&mut self, id: &RequestId) -> Option<EvalFailure> {
353
5186
        let observed = self.interrupt.generation();
354
5186
        (observed > self.interrupt_ack).then(|| {
355
81
            self.ack_interrupt(observed);
356
81
            EvalFailure::Interrupted(id.clone())
357
81
        })
358
5186
    }
359

            
360
4
    async fn load_path(&mut self, path: &str) -> Result<String, EvalFailure> {
361
4
        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
4
        if let Some(err) = self.check_interrupt(&id) {
366
1
            return Err(err);
367
3
        }
368
3
        let source = std::fs::read_to_string(path).map_err(|err| {
369
1
            EvalFailure::Eval(
370
1
                id.clone(),
371
1
                NomiError::Compile(format!("cannot read {path}: {err}")),
372
1
            )
373
1
        })?;
374
2
        if let Some(err) = self.check_interrupt(&id) {
375
            return Err(err);
376
2
        }
377
2
        let program = Reader::parse(&source).map_err(|err| EvalFailure::Eval(id.clone(), err))?;
378
2
        let count = program.exprs.len();
379
4
        for form in program.exprs {
380
4
            self.run(&Request {
381
4
                id: id.clone(),
382
4
                form,
383
4
            })
384
4
            .await?;
385
        }
386
1
        Ok(format!("loaded {path} ({count} forms)"))
387
4
    }
388

            
389
    /// Evaluates a single already-parsed form (mREPL input). The interrupt
390
    /// pre-start check lives in `run`.
391
6
    async fn eval_one_form(&mut self, form: Expr) -> Result<Value, EvalFailure> {
392
6
        self.run(&Request {
393
6
            id: RequestId::Int(0),
394
6
            form,
395
6
        })
396
6
        .await
397
6
    }
398

            
399
2633
    async fn evaluate(&mut self, frame: &str) -> Result<Response, EvalFailure> {
400
202
        let request = parse_request(frame).map_err(EvalFailure::Envelope)?;
401
199
        let value = self.run(&request).await?;
402
169
        Ok(Response {
403
169
            id: request.id,
404
169
            payload: ResponsePayload::Value(value),
405
169
        })
406
202
    }
407

            
408
2623
    async fn run(&mut self, request: &Request) -> Result<Value, EvalFailure> {
409
209
        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
209
        if let Some(err) = self.check_interrupt(&request.id) {
413
7
            return Err(err);
414
202
        }
415
202
        let program = Program::new(vec![request.form.clone()]);
416
202
        let (bytes, result_ty) = self
417
202
            .compiler
418
202
            .compile_eval_with_type(&program, &mut self.symbols)
419
202
            .map_err(|err| EvalFailure::Eval(request.id.clone(), err))?;
420
194
        let module = self
421
194
            .cache
422
194
            .get_or_compile(&self.engine, &bytes)
423
194
            .map_err(|err| EvalFailure::Engine(request.id.clone(), err))?;
424

            
425
194
        let mut linker: Linker<SessionData> = Linker::new(&self.engine);
426
194
        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
194
        let mut store: Store<SessionData> = Store::new(
434
194
            &self.engine,
435
194
            SessionData::new(self.ctx.clone(), Arc::clone(&self.output)),
436
        );
437
194
        store.set_fuel(self.ctx.limits.fuel).map_err(|err| {
438
            EvalFailure::Engine(request.id.clone(), EngineError::Fuel(err.to_string()))
439
        })?;
440
194
        store.set_epoch_deadline(EPOCH_DEADLINE_TICKS);
441

            
442
194
        let instance = linker
443
194
            .instantiate_async(&mut store, &module)
444
194
            .await
445
194
            .map_err(|err| EvalFailure::Engine(request.id.clone(), classify_runtime_error(&err)))?;
446
194
        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
194
        if let Some(err) = self.check_interrupt(&request.id) {
455
5
            return Err(err);
456
189
        }
457
189
        let mut results = [Val::AnyRef(None)];
458
189
        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
189
        if call_result.is_err() {
479
13
            let observed = self.interrupt.generation();
480
13
            self.ack_interrupt(observed);
481
176
        }
482
189
        call_result
483
189
            .map_err(|err| EvalFailure::Engine(request.id.clone(), classify_runtime_error(&err)))?;
484

            
485
176
        let any: Option<Rooted<AnyRef>> = match &results[0] {
486
176
            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
176
        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
176
        Ok(Value::from(captured))
501
209
    }
502
}
503

            
504
enum EvalFailure {
505
    Envelope(EnvelopeError),
506
    Eval(RequestId, NomiError),
507
    Engine(RequestId, EngineError),
508
    Interrupted(RequestId),
509
}
510

            
511
impl EvalFailure {
512
345
    fn into_response(self) -> Response {
513
345
        match self {
514
20
            EvalFailure::Envelope(err) => Response {
515
20
                id: RequestId::Int(0),
516
20
                payload: ResponsePayload::Error {
517
20
                    code: envelope_error_code(&err),
518
20
                    message: err.to_string(),
519
20
                    detail: Some(format!("{err:?}")),
520
20
                },
521
20
            },
522
44
            EvalFailure::Eval(id, err) => Response {
523
44
                id,
524
44
                payload: ResponsePayload::Error {
525
44
                    code: nomi_error_code(&err),
526
44
                    message: err.to_string(),
527
44
                    detail: Some(format!("{err:?}")),
528
44
                },
529
44
            },
530
200
            EvalFailure::Engine(id, err) => Response {
531
200
                id,
532
200
                payload: ResponsePayload::Error {
533
200
                    code: engine_error_code(&err),
534
200
                    message: err.to_string(),
535
200
                    detail: Some(format!("{err:?}")),
536
200
                },
537
200
            },
538
81
            EvalFailure::Interrupted(id) => Response {
539
81
                id,
540
81
                payload: ResponsePayload::Error {
541
81
                    code: ErrorCode::new(ErrorCode::INTERRUPTED),
542
81
                    message: "evaluation interrupted before start".into(),
543
81
                    detail: None,
544
81
                },
545
81
            },
546
        }
547
345
    }
548
}
549

            
550
20
fn envelope_error_code(err: &EnvelopeError) -> ErrorCode {
551
20
    let symbol = match err {
552
1
        EnvelopeError::Parse(_) => ErrorCode::PARSE,
553
        EnvelopeError::NotSingleExpr
554
        | EnvelopeError::NotPlist
555
        | EnvelopeError::MissingKey(_)
556
19
        | EnvelopeError::InvalidValue(_, _) => ErrorCode::ARGS,
557
    };
558
20
    ErrorCode::new(symbol)
559
20
}
560

            
561
44
fn nomi_error_code(err: &NomiError) -> ErrorCode {
562
44
    let symbol = match err {
563
        NomiError::Parse(_) => ErrorCode::PARSE,
564
43
        NomiError::Compile(_) | NomiError::UndefinedSymbol(_) => ErrorCode::COMPILE,
565
        NomiError::Runtime(_) => ErrorCode::RUNTIME,
566
1
        NomiError::Type { .. } | NomiError::Arity { .. } => ErrorCode::ARGS,
567
    };
568
44
    ErrorCode::new(symbol)
569
44
}
570

            
571
200
fn engine_error_code(err: &EngineError) -> ErrorCode {
572
200
    match err {
573
        EngineError::Compile(_) => ErrorCode::new(ErrorCode::COMPILE),
574
128
        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
18
        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
54
        EngineError::ScriptRaised { code, .. } => ErrorCode::new(code.clone()),
589
    }
590
200
}
591

            
592
#[cfg(test)]
593
mod tests {
594
    use super::*;
595
    use nomiscript::{Fraction, Reader};
596

            
597
38
    async fn handle_form_smoke(frame: &str) -> String {
598
38
        let ctx = ScriptCtx::new(uuid::Uuid::nil());
599
38
        let mut session = Session::new(ctx).expect("Session::new");
600
38
        session.handle_form(frame).await
601
38
    }
602

            
603
2
    fn parse_to_value(input: &str) -> Result<Value, NomiError> {
604
2
        let program = Reader::parse(input)?;
605
2
        let mut symbols = SymbolTable::with_builtins();
606
2
        nomiscript::eval_program(&mut symbols, &program)
607
2
    }
608

            
609
    #[tokio::test]
610
1
    async fn evaluates_arithmetic_and_returns_value_envelope() {
611
1
        let response = handle_form_smoke("(:id 1 :form (+ 1 2))").await;
612
1
        assert_eq!(response, "(:id 1 :value 3)");
613
1
    }
614

            
615
    #[tokio::test]
616
1
    async fn evaluates_nested_arithmetic() {
617
1
        let response = handle_form_smoke("(:id 5 :form (* (+ 1 2) (- 10 4)))").await;
618
1
        assert_eq!(response, "(:id 5 :value 18)");
619
1
    }
620

            
621
    #[tokio::test]
622
1
    async fn print_in_eval_mode_does_not_panic() {
623
        // Regression: PRINT / DISPLAY / NEWLINE / DEBUG lower to `env.log`,
624
        // which was script-mode-only. In eval mode the missing func index used
625
        // to SIGABRT the compiler (`registry.rs` `HashMap[key]`). The eval-mode
626
        // `log` import + the rpc `env.log` host fn now make it compile + run.
627
1
        let response = handle_form_smoke("(:id 1 :form (print \"hi\"))").await;
628
1
        assert!(response.contains(":id 1"), "got: {response}");
629
1
        assert!(!response.contains(":code"), "must not error: {response}");
630
1
    }
631

            
632
    #[tokio::test]
633
1
    async fn dolist_with_print_in_eval_mode_runs() {
634
        // The exact shape from the Metro script that first surfaced the panic.
635
1
        let response = handle_form_smoke("(:id 2 :form (dolist (x (list 1 2 3)) (print x)))").await;
636
1
        assert!(response.contains(":id 2"), "got: {response}");
637
1
        assert!(!response.contains(":code"), "must not error: {response}");
638
1
    }
639

            
640
    #[tokio::test]
641
1
    async fn handle_request_captures_output_and_value() {
642
        // `(print "hi")` writes "hi" to the per-request buffer (via env.log) and
643
        // its own return value is nil; the structured outcome must carry the
644
        // captured text in `output` AND the value payload — the SLYNK mREPL
645
        // renders these as `:write-string` + `:write-values` respectively.
646
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
647
1
        let outcome = session.handle_request("(print \"hi\")").await;
648
1
        assert!(
649
1
            outcome.output.contains("hi"),
650
            "captured output should contain the printed text, got: {:?}",
651
            outcome.output
652
        );
653
1
        assert!(
654
1
            matches!(outcome.payload, ResponsePayload::Value(_)),
655
1
            "payload should be a Value, got: {:?}",
656
1
            outcome.payload
657
1
        );
658
1
    }
659

            
660
    #[tokio::test]
661
1
    async fn handle_request_value_only_has_empty_output() {
662
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
663
1
        let outcome = session.handle_request("(+ 1 2)").await;
664
1
        assert!(outcome.output.is_empty(), "got: {:?}", outcome.output);
665
1
        assert_eq!(
666
1
            outcome.payload,
667
1
            ResponsePayload::Value(Value::Number(Fraction::from_integer(3)))
668
1
        );
669
1
    }
670

            
671
    #[tokio::test]
672
1
    async fn handle_request_rejects_plist_shaped_injection() {
673
        // Adversarial review: the source must be parsed as a standalone AST, not
674
        // interpolated into a `(:id 0 :form …)` envelope string — otherwise
675
        // `1 :form (+ 2 3)` would re-read as a plist and eval to `1`. It must
676
        // instead error (more than one top-level form), never silently return 1.
677
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
678
1
        let outcome = session.handle_request("1 :form (+ 2 3)").await;
679
1
        assert!(
680
1
            matches!(outcome.payload, ResponsePayload::Error { .. }),
681
1
            "plist-shaped input must error, got: {:?}",
682
1
            outcome.payload
683
1
        );
684
1
    }
685

            
686
    #[tokio::test]
687
1
    async fn handle_request_interrupt_latch_aborts_before_eval() {
688
        // An interrupt latched before the request (the SLYNK reader arms it on
689
        // `(:emacs-interrupt)`) must abort the eval at the pre-start check, even
690
        // for a form that would otherwise compile fine.
691
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
692
1
        session.interrupt_handle().interrupt();
693
1
        let outcome = session.handle_request("(+ 1 2)").await;
694
1
        match outcome.payload {
695
1
            ResponsePayload::Error { code, .. } => {
696
1
                assert_eq!(code.as_symbol(), ErrorCode::INTERRUPTED);
697
1
            }
698
1
            other => panic!("expected interrupted error, got: {other:?}"),
699
1
        }
700
1
    }
701

            
702
    #[tokio::test]
703
1
    async fn handle_file_evaluates_all_forms_and_persists_state() {
704
        // A file with a defun + a call to it: state accumulates across forms,
705
        // and the load returns a summary value (not the last form's value).
706
1
        let dir = std::env::temp_dir();
707
1
        let path = dir.join(format!("nms_load_test_{}.nms", std::process::id()));
708
1
        std::fs::write(&path, "(defun dbl (x) (* x 2))\n(dbl 21)\n").unwrap();
709
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
710
1
        let outcome = session.handle_file(path.to_str().unwrap()).await;
711
1
        std::fs::remove_file(&path).ok();
712
1
        match outcome.payload {
713
1
            ResponsePayload::Value(Value::String(s)) => {
714
1
                assert!(s.contains("loaded"), "summary: {s}");
715
1
                assert!(s.contains("2 forms"), "summary: {s}");
716
1
            }
717
1
            other => panic!("expected a load summary string, got: {other:?}"),
718
1
        }
719
1
    }
720

            
721
    #[tokio::test]
722
1
    async fn handle_file_missing_path_errors() {
723
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
724
1
        let outcome = session.handle_file("/no/such/nms/file.nms").await;
725
1
        assert!(
726
1
            matches!(outcome.payload, ResponsePayload::Error { .. }),
727
1
            "got: {:?}",
728
1
            outcome.payload
729
1
        );
730
1
    }
731

            
732
    #[tokio::test]
733
1
    async fn handle_file_aborts_on_a_bad_form() {
734
1
        let dir = std::env::temp_dir();
735
1
        let path = dir.join(format!("nms_load_bad_{}.nms", std::process::id()));
736
1
        std::fs::write(&path, "(+ 1 2)\n(undefined-symbol-here)\n").unwrap();
737
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
738
1
        let outcome = session.handle_file(path.to_str().unwrap()).await;
739
1
        std::fs::remove_file(&path).ok();
740
1
        assert!(
741
1
            matches!(outcome.payload, ResponsePayload::Error { .. }),
742
1
            "a bad form must abort the load, got: {:?}",
743
1
            outcome.payload
744
1
        );
745
1
    }
746

            
747
    #[tokio::test]
748
1
    async fn handle_file_honours_interrupt_armed_before_load() {
749
        // A `C-g` that lands before/during the synchronous read+parse must abort
750
        // the load (the pre-read latch check), not be ignored until the first
751
        // form reaches the Wasm call.
752
1
        let dir = std::env::temp_dir();
753
1
        let path = dir.join(format!("nms_load_intr_{}.nms", std::process::id()));
754
1
        std::fs::write(&path, "(+ 1 2)\n(+ 3 4)\n").unwrap();
755
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
756
1
        session.interrupt_handle().interrupt();
757
1
        let outcome = session.handle_file(path.to_str().unwrap()).await;
758
1
        std::fs::remove_file(&path).ok();
759
1
        match outcome.payload {
760
1
            ResponsePayload::Error { code, .. } => {
761
1
                assert_eq!(code.as_symbol(), ErrorCode::INTERRUPTED, "got: {code:?}");
762
1
            }
763
1
            other => panic!("interrupt should abort the load, got: {other:?}"),
764
1
        }
765
1
    }
766

            
767
    #[tokio::test]
768
1
    async fn handle_request_surfaces_error_payload() {
769
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
770
1
        let outcome = session.handle_request("does-not-exist").await;
771
1
        assert!(
772
1
            matches!(outcome.payload, ResponsePayload::Error { .. }),
773
1
            "payload should be an Error, got: {:?}",
774
1
            outcome.payload
775
1
        );
776
1
    }
777

            
778
    #[tokio::test]
779
1
    async fn handle_request_clears_output_between_calls() {
780
        // The buffer must not leak across requests: a print then a pure value.
781
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
782
1
        let _ = session.handle_request("(print \"first\")").await;
783
1
        let second = session.handle_request("(+ 1 1)").await;
784
1
        assert!(
785
1
            second.output.is_empty(),
786
1
            "output leaked from prior request: {:?}",
787
1
            second.output
788
1
        );
789
1
    }
790

            
791
    #[tokio::test]
792
1
    async fn returns_value_for_literal_form() {
793
1
        let response = handle_form_smoke("(:id 9 :form 42)").await;
794
1
        assert_eq!(response, "(:id 9 :value 42)");
795
1
    }
796

            
797
    #[tokio::test]
798
1
    async fn returns_value_for_string_literal() {
799
1
        let response = handle_form_smoke("(:id 9 :form \"hello\")").await;
800
1
        assert_eq!(response, "(:id 9 :value \"hello\")");
801
1
    }
802

            
803
    #[test]
804
1
    fn round_trips_bytes_through_eval() {
805
1
        let value = parse_to_value("'#u8(1 2 3)").unwrap();
806
1
        assert_eq!(value, Value::Bytes(vec![1, 2, 3]));
807
1
    }
808

            
809
    #[tokio::test]
810
1
    async fn bad_envelope_emits_envelope_error() {
811
1
        let response = handle_form_smoke("(:form (+ 1 2))").await;
812
1
        assert!(response.contains(":code args"));
813
1
        assert!(response.contains(":id 0"));
814
1
    }
815

            
816
    #[tokio::test]
817
1
    async fn malformed_envelope_emits_parse_error() {
818
1
        let response = handle_form_smoke("(((((").await;
819
1
        assert!(response.contains(":code parse"));
820
1
    }
821

            
822
    #[tokio::test]
823
1
    async fn undefined_symbol_emits_compile_error() {
824
1
        let response = handle_form_smoke("(:id 7 :form does-not-exist)").await;
825
1
        assert!(response.contains(":id 7"));
826
1
        assert!(response.contains(":code compile"));
827
1
    }
828

            
829
    #[tokio::test]
830
1
    async fn user_function_arity_violation_emits_args_error() {
831
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
832
1
        let _ = session
833
1
            .handle_form("(:id 1 :form (defun id-fn (x) x))")
834
1
            .await;
835
1
        let response = session.handle_form("(:id 2 :form (id-fn))").await;
836
1
        assert!(response.contains(":id 2"));
837
1
        assert!(response.contains(":code args"));
838
1
    }
839

            
840
    #[test]
841
1
    fn completions_match_case_insensitively_and_skip_internal() {
842
1
        let session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
843
        // Lower-case input matches the upper-case folded symbol; the canonical
844
        // upper-case name is returned.
845
1
        let defuns = session.completions("def");
846
1
        assert!(defuns.contains(&"DEFUN".to_string()), "got: {defuns:?}");
847
1
        assert!(
848
7
            defuns.iter().all(|n| n.starts_with("DEF")),
849
            "got: {defuns:?}"
850
        );
851
        // Sorted; no internal `$`/`__` or `(SETF …)` place names.
852
1
        let all = session.completions("");
853
219
        assert!(all.windows(2).all(|w| w[0] <= w[1]), "must be sorted");
854
1
        assert!(
855
1
            all.iter()
856
220
                .all(|n| !n.starts_with('$') && !n.starts_with("__") && !n.starts_with("(SETF")),
857
            "internal/setf symbols must be filtered: {all:?}"
858
        );
859
1
    }
860

            
861
    #[tokio::test]
862
1
    async fn completions_include_a_user_defined_symbol() {
863
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
864
1
        let _ = session
865
1
            .handle_form("(:id 1 :form (defun my-helper (x) x))")
866
1
            .await;
867
        // The defun's name is folded to upper-case; a lower-case prefix finds it.
868
1
        let hits = session.completions("my-");
869
1
        assert!(hits.contains(&"MY-HELPER".to_string()), "got: {hits:?}");
870
1
    }
871

            
872
    #[test]
873
1
    fn completions_unknown_prefix_is_empty() {
874
1
        let session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
875
1
        assert!(session.completions("zzz-no-such-symbol-").is_empty());
876
1
    }
877

            
878
    #[tokio::test]
879
1
    async fn interrupt_before_form_short_circuits_with_interrupted() {
880
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
881
1
        let handle = session.interrupt_handle();
882
1
        handle.interrupt();
883
1
        let response = session.handle_form("(:id 11 :form (+ 1 2))").await;
884
1
        assert!(response.contains(":id 11"));
885
1
        assert!(response.contains(":code interrupted"));
886
1
    }
887

            
888
    #[tokio::test]
889
1
    async fn coalesced_interrupts_abort_one_form_each_in_order() {
890
        // Two `C-g`s observed together before a form cancel THAT form and are
891
        // coalesced (they do not pre-arm cancellation of a later one); a fresh,
892
        // distinct `C-g` later still cancels the next form. This pins the REPL
893
        // contract against both a "lost interrupt" and an "over-eager next-form
894
        // abort" regression.
895
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
896
1
        let handle = session.interrupt_handle();
897
1
        handle.interrupt();
898
1
        handle.interrupt(); // two presses before the form
899
1
        let first = session.handle_form("(:id 60 :form (+ 1 2))").await;
900
1
        assert!(
901
1
            first.contains(":code interrupted"),
902
            "first form must abort: {first}"
903
        );
904
        // Both presses were coalesced into the one abort — the next form is clean.
905
1
        let second = session.handle_form("(:id 61 :form (+ 1 2))").await;
906
1
        assert_eq!(
907
            second, "(:id 61 :value 3)",
908
            "next form coalesced-poisoned: {second}"
909
        );
910
        // A fresh, distinct press still aborts the following form.
911
1
        handle.interrupt();
912
1
        let third = session.handle_form("(:id 62 :form (+ 1 2))").await;
913
1
        assert!(
914
1
            third.contains(":code interrupted"),
915
1
            "a distinct later interrupt must still abort: {third}"
916
1
        );
917
1
    }
918

            
919
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
920
1
    async fn inflight_interrupt_does_not_poison_next_form() {
921
        // The SLYNK reader arms BOTH the epoch bump and the interrupt signal on
922
        // `(:emacs-interrupt)` (mod.rs). When that lands mid-eval the epoch trap
923
        // cancels the running call; that interrupt generation must then be acked
924
        // so the SAME `C-g` can't also abort the next request.
925
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
926
1
        let bumper = session.epoch_bumper();
927
1
        let interrupt = session.interrupt_handle();
928
1
        let cancel_task = tokio::spawn(async move {
929
1
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
930
1
            bumper.bump();
931
1
            interrupt.interrupt();
932
1
        });
933
1
        let cancelled = session
934
1
            .handle_form("(:id 30 :form (do ((i 0 (+ i 1))) ((>= i 1000000) i)))")
935
1
            .await;
936
1
        cancel_task.await.unwrap();
937
1
        assert!(
938
1
            cancelled.contains(":code interrupted") || cancelled.contains(":code runtime"),
939
            "in-flight eval should have been cancelled: {cancelled}"
940
        );
941
        // The next form must evaluate normally — the acked interrupt must not abort it.
942
1
        let next = session.handle_form("(:id 31 :form (+ 1 2))").await;
943
1
        assert_eq!(next, "(:id 31 :value 3)", "next form was poisoned: {next}");
944
1
    }
945

            
946
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
947
1
    async fn non_interrupt_failure_still_consumes_a_concurrent_interrupt() {
948
        // Race: an interrupt is signalled WHILE a form is in flight, but the
949
        // form loses to its own terminal error (out-of-fuel) before any epoch
950
        // bump could win. The in-flight form is finished, so the interrupt must
951
        // be acked on the error path too — otherwise it poisons the next form.
952
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
953
1
        let interrupt = session.interrupt_handle();
954
1
        let latch_task = tokio::spawn(async move {
955
            // Latch only (no epoch bump): the form trips out-of-fuel on its own.
956
1
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
957
1
            interrupt.interrupt();
958
1
        });
959
1
        let failed = session
960
1
            .handle_form("(:id 50 :form (do ((i 0 (+ i 1))) ((>= i 100000000) i)))")
961
1
            .await;
962
1
        latch_task.await.unwrap();
963
1
        assert!(
964
1
            failed.contains(":code runtime") || failed.contains(":code interrupted"),
965
            "in-flight form should fail terminally: {failed}"
966
        );
967
1
        let next = session.handle_form("(:id 51 :form (+ 1 2))").await;
968
1
        assert_eq!(next, "(:id 51 :value 3)", "next form was poisoned: {next}");
969
1
    }
970

            
971
    #[tokio::test]
972
1
    async fn interrupt_after_clean_eval_aborts_next_form() {
973
        // The mirror of the above: after a form completes NORMALLY, an interrupt
974
        // armed before the next form must still abort it. The in-flight cleanup
975
        // only consumes the latch on an actual epoch-interrupt trap, so a `C-g`
976
        // that lands post-completion is NOT swallowed.
977
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
978
1
        let clean = session.handle_form("(:id 40 :form (+ 1 2))").await;
979
1
        assert_eq!(clean, "(:id 40 :value 3)");
980
1
        session.interrupt_handle().interrupt();
981
1
        let interrupted = session.handle_form("(:id 41 :form (+ 4 5))").await;
982
1
        assert!(
983
1
            interrupted.contains(":code interrupted"),
984
1
            "post-completion interrupt must abort the next form: {interrupted}"
985
1
        );
986
1
    }
987

            
988
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
989
1
    async fn epoch_bumper_cancels_inflight_long_eval() {
990
        // Concurrent cancel scenario: one task drives a CPU-bound eval,
991
        // another task bumps the epoch shortly after. Verifies the
992
        // engine clone really does share state across threads so emacs
993
        // C-g can land mid-eval.
994
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
995
1
        let bumper = session.epoch_bumper();
996
1
        let bump_task = tokio::spawn(async move {
997
1
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
998
1
            bumper.bump();
999
1
        });
        // Tight loop that'd otherwise exhaust default fuel before
        // returning. Either fuel or epoch will fire — the assert below
        // tolerates both possibilities by checking the response is an
        // error envelope, but in practice the 20ms bump arrives first.
1
        let response = session
1
            .handle_form("(:id 22 :form (do ((i 0 (+ i 1))) ((>= i 1000000) i)))")
1
            .await;
1
        bump_task.await.unwrap();
1
        assert!(response.contains(":id 22"), "{response}");
1
        assert!(
1
            response.contains(":code interrupted") || response.contains(":code runtime"),
1
            "{response}"
1
        );
1
    }
    #[test]
1
    fn host_prelude_helper_is_loaded_and_compiles() {
        // ADR-0029 host-dependent prelude: split:list-for-transaction is loaded
        // on the Session path (after register_host_fns) and is callable. We
        // compile a form referencing it — proving load + name resolution + the
        // qualified native dispatch wire up — WITHOUT running it (it bottoms out
        // in the DB-backed list-splits-by-transaction native; execution is
        // covered by the db-gated integration test).
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1
        assert!(
1
            session.symbols.contains("SPLIT:LIST-FOR-TRANSACTION"),
            "host prelude helper not registered"
        );
        // Compile (not run) a real call: lowering emits the qualified-name
        // dispatch + the native import, proving resolution wires up. Host fns
        // never execute at compile time, so no DB is touched.
1
        let program =
1
            Reader::parse("(split:list-for-transaction (car (list-transactions)))").expect("parse");
1
        if let Err(e) = session
1
            .compiler
1
            .compile_eval_with_type(&program, &mut session.symbols)
        {
            panic!("host prelude helper failed to compile: {e:?}");
1
        }
1
    }
    #[tokio::test]
1
    async fn car_of_quoted_constant_list_compiles_on_eval_path() {
        // Regression: CAR/CDR of a quoted CONSTANT list folds on the codegen
        // path but used to trap on the eval-with-type (Session) path because
        // the stack handler called compile_for_stack on the quoted arg, which
        // has no Quote arm. The stack handlers now const-fold first.
1
        let response = handle_form_smoke("(:id 1 :form (car '(1 2 3)))").await;
1
        assert_eq!(response, "(:id 1 :value 1)");
1
    }
    #[tokio::test]
1
    async fn car_of_quoted_heterogeneous_list_compiles_on_eval_path() {
        // Mixed number + string quoted list — the element is extracted by fold.
1
        let response = handle_form_smoke("(:id 1 :form (car '(7 \"x\")))").await;
1
        assert_eq!(response, "(:id 1 :value 7)");
1
    }
    #[tokio::test]
1
    async fn car_of_cdr_of_quoted_constant_compiles_on_eval_path() {
        // CDR folds to the quoted tail, then CAR extracts its head — exercises
        // the cdr fold-first path feeding car.
1
        let response = handle_form_smoke("(:id 1 :form (car (cdr '(1 2 3))))").await;
1
        assert_eq!(response, "(:id 1 :value 2)");
1
    }
    #[tokio::test]
1
    async fn cdr_of_quoted_constant_renders_tail_on_eval_path() {
        // The bare-CDR case (no enclosing CAR): folds to a quoted tail and
        // renders to its printed form rather than trapping.
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (cdr '(1 2 3)))").await,
            "(:id 1 :value \"(2 3)\")"
        );
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (cdr '(1)))").await,
1
            "(:id 1 :value NIL)"
1
        );
1
    }
    #[tokio::test]
1
    async fn car_of_quoted_compound_and_symbol_heads_render_as_data() {
        // A compound or symbol head is quoted DATA, not code — it renders to
        // its printed form (not resolved as a call / variable).
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (car '((1 2) 3)))").await,
            "(:id 1 :value \"(1 2)\")"
        );
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (car '(x y)))").await,
1
            "(:id 1 :value \"X\")"
1
        );
1
    }
    #[tokio::test]
1
    async fn reverse_of_constant_list_renders_on_eval_path() {
        // Regression (same class as CAR/CDR): REVERSE of a constant or
        // runtime-builder list folded but the stack handler rejected the
        // non-runtime-pair result on the eval-with-type path. Now it renders
        // the reversed datum on both surfaces.
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (reverse '(1 2 3)))").await,
            "(:id 1 :value \"(3 2 1)\")"
        );
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (reverse (list 1 2 3)))").await,
            "(:id 1 :value \"(3 2 1)\")"
        );
        // Composition still folds through to the element.
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (car (reverse '(1 2 3))))").await,
1
            "(:id 1 :value 3)"
1
        );
1
    }
    #[tokio::test]
1
    async fn cons_onto_constant_list_renders_on_eval_path() {
        // Regression: CONS with a constant / runtime-builder list cdr trapped
        // in push_pair_cdr on the eval-with-type path. A fully-constant cons
        // now folds and renders the list datum; a dotted pair renders too.
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (cons 0 '(1 2 3)))").await,
            "(:id 1 :value \"(0 1 2 3)\")"
        );
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (cons 0 (list 1 2 3)))").await,
            "(:id 1 :value \"(0 1 2 3)\")"
        );
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (cons 1 2))").await,
1
            "(:id 1 :value \"(1 . 2)\")"
1
        );
1
    }
    #[tokio::test]
1
    async fn append_of_constant_lists_renders_on_eval_path() {
        // Regression: all-constant APPEND folds to a quoted list; the stack
        // handler used to force it through runtime materialization (which can't
        // represent symbols) instead of rendering the folded datum.
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (append '(1 2) '(3)))").await,
            "(:id 1 :value \"(1 2 3)\")"
        );
1
        assert_eq!(
1
            handle_form_smoke("(:id 1 :form (append '(a b) '(c)))").await,
1
            "(:id 1 :value \"(A B C)\")"
1
        );
1
    }
    #[tokio::test]
1
    async fn universal_prelude_helper_runs_end_to_end() {
        // The universal prelude is loaded on the Session path too; a math:*
        // helper executes through wasm and returns its value. No DB.
1
        let response = handle_form_smoke("(:id 9 :form (math:square 9))").await;
1
        assert_eq!(response, "(:id 9 :value 81)");
1
    }
    #[tokio::test]
1
    async fn pp_form_at_value_position_returns_string() {
        // Exercises compile_pp_for_stack — the path nms / emacs see
        // when `(pp 42)` is the request form.
1
        let resp = handle_form_smoke("(:id 7 :form (pp 42))").await;
1
        assert!(resp.contains(":id 7"), "{resp}");
1
        assert!(resp.contains("\"42\""), "{resp}");
1
    }
    #[tokio::test]
1
    async fn describe_form_at_value_position_returns_doc() {
        // compile_describe_for_stack — same shape.
1
        let resp = handle_form_smoke("(:id 8 :form (describe '+))").await;
1
        assert!(resp.contains(":id 8"), "{resp}");
1
        assert!(!resp.contains(":error"), "{resp}");
1
    }
    #[tokio::test]
1
    async fn apropos_form_at_value_position_returns_list() {
1
        let resp = handle_form_smoke("(:id 9 :form (apropos \"entity\"))").await;
1
        assert!(resp.contains(":id 9"), "{resp}");
1
        assert!(!resp.contains(":error"), "{resp}");
1
    }
    #[tokio::test]
1
    async fn deftest_form_at_value_position_returns_quoted_name() {
1
        let resp = handle_form_smoke("(:id 10 :form (deftest sanity (assert-equal 1 1)))").await;
1
        assert!(resp.contains(":id 10"), "{resp}");
1
        assert!(!resp.contains(":error"), "{resp}");
1
    }
    #[tokio::test]
1
    async fn assert_equal_pass_form_at_value_position() {
1
        let resp = handle_form_smoke("(:id 11 :form (assert-equal 2 2))").await;
1
        assert!(resp.contains(":id 11"), "{resp}");
1
        assert!(!resp.contains(":error"), "{resp}");
1
    }
    #[tokio::test]
1
    async fn assert_equal_fail_surfaces_as_error() {
        // assert_equal returns Err on mismatch; Session maps to
        // :code compile error envelope (it's a NomiError::Compile).
1
        let resp = handle_form_smoke("(:id 12 :form (assert-equal 1 2))").await;
1
        assert!(resp.contains(":id 12"), "{resp}");
1
        assert!(resp.contains(":error"), "{resp}");
1
    }
    #[tokio::test]
1
    async fn coverage_dump_lists_called_natives() {
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1
        let _ = session
1
            .handle_form("(:id 1 :form (rpc-protocol-version))")
1
            .await;
1
        let dump = session.handle_form("(:id 2 :form (coverage-dump))").await;
1
        assert!(dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
1
        assert!(dump.contains(":id 2"), "{dump}");
1
    }
    #[tokio::test]
1
    async fn coverage_dump_reports_pre_warmed_natives() {
        // Session::new pre-compiles every zero-arg native fn (phase 4
        // fast-path stubs), so coverage-dump is non-empty even before
        // a user form lands. This is the desired semantic: it
        // reflects compile-time reference counts, including pre-warm
        // compilations, which is what the parity contract gates on.
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1
        let dump = session.handle_form("(:id 1 :form (coverage-dump))").await;
1
        assert!(dump.contains(":id 1"), "{dump}");
1
        assert!(dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
1
    }
    #[tokio::test]
1
    async fn interrupt_does_not_persist_across_forms() {
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1
        session.interrupt_handle().interrupt();
1
        let _ = session.handle_form("(:id 11 :form (+ 1 2))").await;
1
        let response = session.handle_form("(:id 12 :form (+ 1 2))").await;
1
        assert_eq!(response, "(:id 12 :value 3)");
1
    }
    #[tokio::test]
1
    async fn session_state_persists_across_forms() {
1
        let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
1
        let defun = session
1
            .handle_form("(:id 1 :form (defun double (x) (* 2 x)))")
1
            .await;
1
        assert!(defun.contains(":id 1"));
1
        let call = session.handle_form("(:id 2 :form (double 21))").await;
1
        assert_eq!(call, "(:id 2 :value 42)");
1
    }
    #[tokio::test]
1
    async fn fraction_results_format_canonically() {
        // A fractional (Scalar) literal renders canonically as `n/d`. (Integer
        // `(/ 1 4)` is now Index division → 0 per ADR-0028; the canonical
        // fraction idiom is the `1/4` Scalar literal.)
1
        let response = handle_form_smoke("(:id 3 :form 1/4)").await;
1
        assert_eq!(response, "(:id 3 :value 1/4)");
1
    }
    #[test]
1
    fn nomi_runtime_value_carries_through() {
1
        let value = parse_to_value("(+ 0.5 0.25)").unwrap();
1
        assert_eq!(value, Value::Number(Fraction::new(3, 4)));
1
    }
    #[tokio::test]
1
    async fn calls_meta_native_from_nomiscript_source() {
1
        let response = handle_form_smoke("(:id 1 :form (rpc-protocol-version))").await;
1
        let expected_version = crate::natives::meta::PROTOCOL_VERSION;
1
        assert_eq!(response, format!("(:id 1 :value {expected_version})"));
1
    }
    #[tokio::test]
1
    async fn calls_server_get_version_from_nomiscript_source() {
1
        let response = handle_form_smoke("(:id 1 :form (get-version))").await;
        // GIT_HASH is baked at server crate build time via env!. We don't
        // assert its exact value (changes per build) — just that the
        // envelope round-trips a non-empty :value string.
1
        assert!(
1
            response.starts_with("(:id 1 :value \""),
            "expected string response, got: {response}"
        );
1
        assert!(response.ends_with("\")"));
1
    }
    #[tokio::test]
1
    async fn calls_server_get_build_date_from_nomiscript_source() {
1
        let response = handle_form_smoke("(:id 2 :form (get-build-date))").await;
1
        assert!(
1
            response.starts_with("(:id 2 :value \""),
            "expected string response, got: {response}"
        );
1
        assert!(response.ends_with("\")"));
1
    }
    #[tokio::test]
1
    async fn cons_list_surfaces_as_printable_string() {
        // First WasmGC sub-slice: eval-mode `(cons ...)` chains now
        // capture through pending_string instead of erroring at compile
        // time. The result is a textual `(1 2 3)` value the emacs client
        // can (read) back into a real list. Heterogeneous car types ride
        // a follow-up slice once Pair/Vector/Closure/Struct share a
        // tagged union — today's cons cell stores i32 payloads only.
1
        let response = handle_form_smoke("(:id 12 :form (cons 1 (cons 2 (cons 3 nil))))").await;
1
        assert!(
1
            response.contains(":value \"(1 2 3)\""),
1
            "expected :value \"(1 2 3)\", got: {response}"
1
        );
1
    }
    #[tokio::test]
1
    async fn count_native_cannot_mix_with_ratio_arithmetic() {
        // account-count returns i32 (a count / Index, not a Scalar). Mixing it
        // with a fractional Scalar literal must fail to compile — the design
        // forbids accidental arithmetic across the Index/Scalar strata. (An
        // integer literal like `10` is itself an Index now (ADR-0028), so
        // `(+ 10 (account-count))` is valid Index arithmetic; the genuine
        // stratum clash needs a fractional `1/2` Scalar operand.) The explicit
        // `index->scalar` bridge is the only legal crossing.
1
        let response = handle_form_smoke("(:id 11 :form (+ 1/2 (account-count)))").await;
1
        assert!(response.contains(":code compile"), "got: {response}");
1
        assert!(
1
            response.contains("scalar") && response.contains("index"),
1
            "expected Index/Scalar stratum-separation error, got: {response}"
1
        );
1
    }
    #[tokio::test]
1
    async fn get_commodity_with_non_uuid_arg_falls_back_to_symbol_lookup() {
        // get-commodity now accepts a uuid OR a symbol (mirroring get-account's
        // name fallback), so a non-uuid arg is NO LONGER an "invalid uuid"
        // error — it's treated as a symbol and routed to a DB lookup. On this
        // no-DB smoke harness that lookup surfaces a runtime DB-access error
        // (not a parse error); a DB-backed test
        // (`get_commodity_resolves_by_symbol` in tests-integration) covers the
        // successful resolution.
1
        let response = handle_form_smoke("(:id 9 :form (get-commodity \"USD\"))").await;
1
        assert!(response.contains(":id 9"), "got: {response}");
1
        assert!(
1
            response.contains(":code runtime") && response.contains("get-commodity"),
            "expected a get-commodity runtime error (symbol path hits the DB), got: {response}"
        );
1
        assert!(
1
            !response.contains("invalid uuid"),
1
            "a non-uuid arg must no longer short-circuit as an invalid-uuid error: {response}"
1
        );
1
    }
    #[test]
1
    fn meta_native_unknown_in_script_mode_compile() {
        // host_fns are only registered in eval-mode contexts; the compiler
        // built without with_host_fns shouldn't see them. Sanity that the
        // mode flag actually gates the registration.
        use nomiscript::CompileMode;
1
        let mut compiler = Compiler::new();
1
        let mut symbols = SymbolTable::with_builtins();
1
        let program = nomiscript::Reader::parse("(rpc-protocol-version)").unwrap();
1
        let result = compiler.compile_with_mode(&program, &mut symbols, CompileMode::Script);
1
        assert!(
1
            result.is_err(),
            "host fn should not be callable when compiler has no specs"
        );
1
    }
}