docs: formal specs, ADRs, STATUS matrix, contributor guide and honest README
Complete the documentation set for the M0.5 toolchain, grounded in the code: - spec/references.md: import/reference/call edges, identities, loader rules, anchors, `ref` resolution/ambiguity, `check --refs` validation. - spec/value-wire.md: exact wire_version 1 typed encoding, integer-as-decimal string rule, special floats, limits and exact float round-trip. - spec/security.md: modes (strict refuses without OS isolation), capabilities, path rules, budgets, tracing, foreign process caveats, plugin trust. - spec/package-format.md: marqdo.yaml/lock formats, validation, lock/sync, unsupported registry/git/environment work. - spec/plugin-abi.md: ABI v1 layout/ownership/negotiation, manifest/artifact rules, wire envelopes, loader lifecycle, trust boundary, SDK/C demo, and the implemented CLI plugin-invocation flow with its limitations. - spec/math.md and spec/foreign.md: the implemented math subset and the foreign request/response/timeout/capability contract. - docs/adr/0001..0007: accepted decisions for the span foundation, inert executables outside function bodies, ref ambiguity (S-07), strict refusal, AST normalization, CLI contract and BigInt/typed wire (S-08). - docs/contributor-guide.md: repo map, workflow, fixture/golden updates, quality gate, ADR/commit policy. - docs/STATUS.md: P0-P7 matrix with evidence, open items and the blueprint §18 Alpha checklist (conclusion: not yet Alpha-complete), including known divergences rather than overclaims. - README.md rewritten from "design-only starter" to the working implementation with quickstart, implemented phases, deliberate limits and doc index. - AGENTS.md: updated pointers/workflow only; architectural rules unchanged. - Consistency fixes in spec/mapping.md/evaluation.md (inert emph outside functions, unsupported math.simplify/differentiate).
This commit is contained in:
1 parent
fae0cd4b18
commit
0116160399
19 files changed
+3767
-30
No files matched your search
+2
-2
@@ -124,7 +124,7 @@ host boundary. Names are stable; adding one requires a spec update.
|
||||
| `整数` / `int` | `(x) -> Int` | from Int/Float(if integral)/Text(decimal) |
|
||||
| `小数` / `float` | `(x) -> Float` | from Int/Float/Text |
|
||||
| `表` / `table` | `(header: List, rows: List) -> Table` | validates row widths |
|
||||
| `ref` | `(target: Text) -> FunctionRef/Formula/Code` | resolves a document address |
|
||||
| `math.parse/eval/simplify/differentiate` | see `spec/math.md` | P5 |
|
||||
| `ref` | `(target: Text) -> FunctionRef/Formula/Code` | resolves a document address; module-local, refuses ambiguous resource units (`ref.ambiguous`) |
|
||||
| `math.parse/eval/substitute` | see `spec/math.md` | P5; `math.simplify`/`math.differentiate` are declared but return `math.unsupported` in this build |
|
||||
| `foreign.run/call` | see `spec/foreign.md` | P5, capability-gated |
|
||||
| `fs.read/write` | see `spec/security.md` | capability-gated |
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
# Marqdo Foreign Kernels v1
|
||||
|
||||
Status: **one-shot kernels for Python and Node/JavaScript**, frozen protocol for
|
||||
M0.5. There are no sessions or persistent kernels.
|
||||
|
||||
Implementation: `crates/marqdo-foreign/src/lib.rs` (process supervision and
|
||||
protocol), `crates/marqdo-host/src/kernels.rs::foreign` (capability check and
|
||||
Marqdo mapping), runtime tests `crates/marqdo-foreign/tests/kernels.rs`,
|
||||
example `examples/python-foreign.mq.md`.
|
||||
|
||||
## 1. A code fence is a value, not a process
|
||||
|
||||
A `code_block` node becomes an inert `Code { language, source, content_hash,
|
||||
unit }` resource (`crates/marqdo-frontend/src/resources.rs`,
|
||||
`marqdo_value::CodeValue`). Reading, checking, rendering or importing a
|
||||
document never starts a foreign process. A process is created only when
|
||||
`foreign.run` or `foreign.call` is executed after the capability check.
|
||||
|
||||
Access is by `ref("#heading")` (exactly one code/formula resource in that
|
||||
heading range; `ref.ambiguous` otherwise — see `spec/references.md` §5):
|
||||
|
||||
````markdown
|
||||
## Python 校验
|
||||
|
||||
```python
|
||||
import json, math, sys
|
||||
envelope = json.load(sys.stdin)
|
||||
r = int(envelope["input"]["entries"]["r"]["decimal"])
|
||||
print(json.dumps({"type": "float", "value": math.pi * r ** 2}))
|
||||
```
|
||||
|
||||
## main
|
||||
|
||||
**结果 = foreign.call(ref("#python-校验"), {"r": 3})**
|
||||
|
||||
> 打印(结果)
|
||||
````
|
||||
|
||||
The anchor is the GitHub-style slug of the heading (`#python-校验`), not the
|
||||
heading text.
|
||||
|
||||
## 2. Supported languages
|
||||
|
||||
`normalize_language` is ASCII case-insensitive and trims surrounding
|
||||
whitespace:
|
||||
|
||||
| Canonical | Accepted aliases | Interpreter lookup |
|
||||
|---|---|---|
|
||||
| `python` | `python`, `python3`, `py` | `python3`, then `python` on `PATH` |
|
||||
| `javascript` | `javascript`, `js`, `node`, `nodejs` | `node` on `PATH` |
|
||||
|
||||
Any other language yields `ForeignError::KernelUnavailable` (the `Code` value is
|
||||
still valid data). `detect(language)` returns `KernelInfo { language, program,
|
||||
version }` by scanning `PATH` and running the interpreter with `--version`
|
||||
(5 s timeout, 64 KiB output cap, cleared environment except `PATH`); the
|
||||
`version` is the first non-empty line of stdout or stderr. `detect` and
|
||||
execution use the same `PATH`-only environment.
|
||||
|
||||
## 3. `RunRequest` / `RunResult`
|
||||
|
||||
```rust
|
||||
pub struct RunRequest {
|
||||
pub language: String,
|
||||
pub source: String,
|
||||
pub input: Option<serde_json::Value>, // an already-typed wire value
|
||||
pub timeout: std::time::Duration,
|
||||
pub max_output_bytes: usize,
|
||||
}
|
||||
|
||||
pub struct RunResult {
|
||||
pub status: RunStatus, // Success | Failed | TimedOut
|
||||
pub stdout: String, // lossy UTF-8 for `run`
|
||||
pub stderr: String, // preserved on failure
|
||||
pub exit_code: Option<i32>, // None on signal/timeout
|
||||
pub duration: std::time::Duration,
|
||||
pub value: Option<Value>, // only `call` on success
|
||||
}
|
||||
```
|
||||
|
||||
`RunRequest::new` defaults to a 30 s timeout and 1 MiB output cap **for direct
|
||||
library use**. The CLI host constructs requests with `StdHost` defaults:
|
||||
`foreign_timeout = 10 s`, `foreign_output_limit = 1 MiB` (stdout+stderr
|
||||
combined). The CLI has no per-kernel flag for these; `--timeout` only sets the
|
||||
interpreter wall-clock deadline.
|
||||
|
||||
`RunStatus::as_str()` yields `success`, `failed`, `timed_out`.
|
||||
|
||||
## 4. stdin envelope
|
||||
|
||||
The program is executed as a file; stdin receives exactly one JSON object
|
||||
(`crates/marqdo-foreign/src/lib.rs::request_envelope`):
|
||||
|
||||
```json
|
||||
{
|
||||
"wire_version": 1,
|
||||
"kind": "request",
|
||||
"input": <type-tagged wire value, or null>
|
||||
}
|
||||
```
|
||||
|
||||
`input` is **already encoded with the type-tagged value format**
|
||||
(`spec/value-wire.md`), not a plain JSON mirror of the Marqdo value. For
|
||||
example `{"r": 3}` becomes
|
||||
`{"type":"map","entries":{"r":{"type":"int","decimal":"3"}}}`. The example
|
||||
kernel reads `envelope["input"]["entries"]["r"]["decimal"]` accordingly. When
|
||||
`input` is absent, the field is JSON `null` and the write side closes stdin
|
||||
right after writing (EOF).
|
||||
|
||||
Marqdo values that cannot cross the wire (none in v1, but see
|
||||
`spec/value-wire.md` limits) are encoded before the process starts; the
|
||||
envelope may be size-limited by the host's output/wire budgets.
|
||||
|
||||
## 5. `run` versus `call`
|
||||
|
||||
| | `foreign.run(ref, input?)` | `foreign.call(ref, input)` |
|
||||
|---|---|---|
|
||||
| One-shot isolated execution | yes | yes |
|
||||
| stdin envelope | yes (input optional) | yes (input required) |
|
||||
| stdout contract | captured as data; any content | must be **exactly one** `wire_version` 1 typed value; no logs/trailing noise |
|
||||
| result | `Map` with `status`, `stdout`, `stderr`, `exit_code`, `duration_ms`, `value` (always `None` for `run`) | decoded `Value` |
|
||||
| non-zero exit | `status: "failed"` in the returned map | `Ok(RunResult { status: Failed })` from the library, but the host turns it into `foreign.failure` runtime error |
|
||||
| timeout | library returns `Ok` with `status: "timed_out"`; host returns the map | library returns `ForeignError::Timeout`; host maps to `resource.exhausted` |
|
||||
| stdout pollution / invalid wire | irrelevant | `ForeignError::Protocol` → `foreign.failure` |
|
||||
|
||||
`call` parses stdout with `serde_json` and then `marqdo_value::from_wire` with
|
||||
default limits; an unknown `type` tag, bad decimal, JSON syntax error, invalid
|
||||
UTF-8 or trailing bytes are all protocol errors. A successful call whose stdout
|
||||
has no typed value is a protocol error at the host boundary ("foreign call
|
||||
returned no typed value" / `foreign.failure`).
|
||||
|
||||
The host result map from `foreign.run` (`crates/marqdo-host/src/kernels.rs::
|
||||
foreign_result_map`) is:
|
||||
|
||||
```text
|
||||
{status: "success"|"failed"|"timed_out",
|
||||
stdout: Text, stderr: Text, exit_code: Int|None,
|
||||
duration_ms: Int, value: None}
|
||||
```
|
||||
|
||||
## 6. Process model and limits
|
||||
|
||||
`crates/marqdo-foreign/src/lib.rs`:
|
||||
|
||||
1. normalize the language (unknown → `KernelUnavailable` with install hint);
|
||||
2. find the interpreter on `PATH`;
|
||||
3. create a **private unique temp working directory** (mode 0700 on Unix) and
|
||||
write the program to a private temp file (`.py`/`.js`, mode 0600);
|
||||
4. spawn the interpreter **directly with `argv`** (no shell, no string
|
||||
interpolation), cwd = the fresh temp dir, environment cleared except
|
||||
`PATH`, stdin/stdout/stderr piped;
|
||||
5. a dedicated thread writes the stdin envelope and closes the pipe; reader
|
||||
threads capture stdout and stderr against a **shared** `max_output_bytes`
|
||||
budget; the supervisor polls `try_wait` and kills the child on the wall-clock
|
||||
deadline or output overflow;
|
||||
6. after the child is reaped, readers get a 2 s grace to finish; cleanup (Drop)
|
||||
removes the temp file and directory on success and error paths.
|
||||
|
||||
Bounds: wall-clock `timeout` (10 s in the CLI), combined output
|
||||
`max_output_bytes` (1 MiB in the CLI; stderr counts too), reader grace 2 s,
|
||||
detection timeout 5 s / 64 KiB. A kernel that ignores stdin and exits does not
|
||||
hang the host. `OutputTooLarge` drops the captured output and kills the child.
|
||||
Grandchild processes are not killed when the direct child is killed.
|
||||
|
||||
Environment: only `PATH` is inherited, so Marqdo secrets in other environment
|
||||
variables do not leak by default. This is hygiene, not a security boundary.
|
||||
|
||||
## 7. Capability gating
|
||||
|
||||
`foreign.run`/`foreign.call` are effectful builtins
|
||||
(`crates/marqdo-ir/src/builtins.rs`). The host adapter
|
||||
(`crates/marqdo-host/src/kernels.rs::foreign`) first checks
|
||||
`Capabilities::check_foreign`, which requires both `foreign` and `process`.
|
||||
|
||||
* default (`Capabilities::pure()`/`project()`): denied →
|
||||
`permission.denied` "foreign kernel execution was not granted";
|
||||
* `--allow-foreign` in `project` mode grants both flags;
|
||||
* `pure` mode rejects `--allow-foreign` at CLI parse time (exit 2);
|
||||
* `strict` mode refuses to run before any kernel is considered;
|
||||
* a manifest `capabilities.process`/foreign request never grants anything;
|
||||
* the check is repeated for every call (not cached at parse time).
|
||||
|
||||
If the first argument is not a `Code` value, the host returns `runtime.type`
|
||||
("foreign kernel expects a Code value from `ref("#代码块")`"). A code fence with
|
||||
an empty language is `kernel.unavailable` ("no kernel can be selected").
|
||||
Unsupported languages are `kernel.unavailable` with an install hint.
|
||||
|
||||
## 8. Error mapping
|
||||
|
||||
| Library error | Host runtime code |
|
||||
|---|---|
|
||||
| `KernelUnavailable { language, hint }` | `kernel.unavailable` (message includes hint) |
|
||||
| `Spawn { message }` | `host.error` |
|
||||
| `Io { message }` | `host.error` |
|
||||
| `Timeout { seconds }` | `resource.exhausted` |
|
||||
| `Protocol { message }` | `foreign.failure` |
|
||||
| `OutputTooLarge { limit }` | `resource.exhausted` |
|
||||
|
||||
`foreign.call` with a non-zero exit becomes `foreign.failure` with the stderr
|
||||
text preserved; a successful process whose stdout is not a typed value is also
|
||||
`foreign.failure`.
|
||||
|
||||
## 9. Security caveats
|
||||
|
||||
* **Process isolation is not a sandbox.** The child runs as the host user and
|
||||
can read/write any file the user can, open sockets, spawn processes and
|
||||
persist beyond the one-shot execution. Fresh cwd, cleared environment and a
|
||||
killed direct child are limited hygiene measures. Only `strict` mode
|
||||
(currently refused) would promise OS-level confinement.
|
||||
* There is no network policy for foreign kernels; `net` capability applies to
|
||||
host operations, not to what a child process does.
|
||||
* Output is captured, not inherited; the child cannot write to the CLI's
|
||||
stdout/stderr directly, but it can still affect the filesystem.
|
||||
* Kernel availability is environment-dependent (`python3`/`python`/`node` on
|
||||
`PATH`). Missing kernels must be fixable by the user (the error includes an
|
||||
install hint).
|
||||
* `foreign.run` returns raw stdout as data; consumers must not treat it as a
|
||||
typed value unless they parse it themselves. `foreign.call` is the typed
|
||||
path.
|
||||
* Sessions, shared state, persistent kernels and automatic environment
|
||||
management (uv/venv) are not implemented. Python/Node are optional foreign
|
||||
kernels, never the official execution backend.
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
# Marqdo Math v1
|
||||
|
||||
Status: **frozen subset for M0.5.** The kernel computes numbers; it never proves
|
||||
theorems. `math.simplify` and `math.differentiate` are **not implemented** in
|
||||
this build.
|
||||
|
||||
Implementation: `crates/marqdo-math` (`parse`, `evaluate`, `substitute`,
|
||||
`MathError`), host adapter `crates/marqdo-host/src/kernels.rs::math`, builtin
|
||||
metadata `crates/marqdo-ir/src/builtins.rs`, tests
|
||||
`crates/marqdo-math/tests/kernel.rs`.
|
||||
|
||||
## 1. From Markdown to a value
|
||||
|
||||
A `math` / `math_block` node is an **inert document resource**, never an
|
||||
operation. The frontend attributes every formula to its innermost enclosing
|
||||
heading unit (`crates/marqdo-frontend/src/resources.rs`). `ref("#heading")`
|
||||
returns a `Formula` value only when that unit contains exactly one resource;
|
||||
with two or more formulas the runtime raises `ref.ambiguous` and refuses to
|
||||
pick one (`spec/references.md` §5, ADR-0003).
|
||||
|
||||
```markdown
|
||||
## 圆面积公式
|
||||
|
||||
$$
|
||||
A = \pi r^2
|
||||
$$
|
||||
|
||||
## 计算圆面积
|
||||
|
||||
**面积 = math.eval(ref("#圆面积公式"), {"r": 4}, target="A")**
|
||||
|
||||
*面积*
|
||||
```
|
||||
|
||||
Nothing is evaluated by reading or rendering the document. `math.*` must be
|
||||
called explicitly and the host checks the `math` capability at execution time
|
||||
(default: granted in `pure`/`project`; there is no CLI flag to revoke it today).
|
||||
|
||||
## 2. Grammar
|
||||
|
||||
The parser accepts exactly this LaTeX subset (whitespace insignificant; byte
|
||||
offsets are reported for parse errors):
|
||||
|
||||
```text
|
||||
formula = expr [ "=" expr ] ; single '=' only, never assignment
|
||||
expr = term { ("+" | "-") term } ;
|
||||
term = unary { ("*" | "/" | "·" | "×" | implicit) unary } ;
|
||||
unary = ("+" | "-") unary | power ;
|
||||
power = primary [ "^" unary ] ; right associative
|
||||
primary = number
|
||||
| identifier Unicode letter (plus digits/underscore)
|
||||
| "\pi" | "pi" | "\e" | "e" constants
|
||||
| "(" expr ")" | "\left(" expr "\right)"
|
||||
| "{" expr "}" grouping
|
||||
| "\frac{" expr "}{" expr "}" exact division
|
||||
| "\sqrt{" expr "}" real square root
|
||||
;
|
||||
number = digits [ "." digits ] | "." digits ; decimal, no exponent form
|
||||
```
|
||||
|
||||
Rules and edge cases:
|
||||
|
||||
* `\cdot` and `\times` are accepted as synonyms for `*`.
|
||||
* Implicit multiplication between adjacent factors: `2x`, `\pi r^2`, `(a)(b)`,
|
||||
`m a`. Concatenated letters form one identifier (`ma` is the variable `ma`;
|
||||
write `m a` or `m * a` for a product). Two adjacent number literals are a
|
||||
parse error ("ambiguous").
|
||||
* Unicode identifiers are supported (`面积`, `r`, `速度`). Plain multi-letter
|
||||
identifiers are variables, but names of common elementary functions (`sin`,
|
||||
`cos`, `log`, `exp`, ...) are reserved and rejected as unsupported so
|
||||
`sin(x)` is not silently read as a product.
|
||||
* Underscores are ordinary identifier characters: `x_1` is the variable `x_1`,
|
||||
not a subscript.
|
||||
* A single top-level `=` creates an `Equation { left, right }`; it is **not**
|
||||
assignment and never writes a Marqdo binding. A second `=` is a parse error.
|
||||
* `\frac{a}{b}` is exact division (`Div`); `1.5 + 2.5` is an exact rational
|
||||
computation, not binary floating point.
|
||||
* `\sqrt{x}` is real-valued: a negative radicand is a `Type` error.
|
||||
* Decimal literals have no exponent form (`1e3` is rejected); `2e` is
|
||||
`2 * e` (number times the constant), while `2e3` is rejected rather than
|
||||
reinterpreted.
|
||||
|
||||
### 2.1 Unsupported constructs
|
||||
|
||||
Everything outside the grammar is a structured `Unsupported` or `Parse` error:
|
||||
matrices, sums/integrals, function applications (`\sin`, `\cos`, ...),
|
||||
multi-letter commands, `\left[`/`\right]`, mismatched/missing braces,
|
||||
scientific notation, commas, arrays, subscripts, superscript commands other
|
||||
than the `^` operator, `\frac`/`\sqrt` without braced arguments, labels,
|
||||
alignment characters, line breaks. There is no `simplify`, `differentiate`,
|
||||
`integrate`, `matrix` or theorem/proof operation in this build.
|
||||
|
||||
## 3. Numeric semantics
|
||||
|
||||
* Exact rational arithmetic uses `num-rational::Ratio` over the
|
||||
`num-bigint::BigInt` type that also backs `Value::Int`; integers are
|
||||
arbitrary precision.
|
||||
* Decimal literals are exact rationals (`0.25` = `1/4`). `Int` bindings become
|
||||
exact integers. `Float` bindings are interpreted as the **exact binary
|
||||
rational value** of the `f64`; non-finite `Float` bindings are a `Type`
|
||||
error.
|
||||
* `+ - * / ^` are exact when both operands are exact. Division keeps a
|
||||
rational, so `\frac{1}{3} * 3` is exactly `1`.
|
||||
* The final value is a Marqdo `Int` exactly when the exact result is an
|
||||
integer, and a `Float` otherwise (`crates/marqdo-math/src/eval.rs`, the
|
||||
`Numeric` enum). Examples: `1.5 + 2.5` → `Int(4)`; `1 / 2` → `Float(0.5)`;
|
||||
`100 / 4` → `Int(25)`.
|
||||
* `\pi`, `\e` and a `\sqrt` of a non-perfect square introduce an `f64`
|
||||
approximation. Anything derived from them is a `Float`. Precision follows
|
||||
IEEE-754 binary64 (about 15–17 significant decimal digits); approximate
|
||||
results are never presented as exact.
|
||||
* `\sqrt` of a perfect square of an exact rational stays exact (`\sqrt{4}` →
|
||||
`Int(2)`); `\sqrt{1/4}` is `Float(0.5)` in this build (it becomes
|
||||
approximate via the root path, matching the documented test).
|
||||
* The exponent of `^` must evaluate to an integer. Magnitude is bounded by
|
||||
`MAX_EXPONENT`. Negative integer exponents are exact reciprocals (the result
|
||||
is therefore a `Float` unless it is integral). `0 ^ 0` is a `Type` error.
|
||||
* Exact intermediate/result numerators and denominators are bounded by
|
||||
`MAX_RESULT_BITS`; exceeding the bound returns a structured `Unsupported`
|
||||
error rather than hanging or exhausting memory.
|
||||
* Division by zero is a `Type` error inside the kernel. The host maps all
|
||||
`MathErrorKind::Type` to `runtime.type`; it does **not** produce
|
||||
the interpreter's `runtime.division_by_zero` code for a formula. This is a
|
||||
known divergence from the module doc comment in
|
||||
`crates/marqdo-math/src/lib.rs` and should be fixed or documented in an ADR.
|
||||
* Constants are reserved: a binding named `pi` or `e` never overrides the
|
||||
constant, and they cannot be solve targets (use `E`, `x`, `r`, ...).
|
||||
|
||||
### 3.1 Bounds
|
||||
|
||||
| Constant | Value | Location |
|
||||
|---|---|---|
|
||||
| `MAX_SOURCE_BYTES` | 1,000,000 | `crates/marqdo-math/src/parse.rs` |
|
||||
| `MAX_PARSE_DEPTH` | 256 | parse.rs |
|
||||
| `MAX_AST_NODES` | 512 | parse.rs |
|
||||
| `MAX_EXPONENT` | 10,000 | `crates/marqdo-math/src/eval.rs` |
|
||||
| `MAX_RESULT_BITS` | 1,000,000 | eval.rs |
|
||||
|
||||
The AST-node bound also bounds later recursion for long operator chains, so
|
||||
hostile input cannot overflow the stack during evaluation, rendering or
|
||||
substitution. No code path panics on untrusted LaTeX; all failures are
|
||||
`MathError`.
|
||||
|
||||
## 4. Equations and solving a target
|
||||
|
||||
`FormulaKind` is `Expression(expr)` or `Equation { left, right }`.
|
||||
|
||||
`evaluate(formula, bindings, target)`:
|
||||
|
||||
* for a plain expression, `target` must be `None`; supplying one is
|
||||
`Unsupported` ("a target variable is only meaningful for an equation");
|
||||
* for an equation, `target` is required (`Unsupported` otherwise); an empty
|
||||
target is `Unsupported`;
|
||||
* the target must occur **exactly once** and the equation must be **affine in
|
||||
the target**. Other variables are substituted from `bindings` first; a
|
||||
missing binding is `UndefinedVariable`.
|
||||
|
||||
Affine solving supported by `crates/marqdo-math/src/eval.rs`:
|
||||
|
||||
* `+`, `-`, multiplication by a target-free side, division where the target is
|
||||
in the numerator, and inversion of those operations in either direction:
|
||||
`2x = 4`, `4 = 2x`, `x / 2 = 3`, `3 = x / 2`, `x + 2 = 5`, `2x + 1 = 7`,
|
||||
`(x + 1) * 2 = 8`, `x = 2y + 1` (with `y` bound);
|
||||
* `F = m a` solved for `F`, `m` or `a`.
|
||||
|
||||
Rejected as `Unsupported`: target occurs zero times or more than once, target
|
||||
in a denominator, under a root, in an exponent, or in a non-affine product
|
||||
(e.g. `x * x`). Equations never mutate bindings and never create Marqdo
|
||||
variables.
|
||||
|
||||
## 5. Substitution
|
||||
|
||||
`substitute(formula, bindings)` replaces bound variables with exact numeric
|
||||
literals and keeps unbound variables symbolic. The returned `Formula.source` is
|
||||
a deterministic canonical rendering that `parse` accepts again; it is not
|
||||
byte-identical to the input formatting. Host-facing `math.substitute` wraps the
|
||||
result in a `Formula` value with `unit: None`, `display: false`, span of the
|
||||
call site.
|
||||
|
||||
## 6. Host-facing surface (`math.*`)
|
||||
|
||||
All operations are builtins in `crates/marqdo-ir/src/builtins.rs`, dispatched
|
||||
in `crates/marqdo-runtime/src/builtins_impl.rs`, and executed in the host
|
||||
adapter `crates/marqdo-host/src/kernels.rs`. `math` capability is checked
|
||||
before touching the kernel.
|
||||
|
||||
| Builtin | Parameters | Result |
|
||||
|---|---|---|
|
||||
| `math.parse` | `formula: Formula or Text` | `Formula` (source = input; `unit=None`, `display=false`); parse errors are `math.unsupported` |
|
||||
| `math.eval` | `formula: Formula or Text`, `bindings: Map`, optional `target: Text` | `Int` or `Float`; equation requires target |
|
||||
| `math.substitute` | `formula: Formula or Text`, `bindings: Map` | `Formula` with canonical source |
|
||||
| `math.simplify` | `formula` | **not implemented**: runtime error `math.unsupported` ("not part of the supported math subset in this build") |
|
||||
| `math.differentiate` | `formula`, `变量` | **not implemented**: `math.unsupported` |
|
||||
|
||||
Input typing:
|
||||
|
||||
* formula arguments accept a `Formula` value or `Text`; anything else is
|
||||
`runtime.type`;
|
||||
* `bindings` must be a `Map` whose values are `Int` or `Float`; any other
|
||||
value type is `runtime.type` with a message naming the binding;
|
||||
* `target` must be `Text` or `None`; other types are `runtime.type`;
|
||||
* builtin arity/parameter-name mistakes are catchable statically
|
||||
(`call.arity`, see `spec/mapping.md` §6);
|
||||
* the math kernel is a pure computation: no file, network, process or plugin
|
||||
effects, and nothing it returns is a capability.
|
||||
|
||||
### 6.1 Error mapping
|
||||
|
||||
Host mapping (`crates/marqdo-host/src/kernels.rs::math_error`):
|
||||
|
||||
| `MathErrorKind` | Runtime code |
|
||||
|---|---|
|
||||
| `Parse` | `math.unsupported` (message includes the byte offset when known) |
|
||||
| `Unsupported` | `math.unsupported` |
|
||||
| `UndefinedVariable` | `symbol.unknown` |
|
||||
| `Type` | `runtime.type` |
|
||||
|
||||
## 7. What this is not
|
||||
|
||||
* Not a computer-algebra system: no simplification, differentiation,
|
||||
integration, matrices, series, assumptions, units or symbolic rewriting.
|
||||
* Not a theorem prover: `F = m a` is an equation value, not a proof, and a
|
||||
numeric solve is not a certificate.
|
||||
* Not a LaTeX renderer: rendering is the Markdown reader's job; the kernel only
|
||||
receives the raw `latex` text of a `math` node.
|
||||
* Not error-free in the floating-point sense: approximation precision is
|
||||
documented, not guaranteed beyond binary64.
|
||||
@@ -0,0 +1,258 @@
|
||||
# Marqdo Project Package Format v1
|
||||
|
||||
Status: **v1 format as implemented in M0.5.** Git resolution, registries,
|
||||
transitive dependency solving and environment materialization are **not**
|
||||
implemented; see §8.
|
||||
|
||||
Implementation: `crates/marqdo-package/src/lib.rs` (manifest/lock types and
|
||||
validation), `crates/cli/src/commands.rs` (`init`, `add`, `remove`, `lock`,
|
||||
`sync`).
|
||||
|
||||
## 1. Project root
|
||||
|
||||
The project root is the nearest ancestor directory of the entry document that
|
||||
contains `marqdo.yaml` (`marqdo_package::discover_root`). If no manifest exists
|
||||
anywhere above the entry, the entry file's parent directory is used as the
|
||||
root (a document can be checked/run without a project).
|
||||
|
||||
`ModuleId`s are project-root-relative paths (`spec/references.md` §2.1), so the
|
||||
root choice affects import identity. `add`/`remove`/`lock`/`sync` discover the
|
||||
root from the **current working directory**, not from a document path. There is
|
||||
no `--project` flag in this build.
|
||||
|
||||
## 2. `marqdo.yaml`
|
||||
|
||||
```yaml
|
||||
project:
|
||||
name: example
|
||||
version: 0.1.0
|
||||
marqdo: ">=0.1,<1" # optional; informational in this build
|
||||
dependencies:
|
||||
geometry:
|
||||
path: ./packages/geometry # exactly one of path / git+rev
|
||||
quantum:
|
||||
git: https://github.com/example/quantum-mq
|
||||
rev: 0123456789abcdef0123456789abcdef01234567
|
||||
plugins:
|
||||
solver:
|
||||
version: "^1.0" # requirement only; not installed or authorized
|
||||
capabilities: # requests only; not grants
|
||||
fs:
|
||||
read: ["./data/**"]
|
||||
write: ["./output/**"]
|
||||
net:
|
||||
hosts: []
|
||||
process: false
|
||||
```
|
||||
|
||||
### 2.1 Fields
|
||||
|
||||
| Field | Type | Required | Semantics |
|
||||
|---|---|---|---|
|
||||
| `project.name` | string | yes | non-empty package name |
|
||||
| `project.version` | string | yes | non-empty version string (not semver-validated in the manifest) |
|
||||
| `project.marqdo` | string | no | required Marqdo runtime range; stored, not enforced by this build |
|
||||
| `dependencies.<name>.path` | path string | one of path/git | project-relative path to a dependency directory or `.mq.md` file |
|
||||
| `dependencies.<name>.git` | URL string | one of path/git | git source (must be pinned with `rev`) |
|
||||
| `dependencies.<name>.rev` | string | with `git` | exact revision; a bare `git` without `rev` is rejected |
|
||||
| `plugins.<name>.version` | string | per entry | non-empty plugin version requirement; declarative only |
|
||||
| `capabilities.fs.read` / `.write` | list of strings | no | requested project-relative globs |
|
||||
| `capabilities.net.hosts` | list of strings | no | requested hosts |
|
||||
| `capabilities.process` | bool | no | requested process execution |
|
||||
|
||||
Unknown fields are ignored (the types do not `deny_unknown_fields`). Duplicate
|
||||
YAML keys follow `serde_yaml` behavior (last one wins); there is no duplicate-key
|
||||
diagnostic.
|
||||
|
||||
### 2.2 Validation (`Manifest::validate`)
|
||||
|
||||
`marqdo.yaml` is loaded with `Manifest::from_yaml`/`Manifest::load`, which
|
||||
validates:
|
||||
|
||||
* `project.name` and `project.version` are non-empty after trimming;
|
||||
* for every dependency, exactly one source is present:
|
||||
* both `path` and `git` → error "declares both path and git";
|
||||
* neither → error "must declare a path or a pinned git source";
|
||||
* `git` without `rev` → error requiring an exact revision;
|
||||
* an absolute `path` → error "path must be project-relative";
|
||||
* for every plugin requirement, `version` is non-empty.
|
||||
|
||||
Note: a *git* dependency passes manifest validation when pinned, but the
|
||||
dependency is not resolvable or lockable in this build (§5).
|
||||
|
||||
### 2.3 `capabilities` are requests
|
||||
|
||||
The manifest is the project's **request** for permissions. It is never read
|
||||
when constructing the host capability set; only CLI flags/grant APIs create
|
||||
grants (`spec/security.md` §2). `net` has no implementing host op, and
|
||||
`process`/foreign execution still needs `--allow-foreign`.
|
||||
|
||||
## 3. `marqdo.lock`
|
||||
|
||||
`lock_version: 1`, packages sorted by name:
|
||||
|
||||
```yaml
|
||||
lock_version: 1
|
||||
packages:
|
||||
- name: geo
|
||||
source: path:./packages/geo
|
||||
sha256: 101a6f6db4cc48d8c138b4716028da9bc8934b5f02b2235e020fbcabba15b757
|
||||
```
|
||||
|
||||
| Field | Semantics |
|
||||
|---|---|
|
||||
| `lock_version` | must be `1`; any other value is rejected ("unsupported lock_version N (expected 1)") |
|
||||
| `packages[].name` | dependency name from the manifest |
|
||||
| `packages[].source` | `path:<manifest path as written>`; a git source would be `git:...@rev`, but git is not implemented in this build |
|
||||
| `packages[].sha256` | optional; lowercase hex SHA-256 of the dependency tree |
|
||||
|
||||
### 3.1 Deterministic serialization
|
||||
|
||||
`LockFile::to_yaml` sorts packages by name before serializing. The format
|
||||
contains no timestamps, no absolute paths and no machine-specific values, so
|
||||
repeated `marqdo lock` runs over unchanged dependencies are byte-identical.
|
||||
`LockFile::from_yaml` also sorts packages on load, so equality comparison is
|
||||
order-insensitive.
|
||||
|
||||
### 3.2 Tree hash
|
||||
|
||||
For a path dependency, `lock` hashes the dependency tree
|
||||
(`crates/cli/src/commands.rs::hash_tree`):
|
||||
|
||||
1. walk the dependency directory with `walkdir` (`sort_by_file_name`), keeping
|
||||
regular files only (symlinks are not followed and are skipped);
|
||||
2. sort files by their root-relative path (normalized to `/`);
|
||||
3. feed into SHA-256, for each file in order:
|
||||
`relative_path bytes`, a `0x00` byte, `file bytes`, a `0x00` byte;
|
||||
4. hex-encode the digest.
|
||||
|
||||
This is a deterministic content fingerprint, not a Merkle tree and not a
|
||||
cryptographic commitment to file modes, symlinks or empty directories. It
|
||||
detects content changes and additions/removals of regular files.
|
||||
|
||||
### 3.3 Lock generation rules
|
||||
|
||||
`marqdo lock` (run in a project):
|
||||
|
||||
1. discovers the project root from the cwd;
|
||||
2. loads and validates `marqdo.yaml`;
|
||||
3. for each manifest dependency, in map (sorted) order:
|
||||
* a git source → error "git dependencies are not supported by this build
|
||||
(path dependencies only)"; no lock is written;
|
||||
* no path source → error;
|
||||
* the dependency path must exist; the tree is hashed;
|
||||
4. writes `marqdo.lock` (exit 0, `locked N package(s)`).
|
||||
|
||||
What it does **not** do: resolve semver ranges, traverse a dependency's own
|
||||
`marqdo.yaml`/transitive dependencies, clone git repositories, download
|
||||
registries, verify package signatures, or install anything.
|
||||
|
||||
## 4. `sync`
|
||||
|
||||
`marqdo sync [--locked]` (run in a project):
|
||||
|
||||
1. discovers the root; loads the manifest and the existing `marqdo.lock`
|
||||
(missing lock → error "run `marqdo lock` first", exit 1);
|
||||
2. recomputes the expected lock file exactly as `lock` would (including tree
|
||||
hashes);
|
||||
3. with `--locked`, compares the expected lock with the on-disk lock; a
|
||||
difference (manifest change or dependency content change) prints
|
||||
"lockfile is out of date or dependency contents changed; run `marqdo lock`"
|
||||
and exits 1;
|
||||
4. without `--locked`, prints `synchronized N package(s)` and exits 0.
|
||||
|
||||
Current behavior: `sync` **never rewrites** the lock file and never materializes
|
||||
a `.marqdo/env` directory. It is a verification command only. There is no
|
||||
`--offline` flag and no `lock --locked` flag (the blueprint example
|
||||
`marqdo lock --locked` is not implemented; use `sync --locked`).
|
||||
|
||||
## 5. `init`
|
||||
|
||||
`marqdo init [dir]`:
|
||||
|
||||
* refuses to overwrite an existing `marqdo.yaml` (exit 1);
|
||||
* creates the directory if needed;
|
||||
* derives the project name from the directory's final path component (falling
|
||||
back to `example`);
|
||||
* writes `marqdo.yaml` from `Manifest::template`:
|
||||
|
||||
```yaml
|
||||
project:
|
||||
name: <name>
|
||||
version: 0.1.0
|
||||
marqdo: '>=0.1,<1'
|
||||
```
|
||||
|
||||
(empty dependencies/plugins/capabilities are omitted);
|
||||
* writes `index.mq.md`:
|
||||
|
||||
```markdown
|
||||
## main
|
||||
|
||||
**问候 = "你好,Marqdo"**
|
||||
|
||||
> 打印(问候)
|
||||
|
||||
*问候*
|
||||
```
|
||||
|
||||
* writes `.gitignore` containing `.marqdo/`;
|
||||
* prints the next-step hint `marqdo check index.mq.md && marqdo run index.mq.md`.
|
||||
|
||||
`init` does not create `marqdo.lock`, `.marqdo/`, a plugin directory or a git
|
||||
repository.
|
||||
|
||||
## 6. `add` / `remove`
|
||||
|
||||
Both commands edit **only** `marqdo.yaml` (and, for `remove`, prune a matching
|
||||
stale lock entry). They do not fetch, install, resolve or authorize anything.
|
||||
|
||||
* `marqdo add <name> --path <dir>` or
|
||||
`marqdo add <name> --git <url> --rev <sha>`:
|
||||
validate the dependency shape (same rules as §2.2), refuse an existing name
|
||||
(exit 1), insert it, write the manifest, and tell the user to run
|
||||
`marqdo lock`. Adding a git dependency is allowed at the manifest level even
|
||||
though `lock` will refuse it later.
|
||||
* `marqdo remove <name>`: remove the dependency from the manifest (exit 1 if
|
||||
absent), and drop a same-named entry from an existing lock file. It does not
|
||||
re-hash or re-lock the rest of the project.
|
||||
|
||||
This differs from the original task brief that listed `add`/`remove` as
|
||||
"not supported yet": the CLI does implement them as manifest editors. They
|
||||
are intentionally shallow and should not be confused with a dependency
|
||||
resolver.
|
||||
|
||||
## 7. JSON output
|
||||
|
||||
`check`, `inspect`, `run`, `call`, `plugin inspect`, `add`, `remove`, `lock`
|
||||
and `sync` accept `--json` and print a single JSON object on stdout. Relevant
|
||||
package shapes:
|
||||
|
||||
* `lock`: `{"command":"lock","success":true,"packages":[...]}`
|
||||
* `sync`: `{"command":"sync","success":true,"locked":<bool>,"packages":[...]}`
|
||||
* `add`: `{"command":"add","success":true,"dependency":"<name>","manifest":"<path>"}`
|
||||
* `remove`: `{"command":"remove","success":true,"dependency":"<name>"}`
|
||||
|
||||
Errors in these commands are printed as plain `error: ...` on stderr even when
|
||||
`--json` is passed (the JSON success envelope is not emitted; this is an
|
||||
inconsistency to fix).
|
||||
|
||||
## 8. Not supported in this build (explicit)
|
||||
|
||||
| Capability | Status |
|
||||
|---|---|
|
||||
| Git dependency resolution (clone/fetch/checkout) | Not implemented. Manifest accepts `git`+`rev`; `lock` refuses it. |
|
||||
| Registry / package publishing / SemVer resolution | Not implemented. |
|
||||
| Transitive dependency graph and conflict resolution | Not implemented; only direct manifest dependencies are hashed. |
|
||||
| `tree`, `update`, `vendor`, `package` commands | Not implemented. |
|
||||
| `--offline`, `--locked` on `lock` | Not implemented. |
|
||||
| Environment materialization (`.marqdo/env`, `cache`, `kernels`, `sessions`) | Not implemented; `init` only writes `.marqdo/` to `.gitignore`. |
|
||||
| Dependency signatures / trusted publishers / supply-chain scanning | Not implemented. |
|
||||
| Multiple/nested project boundary diagnostics | Not implemented; nearest `marqdo.yaml` wins. |
|
||||
| Lock file migration between `lock_version`s | Only version 1 is accepted. |
|
||||
| Plugin installation / plugin dependency management | Not implemented; `plugins:` in the manifest is declarative only. |
|
||||
|
||||
The lock/package format is deliberately small and deterministic so these can
|
||||
be added later without changing the identity model. Changing `lock_version`,
|
||||
`source` syntax or the tree-hash algorithm requires an ADR and a migration
|
||||
plan.
|
||||
@@ -0,0 +1,419 @@
|
||||
# Marqdo Plugin ABI v1
|
||||
|
||||
Status: **frozen ABI v1.0** (`MQ_ABI_MAJOR=1`, `MQ_ABI_MINOR=0`). The canonical
|
||||
C header is `include/marqdo_plugin_abi_v1.h`; the Rust mirrors are in
|
||||
`crates/marqdo-plugin-abi/src/abi.rs`; the plugin-side SDK is
|
||||
`crates/marqdo-plugin-sdk`.
|
||||
|
||||
> **A C ABI is not a sandbox.** A native plugin is loaded into the host
|
||||
> process. It can read host memory, call arbitrary syscalls, bypass the
|
||||
> capability model and crash the host. Loading requires an explicit trust
|
||||
> decision. The isolated worker transport described by the blueprint is **not
|
||||
> implemented** in this build.
|
||||
|
||||
## 1. Independent versions
|
||||
|
||||
Three versions exist and are never conflated:
|
||||
|
||||
| Version | Current | Where |
|
||||
|---|---|---|
|
||||
| Marqdo language/semantics | M0.5, `spec/*` | `docs/DEVELOPMENT_BLUEPRINT.md`, specs |
|
||||
| Plugin ABI major.minor | `1.0` | `MQ_ABI_MAJOR`/`MQ_ABI_MINOR`, `marqdo_plugin_abi::ABI_MAJOR/ABI_MINOR` |
|
||||
| Plugin package SemVer | e.g. `0.1.0` | `plugin.yaml` `plugin.version` |
|
||||
|
||||
The JSON wire protocol has its own `wire_version = 1`
|
||||
(`spec/value-wire.md`); it is not the ABI major version.
|
||||
|
||||
## 2. Stable-layout types
|
||||
|
||||
The header defines only fixed-width integers, `size_t`-based length+pointer
|
||||
buffers, opaque host-owned handles and `extern "C"` function pointers. Rust
|
||||
`String`/`Vec`/trait objects/`Result`/panics, C++ exceptions, Go objects and
|
||||
raw host pointers never cross the boundary.
|
||||
|
||||
On a 64-bit target (`include/marqdo_plugin_abi_v1.h` static assertions and
|
||||
`crates/marqdo-plugin-abi/src/abi.rs` compile-time assertions):
|
||||
|
||||
| Type | Size | Align |
|
||||
|---|---|---|
|
||||
| `MqBytes` | 16 | 8 |
|
||||
| `MqOwnedBytes` | 16 | 8 |
|
||||
| `MqPluginApi` | 32 | 8 |
|
||||
| `MqHostApi` | 56 | 8 |
|
||||
| `MqPluginInvoke` (fn ptr) | 8 | — |
|
||||
|
||||
On a 32-bit target the Rust side asserts `MqBytes`/`MqOwnedBytes` = 8,
|
||||
`MqPluginApi` = 20 and `MqHostApi` = 28. The layout uses `size_t`, so an
|
||||
artifact is only loadable by a host built for the **same target triple, pointer
|
||||
width and byte order**. `(NULL, 0)` is the only empty buffer; a non-empty
|
||||
buffer with a NULL pointer is a protocol error.
|
||||
|
||||
### 2.1 Ownership and lifetimes
|
||||
|
||||
| Buffer | Producer | Lifetime / free rule |
|
||||
|---|---|---|
|
||||
| `args_wire`, `export_name`, `signature_wire`, capability `request_wire`, `plugin_metadata_wire` (as seen by host) | caller passing it | borrowed for the duration of the call; consumer must copy and must not retain or free |
|
||||
| `result_wire`, `error_wire`, capability `reply_wire` | the producing side | must be allocated with `host->alloc(host->host_data, size)` and is released with `host->free(host->host_data, data)` by the consumer after copying; never `malloc`/`new`/GC memory |
|
||||
| `plugin_metadata_wire` (as returned by plugin) | plugin static storage | valid only during `mq_plugin_entry_v1`; host copies it immediately |
|
||||
|
||||
`MqCallContext` is opaque, host-owned and valid only for the invocation it was
|
||||
minted for. Plugins must not dereference, cache or copy it.
|
||||
|
||||
### 2.2 Size limits (enforced by host / SDK)
|
||||
|
||||
| Constant | Bytes |
|
||||
|---|---|
|
||||
| `MQ_MAX_EXPORT_NAME_BYTES` | 256 |
|
||||
| `MQ_MAX_SIGNATURE_WIRE_BYTES` | 4096 |
|
||||
| `MQ_MAX_METADATA_WIRE_BYTES` | 65536 |
|
||||
| `MQ_MAX_REQUEST_WIRE_BYTES` | 16 MiB (16,777,216) |
|
||||
| `MQ_MAX_RESULT_WIRE_BYTES` | 64 MiB (67,108,864) |
|
||||
| `MQ_MAX_ERROR_WIRE_BYTES` | 1 MiB (1,048,576) |
|
||||
| `MQ_MAX_CAPABILITY_REPLY_BYTES` | 16 MiB |
|
||||
|
||||
Wire JSON decoding additionally applies the default `WireLimits` from
|
||||
`spec/value-wire.md` (depth 64, 1,000,000 items, 16 MiB strings). Oversized
|
||||
payloads are rejected, never truncated and reparsed.
|
||||
|
||||
## 3. Exported entry point and tables
|
||||
|
||||
Required symbol: `mq_plugin_entry_v1` (`MQ_PLUGIN_ENTRY_SYMBOL`):
|
||||
|
||||
```c
|
||||
int32_t mq_plugin_entry_v1(const MqHostApi *host, MqPluginApi *plugin);
|
||||
```
|
||||
|
||||
Called exactly once, by the loading thread:
|
||||
|
||||
* the host supplies a valid `MqHostApi` table and a zeroed `MqPluginApi` whose
|
||||
`struct_size = sizeof(MqPluginApi)`;
|
||||
* the plugin must validate the host table, fill `plugin.abi_major`,
|
||||
`plugin.abi_minor`, `plugin.plugin_metadata_wire` and register every export
|
||||
via `host->register_function`;
|
||||
* it returns `MQ_OK` only after all exports are registered; on non-zero the
|
||||
host ignores the plugin table and fails the load.
|
||||
|
||||
`MQ_OK` and error codes (stable integers):
|
||||
|
||||
| Code | Name | Meaning |
|
||||
|---|---|---|
|
||||
| 0 | `MQ_OK` | success |
|
||||
| 1 | `MQ_ERR_INVALID_ARGUMENT` | NULL pointer, bad pointer/length pair |
|
||||
| 2 | `MQ_ERR_ABI_MISMATCH` | ABI major mismatch or incompatible `struct_size` |
|
||||
| 3 | `MQ_ERR_REGISTRATION` | an export could not be registered |
|
||||
| 4 | `MQ_ERR_UNSUPPORTED` | operation/type/capability unsupported |
|
||||
| 5 | `MQ_ERR_OUT_OF_MEMORY` | host allocation failed |
|
||||
| 6 | `MQ_ERR_BAD_WIRE` | payload is not valid `wire_version` 1 JSON |
|
||||
| 7 | `MQ_ERR_INTERNAL` | unexpected failure, including caught panic |
|
||||
| 8 | `MQ_ERR_CAPABILITY` | capability request denied/failed |
|
||||
|
||||
### 3.1 Version negotiation
|
||||
|
||||
Host side (`negotiate` in `crates/marqdo-plugin-abi/src/abi.rs`):
|
||||
|
||||
* `plugin.abi_major != 1` → load fails with `abi_major_mismatch`;
|
||||
* `plugin.abi_minor` greater than the host minor is accepted but **clamped** to
|
||||
the host minor (currently 0); only features defined at that minor may be
|
||||
used;
|
||||
* the plugin must reject a host table with `host == NULL`,
|
||||
`host->struct_size < sizeof(MqHostApi)`, `host->abi_major != 1` or NULL
|
||||
alloc/free/register_function/request_capability pointers.
|
||||
|
||||
`struct_size` is the extension mechanism: fields may only be appended in a
|
||||
later ABI minor, never reordered or resized; a consumer only reads a field when
|
||||
`struct_size` covers it.
|
||||
|
||||
### 3.2 Threading and lifetime
|
||||
|
||||
v1 is single-threaded and synchronous: entry is called once; invocations and
|
||||
host callbacks for one plugin instance run on that thread with at most one call
|
||||
in flight. A plugin must not start threads that call back into `MqHostApi`.
|
||||
v1 has no shutdown callback and no hot unload/reload; the `LoadedPlugin`
|
||||
wrapper unloads only when dropped, and the docs require one instance per
|
||||
artifact path (ABI v1 plugins may keep the host table in a process-global).
|
||||
No panic/exception/longjmp may cross the boundary; the SDK catches Rust panics
|
||||
and returns `MQ_ERR_INTERNAL` with a structured error envelope.
|
||||
|
||||
## 4. `plugin.yaml`
|
||||
|
||||
```yaml
|
||||
plugin:
|
||||
name: demo.solver
|
||||
version: 0.1.0
|
||||
abi: "^1.0"
|
||||
kind: native
|
||||
exports:
|
||||
- name: solver.add
|
||||
signature: "($a: Int, $b: Int) -> Int"
|
||||
requires:
|
||||
- math.compute
|
||||
artifacts:
|
||||
- target: x86_64-unknown-linux-gnu
|
||||
file: solver.so
|
||||
sha256: "<64 lowercase hex>"
|
||||
```
|
||||
|
||||
Validation (`crates/marqdo-plugin-abi/src/manifest.rs::PluginManifest::validate`):
|
||||
|
||||
* `name`: dotted segments, each starting with a lowercase ASCII letter and
|
||||
containing only lowercase letters, digits, `-`/`_`; ≤ 256 bytes; package
|
||||
names starting with `marqdo.` or `builtin.` are reserved;
|
||||
* `version`: semver-ish `major.minor.patch` with optional `-prerelease` and
|
||||
`+build`; numeric components have no leading zeros; not a SemVer range
|
||||
resolver;
|
||||
* `abi`: caret-style requirement `^1`, `^1.0`, `1` or `1.0`. Caret semantics:
|
||||
`^M.m` means `>=M.m, <M+1.0`; the `0.x` special case pins the minor. It must
|
||||
be compatible with the host's `1.0`;
|
||||
* `kind`: `native`, `worker` or `wasm`; only `native` is loadable in-process in
|
||||
this build;
|
||||
* `exports`: at least one, at most 256, unique, namespaced (must contain `.`),
|
||||
no reserved prefix (`marqdo.`, `builtin.`, `core.`, `std.`, `host.`),
|
||||
non-empty signature ≤ 4096 bytes, signature starts with `(` and contains
|
||||
`->`, no control characters;
|
||||
* `requires`: at most 64, unique names from the whitelist `log`,
|
||||
`math.compute`, `fs.read`, `fs.write`, `net.request`, `clock`, `random`,
|
||||
`artifact.record`; this is a declaration, not a grant;
|
||||
* `artifacts`: 1–32, one per target triple; target parses as a 3–4 component
|
||||
triple (lowercase, alphanumeric/underscore components); `file` is a
|
||||
non-empty relative path ≤ 256 bytes with no absolute/`..` escape; `sha256` is
|
||||
exactly 64 hex characters.
|
||||
|
||||
Manifest parsing/validation never executes plugin code and performs no
|
||||
filesystem access (artifact verification is a separate step).
|
||||
|
||||
## 5. Artifact selection and verification
|
||||
|
||||
* `HostTarget::current()` derives a conventional triple from
|
||||
`std::env::consts` (`x86_64-unknown-linux-gnu`, `aarch64-apple-darwin`,
|
||||
`x86_64-pc-windows-msvc`, ...).
|
||||
* `resolve_artifact` requires **exact triple equality**; pointer width and byte
|
||||
order follow from the triple. No fallback to another architecture.
|
||||
* `verify_artifact` resolves the path below the manifest directory and compares
|
||||
the file's SHA-256 with the manifest (case-insensitive hex). A missing file
|
||||
is `missing_artifact`; a digest mismatch is `hash_mismatch`.
|
||||
* Verification proves the artifact is the file described by the manifest; it
|
||||
proves nothing about trust or authorization.
|
||||
|
||||
## 6. Loader lifecycle (host side)
|
||||
|
||||
`crates/marqdo-plugin-abi/src/loader.rs::LoadedPlugin::open_with`:
|
||||
|
||||
1. `manifest.validate()`;
|
||||
2. require `kind == native` (otherwise `plugin_kind_not_loadable_in_process`);
|
||||
3. resolve artifact for the host target and verify its sha256;
|
||||
4. `libloading::Library::new` (a trust decision, not a sandbox);
|
||||
5. build the host API table and `HostState`, initialize `MqPluginApi`;
|
||||
6. call `mq_plugin_entry_v1` once; non-zero → structured load error;
|
||||
7. require `plugin_api.struct_size >= sizeof(MqPluginApi)`;
|
||||
8. negotiate ABI major/minor;
|
||||
9. copy `plugin_metadata_wire` and require metadata `name`/`version` to match
|
||||
the manifest exactly;
|
||||
10. take registered exports and cross-check against the manifest: duplicate
|
||||
registrations, missing declared exports, signature mismatches and
|
||||
undeclared exports all fail the load;
|
||||
11. keep the library mapped for the lifetime of `LoadedPlugin`.
|
||||
|
||||
Invocation (`LoadedPlugin::invoke`): validate the request envelope, check the
|
||||
request size, find the registered export, mint a `MqCallContext`, call the
|
||||
plugin's `MqPluginInvoke`, copy and free both output buffers, then enforce the
|
||||
envelope contract:
|
||||
|
||||
* success (`MQ_OK`): `error_wire` must be empty; `result_wire` must be a
|
||||
non-empty `wire_version` 1 success envelope; the typed value is optional
|
||||
(absent `value` means `Unit`);
|
||||
* failure: `error_wire` must hold a `wire_version` 1 error envelope; a missing
|
||||
or invalid error envelope still fails the call (structured
|
||||
`plugin_call_failed`).
|
||||
|
||||
Capability requests from the plugin go through
|
||||
`MqHostApi::request_capability` into the host's `CapabilityHandler`, on the
|
||||
same thread, one at a time. The handler receives the capability name and
|
||||
request bytes and returns reply bytes or an error. When no handler is
|
||||
installed (the CLI's case) **every request is denied** (`MQ_ERR_CAPABILITY`).
|
||||
|
||||
## 7. Wire envelopes
|
||||
|
||||
All payloads are UTF-8 JSON with `wire_version = 1`. Values inside `args` and
|
||||
`value` use the type-tagged encoding of `spec/value-wire.md`.
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"wire_version": 1,
|
||||
"kind": "request",
|
||||
"function": "solver.add",
|
||||
"args": {
|
||||
"a": {"type": "int", "decimal": "20"},
|
||||
"b": {"type": "int", "decimal": "22"}
|
||||
},
|
||||
"context": {"trace_id": "optional", "capability_handle": "opaque-and-scoped"}
|
||||
}
|
||||
```
|
||||
|
||||
Response (success):
|
||||
|
||||
```json
|
||||
{"wire_version":1,"kind":"response","value":{"type":"int","decimal":"42"}}
|
||||
```
|
||||
|
||||
A success response with neither `value` nor `error` means `Unit`. A response
|
||||
with both is rejected (`ambiguous_response`). Error:
|
||||
|
||||
```json
|
||||
{"wire_version":1,"kind":"response","error":{
|
||||
"kind":"invoke","code":"missing_argument",
|
||||
"message":"required argument `a` is missing","recoverable":true
|
||||
}}
|
||||
```
|
||||
|
||||
Envelope limits: at most `MAX_ARGS = 1024` request arguments; request ≤ 16 MiB,
|
||||
response ≤ 64 MiB, error ≤ 1 MiB. `context` carries no secrets and no
|
||||
reconstructable OS handles. `signature_wire` is
|
||||
`{"signature":"<signature string>"}`; `plugin_metadata_wire` is
|
||||
`{"name":"<package name>","version":"<semver>"}` (extra fields ignored).
|
||||
|
||||
## 8. Error model
|
||||
|
||||
Host-side errors are `PluginError { kind, code, message, detail?, recoverable }`
|
||||
with stable `kind` values: `manifest`, `abi`, `artifact`, `load`, `wire`,
|
||||
`capability`, `invoke`, `unsupported`. Examples: `abi_major_mismatch`,
|
||||
`no_matching_artifact`, `hash_mismatch`, `missing_entry_symbol`,
|
||||
`metadata_name_mismatch`, `export_signature_mismatch`,
|
||||
`unsupported_wire_version`. `message`/`detail` must never contain secrets.
|
||||
They are serializable and are used as the wire error body.
|
||||
|
||||
When a plugin is used from Marqdo code, host errors become structured
|
||||
`RuntimeError`s (`crates/marqdo-host/src/host.rs::map_plugin_error`):
|
||||
|
||||
| Plugin error kind | Runtime code |
|
||||
|---|---|
|
||||
| `abi` | `plugin.abi_mismatch` |
|
||||
| `capability` | `permission.denied` |
|
||||
| `wire` | `runtime.type` |
|
||||
| `unsupported` | `kernel.unavailable` |
|
||||
| `manifest`/`artifact`/`load`/`invoke` | `plugin.failure` |
|
||||
|
||||
## 9. Using a plugin from `.mq.md`
|
||||
|
||||
This build supports explicitly trusted **in-process native** plugins only.
|
||||
|
||||
```bash
|
||||
# 1. Build the C example and refresh its sha256 in plugin.yaml
|
||||
./examples/plugin-demo/build.sh
|
||||
|
||||
# 2. Inspect without loading
|
||||
marqdo plugin inspect examples/plugin-demo
|
||||
|
||||
# 3. Load with an explicit trust decision and a capability grant, then run
|
||||
marqdo run examples/plugin-demo/plugin-call.mq.md \
|
||||
--plugin examples/plugin-demo \
|
||||
--trust-native-plugin \
|
||||
--allow-plugin demo.solver
|
||||
```
|
||||
|
||||
Flags (`run` and `call`): `--plugin <manifest-or-dir>` (repeatable),
|
||||
`--trust-native-plugin`, `--allow-plugin <package-name>`. `--allow-plugin`
|
||||
matches the manifest `plugin.name` (for example `demo.solver`), not the call
|
||||
namespace (`solver`).
|
||||
|
||||
Document syntax (`examples/plugin-demo/plugin-call.mq.md`):
|
||||
|
||||
```markdown
|
||||
## main
|
||||
|
||||
**结果 = solver.add(a=20, b=22)**
|
||||
|
||||
> 打印(结果)
|
||||
```
|
||||
|
||||
Semantics:
|
||||
|
||||
* the call namespace is derived from the **first** manifest export prefix
|
||||
(`solver.add` → namespace `solver`); a manifest whose exports use several
|
||||
prefixes registers only the first namespace, so exports under other prefixes
|
||||
are not reachable in this build;
|
||||
* calls accept **named arguments only**; positional arguments are a runtime
|
||||
type error ("plugin calls require named arguments");
|
||||
* each call becomes `HostOp::PluginInvoke` with a typed wire request; the
|
||||
plugin's typed response becomes a Marqdo value (`Unit` when absent);
|
||||
* the capability grant is re-checked on every call, not only at load;
|
||||
* plugin namespace calls are **not statically validated**: `check` passes a
|
||||
document that calls a namespace no plugin provides; at runtime the call fails
|
||||
with `symbol.unknown` ("no plugin namespace ... is loaded") unless
|
||||
`--plugin` loaded it;
|
||||
* plugin host-capability requests are denied because the CLI installs no
|
||||
capability handler (`LoadOptions::default()`);
|
||||
* `--mode strict` refuses to run before any plugin is loaded; `--mode pure`
|
||||
cannot grant `--allow-plugin`, so loading fails with `permission.denied`.
|
||||
|
||||
Trust boundary: `--trust-native-plugin` is an explicit unsafe decision; the
|
||||
process shares its address space with the plugin. In-process load is the only
|
||||
transport implemented — there is no worker process, Wasm component, hot
|
||||
unload, or plugin capability bridge. A native plugin crash is a host crash.
|
||||
|
||||
## 10. SDK usage (`crates/marqdo-plugin-sdk`)
|
||||
|
||||
The SDK hides the raw C layout and exports the entry symbol with
|
||||
`mq_plugin_entry!`. A complete Rust plugin crate:
|
||||
|
||||
```rust
|
||||
use marqdo_plugin_sdk::{mq_plugin_entry, CallContext, PluginError, Value};
|
||||
|
||||
fn add(_ctx: &mut CallContext, args: &[u8]) -> Result<Vec<u8>, PluginError> {
|
||||
let request = marqdo_plugin_sdk::wire::decode_request(args)?;
|
||||
let values = request.typed_args()?;
|
||||
let a = marqdo_plugin_sdk::wire::int_arg(&values, "a")?;
|
||||
let b = marqdo_plugin_sdk::wire::int_arg(&values, "b")?;
|
||||
marqdo_plugin_sdk::wire::success(Some(Value::Int((a + b).into())))
|
||||
}
|
||||
|
||||
mq_plugin_entry! {
|
||||
name: "demo.solver",
|
||||
version: "0.1.0",
|
||||
exports: {
|
||||
"solver.add" => "($a: Int, $b: Int) -> Int" => add,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The macro validates the host table, reports the plugin ABI version and metadata,
|
||||
registers every export and returns `MQ_OK` only if all registrations succeed.
|
||||
Handlers receive a `&mut CallContext`; use
|
||||
`CallContext::capability_request(name, request_bytes)` for an already-authorized
|
||||
host capability (the CLI grants none today), and
|
||||
`CallContext::log(level, message)` as a convenience. Panics are caught at the
|
||||
ABI boundary and converted to a structured error. The SDK depends only on
|
||||
`marqdo-plugin-abi`, never on host-only crates or the interpreter.
|
||||
|
||||
## 11. C demo (`examples/plugin-demo`)
|
||||
|
||||
`examples/plugin-demo/solver.c` is a dependency-free C implementation of one
|
||||
export (`solver.add`, `Int + Int`) using a minimal JSON scanner; it validates
|
||||
the host table, reports metadata and registers the export. `build.sh` compiles
|
||||
it (`cc -shared -fPIC -I include`) and rewrites the `sha256` in `plugin.yaml`
|
||||
(the checked-in manifest uses a zero placeholder until you build). The
|
||||
`examples/plugin-demo/README.md` walks through the security model, build,
|
||||
inspect and call flow.
|
||||
|
||||
The integration test `crates/marqdo-plugin-abi/tests/loader_unix.rs` compiles
|
||||
this exact `solver.c` with `cc`, loads it with `libloading`, round-trips a wire
|
||||
request, and rebuilds it with `-DMQ_SOLVER_REPORT_ABI_MAJOR=2` to prove that a
|
||||
wrong ABI major fails the load. It also covers a missing entry symbol, missing
|
||||
artifact, hash mismatch, malformed manifest and target mismatch. The tests skip
|
||||
when no C compiler is available.
|
||||
|
||||
## 12. Not implemented / explicit limitations
|
||||
|
||||
| Item | Status |
|
||||
|---|---|
|
||||
| Isolated plugin worker transport (length-prefixed IPC) | Not implemented; `PlatformCapabilities::worker_processes = false`. |
|
||||
| Wasm Component + WIT | Not implemented. |
|
||||
| Hot unload/reload | Not supported by ABI v1 (no shutdown callback). |
|
||||
| Plugin capability bridge from the CLI | Not implemented; CLI supplies no handler, so all plugin host-capability requests are denied. |
|
||||
| Plugin installation by package manager | Not implemented; `marqdo.yaml` `plugins:` is declarative only. |
|
||||
| Static validation of plugin namespaces/exports/signatures against loaded plugins | Not implemented; errors are runtime-only. |
|
||||
| Multiple namespaces per plugin | Not implemented; single namespace from the first export prefix. |
|
||||
| Positional plugin arguments | Not supported; named only. |
|
||||
| Thread-safe / concurrent invocation | Not supported (single-threaded v1 contract). |
|
||||
| Old `marqdo_abi.h` v2 compatibility | Not implemented and not claimed; a separate adapter would be required. |
|
||||
@@ -0,0 +1,362 @@
|
||||
# Marqdo References, Imports and Calls v1
|
||||
|
||||
Status: **frozen for M0.5 except where marked "current limitation".** Language
|
||||
changes require an ADR plus updated tests.
|
||||
|
||||
Normative implementation:
|
||||
|
||||
| Concern | Code |
|
||||
|---|---|
|
||||
| Front matter parsing | `crates/marqdo-markdown/src/frontmatter.rs` |
|
||||
| Module loading, project root, cycles | `crates/cli/src/loader.rs` |
|
||||
| Module/Unit/Function identities | `crates/marqdo-ir/src/lib.rs` |
|
||||
| Heading scopes, anchors | `crates/marqdo-frontend/src/units.rs` |
|
||||
| Resource collection and attribution | `crates/marqdo-frontend/src/resources.rs` |
|
||||
| Link-call mapping | `crates/marqdo-frontend/src/map.rs` |
|
||||
| `ref(...)` runtime resolution | `crates/marqdo-runtime/src/builtins_impl.rs` |
|
||||
| Static link graph (`inspect --refs`) | `crates/cli/src/commands.rs` |
|
||||
| Cross-module call validation | `crates/marqdo-frontend/src/calls.rs` |
|
||||
|
||||
This file complements [`mapping.md`](mapping.md) (AST roles) and
|
||||
[`package-format.md`](package-format.md) (manifest/lock file).
|
||||
|
||||
## 1. Three edge kinds, never conflated
|
||||
|
||||
Blueprint §4.1 distinguishes three relations. The implementation keeps them
|
||||
separate and **only the call edge can execute code**.
|
||||
|
||||
| Edge | Written as | Resolved when | Executes? |
|
||||
|---|---|---|---|
|
||||
| **Import** | YAML front matter `import:` mapping | module load / static analysis | No. Imported modules only register definitions; their `main` never runs. |
|
||||
| **Reference** | Markdown `[label](url)` | `inspect --refs` (static graph) | No. Rendered/read as documentation only. |
|
||||
| **Call** | `> f(...)` block quote, link-form block quote `> [f](#f)(...)`, or a `CallExpr` in an executable payload | static validation, then execution | Yes, at runtime, through the interpreter. |
|
||||
|
||||
Consequences:
|
||||
|
||||
* A Markdown link never calls, imports or loads anything. `[计算作用力](#计算作用力)`
|
||||
is a reference; `> [计算作用力](#计算作用力)()` is a call.
|
||||
* `ref("#anchor")` is a **runtime builtin**, not an import; it only resolves an
|
||||
inert resource or function reference in the running module.
|
||||
* The reference graph is static metadata and creates no proof obligation.
|
||||
|
||||
## 2. Canonical identities
|
||||
|
||||
These types live in `crates/marqdo-ir/src/lib.rs`.
|
||||
|
||||
### 2.1 `ModuleId`
|
||||
|
||||
```rust
|
||||
pub struct ModuleId(pub String);
|
||||
```
|
||||
|
||||
* A canonical **project-root-relative path with `/` separators**, for example
|
||||
`index.mq.md`, `packages/geometry/index.mq.md`.
|
||||
* Computed in `crates/cli/src/loader.rs::Loader::module_id_for` by taking the
|
||||
canonicalized file path, stripping the project root, and joining the path
|
||||
components with `/`.
|
||||
* There is no "module name" identity separate from the path: the module id is
|
||||
the file location inside the project. Imported `@dependency` modules keep
|
||||
their project-root-relative path (for example
|
||||
`packages/geometry/index.mq.md`).
|
||||
* A file that canonicalizes outside the project root is rejected with
|
||||
"outside the project root" (entry: fatal load error; imported module:
|
||||
`module.unresolved` diagnostic).
|
||||
* Module ids use `BTreeMap` ordering, so program iteration and JSON output are
|
||||
deterministic.
|
||||
|
||||
### 2.2 `UnitId`
|
||||
|
||||
```rust
|
||||
pub struct UnitId { pub module: ModuleId, pub path: Vec<String> }
|
||||
```
|
||||
|
||||
* One addressable heading range. `path` is the canonical heading path from the
|
||||
outermost heading to this unit (root unit has an empty path).
|
||||
* Canonical heading names are NFC-normalized, trimmed, with internal
|
||||
whitespace collapsed to one space (`marqdo_markdown::normalize_name`).
|
||||
* `Display` renders `<module>#<path joined by '/'>`; the root unit renders as
|
||||
`<module>#`.
|
||||
* `UnitKind` is `root`, `namespace` (`#`), `function` (`##`) or `section`
|
||||
(`###`+). Sections are addressable but not callable.
|
||||
|
||||
### 2.3 `FunctionId`
|
||||
|
||||
```rust
|
||||
pub struct FunctionId { pub module: ModuleId, pub name: String }
|
||||
```
|
||||
|
||||
* `name` is the **namespace-qualified canonical function name**: `ns.fn` when
|
||||
the level-2 heading is inside a level-1 namespace, otherwise `fn`
|
||||
(`crates/marqdo-frontend/src/units.rs`). `Display` is `<module>::<name>`.
|
||||
* A function heading under a namespace yields both `FunctionId.name` (`ns.fn`)
|
||||
and `Function.local_name` (`fn`) plus `namespace: Some("ns")`.
|
||||
* Lookup (`Module::function`) first matches `id.name`, then a unique
|
||||
`local_name`; if several namespaces define the same local name the lookup
|
||||
returns `None` rather than guessing.
|
||||
* No overloads: duplicate canonical names in one scope are hard errors
|
||||
(`markdown.invalid_construct`, "duplicate heading name ... in the same
|
||||
scope") and are never auto-suffixed.
|
||||
|
||||
### 2.4 Anchors are a separate namespace, not identity
|
||||
|
||||
`UnitIndex.anchors: BTreeMap<String, UnitId>` maps GitHub-style heading slugs
|
||||
to units (`crates/marqdo-frontend/src/units.rs::github_slug`):
|
||||
|
||||
* digits/letters/`_` are kept and lowercased; spaces, `-` and tabs become `-`;
|
||||
all other punctuation is dropped;
|
||||
* collisions receive `-1`, `-2`, ... suffixes in document order (separate
|
||||
counters per base slug);
|
||||
* anchors exist for navigation and `ref("#...")`; they are **not** identity.
|
||||
Internal ids stay canonical, so a heading rename is a link-breaking change
|
||||
but never changes the canonical unit path.
|
||||
|
||||
## 3. Front matter imports
|
||||
|
||||
Imports exist only as an explicit YAML mapping in the document's front matter
|
||||
(recognized by the Markdown profile only at the start of the file):
|
||||
|
||||
```markdown
|
||||
---
|
||||
import:
|
||||
geometry: "@geometry"
|
||||
util: "./lib/util.mq.md"
|
||||
---
|
||||
|
||||
# main
|
||||
```
|
||||
|
||||
Parsing rules (`crates/marqdo-markdown/src/frontmatter.rs::imports`):
|
||||
|
||||
* the `import` key must be a mapping of **string alias → string target**;
|
||||
* aliases must be non-empty and unique within the document;
|
||||
* any other front matter key is metadata for tools and is ignored by the
|
||||
interpreter; invalid YAML is a `frontmatter.invalid` diagnostic, never
|
||||
silently dropped;
|
||||
* imports are never inferred: a link, a file name or a directory scan does not
|
||||
create an import;
|
||||
* the diagnostic span recorded for all imports of a file is the whole front
|
||||
matter span (`ImportDecl.span`).
|
||||
|
||||
### 3.1 Target forms
|
||||
|
||||
`Loader::resolve_import` recognizes exactly two forms:
|
||||
|
||||
| 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). |
|
||||
| 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
|
||||
project root, the loader reports "outside the project root" and the import
|
||||
becomes a `module.unresolved` diagnostic.
|
||||
|
||||
### 3.2 Loader rules
|
||||
|
||||
`crates/cli/src/loader.rs` implements a static, non-executing load:
|
||||
|
||||
1. **Project root discovery** (`marqdo_package::discover_root`): walk up from
|
||||
the entry file (starting at its parent directory) to the nearest directory
|
||||
containing `marqdo.yaml`; if none is found, the entry file's parent
|
||||
directory is the root. There is no `--project` flag in this build and nested
|
||||
project boundaries are not diagnosed — the nearest manifest wins (open
|
||||
item).
|
||||
2. **Manifest** is optional at run time; if a `marqdo.yaml` exists in the root
|
||||
it is parsed and validated. An invalid manifest is a fatal load error
|
||||
(exit code 2), not a diagnostic list.
|
||||
3. **Entry module** is loaded first; `Program.main_module` is set to its
|
||||
module id.
|
||||
4. **Imports** are processed depth-first in front matter order. Each import
|
||||
target is resolved as in §3.1, then loaded recursively.
|
||||
5. **Cycle detection** uses the path stack: if the canonical target path is
|
||||
already being loaded, the importing file gets a `module.cycle` diagnostic
|
||||
("import ... creates an import cycle") and the import edge is skipped.
|
||||
Modules are otherwise loaded once; a second import of an already-loaded
|
||||
module reuses it.
|
||||
6. **Definitions only**: loading parses, indexes and analyzes source. No
|
||||
function is called, no `main` is run, no foreign/plugin code is touched.
|
||||
7. After loading, `calls::validate_program` validates cross-module and
|
||||
qualified-builtin calls and attaches diagnostics to the emitting file.
|
||||
8. Diagnostics are collected in deterministic (load) order; the CLI renders
|
||||
them on stderr (or as JSON).
|
||||
|
||||
Failure mapping: a failure to load the entry file or read the manifest is a
|
||||
**fatal load error** (`exit 2`, `error: ...` on stderr). A failure to resolve
|
||||
an imported module becomes a `module.unresolved` **diagnostic** on the
|
||||
importing file and `check`/`run` then exit 1 (run refuses to execute a project
|
||||
with compile errors).
|
||||
|
||||
## 4. Markdown reference edges (`inspect --refs`)
|
||||
|
||||
`marqdo inspect <file> --refs` walks every `link` node in the entry document
|
||||
and prints/writes an edge array (`crates/cli/src/commands.rs::refs_json`,
|
||||
`resolve_link`). Each edge has:
|
||||
|
||||
```json
|
||||
{
|
||||
"source": "references.mq.md",
|
||||
"url": "./math.mq.md#计算圆面积",
|
||||
"span": {"start": 54, "end": 95},
|
||||
"resolved": true,
|
||||
"target_module": "math.mq.md",
|
||||
"target_unit": "math.mq.md#数学/计算圆面积"
|
||||
}
|
||||
```
|
||||
|
||||
Resolution rules:
|
||||
|
||||
1. `#fragment` → look up the entry module's anchors, then fall back to a unit
|
||||
whose canonical **name** equals the fragment.
|
||||
2. `http://` / `https://` URL → counted as resolved with no local target.
|
||||
External links are never downloaded, imported or executed.
|
||||
3. anything else → split optional `#fragment`, canonicalize the path relative
|
||||
to the **entry file's directory**, require the target to be inside the
|
||||
project root, derive its `ModuleId`, and look up anchors/names in that
|
||||
module. If the target module is not imported, its source is **read and
|
||||
analyzed statically on demand** (`analyze_source`) so broken anchors can be
|
||||
detected without execution.
|
||||
4. `resolved: false` means the file or anchor could not be resolved.
|
||||
|
||||
Current limitations (documented, not hidden):
|
||||
|
||||
* `check --refs` validates every loaded document's link nodes and reports
|
||||
broken same-document anchors, missing files and missing target anchors as
|
||||
`ref.unresolved` errors, changing the command's exit status. Plain `check`
|
||||
(without `--refs`) does not treat reference problems as errors, because links
|
||||
are static knowledge references rather than execution inputs.
|
||||
* `inspect --refs` prints the edge graph for the entry document only; other
|
||||
loaded modules are still covered by `check --refs`.
|
||||
* The graph is not emitted for call edges or import edges; imports are visible
|
||||
through module counts/`inspect` summaries, calls through `inspect --ir`.
|
||||
|
||||
## 5. `ref("#anchor")` runtime semantics
|
||||
|
||||
`ref` is a **pure builtin** (`crates/marqdo-ir/src/builtins.rs`,
|
||||
`crates/marqdo-runtime/src/builtins_impl.rs::resolve_ref`). It never evaluates
|
||||
the referenced resource and requires no capability.
|
||||
|
||||
Signature: `ref(target: Text) -> Formula | Code | FunctionRef`.
|
||||
|
||||
Algorithm:
|
||||
|
||||
1. `target` must start with `#`; otherwise `symbol.unknown` with the message
|
||||
"ref target ... must be a `#heading` anchor". It is not a URL resolver.
|
||||
2. The fragment is looked up **only in the running module's anchor map**
|
||||
(`Module::anchors`). In this build `ref` does not fall back to canonical
|
||||
unit names and does not search imported modules; a fragment that is not a
|
||||
GitHub slug yields `symbol.unknown` ("no heading matches ...").
|
||||
3. The matching unit's resources (`Module::resources_of`, computed from the
|
||||
unit's heading range) are examined:
|
||||
* **exactly one resource** → `Formula { latex, display, unit }` or
|
||||
`Code { language, source, content_hash, unit }`. `unit` is the
|
||||
`UnitId::Display` string.
|
||||
* **zero resources** → if a function's unit equals the resolved unit, return
|
||||
a `FunctionRef`; otherwise `symbol.unknown`.
|
||||
* **more than one resource → `ref.ambiguous`** with the message
|
||||
"`#...` contains N resources; select one explicitly instead of relying on
|
||||
the first". The runtime **never** picks the first resource. This is
|
||||
blueprint issue S-07 and is frozen by ADR-0003.
|
||||
4. Resource attribution (`resources.rs`): a formula or code fence belongs to
|
||||
the **innermost enclosing heading unit** by source span; resources are kept
|
||||
in document order. Markdown `math`/`math_block` and `code_block` nodes
|
||||
become resources; inline `code` spans do not.
|
||||
|
||||
Example (from `examples/math.mq.md`):
|
||||
|
||||
```markdown
|
||||
## 圆面积公式
|
||||
|
||||
$$
|
||||
A = \pi r^2
|
||||
$$
|
||||
|
||||
## 计算圆面积
|
||||
|
||||
**面积 = math.eval(ref("#圆面积公式"), {"r": 4}, target="A")**
|
||||
|
||||
*面积*
|
||||
```
|
||||
|
||||
`ref("#圆面积公式")` resolves the single formula under that heading; the
|
||||
formula is still inert until `math.eval` consumes it.
|
||||
|
||||
### 5.1 Error model of `ref`
|
||||
|
||||
| Situation | Runtime code |
|
||||
|---|---|
|
||||
| target not starting with `#` | `symbol.unknown` |
|
||||
| no anchor/unit matches | `symbol.unknown` |
|
||||
| unit has no formula/code/function | `symbol.unknown` |
|
||||
| unit has ≥ 2 resources | `ref.ambiguous` |
|
||||
| module not present in the program | `symbol.unknown` |
|
||||
|
||||
`ref.unresolved` (`crates/marqdo-value/src/diagnostic.rs`) is a **compile-time**
|
||||
code used by link-form call mapping, not by the `ref` builtin.
|
||||
|
||||
## 6. Call edges
|
||||
|
||||
### 6.1 Expression calls
|
||||
|
||||
Inside a function body, `CallExpr` nodes call:
|
||||
|
||||
* a local function (same module, namespace-aware resolution: current namespace
|
||||
first, then canonical name, then a unique local name);
|
||||
* an imported function through its alias: `geometry.圆面积(r=2)`;
|
||||
* a builtin (`打印`, `math.eval`, ...) by canonical name or alias;
|
||||
* a `FunctionRef` value obtained earlier (for example from `ref`).
|
||||
|
||||
Static validation (`crates/marqdo-frontend/src/calls.rs`) checks arity,
|
||||
argument names and the named-only rule for inferred signatures. Type errors are
|
||||
runtime concerns in v1.
|
||||
|
||||
### 6.2 Block-quote calls
|
||||
|
||||
`> f(...)` is a call statement; the payload must parse as a complete
|
||||
`CallExpr` (see `mapping.md` §4.5). Its result is discarded.
|
||||
|
||||
Link form `> [label](#anchor)(args)` resolves the link target **within the
|
||||
current module** using the anchor map, then the canonical unit-name fallback,
|
||||
and requires the target to be a function unit. Notes:
|
||||
|
||||
* a zero-argument link call must still write `()`; proxy `> [f](#f)` is a hard
|
||||
error;
|
||||
* `http(s)` link targets used as calls are a hard `ref.unresolved` error;
|
||||
external links are never downloaded or called;
|
||||
* a relative-path link target such as `> [f](./other.mq.md#f)(x=1)` is
|
||||
**not supported** in this build: mapping reports `module.unresolved` with
|
||||
the hint to import the module in front matter and call through its alias.
|
||||
Cross-module calls therefore go through an alias expression
|
||||
(`geometry.f(...)`), not through a Markdown link. Blueprint §2.4's
|
||||
`> [计算面积](./math.mq.md#计算面积)(r=3)` example is a **known divergence**.
|
||||
|
||||
### 6.3 Imported alias calls
|
||||
|
||||
At runtime an alias call resolves via `Program::resolve_import` to the target
|
||||
module id; an unresolved alias is `symbol.unknown`. The static validator
|
||||
checks the target module exists and the function/arity match when the whole
|
||||
program is loaded, and emits `symbol.unknown` for an unknown function.
|
||||
|
||||
## 7. Diagnostics for references/imports
|
||||
|
||||
| Code | Severity | Where produced |
|
||||
|---|---|---|
|
||||
| `module.unresolved` | error | loader (bad import target), call mapping (relative-path link call) |
|
||||
| `module.cycle` | error | loader |
|
||||
| `ref.unresolved` | error | link-form call target mapping, external links used as calls, `check --refs` link validation |
|
||||
| `ref.ambiguous` | runtime error | `ref` builtin (≥ 2 resources) |
|
||||
| `symbol.unknown` | error / runtime error | static call validation; `ref` anchor miss at runtime |
|
||||
| `frontmatter.invalid` | error | analyser (bad YAML/import shape) |
|
||||
| `markdown.invalid_construct` | error | empty/duplicate heading names, invalid link-call shape |
|
||||
|
||||
## 8. Current limitations (open ADR candidates)
|
||||
|
||||
1. `ref("#...")` is module-local and has no cross-module form; there is no
|
||||
documented syntax for referencing a resource in an imported module.
|
||||
2. Ordinary Markdown link resolution is inspection-only; `check` does not
|
||||
validate links and there is no `--refs` flag on `check`.
|
||||
3. Link-form calls cannot cross modules (see §6.2).
|
||||
4. Headings inside container blocks or fenced code never create units, and a
|
||||
heading whose name is empty is a hard error rather than an ignored heading.
|
||||
5. Anchor lookup in `ref` does not fall back to canonical unit names, while
|
||||
link-form calls and `inspect --refs` do. This asymmetry is intentional for
|
||||
the frozen build but should be unified by a future ADR (it is easy to get
|
||||
surprising results with headings containing punctuation).
|
||||
@@ -0,0 +1,276 @@
|
||||
# Marqdo Security Model (implemented status)
|
||||
|
||||
Status: **describes the M0.5 implementation, including honest gaps.** This is
|
||||
not a security audit; it documents what the code actually enforces.
|
||||
|
||||
Normative implementation:
|
||||
|
||||
| Concern | Code |
|
||||
|---|---|
|
||||
| Sandbox modes, strict refusal | `crates/marqdo-sandbox/src/lib.rs` |
|
||||
| Capability sets and checks | `crates/marqdo-host/src/capability.rs` |
|
||||
| Host dispatch and per-op checks | `crates/marqdo-host/src/host.rs` |
|
||||
| Project-rooted filesystem | `crates/marqdo-host/src/fs.rs` |
|
||||
| Budgets | `crates/marqdo-host/src/budget.rs` |
|
||||
| Effect tracing | `crates/marqdo-host/src/trace.rs` |
|
||||
| Math/foreign adapters | `crates/marqdo-host/src/kernels.rs` |
|
||||
| Foreign child processes | `crates/marqdo-foreign/src/lib.rs` |
|
||||
| Plugin loading / in-process plugin calls | `crates/marqdo-host/src/host.rs` (`load_plugin`, `op_plugin_invoke`), `crates/marqdo-plugin-abi/src/loader.rs` |
|
||||
| CLI flags and capability construction | `crates/cli/src/main.rs`, `crates/cli/src/commands.rs` |
|
||||
|
||||
## 1. Modes
|
||||
|
||||
`SandboxMode` has three values (`pure`, `project`, `strict`), parsed by
|
||||
`SandboxMode::parse` and selectable on `run`/`call` with
|
||||
`--mode <pure|project|strict>` (default: `project`).
|
||||
|
||||
| Mode | Intended guarantee | Actual status in this build |
|
||||
|---|---|---|
|
||||
| `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. Sandboxing is cooperative (in-process checks), not OS isolation. |
|
||||
| `strict` | Untrusted documents/code/plugins under real OS isolation. | **Refused.** `PlatformCapabilities::detect()` always reports `strict: Unavailable` in this build because no OS isolation adapter (namespaces/seccomp/cgroups, Job Object, macOS sandbox) is implemented, and `worker_processes: false`. `marqdo_sandbox::enforce` returns `StrictUnavailable`; `run`/`call` print the reason and exit 1. There is **no fallback** to an unrestricted process. |
|
||||
|
||||
`marqdo inspect <file> --platform` prints the detection report, for example:
|
||||
|
||||
```json
|
||||
{
|
||||
"os": "linux",
|
||||
"arch": "x86_64",
|
||||
"strict": {"state": "unavailable",
|
||||
"reason": "no OS isolation adapter (namespaces/seccomp/cgroups) is implemented in this build"},
|
||||
"worker_processes": false
|
||||
}
|
||||
```
|
||||
|
||||
Design rule (blueprint §9): a cooperative in-process check is not a sandbox.
|
||||
Strict mode will remain refused until a platform isolation adapter exists; see
|
||||
ADR-0004.
|
||||
|
||||
## 2. Capability grants
|
||||
|
||||
`Capabilities` (`crates/marqdo-host/src/capability.rs`) is the effective grant
|
||||
set for one run:
|
||||
|
||||
| Field | Default (`pure`/`project`) | Used by |
|
||||
|---|---|---|
|
||||
| `console: bool` | `true` | currently **not checked** by `op_print`; print is always allowed and only bounded by the output budget (open item) |
|
||||
| `math: bool` | `true` | `math.*` host ops |
|
||||
| `fs_read: Vec<String>` | empty | `fs.read` |
|
||||
| `fs_write: Vec<String>` | empty | `fs.write` |
|
||||
| `net_hosts: Vec<String>` | empty | `Capabilities::check_net` exists, but no host op or builtin performs network access in this build |
|
||||
| `process: bool` | `false` | required together with `foreign` for `foreign.run`/`foreign.call` |
|
||||
| `foreign: bool` | `false` | foreign kernels |
|
||||
| `plugins: Vec<String>` | empty | `plugin.invoke` and `StdHost::load_plugin` |
|
||||
|
||||
Requests versus grants:
|
||||
|
||||
* `marqdo.yaml` `capabilities:` is a **request**. It is parsed and validated
|
||||
(`crates/marqdo-package/src/lib.rs`) but is not consulted when building the
|
||||
host capability set: only CLI flags/grant APIs produce grants. Declaring a
|
||||
capability never grants it.
|
||||
* `marqdo.yaml` `plugins:` likewise declares requirements only; it does not
|
||||
install, load or authorize a plugin.
|
||||
* CLI grant flags (`crates/cli/src/main.rs::SandboxArgs`):
|
||||
* `--allow-read <GLOB>` and `--allow-write <GLOB>` (repeatable, project mode
|
||||
only);
|
||||
* `--allow-foreign` (project mode only; sets `foreign = true` and
|
||||
`process = true`);
|
||||
* `--allow-plugin <PACKAGE-NAME>` (repeatable, project mode only; matches the
|
||||
manifest `plugin.name`, not the call namespace);
|
||||
* `--plugin <PATH>` (repeatable, loads a plugin after the trust check);
|
||||
* `--trust-native-plugin` (explicit in-process native trust decision).
|
||||
* There is no network grant flag and no plugin-worker flag in this build.
|
||||
|
||||
### 2.1 Filesystem grants are project-relative globs
|
||||
|
||||
`fs.read`/`fs.write` paths go through `ProjectFs::resolve` **before** the
|
||||
capability check:
|
||||
|
||||
* empty paths, absolute paths, `..` components and root/prefix components are
|
||||
rejected with `permission.denied`;
|
||||
* `.` components are dropped; the normalized relative string uses `/`;
|
||||
* the capability patterns are matched with the `glob` crate against that
|
||||
normalized relative path; a leading `./` is stripped on both sides, so
|
||||
`--allow-read ./data/**` matches `data/a.csv` and does not match
|
||||
`secrets/key`; `--allow-write output/**` does not grant reads;
|
||||
* an invalid glob pattern is treated as non-matching (`Pattern::new(...).
|
||||
unwrap_or(false)`), so it silently grants nothing (there is no diagnostic).
|
||||
|
||||
Additional FS limits (`crates/marqdo-host/src/fs.rs`):
|
||||
|
||||
* reads are capped at `MAX_READ_BYTES = 8 MiB`, must be regular files, must be
|
||||
UTF-8, and the canonicalized path must stay inside the project root;
|
||||
* writes create parent directories; before writing, the **closest existing
|
||||
ancestor** is canonicalized and must stay inside the project root.
|
||||
|
||||
Known caveat: for writes, only the closest existing ancestor is checked and the
|
||||
final write uses the non-canonical path. A symlink created between the check
|
||||
and the write (TOCTOU) could escape the root. Reads canonicalize the final path
|
||||
before reading. Full symlink/race hardening is an open P4 item.
|
||||
|
||||
### 2.2 Checks happen at execution points, not only at parse time
|
||||
|
||||
`Host::invoke` is the only path to effects. `StdHost::dispatch` routes every
|
||||
`HostOp` and the checks run immediately before the effect:
|
||||
|
||||
* `fs.read` → `capabilities.check_read(path)` then `ProjectFs::read`;
|
||||
* `fs.write` → `check_write` then `ProjectFs::write`;
|
||||
* math ops → `capabilities.math` (in `crates/marqdo-host/src/kernels.rs`);
|
||||
* foreign ops → `capabilities.check_foreign` (requires `foreign && process`);
|
||||
* plugin invoke → the namespace must be loaded, then
|
||||
`capabilities.check_plugin(package)` on **every call**; loading also checks
|
||||
the same grant once;
|
||||
* print → output budget only.
|
||||
|
||||
Static analysis may warn, but it never replaces these runtime checks. There is
|
||||
no host bypass for tests: tests use the same `StdHost`/`Capabilities` types.
|
||||
|
||||
## 3. Resource budgets
|
||||
|
||||
`Budget` (`crates/marqdo-host/src/budget.rs`) is per run:
|
||||
|
||||
| Limit | Default | Enforcement |
|
||||
|---|---|---|
|
||||
| evaluation fuel | `DEFAULT_FUEL = 5_000_000` | 1 unit per statement, 1 per expression node, 1 per branch condition, 1 per loop iteration; exhaustion → `resource.exhausted` |
|
||||
| call stack depth | `DEFAULT_STACK_LIMIT = 256` | checked before every function call → `resource.exhausted` |
|
||||
| program output bytes | `DEFAULT_OUTPUT_BYTES = 1 MiB` | `print` output and `fs.read` contents are charged; overflow → `resource.exhausted` |
|
||||
| wall clock | none by default | `--timeout <seconds>` sets a deadline; checked between statements/iterations and before loop conditions → `resource.exhausted`. It does **not** preempt a running host call. |
|
||||
|
||||
CLI overrides (`run`/`call`): `--fuel`, `--stack`, `--max-output`,
|
||||
`--timeout`. Additional fixed limits:
|
||||
|
||||
* foreign kernel: `StdHost.foreign_timeout = 10s` and
|
||||
`foreign_output_limit = 1 MiB` (stdout+stderr combined) per call.
|
||||
These are not configurable from the CLI in this build;
|
||||
* plugin wire: request ≤ 16 MiB, result ≤ 64 MiB, error ≤ 1 MiB, metadata
|
||||
≤ 64 KiB (see `include/marqdo_plugin_abi_v1.h`);
|
||||
* math kernel: 1 MB source, 256 parse depth, 512 AST nodes, exponent ≤ 10,000,
|
||||
exact-result ≤ 1,000,000 bits (`crates/marqdo-math`).
|
||||
|
||||
These are cooperative budgets in the interpreter and the supervisor. Only
|
||||
foreign child processes are terminated by the host (kill on timeout/output
|
||||
overflow). In-process math and native plugins are not OS-limited.
|
||||
|
||||
## 4. Effect tracing
|
||||
|
||||
`EffectTrace` records one entry per `Host::invoke` when enabled
|
||||
(`--trace`, or implicitly `--json` for `run`/`call`). Entry fields:
|
||||
|
||||
```json
|
||||
{"op":"print","module":"hello.mq.md","span":{"start":43,"end":57},
|
||||
"duration_micros":3,"result":"ok"}
|
||||
```
|
||||
|
||||
* `op` is the stable `HostOp::name()` (`print`, `math.eval`, `foreign.call`,
|
||||
`fs.write`, `plugin.invoke`, ...);
|
||||
* `module` is the initiating module id; `span` is the call site when known;
|
||||
* `result` is `"ok"` or the failure code (for example
|
||||
`permission.denied`);
|
||||
* argument values and file contents are **not** recorded, so ordinary secrets
|
||||
do not leak into traces by design. Paths are also not recorded today.
|
||||
* In text mode trace entries are printed to stderr (`trace: ...`); in JSON
|
||||
mode they are a `trace` array next to the program stdout string. Program
|
||||
output is always separate from diagnostics/trace (stdout vs stderr).
|
||||
|
||||
## 5. Foreign kernels: process isolation, not a sandbox
|
||||
|
||||
`crates/marqdo-foreign/src/lib.rs` runs Python/Node one-shot:
|
||||
|
||||
* the program is written to a private temp file (mode 0600 on Unix), the child
|
||||
runs with a fresh private temp directory (mode 0700) as cwd, and both are
|
||||
removed on drop;
|
||||
* direct `argv` exec (no shell, no interpolation); stdin carries the versioned
|
||||
request envelope; stdout/stderr are always piped and captured;
|
||||
* the environment is cleared except `PATH`;
|
||||
* the wall-clock deadline is enforced by polling `try_wait` and killing the
|
||||
direct child; output flooding kills the child once the combined byte budget
|
||||
is exceeded; a 2-second reader grace prevents a grandchild holding the pipes
|
||||
from hanging the host;
|
||||
* a non-zero exit is data (`RunStatus::Failed`, stderr preserved); a timeout is
|
||||
`TimedOut` for `run` and an error for the typed `call`.
|
||||
|
||||
What this is **not**: an OS sandbox. The child runs with the host user's
|
||||
privileges, can open absolute paths, use the network and spawn further
|
||||
processes. Clearing the environment and changing cwd are hygiene, not
|
||||
confinement. Grandchildren are not killed when the direct child is killed.
|
||||
Strict mode refuses to run at all, so foreign execution is only reachable in
|
||||
`project` mode with `--allow-foreign`.
|
||||
|
||||
## 6. Plugins: in-process native trust boundary
|
||||
|
||||
* The only plugin transport implemented in this build is **in-process native
|
||||
loading** (`libloading`) after `manifest.validate()`, exact target-triple
|
||||
artifact resolution, sha256 verification, ABI negotiation and export
|
||||
metadata cross-checking (`crates/marqdo-plugin-abi/src/loader.rs`).
|
||||
* Loading requires **three** independent things:
|
||||
1. `--plugin <manifest-or-dir>` names the plugin;
|
||||
2. `--trust-native-plugin` is an explicit unsafe trust decision;
|
||||
3. the manifest package name is granted with `--allow-plugin <name>` (checked
|
||||
again on every invocation).
|
||||
Without trust, `StdHost::load_plugin` returns `permission.denied` with the
|
||||
hint that no isolated worker transport exists. There is no isolated worker,
|
||||
Wasm component or out-of-process plugin path in this build.
|
||||
* A native plugin runs in the host address space. It can read host memory,
|
||||
call arbitrary syscalls, bypass the capability model entirely and crash or
|
||||
corrupt the host process. **A C ABI is not a sandbox.** `--trust-native-plugin`
|
||||
must be treated as equivalent to running the plugin's code as the host user.
|
||||
* Plugin call sites are written as `<namespace>.<export>(...)` in a function
|
||||
body (for example `solver.add(a=20, b=22)`). The namespace is derived from
|
||||
the **first** manifest export prefix. Calls are made through
|
||||
`HostOp::PluginInvoke` with a typed `wire_version` 1 request; the plugin's
|
||||
typed response becomes a Marqdo value (`Unit` when absent). Named arguments
|
||||
are required in this build; positional plugin arguments are `runtime.type`.
|
||||
* Plugin errors map to structured runtime errors: ABI → `plugin.abi_mismatch`,
|
||||
capability → `permission.denied`, wire/value → `runtime.type`, unsupported →
|
||||
`kernel.unavailable`, everything else → `plugin.failure`. An unloaded
|
||||
namespace is `symbol.unknown`.
|
||||
* The plugin's host-capability requests are denied by default: the CLI loads
|
||||
plugins with `LoadOptions::default()`, i.e. **no capability handler**, so
|
||||
every `request_capability` call fails. The `requires:` list in `plugin.yaml`
|
||||
is only a declaration.
|
||||
* `marqdo plugin inspect` never loads code: it validates the manifest and
|
||||
verifies the artifact hash only.
|
||||
* Installation, inspection, loading and authorization are four separate
|
||||
decisions. Copying a plugin into the project or declaring it in
|
||||
`marqdo.yaml` grants nothing.
|
||||
|
||||
Known limitations: single namespace per plugin (derived from the first export),
|
||||
no hot unload/reload, no plugin capability bridge from this CLI, native crash
|
||||
is a host crash, `plugin.invoke` cannot be used in `pure`/`strict` mode.
|
||||
|
||||
## 7. `check` and `inspect` are side-effect free
|
||||
|
||||
Neither command executes project code. Specifically:
|
||||
|
||||
* `check` parses, indexes, maps to IR, runs static call validation and reports
|
||||
diagnostics. It never calls a function, never spawns a foreign kernel, never
|
||||
loads a native plugin and never writes files.
|
||||
* `inspect` with `--ast`, `--units`, `--signature`, `--ir`, `--refs` or
|
||||
`--platform` does the same static work and prints metadata. `--refs` may
|
||||
**read** other project source files on demand to resolve link targets, but
|
||||
only parses/analyzes them; nothing is executed or written.
|
||||
* `plugin inspect` reads `plugin.yaml`, validates it, resolves the artifact for
|
||||
the host target and computes/compares its sha256. It does not `dlopen`,
|
||||
instantiate or call the plugin (`"loaded": false` in JSON output).
|
||||
* `run`/`call` first perform the same static load; if any diagnostic has error
|
||||
severity, execution is refused before the host or any kernel is touched.
|
||||
* `fmt` only normalizes line endings/trailing newlines and writes the document
|
||||
when not in `--check` mode; it does not execute anything.
|
||||
|
||||
## 8. Open security items
|
||||
|
||||
1. Strict mode: no OS isolation adapter (P4); worker processes report
|
||||
`false`.
|
||||
2. `console` capability is not enforced; print is always allowed.
|
||||
3. Network capability exists in the model but no host operation uses it; there
|
||||
is no `--allow-net` flag.
|
||||
4. Write-path symlink TOCTOU (see §2.1).
|
||||
5. Plugin worker isolation and a plugin capability bridge are unimplemented;
|
||||
only trusted in-process native plugins are possible.
|
||||
6. `plugin_handle` wire values are structurally reconstructable (see
|
||||
`value-wire.md` §4.1); no unforgeable capability token.
|
||||
7. Budgets do not cover all allocation paths (for example list/table growth
|
||||
inside the interpreter is bounded only by fuel and memory).
|
||||
8. `--json` run output includes the full program stdout string; that is the
|
||||
program's own output, not a host secret, but it is embedded in the JSON
|
||||
document consumers must parse.
|
||||
@@ -0,0 +1,161 @@
|
||||
# Marqdo Value Wire Format v1
|
||||
|
||||
Status: **frozen for M0.5**. Changes require an ADR, a `wire_version` bump and
|
||||
cross-ABI compatibility tests.
|
||||
|
||||
Canonical implementation: `crates/marqdo-value/src/wire.rs`
|
||||
(`to_wire`, `from_wire`, `from_wire_with_limits`, `WireLimits`, `WireError`).
|
||||
The plugin envelope that *carries* these values is defined in
|
||||
`crates/marqdo-plugin-abi/src/wire.rs` and documented in
|
||||
[`plugin-abi.md`](plugin-abi.md); the foreign-kernel stdin envelope is
|
||||
documented in [`foreign.md`](foreign.md) and implemented in
|
||||
`crates/marqdo-foreign/src/lib.rs`.
|
||||
|
||||
## 1. Why a typed wire format
|
||||
|
||||
JSON has one numeric type. Marqdo has arbitrary-precision `Int`, IEEE-754
|
||||
`Float` (including `NaN` and both infinities) and 13 distinct runtime value
|
||||
types (`crates/marqdo-value/src/value.rs`). Encoding an `Int` as a JSON number
|
||||
would silently lose digits above 2^53 and would make `Int(1)` and
|
||||
`Float(1.0)` indistinguishable after a round trip. The wire format is
|
||||
therefore **fully type-tagged** and is not plain JSON.
|
||||
|
||||
The rules that follow from this:
|
||||
|
||||
1. Every value is a JSON **object** with a mandatory `"type"` string.
|
||||
2. **Integers never travel as JSON numbers.** `Int` is
|
||||
`{"type":"int","decimal":"<base-10 string>"}`, which preserves every digit,
|
||||
sign and leading-zero-free canonical form.
|
||||
3. `Float` uses a JSON number only for finite values; `NaN`, `Infinity` and
|
||||
`-Infinity` use an explicit `"special"` tag.
|
||||
4. Unknown tags, missing fields, wrong JSON types and invalid payloads are
|
||||
structured decode errors, never silent coercions.
|
||||
|
||||
JSON numbers still appear in two non-value positions: `plugin_handle.id` (a
|
||||
host generation counter) and `span.start` / `span.end` (byte offsets). Neither
|
||||
is a user arithmetic value.
|
||||
|
||||
## 2. Version and entry points
|
||||
|
||||
* `WIRE_VERSION: u32 = 1` (`marqdo_value::WIRE_VERSION`).
|
||||
* `to_wire(&Value) -> serde_json::Value` encodes.
|
||||
* `from_wire(&Json) -> Result<Value, WireError>` decodes with
|
||||
`WireLimits::default()`.
|
||||
* `from_wire_with_limits(&Json, WireLimits) -> Result<Value, WireError>`
|
||||
decodes with explicit limits.
|
||||
* Decoding is recursive and never panics on untrusted input; every failure is
|
||||
a `WireError`:
|
||||
* `UnknownType(String)` — missing/non-string/unknown `"type"`;
|
||||
* `MissingField(&'static str)` — required field absent;
|
||||
* `Invalid(String)` — wrong JSON type, invalid decimal, bad table shape,
|
||||
unknown float `special`, non-object value;
|
||||
* `LimitExceeded(&'static str)` — nesting depth, list/map length, string or
|
||||
map-key length.
|
||||
|
||||
`WireError` implements `Display` but has no stable numeric code of its own.
|
||||
Boundary crates wrap it in their own stable codes: the plugin layer uses
|
||||
`invalid_argument_wire` / `invalid_result_wire`
|
||||
(`crates/marqdo-plugin-abi/src/wire.rs`), and the foreign layer maps it to
|
||||
`Protocol` (`crates/marqdo-foreign/src/lib.rs`).
|
||||
|
||||
## 3. Value encodings
|
||||
|
||||
The table is normative and mirrors `encode`/`decode` in
|
||||
`crates/marqdo-value/src/wire.rs`.
|
||||
|
||||
| Marqdo type | Wire JSON | Notes |
|
||||
|---|---|---|
|
||||
| `Unit` | `{"type":"unit"}` | No result; distinct from `None`. |
|
||||
| `None` | `{"type":"none"}` | Explicit empty value. |
|
||||
| `Bool` | `{"type":"bool","value":true}` | `value` must be a JSON boolean. |
|
||||
| `Int` | `{"type":"int","decimal":"-123..."}` | Decimal string; decoded with `num_bigint::BigInt::from_str`. No `+`, no separators, no hex. `-0` decodes to `0` because `BigInt` has no signed zero. |
|
||||
| `Float` (finite) | `{"type":"float","value":1.5}` | JSON number decoded with `as_f64()`. `-0.0` round-trips as a JSON negative-zero number and decodes to `Float(-0.0)`. |
|
||||
| `Float` (`NaN`) | `{"type":"float","special":"nan"}` | Never a JSON number. |
|
||||
| `Float` (`+∞`) | `{"type":"float","special":"infinity"}` | |
|
||||
| `Float` (`−∞`) | `{"type":"float","special":"-infinity"}` | |
|
||||
| `Text` | `{"type":"text","value":"..."}` | UTF-8 JSON string. |
|
||||
| `List` | `{"type":"list","items":[<value>, ...]}` | Order preserved. |
|
||||
| `Map` | `{"type":"map","entries":{"key":<value>, ...}}` | Decoding rebuilds a `BTreeMap`, so key order is canonical (sorted) after a round trip. Duplicate keys cannot occur in a JSON object. |
|
||||
| `Table` | `{"type":"table","header":["a","b"],"rows":[[<value>,<value>], ...]}` | Header cells must be JSON strings; every row must have exactly `header.len()` cells or decoding fails. |
|
||||
| `Formula` | `{"type":"formula","latex":"...","display":false}` plus optional `"unit"` (string) and `"span":{"start":N,"end":M}` | `display` defaults to `false` on decode when absent. The formula is inert data; this encoding never evaluates it. |
|
||||
| `Code` | `{"type":"code","language":"python","source":"...","content_hash":"<sha256 hex>"}` plus optional `"unit"` and `"span"` | `language` and `content_hash` default to `""` on decode when absent; `source` is required. |
|
||||
| `FunctionRef` | `{"type":"function_ref","module":"index.mq.md","name":"main"}` | Both fields required strings. |
|
||||
| `PluginHandle` | `{"type":"plugin_handle","plugin":"demo.solver","export":"solver.add","id":1}` | `id` must be a non-negative JSON integer (`as_u64`). |
|
||||
|
||||
Optional fields (`unit`, `span`, `display`, `language`, `content_hash`) are
|
||||
emitted only when present on the value. Encoders always emit `display` for
|
||||
`Formula` and `language`/`content_hash`/`source` for `Code`.
|
||||
|
||||
### 3.1 Round-trip guarantees
|
||||
|
||||
For the values covered by `wire::tests::scalar_roundtrip`,
|
||||
`from_wire(to_wire(v)) == v`. Exact guarantees:
|
||||
|
||||
* all `Int` digits survive (`big_integers_keep_all_digits`);
|
||||
* `NaN`/`±Infinity` survive with their sign/tag (`special_floats_are_tagged`);
|
||||
* `Unit` and `None` never collapse into each other;
|
||||
* `Map` iteration order after decoding is sorted, which is the canonical order
|
||||
used by `Value::display`/`repr`.
|
||||
|
||||
Finite `Float` values are encoded as JSON numbers. The workspace enables
|
||||
`serde_json`'s `float_roundtrip` feature, so a finite binary64 value survives an
|
||||
encode/decode cycle exactly (verified by the Python `math.pi * 9` semantic
|
||||
fixture: `28.274333882308138` stays `28.274333882308138`). `NaN`/infinities are
|
||||
tagged and exact. Use `Text` when a decimal string must be exchanged verbatim
|
||||
rather than as a binary64 float.
|
||||
|
||||
## 4. Limits
|
||||
|
||||
`WireLimits::default()` (`crates/marqdo-value/src/wire.rs`):
|
||||
|
||||
| Limit | Default | Applies to |
|
||||
|---|---|---|
|
||||
| `max_depth` | 64 | Nesting of *values*: the top-level object is depth 0, every child decoded through `list.items`, `map.entries`, `table.rows` or `table.header` cells is depth+1. A value at depth > 64 is rejected with `LimitExceeded("nesting depth")`. |
|
||||
| `max_items` | 1,000,000 | `list.items` length and `map.entries` key count. |
|
||||
| `max_string_bytes` | 16 MiB | `text.value` byte length and map key byte length. |
|
||||
|
||||
### 4.1 Known gaps in the current decoder
|
||||
|
||||
The following are **not** bounded by `WireLimits` in
|
||||
`crates/marqdo-value/src/wire.rs` today and must be treated as open items
|
||||
(they are also bounded indirectly at the ABI layer by envelope size limits):
|
||||
|
||||
* `table.header` length, `table.rows` length and the number of columns are not
|
||||
checked against `max_items` (only row/header width equality is checked);
|
||||
* table header cell strings are not checked against `max_string_bytes`;
|
||||
* `formula.latex`, `code.source`, `function_ref.module`/`name`,
|
||||
`plugin_handle.plugin`/`export` are not checked against
|
||||
`max_string_bytes`;
|
||||
* a `plugin_handle` decoded from wire data is a structurally reconstructed
|
||||
`Value::PluginHandle`; this build does not carry an unforgeable capability
|
||||
token. Host code must therefore treat a plugin handle as opaque *intent* and
|
||||
re-authorize any operation, not as proof of prior authorization. The header
|
||||
comment "cannot be forged from wire data" is aspirational for this point and
|
||||
should be fixed or reworded in a future ADR.
|
||||
* JSON itself may contain duplicate object keys; `serde_json` keeps the last
|
||||
one, so `map.entries` with duplicate keys is not rejected as an error.
|
||||
|
||||
## 5. Where the format is used
|
||||
|
||||
| Boundary | Envelope | Value positions |
|
||||
|---|---|---|
|
||||
| Plugin ABI v1 request/response | `{"wire_version":1,"kind":"request"/"response",...}` (`crates/marqdo-plugin-abi/src/wire.rs`) | `request.args` is an object of name → typed value; `response.value` is one typed value. |
|
||||
| Plugin host capability request/reply | opaque bytes, shape per capability | defined by the capability, not by this file. |
|
||||
| Foreign kernel stdin | `{"wire_version":1,"kind":"request","input":<typed value or null>}` (`crates/marqdo-foreign/src/lib.rs`) | `input` is a `to_wire(value)` result, or JSON `null` when absent. |
|
||||
| Foreign typed return (`foreign.call`) | stdout must be exactly one typed value | decoded with `from_wire` and default limits. |
|
||||
|
||||
The `--json` output of `run`/`call` embeds `"value": to_wire(outcome.value)`
|
||||
(`crates/cli/src/commands.rs`), so CLI JSON consumers see the same typed
|
||||
representation.
|
||||
|
||||
## 6. Compatibility rules
|
||||
|
||||
* `wire_version` is part of the frozen contract. A decoder rejects a different
|
||||
version rather than guessing.
|
||||
* Adding a new `"type"` tag requires a spec change and an ADR; removing or
|
||||
renaming a tag is a breaking change.
|
||||
* The plugin envelope independently carries `wire_version`; do not assume the
|
||||
C ABI major version and `wire_version` are the same number
|
||||
(see `spec/plugin-abi.md` §2).
|
||||
* All payloads are UTF-8 JSON. Endianness is irrelevant for the JSON layer,
|
||||
but the C ABI structs that carry these bytes are target-dependent.
|
||||
Reference in new issue
Block a user