camel-language-js 0.47.0

JavaScript language plugin for camel-language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
//! Dedicated worker thread for Boa JS evaluation.
//!
//! All JS execution for one [`BoaEngine`](super::boa::BoaEngine)
//! limits-configuration runs on a single `camel-js-worker` OS thread. Jobs
//! arrive over a bounded `sync_channel(1024)`; each call gets its own reply
//! channel (`sync_channel(1)`) and blocks on it.
//!
//! Realm strategy: the worker owns ONE stable realm per generation. Each
//! eval re-enters that realm, scrubs configurable global keys added since
//! the baseline, installs fresh `console` and `camel` globals, verifies
//! the protected invariants (`camel`/`console` present as objects, `eval`
//! identity), and runs the source inside the wrapper
//! `eval("<escaped source>")`, whose compiled `Script` is cached in a
//! bounded LRU keyed by the full source bytes (cap
//! 256). The wrapper gives every eval a fresh declarative environment
//! (lexical `let`/`const` declarations do not leak across evals). After
//! the eval — on success, error, and limit-failure paths alike — the named
//! integrity set (global `globalThis` own keys, the `eval` function, and
//! the prototypes of `Object`, `Array`, and `Function`; see
//! `integrity.rs` for the measured Requirement-4 shrink) is verified
//! against a baseline captured right after the initial `camel`/`console`
//! install. Any drift, a failed install-verify, or a panic recycles the whole realm (fresh realm, fresh baseline,
//! dropped wrapper cache); the worker never attempts restoration. State
//! OUTSIDE the named integrity set (for example `Date.prototype`
//! mutations, retained object graphs, engine-internal state) may survive
//! across evals, exchanges, and routes until a recycle — see the crate
//! `CONTEXT.md` "Sandbox posture" section for the honest contract.
//!
//! `Validate` jobs are parse-only: the raw source goes through
//! `Script::parse` on the worker's persistent context (no wrapper — the
//! wrapper of an invalid source is itself valid JS and would swallow the
//! parse error) and is never evaluated, so validation creates no global or
//! lexical bindings and the stable realm stays clean.
//!
//! Panic self-heal: every job attempt runs behind `catch_unwind`; a panic is
//! answered with an execution error and the worker rebuilds its full state
//! (fresh `Context` + realm, empty script cache) before the next job, so one
//! bad evaluation cannot brick the worker.
//!
//! Worker-thread death leaves a stale handle behind; later jobs fail with
//! `"JS worker unavailable"`. Recreating the `JsLanguage` is the recovery
//! path.

use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;
use std::time::Instant;

use boa_engine::property::PropertyKey;
use boa_engine::realm::Realm;
use boa_engine::{Context, JsObject, JsValue, Script, Source, js_string};

use super::boa::{ResolvedJsLimits, resolve_js_limits};
use super::integrity::IntegrityBaseline;
use crate::{
    bindings,
    engine::{JsEvalResult, JsExchange},
    error::JsLanguageError,
    value::js_to_value,
};

#[cfg(test)]
use serde_json::Value;

/// Bounded capacity of the job queue feeding the worker thread.
const JOB_CHANNEL_CAPACITY: usize = 1024;

/// Bounded number of compiled wrapper scripts kept by [`ScriptCache`].
const SCRIPT_CACHE_CAPACITY: usize = 256;

/// A unit of work for the JS worker thread.
pub(super) enum JsJob {
    Eval {
        source: Arc<str>,
        exchange: JsExchange,
        timeout_ms: u64,
        enqueued: Instant,
        reply: mpsc::SyncSender<Result<JsEvalResult, JsLanguageError>>,
    },
    Validate {
        source: Arc<str>,
        reply: mpsc::SyncSender<Result<(), JsLanguageError>>,
    },
    #[cfg(test)]
    TestBlock {
        millis: u64,
        reply: mpsc::SyncSender<Result<JsEvalResult, JsLanguageError>>,
    },
    #[cfg(test)]
    TestPanic {
        reply: mpsc::SyncSender<Result<JsEvalResult, JsLanguageError>>,
    },
}

/// Handle to the single `camel-js-worker` thread for one limits-configuration.
#[derive(Debug)]
pub(super) struct JsWorkerHandle {
    tx: mpsc::SyncSender<JsJob>,
}

impl JsWorkerHandle {
    /// Spawn the `camel-js-worker` thread.
    ///
    /// If thread creation fails, the job receiver drops with the closure and
    /// every later [`send`](JsWorkerHandle::send) fails — callers surface
    /// `"JS worker unavailable"`.
    pub(super) fn spawn(limits: camel_language_api::JsLimitsConfig) -> JsWorkerHandle {
        let (tx, rx) = mpsc::sync_channel(JOB_CHANNEL_CAPACITY);
        let _ = std::thread::Builder::new()
            .name("camel-js-worker".to_string())
            .spawn(move || worker_loop(rx, limits));
        JsWorkerHandle { tx }
    }

    /// Queue a job. `Err` means the worker thread is gone.
    pub(super) fn send(&self, job: JsJob) -> Result<(), JsLanguageError> {
        self.tx.send(job).map_err(|_| worker_unavailable())
    }
}

/// The error surfaced when the worker thread is gone (stale handle).
pub(super) fn worker_unavailable() -> JsLanguageError {
    JsLanguageError::Execution {
        message: "JS worker unavailable".to_string(),
    }
}

/// Escape `src` into the body of a valid JS double-quoted string literal
/// (without the surrounding quotes), such that parsing the literal yields
/// exactly `src` for arbitrary Unicode input.
///
/// Backslash is escaped first, then the `"` delimiter, then control
/// characters: LF as `\n`, CR as `\r`, tab as `\t`, U+2028/U+2029 (line/paragraph
/// separators) as `\u2028`/`\u2029`, and every other `char` below U+0020 as
/// `\uXXXX`. No other transformations.
fn escape_js_string_literal(src: &str) -> String {
    let mut out = String::with_capacity(src.len() + 2);
    for c in src.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\u{2028}' => out.push_str("\\u2028"),
            '\u{2029}' => out.push_str("\\u2029"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out
}

/// Bounded LRU cache of compiled wrapper scripts.
///
/// Keyed by the full source bytes — `Arc<str>` equality compares content by
/// Rust semantics, so two evals share an entry only when their sources are
/// byte-identical. Values are realm-bound `Script`s: they live and die with
/// the worker thread's stable realm and never cross a thread boundary.
struct ScriptCache {
    map: HashMap<Arc<str>, boa_engine::Script>,
    order: VecDeque<Arc<str>>,
    cap: usize,
}

impl ScriptCache {
    fn new(cap: usize) -> Self {
        Self {
            map: HashMap::new(),
            order: VecDeque::new(),
            cap,
        }
    }

    /// Returns the compiled script for `key`, refreshing its recency.
    fn get(&mut self, key: &Arc<str>) -> Option<&boa_engine::Script> {
        if self.map.contains_key(key) {
            self.touch(key);
        }
        self.map.get(key)
    }

    /// Inserts `script` under `key`, evicting the least-recently-used entry
    /// while the cache is at capacity.
    fn insert(&mut self, key: Arc<str>, script: boa_engine::Script) {
        if self.map.contains_key(&key) {
            self.touch(&key);
            self.map.insert(key, script);
            return;
        }
        while self.map.len() >= self.cap {
            match self.order.pop_front() {
                Some(evicted) => {
                    self.map.remove(&evicted);
                }
                None => break,
            }
        }
        self.order.push_back(key.clone());
        self.map.insert(key, script);
    }

    /// Number of cached scripts (test assertions; production reads only
    /// observe it via [`ScriptCache::clear`] bookkeeping).
    #[allow(dead_code)]
    fn len(&self) -> usize {
        self.map.len()
    }

    /// Drops every cached script (the wrapper scripts are realm-bound and
    /// die with the recycled realm).
    fn clear(&mut self) {
        self.map.clear();
        self.order.clear();
    }

    /// Moves `key` to the most-recently-used position.
    fn touch(&mut self, key: &Arc<str>) {
        if let Some(pos) = self.order.iter().position(|k| k == key)
            && let Some(k) = self.order.remove(pos)
        {
            self.order.push_back(k);
        }
    }
}

/// The freshly installed per-eval bindings, returned by
/// [`install_bindings`] and identity-verified by
/// [`WorkerState::install_verify`].
struct InstalledGlobals {
    console: JsObject,
    camel: JsObject,
}

/// Why an [`install_bindings`] attempt failed.
enum InstallError {
    /// Deterministic exchange-data failure (conversion error): recurs
    /// identically on a fresh realm, so it is surfaced directly without a
    /// recycle-retry.
    Data(JsLanguageError),
    /// A global `console`/`camel` set was REJECTED (for example a spoofed
    /// non-writable binding): the realm is poisoned and must be recycled.
    Rejected,
}

/// Install fresh `console` (tracing-backed) and `camel` (exchange-bound)
/// globals on the currently active realm. Both global sets use
/// `throw = true` — a rejected set (a silent `Ok(false)` under
/// `throw = false`) is reported as [`InstallError::Rejected`] so the caller
/// can recycle the realm instead of evaluating against a spoofed binding.
fn install_bindings(
    ctx: &mut Context,
    exchange: &JsExchange,
) -> Result<InstalledGlobals, InstallError> {
    let console = bindings::register_console(ctx).map_err(|_| InstallError::Rejected)?;
    let camel_obj = bindings::build_camel_global(exchange, ctx).map_err(|e| {
        InstallError::Data(JsLanguageError::Execution {
            message: e.to_string(),
        })
    })?;
    ctx.global_object()
        .set(
            js_string!("camel"),
            JsValue::from(camel_obj.clone()),
            true,
            ctx,
        )
        .map_err(|_| InstallError::Rejected)?;
    Ok(InstalledGlobals {
        console,
        camel: camel_obj,
    })
}

/// Worker-thread-local engine state. Never crosses a thread boundary; tests
/// may build it directly and call the same methods the worker loop calls.
pub(super) struct WorkerState {
    /// Persistent context shell hosting the stable realm.
    ctx: Context,
    /// The stable realm: entered for every eval, recycled in whole on
    /// integrity drift, install-verify failure, or panic.
    realm: Realm,
    /// Compiled wrapper scripts keyed by full source bytes.
    cache: ScriptCache,
    /// Resolved runtime limits; re-applied when the realm is recycled.
    limits: ResolvedJsLimits,
    /// Integrity baseline for the current realm generation, captured after
    /// the initial `camel`/`console` install.
    baseline: IntegrityBaseline,
    /// Number of realm recycles (drift, install-verify failure, or panic
    /// rebuild) since construction. A plain field, deliberately NOT a
    /// process-wide static: parallel tests share one binary and a global
    /// counter would flake.
    #[allow(dead_code)]
    recycle_count: u64,
}

impl WorkerState {
    /// Build the worker state: fresh `Context` with resolved runtime limits,
    /// its default realm kept as the stable realm (entered explicitly for
    /// clarity), an empty wrapper-script cache, a minimal initial
    /// `camel`/`console` install, and the integrity baseline captured
    /// AFTER that install.
    ///
    /// Soundness: the baseline must include `camel`/`console` in its global
    /// key set so the per-eval reinstall (which replaces their VALUES each
    /// eval) cannot count as drift — their values are excluded from
    /// identity comparison, but their key presence is checked. On the
    /// (practically impossible) initial-install failure the baseline stays
    /// empty, the next eval's install-verify fails, and the recycle path
    /// rebuilds a healthy state.
    pub(super) fn new(limits: ResolvedJsLimits) -> Self {
        let mut ctx = Context::default();
        apply_runtime_limits(&mut ctx, &limits);
        let realm = ctx.realm().clone();
        ctx.enter_realm(realm.clone());
        let mut state = Self {
            ctx,
            realm,
            cache: ScriptCache::new(SCRIPT_CACHE_CAPACITY),
            limits,
            baseline: IntegrityBaseline::empty(),
            recycle_count: 0,
        };
        if install_bindings(&mut state.ctx, &JsExchange::default()).is_ok() {
            state.baseline = IntegrityBaseline::capture(&mut state.ctx);
        }
        state
    }

    /// Run one `Eval` job end to end: deadline check → scrub stray global
    /// keys → install fresh `console`/`camel` → install-verify (on failure:
    /// recycle the realm, recheck the deadline, retry install+verify once;
    /// a second consecutive failure fails the job after one more recycle)
    /// → evaluate the cached wrapper → convert + extract → integrity-check
    /// (drift → recycle).
    ///
    /// Scrub and the integrity check run on every completion path —
    /// success, script error, and limit failure (the panic path is covered
    /// by the dispatch loop's `catch_unwind` plus a full state rebuild,
    /// which subsumes recycling). Wrapper and inner-eval failures map to
    /// [`JsLanguageError::Execution`], exactly as the pre-worker `ctx.eval`
    /// path mapped them.
    pub(super) fn run_eval(
        &mut self,
        source: &str,
        exchange: JsExchange,
        deadline: Instant,
    ) -> Result<JsEvalResult, JsLanguageError> {
        // Job deadline. The dequeue-time check in the dispatch loop already
        // skipped expired jobs; this re-check guards the budget for the
        // recycle-retry path inside this method.
        if Instant::now() > deadline {
            return Err(JsLanguageError::Execution {
                message: "JS execution timeout".to_string(),
            });
        }
        // Re-enter the stable realm explicitly: the persistent context must
        // always evaluate against the same realm.
        self.ctx.enter_realm(self.realm.clone());

        // Install fresh bindings and verify the protected invariants. A
        // rejected global set (spoofed non-writable binding) or a failed
        // verify recycles the realm, rechecks the job deadline (a recycle
        // costs on the order of a fresh realm build), and retries
        // install+verify exactly once. Deterministic exchange-data errors
        // are surfaced directly — they recur identically on a fresh realm.
        let mut verified = false;
        for attempt in 0..2 {
            self.scrub_globals();
            match install_bindings(&mut self.ctx, &exchange) {
                Ok(installed) => {
                    if self.install_verify(&installed) {
                        verified = true;
                        break;
                    }
                }
                Err(InstallError::Data(e)) => return Err(e),
                Err(InstallError::Rejected) => {}
            }
            self.recycle();
            if attempt == 0 && Instant::now() > deadline {
                return Err(JsLanguageError::Execution {
                    message: "JS execution timeout".to_string(),
                });
            }
        }
        if !verified {
            // Second consecutive install/verify failure. The loop's last
            // `recycle` already ran; surface the failure to the caller.
            return Err(JsLanguageError::Execution {
                message: "JS engine realm failed verification".to_string(),
            });
        }

        let outcome = self.eval_cached(source);

        // Integrity verification runs after the eval regardless of its
        // outcome; drift recycles the realm for the NEXT job. The current
        // job's result is still reported.
        if !self.integrity_check() {
            self.recycle();
        }
        outcome
    }

    /// Evaluate the cached wrapper for `source`, convert the completion
    /// value, and extract the exchange state.
    ///
    /// Cache lookup by full source bytes; on a miss, compile the wrapper
    /// `eval("<escaped source>")` (parse is pure — it creates no bindings),
    /// evaluate it — the wrapper's completion value is the inner eval's
    /// completion value — and cache the compiled script.
    fn eval_cached(&mut self, source: &str) -> Result<JsEvalResult, JsLanguageError> {
        // Field split so the cache and the context can be borrowed disjointly.
        let WorkerState { ctx, cache, .. } = self;

        let key: Arc<str> = Arc::from(source);

        let outcome = match cache.get(&key) {
            Some(script) => script.evaluate(ctx),
            None => {
                let wrapper = format!("eval(\"{}\")", escape_js_string_literal(source));
                let script = Script::parse(Source::from_bytes(wrapper.as_bytes()), None, ctx)
                    .map_err(|e| JsLanguageError::Execution {
                        message: e.to_string(),
                    })?;
                let outcome = script.evaluate(ctx);
                cache.insert(key, script);
                outcome
            }
        };

        let result = outcome.map_err(|e| JsLanguageError::Execution {
            message: e.to_string(),
        })?;

        let return_value = js_to_value(&result, ctx)?;

        // Extract modified exchange state.
        let modified = bindings::extract_camel_state(ctx)?;

        Ok(JsEvalResult {
            return_value,
            headers: modified.headers,
            body: modified.body,
            properties: modified.properties,
        })
    }

    /// Delete every own key of the global object that is not in the
    /// baseline key set, ignoring delete failures — a non-configurable key
    /// remains and is caught as drift by the integrity check, which
    /// recycles the realm.
    ///
    /// Redundant by design: any key scrub can remove would also register
    /// as drift in the integrity check and trigger a recycle. The spec
    /// blesses keeping the scrub; it is this trivial loop (revisit at
    /// archive).
    fn scrub_globals(&mut self) {
        let global = self.ctx.global_object();
        let Ok(keys) = global.own_property_keys(&mut self.ctx) else {
            return;
        };
        for key in keys {
            if !self.baseline.contains_global_key(&key) {
                let _ = global.delete_property_or_throw(key, &mut self.ctx);
            }
        }
    }

    /// Verify the protected invariants for the next eval: the global OWN
    /// values of `camel` and `console` strictly equal the freshly installed
    /// bindings (catches a spoofed non-writable binding, whose rejected
    /// replacement leaves the spoof in place, and accessor traps whose set
    /// "succeeds" without replacing the value), and the global `eval` own
    /// value strictly equals the baseline-captured `eval` function.
    /// `false` on any miss.
    ///
    /// Own-key lookups, deliberately not `get`: the global object's
    /// prototype chain is user-mutable, so a chain lookup could
    /// false-positive on an inherited binding after a deletion.
    fn install_verify(&mut self, installed: &InstalledGlobals) -> bool {
        let Some(eval_object) = self.baseline.eval_object() else {
            return false;
        };
        let eval_object = eval_object.clone();
        let global = self.ctx.global_object();
        for (key, expected) in [
            (js_string!("camel"), JsValue::from(installed.camel.clone())),
            (
                js_string!("console"),
                JsValue::from(installed.console.clone()),
            ),
            (js_string!("eval"), JsValue::from(eval_object)),
        ] {
            let Some(desc) = global.borrow().properties().get(&PropertyKey::from(key)) else {
                return false;
            };
            let Some(value) = desc.value() else {
                return false;
            };
            if !value.strict_equals(&expected) {
                return false;
            }
        }
        true
    }

    /// Verify the named integrity set against the baseline (see
    /// [`IntegrityBaseline::verify`]): global own-key set (including symbol
    /// keys) and `eval` value identity, plus each root's key set,
    /// descriptor hash, and prototype identity. Any difference → `false`.
    fn integrity_check(&mut self) -> bool {
        let WorkerState { ctx, baseline, .. } = self;
        baseline.verify(ctx)
    }

    /// Replace the current realm with a fresh one: create it on the
    /// persistent context, enter it, re-apply the runtime limits, re-install
    /// the minimal `camel`/`console` bindings, re-capture the integrity
    /// baseline, and drop the realm-bound wrapper cache. On `create_realm`
    /// failure (or a failed minimal install on the new realm), rebuild the
    /// whole `WorkerState`. Recycling is the entire recovery story — the
    /// worker never attempts restoration of drifted state.
    fn recycle(&mut self) {
        self.recycle_count += 1;
        let limits = self.limits.clone();
        if let Ok(realm) = self.ctx.create_realm() {
            // `create_realm` binds default globals on the new realm but
            // RETURNS with the OLD realm re-activated — enter the new one.
            let _previous = self.ctx.enter_realm(realm.clone());
            self.realm = realm;
            apply_runtime_limits(&mut self.ctx, &self.limits);
            if install_bindings(&mut self.ctx, &JsExchange::default()).is_ok() {
                self.baseline = IntegrityBaseline::capture(&mut self.ctx);
                self.cache.clear();
                return;
            }
        }
        // `create_realm` failed or the minimal install on the fresh realm
        // failed: rebuild everything from scratch, preserving the count so
        // tests can still observe that a recycle happened.
        let count = self.recycle_count;
        *self = WorkerState::new(limits);
        self.recycle_count = count;
    }

    /// Parse-only validation on the worker's persistent context. The RAW
    /// source goes through `Script::parse` — no wrapper: the wrapper of an
    /// invalid source is itself valid JS, which would swallow the parse
    /// error — and is never evaluated, so no global or lexical bindings are
    /// created and the stable realm stays clean. Failures map to
    /// [`JsLanguageError::Parse`].
    pub(super) fn run_validate(&mut self, source: &str) -> Result<(), JsLanguageError> {
        let _script = Script::parse(Source::from_bytes(source.as_bytes()), None, &mut self.ctx)
            .map_err(|e| JsLanguageError::Parse {
                message: e.to_string(),
            })?;
        Ok(())
    }

    /// Sleep for `millis`, then reply with a default result (test-only job).
    #[cfg(test)]
    fn run_test_block(&self, millis: u64) -> Result<JsEvalResult, JsLanguageError> {
        std::thread::sleep(Duration::from_millis(millis));
        Ok(JsEvalResult {
            return_value: Value::Null,
            headers: HashMap::new(),
            body: Value::Null,
            properties: HashMap::new(),
        })
    }

    /// Deliberate panic to exercise the self-heal path (test-only job).
    #[cfg(test)]
    fn run_test_panic(&self) -> Result<JsEvalResult, JsLanguageError> {
        panic!("TestPanic: deliberate worker panic for the self-heal test");
    }
}

/// Apply the resolved runtime limits to a context.
fn apply_runtime_limits(ctx: &mut Context, limits: &ResolvedJsLimits) {
    let runtime_limits = ctx.runtime_limits_mut();
    runtime_limits.set_loop_iteration_limit(limits.max_loop_iterations);
    runtime_limits.set_recursion_limit(limits.max_recursion_depth);
    runtime_limits.set_stack_size_limit(limits.max_stack_size);
}

/// Worker-thread main loop: a thin recv-dispatch shell. All per-job logic
/// lives in [`WorkerState`] methods.
fn worker_loop(rx: mpsc::Receiver<JsJob>, limits: camel_language_api::JsLimitsConfig) {
    let resolved = resolve_js_limits(&limits);
    let mut state = WorkerState::new(resolved);
    while let Ok(job) = rx.recv() {
        let panicked = match job {
            JsJob::Eval {
                source,
                exchange,
                timeout_ms,
                enqueued,
                reply,
            } => {
                // Deadline check at dequeue: an expired job never executes.
                if enqueued.elapsed() > Duration::from_millis(timeout_ms) {
                    let _ = reply.send(Err(JsLanguageError::Execution {
                        message: "JS execution timeout".to_string(),
                    }));
                    continue;
                }
                let deadline = enqueued + Duration::from_millis(timeout_ms);
                let attempt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    state.run_eval(&source, exchange, deadline)
                }));
                reply_outcome(&reply, attempt)
            }
            JsJob::Validate { source, reply } => {
                let attempt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    state.run_validate(&source)
                }));
                reply_outcome(&reply, attempt)
            }
            #[cfg(test)]
            JsJob::TestBlock { millis, reply } => {
                let attempt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    state.run_test_block(millis)
                }));
                reply_outcome(&reply, attempt)
            }
            #[cfg(test)]
            JsJob::TestPanic { reply } => {
                let attempt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    state.run_test_panic()
                }));
                reply_outcome(&reply, attempt)
            }
        };
        if panicked {
            // Self-heal: a panic invalidates the engine state — rebuild the
            // full worker state (fresh `Context` + realm, empty script cache)
            // before the next job. Preserve the recycle count for test
            // observability, matching `recycle()`'s rebuild semantics.
            let count = state.recycle_count;
            state = WorkerState::new(resolve_js_limits(&limits));
            state.recycle_count = count;
        }
    }
}

/// Send the job outcome, never blocking on a gone receiver; returns `true`
/// when the attempt panicked so the loop can rebuild its state.
fn reply_outcome<T>(
    reply: &mpsc::SyncSender<Result<T, JsLanguageError>>,
    attempt: std::thread::Result<Result<T, JsLanguageError>>,
) -> bool {
    match attempt {
        Ok(outcome) => {
            let _ = reply.send(outcome);
            false
        }
        Err(_) => {
            let _ = reply.send(Err(JsLanguageError::Execution {
                message: "JS worker panic recovered".to_string(),
            }));
            true
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::mpsc;

    use camel_language_api::{Exchange, JsLimitsConfig, Language, Message};

    use crate::engine::JsEngine as _;
    use crate::language::JsLanguage;

    /// Run an eval with a generous 5 s deadline (test helper mirroring the
    /// dispatch loop's deadline computation).
    fn eval_now(
        state: &mut WorkerState,
        source: &str,
        exchange: JsExchange,
    ) -> Result<JsEvalResult, JsLanguageError> {
        state.run_eval(source, exchange, Instant::now() + Duration::from_secs(5))
    }

    /// Local `WorkerState` under default limits.
    fn worker_state() -> WorkerState {
        WorkerState::new(resolve_js_limits(&JsLimitsConfig::default()))
    }

    #[test]
    fn expired_queued_job_is_skipped() {
        let handle = JsWorkerHandle::spawn(JsLimitsConfig::default());

        // Occupy the worker for 300 ms so the next job queues behind it.
        let (block_tx, _block_rx) = mpsc::sync_channel(1);
        handle
            .tx
            .send(JsJob::TestBlock {
                millis: 300,
                reply: block_tx,
            })
            .unwrap();

        // Queue an `Eval` whose 1 ms budget expires while the worker is busy.
        let (tx, rx) = mpsc::sync_channel(1);
        handle
            .tx
            .send(JsJob::Eval {
                source: Arc::from("1+1"),
                exchange: JsExchange::default(),
                timeout_ms: 1,
                enqueued: Instant::now(),
                reply: tx,
            })
            .unwrap();

        let reply = rx.recv().unwrap();
        let err = reply.expect_err("expired job must be skipped with an error");
        assert!(
            err.to_string().contains("timeout"),
            "expected timeout error, got: {err}"
        );

        // The worker stays healthy after skipping the expired job.
        let (tx, rx) = mpsc::sync_channel(1);
        handle
            .tx
            .send(JsJob::Eval {
                source: Arc::from("1+1"),
                exchange: JsExchange::default(),
                timeout_ms: 5_000,
                enqueued: Instant::now(),
                reply: tx,
            })
            .unwrap();
        let result = rx.recv().unwrap().unwrap();
        assert_eq!(result.return_value.as_i64().unwrap(), 2);
    }

    #[test]
    fn worker_self_heals_after_panic() {
        let handle = JsWorkerHandle::spawn(JsLimitsConfig::default());

        let (tx, rx) = mpsc::sync_channel(1);
        handle.tx.send(JsJob::TestPanic { reply: tx }).unwrap();
        let reply = rx.recv().unwrap();
        let err = reply.expect_err("TestPanic must produce an error");
        assert!(
            err.to_string().contains("panic"),
            "expected panic error, got: {err}"
        );

        // The worker rebuilt its state and still evaluates normally.
        let (tx, rx) = mpsc::sync_channel(1);
        handle
            .tx
            .send(JsJob::Eval {
                source: Arc::from("1+1"),
                exchange: JsExchange::default(),
                timeout_ms: 5_000,
                enqueued: Instant::now(),
                reply: tx,
            })
            .unwrap();
        let result = rx.recv().unwrap().unwrap();
        assert_eq!(result.return_value.as_i64().unwrap(), 2);
    }

    #[test]
    fn lexical_declarations_are_fresh_per_eval() {
        // NOTE (task 1.2 deviation): the task text specifies the source
        // `let count = (typeof count === 'undefined') ? 0 : count; count++; count`.
        // That source throws `ReferenceError: count is not defined` in Boa 0.22 —
        // the initializer references its own `let` binding while it is still in
        // the temporal dead zone (`typeof` throws; it does not yield
        // `'undefined'`) — on today's fresh-`Context` path as well, so the
        // "both return 1" assertion is unachievable as written. The test below
        // keeps the documented intent (repeated evals of a lexically declaring
        // source return the fresh value; no redeclaration error) with the
        // minimal TDZ-free source. Reported as test-design-gap.
        let engine = super::super::boa::BoaEngine::default();
        let source = "let count = 0; count++; count";

        let r1 = engine.eval(source, JsExchange::default()).unwrap();
        let r2 = engine.eval(source, JsExchange::default()).unwrap();

        assert_eq!(r1.return_value.as_i64().unwrap(), 1);
        assert_eq!(r2.return_value.as_i64().unwrap(), 1);
    }

    #[test]
    fn wrapper_escapes_adversarial_sources() {
        // Expected-value table: source → exact result under today's raw-source
        // semantics (recorded empirically against Boa 0.22). Sources 3–6 are
        // the string-literal forms: as bare tokens they are invalid JS (or
        // reference undefined identifiers), while the recorded expected values
        // (`back\slash`, `trailing\`, …) are the raw-eval results of the
        // quoted forms.
        let table: Vec<(String, String)> = vec![
            (r#"'it\'s'"#.to_string(), "it's".to_string()),
            (r#""quo\"te""#.to_string(), "quo\"te".to_string()),
            (r#""back\\slash""#.to_string(), "back\\slash".to_string()),
            ("\"cr\\rLf\\n\"".to_string(), "cr\rLf\n".to_string()),
            (
                // Raw U+2028/U+2029 bytes inside the literal: the wrapper
                // escape must turn them into \u2028/\u2029 escapes.
                "\"ls\u{2028}ps\u{2029}\"".to_string(),
                "ls\u{2028}ps\u{2029}".to_string(),
            ),
            ("\"trailing\\\\\"".to_string(), "trailing\\".to_string()),
        ];
        let distinct_sources = table.len();

        let mut state = worker_state();
        for (source, expected) in &table {
            for _ in 0..3 {
                let result = eval_now(&mut state, source, JsExchange::default())
                    .unwrap_or_else(|e| panic!("eval must succeed for {source:?}: {e}"));
                assert_eq!(
                    result.return_value.as_str(),
                    Some(expected.as_str()),
                    "adversarial source must round-trip: {source:?}"
                );
            }
        }

        // One wrapper entry per distinct source after 3 repeats each.
        assert_eq!(state.cache.len(), distinct_sources);
    }

    #[test]
    fn invalid_source_maps_to_execution_error() {
        let mut state = worker_state();

        // Eval: the wrapper of an invalid source is itself valid JS, so the
        // failure surfaces at inner-eval time → Execution (never Parse).
        let err = eval_now(&mut state, "let x = {{{", JsExchange::default())
            .expect_err("invalid source must fail eval");
        assert!(
            matches!(err, JsLanguageError::Execution { .. }),
            "eval of invalid source must be Execution, got: {err:?}"
        );

        // Validate: raw-source parse → Parse.
        let verr = state
            .run_validate("let x = {{{")
            .expect_err("invalid source must fail validate");
        assert!(
            matches!(verr, JsLanguageError::Parse { .. }),
            "validate of invalid source must be Parse, got: {verr:?}"
        );
    }

    #[test]
    fn identical_results_across_repeats() {
        let mut state = worker_state();
        let source = "1 + 1";
        let exchange = JsExchange::default();

        let r1 = eval_now(&mut state, source, exchange.clone()).unwrap();
        let r2 = eval_now(&mut state, source, exchange.clone()).unwrap();
        let r3 = eval_now(&mut state, source, exchange).unwrap();

        assert_eq!(r1, r2);
        assert_eq!(r2, r3);
    }

    #[test]
    fn identical_errors_first_and_cached() {
        let mut state = worker_state();
        let source = "throw new Error('boom')";

        let e1 =
            eval_now(&mut state, source, JsExchange::default()).expect_err("first eval must fail");
        let e2 = eval_now(&mut state, source, JsExchange::default())
            .expect_err("cached eval must fail identically");

        // JsLanguageError has no PartialEq: compare via variant + message.
        let message_of = |e: &JsLanguageError| match e {
            JsLanguageError::Execution { message } => message.clone(),
            other => panic!("expected Execution error, got: {other:?}"),
        };
        let m1 = message_of(&e1);
        let m2 = message_of(&e2);
        assert!(m1.contains("boom"), "error must carry the JS message: {m1}");
        assert_eq!(m1, m2, "first and cached error must be identical");
    }

    #[tokio::test]
    async fn route_rebuild_reuses_cache_entry() {
        // WorkerState level: two "expression builds" sharing one engine (the
        // watch-reload shape) reuse one cache entry.
        let mut state = worker_state();
        let source = "1 + 1";
        let r1 = eval_now(&mut state, source, JsExchange::default()).unwrap();
        let r2 = eval_now(&mut state, source, JsExchange::default()).unwrap();
        assert_eq!(r1, r2);
        assert_eq!(state.cache.len(), 1);

        // Engine level: one JsLanguage, create_expression twice, identical
        // evaluated values.
        let lang = JsLanguage::new();
        let expr1 = lang.create_expression("1 + 1").unwrap();
        let expr2 = lang.create_expression("1 + 1").unwrap();
        let exchange = Exchange::new(Message::default());
        let v1 = expr1.evaluate(&exchange).await.unwrap();
        let v2 = expr2.evaluate(&exchange).await.unwrap();
        assert_eq!(v1, v2);
    }

    #[test]
    fn lru_eviction_bounds_cache_at_256() {
        let mut ctx = Context::default();
        let mut cache = ScriptCache::new(256);

        let keys: Vec<Arc<str>> = (0..257)
            .map(|i| Arc::<str>::from(format!("source-{i}").as_str()))
            .collect();
        for key in &keys {
            let script = Script::parse(Source::from_bytes(b"0"), None, &mut ctx).unwrap();
            cache.insert(key.clone(), script);
        }

        assert_eq!(cache.len(), 256, "cache must stay bounded at the cap");
        assert!(cache.get(&keys[0]).is_none(), "oldest key must be evicted");
        assert!(
            cache.get(&keys[256]).is_some(),
            "most recently inserted key must be present"
        );
    }

    // ── Task 1.3: cleanup, install-verify, integrity set, realm recycling ──

    #[test]
    fn global_assignment_does_not_persist() {
        let mut state = worker_state();
        let r = eval_now(&mut state, "globalThis.leak = 42; 0", JsExchange::default()).unwrap();
        assert_eq!(r.return_value.as_i64(), Some(0));

        let r = eval_now(&mut state, "typeof leak", JsExchange::default()).unwrap();
        assert_eq!(
            r.return_value.as_str(),
            Some("undefined"),
            "global assignment must not persist across evals"
        );
    }

    #[test]
    fn poisoned_eval_recovers() {
        // Through one BoaEngine: behavioral proof that `eval = 1` poisoning
        // triggers recovery (install-verify eval-identity check + recycle)
        // BEFORE the next eval runs.
        let engine = super::super::boa::BoaEngine::default();
        let r1 = engine.eval("eval = 1; 0", JsExchange::default()).unwrap();
        assert_eq!(r1.return_value.as_i64(), Some(0));

        let r2 = engine.eval("typeof eval", JsExchange::default()).unwrap();
        assert_eq!(
            r2.return_value.as_str(),
            Some("function"),
            "poisoned eval must be restored via realm recycle before the next eval"
        );
    }

    #[test]
    fn undeletable_global_recycles_realm() {
        let mut state = worker_state();
        eval_now(
            &mut state,
            "Object.defineProperty(globalThis, 'pwn', { value: 1, configurable: false }); 0",
            JsExchange::default(),
        )
        .unwrap();

        let r = eval_now(&mut state, "typeof pwn", JsExchange::default()).unwrap();
        assert_eq!(
            r.return_value.as_str(),
            Some("undefined"),
            "undeletable global key must be detected as drift and the realm recycled"
        );
    }

    #[test]
    fn frozen_prototype_recycles_realm() {
        let mut state = worker_state();
        eval_now(
            &mut state,
            "Object.freeze(Object.prototype); 0",
            JsExchange::default(),
        )
        .unwrap();

        let r = eval_now(
            &mut state,
            "Object.isFrozen(Object.prototype)",
            JsExchange::default(),
        )
        .unwrap();
        assert_eq!(
            r.return_value.as_bool(),
            Some(false),
            "descriptor drift on Object.prototype must recycle the realm"
        );
    }

    #[test]
    fn failed_eval_residue_is_cleaned() {
        let mut state = worker_state();
        let err = eval_now(
            &mut state,
            "globalThis.x = 1; throw new Error('boom')",
            JsExchange::default(),
        );
        assert!(err.is_err(), "eval must fail with the thrown error");

        let r = eval_now(&mut state, "typeof x", JsExchange::default()).unwrap();
        assert_eq!(
            r.return_value.as_str(),
            Some("undefined"),
            "cleanup must run on the error path"
        );
    }

    #[test]
    fn limit_failure_residue_is_cleaned() {
        let mut state = worker_state();
        let err = eval_now(
            &mut state,
            "globalThis.x = 1; while (true) {}",
            JsExchange::default(),
        );
        assert!(
            err.is_err(),
            "infinite loop must trip the loop-iteration limit"
        );

        let r = eval_now(&mut state, "typeof x", JsExchange::default()).unwrap();
        assert_eq!(
            r.return_value.as_str(),
            Some("undefined"),
            "cleanup must run on the limit-failure path"
        );
    }

    #[test]
    fn console_and_camel_are_fresh_each_eval() {
        let mut state = worker_state();
        eval_now(
            &mut state,
            "console.log = function(){}; camel.headers = 1; 0",
            JsExchange::default(),
        )
        .unwrap();

        // The re-installed console must still route to tracing (fresh
        // console.log) and the re-installed camel must serve this exchange.
        let exchange = JsExchange::from_headers_body_properties(
            [("k".to_string(), serde_json::json!("v"))]
                .into_iter()
                .collect(),
            serde_json::Value::Null,
            HashMap::new(),
        );
        let r = eval_now(
            &mut state,
            "console.log('x'); camel.headers.get('k')",
            exchange,
        )
        .unwrap();
        assert_eq!(
            r.return_value.as_str(),
            Some("v"),
            "camel/console must be re-installed fresh with the new exchange"
        );
    }

    #[test]
    fn spoofed_non_writable_binding_recycles() {
        // Spoof: replace the camel global with a non-writable binding. No
        // key-set drift occurs (camel/console values are excluded from
        // identity comparison), so the integrity check cannot catch it —
        // install-verify must, by strict-equaling the global own values
        // against the freshly installed bindings, and recycle the realm
        // at the next eval's start.
        let mut state = worker_state();
        eval_now(
            &mut state,
            "Object.defineProperty(globalThis, 'camel', { value: { pwn: 1 }, writable: false }); 0",
            JsExchange::default(),
        )
        .unwrap();

        let r = eval_now(&mut state, "camel.pwn", JsExchange::default()).unwrap();
        assert!(
            r.return_value.is_null(),
            "spoofed non-writable camel must be replaced via realm recycle before the eval, got {:?}",
            r.return_value
        );

        // The recycled realm serves the real camel binding.
        let exchange = JsExchange::from_headers_body_properties(
            HashMap::new(),
            serde_json::Value::String("payload".to_string()),
            HashMap::new(),
        );
        let r = eval_now(&mut state, "camel.body", exchange).unwrap();
        assert_eq!(r.return_value.as_str(), Some("payload"));
    }

    #[test]
    fn date_prototype_persists_until_recycle() {
        // Declared non-guarantee: `Date.prototype` is OUTSIDE the named
        // integrity set, so its mutation persists between evals of one realm
        // generation and disappears after a drift-triggered recycle.
        let mut state = worker_state();
        eval_now(
            &mut state,
            "Date.prototype.probe = 1; 0",
            JsExchange::default(),
        )
        .unwrap();

        let r = eval_now(&mut state, "new Date().probe", JsExchange::default()).unwrap();
        assert_eq!(
            r.return_value.as_i64(),
            Some(1),
            "state outside the integrity set persists within a realm generation"
        );

        // Drift trigger: an undeletable global recycles the realm.
        eval_now(
            &mut state,
            "Object.defineProperty(globalThis, 'pwn', { value: 1, configurable: false }); 0",
            JsExchange::default(),
        )
        .unwrap();

        let r = eval_now(&mut state, "new Date().probe", JsExchange::default()).unwrap();
        assert!(
            r.return_value.is_null(),
            "recycled realm must drop the persisted Date.prototype state, got {:?}",
            r.return_value
        );
    }

    #[test]
    fn no_recycle_storm_on_steady_state() {
        // camel/console are re-installed (new object identity) every eval;
        // that churn must NOT register as drift.
        let mut state = worker_state();
        let exchange = JsExchange::from_headers_body_properties(
            HashMap::new(),
            serde_json::Value::String("payload".to_string()),
            HashMap::new(),
        );
        for _ in 0..100 {
            eval_now(&mut state, "camel.body", exchange.clone()).unwrap();
        }
        assert_eq!(
            state.recycle_count, 0,
            "steady-state evals must not trigger realm recycles"
        );
    }

    #[test]
    #[ignore = "slow test: timing diagnostic; run via cargo test -p camel-language-js integrity_detector -- --ignored --nocapture; printed mean feeds the Req-4 shrink decision"]
    fn integrity_detector_cost_diagnostic() {
        let mut state = worker_state();
        // Warm up outside the measurement window.
        for _ in 0..20 {
            state.integrity_check();
        }
        let start = Instant::now();
        for _ in 0..200 {
            state.integrity_check();
        }
        let mean_us = start.elapsed().as_micros() as f64 / 200.0;
        println!(
            "integrity_detector_cost_diagnostic: mean {mean_us:.2} µs per integrity_check over 200 calls"
        );
    }
}