From e4387259bab774aa319b70b24a55e5b0af150511 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sun, 18 Oct 2026 13:19:41 +0800 Subject: [PATCH] fix(view): make static examples self-contained MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Foreign cell examples read stdin with a deterministic fallback, so static exports show real output without tracebacks. Low-level session bindings named 会话 are no longer mistaken for a view plan; static export runs all cells again. --- CHANGELOG.md | 2 +- public/features/19-foreign-view-cells.mq.md | 13 +++++++-- public/features/19-外联视图单元.mq.md | 13 +++++++-- src/host/foreign_doc.rs | 12 ++++++-- src/view/output.rs | 31 --------------------- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae984ef..1f1846f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added - **view Cell stdin**:view 增加 `Cell stdin / input` 输入框,Run/Step/Run All 都会传给外联 cells;无状态 cell 使用 raw stdin,持久内核使用 `marqdo_input`;静态导出使用 frontmatter `stdin` 作为构建时输入。 -- **foreign 拥有视图会话**:新增 `foreign.session kernels= cells=`,返回 foreign 会话句柄;`view` 只消费 `会话` 绑定,不再识别 `内核表` / `会话表` 保留名。没有 `会话` 时,view cells 使用无状态一次性执行,不创建内核。清除输出已接通 `/api/session/clear`,普通 view 与 foreign cells 共用,并由 Execution 面板的 Clear 按钮清除页面输出;无 `会话` 的 view 在 Run All 时执行整个文档并回写 Execution(不再空转);静态导出与动态 view 共用同一套 View 外壳和按钮布局;静态页面执行按钮点击显示“静态页面无法真实执行”,不伪造成功;静态导出与 view 的 Run All 共用同一 foreign_doc `run_all` 路径;构建时实际执行代码单元并把 stdout/stderr 注入代码卡;没有提供 frontmatter `stdin` 时,读取 stdin 的示例不执行,显示说明而不是 traceback;移除 `In []` / `Out []` 与 Notebook 品牌;没有新增 CLI 或第二个交互模式。 +- **foreign 拥有视图会话**:新增 `foreign.session kernels= cells=`,返回 foreign 会话句柄;`view` 只消费 `会话` 绑定,不再识别 `内核表` / `会话表` 保留名。没有 `会话` 时,view cells 使用无状态一次性执行,不创建内核。清除输出已接通 `/api/session/clear`,普通 view 与 foreign cells 共用,并由 Execution 面板的 Clear 按钮清除页面输出;无 `会话` 的 view 在 Run All 时执行整个文档并回写 Execution(不再空转);静态导出与动态 view 共用同一套 View 外壳和按钮布局;静态页面执行按钮点击显示“静态页面无法真实执行”,不伪造成功;静态导出与 view 的 Run All 共用同一 foreign_doc `run_all` 路径;构建时实际执行代码单元并把 stdout/stderr 注入代码卡;静态示例通过代码 fallback 或 frontmatter `stdin` 提供输入,不再依赖空 stdin;`会话` 作为低层 session id 时不会被误判为 view plan;移除 `In []` / `Out []` 与 Notebook 品牌;没有新增 CLI 或第二个交互模式。 ### Fixed diff --git a/public/features/19-foreign-view-cells.mq.md b/public/features/19-foreign-view-cells.mq.md index 1152aa2..9073bcb 100644 --- a/public/features/19-foreign-view-cells.mq.md +++ b/public/features/19-foreign-view-cells.mq.md @@ -46,7 +46,15 @@ import json import statistics import sys -rows = json.load(sys.stdin) +raw = sys.stdin.read() +rows = json.loads(raw) if raw.strip() else [ + {"region": "华东", "amount": 120}, + {"region": "华东", "amount": 300}, + {"region": "华南", "amount": 90}, + {"region": "华南", "amount": 140}, + {"region": "华北", "amount": 220}, + {"region": "西部", "amount": 80}, +] by_region = {} for row in rows: by_region.setdefault(row["region"], []).append(float(row["amount"])) @@ -128,7 +136,8 @@ Parallel fan-out. Every input gets its own process; failures are isolated and or import json import sys -value = json.load(sys.stdin) +raw = sys.stdin.read() +value = json.loads(raw) if raw.strip() else 1 print(json.dumps(value + 1)) ``` diff --git a/public/features/19-外联视图单元.mq.md b/public/features/19-外联视图单元.mq.md index 4bbd46c..bb73cd5 100644 --- a/public/features/19-外联视图单元.mq.md +++ b/public/features/19-外联视图单元.mq.md @@ -46,7 +46,15 @@ import json import statistics import sys -rows = json.load(sys.stdin) +raw = sys.stdin.read() +rows = json.loads(raw) if raw.strip() else [ + {"区域": "华东", "金额": 120}, + {"区域": "华东", "金额": 300}, + {"区域": "华南", "金额": 90}, + {"区域": "华南", "金额": 140}, + {"区域": "华北", "金额": 220}, + {"区域": "西部", "金额": 80}, +] by_region = {} for row in rows: by_region.setdefault(row["区域"], []).append(float(row["金额"])) @@ -128,7 +136,8 @@ print(json.dumps({ import json import sys -value = json.load(sys.stdin) +raw = sys.stdin.read() +value = json.loads(raw) if raw.strip() else 1 print(json.dumps(value + 1)) ``` diff --git a/src/host/foreign_doc.rs b/src/host/foreign_doc.rs index 60852ea..ad32ad0 100644 --- a/src/host/foreign_doc.rs +++ b/src/host/foreign_doc.rs @@ -170,7 +170,9 @@ fn plan_tables( return Ok((None, None)); }; let Value::Map(fields) = plan else { - return Err("`会话` must be a foreign.session handle".into()); + // `会话` is also a common low-level session id binding (e.g. from + // foreign.session_open). It is not a view plan; leave cells stateless. + return Ok((None, None)); }; let field = |name: &str| { fields @@ -309,8 +311,14 @@ impl DocumentSession { collect_cells(&entry.body, &mut cells, &mut seen)?; let declarations = capture_bindings(path, root, &module, &source)?; - let persistent = declarations.contains_key("会话"); let (plan_kernels, plan_cells) = plan_tables(&declarations)?; + let persistent = matches!( + declarations.get("会话"), + Some(Value::Map(fields)) + if fields + .iter() + .any(|(k, v)| k == "kind" && value_text(v) == "foreign-session") + ); let mut kernels: Vec = Vec::new(); let mut kernel_map: HashMap = HashMap::new(); if let Some(rows) = plan_kernels { diff --git a/src/view/output.rs b/src/view/output.rs index 0d78e1b..f5da4c3 100644 --- a/src/view/output.rs +++ b/src/view/output.rs @@ -50,13 +50,6 @@ fn record_field(record: &Value, key: &str) -> Option { /// Run the document's cells at export time and keep their real outputs. This /// is what makes a static page show foreign results instead of just source. -fn cell_needs_stdin(source: &str) -> bool { - source.contains("sys.stdin") - || source.contains("process.stdin") - || source.contains("readFileSync(0") - || source.contains("readFileSync(0,") -} - fn static_cell_outputs( root: &ViewRoot, abs: &std::path::Path, @@ -66,23 +59,6 @@ fn static_cell_outputs( let Ok(mut session) = DocumentSession::open(abs, &root.root) else { return out; }; - if stdin.trim().is_empty() { - let mut run_order = Vec::new(); - for cell in session.cells.iter() { - if cell_needs_stdin(&cell.source) { - out.insert( - cell.name.clone(), - StaticCellOutput { - ok: true, - text: "(static: this cell reads stdin; its input is supplied by foreign.call — see Execution)".to_string(), - }, - ); - } else { - run_order.push(cell.name.clone()); - } - } - session.order = run_order; - } let Ok(summary) = session.run_all_with_stdin(false, Some(stdin)) else { session.close(); return out; @@ -263,13 +239,6 @@ fn resolve_abs(root: &ViewRoot, rel: &str) -> Result { mod tests { use super::*; - #[test] - fn detects_stdin_dependent_cells() { - assert!(cell_needs_stdin("import sys\njson.load(sys.stdin)")); - assert!(cell_needs_stdin("process.stdin.on('data', () => {})")); - assert!(!cell_needs_stdin("console.log('hello')")); - } - #[test] fn injects_visible_static_cell_outputs() { let html = r#"
"#;