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

+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.