Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/cli-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-rs/crates/plugin/src/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ pub fn idle_memory(sources: &Sources) -> Result<u64, String> {
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())
Expand Down
24 changes: 20 additions & 4 deletions packages/cli-rs/crates/plugin/src/script/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ pub(crate) fn made() -> usize {
MADE.with(std::cell::Cell::get)
}

thread_local! {
static IDLE_HEAP: std::cell::Cell<bool> = 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<R>(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;
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -439,6 +453,8 @@ fn typescript_refusal(diag: &Diagnostic) -> Option<String> {

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 => {
Expand All @@ -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)| {
Expand Down
Loading