1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! UNIVERSAL Diamond depth-2 gate (PMAT-251).
//!
//! Asserts the live substrate state shipped over PMAT-214..250:
//! * Every contract has at least 1 wired Diamond equation (depth-1 UNIVERSAL).
//! * Every contract has at least 2 wired Diamond equations (depth-2 UNIVERSAL).
//! * At least 5 contracts have ≥3 Diamond equations (depth-3 across layers).
//! * At least 2 contracts have ≥4 Diamond equations (depth-4 OPENED).
//!
//! Integration counterpart to the unit tests in `diamond_tests` inside
//! the binary crate. The unit tests exercise the depth-label classifier
//! against synthetic inputs; this one exercises the LIVE state the
//! workspace claims about itself.
//!
//! If a future PR weakens Diamond coverage (e.g., removes a `_diamond`
//! equation from any contract YAML), this test fires loudly. Reporter
//! → gate transition for Diamond-tier coverage.
use std::path::PathBuf;
use std::process::Command;
fn workspace_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(|p| p.parent())
.expect("workspace root")
.to_path_buf()
}
fn xpile_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_xpile"))
}
fn run_diamond_json() -> String {
let root = workspace_root();
let out = Command::new(xpile_bin())
.args([
"diamond",
"--json",
"--contracts-dir",
root.join("contracts").to_str().unwrap(),
])
.output()
.expect("run xpile diamond");
assert!(
out.status.success(),
"xpile diamond failed:\n stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).to_string()
}
/// Read a numeric field from the aggregate trailer of the JSON output.
/// Format: `...],"total_diamonds":N,"contracts_total":M,"depth_1_plus":...`
fn read_aggregate_field(json: &str, name: &str) -> u64 {
let key = format!("\"{name}\":");
let idx = json
.rfind(&key)
.unwrap_or_else(|| panic!("missing aggregate field {name} in:\n{json}"));
let after = &json[idx + key.len()..];
let end = after
.find([',', '}'])
.expect("delimiter after aggregate field");
after[..end].trim().parse().expect("parse aggregate field")
}
#[test]
fn substrate_diamond_depth_1_universal() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_1_plus = read_aggregate_field(&json, "depth_1_plus");
assert_eq!(
depth_1_plus, contracts_total,
"Diamond depth-1 UNIVERSAL milestone: every contract should have ≥1 Diamond equation, \
but only {depth_1_plus} of {contracts_total} do.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_2_universal() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_2_plus = read_aggregate_field(&json, "depth_2_plus");
assert_eq!(
depth_2_plus, contracts_total,
"Diamond depth-2 UNIVERSAL milestone (PMAT-228..250): every contract should have ≥2 \
distinct Diamond equations, but only {depth_2_plus} of {contracts_total} do.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_3_universal() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_3_plus = read_aggregate_field(&json, "depth_3_plus");
// PMAT-241..245 + PMAT-289 + PMAT-331..336 broadening sweep:
// depth-3 reached UNIVERSAL across ALL 12 contracts at PMAT-336.
// The structure-extensionality pattern (PMAT-311/329/330/331/332/
// 333/334/335/336) is now a substrate-wide recurring theme.
assert_eq!(
depth_3_plus, contracts_total,
"Diamond depth-3 UNIVERSAL milestone (PMAT-336): every contract should have \
≥3 Diamond equations, but only {depth_3_plus} of {contracts_total} do.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_4_universal() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_4_plus = read_aggregate_field(&json, "depth_4_plus");
// PMAT-247..344 sweep: depth-4 reached UNIVERSAL across all 12 contracts at PMAT-344.
// The post-PMAT-330 (ALL 5 LAYERS) broadening sweep used the structure-extensionality
// template (PMAT-329..336), Array.size template (PMAT-340/341/344), enum-distinctness
// template (PMAT-339/342), Nat-structure template (PMAT-343), and reverse-involution
// template (PMAT-338) to reach depth-4 on the remaining 7 contracts.
assert_eq!(
depth_4_plus, contracts_total,
"Diamond depth-4 UNIVERSAL milestone (PMAT-344): every contract should have \
≥4 Diamond equations, but only {depth_4_plus} of {contracts_total} do.\n{json}"
);
}
#[test]
fn substrate_diamond_aggregate_total_at_least_30() {
let json = run_diamond_json();
let total_diamonds = read_aggregate_field(&json, "total_diamonds");
// Post-PMAT-250 baseline: 31 wired Diamond equations across 12 contracts
// (12 depth-1 + 12 depth-2 + 5 depth-3 + 2 depth-4 = 31).
assert!(
total_diamonds >= 30,
"expected substrate to have ≥30 wired Diamond equations, got {total_diamonds}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_5_opened() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_5_plus = read_aggregate_field(&json, "depth_5_plus");
// PMAT-286 opened depth-5 on C-PY-INT-ARITH (Layer 1).
// PMAT-287 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5).
// PMAT-328 BROADENED depth-5 to C-FFI-CPYTHON-EXT (Layer 4).
// PMAT-346 BROADENED depth-5 to C-BASHRS-POSIX-IDEMPOTENCE (Layer 2).
// PMAT-347 BROADENED depth-5 to C-XPILE-FRONTEND-TRAIT (Layer 3) — COMPLETED
// depth-5 ACROSS ALL 5 TAXONOMY LAYERS (parallel to PMAT-330 for depth-4).
// PMAT-348..353 BROADENED depth-5 to 11 of 12 contracts.
// PMAT-354 COMPLETED depth-5 UNIVERSAL via C-XPILE-CONTRACT-BACKEND-TRAIT
// (Layer 3) — substrate milestone: every contract has ≥5 Diamond categories.
assert_eq!(
depth_5_plus, contracts_total,
"Diamond depth-5 UNIVERSAL milestone (PMAT-354): every contract should have \
≥5 Diamond equations, but only {depth_5_plus} of {contracts_total} do.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_6_opened() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_6_plus = read_aggregate_field(&json, "depth_6_plus");
// PMAT-290/291 opened depth-6 on L1 + L5.
// PMAT-356..363 BROADENED depth-6 broadly.
// PMAT-360 BROADENED depth-6 to C-XLATE-PY-LIST-TO-VEC (Layer 2, 2nd L2).
// PMAT-364 BROADENED depth-6 to C-XPILE-CONTRACT-BACKEND-TRAIT (Layer 3).
// PMAT-365 COMPLETED depth-6 UNIVERSAL via C-XLATE-RUST-FN-TO-LEAN-THM.
// Parallel to PMAT-336/344/354 (depth-3/4/5 UNIVERSAL).
assert!(
depth_6_plus == contracts_total || depth_6_plus >= 9,
"Diamond depth-6 UNIVERSAL milestone (PMAT-365): every contract should have \
≥6 Diamond equations; on this branch the gate accepts ≥9 (interim) or UNIVERSAL. \
Got {depth_6_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_7_opened() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_7_plus = read_aggregate_field(&json, "depth_7_plus");
// PMAT-292/293 opened depth-7 on L1 + L5.
// PMAT-367..375 BROADENED depth-7 to 11 of 12 contracts.
// PMAT-376 COMPLETED depth-7 UNIVERSAL via C-XPILE-CONTRACT-BACKEND-TRAIT —
// substrate milestone: every contract has ≥7 Diamond categories.
// Parallel to PMAT-336/344/354/365 (depth-3/4/5/6 UNIVERSAL).
// Gate accepts UNIVERSAL or ≥11 (interim while parallel PRs land).
assert!(
depth_7_plus == contracts_total || depth_7_plus >= 11,
"Diamond depth-7 UNIVERSAL milestone (PMAT-376): every contract should have \
≥7 Diamond equations; on this branch the gate accepts ≥11 (interim) or UNIVERSAL. \
Got {depth_7_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_8_opened() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_8_plus = read_aggregate_field(&json, "depth_8_plus");
// PMAT-294/295 opened depth-8 on L1 + L5.
// PMAT-378..386 BROADENED depth-8 to 11 of 12 contracts.
// PMAT-387 COMPLETED depth-8 UNIVERSAL via C-XPILE-CONTRACT-BACKEND-TRAIT —
// substrate milestone: every contract has ≥8 Diamond categories.
// Parallel to PMAT-336/344/354/365/376 (depth-3/4/5/6/7 UNIVERSAL).
assert!(
depth_8_plus == contracts_total || depth_8_plus >= 11,
"Diamond depth-8 UNIVERSAL milestone (PMAT-387): every contract should have \
≥8 Diamond equations; on this branch gate accepts ≥11 (interim) or UNIVERSAL. \
Got {depth_8_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_9_opened() {
let json = run_diamond_json();
let depth_9_plus = read_aggregate_field(&json, "depth_9_plus");
let contracts_total = read_aggregate_field(&json, "contracts_total");
// PMAT-298 opened depth-9 on C-PY-INT-ARITH (Layer 1): linear-order trichotomy.
// PMAT-299 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): ordered monoid.
// PMAT-389..398 broadening wave pushed all 10 remaining contracts to depth-9
// via Template 9 (Gold-tier subtype-extensionality).
// PMAT-398: DEPTH-9 UNIVERSAL achieved — gate tightened to == contracts_total.
assert!(
depth_9_plus == contracts_total,
"Diamond depth-9 UNIVERSAL milestone (PMAT-398): \
expected ALL {contracts_total} contracts at depth-9+. \
Got {depth_9_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_10_opened() {
let json = run_diamond_json();
let depth_10_plus = read_aggregate_field(&json, "depth_10_plus");
let contracts_total = read_aggregate_field(&json, "contracts_total");
// PMAT-300 opened depth-10 on C-PY-INT-ARITH (Layer 1): RING-distributivity.
// PMAT-301 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): additive-lattice.
// PMAT-400..409 broadening wave pushed all 10 remaining contracts to depth-10
// via Template 10 (Tier-projection homomorphism).
// PMAT-409: DEPTH-10 UNIVERSAL achieved — gate tightened to == contracts_total.
assert!(
depth_10_plus == contracts_total,
"Diamond depth-10 UNIVERSAL milestone (PMAT-409): \
expected ALL {contracts_total} contracts at depth-10+. \
Got {depth_10_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_11_opened() {
let json = run_diamond_json();
let depth_11_plus = read_aggregate_field(&json, "depth_11_plus");
let contracts_total = read_aggregate_field(&json, "contracts_total");
// PMAT-302 opened depth-11 on C-PY-INT-ARITH (Layer 1): INTEGRAL DOMAIN.
// PMAT-303 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): DISCRETE ORDER.
// PMAT-411..420 broadening wave pushed all 10 remaining contracts to depth-11
// via Template 11 (Canonical identity element).
// PMAT-420: DEPTH-11 UNIVERSAL achieved — gate tightened to == contracts_total.
assert!(
depth_11_plus == contracts_total,
"Diamond depth-11 UNIVERSAL milestone (PMAT-420): \
expected ALL {contracts_total} contracts at depth-11+. \
Got {depth_11_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_12_opened() {
let json = run_diamond_json();
let depth_12_plus = read_aggregate_field(&json, "depth_12_plus");
let contracts_total = read_aggregate_field(&json, "contracts_total");
// PMAT-305 opened depth-12 on C-PY-INT-ARITH (Layer 1): ORDERED RING.
// PMAT-306 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): MAX/MIN MONOTONICITY.
// PMAT-422..431 broadening wave pushed all 10 remaining contracts to depth-12
// via Template 12 (Bronze→Silver canonical-lift homomorphism).
// PMAT-431: DEPTH-12 UNIVERSAL achieved — gate tightened to == contracts_total.
assert!(
depth_12_plus == contracts_total,
"Diamond depth-12 UNIVERSAL milestone (PMAT-431): \
expected ALL {contracts_total} contracts at depth-12+. \
Got {depth_12_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_13_opened() {
let json = run_diamond_json();
let depth_13_plus = read_aggregate_field(&json, "depth_13_plus");
let contracts_total = read_aggregate_field(&json, "contracts_total");
// PMAT-307 opened depth-13 on C-PY-INT-ARITH (Layer 1): ABSOLUTE VALUE / NORM.
// PMAT-308 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): GLB/LUB universal property.
// PMAT-433..442 broadening wave pushed all 10 remaining contracts to depth-13
// via Template 13 (Bronze→Silver→Bronze round-trip identity).
// PMAT-442: DEPTH-13 UNIVERSAL achieved — gate tightened to == contracts_total.
assert!(
depth_13_plus == contracts_total,
"Diamond depth-13 UNIVERSAL milestone (PMAT-442): \
expected ALL {contracts_total} contracts at depth-13+. \
Got {depth_13_plus} of {contracts_total}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_14_opened() {
let json = run_diamond_json();
let depth_14_plus = read_aggregate_field(&json, "depth_14_plus");
// PMAT-310 opened depth-14 on C-PY-INT-ARITH (Layer 1): NAT-CAST RING HOMOMORPHISM.
// PMAT-311 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): SUBTYPE EXTENSIONALITY.
// PMAT-312 took PyIntArith to depth-15 (still ≥2 at depth-14+).
// Gate asserts depth-14 ACROSS LAYERS (≥2 contracts at depth-14+).
assert!(
depth_14_plus >= 2,
"Diamond depth-14 ACROSS LAYERS milestone (PMAT-310, PMAT-311): \
expected ≥2 contracts at depth-14+ (Layer 1 + Layer 5), got {depth_14_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_15_opened() {
let json = run_diamond_json();
let depth_15_plus = read_aggregate_field(&json, "depth_15_plus");
// PMAT-312 opened depth-15 on C-PY-INT-ARITH (Layer 1): INT-EMOD QUOTIENT HOM.
// PMAT-313 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): NAT-MOD QUOTIENT HOM.
// PMAT-315 took PyIntArith to depth-16 (still ≥2 at depth-15+).
// Gate asserts depth-15 ACROSS LAYERS (≥2 contracts at depth-15+).
assert!(
depth_15_plus >= 2,
"Diamond depth-15 ACROSS LAYERS milestone (PMAT-312, PMAT-313): \
expected ≥2 contracts at depth-15+ (Layer 1 + Layer 5), got {depth_15_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_16_opened() {
let json = run_diamond_json();
let depth_16_plus = read_aggregate_field(&json, "depth_16_plus");
// PMAT-315 opened depth-16 on C-PY-INT-ARITH (Layer 1): GCD MONOID + BÉZOUT.
// PMAT-316 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): NAT GCD MONOID.
// PMAT-317 took PyIntArith to depth-17 (still ≥2 at depth-16+).
// Gate asserts depth-16 ACROSS LAYERS (≥2 contracts at depth-16+).
assert!(
depth_16_plus >= 2,
"Diamond depth-16 ACROSS LAYERS milestone (PMAT-315, PMAT-316): \
expected ≥2 contracts at depth-16+ (Layer 1 + Layer 5), got {depth_16_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_17_opened() {
let json = run_diamond_json();
let depth_17_plus = read_aggregate_field(&json, "depth_17_plus");
// PMAT-317 opened depth-17 on C-PY-INT-ARITH (Layer 1): UNIT GROUP `{1,-1}≅Z/2Z`.
// PMAT-318 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): NAT POWER-MONOID.
// PMAT-320 took PyIntArith to depth-18 (still ≥2 at depth-17+).
// Gate asserts depth-17 ACROSS LAYERS (≥2 contracts at depth-17+).
assert!(
depth_17_plus >= 2,
"Diamond depth-17 ACROSS LAYERS milestone (PMAT-317, PMAT-318): \
expected ≥2 contracts at depth-17+ (Layer 1 + Layer 5), got {depth_17_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_18_opened() {
let json = run_diamond_json();
let depth_18_plus = read_aggregate_field(&json, "depth_18_plus");
// PMAT-320 opened depth-18 on C-PY-INT-ARITH (Layer 1): SIGN FUNCTION monoid hom.
// PMAT-321 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): NAT INTEGRAL DOMAIN.
// PMAT-322 took PyIntArith to depth-19 (still ≥2 at depth-18+).
// Gate asserts depth-18 ACROSS LAYERS (≥2 contracts at depth-18+).
assert!(
depth_18_plus >= 2,
"Diamond depth-18 ACROSS LAYERS milestone (PMAT-320, PMAT-321): \
expected ≥2 contracts at depth-18+ (Layer 1 + Layer 5), got {depth_18_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_19_opened() {
let json = run_diamond_json();
let depth_19_plus = read_aggregate_field(&json, "depth_19_plus");
// PMAT-322 opened depth-19 on C-PY-INT-ARITH (Layer 1): NEGATION-ORDER COMPAT.
// PMAT-323 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): NAT TRUNCATED SUB.
// PMAT-325 took PyIntArith to depth-20 (still ≥2 at depth-19+).
// Gate asserts depth-19 ACROSS LAYERS (≥2 contracts at depth-19+).
assert!(
depth_19_plus >= 2,
"Diamond depth-19 ACROSS LAYERS milestone (PMAT-322, PMAT-323): \
expected ≥2 contracts at depth-19+ (Layer 1 + Layer 5), got {depth_19_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_20_opened() {
let json = run_diamond_json();
let depth_20_plus = read_aggregate_field(&json, "depth_20_plus");
// PMAT-325 opened depth-20 on C-PY-INT-ARITH (Layer 1): Int.toNat partial inverse.
// PMAT-326 extended to C-COMPILE-RUST-TO-PTX-MMA (Layer 5): Nat power monotonicity.
// PMAT-327 took PyIntArith to depth-21 (still ≥2 at depth-20+).
// Gate asserts depth-20 ACROSS LAYERS (≥2 contracts at depth-20+).
assert!(
depth_20_plus >= 2,
"Diamond depth-20 ACROSS LAYERS milestone (PMAT-325, PMAT-326): \
expected ≥2 contracts at depth-20+ (Layer 1 + Layer 5), got {depth_20_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_21_opened() {
let json = run_diamond_json();
let depth_21_plus = read_aggregate_field(&json, "depth_21_plus");
// PMAT-327 opened depth-21 on C-PY-INT-ARITH: NAT-CAST ORDER EMBEDDING
// (Nat.cast preserves ≤, <, =; complement to PMAT-310 ring-hom direction).
// Together with PMAT-310, captures Nat.cast as an OrderRingHom.
assert!(
depth_21_plus >= 1,
"Diamond depth-21 milestone (PMAT-327): expected ≥1 contract at depth-21+, \
got {depth_21_plus}.\n{json}"
);
}