release: v2.3.0 Foreign Strong session/handle/replay/arity
Opaque session handles, affine close, replay_check vs oracle journal, table arity refuse, and Ω∈V_safe gate for the Strong foreign narrative. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
82c8d78fd3
commit
142ab523d6
31 files changed
+827
-57
No files matched your search
@@ -42,6 +42,7 @@ pub fn builtin_type(v: &Value) -> Value {
|
||||
Value::Num(_) => "num",
|
||||
Value::Text(_) => "text",
|
||||
Value::Secret(_) => "secret",
|
||||
Value::Handle(_) => "handle",
|
||||
Value::List(_) => "list",
|
||||
Value::Map(_) => "map",
|
||||
Value::Formula(_) => "formula",
|
||||
|
||||
@@ -109,6 +109,7 @@ pub fn type_of(v: &Value) -> &'static str {
|
||||
Value::Num(_) => "num",
|
||||
Value::Text(_) => "text",
|
||||
Value::Secret(_) => "secret",
|
||||
Value::Handle(_) => "handle",
|
||||
Value::List(_) => "list",
|
||||
Value::Map(_) => "map",
|
||||
Value::Formula(_) => "formula",
|
||||
|
||||
+372
-26
@@ -1,4 +1,9 @@
|
||||
//! Document session: cells, kernels, execution journal, state store.
|
||||
//! Document session: cells, kernels, journals, state store.
|
||||
//!
|
||||
//! Two journals (do not mix names):
|
||||
//! - **execution journal** (`JournalRecord`): UI timing / status / errors.
|
||||
//! - **oracle journal** (`OracleRecord`): Ω step recordings for `replay_check`
|
||||
//! (source hash, stdout, result, ok, code).
|
||||
//!
|
||||
//! Stateful cells live here, owned by the foreign runtime. The view reads a
|
||||
//! `会话 = > foreign.session ...` handle; no table names are reserved, and a
|
||||
@@ -56,6 +61,9 @@ pub struct Cell {
|
||||
pub last_ms: u64,
|
||||
pub last_ok: bool,
|
||||
pub last_error: String,
|
||||
/// Captured stdout from the last evaluation (recording surface for replay_check).
|
||||
pub last_stdout: String,
|
||||
pub last_code: i64,
|
||||
/// Value returned by the last successful evaluation, used by explicit persist.
|
||||
pub last_result: Value,
|
||||
}
|
||||
@@ -81,6 +89,20 @@ pub struct JournalRecord {
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
/// One recorded Ω step: what a later `replay_check` compares against.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OracleRecord {
|
||||
pub seq: u64,
|
||||
pub cell: String,
|
||||
pub kernel: String,
|
||||
pub lang: String,
|
||||
pub source_hash: String,
|
||||
pub ok: bool,
|
||||
pub code: i64,
|
||||
pub stdout: String,
|
||||
pub result: Value,
|
||||
}
|
||||
|
||||
pub struct DocumentSession {
|
||||
pub path: PathBuf,
|
||||
root: PathBuf,
|
||||
@@ -89,6 +111,7 @@ pub struct DocumentSession {
|
||||
pub kernels: Vec<KernelDecl>,
|
||||
pub order: Vec<String>,
|
||||
pub journal: Vec<JournalRecord>,
|
||||
pub oracle_journal: Vec<OracleRecord>,
|
||||
pub value_store: HashMap<String, Value>,
|
||||
pub cursor: usize,
|
||||
pub generation: u64,
|
||||
@@ -289,6 +312,8 @@ fn collect_cells(stmts: &[crate::ast::Stmt], out: &mut Vec<Cell>, seen: &mut Has
|
||||
last_ms: 0,
|
||||
last_ok: false,
|
||||
last_error: String::new(),
|
||||
last_stdout: String::new(),
|
||||
last_code: 0,
|
||||
last_result: Value::None,
|
||||
});
|
||||
}
|
||||
@@ -464,6 +489,7 @@ impl DocumentSession {
|
||||
kernels,
|
||||
order,
|
||||
journal: Vec::new(),
|
||||
oracle_journal: Vec::new(),
|
||||
value_store: HashMap::new(),
|
||||
cursor: 0,
|
||||
generation: 1,
|
||||
@@ -484,7 +510,7 @@ impl DocumentSession {
|
||||
let id = session.ensure_kernel_session(&kernel)?;
|
||||
foreign_session::set(
|
||||
&mut session.host,
|
||||
&Value::Text(id),
|
||||
&Value::Handle(id),
|
||||
&Value::Text(name),
|
||||
&value,
|
||||
)?;
|
||||
@@ -545,7 +571,7 @@ impl DocumentSession {
|
||||
.collect();
|
||||
for k in orphans {
|
||||
if let Some(id) = fresh.kernel_sessions.remove(&k) {
|
||||
let _ = foreign_session::close(&mut fresh.host, &Value::Text(id));
|
||||
let _ = foreign_session::close(&mut fresh.host, &Value::Handle(id));
|
||||
}
|
||||
}
|
||||
*self = fresh;
|
||||
@@ -590,8 +616,8 @@ impl DocumentSession {
|
||||
None,
|
||||
)?;
|
||||
let id = match id {
|
||||
Value::Text(s) => s,
|
||||
_ => return Err("session_open returned non-text id".into()),
|
||||
Value::Handle(s) => s,
|
||||
_ => return Err("session_open returned non-handle id".into()),
|
||||
};
|
||||
self.kernel_sessions
|
||||
.insert(kernel_name.to_string(), id.clone());
|
||||
@@ -618,23 +644,34 @@ impl DocumentSession {
|
||||
Some(Value::Int(n)) => n,
|
||||
_ => 0,
|
||||
};
|
||||
let lang = self
|
||||
.cell_index(cell)
|
||||
.map(|i| self.cells[i].lang.clone())
|
||||
let stdout = field("stdout")
|
||||
.and_then(|v| v.reveal_text().map(|s| s.to_string()))
|
||||
.unwrap_or_default();
|
||||
let (lang, source_hash) = self
|
||||
.cell_index(cell)
|
||||
.map(|i| {
|
||||
(
|
||||
self.cells[i].lang.clone(),
|
||||
self.cells[i].source_hash.clone(),
|
||||
)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let last_result = field("result")
|
||||
.filter(|v| !matches!(v, Value::None))
|
||||
.unwrap_or_else(|| {
|
||||
field("stdout")
|
||||
.and_then(|v| v.reveal_text().map(|s| Value::Text(s.to_string())))
|
||||
.unwrap_or(Value::None)
|
||||
});
|
||||
if let Some(i) = self.cell_index(cell) {
|
||||
let c = &mut self.cells[i];
|
||||
c.runs += 1;
|
||||
c.last_ms = ms;
|
||||
c.last_ok = ok;
|
||||
c.last_error = error.clone();
|
||||
c.last_result = field("result")
|
||||
.filter(|v| !matches!(v, Value::None))
|
||||
.unwrap_or_else(|| {
|
||||
field("stdout")
|
||||
.and_then(|v| v.reveal_text().map(|s| Value::Text(s.to_string())))
|
||||
.unwrap_or(Value::None)
|
||||
});
|
||||
c.last_stdout = stdout.clone();
|
||||
c.last_code = code;
|
||||
c.last_result = last_result.clone();
|
||||
c.status = if ok { CellStatus::Clean } else { CellStatus::Error };
|
||||
}
|
||||
self.seq += 1;
|
||||
@@ -642,13 +679,24 @@ impl DocumentSession {
|
||||
seq: self.seq,
|
||||
cell: cell.to_string(),
|
||||
kernel: kernel.to_string(),
|
||||
lang,
|
||||
lang: lang.clone(),
|
||||
ok,
|
||||
code,
|
||||
ms,
|
||||
timestamp_ms: now_ms(),
|
||||
error,
|
||||
});
|
||||
self.oracle_journal.push(OracleRecord {
|
||||
seq: self.seq,
|
||||
cell: cell.to_string(),
|
||||
kernel: kernel.to_string(),
|
||||
lang,
|
||||
source_hash,
|
||||
ok,
|
||||
code,
|
||||
stdout,
|
||||
result: last_result,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -727,7 +775,7 @@ impl DocumentSession {
|
||||
let source = Value::Text(self.cells[ci].source.clone());
|
||||
let record = foreign_session::eval_with_stdin(
|
||||
&mut self.host,
|
||||
&Value::Text(id),
|
||||
&Value::Handle(id),
|
||||
None,
|
||||
Some(&source),
|
||||
None,
|
||||
@@ -847,7 +895,7 @@ impl DocumentSession {
|
||||
let mut out = Vec::new();
|
||||
for name in ids {
|
||||
if let Some(id) = self.kernel_sessions.get(&name) {
|
||||
match foreign_session::interrupt(&mut self.host, &Value::Text(id.clone())) {
|
||||
match foreign_session::interrupt(&mut self.host, &Value::Handle(id.clone())) {
|
||||
Ok(_) => out.push(Value::Text(name)),
|
||||
Err(e) => out.push(Value::Text(format!("{name}: {e}"))),
|
||||
}
|
||||
@@ -858,7 +906,7 @@ impl DocumentSession {
|
||||
|
||||
pub fn restart_kernel(&mut self, name: &str) -> Result<Value, String> {
|
||||
if let Some(id) = self.kernel_sessions.remove(name) {
|
||||
let _ = foreign_session::close(&mut self.host, &Value::Text(id));
|
||||
let _ = foreign_session::close(&mut self.host, &Value::Handle(id));
|
||||
}
|
||||
for c in &mut self.cells {
|
||||
if c.kernel == name {
|
||||
@@ -938,6 +986,8 @@ impl DocumentSession {
|
||||
last_ms: 0,
|
||||
last_ok: false,
|
||||
last_error: String::new(),
|
||||
last_stdout: String::new(),
|
||||
last_code: 0,
|
||||
last_result: Value::None,
|
||||
});
|
||||
self.order.push(cell.to_string());
|
||||
@@ -974,7 +1024,7 @@ impl DocumentSession {
|
||||
let mut vars = Vec::new();
|
||||
if let Some(id) = live {
|
||||
if self.host.foreign_sessions.contains_key(&id) {
|
||||
if let Ok(names) = foreign_session::names(&mut self.host, &Value::Text(id)) {
|
||||
if let Ok(names) = foreign_session::names(&mut self.host, &Value::Handle(id)) {
|
||||
if let Value::List(xs) = names {
|
||||
vars = xs;
|
||||
}
|
||||
@@ -997,7 +1047,7 @@ impl DocumentSession {
|
||||
let id = self.ensure_kernel_session(kernel)?;
|
||||
foreign_session::set(
|
||||
&mut self.host,
|
||||
&Value::Text(id),
|
||||
&Value::Handle(id),
|
||||
&Value::Text(name.to_string()),
|
||||
&value,
|
||||
)?;
|
||||
@@ -1009,7 +1059,7 @@ impl DocumentSession {
|
||||
let id = self.ensure_kernel_session(kernel)?;
|
||||
let value = foreign_session::get(
|
||||
&mut self.host,
|
||||
&Value::Text(id),
|
||||
&Value::Handle(id),
|
||||
&Value::Text(name.to_string()),
|
||||
)?;
|
||||
self.value_store
|
||||
@@ -1095,10 +1145,14 @@ impl DocumentSession {
|
||||
self.cells[i].last_ms = 0;
|
||||
self.cells[i].last_ok = false;
|
||||
self.cells[i].last_error.clear();
|
||||
self.cells[i].last_stdout.clear();
|
||||
self.cells[i].last_code = 0;
|
||||
self.cells[i].last_result = Value::None;
|
||||
if self.cells[i].listed {
|
||||
self.cells[i].status = CellStatus::Never;
|
||||
}
|
||||
self.journal.retain(|j| j.cell != cell);
|
||||
self.oracle_journal.retain(|j| j.cell != cell);
|
||||
Ok(self.summary())
|
||||
}
|
||||
|
||||
@@ -1110,13 +1164,13 @@ impl DocumentSession {
|
||||
let mut values = Vec::new();
|
||||
if let Some(id) = self.kernel_sessions.get(&decl.name).cloned() {
|
||||
if let Ok(Value::List(names)) =
|
||||
foreign_session::names(&mut self.host, &Value::Text(id.clone()))
|
||||
foreign_session::names(&mut self.host, &Value::Handle(id.clone()))
|
||||
{
|
||||
for name in names {
|
||||
if let Value::Text(n) = &name {
|
||||
if let Ok(v) = foreign_session::get(
|
||||
&mut self.host,
|
||||
&Value::Text(id.clone()),
|
||||
&Value::Handle(id.clone()),
|
||||
&Value::Text(n.clone()),
|
||||
) {
|
||||
values.push(Value::Map(vec![
|
||||
@@ -1144,10 +1198,16 @@ impl DocumentSession {
|
||||
("lang".into(), Value::Text(c.lang.clone())),
|
||||
("kernel".into(), Value::Text(c.kernel.clone())),
|
||||
("source".into(), Value::Text(c.source.clone())),
|
||||
("source_hash".into(), Value::Text(c.source_hash.clone())),
|
||||
("stdin".into(), Value::Text(c.stdin.clone())),
|
||||
("status".into(), Value::Text(c.status.as_str().into())),
|
||||
("runs".into(), Value::Int(c.runs as i64)),
|
||||
("last_ok".into(), Value::Bool(c.last_ok)),
|
||||
// Recording fields: replay_check compares a later run against these.
|
||||
("ok".into(), Value::Bool(c.last_ok)),
|
||||
("code".into(), Value::Int(c.last_code)),
|
||||
("stdout".into(), Value::Text(c.last_stdout.clone())),
|
||||
("result".into(), c.last_result.clone()),
|
||||
])
|
||||
})
|
||||
.collect();
|
||||
@@ -1163,6 +1223,7 @@ impl DocumentSession {
|
||||
self.order.iter().cloned().map(Value::Text).collect(),
|
||||
)),
|
||||
("journal".into(), journal),
|
||||
("oracle_journal".into(), self.oracle_journal_json()),
|
||||
]))
|
||||
}
|
||||
|
||||
@@ -1208,7 +1269,7 @@ impl DocumentSession {
|
||||
let Some(value) = getv("value") else { continue };
|
||||
foreign_session::set(
|
||||
&mut self.host,
|
||||
&Value::Text(id.clone()),
|
||||
&Value::Handle(id.clone()),
|
||||
&Value::Text(vname),
|
||||
&value,
|
||||
)?;
|
||||
@@ -1243,6 +1304,8 @@ impl DocumentSession {
|
||||
}
|
||||
|
||||
/// Replay recorded cells in order, using the snapshot's per-cell stdin.
|
||||
/// Re-runs only; does not compare against the recording. Prefer
|
||||
/// [`Self::replay_check`] when equivalence against a recording matters.
|
||||
pub fn replay_snapshot(&mut self, snapshot: &Value) -> Result<Value, String> {
|
||||
let Value::Map(fields) = snapshot else {
|
||||
return Err("snapshot must be a map".into());
|
||||
@@ -1287,6 +1350,157 @@ impl DocumentSession {
|
||||
]))
|
||||
}
|
||||
|
||||
/// Replay cells and compare each step against the snapshot recording.
|
||||
///
|
||||
/// Expected fields per cell (when present): `ok`, `code`, `stdout`, `result`.
|
||||
/// A difference is localized to the first mismatched field on that cell.
|
||||
/// Cells without any recording fields are skipped for comparison but still run.
|
||||
pub fn replay_check(&mut self, snapshot: &Value) -> Result<Value, String> {
|
||||
let Value::Map(fields) = snapshot else {
|
||||
return Err("snapshot must be a map".into());
|
||||
};
|
||||
let cells = fields
|
||||
.iter()
|
||||
.find(|(k, _)| k == "cells")
|
||||
.and_then(|(_, v)| match v {
|
||||
Value::List(xs) => Some(xs.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.ok_or_else(|| "snapshot: missing cells".to_string())?;
|
||||
let mut checks = Vec::new();
|
||||
let mut matched = 0i64;
|
||||
let mut mismatched = 0i64;
|
||||
let mut skipped = 0i64;
|
||||
for item in cells {
|
||||
let Value::Map(cfields) = item else { continue };
|
||||
let getc = |name: &str| {
|
||||
cfields
|
||||
.iter()
|
||||
.find(|(k, _)| k == name)
|
||||
.map(|(_, v)| v.clone())
|
||||
};
|
||||
let Some(Value::Text(name)) = getc("name") else { continue };
|
||||
if self.cell_index(&name).is_none() {
|
||||
continue;
|
||||
}
|
||||
let has_recording = getc("ok").is_some()
|
||||
|| getc("code").is_some()
|
||||
|| getc("stdout").is_some()
|
||||
|| getc("result").is_some();
|
||||
let stdin = getc("stdin").and_then(|v| match v {
|
||||
Value::Text(s) if !s.is_empty() => Some(s),
|
||||
_ => None,
|
||||
});
|
||||
let record = self.run_cell_with_stdin(&name, None, stdin.as_deref())?;
|
||||
let observed_ok = match &record {
|
||||
Value::Map(m) => matches!(
|
||||
m.iter().find(|(k, _)| k == "ok").map(|(_, v)| v),
|
||||
Some(Value::Bool(true))
|
||||
),
|
||||
_ => false,
|
||||
};
|
||||
let observed_code = match &record {
|
||||
Value::Map(m) => match m.iter().find(|(k, _)| k == "code").map(|(_, v)| v) {
|
||||
Some(Value::Int(n)) => *n,
|
||||
_ => 0,
|
||||
},
|
||||
_ => 0,
|
||||
};
|
||||
let observed_stdout = match &record {
|
||||
Value::Map(m) => m
|
||||
.iter()
|
||||
.find(|(k, _)| k == "stdout")
|
||||
.and_then(|(_, v)| v.reveal_text().map(|s| s.to_string()))
|
||||
.unwrap_or_default(),
|
||||
_ => String::new(),
|
||||
};
|
||||
// Match record_result: prefer explicit result, else stdout as text.
|
||||
let observed_result = match &record {
|
||||
Value::Map(m) => {
|
||||
let explicit = m
|
||||
.iter()
|
||||
.find(|(k, _)| k == "result")
|
||||
.map(|(_, v)| v.clone())
|
||||
.filter(|v| !matches!(v, Value::None));
|
||||
explicit.unwrap_or_else(|| {
|
||||
m.iter()
|
||||
.find(|(k, _)| k == "stdout")
|
||||
.and_then(|(_, v)| v.reveal_text().map(|s| Value::Text(s.to_string())))
|
||||
.unwrap_or(Value::None)
|
||||
})
|
||||
}
|
||||
_ => Value::None,
|
||||
};
|
||||
if !has_recording {
|
||||
skipped += 1;
|
||||
checks.push(Value::Map(vec![
|
||||
("cell".into(), Value::Text(name)),
|
||||
("match".into(), Value::Bool(true)),
|
||||
("compared".into(), Value::Bool(false)),
|
||||
("diff".into(), Value::Text(String::new())),
|
||||
]));
|
||||
continue;
|
||||
}
|
||||
let mut diff = String::new();
|
||||
if let Some(Value::Bool(exp_ok)) = getc("ok") {
|
||||
if exp_ok != observed_ok {
|
||||
diff = format!("ok: expected {exp_ok}, got {observed_ok}");
|
||||
}
|
||||
}
|
||||
if diff.is_empty() {
|
||||
if let Some(Value::Int(exp_code)) = getc("code") {
|
||||
if exp_code != observed_code {
|
||||
diff = format!("code: expected {exp_code}, got {observed_code}");
|
||||
}
|
||||
}
|
||||
}
|
||||
if diff.is_empty() {
|
||||
if let Some(Value::Text(exp_stdout)) = getc("stdout") {
|
||||
if exp_stdout != observed_stdout {
|
||||
diff = format!(
|
||||
"stdout: expected {:?}, got {:?}",
|
||||
exp_stdout, observed_stdout
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if diff.is_empty() {
|
||||
if let Some(exp_result) = getc("result") {
|
||||
if exp_result != observed_result {
|
||||
diff = format!(
|
||||
"result: expected {}, got {}",
|
||||
exp_result.as_display(),
|
||||
observed_result.as_display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
let is_match = diff.is_empty();
|
||||
if is_match {
|
||||
matched += 1;
|
||||
} else {
|
||||
mismatched += 1;
|
||||
}
|
||||
checks.push(Value::Map(vec![
|
||||
("cell".into(), Value::Text(name)),
|
||||
("match".into(), Value::Bool(is_match)),
|
||||
("compared".into(), Value::Bool(true)),
|
||||
("diff".into(), Value::Text(diff)),
|
||||
("observed_ok".into(), Value::Bool(observed_ok)),
|
||||
("observed_code".into(), Value::Int(observed_code)),
|
||||
("observed_stdout".into(), Value::Text(observed_stdout)),
|
||||
("observed_result".into(), observed_result),
|
||||
]));
|
||||
}
|
||||
Ok(Value::Map(vec![
|
||||
("ok".into(), Value::Bool(mismatched == 0)),
|
||||
("matched".into(), Value::Int(matched)),
|
||||
("mismatched".into(), Value::Int(mismatched)),
|
||||
("skipped".into(), Value::Int(skipped)),
|
||||
("checks".into(), Value::List(checks)),
|
||||
]))
|
||||
}
|
||||
|
||||
/// Explicitly persist the last cell result through lib/writeback.
|
||||
pub fn persist_cell(&mut self, cell: &str, key: &str) -> Result<Value, String> {
|
||||
let i = self
|
||||
@@ -1315,11 +1529,15 @@ impl DocumentSession {
|
||||
cell.last_ms = 0;
|
||||
cell.last_ok = false;
|
||||
cell.last_error.clear();
|
||||
cell.last_stdout.clear();
|
||||
cell.last_code = 0;
|
||||
cell.last_result = Value::None;
|
||||
if cell.listed {
|
||||
cell.status = CellStatus::Never;
|
||||
}
|
||||
}
|
||||
self.journal.clear();
|
||||
self.oracle_journal.clear();
|
||||
self.cursor = 0;
|
||||
self.summary()
|
||||
}
|
||||
@@ -1345,10 +1563,31 @@ impl DocumentSession {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn oracle_journal_json(&self) -> Value {
|
||||
Value::List(
|
||||
self.oracle_journal
|
||||
.iter()
|
||||
.map(|j| {
|
||||
Value::Map(vec![
|
||||
("seq".into(), Value::Int(j.seq as i64)),
|
||||
("cell".into(), Value::Text(j.cell.clone())),
|
||||
("kernel".into(), Value::Text(j.kernel.clone())),
|
||||
("lang".into(), Value::Text(j.lang.clone())),
|
||||
("source_hash".into(), Value::Text(j.source_hash.clone())),
|
||||
("ok".into(), Value::Bool(j.ok)),
|
||||
("code".into(), Value::Int(j.code)),
|
||||
("stdout".into(), Value::Text(j.stdout.clone())),
|
||||
("result".into(), j.result.clone()),
|
||||
])
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
let ids: Vec<String> = self.kernel_sessions.values().cloned().collect();
|
||||
for id in ids {
|
||||
let _ = foreign_session::close(&mut self.host, &Value::Text(id));
|
||||
let _ = foreign_session::close(&mut self.host, &Value::Handle(id));
|
||||
}
|
||||
self.kernel_sessions.clear();
|
||||
}
|
||||
@@ -2066,4 +2305,111 @@ console.log("after error");
|
||||
s.close();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_journal_records_stdout_separate_from_execution_journal() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join("tests/notebook/shared.mq.md");
|
||||
let mut s = DocumentSession::open(&path, &root).unwrap();
|
||||
s.run_all(true).unwrap();
|
||||
assert!(!s.journal.is_empty());
|
||||
assert_eq!(s.oracle_journal.len(), s.journal.len());
|
||||
let read = s
|
||||
.oracle_journal
|
||||
.iter()
|
||||
.find(|j| j.cell == "读取")
|
||||
.expect("oracle entry for 读取");
|
||||
assert!(read.ok);
|
||||
assert!(
|
||||
read.stdout.contains('4') || read.stdout.contains("42"),
|
||||
"oracle must capture stdout, got {:?}",
|
||||
read.stdout
|
||||
);
|
||||
assert!(!read.source_hash.is_empty());
|
||||
// Execution journal must not pretend to be the oracle (no stdout field).
|
||||
let exec = s.journal.iter().find(|j| j.cell == "读取").unwrap();
|
||||
assert_eq!(exec.ok, read.ok);
|
||||
s.close();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replay_check_matches_exported_recording() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join("tests/notebook/shared.mq.md");
|
||||
let mut a = DocumentSession::open(&path, &root).unwrap();
|
||||
a.run_all(true).unwrap();
|
||||
let snap = a.export_snapshot().unwrap();
|
||||
a.close();
|
||||
|
||||
let mut b = DocumentSession::open(&path, &root).unwrap();
|
||||
let check = b.replay_check(&snap).unwrap();
|
||||
assert_eq!(field(&check, "ok"), Some(Value::Bool(true)));
|
||||
assert_eq!(field(&check, "mismatched"), Some(Value::Int(0)));
|
||||
let matched = field(&check, "matched").and_then(|v| match v {
|
||||
Value::Int(n) => Some(n),
|
||||
_ => None,
|
||||
});
|
||||
assert!(matched.unwrap_or(0) >= 1, "expected at least one compared cell");
|
||||
b.close();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replay_check_localizes_stdout_mismatch() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join("tests/notebook/shared.mq.md");
|
||||
let mut a = DocumentSession::open(&path, &root).unwrap();
|
||||
a.run_all(true).unwrap();
|
||||
let mut snap = a.export_snapshot().unwrap();
|
||||
a.close();
|
||||
|
||||
// Tamper with the recording for cell 读取.
|
||||
if let Value::Map(ref mut fields) = snap {
|
||||
if let Some((_, Value::List(cells))) =
|
||||
fields.iter_mut().find(|(k, _)| k == "cells")
|
||||
{
|
||||
for cell in cells.iter_mut() {
|
||||
let Value::Map(cfields) = cell else { continue };
|
||||
let name = cfields
|
||||
.iter()
|
||||
.find(|(k, _)| k == "name")
|
||||
.and_then(|(_, v)| v.reveal_text().map(|s| s.to_string()));
|
||||
if name.as_deref() == Some("读取") {
|
||||
if let Some((_, v)) = cfields.iter_mut().find(|(k, _)| k == "stdout") {
|
||||
*v = Value::Text("wrong-recording\n".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut b = DocumentSession::open(&path, &root).unwrap();
|
||||
let check = b.replay_check(&snap).unwrap();
|
||||
assert_eq!(field(&check, "ok"), Some(Value::Bool(false)));
|
||||
assert_eq!(field(&check, "mismatched"), Some(Value::Int(1)));
|
||||
let Some(Value::List(checks)) = field(&check, "checks") else {
|
||||
panic!("missing checks");
|
||||
};
|
||||
let bad = checks.iter().find(|c| {
|
||||
field(c, "cell")
|
||||
.and_then(|v| v.reveal_text().map(|s| s.to_string()))
|
||||
.as_deref()
|
||||
== Some("读取")
|
||||
});
|
||||
let Some(bad) = bad else { panic!("missing 读取 check") };
|
||||
assert_eq!(field(bad, "match"), Some(Value::Bool(false)));
|
||||
let diff = field(bad, "diff")
|
||||
.and_then(|v| v.reveal_text().map(|s| s.to_string()))
|
||||
.unwrap_or_default();
|
||||
assert!(diff.contains("stdout"), "diff should localize to stdout: {diff}");
|
||||
b.close();
|
||||
}
|
||||
}
|
||||
+87
-13
@@ -22,8 +22,35 @@ const MAX_PROCESS_STDERR: usize = 1_048_576;
|
||||
|
||||
const PY_BOOTSTRAP: &str = r#"import sys, json, io, ast, traceback, contextlib
|
||||
_out = sys.stdout
|
||||
_MAX_SAFE = 9007199254740991
|
||||
def _sanitize(o):
|
||||
if o is None or isinstance(o, bool):
|
||||
return o
|
||||
if isinstance(o, int):
|
||||
if abs(o) > _MAX_SAFE:
|
||||
raise ValueError("json: integer outside V_safe (±(2^53-1)): %s" % o)
|
||||
return o
|
||||
if isinstance(o, float):
|
||||
return o
|
||||
if isinstance(o, str):
|
||||
return o
|
||||
if isinstance(o, (list, tuple)):
|
||||
return [_sanitize(x) for x in o]
|
||||
if isinstance(o, dict):
|
||||
return {str(k): _sanitize(v) for k, v in o.items()}
|
||||
raise TypeError("not JSON-safe: %s" % type(o).__name__)
|
||||
def _send(o):
|
||||
_out.write(json.dumps(o, ensure_ascii=False, default=str) + "\n")
|
||||
try:
|
||||
line = json.dumps(_sanitize(o), ensure_ascii=False)
|
||||
except Exception as e:
|
||||
rid = o.get("id") if isinstance(o, dict) else None
|
||||
line = json.dumps({
|
||||
"id": rid, "ok": False,
|
||||
"stdout": (o.get("stdout") if isinstance(o, dict) else "") or "",
|
||||
"stderr": (o.get("stderr") if isinstance(o, dict) else "") or "",
|
||||
"result": None, "error": str(e),
|
||||
}, ensure_ascii=False)
|
||||
_out.write(line + "\n")
|
||||
_out.flush()
|
||||
_g = {"__name__": "__main__", "marqdo_input": None}
|
||||
for _line in sys.stdin:
|
||||
@@ -81,10 +108,40 @@ const JS_BOOTSTRAP: &str = r#"const readline = require("readline");
|
||||
const util = require("util");
|
||||
const vm = require("vm");
|
||||
const out = process.stdout.write.bind(process.stdout);
|
||||
const MAX_SAFE = Number.MAX_SAFE_INTEGER;
|
||||
globalThis.require = require;
|
||||
const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
|
||||
function send(o) { out(JSON.stringify(o) + "\n"); }
|
||||
function toJSON(v) { try { return JSON.parse(JSON.stringify(v)); } catch (e) { return String(v); } }
|
||||
function sanitize(v) {
|
||||
if (v === null || typeof v === "boolean" || typeof v === "string") return v;
|
||||
if (typeof v === "number") {
|
||||
if (Number.isInteger(v) && Math.abs(v) > MAX_SAFE)
|
||||
throw new Error("json: integer outside V_safe (±(2^53-1)): " + v);
|
||||
return v;
|
||||
}
|
||||
if (typeof v === "bigint") {
|
||||
if (v > BigInt(MAX_SAFE) || v < -BigInt(MAX_SAFE))
|
||||
throw new Error("json: integer outside V_safe (±(2^53-1)): " + v.toString());
|
||||
return Number(v);
|
||||
}
|
||||
if (Array.isArray(v)) return v.map(sanitize);
|
||||
if (v && typeof v === "object") {
|
||||
const o = {};
|
||||
for (const k of Object.keys(v)) o[k] = sanitize(v[k]);
|
||||
return o;
|
||||
}
|
||||
throw new Error("not JSON-safe: " + (typeof v));
|
||||
}
|
||||
function send(o) {
|
||||
try { out(JSON.stringify(sanitize(o)) + "\n"); }
|
||||
catch (e) {
|
||||
out(JSON.stringify({
|
||||
id: o && o.id !== undefined ? o.id : null, ok: false,
|
||||
stdout: (o && o.stdout) || "", stderr: (o && o.stderr) || "",
|
||||
result: null, error: String(e && e.message ? e.message : e)
|
||||
}) + "\n");
|
||||
}
|
||||
}
|
||||
function toJSON(v) { return sanitize(v); }
|
||||
async function capture(fn) {
|
||||
const logs = [], errs = [];
|
||||
const ow = process.stdout.write.bind(process.stdout);
|
||||
@@ -182,6 +239,15 @@ fn opt_text(v: Option<&Value>) -> Result<Option<String>, String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Session ids are opaque handles — text fabrications are refused.
|
||||
fn as_session_id<'a>(id: &'a Value) -> Result<&'a str, String> {
|
||||
match id {
|
||||
Value::Handle(s) => Ok(s.as_str()),
|
||||
Value::Text(_) => Err("session id must be a handle (not text)".into()),
|
||||
_ => Err("session id must be a handle".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_timeout(v: Option<&Value>, default: f64) -> Result<Duration, String> {
|
||||
let secs = match v {
|
||||
None | Some(Value::None) => default,
|
||||
@@ -418,7 +484,7 @@ pub fn open(
|
||||
};
|
||||
ctx.foreign_sessions.insert(id.clone(), session);
|
||||
let _ = timeout;
|
||||
Ok(Value::Text(id))
|
||||
Ok(Value::Handle(id))
|
||||
}
|
||||
|
||||
fn session_dead_stderr(ctx: &HostContext, id: &str) -> String {
|
||||
@@ -453,7 +519,7 @@ pub fn eval_with_stdin(
|
||||
timeout: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
ensure_config(ctx);
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let id = as_session_id(id)?.to_string();
|
||||
if !ctx.foreign_sessions.contains_key(&id) {
|
||||
return Err(format!("foreign session `{id}` not found"));
|
||||
}
|
||||
@@ -544,6 +610,10 @@ pub fn eval_with_stdin(
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
// Ω ∉ V_safe is a language fault, not a soft cell failure.
|
||||
if error.contains("outside V_safe") {
|
||||
return Err(error);
|
||||
}
|
||||
let result = reply
|
||||
.get("result")
|
||||
.map(json_to_value)
|
||||
@@ -650,7 +720,7 @@ fn kill_session(ctx: &mut HostContext, id: &str) {
|
||||
/// Interrupt the current evaluation without destroying the kernel. Python
|
||||
/// reports KeyboardInterrupt through the normal result frame.
|
||||
pub fn interrupt(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let id = as_session_id(id)?.to_string();
|
||||
let session = ctx
|
||||
.foreign_sessions
|
||||
.get_mut(&id)
|
||||
@@ -675,7 +745,7 @@ pub fn set(
|
||||
name: &Value,
|
||||
value: &Value,
|
||||
) -> Result<Value, String> {
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let id = as_session_id(id)?.to_string();
|
||||
let name = as_text(name, "name")?.to_string();
|
||||
if !valid_name(&name) {
|
||||
return Err("session_set: name must be an identifier".into());
|
||||
@@ -695,7 +765,7 @@ pub fn set(
|
||||
}
|
||||
|
||||
pub fn get(ctx: &mut HostContext, id: &Value, name: &Value) -> Result<Value, String> {
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let id = as_session_id(id)?.to_string();
|
||||
let name = as_text(name, "name")?.to_string();
|
||||
if !valid_name(&name) {
|
||||
return Err("session_get: name must be an identifier".into());
|
||||
@@ -716,7 +786,7 @@ pub fn get(ctx: &mut HostContext, id: &Value, name: &Value) -> Result<Value, Str
|
||||
}
|
||||
|
||||
pub fn names(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let id = as_session_id(id)?.to_string();
|
||||
let request = serde_json::json!({"id": 1, "op": "names"});
|
||||
let reply = send_and_wait(ctx, &id, &request, Duration::from_secs(10))?;
|
||||
if !reply.get("ok").and_then(|v| v.as_bool()).unwrap_or(false) {
|
||||
@@ -733,10 +803,14 @@ pub fn names(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
|
||||
}
|
||||
|
||||
pub fn close(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let existed = ctx.foreign_sessions.contains_key(&id);
|
||||
let id = as_session_id(id)?.to_string();
|
||||
// Affine close: consuming an absent / already-closed handle is a fault
|
||||
// (same surface as eval/set/get after close).
|
||||
if !ctx.foreign_sessions.contains_key(&id) {
|
||||
return Err(format!("foreign session `{id}` not found"));
|
||||
}
|
||||
kill_session(ctx, &id);
|
||||
Ok(Value::Bool(existed))
|
||||
Ok(Value::Bool(true))
|
||||
}
|
||||
|
||||
pub fn list(ctx: &HostContext) -> Result<Value, String> {
|
||||
@@ -745,7 +819,7 @@ pub fn list(ctx: &HostContext) -> Result<Value, String> {
|
||||
let mut out = Vec::new();
|
||||
for s in sessions {
|
||||
out.push(Value::Map(vec![
|
||||
("id".into(), Value::Text(s.id.clone())),
|
||||
("id".into(), Value::Handle(s.id.clone())),
|
||||
("lang".into(), Value::Text(s.lang.clone())),
|
||||
("cmd".into(), Value::Text(s.cmd.clone())),
|
||||
("evals".into(), Value::Int(s.evals as i64)),
|
||||
|
||||
@@ -138,6 +138,22 @@ pub(crate) fn value_to_json(v: &Value) -> Result<serde_json::Value, String> {
|
||||
serde_json::Value::Array(arr)
|
||||
}
|
||||
Value::Map(entries) => {
|
||||
// Column-table maps (all fields lists): refuse unequal list lengths.
|
||||
let list_lens: Vec<usize> = entries
|
||||
.iter()
|
||||
.filter_map(|(_, v)| match v {
|
||||
Value::List(xs) => Some(xs.len()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
if list_lens.len() == entries.len() && list_lens.len() >= 2 {
|
||||
let n0 = list_lens[0];
|
||||
if list_lens.iter().any(|&n| n != n0) {
|
||||
return Err(
|
||||
"json: table column arity mismatch (unequal list lengths)".into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut map = serde_json::Map::new();
|
||||
for (k, v) in entries {
|
||||
map.insert(k.clone(), value_to_json(v)?);
|
||||
@@ -173,6 +189,9 @@ pub(crate) fn value_to_json(v: &Value) -> Result<serde_json::Value, String> {
|
||||
},
|
||||
Value::Code(c) => serde_json::Value::String(c.source.clone()),
|
||||
Value::Secret(_) => serde_json::Value::String("<secret>".into()),
|
||||
Value::Handle(_) => {
|
||||
return Err("json: session handle cannot be serialized".into());
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -264,4 +283,23 @@ mod tests {
|
||||
let err = json_to_value(&j).unwrap_err();
|
||||
assert!(err.contains("outside V_safe"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuse_session_handle_encode() {
|
||||
let err = value_to_json(&Value::Handle("s1".into())).unwrap_err();
|
||||
assert!(
|
||||
err.contains("session handle cannot be serialized"),
|
||||
"{err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuse_unequal_column_table_encode() {
|
||||
let ragged = Value::Map(vec![
|
||||
("a".into(), Value::List(vec![Value::Int(1), Value::Int(2)])),
|
||||
("b".into(), Value::List(vec![Value::Int(3)])),
|
||||
]);
|
||||
let err = value_to_json(&ragged).unwrap_err();
|
||||
assert!(err.contains("arity mismatch"), "{err}");
|
||||
}
|
||||
}
|
||||
@@ -891,6 +891,24 @@ impl<'a> Cursor<'a> {
|
||||
if !header_done {
|
||||
bail!("expected GFM table after empty assignment");
|
||||
}
|
||||
// Strong / Surface: unequal row arity is stuck — no silent pad/truncate.
|
||||
for (line_no, cells) in &data_rows {
|
||||
if cells.len() != header.len() {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
Span {
|
||||
line: *line_no,
|
||||
col: 1,
|
||||
},
|
||||
format!(
|
||||
"table row arity mismatch: expected {} columns, got {}",
|
||||
header.len(),
|
||||
cells.len()
|
||||
),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
let row_oriented = header
|
||||
.first()
|
||||
.map(|h| is_row_table_marker(h))
|
||||
|
||||
@@ -29,6 +29,8 @@ pub enum Value {
|
||||
Code(CodeBlock),
|
||||
/// Sensitive text from `${secret.*}` — Display shows `<secret>`; reveal on Text concat.
|
||||
Secret(String),
|
||||
/// Opaque foreign-session handle. Not JSON-serializable; not printable structure.
|
||||
Handle(String),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@@ -40,6 +42,7 @@ impl Value {
|
||||
Value::Num(n) => *n != 0.0 && !n.is_nan(),
|
||||
Value::Text(s) => !s.is_empty(),
|
||||
Value::Secret(s) => !s.is_empty(),
|
||||
Value::Handle(s) => !s.is_empty(),
|
||||
Value::List(xs) => !xs.is_empty(),
|
||||
Value::Map(xs) => !xs.is_empty(),
|
||||
Value::Formula(_) => true,
|
||||
@@ -64,6 +67,7 @@ impl Value {
|
||||
Value::Num(n) => crate::formula::format_num(*n),
|
||||
Value::Text(s) => s.clone(),
|
||||
Value::Secret(_) => "<secret>".into(),
|
||||
Value::Handle(_) => "<handle>".into(),
|
||||
Value::List(xs) => {
|
||||
let parts: Vec<String> = xs.iter().map(|v| v.as_display()).collect();
|
||||
format!("[{}]", parts.join(", "))
|
||||
|
||||
@@ -199,6 +199,7 @@ const API_ROUTES: &[(&str, ApiRoute)] = &[
|
||||
("/api/session/snapshot/export", ApiRoute::Notebook(notebook_api::api_export_snapshot)),
|
||||
("/api/session/snapshot/import", ApiRoute::Notebook(notebook_api::api_import_snapshot)),
|
||||
("/api/session/snapshot/replay", ApiRoute::Notebook(notebook_api::api_replay_snapshot)),
|
||||
("/api/session/snapshot/replay_check", ApiRoute::Notebook(notebook_api::api_replay_check)),
|
||||
("/api/session/plan/assign", ApiRoute::Notebook(notebook_api::api_plan_assign)),
|
||||
("/api/session/plan/add", ApiRoute::Notebook(notebook_api::api_plan_add)),
|
||||
("/api/session/plan/remove", ApiRoute::Notebook(notebook_api::api_plan_remove)),
|
||||
@@ -216,6 +217,7 @@ const API_ROUTES: &[(&str, ApiRoute)] = &[
|
||||
("/api/session/state/push", ApiRoute::Notebook(notebook_api::api_state_push)),
|
||||
("/api/session/state/pull", ApiRoute::Notebook(notebook_api::api_state_pull)),
|
||||
("/api/session/journal", ApiRoute::Notebook(notebook_api::api_journal)),
|
||||
("/api/session/oracle_journal", ApiRoute::Notebook(notebook_api::api_oracle_journal)),
|
||||
("/api/debug/start", ApiRoute::Notebook(debug_api::api_debug_start)),
|
||||
("/api/debug/continue", ApiRoute::Action(crate::debug::DebugAction::Continue)),
|
||||
("/api/debug/step", ApiRoute::Action(crate::debug::DebugAction::Step)),
|
||||
|
||||
@@ -208,6 +208,16 @@ pub fn api_replay_snapshot(root: &Path, body: &str) -> serde_json::Value {
|
||||
with_session(root, body, "replay", |s| s.replay_snapshot(&snapshot))
|
||||
}
|
||||
|
||||
pub fn api_replay_check(root: &Path, body: &str) -> serde_json::Value {
|
||||
let v = body_value(body);
|
||||
let snapshot = v.get("snapshot").cloned().unwrap_or(serde_json::Value::Null);
|
||||
let snapshot = match json_to_value(&snapshot) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return err(e),
|
||||
};
|
||||
with_session(root, body, "replay_check", |s| s.replay_check(&snapshot))
|
||||
}
|
||||
|
||||
pub fn api_clear_cell(root: &Path, body: &str) -> serde_json::Value {
|
||||
let v = body_value(body);
|
||||
let cell = v.get("cell").and_then(|x| x.as_str()).unwrap_or("");
|
||||
@@ -352,6 +362,10 @@ pub fn api_journal(root: &Path, body: &str) -> serde_json::Value {
|
||||
with_session(root, body, "journal", |s| Ok(s.journal_json()))
|
||||
}
|
||||
|
||||
pub fn api_oracle_journal(root: &Path, body: &str) -> serde_json::Value {
|
||||
with_session(root, body, "oracle_journal", |s| Ok(s.oracle_journal_json()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -542,6 +542,7 @@ fn value_kind_label(v: &crate::value::Value) -> String {
|
||||
Value::Num(_) => "num".into(),
|
||||
Value::Text(_) => "text".into(),
|
||||
Value::Secret(_) => "secret".into(),
|
||||
Value::Handle(_) => "handle".into(),
|
||||
Value::Formula(crate::formula::Expr::Matrix { rows, .. }) => {
|
||||
format!("formula · {}×{}", rows.len(), rows.first().map(|r| r.len()).unwrap_or(0))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user