From 14c60591bff708a6acec5aa0573612a904e8e135 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Thu, 3 Dec 2026 11:29:53 +0100 Subject: [PATCH] chore(cli-rs): pin psychoscript to the 0.2 integration branch Move the script carrier from opencode-carrier (52337cb, psychoscript 0.1.3) to opencode-carrier-0.2 (aeba13aa): upstream dev e79e7ee, which is psychoscript 0.2.0 plus #202, with the carrier branches rebased onto it and no TypeScript mode. The dependency graph and the regress fork do not change. A Source has no typescript flag now, so a plugin's files go in through Source::new. A diagnostic no longer carries a separate stack: its message ends with the stack, as Node prints an uncaught error, so the report splits the headline from the frames. The heap cap no longer measures a run's heap at its first wait for the host; only Runtime::measure_heap measures at waits. It measures again once a run allocated a sixteenth of the last measurement, which for a plugin's small heap is nearly every call: turned on for every activation it cost G1 and G3 40 to 55% and G9's load 70%. So only idle_memory, the one reader of an idle activation's heap_bytes, turns it on. --- packages/cli-rs/Cargo.lock | 4 ++-- packages/cli-rs/Cargo.toml | 4 ++-- .../cli-rs/crates/plugin/src/script/mod.rs | 3 ++- .../cli-rs/crates/plugin/src/script/vm.rs | 24 +++++++++++++++---- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/cli-rs/Cargo.lock b/packages/cli-rs/Cargo.lock index f6a5564ff8cd..5fedb1f03d74 100644 --- a/packages/cli-rs/Cargo.lock +++ b/packages/cli-rs/Cargo.lock @@ -1727,8 +1727,8 @@ dependencies = [ [[package]] name = "psychoscript" -version = "0.1.3" -source = "git+https://github.com/anomalyco/psychoscript?rev=52337cb6114aa93ad24e2303984257b41fda7c52#52337cb6114aa93ad24e2303984257b41fda7c52" +version = "0.2.0" +source = "git+https://github.com/anomalyco/psychoscript?rev=aeba13aa135062cd007ef122d1cb0f20b9860bb6#aeba13aa135062cd007ef122d1cb0f20b9860bb6" dependencies = [ "fixed_decimal", "icu_calendar", diff --git a/packages/cli-rs/Cargo.toml b/packages/cli-rs/Cargo.toml index a0abf76bd747..b5d7ae6539ce 100644 --- a/packages/cli-rs/Cargo.toml +++ b/packages/cli-rs/Cargo.toml @@ -59,8 +59,8 @@ libc = "0.2" log = { version = "0.4", features = ["std"] } memchr = "2" # Script plugins, behind opencode-plugin's default feature `script`: psychoscript's VM and compiler. -# Pinned to the `opencode-carrier` integration branch until the psychoscript changes land upstream. -psychoscript = { git = "https://github.com/anomalyco/psychoscript", rev = "52337cb6114aa93ad24e2303984257b41fda7c52" } +# Pinned to the `opencode-carrier-0.2` integration branch until the psychoscript changes land upstream. +psychoscript = { git = "https://github.com/anomalyco/psychoscript", rev = "aeba13aa135062cd007ef122d1cb0f20b9860bb6" } notify = "8" percent-encoding = "2" rand = "0.10" diff --git a/packages/cli-rs/crates/plugin/src/script/mod.rs b/packages/cli-rs/crates/plugin/src/script/mod.rs index 1e466f64f485..cdaea993e20e 100644 --- a/packages/cli-rs/crates/plugin/src/script/mod.rs +++ b/packages/cli-rs/crates/plugin/src/script/mod.rs @@ -95,7 +95,8 @@ pub fn idle_memory(sources: &Sources) -> Result { let built = Built::new(sources)?; let mut registered = opencode_plugin_abi::Registered::new(&built.manifest.id, None); let limit = crate::contain::Limits::default().overrun; - let plugin = bind::setup(&built.compiled, &mut registered.setup(), limit, MEMORY, None)?; + let plugin = + vm::measuring_idle_heap(|| bind::setup(&built.compiled, &mut registered.setup(), limit, MEMORY, None))?; let vm = plugin.vm.borrow(); let vm = vm.as_ref().ok_or("the plugin has ended")?; vm.heap_bytes().ok_or_else(|| "the activation did not measure its heap".to_owned()) diff --git a/packages/cli-rs/crates/plugin/src/script/vm.rs b/packages/cli-rs/crates/plugin/src/script/vm.rs index 57fc72ac7160..7ed4f7e9a32a 100644 --- a/packages/cli-rs/crates/plugin/src/script/vm.rs +++ b/packages/cli-rs/crates/plugin/src/script/vm.rs @@ -32,6 +32,20 @@ pub(crate) fn made() -> usize { MADE.with(std::cell::Cell::get) } +thread_local! { + static IDLE_HEAP: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Runs `start` with each activation it starts measuring its heap at its waits for the host, so `heap_bytes` reads an +/// idle heap. Activations don't measure otherwise: psychoscript measures again once a run allocated a sixteenth of what +/// it last measured, which for a plugin's small heap is nearly every call. +pub(crate) fn measuring_idle_heap(start: impl FnOnce() -> R) -> R { + IDLE_HEAP.set(true); + let started = start(); + IDLE_HEAP.set(false); + started +} + const OUTPUT: usize = 64 << 10; // Carrier 1 MiB source cap plus headroom for synthetic `opencode:entry`. const MAX_SOURCE_BYTES: usize = crate::source::SOURCES as usize + 4096; @@ -69,8 +83,7 @@ impl Compiled { continue; } let text = sources.text(path)?; - // `Source::new` would pick TypeScript from a `.ts` name; a plugin's sources compile as JavaScript. - list.push(Source { typescript: false, ..Source::new(path, text) }); + list.push(Source::new(path, text)); files.push((path.to_owned(), text.to_owned())); } list.push(Source::host(ENTRY_MODULE, entry_source(entry))); @@ -246,6 +259,7 @@ impl Activation { let runtime = Runtime::make() .offload(false) + .measure_heap(IDLE_HEAP.get()) .limits(limits) .modules(modules(prelude)) .globals(move |_| (*public_globals).clone()) @@ -439,6 +453,8 @@ fn typescript_refusal(diag: &Diagnostic) -> Option { pub(crate) fn format_diagnostic(id: &str, diag: &Diagnostic, slice_ms: u64, memory: usize) -> String { let raw = diag.message_lossy(); + // psychoscript ends a diagnostic's message with its stack, as Node prints an uncaught error. + let (head, stack) = raw.find("\n at ").map_or((raw.as_str(), ""), |at| (&raw[..at], &raw[at + 1..])); let body = match &diag.kind { DiagnosticKind::SliceExceeded => format!("used over {slice_ms} ms of CPU: stopped by the script budget"), DiagnosticKind::MemoryLimitExceeded => { @@ -449,9 +465,9 @@ pub(crate) fn format_diagnostic(id: &str, diag: &Diagnostic, slice_ms: u64, memo }; format!("out of memory: an allocation went past its {cap} script heap cap") } - _ => strip_location_suffix(&raw, diag.module.as_deref(), diag.location).to_owned(), + _ => strip_location_suffix(head, diag.module.as_deref(), diag.location).to_owned(), }; - let frames = diag.stack_lossy().map(|s| parse_stack_frames(&s)).unwrap_or_default(); + let frames = parse_stack_frames(stack); let loc = diag .location .map(|(line, col)| {