From 8f1010f967bc43241e213dfeb403bb2951e5235a Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 6 Nov 2026 15:04:27 +0800 Subject: [PATCH] Ship oracle journal values, bytecode/frontend hardening, and replay-policy Lean. Host exposes oracle journals as language values with refuse/copy gates; bytecode and foreign session paths get modularized; applications and verification replay-policy sources land with Lake builds ignored. Co-authored-by: Cursor --- .gitignore | 3 + Cargo.toml | 2 +- ...eign-library-paper-direction-2026-11-05.md | 24 + doc/design/foreign-strong-correspondence.md | 19 + .../frontend-call-semantics-2026-11-03.md | 336 +++ doc/design/notebook-session.md | 30 + doc/design/oracle-journal-values.md | 131 + doc/design/v-safe-json.md | 14 +- lib/foreign.mq.md | 17 + src/application_tests.rs | 293 ++ src/arguments.rs | 122 + src/ast.rs | 60 +- src/builtin.rs | 30 +- src/builtin/primitives.rs | 88 + src/bytecode/call.rs | 327 +++ src/bytecode/compile.rs | 861 +++--- src/bytecode/expression.rs | 439 +++ src/bytecode/mod.rs | 87 +- src/bytecode/names.rs | 172 ++ src/bytecode/source.rs | 366 +++ src/bytecode/statement.rs | 365 +++ src/bytecode/verify.rs | 331 +++ src/bytecode/vm.rs | 705 +++-- src/call.rs | 11 +- src/causal_replay_tests.rs | 235 ++ src/contract.rs | 24 +- src/diagnostics.rs | 87 +- src/frontend_elaboration_tests.rs | 2117 ++++++++++++++ src/host/dispatch.rs | 31 +- src/host/foreign.rs | 109 +- src/host/foreign_doc.rs | 641 ++-- src/host/foreign_doc/control.rs | 174 ++ src/host/foreign_doc/packing.rs | 399 +++ src/host/foreign_doc/replay.rs | 2581 +++++++++++++++++ src/host/foreign_session.rs | 493 +++- src/host/foreign_session/control.rs | 204 ++ src/host/foreign_session/validation.rs | 173 ++ src/host/foreign_session/validation/policy.rs | 92 + src/host/json.rs | 132 +- src/host/mod.rs | 15 + src/host/oracle.rs | 136 + src/host/plugin.rs | 11 + src/inherit.rs | 164 +- src/interp.rs | 475 +-- src/lib.rs | 11 + src/load.rs | 38 +- src/parse/expr.rs | 14 + src/parse/mod.rs | 30 +- src/sema/bind.rs | 198 +- src/sema/ir.rs | 5 + src/sema/mod.rs | 1 + src/sema/resolution.rs | 254 ++ src/source_map_tests.rs | 577 ++++ src/source_refinement_tests.rs | 452 +++ src/value.rs | 39 + src/view/notebook_api.rs | 206 +- src/view/notebook_api/interrupt_http_tests.rs | 296 ++ tests/applications/CASES.json | 1960 +++++++++++++ tests/applications/README.md | 34 + tests/applications/concordance/consumer.js | 10 + tests/applications/concordance/consumer.py | 10 + tests/applications/concordance/producer.js | 3 + tests/applications/concordance/producer.py | 6 + tests/applications/data/PROVENANCE.json | 21 + tests/applications/data/wine-quality.zip | Bin 0 -> 91353 bytes tests/applications/data/winequality-red.csv | 1600 ++++++++++ tests/applications/data/winequality.names | 72 + tests/applications/invoice/consumer.js | 10 + tests/applications/invoice/consumer.py | 8 + tests/applications/invoice/producer.js | 10 + tests/applications/invoice/producer.py | 8 + tests/applications/routes/consumer.js | 14 + tests/applications/routes/consumer.py | 17 + tests/applications/routes/producer.js | 11 + tests/applications/routes/producer.py | 8 + tests/applications/wine/consumer.js | 9 + tests/applications/wine/consumer.py | 7 + tests/applications/wine/producer.js | 13 + tests/applications/wine/producer.py | 5 + tests/errors/lexical-private-descendant.mq.md | 11 + tests/gold.rs | 92 +- tests/lib/foreign-copy-code-refuse.mq.md | 15 + tests/lib/foreign-oracle-failure.mq.md | 17 + tests/lib/foreign-oracle-journal.mq.md | 23 + tests/lib/foreign-oracle-names.mq.md | 16 + .../foreign-session-copy-code-refuse.mq.md | 16 + tests/oracle_journal.rs | 46 + tests/structure/lexical-sibling-calls.mq.md | 47 + tests/structure/self-method-paths.mq.md | 38 + .../replay-policy/BUILD-IDENTITY.json | 12 + verification/replay-policy/Main.lean | 69 + verification/replay-policy/POLICY-SHA256 | 1 + .../replay-policy/PolicyIdentity.lean | 3 + verification/replay-policy/README.md | 30 + verification/replay-policy/ReplayPolicy.lean | 1 + .../replay-policy/ReplayPolicy/Cursor.lean | 132 + .../replay-policy/ReplayPolicy/Identity.lean | 3 + verification/replay-policy/build_policy.py | 20 + verification/replay-policy/lake-manifest.json | 6 + verification/replay-policy/lakefile.lean | 7 + verification/replay-policy/lean-toolchain | 1 + 101 files changed, 17962 insertions(+), 1727 deletions(-) create mode 100644 doc/design/foreign-library-paper-direction-2026-11-05.md create mode 100644 doc/design/frontend-call-semantics-2026-11-03.md create mode 100644 doc/design/oracle-journal-values.md create mode 100644 src/application_tests.rs create mode 100644 src/arguments.rs create mode 100644 src/builtin/primitives.rs create mode 100644 src/bytecode/call.rs create mode 100644 src/bytecode/expression.rs create mode 100644 src/bytecode/names.rs create mode 100644 src/bytecode/source.rs create mode 100644 src/bytecode/statement.rs create mode 100644 src/bytecode/verify.rs create mode 100644 src/causal_replay_tests.rs create mode 100644 src/frontend_elaboration_tests.rs create mode 100644 src/host/foreign_doc/control.rs create mode 100644 src/host/foreign_doc/packing.rs create mode 100644 src/host/foreign_doc/replay.rs create mode 100644 src/host/foreign_session/control.rs create mode 100644 src/host/foreign_session/validation.rs create mode 100644 src/host/foreign_session/validation/policy.rs create mode 100644 src/host/oracle.rs create mode 100644 src/sema/resolution.rs create mode 100644 src/source_map_tests.rs create mode 100644 src/source_refinement_tests.rs create mode 100644 src/view/notebook_api/interrupt_http_tests.rs create mode 100644 tests/applications/CASES.json create mode 100644 tests/applications/README.md create mode 100644 tests/applications/concordance/consumer.js create mode 100644 tests/applications/concordance/consumer.py create mode 100644 tests/applications/concordance/producer.js create mode 100644 tests/applications/concordance/producer.py create mode 100644 tests/applications/data/PROVENANCE.json create mode 100644 tests/applications/data/wine-quality.zip create mode 100644 tests/applications/data/winequality-red.csv create mode 100644 tests/applications/data/winequality.names create mode 100644 tests/applications/invoice/consumer.js create mode 100644 tests/applications/invoice/consumer.py create mode 100644 tests/applications/invoice/producer.js create mode 100644 tests/applications/invoice/producer.py create mode 100644 tests/applications/routes/consumer.js create mode 100644 tests/applications/routes/consumer.py create mode 100644 tests/applications/routes/producer.js create mode 100644 tests/applications/routes/producer.py create mode 100644 tests/applications/wine/consumer.js create mode 100644 tests/applications/wine/consumer.py create mode 100644 tests/applications/wine/producer.js create mode 100644 tests/applications/wine/producer.py create mode 100644 tests/errors/lexical-private-descendant.mq.md create mode 100644 tests/lib/foreign-copy-code-refuse.mq.md create mode 100644 tests/lib/foreign-oracle-failure.mq.md create mode 100644 tests/lib/foreign-oracle-journal.mq.md create mode 100644 tests/lib/foreign-oracle-names.mq.md create mode 100644 tests/lib/foreign-session-copy-code-refuse.mq.md create mode 100644 tests/oracle_journal.rs create mode 100644 tests/structure/lexical-sibling-calls.mq.md create mode 100644 tests/structure/self-method-paths.mq.md create mode 100644 verification/replay-policy/BUILD-IDENTITY.json create mode 100644 verification/replay-policy/Main.lean create mode 100644 verification/replay-policy/POLICY-SHA256 create mode 100644 verification/replay-policy/PolicyIdentity.lean create mode 100644 verification/replay-policy/README.md create mode 100644 verification/replay-policy/ReplayPolicy.lean create mode 100644 verification/replay-policy/ReplayPolicy/Cursor.lean create mode 100644 verification/replay-policy/ReplayPolicy/Identity.lean create mode 100644 verification/replay-policy/build_policy.py create mode 100644 verification/replay-policy/lake-manifest.json create mode 100644 verification/replay-policy/lakefile.lean create mode 100644 verification/replay-policy/lean-toolchain diff --git a/.gitignore b/.gitignore index beaacaf..4597071 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,9 @@ plugins/*/tmp/ # Plot SVGs emitted beside tests / notebooks when demos run /tests/**/*-plot-*.svg +# Lean Lake build outputs (sources under verification/ stay tracked) +**/.lake/ + # PLDI / thesis harness scratch must never land in this tree .lx*-tmp/ diff --git a/Cargo.toml b/Cargo.toml index fa3da59..ceb68e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ chrono = { version = "0.4", default-features = false, features = ["clock", "std" clap = { version = "=4.4.18", features = ["derive"], optional = true } pulldown-cmark = "0.12" serde = { version = "1", features = ["derive"] } -serde_json = "1" +serde_json = { version = "1", features = ["float_roundtrip"] } thiserror = "1" tiny_http = { version = "0.12", optional = true } sha2 = { version = "=0.10.9", default-features = false } diff --git a/doc/design/foreign-library-paper-direction-2026-11-05.md b/doc/design/foreign-library-paper-direction-2026-11-05.md new file mode 100644 index 0000000..fb159d3 --- /dev/null +++ b/doc/design/foreign-library-paper-direction-2026-11-05.md @@ -0,0 +1,24 @@ +# 外联库论文的研究方向 + +作者于 2026-11-05 将论文主线明确为:**叙述本身是程序,外部代码是值**。 +Marqdo 的外联库用于在这一宿主模型中组合既有语言的计算能力,统一说明、控制流、 +代码复用、状态与显式数据交换。论文应先解释 Notebook/文学编程中的实际设计问题, +再给出解决方案、可核验的收益、形式化依据和应用实验。 + +后续实现工作围绕绑定的代码值、参数化调用、持久会话、复制值和文档计划展开。 +生命周期、值域和重放机制为这个统一设计提供保证;不能将重放检查器重新提升成 +取代外联库与叙述式编程的论文主题。不得把未测的人因优势或未证明的实现正确性 +写成既成事实。 + +完整方向文件在论文工作区: + +- 方向(当前):`MarqdoThesis/PLDI/design/V7-NARRATIVE-FIRST-DIRECTION-2026-11-05.md` +- v6 方向(历史):`MarqdoThesis/PLDI/design/V6-NARRATIVE-FIRST-DIRECTION-2026-11-05.md` +- 证据映射:`MarqdoThesis/PLDI/design/V6-EVIDENCE-MAP-2026-11-05.md` +- 章节与页预算:`MarqdoThesis/PLDI/design/V6-OUTLINE-2026-11-05.md` + +当前稿位于 `MarqdoThesis/PLDI/paper-v7`;v6 保留,历史 v5 及其证据保留。方向文件取代了 +此前把论文缩成“外联会话值 + 因果重放”的 N3 路线:重放是支撑机制,不是主线。 + +2026-11-05 交付:`MarqdoThesis/PLDI/paper-v7/main.pdf`,正文(含 AI Use)20 页。 +v6 保留为上一版尝试;更早的 v6 草稿保留在 `paper-v6-draft-2026-11-05/`。 diff --git a/doc/design/foreign-strong-correspondence.md b/doc/design/foreign-strong-correspondence.md index d4088c1..11bb5f0 100644 --- a/doc/design/foreign-strong-correspondence.md +++ b/doc/design/foreign-strong-correspondence.md @@ -1,5 +1,14 @@ # Foreign Strong: Lean ↔ host correspondence +2026-11-02 补充:`ForeignSource.lean` 对显式局部环境、返回、函数调用/递归、 +分支和有界 while 的源解释器与独立 effect 编译器证明一般精化,并推出安全 +观察和旧句柄不可复活。`CheckForeignSource.lean` 消费真实加载后 AST 的显式 +适配格式,用独立 counter oracle 对照 tree/bytecode 的返回、journal、fault +与故障后 n。它已用于发现并修复 tree 的父词法作用域查找缺陷。 +这是源控制流层的证明与实现 translation validation;Rust parser/adapter、 +全部 AST 构造、机器数值运算和物理调度的完整 refinement 仍须完成。 +模型中的 fuel 是分析界限,不能称为真实程序必然终止或有界取消。 + Pinned with PLDI Strong / paper-v5 (sessions as values; three gates). Marqdo **2.3.0** host implements the same objects the Lean development decides. @@ -23,3 +32,13 @@ Marqdo **2.3.0** host implements the same objects the Lean development decides. - SafeVal mechanization covers the **integer** fragment; finite floats are admitted by the host under the same refuse-not-coerce bridge policy for non-integers already in SafeVal. - Session discipline is a **dynamic registry** (affine close + borrow-use). Static linear types over OS-killable processes are out of scope by design, not an unfinished host feature. - Correspondence is mechanized for the **closed / fresh-open / functional-Ω** fragment (`corr_fresh_functional`): per-step interactive value = Ω answer, interactive runner = normative runner, `replayCheck` on the journal. Broken hypotheses are `corr_fail_*`. Host discharge is foreign-session gold plus R1–R3, not R1–R3 alone. + +## Reviewer-driven implementation update (2026-11-01) + +The independent live cursor and multisession action-plan simulation are in +`DocCalc.runForeignLive_simulation` and `MarqdoProof.ForeignTrace`. The language +now exposes oracle observations and pure journal comparison through +`foreign.oracle_journal` / `foreign.replay_check`. Strict interactive snapshot +replay uses the full oracle history with source identity and per-step input. +See `oracle-journal-values.md` for the API and the research experiment manifest +under `PLDI/experiments/reviewer-v5/` for exact source hashes and actual runs. diff --git a/doc/design/frontend-call-semantics-2026-11-03.md b/doc/design/frontend-call-semantics-2026-11-03.md new file mode 100644 index 0000000..362a973 --- /dev/null +++ b/doc/design/frontend-call-semantics-2026-11-03.md @@ -0,0 +1,336 @@ +# Executable call and integer semantics + +This describes the implementation contract exercised by +`src/frontend_elaboration_tests.rs`; it is not a claim of a verified parser. + +All explicit actual arguments evaluate once in authored order, including extra +named arguments whose values the callee does not read. Binding then permutes +already computed values into parameter slots. Named arguments take precedence +over positional filling; duplicate names and excess positional arguments fail. +A duplicate-actual binding fault occurs only when its call is reached, after +all explicit actuals have evaluated; it cannot reject an unused function at +compile time or erase an earlier argument effect. +Local calls and imported calls use the same order. Builtins and host primitives +must not reorder expression evaluation when selecting their parameter values. + +All supplied parameters are available before defaults. Missing defaults execute +in parameter declaration order, once per call, in the target definition's +module and lexical scope. A default can see supplied parameters, earlier +computed defaults, and an explicitly bound method receiver. It cannot capture +the caller's locals. A still omitted later parameter is unbound. Explicit None +is a supplied value and never triggers a default. Required-argument validation +precedes default execution. Default expressions participate in receiver +classification, dependency analysis and transitive effect inference. + +A dynamic method's signature belongs to the resolved receiver and selected +method; a different object's same-named method cannot supply its parameter +list. Imported definitions retain their module scope. A compiled document's +entry is selected only from its own module; an imported `main` is not an entry. + +The bytecode retains argument names and explicit presence bits. Callee +prologues evaluate defaults in their own frame. Local slots distinguish an +unbound slot from a slot holding None. Local, constant, call-layout and jump +indices use container-sized indices rather than unchecked 8/16-bit casts. +Scratch slots are reused across sequential calls while pending outer actuals +remain live. Scalar constant interning uses a hash table; floating constants +are keyed by their exact bits. + +Host integers are signed 64-bit values. Add, subtract, multiply, divide and +negation report a language error on overflow in every build profile; division +by zero is a separate error. Division truncates toward zero. The negative +endpoint has a valid signed literal. Float-to-int conversion rejects nonfinite +or out-of-range inputs instead of silently saturating. This local integer +domain is distinct from the narrower continuous foreign-copy domain. + +The Lean call-combinator proof preserves complete interaction programs for +arbitrary argument evaluators, including failure and reply-dependent requests. +The integer model specifies exact arithmetic followed by representability +checking. Native documents, independently derived Python expectations, a Lean +arithmetic consumer and isolated source mutations validate the implementation +links. These do not prove the entire Rust parser, binder, object representation +or adapter. The research coverage ledger must keep those obligations explicit. + +## Module identity, entry inputs and source-bound invocation + +Instances retain the existing map representation and safe-copy domain. The +reserved `_type_module` field is a list of import binding names, paired with +`_type`. Class selection uses that module instance within the active import +tree; inherited methods resolve their bases in the defining module. Equal +class names in different imports remain distinct. These fields are ordinary, +forgeable data, not capabilities or globally portable nominal identities. +Round trips through Python/Node preserve these descriptors in the same module +context. Opaque session handles remain outside the copy domain. + +Entry metadata is passed to `Vm::run`, never compiled into `main`'s body. +Explicit named/recursive invocations of `main` therefore keep their own inputs. +Defaults run with the invocation's actual registry; a session-valued default +must not be evaluated in a separate interpreter and then injected into a VM. +Reads appearing only in explicit/prose defaults participate in input inference +and effect analysis. Default failures use the parameter declaration's span. +Source-bound APIs preserve the selected declaration's source identity and +span, even when two declarations have the same dotted path. Textual path calls +retain the existing first-declaration rule. Duplicate parameter names are +rejected because they cannot define distinct binding slots. + +Both backends retain parameter, return and local field contracts. Bytecode +checks supplied/defaulted inputs before the body, explicit returns before +returning, table bindings before writing a local and direct footnote accesses +before/after lookup. Unknown fields report the contract error before a generic +missing-key error. Entry/raw invocations also check their completed inputs. +Contract diagnostics retain the declaration quote and the failure location. + +## Refinement obligations and current evidence + +| Link | Mechanized result or implementation evidence | Remaining obligation | +|---|---|---| +| Raw Markdown to executable AST | Native explicit/prose defaults, source snapshots, parameter spans, inference and duplicate-parameter diagnostics | Prove the actual Rust classifier/parser and extraction rules; the older abstract grammar is not that proof | +| Loaded module tree to flattened declarations | Independent inventories of source digests, module instances, lexical parents, class/method ownership and resolved bases | Connect the actual resolver to the Lean namespace lookup model, including failed/ambiguous resolution | +| Effectful actual evaluation to parameter values | `FrontendCalls` proves interaction-program equality for arbitrary expression evaluators; native authored-order/unused-actual/builtin/foreign probes | Prove Rust scratch allocation, every expression case and its stack invariant against that combinator | +| Default evaluation and invocation inputs | Native callee-scope, presence, receiver, entry/raw/metadata, inference, error-span and stateful-session cases | A complete default-binding semantics and universal compiler refinement, including errors and host context | +| Module-qualified object dispatch | `ModularObjects` proves namespace flattening lookup, resolved-parent dispatch and safe descriptor round trips | Connect every real object constructor, resolver and copied descriptor to the model; justify Rust module-root lifetimes | +| Machine integers | `MachineInts` exact signed-64 model, checked boundary implementation, independent Python and Lean consumers | Connect Rust checked arithmetic and every conversion to the mathematical evaluator; floats remain an extension | +| Progressive contracts | Four real source boundary failures preserve codes, anchors and failure spans; valid contract execution | Universal contract-preservation theorem and imported declaration/call-site attribution | +| Source-bound external calls | Two same-path declarations execute their separately selected source lines on both backends | Full external argument validation, effect-policy and environment-injection refinement | + +The ledger deliberately does not turn a new model lemma, a finite passing +corpus, or independent reconstruction into a complete frontend theorem. Those +remaining proof links are required to discharge the full frontend objective. + +## Default prologue proof and imported contract attribution (November 4) + +`DefaultPrologue.default_prologue_refines` relates source default binding, which +rechecks the current environment for omission, to a guarded block machine with +an immutable initial presence mask. It proves whole interaction-program equality +for arbitrary effectful evaluators, assuming distinct parameter slots and an +initially faithful presence mask. Earlier default stores preserve that invariant +for later parameters. The duplicate-slot counterexample issues two requests in +the machine versus one in the source, demonstrating why slot uniqueness matters. +Explicit None skips and immediate failure stopping are separately checked. +This is a model of guard/evaluate/store blocks, not a proof of the Rust emitter, +flat instruction stepping, actual expression compilation or parser. + +The generated implementation family crosses all 27 omitted/int/None states for +three parameters with all six declaration orders. An independent environment +model checks 162 documents and 324 executions, including 34 faults when a default +reads a later omitted parameter. Source text is reconstructed independently; +expected traces are not derived from either backend. This finite family does +not prove arbitrary defaults. Imported argument-contract diagnostics now retain +the caller's failure span and source revision while pointing the declaration +anchor and suggestion at the actual library. Supplied and defaulted invalid +arguments both refuse before entering the callee body. + +## Concrete name and flat-code interfaces + +The compiler now borrows each original declaration and owning module while +flattening. Bare-call selection uses the same source `lookup_local` operation +as Tree, then maps the selected declaration identity to its flat index. This +removes a second, subtly different lookup algorithm: repeated top-level names +previously selected the first declaration in Tree but failed compilation in VM. +It does not prove the source lookup operation itself. + +`NameSlots` proves reads, writes and the complete named-argument binding fold +commute with an injective local-name vector. Unknown names and repeated raw +API names are covered; repeated raw names retain last-write semantics. A +presence mask computed from initialized slots is faithful by construction. +The Rust raw entry and argument binder now derive their mask from the actual +initial logical slots. Method receiver aliases are overlaid only for default +evaluation, and restored into the body frame after logical argument checks. +`ReceiverDefaults` supplies the additional environment reasoning for explicitly +declared self/自 parameters; the slot theorem alone does not cover that case. + +Every compiled Program is structurally verified before return, and the VM +verifies even publicly constructed/mutated Programs before any instruction. +All current opcode variants are handled exhaustively. Checks include operand +bounds, parameter/local prefix and injectivity, declaration and inheritance +forests, call-layout and function references, spans, reachable stack underflow, +branch/loop join heights and absence of leaked stack values on return. Missing +variable values remain language faults; this checker does not prohibit them. + +Rust exports the computed per-PC heights alongside the real emitted opcode +text. `CheckFlatCertificate.lean` independently decodes that text and applies +an executable checker whose soundness is proved in `FlatCertificate`. Accepted +certificates preserve heights along every finite path and guarantee a valid +fetch with sufficient operands on each reachable PC, including loop prefixes. +The independent reader rejects unmodeled opcodes. Rust debug serialization and +opcode-to-concrete-VM-step correspondence remain interfaces to justify; a +structural certificate does not establish source/bytecode functional equality. + +Neither this ordinary bytecode verification technique nor reuse of a resolver +is an originality claim. Complete functional lowering, parser semantics and the +Rust implementation correspondence remain open proof obligations. + +## Functional expression checks and receiver views (November 4) + +The emitter now records source-directed expression rules before emission, with +child order and exact instruction intervals. The mandatory native verifier +checks literal identity (including floating-point bits), local names, unary and +binary operators, short-circuit targets, interpolation, collections, indexing +and occlusion. Calls delegate staging and selection checks to `bytecode/call.rs`; +resolution and dispatch semantics remain explicit boundaries. +All seven statement forms have source-directed templates; a separate forest +check covers the entire default prologue, statement body and implicit return. +These checks detect stack-preserving semantic corruption, including Add to Sub, +and refuse it before host effects. A forged source witness remains outside this +check's guarantee; the recorder and actual AST correspondence need justification. + +`ExpressionLowering` proves postfix expression compilation preserves complete +interaction programs for arbitrary effectful leaves and operator interpretations, +including faults and an arbitrary caller stack and continuation. `ShortCircuit` +proves the exact absolute-PC AND/OR macros under arbitrary effectful child +evaluation. `CheckExpressionLowering.lean` independently reads actual emitted +operators, constants and local names, and applies the proved core checker. +It counts call, collection, index and interpolation boundaries explicitly. +Child inlining, Rust VM stepping, leaf interpretation and source-witness fidelity +remain obligations; none of these results is whole-frontend functional refinement. + +`ReceiverDefaults` proves that the source's separately maintained default +environment equals the machine's receiver view after every successful prefix. +The mask describes logical argument slots, while omitted self/自 defaults may +replace the evaluation view. Supplied None remains present. The real receiver +is restored before entering the body; argument contracts check both the logical +arguments at the call site and the restored frame at the declaration, preserving +source identity and diagnostic frames. The theorem composes this view invariant +with `DefaultPrologue` for arbitrary effectful defaults and distinct slots. + +Concrete repairs also share scalar/unary semantics across evaluators, stringify +every interpolation part, allocate fresh loop temporaries, restrict dynamic +calls to their defining module scope, and enforce the existing one-million +truthy-iteration limit in VM as well as Tree. Regression evidence records each +repair separately from proof claims. These compiler techniques are not new +originality claims. + +## Actual call staging and bounded control-flow macros + +Every emitted call now has a separate source obligation captured before actuals +are replaced with scratch reads. The checker covers authored actual order, +evaluate/store/pop triples, source-local and loop-slot separation, nested-call +scratch separation, argument reloads, layouts, builtin selection/defaults, host +primitive identity, plugin name/pair ordering, method name/receiver and resolved +user target/tag. Every call expression must have exactly one obligation. The +reserved source-local prefix is established before any synthetic slots are +allocated. Disjoint nested calls may reuse scratch slots after their lifetimes. + +`CallStaging` proves the flat evaluate/save/pop program restores an arbitrary +caller stack and has the same effects and failures as authored-order collection. +It proves outside slots unchanged and unique staged slots read back the exact +actual vector. The dispatch continuation is arbitrary and effectful, so binding, +defaults and receiver effects can follow collection without commuting effects. +`CheckCallStaging.lean` decodes real stores and reloads, applies the proved stage +checker and independently checks the complete selection/dispatch template. +Source-expression callback correspondence, name resolution and the semantics of +each dispatch continuation remain proof interfaces. Host parameter metadata is +an exported registry interface, not an independently verified registry theorem. + +`WhileLowering` proves the condition/JF/check/body/backedge macro preserves the +complete interaction program for a bounded loop, including a final false +condition, condition/body faults, the truthy-iteration limit and early returns. +It relates an increasing native counter to a decreasing model budget and proves +admitted increments below one million fit u32. Model machine-step fuel is +distinct from this language-visible iteration budget. +`CheckStatementLowering.lean` checks actual default prologues, all seven statement +templates, forests and implicit returns, including the exact while macro layout. +The while theorem uses condition/body callbacks; concrete child inlining and the +full Rust state machine are not discharged by checking those layouts. Branches +and foreach currently have independent template evidence, not new functional +refinement theorems. No ordinary compiler-checking technique is a novelty claim. + +## Loaded executable AST and coherent witness corruption + +`SourceFunction` now captures parameters, defaults, executable bodies and complete +contract data directly from the loaded declaration before emission. The native +source checker walks that snapshot separately from the rule recorder, checks +ordered expression/statement correspondence and complete witness ownership, +and accounts for the generated scratch reloads explicitly. It also compares +full contracts with structural Rust equality, including types, nullable fields, +descriptions, source lines and declaration quotes. Contract presence alone would +not detect a Number-to-Any weakening. + +The evidence exports the executable AST separately from the original module and +from the compiled snapshot. `CheckSourceWitness.lean` matches declaration +identities and applies `SourceWitness`'s proved ordered-tree reconstruction +checker. Interpolation parts, index labels, code payloads and formula structure +are retained; all formula floats and matrix entries retain their exact bits. +The old Debug formula text was insufficient for NaN payload identity. Inert +occlusion prose and pre-inference parser metadata are not execution-tree nodes. + +Ten native coordinated-corruption cases change code together with its witness, +including operators, constants, variables, assignments, callees, NaN payloads, +index keys, interpolation, contract metadata and an orphan expression record. +Eight independent source-witness negatives include changing the compiled snapshot +as well. A coordinated wrong operator is a positive acceptance control for all +four earlier independent consumers and a rejection control for the AST checker. +This distinguishes trusted source correspondence from agreement among emitted +metadata fields. + +The theorem proves exact correspondence of the supplied executable trees, hence +preservation under any interpretation of those trees. The native and JSON AST +adapters, normalization helpers and snapshot capture remain trusted interfaces. +No theorem here verifies the actual Markdown classifier/parser/inference or +connects every concrete Rust operation to the semantic models. A caller who +changes both the reference AST and its program has supplied a different +translation-validation input; this is not source authentication. + +## Runtime argument-preparation faults + +The expanded AST audit exposed a concrete acceptance mismatch: Tree can execute +main while an unused function contains a conflicting bracket modifier, whereas +VM compilation previously rejected the entire module. Modifier preparation now +lowers its failure to `RejectArgumentExpansion` before actual evaluation. A +provided default parameter, including explicit None, skips that fault; reaching +it preserves prior output and stops before actual effects or a suffix. The +error's message, span, source revision and diagnostic frames match Tree. + +`CallExpr::for_evaluation` normalizes bare aliases before modifier expansion, +matching Tree's existing order. The expression checker validates an effect-free +fault leaf and the independent decoder handles its actual terminal opcode. +The AST adapter explicitly represents that executable fault without children; +unexecuted actual expressions are not falsely counted as evaluated code. +Two independent fault-opcode/message negatives and a positive AST-identity-only +control keep representation checking distinct from instruction interpretation. + +The independent Python oracle now compares JSON values with exact recursive +types and requires integer arithmetic inputs to be actual integers. Python's +True==1 and 1==1.0 equalities must not erase language-visible value distinctions. +Four type-corruption controls are added. These improvements do not discharge +other call-error timing, parser, resolver, dispatch or Rust-step proof obligations. + +## Native binding and declaration graph validation + +Builtins, host primitives and plugins retain authored labels in native bytecode. +The shared value-independent binding plan selects named parameters before +positionals, preserves omitted optional bindings and unknown named extras, and +reports duplicate, missing and surplus positional arguments after actual effects. +Plugin lookup precedes signature binding. Eighteen reached/unreached error cases +check explicit expected output, messages, spans, source revisions and frames. +A registered C ABI echo probe checks mixed and extra named arguments. Execution +uses the native VM and plugin bridge throughout. + +The source and flat declaration views share ordered resolution. A loaded snapshot +captures ordinal declaration paths, levels, child/root counts, imports and ordered +use aliases before emission. Verification checks identity, parent/child/root order, +module scopes and object/method names, then derives call targets independently of +emitted target records. Two-component paths check runtime receiver presence before +static fallback; an unbound reserved slot differs from a supplied None. Imports +retain priority. Resolution failures are native terminal instructions after actuals. + +`NameResolution` proves first-match, path and lexical commutation under an +ordered embedding. Its rank checker proves sufficient depth and connects a +well-founded total source policy to flat lookup. Rust uses the same parent-chain +policy; parent-view extraction remains a trusted adapter. The finite checkers consume actual AST/flat evidence in +`CheckNameResolution.lean`. Nine native corruptions and seven independent graph +negatives test this boundary. A coordinated wrong target remains a positive control +for the older stack, staging and AST-shape consumers. Ordinary compiler correctness +ingredients are not novelty claims. Snapshot/JSON adapters, primitive registries, +parent-view extraction and concrete Rust +instruction refinement remain explicit obligations; universal frontend verification +and overall paper readiness are not established by these results. + +`ArgumentBinding` separately models per-parameter omission permission and proves +value substitution preserves selections and faults. A successful index plan has +a finite-index witness. `CheckArgumentBinding.lean` checks actual static call +plans and an independently enumerated mask/label corpus. User VM calls now use +the shared planner with the callee default mask, correcting their surplus-count +diagnostic and preserving missing-required-before-default priority. Default +evaluation, contracts, dynamic signature selection and concrete Rust/JSON steps +remain separate interfaces. The mathematical planner is not a proof of the +entire native binding implementation. diff --git a/doc/design/notebook-session.md b/doc/design/notebook-session.md index f76445b..ad29913 100644 --- a/doc/design/notebook-session.md +++ b/doc/design/notebook-session.md @@ -1,5 +1,35 @@ # 文档会话(View Cells) +## 2026-11-02:并发控制与完整历史 v5 + +以下为当前实现;后文保留首版设计与当时的状态表。 + +- HTTP `kernel/interrupt` 查找已存在的会话,经独立控制器发送请求,不等待单元执行锁或其他文档的初始化。未打开、尚在初始化的会话返回错误,不启动新内核。已加载文档的源码文件被删除后仍可取消正在执行的单元。 +- 内核的中断能力绑定其持有的子进程,并在回收前撤销;旧能力不能指向后来启动的内核。引导阶段的取消被暂存,收到 ready 后投递。逻辑取消输入会合并重复信号;投递错误明确返回。Windows 尚无对应的保状态中断实现。 +- 取消使 Run All 在当前单元结束后停止,返回实际 `ran`、`remaining` 和 `cancelled`;不会继续运行后续单元。内核没有退出时,取消前已发生的状态修改保留。close 撤销 API 会话,重新打开不会激活旧引用。 +- 完整历史 v5 将并发取消作为所在操作的 `controls` 输入记录:操作起点之后的微秒偏移及各内核的真实接收结果。严格回放按输入重新投递并重新观察回执;操作先结束时撤销剩余定时请求,不能流入下一操作。实际回执或观察不匹配就报告差异。v4 无控制输入的历史仍可检查;v4 中出现 controls 字段会被拒绝。 +- 严格检查先完成纯验证,再启动回放内核。实际执行中,每次协议请求必须先与预期请求一致;实际回复和每个单元的观察、批次结果也立即比较。首个差异后停止依赖后缀,包括同一个 Run All 内的后续单元。未执行的观察标记 `compared=false`,不冒充已比较的第二个差异。 + +快照导出支持可选 `compact:true`。默认仍为完整 JSON;紧凑格式 +`marqdo.history.dag` v1 对相同子值做自底向上共享,整数使用十进制文本、 +浮点数保存 IEEE 位模式,保留映射顺序和全部历史字段。导入和严格回放会自动 +识别格式。SHA-256 校验只检测内容一致性,不提供来源认证。 +解码要求规范节点、向后引用、全部可达和唯一键,并在展开前检查最多 +1,000,000 个节点、8,000,000 个展开值、512 MiB 保守展开预算、128 层深度; +展开后再次检查安全值域。此格式减少导出字节数,尚未减少录制时内存,也不保证更快。 + +实际取消实验覆盖 Unix 上同步 Python/Node 持久会话;不能推导任意外部代码均有界停止。 +[Python 3.14 信号文档](https://docs.python.org/3.14/library/signal.html#execution-of-python-signal-handlers) +说明纯 C 计算可延迟信号处理; +[Node 22.22.1 vm 文档](https://nodejs.org/download/release/v22.22.1/docs/api/vm.html#timeout-interactions-with-asynchronous-tasks-and-promises) +说明异步任务可能逃出同步求值的中断范围。完整源码 elaboration、控制适配器、 +真实时钟与信号调度到形式模型的 refinement 仍需完成,不能以这些测试代替。 + +相关实现:`foreign_session/control.rs`、`foreign_doc/control.rs`、 +`foreign_doc/packing.rs`、`foreign_doc/replay.rs` 与 `view/notebook_api.rs`。 +真实 TCP/HTTP 取消、逐单元副作用屏障和独立快照消费记录位于论文 +`PLDI/experiments/reviewer-v5/`。 + | | | |---|---| | 状态 | **D1–D3 首版已实现;D4/D5 待做** | diff --git a/doc/design/oracle-journal-values.md b/doc/design/oracle-journal-values.md new file mode 100644 index 0000000..2125f8e --- /dev/null +++ b/doc/design/oracle-journal-values.md @@ -0,0 +1,131 @@ +# Document oracle observations and strict replay + +`foreign.oracle_journal` returns a copy of this evaluation's observations as +ordinary `index/channel/value` records. Successful admitted `session_eval`, +`session_get`, and document `json.parse` operations append `eval`, `get`, and +`decode`, respectively. Set/open/close do not append. An observation is +validated recursively before append; handles and code objects cannot enter. +Each new HostContext starts with an empty observation list. + +`foreign.replay_check expected=J observed=J2` is a pure comparison, with no +foreign execution. It validates contiguous zero-based indices, channel tags +and crossing-domain membership. Records compare structurally by field name, +independent of field order. The result contains `ok`, the least mismatching +`index`, and both records (None for a missing entry). Two empty journals +match. Failure to provide a valid journal is an error, not a match. + +The interactive `DocumentSession::replay_check(snapshot)` is a different layer: +it executes a recorded trace from fresh state. Version-2 snapshots contain the +full chronological oracle trace, recorded stdin per step, its declared count, +the plan and source identity. Preflight validates every step before any foreign +execution. Every step is compared; none is skipped. Repeated cell executions +are distinct observations. Execution timing logs and cell display summaries +are not comparison inputs. Matching input values in a supplied snapshot is +consistency checking, not cryptographic authentication of the recording. + +Snapshots carry the SHA-256 of the loaded document text. Import checks it +before restoring foreign bindings, and preflights all crossing payloads before +mutation. Reload retains prior oracle observations with their original source +hashes, so a later checker can reject an incompatible trace. + +Lean: DocCalc's independent `runForeignLive` cursor simulates `runForeignAcc` +for arbitrary prefixes, observation accumulators and code sequences, including +oracle failures. `ForeignTrace` composes the registry's open/eval/close +judgments using independent normative and interactive execution control; +`trace_simulation`, `trace_correspondence`, and `trace_replay` quantify arbitrary +multisession action sequences, also when a run faults. These supplement the +existing theorems without removing them. + +Tests: `tests/oracle_journal.rs` runs the public document API on both evaluators; +`host::oracle::tests` covers journal equality and length localization; +`host::foreign_doc::tests` covers the live trace preflight and actual replay; +`host::foreign_session::tests` includes generated two-session model checks and +Python/Node boundary and process-failure experiments. The research runner is +`MarqdoThesis/PLDI/experiments/reviewer-v5/run.py`. + +## Stateful requests and semantic projection + +`HostContext.foreign_protocol_steps` records internal request attempts and +replies, including set values, copied eval input and raw stdin. It is separate +from the admitted language journal: rejected inbound responses belong to the +protocol evidence, while only admitted values belong to the oracle journal. +An input refused before send produces no protocol request. A response refused +after execution does not undo kernel effects; the post-state is retained. + +Language observations are recorded at the document API boundary and by live +cell evaluation. Low-level get/names used during snapshot export do not append +language observations. A successful eval projects to its result. A failed eval +projects to a record of stable status, output and diagnostics. Process timing, +commands and handles are not semantic values. The exact source path allocated +for one-shot execution is alpha-renamed only in diagnostic fields; raw stderr +and stdout remain available unchanged, and ordinary paths/error differences +are still compared. Live oracle records carry this same projected `value`. + +Python/Node replies mark serialization-domain failure with `domain_error`. +Error text is not used to infer this flag. Python refuses non-string object +keys; Node sanitizes objects with a null prototype to preserve special keys. +The common strict JSON decoder checks duplicate keys before materializing a +map, rather than losing them through overwrite. + +`ForeignState.lean` refines the earlier eval-only fragment with opaque kernel +state, complete request histories, set/get/eval input/decode, monotonic handle +allocation and fault outcomes carrying the admitted prefix. An adaptive +Program has continuations on actual observations; its correspondence theorem +covers data-dependent next actions, not just a fixed schedule. Mapping actual +document elaboration and host bindings to this program is a further proof +obligation recorded in the research status file. + +## Complete history, snapshot version 4 (2026-11-02) + +Strict interactive replay now records operations, not just successful cells. +Each public execution/mutation boundary records typed arguments, success or +failure, admitted observations, cell-oracle records, and stable resulting +state. Nested implementation calls are grouped under the user's operation. +The history includes set/get, run/step/run-all, restart, reload, plan edits, +clear, snapshot inspection and imported values. Failed attempts remain in the +history; rejected handles and secrets have only non-capability/redacted tags. +Clearing display outputs cannot erase the history's oracle projection. + +The snapshot stores the materialized initial plan and historical reload plans. +Entry text is parsed at its original path, retaining relative imports without +rewriting the user's document. Initial bindings are reapplied when a kernel is +reopened; unchanged reload kernels retain state, whereas changed language or +command retires the incompatible process. Loaded source/metadata and cached +cell outputs are updated consistently. + +Preflight checks the entire operation stream, all source revisions, plan edits, +binding/cursor/generation transitions, and oracle projections before starting +any replay kernel. Executing the trace compares each result, admitted value, +cell result and stable state. The first mismatch stops its dependent suffix; +`executed_actions` and `remaining_actions` make this explicit. `matched` counts +admitted observations and `matched_actions` counts operations. No missing +record is silently accepted or skipped. + +The HTTP replay-check endpoint performs an isolated comparison and retains an +existing warm session. It validates malformed input before starting even a +seed kernel; duplicate JSON fields are refused before overwrite. Verification +can still run recorded foreign work and its external effects: isolation is of +the document session, not a process sandbox. `replay_snapshot` remains the +separate legacy cell re-run API and makes no strict-history comparison claim. +Older version-2/3 snapshots may be used by legacy restoration/re-run surfaces; +they cannot prove a complete action history and strict checking requires v4. + +`ForeignHistory.lean` models explicit command boundaries. A fault terminates +its command, a later command receives the actual retained registry, and the +history retains all command faults and admitted observations. It proves +command-history correspondence, journal membership and no alias resurrection +through arbitrary recovery histories. This extends the adaptive Program +results; the general source-elaboration refinement is a separate outstanding +proof obligation. + +Experiment source inputs are frozen before running, checked again afterward, +and archived with their hashes. The current passing bundle is identified by +`MarqdoThesis/PLDI/experiments/reviewer-v5/LATEST-PASSING.json`. + +Each operation also retains its internal protocol attempts/replies, including +refused inspection results. Those records are compared even when an inspector +could not return a value. They are control evidence and do not admit rejected +payloads into the language journal. An explicit document session_names request +adds its admitted array on the decode channel; internal names/get probes do not. +Reload records either a historical source/plan input or its input-read error; +replay consumes that captured version input and does not rewrite past files. diff --git a/doc/design/v-safe-json.md b/doc/design/v-safe-json.md index 265160e..aff0b5c 100644 --- a/doc/design/v-safe-json.md +++ b/doc/design/v-safe-json.md @@ -24,7 +24,7 @@ MAX_SAFE = 9007199254740991 # 2^53 - 1 | `array` | `List` | | `object` | `Map` | -**Host extension (not in Lean `SafeVal`):** JSON non-integer numbers → `Value::Num(f64)` (NaN/Inf encode as `null`). Floats are documented host behavior; they are **not** claimed as mechanized \(V_{\mathsf{safe}}\) fidelity. +**Host extension (not in Lean `SafeVal`):** JSON non-integer numbers → `Value::Num(f64)`. Non-finite numbers (NaN/Inf) are refused on encode and in Python/Node oracle responses, including nested payloads. Floats are documented host behavior; they are **not** claimed as mechanized \(V_{\mathsf{safe}}\) fidelity. ## Encode (`value_to_json`) @@ -76,3 +76,15 @@ json: integer outside V_safe (±(2^53-1)): - Not automatic BigInt promotion inside Node/Python bootstraps. - Document-level `Value::Int` may still hold i64 values that are OOD for JSON; the gate fires when those values enter JSON encode/decode. + +## Strict request boundary update + +The numeric gate applies to the represented value, including integer-valued +floats and exponent notation. `9007199254740992.0` and +`9.007199254740992e15` are refused; fractions such as `1.5` remain in the host +extension. `serde_json` uses `float_roundtrip` to avoid endpoint decoding loss. +The strict text decoder rejects duplicate object keys and unequal column maps +before information is lost. One-shot, call_many and persistent input paths +use recursive domain admission; code, handle and secret values are not copied +implicitly. Auto-detection may leave non-JSON output as text, but valid JSON +outside the domain is a refusal, including in batch results. diff --git a/lib/foreign.mq.md b/lib/foreign.mq.md index be5b5fa..05d02d9 100644 --- a/lib/foreign.mq.md +++ b/lib/foreign.mq.md @@ -249,3 +249,20 @@ List live sessions with id/lang/cmd/evals/alive/started_ms. List configured foreign languages. *> host_foreign_langs* + +## oracle_journal + +Return this document evaluation's admitted eval/get/decode observations as +ordinary records with index, channel, value. Opening, setting and closing do +not append observations. + +*> host_oracle_journal * + +## replay_check + + `expected` + + `observed` + +Compare two oracle journals without executing foreign code. Returns ok and +the least mismatching index, with the expected and observed records. + +*> host_oracle_replay_check expected=`expected` observed=`observed`* diff --git a/src/application_tests.rs b/src/application_tests.rs new file mode 100644 index 0000000..efbf327 --- /dev/null +++ b/src/application_tests.rs @@ -0,0 +1,293 @@ +//! Application compositions with precomputed independent SQL/Floyd oracles. +use crate::host::{ + foreign_doc::{packing, DocumentSession}, + json::{json_to_value, value_to_json}, +}; +use crate::value::Value; +use serde_json::{json, Value as Json}; +use sha2::{Digest, Sha256}; +use std::path::{Path, PathBuf}; + +fn root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) +} +fn cases() -> Vec { + let path = std::env::var_os("MARQDO_APPLICATION_CASES") + .map(PathBuf::from) + .unwrap_or_else(|| root().join("tests/applications/CASES.json")); + let rows: Vec = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap(); + assert!(!rows.is_empty()); + rows +} +fn out() -> PathBuf { + let path = std::env::var_os("MARQDO_APPLICATION_ARTIFACT_DIR") + .map(PathBuf::from) + .unwrap_or_else(|| root().join("target/applications")); + std::fs::create_dir_all(&path).unwrap(); + path +} +fn code(case: &Json, stage: &str) -> String { + let lang = if stage == "producer" { + case["producer"].as_str().unwrap() + } else { + case["consumer"].as_str().unwrap() + }; + let ext = if lang == "python" { "py" } else { "js" }; + std::fs::read_to_string( + root() + .join("tests/applications") + .join(case["family"].as_str().unwrap()) + .join(format!("{stage}.{ext}")), + ) + .unwrap() +} +fn failure(lang: &str) -> &'static str { + if lang == "python" { + "retained = answer\nraise RuntimeError('application_expected_failure')" + } else { + "globalThis.retained = answer; throw new Error('application_expected_failure');" + } +} +fn mutate(lang: &str) -> &'static str { + if lang == "python" { + "bridge = None\nNone" + } else { + "bridge = null; null;" + } +} +fn field<'a>(value: &'a Value, name: &str) -> &'a Value { + let Value::Map(fields) = value else { + panic!("expected record: {value:?}") + }; + &fields.iter().find(|(k, _)| k == name).unwrap().1 +} +fn save(path: &Path, value: &Json) { + std::fs::write(path, serde_json::to_vec_pretty(value).unwrap()).unwrap(); +} + +#[test] +fn compositions_preserve_data_failure_recovery_and_copy_isolation_on_both_backends() { + let out = out(); + let rows = cases(); + for case in &rows { + let id = case["id"].as_str().unwrap(); + let q = |s: &str| serde_json::to_string(s).unwrap(); + let input = q(&case["input"].to_string()); + let family = root().join("lib"); + let consumer = case["consumer"].as_str().unwrap(); + let source = format!( + r#"--- +markup: occlusion +import: + foreign: {foreign} + json: {json} +--- +# main +**data = > json.parse text={input}** +**p = > foreign.session_open lang={producer}** +**c = > foreign.session_open lang={consumer}** +**> foreign.session_set id=`p` name="data" value=`data`** +**first = > evaluate target=`p` source={produce}** +**bridge = > foreign.session_get id=`p` name="bridge"** +**> foreign.session_set id=`c` name="bridge" value=`bridge`** +**second = > evaluate target=`c` source={consume}** +**failure = > evaluate target=`c` source={failure}** +**kept = > foreign.session_get id=`c` name="retained"** +**cleared = > evaluate target=`c` source={mutate}** +**empty = > foreign.session_get id=`c` name="bridge"** +**original = > foreign.session_get id=`p` name="bridge"** +**answer = > foreign.session_get id=`c` name="answer"** +**> foreign.session_close id=`p`** +**> foreign.session_close id=`c`** +*answer* + +## evaluate + + `target` + + `source` +**record = > foreign.session_eval id=`target` source=`source`** +*record* +"#, + foreign = family.join("foreign.mq.md").display(), + json = family.join("json.mq.md").display(), + producer = q(case["producer"].as_str().unwrap()), + consumer = q(consumer), + produce = q(&code(case, "producer")), + consume = q(&code(case, "consumer")), + failure = q(failure(consumer)), + mutate = q(mutate(consumer)) + ); + let path = out.join(format!("{id}.mq.md")); + std::fs::write(&path, &source).unwrap(); + let module = crate::load::load_module(&path).unwrap(); + let mut executions = Vec::new(); + for backend in ["tree", "bytecode"] { + let (result, stdout, (journal, protocol, live)) = if backend == "tree" { + let mut tree = crate::interp::Interpreter::with_capture(Some(&path), false); + let result = tree.run_module(&module); + ( + result, + tree.captured_stdout.clone(), + tree.application_evidence(), + ) + } else { + let program = crate::bytecode::compile_module(Some(&path), &module).unwrap(); + let mut vm = crate::bytecode::Vm::with_capture(Some(&path)); + let result = vm.run(&program); + ( + result, + vm.captured_stdout.clone(), + vm.application_evidence(), + ) + }; + let error = result.as_ref().err().map(|e| e.to_string()); + let actual = result.ok().map(|v| value_to_json(&v).unwrap()); + executions.push(json!({"backend":backend,"ok":error.is_none(),"error":error,"result":actual,"stdout":stdout,"live_sessions":live, + "journal":value_to_json(&Value::List(journal)).unwrap(),"protocol":protocol.iter().map(|p|json!({"session":p.session,"request":p.request,"response":p.response})).collect::>()})); + } + let result = json!({"version":1,"kind":"document","case":case,"source":source,"source_sha256":format!("{:x}",Sha256::digest(source.as_bytes())),"executions":executions}); + save(&out.join(format!("{id}-document.json")), &result); + for execution in result["executions"].as_array().unwrap() { + assert_eq!(execution["result"], case["answer"], "{id}: {execution}"); + assert_eq!(execution["ok"], true, "{id}: {execution}"); + assert_eq!(execution["stdout"], ""); + assert_eq!(execution["live_sessions"], 0); + } + } + println!( + "APPLICATION_METRIC:{}", + json!({"cases":rows.len(),"executions":rows.len()*2}) + ); +} + +#[test] +fn application_action_histories_replay_raw_and_packed_after_failure() { + let out = out(); + let rows = cases(); + for case in &rows { + let id = case["id"].as_str().unwrap(); + let consumer = case["consumer"].as_str().unwrap(); + let source=format!("---\nimport:\n foreign: {}\n---\n# main\n\n`会话` = > foreign.session\n\n`produce` =\n```{}\n{}\n```\n\n`consume` =\n```{}\n{}\n```\n\n`failure` =\n```{}\n{}\n```\n\n`mutate` =\n```{}\n{}\n```\n",root().join("lib/foreign.mq.md").display(),case["producer"].as_str().unwrap(),code(case,"producer"),consumer,code(case,"consumer"),consumer,failure(consumer),consumer,mutate(consumer)); + let path = out.join(format!("{id}-interactive.mq.md")); + std::fs::write(&path, &source).unwrap(); + let mut session = DocumentSession::open(&path, &root()).unwrap(); + let p = session + .cells + .iter() + .find(|c| c.name == "produce") + .unwrap() + .kernel + .clone(); + let c = session + .cells + .iter() + .find(|c| c.name == "consume") + .unwrap() + .kernel + .clone(); + let expected = json_to_value(&case["answer"]).unwrap(); + session + .set_value(&p, "data", json_to_value(&case["input"]).unwrap()) + .unwrap(); + let first = session.run_cell("produce", None).unwrap(); + assert_eq!(field(&first, "ok"), &Value::Bool(true), "{id}: {first:?}"); + let bridge = session.pull_value(&p, "bridge").unwrap(); + assert_eq!(value_to_json(&bridge).unwrap(), case["bridge"]); + session.set_value(&c, "bridge", bridge.clone()).unwrap(); + let second = session.run_cell("consume", None).unwrap(); + assert_eq!(field(&second, "result"), &expected, "{id}: {second:?}"); + let failed = session.run_cell("failure", None).unwrap(); + assert_eq!(field(&failed, "ok"), &Value::Bool(false)); + assert_eq!( + session + .cells + .iter() + .find(|c| c.name == "failure") + .unwrap() + .last_result, + Value::None + ); + assert_eq!(session.pull_value(&c, "retained").unwrap(), expected); + session.run_cell("mutate", None).unwrap(); + assert_eq!( + session + .cells + .iter() + .find(|c| c.name == "mutate") + .unwrap() + .last_result, + Value::None + ); + assert_eq!(session.pull_value(&c, "bridge").unwrap(), Value::None); + assert_eq!(session.pull_value(&p, "bridge").unwrap(), bridge); + assert_eq!(session.pull_value(&c, "answer").unwrap(), expected); + let snapshot = session.export_snapshot().unwrap(); + session.close(); + let packed = packing::pack(&snapshot).unwrap(); + assert_eq!( + value_to_json(&packing::unpack(&packed).unwrap()).unwrap(), + value_to_json(&snapshot).unwrap() + ); + let raw_check = + DocumentSession::check_snapshot_isolated(&path, &root(), &snapshot).unwrap(); + let packed_check = + DocumentSession::check_snapshot_isolated(&path, &root(), &packed).unwrap(); + save( + &out.join(format!("{id}-interactive.json")), + &json!({"version":1,"kind":"interactive","case":case,"source":source,"source_sha256":format!("{:x}",Sha256::digest(source.as_bytes())),"snapshot":value_to_json(&snapshot).unwrap(),"packed":value_to_json(&packed).unwrap(),"raw_check":value_to_json(&raw_check).unwrap(),"packed_check":value_to_json(&packed_check).unwrap()}), + ); + assert_eq!( + field(&raw_check, "ok"), + &Value::Bool(true), + "{id}: {raw_check:?}" + ); + assert_eq!( + field(&packed_check, "ok"), + &Value::Bool(true), + "{id}: {packed_check:?}" + ); + } + println!( + "APPLICATION_REPLAY_METRIC:{}", + json!({"cases":rows.len(),"strict_replays":rows.len()*2}) + ); +} + +#[test] +fn explicit_null_and_stdout_remain_distinct_and_only_success_can_persist() { + for lang in ["python", "node"] { + let (good, bad) = if lang == "python" { + ( + "print('audit')\nNone", + "print('failed audit')\nraise RuntimeError('expected_failure')", + ) + } else { + ( + "console.log('audit'); null;", + "console.log('failed audit'); throw Error('expected_failure');", + ) + }; + let path = out().join(format!("null-persist-{lang}-{}.mq.md", std::process::id())); + std::fs::write(&path,format!("---\nimport:\n foreign: {}\n---\n# main\n\n`会话` = > foreign.session\n\n`good` =\n```{lang}\n{good}\n```\n\n`bad` =\n```{lang}\n{bad}\n```\n",root().join("lib/foreign.mq.md").display())).unwrap(); + let mut session = DocumentSession::open(&path, &root()).unwrap(); + assert!(session.persist_cell("good", "empty").is_err()); + session.run_cell("good", None).unwrap(); + let cell = session.cells.iter().find(|c| c.name == "good").unwrap(); + assert_eq!(cell.last_result, Value::None); + assert_eq!(cell.last_stdout, "audit\n"); + assert_eq!(session.oracle_journal.last().unwrap().value, Value::None); + assert_eq!(session.oracle_journal.last().unwrap().result, Value::None); + session.persist_cell("good", "empty").unwrap(); + session.run_cell("bad", None).unwrap(); + assert_eq!( + session + .cells + .iter() + .find(|c| c.name == "bad") + .unwrap() + .last_result, + Value::None + ); + assert!(session.persist_cell("bad", "must_not_write").is_err()); + session.close(); + } +} diff --git a/src/arguments.rs b/src/arguments.rs new file mode 100644 index 0000000..9a95a3f --- /dev/null +++ b/src/arguments.rs @@ -0,0 +1,122 @@ +//! Value-independent argument binding. Effects finish before this plan is used. +use std::collections::HashMap; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct ArgumentPlan { + pub parameters: Vec>, + pub assignments: Vec<(String, usize)>, +} +pub(crate) fn plan( + params: &[String], + names: &[Option], + allow_missing: bool, +) -> Result { + plan_with(params, names, |_| allow_missing) +} + +pub(crate) fn plan_with_omissions( + params: &[String], + names: &[Option], + omissions: &[bool], +) -> Result { + if params.len() != omissions.len() { + return Err("argument omission mask count mismatch".into()); + } + plan_with(params, names, |index| omissions[index]) +} + +fn plan_with( + params: &[String], + names: &[Option], + may_omit: impl Fn(usize) -> bool, +) -> Result { + let mut named = HashMap::new(); + let mut positionals = Vec::new(); + for (index, name) in names.iter().enumerate() { + match name { + Some(name) => { + if named.insert(name.clone(), index).is_some() { + return Err(format!("duplicate named argument `{name}`")); + } + } + None => positionals.push(index), + } + } + let mut pos = 0; + let mut parameters = Vec::new(); + let mut assignments = Vec::new(); + for (parameter_index, param) in params.iter().enumerate() { + let index = if let Some(index) = named.get(param) { + Some(*index) + } else if let Some(index) = positionals.get(pos) { + pos += 1; + Some(*index) + } else if may_omit(parameter_index) { + None + } else { + return Err(format!("missing argument for parameter `{param}`")); + }; + parameters.push(index); + if let Some(index) = index { + assignments.push((param.clone(), index)); + } + } + if pos < positionals.len() { + return Err(format!( + "too many positional arguments ({} extra)", + positionals.len() - pos + )); + } + for (index, name) in names.iter().enumerate() { + if let Some(name) = name { + if !params.contains(name) { + assignments.push((name.clone(), index)); + } + } + } + Ok(ArgumentPlan { + parameters, + assignments, + }) +} +pub(crate) fn bind( + params: &[String], + names: &[Option], + values: &[T], + allow_missing: bool, +) -> Result, String> { + if names.len() != values.len() { + return Err("argument layout/value count mismatch".into()); + } + let plan = plan(params, names, allow_missing)?; + Ok(plan + .assignments + .into_iter() + .map(|(name, index)| (name, values[index].clone())) + .collect()) +} + +pub(crate) fn plan_evidence( + params: &[String], + names: &[Option], + omissions: &[bool], +) -> serde_json::Value { + let result = match plan_with_omissions(params, names, omissions) { + Ok(plan) => { + serde_json::json!({"ok":true,"parameters":plan.parameters,"assignments":plan.assignments}) + } + Err(error) => serde_json::json!({"ok":false,"error":error}), + }; + serde_json::json!({"params":params,"arguments":names,"omissions":omissions,"result":result}) +} +pub(crate) fn builtin_signature(name: &str) -> Option<(&'static [&'static str], bool)> { + Some(match name { + "print" => (&["text"], false), + "input" => (&["prompt"], true), + "len" | "str" | "int" | "type" | "trim" => (&["value"], false), + "split" | "join" => (&["value", "sep"], false), + "at" => (&["value", "index"], false), + "call_fn" => (&["name"], false), + _ => return None, + }) +} diff --git a/src/ast.rs b/src/ast.rs index 0914a4d..2e1c4e5 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -22,6 +22,7 @@ pub struct Use { #[derive(Debug, Clone)] pub struct Module { + pub source: crate::diagnostics::SourceIdentity, pub imports: Vec, pub uses: Vec, pub functions: Vec, @@ -36,6 +37,7 @@ pub struct Module { impl Default for Module { fn default() -> Self { Self { + source: Default::default(), imports: Vec::new(), uses: Vec::new(), functions: Vec::new(), @@ -46,9 +48,31 @@ impl Default for Module { } } +/// Parameter names have distinct slots. Function lookup retains its existing +/// first-match/ambiguity rules rather than narrowing accepted declaration trees. +pub(crate) fn validate_parameters(module:&Module) -> Result<(),crate::diagnostics::Diagnostic> { + fn siblings(module:&Module,functions:&[Function]) -> Result<(),crate::diagnostics::Diagnostic> { + for fun in functions { + let mut params=std::collections::HashSet::new(); + for param in &fun.params { + if !params.insert(¶m.name) { + return Err(crate::diagnostics::Diagnostic::new(module.source.path.as_deref(),param.span,format!("duplicate parameter `{}`",param.name)).with_source(&module.source)); + } + } + siblings(module,&fun.children)?; + } + Ok(()) + } + siblings(module,&module.functions)?; + let mut imports=module.import_modules.iter().collect::>();imports.sort_by_key(|(name,_)|*name); + for (_,child) in imports { validate_parameters(child)?; } + Ok(()) +} + #[derive(Debug, Clone)] pub struct Param { pub name: String, + pub span: Span, pub default: Option, /// True when collected by v0.3 body/prose inference (not an explicit `+` line). pub inferred: bool, @@ -91,6 +115,16 @@ pub fn function_uses_receiver(fun: &Function) -> bool { matches!(name, "self" | "自") } + fn call_uses_receiver(call: &CallExpr) -> bool { + call.receiver.as_deref().is_some_and(is_receiver) + || call.path.as_ref().is_some_and(|path| { + path.first().is_some_and(|name| is_receiver(name)) + }) + || call.args.iter().any(|arg| match arg { + Arg::Positional(value) | Arg::Named { value, .. } => expr_uses_receiver(value), + }) + } + fn expr_uses_receiver(expr: &Expr) -> bool { match expr { Expr::Var(name) => is_receiver(name), @@ -102,12 +136,7 @@ pub fn function_uses_receiver(fun: &Function) -> bool { Expr::Binary { left, right, .. } => { expr_uses_receiver(left) || expr_uses_receiver(right) } - Expr::Call(call) => { - call.receiver.as_deref().is_some_and(is_receiver) - || call.args.iter().any(|arg| match arg { - Arg::Positional(value) | Arg::Named { value, .. } => expr_uses_receiver(value), - }) - } + Expr::Call(call) => call_uses_receiver(call), Expr::List(items) => items.iter().any(expr_uses_receiver), Expr::Map(entries) => entries.iter().any(|(_, value)| expr_uses_receiver(value)), Expr::Index { base, label } => { @@ -124,12 +153,7 @@ pub fn function_uses_receiver(fun: &Function) -> bool { Stmt::Assign { value, .. } | Stmt::Return { value, .. } | Stmt::Expr { value, .. } => { expr_uses_receiver(value) } - Stmt::Call { call, .. } => { - call.receiver.as_deref().is_some_and(is_receiver) - || call.args.iter().any(|arg| match arg { - Arg::Positional(value) | Arg::Named { value, .. } => expr_uses_receiver(value), - }) - } + Stmt::Call { call, .. } => call_uses_receiver(call), Stmt::Branch { arms, .. } => arms.iter().any(|arm| { arm.condition.as_ref().is_some_and(expr_uses_receiver) || arm.body.iter().any(stmt_uses_receiver) @@ -143,7 +167,8 @@ pub fn function_uses_receiver(fun: &Function) -> bool { } } - fun.body.iter().any(stmt_uses_receiver) + fun.params.iter().any(|param| param.default.as_ref().is_some_and(expr_uses_receiver)) + || fun.body.iter().any(stmt_uses_receiver) } /// Source line of a statement (every variant carries a span). @@ -257,6 +282,15 @@ pub struct CallExpr { } impl CallExpr { + /// Shared execution preparation: normalize bare aliases before testing + /// modifier conflicts, matching the source evaluator's binding order. + pub fn for_evaluation(&self) -> Result { + let mut call=self.clone(); + if call.path.is_none() && call.receiver.is_none(){ + call.callee=crate::aliases::normalize_call_callee_and_args(&call.callee,&mut call.args); + } + call.with_modifiers_expanded() + } /// Fold `pre_modifiers` into leading `name=True` named args (conflicts → Err). pub fn with_modifiers_expanded(&self) -> Result { let mut call = self.clone(); diff --git a/src/builtin.rs b/src/builtin.rs index 61eb291..e30f07e 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -1,7 +1,29 @@ //! Built-in helpers shared by tree-walk and bytecode. +mod primitives; +pub use primitives::{eval_binary,eval_unary}; + use crate::value::Value; +/// Machine integer arithmetic has one profile-independent failure contract. +/// Overflow must remain a language diagnostic in debug, release and Wasm. +pub fn checked_int_binary(op: crate::ast::BinaryOp, left: i64, right: i64) -> Result { + use crate::ast::BinaryOp; + if matches!(op, BinaryOp::Div) && right == 0 { return Err("division by zero".into()); } + let result = match op { + BinaryOp::Add => left.checked_add(right), + BinaryOp::Sub => left.checked_sub(right), + BinaryOp::Mul => left.checked_mul(right), + BinaryOp::Div => left.checked_div(right), + _ => return Err("not an integer arithmetic operation".into()), + }; + result.ok_or_else(|| "integer overflow".into()) +} + +pub fn checked_int_neg(value: i64) -> Result { + value.checked_neg().ok_or_else(|| "integer overflow".into()) +} + /// `len`: text → Unicode scalar count; list → element count. pub fn builtin_len(v: &Value) -> Result { match v { @@ -20,7 +42,13 @@ pub fn builtin_str(v: &Value) -> Value { pub fn builtin_int(v: &Value) -> Result { match v { Value::Int(n) => Ok(*n), - Value::Num(n) => Ok(*n as i64), + Value::Num(n) => { + // The upper bound is exclusive: i64::MAX rounds to 2^63 as f64. + if !n.is_finite() || *n < -9223372036854775808.0 || *n >= 9223372036854775808.0 { + return Err("int conversion outside signed 64-bit domain".into()); + } + Ok(*n as i64) + } Value::Bool(true) => Ok(1), Value::Bool(false) => Ok(0), Value::Text(s) => { diff --git a/src/builtin/primitives.rs b/src/builtin/primitives.rs new file mode 100644 index 0000000..ab8b25c --- /dev/null +++ b/src/builtin/primitives.rs @@ -0,0 +1,88 @@ +//! Scalar operator semantics shared by source evaluation and VM. +use crate::{ast::BinaryOp, value::Value}; +pub fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result { + match op { + BinaryOp::Add => match (l, r) { + (Value::Int(a), Value::Int(b)) => { + crate::builtin::checked_int_binary(op, *a, *b).map(Value::Int) + } + (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a + b)), + (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 + b)), + (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a + *b as f64)), + (Value::Text(a), Value::Text(b)) => Ok(Value::Text(format!("{a}{b}"))), + (Value::Text(a), Value::Secret(b)) => Ok(Value::Text(format!("{a}{b}"))), + (Value::Secret(a), Value::Text(b)) => Ok(Value::Text(format!("{a}{b}"))), + (Value::Secret(a), Value::Secret(b)) => Ok(Value::Text(format!("{a}{b}"))), + (Value::Text(a), b) => Ok(Value::Text(format!("{a}{}", b.as_display()))), + (a, Value::Text(b)) => Ok(Value::Text(format!("{}{b}", a.as_display()))), + _ => Err("`+` needs ints, nums, or text".into()), + }, + BinaryOp::Sub => match (l, r) { + (Value::Int(a), Value::Int(b)) => { + crate::builtin::checked_int_binary(op, *a, *b).map(Value::Int) + } + (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a - b)), + (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 - b)), + (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a - *b as f64)), + _ => Err("`-` needs ints or nums".into()), + }, + BinaryOp::Mul => match (l, r) { + (Value::Int(a), Value::Int(b)) => { + crate::builtin::checked_int_binary(op, *a, *b).map(Value::Int) + } + (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a * b)), + (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 * b)), + (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a * *b as f64)), + _ => Err("`*` needs ints or nums".into()), + }, + BinaryOp::Div => match (l, r) { + (Value::Int(_), Value::Int(0)) => Err("division by zero".into()), + (Value::Int(a), Value::Int(b)) => { + crate::builtin::checked_int_binary(op, *a, *b).map(Value::Int) + } + (Value::Num(_), Value::Num(b)) if *b == 0.0 => Err("division by zero".into()), + (Value::Int(_), Value::Num(b)) if *b == 0.0 => Err("division by zero".into()), + (Value::Num(_), Value::Int(0)) => Err("division by zero".into()), + (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a / b)), + (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 / b)), + (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a / *b as f64)), + _ => Err("`/` needs ints or nums".into()), + }, + BinaryOp::Eq => Ok(Value::Bool(l == r)), + BinaryOp::Ne => Ok(Value::Bool(l != r)), + BinaryOp::Lt => cmp_ord(l, r, |o| o.is_lt()), + BinaryOp::Le => cmp_ord(l, r, |o| o.is_le()), + BinaryOp::Gt => cmp_ord(l, r, |o| o.is_gt()), + BinaryOp::Ge => cmp_ord(l, r, |o| o.is_ge()), + BinaryOp::And | BinaryOp::Or => unreachable!("handled above"), + } +} + +fn cmp_ord(l: &Value, r: &Value, f: impl Fn(std::cmp::Ordering) -> bool) -> Result { + match (l, r) { + (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(f(a.cmp(b)))), + (Value::Num(a), Value::Num(b)) => Ok(Value::Bool(f(a + .partial_cmp(b) + .unwrap_or(std::cmp::Ordering::Equal)))), + (Value::Int(a), Value::Num(b)) => Ok(Value::Bool(f((*a as f64) + .partial_cmp(b) + .unwrap_or(std::cmp::Ordering::Equal)))), + (Value::Num(a), Value::Int(b)) => Ok(Value::Bool(f(a + .partial_cmp(&(*b as f64)) + .unwrap_or(std::cmp::Ordering::Equal)))), + (Value::Text(a), Value::Text(b)) => Ok(Value::Bool(f(a.cmp(b)))), + _ => Err("comparison needs two ints/nums or two texts".into()), + } +} + +/// Scalar unary semantics shared by source and target execution. +pub fn eval_unary(op: crate::ast::UnaryOp, value: &Value) -> Result { + match op { + crate::ast::UnaryOp::Not => Ok(Value::Bool(!value.truthy())), + crate::ast::UnaryOp::Neg => match value { + Value::Int(n) => crate::builtin::checked_int_neg(*n).map(Value::Int), + Value::Num(n) => Ok(Value::Num(-n)), + _ => Err("unary `-` needs int or num".into()), + }, + } +} diff --git a/src/bytecode/call.rs b/src/bytecode/call.rs new file mode 100644 index 0000000..c1e63a5 --- /dev/null +++ b/src/bytecode/call.rs @@ -0,0 +1,327 @@ +//! Check actual staging, scratch separation and post-evaluation call selection. +//! Resolution and dispatch implementations remain semantic interfaces. +use super::{expression::Cursor, ExpressionRule, FnChunk, Op, Program}; +use crate::{ + ast::{Arg, CallExpr}, + value::Value, +}; +use std::collections::HashSet; + +#[derive(Debug, Clone)] +pub struct CallStage { + pub expression: usize, + pub slot: usize, +} +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum PathFallback { + User { target: usize, tag_instance: bool }, + Error { message: String }, +} +impl PathFallback { + fn evidence(&self) -> serde_json::Value { + match self { + Self::User { target, tag_instance } => serde_json::json!({"kind":"user","target":target,"tag_instance":tag_instance}), + Self::Error { message } => serde_json::json!({"kind":"error","message":message}), + } + } +} +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum CallDispatch { + User { target: usize, tag_instance: bool }, + Method { receiver: String }, + Builtin, + Host { function: u16 }, + Plugin, + RejectDuplicate { name: String }, + Path { receiver: String, check_receiver: bool, fallback: PathFallback }, + ResolutionFault { message: String }, +} +#[derive(Debug, Clone)] +pub struct CallEmission { + pub expression: usize, + /// Expanded and normalized source, captured before argument replacement. + pub source: CallExpr, + pub stages: Vec, + pub dispatch: Option, + pub as_statement: bool, +} +impl CallEmission { + pub fn binding_evidence(&self,program:&Program)->serde_json::Value { + let signature=match self.dispatch.as_ref() { + Some(CallDispatch::User{target,..})|Some(CallDispatch::Path{fallback:PathFallback::User{target,..},..})=>{ + program.functions.get(*target).map(|callee|(callee.params.clone(),callee.defaults.clone())) + }, + Some(CallDispatch::Builtin)=>crate::arguments::builtin_signature(&self.source.callee).map(|(params,optional)| + (params.iter().map(|p|p.to_string()).collect::>(),vec![optional;params.len()])), + Some(CallDispatch::Host{function})=>crate::host::HostFn::from_u16(*function).map(|h|{ + let params=h.all_params();let len=params.len();(params.into_iter().map(str::to_string).collect(),vec![true;len]) + }), + _=>None, + }; + match signature { + Some((params,omissions))=>crate::arguments::plan_evidence(¶ms,&names(self),&omissions), + None=>serde_json::Value::Null, + } + } + pub fn evidence(&self) -> serde_json::Value { + use serde_json::json; + let dispatch = match &self.dispatch { + Some(CallDispatch::User { + target, + tag_instance, + }) => json!({"kind":"user","target":target,"tag_instance":tag_instance}), + Some(CallDispatch::Method { receiver }) => json!({"kind":"method","receiver":receiver}), + Some(CallDispatch::Builtin) => json!({"kind":"builtin"}), + Some(CallDispatch::Host { function }) => { + let host = crate::host::HostFn::from_u16(*function); + json!({"kind":"host","function":function,"params":host.map(|h|h.all_params()),"required":host.map(|h|h.required_params().len())}) + } + Some(CallDispatch::Plugin) => json!({"kind":"plugin"}), + Some(CallDispatch::RejectDuplicate { name }) => json!({"kind":"duplicate","name":name}), + Some(CallDispatch::Path { receiver, check_receiver, fallback }) => json!({"kind":"path","receiver":receiver,"check_receiver":check_receiver,"fallback":fallback.evidence()}), + Some(CallDispatch::ResolutionFault { message }) => json!({"kind":"resolution_fault","message":message}), + None => serde_json::Value::Null, + }; + json!({"expression":self.expression,"callee":self.source.callee,"receiver":self.source.receiver,"path":self.source.path, + "arguments":self.source.args.iter().map(|a|match a {Arg::Positional(_)=>None,Arg::Named{name,..}=>Some(name)}).collect::>(), + "source_rules":self.source.args.iter().map(|a|match a {Arg::Positional(e)|Arg::Named{value:e,..}=>ExpressionRule::from_source(e).evidence()}).collect::>(), + "stages":self.stages.iter().map(|s|json!({"expression":s.expression,"slot":s.slot})).collect::>(), + "dispatch":dispatch,"as_statement":self.as_statement}) + } +} +fn names(call: &CallEmission) -> Vec> { + call.source + .args + .iter() + .map(|a| match a { + Arg::Positional(_) => None, + Arg::Named { name, .. } => Some(name.clone()), + }) + .collect() +} +fn read(c: &mut Cursor<'_>, slot: usize) -> Result<(), String> { + c.op(Op::GetLocal(slot)) +} +fn read_child( + c: &mut Cursor<'_>, + ids: &mut std::slice::Iter<'_, usize>, + slot: usize, +) -> Result<(), String> { + let id = *ids.next().ok_or("missing staged argument reload witness")?; + let expr = c + .fun + .expressions + .get(id) + .ok_or("missing reload expression")?; + if expr.start != c.position + || expr.end != c.position + 1 + || !matches!(&expr.rule,ExpressionRule::Read(name) if c.fun.locals.get(slot)==Some(name)) + || !expr.children.is_empty() + { + return Err("staged argument reload source differs".into()); + } + read(c, slot) +} +fn layout(c: &mut Cursor<'_>, arguments: &[Option]) -> Result { + let id = match c.fun.code.get(c.position) { + Some(Op::CallArgs(_, id) | Op::MethodCallArgs(_, id)) => *id, + _ => return Err("missing argument-layout call".into()), + }; + if c.fun.call_layouts.get(id).map(Vec::as_slice) != Some(arguments) { + return Err("call argument layout differs from source".into()); + } + Ok(id) +} +pub fn verify(program: &Program, fun: &FnChunk) -> Result<(), String> { + let caller=program.functions.iter().position(|f|std::ptr::eq(f,fun)).ok_or("missing call owner")?; + if fun.source_local_count < fun.params.len() || fun.source_local_count > fun.locals.len() { + return Err("invalid source/scratch local boundary".into()); + } + let mut owners = HashSet::new(); + let loops = fun + .statements + .iter() + .flat_map(|s| s.auxiliary.iter().copied()) + .collect::>(); + let mut active: Vec<(&super::ExpressionEmission, HashSet)> = Vec::new(); + for call in &fun.calls { + let node = fun + .expressions + .get(call.expression) + .ok_or("missing call source expression")?; + if !matches!(node.rule, ExpressionRule::CallBoundary(_)) + || !owners.insert(call.expression) + || call.as_statement + { + return Err("call source owner differs".into()); + } + let arguments = names(call); + if call.stages.len() != arguments.len() || node.children.len() < arguments.len() { + return Err("call authored-actual inventory".into()); + } + let slots = call.stages.iter().map(|s| s.slot).collect::>(); + if slots.len() != arguments.len() + || slots + .iter() + .any(|s| *s < fun.source_local_count || *s >= fun.locals.len() || loops.contains(s)) + { + return Err("call scratch aliases source/loop/another actual".into()); + } + while active + .last() + .is_some_and(|(parent, _)| parent.end <= node.start) + { + active.pop(); + } + if active.iter().any(|(parent, other)| { + node.start < parent.start || node.end > parent.end || !slots.is_disjoint(other) + }) { + return Err("nested call scratch overlap or interval order".into()); + } + let mut c = Cursor { + fun, + position: node.start, + end: node.end, + }; + for (i, stage) in call.stages.iter().enumerate() { + if node.children[i] != stage.expression { + return Err("authored actual order differs".into()); + } + let source = match &call.source.args[i] { + Arg::Positional(e) | Arg::Named { value: e, .. } => e, + }; + if ExpressionRule::from_source(source).evidence() + != fun.expressions[stage.expression].rule.evidence() + { + return Err("authored argument source rule differs".into()); + } + c.child(stage.expression)?; + c.op(Op::SetLocal(stage.slot))?; + c.op(Op::Pop)?; + } + let mut reloads = node.children[arguments.len()..].iter(); + let dispatch=call.dispatch.as_ref().ok_or("missing call dispatch obligation")?; + use crate::sema::{DynamicReason, resolution::Target}; + let target=super::names::resolve(program,caller,&call.source); + let user=match &target { Target::Local(id)|Target::Import{fun:id,..}=>Some(*id),_=>None }; + let user_dispatch=|id:usize|CallDispatch::User{target:id,tag_instance:program.parents[id].is_none()&&program.functions[id].source_ast.level==1}; + let expected=if let Some(receiver)=&call.source.receiver { + CallDispatch::Method{receiver:receiver.clone()} + } else if let Some(path)=call.source.path.as_ref().filter(|p|p.len()==2) { + let fallback=match user.map(user_dispatch) { + Some(CallDispatch::User{target,tag_instance})=>PathFallback::User{target,tag_instance}, + _=>PathFallback::Error{message:match target {Target::Unresolved{message,..}=>message,_=>format!("unresolved dynamic call `{}`",crate::sema::call_display(&call.source))}}, + }; + CallDispatch::Path{receiver:path[0].clone(),check_receiver:!fun.source_ast.imports.contains(&path[0]),fallback} + } else if let Some(id)=user {user_dispatch(id)} else {match target { + Target::Builtin(_)|Target::Dynamic{reason:DynamicReason::CallFn,..}=>CallDispatch::Builtin, + Target::Host(h)=>CallDispatch::Host{function:h.as_u16()}, + Target::Dynamic{reason:DynamicReason::Plugin,..}=>CallDispatch::Plugin, + Target::Unresolved{message,..}=>CallDispatch::ResolutionFault{message}, + _=>return Err("unmodeled source call resolution".into()), + }}; + if *dispatch!=expected{return Err("call resolution differs from source declaration graph".into());} + match dispatch { + CallDispatch::Path{..}=>{ + for stage in &call.stages{read_child(&mut c,&mut reloads,stage.slot)?;} + let id=match fun.code.get(c.position){Some(Op::PathCallArgs(owner,id)) if fun.calls.get(*owner).is_some_and(|other|std::ptr::eq(other,call))=>*id,_=>return Err("path call owner differs".into())}; + if fun.call_layouts.get(id)!=Some(&arguments){return Err("path call layout differs".into());} + let owner=fun.calls.iter().position(|other|std::ptr::eq(other,call)).unwrap(); + c.op(Op::PathCallArgs(owner,id))?; + }, + CallDispatch::ResolutionFault{message}=>{ + let index=match fun.code.get(c.position){Some(Op::RejectCallResolution(index))=>*index,_=>return Err("missing call resolution fault".into())}; + if fun.constants.get(index)!=Some(&Value::Text(message.clone())){return Err("call resolution fault payload differs".into());} + c.op(Op::RejectCallResolution(index))?; + }, + CallDispatch::RejectDuplicate { name } => { + let index = match fun.code.get(c.position) { + Some(Op::RejectDuplicateArgument(i)) => *i, + _ => return Err("duplicate argument refusal missing".into()), + }; + if fun.constants.get(index) != Some(&Value::Text(name.clone())) { + return Err("duplicate argument name differs".into()); + } + c.op(Op::RejectDuplicateArgument(index))?; + } + CallDispatch::User { + target, + tag_instance, + } => { + if *target >= program.functions.len() + || *tag_instance != program.objects.iter().any(|(_, id)| id == target) + { + return Err("resolved call target/tag differs".into()); + } + for stage in &call.stages { + read_child(&mut c, &mut reloads, stage.slot)?; + } + let id = layout(&mut c, &arguments)?; + c.op(Op::CallArgs(*target, id))?; + if *tag_instance { + c.op(Op::TagInstance(*target))?; + } + } + CallDispatch::Method { receiver } => { + if call.source.receiver.as_ref() != Some(receiver) + && call.source.path.as_ref().and_then(|p| p.first()) != Some(receiver) + { + return Err("method receiver differs from source".into()); + } + for stage in &call.stages { + read_child(&mut c, &mut reloads, stage.slot)?; + } + let slot = fun + .locals + .iter() + .position(|n| n == receiver) + .ok_or("method receiver slot")?; + read(&mut c, slot)?; + let index = match fun.code.get(c.position) { + Some(Op::MethodCallArgs(i, _)) => *i, + _ => return Err("method dispatch missing".into()), + }; + if fun.constants.get(index) != Some(&Value::Text(call.source.callee.clone())) { + return Err("method name differs from source".into()); + } + let id = layout(&mut c, &arguments)?; + c.op(Op::MethodCallArgs(index, id))?; + } + CallDispatch::Builtin|CallDispatch::Plugin|CallDispatch::Host{..}=>{ + for stage in &call.stages{read_child(&mut c,&mut reloads,stage.slot)?;} + let (first,id)=match fun.code.get(c.position){Some(Op::BuiltinCallArgs(n,l)|Op::PluginCallArgs(n,l))=>(*n,*l),Some(Op::HostCallArgs(n,l))=>(*n as usize,*l),_=>return Err("missing native argument dispatch".into())}; + if fun.call_layouts.get(id)!=Some(&arguments){return Err("native argument layout differs".into());} + match dispatch { + CallDispatch::Builtin=>{ + if crate::arguments::builtin_signature(&call.source.callee).is_none() || fun.constants.get(first)!=Some(&Value::Text(call.source.callee.clone())){return Err("builtin identity differs".into());} + c.op(Op::BuiltinCallArgs(first,id))?; + }, + CallDispatch::Plugin=>{ + if fun.constants.get(first)!=Some(&Value::Text(call.source.callee.clone())){return Err("plugin identity differs".into());} + c.op(Op::PluginCallArgs(first,id))?; + }, + CallDispatch::Host{function}=>{ + let hf=crate::host::HostFn::from_name(&call.source.callee).ok_or("source host name")?; + if hf.as_u16()!=*function || first!=*function as usize{return Err("host primitive identity differs".into());} + c.op(Op::HostCallArgs(*function,id))?; + },_=>unreachable!(), + } + } + + } + if reloads.next().is_some() || c.position != node.end { + return Err("call instruction/expression coverage differs".into()); + } + active.push((node, slots)); + } + if fun + .expressions + .iter() + .filter(|e| matches!(e.rule, ExpressionRule::CallBoundary(_))) + .count() + != owners.len() + { + return Err("uncovered call source expression".into()); + } + Ok(()) +} diff --git a/src/bytecode/compile.rs b/src/bytecode/compile.rs index 6bccbc3..4961f43 100644 --- a/src/bytecode/compile.rs +++ b/src/bytecode/compile.rs @@ -11,7 +11,11 @@ use crate::bytecode::{FnChunk, Op, Program}; use crate::diagnostics::{bail_at, Span}; use crate::value::Value; -struct FlatFun { +struct FlatFun<'m> { + owner: &'m Module, + definition: &'m Function, + source: crate::diagnostics::SourceIdentity, + module_path: Vec, name: String, level: u8, /// Receiver-free children of an object are associated functions, not @@ -21,6 +25,8 @@ struct FlatFun { /// Inheritance base type name (`# Child = > Parent`), objects only. base: Option, params: Vec, + contract: Option, + var_contracts: HashMap, body: Vec, children: Vec, span: Span, @@ -43,21 +49,27 @@ pub fn compile_for_call(path: Option<&Path>, module: &Module) -> Result } fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Result { + crate::ast::validate_parameters(module)?; let mut flat = Vec::new(); let mut next_scope = 1u32; - let _exports = collect_module_tree(module, &mut flat, 0, &mut next_scope); + let _exports = collect_module_tree(module, &mut flat, 0, &mut next_scope, &[]); + for fun in &mut flat { + if fun.scope == 0 && fun.source.path.is_none() { + fun.source.path = path.map(Path::to_path_buf); + } + } if flat.is_empty() { bail!("bytecode: no functions to compile"); } let chosen = flat .iter() - .position(|f| f.name == "main" && f.parent.is_none()) + .position(|f| f.scope == 0 && f.name == "main" && f.parent.is_none()) .or_else(|| { let tops: Vec = flat .iter() .enumerate() - .filter(|(_, f)| f.parent.is_none() && f.level == 1 && f.params.is_empty()) + .filter(|(_, f)| f.scope == 0 && f.parent.is_none() && f.level == 1 && f.params.is_empty()) .map(|(i, _)| i) .collect(); if tops.len() == 1 { @@ -72,7 +84,11 @@ fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Re None => bail!("bytecode: no `# main` object"), }; // Imports are compiled first, so a missing entry must take paths from the file's own functions. - let path_owner = if chosen.is_some() { entry } else { flat.len() - 1 }; + let path_owner = if chosen.is_some() { + entry + } else { + flat.len() - 1 + }; let mut objects = Vec::new(); let mut methods = Vec::new(); @@ -84,21 +100,23 @@ fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Re } } } - let name_to_flat: HashMap<&str, usize> = objects - .iter() - .map(|(n, i)| (n.as_str(), *i)) - .collect(); let mut object_bases = Vec::with_capacity(objects.len()); - for (name, _) in &objects { - let flat_i = name_to_flat[name.as_str()]; - let base = flat[flat_i] - .base - .as_deref() - .and_then(|b| name_to_flat.get(b).copied()); + for (_, flat_i) in &objects { + let owner = &flat[*flat_i]; + let base = if let Some(base) = &owner.base { + let path = owner.uses.get(base).cloned().unwrap_or_else(|| vec![base.clone()]); + let (name, prefix) = path.split_last().expect("base path"); + let mut wanted = owner.module_path.clone(); + wanted.extend_from_slice(prefix); + let local = objects.iter().find(|(n,id)| n == name && flat[*id].module_path == wanted).map(|(_,id)| *id); + if local.is_some() || !prefix.is_empty() { local } else { + let candidates = objects.iter().filter(|(n,id)| n == name && flat[*id].module_path.starts_with(&owner.module_path)).map(|(_,id)| *id).collect::>(); + if candidates.len() == 1 { Some(candidates[0]) } else { None } + } + } else { None }; object_bases.push(base); } - let path_buf = path.map(|p| p.to_path_buf()); let mut functions = Vec::new(); for i in 0..flat.len() { let meta = if i == entry { @@ -106,14 +124,23 @@ fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Re } else { None }; - functions.push(compile_function(i, &flat, path_buf.as_deref(), meta)?); + let compiled = + compile_function(i, &flat, flat[i].source.path.as_deref(), meta).map_err(|error| { + if let Some(diagnostic) = crate::diagnostics::Diagnostic::find(&error).cloned() { + anyhow::Error::new(diagnostic.with_source(&flat[i].source)) + } else { + error + } + })?; + functions.push(compiled); } let parents: Vec> = flat.iter().map(|f| f.parent).collect(); let children: Vec> = flat.iter().map(|f| f.children.clone()).collect(); let path_index = flat[path_owner].path_index.clone(); - Ok(Program { + let program=Program { + entry_metadata: module.metadata.clone(), functions, entry, objects, @@ -122,7 +149,9 @@ fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Re parents, children, path_index, - }) + }; + program.verify().map_err(|error|anyhow::anyhow!(error))?; + Ok(program) } /// Collect `module` and its imports into `flat`. @@ -131,17 +160,21 @@ fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Re /// Each module gets a distinct `scope` id. Bare-name `resolve_call` only searches /// within the caller's scope so imported same-named functions cannot be silently /// selected (BoundCall / G02). Cross-module calls must use `bind.member` paths. -fn collect_module_tree( - module: &Module, - flat: &mut Vec, +fn collect_module_tree<'m>( + module: &'m Module, + flat: &mut Vec>, scope: u32, next_scope: &mut u32, + module_path: &[String], ) -> HashMap, usize> { let mut path_index = HashMap::new(); - for (bind, dep) in &module.import_modules { + let mut imports = module.import_modules.iter().collect::>(); + imports.sort_by_key(|(name,_)| *name); + for (bind, dep) in imports { let child_scope = *next_scope; *next_scope += 1; - let dep_exports = collect_module_tree(dep, flat, child_scope, next_scope); + let mut child_path=module_path.to_vec(); child_path.push(bind.clone()); + let dep_exports = collect_module_tree(dep, flat, child_scope, next_scope, &child_path); for (rel, idx) in dep_exports { let mut full = Vec::with_capacity(rel.len() + 1); full.push(bind.clone()); @@ -159,7 +192,7 @@ fn collect_module_tree( let start = flat.len(); let mut exports = HashMap::new(); for fun in &module.functions { - let id = collect_fun(fun, None, flat, &path_index, &uses, scope); + let id = collect_fun(fun, module, None, flat, &path_index, &uses, scope, &module.source, module_path); export_tree(&mut exports, &[], id, flat); } // Same-module BoundCall paths (`Type.member`) must be visible to siblings. @@ -184,28 +217,36 @@ fn export_tree( ) { let mut p = prefix.to_vec(); p.push(flat[id].name.clone()); - out.insert(p.clone(), id); + out.entry(p.clone()).or_insert(id); for &cid in &flat[id].children { export_tree(out, &p, cid, flat); } } -fn collect_fun( - fun: &Function, +fn collect_fun<'m>( + fun: &'m Function, + owner: &'m Module, parent: Option, - flat: &mut Vec, + flat: &mut Vec>, path_index: &HashMap, usize>, uses: &HashMap>, scope: u32, + source: &crate::diagnostics::SourceIdentity, + module_path: &[String], ) -> usize { let id = flat.len(); flat.push(FlatFun { + owner, definition: fun, + source: source.clone(), + module_path: module_path.to_vec(), name: fun.name.clone(), level: fun.level, uses_receiver: crate::ast::function_uses_receiver(fun), parent, base: fun.base.clone(), params: fun.params.clone(), + contract: fun.contract.clone(), + var_contracts: fun.var_contracts.clone(), body: fun.body.clone(), children: Vec::new(), span: fun.span, @@ -215,19 +256,87 @@ fn collect_fun( }); let mut child_ids = Vec::new(); for child in &fun.children { - child_ids.push(collect_fun(child, Some(id), flat, path_index, uses, scope)); + child_ids.push(collect_fun( + child, + owner, + Some(id), + flat, + path_index, + uses, + scope, + source, + module_path, + )); } flat[id].children = child_ids; id } +#[derive(Hash, PartialEq, Eq)] +enum ConstantKey { + None, + Bool(bool), + Int(i64), + Num(u64), + Text(String), +} + struct FnCompiler<'a> { path: Option<&'a Path>, - flat: &'a [FlatFun], + flat: &'a [FlatFun<'a>], fn_id: usize, chunk: FnChunk, - locals: HashMap, + locals: HashMap, stmt_span: Span, + call_depth: usize, + constant_indices: HashMap, + argument_scratch: HashMap<(usize,usize),(String,usize)>, + expression_stack: Vec, + statement_stack: Vec, + call_emission_stack: Vec, +} + +/// Reserve source names before synthesizing scratch names. This covers future +/// statements as well as defaults, so even an authored backtick name containing +/// the internal prefix cannot alias a temporary. +fn reserve_function_names(fun:&FlatFun,locals:&mut HashMap) { + fn name(name:&str,locals:&mut HashMap) { + if !locals.contains_key(name) { let slot=locals.len();locals.insert(name.into(),slot); } + } + fn call(call:&CallExpr,locals:&mut HashMap) { + if let Some(receiver)=&call.receiver { name(receiver,locals); } + if let Some(path)=&call.path { if let Some(first)=path.first() { name(first,locals); } } + for arg in &call.args { match arg { Arg::Named{value,..}|Arg::Positional(value)=>expr(value,locals) } } + } + fn expr(value:&Expr,locals:&mut HashMap) { + match value { + Expr::Var(value)=>name(value,locals), + Expr::Interp(parts)=>for part in parts { match part { + InterpPart::Lit(_)=>{}, + InterpPart::Var(value)|InterpPart::Index{base:value,..}=>name(value,locals), + } }, + Expr::Unary{expr:value,..}=>expr(value,locals), + Expr::Binary{left,right,..}=>{expr(left,locals);expr(right,locals);}, + Expr::Call(value)=>call(value,locals), + Expr::List(values)=>for value in values {expr(value,locals);}, + Expr::Map(values)=>for (_,value) in values {expr(value,locals);}, + Expr::Index{base,label}=>{expr(base,locals);if let crate::ast::IndexKey::Var(value)=label {name(value,locals);}}, + Expr::Occluded{payload,..}=>if let Some(value)=payload {expr(value,locals);}, + Expr::Literal(_)|Expr::Formula(_)|Expr::Code(_)=>{}, + } + } + fn block(statements:&[Stmt],locals:&mut HashMap) { + for statement in statements { match statement { + Stmt::Assign{name:key,value,..}=>{name(key,locals);expr(value,locals);}, + Stmt::Return{value,..}|Stmt::Expr{value,..}=>expr(value,locals), + Stmt::Call{call:value,..}=>call(value,locals), + Stmt::Branch{arms,..}=>for arm in arms {if let Some(condition)=&arm.condition {expr(condition,locals);}block(&arm.body,locals);}, + Stmt::While{condition,body,..}=>{expr(condition,locals);block(body,locals);}, + Stmt::ForEach{item,collection,body,..}=>{name(item,locals);name(collection,locals);block(body,locals);}, + } } + } + for param in &fun.params {if let Some(default)=¶m.default {expr(default,locals);}} + block(&fun.body,locals); } fn compile_function( @@ -239,13 +348,13 @@ fn compile_function( let fun = &flat[fn_id]; let mut locals = HashMap::new(); for (i, p) in fun.params.iter().enumerate() { - locals.insert(p.name.clone(), i as u8); + locals.insert(p.name.clone(), i as usize); } let is_method = fun.uses_receiver; if is_method { for name in ["自", "self"] { if !locals.contains_key(name) { - let s = locals.len() as u8; + let s = locals.len() as usize; locals.insert(name.to_string(), s); } } @@ -254,7 +363,7 @@ fn compile_function( if let Some(meta) = metadata { for (k, _) in meta { if !locals.contains_key(k) { - let s = locals.len() as u8; + let s = locals.len() as usize; locals.insert(k.clone(), s); } } @@ -264,8 +373,22 @@ fn compile_function( flat, fn_id, chunk: FnChunk { + source_ast: crate::bytecode::SourceFunction::from_declaration(fun.owner, fun.definition), + module_scope: fun.scope, + module_path: fun.module_path.clone(), + source: fun.source.clone(), + declaration_span: fun.span, name: fun.name.clone(), params: fun.params.iter().map(|p| p.name.clone()).collect(), + contract: fun.contract.clone(), + var_contracts: fun.var_contracts.clone(), + receiver_aliases: is_method, + defaults: fun.params.iter().map(|p| p.default.is_some()).collect(), + default_expressions:vec![None;fun.params.len()], + call_layouts: Vec::new(), + calls: Vec::new(), source_local_count:0, + expressions: Vec::new(), + statements: Vec::new(),statement_roots: Vec::new(),body_start:0, code: Vec::new(), spans: Vec::new(), constants: Vec::new(), @@ -273,17 +396,37 @@ fn compile_function( }, locals, stmt_span: fun.span, + call_depth: 0, + constant_indices: HashMap::new(), + argument_scratch: HashMap::new(), + expression_stack: Vec::new(), + statement_stack: Vec::new(), + call_emission_stack: Vec::new(), }; - // Prologue: seed entry locals from bound metadata. - if let Some(meta) = metadata { - for (k, v) in meta { - let slot = *c.locals.get(k).expect("metadata local reserved"); - let ci = c.add_const(v.clone()); - c.emit(Op::Constant(ci)); - c.emit(Op::SetLocal(slot)); + reserve_function_names(fun,&mut c.locals); + c.chunk.source_local_count=c.locals.len(); + // Defaults execute in the declaration's lexical scope and callee frame. + // None is a valid supplied value, so omission has a separate presence bit. + for (index, param) in fun.params.iter().enumerate() { + if let Some(default) = ¶m.default { + c.stmt_span=param.span; + let jump = c.here(); + c.emit(Op::JumpIfProvided(index as usize, 0)); + c.chunk.default_expressions[index]=Some(c.chunk.expressions.len()); + c.compile_expr(default)?; + c.emit(Op::SetLocal(index as usize)); c.emit(Op::Pop); + c.patch(jump, Op::JumpIfProvided(index as usize, c.here())); } } + c.stmt_span=fun.span; + // Metadata is an input to Program::run, not code baked into a function. + // Fill only after defaults, so a later omitted default stays unbound while + // an earlier default evaluates. + c.emit(Op::FillMissingParameters); + if fun.contract.is_some() { c.emit(Op::CheckArguments); } + if is_method {c.emit(Op::RestoreReceiver);if fun.contract.is_some(){c.emit(Op::CheckFrameArguments);}} + c.chunk.body_start=c.here(); for stmt in &fun.body { c.compile_stmt(stmt)?; } @@ -309,121 +452,81 @@ impl<'a> FnCompiler<'a> { self.chunk.spans.push(self.stmt_span); } - fn named_or_first<'b>(&self, call: &'b CallExpr, name: &str) -> Option<&'b Expr> { - call.args - .iter() - .find_map(|a| match a { - Arg::Named { name: n, value } if n == name => Some(value), - _ => None, - }) - .or_else(|| { - call.args.iter().find_map(|a| match a { - Arg::Positional(e) => Some(e), - _ => None, - }) - }) + fn here(&self) -> usize { + self.chunk.code.len() as usize } - fn named_or_pos<'b>(&self, call: &'b CallExpr, name: &str, pos: usize) -> Option<&'b Expr> { - if let Some(e) = call.args.iter().find_map(|a| match a { - Arg::Named { name: n, value } if n == name => Some(value), - _ => None, - }) { - return Some(e); - } - let mut i = 0usize; - for a in &call.args { - if let Arg::Positional(e) = a { - if i == pos { - return Some(e); - } - i += 1; - } - } - None - } - - fn here(&self) -> u16 { - self.chunk.code.len() as u16 - } - - fn patch(&mut self, at: u16, op: Op) { + fn patch(&mut self, at: usize, op: Op) { self.chunk.code[at as usize] = op; } - fn add_const(&mut self, v: Value) -> u16 { - if let Some(i) = self.chunk.constants.iter().position(|c| c == &v) { - return i as u16; + fn add_const(&mut self, v: Value) -> usize { + // Index widths follow Vec rather than truncating at 8/16 bits. Scalar + // interning is linear overall; float keys preserve their exact bits. + let key = match &v { + Value::None => Some(ConstantKey::None), + Value::Bool(value) => Some(ConstantKey::Bool(*value)), + Value::Int(value) => Some(ConstantKey::Int(*value)), + Value::Num(value) => Some(ConstantKey::Num(value.to_bits())), + Value::Text(value) => Some(ConstantKey::Text(value.clone())), + _ => None, + }; + if let Some(index) = key.as_ref().and_then(|key| self.constant_indices.get(key)) { + return *index; } - let i = self.chunk.constants.len() as u16; + let i = self.chunk.constants.len(); + if let Some(key) = key { self.constant_indices.insert(key, i); } self.chunk.constants.push(v); i } - fn local_slot(&mut self, name: &str) -> u8 { + fn fresh_slot(&mut self,label:&str)->usize { + let mut name=format!("\0{label}.{}",self.locals.len()); + while self.locals.contains_key(&name){name.push('\0');} + let slot=self.locals.len();self.locals.insert(name,slot);slot + } + + fn local_slot(&mut self, name: &str) -> usize { if let Some(&s) = self.locals.get(name) { return s; } - let s = self.locals.len() as u8; + let s = self.locals.len() as usize; self.locals.insert(name.to_string(), s); s } - /// BoundCall-equivalent bare-name resolve (G02): only the caller's module - /// scope. Imported functions are reachable solely via `path_index` paths. - fn resolve_call(&self, name: &str) -> Result { - let fun = &self.flat[self.fn_id]; - let scope = fun.scope; - for &cid in &fun.children { - if self.flat[cid].name == name && self.flat[cid].scope == scope { - return Ok(cid); - } - } - let mut parent = fun.parent; - while let Some(pid) = parent { - if self.flat[pid].scope != scope { - break; - } - for &cid in &self.flat[pid].children { - if self.flat[cid].name == name && self.flat[cid].scope == scope { - return Ok(cid); - } - } - parent = self.flat[pid].parent; - } - let mut matches = Vec::new(); - for (i, f) in self.flat.iter().enumerate() { - if f.parent.is_none() && f.scope == scope && f.name == name { - matches.push(i); - } - } - match matches.as_slice() { - [only] => Ok(*only), - [] => Err(self.err(format!("unknown function `{name}`"))), - _ => Err(self.err(format!( - "ambiguous function `{name}`: {} candidates in this module; use an explicit path", - matches.len() - ))), - } + fn compile_stmt(&mut self,stmt:&Stmt)->Result<()> { + let id=self.chunk.statements.len();let start=self.here(); + self.chunk.statements.push(crate::bytecode::StatementEmission{ + rule:crate::bytecode::StatementRule::from_source(stmt),start,end:start, + statements:Vec::new(),expressions:Vec::new(),auxiliary:Vec::new(), + }); + if let Some(parent)=self.statement_stack.last(){self.chunk.statements[*parent].statements.push(id);}else{self.chunk.statement_roots.push(id);} + self.statement_stack.push(id);let result=self.compile_stmt_inner(stmt);self.statement_stack.pop(); + self.chunk.statements[id].end=self.here();result } - - fn compile_stmt(&mut self, stmt: &Stmt) -> Result<()> { + fn compile_stmt_inner(&mut self, stmt: &Stmt) -> Result<()> { match stmt { - Stmt::Assign { name, value, span, .. } => { + Stmt::Assign { + name, value, span, .. + } => { self.stmt_span = *span; self.compile_expr(value)?; let slot = self.local_slot(name); + if self.chunk.var_contracts.contains_key(name) { self.emit(Op::CheckBinding(slot)); } self.emit(Op::SetLocal(slot)); self.emit(Op::Pop); } Stmt::Return { value, span } => { self.stmt_span = *span; self.compile_expr(value)?; + if self.chunk.contract.is_some() { self.emit(Op::CheckReturn); } self.emit(Op::Return); } Stmt::Call { call, span } => { self.stmt_span = *span; - self.compile_call(call, true)?; + self.compile_expr(&Expr::Call(call.clone()))?; + self.emit(Op::Pop); } Stmt::Expr { value, span } => { self.stmt_span = *span; @@ -466,10 +569,13 @@ impl<'a> FnCompiler<'a> { span, } => { self.stmt_span = *span; + let loop_id=*self.statement_stack.last().unwrap(); + self.emit(Op::ResetLoop(loop_id)); let loop_start = self.here(); self.compile_expr(condition)?; let exit_j = self.here(); self.emit(Op::JumpIfFalse(0)); + self.emit(Op::CheckLoop(loop_id)); for st in body { self.compile_stmt(st)?; } @@ -487,10 +593,12 @@ impl<'a> FnCompiler<'a> { self.stmt_span = *span; self.compile_expr(&Expr::Var(collection.clone()))?; self.emit(Op::IterItems); - let coll_slot = self.local_slot(&format!("__coll_{collection}")); + let coll_slot = self.fresh_slot("loop.collection"); self.emit(Op::SetLocal(coll_slot)); self.emit(Op::Pop); - let i_slot = self.local_slot(&format!("__i_{collection}")); + let i_slot = self.fresh_slot("loop.index"); + let statement=*self.statement_stack.last().unwrap(); + self.chunk.statements[statement].auxiliary=vec![coll_slot,i_slot]; let zero = self.add_const(Value::Int(0)); self.emit(Op::Constant(zero)); self.emit(Op::SetLocal(i_slot)); @@ -530,377 +638,163 @@ impl<'a> FnCompiler<'a> { } fn compile_call(&mut self, call: &CallExpr, as_stmt: bool) -> Result<()> { + let id=self.chunk.calls.len(); + self.chunk.calls.push(crate::bytecode::CallEmission{ + expression:*self.expression_stack.last().ok_or_else(||self.err("call has no source expression"))?, + source:call.clone(),stages:Vec::new(),dispatch:None,as_statement:as_stmt, + }); + self.call_emission_stack.push(id); + self.call_depth += 1; + let result = self.compile_call_inner(call, as_stmt); + self.call_depth -= 1; + self.call_emission_stack.pop(); + result + } + + fn record_dispatch(&mut self, dispatch:crate::bytecode::CallDispatch){ + let id=*self.call_emission_stack.last().expect("active call emission"); + self.chunk.calls[id].dispatch=Some(dispatch); + } + + fn compile_call_inner(&mut self, call: &CallExpr, as_stmt: bool) -> Result<()> { // Mirror `sema::bind::resolve_for_exec` (G02 BoundCall): // static → Op::Call(fid); dynamic → MethodCall / CallFn / PluginCall. - let mut call = call - .with_modifiers_expanded() - .map_err(|m| self.err(m))?; + let mut call = call.for_evaluation().map_err(|m| self.err(m))?; + let emission=*self.call_emission_stack.last().expect("active call emission"); + self.chunk.calls[emission].source=call.clone(); + // All actuals run once, left to right, including unused extra named + // arguments. Subsequent parameter permutation only reads temporaries. + for (index, arg) in call.args.iter_mut().enumerate() { + let value = match arg { + Arg::Positional(value) => value, + Arg::Named { value, .. } => value + }; + let expression=self.chunk.expressions.len(); + self.compile_expr(value)?; + let key=(self.call_depth,index); + let (name,slot)=if let Some(existing)=self.argument_scratch.get(&key) { existing.clone() } else { + let mut name=format!("\0call.arg.{}.{}", self.call_depth, index); + while self.locals.contains_key(&name) { name.push('\0'); } + let slot = self.locals.len() as usize; + self.locals.insert(name.clone(), slot); + self.argument_scratch.insert(key,(name.clone(),slot)); + (name,slot) + }; + self.emit(Op::SetLocal(slot)); + self.emit(Op::Pop); + self.chunk.calls[emission].stages.push(crate::bytecode::CallStage{expression,slot}); + *value = Expr::Var(name); + } if let Some(recv) = &call.receiver { return self.compile_method_call(recv, &call, as_stmt); } - if let Some(path) = &call.path { - // Local-var method sugar before import path (same as Tree / resolve_for_exec). - if path.len() == 2 - && self.locals.contains_key(&path[0]) - && !self.flat[self.fn_id].path_index.keys().any(|p| p.first() == Some(&path[0])) - { - let recv = path[0].clone(); - return self.compile_method_call(&recv, &call, as_stmt); - } - return self.compile_path_call(path, &call, as_stmt); + use crate::sema::{ExecTarget, DynamicReason}; + let caller=&self.flat[self.fn_id]; + let target=crate::sema::resolve_for_exec(caller.owner,caller.definition,&call); + let user=match &target { + ExecTarget::Local(fun)=>Some((caller.owner,*fun)), + ExecTarget::Import{lib,fun,..}=>Some((*lib,*fun)), + _=>None, + }; + let fid=user.map(|(owner,definition)|self.flat.iter().position(|f|std::ptr::eq(f.owner,owner)&&std::ptr::eq(f.definition,definition)) + .ok_or_else(||self.err("resolved declaration missing from flat program"))).transpose()?; + if let Some(path)=call.path.as_ref().filter(|p|p.len()==2) { + let fallback=if let Some(target)=fid { crate::bytecode::PathFallback::User{target,tag_instance:self.flat[target].parent.is_none()&&self.flat[target].level==1} } + else { crate::bytecode::PathFallback::Error{message:match target { + ExecTarget::Unresolved{message,..}=>message, + _=>format!("unresolved dynamic call `{}`",crate::sema::call_display(&call)), + }} }; + self.record_dispatch(crate::bytecode::CallDispatch::Path{receiver:path[0].clone(),check_receiver:!caller.owner.import_modules.contains_key(&path[0]),fallback}); + let layout=self.argument_layout(&call)?; + self.emit(Op::PathCallArgs(emission,layout)); + if as_stmt{self.emit(Op::Pop);}return Ok(()); } - if let Some(path) = self.flat[self.fn_id].uses.get(&call.callee).cloned() { - return self.compile_path_call(&path, &call, as_stmt); - } - call.callee = - crate::aliases::normalize_call_callee_and_args(&call.callee, &mut call.args); - - match call.callee.as_str() { - "print" => { - let text_expr = call - .args - .iter() - .find_map(|a| match a { - Arg::Named { name, value } if name == "text" => Some(value), - _ => None, - }) - .or_else(|| { - call.args.iter().find_map(|a| match a { - Arg::Positional(e) => Some(e), - _ => None, - }) - }) - .ok_or_else(|| self.err("print requires text (named or positional)"))?; - self.compile_expr(text_expr)?; - self.emit(Op::Print); - if !as_stmt { - self.emit(Op::None_); - } - return Ok(()); - } - "input" => { - let prompt_expr = call - .args - .iter() - .find_map(|a| match a { - Arg::Named { name, value } if name == "prompt" => Some(value), - _ => None, - }) - .or_else(|| { - call.args.iter().find_map(|a| match a { - Arg::Positional(e) => Some(e), - _ => None, - }) - }); - if let Some(e) = prompt_expr { - self.compile_expr(e)?; - } else { - let i = self.add_const(Value::Text(String::new())); - self.emit(Op::Constant(i)); - } - self.emit(Op::Input); - if as_stmt { - self.emit(Op::Pop); - } - return Ok(()); - } - "len" | "str" | "int" | "type" | "trim" => { - let name = call.callee.as_str(); - let value_expr = self - .named_or_first(&call, "value") - .ok_or_else(|| self.err(format!("{name} requires value")))?; - self.compile_expr(value_expr)?; - self.emit(match name { - "len" => Op::Len, - "str" => Op::Str, - "int" => Op::Int, - "type" => Op::TypeOf, - "trim" => Op::Trim, - _ => unreachable!(), - }); - if as_stmt { - self.emit(Op::Pop); - } - return Ok(()); - } - "split" | "join" => { - let name = call.callee.as_str(); - let value_expr = self - .named_or_pos(&call, "value", 0) - .ok_or_else(|| self.err(format!("{name} requires value")))?; - let sep_expr = self - .named_or_pos(&call, "sep", 1) - .ok_or_else(|| self.err(format!("{name} requires sep")))?; - self.compile_expr(value_expr)?; - self.compile_expr(sep_expr)?; - self.emit(match name { - "split" => Op::Split, - "join" => Op::Join, - _ => unreachable!(), - }); - if as_stmt { - self.emit(Op::Pop); - } - return Ok(()); - } - "at" => { - let value_expr = self - .named_or_pos(&call, "value", 0) - .ok_or_else(|| self.err("at requires value"))?; - let index_expr = self - .named_or_pos(&call, "index", 1) - .ok_or_else(|| self.err("at requires index"))?; - self.compile_expr(value_expr)?; - self.compile_expr(index_expr)?; - self.emit(Op::GetIndex); - if as_stmt { - self.emit(Op::Pop); - } - return Ok(()); - } - "call_fn" => { - let name_expr = self - .named_or_first(&call, "name") - .ok_or_else(|| self.err("call_fn requires name"))?; - self.compile_expr(name_expr)?; - self.emit(Op::CallFn); - if as_stmt { - self.emit(Op::Pop); - } - return Ok(()); - } - other if crate::host::HostFn::from_name(other).is_some() => { - let hf = crate::host::HostFn::from_name(other).unwrap(); - let params = hf.all_params(); - let mut named: HashMap = HashMap::new(); - let mut positionals = Vec::new(); - for a in &call.args { - match a { - Arg::Positional(e) => positionals.push(e), - Arg::Named { name, value } => { - named.insert(name.clone(), value); - } - } - } - let mut pos_i = 0usize; - let required_n = hf.required_params().len(); - for (i, p) in params.iter().enumerate() { - if let Some(e) = named.get(*p) { - self.compile_expr(e)?; - } else if pos_i < positionals.len() { - self.compile_expr(positionals[pos_i])?; - pos_i += 1; - } else if i < required_n { - return Err(self.err(format!("{} requires `{p}`", hf.name()))); - } else { - self.emit(Op::None_); - } - } - if pos_i < positionals.len() { - return Err(self.err(format!( - "too many positional arguments ({} extra)", - positionals.len() - pos_i - ))); - } - self.emit(Op::HostCall(hf.as_u16(), params.len() as u8)); - if as_stmt { - self.emit(Op::Pop); - } - return Ok(()); - } - _ => {} - } - - // BoundCall bare name (same-module scope only) or DynamicCall plugin. - match self.resolve_call(&call.callee) { - Ok(fid) => self.compile_user_call(fid, &call, as_stmt), - Err(err) => { - let msg = format!("{err:#}"); - if msg.contains("ambiguous function") { - return Err(err); - } - // DynamicCall(Plugin): unresolved bare names may be runtime plugins. - self.compile_plugin_call(&call, as_stmt) - } + if let Some(fid)=fid{return self.compile_user_call(fid,&call,as_stmt);} + match target { + ExecTarget::Builtin(_) | ExecTarget::Dynamic{reason:DynamicReason::CallFn,..}=>{ + self.record_dispatch(crate::bytecode::CallDispatch::Builtin); + let layout=self.argument_layout(&call)?;let name=self.add_const(Value::Text(call.callee.clone())); + self.emit(Op::BuiltinCallArgs(name,layout));if as_stmt{self.emit(Op::Pop);}return Ok(()); + }, + ExecTarget::Host(hf)=>{ + self.record_dispatch(crate::bytecode::CallDispatch::Host{function:hf.as_u16()}); + let layout=self.argument_layout(&call)?;self.emit(Op::HostCallArgs(hf.as_u16(),layout)); + if as_stmt{self.emit(Op::Pop);}return Ok(()); + }, + ExecTarget::Dynamic{reason:DynamicReason::Plugin,..}=>self.compile_plugin_call(&call,as_stmt), + ExecTarget::Unresolved{message,..}=>{ + self.record_dispatch(crate::bytecode::CallDispatch::ResolutionFault{message:message.clone()}); + let index=self.add_const(Value::Text(message));self.emit(Op::RejectCallResolution(index));Ok(()) + }, + _=>Err(self.err("unsupported resolved call")), } } - fn compile_path_call(&mut self, path: &[String], call: &CallExpr, as_stmt: bool) -> Result<()> { - let display = path.join("."); - if path.len() < 2 { - return Err(self.err(format!( - "library path `{display}`: need at least `lib.member`" - ))); + fn argument_layout(&mut self, call: &CallExpr) -> Result { + let mut layout = Vec::new(); + for arg in &call.args { + let (name, expr) = match arg { + Arg::Positional(expr) => (None, expr), + Arg::Named { name, value } => (Some(name.clone()), value), + }; + self.compile_expr(expr)?; + layout.push(name); } - let fid = *self.flat[self.fn_id] - .path_index - .get(path) - .ok_or_else(|| self.err(format!("unknown `{display}`")))?; - if let Some(pid) = self.flat[fid].parent { - let parent = &self.flat[pid]; - if parent.level == 1 - && parent.parent.is_none() - && self.flat[fid].level != 1 - && self.flat[fid].uses_receiver - { - return Err(self.err(format!( - "`{display}` is an instance method; call it as `var`.{}", - self.flat[fid].name - ))); - } - } - self.compile_user_call(fid, call, as_stmt) + let index = self.chunk.call_layouts.len() as usize; + self.chunk.call_layouts.push(layout); + Ok(index) } fn compile_user_call(&mut self, fid: usize, call: &CallExpr, as_stmt: bool) -> Result<()> { - let params = &self.flat[fid].params; - let mut named: HashMap = HashMap::new(); - let mut positionals = Vec::new(); - for a in &call.args { - match a { - Arg::Positional(e) => positionals.push(e), - Arg::Named { name, value } => { - named.insert(name.clone(), value); - } - } - } - let mut pos_i = 0; - for p in params { - if let Some(e) = named.get(&p.name) { - self.compile_expr(e)?; - } else if pos_i < positionals.len() { - self.compile_expr(positionals[pos_i])?; - pos_i += 1; - } else if let Some(def) = &p.default { - self.compile_expr(def)?; - } else { - return Err(self.err(format!("missing argument for parameter `{}`", p.name))); - } - } - if pos_i < positionals.len() { - return Err(self.err(format!( - "too many positional arguments ({} extra)", - positionals.len() - pos_i - ))); - } - self.emit(Op::Call(fid as u16, params.len() as u8)); + self.record_dispatch(crate::bytecode::CallDispatch::User{target:fid,tag_instance:self.flat[fid].parent.is_none() && self.flat[fid].level==1}); + let layout = self.argument_layout(call)?; + self.emit(Op::CallArgs(fid as usize, layout)); if self.flat[fid].parent.is_none() && self.flat[fid].level == 1 { - let ti = self.add_const(Value::Text(self.flat[fid].name.clone())); - self.emit(Op::TagInstance(ti)); - } - if as_stmt { - self.emit(Op::Pop); + self.emit(Op::TagInstance(fid)); } + if as_stmt { self.emit(Op::Pop); } Ok(()) } /// Unresolved callee → runtime plugin registry (or unknown-fn error). - /// Named args: stack pairs (name, value)×N, argc = 2N, name const prefixed with `@`. - /// Positional: stack values, argc = N, name const as-is. + /// Preserve authored labels until the runtime registry supplies the signature. fn compile_plugin_call(&mut self, call: &CallExpr, as_stmt: bool) -> Result<()> { - let mut named: HashMap = HashMap::new(); - let mut positionals = Vec::new(); - for a in &call.args { - match a { - Arg::Positional(e) => positionals.push(e), - Arg::Named { name, value } => { - named.insert(name.clone(), value); - } - } - } - if !named.is_empty() && !positionals.is_empty() { - return Err(self.err( - "plugin/unknown calls cannot mix positional and named args in bytecode", - )); - } - if !named.is_empty() { - let mut pairs: Vec<_> = named.into_iter().collect(); - pairs.sort_by(|a, b| a.0.cmp(&b.0)); - for (k, e) in &pairs { - let ki = self.add_const(Value::Text(k.clone())); - self.emit(Op::Constant(ki)); - self.compile_expr(e)?; - } - let ni = self.add_const(Value::Text(format!("@{}", call.callee))); - self.emit(Op::PluginCall(ni, (pairs.len() * 2) as u8)); - } else { - for e in &positionals { - self.compile_expr(e)?; - } - let ni = self.add_const(Value::Text(call.callee.clone())); - self.emit(Op::PluginCall(ni, positionals.len() as u8)); - } - if as_stmt { - self.emit(Op::Pop); - } - Ok(()) + self.record_dispatch(crate::bytecode::CallDispatch::Plugin); + let layout=self.argument_layout(call)?; + let name=self.add_const(Value::Text(call.callee.clone())); + self.emit(Op::PluginCallArgs(name,layout)); + if as_stmt{self.emit(Op::Pop);}Ok(()) } fn compile_method_call(&mut self, recv: &str, call: &CallExpr, as_stmt: bool) -> Result<()> { - // Resolve method among all object children by name (unique enough for compile); - // runtime still checks `_type`. - let mut method_fid = None; - for f in self.flat { - if f.parent.is_none() && f.level == 1 { - for &cid in &f.children { - if self.flat[cid].name == call.callee { - method_fid = Some(cid); - break; - } - } - } - if method_fid.is_some() { - break; - } - } - let fid = method_fid.ok_or_else(|| { - self.err(format!("unknown method `{}`", call.callee)) - })?; - let params = self.flat[fid].params.clone(); - let mut named: HashMap = HashMap::new(); - let mut positionals = Vec::new(); - for a in &call.args { - match a { - Arg::Positional(e) => positionals.push(e), - Arg::Named { name, value } => { - named.insert(name.clone(), value); - } - } - } - let mut pos_i = 0; - for p in ¶ms { - if let Some(e) = named.get(&p.name) { - self.compile_expr(e)?; - } else if pos_i < positionals.len() { - self.compile_expr(positionals[pos_i])?; - pos_i += 1; - } else if let Some(def) = &p.default { - self.compile_expr(def)?; - } else { - return Err(self.err(format!("missing argument for parameter `{}`", p.name))); - } - } - if pos_i < positionals.len() { - return Err(self.err(format!( - "too many positional arguments ({} extra)", - positionals.len() - pos_i - ))); - } - let slot = self - .locals - .get(recv) - .copied() + self.record_dispatch(crate::bytecode::CallDispatch::Method{receiver:recv.into()}); + // A method name alone does not determine a signature. Its owner is + // selected from the actual receiver before runtime argument binding. + let layout = self.argument_layout(call)?; + let slot = self.locals.get(recv).copied() .ok_or_else(|| self.err(format!("undefined variable `{recv}`")))?; self.emit(Op::GetLocal(slot)); - let name_i = self.add_const(Value::Text(call.callee.clone())); - self.emit(Op::MethodCall(name_i, params.len() as u8)); - if as_stmt { - self.emit(Op::Pop); - } + let name = self.add_const(Value::Text(call.callee.clone())); + self.emit(Op::MethodCallArgs(name, layout)); + if as_stmt { self.emit(Op::Pop); } Ok(()) } - fn compile_expr(&mut self, expr: &Expr) -> Result<()> { + fn compile_expr(&mut self, expr:&Expr)->Result<()> { + let id=self.chunk.expressions.len(); + let start=self.here(); + self.chunk.expressions.push(crate::bytecode::ExpressionEmission{ + rule:crate::bytecode::ExpressionRule::from_source(expr),start,end:start,children:Vec::new(), + }); + if let Some(parent)=self.expression_stack.last(){self.chunk.expressions[*parent].children.push(id);}else if let Some(stmt)=self.statement_stack.last(){self.chunk.statements[*stmt].expressions.push(id);} + self.expression_stack.push(id); + let result=self.compile_expr_inner(expr); + self.expression_stack.pop(); + self.chunk.expressions[id].end=self.here(); + result + } + + fn compile_expr_inner(&mut self, expr: &Expr) -> Result<()> { match expr { // `` M2: occlusion is transparent for execution — only the // payload runs; the label is diagnosed/rendered, never evaluated. @@ -954,11 +848,10 @@ impl<'a> FnCompiler<'a> { self.emit(Op::GetLocal(slot)); } InterpPart::Index { base, labels } => { - let slot = self - .locals - .get(base) - .copied() - .ok_or_else(|| self.err(format!("undefined variable `{base}`")))?; + let slot = + self.locals.get(base).copied().ok_or_else(|| { + self.err(format!("undefined variable `{base}`")) + })?; self.emit(Op::GetLocal(slot)); for label in labels { let li = self.add_const(Value::Text(label.clone())); @@ -967,6 +860,7 @@ impl<'a> FnCompiler<'a> { } } } + self.emit(Op::Str); if idx > 0 { self.emit(Op::Add); } @@ -1048,12 +942,17 @@ impl<'a> FnCompiler<'a> { BinaryOp::And | BinaryOp::Or => unreachable!(), }); } - Expr::Call(call) => self.compile_call(call, false)?, + Expr::Call(call) => { + if let Err(message)=call.for_evaluation(){ + let index=self.add_const(Value::Text(message)); + self.emit(Op::RejectArgumentExpansion(index)); + }else{self.compile_call(call, false)?;} + }, Expr::List(items) => { for it in items { self.compile_expr(it)?; } - self.emit(Op::BuildList(items.len() as u16)); + self.emit(Op::BuildList(items.len() as usize)); } Expr::Map(pairs) => { for (k, e) in pairs { @@ -1061,7 +960,7 @@ impl<'a> FnCompiler<'a> { self.emit(Op::Constant(ki)); self.compile_expr(e)?; } - self.emit(Op::BuildMap(pairs.len() as u16)); + self.emit(Op::BuildMap(pairs.len() as usize)); } Expr::Index { base, label } => { self.compile_expr(base)?; @@ -1078,7 +977,11 @@ impl<'a> FnCompiler<'a> { self.emit(Op::Str); } } - self.emit(Op::FootnoteGet); + if let Expr::Var(name)=base.as_ref() { + if self.chunk.var_contracts.contains_key(name) { + self.emit(Op::ContractFootnoteGet(self.locals[name])); + } else { self.emit(Op::FootnoteGet); } + } else { self.emit(Op::FootnoteGet); } } Expr::Formula(e) => { let i = self.add_const(Value::Formula(e.clone())); diff --git a/src/bytecode/expression.rs b/src/bytecode/expression.rs new file mode 100644 index 0000000..e9aa5a2 --- /dev/null +++ b/src/bytecode/expression.rs @@ -0,0 +1,439 @@ +//! Source-directed expression emission obligations. Call staging and selection +//! are checked separately in call.rs; dispatch semantics remain a proof boundary. +use super::{FnChunk, Op}; +use crate::{ + ast::{BinaryOp, Expr, IndexKey, InterpPart, Literal, UnaryOp}, + value::Value, +}; + +#[derive(Debug, Clone)] +pub enum ExpressionRule { + Literal(Value), + Read(String), + Unary(UnaryOp), + Binary(BinaryOp), + Interpolation(Vec), + List(usize), + Map(Vec), + Index { + label: IndexKey, + base_variable: Option, + }, + Occluded(bool), + CallBoundary(Vec), + CallFault(String), +} +impl ExpressionRule { + pub fn from_source(expr: &Expr) -> Self { + match expr { + Expr::Literal(value) => Self::Literal(match value { + Literal::None => Value::None, + Literal::Bool(v) => Value::Bool(*v), + Literal::Int(v) => Value::Int(*v), + Literal::Num(v) => Value::Num(*v), + Literal::Text(v) => Value::Text(v.clone()), + }), + Expr::Var(name) => Self::Read(name.clone()), + Expr::Unary { op, .. } => Self::Unary(*op), + Expr::Binary { op, .. } => Self::Binary(*op), + Expr::Interp(parts) => Self::Interpolation(parts.clone()), + Expr::List(items) => Self::List(items.len()), + Expr::Map(items) => Self::Map(items.iter().map(|(k, _)| k.clone()).collect()), + Expr::Index { base, label } => Self::Index { + label: label.clone(), + base_variable: if let Expr::Var(name) = base.as_ref() { + Some(name.clone()) + } else { + None + }, + }, + Expr::Formula(value) => Self::Literal(Value::Formula(value.clone())), + Expr::Code(value) => Self::Literal(Value::Code(value.clone())), + Expr::Occluded { payload, .. } => Self::Occluded(payload.is_some()), + Expr::Call(call) if call.for_evaluation().is_err() => { + Self::CallFault(call.for_evaluation().unwrap_err()) + } + Expr::Call(call) => Self::CallBoundary( + call.receiver + .iter() + .cloned() + .chain(call.path.iter().filter_map(|path| path.first().cloned())) + .collect(), + ), + } + } +} +#[derive(Debug, Clone)] +pub struct ExpressionEmission { + pub rule: ExpressionRule, + pub start: usize, + pub end: usize, + pub children: Vec, +} + +pub(super) fn formula_equal(a: &crate::formula::Expr, b: &crate::formula::Expr) -> bool { + use crate::formula::Expr::*; + match (a, b) { + (Num(x), Num(y)) => x.to_bits() == y.to_bits(), + (Var(x), Var(y)) => x == y, + (UnaryNeg(x), UnaryNeg(y)) => formula_equal(x, y), + ( + Bin { + op: a, + left: x, + right: y, + }, + Bin { + op: b, + left: u, + right: v, + }, + ) => a == b && formula_equal(x, u) && formula_equal(y, v), + (Call { name: a, args: x }, Call { name: b, args: y }) => { + a == b && x.len() == y.len() && x.iter().zip(y).all(|(a, b)| formula_equal(a, b)) + } + (Matrix { source: a, rows: x }, Matrix { source: b, rows: y }) => { + a == b + && x.len() == y.len() + && x.iter().zip(y).all(|(a, b)| { + a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.to_bits() == y.to_bits()) + }) + } + _ => false, + } +} +fn value_equal(a: &Value, b: &Value) -> bool { + match (a, b) { + (Value::Num(x), Value::Num(y)) => x.to_bits() == y.to_bits(), + (Value::Formula(x), Value::Formula(y)) => formula_equal(x, y), + _ => a == b, + } +} +pub(super) struct Cursor<'a> { + pub(super) fun: &'a FnChunk, + pub(super) position: usize, + pub(super) end: usize, +} +impl Cursor<'_> { + pub(super) fn op(&mut self, expected: Op) -> Result<(), String> { + if self.position >= self.end || self.fun.code.get(self.position) != Some(&expected) { + return Err(format!( + "source expression opcode differs at {}: expected {expected:?}", + self.position + )); + } + self.position += 1; + Ok(()) + } + pub(super) fn literal(&mut self, value: &Value) -> Result<(), String> { + match value { + Value::None => self.op(Op::None_), + Value::Bool(true) => self.op(Op::True), + Value::Bool(false) => self.op(Op::False), + _ => { + let Some(Op::Constant(index)) = self.fun.code.get(self.position) else { + return Err("source literal is not a constant instruction".into()); + }; + if !self + .fun + .constants + .get(*index) + .is_some_and(|actual| value_equal(actual, value)) + { + return Err("source literal value differs".into()); + } + self.op(Op::Constant(*index)) + } + } + } + fn read(&mut self, name: &str) -> Result<(), String> { + let slot = self + .fun + .locals + .iter() + .position(|local| local == name) + .ok_or("source variable has no slot")?; + self.op(Op::GetLocal(slot)) + } + pub(super) fn child(&mut self, id: usize) -> Result<(), String> { + let child = &self.fun.expressions[id]; + if child.start != self.position { + return Err("source operand order/range differs".into()); + } + self.position = child.end; + Ok(()) + } +} +pub fn verify(fun: &FnChunk) -> Result { + let mut boundaries = 0; + for (id, node) in fun.expressions.iter().enumerate() { + if node.start >= node.end || node.end > fun.code.len() { + return Err("invalid source expression interval".into()); + } + for child in &node.children { + if *child <= id || *child >= fun.expressions.len() { + return Err("invalid source expression child".into()); + } + let c = &fun.expressions[*child]; + if c.start < node.start || c.end > node.end { + return Err("expression child outside parent".into()); + } + } + let mut c = Cursor { + fun, + position: node.start, + end: node.end, + }; + let expected_children = match &node.rule { + ExpressionRule::Literal(v) => { + c.literal(v)?; + 0 + } + ExpressionRule::Read(name) => { + c.read(name)?; + 0 + } + ExpressionRule::Unary(op) => { + if node.children.len() != 1 { + return Err("unary source arity".into()); + } + c.child(node.children[0])?; + c.op(match op { + UnaryOp::Not => Op::Not, + UnaryOp::Neg => Op::Negate, + })?; + 1 + } + ExpressionRule::Binary(op) => { + if node.children.len() != 2 { + return Err("binary source arity".into()); + } + let left = &fun.expressions[node.children[0]]; + let right = &fun.expressions[node.children[1]]; + c.child(node.children[0])?; + match op { + BinaryOp::And => { + c.op(Op::JumpIfFalse(right.end + 5))?; + c.child(node.children[1])?; + for op in [ + Op::JumpIfFalse(right.end + 3), + Op::True, + Op::Jump(node.end), + Op::False, + Op::Jump(node.end), + Op::False, + ] { + c.op(op)?; + } + } + BinaryOp::Or => { + c.op(Op::JumpIfFalse(right.start))?; + c.op(Op::True)?; + c.op(Op::Jump(node.end))?; + c.child(node.children[1])?; + for op in [ + Op::JumpIfFalse(right.end + 3), + Op::True, + Op::Jump(node.end), + Op::False, + ] { + c.op(op)?; + } + } + _ => { + let _ = left; + c.child(node.children[1])?; + c.op(match op { + BinaryOp::Add => Op::Add, + BinaryOp::Sub => Op::Sub, + BinaryOp::Mul => Op::Mul, + BinaryOp::Div => Op::Div, + BinaryOp::Eq => Op::Equal, + BinaryOp::Ne => Op::NotEqual, + BinaryOp::Lt => Op::Less, + BinaryOp::Le => Op::Lessequal, + BinaryOp::Gt => Op::Greater, + BinaryOp::Ge => Op::GreaterEqual, + _ => unreachable!(), + })?; + } + } + 2 + } + ExpressionRule::List(count) => { + for child in &node.children { + c.child(*child)?; + } + c.op(Op::BuildList(*count))?; + *count + } + ExpressionRule::Map(keys) => { + if node.children.len() != keys.len() { + return Err("map source arity".into()); + } + for (key, child) in keys.iter().zip(&node.children) { + c.literal(&Value::Text(key.clone()))?; + c.child(*child)?; + } + c.op(Op::BuildMap(keys.len()))?; + keys.len() + } + ExpressionRule::Occluded(payload) => { + if *payload { + if node.children.len() != 1 { + return Err("occluded source arity".into()); + } + c.child(node.children[0])?; + 1 + } else { + c.literal(&Value::None)?; + 0 + } + } + ExpressionRule::Index { + label, + base_variable, + } => { + if node.children.len() != 1 { + return Err("index source arity".into()); + } + c.child(node.children[0])?; + match label { + IndexKey::Lit(key) => c.literal(&Value::Text(key.clone()))?, + IndexKey::Var(name) => { + c.read(name)?; + c.op(Op::Str)?; + } + } + if let Some(name) = base_variable + .as_ref() + .filter(|name| fun.var_contracts.contains_key(*name)) + { + let slot = fun + .locals + .iter() + .position(|local| local == name) + .ok_or("index source slot")?; + c.op(Op::ContractFootnoteGet(slot))?; + } else { + c.op(Op::FootnoteGet)?; + } + 1 + } + ExpressionRule::Interpolation(parts) => { + if parts.is_empty() { + c.literal(&Value::Text(String::new()))?; + } + for (i, part) in parts.iter().enumerate() { + match part { + InterpPart::Lit(value) => c.literal(&Value::Text(value.clone()))?, + InterpPart::Var(name) => c.read(name)?, + InterpPart::Index { base, labels } => { + c.read(base)?; + for label in labels { + c.literal(&Value::Text(label.clone()))?; + c.op(Op::FootnoteGet)?; + } + } + } + c.op(Op::Str)?; + if i > 0 { + c.op(Op::Add)?; + } + } + 0 + } + ExpressionRule::CallFault(message) => { + let Some(Op::RejectArgumentExpansion(index)) = fun.code.get(c.position) else { + return Err("missing argument preparation fault".into()); + }; + if fun.constants.get(*index) != Some(&Value::Text(message.clone())) { + return Err("argument preparation message differs".into()); + } + c.op(Op::RejectArgumentExpansion(*index))?; + 0 + } + ExpressionRule::CallBoundary(_) => { + boundaries += 1; + continue; + } + }; + if node.children.len() != expected_children || c.position != node.end { + return Err("source expression coverage differs".into()); + } + } + Ok(boundaries) +} + +/// Local evidence preserves machine values, not the narrower foreign copy domain. +pub(super) fn interpolation_evidence(part: &InterpPart) -> serde_json::Value { + use serde_json::json; + match part { + InterpPart::Lit(value) => json!({"kind":"text","value":value}), + InterpPart::Var(name) => json!({"kind":"read","name":name}), + InterpPart::Index { base, labels } => json!({"kind":"index","base":base,"labels":labels}), + } +} +pub(super) fn index_evidence(key: &IndexKey) -> serde_json::Value { + use serde_json::json; + match key { + IndexKey::Lit(value) => json!({"kind":"literal","value":value}), + IndexKey::Var(name) => json!({"kind":"read","name":name}), + } +} +pub(super) fn formula_evidence(value: &crate::formula::Expr) -> serde_json::Value { + use crate::formula::Expr::*; + use serde_json::json; + match value { + Num(n) => json!({"kind":"number","bits":n.to_bits()}), + Var(name) => json!({"kind":"variable","name":name}), + UnaryNeg(value) => json!({"kind":"negative","value":formula_evidence(value)}), + Bin { op, left, right } => { + json!({"kind":"binary","op":format!("{op:?}"),"left":formula_evidence(left),"right":formula_evidence(right)}) + } + Call { name, args } => { + json!({"kind":"call","name":name,"args":args.iter().map(formula_evidence).collect::>()}) + } + Matrix { source, rows } => { + json!({"kind":"matrix","source":source,"rows":rows.iter().map(|row|row.iter().map(|n|n.to_bits()).collect::>()).collect::>()}) + } + } +} +pub fn value_evidence(value: &Value) -> serde_json::Value { + use serde_json::json; + match value { + Value::None => json!({"tag":"none"}), + Value::Bool(v) => json!({"tag":"bool","value":v}), + Value::Int(v) => json!({"tag":"int","value":v}), + Value::Num(v) => json!({"tag":"num","bits":v.to_bits()}), + Value::Text(v) => json!({"tag":"text","value":v}), + Value::Code(v) => json!({"tag":"code","lang":v.lang,"source":v.source}), + Value::Formula(v) => json!({"tag":"formula","value":formula_evidence(v)}), + _ => json!({"tag":"nonliteral","value":format!("{value:?}")}), + } +} +impl ExpressionRule { + pub fn evidence(&self) -> serde_json::Value { + use serde_json::json; + match self { + Self::Literal(v) => json!({"kind":"literal","value":value_evidence(v)}), + Self::Read(name) => json!({"kind":"read","name":name}), + Self::Unary(op) => json!({"kind":"unary","op":format!("{op:?}")}), + Self::Binary(op) => json!({"kind":"binary","op":format!("{op:?}")}), + Self::CallBoundary(names) => json!({"kind":"call_boundary","names":names}), + Self::CallFault(message) => json!({"kind":"call_expansion_fault","message":message}), + Self::Interpolation(parts) => { + json!({"kind":"interpolation_boundary","parts":parts.iter().map(interpolation_evidence).collect::>()}) + } + Self::List(n) => json!({"kind":"list","arity":n}), + Self::Map(keys) => json!({"kind":"map","keys":keys}), + Self::Index { + label, + base_variable, + } => { + json!({"kind":"index_boundary","label":index_evidence(label),"base_variable":base_variable}) + } + Self::Occluded(payload) => json!({"kind":"occluded","payload":payload}), + } + } +} diff --git a/src/bytecode/mod.rs b/src/bytecode/mod.rs index 1cdd804..2a258ab 100644 --- a/src/bytecode/mod.rs +++ b/src/bytecode/mod.rs @@ -2,6 +2,17 @@ mod compile; mod vm; +mod verify; +mod expression; +mod statement; +mod call; +mod source; +mod names; +pub use source::SourceFunction; +pub use call::{CallEmission, CallStage, CallDispatch, PathFallback}; +pub use statement::{StatementRule,StatementEmission}; +pub use expression::{ExpressionRule,ExpressionEmission,value_evidence as expression_value_evidence}; +pub use verify::StackCertificate; pub use compile::{compile_for_call, compile_module}; pub use vm::Vm; @@ -13,13 +24,13 @@ use crate::value::Value; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Op { - Constant(u16), + Constant(usize), True, False, None_, Pop, - GetLocal(u8), - SetLocal(u8), + GetLocal(usize), + SetLocal(usize), Add, Sub, Mul, @@ -33,14 +44,14 @@ pub enum Op { Less, Lessequal, /// Absolute jump to instruction index. - Jump(u16), - JumpIfFalse(u16), + Jump(usize), + JumpIfFalse(usize), Print, /// Pop prompt text, print it (no newline), read one stdin line → push Text. Input, - BuildList(u16), + BuildList(usize), /// Stack (bottom→top): k0, v0, k1, v1, … (n pairs); push Map. - BuildMap(u16), + BuildMap(usize), /// Pop value → push `len` (text/list). Len, /// Pop value → push display text. @@ -61,25 +72,73 @@ pub enum Op { /// Pop footnote label text, then base → push indexed value (1-based list / map key). FootnoteGet, /// Call function by index; argc values are on the stack (param order). - Call(u16, u8), + Call(usize, usize), + /// Evaluated actuals in authored order; layout preserves optional names. + CallArgs(usize, usize), + /// Runtime binding fault after all explicit actual effects. + RejectDuplicateArgument(usize), + /// Modifier/argument preparation failed before any actual is evaluated. + RejectArgumentExpansion(usize), + /// Skip a callee default when the parameter was explicitly supplied. + JumpIfProvided(usize, usize), + /// Entry/raw-call parameters without a value retain language None after defaults. + FillMissingParameters, + ResetLoop(usize), + CheckLoop(usize), + CheckArguments, + RestoreReceiver, + CheckFrameArguments, + CheckReturn, + /// Check the stack top against the named local's field contract. + CheckBinding(usize), + /// Checked footnote access; operand identifies the contracted base local. + ContractFootnoteGet(usize), /// Pop function name text → call zero-arg top-level or lexically visible function. CallFn, - /// After a constructor Call: tag top-of-stack map with `_type` = constants[idx]. - TagInstance(u16), + /// After a constructor call: stamp name and module of functions[idx]. + TagInstance(usize), /// Method call: stack has argc args (param order) then receiver on top. /// constants[name_idx] is method name text; resolve via receiver `_type`. - MethodCall(u16, u8), + MethodCall(usize, usize), + /// Resolve the receiver first, then bind against that method's signature. + MethodCallArgs(usize, usize), /// Plugin call: stack has argc args in registered param order; name in constants. - PluginCall(u16, u8), + PluginCall(usize, usize), /// Host primitive: pop `argc` args (param order), push result. - HostCall(u16, u8), + HostCall(u16, usize), + /// Native dispatch with authored argument labels; binding happens at runtime. + BuiltinCallArgs(usize, usize), + HostCallArgs(u16, usize), + PluginCallArgs(usize, usize), + /// Two-component paths inspect actual receiver presence before static fallback. + PathCallArgs(usize, usize), + RejectCallResolution(usize), Return, } #[derive(Debug, Clone)] pub struct FnChunk { + /// Captured directly from the loaded declaration before emission. + pub source_ast: SourceFunction, + pub module_scope: u32, + pub module_path: Vec, + pub source: crate::diagnostics::SourceIdentity, + pub declaration_span: Span, pub name: String, pub params: Vec, + pub contract: Option, + pub var_contracts: HashMap, + pub receiver_aliases: bool, + pub defaults: Vec, + pub default_expressions: Vec>, + pub call_layouts: Vec>>, + pub calls: Vec, + /// Source names are reserved before synthetic slots are allocated. + pub source_local_count: usize, + pub expressions: Vec, + pub statements: Vec, + pub statement_roots: Vec, + pub body_start: usize, pub code: Vec, /// Parallel to `code`: source location for diagnostics / debug. pub spans: Vec, @@ -92,6 +151,8 @@ pub struct FnChunk { pub struct Program { pub functions: Vec, pub entry: usize, + /// Applied by run(), not by named or nested calls to the entry function. + pub entry_metadata: Vec<(String, Value)>, /// Top-level object name → function index (level-1). pub objects: Vec<(String, usize)>, /// Parallel to `objects`: base object flat index (`# Child = > Parent`). diff --git a/src/bytecode/names.rs b/src/bytecode/names.rs new file mode 100644 index 0000000..577efc5 --- /dev/null +++ b/src/bytecode/names.rs @@ -0,0 +1,172 @@ +//! Source-grounded declaration identities and the flat view of name resolution. +use super::Program; +use crate::sema::resolution::{NameView, Target}; +use std::collections::{HashMap, HashSet}; + +pub(crate) struct FlatView<'a>(pub &'a Program); +impl NameView for FlatView<'_> { + type Function = usize; + type Module = Vec; + fn roots(&self, m: &Self::Module) -> Vec { + self.0 + .functions + .iter() + .enumerate() + .filter(|(i, f)| &f.module_path == m && self.0.parents[*i].is_none()) + .map(|(i, _)| i) + .collect() + } + fn children(&self, f: usize) -> Vec { + self.0.children[f].clone() + } + fn parent(&self, _: &Self::Module, f: usize) -> Option { + self.0.parents[f] + } + fn same(&self, a: usize, b: usize) -> bool { + a == b + } + fn name(&self, f: usize) -> String { + self.0.functions[f].source_ast.name.clone() + } + fn level(&self, f: usize) -> u8 { + self.0.functions[f].source_ast.level + } + fn is_object(&self, f: usize) -> bool { + self.level(f) == 1 + } + fn uses_receiver(&self, f: usize) -> bool { + self.0.functions[f].source_ast.receiver_aliases + } + fn import(&self, m: &Self::Module, name: &str) -> Option { + let owner = self.0.functions.iter().find(|f| &f.module_path == m)?; + if !owner.source_ast.imports.iter().any(|n| n == name) { + return None; + } + let mut path = m.clone(); + path.push(name.into()); + Some(path) + } + fn used_path(&self, m: &Self::Module, name: &str) -> Option> { + self.0 + .functions + .iter() + .find(|f| &f.module_path == m)? + .source_ast + .uses + .iter() + .find(|(n, _)| n == name) + .map(|(_, p)| p.clone()) + } +} +pub(crate) fn resolve( + program: &Program, + caller: usize, + call: &crate::ast::CallExpr, +) -> Target> { + crate::sema::resolution::resolve( + &FlatView(program), + &program.functions[caller].module_path, + caller, + call, + ) +} +pub(crate) fn lookup(program: &Program, caller: usize, name: &str) -> Option { + crate::sema::resolution::lookup( + &FlatView(program), + &program.functions[caller].module_path, + caller, + name, + ) +} +pub(crate) fn verify(program: &Program) -> Result<(), String> { + let objects = program + .functions + .iter() + .enumerate() + .filter(|(id, f)| program.parents[*id].is_none() && f.source_ast.level == 1) + .map(|(id, f)| (f.source_ast.name.clone(), id)) + .collect::>(); + if program.objects != objects { + return Err("object names differ from source declarations".into()); + } + let methods = objects + .iter() + .flat_map(|(_, owner)| { + program.children[*owner].iter().map(|child| { + ( + *owner, + program.functions[*child].source_ast.name.clone(), + *child, + ) + }) + }) + .collect::>(); + if program.methods != methods { + return Err("method names differ from source declarations".into()); + } + let mut identities = HashSet::new(); + let mut scopes = HashMap::new(); + let mut namespaces = HashMap::new(); + for f in &program.functions { + let source = &f.source_ast; + if source.declaration_path.is_empty() + || !identities.insert((f.module_path.clone(), source.declaration_path.clone())) + { + return Err("duplicate or missing source declaration identity".into()); + } + if scopes + .insert(f.module_scope, f.module_path.clone()) + .is_some_and(|p| p != f.module_path) + || namespaces + .insert(f.module_path.clone(), f.module_scope) + .is_some_and(|s| s != f.module_scope) + || (f.module_scope == 0) != f.module_path.is_empty() + { + return Err("source module scope mapping differs".into()); + } + let owner = program + .functions + .iter() + .find(|g| g.module_path == f.module_path) + .unwrap(); + if source.module_root_count != owner.source_ast.module_root_count + || source.imports != owner.source_ast.imports + || source.uses != owner.source_ast.uses + { + return Err("inconsistent source module header".into()); + } + } + for (id, f) in program.functions.iter().enumerate() { + let source = &f.source_ast; + let parent_path = &source.declaration_path[..source.declaration_path.len() - 1]; + match program.parents[id] { + Some(parent) + if program.functions[parent].source_ast.declaration_path == parent_path => + { + () + } + None if parent_path.is_empty() => (), + _ => return Err("flat parent differs from source declaration".into()), + } + if program.children[id].len() != source.child_count { + return Err("source child inventory differs".into()); + } + for (ordinal, child) in program.children[id].iter().enumerate() { + let mut expected = source.declaration_path.clone(); + expected.push(ordinal); + if program.functions[*child].source_ast.declaration_path != expected { + return Err("source child order differs".into()); + } + } + let roots = FlatView(program).roots(&f.module_path); + if roots.len() != source.module_root_count + || roots + .iter() + .enumerate() + .any(|(i, r)| program.functions[*r].source_ast.declaration_path != [i]) + { + return Err("source root inventory/order differs".into()); + } + } + Ok(()) +} diff --git a/src/bytecode/source.rs b/src/bytecode/source.rs new file mode 100644 index 0000000..716fb8a --- /dev/null +++ b/src/bytecode/source.rs @@ -0,0 +1,366 @@ +//! Structural correspondence from a separately captured loaded AST to emission +//! witnesses. Parsing and capture of the source snapshot remain trusted inputs. +use super::{ExpressionRule, FnChunk}; +use crate::ast::{Arg, CallExpr, Expr, Function, Literal, Param, Stmt}; +use serde_json::{json, Value as Json}; + +#[derive(Debug, Clone)] +pub struct SourceFunction { + pub declaration_path: Vec, + pub level: u8, + pub child_count: usize, + pub module_root_count: usize, + pub imports: Vec, + pub uses: Vec<(String, Vec)>, + pub name: String, + pub line: u32, + pub params: Vec, + pub body: Vec, + pub receiver_aliases: bool, + pub has_contract: bool, + pub binding_contracts: Vec, + pub contract: Option, + pub var_contracts: std::collections::HashMap, +} +impl SourceFunction { + pub fn from_function(fun: &Function) -> Self { + let mut bindings = fun.var_contracts.keys().cloned().collect::>(); + bindings.sort(); + Self { + declaration_path: Vec::new(), + level: fun.level, + child_count: fun.children.len(), + module_root_count: 0, + imports: Vec::new(), + uses: Vec::new(), + name: fun.name.clone(), + line: fun.span.line, + params: fun.params.clone(), + body: fun.body.clone(), + receiver_aliases: crate::ast::function_uses_receiver(fun), + has_contract: fun.contract.is_some(), + binding_contracts: bindings, + contract: fun.contract.clone(), + var_contracts: fun.var_contracts.clone(), + } + } + pub fn from_declaration(module: &crate::ast::Module, fun: &Function) -> Self { + fn locate(nodes: &[Function], target: &Function, path: &mut Vec) -> bool { + for (index, node) in nodes.iter().enumerate() { + path.push(index); + if std::ptr::eq(node, target) || locate(&node.children, target, path) { return true; } + path.pop(); + } + false + } + let mut result = Self::from_function(fun); + assert!(locate(&module.functions, fun, &mut result.declaration_path), "source declaration belongs to its module"); + result.module_root_count = module.functions.len(); + result.imports = module.import_modules.keys().cloned().collect(); + result.imports.sort(); + result.uses = module.uses.iter().map(|u| (u.bind.clone(), u.path.clone())).collect(); + result + } + pub fn evidence(&self) -> Result { + Ok( + json!({"declaration_path":self.declaration_path,"level":self.level,"child_count":self.child_count,"module_root_count":self.module_root_count,"imports":self.imports,"uses":self.uses, + "name":self.name,"line":self.line,"params":self.params.iter().map(|p|Ok(json!({"name":p.name,"default":p.default.as_ref().map(expression_tree).transpose()?}))).collect::,String>>()?, + "body":self.body.iter().map(statement_tree).collect::,_>>()?,"receiver_aliases":self.receiver_aliases,"has_contract":self.has_contract,"binding_contracts":self.binding_contracts,"contracts":Self::contract_metadata(self.contract.as_ref(),&self.var_contracts)}), + ) + } + pub fn contract_metadata( + contract: Option<&crate::contract::Contract>, + bindings: &std::collections::HashMap, + ) -> Json { + fn ty(ty: &crate::contract::Ty) -> Json { + match ty { + crate::contract::Ty::Object(name) => json!({"kind":"object","name":name}), + other => json!({"kind":other.name()}), + } + } + fn encode(c: &crate::contract::Contract) -> Json { + json!({"params":c.params.iter().map(|p|json!({"name":p.name,"type":ty(&p.ty),"description":p.desc})).collect::>(), + "returns":c.returns.as_ref().map(|r|json!({"type":ty(&r.ty),"description":r.desc})), + "fields":c.fields.iter().map(|f|json!({"name":f.name,"type":ty(&f.ty),"nullable":f.nullable,"description":f.desc})).collect::>(), + "start_line":c.start_line,"end_line":c.end_line,"quote":c.quote}) + } + let bindings = bindings + .iter() + .map(|(name, c)| (name, encode(c))) + .collect::>(); + json!({"function":contract.map(encode),"bindings":bindings}) + } +} +fn normalized(call: &CallExpr) -> Result { + call.for_evaluation() +} +fn call_shape(call: &CallExpr) -> Json { + json!({"callee":call.callee,"receiver":call.receiver,"path":call.path,"arguments":call.args.iter().map(|a|match a {Arg::Positional(_)=>None,Arg::Named{name,..}=>Some(name)}).collect::>()}) +} +fn arg_expr(arg: &Arg) -> &Expr { + match arg { + Arg::Positional(e) | Arg::Named { value: e, .. } => e, + } +} + +// Deliberately match the loaded AST directly, rather than invoking the emitter's +// ExpressionRule::from_source or StatementRule::from_source recorder. +fn rule(expr: &Expr) -> Json { + use crate::value::Value; + match expr { + Expr::Literal(lit) => { + let value = match lit { + Literal::None => json!({"tag":"none"}), + Literal::Bool(v) => json!({"tag":"bool","value":v}), + Literal::Int(v) => json!({"tag":"int","value":v}), + Literal::Num(v) => json!({"tag":"num","bits":v.to_bits()}), + Literal::Text(v) => json!({"tag":"text","value":v}), + }; + json!({"kind":"literal","value":value}) + } + Expr::Var(name) => json!({"kind":"read","name":name}), + Expr::Unary { op, .. } => json!({"kind":"unary","op":format!("{op:?}")}), + Expr::Binary { op, .. } => json!({"kind":"binary","op":format!("{op:?}")}), + Expr::Call(call) => { + if let Err(message) = normalized(call) { + return json!({"kind":"call_expansion_fault","message":message}); + } + json!({"kind":"call_boundary","names":call.receiver.iter().cloned().chain(call.path.iter().filter_map(|p|p.first().cloned())).collect::>()}) + } + Expr::List(items) => json!({"kind":"list","arity":items.len()}), + Expr::Map(items) => { + json!({"kind":"map","keys":items.iter().map(|(name,_)|name).collect::>()}) + } + Expr::Interp(parts) => { + json!({"kind":"interpolation_boundary","parts":parts.iter().map(super::expression::interpolation_evidence).collect::>()}) + } + Expr::Index { base, label } => { + json!({"kind":"index_boundary","label":super::expression::index_evidence(label),"base_variable":if let Expr::Var(name)=base.as_ref(){Some(name)}else{None}}) + } + Expr::Occluded { payload, .. } => json!({"kind":"occluded","payload":payload.is_some()}), + Expr::Formula(value) => { + json!({"kind":"literal","value":super::expression::value_evidence(&Value::Formula(value.clone()))}) + } + Expr::Code(value) => { + json!({"kind":"literal","value":{"tag":"code","lang":value.lang,"source":value.source}}) + } + } +} +fn children(expr: &Expr) -> Vec<&Expr> { + match expr { + Expr::Unary { expr, .. } => vec![expr], + Expr::Binary { left, right, .. } => vec![left, right], + Expr::List(items) => items.iter().collect(), + Expr::Map(items) => items.iter().map(|(_, e)| e).collect(), + Expr::Index { base, .. } => vec![base], + Expr::Occluded { payload, .. } => payload.iter().map(|e| e.as_ref()).collect(), + _ => Vec::new(), + } +} +fn expression_tree(expr: &Expr) -> Result { + if matches!(expr,Expr::Call(call) if normalized(call).is_err()) { + return Ok(json!({"rule":rule(expr),"children":[],"call":null})); + } + let (children, call) = if let Expr::Call(call) = expr { + let expanded = normalized(call)?; + ( + expanded + .args + .iter() + .map(|a| expression_tree(arg_expr(a))) + .collect::, _>>()?, + Some(call_shape(&expanded)), + ) + } else { + ( + children(expr) + .iter() + .map(|e| expression_tree(e)) + .collect::, _>>()?, + None, + ) + }; + Ok(json!({"rule":rule(expr),"children":children,"call":call})) +} +fn statement_rule(stmt: &Stmt) -> Json { + match stmt { + Stmt::Assign { name, .. } => json!({"kind":"assign","name":name}), + Stmt::Return { .. } => json!({"kind":"return"}), + Stmt::Call { .. } => json!({"kind":"call"}), + Stmt::Expr { .. } => json!({"kind":"expression"}), + Stmt::Branch { arms, .. } => { + json!({"kind":"branch","arms":arms.iter().map(|a|(a.condition.is_some(),a.body.len())).collect::>()}) + } + Stmt::While { body, .. } => json!({"kind":"while","body":body.len()}), + Stmt::ForEach { + item, + collection, + body, + .. + } => json!({"kind":"foreach","item":item,"collection":collection,"body":body.len()}), + } +} +fn statement_children(stmt: &Stmt) -> Vec<&Stmt> { + match stmt { + Stmt::Branch { arms, .. } => arms.iter().flat_map(|a| a.body.iter()).collect(), + Stmt::While { body, .. } | Stmt::ForEach { body, .. } => body.iter().collect(), + _ => Vec::new(), + } +} +fn statement_expressions(stmt: &Stmt) -> Vec { + match stmt { + Stmt::Assign { value, .. } | Stmt::Return { value, .. } | Stmt::Expr { value, .. } => { + vec![value.clone()] + } + Stmt::Call { call, .. } => vec![Expr::Call(call.clone())], + Stmt::Branch { arms, .. } => arms.iter().filter_map(|a| a.condition.clone()).collect(), + Stmt::While { condition, .. } => vec![condition.clone()], + Stmt::ForEach { collection, .. } => vec![Expr::Var(collection.clone())], + } +} +fn statement_tree(stmt: &Stmt) -> Result { + Ok( + json!({"rule":statement_rule(stmt),"expressions":statement_expressions(stmt).iter().map(expression_tree).collect::,_>>()?,"statements":statement_children(stmt).iter().map(|s|statement_tree(s)).collect::,_>>()?}), + ) +} + +struct Walk<'a> { + fun: &'a FnChunk, + expressions: Vec, + statements: Vec, +} +impl Walk<'_> { + fn expression(&mut self, source: &Expr, id: usize) -> Result<(), String> { + let node = self + .fun + .expressions + .get(id) + .ok_or("AST expression reference")?; + if self.expressions[id] { + return Err("AST expression has multiple owners".into()); + } + self.expressions[id] = true; + if node.rule.evidence() != rule(source) { + return Err("loaded AST expression rule differs from witness".into()); + } + if let Expr::Call(call) = source { + let call = match normalized(call) { + Ok(call) => call, + Err(_) => { + if !node.children.is_empty() + || self.fun.calls.iter().any(|c| c.expression == id) + { + return Err("AST preparation fault evaluated actuals".into()); + } + return Ok(()); + } + }; + let record = self + .fun + .calls + .iter() + .find(|c| c.expression == id) + .ok_or("AST call obligation missing")?; + if call_shape(&call) != call_shape(&record.source) + || node.children.len() < call.args.len() + { + return Err("loaded AST call differs from witness".into()); + } + for (arg, child) in call.args.iter().zip(&node.children) { + self.expression(arg_expr(arg), *child)?; + } + for &generated in &node.children[call.args.len()..] { + let expr = self + .fun + .expressions + .get(generated) + .ok_or("generated reload reference")?; + if self.expressions[generated] + || !expr.children.is_empty() + || !matches!(expr.rule, ExpressionRule::Read(_)) + { + return Err("generated reload is shared or not a read".into()); + } + self.expressions[generated] = true; + } + } else { + let expected = children(source); + if expected.len() != node.children.len() { + return Err("loaded AST expression child inventory differs".into()); + } + for (source, child) in expected.iter().zip(&node.children) { + self.expression(source, *child)?; + } + } + Ok(()) + } + fn statement(&mut self, source: &Stmt, id: usize) -> Result<(), String> { + let node = self + .fun + .statements + .get(id) + .ok_or("AST statement reference")?; + if self.statements[id] { + return Err("AST statement has multiple owners".into()); + } + self.statements[id] = true; + if node.rule.evidence() != statement_rule(source) { + return Err("loaded AST statement rule differs from witness".into()); + } + let exprs = statement_expressions(source); + let stmts = statement_children(source); + if node.expressions.len() != exprs.len() || node.statements.len() != stmts.len() { + return Err("loaded AST statement child inventory differs".into()); + } + for (expr, id) in exprs.iter().zip(&node.expressions) { + self.expression(expr, *id)?; + } + for (stmt, id) in stmts.iter().zip(&node.statements) { + self.statement(stmt, *id)?; + } + Ok(()) + } +} +pub fn verify(fun: &FnChunk) -> Result<(), String> { + let source = &fun.source_ast; + if source.contract != fun.contract || source.var_contracts != fun.var_contracts { + return Err("loaded AST contract metadata differs".into()); + } + if source.name != fun.name + || source.line != fun.declaration_span.line + || source.params.iter().map(|p| &p.name).ne(fun.params.iter()) + || source.receiver_aliases != fun.receiver_aliases + || source.has_contract != fun.contract.is_some() + || source.binding_contracts.len() != fun.var_contracts.len() + || source + .binding_contracts + .iter() + .any(|name| !fun.var_contracts.contains_key(name)) + { + return Err("loaded AST function metadata differs".into()); + } + let mut walk = Walk { + fun, + expressions: vec![false; fun.expressions.len()], + statements: vec![false; fun.statements.len()], + }; + if source.params.len() != fun.default_expressions.len() + || source.body.len() != fun.statement_roots.len() + { + return Err("loaded AST function root inventory differs".into()); + } + for (param, id) in source.params.iter().zip(&fun.default_expressions) { + match (¶m.default, id) { + (Some(expr), Some(id)) => walk.expression(expr, *id)?, + (None, None) => {} + _ => return Err("loaded AST default presence differs".into()), + } + } + for (stmt, id) in source.body.iter().zip(&fun.statement_roots) { + walk.statement(stmt, *id)?; + } + if walk.expressions.iter().any(|seen| !seen) || walk.statements.iter().any(|seen| !seen) { + return Err("emission witness has no loaded AST owner".into()); + } + Ok(()) +} diff --git a/src/bytecode/statement.rs b/src/bytecode/statement.rs new file mode 100644 index 0000000..bceab59 --- /dev/null +++ b/src/bytecode/statement.rs @@ -0,0 +1,365 @@ +//! Source-directed statement and control-flow templates. These checks validate +//! emitted templates; they do not prove the parser or source-witness exporter. +use super::expression::Cursor; +use super::{FnChunk, Op}; +use crate::ast::Stmt; + +#[derive(Debug, Clone)] +pub enum StatementRule { + Assign(String), + Return, + Call, + Expression, + Branch(Vec<(bool, usize)>), + While(usize), + ForEach { + item: String, + collection: String, + body: usize, + }, +} +impl StatementRule { + pub fn from_source(stmt: &Stmt) -> Self { + match stmt { + Stmt::Assign { name, .. } => Self::Assign(name.clone()), + Stmt::Return { .. } => Self::Return, + Stmt::Call { .. } => Self::Call, + Stmt::Expr { .. } => Self::Expression, + Stmt::Branch { arms, .. } => Self::Branch( + arms.iter() + .map(|a| (a.condition.is_some(), a.body.len())) + .collect(), + ), + Stmt::While { body, .. } => Self::While(body.len()), + Stmt::ForEach { + item, + collection, + body, + .. + } => Self::ForEach { + item: item.clone(), + collection: collection.clone(), + body: body.len(), + }, + } + } +} +#[derive(Debug, Clone)] +pub struct StatementEmission { + pub rule: StatementRule, + pub start: usize, + pub end: usize, + pub statements: Vec, + pub expressions: Vec, + pub auxiliary: Vec, +} +fn statement(c: &mut Cursor<'_>, id: usize) -> Result<(), String> { + let child = c.fun.statements.get(id).ok_or("missing statement child")?; + if child.start != c.position { + return Err("source statement order/range differs".into()); + } + c.position = child.end; + Ok(()) +} +pub fn verify(fun: &FnChunk) -> Result<(), String> { + if fun.defaults.len() != fun.params.len() || fun.default_expressions.len() != fun.params.len() { + return Err("default expression inventory".into()); + } + let mut prefix = 0; + for (parameter, default) in fun.default_expressions.iter().enumerate() { + if fun.defaults[parameter] != default.is_some() { + return Err("default source presence differs".into()); + } + if let Some(id) = default { + let expr = fun + .expressions + .get(*id) + .ok_or("default source expression missing")?; + if expr.start != prefix + 1 + || fun.code.get(prefix) != Some(&Op::JumpIfProvided(parameter, expr.end + 2)) + || fun.code.get(expr.end..expr.end + 2) + != Some([Op::SetLocal(parameter), Op::Pop].as_slice()) + { + return Err("source default prologue differs".into()); + } + prefix = expr.end + 2; + } + } + if fun.code.get(prefix) != Some(&Op::FillMissingParameters) { + return Err("missing parameter-fill boundary".into()); + } + prefix += 1; + if fun.contract.is_some() { + if fun.code.get(prefix) != Some(&Op::CheckArguments) { + return Err("missing input contract boundary".into()); + } + prefix += 1; + } + if fun.receiver_aliases { + if fun.code.get(prefix)!=Some(&Op::RestoreReceiver){return Err("missing receiver restoration".into());}prefix+=1; + if fun.contract.is_some(){if fun.code.get(prefix)!=Some(&Op::CheckFrameArguments){return Err("missing frame contract boundary".into());}prefix+=1;} + } + if prefix != fun.body_start { + return Err("function prologue/body boundary differs".into()); + } + let mut protected = fun + .params + .iter() + .cloned() + .collect::>(); + protected.extend(["self".to_string(), "自".to_string()]); + for expression in &fun.expressions { + use super::ExpressionRule; + match &expression.rule { + ExpressionRule::Read(name) => { + protected.insert(name.clone()); + } + ExpressionRule::CallBoundary(names) => protected.extend(names.iter().cloned()), + ExpressionRule::Interpolation(parts) => { + for part in parts { + match part { + crate::ast::InterpPart::Var(name) + | crate::ast::InterpPart::Index { base: name, .. } => { + protected.insert(name.clone()); + } + _ => {} + } + } + } + ExpressionRule::Index { + label, + base_variable, + } => { + if let Some(name) = base_variable { + protected.insert(name.clone()); + } + if let crate::ast::IndexKey::Var(name) = label { + protected.insert(name.clone()); + } + } + _ => {} + } + } + for statement in &fun.statements { + match &statement.rule { + StatementRule::Assign(name) => { + protected.insert(name.clone()); + } + StatementRule::ForEach { + item, collection, .. + } => { + protected.insert(item.clone()); + protected.insert(collection.clone()); + } + _ => {} + } + } + let mut auxiliary_slots = std::collections::HashSet::new(); + let mut parent_counts = vec![0usize; fun.statements.len()]; + for (id, node) in fun.statements.iter().enumerate() { + if node.start > node.end || node.end > fun.code.len() { + return Err("invalid statement interval".into()); + } + for &child in &node.statements { + if child <= id || child >= fun.statements.len() { + return Err("invalid statement child".into()); + } + parent_counts[child] += 1; + let child = &fun.statements[child]; + if child.start < node.start || child.end > node.end { + return Err("statement child outside parent".into()); + } + } + for &expr in &node.expressions { + let expr = fun + .expressions + .get(expr) + .ok_or("invalid statement expression")?; + if expr.start < node.start || expr.end > node.end { + return Err("expression outside statement".into()); + } + } + let mut c = Cursor { + fun, + position: node.start, + end: node.end, + }; + match &node.rule { + StatementRule::Assign(_) + | StatementRule::Return + | StatementRule::Call + | StatementRule::Expression => { + if !node.statements.is_empty() || node.expressions.len() != 1 { + return Err("statement source arity".into()); + } + c.child(node.expressions[0])?; + match &node.rule { + StatementRule::Assign(name) => { + let slot = fun + .locals + .iter() + .position(|n| n == name) + .ok_or("assignment slot")?; + if fun.var_contracts.contains_key(name) { + c.op(Op::CheckBinding(slot))?; + } + c.op(Op::SetLocal(slot))?; + c.op(Op::Pop)?; + } + StatementRule::Return => { + if fun.contract.is_some() { + c.op(Op::CheckReturn)?; + } + c.op(Op::Return)?; + } + StatementRule::Call | StatementRule::Expression => c.op(Op::Pop)?, + _ => unreachable!(), + } + } + StatementRule::While(count) => { + if node.expressions.len() != 1 || node.statements.len() != *count { + return Err("while source arity".into()); + } + c.op(Op::ResetLoop(id))?; + let head = c.position; + c.child(node.expressions[0])?; + c.op(Op::JumpIfFalse(node.end))?; + c.op(Op::CheckLoop(id))?; + for &child in &node.statements { + statement(&mut c, child)?; + } + c.op(Op::Jump(head))?; + } + StatementRule::Branch(arms) => { + if node.expressions.len() != arms.iter().filter(|(condition, _)| *condition).count() + || node.statements.len() != arms.iter().map(|(_, n)| n).sum::() + { + return Err("branch source arity".into()); + } + let (mut expr, mut stmt) = (0, 0); + for (index, (condition, count)) in arms.iter().enumerate() { + let guard = if *condition { + c.child(node.expressions[expr])?; + expr += 1; + let pc = c.position; + c.position += 1; + Some(pc) + } else { + None + }; + for _ in 0..*count { + statement(&mut c, node.statements[stmt])?; + stmt += 1; + } + if index + 1 < arms.len() { + c.op(Op::Jump(node.end))?; + } + if let Some(guard) = guard { + if fun.code.get(guard) != Some(&Op::JumpIfFalse(c.position)) { + return Err("source branch false target differs".into()); + } + } + } + } + StatementRule::ForEach { + item, + collection, + body, + } => { + if node.expressions.len() != 1 + || node.statements.len() != *body + || node.auxiliary.len() != 2 + { + return Err("foreach source arity".into()); + } + let (coll, index) = (node.auxiliary[0], node.auxiliary[1]); + if coll == index || coll >= fun.locals.len() || index >= fun.locals.len() { + return Err("foreach auxiliary slots".into()); + } + for slot in [coll, index] { + if protected.contains(&fun.locals[slot]) || !auxiliary_slots.insert(slot) { + return Err("loop temporary aliases source or another loop".into()); + } + } + // The initial expression must read the authored collection. + if !matches!(&fun.expressions[node.expressions[0]].rule,super::ExpressionRule::Read(name) if name==collection) + { + return Err("foreach source collection differs".into()); + } + c.child(node.expressions[0])?; + c.op(Op::IterItems)?; + c.op(Op::SetLocal(coll))?; + c.op(Op::Pop)?; + c.literal(&crate::value::Value::Int(0))?; + c.op(Op::SetLocal(index))?; + c.op(Op::Pop)?; + let head = c.position; + for op in [ + Op::GetLocal(index), + Op::GetLocal(coll), + Op::Len, + Op::Less, + Op::JumpIfFalse(node.end), + Op::GetLocal(coll), + Op::GetLocal(index), + Op::GetIndex, + ] { + c.op(op)?; + } + let item = fun + .locals + .iter() + .position(|n| n == item) + .ok_or("foreach item slot")?; + c.op(Op::SetLocal(item))?; + c.op(Op::Pop)?; + for &child in &node.statements { + statement(&mut c, child)?; + } + c.op(Op::GetLocal(index))?; + c.literal(&crate::value::Value::Int(1))?; + for op in [Op::Add, Op::SetLocal(index), Op::Pop, Op::Jump(head)] { + c.op(op)?; + } + } + } + if c.position != node.end { + return Err("statement instruction coverage differs".into()); + } + } + let mut cursor = fun.body_start; + for &root in &fun.statement_roots { + let node = fun.statements.get(root).ok_or("statement root missing")?; + if parent_counts[root] != 0 || node.start != cursor { + return Err("source body root order differs".into()); + } + parent_counts[root] += 1; + cursor = node.end; + } + if parent_counts.iter().any(|n| *n != 1) { + return Err("statement forest coverage differs".into()); + } + if fun.code.get(cursor..) != Some([Op::None_, Op::Return].as_slice()) { + return Err("implicit function return differs".into()); + } + Ok(()) +} + +impl StatementRule { + pub fn evidence(&self) -> serde_json::Value { + use serde_json::json; + match self { + Self::Assign(name) => json!({"kind":"assign","name":name}), + Self::Return => json!({"kind":"return"}), + Self::Call => json!({"kind":"call"}), + Self::Expression => json!({"kind":"expression"}), + Self::Branch(arms) => json!({"kind":"branch","arms":arms}), + Self::While(body) => json!({"kind":"while","body":body}), + Self::ForEach { + item, + collection, + body, + } => json!({"kind":"foreach","item":item,"collection":collection,"body":body}), + } + } +} diff --git a/src/bytecode/verify.rs b/src/bytecode/verify.rs new file mode 100644 index 0000000..b6dc8be --- /dev/null +++ b/src/bytecode/verify.rs @@ -0,0 +1,331 @@ +//! Checks every emitted instruction and a control-flow stack-height certificate. +//! This is structural validation, not source/bytecode semantic equivalence. +use super::{FnChunk, Op, Program}; +use crate::value::Value; +use std::collections::{HashSet, VecDeque}; + +#[derive(Debug, Clone)] +pub struct StackCertificate { + pub heights: Vec>, +} + +impl Program { + pub fn verify(&self) -> Result, String> { + let n = self.functions.len(); + if self.entry >= n || self.parents.len() != n || self.children.len() != n { + return Err("invalid program function tables".into()); + } + for (i, parent) in self.parents.iter().enumerate() { + if parent.is_some_and(|p| p >= n || p == i) { + return Err("invalid function parent".into()); + } + for &child in &self.children[i] { + if child >= n || self.parents[child] != Some(i) { + return Err("inconsistent function child".into()); + } + } + } + check_forest(&self.parents)?; + for (child, parent) in self.parents.iter().enumerate() { + if let Some(parent) = parent { + if !self.children[*parent].contains(&child) + || self.functions[*parent].module_path != self.functions[child].module_path + { + return Err("inconsistent parent owner".into()); + } + } + } + super::names::verify(self)?; + if self.objects.len() != self.object_bases.len() { + return Err("invalid object tables".into()); + } + for (i, (_, fid)) in self.objects.iter().enumerate() { + if *fid >= n + || self.object_bases[i] + .is_some_and(|base| base >= n || !self.objects.iter().any(|(_, f)| *f == base)) + { + return Err("invalid object reference".into()); + } + } + if self.methods.iter().any(|(owner, _, fid)| { + *owner >= n || *fid >= n || !self.objects.iter().any(|(_, f)| f == owner) + }) || self.path_index.values().any(|id| *id >= n) + { + return Err("invalid method/export reference".into()); + } + let object_ids = self + .objects + .iter() + .enumerate() + .map(|(i, (_, fid))| (*fid, i)) + .collect::>(); + if object_ids.len() != self.objects.len() { + return Err("duplicate object reference".into()); + } + let bases = self + .object_bases + .iter() + .map(|base| base.map(|fid| object_ids[&fid])) + .collect::>(); + check_forest(&bases)?; + if self + .methods + .iter() + .any(|(owner, _, fid)| self.parents[*fid] != Some(*owner)) + { + return Err("inconsistent method owner".into()); + } + self.functions + .iter() + .enumerate() + .map(|(i, f)| { + verify_chunk(self, i, f) + .map_err(|e| format!("bytecode verification in {}: {e}", f.name)) + }) + .collect() + } +} + +fn verify_chunk(program: &Program, _fid: usize, fun: &FnChunk) -> Result { + super::expression::verify(fun)?; + super::statement::verify(fun)?; + super::call::verify(program,fun)?; + super::source::verify(fun)?; + let size = fun.code.len(); + if size == 0 || fun.spans.len() != size { + return Err("invalid code/span table".into()); + } + if fun.defaults.len() != fun.params.len() + || fun.locals.get(..fun.params.len()) != Some(fun.params.as_slice()) + { + return Err("parameters must be a local-slot prefix".into()); + } + if fun.locals.iter().collect::>().len() != fun.locals.len() { + return Err("duplicate local slot name".into()); + } + let local = |i: usize| { + if i < fun.locals.len() { + Ok(()) + } else { + Err("invalid local reference".to_string()) + } + }; + let target = |i: usize| { + if i < size { + Ok(()) + } else { + Err("invalid jump target".to_string()) + } + }; + let function = |i: usize| { + if i < program.functions.len() { + Ok(()) + } else { + Err("invalid call target".to_string()) + } + }; + let text = |i: usize| { + if matches!(fun.constants.get(i), Some(Value::Text(_))) { + Ok(()) + } else { + Err("invalid text constant reference".to_string()) + } + }; + let mut shapes = Vec::with_capacity(size); + for (ip, op) in fun.code.iter().copied().enumerate() { + // pop count, push count, successor PCs. All opcodes are handled. + let next = vec![ip + 1]; + let shape = match op { + Op::Constant(i) => { + if i >= fun.constants.len() { + return Err("invalid constant reference".into()); + } + (0, 1, next) + } + Op::True | Op::False | Op::None_ => (0, 1, next), + Op::GetLocal(i) => { + local(i)?; + (0, 1, next) + } + Op::SetLocal(i) | Op::CheckBinding(i) => { + local(i)?; + (1, 1, next) + } + Op::Pop | Op::Print => (1, 0, next), + Op::Add + | Op::Sub + | Op::Mul + | Op::Div + | Op::Equal + | Op::NotEqual + | Op::Greater + | Op::GreaterEqual + | Op::Less + | Op::Lessequal + | Op::Split + | Op::Join + | Op::GetIndex + | Op::FootnoteGet => (2, 1, next), + Op::ContractFootnoteGet(i) => { + local(i)?; + (2, 1, next) + } + Op::Negate + | Op::Not + | Op::Input + | Op::Len + | Op::Str + | Op::Int + | Op::TypeOf + | Op::Trim + | Op::IterItems + | Op::CallFn => (1, 1, next), + Op::BuildList(n) => (n, 1, next), + Op::BuildMap(n) => (n.checked_mul(2).ok_or("map arity overflow")?, 1, next), + Op::Jump(t) => { + target(t)?; + (0, 0, vec![t]) + } + Op::JumpIfFalse(t) => { + target(t)?; + (1, 0, vec![ip + 1, t]) + } + Op::JumpIfProvided(p, t) => { + target(t)?; + if p >= fun.params.len() { + return Err("invalid presence reference".into()); + } + (0, 0, vec![ip + 1, t]) + } + Op::ResetLoop(id)|Op::CheckLoop(id)=>{ + if !matches!(fun.statements.get(id).map(|s|&s.rule),Some(super::StatementRule::While(_))){return Err("invalid source loop reference".into());} + (0,0,next) + }, + Op::FillMissingParameters | Op::CheckArguments | Op::RestoreReceiver | Op::CheckFrameArguments => (0, 0, next), + Op::CheckReturn => (1, 1, next), + Op::Call(f, n) => { + function(f)?; + (n, 1, next) + } + Op::CallArgs(f, l) => { + function(f)?; + let layout = fun.call_layouts.get(l).ok_or("invalid call layout")?; + (layout.len(), 1, next) + } + Op::BuiltinCallArgs(name,l)|Op::PluginCallArgs(name,l)=>{ + text(name)?; + if matches!(op,Op::BuiltinCallArgs(_,_)) && !matches!(&fun.constants[name],Value::Text(name) if crate::arguments::builtin_signature(name).is_some()){return Err("unknown builtin signature".into());} + let layout=fun.call_layouts.get(l).ok_or("invalid call layout")?;(layout.len(),1,next) + }, + Op::HostCallArgs(id,l)=>{ + if crate::host::HostFn::from_u16(id).is_none(){return Err("unknown host id".into());} + let layout=fun.call_layouts.get(l).ok_or("invalid call layout")?;(layout.len(),1,next) + }, + Op::PathCallArgs(id,l)=>{ + if !matches!(fun.calls.get(id).and_then(|c|c.dispatch.as_ref()),Some(super::CallDispatch::Path{..})){return Err("invalid path call record".into());} + let layout=fun.call_layouts.get(l).ok_or("invalid path call layout")?;(layout.len(),1,next) + }, + Op::RejectCallResolution(index)=>{text(index)?;(0,0,vec![])}, + Op::MethodCall(name, n) => { + text(name)?; + (n.checked_add(1).ok_or("method arity overflow")?, 1, next) + } + Op::MethodCallArgs(name, l) => { + text(name)?; + let layout = fun.call_layouts.get(l).ok_or("invalid method layout")?; + ( + layout.len().checked_add(1).ok_or("method arity overflow")?, + 1, + next, + ) + } + Op::PluginCall(name, n) => { + text(name)?; + if let Value::Text(s) = &fun.constants[name] { + if s.starts_with('@') && n % 2 != 0 { + return Err("odd named plugin arity".into()); + } + } + (n, 1, next) + } + Op::HostCall(id, n) => { + if crate::host::HostFn::from_u16(id).is_none() { + return Err("invalid host reference".into()); + } + (n, 1, next) + } + Op::TagInstance(f) => { + function(f)?; + if !program.objects.iter().any(|(_, id)| *id == f) { + return Err("invalid constructor reference".into()); + } + (1, 1, next) + } + Op::RejectDuplicateArgument(name) | Op::RejectArgumentExpansion(name) => { + text(name)?; + (0, 0, vec![]) + } + Op::Return => (1, 0, vec![]), + }; + shapes.push(shape); + } + let mut heights = vec![None; size]; + heights[0] = Some(0); + let mut queue = VecDeque::from([0]); + while let Some(ip) = queue.pop_front() { + let height = heights[ip].unwrap(); + let (pop, push, edges) = &shapes[ip]; + if height < *pop { + return Err(format!("stack underflow at {ip}")); + } + if matches!(fun.code[ip], Op::Return) && height != 1 { + return Err(format!("return leaks stack values at {ip}")); + } + let after = (height - pop) + .checked_add(*push) + .ok_or("stack height overflow")?; + for &to in edges { + target(to)?; + match heights[to] { + Some(known) if known != after => { + return Err(format!("inconsistent stack join at {to}")) + } + Some(_) => {} + None => { + heights[to] = Some(after); + queue.push_back(to); + } + } + } + } + Ok(StackCertificate { heights }) +} + +fn check_forest(parents: &[Option]) -> Result<(), String> { + let mut colors = vec![0u8; parents.len()]; + for start in 0..parents.len() { + if colors[start] != 0 { + continue; + } + let mut cursor = Some(start); + let mut path = Vec::new(); + while let Some(node) = cursor { + if node >= parents.len() { + return Err("invalid forest reference".into()); + } + if colors[node] == 1 { + return Err("cyclic bytecode declaration graph".into()); + } + if colors[node] == 2 { + break; + } + colors[node] = 1; + path.push(node); + cursor = parents[node]; + } + for node in path { + colors[node] = 2; + } + } + Ok(()) +} diff --git a/src/bytecode/vm.rs b/src/bytecode/vm.rs index 0b02ad5..3c26bda 100644 --- a/src/bytecode/vm.rs +++ b/src/bytecode/vm.rs @@ -5,14 +5,13 @@ use std::io::Write; use std::path::{Path, PathBuf}; use crate::builtin::{ - builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join, - builtin_len, builtin_split, builtin_str, builtin_trim, - builtin_type, + builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join, builtin_len, + builtin_split, builtin_str, builtin_trim, builtin_type, }; -use crate::host::{call_host, HostContext, HostFn}; -use crate::bytecode::{Op, Program}; +use crate::bytecode::{FnChunk, Op, Program}; use crate::debug::emit_trace; -use crate::diagnostics::{bail_at, Span}; +use crate::diagnostics::{Diagnostic, DiagnosticFrame, SourceIdentity, Span}; +use crate::host::{call_host, HostContext, HostFn}; use crate::input_feed::InputFeed; use crate::value::Value; use std::collections::HashMap; @@ -24,14 +23,32 @@ pub struct Vm { input: InputFeed, trace: bool, host: HostContext, + current_source: SourceIdentity, + current_scope: Option, + diagnostic_frames: Vec, } struct Frame { fn_idx: usize, ip: usize, - slots: Vec, + slots: Vec>, + provided: Vec, + loop_counts: HashMap, + receiver: Option, + defaults_active: bool, /// When set, retag matrix-like return to this `_type` (method chaining). - retag_matrix: Option, + retag_matrix: Option<(String, Option>)>, + has_call_site: bool, + retag_instance: Option, +} + +impl Frame { + fn read(&self, fun: &FnChunk, index: usize) -> Option { + let name=&fun.locals[index]; + let overlay=self.defaults_active && self.receiver.is_some() && (name=="self" || name=="自") && + (index>=fun.params.len() || self.provided.get(index).copied().unwrap_or(false) || self.slots[index].is_none()); + if overlay {self.receiver.clone()} else {self.slots[index].clone()} + } } impl Vm { @@ -43,6 +60,12 @@ impl Vm { input: InputFeed::new(false, Vec::new()), trace: false, host: HostContext::for_run(path, Default::default(), Vec::new()), + current_source: SourceIdentity { + path: path.map(Path::to_path_buf), + sha256: None, + }, + current_scope: None, + diagnostic_frames: Vec::new(), } } @@ -54,6 +77,12 @@ impl Vm { input: InputFeed::new(true, Vec::new()), trace: false, host: HostContext::for_capture(path, Default::default()), + current_source: SourceIdentity { + path: path.map(Path::to_path_buf), + sha256: None, + }, + current_scope: None, + diagnostic_frames: Vec::new(), } } @@ -76,6 +105,50 @@ impl Vm { std::mem::take(&mut self.host.plots) } + #[cfg(all(test, feature = "exec-host"))] + pub(crate) fn foreign_source_evidence( + &mut self, + ) -> ( + Vec, + Vec, + Vec, + ) { + crate::source_refinement_tests::boundary_evidence(&mut self.host) + } + + #[cfg(test)] + pub(crate) fn diagnostic_scope_state( + &self, + ) -> (SourceIdentity, usize, Vec, Vec, Option) { + ( + self.current_source.clone(), + self.diagnostic_frames.len(), + self.host.call_stack.clone(), + self.host.call_site_lines.clone(), + self.current_scope, + ) + } + + #[cfg(all(test, feature = "exec-host"))] + pub(crate) fn causal_replay_evidence(&mut self) -> serde_json::Value { + crate::causal_replay_tests::evidence(&mut self.host) + } + + #[cfg(all(test, feature = "exec-host"))] + pub(crate) fn application_evidence( + &self, + ) -> ( + Vec, + Vec, + usize, + ) { + ( + self.host.oracle_observations.clone(), + self.host.foreign_protocol_steps.clone(), + self.host.foreign_sessions.len(), + ) + } + fn emit_line(&mut self, text: &str) { if self.capture { self.captured_stdout.push_str(text); @@ -98,21 +171,34 @@ impl Vm { } fn err_at(&self, span: Span, message: impl Into) -> anyhow::Error { - bail_at(self.path.as_deref(), span, message) + Diagnostic::new(self.current_source.path.as_deref(), span, message) + .with_source(&self.current_source) + .with_frames(&self.diagnostic_frames) + .into() + } + + fn push_source_frame(&mut self, callee: &FnChunk, call_span: Option) { + let mut source = callee.source.clone(); + if source.path.is_none() { + if self.diagnostic_frames.is_empty() { + source.path = self.path.clone(); + } else if self.current_scope == Some(callee.module_scope) { + source.path = self.current_source.path.clone(); + } + } + self.diagnostic_frames.push(DiagnosticFrame { + function: callee.name.clone(), + declaration: source.at(callee.declaration_span), + call_site: call_span.map(|span| self.current_source.at(span)), + }); } pub fn run(&mut self, program: &Program) -> Result { - let entry = program.entry; - let arity = program.functions[entry].params.len(); - if arity != 0 { - let span = program.functions[entry] - .spans - .first() - .copied() - .unwrap_or(Span::new(1, 1)); - return Err(self.err_at(span, "bytecode: entry function needs arguments")); - } - self.execute(program, entry, &[]) + // Entry execution, like Interpreter::run_function, seeds unbound locals + // with None. Inferred prose parameters do not require a program caller. + let mut seen=std::collections::HashSet::new(); + let metadata=program.entry_metadata.iter().filter(|(name,_)|seen.insert(name.clone())).cloned().collect::>(); + self.execute(program, program.entry, &metadata) } /// Run `fn_idx` with named arguments already bound to local slots. @@ -137,9 +223,35 @@ impl Vm { program: &Program, fn_idx: usize, args: &[(String, Value)], + ) -> Result { + program.verify().map_err(|error|self.err_at(Span::new(1,1),error))?; + let previous_source = self.current_source.clone(); + let previous_scope = self.current_scope; + let previous_depth = self.diagnostic_frames.len(); + let previous_calls = self.host.call_stack.clone(); + let previous_sites = self.host.call_site_lines.clone(); + let previous_line = self.host.current_line; + // Scope numbers belong to one compiled program; never carry them into + // a new external invocation, even when it preserves an outer prefix. + self.current_scope = None; + let result = self.execute_inner(program, fn_idx, args); + self.diagnostic_frames.truncate(previous_depth); + self.current_source = previous_source; + self.current_scope = previous_scope; + self.host.call_stack = previous_calls; + self.host.call_site_lines = previous_sites; + self.host.current_line = previous_line; + result + } + + fn execute_inner( + &mut self, + program: &Program, + fn_idx: usize, + args: &[(String, Value)], ) -> Result { let mut stack: Vec = Vec::new(); - let mut slots = vec![Value::None; program.functions[fn_idx].locals.len().max(1)]; + let mut slots = vec![None; program.functions[fn_idx].locals.len().max(1)]; for (name, value) in args { if let Some(i) = program.functions[fn_idx] .locals @@ -147,45 +259,89 @@ impl Vm { .position(|local| local == name) { if i < slots.len() { - slots[i] = value.clone(); + slots[i] = Some(value.clone()); } } } let mut frames = vec![Frame { fn_idx, + loop_counts: HashMap::new(), + receiver: None, defaults_active: true, ip: 0, + provided: initial_presence(&slots,program.functions[fn_idx].params.len()), slots, retag_matrix: None, + has_call_site: false, + retag_instance: None, }]; + self.push_source_frame(&program.functions[fn_idx], None); self.host .push_call_frame(&program.functions[fn_idx].name.clone()); + let mut source_owner = None; loop { let frame = frames.last_mut().unwrap(); let fun = &program.functions[frame.fn_idx]; + if source_owner != Some(frame.fn_idx) { + self.current_source = self + .diagnostic_frames + .last() + .expect("active source frame") + .declaration + .source + .clone(); + self.current_scope = Some(fun.module_scope); + source_owner = Some(frame.fn_idx); + } if frame.ip >= fun.code.len() { let span = fun.spans.last().copied().unwrap_or(Span::new(1, 1)); return Err(self.err_at(span, format!("bytecode: ip out of range in {}", fun.name))); } let ip = frame.ip; - let op = fun.code[ip]; + let mut op = fun.code[ip]; let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1)); frame.ip += 1; self.host.current_line = span.line; + let mut print_returns_none=false; + let mut method_name_override=None; + let mut constructor_tag=None; + if let Op::PathCallArgs(owner,layout)=op { + let call=&fun.calls[owner]; + let Some(super::CallDispatch::Path{receiver,check_receiver,fallback})=&call.dispatch else{unreachable!("verified path dispatch")}; + let value=if *check_receiver {fun.locals.iter().position(|n|n==receiver).and_then(|slot|frame.read(fun,slot))}else{None}; + if let Some(value)=value { + stack.push(value);method_name_override=Some(call.source.callee.clone());op=Op::MethodCallArgs(0,layout); + } else {match fallback { + super::PathFallback::User{target,tag_instance}=>{op=Op::CallArgs(*target,layout);if *tag_instance{constructor_tag=Some(*target);}}, + super::PathFallback::Error{message}=>return Err(self.err_at(span,message)), + }} + } + if let Op::BuiltinCallArgs(name,layout)=op { + let name=fun.constants[name].as_display();let names=&fun.call_layouts[layout]; + let values=take_actuals(&mut stack,names.len()).map_err(|m|self.err_at(span,m))?; + let (params,allow_missing)=crate::arguments::builtin_signature(&name).ok_or_else(||self.err_at(span,"unknown builtin signature"))?; + let parameters=params.iter().map(|p|p.to_string()).collect::>(); + let bound=crate::arguments::bind(¶meters,names,&values,allow_missing).map_err(|m|self.err_at(span,m))?; + for param in params { + stack.push(bound.get(*param).cloned().unwrap_or_else(||Value::Text(String::new()))); + } + op=match name.as_str(){"print"=>{print_returns_none=true;Op::Print},"input"=>Op::Input,"len"=>Op::Len,"str"=>Op::Str,"int"=>Op::Int,"type"=>Op::TypeOf,"trim"=>Op::Trim,"split"=>Op::Split,"join"=>Op::Join,"at"=>Op::GetIndex,"call_fn"=>Op::CallFn,_=>unreachable!()}; + } + if self.trace { match op { - Op::Print | Op::Input | Op::Call(_, _) | Op::Return => { + Op::Print | Op::Input | Op::Call(_, _) | Op::CallArgs(_, _) | Op::Return => { let kind = match op { Op::Print => "print", Op::Input => "input", - Op::Call(_, _) => "call", + Op::Call(_, _) | Op::CallArgs(_, _) => "call", Op::Return => "return", _ => unreachable!(), }; let ip_s = ip.to_string(); emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(span), "op", &[ @@ -200,6 +356,61 @@ impl Vm { } match op { + Op::CheckArguments => { + if let Some(contract)=&fun.contract { + let bound=fun.locals.iter().zip(&frame.slots).filter_map(|(name,value)| + value.clone().map(|value|(name.clone(),value))).collect(); + // Internal call errors identify the authored call site, like Tree. + let site=self.diagnostic_frames.last().and_then(|f|f.call_site.as_ref()); + let call_site=site.cloned().unwrap_or_else(||self.current_source.at(span)); + if let Some(diag)=crate::contract::call_arg_diag_in(contract,&fun.name,&bound,&call_site,&fun.source) { + let depth=self.diagnostic_frames.len()-usize::from(site.is_some()); + return Err(diag.with_frames(&self.diagnostic_frames[..depth]).into()); + } + } + } + Op::CheckReturn => { + if let Some(contract)=&fun.contract { + let value=stack.last().cloned().unwrap_or(Value::None); + if let Some(diag)=crate::contract::return_diag(contract,&fun.name,&value,self.current_source.path.as_deref(),span) { + return Err(diag.into()); + } + } + } + Op::CheckBinding(slot) => { + let name=&fun.locals[slot]; + if let Some(contract)=fun.var_contracts.get(name) { + let value=stack.last().cloned().unwrap_or(Value::None); + if let Some(diag)=crate::contract::fields_diag(contract,name,&value,self.current_source.path.as_deref(),span) { + return Err(diag.into()); + } + } + } + Op::RestoreReceiver => { + if let Some(receiver)=&frame.receiver { + for (index,name) in fun.locals.iter().enumerate() { + if name=="self" || name=="自" {frame.slots[index]=Some(receiver.clone());} + } + } + frame.defaults_active=false; + } + Op::CheckFrameArguments => { + if let Some(contract)=&fun.contract { + let bound=fun.locals.iter().zip(&frame.slots).filter_map(|(name,value)|value.clone().map(|value|(name.clone(),value))).collect(); + if let Some(diag)=crate::contract::call_arg_diag(contract,&fun.name,&bound,self.current_source.path.as_deref(),fun.declaration_span){return Err(diag.with_source(&self.current_source).with_frames(&self.diagnostic_frames).into());} + } + } + Op::ResetLoop(id) => {frame.loop_counts.insert(id,0);} + Op::CheckLoop(id) => { + let counter=frame.loop_counts.get_mut(&id).ok_or_else(||self.err_at(span,"uninitialized loop counter"))?; + if *counter>=1_000_000 {return Err(self.err_at(span,"while loop exceeded iteration limit"));} + *counter+=1; + } + Op::FillMissingParameters => { + for index in 0..fun.params.len() { + if frame.slots[index].is_none() { frame.slots[index]=Some(Value::None); } + } + } Op::Constant(i) => stack.push(fun.constants[i as usize].clone()), Op::True => stack.push(Value::Bool(true)), Op::False => stack.push(Value::Bool(false)), @@ -208,7 +419,10 @@ impl Vm { stack.pop(); } Op::GetLocal(i) => { - let v = frames.last().unwrap().slots[i as usize].clone(); + let current=frames.last().unwrap(); + let index=i as usize;let name=&fun.locals[index]; + let value=current.read(fun,index); + let v=value.ok_or_else(||self.err_at(span,format!("undefined variable `{name}`")))?; stack.push(v); } Op::SetLocal(i) => { @@ -216,130 +430,32 @@ impl Vm { let slot = i as usize; let fr = frames.last_mut().unwrap(); if fr.slots.len() <= slot { - fr.slots.resize(slot + 1, Value::None); + fr.slots.resize(slot + 1, None); } - fr.slots[slot] = v; + fr.slots[slot] = Some(v); } - Op::Add => { - let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - if let Some(v) = crate::linalg_ops::try_matrix_binary( - &mut self.host, - crate::ast::BinaryOp::Add, - &a, - &b, - ) - .map_err(|m| self.err_at(span, m))? - { - stack.push(v); - } else { - stack.push(add(a, b).map_err(|m| self.err_at(span, m))?); - } + Op::Add | Op::Sub | Op::Mul | Op::Div | Op::Equal | Op::NotEqual | + Op::Greater | Op::GreaterEqual | Op::Less | Op::Lessequal => { + use crate::ast::BinaryOp; + let operation=match op { + Op::Add=>BinaryOp::Add,Op::Sub=>BinaryOp::Sub,Op::Mul=>BinaryOp::Mul,Op::Div=>BinaryOp::Div, + Op::Equal=>BinaryOp::Eq,Op::NotEqual=>BinaryOp::Ne,Op::Greater=>BinaryOp::Gt, + Op::GreaterEqual=>BinaryOp::Ge,Op::Less=>BinaryOp::Lt,Op::Lessequal=>BinaryOp::Le, + _=>unreachable!(), + }; + let right=pop(&mut stack).map_err(|m|self.err_at(span,m))?; + let left=pop(&mut stack).map_err(|m|self.err_at(span,m))?; + let value=if let Some(value)=crate::linalg_ops::try_matrix_binary(&mut self.host,operation,&left,&right) + .map_err(|m|self.err_at(span,m))? {value} else { + crate::builtin::eval_binary(operation,&left,&right).map_err(|m|self.err_at(span,m))? + }; + stack.push(value); } - Op::Sub => { - let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - if let Some(v) = crate::linalg_ops::try_matrix_binary( - &mut self.host, - crate::ast::BinaryOp::Sub, - &a, - &b, - ) - .map_err(|m| self.err_at(span, m))? - { - stack.push(v); - } else { - match (a, b) { - (Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x - y)), - (Value::Num(x), Value::Num(y)) => stack.push(Value::Num(x - y)), - (Value::Int(x), Value::Num(y)) => { - stack.push(Value::Num(x as f64 - y)) - } - (Value::Num(x), Value::Int(y)) => { - stack.push(Value::Num(x - y as f64)) - } - _ => return Err(self.err_at(span, "`-` needs ints or nums")), - } - } + Op::Negate | Op::Not => { + let value=pop(&mut stack).map_err(|m|self.err_at(span,m))?; + let operation=if matches!(op,Op::Negate){crate::ast::UnaryOp::Neg}else{crate::ast::UnaryOp::Not}; + stack.push(crate::builtin::eval_unary(operation,&value).map_err(|m|self.err_at(span,m))?); } - Op::Mul => { - let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - if let Some(v) = crate::linalg_ops::try_matrix_binary( - &mut self.host, - crate::ast::BinaryOp::Mul, - &a, - &b, - ) - .map_err(|m| self.err_at(span, m))? - { - stack.push(v); - } else { - match (a, b) { - (Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x * y)), - (Value::Num(x), Value::Num(y)) => stack.push(Value::Num(x * y)), - (Value::Int(x), Value::Num(y)) => { - stack.push(Value::Num(x as f64 * y)) - } - (Value::Num(x), Value::Int(y)) => { - stack.push(Value::Num(x * y as f64)) - } - _ => return Err(self.err_at(span, "`*` needs ints or nums")), - } - } - } - Op::Div => { - let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - match (a, b) { - (Value::Int(_), Value::Int(0)) => { - return Err(self.err_at(span, "division by zero")); - } - (Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x / y)), - (Value::Num(_), Value::Num(y)) if y == 0.0 => { - return Err(self.err_at(span, "division by zero")); - } - (Value::Int(_), Value::Num(y)) if y == 0.0 => { - return Err(self.err_at(span, "division by zero")); - } - (Value::Num(_), Value::Int(0)) => { - return Err(self.err_at(span, "division by zero")); - } - (Value::Num(x), Value::Num(y)) => stack.push(Value::Num(x / y)), - (Value::Int(x), Value::Num(y)) => { - stack.push(Value::Num(x as f64 / y)) - } - (Value::Num(x), Value::Int(y)) => { - stack.push(Value::Num(x / y as f64)) - } - _ => return Err(self.err_at(span, "`/` needs ints or nums")), - } - } - Op::Negate => match pop(&mut stack).map_err(|m| self.err_at(span, m))? { - Value::Int(n) => stack.push(Value::Int(-n)), - Value::Num(n) => stack.push(Value::Num(-n)), - _ => return Err(self.err_at(span, "unary `-` needs int or num")), - }, - Op::Not => { - let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - stack.push(Value::Bool(!v.truthy())); - } - Op::Equal => { - let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - stack.push(Value::Bool(a == b)); - } - Op::NotEqual => { - let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - stack.push(Value::Bool(a != b)); - } - Op::Greater => cmp(&mut stack, |o| o.is_gt()).map_err(|m| self.err_at(span, m))?, - Op::GreaterEqual => { - cmp(&mut stack, |o| o.is_ge()).map_err(|m| self.err_at(span, m))? - } - Op::Less => cmp(&mut stack, |o| o.is_lt()).map_err(|m| self.err_at(span, m))?, - Op::Lessequal => cmp(&mut stack, |o| o.is_le()).map_err(|m| self.err_at(span, m))?, Op::Jump(t) => { frames.last_mut().unwrap().ip = t as usize; } @@ -444,7 +560,7 @@ impl Vm { let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; stack.push(builtin_iter_items(&v).map_err(|m| self.err_at(span, m))?); } - Op::FootnoteGet => { + Op::FootnoteGet | Op::ContractFootnoteGet(_) => { let label = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let base = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let label = match label { @@ -453,10 +569,27 @@ impl Vm { return Err(self.err_at(span, "footnote label must be text")); } }; - stack.push( - builtin_footnote_get(&base, &label).map_err(|m| self.err_at(span, m))?, - ); + let contract=if let Op::ContractFootnoteGet(slot)=op { + let name=&fun.locals[slot];fun.var_contracts.get(name).map(|c|(name,c)) + } else {None}; + if let Some((name,c))=contract { + if let Some(diag)=crate::contract::index_diag(c,name,&label,&Value::None,self.current_source.path.as_deref(),span) {return Err(diag.into());} + } + let value=builtin_footnote_get(&base,&label).map_err(|m|self.err_at(span,m))?; + if let Some((name,c))=contract { + if let Some(diag)=crate::contract::index_diag(c,name,&label,&value,self.current_source.path.as_deref(),span) {return Err(diag.into());} + } + stack.push(value); } + Op::HostCallArgs(id, layout) => { + let hf=HostFn::from_u16(id).ok_or_else(||self.err_at(span,"unknown host id"))?; + let names=&fun.call_layouts[layout];let values=take_actuals(&mut stack,names.len()).map_err(|m|self.err_at(span,m))?; + let params=hf.all_params().iter().map(|p|p.to_string()).collect::>(); + let bound=crate::arguments::bind(¶ms,names,&values,true).map_err(|m|self.err_at(span,m))?; + for req in hf.required_params(){if !bound.contains_key(*req){return Err(self.err_at(span,format!("{} requires `{req}`",hf.name())));}} + stack.push(call_host(&mut self.host,hf,&bound).map_err(|m|self.err_at(span,m))?); + } + Op::BuiltinCallArgs(_,_)=>unreachable!("builtin dispatch was prepared above"), Op::HostCall(id, argc) => { let hf = HostFn::from_u16(id) .ok_or_else(|| self.err_at(span, format!("unknown host id {id}")))?; @@ -476,71 +609,105 @@ impl Vm { } for req in hf.required_params() { if !bound.contains_key(*req) { - return Err(self.err_at( - span, - format!("{} requires `{req}`", hf.name()), - )); + return Err( + self.err_at(span, format!("{} requires `{req}`", hf.name())) + ); } } let result = call_host(&mut self.host, hf, &bound).map_err(|m| self.err_at(span, m))?; stack.push(result); } - Op::Call(fid, argc) => { + Op::RejectDuplicateArgument(index) => { + let name=fun.constants[index].as_display(); + return Err(self.err_at(span,format!("duplicate named argument `{name}`"))); + } + Op::RejectArgumentExpansion(index) => { + return Err(self.err_at(span,fun.constants[index].as_display())); + } + Op::RejectCallResolution(index)=>return Err(self.err_at(span,fun.constants[index].as_display())), + Op::PathCallArgs(_,_)=>unreachable!("path dispatch was selected above"), + Op::JumpIfProvided(index, target) => { + if frame.provided.get(index as usize).copied().unwrap_or(false) { + frame.ip = target as usize; + } + } + Op::Call(_, _) | Op::CallArgs(_, _) => { + let (fid, argc) = match op { + Op::Call(fid, argc) => (fid, argc as usize), + Op::CallArgs(fid, layout) => (fid, layout), + _ => unreachable!(), + }; let fid = fid as usize; - let argc = argc as usize; let callee = &program.functions[fid]; - if callee.params.len() != argc { - return Err(self.err_at(span, "call argument count mismatch")); - } - let mut slots = vec![Value::None; callee.locals.len().max(argc).max(1)]; - for i in (0..argc).rev() { - slots[i] = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - } + let layout = if matches!(op, Op::CallArgs(_, _)) { + fun.call_layouts[argc as usize].clone() + } else { vec![None; argc as usize] }; + let mut values = Vec::new(); + for _ in &layout { values.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?); } + values.reverse(); + let (slots, provided) = bind_call_values(callee, &layout, values).map_err(|m| self.err_at(span, m))?; let fname = program.functions[fid].name.clone(); frames.push(Frame { fn_idx: fid, + loop_counts: HashMap::new(), + receiver: None, defaults_active: true, ip: 0, + provided, slots, retag_matrix: None, + has_call_site: true, + retag_instance: constructor_tag, }); + self.push_source_frame(callee, Some(span)); self.host.push_call_frame(&fname); self.host.push_call_site_line(span.line); } Op::TagInstance(idx) => { - let type_name = match fun.constants.get(idx as usize) { - Some(Value::Text(s)) => s.clone(), - _ => { - return Err(self.err_at(span, "TagInstance needs type name constant")); - } - }; + let class = program.functions.get(idx).ok_or_else(|| self.err_at(span,"TagInstance needs an object function"))?; let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; - stack.push(tag_instance_value(&type_name, v)); + stack.push(crate::value::tag_instance(&class.name, &class.module_path, v)); } - Op::MethodCall(name_idx, argc) => { - let argc = argc as usize; - let method_name = match fun.constants.get(name_idx as usize) { + Op::MethodCall(_, _) | Op::MethodCallArgs(_, _) => { + let (name_idx, argc) = match op { + Op::MethodCall(name, argc) => (name, argc as usize), + Op::MethodCallArgs(name, layout) => (name, layout), + _ => unreachable!(), + }; + let layout = if matches!(op, Op::MethodCallArgs(_, _)) { + fun.call_layouts[argc as usize].clone() + } else { vec![None; argc as usize] }; + let argc = layout.len(); + let method_name = if let Some(name)=method_name_override {name} else {match fun.constants.get(name_idx as usize) { Some(Value::Text(s)) => s.clone(), _ => { return Err(self.err_at(span, "MethodCall needs method name constant")); } - }; + }}; let recv = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let mut vals = Vec::with_capacity(argc); for _ in 0..argc { vals.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?); } vals.reverse(); - let type_name = instance_type_name_bc(&recv).map_err(|m| self.err_at(span, m))?; - let start_oi = program - .objects - .iter() - .position(|(n, _)| n == &type_name) + let type_name = + instance_type_name_bc(&recv).map_err(|m| self.err_at(span, m))?; + let namespace=crate::value::instance_module(&recv).map_err(|m| self.err_at(span,m))?; + let start_oi = if let Some(namespace)=&namespace { + program.objects.iter().position(|(name,id)| name==&type_name && &program.functions[*id].module_path==namespace) + } else { + let local=program.objects.iter().position(|(name,id)| name==&type_name && program.functions[*id].module_scope==fun.module_scope); + if local.is_some() { local } else { + let candidates=program.objects.iter().enumerate().filter(|(_, (name,_))| name==&type_name).map(|(index,_)|index).collect::>(); + if candidates.len()>1 { return Err(self.err_at(span,format!("ambiguous object type `{type_name}`: a module tag is required"))); } + candidates.first().copied() + } + } .ok_or_else(|| { self.err_at(span, format!("unknown object type `{type_name}`")) })?; - let method_fid = resolve_method_fid(program, start_oi, &method_name).ok_or_else( - || { + let method_fid = resolve_method_fid(program, start_oi, &method_name) + .ok_or_else(|| { let mut chain = Vec::new(); let mut oi = Some(start_oi); let mut seen = std::collections::HashSet::new(); @@ -562,36 +729,37 @@ impl Vm { chain.join(" → ") ), ) - }, - )?; + })?; let callee = &program.functions[method_fid]; - if callee.params.len() != argc { - return Err(self.err_at(span, "method argument count mismatch")); - } - let mut slots = vec![Value::None; callee.locals.len().max(1)]; - for (i, v) in vals.into_iter().enumerate() { - slots[i] = v; - } - for (i, name) in callee.locals.iter().enumerate() { - if name == "自" || name == "self" { - slots[i] = recv.clone(); - } - } + let (slots, provided) = bind_call_values(callee, &layout, vals).map_err(|m| self.err_at(span, m))?; let retag_matrix = if crate::linalg_ops::is_matrix_value(&recv) { - Some(type_name.clone()) + Some((type_name.clone(), namespace)) } else { None }; let fname = callee.name.clone(); frames.push(Frame { fn_idx: method_fid, + loop_counts: HashMap::new(), + receiver: Some(recv.clone()), defaults_active: true, ip: 0, + provided, slots, retag_matrix, + has_call_site: true, + retag_instance: None, }); + self.push_source_frame(callee, Some(span)); self.host.push_call_frame(&fname); self.host.push_call_site_line(span.line); } + Op::PluginCallArgs(name_idx,layout) => { + let name=fun.constants[name_idx].as_display();let names=&fun.call_layouts[layout]; + let values=take_actuals(&mut stack,names.len()).map_err(|m|self.err_at(span,m))?; + let reg=self.host.plugins.get(&name).cloned().ok_or_else(||self.err_at(span,format!("unknown function `{name}`")))?; + let bound=crate::arguments::bind(®.params,names,&values,false).map_err(|m|self.err_at(span,m))?; + stack.push(crate::host::plugin::call_registered(&mut self.host,&name,&bound).map_err(|m|self.err_at(span,m))?); + } Op::PluginCall(name_idx, argc) => { let argc = argc as usize; let raw_name = match fun.constants.get(name_idx as usize) { @@ -628,14 +796,9 @@ impl Vm { vals.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?); } vals.reverse(); - let reg = self - .host - .plugins - .get(&raw_name) - .cloned() - .ok_or_else(|| { - self.err_at(span, format!("unknown function `{raw_name}`")) - })?; + let reg = self.host.plugins.get(&raw_name).cloned().ok_or_else(|| { + self.err_at(span, format!("unknown function `{raw_name}`")) + })?; if reg.params.len() != vals.len() { return Err(self.err_at( span, @@ -666,41 +829,60 @@ impl Vm { .map_err(|m| self.err_at(span, m))?; let callee = &program.functions[fid]; if !callee.params.is_empty() { - return Err(self.err_at( - span, - format!("call_fn: `{name}` must have no parameters"), - )); + return Err( + self.err_at(span, format!("call_fn: `{name}` must have no parameters")) + ); } - let slots = vec![Value::None; callee.locals.len().max(1)]; + let slots = vec![None; callee.locals.len().max(1)]; let fname = callee.name.clone(); frames.push(Frame { fn_idx: fid, + loop_counts: HashMap::new(), + receiver: None, defaults_active: true, ip: 0, + provided: Vec::new(), slots, retag_matrix: None, + has_call_site: true, + retag_instance: None, }); + self.push_source_frame(callee, Some(span)); self.host.push_call_frame(&fname); self.host.push_call_site_line(span.line); } Op::Return => { let ret = pop(&mut stack).unwrap_or(Value::None); let frame = frames.pop(); + let constructor=frame.as_ref().and_then(|f|f.retag_instance); + self.diagnostic_frames.pop(); self.host.pop_call_frame(); - self.host.pop_call_site_line(); + if frame.as_ref().is_some_and(|f| f.has_call_site) { + self.host.pop_call_site_line(); + } let ret = match frame.and_then(|f| f.retag_matrix) { - Some(ty) => crate::linalg_ops::retag_matrix_like(&ty, ret), + Some((ty,namespace)) => { + let result=crate::linalg_ops::retag_matrix_like(&ty,ret); + if let Some(namespace)=namespace { crate::value::tag_instance(&ty,&namespace,result) } else { result } + }, None => ret, }; + let ret=if let Some(id)=constructor {let class=&program.functions[id];crate::value::tag_instance(&class.name,&class.module_path,ret)}else{ret}; if frames.is_empty() { return Ok(ret); } stack.push(ret); } } + if print_returns_none{stack.push(Value::None);} } } } +fn take_actuals(stack:&mut Vec,count:usize)->Result,String>{ + if stack.len() Option { let mut oi = Some(start_oi); let mut seen = std::collections::HashSet::new(); @@ -716,9 +898,12 @@ fn resolve_method_fid(program: &Program, start_oi: usize, method_name: &str) -> { return Some(*fid); } - oi = program.object_bases.get(i).copied().flatten().and_then(|base_flat| { - program.objects.iter().position(|(_, f)| *f == base_flat) - }); + oi = program + .object_bases + .get(i) + .copied() + .flatten() + .and_then(|base_flat| program.objects.iter().position(|(_, f)| *f == base_flat)); } None } @@ -729,44 +914,6 @@ fn pop(stack: &mut Vec) -> Result { .ok_or_else(|| "bytecode: stack underflow".to_string()) } -fn add(a: Value, b: Value) -> Result { - match (a, b) { - (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)), - (Value::Num(x), Value::Num(y)) => Ok(Value::Num(x + y)), - (Value::Int(x), Value::Num(y)) => Ok(Value::Num(x as f64 + y)), - (Value::Num(x), Value::Int(y)) => Ok(Value::Num(x + y as f64)), - (Value::Text(x), Value::Text(y)) => Ok(Value::Text(format!("{x}{y}"))), - (Value::Text(x), Value::Secret(y)) => Ok(Value::Text(format!("{x}{y}"))), - (Value::Secret(x), Value::Text(y)) => Ok(Value::Text(format!("{x}{y}"))), - (Value::Secret(x), Value::Secret(y)) => Ok(Value::Text(format!("{x}{y}"))), - (Value::Text(x), y) => Ok(Value::Text(format!("{x}{}", y.as_display()))), - (x, Value::Text(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))), - (Value::Int(x), y) => Ok(Value::Text(format!("{}{}", x, y.as_display()))), - (x, Value::Int(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))), - _ => Err("`+` needs ints, nums, or text".into()), - } -} - - -fn cmp(stack: &mut Vec, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<(), String> { - let b = pop(stack)?; - let a = pop(stack)?; - let ord = match (&a, &b) { - (Value::Int(x), Value::Int(y)) => x.cmp(y), - (Value::Num(x), Value::Num(y)) => x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal), - (Value::Int(x), Value::Num(y)) => { - (*x as f64).partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal) - } - (Value::Num(x), Value::Int(y)) => { - x.partial_cmp(&(*y as f64)).unwrap_or(std::cmp::Ordering::Equal) - } - (Value::Text(x), Value::Text(y)) => x.cmp(y), - _ => return Err("comparison needs two ints/nums or two texts".into()), - }; - stack.push(Value::Bool(pred(ord))); - Ok(()) -} - fn instance_type_name_bc(v: &Value) -> Result { match v { Value::Map(entries) => entries @@ -781,42 +928,24 @@ fn instance_type_name_bc(v: &Value) -> Result { } } -fn tag_instance_value(type_name: &str, value: Value) -> Value { - match value { - Value::Map(mut entries) => { - // Always stamp the constructed type so an explicit `` `self` = > Parent `` - // inside a child constructor still yields the most specific `_type`. - entries.retain(|(k, _)| k != "_type"); - entries.insert(0, ("_type".into(), Value::Text(type_name.into()))); - Value::Map(entries) - } - Value::None => Value::Map(vec![("_type".into(), Value::Text(type_name.into()))]), - other => Value::Map(vec![ - ("_type".into(), Value::Text(type_name.into())), - ("value".into(), other), - ]), - } +fn resolve_fn_dynamic(program: &Program, current: usize, name: &str) -> Result { + super::names::lookup(program,current,name).ok_or_else(||format!("call_fn: unknown function `{name}`")) } -fn resolve_fn_dynamic(program: &Program, current: usize, name: &str) -> Result { - for &cid in &program.children[current] { - if program.functions[cid].name == name { - return Ok(cid); - } +/// Bind already evaluated actuals; no expression or foreign effect runs here. +fn bind_call_values(callee: &FnChunk, layout: &[Option], values: Vec) + -> std::result::Result<(Vec>, Vec), String> { + if layout.len()!=values.len(){return Err("argument layout/value count mismatch".into());} + let plan=crate::arguments::plan_with_omissions(&callee.params,layout,&callee.defaults)?; + let mut slots = vec![None; callee.locals.len().max(1)]; + for (name,actual) in plan.assignments { + if let Some(index)=callee.locals.iter().position(|local|local==&name){slots[index]=Some(values[actual].clone());} } - let mut parent = program.parents[current]; - while let Some(pid) = parent { - for &cid in &program.children[pid] { - if program.functions[cid].name == name { - return Ok(cid); - } - } - parent = program.parents[pid]; - } - for (i, f) in program.functions.iter().enumerate() { - if program.parents[i].is_none() && f.name == name { - return Ok(i); - } - } - Err(format!("call_fn: unknown function `{name}`")) + let provided=initial_presence(&slots,callee.params.len()); + Ok((slots, provided)) +} + +/// Presence is derived once, before defaults, rather than independently rebound. +fn initial_presence(slots:&[Option],parameters:usize)->Vec { + slots.iter().take(parameters).map(Option::is_some).collect() } diff --git a/src/call.rs b/src/call.rs index bb84637..4eda12e 100644 --- a/src/call.rs +++ b/src/call.rs @@ -100,12 +100,11 @@ pub(crate) fn execute_source_function( } Backend::Bytecode => { let program = compile_for_call(Some(path), &module)?; - let key: Vec = display_name - .split('.') - .filter(|s| !s.is_empty()) - .map(|s| s.to_string()) - .collect(); - let idx = program.path_index.get(&key).copied().ok_or_else(|| { + // Source binding is authoritative even when declaration paths repeat. + let idx = program.functions.iter().position(|chunk| + chunk.module_scope == 0 && chunk.declaration_span == fun.span + && chunk.name == fun.name && chunk.source.sha256 == module.source.sha256 + ).ok_or_else(|| { call_diag( Some(path), line, diff --git a/src/causal_replay_tests.rs b/src/causal_replay_tests.rs new file mode 100644 index 0000000..b02bcf0 --- /dev/null +++ b/src/causal_replay_tests.rs @@ -0,0 +1,235 @@ +use crate::host::{foreign_session as session, json::value_to_json, oracle, HostContext}; +use crate::value::Value; +use serde_json::{json, Value as Json}; +use sha2::{Digest, Sha256}; +use std::path::PathBuf; + +fn root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) +} +fn directory() -> PathBuf { + let p = std::env::var_os("MARQDO_CAUSAL_DIR") + .map(PathBuf::from) + .unwrap_or_else(|| root().join("target/causal-replay")); + std::fs::create_dir_all(&p).unwrap(); + p +} +fn steps(protocol: &[session::ProtocolStep]) -> Vec { + protocol + .iter() + .map(|p| json!({"session":p.session,"request":p.request,"response":p.response})) + .collect() +} +pub(crate) fn evidence(ctx: &mut HostContext) -> Json { + let journal = value_to_json(&Value::List(ctx.oracle_observations.clone())).unwrap(); + let protocol = steps(&ctx.foreign_protocol_steps); + let divergence = ctx.foreign_replay_divergence.clone(); + let live = ctx.foreign_sessions.len(); + // Inspection is explicitly outside replay and outside the admitted journal. + ctx.foreign_replay_expected = None; + ctx.foreign_replay_divergence = None; + let n = if live == 1 { + session::get(ctx, &Value::Handle("s1".into()), &Value::Text("n".into())) + .ok() + .map(|v| value_to_json(&v).unwrap()) + } else { + None + }; + json!({"journal":journal,"protocol":protocol,"divergence":divergence,"live":live,"n":n}) +} + +#[test] +fn observation_journal_erases_writes_but_a_context_distinguishes_them() { + for lang in ["python", "node"] { + let out = directory(); + let path = out.join(format!("journal-only-{lang}.marker")); + let literal = serde_json::to_string(&path.display().to_string()).unwrap(); + let code = if lang == "python" { + format!("from pathlib import Path\nPath({literal}).write_text(str(ticket))\nNone") + } else { + format!("require('fs').writeFileSync({literal},String(ticket)); null;") + }; + let mut journals = Vec::new(); + let mut histories = Vec::new(); + let mut contents = Vec::new(); + let mut reads = Vec::new(); + let mut first_history = Vec::new(); + for ticket in [7, 8] { + let mut ctx = HostContext::default(); + let id = + session::open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + session::set( + &mut ctx, + &id, + &Value::Text("ticket".into()), + &Value::Int(ticket), + ) + .unwrap(); + let reply = session::eval( + &mut ctx, + &id, + None, + Some(&Value::Text(code.clone())), + None, + None, + ) + .unwrap(); + let observed = oracle::eval_observation(&reply).unwrap(); + oracle::append(&mut ctx, "eval", &observed).unwrap(); + journals.push(Value::List(ctx.oracle_observations.clone())); + histories.push(steps(&ctx.foreign_protocol_steps)); + if ticket == 7 { + first_history = ctx.foreign_protocol_steps.clone(); + } + contents.push(std::fs::read_to_string(&path).unwrap()); + reads.push(session::get(&mut ctx, &id, &Value::Text("ticket".into())).unwrap()); + session::close(&mut ctx, &id).unwrap(); + } + let checked = oracle::replay_check(&journals[0], &journals[1]).unwrap(); + assert_eq!(journals[0], journals[1]); + assert_eq!(contents, ["7", "8"]); + assert_ne!(reads[0], reads[1]); + assert_eq!(value_to_json(&checked).unwrap()["ok"], true); + // Control requests use a different wrapper than eval. A mismatching + // setter must not retire the live kernel or overwrite its prior value. + let mut ctx = HostContext::default(); + let id = + session::open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + session::set( + &mut ctx, + &id, + &Value::Text("ticket".into()), + &Value::Int(99), + ) + .unwrap(); + ctx.foreign_protocol_steps.clear(); + ctx.foreign_replay_expected = Some(first_history.into()); + let error = + session::set(&mut ctx, &id, &Value::Text("ticket".into()), &Value::Int(8)).unwrap_err(); + let dispatched = ctx.foreign_protocol_steps.len(); + let live = ctx.foreign_sessions.len(); + ctx.foreign_replay_expected = None; + ctx.foreign_replay_divergence = None; + let retained = session::get(&mut ctx, &id, &Value::Text("ticket".into())).unwrap(); + assert_eq!(dispatched, 0); + assert_eq!(live, 1); + assert_eq!(retained, Value::Int(99)); + session::close(&mut ctx, &id).unwrap(); + std::fs::write(out.join(format!("journal-only-{lang}.json")),serde_json::to_vec_pretty(&json!({"kind":"journal-erasure","language":lang,"journals":journals.iter().map(|j|value_to_json(j).unwrap()).collect::>(),"histories":histories,"contents":contents,"reads":reads.iter().map(|v|value_to_json(v).unwrap()).collect::>(),"journal_check":value_to_json(&checked).unwrap(),"setter_guard":{"error":error,"dispatched":dispatched,"live":live,"retained":value_to_json(&retained).unwrap()}})).unwrap()).unwrap(); + } +} + +#[test] +fn first_protocol_divergence_stops_host_suffix_and_preserves_current_effects() { + let lengths = std::env::var("MARQDO_CAUSAL_PREFIXES") + .unwrap_or("0,4".into()) + .split(',') + .map(|s| s.parse::().unwrap()) + .collect::>(); + let seeds = std::env::var("MARQDO_CAUSAL_SEEDS") + .unwrap_or("3".into()) + .split(',') + .map(|s| s.parse::().unwrap()) + .collect::>(); + let mut count = 0; + for lang in ["python", "node"] { + for &length in &lengths { + for &seed in &seeds { + for backend in ["tree", "bytecode"] { + for mode in ["reply", "request"] { + let out = directory(); + let id = format!("{lang}-{length}-{seed}-{backend}-{mode}"); + let path = out.join(format!("{id}.mq.md")); + let input = out.join(format!("{id}.input")); + let marker = out.join(format!("{id}.suffix")); + let q = |s: &str| serde_json::to_string(s).unwrap(); + let code = if lang == "python" { + format!( + "from pathlib import Path\nn = 99\nint(Path({}).read_text())", + q(&input.display().to_string()) + ) + } else { + format!( + "n = 99; Number(require('fs').readFileSync({},'utf8'));", + q(&input.display().to_string()) + ) + }; + let prefix=(0..length).map(|i|format!("**p{i} = > foreign.session_eval id=`s` source=\"n += 1; n\"**\n")).collect::(); + let source=format!("---\nmarkup: occlusion\nimport:\n foreign: {}\n fs: {}\n---\n# main\n**s = > foreign.session_open lang={}**\n**> foreign.session_set id=`s` name=\"n\" value={seed}**\n{prefix}**r = > foreign.session_eval id=`s` source={}**\n**> fs.write_text path={} text=\"suffix\"**\n*r*\n",root().join("lib/foreign.mq.md").display(),root().join("lib/fs.mq.md").display(),q(lang),q(&code),q(&marker.display().to_string())); + std::fs::write(&path, &source).unwrap(); + std::fs::write(&input, "1").unwrap(); + let module = crate::load::load_module(&path).unwrap(); + let expected = if backend == "tree" { + let mut tree = + crate::interp::Interpreter::with_capture(Some(&path), false); + tree.run_module(&module).unwrap(); + tree.application_evidence().1 + } else { + let program = + crate::bytecode::compile_module(Some(&path), &module).unwrap(); + let mut vm = crate::bytecode::Vm::with_capture(Some(&path)); + vm.run(&program).unwrap(); + vm.application_evidence().1 + }; + assert_eq!(std::fs::read_to_string(&marker).unwrap(), "suffix"); + std::fs::remove_file(&marker).unwrap(); + let actual_source = if mode == "request" { + source.replace(&q(&code), &q(&code.replace("n = 99", "n = 77"))) + } else { + std::fs::write(&input, "2").unwrap(); + source.clone() + }; + std::fs::write(&path, &actual_source).unwrap(); + let actual = crate::load::load_module(&path).unwrap(); + let mut host = HostContext::for_capture(Some(&path), Default::default()); + host.foreign_replay_expected = Some(expected.clone().into()); + let (error, observed) = if backend == "tree" { + let mut tree = + crate::interp::Interpreter::with_capture(Some(&path), false) + .with_host(host); + let error = tree.run_module(&actual).err().map(|e| e.to_string()); + (error, tree.causal_replay_evidence()) + } else { + let program = + crate::bytecode::compile_module(Some(&path), &actual).unwrap(); + let mut vm = + crate::bytecode::Vm::with_capture(Some(&path)).with_host(host); + let error = vm.run(&program).err().map(|e| e.to_string()); + (error, vm.causal_replay_evidence()) + }; + let record = json!({"kind":"causal-stop","id":id,"language":lang,"prefix":length,"seed":seed,"backend":backend,"mode":mode,"expected":steps(&expected),"observed":observed,"error":error,"suffix_exists":marker.exists(),"source":actual_source,"source_sha256":format!("{:x}",Sha256::digest(actual_source.as_bytes()))}); + std::fs::write( + out.join(format!("{id}.json")), + serde_json::to_vec_pretty(&record).unwrap(), + ) + .unwrap(); + assert!( + error.is_some(), + "{id}: replay divergence returned a soft result: {record}" + ); + assert!(!marker.exists(), "{id}: host suffix ran after divergence"); + assert_eq!( + observed["n"], + if mode == "reply" { + json!(99) + } else { + json!(seed + length as i64) + } + ); + assert_eq!(observed["live"], 1); + assert_eq!(observed["journal"].as_array().unwrap().len(), length); + assert_eq!( + observed["protocol"].as_array().unwrap().len(), + length + if mode == "reply" { 2 } else { 1 } + ); + count += 1; + } + } + } + } + } + println!( + "CAUSAL_METRIC:{}", + json!({"cases":count,"document_runs":count*2}) + ); +} diff --git a/src/contract.rs b/src/contract.rs index 0e73071..83a3fca 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -128,20 +128,20 @@ fn instance_type_name(v: &Value) -> Option { }) } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct ParamContract { pub name: String, pub ty: Ty, pub desc: String, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct ReturnContract { pub ty: Ty, pub desc: String, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct FieldContract { pub name: String, pub ty: Ty, @@ -150,7 +150,7 @@ pub struct FieldContract { } /// 一份契约(一个契约表块;同单元多表由 [`merge_contract`] 合并)。 -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Contract { pub params: Vec, pub returns: Option, @@ -505,6 +505,22 @@ pub fn call_arg_diag( None } +/// Argument blame belongs to the caller; contract evidence belongs to the +/// declaration. Import aliases must not relocate a contract into the caller. +pub fn call_arg_diag_in( + c: &Contract, unit: &str, bound: &HashMap, + call_site: &crate::diagnostics::SourceLocation, + declaration: &crate::diagnostics::SourceIdentity, +) -> Option { + let mut diag=call_arg_diag(c,unit,bound,call_site.source.path.as_deref(),call_site.span)? + .with_source(&call_site.source); + let old=c.anchor(call_site.source.path.as_deref()); + let new=c.anchor(declaration.path.as_deref()); + diag.doc_anchor=Some(new.clone()); + diag.suggestion=diag.suggestion.map(|text|text.replace(&old,&new)); + Some(diag) +} + /// 边界 2:返回值 vs 返回契约。 pub fn return_diag( c: &Contract, diff --git a/src/diagnostics.rs b/src/diagnostics.rs index feed559..4af5476 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -23,6 +23,58 @@ impl fmt::Display for Span { } } +/// Identity of the exact text that was parsed, including recorded revisions. +/// A path is a label; the digest is never recomputed from a later disk version. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct SourceIdentity { + pub path: Option, + pub sha256: Option, +} + +impl SourceIdentity { + pub fn from_source(path: Option<&Path>, text: &str) -> Self { + use sha2::{Digest, Sha256}; + Self { + path: path.map(Path::to_path_buf), + sha256: Some(format!("{:x}", Sha256::digest(text.as_bytes()))), + } + } + + pub fn at(&self, span: Span) -> SourceLocation { + SourceLocation { + source: self.clone(), + span, + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SourceLocation { + pub source: SourceIdentity, + pub span: Span, +} + +impl SourceLocation { + pub fn to_json(&self) -> serde_json::Value { + serde_json::json!({"file": self.source.path.as_ref().map(|p| display_path(p)), + "source_sha256": self.source.sha256, "line": self.span.line, "col": self.span.col}) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DiagnosticFrame { + pub function: String, + pub declaration: SourceLocation, + pub call_site: Option, +} + +impl DiagnosticFrame { + pub fn to_json(&self) -> serde_json::Value { + serde_json::json!({"function": self.function, "declaration": self.declaration.to_json(), + "call_site": self.call_site.as_ref().map(SourceLocation::to_json)}) + } +} + /// Format a path for user-facing diagnostics. /// /// Strips the Windows extended-length prefix (`\\?\` / `\\?\UNC\`) that @@ -75,6 +127,9 @@ pub struct Diagnostic { pub doc_anchor: Option, pub doc_quote: Option, pub contract_ref: Option, + pub source_sha256: Option, + /// Ordered outermost to innermost; every site retains its own source. + pub frames: Vec, } impl Diagnostic { @@ -89,6 +144,8 @@ impl Diagnostic { doc_anchor: None, doc_quote: None, contract_ref: None, + source_sha256: None, + frames: Vec::new(), } } @@ -101,6 +158,17 @@ impl Diagnostic { self } + pub fn with_source(mut self, source: &SourceIdentity) -> Self { + self.path = source.path.clone(); + self.source_sha256 = source.sha256.clone(); + self + } + + pub fn with_frames(mut self, frames: &[DiagnosticFrame]) -> Self { + self.frames = frames.to_vec(); + self + } + pub fn with_severity(mut self, severity: Severity) -> Self { self.severity = severity; self @@ -132,10 +200,25 @@ impl Diagnostic { } pub fn format_message(&self) -> String { - match &self.path { + let mut message = match &self.path { Some(p) => format!("{}:{}: {}", display_path(p), self.span, self.message), None => format!("{}: {}", self.span, self.message), + }; + for frame in self.frames.iter().rev() { + if let Some(site) = &frame.call_site { + let file = site + .source + .path + .as_ref() + .map(|p| display_path(p)) + .unwrap_or_else(|| "".into()); + message.push_str(&format!( + "\n called {} from {}:{}", + frame.function, file, site.span + )); + } } + message } /// Machine-readable form(AI/MLSP 面;`run --json` 出口)。 @@ -153,6 +236,8 @@ impl Diagnostic { "line": self.span.line, "col": self.span.col, }, + "source_sha256": self.source_sha256, + "frames": self.frames.iter().map(DiagnosticFrame::to_json).collect::>(), "doc_anchor": self.doc_anchor, "doc_quote": self.doc_quote, "contract_ref": self.contract_ref.as_ref().map(|c| json!({ diff --git a/src/frontend_elaboration_tests.rs b/src/frontend_elaboration_tests.rs new file mode 100644 index 0000000..b6a2330 --- /dev/null +++ b/src/frontend_elaboration_tests.rs @@ -0,0 +1,2117 @@ +//! Source-level obligations for the real elaborator and both execution paths. +use crate::{ + bytecode::{compile_module, Vm}, + interp::Interpreter, + load::load_module, + value::Value, +}; +use std::path::PathBuf; + +// Evidence describes local machine values, not a foreign copied payload. +fn local_json(value: &Value) -> serde_json::Value { + match value { + Value::Int(value) => serde_json::json!(value), + _ => crate::host::json::value_to_json(value).unwrap(), + } +} + +fn document(id: &str, source: &str) -> (PathBuf, crate::ast::Module) { + let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("target/frontend-elaboration") + .join(format!("{id}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("main.mq.md"); + std::fs::write(&path, source).unwrap(); + let module = load_module(&path).unwrap(); + (path, module) +} + +#[test] +fn named_arguments_preserve_authored_effect_order() { + let source = "# main\n**answer = combine(b=mark(2), a=mark(1))**\n*answer*\n\n## combine\n + `a`\n + `b`\n**n = a * 10 + b**\n*n*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + both("argument-order", source, Value::Int(12), "2\n1\n"); +} + +#[test] +fn defaults_see_target_lexical_scope_and_earlier_parameters() { + let source = "# main\n**answer = > choose a=7**\n*answer*\n\n## choose\n + `a`\n + `b`=`a`\n + `c`=> helper\n**n = b * 10 + c**\n\n### helper\n*3*\n\n*n*\n\n## helper\n*9*\n"; + both("default-scope", source, Value::Int(73), ""); + let (_, module) = document("default-scope", source); + let choose = module.functions[0] + .children + .iter() + .find(|f| f.name == "choose") + .unwrap(); + assert!(choose.children.iter().any(|f| f.name == "helper")); + assert_eq!( + module.functions[0] + .children + .iter() + .filter(|f| f.name == "helper") + .count(), + 1 + ); +} + +fn both(id: &str, source: &str, value: Value, stdout: &str) { + let (path, module) = document(id, source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let tree_value = tree.run_module(&module).unwrap(); + assert_eq!(tree_value, value, "{id}: tree"); + assert_eq!(tree.captured_stdout, stdout, "{id}: tree stdout"); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let vm_value = vm.run(&program).unwrap(); + assert_eq!(vm_value, value, "{id}: bytecode"); + assert_eq!(vm.captured_stdout, stdout, "{id}: bytecode stdout"); + #[cfg(feature = "exec-host")] + let boundary = { + let encode = |(journal, protocol, live): ( + Vec, + Vec, + usize, + )| { + serde_json::json!({ + "journal":crate::host::json::value_to_json(&Value::List(journal)).unwrap(), + "protocol":protocol.iter().map(|step|serde_json::json!({"session":step.session,"request":step.request,"response":step.response})).collect::>(),"live":live}) + }; + serde_json::json!({"tree":encode(tree.application_evidence()),"bytecode":encode(vm.application_evidence())}) + }; + #[cfg(not(feature = "exec-host"))] + let boundary = serde_json::Value::Null; + if let Some(out) = std::env::var_os("MARQDO_FRONTEND_DIR") { + use serde_json::json; + fn rows( + module: &crate::ast::Module, + namespace: &[String], + out: &mut Vec, + modules: &mut Vec, + ) { + fn functions( + module: &crate::ast::Module, + namespace: &[String], + lexical: &[String], + parent: Option, + items: &[crate::ast::Function], + out: &mut Vec, + ) { + for fun in items { + let mut path = lexical.to_vec(); + path.push(fun.name.clone()); + out.push(json!({"name":fun.name,"line":fun.span.line,"path":module.source.path,"sha256":module.source.sha256, + "module_path":namespace,"lexical_path":path,"parent_line":parent,"level":fun.level,"base":fun.base, + "params":fun.params.iter().map(|p|json!({"name":p.name,"default":p.default.is_some(),"line":p.span.line})).collect::>(), + "source_ast":crate::bytecode::SourceFunction::from_declaration(module,fun).evidence().unwrap(),"body_count":fun.body.len(),"uses_receiver":crate::ast::function_uses_receiver(fun)})); + functions( + module, + namespace, + &path, + Some(fun.span.line), + &fun.children, + out, + ); + } + } + let text = module + .source + .path + .as_ref() + .and_then(|path| std::fs::read_to_string(path).ok()); + modules.push(json!({"module_path":namespace,"path":module.source.path,"sha256":module.source.sha256,"text":text, + "imports":({let mut names=module.import_modules.keys().cloned().collect::>();names.sort();names}), + "uses":module.uses.iter().map(|binding|json!({"bind":binding.bind,"path":binding.path})).collect::>()})); + functions(module, namespace, &[], None, &module.functions, out); + let mut imports = module.import_modules.iter().collect::>(); + imports.sort_by_key(|(name, _)| *name); + for (bind, imported) in imports { + let mut child = namespace.to_vec(); + child.push(bind.clone()); + rows(imported, &child, out, modules); + } + } + let mut ast = Vec::new(); + let mut modules = Vec::new(); + rows(&module, &[], &mut ast, &mut modules); + let certificates = program.verify().unwrap(); + let evidence = json!({"version":1,"id":id,"source":source,"path":path,"sha256":module.source.sha256, + "expected":{"value":local_json(&value),"stdout":stdout}, + "tree":{"value":local_json(&tree_value),"stdout":tree.captured_stdout}, + "bytecode":{"value":local_json(&vm_value),"stdout":vm.captured_stdout}, + "boundary":boundary,"ast":ast,"modules":modules,"objects":program.objects,"object_bases":program.object_bases,"methods":program.methods,"parents":program.parents,"children":program.children, + "entry":program.entry,"entry_metadata":program.entry_metadata.iter().map(|(name,value)|json!([name,local_json(value)])).collect::>(),"functions":program.functions.iter().enumerate().map(|(fi,f)|{let mut row=json!({ + "stack_heights":certificates[fi].heights, + "body_start":f.body_start,"default_expressions":f.default_expressions,"statement_roots":f.statement_roots, + "statements":f.statements.iter().map(|s|json!({"rule":s.rule.evidence(),"start":s.start,"end":s.end,"statements":s.statements,"expressions":s.expressions,"auxiliary":s.auxiliary})).collect::>(), + "constants":f.constants.iter().map(crate::bytecode::expression_value_evidence).collect::>(), + "expressions":f.expressions.iter().map(|e|json!({"rule":e.rule.evidence(),"start":e.start,"end":e.end,"children":e.children})).collect::>(), + "name":f.name,"line":f.declaration_span.line,"path":f.source.path,"sha256":f.source.sha256, + "module_path":f.module_path,"spans":f.spans.iter().map(|span|span.line).collect::>(), + "params":f.params,"receiver_aliases":f.receiver_aliases,"defaults":f.defaults,"scope":f.module_scope,"locals":f.locals,"call_layouts":f.call_layouts,"constant_count":f.constants.len(), + "code":f.code.iter().map(|op|format!("{op:?}")).collect::>() + });row["source_ast"]=f.source_ast.evidence().unwrap();row["contracts"]=crate::bytecode::SourceFunction::contract_metadata(f.contract.as_ref(),&f.var_contracts);row["has_contract"]=json!(f.contract.is_some());row["binding_contracts"]=json!(f.var_contracts.keys().collect::>());row["source_local_count"]=json!(f.source_local_count);row["calls"]=json!(f.calls.iter().map(|c|{let mut row=c.evidence();row["binding_plan"]=c.binding_evidence(&program);row}).collect::>());row}).collect::>()}); + let out = PathBuf::from(out); + std::fs::create_dir_all(&out).unwrap(); + std::fs::write( + out.join(format!("{id}.json")), + serde_json::to_vec_pretty(&evidence).unwrap(), + ) + .unwrap(); + } +} + +#[test] +fn method_signature_belongs_to_the_actual_receiver() { + both("receiver-signature", "# A\n## m\n + `x`\n*x*\n\n# B\n## m\n + `y`\n + `z`=3\n**r = y * 10 + z**\n*r*\n\n# main\n**b = > B**\n**answer = > `b`.m y=7**\n*answer*\n", Value::Int(73), ""); +} + +#[test] +fn defaults_distinguish_omission_from_explicit_none() { + both( + "explicit-none", + "# main\n**answer = > f value=None**\n*answer*\n\n## f\n + `value`=7\n*value*\n", + Value::None, + "", + ); +} + +#[test] +fn default_effects_execute_once_and_in_parameter_order() { + both("default-order", "# main\n**answer = f(c=mark(9))**\n*answer*\n\n## f\n + `a`=> mark value=1\n + `b`=> mark value=2\n + `c`=0\n**r = a * 100 + b * 10 + c**\n*r*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n", Value::Int(129), "9\n1\n2\n"); +} + +#[test] +fn supplied_later_parameter_is_available_to_a_default() { + both( + "provided-later", + "# main\n**answer = > f b=8**\n*answer*\n\n## f\n + `a`=`b`\n + `b`=7\n*a*\n", + Value::Int(8), + "", + ); +} + +#[test] +fn imported_defaults_keep_the_definition_module() { + let (path, _) = document("import-default", "# main\n*0*\n"); + let library = "## choose\n + `value`=> helper\n*value*\n\n## helper\n*3*\n"; + std::fs::write(path.parent().unwrap().join("lib.mq.md"), library).unwrap(); + both("import-default", "---\nimport:\n lib: lib.mq.md\n---\n# main\n**answer = > lib.choose**\n*answer*\n\n## helper\n*9*\n", Value::Int(3), ""); +} + +#[test] +fn nested_calls_do_not_overwrite_pending_actuals_or_leak_temporary_slots() { + let mut source = String::from("# main\n"); + for _ in 0..400 { + source.push_str("**answer = pair(b=pair(2, 3), a=pair(4, 5))**\n"); + } + source.push_str("*answer*\n\n## pair\n + `a`\n + `b`\n**r = a * 10 + b**\n*r*\n"); + both("temporary-lifetimes", &source, Value::Int(473), ""); +} + +#[test] +fn unused_extra_named_arguments_still_execute_in_authored_order() { + both("extra-effects", "# main\n**answer = f(extra=mark(2), x=mark(1))**\n*answer*\n\n## f\n + `x`\n*x*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n", Value::Int(1), "2\n1\n"); +} + +#[test] +fn builtin_argument_permutation_happens_after_authored_evaluation() { + both("builtin-order", "# main\n**answer = split(sep=mark(\"S\"), value=mark(\"X\"))**\n*answer*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n", Value::List(vec![Value::Text("X".into())]), "S\nX\n"); +} + +#[test] +fn imported_object_identity_survives_cross_module_calls() { + let (path, _) = document("qualified-objects", "# main\n*0*\n"); + std::fs::write( + path.parent().unwrap().join("a.mq.md"), + "# Counter\n## read\n*3*\n", + ) + .unwrap(); + std::fs::write( + path.parent().unwrap().join("b.mq.md"), + "## through\n + `value`\n**r = > `value`.read**\n*r*\n\n# Counter\n## read\n*7*\n", + ) + .unwrap(); + both("qualified-objects", "---\nimport:\n a: a.mq.md\n b: b.mq.md\n---\n# main\n**x = > a.Counter**\n**y = > b.Counter**\n**p = > `x`.read**\n**q = > `y`.read**\n**r = > b.through value=`x`**\n**answer = p * 100 + q * 10 + r**\n*answer*\n", Value::Int(373), ""); +} + +#[test] +fn inherited_methods_resolve_bases_in_the_defining_module() { + let (path, _) = document("qualified-inheritance", "# main\n*0*\n"); + for (name, value) in [("a", 3), ("b", 7)] { + std::fs::write( + path.parent().unwrap().join(format!("{name}.mq.md")), + format!("# Parent\n## read\n*{value}*\n\n# Child = > Parent\n"), + ) + .unwrap(); + } + both("qualified-inheritance", "---\nimport:\n a: a.mq.md\n b: b.mq.md\n---\n# main\n**x = > a.Child**\n**y = > b.Child**\n**p = > `x`.read**\n**q = > `y`.read**\n**answer = p * 10 + q**\n*answer*\n", Value::Int(37), ""); +} + +#[test] +fn imported_type_aliases_disambiguate_inheritance() { + let (path, _) = document("aliased-base", "# main\n*0*\n"); + for (name, value) in [("a", 3), ("b", 7)] { + std::fs::write( + path.parent().unwrap().join(format!("{name}.mq.md")), + format!("# Parent\n## read\n*{value}*\n"), + ) + .unwrap(); + } + both("aliased-base","---\nimport:\n a: a.mq.md\n b: b.mq.md\n Base: a.Parent\n---\n# Child = > Base\n\n# main\n**x = > Child**\n**answer = > `x`.read**\n*answer*\n",Value::Int(3),""); +} + +#[test] +fn default_object_arguments_keep_root_module_context() { + let (path, _) = document("external-object-default", "# main\n*0*\n"); + std::fs::write( + path.parent().unwrap().join("a.mq.md"), + "# Counter\n## read\n*3*\n", + ) + .unwrap(); + std::fs::write( + path.parent().unwrap().join("b.mq.md"), + "## through\n + `value`\n**r = > `value`.read**\n*r*\n\n# Counter\n## read\n*7*\n", + ) + .unwrap(); + let source=format!("---\nimport:\n a: a.mq.md\n b: b.mq.md\n json: {}\n---\n## choose\n + `value`=> a.Counter\n**r = > json.get value=`value` key=\"_type_module\"**\n*r*\n",PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib/json.mq.md").display()); + std::fs::write(&path, &source).unwrap(); + for backend in [crate::Backend::Tree, crate::Backend::Bytecode] { + assert_eq!( + crate::call::call_file(&path, "choose", &[], backend).unwrap(), + Value::List(vec![Value::Text("a".into())]) + ); + } +} + +#[test] +fn entry_defaults_use_metadata_as_supplied_arguments() { + both("entry-metadata-default","---\nvalue: 7\n---\n# main\n + `value`=> mark\n*value*\n\n## mark\n> print text=DEFAULT\n*99*\n",Value::Int(7),""); + both( + "entry-omitted-default", + "# main\n + `value`=> mark\n*value*\n\n## mark\n> print text=DEFAULT\n*99*\n", + Value::Int(99), + "DEFAULT\n", + ); +} + +#[test] +fn named_calls_to_main_do_not_inject_entry_metadata() { + let (path, module) = document( + "named-main", + "---\nvalue: 7\n---\n# main\n + `value`=3\n*value*\n", + ); + let fun = module.functions.iter().find(|f| f.name == "main").unwrap(); + let args = vec![("value".into(), Value::Int(42))]; + let mut tree = Interpreter::with_capture(Some(&path), false); + assert_eq!( + tree.invoke_bound_function(&module, fun, &args).unwrap(), + Value::Int(42) + ); + let program = crate::bytecode::compile_for_call(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + assert_eq!( + vm.call_function(&program, program.entry, &args).unwrap(), + Value::Int(42) + ); +} + +#[cfg(feature = "exec-host")] +#[test] +fn entry_session_defaults_execute_in_the_owning_backend_context() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + for language in ["python", "node"] { + let source=format!("---\nimport:\n foreign: {}\n---\n# main\n + `s`=> foreign.session_open lang=\"{language}\"\n**> foreign.session_set id=`s` name=\"n\" value=7**\n**answer = > foreign.session_get id=`s` name=\"n\"**\n**> foreign.session_close id=`s`**\n*answer*\n",root.join("lib/foreign.mq.md").display()); + both( + &format!("entry-session-{language}"), + &source, + Value::Int(7), + "", + ); + } +} + +#[test] +fn declarations_read_only_by_defaults_are_inferred_inputs() { + let source="# main\n**answer = > choose b=7**\n*answer*\n\n## choose\n + `a`=`b`\n请求参数`b`。\n*a*\n"; + both("default-only-input", source, Value::Int(7), ""); + let (path, module) = document("default-only-input", source); + let choose = &module.functions[0].children[0]; + assert!(choose.params.iter().any(|p| p.name == "b" && p.inferred)); + for backend in [crate::Backend::Tree, crate::Backend::Bytecode] { + assert_eq!( + crate::call::call_file( + &path, + "main.choose", + &[("b".into(), Value::Int(7))], + backend + ) + .unwrap(), + Value::Int(7) + ); + } +} + +#[test] +fn explicit_and_prose_defaults_keep_declaration_order() { + let source="# main\n**answer = > choose**\n*answer*\n\n## choose\n + `a`=> mark value=1\n参数`b`=mark(2)\n**r = a * 10 + b**\n*r*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + both("prose-default-order", source, Value::Int(12), "1\n2\n"); +} + +#[test] +fn default_faults_use_the_parameter_source_line() { + let source = "# main\n**answer = > bad**\n*answer*\n\n## bad\n + `value`=1 / 0\n*value*\n"; + let (path, module) = document("default-fault-line", source); + let expected = source + .lines() + .position(|line| line.contains("+ `value`")) + .unwrap() as u32 + + 1; + let mut tree = Interpreter::with_capture(Some(&path), false); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + for result in [tree.run_module(&module), vm.run(&program)] { + let error = result.unwrap_err(); + let diagnostic = crate::diagnostics::Diagnostic::find(&error).unwrap(); + assert!(diagnostic.message.contains("division by zero")); + assert_eq!(diagnostic.span, crate::diagnostics::Span::new(expected, 1)); + assert_eq!(diagnostic.path.as_deref(), Some(path.as_path())); + } +} + +#[test] +fn duplicate_parameters_are_frontend_errors() { + let (path, _) = document("duplicate-names", "# main\n*0*\n"); + for (source, expected) in [( + "# main\n + `value`\n + `value`=7\n*value*\n", + "duplicate parameter", + )] { + std::fs::write(&path, source).unwrap(); + let error = load_module(&path).unwrap_err(); + assert!(error.to_string().contains(expected), "{error:#}"); + } +} + +#[test] +fn qualified_paths_keep_the_first_source_declaration() { + let (path, _) = document("first-qualified-declaration", "# main\n*0*\n"); + std::fs::write( + path.parent().unwrap().join("a.mq.md"), + "# A\n## read\n*1*\n\n## read\n*2*\n", + ) + .unwrap(); + both( + "first-qualified-declaration", + "---\nimport:\n a: a.mq.md\n---\n# main\n**answer = > a.A.read**\n*answer*\n", + Value::Int(1), + "", + ); +} + +#[test] +fn scratch_slots_do_not_alias_authored_names() { + let source="# main\n**ignored = mark(1)**\n**`\0call.arg.1.0` = 7**\n**ignored = mark(2)**\n*`\0call.arg.1.0`*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + both("scratch-freshness", source, Value::Int(7), "1\n2\n"); +} + +#[test] +fn default_expressions_can_read_extra_named_bindings() { + both( + "default-extra-binding", + "# main\n**answer = > choose b=7**\n*answer*\n\n## choose\n + `a`=`b`\n*a*\n", + Value::Int(7), + "", + ); +} + +#[test] +fn metadata_initialization_matches_first_binding_semantics() { + let (path, mut module) = document("metadata-first-binding", "# main\n*value*\n"); + module.metadata = vec![ + ("value".into(), Value::Int(7)), + ("value".into(), Value::Int(8)), + ]; + let mut tree = Interpreter::with_capture(Some(&path), false); + assert_eq!(tree.run_module(&module).unwrap(), Value::Int(7)); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + assert_eq!(vm.run(&program).unwrap(), Value::Int(7)); +} + +#[cfg(feature = "exec-host")] +#[test] +fn copied_object_module_tags_remain_data_across_foreign_boundaries() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + for language in ["python", "node"] { + let id = format!("copied-object-{language}"); + let (path, _) = document(&id, "# main\n*0*\n"); + for (name, value) in [("a", 3), ("b", 7)] { + std::fs::write( + path.parent().unwrap().join(format!("{name}.mq.md")), + format!("# Counter\n## read\n*{value}*\n"), + ) + .unwrap(); + } + let source=format!("---\nimport:\n a: a.mq.md\n b: b.mq.md\n foreign: {}\n---\n# main\n**x = > a.Counter**\n**s = > foreign.session_open lang=\"{language}\"**\n**> foreign.session_set id=`s` name=\"object\" value=`x`**\n**copy = > foreign.session_get id=`s` name=\"object\"**\n**> foreign.session_close id=`s`**\n**p = > `copy`.read**\n**y = > b.Counter**\n**q = > `y`.read**\n**answer = p * 10 + q**\n*answer*\n",root.join("lib/foreign.mq.md").display()); + both(&id, &source, Value::Int(37), ""); + } +} + +#[test] +fn local_constant_and_jump_indices_do_not_truncate() { + let mut locals = String::from("# main\n"); + for i in 0..700 { + locals.push_str(&format!("**x{i} = {i}**\n")); + } + locals.push_str("**answer = x0 + x699**\n*answer*\n"); + both("wide-locals", &locals, Value::Int(699), ""); + let mut constants = String::from("# main\n"); + for i in 0..70000 { + constants.push_str(&format!("**answer = {i}**\n")); + } + constants.push_str("*answer*\n"); + both("wide-constants", &constants, Value::Int(69999), ""); + let mut jump = String::from("# main\n**flag = False**\n1. `flag`\n"); + for _ in 0..25000 { + jump.push_str(" **answer = 0**\n"); + } + jump.push_str("2. *\n **answer = 8**\n\n*answer*\n"); + both("wide-jumps", &jump, Value::Int(8), ""); +} + +#[test] +fn receiver_used_only_by_a_default_is_still_an_instance_method() { + let source = format!("---\nimport:\n json: {}\n---\n# B\n**state = > json.parse text={{\"n\":7}}**\n*state*\n\n## m\n + `value`=> json.get value=`self` key=\"n\"\n*value*\n\n# main\n**b = > B**\n**answer = > `b`.m**\n*answer*\n", PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib/json.mq.md").display()); + both("receiver-default", &source, Value::Int(7), ""); + let (_, module) = document("receiver-default", &source); + assert!(crate::ast::function_uses_receiver( + &module.functions[0].children[0] + )); +} + +#[test] +fn pure_call_checks_transitive_default_effects_before_executing_them() { + let source = "## choose\n + `value`=> mark\n*value*\n\n## mark\n> print text=effect\n*7*\n"; + let (path, _) = document("default-effects-policy", source); + for backend in [crate::Backend::Tree, crate::Backend::Bytecode] { + let error = crate::call::call_file(&path, "choose", &[], backend).unwrap_err(); + let diagnostic = crate::diagnostics::Diagnostic::find(&error).unwrap(); + assert_eq!(diagnostic.code, "artifact.effect_denied", "{error:#}"); + } +} + +#[test] +fn an_imported_main_cannot_replace_the_entry_module() { + let (path, _) = document("entry-scope", "# main\n*0*\n"); + std::fs::write( + path.parent().unwrap().join("lib.mq.md"), + "# main\n> print text=WRONG-ENTRY\n*99*\n", + ) + .unwrap(); + both( + "entry-scope", + "---\nimport:\n lib: lib.mq.md\n---\n# main\n*7*\n", + Value::Int(7), + "", + ); +} + +#[test] +fn omitted_later_defaults_are_unbound_and_none_is_not_a_missing_slot() { + let source = "# main\n**answer = > f**\n*answer*\n\n## f\n + `a`=`b`\n + `b`=7\n*a*\n"; + let (path, module) = document("unbound-default", source); + let mut tree = Interpreter::with_capture(Some(&path), false); + assert!(tree + .run_module(&module) + .unwrap_err() + .to_string() + .contains("undefined variable `b`")); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + assert!(vm + .run(&program) + .unwrap_err() + .to_string() + .contains("undefined variable `b`")); +} + +#[test] +fn signed_integer_literals_include_the_negative_endpoint() { + both( + "minimum-int", + "# main\n**answer = -9223372036854775808**\n*answer*\n", + Value::Int(i64::MIN), + "", + ); +} + +#[cfg(feature = "exec-host")] +#[test] +fn nested_actuals_and_lexical_defaults_preserve_real_foreign_transitions() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + for language in ["python", "node"] { + let code = "n = n * 10 + marqdo_input; n"; + let source = format!( + r#"--- +import: + foreign: {} + json: {} +--- +# main +**h = > foreign.session_open lang="{language}"** +**> foreign.session_set id=`h` name="n" value=0** +**answer = combine(b=mark(id=h, value=2), a=mark(id=h, value=1))** +**n = > foreign.session_get id=`h` name="n"** +**> foreign.session_close id=`h`** +**r = answer * 100 + n** +*r* + +## mark + + `id` + + `value` +**record = > foreign.session_eval id=`id` source={code:?} input=`value`** +**answer = > json.get value=`record` key="result"** +*answer* + +## combine + + `a` + + `b` + + `c`=> helper +**r = a * 1000 + b * 10 + c** + +### helper +*3* + +*r* + +## helper +*9* +"#, + root.join("lib/foreign.mq.md").display(), + root.join("lib/json.mq.md").display() + ); + both( + &format!("foreign-arguments-{language}"), + &source, + Value::Int(2102321), + "", + ); + } +} + +#[test] +fn machine_arithmetic_matches_wide_reference_and_reports_language_faults() { + let values = [ + i64::MIN, + i64::MIN + 1, + -3037000500, + -7, + -1, + 0, + 1, + 7, + 3037000500, + i64::MAX - 1, + i64::MAX, + ]; + let mut records = Vec::new(); + for left in values { + for right in values { + for op in ["+", "-", "*", "/"] { + let wide = if op == "/" && right == 0 { + None + } else { + Some(match op { + "+" => left as i128 + right as i128, + "-" => left as i128 - right as i128, + "*" => left as i128 * right as i128, + "/" => left as i128 / right as i128, + _ => unreachable!(), + }) + }; + let expected = wide.and_then(|n| i64::try_from(n).ok()); + let fault = if op == "/" && right == 0 { + "division by zero" + } else { + "integer overflow" + }; + let source = format!("# main\n**answer = ({left}) {op} ({right})**\n*answer*\n"); + let (path, module) = document("machine-ints", &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let result = tree.run_module(&module); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let result_vm = vm.run(&program); + let encode = |result: anyhow::Result| match result { + Ok(value) => { + assert_eq!(Some(value.clone()), expected.map(Value::Int), "{source}"); + serde_json::json!({"value":local_json(&value)}) + } + Err(error) => { + assert!(expected.is_none(), "{source}: {error}"); + assert!(error.to_string().contains(fault), "{source}: {error}"); + serde_json::json!({"error":error.to_string()}) + } + }; + records.push(serde_json::json!({"left":left,"right":right,"op":op,"tree":encode(result),"bytecode":encode(result_vm)})); + } + } + } + for value in values { + let source = format!("# main\n**answer = -({value})**\n*answer*\n"); + let (path, module) = document("machine-negation", &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + for result in [tree.run_module(&module), vm.run(&program)] { + if value == i64::MIN { + assert!(result.unwrap_err().to_string().contains("integer overflow")); + } else { + assert_eq!(result.unwrap(), Value::Int((-(value as i128)) as i64)); + } + } + } + for value in [ + f64::INFINITY, + f64::NEG_INFINITY, + f64::NAN, + 9223372036854775808.0, + ] { + assert!(crate::builtin::builtin_int(&Value::Num(value)) + .unwrap_err() + .contains("64-bit")); + } + assert_eq!( + crate::builtin::builtin_int(&Value::Num(-9223372036854775808.0)).unwrap(), + i64::MIN + ); + if let Some(out) = std::env::var_os("MARQDO_FRONTEND_DIR") { + let out = PathBuf::from(out); + std::fs::create_dir_all(&out).unwrap(); + std::fs::write( + out.join("machine-arithmetic.json"), + serde_json::to_vec_pretty(&serde_json::json!({"version":1,"cases":records})).unwrap(), + ) + .unwrap(); + } +} + +#[test] +fn source_bound_calls_preserve_the_selected_declaration() { + let source = "# A\n## read\n*1*\n\n## read\n*2*\n"; + let (path, module) = document("source-bound-duplicates", source); + assert_eq!(module.functions[0].children.len(), 2); + let mut rows = Vec::new(); + for (index, fun) in module.functions[0].children.iter().enumerate() { + let expected = Value::Int(index as i64 + 1); + let mut observed = Vec::new(); + for backend in [crate::Backend::Tree, crate::Backend::Bytecode] { + let (value, stdout) = crate::call::execute_source_function( + &path, + fun.span.line, + "A.read", + &[], + crate::host::HostCaps::default(), + backend, + false, + ) + .unwrap(); + assert_eq!(value, expected); + assert!(stdout.is_empty()); + observed.push(local_json(&value)); + } + rows.push( + serde_json::json!({"line":fun.span.line,"tree":observed[0],"bytecode":observed[1]}), + ); + } + write_extra_evidence( + "source-bound-duplicates", + serde_json::json!({"source":source,"calls":rows}), + ); +} + +fn write_extra_evidence(name: &str, value: serde_json::Value) { + if let Some(out) = std::env::var_os("MARQDO_FRONTEND_DIR") { + let out = PathBuf::from(out); + std::fs::create_dir_all(&out).unwrap(); + std::fs::write( + out.join(format!("{name}.json")), + serde_json::to_vec_pretty(&value).unwrap(), + ) + .unwrap(); + } +} + +#[test] +fn all_four_contract_boundaries_preserve_diagnostics() { + let mut rows = Vec::new(); + for (fixture, code) in [ + ("arg_mismatch", "contract.arg_mismatch"), + ("return_mismatch", "contract.return_mismatch"), + ("field_mismatch", "contract.field_mismatch"), + ("unknown_key", "contract.unknown_key"), + ] { + let source = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/contracts") + .join(format!("{fixture}.mq.md")), + ) + .unwrap(); + let (path, module) = document(fixture, &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let tree_error = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let vm_error = vm.run(&program).unwrap_err(); + let t = crate::diagnostics::Diagnostic::find(&tree_error) + .unwrap() + .to_json(); + let v = crate::diagnostics::Diagnostic::find(&vm_error) + .unwrap() + .to_json(); + assert_eq!(t["code"], code); + assert_eq!(v["code"], code); + for key in ["span", "doc_anchor", "doc_quote", "contract_ref"] { + assert_eq!(t[key], v[key], "{fixture}: {key}"); + } + assert!(tree.captured_stdout.is_empty()); + assert!(vm.captured_stdout.is_empty()); + rows.push(serde_json::json!({"fixture":fixture,"source":source,"tree":t,"bytecode":v,"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout})); + } + // Invalid defaults must be checked before any authored body effect. + let source="# main\n**answer = > choose**\n*answer*\n\n## choose\n + `n`=\"bad\"\n\n| 参数 | 类型 | 说明 |\n|------|------|------|\n| n | number | 输入 |\n\n> print text=\"BODY\"\n*n*\n"; + let (path, module) = document("default-contract", source); + for bytecode in [false, true] { + let (error, stdout) = if bytecode { + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + (vm.run(&program).unwrap_err(), vm.captured_stdout) + } else { + let mut tree = Interpreter::with_capture(Some(&path), false); + (tree.run_module(&module).unwrap_err(), tree.captured_stdout) + }; + assert_eq!( + crate::diagnostics::Diagnostic::find(&error) + .unwrap() + .to_json()["code"], + "contract.arg_mismatch" + ); + assert!(stdout.is_empty()); + } + write_extra_evidence("contract-boundaries", serde_json::json!({"cases":rows})); + let source = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/contracts/ok_contract.mq.md"), + ) + .unwrap(); + both("valid-contract", &source, Value::Int(4), ""); +} + +#[test] +fn duplicate_actuals_fault_after_evaluation_and_only_when_reached() { + both( + "unreached-duplicate", + "# main\n*7*\n\n## unused\n> print text=\"a\" text=\"b\"\n", + Value::Int(7), + "", + ); + let source="# main\n**answer = combine(a=mark(1), a=mark(2), b=mark(3))**\n> print text=\"SUFFIX\"\n*answer*\n\n## combine\n + `a`\n + `b`\n*a*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + let (path, module) = document("duplicate-arguments", source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let t = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let v = vm.run(&program).unwrap_err(); + assert!(t.to_string().contains("duplicate named argument `a`")); + assert!(v.to_string().contains("duplicate named argument `a`")); + assert_eq!(tree.captured_stdout, "1\n2\n3\n"); + assert_eq!(vm.captured_stdout, tree.captured_stdout); + write_extra_evidence( + "duplicate-arguments", + serde_json::json!({"source":source,"tree_error":t.to_string(),"bytecode_error":v.to_string(),"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout}), + ); +} + +#[test] +fn imported_contract_blame_separates_call_site_and_declaration() { + let mut rows = Vec::new(); + for omitted in [false, true] { + let (path, _) = document( + if omitted { + "import-contract-default" + } else { + "import-contract-supplied" + }, + "# main\n*0*\n", + ); + let library="## check\n + `n`=\"bad\"\n\n| 参数 | 类型 | 说明 |\n|------|------|------|\n| n | number | 输入 |\n\n> print text=\"BODY\"\n*n*\n"; + let library_path = path.parent().unwrap().join("api.mq.md"); + std::fs::write(&library_path, library).unwrap(); + let source = format!( + "---\nimport:\n api: api.mq.md\n---\n# main\n**answer = > api.check{}**\n*answer*\n", + if omitted { "" } else { " n=\"bad\"" } + ); + std::fs::write(&path, &source).unwrap(); + let module = load_module(&path).unwrap(); + let mut tree = Interpreter::with_capture(Some(&path), false); + let te = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let ve = vm.run(&program).unwrap_err(); + let t = crate::diagnostics::Diagnostic::find(&te).unwrap().to_json(); + let v = crate::diagnostics::Diagnostic::find(&ve).unwrap().to_json(); + for diag in [&t, &v] { + assert_eq!(diag["code"], "contract.arg_mismatch"); + assert_eq!(diag["span"]["line"], 6); + assert_eq!(diag["span"]["file"], path.to_str().unwrap()); + assert_eq!( + diag["doc_anchor"], + format!("{}#L4-L6", library_path.display()) + ); + assert!(diag["suggestion"] + .as_str() + .unwrap() + .contains(&format!("{}#L4-L6", library_path.display()))); + } + assert!(tree.captured_stdout.is_empty() && vm.captured_stdout.is_empty()); + rows.push(serde_json::json!({"omitted":omitted,"source":source,"library":library,"tree":t,"bytecode":v,"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout})); + } + write_extra_evidence("imported-contracts", serde_json::json!({"cases":rows})); +} + +#[test] +fn generated_default_masks_and_declaration_orders_preserve_effects() { + let names = ["a", "b", "c"]; + let mut rows = Vec::new(); + for order in [ + [0, 1, 2], + [0, 2, 1], + [1, 0, 2], + [1, 2, 0], + [2, 0, 1], + [2, 1, 0], + ] { + for mask in 0..27usize { + let states = [mask % 3, (mask / 3) % 3, (mask / 9) % 3]; + let args = (0..3) + .filter_map(|i| match states[i] { + 0 => None, + 1 => Some(format!("{}={}", names[i], (i + 1) * 10)), + _ => Some(format!("{}=None", names[i])), + }) + .collect::>() + .join(" "); + let params = order + .iter() + .map(|i| { + format!( + " + `{}`=> mark value={}\n", + names[*i], + match i { + 0 => "1", + 1 => "`a`", + _ => "`b`", + } + ) + }) + .collect::(); + let source=format!("# main\n**answer = > choose {args}**\n*answer*\n\n## choose\n{params}\n**out = str(a) + \"|\" + str(b) + \"|\" + str(c)**\n*out*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"); + let (path, module) = document("generated-defaults", &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let tr = tree.run_module(&module); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let vr = vm.run(&program); + let encode = |r: anyhow::Result, stdout: String| match r { + Ok(value) => serde_json::json!({"value":local_json(&value),"stdout":stdout}), + Err(error) => serde_json::json!({"error":error.to_string(),"stdout":stdout}), + }; + rows.push(serde_json::json!({"order":order,"states":states,"source":source,"tree":encode(tr,tree.captured_stdout),"bytecode":encode(vr,vm.captured_stdout)})); + } + } + write_extra_evidence("generated-defaults", serde_json::json!({"cases":rows})); +} + +#[test] +fn flat_bytecode_validation_rejects_corruption_before_effects() { + use crate::bytecode::Op; + let (path, module) = document( + "flat-corruptions", + "# main\n> print text=\"BEFORE\"\n*7*\n\n## f\n + `n`=3\n*n*\n", + ); + let program = compile_module(Some(&path), &module).unwrap(); + let certs = program.verify().unwrap(); + assert_eq!(certs.len(), program.functions.len()); + let main = program.entry; + let mutations: Vec<(&str, Box)> = vec![ + ( + "constant", + Box::new(move |p| p.functions[main].code[0] = Op::Constant(usize::MAX)), + ), + ( + "local", + Box::new(move |p| p.functions[main].code[0] = Op::GetLocal(usize::MAX)), + ), + ( + "jump", + Box::new(move |p| p.functions[main].code[0] = Op::Jump(usize::MAX)), + ), + ( + "underflow", + Box::new(move |p| p.functions[main].code[0] = Op::Add), + ), + ( + "callee", + Box::new(move |p| p.functions[main].code[0] = Op::Call(usize::MAX, 0)), + ), + ( + "layout", + Box::new(move |p| p.functions[main].code[0] = Op::CallArgs(main, usize::MAX)), + ), + ( + "presence", + Box::new(move |p| p.functions[main].code[0] = Op::JumpIfProvided(usize::MAX, 1)), + ), + ( + "span", + Box::new(move |p| { + p.functions[main].spans.pop(); + }), + ), + ( + "duplicate-slot", + Box::new(move |p| { + p.functions[main].locals = vec!["x".into(), "x".into()]; + }), + ), + ( + "parameter-prefix", + Box::new(move |p| { + let f = p.functions.iter_mut().find(|f| f.name == "f").unwrap(); + f.locals[0] = "wrong".into(); + }), + ), + ]; + let mut cycle = program.clone(); + let child = cycle.functions.iter().position(|f| f.name == "f").unwrap(); + cycle.parents[main] = Some(child); + cycle.children[child].push(main); + assert!(cycle.verify().unwrap_err().contains("cyclic")); + let mut rows = Vec::new(); + for (name, mutate) in mutations { + let mut corrupted = program.clone(); + mutate(&mut corrupted); + let error = corrupted.verify().unwrap_err(); + let mut vm = Vm::with_capture(Some(&path)); + let runtime = vm.run(&corrupted).unwrap_err(); + assert!(vm.captured_stdout.is_empty(), "{name}"); + assert!( + runtime.to_string().contains("bytecode") || runtime.to_string().contains("invalid") + ); + rows.push(serde_json::json!({"mutation":name,"error":error,"stdout":vm.captured_stdout})); + } + write_extra_evidence("flat-corruptions", serde_json::json!({"cases":rows})); +} + +#[test] +fn bare_calls_use_the_source_resolver_declaration_identity() { + both( + "first-bare-declaration", + "## pick\n*1*\n\n## pick\n*2*\n\n# main\n**answer = > pick**\n*answer*\n", + Value::Int(1), + "", + ); +} + +#[test] +fn semantic_emission_checks_reject_stack_preserving_corruption() { + both("short-circuit-effects","# main\n**a = False and mark()**\n**b = True or mark()**\n**c = True and mark()**\n**d = False or mark()**\n*b and c and d*\n\n## mark\n> print text=\"RHS\"\n*True*\n",Value::Bool(true),"RHS\nRHS\n"); + use crate::bytecode::Op; + let (path, module) = document( + "semantic-corruptions", + "# main\n**a = 7**\n**b = 3**\n**answer = a + b**\n> print text=\"AFTER\"\n*answer*\n", + ); + let original = compile_module(Some(&path), &module).unwrap(); + let mut rows = Vec::new(); + for mutation in ["operator", "constant", "variable", "operand_order"] { + let mut p = original.clone(); + let f = &mut p.functions[p.entry]; + match mutation { + "operator" => *f.code.iter_mut().find(|op| matches!(op, Op::Add)).unwrap() = Op::Sub, + "constant" => { + *f.constants + .iter_mut() + .find(|v| matches!(v, Value::Int(7))) + .unwrap() = Value::Int(8) + } + "variable" => { + let a = f.locals.iter().position(|n| n == "a").unwrap(); + let b = f.locals.iter().position(|n| n == "b").unwrap(); + *f.code + .iter_mut() + .find(|op| **op == Op::GetLocal(a)) + .unwrap() = Op::GetLocal(b); + } + _ => { + let e = f + .expressions + .iter_mut() + .find(|e| { + matches!( + e.rule, + crate::bytecode::ExpressionRule::Binary(crate::ast::BinaryOp::Add) + ) + }) + .unwrap(); + e.children.swap(0, 1); + } + } + let error = p.verify().unwrap_err(); + let mut vm = Vm::with_capture(Some(&path)); + assert!(vm.run(&p).is_err()); + assert!(vm.captured_stdout.is_empty()); + rows.push( + serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout}), + ); + } + for (name, source) in [ + ("unary", "# main\n**a = 7**\n**answer = -a**\n*answer*\n"), + ( + "short_circuit", + "# main\n**answer = False and True**\n*answer*\n", + ), + ] { + let (path, module) = document(name, source); + let mut p = compile_module(Some(&path), &module).unwrap(); + let f = &mut p.functions[p.entry]; + if name == "unary" { + *f.code + .iter_mut() + .find(|op| matches!(op, Op::Negate)) + .unwrap() = Op::Not; + } else { + let op = f + .code + .iter_mut() + .find(|op| matches!(op, Op::JumpIfFalse(_))) + .unwrap(); + if let Op::JumpIfFalse(t) = op { + *t -= 1; + } + } + let error = p.verify().unwrap_err(); + rows.push(serde_json::json!({"mutation":name,"error":error,"stdout":""})); + } + write_extra_evidence("semantic-corruptions", serde_json::json!({"cases":rows})); +} + +#[test] +fn scalar_addition_and_interpolation_preserve_source_types() { + let mut rows = Vec::new(); + for expression in ["7 + True", "None + 7", "7 + False"] { + let source = format!("# main\n**answer = {expression}**\n*answer*\n"); + let (path, module) = document("scalar-source-types", &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let tr = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let vr = vm.run(&program).unwrap_err(); + assert!(tr.to_string().contains("`+` needs ints, nums, or text")); + assert!(vr.to_string().contains("`+` needs ints, nums, or text")); + rows.push(serde_json::json!({"expression":expression,"source":source,"tree_error":tr.to_string(),"bytecode_error":vr.to_string()})); + } + let (path, mut module) = document("adjacent-interpolation-ast", "# main\n*0*\n"); + module.metadata = vec![("a".into(), Value::Int(7)), ("b".into(), Value::Int(3))]; + module.functions[0].body[0] = crate::ast::Stmt::Return { + value: crate::ast::Expr::Interp(vec![ + crate::ast::InterpPart::Var("a".into()), + crate::ast::InterpPart::Var("b".into()), + ]), + span: crate::diagnostics::Span::new(2, 1), + }; + let mut tree = Interpreter::with_capture(Some(&path), false); + let tr = tree.run_module(&module).unwrap(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let vr = vm.run(&program).unwrap(); + assert_eq!(tr, Value::Text("73".into())); + assert_eq!(vr, tr); + write_extra_evidence( + "primitive-regressions", + serde_json::json!({"cases":rows,"interpolation_ast":{"tree":local_json(&tr),"bytecode":local_json(&vr)}}), + ); +} + +#[test] +fn nested_foreach_uses_fresh_slots_and_preserves_authored_names() { + let source="# main\n`xs` =\n| value |\n|---|\n| 1 |\n| 2 |\n\n**__i_xs = 99**\n**total = 0**\n- [x](遍历 xs 为 x)\n - [y](遍历 xs 为 y)\n **total = total + 1**\n\n**answer = __i_xs * 10 + total**\n*answer*\n"; + both("nested-loop-hygiene", source, Value::Int(994), ""); +} + +#[test] +fn statement_and_prologue_changes_are_rejected_before_effects() { + use crate::bytecode::Op; + let source="# main\n**answer = > choose**\n> print text=\"AFTER\"\n*answer*\n\n## choose\n + `n`=7\n**value = n + 1**\n*value*\n"; + let (path, module) = document("statement-corruptions", source); + let p = compile_module(Some(&path), &module).unwrap(); + let fid = p.functions.iter().position(|f| f.name == "choose").unwrap(); + let mut rows = Vec::new(); + for mutation in [ + "wrong_default_target", + "missing_fill", + "wrong_assignment", + "wrong_return", + "extra_instruction", + ] { + let mut bad = p.clone(); + let f = &mut bad.functions[fid]; + match mutation { + "wrong_default_target" => { + if let Op::JumpIfProvided(_, target) = &mut f.code[0] { + *target = f.body_start; + } + } + "missing_fill" => { + let pc = f + .code + .iter() + .position(|op| matches!(op, Op::FillMissingParameters)) + .unwrap(); + f.code[pc] = Op::CheckArguments; + } + "wrong_assignment" => { + let value = f.locals.iter().position(|n| n == "value").unwrap(); + let n = f.locals.iter().position(|n| n == "n").unwrap(); + *f.code + .iter_mut() + .find(|op| **op == Op::SetLocal(value)) + .unwrap() = Op::SetLocal(n); + } + "wrong_return" => { + let pc = f + .code + .iter() + .position(|op| matches!(op, Op::Return)) + .unwrap(); + f.code[pc] = Op::Pop; + } + _ => { + f.code.push(Op::None_); + f.spans.push(f.declaration_span); + } + } + let error = bad.verify().unwrap_err(); + let mut vm = Vm::with_capture(Some(&path)); + assert!(vm.run(&bad).is_err()); + assert!(vm.captured_stdout.is_empty()); + rows.push( + serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout}), + ); + } + write_extra_evidence("statement-corruptions", serde_json::json!({"cases":rows})); +} + +#[test] +fn while_iteration_limit_is_the_same_language_fault_on_both_backends() { + both("while-condition-effects", "# main\n**n = 2**\n- test(value=`n`)\n > print text=\"BODY\"\n **n = n - 1**\n*n*\n\n## test\n + `value`\n> print text=`value`\n**answer = value > 0**\n*answer*\n",Value::Int(0),"2\nBODY\n1\nBODY\n0\n"); + both( + "while-early-return", + "# main\n- True\n *7*\n> print text=\"UNREACHABLE\"\n*0*\n", + Value::Int(7), + "", + ); + both( + "while-normal", + "# main\n**n = 3**\n- `n` > 0\n **n = n - 1**\n*n*\n", + Value::Int(0), + "", + ); + let (path, mut module) = document("while-limit-ast", "# main\n*0*\n"); + module.functions[0].body = vec![crate::ast::Stmt::While { + condition: crate::ast::Expr::Literal(crate::ast::Literal::Bool(true)), + body: Vec::new(), + span: crate::diagnostics::Span::new(2, 1), + }]; + let mut tree = Interpreter::new(Some(&path), false); + let tr = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::new(Some(&path)); + let vr = vm.run(&program).unwrap_err(); + assert!(tr + .to_string() + .contains("while loop exceeded iteration limit")); + assert!(vr + .to_string() + .contains("while loop exceeded iteration limit")); + for error in [&tr, &vr] { + assert_eq!( + crate::diagnostics::Diagnostic::find(error) + .unwrap() + .span + .line, + 2 + ); + } + write_extra_evidence( + "while-limit", + serde_json::json!({"limit":1000000,"tree_error":tr.to_string(),"bytecode_error":vr.to_string(),"line":2,"input":"direct AST with literal-True empty while"}), + ); +} + +#[test] +fn dynamic_calls_keep_the_calling_module_scope() { + let (path, _) = document("dynamic-local-scope", "# main\n*0*\n"); + std::fs::write(path.parent().unwrap().join("a.mq.md"), "## hidden\n*99*\n").unwrap(); + let source="---\nimport:\n a: a.mq.md\n---\n## hidden\n*7*\n\n# main\n**answer = > call_fn name=\"hidden\"**\n*answer*\n"; + both("dynamic-local-scope", source, Value::Int(7), ""); + let denied="---\nimport:\n a: a.mq.md\n---\n# main\n**answer = > call_fn name=\"hidden\"**\n*answer*\n"; + std::fs::write(&path, denied).unwrap(); + let module = load_module(&path).unwrap(); + let mut tree = Interpreter::with_capture(Some(&path), false); + let tr = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let vr = vm.run(&program).unwrap_err(); + assert!(tr.to_string().contains("hidden")); + assert!(vr.to_string().contains("hidden")); + assert!(tree.captured_stdout.is_empty() && vm.captured_stdout.is_empty()); + write_extra_evidence( + "dynamic-import-refused", + serde_json::json!({"source":denied,"tree_error":tr.to_string(),"bytecode_error":vr.to_string(),"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout}), + ); +} + +#[test] +fn receiver_alias_defaults_use_logical_presence_and_restore_the_body_receiver() { + let method="## mark\n> print text=\"DEFAULT\"\n*9*\n\n# Box\n## m\n + `self`=> mark\n + `x`=`self`\n**answer = type(value=self) + \"|\" + type(value=x)**\n*answer*\n\n# main\n**box = > Box**\n"; + for (id, args, value, stdout) in [ + ("receiver-alias-omitted", "", "map|int", "DEFAULT\n"), + ("receiver-alias-none", " self=None", "map|map", ""), + ] { + let source = format!("{method}**answer = > `box`.m{args}**\n*answer*\n"); + both(id, &source, Value::Text(value.into()), stdout); + } + let mut cases = Vec::new(); + for (argument, expect_call_site) in + [("", false), (" self=None", false), (" self=\"bad\"", true)] + { + let source=format!("## mark\n*9*\n\n# Box\n## m\n + `self`=> mark\n + `x`=`self`\n\n| 参数 | 类型 |\n|---|---|\n| self | number |\n\n*x*\n\n# main\n**box = > Box**\n**answer = > `box`.m{argument}**\n*answer*\n"); + let (path, module) = document("receiver-alias-contract", &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let te = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let ve = vm.run(&program).unwrap_err(); + let t = crate::diagnostics::Diagnostic::find(&te).unwrap().to_json(); + let v = crate::diagnostics::Diagnostic::find(&ve).unwrap().to_json(); + assert_eq!(t["code"], "contract.arg_mismatch"); + assert_eq!(v["code"], t["code"]); + assert_eq!(t["span"], v["span"]); + assert_eq!(t["doc_anchor"], v["doc_anchor"]); + assert_eq!(t["source_sha256"], v["source_sha256"]); + assert_eq!(t["frames"], v["frames"]); + let line = if expect_call_site { 17 } else { 5 }; + assert_eq!(t["span"]["line"], line); + cases.push(serde_json::json!({"argument":argument,"source":source,"tree":t,"bytecode":v,"expected_line":line})); + } + write_extra_evidence( + "receiver-alias-contracts", + serde_json::json!({"cases":cases}), + ); +} + +#[test] +fn call_staging_checks_reject_layout_target_and_scratch_corruption() { + both("unreached-plugin", "# main\n*7*\n\n## unused_named\n> unknown_plugin z=7 a=3\n\n## unused_positional\n> unknown_plugin 9 3\n> input\n",Value::Int(7),""); + use crate::bytecode::{CallDispatch, Op}; + let source="# main\n> print text=\"PROBE\"\n**answer = combine(b=mark(2), a=mark(1))**\n*answer*\n\n## combine\n + `a`\n + `b`\n**n = a * 10 + b**\n*n*\n\n## mark\n + `value`\n*value*\n"; + let (path, module) = document("call-corruption", source); + let original = compile_module(Some(&path), &module).unwrap(); + let entry = original.entry; + let call = original.functions[entry] + .calls + .iter() + .position(|c| c.source.callee == "combine") + .unwrap(); + let target = original.functions[entry] + .calls + .iter() + .find_map(|c| { + if c.source.callee == "mark" { + match c.dispatch { + Some(CallDispatch::User { target, .. }) => Some(target), + _ => None, + } + } else { + None + } + }) + .unwrap(); + let mut cases = Vec::new(); + for mutation in [ + "target", + "layout", + "reload", + "scratch_store", + "source_alias", + "nested_alias", + "actual_order", + "uncovered_call", + ] { + let mut bad = original.clone(); + let f = &mut bad.functions[entry]; + let record = f.calls[call].clone(); + let node = &f.expressions[record.expression]; + match mutation { + "target" => { + let pc = (node.start..node.end) + .rev() + .find(|i| matches!(f.code[*i], Op::CallArgs(_, _))) + .unwrap(); + let Op::CallArgs(_, layout) = f.code[pc] else { + unreachable!() + }; + f.code[pc] = Op::CallArgs(target, layout); + } + "layout" => { + let Op::CallArgs(_, layout) = f.code[node.end - 1] else { + unreachable!() + }; + f.call_layouts[layout].swap(0, 1); + } + "reload" => { + let id = node.children[record.stages.len()]; + let pc = f.expressions[id].start; + let other = record.stages[1].slot; + f.code[pc] = Op::GetLocal(other); + f.expressions[id].rule = + crate::bytecode::ExpressionRule::Read(f.locals[other].clone()); + } + "scratch_store" => { + let pc = f.expressions[record.stages[0].expression].end; + f.code[pc] = Op::SetLocal(record.stages[1].slot); + } + "source_alias" => { + f.calls[call].stages[0].slot = 0; + } + "nested_alias" => { + let nested = f + .calls + .iter() + .position(|c| c.source.callee == "mark") + .unwrap(); + f.calls[nested].stages[0].slot = record.stages[0].slot; + } + "actual_order" => { + f.calls[call].stages.swap(0, 1); + } + "uncovered_call" => { + f.calls.remove(call); + } + _ => unreachable!(), + } + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!( + error.contains("bytecode verification"), + "{mutation}: {error}" + ); + assert_eq!(vm.captured_stdout, ""); + cases.push( + serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout}), + ); + } + for (mutation,source) in [ + ("host_identity","# main\n> print text=\"PROBE\"\n**answer = host_exists(path=\"unused\")**\n*answer*\n"), + ("method_name","# Box\n## m\n*7*\n\n# main\n> print text=\"PROBE\"\n**box = > Box**\n**answer = > `box`.m**\n*answer*\n"), + ("plugin_name","# main\n> print text=\"PROBE\"\n**answer = > unknown_plugin x=7**\n*answer*\n"), + ] { + let (path,module)=document(mutation,source);let mut bad=compile_module(Some(&path),&module).unwrap();let f=&mut bad.functions[bad.entry]; + for op in &mut f.code { + match op { + Op::HostCallArgs(id,_) if mutation=="host_identity"=>{*id=5;break;}, + Op::MethodCallArgs(id,_) if mutation=="method_name"=>{let index=f.constants.len();f.constants.push(Value::Text("other_method".into()));*id=index;break;}, + Op::PluginCallArgs(id,_) if mutation=="plugin_name"=>{let index=f.constants.len();f.constants.push(Value::Text("@other_plugin".into()));*id=index;break;}, + _=>{}, + } + } + let mut vm=Vm::with_capture(Some(&path));let error=vm.run(&bad).unwrap_err().to_string();assert!(error.contains("bytecode verification"),"{mutation}: {error}");assert_eq!(vm.captured_stdout,""); + cases.push(serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout})); + } + write_extra_evidence("call-corruptions", serde_json::json!({"cases":cases})); +} + +#[test] +fn loaded_ast_refuses_coherently_forged_emission_witnesses() { + use crate::bytecode::{CallDispatch, ExpressionRule, Op, StatementRule}; + let (path, module) = document( + "source-witness-corruption", + "# main\n> print text=\"PROBE\"\n**n = 7**\n**m = 3**\n**result = n + m**\n*result*\n", + ); + let original = compile_module(Some(&path), &module).unwrap(); + let mut cases = Vec::new(); + for mutation in [ + "operator_and_rule", + "literal_and_rule", + "variable_and_rule", + "assignment_and_rule", + "orphan_expression", + ] { + let mut bad = original.clone(); + let f = &mut bad.functions[bad.entry]; + match mutation { + "operator_and_rule" => { + let e = f + .expressions + .iter_mut() + .find(|e| matches!(e.rule, ExpressionRule::Binary(crate::ast::BinaryOp::Add))) + .unwrap(); + e.rule = ExpressionRule::Binary(crate::ast::BinaryOp::Sub); + f.code[e.end - 1] = Op::Sub; + } + "literal_and_rule" => { + let e = f + .expressions + .iter_mut() + .find(|e| matches!(e.rule, ExpressionRule::Literal(Value::Int(7)))) + .unwrap(); + let Op::Constant(index) = f.code[e.start] else { + unreachable!() + }; + f.constants[index] = Value::Int(9); + e.rule = ExpressionRule::Literal(Value::Int(9)); + } + "variable_and_rule" => { + let e = f + .expressions + .iter_mut() + .find(|e| matches!(&e.rule,ExpressionRule::Read(name) if name=="result")) + .unwrap(); + let slot = f.locals.iter().position(|name| name == "m").unwrap(); + e.rule = ExpressionRule::Read("m".into()); + f.code[e.start] = Op::GetLocal(slot); + } + "assignment_and_rule" => { + let s = f + .statements + .iter_mut() + .find(|s| matches!(&s.rule,StatementRule::Assign(name) if name=="n")) + .unwrap(); + let slot = f.locals.iter().position(|name| name == "m").unwrap(); + s.rule = StatementRule::Assign("m".into()); + f.code[s.end - 2] = Op::SetLocal(slot); + } + "orphan_expression" => { + f.expressions.push( + f.expressions + .iter() + .find(|e| matches!(e.rule, ExpressionRule::Literal(Value::Int(7)))) + .unwrap() + .clone(), + ); + } + _ => unreachable!(), + } + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!(error.contains("AST"), "{mutation}: {error}"); + assert_eq!(vm.captured_stdout, ""); + cases.push( + serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout}), + ); + } + let (path,module)=document("source-call-witness-corruption","# main\n> print text=\"PROBE\"\n**answer = f(value=7)**\n*answer*\n\n## f\n + `value`\n*7*\n\n## g\n + `value`\n*3*\n"); + let mut bad = compile_module(Some(&path), &module).unwrap(); + let target = bad.functions.iter().position(|f| f.name == "g").unwrap(); + let f = &mut bad.functions[bad.entry]; + let call = f.calls.iter_mut().find(|c| c.source.callee == "f").unwrap(); + call.source.callee = "g".into(); + call.dispatch = Some(CallDispatch::User { + target, + tag_instance: false, + }); + let node = &f.expressions[call.expression]; + let Op::CallArgs(_, layout) = f.code[node.end - 1] else { + unreachable!() + }; + f.code[node.end - 1] = Op::CallArgs(target, layout); + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!(error.contains("AST"), "{error}"); + assert_eq!(vm.captured_stdout, ""); + cases.push(serde_json::json!({"mutation":"callee_and_dispatch_witness","error":error,"stdout":vm.captured_stdout})); + for mutation in [ + "formula_bits_and_rule", + "index_key_and_rule", + "interpolation_and_rule", + ] { + use crate::ast::{Expr, IndexKey, InterpPart, Literal, Stmt}; + let (path, mut module) = document(mutation, "# main\n*0*\n"); + let span = crate::diagnostics::Span::new(2, 1); + let (prefix, value) = match mutation { + "formula_bits_and_rule" => ( + Vec::new(), + Expr::Formula(crate::formula::Expr::Num(f64::from_bits( + 0x7ff8_0000_0000_0001, + ))), + ), + "index_key_and_rule" => ( + vec![Stmt::Assign { + name: "m".into(), + value: Expr::Map(vec![ + ("a".into(), Expr::Literal(Literal::Int(7))), + ("b".into(), Expr::Literal(Literal::Int(3))), + ]), + span, + end_line: 2, + }], + Expr::Index { + base: Box::new(Expr::Var("m".into())), + label: IndexKey::Lit("a".into()), + }, + ), + _ => ( + vec![Stmt::Assign { + name: "n".into(), + value: Expr::Literal(Literal::Int(7)), + span, + end_line: 2, + }], + Expr::Interp(vec![ + InterpPart::Lit("ORIGINAL-".into()), + InterpPart::Var("n".into()), + ]), + ), + }; + module.functions[0].body = prefix; + module.functions[0].body.push(Stmt::Return { value, span }); + let mut bad = compile_module(Some(&path), &module).unwrap(); + let f = &mut bad.functions[bad.entry]; + match mutation { + "formula_bits_and_rule" => { + let e = f + .expressions + .iter_mut() + .find(|e| matches!(e.rule, ExpressionRule::Literal(Value::Formula(_)))) + .unwrap(); + let value = Value::Formula(crate::formula::Expr::Num(f64::from_bits( + 0x7ff8_0000_0000_0002, + ))); + let Op::Constant(index) = f.code[e.start] else { + unreachable!() + }; + f.constants[index] = value.clone(); + e.rule = ExpressionRule::Literal(value); + } + "index_key_and_rule" => { + let id = f + .expressions + .iter() + .position(|e| matches!(e.rule, ExpressionRule::Index { .. })) + .unwrap(); + let pc = f.expressions[f.expressions[id].children[0]].end; + let index = f.constants.len(); + f.constants.push(Value::Text("b".into())); + f.code[pc] = Op::Constant(index); + let ExpressionRule::Index { label, .. } = &mut f.expressions[id].rule else { + unreachable!() + }; + *label = IndexKey::Lit("b".into()); + } + _ => { + let e = f + .expressions + .iter_mut() + .find(|e| matches!(e.rule, ExpressionRule::Interpolation(_))) + .unwrap(); + let ExpressionRule::Interpolation(parts) = &mut e.rule else { + unreachable!() + }; + parts[0] = InterpPart::Lit("CHANGED-".into()); + let Op::Constant(index) = f.code[e.start] else { + unreachable!() + }; + f.constants[index] = Value::Text("CHANGED-".into()); + } + } + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!(error.contains("AST"), "{mutation}: {error}"); + assert_eq!(vm.captured_stdout, ""); + cases.push(serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout,"input":"direct AST; formula NaN payload bits, static index and interpolation component identity"})); + } + let (path, mut module) = document("source-contract-witness-corruption", "# main\n*7*\n"); + module.functions[0].contract = Some(crate::contract::Contract { + params: Vec::new(), + returns: Some(crate::contract::ReturnContract { + ty: crate::contract::Ty::Number, + desc: "must remain numeric".into(), + }), + fields: Vec::new(), + start_line: 1, + end_line: 1, + quote: vec!["return number".into()], + }); + let mut bad = compile_module(Some(&path), &module).unwrap(); + bad.functions[bad.entry] + .contract + .as_mut() + .unwrap() + .returns + .as_mut() + .unwrap() + .ty = crate::contract::Ty::Any; + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!(error.contains("AST"), "{error}"); + assert_eq!(vm.captured_stdout, ""); + cases.push(serde_json::json!({"mutation":"contract_metadata","error":error,"stdout":vm.captured_stdout,"input":"direct AST contract; preserve contract presence but weaken Number to Any"})); + write_extra_evidence( + "source-witness-corruptions", + serde_json::json!({"cases":cases}), + ); +} + +#[test] +fn modifier_conflicts_are_runtime_faults_before_actual_effects() { + both( + "unreached-modifier-conflict", + "# main\n*7*\n\n## unused\n> 礼貌 [f] 礼貌=False\n\n## f\n*3*\n", + Value::Int(7), + "", + ); + both( + "modifier-valid", + "# main\n**answer = 礼貌 [f]**\n*answer*\n\n## f\n + `礼貌`\n*礼貌*\n", + Value::Bool(true), + "", + ); + both( + "modifier-alias-order", + "# main\n> 内容 [打印] 内容=False\n*7*\n", + Value::Int(7), + "False\n", + ); + let defaults = + "## choose\n + `value`=> 礼貌 [f] 礼貌=False\n*value*\n\n## f\n*3*\n\n# main\n"; + both( + "modifier-default-skipped", + &format!("{defaults}**answer = > choose value=7**\n*answer*\n"), + Value::Int(7), + "", + ); + both( + "modifier-default-none", + &format!("{defaults}**answer = > choose value=None**\n*answer*\n"), + Value::None, + "", + ); + let mut cases = Vec::new(); + for (id,source,wanted_stdout,line) in [ + ("modifier-reached","# main\n> print text=\"PREFIX\"\n**answer = 礼貌 [f] 礼貌=([mark] 2)**\n> print text=\"SUFFIX\"\n\n## mark\n + `value`\n> print text=\"ACTUAL\"\n*value*\n\n## f\n*3*\n".to_string(),"PREFIX\n",3), + ("modifier-default-omitted",format!("{defaults}**answer = > choose**\n*answer*\n"),"",2), + ] { + let (path,module)=document(id,&source); + if id=="modifier-reached" { + let crate::ast::Stmt::Assign{value:crate::ast::Expr::Call(call),..}=&module.functions[0].body[1] else{panic!("not a source call")}; + assert!(matches!(&call.args[0],crate::ast::Arg::Named{value:crate::ast::Expr::Call(_),..}),"probe must contain an actual effectful argument: {:?}",call.args); + } + let mut tree=Interpreter::with_capture(Some(&path),false);let te=tree.run_module(&module).unwrap_err(); + let program=compile_module(Some(&path),&module).unwrap();let mut vm=Vm::with_capture(Some(&path));let ve=vm.run(&program).unwrap_err(); + assert!(te.to_string().contains("pre-bracket modifier"));assert!(ve.to_string().contains("pre-bracket modifier")); + assert_eq!(tree.captured_stdout,wanted_stdout);assert_eq!(vm.captured_stdout,wanted_stdout); + let t=crate::diagnostics::Diagnostic::find(&te).unwrap().to_json();let v=crate::diagnostics::Diagnostic::find(&ve).unwrap().to_json(); + for key in ["code","message","span","source_sha256","frames"]{assert_eq!(t[key],v[key],"{id}: {key}");} + assert_eq!(t["span"]["line"],line); + cases.push(serde_json::json!({"id":id,"source":source,"tree":t,"bytecode":v,"stdout":wanted_stdout,"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout,"line":line})); + } + write_extra_evidence("modifier-faults", serde_json::json!({"cases":cases})); +} + +#[test] +fn native_call_binding_faults_preserve_effects_and_diagnostic_frames() { + let mut rows = Vec::new(); + for (id, call, trace, message) in [ + ( + "len-missing", + "len()", + "", + "missing argument for parameter `value`", + ), + ( + "len-extra", + "len(mark(1), mark(2))", + "1\n2\n", + "too many positional arguments (1 extra)", + ), + ( + "len-unknown-named", + "len(other=mark(1))", + "1\n", + "missing argument for parameter `value`", + ), + ( + "str-missing", + "str()", + "", + "missing argument for parameter `value`", + ), + ( + "print-missing", + "print()", + "", + "missing argument for parameter `text`", + ), + ( + "split-missing", + "split(mark(1))", + "1\n", + "missing argument for parameter `sep`", + ), + ( + "split-extra", + "split(mark(1), mark(2), mark(3))", + "1\n2\n3\n", + "too many positional arguments (1 extra)", + ), + ( + "at-missing", + "at(mark(1))", + "1\n", + "missing argument for parameter `index`", + ), + ( + "call-fn-missing", + "call_fn()", + "", + "missing argument for parameter `name`", + ), + ( + "host-missing", + "host_exists(extra=mark(1))", + "1\n", + "host_exists requires `path`", + ), + ( + "host-extra", + "host_now_unix(mark(1))", + "1\n", + "too many positional arguments (1 extra)", + ), + ( + "plugin-mixed", + "missing_plugin(mark(1), extra=mark(2))", + "1\n2\n", + "unknown function `missing_plugin`", + ), + ( + "unknown-path", + "missing_lib.f(value=mark(1))", + "1\n", + "unresolved dynamic call `missing_lib.f`", + ), + ( + "unknown-deep-path", + "missing_lib.Box.f(value=mark(1))", + "1\n", + "unknown `missing_lib.Box.f`", + ), + ( + "duplicate-builtin", + "len(value=mark(1), value=mark(2))", + "1\n2\n", + "duplicate named argument `value`", + ), + ( + "duplicate-host", + "host_now_unix(extra=mark(1), extra=mark(2))", + "1\n2\n", + "duplicate named argument `extra`", + ), + ( + "duplicate-unknown-plugin", + "missing_plugin(a=mark(1), a=mark(2))", + "1\n2\n", + "unknown function `missing_plugin`", + ), + ( + "duplicate-unknown-path", + "missing_lib.f(a=mark(1), a=mark(2))", + "1\n2\n", + "unresolved dynamic call `missing_lib.f`", + ), + ] { + let body = format!( + "> print text=\"PREFIX\"\n**answer = {call}**\n> print text=\"SUFFIX\"\n*answer*\n" + ); + let mark = "\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + let unreached = format!("# main\n*7*\n\n## unused\n{body}{mark}"); + both( + &format!("call-fault-{id}-unreached"), + &unreached, + Value::Int(7), + "", + ); + let source = format!("# main\n{body}{mark}"); + let (path, module) = document(&format!("call-fault-{id}"), &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let te = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let ve = vm.run(&program).unwrap_err(); + let t = crate::diagnostics::Diagnostic::find(&te).unwrap().to_json(); + let v = crate::diagnostics::Diagnostic::find(&ve).unwrap().to_json(); + let wanted_stdout = format!("PREFIX\n{trace}"); + assert_eq!(t["message"], message, "{id}: source error"); + assert_eq!(tree.captured_stdout, wanted_stdout, "{id}: source trace"); + assert_eq!(vm.captured_stdout, wanted_stdout, "{id}: VM trace"); + for key in ["code", "message", "span", "source_sha256", "frames"] { + assert_eq!(t[key], v[key], "{id}: {key}"); + } + assert_eq!(t["span"]["line"], 3); + rows.push(serde_json::json!({"id":id,"source":source,"expected_message":message,"expected_stdout":wanted_stdout,"tree":t,"bytecode":v,"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout})); + } + write_extra_evidence("call-binding-faults", serde_json::json!({"cases":rows})); +} + +#[test] +fn path_dispatch_uses_runtime_presence_before_static_fallback() { + let definitions = "# Box\n## read\n*1*\n\n# Other\n## read\n*7*\n\n"; + both( + "path-bound-receiver", + &format!("{definitions}# main\n**Box = > Other**\n**answer = > Box.read**\n*answer*\n"), + Value::Int(7), + "", + ); + both( + "path-unbound-fallback", + &format!("{definitions}# main\n**answer = > Box.read**\n*answer*\n**Box = > Other**\n"), + Value::Int(1), + "", + ); +} + +#[cfg(feature = "plugin-host")] +#[test] +fn registered_plugins_bind_mixed_arguments_after_authored_effects() { + let source="# main\n**answer = binding_probe(mark(2), b=mark(3), ignored=mark(9))**\n*answer*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + let (path, module) = document("plugin-native-binding", source); + let host = || { + let mut h = crate::host::HostContext::for_capture(Some(&path), Default::default()); + h.plugins.register_binding_probe(); + h + }; + let expected = Value::Map(vec![ + ("a".into(), Value::Int(2)), + ("b".into(), Value::Int(3)), + ("ignored".into(), Value::Int(9)), + ]); + let mut tree = Interpreter::with_capture(Some(&path), false).with_host(host()); + let t = tree.run_module(&module).unwrap(); + assert_eq!(t, expected); + assert_eq!(tree.captured_stdout, "2\n3\n9\n"); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)).with_host(host()); + let v = vm.run(&program).unwrap(); + assert_eq!(v, expected); + assert_eq!(vm.captured_stdout, "2\n3\n9\n"); + write_extra_evidence( + "registered-plugin-binding", + serde_json::json!({"source":source,"expected":local_json(&expected),"tree":local_json(&t),"bytecode":local_json(&v),"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout,"abi":"native-json-c"}), + ); +} + +#[test] +fn source_declaration_graph_rejects_coherent_target_and_order_corruption() { + use crate::bytecode::{CallDispatch, Op, PathFallback}; + let source="# main\n> print text=\"PROBE\"\n**answer = choose()**\n*answer*\n\n## choose\n*1*\n\n## other\n*2*\n"; + both("same-signature-targets", source, Value::Int(1), "PROBE\n"); + let (path, module) = document("name-graph-corruption", source); + let original = compile_module(Some(&path), &module).unwrap(); + let entry = original.entry; + let other = original + .functions + .iter() + .position(|f| f.name == "other") + .unwrap(); + let mut rows = Vec::new(); + for mutation in [ + "coherent-target", + "child-order", + "declaration-identity", + "scope", + "module-header", + "missing-root", + "object-name", + "method-name", + ] { + let mut bad = original.clone(); + match mutation { + "coherent-target" => { + let f = &mut bad.functions[entry]; + let c = f + .calls + .iter_mut() + .find(|c| c.source.callee == "choose") + .unwrap(); + c.dispatch = Some(CallDispatch::User { + target: other, + tag_instance: false, + }); + let e = &f.expressions[c.expression]; + let Op::CallArgs(_, layout) = f.code[e.end - 1] else { + panic!("native call") + }; + f.code[e.end - 1] = Op::CallArgs(other, layout); + } + "child-order" => bad.children[entry].reverse(), + "declaration-identity" => { + bad.functions[other].source_ast.declaration_path = + bad.functions[entry].source_ast.declaration_path.clone() + } + "scope" => bad.functions[other].module_scope = 99, + "module-header" => bad.functions[other] + .source_ast + .imports + .push("invented".into()), + "missing-root" => bad.functions[entry].source_ast.module_root_count += 1, + "object-name" => bad.objects[0].0 = "invented".into(), + "method-name" => bad.methods[0].1 = "invented".into(), + _ => unreachable!(), + } + let rejection = bad.verify().unwrap_err(); + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!(error.contains(&rejection), "{mutation}: {error}"); + assert_eq!(vm.captured_stdout, ""); + rows.push( + serde_json::json!({"mutation":mutation,"error":error,"stdout":vm.captured_stdout}), + ); + } + let (path,module)=document("path-fallback-corruption","# Box\n## read\n*1*\n\n## other\n*2*\n\n# main\n> print text=\"PROBE\"\n**answer = > Box.read**\n*answer*\n"); + let mut bad = compile_module(Some(&path), &module).unwrap(); + let other = bad + .functions + .iter() + .position(|f| f.name == "other") + .unwrap(); + let call = bad.functions[bad.entry] + .calls + .iter_mut() + .find(|c| c.source.path.is_some()) + .unwrap(); + let Some(CallDispatch::Path { fallback, .. }) = &mut call.dispatch else { + panic!("path call") + }; + *fallback = PathFallback::User { + target: other, + tag_instance: false, + }; + let mut vm = Vm::with_capture(Some(&path)); + let error = vm.run(&bad).unwrap_err().to_string(); + assert!(error.contains("bytecode verification")); + assert_eq!(vm.captured_stdout, ""); + rows.push(serde_json::json!({"mutation":"coherent-path-fallback","error":error,"stdout":vm.captured_stdout})); + write_extra_evidence("name-graph-corruptions", serde_json::json!({"cases":rows})); +} + +#[test] +fn user_parameter_masks_preserve_fault_priority_and_actual_effects() { + let definitions="\n## choose\n + `a`=> mark value=9\n + `b`\n**answer = a * 10 + b**\n*answer*\n\n## mark\n + `value`\n> print text=`value`\n*value*\n"; + both( + "masked-default-binding", + &format!("# main\n**answer = choose(b=mark(2))**\n*answer*\n{definitions}"), + Value::Int(92), + "2\n9\n", + ); + both( + "named-before-positional", + &format!("# main\n**answer = choose(mark(1), a=mark(2))**\n*answer*\n{definitions}"), + Value::Int(21), + "1\n2\n", + ); + let mut rows = Vec::new(); + for (id, call, trace, message) in [ + ( + "user-surplus", + "choose(mark(1), mark(2), mark(3))", + "1\n2\n3\n", + "too many positional arguments (1 extra)", + ), + ( + "user-required-before-default", + "choose()", + "", + "missing argument for parameter `b`", + ), + ( + "user-duplicate", + "choose(a=mark(1), a=mark(2))", + "1\n2\n", + "duplicate named argument `a`", + ), + ] { + let body = format!( + "> print text=\"PREFIX\"\n**answer = {call}**\n> print text=\"SUFFIX\"\n*answer*\n" + ); + both( + &format!("{id}-unreached"), + &format!("# main\n*7*\n\n## unused\n{body}{definitions}"), + Value::Int(7), + "", + ); + let source = format!("# main\n{body}{definitions}"); + let (path, module) = document(id, &source); + let mut tree = Interpreter::with_capture(Some(&path), false); + let te = tree.run_module(&module).unwrap_err(); + let program = compile_module(Some(&path), &module).unwrap(); + let mut vm = Vm::with_capture(Some(&path)); + let ve = vm.run(&program).unwrap_err(); + let t = crate::diagnostics::Diagnostic::find(&te).unwrap().to_json(); + let v = crate::diagnostics::Diagnostic::find(&ve).unwrap().to_json(); + let wanted_stdout = format!("PREFIX\n{trace}"); + assert_eq!(t["message"], message); + assert_eq!(tree.captured_stdout, wanted_stdout); + assert_eq!(vm.captured_stdout, wanted_stdout); + for key in ["message", "code", "span", "source_sha256", "frames"] { + assert_eq!(t[key], v[key], "{id}: {key}"); + } + rows.push(serde_json::json!({"id":id,"source":source,"expected_message":message,"expected_stdout":wanted_stdout,"tree":t,"bytecode":v,"tree_stdout":tree.captured_stdout,"bytecode_stdout":vm.captured_stdout})); + } + write_extra_evidence("user-binding-faults", serde_json::json!({"cases":rows})); +} + +#[test] +fn argument_binding_plans_cover_parameter_masks_and_labels() { + let alphabet = [ + None, + Some("a".to_string()), + Some("b".to_string()), + Some("extra".to_string()), + ]; + let mut cases = Vec::new(); + for parameters in [ + vec![], + vec!["a"], + vec!["a", "b"], + vec!["a", "a"], + vec!["a", "b", "c"], + ] { + let params = parameters.iter().map(|p| p.to_string()).collect::>(); + for mask in 0..(1usize << params.len()) { + let omissions = (0..params.len()) + .map(|i| mask & (1 << i) != 0) + .collect::>(); + for size in 0..=4 { + for code in 0..4usize.pow(size as u32) { + let mut digits = code; + let mut names = Vec::new(); + for _ in 0..size { + names.push(alphabet[digits % 4].clone()); + digits /= 4; + } + cases.push(crate::arguments::plan_evidence(¶ms, &names, &omissions)); + } + } + } + } + assert_eq!(cases.len(), 6479, "complete mask/label corpus"); + write_extra_evidence("argument-binding-plans", serde_json::json!({"cases":cases})); +} diff --git a/src/host/dispatch.rs b/src/host/dispatch.rs index d763cb0..3740de0 100644 --- a/src/host/dispatch.rs +++ b/src/host/dispatch.rs @@ -220,6 +220,8 @@ pub enum HostFn { ForeignSessionInterrupt = 211, /// `` M7: `access(集合, 键)` — polymorphic collection access (legacy index). Access = 212, + OracleJournal = 213, + OracleReplayCheck = 214, } /// Single source of truth for every host function. `` slimming @@ -438,6 +440,8 @@ pub const HOST_FN_TABLE: &[HostFnDef] = &[ HostFnDef { id: 210, f: HostFn::ForeignSessionList, name: "host_foreign_session_list", required: &[], optional: &[] }, HostFnDef { id: 211, f: HostFn::ForeignSessionInterrupt, name: "host_foreign_session_interrupt", required: &["id"], optional: &[] }, HostFnDef { id: 212, f: HostFn::Access, name: "host_access", required: &["value", "key"], optional: &[] }, + HostFnDef { id: 213, f: HostFn::OracleJournal, name: "host_oracle_journal", required: &[], optional: &[] }, + HostFnDef { id: 214, f: HostFn::OracleReplayCheck, name: "host_oracle_replay_check", required: &["expected", "observed"], optional: &[] }, ]; @@ -466,6 +470,8 @@ impl HostFn { "host_cwd" => Self::Cwd, "host_exit" => Self::Exit, "host_exec" => Self::Exec, + "host_oracle_journal" => Self::OracleJournal, + "host_oracle_replay_check" => Self::OracleReplayCheck, "host_json_parse" => Self::JsonParse, "host_json_stringify" => Self::JsonStringify, "host_json_get" => Self::JsonGet, @@ -748,7 +754,13 @@ pub fn call_host( bound.get("args"), bound.get("capture"), ), - HostFn::JsonParse => json::parse(require(bound, "text")?), + HostFn::OracleJournal => Ok(Value::List(ctx.oracle_observations.clone())), + HostFn::OracleReplayCheck => super::oracle::replay_check(require(bound, "expected")?, require(bound, "observed")?), + HostFn::JsonParse => { + let value = json::parse(require(bound, "text")?)?; + super::oracle::append(ctx, "decode", &value)?; + Ok(value) + }, HostFn::JsonStringify => { json::stringify(require(bound, "value")?, bound.get("indent")) } @@ -1127,7 +1139,7 @@ pub fn call_host( HostFn::ForeignSessionEval => { #[cfg(feature = "exec-host")] { - crate::host::foreign_session::eval_with_stdin( + let record = crate::host::foreign_session::eval_with_stdin( ctx, require(bound, "id")?, bound.get("code"), @@ -1135,7 +1147,10 @@ pub fn call_host( bound.get("input"), bound.get("stdin").and_then(|v| v.reveal_text()), bound.get("timeout"), - ) + )?; + let value = super::oracle::eval_observation(&record)?; + super::oracle::append(ctx, "eval", &value)?; + Ok(record) } #[cfg(not(feature = "exec-host"))] { @@ -1162,11 +1177,13 @@ pub fn call_host( HostFn::ForeignSessionGet => { #[cfg(feature = "exec-host")] { - crate::host::foreign_session::get( + let value = crate::host::foreign_session::get( ctx, require(bound, "id")?, require(bound, "name")?, - ) + )?; + super::oracle::append(ctx, "get", &value)?; + Ok(value) } #[cfg(not(feature = "exec-host"))] { @@ -1177,7 +1194,9 @@ pub fn call_host( HostFn::ForeignSessionNames => { #[cfg(feature = "exec-host")] { - crate::host::foreign_session::names(ctx, require(bound, "id")?) + let value = crate::host::foreign_session::names(ctx, require(bound, "id")?)?; + super::oracle::append(ctx, "decode", &value)?; + Ok(value) } #[cfg(not(feature = "exec-host"))] { diff --git a/src/host/foreign.rs b/src/host/foreign.rs index 1122715..7937587 100644 --- a/src/host/foreign.rs +++ b/src/host/foreign.rs @@ -15,7 +15,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use crate::foreign::registry::{self, LangSpec}; use crate::foreign::split_cmd_line; -use crate::host::json::{json_to_value, value_to_json}; +use crate::host::json::value_to_json; use crate::host::{HostCaps, HostContext}; use crate::value::{CodeBlock, Value}; @@ -158,6 +158,7 @@ pub(crate) struct Execution { pub lang: String, pub cmd: String, pub script: String, + pub diagnostic_source: String, } impl Execution { @@ -180,6 +181,7 @@ impl Execution { ("result_error".into(), Value::Text(result_error)), ("error".into(), Value::Text(self.error.clone())), ("script".into(), Value::Text(self.script.clone())), + ("diagnostic_source".into(), Value::Text(self.diagnostic_source.clone())), ("cached".into(), Value::Bool(false)), ]) } @@ -588,10 +590,11 @@ fn prepare_invoke(args: &InvokeArgs<'_>) -> Result { let stdin = opt_text(args.stdin)?; let input_json = match args.input { None | Some(Value::None) => None, - Some(v) => Some( - serde_json::to_string(&value_to_json(v)?) - .map_err(|e| format!("input JSON encode: {e}"))?, - ), + Some(v) => { + super::oracle::admit(v)?; + Some(serde_json::to_string(&value_to_json(v)?) + .map_err(|e| format!("input JSON encode: {e}"))?) + }, }; if stdin.is_some() && input_json.is_some() { return Err("stdin and input are mutually exclusive".into()); @@ -620,7 +623,7 @@ pub fn invoke(ctx: &mut HostContext, args: InvokeArgs<'_>) -> Result) -> Result(&exec.stdout) { - Ok(j) => json_to_value(&j), - Err(e) => Err(format!( - "foreign call: stdout is not JSON: {e}; stdout: {}", - tail(exec.stdout.trim(), 512) - )), - } + super::json::decode_text(&exec.stdout).map_err(|e| match e { + super::json::DecodeError::Domain(e) => e, + super::json::DecodeError::Syntax(e) => format!("foreign call: stdout is not JSON: {e}; stdout: {}", tail(exec.stdout.trim(), 512)), + }) } fn present(v: Option<&Value>) -> Option<&Value> { @@ -752,17 +752,18 @@ fn error_record(ctx: &HostContext, job: &Job, error: String) -> Value { lang: job.lang.clone(), cmd: display_for_job(ctx, job, None), script: String::new(), + diagnostic_source: String::new(), } .record(Value::None, String::new()) } -fn run_prepared(env: &ForeignEnv, items: Vec, concurrency: usize) -> Vec<(usize, Value)> { +fn run_prepared(env: &ForeignEnv, items: Vec, concurrency: usize) -> Result, String> { if items.is_empty() { - return Vec::new(); + return Ok(Vec::new()); } let workers = concurrency.max(1).min(items.len()); let queue = Arc::new(Mutex::new(VecDeque::from(items))); - let results: Arc>> = Arc::new(Mutex::new(Vec::new())); + let results: Arc)>>> = Arc::new(Mutex::new(Vec::new())); thread::scope(|scope| { for _ in 0..workers { let queue = Arc::clone(&queue); @@ -782,11 +783,10 @@ fn run_prepared(env: &ForeignEnv, items: Vec, concurrency: usize) execute_job(&ctx, &item.job) })) { Ok(Ok(exec)) => { - let (result, result_error) = parse_output(&exec.stdout, &item.output); - exec.record(result, result_error) + parse_output(&exec.stdout, &item.output).map(|(result, error)| exec.record(result, error)) } - Ok(Err(e)) => error_record(&ctx, &item.job, e), - Err(_) => error_record(&ctx, &item.job, "foreign worker panicked".into()), + Ok(Err(e)) => Ok(error_record(&ctx, &item.job, e)), + Err(_) => Ok(error_record(&ctx, &item.job, "foreign worker panicked".into())), }; if let Ok(mut out) = results.lock() { out.push((item.idx, value)); @@ -799,7 +799,7 @@ fn run_prepared(env: &ForeignEnv, items: Vec, concurrency: usize) Err(shared) => shared.lock().map(|m| m.clone()).unwrap_or_default(), }; out.sort_by_key(|(i, _)| *i); - out + out.into_iter().map(|(i, result)| result.map(|value| (i, value)).map_err(|e| format!("foreign[{i}]: {e}"))).collect() } /// Build a prepared job for one `runs` item (code value or option map). @@ -851,7 +851,7 @@ pub fn run_many( requested }; let env = ForeignEnv::from_ctx(ctx); - let results = run_prepared(&env, prepared, concurrency); + let results = run_prepared(&env, prepared, concurrency)?; Ok(Value::List(results.into_iter().map(|(_, v)| v).collect())) } @@ -879,6 +879,7 @@ pub fn call_many( }; let mut prepared = Vec::with_capacity(n); for (idx, input) in list.iter().enumerate() { + super::oracle::admit(input)?; let json = serde_json::to_string(&value_to_json(input)?) .map_err(|e| format!("call_many input JSON encode: {e}"))?; let mut job = base.job.clone(); @@ -891,7 +892,7 @@ pub fn call_many( }); } let env = ForeignEnv::from_ctx(ctx); - let records = run_prepared(&env, prepared, concurrency); + let records = run_prepared(&env, prepared, concurrency)?; let mut results = Vec::with_capacity(records.len()); let mut errors = Vec::new(); for (idx, record) in records { @@ -936,34 +937,15 @@ fn resolve_invoke_source(args: &InvokeArgs<'_>) -> Result<(String, String), Stri )) } -fn parse_output(stdout: &str, mode: &str) -> (Value, String) { - match mode { - "text" => (Value::None, String::new()), - "lines" => ( - Value::List(stdout.lines().map(|l| Value::Text(l.to_string())).collect()), - String::new(), - ), - "json" => { - if stdout.trim().is_empty() { - return (Value::None, String::new()); - } - match serde_json::from_str::(stdout) { - Ok(j) => match json_to_value(&j) { - Ok(v) => (v, String::new()), - Err(e) => (Value::None, e), - }, - Err(e) => (Value::None, format!("stdout is not JSON: {e}")), - } - } - _ => { - if stdout.trim().is_empty() { - return (Value::None, String::new()); - } - match serde_json::from_str::(stdout) { - Ok(j) => (json_to_value(&j).unwrap_or(Value::None), String::new()), - Err(_) => (Value::None, String::new()), - } - } +fn parse_output(stdout: &str, mode: &str) -> Result<(Value, String), String> { + if mode == "text" { return Ok((Value::None, String::new())); } + if mode == "lines" { return Ok((Value::List(stdout.lines().map(|s| Value::Text(s.into())).collect()), String::new())); } + if stdout.trim().is_empty() { return Ok((Value::None, String::new())); } + match super::json::decode_text(stdout) { + Ok(v) => Ok((v, String::new())), + Err(super::json::DecodeError::Domain(e)) => Err(e), + Err(super::json::DecodeError::Syntax(e)) if mode == "json" => Ok((Value::None, format!("stdout is not JSON: {e}"))), + Err(super::json::DecodeError::Syntax(_)) => Ok((Value::None, String::new())), } } @@ -1734,6 +1716,7 @@ fn execute_job(ctx: &HostContext, job: &Job) -> Result { lang: job.lang.clone(), cmd: display_for_job(ctx, job, None), script: String::new(), + diagnostic_source: String::new(), }); } }; @@ -1803,6 +1786,7 @@ fn execute_job(ctx: &HostContext, job: &Job) -> Result { lang: job.lang.clone(), cmd, script: script_keep, + diagnostic_source: script_path.display().to_string(), }) } @@ -1837,7 +1821,7 @@ pub fn invoke_for_view( cmd_override: cmd_override.map(str::to_string), }; let exec = execute_job(&ctx, &job)?; - let (result, result_error) = parse_output(&exec.stdout, "auto"); + let (result, result_error) = parse_output(&exec.stdout, "auto")?; Ok(exec.record(result, result_error)) } @@ -1886,13 +1870,13 @@ mod tests { #[test] fn output_modes() { - let (v, e) = parse_output("42", "auto"); + let (v, e) = parse_output("42", "auto").unwrap(); assert!(e.is_empty()); assert_eq!(v, Value::Int(42)); - let (v, e) = parse_output("a\nb\n", "lines"); + let (v, e) = parse_output("a\nb\n", "lines").unwrap(); assert!(e.is_empty()); assert_eq!(v, Value::List(vec![Value::Text("a".into()), Value::Text("b".into())])); - let (v, e) = parse_output("not json", "json"); + let (v, e) = parse_output("not json", "json").unwrap(); assert!(v == Value::None); assert!(!e.is_empty()); } @@ -1965,4 +1949,19 @@ mod tests { assert_eq!(map_get(&records[1], "ok"), Some(&Value::Bool(false))); assert_eq!(map_get(&records[2], "stdout").and_then(|v| v.reveal_text()), Some("c\n")); } + #[test] + fn structured_output_domain_errors_are_not_none_or_success_records() { + for mode in ["auto", "json"] { + for bad in ["9007199254740992", "9.007199254740992e15", "1e9999", r#"{"x":1,"x":2}"#, r#"{"a":[],"b":[1]}"#] { + assert!(parse_output(bad, mode).is_err(), "{mode}: {bad}"); + } + } + let ctx = HostContext::default(); + let env = ForeignEnv::from_ctx(&ctx); + let items = (0..2).map(|idx| PreparedMany { + idx, job: Job::simple("python", "print('{\"x\":1,\"x\":2}')", None), output: "auto".into(), + }).collect(); + assert!(run_prepared(&env, items, 2).unwrap_err().contains("duplicate")); + } + } diff --git a/src/host/foreign_doc.rs b/src/host/foreign_doc.rs index 1c9b139..0151043 100644 --- a/src/host/foreign_doc.rs +++ b/src/host/foreign_doc.rs @@ -19,6 +19,10 @@ use crate::host::foreign_session; use crate::host::HostContext; use crate::value::Value; +mod control; +pub mod packing; +pub use control::DocumentControl; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum CellStatus { Never, @@ -92,6 +96,8 @@ pub struct JournalRecord { /// One recorded Ω step: what a later `replay_check` compares against. #[derive(Debug, Clone)] pub struct OracleRecord { + pub value: Value, + pub stdin: String, pub seq: u64, pub cell: String, pub kernel: String, @@ -103,7 +109,34 @@ pub struct OracleRecord { pub result: Value, } +impl OracleRecord { + fn to_value(&self) -> Value { + Value::Map(vec![ + ("value".into(), self.value.clone()), + ("seq".into(), Value::Int(self.seq as i64)), + ("stdin".into(), Value::Text(self.stdin.clone())), + ("cell".into(), Value::Text(self.cell.clone())), + ("kernel".into(), Value::Text(self.kernel.clone())), + ("lang".into(), Value::Text(self.lang.clone())), + ("source_hash".into(), Value::Text(self.source_hash.clone())), + ("ok".into(), Value::Bool(self.ok)), + ("code".into(), Value::Int(self.code)), + ("stdout".into(), Value::Text(self.stdout.clone())), + ("result".into(), self.result.clone()), + ]) + } +} + pub struct DocumentSession { + pub control: DocumentControl, + replay_controls: Vec, + replay_boundary: Option, + source_hash: String, + initial_values: Vec<(String, String, Value)>, + initial_image: Value, + action_history: Vec, + action_depth: usize, + replay_fresh: bool, pub path: PathBuf, root: PathBuf, pub persistent: bool, @@ -347,15 +380,29 @@ fn has_nested_plan(module: &crate::ast::Module, entry_name: &str) -> bool { .unwrap_or(false) } +mod replay; + impl DocumentSession { + pub(crate) fn adopt_control(&mut self, control: DocumentControl) { + self.control = control; + for (name, id) in &self.kernel_sessions { + if let Some(session) = self.host.foreign_sessions.get(id) { + self.control.register(name, session.interrupt_handle.clone()); + } + } + } pub fn open(path: &Path, root: &Path) -> Result { let source = std::fs::read_to_string(path) .map_err(|e| format!("read {}: {e}", path.display()))?; + Self::open_source(path, root, &source, true) + } + + fn open_source(path: &Path, root: &Path, source: &str, initialize: bool) -> Result { let bind_ctx = crate::binding::BindingContext::from_binds( &[], crate::binding::cwd_for_path(Some(path)), ); - let module = crate::load::load_module_with_ctx(path, &bind_ctx) + let module = crate::load::load_module_with_source(path, source, &bind_ctx) .map_err(|e| format!("load {}: {e}", path.display()))?; let entry = module .functions @@ -479,9 +526,20 @@ impl DocumentSession { host.cwd = doc_dir.to_path_buf(); host.fs_root = Some(root.to_path_buf()); host.entry_path = Some(path.to_path_buf()); - host.entry_source = Some(source); + host.entry_metadata = module.metadata.clone(); + let source_hash = hash_source(&source); + host.entry_source = Some(source.to_string()); let mut session = Self { + control: DocumentControl::default(), + replay_controls: Vec::new(), + replay_boundary: None, + source_hash, + initial_values: Vec::new(), + initial_image: Value::None, + action_history: Vec::new(), + action_depth: 0, + replay_fresh: false, path: path.to_path_buf(), root: root.to_path_buf(), persistent, @@ -507,13 +565,10 @@ impl DocumentSession { let value = col_value(&row, &["值", "Value", "value", "数据"]) .ok_or_else(|| format!("foreign.session values[{name}]: missing 值/Value"))? .clone(); - let id = session.ensure_kernel_session(&kernel)?; - foreign_session::set( - &mut session.host, - &Value::Handle(id), - &Value::Text(name), - &value, - )?; + if session.kernel(&kernel).is_none() { return Err(format!("unknown kernel `{kernel}`")); } + if !foreign_session::valid_name(&name) { return Err("initial value: name must be an identifier".into()); } + super::oracle::admit(&value)?; + session.initial_values.push((kernel, name, value)); } } if let Some(rows) = plan_inputs { @@ -528,12 +583,22 @@ impl DocumentSession { } } } + session.initial_image = session.plan_image(); + if initialize { session.initialize_values()?; } + session.replay_fresh = true; Ok(session) } /// Re-read the document, preserving kernels/state/journal. Changed cells become dirty. - pub fn reload(&mut self, root: &Path) -> Result<(), String> { - let mut fresh = Self::open(&self.path, root)?; + fn reload_source(&mut self, source: &str, root: &Path) -> Result<(), String> { + let fresh = Self::open_source(&self.path, root, source, false)?; + self.install_reload(fresh) + } + + fn install_reload(&mut self, mut fresh: Self) -> Result<(), String> { + let new_source = fresh.host.entry_source.clone(); + let new_metadata = fresh.host.entry_metadata.clone(); + let old_kernels = self.kernels.clone(); let old: HashMap = self .cells .iter() @@ -546,6 +611,9 @@ impl DocumentSession { c.last_ms = o.last_ms; c.last_ok = o.last_ok; c.last_error = o.last_error.clone(); + c.last_stdout = o.last_stdout.clone(); + c.last_code = o.last_code; + c.last_result = o.last_result.clone(); c.status = if o.source_hash != c.source_hash || o.kernel != c.kernel { CellStatus::Dirty } else if o.status == CellStatus::Running { @@ -556,13 +624,26 @@ impl DocumentSession { } } fresh.host = std::mem::replace(&mut self.host, HostContext::default()); + fresh.host.entry_source = new_source; + fresh.host.entry_metadata = new_metadata; + fresh.initial_image = self.initial_image.clone(); + fresh.action_history = std::mem::take(&mut self.action_history); + fresh.control = self.control.clone(); + fresh.replay_controls = std::mem::take(&mut self.replay_controls); + fresh.replay_boundary = self.replay_boundary.take(); + fresh.action_depth = self.action_depth; + fresh.generation = self.generation + 1; fresh.kernel_sessions = std::mem::take(&mut self.kernel_sessions); fresh.value_store = std::mem::take(&mut self.value_store); fresh.journal = std::mem::take(&mut self.journal); + fresh.oracle_journal = std::mem::take(&mut self.oracle_journal); fresh.seq = self.seq; + fresh.replay_fresh = false; fresh.cursor = self.cursor.min(fresh.order.len()); fresh.opened_ms = self.opened_ms; - let known: HashSet = fresh.kernels.iter().map(|k| k.name.clone()).collect(); + let known: HashSet = fresh.kernels.iter().filter(|k| + old_kernels.iter().any(|old| old.name == k.name && old.lang == k.lang && old.cmd == k.cmd) + ).map(|k| k.name.clone()).collect(); let orphans: Vec = fresh .kernel_sessions .keys() @@ -596,6 +677,7 @@ impl DocumentSession { } fn ensure_kernel_session(&mut self, kernel_name: &str) -> Result { + self.replay_fresh = false; if let Some(id) = self.kernel_sessions.get(kernel_name) { if self.host.foreign_sessions.contains_key(id) { return Ok(id.clone()); @@ -619,12 +701,20 @@ impl DocumentSession { Value::Handle(s) => s, _ => return Err("session_open returned non-handle id".into()), }; - self.kernel_sessions - .insert(kernel_name.to_string(), id.clone()); + for (kernel, name, value) in self.initial_values.clone() { + if kernel == kernel_name { + if let Err(error) = foreign_session::set(&mut self.host, &Value::Handle(id.clone()), &Value::Text(name), &value) { + let _ = foreign_session::close(&mut self.host, &Value::Handle(id.clone())); + return Err(error); + } + } + } + self.kernel_sessions.insert(kernel_name.to_string(), id.clone()); + self.control.register(kernel_name, self.host.foreign_sessions[&id].interrupt_handle.clone()); Ok(id) } - fn record_result(&mut self, cell: &str, kernel: &str, record: &Value) -> Result<(), String> { + fn record_result(&mut self, cell: &str, kernel: &str, record: &Value, stdin: Option<&str>) -> Result<(), String> { let field = |k: &str| match record { Value::Map(entries) => entries .iter() @@ -656,9 +746,10 @@ impl DocumentSession { ) }) .unwrap_or_default(); - let last_result = field("result") - .filter(|v| !matches!(v, Value::None)) - .unwrap_or_else(|| { + // An explicit null is a semantic result, even when stdout is nonempty. + // Keep display output separate; only legacy records lacking the field + // can use their stdout as a fallback. + let last_result = field("result").unwrap_or_else(|| { field("stdout") .and_then(|v| v.reveal_text().map(|s| Value::Text(s.to_string()))) .unwrap_or(Value::None) @@ -686,7 +777,11 @@ impl DocumentSession { timestamp_ms: now_ms(), error, }); + let value = super::oracle::eval_observation(record)?; + super::oracle::append(&mut self.host, "eval", &value)?; self.oracle_journal.push(OracleRecord { + value, + stdin: stdin.unwrap_or_default().to_string(), seq: self.seq, cell: cell.to_string(), kernel: kernel.to_string(), @@ -697,6 +792,7 @@ impl DocumentSession { stdout, result: last_result, }); + self.check_replay_cell_boundary(); Ok(()) } @@ -704,12 +800,13 @@ impl DocumentSession { self.run_cell_with_stdin(cell_name, kernel_override, None) } - pub fn run_cell_with_stdin( + fn run_cell_with_stdin_inner( &mut self, cell_name: &str, kernel_override: Option<&str>, stdin: Option<&str>, ) -> Result { + self.replay_fresh = false; let ci = self .cell_index(cell_name) .ok_or_else(|| format!("unknown cell `{cell_name}`"))?; @@ -736,7 +833,7 @@ impl DocumentSession { ); return match result { Ok(record) => { - self.record_result(cell_name, "one-shot", &record)?; + self.record_result(cell_name, "one-shot", &record, stdin)?; Ok(record) } Err(e) => { @@ -772,6 +869,7 @@ impl DocumentSession { self.cells[ci].status = CellStatus::Running; let result = (|| -> Result { let id = self.ensure_kernel_session(&kernel)?; + if self.control.cancelled() { return Err("document action interrupted before evaluation".into()); } let source = Value::Text(self.cells[ci].source.clone()); let record = foreign_session::eval_with_stdin( &mut self.host, @@ -782,7 +880,7 @@ impl DocumentSession { stdin, None, )?; - self.record_result(cell_name, &kernel, &record)?; + self.record_result(cell_name, &kernel, &record, stdin)?; Ok(record) })(); if result.is_err() { @@ -795,7 +893,7 @@ impl DocumentSession { self.step_with_stdin(None) } - pub fn step_with_stdin(&mut self, stdin: Option<&str>) -> Result { + fn step_with_stdin_inner(&mut self, stdin: Option<&str>) -> Result { if self.cursor >= self.order.len() { return Ok(Value::Map(vec![ ("done".into(), Value::Bool(true)), @@ -830,7 +928,7 @@ impl DocumentSession { self.run_all_with_inputs(restart, stdin, None) } - pub fn run_all_with_inputs( + fn run_all_with_inputs_inner( &mut self, restart: bool, stdin: Option<&str>, @@ -843,6 +941,9 @@ impl DocumentSession { let mut records = Vec::new(); let mut failed = 0i64; for name in self.order.clone() { + if self.control.cancelled() { break; } + if !self.replay_batch_can_continue() { break; } + let seq_before = self.seq; let cell_stdin = inputs .and_then(|m| m.get(&name)) .map(String::as_str) @@ -879,19 +980,27 @@ impl DocumentSession { other => vec![("result".into(), other)], }; m.push(("cell".into(), Value::Text(name))); - records.push(Value::Map(m)); + let record = Value::Map(m); + self.check_replay_batch_record(&record, seq_before); + records.push(record); self.cursor += 1; } - Ok(Value::Map(vec![ - ("ok".into(), Value::Bool(failed == 0)), - ("ran".into(), Value::Int(self.order.len() as i64)), + let mut summary = vec![ + ("ok".into(), Value::Bool(failed == 0 && !self.control.cancelled())), + ("ran".into(), Value::Int(records.len() as i64)), ("failed".into(), Value::Int(failed)), ("records".into(), Value::List(records)), - ])) + ]; + if self.control.cancelled() { + summary.push(("cancelled".into(), Value::Bool(true))); + summary.push(("remaining".into(), Value::Int((self.order.len() - self.cursor) as i64))); + } + Ok(Value::Map(summary)) } - pub fn interrupt(&mut self) -> Result { - let ids: Vec = self.kernel_sessions.keys().cloned().collect(); + fn interrupt_inner(&mut self) -> Result { + let mut ids: Vec = self.kernel_sessions.keys().cloned().collect(); + ids.sort(); let mut out = Vec::new(); for name in ids { if let Some(id) = self.kernel_sessions.get(&name) { @@ -904,7 +1013,7 @@ impl DocumentSession { Ok(Value::List(out)) } - pub fn restart_kernel(&mut self, name: &str) -> Result { + fn restart_kernel_inner(&mut self, name: &str) -> Result { if let Some(id) = self.kernel_sessions.remove(name) { let _ = foreign_session::close(&mut self.host, &Value::Handle(id)); } @@ -917,8 +1026,9 @@ impl DocumentSession { Ok(Value::Bool(true)) } - pub fn restart_all(&mut self) -> Result { - let names: Vec = self.kernel_sessions.keys().cloned().collect(); + fn restart_all_inner(&mut self) -> Result { + let mut names: Vec = self.kernel_sessions.keys().cloned().collect(); + names.sort(); for name in names { self.restart_kernel(&name)?; } @@ -929,7 +1039,7 @@ impl DocumentSession { Ok(Value::Bool(true)) } - pub fn assign_kernel(&mut self, cell: &str, kernel: &str) -> Result { + fn assign_kernel_inner(&mut self, cell: &str, kernel: &str) -> Result { let ci = self .cell_index(cell) .ok_or_else(|| format!("unknown cell `{cell}`"))?; @@ -950,7 +1060,7 @@ impl DocumentSession { Ok(Value::None) } - pub fn add_cell( + fn add_cell_inner( &mut self, cell: &str, lang: &str, @@ -995,7 +1105,7 @@ impl DocumentSession { Ok(Value::Bool(true)) } - pub fn remove_cell(&mut self, cell: &str) -> Result { + fn remove_cell_inner(&mut self, cell: &str) -> Result { let Some(i) = self.cell_index(cell) else { return Err(format!("unknown cell `{cell}`")); }; @@ -1006,13 +1116,13 @@ impl DocumentSession { Ok(Value::Bool(true)) } - pub fn set_value(&mut self, kernel: &str, name: &str, value: Value) -> Result { + fn set_value_inner(&mut self, kernel: &str, name: &str, value: Value) -> Result { let out = self.push_value(kernel, name, value)?; self.generation += 1; Ok(out) } - pub fn state_list(&mut self) -> Result { + fn state_list_inner(&mut self) -> Result { let marqdo = self .value_store .iter() @@ -1043,7 +1153,10 @@ impl DocumentSession { ])) } - pub fn push_value(&mut self, kernel: &str, name: &str, value: Value) -> Result { + fn push_value_inner(&mut self, kernel: &str, name: &str, value: Value) -> Result { + if self.kernel(kernel).is_none() { return Err(format!("unknown kernel `{kernel}`")); } + if !foreign_session::valid_name(name) { return Err("session_set: name must be an identifier".into()); } + super::oracle::admit(&value)?; let id = self.ensure_kernel_session(kernel)?; foreign_session::set( &mut self.host, @@ -1055,13 +1168,16 @@ impl DocumentSession { Ok(Value::None) } - pub fn pull_value(&mut self, kernel: &str, name: &str) -> Result { + fn pull_value_inner(&mut self, kernel: &str, name: &str) -> Result { + if self.kernel(kernel).is_none() { return Err(format!("unknown kernel `{kernel}`")); } + if !foreign_session::valid_name(name) { return Err("session_get: name must be an identifier".into()); } let id = self.ensure_kernel_session(kernel)?; let value = foreign_session::get( &mut self.host, &Value::Handle(id), &Value::Text(name.to_string()), )?; + super::oracle::append(&mut self.host, "get", &value)?; self.value_store .insert(name.to_string(), value.clone()); Ok(value) @@ -1137,7 +1253,7 @@ impl DocumentSession { } /// Clear one cell's displayed output/record; kernels and live state survive. - pub fn clear_cell(&mut self, cell: &str) -> Result { + fn clear_cell_inner(&mut self, cell: &str) -> Result { let i = self .cell_index(cell) .ok_or_else(|| format!("unknown cell `{cell}`"))?; @@ -1159,36 +1275,7 @@ impl DocumentSession { /// Clear displayed outputs and per-cell records; kernels and live state survive. /// Export plan, explicit values, kernel values and journal as JSON-safe data. pub fn export_snapshot(&mut self) -> Result { - let mut kernels = Vec::new(); - for decl in self.kernels.clone() { - 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::Handle(id.clone())) - { - for name in names { - if let Value::Text(n) = &name { - if let Ok(v) = foreign_session::get( - &mut self.host, - &Value::Handle(id.clone()), - &Value::Text(n.clone()), - ) { - values.push(Value::Map(vec![ - ("name".into(), Value::Text(n.clone())), - ("value".into(), v), - ])); - } - } - } - } - } - kernels.push(Value::Map(vec![ - ("name".into(), Value::Text(decl.name.clone())), - ("lang".into(), Value::Text(decl.lang.clone())), - ("cmd".into(), Value::Text(decl.cmd.clone().unwrap_or_default())), - ("values".into(), Value::List(values)), - ])); - } + let kernels = self.tracked("inspect",Value::Map(Vec::new()), |session| session.inspect_kernels())?; let cells = self .cells .iter() @@ -1213,23 +1300,30 @@ impl DocumentSession { .collect(); let journal = self.journal_json(); Ok(Value::Map(vec![ - ("version".into(), Value::Int(1)), + ("document_hash".into(), Value::Text(self.source_hash.clone())), + ("version".into(), Value::Int(5)), + ("initial_image".into(), self.initial_image.clone()), + ("action_count".into(), Value::Int(self.action_history.len() as i64)), + ("actions".into(), self.action_history_json()), + ("observation_journal".into(), Value::List(self.host.oracle_observations.clone())), + ("oracle_count".into(), Value::Int(self.complete_oracle_rows().len() as i64)), ("path".into(), Value::Text(self.path.display().to_string())), ("persistent".into(), Value::Bool(self.persistent)), ("generation".into(), Value::Int(self.generation as i64)), - ("kernels".into(), Value::List(kernels)), + ("kernels".into(), kernels), ("cells".into(), Value::List(cells)), ("order".into(), Value::List( self.order.iter().cloned().map(Value::Text).collect(), )), ("journal".into(), journal), - ("oracle_journal".into(), self.oracle_journal_json()), + ("oracle_journal".into(), Value::List(self.complete_oracle_rows())), ])) } /// Import JSON-safe values from an exported snapshot. Source identity and /// cell names must already match; no arbitrary objects are deserialized. - pub fn import_snapshot(&mut self, snapshot: &Value) -> Result { + fn import_snapshot_inner(&mut self, snapshot: &Value) -> Result { + replay::validate_control(snapshot)?; let Value::Map(fields) = snapshot else { return Err("snapshot must be a map".into()); }; @@ -1242,6 +1336,27 @@ impl DocumentSession { let Some(Value::List(kernels)) = field("kernels") else { return Err("snapshot: missing kernels".into()); }; + if field("document_hash") != Some(Value::Text(self.source_hash.clone())) { + return Err("snapshot: document hash differs".into()); + } + // Validate all crossing payloads before mutating any live foreign store. + for kernel in &kernels { + let Value::Map(kfields) = kernel else { return Err("snapshot: malformed kernel".into()) }; + let Some((_, Value::Text(name))) = kfields.iter().find(|(k, _)| k == "name") + else { return Err("snapshot: kernel name missing".into()) }; + if self.kernel(name).is_none() { return Err(format!("snapshot: unknown kernel `{name}`")); } + let Some((_, Value::List(values))) = kfields.iter().find(|(k, _)| k == "values") + else { return Err("snapshot: values missing".into()) }; + for item in values { + let Value::Map(vfields) = item else { return Err("snapshot: malformed value".into()) }; + let Some((_, Value::Text(name))) = vfields.iter().find(|(k, _)| k == "name") + else { return Err("snapshot: value name missing".into()) }; + if !foreign_session::valid_name(name) { return Err("snapshot: invalid value name".into()); } + let Some((_, value)) = vfields.iter().find(|(k, _)| k == "value") + else { return Err("snapshot: value missing".into()) }; + super::oracle::admit(value)?; + } + } let mut restored = 0i64; for kernel in kernels { let Value::Map(kfields) = kernel else { continue }; @@ -1299,6 +1414,7 @@ impl DocumentSession { } } } + self.replay_fresh = false; self.generation += 1; Ok(Value::Int(restored)) } @@ -1306,7 +1422,7 @@ 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 { + fn replay_snapshot_inner(&mut self, snapshot: &Value) -> Result { let Value::Map(fields) = snapshot else { return Err("snapshot must be a map".into()); }; @@ -1331,11 +1447,13 @@ impl DocumentSession { if self.cell_index(&name).is_none() { continue; } + if !self.replay_batch_can_continue() { break; } 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())?; + self.check_replay_batch_record(&record, self.seq.saturating_sub(1)); let mut m = match record { Value::Map(m) => m, other => vec![("result".into(), other)], @@ -1350,165 +1468,14 @@ 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 { - 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 { + fn persist_cell_inner(&mut self, cell: &str, key: &str) -> Result { let i = self .cell_index(cell) .ok_or_else(|| format!("unknown cell `{cell}`"))?; let value = self.cells[i].last_result.clone(); - if matches!(value, Value::None) { - return Err(format!("cell `{cell}` has no result to persist")); + if self.cells[i].runs == 0 || !self.cells[i].last_ok { + return Err(format!("cell `{cell}` has no successful result to persist")); } let key = if key.is_empty() { cell } else { key }; let line = self.cells[i].line_end.max(self.cells[i].line_start); @@ -1523,7 +1490,7 @@ impl DocumentSession { Ok(Value::Bool(true)) } - pub fn clear_outputs(&mut self) -> Value { + fn clear_outputs_inner(&mut self) -> Value { for cell in &mut self.cells { cell.runs = 0; cell.last_ms = 0; @@ -1564,27 +1531,11 @@ 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(), - ) + Value::List(self.oracle_journal.iter().map(OracleRecord::to_value).collect()) } - pub fn close(&mut self) { + fn close_inner(&mut self) { + self.replay_fresh = false; let ids: Vec = self.kernel_sessions.values().cloned().collect(); for id in ids { let _ = foreign_session::close(&mut self.host, &Value::Handle(id)); @@ -1950,6 +1901,10 @@ console.log(globalThis.kept); let snap = a.export_snapshot().unwrap(); a.close(); let mut b = DocumentSession::open(&path, &root).unwrap(); + let mut wrong_source = snap.clone(); + replace_field(&mut wrong_source, "document_hash", Value::Text("different-document".into())); + assert!(b.import_snapshot(&wrong_source).unwrap_err().contains("hash")); + assert!(b.kernel_sessions.is_empty()); let restored = b.import_snapshot(&snap).unwrap(); assert!(matches!(restored, Value::Int(n) if n > 0)); let rate = b.pull_value("分析", "rate").unwrap(); @@ -2142,7 +2097,14 @@ import: field(&record, "stdout").and_then(|v| v.reveal_text().map(|s| s.to_string())), Some("hello node\n".to_string()) ); + s.run_cell_with_stdin("a", None, Some("second input 中文")).unwrap(); + let snapshot = s.export_snapshot().unwrap(); s.close(); + let mut fresh = DocumentSession::open(&path, &root).unwrap(); + let replay = fresh.replay_check(&snapshot).unwrap(); + assert_eq!(field(&replay, "ok"), Some(Value::Bool(true))); + assert_eq!(field(&replay, "matched"), Some(Value::Int(2))); + fresh.close(); let _ = std::fs::remove_file(&path); } @@ -2192,7 +2154,14 @@ print(sys.stdin.read().strip()) field(&record, "stdout").and_then(|v| v.reveal_text().map(|s| s.to_string())), Some("hello persistent\n".to_string()) ); + s.run_cell_with_stdin("a", None, Some("second input 中文")).unwrap(); + let snapshot = s.export_snapshot().unwrap(); s.close(); + let mut fresh = DocumentSession::open(&path, &root).unwrap(); + let replay = fresh.replay_check(&snapshot).unwrap(); + assert_eq!(field(&replay, "ok"), Some(Value::Bool(true))); + assert_eq!(field(&replay, "matched"), Some(Value::Int(2))); + fresh.close(); let _ = std::fs::remove_file(&path); } @@ -2297,11 +2266,13 @@ console.log("after error"); let r = s.run_cell("a", None).unwrap(); assert_eq!(field(&r, "ok"), Some(Value::Bool(true))); assert_eq!(s.cells[0].status, CellStatus::Clean); + let before = s.oracle_journal_json(); let src2 = "# main\n\n`a` =\n```node\nconsole.log(2);\n```\n"; std::fs::write(&path, src2).unwrap(); s.reload(&root).unwrap(); assert_eq!(s.cells[0].status, CellStatus::Dirty); assert!(s.cells[0].source.contains("console.log(2)")); + assert_eq!(s.oracle_journal_json(), before, "reload must retain observations of the old source"); s.close(); let _ = std::fs::remove_file(&path); } @@ -2374,13 +2345,13 @@ console.log("after error"); // 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") + fields.iter_mut().find(|(k, _)| k == "oracle_journal") { for cell in cells.iter_mut() { let Value::Map(cfields) = cell else { continue }; let name = cfields .iter() - .find(|(k, _)| k == "name") + .find(|(k, _)| k == "cell") .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") { @@ -2391,10 +2362,12 @@ console.log("after error"); } } + replay::sync_expected_cells(&mut snap).unwrap(); 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))); + assert_eq!(field(&check, "skipped"), Some(Value::Int(1))); let Some(Value::List(checks)) = field(&check, "checks") else { panic!("missing checks"); }; @@ -2412,4 +2385,168 @@ console.log("after error"); assert!(diff.contains("stdout"), "diff should localize to stdout: {diff}"); b.close(); } + fn snapshot_list_mut<'a>(snap: &'a mut Value, key: &str) -> &'a mut Vec { + let Value::Map(m) = snap else { panic!("map") }; + let (_, Value::List(xs)) = m.iter_mut().find(|(k, _)| k == key).unwrap() + else { panic!("list") }; + xs + } + + fn replace_field(v: &mut Value, key: &str, value: Value) { + let Value::Map(m) = v else { panic!("map") }; + m.iter_mut().find(|(k, _)| k == key).unwrap().1 = value; + } + + #[test] + fn replay_check_rejects_incomplete_recordings_before_execution() { + assert!(node_available(), "replay experiment requires node"); + 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 original = a.export_snapshot().unwrap(); + a.close(); + let cases = ["empty", "truncate", "insert", "duplicate", "reorder", "unknown", + "missing_cell", "non_map", "missing_outcome", "wrong_bool", "wrong_code", + "wrong_stdout", "missing_result", "source", "routing", "plan", "version", + "missing_oracle", "duplicate_field", "stdin_type"]; + for case in cases { + let mut snap = original.clone(); + match case { + "empty" => snapshot_list_mut(&mut snap, "oracle_journal").clear(), + "truncate" => { snapshot_list_mut(&mut snap, "oracle_journal").pop(); } + "insert" | "duplicate" => { + let xs = snapshot_list_mut(&mut snap, "oracle_journal"); + xs.push(xs[0].clone()); + } + "reorder" => snapshot_list_mut(&mut snap, "oracle_journal").swap(0, 1), + "unknown" => replace_field(&mut snapshot_list_mut(&mut snap, "oracle_journal")[0], "cell", Value::Text("unknown".into())), + "missing_cell" => { snapshot_list_mut(&mut snap, "cells").pop(); } + "non_map" => snapshot_list_mut(&mut snap, "oracle_journal")[0] = Value::None, + "missing_outcome" | "missing_result" => { + let Value::Map(m) = &mut snapshot_list_mut(&mut snap, "oracle_journal")[0] else { panic!() }; + m.retain(|(k, _)| k != if case == "missing_result" { "result" } else { "ok" }); + } + "wrong_bool" | "wrong_code" | "wrong_stdout" | "stdin_type" => { + let key = match case { "wrong_bool" => "ok", "wrong_code" => "code", "wrong_stdout" => "stdout", _ => "stdin" }; + replace_field(&mut snapshot_list_mut(&mut snap, "oracle_journal")[0], key, Value::None); + } + "source" => replace_field(&mut snapshot_list_mut(&mut snap, "cells")[0], "source", Value::Text("console.log(42)".into())), + "routing" => replace_field(&mut snapshot_list_mut(&mut snap, "oracle_journal")[0], "kernel", Value::Text("绘图".into())), + "plan" => snapshot_list_mut(&mut snap, "order").swap(0, 1), + "version" => replace_field(&mut snap, "version", Value::Int(1)), + "missing_oracle" => { let Value::Map(m) = &mut snap else { panic!() }; m.retain(|(k, _)| k != "oracle_journal"); } + "duplicate_field" => { let Value::Map(m) = &mut snapshot_list_mut(&mut snap, "oracle_journal")[0] else { panic!() }; m.push(m[0].clone()); } + _ => unreachable!(), + } + let mut b = DocumentSession::open(&path, &root).unwrap(); + let result = b.replay_check(&snap); + assert!(result.is_err(), "{case}: {result:?}"); + assert!(b.oracle_journal.is_empty(), "{case}: executed before validation"); + assert!(b.kernel_sessions.is_empty(), "{case}: started a kernel before validation"); + println!("E1 {case}: rejected before execution: {}", result.unwrap_err()); + b.close(); + } + } + + #[test] + fn replay_check_uses_oracle_not_cell_summary_or_execution_journal() { + assert!(node_available()); + 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(); + // Repeat the read: a per-cell summary cannot represent this history. + a.run_cell("读取", None).unwrap(); + let mut snap = a.export_snapshot().unwrap(); + a.close(); + snapshot_list_mut(&mut snap, "journal").clear(); + for c in snapshot_list_mut(&mut snap, "cells") { + replace_field(c, "stdout", Value::Text("irrelevant-summary".into())); + } + 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, "matched"), Some(Value::Int(4))); + assert_eq!(field(&check, "skipped"), Some(Value::Int(0))); + assert!(b.replay_check(&snap).unwrap_err().contains("fresh")); + b.close(); + } + + #[test] + fn replay_check_accepts_declared_initial_values_but_rejects_external_changes() { + assert!(node_available()); + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let path = root.join(format!("target/replay-initial-{}.mq.md", std::process::id())); + let source = std::fs::read_to_string(root.join("tests/notebook/shared.mq.md")).unwrap() + .replace("`会话` = > foreign.session kernels=`内核表` cells=`会话表`", + "`初始` =\n\n| 内核 | 名称 | 值 |\n|---|---|---|\n| 分析 | seed | 41 |\n\n`会话` = > foreign.session kernels=`内核表` cells=`会话表` values=`初始`"); + std::fs::write(&path, source).unwrap(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + assert!(!a.kernel_sessions.is_empty(), "fixture must initialize a live kernel"); + a.run_all(false).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))); + b.close(); + let mut warm = DocumentSession::open(&path, &root).unwrap(); + warm.set_value("分析", "seed", Value::Int(99)).unwrap(); + assert!(warm.replay_check(&snap).unwrap_err().contains("fresh")); + warm.close(); + std::fs::remove_file(path).unwrap(); + } + + #[test] + fn snapshot_introspection_does_not_pollute_document_observations() { + assert!(node_available()); + 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(false).unwrap(); + let before = s.host.oracle_observations.clone(); + assert_eq!(before.len(), 3); + s.export_snapshot().unwrap(); + s.export_snapshot().unwrap(); + assert_eq!(s.host.oracle_observations, before, "snapshot get/names are metadata, not document actions"); + // The cell trace projects to exactly the same language observations. + let projected: Vec = s.oracle_journal.iter().enumerate().map(|(i,r)| + Value::Map(vec![("index".into(), Value::Int(i as i64)), + ("channel".into(), Value::Text("eval".into())), ("value".into(), r.value.clone())])).collect(); + assert_eq!(before, projected); + s.pull_value("分析", "shared").unwrap(); + assert_eq!(s.host.oracle_observations.len(), before.len() + 1, "explicit user get must be observed"); + s.close(); + } + + #[test] + fn replay_check_compares_failed_cell_diagnostics() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let path = root.join(format!("target/replay-failure-{}.mq.md", std::process::id())); + let source = "# main\n\n`a` =\n```python\nraise ValueError('ordinary error')\n```\n"; + std::fs::write(&path, source).unwrap(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + let record = a.run_cell("a", None).unwrap(); + assert_eq!(field(&record, "ok"), Some(Value::Bool(false))); + let mut snap = a.export_snapshot().unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + let initial_check = b.replay_check(&snap).unwrap(); + assert_eq!(field(&initial_check, "ok"), Some(Value::Bool(true)), "{initial_check:?}"); + b.close(); + let rows = snapshot_list_mut(&mut snap, "oracle_journal"); + let Value::Map(fields) = &mut rows[0] else { panic!() }; + let (_, value) = fields.iter_mut().find(|(k,_)| k == "value").unwrap(); + replace_field(value, "error", Value::Text("different failure".into())); + replay::sync_expected_cells(&mut snap).unwrap(); + let mut c = DocumentSession::open(&path, &root).unwrap(); + let check = c.replay_check(&snap).unwrap(); + assert_eq!(field(&check, "ok"), Some(Value::Bool(false))); + let Some(Value::List(checks)) = field(&check, "checks") else { panic!() }; + assert!(field(&checks[0], "diff").unwrap().as_display().contains("value")); + c.close(); + std::fs::remove_file(path).unwrap(); + } + } diff --git a/src/host/foreign_doc/control.rs b/src/host/foreign_doc/control.rs new file mode 100644 index 0000000..2ac8c77 --- /dev/null +++ b/src/host/foreign_doc/control.rs @@ -0,0 +1,174 @@ +//! Concurrent control inputs are scoped to a single recorded document action. +//! Replay supplies their elapsed-time inputs, reobserves acceptance, and stops +//! on divergence; elapsed wall time is not a deterministic foreign-state oracle. +use crate::host::foreign_session::InterruptHandle; +use crate::value::Value; +use std::collections::BTreeMap; +use std::sync::{Arc, Condvar, Mutex}; +use std::thread::JoinHandle; +use std::time::{Duration, Instant}; + +#[derive(Clone, Debug, PartialEq)] +pub(super) struct ControlEvent { + pub after_us: u64, + pub results: Vec<(String, Result)>, +} +impl ControlEvent { + pub fn value(&self) -> Value { + Value::Map(vec![ + ("after_us".into(), Value::Int(self.after_us as i64)), + ( + "results".into(), + Value::List( + self.results + .iter() + .map(|(name, result)| { + Value::Map(vec![ + ("kernel".into(), Value::Text(name.clone())), + ("accepted".into(), Value::Bool(result.is_ok())), + ("active".into(), Value::Bool(matches!(result, Ok(true)))), + ( + "error".into(), + Value::Text(result.as_ref().err().cloned().unwrap_or_default()), + ), + ]) + }) + .collect(), + ), + ), + ]) + } +} +#[derive(Default, Debug)] +struct State { + kernels: BTreeMap, + generation: u64, + action: Option<(Instant, Vec)>, +} +#[derive(Default, Debug)] +struct Shared { + state: Mutex, + changed: Condvar, +} +#[derive(Clone, Default, Debug)] +pub struct DocumentControl(Arc); + +impl DocumentControl { + pub(super) fn cancelled(&self) -> bool { + self.0 + .state + .lock() + .unwrap() + .action + .as_ref() + .is_some_and(|(_, events)| !events.is_empty()) + } + pub(super) fn register(&self, kernel: &str, handle: InterruptHandle) { + self.0 + .state + .lock() + .unwrap() + .kernels + .insert(kernel.into(), handle); + } + pub(super) fn begin(&self, plan: Vec) -> Option> { + let generation = { + let mut state = self.0.state.lock().unwrap(); + state.generation += 1; + state.action = Some((Instant::now(), Vec::new())); + state.generation + }; + if plan.is_empty() { + return None; + } + let controller = self.clone(); + Some(std::thread::spawn(move || { + for event in plan { + let mut state = controller.0.state.lock().unwrap(); + loop { + if state.generation != generation || state.action.is_none() { + return; + } + let elapsed = state.action.as_ref().unwrap().0.elapsed(); + let remaining = Duration::from_micros(event.after_us).saturating_sub(elapsed); + if remaining.is_zero() { + break; + } + state = controller + .0 + .changed + .wait_timeout(state, remaining.min(Duration::from_millis(200))) + .unwrap() + .0; + } + // Keep the action-generation check and signal under one lock. + let _ = Self::request_locked(&mut state, Some(event.after_us)); + } + })) + } + pub(super) fn finish(&self, scheduler: Option>) -> Vec { + let events = self + .0 + .state + .lock() + .unwrap() + .action + .take() + .map(|(_, events)| events) + .unwrap_or_default(); + self.0.changed.notify_all(); + if let Some(scheduler) = scheduler { + let _ = scheduler.join(); + } + events + } + pub fn interrupt(&self) -> Result { + let mut state = self + .0 + .state + .lock() + .map_err(|_| "document control lock poisoned")?; + Self::request_locked(&mut state, None).map(|event| event.value()) + } + fn request_locked( + state: &mut State, + scheduled_us: Option, + ) -> Result { + let start = state + .action + .as_ref() + .ok_or("no document action in flight")? + .0; + let after_us = scheduled_us + .unwrap_or_else(|| start.elapsed().as_micros().min(i64::MAX as u128) as u64); + let results = state + .kernels + .iter() + .filter(|(_, handle)| handle.is_live()) + .map(|(name, handle)| (name.clone(), handle.request())) + .collect(); + let event = ControlEvent { after_us, results }; + state.action.as_mut().unwrap().1.push(event.clone()); + Ok(event) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn completed_action_cancels_pending_scheduler_before_next_generation() { + let controller = DocumentControl::default(); + let scheduler = controller.begin(vec![ControlEvent { + after_us: 60_000_000, + results: Vec::new(), + }]); + let start = Instant::now(); + assert!(controller.finish(scheduler).is_empty()); + assert!(start.elapsed() < Duration::from_secs(1)); + assert!(controller.interrupt().is_err()); + let next = controller.begin(Vec::new()); + assert!(!controller.cancelled()); + assert!(controller.finish(next).is_empty()); + } +} diff --git a/src/host/foreign_doc/packing.rs b/src/host/foreign_doc/packing.rs new file mode 100644 index 0000000..ff615f6 --- /dev/null +++ b/src/host/foreign_doc/packing.rs @@ -0,0 +1,399 @@ +//! Lossless, typed, bottom-up sharing for complete history snapshots. +//! References always point backwards. Expansion costs and depth are checked +//! before building the expanded value, so a small DAG cannot expand unboundedly. +use crate::host::{json::value_to_json, oracle::admit}; +use crate::value::Value; +use sha2::{Digest, Sha256}; +use std::collections::{HashMap, HashSet}; + +const FORMAT: &str = "marqdo.history.dag"; +const MAX_NODES: usize = 1_000_000; +const MAX_VALUES: usize = 8_000_000; +const MAX_BYTES: usize = 512 * 1024 * 1024; +const MAX_DEPTH: usize = 128; + +fn text(s: &str) -> Value { + Value::Text(s.into()) +} +fn list(v: &Value) -> Result<&[Value], String> { + if let Value::List(v) = v { + Ok(v) + } else { + Err("snapshot DAG: expected list".into()) + } +} +fn string(v: &Value) -> Result<&str, String> { + if let Value::Text(v) = v { + Ok(v) + } else { + Err("snapshot DAG: expected text".into()) + } +} +fn field<'a>(v: &'a Value, key: &str) -> Result<&'a Value, String> { + if let Value::Map(fields) = v { + fields + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v) + .ok_or_else(|| format!("snapshot DAG: missing {key}")) + } else { + Err("snapshot DAG: expected object".into()) + } +} +fn wire(v: &Value) -> Result { + serde_json::to_string(&value_to_json(v)?).map_err(|e| e.to_string()) +} +fn checksum(root: &Value, nodes: &Value) -> Result { + Ok(format!( + "{:x}", + Sha256::digest(wire(&Value::List(vec![root.clone(), nodes.clone()]))?.as_bytes()) + )) +} +pub fn is_packed(v: &Value) -> bool { + field(v, "format").ok() == Some(&text(FORMAT)) +} + +pub fn pack(snapshot: &Value) -> Result { + fn intern( + v: &Value, + nodes: &mut Vec, + seen: &mut HashMap, + depth: usize, + ) -> Result { + if depth > MAX_DEPTH { + return Err("snapshot DAG: depth budget exceeded".into()); + } + let row = match v { + Value::None => vec![text("null")], + Value::Bool(v) => vec![text("bool"), Value::Bool(*v)], + Value::Int(v) => vec![text("int"), text(&v.to_string())], + Value::Num(v) => vec![text("float"), text(&format!("{:016x}", v.to_bits()))], + Value::Text(v) => vec![text("text"), text(v)], + Value::List(values) => { + let refs = values + .iter() + .map(|v| intern(v, nodes, seen, depth + 1).map(|i| Value::Int(i as i64))) + .collect::, _>>()?; + vec![text("list"), Value::List(refs)] + } + Value::Map(fields) => { + let refs = fields + .iter() + .map(|(k, v)| { + intern(v, nodes, seen, depth + 1) + .map(|i| Value::List(vec![text(k), Value::Int(i as i64)])) + }) + .collect::, _>>()?; + vec![text("map"), Value::List(refs)] + } + _ => return Err("snapshot DAG: value outside V_safe".into()), + }; + let row = Value::List(row); + let key = wire(&row)?; + if let Some(index) = seen.get(&key) { + return Ok(*index); + } + if nodes.len() >= MAX_NODES { + return Err("snapshot DAG: node budget exceeded".into()); + } + let index = nodes.len(); + nodes.push(row); + seen.insert(key, index); + Ok(index) + } + let mut nodes = Vec::new(); + let root = intern(snapshot, &mut nodes, &mut HashMap::new(), 0)?; + admit(snapshot)?; + let nodes = Value::List(nodes); + let root = Value::Int(root as i64); + let hash = checksum(&root, &nodes)?; + let envelope = Value::Map(vec![ + ("format".into(), text(FORMAT)), + ("version".into(), Value::Int(1)), + ("root".into(), root), + ("nodes".into(), nodes), + ("sha256".into(), text(&hash)), + ]); + // Apply exactly the decoder's resource limits to locally produced envelopes. + validate(&envelope)?; + Ok(envelope) +} + +enum Node { + Atom(Value), + List(Vec), + Map(Vec<(String, usize)>), +} +impl Node { + fn refs(&self) -> Vec { + match self { + Self::Atom(_) => Vec::new(), + Self::List(v) => v.clone(), + Self::Map(v) => v.iter().map(|(_, i)| *i).collect(), + } + } +} +fn index(v: &Value, before: usize) -> Result { + match v { + Value::Int(i) if *i >= 0 && (*i as u64) < before as u64 => Ok(*i as usize), + _ => Err("snapshot DAG: forward/cyclic/out-of-range reference".into()), + } +} +fn validate(envelope: &Value) -> Result<(Vec, usize), String> { + let Value::Map(fields) = envelope else { + return Err("snapshot DAG: expected envelope".into()); + }; + let names: HashSet<_> = fields.iter().map(|(k, _)| k.as_str()).collect(); + if fields.len() != 5 + || names != HashSet::from(["format", "version", "root", "nodes", "sha256"]) + || string(field(envelope, "format")?)? != FORMAT + || field(envelope, "version")? != &Value::Int(1) + { + return Err("snapshot DAG: invalid envelope schema".into()); + } + let rows = list(field(envelope, "nodes")?)?; + if rows.is_empty() || rows.len() > MAX_NODES { + return Err("snapshot DAG: node budget exceeded".into()); + } + let root = index(field(envelope, "root")?, rows.len())?; + if root + 1 != rows.len() { + return Err("snapshot DAG: root must be last".into()); + } + let expected = string(field(envelope, "sha256")?)?; + if expected != checksum(field(envelope, "root")?, field(envelope, "nodes")?)? { + return Err("snapshot DAG: checksum differs".into()); + } + let mut nodes = Vec::new(); + let mut costs: Vec<(usize, usize, usize)> = Vec::new(); + let mut unique = HashSet::new(); + for (i, raw) in rows.iter().enumerate() { + if !unique.insert(wire(raw)?) { + return Err("snapshot DAG: duplicate dictionary node".into()); + } + let row = list(raw)?; + let tag = string(row.first().ok_or("snapshot DAG: empty node")?)?; + let node = match (tag, row) { + ("null", [_]) => Node::Atom(Value::None), + ("bool", [_, Value::Bool(v)]) => Node::Atom(Value::Bool(*v)), + ("text", [_, Value::Text(v)]) => Node::Atom(text(v)), + ("int", [_, Value::Text(v)]) => { + let n = v + .parse::() + .map_err(|_| "snapshot DAG: invalid integer")?; + if n.to_string() != *v { + return Err("snapshot DAG: noncanonical integer".into()); + } + Node::Atom(Value::Int(n)) + } + ("float", [_, Value::Text(v)]) if v.len() == 16 => { + let bits = + u64::from_str_radix(v, 16).map_err(|_| "snapshot DAG: invalid float bits")?; + if format!("{bits:016x}") != *v { + return Err("snapshot DAG: noncanonical float bits".into()); + } + Node::Atom(Value::Num(f64::from_bits(bits))) + } + ("list", [_, refs]) => Node::List( + list(refs)? + .iter() + .map(|v| index(v, i)) + .collect::>()?, + ), + ("map", [_, refs]) => { + let mut keys = HashSet::new(); + let mut fields = Vec::new(); + for pair in list(refs)? { + let [key, value] = list(pair)? else { + return Err("snapshot DAG: invalid map pair".into()); + }; + let key = string(key)?; + if !keys.insert(key) { + return Err("snapshot DAG: duplicate map key".into()); + } + fields.push((key.into(), index(value, i)?)); + } + Node::Map(fields) + } + _ => return Err("snapshot DAG: invalid node tag/payload".into()), + }; + let mut values = 1usize; + let mut bytes = match &node { + Node::Atom(v) => { + admit(v)?; + match v { + Value::Text(s) => s + .len() + .checked_mul(6) + .and_then(|n| n.checked_add(2)) + .ok_or("snapshot DAG: size overflow")?, + _ => 32, + } + } + Node::Map(v) => v + .iter() + .try_fold(2usize, |n, (k, _)| { + n.checked_add(k.len().checked_mul(6)?.checked_add(4)?) + }) + .ok_or("snapshot DAG: size overflow")?, + Node::List(_) => 2, + }; + let mut depth = 1; + for reference in node.refs() { + let (count, size, height) = costs[reference]; + values = values + .checked_add(count) + .ok_or("snapshot DAG: expansion overflow")?; + bytes = bytes + .checked_add(size) + .and_then(|n| n.checked_add(1)) + .ok_or("snapshot DAG: expansion overflow")?; + depth = depth.max(height + 1); + } + if values > MAX_VALUES || bytes > MAX_BYTES || depth > MAX_DEPTH { + return Err("snapshot DAG: expansion budget exceeded".into()); + } + costs.push((values, bytes, depth)); + nodes.push(node); + } + let mut reachable = HashSet::new(); + let mut todo = vec![root]; + while let Some(i) = todo.pop() { + if reachable.insert(i) { + todo.extend(nodes[i].refs()); + } + } + if reachable.len() != nodes.len() { + return Err("snapshot DAG: unreachable dictionary node".into()); + } + Ok((nodes, root)) +} + +pub fn unpack(envelope: &Value) -> Result { + fn expand(nodes: &[Node], i: usize) -> Value { + match &nodes[i] { + Node::Atom(v) => v.clone(), + Node::List(refs) => Value::List(refs.iter().map(|i| expand(nodes, *i)).collect()), + Node::Map(fields) => Value::Map( + fields + .iter() + .map(|(k, i)| (k.clone(), expand(nodes, *i))) + .collect(), + ), + } + } + let (nodes, root) = validate(envelope)?; + let value = expand(&nodes, root); + admit(&value)?; + Ok(value) +} + +#[cfg(test)] +mod tests { + use super::*; + fn envelope(rows: Vec) -> Value { + let root = Value::Int(rows.len() as i64 - 1); + let nodes = Value::List(rows); + let hash = checksum(&root, &nodes).unwrap(); + Value::Map(vec![ + ("format".into(), text(FORMAT)), + ("version".into(), Value::Int(1)), + ("root".into(), root), + ("nodes".into(), nodes), + ("sha256".into(), text(&hash)), + ]) + } + fn row(tag: &str, value: Value) -> Value { + Value::List(vec![text(tag), value]) + } + #[test] + fn typed_roundtrip_preserves_order_float_bits_and_repeated_values() { + let shared = Value::Map(vec![ + ( + "z".into(), + Value::List(vec![ + Value::None, + Value::Bool(true), + Value::Int(1), + Value::Num(-0.0), + Value::Num(1.25), + ]), + ), + ("汉字".into(), text("🧪")), + ]); + let input = Value::List(vec![shared; 128]); + let packed = pack(&input).unwrap(); + let decoded_wire = + crate::host::json::parse(&crate::host::json::stringify(&packed, None).unwrap()) + .unwrap(); + let expanded = unpack(&decoded_wire).unwrap(); + assert_eq!(input, expanded); + assert_eq!(wire(&input).unwrap(), wire(&expanded).unwrap()); + let Value::List(ref xs) = expanded else { + panic!() + }; + let Value::Map(ref fields) = xs[0] else { + panic!() + }; + assert_eq!(fields[0].0, "z"); + let Value::List(ref values) = fields[0].1 else { + panic!() + }; + let Value::Num(number) = values[3] else { + panic!() + }; + assert_eq!(number.to_bits(), (-0.0f64).to_bits()); + assert!(list(field(&packed, "nodes").unwrap()).unwrap().len() < 20); + assert!(wire(&packed).unwrap().len() < wire(&input).unwrap().len()); + } + #[test] + fn refuses_cycles_amplification_unreachable_duplicates_and_unsafe_atoms() { + let bad = [ + envelope(vec![row("list", Value::List(vec![Value::Int(0)]))]), + envelope(vec![row("text", text("unused")), row("text", text("root"))]), + envelope(vec![row("text", text("same")), row("text", text("same"))]), + envelope(vec![row("int", text("9007199254740992"))]), + envelope(vec![row("float", text("7ff8000000000000"))]), + envelope(vec![row("int", text("00"))]), + ]; + for value in bad { + assert!(unpack(&value).is_err()); + } + let mut rows = vec![row("text", text("x"))]; + for n in 0..40 { + rows.push(row("list", Value::List(vec![Value::Int(n), Value::Int(n)]))); + } + let error = unpack(&envelope(rows)).unwrap_err(); + assert!(error.contains("budget"), "{error}"); + let mut hash = pack(&Value::Int(1)).unwrap(); + if let Value::Map(ref mut fields) = hash { + fields.iter_mut().find(|(k, _)| k == "sha256").unwrap().1 = text("bad"); + } + assert!(unpack(&hash).unwrap_err().contains("checksum")); + } + #[test] + fn sharing_does_not_admit_ragged_columns_or_duplicate_keys() { + let ragged = Value::Map(vec![ + ("a".into(), Value::List(vec![Value::Int(1)])), + ("b".into(), Value::List(Vec::new())), + ]); + assert!(pack(&ragged).is_err()); + let duplicate = Value::Map(vec![ + ("a".into(), Value::Int(1)), + ("a".into(), Value::Int(2)), + ]); + assert!(pack(&duplicate).is_err()); + let value = envelope(vec![ + row("int", text("1")), + row("list", Value::List(vec![Value::Int(0)])), + row("list", Value::List(Vec::new())), + row( + "map", + Value::List(vec![ + Value::List(vec![text("a"), Value::Int(1)]), + Value::List(vec![text("b"), Value::Int(2)]), + ]), + ), + ]); + assert!(unpack(&value).is_err()); + } +} diff --git a/src/host/foreign_doc/replay.rs b/src/host/foreign_doc/replay.rs new file mode 100644 index 0000000..d97e78a --- /dev/null +++ b/src/host/foreign_doc/replay.rs @@ -0,0 +1,2581 @@ +//! Complete interactive operation history. Wire values are tagged so invalid +//! boundary attempts can be replayed without serializing an opaque capability. +use super::*; + +fn map(fields: Vec<(&str, Value)>) -> Value { + Value::Map(fields.into_iter().map(|(k, v)| (k.into(), v)).collect()) +} +fn object(v: &Value) -> Result<&[(String, Value)], String> { + let Value::Map(xs) = v else { + return Err("history: expected record".into()); + }; + let mut seen = HashSet::new(); + if xs.iter().any(|(k, _)| !seen.insert(k)) { + return Err("history: duplicate field".into()); + } + Ok(xs) +} +fn field<'a>(v: &'a Value, key: &str) -> Result<&'a Value, String> { + object(v)? + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v) + .ok_or_else(|| format!("history: missing {key}")) +} +fn text<'a>(v: &'a Value, key: &str) -> Result<&'a str, String> { + match field(v, key)? { + Value::Text(s) => Ok(s), + _ => Err(format!("history: {key} must be text")), + } +} +fn list<'a>(v: &'a Value, key: &str) -> Result<&'a [Value], String> { + match field(v, key)? { + Value::List(xs) => Ok(xs), + _ => Err(format!("history: {key} must be a list")), + } +} +fn boolean(v: &Value, key: &str) -> Result { + match field(v, key)? { + Value::Bool(x) => Ok(*x), + _ => Err(format!("history: {key} must be boolean")), + } +} +fn optional_text<'a>(v: &'a Value, key: &str) -> Result, String> { + match field(v, key)? { + Value::None => Ok(None), + Value::Text(x) => Ok(Some(x)), + _ => Err(format!("history: {key} must be text or None")), + } +} +fn exact(v: &Value, keys: &[&str]) -> Result<(), String> { + let xs = object(v)?; + if xs.len() != keys.len() || xs.iter().any(|(k, _)| !keys.contains(&k.as_str())) { + return Err("history: unexpected or missing fields".into()); + } + Ok(()) +} +// Internal metadata is not itself a foreign column table. Compare it without +// applying a payload-domain rule to a dictionary of separate host bindings. +fn control_canonical(v: &Value) -> Value { + match v { + Value::Map(fields) => { + let mut fields: Vec<_> = fields + .iter() + .map(|(k, v)| (k.clone(), control_canonical(v))) + .collect(); + fields.sort_by(|a, b| a.0.cmp(&b.0)); + Value::Map(fields) + } + Value::List(values) => Value::List(values.iter().map(control_canonical).collect()), + Value::Num(n) if *n == 0.0 => Value::Num(0.0), + value => value.clone(), + } +} +fn same(a: &Value, b: &Value) -> Result { + Ok(encode(&control_canonical(a)) == encode(&control_canonical(b))) +} +pub(super) fn validate_control(v: &Value) -> Result<(), String> { + match v { + Value::Map(fields) => { + object(v)?; + for (_, v) in fields { + validate_control(v)?; + } + Ok(()) + } + Value::List(values) => { + for v in values { + validate_control(v)?; + } + Ok(()) + } + Value::None | Value::Bool(_) | Value::Int(_) | Value::Num(_) | Value::Text(_) => { + super::super::oracle::admit(v) + } + _ => Err("history: control result contains a non-data value".into()), + } +} + +fn encode(v: &Value) -> Value { + let (tag, payload) = match v { + Value::None => ("null", Value::None), + Value::Bool(b) => ("bool", Value::Bool(*b)), + Value::Int(n) => ("int", Value::Text(n.to_string())), + Value::Num(n) => ("num", Value::Text(format!("{:016x}", n.to_bits()))), + Value::Text(s) => ("text", Value::Text(s.clone())), + Value::List(xs) => ("list", Value::List(xs.iter().map(encode).collect())), + Value::Map(xs) => ( + "map", + Value::List( + xs.iter() + .map(|(k, v)| Value::List(vec![Value::Text(k.clone()), encode(v)])) + .collect(), + ), + ), + Value::Code(c) => ( + "code", + map(vec![ + ("lang", Value::Text(c.lang.clone())), + ("source", Value::Text(c.source.clone())), + ]), + ), + Value::Handle(_) => ("invalid-handle", Value::None), + Value::Secret(_) => ("invalid-secret", Value::None), + Value::Formula(_) => ("invalid-formula", Value::None), + }; + Value::List(vec![Value::Text(tag.into()), payload]) +} +fn decode(v: &Value) -> Result { + let Value::List(pair) = v else { + return Err("history: expected tagged value".into()); + }; + if pair.len() != 2 { + return Err("history: invalid tagged value".into()); + } + let Value::Text(tag) = &pair[0] else { + return Err("history: missing value tag".into()); + }; + let p = &pair[1]; + Ok(match (tag.as_str(), p) { + ("null", Value::None) => Value::None, + ("bool", Value::Bool(b)) => Value::Bool(*b), + ("text", Value::Text(s)) => Value::Text(s.clone()), + ("int", Value::Text(s)) => Value::Int(s.parse().map_err(|_| "history: invalid integer")?), + ("num", Value::Text(s)) => Value::Num(f64::from_bits( + u64::from_str_radix(s, 16).map_err(|_| "history: invalid float bits")?, + )), + ("list", Value::List(xs)) => Value::List(xs.iter().map(decode).collect::>()?), + ("map", Value::List(xs)) => { + let mut entries = Vec::new(); + for item in xs { + let Value::List(kv) = item else { + return Err("history: invalid map entry".into()); + }; + if kv.len() != 2 { + return Err("history: invalid map entry".into()); + } + let Value::Text(key) = &kv[0] else { + return Err("history: invalid map key".into()); + }; + entries.push((key.clone(), decode(&kv[1])?)); + } + Value::Map(entries) + } + ("code", _) => { + exact(p, &["lang", "source"])?; + Value::Code(crate::value::CodeBlock { + lang: text(p, "lang")?.into(), + source: text(p, "source")?.into(), + }) + } + ("invalid-handle", Value::None) => Value::Handle("replay-invalid-payload".into()), + ("invalid-secret", Value::None) => Value::Secret(String::new()), + ("invalid-formula", Value::None) => Value::Formula(crate::formula::Expr::Num(0.0)), + _ => return Err("history: unknown tag or incorrect payload type".into()), + }) +} + +#[derive(Clone, Debug)] +pub(super) struct HistoryEntry { + controls: Vec, + kind: String, + args: Value, + outcome: Value, + protocol: Vec, + observations: Vec, + cells: Vec, + state: Value, +} + +pub(super) struct ReplayBoundary { + cells: std::collections::VecDeque, + records: Option>, + error: Option, +} +impl HistoryEntry { + fn wire(&self, index: usize) -> Value { + let mut fields = vec![ + ("index", Value::Int(index as i64)), + ("kind", Value::Text(self.kind.clone())), + ("args", encode(&self.args)), + ("outcome", encode(&self.outcome)), + ("protocol", Value::List(self.protocol.clone())), + ( + "observations", + encode(&Value::List(self.observations.clone())), + ), + ("cells", Value::List(self.cells.clone())), + ("state", encode(&self.state)), + ]; + if !self.controls.is_empty() { + fields.push(( + "controls", + Value::List(self.controls.iter().map(|event| event.value()).collect()), + )); + } + map(fields) + } + fn parse(v: &Value, index: usize) -> Result { + let mut schema = vec![ + "index", + "kind", + "args", + "outcome", + "protocol", + "observations", + "cells", + "state", + ]; + let has_controls = object(v)?.iter().any(|(key, _)| key == "controls"); + if has_controls { + schema.push("controls"); + } + exact(v, &schema)?; + let mut controls = Vec::new(); + if has_controls { + let mut previous = 0; + for row in list(v, "controls")? { + exact(row, &["after_us", "results"])?; + let Value::Int(after) = field(row, "after_us")? else { + return Err("history: control time must be integer".into()); + }; + if *after < 0 || (*after as u64) < previous { + return Err("history: invalid control ordering".into()); + } + previous = *after as u64; + let mut names = HashSet::new(); + let mut results = Vec::new(); + for result in list(row, "results")? { + exact(result, &["kernel", "accepted", "active", "error"])?; + let name = text(result, "kernel")?; + if name.is_empty() || !names.insert(name) { + return Err("history: duplicate/empty control kernel".into()); + } + let accepted = boolean(result, "accepted")?; + let active = boolean(result, "active")?; + let error = text(result, "error")?; + if (!accepted && active) + || (accepted && !error.is_empty()) + || (!accepted && error.is_empty()) + { + return Err("history: inconsistent interrupt receipt".into()); + } + results.push(( + name.into(), + if accepted { + Ok(active) + } else { + Err(error.into()) + }, + )); + } + controls.push(super::control::ControlEvent { + after_us: *after as u64, + results, + }); + } + } + if field(v, "index")? != &Value::Int(index as i64) { + return Err(format!("history: action {index} index differs")); + } + let observations = decode(field(v, "observations")?)?; + let Value::List(observations) = observations else { + return Err("history: observations must be a list".into()); + }; + for value in &observations { + super::super::oracle::admit(value)?; + } + Ok(Self { + controls, + kind: text(v, "kind")?.into(), + args: decode(field(v, "args")?)?, + outcome: decode(field(v, "outcome")?)?, + protocol: list(v, "protocol")?.to_vec(), + observations, + cells: list(v, "cells")?.to_vec(), + state: decode(field(v, "state")?)?, + }) + } +} + +impl DocumentSession { + pub(super) fn initialize_values(&mut self) -> Result<(), String> { + let mut initialized = HashSet::new(); + for (kernel, _, _) in self.initial_values.clone() { + if initialized.insert(kernel.clone()) { + self.ensure_kernel_session(&kernel)?; + } + } + Ok(()) + } + pub(super) fn plan_image(&self) -> Value { + map(vec![ + ( + "source", + Value::Text(self.host.entry_source.clone().unwrap_or_default()), + ), + ("source_hash", Value::Text(self.source_hash.clone())), + ("persistent", Value::Bool(self.persistent)), + ( + "kernels", + Value::List( + self.kernels + .iter() + .map(|k| { + map(vec![ + ("name", Value::Text(k.name.clone())), + ("lang", Value::Text(k.lang.clone())), + ("cmd", k.cmd.clone().map(Value::Text).unwrap_or(Value::None)), + ("implicit", Value::Bool(k.implicit)), + ]) + }) + .collect(), + ), + ), + ( + "cells", + Value::List( + self.cells + .iter() + .map(|c| { + map(vec![ + ("name", Value::Text(c.name.clone())), + ("lang", Value::Text(c.lang.clone())), + ("source", Value::Text(c.source.clone())), + ("source_hash", Value::Text(c.source_hash.clone())), + ("kernel", Value::Text(c.kernel.clone())), + ("listed", Value::Bool(c.listed)), + ("stdin", Value::Text(c.stdin.clone())), + ]) + }) + .collect(), + ), + ), + ( + "order", + Value::List(self.order.iter().cloned().map(Value::Text).collect()), + ), + ( + "initial_values", + Value::List( + self.initial_values + .iter() + .map(|(k, n, v)| { + map(vec![ + ("kernel", Value::Text(k.clone())), + ("name", Value::Text(n.clone())), + ("value", v.clone()), + ]) + }) + .collect(), + ), + ), + ]) + } + fn from_image( + path: &Path, + root: &Path, + image: &Value, + initialize: bool, + ) -> Result { + exact( + image, + &[ + "source", + "source_hash", + "persistent", + "kernels", + "cells", + "order", + "initial_values", + ], + )?; + let source = text(image, "source")?; + let source_hash = hash_source(source); + if text(image, "source_hash")? != source_hash { + return Err("history: source hash differs".into()); + } + let ctx = crate::binding::BindingContext::from_binds( + &[], + crate::binding::cwd_for_path(Some(path)), + ); + let module = crate::load::load_module_with_source(path, source, &ctx) + .map_err(|e| format!("history source: {e}"))?; + let entry = module + .functions + .iter() + .find(|f| f.name == "main" && f.is_object()) + .or_else(|| { + let objs: Vec<_> = module.functions.iter().filter(|f| f.is_object()).collect(); + if objs.len() == 1 { + Some(objs[0]) + } else { + None + } + }) + .ok_or("history: missing entry")?; + let mut parsed = Vec::new(); + collect_cells(&entry.body, &mut parsed, &mut HashSet::new())?; + let persistent = boolean(image, "persistent")?; + let mut kernels = Vec::new(); + let mut names = HashSet::new(); + for row in list(image, "kernels")? { + exact(row, &["name", "lang", "cmd", "implicit"])?; + let name = text(row, "name")?.to_string(); + if !names.insert(name.clone()) { + return Err("history: duplicate kernel".into()); + } + kernels.push(KernelDecl { + name, + lang: text(row, "lang")?.into(), + cmd: optional_text(row, "cmd")?.map(str::to_string), + implicit: boolean(row, "implicit")?, + }); + } + let mut cells = Vec::new(); + let mut cellnames = HashSet::new(); + for row in list(image, "cells")? { + exact( + row, + &[ + "name", + "lang", + "source", + "source_hash", + "kernel", + "listed", + "stdin", + ], + )?; + let name = text(row, "name")?; + if !cellnames.insert(name.to_string()) { + return Err("history: duplicate cell".into()); + } + let mut cell = parsed + .iter() + .find(|c| c.name == name) + .cloned() + .ok_or("history: cell absent from source")?; + if cell.source != text(row, "source")? + || cell.source_hash != text(row, "source_hash")? + || cell.lang != text(row, "lang")? + { + return Err("history: cell source differs".into()); + } + cell.kernel = text(row, "kernel")?.into(); + cell.listed = boolean(row, "listed")?; + cell.stdin = text(row, "stdin")?.into(); + if persistent { + if cell.listed + && !kernels.iter().any(|k| { + k.name == cell.kernel + && registry::canonical(&k.lang) == registry::canonical(&cell.lang) + }) + { + return Err("history: invalid cell routing".into()); + } + } else if cell.kernel != "one-shot" { + return Err("history: invalid one-shot routing".into()); + } + if !cell.listed { + cell.status = CellStatus::Unlisted; + } + cells.push(cell); + } + if cells.len() != parsed.len() { + return Err("history: incomplete source cells".into()); + } + let mut order = Vec::new(); + let mut seen = HashSet::new(); + for item in list(image, "order")? { + let Value::Text(name) = item else { + return Err("history: order must contain names".into()); + }; + if !seen.insert(name.clone()) || !cells.iter().any(|c| &c.name == name && c.listed) { + return Err("history: invalid order".into()); + } + order.push(name.clone()); + } + if cells.iter().filter(|c| c.listed).count() != order.len() { + return Err("history: incomplete order".into()); + } + let mut initial_values = Vec::new(); + for row in list(image, "initial_values")? { + exact(row, &["kernel", "name", "value"])?; + let kernel = text(row, "kernel")?; + let name = text(row, "name")?; + let value = field(row, "value")?.clone(); + if !kernels.iter().any(|k| k.name == kernel) || !foreign_session::valid_name(name) { + return Err("history: invalid initial binding".into()); + } + super::super::oracle::admit(&value)?; + initial_values.push((kernel.into(), name.into(), value)); + } + let mut host = HostContext::default(); + host.cwd = path.parent().unwrap_or_else(|| Path::new(".")).into(); + host.fs_root = Some(root.into()); + host.entry_path = Some(path.into()); + host.entry_source = Some(source.into()); + host.entry_metadata = module.metadata; + let mut session = Self { + control: super::DocumentControl::default(), + replay_controls: Vec::new(), + replay_boundary: None, + source_hash, + replay_fresh: true, + initial_values, + initial_image: image.clone(), + action_history: Vec::new(), + action_depth: 0, + path: path.into(), + root: root.into(), + persistent, + cells, + kernels, + order, + journal: Vec::new(), + oracle_journal: Vec::new(), + value_store: HashMap::new(), + cursor: 0, + generation: 1, + opened_ms: now_ms(), + host, + kernel_sessions: HashMap::new(), + seq: 0, + }; + if initialize { + session.initialize_values()?; + session.replay_fresh = true; + } + Ok(session) + } + fn history_state(&self) -> Value { + map(vec![ + ("plan", self.plan_image()), + ("cursor", Value::Int(self.cursor as i64)), + ("generation", Value::Int(self.generation as i64)), + ( + "live_kernels", + Value::List({ + let mut names: Vec<_> = self + .kernel_sessions + .iter() + .filter(|(_, id)| self.host.foreign_sessions.contains_key(*id)) + .map(|(name, _)| t(name)) + .collect(); + names.sort_by_key(|v| v.as_display()); + names + }), + ), + ( + "values", + Value::List({ + let mut bindings: Vec<_> = self.value_store.iter().collect(); + bindings.sort_by_key(|(name, _)| *name); + bindings + .into_iter() + .map(|(name, value)| map(vec![("name", t(name)), ("value", value.clone())])) + .collect() + }), + ), + ( + "cells", + Value::List( + self.cells + .iter() + .map(|c| { + map(vec![ + ("name", Value::Text(c.name.clone())), + ("runs", Value::Int(c.runs as i64)), + ("status", Value::Text(c.status.as_str().into())), + ("ok", Value::Bool(c.last_ok)), + ("stdout", Value::Text(c.last_stdout.clone())), + ("result", c.last_result.clone()), + ]) + }) + .collect(), + ), + ), + ]) + } +} + +fn t(s: &str) -> Value { + Value::Text(s.into()) +} +fn ot(s: Option<&str>) -> Value { + s.map(t).unwrap_or(Value::None) +} +impl DocumentSession { + pub(super) fn check_replay_cell_boundary(&mut self) { + let Some(boundary) = &mut self.replay_boundary else { + return; + }; + let actual = self + .oracle_journal + .last() + .expect("record just appended") + .to_value(); + let expected = boundary.cells.pop_front(); + if !expected + .as_ref() + .is_some_and(|expected| same(&actual, expected).unwrap_or(false)) + { + boundary.error = + Some("replay: cell observation differs at its execution boundary".into()); + } + } + + pub(super) fn replay_batch_can_continue(&mut self) -> bool { + if self.host.foreign_replay_divergence.is_some() { + return false; + } + let Some(boundary) = &mut self.replay_boundary else { + return true; + }; + if boundary.error.is_some() { + return false; + } + if boundary + .records + .as_ref() + .is_some_and(|records| records.is_empty()) + { + boundary.error = Some("replay: unexpected extra batch unit".into()); + return false; + } + true + } + + pub(super) fn check_replay_batch_record(&mut self, record: &Value, seq_before: u64) { + let Some(boundary) = &mut self.replay_boundary else { + return; + }; + if let Some(records) = &mut boundary.records { + let actual = eval_result(record); + let expected = records.pop_front(); + if !matches!((actual, expected), (Ok(actual), Some(expected)) if same(&actual, &expected).unwrap_or(false)) + { + boundary.error = + Some("replay: batch result differs at its execution boundary".into()); + } + } + // Run-all's order is unique (validated in both plan readers). A refused + // unit must not silently consume an expected admitted observation. + if self.seq == seq_before { + if let (Ok(name), Some(expected)) = (text(record, "cell"), boundary.cells.front()) { + if text(expected, "cell").ok() == Some(name) { + boundary.error = + Some("replay: missing cell observation at execution boundary".into()); + } + } + } + } + + pub fn run_cell_with_stdin( + &mut self, + cell_name: &str, + kernel_override: Option<&str>, + stdin: Option<&str>, + ) -> Result { + self.tracked( + "cell", + map(vec![ + ("cell", t(cell_name)), + ("kernel", ot(kernel_override)), + ("stdin", ot(stdin)), + ]), + |session| session.run_cell_with_stdin_inner(cell_name, kernel_override, stdin), + ) + } + pub fn step_with_stdin(&mut self, stdin: Option<&str>) -> Result { + self.tracked("step", map(vec![("stdin", ot(stdin))]), |session| { + session.step_with_stdin_inner(stdin) + }) + } + pub fn run_all_with_inputs( + &mut self, + restart: bool, + stdin: Option<&str>, + inputs: Option<&HashMap>, + ) -> Result { + self.tracked( + "run_all", + map(vec![ + ("restart", Value::Bool(restart)), + ("stdin", ot(stdin)), + ( + "inputs", + inputs + .map(|m| Value::Map(m.iter().map(|(k, v)| (k.clone(), t(v))).collect())) + .unwrap_or(Value::None), + ), + ]), + |session| session.run_all_with_inputs_inner(restart, stdin, inputs), + ) + } + pub fn interrupt(&mut self) -> Result { + self.tracked("interrupt", map(vec![]), |session| { + session.interrupt_inner() + }) + } + pub fn restart_kernel(&mut self, name: &str) -> Result { + self.tracked("restart", map(vec![("kernel", t(name))]), |session| { + session.restart_kernel_inner(name) + }) + } + pub fn restart_all(&mut self) -> Result { + self.tracked("restart_all", map(vec![]), |session| { + session.restart_all_inner() + }) + } + pub fn assign_kernel(&mut self, cell: &str, kernel: &str) -> Result { + self.tracked( + "assign", + map(vec![("cell", t(cell)), ("kernel", t(kernel))]), + |session| session.assign_kernel_inner(cell, kernel), + ) + } + pub fn add_cell( + &mut self, + cell: &str, + lang: &str, + source: &str, + kernel: &str, + ) -> Result { + self.tracked( + "add", + map(vec![ + ("cell", t(cell)), + ("lang", t(lang)), + ("source", t(source)), + ("kernel", t(kernel)), + ]), + |session| session.add_cell_inner(cell, lang, source, kernel), + ) + } + pub fn remove_cell(&mut self, cell: &str) -> Result { + self.tracked("remove", map(vec![("cell", t(cell))]), |session| { + session.remove_cell_inner(cell) + }) + } + pub fn set_value(&mut self, kernel: &str, name: &str, value: Value) -> Result { + self.tracked( + "set", + map(vec![ + ("kernel", t(kernel)), + ("name", t(name)), + ("value", value.clone()), + ]), + |session| session.set_value_inner(kernel, name, value), + ) + } + pub fn push_value(&mut self, kernel: &str, name: &str, value: Value) -> Result { + self.tracked( + "push", + map(vec![ + ("kernel", t(kernel)), + ("name", t(name)), + ("value", value.clone()), + ]), + |session| session.push_value_inner(kernel, name, value), + ) + } + pub fn pull_value(&mut self, kernel: &str, name: &str) -> Result { + self.tracked( + "get", + map(vec![("kernel", t(kernel)), ("name", t(name))]), + |session| session.pull_value_inner(kernel, name), + ) + } + pub fn state_list(&mut self) -> Result { + self.tracked("state", map(vec![]), |session| session.state_list_inner()) + } + pub fn clear_cell(&mut self, cell: &str) -> Result { + self.tracked("clear_cell", map(vec![("cell", t(cell))]), |session| { + session.clear_cell_inner(cell) + }) + } + pub fn import_snapshot(&mut self, snapshot: &Value) -> Result { + self.tracked( + "import", + map(vec![("snapshot", snapshot.clone())]), + |session| session.import_snapshot_inner(snapshot), + ) + } + pub fn replay_snapshot(&mut self, snapshot: &Value) -> Result { + self.tracked( + "rerun", + map(vec![("snapshot", snapshot.clone())]), + |session| session.replay_snapshot_inner(snapshot), + ) + } + pub fn persist_cell(&mut self, cell: &str, key: &str) -> Result { + self.tracked( + "persist", + map(vec![("cell", t(cell)), ("key", t(key))]), + |session| session.persist_cell_inner(cell, key), + ) + } + pub(super) fn inspect_kernels(&mut self) -> Result { + let mut kernels = Vec::new(); + for decl in self.kernels.clone() { + 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::Handle(id.clone())) + { + for name in names { + if let Value::Text(n) = &name { + if let Ok(v) = foreign_session::get( + &mut self.host, + &Value::Handle(id.clone()), + &Value::Text(n.clone()), + ) { + values.push(Value::Map(vec![ + ("name".into(), Value::Text(n.clone())), + ("value".into(), v), + ])); + } + } + } + } + } + kernels.push(Value::Map(vec![ + ("name".into(), Value::Text(decl.name.clone())), + ("lang".into(), Value::Text(decl.lang.clone())), + ( + "cmd".into(), + Value::Text(decl.cmd.clone().unwrap_or_default()), + ), + ("values".into(), Value::List(values)), + ])); + } + Ok(Value::List(kernels)) + } + pub fn clear_outputs(&mut self) -> Value { + self.tracked("clear", map(vec![]), |s| Ok(s.clear_outputs_inner())) + .expect("clearing outputs cannot fail") + } + pub fn close(&mut self) { + let _ = self.tracked("close", map(vec![]), |s| { + s.close_inner(); + Ok(Value::None) + }); + } + pub fn reload(&mut self, root: &Path) -> Result<(), String> { + match std::fs::read_to_string(&self.path) { + Ok(source) => self + .tracked( + "reload", + map(vec![ + ("source", t(&source)), + ("image", Value::None), + ("input_error", Value::None), + ]), + |s| s.reload_source(&source, root).map(|_| Value::None), + ) + .map(|_| ()), + Err(error) => { + let error = format!("read {}: {error}", self.path.display()); + self.tracked( + "reload", + map(vec![ + ("source", Value::None), + ("image", Value::None), + ("input_error", t(&error)), + ]), + |_| Err(error), + ) + .map(|_| ()) + } + } + } +} + +fn eval_result(value: &Value) -> Result { + let xs = object(value)?; + if xs.iter().any(|(k, _)| k == "ok") { + let mut fields = vec![ + ("value", super::super::oracle::eval_observation(value)?), + ("ok", field(value, "ok")?.clone()), + ("code", field(value, "code")?.clone()), + ("stdout", field(value, "stdout")?.clone()), + ]; + for key in ["cursor", "generation"] { + if let Some((_, v)) = xs.iter().find(|(k, _)| k == key) { + fields.push((key, v.clone())); + } + } + Ok(map(fields)) + } else { + Ok(value.clone()) + } +} +fn outcome(kind: &str, result: &Result) -> Result { + Ok(match result { + Err(error) => map(vec![("ok", Value::Bool(false)), ("error", t(error))]), + Ok(value) => { + let projected = match kind { + "cell" | "step" => eval_result(value)?, + "run_all" | "rerun" => { + let mut fields = Vec::new(); + for key in ["ok", "ran", "failed", "replayed", "cancelled", "remaining"] { + if let Some((_, v)) = object(value)?.iter().find(|(k, _)| k == key) { + fields.push((key, v.clone())); + } + } + fields.push(( + "records", + Value::List( + list(value, "records")? + .iter() + .map(eval_result) + .collect::>()?, + ), + )); + map(fields) + } + "clear" | "clear_cell" => Value::None, + _ => value.clone(), + }; + map(vec![("ok", Value::Bool(true)), ("value", projected)]) + } + }) +} +impl DocumentSession { + pub(super) fn tracked( + &mut self, + kind: &str, + mut args: Value, + f: impl FnOnce(&mut Self) -> Result, + ) -> Result { + if self.action_depth != 0 { + return f(self); + } + let seq = self.seq; + let observed = self.host.oracle_observations.len(); + let requests = self.host.foreign_protocol_steps.len(); + let scheduler = self + .control + .begin(std::mem::take(&mut self.replay_controls)); + self.action_depth += 1; + let result = f(self); + self.action_depth -= 1; + let controls = self.control.finish(scheduler); + self.replay_fresh = false; + if kind == "reload" && result.is_ok() { + let Value::Map(fields) = &mut args else { + unreachable!() + }; + fields.iter_mut().find(|(k, _)| k == "image").unwrap().1 = self.plan_image(); + } + let entry = HistoryEntry { + controls, + kind: kind.into(), + args, + outcome: outcome(kind, &result)?, + protocol: self.host.foreign_protocol_steps[requests..] + .iter() + .map(|step| { + map(vec![ + ("session", t(&step.session)), + ("request", t(&step.request.to_string())), + ( + "response", + match &step.response { + Ok(value) => map(vec![ + ("ok", Value::Bool(true)), + ("json", t(&value.to_string())), + ]), + Err(error) => { + map(vec![("ok", Value::Bool(false)), ("error", t(error))]) + } + }, + ), + ]) + }) + .collect(), + observations: self.host.oracle_observations[observed..].to_vec(), + cells: self + .oracle_journal + .iter() + .filter(|j| j.seq > seq) + .map(OracleRecord::to_value) + .collect(), + state: self.history_state(), + }; + self.action_history.push(entry); + result + } + pub(super) fn action_history_json(&self) -> Value { + Value::List( + self.action_history + .iter() + .enumerate() + .map(|(i, e)| e.wire(i)) + .collect(), + ) + } + pub(super) fn complete_oracle_rows(&self) -> Vec { + self.action_history + .iter() + .flat_map(|e| e.cells.clone()) + .collect() + } + + fn check_action(entry: &HistoryEntry) -> Result<(), String> { + let a = &entry.args; + let schema: &[&str] = match entry.kind.as_str() { + "cell" => &["cell", "kernel", "stdin"], + "step" => &["stdin"], + "run_all" => &["restart", "stdin", "inputs"], + "restart" => &["kernel"], + "assign" => &["cell", "kernel"], + "add" => &["cell", "lang", "source", "kernel"], + "remove" | "clear_cell" => &["cell"], + "set" | "push" => &["kernel", "name", "value"], + "get" => &["kernel", "name"], + "import" | "rerun" => &["snapshot"], + "persist" => &["cell", "key"], + "reload" => &["source", "image", "input_error"], + "restart_all" | "interrupt" | "state" | "inspect" | "clear" | "close" => &[], + _ => return Err("history: unknown action kind".into()), + }; + exact(a, schema)?; + for key in schema { + match *key { + "value" | "snapshot" | "image" => {} + "restart" => { + boolean(a, key)?; + } + "input_error" => { + optional_text(a, key)?; + } + "source" if entry.kind == "reload" => { + optional_text(a, key)?; + } + "stdin" => { + optional_text(a, key)?; + } + "kernel" if entry.kind == "cell" => { + optional_text(a, key)?; + } + "inputs" => { + if field(a, key)? != &Value::None { + for (_, v) in object(field(a, key)?)? { + if !matches!(v, Value::Text(_)) { + return Err("history: inputs must map names to text".into()); + } + } + } + } + _ => { + text(a, key)?; + } + } + } + for step in &entry.protocol { + exact(step, &["session", "request", "response"])?; + text(step, "session")?; + serde_json::from_str::(text(step, "request")?) + .map_err(|_| "history: malformed protocol request")?; + let response = field(step, "response")?; + if boolean(response, "ok")? { + exact(response, &["ok", "json"])?; + serde_json::from_str::(text(response, "json")?) + .map_err(|_| "history: malformed protocol response")?; + } else { + exact(response, &["ok", "error"])?; + text(response, "error")?; + } + } + validate_control(&entry.outcome)?; + validate_control(&entry.state)?; + if boolean(&entry.outcome, "ok")? { + exact(&entry.outcome, &["ok", "value"])?; + } else { + exact(&entry.outcome, &["ok", "error"])?; + text(&entry.outcome, "error")?; + } + Ok(()) + } + fn execute_action(&mut self, entry: &HistoryEntry) -> Result { + let a = &entry.args; + match entry.kind.as_str() { + "cell" => self.run_cell_with_stdin( + text(a, "cell")?, + optional_text(a, "kernel")?, + optional_text(a, "stdin")?, + ), + "step" => self.step_with_stdin(optional_text(a, "stdin")?), + "run_all" => { + let inputs = match field(a, "inputs")? { + Value::None => None, + v => Some( + object(v)? + .iter() + .map(|(k, v)| match v { + Value::Text(s) => Ok((k.clone(), s.clone())), + _ => Err("history: input type".into()), + }) + .collect::, String>>()?, + ), + }; + self.run_all_with_inputs( + boolean(a, "restart")?, + optional_text(a, "stdin")?, + inputs.as_ref(), + ) + } + "restart" => self.restart_kernel(text(a, "kernel")?), + "restart_all" => self.restart_all(), + "assign" => self.assign_kernel(text(a, "cell")?, text(a, "kernel")?), + "add" => self.add_cell( + text(a, "cell")?, + text(a, "lang")?, + text(a, "source")?, + text(a, "kernel")?, + ), + "remove" => self.remove_cell(text(a, "cell")?), + "set" => self.set_value( + text(a, "kernel")?, + text(a, "name")?, + field(a, "value")?.clone(), + ), + "push" => self.push_value( + text(a, "kernel")?, + text(a, "name")?, + field(a, "value")?.clone(), + ), + "get" => self.pull_value(text(a, "kernel")?, text(a, "name")?), + "state" => self.state_list(), + "inspect" => self.tracked("inspect", a.clone(), |s| s.inspect_kernels()), + "import" => self.import_snapshot(field(a, "snapshot")?), + "rerun" => self.replay_snapshot(field(a, "snapshot")?), + "persist" => self.persist_cell(text(a, "cell")?, text(a, "key")?), + "clear_cell" => self.clear_cell(text(a, "cell")?), + "clear" => Ok(self.clear_outputs()), + "interrupt" => self.interrupt(), + "close" => { + self.close(); + Ok(Value::None) + } + "reload" => { + let args = a.clone(); + let image = field(a, "image")?.clone(); + let source = optional_text(a, "source")?.map(str::to_string); + let input_error = optional_text(a, "input_error")?.map(str::to_string); + let root = self.root.clone(); + self.tracked("reload", args, |s| { + if let Some(error) = input_error { + return Err(error); + } + if image == Value::None { + s.reload_source( + source.as_deref().ok_or("history: reload source missing")?, + &root, + )?; + } else { + let fresh = Self::from_image(&s.path, &root, &image, false)?; + s.install_reload(fresh)?; + } + Ok(Value::None) + }) + } + _ => Err("history: unsupported action".into()), + } + } + // Validate configuration transitions without querying any foreign kernel. + fn simulate_action(&mut self, entry: &HistoryEntry) -> Result<(), String> { + Self::check_action(entry)?; + let a = &entry.args; + let success = boolean(&entry.outcome, "ok")?; + match entry.kind.as_str() { + "reload" if success => { + let image = field(a, "image")?; + if optional_text(a, "input_error")?.is_some() + || Some(text(image, "source")?) != optional_text(a, "source")? + { + return Err("history: reload source differs".into()); + } + let fresh = Self::from_image(&self.path, &self.root, image, false)?; + self.install_reload(fresh)?; + } + "reload" => { + if let Some(error) = optional_text(a, "input_error")? { + if optional_text(a, "source")?.is_some() + || text(&entry.outcome, "error")? != error + { + return Err("history: invalid reload input failure".into()); + } + } else if optional_text(a, "source")?.is_none() { + return Err("history: missing reload source".into()); + } + if field(a, "image")? != &Value::None { + return Err("history: failed reload has a plan image".into()); + } + } + "assign" | "add" | "remove" => { + let result = match entry.kind.as_str() { + "assign" => self.assign_kernel_inner(text(a, "cell")?, text(a, "kernel")?), + "add" => self.add_cell_inner( + text(a, "cell")?, + text(a, "lang")?, + text(a, "source")?, + text(a, "kernel")?, + ), + _ => self.remove_cell_inner(text(a, "cell")?), + }; + if result.is_ok() != success { + return Err("history: impossible plan edit outcome".into()); + } + } + "cell" => { + if let Some(kernel) = optional_text(a, "kernel")? { + if let Some(ci) = self.cell_index(text(a, "cell")?) { + if self.kernel(kernel).map(|k| registry::canonical(&k.lang)) + == Some(registry::canonical(&self.cells[ci].lang)) + { + self.cells[ci].kernel = kernel.into(); + self.cells[ci].listed = true; + } + } + } + } + "step" if success => { + if self.cursor < self.order.len() { + self.cursor += 1; + } + } + "run_all" if success => { + let Value::Int(ran) = field(field(&entry.outcome, "value")?, "ran")? else { + return Err("history: invalid run count".into()); + }; + if *ran < 0 + || *ran as usize > self.order.len() + || (entry.controls.is_empty() && *ran as usize != self.order.len()) + { + return Err("history: impossible run count".into()); + } + self.cursor = *ran as usize; + } + "restart_all" | "clear" => { + self.cursor = 0; + } + "set" | "import" | "persist" if success => { + self.generation += 1; + } + _ => {} + } + match entry.kind.as_str() { + "cell" | "step" | "run_all" | "rerun" => { + if entry.observations.len() != entry.cells.len() { + return Err("history: cell/observation projection length differs".into()); + } + for (row, obs) in entry.cells.iter().zip(&entry.observations) { + if text(obs, "channel")? != "eval" + || !same(field(obs, "value")?, field(row, "value")?)? + { + return Err("history: cell observation value differs".into()); + } + } + if entry.kind == "cell" && entry.cells.len() != if success { 1 } else { 0 } { + return Err("history: impossible cell outcome".into()); + } + } + "get" => { + if !entry.cells.is_empty() + || entry.observations.len() != if success { 1 } else { 0 } + { + return Err("history: get observation count differs".into()); + } + if success + && (text(&entry.observations[0], "channel")? != "get" + || !same( + field(&entry.observations[0], "value")?, + field(&entry.outcome, "value")?, + )?) + { + return Err("history: get observation differs".into()); + } + } + _ => { + if !entry.cells.is_empty() || !entry.observations.is_empty() { + return Err("history: control action has foreign observations".into()); + } + } + } + if success && ["set", "push", "get"].contains(&entry.kind.as_str()) { + let kernel = text(a, "kernel")?; + let name = text(a, "name")?; + if self.kernel(kernel).is_none() || !foreign_session::valid_name(name) { + return Err("history: impossible binding operation".into()); + } + let value = if entry.kind == "get" { + field(&entry.outcome, "value")? + } else { + field(a, "value")? + }; + super::super::oracle::admit(value)?; + self.value_store.insert(name.to_string(), value.clone()); + } + if !same( + field(&self.history_state(), "values")?, + field(&entry.state, "values")?, + )? { + return Err("history: inconsistent host bindings".into()); + } + for row in &entry.cells { + let name = text(row, "cell")?; + let ci = self + .cell_index(name) + .ok_or("history: observation refers to missing cell")?; + let cell = &self.cells[ci]; + if text(row, "source_hash")? != cell.source_hash + || text(row, "lang")? != cell.lang + || text(row, "kernel")? != cell.kernel + { + return Err("history: observation source or route differs".into()); + } + text(row, "stdin")?; + text(row, "stdout")?; + boolean(row, "ok")?; + if !matches!(field(row, "code")?, Value::Int(_)) { + return Err("history: invalid observation code".into()); + } + field(row, "result")?; + super::super::oracle::admit(field(row, "value")?)?; + } + if !same(&self.plan_image(), field(&entry.state, "plan")?)? { + return Err("history: inconsistent plan transition".into()); + } + if field(&entry.state, "cursor")? != &Value::Int(self.cursor as i64) + || field(&entry.state, "generation")? != &Value::Int(self.generation as i64) + { + return Err("history: inconsistent cursor or generation".into()); + } + Ok(()) + } + pub fn replay_check(&mut self, snapshot: &Value) -> Result { + let expanded = if super::packing::is_packed(snapshot) { + Some(super::packing::unpack(snapshot)?) + } else { + None + }; + let snapshot = expanded.as_ref().unwrap_or(snapshot); + super::super::oracle::admit(snapshot)?; + let version = field(snapshot, "version")?; + if version != &Value::Int(4) && version != &Value::Int(5) { + return Err("replay: version 4/5 complete action history required".into()); + } + if !self.replay_fresh || self.seq != 0 || !self.action_history.is_empty() { + return Err("replay: fresh document session required".into()); + } + if text(snapshot, "document_hash")? != self.source_hash { + return Err("replay: document hash differs".into()); + } + let rows = list(snapshot, "actions")?; + if field(snapshot, "action_count")? != &Value::Int(rows.len() as i64) { + return Err("history: action count differs".into()); + } + let entries = rows + .iter() + .enumerate() + .map(|(i, row)| HistoryEntry::parse(row, i)) + .collect::, _>>()?; + if version == &Value::Int(4) + && rows.iter().any(|row| { + object(row).is_ok_and(|fields| fields.iter().any(|(key, _)| key == "controls")) + }) + { + return Err("history: concurrent controls require version 5".into()); + } + let image = field(snapshot, "initial_image")?; + let mut simulation = Self::from_image(&self.path, &self.root, image, false)?; + let mut cells = Vec::new(); + let mut observations = Vec::new(); + for entry in &entries { + simulation.simulate_action(entry)?; + cells.extend(entry.cells.clone()); + observations.extend(entry.observations.clone()); + } + for (i, row) in cells.iter().enumerate() { + if field(row, "seq")? != &Value::Int((i + 1) as i64) { + return Err("history: noncontiguous oracle sequence".into()); + } + } + if field(snapshot, "oracle_count")? != &Value::Int(cells.len() as i64) + || !same(&Value::List(cells), field(snapshot, "oracle_journal")?)? + { + return Err("history: oracle projection differs".into()); + } + let observed = Value::List(observations); + super::super::oracle::replay_check(&observed, &observed)?; + if !same(&observed, field(snapshot, "observation_journal")?)? { + return Err("history: document observation projection differs".into()); + } + if text(snapshot, "document_hash")? != simulation.source_hash + || boolean(snapshot, "persistent")? != simulation.persistent + { + return Err("history: final source or mode differs".into()); + } + let expected_order = + Value::List(simulation.order.iter().cloned().map(Value::Text).collect()); + if !same(field(snapshot, "order")?, &expected_order)? { + return Err("history: final plan order differs".into()); + } + let final_cells = list(snapshot, "cells")?; + if final_cells.len() != simulation.cells.len() { + return Err("history: final cell manifest differs".into()); + } + for (row, cell) in final_cells.iter().zip(&simulation.cells) { + for (key, expected) in [ + ("name", &cell.name), + ("source", &cell.source), + ("source_hash", &cell.source_hash), + ("kernel", &cell.kernel), + ("lang", &cell.lang), + ] { + if text(row, key)? != expected { + return Err("history: final cell identity differs".into()); + } + } + } + if field(snapshot, "generation")? != &Value::Int(simulation.generation as i64) { + return Err("history: final generation differs".into()); + } + let last = entries + .last() + .ok_or("history: snapshot lacks state inspection")?; + if last.kind != "inspect" + || !boolean(&last.outcome, "ok")? + || !same(field(&last.outcome, "value")?, field(snapshot, "kernels")?)? + { + return Err("history: final state inspection differs".into()); + } + // All validation above is pure with respect to external kernels. + let mut fresh = Self::from_image(&self.path, &self.root, image, true)?; + self.close_inner(); + std::mem::swap(self, &mut fresh); + let mut checks = Vec::new(); + let mut action_checks = Vec::new(); + let mut matched_observations = 0i64; + let mut mismatched = 0i64; + let mut skipped = 0i64; + for (index, expected) in entries.iter().enumerate() { + let protocol = expected + .protocol + .iter() + .map(|step| { + let response = field(step, "response")?; + Ok(super::foreign_session::ProtocolStep { + session: text(step, "session")?.into(), + request: serde_json::from_str(text(step, "request")?) + .map_err(|e| e.to_string())?, + response: if boolean(response, "ok")? { + Ok(serde_json::from_str(text(response, "json")?) + .map_err(|e| e.to_string())?) + } else { + Err(text(response, "error")?.into()) + }, + }) + }) + .collect::, String>>()?; + self.host.foreign_replay_expected = Some(protocol.into()); + self.host.foreign_replay_divergence = None; + let records = if ["run_all", "rerun"].contains(&expected.kind.as_str()) + && boolean(&expected.outcome, "ok")? + { + Some( + list(field(&expected.outcome, "value")?, "records")? + .to_vec() + .into(), + ) + } else if expected.kind == "run_all" { + Some(std::collections::VecDeque::new()) + } else { + None + }; + self.replay_boundary = Some(ReplayBoundary { + cells: expected.cells.clone().into(), + records, + error: None, + }); + self.replay_controls = expected.controls.clone(); + let _result = self.execute_action(expected); + let boundary = self.replay_boundary.take().unwrap(); + let mut guarded_error = self + .host + .foreign_replay_divergence + .take() + .or(boundary.error); + if self + .host + .foreign_replay_expected + .take() + .is_some_and(|cursor| cursor.finish().is_err()) + || !boundary.cells.is_empty() + || boundary.records.is_some_and(|records| !records.is_empty()) + { + guarded_error + .get_or_insert("replay: recorded execution prefix was not consumed".into()); + } + let actual = self + .action_history + .last() + .ok_or("history: action produced no record")?; + let mut bad_cells = 0i64; + for n in 0..expected.cells.len().max(actual.cells.len()) { + let exp = expected.cells.get(n); + let act = actual.cells.get(n); + if act.is_none() { + // The causal guard deliberately did not execute this suffix. + // It is unobserved, not a second observed mismatch. + skipped += 1; + checks.push(map(vec![ + ( + "cell", + t(exp + .map(|r| text(r, "cell")) + .transpose()? + .unwrap_or("missing")), + ), + ("action", Value::Int(index as i64)), + ("step", Value::Int(n as i64)), + ("match", Value::Bool(false)), + ("compared", Value::Bool(false)), + ("diff", t("not executed after replay divergence")), + ])); + continue; + } + let mut diff = String::new(); + match (exp, act) { + (Some(exp), Some(act)) => { + for key in [ + "seq", + "cell", + "kernel", + "lang", + "source_hash", + "stdin", + "ok", + "code", + "stdout", + "result", + "value", + ] { + if !same(field(exp, key)?, field(act, key)?)? { + diff = format!( + "action {index} observation {n} {key}: expected {}, got {}", + field(exp, key)?.as_display(), + field(act, key)?.as_display() + ); + break; + } + } + } + _ => diff = format!("action {index}: oracle observation length differs"), + } + let matched = diff.is_empty(); + if !matched { + bad_cells += 1; + } + let cell = exp + .or(act) + .map(|r| text(r, "cell")) + .transpose()? + .unwrap_or("missing"); + checks.push(map(vec![ + ("cell", t(cell)), + ("action", Value::Int(index as i64)), + ("step", Value::Int(n as i64)), + ("match", Value::Bool(matched)), + ("compared", Value::Bool(true)), + ("diff", t(&diff)), + ])); + } + let mut diff = String::new(); + for (key, a, b) in [ + ( + "controls", + Value::List(actual.controls.iter().map(|e| e.value()).collect()), + Value::List(expected.controls.iter().map(|e| e.value()).collect()), + ), + ("outcome", actual.outcome.clone(), expected.outcome.clone()), + ( + "protocol", + Value::List(actual.protocol.clone()), + Value::List(expected.protocol.clone()), + ), + ( + "observations", + Value::List(actual.observations.clone()), + Value::List(expected.observations.clone()), + ), + ("state", actual.state.clone(), expected.state.clone()), + ] { + if !same(&a, &b)? { + diff = format!( + "action {index} {key}: expected {}, got {}", + b.as_display(), + a.as_display() + ); + break; + } + } + if let Some(error) = guarded_error { + diff = error; + } + let matched = diff.is_empty() && bad_cells == 0; + action_checks.push(map(vec![ + ("action", Value::Int(index as i64)), + ("kind", t(&expected.kind)), + ("match", Value::Bool(matched)), + ("diff", t(&diff)), + ])); + if bad_cells > 0 { + mismatched += bad_cells; + matched_observations += (actual.cells.len() as i64 - bad_cells).max(0); + } else if !matched { + mismatched += 1; + checks.push(map(vec![ + ("cell", t(&expected.kind)), + ("action", Value::Int(index as i64)), + ("step", Value::Int(index as i64)), + ("match", Value::Bool(false)), + ("compared", Value::Bool(true)), + ("diff", t(&diff)), + ])); + } else { + matched_observations += expected.observations.len() as i64; + } + // A mismatched prefix cannot justify executing its dependent suffix. + if !matched { + break; + } + } + let executed = action_checks.len(); + Ok(map(vec![ + ("ok", Value::Bool(mismatched == 0)), + ("matched", Value::Int(matched_observations)), + ( + "matched_actions", + Value::Int(executed as i64 - if mismatched > 0 { 1 } else { 0 }), + ), + ("executed_actions", Value::Int(executed as i64)), + ( + "remaining_actions", + Value::Int((entries.len() - executed) as i64), + ), + ("mismatched", Value::Int(mismatched)), + ("skipped", Value::Int(skipped)), + ("checks", Value::List(checks)), + ("action_checks", Value::List(action_checks)), + ])) + } +} + +#[cfg(test)] +pub(super) fn sync_expected_cells(snapshot: &mut Value) -> Result<(), String> { + let originals = list(snapshot, "oracle_journal")?.to_vec(); + let mut entries = list(snapshot, "actions")? + .iter() + .enumerate() + .map(|(i, v)| HistoryEntry::parse(v, i)) + .collect::, _>>()?; + for entry in &mut entries { + for cell in &mut entry.cells { + if let Some(updated) = originals + .iter() + .find(|r| field(r, "seq").ok() == field(cell, "seq").ok()) + { + let old_value = field(cell, "value")?.clone(); + let new_value = field(updated, "value")?.clone(); + let old_stdout = field(cell, "stdout")?.clone(); + let new_stdout = field(updated, "stdout")?.clone(); + if !same(&old_value, &new_value)? { + for obs in &mut entry.observations { + if field(obs, "channel")? == &t("eval") + && same(field(obs, "value")?, &old_value)? + { + set_field(obs, "value", new_value.clone())?; + } + } + } + fn update_projection( + v: &mut Value, + old_value: &Value, + new_value: &Value, + old_stdout: &Value, + new_stdout: &Value, + ) -> Result<(), String> { + if let Value::Map(fields) = v { + let has_eval = fields.iter().any(|(k, _)| k == "stdout") + && fields.iter().any(|(k, _)| k == "code"); + if has_eval { + for (k, value) in fields.iter_mut() { + if k == "value" && same(value, old_value)? { + *value = new_value.clone(); + } + if k == "stdout" && same(value, old_stdout)? { + *value = new_stdout.clone(); + } + } + } + for (_, value) in fields { + update_projection(value, old_value, new_value, old_stdout, new_stdout)?; + } + } else if let Value::List(values) = v { + for value in values { + update_projection(value, old_value, new_value, old_stdout, new_stdout)?; + } + } + Ok(()) + } + update_projection( + &mut entry.outcome, + &old_value, + &new_value, + &old_stdout, + &new_stdout, + )?; + *cell = updated.clone(); + } + } + } + let observations = entries + .iter() + .flat_map(|e| e.observations.clone()) + .collect(); + let actions = entries.iter().enumerate().map(|(i, e)| e.wire(i)).collect(); + set_field(snapshot, "actions", Value::List(actions))?; + set_field(snapshot, "observation_journal", Value::List(observations))?; + Ok(()) +} +#[cfg(test)] +fn set_field(record: &mut Value, key: &str, value: Value) -> Result<(), String> { + let Value::Map(fields) = record else { + return Err("test: expected map".into()); + }; + fields + .iter_mut() + .find(|(k, _)| k == key) + .ok_or("test: missing field")? + .1 = value; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn aggregate_replay_checks_each_unit_before_dependent_side_effects() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + for language in ["python", "node"] { + for persistent in [false, true] { + let nonce = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(); + let path = root.join(format!( + "target/batch-boundary-{}-{nonce}.mq.md", + std::process::id() + )); + let flag = path.with_extension("input"); + let started = path.with_extension("started"); + let suffix = path.with_extension("suffix"); + let literal = |p: &Path| serde_json::to_string(&p.display().to_string()).unwrap(); + let (first, next) = if language == "python" { + (format!("from pathlib import Path\nPath({}).write_text('first')\nprint(1 if Path({}).exists() else 2)", literal(&started), literal(&flag)), + format!("open({}, 'w').write('suffix')\nprint(7)", literal(&suffix))) + } else { + (format!("require('fs').writeFileSync({}, 'first'); console.log(require('fs').existsSync({}) ? 1 : 2);", literal(&started), literal(&flag)), + format!("require('fs').writeFileSync({}, 'suffix'); console.log(7);", literal(&suffix))) + }; + let header = if persistent { + "---\nimport:\n foreign: lib/foreign.mq.md\n---\n# main\n\n`会话` = > foreign.session\n" + } else { + "# main\n" + }; + std::fs::write(&path, format!("{header}\n`first` =\n```{language}\n{first}\n```\n\n`next` =\n```{language}\n{next}\n```\n")).unwrap(); + std::fs::write(&flag, "present").unwrap(); + let mut recording = DocumentSession::open(&path, &root).unwrap(); + recording.run_all(false).unwrap(); + let snapshot = recording.export_snapshot().unwrap(); + recording.close(); + let good = + DocumentSession::check_snapshot_isolated(&path, &root, &snapshot).unwrap(); + assert_eq!( + field(&good, "ok").unwrap(), + &Value::Bool(true), + "{language} persistent={persistent}: {}", + good.as_display() + ); + std::fs::remove_file(&flag).unwrap(); + for compact in [false, true] { + let _ = std::fs::remove_file(&started); + let _ = std::fs::remove_file(&suffix); + let input = if compact { + super::super::packing::pack(&snapshot).unwrap() + } else { + snapshot.clone() + }; + let checked = + DocumentSession::check_snapshot_isolated(&path, &root, &input).unwrap(); + assert_eq!(field(&checked, "ok").unwrap(), &Value::Bool(false)); + assert!( + started.exists(), + "first primitive must execute to observe the changed response" + ); + assert!( + !suffix.exists(), + "{language} persistent={persistent}: mismatching batch executed its suffix" + ); + } + if persistent { + let _ = std::fs::remove_file(&started); + let mut wrong_request = snapshot.clone(); + let Value::Map(fields) = &mut wrong_request else { + panic!() + }; + let (_, Value::List(actions)) = + fields.iter_mut().find(|(k, _)| k == "actions").unwrap() + else { + panic!() + }; + let mut entry = HistoryEntry::parse(&actions[0], 0).unwrap(); + let Value::Map(step) = &mut entry.protocol[0] else { + panic!() + }; + let (_, Value::Text(json)) = + step.iter_mut().find(|(k, _)| k == "request").unwrap() + else { + panic!() + }; + let mut request: serde_json::Value = serde_json::from_str(json).unwrap(); + request["code"] = "different code".into(); + *json = request.to_string(); + actions[0] = entry.wire(0); + let checked = + DocumentSession::check_snapshot_isolated(&path, &root, &wrong_request) + .unwrap(); + assert_eq!(field(&checked, "ok").unwrap(), &Value::Bool(false)); + assert!( + !started.exists(), + "request divergence was detected after dispatch" + ); + assert!(!suffix.exists()); + } + for file in [path, started] { + let _ = std::fs::remove_file(file); + } + println!("E12 {language} persistent={persistent}: matching replay passes; changed response stops inside batch; compact replay has same boundary"); + } + } + } + fn fixture() -> (PathBuf, PathBuf, String) { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let path = root.join(format!( + "target/history-{}-{}.mq.md", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + let source = r#"--- +import: + foreign: lib/foreign.mq.md +--- +# main + +`kernels` = +| 名称 | 语言 | 命令 | +|---|---|---| +| py | python | | + +`units` = +| 单元 | 内核 | +|---|---| +| inc | py | +| bad | py | +| read | py | + +`initial` = +| 内核 | 名称 | 值 | +|---|---|---| +| py | n | 1 | + +`会话` = > foreign.session kernels=`kernels` cells=`units` values=`initial` + +`inc` = +```python +n += 1 +n +``` + +`bad` = +```python +n = 99 +2**53 +``` + +`read` = +```python +n +``` +"# + .to_string(); + std::fs::write(&path, &source).unwrap(); + (root, path, source) + } + fn assert_replay(b: &mut DocumentSession, snap: &Value) { + let result = b.replay_check(snap).unwrap(); + assert_eq!( + field(&result, "ok").unwrap(), + &Value::Bool(true), + "{}", + result.as_display() + ); + assert_eq!(field(&result, "remaining_actions").unwrap(), &Value::Int(0)); + } + #[test] + fn history_replays_inputs_fault_recovery_restart_edits_and_clear() { + let (root, path, source) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(1)); + a.set_value("py", "n", Value::Int(4)).unwrap(); + a.run_cell("inc", None).unwrap(); + assert!(a + .run_cell("bad", None) + .unwrap_err() + .contains("outside V_safe")); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(99)); + a.restart_kernel("py").unwrap(); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(1)); + // Invalid outbound requests must be represented without making them legal wire payloads. + assert!(a.set_value("py", "n", Value::Int(i64::MAX)).is_err()); + assert!(a + .set_value("py", "n", Value::Handle("must-not-escape".into())) + .is_err()); + assert!(a + .set_value("py", "n", Value::Secret("must-not-be-recorded".into())) + .is_err()); + a.set_value("py", "n", Value::Int(8)).unwrap(); + a.step().unwrap(); + let result = a.run_all(false).unwrap(); + assert_eq!(field(&result, "failed").unwrap(), &Value::Int(1)); + a.clear_outputs(); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(99)); + a.add_cell("extra", "python", "n += 2\nn", "py").unwrap(); + a.run_cell("extra", None).unwrap(); + a.clear_cell("extra").unwrap(); + a.remove_cell("extra").unwrap(); + a.state_list().unwrap(); + a.interrupt().unwrap(); + assert_eq!( + a.pull_value("py", "n").unwrap(), + Value::Int(101), + "idle interrupt preserves kernel state" + ); + let snap = a.export_snapshot().unwrap(); + let wire = super::super::super::json::stringify(&snap, None).unwrap(); + let Value::Text(ref serialized) = wire else { + panic!() + }; + assert!(!serialized.contains("must-not-escape")); + assert!(!serialized.contains("must-not-be-recorded")); + let snap = super::super::super::json::parse(&wire).unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + assert_replay(&mut b, &snap); + assert_eq!(b.pull_value("py", "n").unwrap(), Value::Int(101)); + assert_eq!( + std::fs::read_to_string(&path).unwrap(), + source, + "replay must not rewrite source" + ); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn history_replays_reload_versions_without_rewriting_the_document() { + let (root, path, source) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + a.set_value("py", "n", Value::Int(5)).unwrap(); + a.run_cell("inc", None).unwrap(); + let second = source + .replace("n += 1\nn", "n += 10\nn") + .replace("| py | n | 1 |", "| py | n | 2 |"); + std::fs::write(&path, &second).unwrap(); + a.reload(&root).unwrap(); + a.run_cell("inc", None).unwrap(); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(16)); + a.restart_all().unwrap(); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(2)); + let third = second + .replace("| py | python | |", "| py | python | \"python3 -u\" |") + .replace("| py | n | 2 |", "| py | n | 3 |"); + std::fs::write(&path, &third).unwrap(); + a.reload(&root).unwrap(); + assert_eq!( + a.pull_value("py", "n").unwrap(), + Value::Int(3), + "changed kernel command must retire the old process" + ); + assert_eq!(a.host.entry_source.as_ref().unwrap(), &third); + let snap = a.export_snapshot().unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + assert_replay(&mut b, &snap); + assert_eq!(b.host.entry_source.as_ref().unwrap(), &third); + assert_eq!(std::fs::read_to_string(&path).unwrap(), third); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn malformed_action_suffix_is_rejected_before_any_kernel_request() { + let (root, path, _) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + a.run_cell("inc", None).unwrap(); + a.pull_value("py", "n").unwrap(); + let original = a.export_snapshot().unwrap(); + a.close(); + for kind in [ + "unknown", + "count", + "index", + "args", + "image", + "projection", + "truncated", + "state", + "outcome", + ] { + let mut snap = original.clone(); + match kind { + "count" => set_field(&mut snap, "action_count", Value::Int(0)).unwrap(), + "image" => { + let Value::Map(fields) = &mut snap else { + panic!() + }; + let img = &mut fields + .iter_mut() + .find(|(k, _)| k == "initial_image") + .unwrap() + .1; + set_field(img, "source_hash", t("wrong")).unwrap(); + } + "projection" => { + set_field(&mut snap, "observation_journal", Value::List(vec![])).unwrap() + } + _ => { + let Value::Map(fields) = &mut snap else { + panic!() + }; + let (_, Value::List(actions)) = + fields.iter_mut().find(|(k, _)| k == "actions").unwrap() + else { + panic!() + }; + if kind == "truncated" { + actions.pop(); + } else { + let last = actions.last_mut().unwrap(); + match kind { + "unknown" => set_field(last, "kind", t("not-an-operation")).unwrap(), + "index" => set_field(last, "index", Value::Int(0)).unwrap(), + "args" => set_field(last, "args", encode(&Value::Int(1))).unwrap(), + "state" => set_field(last, "state", encode(&Value::None)).unwrap(), + "outcome" => { + set_field(last, "outcome", encode(&Value::Handle("bad".into()))) + .unwrap() + } + _ => unreachable!(), + } + } + } + } + let mut b = DocumentSession::open(&path, &root).unwrap(); + let requests = b.host.foreign_protocol_steps.len(); + assert!( + b.replay_check(&snap).is_err(), + "{kind}: malformed history accepted" + ); + assert_eq!( + b.host.foreign_protocol_steps.len(), + requests, + "{kind}: executed before suffix validation" + ); + assert!(b.action_history.is_empty()); + b.close(); + } + std::fs::remove_file(path).unwrap(); + } + #[test] + fn generated_history_matches_independent_state_and_survives_json_roundtrip() { + // The experiment driver supplies larger budgets; ordinary CI retains the short corpus. + let steps: usize = std::env::var("MARQDO_HISTORY_STEPS") + .map(|s| s.parse().expect("positive history length")) + .unwrap_or(32); + assert!(steps > 0); + let seeds: Vec = std::env::var("MARQDO_HISTORY_SEEDS") + .map(|s| s.split(',').map(|s| s.parse().expect("u64 seed")).collect()) + .unwrap_or_else(|_| vec![3, 11, 29, 71]); + assert!(!seeds.is_empty()); + for lang in ["python", "node"] { + for &seed in &seeds { + let (root, path, source) = fixture(); + let source = source.replace("python", lang); + std::fs::write(&path, &source).unwrap(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + let mut expected = 1i64; + let mut cursor = 0usize; + let mut rng = seed; + let mut operations = [0usize; 13]; + let record_start = std::time::Instant::now(); + for step in 0..steps { + rng = rng + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + let operation = ((rng >> 32) % 13) as usize; + operations[operation] += 1; + match operation { + 0 => { + expected = ((rng >> 40) % 1000) as i64; + a.set_value("py", "n", Value::Int(expected)).unwrap(); + } + 1 => { + a.run_cell("inc", None).unwrap(); + expected += 1; + } + 2 => { + assert!(a.run_cell("bad", None).is_err()); + expected = 99; + } + 3 => { + a.restart_kernel("py").unwrap(); + expected = 1; + } + 4 => { + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(expected)); + } + 5 => { + a.clear_outputs(); + cursor = 0; + } + 6 => { + assert!(a.set_value("py", "n", Value::Int(i64::MAX)).is_err()); + } + 7 => { + a.run_all(false).unwrap(); + expected = 99; + cursor = 3; + } + 8 => { + let result = a.step(); + match cursor { + 0 => { + result.unwrap(); + expected += 1; + cursor = 1; + } + 1 => { + assert!(result.is_err()); + expected = 99; + } + 2 => { + result.unwrap(); + cursor = 3; + } + _ => { + result.unwrap(); + } + } + } + 9 => { + a.restart_all().unwrap(); + expected = 1; + cursor = 0; + } + 10 => { + a.interrupt().unwrap(); + } + 11 => { + a.state_list().unwrap(); + } + _ => { + assert!(a.run_cell("missing", None).is_err()); + } + } + assert_eq!( + a.pull_value("py", "n").unwrap(), + Value::Int(expected), + "{lang} seed {seed} step {step}" + ); + assert_eq!(a.cursor, cursor, "{lang} seed {seed} step {step} cursor"); + } + let record_ns = record_start.elapsed().as_nanos(); + let snapshot_start = std::time::Instant::now(); + let snap = a.export_snapshot().unwrap(); + let snapshot_ns = snapshot_start.elapsed().as_nanos(); + let action_count = a.action_history.len(); + let protocol_count = a.host.foreign_protocol_steps.len(); + let observation_count = a.host.oracle_observations.len(); + let encode_start = std::time::Instant::now(); + let wire = super::super::super::json::stringify(&snap, None).unwrap(); + let encode_ns = encode_start.elapsed().as_nanos(); + let decode_start = std::time::Instant::now(); + let decoded_raw = super::super::super::json::parse(&wire).unwrap(); + let decode_ns = decode_start.elapsed().as_nanos(); + drop(decoded_raw); + let pack_start = std::time::Instant::now(); + let packed = super::super::packing::pack(&snap).unwrap(); + let pack_ns = pack_start.elapsed().as_nanos(); + let compact_nodes = list(&packed, "nodes").unwrap().len(); + let compact_encode_start = std::time::Instant::now(); + let compact_wire = super::super::super::json::stringify(&packed, None).unwrap(); + let compact_encode_ns = compact_encode_start.elapsed().as_nanos(); + let compact_decode_start = std::time::Instant::now(); + let packed = super::super::super::json::parse(&compact_wire).unwrap(); + let compact_decode_ns = compact_decode_start.elapsed().as_nanos(); + let unpack_start = std::time::Instant::now(); + let unpacked = super::super::packing::unpack(&packed).unwrap(); + let unpack_ns = unpack_start.elapsed().as_nanos(); + assert_eq!( + unpacked, snap, + "lossless packing changed the complete history" + ); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + let replay_start = std::time::Instant::now(); + assert_replay(&mut b, &packed); + let replay_ns = replay_start.elapsed().as_nanos(); + assert_eq!(b.pull_value("py", "n").unwrap(), Value::Int(expected)); + assert_eq!(b.cursor, cursor); + b.close(); + std::fs::remove_file(path).unwrap(); + if let Ok(directory) = std::env::var("MARQDO_HISTORY_SNAPSHOT_DIR") { + let directory = PathBuf::from(directory); + std::fs::create_dir_all(&directory).unwrap(); + let Value::Text(raw) = &wire else { + panic!("snapshot text") + }; + let Value::Text(compact) = &compact_wire else { + panic!("snapshot text") + }; + std::fs::write( + directory.join(format!("raw-{lang}-{seed}-{steps}.json")), + raw, + ) + .unwrap(); + std::fs::write( + directory.join(format!("compact-{lang}-{seed}-{steps}.json")), + compact, + ) + .unwrap(); + } + println!( + "HISTORY_METRIC:{}", + serde_json::json!({ + "language": lang, "seed": seed, "steps": steps, + "operations": operations, "actions": action_count, + "protocol_steps": protocol_count, "observations": observation_count, + "snapshot_bytes": match &wire { Value::Text(text) => text.len(), _ => panic!("stringify must return text") }, "record_ns": record_ns, + "snapshot_ns": snapshot_ns, "encode_ns": encode_ns, + "compact_snapshot_bytes": match &compact_wire { Value::Text(text) => text.len(), _ => panic!("stringify must return text") }, + "compact_nodes": compact_nodes, "pack_ns": pack_ns, "unpack_ns": unpack_ns, + "compact_encode_ns": compact_encode_ns, "compact_decode_ns": compact_decode_ns, + "decode_ns": decode_ns, "replay_ns": replay_ns, + "expected_final_value": expected, "expected_cursor": cursor, + "correct": true + }) + ); + } + } + } + + #[test] + fn mismatch_stops_before_dependent_external_effect() { + let (root, path, _) = fixture(); + let marker = path.with_extension("marker"); + let literal = serde_json::to_string(&marker.display().to_string()).unwrap(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + a.pull_value("py", "n").unwrap(); + a.add_cell( + "touch", + "python", + &format!("with open({literal}, 'w') as out:\n out.write('effect')\nn"), + "py", + ) + .unwrap(); + a.run_cell("touch", None).unwrap(); + assert!(marker.exists()); + let mut snap = a.export_snapshot().unwrap(); + a.close(); + std::fs::remove_file(&marker).unwrap(); + let mut entries = list(&snap, "actions") + .unwrap() + .iter() + .enumerate() + .map(|(i, v)| HistoryEntry::parse(v, i).unwrap()) + .collect::>(); + set_field(&mut entries[0].outcome, "value", Value::Int(2)).unwrap(); + set_field(&mut entries[0].observations[0], "value", Value::Int(2)).unwrap(); + for entry in &mut entries { + let Value::Map(fields) = &mut entry.state else { + panic!() + }; + let (_, Value::List(bindings)) = + fields.iter_mut().find(|(k, _)| k == "values").unwrap() + else { + panic!() + }; + for binding in bindings { + if text(binding, "name").unwrap() == "n" { + set_field(binding, "value", Value::Int(2)).unwrap(); + } + } + } + + set_field( + &mut snap, + "actions", + Value::List(entries.iter().enumerate().map(|(i, e)| e.wire(i)).collect()), + ) + .unwrap(); + set_field( + &mut snap, + "observation_journal", + Value::List( + entries + .iter() + .flat_map(|e| e.observations.clone()) + .collect(), + ), + ) + .unwrap(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + let check = b.replay_check(&snap).unwrap(); + assert_eq!(field(&check, "ok").unwrap(), &Value::Bool(false)); + assert_eq!(field(&check, "executed_actions").unwrap(), &Value::Int(1)); + assert!( + !marker.exists(), + "dependent code ran after a mismatched prefix" + ); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn separate_array_bindings_are_not_a_ragged_metadata_table() { + let (root, path, _) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + a.push_value("py", "left", Value::List(vec![Value::Int(1)])) + .unwrap(); + a.push_value( + "py", + "right", + Value::List(vec![Value::Int(2), Value::Int(3)]), + ) + .unwrap(); + a.state_list().unwrap(); + let snap = a.export_snapshot().unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + assert_replay(&mut b, &snap); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn invalid_import_payload_is_checked_before_any_binding_write() { + let (root, path, _) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + let mut invalid = a.export_snapshot().unwrap(); + let Value::Map(fields) = &mut invalid else { + panic!() + }; + let (_, Value::List(kernels)) = fields.iter_mut().find(|(k, _)| k == "kernels").unwrap() + else { + panic!() + }; + set_field( + &mut kernels[0], + "values", + Value::List(vec![ + map(vec![("name", t("n")), ("value", Value::Int(999))]), + map(vec![ + ("name", t("bad")), + ( + "value", + Value::Code(crate::value::CodeBlock { + lang: "python".into(), + source: "1".into(), + }), + ), + ]), + ]), + ) + .unwrap(); + let requests = a.host.foreign_protocol_steps.len(); + assert!(a.import_snapshot(&invalid).is_err()); + assert_eq!( + a.host.foreign_protocol_steps.len(), + requests, + "invalid imported code caused a partial foreign write" + ); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(1)); + let snap = a.export_snapshot().unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + assert_replay(&mut b, &snap); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn failed_reload_input_is_an_explicit_historical_input() { + let (root, path, source) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + std::fs::remove_file(&path).unwrap(); + assert!(a.reload(&root).is_err()); + std::fs::write(&path, &source).unwrap(); + a.run_cell("inc", None).unwrap(); + let snap = a.export_snapshot().unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + assert_replay(&mut b, &snap); + assert_eq!(std::fs::read_to_string(&path).unwrap(), source); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn failed_introspection_replies_are_not_silently_ignored() { + let (root, path, _) = fixture(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + a.add_cell( + "function", + "python", + "def unsupported(): return 1\nunsupported", + "py", + ) + .unwrap(); + assert!(a.run_cell("function", None).is_err()); + let mut snap = a.export_snapshot().unwrap(); + a.close(); + let mut entries = list(&snap, "actions") + .unwrap() + .iter() + .enumerate() + .map(|(i, row)| HistoryEntry::parse(row, i).unwrap()) + .collect::>(); + let inspect = entries.last_mut().unwrap(); + let mut changed = false; + for step in &mut inspect.protocol { + let response = field(step, "response").unwrap(); + if boolean(response, "ok").unwrap() { + let mut raw: serde_json::Value = + serde_json::from_str(text(response, "json").unwrap()).unwrap(); + if raw.get("domain_error").and_then(|v| v.as_bool()) == Some(true) { + raw["error"] = + serde_json::Value::String("not JSON-safe: changed diagnostic".into()); + let Value::Map(fields) = step else { panic!() }; + let (_, response) = fields.iter_mut().find(|(k, _)| k == "response").unwrap(); + set_field(response, "json", t(&raw.to_string())).unwrap(); + changed = true; + break; + } + } + } + assert!( + changed, + "fixture must contain a refused introspection response" + ); + set_field( + &mut snap, + "actions", + Value::List(entries.iter().enumerate().map(|(i, e)| e.wire(i)).collect()), + ) + .unwrap(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + let check = b.replay_check(&snap).unwrap(); + assert_eq!(field(&check, "ok").unwrap(), &Value::Bool(false)); + assert!(field(&check, "action_checks") + .unwrap() + .as_display() + .contains("protocol")); + b.close(); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn repeated_process_death_history_has_stable_failure_observations() { + for lang in ["python", "node"] { + for _ in 0..4 { + let (root, path, source) = fixture(); + let source = source.replace("python", lang).replace( + "n = 99\n2**53", + if lang == "python" { + "import os\nos._exit(7)" + } else { + "process.exit(7)" + }, + ); + std::fs::write(&path, &source).unwrap(); + let mut a = DocumentSession::open(&path, &root).unwrap(); + let record = a.run_cell("bad", None).unwrap(); + assert_eq!(field(&record, "ok").unwrap(), &Value::Bool(false)); + assert_eq!(a.pull_value("py", "n").unwrap(), Value::Int(1)); + let snap = a.export_snapshot().unwrap(); + a.close(); + let mut b = DocumentSession::open(&path, &root).unwrap(); + assert_replay(&mut b, &snap); + b.close(); + std::fs::remove_file(path).unwrap(); + } + } + } +} + +impl DocumentSession { + /// Validate and compare a recording without touching an existing live view. + /// No declarations or seed kernels run before the history preflight. + pub(crate) fn check_snapshot_isolated( + path: &Path, + root: &Path, + snapshot: &Value, + ) -> Result { + let source = + std::fs::read_to_string(path).map_err(|e| format!("read {}: {e}", path.display()))?; + let mut host = HostContext::default(); + host.cwd = path.parent().unwrap_or_else(|| Path::new(".")).into(); + host.fs_root = Some(root.into()); + host.entry_path = Some(path.into()); + host.entry_source = Some(source.clone()); + let mut target = Self { + control: super::DocumentControl::default(), + replay_controls: Vec::new(), + replay_boundary: None, + source_hash: hash_source(&source), + initial_values: Vec::new(), + initial_image: Value::None, + action_history: Vec::new(), + action_depth: 0, + replay_fresh: true, + path: path.into(), + root: root.into(), + persistent: false, + cells: Vec::new(), + kernels: Vec::new(), + order: Vec::new(), + journal: Vec::new(), + oracle_journal: Vec::new(), + value_store: HashMap::new(), + cursor: 0, + generation: 1, + opened_ms: now_ms(), + host, + kernel_sessions: HashMap::new(), + seq: 0, + }; + target.replay_check(snapshot) + } +} diff --git a/src/host/foreign_session.rs b/src/host/foreign_session.rs index b1e9f98..bb1516a 100644 --- a/src/host/foreign_session.rs +++ b/src/host/foreign_session.rs @@ -16,11 +16,15 @@ use crate::host::json::{json_to_value, value_to_json}; use crate::host::HostContext; use crate::value::Value; +mod control; +pub mod validation; +pub use control::InterruptHandle; + const SESSION_TIMEOUT_SECS: f64 = 10.0; const MAX_SESSION_TIMEOUT_SECS: f64 = 3600.0; const MAX_PROCESS_STDERR: usize = 1_048_576; -const PY_BOOTSTRAP: &str = r#"import sys, json, io, ast, traceback, contextlib +const PY_BOOTSTRAP: &str = r#"import sys, json, io, ast, traceback, contextlib, math, signal _out = sys.stdout _MAX_SAFE = 9007199254740991 def _sanitize(o): @@ -31,13 +35,19 @@ def _sanitize(o): raise ValueError("json: integer outside V_safe (±(2^53-1)): %s" % o) return o if isinstance(o, float): + if not math.isfinite(o): + raise ValueError("json: non-finite number outside V_safe") + if o.is_integer() and abs(o) > _MAX_SAFE: + raise ValueError("json: integer outside V_safe (±(2^53-1)): %s" % o) 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()} + if any(not isinstance(k, str) for k in o): + raise TypeError("not JSON-safe: object keys must be strings") + return {k: _sanitize(v) for k, v in o.items()} raise TypeError("not JSON-safe: %s" % type(o).__name__) def _send(o): try: @@ -48,10 +58,17 @@ def _send(o): "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), + "result": None, "error": str(e), "domain_error": True, }, ensure_ascii=False) _out.write(line + "\n") _out.flush() +_busy = False +def _interrupt(signum, frame): + global _busy + if _busy: + _busy = False + raise KeyboardInterrupt +signal.signal(signal.SIGINT, _interrupt) _g = {"__name__": "__main__", "marqdo_input": None} for _line in sys.stdin: _line = _line.strip() @@ -87,6 +104,8 @@ for _line in sys.stdin: _out_buf = io.StringIO() _err_buf = io.StringIO() try: + _busy = True + _send({"id": rid, "phase": "ready"}) _tree = ast.parse(_code, "") _result = None with contextlib.redirect_stdout(_out_buf), contextlib.redirect_stderr(_err_buf): @@ -97,10 +116,13 @@ for _line in sys.stdin: _result = eval(compile(ast.Expression(_last.value), "", "eval"), _g) else: exec(compile(_tree, "", "exec"), _g) + _busy = False _send({"id": rid, "ok": True, "stdout": _out_buf.getvalue(), "stderr": _err_buf.getvalue(), "result": _result, "error": ""}) except BaseException: + _busy = False _send({"id": rid, "ok": False, "stdout": _out_buf.getvalue(), "stderr": _err_buf.getvalue(), "result": None, "error": traceback.format_exc(limit=8)}) finally: + _busy = False sys.stdin = _old_stdin "#; @@ -108,12 +130,14 @@ 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); +process.on("SIGINT", () => {}); const MAX_SAFE = Number.MAX_SAFE_INTEGER; globalThis.require = require; const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity }); -function sanitize(v) { +function sanitize(v, active = new Set()) { if (v === null || typeof v === "boolean" || typeof v === "string") return v; if (typeof v === "number") { + if (!Number.isFinite(v)) throw new Error("json: non-finite number outside V_safe"); if (Number.isInteger(v) && Math.abs(v) > MAX_SAFE) throw new Error("json: integer outside V_safe (±(2^53-1)): " + v); return v; @@ -123,11 +147,36 @@ function sanitize(v) { 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; + if (active.has(v)) throw new Error("not JSON-safe: cyclic value"); + active.add(v); + try { + const keys = Reflect.ownKeys(v); + if (Array.isArray(v)) { + if (keys.length !== v.length + 1) + throw new Error("not JSON-safe: sparse array or extra array properties"); + const rows = []; + for (let i = 0; i < v.length; i++) { + const d = Object.getOwnPropertyDescriptor(v, String(i)); + if (!d || !d.enumerable || !("value" in d)) + throw new Error("not JSON-safe: array accessor or missing element"); + rows.push(sanitize(d.value, active)); + } + return rows; + } + const prototype = Object.getPrototypeOf(v); + if (prototype !== Object.prototype && prototype !== null) + throw new Error("not JSON-safe: expected plain record"); + const o = Object.create(null); + for (const k of keys) { + if (typeof k !== "string") throw new Error("not JSON-safe: symbol record key"); + const d = Object.getOwnPropertyDescriptor(v, k); + if (!d || !d.enumerable || !("value" in d)) + throw new Error("not JSON-safe: record accessor or hidden property"); + o[k] = sanitize(d.value, active); + } + return o; + } finally { active.delete(v); } } throw new Error("not JSON-safe: " + (typeof v)); } @@ -137,7 +186,7 @@ function send(o) { 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) + result: null, error: String(e && e.message ? e.message : e), domain_error: true }) + "\n"); } } @@ -166,7 +215,7 @@ rl.on("line", async (line) => { let result = null; try { result = vm.runInThisContext(String(req.name), { filename: "" }); } catch (e) { result = globalThis[req.name] === undefined ? null : globalThis[req.name]; } - send({ id: rid, ok: true, stdout: "", stderr: "", result: toJSON(result), error: "" }); + send({ id: rid, ok: true, stdout: "", stderr: "", result: result, error: "" }); return; } if (op === "names") { @@ -187,15 +236,27 @@ rl.on("line", async (line) => { stdinPatched = true; } catch (e) {} } - const cap = await capture(async () => vm.runInThisContext(req.code || "", { filename: "" })); + send({ id: rid, phase: "ready" }); + const cap = await capture(async () => vm.runInThisContext(req.code || "", { filename: "", breakOnSigint: true })); if (stdinPatched) { try { Object.defineProperty(process, "stdin", { value: oldStdin, configurable: true, writable: true }); } catch (e) {} } - send({ id: rid, ok: !cap.error, stdout: cap.logs, stderr: cap.errs, result: toJSON(cap.result), error: cap.error }); + send({ id: rid, ok: !cap.error, stdout: cap.logs, stderr: cap.errs, result: cap.result, error: cap.error }); }); "#; +/// Internal protocol evidence, distinct from admitted language observations. +/// Includes failed requests and post-execution domain refusals, so set/input +/// dependencies cannot disappear from the recorded history. +#[derive(Debug, Clone)] +pub struct ProtocolStep { + pub session: String, + pub request: serde_json::Value, + pub response: Result, +} + pub struct Session { + pub interrupt_handle: InterruptHandle, pub id: String, pub lang: String, pub cmd: String, @@ -224,7 +285,11 @@ impl std::fmt::Debug for Session { impl Drop for Session { fn drop(&mut self) { - let _ = self.child.kill(); + if let Some(_pid) = self.interrupt_handle.retire() { + #[cfg(unix)] + unsafe { libc::kill(-(_pid as i32), libc::SIGKILL); } + let _ = self.child.kill(); + } let _ = self.child.wait(); } } @@ -468,6 +533,7 @@ pub fn open( let started_ms = now_ms(); let cmd_display = argv[0].clone(); let session = Session { + interrupt_handle: InterruptHandle::new(child.id()), id: id.clone(), lang: canonical.clone(), cmd: cmd_display.clone(), @@ -545,7 +611,7 @@ pub fn eval_with_stdin( }; let input_json = match input { None | Some(Value::None) => serde_json::Value::Null, - Some(v) => value_to_json(v)?, + Some(v) => { super::oracle::admit(v)?; value_to_json(v)? }, }; let default_timeout = ctx.foreign_sessions[&id].timeout.as_secs_f64(); let dur = parse_timeout(timeout, default_timeout)?; @@ -564,6 +630,9 @@ pub fn eval_with_stdin( let reply = match send_and_wait(ctx, &id, &request, dur) { Ok(r) => r, Err(e) => { + // Replay divergence is a language-level stop, not process death or + // an ordinary failed-eval value. Keep the actual committed kernel. + if ctx.foreign_replay_divergence.is_some() { return Err(e); } let timed_out = e.contains("timed out"); let stderr = session_dead_stderr(ctx, &id); remove_session(ctx, &id); @@ -611,7 +680,7 @@ pub fn eval_with_stdin( .unwrap_or("") .to_string(); // Ω ∉ V_safe is a language fault, not a soft cell failure. - if error.contains("outside V_safe") { + if reply.get("domain_error").and_then(|v| v.as_bool()) == Some(true) { return Err(error); } let result = reply @@ -619,6 +688,7 @@ pub fn eval_with_stdin( .map(json_to_value) .transpose()? .unwrap_or(Value::None); + super::oracle::admit(&result)?; Ok(record( Some(&id), &lang, @@ -647,13 +717,54 @@ fn send_and_wait( request: &serde_json::Value, timeout: Duration, ) -> Result { + if ctx.foreign_replay_expected.is_some() { + if let Some(error) = &ctx.foreign_replay_divergence { return Err(error.clone()); } + } + let permit = validation::prepare(ctx.foreign_replay_expected.as_mut(), id, request) + .map_err(|failure| { + let error = failure.to_string(); + ctx.foreign_replay_divergence = Some(error.clone()); + error + })?; + let control = ctx.foreign_sessions.get(id).map(|s| s.interrupt_handle.clone()); + if let Some(handle) = &control { handle.begin(request.get("op").and_then(|v| v.as_str()) == Some("eval")); } + let response = send_and_wait_inner(ctx, permit, timeout); + if let Some(handle) = control { handle.finish(); } + ctx.foreign_protocol_steps.push(ProtocolStep { + session: id.to_string(), request: request.clone(), response: response.clone(), + }); + if let Some(steps) = &mut ctx.foreign_replay_expected { + if let Err(failure) = steps.accept_response(&response) { + let error = failure.to_string(); + ctx.foreign_replay_divergence = Some(error.clone()); + return Err(error); + } + } + response +} + +fn send_control( + ctx: &mut HostContext, id: &str, request: &serde_json::Value, +) -> Result { + let response = send_and_wait(ctx, id, request, Duration::from_secs(10)); + if response.is_err() && ctx.foreign_replay_divergence.is_none() { remove_session(ctx, id); } + response +} + +fn send_and_wait_inner( + ctx: &mut HostContext, + permit: validation::DispatchPermit, + timeout: Duration, +) -> Result { + let id = permit.session(); + let request = permit.request(); let line = serde_json::to_string(request).map_err(|e| format!("session request: {e}"))?; { let session = ctx .foreign_sessions .get_mut(id) .ok_or_else(|| format!("foreign session `{id}` not found"))?; - if let Ok(Some(_)) = session.child.try_wait() { + if let Ok(Some(_)) = session.interrupt_handle.try_wait(&mut session.child) { return Err("session process exited".into()); } session @@ -687,13 +798,18 @@ fn send_and_wait( if rid != 1 { continue; } + if v.get("phase").and_then(|v| v.as_str()) == Some("ready") { + session.interrupt_handle.ready()?; + continue; + } return Ok(v); } // Unexpected non-protocol output: keep waiting until the deadline. continue; } Err(mpsc::RecvTimeoutError::Timeout) => { - if let Ok(Some(_)) = session.child.try_wait() { + session.interrupt_handle.retry()?; + if let Ok(Some(_)) = session.interrupt_handle.try_wait(&mut session.child) { return Err("session process exited".into()); } continue; @@ -707,11 +823,13 @@ fn send_and_wait( fn kill_session(ctx: &mut HostContext, id: &str) { if let Some(mut s) = ctx.foreign_sessions.remove(id) { - #[cfg(unix)] - unsafe { - libc::kill(-(s.child.id() as i32), libc::SIGKILL); + if let Some(_pid) = s.interrupt_handle.retire() { + #[cfg(unix)] + unsafe { + libc::kill(-(_pid as i32), libc::SIGKILL); + } + let _ = s.child.kill(); } - let _ = s.child.kill(); let _ = s.child.wait(); s.alive = false; } @@ -725,17 +843,7 @@ pub fn interrupt(ctx: &mut HostContext, id: &Value) -> Result { .foreign_sessions .get_mut(&id) .ok_or_else(|| format!("foreign session `{id}` not found"))?; - #[cfg(unix)] - unsafe { - libc::kill(-(session.child.id() as i32), libc::SIGINT); - } - #[cfg(windows)] - { - let _ = Command::new("taskkill") - .args(["/PID", &session.child.id().to_string(), "/T", "/F"]) - .output(); - session.alive = false; - } + session.interrupt_handle.request()?; Ok(Value::Bool(true)) } @@ -746,13 +854,17 @@ pub fn set( value: &Value, ) -> Result { let id = as_session_id(id)?.to_string(); + if !ctx.foreign_sessions.contains_key(&id) { + return Err(format!("foreign session `{id}` not found")); + } let name = as_text(name, "name")?.to_string(); if !valid_name(&name) { return Err("session_set: name must be an identifier".into()); } + super::oracle::admit(value)?; let json = value_to_json(value)?; let request = serde_json::json!({"id": 1, "op": "set", "name": name, "value": json}); - let reply = send_and_wait(ctx, &id, &request, Duration::from_secs(10))?; + let reply = send_control(ctx, &id, &request)?; if reply.get("ok").and_then(|v| v.as_bool()).unwrap_or(false) { Ok(Value::None) } else { @@ -766,12 +878,15 @@ pub fn set( pub fn get(ctx: &mut HostContext, id: &Value, name: &Value) -> Result { let id = as_session_id(id)?.to_string(); + if !ctx.foreign_sessions.contains_key(&id) { + return Err(format!("foreign session `{id}` not found")); + } let name = as_text(name, "name")?.to_string(); if !valid_name(&name) { return Err("session_get: name must be an identifier".into()); } let request = serde_json::json!({"id": 1, "op": "get", "name": name}); - let reply = send_and_wait(ctx, &id, &request, Duration::from_secs(10))?; + let reply = send_control(ctx, &id, &request)?; if !reply.get("ok").and_then(|v| v.as_bool()).unwrap_or(false) { return Err(reply .get("error") @@ -779,16 +894,21 @@ pub fn get(ctx: &mut HostContext, id: &Value, name: &Value) -> Result json_to_value(v), None => Ok(Value::None), - } + }?; + super::oracle::admit(&value)?; + Ok(value) } pub fn names(ctx: &mut HostContext, id: &Value) -> Result { let id = as_session_id(id)?.to_string(); + if !ctx.foreign_sessions.contains_key(&id) { + return Err(format!("foreign session `{id}` not found")); + } let request = serde_json::json!({"id": 1, "op": "names"}); - let reply = send_and_wait(ctx, &id, &request, Duration::from_secs(10))?; + let reply = send_control(ctx, &id, &request)?; if !reply.get("ok").and_then(|v| v.as_bool()).unwrap_or(false) { return Err(reply .get("error") @@ -837,7 +957,7 @@ pub fn shutdown(ctx: &mut HostContext) { } } -fn valid_name(name: &str) -> bool { +pub(crate) fn valid_name(name: &str) -> bool { let mut chars = name.chars(); match chars.next() { Some(c) if c == '_' || c.is_ascii_alphabetic() => {} @@ -857,4 +977,301 @@ mod tests { assert!(!valid_name("1x")); assert!(!valid_name("a-b")); } + #[test] + fn bridge_live_boundary_and_refusal_preserves_input_store() { + let edge = 9_007_199_254_740_991i64; + for lang in ["python", "node"] { + let mut ctx = HostContext::default(); + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + let name = Value::Text("payload".into()); + for n in [i64::MIN, -edge-1, -edge, -edge+1, -1, 0, 1, edge-1, edge, edge+1, i64::MAX] { + set(&mut ctx, &id, &name, &Value::Int(17)).unwrap(); + let input = Value::List(vec![Value::Text("中文🧪".into()), Value::List(vec![Value::Int(n)])]); + let result = set(&mut ctx, &id, &name, &input); + if (-edge..=edge).contains(&n) { + result.unwrap(); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), input, "{lang} {n}"); + } else { + assert!(result.unwrap_err().contains("outside V_safe")); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), Value::Int(17)); + } + println!("E4 {lang} integer {n}: verified"); + } + for source in if lang == "python" { + ["2**53", "float('nan')", "float('inf')"] + } else { ["2**53", "NaN", "Infinity"] } { + let error = eval(&mut ctx, &id, None, Some(&Value::Text(source.into())), None, None).unwrap_err(); + assert!(error.contains("outside V_safe"), "{lang} {source}: {error}"); + // A refused response must remain a protocol response, not kill Node. + assert_eq!(get(&mut ctx, &id, &name).unwrap(), Value::Int(17)); + println!("E4 {lang} response {source}: refused, session responsive"); + } + let alias = id.clone(); + close(&mut ctx, &id).unwrap(); + // Missing handle wins over an invalid name and an OOD payload. + let err = set(&mut ctx, &alias, &Value::Text("bad-name".into()), &Value::Int(i64::MAX)).unwrap_err(); + assert!(err.contains("not found"), "{err}"); + assert!(get(&mut ctx, &alias, &name).is_err()); + assert!(close(&mut ctx, &alias).is_err()); + assert!(ctx.foreign_sessions.is_empty()); + } + } + + #[test] + fn generated_two_session_model_traces() { + for lang in ["python", "node"] { + let mut ctx = HostContext::default(); + let mut ids = Vec::new(); + let mut live = [true; 2]; + let mut expected = [0i64; 2]; + let name = Value::Text("counter".into()); + for _ in 0..2 { + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + set(&mut ctx, &id, &name, &Value::Int(0)).unwrap(); + ids.push(id); + } + let mut seed = 0x52455155495245u64; + for step in 0..256 { + seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1); + let slot = ((seed >> 32) & 1) as usize; + let op = (seed >> 40) % 5; + match op { + 0 => { + let value = step as i64 - 128; + let result = set(&mut ctx, &ids[slot], &name, &Value::Int(value)); + assert_eq!(result.is_ok(), live[slot]); + if live[slot] { expected[slot] = value; } + } + 1 => { + let result = get(&mut ctx, &ids[slot], &name); + if live[slot] { assert_eq!(result.unwrap(), Value::Int(expected[slot])); } + else { assert!(result.unwrap_err().contains("not found")); } + } + 2 => { + let src = if lang == "python" { "counter += 1\ncounter" } else { "counter += 1; counter" }; + let result = eval(&mut ctx, &ids[slot], None, Some(&Value::Text(src.into())), None, None); + assert_eq!(result.is_ok(), live[slot]); + if live[slot] { expected[slot] += 1; } + } + 3 => { + assert_eq!(close(&mut ctx, &ids[slot]).is_ok(), live[slot]); + live[slot] = false; + } + _ => { + if live[slot] { close(&mut ctx, &ids[slot]).unwrap(); } + let stale = ids[slot].clone(); + ids[slot] = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + assert_ne!(stale, ids[slot]); + assert!(get(&mut ctx, &stale, &name).is_err()); + set(&mut ctx, &ids[slot], &name, &Value::Int(0)).unwrap(); + expected[slot] = 0; + live[slot] = true; + } + } + // Independent model predicts every remaining store after every command. + for j in 0..2 { + assert_eq!(ctx.foreign_sessions.contains_key(as_session_id(&ids[j]).unwrap()), live[j]); + if live[j] { assert_eq!(get(&mut ctx, &ids[j], &name).unwrap(), Value::Int(expected[j]), "{lang} step {step} slot {j}"); } + } + } + shutdown(&mut ctx); + assert!(ctx.foreign_sessions.is_empty()); + println!("E3 {lang}: seed 0x52455155495245, 256 commands verified"); + } + } + + #[test] + fn failure_process_exit_timeout_and_reopen() { + for lang in ["python", "node"] { + let mut ctx = HostContext::default(); + for source in if lang == "python" { ["import os; os._exit(7)", "while True: pass"] } + else { ["process.exit(7)", "while(true) {}"] } { + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + let result = eval(&mut ctx, &id, None, Some(&Value::Text(source.into())), None, Some(&Value::Num(0.1))); + assert!(result.is_ok(), "process failure should be a failure record: {result:?}"); + let Value::Map(m) = result.unwrap() else { panic!() }; + assert!(m.iter().any(|(k,v)| k == "ok" && v == &Value::Bool(false))); + assert!(ctx.foreign_sessions.is_empty(), "dead session must be retired"); + assert!(get(&mut ctx, &id, &Value::Text("x".into())).is_err()); + println!("E5 {lang} {source}: recorded and retired"); + } + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + set(&mut ctx, &id, &Value::Text("x".into()), &Value::Int(41)).unwrap(); + assert_eq!(get(&mut ctx, &id, &Value::Text("x".into())).unwrap(), Value::Int(41)); + shutdown(&mut ctx); + } + } + + #[test] + fn input_history_and_post_refusal_effects_are_preserved() { + for lang in ["python", "node"] { + let mut ctx = HostContext::default(); + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + let name = Value::Text("n".into()); + set(&mut ctx, &id, &name, &Value::Int(1)).unwrap(); + let code = if lang == "python" { "n += marqdo_input\nn" } else { "n += marqdo_input; n" }; + let result = eval(&mut ctx, &id, None, Some(&Value::Text(code.into())), Some(&Value::Int(3)), None).unwrap(); + let Value::Map(fields) = result else { panic!() }; + assert_eq!(fields.iter().find(|(k,_)| k == "result").unwrap().1, Value::Int(4)); + let sent = ctx.foreign_protocol_steps.last().unwrap(); + assert_eq!(sent.session, as_session_id(&id).unwrap()); + assert_eq!(sent.request["op"], "eval"); + assert_eq!(sent.request["input"], 3); + assert_eq!(sent.request["code"], code); + let before = ctx.foreign_protocol_steps.len(); + assert!(set(&mut ctx, &id, &name, &Value::Int(i64::MAX)).is_err()); + assert_eq!(ctx.foreign_protocol_steps.len(), before, "invalid input must not query oracle"); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), Value::Int(4)); + let bad = if lang == "python" { "n = 99\n2**53" } else { "n = 99; 2**53" }; + assert!(eval(&mut ctx, &id, None, Some(&Value::Text(bad.into())), None, None).unwrap_err().contains("outside V_safe")); + let fault = ctx.foreign_protocol_steps.last().unwrap(); + assert_eq!(fault.response.as_ref().unwrap()["domain_error"], true); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), Value::Int(99), "foreign effects must not be claimed to roll back"); + let ordinary_error = if lang == "python" { "raise ValueError('outside V_safe')" } + else { "throw new Error('outside V_safe')" }; + let record = eval(&mut ctx, &id, None, Some(&Value::Text(ordinary_error.into())), None, None).unwrap(); + let Value::Map(fields) = record else { panic!() }; + assert_eq!(fields.iter().find(|(k,_)| k == "ok").unwrap().1, Value::Bool(false)); + let unsupported = if lang == "python" { "object()" } else { "(function(){})" }; + assert!(eval(&mut ctx, &id, None, Some(&Value::Text(unsupported.into())), None, None).unwrap_err().contains("not JSON-safe")); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), Value::Int(99)); + close(&mut ctx, &id).unwrap(); + println!("E6 {lang}: complete inputs recorded; pre-send refusal has no request; post-send refusal retains effects; structured domain classification verified"); + } + } + + #[test] + fn numeric_extension_policy_agrees_on_python_and_node() { + for lang in ["python", "node"] { + let mut ctx = HostContext::default(); + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + let key = Value::Text("number".into()); + for value in [1.5, -0.0, 9_007_199_254_740_991.0, -9_007_199_254_740_991.0] { + set(&mut ctx, &id, &key, &Value::Num(value)).unwrap(); + match get(&mut ctx, &id, &key).unwrap() { + Value::Num(n) => assert_eq!(n, value), + Value::Int(n) => assert_eq!(n as f64, value), + other => panic!("{lang}: {other:?}"), + } + } + for value in [9_007_199_254_740_992.0, -9_007_199_254_740_992.0, 1e100] { + let before = ctx.foreign_protocol_steps.len(); + assert!(set(&mut ctx, &id, &key, &Value::Num(value)).unwrap_err().contains("outside V_safe")); + assert_eq!(ctx.foreign_protocol_steps.len(), before); + } + let source = if lang == "python" { "float(2**53)" } else { "Number(2**53)" }; + assert!(eval(&mut ctx, &id, None, Some(&Value::Text(source.into())), None, None).unwrap_err().contains("outside V_safe")); + close(&mut ctx, &id).unwrap(); + println!("E7 {lang}: finite fractions admitted, integral float/scientific-notation bypass refused"); + } + } + + #[test] + fn node_non_json_containers_and_lossy_properties_are_refused_without_coercion() { + let mut ctx = HostContext::default(); + let id = open(&mut ctx, &Value::Text("node".into()), None, None, None, None).unwrap(); + let expressions = [ + "new Map([['k',1]])", "new Set([1,2])", "new Date(0)", "/x/", + "new Uint8Array([1,2])", "new Number(7)", + "Object.assign(Object.create({inherited:1}),{own:2})", + "Object.defineProperty({},'hidden',{value:7})", "({[Symbol('k')]:7})", + "Object.defineProperty({},'data',{enumerable:true,get(){retained=123;return 7}})", + "new Array(2)", "Object.assign([1,2],{tag:3})", "[undefined]", + "(()=>{var x={};x.self=x;return x;})()", + ]; + for expression in expressions { + let code = Value::Text(format!("retained = 99; {expression}")); + let result = eval(&mut ctx,&id,None,Some(&code),None,None); + assert!(result.as_ref().is_err_and(|e|e.contains("not JSON-safe")),"silently coerced {expression}: {result:?}"); + let reply=ctx.foreign_protocol_steps.last().unwrap().response.as_ref().unwrap().clone(); + assert_eq!(reply["domain_error"],serde_json::json!(true)); + assert_eq!(get(&mut ctx,&id,&Value::Text("retained".into())).unwrap(),Value::Int(99),"getter must not run during refusal"); + println!("NODE_DOMAIN_REFUSAL:{}",serde_json::json!({"expression":expression,"reply":reply.clone(),"retained":99})); + } + // Null prototypes, special string keys and shared acyclic subvalues remain records/lists. + let code=Value::Text("var item = Object.assign(Object.create(null),{constructor:7}); [item,item]".into()); + let result=eval(&mut ctx,&id,None,Some(&code),None,None).unwrap(); + let Value::Map(fields)=result else {panic!()}; + let observed=&fields.iter().find(|(k,_)|k=="result").unwrap().1; + assert_eq!(observed,&Value::List(vec![Value::Map(vec![("constructor".into(),Value::Int(7))]);2])); + close(&mut ctx,&id).unwrap(); + } + + #[test] + fn recursive_values_and_object_keys_cross_without_coercion() { + fn payload(seed: u64, depth: usize) -> Value { + if depth == 0 { return match seed % 4 { + 0 => Value::None, 1 => Value::Bool(seed & 4 != 0), + 2 => Value::Int((seed % 2001) as i64 - 1000), + _ => Value::Text(format!("中文🧪\\\"\n{}", seed % 10)), + }; } + match seed % 3 { + 0 => Value::List(vec![payload(seed.wrapping_mul(17).wrapping_add(1), depth-1), payload(seed.wrapping_mul(31).wrapping_add(2), depth-1)]), + 1 => Value::Map(vec![("data".into(), payload(seed.wrapping_mul(43).wrapping_add(3), depth-1)), ("tag".into(), Value::Text("record".into()))]), + _ => payload(seed, 0), + } + } + for lang in ["python", "node"] { + let mut ctx = HostContext::default(); + let id = open(&mut ctx, &Value::Text(lang.into()), None, None, None, None).unwrap(); + let name = Value::Text("payload".into()); + for seed in 0..64 { + let v = payload(seed, 4); + set(&mut ctx, &id, &name, &v).unwrap(); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), v, "{lang} seed {seed}"); + } + let special = Value::Map(vec![ + ("__proto__".into(), Value::Map(vec![("polluted".into(), Value::Bool(true))])), + ("constructor".into(), Value::Text("data".into())), + ]); + set(&mut ctx, &id, &name, &special).unwrap(); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), special, "{lang} valid special keys lost"); + for bad in [Value::Map(vec![("a".into(), Value::Int(1)), ("a".into(), Value::Int(2))]), + Value::Map(vec![("a".into(), Value::List(vec![])), ("b".into(), Value::List(vec![Value::Int(1)]))])] { + let before = ctx.foreign_protocol_steps.len(); + assert!(set(&mut ctx, &id, &name, &bad).is_err()); + assert_eq!(ctx.foreign_protocol_steps.len(), before); + assert_eq!(get(&mut ctx, &id, &name).unwrap(), special); + } + if lang == "python" { + let code = Value::Text("{1: 'integer key', '1': 'string key'}".into()); + assert!(eval(&mut ctx, &id, None, Some(&code), None, None).unwrap_err().contains("keys must be strings")); + } else { + let code = Value::Text("typeof ({}).polluted === 'undefined'".into()); + let Value::Map(m) = eval(&mut ctx, &id, None, Some(&code), None, None).unwrap() else { panic!() }; + assert_eq!(m.iter().find(|(k,_)| k == "result").unwrap().1, Value::Bool(true)); + } + close(&mut ctx, &id).unwrap(); + println!("E8 {lang}: 64 nested round-trips, special keys preserved, invalid key/shape inputs refused without requests"); + } + } + + #[cfg(unix)] + #[test] + fn bootstrap_interrupts_busy_eval_and_preserves_idle_state(){ + for lang in ["python","node"]{ + let mut ctx=HostContext::default();let id=open(&mut ctx,&Value::Text(lang.into()),None,None,None,None).unwrap(); + let name=Value::Text("n".into());set(&mut ctx,&id,&name,&Value::Int(8)).unwrap(); + interrupt(&mut ctx,&id).unwrap();assert_eq!(get(&mut ctx,&id,&name).unwrap(),Value::Int(8)); + let marker=ctx.cwd.join(format!("target/interrupt-marker-{}-{lang}",std::process::id()));let _=std::fs::remove_file(&marker); + let literal=serde_json::to_string(&marker.display().to_string()).unwrap(); + let code=if lang=="python"{format!("with open({literal}, 'w') as marker:\n marker.write('ready')\nwhile True: pass")} + else{format!("require('fs').writeFileSync({literal}, 'ready'); while(true) {{}}")}; + let pid=ctx.foreign_sessions[as_session_id(&id).unwrap()].child.id();let child_id=id.clone(); + let worker=std::thread::spawn(move||{let out=eval(&mut ctx,&child_id,None,Some(&Value::Text(code)),None,Some(&Value::Num(3.0)));(ctx,out)}); + let deadline=Instant::now()+Duration::from_secs(2); + while !marker.exists()&&Instant::now(), + evaluating: bool, + ready: bool, + requested: bool, +} + +#[derive(Clone, Debug, Default)] +pub struct InterruptHandle(Arc>); + +impl InterruptHandle { + pub(super) fn new(pid: u32) -> Self { + Self(Arc::new(Mutex::new(State { + pid: Some(pid), + ..State::default() + }))) + } + pub(super) fn retire(&self) -> Option { + if let Ok(mut state) = self.0.lock() { + let pid = state.pid.take(); + state.evaluating = false; + pid + } else { + None + } + } + pub(super) fn try_wait( + &self, + child: &mut std::process::Child, + ) -> std::io::Result> { + let mut state = self.0.lock().unwrap(); + let result = child.try_wait(); + if matches!(result, Ok(Some(_))) { + state.pid = None; + state.evaluating = false; + } + result + } + pub(super) fn begin(&self, evaluating: bool) { + let mut state = self.0.lock().unwrap(); + state.evaluating = evaluating; + state.ready = false; + state.requested = false; + } + pub(super) fn finish(&self) { + self.0.lock().unwrap().evaluating = false; + } + // SIGINT can arrive between a Node ready frame and installation of V8's + // breakOnSigint handler. Retry the same logical request while awaiting its + // response; never carry a retry into the next request or retired process. + pub(super) fn retry(&self) -> Result<(), String> { + let state = self.0.lock().map_err(|_| "interrupt lock poisoned")?; + if state.evaluating && state.ready && state.requested { + Self::signal(&state)?; + } + Ok(()) + } + pub(super) fn ready(&self) -> Result<(), String> { + let mut state = self.0.lock().map_err(|_| "interrupt lock poisoned")?; + if state.ready { + return Ok(()); + } + state.ready = true; + if state.requested { + Self::signal(&state)?; + } + Ok(()) + } + pub fn is_live(&self) -> bool { + self.0.lock().is_ok_and(|s| s.pid.is_some()) + } + + /// False means idle; cancellation is coalesced within one evaluation. + /// Before the bootstrap's ready frame it is queued rather than sent into + /// interpreter startup or transport parsing. + pub fn request(&self) -> Result { + let mut state = self.0.lock().map_err(|_| "interrupt lock poisoned")?; + if state.pid.is_none() { + return Err("interrupt capability retired".into()); + } + if !state.evaluating { + return Ok(false); + } + #[cfg(not(unix))] + return Err("non-destructive interrupt is unsupported on this platform".into()); + if !state.requested { + if state.ready { + Self::signal(&state)?; + } + state.requested = true; + } + Ok(true) + } + fn signal(state: &State) -> Result<(), String> { + let pid = state.pid.ok_or("interrupt capability retired")?; + #[cfg(unix)] + { + if unsafe { libc::kill(-(pid as i32), libc::SIGINT) } != 0 { + return Err(format!( + "interrupt failed: {}", + std::io::Error::last_os_error() + )); + } + Ok(()) + } + #[cfg(not(unix))] + { + let _ = pid; + Err("non-destructive interrupt is unsupported on this platform".into()) + } + } +} + +#[cfg(all(test, unix))] +mod tests { + use crate::host::{foreign_session, HostContext}; + use crate::value::Value; + #[test] + fn retired_interrupt_capability_cannot_reach_replacement_kernel() { + for language in ["python", "node"] { + let mut host = HostContext::default(); + let lang = Value::Text(language.into()); + let old = foreign_session::open(&mut host, &lang, None, None, None, None).unwrap(); + let Value::Handle(id) = &old else { panic!() }; + let control = host.foreign_sessions[id].interrupt_handle.clone(); + assert!(!control.request().unwrap()); + foreign_session::close(&mut host, &old).unwrap(); + let new = foreign_session::open(&mut host, &lang, None, None, None, None).unwrap(); + assert!(!control.is_live()); + assert!(control.request().unwrap_err().contains("retired")); + foreign_session::set(&mut host, &new, &Value::Text("n".into()), &Value::Int(8)) + .unwrap(); + assert_eq!( + foreign_session::get(&mut host, &new, &Value::Text("n".into())).unwrap(), + Value::Int(8) + ); + foreign_session::close(&mut host, &new).unwrap(); + } + } + #[test] + fn cancellation_queued_during_bootstrap_reaches_busy_evaluation() { + use std::time::{Duration, Instant}; + for language in ["python", "node"] { + let mut host = HostContext::default(); + let id = foreign_session::open( + &mut host, + &Value::Text(language.into()), + None, + None, + None, + None, + ) + .unwrap(); + let Value::Handle(ref name) = id else { + panic!() + }; + let control = host.foreign_sessions[name].interrupt_handle.clone(); + let kernel = id.clone(); + let code = if language == "python" { + "while True: pass" + } else { + "while(true) {}" + }; + let worker = std::thread::spawn(move || { + let record = foreign_session::eval( + &mut host, + &kernel, + None, + Some(&Value::Text(code.into())), + None, + Some(&Value::Int(2)), + ) + .unwrap(); + (host, record) + }); + let deadline = Instant::now() + Duration::from_secs(1); + while !control.0.lock().unwrap().evaluating && Instant::now() < deadline { + std::thread::yield_now(); + } + assert!(control.request().unwrap()); + assert!( + control.request().unwrap(), + "logical duplicates must coalesce" + ); + let (mut host, record) = worker.join().unwrap(); + let Value::Map(fields) = record else { panic!() }; + assert_eq!( + fields.iter().find(|(k, _)| k == "timed_out").unwrap().1, + Value::Bool(false), + "{language}: {fields:?}" + ); + assert_eq!( + fields.iter().find(|(k, _)| k == "alive").unwrap().1, + Value::Bool(true) + ); + foreign_session::close(&mut host, &id).unwrap(); + } + } +} diff --git a/src/host/foreign_session/validation.rs b/src/host/foreign_session/validation.rs new file mode 100644 index 0000000..cf86167 --- /dev/null +++ b/src/host/foreign_session/validation.rs @@ -0,0 +1,173 @@ +//! Production adapter to the executable Lean replay policy. +//! All replay decisions come from ReplayPolicy.Cursor, with no Rust fallback. +use super::ProtocolStep; +use serde_json::{json, Value}; +use std::collections::VecDeque; +mod policy; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DivergenceKind { Missing, Request, Reply, Surplus, Phase, Policy } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Divergence { + pub kind: DivergenceKind, + pub matched_prefix: usize, + pub detail: Option, +} +impl std::fmt::Display for Divergence { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self.kind { + DivergenceKind::Missing | DivergenceKind::Request => "replay: protocol request differs before dispatch", + DivergenceKind::Reply => "replay: protocol response differs", + DivergenceKind::Surplus => "replay: recorded execution prefix was not consumed", + DivergenceKind::Phase => "replay: invalid protocol validation phase", + DivergenceKind::Policy => "replay: verified policy unavailable", + })?; + if let Some(detail) = &self.detail { write!(f, ": {detail}")? } + Ok(()) + } +} +#[derive(Debug)] +pub struct ReplayCursor { + client: Option, + progress: (usize, usize), + stopped: Option, +} +impl From> for ReplayCursor { + fn from(expected: VecDeque) -> Self { + let count = expected.len(); + match policy::Client::start(expected) { + Ok((client, reply)) if reply.allow && reply.matched == 0 && reply.remaining == count => + Self { client: Some(client), progress: (0, count), stopped: None }, + result => { + let detail = match result { Err(e) => e, Ok(_) => "invalid policy initialization".into() }; + Self { client: None, progress: (0,count), stopped: Some(Divergence { + kind: DivergenceKind::Policy, matched_prefix: 0, detail: Some(detail) }) } + } + } + } +} +impl From> for ReplayCursor { + fn from(expected: Vec) -> Self { VecDeque::from(expected).into() } +} +/// Private construction, no Clone: actual dispatch consumes this snapshot. +pub(super) struct DispatchPermit { session: String, request: Value } +impl DispatchPermit { + pub(super) fn session(&self) -> &str { &self.session } + pub(super) fn request(&self) -> &Value { &self.request } +} +impl ReplayCursor { + fn unavailable(&mut self, detail: String) -> Divergence { + self.client.take(); + let error = Divergence { kind: DivergenceKind::Policy, matched_prefix: self.progress.0, detail: Some(detail) }; + self.stopped = Some(error.clone()); + error + } + fn command(&mut self, command: Value) -> Result<(), Divergence> { + if let Some(error) = &self.stopped { return Err(error.clone()) } + let result = self.client.as_mut().ok_or_else(|| "no policy process".to_string()) + .and_then(|client| client.exchange(command)); + let reply = result.map_err(|e| self.unavailable(e))?; + self.progress = (reply.matched, reply.remaining); + if reply.allow { return Ok(()) } + let kind = match reply.reason.as_deref() { + Some("missing") => DivergenceKind::Missing, + Some("request") => DivergenceKind::Request, + Some("reply") => DivergenceKind::Reply, + Some("surplus") => DivergenceKind::Surplus, + Some("phase") => DivergenceKind::Phase, + _ => return Err(self.unavailable("unknown policy stop reason".into())), + }; + let error = Divergence { kind, matched_prefix: reply.matched, detail: None }; + self.stopped = Some(error.clone()); + Err(error) + } + fn prepare(&mut self, session: &str, request: &Value) -> Result { + self.command(json!({"op":"prepare", "value":policy::request_key(session,request)}))?; + Ok(DispatchPermit { session: session.into(), request: request.clone() }) + } + pub(super) fn accept_response(&mut self, response: &Result) -> Result<(), Divergence> { + self.command(json!({"op":"receive", "value":policy::response_key(response)})) + } + pub fn progress(&self) -> (usize,usize) { self.progress } + /// Called only at the outer action boundary; consumes the cursor. + pub fn finish(mut self) -> Result<(),Divergence> { self.command(json!({"op":"finish"})) } +} +pub(super) fn prepare(cursor: Option<&mut ReplayCursor>, session: &str, request: &Value) + -> Result { + if let Some(cursor) = cursor { return cursor.prepare(session,request) } + Ok(DispatchPermit { session: session.into(), request: request.clone() }) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + fn step(reply: Result) -> ProtocolStep { + ProtocolStep { + session: "s".into(), + request: json!({"op":"get"}), + response: reply, + } + } + #[test] + fn local_returns_and_ordinary_failures_preserve_residual_history() { + let mut cursor: ReplayCursor = + vec![step(Err("foreign failure".into())), step(Ok(json!(7)))].into(); + assert_eq!(cursor.progress(), (0, 2)); + let permit = cursor.prepare("s", &json!({"op":"get"})).unwrap(); + assert_eq!(permit.session(), "s"); + cursor + .accept_response(&Err("foreign failure".into())) + .unwrap(); + assert_eq!(cursor.progress(), (1, 1)); + cursor.prepare("s", &json!({"op":"get"})).unwrap(); + cursor.accept_response(&Ok(json!(7))).unwrap(); + cursor.finish().unwrap(); + } + #[test] + fn divergence_is_sticky_and_matching_reply_cannot_revive_it() { + let mut cursor: ReplayCursor = vec![step(Ok(json!(7)))].into(); + cursor.prepare("s", &json!({"op":"get"})).unwrap(); + let first = cursor.accept_response(&Ok(json!(8))).unwrap_err(); + assert_eq!(first.kind, DivergenceKind::Reply); + assert_eq!( + cursor.prepare("s", &json!({"op":"get"})).err().unwrap(), + first + ); + assert_eq!(cursor.accept_response(&Ok(json!(7))).unwrap_err(), first); + assert_eq!(cursor.finish().unwrap_err(), first); + } + #[test] + fn permit_is_a_snapshot_and_phase_errors_fail_closed() { + let mut request = json!({"op":"get"}); + let mut cursor: ReplayCursor = vec![step(Ok(json!(7)))].into(); + let permit = cursor.prepare("s", &request).unwrap(); + request["op"] = json!("write"); + assert_ne!(permit.request(), &request); + assert_eq!( + cursor.prepare("s", permit.request()).err().unwrap().kind, + DivergenceKind::Phase + ); + assert!(cursor.accept_response(&Ok(json!(7))).is_err()); + } + #[test] + fn mismatching_and_missing_requests_never_issue_a_permit() { + let mut cursor: ReplayCursor = vec![step(Ok(json!(7)))].into(); + let error = cursor + .prepare("other-session", &json!({"op":"get"})) + .err() + .unwrap(); + assert_eq!(error.kind, DivergenceKind::Request); + assert_eq!(cursor.progress(), (0, 1)); + assert_eq!( + cursor.prepare("s", &json!({"op":"get"})).err().unwrap(), + error + ); + let mut empty: ReplayCursor = Vec::::new().into(); + assert_eq!( + empty.prepare("s", &json!({"op":"get"})).err().unwrap().kind, + DivergenceKind::Missing + ); + assert!(empty.finish().is_err()); + } +} diff --git a/src/host/foreign_session/validation/policy.rs b/src/host/foreign_session/validation/policy.rs new file mode 100644 index 0000000..ffdf116 --- /dev/null +++ b/src/host/foreign_session/validation/policy.rs @@ -0,0 +1,92 @@ +//! Transport for the executable Lean policy. This module makes no replay +//! acceptance decisions; malformed/unavailable policy responses fail closed. +use super::super::ProtocolStep; +use serde::Deserialize; +use serde_json::{json, Value}; +use std::io::{BufRead, BufReader, Write}; +use std::path::PathBuf; +use std::process::{Child, ChildStdin, Command, Stdio}; +use std::sync::mpsc::{self, Receiver}; +use std::time::Duration; + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct Reply { + pub policy: String, + pub allow: bool, + pub matched: usize, + pub remaining: usize, + pub reason: Option, +} + +#[derive(Debug)] +pub(super) struct Client { + child: Child, + input: ChildStdin, + replies: Receiver>, +} + +pub(super) fn request_key(session: &str, request: &Value) -> String { + serde_json::to_string(&(session,request.to_string())).expect("strings serialize") +} +pub(super) fn response_key(response: &Result) -> String { + serde_json::to_string(response).expect("protocol responses serialize") +} + +fn executable() -> PathBuf { + if let Some(path) = std::env::var_os("MARQDO_REPLAY_POLICY") { return path.into() } + if let Ok(exe) = std::env::current_exe() { + if let Some(parent) = exe.parent() { + let adjacent = parent.join("marqdo-replay-policy"); + if adjacent.is_file() { return adjacent } + } + } + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("verification/replay-policy/.lake/build/bin/replayPolicy") +} + +impl Client { + pub(super) fn start(steps: impl IntoIterator) -> Result<(Self,Reply),String> { + let path=executable(); + let mut child=Command::new(&path).stdin(Stdio::piped()).stdout(Stdio::piped()) + .stderr(Stdio::null()).spawn().map_err(|e|format!("{}: {e}",path.display()))?; + let input=child.stdin.take().ok_or("policy stdin unavailable")?; + let output=child.stdout.take().ok_or("policy stdout unavailable")?; + let (sender,replies)=mpsc::channel(); + std::thread::spawn(move || { + for line in BufReader::new(output).lines() { + let reply=line.map_err(|e|e.to_string()) + .and_then(|line|serde_json::from_str(&line).map_err(|e|e.to_string())); + let failed=reply.is_err(); + if sender.send(reply).is_err() || failed { break } + } + }); + let steps: Vec<_>=steps.into_iter().map(|step|json!({ + "q":request_key(&step.session,&step.request),"r":response_key(&step.response) + })).collect(); + let mut client=Self {child,input,replies}; + let reply=client.exchange(json!({"op":"init","steps":steps}))?; + Ok((client,reply)) + } + + pub(super) fn exchange(&mut self, command: Value) -> Result { + serde_json::to_writer(&mut self.input,&command).map_err(|e|e.to_string())?; + self.input.write_all(b"\n").map_err(|e|e.to_string())?; + self.input.flush().map_err(|e|e.to_string())?; + let reply=self.replies.recv_timeout(Duration::from_secs(30)) + .map_err(|e|format!("policy response: {e}"))??; + if reply.policy != include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/verification/replay-policy/POLICY-SHA256")).trim() { + return Err("policy executable does not match the compiled source identity".into()) + } + if reply.allow == reply.reason.is_some() { + return Err("policy response has inconsistent verdict and reason".into()) + } + Ok(reply) + } +} +impl Drop for Client { + fn drop(&mut self) { + let _=self.child.kill(); + let _=self.child.wait(); + } +} diff --git a/src/host/json.rs b/src/host/json.rs index 88f0df2..344da93 100644 --- a/src/host/json.rs +++ b/src/host/json.rs @@ -16,14 +16,95 @@ fn ood_int_err(n: impl std::fmt::Display) -> String { format!("json: integer outside V_safe (±(2^53-1)): {n}") } +/// The integer boundary is semantic, not a property of JSON spelling or the +/// host's Int/Num tag. Exponent notation must not bypass it. +fn checked_number(n: f64) -> Result { + if !n.is_finite() { return Err("json: non-finite number outside V_safe".into()); } + if n.fract() == 0.0 && n.abs() > MAX_SAFE_INT as f64 { return Err(ood_int_err(n)); } + Ok(serde_json::Value::Number(serde_json::Number::from_f64(n) + .ok_or("json: non-finite number outside V_safe")?)) +} + +#[derive(Debug)] +pub(crate) enum DecodeError { Syntax(String), Domain(String) } + +struct BoundaryValue(Value); +impl<'de> serde::Deserialize<'de> for BoundaryValue { + fn deserialize>(de: D) -> Result { + struct BoundaryVisitor; + impl<'de> serde::de::Visitor<'de> for BoundaryVisitor { + type Value = BoundaryValue; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.write_str("a boundary JSON value") } + fn visit_unit(self) -> Result { Ok(BoundaryValue(Value::None)) } + fn visit_bool(self, v: bool) -> Result { Ok(BoundaryValue(Value::Bool(v))) } + fn visit_i64(self, n: i64) -> Result { + if !in_safe_int(n) { return Err(E::custom(ood_int_err(n))); } + Ok(BoundaryValue(Value::Int(n))) + } + fn visit_u64(self, n: u64) -> Result { + if n > MAX_SAFE_INT as u64 { return Err(E::custom(ood_int_err(n))); } + Ok(BoundaryValue(Value::Int(n as i64))) + } + fn visit_f64(self, n: f64) -> Result { + checked_number(n).map_err(E::custom)?; + Ok(BoundaryValue(Value::Num(n))) + } + fn visit_str(self, v: &str) -> Result { Ok(BoundaryValue(Value::Text(v.into()))) } + fn visit_string(self, v: String) -> Result { Ok(BoundaryValue(Value::Text(v))) } + fn visit_seq>(self, mut seq: A) -> Result { + let mut xs = Vec::new(); + while let Some(BoundaryValue(v)) = seq.next_element()? { xs.push(v); } + Ok(BoundaryValue(Value::List(xs))) + } + fn visit_map>(self, mut map: A) -> Result { + use serde::de::Error; + let mut fields = Vec::new(); + let mut keys = std::collections::HashSet::new(); + while let Some(key) = map.next_key::()? { + if !keys.insert(key.clone()) { return Err(A::Error::custom("json: duplicate object key outside V_safe")); } + let BoundaryValue(v) = map.next_value()?; + fields.push((key, v)); + } + // Preserve the pre-existing JSON object's canonical key order. + fields.sort_by(|a, b| a.0.cmp(&b.0)); + let value = Value::Map(fields); + value_to_json(&value).map_err(A::Error::custom)?; + Ok(BoundaryValue(value)) + } + } + de.deserialize_any(BoundaryVisitor) + } +} + +pub(crate) fn decode_text(text: &str) -> Result { + use serde::Deserialize; + let classify = |error: serde_json::Error| { + let message = error.to_string(); + // serde_json categorizes its own numeric-overflow diagnostic as Syntax. + // It is a domain refusal for a grammar-valid unbounded JSON number. + // Match this trusted decoder diagnostic, never foreign program output. + if message.starts_with("number out of range at line ") { + return DecodeError::Domain(format!("json: number outside V_safe: {message}")); + } + match error.classify() { + serde_json::error::Category::Data => DecodeError::Domain(message), + _ => DecodeError::Syntax(message), + } + }; + let mut de = serde_json::Deserializer::from_str(text); + let value = BoundaryValue::deserialize(&mut de).map_err(classify)?; + de.end().map_err(classify)?; + Ok(value.0) +} + pub fn parse(text: &Value) -> Result { let s = match text { Value::Text(s) => s.as_str(), _ => return Err("json parse needs text".into()), }; - let v: serde_json::Value = - serde_json::from_str(s).map_err(|e| format!("json parse: {e}"))?; - json_to_value(&v) + decode_text(s).map_err(|e| match e { + DecodeError::Syntax(e) | DecodeError::Domain(e) => format!("json parse: {e}"), + }) } pub fn stringify(value: &Value, indent: Option<&Value>) -> Result { @@ -93,6 +174,7 @@ pub(crate) fn json_to_value(v: &serde_json::Value) -> Result { return Err(ood_int_err(u)); } else if let Some(f) = n.as_f64() { // Host extension: non-integer JSON numbers → Num (not in Lean SafeVal). + checked_number(f)?; Value::Num(f) } else { return Err(format!("json: unsupported number: {n}")); @@ -126,9 +208,7 @@ pub(crate) fn value_to_json(v: &Value) -> Result { } serde_json::Value::Number((*n).into()) } - Value::Num(n) => serde_json::Number::from_f64(*n) - .map(serde_json::Value::Number) - .unwrap_or(serde_json::Value::Null), + Value::Num(n) => checked_number(*n)?, Value::Text(s) => serde_json::Value::String(s.clone()), Value::List(xs) => { let mut arr = Vec::with_capacity(xs.len()); @@ -169,7 +249,7 @@ pub(crate) fn value_to_json(v: &Value) -> Result { cells.push( serde_json::Number::from_f64(n) .map(serde_json::Value::Number) - .unwrap_or(serde_json::Value::Null), + .ok_or_else(|| "json: non-finite number outside V_safe".to_string())?, ); } arr.push(serde_json::Value::Array(cells)); @@ -181,7 +261,7 @@ pub(crate) fn value_to_json(v: &Value) -> Result { if let crate::formula::Expr::Num(n) = s { serde_json::Number::from_f64(n) .map(serde_json::Value::Number) - .unwrap_or(serde_json::Value::Null) + .ok_or_else(|| "json: non-finite number outside V_safe".to_string())? } else { serde_json::Value::String(e.as_display()) } @@ -302,4 +382,40 @@ mod tests { let err = value_to_json(&ragged).unwrap_err(); assert!(err.contains("arity mismatch"), "{err}"); } + #[test] + fn refuse_nonfinite_in_nested_payloads() { + for n in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + for v in [Value::Num(n), Value::List(vec![Value::Num(n)]), + Value::Map(vec![("x".into(), Value::Num(n))])] { + assert!(value_to_json(&v).unwrap_err().contains("outside V_safe")); + } + } + } + + #[test] + fn exponent_and_float_tags_cannot_bypass_integer_domain() { + for text in ["9007199254740992.0", "9.007199254740992e15", "1e100", "1e9999", "-9.007199254740992e15"] { + assert!(parse(&Value::Text(text.into())).unwrap_err().contains("outside V_safe"), "{text}"); + } + for n in [9_007_199_254_740_992.0, -9_007_199_254_740_992.0, 1e100] { + assert!(value_to_json(&Value::List(vec![Value::Num(n)])).unwrap_err().contains("outside V_safe")); + } + for text in ["1.5", "-0.0", "9.007199254740991e15"] { + assert!(parse(&Value::Text(text.into())).is_ok(), "valid extension {text}"); + } + } + + #[test] + fn strict_decode_refuses_duplicate_keys_and_ragged_columns() { + for text in [r#"{"a":1,"a":2}"#, r#"[{"x":null,"x":true}]"#, + r#"{"a":[1],"b":[2,3]}"#] { + assert!(matches!(decode_text(text), Err(DecodeError::Domain(_))), "{text}"); + } + for text in [r#"{"a":[1],"b":"metadata"}"#, r#"{"a":[],"b":[]}"#, + r#"{"__proto__":{"safe":true},"constructor":"data"}"#] { + assert!(decode_text(text).is_ok(), "{text}"); + } + assert!(matches!(decode_text("not JSON"), Err(DecodeError::Syntax(_)))); + } + } diff --git a/src/host/mod.rs b/src/host/mod.rs index 380f30b..ef041cf 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -23,6 +23,7 @@ mod hash; mod html_ops; pub(crate) mod json; mod log; +pub(crate) mod oracle; pub mod math; mod net; mod path; @@ -106,6 +107,7 @@ pub struct HostContext { /// LCG state for `random` / `random_int`. pub rng: u64, pub plots: Vec, + pub oracle_observations: Vec, /// lang → interpreter argv (from `set_cmd`). pub foreign_cmds: HashMap>, /// Process-local language registrations (`register` / `set_cmd`). @@ -120,6 +122,12 @@ pub struct HostContext { /// Monotonic session id counter. #[cfg(feature = "exec-host")] pub foreign_session_seq: u64, + #[cfg(feature = "exec-host")] + pub foreign_protocol_steps: Vec, + #[cfg(feature = "exec-host")] + pub foreign_replay_expected: Option, + #[cfg(feature = "exec-host")] + pub foreign_replay_divergence: Option, /// Loaded native plugins (ABI v1/v2). pub plugins: PluginState, /// Entry `.mq.md` source text (for ABI host_query / context injection). @@ -154,6 +162,7 @@ impl Default for HostContext { sleep_limit_ms: Some(30_000), rng: 0xC0FF_EE42_CAFE_BABE, plots: Vec::new(), + oracle_observations: Vec::new(), foreign_cmds: HashMap::new(), foreign_specs: HashMap::new(), foreign_config_specs: HashMap::new(), @@ -162,6 +171,12 @@ impl Default for HostContext { foreign_sessions: HashMap::new(), #[cfg(feature = "exec-host")] foreign_session_seq: 0, + #[cfg(feature = "exec-host")] + foreign_protocol_steps: Vec::new(), + #[cfg(feature = "exec-host")] + foreign_replay_expected: None, + #[cfg(feature = "exec-host")] + foreign_replay_divergence: None, plugins: PluginState::default(), entry_source: None, entry_path: None, diff --git a/src/host/oracle.rs b/src/host/oracle.rs new file mode 100644 index 0000000..1ff4a3e --- /dev/null +++ b/src/host/oracle.rs @@ -0,0 +1,136 @@ +//! Document-level observations, separate from process timing and execution logs. +use crate::value::Value; +use super::HostContext; + +pub(crate) fn admit(value: &Value) -> Result<(), String> { + match value { + Value::None | Value::Bool(_) | Value::Text(_) => Ok(()), + Value::Int(_) => super::json::value_to_json(value).map(|_| ()), + Value::Num(_) => super::json::value_to_json(value).map(|_| ()), + Value::List(xs) => { for x in xs { admit(x)?; } Ok(()) } + Value::Map(xs) => { + super::json::value_to_json(value)?; + let mut keys = std::collections::HashSet::new(); + for (key, x) in xs { + if !keys.insert(key) { return Err("oracle: duplicate record field".into()); } + admit(x)?; + } + Ok(()) + } + _ => Err("oracle: value outside V_safe".into()), + } +} + +pub(crate) fn append(ctx: &mut HostContext, channel: &str, value: &Value) -> Result<(), String> { + admit(value)?; + ctx.oracle_observations.push(Value::Map(vec![ + ("index".into(), Value::Int(ctx.oracle_observations.len() as i64)), + ("channel".into(), Value::Text(channel.into())), + ("value".into(), value.clone()), + ])); + Ok(()) +} + +/// Project a process result to its semantic observation. Success observes the +/// returned payload. Failure observes stable diagnostics, never elapsed time, +/// command paths or opaque handles. Both document and live runners use this. +pub(crate) fn eval_observation(record: &Value) -> Result { + let Value::Map(fields) = record else { return Err("oracle: expected eval record".into()) }; + let get = |key: &str| fields.iter().find(|(k, _)| k == key).map(|(_,v)|v.clone()).unwrap_or(Value::None); + let value = match get("ok") { + Value::Bool(true) => get("result"), + Value::Bool(false) => { + let source = get("diagnostic_source"); + Value::Map(["ok", "code", "stdout", "stderr", "error", "timed_out", "alive"] + .into_iter().map(|key| { + let value = get(key); + // Alpha-rename only this execution's allocated source path, + // only in diagnostics. Keep raw output and ordinary paths intact. + let value = match (key, value, &source) { + ("stderr" | "error", Value::Text(text), Value::Text(path)) if !path.is_empty() => + Value::Text(text.replace(path, "")), + (_, value, _) => value, + }; + (key.to_string(), value) + }).collect()) + }, + _ => return Err("oracle: eval record missing boolean ok".into()), + }; + admit(&value)?; + Ok(value) +} + +fn validate(journal: &Value) -> Result<&[Value], String> { + let Value::List(rows) = journal else { return Err("oracle journal must be a list".into()) }; + for (i, row) in rows.iter().enumerate() { + let Value::Map(fields) = row else { return Err(format!("oracle step {i}: expected record")) }; + if fields.len() != 3 { return Err(format!("oracle step {i}: expected index/channel/value")); } + let get = |k: &str| fields.iter().find(|(key,_)| key == k).map(|(_,v)|v); + if get("index") != Some(&Value::Int(i as i64)) { return Err(format!("oracle step {i}: invalid index")); } + if !matches!(get("channel"), Some(Value::Text(s)) if ["eval", "get", "decode"].contains(&s.as_str())) { + return Err(format!("oracle step {i}: invalid channel")); + } + admit(get("value").ok_or_else(|| format!("oracle step {i}: missing value"))?)?; + } + Ok(rows) +} + +/// Pure structural equality on admitted journals; reports the least differing index. +pub(crate) fn replay_check(expected: &Value, observed: &Value) -> Result { + let a = validate(expected)?; + let b = validate(observed)?; + // Map field order is not part of record identity (matches JSON object semantics). + let canonical = |v: &Value| super::json::value_to_json(v); + let mut mismatch = None; + for i in 0..a.len().max(b.len()) { + if a.get(i).map(canonical).transpose()? != b.get(i).map(canonical).transpose()? { + mismatch = Some(i); + break; + } + } + let index = mismatch.map(|i| Value::Int(i as i64)).unwrap_or(Value::None); + Ok(Value::Map(vec![ + ("ok".into(), Value::Bool(mismatch.is_none())), + ("index".into(), index), + ("expected".into(), mismatch.and_then(|i| a.get(i)).cloned().unwrap_or(Value::None)), + ("observed".into(), mismatch.and_then(|i| b.get(i)).cloned().unwrap_or(Value::None)), + ])) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn oracle_journal_structural_mismatch_and_length() { + let mut ctx = HostContext::default(); + append(&mut ctx, "eval", &Value::Int(3)).unwrap(); + append(&mut ctx, "get", &Value::Int(2)).unwrap(); + let journal = Value::List(ctx.oracle_observations.clone()); + let result = replay_check(&journal, &journal).unwrap(); + assert_eq!(super::super::json::get(&result, &Value::Text("ok".into())).unwrap(), Value::Bool(true)); + let short = Value::List(ctx.oracle_observations[..1].to_vec()); + let result = replay_check(&journal, &short).unwrap(); + assert_eq!(super::super::json::get(&result, &Value::Text("index".into())).unwrap(), Value::Int(1)); + assert!(append(&mut ctx, "eval", &Value::List(vec![Value::Handle("x".into())])).is_err()); + let code = Value::Code(crate::value::CodeBlock { lang: "python".into(), source: "1".into() }); + assert!(admit(&Value::List(vec![code])).is_err()); + assert_eq!(ctx.oracle_observations.len(), 2); + } + #[test] + fn diagnostic_projection_renames_only_allocated_source_identity() { + let make = |path: &str, message: &str| Value::Map(vec![ + ("ok".into(), Value::Bool(false)), + ("stderr".into(), Value::Text(format!("File {path}: {message}; input /data/original.csv"))), + ("diagnostic_source".into(), Value::Text(path.into())), + ]); + let a = make("/tmp/allocated-a.py", "ValueError"); + let b = make("/tmp/allocated-b.py", "ValueError"); + assert_ne!(a, b, "raw diagnostics must survive"); + assert_eq!(eval_observation(&a).unwrap(), eval_observation(&b).unwrap()); + let c = make("/tmp/allocated-b.py", "TypeError"); + assert_ne!(eval_observation(&a).unwrap(), eval_observation(&c).unwrap()); + let d = make("/tmp/allocated-b.py", "ValueError /data/changed.csv"); + assert_ne!(eval_observation(&a).unwrap(), eval_observation(&d).unwrap()); + } + +} diff --git a/src/host/plugin.rs b/src/host/plugin.rs index 14e0be0..ce4af87 100644 --- a/src/host/plugin.rs +++ b/src/host/plugin.rs @@ -194,6 +194,17 @@ impl Clone for PluginState { } impl PluginState { + /// Exercise the real JSON/C ABI without loading an external shared library. + #[cfg(test)] + pub(crate) fn register_binding_probe(&mut self) { + unsafe extern "C" fn echo(args: *const c_char, out: *mut *mut c_char, _err: *mut *mut c_char) -> c_int { + if args.is_null() || out.is_null() { return 1; } + let Ok(input)=CStr::from_ptr(args).to_str() else { return 1; }; + *out=host_strdup(input); + if (*out).is_null() {1} else {0} + } + self.fns.insert("binding_probe".into(),RegisteredFn{params:vec!["a".into(),"b".into()],fn_ptr:echo}); + } pub fn get(&self, name: &str) -> Option<&RegisteredFn> { self.fns.get(name) } diff --git a/src/inherit.rs b/src/inherit.rs index b761145..ae2c511 100644 --- a/src/inherit.rs +++ b/src/inherit.rs @@ -1,107 +1,101 @@ //! Object inheritance: `# Child = > Parent` validation and helpers. -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use anyhow::Result; use crate::ast::{Function, Module}; -use crate::diagnostics::{Diagnostic, Span}; +use crate::diagnostics::Diagnostic; + +/// Import binding paths identify module instances, including repeated imports +/// of the same file under different bindings. The tree remains immutable. +pub(crate) fn module_namespace(root:&Module, wanted:&Module) -> Option> { + if std::ptr::eq(root,wanted) { return Some(Vec::new()); } + let mut imports=root.import_modules.iter().collect::>(); + imports.sort_by_key(|(name,_)| *name); + for (bind,module) in imports { + if let Some(mut path)=module_namespace(module,wanted) { + path.insert(0,bind.clone()); return Some(path); + } + } + None +} + +pub(crate) fn object_at_namespace<'a>(root:&'a Module,path:&[String],name:&str) -> Option<(&'a Module,&'a Function)> { + let mut owner=root; + for segment in path { owner=owner.import_modules.get(segment)?; } + let object=owner.functions.iter().find(|fun|fun.is_object() && fun.name==name)?; + Some((owner,object)) +} + +/// Bare types prefer their defining module. An imported short-name binding +/// resolves its explicit path; a legacy imported bare name must be unique. +pub(crate) fn resolve_object<'a>(owner:&'a Module,name:&str) -> Result<(&'a Module,&'a Function),String> { + if let Some(alias)=owner.uses.iter().find(|binding|binding.bind==name) { + let Some((class,path))=alias.path.split_last() else { return Err("empty object alias".into()); }; + return object_at_namespace(owner,path,class).ok_or_else(||format!("unknown object type `{name}`")); + } + if let Some(object)=owner.functions.iter().find(|fun|fun.is_object() && fun.name==name) { return Ok((owner,object)); } + fn imported<'a>(module:&'a Module,name:&str,out:&mut Vec<(&'a Module,&'a Function)>) { + let mut imports=module.import_modules.iter().collect::>();imports.sort_by_key(|(name,_)|*name); + for (_,child) in imports { + out.extend(child.functions.iter().filter(|fun|fun.is_object() && fun.name==name).map(|fun|(child,fun))); + imported(child,name,out); + } + } + let mut candidates=Vec::new(); imported(owner,name,&mut candidates); + match candidates.as_slice() { + [only]=>Ok(*only), + []=>Err(format!("unknown object type `{name}`")), + _=>Err(format!("ambiguous object type `{name}`; use an explicit import binding")), + } +} /// Validate that every object's `base` resolves and that the inheritance graph is acyclic. pub fn validate_inheritance(module: &Module) -> Result<()> { - let mut by_name: HashMap)> = HashMap::new(); - collect_objects(module, &mut by_name); - // Deterministic order: source line, then name (stable gold diagnostics). - let mut names: Vec = by_name.keys().cloned().collect(); - names.sort_by(|a, b| { - let (sa, _) = &by_name[a]; - let (sb, _) = &by_name[b]; - sa.line.cmp(&sb.line).then_with(|| a.cmp(b)) - }); - - for name in &names { - let (span, base) = &by_name[name]; - let Some(base) = base else { continue }; - if !by_name.contains_key(base) { - return Err(Diagnostic::new( - None, - *span, - format!("unknown base type `{base}` for object `{name}`"), - ) - .into()); - } + fn modules<'a>(module: &'a Module, out: &mut Vec<&'a Module>) { + out.push(module); + let mut imports=module.import_modules.iter().collect::>(); + imports.sort_by_key(|(name,_)| *name); + for (_, child) in imports { modules(child,out); } } - - for name in &names { - let (span, base) = &by_name[name]; - if base.is_none() { - continue; - } - let mut path = Vec::new(); - let mut cur = name.clone(); - loop { - if path.iter().any(|p| p == &cur) { - path.push(cur); - return Err(Diagnostic::new( - None, - *span, - format!( - "cyclic inheritance involving `{name}` ({})", - path.join(" → ") - ), - ) - .into()); + let mut all=Vec::new(); modules(module,&mut all); + for owner in all { + for object in owner.functions.iter().filter(|fun|fun.is_object()) { + let mut current_owner=owner; + let mut current=object; + let mut seen=HashSet::new(); + let mut chain=Vec::new(); + loop { + chain.push(current.name.clone()); + if !seen.insert((current_owner as *const Module,current as *const Function)) { + return Err(Diagnostic::new(owner.source.path.as_deref(),object.span, + format!("cyclic inheritance involving `{}` ({})",object.name,chain.join(" → "))) + .with_source(&owner.source).into()); + } + let Some(base)=¤t.base else { break; }; + (current_owner,current)=resolve_object(current_owner,base).map_err(|message| { + Diagnostic::new(current_owner.source.path.as_deref(),current.span, + format!("{} base type `{base}` for object `{}`: {message}",if message.starts_with("ambiguous") { "ambiguous" } else { "unknown" },current.name)) + .with_source(¤t_owner.source) + })?; } - path.push(cur.clone()); - let Some((_, Some(next))) = by_name.get(&cur) else { - break; - }; - cur = next.clone(); } } Ok(()) } -fn collect_objects(module: &Module, out: &mut HashMap)>) { - for fun in &module.functions { - if fun.is_object() { - out.entry(fun.name.clone()) - .or_insert((fun.span, fun.base.clone())); - } - } - for lib in module.import_modules.values() { - collect_objects(lib, out); - } -} - /// Walk the base chain starting at `type_name` (inclusive). pub fn inheritance_chain<'a>(module: &'a Module, type_name: &str) -> Vec<&'a Function> { - let mut out = Vec::new(); - let mut cur = type_name.to_string(); - let mut seen = HashSet::new(); - while seen.insert(cur.clone()) { - let Some(obj) = find_object(module, &cur) else { - break; - }; - out.push(obj); - match &obj.base { - Some(b) => cur = b.clone(), - None => break, - } + let mut out=Vec::new(); + let Ok((mut owner,mut object))=resolve_object(module,type_name) else { return out; }; + let mut seen=HashSet::new(); + while seen.insert((owner as *const Module,object as *const Function)) { + out.push(object); + let Some(base)=&object.base else { break; }; + let Ok(next)=resolve_object(owner,base) else { break; }; + (owner,object)=next; } out } -fn find_object<'a>(module: &'a Module, name: &str) -> Option<&'a Function> { - module - .functions - .iter() - .find(|f| f.name == name && f.is_object()) - .or_else(|| { - module.import_modules.values().find_map(|lib| { - lib.functions - .iter() - .find(|f| f.name == name && f.is_object()) - }) - }) -} diff --git a/src/interp.rs b/src/interp.rs index 1f3fc41..0e7309e 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -6,7 +6,6 @@ use std::path::{Path, PathBuf}; use crate::ast::{ Arg, BinaryOp, CallExpr, Expr, Function, IndexKey, InterpPart, Literal, Module, Param, Stmt, - UnaryOp, }; use crate::builtin::{ builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join, @@ -14,7 +13,7 @@ use crate::builtin::{ builtin_type, }; use crate::debug::{emit_trace, DebugController}; -use crate::diagnostics::{bail_at, Span}; +use crate::diagnostics::{Diagnostic, DiagnosticFrame, SourceIdentity, Span}; use crate::host::{call_host, HostContext}; use crate::input_feed::InputFeed; use crate::capture::{ @@ -39,6 +38,9 @@ pub struct Interpreter { /// Active loops: `(loop_id, iter_index_0based)`. loop_stack: Vec<(u32, u32)>, current_span: Span, + current_source: SourceIdentity, + current_module: Option<*const Module>, + diagnostic_frames: Vec, input: InputFeed, host: HostContext, debug: Option>, @@ -100,6 +102,9 @@ impl Interpreter { next_loop_id: 1, loop_stack: Vec::new(), current_span: Span::new(1, 1), + current_source: SourceIdentity { path: path.map(Path::to_path_buf), sha256: None }, + current_module: None, + diagnostic_frames: Vec::new(), input: InputFeed::new(false, Vec::new()), host: HostContext::for_run(path, Default::default(), Vec::new()), debug: None, @@ -121,6 +126,9 @@ impl Interpreter { next_loop_id: 1, loop_stack: Vec::new(), current_span: Span::new(1, 1), + current_source: SourceIdentity { path: path.map(Path::to_path_buf), sha256: None }, + current_module: None, + diagnostic_frames: Vec::new(), input: InputFeed::new(true, Vec::new()), host: HostContext::for_capture(path, Default::default()), debug: None, @@ -147,14 +155,36 @@ impl Interpreter { std::mem::take(&mut self.host.plots) } + #[cfg(all(test, feature = "exec-host"))] + pub(crate) fn foreign_source_evidence(&mut self) -> (Vec, Vec, Vec) { + crate::source_refinement_tests::boundary_evidence(&mut self.host) + } + + #[cfg(all(test, feature = "exec-host"))] + pub(crate) fn application_evidence(&self) -> (Vec, Vec, usize) { + (self.host.oracle_observations.clone(), self.host.foreign_protocol_steps.clone(), self.host.foreign_sessions.len()) + } + + #[cfg(all(test, feature = "exec-host"))] + pub(crate) fn causal_replay_evidence(&mut self) -> serde_json::Value { + crate::causal_replay_tests::evidence(&mut self.host) + } + + #[cfg(test)] + pub(crate) fn diagnostic_scope_state(&self) -> (SourceIdentity, usize, Vec, Vec, bool) { + (self.current_source.clone(), self.diagnostic_frames.len(), self.host.call_stack.clone(), + self.host.call_site_lines.clone(), self.current_module.is_some()) + } + fn err(&self, message: impl Into) -> anyhow::Error { - bail_at(self.path.as_deref(), self.current_span, message) + Diagnostic::new(self.current_source.path.as_deref(), self.current_span, message) + .with_source(&self.current_source).with_frames(&self.diagnostic_frames).into() } fn err_code(&self, code: &str, message: impl Into) -> anyhow::Error { anyhow::Error::new( - crate::diagnostics::Diagnostic::new(self.path.as_deref(), self.current_span, message) - .with_code(code), + Diagnostic::new(self.current_source.path.as_deref(), self.current_span, message) + .with_source(&self.current_source).with_frames(&self.diagnostic_frames).with_code(code), ) } @@ -367,7 +397,11 @@ impl Interpreter { fun: &Function, args: &[(String, Value)], ) -> Result { - self.run_function(module, fun, Env::new(), args, false) + let previous=self.site_module; + self.site_module=Some(module as *const Module); + let result=self.run_function(module, fun, Env::new(), args, false); + self.site_module=previous; + result } /// Bind named arguments for an external call. Unknown names, missing @@ -377,6 +411,19 @@ impl Interpreter { module: &Module, fun: &Function, args: &[(String, Value)], + ) -> Result> { + let previous=self.site_module; + self.site_module=Some(module as *const Module); + let result=self.in_function_source(module, fun, |this| this.bind_external_args_inner(module, fun, args)); + self.site_module=previous; + result + } + + fn bind_external_args_inner( + &mut self, + module: &Module, + fun: &Function, + args: &[(String, Value)], ) -> Result> { self.current_span = fun.span; self.host.current_line = fun.span.line; @@ -403,7 +450,9 @@ impl Interpreter { .collect(); let mut env = Env::new(); let bound = bind_function_args(self, module, fun, &mut env, &fun.params, &ev, false) - .map_err(|m| { + .map_err(|error| { + if Diagnostic::find(&error).is_some() { return error; } + let m = error.to_string(); if m.starts_with("missing argument") { self.err_code("call.missing_arg", m) } else if m.starts_with("duplicate named argument") { @@ -417,7 +466,7 @@ impl Interpreter { c, &fun.name, &bound, - self.path.as_deref(), + self.current_source.path.as_deref(), fun.span, ) { return Err(d.into()); @@ -432,6 +481,10 @@ impl Interpreter { Ok(out) } + fn argument_error(&self, error: anyhow::Error) -> anyhow::Error { + if Diagnostic::find(&error).is_some() { error } else { self.err(error.to_string()) } + } + /// Call a function using (and updating) [`Self::entry_bindings`] from `# main`. /// Used by browser WASM sessions so handlers share state with the entry object. pub fn invoke_in_entry( @@ -688,6 +741,59 @@ impl Interpreter { } fn run_function( + &mut self, + module: &Module, + fun: &Function, + env: Env, + args: &[(String, Value)], + capture_entry_env: bool, + ) -> Result { + self.in_function_source(module, fun, |this| { + this.run_function_inner(module, fun, env, args, capture_entry_env) + }) + } + + fn in_function_source( + &mut self, + module: &Module, + fun: &Function, + run: impl FnOnce(&mut Self) -> Result, + ) -> Result { + let mut source = module.source.clone(); + if source.path.is_none() { + if self.diagnostic_frames.is_empty() { + source.path = self.path.clone(); + } else if self.current_module == Some(module as *const Module) { + source.path = self.current_source.path.clone(); + } + } + let caller = (!self.diagnostic_frames.is_empty()).then(|| self.current_source.at(self.current_span)); + let previous_source = std::mem::replace(&mut self.current_source, source); + let previous_module = self.current_module.replace(module as *const Module); + let previous_span = std::mem::replace(&mut self.current_span, fun.span); + let previous_line = self.host.current_line; + self.diagnostic_frames.push(DiagnosticFrame { + function: fun.name.clone(), declaration: self.current_source.at(fun.span), call_site: caller, + }); + let result = run(self) + .map_err(|error| { + if let Some(mut diagnostic) = Diagnostic::find(&error).cloned() { + if diagnostic.source_sha256.is_none() && diagnostic.path == self.current_source.path { + diagnostic.source_sha256 = self.current_source.sha256.clone(); + } + if diagnostic.frames.is_empty() { diagnostic.frames = self.diagnostic_frames.clone(); } + diagnostic.into() + } else { self.err(error.to_string()) } + }); + self.diagnostic_frames.pop(); + self.current_source = previous_source; + self.current_module = previous_module; + self.current_span = previous_span; + self.host.current_line = previous_line; + result + } + + fn run_function_inner( &mut self, module: &Module, fun: &Function, @@ -697,7 +803,7 @@ impl Interpreter { ) -> Result { if self.trace { emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(fun.span), "enter_fn", &[("fn", fun.name.as_str())], @@ -706,12 +812,23 @@ impl Interpreter { for (k, v) in args { env.set(k.clone(), v.clone()); } + // Raw/named entry APIs and document metadata must use the same default + // semantics as ordinary calls. Already bound defaults are supplied + // values here and therefore never execute a second time. + let supplied=env.vars.iter().map(|(name,value)|EvArg::Named(name.clone(),value.clone())).collect::>(); + let completed=bind_function_args(self,module,fun,&mut env,&fun.params,&supplied,true)?; + for (name,value) in completed { env.set(name,value); } for p in &fun.params { if env.get(&p.name).is_none() { env.set(p.name.clone(), Value::None); } } + if let Some(contract)=&fun.contract { + if let Some(diag)=crate::contract::call_arg_diag(contract,&fun.name,&env.vars,self.current_source.path.as_deref(),fun.span) { + return Err(diag.into()); + } + } let ret = Value::None; self.push_view_call(fun, &env); self.host.push_call_frame(&fun.name); @@ -732,7 +849,7 @@ impl Interpreter { if self.trace { let display = ret.as_display(); emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(fun.span), "leave_fn", &[("fn", fun.name.as_str()), ("value", display.as_str())], @@ -762,7 +879,7 @@ impl Interpreter { // 契约边界 3(T2.2):表绑定 vs 字段契约。 if let Some(c) = fun.var_contracts.get(name) { if let Some(d) = - crate::contract::fields_diag(c, name, &v, self.path.as_deref(), *span) + crate::contract::fields_diag(c, name, &v, self.current_source.path.as_deref(), *span) { return Err(d.into()); } @@ -770,7 +887,7 @@ impl Interpreter { if self.trace { let display = v.as_display(); emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[ @@ -801,7 +918,7 @@ impl Interpreter { // 契约边界 2(T2.2):返回值 vs 返回契约。 if let Some(c) = &fun.contract { if let Some(d) = - crate::contract::return_diag(c, &fun.name, &v, self.path.as_deref(), *span) + crate::contract::return_diag(c, &fun.name, &v, self.current_source.path.as_deref(), *span) { return Err(d.into()); } @@ -809,7 +926,7 @@ impl Interpreter { if self.trace { let display = v.as_display(); emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[("kind", "return"), ("value", display.as_str())], @@ -833,7 +950,7 @@ impl Interpreter { self.current_span = *span; if self.trace { emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[("kind", "call"), ("callee", call.callee.as_str())], @@ -865,7 +982,7 @@ impl Interpreter { self.current_span = *span; if self.trace { emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[("kind", "expr")], @@ -901,7 +1018,7 @@ impl Interpreter { self.current_span = *span; if self.trace { emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[("kind", "branch")], @@ -934,7 +1051,7 @@ impl Interpreter { self.current_span = *span; if self.trace { emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[("kind", "while")], @@ -946,7 +1063,10 @@ impl Interpreter { None }; let mut guard = 0u32; - while self.eval_expr(module, fun, env, condition)?.truthy() { + loop { + self.current_span=*span; + self.host.current_line=span.line; + if !self.eval_expr(module,fun,env,condition)?.truthy(){break;} guard += 1; if guard > 1_000_000 { return Err(self.err("while loop exceeded iteration limit")); @@ -984,7 +1104,7 @@ impl Interpreter { self.current_span = *span; if self.trace { emit_trace( - self.path.as_deref(), + self.current_source.path.as_deref(), Some(*span), "stmt", &[ @@ -1113,15 +1233,8 @@ impl Interpreter { Ok(Value::Text(s)) } Expr::Unary { op, expr } => { - let v = self.eval_expr(module, fun, env, expr)?; - match op { - UnaryOp::Not => Ok(Value::Bool(!v.truthy())), - UnaryOp::Neg => match v { - Value::Int(n) => Ok(Value::Int(-n)), - Value::Num(n) => Ok(Value::Num(-n)), - _ => Err(self.err("unary `-` needs int or num")), - }, - } + let value=self.eval_expr(module,fun,env,expr)?; + crate::builtin::eval_unary(*op,&value).map_err(|message|self.err(message)) } Expr::Binary { op, left, right } => { let l = self.eval_expr(module, fun, env, left)?; @@ -1153,7 +1266,7 @@ impl Interpreter { { return Ok(v); } - eval_binary(*op, &l, &r).map_err(|e| self.err(e.to_string())) + crate::builtin::eval_binary(*op, &l, &r).map_err(|e| self.err(e.to_string())) } Expr::Call(call) => self.eval_call(module, fun, env, call), Expr::List(items) => { @@ -1182,7 +1295,7 @@ impl Interpreter { name, &key, &Value::None, - self.path.as_deref(), + self.current_source.path.as_deref(), self.current_span, ) { return Err(d.into()); @@ -1197,7 +1310,7 @@ impl Interpreter { name, &key, &out, - self.path.as_deref(), + self.current_source.path.as_deref(), self.current_span, ) { return Err(d.into()); @@ -1359,21 +1472,21 @@ impl Interpreter { fn eval_bound_function_call( &mut self, module: &Module, - caller: &Function, - env: &mut Env, + _caller: &Function, + _env: &mut Env, target: &Function, ev_args: &[EvArg], ) -> Result { let bound = - bind_function_args(self, module, caller, env, &target.params, ev_args, false) - .map_err(|m| self.err(m))?; + bind_function_args(self, module, target, &mut Env::new(), &target.params, ev_args, false) + .map_err(|e| self.argument_error(e))?; if let Some(c) = &target.contract { - if let Some(d) = crate::contract::call_arg_diag( + if let Some(d) = crate::contract::call_arg_diag_in( c, &target.name, &bound, - self.path.as_deref(), - self.current_span, + &self.current_source.at(self.current_span), + &module.source, ) { return Err(d.into()); } @@ -1387,7 +1500,7 @@ impl Interpreter { self.host.pop_call_site_line(); let result = result?; if target.is_object() { - Ok(tag_instance(&target.name, result)) + Ok(crate::value::tag_instance(&target.name, &self.object_module_path(module), result)) } else { Ok(result) } @@ -1520,7 +1633,6 @@ impl Interpreter { ev_args: &[EvArg], ) -> Result { let (lib, node) = locate_import_function(module, path).map_err(|m| self.err(m))?; - let display = path.join("."); let bound = { let mut dummy_env = Env::new(); @@ -1533,16 +1645,16 @@ impl Interpreter { ev_args, false, ) - .map_err(|m| self.err(m))? + .map_err(|e| self.argument_error(e))? }; // 契约边界 1(T2.2):库路径调用的实参 vs 形参契约。 if let Some(c) = &node.contract { - if let Some(d) = crate::contract::call_arg_diag( + if let Some(d) = crate::contract::call_arg_diag_in( c, - &display, + &node.name, &bound, - self.path.as_deref(), - self.current_span, + &self.current_source.at(self.current_span), + &lib.source, ) { return Err(d.into()); } @@ -1557,7 +1669,7 @@ impl Interpreter { self.host.pop_call_site_line(); let result = result?; if node.is_object() { - Ok(tag_instance(&node.name, result)) + Ok(crate::value::tag_instance(&node.name, &self.object_module_path(lib), result)) } else { Ok(result) } @@ -1566,7 +1678,7 @@ impl Interpreter { fn eval_method_call( &mut self, module: &Module, - fun: &Function, + _fun: &Function, env: &mut Env, recv_name: &str, method: &str, @@ -1577,12 +1689,19 @@ impl Interpreter { .cloned() .ok_or_else(|| self.err(format!("undefined variable `{recv_name}`")))?; let type_name = instance_type_name(&recv).map_err(|m| self.err(m))?; - if find_object_type(module, &type_name).is_none() { - return Err(self.err(format!("unknown object type `{type_name}`"))); - } - let (owner, _obj, target) = find_method_along_bases(module, &type_name, method) + // The active root is immutable for this invocation; site_module is + // installed/restored by the public run/call entry points. + let root=unsafe { self.site_module.map(|root| &*root) }.unwrap_or(module); + let namespace=crate::value::instance_module(&recv).map_err(|m|self.err(m))?; + let (class_owner,class)=if let Some(namespace)=namespace { + crate::inherit::object_at_namespace(root,&namespace,&type_name) + .ok_or_else(||self.err(format!("unknown object type `{type_name}` in module `{}`",namespace.join("."))))? + } else { + crate::inherit::resolve_object(module,&type_name).map_err(|m|self.err(m))? + }; + let (owner, _obj, target) = find_method_along_bases(class_owner, class, method) .ok_or_else(|| { - let chain = crate::inherit::inheritance_chain(module, &type_name) + let chain = crate::inherit::inheritance_chain(class_owner, &type_name) .into_iter() .map(|o| o.name.as_str()) .collect::>() @@ -1591,16 +1710,19 @@ impl Interpreter { "unknown method `{type_name}.{method}` (searched {chain})" )) })?; - let bound = bind_function_args(self, module, fun, env, &target.params, ev_args, false) - .map_err(|m| self.err(m))?; + let mut receiver_env = Env::new(); + receiver_env.set("self".into(), recv.clone()); + receiver_env.set("自".into(), recv.clone()); + let bound = bind_function_args(self, owner, target, &mut receiver_env, &target.params, ev_args, false) + .map_err(|e| self.argument_error(e))?; // 契约边界 1(T2.2):方法调用的实参 vs 形参契约。 if let Some(c) = &target.contract { - if let Some(d) = crate::contract::call_arg_diag( + if let Some(d) = crate::contract::call_arg_diag_in( c, &target.name, &bound, - self.path.as_deref(), - self.current_span, + &self.current_source.at(self.current_span), + &owner.source, ) { return Err(d.into()); } @@ -1616,11 +1738,16 @@ impl Interpreter { self.host.pop_call_site_line(); let result = result?; if crate::linalg_ops::is_matrix_value(&recv) { - Ok(crate::linalg_ops::retag_matrix_like(&type_name, result)) + Ok(crate::value::preserve_instance_module(&recv,crate::linalg_ops::retag_matrix_like(&type_name, result))) } else { Ok(result) } } + + fn object_module_path(&self, module:&Module) -> Vec { + let root=unsafe { self.site_module.map(|root| &*root) }.unwrap_or(module); + crate::inherit::module_namespace(root,module).unwrap_or_default() + } } fn instance_type_name(v: &Value) -> std::result::Result { @@ -1637,23 +1764,6 @@ fn instance_type_name(v: &Value) -> std::result::Result { } } -fn tag_instance(type_name: &str, value: Value) -> Value { - match value { - Value::Map(mut entries) => { - // Always stamp the constructed type so explicit `` `self` = > Parent `` - // in a child constructor still yields the most specific `_type`. - entries.retain(|(k, _)| k != "_type"); - entries.insert(0, ("_type".into(), Value::Text(type_name.into()))); - Value::Map(entries) - } - Value::None => Value::Map(vec![("_type".into(), Value::Text(type_name.into()))]), - other => Value::Map(vec![ - ("_type".into(), Value::Text(type_name.into())), - ("value".into(), other), - ]), - } -} - /// `lib.member` / `lib.Type.member` against frontmatter imports. Does not execute. pub(crate) fn locate_import_function<'a>( module: &'a Module, @@ -1696,12 +1806,12 @@ pub(crate) fn locate_import_function<'a>( fn bind_function_args( interp: &mut Interpreter, module: &Module, - caller: &Function, - caller_env: &mut Env, + target: &Function, + initial_env: &mut Env, params: &[Param], args: &[EvArg], allow_missing: bool, -) -> std::result::Result, String> { +) -> Result> { let mut named: HashMap = HashMap::new(); let mut positionals = Vec::new(); for a in args { @@ -1709,7 +1819,7 @@ fn bind_function_args( EvArg::Positional(v) => positionals.push(v.clone()), EvArg::Named(k, v) => { if named.contains_key(k) { - return Err(format!("duplicate named argument `{k}`")); + return Err(anyhow::anyhow!("duplicate named argument `{k}`")); } named.insert(k.clone(), v.clone()); } @@ -1719,6 +1829,13 @@ fn bind_function_args( let mut out: HashMap = HashMap::new(); let mut used_named = HashSet::new(); let mut pos_i = 0usize; + let mut defaults_env = Env::new(); + // Only an explicit receiver crosses into a method's default environment. + for name in ["self", "自"] { + if let Some(value) = initial_env.get(name) { + defaults_env.set(name.into(), value.clone()); + } + } for p in params { if let Some(v) = named.get(&p.name) { @@ -1727,26 +1844,44 @@ fn bind_function_args( } else if pos_i < positionals.len() { out.insert(p.name.clone(), positionals[pos_i].clone()); pos_i += 1; - } else if let Some(def) = &p.default { - let v = interp - .eval_expr(module, caller, caller_env, def) - .map_err(|e| e.to_string())?; - out.insert(p.name.clone(), v); - } else if !allow_missing { - return Err(format!("missing argument for parameter `{}`", p.name)); + } else if p.default.is_none() && !allow_missing { + return Err(anyhow::anyhow!("missing argument for parameter `{}`", p.name)); } } - if pos_i < positionals.len() { - return Err(format!( - "too many positional arguments ({} extra)", - positionals.len() - pos_i - )); + return Err(anyhow::anyhow!("too many positional arguments ({} extra)", positionals.len() - pos_i)); } - for (k, v) in named { - if !used_named.contains(&k) && !out.contains_key(&k) { - out.insert(k, v); + if !used_named.contains(&k) && !out.contains_key(&k) { out.insert(k, v); } + } + for (name, value) in &out { defaults_env.set(name.clone(), value.clone()); } + for name in ["self", "自"] { + if let Some(value)=initial_env.get(name) { defaults_env.set(name.into(),value.clone()); } + } + for p in params { + if out.contains_key(&p.name) { continue; } + if let Some(def) = &p.default { + let mut evaluate=|this:&mut Interpreter| { + let previous_span=this.current_span; + let previous_line=this.host.current_line; + this.current_span=p.span; + this.host.current_line=p.span.line; + let result=this.eval_expr(module,target,&mut defaults_env,def); + this.current_span=previous_span; + this.host.current_line=previous_line; + result + }; + let v = if interp.current_module == Some(module as *const Module) + && interp.diagnostic_frames.last().is_some_and(|frame| frame.declaration.span == target.span) + { + evaluate(interp)? + } else { + interp.in_function_source(module, target, evaluate)? + }; + out.insert(p.name.clone(), v); + } + if let Some(value) = out.get(&p.name) { + defaults_env.set(p.name.clone(), value.clone()); } } @@ -1759,50 +1894,9 @@ fn bind_args( args: &[EvArg], allow_missing: bool, ) -> std::result::Result, String> { - let mut named: HashMap = HashMap::new(); - let mut positionals = Vec::new(); - for a in args { - match a { - EvArg::Positional(v) => positionals.push(v.clone()), - EvArg::Named(k, v) => { - if named.contains_key(k) { - return Err(format!("duplicate named argument `{k}`")); - } - named.insert(k.clone(), v.clone()); - } - } - } - - let mut out: HashMap = HashMap::new(); - let mut used_named = HashSet::new(); - let mut pos_i = 0usize; - - for p in params { - if let Some(v) = named.get(p) { - out.insert(p.clone(), v.clone()); - used_named.insert(p.clone()); - } else if pos_i < positionals.len() { - out.insert(p.clone(), positionals[pos_i].clone()); - pos_i += 1; - } else if !allow_missing { - return Err(format!("missing argument for parameter `{p}`")); - } - } - - if pos_i < positionals.len() { - return Err(format!( - "too many positional arguments ({} extra)", - positionals.len() - pos_i - )); - } - - for (k, v) in named { - if !used_named.contains(&k) && !out.contains_key(&k) { - out.insert(k, v); - } - } - - Ok(out) + let names=args.iter().map(|a|match a{EvArg::Positional(_)=>None,EvArg::Named(name,_)=>Some(name.clone())}).collect::>(); + let values=args.iter().map(|a|match a{EvArg::Positional(value)|EvArg::Named(_,value)=>value.clone()}).collect::>(); + crate::arguments::bind(params,&names,&values,allow_missing) } fn collect_decl_reads(expr: &Expr, out: &mut std::collections::HashSet) { @@ -1870,49 +1964,21 @@ fn find_top<'a>(module: &'a Module, name: &str) -> Option<&'a Function> { } /// Resolve `#` object type by name in the entry module or any imported library. -fn find_object_type<'a>(module: &'a Module, name: &str) -> Option<(&'a Module, &'a Function)> { - fn search<'a>( - module: &'a Module, - name: &str, - seen: &mut std::collections::HashSet<*const Module>, - ) -> Option<(&'a Module, &'a Function)> { - let key = module as *const Module; - if !seen.insert(key) { - return None; - } - if let Some(f) = module - .functions - .iter() - .find(|f| f.name == name && f.is_object()) - { - return Some((module, f)); - } - for lib in module.import_modules.values() { - if let Some(hit) = search(lib, name, seen) { - return Some(hit); - } - } - None - } - let mut seen = std::collections::HashSet::new(); - search(module, name, &mut seen) -} - /// Resolve `` `obj`.method `` along the inheritance chain (`_type` first, then bases). fn find_method_along_bases<'a>( module: &'a Module, - type_name: &str, + object: &'a Function, method: &str, ) -> Option<(&'a Module, &'a Function, &'a Function)> { - let mut cur = type_name.to_string(); + let mut owner=module; + let mut obj=object; let mut seen = std::collections::HashSet::new(); - while seen.insert(cur.clone()) { - let (owner, obj) = find_object_type(module, &cur)?; + while seen.insert((owner as *const Module,obj as *const Function)) { if let Some(target) = obj.children.iter().find(|c| c.name == method) { return Some((owner, obj, target)); } match &obj.base { - Some(b) => cur = b.clone(), + Some(base) => { (owner,obj)=crate::inherit::resolve_object(owner,base).ok()?; }, None => break, } } @@ -1964,71 +2030,6 @@ fn lit_to_value(lit: &Literal) -> Value { } } -fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result { - match op { - BinaryOp::Add => match (l, r) { - (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a + b)), - (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a + b)), - (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 + b)), - (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a + *b as f64)), - (Value::Text(a), Value::Text(b)) => Ok(Value::Text(format!("{a}{b}"))), - (Value::Text(a), Value::Secret(b)) => Ok(Value::Text(format!("{a}{b}"))), - (Value::Secret(a), Value::Text(b)) => Ok(Value::Text(format!("{a}{b}"))), - (Value::Secret(a), Value::Secret(b)) => Ok(Value::Text(format!("{a}{b}"))), - (Value::Text(a), b) => Ok(Value::Text(format!("{a}{}", b.as_display()))), - (a, Value::Text(b)) => Ok(Value::Text(format!("{}{b}", a.as_display()))), - _ => bail!("`+` needs ints, nums, or text"), - }, - BinaryOp::Sub => match (l, r) { - (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)), - (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a - b)), - (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 - b)), - (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a - *b as f64)), - _ => bail!("`-` needs ints or nums"), - }, - BinaryOp::Mul => match (l, r) { - (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a * b)), - (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a * b)), - (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 * b)), - (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a * *b as f64)), - _ => bail!("`*` needs ints or nums"), - }, - BinaryOp::Div => match (l, r) { - (Value::Int(_), Value::Int(0)) => bail!("division by zero"), - (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a / b)), - (Value::Num(_), Value::Num(b)) if *b == 0.0 => bail!("division by zero"), - (Value::Int(_), Value::Num(b)) if *b == 0.0 => bail!("division by zero"), - (Value::Num(_), Value::Int(0)) => bail!("division by zero"), - (Value::Num(a), Value::Num(b)) => Ok(Value::Num(a / b)), - (Value::Int(a), Value::Num(b)) => Ok(Value::Num(*a as f64 / b)), - (Value::Num(a), Value::Int(b)) => Ok(Value::Num(a / *b as f64)), - _ => bail!("`/` needs ints or nums"), - }, - BinaryOp::Eq => Ok(Value::Bool(l == r)), - BinaryOp::Ne => Ok(Value::Bool(l != r)), - BinaryOp::Lt => cmp_ord(l, r, |o| o.is_lt()), - BinaryOp::Le => cmp_ord(l, r, |o| o.is_le()), - BinaryOp::Gt => cmp_ord(l, r, |o| o.is_gt()), - BinaryOp::Ge => cmp_ord(l, r, |o| o.is_ge()), - BinaryOp::And | BinaryOp::Or => unreachable!("handled above"), - } -} - -fn cmp_ord(l: &Value, r: &Value, f: impl Fn(std::cmp::Ordering) -> bool) -> Result { - match (l, r) { - (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(f(a.cmp(b)))), - (Value::Num(a), Value::Num(b)) => Ok(Value::Bool(f(a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)))), - (Value::Int(a), Value::Num(b)) => { - Ok(Value::Bool(f((*a as f64).partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)))) - } - (Value::Num(a), Value::Int(b)) => { - Ok(Value::Bool(f(a.partial_cmp(&(*b as f64)).unwrap_or(std::cmp::Ordering::Equal)))) - } - (Value::Text(a), Value::Text(b)) => Ok(Value::Bool(f(a.cmp(b)))), - _ => bail!("comparison needs two ints/nums or two texts"), - } -} - fn brief_trace_val(v: &Value) -> String { let s = v.as_display(); if s.chars().count() > 24 { diff --git a/src/lib.rs b/src/lib.rs index 3f6ef63..3241128 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ //! CLI also offers `view` for AST-backed browsing. pub mod aliases; +mod arguments; pub mod artifact; pub mod label; pub mod migrate; @@ -45,6 +46,16 @@ pub mod project; pub mod repair; pub mod sema; pub mod value; +#[cfg(all(test, feature = "exec-host"))] +mod source_refinement_tests; +#[cfg(all(test, feature = "exec-host"))] +mod application_tests; +#[cfg(all(test, feature = "exec-host"))] +mod causal_replay_tests; +#[cfg(test)] +mod source_map_tests; +#[cfg(test)] +mod frontend_elaboration_tests; #[cfg(feature = "net-host")] pub mod version_check; #[cfg(feature = "view")] diff --git a/src/load.rs b/src/load.rs index 1ca4bb7..1552a04 100644 --- a/src/load.rs +++ b/src/load.rs @@ -26,10 +26,22 @@ pub fn load_module_with_ctx(path: &Path, ctx: &BindingContext) -> Result load_module_inner(path, &mut visited, ctx) } +/// Load recorded entry text at its original path. Imports and metadata retain +/// normal file-relative resolution; the user's source file is never rewritten. +pub(crate) fn load_module_with_source(path: &Path, source: &str, ctx: &BindingContext) -> Result { + load_module_inner_source(path, &mut HashSet::new(), ctx, Some(source)) +} + fn load_module_inner( path: &Path, visited: &mut HashSet, ctx: &BindingContext, +) -> Result { + load_module_inner_source(path, visited, ctx, None) +} + +fn load_module_inner_source( + path: &Path, visited: &mut HashSet, ctx: &BindingContext, source_override: Option<&str>, ) -> Result { let canon = path.canonicalize().unwrap_or_else(|_| path.to_path_buf()); if !visited.insert(canon.clone()) { @@ -37,11 +49,14 @@ fn load_module_inner( } let result = (|| { - let source = read_module_source(path) - .with_context(|| format!("failed to read {}", path.display()))?; + let source = match source_override { + Some(source) => source.to_string(), + None => read_module_source(path).with_context(|| format!("failed to read {}", path.display()))?, + }; let markup = frontmatter_markup(&source); let mut module = crate::parse::expr::with_markup(markup, || parse_source(&source)) .map_err(|e| attach_path(path, e))?; + module.source.path = Some(path.to_path_buf()); // Resolve Artifact Metadata bindings (entry and imports share the same ctx). let file_ctx = BindingContext { @@ -343,6 +358,7 @@ fn load_module_from_source_inner( let markup = frontmatter_markup(source); let mut module = crate::parse::expr::with_markup(markup, || parse_source(source)) .map_err(|e| label_error(label, e))?; + module.source.path = Some(PathBuf::from(label)); module.metadata = resolve_metadata(&module.metadata_raw, ctx, Span::new(1, 1)) .map_err(|e| label_error(label, e))?; let imports = module.imports.clone(); @@ -429,4 +445,22 @@ mod tests { "{msg}" ); } + #[test] + fn recorded_source_resolves_relative_imports_without_rewriting_files() { + let root=std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let dir=root.join(format!("target/load-recorded-{}-{}",std::process::id(),std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos())); + std::fs::create_dir_all(&dir).unwrap();let entry=dir.join("main.mq.md"); + let original="# main\n\n> print text=original\n"; + std::fs::write(&entry,original).unwrap();std::fs::write(dir.join("dep.mq.md"),"# helper\n\n> print text=dependency\n").unwrap(); + let recorded="---\nimport:\n dep: dep.mq.md\n---\n# main\n\n> print text=recorded\n"; + let ctx=BindingContext::from_binds(&[],cwd_for_path(Some(&entry))); + let module=load_module_with_source(&entry,recorded,&ctx).unwrap(); + assert!(module.import_modules.contains_key("dep")); + assert_eq!(std::fs::read_to_string(&entry).unwrap(),original); + let cyclic="---\nimport:\n again: main.mq.md\n---\n# main\n"; + assert!(load_module_with_source(&entry,cyclic,&ctx).unwrap_err().to_string().contains("circular import")); + assert_eq!(std::fs::read_to_string(&entry).unwrap(),original); + std::fs::remove_dir_all(dir).unwrap(); + } + } diff --git a/src/parse/expr.rs b/src/parse/expr.rs index 15c3791..3ef18b1 100644 --- a/src/parse/expr.rs +++ b/src/parse/expr.rs @@ -508,6 +508,20 @@ impl<'a> Parser<'a> { }); } if self.eat("-") { + // The magnitude of i64::MIN is not itself a positive i64 literal. + // Recognize its signed token before recursively parsing a unary + // operand; decimal literals keep their floating-point type. + let start = self.i; + self.skip_ws(); + let digits = self.rest().bytes().take_while(u8::is_ascii_digit).count(); + let end = self.i + digits; + if &self.src[self.i..end] == "9223372036854775808" + && !self.src[end..].starts_with(['.', 'e', 'E']) + { + self.i = end; + return Ok(Expr::Literal(Literal::Int(i64::MIN))); + } + self.i = start; let expr = self.parse_unary()?; return Ok(Expr::Unary { op: UnaryOp::Neg, diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 87f0dfd..bc28e8f 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -26,7 +26,9 @@ use crate::parse::expr::parse_expr as parse_expr_str; /// Parse full source (with optional frontmatter) into a module AST. pub fn parse_source(source: &str) -> Result { let lines = classify_source(source); - parse_classified(&lines) + let mut module = parse_classified(&lines)?; + module.source = crate::diagnostics::SourceIdentity::from_source(None, source); + Ok(module) } pub fn parse_classified(lines: &[ClassifiedLine]) -> Result { @@ -63,14 +65,17 @@ pub fn parse_classified(lines: &[ClassifiedLine]) -> Result { bail!("import bind `{}` conflicts with top-level name", u.bind); } } - Ok(Module { + let module=Module { + source: Default::default(), imports, uses, functions, import_modules: std::collections::HashMap::new(), metadata_raw, metadata: Vec::new(), - }) + }; + crate::ast::validate_parameters(&module)?; + Ok(module) } struct Cursor<'a> { @@ -320,7 +325,7 @@ impl<'a> Cursor<'a> { if self.peek_is_prose() { break; } - if let Some(param) = parse_param_line(trimmed) { + if let Some(param) = parse_param_line(trimmed, Span::new(l.line_no,1)) { if fun.body.is_empty() { fun.params.push(param); self.bump(); @@ -1278,7 +1283,7 @@ fn parse_backtick_ident(s: &str) -> Option { Some(name.to_string()) } -fn parse_param_line(trimmed: &str) -> Option { +fn parse_param_line(trimmed: &str, span:Span) -> Option { use crate::ast::Param; let rest = trimmed.strip_prefix('+')?.trim(); if rest.is_empty() || !rest.starts_with('`') { @@ -1303,6 +1308,7 @@ fn parse_param_line(trimmed: &str) -> Option { }; Some(Param { name, + span, default, inferred: false, }) @@ -2104,6 +2110,17 @@ fn merge_inferred_params( let mut needs_input = HashSet::new(); let mut bound = HashSet::new(); let mut assign_decls = Vec::new(); + // Defaults execute before the body. Reads used only there still require + // declared inputs; later body assignments cannot satisfy those reads. + for param in &fun.params { + if let Some(default)=¶m.default { collect_reads_expr(default,&mut needs_input); } + } + for (_,default,_) in prose_decls { + if let Some(default)=default { + let parsed=parse_expr_prefer_var(default).or_else(|_|parse_value_or_interp(default))?; + collect_reads_expr(&parsed,&mut needs_input); + } + } collect_names_needing_input( &fun.body, &mut bound, @@ -2131,7 +2148,7 @@ fn merge_inferred_params( } events.sort_by_key(|e| (e.0, e.1)); - for (_line, _ord, name, default_txt) in events { + for (line, _ord, name, default_txt) in events { if name == "self" || name == "自" { continue; } @@ -2153,6 +2170,7 @@ fn merge_inferred_params( }; fun.params.push(Param { name, + span:Span::new(line,1), default, inferred: true, }); diff --git a/src/sema/bind.rs b/src/sema/bind.rs index ce97a22..b8ce769 100644 --- a/src/sema/bind.rs +++ b/src/sema/bind.rs @@ -5,8 +5,6 @@ //! dynamics are marked separately. use crate::ast::{CallExpr, Function, Module}; -use crate::diagnostics::Span; -use crate::host::HostFn; /// Why a call site is not a static BoundCall. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -26,31 +24,7 @@ impl DynamicReason { } } -/// Runtime resolution shared by Tree (and mirrored by Bytecode rules). -#[derive(Debug)] -pub enum ExecTarget<'a> { - /// Function in the calling module (or nested children). - Local(&'a Function), - /// `bind.member…` through `module.import_modules`. - Import { - bind: String, - lib: &'a Module, - fun: &'a Function, - path: Vec, - }, - Builtin(&'static str), - Host(HostFn), - /// Plugin / method / call_fn — not a static BoundCall. - Dynamic { - reason: DynamicReason, - name: String, - }, - Unresolved { - message: String, - #[allow(dead_code)] - span_hint: Option, - }, -} +pub type ExecTarget<'a> = super::resolution::Target<&'a Function, &'a Module>; pub fn call_display(call: &CallExpr) -> String { if let Some(path) = &call.path { @@ -62,172 +36,12 @@ pub fn call_display(call: &CallExpr) -> String { call.callee.clone() } -fn find_in_children<'a>(fun: &'a Function, name: &str) -> Option<&'a Function> { - for child in &fun.children { - if child.name == name { - return Some(child); - } - if let Some(f) = find_in_children(child, name) { - return Some(f); - } - } - None +/// Same-module bare names never search imports. +pub fn lookup_local<'a>(module: &'a Module, current: &'a Function, name: &str) -> Option<&'a Function> { + super::resolution::lookup(&super::resolution::AstView::default(), &module, current, name) } - -/// Same-module bare-name lookup (never searches imports). -pub fn lookup_local<'a>( - module: &'a Module, - current: &'a Function, - name: &str, -) -> Option<&'a Function> { - if let Some(f) = find_in_children(current, name) { - return Some(f); - } - module.functions.iter().find(|f| f.name == name) -} - -fn walk_path<'a>(lib: &'a Module, path: &[String]) -> Result<&'a Function, String> { - let display = path.join("."); - if path.is_empty() { - return Err("empty knowledge path".into()); - } - let mut parent_is_object = false; - let mut node = lib - .functions - .iter() - .find(|f| f.name == path[0]) - .ok_or_else(|| format!("unknown `{display}`"))?; - for seg in &path[1..] { - parent_is_object = node.is_object(); - node = node - .children - .iter() - .find(|c| c.name == *seg) - .ok_or_else(|| format!("unknown `{display}`"))?; - } - if parent_is_object && node.level != 1 && crate::ast::function_uses_receiver(node) { - return Err(format!( - "`{display}` is an instance method; call it as `var`.{}", - node.name - )); - } - Ok(node) -} - -fn resolve_import_path<'a>(module: &'a Module, path: &[String]) -> ExecTarget<'a> { - let display = path.join("."); - if path.len() < 2 { - return ExecTarget::Unresolved { - message: format!("library path `{display}`: need at least `lib.member`"), - span_hint: None, - }; - } - let bind = &path[0]; - let Some(lib) = module.import_modules.get(bind) else { - return ExecTarget::Unresolved { - message: format!("unknown library `{bind}` (import it in frontmatter)"), - span_hint: None, - }; - }; - match walk_path(lib, &path[1..]) { - Ok(fun) => ExecTarget::Import { - bind: bind.clone(), - lib, - fun, - path: path.to_vec(), - }, - Err(message) => ExecTarget::Unresolved { - message, - span_hint: None, - }, - } -} - -/// Resolve a call for execution. Import bare-name leakage is forbidden (G02). -pub fn resolve_for_exec<'a>( - module: &'a Module, - current: &'a Function, - call: &CallExpr, -) -> ExecTarget<'a> { - if call.receiver.is_some() { - return ExecTarget::Dynamic { - reason: DynamicReason::MethodReceiver, - name: call_display(call), - }; - } - - if let Some(path) = &call.path { - if module.import_modules.contains_key(&path[0]) { - return resolve_import_path(module, path); - } - // Same-module definition-tree path (`Type.member` / nested), BoundCall. - if path.len() >= 2 { - match walk_path(module, path) { - Ok(fun) => return ExecTarget::Local(fun), - Err(_) if path.len() == 2 => { - // Possible local-variable method; caller checks env. - return ExecTarget::Dynamic { - reason: DynamicReason::MethodReceiver, - name: call_display(call), - }; - } - Err(message) => { - return ExecTarget::Unresolved { - message, - span_hint: None, - }; - } - } - } - return ExecTarget::Unresolved { - message: format!( - "library path `{}`: need at least `lib.member`", - path.join(".") - ), - span_hint: None, - }; - } - - if let Some(u) = module.uses.iter().find(|u| u.bind == call.callee) { - return resolve_import_path(module, &u.path); - } - - match call.callee.as_str() { - "print" | "input" | "len" | "str" | "int" | "type" | "trim" | "split" | "join" | "at" => { - return ExecTarget::Builtin(match call.callee.as_str() { - "print" => "print", - "input" => "input", - "len" => "len", - "str" => "str", - "int" => "int", - "type" => "type", - "trim" => "trim", - "split" => "split", - "join" => "join", - "at" => "at", - _ => unreachable!(), - }); - } - "call_fn" => { - return ExecTarget::Dynamic { - reason: DynamicReason::CallFn, - name: "call_fn".into(), - }; - } - other if HostFn::from_name(other).is_some() => { - return ExecTarget::Host(HostFn::from_name(other).unwrap()); - } - _ => {} - } - - if let Some(fun) = lookup_local(module, current, &call.callee) { - return ExecTarget::Local(fun); - } - - ExecTarget::Dynamic { - reason: DynamicReason::Plugin, - name: call.callee.clone(), - } +pub fn resolve_for_exec<'a>(module: &'a Module, current: &'a Function, call: &CallExpr) -> ExecTarget<'a> { + super::resolution::resolve(&super::resolution::AstView::default(), &module, current, call) } pub fn is_static_bound(target: &ExecTarget<'_>) -> bool { diff --git a/src/sema/ir.rs b/src/sema/ir.rs index c3d54d6..ccf6c73 100644 --- a/src/sema/ir.rs +++ b/src/sema/ir.rs @@ -857,6 +857,11 @@ fn collect_call_sites(function: &Function) -> Vec<(usize, CallExpr, u32)> { } } } + // Omitted arguments execute these expressions before the body. Leaving + // them out lets the pure-call policy miss both direct and transitive effects. + for param in &function.params { + if let Some(default) = ¶m.default { expr(default, function.span.line, &mut calls); } + } statements(&function.body, &mut calls); calls } diff --git a/src/sema/mod.rs b/src/sema/mod.rs index fb3eb41..b3ec55b 100644 --- a/src/sema/mod.rs +++ b/src/sema/mod.rs @@ -5,6 +5,7 @@ //! the meaning of the same source text. pub mod bind; +pub(crate) mod resolution; pub mod extract; pub mod ir; pub mod lock; diff --git a/src/sema/resolution.rs b/src/sema/resolution.rs new file mode 100644 index 0000000..901b607 --- /dev/null +++ b/src/sema/resolution.rs @@ -0,0 +1,254 @@ +//! Ordered declaration lookup, independent of the declaration representation. +use super::bind::{call_display, DynamicReason}; +use crate::{ast::CallExpr, diagnostics::Span, host::HostFn}; + +pub(crate) trait NameView { + type Function: Copy; + type Module: Clone; + fn roots(&self, module: &Self::Module) -> Vec; + fn children(&self, function: Self::Function) -> Vec; + fn parent(&self, module: &Self::Module, function: Self::Function) -> Option; + fn same(&self, left: Self::Function, right: Self::Function) -> bool; + fn name(&self, function: Self::Function) -> String; + fn level(&self, function: Self::Function) -> u8; + fn is_object(&self, function: Self::Function) -> bool; + fn uses_receiver(&self, function: Self::Function) -> bool; + fn import(&self, module: &Self::Module, name: &str) -> Option; + fn used_path(&self, module: &Self::Module, name: &str) -> Option>; +} +#[derive(Debug)] +pub enum Target { + Local(F), + Import { + bind: String, + lib: M, + fun: F, + path: Vec, + }, + Builtin(&'static str), + Host(HostFn), + Dynamic { + reason: DynamicReason, + name: String, + }, + Unresolved { + message: String, + span_hint: Option, + }, +} +fn ancestors( + view: &V, + nodes: Vec, + current: V::Function, + path: &mut Vec, +) -> bool { + for node in nodes { + if view.same(node, current) { + return true; + } + path.push(node); + if ancestors(view, view.children(node), current, path) { + return true; + } + path.pop(); + } + false +} +pub(crate) fn lookup( + view: &V, + module: &V::Module, + current: V::Function, + name: &str, +) -> Option { + let mut cursor = Some(current); + while let Some(node) = cursor { + if let Some(f) = view + .children(node) + .into_iter() + .find(|f| view.name(*f) == name) + { + return Some(f); + } + cursor = view.parent(module, node); + } + view.roots(module) + .into_iter() + .find(|f| view.name(*f) == name) +} +fn walk(view: &V, module: &V::Module, path: &[String]) -> Result { + let display = path.join("."); + let first = path.first().ok_or("empty knowledge path")?; + let mut node = view + .roots(module) + .into_iter() + .find(|f| view.name(*f) == *first) + .ok_or_else(|| format!("unknown `{display}`"))?; + let mut parent_is_object = false; + for segment in &path[1..] { + parent_is_object = view.is_object(node); + node = view + .children(node) + .into_iter() + .find(|f| view.name(*f) == *segment) + .ok_or_else(|| format!("unknown `{display}`"))?; + } + if parent_is_object && view.level(node) != 1 && view.uses_receiver(node) { + return Err(format!( + "`{display}` is an instance method; call it as `var`.{}", + view.name(node) + )); + } + Ok(node) +} +fn imported( + view: &V, + module: &V::Module, + path: &[String], +) -> Target { + let unresolved = |message| Target::Unresolved { + message, + span_hint: None, + }; + if path.len() < 2 { + return unresolved(format!( + "library path `{}`: need at least `lib.member`", + path.join(".") + )); + } + let bind = &path[0]; + let Some(lib) = view.import(module, bind) else { + return unresolved(format!( + "unknown library `{bind}` (import it in frontmatter)" + )); + }; + match walk(view, &lib, &path[1..]) { + Ok(fun) => Target::Import { + bind: bind.clone(), + lib, + fun, + path: path.to_vec(), + }, + Err(message) => unresolved(message), + } +} +pub(crate) fn resolve( + view: &V, + module: &V::Module, + current: V::Function, + call: &CallExpr, +) -> Target { + if call.receiver.is_some() { + return Target::Dynamic { + reason: DynamicReason::MethodReceiver, + name: call_display(call), + }; + } + if let Some(path) = &call.path { + if path + .first() + .is_some_and(|p| view.import(module, p).is_some()) + { + return imported(view, module, path); + } + if path.len() >= 2 { + return match walk(view, module, path) { + Ok(fun) => Target::Local(fun), + Err(_) if path.len() == 2 => Target::Dynamic { + reason: DynamicReason::MethodReceiver, + name: call_display(call), + }, + Err(message) => Target::Unresolved { + message, + span_hint: None, + }, + }; + } + return Target::Unresolved { + message: format!( + "library path `{}`: need at least `lib.member`", + path.join(".") + ), + span_hint: None, + }; + } + if let Some(path) = view.used_path(module, &call.callee) { + return imported(view, module, &path); + } + if crate::arguments::builtin_signature(&call.callee).is_some() { + if call.callee == "call_fn" { + return Target::Dynamic { + reason: DynamicReason::CallFn, + name: "call_fn".into(), + }; + } + let name = match call.callee.as_str() { + "print" => "print", + "input" => "input", + "len" => "len", + "str" => "str", + "int" => "int", + "type" => "type", + "trim" => "trim", + "split" => "split", + "join" => "join", + "at" => "at", + _ => unreachable!(), + }; + return Target::Builtin(name); + } + if let Some(host) = HostFn::from_name(&call.callee) { + return Target::Host(host); + } + if let Some(fun) = lookup(view, module, current, &call.callee) { + return Target::Local(fun); + } + Target::Dynamic { + reason: DynamicReason::Plugin, + name: call.callee.clone(), + } +} + +#[derive(Default)] +pub(crate) struct AstView<'a>(std::marker::PhantomData<&'a crate::ast::Module>); +impl<'a> NameView for AstView<'a> { + type Function = &'a crate::ast::Function; + type Module = &'a crate::ast::Module; + fn roots(&self, m: &Self::Module) -> Vec { + m.functions.iter().collect() + } + fn children(&self, f: Self::Function) -> Vec { + f.children.iter().collect() + } + fn parent(&self, m: &Self::Module, f: Self::Function) -> Option { + let mut path = Vec::new(); + if ancestors(self, self.roots(m), f, &mut path) { + path.last().copied() + } else { + None + } + } + fn same(&self, a: Self::Function, b: Self::Function) -> bool { + std::ptr::eq(a, b) + } + fn name(&self, f: Self::Function) -> String { + f.name.clone() + } + fn level(&self, f: Self::Function) -> u8 { + f.level + } + fn is_object(&self, f: Self::Function) -> bool { + f.is_object() + } + fn uses_receiver(&self, f: Self::Function) -> bool { + crate::ast::function_uses_receiver(f) + } + fn import(&self, m: &Self::Module, name: &str) -> Option { + m.import_modules.get(name) + } + fn used_path(&self, m: &Self::Module, name: &str) -> Option> { + m.uses + .iter() + .find(|u| u.bind == name) + .map(|u| u.path.clone()) + } +} diff --git a/src/source_map_tests.rs b/src/source_map_tests.rs new file mode 100644 index 0000000..67d52a9 --- /dev/null +++ b/src/source_map_tests.rs @@ -0,0 +1,577 @@ +use crate::diagnostics::{Diagnostic, Span}; +use crate::{ + bytecode::{compile_module, Vm}, + interp::Interpreter, + load::load_module, + value::Value, +}; +use serde_json::json; +use sha2::{Digest, Sha256}; +use std::path::{Path, PathBuf}; + +fn digest(text: &str) -> String { + format!("{:x}", Sha256::digest(text.as_bytes())) +} +fn line(text: &str, marker: &str) -> u32 { + text.lines() + .position(|s| s.contains(marker)) + .expect("oracle marker") as u32 + + 1 +} +fn root(label: &str) -> PathBuf { + let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("target/source-map") + .join(format!("{label}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + dir +} +fn save(path: &Path, text: &str) { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, text).unwrap(); +} +fn source(key: &str, path: &Path, text: &str) -> serde_json::Value { + json!({"key":key,"path":path,"text":text,"virtual":!path.is_absolute()}) +} +fn site(key: &str, marker: &str) -> serde_json::Value { + json!({"source":key,"marker":marker}) +} +fn frame( + name: &str, + declaration: serde_json::Value, + call_site: Option, +) -> serde_json::Value { + json!({"function":name,"declaration":declaration,"call_site":call_site}) +} +fn record( + id: &str, + backend: &str, + diagnostic: &Diagnostic, + sources: Vec, + origin: serde_json::Value, + frames: Vec, +) { + if let Some(directory) = std::env::var_os("MARQDO_SOURCE_MAP_DIR") { + let directory = PathBuf::from(directory); + std::fs::create_dir_all(&directory).unwrap(); + std::fs::write( + directory.join(format!("{id}-{backend}.json")), + serde_json::to_vec_pretty( + &json!({"version":1,"id":id,"backend":backend,"sources":sources, + "expected":{"origin":origin,"frames":frames},"diagnostic":diagnostic.to_json()}), + ) + .unwrap(), + ) + .unwrap(); + } +} +fn assert_site(site: &crate::diagnostics::SourceLocation, path: &Path, text: &str, expected: u32) { + assert_eq!( + site.source.path.as_deref().unwrap().canonicalize().unwrap(), + path.canonicalize().unwrap() + ); + assert_eq!(site.source.sha256.as_deref(), Some(digest(text).as_str())); + assert_eq!(site.span, Span::new(expected, 1)); +} +fn assert_origin(d: &Diagnostic, path: &Path, text: &str, expected: u32) { + assert_eq!( + d.path.as_deref().unwrap().canonicalize().unwrap(), + path.canonicalize().unwrap() + ); + assert_eq!(d.source_sha256.as_deref(), Some(digest(text).as_str())); + assert_eq!(d.span, Span::new(expected, 1)); + assert!(d.message.contains("division by zero"), "{d}"); + assert_eq!(d.to_json()["source_sha256"], json!(digest(text))); +} + +#[test] +fn external_argument_binding_uses_target_declaration_source() { + let dir = root("external-binding"); + let entry = dir.join("entry.mq.md"); + let target = dir.join("target.mq.md"); + let leaf = dir.join("leaf.mq.md"); + let main = "# main\n*7*\n"; + let text = "---\nimport:\n leaf: leaf.mq.md\n---\n\n\n## required\n + `x`\n*x*\n\n## defaulted\n + `x`=> leaf.explode\n*x*\n"; + let leaf_text = "\n\n\n## explode\n**bad = 1 / 0**\n*bad*\n"; + save(&entry, main); + save(&target, text); + save(&leaf, leaf_text); + let module = load_module(&target).unwrap(); + let mut tree = Interpreter::with_capture(Some(&entry), false); + let before = tree.diagnostic_scope_state(); + for name in ["required", "defaulted"] { + let fun = module.functions.iter().find(|f| f.name == name).unwrap(); + let error = tree.bind_external_args(&module, fun, &[]).unwrap_err(); + let d = Diagnostic::find(&error).unwrap(); + assert_eq!(tree.diagnostic_scope_state(), before); + let expected_frames = if name == "required" { + assert_eq!(d.code, "call.missing_arg"); + assert_eq!(d.path.as_deref(), Some(target.as_path())); + assert_eq!(d.source_sha256.as_deref(), Some(digest(text).as_str())); + assert_eq!(d.span, fun.span); + vec![frame("required", site("target", "## required"), None)] + } else { + assert_origin(d, &leaf, leaf_text, line(leaf_text, "**bad")); + vec![ + frame("defaulted", site("target", "## defaulted"), None), + frame( + "explode", + site("leaf", "## explode"), + Some(site("target", "+ `x`=> leaf.explode")), + ), + ] + }; + record( + &format!("external-binding-{name}"), + "tree", + d, + vec![ + source("entry", &entry, main), + source("target", &target, text), + source("leaf", &leaf, leaf_text), + ], + if name == "required" { + site("target", "## required") + } else { + site("leaf", "**bad") + }, + expected_frames, + ); + let first = d.to_json(); + let bound = tree + .bind_external_args(&module, fun, &[("x".into(), Value::Int(7))]) + .unwrap(); + assert_eq!(bound, vec![("x".into(), Value::Int(7))]); + assert_eq!(tree.diagnostic_scope_state(), before); + let again = tree.bind_external_args(&module, fun, &[]).unwrap_err(); + assert_eq!(Diagnostic::find(&again).unwrap().to_json(), first); + assert_eq!(tree.diagnostic_scope_state(), before); + } +} + +#[test] +fn imported_source_identity_frames_and_reuse_match_on_both_backends() { + let root = root("imports"); + let mut count = 0; + for pad in [0, 4, 17] { + for crlf in [false, true] { + for side in ["left", "right"] { + let dir = root.join(format!("{pad}-{crlf}-{side}")); + let basename = if pad == 17 { + "共同源.mq.md" + } else { + "shared.mq.md" + }; + let left = dir.join("left").join(basename); + let right = dir.join("right").join(basename); + let middle = dir.join("middle.mq.md"); + let entry = dir.join("entry.mq.md"); + let lf = |s: String| if crlf { s.replace('\n', "\r\n") } else { s }; + let leaf_a = lf(format!( + "源位置验证 🧪\n{}## explode\n**bad = 1 / 0**\n*bad*\n\n## good\n*7*\n", + "\n".repeat(pad) + )); + let leaf_b = if pad == 4 { + leaf_a.clone() + } else { + lf(format!( + "{}## explode\n\n**bad = 1 / 0**\n*bad*\n\n## good\n*7*\n", + "\n".repeat(pad + 3) + )) + }; + save(&left, &leaf_a); + save(&right, &leaf_b); + let mid=lf(format!("---\nmarkup: occlusion\nimport:\n leaf: {side}/{basename}\n---\n## relay\n**answer = > leaf.explode**\n*answer*\n")); + let main=lf("---\nmarkup: occlusion\nimport:\n mid: middle.mq.md\n---\n# main\n**answer = > mid.relay**\n*answer*\n".into()); + save(&middle, &mid); + save(&entry, &main); + let module = load_module(&entry).unwrap(); + let (leaf_path, leaf) = if side == "left" { + (&left, &leaf_a) + } else { + (&right, &leaf_b) + }; + let leaf_module = &module.import_modules["mid"].import_modules["leaf"]; + let program = compile_module(Some(&entry), &module).unwrap(); + // Dependencies are private, so API calls compile that module + // directly rather than assuming transitive re-exports. + let good_code = + crate::bytecode::compile_for_call(Some(leaf_path), leaf_module).unwrap(); + let good_fid = good_code.path_index[&vec!["good".into()]]; + for backend in ["tree", "bytecode"] { + let host = || { + let mut h = + crate::host::HostContext::for_capture(Some(&entry), Default::default()); + h.call_stack.push("outside-source-entry".into()); + h.call_site_lines.push(99); + h + }; + let mut tree = Interpreter::with_capture(Some(&entry), false).with_host(host()); + let mut vm = Vm::with_capture(Some(&entry)).with_host(host()); + let before_tree = tree.diagnostic_scope_state(); + let before_vm = vm.diagnostic_scope_state(); + let err = if backend == "tree" { + tree.run_module(&module) + } else { + vm.run(&program) + } + .unwrap_err(); + let diagnostic = Diagnostic::find(&err).unwrap(); + assert_origin(diagnostic, leaf_path, leaf, line(leaf, "**bad")); + assert_eq!(diagnostic.frames.len(), 3); + assert_eq!( + diagnostic + .frames + .iter() + .map(|f| f.function.as_str()) + .collect::>(), + ["main", "relay", "explode"] + ); + assert!(diagnostic.frames[0].call_site.is_none()); + assert_site( + diagnostic.frames[1].call_site.as_ref().unwrap(), + &entry, + &main, + line(&main, "**answer"), + ); + assert_site( + diagnostic.frames[2].call_site.as_ref().unwrap(), + &middle, + &mid, + line(&mid, "**answer"), + ); + assert_site( + &diagnostic.frames[2].declaration, + leaf_path, + leaf, + line(leaf, "## explode"), + ); + assert_eq!(tree.diagnostic_scope_state(), before_tree); + assert_eq!(vm.diagnostic_scope_state(), before_vm); + record( + &format!("imports-{pad}-{crlf}-{side}"), + backend, + diagnostic, + vec![ + source("entry", &entry, &main), + source("middle", &middle, &mid), + source("leaf", leaf_path, leaf), + ], + site("leaf", "**bad"), + vec![ + frame("main", site("entry", "# main"), None), + frame( + "relay", + site("middle", "## relay"), + Some(site("entry", "**answer")), + ), + frame( + "explode", + site("leaf", "## explode"), + Some(site("middle", "**answer")), + ), + ], + ); + let expected = diagnostic.to_json(); + let good = if backend == "tree" { + tree.invoke_function(leaf_module, "good", &[]) + } else { + vm.call_function(&good_code, good_fid, &[]) + } + .unwrap(); + assert_eq!(good, Value::Int(7)); + let again = if backend == "tree" { + tree.run_module(&module) + } else { + vm.run(&program) + } + .unwrap_err(); + assert_eq!( + Diagnostic::find(&again).unwrap().to_json(), + expected, + "stale caller leaked after reuse" + ); + assert_eq!(tree.diagnostic_scope_state(), before_tree); + assert_eq!(vm.diagnostic_scope_state(), before_vm); + count += 1; + } + } + } + } + println!( + "SOURCE_MAP_METRIC:{}", + json!({"kind":"imports","cases":count,"correct":true}) + ); +} + +#[test] +fn source_revision_is_loaded_text_not_later_disk_contents() { + let root = root("revision"); + let lib = root.join("shared.mq.md"); + let entry = root.join("entry.mq.md"); + let first = "## explode\n**bad = 1 / 0**\n*bad*\n"; + let second = "\n\n\n\n\n## explode\n**bad = 1 / 0**\n*bad*\n"; + let main="---\nmarkup: occlusion\nimport:\n lib: shared.mq.md\n---\n# main\n**answer = > lib.explode**\n*answer*\n"; + save(&entry, main); + save(&lib, first); + let old = load_module(&entry).unwrap(); + let old_code = compile_module(Some(&entry), &old).unwrap(); + save(&lib, second); + let new = load_module(&entry).unwrap(); + let new_code = compile_module(Some(&entry), &new).unwrap(); + for (revision, module, code, text) in [ + ("old", &old, &old_code, first), + ("new", &new, &new_code, second), + ] { + let mut tree = Interpreter::with_capture(Some(&entry), false); + let mut vm = Vm::with_capture(Some(&entry)); + for (backend, error) in [ + ("tree", tree.run_module(module).unwrap_err()), + ("bytecode", vm.run(code).unwrap_err()), + ] { + let d = Diagnostic::find(&error).unwrap(); + assert_origin(d, &lib, text, line(text, "**bad")); + assert_site( + d.frames[1].call_site.as_ref().unwrap(), + &entry, + main, + line(main, "**answer"), + ); + record( + &format!("revision-{revision}"), + backend, + d, + vec![source("entry", &entry, main), source("leaf", &lib, text)], + site("leaf", "**bad"), + vec![ + frame("main", site("entry", "# main"), None), + frame( + "explode", + site("leaf", "## explode"), + Some(site("entry", "**answer")), + ), + ], + ); + } + } +} + +#[test] +#[cfg(feature = "exec-host")] +fn foreign_boundary_diagnostics_keep_library_origin_and_real_user_call_site() { + let root = root("foreign"); + let lib = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib/foreign.mq.md"); + let library = std::fs::read_to_string(&lib).unwrap(); + let mut count = 0; + for lang in ["python", "node"] { + for output in [false, true] { + let entry = root.join(format!("{lang}-{output}.mq.md")); + let action = if output { + "**> foreign.session_eval id=`s` source=\"n = 99; 9007199254740992\"**" + } else { + "**> foreign.session_set id=`s` name=\"n\" value=`s`**" + }; + let main=format!("---\nmarkup: occlusion\nimport:\n foreign: {}\n---\n# main\n**s = > foreign.session_open lang=\"{lang}\"**\n**> foreign.session_set id=`s` name=\"n\" value=1**\n{action}\n",lib.display()); + save(&entry, &main); + let module = load_module(&entry).unwrap(); + let program = compile_module(Some(&entry), &module).unwrap(); + let expected_function = if output { + "session_eval" + } else { + "session_set" + }; + let expected_primitive = if output { + "host_foreign_session_eval" + } else { + "host_foreign_session_set" + }; + for backend in ["tree", "bytecode"] { + let mut tree = Interpreter::with_capture(Some(&entry), false); + let mut vm = Vm::with_capture(Some(&entry)); + let error = if backend == "tree" { + tree.run_module(&module) + } else { + vm.run(&program) + } + .unwrap_err(); + let d = Diagnostic::find(&error).unwrap(); + assert!(d.message.contains("outside V_safe")); + assert_eq!( + d.path.as_ref().unwrap().canonicalize().unwrap(), + lib.canonicalize().unwrap() + ); + assert_eq!(d.source_sha256, Some(digest(&library))); + assert_eq!(d.span.line, line(&library, expected_primitive)); + let last_frame = d.frames.last().unwrap(); + assert_eq!(last_frame.function, expected_function); + assert_site( + last_frame.call_site.as_ref().unwrap(), + &entry, + &main, + line(&main, action), + ); + assert!(d.format_message().contains(&format!( + "{}:{}:1", + entry.display(), + line(&main, action) + ))); + let (_, values, _) = if backend == "tree" { + tree.foreign_source_evidence() + } else { + vm.foreign_source_evidence() + }; + assert_eq!(values, vec![Value::Int(if output { 99 } else { 1 })]); + record( + &format!("foreign-{lang}-{output}"), + backend, + d, + vec![ + source("entry", &entry, &main), + source("foreign", &lib, &library), + ], + site("foreign", expected_primitive), + vec![ + frame("main", site("entry", "# main"), None), + frame( + expected_function, + site("foreign", &format!("## {expected_function}")), + Some(site("entry", action)), + ), + ], + ); + count += 1; + } + } + } + println!( + "SOURCE_MAP_METRIC:{}", + json!({"kind":"foreign","cases":count,"correct":true}) + ); +} + +#[test] +fn memory_dynamic_and_method_calls_keep_their_own_sources() { + let directory = root("entry-kinds"); + let memory="---\nmarkup: occlusion\nimport:\n json: lib/json.mq.md\n---\n# main\n**bad = > json.parse text=not-json**\n"; + let module = crate::load::load_module_from_source(memory).unwrap(); + let json_text = crate::embedded_lib::read_file("json.mq.md").unwrap(); + let code = compile_module(None, &module).unwrap(); + let dynamic="# main\n**name = \"explode\"**\n> call_fn name=`name`\n\n## explode\n**bad = 1 / 0**\n*bad*\n"; + let dynamic_path = directory.join("dynamic.mq.md"); + save(&dynamic_path, dynamic); + let dynamic_module = load_module(&dynamic_path).unwrap(); + let dynamic_code = compile_module(Some(&dynamic_path), &dynamic_module).unwrap(); + let object_path = directory.join("object.mq.md"); + let json_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib/json.mq.md"); + let object=format!("---\nmarkup: occlusion\nimport:\n json: {}\n---\n# Counter\n**state = > json.parse text={{\"n\":1}}**\n*state*\n\n## fail\n**saved = `self`**\n**bad = 1 / 0**\n*bad*\n",json_path.display()); + save(&object_path, &object); + let entry_path = directory.join("method.mq.md"); + let method="---\nmarkup: occlusion\nimport:\n obj: object.mq.md\n---\n# main\n**c = > obj.Counter**\n> `c`.fail\n"; + save(&entry_path, method); + let method_module = load_module(&entry_path).unwrap(); + let method_code = compile_module(Some(&entry_path), &method_module).unwrap(); + for backend in ["tree", "bytecode"] { + let error = if backend == "tree" { + Interpreter::with_capture(None, false).run_module(&module) + } else { + Vm::with_capture(None).run(&code) + } + .unwrap_err(); + let d = Diagnostic::find(&error).unwrap(); + assert_eq!(d.path.as_deref(), Some(Path::new("lib/json.mq.md"))); + assert_eq!(d.source_sha256, Some(digest(&json_text))); + assert_eq!(d.span.line, line(&json_text, "host_json_parse")); + assert_eq!( + d.frames + .last() + .unwrap() + .call_site + .as_ref() + .unwrap() + .source + .path + .as_deref(), + Some(Path::new("")) + ); + record( + "memory", + backend, + d, + vec![ + source("entry", Path::new(""), memory), + source("json", Path::new("lib/json.mq.md"), &json_text), + ], + site("json", "host_json_parse"), + vec![ + frame("main", site("entry", "# main"), None), + frame( + "parse", + site("json", "## parse"), + Some(site("entry", "**bad")), + ), + ], + ); + let error = if backend == "tree" { + Interpreter::with_capture(Some(&dynamic_path), false).run_module(&dynamic_module) + } else { + Vm::with_capture(Some(&dynamic_path)).run(&dynamic_code) + } + .unwrap_err(); + let d = Diagnostic::find(&error).unwrap(); + assert_origin(d, &dynamic_path, dynamic, line(dynamic, "**bad")); + assert_eq!(d.frames.last().unwrap().function, "explode"); + assert_site( + d.frames.last().unwrap().call_site.as_ref().unwrap(), + &dynamic_path, + dynamic, + line(dynamic, "> call_fn"), + ); + record( + "dynamic", + backend, + d, + vec![source("entry", &dynamic_path, dynamic)], + site("entry", "**bad"), + vec![ + frame("main", site("entry", "# main"), None), + frame( + "explode", + site("entry", "## explode"), + Some(site("entry", "> call_fn")), + ), + ], + ); + let error = if backend == "tree" { + Interpreter::with_capture(Some(&entry_path), false).run_module(&method_module) + } else { + Vm::with_capture(Some(&entry_path)).run(&method_code) + } + .unwrap_err(); + let d = Diagnostic::find(&error).unwrap(); + assert_origin(d, &object_path, &object, line(&object, "**bad")); + assert_eq!(d.frames.last().unwrap().function, "fail"); + assert_site( + d.frames.last().unwrap().call_site.as_ref().unwrap(), + &entry_path, + method, + line(method, "> `c`.fail"), + ); + record( + "method", + backend, + d, + vec![ + source("entry", &entry_path, method), + source("object", &object_path, &object), + ], + site("object", "**bad"), + vec![ + frame("main", site("entry", "# main"), None), + frame( + "fail", + site("object", "## fail"), + Some(site("entry", "> `c`.fail")), + ), + ], + ); + } +} diff --git a/src/source_refinement_tests.rs b/src/source_refinement_tests.rs new file mode 100644 index 0000000..1408001 --- /dev/null +++ b/src/source_refinement_tests.rs @@ -0,0 +1,452 @@ +//! Export the actual loaded AST and independently observe both real backends. +//! Unsupported AST constructs are errors, never omitted or normalized to no-op. +use crate::ast::{Arg, BinaryOp, CallExpr, Expr, Function, Literal, Module, Stmt}; +use crate::host::{foreign_session, json::value_to_json, HostContext}; +use crate::value::Value; +use serde_json::{json, Value as Json}; +use sha2::{Digest, Sha256}; +use std::path::PathBuf; + +pub(crate) fn boundary_evidence( + ctx: &mut HostContext, +) -> (Vec, Vec, Vec) { + let observations = ctx.oracle_observations.clone(); + let protocol = ctx.foreign_protocol_steps.clone(); + let mut ids = ctx.foreign_sessions.keys().cloned().collect::>(); + ids.sort(); + // These verification reads bypass the document observation API. They occur + // only after its journal was captured, and never alter that journal. + let values = ids + .iter() + .map(|id| { + foreign_session::get(ctx, &Value::Handle(id.clone()), &Value::Text("n".into())).unwrap() + }) + .collect(); + assert_eq!(observations, ctx.oracle_observations); + (observations, values, protocol) +} + +fn expr(e: &Expr) -> Result { + Ok(match e { + Expr::Literal(Literal::None) => json!(["literal", null]), + Expr::Literal(Literal::Bool(b)) => json!(["literal", b]), + Expr::Literal(Literal::Int(n)) => json!(["literal", n]), + Expr::Literal(Literal::Text(s)) => json!(["literal", s]), + Expr::Var(name) => json!(["var", name]), + Expr::Binary { op, left, right } => { + let tag = match op { + BinaryOp::Add => "add", + BinaryOp::Sub => "sub", + BinaryOp::Lt => "lt", + _ => return Err(format!("unsupported source binary op {op:?}")), + }; + json!([tag, expr(left)?, expr(right)?]) + } + _ => return Err(format!("unsupported source expression {e:?}")), + }) +} + +fn call(c: &CallExpr, dest: Option<&str>, defs: &[&Function]) -> Result { + if c.receiver.is_some() || !c.pre_modifiers.is_empty() { + return Err("unsupported receiver/modifiers".into()); + } + if let Some(path) = &c.path { + if path.len() != 2 || path[0] != "foreign" { + return Err(format!("unsupported library path {path:?}")); + } + let mut args = std::collections::BTreeMap::new(); + for a in &c.args { + let Arg::Named { name, value } = a else { + return Err("foreign primitive needs named arguments".into()); + }; + if args.insert(name.as_str(), value).is_some() { + return Err("duplicate argument".into()); + } + } + let allowed: &[&str] = match path[1].as_str() { + "session_open" => &["lang"], + "session_set" => &["id", "name", "value"], + "session_get" => &["id", "name"], + "session_eval" => &["id", "source", "input", "stdin"], + "session_close" => &["id"], + _ => return Err(format!("unsupported primitive {path:?}")), + }; + if args.keys().any(|k| !allowed.contains(k)) { + return Err("unsupported primitive option".into()); + } + let arg = |name: &str| { + args.get(name) + .copied() + .ok_or_else(|| format!("missing {name}")) + }; + let name = || match arg("name")? { + Expr::Literal(Literal::Text(s)) => Ok(s.clone()), + _ => Err("dynamic binding name unsupported".to_string()), + }; + match path[1].as_str() { + "session_open" => Ok(json!([ + "open", + dest.ok_or("open destination required")?, + expr(arg("lang")?)? + ])), + "session_get" => Ok(json!([ + "get", + dest.ok_or("get destination required")?, + expr(arg("id")?)?, + name()? + ])), + _ if dest.is_some() => { + Err("assigned set/eval/close record requires a richer adapter".into()) + } + "session_set" => Ok(json!([ + "set", + expr(arg("id")?)?, + name()?, + expr(arg("value")?)? + ])), + "session_eval" => Ok(json!([ + "eval", + expr(arg("id")?)?, + expr(arg("source")?)?, + args.get("input") + .map(|v| expr(v)) + .transpose()? + .unwrap_or(json!(["literal", null])), + args.get("stdin").map(|v| expr(v)).transpose()? + ])), + "session_close" => Ok(json!(["close", expr(arg("id")?)?])), + _ => unreachable!(), + } + } else { + let f = defs + .iter() + .find(|f| f.name == c.callee) + .ok_or_else(|| format!("unknown function {}", c.callee))?; + let mut values = std::collections::BTreeMap::new(); + let mut positional = 0; + for a in &c.args { + let (name, value) = match a { + Arg::Named { name, value } => (name.as_str(), value), + Arg::Positional(value) => { + let p = f.params.get(positional).ok_or("too many arguments")?; + positional += 1; + (p.name.as_str(), value) + } + }; + if !f.params.iter().any(|p| p.name == name) || values.insert(name, value).is_some() { + return Err("invalid call argument".into()); + } + } + let ordered = f + .params + .iter() + .map(|p| expr(values.get(p.name.as_str()).ok_or("missing parameter")?)) + .collect::, _>>()?; + Ok(json!(["call", dest, c.callee, ordered])) + } +} + +fn block(body: &[Stmt], defs: &[&Function]) -> Result, String> { + body.iter() + .map(|s| { + Ok(match s { + Stmt::Assign { + name, + value: Expr::Call(c), + .. + } => call(c, Some(name), defs)?, + Stmt::Assign { name, value, .. } => json!(["assign", name, expr(value)?]), + Stmt::Return { value, .. } => json!(["return", expr(value)?]), + Stmt::Call { call: c, .. } => call(c, None, defs)?, + Stmt::While { + condition, body, .. + } => json!(["while", expr(condition)?, block(body, defs)?]), + Stmt::Branch { arms, .. } => { + let mut rest = Vec::new(); + for (i, arm) in arms.iter().enumerate().rev() { + let body = block(&arm.body, defs)?; + if let Some(condition) = &arm.condition { + rest = vec![json!(["if", expr(condition)?, body, rest])]; + } else { + if i != arms.len() - 1 { + return Err("nonterminal else".into()); + } + rest = body; + } + } + if rest.len() != 1 || rest[0][0] != "if" { + return Err("branch needs condition".into()); + } + rest.pop().unwrap() + } + _ => return Err(format!("unsupported source statement {s:?}")), + }) + }) + .collect() +} + +fn source_ir(module: &Module) -> Result { + if module.functions.len() != 1 || !module.uses.is_empty() { + return Err("module needs the declared single-entry adapter".into()); + } + let main = module + .functions + .iter() + .find(|f| f.name == "main") + .ok_or("missing main")?; + let defs = main.children.iter().collect::>(); + if !main.params.is_empty() { + return Err("entry parameters need an explicit environment adapter".into()); + } + let mut seen = std::collections::HashSet::new(); + for f in &defs { + if !seen.insert(&f.name) + || !f.children.is_empty() + || f.params.iter().any(|p| p.default.is_some()) + { + return Err("unsupported function scope/default".into()); + } + } + let functions = defs.iter().map(|f| Ok(json!({"name":f.name,"params":f.params.iter().map(|p|p.name.clone()).collect::>(),"body":block(&f.body,&defs)?}))).collect::,String>>()?; + let initial = module + .metadata + .iter() + .map(|(name, value)| { + if !matches!( + value, + Value::None | Value::Bool(_) | Value::Int(_) | Value::Text(_) + ) { + return Err("non-scalar metadata needs an explicit initial-value adapter".into()); + } + Ok(json!([name, value_to_json(value)?])) + }) + .collect::, String>>()?; + Ok(json!({"initial":initial,"entry":block(&main.body,&defs)?,"functions":functions})) +} + +fn source(lang: &str, seed: i64, rounds: usize, scenario: &str) -> String { + let increment = "n += 1; n"; + let bad = "n = 99; 9007199254740992"; + let stdin = if scenario == "explicit_empty_stdin" { + " stdin=\"\"" + } else { + "" + }; + let condition = if scenario == "else_branch" { + seed - 1 + } else { + seed + 1 + }; + let function = match scenario { + "recursive" => "advance_rec", + "early_return" => "early", + _ => "adjust", + }; + let failure = match scenario { + "closed_get" => "**> foreign.session_close id=`alias`**\n**lost = > foreign.session_get id=`alias` name=\"n\"**".into(), + "copy_handle" => "**> foreign.session_set id=`a` name=\"n\" value=`b`**".into(), + "closed_copy" => "**> foreign.session_close id=`alias`**\n**> foreign.session_set id=`alias` name=\"n\" value=`b`**".into(), + "wide_input" => "**> foreign.session_set id=`a` name=\"n\" value=9007199254740992**".into(), + "wide_output" => format!("**> foreign.session_eval id=`a` source=\"{bad}\"**"), + _ => String::new(), + }; + format!( + r#"--- +markup: occlusion +import: + foreign: lib/foreign.mq.md +--- +# main +**a = > foreign.session_open lang="{lang}"** +**b = > foreign.session_open lang="{lang}"** +**> foreign.session_set id=`a` name="n" value={seed}** +**> foreign.session_set id=`b` name="n" value=100** +**alias = `a`** +**counter = 777** +**before = > foreign.session_get id=`a` name="n"** +{failure} + +1. `before` < {condition} + **answer = > {function} s=`alias` rounds={rounds}** +2. * + **> foreign.session_set id=`a` name="n" value={else_value}** + +**after = > foreign.session_get id=`a` name="n"** +**peer = > foreign.session_get id=`b` name="n"** +**> foreign.session_close id=`alias`** +**c = > foreign.session_open lang="{lang}"** +**> foreign.session_set id=`c` name="n" value=7** +**fresh = > foreign.session_get id=`c` name="n"** +**> foreign.session_close id=`b`** +**> foreign.session_close id=`c`** +**total = after + peer + fresh + counter** +*total* + +## adjust + + `s` + + `rounds` +**counter = 0** +- `counter` < `rounds` + > bump s=`s` + **counter = counter + 1** + +**answer = > foreign.session_get id=`s` name="n"** +*answer* + +## bump + + `s` +> foreign.session_eval id=`s` source="{increment}"{stdin} + +## advance_rec + + `s` + + `rounds` +1. 0 < `rounds` + > bump s=`s` + **remaining = rounds - 1** + **answer = > advance_rec s=`s` rounds=`remaining`** + *answer* +2. * + **answer = > foreign.session_get id=`s` name="n"** + *answer* + +## early + + `s` + + `rounds` +**counter = 0** +- `counter` < `rounds` + **answer = > foreign.session_get id=`s` name="n"** + *answer* + +**answer = > foreign.session_get id=`s` name="n"** +*answer* +"#, + else_value = seed + 20 + ) +} + +#[test] +fn parsed_source_control_matches_independent_oracles_on_both_backends() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let out = std::env::var_os("MARQDO_SOURCE_ARTIFACT_DIR") + .map(PathBuf::from) + .unwrap_or_else(|| root.join("target/source-refinement")); + std::fs::create_dir_all(&out).unwrap(); + let seeds = std::env::var("MARQDO_SOURCE_SEEDS") + .unwrap_or("3".into()) + .split(',') + .map(|x| x.parse::().unwrap()) + .collect::>(); + let lengths = std::env::var("MARQDO_SOURCE_ROUNDS") + .unwrap_or("0,3".into()) + .split(',') + .map(|x| x.parse::().unwrap()) + .collect::>(); + let scenarios = [ + "normal", + "recursive", + "early_return", + "else_branch", + "closed_get", + "copy_handle", + "closed_copy", + "wide_input", + "wide_output", + "explicit_empty_stdin", + ]; + let mut count = 0; + for lang in ["python", "node"] { + for &seed in &seeds { + for &rounds in &lengths { + for scenario in scenarios { + let id = format!("{lang}-{seed}-{rounds}-{scenario}"); + let text = source(lang, seed, rounds, scenario); + // Absolute import is only a fixture location; the actual module is loaded. + let text = text.replace( + "foreign: lib/foreign.mq.md", + &format!("foreign: {}", root.join("lib/foreign.mq.md").display()), + ); + let path = out.join(format!("{id}.mq.md")); + std::fs::write(&path, &text).unwrap(); + let module = crate::load::load_module(&path).unwrap(); + let ir = source_ir(&module).unwrap_or_else(|e| panic!("{id}: AST export: {e}")); + let mut executions = Vec::new(); + for backend in ["tree", "bytecode"] { + let (result, journal, values, protocol) = if backend == "tree" { + let mut interpreter = + crate::interp::Interpreter::with_capture(Some(&path), false); + let result = interpreter.run_module(&module); + let (journal, values, protocol) = interpreter.foreign_source_evidence(); + assert!(interpreter.captured_stdout.is_empty(), "unexpected stdout"); + (result, journal, values, protocol) + } else { + let code = + crate::bytecode::compile_module(Some(&path), &module).unwrap(); + let mut vm = crate::bytecode::Vm::with_capture(Some(&path)); + let result = vm.run(&code); + let (journal, values, protocol) = vm.foreign_source_evidence(); + assert!(vm.captured_stdout.is_empty(), "unexpected stdout"); + (result, journal, values, protocol) + }; + let failure = matches!( + scenario, + "closed_get" + | "copy_handle" + | "closed_copy" + | "wide_input" + | "wide_output" + ); + assert_eq!(result.is_err(), failure, "{id} {backend}: {result:?}"); + if !failure { + let expected = seed + + if scenario == "early_return" { + 0 + } else if scenario == "else_branch" { + 20 + } else { + rounds as i64 + }; + assert_eq!( + result.as_ref().unwrap(), + &Value::Int(expected + 884), + "{id} {backend}" + ); + } + let error = result.as_ref().err().map(|e| e.to_string()); + let diagnostic = result.as_ref().err().and_then(crate::diagnostics::Diagnostic::find).map(crate::diagnostics::Diagnostic::to_json); + let fault = if let Some(e) = &error { + if e.contains("not found") { + "unknown" + } else if e.contains("outside V_safe") + || e.contains("cannot be serialized") + { + if protocol.last().is_some_and(|p| { + p.response.as_ref().is_ok_and(|reply| { + reply.get("domain_error").and_then(Json::as_bool) + == Some(true) + }) + }) { + "outputDomain" + } else { + "inputDomain" + } + } else { + panic!("{id} {backend}: unclassified error {e}") + } + } else { + "none" + }; + let protocol = protocol.iter().map(|p| json!({"session":p.session,"request":p.request,"response":p.response})).collect::>(); + executions.push(json!({"backend":backend,"ok":result.is_ok(),"result":result.ok().map(|v|value_to_json(&v).unwrap()),"error":error,"fault":fault,"protocol":protocol,"diagnostic":diagnostic, + "journal":value_to_json(&Value::List(journal)).unwrap(),"live_values":value_to_json(&Value::List(values)).unwrap()})); + } + std::fs::write(out.join(format!("{id}.json")),serde_json::to_vec_pretty(&json!({"version":1,"id":id,"language":lang,"seed":seed,"rounds":rounds,"scenario":scenario,"source_sha256":format!("{:x}",Sha256::digest(text.as_bytes())),"ast":ir,"executions":executions})).unwrap()).unwrap(); + count += 1; + } + } + } + } + println!( + "SOURCE_METRIC:{}", + json!({"cases":count,"executions":count*2,"scenarios":scenarios,"correct":true}) + ); +} diff --git a/src/value.rs b/src/value.rs index da6f0dc..fbea257 100644 --- a/src/value.rs +++ b/src/value.rs @@ -4,6 +4,44 @@ use std::fmt; use crate::formula::Expr as FormulaExpr; +/// An object's defining module instance, expressed as import binding segments. +/// This is ordinary copied data, not an unforgeable session capability. +pub(crate) const OBJECT_MODULE_KEY: &str = "_type_module"; + +pub(crate) fn instance_module(value: &Value) -> Result>, String> { + let Value::Map(fields) = value else { return Ok(None); }; + let Some((_, module)) = fields.iter().find(|(key, _)| key == OBJECT_MODULE_KEY) else { return Ok(None); }; + let Value::List(segments) = module else { return Err("object module tag must be a list of text segments".into()); }; + segments.iter().map(|segment| match segment { + Value::Text(name) => Ok(name.clone()), + _ => Err("object module tag must be a list of text segments".into()), + }).collect::,_>>().map(Some) +} + +pub(crate) fn tag_instance(type_name: &str, module: &[String], value: Value) -> Value { + let mut fields = match value { + Value::Map(fields) => fields, + Value::None => Vec::new(), + other => vec![("value".into(),other)], + }; + fields.retain(|(key,_)| key != "_type" && key != OBJECT_MODULE_KEY); + fields.insert(0,("_type".into(),Value::Text(type_name.into()))); + fields.insert(1,(OBJECT_MODULE_KEY.into(),Value::List(module.iter().cloned().map(Value::Text).collect()))); + Value::Map(fields) +} + +pub(crate) fn preserve_instance_module(receiver: &Value, value: Value) -> Value { + let Ok(Some(module)) = instance_module(receiver) else { return value; }; + match value { + Value::Map(mut fields) => { + fields.retain(|(key,_)| key != OBJECT_MODULE_KEY); + fields.insert(1.min(fields.len()),(OBJECT_MODULE_KEY.into(),Value::List(module.into_iter().map(Value::Text).collect()))); + Value::Map(fields) + } + other => other, + } +} + /// Bound Markdown code fence (`type` → `code`). #[derive(Debug, Clone, PartialEq, Eq)] pub struct CodeBlock { @@ -75,6 +113,7 @@ impl Value { Value::Map(pairs) => { let parts: Vec = pairs .iter() + .filter(|(key,_)| key != OBJECT_MODULE_KEY) .map(|(k, v)| format!("{k}: {}", v.as_display())) .collect(); format!("{{{}}}", parts.join(", ")) diff --git a/src/view/notebook_api.rs b/src/view/notebook_api.rs index 9335c7b..4151c01 100644 --- a/src/view/notebook_api.rs +++ b/src/view/notebook_api.rs @@ -1,15 +1,34 @@ //! Live-view notebook session APIs. +#[cfg(all(test, unix))] +mod interrupt_http_tests; + use std::collections::HashMap; use std::path::{Path, PathBuf}; -use std::sync::{Arc, Mutex, OnceLock}; +use std::sync::{Arc, Mutex, OnceLock, MutexGuard, TryLockError}; +use std::sync::atomic::{AtomicBool, Ordering}; use crate::host::json::{json_to_value, value_to_json}; use crate::host::foreign_doc::DocumentSession; use crate::value::Value; -fn sessions() -> &'static Mutex>>> { - static S: OnceLock>>>> = OnceLock::new(); +struct ApiSession { + closed: AtomicBool, + document: OnceLock, String>>, + control: crate::host::foreign_doc::DocumentControl, +} +impl ApiSession { + fn mutex(&self) -> Result<&Mutex, String> { + self.document.wait().as_ref().map_err(Clone::clone) + } + fn lock(&self) -> Result, String> { + let guard = self.mutex()?.lock().map_err(|_| "session lock poisoned".to_string())?; + if self.closed.load(Ordering::Acquire) { return Err("session closed".into()); } + Ok(guard) + } +} +fn sessions() -> &'static Mutex>> { + static S: OnceLock>>> = OnceLock::new(); S.get_or_init(|| Mutex::new(HashMap::new())) } @@ -17,7 +36,7 @@ fn body_value(body: &str) -> serde_json::Value { serde_json::from_str(body).unwrap_or(serde_json::Value::Null) } -fn path_of(root: &Path, v: &serde_json::Value) -> Result<(String, PathBuf), String> { +fn requested_path(root: &Path, v: &serde_json::Value) -> Result<(String, PathBuf), String> { let rel = v .get("path") .and_then(|p| p.as_str()) @@ -28,21 +47,37 @@ fn path_of(root: &Path, v: &serde_json::Value) -> Result<(String, PathBuf), Stri return Err("missing path".into()); } let abs = root.join(rel.replace('/', std::path::MAIN_SEPARATOR_STR)); + Ok((rel, abs)) +} + +fn path_of(root: &Path, v: &serde_json::Value) -> Result<(String, PathBuf), String> { + let (rel, abs) = requested_path(root, v)?; if !abs.is_file() { return Err(format!("file not found: {rel}")); } Ok((rel, abs)) } -fn get_or_open(root: &Path, abs: &Path) -> Result>, String> { +fn get_or_open(root: &Path, abs: &Path) -> Result, String> { let key = abs.display().to_string(); - let mut map = sessions().lock().map_err(|_| "session lock poisoned".to_string())?; - if let Some(s) = map.get(&key) { - return Ok(Arc::clone(s)); + let arc = { + let mut map = sessions().lock().map_err(|_| "session lock poisoned".to_string())?; + Arc::clone(map.entry(key.clone()).or_insert_with(|| Arc::new(ApiSession { + closed: AtomicBool::new(false), + control: Default::default(), document: OnceLock::new(), + }))) + }; + // Kernel startup may block. It must never hold the registry lock needed by + // interrupt requests for an unrelated already-running document. + let initialized = arc.document.get_or_init(|| DocumentSession::open(abs, root).map(|mut session| { + session.adopt_control(arc.control.clone()); + Mutex::new(session) + })); + if let Err(error) = initialized { + let mut map = sessions().lock().map_err(|_| "session lock poisoned".to_string())?; + if map.get(&key).is_some_and(|old| Arc::ptr_eq(old, &arc)) { map.remove(&key); } + return Err(error.clone()); } - let session = DocumentSession::open(abs, root)?; - let arc = Arc::new(Mutex::new(session)); - map.insert(key, Arc::clone(&arc)); Ok(arc) } @@ -137,6 +172,7 @@ pub fn api_close(root: &Path, body: &str) -> serde_json::Value { .and_then(|mut m| m.remove(&key)); if let Some(arc) = removed { if let Ok(mut s) = arc.lock() { + arc.closed.store(true, Ordering::Release); s.close(); } } @@ -181,17 +217,54 @@ pub fn api_persist_cell(root: &Path, body: &str) -> serde_json::Value { } pub fn api_interrupt(root: &Path, body: &str) -> serde_json::Value { - with_session(root, body, "interrupted", |s| s.interrupt()) + let v = body_value(body); + // A control request addresses an existing loaded session. It neither reads + // the source again nor creates a process as a side effect of cancellation. + let (_, abs) = match requested_path(root, &v) { Ok(p) => p, Err(e) => return err(e) }; + let session = match sessions().lock() { + Ok(map) => match map.get(&abs.display().to_string()) { Some(s) => s.clone(), None => return err("no open session".into()) }, + Err(_) => return err("session lock poisoned".into()), + }; + if session.closed.load(Ordering::Acquire) { return err("session closed".into()); } + let mutex = match session.document.get() { + Some(Ok(mutex)) => mutex, Some(Err(error)) => return err(error.clone()), + None => return err("session is still opening; no evaluation is active".into()), + }; + let result = match mutex.try_lock() { + Ok(mut document) => if session.closed.load(Ordering::Acquire) { err("session closed".into()) } else { match document.interrupt() { + Ok(value) => ok_session(&document, serde_json::json!({ "interrupted": json_value(&value), "in_flight": false })), + Err(error) => err(error), + } }, + Err(TryLockError::WouldBlock) => match session.control.interrupt() { + Ok(value) => { + let receipt = json_value(&value); + let accepted = receipt["results"].as_array().is_some_and(|rows| rows.iter().all(|row| row["accepted"] == true)); + if accepted { serde_json::json!({ "ok": true, "interrupted": receipt, "in_flight": true }) } + else { serde_json::json!({ "ok": false, "error": "interrupt delivery failed", "interrupted": receipt, "in_flight": true }) } + }, + Err(error) => err(error), + }, + Err(TryLockError::Poisoned(_)) => err("session lock poisoned".into()), + }; + result } pub fn api_export_snapshot(root: &Path, body: &str) -> serde_json::Value { - with_session(root, body, "snapshot", |s| s.export_snapshot()) + let v = body_value(body); + let compact = match v.get("compact") { None | Some(serde_json::Value::Bool(false)) => false, + Some(serde_json::Value::Bool(true)) => true, _ => return err("compact must be boolean".into()) }; + with_session(root, body, "snapshot", |s| { + let snapshot = s.export_snapshot()?; + if compact { crate::host::foreign_doc::packing::pack(&snapshot) } else { Ok(snapshot) } + }) } pub fn api_import_snapshot(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) { + let snapshot = match json_to_value(&snapshot).and_then(|value| if crate::host::foreign_doc::packing::is_packed(&value) { + crate::host::foreign_doc::packing::unpack(&value) + } else { Ok(value) }) { Ok(v) => v, Err(e) => return err(e), }; @@ -201,7 +274,9 @@ pub fn api_import_snapshot(root: &Path, body: &str) -> serde_json::Value { pub fn api_replay_snapshot(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) { + let snapshot = match json_to_value(&snapshot).and_then(|value| if crate::host::foreign_doc::packing::is_packed(&value) { + crate::host::foreign_doc::packing::unpack(&value) + } else { Ok(value) }) { Ok(v) => v, Err(e) => return err(e), }; @@ -209,13 +284,28 @@ pub fn api_replay_snapshot(root: &Path, body: &str) -> serde_json::Value { } 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), + // Decode before serde_json::Value can overwrite duplicate recording fields. + let request = match crate::host::json::parse(&Value::Text(body.to_string())) { + Ok(value) => value, + Err(error) => return err(error), }; - with_session(root, body, "replay_check", |s| s.replay_check(&snapshot)) + let v = match value_to_json(&request) { Ok(v) => v, Err(e) => return err(e) }; + let (_, abs) = match path_of(root, &v) { Ok(path) => path, Err(e) => return err(e) }; + let Value::Map(fields) = request else { return err("request must be a map".into()) }; + let Some((_, snapshot)) = fields.iter().find(|(k,_)| k == "snapshot") else { return err("missing snapshot".into()) }; + let check = match DocumentSession::check_snapshot_isolated(&abs, root, snapshot) { + Ok(check) => check, Err(error) => return err(error), + }; + let existing = match sessions().lock() { + Ok(map) => map.get(&abs.display().to_string()).cloned(), + Err(_) => return err("session lock poisoned".into()), + }; + let extra = serde_json::json!({"replay_check": json_value(&check)}); + if let Some(arc) = existing { + match arc.lock() { Ok(session) => ok_session(&session, extra), Err(_) => err("session lock poisoned".into()) } + } else { + serde_json::json!({"ok": true, "replay_check": json_value(&check)}) + } } pub fn api_clear_cell(root: &Path, body: &str) -> serde_json::Value { @@ -443,3 +533,77 @@ mod tests { let _ = api_close(&root, open_body()); } } + +#[cfg(test)] +mod replay_api_tests { + use super::*; + #[test] + fn closed_api_session_cannot_be_reactivated_after_path_reopen() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let relative = format!("target/api-close-capability-{}.mq.md", std::process::id()); + let path = root.join(&relative); + std::fs::write(&path, "---\nimport:\n foreign: lib/foreign.mq.md\n---\n# main\n\n`会话` = > foreign.session\n\n`a` =\n```python\nn = 1\n```\n").unwrap(); + let old = get_or_open(&root, &path).unwrap(); + old.lock().unwrap().run_all(false).unwrap(); + let body = serde_json::json!({"path": relative}).to_string(); + assert_eq!(api_close(&root, &body)["ok"], true); + let fresh = get_or_open(&root, &path).unwrap(); + assert!(!Arc::ptr_eq(&old, &fresh)); + assert!(old.lock().is_err()); + fresh.lock().unwrap().run_all(false).unwrap(); + assert_eq!(api_close(&root, &body)["ok"], true); + std::fs::remove_file(path).unwrap(); + } + #[test] + fn strict_replay_endpoint_uses_isolation_and_preserves_warm_session() { + let root=PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let relative=format!("target/api-replay-isolated-{}.mq.md",std::process::id());let path=root.join(&relative); + std::fs::write(&path,"---\nimport:\n foreign: lib/foreign.mq.md\n---\n# main\n\n`会话` = > foreign.session\n\n`a` =\n```python\nstate = globals().get('state',0) + 1\nprint(state)\n```\n").unwrap(); + let arc=get_or_open(&root,&path).unwrap(); + let (snapshot,kernel)={let mut live=arc.lock().unwrap();live.run_all(false).unwrap();let snapshot=live.export_snapshot().unwrap();let kernel=live.cells[0].kernel.clone();live.set_value(&kernel,"state",Value::Int(99)).unwrap();(snapshot,kernel)}; + let before={let live=arc.lock().unwrap();live.summary()}; + let body=serde_json::json!({"path":relative,"snapshot":value_to_json(&snapshot).unwrap()}).to_string(); + let response=api_replay_check(&root,&body); + assert_eq!(response["ok"],true,"{response}");assert_eq!(response["replay_check"]["ok"],true,"{response}"); + assert_eq!(arc.lock().unwrap().summary(),before,"verification mutated live state"); + let duplicate=body.replacen("\"snapshot\":", "\"snapshot\":null,\"snapshot\":",1); + let bad=api_replay_check(&root,&duplicate);assert_eq!(bad["ok"],false);assert!(bad["error"].as_str().unwrap().contains("duplicate")); + assert_eq!(arc.lock().unwrap().summary(),before); + assert_eq!(arc.lock().unwrap().pull_value(&kernel,"state").unwrap(),Value::Int(99),"isolated verification reset the user's actual kernel"); + sessions().lock().unwrap().remove(&path.display().to_string());arc.lock().unwrap().close();std::fs::remove_file(path).unwrap(); + } + #[cfg(unix)] + #[test] + fn malformed_replay_does_not_start_declared_seed_kernel() { + use std::os::unix::fs::PermissionsExt; + let root=PathBuf::from(env!("CARGO_MANIFEST_DIR"));let pid=std::process::id(); + let relative=format!("target/api-replay-seeded-{pid}.mq.md");let path=root.join(&relative); + let wrapper=root.join(format!("target/api-replay-wrapper-{pid}.py"));let marker=root.join(format!("target/api-replay-marker-{pid}")); + let marker_literal=serde_json::to_string(&marker.display().to_string()).unwrap(); + let script=format!("#!/usr/bin/python3\nimport os,sys\nfrom pathlib import Path\nPath({marker_literal}).write_text('started')\nos.execv(sys.executable,[sys.executable]+sys.argv[1:])\n"); + std::fs::write(&wrapper,script).unwrap();std::fs::set_permissions(&wrapper,std::fs::Permissions::from_mode(0o755)).unwrap(); + let command=serde_json::to_string(&wrapper.display().to_string()).unwrap(); + let source=format!("---\nimport:\n foreign: lib/foreign.mq.md\n---\n# main\n\n`kernels` =\n| 名称 | 语言 | 命令 |\n|---|---|---|\n| py | python | {command} |\n\n`units` =\n| 单元 | 内核 |\n|---|---|\n| a | py |\n\n`initial` =\n| 内核 | 名称 | 值 |\n|---|---|---|\n| py | n | 1 |\n\n`会话` = > foreign.session kernels=`kernels` cells=`units` values=`initial`\n\n`a` =\n```python\nprint(n)\n```\n"); + std::fs::write(&path,source).unwrap();let mut recording=DocumentSession::open(&path,&root).unwrap(); + recording.run_all(false).unwrap();let snapshot=recording.export_snapshot().unwrap();recording.close();assert!(marker.exists());std::fs::remove_file(&marker).unwrap(); + let valid=value_to_json(&snapshot).unwrap();let mut malformed=valid.clone();malformed["actions"].as_array_mut().unwrap().pop(); + let cold = api_interrupt(&root, &serde_json::json!({"path":relative}).to_string()); + assert_eq!(cold["ok"], false); assert!(!marker.exists(), "cold interrupt started a kernel"); + let body=serde_json::json!({"path":relative,"snapshot":malformed}).to_string();let response=api_replay_check(&root,&body); + assert_eq!(response["ok"],false,"{response}");assert!(!marker.exists(),"malformed suffix started a seed kernel before preflight"); + assert!(!sessions().lock().unwrap().contains_key(&path.display().to_string())); + let packed = crate::host::foreign_doc::packing::pack(&snapshot).unwrap(); + let mut corrupt = value_to_json(&packed).unwrap(); corrupt["sha256"] = "bad".into(); + let response = api_replay_check(&root, &serde_json::json!({"path":relative,"snapshot":corrupt}).to_string()); + assert_eq!(response["ok"], false); assert!(!marker.exists(), "corrupt DAG started a seed kernel"); + let mut controls = valid.clone(); + controls["actions"][0]["controls"] = serde_json::json!([{"after_us":-1,"results":[]}]); + let response = api_replay_check(&root, &serde_json::json!({"path":relative,"snapshot":controls}).to_string()); + assert_eq!(response["ok"], false); assert!(!marker.exists(), "invalid control started a seed kernel"); + let response=api_replay_check(&root,&serde_json::json!({"path":relative,"snapshot":valid}).to_string()); + assert_eq!(response["replay_check"]["ok"],true,"{response}");assert!(marker.exists()); + assert!(!sessions().lock().unwrap().contains_key(&path.display().to_string())); + std::fs::remove_file(path).unwrap();std::fs::remove_file(wrapper).unwrap();std::fs::remove_file(marker).unwrap(); + } + +} diff --git a/src/view/notebook_api/interrupt_http_tests.rs b/src/view/notebook_api/interrupt_http_tests.rs new file mode 100644 index 0000000..32ce1cb --- /dev/null +++ b/src/view/notebook_api/interrupt_http_tests.rs @@ -0,0 +1,296 @@ +use super::*; +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpStream}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +struct HttpServer { + addr: SocketAddr, + stop: Arc, + thread: Option>, +} +impl HttpServer { + fn start(root: &Path) -> Self { + let root = Arc::new(crate::view::build_root(root).unwrap()); + let server = tiny_http::Server::http("127.0.0.1:0").unwrap(); + let addr = server.server_addr().to_ip().unwrap(); + let stop = Arc::new(AtomicBool::new(false)); + let stopping = stop.clone(); + let thread = std::thread::spawn(move || { + let mut workers = Vec::new(); + while !stopping.load(Ordering::SeqCst) { + if let Some(request) = server.recv_timeout(Duration::from_millis(50)).unwrap() { + let root = root.clone(); + workers.push(std::thread::spawn(move || { + crate::view::handle(&root, request) + })); + } + } + for worker in workers { + let _ = worker.join(); + } + }); + Self { + addr, + stop, + thread: Some(thread), + } + } +} +impl Drop for HttpServer { + fn drop(&mut self) { + self.stop.store(true, Ordering::SeqCst); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} +fn post(addr: SocketAddr, endpoint: &str, body: &serde_json::Value) -> serde_json::Value { + let bytes = body.to_string(); + let mut stream = TcpStream::connect(addr).unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(20))) + .unwrap(); + write!(stream, "POST /api/session/{endpoint} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{bytes}", bytes.len()).unwrap(); + let mut response = String::new(); + stream.read_to_string(&mut response).unwrap(); + let (_, body) = response.split_once("\r\n\r\n").unwrap(); + serde_json::from_str(body).unwrap_or_else(|e| panic!("{endpoint}: {e}: {response}")) +} + +#[test] +fn real_http_interrupt_preserves_store_stops_batch_and_replays_control_input() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let server = HttpServer::start(&root); + let repeats: usize = std::env::var("MARQDO_CANCEL_REPEATS") + .map(|s| s.parse().unwrap()) + .unwrap_or(1); + assert!(repeats > 0); + for language in ["python", "node"] { + for mode in ["cell/run", "cell/step", "run_all"] { + for trial in 0..repeats { + let id = format!( + "{}-{}-{language}", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos() + ); + let relative = format!("target/http-cancel-{id}.mq.md"); + let path = root.join(&relative); + let marker = root.join(format!("target/http-cancel-started-{id}")); + let suffix = root.join(format!("target/http-cancel-suffix-{id}")); + let marker_literal = serde_json::to_string(&marker.display().to_string()).unwrap(); + let suffix_literal = serde_json::to_string(&suffix.display().to_string()).unwrap(); + let (busy, after) = if language == "python" { + (format!("n = 99\nopen({marker_literal}, 'w').write('started')\nwhile True: pass"), + format!("open({suffix_literal}, 'w').write('unexpected')\nn = 100")) + } else { + (format!("n = 99; require('fs').writeFileSync({marker_literal}, 'started'); while (true) {{}}"), + format!("require('fs').writeFileSync({suffix_literal}, 'unexpected'); n = 100;")) + }; + let source = format!("---\nimport:\n foreign: lib/foreign.mq.md\n---\n# main\n\n`kernels` =\n| 名称 | 语言 |\n|---|---|\n| worker | {language} |\n\n`units` =\n| 单元 | 内核 |\n|---|---|\n| busy | worker |\n| after | worker |\n\n`initial` =\n| 内核 | 名称 | 值 |\n|---|---|---|\n| worker | n | 1 |\n\n`会话` = > foreign.session kernels=`kernels` cells=`units` values=`initial`\n\n`busy` =\n```{language}\n{busy}\n```\n\n`after` =\n```{language}\n{after}\n```\n"); + std::fs::write(&path, &source).unwrap(); + let body = serde_json::json!({"path": relative}); + assert_eq!(post(server.addr, "open", &body)["ok"], true); + let addr = server.addr; + let mut running_body = body.clone(); + if mode == "cell/run" { + running_body["cell"] = "busy".into(); + } + let runner = std::thread::spawn(move || post(addr, mode, &running_body)); + let deadline = Instant::now() + Duration::from_secs(3); + while !marker.exists() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(2)); + } + assert!( + marker.exists(), + "{language}: evaluation never reached its side effect" + ); + let slow_open = if language == "python" && mode == "run_all" && trial == 0 { + let slow_relative = format!("target/http-slow-open-{id}.mq.md"); + let slow_path = root.join(&slow_relative); + let wrapper = root.join(format!("target/http-slow-wrapper-{id}.py")); + let started = root.join(format!("target/http-slow-started-{id}")); + let literal = serde_json::to_string(&started.display().to_string()).unwrap(); + std::fs::write(&wrapper, format!("import os,sys,time\nfrom pathlib import Path\nPath({literal}).write_text('opening')\ntime.sleep(2)\nos.execv(sys.executable,[sys.executable]+sys.argv[1:])\n")).unwrap(); + let command = + serde_json::to_string(&format!("python3 {}", wrapper.display())).unwrap(); + std::fs::write(&slow_path, format!("---\nimport:\n foreign: lib/foreign.mq.md\n---\n# main\n\n`kernels` =\n| 名称 | 语言 | 命令 |\n|---|---|---|\n| slow | python | {command} |\n\n`initial` =\n| 内核 | 名称 | 值 |\n|---|---|---|\n| slow | n | 1 |\n\n`会话` = > foreign.session kernels=`kernels` values=`initial`\n")).unwrap(); + let slow_body = serde_json::json!({"path":slow_relative}); + let opening_body = slow_body.clone(); + let opener = std::thread::spawn(move || post(addr, "open", &opening_body)); + let deadline = Instant::now() + Duration::from_secs(3); + while !started.exists() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(2)); + } + assert!(started.exists(), "unrelated slow kernel never started"); + let start = Instant::now(); + let pending = post(addr, "interrupt", &slow_body); + assert_eq!(pending["ok"], false, "{pending}"); + assert!( + start.elapsed() < Duration::from_secs(1), + "control waited for initialization" + ); + Some((opener, slow_body, slow_path, wrapper, started)) + } else { + None + }; + std::thread::sleep(Duration::from_millis([0, 5, 25, 50][trial % 4])); + let removed_source = language == "node" && mode == "cell/run" && trial == 0; + if removed_source { + std::fs::remove_file(&path).unwrap(); + } + let start = Instant::now(); + let interrupted = post(server.addr, "interrupt", &body); + let latency = start.elapsed(); + assert!( + latency < Duration::from_secs(1), + "interrupt queued behind execution: {latency:?}" + ); + assert_eq!(interrupted["ok"], true, "{interrupted}"); + assert_eq!(interrupted["in_flight"], true, "{interrupted}"); + assert_eq!( + interrupted["interrupted"]["results"][0]["active"], true, + "{interrupted}" + ); + let run = runner.join().unwrap(); + let completion_latency = start.elapsed(); + assert!( + completion_latency < Duration::from_secs(1), + "cancel did not finish execution promptly: {completion_latency:?}" + ); + if removed_source { + std::fs::write(&path, &source).unwrap(); + } + if let Some((opener, slow_body, slow_path, wrapper, started)) = slow_open { + assert_eq!(opener.join().unwrap()["ok"], true); + assert_eq!(post(server.addr, "close", &slow_body)["ok"], true); + for file in [slow_path, wrapper, started] { + std::fs::remove_file(file).unwrap(); + } + } + let record = if mode == "run_all" { + assert_eq!(run["summary"]["cancelled"], true, "{run}"); + assert_eq!(run["summary"]["ran"], 1, "{run}"); + assert_eq!(run["summary"]["remaining"], 1, "{run}"); + &run["summary"]["records"][0] + } else { + &run["record"] + }; + assert_eq!(record["ok"], false, "{run}"); + assert_eq!(record["timed_out"], false, "{run}"); + assert_eq!(record["alive"], true, "{run}"); + assert!( + !suffix.exists(), + "cancelled batch executed its dependent suffix" + ); + let pull = serde_json::json!({"path":relative,"kernel":"worker","name":"n"}); + assert_eq!(post(server.addr, "state/pull", &pull)["value"], 99); + let exported = post(server.addr, "snapshot/export", &body); + let snapshot = &exported["snapshot"]; + assert_eq!(snapshot["version"], 5); + let controls: Vec<_> = snapshot["actions"] + .as_array() + .unwrap() + .iter() + .filter(|a| a.get("controls").is_some()) + .collect(); + assert_eq!(controls.len(), 1); + assert_eq!( + controls[0]["kind"], + match mode { + "cell/run" => "cell", + "cell/step" => "step", + _ => "run_all", + } + ); + assert_eq!(controls[0]["controls"].as_array().unwrap().len(), 1); + let replay = post( + server.addr, + "snapshot/replay_check", + &serde_json::json!({"path":relative,"snapshot":snapshot}), + ); + assert_eq!(replay["replay_check"]["ok"], true, + "{language}: expected snapshot={snapshot}; actual replay={replay}"); + assert!(!suffix.exists(), "replay executed a cancelled suffix"); + let compact = post( + server.addr, + "snapshot/export", + &serde_json::json!({"path":relative,"compact":true}), + ); + assert_eq!( + compact["snapshot"]["format"], "marqdo.history.dag", + "{compact}" + ); + let replay_compact = post( + server.addr, + "snapshot/replay_check", + &serde_json::json!({"path":relative,"snapshot":compact["snapshot"]}), + ); + assert_eq!( + replay_compact["replay_check"]["ok"], true, + "expected snapshot={snapshot}; actual compact replay={replay_compact}" + ); + let mut corrupt = compact["snapshot"].clone(); + corrupt["sha256"] = "bad".into(); + let rejected = post( + server.addr, + "snapshot/replay_check", + &serde_json::json!({"path":relative,"snapshot":corrupt}), + ); + assert_eq!(rejected["ok"], false, "{rejected}"); + // A malformed cancellation suffix must fail before any replay executes. + for mutation in ["time", "receipt", "duplicate", "version"] { + let mut bad = snapshot.clone(); + let index = bad["actions"] + .as_array() + .unwrap() + .iter() + .position(|a| a.get("controls").is_some()) + .unwrap(); + match mutation { + "time" => bad["actions"][index]["controls"][0]["after_us"] = (-1).into(), + "receipt" => { + bad["actions"][index]["controls"][0]["results"][0]["accepted"] = + false.into() + } + "duplicate" => { + let row = bad["actions"][index]["controls"][0]["results"][0].clone(); + bad["actions"][index]["controls"][0]["results"] + .as_array_mut() + .unwrap() + .push(row); + } + _ => bad["version"] = 4.into(), + } + let rejected = post( + server.addr, + "snapshot/replay_check", + &serde_json::json!({"path":relative,"snapshot":bad}), + ); + assert_eq!(rejected["ok"], false, "{mutation}: {rejected}"); + } + assert_eq!( + post(server.addr, "state/pull", &pull)["value"], + 99, + "checker disturbed warm state" + ); + assert_eq!(post(server.addr, "close", &body)["ok"], true); + println!("E11 {language} {mode} trial {trial}: real HTTP interrupt {:?}, retained n=99, skipped suffix, control-input replay and four malformed histories verified", latency); + println!( + "CONTROL_METRIC:{}", + serde_json::json!({ + "language": language, "mode": mode, "trial": trial, + "ack_ns": latency.as_nanos(), "completion_ns": completion_latency.as_nanos(), + "expected_store": 99, "plain_replay": true, "compact_replay": true, + "malformed_histories": 5, "correct": true + }) + ); + std::fs::remove_file(path).unwrap(); + std::fs::remove_file(marker).unwrap(); + } + } + } +} diff --git a/tests/applications/CASES.json b/tests/applications/CASES.json new file mode 100644 index 0000000..31aff4e --- /dev/null +++ b/tests/applications/CASES.json @@ -0,0 +1,1960 @@ +[ + { + "version": 1, + "id": "invoice-17-17-python", + "family": "invoice", + "seed": 17, + "size": 17, + "producer": "python", + "consumer": "node", + "input": [ + { + "id": "9007199254740993", + "region": "", + "units": 3, + "cents": 4972, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740993", + "region": "__proto__", + "units": -1, + "cents": 8851, + "discount": 3333, + "status": "void" + }, + { + "id": "9007199254740993", + "region": "华东", + "units": -3, + "cents": 4079, + "discount": 10000, + "status": "posted" + }, + { + "id": "9007199254740994", + "region": "😀", + "units": 1, + "cents": 8219, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740994", + "region": "😀", + "units": -1, + "cents": 9031, + "discount": 0, + "status": "posted" + }, + { + "id": "9007199254740994", + "region": "EU", + "units": 0, + "cents": 2473, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740995", + "region": "__proto__", + "units": 5, + "cents": 2024, + "discount": 0, + "status": "posted" + }, + { + "id": "9007199254740995", + "region": "__proto__", + "units": 3, + "cents": 1342, + "discount": 10000, + "status": "posted" + }, + { + "id": "9007199254740995", + "region": "", + "units": -1, + "cents": 6696, + "discount": 3333, + "status": "void" + }, + { + "id": "9007199254740996", + "region": "华东", + "units": 3, + "cents": 6099, + "discount": 0, + "status": "posted" + }, + { + "id": "9007199254740996", + "region": "__proto__", + "units": -3, + "cents": 7820, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740996", + "region": "", + "units": -3, + "cents": 6644, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740997", + "region": "华东", + "units": 5, + "cents": 3504, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740997", + "region": "", + "units": 2, + "cents": 953, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740997", + "region": "华东", + "units": 1, + "cents": 3158, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740998", + "region": "__proto__", + "units": 7, + "cents": 5040, + "discount": 3333, + "status": "void" + }, + { + "id": "9007199254740998", + "region": "__proto__", + "units": -1, + "cents": 5441, + "discount": 0, + "status": "posted" + } + ], + "bridge": [ + { + "id": "9007199254740993", + "region": "", + "units": 3, + "cents": 4972, + "discount": 3333 + }, + { + "id": "9007199254740994", + "region": "😀", + "units": 1, + "cents": 8219, + "discount": 3333 + }, + { + "id": "9007199254740995", + "region": "__proto__", + "units": 5, + "cents": 2024, + "discount": 0 + }, + { + "id": "9007199254740996", + "region": "华东", + "units": 3, + "cents": 6099, + "discount": 0 + }, + { + "id": "9007199254740997", + "region": "华东", + "units": 5, + "cents": 3504, + "discount": 500 + }, + { + "id": "9007199254740998", + "region": "__proto__", + "units": -1, + "cents": 5441, + "discount": 0 + } + ], + "answer": [ + { + "region": "__proto__", + "invoices": 2, + "net_cents": 4679 + }, + { + "region": "华东", + "invoices": 2, + "net_cents": 34941 + }, + { + "region": "", + "invoices": 1, + "net_cents": 9944 + }, + { + "region": "😀", + "invoices": 1, + "net_cents": 5479 + } + ] + }, + { + "version": 1, + "id": "invoice-17-17-node", + "family": "invoice", + "seed": 17, + "size": 17, + "producer": "node", + "consumer": "python", + "input": [ + { + "id": "9007199254740993", + "region": "", + "units": 3, + "cents": 4972, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740993", + "region": "__proto__", + "units": -1, + "cents": 8851, + "discount": 3333, + "status": "void" + }, + { + "id": "9007199254740993", + "region": "华东", + "units": -3, + "cents": 4079, + "discount": 10000, + "status": "posted" + }, + { + "id": "9007199254740994", + "region": "😀", + "units": 1, + "cents": 8219, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740994", + "region": "😀", + "units": -1, + "cents": 9031, + "discount": 0, + "status": "posted" + }, + { + "id": "9007199254740994", + "region": "EU", + "units": 0, + "cents": 2473, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740995", + "region": "__proto__", + "units": 5, + "cents": 2024, + "discount": 0, + "status": "posted" + }, + { + "id": "9007199254740995", + "region": "__proto__", + "units": 3, + "cents": 1342, + "discount": 10000, + "status": "posted" + }, + { + "id": "9007199254740995", + "region": "", + "units": -1, + "cents": 6696, + "discount": 3333, + "status": "void" + }, + { + "id": "9007199254740996", + "region": "华东", + "units": 3, + "cents": 6099, + "discount": 0, + "status": "posted" + }, + { + "id": "9007199254740996", + "region": "__proto__", + "units": -3, + "cents": 7820, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740996", + "region": "", + "units": -3, + "cents": 6644, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740997", + "region": "华东", + "units": 5, + "cents": 3504, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740997", + "region": "", + "units": 2, + "cents": 953, + "discount": 3333, + "status": "posted" + }, + { + "id": "9007199254740997", + "region": "华东", + "units": 1, + "cents": 3158, + "discount": 500, + "status": "posted" + }, + { + "id": "9007199254740998", + "region": "__proto__", + "units": 7, + "cents": 5040, + "discount": 3333, + "status": "void" + }, + { + "id": "9007199254740998", + "region": "__proto__", + "units": -1, + "cents": 5441, + "discount": 0, + "status": "posted" + } + ], + "bridge": [ + { + "id": "9007199254740993", + "region": "", + "units": 3, + "cents": 4972, + "discount": 3333 + }, + { + "id": "9007199254740994", + "region": "😀", + "units": 1, + "cents": 8219, + "discount": 3333 + }, + { + "id": "9007199254740995", + "region": "__proto__", + "units": 5, + "cents": 2024, + "discount": 0 + }, + { + "id": "9007199254740996", + "region": "华东", + "units": 3, + "cents": 6099, + "discount": 0 + }, + { + "id": "9007199254740997", + "region": "华东", + "units": 5, + "cents": 3504, + "discount": 500 + }, + { + "id": "9007199254740998", + "region": "__proto__", + "units": -1, + "cents": 5441, + "discount": 0 + } + ], + "answer": [ + { + "region": "__proto__", + "invoices": 2, + "net_cents": 4679 + }, + { + "region": "华东", + "invoices": 2, + "net_cents": 34941 + }, + { + "region": "", + "invoices": 1, + "net_cents": 9944 + }, + { + "region": "😀", + "invoices": 1, + "net_cents": 5479 + } + ] + }, + { + "version": 1, + "id": "routes-17-17-python", + "family": "routes", + "seed": 17, + "size": 17, + "producer": "python", + "consumer": "node", + "input": { + "source": "A", + "nodes": [ + "A", + "B", + "C", + "D", + "😀", + "", + "孤岛" + ], + "edges": [ + { + "from": "A", + "to": "B", + "weight": 0 + }, + { + "from": "A", + "to": "B", + "weight": 9 + }, + { + "from": "B", + "to": "C", + "weight": 2 + }, + { + "from": "A", + "to": "A", + "weight": 0 + }, + { + "from": "C", + "to": "D", + "weight": -1 + }, + { + "from": "😀", + "to": "D", + "weight": 8 + }, + { + "from": "C", + "to": "C", + "weight": 4 + }, + { + "from": "", + "to": "", + "weight": 16 + }, + { + "from": "", + "to": "C", + "weight": 2 + }, + { + "from": "A", + "to": "B", + "weight": 11 + }, + { + "from": "", + "to": "D", + "weight": 7 + }, + { + "from": "😀", + "to": "C", + "weight": 19 + }, + { + "from": "", + "to": "", + "weight": 11 + }, + { + "from": "B", + "to": "😀", + "weight": 0 + }, + { + "from": "B", + "to": "B", + "weight": 3 + }, + { + "from": "", + "to": "😀", + "weight": 16 + }, + { + "from": "", + "to": "B", + "weight": 9 + } + ] + }, + "bridge": { + "source": "A", + "nodes": [ + "A", + "B", + "C", + "D", + "孤岛", + "", + "😀" + ], + "edges": [ + { + "from": "A", + "to": "A", + "weight": 0 + }, + { + "from": "A", + "to": "B", + "weight": 0 + }, + { + "from": "B", + "to": "B", + "weight": 3 + }, + { + "from": "B", + "to": "C", + "weight": 2 + }, + { + "from": "B", + "to": "😀", + "weight": 0 + }, + { + "from": "C", + "to": "C", + "weight": 4 + }, + { + "from": "", + "to": "B", + "weight": 9 + }, + { + "from": "", + "to": "C", + "weight": 2 + }, + { + "from": "", + "to": "D", + "weight": 7 + }, + { + "from": "", + "to": "", + "weight": 11 + }, + { + "from": "", + "to": "😀", + "weight": 16 + }, + { + "from": "😀", + "to": "C", + "weight": 19 + }, + { + "from": "😀", + "to": "D", + "weight": 8 + } + ] + }, + "answer": [ + { + "node": "A", + "distance": 0 + }, + { + "node": "B", + "distance": 0 + }, + { + "node": "C", + "distance": 2 + }, + { + "node": "D", + "distance": 8 + }, + { + "node": "孤岛", + "distance": null + }, + { + "node": "", + "distance": null + }, + { + "node": "😀", + "distance": 0 + } + ] + }, + { + "version": 1, + "id": "routes-17-17-node", + "family": "routes", + "seed": 17, + "size": 17, + "producer": "node", + "consumer": "python", + "input": { + "source": "A", + "nodes": [ + "A", + "B", + "C", + "D", + "😀", + "", + "孤岛" + ], + "edges": [ + { + "from": "A", + "to": "B", + "weight": 0 + }, + { + "from": "A", + "to": "B", + "weight": 9 + }, + { + "from": "B", + "to": "C", + "weight": 2 + }, + { + "from": "A", + "to": "A", + "weight": 0 + }, + { + "from": "C", + "to": "D", + "weight": -1 + }, + { + "from": "😀", + "to": "D", + "weight": 8 + }, + { + "from": "C", + "to": "C", + "weight": 4 + }, + { + "from": "", + "to": "", + "weight": 16 + }, + { + "from": "", + "to": "C", + "weight": 2 + }, + { + "from": "A", + "to": "B", + "weight": 11 + }, + { + "from": "", + "to": "D", + "weight": 7 + }, + { + "from": "😀", + "to": "C", + "weight": 19 + }, + { + "from": "", + "to": "", + "weight": 11 + }, + { + "from": "B", + "to": "😀", + "weight": 0 + }, + { + "from": "B", + "to": "B", + "weight": 3 + }, + { + "from": "", + "to": "😀", + "weight": 16 + }, + { + "from": "", + "to": "B", + "weight": 9 + } + ] + }, + "bridge": { + "source": "A", + "nodes": [ + "A", + "B", + "C", + "D", + "孤岛", + "", + "😀" + ], + "edges": [ + { + "from": "A", + "to": "A", + "weight": 0 + }, + { + "from": "A", + "to": "B", + "weight": 0 + }, + { + "from": "B", + "to": "B", + "weight": 3 + }, + { + "from": "B", + "to": "C", + "weight": 2 + }, + { + "from": "B", + "to": "😀", + "weight": 0 + }, + { + "from": "C", + "to": "C", + "weight": 4 + }, + { + "from": "", + "to": "B", + "weight": 9 + }, + { + "from": "", + "to": "C", + "weight": 2 + }, + { + "from": "", + "to": "D", + "weight": 7 + }, + { + "from": "", + "to": "", + "weight": 11 + }, + { + "from": "", + "to": "😀", + "weight": 16 + }, + { + "from": "😀", + "to": "C", + "weight": 19 + }, + { + "from": "😀", + "to": "D", + "weight": 8 + } + ] + }, + "answer": [ + { + "node": "A", + "distance": 0 + }, + { + "node": "B", + "distance": 0 + }, + { + "node": "C", + "distance": 2 + }, + { + "node": "D", + "distance": 8 + }, + { + "node": "孤岛", + "distance": null + }, + { + "node": "", + "distance": null + }, + { + "node": "😀", + "distance": 0 + } + ] + }, + { + "version": 1, + "id": "concordance-17-17-python", + "family": "concordance", + "seed": 17, + "size": 17, + "producer": "python", + "consumer": "node", + "input": [ + { + "id": "9007199254740993", + "text": "café café A __proto__ 😀 CAFÉ alpha CAFÉ constructor ß constructor constructor" + }, + { + "id": "9007199254740994", + "text": "alpha Ä" + }, + { + "id": "9007199254740995", + "text": "constructor\n😀" + }, + { + "id": "9007199254740996", + "text": "😀\nconstructor\n\ncafé\nÄ" + }, + { + "id": "9007199254740997", + "text": "alpha A constructor constructor alpha Ä café Ä __proto__ café" + }, + { + "id": "9007199254740998", + "text": "😀 Ä ß A CAFÉ" + }, + { + "id": "9007199254740999", + "text": "alpha\nÄ\nÄ\nCAFÉ\n__proto__\nconstructor\n😀\nalpha" + }, + { + "id": "9007199254741000", + "text": "café\tcafé\tA\tÄ\t😀\t__proto__\tcafé\tÄ\talpha\tÄ" + }, + { + "id": "9007199254741001", + "text": "\t__proto__\tcafé\tÄ\talpha\talpha\tÄ" + }, + { + "id": "9007199254741002", + "text": "ß\nalpha\nalpha\nconstructor\nA\nalpha" + }, + { + "id": "9007199254741003", + "text": "A\nCAFÉ\nconstructor\n😀\nconstructor\nalpha\nconstructor\nÄ\nalpha\nÄ" + }, + { + "id": "9007199254741004", + "text": "CAFÉ\talpha\tß\t😀\t😀\talpha\tCAFÉ\talpha\t😀\tconstructor\tÄ\tconstructor\t😀" + }, + { + "id": "9007199254741005", + "text": "😀 😀 ß" + }, + { + "id": "9007199254741006", + "text": "CAFÉ ß constructor alpha 😀 alpha 😀 😀 Ä constructor alpha A" + }, + { + "id": "9007199254741007", + "text": "café\nA\nalpha\nCAFÉ\nß\nß\nÄ\nA\nÄ" + }, + { + "id": "9007199254741008", + "text": "ß\nconstructor\nA\n😀\nß\nCAFÉ\nA" + }, + { + "id": "9007199254741009", + "text": "alpha café café Ä Ä  Ä A __proto__ café CAFÉ __proto__ __proto__" + } + ], + "bridge": [ + { + "id": "9007199254740993", + "tokens": [ + "café", + "café", + "a", + "__proto__", + "😀", + "cafÉ", + "alpha", + "cafÉ", + "constructor", + "ß", + "constructor", + "constructor" + ] + }, + { + "id": "9007199254740994", + "tokens": [ + "alpha", + "Ä" + ] + }, + { + "id": "9007199254740995", + "tokens": [ + "constructor", + "😀" + ] + }, + { + "id": "9007199254740996", + "tokens": [ + "😀", + "constructor", + "", + "café", + "Ä" + ] + }, + { + "id": "9007199254740997", + "tokens": [ + "alpha", + "a", + "constructor", + "constructor", + "alpha", + "Ä", + "café", + "Ä", + "__proto__", + "café" + ] + }, + { + "id": "9007199254740998", + "tokens": [ + "😀", + "Ä", + "ß", + "a", + "cafÉ" + ] + }, + { + "id": "9007199254740999", + "tokens": [ + "alpha", + "Ä", + "Ä", + "cafÉ", + "__proto__", + "constructor", + "😀", + "alpha" + ] + }, + { + "id": "9007199254741000", + "tokens": [ + "café", + "café", + "a", + "Ä", + "😀", + "__proto__", + "café", + "Ä", + "alpha", + "Ä" + ] + }, + { + "id": "9007199254741001", + "tokens": [ + "", + "__proto__", + "café", + "Ä", + "alpha", + "alpha", + "Ä" + ] + }, + { + "id": "9007199254741002", + "tokens": [ + "ß", + "alpha", + "alpha", + "constructor", + "a", + "alpha" + ] + }, + { + "id": "9007199254741003", + "tokens": [ + "a", + "cafÉ", + "constructor", + "😀", + "constructor", + "alpha", + "constructor", + "Ä", + "alpha", + "Ä" + ] + }, + { + "id": "9007199254741004", + "tokens": [ + "cafÉ", + "alpha", + "ß", + "😀", + "😀", + "alpha", + "cafÉ", + "alpha", + "😀", + "constructor", + "Ä", + "constructor", + "😀" + ] + }, + { + "id": "9007199254741005", + "tokens": [ + "😀", + "😀", + "ß" + ] + }, + { + "id": "9007199254741006", + "tokens": [ + "cafÉ", + "ß", + "constructor", + "alpha", + "😀", + "alpha", + "😀", + "😀", + "Ä", + "constructor", + "alpha", + "a" + ] + }, + { + "id": "9007199254741007", + "tokens": [ + "café", + "a", + "alpha", + "cafÉ", + "ß", + "ß", + "Ä", + "a", + "Ä" + ] + }, + { + "id": "9007199254741008", + "tokens": [ + "ß", + "constructor", + "a", + "😀", + "ß", + "cafÉ", + "a" + ] + }, + { + "id": "9007199254741009", + "tokens": [ + "alpha", + "café", + "café", + "Ä", + "Ä", + "", + "Ä", + "a", + "__proto__", + "café", + "cafÉ", + "__proto__", + "__proto__" + ] + } + ], + "answer": [ + { + "word": "__proto__", + "occurrences": 8, + "documents": 6 + }, + { + "word": "a", + "occurrences": 12, + "documents": 10 + }, + { + "word": "alpha", + "occurrences": 22, + "documents": 12 + }, + { + "word": "cafÉ", + "occurrences": 11, + "documents": 9 + }, + { + "word": "café", + "occurrences": 13, + "documents": 7 + }, + { + "word": "constructor", + "occurrences": 17, + "documents": 10 + }, + { + "word": "Ä", + "occurrences": 21, + "documents": 12 + }, + { + "word": "ß", + "occurrences": 10, + "documents": 8 + }, + { + "word": "", + "occurrences": 3, + "documents": 3 + }, + { + "word": "😀", + "occurrences": 17, + "documents": 11 + } + ] + }, + { + "version": 1, + "id": "concordance-17-17-node", + "family": "concordance", + "seed": 17, + "size": 17, + "producer": "node", + "consumer": "python", + "input": [ + { + "id": "9007199254740993", + "text": "café café A __proto__ 😀 CAFÉ alpha CAFÉ constructor ß constructor constructor" + }, + { + "id": "9007199254740994", + "text": "alpha Ä" + }, + { + "id": "9007199254740995", + "text": "constructor\n😀" + }, + { + "id": "9007199254740996", + "text": "😀\nconstructor\n\ncafé\nÄ" + }, + { + "id": "9007199254740997", + "text": "alpha A constructor constructor alpha Ä café Ä __proto__ café" + }, + { + "id": "9007199254740998", + "text": "😀 Ä ß A CAFÉ" + }, + { + "id": "9007199254740999", + "text": "alpha\nÄ\nÄ\nCAFÉ\n__proto__\nconstructor\n😀\nalpha" + }, + { + "id": "9007199254741000", + "text": "café\tcafé\tA\tÄ\t😀\t__proto__\tcafé\tÄ\talpha\tÄ" + }, + { + "id": "9007199254741001", + "text": "\t__proto__\tcafé\tÄ\talpha\talpha\tÄ" + }, + { + "id": "9007199254741002", + "text": "ß\nalpha\nalpha\nconstructor\nA\nalpha" + }, + { + "id": "9007199254741003", + "text": "A\nCAFÉ\nconstructor\n😀\nconstructor\nalpha\nconstructor\nÄ\nalpha\nÄ" + }, + { + "id": "9007199254741004", + "text": "CAFÉ\talpha\tß\t😀\t😀\talpha\tCAFÉ\talpha\t😀\tconstructor\tÄ\tconstructor\t😀" + }, + { + "id": "9007199254741005", + "text": "😀 😀 ß" + }, + { + "id": "9007199254741006", + "text": "CAFÉ ß constructor alpha 😀 alpha 😀 😀 Ä constructor alpha A" + }, + { + "id": "9007199254741007", + "text": "café\nA\nalpha\nCAFÉ\nß\nß\nÄ\nA\nÄ" + }, + { + "id": "9007199254741008", + "text": "ß\nconstructor\nA\n😀\nß\nCAFÉ\nA" + }, + { + "id": "9007199254741009", + "text": "alpha café café Ä Ä  Ä A __proto__ café CAFÉ __proto__ __proto__" + } + ], + "bridge": [ + { + "id": "9007199254740993", + "tokens": [ + "café", + "café", + "a", + "__proto__", + "😀", + "cafÉ", + "alpha", + "cafÉ", + "constructor", + "ß", + "constructor", + "constructor" + ] + }, + { + "id": "9007199254740994", + "tokens": [ + "alpha", + "Ä" + ] + }, + { + "id": "9007199254740995", + "tokens": [ + "constructor", + "😀" + ] + }, + { + "id": "9007199254740996", + "tokens": [ + "😀", + "constructor", + "", + "café", + "Ä" + ] + }, + { + "id": "9007199254740997", + "tokens": [ + "alpha", + "a", + "constructor", + "constructor", + "alpha", + "Ä", + "café", + "Ä", + "__proto__", + "café" + ] + }, + { + "id": "9007199254740998", + "tokens": [ + "😀", + "Ä", + "ß", + "a", + "cafÉ" + ] + }, + { + "id": "9007199254740999", + "tokens": [ + "alpha", + "Ä", + "Ä", + "cafÉ", + "__proto__", + "constructor", + "😀", + "alpha" + ] + }, + { + "id": "9007199254741000", + "tokens": [ + "café", + "café", + "a", + "Ä", + "😀", + "__proto__", + "café", + "Ä", + "alpha", + "Ä" + ] + }, + { + "id": "9007199254741001", + "tokens": [ + "", + "__proto__", + "café", + "Ä", + "alpha", + "alpha", + "Ä" + ] + }, + { + "id": "9007199254741002", + "tokens": [ + "ß", + "alpha", + "alpha", + "constructor", + "a", + "alpha" + ] + }, + { + "id": "9007199254741003", + "tokens": [ + "a", + "cafÉ", + "constructor", + "😀", + "constructor", + "alpha", + "constructor", + "Ä", + "alpha", + "Ä" + ] + }, + { + "id": "9007199254741004", + "tokens": [ + "cafÉ", + "alpha", + "ß", + "😀", + "😀", + "alpha", + "cafÉ", + "alpha", + "😀", + "constructor", + "Ä", + "constructor", + "😀" + ] + }, + { + "id": "9007199254741005", + "tokens": [ + "😀", + "😀", + "ß" + ] + }, + { + "id": "9007199254741006", + "tokens": [ + "cafÉ", + "ß", + "constructor", + "alpha", + "😀", + "alpha", + "😀", + "😀", + "Ä", + "constructor", + "alpha", + "a" + ] + }, + { + "id": "9007199254741007", + "tokens": [ + "café", + "a", + "alpha", + "cafÉ", + "ß", + "ß", + "Ä", + "a", + "Ä" + ] + }, + { + "id": "9007199254741008", + "tokens": [ + "ß", + "constructor", + "a", + "😀", + "ß", + "cafÉ", + "a" + ] + }, + { + "id": "9007199254741009", + "tokens": [ + "alpha", + "café", + "café", + "Ä", + "Ä", + "", + "Ä", + "a", + "__proto__", + "café", + "cafÉ", + "__proto__", + "__proto__" + ] + } + ], + "answer": [ + { + "word": "__proto__", + "occurrences": 8, + "documents": 6 + }, + { + "word": "a", + "occurrences": 12, + "documents": 10 + }, + { + "word": "alpha", + "occurrences": 22, + "documents": 12 + }, + { + "word": "cafÉ", + "occurrences": 11, + "documents": 9 + }, + { + "word": "café", + "occurrences": 13, + "documents": 7 + }, + { + "word": "constructor", + "occurrences": 17, + "documents": 10 + }, + { + "word": "Ä", + "occurrences": 21, + "documents": 12 + }, + { + "word": "ß", + "occurrences": 10, + "documents": 8 + }, + { + "word": "", + "occurrences": 3, + "documents": 3 + }, + { + "word": "😀", + "occurrences": 17, + "documents": 11 + } + ] + }, + { + "version": 1, + "id": "wine-17-31-python", + "family": "wine", + "seed": 17, + "size": 31, + "producer": "python", + "consumer": "node", + "input": "\"fixed acidity\";\"volatile acidity\";\"citric acid\";\"residual sugar\";\"chlorides\";\"free sulfur dioxide\";\"total sulfur dioxide\";\"density\";\"pH\";\"sulphates\";\"alcohol\";\"quality\"\n7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5\n7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5\n7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5\n11.2;0.28;0.56;1.9;0.075;17;60;0.998;3.16;0.58;9.8;6\n7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5\n7.4;0.66;0;1.8;0.075;13;40;0.9978;3.51;0.56;9.4;5\n7.9;0.6;0.06;1.6;0.069;15;59;0.9964;3.3;0.46;9.4;5\n7.3;0.65;0;1.2;0.065;15;21;0.9946;3.39;0.47;10;7\n7.8;0.58;0.02;2;0.073;9;18;0.9968;3.36;0.57;9.5;7\n7.5;0.5;0.36;6.1;0.071;17;102;0.9978;3.35;0.8;10.5;5\n6.7;0.58;0.08;1.8;0.097;15;65;0.9959;3.28;0.54;9.2;5\n7.5;0.5;0.36;6.1;0.071;17;102;0.9978;3.35;0.8;10.5;5\n5.6;0.615;0;1.6;0.089;16;59;0.9943;3.58;0.52;9.9;5\n7.8;0.61;0.29;1.6;0.114;9;29;0.9974;3.26;1.56;9.1;5\n8.9;0.62;0.18;3.8;0.176;52;145;0.9986;3.16;0.88;9.2;5\n8.9;0.62;0.19;3.9;0.17;51;148;0.9986;3.17;0.93;9.2;5\n8.5;0.28;0.56;1.8;0.092;35;103;0.9969;3.3;0.75;10.5;7\n8.1;0.56;0.28;1.7;0.368;16;56;0.9968;3.11;1.28;9.3;5\n7.4;0.59;0.08;4.4;0.086;6;29;0.9974;3.38;0.5;9;4\n7.9;0.32;0.51;1.8;0.341;17;56;0.9969;3.04;1.08;9.2;6\n8.9;0.22;0.48;1.8;0.077;29;60;0.9968;3.39;0.53;9.4;6\n7.6;0.39;0.31;2.3;0.082;23;71;0.9982;3.52;0.65;9.7;5\n7.9;0.43;0.21;1.6;0.106;10;37;0.9966;3.17;0.91;9.5;5\n8.5;0.49;0.11;2.3;0.084;9;67;0.9968;3.17;0.53;9.4;5\n6.9;0.4;0.14;2.4;0.085;21;40;0.9968;3.43;0.63;9.7;6\n6.3;0.39;0.16;1.4;0.08;11;23;0.9955;3.34;0.56;9.3;5\n7.6;0.41;0.24;1.8;0.08;4;11;0.9962;3.28;0.59;9.5;5\n7.9;0.43;0.21;1.6;0.106;10;37;0.9966;3.17;0.91;9.5;5\n7.1;0.71;0;1.9;0.08;14;35;0.9972;3.47;0.55;9.4;5\n7.8;0.645;0;2;0.082;8;16;0.9964;3.38;0.59;9.8;6\n6.7;0.675;0.07;2.4;0.089;17;82;0.9958;3.35;0.54;10.1;5\n", + "bridge": [ + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 351 + }, + { + "quality": 5, + "density_ppm": 996800, + "ph_cent": 320 + }, + { + "quality": 5, + "density_ppm": 997000, + "ph_cent": 326 + }, + { + "quality": 6, + "density_ppm": 998000, + "ph_cent": 316 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 351 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 351 + }, + { + "quality": 5, + "density_ppm": 996400, + "ph_cent": 330 + }, + { + "quality": 7, + "density_ppm": 994600, + "ph_cent": 339 + }, + { + "quality": 7, + "density_ppm": 996800, + "ph_cent": 336 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 335 + }, + { + "quality": 5, + "density_ppm": 995900, + "ph_cent": 328 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 335 + }, + { + "quality": 5, + "density_ppm": 994300, + "ph_cent": 358 + }, + { + "quality": 5, + "density_ppm": 997400, + "ph_cent": 326 + }, + { + "quality": 5, + "density_ppm": 998600, + "ph_cent": 316 + }, + { + "quality": 5, + "density_ppm": 998600, + "ph_cent": 317 + }, + { + "quality": 7, + "density_ppm": 996900, + "ph_cent": 330 + }, + { + "quality": 5, + "density_ppm": 996800, + "ph_cent": 311 + }, + { + "quality": 4, + "density_ppm": 997400, + "ph_cent": 338 + }, + { + "quality": 6, + "density_ppm": 996900, + "ph_cent": 304 + }, + { + "quality": 6, + "density_ppm": 996800, + "ph_cent": 339 + }, + { + "quality": 5, + "density_ppm": 998200, + "ph_cent": 352 + }, + { + "quality": 5, + "density_ppm": 996600, + "ph_cent": 317 + }, + { + "quality": 5, + "density_ppm": 996800, + "ph_cent": 317 + }, + { + "quality": 6, + "density_ppm": 996800, + "ph_cent": 343 + }, + { + "quality": 5, + "density_ppm": 995500, + "ph_cent": 334 + }, + { + "quality": 5, + "density_ppm": 996200, + "ph_cent": 328 + }, + { + "quality": 5, + "density_ppm": 996600, + "ph_cent": 317 + }, + { + "quality": 5, + "density_ppm": 997200, + "ph_cent": 347 + }, + { + "quality": 6, + "density_ppm": 996400, + "ph_cent": 338 + }, + { + "quality": 5, + "density_ppm": 995800, + "ph_cent": 335 + } + ], + "answer": [ + { + "quality": 4, + "count": 1, + "sum_density_ppm": 997400, + "min_ph_cent": 338, + "max_ph_cent": 338 + }, + { + "quality": 5, + "count": 22, + "sum_density_ppm": 21933700, + "min_ph_cent": 311, + "max_ph_cent": 358 + }, + { + "quality": 6, + "count": 5, + "sum_density_ppm": 4984900, + "min_ph_cent": 304, + "max_ph_cent": 343 + }, + { + "quality": 7, + "count": 3, + "sum_density_ppm": 2988300, + "min_ph_cent": 330, + "max_ph_cent": 339 + } + ] + }, + { + "version": 1, + "id": "wine-17-31-node", + "family": "wine", + "seed": 17, + "size": 31, + "producer": "node", + "consumer": "python", + "input": "\"fixed acidity\";\"volatile acidity\";\"citric acid\";\"residual sugar\";\"chlorides\";\"free sulfur dioxide\";\"total sulfur dioxide\";\"density\";\"pH\";\"sulphates\";\"alcohol\";\"quality\"\n7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5\n7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5\n7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5\n11.2;0.28;0.56;1.9;0.075;17;60;0.998;3.16;0.58;9.8;6\n7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5\n7.4;0.66;0;1.8;0.075;13;40;0.9978;3.51;0.56;9.4;5\n7.9;0.6;0.06;1.6;0.069;15;59;0.9964;3.3;0.46;9.4;5\n7.3;0.65;0;1.2;0.065;15;21;0.9946;3.39;0.47;10;7\n7.8;0.58;0.02;2;0.073;9;18;0.9968;3.36;0.57;9.5;7\n7.5;0.5;0.36;6.1;0.071;17;102;0.9978;3.35;0.8;10.5;5\n6.7;0.58;0.08;1.8;0.097;15;65;0.9959;3.28;0.54;9.2;5\n7.5;0.5;0.36;6.1;0.071;17;102;0.9978;3.35;0.8;10.5;5\n5.6;0.615;0;1.6;0.089;16;59;0.9943;3.58;0.52;9.9;5\n7.8;0.61;0.29;1.6;0.114;9;29;0.9974;3.26;1.56;9.1;5\n8.9;0.62;0.18;3.8;0.176;52;145;0.9986;3.16;0.88;9.2;5\n8.9;0.62;0.19;3.9;0.17;51;148;0.9986;3.17;0.93;9.2;5\n8.5;0.28;0.56;1.8;0.092;35;103;0.9969;3.3;0.75;10.5;7\n8.1;0.56;0.28;1.7;0.368;16;56;0.9968;3.11;1.28;9.3;5\n7.4;0.59;0.08;4.4;0.086;6;29;0.9974;3.38;0.5;9;4\n7.9;0.32;0.51;1.8;0.341;17;56;0.9969;3.04;1.08;9.2;6\n8.9;0.22;0.48;1.8;0.077;29;60;0.9968;3.39;0.53;9.4;6\n7.6;0.39;0.31;2.3;0.082;23;71;0.9982;3.52;0.65;9.7;5\n7.9;0.43;0.21;1.6;0.106;10;37;0.9966;3.17;0.91;9.5;5\n8.5;0.49;0.11;2.3;0.084;9;67;0.9968;3.17;0.53;9.4;5\n6.9;0.4;0.14;2.4;0.085;21;40;0.9968;3.43;0.63;9.7;6\n6.3;0.39;0.16;1.4;0.08;11;23;0.9955;3.34;0.56;9.3;5\n7.6;0.41;0.24;1.8;0.08;4;11;0.9962;3.28;0.59;9.5;5\n7.9;0.43;0.21;1.6;0.106;10;37;0.9966;3.17;0.91;9.5;5\n7.1;0.71;0;1.9;0.08;14;35;0.9972;3.47;0.55;9.4;5\n7.8;0.645;0;2;0.082;8;16;0.9964;3.38;0.59;9.8;6\n6.7;0.675;0.07;2.4;0.089;17;82;0.9958;3.35;0.54;10.1;5\n", + "bridge": [ + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 351 + }, + { + "quality": 5, + "density_ppm": 996800, + "ph_cent": 320 + }, + { + "quality": 5, + "density_ppm": 997000, + "ph_cent": 326 + }, + { + "quality": 6, + "density_ppm": 998000, + "ph_cent": 316 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 351 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 351 + }, + { + "quality": 5, + "density_ppm": 996400, + "ph_cent": 330 + }, + { + "quality": 7, + "density_ppm": 994600, + "ph_cent": 339 + }, + { + "quality": 7, + "density_ppm": 996800, + "ph_cent": 336 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 335 + }, + { + "quality": 5, + "density_ppm": 995900, + "ph_cent": 328 + }, + { + "quality": 5, + "density_ppm": 997800, + "ph_cent": 335 + }, + { + "quality": 5, + "density_ppm": 994300, + "ph_cent": 358 + }, + { + "quality": 5, + "density_ppm": 997400, + "ph_cent": 326 + }, + { + "quality": 5, + "density_ppm": 998600, + "ph_cent": 316 + }, + { + "quality": 5, + "density_ppm": 998600, + "ph_cent": 317 + }, + { + "quality": 7, + "density_ppm": 996900, + "ph_cent": 330 + }, + { + "quality": 5, + "density_ppm": 996800, + "ph_cent": 311 + }, + { + "quality": 4, + "density_ppm": 997400, + "ph_cent": 338 + }, + { + "quality": 6, + "density_ppm": 996900, + "ph_cent": 304 + }, + { + "quality": 6, + "density_ppm": 996800, + "ph_cent": 339 + }, + { + "quality": 5, + "density_ppm": 998200, + "ph_cent": 352 + }, + { + "quality": 5, + "density_ppm": 996600, + "ph_cent": 317 + }, + { + "quality": 5, + "density_ppm": 996800, + "ph_cent": 317 + }, + { + "quality": 6, + "density_ppm": 996800, + "ph_cent": 343 + }, + { + "quality": 5, + "density_ppm": 995500, + "ph_cent": 334 + }, + { + "quality": 5, + "density_ppm": 996200, + "ph_cent": 328 + }, + { + "quality": 5, + "density_ppm": 996600, + "ph_cent": 317 + }, + { + "quality": 5, + "density_ppm": 997200, + "ph_cent": 347 + }, + { + "quality": 6, + "density_ppm": 996400, + "ph_cent": 338 + }, + { + "quality": 5, + "density_ppm": 995800, + "ph_cent": 335 + } + ], + "answer": [ + { + "quality": 4, + "count": 1, + "sum_density_ppm": 997400, + "min_ph_cent": 338, + "max_ph_cent": 338 + }, + { + "quality": 5, + "count": 22, + "sum_density_ppm": 21933700, + "min_ph_cent": 311, + "max_ph_cent": 358 + }, + { + "quality": 6, + "count": 5, + "sum_density_ppm": 4984900, + "min_ph_cent": 304, + "max_ph_cent": 343 + }, + { + "quality": 7, + "count": 3, + "sum_density_ppm": 2988300, + "min_ph_cent": 330, + "max_ph_cent": 339 + } + ] + } +] diff --git a/tests/applications/README.md b/tests/applications/README.md new file mode 100644 index 0000000..715c3ab --- /dev/null +++ b/tests/applications/README.md @@ -0,0 +1,34 @@ +# Application composition corpus + +Four authored tasks exercise the actual document loader, tree/bytecode evaluators +and interactive history API. Each has separate Python and Node implementations +of the producer and consumer. `CASES.json` is the eight-case regression subset; +the reviewer driver generates 80 size/seed/direction instances before execution. +They are four application families, not 80 independent real-user applications. + +- Invoice ledger: discard void rows, first-posted invoice deduplication, integer + cents/discount accounting, signed refunds with floor division, regional totals. +- Routes: minimum nonnegative parallel edge, zero-cost edges, disconnected nodes + and weighted single-source distances. Python uses heap-based Dijkstra, Node a + linear frontier; independent reference uses SQL and all-pairs Floyd-Warshall. +- Concordance: NFC plus **ASCII** lowercase, explicit space/tab/LF/CR/NBSP + separators, occurrence/document frequencies, UTF-8 byte order. This is not + Unicode full case folding. Wide IDs remain strings; special object-key text, + combining marks, BMP and astral characters are included. +- Wine: UCI red-wine CSV, exact decimal density/pH scaling to integers and + quality-group count/sum/min/max. Full input contains 1599 rows; no predictor + accuracy or general machine-learning performance is claimed. + +Independent SQL/Fraction/Floyd references live in the reviewer experiment +`application_cases.py`; no foreign program output supplies expected answers. +The raw/public-data digest, citation, license and retrieval source are preserved +in `data/PROVENANCE.json`. Wine Quality is by Cortez, Cerdeira, Almeida, Matos and +Reis (2009), UCI, DOI 10.24432/C56S3T, CC BY 4.0. CSV text fixture generation +normalizes physical line endings and uses the requested first N data rows. + +The composition copies input into a producer, gets a normalized value, copies it +into the consumer, records an expected ordinary exception after saving `answer`, +recovers it, replaces the consumer's bridge with null, verifies the producer's +copy and answer remain, and closes both sessions. Interactive recordings repeat +these actions and strictly replay both raw and losslessly packed history. +Native and independent checks distinguish explicit null from display stdout. diff --git a/tests/applications/concordance/consumer.js b/tests/applications/concordance/consumer.js new file mode 100644 index 0000000..a2cb183 --- /dev/null +++ b/tests/applications/concordance/consumer.js @@ -0,0 +1,10 @@ +var index = new Map(); +for (var row of bridge) { + for (var word of row.tokens) { + var value = index.get(word) || {count:0,docs:new Set()}; + value.count++; value.docs.add(row.id); index.set(word,value); + } +} +var answer = [...index].sort((a,b)=>Buffer.compare(Buffer.from(a[0]),Buffer.from(b[0]))) + .map(([word,v])=>({word,occurrences:v.count,documents:v.docs.size})); +answer; diff --git a/tests/applications/concordance/consumer.py b/tests/applications/concordance/consumer.py new file mode 100644 index 0000000..2c9b04f --- /dev/null +++ b/tests/applications/concordance/consumer.py @@ -0,0 +1,10 @@ +from collections import Counter, defaultdict +occurrences = Counter() +documents = defaultdict(set) +for row in bridge: + for word in row['tokens']: + occurrences[word] += 1 + documents[word].add(row['id']) +answer = [{'word':word,'occurrences':occurrences[word],'documents':len(documents[word])} + for word in sorted(occurrences,key=lambda s:s.encode('utf-8'))] +answer diff --git a/tests/applications/concordance/producer.js b/tests/applications/concordance/producer.js new file mode 100644 index 0000000..9300377 --- /dev/null +++ b/tests/applications/concordance/producer.js @@ -0,0 +1,3 @@ +var bridge = data.map(row=>({id:row.id,tokens:row.text.normalize('NFC').replace(/[A-Z]/g,c=>c.toLowerCase()).split(/[ \n\t\r\u00a0]+/).filter(t=>t.length)})); +bridge.sort((a,b)=>Buffer.compare(Buffer.from(a.id),Buffer.from(b.id))); +bridge; diff --git a/tests/applications/concordance/producer.py b/tests/applications/concordance/producer.py new file mode 100644 index 0000000..c342033 --- /dev/null +++ b/tests/applications/concordance/producer.py @@ -0,0 +1,6 @@ +import re, unicodedata +ascii_lower = str.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') +bridge = [{'id': row['id'], 'tokens': [t for t in re.split('[ \n\t\r\u00a0]+',unicodedata.normalize('NFC',row['text']).translate(ascii_lower)) if t]} + for row in data] +bridge.sort(key=lambda row:row['id'].encode('utf-8')) +bridge diff --git a/tests/applications/data/PROVENANCE.json b/tests/applications/data/PROVENANCE.json new file mode 100644 index 0000000..fa980fb --- /dev/null +++ b/tests/applications/data/PROVENANCE.json @@ -0,0 +1,21 @@ +{ + "source": "https://archive.ics.uci.edu/static/public/186/wine+quality.zip", + "landing_page": "https://archive.ics.uci.edu/dataset/186/wine%2Bquality", + "doi": "10.24432/C56S3T", + "authors": [ + "Paulo Cortez", + "A. Cerdeira", + "F. Almeida", + "T. Matos", + "J. Reis" + ], + "year": 2009, + "license": "CC BY 4.0", + "license_url": "https://creativecommons.org/licenses/by/4.0/", + "retrieved": "2026-11-03", + "files": { + "winequality.names": "15d215e73b39105952380fd487e4ce1bf5a6b83a425abb285c2ec55193d3ca92", + "winequality-red.csv": "4a402cf041b025d4566d954c3b9ba8635a3a8a01e039005d97d6a710278cf05e", + "wine-quality.zip": "3ed56667f4b828242bd732d7d1dd7f2861e54432239d7fa63877014cbb0304d4" + } +} diff --git a/tests/applications/data/wine-quality.zip b/tests/applications/data/wine-quality.zip new file mode 100644 index 0000000000000000000000000000000000000000..91805819fdff18daff641cec9305f9aee76e39ca GIT binary patch literal 91353 zcmV)9K*hgMO9KQH00;mG006VLRsaA10000000000022TJ0C#C_WpQ<3Y-x0PEplaK zE@N|c&Aok&FFA4~_? z0=n_)RWcdDAf5hUZuXD=`ltW#$A9^UfBNS?{mY;J_J953*FXNBfBEx2{q0YG{$u*9 zfBw_o{`#kX*1vfF?XQ3Q_dos1fBe&*|KZ>NP-%*#%j7RHVWqyrE2c3D!8Psi}@yV;4|1^e(%vn>X(j~?dzRn#R4~d z@%c{Cbwoe^`u^|z`|+#jkaE_lJT+dWou63m-@QND|1i!<`7{gf|9*=1(ayuZgWgwa z{t4ursp`voFjLoc8pW1C*KED7Gq7gT&N}Nn^_^#P&y4f?>-IsVyif5}ZOjzgGsV75 zdB-gKHs>+Qd5H5H%DV;LUs}Z8Xbo(dwfv3psPn#CM}57Yy~s*~bayun>eudEzNO#y zE>E-1w0$F-E%oSUTEBcR>CmF<-`>x8R$|1reYb2mKd|57*rU2q?`IPIx95GNajitt zM|qb=e2cd8Lgyv)*_!A#!Yt>}-lJH~Z#*wFd==^$B-{*ho;m!u*|s}B)tm0@bKcJH z)4yv42YbC+wtY?8-0#1%^HKd|Po5w-KU)jF>^V%&S91#z>s@q%W4%ScP0Vz*L$*3P z#-Hx&Z=L82IabHkO^=W^B z_kO7}N~ftjMcZHJqcxbid(OhbyuA-5U(I&*JhO-{K)($2I9uPW$on;PvxS}FQ!Rb@ zGknah-=0gh&(2wPxH}K`YZ2aEUAWF;=`{MC%JVLByKGoyHiW+;hj_)U3Hf$ebqQ_m0$=Rn}&90t^_nxvZbdKpeQMawoCs(7> z(#7g^eXVoqI;^l71;K!!r(yZkkoMdAQ|F!Z3!SYadKS);FI|T7Np;?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pIUT`s>?5X#Zw=F zhK3J+#^w_GKrDxum`BoZ=C@{q-EPa`)gqYYHB}yg<&oGB^kM@d3M171^ULX61Fq7O+mVTX~uynsBBpzF74>^ao0Hzj4n zo8t_R{bGzovD9C`mw)FxjxqAZqFb4Y$bvcVkS%l^Hl_Ha3PrwX4=o`RgKXz}8Q?X! zI3qN5DrIoOPdprCE@i#$<0e=95M0(gHMRS%q7GJOV`b}lw;p=e!P=&hHJ1oJOAFj= zb8jWV8%BOa#Jj~n3UEFTXg8o`@uxx@uvC&$R$;Mbu2nSk z^Jswsprf2!A&h{CLzmfa51tw#95J8wTjc;I#4HWU=@>|o2tzFe9$nU%(1c(f=(w@Q$1 zrJA+hE1K`slsUQI?sZH5S#ks-czwOsxcnwb8bZ+vcnaSs9~&kpTmDagT>9#;-5{6=l?74 z-vs*de+g{=ll^Z2FB@AA%m1Imf4}cPsQRDy&l@(4p#NLqf54@x0FUsWE3p4^=sz0^ I=zq)q0(6#3w*UYD literal 0 HcmV?d00001 diff --git a/tests/applications/data/winequality-red.csv b/tests/applications/data/winequality-red.csv new file mode 100644 index 0000000..9bb4e3c --- /dev/null +++ b/tests/applications/data/winequality-red.csv @@ -0,0 +1,1600 @@ +"fixed acidity";"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality" +7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5 +7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5 +7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5 +11.2;0.28;0.56;1.9;0.075;17;60;0.998;3.16;0.58;9.8;6 +7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5 +7.4;0.66;0;1.8;0.075;13;40;0.9978;3.51;0.56;9.4;5 +7.9;0.6;0.06;1.6;0.069;15;59;0.9964;3.3;0.46;9.4;5 +7.3;0.65;0;1.2;0.065;15;21;0.9946;3.39;0.47;10;7 +7.8;0.58;0.02;2;0.073;9;18;0.9968;3.36;0.57;9.5;7 +7.5;0.5;0.36;6.1;0.071;17;102;0.9978;3.35;0.8;10.5;5 +6.7;0.58;0.08;1.8;0.097;15;65;0.9959;3.28;0.54;9.2;5 +7.5;0.5;0.36;6.1;0.071;17;102;0.9978;3.35;0.8;10.5;5 +5.6;0.615;0;1.6;0.089;16;59;0.9943;3.58;0.52;9.9;5 +7.8;0.61;0.29;1.6;0.114;9;29;0.9974;3.26;1.56;9.1;5 +8.9;0.62;0.18;3.8;0.176;52;145;0.9986;3.16;0.88;9.2;5 +8.9;0.62;0.19;3.9;0.17;51;148;0.9986;3.17;0.93;9.2;5 +8.5;0.28;0.56;1.8;0.092;35;103;0.9969;3.3;0.75;10.5;7 +8.1;0.56;0.28;1.7;0.368;16;56;0.9968;3.11;1.28;9.3;5 +7.4;0.59;0.08;4.4;0.086;6;29;0.9974;3.38;0.5;9;4 +7.9;0.32;0.51;1.8;0.341;17;56;0.9969;3.04;1.08;9.2;6 +8.9;0.22;0.48;1.8;0.077;29;60;0.9968;3.39;0.53;9.4;6 +7.6;0.39;0.31;2.3;0.082;23;71;0.9982;3.52;0.65;9.7;5 +7.9;0.43;0.21;1.6;0.106;10;37;0.9966;3.17;0.91;9.5;5 +8.5;0.49;0.11;2.3;0.084;9;67;0.9968;3.17;0.53;9.4;5 +6.9;0.4;0.14;2.4;0.085;21;40;0.9968;3.43;0.63;9.7;6 +6.3;0.39;0.16;1.4;0.08;11;23;0.9955;3.34;0.56;9.3;5 +7.6;0.41;0.24;1.8;0.08;4;11;0.9962;3.28;0.59;9.5;5 +7.9;0.43;0.21;1.6;0.106;10;37;0.9966;3.17;0.91;9.5;5 +7.1;0.71;0;1.9;0.08;14;35;0.9972;3.47;0.55;9.4;5 +7.8;0.645;0;2;0.082;8;16;0.9964;3.38;0.59;9.8;6 +6.7;0.675;0.07;2.4;0.089;17;82;0.9958;3.35;0.54;10.1;5 +6.9;0.685;0;2.5;0.105;22;37;0.9966;3.46;0.57;10.6;6 +8.3;0.655;0.12;2.3;0.083;15;113;0.9966;3.17;0.66;9.8;5 +6.9;0.605;0.12;10.7;0.073;40;83;0.9993;3.45;0.52;9.4;6 +5.2;0.32;0.25;1.8;0.103;13;50;0.9957;3.38;0.55;9.2;5 +7.8;0.645;0;5.5;0.086;5;18;0.9986;3.4;0.55;9.6;6 +7.8;0.6;0.14;2.4;0.086;3;15;0.9975;3.42;0.6;10.8;6 +8.1;0.38;0.28;2.1;0.066;13;30;0.9968;3.23;0.73;9.7;7 +5.7;1.13;0.09;1.5;0.172;7;19;0.994;3.5;0.48;9.8;4 +7.3;0.45;0.36;5.9;0.074;12;87;0.9978;3.33;0.83;10.5;5 +7.3;0.45;0.36;5.9;0.074;12;87;0.9978;3.33;0.83;10.5;5 +8.8;0.61;0.3;2.8;0.088;17;46;0.9976;3.26;0.51;9.3;4 +7.5;0.49;0.2;2.6;0.332;8;14;0.9968;3.21;0.9;10.5;6 +8.1;0.66;0.22;2.2;0.069;9;23;0.9968;3.3;1.2;10.3;5 +6.8;0.67;0.02;1.8;0.05;5;11;0.9962;3.48;0.52;9.5;5 +4.6;0.52;0.15;2.1;0.054;8;65;0.9934;3.9;0.56;13.1;4 +7.7;0.935;0.43;2.2;0.114;22;114;0.997;3.25;0.73;9.2;5 +8.7;0.29;0.52;1.6;0.113;12;37;0.9969;3.25;0.58;9.5;5 +6.4;0.4;0.23;1.6;0.066;5;12;0.9958;3.34;0.56;9.2;5 +5.6;0.31;0.37;1.4;0.074;12;96;0.9954;3.32;0.58;9.2;5 +8.8;0.66;0.26;1.7;0.074;4;23;0.9971;3.15;0.74;9.2;5 +6.6;0.52;0.04;2.2;0.069;8;15;0.9956;3.4;0.63;9.4;6 +6.6;0.5;0.04;2.1;0.068;6;14;0.9955;3.39;0.64;9.4;6 +8.6;0.38;0.36;3;0.081;30;119;0.997;3.2;0.56;9.4;5 +7.6;0.51;0.15;2.8;0.11;33;73;0.9955;3.17;0.63;10.2;6 +7.7;0.62;0.04;3.8;0.084;25;45;0.9978;3.34;0.53;9.5;5 +10.2;0.42;0.57;3.4;0.07;4;10;0.9971;3.04;0.63;9.6;5 +7.5;0.63;0.12;5.1;0.111;50;110;0.9983;3.26;0.77;9.4;5 +7.8;0.59;0.18;2.3;0.076;17;54;0.9975;3.43;0.59;10;5 +7.3;0.39;0.31;2.4;0.074;9;46;0.9962;3.41;0.54;9.4;6 +8.8;0.4;0.4;2.2;0.079;19;52;0.998;3.44;0.64;9.2;5 +7.7;0.69;0.49;1.8;0.115;20;112;0.9968;3.21;0.71;9.3;5 +7.5;0.52;0.16;1.9;0.085;12;35;0.9968;3.38;0.62;9.5;7 +7;0.735;0.05;2;0.081;13;54;0.9966;3.39;0.57;9.8;5 +7.2;0.725;0.05;4.65;0.086;4;11;0.9962;3.41;0.39;10.9;5 +7.2;0.725;0.05;4.65;0.086;4;11;0.9962;3.41;0.39;10.9;5 +7.5;0.52;0.11;1.5;0.079;11;39;0.9968;3.42;0.58;9.6;5 +6.6;0.705;0.07;1.6;0.076;6;15;0.9962;3.44;0.58;10.7;5 +9.3;0.32;0.57;2;0.074;27;65;0.9969;3.28;0.79;10.7;5 +8;0.705;0.05;1.9;0.074;8;19;0.9962;3.34;0.95;10.5;6 +7.7;0.63;0.08;1.9;0.076;15;27;0.9967;3.32;0.54;9.5;6 +7.7;0.67;0.23;2.1;0.088;17;96;0.9962;3.32;0.48;9.5;5 +7.7;0.69;0.22;1.9;0.084;18;94;0.9961;3.31;0.48;9.5;5 +8.3;0.675;0.26;2.1;0.084;11;43;0.9976;3.31;0.53;9.2;4 +9.7;0.32;0.54;2.5;0.094;28;83;0.9984;3.28;0.82;9.6;5 +8.8;0.41;0.64;2.2;0.093;9;42;0.9986;3.54;0.66;10.5;5 +8.8;0.41;0.64;2.2;0.093;9;42;0.9986;3.54;0.66;10.5;5 +6.8;0.785;0;2.4;0.104;14;30;0.9966;3.52;0.55;10.7;6 +6.7;0.75;0.12;2;0.086;12;80;0.9958;3.38;0.52;10.1;5 +8.3;0.625;0.2;1.5;0.08;27;119;0.9972;3.16;1.12;9.1;4 +6.2;0.45;0.2;1.6;0.069;3;15;0.9958;3.41;0.56;9.2;5 +7.8;0.43;0.7;1.9;0.464;22;67;0.9974;3.13;1.28;9.4;5 +7.4;0.5;0.47;2;0.086;21;73;0.997;3.36;0.57;9.1;5 +7.3;0.67;0.26;1.8;0.401;16;51;0.9969;3.16;1.14;9.4;5 +6.3;0.3;0.48;1.8;0.069;18;61;0.9959;3.44;0.78;10.3;6 +6.9;0.55;0.15;2.2;0.076;19;40;0.9961;3.41;0.59;10.1;5 +8.6;0.49;0.28;1.9;0.11;20;136;0.9972;2.93;1.95;9.9;6 +7.7;0.49;0.26;1.9;0.062;9;31;0.9966;3.39;0.64;9.6;5 +9.3;0.39;0.44;2.1;0.107;34;125;0.9978;3.14;1.22;9.5;5 +7;0.62;0.08;1.8;0.076;8;24;0.9978;3.48;0.53;9;5 +7.9;0.52;0.26;1.9;0.079;42;140;0.9964;3.23;0.54;9.5;5 +8.6;0.49;0.28;1.9;0.11;20;136;0.9972;2.93;1.95;9.9;6 +8.6;0.49;0.29;2;0.11;19;133;0.9972;2.93;1.98;9.8;5 +7.7;0.49;0.26;1.9;0.062;9;31;0.9966;3.39;0.64;9.6;5 +5;1.02;0.04;1.4;0.045;41;85;0.9938;3.75;0.48;10.5;4 +4.7;0.6;0.17;2.3;0.058;17;106;0.9932;3.85;0.6;12.9;6 +6.8;0.775;0;3;0.102;8;23;0.9965;3.45;0.56;10.7;5 +7;0.5;0.25;2;0.07;3;22;0.9963;3.25;0.63;9.2;5 +7.6;0.9;0.06;2.5;0.079;5;10;0.9967;3.39;0.56;9.8;5 +8.1;0.545;0.18;1.9;0.08;13;35;0.9972;3.3;0.59;9;6 +8.3;0.61;0.3;2.1;0.084;11;50;0.9972;3.4;0.61;10.2;6 +7.8;0.5;0.3;1.9;0.075;8;22;0.9959;3.31;0.56;10.4;6 +8.1;0.545;0.18;1.9;0.08;13;35;0.9972;3.3;0.59;9;6 +8.1;0.575;0.22;2.1;0.077;12;65;0.9967;3.29;0.51;9.2;5 +7.2;0.49;0.24;2.2;0.07;5;36;0.996;3.33;0.48;9.4;5 +8.1;0.575;0.22;2.1;0.077;12;65;0.9967;3.29;0.51;9.2;5 +7.8;0.41;0.68;1.7;0.467;18;69;0.9973;3.08;1.31;9.3;5 +6.2;0.63;0.31;1.7;0.088;15;64;0.9969;3.46;0.79;9.3;5 +8;0.33;0.53;2.5;0.091;18;80;0.9976;3.37;0.8;9.6;6 +8.1;0.785;0.52;2;0.122;37;153;0.9969;3.21;0.69;9.3;5 +7.8;0.56;0.19;1.8;0.104;12;47;0.9964;3.19;0.93;9.5;5 +8.4;0.62;0.09;2.2;0.084;11;108;0.9964;3.15;0.66;9.8;5 +8.4;0.6;0.1;2.2;0.085;14;111;0.9964;3.15;0.66;9.8;5 +10.1;0.31;0.44;2.3;0.08;22;46;0.9988;3.32;0.67;9.7;6 +7.8;0.56;0.19;1.8;0.104;12;47;0.9964;3.19;0.93;9.5;5 +9.4;0.4;0.31;2.2;0.09;13;62;0.9966;3.07;0.63;10.5;6 +8.3;0.54;0.28;1.9;0.077;11;40;0.9978;3.39;0.61;10;6 +7.8;0.56;0.12;2;0.082;7;28;0.997;3.37;0.5;9.4;6 +8.8;0.55;0.04;2.2;0.119;14;56;0.9962;3.21;0.6;10.9;6 +7;0.69;0.08;1.8;0.097;22;89;0.9959;3.34;0.54;9.2;6 +7.3;1.07;0.09;1.7;0.178;10;89;0.9962;3.3;0.57;9;5 +8.8;0.55;0.04;2.2;0.119;14;56;0.9962;3.21;0.6;10.9;6 +7.3;0.695;0;2.5;0.075;3;13;0.998;3.49;0.52;9.2;5 +8;0.71;0;2.6;0.08;11;34;0.9976;3.44;0.53;9.5;5 +7.8;0.5;0.17;1.6;0.082;21;102;0.996;3.39;0.48;9.5;5 +9;0.62;0.04;1.9;0.146;27;90;0.9984;3.16;0.7;9.4;5 +8.2;1.33;0;1.7;0.081;3;12;0.9964;3.53;0.49;10.9;5 +8.1;1.33;0;1.8;0.082;3;12;0.9964;3.54;0.48;10.9;5 +8;0.59;0.16;1.8;0.065;3;16;0.9962;3.42;0.92;10.5;7 +6.1;0.38;0.15;1.8;0.072;6;19;0.9955;3.42;0.57;9.4;5 +8;0.745;0.56;2;0.118;30;134;0.9968;3.24;0.66;9.4;5 +5.6;0.5;0.09;2.3;0.049;17;99;0.9937;3.63;0.63;13;5 +5.6;0.5;0.09;2.3;0.049;17;99;0.9937;3.63;0.63;13;5 +6.6;0.5;0.01;1.5;0.06;17;26;0.9952;3.4;0.58;9.8;6 +7.9;1.04;0.05;2.2;0.084;13;29;0.9959;3.22;0.55;9.9;6 +8.4;0.745;0.11;1.9;0.09;16;63;0.9965;3.19;0.82;9.6;5 +8.3;0.715;0.15;1.8;0.089;10;52;0.9968;3.23;0.77;9.5;5 +7.2;0.415;0.36;2;0.081;13;45;0.9972;3.48;0.64;9.2;5 +7.8;0.56;0.19;2.1;0.081;15;105;0.9962;3.33;0.54;9.5;5 +7.8;0.56;0.19;2;0.081;17;108;0.9962;3.32;0.54;9.5;5 +8.4;0.745;0.11;1.9;0.09;16;63;0.9965;3.19;0.82;9.6;5 +8.3;0.715;0.15;1.8;0.089;10;52;0.9968;3.23;0.77;9.5;5 +5.2;0.34;0;1.8;0.05;27;63;0.9916;3.68;0.79;14;6 +6.3;0.39;0.08;1.7;0.066;3;20;0.9954;3.34;0.58;9.4;5 +5.2;0.34;0;1.8;0.05;27;63;0.9916;3.68;0.79;14;6 +8.1;0.67;0.55;1.8;0.117;32;141;0.9968;3.17;0.62;9.4;5 +5.8;0.68;0.02;1.8;0.087;21;94;0.9944;3.54;0.52;10;5 +7.6;0.49;0.26;1.6;0.236;10;88;0.9968;3.11;0.8;9.3;5 +6.9;0.49;0.1;2.3;0.074;12;30;0.9959;3.42;0.58;10.2;6 +8.2;0.4;0.44;2.8;0.089;11;43;0.9975;3.53;0.61;10.5;6 +7.3;0.33;0.47;2.1;0.077;5;11;0.9958;3.33;0.53;10.3;6 +9.2;0.52;1;3.4;0.61;32;69;0.9996;2.74;2.0;9.4;4 +7.5;0.6;0.03;1.8;0.095;25;99;0.995;3.35;0.54;10.1;5 +7.5;0.6;0.03;1.8;0.095;25;99;0.995;3.35;0.54;10.1;5 +7.1;0.43;0.42;5.5;0.07;29;129;0.9973;3.42;0.72;10.5;5 +7.1;0.43;0.42;5.5;0.071;28;128;0.9973;3.42;0.71;10.5;5 +7.1;0.43;0.42;5.5;0.07;29;129;0.9973;3.42;0.72;10.5;5 +7.1;0.43;0.42;5.5;0.071;28;128;0.9973;3.42;0.71;10.5;5 +7.1;0.68;0;2.2;0.073;12;22;0.9969;3.48;0.5;9.3;5 +6.8;0.6;0.18;1.9;0.079;18;86;0.9968;3.59;0.57;9.3;6 +7.6;0.95;0.03;2;0.09;7;20;0.9959;3.2;0.56;9.6;5 +7.6;0.68;0.02;1.3;0.072;9;20;0.9965;3.17;1.08;9.2;4 +7.8;0.53;0.04;1.7;0.076;17;31;0.9964;3.33;0.56;10;6 +7.4;0.6;0.26;7.3;0.07;36;121;0.9982;3.37;0.49;9.4;5 +7.3;0.59;0.26;7.2;0.07;35;121;0.9981;3.37;0.49;9.4;5 +7.8;0.63;0.48;1.7;0.1;14;96;0.9961;3.19;0.62;9.5;5 +6.8;0.64;0.1;2.1;0.085;18;101;0.9956;3.34;0.52;10.2;5 +7.3;0.55;0.03;1.6;0.072;17;42;0.9956;3.37;0.48;9;4 +6.8;0.63;0.07;2.1;0.089;11;44;0.9953;3.47;0.55;10.4;6 +7.5;0.705;0.24;1.8;0.36;15;63;0.9964;3;1.59;9.5;5 +7.9;0.885;0.03;1.8;0.058;4;8;0.9972;3.36;0.33;9.1;4 +8;0.42;0.17;2;0.073;6;18;0.9972;3.29;0.61;9.2;6 +8;0.42;0.17;2;0.073;6;18;0.9972;3.29;0.61;9.2;6 +7.4;0.62;0.05;1.9;0.068;24;42;0.9961;3.42;0.57;11.5;6 +7.3;0.38;0.21;2;0.08;7;35;0.9961;3.33;0.47;9.5;5 +6.9;0.5;0.04;1.5;0.085;19;49;0.9958;3.35;0.78;9.5;5 +7.3;0.38;0.21;2;0.08;7;35;0.9961;3.33;0.47;9.5;5 +7.5;0.52;0.42;2.3;0.087;8;38;0.9972;3.58;0.61;10.5;6 +7;0.805;0;2.5;0.068;7;20;0.9969;3.48;0.56;9.6;5 +8.8;0.61;0.14;2.4;0.067;10;42;0.9969;3.19;0.59;9.5;5 +8.8;0.61;0.14;2.4;0.067;10;42;0.9969;3.19;0.59;9.5;5 +8.9;0.61;0.49;2;0.27;23;110;0.9972;3.12;1.02;9.3;5 +7.2;0.73;0.02;2.5;0.076;16;42;0.9972;3.44;0.52;9.3;5 +6.8;0.61;0.2;1.8;0.077;11;65;0.9971;3.54;0.58;9.3;5 +6.7;0.62;0.21;1.9;0.079;8;62;0.997;3.52;0.58;9.3;6 +8.9;0.31;0.57;2;0.111;26;85;0.9971;3.26;0.53;9.7;5 +7.4;0.39;0.48;2;0.082;14;67;0.9972;3.34;0.55;9.2;5 +7.7;0.705;0.1;2.6;0.084;9;26;0.9976;3.39;0.49;9.7;5 +7.9;0.5;0.33;2;0.084;15;143;0.9968;3.2;0.55;9.5;5 +7.9;0.49;0.32;1.9;0.082;17;144;0.9968;3.2;0.55;9.5;5 +8.2;0.5;0.35;2.9;0.077;21;127;0.9976;3.23;0.62;9.4;5 +6.4;0.37;0.25;1.9;0.074;21;49;0.9974;3.57;0.62;9.8;6 +6.8;0.63;0.12;3.8;0.099;16;126;0.9969;3.28;0.61;9.5;5 +7.6;0.55;0.21;2.2;0.071;7;28;0.9964;3.28;0.55;9.7;5 +7.6;0.55;0.21;2.2;0.071;7;28;0.9964;3.28;0.55;9.7;5 +7.8;0.59;0.33;2;0.074;24;120;0.9968;3.25;0.54;9.4;5 +7.3;0.58;0.3;2.4;0.074;15;55;0.9968;3.46;0.59;10.2;5 +11.5;0.3;0.6;2;0.067;12;27;0.9981;3.11;0.97;10.1;6 +5.4;0.835;0.08;1.2;0.046;13;93;0.9924;3.57;0.85;13;7 +6.9;1.09;0.06;2.1;0.061;12;31;0.9948;3.51;0.43;11.4;4 +9.6;0.32;0.47;1.4;0.056;9;24;0.99695;3.22;0.82;10.3;7 +8.8;0.37;0.48;2.1;0.097;39;145;0.9975;3.04;1.03;9.3;5 +6.8;0.5;0.11;1.5;0.075;16;49;0.99545;3.36;0.79;9.5;5 +7;0.42;0.35;1.6;0.088;16;39;0.9961;3.34;0.55;9.2;5 +7;0.43;0.36;1.6;0.089;14;37;0.99615;3.34;0.56;9.2;6 +12.8;0.3;0.74;2.6;0.095;9;28;0.9994;3.2;0.77;10.8;7 +12.8;0.3;0.74;2.6;0.095;9;28;0.9994;3.2;0.77;10.8;7 +7.8;0.57;0.31;1.8;0.069;26;120;0.99625;3.29;0.53;9.3;5 +7.8;0.44;0.28;2.7;0.1;18;95;0.9966;3.22;0.67;9.4;5 +11;0.3;0.58;2.1;0.054;7;19;0.998;3.31;0.88;10.5;7 +9.7;0.53;0.6;2;0.039;5;19;0.99585;3.3;0.86;12.4;6 +8;0.725;0.24;2.8;0.083;10;62;0.99685;3.35;0.56;10;6 +11.6;0.44;0.64;2.1;0.059;5;15;0.998;3.21;0.67;10.2;6 +8.2;0.57;0.26;2.2;0.06;28;65;0.9959;3.3;0.43;10.1;5 +7.8;0.735;0.08;2.4;0.092;10;41;0.9974;3.24;0.71;9.8;6 +7;0.49;0.49;5.6;0.06;26;121;0.9974;3.34;0.76;10.5;5 +8.7;0.625;0.16;2;0.101;13;49;0.9962;3.14;0.57;11;5 +8.1;0.725;0.22;2.2;0.072;11;41;0.9967;3.36;0.55;9.1;5 +7.5;0.49;0.19;1.9;0.076;10;44;0.9957;3.39;0.54;9.7;5 +7.8;0.53;0.33;2.4;0.08;24;144;0.99655;3.3;0.6;9.5;5 +7.8;0.34;0.37;2;0.082;24;58;0.9964;3.34;0.59;9.4;6 +7.4;0.53;0.26;2;0.101;16;72;0.9957;3.15;0.57;9.4;5 +6.8;0.61;0.04;1.5;0.057;5;10;0.99525;3.42;0.6;9.5;5 +8.6;0.645;0.25;2;0.083;8;28;0.99815;3.28;0.6;10;6 +8.4;0.635;0.36;2;0.089;15;55;0.99745;3.31;0.57;10.4;4 +7.7;0.43;0.25;2.6;0.073;29;63;0.99615;3.37;0.58;10.5;6 +8.9;0.59;0.5;2;0.337;27;81;0.9964;3.04;1.61;9.5;6 +9;0.82;0.14;2.6;0.089;9;23;0.9984;3.39;0.63;9.8;5 +7.7;0.43;0.25;2.6;0.073;29;63;0.99615;3.37;0.58;10.5;6 +6.9;0.52;0.25;2.6;0.081;10;37;0.99685;3.46;0.5;11;5 +5.2;0.48;0.04;1.6;0.054;19;106;0.9927;3.54;0.62;12.2;7 +8;0.38;0.06;1.8;0.078;12;49;0.99625;3.37;0.52;9.9;6 +8.5;0.37;0.2;2.8;0.09;18;58;0.998;3.34;0.7;9.6;6 +6.9;0.52;0.25;2.6;0.081;10;37;0.99685;3.46;0.5;11;5 +8.2;1;0.09;2.3;0.065;7;37;0.99685;3.32;0.55;9;6 +7.2;0.63;0;1.9;0.097;14;38;0.99675;3.37;0.58;9;6 +7.2;0.63;0;1.9;0.097;14;38;0.99675;3.37;0.58;9;6 +7.2;0.645;0;1.9;0.097;15;39;0.99675;3.37;0.58;9.2;6 +7.2;0.63;0;1.9;0.097;14;38;0.99675;3.37;0.58;9;6 +8.2;1;0.09;2.3;0.065;7;37;0.99685;3.32;0.55;9;6 +8.9;0.635;0.37;1.7;0.263;5;62;0.9971;3;1.09;9.3;5 +12;0.38;0.56;2.1;0.093;6;24;0.99925;3.14;0.71;10.9;6 +7.7;0.58;0.1;1.8;0.102;28;109;0.99565;3.08;0.49;9.8;6 +15;0.21;0.44;2.2;0.075;10;24;1.00005;3.07;0.84;9.2;7 +15;0.21;0.44;2.2;0.075;10;24;1.00005;3.07;0.84;9.2;7 +7.3;0.66;0;2;0.084;6;23;0.9983;3.61;0.96;9.9;6 +7.1;0.68;0.07;1.9;0.075;16;51;0.99685;3.38;0.52;9.5;5 +8.2;0.6;0.17;2.3;0.072;11;73;0.9963;3.2;0.45;9.3;5 +7.7;0.53;0.06;1.7;0.074;9;39;0.99615;3.35;0.48;9.8;6 +7.3;0.66;0;2;0.084;6;23;0.9983;3.61;0.96;9.9;6 +10.8;0.32;0.44;1.6;0.063;16;37;0.9985;3.22;0.78;10;6 +7.1;0.6;0;1.8;0.074;16;34;0.9972;3.47;0.7;9.9;6 +11.1;0.35;0.48;3.1;0.09;5;21;0.9986;3.17;0.53;10.5;5 +7.7;0.775;0.42;1.9;0.092;8;86;0.9959;3.23;0.59;9.5;5 +7.1;0.6;0;1.8;0.074;16;34;0.9972;3.47;0.7;9.9;6 +8;0.57;0.23;3.2;0.073;17;119;0.99675;3.26;0.57;9.3;5 +9.4;0.34;0.37;2.2;0.075;5;13;0.998;3.22;0.62;9.2;5 +6.6;0.695;0;2.1;0.075;12;56;0.9968;3.49;0.67;9.2;5 +7.7;0.41;0.76;1.8;0.611;8;45;0.9968;3.06;1.26;9.4;5 +10;0.31;0.47;2.6;0.085;14;33;0.99965;3.36;0.8;10.5;7 +7.9;0.33;0.23;1.7;0.077;18;45;0.99625;3.29;0.65;9.3;5 +7;0.975;0.04;2;0.087;12;67;0.99565;3.35;0.6;9.4;4 +8;0.52;0.03;1.7;0.07;10;35;0.99575;3.34;0.57;10;5 +7.9;0.37;0.23;1.8;0.077;23;49;0.9963;3.28;0.67;9.3;5 +12.5;0.56;0.49;2.4;0.064;5;27;0.9999;3.08;0.87;10.9;5 +11.8;0.26;0.52;1.8;0.071;6;10;0.9968;3.2;0.72;10.2;7 +8.1;0.87;0;3.3;0.096;26;61;1.00025;3.6;0.72;9.8;4 +7.9;0.35;0.46;3.6;0.078;15;37;0.9973;3.35;0.86;12.8;8 +6.9;0.54;0.04;3;0.077;7;27;0.9987;3.69;0.91;9.4;6 +11.5;0.18;0.51;4;0.104;4;23;0.9996;3.28;0.97;10.1;6 +7.9;0.545;0.06;4;0.087;27;61;0.9965;3.36;0.67;10.7;6 +11.5;0.18;0.51;4;0.104;4;23;0.9996;3.28;0.97;10.1;6 +10.9;0.37;0.58;4;0.071;17;65;0.99935;3.22;0.78;10.1;5 +8.4;0.715;0.2;2.4;0.076;10;38;0.99735;3.31;0.64;9.4;5 +7.5;0.65;0.18;7;0.088;27;94;0.99915;3.38;0.77;9.4;5 +7.9;0.545;0.06;4;0.087;27;61;0.9965;3.36;0.67;10.7;6 +6.9;0.54;0.04;3;0.077;7;27;0.9987;3.69;0.91;9.4;6 +11.5;0.18;0.51;4;0.104;4;23;0.9996;3.28;0.97;10.1;6 +10.3;0.32;0.45;6.4;0.073;5;13;0.9976;3.23;0.82;12.6;8 +8.9;0.4;0.32;5.6;0.087;10;47;0.9991;3.38;0.77;10.5;7 +11.4;0.26;0.44;3.6;0.071;6;19;0.9986;3.12;0.82;9.3;6 +7.7;0.27;0.68;3.5;0.358;5;10;0.9972;3.25;1.08;9.9;7 +7.6;0.52;0.12;3;0.067;12;53;0.9971;3.36;0.57;9.1;5 +8.9;0.4;0.32;5.6;0.087;10;47;0.9991;3.38;0.77;10.5;7 +9.9;0.59;0.07;3.4;0.102;32;71;1.00015;3.31;0.71;9.8;5 +9.9;0.59;0.07;3.4;0.102;32;71;1.00015;3.31;0.71;9.8;5 +12;0.45;0.55;2;0.073;25;49;0.9997;3.1;0.76;10.3;6 +7.5;0.4;0.12;3;0.092;29;53;0.9967;3.37;0.7;10.3;6 +8.7;0.52;0.09;2.5;0.091;20;49;0.9976;3.34;0.86;10.6;7 +11.6;0.42;0.53;3.3;0.105;33;98;1.001;3.2;0.95;9.2;5 +8.7;0.52;0.09;2.5;0.091;20;49;0.9976;3.34;0.86;10.6;7 +11;0.2;0.48;2;0.343;6;18;0.9979;3.3;0.71;10.5;5 +10.4;0.55;0.23;2.7;0.091;18;48;0.9994;3.22;0.64;10.3;6 +6.9;0.36;0.25;2.4;0.098;5;16;0.9964;3.41;0.6;10.1;6 +13.3;0.34;0.52;3.2;0.094;17;53;1.0014;3.05;0.81;9.5;6 +10.8;0.5;0.46;2.5;0.073;5;27;1.0001;3.05;0.64;9.5;5 +10.6;0.83;0.37;2.6;0.086;26;70;0.9981;3.16;0.52;9.9;5 +7.1;0.63;0.06;2;0.083;8;29;0.99855;3.67;0.73;9.6;5 +7.2;0.65;0.02;2.3;0.094;5;31;0.9993;3.67;0.8;9.7;5 +6.9;0.67;0.06;2.1;0.08;8;33;0.99845;3.68;0.71;9.6;5 +7.5;0.53;0.06;2.6;0.086;20;44;0.9965;3.38;0.59;10.7;6 +11.1;0.18;0.48;1.5;0.068;7;15;0.9973;3.22;0.64;10.1;6 +8.3;0.705;0.12;2.6;0.092;12;28;0.9994;3.51;0.72;10;5 +7.4;0.67;0.12;1.6;0.186;5;21;0.996;3.39;0.54;9.5;5 +8.4;0.65;0.6;2.1;0.112;12;90;0.9973;3.2;0.52;9.2;5 +10.3;0.53;0.48;2.5;0.063;6;25;0.9998;3.12;0.59;9.3;6 +7.6;0.62;0.32;2.2;0.082;7;54;0.9966;3.36;0.52;9.4;5 +10.3;0.41;0.42;2.4;0.213;6;14;0.9994;3.19;0.62;9.5;6 +10.3;0.43;0.44;2.4;0.214;5;12;0.9994;3.19;0.63;9.5;6 +7.4;0.29;0.38;1.7;0.062;9;30;0.9968;3.41;0.53;9.5;6 +10.3;0.53;0.48;2.5;0.063;6;25;0.9998;3.12;0.59;9.3;6 +7.9;0.53;0.24;2;0.072;15;105;0.996;3.27;0.54;9.4;6 +9;0.46;0.31;2.8;0.093;19;98;0.99815;3.32;0.63;9.5;6 +8.6;0.47;0.3;3;0.076;30;135;0.9976;3.3;0.53;9.4;5 +7.4;0.36;0.29;2.6;0.087;26;72;0.99645;3.39;0.68;11;5 +7.1;0.35;0.29;2.5;0.096;20;53;0.9962;3.42;0.65;11;6 +9.6;0.56;0.23;3.4;0.102;37;92;0.9996;3.3;0.65;10.1;5 +9.6;0.77;0.12;2.9;0.082;30;74;0.99865;3.3;0.64;10.4;6 +9.8;0.66;0.39;3.2;0.083;21;59;0.9989;3.37;0.71;11.5;7 +9.6;0.77;0.12;2.9;0.082;30;74;0.99865;3.3;0.64;10.4;6 +9.8;0.66;0.39;3.2;0.083;21;59;0.9989;3.37;0.71;11.5;7 +9.3;0.61;0.26;3.4;0.09;25;87;0.99975;3.24;0.62;9.7;5 +7.8;0.62;0.05;2.3;0.079;6;18;0.99735;3.29;0.63;9.3;5 +10.3;0.59;0.42;2.8;0.09;35;73;0.999;3.28;0.7;9.5;6 +10;0.49;0.2;11;0.071;13;50;1.0015;3.16;0.69;9.2;6 +10;0.49;0.2;11;0.071;13;50;1.0015;3.16;0.69;9.2;6 +11.6;0.53;0.66;3.65;0.121;6;14;0.9978;3.05;0.74;11.5;7 +10.3;0.44;0.5;4.5;0.107;5;13;0.998;3.28;0.83;11.5;5 +13.4;0.27;0.62;2.6;0.082;6;21;1.0002;3.16;0.67;9.7;6 +10.7;0.46;0.39;2;0.061;7;15;0.9981;3.18;0.62;9.5;5 +10.2;0.36;0.64;2.9;0.122;10;41;0.998;3.23;0.66;12.5;6 +10.2;0.36;0.64;2.9;0.122;10;41;0.998;3.23;0.66;12.5;6 +8;0.58;0.28;3.2;0.066;21;114;0.9973;3.22;0.54;9.4;6 +8.4;0.56;0.08;2.1;0.105;16;44;0.9958;3.13;0.52;11;5 +7.9;0.65;0.01;2.5;0.078;17;38;0.9963;3.34;0.74;11.7;7 +11.9;0.695;0.53;3.4;0.128;7;21;0.9992;3.17;0.84;12.2;7 +8.9;0.43;0.45;1.9;0.052;6;16;0.9948;3.35;0.7;12.5;6 +7.8;0.43;0.32;2.8;0.08;29;58;0.9974;3.31;0.64;10.3;5 +12.4;0.49;0.58;3;0.103;28;99;1.0008;3.16;1;11.5;6 +12.5;0.28;0.54;2.3;0.082;12;29;0.9997;3.11;1.36;9.8;7 +12.2;0.34;0.5;2.4;0.066;10;21;1;3.12;1.18;9.2;6 +10.6;0.42;0.48;2.7;0.065;5;18;0.9972;3.21;0.87;11.3;6 +10.9;0.39;0.47;1.8;0.118;6;14;0.9982;3.3;0.75;9.8;6 +10.9;0.39;0.47;1.8;0.118;6;14;0.9982;3.3;0.75;9.8;6 +11.9;0.57;0.5;2.6;0.082;6;32;1.0006;3.12;0.78;10.7;6 +7;0.685;0;1.9;0.067;40;63;0.9979;3.6;0.81;9.9;5 +6.6;0.815;0.02;2.7;0.072;17;34;0.9955;3.58;0.89;12.3;7 +13.8;0.49;0.67;3;0.093;6;15;0.9986;3.02;0.93;12;6 +9.6;0.56;0.31;2.8;0.089;15;46;0.9979;3.11;0.92;10;6 +9.1;0.785;0;2.6;0.093;11;28;0.9994;3.36;0.86;9.4;6 +10.7;0.67;0.22;2.7;0.107;17;34;1.0004;3.28;0.98;9.9;6 +9.1;0.795;0;2.6;0.096;11;26;0.9994;3.35;0.83;9.4;6 +7.7;0.665;0;2.4;0.09;8;19;0.9974;3.27;0.73;9.3;5 +13.5;0.53;0.79;4.8;0.12;23;77;1.0018;3.18;0.77;13;5 +6.1;0.21;0.4;1.4;0.066;40.5;165;0.9912;3.25;0.59;11.9;6 +6.7;0.75;0.01;2.4;0.078;17;32;0.9955;3.55;0.61;12.8;6 +11.5;0.41;0.52;3;0.08;29;55;1.0001;3.26;0.88;11;5 +10.5;0.42;0.66;2.95;0.116;12;29;0.997;3.24;0.75;11.7;7 +11.9;0.43;0.66;3.1;0.109;10;23;1;3.15;0.85;10.4;7 +12.6;0.38;0.66;2.6;0.088;10;41;1.001;3.17;0.68;9.8;6 +8.2;0.7;0.23;2;0.099;14;81;0.9973;3.19;0.7;9.4;5 +8.6;0.45;0.31;2.6;0.086;21;50;0.9982;3.37;0.91;9.9;6 +11.9;0.58;0.66;2.5;0.072;6;37;0.9992;3.05;0.56;10;5 +12.5;0.46;0.63;2;0.071;6;15;0.9988;2.99;0.87;10.2;5 +12.8;0.615;0.66;5.8;0.083;7;42;1.0022;3.07;0.73;10;7 +10;0.42;0.5;3.4;0.107;7;21;0.9979;3.26;0.93;11.8;6 +12.8;0.615;0.66;5.8;0.083;7;42;1.0022;3.07;0.73;10;7 +10.4;0.575;0.61;2.6;0.076;11;24;1;3.16;0.69;9;5 +10.3;0.34;0.52;2.8;0.159;15;75;0.9998;3.18;0.64;9.4;5 +9.4;0.27;0.53;2.4;0.074;6;18;0.9962;3.2;1.13;12;7 +6.9;0.765;0.02;2.3;0.063;35;63;0.9975;3.57;0.78;9.9;5 +7.9;0.24;0.4;1.6;0.056;11;25;0.9967;3.32;0.87;8.7;6 +9.1;0.28;0.48;1.8;0.067;26;46;0.9967;3.32;1.04;10.6;6 +7.4;0.55;0.22;2.2;0.106;12;72;0.9959;3.05;0.63;9.2;5 +14;0.41;0.63;3.8;0.089;6;47;1.0014;3.01;0.81;10.8;6 +11.5;0.54;0.71;4.4;0.124;6;15;0.9984;3.01;0.83;11.8;7 +11.5;0.45;0.5;3;0.078;19;47;1.0003;3.26;1.11;11;6 +9.4;0.27;0.53;2.4;0.074;6;18;0.9962;3.2;1.13;12;7 +11.4;0.625;0.66;6.2;0.088;6;24;0.9988;3.11;0.99;13.3;6 +8.3;0.42;0.38;2.5;0.094;24;60;0.9979;3.31;0.7;10.8;6 +8.3;0.26;0.42;2;0.08;11;27;0.9974;3.21;0.8;9.4;6 +13.7;0.415;0.68;2.9;0.085;17;43;1.0014;3.06;0.8;10;6 +8.3;0.26;0.42;2;0.08;11;27;0.9974;3.21;0.8;9.4;6 +8.3;0.26;0.42;2;0.08;11;27;0.9974;3.21;0.8;9.4;6 +7.7;0.51;0.28;2.1;0.087;23;54;0.998;3.42;0.74;9.2;5 +7.4;0.63;0.07;2.4;0.09;11;37;0.9979;3.43;0.76;9.7;6 +7.8;0.54;0.26;2;0.088;23;48;0.9981;3.41;0.74;9.2;6 +8.3;0.66;0.15;1.9;0.079;17;42;0.9972;3.31;0.54;9.6;6 +7.8;0.46;0.26;1.9;0.088;23;53;0.9981;3.43;0.74;9.2;6 +9.6;0.38;0.31;2.5;0.096;16;49;0.9982;3.19;0.7;10;7 +5.6;0.85;0.05;1.4;0.045;12;88;0.9924;3.56;0.82;12.9;8 +13.7;0.415;0.68;2.9;0.085;17;43;1.0014;3.06;0.8;10;6 +9.5;0.37;0.52;2;0.082;6;26;0.998;3.18;0.51;9.5;5 +8.4;0.665;0.61;2;0.112;13;95;0.997;3.16;0.54;9.1;5 +12.7;0.6;0.65;2.3;0.063;6;25;0.9997;3.03;0.57;9.9;5 +12;0.37;0.76;4.2;0.066;7;38;1.0004;3.22;0.6;13;7 +6.6;0.735;0.02;7.9;0.122;68;124;0.9994;3.47;0.53;9.9;5 +11.5;0.59;0.59;2.6;0.087;13;49;0.9988;3.18;0.65;11;6 +11.5;0.59;0.59;2.6;0.087;13;49;0.9988;3.18;0.65;11;6 +8.7;0.765;0.22;2.3;0.064;9;42;0.9963;3.1;0.55;9.4;5 +6.6;0.735;0.02;7.9;0.122;68;124;0.9994;3.47;0.53;9.9;5 +7.7;0.26;0.3;1.7;0.059;20;38;0.9949;3.29;0.47;10.8;6 +12.2;0.48;0.54;2.6;0.085;19;64;1;3.1;0.61;10.5;6 +11.4;0.6;0.49;2.7;0.085;10;41;0.9994;3.15;0.63;10.5;6 +7.7;0.69;0.05;2.7;0.075;15;27;0.9974;3.26;0.61;9.1;5 +8.7;0.31;0.46;1.4;0.059;11;25;0.9966;3.36;0.76;10.1;6 +9.8;0.44;0.47;2.5;0.063;9;28;0.9981;3.24;0.65;10.8;6 +12;0.39;0.66;3;0.093;12;30;0.9996;3.18;0.63;10.8;7 +10.4;0.34;0.58;3.7;0.174;6;16;0.997;3.19;0.7;11.3;6 +12.5;0.46;0.49;4.5;0.07;26;49;0.9981;3.05;0.57;9.6;4 +9;0.43;0.34;2.5;0.08;26;86;0.9987;3.38;0.62;9.5;6 +9.1;0.45;0.35;2.4;0.08;23;78;0.9987;3.38;0.62;9.5;5 +7.1;0.735;0.16;1.9;0.1;15;77;0.9966;3.27;0.64;9.3;5 +9.9;0.4;0.53;6.7;0.097;6;19;0.9986;3.27;0.82;11.7;7 +8.8;0.52;0.34;2.7;0.087;24;122;0.9982;3.26;0.61;9.5;5 +8.6;0.725;0.24;6.6;0.117;31;134;1.0014;3.32;1.07;9.3;5 +10.6;0.48;0.64;2.2;0.111;6;20;0.997;3.26;0.66;11.7;6 +7;0.58;0.12;1.9;0.091;34;124;0.9956;3.44;0.48;10.5;5 +11.9;0.38;0.51;2;0.121;7;20;0.9996;3.24;0.76;10.4;6 +6.8;0.77;0;1.8;0.066;34;52;0.9976;3.62;0.68;9.9;5 +9.5;0.56;0.33;2.4;0.089;35;67;0.9972;3.28;0.73;11.8;7 +6.6;0.84;0.03;2.3;0.059;32;48;0.9952;3.52;0.56;12.3;7 +7.7;0.96;0.2;2;0.047;15;60;0.9955;3.36;0.44;10.9;5 +10.5;0.24;0.47;2.1;0.066;6;24;0.9978;3.15;0.9;11;7 +7.7;0.96;0.2;2;0.047;15;60;0.9955;3.36;0.44;10.9;5 +6.6;0.84;0.03;2.3;0.059;32;48;0.9952;3.52;0.56;12.3;7 +6.4;0.67;0.08;2.1;0.045;19;48;0.9949;3.49;0.49;11.4;6 +9.5;0.78;0.22;1.9;0.077;6;32;0.9988;3.26;0.56;10.6;6 +9.1;0.52;0.33;1.3;0.07;9;30;0.9978;3.24;0.6;9.3;5 +12.8;0.84;0.63;2.4;0.088;13;35;0.9997;3.1;0.6;10.4;6 +10.5;0.24;0.47;2.1;0.066;6;24;0.9978;3.15;0.9;11;7 +7.8;0.55;0.35;2.2;0.074;21;66;0.9974;3.25;0.56;9.2;5 +11.9;0.37;0.69;2.3;0.078;12;24;0.9958;3;0.65;12.8;6 +12.3;0.39;0.63;2.3;0.091;6;18;1.0004;3.16;0.49;9.5;5 +10.4;0.41;0.55;3.2;0.076;22;54;0.9996;3.15;0.89;9.9;6 +12.3;0.39;0.63;2.3;0.091;6;18;1.0004;3.16;0.49;9.5;5 +8;0.67;0.3;2;0.06;38;62;0.9958;3.26;0.56;10.2;6 +11.1;0.45;0.73;3.2;0.066;6;22;0.9986;3.17;0.66;11.2;6 +10.4;0.41;0.55;3.2;0.076;22;54;0.9996;3.15;0.89;9.9;6 +7;0.62;0.18;1.5;0.062;7;50;0.9951;3.08;0.6;9.3;5 +12.6;0.31;0.72;2.2;0.072;6;29;0.9987;2.88;0.82;9.8;8 +11.9;0.4;0.65;2.15;0.068;7;27;0.9988;3.06;0.68;11.3;6 +15.6;0.685;0.76;3.7;0.1;6;43;1.0032;2.95;0.68;11.2;7 +10;0.44;0.49;2.7;0.077;11;19;0.9963;3.23;0.63;11.6;7 +5.3;0.57;0.01;1.7;0.054;5;27;0.9934;3.57;0.84;12.5;7 +9.5;0.735;0.1;2.1;0.079;6;31;0.9986;3.23;0.56;10.1;6 +12.5;0.38;0.6;2.6;0.081;31;72;0.9996;3.1;0.73;10.5;5 +9.3;0.48;0.29;2.1;0.127;6;16;0.9968;3.22;0.72;11.2;5 +8.6;0.53;0.22;2;0.1;7;27;0.9967;3.2;0.56;10.2;6 +11.9;0.39;0.69;2.8;0.095;17;35;0.9994;3.1;0.61;10.8;6 +11.9;0.39;0.69;2.8;0.095;17;35;0.9994;3.1;0.61;10.8;6 +8.4;0.37;0.53;1.8;0.413;9;26;0.9979;3.06;1.06;9.1;6 +6.8;0.56;0.03;1.7;0.084;18;35;0.9968;3.44;0.63;10;6 +10.4;0.33;0.63;2.8;0.084;5;22;0.9998;3.26;0.74;11.2;7 +7;0.23;0.4;1.6;0.063;21;67;0.9952;3.5;0.63;11.1;5 +11.3;0.62;0.67;5.2;0.086;6;19;0.9988;3.22;0.69;13.4;8 +8.9;0.59;0.39;2.3;0.095;5;22;0.9986;3.37;0.58;10.3;5 +9.2;0.63;0.21;2.7;0.097;29;65;0.9988;3.28;0.58;9.6;5 +10.4;0.33;0.63;2.8;0.084;5;22;0.9998;3.26;0.74;11.2;7 +11.6;0.58;0.66;2.2;0.074;10;47;1.0008;3.25;0.57;9;3 +9.2;0.43;0.52;2.3;0.083;14;23;0.9976;3.35;0.61;11.3;6 +8.3;0.615;0.22;2.6;0.087;6;19;0.9982;3.26;0.61;9.3;5 +11;0.26;0.68;2.55;0.085;10;25;0.997;3.18;0.61;11.8;5 +8.1;0.66;0.7;2.2;0.098;25;129;0.9972;3.08;0.53;9;5 +11.5;0.315;0.54;2.1;0.084;5;15;0.9987;2.98;0.7;9.2;6 +10;0.29;0.4;2.9;0.098;10;26;1.0006;3.48;0.91;9.7;5 +10.3;0.5;0.42;2;0.069;21;51;0.9982;3.16;0.72;11.5;6 +8.8;0.46;0.45;2.6;0.065;7;18;0.9947;3.32;0.79;14;6 +11.4;0.36;0.69;2.1;0.09;6;21;1;3.17;0.62;9.2;6 +8.7;0.82;0.02;1.2;0.07;36;48;0.9952;3.2;0.58;9.8;5 +13;0.32;0.65;2.6;0.093;15;47;0.9996;3.05;0.61;10.6;5 +9.6;0.54;0.42;2.4;0.081;25;52;0.997;3.2;0.71;11.4;6 +12.5;0.37;0.55;2.6;0.083;25;68;0.9995;3.15;0.82;10.4;6 +9.9;0.35;0.55;2.1;0.062;5;14;0.9971;3.26;0.79;10.6;5 +10.5;0.28;0.51;1.7;0.08;10;24;0.9982;3.2;0.89;9.4;6 +9.6;0.68;0.24;2.2;0.087;5;28;0.9988;3.14;0.6;10.2;5 +9.3;0.27;0.41;2;0.091;6;16;0.998;3.28;0.7;9.7;5 +10.4;0.24;0.49;1.8;0.075;6;20;0.9977;3.18;1.06;11;6 +9.6;0.68;0.24;2.2;0.087;5;28;0.9988;3.14;0.6;10.2;5 +9.4;0.685;0.11;2.7;0.077;6;31;0.9984;3.19;0.7;10.1;6 +10.6;0.28;0.39;15.5;0.069;6;23;1.0026;3.12;0.66;9.2;5 +9.4;0.3;0.56;2.8;0.08;6;17;0.9964;3.15;0.92;11.7;8 +10.6;0.36;0.59;2.2;0.152;6;18;0.9986;3.04;1.05;9.4;5 +10.6;0.36;0.6;2.2;0.152;7;18;0.9986;3.04;1.06;9.4;5 +10.6;0.44;0.68;4.1;0.114;6;24;0.997;3.06;0.66;13.4;6 +10.2;0.67;0.39;1.9;0.054;6;17;0.9976;3.17;0.47;10;5 +10.2;0.67;0.39;1.9;0.054;6;17;0.9976;3.17;0.47;10;5 +10.2;0.645;0.36;1.8;0.053;5;14;0.9982;3.17;0.42;10;6 +11.6;0.32;0.55;2.8;0.081;35;67;1.0002;3.32;0.92;10.8;7 +9.3;0.39;0.4;2.6;0.073;10;26;0.9984;3.34;0.75;10.2;6 +9.3;0.775;0.27;2.8;0.078;24;56;0.9984;3.31;0.67;10.6;6 +9.2;0.41;0.5;2.5;0.055;12;25;0.9952;3.34;0.79;13.3;7 +8.9;0.4;0.51;2.6;0.052;13;27;0.995;3.32;0.9;13.4;7 +8.7;0.69;0.31;3;0.086;23;81;1.0002;3.48;0.74;11.6;6 +6.5;0.39;0.23;8.3;0.051;28;91;0.9952;3.44;0.55;12.1;6 +10.7;0.35;0.53;2.6;0.07;5;16;0.9972;3.15;0.65;11;8 +7.8;0.52;0.25;1.9;0.081;14;38;0.9984;3.43;0.65;9;6 +7.2;0.34;0.32;2.5;0.09;43;113;0.9966;3.32;0.79;11.1;5 +10.7;0.35;0.53;2.6;0.07;5;16;0.9972;3.15;0.65;11;8 +8.7;0.69;0.31;3;0.086;23;81;1.0002;3.48;0.74;11.6;6 +7.8;0.52;0.25;1.9;0.081;14;38;0.9984;3.43;0.65;9;6 +10.4;0.44;0.73;6.55;0.074;38;76;0.999;3.17;0.85;12;7 +10.4;0.44;0.73;6.55;0.074;38;76;0.999;3.17;0.85;12;7 +10.5;0.26;0.47;1.9;0.078;6;24;0.9976;3.18;1.04;10.9;7 +10.5;0.24;0.42;1.8;0.077;6;22;0.9976;3.21;1.05;10.8;7 +10.2;0.49;0.63;2.9;0.072;10;26;0.9968;3.16;0.78;12.5;7 +10.4;0.24;0.46;1.8;0.075;6;21;0.9976;3.25;1.02;10.8;7 +11.2;0.67;0.55;2.3;0.084;6;13;1;3.17;0.71;9.5;6 +10;0.59;0.31;2.2;0.09;26;62;0.9994;3.18;0.63;10.2;6 +13.3;0.29;0.75;2.8;0.084;23;43;0.9986;3.04;0.68;11.4;7 +12.4;0.42;0.49;4.6;0.073;19;43;0.9978;3.02;0.61;9.5;5 +10;0.59;0.31;2.2;0.09;26;62;0.9994;3.18;0.63;10.2;6 +10.7;0.4;0.48;2.1;0.125;15;49;0.998;3.03;0.81;9.7;6 +10.5;0.51;0.64;2.4;0.107;6;15;0.9973;3.09;0.66;11.8;7 +10.5;0.51;0.64;2.4;0.107;6;15;0.9973;3.09;0.66;11.8;7 +8.5;0.655;0.49;6.1;0.122;34;151;1.001;3.31;1.14;9.3;5 +12.5;0.6;0.49;4.3;0.1;5;14;1.001;3.25;0.74;11.9;6 +10.4;0.61;0.49;2.1;0.2;5;16;0.9994;3.16;0.63;8.4;3 +10.9;0.21;0.49;2.8;0.088;11;32;0.9972;3.22;0.68;11.7;6 +7.3;0.365;0.49;2.5;0.088;39;106;0.9966;3.36;0.78;11;5 +9.8;0.25;0.49;2.7;0.088;15;33;0.9982;3.42;0.9;10;6 +7.6;0.41;0.49;2;0.088;16;43;0.998;3.48;0.64;9.1;5 +8.2;0.39;0.49;2.3;0.099;47;133;0.9979;3.38;0.99;9.8;5 +9.3;0.4;0.49;2.5;0.085;38;142;0.9978;3.22;0.55;9.4;5 +9.2;0.43;0.49;2.4;0.086;23;116;0.9976;3.23;0.64;9.5;5 +10.4;0.64;0.24;2.8;0.105;29;53;0.9998;3.24;0.67;9.9;5 +7.3;0.365;0.49;2.5;0.088;39;106;0.9966;3.36;0.78;11;5 +7;0.38;0.49;2.5;0.097;33;85;0.9962;3.39;0.77;11.4;6 +8.2;0.42;0.49;2.6;0.084;32;55;0.9988;3.34;0.75;8.7;6 +9.9;0.63;0.24;2.4;0.077;6;33;0.9974;3.09;0.57;9.4;5 +9.1;0.22;0.24;2.1;0.078;1;28;0.999;3.41;0.87;10.3;6 +11.9;0.38;0.49;2.7;0.098;12;42;1.0004;3.16;0.61;10.3;5 +11.9;0.38;0.49;2.7;0.098;12;42;1.0004;3.16;0.61;10.3;5 +10.3;0.27;0.24;2.1;0.072;15;33;0.9956;3.22;0.66;12.8;6 +10;0.48;0.24;2.7;0.102;13;32;1;3.28;0.56;10;6 +9.1;0.22;0.24;2.1;0.078;1;28;0.999;3.41;0.87;10.3;6 +9.9;0.63;0.24;2.4;0.077;6;33;0.9974;3.09;0.57;9.4;5 +8.1;0.825;0.24;2.1;0.084;5;13;0.9972;3.37;0.77;10.7;6 +12.9;0.35;0.49;5.8;0.066;5;35;1.0014;3.2;0.66;12;7 +11.2;0.5;0.74;5.15;0.1;5;17;0.9996;3.22;0.62;11.2;5 +9.2;0.59;0.24;3.3;0.101;20;47;0.9988;3.26;0.67;9.6;5 +9.5;0.46;0.49;6.3;0.064;5;17;0.9988;3.21;0.73;11;6 +9.3;0.715;0.24;2.1;0.07;5;20;0.9966;3.12;0.59;9.9;5 +11.2;0.66;0.24;2.5;0.085;16;53;0.9993;3.06;0.72;11;6 +14.3;0.31;0.74;1.8;0.075;6;15;1.0008;2.86;0.79;8.4;6 +9.1;0.47;0.49;2.6;0.094;38;106;0.9982;3.08;0.59;9.1;5 +7.5;0.55;0.24;2;0.078;10;28;0.9983;3.45;0.78;9.5;6 +10.6;0.31;0.49;2.5;0.067;6;21;0.9987;3.26;0.86;10.7;6 +12.4;0.35;0.49;2.6;0.079;27;69;0.9994;3.12;0.75;10.4;6 +9;0.53;0.49;1.9;0.171;6;25;0.9975;3.27;0.61;9.4;6 +6.8;0.51;0.01;2.1;0.074;9;25;0.9958;3.33;0.56;9.5;6 +9.4;0.43;0.24;2.8;0.092;14;45;0.998;3.19;0.73;10;6 +9.5;0.46;0.24;2.7;0.092;14;44;0.998;3.12;0.74;10;6 +5;1.04;0.24;1.6;0.05;32;96;0.9934;3.74;0.62;11.5;5 +15.5;0.645;0.49;4.2;0.095;10;23;1.00315;2.92;0.74;11.1;5 +15.5;0.645;0.49;4.2;0.095;10;23;1.00315;2.92;0.74;11.1;5 +10.9;0.53;0.49;4.6;0.118;10;17;1.0002;3.07;0.56;11.7;6 +15.6;0.645;0.49;4.2;0.095;10;23;1.00315;2.92;0.74;11.1;5 +10.9;0.53;0.49;4.6;0.118;10;17;1.0002;3.07;0.56;11.7;6 +13;0.47;0.49;4.3;0.085;6;47;1.0021;3.3;0.68;12.7;6 +12.7;0.6;0.49;2.8;0.075;5;19;0.9994;3.14;0.57;11.4;5 +9;0.44;0.49;2.4;0.078;26;121;0.9978;3.23;0.58;9.2;5 +9;0.54;0.49;2.9;0.094;41;110;0.9982;3.08;0.61;9.2;5 +7.6;0.29;0.49;2.7;0.092;25;60;0.9971;3.31;0.61;10.1;6 +13;0.47;0.49;4.3;0.085;6;47;1.0021;3.3;0.68;12.7;6 +12.7;0.6;0.49;2.8;0.075;5;19;0.9994;3.14;0.57;11.4;5 +8.7;0.7;0.24;2.5;0.226;5;15;0.9991;3.32;0.6;9;6 +8.7;0.7;0.24;2.5;0.226;5;15;0.9991;3.32;0.6;9;6 +9.8;0.5;0.49;2.6;0.25;5;20;0.999;3.31;0.79;10.7;6 +6.2;0.36;0.24;2.2;0.095;19;42;0.9946;3.57;0.57;11.7;6 +11.5;0.35;0.49;3.3;0.07;10;37;1.0003;3.32;0.91;11;6 +6.2;0.36;0.24;2.2;0.095;19;42;0.9946;3.57;0.57;11.7;6 +10.2;0.24;0.49;2.4;0.075;10;28;0.9978;3.14;0.61;10.4;5 +10.5;0.59;0.49;2.1;0.07;14;47;0.9991;3.3;0.56;9.6;4 +10.6;0.34;0.49;3.2;0.078;20;78;0.9992;3.19;0.7;10;6 +12.3;0.27;0.49;3.1;0.079;28;46;0.9993;3.2;0.8;10.2;6 +9.9;0.5;0.24;2.3;0.103;6;14;0.9978;3.34;0.52;10;4 +8.8;0.44;0.49;2.8;0.083;18;111;0.9982;3.3;0.6;9.5;5 +8.8;0.47;0.49;2.9;0.085;17;110;0.9982;3.29;0.6;9.8;5 +10.6;0.31;0.49;2.2;0.063;18;40;0.9976;3.14;0.51;9.8;6 +12.3;0.5;0.49;2.2;0.089;5;14;1.0002;3.19;0.44;9.6;5 +12.3;0.5;0.49;2.2;0.089;5;14;1.0002;3.19;0.44;9.6;5 +11.7;0.49;0.49;2.2;0.083;5;15;1;3.19;0.43;9.2;5 +12;0.28;0.49;1.9;0.074;10;21;0.9976;2.98;0.66;9.9;7 +11.8;0.33;0.49;3.4;0.093;54;80;1.0002;3.3;0.76;10.7;7 +7.6;0.51;0.24;2.4;0.091;8;38;0.998;3.47;0.66;9.6;6 +11.1;0.31;0.49;2.7;0.094;16;47;0.9986;3.12;1.02;10.6;7 +7.3;0.73;0.24;1.9;0.108;18;102;0.9967;3.26;0.59;9.3;5 +5;0.42;0.24;2;0.06;19;50;0.9917;3.72;0.74;14;8 +10.2;0.29;0.49;2.6;0.059;5;13;0.9976;3.05;0.74;10.5;7 +9;0.45;0.49;2.6;0.084;21;75;0.9987;3.35;0.57;9.7;5 +6.6;0.39;0.49;1.7;0.07;23;149;0.9922;3.12;0.5;11.5;6 +9;0.45;0.49;2.6;0.084;21;75;0.9987;3.35;0.57;9.7;5 +9.9;0.49;0.58;3.5;0.094;9;43;1.0004;3.29;0.58;9;5 +7.9;0.72;0.17;2.6;0.096;20;38;0.9978;3.4;0.53;9.5;5 +8.9;0.595;0.41;7.9;0.086;30;109;0.9998;3.27;0.57;9.3;5 +12.4;0.4;0.51;2;0.059;6;24;0.9994;3.04;0.6;9.3;6 +11.9;0.58;0.58;1.9;0.071;5;18;0.998;3.09;0.63;10;6 +8.5;0.585;0.18;2.1;0.078;5;30;0.9967;3.2;0.48;9.8;6 +12.7;0.59;0.45;2.3;0.082;11;22;1;3;0.7;9.3;6 +8.2;0.915;0.27;2.1;0.088;7;23;0.9962;3.26;0.47;10;4 +13.2;0.46;0.52;2.2;0.071;12;35;1.0006;3.1;0.56;9;6 +7.7;0.835;0;2.6;0.081;6;14;0.9975;3.3;0.52;9.3;5 +13.2;0.46;0.52;2.2;0.071;12;35;1.0006;3.1;0.56;9;6 +8.3;0.58;0.13;2.9;0.096;14;63;0.9984;3.17;0.62;9.1;6 +8.3;0.6;0.13;2.6;0.085;6;24;0.9984;3.31;0.59;9.2;6 +9.4;0.41;0.48;4.6;0.072;10;20;0.9973;3.34;0.79;12.2;7 +8.8;0.48;0.41;3.3;0.092;26;52;0.9982;3.31;0.53;10.5;6 +10.1;0.65;0.37;5.1;0.11;11;65;1.0026;3.32;0.64;10.4;6 +6.3;0.36;0.19;3.2;0.075;15;39;0.9956;3.56;0.52;12.7;6 +8.8;0.24;0.54;2.5;0.083;25;57;0.9983;3.39;0.54;9.2;5 +13.2;0.38;0.55;2.7;0.081;5;16;1.0006;2.98;0.54;9.4;5 +7.5;0.64;0;2.4;0.077;18;29;0.9965;3.32;0.6;10;6 +8.2;0.39;0.38;1.5;0.058;10;29;0.9962;3.26;0.74;9.8;5 +9.2;0.755;0.18;2.2;0.148;10;103;0.9969;2.87;1.36;10.2;6 +9.6;0.6;0.5;2.3;0.079;28;71;0.9997;3.5;0.57;9.7;5 +9.6;0.6;0.5;2.3;0.079;28;71;0.9997;3.5;0.57;9.7;5 +11.5;0.31;0.51;2.2;0.079;14;28;0.9982;3.03;0.93;9.8;6 +11.4;0.46;0.5;2.7;0.122;4;17;1.0006;3.13;0.7;10.2;5 +11.3;0.37;0.41;2.3;0.088;6;16;0.9988;3.09;0.8;9.3;5 +8.3;0.54;0.24;3.4;0.076;16;112;0.9976;3.27;0.61;9.4;5 +8.2;0.56;0.23;3.4;0.078;14;104;0.9976;3.28;0.62;9.4;5 +10;0.58;0.22;1.9;0.08;9;32;0.9974;3.13;0.55;9.5;5 +7.9;0.51;0.25;2.9;0.077;21;45;0.9974;3.49;0.96;12.1;6 +6.8;0.69;0;5.6;0.124;21;58;0.9997;3.46;0.72;10.2;5 +6.8;0.69;0;5.6;0.124;21;58;0.9997;3.46;0.72;10.2;5 +8.8;0.6;0.29;2.2;0.098;5;15;0.9988;3.36;0.49;9.1;5 +8.8;0.6;0.29;2.2;0.098;5;15;0.9988;3.36;0.49;9.1;5 +8.7;0.54;0.26;2.5;0.097;7;31;0.9976;3.27;0.6;9.3;6 +7.6;0.685;0.23;2.3;0.111;20;84;0.9964;3.21;0.61;9.3;5 +8.7;0.54;0.26;2.5;0.097;7;31;0.9976;3.27;0.6;9.3;6 +10.4;0.28;0.54;2.7;0.105;5;19;0.9988;3.25;0.63;9.5;5 +7.6;0.41;0.14;3;0.087;21;43;0.9964;3.32;0.57;10.5;6 +10.1;0.935;0.22;3.4;0.105;11;86;1.001;3.43;0.64;11.3;4 +7.9;0.35;0.21;1.9;0.073;46;102;0.9964;3.27;0.58;9.5;5 +8.7;0.84;0;1.4;0.065;24;33;0.9954;3.27;0.55;9.7;5 +9.6;0.88;0.28;2.4;0.086;30;147;0.9979;3.24;0.53;9.4;5 +9.5;0.885;0.27;2.3;0.084;31;145;0.9978;3.24;0.53;9.4;5 +7.7;0.915;0.12;2.2;0.143;7;23;0.9964;3.35;0.65;10.2;7 +8.9;0.29;0.35;1.9;0.067;25;57;0.997;3.18;1.36;10.3;6 +9.9;0.54;0.45;2.3;0.071;16;40;0.9991;3.39;0.62;9.4;5 +9.5;0.59;0.44;2.3;0.071;21;68;0.9992;3.46;0.63;9.5;5 +9.9;0.54;0.45;2.3;0.071;16;40;0.9991;3.39;0.62;9.4;5 +9.5;0.59;0.44;2.3;0.071;21;68;0.9992;3.46;0.63;9.5;5 +9.9;0.54;0.45;2.3;0.071;16;40;0.9991;3.39;0.62;9.4;5 +7.8;0.64;0.1;6;0.115;5;11;0.9984;3.37;0.69;10.1;7 +7.3;0.67;0.05;3.6;0.107;6;20;0.9972;3.4;0.63;10.1;5 +8.3;0.845;0.01;2.2;0.07;5;14;0.9967;3.32;0.58;11;4 +8.7;0.48;0.3;2.8;0.066;10;28;0.9964;3.33;0.67;11.2;7 +6.7;0.42;0.27;8.6;0.068;24;148;0.9948;3.16;0.57;11.3;6 +10.7;0.43;0.39;2.2;0.106;8;32;0.9986;2.89;0.5;9.6;5 +9.8;0.88;0.25;2.5;0.104;35;155;1.001;3.41;0.67;11.2;5 +15.9;0.36;0.65;7.5;0.096;22;71;0.9976;2.98;0.84;14.9;5 +9.4;0.33;0.59;2.8;0.079;9;30;0.9976;3.12;0.54;12;6 +8.6;0.47;0.47;2.4;0.074;7;29;0.9979;3.08;0.46;9.5;5 +9.7;0.55;0.17;2.9;0.087;20;53;1.0004;3.14;0.61;9.4;5 +10.7;0.43;0.39;2.2;0.106;8;32;0.9986;2.89;0.5;9.6;5 +12;0.5;0.59;1.4;0.073;23;42;0.998;2.92;0.68;10.5;7 +7.2;0.52;0.07;1.4;0.074;5;20;0.9973;3.32;0.81;9.6;6 +7.1;0.84;0.02;4.4;0.096;5;13;0.997;3.41;0.57;11;4 +7.2;0.52;0.07;1.4;0.074;5;20;0.9973;3.32;0.81;9.6;6 +7.5;0.42;0.31;1.6;0.08;15;42;0.9978;3.31;0.64;9;5 +7.2;0.57;0.06;1.6;0.076;9;27;0.9972;3.36;0.7;9.6;6 +10.1;0.28;0.46;1.8;0.05;5;13;0.9974;3.04;0.79;10.2;6 +12.1;0.4;0.52;2;0.092;15;54;1;3.03;0.66;10.2;5 +9.4;0.59;0.14;2;0.084;25;48;0.9981;3.14;0.56;9.7;5 +8.3;0.49;0.36;1.8;0.222;6;16;0.998;3.18;0.6;9.5;6 +11.3;0.34;0.45;2;0.082;6;15;0.9988;2.94;0.66;9.2;6 +10;0.73;0.43;2.3;0.059;15;31;0.9966;3.15;0.57;11;5 +11.3;0.34;0.45;2;0.082;6;15;0.9988;2.94;0.66;9.2;6 +6.9;0.4;0.24;2.5;0.083;30;45;0.9959;3.26;0.58;10;5 +8.2;0.73;0.21;1.7;0.074;5;13;0.9968;3.2;0.52;9.5;5 +9.8;1.24;0.34;2;0.079;32;151;0.998;3.15;0.53;9.5;5 +8.2;0.73;0.21;1.7;0.074;5;13;0.9968;3.2;0.52;9.5;5 +10.8;0.4;0.41;2.2;0.084;7;17;0.9984;3.08;0.67;9.3;6 +9.3;0.41;0.39;2.2;0.064;12;31;0.9984;3.26;0.65;10.2;5 +10.8;0.4;0.41;2.2;0.084;7;17;0.9984;3.08;0.67;9.3;6 +8.6;0.8;0.11;2.3;0.084;12;31;0.9979;3.4;0.48;9.9;5 +8.3;0.78;0.1;2.6;0.081;45;87;0.9983;3.48;0.53;10;5 +10.8;0.26;0.45;3.3;0.06;20;49;0.9972;3.13;0.54;9.6;5 +13.3;0.43;0.58;1.9;0.07;15;40;1.0004;3.06;0.49;9;5 +8;0.45;0.23;2.2;0.094;16;29;0.9962;3.21;0.49;10.2;6 +8.5;0.46;0.31;2.25;0.078;32;58;0.998;3.33;0.54;9.8;5 +8.1;0.78;0.23;2.6;0.059;5;15;0.997;3.37;0.56;11.3;5 +9.8;0.98;0.32;2.3;0.078;35;152;0.998;3.25;0.48;9.4;5 +8.1;0.78;0.23;2.6;0.059;5;15;0.997;3.37;0.56;11.3;5 +7.1;0.65;0.18;1.8;0.07;13;40;0.997;3.44;0.6;9.1;5 +9.1;0.64;0.23;3.1;0.095;13;38;0.9998;3.28;0.59;9.7;5 +7.7;0.66;0.04;1.6;0.039;4;9;0.9962;3.4;0.47;9.4;5 +8.1;0.38;0.48;1.8;0.157;5;17;0.9976;3.3;1.05;9.4;5 +7.4;1.185;0;4.25;0.097;5;14;0.9966;3.63;0.54;10.7;3 +9.2;0.92;0.24;2.6;0.087;12;93;0.9998;3.48;0.54;9.8;5 +8.6;0.49;0.51;2;0.422;16;62;0.9979;3.03;1.17;9;5 +9;0.48;0.32;2.8;0.084;21;122;0.9984;3.32;0.62;9.4;5 +9;0.47;0.31;2.7;0.084;24;125;0.9984;3.31;0.61;9.4;5 +5.1;0.47;0.02;1.3;0.034;18;44;0.9921;3.9;0.62;12.8;6 +7;0.65;0.02;2.1;0.066;8;25;0.9972;3.47;0.67;9.5;6 +7;0.65;0.02;2.1;0.066;8;25;0.9972;3.47;0.67;9.5;6 +9.4;0.615;0.28;3.2;0.087;18;72;1.0001;3.31;0.53;9.7;5 +11.8;0.38;0.55;2.1;0.071;5;19;0.9986;3.11;0.62;10.8;6 +10.6;1.02;0.43;2.9;0.076;26;88;0.9984;3.08;0.57;10.1;6 +7;0.65;0.02;2.1;0.066;8;25;0.9972;3.47;0.67;9.5;6 +7;0.64;0.02;2.1;0.067;9;23;0.997;3.47;0.67;9.4;6 +7.5;0.38;0.48;2.6;0.073;22;84;0.9972;3.32;0.7;9.6;4 +9.1;0.765;0.04;1.6;0.078;4;14;0.998;3.29;0.54;9.7;4 +8.4;1.035;0.15;6;0.073;11;54;0.999;3.37;0.49;9.9;5 +7;0.78;0.08;2;0.093;10;19;0.9956;3.4;0.47;10;5 +7.4;0.49;0.19;3;0.077;16;37;0.9966;3.37;0.51;10.5;5 +7.8;0.545;0.12;2.5;0.068;11;35;0.996;3.34;0.61;11.6;6 +9.7;0.31;0.47;1.6;0.062;13;33;0.9983;3.27;0.66;10;6 +10.6;1.025;0.43;2.8;0.08;21;84;0.9985;3.06;0.57;10.1;5 +8.9;0.565;0.34;3;0.093;16;112;0.9998;3.38;0.61;9.5;5 +8.7;0.69;0;3.2;0.084;13;33;0.9992;3.36;0.45;9.4;5 +8;0.43;0.36;2.3;0.075;10;48;0.9976;3.34;0.46;9.4;5 +9.9;0.74;0.28;2.6;0.078;21;77;0.998;3.28;0.51;9.8;5 +7.2;0.49;0.18;2.7;0.069;13;34;0.9967;3.29;0.48;9.2;6 +8;0.43;0.36;2.3;0.075;10;48;0.9976;3.34;0.46;9.4;5 +7.6;0.46;0.11;2.6;0.079;12;49;0.9968;3.21;0.57;10;5 +8.4;0.56;0.04;2;0.082;10;22;0.9976;3.22;0.44;9.6;5 +7.1;0.66;0;3.9;0.086;17;45;0.9976;3.46;0.54;9.5;5 +8.4;0.56;0.04;2;0.082;10;22;0.9976;3.22;0.44;9.6;5 +8.9;0.48;0.24;2.85;0.094;35;106;0.9982;3.1;0.53;9.2;5 +7.6;0.42;0.08;2.7;0.084;15;48;0.9968;3.21;0.59;10;5 +7.1;0.31;0.3;2.2;0.053;36;127;0.9965;2.94;1.62;9.5;5 +7.5;1.115;0.1;3.1;0.086;5;12;0.9958;3.54;0.6;11.2;4 +9;0.66;0.17;3;0.077;5;13;0.9976;3.29;0.55;10.4;5 +8.1;0.72;0.09;2.8;0.084;18;49;0.9994;3.43;0.72;11.1;6 +6.4;0.57;0.02;1.8;0.067;4;11;0.997;3.46;0.68;9.5;5 +6.4;0.57;0.02;1.8;0.067;4;11;0.997;3.46;0.68;9.5;5 +6.4;0.865;0.03;3.2;0.071;27;58;0.995;3.61;0.49;12.7;6 +9.5;0.55;0.66;2.3;0.387;12;37;0.9982;3.17;0.67;9.6;5 +8.9;0.875;0.13;3.45;0.088;4;14;0.9994;3.44;0.52;11.5;5 +7.3;0.835;0.03;2.1;0.092;10;19;0.9966;3.39;0.47;9.6;5 +7;0.45;0.34;2.7;0.082;16;72;0.998;3.55;0.6;9.5;5 +7.7;0.56;0.2;2;0.075;9;39;0.9987;3.48;0.62;9.3;5 +7.7;0.965;0.1;2.1;0.112;11;22;0.9963;3.26;0.5;9.5;5 +7.7;0.965;0.1;2.1;0.112;11;22;0.9963;3.26;0.5;9.5;5 +8.2;0.59;0;2.5;0.093;19;58;1.0002;3.5;0.65;9.3;6 +9;0.46;0.23;2.8;0.092;28;104;0.9983;3.1;0.56;9.2;5 +9;0.69;0;2.4;0.088;19;38;0.999;3.35;0.6;9.3;5 +8.3;0.76;0.29;4.2;0.075;12;16;0.9965;3.45;0.68;11.5;6 +9.2;0.53;0.24;2.6;0.078;28;139;0.99788;3.21;0.57;9.5;5 +6.5;0.615;0;1.9;0.065;9;18;0.9972;3.46;0.65;9.2;5 +11.6;0.41;0.58;2.8;0.096;25;101;1.00024;3.13;0.53;10;5 +11.1;0.39;0.54;2.7;0.095;21;101;1.0001;3.13;0.51;9.5;5 +7.3;0.51;0.18;2.1;0.07;12;28;0.99768;3.52;0.73;9.5;6 +8.2;0.34;0.38;2.5;0.08;12;57;0.9978;3.3;0.47;9;6 +8.6;0.33;0.4;2.6;0.083;16;68;0.99782;3.3;0.48;9.4;5 +7.2;0.5;0.18;2.1;0.071;12;31;0.99761;3.52;0.72;9.6;6 +7.3;0.51;0.18;2.1;0.07;12;28;0.99768;3.52;0.73;9.5;6 +8.3;0.65;0.1;2.9;0.089;17;40;0.99803;3.29;0.55;9.5;5 +8.3;0.65;0.1;2.9;0.089;17;40;0.99803;3.29;0.55;9.5;5 +7.6;0.54;0.13;2.5;0.097;24;66;0.99785;3.39;0.61;9.4;5 +8.3;0.65;0.1;2.9;0.089;17;40;0.99803;3.29;0.55;9.5;5 +7.8;0.48;0.68;1.7;0.415;14;32;0.99656;3.09;1.06;9.1;6 +7.8;0.91;0.07;1.9;0.058;22;47;0.99525;3.51;0.43;10.7;6 +6.3;0.98;0.01;2;0.057;15;33;0.99488;3.6;0.46;11.2;6 +8.1;0.87;0;2.2;0.084;10;31;0.99656;3.25;0.5;9.8;5 +8.1;0.87;0;2.2;0.084;10;31;0.99656;3.25;0.5;9.8;5 +8.8;0.42;0.21;2.5;0.092;33;88;0.99823;3.19;0.52;9.2;5 +9;0.58;0.25;2.8;0.075;9;104;0.99779;3.23;0.57;9.7;5 +9.3;0.655;0.26;2;0.096;5;35;0.99738;3.25;0.42;9.6;5 +8.8;0.7;0;1.7;0.069;8;19;0.99701;3.31;0.53;10;6 +9.3;0.655;0.26;2;0.096;5;35;0.99738;3.25;0.42;9.6;5 +9.1;0.68;0.11;2.8;0.093;11;44;0.99888;3.31;0.55;9.5;6 +9.2;0.67;0.1;3;0.091;12;48;0.99888;3.31;0.54;9.5;6 +8.8;0.59;0.18;2.9;0.089;12;74;0.99738;3.14;0.54;9.4;5 +7.5;0.6;0.32;2.7;0.103;13;98;0.99938;3.45;0.62;9.5;5 +7.1;0.59;0.02;2.3;0.082;24;94;0.99744;3.55;0.53;9.7;6 +7.9;0.72;0.01;1.9;0.076;7;32;0.99668;3.39;0.54;9.6;5 +7.1;0.59;0.02;2.3;0.082;24;94;0.99744;3.55;0.53;9.7;6 +9.4;0.685;0.26;2.4;0.082;23;143;0.9978;3.28;0.55;9.4;5 +9.5;0.57;0.27;2.3;0.082;23;144;0.99782;3.27;0.55;9.4;5 +7.9;0.4;0.29;1.8;0.157;1;44;0.9973;3.3;0.92;9.5;6 +7.9;0.4;0.3;1.8;0.157;2;45;0.99727;3.31;0.91;9.5;6 +7.2;1;0;3;0.102;7;16;0.99586;3.43;0.46;10;5 +6.9;0.765;0.18;2.4;0.243;5.5;48;0.99612;3.4;0.6;10.3;6 +6.9;0.635;0.17;2.4;0.241;6;18;0.9961;3.4;0.59;10.3;6 +8.3;0.43;0.3;3.4;0.079;7;34;0.99788;3.36;0.61;10.5;5 +7.1;0.52;0.03;2.6;0.076;21;92;0.99745;3.5;0.6;9.8;5 +7;0.57;0;2;0.19;12;45;0.99676;3.31;0.6;9.4;6 +6.5;0.46;0.14;2.4;0.114;9;37;0.99732;3.66;0.65;9.8;5 +9;0.82;0.05;2.4;0.081;26;96;0.99814;3.36;0.53;10;5 +6.5;0.46;0.14;2.4;0.114;9;37;0.99732;3.66;0.65;9.8;5 +7.1;0.59;0.01;2.5;0.077;20;85;0.99746;3.55;0.59;9.8;5 +9.9;0.35;0.41;2.3;0.083;11;61;0.9982;3.21;0.5;9.5;5 +9.9;0.35;0.41;2.3;0.083;11;61;0.9982;3.21;0.5;9.5;5 +10;0.56;0.24;2.2;0.079;19;58;0.9991;3.18;0.56;10.1;6 +10;0.56;0.24;2.2;0.079;19;58;0.9991;3.18;0.56;10.1;6 +8.6;0.63;0.17;2.9;0.099;21;119;0.998;3.09;0.52;9.3;5 +7.4;0.37;0.43;2.6;0.082;18;82;0.99708;3.33;0.68;9.7;6 +8.8;0.64;0.17;2.9;0.084;25;130;0.99818;3.23;0.54;9.6;5 +7.1;0.61;0.02;2.5;0.081;17;87;0.99745;3.48;0.6;9.7;6 +7.7;0.6;0;2.6;0.055;7;13;0.99639;3.38;0.56;10.8;5 +10.1;0.27;0.54;2.3;0.065;7;26;0.99531;3.17;0.53;12.5;6 +10.8;0.89;0.3;2.6;0.132;7;60;0.99786;2.99;1.18;10.2;5 +8.7;0.46;0.31;2.5;0.126;24;64;0.99746;3.1;0.74;9.6;5 +9.3;0.37;0.44;1.6;0.038;21;42;0.99526;3.24;0.81;10.8;7 +9.4;0.5;0.34;3.6;0.082;5;14;0.9987;3.29;0.52;10.7;6 +9.4;0.5;0.34;3.6;0.082;5;14;0.9987;3.29;0.52;10.7;6 +7.2;0.61;0.08;4;0.082;26;108;0.99641;3.25;0.51;9.4;5 +8.6;0.55;0.09;3.3;0.068;8;17;0.99735;3.23;0.44;10;5 +5.1;0.585;0;1.7;0.044;14;86;0.99264;3.56;0.94;12.9;7 +7.7;0.56;0.08;2.5;0.114;14;46;0.9971;3.24;0.66;9.6;6 +8.4;0.52;0.22;2.7;0.084;4;18;0.99682;3.26;0.57;9.9;6 +8.2;0.28;0.4;2.4;0.052;4;10;0.99356;3.33;0.7;12.8;7 +8.4;0.25;0.39;2;0.041;4;10;0.99386;3.27;0.71;12.5;7 +8.2;0.28;0.4;2.4;0.052;4;10;0.99356;3.33;0.7;12.8;7 +7.4;0.53;0.12;1.9;0.165;4;12;0.99702;3.26;0.86;9.2;5 +7.6;0.48;0.31;2.8;0.07;4;15;0.99693;3.22;0.55;10.3;6 +7.3;0.49;0.1;2.6;0.068;4;14;0.99562;3.3;0.47;10.5;5 +12.9;0.5;0.55;2.8;0.072;7;24;1.00012;3.09;0.68;10.9;6 +10.8;0.45;0.33;2.5;0.099;20;38;0.99818;3.24;0.71;10.8;5 +6.9;0.39;0.24;2.1;0.102;4;7;0.99462;3.44;0.58;11.4;4 +12.6;0.41;0.54;2.8;0.103;19;41;0.99939;3.21;0.76;11.3;6 +10.8;0.45;0.33;2.5;0.099;20;38;0.99818;3.24;0.71;10.8;5 +9.8;0.51;0.19;3.2;0.081;8;30;0.9984;3.23;0.58;10.5;6 +10.8;0.29;0.42;1.6;0.084;19;27;0.99545;3.28;0.73;11.9;6 +7.1;0.715;0;2.35;0.071;21;47;0.99632;3.29;0.45;9.4;5 +9.1;0.66;0.15;3.2;0.097;9;59;0.99976;3.28;0.54;9.6;5 +7;0.685;0;1.9;0.099;9;22;0.99606;3.34;0.6;9.7;5 +4.9;0.42;0;2.1;0.048;16;42;0.99154;3.71;0.74;14;7 +6.7;0.54;0.13;2;0.076;15;36;0.9973;3.61;0.64;9.8;5 +6.7;0.54;0.13;2;0.076;15;36;0.9973;3.61;0.64;9.8;5 +7.1;0.48;0.28;2.8;0.068;6;16;0.99682;3.24;0.53;10.3;5 +7.1;0.46;0.14;2.8;0.076;15;37;0.99624;3.36;0.49;10.7;5 +7.5;0.27;0.34;2.3;0.05;4;8;0.9951;3.4;0.64;11;7 +7.1;0.46;0.14;2.8;0.076;15;37;0.99624;3.36;0.49;10.7;5 +7.8;0.57;0.09;2.3;0.065;34;45;0.99417;3.46;0.74;12.7;8 +5.9;0.61;0.08;2.1;0.071;16;24;0.99376;3.56;0.77;11.1;6 +7.5;0.685;0.07;2.5;0.058;5;9;0.99632;3.38;0.55;10.9;4 +5.9;0.61;0.08;2.1;0.071;16;24;0.99376;3.56;0.77;11.1;6 +10.4;0.44;0.42;1.5;0.145;34;48;0.99832;3.38;0.86;9.9;3 +11.6;0.47;0.44;1.6;0.147;36;51;0.99836;3.38;0.86;9.9;4 +8.8;0.685;0.26;1.6;0.088;16;23;0.99694;3.32;0.47;9.4;5 +7.6;0.665;0.1;1.5;0.066;27;55;0.99655;3.39;0.51;9.3;5 +6.7;0.28;0.28;2.4;0.012;36;100;0.99064;3.26;0.39;11.7;7 +6.7;0.28;0.28;2.4;0.012;36;100;0.99064;3.26;0.39;11.7;7 +10.1;0.31;0.35;1.6;0.075;9;28;0.99672;3.24;0.83;11.2;7 +6;0.5;0.04;2.2;0.092;13;26;0.99647;3.46;0.47;10;5 +11.1;0.42;0.47;2.65;0.085;9;34;0.99736;3.24;0.77;12.1;7 +6.6;0.66;0;3;0.115;21;31;0.99629;3.45;0.63;10.3;5 +10.6;0.5;0.45;2.6;0.119;34;68;0.99708;3.23;0.72;10.9;6 +7.1;0.685;0.35;2;0.088;9;92;0.9963;3.28;0.62;9.4;5 +9.9;0.25;0.46;1.7;0.062;26;42;0.9959;3.18;0.83;10.6;6 +6.4;0.64;0.21;1.8;0.081;14;31;0.99689;3.59;0.66;9.8;5 +6.4;0.64;0.21;1.8;0.081;14;31;0.99689;3.59;0.66;9.8;5 +7.4;0.68;0.16;1.8;0.078;12;39;0.9977;3.5;0.7;9.9;6 +6.4;0.64;0.21;1.8;0.081;14;31;0.99689;3.59;0.66;9.8;5 +6.4;0.63;0.21;1.6;0.08;12;32;0.99689;3.58;0.66;9.8;5 +9.3;0.43;0.44;1.9;0.085;9;22;0.99708;3.28;0.55;9.5;5 +9.3;0.43;0.44;1.9;0.085;9;22;0.99708;3.28;0.55;9.5;5 +8;0.42;0.32;2.5;0.08;26;122;0.99801;3.22;1.07;9.7;5 +9.3;0.36;0.39;1.5;0.08;41;55;0.99652;3.47;0.73;10.9;6 +9.3;0.36;0.39;1.5;0.08;41;55;0.99652;3.47;0.73;10.9;6 +7.6;0.735;0.02;2.5;0.071;10;14;0.99538;3.51;0.71;11.7;7 +9.3;0.36;0.39;1.5;0.08;41;55;0.99652;3.47;0.73;10.9;6 +8.2;0.26;0.34;2.5;0.073;16;47;0.99594;3.4;0.78;11.3;7 +11.7;0.28;0.47;1.7;0.054;17;32;0.99686;3.15;0.67;10.6;7 +6.8;0.56;0.22;1.8;0.074;15;24;0.99438;3.4;0.82;11.2;6 +7.2;0.62;0.06;2.7;0.077;15;85;0.99746;3.51;0.54;9.5;5 +5.8;1.01;0.66;2;0.039;15;88;0.99357;3.66;0.6;11.5;6 +7.5;0.42;0.32;2.7;0.067;7;25;0.99628;3.24;0.44;10.4;5 +7.2;0.62;0.06;2.5;0.078;17;84;0.99746;3.51;0.53;9.7;5 +7.2;0.62;0.06;2.7;0.077;15;85;0.99746;3.51;0.54;9.5;5 +7.2;0.635;0.07;2.6;0.077;16;86;0.99748;3.51;0.54;9.7;5 +6.8;0.49;0.22;2.3;0.071;13;24;0.99438;3.41;0.83;11.3;6 +6.9;0.51;0.23;2;0.072;13;22;0.99438;3.4;0.84;11.2;6 +6.8;0.56;0.22;1.8;0.074;15;24;0.99438;3.4;0.82;11.2;6 +7.6;0.63;0.03;2;0.08;27;43;0.99578;3.44;0.64;10.9;6 +7.7;0.715;0.01;2.1;0.064;31;43;0.99371;3.41;0.57;11.8;6 +6.9;0.56;0.03;1.5;0.086;36;46;0.99522;3.53;0.57;10.6;5 +7.3;0.35;0.24;2;0.067;28;48;0.99576;3.43;0.54;10;4 +9.1;0.21;0.37;1.6;0.067;6;10;0.99552;3.23;0.58;11.1;7 +10.4;0.38;0.46;2.1;0.104;6;10;0.99664;3.12;0.65;11.8;7 +8.8;0.31;0.4;2.8;0.109;7;16;0.99614;3.31;0.79;11.8;7 +7.1;0.47;0;2.2;0.067;7;14;0.99517;3.4;0.58;10.9;4 +7.7;0.715;0.01;2.1;0.064;31;43;0.99371;3.41;0.57;11.8;6 +8.8;0.61;0.19;4;0.094;30;69;0.99787;3.22;0.5;10;6 +7.2;0.6;0.04;2.5;0.076;18;88;0.99745;3.53;0.55;9.5;5 +9.2;0.56;0.18;1.6;0.078;10;21;0.99576;3.15;0.49;9.9;5 +7.6;0.715;0;2.1;0.068;30;35;0.99533;3.48;0.65;11.4;6 +8.4;0.31;0.29;3.1;0.194;14;26;0.99536;3.22;0.78;12;6 +7.2;0.6;0.04;2.5;0.076;18;88;0.99745;3.53;0.55;9.5;5 +8.8;0.61;0.19;4;0.094;30;69;0.99787;3.22;0.5;10;6 +8.9;0.75;0.14;2.5;0.086;9;30;0.99824;3.34;0.64;10.5;5 +9;0.8;0.12;2.4;0.083;8;28;0.99836;3.33;0.65;10.4;6 +10.7;0.52;0.38;2.6;0.066;29;56;0.99577;3.15;0.79;12.1;7 +6.8;0.57;0;2.5;0.072;32;64;0.99491;3.43;0.56;11.2;6 +10.7;0.9;0.34;6.6;0.112;23;99;1.00289;3.22;0.68;9.3;5 +7.2;0.34;0.24;2;0.071;30;52;0.99576;3.44;0.58;10.1;5 +7.2;0.66;0.03;2.3;0.078;16;86;0.99743;3.53;0.57;9.7;5 +10.1;0.45;0.23;1.9;0.082;10;18;0.99774;3.22;0.65;9.3;6 +7.2;0.66;0.03;2.3;0.078;16;86;0.99743;3.53;0.57;9.7;5 +7.2;0.63;0.03;2.2;0.08;17;88;0.99745;3.53;0.58;9.8;6 +7.1;0.59;0.01;2.3;0.08;27;43;0.9955;3.42;0.58;10.7;6 +8.3;0.31;0.39;2.4;0.078;17;43;0.99444;3.31;0.77;12.5;7 +7.1;0.59;0.01;2.3;0.08;27;43;0.9955;3.42;0.58;10.7;6 +8.3;0.31;0.39;2.4;0.078;17;43;0.99444;3.31;0.77;12.5;7 +8.3;1.02;0.02;3.4;0.084;6;11;0.99892;3.48;0.49;11;3 +8.9;0.31;0.36;2.6;0.056;10;39;0.99562;3.4;0.69;11.8;5 +7.4;0.635;0.1;2.4;0.08;16;33;0.99736;3.58;0.69;10.8;7 +7.4;0.635;0.1;2.4;0.08;16;33;0.99736;3.58;0.69;10.8;7 +6.8;0.59;0.06;6;0.06;11;18;0.9962;3.41;0.59;10.8;7 +6.8;0.59;0.06;6;0.06;11;18;0.9962;3.41;0.59;10.8;7 +9.2;0.58;0.2;3;0.081;15;115;0.998;3.23;0.59;9.5;5 +7.2;0.54;0.27;2.6;0.084;12;78;0.9964;3.39;0.71;11;5 +6.1;0.56;0;2.2;0.079;6;9;0.9948;3.59;0.54;11.5;6 +7.4;0.52;0.13;2.4;0.078;34;61;0.99528;3.43;0.59;10.8;6 +7.3;0.305;0.39;1.2;0.059;7;11;0.99331;3.29;0.52;11.5;6 +9.3;0.38;0.48;3.8;0.132;3;11;0.99577;3.23;0.57;13.2;6 +9.1;0.28;0.46;9;0.114;3;9;0.99901;3.18;0.6;10.9;6 +10;0.46;0.44;2.9;0.065;4;8;0.99674;3.33;0.62;12.2;6 +9.4;0.395;0.46;4.6;0.094;3;10;0.99639;3.27;0.64;12.2;7 +7.3;0.305;0.39;1.2;0.059;7;11;0.99331;3.29;0.52;11.5;6 +8.6;0.315;0.4;2.2;0.079;3;6;0.99512;3.27;0.67;11.9;6 +5.3;0.715;0.19;1.5;0.161;7;62;0.99395;3.62;0.61;11;5 +6.8;0.41;0.31;8.8;0.084;26;45;0.99824;3.38;0.64;10.1;6 +8.4;0.36;0.32;2.2;0.081;32;79;0.9964;3.3;0.72;11;6 +8.4;0.62;0.12;1.8;0.072;38;46;0.99504;3.38;0.89;11.8;6 +9.6;0.41;0.37;2.3;0.091;10;23;0.99786;3.24;0.56;10.5;5 +8.4;0.36;0.32;2.2;0.081;32;79;0.9964;3.3;0.72;11;6 +8.4;0.62;0.12;1.8;0.072;38;46;0.99504;3.38;0.89;11.8;6 +6.8;0.41;0.31;8.8;0.084;26;45;0.99824;3.38;0.64;10.1;6 +8.6;0.47;0.27;2.3;0.055;14;28;0.99516;3.18;0.8;11.2;5 +8.6;0.22;0.36;1.9;0.064;53;77;0.99604;3.47;0.87;11;7 +9.4;0.24;0.33;2.3;0.061;52;73;0.99786;3.47;0.9;10.2;6 +8.4;0.67;0.19;2.2;0.093;11;75;0.99736;3.2;0.59;9.2;4 +8.6;0.47;0.27;2.3;0.055;14;28;0.99516;3.18;0.8;11.2;5 +8.7;0.33;0.38;3.3;0.063;10;19;0.99468;3.3;0.73;12;7 +6.6;0.61;0.01;1.9;0.08;8;25;0.99746;3.69;0.73;10.5;5 +7.4;0.61;0.01;2;0.074;13;38;0.99748;3.48;0.65;9.8;5 +7.6;0.4;0.29;1.9;0.078;29;66;0.9971;3.45;0.59;9.5;6 +7.4;0.61;0.01;2;0.074;13;38;0.99748;3.48;0.65;9.8;5 +6.6;0.61;0.01;1.9;0.08;8;25;0.99746;3.69;0.73;10.5;5 +8.8;0.3;0.38;2.3;0.06;19;72;0.99543;3.39;0.72;11.8;6 +8.8;0.3;0.38;2.3;0.06;19;72;0.99543;3.39;0.72;11.8;6 +12;0.63;0.5;1.4;0.071;6;26;0.99791;3.07;0.6;10.4;4 +7.2;0.38;0.38;2.8;0.068;23;42;0.99356;3.34;0.72;12.9;7 +6.2;0.46;0.17;1.6;0.073;7;11;0.99425;3.61;0.54;11.4;5 +9.6;0.33;0.52;2.2;0.074;13;25;0.99509;3.36;0.76;12.4;7 +9.9;0.27;0.49;5;0.082;9;17;0.99484;3.19;0.52;12.5;7 +10.1;0.43;0.4;2.6;0.092;13;52;0.99834;3.22;0.64;10;7 +9.8;0.5;0.34;2.3;0.094;10;45;0.99864;3.24;0.6;9.7;7 +8.3;0.3;0.49;3.8;0.09;11;24;0.99498;3.27;0.64;12.1;7 +10.2;0.44;0.42;2;0.071;7;20;0.99566;3.14;0.79;11.1;7 +10.2;0.44;0.58;4.1;0.092;11;24;0.99745;3.29;0.99;12;7 +8.3;0.28;0.48;2.1;0.093;6;12;0.99408;3.26;0.62;12.4;7 +8.9;0.12;0.45;1.8;0.075;10;21;0.99552;3.41;0.76;11.9;7 +8.9;0.12;0.45;1.8;0.075;10;21;0.99552;3.41;0.76;11.9;7 +8.9;0.12;0.45;1.8;0.075;10;21;0.99552;3.41;0.76;11.9;7 +8.3;0.28;0.48;2.1;0.093;6;12;0.99408;3.26;0.62;12.4;7 +8.2;0.31;0.4;2.2;0.058;6;10;0.99536;3.31;0.68;11.2;7 +10.2;0.34;0.48;2.1;0.052;5;9;0.99458;3.2;0.69;12.1;7 +7.6;0.43;0.4;2.7;0.082;6;11;0.99538;3.44;0.54;12.2;6 +8.5;0.21;0.52;1.9;0.09;9;23;0.99648;3.36;0.67;10.4;5 +9;0.36;0.52;2.1;0.111;5;10;0.99568;3.31;0.62;11.3;6 +9.5;0.37;0.52;2;0.088;12;51;0.99613;3.29;0.58;11.1;6 +6.4;0.57;0.12;2.3;0.12;25;36;0.99519;3.47;0.71;11.3;7 +8;0.59;0.05;2;0.089;12;32;0.99735;3.36;0.61;10;5 +8.5;0.47;0.27;1.9;0.058;18;38;0.99518;3.16;0.85;11.1;6 +7.1;0.56;0.14;1.6;0.078;7;18;0.99592;3.27;0.62;9.3;5 +6.6;0.57;0.02;2.1;0.115;6;16;0.99654;3.38;0.69;9.5;5 +8.8;0.27;0.39;2;0.1;20;27;0.99546;3.15;0.69;11.2;6 +8.5;0.47;0.27;1.9;0.058;18;38;0.99518;3.16;0.85;11.1;6 +8.3;0.34;0.4;2.4;0.065;24;48;0.99554;3.34;0.86;11;6 +9;0.38;0.41;2.4;0.103;6;10;0.99604;3.13;0.58;11.9;7 +8.5;0.66;0.2;2.1;0.097;23;113;0.99733;3.13;0.48;9.2;5 +9;0.4;0.43;2.4;0.068;29;46;0.9943;3.2;0.6;12.2;6 +6.7;0.56;0.09;2.9;0.079;7;22;0.99669;3.46;0.61;10.2;5 +10.4;0.26;0.48;1.9;0.066;6;10;0.99724;3.33;0.87;10.9;6 +10.4;0.26;0.48;1.9;0.066;6;10;0.99724;3.33;0.87;10.9;6 +10.1;0.38;0.5;2.4;0.104;6;13;0.99643;3.22;0.65;11.6;7 +8.5;0.34;0.44;1.7;0.079;6;12;0.99605;3.52;0.63;10.7;5 +8.8;0.33;0.41;5.9;0.073;7;13;0.99658;3.3;0.62;12.1;7 +7.2;0.41;0.3;2.1;0.083;35;72;0.997;3.44;0.52;9.4;5 +7.2;0.41;0.3;2.1;0.083;35;72;0.997;3.44;0.52;9.4;5 +8.4;0.59;0.29;2.6;0.109;31;119;0.99801;3.15;0.5;9.1;5 +7;0.4;0.32;3.6;0.061;9;29;0.99416;3.28;0.49;11.3;7 +12.2;0.45;0.49;1.4;0.075;3;6;0.9969;3.13;0.63;10.4;5 +9.1;0.5;0.3;1.9;0.065;8;17;0.99774;3.32;0.71;10.5;6 +9.5;0.86;0.26;1.9;0.079;13;28;0.99712;3.25;0.62;10;5 +7.3;0.52;0.32;2.1;0.07;51;70;0.99418;3.34;0.82;12.9;6 +9.1;0.5;0.3;1.9;0.065;8;17;0.99774;3.32;0.71;10.5;6 +12.2;0.45;0.49;1.4;0.075;3;6;0.9969;3.13;0.63;10.4;5 +7.4;0.58;0;2;0.064;7;11;0.99562;3.45;0.58;11.3;6 +9.8;0.34;0.39;1.4;0.066;3;7;0.9947;3.19;0.55;11.4;7 +7.1;0.36;0.3;1.6;0.08;35;70;0.99693;3.44;0.5;9.4;5 +7.7;0.39;0.12;1.7;0.097;19;27;0.99596;3.16;0.49;9.4;5 +9.7;0.295;0.4;1.5;0.073;14;21;0.99556;3.14;0.51;10.9;6 +7.7;0.39;0.12;1.7;0.097;19;27;0.99596;3.16;0.49;9.4;5 +7.1;0.34;0.28;2;0.082;31;68;0.99694;3.45;0.48;9.4;5 +6.5;0.4;0.1;2;0.076;30;47;0.99554;3.36;0.48;9.4;6 +7.1;0.34;0.28;2;0.082;31;68;0.99694;3.45;0.48;9.4;5 +10;0.35;0.45;2.5;0.092;20;88;0.99918;3.15;0.43;9.4;5 +7.7;0.6;0.06;2;0.079;19;41;0.99697;3.39;0.62;10.1;6 +5.6;0.66;0;2.2;0.087;3;11;0.99378;3.71;0.63;12.8;7 +5.6;0.66;0;2.2;0.087;3;11;0.99378;3.71;0.63;12.8;7 +8.9;0.84;0.34;1.4;0.05;4;10;0.99554;3.12;0.48;9.1;6 +6.4;0.69;0;1.65;0.055;7;12;0.99162;3.47;0.53;12.9;6 +7.5;0.43;0.3;2.2;0.062;6;12;0.99495;3.44;0.72;11.5;7 +9.9;0.35;0.38;1.5;0.058;31;47;0.99676;3.26;0.82;10.6;7 +9.1;0.29;0.33;2.05;0.063;13;27;0.99516;3.26;0.84;11.7;7 +6.8;0.36;0.32;1.8;0.067;4;8;0.9928;3.36;0.55;12.8;7 +8.2;0.43;0.29;1.6;0.081;27;45;0.99603;3.25;0.54;10.3;5 +6.8;0.36;0.32;1.8;0.067;4;8;0.9928;3.36;0.55;12.8;7 +9.1;0.29;0.33;2.05;0.063;13;27;0.99516;3.26;0.84;11.7;7 +9.1;0.3;0.34;2;0.064;12;25;0.99516;3.26;0.84;11.7;7 +8.9;0.35;0.4;3.6;0.11;12;24;0.99549;3.23;0.7;12;7 +9.6;0.5;0.36;2.8;0.116;26;55;0.99722;3.18;0.68;10.9;5 +8.9;0.28;0.45;1.7;0.067;7;12;0.99354;3.25;0.55;12.3;7 +8.9;0.32;0.31;2;0.088;12;19;0.9957;3.17;0.55;10.4;6 +7.7;1.005;0.15;2.1;0.102;11;32;0.99604;3.23;0.48;10;5 +7.5;0.71;0;1.6;0.092;22;31;0.99635;3.38;0.58;10;6 +8;0.58;0.16;2;0.12;3;7;0.99454;3.22;0.58;11.2;6 +10.5;0.39;0.46;2.2;0.075;14;27;0.99598;3.06;0.84;11.4;6 +8.9;0.38;0.4;2.2;0.068;12;28;0.99486;3.27;0.75;12.6;7 +8;0.18;0.37;0.9;0.049;36;109;0.99007;2.89;0.44;12.7;6 +8;0.18;0.37;0.9;0.049;36;109;0.99007;2.89;0.44;12.7;6 +7;0.5;0.14;1.8;0.078;10;23;0.99636;3.53;0.61;10.4;5 +11.3;0.36;0.66;2.4;0.123;3;8;0.99642;3.2;0.53;11.9;6 +11.3;0.36;0.66;2.4;0.123;3;8;0.99642;3.2;0.53;11.9;6 +7;0.51;0.09;2.1;0.062;4;9;0.99584;3.35;0.54;10.5;5 +8.2;0.32;0.42;2.3;0.098;3;9;0.99506;3.27;0.55;12.3;6 +7.7;0.58;0.01;1.8;0.088;12;18;0.99568;3.32;0.56;10.5;7 +8.6;0.83;0;2.8;0.095;17;43;0.99822;3.33;0.6;10.4;6 +7.9;0.31;0.32;1.9;0.066;14;36;0.99364;3.41;0.56;12.6;6 +6.4;0.795;0;2.2;0.065;28;52;0.99378;3.49;0.52;11.6;5 +7.2;0.34;0.21;2.5;0.075;41;68;0.99586;3.37;0.54;10.1;6 +7.7;0.58;0.01;1.8;0.088;12;18;0.99568;3.32;0.56;10.5;7 +7.1;0.59;0;2.1;0.091;9;14;0.99488;3.42;0.55;11.5;7 +7.3;0.55;0.01;1.8;0.093;9;15;0.99514;3.35;0.58;11;7 +8.1;0.82;0;4.1;0.095;5;14;0.99854;3.36;0.53;9.6;5 +7.5;0.57;0.08;2.6;0.089;14;27;0.99592;3.3;0.59;10.4;6 +8.9;0.745;0.18;2.5;0.077;15;48;0.99739;3.2;0.47;9.7;6 +10.1;0.37;0.34;2.4;0.085;5;17;0.99683;3.17;0.65;10.6;7 +7.6;0.31;0.34;2.5;0.082;26;35;0.99356;3.22;0.59;12.5;7 +7.3;0.91;0.1;1.8;0.074;20;56;0.99672;3.35;0.56;9.2;5 +8.7;0.41;0.41;6.2;0.078;25;42;0.9953;3.24;0.77;12.6;7 +8.9;0.5;0.21;2.2;0.088;21;39;0.99692;3.33;0.83;11.1;6 +7.4;0.965;0;2.2;0.088;16;32;0.99756;3.58;0.67;10.2;5 +6.9;0.49;0.19;1.7;0.079;13;26;0.99547;3.38;0.64;9.8;6 +8.9;0.5;0.21;2.2;0.088;21;39;0.99692;3.33;0.83;11.1;6 +9.5;0.39;0.41;8.9;0.069;18;39;0.99859;3.29;0.81;10.9;7 +6.4;0.39;0.33;3.3;0.046;12;53;0.99294;3.36;0.62;12.2;6 +6.9;0.44;0;1.4;0.07;32;38;0.99438;3.32;0.58;11.4;6 +7.6;0.78;0;1.7;0.076;33;45;0.99612;3.31;0.62;10.7;6 +7.1;0.43;0.17;1.8;0.082;27;51;0.99634;3.49;0.64;10.4;5 +9.3;0.49;0.36;1.7;0.081;3;14;0.99702;3.27;0.78;10.9;6 +9.3;0.5;0.36;1.8;0.084;6;17;0.99704;3.27;0.77;10.8;6 +7.1;0.43;0.17;1.8;0.082;27;51;0.99634;3.49;0.64;10.4;5 +8.5;0.46;0.59;1.4;0.414;16;45;0.99702;3.03;1.34;9.2;5 +5.6;0.605;0.05;2.4;0.073;19;25;0.99258;3.56;0.55;12.9;5 +8.3;0.33;0.42;2.3;0.07;9;20;0.99426;3.38;0.77;12.7;7 +8.2;0.64;0.27;2;0.095;5;77;0.99747;3.13;0.62;9.1;6 +8.2;0.64;0.27;2;0.095;5;77;0.99747;3.13;0.62;9.1;6 +8.9;0.48;0.53;4;0.101;3;10;0.99586;3.21;0.59;12.1;7 +7.6;0.42;0.25;3.9;0.104;28;90;0.99784;3.15;0.57;9.1;5 +9.9;0.53;0.57;2.4;0.093;30;52;0.9971;3.19;0.76;11.6;7 +8.9;0.48;0.53;4;0.101;3;10;0.99586;3.21;0.59;12.1;7 +11.6;0.23;0.57;1.8;0.074;3;8;0.9981;3.14;0.7;9.9;6 +9.1;0.4;0.5;1.8;0.071;7;16;0.99462;3.21;0.69;12.5;8 +8;0.38;0.44;1.9;0.098;6;15;0.9956;3.3;0.64;11.4;6 +10.2;0.29;0.65;2.4;0.075;6;17;0.99565;3.22;0.63;11.8;6 +8.2;0.74;0.09;2;0.067;5;10;0.99418;3.28;0.57;11.8;6 +7.7;0.61;0.18;2.4;0.083;6;20;0.9963;3.29;0.6;10.2;6 +6.6;0.52;0.08;2.4;0.07;13;26;0.99358;3.4;0.72;12.5;7 +11.1;0.31;0.53;2.2;0.06;3;10;0.99572;3.02;0.83;10.9;7 +11.1;0.31;0.53;2.2;0.06;3;10;0.99572;3.02;0.83;10.9;7 +8;0.62;0.35;2.8;0.086;28;52;0.997;3.31;0.62;10.8;5 +9.3;0.33;0.45;1.5;0.057;19;37;0.99498;3.18;0.89;11.1;7 +7.5;0.77;0.2;8.1;0.098;30;92;0.99892;3.2;0.58;9.2;5 +7.2;0.35;0.26;1.8;0.083;33;75;0.9968;3.4;0.58;9.5;6 +8;0.62;0.33;2.7;0.088;16;37;0.9972;3.31;0.58;10.7;6 +7.5;0.77;0.2;8.1;0.098;30;92;0.99892;3.2;0.58;9.2;5 +9.1;0.25;0.34;2;0.071;45;67;0.99769;3.44;0.86;10.2;7 +9.9;0.32;0.56;2;0.073;3;8;0.99534;3.15;0.73;11.4;6 +8.6;0.37;0.65;6.4;0.08;3;8;0.99817;3.27;0.58;11;5 +8.6;0.37;0.65;6.4;0.08;3;8;0.99817;3.27;0.58;11;5 +7.9;0.3;0.68;8.3;0.05;37.5;278;0.99316;3.01;0.51;12.3;7 +10.3;0.27;0.56;1.4;0.047;3;8;0.99471;3.16;0.51;11.8;6 +7.9;0.3;0.68;8.3;0.05;37.5;289;0.99316;3.01;0.51;12.3;7 +7.2;0.38;0.3;1.8;0.073;31;70;0.99685;3.42;0.59;9.5;6 +8.7;0.42;0.45;2.4;0.072;32;59;0.99617;3.33;0.77;12;6 +7.2;0.38;0.3;1.8;0.073;31;70;0.99685;3.42;0.59;9.5;6 +6.8;0.48;0.08;1.8;0.074;40;64;0.99529;3.12;0.49;9.6;5 +8.5;0.34;0.4;4.7;0.055;3;9;0.99738;3.38;0.66;11.6;7 +7.9;0.19;0.42;1.6;0.057;18;30;0.994;3.29;0.69;11.2;6 +11.6;0.41;0.54;1.5;0.095;22;41;0.99735;3.02;0.76;9.9;7 +11.6;0.41;0.54;1.5;0.095;22;41;0.99735;3.02;0.76;9.9;7 +10;0.26;0.54;1.9;0.083;42;74;0.99451;2.98;0.63;11.8;8 +7.9;0.34;0.42;2;0.086;8;19;0.99546;3.35;0.6;11.4;6 +7;0.54;0.09;2;0.081;10;16;0.99479;3.43;0.59;11.5;6 +9.2;0.31;0.36;2.2;0.079;11;31;0.99615;3.33;0.86;12;7 +6.6;0.725;0.09;5.5;0.117;9;17;0.99655;3.35;0.49;10.8;6 +9.4;0.4;0.47;2.5;0.087;6;20;0.99772;3.15;0.5;10.5;5 +6.6;0.725;0.09;5.5;0.117;9;17;0.99655;3.35;0.49;10.8;6 +8.6;0.52;0.38;1.5;0.096;5;18;0.99666;3.2;0.52;9.4;5 +8;0.31;0.45;2.1;0.216;5;16;0.99358;3.15;0.81;12.5;7 +8.6;0.52;0.38;1.5;0.096;5;18;0.99666;3.2;0.52;9.4;5 +8.4;0.34;0.42;2.1;0.072;23;36;0.99392;3.11;0.78;12.4;6 +7.4;0.49;0.27;2.1;0.071;14;25;0.99388;3.35;0.63;12;6 +6.1;0.48;0.09;1.7;0.078;18;30;0.99402;3.45;0.54;11.2;6 +7.4;0.49;0.27;2.1;0.071;14;25;0.99388;3.35;0.63;12;6 +8;0.48;0.34;2.2;0.073;16;25;0.9936;3.28;0.66;12.4;6 +6.3;0.57;0.28;2.1;0.048;13;49;0.99374;3.41;0.6;12.8;5 +8.2;0.23;0.42;1.9;0.069;9;17;0.99376;3.21;0.54;12.3;6 +9.1;0.3;0.41;2;0.068;10;24;0.99523;3.27;0.85;11.7;7 +8.1;0.78;0.1;3.3;0.09;4;13;0.99855;3.36;0.49;9.5;5 +10.8;0.47;0.43;2.1;0.171;27;66;0.9982;3.17;0.76;10.8;6 +8.3;0.53;0;1.4;0.07;6;14;0.99593;3.25;0.64;10;6 +5.4;0.42;0.27;2;0.092;23;55;0.99471;3.78;0.64;12.3;7 +7.9;0.33;0.41;1.5;0.056;6;35;0.99396;3.29;0.71;11;6 +8.9;0.24;0.39;1.6;0.074;3;10;0.99698;3.12;0.59;9.5;6 +5;0.4;0.5;4.3;0.046;29;80;0.9902;3.49;0.66;13.6;6 +7;0.69;0.07;2.5;0.091;15;21;0.99572;3.38;0.6;11.3;6 +7;0.69;0.07;2.5;0.091;15;21;0.99572;3.38;0.6;11.3;6 +7;0.69;0.07;2.5;0.091;15;21;0.99572;3.38;0.6;11.3;6 +7.1;0.39;0.12;2.1;0.065;14;24;0.99252;3.3;0.53;13.3;6 +5.6;0.66;0;2.5;0.066;7;15;0.99256;3.52;0.58;12.9;5 +7.9;0.54;0.34;2.5;0.076;8;17;0.99235;3.2;0.72;13.1;8 +6.6;0.5;0;1.8;0.062;21;28;0.99352;3.44;0.55;12.3;6 +6.3;0.47;0;1.4;0.055;27;33;0.9922;3.45;0.48;12.3;6 +10.7;0.4;0.37;1.9;0.081;17;29;0.99674;3.12;0.65;11.2;6 +6.5;0.58;0;2.2;0.096;3;13;0.99557;3.62;0.62;11.5;4 +8.8;0.24;0.35;1.7;0.055;13;27;0.99394;3.14;0.59;11.3;7 +5.8;0.29;0.26;1.7;0.063;3;11;0.9915;3.39;0.54;13.5;6 +6.3;0.76;0;2.9;0.072;26;52;0.99379;3.51;0.6;11.5;6 +10;0.43;0.33;2.7;0.095;28;89;0.9984;3.22;0.68;10;5 +10.5;0.43;0.35;3.3;0.092;24;70;0.99798;3.21;0.69;10.5;6 +9.1;0.6;0;1.9;0.058;5;10;0.9977;3.18;0.63;10.4;6 +5.9;0.19;0.21;1.7;0.045;57;135;0.99341;3.32;0.44;9.5;5 +7.4;0.36;0.34;1.8;0.075;18;38;0.9933;3.38;0.88;13.6;7 +7.2;0.48;0.07;5.5;0.089;10;18;0.99684;3.37;0.68;11.2;7 +8.5;0.28;0.35;1.7;0.061;6;15;0.99524;3.3;0.74;11.8;7 +8;0.25;0.43;1.7;0.067;22;50;0.9946;3.38;0.6;11.9;6 +10.4;0.52;0.45;2;0.08;6;13;0.99774;3.22;0.76;11.4;6 +10.4;0.52;0.45;2;0.08;6;13;0.99774;3.22;0.76;11.4;6 +7.5;0.41;0.15;3.7;0.104;29;94;0.99786;3.14;0.58;9.1;5 +8.2;0.51;0.24;2;0.079;16;86;0.99764;3.34;0.64;9.5;6 +7.3;0.4;0.3;1.7;0.08;33;79;0.9969;3.41;0.65;9.5;6 +8.2;0.38;0.32;2.5;0.08;24;71;0.99624;3.27;0.85;11;6 +6.9;0.45;0.11;2.4;0.043;6;12;0.99354;3.3;0.65;11.4;6 +7;0.22;0.3;1.8;0.065;16;20;0.99672;3.61;0.82;10;6 +7.3;0.32;0.23;2.3;0.066;35;70;0.99588;3.43;0.62;10.1;5 +8.2;0.2;0.43;2.5;0.076;31;51;0.99672;3.53;0.81;10.4;6 +7.8;0.5;0.12;1.8;0.178;6;21;0.996;3.28;0.87;9.8;6 +10;0.41;0.45;6.2;0.071;6;14;0.99702;3.21;0.49;11.8;7 +7.8;0.39;0.42;2;0.086;9;21;0.99526;3.39;0.66;11.6;6 +10;0.35;0.47;2;0.061;6;11;0.99585;3.23;0.52;12;6 +8.2;0.33;0.32;2.8;0.067;4;12;0.99473;3.3;0.76;12.8;7 +6.1;0.58;0.23;2.5;0.044;16;70;0.99352;3.46;0.65;12.5;6 +8.3;0.6;0.25;2.2;0.118;9;38;0.99616;3.15;0.53;9.8;5 +9.6;0.42;0.35;2.1;0.083;17;38;0.99622;3.23;0.66;11.1;6 +6.6;0.58;0;2.2;0.1;50;63;0.99544;3.59;0.68;11.4;6 +8.3;0.6;0.25;2.2;0.118;9;38;0.99616;3.15;0.53;9.8;5 +8.5;0.18;0.51;1.75;0.071;45;88;0.99524;3.33;0.76;11.8;7 +5.1;0.51;0.18;2.1;0.042;16;101;0.9924;3.46;0.87;12.9;7 +6.7;0.41;0.43;2.8;0.076;22;54;0.99572;3.42;1.16;10.6;6 +10.2;0.41;0.43;2.2;0.11;11;37;0.99728;3.16;0.67;10.8;5 +10.6;0.36;0.57;2.3;0.087;6;20;0.99676;3.14;0.72;11.1;7 +8.8;0.45;0.43;1.4;0.076;12;21;0.99551;3.21;0.75;10.2;6 +8.5;0.32;0.42;2.3;0.075;12;19;0.99434;3.14;0.71;11.8;7 +9;0.785;0.24;1.7;0.078;10;21;0.99692;3.29;0.67;10;5 +9;0.785;0.24;1.7;0.078;10;21;0.99692;3.29;0.67;10;5 +8.5;0.44;0.5;1.9;0.369;15;38;0.99634;3.01;1.1;9.4;5 +9.9;0.54;0.26;2;0.111;7;60;0.99709;2.94;0.98;10.2;5 +8.2;0.33;0.39;2.5;0.074;29;48;0.99528;3.32;0.88;12.4;7 +6.5;0.34;0.27;2.8;0.067;8;44;0.99384;3.21;0.56;12;6 +7.6;0.5;0.29;2.3;0.086;5;14;0.99502;3.32;0.62;11.5;6 +9.2;0.36;0.34;1.6;0.062;5;12;0.99667;3.2;0.67;10.5;6 +7.1;0.59;0;2.2;0.078;26;44;0.99522;3.42;0.68;10.8;6 +9.7;0.42;0.46;2.1;0.074;5;16;0.99649;3.27;0.74;12.3;6 +7.6;0.36;0.31;1.7;0.079;26;65;0.99716;3.46;0.62;9.5;6 +7.6;0.36;0.31;1.7;0.079;26;65;0.99716;3.46;0.62;9.5;6 +6.5;0.61;0;2.2;0.095;48;59;0.99541;3.61;0.7;11.5;6 +6.5;0.88;0.03;5.6;0.079;23;47;0.99572;3.58;0.5;11.2;4 +7.1;0.66;0;2.4;0.052;6;11;0.99318;3.35;0.66;12.7;7 +5.6;0.915;0;2.1;0.041;17;78;0.99346;3.68;0.73;11.4;5 +8.2;0.35;0.33;2.4;0.076;11;47;0.99599;3.27;0.81;11;6 +8.2;0.35;0.33;2.4;0.076;11;47;0.99599;3.27;0.81;11;6 +9.8;0.39;0.43;1.65;0.068;5;11;0.99478;3.19;0.46;11.4;5 +10.2;0.4;0.4;2.5;0.068;41;54;0.99754;3.38;0.86;10.5;6 +6.8;0.66;0.07;1.6;0.07;16;61;0.99572;3.29;0.6;9.3;5 +6.7;0.64;0.23;2.1;0.08;11;119;0.99538;3.36;0.7;10.9;5 +7;0.43;0.3;2;0.085;6;39;0.99346;3.33;0.46;11.9;6 +6.6;0.8;0.03;7.8;0.079;6;12;0.9963;3.52;0.5;12.2;5 +7;0.43;0.3;2;0.085;6;39;0.99346;3.33;0.46;11.9;6 +6.7;0.64;0.23;2.1;0.08;11;119;0.99538;3.36;0.7;10.9;5 +8.8;0.955;0.05;1.8;0.075;5;19;0.99616;3.3;0.44;9.6;4 +9.1;0.4;0.57;4.6;0.08;6;20;0.99652;3.28;0.57;12.5;6 +6.5;0.885;0;2.3;0.166;6;12;0.99551;3.56;0.51;10.8;5 +7.2;0.25;0.37;2.5;0.063;11;41;0.99439;3.52;0.8;12.4;7 +6.4;0.885;0;2.3;0.166;6;12;0.99551;3.56;0.51;10.8;5 +7;0.745;0.12;1.8;0.114;15;64;0.99588;3.22;0.59;9.5;6 +6.2;0.43;0.22;1.8;0.078;21;56;0.99633;3.52;0.6;9.5;6 +7.9;0.58;0.23;2.3;0.076;23;94;0.99686;3.21;0.58;9.5;6 +7.7;0.57;0.21;1.5;0.069;4;9;0.99458;3.16;0.54;9.8;6 +7.7;0.26;0.26;2;0.052;19;77;0.9951;3.15;0.79;10.9;6 +7.9;0.58;0.23;2.3;0.076;23;94;0.99686;3.21;0.58;9.5;6 +7.7;0.57;0.21;1.5;0.069;4;9;0.99458;3.16;0.54;9.8;6 +7.9;0.34;0.36;1.9;0.065;5;10;0.99419;3.27;0.54;11.2;7 +8.6;0.42;0.39;1.8;0.068;6;12;0.99516;3.35;0.69;11.7;8 +9.9;0.74;0.19;5.8;0.111;33;76;0.99878;3.14;0.55;9.4;5 +7.2;0.36;0.46;2.1;0.074;24;44;0.99534;3.4;0.85;11;7 +7.2;0.36;0.46;2.1;0.074;24;44;0.99534;3.4;0.85;11;7 +7.2;0.36;0.46;2.1;0.074;24;44;0.99534;3.4;0.85;11;7 +9.9;0.72;0.55;1.7;0.136;24;52;0.99752;3.35;0.94;10;5 +7.2;0.36;0.46;2.1;0.074;24;44;0.99534;3.4;0.85;11;7 +6.2;0.39;0.43;2;0.071;14;24;0.99428;3.45;0.87;11.2;7 +6.8;0.65;0.02;2.1;0.078;8;15;0.99498;3.35;0.62;10.4;6 +6.6;0.44;0.15;2.1;0.076;22;53;0.9957;3.32;0.62;9.3;5 +6.8;0.65;0.02;2.1;0.078;8;15;0.99498;3.35;0.62;10.4;6 +9.6;0.38;0.42;1.9;0.071;5;13;0.99659;3.15;0.75;10.5;6 +10.2;0.33;0.46;1.9;0.081;6;9;0.99628;3.1;0.48;10.4;6 +8.8;0.27;0.46;2.1;0.095;20;29;0.99488;3.26;0.56;11.3;6 +7.9;0.57;0.31;2;0.079;10;79;0.99677;3.29;0.69;9.5;6 +8.2;0.34;0.37;1.9;0.057;43;74;0.99408;3.23;0.81;12;6 +8.2;0.4;0.31;1.9;0.082;8;24;0.996;3.24;0.69;10.6;6 +9;0.39;0.4;1.3;0.044;25;50;0.99478;3.2;0.83;10.9;6 +10.9;0.32;0.52;1.8;0.132;17;44;0.99734;3.28;0.77;11.5;6 +10.9;0.32;0.52;1.8;0.132;17;44;0.99734;3.28;0.77;11.5;6 +8.1;0.53;0.22;2.2;0.078;33;89;0.99678;3.26;0.46;9.6;6 +10.5;0.36;0.47;2.2;0.074;9;23;0.99638;3.23;0.76;12;6 +12.6;0.39;0.49;2.5;0.08;8;20;0.9992;3.07;0.82;10.3;6 +9.2;0.46;0.23;2.6;0.091;18;77;0.99922;3.15;0.51;9.4;5 +7.5;0.58;0.03;4.1;0.08;27;46;0.99592;3.02;0.47;9.2;5 +9;0.58;0.25;2;0.104;8;21;0.99769;3.27;0.72;9.6;5 +5.1;0.42;0;1.8;0.044;18;88;0.99157;3.68;0.73;13.6;7 +7.6;0.43;0.29;2.1;0.075;19;66;0.99718;3.4;0.64;9.5;5 +7.7;0.18;0.34;2.7;0.066;15;58;0.9947;3.37;0.78;11.8;6 +7.8;0.815;0.01;2.6;0.074;48;90;0.99621;3.38;0.62;10.8;5 +7.6;0.43;0.29;2.1;0.075;19;66;0.99718;3.4;0.64;9.5;5 +10.2;0.23;0.37;2.2;0.057;14;36;0.99614;3.23;0.49;9.3;4 +7.1;0.75;0.01;2.2;0.059;11;18;0.99242;3.39;0.4;12.8;6 +6;0.33;0.32;12.9;0.054;6;113;0.99572;3.3;0.56;11.5;4 +7.8;0.55;0;1.7;0.07;7;17;0.99659;3.26;0.64;9.4;6 +7.1;0.75;0.01;2.2;0.059;11;18;0.99242;3.39;0.4;12.8;6 +8.1;0.73;0;2.5;0.081;12;24;0.99798;3.38;0.46;9.6;4 +6.5;0.67;0;4.3;0.057;11;20;0.99488;3.45;0.56;11.8;4 +7.5;0.61;0.2;1.7;0.076;36;60;0.99494;3.1;0.4;9.3;5 +9.8;0.37;0.39;2.5;0.079;28;65;0.99729;3.16;0.59;9.8;5 +9;0.4;0.41;2;0.058;15;40;0.99414;3.22;0.6;12.2;6 +8.3;0.56;0.22;2.4;0.082;10;86;0.9983;3.37;0.62;9.5;5 +5.9;0.29;0.25;13.4;0.067;72;160;0.99721;3.33;0.54;10.3;6 +7.4;0.55;0.19;1.8;0.082;15;34;0.99655;3.49;0.68;10.5;5 +7.4;0.74;0.07;1.7;0.086;15;48;0.99502;3.12;0.48;10;5 +7.4;0.55;0.19;1.8;0.082;15;34;0.99655;3.49;0.68;10.5;5 +6.9;0.41;0.33;2.2;0.081;22;36;0.9949;3.41;0.75;11.1;6 +7.1;0.6;0.01;2.3;0.079;24;37;0.99514;3.4;0.61;10.9;6 +7.1;0.6;0.01;2.3;0.079;24;37;0.99514;3.4;0.61;10.9;6 +7.5;0.58;0.14;2.2;0.077;27;60;0.9963;3.28;0.59;9.8;5 +7.1;0.72;0;1.8;0.123;6;14;0.99627;3.45;0.58;9.8;5 +7.9;0.66;0;1.4;0.096;6;13;0.99569;3.43;0.58;9.5;5 +7.8;0.7;0.06;1.9;0.079;20;35;0.99628;3.4;0.69;10.9;5 +6.1;0.64;0.02;2.4;0.069;26;46;0.99358;3.47;0.45;11;5 +7.5;0.59;0.22;1.8;0.082;43;60;0.99499;3.1;0.42;9.2;5 +7;0.58;0.28;4.8;0.085;12;69;0.99633;3.32;0.7;11;6 +6.8;0.64;0;2.7;0.123;15;33;0.99538;3.44;0.63;11.3;6 +6.8;0.64;0;2.7;0.123;15;33;0.99538;3.44;0.63;11.3;6 +8.6;0.635;0.68;1.8;0.403;19;56;0.99632;3.02;1.15;9.3;5 +6.3;1.02;0;2;0.083;17;24;0.99437;3.59;0.55;11.2;4 +9.8;0.45;0.38;2.5;0.081;34;66;0.99726;3.15;0.58;9.8;5 +8.2;0.78;0;2.2;0.089;13;26;0.9978;3.37;0.46;9.6;4 +8.5;0.37;0.32;1.8;0.066;26;51;0.99456;3.38;0.72;11.8;6 +7.2;0.57;0.05;2.3;0.081;16;36;0.99564;3.38;0.6;10.3;6 +7.2;0.57;0.05;2.3;0.081;16;36;0.99564;3.38;0.6;10.3;6 +10.4;0.43;0.5;2.3;0.068;13;19;0.996;3.1;0.87;11.4;6 +6.9;0.41;0.31;2;0.079;21;51;0.99668;3.47;0.55;9.5;6 +5.5;0.49;0.03;1.8;0.044;28;87;0.9908;3.5;0.82;14;8 +5;0.38;0.01;1.6;0.048;26;60;0.99084;3.7;0.75;14;6 +7.3;0.44;0.2;1.6;0.049;24;64;0.9935;3.38;0.57;11.7;6 +5.9;0.46;0;1.9;0.077;25;44;0.99385;3.5;0.53;11.2;5 +7.5;0.58;0.2;2;0.073;34;44;0.99494;3.1;0.43;9.3;5 +7.8;0.58;0.13;2.1;0.102;17;36;0.9944;3.24;0.53;11.2;6 +8;0.715;0.22;2.3;0.075;13;81;0.99688;3.24;0.54;9.5;6 +8.5;0.4;0.4;6.3;0.05;3;10;0.99566;3.28;0.56;12;4 +7;0.69;0;1.9;0.114;3;10;0.99636;3.35;0.6;9.7;6 +8;0.715;0.22;2.3;0.075;13;81;0.99688;3.24;0.54;9.5;6 +9.8;0.3;0.39;1.7;0.062;3;9;0.9948;3.14;0.57;11.5;7 +7.1;0.46;0.2;1.9;0.077;28;54;0.9956;3.37;0.64;10.4;6 +7.1;0.46;0.2;1.9;0.077;28;54;0.9956;3.37;0.64;10.4;6 +7.9;0.765;0;2;0.084;9;22;0.99619;3.33;0.68;10.9;6 +8.7;0.63;0.28;2.7;0.096;17;69;0.99734;3.26;0.63;10.2;6 +7;0.42;0.19;2.3;0.071;18;36;0.99476;3.39;0.56;10.9;5 +11.3;0.37;0.5;1.8;0.09;20;47;0.99734;3.15;0.57;10.5;5 +7.1;0.16;0.44;2.5;0.068;17;31;0.99328;3.35;0.54;12.4;6 +8;0.6;0.08;2.6;0.056;3;7;0.99286;3.22;0.37;13;5 +7;0.6;0.3;4.5;0.068;20;110;0.99914;3.3;1.17;10.2;5 +7;0.6;0.3;4.5;0.068;20;110;0.99914;3.3;1.17;10.2;5 +7.6;0.74;0;1.9;0.1;6;12;0.99521;3.36;0.59;11;5 +8.2;0.635;0.1;2.1;0.073;25;60;0.99638;3.29;0.75;10.9;6 +5.9;0.395;0.13;2.4;0.056;14;28;0.99362;3.62;0.67;12.4;6 +7.5;0.755;0;1.9;0.084;6;12;0.99672;3.34;0.49;9.7;4 +8.2;0.635;0.1;2.1;0.073;25;60;0.99638;3.29;0.75;10.9;6 +6.6;0.63;0;4.3;0.093;51;77.5;0.99558;3.2;0.45;9.5;5 +6.6;0.63;0;4.3;0.093;51;77.5;0.99558;3.2;0.45;9.5;5 +7.2;0.53;0.14;2.1;0.064;15;29;0.99323;3.35;0.61;12.1;6 +5.7;0.6;0;1.4;0.063;11;18;0.99191;3.45;0.56;12.2;6 +7.6;1.58;0;2.1;0.137;5;9;0.99476;3.5;0.4;10.9;3 +5.2;0.645;0;2.15;0.08;15;28;0.99444;3.78;0.61;12.5;6 +6.7;0.86;0.07;2;0.1;20;57;0.99598;3.6;0.74;11.7;6 +9.1;0.37;0.32;2.1;0.064;4;15;0.99576;3.3;0.8;11.2;6 +8;0.28;0.44;1.8;0.081;28;68;0.99501;3.36;0.66;11.2;5 +7.6;0.79;0.21;2.3;0.087;21;68;0.9955;3.12;0.44;9.2;5 +7.5;0.61;0.26;1.9;0.073;24;88;0.99612;3.3;0.53;9.8;5 +9.7;0.69;0.32;2.5;0.088;22;91;0.9979;3.29;0.62;10.1;5 +6.8;0.68;0.09;3.9;0.068;15;29;0.99524;3.41;0.52;11.1;4 +9.7;0.69;0.32;2.5;0.088;22;91;0.9979;3.29;0.62;10.1;5 +7;0.62;0.1;1.4;0.071;27;63;0.996;3.28;0.61;9.2;5 +7.5;0.61;0.26;1.9;0.073;24;88;0.99612;3.3;0.53;9.8;5 +6.5;0.51;0.15;3;0.064;12;27;0.9929;3.33;0.59;12.8;6 +8;1.18;0.21;1.9;0.083;14;41;0.99532;3.34;0.47;10.5;5 +7;0.36;0.21;2.3;0.086;20;65;0.99558;3.4;0.54;10.1;6 +7;0.36;0.21;2.4;0.086;24;69;0.99556;3.4;0.53;10.1;6 +7.5;0.63;0.27;2;0.083;17;91;0.99616;3.26;0.58;9.8;6 +5.4;0.74;0;1.2;0.041;16;46;0.99258;4.01;0.59;12.5;6 +9.9;0.44;0.46;2.2;0.091;10;41;0.99638;3.18;0.69;11.9;6 +7.5;0.63;0.27;2;0.083;17;91;0.99616;3.26;0.58;9.8;6 +9.1;0.76;0.68;1.7;0.414;18;64;0.99652;2.9;1.33;9.1;6 +9.7;0.66;0.34;2.6;0.094;12;88;0.99796;3.26;0.66;10.1;5 +5;0.74;0;1.2;0.041;16;46;0.99258;4.01;0.59;12.5;6 +9.1;0.34;0.42;1.8;0.058;9;18;0.99392;3.18;0.55;11.4;5 +9.1;0.36;0.39;1.8;0.06;21;55;0.99495;3.18;0.82;11;7 +6.7;0.46;0.24;1.7;0.077;18;34;0.9948;3.39;0.6;10.6;6 +6.7;0.46;0.24;1.7;0.077;18;34;0.9948;3.39;0.6;10.6;6 +6.7;0.46;0.24;1.7;0.077;18;34;0.9948;3.39;0.6;10.6;6 +6.7;0.46;0.24;1.7;0.077;18;34;0.9948;3.39;0.6;10.6;6 +6.5;0.52;0.11;1.8;0.073;13;38;0.9955;3.34;0.52;9.3;5 +7.4;0.6;0.26;2.1;0.083;17;91;0.99616;3.29;0.56;9.8;6 +7.4;0.6;0.26;2.1;0.083;17;91;0.99616;3.29;0.56;9.8;6 +7.8;0.87;0.26;3.8;0.107;31;67;0.99668;3.26;0.46;9.2;5 +8.4;0.39;0.1;1.7;0.075;6;25;0.99581;3.09;0.43;9.7;6 +9.1;0.775;0.22;2.2;0.079;12;48;0.9976;3.18;0.51;9.6;5 +7.2;0.835;0;2;0.166;4;11;0.99608;3.39;0.52;10;5 +6.6;0.58;0.02;2.4;0.069;19;40;0.99387;3.38;0.66;12.6;6 +6;0.5;0;1.4;0.057;15;26;0.99448;3.36;0.45;9.5;5 +6;0.5;0;1.4;0.057;15;26;0.99448;3.36;0.45;9.5;5 +6;0.5;0;1.4;0.057;15;26;0.99448;3.36;0.45;9.5;5 +7.5;0.51;0.02;1.7;0.084;13;31;0.99538;3.36;0.54;10.5;6 +7.5;0.51;0.02;1.7;0.084;13;31;0.99538;3.36;0.54;10.5;6 +7.5;0.51;0.02;1.7;0.084;13;31;0.99538;3.36;0.54;10.5;6 +7.6;0.54;0.02;1.7;0.085;17;31;0.99589;3.37;0.51;10.4;6 +7.5;0.51;0.02;1.7;0.084;13;31;0.99538;3.36;0.54;10.5;6 +11.5;0.42;0.48;2.6;0.077;8;20;0.99852;3.09;0.53;11;5 +8.2;0.44;0.24;2.3;0.063;10;28;0.99613;3.25;0.53;10.2;6 +6.1;0.59;0.01;2.1;0.056;5;13;0.99472;3.52;0.56;11.4;5 +7.2;0.655;0.03;1.8;0.078;7;12;0.99587;3.34;0.39;9.5;5 +7.2;0.655;0.03;1.8;0.078;7;12;0.99587;3.34;0.39;9.5;5 +6.9;0.57;0;2.8;0.081;21;41;0.99518;3.41;0.52;10.8;5 +9;0.6;0.29;2;0.069;32;73;0.99654;3.34;0.57;10;5 +7.2;0.62;0.01;2.3;0.065;8;46;0.99332;3.32;0.51;11.8;6 +7.6;0.645;0.03;1.9;0.086;14;57;0.9969;3.37;0.46;10.3;5 +7.6;0.645;0.03;1.9;0.086;14;57;0.9969;3.37;0.46;10.3;5 +7.2;0.58;0.03;2.3;0.077;7;28;0.99568;3.35;0.52;10;5 +6.1;0.32;0.25;1.8;0.086;5;32;0.99464;3.36;0.44;10.1;5 +6.1;0.34;0.25;1.8;0.084;4;28;0.99464;3.36;0.44;10.1;5 +7.3;0.43;0.24;2.5;0.078;27;67;0.99648;3.6;0.59;11.1;6 +7.4;0.64;0.17;5.4;0.168;52;98;0.99736;3.28;0.5;9.5;5 +11.6;0.475;0.4;1.4;0.091;6;28;0.99704;3.07;0.65;10.0333333333333;6 +9.2;0.54;0.31;2.3;0.112;11;38;0.99699;3.24;0.56;10.9;5 +8.3;0.85;0.14;2.5;0.093;13;54;0.99724;3.36;0.54;10.1;5 +11.6;0.475;0.4;1.4;0.091;6;28;0.99704;3.07;0.65;10.0333333333333;6 +8;0.83;0.27;2;0.08;11;63;0.99652;3.29;0.48;9.8;4 +7.2;0.605;0.02;1.9;0.096;10;31;0.995;3.46;0.53;11.8;6 +7.8;0.5;0.09;2.2;0.115;10;42;0.9971;3.18;0.62;9.5;5 +7.3;0.74;0.08;1.7;0.094;10;45;0.99576;3.24;0.5;9.8;5 +6.9;0.54;0.3;2.2;0.088;9;105;0.99725;3.25;1.18;10.5;6 +8;0.77;0.32;2.1;0.079;16;74;0.99656;3.27;0.5;9.8;6 +6.6;0.61;0;1.6;0.069;4;8;0.99396;3.33;0.37;10.4;4 +8.7;0.78;0.51;1.7;0.415;12;66;0.99623;3;1.17;9.2;5 +7.5;0.58;0.56;3.1;0.153;5;14;0.99476;3.21;1.03;11.6;6 +8.7;0.78;0.51;1.7;0.415;12;66;0.99623;3;1.17;9.2;5 +7.7;0.75;0.27;3.8;0.11;34;89;0.99664;3.24;0.45;9.3;5 +6.8;0.815;0;1.2;0.267;16;29;0.99471;3.32;0.51;9.8;3 +7.2;0.56;0.26;2;0.083;13;100;0.99586;3.26;0.52;9.9;5 +8.2;0.885;0.2;1.4;0.086;7;31;0.9946;3.11;0.46;10;5 +5.2;0.49;0.26;2.3;0.09;23;74;0.9953;3.71;0.62;12.2;6 +7.2;0.45;0.15;2;0.078;10;28;0.99609;3.29;0.51;9.9;6 +7.5;0.57;0.02;2.6;0.077;11;35;0.99557;3.36;0.62;10.8;6 +7.5;0.57;0.02;2.6;0.077;11;35;0.99557;3.36;0.62;10.8;6 +6.8;0.83;0.09;1.8;0.074;4;25;0.99534;3.38;0.45;9.6;5 +8;0.6;0.22;2.1;0.08;25;105;0.99613;3.3;0.49;9.9;5 +8;0.6;0.22;2.1;0.08;25;105;0.99613;3.3;0.49;9.9;5 +7.1;0.755;0.15;1.8;0.107;20;84;0.99593;3.19;0.5;9.5;5 +8;0.81;0.25;3.4;0.076;34;85;0.99668;3.19;0.42;9.2;5 +7.4;0.64;0.07;1.8;0.1;8;23;0.9961;3.3;0.58;9.6;5 +7.4;0.64;0.07;1.8;0.1;8;23;0.9961;3.3;0.58;9.6;5 +6.6;0.64;0.31;6.1;0.083;7;49;0.99718;3.35;0.68;10.3;5 +6.7;0.48;0.02;2.2;0.08;36;111;0.99524;3.1;0.53;9.7;5 +6;0.49;0;2.3;0.068;15;33;0.99292;3.58;0.59;12.5;6 +8;0.64;0.22;2.4;0.094;5;33;0.99612;3.37;0.58;11;5 +7.1;0.62;0.06;1.3;0.07;5;12;0.9942;3.17;0.48;9.8;5 +8;0.52;0.25;2;0.078;19;59;0.99612;3.3;0.48;10.2;5 +6.4;0.57;0.14;3.9;0.07;27;73;0.99669;3.32;0.48;9.2;5 +8.6;0.685;0.1;1.6;0.092;3;12;0.99745;3.31;0.65;9.55;6 +8.7;0.675;0.1;1.6;0.09;4;11;0.99745;3.31;0.65;9.55;5 +7.3;0.59;0.26;2;0.08;17;104;0.99584;3.28;0.52;9.9;5 +7;0.6;0.12;2.2;0.083;13;28;0.9966;3.52;0.62;10.2;7 +7.2;0.67;0;2.2;0.068;10;24;0.9956;3.42;0.72;11.1;6 +7.9;0.69;0.21;2.1;0.08;33;141;0.9962;3.25;0.51;9.9;5 +7.9;0.69;0.21;2.1;0.08;33;141;0.9962;3.25;0.51;9.9;5 +7.6;0.3;0.42;2;0.052;6;24;0.9963;3.44;0.82;11.9;6 +7.2;0.33;0.33;1.7;0.061;3;13;0.996;3.23;1.1;10;8 +8;0.5;0.39;2.6;0.082;12;46;0.9985;3.43;0.62;10.7;6 +7.7;0.28;0.3;2;0.062;18;34;0.9952;3.28;0.9;11.3;7 +8.2;0.24;0.34;5.1;0.062;8;22;0.9974;3.22;0.94;10.9;6 +6;0.51;0;2.1;0.064;40;54;0.995;3.54;0.93;10.7;6 +8.1;0.29;0.36;2.2;0.048;35;53;0.995;3.27;1.01;12.4;7 +6;0.51;0;2.1;0.064;40;54;0.995;3.54;0.93;10.7;6 +6.6;0.96;0;1.8;0.082;5;16;0.9936;3.5;0.44;11.9;6 +6.4;0.47;0.4;2.4;0.071;8;19;0.9963;3.56;0.73;10.6;6 +8.2;0.24;0.34;5.1;0.062;8;22;0.9974;3.22;0.94;10.9;6 +9.9;0.57;0.25;2;0.104;12;89;0.9963;3.04;0.9;10.1;5 +10;0.32;0.59;2.2;0.077;3;15;0.9994;3.2;0.78;9.6;5 +6.2;0.58;0;1.6;0.065;8;18;0.9966;3.56;0.84;9.4;5 +10;0.32;0.59;2.2;0.077;3;15;0.9994;3.2;0.78;9.6;5 +7.3;0.34;0.33;2.5;0.064;21;37;0.9952;3.35;0.77;12.1;7 +7.8;0.53;0.01;1.6;0.077;3;19;0.995;3.16;0.46;9.8;5 +7.7;0.64;0.21;2.2;0.077;32;133;0.9956;3.27;0.45;9.9;5 +7.8;0.53;0.01;1.6;0.077;3;19;0.995;3.16;0.46;9.8;5 +7.5;0.4;0.18;1.6;0.079;24;58;0.9965;3.34;0.58;9.4;5 +7;0.54;0;2.1;0.079;39;55;0.9956;3.39;0.84;11.4;6 +6.4;0.53;0.09;3.9;0.123;14;31;0.9968;3.5;0.67;11;4 +8.3;0.26;0.37;1.4;0.076;8;23;0.9974;3.26;0.7;9.6;6 +8.3;0.26;0.37;1.4;0.076;8;23;0.9974;3.26;0.7;9.6;6 +7.7;0.23;0.37;1.8;0.046;23;60;0.9971;3.41;0.71;12.1;6 +7.6;0.41;0.33;2.5;0.078;6;23;0.9957;3.3;0.58;11.2;5 +7.8;0.64;0;1.9;0.072;27;55;0.9962;3.31;0.63;11;5 +7.9;0.18;0.4;2.2;0.049;38;67;0.996;3.33;0.93;11.3;5 +7.4;0.41;0.24;1.8;0.066;18;47;0.9956;3.37;0.62;10.4;5 +7.6;0.43;0.31;2.1;0.069;13;74;0.9958;3.26;0.54;9.9;6 +5.9;0.44;0;1.6;0.042;3;11;0.9944;3.48;0.85;11.7;6 +6.1;0.4;0.16;1.8;0.069;11;25;0.9955;3.42;0.74;10.1;7 +10.2;0.54;0.37;15.4;0.214;55;95;1.00369;3.18;0.77;9;6 +10.2;0.54;0.37;15.4;0.214;55;95;1.00369;3.18;0.77;9;6 +10;0.38;0.38;1.6;0.169;27;90;0.99914;3.15;0.65;8.5;5 +6.8;0.915;0.29;4.8;0.07;15;39;0.99577;3.53;0.54;11.1;5 +7;0.59;0;1.7;0.052;3;8;0.996;3.41;0.47;10.3;5 +7.3;0.67;0.02;2.2;0.072;31;92;0.99566;3.32;0.68;11.0666666666667;6 +7.2;0.37;0.32;2;0.062;15;28;0.9947;3.23;0.73;11.3;7 +7.4;0.785;0.19;5.2;0.094;19;98;0.99713;3.16;0.52;9.56666666666667;6 +6.9;0.63;0.02;1.9;0.078;18;30;0.99712;3.4;0.75;9.8;5 +6.9;0.58;0.2;1.75;0.058;8;22;0.99322;3.38;0.49;11.7;5 +7.3;0.67;0.02;2.2;0.072;31;92;0.99566;3.32;0.68;11.1;6 +7.4;0.785;0.19;5.2;0.094;19;98;0.99713;3.16;0.52;9.6;6 +6.9;0.63;0.02;1.9;0.078;18;30;0.99712;3.4;0.75;9.8;5 +6.8;0.67;0;1.9;0.08;22;39;0.99701;3.4;0.74;9.7;5 +6.9;0.58;0.01;1.9;0.08;40;54;0.99683;3.4;0.73;9.7;5 +7.2;0.38;0.31;2;0.056;15;29;0.99472;3.23;0.76;11.3;8 +7.2;0.37;0.32;2;0.062;15;28;0.9947;3.23;0.73;11.3;7 +7.8;0.32;0.44;2.7;0.104;8;17;0.99732;3.33;0.78;11;7 +6.6;0.58;0.02;2;0.062;37;53;0.99374;3.35;0.76;11.6;7 +7.6;0.49;0.33;1.9;0.074;27;85;0.99706;3.41;0.58;9;5 +11.7;0.45;0.63;2.2;0.073;7;23;0.99974;3.21;0.69;10.9;6 +6.5;0.9;0;1.6;0.052;9;17;0.99467;3.5;0.63;10.9;6 +6;0.54;0.06;1.8;0.05;38;89;0.99236;3.3;0.5;10.55;6 +7.6;0.49;0.33;1.9;0.074;27;85;0.99706;3.41;0.58;9;5 +8.4;0.29;0.4;1.7;0.067;8;20;0.99603;3.39;0.6;10.5;5 +7.9;0.2;0.35;1.7;0.054;7;15;0.99458;3.32;0.8;11.9;7 +6.4;0.42;0.09;2.3;0.054;34;64;0.99724;3.41;0.68;10.4;6 +6.2;0.785;0;2.1;0.06;6;13;0.99664;3.59;0.61;10;4 +6.8;0.64;0.03;2.3;0.075;14;31;0.99545;3.36;0.58;10.4;6 +6.9;0.63;0.01;2.4;0.076;14;39;0.99522;3.34;0.53;10.8;6 +6.8;0.59;0.1;1.7;0.063;34;53;0.9958;3.41;0.67;9.7;5 +6.8;0.59;0.1;1.7;0.063;34;53;0.9958;3.41;0.67;9.7;5 +7.3;0.48;0.32;2.1;0.062;31;54;0.99728;3.3;0.65;10;7 +6.7;1.04;0.08;2.3;0.067;19;32;0.99648;3.52;0.57;11;4 +7.3;0.48;0.32;2.1;0.062;31;54;0.99728;3.3;0.65;10;7 +7.3;0.98;0.05;2.1;0.061;20;49;0.99705;3.31;0.55;9.7;3 +10;0.69;0.11;1.4;0.084;8;24;0.99578;2.88;0.47;9.7;5 +6.7;0.7;0.08;3.75;0.067;8;16;0.99334;3.43;0.52;12.6;5 +7.6;0.35;0.6;2.6;0.073;23;44;0.99656;3.38;0.79;11.1;6 +6.1;0.6;0.08;1.8;0.071;14;45;0.99336;3.38;0.54;11;5 +9.9;0.5;0.5;13.8;0.205;48;82;1.00242;3.16;0.75;8.8;5 +5.3;0.47;0.11;2.2;0.048;16;89;0.99182;3.54;0.88;13.5666666666667;7 +9.9;0.5;0.5;13.8;0.205;48;82;1.00242;3.16;0.75;8.8;5 +5.3;0.47;0.11;2.2;0.048;16;89;0.99182;3.54;0.88;13.6;7 +7.1;0.875;0.05;5.7;0.082;3;14;0.99808;3.4;0.52;10.2;3 +8.2;0.28;0.6;3;0.104;10;22;0.99828;3.39;0.68;10.6;5 +5.6;0.62;0.03;1.5;0.08;6;13;0.99498;3.66;0.62;10.1;4 +8.2;0.28;0.6;3;0.104;10;22;0.99828;3.39;0.68;10.6;5 +7.2;0.58;0.54;2.1;0.114;3;9;0.99719;3.33;0.57;10.3;4 +8.1;0.33;0.44;1.5;0.042;6;12;0.99542;3.35;0.61;10.7;5 +6.8;0.91;0.06;2;0.06;4;11;0.99592;3.53;0.64;10.9;4 +7;0.655;0.16;2.1;0.074;8;25;0.99606;3.37;0.55;9.7;5 +6.8;0.68;0.21;2.1;0.07;9;23;0.99546;3.38;0.6;10.3;5 +6;0.64;0.05;1.9;0.066;9;17;0.99496;3.52;0.78;10.6;5 +5.6;0.54;0.04;1.7;0.049;5;13;0.9942;3.72;0.58;11.4;5 +6.2;0.57;0.1;2.1;0.048;4;11;0.99448;3.44;0.76;10.8;6 +7.1;0.22;0.49;1.8;0.039;8;18;0.99344;3.39;0.56;12.4;6 +5.6;0.54;0.04;1.7;0.049;5;13;0.9942;3.72;0.58;11.4;5 +6.2;0.65;0.06;1.6;0.05;6;18;0.99348;3.57;0.54;11.95;5 +7.7;0.54;0.26;1.9;0.089;23;147;0.99636;3.26;0.59;9.7;5 +6.4;0.31;0.09;1.4;0.066;15;28;0.99459;3.42;0.7;10;7 +7;0.43;0.02;1.9;0.08;15;28;0.99492;3.35;0.81;10.6;6 +7.7;0.54;0.26;1.9;0.089;23;147;0.99636;3.26;0.59;9.7;5 +6.9;0.74;0.03;2.3;0.054;7;16;0.99508;3.45;0.63;11.5;6 +6.6;0.895;0.04;2.3;0.068;7;13;0.99582;3.53;0.58;10.8;6 +6.9;0.74;0.03;2.3;0.054;7;16;0.99508;3.45;0.63;11.5;6 +7.5;0.725;0.04;1.5;0.076;8;15;0.99508;3.26;0.53;9.6;5 +7.8;0.82;0.29;4.3;0.083;21;64;0.99642;3.16;0.53;9.4;5 +7.3;0.585;0.18;2.4;0.078;15;60;0.99638;3.31;0.54;9.8;5 +6.2;0.44;0.39;2.5;0.077;6;14;0.99555;3.51;0.69;11;6 +7.5;0.38;0.57;2.3;0.106;5;12;0.99605;3.36;0.55;11.4;6 +6.7;0.76;0.02;1.8;0.078;6;12;0.996;3.55;0.63;9.95;3 +6.8;0.81;0.05;2;0.07;6;14;0.99562;3.51;0.66;10.8;6 +7.5;0.38;0.57;2.3;0.106;5;12;0.99605;3.36;0.55;11.4;6 +7.1;0.27;0.6;2.1;0.074;17;25;0.99814;3.38;0.72;10.6;6 +7.9;0.18;0.4;1.8;0.062;7;20;0.9941;3.28;0.7;11.1;5 +6.4;0.36;0.21;2.2;0.047;26;48;0.99661;3.47;0.77;9.7;6 +7.1;0.69;0.04;2.1;0.068;19;27;0.99712;3.44;0.67;9.8;5 +6.4;0.79;0.04;2.2;0.061;11;17;0.99588;3.53;0.65;10.4;6 +6.4;0.56;0.15;1.8;0.078;17;65;0.99294;3.33;0.6;10.5;6 +6.9;0.84;0.21;4.1;0.074;16;65;0.99842;3.53;0.72;9.23333333333333;6 +6.9;0.84;0.21;4.1;0.074;16;65;0.99842;3.53;0.72;9.25;6 +6.1;0.32;0.25;2.3;0.071;23;58;0.99633;3.42;0.97;10.6;5 +6.5;0.53;0.06;2;0.063;29;44;0.99489;3.38;0.83;10.3;6 +7.4;0.47;0.46;2.2;0.114;7;20;0.99647;3.32;0.63;10.5;5 +6.6;0.7;0.08;2.6;0.106;14;27;0.99665;3.44;0.58;10.2;5 +6.5;0.53;0.06;2;0.063;29;44;0.99489;3.38;0.83;10.3;6 +6.9;0.48;0.2;1.9;0.082;9;23;0.99585;3.39;0.43;9.05;4 +6.1;0.32;0.25;2.3;0.071;23;58;0.99633;3.42;0.97;10.6;5 +6.8;0.48;0.25;2;0.076;29;61;0.9953;3.34;0.6;10.4;5 +6;0.42;0.19;2;0.075;22;47;0.99522;3.39;0.78;10;6 +6.7;0.48;0.08;2.1;0.064;18;34;0.99552;3.33;0.64;9.7;5 +6.8;0.47;0.08;2.2;0.064;18;38;0.99553;3.3;0.65;9.6;6 +7.1;0.53;0.07;1.7;0.071;15;24;0.9951;3.29;0.66;10.8;6 +7.9;0.29;0.49;2.2;0.096;21;59;0.99714;3.31;0.67;10.1;6 +7.1;0.69;0.08;2.1;0.063;42;52;0.99608;3.42;0.6;10.2;6 +6.6;0.44;0.09;2.2;0.063;9;18;0.99444;3.42;0.69;11.3;6 +6.1;0.705;0.1;2.8;0.081;13;28;0.99631;3.6;0.66;10.2;5 +7.2;0.53;0.13;2;0.058;18;22;0.99573;3.21;0.68;9.9;6 +8;0.39;0.3;1.9;0.074;32;84;0.99717;3.39;0.61;9;5 +6.6;0.56;0.14;2.4;0.064;13;29;0.99397;3.42;0.62;11.7;7 +7;0.55;0.13;2.2;0.075;15;35;0.9959;3.36;0.59;9.7;6 +6.1;0.53;0.08;1.9;0.077;24;45;0.99528;3.6;0.68;10.3;6 +5.4;0.58;0.08;1.9;0.059;20;31;0.99484;3.5;0.64;10.2;6 +6.2;0.64;0.09;2.5;0.081;15;26;0.99538;3.57;0.63;12;5 +7.2;0.39;0.32;1.8;0.065;34;60;0.99714;3.46;0.78;9.9;5 +6.2;0.52;0.08;4.4;0.071;11;32;0.99646;3.56;0.63;11.6;6 +7.4;0.25;0.29;2.2;0.054;19;49;0.99666;3.4;0.76;10.9;7 +6.7;0.855;0.02;1.9;0.064;29;38;0.99472;3.3;0.56;10.75;6 +11.1;0.44;0.42;2.2;0.064;14;19;0.99758;3.25;0.57;10.4;6 +8.4;0.37;0.43;2.3;0.063;12;19;0.9955;3.17;0.81;11.2;7 +6.5;0.63;0.33;1.8;0.059;16;28;0.99531;3.36;0.64;10.1;6 +7;0.57;0.02;2;0.072;17;26;0.99575;3.36;0.61;10.2;5 +6.3;0.6;0.1;1.6;0.048;12;26;0.99306;3.55;0.51;12.1;5 +11.2;0.4;0.5;2;0.099;19;50;0.99783;3.1;0.58;10.4;5 +7.4;0.36;0.3;1.8;0.074;17;24;0.99419;3.24;0.7;11.4;8 +7.1;0.68;0;2.3;0.087;17;26;0.99783;3.45;0.53;9.5;5 +7.1;0.67;0;2.3;0.083;18;27;0.99768;3.44;0.54;9.4;5 +6.3;0.68;0.01;3.7;0.103;32;54;0.99586;3.51;0.66;11.3;6 +7.3;0.735;0;2.2;0.08;18;28;0.99765;3.41;0.6;9.4;5 +6.6;0.855;0.02;2.4;0.062;15;23;0.99627;3.54;0.6;11;6 +7;0.56;0.17;1.7;0.065;15;24;0.99514;3.44;0.68;10.55;7 +6.6;0.88;0.04;2.2;0.066;12;20;0.99636;3.53;0.56;9.9;5 +6.6;0.855;0.02;2.4;0.062;15;23;0.99627;3.54;0.6;11;6 +6.9;0.63;0.33;6.7;0.235;66;115;0.99787;3.22;0.56;9.5;5 +7.8;0.6;0.26;2;0.08;31;131;0.99622;3.21;0.52;9.9;5 +7.8;0.6;0.26;2;0.08;31;131;0.99622;3.21;0.52;9.9;5 +7.8;0.6;0.26;2;0.08;31;131;0.99622;3.21;0.52;9.9;5 +7.2;0.695;0.13;2;0.076;12;20;0.99546;3.29;0.54;10.1;5 +7.2;0.695;0.13;2;0.076;12;20;0.99546;3.29;0.54;10.1;5 +7.2;0.695;0.13;2;0.076;12;20;0.99546;3.29;0.54;10.1;5 +6.7;0.67;0.02;1.9;0.061;26;42;0.99489;3.39;0.82;10.9;6 +6.7;0.16;0.64;2.1;0.059;24;52;0.99494;3.34;0.71;11.2;6 +7.2;0.695;0.13;2;0.076;12;20;0.99546;3.29;0.54;10.1;5 +7;0.56;0.13;1.6;0.077;25;42;0.99629;3.34;0.59;9.2;5 +6.2;0.51;0.14;1.9;0.056;15;34;0.99396;3.48;0.57;11.5;6 +6.4;0.36;0.53;2.2;0.23;19;35;0.9934;3.37;0.93;12.4;6 +6.4;0.38;0.14;2.2;0.038;15;25;0.99514;3.44;0.65;11.1;6 +7.3;0.69;0.32;2.2;0.069;35;104;0.99632;3.33;0.51;9.5;5 +6;0.58;0.2;2.4;0.075;15;50;0.99467;3.58;0.67;12.5;6 +5.6;0.31;0.78;13.9;0.074;23;92;0.99677;3.39;0.48;10.5;6 +7.5;0.52;0.4;2.2;0.06;12;20;0.99474;3.26;0.64;11.8;6 +8;0.3;0.63;1.6;0.081;16;29;0.99588;3.3;0.78;10.8;6 +6.2;0.7;0.15;5.1;0.076;13;27;0.99622;3.54;0.6;11.9;6 +6.8;0.67;0.15;1.8;0.118;13;20;0.9954;3.42;0.67;11.3;6 +6.2;0.56;0.09;1.7;0.053;24;32;0.99402;3.54;0.6;11.3;5 +7.4;0.35;0.33;2.4;0.068;9;26;0.9947;3.36;0.6;11.9;6 +6.2;0.56;0.09;1.7;0.053;24;32;0.99402;3.54;0.6;11.3;5 +6.1;0.715;0.1;2.6;0.053;13;27;0.99362;3.57;0.5;11.9;5 +6.2;0.46;0.29;2.1;0.074;32;98;0.99578;3.33;0.62;9.8;5 +6.7;0.32;0.44;2.4;0.061;24;34;0.99484;3.29;0.8;11.6;7 +7.2;0.39;0.44;2.6;0.066;22;48;0.99494;3.3;0.84;11.5;6 +7.5;0.31;0.41;2.4;0.065;34;60;0.99492;3.34;0.85;11.4;6 +5.8;0.61;0.11;1.8;0.066;18;28;0.99483;3.55;0.66;10.9;6 +7.2;0.66;0.33;2.5;0.068;34;102;0.99414;3.27;0.78;12.8;6 +6.6;0.725;0.2;7.8;0.073;29;79;0.9977;3.29;0.54;9.2;5 +6.3;0.55;0.15;1.8;0.077;26;35;0.99314;3.32;0.82;11.6;6 +5.4;0.74;0.09;1.7;0.089;16;26;0.99402;3.67;0.56;11.6;6 +6.3;0.51;0.13;2.3;0.076;29;40;0.99574;3.42;0.75;11;6 +6.8;0.62;0.08;1.9;0.068;28;38;0.99651;3.42;0.82;9.5;6 +6.2;0.6;0.08;2;0.09;32;44;0.9949;3.45;0.58;10.5;5 +5.9;0.55;0.1;2.2;0.062;39;51;0.99512;3.52;0.76;11.2;6 +6.3;0.51;0.13;2.3;0.076;29;40;0.99574;3.42;0.75;11;6 +5.9;0.645;0.12;2;0.075;32;44;0.99547;3.57;0.71;10.2;5 +6;0.31;0.47;3.6;0.067;18;42;0.99549;3.39;0.66;11;6 diff --git a/tests/applications/data/winequality.names b/tests/applications/data/winequality.names new file mode 100644 index 0000000..4e1de1f --- /dev/null +++ b/tests/applications/data/winequality.names @@ -0,0 +1,72 @@ +Citation Request: + This dataset is public available for research. The details are described in [Cortez et al., 2009]. + Please include this citation if you plan to use this database: + + P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J. Reis. + Modeling wine preferences by data mining from physicochemical properties. + In Decision Support Systems, Elsevier, 47(4):547-553. ISSN: 0167-9236. + + Available at: [@Elsevier] http://dx.doi.org/10.1016/j.dss.2009.05.016 + [Pre-press (pdf)] http://www3.dsi.uminho.pt/pcortez/winequality09.pdf + [bib] http://www3.dsi.uminho.pt/pcortez/dss09.bib + +1. Title: Wine Quality + +2. Sources + Created by: Paulo Cortez (Univ. Minho), Antonio Cerdeira, Fernando Almeida, Telmo Matos and Jose Reis (CVRVV) @ 2009 + +3. Past Usage: + + P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J. Reis. + Modeling wine preferences by data mining from physicochemical properties. + In Decision Support Systems, Elsevier, 47(4):547-553. ISSN: 0167-9236. + + In the above reference, two datasets were created, using red and white wine samples. + The inputs include objective tests (e.g. PH values) and the output is based on sensory data + (median of at least 3 evaluations made by wine experts). Each expert graded the wine quality + between 0 (very bad) and 10 (very excellent). Several data mining methods were applied to model + these datasets under a regression approach. The support vector machine model achieved the + best results. Several metrics were computed: MAD, confusion matrix for a fixed error tolerance (T), + etc. Also, we plot the relative importances of the input variables (as measured by a sensitivity + analysis procedure). + +4. Relevant Information: + + The two datasets are related to red and white variants of the Portuguese "Vinho Verde" wine. + For more details, consult: http://www.vinhoverde.pt/en/ or the reference [Cortez et al., 2009]. + Due to privacy and logistic issues, only physicochemical (inputs) and sensory (the output) variables + are available (e.g. there is no data about grape types, wine brand, wine selling price, etc.). + + These datasets can be viewed as classification or regression tasks. + The classes are ordered and not balanced (e.g. there are munch more normal wines than + excellent or poor ones). Outlier detection algorithms could be used to detect the few excellent + or poor wines. Also, we are not sure if all input variables are relevant. So + it could be interesting to test feature selection methods. + +5. Number of Instances: red wine - 1599; white wine - 4898. + +6. Number of Attributes: 11 + output attribute + + Note: several of the attributes may be correlated, thus it makes sense to apply some sort of + feature selection. + +7. Attribute information: + + For more information, read [Cortez et al., 2009]. + + Input variables (based on physicochemical tests): + 1 - fixed acidity + 2 - volatile acidity + 3 - citric acid + 4 - residual sugar + 5 - chlorides + 6 - free sulfur dioxide + 7 - total sulfur dioxide + 8 - density + 9 - pH + 10 - sulphates + 11 - alcohol + Output variable (based on sensory data): + 12 - quality (score between 0 and 10) + +8. Missing Attribute Values: None diff --git a/tests/applications/invoice/consumer.js b/tests/applications/invoice/consumer.js new file mode 100644 index 0000000..afc5ae2 --- /dev/null +++ b/tests/applications/invoice/consumer.js @@ -0,0 +1,10 @@ +var totals = new Map(); +for (var row of bridge) { + var value = totals.get(row.region) || [0, 0]; + value[0] += 1; + value[1] += Math.floor(row.units * row.cents * (10000-row.discount) / 10000); + totals.set(row.region, value); +} +var answer = [...totals].sort((a,b) => Buffer.compare(Buffer.from(a[0]),Buffer.from(b[0]))) + .map(([region, value]) => ({region, invoices: value[0], net_cents: value[1]})); +answer; diff --git a/tests/applications/invoice/consumer.py b/tests/applications/invoice/consumer.py new file mode 100644 index 0000000..6c48b36 --- /dev/null +++ b/tests/applications/invoice/consumer.py @@ -0,0 +1,8 @@ +from collections import defaultdict +totals = defaultdict(lambda: [0, 0]) +for row in bridge: + totals[row['region']][0] += 1 + totals[row['region']][1] += row['units'] * row['cents'] * (10000-row['discount']) // 10000 +answer = [{'region': k, 'invoices': v[0], 'net_cents': v[1]} + for k, v in sorted(totals.items(), key=lambda kv: kv[0].encode('utf-8'))] +answer diff --git a/tests/applications/invoice/producer.js b/tests/applications/invoice/producer.js new file mode 100644 index 0000000..2dff4b5 --- /dev/null +++ b/tests/applications/invoice/producer.js @@ -0,0 +1,10 @@ +var seen = new Set(); +var bridge = []; +for (var row of data) { + if (row.status === 'posted' && !seen.has(row.id)) { + seen.add(row.id); + bridge.push({id: row.id, region: row.region, units: row.units, cents: row.cents, discount: row.discount}); + } +} +bridge.sort((a, b) => Buffer.compare(Buffer.from(a.id), Buffer.from(b.id))); +bridge; diff --git a/tests/applications/invoice/producer.py b/tests/applications/invoice/producer.py new file mode 100644 index 0000000..31b8e5d --- /dev/null +++ b/tests/applications/invoice/producer.py @@ -0,0 +1,8 @@ +seen = set() +bridge = [] +for row in data: + if row['status'] == 'posted' and row['id'] not in seen: + seen.add(row['id']) + bridge.append({k: row[k] for k in ('id', 'region', 'units', 'cents', 'discount')}) +bridge.sort(key=lambda r: r['id'].encode('utf-8')) +bridge diff --git a/tests/applications/routes/consumer.js b/tests/applications/routes/consumer.js new file mode 100644 index 0000000..1b59831 --- /dev/null +++ b/tests/applications/routes/consumer.js @@ -0,0 +1,14 @@ +var distance = new Map(bridge.nodes.map(n=>[n,Infinity])); +distance.set(bridge.source,0); +var unvisited = new Set(bridge.nodes); +while (unvisited.size) { + var best = [...unvisited].reduce((a,b)=>distance.get(a)<=distance.get(b)?a:b); + unvisited.delete(best); + if (!Number.isFinite(distance.get(best))) break; + for (var edge of bridge.edges) { + if (edge.from === best && distance.get(best)+edge.weight < distance.get(edge.to)) + distance.set(edge.to,distance.get(best)+edge.weight); + } +} +var answer = bridge.nodes.map(node=>({node,distance:Number.isFinite(distance.get(node))?distance.get(node):null})); +answer; diff --git a/tests/applications/routes/consumer.py b/tests/applications/routes/consumer.py new file mode 100644 index 0000000..3ffccd9 --- /dev/null +++ b/tests/applications/routes/consumer.py @@ -0,0 +1,17 @@ +import heapq +adjacency = {node: [] for node in bridge['nodes']} +for edge in bridge['edges']: + adjacency[edge['from']].append((edge['to'], edge['weight'])) +distance = {bridge['source']: 0} +pending = [(0, bridge['source'])] +while pending: + cost, node = heapq.heappop(pending) + if distance[node] != cost: + continue + for target, weight in adjacency[node]: + candidate = cost + weight + if target not in distance or candidate < distance[target]: + distance[target] = candidate + heapq.heappush(pending, (candidate, target)) +answer = [{'node': n, 'distance': distance.get(n)} for n in bridge['nodes']] +answer diff --git a/tests/applications/routes/producer.js b/tests/applications/routes/producer.js new file mode 100644 index 0000000..e5fac90 --- /dev/null +++ b/tests/applications/routes/producer.js @@ -0,0 +1,11 @@ +var minimum = new Map(); +for (var edge of data.edges) { + if (edge.weight >= 0) { + var key = JSON.stringify([edge.from, edge.to]); + minimum.set(key, Math.min(minimum.has(key) ? minimum.get(key) : edge.weight, edge.weight)); + } +} +var edges = [...minimum].map(([key,weight]) => ({from: JSON.parse(key)[0], to: JSON.parse(key)[1], weight})); +edges.sort((a,b) => Buffer.compare(Buffer.from(a.from),Buffer.from(b.from)) || Buffer.compare(Buffer.from(a.to),Buffer.from(b.to))); +var bridge = {source:data.source, nodes:[...data.nodes].sort((a,b)=>Buffer.compare(Buffer.from(a),Buffer.from(b))), edges}; +bridge; diff --git a/tests/applications/routes/producer.py b/tests/applications/routes/producer.py new file mode 100644 index 0000000..c8938a8 --- /dev/null +++ b/tests/applications/routes/producer.py @@ -0,0 +1,8 @@ +minimum = {} +for edge in data['edges']: + if edge['weight'] >= 0: + key = (edge['from'], edge['to']) + minimum[key] = min(minimum.get(key, edge['weight']), edge['weight']) +bridge = {'source': data['source'], 'nodes': sorted(data['nodes'], key=lambda n:n.encode('utf-8')), + 'edges': [{'from': a, 'to': b, 'weight': w} for (a,b), w in sorted(minimum.items(), key=lambda kv: (kv[0][0].encode('utf-8'), kv[0][1].encode('utf-8')))]} +bridge diff --git a/tests/applications/wine/consumer.js b/tests/applications/wine/consumer.js new file mode 100644 index 0000000..dcd66de --- /dev/null +++ b/tests/applications/wine/consumer.js @@ -0,0 +1,9 @@ +var groups = new Map(); +for (var row of bridge) { + var g=groups.get(row.quality) || {quality:row.quality,count:0,sum_density_ppm:0,min_ph_cent:row.ph_cent,max_ph_cent:row.ph_cent}; + g.count++;g.sum_density_ppm+=row.density_ppm; + g.min_ph_cent=Math.min(g.min_ph_cent,row.ph_cent);g.max_ph_cent=Math.max(g.max_ph_cent,row.ph_cent); + groups.set(row.quality,g); +} +var answer=[...groups.values()].sort((a,b)=>a.quality-b.quality); +answer; diff --git a/tests/applications/wine/consumer.py b/tests/applications/wine/consumer.py new file mode 100644 index 0000000..bdb808c --- /dev/null +++ b/tests/applications/wine/consumer.py @@ -0,0 +1,7 @@ +from itertools import groupby +answer = [] +for quality, group in groupby(sorted(bridge,key=lambda r:r['quality']),key=lambda r:r['quality']): + rows = list(group) + answer.append({'quality':quality,'count':len(rows),'sum_density_ppm':sum(r['density_ppm'] for r in rows), + 'min_ph_cent':min(r['ph_cent'] for r in rows),'max_ph_cent':max(r['ph_cent'] for r in rows)}) +answer diff --git a/tests/applications/wine/producer.js b/tests/applications/wine/producer.js new file mode 100644 index 0000000..83c50b0 --- /dev/null +++ b/tests/applications/wine/producer.js @@ -0,0 +1,13 @@ +function scaledDecimal(s, digits) { + var parts = s.split('.'); + var tail = parts.length > 1 ? parts[1] : ''; + if (tail.length > digits) throw Error('unsupported decimal precision'); + return Number(parts[0])*10**digits + Number(tail.padEnd(digits,'0')); +} +var lines = data.trim().split(/\r?\n/); +var header = lines[0].split(';').map(s=>s.replaceAll('"','')); +var bridge = lines.slice(1).map(line=>{ + var fields=line.split(';'); + return {quality:Number(fields[header.indexOf('quality')]),density_ppm:scaledDecimal(fields[header.indexOf('density')],6),ph_cent:scaledDecimal(fields[header.indexOf('pH')],2)}; +}); +bridge; diff --git a/tests/applications/wine/producer.py b/tests/applications/wine/producer.py new file mode 100644 index 0000000..b66076e --- /dev/null +++ b/tests/applications/wine/producer.py @@ -0,0 +1,5 @@ +import csv, io +from decimal import Decimal +bridge = [{'quality':int(row['quality']), 'density_ppm':int(Decimal(row['density'])*1000000), 'ph_cent':int(Decimal(row['pH'])*100)} + for row in csv.DictReader(io.StringIO(data),delimiter=';')] +bridge diff --git a/tests/errors/lexical-private-descendant.mq.md b/tests/errors/lexical-private-descendant.mq.md new file mode 100644 index 0000000..3651cc8 --- /dev/null +++ b/tests/errors/lexical-private-descendant.mq.md @@ -0,0 +1,11 @@ +--- +markup: occlusion +title: Private grandchildren are not in the enclosing scope +--- +# main +> concealed + +## outer + +### concealed +*1* diff --git a/tests/gold.rs b/tests/gold.rs index 58e43d3..160f4d8 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -45,14 +45,18 @@ fn run_with_env(args: &[&str], env: &[(&str, &str)]) -> (i32, String, String) { } fn assert_out(path: &str, expect: &str) { - let (code, stdout, stderr) = run(&["run", path]); - assert_eq!(code, 0, "{path} stderr={stderr}"); - assert_eq!(stdout.trim_end(), expect.trim_end(), "{path}"); + for backend in ["tree", "bytecode"] { + let (code, stdout, stderr) = run(&["run", path, "--backend", backend]); + assert_eq!(code, 0, "{path} {backend} stderr={stderr}"); + assert_eq!(stdout.trim_end(), expect.trim_end(), "{path} {backend}"); + } } fn assert_run_ok(path: &str) { - let (code, _stdout, stderr) = run(&["run", path]); - assert_eq!(code, 0, "{path} stderr={stderr}"); + for backend in ["tree", "bytecode"] { + let (code, _stdout, stderr) = run(&["run", path, "--backend", backend]); + assert_eq!(code, 0, "{path} {backend} stderr={stderr}"); + } } /// Skip live LLM/agent tests when no real API key is configured. @@ -189,6 +193,21 @@ fn assert_err(path: &str, line_col: &str, substr: &str) { } } +/// Imported faults have an actual library origin and a separate authored call site. +fn assert_import_err(path: &str, library: &str, origin: &str, caller: &str, substr: &str) { + for backend in ["tree", "bytecode"] { + let (code, stdout, stderr) = run(&["run", path, "--backend", backend]); + assert_ne!(code, 0, "{path} backend={backend} unexpectedly succeeded stdout={stdout}"); + let origin = format!("{library}:{origin}:"); + assert!(stderr.lines().next().unwrap_or("").contains(&origin), + "{path} backend={backend} missing actual library origin {origin:?}: {stderr}"); + let caller = format!(" from {path}:{caller}"); + assert!(stderr.lines().skip(1).any(|line| line.starts_with(" called ") && line.ends_with(&caller)), + "{path} backend={backend} missing authored call site {caller:?}: {stderr}"); + assert!(stderr.contains(substr), "{path} backend={backend} stderr missing {substr:?}: {stderr}"); + } +} + /// Like [`assert_out`], but sets a dummy OpenAI key so offline LLM/agent paths /// that only check for the env var (no live HTTP) can proceed. /// Also points `MARQDO_EXT` at the repo `ext/` so tests do not pick up a stale @@ -1228,7 +1247,7 @@ fn bytecode_paragraph_comment() { #[test] fn trace_eval_json_tree() { - let (code, stdout, stderr) = run(&["run", "tests/structure/hello.mq.md", "--trace-eval"]); + let (code, stdout, stderr) = run(&["run", "tests/structure/hello.mq.md", "--backend", "tree", "--trace-eval"]); assert_eq!(code, 0, "{stderr}"); assert_eq!(stdout.trim_end(), "Hello World!"); assert!(stderr.contains("\"event\":\"enter_fn\""), "{stderr}"); @@ -1237,6 +1256,21 @@ fn trace_eval_json_tree() { assert!(stderr.contains("\"event\":\"leave_fn\""), "{stderr}"); } +#[test] +fn structure_self_method_paths_are_instance_methods() { + assert_out("tests/structure/self-method-paths.mq.md", "41\n41\n7"); +} + +#[test] +fn structure_lexical_siblings_recursion_and_shadowing() { + assert_out("tests/structure/lexical-sibling-calls.mq.md", "120\n41\n99"); +} + +#[test] +fn error_lexical_private_descendant_is_not_visible() { + assert_err("tests/errors/lexical-private-descendant.mq.md", "6:1", "unknown function `concealed`"); +} + #[test] fn trace_eval_json_bytecode() { let (code, stdout, stderr) = run(&[ @@ -1884,19 +1918,23 @@ fn lib_json_vsafe_string_ok() { #[test] fn lib_json_vsafe_refuse() { - // Span points at lib/json.mq.md `parse` body while path stays the caller file. - assert_err( + // Check both the actual library body and the user call, without mixing them. + assert_import_err( "tests/lib/json-vsafe-refuse.mq.md", + "lib/json.mq.md", "16:1", + "10:1", "json: integer outside V_safe", ); } #[test] fn lib_json_vsafe_stringify_refuse() { - assert_err( + assert_import_err( "tests/lib/json-vsafe-stringify-refuse.mq.md", + "lib/json.mq.md", "24:1", + "11:1", "json: integer outside V_safe", ); } @@ -3630,9 +3668,11 @@ fn lib_foreign_session_vsafe_refuse() { return; } // Span points at lib/foreign.mq.md session_set body. - assert_err( + assert_import_err( "tests/lib/foreign-session-vsafe-refuse.mq.md", + "lib/foreign.mq.md", "207:1", + "12:1", "json: integer outside V_safe", ); } @@ -3643,9 +3683,11 @@ fn lib_foreign_session_use_after_close() { eprintln!("skip lib_foreign_session_use_after_close: no node on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-session-use-after-close.mq.md", + "lib/foreign.mq.md", "198:1", + "12:1", "foreign session", ); } @@ -3656,9 +3698,11 @@ fn lib_foreign_session_double_close() { eprintln!("skip lib_foreign_session_double_close: no node on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-session-double-close.mq.md", + "lib/foreign.mq.md", "231:1", + "12:1", "foreign session", ); } @@ -3666,9 +3710,11 @@ fn lib_foreign_session_double_close() { #[test] fn lib_foreign_session_use_before_open() { // Text fabrication is refused before registry lookup (opaque handle discipline). - assert_err( + assert_import_err( "tests/lib/foreign-session-use-before-open.mq.md", + "lib/foreign.mq.md", "198:1", + "10:1", "session id must be a handle", ); } @@ -3679,9 +3725,11 @@ fn lib_foreign_session_handle_no_serialize() { eprintln!("skip lib_foreign_session_handle_no_serialize: no node on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-session-handle-no-serialize.mq.md", + "lib/json.mq.md", "24:1", + "12:1", "session handle cannot be serialized", ); } @@ -3692,9 +3740,11 @@ fn lib_foreign_session_result_vsafe_refuse() { eprintln!("skip lib_foreign_session_result_vsafe_refuse: no python on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-session-result-vsafe-refuse.mq.md", + "lib/foreign.mq.md", "198:1", + "11:1", "json: integer outside V_safe", ); } @@ -3714,9 +3764,11 @@ fn lib_foreign_session_cold_replay_empty() { eprintln!("skip lib_foreign_session_cold_replay_empty: no node on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-session-cold-replay-empty.mq.md", + "lib/foreign.mq.md", "198:1", + "13:1", "foreign session", ); } @@ -3727,9 +3779,11 @@ fn lib_foreign_vsafe_refuse() { eprintln!("skip lib_foreign_vsafe_refuse: no python on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-vsafe-refuse.mq.md", + "lib/json.mq.md", "16:1", + "21:1", "json: integer outside V_safe", ); } @@ -3740,9 +3794,11 @@ fn lib_foreign_vsafe_postarith_refuse() { eprintln!("skip lib_foreign_vsafe_postarith_refuse: no python on PATH"); return; } - assert_err( + assert_import_err( "tests/lib/foreign-vsafe-postarith-refuse.mq.md", + "lib/json.mq.md", "16:1", + "22:1", "json: integer outside V_safe", ); } diff --git a/tests/lib/foreign-copy-code-refuse.mq.md b/tests/lib/foreign-copy-code-refuse.mq.md new file mode 100644 index 0000000..af1c3d7 --- /dev/null +++ b/tests/lib/foreign-copy-code-refuse.mq.md @@ -0,0 +1,15 @@ +--- +markup: occlusion +title: code values are not copied foreign payloads +import: + foreign: lib/foreign.mq.md +--- + +# main + +`payload` = +```python +1 +``` + +**> foreign.invoke lang="python" source="print('unexpected')" input=`payload`** diff --git a/tests/lib/foreign-oracle-failure.mq.md b/tests/lib/foreign-oracle-failure.mq.md new file mode 100644 index 0000000..d99985a --- /dev/null +++ b/tests/lib/foreign-oracle-failure.mq.md @@ -0,0 +1,17 @@ +--- +markup: occlusion +title: failed foreign responses remain observable values +import: + foreign: lib/foreign.mq.md +--- + +# main + +**s = > foreign.session_open lang="python"** +**> foreign.session_eval id=`s` source="raise ValueError('outside V_safe')"** +**j = > foreign.oracle_journal** +**> print text=[channel](access(access(`j`, 1), "channel"))** +**> print text=[ok](access(access(access(`j`, 1), "value"), "ok"))** +**> foreign.session_eval id=`s` source="import os; os._exit(7)"** +**later = > foreign.oracle_journal** +**> print text=[alive](access(access(access(`later`, 2), "value"), "alive"))** diff --git a/tests/lib/foreign-oracle-journal.mq.md b/tests/lib/foreign-oracle-journal.mq.md new file mode 100644 index 0000000..f275e90 --- /dev/null +++ b/tests/lib/foreign-oracle-journal.mq.md @@ -0,0 +1,23 @@ +--- +markup: occlusion +title: document oracle journal is a first-class value +import: + foreign: lib/foreign.mq.md + json: lib/json.mq.md +--- + +# main + +**s = > foreign.session_open lang="python"** +**> foreign.session_set id=`s` name="n" value=2** +**> foreign.session_eval id=`s` source="n + 1"** +**> foreign.session_get id=`s` name="n"** +**> foreign.session_close id=`s`** +**j = > foreign.oracle_journal** +**same = > foreign.replay_check expected=`j` observed=`j`** +**> print text=[ok](access(`same`, "ok"))** +**> json.parse text="3"** +**later = > foreign.oracle_journal** +**changed = > foreign.replay_check expected=`j` observed=`later`** +**> print text=[ok](access(`changed`, "ok"))** +**> print text=[index](access(`changed`, "index"))** diff --git a/tests/lib/foreign-oracle-names.mq.md b/tests/lib/foreign-oracle-names.mq.md new file mode 100644 index 0000000..e845ceb --- /dev/null +++ b/tests/lib/foreign-oracle-names.mq.md @@ -0,0 +1,16 @@ +--- +markup: occlusion +title: explicit names response is a decode observation +import: + foreign: lib/foreign.mq.md +--- + +# main + +**s = > foreign.session_open lang="python"** +**> foreign.session_set id=`s` name="zzz" value=7** +**> foreign.session_names id=`s`** +**j = > foreign.oracle_journal** +**> print text=[channel](access(access(`j`, 1), "channel"))** +**> print text=[name](access(access(access(`j`, 1), "value"), 2))** +**> foreign.session_close id=`s`** diff --git a/tests/lib/foreign-session-copy-code-refuse.mq.md b/tests/lib/foreign-session-copy-code-refuse.mq.md new file mode 100644 index 0000000..cadd2e1 --- /dev/null +++ b/tests/lib/foreign-session-copy-code-refuse.mq.md @@ -0,0 +1,16 @@ +--- +markup: occlusion +title: persistent sessions enforce the same code payload refusal +import: + foreign: lib/foreign.mq.md +--- + +# main + +`payload` = +```python +1 +``` + +**s = > foreign.session_open lang="python"** +**> foreign.session_set id=`s` name="payload" value=`payload`** diff --git a/tests/oracle_journal.rs b/tests/oracle_journal.rs new file mode 100644 index 0000000..6fbb532 --- /dev/null +++ b/tests/oracle_journal.rs @@ -0,0 +1,46 @@ +use std::process::Command; + +#[test] +fn document_oracle_values_both_backends() { + for backend in ["tree", "bytecode"] { + let out = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", "tests/lib/foreign-oracle-journal.mq.md", "--backend", backend]) + .output().unwrap(); + assert!(out.status.success(), "{backend}: {}", String::from_utf8_lossy(&out.stderr)); + assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "True\nFalse\n2", "{backend}"); + } +} + +#[test] +fn failed_responses_are_values_on_both_backends() { + for backend in ["tree", "bytecode"] { + let out = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", "tests/lib/foreign-oracle-failure.mq.md", "--backend", backend]) + .output().unwrap(); + assert!(out.status.success(), "{backend}: {}", String::from_utf8_lossy(&out.stderr)); + assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "eval\nFalse\nFalse", "{backend}"); + } +} + +#[test] +fn copied_code_values_are_refused_on_both_backends_and_lifetimes() { + for backend in ["tree", "bytecode"] { + for path in ["tests/lib/foreign-copy-code-refuse.mq.md", "tests/lib/foreign-session-copy-code-refuse.mq.md"] { + let out = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", path, "--backend", backend]).output().unwrap(); + assert!(!out.status.success(), "{backend} {path}: accepted code payload"); + assert!(String::from_utf8_lossy(&out.stderr).contains("outside V_safe"), "{}", String::from_utf8_lossy(&out.stderr)); + assert!(out.stdout.is_empty(), "refused input must not run the source"); + } + } +} + +#[test] +fn explicit_names_is_a_decode_observation_on_both_backends() { + for backend in ["tree", "bytecode"] { + let out=Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run","tests/lib/foreign-oracle-names.mq.md","--backend",backend]).output().unwrap(); + assert!(out.status.success(),"{backend}: {}",String::from_utf8_lossy(&out.stderr)); + assert_eq!(String::from_utf8_lossy(&out.stdout).trim(),"decode\nzzz","{backend}"); + } +} diff --git a/tests/structure/lexical-sibling-calls.mq.md b/tests/structure/lexical-sibling-calls.mq.md new file mode 100644 index 0000000..b13ef53 --- /dev/null +++ b/tests/structure/lexical-sibling-calls.mq.md @@ -0,0 +1,47 @@ +--- +markup: occlusion +title: Lexical sibling calls, recursion and nearest declaration +--- + +# main + +**a = > first** +> print text=`a` +**b = > outer** +> print text=`b` +**c = > helper** +> print text=`c` + +## first + +**answer = > second** +*answer* + +## second + +**answer = > factorial n=5** +*answer* + +## factorial + + `n` + +1. `n` < 2 + *1* +2. * + **previous = n - 1** + **part = > factorial n=`previous`** + **answer = n * part** + *answer* + +## outer + +**answer = > helper** + +### helper + +*41* +*answer* + +## helper + +*99* diff --git a/tests/structure/self-method-paths.mq.md b/tests/structure/self-method-paths.mq.md new file mode 100644 index 0000000..43ca17f --- /dev/null +++ b/tests/structure/self-method-paths.mq.md @@ -0,0 +1,38 @@ +--- +markup: occlusion +title: Bare self and 自 method paths retain the receiver +import: + json: lib/json.mq.md +--- + +# Counter + +**state = > json.parse text={"n":41}** +*state* + +## read + +*> json.get value=`self` key="n"* + +## via + +*> self.read* + +## alias + +*> 自.read* + +## announce + +> self.via +*7* + +# main + +**c = > Counter** +**x = > `c`.via** +> print text=`x` +**y = > `c`.alias** +> print text=`y` +**z = > `c`.announce** +> print text=`z` diff --git a/verification/replay-policy/BUILD-IDENTITY.json b/verification/replay-policy/BUILD-IDENTITY.json new file mode 100644 index 0000000..e50ca1b --- /dev/null +++ b/verification/replay-policy/BUILD-IDENTITY.json @@ -0,0 +1,12 @@ +{ + "policy_sha256": "27b416847d06b130a066e0eb967a9b30c678e953c51d2ebe2bed9a8444ee0238", + "source_sha256": { + "ReplayPolicy/Cursor.lean": "cbc4c7d6769824148f3612e3fd3c23c0180d87b848fdec045ae2eeef08379048", + "Main.lean": "a133dd67e11d0fcd785badfcecb158937aaf484389e0d428af702fa0a4221e4a", + "ReplayPolicy.lean": "bd90ab6aed058d8983e7bbf8957d6f809930a11465fff3eac06d3ed257fa1213", + "lakefile.lean": "72f74dda45a2acb8c50266d642ceb48bf07669d7d79f73de113aa50d5cb62072", + "lean-toolchain": "ca5f1f601d34c49079e38d7e7041eb0f2a9c227fbc1df0f404820069a052330c", + "build_policy.py": "3bfb734e848a3aa4cc5fb9592dd86882ec0fd24f6134bceeae5c20aae1ef2fc3" + }, + "binary_sha256": "06843ebb5fc4ae35491b9cc125cb7053122a2b99970555b72cd23eec8129ec8b" +} diff --git a/verification/replay-policy/Main.lean b/verification/replay-policy/Main.lean new file mode 100644 index 0000000..f5cf842 --- /dev/null +++ b/verification/replay-policy/Main.lean @@ -0,0 +1,69 @@ +import Lean +import ReplayPolicy.Cursor +import ReplayPolicy.Identity + +/-! JSON-lines ABI for the executable proved policy. JSON transport is an +explicit adapter; all validation decisions call the proved functions. -/ +open Lean +open Marqdo.ReplayCursor + +structure StepWire where + q : String + r : String + deriving FromJson +structure PolicyCommand where + op : String + steps : List StepWire := [] + value : String := "" + deriving FromJson + +def reasonName : Reason → String + | .missing => "missing" + | .request => "request" + | .reply => "reply" + | .surplus => "surplus" + | .phase => "phase" + +def report (out : IO.FS.Stream) (c : Cursor String String) (allow : Bool) + (reason : Option Reason := none) : IO Unit := do + let why := match reason,c.phase with + | some r,_ => some r + | none,.stopped r => some r + | _,_ => none + let json := Json.mkObj [ + ("policy",toJson PolicyIdentity.digest), ("allow",toJson allow), ("matched",toJson c.matched), + ("remaining",toJson c.remaining.length), + ("reason",toJson (why.map reasonName))] + out.putStrLn json.compress + out.flush + +def readCommand (input : IO.FS.Stream) : IO (Option PolicyCommand) := do + let line ← input.getLine + if line.isEmpty then return none + let json ← IO.ofExcept (Json.parse line) + return some (← IO.ofExcept (fromJson? json)) + +partial def loop (input out : IO.FS.Stream) (c : Cursor String String) : IO Unit := do + let some command ← readCommand input | return + match command.op with + | "prepare" => + let (next,allowed) := prepare c command.value + report out next allowed + loop input out next + | "receive" => + let (next,accepted) := receive c command.value + report out next accepted + loop input out next + | "finish" => + let why := finish c + report out c why.isNone why + | _ => throw (IO.userError "replay policy: unknown command") + +def main : IO Unit := do + let input ← IO.getStdin + let out ← IO.getStdout + let some command ← readCommand input | throw (IO.userError "replay policy: missing initialization") + unless command.op == "init" do throw (IO.userError "replay policy: expected initialization") + let c : Cursor String String := ⟨command.steps.map (fun e => (e.q,e.r)),0,.ready⟩ + report out c true + loop input out c diff --git a/verification/replay-policy/POLICY-SHA256 b/verification/replay-policy/POLICY-SHA256 new file mode 100644 index 0000000..04dc300 --- /dev/null +++ b/verification/replay-policy/POLICY-SHA256 @@ -0,0 +1 @@ +27b416847d06b130a066e0eb967a9b30c678e953c51d2ebe2bed9a8444ee0238 diff --git a/verification/replay-policy/PolicyIdentity.lean b/verification/replay-policy/PolicyIdentity.lean new file mode 100644 index 0000000..453dc5a --- /dev/null +++ b/verification/replay-policy/PolicyIdentity.lean @@ -0,0 +1,3 @@ +namespace PolicyIdentity +def digest : String := "0cec24b295bfa5856fdb45584afaf317e4b20adef2365705458cdb3c3eba221f" +end PolicyIdentity diff --git a/verification/replay-policy/README.md b/verification/replay-policy/README.md new file mode 100644 index 0000000..d825d7d --- /dev/null +++ b/verification/replay-policy/README.md @@ -0,0 +1,30 @@ +# Executable replay policy + +Strict protocol replay uses this Lean policy to decide request permissions, +reply acceptance and final-history exhaustion. The Rust transport adapter does +not contain a second implementation of these decisions. Ordinary recording +does not start a policy process. + +Build the companion before building or testing strict replay: + +```sh +python3 verification/replay-policy/build_policy.py --lake /path/to/lake +cargo build --locked +``` + +The toolchain is pinned in `lean-toolchain`. The build writes `POLICY-SHA256`, +the compiled executable and `BUILD-IDENTITY.json`. Compile Rust after generating +the identity: its adapter rejects a companion with a different policy identity. +Do not modify policy sources during a build. + +For deployment, install the companion next to the marqdo executable as +`marqdo-replay-policy`. `MARQDO_REPLAY_POLICY` can select a trusted installation. +Development builds also find this package's `.lake/build/bin/replayPolicy`. +An unavailable, stale or malformed companion stops strict replay; there is no +fallback to the historical handwritten Rust policy. + +`ReplayPolicy/Cursor.lean` contains the executable transitions and their basic +invariants. The paper proof project imports this module and proves interaction- +program correspondence in `MarqdoProof/ReplayCursor.lean`. The compiler, process +transport, JSON/string encoding and host dispatch adapter remain explicit trust +interfaces. These files do not establish correctness of the full Rust frontend. diff --git a/verification/replay-policy/ReplayPolicy.lean b/verification/replay-policy/ReplayPolicy.lean new file mode 100644 index 0000000..d0f1d3e --- /dev/null +++ b/verification/replay-policy/ReplayPolicy.lean @@ -0,0 +1 @@ +import ReplayPolicy.Cursor diff --git a/verification/replay-policy/ReplayPolicy/Cursor.lean b/verification/replay-policy/ReplayPolicy/Cursor.lean new file mode 100644 index 0000000..c0366b7 --- /dev/null +++ b/verification/replay-policy/ReplayPolicy/Cursor.lean @@ -0,0 +1,132 @@ +import Lean + +namespace Marqdo.ReplayCursor + +inductive Reason where + | missing | request | reply | surplus | phase + deriving DecidableEq, Repr +inductive Phase where + | ready | pending | stopped (reason : Reason) + deriving DecidableEq, Repr +structure Cursor (Q R : Type) where + remaining : List (Q × R) + matched : Nat + phase : Phase + deriving Repr + +def halt (c : Cursor Q R) (why : Reason) : Cursor Q R := + match c.phase with + | .stopped _ => c + | _ => {c with phase := .stopped why} + +variable {Q R S V : Type} [DecidableEq Q] [DecidableEq R] + +def prepare (c : Cursor Q R) (request : Q) : Cursor Q R × Bool := + match c.phase with + | .stopped _ => (c,false) + | .pending => (halt c .phase,false) + | .ready => match c.remaining with + | [] => (halt c .missing,false) + | (wanted,_)::_ => if request = wanted then ({c with phase := .pending},true) + else (halt c .request,false) + +def receive (c : Cursor Q R) (actual : R) : Cursor Q R × Bool := + match c.phase with + | .stopped _ => (c,false) + | .ready => (halt c .phase,false) + | .pending => match c.remaining with + | [] => (halt c .phase,false) + | (_,wanted)::rest => + let consumed := {c with remaining := rest} + if actual = wanted then ({consumed with matched := c.matched+1, phase := .ready},true) + else (halt consumed .reply,false) + +def finish (c : Cursor Q R) : Option Reason := + match c.phase with + | .stopped why => some why + | .pending => some .phase + | .ready => if c.remaining.isEmpty then none else some .surplus + +def WellFormed (c : Cursor Q R) : Prop := c.phase = .pending → c.remaining ≠ [] + +omit [DecidableEq R] in +theorem prepare_wellFormed (c : Cursor Q R) (q : Q) : WellFormed (prepare c q).1 := by + cases hp : c.phase with + | stopped why => simp [prepare,hp,WellFormed] + | pending => simp [prepare,hp,halt,WellFormed] + | ready => + cases hs : c.remaining with + | nil => simp [prepare,hp,hs,halt,WellFormed] + | cons event rest => + rcases event with ⟨wanted,reply⟩ + by_cases h : q = wanted <;> simp [prepare,hp,hs,h,halt,WellFormed] + +omit [DecidableEq Q] in +theorem receive_wellFormed (c : Cursor Q R) (r : R) : WellFormed (receive c r).1 := by + cases hp : c.phase with + | stopped why => simp [receive,hp,WellFormed] + | ready => simp [receive,hp,halt,WellFormed] + | pending => + cases hs : c.remaining with + | nil => simp [receive,hp,hs,halt,WellFormed] + | cons event rest => + rcases event with ⟨q,wanted⟩ + by_cases h : r = wanted <;> simp [receive,hp,hs,h,halt,WellFormed] + +omit [DecidableEq R] in +theorem prepare_stopped (events : List (Q × R)) (n : Nat) (why : Reason) (q : Q) : + prepare ⟨events,n,.stopped why⟩ q = (⟨events,n,.stopped why⟩,false) := rfl +omit [DecidableEq Q] in +theorem receive_stopped (events : List (Q × R)) (n : Nat) (why : Reason) (r : R) : + receive ⟨events,n,.stopped why⟩ r = (⟨events,n,.stopped why⟩,false) := rfl + +omit [DecidableEq R] in +/-- A permit identifies exactly the head request; no assumption on replies. -/ +theorem permit_has_expected_request (c : Cursor Q R) (q : Q) + (granted : (prepare c q).2 = true) : + ∃ r rest, c.phase = .ready ∧ c.remaining = (q,r)::rest := by + cases hp : c.phase with + | stopped why => simp [prepare,hp] at granted + | pending => simp [prepare,hp] at granted + | ready => + cases hs : c.remaining with + | nil => simp [prepare,hp,hs] at granted + | cons event rest => + rcases event with ⟨wanted,reply⟩ + by_cases h : q = wanted + · subst wanted; exact ⟨reply,rest,rfl,rfl⟩ + · simp [prepare,hp,hs,h] at granted + + +omit [DecidableEq Q] [DecidableEq R] in +theorem finish_ready (events : List (Q × R)) (n : Nat) : + finish (Cursor.mk events n .ready) = none ↔ events = [] := by + cases events <;> simp [finish] + +omit [DecidableEq Q] in +theorem matching_reply_consumes_one (q : Q) (r : R) (rest : List (Q × R)) (n : Nat) : + receive (Cursor.mk ((q,r)::rest) n .pending) r = + (Cursor.mk rest (n+1) .ready,true) := by + simp [receive] + +omit [DecidableEq Q] in +theorem mismatching_reply_retains_match_count (q : Q) (wanted actual : R) + (rest : List (Q × R)) (n : Nat) (different : actual ≠ wanted) : + receive (Cursor.mk ((q,wanted)::rest) n .pending) actual = + (Cursor.mk rest n (.stopped .reply),false) := by + simp [receive,different,halt] + +omit [DecidableEq R] in +theorem prepare_preserves_history (c : Cursor Q R) (q : Q) : + (prepare c q).1.remaining = c.remaining ∧ (prepare c q).1.matched = c.matched := by + cases hp : c.phase with + | stopped why => simp [prepare,hp] + | pending => simp [prepare,hp,halt] + | ready => + cases hs : c.remaining with + | nil => simp [prepare,hp,hs,halt] + | cons event rest => + rcases event with ⟨wanted,r⟩ + by_cases h : q = wanted <;> simp [prepare,hp,hs,h,halt] + +end Marqdo.ReplayCursor diff --git a/verification/replay-policy/ReplayPolicy/Identity.lean b/verification/replay-policy/ReplayPolicy/Identity.lean new file mode 100644 index 0000000..6103fc6 --- /dev/null +++ b/verification/replay-policy/ReplayPolicy/Identity.lean @@ -0,0 +1,3 @@ +namespace PolicyIdentity +def digest : String := "27b416847d06b130a066e0eb967a9b30c678e953c51d2ebe2bed9a8444ee0238" +end PolicyIdentity diff --git a/verification/replay-policy/build_policy.py b/verification/replay-policy/build_policy.py new file mode 100644 index 0000000..c19870f --- /dev/null +++ b/verification/replay-policy/build_policy.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +"""Build the executable proved policy and bind its source identity to the ABI.""" +from pathlib import Path +import argparse,hashlib,json,shutil,subprocess +ROOT=Path(__file__).resolve().parent +p=argparse.ArgumentParser();p.add_argument('--lake',default=shutil.which('lake'));a=p.parse_args() +if not a.lake:raise SystemExit('lake is required to build the verified replay policy') +files=['ReplayPolicy/Cursor.lean','Main.lean','ReplayPolicy.lean','lakefile.lean','lean-toolchain','build_policy.py'] +hashes={name:hashlib.sha256((ROOT/name).read_bytes()).hexdigest() for name in files} +digest=hashlib.sha256(json.dumps(hashes,sort_keys=True,separators=(',',':')).encode()).hexdigest() +(ROOT/'POLICY-SHA256').write_text(digest+'\n') +(ROOT/'ReplayPolicy/Identity.lean').write_text('namespace PolicyIdentity\ndef digest : String := "'+digest+'"\nend PolicyIdentity\n') +result=subprocess.run([a.lake,'build','replayPolicy'],cwd=ROOT) +if result.returncode:raise SystemExit(result.returncode) +changed=[name for name,digest_before in hashes.items() + if hashlib.sha256((ROOT/name).read_bytes()).hexdigest()!=digest_before] +if changed:raise SystemExit('policy sources changed during build: '+', '.join(changed)) +binary=ROOT/'.lake/build/bin/replayPolicy' +(ROOT/'BUILD-IDENTITY.json').write_text(json.dumps(dict(policy_sha256=digest,source_sha256=hashes,binary_sha256=hashlib.sha256(binary.read_bytes()).hexdigest()),indent=2)+'\n') +print(binary) diff --git a/verification/replay-policy/lake-manifest.json b/verification/replay-policy/lake-manifest.json new file mode 100644 index 0000000..bdeee64 --- /dev/null +++ b/verification/replay-policy/lake-manifest.json @@ -0,0 +1,6 @@ +{"version": "1.3.0", + "packagesDir": ".lake/packages", + "packages": [], + "name": "replay_policy", + "lakeDir": ".lake", + "fixedToolchain": false} diff --git a/verification/replay-policy/lakefile.lean b/verification/replay-policy/lakefile.lean new file mode 100644 index 0000000..5252992 --- /dev/null +++ b/verification/replay-policy/lakefile.lean @@ -0,0 +1,7 @@ +import Lake +open Lake DSL +package replay_policy +@[default_target] +lean_lib ReplayPolicy +lean_exe replayPolicy where + root := `Main diff --git a/verification/replay-policy/lean-toolchain b/verification/replay-policy/lean-toolchain new file mode 100644 index 0000000..a6aff06 --- /dev/null +++ b/verification/replay-policy/lean-toolchain @@ -0,0 +1 @@ +leanprover/lean4:v4.35.0