feat: runtime document outline/read builtins (#24) #29

Merged
cflmy merged 2 commits from codex/issue-24-document-runtime-read into master 2026-11-30 15:52:09 +08:00
Owner

概述

实现 marqdo-cli#24(AI-U1):普通 .mq.md 程序可通过 document.outline / document.read 读取 prepare 已加载的文稿快照(精确 unit、anchor、UTF-8 byte span、source digest 与原文),不复制 Markdown 解析器、不读宿主文件路径、不执行文稿;预期失败是结构化 result map,不是崩溃。

设计

  • marqdo-host-contract:新增只读 DocumentModule/DocumentUnit 快照类型 + 两个默认不可用 Host 方法(document_outline/document_read)。引擎仍只依赖中立 contract(ADR-0071)。
  • crates/cli/src/document_snapshot.rs:从与 Analysis 相同的 prepare 加载构建快照(入口 + 显式导入闭包),携带 canonical module/unit id、path/parent/level/mode/anchors、完整 section span、heading span、source digest、exact source。
  • 原生宿主只按 frontend 的 section span 做 byte slice;不解析、不执行、不加载模块、不联网。
  • 新 builtins:document.outline(module?, parent?, limit?)、document.read(module, unit, expected_digest?, max_bytes?);结果 {ok:true,...} 或稳定失败码 document.not_loaded / document.unknown_unit / document.stale_source / document.too_large / document.invalid_request。
  • 契约:spec/document-access.md、spec/evaluation.md §8、ADR-0074;README/developers/STATUS 链接;docs_consistency 单一契约门禁;builtin 评审表更新。
  • 预算显式:快照 ≤128 模块/16 MiB;outline 默认 1024/最大 4096;read 默认 256 KiB/最大 1 MiB;超限返回 document.too_large 不静默裁剪。

验证(真实,Linux x86_64 / glibc 2.43;Bundle 0.3.0)

cargo fmt --all -- --check                                       PASS
cargo clippy --workspace --all-targets -- -D warnings             PASS
MARQDO_TEST_STDLIB_BUNDLE=<0.3.0> cargo test --workspace --locked  641 passed, 0 failed, 2 ignored
cargo test -p marqdo-cli --test document_access                    2 passed
cargo test -p marqdo-cli --test docs_consistency                   10 passed
check_dependency_boundaries / check_engine_boundary / check_plugin_sdk  PASS
independent_build                                                 PASS

document_access.rs 与 check --emit Analysis 3 逐字节比对:module/unit id、source_digest、section span、跨模块 anchor 读取;负例覆盖 not_loaded/unknown_unit/stale_source/too_large;同名标题 anchor(说明/说明-2)可区分。证据:docs/evidence/2026-11-30-cli-document-runtime-access.md。

边界 / 未验证

  • check/inspect 不执行,故不暴露读取;strict 子进程按同一 prepare 重建快照。
  • 未改 Analysis/Observation/Tool 协议;未新增文件/网络/执行能力;未创建 release。
  • Windows/macOS 原生 lane、strict VM 仍 UNVERIFIED。
  • ext-ai 的 AI-U1 消费闭环(选择锚点→精确读取→判断→计算)待本 CLI 合并后,在 ext-ai 侧用固定 CLI SHA 验证。

refs #24 #13

## 概述 实现 `marqdo-cli#24`(AI-U1):普通 `.mq.md` 程序可通过 `document.outline` / `document.read` 读取 `prepare` 已加载的文稿快照(精确 unit、anchor、UTF-8 byte span、source digest 与原文),不复制 Markdown 解析器、不读宿主文件路径、不执行文稿;预期失败是结构化 result map,不是崩溃。 ## 设计 - `marqdo-host-contract`:新增只读 `DocumentModule`/`DocumentUnit` 快照类型 + 两个默认不可用 Host 方法(`document_outline`/`document_read`)。引擎仍只依赖中立 contract(ADR-0071)。 - `crates/cli/src/document_snapshot.rs`:从与 Analysis 相同的 `prepare` 加载构建快照(入口 + 显式导入闭包),携带 canonical module/unit id、path/parent/level/mode/anchors、完整 section span、heading span、source digest、exact source。 - 原生宿主只按 frontend 的 section span 做 byte slice;不解析、不执行、不加载模块、不联网。 - 新 builtins:`document.outline(module?, parent?, limit?)`、`document.read(module, unit, expected_digest?, max_bytes?)`;结果 `{ok:true,...}` 或稳定失败码 `document.not_loaded` / `document.unknown_unit` / `document.stale_source` / `document.too_large` / `document.invalid_request`。 - 契约:`spec/document-access.md`、`spec/evaluation.md` §8、ADR-0074;README/developers/STATUS 链接;docs_consistency 单一契约门禁;builtin 评审表更新。 - 预算显式:快照 ≤128 模块/16 MiB;outline 默认 1024/最大 4096;read 默认 256 KiB/最大 1 MiB;超限返回 `document.too_large` 不静默裁剪。 ## 验证(真实,Linux x86_64 / glibc 2.43;Bundle 0.3.0) ```text cargo fmt --all -- --check PASS cargo clippy --workspace --all-targets -- -D warnings PASS MARQDO_TEST_STDLIB_BUNDLE=<0.3.0> cargo test --workspace --locked 641 passed, 0 failed, 2 ignored cargo test -p marqdo-cli --test document_access 2 passed cargo test -p marqdo-cli --test docs_consistency 10 passed check_dependency_boundaries / check_engine_boundary / check_plugin_sdk PASS independent_build PASS ``` `document_access.rs` 与 `check --emit` Analysis 3 逐字节比对:module/unit id、source_digest、section span、跨模块 anchor 读取;负例覆盖 not_loaded/unknown_unit/stale_source/too_large;同名标题 anchor(`说明`/`说明-2`)可区分。证据:`docs/evidence/2026-11-30-cli-document-runtime-access.md`。 ## 边界 / 未验证 - `check`/`inspect` 不执行,故不暴露读取;strict 子进程按同一 prepare 重建快照。 - 未改 Analysis/Observation/Tool 协议;未新增文件/网络/执行能力;未创建 release。 - Windows/macOS 原生 lane、strict VM 仍 UNVERIFIED。 - ext-ai 的 AI-U1 消费闭环(选择锚点→精确读取→判断→计算)待本 CLI 合并后,在 ext-ai 侧用固定 CLI SHA 验证。 refs #24 #13
cflmy added 1 commit 2026-11-30 15:42:43 +08:00
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.
cflmy added 1 commit 2026-11-30 15:45:08 +08:00
The evidence consistency gate requires a commit/security SHA in every
docs/evidence Markdown file; record the Bundle-0.3.0 baseline commit so the
document-access evidence passes the same gate as the other records.
cflmy merged commit afea4f9474 into master 2026-11-30 15:52:09 +08:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: cflmy/marqdo-cli#29