-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathlib.rs
More file actions
1150 lines (1127 loc) · 48.6 KB
/
Copy pathlib.rs
File metadata and controls
1150 lines (1127 loc) · 48.6 KB
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
//! chidori-js — a pure-Rust JavaScript engine with deterministic-replay durable
//! execution. No C, no `boa_engine` dependency. The front end is `oxc` (parser +
//! AST); everything below — bytecode compiler, VM, object model, GC, builtins,
//! microtask scheduler, and the replay runtime — is implemented here.
//!
//! See `docs/architecture.md` and `docs/conformance.md` for the engine design
//! and Test262 conformance status.
// The engine is the sandbox: memory safety is a security property here, not a
// style preference (docs/sandbox-model.md). Enforce the zero-`unsafe` claim at
// compile time rather than by convention.
#![forbid(unsafe_code)]
pub mod builtins;
pub mod bytecode;
pub mod compiler;
pub mod convert;
pub mod dom;
pub mod exec;
pub mod fuse;
pub mod fxhash;
pub mod gc;
pub mod generator;
pub mod host;
pub mod iter;
pub mod journal;
pub mod jsx;
pub mod kernel;
pub mod localize;
pub mod module;
pub mod names;
/// Phase-0 opcode-frequency instrumentation; present only under the
/// `op-histogram` feature (see `docs/interpreter-optimization.md`).
#[cfg(feature = "op-histogram")]
pub mod opstats;
pub mod promise;
pub mod proxy;
pub mod realm;
pub mod reg;
pub mod regexp;
pub mod replay;
pub mod shape;
pub mod trace;
pub mod typed_array;
mod unicode_tables;
pub mod value;
pub mod vm;
pub mod wtf8;
pub use trace::{TraceEnter, TraceObserver};
pub use value::Value;
pub use vm::{RunOutcome, Vm};
/// A handle bundling a VM and the top-level compiled script. This is the unit the
/// replay runtime and the `SnapshotCapableJsEngine` adapter build on.
pub struct Engine {
pub vm: Vm,
/// The global `chidori` host object, captured by `install_chidori_effects`.
/// Passed as the entrypoint's second argument so agents written for the
/// QuickJS convention — `agent(input, chidori)` — receive it as a parameter,
/// in addition to it being reachable as a global.
chidori: Option<Value>,
}
impl Default for Engine {
fn default() -> Self {
Engine::new()
}
}
impl Engine {
pub fn new() -> Engine {
Engine {
vm: Vm::new(),
chidori: None,
}
}
/// Compile and run a script to completion (draining microtasks), returning
/// the completion value. Errors are returned as their string form.
pub fn eval(&mut self, src: &str) -> Result<Value, String> {
let proto = compiler::compile_script(src)?;
let func = self.vm.make_closure(std::rc::Rc::new(proto), Vec::new());
let result = self
.vm
.call(Value::Object(func), Value::Undefined, &[])
.map_err(|e| self.vm.error_to_string(&e))?;
// Drain microtasks (promise reactions scheduled by the script).
let _ = self.vm.run_jobs_until_blocked();
Ok(result)
}
/// As [`Engine::eval`], but compiling through the thread-local
/// source→proto cache (`compiler::compile_script_cached`). Use for scripts
/// evaluated verbatim on every fresh engine — the runtime's prelude/helper
/// scripts — so their parse+lower cost is paid once per thread instead of
/// once per engine. Execution (which must run to populate the new realm)
/// is unchanged; only the compile step is memoized.
pub fn eval_cached(&mut self, src: &str) -> Result<Value, String> {
let proto = compiler::compile_script_cached(src)?;
let func = self.vm.make_closure(proto, Vec::new());
let result = self
.vm
.call(Value::Object(func), Value::Undefined, &[])
.map_err(|e| self.vm.error_to_string(&e))?;
let _ = self.vm.run_jobs_until_blocked();
Ok(result)
}
/// Console output collected so far.
pub fn console(&self) -> &[String] {
&self.vm.console_log
}
/// Install a deterministic virtual DOM (`document` / `window` globals) into
/// this engine and return the handle the embedder drives — drain mutations to
/// render, dispatch events to interact, render to HTML to assert/visualize.
/// See [`crate::dom`] for the design (DOM-behind-the-host-boundary).
pub fn install_dom(&mut self) -> dom::DomHandle {
dom::install(&mut self.vm)
}
/// Install a global `chidori` host object whose methods forward to
/// `dispatch` as `(effect_name, json_args) -> json_result`. The durable
/// host (in the main crate) routes those through its own call log + OTEL, so
/// host-effect durability and the host-call span tree are unchanged while
/// this engine runs the JS and emits JS-level trace spans. Unknown effects
/// should be surfaced as `Err` by the dispatcher; they become a thrown JS
/// error here.
pub fn install_chidori_effects(
&mut self,
dispatch: std::rc::Rc<
dyn Fn(&str, &serde_json::Value) -> Result<serde_json::Value, String>,
>,
) {
let chidori = self.vm.new_object();
let d = dispatch.clone();
self.vm
.define_method(&chidori, "log", 2, move |vm, _t, args| {
let msg = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
// Forward the structured fields object too — the host side
// (`host_core::execute_log`) has always accepted it, but the
// binding used to drop everything past the message, silently
// losing every `chidori.log(msg, {...})` payload.
let fields = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let payload = if fields.is_null() {
serde_json::json!({ "message": msg })
} else {
serde_json::json!({ "message": msg, "fields": fields })
};
forward_effect(vm, &d, "log", payload)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "tool", 2, move |vm, _t, args| {
let name = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let kwargs = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"tool",
serde_json::json!({ "name": name, "kwargs": kwargs }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "prompt", 2, move |vm, _t, args| {
let text = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"prompt",
serde_json::json!({ "text": text, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "input", 2, move |vm, _t, args| {
let prompt = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"input",
serde_json::json!({ "prompt": prompt, "opts": opts }),
)
});
let d = dispatch.clone();
// chidori.signal(name | names[], opts) — a single listen verb: a
// string listens for one name, an array is the fan-in form (routed to
// the signal_any host effect, whose recorded result's `name` says
// which fired).
self.vm
.define_method(&chidori, "signal", 2, move |vm, _t, args| {
let name = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
if name.is_array() {
return forward_effect(
vm,
&d,
"signal_any",
serde_json::json!({ "names": name, "opts": opts }),
);
}
forward_effect(
vm,
&d,
"signal",
serde_json::json!({ "name": name, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "pollSignal", 1, move |vm, _t, args| {
let name = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(vm, &d, "poll_signal", serde_json::json!({ "name": name }))
});
let d = dispatch.clone();
// chidori.step(name, fn) — durable value checkpoint. Unlike the other
// effects, the second argument is a JS callback, which cannot cross the
// JSON dispatch boundary; instead the binding drives a two-call
// protocol: probe the journal (`step_begin`), run the callback
// synchronously only on a miss, then record its outcome (`step_end`).
// On replay the probe returns the recorded value (or re-throws the
// recorded error) and the callback never runs — that is the point: it
// bounds resume cost for expensive deterministic compute. Results are
// JSON round-tripped on the live path too, so live and replayed runs
// observe byte-identical values.
self.vm
.define_method(&chidori, "step", 2, move |vm, _t, args| {
let name = match args.first() {
Some(Value::String(s)) => s.as_str().to_string(),
_ => {
return Err(vm.make_error(
crate::vm::ErrorKind::Type,
"chidori.step requires a string name as its first argument",
))
}
};
let f = args.get(1).cloned().unwrap_or(Value::Undefined);
if !vm.is_callable(&f) {
return Err(vm.make_error(
crate::vm::ErrorKind::Type,
"chidori.step requires a callback as its second argument",
));
}
let begin = match d("step_begin", &serde_json::json!({ "name": name })) {
Ok(j) => j,
Err(e) => return Err(vm.make_error(crate::vm::ErrorKind::Error, &e)),
};
if begin
.get("cached")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
{
if let Some(err) = begin.get("error").and_then(serde_json::Value::as_str) {
return Err(vm.make_error(crate::vm::ErrorKind::Error, err));
}
let value = begin.get("value").unwrap_or(&serde_json::Value::Null);
return Ok(vm.json_to_value(value));
}
match vm.call(f, Value::Undefined, &[]) {
Ok(v) => {
let is_promise = matches!(
&v,
Value::Object(o) if matches!(
o.borrow().internal,
crate::value::Internal::Promise(_)
)
);
if is_promise {
let msg = "chidori.step callback must return synchronously \
(got a Promise): step bodies are pure compute — run \
host effects outside the step and pass results in";
let _ = d(
"step_end",
&serde_json::json!({ "name": name, "error": msg }),
);
return Err(vm.make_error(crate::vm::ErrorKind::Type, msg));
}
let j = vm.value_to_json(&v);
match d("step_end", &serde_json::json!({ "name": name, "value": j })) {
Ok(rj) => Ok(vm.json_to_value(&rj)),
Err(e) => Err(vm.make_error(crate::vm::ErrorKind::Error, &e)),
}
}
Err(e) => {
let msg = vm.error_to_string(&e);
let _ = d(
"step_end",
&serde_json::json!({ "name": name, "error": msg }),
);
// Rebuild from the recorded message (instead of
// re-throwing `e`) so the live throw matches the
// replayed one exactly.
Err(vm.make_error(crate::vm::ErrorKind::Error, &msg))
}
}
});
let d = dispatch.clone();
// chidori.mark(label, data) — a labelled trace marker in the call log.
// (Named `mark`, not `checkpoint`: the durable value checkpoint is
// `chidori.step`, and `checkpoint.json` is the run's call log — this
// is neither, just an annotation.)
self.vm
.define_method(&chidori, "mark", 2, move |vm, _t, args| {
let label = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let data = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"mark",
serde_json::json!({ "label": label, "data": data }),
)
});
let d = dispatch.clone();
self.vm.define_method(&chidori, "memory", 4, move |vm, _t, args| {
let action = args.first().map(|v| vm.to_string_lossy(v)).unwrap_or_default();
let key = args.get(1).map(|v| vm.value_to_json(v)).unwrap_or(serde_json::Value::Null);
let value = args.get(2).map(|v| vm.value_to_json(v)).unwrap_or(serde_json::Value::Null);
let opts = args.get(3).map(|v| vm.value_to_json(v)).unwrap_or(serde_json::Value::Null);
forward_effect(vm, &d, "memory", serde_json::json!({ "action": action, "key": key, "value": value, "opts": opts }))
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "template", 3, move |vm, _t, args| {
let template = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let vars = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"template",
serde_json::json!({ "template": template, "vars": vars }),
)
});
let d = dispatch.clone();
// The captured networking host op. There is no public `chidori.http`:
// the base networking surface the runtime installs — `globalThis.fetch`
// (+ `Headers`/`Request`/`Response`) and the `node:http`/`node:https`
// client shims — all route through this internal global, so every
// network call (including ones made deep inside a dependency) inherits
// the same policy enforcement, approval-pause, and record/replay. It is
// a global rather than a method on `chidori` so those polyfills and shims
// can reach it without the host object in scope.
let http_global = self.vm.realm.global.clone();
self.vm
.define_method(&http_global, "__chidori_http", 2, move |vm, _t, args| {
let arg0 = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let arg1 = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"http",
serde_json::json!({ "arg0": arg0, "arg1": arg1 }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "callAgent", 2, move |vm, _t, args| {
let path = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let input = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"callAgent",
serde_json::json!({ "path": path, "input": input }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "branch", 2, move |vm, _t, args| {
let variants = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let options = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"branch",
serde_json::json!({ "variants": variants, "options": options }),
)
});
// chidori.actors.<method> — supervised actor sub-runs + message
// passing (`docs/actors.md`). Each method forwards its effect to the
// durable host, which owns spawning, mailboxes, supervision trees,
// and record/replay. The runtime's helper script wraps `spawn` and
// `lookup` results into actor handles (`.send()`/`.join()`/...);
// these natives return the raw durable JSON.
let actors = self.vm.new_object();
let d = dispatch.clone();
self.vm
.define_method(&actors, "spawn", 3, move |vm, _t, args| {
let source = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let input = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let options = args
.get(2)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"spawn_actor",
serde_json::json!({ "source": source, "input": input, "options": options }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&actors, "send", 3, move |vm, _t, args| {
let to = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let name = args
.get(1)
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let payload = args
.get(2)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"send_actor",
serde_json::json!({ "to": to, "name": name, "payload": payload }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&actors, "join", 2, move |vm, _t, args| {
let pid = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"join_actor",
serde_json::json!({ "pid": pid, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&actors, "stop", 2, move |vm, _t, args| {
let pid = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"stop_actor",
serde_json::json!({ "pid": pid, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&actors, "status", 1, move |vm, _t, args| {
let pid = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
forward_effect(vm, &d, "actor_status", serde_json::json!({ "pid": pid }))
});
let d = dispatch.clone();
self.vm
.define_method(&actors, "lookup", 1, move |vm, _t, args| {
let name = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
forward_effect(vm, &d, "whereis", serde_json::json!({ "name": name }))
});
self.vm
.define_value(&chidori, "actors", Value::Object(actors));
// chidori.agents.<method> — detached, durable, addressable agent
// processes. Unlike actors (in-run, fold-at-join), a detached agent is
// its own durable run with a registered name, a durable mailbox, and a
// hibernate/wake lifecycle owned by the process-global supervisor. The
// natives return the raw durable JSON; the helper script wraps spawn/
// lookup results into handles.
let agents = self.vm.new_object();
let d = dispatch.clone();
self.vm
.define_method(&agents, "spawn", 3, move |vm, _t, args| {
let source = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let input = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let options = args
.get(2)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"spawn_agent",
serde_json::json!({ "source": source, "input": input, "options": options }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&agents, "send", 3, move |vm, _t, args| {
let to = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let name = args
.get(1)
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let payload = args
.get(2)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"send_agent",
serde_json::json!({ "to": to, "name": name, "payload": payload }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&agents, "join", 2, move |vm, _t, args| {
let to = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"join_agent",
serde_json::json!({ "to": to, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&agents, "stop", 2, move |vm, _t, args| {
let to = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"stop_agent",
serde_json::json!({ "to": to, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&agents, "status", 1, move |vm, _t, args| {
let to = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
forward_effect(vm, &d, "agent_status", serde_json::json!({ "to": to }))
});
let d = dispatch.clone();
self.vm
.define_method(&agents, "lookup", 1, move |vm, _t, args| {
let name = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
forward_effect(vm, &d, "lookup_agent", serde_json::json!({ "name": name }))
});
self.vm
.define_value(&chidori, "agents", Value::Object(agents));
// chidori.alarm(ms) — a durable timer: the run (or detached agent)
// hibernates and is woken at the deadline, surviving process
// restarts. Lowered onto the durable signal machinery host-side.
let d = dispatch.clone();
self.vm
.define_method(&chidori, "alarm", 1, move |vm, _t, args| {
let ms = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(vm, &d, "alarm", serde_json::json!({ "ms": ms }))
});
// chidori.receive stays top-level: it is the general listen verb for
// both the run (parent-addressed messages) and actor code.
let d = dispatch.clone();
self.vm
.define_method(&chidori, "receive", 2, move |vm, _t, args| {
let names = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"receive",
serde_json::json!({ "names": names, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "execJs", 2, move |vm, _t, args| {
let source = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"execJs",
serde_json::json!({ "source": source, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "execPython", 2, move |vm, _t, args| {
let source = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"execPython",
serde_json::json!({ "source": source, "opts": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&chidori, "execWasm", 2, move |vm, _t, args| {
let source = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"execWasm",
serde_json::json!({ "source": source, "opts": opts }),
)
});
let d = dispatch.clone();
// Synchronous, pure digest of a context segment chain — backs the JS
// SDK's `Context.digest()`. Unlike the async-shaped effects above it
// returns its value inline; the dispatcher computes a deterministic
// hash and records nothing.
self.vm
.define_method(&chidori, "__contextDigest", 2, move |vm, _t, args| {
let segments = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
let opts = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"contextDigest",
serde_json::json!({ "segments": segments, "opts": opts }),
)
});
// chidori.workspace.<action>(...) — a nested object whose methods all
// forward the "workspace" effect tagged with their action.
let workspace = self.vm.new_object();
let d = dispatch.clone();
self.vm
.define_method(&workspace, "list", 1, move |vm, _t, args| {
let opts = args
.first()
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"workspace",
serde_json::json!({ "action": "list", "args": opts }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&workspace, "read", 1, move |vm, _t, args| {
let path = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
forward_effect(
vm,
&d,
"workspace",
serde_json::json!({ "action": "read", "args": { "path": path } }),
)
});
let d = dispatch.clone();
self.vm.define_method(&workspace, "write", 3, move |vm, _t, args| {
let path = args.first().map(|v| vm.to_string_lossy(v)).unwrap_or_default();
let content = args.get(1).map(|v| vm.to_string_lossy(v)).unwrap_or_default();
let options = args.get(2).map(|v| vm.value_to_json(v)).unwrap_or(serde_json::Value::Null);
forward_effect(vm, &d, "workspace", serde_json::json!({ "action": "write", "args": { "path": path, "content": content, "options": options } }))
});
let d = dispatch.clone();
self.vm.define_method(&workspace, "delete", 2, move |vm, _t, args| {
let path = args.first().map(|v| vm.to_string_lossy(v)).unwrap_or_default();
let reason = args.get(1).map(|v| vm.to_string_lossy(v));
forward_effect(vm, &d, "workspace", serde_json::json!({ "action": "delete", "args": { "path": path, "reason": reason } }))
});
let d = dispatch.clone();
self.vm.define_method(&workspace, "remove", 2, move |vm, _t, args| {
let path = args.first().map(|v| vm.to_string_lossy(v)).unwrap_or_default();
let reason = args.get(1).map(|v| vm.to_string_lossy(v));
forward_effect(vm, &d, "workspace", serde_json::json!({ "action": "remove", "args": { "path": path, "reason": reason } }))
});
let d = dispatch.clone();
self.vm
.define_method(&workspace, "manifest", 0, move |vm, _t, _args| {
forward_effect(
vm,
&d,
"workspace",
serde_json::json!({ "action": "manifest", "args": {} }),
)
});
self.vm
.define_value(&chidori, "workspace", Value::Object(workspace));
// chidori.appData.{write,query}(sql, params?) — the generative-UI
// agent-run write tool. Each method forwards the "app_data" effect
// tagged with its action; the host performs the write through the
// host boundary (the guest never holds a DB credential) and journals
// it. `params` are bound server-side ($1,$2,…), never string-concatted.
let app_data = self.vm.new_object();
let d = dispatch.clone();
self.vm
.define_method(&app_data, "write", 2, move |vm, _t, args| {
let sql = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let params = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"app_data",
serde_json::json!({ "action": "write", "sql": sql, "params": params }),
)
});
let d = dispatch.clone();
self.vm
.define_method(&app_data, "query", 2, move |vm, _t, args| {
let sql = args
.first()
.map(|v| vm.to_string_lossy(v))
.unwrap_or_default();
let params = args
.get(1)
.map(|v| vm.value_to_json(v))
.unwrap_or(serde_json::Value::Null);
forward_effect(
vm,
&d,
"app_data",
serde_json::json!({ "action": "query", "sql": sql, "params": params }),
)
});
self.vm
.define_value(&chidori, "appData", Value::Object(app_data));
let global = self.vm.realm.global.clone();
self.vm
.define_value(&global, "chidori", Value::Object(chidori.clone()));
// Keep a handle so the entrypoint can be invoked as `agent(input, chidori)`.
self.chidori = Some(Value::Object(chidori));
}
/// Install synchronous `__chidori_*`-style native globals backed by
/// `dispatch`. Unlike [`Engine::install_chidori_effects`] (whose host effects
/// are async and return promises), each function installed here returns its
/// result *synchronously* — the `node:` builtin shims (crypto hashing/HMAC,
/// captured randomness, the VFS) call these inline and use the value
/// immediately. Each call forwards `(name, [args...])` to `dispatch`; the
/// JSON result becomes the return value, and an `Err` becomes a thrown JS
/// `Error` (matching the QuickJS path). `names` pairs each global function
/// name with its declared arity.
pub fn install_sync_natives(
&mut self,
names: &[(&'static str, u32)],
dispatch: std::rc::Rc<
dyn Fn(&str, &serde_json::Value) -> Result<serde_json::Value, String>,
>,
) {
let global = self.vm.realm.global.clone();
for &(name, arity) in names {
let d = dispatch.clone();
self.vm
.define_method(&global, name, arity, move |vm, _t, args| {
let json_args = serde_json::Value::Array(
args.iter().map(|v| vm.value_to_json(v)).collect(),
);
match d(name, &json_args) {
Ok(j) => Ok(vm.json_to_value(&j)),
Err(e) => Err(vm.make_error(crate::vm::ErrorKind::Error, &e)),
}
});
}
}
/// Install the `run(handler)` entrypoint registrar as a global. Call before
/// evaluating an agent module; whatever the module passes to `run(...)` lands
/// in the returned slot.
pub fn install_entrypoint(&mut self) -> std::rc::Rc<std::cell::RefCell<Option<Value>>> {
let slot = std::rc::Rc::new(std::cell::RefCell::new(None));
let captured = slot.clone();
let global = self.vm.realm.global.clone();
self.vm
.define_method(&global, "run", 1, move |_vm, _t, args| {
*captured.borrow_mut() = args.first().cloned();
Ok(Value::Undefined)
});
slot
}
/// Compile `src` as an ES module and evaluate it — so a top-level
/// `run(handler)` registers the entrypoint into `slot` — then call the
/// entrypoint with `input` and return the settled result (awaiting a
/// returned promise). Falls back to a named export (`fallback_export`, e.g.
/// `agent` for agents or `run` for tools) when `run(...)` wasn't called.
/// Single-file only — a module with (non-stripped) imports is rejected.
pub fn run_entrypoint(
&mut self,
src: &str,
input: &serde_json::Value,
slot: &std::rc::Rc<std::cell::RefCell<Option<Value>>>,
fallback_export: &str,
) -> Result<serde_json::Value, String> {
let compiled = compiler::compile_module(src)?;
if !compiled.requested.is_empty() {
return Err("module imports are not supported in single-file entrypoints".to_string());
}
let cell_of_name = compiled.cell_of_name.clone();
let rec = std::rc::Rc::new(std::cell::RefCell::new(module::ModuleRecord::new(compiled)));
let entry = "<entry>";
let mut registry = module::ModuleRegistry::default();
registry.modules.insert(entry.to_string(), rec.clone());
self.vm
.run_module_graph(®istry, entry)
.map_err(|e| self.vm.error_to_string_with_stack(&e))?;
// Entrypoint: whatever `run(...)` captured, else the named export.
let handler = slot.borrow().clone().or_else(|| {
cell_of_name
.get(fallback_export)
.map(|idx| rec.borrow().cells[*idx as usize].borrow().clone())
});
let handler = handler.ok_or_else(|| {
format!("module did not call run(...) and has no `{fallback_export}` export")
})?;
let arg = self.vm.json_to_value(input);
let chidori = self.chidori.clone().unwrap_or(Value::Undefined);
let ret = self
.vm
.call(handler, Value::Undefined, &[arg, chidori])
.map_err(|e| self.vm.error_to_string_with_stack(&e))?;
let settled = self
.vm
.settle(ret)
.map_err(|e| self.vm.error_to_string_with_stack(&e))?;
Ok(self.vm.value_to_json(&settled))
}
/// Like [`run_entrypoint`], but resolves a multi-file module graph. `entry_key`
/// is the entry module's registry key (e.g. its canonical path) and `entry_src`
/// its already-transpiled source. Each transitively-requested specifier is
/// resolved by the host `load` callback, which maps `(specifier, importer_key)`
/// to `(resolved_key, transpiled_source)` — keeping filesystem resolution in the
/// host while this engine handles ES module linking/evaluation.
pub fn run_entrypoint_graph(
&mut self,
entry_key: &str,
entry_src: &str,
input: &serde_json::Value,
slot: &std::rc::Rc<std::cell::RefCell<Option<Value>>>,
fallback_export: &str,
load: &mut dyn FnMut(&str, &str) -> Result<(String, String), String>,
) -> Result<serde_json::Value, String> {
let mut registry = module::ModuleRegistry::default();
// BFS the import graph, compiling each module once and recording how its
// requested specifiers resolved (the linker reads `resolved` per record).
let mut queue: Vec<(String, String)> = vec![(entry_key.to_string(), entry_src.to_string())];
let mut entry_cell_of_name = None;
let mut entry_rec = None;
while let Some((key, src)) = queue.pop() {
if registry.modules.contains_key(&key) {
continue;
}
let compiled = compiler::compile_module_labeled(&src, Some(&key))
.map_err(|e| format!("compiling module '{key}': {e}"))?;