# Marqdo Runtime Document Access Current public contract for the read-only runtime view of already loaded documents: the `document.outline` and `document.read` builtins. It is implemented by the composition root's immutable snapshot (`crates/cli/src/document_snapshot.rs`), the neutral Host contract (`marqdo-host-contract`) and the native host (ADR-0074). Analysis revision 3 remains the static external projection; this contract lets ordinary `.mq.md` programs read the same snapshot without a parser or host paths. ## 1. Snapshot scope and invariants The snapshot contains exactly the modules `prepare` already loaded for this `run`/`call`: the entry module plus its explicit import closure. `check` and `inspect` never execute and therefore never expose it. * Identity, ordering and spans match the Analysis revision 3 projection from the same load: canonical module id, canonical unit id, `path`, `parent_id`, level, effective mode, anchors, complete section `span`, `heading_span`, module `source_digest` (lowercase hex SHA-256) and the exact `source`. * The section `span` is the heading through the next heading of the same or a higher level, or EOF; the root covers the whole source. Spans are UTF-8 half-open byte offsets into the exact module `source`. * Reading is pure and read-only: no file is opened, no module is loaded dynamically, no Markdown is parsed, no `@knowledge` is executed, no link is expanded, no worker/model/network is started. * The same run is deterministic: the same snapshot returns the same bytes, identities and ordering. * Snapshot budget: at most 128 modules and 16 MiB total source bytes per run. A project over budget keeps running, but document access reports `document.too_large` instead of serving a truncated view. ## 2. `document.outline(module?, parent?, limit?)` All arguments are optional. | Argument | Meaning | |---|---| | `module` | Target loaded module id. Defaults to the calling module. | | `parent` | Only direct children of this unit id. Empty/absent = all units. | | `limit` | Maximum units to return. Default 1024, maximum 4096. | Success result: ```json { "ok": true, "module": "root:demo@0.1.0::index.mq.md", "source_digest": "<64 lowercase hex>", "count": 3, "units": [ { "id": "root:demo@0.1.0::index.mq.md#力学", "path": ["力学"], "parent_id": "root:demo@0.1.0::index.mq.md#", "level": 1, "mode": "knowledge", "anchors": ["力学"], "span": {"start": 0, "end": 120}, "heading_span": {"start": 0, "end": 12} } ] } ``` `units` is in document order (root first). The root has `path: []`, `parent_id: null`, `level: 0`, `heading_span: null` and its `span` covers the whole source. When more units match than `limit`, the result is `{ok: false, code: "document.too_large", ...}` — units are never silently dropped. ## 3. `document.read(module, unit, expected_digest?, max_bytes?)` | Argument | Meaning | |---|---| | `module` | Target loaded module id (required). | | `unit` | Canonical unit id **or** one of its navigation anchors (required). | | `expected_digest` | Optional exact module source digest. Absent/empty = no binding. | | `max_bytes` | Maximum returned text bytes. Default 262144, maximum 1048576. | Success result: ```json { "ok": true, "module": "root:demo@0.1.0::index.mq.md", "unit": "root:demo@0.1.0::index.mq.md#力学/计算力", "anchors": ["计算力"], "source_digest": "<64 lowercase hex>", "span": {"start": 100, "end": 180}, "text": "" } ``` `text` is exactly `source[span.start..span.end]` of the same snapshot; it is not a copy of a second knowledge body or a re-rendered document. Calling programs must treat it as data (never as system instructions). ## 4. Failure codes Failures are result maps (`ok: false`), not execution crashes: | Code | Meaning | |---|---| | `document.not_loaded` | The module is not part of this run's loaded snapshot. | | `document.unknown_unit` | The module has no unit id or anchor matching `unit`. | | `document.stale_source` | `expected_digest` is non-empty and differs from the loaded module digest. | | `document.too_large` | A limit/size (outline units, section bytes, snapshot budget) is exceeded. | | `document.invalid_request` | The selected span is outside the source or not on UTF-8 boundaries (defensive). | Callers can branch on these codes with ordinary `if`/`else`; no exception or second result protocol exists. ## 5. Non-goals * No arbitrary file, directory, network or package-cache access. * No dynamic import or loading of modules outside the prepared closure. * No execution of `@exec` functions from a read; `document.read` never runs project code. * No new Markdown/document syntax, no AI-specific metadata and no second document structure specification. The semantic authority remains `spec/mapping.md`, `spec/analysis.md` and `spec/document-structure.md`. * No guarantee about files changed on disk after preparation: the snapshot is the prepared load, and `expected_digest` reports whether the caller and the snapshot agree.