|
| 1 | +# State protocols × effect summaries: the missing wire for harmless calls in a region |
| 2 | + |
| 3 | +Status: **BLOCKED BY SUMMARY INFRASTRUCTURE** — a kill-first finding, no code |
| 4 | +change. Measured at `main` = `a27a8277b40309aa56b945e73dd305ddb90e72b4`. |
| 5 | + |
| 6 | +Question: can `ProtocolLowering` let a *provably harmless* call stand inside a |
| 7 | +state-protocol region by consuming the existing P-036/P-037 summary layer, |
| 8 | +without weakening the fail-closed default? |
| 9 | + |
| 10 | + unknown / no summary -> refusal (as today) |
| 11 | + summary proves harmless -> allowed |
| 12 | + summary may mutate / escape the entity -> refusal or verdict, never clean |
| 13 | + |
| 14 | +Answer: **not with what exists.** The only summary the repository computes |
| 15 | +does not carry the property, and there is no channel through which the lowering |
| 16 | +could consult a core-side summary without an OwnIR vocabulary change. Building |
| 17 | +the proof inside the extractor would be the typestate-only purity analyzer this |
| 18 | +work must not create. What follows is the exact gap and the smallest slice that |
| 19 | +closes it. |
| 20 | + |
| 21 | +## 1. What exists |
| 22 | + |
| 23 | +| Layer | What it summarizes | Can it prove "harmless"? | |
| 24 | +|---|---|---| |
| 25 | +| MOS (`ownlang/ownership.py`, `own-analysis` port), P-005 D5 | `Transfer ∈ {no, must, may, unknown}` per **disposable** parameter, plus owned-return kind | **No.** Non-disposable methods get no record; `borrow` and `borrow_mut` both seed `borrow` (`_build_skeletons`); the `escapes` axis is reserved with **no producer** (TZ D2); no heap/static/receiver effect at all | |
| 26 | +| P-037 guarded transfer (A1 in progress; A2.1 sidecar inert) | The same `Transfer` lattice, split on one bool/null guard | **No.** A refinement of the transfer axis, not a new axis | |
| 27 | +| P-036 `ParameterEffect = … BorrowMut \| MayEscape \| Unknown`, `FieldEffects`, region `Escapes(...)` | Named in the design (ownership/region summary sections) | Would be enough — but it is **design only**, scheduled under P-036 Phase 5 ("#122 cross-method exclusivity"), and has no producer, solver or schema | |
| 28 | +| `ProtocolLowering.CheckBoundary.Effects` (extractor) | Writes to the protocol's *entity family* and the family methods reached, for **admission** | Not a candidate: protocol-local, blind to statics, and extending it into a general effect proof is exactly the purity-analyzer-for-typestate this work rules out | |
| 29 | + |
| 30 | +### Kill-first probe (reproducible) |
| 31 | + |
| 32 | +Sample API (`frontend/roslyn/protocol-samples/Api`) plus: |
| 33 | + |
| 34 | +```csharp |
| 35 | +public static class Helpers |
| 36 | +{ |
| 37 | + private static int _counter; |
| 38 | + private static Order? _kept; |
| 39 | + public static int Twice(int x) => x * 2; // harmless |
| 40 | + public static void Mutates(Order o) => o.Annotate("x"); // mutates the entity |
| 41 | + public static void Escapes(Order o) => _kept = o; // escapes the entity |
| 42 | + public static void TouchesGlobalState() => _counter++; // global write, no entity |
| 43 | +} |
| 44 | +// each inside its own Protocol.WithApproved(order, approved => { <call>; approved.Ship(); }); |
| 45 | +// plus Console.WriteLine("shipping") as the unknown external call |
| 46 | +``` |
| 47 | + |
| 48 | +Extractor at `a27a827` (`--flow-locals`): exit **2**, refusals at |
| 49 | +`Twice`, `TouchesGlobalState`, `Console.WriteLine` ("runs code with no stated |
| 50 | +contract"). `Mutates(order)` / `Escapes(order)` are not refused: the entity |
| 51 | +argument lowers as `use order`, and the core rejects it with a verdict. So the |
| 52 | +fail-closed contract holds today, and the one case the slice wants to open |
| 53 | +(`Twice`) is refused for the right reason: nothing proves it harmless. |
| 54 | + |
| 55 | +The same helpers run through the MOS layer (`ownsharp-extract … --flow-locals` |
| 56 | +then `python -m ownlang summaries`), with disposable twins added because MOS |
| 57 | +sees nothing else: |
| 58 | + |
| 59 | +| Method | MOS summary | |
| 60 | +|---|---| |
| 61 | +| `Twice(int)`, `Mutates(Order)`, `Escapes(Order)`, `TouchesGlobalState()` | **no record** (no disposable) | |
| 62 | +| `ReadsStream(Stream s) { var n = s.Length; }` | **no record** | |
| 63 | +| `WritesStream(Stream s) { s.WriteByte(1); }` | `transfer: no` | |
| 64 | +| `EscapesStream(Stream s) { _keptStream = s; }` | `transfer: no` | |
| 65 | + |
| 66 | +A writer and an escaper solve to the same value a reader would. Using MOS as |
| 67 | +the proof would allow `EscapesStream`-shaped calls into a region and call them |
| 68 | +clean. **MOS cannot be the proof, by construction**: `no` means "ownership did |
| 69 | +not leave the caller", not "nothing was touched". |
| 70 | + |
| 71 | +## 2. Why the decision cannot simply move into the core |
| 72 | + |
| 73 | +The summaries are solved in the core, from facts; the refusal happens in the |
| 74 | +extractor, before any fact exists. Deferring the decision means lowering the |
| 75 | +region call as something the core must check. The OwnIR rules close both |
| 76 | +spellings at v1 (spec/OwnIR.md §2, IR3/IR4): |
| 77 | + |
| 78 | +- a new op (`opaque_call`, say) is a flow-op vocabulary change — **version bump**; |
| 79 | +- a plain `call` with an additive `requires: harmless` field changes what `call` |
| 80 | + means inside `borrow_mut`; a core that does not read the field (or reads it |
| 81 | + before the summary domain exists) sees a call to an extern with no tracked |
| 82 | + argument — i.e. **nothing** — and `TouchesGlobalState` becomes clean. That is |
| 83 | + fail-open, which the additive rule exists to forbid. |
| 84 | + |
| 85 | +So two things are missing, and the second is not this work's to decide. |
| 86 | + |
| 87 | +## 3. The missing contract |
| 88 | + |
| 89 | + ProtocolLowering (or the core on its behalf) needs, per call site in a region: |
| 90 | + |
| 91 | + callee — resolved, first-party source, statically bound |
| 92 | + (non-virtual, or sealed / devirtualized; else Unknown) |
| 93 | + -> solved HeapEffectSummary (a P-036 summary domain, SCC fixpoint, |
| 94 | + Unknown absorbing, Python + Rust parity) |
| 95 | + -> Harmless(callee) := |
| 96 | + every parameter ∈ {Plain, Borrow} (no BorrowMut, no MayEscape) |
| 97 | + ∧ receiver ∈ {none, Borrow} |
| 98 | + ∧ FieldEffects.write = ∅ (no static, field, array-element |
| 99 | + or ref/out write, transitively) |
| 100 | + ∧ return is not AliasOf(parameter | heap) unless its type is inert |
| 101 | + ∧ closed: no Unknown / extern / unresolved-virtual callee in the closure |
| 102 | + absent summary or any Unknown -> refusal, exactly as today |
| 103 | + |
| 104 | +Notes on the predicate: |
| 105 | + |
| 106 | +- It is a property of **all** parameters, not just disposable ones, and of |
| 107 | + **statics** — the `Backdoor.AnnotateLast()` shape (R9) reaches the entity with no |
| 108 | + argument at all. A call is never allowed because it "does not take the entity". |
| 109 | +- Reads are allowed (an exclusive region forbids change, not observation); a read |
| 110 | + that *yields* an entity-typed value is still caught by the lowering's |
| 111 | + exclusivity-by-type rule on the call's result. |
| 112 | +- Arguments keep today's lowering: an entity-typed argument is still a `use` of |
| 113 | + the entity (a verdict), whatever the summary says. The summary only replaces |
| 114 | + the *refusal* of a call that touched nothing visible. |
| 115 | + |
| 116 | +## 4. Minimal next slice |
| 117 | + |
| 118 | +**H0 — heap-effect summary domain, inert.** No verdict change, no OwnIR bump. |
| 119 | +Precedent: P-037 A2.1 ("emit the guarded-fact sidecar from the Roslyn |
| 120 | +extractor, validated and inert"). |
| 121 | + |
| 122 | +1. Extractor: an additive optional sidecar of per-method **local** heap-effect |
| 123 | + evidence for source methods — writes to statics / fields / array elements / |
| 124 | + ref-out params, stores of a parameter into the heap, direct callees (with |
| 125 | + `virtual`/extern flagged). Facts only (IR6): no solving in C#. |
| 126 | +2. Core (Python reference + Rust port): a second domain in the summary envelope, |
| 127 | + solved by the same SCC condensation as MOS, `Unknown` absorbing; serialized in |
| 128 | + `own summaries` under its own key so the MOS parity artifact is untouched. |
| 129 | +3. Acceptance — the probe above as fixtures: `Twice` → harmless; |
| 130 | + `Mutates` → `BorrowMut(0)`; `Escapes` → `MayEscape(0)` + static write; |
| 131 | + `TouchesGlobalState` → static write; `Console.WriteLine` → Unknown; a |
| 132 | + recursive pure pair → harmless; a pure helper calling an unknown → Unknown. |
| 133 | + Python/Rust parity fixture; `tests/run_tests.py`; Rust fmt/clippy/tests. |
| 134 | + Nothing reads the domain yet, so every existing verdict and refusal is |
| 135 | + byte-identical. |
| 136 | + |
| 137 | +**H1 — the wire (needs an owner ruling first).** Region calls with no visible |
| 138 | +entity touch lower to a must-understand construct instead of a refusal; the core |
| 139 | +applies `Harmless(callee)` and emits a refusal-equivalent error otherwise. This |
| 140 | +is verdict-changing (a refusal becomes clean) and needs one of: an OwnIR v2 op, |
| 141 | +or an explicitly ruled exception to IR3 — both outside this task's limits. Until |
| 142 | +H1, `refused/R9_opaque_call_inside_region` and the probe's `Twice` case stay |
| 143 | +refused. |
| 144 | + |
| 145 | +## 5. Untouched |
| 146 | + |
| 147 | +OwnIR version, the token/region model, the trust boundary, OWN053, T0/perf, the |
| 148 | +DB-side known gaps, the unknown-call fail-closed default, and the P-022 / P-037 |
| 149 | +freezes and amendments: this note changes no code and no fixture. |
0 commit comments