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.
85 lines
4.6 KiB
Markdown
85 lines
4.6 KiB
Markdown
# ADR-0074 — Read-only loaded-document snapshot and two runtime document builtins
|
|
|
|
Status: **Accepted** (2026-11-30).
|
|
Related: [marqdo-cli#24](https://git.cflmy.cn/cflmy/marqdo-cli/issues/24),
|
|
`marqdo-ext-ai#2` (AI-U1), [ADR-0064](0064-new-builtin-hostop-value-and-top-level-command-policy.md),
|
|
[ADR-0069](0069-public-analysis-boundary.md), [ADR-0071](0071-engine-host-contract-boundary.md).
|
|
|
|
## Context
|
|
|
|
The public Analysis revision 3 projection already lets external tools see every
|
|
loaded module, unit identity, anchor and exact UTF-8 span. Ordinary `.mq.md`
|
|
programs have no equivalent runtime access: `ref("#anchor")` returns an inert
|
|
`Formula | Code | FunctionRef`, and there is no way to walk the loaded heading
|
|
tree or read one exact section without copying the CLI parser or reading host
|
|
files privately. The AI extension (AI-U1 / I08: select anchor → read exact
|
|
section → judge → compute → explain) is blocked on this, and any ordinary
|
|
document-navigation program has the same gap.
|
|
|
|
The engine must not read files or parse Markdown. The loader already holds one
|
|
immutable snapshot (`prepare` = the same load used by `check`, `run`, `call`,
|
|
`inspect` and Analysis), so the missing piece is a controlled projection of that
|
|
snapshot through the neutral Host contract.
|
|
|
|
## Decision
|
|
|
|
1. The composition root builds a read-only `DocumentModule` snapshot in the
|
|
`prepare`-derived load: canonical module id, exact source text, lowercase
|
|
SHA-256 source digest, and units in document order with canonical unit id,
|
|
path, parent id, level, effective mode, anchors, complete section span and
|
|
heading span. Only modules already loaded for this run are included (entry
|
|
plus explicit import closure); nothing is read from disk again.
|
|
2. `marqdo-host-contract` gains the neutral snapshot types and two default-
|
|
unavailable Host methods:
|
|
`document_outline(module?, parent?, limit)` and
|
|
`document_read(module, unit, expected_digest?, max_bytes)`.
|
|
The native host serves them from the snapshot; the embedded/test host may
|
|
leave them unavailable.
|
|
3. Two pure builtins expose them to programs: `document.outline` (all optional
|
|
`module`, `parent`, `limit`) and `document.read` (`module`, `unit`, optional
|
|
`expected_digest`, `max_bytes`). Both return structured result maps
|
|
(`{ok: true, ...}` / `{ok: false, code, message}`) so callers can branch on
|
|
stale sources, unknown units or budget failures instead of crashing.
|
|
4. Specification: `spec/document-access.md`; builtin table in
|
|
`spec/evaluation.md` §8. Analysis is unchanged; the snapshot and the
|
|
Analysis projection are produced from the same load and must agree on
|
|
module/unit ids, source digests and byte spans (contract test).
|
|
|
|
## Boundaries
|
|
|
|
- The engine continues to depend only on `marqdo-host-contract`; it never sees
|
|
the loader, filesystem, Markdown AST or host paths.
|
|
- The host only slices the immutable source by the frontend's exact section
|
|
span; it does not parse, normalise, execute `@knowledge`, expand links, or
|
|
start workers/models.
|
|
- No dynamic import, no extra files, no absolute paths, no second knowledge
|
|
body: the snapshot exposes only already-loaded modules, and the returned text
|
|
is the exact source slice.
|
|
- Budgets are explicit and bounded: outline limit (default 1024, max 4096),
|
|
read `max_bytes` (default 256 KiB, max 1 MiB), snapshot module count (128) and
|
|
total source bytes (16 MiB). Over-budget requests/installations report
|
|
`document.too_large`, never silent truncation.
|
|
|
|
## Rejected alternatives
|
|
|
|
- **Exposing host paths or a file-read builtin**: would let programs read
|
|
arbitrary files and bypass the package/import authority model.
|
|
- **A CLI Tool subprocess for reading**: adds a process boundary and a second
|
|
protocol for an in-process, already-loaded fact.
|
|
- **Copying the Markdown/unit index into the runtime**: duplicates the parser
|
|
and violates ADR-0071; the frontend already owns the authoritative index.
|
|
- **Returning runtime errors for expected states**: stale/unknown/too-large are
|
|
expected data states in document workflows; structured result maps let
|
|
ordinary control flow decide.
|
|
|
|
## Consequences
|
|
|
|
- AI-U1 can read exact sections from the same snapshot as Analysis; the AI
|
|
extension keeps its context boundary and does not need a second index.
|
|
- Ordinary programs can implement catalog/read/navigation flows with the same
|
|
bytes that `check --emit` reports.
|
|
- A new public builtin and Host surface exist; they are read-only and do not
|
|
add capabilities, grants, network access or execution paths.
|
|
- Future changes to unit identity/span semantics remain CLI-owned and must keep
|
|
this contract in lockstep.
|