Files
marqdo-cli/crates/cli/tests/docs_consistency.rs
T
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

430 lines
14 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Documentation consistency gate (review RFC-CM P0).
//!
//! The current architecture lives in one file, the status file must link it,
//! relative links must resolve, and a removed "running status" claim must not
//! come back. This is a source check, not a release proof.
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}
fn read(path: &Path) -> String {
std::fs::read_to_string(path).unwrap_or_else(|error| {
panic!("read {}: {error}", path.display());
})
}
#[test]
fn current_architecture_has_one_overview_and_four_state_dimensions() {
let current = read(&repo_root().join("docs/architecture/CURRENT.md"));
for heading in [
"## 1. Four state dimensions",
"## 2. Responsibility layers",
"## 3. Dependency direction and public contracts",
"## 4. Extension path classification",
"## 5. Gap and closure backlog",
"## 6. Documentation governance",
] {
assert!(current.contains(heading), "CURRENT.md is missing {heading}");
}
for dimension in [
"Implemented",
"Tested locally",
"Bundled stdlib released",
"Multi-platform verified",
] {
assert!(
current.contains(dimension),
"CURRENT.md is missing the `{dimension}` state dimension"
);
}
let baseline = current
.lines()
.find(|line| line.starts_with("- Baseline:"))
.expect("CURRENT.md has no Baseline line");
let sha = baseline
.split('`')
.nth(1)
.expect("Baseline line has no backtick-quoted SHA");
assert_eq!(sha.len(), 40, "Baseline SHA is not 40 hex digits: {sha}");
assert!(
sha.chars().all(|c| c.is_ascii_hexdigit()),
"Baseline SHA is not hex: {sha}"
);
}
#[test]
fn index_and_status_point_at_the_current_overview_and_history() {
let root = repo_root();
let index = read(&root.join("docs/architecture/README.md"));
for target in ["CURRENT.md", "../STATUS.md", "../history/README.md"] {
assert!(
index.contains(target),
"architecture index does not link {target}"
);
}
// The index is an index, not a second running-status page.
assert!(
!index.contains("P5 providers") && !index.contains("P5 Provider"),
"architecture index must not duplicate the current P5 status"
);
let status = read(&root.join("docs/STATUS.md"));
assert!(
status.contains("architecture/CURRENT.md"),
"STATUS.md does not link the current overview"
);
assert!(
!status.contains("is still `c9f01aa8"),
"STATUS.md still claims the stale `c9f01aa8` remote baseline"
);
}
#[test]
fn current_relative_markdown_links_resolve() {
let root = repo_root();
for relative in [
"docs/architecture/CURRENT.md",
"docs/architecture/README.md",
"docs/STATUS.md",
"docs/history/README.md",
] {
let path = root.join(relative);
let text = read(&path);
let mut checked = BTreeSet::new();
for target in markdown_targets(&text) {
if target.starts_with("http://")
|| target.starts_with("https://")
|| target.starts_with('#')
{
continue;
}
let file = target.split('#').next().unwrap();
if file.is_empty() || !checked.insert(file.to_string()) {
continue;
}
let resolved = path.parent().unwrap().join(file);
assert!(
resolved.exists(),
"{} links missing `{target}` (resolved {})",
path.display(),
resolved.display()
);
}
}
}
/// CI/security status drift is a defect: the current architecture table must
/// not simultaneously claim a network endpoint is implemented and absent, and
/// the CI doc must not claim the GitHub-hosted Windows/macOS matrix does not
/// exist (it does; Woodpecker has no such agent).
#[test]
fn ci_and_architecture_status_do_not_contradict_the_workflows() {
let root = repo_root();
let current = read(&root.join("docs/architecture/CURRENT.md"));
assert!(
!current.contains("no DNS/TLS/server socket/UDP"),
"CURRENT.md repeats the stale TCP row that contradicts the network endpoint row"
);
assert!(
!current.contains("file metadata / directory / rename / remove / mkdir"),
"CURRENT.md repeats the stale not-implemented filesystem row"
);
let ci = read(&root.join("docs/ci.md"));
assert!(
!ci.contains("no runners are configured"),
"docs/ci.md denies the existing GitHub-hosted Windows/macOS matrix"
);
assert!(
ci.contains("windows-latest") && ci.contains("macos-latest"),
"docs/ci.md must name the native matrix it cannot replace with Woodpecker"
);
let historical = read(&root.join("docs/NEXT_RELEASE_CI_CONVERGENCE.md"));
assert!(
historical.contains("Historical / Superseded baseline"),
"the old convergence plan must keep its historical banner"
);
}
#[test]
fn evidence_files_are_sha_pinned() {
let evidence = repo_root().join("docs/evidence");
for entry in std::fs::read_dir(&evidence).expect("read evidence dir") {
let path = entry.unwrap().path();
if path.extension().and_then(|ext| ext.to_str()) != Some("md") {
continue;
}
let text = read(&path);
assert!(
has_hex_sha(&text),
"{} has no 7–40 hex commit/security SHA",
path.display()
);
}
}
fn markdown_targets(text: &str) -> Vec<String> {
let bytes = text.as_bytes();
let mut out = Vec::new();
let mut i = 0;
while i + 1 < bytes.len() {
if bytes[i] == b']' && bytes[i + 1] == b'(' {
let start = i + 2;
let mut end = start;
while end < bytes.len() && bytes[end] != b')' && bytes[end] != b'\n' {
end += 1;
}
if end < bytes.len() && bytes[end] == b')' {
out.push(text[start..end].to_string());
i = end;
}
}
i += 1;
}
out
}
fn has_hex_sha(text: &str) -> bool {
for word in text.split(|c: char| !c.is_ascii_hexdigit()) {
if (7..=40).contains(&word.len()) {
return true;
}
}
false
}
/// CI is a consumer of the public protocols: its configuration must exercise
/// only the current contracts. A retired name in a pipeline is a latent
/// failure and implies a compatibility path was kept somewhere.
#[test]
fn ci_configuration_uses_only_current_protocols() {
let root = repo_root();
for relative in [
".woodpecker/main.yml",
".github/workflows/ci.yml",
"docs/ci.md",
] {
let text = read(&root.join(relative));
for retired in [
"--event-stream",
"--trace-file",
"execution-events",
"execution_events",
"analysis_v3_consumer",
"trace_consumer",
"analysis-v3",
] {
assert!(
!text.contains(retired),
"{relative} still references retired `{retired}`"
);
}
}
}
/// The current contract documents must describe the interface that exists.
/// A retired option or a path to a deleted spec in `spec/` or the current
/// developer guides is a release-record defect: consumers would implement a
/// contract the binary does not accept. Historical ADRs, evidence and
/// `docs/history/` keep their original text and are intentionally excluded.
#[test]
fn current_contract_documents_do_not_reference_retired_interfaces() {
let root = repo_root();
let mut files = vec![
"COMPATIBILITY.md".to_string(),
"README.md".to_string(),
"docs/ci.md".to_string(),
"docs/architecture/CURRENT.md".to_string(),
];
for directory in ["spec", "docs/developers", "examples"] {
let base = root.join(directory);
let mut stack = vec![base];
while let Some(path) = stack.pop() {
for entry in std::fs::read_dir(&path).expect("read contract docs") {
let path = entry.unwrap().path();
if path.is_dir() {
stack.push(path);
} else if path.extension().and_then(|ext| ext.to_str()) == Some("md") {
files.push(
path.strip_prefix(&root)
.unwrap()
.to_string_lossy()
.to_string(),
);
}
}
}
}
let retired = [
"--trace-file",
"--event-stream",
"EffectTrace",
"crates/marqdo-host/src/trace.rs",
"spec/trace-v1.md",
"spec/trace-v2.md",
"spec/execution-events-v1.md",
"spec/analysis-v1.md",
"spec/analysis-v2.md",
"spec/tool-v1.md",
"worker_frame_invalid for an over-limit",
];
for relative in files {
let text = read(&root.join(&relative));
for (number, line) in text.lines().enumerate() {
for token in retired {
if !line.contains(token) {
continue;
}
// A sentence that explicitly marks the interface as retired or
// removed is documentation of the migration, not a claim that
// it is available.
let explained = line.contains("retired")
|| line.contains("removed")
|| line.contains("no longer");
assert!(
explained,
"{relative}:{} still documents retired `{token}`",
number + 1
);
}
}
}
}
#[test]
fn environment_report_contract_is_documented_once() {
let root = repo_root();
let spec = read(&root.join("spec/environment.md"));
for needle in [
"marqdo.environment",
"schema_version: 1",
"inspect --environment",
"MARQDO_BUILD_SOURCE_SHA",
"env.stdlib_digest_mismatch",
"never project",
] {
assert!(
spec.contains(needle),
"spec/environment.md is missing `{needle}`"
);
}
assert!(
!root.join("spec/environment-v1.md").exists(),
"the environment report must have one current specification"
);
let readme = read(&root.join("README.md"));
assert!(
readme.contains("spec/environment.md"),
"README.md does not link the environment report spec"
);
let releasing = read(&root.join("docs/releasing.md"));
assert!(
releasing.contains("inspect --environment")
&& releasing.contains("MARQDO_BUILD_SOURCE_SHA"),
"docs/releasing.md does not document the identity binding"
);
let compatibility = read(&root.join("COMPATIBILITY.md"));
assert!(
compatibility.contains("Environment report"),
"COMPATIBILITY.md does not list the environment report contract"
);
}
#[test]
fn document_structure_contract_has_one_overview_and_author_entry_points() {
let root = repo_root();
let spec = read(&root.join("spec/document-structure.md"));
for needle in [
"## 1. Document root and heading modes",
"index.mq.md",
"link, import, call",
"Proposed",
] {
assert!(
spec.contains(needle),
"spec/document-structure.md is missing `{needle}`"
);
}
for linked in [
"mapping.md",
"analysis.md",
"references.md",
"package-format.md",
"heading-annotations-v1.md",
] {
assert!(
spec.contains(linked),
"spec/document-structure.md must link the single source {linked}"
);
}
assert!(
!root.join("spec/document-structure-v1.md").exists(),
"the document structure must have one current overview"
);
let readme = read(&root.join("README.md"));
assert!(
readme.contains("spec/document-structure.md"),
"README.md does not link the document structure contract"
);
let developers = read(&root.join("docs/developers/README.md"));
assert!(
developers.contains("document-structure.md"),
"docs/developers/README.md does not point authors at the structure contract"
);
}
#[test]
fn runtime_document_access_contract_is_documented_once() {
let root = repo_root();
let spec = read(&root.join("spec/document-access.md"));
for needle in [
"document.outline",
"document.read",
"document.not_loaded",
"document.unknown_unit",
"document.stale_source",
"document.too_large",
"half-open byte offsets",
] {
assert!(
spec.contains(needle),
"spec/document-access.md is missing `{needle}`"
);
}
assert!(
!root.join("spec/document-access-v1.md").exists(),
"the runtime document access contract must have one current specification"
);
let readme = read(&root.join("README.md"));
assert!(
readme.contains("spec/document-access.md"),
"README.md does not link the runtime document access spec"
);
let evaluation = read(&root.join("spec/evaluation.md"));
for builtin in ["document.outline", "document.read"] {
assert!(
evaluation.contains(builtin),
"spec/evaluation.md §8 is missing {builtin}"
);
}
assert!(
root.join("docs/adr/0074-document-runtime-read.md").exists(),
"the runtime document access decision is not recorded as an ADR"
);
let developers = read(&root.join("docs/developers/README.md"));
assert!(
developers.contains("document-access.md"),
"docs/developers/README.md does not point at the runtime document access spec"
);
}