Files
marqdo-cli/spec/document-access.md
cflmy d7dbce68b6
ci/woodpecker/push/main Pipeline failed
ci/woodpecker/pr/main Pipeline was canceled
feat: expose the loaded document snapshot to runtime programs
Implement marqdo-cli#24 (AI-U1): ordinary .mq.md programs can read the
document snapshot that `prepare` already loaded, without a parser, host paths
or a second knowledge body.

- marqdo-host-contract gains the neutral read-only DocumentModule/DocumentUnit
  projection (canonical ids, path/parent/level/mode/anchors, exact section and
  heading spans, source digest, exact source) and two default-unavailable Host
  methods.
- The composition root builds the snapshot from the same load as Analysis
  (entry + explicit import closure only; no extra reads) and attaches it to the
  native host. StdHost only slices the immutable source; it never executes
  project code, loads modules, parses Markdown or touches the network.
- New builtins: document.outline(module?, parent?, limit?) and
  document.read(module, unit, expected_digest?, max_bytes?). Expected states
  (not loaded, unknown unit, stale source, over budget, invalid span) are
  structured result maps with stable document.* codes, so ordinary control flow
  can branch instead of crashing.
- Specification: spec/document-access.md, spec/evaluation.md §8, ADR-0074,
  README/developers/STATUS links and a docs-consistency gate for the single
  current contract.
- Tests: document_access.rs checks byte-exact agreement with check --emit
  Analysis (module/unit ids, digests, spans, cross-module anchor read) and all
  failure codes; the ADR-0064 builtin review table records the two additions.

Binary budgets are explicit (snapshot 128 modules/16 MiB; outline 1024/4096;
read 256 KiB/1 MiB) and over-budget requests report document.too_large rather
than truncating.
2026-11-30 15:42:30 +08:00

127 lines
5.0 KiB
Markdown

# 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": "<exact UTF-8 source slice>"
}
```
`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.