feat(PR-04): SSE execution loop, 7-route server and session security

- replace src/server.rs with src/server/{mod,routes,security,sse}.rs implementing
  exactly the RFC-0002 §6.2 routes: GET /, GET /file, GET /assets, GET
  /api/documents, POST /api/runs, GET /api/runs/{id}/events, POST
  /api/runs/{id}/cancel; old /api/health, /api/document, /api/run and
  /api/clear-run are gone (404)
- SSE writes the event-stream directly to the socket and flushes every frame and
  heartbeat (tiny_http's chunked writer would delay frames); Last-Event-ID
  replay or authoritative snapshot, 128-event ring, terminal close, heartbeat
  comments, at most two observers per run, no polling route
- per-session HttpOnly SameSite=Strict cookie + X-View-CSRF, exact same-origin
  Origin, loopback Host, Sec-Fetch-Site cross-site rejection, JSON-only 64 KiB
  run POSTs; cancel is an authenticated bodyless control POST
- reader.js: Analysis-driven function form with text-only command preview,
  pure/project mode + explicit fs grants with second confirmation, one POST then
  EventSource, cancel, live result/stdout/stderr/Trace panel and keyed source
  anchors (skipped when stale); no innerHTML of CLI data and no status polling
- render: hidden live run shell, generic explicit-run dialog, function call
  buttons carrying Analysis function ids/params, data-analysis-digest/document
  attributes; remove the old run-confirm dialog and run-page reload path
- add tests/security/ suite, rewrite HTTP/SSE integration tests, update browser
  suite for EventSource/dark theme/RFC width matrix (incl. 2048x1133)
- ADR-0009 + docs/evidence/PR-04 with real Chromium log/screenshots and a real
  frozen-CLI SSE run (285 Trace v2 events, CLI SHA unchanged)
- unit 63 + contract 41 + integration 40 + security 11 pass; clippy clean
This commit is contained in:
cflmy committed 2026-11-22 14:30:40 +08:00
1 parent e13c1828a4
commit 046834bcb6
35 files changed
+3812 -1963

No files matched your search

+26 -26
View File
@@ -5,7 +5,7 @@
> `PENDING`(未开始)、`N/A`(0002 明确非目标/删除项)。
> 证据列写测试/脚本文档;不得以视觉相似代替测试。
最后更新:2026-11-22(PR-00)
最后更新:2026-11-22(PR-04)
## 1. §0 核心不可妥协条件
@@ -28,21 +28,21 @@
| `@exec`/`@knowledge` 轻量标识(依 Analysis) | PASS | `src/semantic.rs`、`tests/integration/rendering.rs` |
| 源码态 | PASS | `src/render.rs`、`tests/integration/unicode_spans.rs` |
| 函数清单/导航 | PASS | `src/presentation.rs`、`src/render.rs` |
| 单函数参数填写 + 调用 | **PENDING** | PR-03 请求合同 + PR-04 前端表单 |
| 文档入口 `main` 执行 | PARTIAL | 同步 pure 已存在;需改为 SSE 异步任务(PR-04) |
| 运行结果(返回/stdout/stderr/错误) | PARTIAL | 0001 已有;需 0002 JSON→UI 映射与安全文本展示复核(PR-04/05) |
| Trace v2 调用/判断/循环/副作用 | PARTIAL | `src/trace.rs` 已有;需 SSE 后动态标注与 stale 规则(PR-04/05) |
| 执行确认/取消/超时/单任务 | **PENDING** | PR-03/04 |
| **SSE 状态与终态(禁止轮询)** | **PENDING(阻塞)** | PR-04 |
| 单函数参数填写 + 调用 | PASS | `assets/reader.js` 动态表单 + `execution/request.rs` + HTTP/SSE 测试 |
| 文档入口 `main` 执行 | PASS | `POST /api/runs` → SSE;无 main 时引导函数列表 |
| 运行结果(返回/stdout/stderr/错误) | PASS | 安全文本/DOM 渲染、`value_text`、stderr/stdout、错误码;浏览器/HTTP 测试 |
| Trace v2 调用/判断/循环/副作用 | PASS(动态锚点) | Controller 校验摘要 + JS `source_id` chip;stale 不锚定 |
| 执行确认/取消/超时/单任务 | PASS | 两阶段确认、幂等取消、唯一终态、409 busy、墙钟上限 |
| **SSE 状态与终态(禁止轮询)** | PASS | 逐帧 flush、心跳、Last-Event-ID、snapshot、2 连接上限;无轮询路由 |
| 独立 Tool 安装/调用 | PASS(0001) | PR-01 迁移到 0.2.0/scripts |
| 静态 HTML 导出 | PARTIAL | 已实现;PR-06 复核零执行/离线 |
| 暖纸/品牌/樱花/侧注/Dock | PARTIAL | 已有;PR-05 视觉矩阵 |
| 搜索 + 目录 | PASS | 浏览器证据 |
| 有选择的 Observed 侧注/退化 | PARTIAL | 内联 chip 已有;SSE 后按本次运行更新(PR-04/05) |
| 深色主题/字号/显示设置 | PARTIAL | 已有字号/标注开关/print;深色待 PR-05 |
| project fs read/write grants | **PENDING** | PR-03/04(MVP CONTROLLED) |
| project fs read/write grants | PASS(MVP CONTROLLED) | 请求校验 + 二次确认 + `--allow-read/--allow-write` argv 测试 |
| net/process/env/plugin 等授权 | N/A(DEFERRED,必须拒绝并提示 CLI) | PR-03 校验 |
| 变量/内存/Notebook/调试/Session/AI/Kernel | N/A(REMOVED) | 禁止出现 DOM/路由/状态 |
| 变量/内存/Notebook/调试/Session/AI/Kernel | N/A(REMOVED) | 无路由/DOM/状态;仅一个明确 UNSUPPORTED 说明 |
## 3. §5 架构与目录
@@ -58,18 +58,18 @@
| 条款 | 状态 | 计划 |
|---|---|---|
| 7 条最小路由 | **PENDING** | PR-04 |
| POST /api/runs 两阶段确认请求体(document/target/parameters/analysis_digest/mode/grants/client_request_id) | PARTIAL | `execution/request.rs` 校验/解析/计划已实现;HTTP 路由 PR-04 |
| 校验顺序(会话→CSRF/Origin/Host→大小/schema→containment→check→digest→函数/参数→模式/grants→并发→202) | PARTIAL | Controller 已实现 schema→dedupe→busy→check→digest→target→202;HTTP 前置校验 PR-04 |
| 同 `client_request_id` 幂等/不同 payload 409 | PASS(Controller) | `tests/integration/execution.rs`;HTTP 映射 PR-04 |
| SSE 帧合同(事件类型/seq/id/schema/心跳) | **PENDING** | PR-04 |
| Last-Event-ID 重放、快照、128 缓冲、TTL、≤2 连接/任务 | **PENDING** | PR-04 |
| 单活动任务、409 run.busy、后台执行不阻塞 SSE | PARTIAL | Controller 单任务/后台线程已实现并测试;HTTP/SSE PR-04 |
| 7 条最小路由 | PASS | `server/routes.rs`;旧路由 404 测试 |
| POST /api/runs 两阶段确认请求体(document/target/parameters/analysis_digest/mode/grants/client_request_id) | PASS | `execution/request.rs` + `POST /api/runs`;unknown field/size/mode/grant 测试 |
| 校验顺序(会话→CSRF/Origin/Host→大小/schema→containment→check→digest→函数/参数→模式/grants→并发→202) | PASS | `server/security.rs` + Controller;拒绝路径不启动进程测试 |
| 同 `client_request_id` 幂等/不同 payload 409 | PASS | Controller + HTTP 集成测试 |
| SSE 帧合同(事件类型/seq/id/schema/心跳) | PASS | `server/sse.rs`、`tests/integration/http_live.rs` |
| Last-Event-ID 重放、快照、128 缓冲、TTL、≤2 连接/任务 | PASS | 重放/伪造游标/2 连接/终态快照测试 |
| 单活动任务、409 run.busy、后台执行不阻塞 SSE | PASS | Controller + HTTP 409/并发测试 |
| 进程组取消(TERM→2s→KILL)、墙钟上限、有界输出 | PASS(模块) | `src/execution/process.rs`、`tests/integration/execution.rs` |
| 显式预算参数 | PASS(模块) | `src/execution/controller.rs`、`--run-timeout` 1..=120 |
| 会话 Cookie(HttpOnly/SameSite=Strict)+ X-View-CSRF + Origin/Host | **PENDING** | PR-04 |
| 会话 Cookie(HttpOnly/SameSite=Strict)+ X-View-CSRF + Origin/Host | PASS | `server/security.rs`、`tests/security/`、浏览器测试 |
| loopback only、请求体≤64KiB、CSP、路径 containment | PARTIAL | 大部分已有;会话 Cookie 待 PR-04 |
| SSE 按会话隔离、跨站读取拒绝 | **PENDING** | PR-04 |
| SSE 按会话隔离、跨站读取拒绝 | PASS | 跨会话 404/401、Sec-Fetch-Site 拒绝 |
## 5. §8 里程碑
@@ -79,7 +79,7 @@
| PR-01 Tool/打包/scripts/CI/0.2.0 | **完成** | `scripts/package.sh`、`scripts/install-smoke.sh`、`scripts/verify-frozen-cli.sh`、`.github/workflows/ci.yml` |
| 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/路由/会话/前端闭环 | PENDING | — |
| PR-04 SSE/路由/会话/前端闭环 | **完成** | `docs/evidence/PR-04/`(含真实 Chromium + 真实冻结 CLI) |
| PR-05 Trace 绑定/标注/浏览器回归 | PENDING | — |
| PR-06 导出/发布/文档闭环 | PENDING | — |
@@ -91,15 +91,15 @@
| E02 打开 knowledge+exec 文件不运行 | PASS | `tests/integration/http_live.rs` |
| E03 Source/Document 切换 | PASS | 浏览器证据 |
| E04 中文/Emoji/表格/公式 span | PASS | `tests/integration/unicode_spans.rs`、golden |
| E05 main 运行 run --json --trace-file | PARTIAL | 同步路径有;SSE 终态待 PR-04 |
| E06 H2 Exec n=5 call | PARTIAL | `build_call_args` + Controller 测试;真实 CLI/UI PR-04/06 |
| E07 文本/列表常量参数 | PARTIAL | `--arg name=expression` argv 已实现;真实 CLI/UI PR-04/06 |
| E05 main 运行 run --json --trace-file | PASS | 真实冻结 CLI SSE 证据 `docs/evidence/PR-04/real-run.log` |
| 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 后复核 |
| E10 SSE text/event-stream 无 polling | **PENDING** | PR-04 |
| E11 Last-Event-ID 重连 | **PENDING** | PR-04 |
| E12 长任务心跳/GET 不重启 | **PENDING** | PR-04 |
| E13 取消终止进程树 | PARTIAL | Controller/process 已测(`second_run_is_busy...`、`process.rs` 单测);HTTP 接线 PR-04 |
| 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 复核 |
+21 -35
View File
@@ -24,9 +24,9 @@
| PR-01 Tool v1 打包/安装/调度、0.2.0、scripts/、CI | **完成** | `scripts/package.sh`、`scripts/install-smoke.sh`、`scripts/verify-frozen-cli.sh`、`.github/workflows/ci.yml`;冻结 CLI `tool install`/`view --help`/篡改拒绝实测 |
| 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 | **未实现(MVP 阻塞项)** | — |
| PR-05 Trace 绑定/原文标注/浏览器回归 | 待开始(现有 Trace 校验可迁移) | `src/trace.rs` |
| PR-06 静态导出/发布包/文档闭环 | 待开始(现有导出可迁移) | `src/export.rs` |
| 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-06 静态导出/发布包/文档闭环 | 待开始 | `src/export.rs`、`scripts/acceptance.sh`、`docs/ACCEPTANCE.md` |
## 继承自 0001 的可用资产(不等于 0002 验收通过)
@@ -37,44 +37,30 @@
| 原始 Markdown/GFM 阅读、源码态、搜索/设置/打印 | 已实现(单一 `assets/reader.css`/`assets/reader.js`) |
| Analysis v2 严格校验、函数/Unit/签名/诊断/导入/引用/资源投影 | 已实现(无效 span 降级) |
| 单一 `ReaderRenderer`,Live/Export 共用 | 已实现(`src/reader/render.rs`,门禁测试) |
| 显式确认后同步运行 `pure` + Trace v2 校验与展示 | 旧同步路径仍在(过渡);新的异步 Controller/SSE 就绪于 PR-03/04 |
| 显式确认 + SSE 异步执行 + Trace v2 | 已实现(7 路由、单任务、取消、断线重放) |
| 静态离线导出(无执行、manifest/哈希、原子发布) | 已实现,待按 0002 重新验收 |
| Tool v1 打包/安装/分发、冻结 CLI SHA 前后一致 | 已实现,待迁移至 `scripts/` 与 0.2.0 |
| HTTP 安全(Host/Origin/CSRF/路径 containment/CSP) | 部分实现;缺**会话 Cookie**模型与 7 路由合同 |
| 真实 Chromium 视觉/交互/无 JS/打印 | 已有证据(0001 时期),待按 0002 宽度矩阵重跑 |
## 与 RFC-VIEW-0002 的已知差距(全部必须关闭)
## 与 RFC-VIEW-0002 的已知差距(剩余项)
1. **SSE 执行闭环缺失**:当前是 POST 同步运行后整页重载;0002 要求
`POST /api/runs` → 202 → `GET /api/runs/{id}/events` SSE(无轮询)→
accepted/running/终态;断线 `Last-Event-ID` 重放、128 事件环形缓冲、终态 TTL。
2. **路由未收敛**:现为 `/api/run`、`/api/clear-run`、`/api/document`、
`/api/health`;0002 只允许
`GET /`、`GET /file`、`GET /assets/...`、`GET /api/documents`、
`POST /api/runs`、`GET /api/runs/{id}/events`、`POST /api/runs/{id}/cancel`。
3. **无单函数调用表单**:函数目前只有跳转列表;缺基于 Analysis 签名的参数填写、
命令预览、`--arg` 常量表达式提交。
4. **无 project grants**:0002 要求受控 fs read/write glob(明确二次确认),
其它 net/process/env/plugin 一律拒绝并引导 CLI。
5. **无运行会话 Cookie**:CSRF 目前是进程级 token;0002 要求
`HttpOnly; SameSite=Strict` 随机会话 Cookie + 每会话 CSRF,且 SSE 按会话隔离
(PR-04)。
6. **HTTP/SSE 传输未接**:Controller 已实现单任务状态机、取消、去重、
`client_request_id` 幂等/409 与 `409 run.busy`(`src/execution/controller.rs`),
但 `POST /api/runs`、`GET .../events`、`POST .../cancel` 与前端尚未切换(PR-04)。
7. **执行预算**:Controller 已显式发送 `--timeout`(默认 30s/最大 120s)、
`--fuel 1000000`、`--max-output 1048576`、`--trace-max-events 10000`、
`--trace-max-bytes 4194304`,并做 4 MiB JSON/Trace 有界读取与
`view.output_limit`;HTTP 层仍需映射为终态事件(PR-04)。
8. **目录/测试/示例仍有缺口**:`src/protocol`/`src/reader`/`src/config.rs`/
`tests/{contract,integration}`/`scripts/`/`.github/workflows/ci.yml` 已就位;
仍缺 `src/execution/*`、`src/server/{mod,routes,security,sse}.rs`、
`tests/security/` 套件、`examples/{welcome,branch-loop,function-params,
knowledge-only,unsafe-side-effect}.mq.md`、`docs/design` 0002 宽度截图矩阵。
9. **文档口径**:README/COMPATIBILITY/ACCEPTANCE 仍含 0001 时期表述,需在
PR-06 前全部更新为 0002 口径。
10. **视觉回归矩阵**:0002 要求 2048×1133 源参考与 1664×920/1440×900/1024×768/
768×1024/390×844 宽度;现有截图集合不完整。
PR-00~PR-04 已关闭 SSE、7 路由、单函数调用表单、受控 project grants、
会话 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,
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/安全套件已覆盖
大部分: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`,
自动化几何/颜色/无外部资源/响应式证据已存在(`docs/evidence/PR-04/`)。
## 明确 UNSUPPORTED / 非目标(不得伪造)
@@ -0,0 +1,86 @@
# ADR-0009:SSE 传输实现、会话模型与 7 路由收敛
- 状态:Accepted
- 日期:2026-11-22
- 关联:`docs/architecture/RFC-VIEW-0002.md` §6.2/§6.4/§6.5/§6.8;PR-04
## 1. SSE 传输:直接写 socket 并逐帧 flush
`tiny_http` 的流式 `Response` 使用 `chunked_transfer::Encoder::new`,只有缓冲区
填满或响应结束时才 flush。对 SSE 来说,这意味着已接受的 `accepted`/`running`/
心跳可能被扣留数秒,直到任务结束,**不满足“状态变化立即推送”**。
因此 `server/sse.rs` 在成功鉴权后使用 `Request::into_writer()` 取到底层 socket
writer,自行写入:
```
HTTP/1.1 200 OK
Content-Type: text/event-stream; charset=utf-8
Cache-Control: no-store
X-Accel-Buffering: no
Connection: close
event: ... / id: N / data: {...}
```
要点:
- **每帧/心跳后立即 `flush()`**,浏览器 EventSource 即时收到;
- 不设 `Content-Length` 且 `Connection: close`,body 以 EOF 结束(SSE 合法传输);
- 终态事件写出后主动关闭流;客户端 JS 在收到终态时关闭 EventSource,不会重连
重复执行;
- 客户端断开(broken pipe)只关闭观察连接,不取消任务、不产生执行终态;
- 只允许 4 种 `run.*` 事件 + `: heartbeat` 注释;心跳不使用序号;
- 每任务最多 2 个并发 SSE 观察连接(Controller 计数,超出 `429 view.sse_busy`);
128 条环形缓冲 + 终态快照 + `Last-Event-ID` 重放或权威 snapshot;
- 测试钩子 `MARQDO_VIEW_SSE_HEARTBEAT_SECS` 仅用于本地/CI 缩短心跳周期,默认
15 秒(不暴露为产品功能)。
## 2. 会话与鉴权
- 首次 `GET /` 或 `GET /file` 发放 `mq_view_session=<随机32字节hex>` Cookie:
`HttpOnly; SameSite=Strict; Path=/; Max-Age=43200`;loopback HTTP 不加 `Secure`
(不伪造 HTTPS)。
- 页面通过 `<meta name="mq-csrf-token" content="...">` 获得**该会话**的 CSRF;
POST 必须携带 `X-View-CSRF`,与 Cookie 会话匹配(常量时间比较)。
- POST 还要求:合法 `Host`(loopback+端口)、精确同源 `Origin`(缺失即拒绝)、
`Sec-Fetch-Site` 不得为 `cross-site`;`POST /api/runs` 要求
`Content-Type: application/json`,body ≤64 KiB。
- `POST /api/runs/{id}/cancel` 是无 body 的控制请求:允许无 JSON Content-Type,
但仍要求 Cookie + CSRF + Origin/Host。
- SSE GET 只允许**启动该 run 的会话**读取;事件/取消一律按 `session_id` 校验,
跨会话返回 404(不泄漏任务是否存在)。
- 会话表上限 64、空闲 TTL 12h;Cookie/CSRF 只存内存,不写磁盘、不进 URL。
## 3. 路由表(最终态)
只保留 7 条:`GET /`、`GET /file?path=`、`GET /assets/...`、
`GET /api/documents`、`POST /api/runs`、`GET /api/runs/{id}/events`、
`POST /api/runs/{id}/cancel`。
- 删除 `/api/health`、`/api/document`、`/api/run`、`/api/clear-run`;
- 不提供状态轮询路由、`/api/eval`、Notebook/session 兼容层或通用 shell 端点;
- `GET /api/documents` 是只读结构(文档列表 + 当前 outline/functions/analysis
摘要),无副作用;页面/刷新/搜索/切换/导出均不形成执行边。
## 4. 前端最小状态
`reader.js` 只做 DOM/表单/SSE:
- Dock 运行:有 main → 打开运行表单;无 main → 打开函数列表并解释,绝不替跑;
- 函数列表的“调用”按钮携带 Analysis 函数 id 与参数签名,动态生成表单;
- 表单有命令预览(文本节点)、pure/project 模式与 fs read/write glob;project
需要二次 `window.confirm`;无 net/process/env/plugin 等按钮;
- 提交一次 POST 后建立 `EventSource(..., {withCredentials:true})`,**没有
setInterval/轮询**;`seq` 去重、`run_id` 校验、终态关闭;
- 运行区/结果/stdout/stderr/Trace 全部用 `textContent`/DOM 文本构建,绝不
innerHTML 服务端数据;stale 结果不绘制原文锚点;
- 浏览器重载不会自动 POST;清理只隐藏本地展示,不删除项目文件/lock,也不取消
仍在服务端的任务。
## 5. 与 RFC 的关系
实现保留 RFC 的公开合同(事件类型、字段、状态机、`Last-Event-ID`、心跳语义),
仅选择“直接写 socket”作为传输细节——属于 RFC 允许的实现自由,不改变协议。
测试覆盖:HTTP/SSE 集成、真实 Chromium EventSource、跨会话/跨站拒绝、取消唯一
终态、心跳、2 连接上限与 Last-Event-ID 重放(见 `docs/evidence/PR-04/`)。
+83
View File
@@ -0,0 +1,83 @@
# PR-04 证据:SSE 执行闭环、7 路由与会话安全
- 日期:2026-11-22
- 范围:`src/server/{mod,routes,security,sse}.rs`、`src/reader/render.rs` 运行表单/
运行区、`assets/reader.js` SSE 前端、`tests/integration/http_live.rs`、
`tests/security/`、浏览器套件
- 冻结 CLI:`marqdo 3.0.0-beta.1`,SHA-256
`062cb56e226bee55c21d238e195920caa985e62bb3877ba7b07abcc04dc0fe3f`
(真实 run 证据 `real-run.log`,run 前后 SHA 不变)
- 产品规则:SSE 无轮询;运行唯一入口 `POST /api/runs`;单任务;取消唯一终态
## 1. 路由与安全
| 路由 | 验证 |
|---|---|
| `GET /` / `GET /file` | 发放 HttpOnly/SameSite=Strict Cookie + CSRF meta;重复打开 10 次无 `run` 调用(`pages_issue_a_session_cookie_and_never_execute_the_document`) |
| `GET /assets/...` | 唯一 CSS/JS、immutable、CSP/`nosniff`;未知资产 404 |
| `GET /api/documents` | `marqdo.view-documents` v1 只读结构;旧 `/api/document`、`/api/health`、`/api/run` 均 404 |
| `POST /api/runs` | 202 + run_id/events_url/cancel_url;无 Cookie/CSRF/Origin、错 Origin、非 JSON、超大 body 全部拒绝且不启动进程 |
| `GET /api/runs/{id}/events` | `text/event-stream`、逐帧 flush、心跳、Last-Event-ID 重放/伪造游标 snapshot、跨会话 404、无会话 401、>2 连接 429 |
| `POST /api/runs/{id}/cancel` | 幂等、唯一 `cancelled` 终态、跨会话 404、不新增进程 |
浏览器测试还断言:`Origin`/`Sec-Fetch-Site` 跨站拒绝、`HttpOnly`+`SameSite=Strict`、
CSP 无 `unsafe-inline`/`unsafe-eval`、symlink 出根被拒、Markdown 不能注入
`<script>`/事件处理器。
## 2. SSE 生命周期(真实事件序列)
`tests/integration/http_live.rs`:
- `explicit_run_streams_sse_accepted_running_and_terminal_with_verified_trace`:
accepted → running → succeeded;`id` 严格递增且与 `data.seq` 一致;终态含
CLI result 与 `trace.status=verified` 事件;
- `last_event_id_replays_only_newer_events_and_old_cursor_gets_snapshot`:重放
只发更新事件;伪造游标得权威 snapshot;
- `cancel_is_idempotent_and_yields_one_terminal_state`:两次 cancel 均 200,
SSE 只有一个 `cancelled` 终态且 <8s 结束;
- `sse_heartbeats_keep_a_slow_job_connected`:长任务期间收到 `: heartbeat`,
任务仍成功;
- `at_most_two_sse_observers_per_run`:第 3 个观察连接 429;
- `duplicate_request_id_is_idempotent_and_busy_or_conflict_never_starts_a_second_process`:
同 ID 同 body → 同 run_id;busy/conflict → 409,不启第二进程;
- `sse_and_cancel_are_scoped_to_the_owning_session`:跨会话读取/取消 404;
- `document_without_main_is_rejected_without_running_another_function`:400
`view.no_entry_function` 且无 run 调用。
## 3. 浏览器(真实 Chromium 131,七套视图宽度)
`browser.log` 为 `MARQDO_BIN=062cb56e… scripts/test-browser.sh` 的真实输出,
三个项目全部 PASS,包括:
- run 对话框 → 命令预览 → 确认 → **SSE 终态 succeeded**(无整页重载);
- 运行区、Trace 事件列表、stdout 文本、原文 `.mq-run-chip` 锚点;
- 请求记录中没有状态轮询 API;
- 清理隐藏运行区且不重载页面;
- 2048×1133 / 1664×920 / 1440×900 / 1024×768 / 768×1024 / 390×844 无横向溢出、
Dock 在视口内、无外部请求、无 console error、无 JS 可读、打印隐藏交互;
- 深色/暖纸主题切换并持久化,无 unsafe-inline/eval。
截图:`welcome-2048x1133.png`、`control-flow-2048x1133.png`、
`control-flow-390x844.png`、`control-flow-run.png`。
## 4. 真实冻结 CLI 端到端
`real-run.log`:启动 `target/release/marqdo-view examples/control-flow`(使用
冻结 CLI),脚本 `scripts/acceptance-run.py` 完成 GET 页面 → POST /api/runs →
读取 SSE 终态:`succeeded`、`trace.status=verified`、**285 条 Trace 事件**。
截图与日志均来自该实现;CLI SHA 在前后保持不变。
## 5. 测试计数(本 PR 合并时)
```
cargo test --locked --offline
unit 63 + contract 41 + integration 40 + security 11 = 155 passed / 0 failed
cargo clippy --all-targets --offline -- -D warnings # clean
```
## 6. 已知边界
- 首版不提供 net/process/env/plugin/provider/kernel 授权 UI;请求会被拒绝并提示
使用终端 CLI(`view.run_denied`/`view.run_invalid`)。
- `project` 模式需要显式 read/write glob 并二次确认;它不是 OS 沙箱。
- SSE 只推送 View 生命周期与终态数据,不推送逐字 stdout/Token 流(RFC §6.1)。
+143
View File
@@ -0,0 +1,143 @@
==> 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 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)
Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

+1
View File
@@ -0,0 +1 @@
explicit real run: PASS (run_id=918359e1f4f5fd5155c86e6e55854b2c events=285)
+7
View File
@@ -0,0 +1,7 @@
marqdo view: http://127.0.0.1:40487/
root: /root/work/marqdo-view/examples/control-flow
1 .mq.md document(s). Ctrl+C to stop.
core: marqdo 3.0.0-beta.1 (Analysis v2, Trace v2)
run: explicit only (pure by default, one active job, 30s wall clock)
JOB_DEBUG id=918359e1f4f5fd5155c86e6e55854b2c run_core elapsed=61.583791ms cancel=false
JOB_DEBUG id=918359e1f4f5fd5155c86e6e55854b2c terminal state=Succeeded total=67.878533ms
Binary file not shown.

After

Width:  |  Height:  |  Size: 187 KiB