feat(trace): Trace v2 identity, module-owned events and capacity bounds (B-16)
- bind traces to the logical project identity and the exact emitted Analysis digest; every span-bearing event carries module_id - add host TraceState budgets (100k events / 32 MiB defaults), CLI --trace-max-events/--trace-max-bytes, terminal trace.truncated events, and a final serialized byte cap; budgets never change run.status - disable semantic recording when no trace is requested - cap worker frames at 8 MiB, align request/result/capability wire caps, make clean EOF distinct from protocol errors, and mark/kill a worker after a transport failure instead of reusing it - add trace-v2 spec/schema, independent trace consumer, CI check and tests - record the S2/C state in STATUS
This commit is contained in:
1 parent
55dcb57553
commit
d890686a2b
21 files changed
+732
-84
No files matched your search
@@ -38,8 +38,12 @@ pub struct SandboxSettings {
|
||||
pub plugins: Vec<PathBuf>,
|
||||
/// Explicit trust decision for in-process native plugin loading.
|
||||
pub trust_native_plugin: bool,
|
||||
/// Optional Trace v1 output path.
|
||||
/// Optional Trace v2 output path.
|
||||
pub trace_file: Option<PathBuf>,
|
||||
/// Trace event budget override (0 = unlimited).
|
||||
pub trace_max_events: Option<usize>,
|
||||
/// Trace approximate byte budget override (0 = unlimited).
|
||||
pub trace_max_bytes: Option<usize>,
|
||||
/// External foreign-kernel descriptor paths.
|
||||
pub kernels: Vec<PathBuf>,
|
||||
}
|
||||
@@ -690,7 +694,16 @@ pub fn execute(
|
||||
};
|
||||
let mut host = StdHost::new(mode, capabilities, project.root.clone());
|
||||
if trace || json || settings.trace_file.is_some() {
|
||||
host = host.with_trace();
|
||||
host = host
|
||||
.with_trace()
|
||||
.with_trace_budget(marqdo_host::TraceBudget {
|
||||
max_events: settings
|
||||
.trace_max_events
|
||||
.unwrap_or(marqdo_host::trace::DEFAULT_MAX_TRACE_EVENTS),
|
||||
max_bytes: settings
|
||||
.trace_max_bytes
|
||||
.unwrap_or(marqdo_host::trace::DEFAULT_MAX_TRACE_BYTES),
|
||||
});
|
||||
}
|
||||
if let Some(fuel) = settings.fuel {
|
||||
host.budget.fuel = fuel;
|
||||
|
||||
@@ -109,9 +109,15 @@ struct SandboxArgs {
|
||||
/// Explicitly trusted plugin manifest/directory to load (repeatable).
|
||||
#[arg(long = "plugin", value_name = "PATH")]
|
||||
plugin: Vec<PathBuf>,
|
||||
/// Write a versioned Trace v1 file for this run.
|
||||
/// Write a versioned Trace v2 file for this run.
|
||||
#[arg(long = "trace-file", value_name = "PATH")]
|
||||
trace_file: Option<PathBuf>,
|
||||
/// Override the trace event budget (0 = unlimited).
|
||||
#[arg(long = "trace-max-events")]
|
||||
trace_max_events: Option<usize>,
|
||||
/// Override the approximate trace byte budget (0 = unlimited).
|
||||
#[arg(long = "trace-max-bytes")]
|
||||
trace_max_bytes: Option<usize>,
|
||||
/// Register an external foreign-kernel provider (repeatable).
|
||||
#[arg(long = "kernel", value_name = "PATH")]
|
||||
kernel: Vec<PathBuf>,
|
||||
@@ -135,6 +141,8 @@ impl SandboxArgs {
|
||||
plugins: self.plugin.clone(),
|
||||
trust_native_plugin: self.trust_native_plugin,
|
||||
trace_file: self.trace_file.clone(),
|
||||
trace_max_events: self.trace_max_events,
|
||||
trace_max_bytes: self.trace_max_bytes,
|
||||
kernels: self.kernel.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
+54
-12
@@ -1,4 +1,4 @@
|
||||
//! Public Trace v1 writer (spec/trace-v1.md).
|
||||
//! Public Trace v2 writer (spec/trace-v2.md).
|
||||
//!
|
||||
//! Events come from the interpreter (semantic) and the capability host
|
||||
//! (effects). No argument values, secrets or handles are recorded.
|
||||
@@ -12,7 +12,7 @@ use crate::loader::LoadedProject;
|
||||
|
||||
/// Schema name and version.
|
||||
pub const SCHEMA: &str = "marqdo.trace";
|
||||
pub const SCHEMA_VERSION: u32 = 1;
|
||||
pub const SCHEMA_VERSION: u32 = 2;
|
||||
|
||||
/// Write a trace atomically for one run.
|
||||
pub fn write(
|
||||
@@ -32,6 +32,7 @@ pub fn write(
|
||||
);
|
||||
let analysis_digest = crate::analysis::digest(project);
|
||||
let root_id = project.identity.root_id();
|
||||
let state = host.trace_state();
|
||||
let mut events: Vec<Value> = host
|
||||
.semantic_events()
|
||||
.iter()
|
||||
@@ -39,15 +40,55 @@ pub fn write(
|
||||
.chain(host.trace().iter().map(effect_json))
|
||||
.collect();
|
||||
events.sort_by_key(|event| event.get("seq").and_then(Value::as_u64).unwrap_or(u64::MAX));
|
||||
let trace = json!({
|
||||
"schema": SCHEMA,
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"run": {"id": run_id, "entry": entry, "status": status},
|
||||
"source": {"root_id": root_id, "analysis_digest": format!("sha256:{analysis_digest}")},
|
||||
"events": events,
|
||||
});
|
||||
let bytes = serde_json::to_vec_pretty(&trace)
|
||||
.map_err(|error| format!("cannot encode trace: {error}"))?;
|
||||
|
||||
let mut truncation = state.truncation.clone();
|
||||
let max_bytes = state.budget.max_bytes;
|
||||
let bytes = loop {
|
||||
let mut all = events.clone();
|
||||
if let Some(truncation) = &truncation {
|
||||
let seq = all
|
||||
.last()
|
||||
.and_then(|event| event.get("seq"))
|
||||
.and_then(Value::as_u64)
|
||||
.unwrap_or(0)
|
||||
+ 1;
|
||||
all.push(json!({
|
||||
"seq": seq,
|
||||
"kind": "trace.truncated",
|
||||
"module_id": null,
|
||||
"span": null,
|
||||
"detail": truncation.reason,
|
||||
}));
|
||||
}
|
||||
let trace = json!({
|
||||
"schema": SCHEMA,
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"run": {"id": run_id, "entry": entry, "status": status},
|
||||
"source": {
|
||||
"root_id": root_id,
|
||||
"analysis_digest": format!("sha256:{analysis_digest}"),
|
||||
},
|
||||
"limits": {
|
||||
"max_events": state.budget.max_events,
|
||||
"max_bytes": state.budget.max_bytes,
|
||||
},
|
||||
"truncated": truncation,
|
||||
"events": all,
|
||||
});
|
||||
let bytes = serde_json::to_vec_pretty(&trace)
|
||||
.map_err(|error| format!("cannot encode trace: {error}"))?;
|
||||
if max_bytes == 0 || bytes.len() <= max_bytes || events.is_empty() {
|
||||
break bytes;
|
||||
}
|
||||
events.pop();
|
||||
if truncation.is_none() {
|
||||
truncation = Some(marqdo_host::TraceTruncation {
|
||||
reason: "byte_budget".to_string(),
|
||||
limit: max_bytes as u64,
|
||||
written: events.len() as u64,
|
||||
});
|
||||
}
|
||||
};
|
||||
atomic_write(path, &bytes)
|
||||
}
|
||||
|
||||
@@ -56,6 +97,7 @@ fn semantic_json(event: &SemanticEvent) -> Value {
|
||||
"seq": event.seq,
|
||||
"kind": event.kind,
|
||||
"function_id": event.function_id,
|
||||
"module_id": event.module_id,
|
||||
"span": event.span.map(|span| json!({"start": span.start, "end": span.end})),
|
||||
"detail": event.detail,
|
||||
})
|
||||
@@ -65,7 +107,7 @@ fn effect_json(entry: &TraceEntry) -> Value {
|
||||
json!({
|
||||
"seq": entry.seq,
|
||||
"kind": "effect",
|
||||
"module": entry.module,
|
||||
"module_id": entry.module,
|
||||
"span": entry.span.map(|span| json!({"start": span.start, "end": span.end})),
|
||||
"detail": entry.result,
|
||||
"operation": entry.op,
|
||||
|
||||
@@ -241,6 +241,52 @@ print(1)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trace_v2_budget_truncates_long_loops_without_failing_the_run() {
|
||||
let dir = TempDir::new("trace-budget");
|
||||
write(
|
||||
dir.path(),
|
||||
"index.mq.md",
|
||||
"## main\n\n**i = 0**\n\n- `i < 500`\n\n **i = i + 1**\n\n*i*\n",
|
||||
);
|
||||
let trace_path = dir.path().join("trace.json");
|
||||
let output = cli(
|
||||
dir.path(),
|
||||
&[
|
||||
"run",
|
||||
"index.mq.md",
|
||||
"--trace-file",
|
||||
trace_path.to_str().unwrap(),
|
||||
"--trace-max-events",
|
||||
"20",
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let trace: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(&trace_path).unwrap()).unwrap();
|
||||
assert_eq!(trace["schema_version"], 2);
|
||||
assert_eq!(trace["run"]["status"], "ok");
|
||||
assert_eq!(trace["limits"]["max_events"], 20);
|
||||
assert_eq!(trace["truncated"]["reason"], "event_budget");
|
||||
assert_eq!(trace["truncated"]["limit"], 20);
|
||||
let events = trace["events"].as_array().unwrap();
|
||||
assert!(events.len() <= 21, "events: {}", events.len());
|
||||
assert_eq!(events.last().unwrap()["kind"], "trace.truncated");
|
||||
let mut previous = None;
|
||||
for event in events {
|
||||
let seq = event["seq"].as_u64().unwrap();
|
||||
if let Some(previous) = previous {
|
||||
assert!(seq > previous);
|
||||
}
|
||||
previous = Some(seq);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_check_removes_stale_analysis() {
|
||||
let dir = TempDir::new("analysis-error");
|
||||
@@ -282,11 +328,14 @@ fn trace_records_calls_effects_and_status_without_secrets() {
|
||||
);
|
||||
let trace: serde_json::Value = serde_json::from_str(&text).unwrap();
|
||||
assert_eq!(trace["schema"], "marqdo.trace");
|
||||
assert_eq!(trace["schema_version"], 1);
|
||||
assert_eq!(trace["schema_version"], 2);
|
||||
assert_eq!(trace["run"]["status"], "ok");
|
||||
assert_eq!(trace["run"]["entry"], "root:default::index.mq.md::main");
|
||||
let events = trace["events"].as_array().unwrap();
|
||||
assert!(events.iter().any(|event| event["kind"] == "call.enter"));
|
||||
assert!(events
|
||||
.iter()
|
||||
.any(|event| event["kind"] == "call.enter"
|
||||
&& event["module_id"] == "root:default::index.mq.md"));
|
||||
assert!(events.iter().any(|event| event["kind"] == "effect"));
|
||||
assert!(events.iter().any(|event| event["kind"] == "call.exit"));
|
||||
let mut previous = None;
|
||||
|
||||
@@ -20,7 +20,7 @@ use crate::context::ExecutionContext;
|
||||
use crate::fs::ProjectFs;
|
||||
use crate::handles::{contains_plugin_handle, PluginHandleTable};
|
||||
use crate::plugin_worker::WorkerPlugin;
|
||||
use crate::trace::{EffectTrace, SemanticEvent, TraceEntry};
|
||||
use crate::trace::{EffectTrace, SemanticEvent, TraceBudget, TraceEntry, TraceState};
|
||||
use marqdo_sandbox::{PlatformCapabilities, SandboxMode};
|
||||
|
||||
/// Host operations reachable from the language surface.
|
||||
@@ -99,6 +99,7 @@ pub trait Host {
|
||||
&mut self,
|
||||
_kind: &str,
|
||||
_function_id: Option<&str>,
|
||||
_module_id: Option<&str>,
|
||||
_span: Option<Span>,
|
||||
_detail: Option<&str>,
|
||||
) {
|
||||
@@ -144,6 +145,7 @@ pub struct StdHost {
|
||||
pub handles: PluginHandleTable,
|
||||
semantic_events: Vec<SemanticEvent>,
|
||||
next_event_seq: u64,
|
||||
trace_state: TraceState,
|
||||
}
|
||||
|
||||
struct PluginSlot {
|
||||
@@ -203,6 +205,7 @@ impl StdHost {
|
||||
handles: PluginHandleTable::default(),
|
||||
semantic_events: Vec::new(),
|
||||
next_event_seq: 0,
|
||||
trace_state: TraceState::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,6 +341,17 @@ impl StdHost {
|
||||
seq
|
||||
}
|
||||
|
||||
/// Trace capacity state for the writer.
|
||||
pub fn trace_state(&self) -> &TraceState {
|
||||
&self.trace_state
|
||||
}
|
||||
|
||||
/// Override the trace event/byte budget.
|
||||
pub fn with_trace_budget(mut self, budget: TraceBudget) -> Self {
|
||||
self.trace_state.budget = budget;
|
||||
self
|
||||
}
|
||||
|
||||
/// Audited plugin capability decisions.
|
||||
pub fn capability_audit(&self) -> Vec<CapabilityAudit> {
|
||||
self.broker_audit
|
||||
@@ -359,6 +373,7 @@ impl StdHost {
|
||||
/// Enable effect tracing.
|
||||
pub fn with_trace(mut self) -> Self {
|
||||
self.trace = EffectTrace::enabled();
|
||||
self.trace_state.enabled = true;
|
||||
self
|
||||
}
|
||||
|
||||
@@ -504,20 +519,29 @@ impl Host for StdHost {
|
||||
ctx: &ExecutionContext,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
let started = Instant::now();
|
||||
let seq = self.next_seq();
|
||||
let result = self.dispatch(op, args, ctx);
|
||||
let summary = match &result {
|
||||
Ok(_) => "ok".to_string(),
|
||||
Err(error) => error.code().to_string(),
|
||||
};
|
||||
self.trace.record(
|
||||
seq,
|
||||
op.name(),
|
||||
ctx.module_id.clone(),
|
||||
ctx.span,
|
||||
started.elapsed(),
|
||||
summary,
|
||||
let estimate = TraceState::estimate(
|
||||
"effect",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
Some(&summary),
|
||||
Some(op.name()),
|
||||
);
|
||||
if self.trace_state.accept(estimate) {
|
||||
let seq = self.next_seq();
|
||||
self.trace.record(
|
||||
seq,
|
||||
op.name(),
|
||||
ctx.module_id.clone(),
|
||||
ctx.span,
|
||||
started.elapsed(),
|
||||
summary,
|
||||
);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
@@ -545,14 +569,20 @@ impl Host for StdHost {
|
||||
&mut self,
|
||||
kind: &str,
|
||||
function_id: Option<&str>,
|
||||
module_id: Option<&str>,
|
||||
span: Option<Span>,
|
||||
detail: Option<&str>,
|
||||
) {
|
||||
let estimate = TraceState::estimate(kind, function_id, module_id, detail, None);
|
||||
if !self.trace_state.accept(estimate) {
|
||||
return;
|
||||
}
|
||||
let seq = self.next_seq();
|
||||
self.semantic_events.push(SemanticEvent {
|
||||
seq,
|
||||
kind: kind.to_string(),
|
||||
function_id: function_id.map(str::to_string),
|
||||
module_id: module_id.map(str::to_string),
|
||||
span,
|
||||
detail: detail.map(str::to_string),
|
||||
});
|
||||
|
||||
@@ -21,4 +21,4 @@ pub use fs::ProjectFs;
|
||||
pub use handles::{contains_plugin_handle, PluginHandleTable};
|
||||
pub use host::{arity, expect_text, Host, HostOp, StdHost};
|
||||
pub use marqdo_sandbox::{PlatformCapabilities, SandboxMode, StrictIsolation};
|
||||
pub use trace::{EffectTrace, SemanticEvent, TraceEntry};
|
||||
pub use trace::{EffectTrace, SemanticEvent, TraceBudget, TraceEntry, TraceState, TraceTruncation};
|
||||
@@ -22,6 +22,7 @@ pub struct WorkerPlugin {
|
||||
stdin: ChildStdin,
|
||||
frames: Receiver<Result<serde_json::Value, PluginError>>,
|
||||
exports: BTreeMap<String, String>,
|
||||
dead: bool,
|
||||
}
|
||||
|
||||
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
@@ -94,6 +95,7 @@ impl WorkerPlugin {
|
||||
};
|
||||
let Some(mut stdout) = child.stdout.take() else {
|
||||
let _ = child.kill();
|
||||
let _ = std::fs::remove_file(&manifest_path);
|
||||
return Err(PluginError::load(
|
||||
"worker_spawn_failed",
|
||||
"worker stdout unavailable",
|
||||
@@ -103,10 +105,23 @@ impl WorkerPlugin {
|
||||
thread::Builder::new()
|
||||
.name("marqdo-plugin-worker-reader".to_string())
|
||||
.spawn(move || loop {
|
||||
let frame = read_frame(&mut stdout);
|
||||
let failed = frame.is_err();
|
||||
if sender.send(frame).is_err() || failed {
|
||||
break;
|
||||
match read_frame(&mut stdout) {
|
||||
Ok(Some(frame)) => {
|
||||
if sender.send(Ok(frame)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
let _ = sender.send(Err(PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
"plugin worker closed its output stream",
|
||||
)));
|
||||
break;
|
||||
}
|
||||
Err(error) => {
|
||||
let _ = sender.send(Err(error));
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
.map_err(|error| {
|
||||
@@ -169,21 +184,35 @@ impl WorkerPlugin {
|
||||
stdin,
|
||||
frames,
|
||||
exports,
|
||||
dead: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Invoke one export in the worker.
|
||||
pub fn invoke(&mut self, request: &WireRequest) -> Result<WireResponse, PluginError> {
|
||||
if self.dead {
|
||||
return Err(PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
"plugin worker is no longer usable after a transport failure",
|
||||
));
|
||||
}
|
||||
let request_value: serde_json::Value = serde_json::from_slice(&request.to_json_bytes()?)
|
||||
.map_err(|error| PluginError::wire("worker_encode_failed", error.to_string()))?;
|
||||
write_frame(
|
||||
if let Err(error) = write_frame(
|
||||
&mut self.stdin,
|
||||
&json!({"kind": "invoke", "request": request_value}),
|
||||
)?;
|
||||
) {
|
||||
self.mark_dead();
|
||||
return Err(error);
|
||||
}
|
||||
let frame = match self.frames.recv_timeout(CALL_TIMEOUT) {
|
||||
Ok(frame) => frame?,
|
||||
Ok(Ok(frame)) => frame,
|
||||
Ok(Err(error)) => {
|
||||
self.mark_dead();
|
||||
return Err(error);
|
||||
}
|
||||
Err(_) => {
|
||||
let _ = self.child.kill();
|
||||
self.mark_dead();
|
||||
return Err(PluginError::invoke(
|
||||
"plugin.timeout",
|
||||
"plugin worker exceeded its invocation timeout",
|
||||
@@ -202,12 +231,20 @@ impl WorkerPlugin {
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.unwrap_or("worker reported an error"),
|
||||
)),
|
||||
_ => Err(PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
"plugin worker closed or violated the frame protocol",
|
||||
)),
|
||||
_ => {
|
||||
self.mark_dead();
|
||||
Err(PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
"plugin worker closed or violated the frame protocol",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn mark_dead(&mut self) {
|
||||
self.dead = true;
|
||||
let _ = self.child.kill();
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WorkerPlugin {
|
||||
|
||||
@@ -70,7 +70,99 @@ impl EffectTrace {
|
||||
}
|
||||
}
|
||||
|
||||
/// One semantic event emitted by the interpreter (Trace v1).
|
||||
/// Default maximum number of trace events.
|
||||
pub const DEFAULT_MAX_TRACE_EVENTS: usize = 100_000;
|
||||
/// Default approximate maximum serialized trace bytes.
|
||||
pub const DEFAULT_MAX_TRACE_BYTES: usize = 32 * 1024 * 1024;
|
||||
|
||||
/// Trace capacity budget. A zero value means unlimited.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TraceBudget {
|
||||
/// Maximum events (including the terminal truncation marker).
|
||||
pub max_events: usize,
|
||||
/// Approximate maximum serialized bytes.
|
||||
pub max_bytes: usize,
|
||||
}
|
||||
|
||||
impl Default for TraceBudget {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_events: DEFAULT_MAX_TRACE_EVENTS,
|
||||
max_bytes: DEFAULT_MAX_TRACE_BYTES,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why and where a trace stopped recording.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct TraceTruncation {
|
||||
/// `event_budget` or `byte_budget`.
|
||||
pub reason: String,
|
||||
/// The configured limit that was reached.
|
||||
pub limit: u64,
|
||||
/// Events written before truncation.
|
||||
pub written: u64,
|
||||
}
|
||||
|
||||
/// Mutable trace capacity state for one run.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct TraceState {
|
||||
/// Whether trace recording is enabled.
|
||||
pub enabled: bool,
|
||||
/// Configured budget.
|
||||
pub budget: TraceBudget,
|
||||
/// Events accepted so far.
|
||||
pub written: usize,
|
||||
/// Estimated accepted bytes.
|
||||
pub bytes: usize,
|
||||
/// Truncation marker, set once.
|
||||
pub truncation: Option<TraceTruncation>,
|
||||
}
|
||||
|
||||
impl TraceState {
|
||||
/// Accept one event if it fits the budget.
|
||||
pub fn accept(&mut self, estimate: usize) -> bool {
|
||||
if !self.enabled || self.truncation.is_some() {
|
||||
return false;
|
||||
}
|
||||
if self.budget.max_events > 0 && self.written >= self.budget.max_events {
|
||||
self.truncation = Some(TraceTruncation {
|
||||
reason: "event_budget".to_string(),
|
||||
limit: self.budget.max_events as u64,
|
||||
written: self.written as u64,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
if self.budget.max_bytes > 0 && self.bytes + estimate > self.budget.max_bytes {
|
||||
self.truncation = Some(TraceTruncation {
|
||||
reason: "byte_budget".to_string(),
|
||||
limit: self.budget.max_bytes as u64,
|
||||
written: self.written as u64,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
self.written += 1;
|
||||
self.bytes += estimate;
|
||||
true
|
||||
}
|
||||
|
||||
/// Estimate one semantic/effect event envelope.
|
||||
pub fn estimate(
|
||||
kind: &str,
|
||||
function_id: Option<&str>,
|
||||
module_id: Option<&str>,
|
||||
detail: Option<&str>,
|
||||
operation: Option<&str>,
|
||||
) -> usize {
|
||||
96 + kind.len()
|
||||
+ function_id.map(str::len).unwrap_or(0)
|
||||
+ module_id.map(str::len).unwrap_or(0)
|
||||
+ detail.map(str::len).unwrap_or(0)
|
||||
+ operation.map(str::len).unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// One semantic event emitted by the interpreter (Trace v2).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SemanticEvent {
|
||||
/// Global event sequence within the run.
|
||||
@@ -79,6 +171,8 @@ pub struct SemanticEvent {
|
||||
pub kind: String,
|
||||
/// Canonical function id, when inside a call.
|
||||
pub function_id: Option<String>,
|
||||
/// Logical module owning `span`.
|
||||
pub module_id: Option<String>,
|
||||
/// Source span, when known.
|
||||
pub span: Option<Span>,
|
||||
/// Non-sensitive detail (`ok`, `true`, `int`, an error code).
|
||||
|
||||
@@ -73,14 +73,14 @@ pub const MAX_EXPORT_NAME_BYTES: usize = 256;
|
||||
pub const MAX_SIGNATURE_WIRE_BYTES: usize = 4096;
|
||||
/// Maximum plugin metadata wire length in bytes.
|
||||
pub const MAX_METADATA_WIRE_BYTES: usize = 65_536;
|
||||
/// Maximum request wire length in bytes (16 MiB).
|
||||
pub const MAX_REQUEST_WIRE_BYTES: usize = 16 * 1024 * 1024;
|
||||
/// Maximum result wire length in bytes (64 MiB).
|
||||
pub const MAX_RESULT_WIRE_BYTES: usize = 64 * 1024 * 1024;
|
||||
/// Maximum request wire length in bytes (8 MiB frame minus envelope reserve).
|
||||
pub const MAX_REQUEST_WIRE_BYTES: usize = 8 * 1024 * 1024 - 4096;
|
||||
/// Maximum result wire length in bytes (8 MiB frame minus envelope reserve).
|
||||
pub const MAX_RESULT_WIRE_BYTES: usize = 8 * 1024 * 1024 - 4096;
|
||||
/// Maximum error wire length in bytes (1 MiB).
|
||||
pub const MAX_ERROR_WIRE_BYTES: usize = 1024 * 1024;
|
||||
/// Maximum capability reply length in bytes (16 MiB).
|
||||
pub const MAX_CAPABILITY_REPLY_BYTES: usize = 16 * 1024 * 1024;
|
||||
/// Maximum capability reply length in bytes (8 MiB frame minus envelope reserve).
|
||||
pub const MAX_CAPABILITY_REPLY_BYTES: usize = 8 * 1024 * 1024 - 4096;
|
||||
|
||||
/// Borrowed, immutable byte view (`MqBytes`).
|
||||
///
|
||||
|
||||
@@ -19,17 +19,34 @@ use crate::wire::{WireRequest, WireResponse};
|
||||
/// Hidden CLI command name used to start a worker.
|
||||
pub const WORKER_COMMAND: &str = "__plugin-worker";
|
||||
/// Maximum frame size accepted by either side.
|
||||
pub const MAX_FRAME_BYTES: usize = 64 * 1024 * 1024;
|
||||
pub const MAX_FRAME_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
/// Read one length-prefixed frame (4-byte big-endian length + JSON).
|
||||
pub fn read_frame<R: Read>(reader: &mut R) -> Result<Value, PluginError> {
|
||||
///
|
||||
/// `Ok(None)` means a clean EOF at a frame boundary. A partial header or
|
||||
/// truncated payload is a protocol error, never EOF.
|
||||
pub fn read_frame<R: Read>(reader: &mut R) -> Result<Option<Value>, PluginError> {
|
||||
let mut length = [0_u8; 4];
|
||||
reader.read_exact(&mut length).map_err(|error| {
|
||||
PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame read failed: {error}"),
|
||||
)
|
||||
})?;
|
||||
let mut read = 0usize;
|
||||
while read < length.len() {
|
||||
match reader.read(&mut length[read..]) {
|
||||
Ok(0) if read == 0 => return Ok(None),
|
||||
Ok(0) => {
|
||||
return Err(PluginError::wire(
|
||||
"worker_frame_invalid",
|
||||
"worker stream ended inside a frame header",
|
||||
))
|
||||
}
|
||||
Ok(count) => read += count,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => {
|
||||
return Err(PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame read failed: {error}"),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
let length = u32::from_be_bytes(length) as usize;
|
||||
if length == 0 || length > MAX_FRAME_BYTES {
|
||||
return Err(PluginError::wire(
|
||||
@@ -37,14 +54,23 @@ pub fn read_frame<R: Read>(reader: &mut R) -> Result<Value, PluginError> {
|
||||
format!("worker frame length {length} is invalid"),
|
||||
));
|
||||
}
|
||||
let mut bytes = vec![0_u8; length];
|
||||
reader.read_exact(&mut bytes).map_err(|error| {
|
||||
PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame read failed: {error}"),
|
||||
)
|
||||
})?;
|
||||
serde_json::from_slice(&bytes).map_err(|error| {
|
||||
let mut bytes = Vec::with_capacity(length.min(64 * 1024));
|
||||
let read = reader
|
||||
.take(length as u64)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|error| {
|
||||
PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame read failed: {error}"),
|
||||
)
|
||||
})?;
|
||||
if read != length {
|
||||
return Err(PluginError::wire(
|
||||
"worker_frame_invalid",
|
||||
format!("worker frame declared {length} bytes but only {read} arrived"),
|
||||
));
|
||||
}
|
||||
serde_json::from_slice(&bytes).map(Some).map_err(|error| {
|
||||
PluginError::wire(
|
||||
"worker_frame_invalid",
|
||||
format!("worker frame is not valid JSON: {error}"),
|
||||
@@ -110,8 +136,12 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
|
||||
}
|
||||
loop {
|
||||
let frame = match read_frame(&mut stdin) {
|
||||
Ok(frame) => frame,
|
||||
Err(_) => return 0,
|
||||
Ok(Some(frame)) => frame,
|
||||
Ok(None) => return 0,
|
||||
Err(error) => {
|
||||
eprintln!("worker: {}: {}", error.code, error.message);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
match frame.get("kind").and_then(Value::as_str) {
|
||||
Some("shutdown") => return 0,
|
||||
@@ -189,3 +219,45 @@ pub fn response_from_frame(frame: &Value) -> Result<WireResponse, PluginError> {
|
||||
.map_err(|error| PluginError::wire("worker_response_invalid", error.to_string()))?;
|
||||
WireResponse::from_json_bytes(&bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn frame_roundtrip_and_clean_eof() {
|
||||
let mut bytes = Vec::new();
|
||||
write_frame(&mut bytes, &json!({"kind": "shutdown"})).unwrap();
|
||||
let mut cursor = Cursor::new(bytes.clone());
|
||||
assert_eq!(
|
||||
read_frame(&mut cursor).unwrap(),
|
||||
Some(json!({"kind": "shutdown"}))
|
||||
);
|
||||
assert_eq!(read_frame(&mut cursor).unwrap(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_header_is_an_error_not_eof() {
|
||||
let mut cursor = Cursor::new(vec![0_u8, 0]);
|
||||
let error = read_frame(&mut cursor).unwrap_err();
|
||||
assert_eq!(error.code, "worker_frame_invalid");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_and_zero_length_frames_are_rejected() {
|
||||
for length in [0_u32, (MAX_FRAME_BYTES as u32) + 1] {
|
||||
let mut cursor = Cursor::new(length.to_be_bytes().to_vec());
|
||||
let error = read_frame(&mut cursor).unwrap_err();
|
||||
assert_eq!(error.code, "worker_frame_invalid");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_payload_is_rejected() {
|
||||
let mut bytes = 10_u32.to_be_bytes().to_vec();
|
||||
bytes.extend_from_slice(b"{}");
|
||||
let error = read_frame(&mut Cursor::new(bytes)).unwrap_err();
|
||||
assert_eq!(error.code, "worker_frame_invalid");
|
||||
}
|
||||
}
|
||||
@@ -135,11 +135,17 @@ impl<'a> Interpreter<'a> {
|
||||
) -> Result<Value, RuntimeError> {
|
||||
let display = function.id.name.clone();
|
||||
let trace_id = function.id.to_string();
|
||||
let trace_module = caller_module.unwrap_or(&function.id.module).canonical();
|
||||
if self.depth + 1 > 0 {
|
||||
self.host.check_stack(self.depth + 1, call_span)?;
|
||||
}
|
||||
self.host
|
||||
.record_semantic("call.enter", Some(&trace_id), call_span, None);
|
||||
self.host.record_semantic(
|
||||
"call.enter",
|
||||
Some(&trace_id),
|
||||
Some(&trace_module),
|
||||
call_span,
|
||||
None,
|
||||
);
|
||||
let locals = match self.bind_args(function, args, call_span) {
|
||||
Ok(locals) => locals,
|
||||
Err(mut error) => {
|
||||
@@ -162,8 +168,13 @@ impl<'a> Interpreter<'a> {
|
||||
Ok(Flow::Return(_)) => "return",
|
||||
Err(error) => error.code(),
|
||||
};
|
||||
self.host
|
||||
.record_semantic("call.exit", Some(&trace_id), call_span, Some(status));
|
||||
self.host.record_semantic(
|
||||
"call.exit",
|
||||
Some(&trace_id),
|
||||
Some(&trace_module),
|
||||
call_span,
|
||||
Some(status),
|
||||
);
|
||||
match result {
|
||||
Ok(Flow::Next) => Ok(Value::Unit),
|
||||
Ok(Flow::Return(value)) => Ok(value),
|
||||
@@ -301,9 +312,11 @@ impl<'a> Interpreter<'a> {
|
||||
}
|
||||
Stmt::Return { value, span } => {
|
||||
let value = self.eval_expr(value, frame)?;
|
||||
let module = frame.function.module.canonical();
|
||||
self.host.record_semantic(
|
||||
"return",
|
||||
Some(&frame.function.to_string()),
|
||||
Some(&module),
|
||||
Some(*span),
|
||||
Some(value.type_name()),
|
||||
);
|
||||
@@ -322,9 +335,11 @@ impl<'a> Interpreter<'a> {
|
||||
),
|
||||
));
|
||||
};
|
||||
let module = frame.function.module.canonical();
|
||||
self.host.record_semantic(
|
||||
"branch.eval",
|
||||
Some(&frame.function.to_string()),
|
||||
Some(&module),
|
||||
Some(arm.condition_span),
|
||||
Some(if condition { "true" } else { "false" }),
|
||||
);
|
||||
@@ -351,9 +366,11 @@ impl<'a> Interpreter<'a> {
|
||||
format!("loop condition must be Bool, got {}", value.type_name()),
|
||||
));
|
||||
};
|
||||
let module = frame.function.module.canonical();
|
||||
self.host.record_semantic(
|
||||
"loop.iteration",
|
||||
Some(&frame.function.to_string()),
|
||||
Some(&module),
|
||||
Some(*condition_span),
|
||||
Some(if value { "continue" } else { "exit" }),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user