Skip to content

Commit 43a45a6

Browse files
committed
fix: fold PR #170 review — cfg kinds, canonical dump owner, oracle keys, null parity
CodeRabbit + Codex round on the first deliverable; all four confirmed: - cmd_cfg now passes collect_kinds(mod) into build_cfg like the checker path does (Codex P2): without it every kind-tagged resource exported resource_kind=null through the frozen seam. test_cfg_json pins a kind-tagged resource riding the projection (20/20). - cfg_json owns its canonicalization (CodeRabbit): new canonical_json() is the contract's textual form; __main__ prints it instead of an ad-hoc json.dumps at the call site. - oracle_exact verify now gates on BOTH manifest keys (Codex P2 + CodeRabbit): the reference key is a content hash of the reference implementation's source tree (--ref-src, default ownlang/) — not the git commit, which changes on every commit including ones that cannot affect reference behaviour. A signal death (negative returncode) counts as a crash even with no traceback text. Selftest adds the reference-edit staleness case (edit -> rc 2 -> restore -> parity). - own-ir absent-vs-null parity (Codex P2): a present JSON null is now REJECTED exactly where Python load() rejects it (reject_null deserializer on every load()-validated optional), and ACCEPTED AND PRESERVED on round-trip for the three fields Python checks with 'if x is not None' (subscription type/source_type, param effect) via a double-Option with a justified option_option allow. Two new tests pin both directions; 10/10. Gate green: run_tests, ruff, mypy ownlang, cargo test/clippy/fmt, oracle selftest.
1 parent a73a1f7 commit 43a45a6

6 files changed

Lines changed: 323 additions & 57 deletions

File tree

‎ownlang/__main__.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ def cmd_cfg(path: str, fmt: str = "human") -> int:
128128
rnames = {r.name for r in mod.resources}
129129
sigs = collect_signatures(mod)
130130
pols = collect_policies(mod)
131-
cfgs = [build_cfg(fn, rnames, sigs, pols)[0] for fn in mod.functions]
131+
kinds = collect_kinds(mod)
132+
cfgs = [build_cfg(fn, rnames, sigs, pols, kinds)[0] for fn in mod.functions]
132133
if fmt == "json":
133134
# The canonical CFG-layer oracle seam (P-022 step 0): a frozen,
134135
# deterministic JSON contract the Rust port is diffed against. The
135-
# human dump below stays a debug view, not a contract.
136-
import json
137-
138-
from .cfg_json import module_cfg_json
139-
print(json.dumps(module_cfg_json(cfgs), indent=2, sort_keys=True))
136+
# human dump below stays a debug view, not a contract. Canonical text
137+
# (sorted keys) is the contract's own dump, not an ad-hoc json.dumps.
138+
from .cfg_json import canonical_json
139+
print(canonical_json(cfgs))
140140
return 0
141141
for cfg in cfgs:
142142
_print_cfg(cfg)

‎ownlang/cfg_json.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,22 @@ def cfg_json(cfg: CFG) -> dict[str, Any]:
165165

166166
def module_cfg_json(cfgs: list[CFG]) -> dict[str, Any]:
167167
"""The whole module's CFGs as one versioned document — the unit the oracle
168-
diffs at the CFG layer."""
168+
diffs at the CFG layer.
169+
170+
NOTE: the returned dict is deterministic in *content*, but canonical **text**
171+
requires ``sort_keys=True`` at dump time — use :func:`canonical_json` rather
172+
than calling ``json.dumps`` yourself, or the seam's byte-identity property
173+
silently degrades to value-identity."""
169174
return {
170175
"ownlang_cfg_version": CFG_JSON_VERSION,
171176
"functions": [cfg_json(c) for c in cfgs],
172177
}
178+
179+
180+
def canonical_json(cfgs: list[CFG]) -> str:
181+
"""The canonical textual form of the seam — what `cfg --format json` prints
182+
and what the oracle byte-compares. Canonicalization (sorted keys, fixed
183+
indent) lives HERE, with the contract, not at call sites."""
184+
import json
185+
186+
return json.dumps(module_cfg_json(cfgs), indent=2, sort_keys=True)

‎rust/crates/own-ir/src/lib.rs‎

Lines changed: 202 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ impl std::fmt::Display for OwnIrError {
4242

4343
impl std::error::Error for OwnIrError {}
4444

45+
/// Deserializer for load()-validated optional fields: **absent** means default
46+
/// (Python's `d.get("f", default)`), but a **present `null` is rejected** —
47+
/// exactly like Python's `isinstance` check failing on `None`. `serde(default)`
48+
/// handles absence before this runs; here a null hits `T::deserialize` and
49+
/// errors.
50+
fn reject_null<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
51+
where
52+
D: serde::Deserializer<'de>,
53+
T: Deserialize<'de>,
54+
{
55+
T::deserialize(de).map(Some)
56+
}
57+
58+
/// Deserializer for the three fields Python checks with `if x is not None and
59+
/// not isinstance(...)` — a present `null` is **accepted** there, and the value
60+
/// stays `null` in the document, so round-trip must preserve it. Outer `None` =
61+
/// absent (skipped on serialize); `Some(None)` = explicit null (serialized as
62+
/// `null`); `Some(Some(v))` = a value.
63+
#[allow(clippy::option_option)] // the 3 states ARE the contract: absent / explicit null / value
64+
fn nullable<'de, D, T>(de: D) -> Result<Option<Option<T>>, D::Error>
65+
where
66+
D: serde::Deserializer<'de>,
67+
T: Deserialize<'de>,
68+
{
69+
Option::<T>::deserialize(de).map(Some)
70+
}
71+
4572
/// DI registration lifetime — the only closed vocabulary inside the facts
4673
/// (`ownlang/di.py::LIFETIMES`).
4774
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -66,11 +93,24 @@ pub enum ParamEffect {
6693
/// One `{type, file, line}` call-site record (DI004 / DI005 metadata).
6794
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
6895
pub struct Site {
69-
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
96+
#[serde(
97+
rename = "type",
98+
default,
99+
deserialize_with = "reject_null",
100+
skip_serializing_if = "Option::is_none"
101+
)]
70102
pub type_name: Option<String>,
71-
#[serde(skip_serializing_if = "Option::is_none")]
103+
#[serde(
104+
default,
105+
deserialize_with = "reject_null",
106+
skip_serializing_if = "Option::is_none"
107+
)]
72108
pub file: Option<String>,
73-
#[serde(skip_serializing_if = "Option::is_none")]
109+
#[serde(
110+
default,
111+
deserialize_with = "reject_null",
112+
skip_serializing_if = "Option::is_none"
113+
)]
74114
pub line: Option<i64>,
75115
#[serde(flatten)]
76116
pub extra: Map<String, Value>,
@@ -79,20 +119,37 @@ pub struct Site {
79119
/// One event subscription inside a component.
80120
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
81121
pub struct Subscription {
82-
#[serde(skip_serializing_if = "Option::is_none")]
122+
#[serde(
123+
default,
124+
deserialize_with = "reject_null",
125+
skip_serializing_if = "Option::is_none"
126+
)]
83127
pub resource: Option<String>,
84-
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
85-
pub type_name: Option<String>,
86-
#[serde(skip_serializing_if = "Option::is_none")]
87-
pub source_type: Option<String>,
128+
#[serde(
129+
rename = "type",
130+
default,
131+
deserialize_with = "nullable",
132+
skip_serializing_if = "Option::is_none"
133+
)]
134+
pub type_name: Option<Option<String>>,
135+
#[serde(
136+
default,
137+
deserialize_with = "nullable",
138+
skip_serializing_if = "Option::is_none"
139+
)]
140+
pub source_type: Option<Option<String>>,
88141
#[serde(flatten)]
89142
pub extra: Map<String, Value>,
90143
}
91144

92145
/// One component (a view model / window / control the extractor saw).
93146
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
94147
pub struct Component {
95-
#[serde(skip_serializing_if = "Option::is_none")]
148+
#[serde(
149+
default,
150+
deserialize_with = "reject_null",
151+
skip_serializing_if = "Option::is_none"
152+
)]
96153
pub subscriptions: Option<Vec<Subscription>>,
97154
#[serde(flatten)]
98155
pub extra: Map<String, Value>,
@@ -103,27 +160,71 @@ pub struct Component {
103160
pub struct Service {
104161
pub lifetime: Lifetime,
105162
pub name: String,
106-
#[serde(skip_serializing_if = "Option::is_none")]
163+
#[serde(
164+
default,
165+
deserialize_with = "reject_null",
166+
skip_serializing_if = "Option::is_none"
167+
)]
107168
pub deps: Option<Vec<String>>,
108-
#[serde(skip_serializing_if = "Option::is_none")]
169+
#[serde(
170+
default,
171+
deserialize_with = "reject_null",
172+
skip_serializing_if = "Option::is_none"
173+
)]
109174
pub weak_deps: Option<Vec<String>>,
110-
#[serde(skip_serializing_if = "Option::is_none")]
175+
#[serde(
176+
default,
177+
deserialize_with = "reject_null",
178+
skip_serializing_if = "Option::is_none"
179+
)]
111180
pub root_resolves: Option<Vec<String>>,
112-
#[serde(skip_serializing_if = "Option::is_none")]
181+
#[serde(
182+
default,
183+
deserialize_with = "reject_null",
184+
skip_serializing_if = "Option::is_none"
185+
)]
113186
pub file: Option<String>,
114-
#[serde(skip_serializing_if = "Option::is_none")]
187+
#[serde(
188+
default,
189+
deserialize_with = "reject_null",
190+
skip_serializing_if = "Option::is_none"
191+
)]
115192
pub line: Option<i64>,
116-
#[serde(skip_serializing_if = "Option::is_none")]
193+
#[serde(
194+
default,
195+
deserialize_with = "reject_null",
196+
skip_serializing_if = "Option::is_none"
197+
)]
117198
pub ctor_file: Option<String>,
118-
#[serde(skip_serializing_if = "Option::is_none")]
199+
#[serde(
200+
default,
201+
deserialize_with = "reject_null",
202+
skip_serializing_if = "Option::is_none"
203+
)]
119204
pub ctor_line: Option<i64>,
120-
#[serde(skip_serializing_if = "Option::is_none")]
205+
#[serde(
206+
default,
207+
deserialize_with = "reject_null",
208+
skip_serializing_if = "Option::is_none"
209+
)]
121210
pub ctor_type: Option<String>,
122-
#[serde(skip_serializing_if = "Option::is_none")]
211+
#[serde(
212+
default,
213+
deserialize_with = "reject_null",
214+
skip_serializing_if = "Option::is_none"
215+
)]
123216
pub root_resolve_sites: Option<Vec<Site>>,
124-
#[serde(skip_serializing_if = "Option::is_none")]
217+
#[serde(
218+
default,
219+
deserialize_with = "reject_null",
220+
skip_serializing_if = "Option::is_none"
221+
)]
125222
pub scope_cached: Option<Vec<String>>,
126-
#[serde(skip_serializing_if = "Option::is_none")]
223+
#[serde(
224+
default,
225+
deserialize_with = "reject_null",
226+
skip_serializing_if = "Option::is_none"
227+
)]
127228
pub scope_cache_sites: Option<Vec<Site>>,
128229
#[serde(flatten)]
129230
pub extra: Map<String, Value>,
@@ -132,13 +233,29 @@ pub struct Service {
132233
/// One reactive-effect binding row (P-020).
133234
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
134235
pub struct Binding {
135-
#[serde(skip_serializing_if = "Option::is_none")]
236+
#[serde(
237+
default,
238+
deserialize_with = "reject_null",
239+
skip_serializing_if = "Option::is_none"
240+
)]
136241
pub name: Option<String>,
137-
#[serde(skip_serializing_if = "Option::is_none")]
242+
#[serde(
243+
default,
244+
deserialize_with = "reject_null",
245+
skip_serializing_if = "Option::is_none"
246+
)]
138247
pub init: Option<String>,
139-
#[serde(skip_serializing_if = "Option::is_none")]
248+
#[serde(
249+
default,
250+
deserialize_with = "reject_null",
251+
skip_serializing_if = "Option::is_none"
252+
)]
140253
pub refs: Option<Vec<String>>,
141-
#[serde(skip_serializing_if = "Option::is_none")]
254+
#[serde(
255+
default,
256+
deserialize_with = "reject_null",
257+
skip_serializing_if = "Option::is_none"
258+
)]
142259
pub line: Option<i64>,
143260
#[serde(flatten)]
144261
pub extra: Map<String, Value>,
@@ -147,13 +264,29 @@ pub struct Binding {
147264
/// One reactive effect (P-020, EFF001).
148265
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
149266
pub struct Effect {
150-
#[serde(skip_serializing_if = "Option::is_none")]
267+
#[serde(
268+
default,
269+
deserialize_with = "reject_null",
270+
skip_serializing_if = "Option::is_none"
271+
)]
151272
pub deps: Option<Vec<String>>,
152-
#[serde(skip_serializing_if = "Option::is_none")]
273+
#[serde(
274+
default,
275+
deserialize_with = "reject_null",
276+
skip_serializing_if = "Option::is_none"
277+
)]
153278
pub io: Option<bool>,
154-
#[serde(skip_serializing_if = "Option::is_none")]
279+
#[serde(
280+
default,
281+
deserialize_with = "reject_null",
282+
skip_serializing_if = "Option::is_none"
283+
)]
155284
pub line: Option<i64>,
156-
#[serde(skip_serializing_if = "Option::is_none")]
285+
#[serde(
286+
default,
287+
deserialize_with = "reject_null",
288+
skip_serializing_if = "Option::is_none"
289+
)]
157290
pub bindings: Option<Vec<Binding>>,
158291
#[serde(flatten)]
159292
pub extra: Map<String, Value>,
@@ -163,10 +296,18 @@ pub struct Effect {
163296
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
164297
pub struct Param {
165298
pub name: String,
166-
#[serde(skip_serializing_if = "Option::is_none")]
299+
#[serde(
300+
default,
301+
deserialize_with = "reject_null",
302+
skip_serializing_if = "Option::is_none"
303+
)]
167304
pub line: Option<i64>,
168-
#[serde(skip_serializing_if = "Option::is_none")]
169-
pub effect: Option<ParamEffect>,
305+
#[serde(
306+
default,
307+
deserialize_with = "nullable",
308+
skip_serializing_if = "Option::is_none"
309+
)]
310+
pub effect: Option<Option<ParamEffect>>,
170311
#[serde(flatten)]
171312
pub extra: Map<String, Value>,
172313
}
@@ -175,7 +316,11 @@ pub struct Param {
175316
/// untyped here — their vocabulary is the bridge's concern, not the schema's.
176317
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
177318
pub struct Function {
178-
#[serde(skip_serializing_if = "Option::is_none")]
319+
#[serde(
320+
default,
321+
deserialize_with = "reject_null",
322+
skip_serializing_if = "Option::is_none"
323+
)]
179324
pub params: Option<Vec<Param>>,
180325
#[serde(flatten)]
181326
pub extra: Map<String, Value>,
@@ -184,15 +329,35 @@ pub struct Function {
184329
/// The `OwnIR` document root.
185330
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
186331
pub struct OwnIr {
187-
#[serde(skip_serializing_if = "Option::is_none")]
332+
#[serde(
333+
default,
334+
deserialize_with = "reject_null",
335+
skip_serializing_if = "Option::is_none"
336+
)]
188337
pub ownir_version: Option<i64>,
189-
#[serde(skip_serializing_if = "Option::is_none")]
338+
#[serde(
339+
default,
340+
deserialize_with = "reject_null",
341+
skip_serializing_if = "Option::is_none"
342+
)]
190343
pub components: Option<Vec<Component>>,
191-
#[serde(skip_serializing_if = "Option::is_none")]
344+
#[serde(
345+
default,
346+
deserialize_with = "reject_null",
347+
skip_serializing_if = "Option::is_none"
348+
)]
192349
pub services: Option<Vec<Service>>,
193-
#[serde(skip_serializing_if = "Option::is_none")]
350+
#[serde(
351+
default,
352+
deserialize_with = "reject_null",
353+
skip_serializing_if = "Option::is_none"
354+
)]
194355
pub effects: Option<Vec<Effect>>,
195-
#[serde(skip_serializing_if = "Option::is_none")]
356+
#[serde(
357+
default,
358+
deserialize_with = "reject_null",
359+
skip_serializing_if = "Option::is_none"
360+
)]
196361
pub functions: Option<Vec<Function>>,
197362
#[serde(flatten)]
198363
pub extra: Map<String, Value>,

0 commit comments

Comments
 (0)