feat(cli): route print through the bundled stdlib (Compat-B)

- Add Program.print_override and lazy stdlib injection: only programs that
  call 打印/print load std:io and route to io.写值 (ADR-0030).
- Record resource.open/read/write/close as trace effect events so the stdlib
  print path is auditable; HostOp::Print no longer runs on that path.
- Permit only stdio/stdout (existing console) in strict; file/network
  resource opens remain permission.denied.
- Exclude std: modules from the human-readable checked-module count; update
  the Python analysis consumer to accept std: instance ids.
- Add print-routing and strict-console tests, ADR-0030 and evidence.

Gate: fmt, clippy -D warnings, 418 tests passed.
This commit is contained in:
cflmy committed 2026-11-20 22:10:59 +08:00
1 parent e119727c5d
commit e26971f489
21 files changed
+560 -36

No files matched your search

+5
View File
@@ -108,6 +108,11 @@ currently `0.1.0`; no release tag has been cut. Dates use ISO 8601.
an `fs_write` grant, buffers in the host, flushes only on `close`, never
flushes on drop, has an 8 MiB ceiling, and is refused in pure/strict. A
`.mq.md` chunked copy loop reproduces a file without a Rust `copy_file`.
- Compat-B `打印`/`print` routing (ADR-0030): a `Program.print_override` sends
print calls to bundled `io.写值` when the program uses them; stdlib modules
are injected lazily, resource operations appear as trace effects and the
legacy `HostOp::Print` path does not run. Strict permits only stdio/stdout
(existing console), not file/network resources. No Compat-C removal yet.
- Full-value stdlib formatting (ADR-0029): FunctionRef `module/name` and
PluginHandle `plugin/export` read-only members (host id private);
`stdlib/format.mq.md` now reproduces builtin `打印`/`repr` for all 15 value
+3 -2
View File
@@ -102,8 +102,9 @@ executes anything.
* **Bundled stdlib + print prototype (RFC-CM CM-A07/A10)** — offline
`stdlib/` with explicit `std:io`/`std:format`/`std:text` imports, SHA-256
digest verification, Analysis identity and a frozen-CLI closure test; the
`.mq.md` print chain matches builtin `打印` for all 15 value types, while `打印` remains the default
builtin (ADR-0027/0028/0029).
`.mq.md` print chain is the execution path for `打印`/`print` when the bundle is present (ADR-0030),
covering all 15 value types; the legacy builtin is only a no-bundle fallback
(ADR-0027/0028/0029/0030).
* **Resource streams (RFC-CM CM-A06)** — opaque host-issued `Resource` handles;
`resource.open/read/write/close` transfer bytes through `Memory`; stdout
write in pure/project, granted project-file read, and buffered project-file
+7 -1
View File
@@ -353,7 +353,13 @@ pub fn check(
let text = output::render_text(&project.diagnostics);
eprint!("{text}");
if errors == 0 {
println!("checked {} module(s): ok", project.program.modules.len());
let checked = project
.program
.modules
.keys()
.filter(|id| !id.instance.starts_with("std:"))
.count();
println!("checked {checked} module(s): ok");
} else {
eprintln!(
"check failed: {} error(s), {} warning(s)",
+73 -1
View File
@@ -13,8 +13,9 @@
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use marqdo_expression::Expr;
use marqdo_frontend::{analyze_source, AnalysisResult};
use marqdo_ir::{ModuleId, Program, ResolvedImport};
use marqdo_ir::{ModuleId, PlaceTarget, Program, ResolvedImport, Stmt};
use marqdo_package::{
discover_root, LockFile, Manifest, PackageResolver, ResolvedPackage, LOCK_FILE, MANIFEST_FILE,
};
@@ -278,6 +279,23 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
};
let entry_id = loader.load_module(&entry_module.package, &entry_module.relative_path)?;
loader.program.main_module = Some(entry_id.clone());
// Compat-B (ADR-0030): only programs that actually call 打印/print need the
// stdlib print module; injecting it for every program would change the
// module graph, analysis output and startup cost for no benefit.
if program_uses_print(&loader.program) {
if let Some(stdlib) = loader.stdlib.clone() {
let package = stdlib.package();
let io = loader.load_module(&package, "io.mq.md")?;
let Some(module) = loader.program.module(&io) else {
return Err("stdlib print module was not loaded".to_string());
};
let Some(function) = module.function("写值") else {
return Err("stdlib print module has no `写值` function".to_string());
};
loader.program.print_override = Some(function.id.clone());
}
}
for (module_id, diagnostic) in marqdo_frontend::calls::validate_program(&loader.program) {
if let Some(file) = loader.files.get(&module_id) {
loader.diagnostics.push(FileDiagnostic {
@@ -499,3 +517,57 @@ impl Loader {
Ok(module_id)
}
}
/// Whether the loaded program calls the legacy `打印`/`print` builtin.
fn program_uses_print(program: &Program) -> bool {
program.modules.values().any(|module| {
module
.functions
.iter()
.any(|function| stmts_use_print(&function.body))
})
}
fn stmts_use_print(stmts: &[Stmt]) -> bool {
stmts.iter().any(|stmt| match stmt {
Stmt::Assign { target, value, .. } => {
let target_uses = match target {
PlaceTarget::Local { .. } => false,
PlaceTarget::Index { base, index, .. } => {
expr_uses_print(base) || expr_uses_print(index)
}
};
target_uses || expr_uses_print(value)
}
Stmt::Eval { expr, .. } | Stmt::Return { value: expr, .. } => expr_uses_print(expr),
Stmt::Call { call, .. } => call.args.iter().any(|arg| expr_uses_print(&arg.value)),
Stmt::Branch { arms, .. } => arms
.iter()
.any(|arm| expr_uses_print(&arm.condition) || stmts_use_print(&arm.body)),
Stmt::Loop {
condition, body, ..
} => expr_uses_print(condition) || stmts_use_print(body),
})
}
fn expr_uses_print(expr: &Expr) -> bool {
match expr {
Expr::Call { callee, args, .. } => {
matches!(callee.as_ref(), Expr::Ident(ident) if matches!(ident.name.as_str(), "打印" | "print"))
|| expr_uses_print(callee)
|| args.iter().any(|arg| expr_uses_print(&arg.value))
}
Expr::List(items) => items.iter().any(expr_uses_print),
Expr::Record(entries) => entries.iter().any(|entry| expr_uses_print(&entry.value)),
Expr::Unary { operand, .. } => expr_uses_print(operand),
Expr::Binary { left, right, .. } => expr_uses_print(left) || expr_uses_print(right),
Expr::Member { base, .. } => expr_uses_print(base),
Expr::Index { base, index, .. } => expr_uses_print(base) || expr_uses_print(index),
Expr::Int(_)
| Expr::Float(_)
| Expr::Text(_)
| Expr::Bool(_)
| Expr::None
| Expr::Ident(_) => false,
}
}
+111
View File
@@ -0,0 +1,111 @@
//! Compat-B `打印` routing tests (ADR-0030).
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
struct TempDir(PathBuf);
impl TempDir {
fn new(tag: &str) -> Self {
let path =
std::env::temp_dir().join(format!("marqdo-print-route-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).unwrap();
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn cli(dir: &Path, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(args)
.current_dir(dir)
.output()
.expect("run marqdo")
}
fn write(dir: &Path, name: &str, source: &str) -> PathBuf {
let path = dir.join(name);
std::fs::write(&path, source).unwrap();
path
}
#[test]
fn print_is_routed_through_the_stdlib_and_not_hostop_print() {
let dir = TempDir::new("routed");
write(
dir.path(),
"index.mq.md",
"## main\n\n> 打印([\"x\", 1])\n\n*None*\n",
);
let trace = dir.path().join("trace.json");
let output = cli(
dir.path(),
&[
"run",
"index.mq.md",
"--trace-file",
trace.to_str().unwrap(),
],
);
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(String::from_utf8_lossy(&output.stdout), "[\"x\", 1]\n");
let trace: serde_json::Value = serde_json::from_slice(&std::fs::read(&trace).unwrap()).unwrap();
let operations: Vec<&str> = trace["events"]
.as_array()
.unwrap()
.iter()
.filter_map(|event| event["operation"].as_str())
.collect();
assert!(
operations.contains(&"resource.write"),
"stdlib print must write through the resource ABI: {operations:?}"
);
assert!(
!operations.contains(&"print"),
"legacy HostOp::Print must not run in Compat-B: {operations:?}"
);
}
#[test]
fn stdlib_print_modules_are_injected_only_when_print_is_used() {
let dir = TempDir::new("lazy");
write(dir.path(), "plain.mq.md", "## main\n\n*1*\n");
write(
dir.path(),
"prints.mq.md",
"## main\n\n> 打印(1)\n\n*None*\n",
);
for (file, expected_stdlib) in [("plain.mq.md", false), ("prints.mq.md", true)] {
let target = dir.path().join(format!("{file}.json"));
let output = cli(
dir.path(),
&["check", file, "--emit", target.to_str().unwrap()],
);
assert_eq!(output.status.code(), Some(0), "{file}");
let analysis: serde_json::Value =
serde_json::from_slice(&std::fs::read(&target).unwrap()).unwrap();
let modules = analysis["modules"].as_array().unwrap();
let has_stdlib = modules
.iter()
.any(|module| module["id"].as_str().unwrap_or("").starts_with("std:"));
assert_eq!(has_stdlib, expected_stdlib, "{file}: {modules:?}");
// The bundle identity is recorded whenever a bundle is available; it
// does not imply that this program loaded any stdlib module.
assert!(analysis["project"]["stdlib"].is_object(), "{file}");
}
}
+66 -6
View File
@@ -590,6 +590,34 @@ impl StdHost {
Ok(Value::Unit)
}
fn record_resource_effect(
&mut self,
op: &str,
ctx: &ExecutionContext,
started: Instant,
summary: &str,
) {
let estimate = TraceState::estimate(
"effect",
None,
Some(&ctx.module_id),
Some(summary),
Some(op),
);
if !self.trace_state.accept(estimate) {
return;
}
let seq = self.next_seq();
self.trace.record(
seq,
op,
ctx.module_id.clone(),
ctx.span,
started.elapsed(),
summary.to_string(),
);
}
fn open_resource_stream(
&mut self,
kind: &str,
@@ -597,12 +625,21 @@ impl StdHost {
rights: &[String],
ctx: &ExecutionContext,
) -> Result<Value, RuntimeError> {
let started = Instant::now();
let requested: BTreeSet<&str> = rights.iter().map(String::as_str).collect();
if self.mode == SandboxMode::Strict {
return Err(RuntimeError::new(
ErrorCategory::PermissionDenied,
ctx.span,
"resource streams are not verified in strict mode and are refused",
));
// stdio/stdout is the existing console channel already permitted by
// strict L1 (CM-A11); file/network/process providers are not.
let stdio_console =
kind == "stdio" && descriptor == "stdout" && requested == BTreeSet::from(["write"]);
if !stdio_console {
return Err(RuntimeError::new(
ErrorCategory::PermissionDenied,
ctx.span,
"strict mode permits only the existing stdio/stdout console channel; \
other resource streams are refused",
));
}
}
if self.mode == SandboxMode::Pure && kind == "file" {
return Err(RuntimeError::new(
@@ -611,7 +648,6 @@ impl StdHost {
"pure mode does not provide project file resources",
));
}
let requested: BTreeSet<&str> = rights.iter().map(String::as_str).collect();
match kind {
"stdio" => {
if descriptor != "stdout" || requested != BTreeSet::from(["write"]) {
@@ -636,6 +672,7 @@ impl StdHost {
ctx.span,
Some("kind=stdio descriptor=stdout"),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
Ok(Value::Resource(handle))
}
"file" => {
@@ -651,6 +688,7 @@ impl StdHost {
ctx.span,
Some("kind=file rights=read"),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
return Ok(Value::Resource(handle));
}
if requested == BTreeSet::from(["write"]) {
@@ -664,6 +702,7 @@ impl StdHost {
ctx.span,
Some("kind=file rights=write"),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
return Ok(Value::Resource(handle));
}
Err(RuntimeError::new(
@@ -756,6 +795,7 @@ impl Host for StdHost {
max_bytes: usize,
ctx: &ExecutionContext,
) -> Result<ResourceRead, RuntimeError> {
let started = Instant::now();
let kind = self.resources.kind(handle, ctx.span)?;
let read = self.resources.read(handle, max_bytes, ctx.span)?;
self.record_semantic(
@@ -769,6 +809,12 @@ impl Host for StdHost {
read.eof
)),
);
self.record_resource_effect(
"resource.read",
ctx,
started,
&format!("kind={kind} count={}", read.bytes.len()),
);
Ok(read)
}
@@ -778,6 +824,7 @@ impl Host for StdHost {
bytes: &[u8],
ctx: &ExecutionContext,
) -> Result<usize, RuntimeError> {
let started = Instant::now();
let kind = self.resources.kind(handle, ctx.span)?;
let count = self.resources.write(handle, bytes, ctx.span)?;
if kind == "stdio" {
@@ -791,6 +838,12 @@ impl Host for StdHost {
ctx.span,
Some(&format!("kind={kind} count={count}")),
);
self.record_resource_effect(
"resource.write",
ctx,
started,
&format!("kind={kind} count={count}"),
);
Ok(count)
}
@@ -799,6 +852,7 @@ impl Host for StdHost {
handle: ResourceHandle,
ctx: &ExecutionContext,
) -> Result<(), RuntimeError> {
let started = Instant::now();
let pending = self.resources.pending_file_write(handle, ctx.span)?;
let count = match pending {
Some((relative, bytes)) => {
@@ -815,6 +869,12 @@ impl Host for StdHost {
ctx.span,
Some(&format!("flushed_bytes={count}")),
);
self.record_resource_effect(
"resource.close",
ctx,
started,
&format!("flushed_bytes={count}"),
);
Ok(())
}
+16 -4
View File
@@ -54,13 +54,25 @@ fn stdout_write_obeys_the_output_budget_atomically() {
}
#[test]
fn strict_mode_refuses_every_resource_open() {
fn strict_mode_allows_only_the_existing_stdio_console_channel() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Strict, Capabilities::pure(), dir.path());
let error = host
.resource_open("stdio", "stdout", &["write".to_string()], &context())
let ctx = context();
let handle = handle(
host.resource_open("stdio", "stdout", &["write".to_string()], &ctx)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"strict", &ctx).unwrap(), 6);
assert_eq!(host.take_output(), b"strict");
let file = host
.resource_open("file", "a.txt", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
assert_eq!(file.category, ErrorCategory::PermissionDenied);
let socket = host
.resource_open("socket", "example.test", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(socket.category, ErrorCategory::PermissionDenied);
}
#[test]
+4
View File
@@ -517,6 +517,10 @@ pub struct Program {
/// metadata only: `check` never loads a library.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub external_callables: Vec<callable::CallableDescriptor>,
/// Compat-B `打印`/`print` destination (ADR-0030). When set, the runtime
/// calls this stdlib function instead of the legacy `HostOp::Print`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub print_override: Option<FunctionId>,
}
impl Program {
+19
View File
@@ -656,6 +656,25 @@ impl<'a> Interpreter<'a> {
let args = self.eval_args(args, frame)?;
return self.call_value(program, value, args, Some(span), Some(&caller_module));
}
if matches!(ident.name.as_str(), "打印" | "print") {
if let Some(target) = program.print_override.clone() {
let args = self.eval_args(args, frame)?;
let Some(function) = program.function(&target) else {
return Err(RuntimeError::new(
ErrorCategory::UnknownSymbol,
Some(span),
format!("print override `{target}` does not exist"),
));
};
return self.call_function(
program,
function,
args,
Some(span),
Some(&caller_module),
);
}
}
if let Some(builtin) = builtins::find(&ident.name) {
let args = self.eval_args(args, frame)?;
return self.call_builtin(builtin, args, Some(span), frame);
+9 -1
View File
@@ -123,6 +123,11 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL
(`format.显示` → UTF-8 encode → `Memory` → `resource.write(stdout)`) matches
builtin `打印` for the covered value set; builtin remains default and no
alias is routed.
- Compat-B print routing (ADR-0030): `Program.print_override` routes
`打印/print` to the bundled `io.写值` when the program uses it; stdlib
modules are injected lazily; resource operations are trace `effect` events
and `HostOp::Print` no longer runs on that path. Strict permits only
stdio/stdout; file/network resources remain refused.
- Full-value stdlib formatting (ADR-0029): FunctionRef `module/name` and
PluginHandle `plugin/export` are read-only observation members (host id
private); `stdlib/format.mq.md` reproduces builtin `打印`/`repr` for all 15
@@ -177,7 +182,7 @@ behavior. This table records only what is in this tree now.
| P0 freeze (ADRs, spec deltas, baseline) | **P1 + partial P2 slice done** | ADR-0022 freezes CM-A01/CM-A02/CM-A03; ADR-0023 freezes CM-A04 (minimal List/Map/Text/Table observation and construction); ADR-0024 freezes CM-A05 (recoverable `Result` map convention); `spec/expression.ebnf`, `spec/evaluation.md`, `spec/value-wire.md`, `spec/analysis-v2.md`, `spec/security.md` updated; existing AST/value goldens unchanged |
| P1 Memory + place assignment | **Implemented locally** | `Value::Memory`, run-scoped `MemoryStore` (64 KiB pages, 64 MiB total / 16 MiB region), `memory.new/size/grow`, `m[i]` read, `m[i] = byte`; semantic fixtures 45–47, runtime negatives 13–18, `examples/memory-buffer.mq.md`; evidence [`evidence/2026-11-20-memory-p1.md`](evidence/2026-11-20-memory-p1.md) |
| P2 Marqdo-written allocator/data structures + minimal structure observation | **PARTIAL, gate NOT passed** | `.mq.md`-only evidence: bump allocator with u32 codec, linked list, failed-allocation atomicity (48–50); CM-A04 structure primitives and a table filter (51–54, ADR-0023); a first-fit free-list allocator with block reuse, splitting, address-adjacent coalescing and `Result` maps (55, ADR-0024), a `Memory`-backed binary tree with recursive insert/in-order traversal (56), a `.mq.md` CSV row splitter (57), an open-addressing hash table with linear probing, update and `hash.full` (58), a `.mq.md` UTF-8 encoder over `Memory` (59), a validating UTF-8 decoder rejecting truncated, bad-continuation and surrogate/overlong input (60), a `Memory`-backed graph adjacency list (61), a recursive JSON parser for nested arrays/objects, strings with escapes, integers, booleans, null and error Results (62), a `Memory`-backed stack bytecode VM with PUSH/ADD/MUL/JMP/JZ/HALT (63), big-endian 16/32/64-bit plus LEB128 integer codecs (64), a `.mq.md` table sort rebuilding a new table from `table.row/columns/cell` (65), a table join (67), a quoted/escaped CSV row parser (68), table group (69), and an Int matrix multiply (70); the first-fit allocator also detects double free (`alloc.double_free`). Evidence: [`memory-p2-closure`](evidence/2026-11-20-memory-p2-closure.md), [`structure-api-p2`](evidence/2026-11-20-structure-api-p2.md), [`result-and-free-list-p2`](evidence/2026-11-20-result-and-free-list-p2.md). P2 remaining: publication as a separate external stdlib repository. The bundled `stdlib/` tree, `std:` imports, digest verification, Analysis identity and frozen-CLI closure test are implemented (ADR-0028). C01–C06/C11/C12 each have `.mq.md` evidence, including JSON floats and a JSON formatter. |
| P3 Resource/Stream ABI | **PARTIAL** | ADR-0025/0026 CM-A06 slices: opaque `Resource`, `resource.open/read/write/close`, stdout write in pure/project, granted project-file read and buffered project-file write in project, EOF/closed/handle errors, wire/host rejection; strict refuses all opens. Fixture 66 + CLI/host tests. CM-A07 is **Compat-A only**: the bundled `stdlib/` provides an explicitly imported `.mq.md` `format/utf8/io` chain (`std:` imports) whose output matches builtin `打印` for all 15 value types (Unit/None/Bool/Int/Float/Text/List/Map/Table/Formula/Code/FunctionRef/PluginHandle/Memory/Resource); builtin `打印` remains default and no alias routing exists yet (Compat-B pending). Still missing: Compat-B/C, directories, network, generic provider registry. |
| P3 Resource/Stream ABI | **PARTIAL** | ADR-0025/0026 CM-A06 slices: opaque `Resource`, `resource.open/read/write/close`, stdout write in pure/project, granted project-file read and buffered project-file write in project, EOF/closed/handle errors, wire/host rejection; strict permits only stdio/stdout and refuses file/network resources. Fixture 66 + CLI/host tests. CM-A07 **Compat-B** is implemented: when a program calls `打印/print`, the loader injects the bundled `stdlib/io.mq.md` and the runtime routes to `io.写值`, which formats all 15 value types, UTF-8-encodes via `Memory` and writes via `resource.write(stdout)`; legacy `HostOp::Print` is only a no-bundle fallback. Strict permits only the existing stdio console channel. Still missing: Compat-C, directories, network, generic provider registry. |
| P4 Table logical/physical migration | **NOT IMPLEMENTED** | `Value::Table` remains `Vec<Vec<Value>>`; no stdlib table algorithms |
| P5 third-party Provider / TCP / HTTP | **NOT IMPLEMENTED** | `fs.write`, `net.request`, `process`, `clock`, `random` remain unavailable broker services (see §4) |
| P6 performance / native backends | **NOT IMPLEMENTED** | no bytecode/JIT/GPU/QPU work |
@@ -208,6 +213,9 @@ user-written allocator is explicitly **not** claimed.
identity and frozen-binary closure test.
- [`evidence/2026-11-20-full-value-formatting.md`](evidence/2026-11-20-full-value-formatting.md)
— ADR-0029 observation members and all-15-type stdlib formatting comparison.
- [`evidence/2026-11-20-print-compat-b.md`](evidence/2026-11-20-print-compat-b.md)
— ADR-0030 Compat-B print routing, trace resource effects, lazy stdlib
injection and strict stdio console permission.
- [`evidence/RELEASE_CANDIDATE_0.1.0_8af7262.md`](evidence/RELEASE_CANDIDATE_0.1.0_8af7262.md)
— G0..G12 matrix and RC decision.
- [`security-gate.md`](security-gate.md) — mechanism, attack matrix and strict
+8 -5
View File
@@ -103,9 +103,12 @@ resource.close(handle: Resource)
caller constructs a `Capabilities` set with a matching `fs_read` pattern.
Network/process resources do not exist in this slice.
* `project`: stdio stdout and explicitly granted project-file reads.
* `strict`: **every** `resource.open` is refused with `permission.denied`.
Strict L1 is unchanged and no new provider is silently enabled. A future ADR
must add real OS-level evidence before strict accepts streams.
* `strict`: only the existing **stdio/stdout** console channel is permitted
(`resource.open("stdio","stdout",["write"])`, console grant); every file,
network, process or device resource open is refused with
`permission.denied`. This is the existing console capability reached through
the resource ABI, not a new OS capability (ADR-0030). Strict L1 is otherwise
unchanged.
* Manifest capability requests are not grants; only the operator-provided
effective set is consulted.
@@ -143,8 +146,8 @@ file bytes or handles.
process, clock, random or device operations.
* No generic third-party provider registration; CM-A09 remains open.
* No zero-copy, async, deadlines per stream, seek or shared memory.
* No strict-mode resource support and no claim that host-in-process I/O is a
sandbox.
* No strict-mode file/network/process resource support and no claim that
host-in-process I/O is a sandbox; strict stdio is the existing console.
## Compatibility
@@ -33,7 +33,8 @@ resource.open("file", "<project-relative path>", ["write"]) -> Resource
* `project` mode only, with an `fs_write` glob grant matching the normalised
relative path.
* `pure` still refuses file resources by mode check; `strict` still refuses
every `resource.open` (CM-A11 fail-closed).
file resources (only the existing stdio console channel is permitted,
ADR-0030).
* Absolute paths, `..`, symlink escapes and non-regular targets are rejected by
`ProjectFs` exactly like the existing `fs.write`.
* A writer owns an in-memory byte buffer. `resource.write` appends bytes and
+101
View File
@@ -0,0 +1,101 @@
# ADR-0030: Compat-B `打印` routing through the bundled stdlib
Status: **Accepted**
Date: 2026-11-20
Relates to: `docs/MARQDO_COMPUTE_CORE_STDLIB_MIGRATION_RFC_0002.md`
(CM-A07, CM-A11, P3), ADR-0027, ADR-0028, ADR-0029
Specs: `spec/evaluation.md`, `spec/security.md`
Evidence: `docs/evidence/2026-11-20-print-compat-b.md`
## Context
ADR-0027 implemented Compat-A: the stdlib print chain exists behind explicit
imports, but builtin `打印` still runs `Value::display()` in Rust. ADR-0029
removed the formatting gap: the stdlib can reproduce `打印` for all 15 value
types. ADR-0028 made a verified stdlib bundle available offline. The remaining
Compat-B requirement is to route the **same name** to one implementation and
prove the old callers behave, before Compat-C removes the legacy formatting
path.
## Decision
### 1. One program-level override
`Program` gains an optional `print_override: FunctionId`. The loader sets it to
the bundled `io.写值` function when:
* the verified bundle is available, and
* at least one loaded module calls `打印` or `print` as a bare builtin call.
The stdlib `io` module is injected only in that case. A program without print
calls keeps the same module graph and Analysis output as before. If no bundle
is available, the loader leaves the override unset and the legacy builtin is the
fallback; an official release always ships the bundle.
### 2. Resolution order
At a call site, name resolution is:
1. a local variable holding a callable (`f = ...; f(x)`) — local shadow wins;
2. `打印`/`print` with `Program.print_override` set — call the stdlib function;
3. the existing builtin table;
4. a same-module/imported function declaration.
This is a controlled compatibility route for exactly two names. It is not a
general name-shadowing mechanism and does not add a new namespace.
### 3. Runtime behavior
The stdlib function is a normal Marqdo function: its source spans, fuel,
frames and trace are the stdlib file's. It formats through
`stdlib/format.mq.md`, encodes UTF-8 through `stdlib/text.mq.md`, and writes
through `resource.open("stdio","stdout",["write"])` plus `resource.write`.
`打印` still returns `Unit` and appends exactly one newline.
The console/output-budget checks remain in the host: the stdlib path uses the
same console grant and output budget as the legacy path. Resource operations
are recorded as Trace `effect` events (`resource.open/read/write/close`); the
legacy `HostOp::Print` effect does not appear in Compat-B runs.
### 4. Strict mode and CM-A11
Strict L1 already permits the console channel. Compat-B needs stdout writes in
strict so the same print implementation works there. Therefore strict mode
permits **only** `stdio/stdout` write with the console grant; project-file,
network, process and device resource opens remain refused, and no new provider
is enabled. This is the existing console capability reached through the
resource ABI, not a new OS capability. File resources stay fail-closed.
### 5. Compatibility and fallback
* Output for all 15 value types is the ADR-0029 formatter and is
comparison-tested against the legacy path.
* Trace shape changes from one `print` effect to resource effects. Consumers
that hard-code `operation == "print"` must accept `resource.*`; this is a
documented P3 migration.
* `check`/`analysis` include stdlib modules for print-using programs only.
The human-readable `checked N module(s)` count excludes stdlib modules so
bundle internals do not change the project count.
* Compat-C (removing `HostOp::Print` and `Value::display()` formatting from the
public builtin) is **not** part of this ADR. The legacy path remains the
fallback when no bundle exists.
## Non-goals
* No routing for `长度`, `类型`, `文本`, table or any other builtin.
* No bundled stdlib upgrade workflow; the bundle version stays `0.1.0`.
* No removal of `HostOp::Print`, `Value::display()` or the legacy builtin.
* No new OS isolation claim; strict still refuses file/network/process
resources.
* No guarantee for a CLI installed without `stdlib/`; that configuration keeps
legacy print and is not the official layout.
## Verification
`docs/evidence/2026-11-20-print-compat-b.md` records:
* a print program whose trace contains `resource.write` and no `print` effect;
* all semantic goldens and CLI print tests passing byte-identically;
* lazy stdlib injection: no print → no stdlib modules; print → stdlib modules
and `project.stdlib` identity;
* host tests for strict stdio-only permission and file refusal.
+2 -1
View File
@@ -26,7 +26,8 @@ implementation status lives in `docs/STATUS.md`; frozen behavior is in
| CM-A07 print migration staging / Compat-A | [0027](../adr/0027-print-migration-compat-a.md) | Accepted (Compat-A only) |
| CM-A10 bundled stdlib / `std:` imports / digest | [0028](../adr/0028-bundled-stdlib-and-std-imports.md) | Accepted (first slice) |
| Full-value stdlib formatting / FunctionRef+PluginHandle observation | [0029](../adr/0029-full-value-formatting-and-handle-observation.md) | Accepted |
| CM-A07 Compat-B/C, Table storage, generic providers, strict resources | — | Not decided; do not implement by implication |
| CM-A07 Compat-B print routing | [0030](../adr/0030-print-compat-b-routing.md) | Accepted |
| CM-A07 Compat-C, Table storage, generic providers, strict file/network resources | — | Not decided; do not implement by implication |
## What a future phase must not do
+108
View File
@@ -0,0 +1,108 @@
# RFC-CM CM-A07 Compat-B evidence: `打印` routing through the stdlib
Date: 2026-11-20 (Asia/Shanghai)
Repository: `marqdo-cli`
Baseline: `e119727` plus this step's changes.
Decision record: [ADR-0030](../adr/0030-print-compat-b-routing.md)
Related: [Compat-A](2026-11-20-stdlib-print-compat-a.md),
[full-value formatting](2026-11-20-full-value-formatting.md),
[bundled stdlib](2026-11-20-bundled-stdlib-cm-a10.md).
Scope: Compat-B routing; **Compat-C removal is not done**.
## Environment and gate
Ubuntu 26.04 development container, Linux x86_64, Rust 1.100.0.
```text
cargo fmt --all -- --check PASS
cargo clippy --workspace --all-targets -- -D warnings PASS
cargo test --workspace --locked PASS
416 passed, 0 failed, 2 ignored (existing plugin-sdk doc-tests)
```
No GitHub CI is available from this checkout; this is local evidence.
## Routing
* `Program.print_override: Option<FunctionId>` (IR, serde-optional).
* Loader scans loaded IR for bare `打印`/`print` calls. Only if found, it loads
bundled `std:io` and sets the override to `io.写值`.
* Runtime resolution: local callable > print override > builtin table >
same-module/imported function. Local shadowing is unchanged.
* The stdlib path formats all 15 value types (ADR-0029), UTF-8-encodes into
`Memory`, opens `stdio/stdout`, writes and closes.
* Programs without print calls keep their original module graph.
## Trace behavior
`crates/cli/tests/print_routing.rs::print_is_routed_through_the_stdlib_and_not_hostop_print`:
```text
run index.mq.md --trace-file trace.json
stdout = ["x", 1]
trace operations include resource.write
trace operations do NOT include print
```
Resource open/read/write/close are now recorded as `effect` events by
`StdHost`, so trace consumers see the real byte path. No values or secrets are
recorded.
## Lazy injection
`stdlib_print_modules_are_injected_only_when_print_is_used`:
```text
plain.mq.md (no print) -> no std: modules
prints.mq.md (打印) -> std: modules present
```
Both loads record `project.stdlib` identity because a verified bundle is
available; only the print-using program loads its modules. `check`'s
human-readable module count excludes `std:` modules so bundle internals do not
change the project count.
## Strict mode
Strict L1 already permits the console. Compat-B needs the same channel, so
strict now permits **only** `resource.open("stdio","stdout",["write"])` with the
console grant. File/network/process/device resource opens remain
`permission.denied`. Host test:
```text
strict_mode_allows_only_the_existing_stdio_console_channel
stdio write -> "strict"
file read -> permission.denied
socket read -> permission.denied
```
This is the existing console capability through the resource ABI, not a new OS
capability; CM-A11 fail-closed remains for all other providers.
## Compatibility
* All semantic goldens and CLI print tests pass byte-identically, including
the full-value formatting demo.
* `HostOp::Print` remains implemented as the no-bundle fallback and for
Compat-A comparison tests; Compat-C removal is a separate decision.
* Trace consumers that hard-code `operation == "print"` must accept
`resource.*`; this is documented as a P3 migration.
## What is not done
* Compat-C: `HostOp::Print` and `Value::display()` formatting are still in the
public builtin path for no-bundle installs.
* No stdlib version upgrade workflow and no external repository publication.
* No Windows/macOS evidence and no remote CI URL.
## Reproduce
```bash
export PATH=/root/.cargo/bin:$PATH
export CARGO_HOME=/root/work/marqdo-cli/.cache/cargo
export CARGO_TARGET_DIR=/root/work/marqdo-cli/.cache/target
cargo test -p marqdo-cli --test print_routing
cargo test -p marqdo-cli --test analysis_trace
cargo test -p marqdo-host --test resource_streams
cargo test --workspace --locked
```
@@ -6,8 +6,8 @@ Baseline: `a113d89` plus the uncommitted RFC-CM P0–P3 changes.
Decision record: [ADR-0027](../adr/0027-print-migration-compat-a.md)
Related: [Resource P3](2026-11-20-resource-stream-p3.md),
[file write](2026-11-20-resource-file-write-p3.md), [STATUS §5](../STATUS.md).
Scope: **Compat-A only**. Builtin `打印` is not migrated; no alias routing and
no removal of `HostOp::Print`.
Scope: **Compat-A only** at the time of that slice. A later ADR-0030 step
added Compat-B routing; this report remains the Compat-A baseline.
## Environment and gate
@@ -76,7 +76,8 @@ extension, not part of this slice. This is the explicit blocker for Compat-B.
## What is not migrated
* `打印`/`print` still resolves to the existing builtin and `HostOp::Print`.
* At the time of this slice, `打印`/`print` resolved to the builtin and
`HostOp::Print`; ADR-0030 later added Compat-B routing.
* No alias table, no bundled stdlib resolution, no `std` package.
* No stdlib version/digest or offline distribution (CM-A10).
* No Compat-B/C comparison lane or removal of the Rust `Value::display()` path.
+9 -7
View File
@@ -168,7 +168,8 @@ resource.close(handle)
-> {ok:true, value:None} | {ok:false, code, message}
```
* Providers: `stdio/stdout` with `["write"]` (pure/project, console grant);
* Providers: `stdio/stdout` with `["write"]` (pure/project, and strict's
existing console channel, console grant);
`file` with `["read"]` (project, `fs_read` grant, bounded by the 8 MiB file
limit); `file` with `["write"]` (project, `fs_write` grant, 8 MiB buffered
write flushed on `close`, ADR-0026). Mixed rights and other kinds are
@@ -287,12 +288,13 @@ host boundary. Names are stable; adding one requires a spec update.
| `foreign.run/call` | see `spec/foreign.md` | P5, capability-gated |
| `fs.read/write` | see `spec/security.md` | capability-gated |
The legacy `打印/print` builtin remains the default and still uses the host
output path. `stdlib/` (the bundled standard library, `std:` imports) contains an explicitly imported `.mq.md`
`format/utf8/io` prototype (ADR-0027 Compat-A) whose output is comparison-tested
against `打印` for Unit/None/Bool/Int/Float/Text/List/Map/Table; it does **not**
replace the builtin, and Formula/Code/FunctionRef/PluginHandle formatting is not
yet expressible in the standard library.
`打印/print` is **Compat-B** (ADR-0030): when the verified bundled stdlib is
available and a loaded module calls the builtin, the loader injects
`stdlib/io.mq.md` and sets a program-level `print_override`; the runtime calls
`io.写值` instead of `HostOp::Print`. The stdlib chain formats every one of the
15 value types, encodes UTF-8 into `Memory`, and writes via
`resource.write(stdout)`. The legacy builtin remains the fallback when no bundle
is available. Compat-C (removing the legacy formatting path) is not done.
The `list.*`, `map.*`, `text.*` and `table.*` entries are observation and
construction primitives, not algorithms (ADR-0023). They return new values and
+1 -1
View File
@@ -151,7 +151,7 @@ Parsing rules (`crates/marqdo-markdown/src/frontmatter.rs::imports`):
| Target | Resolution |
|---|---|
| `@dependency` | Look up the alias after `@` in `marqdo.yaml` `dependencies`. The dependency must declare a **path** source. Resolve `project_root / path`; if it is a directory, append `index.mq.md`; canonicalize. A git-only dependency cannot be resolved in this build (`dependency.path` is `None` → unresolved). |
| `std:<relative>` | Resolve inside the verified bundled standard library (ADR-0028). The target must stay inside the bundle; `..`/absolute paths are rejected; `.mq.md` is appended when absent. The bundle's declared digest is verified before any module loads. No network/package manager is consulted. |
| `std:<relative>` | Resolve inside the verified bundled standard library (ADR-0028). Print-using programs additionally get the bundled `io` module as the Compat-B `打印` destination (ADR-0030); this is not an implicit user import. The target must stay inside the bundle; `..`/absolute paths are rejected; `.mq.md` is appended when absent. The bundle's declared digest is verified before any module loads. No network/package manager is consulted. |
| any other string (intended: `./file.mq.md`) | Join the string to the **importing file's directory**, then canonicalize. There is no extension inference, no search path and no fallback to a package directory. |
The resulting file is loaded as a module. If it canonicalizes outside the
+1 -1
View File
@@ -28,7 +28,7 @@ Normative implementation:
|---|---|---|
| `pure` | No external I/O; memory computation and budgets only. | **Enforced.** `Capabilities::pure()` = `console`, `math` only; every FS/foreign/plugin grant flag makes the CLI exit 2 with "pure mode prohibits external I/O grants". |
| `project` | Operator-approved project resources under `CapabilityHost` + bounded resources. | **Enforced for the implemented operations.** Defaults equal `pure`; `--allow-read`, `--allow-write`, `--allow-foreign`, `--allow-plugin` add grants. `resource.open` supports stdout write, granted project-file read and granted buffered project-file write (ADR-0025/0026); network/process remain unsupported. Sandboxing is cooperative (in-process checks), not OS isolation. |
| `strict` | Untrusted documents under real OS isolation. | **Linux: enforced when the runtime probe passes (ADR-0021).** `resource.open` is **refused** in strict until a provider passes the isolation lane (CM-A11). `PlatformCapabilities::detect()` runs a real Bubblewrap smoke test; success reports `Available { mechanism }`, failure reports `Unavailable { reason }`. Strict L1 supports the interpreter, console and native Math only; `--allow-read/--allow-write/--allow-foreign/--allow-plugin/--plugin/--trust-native-plugin/--kernel/--trace-file` are rejected with exit 2. macOS/Windows and Linux hosts without the mechanisms remain `Unavailable`; there is **no fallback** to an unrestricted process. |
| `strict` | Untrusted documents under real OS isolation. | **Linux: enforced when the runtime probe passes (ADR-0021).** `resource.open` is **refused** in strict except for the existing `stdio/stdout` console channel (console grant); file/network/process resources remain refused until a provider passes the isolation lane (CM-A11, ADR-0030). `PlatformCapabilities::detect()` runs a real Bubblewrap smoke test; success reports `Available { mechanism }`, failure reports `Unavailable { reason }`. Strict L1 supports the interpreter, console and native Math only; `--allow-read/--allow-write/--allow-foreign/--allow-plugin/--plugin/--trust-native-plugin/--kernel/--trace-file` are rejected with exit 2. macOS/Windows and Linux hosts without the mechanisms remain `Unavailable`; there is **no fallback** to an unrestricted process. |
`marqdo inspect <file> --platform` prints the detection report, for example:
+9 -1
View File
@@ -171,6 +171,13 @@ by:
* fixture 55 additionally has a second-free detection path: freeing an
already-free block returns `{ok:false, code:"alloc.double_free"}`.
RFC-CM CM-A07 Compat-B (ADR-0030) is pinned by
`crates/cli/tests/print_routing.rs`:
* a print program's trace contains `resource.write` and no `print` effect;
* stdlib modules are injected only for programs that call `打印/print`;
* all existing semantic/CLI print goldens still pass byte-identically.
RFC-CM CM-A10 bundled stdlib (ADR-0028) is pinned by
`crates/cli/tests/stdlib_bundle.rs` (6 tests):
@@ -192,7 +199,8 @@ RFC-CM CM-A07 Compat-A (ADR-0027) is pinned by:
* the demo covers Unit/None/Bool, Int, Float, Text, List, Map, Table,
Formula, Code and FunctionRef; PluginHandle formatting uses the same
observation members but needs a plugin to construct a value.
`打印` is not yet routed to the stdlib (Compat-B pending).
`打印` is routed to the stdlib by ADR-0030 Compat-B when the bundle is
present; this demo still calls the explicit `io.写值` to compare paths.
RFC-CM CM-A06 resource streams (ADR-0025) are pinned by:
+2 -1
View File
@@ -22,7 +22,8 @@ if not isinstance(project.get("root_id"), str) or not project["root_id"]:
functions = []
for module in analysis["modules"]:
if not module["id"].startswith("root:"):
module_id = module["id"]
if "::" not in module_id or not module_id.startswith(("root:", "dep:", "std:")):
raise SystemExit("unexpected module identity")
if not isinstance(module["source_digest"], str) or len(module["source_digest"]) != 64:
raise SystemExit("bad source digest")