omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#291 (code only)
Apply it
git checkout -b patch-7a419c61dba135c0 a9b9d0d9f58e20581a06121eb94c53a624276e2c curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/7a419c61dba135c0.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
291
d56cd81b797caa6cda13b268d40c0adfaaa9011d
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":291,"url":"https://github.com/aien-dev/omega-mv/pull/291","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:10Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 828f4250d..7bd530f11 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "b2164e33bc82e720bdaf1d26820682308a051aa6a108457b6d2a367529b26093", - "last_commit": "e9c2829", + "digest": "2b90281fb966737f5cfca02ad640c02326422b4146bca69cdbae095785ce2939", + "last_commit": "84b8efb", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "052b73debc780209673d2f422317d17cd85a45084ea9e97bd11900d2c633bf7f", - "last_commit": "30c65a3", + "digest": "1a09c53fc4d21cd13d4f307282aeabcbf896a0639618c8767a3011d49d4cc613", + "last_commit": "84b8efb", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -99,21 +99,21 @@ "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "59136def1fad3d627f93534c3e5e4a8d93a57c12f10fab5dce0221c6e6c3590b", - "last_commit": "a81f1ef", + "digest": "fcf0d77d97acf0c597b39c054307650cff03295797be8686f7aeaf634ba5c2b8", + "last_commit": "84b8efb", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:abd08092105a0d71476434df3aa2109fd32fea34bb8f3e674ff51e07564d49ff", - "compiled_at": "2026-10-05T14:02:24Z", + "children_root": "sha256:be98693d48cc260552b701dcb16310940951090c323e3ef71e111d29b432d523", + "compiled_at": "2026-10-05T14:06:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:3b7d3673512051ee469067e55d555c4719f227dbd34bbd47bd7c21f55969ec67", + "digest": "sha256:2b3977f6ffc8a26a56977c451a50dd399ef3faa63652c71d686a85edd339b746", "evidence_files": 2749, "evidence_root": "sha256:c6e60c5b2803cfddf6a1fed130c92066d285ddb68aeea0bc65d58668cf139dfc", - "files": 4530, - "generated_at_commit": "cda86de62e7741afb536e0adf517752993f55440", - "source_tree": "sha256:1abe3ba864e9ff8aa5d0f0f894a2dd67e3ed0b3ca54890d3aa0e7cea0180e6b9" + "files": 4532, + "generated_at_commit": "84b8efb7b8cf2f6d7b3bd6c889d44c28c9e41e9f", + "source_tree": "sha256:89bbca0c2dbc0b31c5c074bb1ed807b50bd743a5b165078b9394682864f7004c" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 9c31e11c4..88c01185b 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -75,8 +75,8 @@ "purpose": "DUAL (ARCH-0031) documents in omega: applicability experiment method and findings (APPLICABILITY" }, { - "digest": "439dff6418bd216f90b1d656c6edf055f25ee69b48c39a5862dcac69cab6bfb9", - "last_commit": "10ca03b", + "digest": "15c85e0bc41f6e3934e81753d361dea468177df79cb01b233e6a788af2a1dfb5", + "last_commit": "e9c2829", "name": "estimation", "purpose": "Directory docs/estimation. Purpose not yet described by a human." }, @@ -117,15 +117,15 @@ "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:ccb48faf9291f1fa5e2959db39612fddd9fea22e8dfef18cbaf62232ef600b9b", - "compiled_at": "2026-10-05T13:47:02Z", + "children_root": "sha256:eb93b138839b7bcbe693b0e315a1af8df7f1b82ea1bc7e313bfa606a893e7f6a", + "compiled_at": "2026-10-05T13:53:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:b2164e33bc82e720bdaf1d26820682308a051aa6a108457b6d2a367529b26093", + "digest": "sha256:2b90281fb966737f5cfca02ad640c02326422b4146bca69cdbae095785ce2939", "evidence_files": 31, "evidence_root": "sha256:3f4d8ab7c723c9fddacc5a872541933a4b87d1bb39868b8ec376320db6c929af", - "files": 107, - "generated_at_commit": "10ca03b5c0595dbebdade269fe828318c2d6f1b0", - "source_tree": "sha256:21d94c0a792e4f352f1235060c19d4cb6f60be912d8c1a4b3631d79b21585db5" + "files": 108, + "generated_at_commit": "e9c28294247e2add87335d615c762db9bbc0e48d", + "source_tree": "sha256:b2fab0ee7afbb68b3228f7830c4245a45c681aeaee3f60a88e65fbfccbd4de15" }, "owner_lane": "", "provenance": { diff --git a/docs/estimation/.crumb b/docs/estimation/.crumb index e37e24a7f..75b89d5f1 100644 --- a/docs/estimation/.crumb +++ b/docs/estimation/.crumb @@ -24,14 +24,14 @@ } ], "children_root": "sha256:35cd3af03e079556db7f3ebcfd7590fa68659e84dd603795ae6b5b048dc2af0d", - "compiled_at": "2026-10-05T13:47:02Z", + "compiled_at": "2026-10-05T13:53:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:439dff6418bd216f90b1d656c6edf055f25ee69b48c39a5862dcac69cab6bfb9", + "digest": "sha256:15c85e0bc41f6e3934e81753d361dea468177df79cb01b233e6a788af2a1dfb5", "evidence_files": 31, "evidence_root": "sha256:3f4d8ab7c723c9fddacc5a872541933a4b87d1bb39868b8ec376320db6c929af", - "files": 42, - "generated_at_commit": "10ca03b5c0595dbebdade269fe828318c2d6f1b0", - "source_tree": "sha256:44aa4a32f33842671b85bc7d609c6010bd901c24f99786167cc380f315f05cd1" + "files": 43, + "generated_at_commit": "e9c28294247e2add87335d615c762db9bbc0e48d", + "source_tree": "sha256:6641d05683459dc4f7b550f6ac88c9f93c90f807ad9b844d58cfefcb99a33b48" }, "owner_lane": "", "provenance": { diff --git a/docs/estimation/RECEIPT_JSON.md b/docs/estimation/RECEIPT_JSON.md new file mode 100644 index 000000000..49b8254aa --- /dev/null +++ b/docs/estimation/RECEIPT_JSON.md @@ -0,0 +1,49 @@ +# Estimation receipt JSON rules (est-json-1) + +Status: applies to receipts written by `est5`, `est4`, `est3c_eval` and `est_eval` from this +change on. Receipts already committed are never rewritten (a receipt is named by the SHA-256 of +its bytes). No protocol, parameter, data, threshold or verdict rule changes here. + +## The defect this fixes + +`receipts/est-v5/receipt-7f0bb701...json` is byte-exact to its digest but is not strict JSON +(RFC 8259): `"hi": inf` for the unbounded `n_scored` threshold (3 times) and +`"ten_log_score": nan` for the F1 and E0 baselines, which have no ten-step score (2 times). +Cause: `js_stats` in `tools/estimation/est5.c` printed doubles with `%.17g`, which writes `inf` +and `nan`. `est4.c` (v4) and `est3c_eval.c` (v3) carried the same function; `est_eval.c` printed +several statistics with `%.17g` and no finite check. The same function also wrote a non-finite +statistic value as the finite number `-1.0`. +Strict parsers reject the file. `jq` 1.7 does not (it reads `inf` as 1.797e308 and `nan` as +`null`), so `jq` is not a validator for these receipts; `test_est_json --validate FILE` is. + +## Rules + +1. A receipt never contains `inf`, `-inf` or `nan`. Finite measured values stay finite numbers + (`%.17g`, unchanged). +2. Unbounded threshold: `"lo"` / `"hi"` is `null` and `"lo_unbounded"` / `"hi_unbounded"` is + `true`. Bounded thresholds keep their number and `false`. A NaN threshold is not unbounded: + it is invalid (rule 4). +3. Unavailable measurement: a baseline (F1, E0) has no ten-step log score. `"ten_log_score"` is + `null` and `"ten_log_score_status"` is `"unavailable"`. A measured ten-step score is a number + with status `"ok"`. v3 receipts have no ten-step field. + A gated statistic with too few steps (regime idle / in-trial) that cannot be computed is + `"value": null` with `"value_status": "unmeasured"`. Finite values have `"value_status": "ok"`. +4. Unexpected non-finite measured value (mean log score, width80 mean or median, the selected + family's ten-step score, a gated statistic value, a NaN threshold) is written as `null`, its + name is listed in the block's `"invalid_fields"`, a statistic value gets + `"value_status": "invalid"`, and the top level carries `"invalid_measurements": N`. + The scoring decision fails closed: `N > 0` forces the verdict to `HELD_OUT_FAIL` (v3: `FAIL`) + with reason `non-finite measured value (marked invalid in the receipt)`. It can never pass. + The gate itself already failed such a statistic (`band()` in `est3c_common.c` passes only + finite values). For `est_eval.c` a non-finite number is written as `null` and its `ok_*` gate + comparisons are false for NaN, so it fails there too. +5. New receipts carry `"json_rules": "est-json-1"` (est4, est5, est3c_eval). +6. Naming is unchanged: `receipt-<sha256 of the receipt bytes>.json`, written once, never + overwritten. + +## Code + +`c3_json_num`, `c3_json_stats`, `c3_stats_invalid` in `tools/estimation/est3c_common.c` (used by +est3c_eval, est4, est5); `N17` in `tools/estimation/est_eval.c`. +Tests: `make test-est-json` (unit checks, strict validator, plain and ASan/UBSan) and +`make test-est5-tools` (strict parse of the synthetic and the binding-path receipts). diff --git a/mk/.crumb b/mk/.crumb index a3e0bf826..f0eaa15e0 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T12:20:02Z", + "compiled_at": "2026-10-05T13:53:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:052b73debc780209673d2f422317d17cd85a45084ea9e97bd11900d2c633bf7f", + "digest": "sha256:1a09c53fc4d21cd13d4f307282aeabcbf896a0639618c8767a3011d49d4cc613", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 54, - "generated_at_commit": "85a3fc2488650bde3cebfc67ed71cb87fcbc3163", - "source_tree": "sha256:33cf0553598dd76e805b1f690cd8783149cca76e43c0f19fd3d98cc365c45e51" + "generated_at_commit": "e9c28294247e2add87335d615c762db9bbc0e48d", + "source_tree": "sha256:0c0db4ce1cda31f967e370106adb4971612b07e230f1b41b28f343960cfb6861" }, "owner_lane": "", "provenance": { diff --git a/mk/estimation_v5.mk b/mk/estimation_v5.mk index 23dcbdd7f..c83a34ce5 100644 --- a/mk/estimation_v5.mk +++ b/mk/estimation_v5.mk @@ -4,12 +4,13 @@ # # test-est5-tools tool tests on synthetic data (held-out refusal, open trace, binding refusals) # test-est5-window collector and window-check tests (no data, no load) +# test-est-json strict-JSON receipt rules (est-json-1): unit checks + strict parse of emitted receipts # test-estimation-v5 v4 library tests (reused, unchanged) + the two above # est5 the binding tool (identity compiled in) ifndef ESTIMATION_V5_MK ESTIMATION_V5_MK := 1 include mk/estimation_v4.mk -.PHONY: test-est5-tools test-est5-window test-estimation-v5 est5 est5-force +.PHONY: test-est-json test-est5-tools test-est5-window test-estimation-v5 est5 est5-force ESTV5_DIR = $(OUT_DIR)/estimation-v5 ESTV5_CFLAGS = $(EST_CFLAGS) -Itools/estimation ESTV5_TOOL_SRCS = tools/estimation/est5.c tools/estimation/est3c_common.c tools/estimation/est_replay.c \ @@ -54,14 +55,27 @@ $(ESTV5_DIR)/est5_test: $(ESTV5_TOOL_SRCS) $(ESTV5_TOOL_HDRS) $(ESTV5_DIR)/est5_test_asan: $(ESTV5_TOOL_SRCS) $(ESTV5_TOOL_HDRS) @mkdir -p $(ESTV5_DIR) $(CC) $(ESTV5_CFLAGS) $(EST_ASAN) -Wno-format-truncation -DE5_TEST_BUILD -DTOOL_COMMIT='"test-build"' -DTOOL_DIRTY=1 -o $@ $(ESTV5_TOOL_SRCS) -lm -test-est5-tools: $(ESTV5_DIR)/est5 $(ESTV5_DIR)/est5_test $(ESTV5_DIR)/est5_test_asan - sh tools/estimation/test_est5_tools.sh ./$(ESTV5_DIR)/est5_test $(ESTV5_DIR)/work - sh tools/estimation/test_est5_tools.sh ./$(ESTV5_DIR)/est5_test_asan $(ESTV5_DIR)/work-asan +test-est5-tools: $(ESTV5_DIR)/test_est_json $(ESTV5_DIR)/est5 $(ESTV5_DIR)/est5_test $(ESTV5_DIR)/est5_test_asan + sh tools/estimation/test_est5_tools.sh ./$(ESTV5_DIR)/est5_test $(ESTV5_DIR)/work ./$(ESTV5_DIR)/test_est_json + sh tools/estimation/test_est5_tools.sh ./$(ESTV5_DIR)/est5_test_asan $(ESTV5_DIR)/work-asan ./$(ESTV5_DIR)/test_est_json @echo "test-est5-tools: fit, held-out refusal, open-trace purity, binding refusals and receipt path pass (synthetic data)" +ESTJ_SRCS = tools/estimation/test_est_json.c tools/estimation/est3c_common.c tools/estimation/est_replay.c \ + src/estimation/est_v4.c src/estimation/est_pred.c src/estimation/est_mix.c $(EST_KF_SRCS) +$(ESTV5_DIR)/test_est_json: $(ESTJ_SRCS) $(ESTV5_TOOL_HDRS) + @mkdir -p $(ESTV5_DIR) + $(CC) $(ESTV5_CFLAGS) -Wno-format-truncation -o $@ $(ESTJ_SRCS) -lm +$(ESTV5_DIR)/test_est_json_asan: $(ESTJ_SRCS) $(ESTV5_TOOL_HDRS) + @mkdir -p $(ESTV5_DIR) + $(CC) $(ESTV5_CFLAGS) $(EST_ASAN) -Wno-format-truncation -o $@ $(ESTJ_SRCS) -lm +test-est-json: $(ESTV5_DIR)/test_est_json $(ESTV5_DIR)/test_est_json_asan + ./$(ESTV5_DIR)/test_est_json . + ./$(ESTV5_DIR)/test_est_json_asan . + @echo "test-est-json: strict-JSON receipt rules pass (plain and ASan/UBSan)" + test-est5-window: sh tools/estimation/test_est5_window.sh -test-estimation-v5: est-v4-purity test-est-v4 test-est5-tools test-est5-window +test-estimation-v5: est-v4-purity test-est-v4 test-est-json test-est5-tools test-est5-window @echo "test-estimation-v5: ESTIMATION v5 passes" endif diff --git a/tools/.crumb b/tools/.crumb index 0d7b76dfd..3fd5d1ee9 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -43,8 +43,8 @@ "purpose": "Runner for the DUAL applicability experiment: runs the pre-registered campaign and writes result" }, { - "digest": "5bf3209bf3f715b5a3059c845b62bbe74e24b04968d3de4ae5ef35ff493a83b2", - "last_commit": "30c65a3", + "digest": "f21d39178dd934c3d59c800fb5e2e900365f1d968cca6927ff64533471ed2d5d", + "last_commit": "38b4e4c", "name": "estimation", "purpose": "Directory tools/estimation. Purpose not yet described by a human." }, @@ -55,15 +55,15 @@ "purpose": "Directory tools/turing_verify_indep. Purpose not yet described by a human." } ], - "children_root": "sha256:6ed09e33b610372de30ea2d6372b94240127e9acb4d178879cbd5448faab6909", - "compiled_at": "2026-10-05T13:50:43Z", + "children_root": "sha256:e1e9c33c1bd279d2fe5dfd86f51a39198081fa2b63818fce1ef7b04f43f91e6b", + "compiled_at": "2026-10-05T14:00:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:59136def1fad3d627f93534c3e5e4a8d93a57c12f10fab5dce0221c6e6c3590b", + "digest": "sha256:fcf0d77d97acf0c597b39c054307650cff03295797be8686f7aeaf634ba5c2b8", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 127, - "generated_at_commit": "183fdc04306bae459caf82c7370cec0fe96c8887", - "source_tree": "sha256:ae4426759c2f3df989c5627665fcdb3461bf45dab9efbf38dfe87c808f340b89" + "files": 128, + "generated_at_commit": "38b4e4ccb54e38d96c477e4060c7e38ee06d6135", + "source_tree": "sha256:55541fe8381b0b12806b451ba2b1b75357596e787bd330a2b4a303141c2e3b44" }, "owner_lane": "", "provenance": { diff --git a/tools/estimation/.crumb b/tools/estimation/.crumb index 87c49dd7f..8ac0a4302 100644 --- a/tools/estimation/.crumb +++ b/tools/estimation/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T12:20:02Z", + "compiled_at": "2026-10-05T13:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:5bf3209bf3f715b5a3059c845b62bbe74e24b04968d3de4ae5ef35ff493a83b2", + "digest": "sha256:f21d39178dd934c3d59c800fb5e2e900365f1d968cca6927ff64533471ed2d5d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 24, - "generated_at_commit": "85a3fc2488650bde3cebfc67ed71cb87fcbc3163", - "source_tree": "sha256:13814e2acd49d7ffe63ac73185e64fbe62ca2fb5ba45b56137f29abfe6dacaac" + "files": 25, + "generated_at_commit": "ab4b911f8c078258785cd564b89ca7c7a48119c4", + "source_tree": "sha256:0b8161bcf57fdd2d11769f11a23d7b12c7a7d71ce1c1f9bac481bd50e5d5f726" }, "owner_lane": "", "provenance": { diff --git a/tools/estimation/est3c_common.c b/tools/estimation/est3c_common.c index 119ecb2ee..5bb2b0903 100644 --- a/tools/estimation/est3c_common.c +++ b/tools/estimation/est3c_common.c @@ -855,6 +855,80 @@ void c3_json_str(FILE *fp, const char *s) fputc('"', fp); } + +/* ---- strict-JSON receipt numbers (est-json-1) ---- */ +void c3_json_num(FILE *fp, double v) +{ + if (isfinite(v)) fprintf(fp, "%.17g", v); else fputs("null", fp); +} + +/* A gated statistic that is not finite is invalid (it also fails its gate: band() sets pass only + * for finite values). An ungated one (a regime with too few steps) is merely unmeasured. */ +static int stat_value_state(const c3_stats *st, int i) /* 0 ok, 1 unmeasured, 2 invalid */ +{ + if (isfinite(st->value[i])) return 0; + return st->gated[i] ? 2 : 1; +} + +static int bound_invalid(double b) { return isnan(b); } + +/* names (optional) receives the invalid field names; returns how many */ +static size_t stats_invalid_list(const c3_stats *st, int ten_mode, double ls10, char (*names)[56], size_t cap) +{ + size_t k = 0; +#define NOTE(cond, text) do { if (cond) { if (names && k < cap) snprintf(names[k], 56, "%s", text); k++; } } while (0) + NOTE(!isfinite(st->logscore), "mean_log_score"); + NOTE(!isfinite(st->width_mean), "width80_mean_mc"); + NOTE(!isfinite(st->width_median), "width80_median_mc"); + NOTE(ten_mode == C3_TEN_MEASURED && !isfinite(ls10), "ten_log_score"); + for (int i = 0; i < C3_ST_COUNT; i++) { + char t[56]; + snprintf(t, sizeof t, "stats.%s.value", c3_stat_name(i)); + NOTE(stat_value_state(st, i) == 2, t); + snprintf(t, sizeof t, "stats.%s.bound", c3_stat_name(i)); + NOTE(bound_invalid(st->band_lo[i]) || bound_invalid(st->band_hi[i]), t); + } +#undef NOTE + return k; +} + +size_t c3_stats_invalid(const c3_stats *st, int ten_mode, double ls10) +{ + return stats_invalid_list(st, ten_mode, ls10, NULL, 0); +} + +size_t c3_json_stats(FILE *o, const char *name, const c3_stats *st, int ten_mode, double ls10) +{ + char names[C3_ST_COUNT * 2 + 8][56]; + size_t bad = stats_invalid_list(st, ten_mode, ls10, names, sizeof names / sizeof names[0]); + fprintf(o, " \"%s\": {\"n\": %zu, \"unscorable\": %zu, \"mean_log_score\": ", name, st->n, st->unscorable); + c3_json_num(o, st->logscore); + if (ten_mode != C3_TEN_ABSENT) { + fputs(", \"ten_log_score\": ", o); + c3_json_num(o, ls10); + fprintf(o, ", \"ten_log_score_status\": \"%s\"", ten_mode == C3_TEN_UNAVAILABLE ? "unavailable" : isfinite(ls10) ? "ok" : "invalid"); + } + fprintf(o, ", \"calibrated\": %s, \"first_fail\": ", st->calibrated ? "true" : "false"); + if (st->calibrated) fputs("null", o); else c3_json_str(o, c3_stat_name(st->first_fail)); + fputs(", \"width80_mean_mc\": ", o); c3_json_num(o, st->width_mean); + fputs(", \"width80_median_mc\": ", o); c3_json_num(o, st->width_median); + fputs(", \"invalid_fields\": [", o); + for (size_t k = 0; k < bad && k < sizeof names / sizeof names[0]; k++) fprintf(o, "%s\"%s\"", k ? ", " : "", names[k]); + fputs("], \"stats\": {", o); + for (int i = 0; i < C3_ST_COUNT; i++) { + static const char *const vs[3] = { "ok", "unmeasured", "invalid" }; + fprintf(o, "%s\"%s\": {\"value\": ", i ? ", " : "", c3_stat_name(i)); + c3_json_num(o, st->value[i]); + fprintf(o, ", \"value_status\": \"%s\", \"lo\": ", vs[stat_value_state(st, i)]); + c3_json_num(o, st->band_lo[i]); + fprintf(o, ", \"lo_unbounded\": %s, \"hi\": ", isinf(st->band_lo[i]) ? "true" : "false"); + c3_json_num(o, st->band_hi[i]); + fprintf(o, ", \"hi_unbounded\": %s, \"n\": %zu, \"gated\": %s, \"pass\": %s}", isinf(st->band_hi[i]) ? "true" : "false", + (i >= C3_ST_Q0 && i <= C3_ST_TEN) ? st->sub_n[i] : st->n, st->gated[i] ? "true" : "false", st->pass[i] ? "true" : "false"); + } + fputs("}}", o); + return bad; +} /* -------------------------------------------------------------- synthetic */ static uint64_t sm64(uint64_t *s) diff --git a/tools/estimation/est3c_common.h b/tools/estimation/est3c_common.h index fded6cb78..601e4a94b 100644 --- a/tools/estimation/est3c_common.h +++ b/tools/estimation/est3c_common.h @@ -227,6 +227,18 @@ typedef struct { } c3_synth; int c3_synth_write(const char *dir, const c3_synth *s); void c3_json_str(FILE *fp, const char *s); +/* Strict-JSON receipt rules (est-json-1, docs/estimation/RECEIPT_JSON.md): a receipt never + * contains inf or nan. c3_json_num writes a finite double with %.17g, anything else as null. + * c3_json_stats writes one score block: infinite thresholds as null + lo/hi_unbounded true, + * unmeasured values as null + a status, and every non-finite MEASURED value as null + listed + * in "invalid_fields". c3_stats_invalid counts those invalid fields; a caller must make its + * verdict fail when it is non-zero. ten_mode: ABSENT omits the ten-step score field, + * UNAVAILABLE writes null + status "unavailable" (a baseline that has none), MEASURED writes + * the number, or null + status "invalid" when it is not finite. */ +enum { C3_TEN_ABSENT = 0, C3_TEN_UNAVAILABLE = 1, C3_TEN_MEASURED = 2 }; +void c3_json_num(FILE *fp, double v); +size_t c3_stats_invalid(const c3_stats *st, int ten_mode, double ls10); +size_t c3_json_stats(FILE *o, const char *name, const c3_stats *st, int ten_mode, double ls10); /* grid search: take a point when it is the first finite one or strictly better * (ties keep the earlier point in listed order). */ int c3_grid_take(int have, double ls, double best); diff --git a/tools/estimation/est3c_eval.c b/tools/estimation/est3c_eval.c index 2f0567be0..24faee82d 100644 --- a/tools/estimation/est3c_eval.c +++ b/tools/estimation/est3c_eval.c @@ -161,19 +161,6 @@ static const char *class_hint(int s) return "sensor fault"; } -static void js_stats(FILE *o, const char *name, const c3_stats *st) -{ - fprintf(o, " \"%s\": {\"n\": %zu, \"unscorable\": %zu, \"mean_log_score\": %.17g, \"calibrated\": %s, \"first_fail\": ", - name, st->n, st->unscorable, st->logscore, st->calibrated ? "true" : "false"); - if (st->calibrated) fprintf(o, "null"); else c3_json_str(o, c3_stat_name(st->first_fail)); - fprintf(o, ", \"width80_mean_mc\": %.17g, \"width80_median_mc\": %.17g, \"stats\": {", st->width_mean, st->width_median); - for (int i = 0; i < C3_ST_COUNT; i++) - fprintf(o, "%s\"%s\": {\"value\": %.17g, \"lo\": %.17g, \"hi\": %.17g, \"n\": %zu, \"gated\": %s, \"pass\": %s}", i ? ", " : "", - c3_stat_name(i), isfinite(st->value[i]) ? st->value[i] : -1.0, st->band_lo[i], st->band_hi[i], - (i >= C3_ST_Q0 && i <= C3_ST_TEN) ? st->sub_n[i] : st->n, st->gated[i] ? "true" : "false", st->pass[i] ? "true" : "false"); - fprintf(o, "}}"); -} - static int load_run(const char *dir, c3_ticks *tk, c3_marks *mk, char *raw_sha, char *err, size_t cap) { char path[1200]; @@ -249,9 +236,11 @@ static int recorded(const char *dir, const char *ppath, const char *outdir, int fprintf(stderr, "scoring error: %s%s%s\n", sS.errmsg, s1.errmsg, s0.errmsg); c3_ticks_free(&tk); return 1; } int same = sS.n == s1.n && sS.n == s0.n && sS.unscorable == s1.unscorable && sS.unscorable == s0.unscorable; int beat1 = sS.logscore >= s1.logscore - C3_TIE_NATS, beat0 = sS.logscore >= s0.logscore - C3_TIE_NATS; - int pass = same && sS.calibrated && beat1 && beat0; + /* est-json-1: a non-finite measured value is recorded as invalid and can never pass */ + size_t inv = c3_stats_invalid(&sS, C3_TEN_ABSENT, 0) + c3_stats_invalid(&s1, C3_TEN_ABSENT, 0) + c3_stats_invalid(&s0, C3_TEN_ABSENT, 0); + int pass = inv == 0 && same && sS.calibrated && beat1 && beat0; const char *verdict = pass ? "PASS" : "FAIL"; - const char *reason = !same ? "S, F1 and E0 were not scored on the same steps" + const char *reason = inv ? "non-finite measured value (marked invalid in the receipt)" : !same ? "S, F1 and E0 were not scored on the same steps" : !sS.calibrated ? "S not calibrated" : (!beat1 || !beat0) ? "calibrated but not better than a baseline" : "calibrated and no worse than F1 and E0"; @@ -283,21 +272,28 @@ static int recorded(const char *dir, const char *ppath, const char *outdir, int fprintf(o, " \"d2_dir\": "); c3_json_str(o, dir); fprintf(o, ",\n \"d2_raw_sha256\": \"%s\",\n \"d2_marks_sha256\": \"%s\",\n \"d2_schedule_sha256\": \"%s\",\n", pre.raw_sha, pre.marks_sha, pre.sched_sha); fprintf(o, " \"d2_compiled_raw_sha256\": \"%s\",\n \"d2_compiled_marks_sha256\": \"%s\",\n \"d2_compiled_schedule_sha256\": \"%s\",\n \"params_compiled_sha256\": \"%s\",\n", d2_raw_sha(), d2_marks_sha(), d2_sched_sha(), params_sha()); - fprintf(o, " \"precheck\": {\"verdict\": \"VALID\", \"lines\": %zu, \"gap_big_frac\": %.17g, \"foreign_mean\": %.17g, \"foreign_over3_frac\": %.17g, \"foreign_unmeasured\": %zu, \"sched_trials\": %zu, \"loadavg1_mean\": %.17g},\n", - pre.lines, pre.gap_big_frac, pre.foreign_mean, pre.foreign_over3_frac, pre.foreign_unmeasured, pre.sched_trials, pre.loadavg1_mean); + fprintf(o, " \"precheck\": {\"verdict\": \"VALID\", \"lines\": %zu, \"gap_big_frac\": ", pre.lines); c3_json_num(o, pre.gap_big_frac); + fprintf(o, ", \"foreign_mean\": "); c3_json_num(o, pre.foreign_mean); + fprintf(o, ", \"foreign_over3_frac\": "); c3_json_num(o, pre.foreign_over3_frac); + fprintf(o, ", \"foreign_unmeasured\": %zu, \"sched_trials\": %zu, \"loadavg1_mean\": ", pre.foreign_unmeasured, pre.sched_trials); c3_json_num(o, pre.loadavg1_mean); + fprintf(o, "},\n"); fprintf(o, " \"ticks\": {\"total\": %zu, \"inserted\": %zu, \"bad_t\": %zu, \"absent_value\": %zu, \"unparsable_value\": %zu},\n", tk.n, tk.inserted, tk.bad_t, tk.absent_value, tk.unparsable_value); fprintf(o, " \"selected\": \"%s\",\n \"same_steps\": %s,\n \"scores\": {\n", c3_name(P.selected), same ? "true" : "false"); - js_stats(o, "S", &sS); fprintf(o, ",\n"); js_stats(o, "F1", &s1); fprintf(o, ",\n"); js_stats(o, "E0", &s0); - fprintf(o, "\n },\n \"comparison\": {\"S_minus_F1\": %.17g, \"S_minus_E0\": %.17g, \"tolerance_nats\": %.2f, \"no_worse_than_F1\": %s, \"no_worse_than_E0\": %s},\n", - sS.logscore - s1.logscore, sS.logscore - s0.logscore, C3_TIE_NATS, beat1 ? "true" : "false", beat0 ? "true" : "false"); + c3_json_stats(o, "S", &sS, C3_TEN_ABSENT, 0); fprintf(o, ",\n"); c3_json_stats(o, "F1", &s1, C3_TEN_ABSENT, 0); fprintf(o, ",\n"); c3_json_stats(o, "E0", &s0, C3_TEN_ABSENT, 0); + fprintf(o, "\n },\n \"json_rules\": \"est-json-1\",\n \"invalid_measurements\": %zu,\n \"comparison\": {\"S_minus_F1\": ", inv); + c3_json_num(o, sS.logscore - s1.logscore); fprintf(o, ", \"S_minus_E0\": "); c3_json_num(o, sS.logscore - s0.logscore); + fprintf(o, ", \"tolerance_nats\": %.2f, \"no_worse_than_F1\": %s, \"no_worse_than_E0\": %s},\n", C3_TIE_NATS, beat1 ? "true" : "false", beat0 ? "true" : "false"); fprintf(o, " \"informational\": {"); if (synth) fprintf(o, "\"skipped\": \"synthetic test\""); else for (int i = 0; i < 3; i++) { fprintf(o, "%s\n \"%s\": {\"dir\": \"%s\", \"raw_sha256\": \"%s\", ", i ? "," : "", devn[i], dev[i], si_sha[i]); - if (si_ok[i]) fprintf(o, "\"n\": %zu, \"mean_log_score\": %.17g, \"calibrated\": %s, \"first_fail\": \"%s\", \"cov95\": %.17g}", - si[i].n, si[i].logscore, si[i].calibrated ? "true" : "false", - si[i].calibrated ? "none" : c3_stat_name(si[i].first_fail), si[i].value[C3_ST_COV95]); + if (si_ok[i]) { + fprintf(o, "\"n\": %zu, \"mean_log_score\": ", si[i].n); c3_json_num(o, si[i].logscore); + fprintf(o, ", \"calibrated\": %s, \"first_fail\": \"%s\", \"cov95\": ", si[i].calibrated ? "true" : "false", + si[i].calibrated ? "none" : c3_stat_name(si[i].first_fail)); + c3_json_num(o, si[i].value[C3_ST_COV95]); fprintf(o, "}"); + } else { fprintf(o, "\"error\": "); c3_json_str(o, si_err[i]); fprintf(o, "}"); } } fprintf(o, "},\n \"verdict\": \"%s\",\n \"verdict_reason\": \"%s\",\n", verdict, reason); diff --git a/tools/estimation/est4.c b/tools/estimation/est4.c index be49611b5..d3fb6a9eb 100644 --- a/tools/estimation/est4.c +++ b/tools/estimation/est4.c @@ -567,19 +567,6 @@ static int has_receipt(const char *dir) return found; } -static void js_stats(FILE *o, const char *name, const c3_stats *st, double ls10) -{ - fprintf(o, " \"%s\": {\"n\": %zu, \"unscorable\": %zu, \"mean_log_score\": %.17g, \"ten_log_score\": %.17g, \"calibrated\": %s, \"first_fail\": ", - name, st->n, st->unscorable, st->logscore, ls10, st->calibrated ? "true" : "false"); - if (st->calibrated) fprintf(o, "null"); else c3_json_str(o, c3_stat_name(st->first_fail)); - fprintf(o, ", \"width80_mean_mc\": %.17g, \"width80_median_mc\": %.17g, \"stats\": {", st->width_mean, st->width_median); - for (int i = 0; i < C3_ST_COUNT; i++) - fprintf(o, "%s\"%s\": {\"value\": %.17g, \"lo\": %.17g, \"hi\": %.17g, \"n\": %zu, \"gated\": %s, \"pass\": %s}", i ? ", " : "", - c3_stat_name(i), isfinite(st->value[i]) ? st->value[i] : -1.0, st->band_lo[i], st->band_hi[i], - (i >= C3_ST_Q0 && i <= C3_ST_TEN) ? st->sub_n[i] : st->n, st->gated[i] ? "true" : "false", st->pass[i] ? "true" : "false"); - fprintf(o, "}}"); -} - static int cmd_recorded(int argc, char **argv) { const char *dir = NULL, *ppath = NULL, *outdir = NULL; @@ -656,9 +643,11 @@ static int cmd_recorded(int argc, char **argv) fprintf(stderr, "scoring error: %s %s %s\n", sS.errmsg, s1.errmsg, s0.errmsg); return 1; } int same = sS.n == s1.n && sS.n == s0.n && sS.unscorable == s1.unscorable && sS.unscorable == s0.unscorable; int beat1 = sS.logscore >= s1.logscore - C3_TIE_NATS, beat0 = sS.logscore >= s0.logscore - C3_TIE_NATS; - int pass = same && sS.calibrated && beat1 && beat0; + /* est-json-1: a non-finite measured value is recorded as invalid and can never pass */ + size_t inv = c3_stats_invalid(&sS, C3_TEN_MEASURED, l10S) + c3_stats_invalid(&s1, C3_TEN_UNAVAILABLE, NAN) + c3_stats_invalid(&s0, C3_TEN_UNAVAILABLE, NAN); + int pass = inv == 0 && same && sS.calibrated && beat1 && beat0; const char *verdict = pass ? "PASS" : "HELD_OUT_FAIL"; - const char *reason = !same ? "S, F1 and E0 were not scored on the same steps" : !sS.calibrated ? "S not calibrated" + const char *reason = inv ? "non-finite measured value (marked invalid in the receipt)" : !same ? "S, F1 and E0 were not scored on the same steps" : !sS.calibrated ? "S not calibrated" : (!beat1 || !beat0) ? "calibrated but not better than a baseline" : "calibrated and no worse than F1 and E0"; char *buf = NULL; size_t blen = 0; FILE *o = open_memstream(&buf, &blen); @@ -672,10 +661,11 @@ static int cmd_recorded(int argc, char **argv) fprintf(o, ", \"raw_sha256\": \"%s\", \"marks_sha256\": \"%s\", \"schedule_sha256\": \"%s\", \"lines\": %zu},\n", pre.raw_sha, pre.marks_sha, pre.sched_sha, pre.lines); fprintf(o, " \"selected\": \"%s\",\n \"comparisons\": {\"same_steps\": %s, \"no_worse_than_F1\": %s, \"no_worse_than_E0\": %s, \"tie_nats\": %.2f},\n", gname(P.selected), same ? "true" : "false", beat1 ? "true" : "false", beat0 ? "true" : "false", C3_TIE_NATS); + fprintf(o, " \"json_rules\": \"est-json-1\",\n \"invalid_measurements\": %zu,\n", inv); fprintf(o, " \"scores\": {\n"); - js_stats(o, gname(P.selected), &sS, l10S); fprintf(o, ",\n"); - js_stats(o, "F1", &s1, NAN); fprintf(o, ",\n"); - js_stats(o, "E0", &s0, NAN); fprintf(o, "\n }\n}\n"); + c3_json_stats(o, gname(P.selected), &sS, C3_TEN_MEASURED, l10S); fprintf(o, ",\n"); + c3_json_stats(o, "F1", &s1, C3_TEN_UNAVAILABLE, NAN); fprintf(o, ",\n"); + c3_json_stats(o, "E0", &s0, C3_TEN_UNAVAILABLE, NAN); fprintf(o, "\n }\n}\n"); fclose(o); uint8_t dg[32]; char hex[65]; sha256_ctx c; sha256_init(&c); sha256_update(&c, (const uint8_t *)buf, blen); sha256_final(&c, dg); diff --git a/tools/estimation/est5.c b/tools/estimation/est5.c index 8d20e9a00..bf2a3a536 100644 --- a/tools/estimation/est5.c +++ b/tools/estimation/est5.c @@ -602,19 +602,6 @@ static int has_receipt(const char *dir) return found; } -static void js_stats(FILE *o, const char *name, const c3_stats *st, double ls10) -{ - fprintf(o, " \"%s\": {\"n\": %zu, \"unscorable\": %zu, \"mean_log_score\": %.17g, \"ten_log_score\": %.17g, \"calibrated\": %s, \"first_fail\": ", - name, st->n, st->unscorable, st->logscore, ls10, st->calibrated ? "true" : "false"); - if (st->calibrated) fprintf(o, "null"); else c3_json_str(o, c3_stat_name(st->first_fail)); - fprintf(o, ", \"width80_mean_mc\": %.17g, \"width80_median_mc\": %.17g, \"stats\": {", st->width_mean, st->width_median); - for (int i = 0; i < C3_ST_COUNT; i++) - fprintf(o, "%s\"%s\": {\"value\": %.17g, \"lo\": %.17g, \"hi\": %.17g, \"n\": %zu, \"gated\": %s, \"pass\": %s}", i ? ", " : "", - c3_stat_name(i), isfinite(st->value[i]) ? st->value[i] : -1.0, st->band_lo[i], st->band_hi[i], - (i >= C3_ST_Q0 && i <= C3_ST_TEN) ? st->sub_n[i] : st->n, st->gated[i] ? "true" : "false", st->pass[i] ? "true" : "false"); - fprintf(o, "}}"); -} - static int cmd_recorded(int argc, char **argv) { const char *dir = NULL, *ppath = NULL, *outdir = NULL; @@ -692,9 +679,11 @@ static int cmd_recorded(int argc, char **argv) fprintf(stderr, "scoring error: %s %s %s\n", sS.errmsg, s1.errmsg, s0.errmsg); return 1; } int same = sS.n == s1.n && sS.n == s0.n && sS.unscorable == s1.unscorable && sS.unscorable == s0.unscorable; int beat1 = sS.logscore >= s1.logscore - C3_TIE_NATS, beat0 = sS.logscore >= s0.logscore - C3_TIE_NATS; - int pass = same && sS.calibrated && beat1 && beat0; + /* est-json-1: a non-finite measured value is recorded as invalid and can never pass */ + size_t inv = c3_stats_invalid(&sS, C3_TEN_MEASURED, l10S) + c3_stats_invalid(&s1, C3_TEN_UNAVAILABLE, NAN) + c3_stats_invalid(&s0, C3_TEN_UNAVAILABLE, NAN); + int pass = inv == 0 && same && sS.calibrated && beat1 && beat0; const char *verdict = pass ? "PASS" : "HELD_OUT_FAIL"; - const char *reason = !same ? "S, F1 and E0 were not scored on the same steps" : !sS.calibrated ? "S not calibrated" + const char *reason = inv ? "non-finite measured value (marked invalid in the receipt)" : !same ? "S, F1 and E0 were not scored on the same steps" : !sS.calibrated ? "S not calibrated" : (!beat1 || !beat0) ? "calibrated but not better than a baseline" : "calibrated and no worse than F1 and E0"; char *buf = NULL; size_t blen = 0; FILE *o = open_memstream(&buf, &blen); @@ -708,10 +697,11 @@ static int cmd_recorded(int argc, char **argv) fprintf(o, ", \"raw_sha256\": \"%s\", \"marks_sha256\": \"%s\", \"schedule_sha256\": \"%s\", \"lines\": %zu},\n", pre.raw_sha, pre.marks_sha, pre.sched_sha, pre.lines); fprintf(o, " \"selected\": \"%s\",\n \"comparisons\": {\"same_steps\": %s, \"no_worse_than_F1\": %s, \"no_worse_than_E0\": %s, \"tie_nats\": %.2f},\n", gname(P.selected), same ? "true" : "false", beat1 ? "true" : "false", beat0 ? "true" : "false", C3_TIE_NATS); + fprintf(o, " \"json_rules\": \"est-json-1\",\n \"invalid_measurements\": %zu,\n", inv); fprintf(o, " \"scores\": {\n"); - js_stats(o, gname(P.selected), &sS, l10S); fprintf(o, ",\n"); - js_stats(o, "F1", &s1, NAN); fprintf(o, ",\n"); - js_stats(o, "E0", &s0, NAN); fprintf(o, "\n }\n}\n"); + c3_json_stats(o, gname(P.selected), &sS, C3_TEN_MEASURED, l10S); fprintf(o, ",\n"); + c3_json_stats(o, "F1", &s1, C3_TEN_UNAVAILABLE, NAN); fprintf(o, ",\n"); + c3_json_stats(o, "E0", &s0, C3_TEN_UNAVAILABLE, NAN); fprintf(o, "\n }\n}\n"); fclose(o); uint8_t dg[32]; char hex[65]; sha256_ctx c; sha256_init(&c); sha256_update(&c, (const uint8_t *)buf, blen); sha256_final(&c, dg); diff --git a/tools/estimation/est_eval.c b/tools/estimation/est_eval.c index f83b37cfc..b5ec7dc87 100644 --- a/tools/estimation/est_eval.c +++ b/tools/estimation/est_eval.c @@ -251,6 +251,13 @@ static void js(FILE *fp, const char *s) } fputc('"', fp); } +/* est-json-1: a receipt never holds inf or nan; a non-finite number is written as null */ +static const char *numstr(double v, char *b) +{ + if (isfinite(v)) snprintf(b, 40, "%.17g", v); else snprintf(b, 40, "null"); + return b; +} +#define N17(v) numstr((v), (char[40]){ 0 }) static void jd(FILE *fp, const char *k, double v) { if (isfinite(v)) fprintf(fp, "\"%s\": %.17g", k, v); else fprintf(fp, "\"%s\": null", k); @@ -258,19 +265,19 @@ static void jd(FILE *fp, const char *k, double v) static void emit_model(FILE *fp, int m, double q, double r, double ll, const est_mix *mx, const model_stats *s, int last) { - fprintf(fp, " \"M%d\": {\n \"q\": %.17g, \"r\": %.17g, \"fit_loglik\": %.17g,\n", m, q, r, ll); + fprintf(fp, " \"M%d\": {\n \"q\": %s, \"r\": %s, \"fit_loglik\": %s,\n", m, N17(q), N17(r), N17(ll)); if (mx) { fprintf(fp, " \"predictive_shape\": {\"kind\": \"gaussian_scale_mixture\", \"k\": %u, \"w\": [", mx->k); - for (uint32_t j = 0; j < mx->k; j++) fprintf(fp, "%s%.17g", j ? ", " : "", mx->w[j]); + for (uint32_t j = 0; j < mx->k; j++) fprintf(fp, "%s%s", j ? ", " : "", N17(mx->w[j])); fprintf(fp, "], \"v\": ["); - for (uint32_t j = 0; j < mx->k; j++) fprintf(fp, "%s%.17g", j ? ", " : "", mx->v[j]); + for (uint32_t j = 0; j < mx->k; j++) fprintf(fp, "%s%s", j ? ", " : "", N17(mx->v[j])); fprintf(fp, "]},\n"); } fprintf(fp, " \"samples\": %zu, \"included_logical_span_ns\": [%lld, %lld],\n", s->n, (long long)s->tmin_ns, (long long)s->tmax_ns); const char *nm[3] = { "50", "80", "95" }; for (int k = 0; k < 3; k++) { - fprintf(fp, " \"coverage_%s\": {\"value\": %.17g, \"wilson95\": [%.17g, %.17g], \"pass\": %s},\n", - nm[k], s->cov[k], s->wlo[k], s->whi[k], b(s->ok_cov[k])); + fprintf(fp, " \"coverage_%s\": {\"value\": %s, \"wilson95\": [%s, %s], \"pass\": %s},\n", + nm[k], N17(s->cov[k]), N17(s->wlo[k]), N17(s->whi[k]), b(s->ok_cov[k])); } fprintf(fp, " \"mean_nis\": {"); jd(fp, "value", s->mean_nis); fprintf(fp, ", \"pass\": %s},\n", b(s->ok_nis)); fprintf(fp, " \"standardized_bias\": {"); jd(fp, "value", s->bias); fprintf(fp, ", \"pass\": %s},\n", b(s->ok_bias)); @@ -278,15 +285,15 @@ static void emit_model(FILE *fp, int m, double q, double r, double ll, const est fprintf(fp, " \"ljung_box_q10_reported_only\": "); if (isfinite(s->ljung_box10)) fprintf(fp, "%.17g", s->ljung_box10); else fprintf(fp, "null"); fprintf(fp, ",\n \"quarters_coverage95\": [\n"); for (int q = 0; q < 4; q++) - fprintf(fp, " {\"n\": %zu, \"value\": %.17g, \"pass\": %s}%s\n", s->qn[q], s->qcov[q], b(s->ok_q[q]), q < 3 ? "," : ""); + fprintf(fp, " {\"n\": %zu, \"value\": %s, \"pass\": %s}%s\n", s->qn[q], N17(s->qcov[q]), b(s->ok_q[q]), q < 3 ? "," : ""); fprintf(fp, " ],\n \"regime_coverage95\": {\n"); - fprintf(fp, " \"idle\": {\"n\": %zu, \"value\": %.17g, \"gated\": %s, \"pass\": %s},\n", s->regime_n[0], s->regime_cov[0], b(s->regime_n[0] >= 100), b(s->ok_regime[0])); - fprintf(fp, " \"in_trial\": {\"n\": %zu, \"value\": %.17g, \"gated\": %s, \"pass\": %s}\n },\n", s->regime_n[1], s->regime_cov[1], b(s->regime_n[1] >= 100), b(s->ok_regime[1])); - fprintf(fp, " \"ten_step_coverage95\": {\"n\": %zu, \"value\": %.17g, \"wilson95\": [%.17g, %.17g], \"pass\": %s},\n", s->ten_n, s->ten_cov, s->ten_lo, s->ten_hi, b(s->ok_ten)); + fprintf(fp, " \"idle\": {\"n\": %zu, \"value\": %s, \"gated\": %s, \"pass\": %s},\n", s->regime_n[0], N17(s->regime_cov[0]), b(s->regime_n[0] >= 100), b(s->ok_regime[0])); + fprintf(fp, " \"in_trial\": {\"n\": %zu, \"value\": %s, \"gated\": %s, \"pass\": %s}\n },\n", s->regime_n[1], N17(s->regime_cov[1]), b(s->regime_n[1] >= 100), b(s->ok_regime[1])); + fprintf(fp, " \"ten_step_coverage95\": {\"n\": %zu, \"value\": %s, \"wilson95\": [%s, %s], \"pass\": %s},\n", s->ten_n, N17(s->ten_cov), N17(s->ten_lo), N17(s->ten_hi), b(s->ok_ten)); fprintf(fp, " \"log_score_per_step\": "); if (isfinite(s->logscore)) fprintf(fp, "%.17g", s->logscore); else fprintf(fp, "null"); - fprintf(fp, ",\n \"rmse_one_step\": %.17g, \"persistence_rmse\": %.17g, \"rmse_no_worse_than_persistence\": %s,\n", - s->rmse, s->persist_rmse, b(s->rmse_le_persist)); - fprintf(fp, " \"zero_innovation_fraction_extra\": %.17g,\n", s->zero_innov_frac); + fprintf(fp, ",\n \"rmse_one_step\": %s, \"persistence_rmse\": %s, \"rmse_no_worse_than_persistence\": %s,\n", + N17(s->rmse), N17(s->persist_rmse), b(s->rmse_le_persist)); + fprintf(fp, " \"zero_innovation_fraction_extra\": %s,\n", N17(s->zero_innov_frac)); fprintf(fp, " \"calibrated\": %s,\n \"failed_criteria\": [", b(s->calibrated)); int first = 1; #define FC(cond, name) do { if (!(cond)) { fprintf(fp, "%s\"%s\"", first ? "" : ", ", name); first = 0; } } while (0) diff --git a/tools/estimation/test_est5_tools.sh b/tools/estimation/test_est5_tools.sh index 624b22b0b..2a3f2a355 100644 --- a/tools/estimation/test_est5_tools.sh +++ b/tools/estimation/test_est5_tools.sh @@ -6,6 +6,7 @@ set -u T=${1:?test build} W=${2:?workdir} +JV=${3:-} # optional strict-JSON validator (test_est_json --validate FILE) pass=0; fail=0 ok() { pass=$((pass + 1)); } bad() { fail=$((fail + 1)); echo "FAIL: $*" >&2; } @@ -93,6 +94,13 @@ n=$(ls "$W/rc" | grep -c '^receipt-.*\.json$'); [ "$n" -eq 1 ] && ok || bad "wan r=$(ls "$W/rc"/receipt-*.json | head -1); h=$(sha256sum "$r" | cut -c1-64) case "$r" in *"receipt-$h.json") ok ;; *) bad "receipt name is not its SHA-256" ;; esac grep -q '"same_steps": true' "$r" && ok || bad "S, F1, E0 not on the same steps" +# est-json-1: the receipt is strict JSON; unbounded and unavailable values are explicit +if [ -n "$JV" ]; then "$JV" --validate "$r" && ok || bad "synthetic receipt is not strict JSON"; else bad "no strict-JSON validator given (the check is never skipped)"; fi +if grep -Eq '(^|[^A-Za-z"])(inf|nan)([^A-Za-z"]|$)' "$r"; then bad "receipt holds a bare inf or nan token"; else ok; fi +grep -q '"json_rules": "est-json-1"' "$r" && ok || bad "receipt lacks json_rules" +grep -q '"invalid_measurements": 0' "$r" && ok || bad "invalid_measurements not 0 on a finite run" +grep -q '"n_scored": {"value": [0-9]*, "value_status": "ok", "lo": [0-9]*, "lo_unbounded": false, "hi": null, "hi_unbounded": true' "$r" && ok || bad "n_scored threshold not null + hi_unbounded" +n=$(grep -c '"ten_log_score": null, "ten_log_score_status": "unavailable"' "$r"); [ "$n" -eq 2 ] && ok || bad "want 2 unavailable baseline ten-step scores, have $n" expect 2 "already holds a receipt" "$T" recorded --dir "$S2" --params "$W/pp.txt" --outdir "$W/rc" --synthetic-test # selection mismatch is refused sed -e 's/^selected .*/selected G3/' "$W/pp.txt" > "$W/pm.txt"; mkdir -p "$W/rc2" @@ -119,6 +127,7 @@ if [ -n "$PS" ]; then expect 0 "EST5_RECORDED" rec "$W/pb.txt" n=$(ls "$RO" | grep -c '^receipt-.*\.json$'); [ "$n" -eq 1 ] && ok || bad "binding: want one receipt, have $n" grep -q "\"protocol_sha256\": \"$PS\"" "$RO"/receipt-*.json && ok || bad "binding receipt lacks the protocol SHA" + if [ -n "$JV" ]; then for br in "$RO"/receipt-*.json; do "$JV" --validate "$br" && ok || bad "binding receipt is not strict JSON"; done; fi expect 2 "already holds a receipt" rec "$W/pb.txt" mkdir -p "$W/rc3" expect 2 "(D2 bytes)" env E5_TEST_D2_RAW_SHA="$R2" "$T" recorded --dir "$S2" --params "$W/pp.txt" --outdir "$W/rc3" --synthetic-test diff --git a/tools/estimation/test_est_json.c b/tools/estimation/test_est_json.c new file mode 100644 index 000000000..daae77e75 --- /dev/null +++ b/tools/estimation/test_est_json.c @@ -0,0 +1,255 @@ +/* Tests for the strict-JSON receipt rules (est-json-1, docs/estimation/RECEIPT_JSON.md). + * Synthetic statistics only; opens no run data. + * test_est_json run the unit checks + * test_est_json --validate F exit 0 when file F is strict RFC 8259 JSON, else 1 + * jq is NOT used as the validator: jq 1.7 accepts the bare tokens inf and nan. */ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "est3c_common.h" +#include "est_replay.h" +#include "sha256.h" + +static int fails, checks; +#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) + +/* ---------------------------------------------------------- strict validator */ +typedef struct { const char *p, *end; int depth; } jp; +static void ws(jp *j) { while (j->p < j->end && (*j->p == ' ' || *j->p == '\t' || *j->p == '\n' || *j->p == '\r')) j->p++; } +static int lit(jp *j, const char *w) { size_t n = strlen(w); if ((size_t)(j->end - j->p) < n || memcmp(j->p, w, n)) return 0; j->p += n; return 1; } +static int isd(const jp *j) { return j->p < j->end && *j->p >= '0' && *j->p <= '9'; } +static int jstring(jp *j) +{ + if (j->p >= j->end || *j->p != '"') return 0; + j->p++; + while (j->p < j->end) { + unsigned char c = (unsigned char)*j->p++; + if (c == '"') return 1; + if (c < 0x20) return 0; + if (c == '\\') { + if (j->p >= j->end) return 0; + char e = *j->p++; + if (e == 'u') { for (int k = 0; k < 4; k++) { if (j->p >= j->end || !strchr("0123456789abcdefABCDEF", *j->p)) return 0; j->p++; } } + else if (!strchr("\"\\/bfnrt", e)) return 0; + } + } + return 0; +} +static int jnumber(jp *j) +{ + if (j->p < j->end && *j->p == '-') j->p++; + if (!isd(j)) return 0; + if (*j->p == '0') j->p++; else while (isd(j)) j->p++; + if (j->p < j->end && *j->p == '.') { j->p++; if (!isd(j)) return 0; while (isd(j)) j->p++; } + if (j->p < j->end && (*j->p == 'e' || *j->p == 'E')) { + j->p++; if (j->p < j->end && (*j->p == '+' || *j->p == '-')) j->p++; + if (!isd(j)) return 0; + while (isd(j)) j->p++; + } + return 1; +} +static int jvalue(jp *j) +{ + if (++j->depth > 64) return 0; + ws(j); + int ok = 0; + if (j->p >= j->end) ok = 0; + else if (*j->p == '{') { + j->p++; ws(j); + if (j->p < j->end && *j->p == '}') { j->p++; ok = 1; } + else for (;;) { + ws(j); if (!jstring(j)) break; + ws(j); if (j->p >= j->end || *j->p++ != ':') break; + if (!jvalue(j)) break; + ws(j); if (j->p >= j->end) break; + if (*j->p == ',') { j->p++; continue; } + if (*j->p == '}') { j->p++; ok = 1; } + break; + } + } else if (*j->p == '[') { + j->p++; ws(j); + if (j->p < j->end && *j->p == ']') { j->p++; ok = 1; } + else for (;;) { + if (!jvalue(j)) break; + ws(j); if (j->p >= j->end) break; + if (*j->p == ',') { j->p++; continue; } + if (*j->p == ']') { j->p++; ok = 1; } + break; + } + } else if (*j->p == '"') ok = jstring(j); + else if (*j->p == 't') ok = lit(j, "true"); + else if (*j->p == 'f') ok = lit(j, "false"); + else if (*j->p == 'n') ok = lit(j, "null"); + else ok = jnumber(j); + j->depth--; + return ok; +} +static int strict_json(const char *s, size_t n) +{ + jp j = { s, s + n, 0 }; + if (!jvalue(&j)) return 0; + ws(&j); + return j.p == j.end; +} +static char *slurp(const char *path, size_t *n) +{ + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + size_t cap = 1 << 16, len = 0; char *b = malloc(cap); + for (;;) { + if (len + 1 >= cap) b = realloc(b, cap *= 2); + size_t r = fread(b + len, 1, cap - len - 1, f); + if (!r) break; + len += r; + } + fclose(f); b[len] = 0; *n = len; + return b; +} + +/* -------------------------------------------------------------------- tests */ +static void fill_ok(c3_stats *st) +{ + memset(st, 0, sizeof *st); + st->n = 2651; st->logscore = -1.5; st->width_mean = 800; st->width_median = 300; st->calibrated = 1; st->first_fail = -1; st->ten_n = 2641; + for (int i = 0; i < C3_ST_COUNT; i++) { st->value[i] = 0.5; st->band_lo[i] = 0.25; st->band_hi[i] = 0.75; st->gated[i] = 1; st->pass[i] = 1; st->sub_n[i] = 600; } + st->value[C3_ST_N] = 2651; st->band_lo[C3_ST_N] = 2000; st->band_hi[C3_ST_N] = INFINITY; /* the unbounded threshold */ +} + +static char *emit(const c3_stats *st, int ten_mode, double ls10, size_t *bad, size_t *len) +{ + char *buf = NULL; size_t n = 0; + FILE *o = open_memstream(&buf, &n); + fputs("{\n", o); + *bad = c3_json_stats(o, "X", st, ten_mode, ls10); + fputs("\n}\n", o); + fclose(o); + *len = n; + return buf; +} + +static int has(const char *s, const char *t) { return strstr(s, t) != NULL; } +/* bare inf / nan tokens outside strings: the emitted text never holds them at all */ +static int bare_nonfinite(const char *s) +{ + return has(s, "inf") || has(s, "nan") || has(s, "Inf") || has(s, "NaN"); +} + +static void test_validator(void) +{ + const char *good[] = { "{}", "[1,2.5e-3,-0,\"a\\u00e9\"]", "{\"a\":null,\"b\":[true,false]}", " 7 " }; + const char *bad[] = { "{\"a\": inf}", "{\"a\": nan}", "{\"a\": -inf}", "[Infinity]", "[NaN]", "{\"a\":1,}", "[01]", "[1.]", "{\"a\":1} x", "" }; + for (size_t i = 0; i < sizeof good / sizeof *good; i++) CHECK(strict_json(good[i], strlen(good[i])), "validator rejected %s", good[i]); + for (size_t i = 0; i < sizeof bad / sizeof *bad; i++) CHECK(!strict_json(bad[i], strlen(bad[i])), "validator accepted %s", bad[i]); +} + +static void test_finite_and_unbounded(void) +{ + c3_stats st; size_t bad, n; + fill_ok(&st); + char *s = emit(&st, C3_TEN_MEASURED, -4.25, &bad, &n); + CHECK(strict_json(s, n), "finite block is not strict JSON"); + CHECK(!bare_nonfinite(s), "finite block holds inf/nan"); + CHECK(bad == 0 && c3_stats_invalid(&st, C3_TEN_MEASURED, -4.25) == 0, "finite block counted invalid fields"); + CHECK(has(s, "\"n_scored\": {\"value\": 2651, \"value_status\": \"ok\", \"lo\": 2000, \"lo_unbounded\": false, \"hi\": null, \"hi_unbounded\": true"), "unbounded threshold not null + hi_unbounded true"); + CHECK(has(s, "\"coverage50\": {\"value\": 0.5, \"value_status\": \"ok\", \"lo\": 0.25, \"lo_unbounded\": false, \"hi\": 0.75, \"hi_unbounded\": false"), "finite threshold changed"); + CHECK(has(s, "\"ten_log_score\": -4.25, \"ten_log_score_status\": \"ok\""), "measured ten-step score changed"); + CHECK(has(s, "\"invalid_fields\": []"), "invalid_fields not empty"); + free(s); + /* a lower bound of -inf is unbounded too */ + st.band_lo[C3_ST_BIAS] = -INFINITY; + s = emit(&st, C3_TEN_MEASURED, -4.25, &bad, &n); + CHECK(strict_json(s, n) && bad == 0 && has(s, "\"lo\": null, \"lo_unbounded\": true"), "-inf lower bound not marked unbounded"); + free(s); +} + +static void test_unavailable_baseline(void) +{ + c3_stats st; size_t bad, n; + fill_ok(&st); + char *s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); + CHECK(strict_json(s, n) && !bare_nonfinite(s), "baseline block not strict JSON"); + CHECK(has(s, "\"ten_log_score\": null, \"ten_log_score_status\": \"unavailable\""), "unavailable baseline score not null + status"); + CHECK(bad == 0 && c3_stats_invalid(&st, C3_TEN_UNAVAILABLE, NAN) == 0, "an unavailable baseline score was counted invalid"); + free(s); + s = emit(&st, C3_TEN_ABSENT, 0, &bad, &n); + CHECK(strict_json(s, n) && !has(s, "ten_log_score"), "v3 (no ten-step field) block carries a ten-step field"); + free(s); +} + +static void test_nonfinite_measured_fails_closed(void) +{ + c3_stats st; size_t bad, n; char *s; + /* measured ten-step score of S not finite */ + fill_ok(&st); + s = emit(&st, C3_TEN_MEASURED, -INFINITY, &bad, &n); + CHECK(strict_json(s, n) && !bare_nonfinite(s), "invalid ten-step block not strict JSON"); + CHECK(bad == 1 && has(s, "\"ten_log_score_status\": \"invalid\"") && has(s, "\"invalid_fields\": [\"ten_log_score\"]"), "non-finite ten-step score not marked invalid"); + CHECK(c3_stats_invalid(&st, C3_TEN_MEASURED, NAN) == 1, "NaN measured ten-step score not counted"); + free(s); + /* mean log score, width and a gated statistic value non-finite */ + fill_ok(&st); + st.logscore = -INFINITY; st.width_mean = NAN; st.value[C3_ST_LAG1] = NAN; st.pass[C3_ST_LAG1] = 0; st.calibrated = 0; + s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); + CHECK(strict_json(s, n) && !bare_nonfinite(s), "invalid block not strict JSON"); + CHECK(bad == 3 && c3_stats_invalid(&st, C3_TEN_UNAVAILABLE, NAN) == 3, "want 3 invalid fields, have %zu", bad); + CHECK(has(s, "\"mean_log_score\": null") && has(s, "\"lag1_z\": {\"value\": null, \"value_status\": \"invalid\""), "invalid values not null + invalid status"); + CHECK(has(s, "\"stats.lag1_z.value\"") && has(s, "\"width80_mean_mc\"") && has(s, "\"mean_log_score\""), "invalid_fields misses a field"); + free(s); + /* a NaN threshold is not "unbounded": invalid */ + fill_ok(&st); st.band_hi[C3_ST_COV95] = NAN; + s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); + CHECK(strict_json(s, n) && bad == 1 && has(s, "\"hi\": null, \"hi_unbounded\": false"), "NaN threshold not invalid"); + free(s); + /* an ungated statistic with no steps is unmeasured, not invalid */ + fill_ok(&st); st.value[C3_ST_REG_IDLE] = NAN; st.gated[C3_ST_REG_IDLE] = 0; st.pass[C3_ST_REG_IDLE] = 0; + s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); + CHECK(strict_json(s, n) && bad == 0 && has(s, "\"regime_idle_cov95\": {\"value\": null, \"value_status\": \"unmeasured\""), "ungated NaN not unmeasured"); + free(s); + /* the gate itself: band() fails a non-finite value (est3c_common.c band) */ + c3_stats j; memset(&j, 0, sizeof j); + j.n = 0; j.logscore = -INFINITY; c3_judge(&j); + CHECK(!j.calibrated, "an empty (all-NaN) statistics block judged calibrated"); + CHECK(c3_stats_invalid(&j, C3_TEN_UNAVAILABLE, NAN) > 0, "an empty statistics block not counted invalid"); +} + +static void test_num(void) +{ + char *buf = NULL; size_t n = 0; FILE *o = open_memstream(&buf, &n); + c3_json_num(o, 0.1); fputc(' ', o); c3_json_num(o, INFINITY); fputc(' ', o); c3_json_num(o, -INFINITY); fputc(' ', o); c3_json_num(o, NAN); + fclose(o); + CHECK(!strcmp(buf, "0.10000000000000001 null null null"), "c3_json_num wrote '%s'", buf); + free(buf); +} + +/* the committed v5 receipt: its bytes must still hash to its name, and it is the known strict-JSON offender */ +static void test_original_untouched(const char *root) +{ + static const char *name = "receipt-7f0bb7018d066bc438e7a8a9bf87c0d45615f60be2d029b6ab5194168e3db3d1.json"; + char path[1400]; snprintf(path, sizeof path, "%s/docs/estimation/receipts/est-v5/%s", root, name); + size_t n; char *b = slurp(path, &n); + CHECK(b != NULL, "committed v5 receipt not found at %s (a missing input fails, it is never skipped)", path); + if (!b) return; + uint8_t dg[32]; char hex[65]; + sha256_hash((const uint8_t *)b, n, dg); est_hex(dg, 32, hex); + CHECK(!strncmp(name + 8, hex, 64), "the committed v5 receipt no longer matches its digest name (%s)", hex); + CHECK(!strict_json(b, n), "validator accepted the committed v5 receipt (it contains bare inf/nan)"); + free(b); +} + +int main(int argc, char **argv) +{ + if (argc == 3 && !strcmp(argv[1], "--validate")) { + size_t n; char *b = slurp(argv[2], &n); + if (!b) { fprintf(stderr, "cannot read %s\n", argv[2]); return 2; } + int ok = strict_json(b, n); + free(b); + if (!ok) fprintf(stderr, "not strict JSON: %s\n", argv[2]); + return ok ? 0 : 1; + } + test_validator(); test_num(); test_finite_and_unbounded(); test_unavailable_baseline(); test_nonfinite_measured_fails_closed(); + test_original_untouched(argc > 1 ? argv[1] : "."); + printf("test_est_json: %d checks, %d failed\n", checks, fails); + return fails ? 1 : 0; +}