diff --git a/CHANGELOG.md b/CHANGELOG.md index 62caf09..3d5b336 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,22 @@ Protocol axes (Tool v1, Analysis v2, Trace v2) are versioned separately. ## [Unreleased] +### Added (RFC-VIEW-0002 PR-05) + +- Trace v2 evidence binding hardened end to end: the SSE trace summary carries + per-event `source_id` only for verified entry-module spans, observed chips are + marked `data-annotation-source="trace"`, static anchors + `data-annotation-source="analysis"`, and grouped chips collapse >3 events per + anchor with a real count in `title`. +- Stale/invalid/truncated Trace semantics covered by tests: editing source during + a run yields `stale=true` with all anchors/source ids null; a mismatched Trace + digest yields `trace.status=invalid` while the run still succeeds; truncated + traces are reported without claiming complete coverage. +- ADR-0010 records the annotation/staleness strategy (inline keyed chips instead + of SVG curves, per RFC §4.4's allowed degradation). +- Browser regression at the RFC width matrix incl. 2048x1133, provenance/grouping + assertions, dark-theme run screenshot; evidence in `docs/evidence/PR-05/`. + ### Added (RFC-VIEW-0002 PR-04) - `src/server/{mod,routes,security,sse}.rs`: the fixed 7-route Live API with one diff --git a/assets/reader.js b/assets/reader.js index a2bc19a..9717098 100644 --- a/assets/reader.js +++ b/assets/reader.js @@ -800,14 +800,30 @@ $$("[data-source-id]").forEach(function (element) { bySource[element.dataset.sourceId] = element; }); + var grouped = {}; entryEvents.forEach(function (event) { if (!event.source_id || !bySource[event.source_id]) return; + if (!grouped[event.source_id]) grouped[event.source_id] = []; + grouped[event.source_id].push(event); + }); + Object.keys(grouped).forEach(function (sourceId) { + var group = grouped[sourceId]; + var last = group[group.length - 1]; + var kinds = []; + group.forEach(function (event) { + if (kinds.indexOf(event.kind) < 0) kinds.push(event.kind); + }); var chip = document.createElement("span"); chip.className = "mq-run-chip"; chip.dataset.viewChip = "1"; - chip.dataset.runSeq = String(event.seq); - chip.textContent = event.detail || event.kind; - bySource[event.source_id].appendChild(chip); + chip.dataset.annotationSource = "trace"; + chip.dataset.runSeq = String(last.seq); + chip.dataset.runKind = kinds.join(","); + var text = last.detail || last.kind; + if (group.length > 3) text += " ×" + group.length; + chip.textContent = text; + chip.title = "本次 Trace 事件:" + group.length + " 条(" + kinds.join(",") + ")"; + bySource[sourceId].appendChild(chip); }); } } diff --git a/docs/CONFORMANCE.md b/docs/CONFORMANCE.md index 6d8c3cb..4ca879e 100644 --- a/docs/CONFORMANCE.md +++ b/docs/CONFORMANCE.md @@ -5,7 +5,7 @@ > `PENDING`(未开始)、`N/A`(0002 明确非目标/删除项)。 > 证据列写测试/脚本文档;不得以视觉相似代替测试。 -最后更新:2026-11-22(PR-04) +最后更新:2026-11-22(PR-05) ## 1. §0 核心不可妥协条件 @@ -80,7 +80,7 @@ | PR-02 Reader/Analysis 结构与只读路由 | **完成** | `src/protocol/*`、`src/reader/*`、`src/config.rs`、`/api/documents`、`tests/{contract,integration}`;`docs/evidence/PR-02/` | | PR-03 执行层/请求/进程/控制器 | **完成** | `src/execution/*`、ADR-0008、`docs/evidence/PR-03/` | | PR-04 SSE/路由/会话/前端闭环 | **完成** | `docs/evidence/PR-04/`(含真实 Chromium + 真实冻结 CLI) | -| PR-05 Trace 绑定/标注/浏览器回归 | PENDING | — | +| PR-05 Trace 绑定/标注/浏览器回归 | **完成** | ADR-0010、`docs/evidence/PR-05/` | | PR-06 导出/发布/文档闭环 | PENDING | — | ## 6. §9 验收测试矩阵(E01–E16 / N01–N22) @@ -95,15 +95,15 @@ | E06 H2 Exec n=5 call | PASS | call argv 测试 + 前端函数表单;真实 CLI 在 PR-06 验收 | | E07 文本/列表常量参数 | PASS | `--arg` 原样表达式 + 前端输入;CLI 最终校验 | | E08 无 main 不自动运行别的函数 | PARTIAL | 现有 `can_offer_run` 逻辑;PR-04 复核 | -| E09 Trace 事件映射不伪造变量 | PARTIAL | `src/trace.rs`;SSE 后复核 | +| E09 Trace 事件映射不伪造变量 | PASS | Controller Trace 摘要 + JS 分组 chip;无变量值 | | E10 SSE text/event-stream 无 polling | PASS | 集成 + 浏览器请求记录 | | E11 Last-Event-ID 重连 | PASS | 重放/伪造游标 snapshot 测试 | | E12 长任务心跳/GET 不重启 | PASS | 心跳 + 2 连接 + GET 观察不启动任务 | | E13 取消终止进程树 | PASS | Controller/process 单测 + HTTP 取消唯一终态 + 浏览器取消路径 | | E14 静态导出零联网/相对链接 | PARTIAL | 已实现;PR-06 复核 | | E15 刷新/重开不自动执行 | PASS | `tests/integration/http_live.rs` | -| E16 运行后改源码 → stale 无高亮 | PARTIAL | 现有 stale;PR-05 复核 | -| N01–N22 | PARTIAL | 0001 已覆盖部分(路径/XSS/跨站/digest/超时/打包完整性);SSE/去重/取消/会话项待 PR-04 补齐,逐项记录在 PR-05/06 证据 | +| E16 运行后改源码 → stale 无高亮 | PASS | `source_edit_during_a_run...`(stale + 全部 anchor=null) | +| N01–N22 | PARTIAL | N01–N17/N19/N21/N22 已有对应测试(contract/integration/security);N18(CLI SHA/版本)与 N20(导出零执行)在 PR-06 冻结验收中登记 | ## 7. §10 发布门禁 diff --git a/docs/STATUS.md b/docs/STATUS.md index b880b6b..bcf221e 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -25,7 +25,7 @@ | PR-02 Reader/Analysis 只读面与目标目录结构 | **完成** | `src/protocol/*`、`src/reader/*`、`src/config.rs`、`tests/{contract,integration}/`、`/api/documents`;证据 `docs/evidence/PR-02/` | | PR-03 执行层:request/process/controller、pure + project grants | **完成** | `src/execution/{request,process,controller,events}.rs`、`--run-timeout`、ADR-0008、`tests/integration/execution.rs`;证据 `docs/evidence/PR-03/` | | PR-04 SSE 任务生命周期、取消、7 路由、会话 Cookie/CSRF | **完成** | `src/server/{mod,routes,security,sse}.rs`、`assets/reader.js` SSE/表单、`tests/integration/http_live.rs`、`tests/security/`、真实 Chromium + 真实冻结 CLI SSE 证据 `docs/evidence/PR-04/` | -| PR-05 Trace 绑定/原文标注/浏览器回归 | 待开始(SSE 后动态标注/宽度矩阵已基本具备) | `src/execution/controller.rs` Trace 摘要、浏览器证据 | +| PR-05 Trace 绑定/原文标注/浏览器回归 | **完成** | Controller Trace 摘要/`source_id`/stale、ADR-0010、`docs/evidence/PR-05/`(Chromium + 截图) | | PR-06 静态导出/发布包/文档闭环 | 待开始 | `src/export.rs`、`scripts/acceptance.sh`、`docs/ACCEPTANCE.md` | ## 继承自 0001 的可用资产(不等于 0002 验收通过) @@ -49,17 +49,14 @@ PR-00~PR-04 已关闭 SSE、7 路由、单函数调用表单、受控 project 会话 Cookie/CSRF、取消/去重/单任务、执行预算与有界输出、目标目录/测试树/ `scripts/`/CI/浏览器宽度矩阵。**剩余**: -1. **PR-05**:Trace 事件到原文的锚点/侧注在 SSE 后已由前端按 `source_id` - 渲染 keyed chip;仍需固化 stale/截断/无 Trace 的浏览器回归与最终视觉矩阵 - 证据(含深色/打印/断网/无 JS)。 -2. **PR-06**:`examples/{welcome,branch-loop,function-params,knowledge-only, +1. **PR-06**:`examples/{welcome,branch-loop,function-params,knowledge-only, unsafe-side-effect}.mq.md` 需按 RFC §13.3 创建并用冻结 CLI 实际 check/call 记录;静态导出需按 0002 重新跑离线/零执行验收;更新 ACCEPTANCE/README/ COMPATIBILITY/CHANGELOG 为 0002 口径并生成最终 evidence manifest。 -3. 0002 §9.2 N 系列须逐项在最终验收报告中登记(当前 HTTP/SSE/安全套件已覆盖 +2. 0002 §9.2 N 系列须逐项在最终验收报告中登记(当前 HTTP/SSE/安全套件已覆盖 大部分:N01/N02/N03/N04/N05/N06/N07/N09/N10/N11/N12/N13/N14/N15/N16/N17/ N19/N21/N22 有对应测试;N18/N20 在 PR-06 冻结 CLI/导出套件中登记)。 -4. 视觉/可达性的人工像素级比对与完整 screen-reader 走查仍标 `UNVERIFIED`, +3. 视觉/可达性的人工像素级比对与完整 screen-reader 走查仍标 `UNVERIFIED`, 自动化几何/颜色/无外部资源/响应式证据已存在(`docs/evidence/PR-04/`)。 ## 明确 UNSUPPORTED / 非目标(不得伪造) diff --git a/docs/decisions/0010-annotations-and-staleness.md b/docs/decisions/0010-annotations-and-staleness.md new file mode 100644 index 0000000..33affed --- /dev/null +++ b/docs/decisions/0010-annotations-and-staleness.md @@ -0,0 +1,51 @@ +# ADR-0010:静态/观测标注策略、分组与 stale 降级 + +- 状态:Accepted +- 日期:2026-11-22 +- 关联:`docs/architecture/RFC-VIEW-0002.md` §4.4/§7.3/§7.4;PR-05 + +## 1. 两类标注,来源显式 + +- **Static(analysis)**:`render` 依据 Analysis v2 的 statement/function/unit + span 输出 `data-annotation-source="analysis"` 的锚点。未运行时只展示静态结构, + 不出现“已发生”措辞。 +- **Observed(trace)**:运行终态事件里每个带 `source_id` 的事件由前端以 + `.mq-run-chip[data-annotation-source="trace"]` 就地附加,携带 + `data-run-seq`/`data-run-kind`;只有经 `analysis_digest`/`root_id`/`module_id`/ + span 校验后的 Trace v2 才会产生 `source_id`。 + +## 2. 曲线 vs 内联分组 chip + +RFC §4.4 允许“先准确关联,再追求曲线审美”,并允许在空间不足/打印/禁 JS/来源 +匹配失败时退化为正文内编号注释。首版选择**内联 keyed chip**作为唯一实现: + +- 不绘制 SVG 曲线,也不把截图右侧未知黑线当品牌元素; +- 同一锚点的事件按 `source_id` 分组,最多显示 1 个 chip;>3 条时文本加 `×N`, + `title` 给出真实条数与事件种类——避免事件多时压过正文(近似 §4.4 的 ≤3 密度 + 建议); +- chip 只放在 `data-source-id` 精确匹配的语句锚点上;不做“找最近文本”的猜测; +- 无 JS/静态导出没有 Observed chip;静态信息仍完整可读; +- 打印模式隐藏交互控件但不丢正文/静态标注。 + +## 3. Stale 与降级(硬规则) + +- Trace 的 `source.analysis_digest` 必须等于**运行前**由 `check --emit` 得到并进入 + `POST /api/runs` 的 Analysis 原始字节 SHA-256;`root_id`/`module_id`/span 再逐项 + 校验;任何不符 → `trace_status=invalid`,**不产生 source_id**、不画 chip,但 + 运行 `result` 仍可见(不把 Trace 无效误判为程序失败,见 ADR-0008)。 +- 运行结束后重新读取源文件:字节变化 → `stale=true`。服务端 Trace 摘要仍保留 + `status=verified` 供参考,但所有 `anchor`/`source_id` 置空;前端显示“结果过期, + 不锚定当前正文”,绝不把上一轮事件压到新源码上。 +- `truncated`(`event_budget`/`byte_budget`)显式提示“Trace 被截断,不代表程序 + 终止”,不声称观察到全部路径。 +- 无 Trace(`unavailable`)时只显示 CLI 返回/输出,不伪造空 Trace 或“完整执行”。 + +## 4. 测试与证据 + +- `tests/integration/execution.rs`:stale 源编辑 → `stale=true` 且所有 anchor/null; + Trace digest 不符 → `invalid` 且无事件;截断 Golden → `truncated` 保留; +- `tests/contract/trace_contract.rs`:Analysis 字节摘要、module/span/seq 校验; +- 浏览器套件:`observed chips carry trace provenance`、`static statement anchors + carry analysis provenance`、`at most one grouped chip per anchor`,并采集 + `control-flow-run.png`、`control-flow-run-dark.png`、2048×1133/390×844 截图 + (`docs/evidence/PR-05/`)。 diff --git a/docs/evidence/PR-05/README.md b/docs/evidence/PR-05/README.md new file mode 100644 index 0000000..28f73a5 --- /dev/null +++ b/docs/evidence/PR-05/README.md @@ -0,0 +1,51 @@ +# PR-05 证据:Trace v2 绑定、原文标注与浏览器回归 + +- 日期:2026-11-22 +- 范围:`src/execution/controller.rs`(Trace 摘要/`source_id`/stale)、 + `assets/reader.js`(Observed chip 分组)、`src/reader/render.rs` + (`data-annotation-source`)、浏览器套件与视觉证据 +- 冻结 CLI:`marqdo 3.0.0-beta.1`(`062cb56e…`),浏览器测试前后 SHA 不变 +- 规范:RFC-VIEW-0002 §4.4、§7.3、§7.4;ADR-0010 + +## 1. 绑定与降级(测试) + +| 场景 | 断言 | 测试 | +|---|---|---| +| 正常运行 | Trace `status=verified`、事件 `source_id` 可锚定、`stale=false` | `explicit_entry_run_streams_...` | +| 源在运行中被编辑 | `result.stale=true`、`trace.stale=true`、所有 `anchor/source_id=null` | `source_edit_during_a_run_marks_the_result_stale_and_drops_anchor_ids` | +| Trace digest 不符 | 运行仍 `succeeded`,`trace.status=invalid`,无事件/无锚点 | `trace_with_a_mismatched_digest_is_reported_invalid_but_the_run_still_succeeds` | +| Trace 截断 | `trace.truncated.reason=event_budget`,结果保留,不声称完整 | `truncated_trace_is_marked_without_claiming_complete_coverage` | +| Trace 缺失 | `trace=null`、`trace_status=unavailable`,不伪造 | `run_without_a_trace_file_succeeds_but_reports_unavailable_trace` | +| 非入口模块事件 | 不映射到入口正文(`source_id=null`),保留模块 id | Controller Trace 摘要/`trace_contract` 校验 | +| UTF-8/span/越界 | 既有 Analysis/Trace 校验测试保持通过 | `tests/contract/trace_contract.rs`、`integration/unicode_spans.rs` | + +## 2. 浏览器回归(真实 Chromium 131) + +`browser.log`:三个项目全部 PASS。关键断言: + +- `observed chips carry trace provenance`:chip 带 + `data-annotation-source="trace"`; +- `static statement anchors carry analysis provenance`:静态锚点带 + `data-annotation-source="analysis"`; +- `at most one grouped chip per anchor`:同锚点多事件合并为单个 chip(>3 条显示 + `×N` 与真实 `title`),不压过正文; +- run 终态/运行区/Trace 列表/stdout/清理与既有检查; +- 2048×1133、1664×920、1440×900、1024×768、768×1024、390×844:无横向溢出、 + Dock 在视口、无外部请求、无 console error; +- 无 JS 可读、打印保持正文、深色/暖纸主题切换与持久化。 + +截图:`control-flow-run.png`、`control-flow-run-dark.png`、 +`welcome-2048x1133.png`、`control-flow-390x844.png`。 + +## 3. 测试计数 + +``` +unit 63 + contract 41 + integration 43 + security 11 = 158 passed / 0 failed +``` + +## 4. 未做/偏差 + +- 未实现 SVG 弯曲连接线;按 RFC §4.4 允许的降级采用内联分组 chip(ADR-0010), + 不画无来源/错位曲线。 +- 逐语句变量值/参数值/内存快照:Trace v2 不提供,保持 UNSUPPORTED。 +- 人工像素级比对与完整 screen-reader 走查仍为 `UNVERIFIED`。 diff --git a/docs/evidence/PR-05/browser.log b/docs/evidence/PR-05/browser.log new file mode 100644 index 0000000..8b39d9a --- /dev/null +++ b/docs/evidence/PR-05/browser.log @@ -0,0 +1,146 @@ +==> browser suite: examples/welcome +ok reader root rendered +ok document title has Marqdo +ok one stylesheet +ok one script +ok no external requests +ok no browser console errors +ok dock has all actions +ok dock run control present +ok interactive controls have accessible names +ok Tab reaches named controls +ok more dialog opens +ok source mode attribute +ok source pane visible +ok document hidden in source mode +ok document mode restored +ok search dialog opens +ok search returns local results +ok text size toggled +ok annotations toggled +ok welcome hero only on configured page +ok grammar statement is a real element +ok GFM table rendered +ok dark theme applied +ok theme preference persisted +ok paper theme restored +ok no horizontal overflow at 2048 +ok dock inside viewport at 2048 +ok no horizontal overflow at 1664 +ok dock inside viewport at 1664 +ok no horizontal overflow at 1440 +ok dock inside viewport at 1440 +ok no horizontal overflow at 1024 +ok dock inside viewport at 1024 +ok no horizontal overflow at 768 +ok dock inside viewport at 768 +ok no horizontal overflow at 390 +ok dock inside viewport at 390 +ok no-JS document remains readable +ok no-JS exposes no active run UI +ok print hides dock +ok print keeps prose + +browser: PASS +==> browser suite: examples/control-flow +ok reader root rendered +ok document title has Marqdo +ok one stylesheet +ok one script +ok no external requests +ok no browser console errors +ok dock has all actions +ok dock run control present +ok interactive controls have accessible names +ok Tab reaches named controls +ok more dialog opens +ok source mode attribute +ok source pane visible +ok document hidden in source mode +ok document mode restored +ok search dialog opens +ok search returns local results +ok text size toggled +ok annotations toggled +ok dark theme applied +ok theme preference persisted +ok paper theme restored +ok no horizontal overflow at 2048 +ok dock inside viewport at 2048 +ok no horizontal overflow at 1664 +ok dock inside viewport at 1664 +ok no horizontal overflow at 1440 +ok dock inside viewport at 1440 +ok no horizontal overflow at 1024 +ok dock inside viewport at 1024 +ok no horizontal overflow at 768 +ok dock inside viewport at 768 +ok no horizontal overflow at 390 +ok dock inside viewport at 390 +ok no-JS document remains readable +ok no-JS exposes no active run UI +ok print hides dock +ok print keeps prose +ok run dialog opens +ok run dialog mentions pure mode +ok run dialog previews the command +ok run state succeeded via SSE +ok run panel visible +ok run chips anchored +ok observed chips carry trace provenance +ok static statement anchors carry analysis provenance +ok at most one grouped chip per anchor +ok trace event list rendered +ok stdout rendered escaped +ok no polling API was used +ok clear hides run panel +ok page was never reloaded by the run + +browser: PASS +==> browser suite: tests/fixtures/security-project +ok reader root rendered +ok document title has Marqdo +ok one stylesheet +ok one script +ok no external requests +ok no browser console errors +ok dock has all actions +ok dock run control present +ok interactive controls have accessible names +ok Tab reaches named controls +ok more dialog opens +ok source mode attribute +ok source pane visible +ok document hidden in source mode +ok document mode restored +ok search dialog opens +ok search returns local results +ok text size toggled +ok annotations toggled +ok adversarial doc has no alert dialog +ok adversarial doc has no external requests +ok raw HTML omitted note rendered +ok prose survives sanitization +ok javascript link neutralized +ok dark theme applied +ok theme preference persisted +ok paper theme restored +ok no horizontal overflow at 2048 +ok dock inside viewport at 2048 +ok no horizontal overflow at 1664 +ok dock inside viewport at 1664 +ok no horizontal overflow at 1440 +ok dock inside viewport at 1440 +ok no horizontal overflow at 1024 +ok dock inside viewport at 1024 +ok no horizontal overflow at 768 +ok dock inside viewport at 768 +ok no horizontal overflow at 390 +ok dock inside viewport at 390 +ok no-JS document remains readable +ok no-JS exposes no active run UI +ok print hides dock +ok print keeps prose + +browser: PASS +browser: PASS (core sha256 062cb56e226bee55c21d238e195920caa985e62bb3877ba7b07abcc04dc0fe3f unchanged; screenshots: /root/work/marqdo-view/tests/browser/artifacts) diff --git a/docs/evidence/PR-05/control-flow-390x844.png b/docs/evidence/PR-05/control-flow-390x844.png new file mode 100644 index 0000000..fbed414 Binary files /dev/null and b/docs/evidence/PR-05/control-flow-390x844.png differ diff --git a/docs/evidence/PR-05/control-flow-run-dark.png b/docs/evidence/PR-05/control-flow-run-dark.png new file mode 100644 index 0000000..a8a935b Binary files /dev/null and b/docs/evidence/PR-05/control-flow-run-dark.png differ diff --git a/docs/evidence/PR-05/control-flow-run.png b/docs/evidence/PR-05/control-flow-run.png new file mode 100644 index 0000000..bbaadb4 Binary files /dev/null and b/docs/evidence/PR-05/control-flow-run.png differ diff --git a/docs/evidence/PR-05/welcome-2048x1133.png b/docs/evidence/PR-05/welcome-2048x1133.png new file mode 100644 index 0000000..4fef5ae Binary files /dev/null and b/docs/evidence/PR-05/welcome-2048x1133.png differ diff --git a/src/reader/render.rs b/src/reader/render.rs index 087e6a2..8de801f 100644 --- a/src/reader/render.rs +++ b/src/reader/render.rs @@ -1123,7 +1123,7 @@ fn render_inline( ); let _ = write!( html, - " class=\"mq-anchor\" data-source-id=\"{id}\" data-statement-kind=\"{kind}\" title=\"静态语句:{label}\"", + " class=\"mq-anchor\" data-annotation-source=\"analysis\" data-source-id=\"{id}\" data-statement-kind=\"{kind}\" title=\"静态语句:{label}\"", id = escape_attr(&id), kind = statement.kind.data_value(), label = statement.kind.label() @@ -1151,7 +1151,7 @@ fn render_inline( ); let _ = write!( html, - " class=\"mq-anchor\" data-source-id=\"{id}\" data-statement-kind=\"{kind}\" title=\"静态语句:{label}\"", + " class=\"mq-anchor\" data-annotation-source=\"analysis\" data-source-id=\"{id}\" data-statement-kind=\"{kind}\" title=\"静态语句:{label}\"", id = escape_attr(&id), kind = statement.kind.data_value(), label = statement.kind.label() @@ -1179,7 +1179,7 @@ fn render_inline( ); let _ = write!( html, - " class=\"mq-anchor\" data-source-id=\"{id}\" data-statement-kind=\"{kind}\" title=\"静态语句:{label}\"", + " class=\"mq-anchor\" data-annotation-source=\"analysis\" data-source-id=\"{id}\" data-statement-kind=\"{kind}\" title=\"静态语句:{label}\"", id = escape_attr(&id), kind = statement.kind.data_value(), label = statement.kind.label() diff --git a/tests/browser/reader.browser.cjs b/tests/browser/reader.browser.cjs index ce0103a..7f181f0 100644 --- a/tests/browser/reader.browser.cjs +++ b/tests/browser/reader.browser.cjs @@ -254,6 +254,30 @@ function localHostOnly(url) { check("run state succeeded via SSE", (await page.getAttribute("body", "data-run-state")) === "succeeded"); check("run panel visible", await page.locator("#mq-run-panel").isVisible()); check("run chips anchored", (await page.locator(".mq-run-chip").count()) > 0); + check( + "observed chips carry trace provenance", + (await page.locator('.mq-run-chip[data-annotation-source="trace"]').count()) > 0 + ); + check( + "static statement anchors carry analysis provenance", + (await page.locator('[data-annotation-source="analysis"]').count()) > 0 + ); + check( + "at most one grouped chip per anchor", + (await page.locator(".mq-run-chip").count()) === + (await page.locator(".mq-run-chip").evaluateAll((nodes) => { + const seen = new Set(); + let unique = 0; + nodes.forEach((node) => { + const key = node.parentElement ? node.parentElement.dataset.sourceId : null; + if (key && !seen.has(key)) { + seen.add(key); + unique += 1; + } + }); + return unique; + })) + ); check( "trace event list rendered", (await page.locator("#mq-run-events li").count()) > 0 @@ -269,6 +293,11 @@ function localHostOnly(url) { ); if (screenshotDir) { await page.screenshot({ path: path.join(screenshotDir, "control-flow-run.png") }); + await page.click('.mq-dock-btn[data-action="settings"]'); + await page.click('#mq-dialog-settings [data-action="theme"]'); + await page.screenshot({ path: path.join(screenshotDir, "control-flow-run-dark.png") }); + await page.click('.mq-dock-btn[data-action="settings"]'); + await page.click('#mq-dialog-settings [data-action="theme"]'); } // Clear run hides the display and keeps the project untouched. await page.click('.mq-dock-btn[data-action="clear"]'); diff --git a/tests/integration/execution.rs b/tests/integration/execution.rs index 3ccbf94..e0b1cff 100644 --- a/tests/integration/execution.rs +++ b/tests/integration/execution.rs @@ -512,3 +512,141 @@ fn event_frames_use_the_view_run_events_schema_and_numeric_ids() { assert_eq!(terminal["run_id"], accepted.run_id.as_str()); assert!(!events.is_empty()); } + +#[test] +fn source_edit_during_a_run_marks_the_result_stale_and_drops_anchor_ids() { + let project = crate::common::TempProject::new("exec-stale"); + let source = std::fs::read_to_string(repo_root().join("examples/control-flow/index.mq.md")) + .expect("example source"); + project.write( + "marqdo.yaml", + "project:\n name: control-flow\n version: 0.1.0\n", + ); + project.write("index.mq.md", &source); + let analysis = golden("control-flow.json"); + let run = + std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.run.json")) + .expect("run golden"); + let trace = + std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.trace.json")) + .expect("trace golden"); + let core = FakeCore::new("exec-stale-core"); + core.set_analysis(&analysis); + core.set_run(0, &run, Some(&trace)); + core.set_sleep_seconds(1); + let service = Arc::new( + DocumentService::open(&project.dir, Some(core.script.as_path())).expect("service"), + ); + let controller = Arc::new(ExecutionController::new( + Arc::clone(&service), + ExecutionConfig::default().with_timeout(10), + )); + let body = request_body( + &analysis, + serde_json::json!({"kind": "entry"}), + "pure", + serde_json::json!({}), + "req-stale-source", + ); + let accepted = controller.submit("session-a", &body).expect("accepted"); + wait_for_file(&core.dir.join("run-was-called")); + project.write("index.mq.md", &format!("{source}\n\n追加的编辑。\n")); + let subscription = controller + .subscribe("session-a", &accepted.run_id, Some(0)) + .expect("subscribe"); + let (_events, terminal) = terminal_events(subscription); + assert_eq!(terminal["state"], "succeeded"); + assert_eq!(terminal["result"]["stale"], true); + assert_eq!(terminal["trace"]["status"], "verified"); + assert_eq!(terminal["trace"]["stale"], true); + let events = terminal["trace"]["events"].as_array().expect("events"); + assert!(!events.is_empty()); + assert!( + events + .iter() + .all(|event| event["anchor"].is_null() && event["source_id"].is_null()), + "stale runs must not anchor to the current text" + ); +} + +#[test] +fn trace_with_a_mismatched_digest_is_reported_invalid_but_the_run_still_succeeds() { + let analysis = golden("control-flow.json"); + let run = + std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.run.json")) + .expect("run golden"); + let mut trace: serde_json::Value = serde_json::from_str( + &std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.trace.json")) + .expect("trace golden"), + ) + .expect("trace json"); + trace["source"]["analysis_digest"] = serde_json::json!(format!("sha256:{}", "0".repeat(64))); + let fixture = fixture_with( + "exec-trace-invalid", + "examples/control-flow", + &analysis, + &run, + Some(&trace.to_string()), + ExecutionConfig::default().with_timeout(10), + ); + let body = request_body( + &analysis, + serde_json::json!({"kind": "entry"}), + "pure", + serde_json::json!({}), + "req-trace-invalid", + ); + let accepted = fixture + .controller + .submit("session-a", &body) + .expect("accepted"); + let subscription = fixture + .controller + .subscribe("session-a", &accepted.run_id, Some(0)) + .expect("subscribe"); + let (_events, terminal) = terminal_events(subscription); + assert_eq!(terminal["state"], "succeeded"); + assert_eq!(terminal["trace"]["status"], "invalid"); + assert!(terminal["trace"]["events"].is_null()); +} + +#[test] +fn truncated_trace_is_marked_without_claiming_complete_coverage() { + let analysis = golden("control-flow.json"); + let run = std::fs::read_to_string( + repo_root().join("tests/golden/trace-v2/control-flow.truncated.run.json"), + ) + .expect("run golden"); + let trace = std::fs::read_to_string( + repo_root().join("tests/golden/trace-v2/control-flow.truncated.trace.json"), + ) + .expect("trace golden"); + let fixture = fixture_with( + "exec-trace-truncated", + "examples/control-flow", + &analysis, + &run, + Some(&trace), + ExecutionConfig::default().with_timeout(10), + ); + let body = request_body( + &analysis, + serde_json::json!({"kind": "entry"}), + "pure", + serde_json::json!({}), + "req-trace-truncated", + ); + let accepted = fixture + .controller + .submit("session-a", &body) + .expect("accepted"); + let subscription = fixture + .controller + .subscribe("session-a", &accepted.run_id, Some(0)) + .expect("subscribe"); + let (_events, terminal) = terminal_events(subscription); + assert_eq!(terminal["state"], "succeeded"); + assert_eq!(terminal["trace"]["status"], "verified"); + assert_eq!(terminal["trace"]["truncated"]["reason"], "event_budget"); + assert_eq!(terminal["trace"]["truncated"]["limit"], 5); +}