obs(02): add run/call --event-stream jsonl lifecycle, chunks and terminal events

This commit is contained in:
cflmy committed 2026-11-22 15:57:15 +08:00
1 parent d78faaf8e4
commit e6e497e68b
6 files changed
+1482 -80

No files matched your search

+261 -52
View File
@@ -4,6 +4,7 @@
//! runtime helpers, but keeps the run session out of the command table.
use super::*;
use crate::execution_events::{EventStream, ObservationOptions};
use crate::provider_manifest;
use marqdo_sandbox::StrictChildState;
@@ -14,16 +15,55 @@ pub fn execute(
settings: &SandboxSettings,
json: bool,
trace: bool,
observation: Option<&ObservationOptions>,
) -> i32 {
let command_name = if function.is_some() { "call" } else { "run" };
let prepared = match prepare_project(file, json, &settings.plugins) {
Ok(prepared) => prepared,
Err(code) => return code,
// Open the dedicated protocol channel before any project work so prepare
// and authorization failures can still produce `run.rejected`.
let events = match observation {
Some(options) => match EventStream::open(options) {
Ok(stream) => Some(stream),
Err(error) => {
eprintln!("error[observe.transport_failed]: {error}");
return 1;
}
},
None => None,
};
let stream = events.as_ref();
let prepared = if stream.is_some() {
let options = PrepareOptions {
plugins: settings.plugins.clone(),
};
match project_prepare::prepare(file, &options) {
Ok(prepared) => prepared,
Err(error) => return fail_prepare(stream, json, command_name, "prepare", &error),
}
} else {
match prepare_project(file, json, &settings.plugins) {
Ok(prepared) => prepared,
Err(code) => return code,
}
};
let extension_sources = prepared.loaded.extension_sources.clone();
let project = prepared.loaded;
let static_plugins = prepared.plugins;
if project.has_errors() {
if let Some(stream) = stream {
let code = project
.diagnostics
.iter()
.find(|item| item.diagnostic.is_error())
.map(|item| item.diagnostic.code.clone())
.unwrap_or_else(|| "cli.compile_error".to_string());
if let Err(error) = stream.rejected(
"compile",
&code,
"refusing to run a project with compile errors",
) {
return transport_failure(&error);
}
}
if json {
output::print_json(&json!({
"command": command_name,
@@ -48,8 +88,15 @@ pub fn execute(
let mode = match settings.sandbox_mode() {
Ok(mode) => mode,
Err(error) => {
emit_error(json, command_name, "cli.invalid_mode", &error);
return 2;
return fail(
stream,
json,
command_name,
"authorization",
"cli.invalid_mode",
&error,
2,
);
}
};
// A project-declared extension is activation, not authorization, but it is
@@ -68,8 +115,15 @@ pub fn execute(
marqdo_package::MANIFEST_FILE,
settings.mode
);
emit_error(json, command_name, code, &message);
return 2;
return fail(
stream,
json,
command_name,
"authorization",
code,
&message,
2,
);
}
let mut kernel_paths = settings.kernels.clone();
kernel_paths.extend(
@@ -90,9 +144,28 @@ pub fn execute(
provider_paths.sort();
provider_paths.dedup();
if mode == SandboxMode::Strict {
if stream.is_some() {
return fail(
stream,
json,
command_name,
"strict",
"strict.unsupported_grant",
"--event-stream jsonl is not supported under --mode strict; strict refuses \
trace/event artifacts and never downgrades to project mode",
2,
);
}
if let Err(error) = settings.strict_preflight() {
emit_error(json, command_name, "strict.unsupported_grant", &error);
return 2;
return fail(
stream,
json,
command_name,
"strict",
"strict.unsupported_grant",
&error,
2,
);
}
match marqdo_sandbox::inspect_strict_child() {
// A verified supervisor child may run project code.
@@ -101,8 +174,15 @@ pub fn execute(
Ok(StrictChildState::NotChild) => {
let platform = PlatformCapabilities::detect();
if let Err(error) = marqdo_sandbox::enforce(mode, &platform) {
emit_error(json, command_name, error.code(), &error.to_string());
return 1;
return fail(
stream,
json,
command_name,
"strict",
error.code(),
&error.to_string(),
1,
);
}
return launch_strict_process(&project.root, file, settings, json, command_name);
}
@@ -111,30 +191,49 @@ pub fn execute(
// module so macOS/Windows builds compile.
Err(reason) => {
let message = format!("strict isolation verification failed: {reason}");
emit_error(json, command_name, "strict.identity_invalid", &message);
return 1;
return fail(
stream,
json,
command_name,
"strict",
"strict.identity_invalid",
&message,
1,
);
}
}
}
let platform = PlatformCapabilities::detect();
if let Err(error) = marqdo_sandbox::enforce(mode, &platform) {
emit_error(
return fail(
stream,
json,
command_name,
"authorization",
"sandbox.unavailable",
&error.to_string(),
1,
);
return 1;
}
let capabilities = match settings.capabilities() {
Ok(capabilities) => capabilities,
Err(error) => {
emit_error(json, command_name, "cli.invalid_grants", &error);
return 2;
return fail(
stream,
json,
command_name,
"authorization",
"cli.invalid_grants",
&error,
2,
);
}
};
let stdin = read_stdin();
let mut host = StdHost::new(mode, capabilities, project.root.clone()).with_stdin(stdin);
if let Some(stream) = stream {
host = host.with_observation(stream.config(), Box::new(stream.host_sink()));
}
if trace || json || settings.trace_file.is_some() {
host = host
.with_trace()
@@ -161,12 +260,15 @@ pub fn execute(
}
for kernel_path in &kernel_paths {
if let Err(error) = load_kernel_descriptor(&mut host, kernel_path) {
if json {
output::print_json(&json!({"success": false, "error": error}));
} else {
eprintln!("error: {error}");
}
return 2;
return fail(
stream,
json,
command_name,
"prepare",
"ext.kernel_load_failed",
&error,
2,
);
}
}
// T6: verify every selected artifact/path/hash before starting a worker or
@@ -180,8 +282,7 @@ pub fn execute(
let providers = match provider_manifest::read_provider_manifests(&provider_paths) {
Ok(providers) => providers,
Err(error) => {
emit_prepare_error(json, command_name, &error);
return error.exit_code;
return fail_prepare(stream, json, command_name, "prepare", &error);
}
};
// `--provider` alone never authorizes; the exact `--allow-provider NAME`
@@ -201,8 +302,7 @@ pub fn execute(
provider.name, provider.name
),
);
emit_prepare_error(json, command_name, &error);
return error.exit_code;
return fail_prepare(stream, json, command_name, "authorization", &error);
}
}
// A grant that matches no selected provider is a deterministic option
@@ -217,8 +317,7 @@ pub fn execute(
providers are never discovered automatically"
),
);
emit_prepare_error(json, command_name, &error);
return error.exit_code;
return fail_prepare(stream, json, command_name, "authorization", &error);
}
}
for plugin in &static_plugins.plugins {
@@ -226,14 +325,12 @@ pub fn execute(
&plugin.base_dir,
&plugin.artifact_relative,
) {
emit_prepare_error(json, command_name, &error);
return error.exit_code;
return fail_prepare(stream, json, command_name, "authorization", &error);
}
let artifact = plugin.artifact_path();
if let Err(error) = extension_descriptors::verify_sha256(&artifact, &plugin.artifact_sha256)
{
emit_prepare_error(json, command_name, &error);
return error.exit_code;
return fail_prepare(stream, json, command_name, "authorization", &error);
}
}
// T7: start only selected, granted and verified extensions.
@@ -243,19 +340,29 @@ pub fn execute(
&plugin.manifest_path,
settings.trust_native_plugin,
) {
if json {
output::print_json(&json!({"success": false, "error": message}));
} else {
eprintln!("error: {message}");
}
return 1;
return fail(
stream,
json,
command_name,
"authorization",
"ext.plugin_load_failed",
&message,
1,
);
}
}
// T8: exact one-to-one reconciliation with the static descriptors. Never
// synthesise a missing signature (`params: Vec::new()`) or guess an export.
if let Err(message) = reconcile_plugin_exports(&static_plugins, &host) {
emit_error(json, command_name, "ext.plugin_binding_mismatch", &message);
return 1;
return fail(
stream,
json,
command_name,
"authorization",
"ext.plugin_binding_mismatch",
&message,
1,
);
}
// T7b: start only granted and verified provider helpers. Every manifest,
// artifact, target, grant and duplicate-kind check above already passed, so
@@ -266,8 +373,7 @@ pub fn execute(
provider,
&settings.allow_provider,
) {
emit_prepare_error(json, command_name, &error);
return error.exit_code;
return fail_prepare(stream, json, command_name, "prepare", &error);
}
}
let function_name = function.unwrap_or("main").to_string();
@@ -285,12 +391,15 @@ pub fn execute(
project.entry_file().path.display()
)
};
if json {
emit_error(true, command_name, "sem.no_entry_function", &message);
} else {
eprintln!("error[sem.no_entry_function]: {message}");
}
return 1;
return fail(
stream,
json,
command_name,
"compile",
"sem.no_entry_function",
&message,
1,
);
}
let runtime_args: Vec<EvaluatedArg> = named_args
.iter()
@@ -304,7 +413,19 @@ pub fn execute(
.max_region_memory
.unwrap_or(marqdo_runtime::DEFAULT_MAX_REGION_BYTES),
};
let result = run_with_limits(
if let Some(stream) = stream {
let entry = format!("{}::{}", project.entry, function_name);
let analysis_digest = format!("sha256:{}", crate::analysis::digest(&project));
if let Err(error) = stream.started(
&entry,
mode.as_str(),
&project.identity.root_id(),
&analysis_digest,
) {
return transport_failure(&error);
}
}
let mut result = run_with_limits(
&project.program,
&mut host,
&project.entry,
@@ -312,17 +433,58 @@ pub fn execute(
runtime_args,
memory_limits,
);
// A dead event pipe is a run failure even if the program itself returned.
if result.is_ok() {
if let Some(message) = host.observation_failure() {
result = Err(RuntimeError::new(
marqdo_value::ErrorCategory::Observation,
None,
format!("observe.transport_failed: {message}"),
));
}
}
let trace_run_id = stream.map(EventStream::run_id);
if let Some(path) = &settings.trace_file {
let status = match &result {
Ok(_) => "ok",
Err(error) => error.code(),
};
let entry = format!("{}::{}", project.entry, function_name);
if let Err(error) = crate::trace::write(&project, &host, &entry, status, path) {
emit_error(json, command_name, "cli.trace_failed", &error);
if let Err(error) = crate::trace::write(
&project,
&host,
&entry,
status,
path,
trace_run_id.as_deref(),
) {
if let Some(stream) = stream {
let _ = stream.failed_error("cli.trace_failed", &error);
} else {
emit_error(json, command_name, "cli.trace_failed", &error);
}
return 1;
}
}
if let Some(stream) = stream {
// The program's stdout/stderr were already emitted as chunk events at
// their real host commit points; never replay them on this channel.
match &result {
Ok(outcome) => {
if let Err(error) = stream.completed(&outcome.value) {
return transport_failure(&error);
}
}
Err(error) => {
if let Err(write_error) = stream.failed(error) {
eprintln!("error[observe.transport_failed]: {write_error}");
}
eprint!("{}", render_runtime_error(error, &project));
return 1;
}
}
return 0;
}
let stdout = host.take_output();
let stdout_text = String::from_utf8_lossy(&stdout).to_string();
let stderr = host.take_error();
@@ -383,6 +545,53 @@ pub fn execute(
}
}
fn transport_failure(error: &str) -> i32 {
eprintln!("error[observe.transport_failed]: {error}");
1
}
/// Emit one pre-execution rejection (or the legacy non-event error form).
fn fail(
stream: Option<&EventStream>,
json: bool,
command_name: &str,
phase: &str,
code: &str,
message: &str,
exit: i32,
) -> i32 {
if let Some(stream) = stream {
if let Err(error) = stream.rejected(phase, code, message) {
return transport_failure(&error);
}
}
if json && stream.is_none() {
emit_error(json, command_name, code, message);
} else {
eprintln!("error[{code}]: {message}");
}
exit
}
/// Emit one prepare/authorization rejection using a `PrepareError`.
fn fail_prepare(
stream: Option<&EventStream>,
json: bool,
command_name: &str,
phase: &str,
error: &crate::extension_descriptors::PrepareError,
) -> i32 {
if let Some(stream) = stream {
if let Err(write_error) = stream.rejected(phase, &error.code, &error.message) {
return transport_failure(&write_error);
}
eprintln!("error[{}]: {}", error.code, error.message);
} else {
emit_prepare_error(json, command_name, error);
}
error.exit_code
}
fn emit_prepare_error(
json: bool,
command_name: &str,
+534
View File
@@ -0,0 +1,534 @@
//! Execution Events v1 JSONL writer and public envelope.
//!
//! Normative contract: `spec/execution-events-v1.md`; decision record:
//! ADR-0059. The host owns the typed events; this module only assigns the
//! public envelope/sequence, encodes bytes as base64 and writes one flushed
//! line per event to the CLI protocol stdout.
//!
//! There is no HTTP, SSE, WebSocket, View or Agent branch here.
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::io::{self, Write};
use std::rc::Rc;
use std::time::{SystemTime, UNIX_EPOCH};
use marqdo_host::{
FrameId, ObservationConfig, ObservationError, ObservationEvent, ObservationSink,
ParameterSource, ValueState, DEFAULT_MAX_OBSERVATION_BYTES, DEFAULT_MAX_OBSERVATION_EVENTS,
DEFAULT_MAX_VALUE_BYTES,
};
use marqdo_value::{RuntimeError, Value};
use serde_json::{json, Value as Json};
/// Public schema name.
pub const SCHEMA: &str = "marqdo.execution-events";
/// Public schema major version.
pub const SCHEMA_VERSION: u32 = 1;
/// The only accepted `--event-stream` format in v1.
pub const FORMAT_JSONL: &str = "jsonl";
/// CLI observation options.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObservationOptions {
/// Exact local-variable names selected with `--observe-variable`.
pub variables: Vec<String>,
/// Exact parameter names selected with `--observe-parameter`.
pub parameters: Vec<String>,
/// Second explicit consent for `Text` bytes.
pub allow_text_values: bool,
/// Maximum encoded source bytes for one value (`0` = unlimited).
pub max_value_bytes: usize,
/// Structural/value event budget (`0` = unlimited).
pub max_events: u64,
/// Approximate structural/value byte budget (`0` = unlimited).
pub max_bytes: u64,
}
impl Default for ObservationOptions {
fn default() -> Self {
Self {
variables: Vec::new(),
parameters: Vec::new(),
allow_text_values: false,
max_value_bytes: DEFAULT_MAX_VALUE_BYTES,
max_events: DEFAULT_MAX_OBSERVATION_EVENTS,
max_bytes: DEFAULT_MAX_OBSERVATION_BYTES,
}
}
}
impl ObservationOptions {
/// Typed host policy for this run.
pub fn config(&self) -> ObservationConfig {
ObservationConfig {
structural: true,
selected_variables: self.variables.iter().cloned().collect::<BTreeSet<_>>(),
selected_parameters: self.parameters.iter().cloned().collect::<BTreeSet<_>>(),
allow_text_values: self.allow_text_values,
max_observation_events: self.max_events,
max_observation_bytes: self.max_bytes,
max_value_bytes: self.max_value_bytes,
}
}
}
/// One run's JSONL writer, shared with the host sink.
#[derive(Clone)]
pub struct EventStream {
inner: Rc<RefCell<JsonlWriter>>,
config: ObservationConfig,
}
impl EventStream {
/// Open the protocol channel and write `stream.opened` as `seq: 0`.
pub fn open(options: &ObservationOptions) -> Result<Self, String> {
let run_id = new_run_id();
let config = options.config();
let opened = json!({
"producer": {
"name": "marqdo",
"version": env!("CARGO_PKG_VERSION"),
},
"protocol": {
"name": SCHEMA,
"schema_version": SCHEMA_VERSION,
},
"observation": {
"level": config.level(),
"values": config.values_enabled(),
"selected_variables": config.selected_variables.iter().cloned().collect::<Vec<_>>(),
"selected_parameters": config.selected_parameters.iter().cloned().collect::<Vec<_>>(),
"allow_text_values": config.allow_text_values,
},
"limits": {
"max_observation_events": config.max_observation_events,
"max_observation_bytes": config.max_observation_bytes,
"max_value_bytes": config.max_value_bytes,
},
});
let mut writer = JsonlWriter {
out: Box::new(io::stdout()),
run_id: run_id.clone(),
seq: 0,
};
writer.emit("stream.opened", opened)?;
Ok(Self {
inner: Rc::new(RefCell::new(writer)),
config,
})
}
/// Opaque run identity shared with Trace v2 when both are requested.
pub fn run_id(&self) -> String {
self.inner.borrow().run_id.clone()
}
/// Typed policy (used to install the host sink).
pub fn config(&self) -> ObservationConfig {
self.config.clone()
}
/// Installable host sink handle.
pub fn host_sink(&self) -> HostEventSink {
HostEventSink {
stream: self.clone(),
}
}
/// Write one raw public event. Used for lifecycle and terminal events.
pub fn emit(&self, kind: &str, payload: Json) -> Result<(), String> {
self.inner.borrow_mut().emit(kind, payload)
}
/// Write `run.started`.
pub fn started(
&self,
entry: &str,
mode: &str,
root_id: &str,
analysis_digest: &str,
) -> Result<(), String> {
self.emit(
"run.started",
json!({
"entry": entry,
"mode": mode,
"source": {
"root_id": root_id,
"analysis_digest": analysis_digest,
},
"started_unix_ms": unix_millis(),
}),
)
}
/// Write the successful terminal event.
pub fn completed(&self, value: &Value) -> Result<(), String> {
self.emit(
"run.completed",
json!({"status": "ok", "value": marqdo_value::to_wire(value)}),
)
}
/// Write the failed terminal event.
pub fn failed(&self, error: &RuntimeError) -> Result<(), String> {
self.emit(
"run.failed",
json!({
"status": "error",
"error": {
"code": error.code(),
"message": error.message,
"module_id": error.module_id.as_deref(),
"span": error.span.map(span_json),
}
}),
)
}
/// Write a failed terminal event for a CLI-level failure that still has a
/// stable code (for example `cli.trace_failed`).
pub fn failed_error(&self, code: &str, message: &str) -> Result<(), String> {
self.emit(
"run.failed",
json!({
"status": "error",
"error": {"code": code, "message": message},
}),
)
}
/// Write the pre-execution rejection terminal event.
pub fn rejected(&self, phase: &str, code: &str, message: &str) -> Result<(), String> {
self.emit(
"run.rejected",
json!({
"status": "rejected",
"phase": phase,
"error": {"code": code, "message": message},
}),
)
}
/// Encode and write one typed host/runtime observation event.
pub fn emit_observation(&self, event: ObservationEvent<'_>) -> Result<(), String> {
let (kind, payload) = observation_payload(event);
self.emit(kind, payload)
}
}
/// `ObservationSink` adapter installed into `StdHost`.
pub struct HostEventSink {
stream: EventStream,
}
impl ObservationSink for HostEventSink {
fn emit(&mut self, event: ObservationEvent<'_>) -> Result<(), ObservationError> {
self.stream
.emit_observation(event)
.map_err(ObservationError::new)
}
}
struct JsonlWriter {
out: Box<dyn Write>,
run_id: String,
seq: u64,
}
impl JsonlWriter {
fn emit(&mut self, kind: &str, payload: Json) -> Result<(), String> {
let event = json!({
"schema": SCHEMA,
"schema_version": SCHEMA_VERSION,
"run_id": self.run_id,
"seq": self.seq,
"kind": kind,
"payload": payload,
});
let mut line = serde_json::to_vec(&event)
.map_err(|error| format!("cannot encode execution event: {error}"))?;
line.push(b'\n');
// Flush each line: line-buffered batching would hide real-time output
// behind the run duration (RFC-OBS-0001 §5.2).
self.out
.write_all(&line)
.and_then(|()| self.out.flush())
.map_err(|error| format!("cannot write execution event stream: {error}"))?;
self.seq += 1;
Ok(())
}
}
fn observation_payload(event: ObservationEvent<'_>) -> (&'static str, Json) {
match event {
ObservationEvent::StdoutChunk { data } => (
"stdout.chunk",
json!({
"encoding": "base64",
"data_base64": base64_encode(data),
"byte_count": data.len(),
}),
),
ObservationEvent::StderrChunk { data } => (
"stderr.chunk",
json!({
"encoding": "base64",
"data_base64": base64_encode(data),
"byte_count": data.len(),
}),
),
ObservationEvent::CallEnter {
frame_id,
parent_frame_id,
function_id,
module_id,
span,
} => (
"call.enter",
json!({
"frame_id": frame_id.as_wire(),
"parent_frame_id": parent_frame_id.map(FrameId::as_wire),
"function_id": function_id,
"module_id": module_id,
"span": span.map(span_json),
}),
),
ObservationEvent::CallExit {
frame_id,
status,
module_id,
span,
} => (
"call.exit",
json!({
"frame_id": frame_id.as_wire(),
"status": status,
"module_id": module_id,
"span": span.map(span_json),
}),
),
ObservationEvent::Return {
frame_id,
value_type,
module_id,
span,
} => (
"return",
json!({
"frame_id": frame_id.as_wire(),
"value_type": value_type,
"module_id": module_id,
"span": span.map(span_json),
}),
),
ObservationEvent::BranchEval {
frame_id,
result,
module_id,
span,
} => (
"branch.eval",
json!({
"frame_id": frame_id.as_wire(),
"result": result,
"module_id": module_id,
"span": span.map(span_json),
}),
),
ObservationEvent::LoopIteration {
frame_id,
decision,
module_id,
span,
} => (
"loop.iteration",
json!({
"frame_id": frame_id.as_wire(),
"decision": decision,
"module_id": module_id,
"span": span.map(span_json),
}),
),
ObservationEvent::Effect {
operation,
result_code,
module_id,
span,
} => (
"effect",
json!({
"operation": operation,
"result_code": result_code,
"module_id": module_id,
"span": span.map(span_json),
}),
),
ObservationEvent::VariableAssigned {
frame_id,
name,
value_type,
module_id,
span,
value_state,
value,
} => (
"variable.assigned",
value_event_payload(
frame_id,
name,
None,
value_type,
module_id,
span,
value_state,
value,
),
),
ObservationEvent::ParameterObserved {
frame_id,
name,
source,
value_type,
module_id,
span,
value_state,
value,
} => (
"parameter.observed",
value_event_payload(
frame_id,
name,
Some(source),
value_type,
module_id,
span,
value_state,
value,
),
),
ObservationEvent::ObservationTruncated {
reason,
limit,
written,
dropped,
} => (
"observation.truncated",
json!({
"reason": reason,
"limit": limit,
"written": written,
"dropped": dropped,
}),
),
}
}
#[allow(clippy::too_many_arguments)]
fn value_event_payload(
frame_id: FrameId,
name: &str,
source: Option<ParameterSource>,
value_type: &str,
module_id: &str,
span: Option<marqdo_span::Span>,
value_state: ValueState,
value: Option<marqdo_host::ObservedValue>,
) -> Json {
let mut payload = json!({
"frame_id": frame_id.as_wire(),
"name": name,
"value_type": value_type,
"module_id": module_id,
"span": span.map(span_json),
"value_state": value_state.as_str(),
});
if let Some(source) = source {
payload["source"] = Json::String(source.as_str().to_string());
}
if let Some(value) = value {
payload["value"] = value.wire;
}
payload
}
fn span_json(span: marqdo_span::Span) -> Json {
json!({"start": span.start, "end": span.end})
}
fn new_run_id() -> String {
format!("{}-{}", std::process::id(), unix_nanos())
}
fn unix_nanos() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_nanos())
.unwrap_or(0)
}
fn unix_millis() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0)
}
/// Standard padded base64 (no external dependency; the protocol only needs
/// one canonical encoding).
pub fn base64_encode(bytes: &[u8]) -> String {
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let b0 = chunk[0] as u32;
let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
let triple = (b0 << 16) | (b1 << 8) | b2;
out.push(ALPHABET[((triple >> 18) & 0x3f) as usize] as char);
out.push(ALPHABET[((triple >> 12) & 0x3f) as usize] as char);
out.push(if chunk.len() > 1 {
ALPHABET[((triple >> 6) & 0x3f) as usize] as char
} else {
'='
});
out.push(if chunk.len() > 2 {
ALPHABET[(triple & 0x3f) as usize] as char
} else {
'='
});
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn base64_matches_known_vectors() {
assert_eq!(base64_encode(b""), "");
assert_eq!(base64_encode(b"f"), "Zg==");
assert_eq!(base64_encode(b"fo"), "Zm8=");
assert_eq!(base64_encode(b"foo"), "Zm9v");
assert_eq!(
base64_encode("开始文本\n".as_bytes()),
"5byA5aeL5paH5pysCg=="
);
}
#[test]
fn no_unselected_value_is_encoded() {
let options = ObservationOptions {
variables: vec!["visible".to_string()],
..ObservationOptions::default()
};
let config = options.config();
assert_eq!(config.level(), "values");
assert!(config.selected_variables.contains("visible"));
assert!(!config.allow_text_values);
let empty = ObservationOptions::default().config();
assert_eq!(empty.level(), "structural");
assert!(!empty.values_enabled());
}
}
+104 -14
View File
@@ -7,6 +7,7 @@
mod analysis;
mod args;
mod commands;
mod execution_events;
mod extension_descriptors;
mod extension_selection;
mod loader;
@@ -34,6 +35,83 @@ struct Cli {
command: Command,
}
/// Execution Events v1 options (RFC-OBS-0001).
///
/// `--event-stream jsonl` makes the CLI stdout a dedicated NDJSON channel;
/// program stdout/stderr become events. It is mutually exclusive with
/// `--json` and is not accepted by static commands.
#[derive(Args, Clone)]
struct ObserveArgs {
/// Stream Execution Events v1 as JSON Lines on stdout (only `jsonl`).
#[arg(
long = "event-stream",
value_name = "FORMAT",
value_parser = [execution_events::FORMAT_JSONL],
conflicts_with = "json"
)]
event_stream: Option<String>,
/// Publish the value of one exactly named local variable (repeatable).
#[arg(
long = "observe-variable",
value_name = "NAME",
requires = "event_stream"
)]
observe_variable: Vec<String>,
/// Publish the value of one exactly named parameter (repeatable).
#[arg(
long = "observe-parameter",
value_name = "NAME",
requires = "event_stream"
)]
observe_parameter: Vec<String>,
/// Allow selected Text values to be published (second explicit consent).
#[arg(long = "observe-text-values", requires = "event_stream")]
observe_text_values: bool,
/// Maximum encoded source bytes for one observed value (0 = unlimited).
#[arg(
long = "observe-max-value-bytes",
value_name = "BYTES",
requires = "event_stream"
)]
observe_max_value_bytes: Option<usize>,
/// Override the structural/value event budget (0 = unlimited).
#[arg(
long = "observe-max-events",
value_name = "COUNT",
requires = "event_stream"
)]
observe_max_events: Option<u64>,
/// Override the approximate structural/value byte budget (0 = unlimited).
#[arg(
long = "observe-max-bytes",
value_name = "BYTES",
requires = "event_stream"
)]
observe_max_bytes: Option<u64>,
}
impl ObserveArgs {
fn options(&self) -> Option<execution_events::ObservationOptions> {
self.event_stream.as_ref()?;
let mut options = execution_events::ObservationOptions {
variables: self.observe_variable.clone(),
parameters: self.observe_parameter.clone(),
allow_text_values: self.observe_text_values,
..execution_events::ObservationOptions::default()
};
if let Some(bytes) = self.observe_max_value_bytes {
options.max_value_bytes = bytes;
}
if let Some(events) = self.observe_max_events {
options.max_events = events;
}
if let Some(bytes) = self.observe_max_bytes {
options.max_bytes = bytes;
}
Some(options)
}
}
#[derive(Subcommand)]
enum Command {
/// Initialise a new Marqdo project.
@@ -201,6 +279,8 @@ struct RunArgs {
/// Include the host effect trace.
#[arg(long)]
trace: bool,
#[command(flatten)]
observe: ObserveArgs,
}
#[derive(Args)]
@@ -220,6 +300,8 @@ struct CallArgs {
/// Include the host effect trace.
#[arg(long)]
trace: bool,
#[command(flatten)]
observe: ObserveArgs,
}
#[derive(Args)]
@@ -432,23 +514,31 @@ fn dispatch() -> i32 {
};
commands::inspect(args.file.as_deref(), &flags, args.json, &args.plugin)
}
Command::Run(args) => commands::execute(
args.file.as_deref(),
None,
&[],
&args.sandbox.settings(),
args.json,
args.trace,
),
Command::Call(args) => match commands::parse_named_args(&args.args) {
Ok(named) => commands::execute(
Some(&args.file),
Some(&args.function),
&named,
Command::Run(args) => {
let observation = args.observe.options();
commands::execute(
args.file.as_deref(),
None,
&[],
&args.sandbox.settings(),
args.json,
args.trace,
),
observation.as_ref(),
)
}
Command::Call(args) => match commands::parse_named_args(&args.args) {
Ok(named) => {
let observation = args.observe.options();
commands::execute(
Some(&args.file),
Some(&args.function),
&named,
&args.sandbox.settings(),
args.json,
args.trace,
observation.as_ref(),
)
}
Err(error) => {
eprintln!("error: {error}");
2
+13 -8
View File
@@ -21,15 +21,20 @@ pub fn write(
entry: &str,
status: &str,
path: &Path,
run_id: Option<&str>,
) -> Result<(), String> {
let run_id = format!(
"{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_nanos())
.unwrap_or(0)
);
// A combined `--event-stream` run shares the CLI-generated opaque run id
// with Execution Events v1; Trace v2 keeps its schema and field types.
let run_id = run_id.map(str::to_string).unwrap_or_else(|| {
format!(
"{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_nanos())
.unwrap_or(0)
)
});
let analysis_digest = crate::analysis::digest(project);
let root_id = project.identity.root_id();
let state = host.trace_state();