From 2f18dede8540c82611819e2c24a19db138d5ac14 Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 27 Nov 2026 08:51:40 +0800 Subject: [PATCH] fix: resolve outside_scope design links inside the explicit --root WEB-00's probe and the Preview fixture exposed a real boundary: when an entry's nearest CLI project root is a subdirectory, a component link like `../shared/x.mq.md` is reported as outside_scope without candidate_path even though it is inside the design command's explicit --root. Design now derives the physical candidate from the public reference URL only for that state, resolves it relative to the source module, requires the canonical target to stay inside --root, and still verifies the exact anchor through the target module's Analysis. No private loader, file search or label guessing is introduced; other states keep failing with the candidate diagnostic. Adds a real-CLI contract test and records the rule/limitation in the evidence. Refs cflmy/marqdo-web-design#1, cflmy/marqdo#6 --- docs/evidence/WEB-01-design-core.md | 9 ++++++ src/lib.rs | 46 ++++++++++++++++++++++------- tests/design_contract.rs | 23 ++++++++++++++- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/docs/evidence/WEB-01-design-core.md b/docs/evidence/WEB-01-design-core.md index 159e768..0b7d011 100644 --- a/docs/evidence/WEB-01-design-core.md +++ b/docs/evidence/WEB-01-design-core.md @@ -42,6 +42,15 @@ MARQDO_BIN= scripts/ci.sh - 资源上限与合同建议对齐:文档 100 / 定义 200 / 实例 1000 / 深度 12 / 每行槽 12 / 场景 32 / 转移 128 / 单文件 1 MiB / 总输入 16 MiB;handoff 增加 inputs/events/scenarios/transitions/dependencies/acceptance。 - 测试增至 14 个真实 CLI 契约用例,覆盖上述正例与主要负例(重复 input/event、非法状态、非叶子场景、场景内容指向组件、不可达状态、缺交互、依赖未知/环、空验收、每行槽上限、单文件上限)。 +## 补充候选 3:显式 --root 内的 outside_scope 链接 + +WEB-00 探测发现一个真实边界:CLI 的最近项目根由 entry 父目录决定,组件引用 `../shared/x.mq.md` 时 Analysis 会标 `outside_scope` 且不提供 `candidate_path`,即使目标在设计命令的显式 `--root` 内。Design 的处理(不调用私有 loader、不搜索文件): + +- 仅当 reference `state == outside_scope` 且 URL 是不含 scheme、不以 `/` 开头的相对路径时,用公开 URL 的路径部分相对**来源模块目录**解析; +- 解析结果必须 canonicalize 到显式 `--root` 内,否则 `web.link.outside_root`; +- 精确 anchor 仍由目标模块 Analysis 的 anchors 验证; +- 该规则支撑合同 §2 的 `--root` 设计范围;未发现 CLI 私有字段需求,记录为 Design 解释规则(后续若 CLI Analysis 提供显式 root-relative candidate 可简化)。 + ## 补充候选 2:内容投影、并发一致性与安全边界 - **内容投影**:`ContentBlock`/`ContentLink` 只从 Analysis AST 的结构节点与源码 byte span 投影;raw HTML 一律成为 `[raw HTML as inert text]` 占位,绝不当活动 HTML;组件 `内容` 场景内容均带 block/link span,Preview 不需要自行解析 Markdown。 diff --git a/src/lib.rs b/src/lib.rs index 6789e39..1974028 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -335,6 +335,34 @@ fn parse_initial(value: &str) -> Result { } } +fn design_candidate( + reference: &serde_json::Value, + link: &Link, + module: &Module, +) -> Result { + if let Some(candidate) = reference["candidate_path"].as_str() { + if !candidate.is_empty() { + return Ok(candidate.to_string()); + } + } + // A link inside the explicit design --root can lie outside the CLI's nearest + // project root (for example a component referencing `../shared/x.mq.md`). + // Analysis then reports `outside_scope` without candidate_path; the public + // reference URL plus the explicit design root still determine the physical + // target, so derive it and require the root check afterwards. + if reference["state"].as_str() == Some("outside_scope") { + let raw = link.url.split('#').next().unwrap_or(""); + if !raw.is_empty() && !raw.starts_with('/') && !raw.contains("://") && !raw.contains('\\') { + return Ok(raw.to_string()); + } + } + Err(LoadError::Design(vec![Diagnostic::error( + "web.link.candidate", + "design link must expose a project-relative candidate path", + ) + .at(module.relative.clone(), Some(link.span))])) +} + fn json_span(span: (usize, usize)) -> Span { Span { start: span.0, @@ -395,18 +423,15 @@ impl Loader { link: &Link, ) -> Result<(Module, Unit, String), LoadError> { let reference = find_reference(module, link)?; - let candidate = reference["candidate_path"] - .as_str() - .unwrap_or("") - .to_string(); let fragment = reference["fragment"].as_str().unwrap_or("").to_string(); - if candidate.is_empty() || fragment.is_empty() { + if fragment.is_empty() { return Err(LoadError::Design(vec![Diagnostic::error( "web.link.candidate", - "design link must expose a candidate_path and a fragment", + "design link must expose an exact anchor fragment", ) .at(module.relative.clone(), Some(link.span))])); } + let candidate = design_candidate(reference, link, module)?; if Path::new(&candidate).is_absolute() || link.url.contains("://") { return Err(LoadError::Design(vec![Diagnostic::error( "web.link.absolute", @@ -451,10 +476,6 @@ impl Loader { link: &Link, ) -> Result<(Module, Unit), LoadError> { let reference = find_reference(module, link)?; - let candidate = reference["candidate_path"] - .as_str() - .unwrap_or("") - .to_string(); let fragment = reference["fragment"].as_str().unwrap_or("").to_string(); if fragment.is_empty() { return Err(LoadError::Design(vec![Diagnostic::error( @@ -463,6 +484,11 @@ impl Loader { ) .at(module.relative.clone(), Some(link.span))])); } + let candidate = match design_candidate(reference, link, module) { + Ok(candidate) => candidate, + Err(_) if reference["fragment"].as_str().is_some() => String::new(), + Err(error) => return Err(error), + }; let target_module = if candidate.is_empty() { module.clone() } else { diff --git a/tests/design_contract.rs b/tests/design_contract.rs index 4fbe9f0..d711c83 100644 --- a/tests/design_contract.rs +++ b/tests/design_contract.rs @@ -249,7 +249,7 @@ fn rejects_scope_anchor_and_cycle_errors() { // the deterministic rejection is the candidate diagnostic (never a guess). assert_eq!( design_code(load(&outside.config("index.mq.md", "工作台")).unwrap_err()), - "web.link.candidate" + "web.link.target_missing" ); let missing = basic_project(); @@ -647,3 +647,24 @@ fn detects_a_changed_source_digest_during_one_load() { let error = load(&config).unwrap_err(); assert_eq!(engine_code(error), "web.engine.stale"); } + +#[test] +fn resolves_outside_scope_links_that_stay_inside_the_explicit_root() { + let project = TempProject::new(); + project.write( + "components/a.mq.md", + "# A 组件\n\n## @knowledge 甲\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | b | [乙](../shared/b.mq.md#乙) | auto |\n", + ); + project.write( + "shared/b.mq.md", + "# B 组件\n\n## @knowledge 乙\n\n### 内容\n\n共享正文。\n", + ); + let mut config = project.config("components/a.mq.md", "甲"); + config.root = project.path.clone(); + let design = load(&config).expect("outside_scope link inside --root resolves"); + assert_eq!(design.definitions.len(), 2); + assert_eq!( + design.definitions["components/a.mq.md#甲"].slots[0].target, + "shared/b.mq.md#乙" + ); +} -- 2.54.1