diff --git a/.crumb b/.crumb index fb8a4717b..768a53940 100644 --- a/.crumb +++ b/.crumb @@ -47,8 +47,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "7ef7796693f23ad68d9a5bcaaee0f635018f4371139a040ba202d4ce7083ede8", - "last_commit": "dd39bf71f", + "digest": "040c27b16ee202aa5b91d63a2b2c73ac1256d968adef12805ba0eb74f6d8e892", + "last_commit": "fb9444c02", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -59,8 +59,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "8f0e5cb325040247440527f6b54b65cf72c47ff5adca75b4f32a1ddf2b2d6ade", - "last_commit": "1f320caad", + "digest": "0c0d94373c4b536665d59c118e4f4f8282469cade57980b1116b613c28738f91", + "last_commit": "93384bda5", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -72,38 +72,38 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "d94a503f7", + "last_commit": "62d1d0d66", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "last_commit": "089e5b053", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "dcccc0211", + "digest": "c3e9fa8ee45e3c584fa7653f11f8b64f733b584cd7ffdc9aad011daf6abcd06f", + "last_commit": "fb9444c02", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a50c3bd41", + "digest": "80d9328bd7916b151ac14d02566116700c9f0dc14a7c456f3efb985ea865fa1f", + "last_commit": "fb9444c02", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T16:29:04Z", + "children_root": "sha256:a864810661a06314bbbf372c0389c8e53758a4f4f1f2a1863bc672c9f57aa862", + "compiled_at": "2026-10-30T17:46:42Z", "compiler": "crumb-compile/1", - "digest": "sha256:145642bc89d576e2614c2880b07804488e9369307a4d13f55cf04c7396f2e476", + "digest": "sha256:663d4ae3ff7f6392dfb1936ef0ebb75afa61d2dc445b9f149139144c3bab8984", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17850, - "generated_at_commit": "30a54e64589db9b65d24ba595ef0a610ad35d341", - "source_tree": "sha256:7c95a05d1eae5990bd3f40252a6694204823fb08a90298b49a01c933fac8509e" + "files": 17914, + "generated_at_commit": "fb9444c02fa6199f5f338154a27d2570006b189d", + "source_tree": "sha256:97f919df12eddda5149a132237c6294d0e4deb97947e1851c94bc317639ba15d" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 433100df5..1d8218ee6 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -124,14 +124,14 @@ } ], "children_root": "sha256:c81d43c0c392dfec2b6ed5b38ba95ea7c592782b283950e32b777e7b9cfd9d61", - "compiled_at": "2026-10-30T02:47:38Z", + "compiled_at": "2026-10-30T16:52:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:7ef7796693f23ad68d9a5bcaaee0f635018f4371139a040ba202d4ce7083ede8", + "digest": "sha256:040c27b16ee202aa5b91d63a2b2c73ac1256d968adef12805ba0eb74f6d8e892", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", - "files": 138, - "generated_at_commit": "e615e9ff47d03199514580f655d7670e61ab7e2f", - "source_tree": "sha256:ba44cf86b50127e2ec3824c2e913e032178628d69910a85ca9125cc1ed960bb5" + "files": 139, + "generated_at_commit": "93384bda5fd5b9f20ccb182a48fcc19d2015b357", + "source_tree": "sha256:ca7dc20b03c11569b13ee0bcb99364e5aa64672d409e3ad3cc89fa21a189c217" }, "owner_lane": "", "provenance": { diff --git a/docs/one_layer/GEN_PROTOCOL_V1.md b/docs/one_layer/GEN_PROTOCOL_V1.md new file mode 100644 index 000000000..5a14a7558 --- /dev/null +++ b/docs/one_layer/GEN_PROTOCOL_V1.md @@ -0,0 +1,438 @@ +# GEN: compositional generalization from verified abstractions, pre-registered protocol (v1) + +Issue: aien-spark/omega#476 (GEN-1), program aien-spark/omega#475. This file is committed BEFORE any GEN +generation and before the custodian authors any held-out task. Every setting below is frozen by its +commit; the machine-checked copy is `tests/one_layer/generalization/protocol.json`, validated by +`tools/gen/protocol/gencheck` (`make test-gen-protocol`). A change follows section 15. GEN is a separate +experiment from EXP445 (`docs/one_layer/EXP445_PROTOCOL.md`): it reuses EXP445's tools by calling them, +never edits EXP445's protocol, PARAMS, seals or data, and never opens EXP445's sealed tasks. + +## 1. Question + +Does retrieving **checker-verified reusable abstractions** (helper functions extracted from earlier +verified solutions) help a model write correct Omegese programs for **unfamiliar combinations of known +primitives**, compared with one example, a fixed set of many examples, and retrieval of earlier raw +solutions under the same budget? + +A positive answer is claimed only when the abstraction condition beats every control, including raw +retrieval, on held-out task families that no example, dev task or retrieved record shares (section 9). +FAIL and INCONCLUSIVE are legitimate outcomes and are reported as such. + +## 2. Tasks and splits + +Three splits, each made of composition **families** defined by the family contract +`tests/one_layer/generalization/families/FAMSIG_V1.md` (frozen first; tool `tools/gen/protocol/famsig`). +A family is the primitive multiset plus nesting shape the task SPECIFICATION asks for, over the +primitives `arith branch loop array call nested`; it is declared from the prompt, never from a solution. +Renaming identifiers or changing constants never makes a new family. + +| split | count | author | visibility | role | +|---|---|---|---|---| +| dev | 10: `devgen01`..`devgen10` | protocol author (GEN-1) | public: `tests/one_layer/generalization/dev/` | prompt and tool development, gates, negative controls; never evidence | +| corpus (exemplars) | about 48 | abstraction builder (GEN-3) | public: `tests/one_layer/generalization/corpus/` | the B prefix (section 3) and the only source of retrieved records | +| held-out | 60, in at least 6 families | custodian (Unix user `gencust`), who builds no corpus or tool | sealed | the only evidence | + +- Disjointness (FAMSIG_V1 section 4, checked mechanically by `famsig rule`): corpus, dev and held-out + families are pairwise disjoint by signature (R3); every primitive a held-out task uses is used by some + corpus task (R4); every held-out family combines at least two distinct primitives (R5); coarse + signatures (duplicate siblings collapsed, `nested` read as `arith`, a top-level `seq` wrapper around one + container plus leaves dropped) are pairwise disjoint across the three splits (R6); every held-out family + has at least one coarse composition edge that no corpus shape has, `novel_edges >= 1` (R7); no top-level + step of a dev or held-out shape (after `seq` flattening) is a container whose coarse form equals a corpus + coarse signature (R8). +- Held-out family metadata lives only in the sealed `SPLIT` file (FAMSIG_V1 section 6), never inside + `tasks.tar`. Before any held-out generation the custodian publishes `HELDOUT.pub` (FAMSIG_V1 section 7): + task ids, the salted family-list digest, the split digest, counts. The independent reviewer verifies + SPLIT against HELDOUT.pub and R1..R8 after the seal (`gencheck split`) and audits semantic overlap with + the corpus using `famsig rule --show-heldout` (`novel_edges` per family). The reviewer who runs any + `--show-heldout` or `--show` command is never a corpus, record or tool builder. +- **Blind re-declaration (before the seal is opened):** the reviewer reads every held-out prompt and + requirement read-only, as `gencust`, without seeing SPLIT, and declares each task's metadata line in + its own `gen-split 1` file (own salt). `famsig compare SPLIT REDECLARED` (as `gencust`; prints only + `tasks= mismatches=`) must give `mismatches=0`. Any mismatch forces the custodian to fix the + declaration or the task and re-seal (new SPLIT, HELDOUT.pub and pack digests, R1..R8 again) before the + seal is opened. The compare line is published with the run meta. +- **Near-duplicate check (before the seal is opened):** `tools/gen/protocol/genjac scan` (as `gencust`) + of every held-out prompt against every corpus prompt and every corpus reference solution: word + 3-gram Jaccard (genret tokenization: lowercase `[a-z0-9]+` words, set of consecutive word triples); + the number of held-out prompts with any Jaccard strictly above 0.5 (`above=`) must be 0, compared + exactly. Only the summary line (`heldout= corpus= threshold= max_jaccard= above=`) is published. A + non-zero count forces a rewrite and re-seal as above. +- Task layout: exactly `tests/one_layer/acceptance/HARNESS_INTERFACE_V2.md` section 3 (requirement, + prompt, `hidden/inputs`, `hidden/expected`, `hidden/reference.osc`), reused unchanged. The prompt + states every trap kind (HARNESS_INTERFACE_V2 section 3), so a held-out trap line is `TRAP ` with + that code whenever the code is unambiguous (a bare `TRAP` accepts any code). Held-out ids + must not start with `dev`, must not reveal family or difficulty. Expected values are computed + independently of the reference (an oracle written from the prompt, as + `tests/one_layer/generalization/dev/gen_expected.c`), and every reference must be ACCEPTed by + `ola.sh tasks-check` (RUN VALID) before the seal is published. The custodian publishes in the run meta + `oracle_match=/60` (tasks whose `hidden/expected` equals the oracle's output byte for byte; must be + 60/60) and the sha256 of the oracle source file. +- Every held-out `hidden/reference.osc` carries one comment line `// canary <32 lowercase hex>` with a + fresh random value per task, used by the source-leak probe (section 8). +- Dev fixtures: packed and sealed by `tests/one_layer/generalization/dev/pack_dev.sh`; `ola.sh tasks-check` + on that pack gives references_accepted=10/10, RUN VALID (dev pack sha256 in protocol.json). +- Entry names: no corpus task may share an entry name with a held-out task; checked by the custodian + against the public view, printing only the count (as EXP445 13.2). + +## 3. Conditions + +Every condition writes Omegese (`docs/one_layer/OMEGESE.md`); the reader is the EXP445 omegese chain +(`tools/exp445/reader.sh`: `omegese emit`, `omega_emit emit`, `x445 check`); harness kind `oscir`. +Decode-time masking is OFF in every condition (the mask is not qualified); a masked sub-experiment needs +its own revision and is never pooled with these results. + +| condition | system message = prefix ++ suffix | +|---|---| +| A (1-shot) | prefix: `arms/common.md` + `arms/omegese.md` + 1 fixed example; suffix empty | +| B (many-shot) | prefix: the same two documents + 14 fixed corpus examples; suffix empty | +| C (B + raw retrieval) | B's prefix byte for byte + suffix: top-k verified raw corpus solutions | +| D (B + abstraction retrieval) | B's prefix byte for byte + suffix: top-k verified abstraction records | + +- **B prefix pick (deterministic, diverse):** order the corpus tasks by family signature (byte order), + then task id; walk the families round robin in signature order, taking each family's lowest unused + task id, until 14 tasks are taken. Every task line of corpus META is eligible, GEN-3 transfer-probe tasks + included (probes are withheld from both retrieval pools, see Candidate pool below). A's single example is the first task + taken. The 14 ids (`record_sets.prefix_ids`, in pick order) and `record_sets.prefix_sha256` = sha256 of + `tests/one_layer/generalization/records/PREFIX` (those ids, one per line) are pinned in protocol.json when + the corpus is frozen. If the prefix reuses EXP445 + examples, they are corpus tasks with corpus META lines and must pass `famsig rule`. +- **Retrieval (C and D):** the file-backed provider `genret` (`tools/gen/retrieval/CONTRACT.md`, + contract `genret-cli-v3`, pinned in protocol.json `interfaces` at commit `4a48b613` with CONTRACT.md + sha256 `48006271d18394c40daf96120eaa20fbe3908d9a12b08cab7994ee02a6351e9d`; it validates every record + with the GEN-3 `recheck`), the same `--k`, `--cap-bytes`, mode `normal` and lexical ranker for C and D; + only `--kind raw` (C) versus `--kind abstraction` (D) and the record manifest differ. k = 3, + cap_bytes = 4096. **Query** = the task's public `requirement` and `prompt` files passed as + `--requirement` and `--prompt`: genret's query text is the `entry`, `params` and `ret` values plus the + whole prompt, the same bytes `tools/exp445/reader.sh` `user_msg` shows the model (CONTRACT.md section + 3). Retrieved text appears only in the suffix, after the shared prefix. A and B never call the provider. +- **Candidate pool:** C's raw-record manifest is the probe-free set: the verified corpus solutions EXCEPT every + GEN-3 transfer-probe task (`split=probe` in the GEN-3 manifest) and EXCEPT the 14 prefix tasks (already in + every B, C, D prompt; retrieving them would waste C's budget): 23 records. D's manifest holds the probe-free + abstraction records produced by the deterministic GEN-3 tool from the corpus solutions, EXCEPT every + abstraction whose ancestry lies entirely within the 14 prefix tasks (the same rule as C: nothing the + prefix already shows). Both are filtered views of the GEN-3 `records/MANIFEST`, written in the genret + manifest format inside `records/`. Their paths (`record_sets.raw_manifest`, + `record_sets.abstraction_manifest`) and sha256 (`record_sets.raw_manifest_sha256`, + `record_sets.abstraction_manifest_sha256`) are frozen in protocol.json and re-hashed by `gencheck` + BEFORE the seal is opened for the run; no record is added, removed or edited afterwards. Every genret + receipt's `manifest_sha256` must equal the frozen digest of its condition. +- **Shared record skeleton:** C and D records follow one schema (GEN-3 + `tools/gen/abstract/RECORD_SCHEMA.md`, `gen-record 1`). An abstraction record is a callable Omegese + helper with a one-line pre/post, one example call, a test receipt, ancestry of at least 2 distinct + corpus solutions, and an IR fingerprint different from every corpus solution. No pattern templates. +- **What the model sees (rendering, genret-cli-v3, approved here):** the C and D renderings are + byte-identical except for record content. Both start with the one neutral heading, the same 66 bytes + for C and D: `"\nVerified reference code from earlier tasks, most relevant first.\n"` (approved + verbatim). Then per record `Record .`, the `entry`, `params` and `ret` lines (the signature), the + one-line description, and the body in a fenced block. Only those record fields differ between C and D + (a whole raw solution versus a factored helper `fn`). The omission of D's pre/post, example call, test + receipt, ancestry and fingerprint from the prompt is approved: they are validated by `recheck` and + logged in the retrieval output, never shown. A heading or any other framing text that names the record + kind is a confound and is not allowed (genret-cli-v2 used kind-specific headings and is not used). +- **Selection cap report:** per task and condition the number of records genret selected (`selected`) + is logged; the dev and held-out reports give, for C and D separately, how many tasks got fewer than k + = 3 records (cap or pool exhausted), so a lift cannot hide behind D simply receiving fewer, shorter + records. +- Realised prompt tokens and suffix bytes are logged per condition and task (run row, GEN-2 FORMATS.md). + +## 4. Model and configuration + +- Config A only: the model, endpoint, request extra, server pin and weight list of EXP445 config A, read + from `tools/exp445/PARAMS` (sha256 pinned in protocol.json): Qwen3-30B-A3B-Instruct-2507 FP8 on Omega's + own `aien-serve-http` (`--engine omega`, :18091). Read-only inference; no weights change. Config B and + OWN-8B need a later revision. +- The GPU run happens after EXP445 Stage 1 attempt 1, under the quiet flag and GPU lock, never + concurrently with any other use of :18091. +- No paid model, new hardware or external service. + +## 5. Generation procedure + +1. Requests: `messages = [{system: prefix ++ suffix}, {user: reader.sh user_msg(requirement, prompt)}]`, + one POST to `/chat/completions`, `stream: false`, the configuration's request extra merged. + `base_url` is PARAMS `config.A.base_url` and already ends in `/v1` (`http://127.0.0.1:18091/v1`); + `sampling-gate.sh` takes the base WITHOUT `/v1` (it posts to `/v1/chat/completions`), so the + gate is called with `config.A.base_url` minus its trailing `/v1`. Both reach the same endpoint. +2. **One request in flight:** gates and run send exactly one request at a time to the server (no + concurrency, no batching across tasks or conditions); the next request starts only after the previous + one has returned or failed. +3. **Budget:** `max_tokens = 3072` per round in every condition, task and attempt (EXP445 section 5: + 3.3 times the longest dev reference in the most verbose spelling; Omegese references are shorter + than OSC text, OMEGESE.md section 5, so the cap is not binding for a correct answer). + `finish_reason=length` is logged. +4. **Greedy gate (before attempt 1):** the 10 dev tasks in condition B, each sent twice at temperature 0: + both replies byte-identical in content and every reply reports `mode=greedy`. No gate PASS, no + primary result (verdict INVALID). +5. **Attempt 1 = PRIMARY:** temperature 0 (greedy), round 1 only. Its candidate is set `-1` + (`a-1` .. `d-1`). Every attempt-1 response must report `usage.aien_sampling.mode = greedy`; a single + attempt-1 response reporting anything else (or nothing) makes the run INVALID (section 8). +6. **Attempts 2 and 3 (pass@3, secondary):** temperature 0.7, top_p 0.8, seeded, only when the G0 sampling + gate passes: the Stage S gate vendored as `tools/exp445/sampling-gate.sh` (sha256 in protocol.json) + with its repro requests set to `devgen01` in condition B at the eight seeds s1..s8 = 476901, 476902, + 476903, 476904, 476905, 476906, 476907, 476908 (protocol.json `gates.g0.seed_values`; disjoint from + every run seed), plus one repeat of s1 = 476901, PASS rules as EXP445 13.4. Otherwise pass@3 is + reported `UNAVAILABLE`. Sets `-2`, `-3`. + **pass@3** of a condition = the tasks with an `ACCEPT` in the union of the round-1 sets `-1`, + `-2`, `-3` only (never a `-final` set), labelled "pass@3 (1 greedy + 2 sampled)". +7. **Repair (secondary only):** exactly one public-check repair round when round 1 is refused, as EXP445 + section 6 step 4; the final candidate is set `--final`. Never part of the primary or of + pass@3. +8. **Seeds:** `seed = 476000 + 1000 x attempt + task index` (1-based position in the sorted task list), + the same for the four conditions of one task and attempt; sent and logged for attempt 1 too. +9. **Transport retry:** a try fails at transport level when there is a transport error, an HTTP status + other than 200, or a 200 with **no usable answer**, defined mechanically: the body is not valid JSON, + or it has no `choices` array, or `choices[0].message.content` is missing or not a string. A failed try + is retried up to 2 times with the byte-identical request (same seed, same temperature, same top_p) + after K x 5 s before try K. A retry repeats the same sample; it is never an extra sample. Still + failing after the third try = no candidate (`no_candidate`, a failure). A well-formed 200 whose + content is empty, a refusal or not a program is NOT a transport failure: that content is the + candidate and is graded (it fails). Attempt-1 `no_candidate` is counted per condition; any count + above 0 makes the run INVALID (section 8), because a transport failure that hits one condition more + than another biases the paired comparison. +10. **Order:** attempt, then task, then condition (A, B, C, D), so attempt 1 of every task in all four + conditions finishes first. A run resumes only with identical settings; finished generations are + never redone. +11. Everything is hashed and logged as `tools/gen/runner/FORMATS.md` (GEN-2) specifies: raw requests and + responses, prompt manifests, retrieval outputs, run rows, SHA256SUMS. + +## 6. Success + +- **Functional success** of a candidate = the sealed harness (`ola.sh experiment`, kind `oscir`) gives + `ACCEPT` on the task's hidden inputs. Anything else (any REJECT reason, `no_candidate`) is failure. +- **Primary measure:** attempt-1, round-1 functional success per condition over the 60 held-out tasks, + paired by task. +- **Checker acceptance** (the public check or checker2 accepting a candidate) is NOT success: a program + that compiles and passes the checker can compute the wrong function. It is reported separately + (`checker_ok`) and never pooled with, or substituted for, functional success. +- The grader runs as `gencust` from a clean tree and publishes a public grading receipt that the reducer + consumes. +- **Outcome classes (decision input):** the grading receipt gives, for every held-out task x condition + (attempt 1, round 1), exactly one class: `ACCEPT`, `REJECT:` (the harness reason, e.g. + `REJECT:wrong_result`), `no_candidate` (section 5 step 9) or `MISSING` (no row for that task and + condition). Only `ACCEPT` is success. The reducer refuses to reduce (no verdict at all, not even + INVALID) when any cell is `MISSING`, when the number of distinct held-out tasks n is not 60, or when a + task x condition cell appears more than once. + +## 7. Secondary measures (reported, never decisive) + +Checker acceptance; pass@3 (1 greedy + 2 sampled, or UNAVAILABLE); success after the repair round; +refusal and REJECT reasons by class; prompt and completion tokens per condition; realised suffix bytes; +records selected per task and the number of tasks with fewer than 3 records, for C and D separately +(section 3); wall time; tokens and wall seconds per correct program; Wilson 95% intervals for every +proportion; the 90% Newcombe bounds. + +## 8. Validity (any failure: verdict INVALID) + +- complete paired matrix: every held-out task x A, B, C, D x attempt 1, exactly once (60 x 4); +- every attempt-1 response reports `mode=greedy`, and attempt-1 `no_candidate` is 0 in each of A, B, C + and D (section 5 steps 5 and 9; the per-condition counts are in the receipt); +- the harness run ends `RUN VALID` (every reference accepted, zero errors, identical replay, probes + PASS, clean tree); +- the greedy gate PASSed before attempt 1; every request was sent one at a time (section 5 step 2); +- every pin of section 13 equals protocol.json; every run SHA256SUMS verifies; every candidate re-derives + from its hashed response; protocol.json passes `gencheck manifest` and `gencheck run-ready` (no null + field, every interface file present, `genret-cli-v3` pinned; there is no "pending merge" exception); +- **seal-open receipt:** before the first `tasks-public` of the sealed pack for generation (= the seal is + opened), the custodian runs its published seal-open script, which runs `gencheck open` and writes + `OPEN`, five lines exactly: + `gen-open 1` / `omega_commit <40 hex>` / `protocol_sha256 ` / + `tasks_sha256 <64 hex>` / `heldout_pub_sha256 <64 hex>`, commits it as + `evidence/GEN/OPEN` and pushes that commit to origin. The run is valid only if that push happened + before the first `tasks-public`, and protocol.json at `omega_commit` passes `gencheck open` with that + OPEN: the record-set manifests (paths and digests), corpus META, prefix, `splits.heldout.pub_sha256` + and `splits.heldout.tasks_sha256` all set (none null) and matching. The reducer refuses a run whose + OPEN commit has a null or different digest; +- `gencheck split` PASSed on SPLIT and HELDOUT.pub against protocol.json at the OPEN commit (it fails + closed when either held-out digest is null), the blind re-declaration gave `mismatches=0` and the + near-duplicate scan gave `above=0` (section 2), all before the seal was opened; +- `oracle_match=60/60` published by the custodian (section 2); +- clean generation tree whose tool files equal the scoring commit's; +- **sanity flag:** B's attempt-1 successes are not lower than A's by more than 2 tasks + (A - B > 2 = the many-shot prefix is broken: INVALID); +- **source-leak probe:** no request, retrieval output or record contains any held-out canary + (section 2) or the bytes of any held-out `hidden/inputs` file; checked by the custodian as `gencust` + after generation, printing only a count (must be 0); +- isolation audit clean (section 12), including no model request by the custodian. + +## 9. Statistics and decision (exact) + +Notation for condition D against a control X in {A, B, C}, over the n held-out tasks (n = 60): `both`, +`d_only`, `x_only`, `neither` = tasks where both / only D / only X / neither succeed (attempt 1, round 1). + +- **Lift:** `diff = (d_only - x_only) / n`; `lift_ok` iff `10 x (d_only - x_only) >= n` (exact integers; + delta = 10 percentage points = 6 tasks at n = 60). +- **Exact one-sided McNemar** (H1: D better): `p = P(Y >= d_only)`, Y ~ Binomial(d_only + x_only, 1/2), + computed exactly as `p_num / 2^(d_only + x_only)` (p = 1 when there are no discordant tasks); + `p_ok` iff `20 x p_num < 2^(d_only + x_only)` (p < 0.05, exact). +- **Newcombe paired interval for D - X**, method 10 of Newcombe (1998), Statist. Med. 17:2635-2650: + a = both, b = d_only, c = x_only, d = neither, p1 = (a+b)/n, p2 = (a+c)/n, theta = (b - c)/n. + (l1, u1), (l2, u2) = Wilson score intervals (no continuity correction) of p1, p2, with lower limit + exactly 0 at a count of 0 and upper exactly 1 at a count of n. phi = N / sqrt((a+b)(c+d)(a+c)(b+d)), + N = a d - b c, replaced by max(a d - b c - n/2, 0) when a d > b c; phi = 0 when the denominator is 0. + delta = sqrt((p1-l1)^2 - 2 phi (p1-l1)(u2-p2) + (u2-p2)^2), eps = sqrt((u1-p1)^2 - 2 phi (u1-p1)(p2-l2) + + (p2-l2)^2) (a negative radicand from rounding is taken as 0); interval [theta - delta, theta + eps], + clamped to [-1, 1]. Constants: z = 1.959963984540054 (95%), z = 1.644853626951472 (90%, two-sided; + its upper limit is a one-sided 95% bound). IEEE double arithmetic. +- **Verdict**, in this order: + 1. **INVALID** when any validity condition (section 8) fails. + 2. **INCONCLUSIVE, reason `floor`** when max(successes of A, B, C, D) <= 3, and **INCONCLUSIVE, reason + `ceiling`** when min(successes of A, B, C, D) >= 57 (both reasons when both hold): with at most 3 + successes everywhere, or at least 57 everywhere, the paired comparison cannot measure anything, and + neither PASS nor FAIL is claimed. Integers, exact (protocol.json `decision.floor_max = 3`, + `decision.ceiling_min = 57`). + 3. **PASS** when, for every X in {A, B, C}: `lift_ok` and `p_ok` (intersection-union test: each control + at level 0.05, no multiplicity correction needed). PASS is reported in exactly these words: "D beats + every control at one-sided 0.05 with observed lift >= 10pp". It never means "a lift of 10pp or more + is established": the observed lift is a point estimate, and the Newcombe intervals are reported + beside it. + 4. **FAIL** when the upper limit of the 95% Newcombe interval for D - C, as the full IEEE double (never + the 6-decimal value printed in the receipt), is below 0.10. C (raw retrieval) is the designated FAIL + control: the strongest fair baseline by design. + 5. **INCONCLUSIVE** otherwise (reasons: each failing `lift_` and `p_`). + PASS and FAIL cannot both hold (PASS needs theta(D-C) >= 0.10, and the upper limit is at least theta). + The receipt names the verdict and its reason tags. +- At n = 60 this test can only separate large effects: most outcomes that are not a clear win will be + **INCONCLUSIVE**, and that is the honest report, not a failure of the experiment. The 90% upper limit is + reported beside the 95% one and is not decisive. +- **Test vectors:** `tests/one_layer/generalization/stats_vectors.tsv`, generated by + `tools/gen/protocol/genstats vectors` (never by hand), with Wilson, McNemar, Newcombe and full verdict + records (including the floor and ceiling boundaries) and length-match records (section 10). `genstats selftest` reproduces methods 8 and 10 on all 18 rows of the paper's Table III to the + printed 4 decimals (one method-10 cell, row 1/97/1/1, is printed 0.8736 where the paper's own method-8 + cell for the identical formula is 0.8737; the computed value is 0.873672). The GEN reducer must + reproduce every vector (reals within 1e-9; counts, p_num, p_den, yes/no and verdicts exactly). +- The receipt (`omega.gen.decision.v1`, GEN-2 FORMATS.md) reports for each condition the attempt-1 + successes with Wilson 95% intervals and checker acceptance, and for each D-X pair the four counts, diff, + p, both Newcombe intervals, lift_ok and p_ok, and the verdict with its reasons. + +## 10. Negative controls (pre-registered; run on the dev tasks; diagnostics, not decisive) + +| control | how | expectation if retrieval works as intended | +|---|---|---| +| no retrieval | C and D with genret mode `empty` | equals B up to sampling noise | +| irrelevant records | genret mode `irrelevant` | no better than B | +| length-matched C | C plus whole extra raw records (rank k+1, ...) until at least D's realised suffix bytes for the same task (genret `length-matched`, `--target-bytes` = D's `suffix_bytes`) | separates "more context" from "better content" on matched tasks only (see below) | +| cross-family | C and D with `--exclude-family ` | shows how much a lift depends on same-family records (on dev, own-family records do not exist in the corpus by R3, so this must change nothing: a check of the split) | +| source-leak probe | canaries and hidden inputs searched in every request (section 8) | 0 hits | + +**Length-matched C (secondary, dev-only diagnostic).** It never feeds the PASS / FAIL / INCONCLUSIVE +verdict or run validity. genret-cli-v3 pads C with whole extra raw records, never truncated, in rank order +after the k selected (rank k+1, k+2, ...), until C's suffix is at least D's realised `suffix_bytes` for +the same task (`--target-bytes`): `pad_reason=reached` (it may overshoot by up to one record), +`exhausted` (candidates ran out, still short) or `c_not_shorter` (C was already at least as long; nothing +padded). Its receipt logs `length_gap` = padded C `suffix_bytes` - target, signed. Because padding moves +in whole solutions, it cannot hit a fine target, so each dev task is classified: + +- **matched** when |length_gap| <= max(256 bytes, 15% of the target), compared exactly as + `100 x |gap| <= max(25600, 15 x target)` (protocol.json `retrieval.length_match_abs_bytes = 256`, + `retrieval.length_match_frac = 0.15`); +- **unmatched** otherwise: listed with its gap and pad_reason, never silently dropped. + +The report gives, per dev task, C's and D's `suffix_bytes`, the padded C `suffix_bytes`, pad count, +pad_reason, length_gap and class; then the matched and unmatched counts; then the attempt-1 success of +length-matched C on the matched tasks only, next to normal C and D on those same tasks. When fewer than +50% of the dev tasks are matched (`retrieval.length_match_min_matched_frac = 0.5`; fewer than 5 of 10), +the result is reported plainly as `length-matched ablation: UNAVAILABLE ( of dev tasks matched)` +and no conclusion about prompt length is drawn. The matched tasks are a selected subset (tasks with +short solutions match more easily), so the report states that the length-matched result applies to the +matched subset only, gives its size, and is not an estimate for the whole dev set. The comparison is +read in both directions: when C's suffix is longer than D's (gap > 0, including `c_not_shorter`) and C +still does worse than D, that is evidence against the "D only wins because its prompt is longer" +explanation and is reported as such; when C is shorter and loses, length is not ruled out. Reference +values: `genstats lenmatch : ...`, and the `lenmatch` / `lenset` records in +`stats_vectors.tsv`. +The held-out decision uses only conditions A, B, C, D in mode `normal`. + +## 11. Dev pilot (before the held-out run, never evidence) + +Config A, the 10 dev tasks, all four conditions, attempt 1, plus the negative controls of section 10. +GO when: the greedy gate passes; every generation is logged and re-derives; the dev harness run ends +RUN VALID; B is not worse than A by more than 2 tasks. Builders may change prompts, records or tools +using dev results only, then rerun, at most twice; each change before the seal is opened is a protocol +revision under section 15. + +## 12. Isolation + +- The custodian is the dedicated Unix user `gencust`; the sealed directory is owned by it, mode 0700. + The custodian authors, seals and grades through `sudo -u gencust`. Builders and the runner never read + it; the runner sees only `ola.sh tasks-public` output. +- Every sudo call is in the system journal. Before the run, and again before the receipt is accepted, + the operator audits the journal (`journalctl _COMM=sudo` for the run window) and lists every command + run as `gencust`; any command not in the custodian's published script list is an isolation failure + (INVALID). +- **The custodian never queries any model.** No custodian script and no command run as `gencust` sends + a request to a model endpoint (:18091, :18097 or any other). The journal audit above also searches + every `gencust` command line for `18091`, `18097`, `chat/completions` and `/v1/`; any hit is an + isolation failure (INVALID). The audit sees command lines only, not traffic inside an allowed script, + so every script in the custodian's published list is reviewed before the run to contain no network + request (stated as a limit of the audit). +- Hidden inputs, expected values, references and grader output are never inputs to generation or + retrieval (GEN-2 manifests record every input). No grader feedback reaches the treatment during + generation; the repair round uses only the public check. + +## 13. Pins recorded with the run (all in protocol.json or the run's meta) + +Model id, endpoint, request extra, server binary sha256 and source commits (via `tools/exp445/PARAMS`); +weight file hashes (`tools/exp445/weights.A.sha256`, re-checked by the operator before the run); +tokenizer files (in the weight list); Omega commit of the run and of scoring; harness contract +(HARNESS_INTERFACE_V2.md) sha256; reader chain (`tools/exp445/reader.sh`) and arm documents sha256; +B and A prefix sha256 and the 14 prefix ids; C and D manifest paths and sha256 (record-set digests); the OPEN +receipt and its commit; genret contract +and binary sha256; dev pack sha256; held-out `TASKS.sha256`, `split_sha256`, `famlist_sha256`; +family contract and famsig sha256; the blind re-declaration line (`tasks= mismatches=`), the genjac summary +line and `oracle_match` with the oracle source sha256; stats vectors sha256; seeds (run and G0), k, +cap_bytes, max_tokens; per-condition attempt-1 `no_candidate` counts; hardware +(DGX Spark GB10) and the GPU-lock / quiet-flag state at run time; this protocol's sha256. + +## 14. Commands + +``` +make test-gen-protocol # famsig, gencheck, genstats, genjac, dev pack (CPU only) +tools/gen/protocol/gencheck manifest tests/one_layer/generalization/protocol.json . +tests/one_layer/generalization/dev/pack_dev.sh +tests/one_layer/acceptance/ola.sh tasks-check /tasks.tar /TASKS.sha256 +famsig rule tests/one_layer/generalization/dev/META tests/one_layer/generalization/corpus/META # custodian, as gencust +famsig compare # reviewer's blind re-declaration, as gencust +genjac scan # near-duplicate check, as gencust +gencheck split tests/one_layer/generalization/protocol.json . # reviewer, after the seal +gencheck run-ready tests/one_layer/generalization/protocol.json . # before seal-open and before the run +gencheck open tests/one_layer/generalization/protocol.json . # custodian at seal-open; reducer before reducing +``` + +The runner, reducer, corpus builder and custodian scripts are specified by their own issues (GEN-2 +#477, GEN-3 #478, GEN-4 #479, GEN-6 #480) and must implement sections 3 to 9 as written here. + +## 15. Revisions + +- Before the seal is opened for the held-out run, a change is a new revision: edit this file and + protocol.json in one commit, add a line below, bump `revision`, and re-run `make test-gen-protocol`. + A revision may use dev results only; no held-out content is ever opened to justify it. +- After the seal is opened, nothing in sections 2 to 9 changes. Any change (tasks, conditions, budget, + decision rule) is a new protocol version (`GEN_PROTOCOL_V2.md`) with a NEW sealed held-out pack; the + v1 result stands as reported. Negative or inconclusive results are never a reason to rewrite v1. + +- **v1**: first registration (PR #486). +- **v1, amended at review of PR #486 before merge and before any held-out task existed** (revision stays + 1: v1 was never on main): record-set manifest paths re-hashed and the public OPEN seal-open receipt; + R6 coarse disjointness and R7 novel edges; blind re-declaration (`famsig compare`) and the genjac + near-duplicate check; floor and ceiling INCONCLUSIVE; PASS wording; mode=greedy and zero attempt-1 + `no_candidate` as validity conditions; mechanical "no usable answer", identical retries, one request in + flight; pass@3 from round-1 sets; outcome classes and reducer refusal on MISSING or n != 60; D pool + excludes prefix-only abstractions; FAIL on the full double; `gencheck run-ready`, `open`, split fail + closed on null digests; custodian model-query ban; oracle_match; numeric G0 seeds; base_url and query + bytes stated; genret-cli-v3 pinned with its heading approved; length-matched ablation made a dev-only + diagnostic with matched/unmatched tasks and UNAVAILABLE below 50% matched. +- **v1, record sets pinned (before merge, before any held-out task):** corpus META, the 14 prefix ids, + records/PREFIX and the C and D manifests from GEN-3 commit 856cc977 (prefix recomputed from the pick + rule and matching); genret-cli-v3 re-pinned at 4a48b613. +- **v1, R8 and verdict n (before merge, before any held-out task):** rule R8 (no dev or held-out + top-level step is a corpus family in coarse form; dev/META passes against the GEN-3 corpus META); + `genstats verdict` refuses n != 60; the dev-pack tasks-check test runs in a private git worktree; the C and D pools stated as + probe-free (GEN-3 `split=probe` filtered out; C pool count pending from GEN-3). The sentence "Every task + line of corpus META is eligible, GEN-3 transfer-probe tasks included" (probes allowed in the B prefix) + was added in commit 8d940556; probes stay eligible for the prefix only. +- **v1, record sets re-pinned to GEN-3 commit e521e818 (before merge, before any held-out task):** C.MANIFEST + is the probe-free raw pool, 23 records (sha256 `d6b8a9a4...`); D.MANIFEST 9 records (sha256 `1fe9af29...`); + corpus META and records/PREFIX unchanged. Pool sizes bound k: k = 3 is below both pools, and any k above 9 + (D's pool) would let D exhaust its pool and is a new revision. The file pins of `OMEGESE.md`, `ola.sh`, + `reader.sh` and `sampling-gate.sh` re-hashed after main's Apache-2.0 header and OM-2 changes (no protocol + content changed); `famsig_lib.h` and `genstats.c` re-hashed after bounded-write fixes (CodeQL 537-541). diff --git a/mk/.crumb b/mk/.crumb index e38efa48f..fd64c034f 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-28T03:06:12Z", + "compiled_at": "2026-10-30T03:04:05Z", "compiler": "crumb-compile/1", - "digest": "sha256:8f0e5cb325040247440527f6b54b65cf72c47ff5adca75b4f32a1ddf2b2d6ade", + "digest": "sha256:0c0d94373c4b536665d59c118e4f4f8282469cade57980b1116b613c28738f91", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 75, - "generated_at_commit": "79b8c1f9c24195c53ac8dad111d35175a4431506", - "source_tree": "sha256:b8302c1e15664dfe98c550a03830f5d4cf75c5451a6c75eed5042e623dea5d10" + "files": 76, + "generated_at_commit": "2899a0a37eb0cd125d30804df4c6b6462bbdac8f", + "source_tree": "sha256:98d109696caea22224abbb1b2c0071320d9cd5ce5777d6f84cebc5599c152e03" }, "owner_lane": "", "provenance": { diff --git a/mk/gen-protocol.mk b/mk/gen-protocol.mk new file mode 100644 index 000000000..c682347e9 --- /dev/null +++ b/mk/gen-protocol.mk @@ -0,0 +1,36 @@ +# GEN-1: pre-registered generalization protocol tools (aien-spark/omega#476, docs/one_layer/GEN_PROTOCOL_V1.md). +# Picked up by `-include mk/*.mk`; not part of `all` or `test`. CPU only. +# +# gen-protocol-tools build/gen_protocol/{famsig,gencheck,genstats,genjac} +# test-gen-protocol genstats selftest, genjac known answers + byte-identical stats_vectors.tsv, famsig known answers and mutants, +# gencheck on tests/one_layer/generalization/protocol.json and its mutants (manifest, +# tree, synthetic held-out split), dev pack reproducibility + oracle negative control. +# GEN_TASKS_CHECK=1 also runs ola.sh tasks-check on the dev pack (clean tree needed). +ifndef GEN_PROTOCOL_MK +GEN_PROTOCOL_MK := 1 +.PHONY: gen-protocol-tools test-gen-protocol +GENP_SRC = tools/gen/protocol +GENP_DIR = $(OUT_DIR)/gen_protocol +GENP_CFLAGS = -std=gnu11 -Wall -Wextra -Werror -O2 -Isrc + +$(GENP_DIR)/famsig: $(GENP_SRC)/famsig.c $(GENP_SRC)/famsig_lib.h src/omega_om_units.c src/sha256.h + @mkdir -p $(GENP_DIR) + $(CC) $(GENP_CFLAGS) -o $@ $(GENP_SRC)/famsig.c src/omega_om_units.c + +$(GENP_DIR)/gencheck: $(GENP_SRC)/gencheck.c $(GENP_SRC)/famsig_lib.h src/omega_om_units.c src/sha256.h + @mkdir -p $(GENP_DIR) + $(CC) $(GENP_CFLAGS) -o $@ $(GENP_SRC)/gencheck.c src/omega_om_units.c + +$(GENP_DIR)/genstats: $(GENP_SRC)/genstats.c + @mkdir -p $(GENP_DIR) + $(CC) $(GENP_CFLAGS) -o $@ $(GENP_SRC)/genstats.c -lm + +$(GENP_DIR)/genjac: $(GENP_SRC)/genjac.c + @mkdir -p $(GENP_DIR) + $(CC) $(GENP_CFLAGS) -o $@ $(GENP_SRC)/genjac.c + +gen-protocol-tools: $(GENP_DIR)/famsig $(GENP_DIR)/gencheck $(GENP_DIR)/genstats $(GENP_DIR)/genjac + +test-gen-protocol: + CC="$(CC)" bash $(GENP_SRC)/test_gen_protocol.sh $(abspath $(GENP_DIR))/test +endif diff --git a/tests/.crumb b/tests/.crumb index 5fbfcb29c..0de495e49 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -302,14 +302,14 @@ } ], "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T16:52:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:c3e9fa8ee45e3c584fa7653f11f8b64f733b584cd7ffdc9aad011daf6abcd06f", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "files": 1442, + "generated_at_commit": "93384bda5fd5b9f20ccb182a48fcc19d2015b357", + "source_tree": "sha256:74ef41e808a6fb9a33a582f5e8ed820feb740c1d3f99fae3f511b5343cbd4f8e" }, "owner_lane": "", "provenance": { diff --git a/tests/one_layer/generalization/dev/META b/tests/one_layer/generalization/dev/META new file mode 100644 index 000000000..fb04bfcd0 --- /dev/null +++ b/tests/one_layer/generalization/dev/META @@ -0,0 +1,13 @@ +gen-meta 1 +# GEN public dev fixtures (aien-spark/omega#476): family metadata, FAMSIG_V1.md format. +# task split arith branch loop array call nested shape +devgen01 dev 0 0 1 0 1 1 loop(call(nested)) +devgen02 dev 0 0 1 0 1 1 loop(call(nested)) +devgen03 dev 0 0 1 0 1 1 loop(call(nested)) +devgen04 dev 1 1 1 0 0 1 branch(loop(arith),nested) +devgen05 dev 1 1 1 0 0 1 branch(loop(arith),nested) +devgen06 dev 1 1 1 0 0 1 branch(loop(arith),nested) +devgen07 dev 1 1 1 2 0 0 seq(array,loop(array,branch(arith))) +devgen08 dev 1 1 1 2 0 0 seq(array,loop(array,branch(arith))) +devgen09 dev 1 1 2 0 0 0 loop(loop(branch(arith))) +devgen10 dev 1 1 2 0 0 0 loop(loop(branch(arith))) diff --git a/tests/one_layer/generalization/dev/gen_expected.c b/tests/one_layer/generalization/dev/gen_expected.c new file mode 100644 index 000000000..2bf390f7a --- /dev/null +++ b/tests/one_layer/generalization/dev/gen_expected.c @@ -0,0 +1,130 @@ +/* + * gen_expected.c -- independent C computation of hidden/expected for the PUBLIC GEN dev tasks + * devgen01..devgen10 (aien-spark/omega#476; tests/one_layer/generalization/dev/). Written from each task's + * prompt, not from its reference.osc (closed forms or exact 128-bit arithmetic where the reference loops), + * so a reference that disagrees with its prompt is caught by `ola.sh tasks-check`. + * gen_expected < hidden/inputs > hidden/expected + * Refuses (exit 1) an input outside what the prompt promises ("for every input checked ..."). + */ +#include +#include +#include +#include + +typedef __int128 i128; + +static int rd(int64_t *v, int n) +{ + char buf[256]; + if (!fgets(buf, sizeof buf, stdin)) return 0; + char *s = buf; + for (int i = 0; i < n; i++) { + char *e; + v[i] = strtoll(s, &e, 10); + if (e == s) { fprintf(stderr, "gen_expected: bad input line\n"); exit(1); } + s = e; + } + return 1; +} + +static void refuse(const char *t, const char *why) { fprintf(stderr, "gen_expected: %s: input %s\n", t, why); exit(1); } + +static void pr128(i128 v) +{ + char b[64]; + int n = 0, neg = v < 0; + unsigned __int128 u = neg ? (unsigned __int128)(-(v + 1)) + 1 : (unsigned __int128)v; + do { b[n++] = (char)('0' + (int)(u % 10)); u /= 10; } while (u); + if (neg) putchar('-'); + while (n) putchar(b[--n]); + putchar('\n'); +} + +static int fits32(i128 v) { return v >= INT32_MIN && v <= INT32_MAX; } +static int fits64(i128 v) { return v >= INT64_MIN && v <= INT64_MAX; } + +int main(int argc, char **argv) +{ + if (argc != 2) { fprintf(stderr, "usage: gen_expected < inputs\n"); return 2; } + const char *t = argv[1]; + int64_t v[8]; + if (!strcmp(t, "devgen01")) while (rd(v, 1)) { + /* sum_{i UINT32_MAX) refuse(t, "result exceeds 32 bits"); + pr128(s); + } + else if (!strcmp(t, "devgen02")) while (rd(v, 2)) { + /* exact sum; i32 semantics: trap iff some partial result leaves i32. The prompt promises that + * either every partial result fits or the final sum does not fit; check that promise. */ + i128 n = v[0], c = v[1], s = 0; + int partial_overflow = 0; + for (i128 x = 0; x < n; x++) { + i128 a = x - c, b = x + c, p = a * b, g = p + x; + if (!fits32(a) || !fits32(b) || !fits32(p) || !fits32(g)) partial_overflow = 1; + s += g; + if (!fits32(s)) partial_overflow = 1; + } + if (partial_overflow && fits32(s)) refuse(t, "a partial result overflows but the final sum fits"); + if (partial_overflow) printf("TRAP\n"); else pr128(s); + } + else if (!strcmp(t, "devgen03")) while (rd(v, 2)) { + uint64_t s = (uint64_t)v[0]; + for (uint64_t i = 0; i < (uint64_t)v[1]; i++) s = (s * 31 + i * i + 7) % 65536; + printf("%" PRIu64 "\n", s); + } + else if (!strcmp(t, "devgen04")) while (rd(v, 1)) { + i128 n = v[0]; + pr128(n % 2 == 0 ? n * (n + 1) / 2 : n * n * n - 2 * n + 5); + } + else if (!strcmp(t, "devgen05")) while (rd(v, 2)) { + i128 x = v[0], k = v[1]; + if (x < 0) { pr128(3 * x * x + 5 * x - 2); continue; } + /* t_k = (x + 1) * 2^k - 1; 2t overflows before t_k does only if t_k overflows (2t <= 2^63 - 2) */ + i128 t1 = x + 1; + int over = 0; + for (i128 i = 0; i < k && !over; i++) { t1 *= 2; if (t1 - 1 > INT64_MAX) over = 1; } + if (over) printf("TRAP\n"); else { if (!fits64(t1 - 1)) refuse(t, "range"); pr128(t1 - 1); } + } + else if (!strcmp(t, "devgen06")) while (rd(v, 2)) { + i128 b = v[0], e = v[1]; + if (b < 10) { + unsigned __int128 r = 1; + for (i128 i = 0; i < e; i++) { r *= (unsigned __int128)b; if (r > UINT64_MAX) refuse(t, "b^e exceeds 64 bits"); } + pr128((i128)r); + } else pr128((b * b + e * e) * 3 + b); + } + else if (!strcmp(t, "devgen07")) while (rd(v, 4)) { + i128 best = (i128)v[1] - v[0]; + for (int i = 1; i < 3; i++) if ((i128)v[i + 1] - v[i] > best) best = (i128)v[i + 1] - v[i]; + pr128(best); + } + else if (!strcmp(t, "devgen08")) while (rd(v, 5)) { + int k = 0; + for (int i = 0; i < 4; i++) k += v[i + 1] > v[i]; + printf("%d\n", k); + } + else if (!strcmp(t, "devgen09")) while (rd(v, 2)) { + /* count by residues: pairs i < j with (i mod k) + (j mod k) = 0 mod k */ + int64_t n = v[0], k = v[1]; + if (k < 1) refuse(t, "k = 0"); + int64_t cnt[256] = {0}; + for (int64_t i = 1; i <= n; i++) cnt[i % k]++; + int64_t pairs = cnt[0] * (cnt[0] - 1) / 2; + for (int64_t r = 1; r < k; r++) { + int64_t q = k - r; + if (r < q) pairs += cnt[r] * cnt[q]; + else if (r == q) pairs += cnt[r] * (cnt[r] - 1) / 2; + } + printf("%" PRId64 "\n", pairs); + } + else if (!strcmp(t, "devgen10")) while (rd(v, 2)) { + /* for each i, the j count is min(n, m / i) */ + int64_t n = v[0], m = v[1], c = 0; + for (int64_t i = 1; i <= n; i++) { int64_t q = m / i; c += q < n ? q : n; } + printf("%" PRId64 "\n", c); + } + else { fprintf(stderr, "gen_expected: unknown task %s\n", t); return 2; } + return 0; +} diff --git a/tests/one_layer/generalization/dev/pack_dev.sh b/tests/one_layer/generalization/dev/pack_dev.sh new file mode 100755 index 000000000..769f0b331 --- /dev/null +++ b/tests/one_layer/generalization/dev/pack_dev.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# tests/one_layer/generalization/dev/pack_dev.sh -- pack and seal the PUBLIC GEN dev fixtures +# devgen01..devgen10 (aien-spark/omega#476) into /tasks.tar + /TASKS.sha256, exactly the +# held-out layout of tests/one_layer/acceptance/HARNESS_INTERFACE_V2.md section 3. Same steps as +# tests/one_layer/exp445/pack_dev.sh (which packs only its own directory, so it cannot be called here): +# first every hidden/expected is checked against gen_expected.c (computed from the prompts, independently +# of reference.osc; a mismatch refuses), and the case directories must be exactly the task ids of META. +# Reproducible bytes (ustar, sorted, owner 0, mtime 0). META stays OUTSIDE the tarball (member rules). +# Then: tests/one_layer/acceptance/ola.sh tasks-check /tasks.tar /TASKS.sha256 +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUT="${1:?usage: pack_dev.sh }" +mkdir -p "$OUT"; OUT="$(realpath -e "$OUT")" +T="$(mktemp -d "${TMPDIR:-/tmp}/gen-pack.XXXXXXXX")"; trap 'rm -rf "$T"' EXIT +${CC:-cc} -std=gnu11 -Wall -Wextra -Werror -O2 -o "$T/gen" "$HERE/gen_expected.c" +awk '!/^#/ && NF && $0 != "gen-meta 1" { print $1 }' "$HERE/META" | LC_ALL=C sort > "$T/meta.ids" +(cd "$HERE/tasks/cases" && ls -1) | LC_ALL=C sort > "$T/case.ids" +cmp -s "$T/meta.ids" "$T/case.ids" || { echo "pack_dev: META task ids differ from tasks/cases/" >&2; exit 1; } +n=0 +for c in "$HERE"/tasks/cases/*/; do + id="$(basename "$c")" + "$T/gen" "$id" < "$c/hidden/inputs" > "$T/exp" + cmp -s "$T/exp" "$c/hidden/expected" || { echo "pack_dev: $id hidden/expected differs from gen_expected" >&2; exit 1; } + n=$((n + 1)) +done +(cd "$HERE" && tar --format=ustar --sort=name --owner=0 --group=0 --numeric-owner --mtime=@0 -cf "$OUT/tasks.tar.tmp" tasks) +mv "$OUT/tasks.tar.tmp" "$OUT/tasks.tar" +(cd "$OUT" && sha256sum tasks.tar > TASKS.sha256.tmp && mv TASKS.sha256.tmp TASKS.sha256) +echo "pack_dev: $n dev tasks -> $OUT/tasks.tar ($(cut -c1-16 "$OUT/TASKS.sha256")...)" diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/expected new file mode 100644 index 000000000..27a3c4ba2 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/expected @@ -0,0 +1,8 @@ +0 +1 +7 +24 +955 +995050 +16354933 +16548990 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/inputs new file mode 100644 index 000000000..409a9cc87 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/inputs @@ -0,0 +1,8 @@ +0 +1 +2 +3 +10 +100 +254 +255 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/reference.osc new file mode 100644 index 000000000..7b496e4ae --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen01/hidden/reference.osc @@ -0,0 +1,14 @@ +fn f(x: u32) -> u32 { + return 3 * x * x + 2 * x + 1; +} + +fn poly_sum(n: u8) -> u32 { + let m: u32 = n as u32; + let mut s: u32 = 0; + let mut i: u32 = 0; + while i < m bound 256 { + s = s + f(i); + i = i + 1; + } + return s; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen01/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen01/prompt new file mode 100644 index 000000000..b04e766fc --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen01/prompt @@ -0,0 +1,4 @@ +Write a function poly_sum that takes an unsigned 8-bit number n and returns an unsigned 32-bit number. +Define a helper function f that takes an unsigned 32-bit number x and returns 3*x*x + 2*x + 1, and use +it: poly_sum(n) is the sum of f(i) for every i from 0 to n - 1 (poly_sum(0) = 0). The result always +fits in 32 bits. It never traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen01/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen01/requirement new file mode 100644 index 000000000..efd6c416f --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen01/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry poly_sum +params u8 +ret u32 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/expected new file mode 100644 index 000000000..7e48d15ac --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/expected @@ -0,0 +1,10 @@ +0 +0 +-4 +-160 +5527040 +666600 +TRAP +TRAP +TRAP +-2147352576 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/inputs new file mode 100644 index 000000000..3ff3dca01 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/inputs @@ -0,0 +1,10 @@ +0 5 +1 0 +3 2 +10 -7 +255 0 +200 100 +255 3000 +255 -32768 +40 32767 +2 32767 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/reference.osc new file mode 100644 index 000000000..bdaf67a6d --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen02/hidden/reference.osc @@ -0,0 +1,15 @@ +fn g(x: i32, c: i32) -> i32 { + return (x - c) * (x + c) + x; +} + +fn shift_sq_sum(n: u8, c: i16) -> i32 { + let m: i32 = n as i32; + let cc: i32 = c as i32; + let mut s: i32 = 0; + let mut i: i32 = 0; + while i < m bound 256 { + s = s + g(i, cc); + i = i + 1; + } + return s; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen02/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen02/prompt new file mode 100644 index 000000000..9f1bbed47 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen02/prompt @@ -0,0 +1,6 @@ +Write a function shift_sq_sum that takes an unsigned 8-bit number n and a signed 16-bit number c and +returns a signed 32-bit number. Define a helper function g that takes signed 32-bit numbers x and c and +returns (x - c) * (x + c) + x, and use it: shift_sq_sum(n, c) is the sum of g(i, c) for every i from 0 to +n - 1 (shift_sq_sum(0, c) = 0). Use signed 32-bit arithmetic: when the sum does not fit in a signed +32-bit number, the function traps (overflow). For every input checked, either every partial result fits +in a signed 32-bit number or the final sum itself does not fit. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen02/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen02/requirement new file mode 100644 index 000000000..c2b17ffc6 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen02/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry shift_sq_sum +params u8 i16 +ret i32 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/expected new file mode 100644 index 000000000..fe5d50421 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/expected @@ -0,0 +1,8 @@ +0 +65535 +38 +29393 +39542 +29609 +18399 +5898 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/inputs new file mode 100644 index 000000000..03539718b --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/inputs @@ -0,0 +1,8 @@ +0 0 +65535 0 +1 1 +0 5 +12345 17 +65535 255 +777 100 +40000 3 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/reference.osc new file mode 100644 index 000000000..8ca20a67f --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen03/hidden/reference.osc @@ -0,0 +1,14 @@ +fn h(s: u32, i: u32) -> u32 { + return (s * 31 + i * i + 7) % 65536; +} + +fn mix_hash(seed: u16, n: u8) -> u16 { + let m: u32 = n as u32; + let mut s: u32 = seed as u32; + let mut i: u32 = 0; + while i < m bound 256 { + s = h(s, i); + i = i + 1; + } + return s as u16; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen03/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen03/prompt new file mode 100644 index 000000000..d6a4fee85 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen03/prompt @@ -0,0 +1,4 @@ +Write a function mix_hash that takes an unsigned 16-bit number seed and an unsigned 8-bit number n and +returns an unsigned 16-bit number. Define a helper function h that takes unsigned 32-bit numbers s and i +and returns (s * 31 + i * i + 7) modulo 65536. Start with s = seed; then for each i from 0 to n - 1 in +order, replace s by h(s, i). Return the final s (mix_hash(seed, 0) = seed). It never traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen03/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen03/requirement new file mode 100644 index 000000000..da5c21489 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen03/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry mix_hash +params u16 u8 +ret u16 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/expected new file mode 100644 index 000000000..61e06f660 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/expected @@ -0,0 +1,8 @@ +0 +4 +3 +26 +55 +1314 +2147385345 +281462091874310 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/inputs new file mode 100644 index 000000000..a133cde25 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/inputs @@ -0,0 +1,8 @@ +0 +1 +2 +3 +10 +11 +65534 +65535 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/reference.osc new file mode 100644 index 000000000..c6357ee9d --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen04/hidden/reference.osc @@ -0,0 +1,13 @@ +fn even_tri_odd_cube(n: u16) -> u64 { + let x: u64 = n as u64; + if x % 2 == 0 { + let mut s: u64 = 0; + let mut i: u64 = 1; + while i <= x bound 65536 { + s = s + i; + i = i + 1; + } + return s; + } + return x * x * x - 2 * x + 5; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen04/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen04/prompt new file mode 100644 index 000000000..fee0ab8ae --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen04/prompt @@ -0,0 +1,3 @@ +Write a function even_tri_odd_cube that takes an unsigned 16-bit number n and returns an unsigned 64-bit +number. If n is even, return the sum 1 + 2 + ... + n, adding the numbers one at a time (0 when n is 0). +If n is odd, return n*n*n - 2*n + 5. It never traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen04/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen04/requirement new file mode 100644 index 000000000..28944a9a9 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen04/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry even_tri_odd_cube +params u16 +ret u64 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/expected new file mode 100644 index 000000000..78c75b1d9 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/expected @@ -0,0 +1,14 @@ +0 +1 +47 +32767 +36028797018963967 +9223372036854775807 +TRAP +4611686018427387903 +9223372036854775807 +9223372036854775807 +-4 +3221061630 +29498 +TRAP diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/inputs new file mode 100644 index 000000000..0bb335d60 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/inputs @@ -0,0 +1,14 @@ +0 0 +0 1 +5 3 +32767 0 +32767 40 +32767 48 +32767 49 +0 62 +0 63 +1 62 +-1 7 +-32768 0 +-100 255 +100 200 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/reference.osc new file mode 100644 index 000000000..c66219f03 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen05/hidden/reference.osc @@ -0,0 +1,14 @@ +fn grow_or_poly(x: i16, k: u8) -> i64 { + let v: i64 = x as i64; + if v >= 0 { + let m: u32 = k as u32; + let mut t: i64 = v; + let mut i: u32 = 0; + while i < m bound 256 { + t = 2 * t + 1; + i = i + 1; + } + return t; + } + return 3 * v * v + 5 * v - 2; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen05/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen05/prompt new file mode 100644 index 000000000..46781cd3d --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen05/prompt @@ -0,0 +1,4 @@ +Write a function grow_or_poly that takes a signed 16-bit number x and an unsigned 8-bit number k and +returns a signed 64-bit number. If x is at least 0: start with t = x and repeat k times: t becomes +2*t + 1; return t. Use signed 64-bit arithmetic; when a value does not fit in a signed 64-bit number the +function traps (overflow). If x is negative: return 3*x*x + 5*x - 2 (this always fits). diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen05/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen05/requirement new file mode 100644 index 000000000..15ac5d733 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen05/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry grow_or_poly +params i16 u8 +ret i64 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/expected new file mode 100644 index 000000000..f1bdbc053 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/expected @@ -0,0 +1,10 @@ +1 +0 +1 +1024 +1350851717672992089 +12157665459056928801 +310 +390405 +120227 +1 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/inputs new file mode 100644 index 000000000..9c2d5528b --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/inputs @@ -0,0 +1,10 @@ +0 0 +0 5 +1 255 +2 10 +9 19 +9 20 +10 0 +255 255 +200 3 +3 0 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/reference.osc new file mode 100644 index 000000000..88feb02be --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen06/hidden/reference.osc @@ -0,0 +1,14 @@ +fn pow_small_or_mix(b: u8, e: u8) -> u64 { + let x: u64 = b as u64; + let y: u64 = e as u64; + if x < 10 { + let mut r: u64 = 1; + let mut i: u64 = 0; + while i < y bound 256 { + r = r * x; + i = i + 1; + } + return r; + } + return (x * x + y * y) * 3 + x; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen06/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen06/prompt new file mode 100644 index 000000000..a76c2fa3d --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen06/prompt @@ -0,0 +1,4 @@ +Write a function pow_small_or_mix that takes unsigned 8-bit numbers b and e and returns an unsigned +64-bit number. If b is less than 10, return b to the power e, computed by multiplying e times (b to the +power 0 is 1, also for b = 0); for every input checked this value fits in 64 bits. Otherwise return +(b*b + e*e) * 3 + b. It never traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen06/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen06/requirement new file mode 100644 index 000000000..d4ccec59e --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen06/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry pow_small_or_mix +params u8 u8 +ret u64 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/expected new file mode 100644 index 000000000..bc7d2d60d --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/expected @@ -0,0 +1,8 @@ +0 +1 +-1 +4294967295 +2147483653 +20 +1 +150 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/inputs new file mode 100644 index 000000000..776d4dbd9 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/inputs @@ -0,0 +1,8 @@ +0 0 0 0 +1 2 3 4 +4 3 2 1 +-2147483648 2147483647 0 0 +2147483647 -2147483648 5 -5 +10 -10 10 -10 +-5 -5 -5 -4 +100 50 200 199 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/reference.osc new file mode 100644 index 000000000..bbd3ab974 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen07/hidden/reference.osc @@ -0,0 +1,13 @@ +fn max_rise4(a: i32, b: i32, c: i32, d: i32) -> i64 { + let v: own [i64; 4] = alloc(0); + v[0] = a as i64; + v[1] = b as i64; + v[2] = c as i64; + v[3] = d as i64; + let mut best: i64 = v[1] - v[0]; + for i in 1 .. 3 { + let r: i64 = v[i + 1] - v[i]; + if r > best { best = r; } + } + return best; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen07/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen07/prompt new file mode 100644 index 000000000..016f1a96e --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen07/prompt @@ -0,0 +1,4 @@ +Write a function max_rise4 that takes four signed 32-bit numbers a, b, c, d and returns a signed 64-bit +number. Put the four values into an array v in this order (v[0] = a, v[1] = b, v[2] = c, v[3] = d). Return +the largest of v[i + 1] - v[i] for i = 0, 1, 2, computed exactly as a signed 64-bit number (it can be +negative). It never traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen07/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen07/requirement new file mode 100644 index 000000000..233ad461b --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen07/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry max_rise4 +params i32 i32 i32 i32 +ret i64 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/expected new file mode 100644 index 000000000..2ebe9233f --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/expected @@ -0,0 +1,8 @@ +0 +4 +0 +2 +2 +2 +2 +1 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/inputs new file mode 100644 index 000000000..d5d1e20c8 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/inputs @@ -0,0 +1,8 @@ +0 0 0 0 0 +1 2 3 4 5 +5 4 3 2 1 +-32768 32767 -32768 32767 -32768 +1 1 2 2 3 +-1 -2 -1 -2 -1 +0 1 0 1 0 +7 7 7 7 8 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/reference.osc new file mode 100644 index 000000000..56adfd6db --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen08/hidden/reference.osc @@ -0,0 +1,13 @@ +fn count_rising5(a: i16, b: i16, c: i16, d: i16, e: i16) -> u8 { + let v: own [i16; 5] = alloc(0); + v[0] = a; + v[1] = b; + v[2] = c; + v[3] = d; + v[4] = e; + let mut k: u8 = 0; + for i in 0 .. 4 { + if v[i + 1] > v[i] { k = k + 1; } + } + return k; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen08/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen08/prompt new file mode 100644 index 000000000..2556106c4 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen08/prompt @@ -0,0 +1,3 @@ +Write a function count_rising5 that takes five signed 16-bit numbers a, b, c, d, e and returns an +unsigned 8-bit number. Put the five values into an array v in this order. Return how many i from 0 to 3 +have v[i + 1] strictly greater than v[i]. It never traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen08/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen08/requirement new file mode 100644 index 000000000..bd8595041 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen08/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry count_rising5 +params i16 i16 i16 i16 i16 +ret u8 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/expected new file mode 100644 index 000000000..32210ee0f --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/expected @@ -0,0 +1,10 @@ +0 +0 +1 +1 +15 +6 +32385 +16129 +127 +381 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/inputs new file mode 100644 index 000000000..398ae024f --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/inputs @@ -0,0 +1,10 @@ +0 1 +1 1 +2 1 +2 3 +10 3 +10 7 +255 1 +255 2 +255 255 +100 13 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/reference.osc new file mode 100644 index 000000000..71795961c --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen09/hidden/reference.osc @@ -0,0 +1,15 @@ +fn pair_sum_div(n: u8, k: u8) -> u32 { + let m: u32 = n as u32; + let q: u32 = k as u32; + let mut c: u32 = 0; + let mut i: u32 = 1; + while i <= m bound 256 { + let mut j: u32 = i + 1; + while j <= m bound 256 { + if (i + j) % q == 0 { c = c + 1; } + j = j + 1; + } + i = i + 1; + } + return c; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen09/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen09/prompt new file mode 100644 index 000000000..4d1e170c7 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen09/prompt @@ -0,0 +1,3 @@ +Write a function pair_sum_div that takes unsigned 8-bit numbers n and k and returns an unsigned 32-bit +number: how many pairs (i, j) with 1 <= i < j <= n have i + j divisible by k (remainder 0). For every +input checked k is at least 1. It never traps for the inputs checked. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen09/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen09/requirement new file mode 100644 index 000000000..c17721072 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen09/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry pair_sum_div +params u8 u8 +ret u32 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/expected b/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/expected new file mode 100644 index 000000000..33e6df77e --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/expected @@ -0,0 +1,10 @@ +0 +0 +1 +6 +27 +100 +65025 +1 +4933 +40000 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/inputs b/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/inputs new file mode 100644 index 000000000..123d630fc --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/inputs @@ -0,0 +1,10 @@ +0 0 +1 0 +1 1 +3 4 +10 10 +10 100 +255 65535 +255 1 +255 1000 +200 40000 diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/reference.osc b/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/reference.osc new file mode 100644 index 000000000..4c698f334 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen10/hidden/reference.osc @@ -0,0 +1,15 @@ +fn grid_le(n: u8, m: u16) -> u32 { + let nn: u32 = n as u32; + let mm: u32 = m as u32; + let mut c: u32 = 0; + let mut i: u32 = 1; + while i <= nn bound 256 { + let mut j: u32 = 1; + while j <= nn bound 256 { + if i * j <= mm { c = c + 1; } + j = j + 1; + } + i = i + 1; + } + return c; +} diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen10/prompt b/tests/one_layer/generalization/dev/tasks/cases/devgen10/prompt new file mode 100644 index 000000000..bd7479704 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen10/prompt @@ -0,0 +1,3 @@ +Write a function grid_le that takes an unsigned 8-bit number n and an unsigned 16-bit number m and returns +an unsigned 32-bit number: how many pairs (i, j) with 1 <= i <= n and 1 <= j <= n have i * j <= m. It never +traps. diff --git a/tests/one_layer/generalization/dev/tasks/cases/devgen10/requirement b/tests/one_layer/generalization/dev/tasks/cases/devgen10/requirement new file mode 100644 index 000000000..2776f3550 --- /dev/null +++ b/tests/one_layer/generalization/dev/tasks/cases/devgen10/requirement @@ -0,0 +1,5 @@ +# public GEN dev task (omega#476), not held out +kind task +entry grid_le +params u8 u16 +ret u32 diff --git a/tests/one_layer/generalization/families/FAMSIG_V1.md b/tests/one_layer/generalization/families/FAMSIG_V1.md new file mode 100644 index 000000000..d8777b83e --- /dev/null +++ b/tests/one_layer/generalization/families/FAMSIG_V1.md @@ -0,0 +1,194 @@ +# GEN family signature and split contract (v1, frozen 2026-10-15) + +Issue: aien-spark/omega#476 (GEN-1), program aien-spark/omega#475. This contract is frozen BEFORE the +custodian authors any held-out task. Its only implementation is `tools/gen/protocol/famsig_lib.h` +(tools `famsig` and `gencheck`). A change to the signature or token definition needs a new version (`FAMSIG_V2.md`, signature prefix +`gs2:`). Additive amendments made before any held-out task exists (the token, the coarse form, rules R6 and R7, `famsig compare`) are dated in place. After the custodian seals, nothing changes. `docs/one_layer/GEN_PROTOCOL_V1.md` pins this file by sha256. + +## 1. What a family is + +A **family** is a composition template: which primitives a task's SPECIFICATION combines, and how they +nest. It is declared by the task author from the task's prompt (what the task asks for), never computed +from a reference solution. Identifiers, constants, types, edge cases and trap rules do not enter the +signature: renaming a function or changing a constant never makes a new family. + +## 2. Primitives and the nesting shape + +Six primitives. Declare each from the prompt: + +| primitive | kind | declare it when the specification asks for | +|---|---|---| +| `arith` | leaf | one arithmetic, bitwise, shift, comparison or conversion step stated as a single operation ("add x to the total", "is n even") | +| `nested` | leaf | one formula combining at least three operands with at least two operations, computed as a single value ("3*x*x + 2*x + 1"); declared instead of `arith` for such a formula | +| `array` | leaf | a fixed-length sequence (length 1..64) of values written or read by index (inputs packed into an array, a table, a buffer); one `array` per distinct use | +| `branch(X)` | container | a choice between behaviours on a condition; X is what the chosen arm(s) do | +| `loop(X)` | container | a step repeated a bounded number of times (count from a parameter or constant, or until a condition with a stated bound); X is the repeated body | +| `call(X)` | container | a helper computation the prompt names (its own parameters and result) that the solution must define and use; X is the helper's own body. The functional check cannot see helper use; the family describes the specification | + +`seq(A,B,...)` writes steps one after another. It is grouping only: not a primitive, never counted. + +Grammar (ASCII, no spaces, 1..512 bytes, at most 8 levels of nesting, at most 64 nodes): + +``` +node := leaf | container "(" list ")" | "seq(" node "," list ")" +list := node ("," node)* +leaf := "arith" | "array" | "nested" +container := "branch" | "call" | "loop" +``` + +A container needs at least one child, a leaf takes none, a `seq` needs at least two children. + +## 3. Signature + +Canonical form: (1) a `seq` directly inside a container or inside another `seq` is replaced by its +children (`loop(seq(a,b))` = `loop(a,b)`, `seq(a,seq(b,c))` = `seq(a,b,c)`); (2) every child list is +sorted by byte order (C locale `strcmp`) of the children's canonical strings, duplicates kept. Sibling +order therefore never matters; nesting does (`loop(branch(arith))` and `branch(loop(arith))` differ). + +**Signature** = `gs1:` + canonical form, e.g. `seq(loop(branch(arith),array),array)` has signature +`gs1:seq(array,loop(array,branch(arith)))`. `famsig canon ` prints it. + +**Family token** (for fields that allow only `[a-z0-9_-]`, such as the record `family` field of +`tools/gen/retrieval/CONTRACT.md` and its `--exclude-family`): `gs1-` + the first 16 lowercase hex digits of +sha256 of the signature bytes (`gs1:` included, no newline). `famsig token ` prints it. A record or +task carries the token of the family of the task it was written for. (Added 2026-10-15, before any +consumer used v1: additive, no rule changed.) + +**Composition edges** of a shape: `P>C` for every container P and every child C of P after flattening, +where C is the child's head primitive (`famsig edges `). **Coarse edges**: the same with a child +`nested` read as `arith`, duplicates removed. Coarse edges gate through R7 (section 4). + +**Coarse form** (rule R6; added 2026-10-15 at review of PR #486, before any held-out task existed): (1) +`nested` is read as `arith`; (2) `seq` is flattened as in the canonical form; (3) every child list is +sorted and duplicate siblings are collapsed to one, and a `seq` left with one child is that child; (4) a +top-level `seq` whose children are exactly one container plus only leaves is replaced by that container. +**Coarse signature** = `gc1:` + coarse form; `famsig coarse ` prints it. Examples: +`loop(branch(nested))`, `loop(branch(arith,arith))` and `seq(arith,loop(branch(arith)))` all have +`gc1:loop(branch(arith))`; `loop(branch(arith),arith)` has `gc1:loop(arith,branch(arith))` (a leaf added +inside a container is a different coarse family; R7 decides whether it adds an edge the corpus lacks). + +## 4. Metadata lines and the disjointness rule + +One line per task, fields separated by exactly one space: + +``` + +``` + +- `task_id`: `[a-z0-9_-]{1,40}`; `split`: `corpus` | `dev` | `heldout`; the six counts: decimal 0..64, + no leading zeros, in exactly this order; `shape`: section 2. +- **R1** every line parses; each declared count equals that primitive's number of occurrences in the + shape (the multiset is declared and cross-checked, so a typo in either is refused). +- **R2** task ids are unique across every split. +- **R3** exemplar (corpus), dev and held-out families are pairwise disjoint: one signature never + appears in two splits. +- **R4** every primitive used by a held-out task is used by at least one corpus task. +- **R5** every held-out shape uses at least two distinct primitives (a held-out family is a combination). +- **R6** coarse signatures (section 3) are pairwise disjoint across corpus, dev and held-out: two tasks + of different splits with different signatures never share a coarse signature (equal signatures are R3). + This refuses a held-out family that is a corpus or dev family with a duplicated sibling, a `nested` + for an `arith`, or a top-level `seq` wrapper around it plus leaves. +- **R7** every held-out shape has at least one coarse composition edge (section 3) that no corpus shape + has (`novel_edges >= 1`). Wrapping a corpus shape in `seq(...)` or relabelling a leaf adds no edge, so + it fails R7. + (R6 and R7 added 2026-10-15 at review of PR #486, before any held-out task existed.) +- **R8** no top-level step of a dev or held-out shape is a corpus family in coarse form: after the + top-level `seq` is flattened, every step that is a container (`branch`, `loop`, `call`) has a coarse + form (section 3) that differs from the coarse signature of every corpus task. Leaf steps are not + compared (R4 requires leaves to be reused); a shape whose whole coarse signature equals a corpus one is + R3/R6. `seq(loop(branch(arith)),loop(array))` next to a corpus `loop(branch(arith))` fails R8. + (R8 added 2026-10-16 at review of PR #486, before any held-out task existed; dev/META passes it against + the GEN-3 corpus META.) + +Known limit: R6, R7 and R8 are syntactic. A corpus family nested below the top level and given a leaf +sibling, such as `loop(branch(arith),arith)` next to a corpus `loop(branch(arith))`, passes R6 (different +coarse form) and R8 (not a top-level step), and passes R7 only if no corpus shape has the edge +`loop>arith`. What the shape cannot show (the same algorithm declared under a different shape) is the job +of the reviewer's blind re-declaration and the near-duplicate check (GEN_PROTOCOL_V1 section 2). + +`famsig rule ...` applies R1..R8 over every task of every file given, prints per split +`split tasks= families=`, then one `family tasks=` line per family +(held-out families only with `--show-heldout`, adding `novel_edges=`: coarse edges no corpus shape +has), and ends `RULE PASS` (exit 0) or `FAIL R ...` lines and `RULE FAIL` (exit 1). A failure that +involves a held-out task hides the held-out signature unless `--show-heldout` is given. Counts (60 +held-out tasks, at least 6 held-out families, 10 dev tasks) are checked by `gencheck` against +`protocol.json`, not by `famsig`. + +`famsig compare ` (blind re-declaration, GEN_PROTOCOL_V1 section 2): both files are in +the `gen-split 1` format of section 6 (any salt; the reviewer draws its own); it prints only `tasks= mismatches=`, where a mismatch +is a task whose signature differs between the two files, and exits 1 when k > 0 or the two files do not +name exactly the same task ids. It never prints a signature. + +## 5. Where metadata lives + +| split | file | visibility | +|---|---|---| +| dev | `tests/one_layer/generalization/dev/META` | public, in the repo | +| corpus | `tests/one_layer/generalization/corpus/META` (Agent D, GEN-3) | public, in the repo | +| heldout | `SPLIT` in the custodian's sealed directory (owner `gencust`, mode 0700) | sealed | + +Public files (`gen-meta 1`): first line exactly `gen-meta 1`; then metadata lines; `#` comment lines +and blank lines are allowed; a `heldout` line is refused. Bytes: `\n` and 0x20..0x7E only, final `\n`. + +Held-out metadata NEVER goes inside `tasks.tar` (whose member rules, HARNESS_INTERFACE_V2.md section 3, +refuse any extra file) and is never published before the run. + +## 6. The sealed split file (`SPLIT`, written by the custodian) + +``` +gen-split 1 +salt <64 lowercase hex: 32 random bytes the custodian draws once> + (one per held-out task) +``` + +Rules: line 1 exactly `gen-split 1`; line 2 the salt; then only `heldout` lines, task ids strictly +ascending in byte order; no comments, no blank lines; `\n` and 0x20..0x7E only; final `\n`. The task ids +are exactly the case directories of the sealed `tasks.tar`. + +- `split_sha256` = sha256 of the SPLIT file's bytes (commits every task's family membership). +- **FAMLIST** = the bytes `gen-famlist 1\n`, `salt \n`, then every distinct held-out signature + sorted by byte order, one per line with `\n`. `famlist_sha256` = sha256 of FAMLIST. The salt makes + both digests unguessable by enumerating the (small) space of shapes. +- `famsig split SPLIT` checks the format and prints `split_sha256=`, `famlist_sha256=`, `tasks=`, + `families=` (nothing else); `famsig famlist SPLIT` prints FAMLIST. + +Before sealing, the custodian runs (as `gencust`) `famsig rule dev/META corpus/META SPLIT` (R1..R8) and seals +only on `RULE PASS`. It must use the corpus META at the commit the run pins. + +## 7. The public commitment (`HELDOUT.pub`, published by the custodian before any held-out generation) + +``` +gen-heldout-pub 1 +tasks_sha256 <64 hex: sha256 of tasks.tar, as in TASKS.sha256> +split_sha256 <64 hex> +famlist_sha256 <64 hex> +tasks +families +task (one per task, strictly ascending, n lines) +``` + +Exactly these lines in this order, single spaces, final `\n`. Nothing about any family is published +except `families ` and the two salted digests. After the run (or for the independent review, after +the seal), anyone holding SPLIT verifies with `gencheck split` that SPLIT matches HELDOUT.pub +(digests, ids, counts) and that R1..R8 pass against the pinned dev and corpus META. + +## 8. Reserved dev families (v1): signature and token + +The 10 dev fixtures `devgen01..devgen10` hold these four signatures; no corpus or held-out task may use +them (R3): + +``` +gs1:branch(loop(arith),nested) gs1-e05d1c5dffd08b06 +gs1:loop(call(nested)) gs1-bd9c105e5ae45397 +gs1:loop(loop(branch(arith))) gs1-f63e09ec03a454ee +gs1:seq(array,loop(array,branch(arith))) gs1-30938c5c7504ef0e +``` + +Their coarse signatures (R6: no corpus or held-out task may have these either): + +``` +gc1:branch(arith,loop(arith)) +gc1:loop(call(arith)) +gc1:loop(loop(branch(arith))) +gc1:loop(array,branch(arith)) +``` diff --git a/tests/one_layer/generalization/protocol.json b/tests/one_layer/generalization/protocol.json new file mode 100644 index 000000000..82517dd69 --- /dev/null +++ b/tests/one_layer/generalization/protocol.json @@ -0,0 +1,81 @@ +{ + "schema": "omega.gen.protocol.v1", + "revision": 1, + "issue": "aien-spark/omega#476", + "doc": "docs/one_layer/GEN_PROTOCOL_V1.md", + "question": "Does retrieving checker-verified abstractions (D) beat 1-shot (A), many-shot (B) and raw retrieval (C) on attempt-1 functional success for sealed held-out composition families?", + "config": {"name": "A", "params": "tools/exp445/PARAMS", "masking": false, "harness_kind": "oscir", "writer": "omegese", "reader": "tools/exp445/reader.sh"}, + "conditions": [ + {"name": "A", "prefix_examples": 1, "retrieval": "none"}, + {"name": "B", "prefix_examples": 14, "retrieval": "none"}, + {"name": "C", "prefix_examples": 14, "retrieval": "raw"}, + {"name": "D", "prefix_examples": 14, "retrieval": "abstraction"} + ], + "retrieval": {"contract": "genret-cli-v3", "record_schema": "gen-record-1", "k": 3, "cap_bytes": 4096, "mode": "normal", "query": "requirement+prompt", "c_pool_excludes_prefix": true, "length_match_abs_bytes": 256, "length_match_frac": 0.15, "length_match_min_matched_frac": 0.5}, + "generation": { + "primary": "attempt1_round1_functional", + "max_tokens": 3072, "max_rounds": 2, "attempts": 3, + "attempt1_temperature": 0, "sampled_temperature": 0.7, "sampled_top_p": 0.8, + "seed_base": 476000, "seed_attempt_stride": 1000, + "retries": 2, "retry_pause_s": 5, + "order": "attempt,task,condition", "in_flight": 1, + "sets": ["-", "--final"] + }, + "gates": { + "greedy": {"tasks": "dev", "condition": "B", "repeats": 2}, + "g0": {"script": "tools/exp445/sampling-gate.sh", "task": "devgen01", "condition": "B", "seeds": 8, "repeat": 1, "otherwise": "UNAVAILABLE", "seed_values": [476901, 476902, 476903, 476904, 476905, 476906, 476907, 476908]} + }, + "decision": { + "schema": "omega.gen.decision.v1", + "primary": "attempt1_round1_functional", + "controls": ["A", "B", "C"], + "delta": 0.10, "alpha": 0.05, + "test": "mcnemar_exact_one_sided", "ci": "newcombe10", + "z95": 1.959963984540054, "z90": 1.644853626951472, + "fail_control": "C", "sanity_ab_max": 2, + "verdicts": ["INVALID", "PASS", "FAIL", "INCONCLUSIVE"], + "vectors": "tests/one_layer/generalization/stats_vectors.tsv", + "floor_max": 3, "ceiling_min": 57 + }, + "splits": { + "dev": { + "count": 10, + "meta": "tests/one_layer/generalization/dev/META", + "meta_sha256": "1de87149b3c2e0ab80b9ba225bdd1e8b928e7a2c921d18df7dcb804daec6f0b2", + "cases": "tests/one_layer/generalization/dev/tasks/cases", + "pack_sha256": "5f06a64a8579fe806119a9831f55290863396dab72ab955a972463004464b718", + "tasks": ["devgen01", "devgen02", "devgen03", "devgen04", "devgen05", "devgen06", "devgen07", "devgen08", "devgen09", "devgen10"], + "families": ["gs1:branch(loop(arith),nested)", "gs1:loop(call(nested))", "gs1:loop(loop(branch(arith)))", "gs1:seq(array,loop(array,branch(arith)))"] + }, + "corpus": {"count_target": 48, "meta": "tests/one_layer/generalization/corpus/META", "meta_sha256": "179c34c50f6d843d41b10aabdbd69c425248a3107e107c64630836199e15ab0b"}, + "heldout": {"count": 60, "min_families": 6, "pub_sha256": null, "tasks_sha256": null, "near_dup_threshold": 0.5} + }, + "record_sets": {"prefix_ids": ["wrap8", "big_pc_small_ds", "gcd_only", "sum_clamped", "even_digsums", "ceil_avg", "sum4_avg", "max4_half", "lcm32", "clamp_sum", "ds2_double", "digcount_nibbles", "digits_both", "avg_double"], "prefix_sha256": "b1a3d7beaae4b1c4220b4bb8f221f31e1ffb87c219119b10449ea2d792c132ef", "raw_manifest_sha256": "d6b8a9a4912640ebda8617110ed9ec2b1960b696535267672c4b4691a5533be0", "abstraction_manifest_sha256": "1fe9af29da784e6af6bf1589e12a68b60be2de12d24cc0904761a34ac6a3cb78", "raw_manifest": "tests/one_layer/generalization/records/C.MANIFEST", "abstraction_manifest": "tests/one_layer/generalization/records/D.MANIFEST"}, + "pins": [ + {"path": "docs/one_layer/GEN_PROTOCOL_V1.md", "sha256": "48de22945215b2135a9e90f8430d6636f92a89ca3d16fb44ec4daced2f2fcf73"}, + {"path": "docs/one_layer/OMEGESE.md", "sha256": "9d8795bc55e417b3a0de8ba911359e69e7d4bb5fa2eaaf9508253fe1fa68e28a"}, + {"path": "tests/one_layer/acceptance/HARNESS_INTERFACE_V2.md", "sha256": "a7e38a6faa7cdf7d479891209055f4fe789a55f5d377e4b6e36840aefe7f04c8"}, + {"path": "tests/one_layer/acceptance/ola.sh", "sha256": "f161e2c18eb7b7609fad12e49d3b659470b7892ca75b0aa8ebc555961d9eb667"}, + {"path": "tests/one_layer/generalization/dev/META", "sha256": "1de87149b3c2e0ab80b9ba225bdd1e8b928e7a2c921d18df7dcb804daec6f0b2"}, + {"path": "tests/one_layer/generalization/dev/gen_expected.c", "sha256": "166f7945a95a98789764cd439f7d8ced126a542a9a251e860df29bf661232506"}, + {"path": "tests/one_layer/generalization/dev/pack_dev.sh", "sha256": "40dd2dcceb8a8570c40022e059f396d3064238134081cabafd993f40222f2390"}, + {"path": "tests/one_layer/generalization/families/FAMSIG_V1.md", "sha256": "5ebbc8efe6e125863b17fb8b73a3b56a191d18d8a9be730dd2b4ea7133714092"}, + {"path": "tests/one_layer/generalization/stats_vectors.tsv", "sha256": "522c534402bf1eadc0c08b8f926c0cffa504b263935b49903064a1f2404a80b5"}, + {"path": "tools/exp445/PARAMS", "sha256": "a37136fb7fa81e9e0730c847b888d890ac8fd0cd79fdf41ee63cc5e762960aa1"}, + {"path": "tools/exp445/arms/common.md", "sha256": "990ffb25bc4bf9da4dacc7f80c39d7b24803aa8dd78eab2d92e2dee88cd0790f"}, + {"path": "tools/exp445/arms/omegese.md", "sha256": "5d1468799559f8042652437e0b13945c4e25e32d783b5624c32821efb47a8cfe"}, + {"path": "tools/exp445/reader.sh", "sha256": "dfeae44eeec0d6f41806f373d85ac3aafe09e195759d9e4174c8eb4c29b94fa9"}, + {"path": "tools/exp445/sampling-gate.sh", "sha256": "704ff70480f280f752e39c50f12dbda057c577150739ef2a22074ef3d19a6acf"}, + {"path": "tools/exp445/weights.A.sha256", "sha256": "a00f5c31ce1f1c1f400a22cbc6ba58ce217d62991a6e7fa590e5803dcfb2ed8b"}, + {"path": "tools/gen/protocol/famsig.c", "sha256": "42d372ffab771b2796e885563de7ff6ec36244fefc6533a09988690562c05c75"}, + {"path": "tools/gen/protocol/famsig_lib.h", "sha256": "7913eb3f57523e4942ce1620f26554208cc6cb6783457292c3e267b556bfaaa4"}, + {"path": "tools/gen/protocol/gencheck.c", "sha256": "855eae3032985f5a74fe40f06c77d8e2048df8ada8acad2ca88c4d088f2057d0"}, + {"path": "tools/gen/protocol/genjac.c", "sha256": "c8c5790c4fc5cd4bab0f6a562f7470895ae9b3803124ad83dbb46252108e2ee9"}, + {"path": "tools/gen/protocol/genstats.c", "sha256": "204e83d837b5d0d39b4fbbaf4185d7f7fb8ab70a59a4c54516d3dae69720e0db"} + ], + "interfaces": [ + {"name": "gen-record-1", "path": "tools/gen/abstract/RECORD_SCHEMA.md", "commit": "937b033c78d087f1a0c64f28a29f4bab69f0a18a", "sha256": "9be17b6bc6f97c7420ced722bb258c420944bd87461097163df0992112ae6b53"}, + {"name": "gen-runner-formats-v1", "path": "tools/gen/runner/FORMATS.md", "commit": "9d81b78fb10cbc57d1178df0def8acb992927318", "sha256": "392ed623bec9d73771ee67434dc73a4bd74914868405bbe6a9d424aeef234293"}, + {"name": "genret-cli-v3", "path": "tools/gen/retrieval/CONTRACT.md", "commit": "4a48b61325865b1edc2d021d621a51f0f515963d", "sha256": "48006271d18394c40daf96120eaa20fbe3908d9a12b08cab7994ee02a6351e9d"} + ] +} diff --git a/tests/one_layer/generalization/stats_vectors.tsv b/tests/one_layer/generalization/stats_vectors.tsv new file mode 100644 index 000000000..9eea51d97 --- /dev/null +++ b/tests/one_layer/generalization/stats_vectors.tsv @@ -0,0 +1,126 @@ +# GEN decision statistics test vectors v2 (aien-spark/omega#476). Generated by tools/gen/protocol/genstats.c +# (`genstats vectors`); never edit by hand. Rules: docs/one_layer/GEN_PROTOCOL_V1.md sections 9 and 10. +# z95 = 1.959963984540054 (z 0.975), z90 = 1.644853626951472 (z 0.95). Reals printed %.10f; a reader must agree within 1e-9, +# and its 6-decimal receipt values must equal these rounded to 6 decimals. Counts, p_num, p_den, yes/no and verdicts exactly. +# One record per line, TAB-separated; field 1 names the record kind; each kind has a #cols line. +# Verdict order: INVALID (validity, A - B > 2) > INCONCLUSIVE floor (max succ <= 3) / ceiling (min succ >= n - 3) > PASS > FAIL +# (D-C newcombe95_hi < 0.10, compared on the full double) > INCONCLUSIVE. v2 = v1 + the floor/ceiling rule and the length-match records (review of PR #486). +#cols wilson id k n conf p lo hi +wilson w01_95 0 60 95 0.0000000000 0.0000000000 0.0601718521 +wilson w01_90 0 60 90 0.0000000000 0.0000000000 0.0431467986 +wilson w02_95 1 60 95 0.0166666667 0.0029481598 0.0885512973 +wilson w02_90 1 60 90 0.0166666667 0.0037270282 0.0713148771 +wilson w03_95 6 60 95 0.1000000000 0.0466428345 0.2014946472 +wilson w03_90 6 60 90 0.1000000000 0.0525974848 0.1819199541 +wilson w04_95 20 60 95 0.3333333333 0.2272926152 0.4594313355 +wilson w04_90 20 60 90 0.3333333333 0.2423415257 0.4387074072 +wilson w05_95 30 60 95 0.5000000000 0.3773502424 0.6226497576 +wilson w05_90 30 60 90 0.5000000000 0.3961409626 0.6038590374 +wilson w06_95 48 60 95 0.8000000000 0.6821819419 0.8817149468 +wilson w06_90 48 60 90 0.8000000000 0.7029665084 0.8711454124 +wilson w07_95 59 60 95 0.9833333333 0.9114487027 0.9970518402 +wilson w07_90 59 60 90 0.9833333333 0.9286851229 0.9962729718 +wilson w08_95 60 60 95 1.0000000000 0.9398281479 1.0000000000 +wilson w08_90 60 60 90 1.0000000000 0.9568532014 1.0000000000 +wilson w09_95 3 10 95 0.3000000000 0.1077912674 0.6032218525 +wilson w09_90 3 10 90 0.3000000000 0.1268765839 0.5583002041 +wilson w10_95 0 10 95 0.0000000000 0.0000000000 0.2775327999 +wilson w10_90 0 10 90 0.0000000000 0.0000000000 0.2129419701 +wilson w11_95 10 10 95 1.0000000000 0.7224672001 1.0000000000 +wilson w11_90 10 10 90 1.0000000000 0.7870580299 1.0000000000 +#cols mcnemar1 id b c p_num p_den p p_lt_0.05 +mcnemar1 m01 0 0 1 1 1.0000000000 no +mcnemar1 m02 1 0 1 2 0.5000000000 no +mcnemar1 m03 0 1 2 2 1.0000000000 no +mcnemar1 m04 4 0 1 16 0.0625000000 no +mcnemar1 m05 5 0 1 32 0.0312500000 yes +mcnemar1 m06 6 0 1 64 0.0156250000 yes +mcnemar1 m07 7 1 9 256 0.0351562500 yes +mcnemar1 m08 8 2 56 1024 0.0546875000 no +mcnemar1 m09 9 3 299 4096 0.0729980469 no +mcnemar1 m10 10 4 1471 16384 0.0897827148 no +mcnemar1 m11 12 6 31180 262144 0.1189422607 no +mcnemar1 m12 3 9 4017 4096 0.9807128906 no +mcnemar1 m13 15 15 614429672 1073741824 0.5722322240 no +mcnemar1 m14 30 30 635593043085854200 1152921504606846976 0.5512890865 no +mcnemar1 m15 60 0 1 1152921504606846976 0.0000000000 yes +mcnemar1 m16 0 60 1152921504606846976 1152921504606846976 1.0000000000 no +mcnemar1 m17 16 2 172 262144 0.0006561279 yes +mcnemar1 m18 45 15 77542088287444 1152921504606846976 0.0000672570 yes +#cols newcombe id a_both b_first_only c_second_only d_neither conf method theta lo hi source +newcombe n01 36 12 2 0 95 10 0.2000000000 0.0569295840 0.3404276030 Newcombe1998_TableIII_published_0.0569_0.3404 +newcombe n02 20 12 2 16 95 10 0.2000000000 0.0561564248 0.3292069954 Newcombe1998_TableIII_published_0.0562_0.3292 +newcombe n03 18 12 2 18 95 10 0.2000000000 0.0561718440 0.3289759689 Newcombe1998_TableIII_published_0.0562_0.3290 +newcombe n04 36 14 0 0 95 10 0.2800000000 0.1528395179 0.4166512370 Newcombe1998_TableIII_published_0.1528_0.4167 +newcombe n05 35 14 0 1 95 10 0.2800000000 0.1460739059 0.4174624742 Newcombe1998_TableIII_published_0.1461_0.4175 +newcombe n06 18 14 0 18 95 10 0.2800000000 0.1440826162 0.3963252817 Newcombe1998_TableIII_published_0.1441_0.3963 +newcombe n07 2 97 1 0 95 10 0.9600000000 0.8721164783 0.9853664502 Newcombe1998_TableIII_published_0.8721_0.9854 +newcombe n08 1 97 1 1 95 10 0.9600000000 0.8736721762 0.9850257704 Newcombe1998_TableIII_published_0.8736_0.9850 +newcombe n09 0 29 1 0 95 10 0.9333333333 0.6665921802 0.9881828192 Newcombe1998_TableIII_published_0.6666_0.9882 +newcombe n10 2 98 0 0 95 10 0.9800000000 0.9177931023 0.9944980324 Newcombe1998_TableIII_published_0.9178_0.9945 +newcombe n11 1 98 0 1 95 10 0.9800000000 0.9170870180 0.9916426092 Newcombe1998_TableIII_published_0.9171_0.9916 +newcombe n12 0 30 0 0 95 10 1.0000000000 0.8394678199 1.0000000000 Newcombe1998_TableIII_published_0.8395_1.0000 +newcombe n13 54 0 0 0 95 10 0.0000000000 -0.0664135881 0.0664135881 Newcombe1998_TableIII_published_-0.0664_0.0664 +newcombe n14 53 0 0 1 95 10 0.0000000000 -0.0729381455 0.0729381455 Newcombe1998_TableIII_published_-0.0729_0.0729 +newcombe n15 30 0 0 24 95 10 0.0000000000 -0.0358376219 0.0358376219 Newcombe1998_TableIII_published_-0.0358_0.0358 +newcombe n16 29 0 0 25 95 10 0.0000000000 -0.0354130341 0.0354130341 Newcombe1998_TableIII_published_-0.0354_0.0354 +newcombe n17 28 0 0 26 95 10 0.0000000000 -0.0351558202 0.0351558202 Newcombe1998_TableIII_published_-0.0352_0.0352 +newcombe n18 27 0 0 27 95 10 0.0000000000 -0.0350696630 0.0350696630 Newcombe1998_TableIII_published_-0.0351_0.0351 +newcombe n19 20 6 0 34 95 10 0.1000000000 0.0174955575 0.1802112622 gen +newcombe n20 20 6 0 34 90 10 0.1000000000 0.0305895419 0.1677836185 gen +newcombe n21 20 8 2 30 95 10 0.1000000000 -0.0048777026 0.2004231431 gen +newcombe n22 20 8 2 30 90 10 0.1000000000 0.0116069764 0.1852037736 gen +newcombe n23 20 3 3 34 95 10 0.0000000000 -0.0849539510 0.0849539510 gen +newcombe n24 20 3 3 34 90 10 0.0000000000 -0.0716599572 0.0716599572 gen +newcombe n25 0 0 0 60 95 10 0.0000000000 -0.0601718521 0.0601718521 gen +newcombe n26 0 0 0 60 90 10 0.0000000000 -0.0431467986 0.0431467986 gen +newcombe n27 60 0 0 0 95 10 0.0000000000 -0.0601718521 0.0601718521 gen +newcombe n28 60 0 0 0 90 10 0.0000000000 -0.0431467986 0.0431467986 gen +newcombe n29 25 12 6 17 95 10 0.1000000000 -0.0393694481 0.2328721375 gen +newcombe n30 25 12 6 17 90 10 0.1000000000 -0.0175277624 0.2128705961 gen +newcombe n31 20 4 1 35 95 10 0.0500000000 -0.0285030858 0.1276222316 gen +newcombe n32 20 4 1 35 90 10 0.0500000000 -0.0160637936 0.1154434851 gen +newcombe n33 3 4 0 3 95 10 0.4000000000 0.0141750506 0.6445698551 gen +newcombe n34 3 4 0 3 90 10 0.4000000000 0.0713341656 0.6202853544 gen +#cols lenmatch id target_bytes suffix_bytes length_gap tolerance_bytes class +lenmatch l01 295 945 650 256.00 unmatched +lenmatch l02 300 738 438 256.00 unmatched +lenmatch l03 300 556 256 256.00 matched +lenmatch l04 300 557 257 256.00 unmatched +lenmatch l05 300 44 -256 256.00 matched +lenmatch l06 300 43 -257 256.00 unmatched +lenmatch l07 2000 2300 300 300.00 matched +lenmatch l08 2000 2301 301 300.00 unmatched +lenmatch l09 2000 1700 -300 300.00 matched +lenmatch l10 2000 1699 -301 300.00 unmatched +lenmatch l11 1000 1000 0 256.00 matched +lenmatch l12 1706 1962 256 256.00 matched +#cols lenset id tasks matched unmatched status +lenset s01 10 10 0 AVAILABLE +lenset s02 10 5 5 AVAILABLE +lenset s03 10 4 6 UNAVAILABLE +lenset s04 10 0 10 UNAVAILABLE +lenset s05 9 5 4 AVAILABLE +lenset s06 9 4 5 UNAVAILABLE +lenset s07 1 1 0 AVAILABLE +#cols verdict id valid vec_A vec_B vec_C vec_D succ_A succ_B succ_C succ_D D-A.both D-A.d_only D-A.x_only D-A.neither D-A.p_num D-A.p_den D-A.p D-A.lift_ok D-A.p_ok D-B.both D-B.d_only D-B.x_only D-B.neither D-B.p_num D-B.p_den D-B.p D-B.lift_ok D-B.p_ok D-C.both D-C.d_only D-C.x_only D-C.neither D-C.p_num D-C.p_den D-C.p D-C.lift_ok D-C.p_ok D-C.newcombe95_lo D-C.newcombe95_hi D-C.newcombe90_lo D-C.newcombe90_hi a_minus_b verdict reasons +verdict v01_pass 1 111111111111111111000000000000000000000000000000000000000000 111111111111111111000000000000000000001100000000000000000000 111111111111111111000000000000000011111100000000000000000000 111111111111111111111111111111111111111100000000000000000000 18 20 24 40 18 22 0 20 1 4194304 0.0000002384 yes yes 20 20 0 20 1 1048576 0.0000009537 yes yes 24 16 0 20 1 65536 0.0000152588 yes yes 0.1457684188 0.3724443926 0.1653525411 0.3571402656 -2 PASS all_controls_lift_and_p +verdict v02_fail_equal 1 111111111111111111110000000000000000000000000000000000000000 111111111111111111110000000000000000000000000000000000000000 111111111111111111111110000000000000000000000000000000000000 111111111111111111110001110000000000000000000000000000000000 20 20 23 23 20 3 0 37 1 8 0.1250000000 no no 20 3 0 37 1 8 0.1250000000 no no 20 3 3 34 42 64 0.6562500000 no no -0.0849539510 0.0849539510 -0.0716599572 0.0716599572 0 FAIL newcombe95_hi_D-C_below_delta +verdict v03_inconclusive_small_lift 1 111111111111111111110000000000000000000000000000000000000000 111111111111111111110000000000000000000000000000000000000000 111111111111111111111111100001000000000000000000000000000000 111111111111111111111111111110000000000000000000000000000000 20 20 26 29 20 9 0 31 1 512 0.0019531250 yes yes 20 9 0 31 1 512 0.0019531250 yes yes 25 4 1 30 6 32 0.1875000000 no no -0.0276496770 0.1258794461 -0.0155973718 0.1143288105 0 INCONCLUSIVE lift_C,p_C +verdict v04_inconclusive_c_p 1 111111111111111000000000000000000000000000000000000000000000 111111111111111000000000000000000000000000000000000000000000 111111111111111111111111100000000110000000000000000000000000 111111111111111111111111111111111000000000000000000000000000 15 15 27 33 15 18 0 27 1 262144 0.0000038147 yes yes 15 18 0 27 1 262144 0.0000038147 yes yes 25 8 2 25 56 1024 0.0546875000 yes no -0.0046751379 0.1996395889 0.0116958321 0.1846933797 0 INCONCLUSIVE p_C +verdict v05_invalid_validity 0 111111111111111111000000000000000000000000000000000000000000 111111111111111111000000000000000000001100000000000000000000 111111111111111111000000000000000011111100000000000000000000 111111111111111111111111111111111111111100000000000000000000 18 20 24 40 18 22 0 20 1 4194304 0.0000002384 yes yes 20 20 0 20 1 1048576 0.0000009537 yes yes 24 16 0 20 1 65536 0.0000152588 yes yes 0.1457684188 0.3724443926 0.1653525411 0.3571402656 -2 INVALID validity_failure +verdict v06_invalid_sanity 1 111111111111111111000000000000000000000000000000000000000000 111111111111111000000000000000000000000000000000000000000000 111111111111111000100000000000000000000000000000000000000000 111111111111111111111111111111111111111000000000000000000000 18 15 16 39 18 21 0 21 1 2097152 0.0000004768 yes yes 15 24 0 21 1 16777216 0.0000000596 yes yes 16 23 0 21 1 8388608 0.0000001192 yes yes 0.2468341706 0.4951356706 0.2695309218 0.4794308124 3 INVALID sanity_a_minus_b_gt_2 +verdict v07_sanity_boundary_pass 1 111111111111111110000000000000000000000000000000000000000000 111111111111111000000000000000000000000000000000000000000000 111111111111111110000000000000000000000000000000000000000000 111111111111111111111111111111111111100000000000000000000000 17 15 17 37 17 20 0 23 1 1048576 0.0000009537 yes yes 15 22 0 23 1 4194304 0.0000002384 yes yes 17 20 0 23 1 1048576 0.0000009537 yes yes 0.2027083005 0.4437233515 0.2241828840 0.4279792146 2 PASS all_controls_lift_and_p +verdict v08_boundary_lift6_p 1 111111111111111111110000000000000000000000000000000000000000 111111111111111111110000000000000000000000000000000000000000 111111111111111111110000000000000000000000000000000000000000 111111111111111111111111110000000000000000000000000000000000 20 20 20 26 20 6 0 34 1 64 0.0156250000 yes yes 20 6 0 34 1 64 0.0156250000 yes yes 20 6 0 34 1 64 0.0156250000 yes yes 0.0174955575 0.1802112622 0.0305895419 0.1677836185 0 PASS all_controls_lift_and_p +verdict v09_boundary_lift5 1 111111111111111111110000000000000000000000000000000000000000 111111111111111111110000000000000000000000000000000000000000 111111111111111111110000000000000000000000000000000000000000 111111111111111111111111100000000000000000000000000000000000 20 20 20 25 20 5 0 35 1 32 0.0312500000 no yes 20 5 0 35 1 32 0.0312500000 no yes 20 5 0 35 1 32 0.0312500000 no yes 0.0063259412 0.1590015628 0.0185798369 0.1471465040 0 INCONCLUSIVE lift_A,lift_B,lift_C +verdict v10_boundary_8_2 1 111111111111111111110000000011000000000000000000000000000000 111111111111111111110000000011000000000000000000000000000000 111111111111111111110000000011000000000000000000000000000000 111111111111111111111111111100000000000000000000000000000000 22 22 22 28 20 8 2 30 56 1024 0.0546875000 yes no 20 8 2 30 56 1024 0.0546875000 yes no 20 8 2 30 56 1024 0.0546875000 yes no -0.0048777026 0.2004231431 0.0116069764 0.1852037736 0 INCONCLUSIVE p_A,p_B,p_C +verdict v11_boundary_7_1 1 111111111111111111110000000100000000000000000000000000000000 111111111111111111110000000100000000000000000000000000000000 111111111111111111110000000100000000000000000000000000000000 111111111111111111111111111000000000000000000000000000000000 21 21 21 27 20 7 1 32 9 256 0.0351562500 yes yes 20 7 1 32 9 256 0.0351562500 yes yes 20 7 1 32 9 256 0.0351562500 yes yes 0.0056383521 0.1908331949 0.0205232103 0.1769558653 0 PASS all_controls_lift_and_p +verdict v12_all_fail 1 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 0 0 0 0 0 0 0 60 1 1 1.0000000000 no no 0 0 0 60 1 1 1.0000000000 no no 0 0 0 60 1 1 1.0000000000 no no -0.0601718521 0.0601718521 -0.0431467986 0.0431467986 0 INCONCLUSIVE floor +verdict v13_all_pass 1 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 60 60 60 60 60 0 0 0 1 1 1.0000000000 no no 60 0 0 0 1 1 1.0000000000 no no 60 0 0 0 1 1 1.0000000000 no no -0.0601718521 0.0601718521 -0.0431467986 0.0431467986 0 INCONCLUSIVE ceiling +verdict v14_d_worse 1 111111111111111111111111111100000000000000000000000000000000 111111111111111111111111111100000000000000000000000000000000 111111111111111111111111111100000000000000000000000000000000 111111111111111111110000000010000000000000000000000000000000 28 28 28 21 20 1 8 31 511 512 0.9980468750 no no 20 1 8 31 511 512 0.9980468750 no no 20 1 8 31 511 512 0.9980468750 no no -0.2109995404 -0.0177177984 -0.1966895420 -0.0333419346 0 FAIL newcombe95_hi_D-C_below_delta +verdict v16_pass_vs_ab_not_c 1 111111111100000000000000000000000000000000000000000000000000 111111111100000000000000000000000000000000000000000000000000 111111111111111111111100000000000000000000000000000000000000 111111111111111111111111110000000000000000000000000000000000 10 10 22 26 10 16 0 34 1 65536 0.0000152588 yes yes 10 16 0 34 1 65536 0.0000152588 yes yes 22 4 0 34 1 16 0.0625000000 no no -0.0039209754 0.1359767812 0.0072318629 0.1251953912 0 INCONCLUSIVE lift_C,p_C +verdict v17_floor_boundary 1 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 111000000000000000000000000000000000000000000000000000000000 0 0 0 3 0 3 0 57 1 8 0.1250000000 no no 0 3 0 57 1 8 0.1250000000 no no 0 3 0 57 1 8 0.1250000000 no no -0.0185551384 0.1370051648 -0.0024619072 0.1186751326 0 INCONCLUSIVE floor +verdict v18_floor_above 1 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 111100000000000000000000000000000000000000000000000000000000 0 0 0 4 0 4 0 56 1 16 0.0625000000 no no 0 4 0 56 1 16 0.0625000000 no no 0 4 0 56 1 16 0.0625000000 no no -0.0058307887 0.1592535731 0.0102248728 0.1404477800 0 INCONCLUSIVE lift_A,p_A,lift_B,p_B,lift_C,p_C +verdict v19_ceiling_boundary 1 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111000 60 60 60 57 57 0 3 0 8 8 1.0000000000 no no 57 0 3 0 8 8 1.0000000000 no no 57 0 3 0 8 8 1.0000000000 no no -0.1370051648 0.0185551384 -0.1186751326 0.0024619072 0 INCONCLUSIVE ceiling +verdict v20_ceiling_below 1 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111110000 60 60 60 56 56 0 4 0 16 16 1.0000000000 no no 56 0 4 0 16 16 1.0000000000 no no 56 0 4 0 16 16 1.0000000000 no no -0.1592535731 0.0058307887 -0.1404477800 -0.0102248728 0 FAIL newcombe95_hi_D-C_below_delta +verdict v21_floor_overrides_fail 1 110000000000000000000000000000000000000000000000000000000000 110000000000000000000000000000000000000000000000000000000000 110000000000000000000000000000000000000000000000000000000000 110000000000000000000000000000000000000000000000000000000000 2 2 2 2 2 0 0 58 1 1 1.0000000000 no no 2 0 0 58 1 1 1.0000000000 no no 2 0 0 58 1 1 1.0000000000 no no -0.0644736706 0.0644736706 -0.0483819597 0.0483819597 0 INCONCLUSIVE floor diff --git a/tools/.crumb b/tools/.crumb index e9e4932b5..3354344c0 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -62,14 +62,14 @@ } ], "children_root": "sha256:8d3d3b952374b3b72e667521f07bf028c4c02bcefff772ad1a71cfff3e5266f2", - "compiled_at": "2026-10-30T11:28:28Z", + "compiled_at": "2026-10-30T16:52:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", + "digest": "sha256:80d9328bd7916b151ac14d02566116700c9f0dc14a7c456f3efb985ea865fa1f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 298, - "generated_at_commit": "a8a6a3d3c47ce797430f045edc5eae11594ba36d", - "source_tree": "sha256:acd1f1b0080968db950791b4eb2dce39bb0289dc0c2e8794bfc7449023446105" + "files": 304, + "generated_at_commit": "93384bda5fd5b9f20ccb182a48fcc19d2015b357", + "source_tree": "sha256:bb7382a75bfced4613f7388f85a4c24d4b5d2d0e56db742680566e2e2b0ddae4" }, "owner_lane": "", "provenance": { diff --git a/tools/gen/protocol/famsig.c b/tools/gen/protocol/famsig.c new file mode 100644 index 000000000..97f5af8c7 --- /dev/null +++ b/tools/gen/protocol/famsig.c @@ -0,0 +1,120 @@ +/* + * famsig.c -- GEN family signatures and the split disjointness rule (aien-spark/omega#476). + * Contract: tests/one_layer/generalization/families/FAMSIG_V1.md (frozen; this tool is its reference). + * Deterministic: no clock, no randomness, no environment; output depends only on the arguments and files. + * + * famsig canon print "gs1:" of one nesting shape + * famsig coarse print the coarse signature "gc1:" (rule R6) + * famsig token print the family token "gs1-<16 hex>" (record family field) + * famsig edges print the composition edges "parent>child", one per line, sorted + * famsig meta ... validate gen-meta / gen-split files; one line per task: + * "\t\t" + * famsig rule [--show-heldout] ... + * apply the disjointness rule (R1..R8) over every task of every file; + * report, then "RULE PASS" (exit 0) or "RULE FAIL" (exit 1). Heldout + * family signatures are printed only with --show-heldout. + * famsig split validate a sealed split file; print split_sha256, famlist_sha256, + * tasks, families (no signature, no task metadata) + * famsig famlist print the FAMLIST bytes whose sha256 is famlist_sha256 + * famsig compare + * the reviewer's blind re-declaration check: both gen-split files with the + * same task ids; prints only "tasks= mismatches=" (no signature, no + * task id); exit 0 when every task has the same signature, else 1 + * Exit: 0 ok, 1 refused (reason on stderr) or rule failure, 2 usage. + */ +#include "famsig_lib.h" + +static int usage(void) +{ + fprintf(stderr, "usage: famsig canon | coarse | token | edges | meta ... | rule [--show-heldout] ... | split | famlist | compare \n"); + return 2; +} + +static int read_all(int argc, char **argv, int from, FsSet *set) +{ + char err[512]; + for (int i = from; i < argc; i++) + if (fs_read(argv[i], set, err, sizeof err)) { fprintf(stderr, "famsig: %s\n", err); return -1; } + return 0; +} + +static int read_split(const char *path, FsSet *set) +{ + char err[512]; + if (fs_read(path, set, err, sizeof err)) { fprintf(stderr, "famsig: %s\n", err); return -1; } + if (!set->is_split) { fprintf(stderr, "famsig: %s: not a gen-split file\n", path); return -1; } + if (set->n == 0) { fprintf(stderr, "famsig: %s: no task lines\n", path); return -1; } + return 0; +} + +int main(int argc, char **argv) +{ + if (argc < 2) return usage(); + const char *cmd = argv[1]; + if (!strcmp(cmd, "coarse")) { + if (argc != 3) return usage(); + FsShape sh; + if (fs_shape(argv[2], &sh)) { fprintf(stderr, "famsig: shape: %s\n", sh.err); return 1; } + char c[FS_SIGMAX + 4]; + fs_coarse(&sh, c); + printf("%s\n", c); + return 0; + } + if (!strcmp(cmd, "canon") || !strcmp(cmd, "edges") || !strcmp(cmd, "token")) { + if (argc != 3) return usage(); + FsShape sh; + if (fs_shape(argv[2], &sh)) { fprintf(stderr, "famsig: shape: %s\n", sh.err); return 1; } + if (cmd[0] == 'c') { printf("gs1:%s\n", sh.canon); return 0; } + if (cmd[0] == 't') { char sig[FS_SIGMAX + 4], tok[24]; snprintf(sig, sizeof sig, "gs1:%s", sh.canon); fs_token(sig, tok); printf("%s\n", tok); return 0; } + char e[64][16]; + int ne = fs_edges(&sh, e, 64); + for (int i = 0; i < ne; i++) printf("%s\n", e[i]); + return 0; + } + if (!strcmp(cmd, "meta")) { + if (argc < 3) return usage(); + FsSet set = {0}; + if (read_all(argc, argv, 2, &set)) return 1; + for (int i = 0; i < set.n; i++) printf("%s\t%s\t%s\n", set.m[i].id, fs_split[set.m[i].split], set.m[i].sig); + return 0; + } + if (!strcmp(cmd, "rule")) { + int show = 0, from = 2; + if (argc > 2 && !strcmp(argv[2], "--show-heldout")) { show = 1; from = 3; } + if (argc <= from) return usage(); + FsSet set = {0}; + if (read_all(argc, argv, from, &set)) { printf("RULE FAIL\n"); return 1; } + int f = fs_rule(&set, stdout, show); + printf(f ? "RULE FAIL\n" : "RULE PASS\n"); + return f ? 1 : 0; + } + if (!strcmp(cmd, "split") || !strcmp(cmd, "famlist")) { + if (argc != 3) return usage(); + FsSet set = {0}; + if (read_split(argv[2], &set)) return 1; + int nf; + char *fl = fs_famlist(&set, &nf); + if (cmd[0] == 'f') { fputs(fl, stdout); return 0; } + unsigned char d[32]; + char hex[65]; + sha256_hash((const unsigned char *)fl, strlen(fl), d); + fs_hex(d, hex); + printf("split_sha256=%s\nfamlist_sha256=%s\ntasks=%d\nfamilies=%d\n", set.split_sha256, hex, set.n, nf); + free(fl); + return 0; + } + if (!strcmp(cmd, "compare")) { + if (argc != 4) return usage(); + FsSet a = {0}, b = {0}; + if (read_split(argv[2], &a) || read_split(argv[3], &b)) return 1; + if (a.n != b.n) { fprintf(stderr, "famsig: compare: %d tasks vs %d tasks\n", a.n, b.n); return 1; } + int mis = 0; + for (int i = 0; i < a.n; i++) { + if (strcmp(a.m[i].id, b.m[i].id)) { fprintf(stderr, "famsig: compare: task lists differ at line %d\n", i + 3); return 1; } + mis += strcmp(a.m[i].sig, b.m[i].sig) != 0; + } + printf("tasks=%d mismatches=%d\n", a.n, mis); + return mis ? 1 : 0; + } + return usage(); +} diff --git a/tools/gen/protocol/famsig_lib.h b/tools/gen/protocol/famsig_lib.h new file mode 100644 index 000000000..4c87b2b0e --- /dev/null +++ b/tools/gen/protocol/famsig_lib.h @@ -0,0 +1,574 @@ +/* + * famsig_lib.h -- GEN family-signature contract v1 (aien-spark/omega#476), shared by famsig.c and + * gencheck.c. The contract is tests/one_layer/generalization/families/FAMSIG_V1.md; this file is its + * only implementation. Header-only (static functions) so both tools compile from one source. + * + * Shape grammar (no spaces, at most FS_MAXLEN bytes, depth at most FS_MAXDEPTH, at most FS_MAXNODES nodes): + * node := leaf | container "(" list ")" | "seq(" node "," list ")" + * list := node ("," node)* + * leaf := "arith" | "array" | "nested" + * container := "branch" | "call" | "loop" + * Canonical form: a "seq" directly inside a container or inside another "seq" is flattened into it; + * every child list is sorted by byte order of the children's canonical strings (duplicates kept). + * Signature = "gs1:" + canonical form. + */ +#ifndef GEN_FAMSIG_LIB_H +#define GEN_FAMSIG_LIB_H +#include +#include +#include +#include "sha256.h" + +#define FS_NPRIM 6 +#define FS_SEQ 6 +#define FS_MAXLEN 512 +#define FS_MAXDEPTH 8 +#define FS_MAXNODES 64 +#define FS_MAXCOUNT 64 +#define FS_MAXID 40 +#define FS_SIGMAX (FS_MAXLEN + 8) + +/* fixed order: the order of the six count fields of a metadata line */ +static const char *const fs_prim[FS_NPRIM + 1] = {"arith", "branch", "loop", "array", "call", "nested", "seq"}; + +static int fs_is_container(int k) { return k == 1 || k == 2 || k == 4; } +static int fs_is_leaf(int k) { return k == 0 || k == 3 || k == 5; } + +typedef struct { + int kind; + int nkid; + int kid[FS_MAXNODES]; +} FsNode; + +typedef struct { + FsNode node[FS_MAXNODES]; + int nnode; + int root; + int count[FS_NPRIM]; /* primitive occurrences (seq not counted) */ + char canon[FS_SIGMAX]; /* canonical form, no "gs1:" prefix */ + char err[160]; +} FsShape; + +typedef struct { + const char *s; + size_t i; + FsShape *sh; +} FsParser; + +static int fs_word(FsParser *p) +{ + for (int k = 0; k <= FS_NPRIM; k++) { + size_t n = strlen(fs_prim[k]); + if (!strncmp(p->s + p->i, fs_prim[k], n)) { + char c = p->s[p->i + n]; + if (c == '(' || c == ',' || c == ')' || c == 0) { p->i += n; return k; } + } + } + return -1; +} + +static int fs_node(FsParser *p, int depth) +{ + FsShape *sh = p->sh; + if (depth > FS_MAXDEPTH) { snprintf(sh->err, sizeof sh->err, "nesting deeper than %d", FS_MAXDEPTH); return -1; } + size_t at = p->i; + int k = fs_word(p); + if (k < 0) { snprintf(sh->err, sizeof sh->err, "byte %zu: expected one of arith array nested branch call loop seq", at); return -1; } + if (sh->nnode >= FS_MAXNODES) { snprintf(sh->err, sizeof sh->err, "more than %d nodes", FS_MAXNODES); return -1; } + int me = sh->nnode++; + FsNode *n = &sh->node[me]; + n->kind = k; + n->nkid = 0; + if (k < FS_NPRIM) sh->count[k]++; + if (fs_is_leaf(k)) { + if (p->s[p->i] == '(') { snprintf(sh->err, sizeof sh->err, "byte %zu: %s is a leaf and takes no children", p->i, fs_prim[k]); return -1; } + return me; + } + if (p->s[p->i] != '(') { snprintf(sh->err, sizeof sh->err, "byte %zu: %s needs '(' and at least %d child", p->i, fs_prim[k], k == FS_SEQ ? 2 : 1); return -1; } + p->i++; + for (;;) { + int c = fs_node(p, depth + 1); + if (c < 0) return -1; + sh->node[me].kid[sh->node[me].nkid++] = c; + if (p->s[p->i] == ',') { p->i++; continue; } + if (p->s[p->i] == ')') { p->i++; break; } + snprintf(sh->err, sizeof sh->err, "byte %zu: expected ',' or ')'", p->i); + return -1; + } + if (k == FS_SEQ && sh->node[me].nkid < 2) { snprintf(sh->err, sizeof sh->err, "byte %zu: seq needs at least 2 children", at); return -1; } + return me; +} + +/* flattened children of node v (a seq child of a container or of a seq is replaced by its own flattened children) */ +static void fs_flat(const FsShape *sh, int v, int *out, int *nout) +{ + const FsNode *n = &sh->node[v]; + for (int j = 0; j < n->nkid; j++) { + int c = n->kid[j]; + if (sh->node[c].kind == FS_SEQ) fs_flat(sh, c, out, nout); + else out[(*nout)++] = c; + } +} + +static int fs_cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } + +/* canonical string of node v, malloc'd */ +static char *fs_canon_node(const FsShape *sh, int v) +{ + const FsNode *n = &sh->node[v]; + if (fs_is_leaf(n->kind)) return strdup(fs_prim[n->kind]); + int kids[FS_MAXNODES], nk = 0; + fs_flat(sh, v, kids, &nk); + char *ks[FS_MAXNODES]; + size_t len = strlen(fs_prim[n->kind]) + 2; + for (int j = 0; j < nk; j++) { ks[j] = fs_canon_node(sh, kids[j]); len += strlen(ks[j]) + 1; } + qsort(ks, (size_t)nk, sizeof ks[0], fs_cmpstr); + char *r = malloc(len + 1); + if (!r) { perror("famsig"); exit(2); } + strcpy(r, fs_prim[n->kind]); + strcat(r, "("); + for (int j = 0; j < nk; j++) { if (j) strcat(r, ","); strcat(r, ks[j]); free(ks[j]); } + strcat(r, ")"); + return r; +} + +/* parse + canonicalise; 0 ok, -1 with sh->err set */ +static int fs_shape(const char *s, FsShape *sh) +{ + memset(sh, 0, sizeof *sh); + size_t len = strlen(s); + if (len == 0 || len > FS_MAXLEN) { snprintf(sh->err, sizeof sh->err, "shape length %zu, must be 1..%d", len, FS_MAXLEN); return -1; } + FsParser p = {s, 0, sh}; + sh->root = fs_node(&p, 1); + if (sh->root < 0) return -1; + if (s[p.i]) { snprintf(sh->err, sizeof sh->err, "byte %zu: extra text after the shape", p.i); return -1; } + char *c = fs_canon_node(sh, sh->root); + if (strlen(c) >= sizeof sh->canon) { free(c); snprintf(sh->err, sizeof sh->err, "canonical form too long"); return -1; } + strcpy(sh->canon, c); + free(c); + return 0; +} + +/* composition edges "parent>child" (container -> head primitive of each flattened child); unique, sorted */ +static int fs_edges(const FsShape *sh, char out[][16], int max) +{ + int ne = 0; + for (int v = 0; v < sh->nnode; v++) { + if (!fs_is_container(sh->node[v].kind)) continue; + int kids[FS_MAXNODES], nk = 0; + fs_flat(sh, v, kids, &nk); + for (int j = 0; j < nk; j++) { + char e[16]; + snprintf(e, sizeof e, "%s>%s", fs_prim[sh->node[v].kind], fs_prim[sh->node[kids[j]].kind]); + int dup = 0; + for (int q = 0; q < ne; q++) if (!strcmp(out[q], e)) dup = 1; + if (!dup && ne < max) strcpy(out[ne++], e); + } + } + for (int a = 1; a < ne; a++) + for (int b = a; b > 0 && strcmp(out[b - 1], out[b]) > 0; b--) { char t[16]; strcpy(t, out[b]); strcpy(out[b], out[b - 1]); strcpy(out[b - 1], t); } + return ne; +} + +/* ---- coarse form (rule R6) and coarse edges (rule R7) ----------------------------------------- * + * Coarse form of a shape: (1) `nested` is read as `arith`; (2) seq is flattened as in the canonical form; + * (3) every child list is sorted and duplicate siblings are collapsed to one; a seq left with one child is + * that child; (4) a top-level seq whose children are exactly one container plus only leaves is replaced by + * that container. Coarse signature = "gc1:" + coarse form. */ +static char *fs_coarse_node(const FsShape *sh, int v) +{ + const FsNode *n = &sh->node[v]; + if (fs_is_leaf(n->kind)) return strdup(n->kind == 5 ? "arith" : fs_prim[n->kind]); + int kids[FS_MAXNODES], nk = 0; + fs_flat(sh, v, kids, &nk); + char *ks[FS_MAXNODES]; + for (int j = 0; j < nk; j++) ks[j] = fs_coarse_node(sh, kids[j]); + qsort(ks, (size_t)nk, sizeof ks[0], fs_cmpstr); + int u = 0; + for (int j = 0; j < nk; j++) { if (u && !strcmp(ks[j], ks[u - 1])) free(ks[j]); else ks[u++] = ks[j]; } + if (n->kind == FS_SEQ && u == 1) return ks[0]; + if (n->kind == FS_SEQ && v == sh->root) { + int nc = 0, ci = -1; + for (int j = 0; j < u; j++) if (strchr(ks[j], '(')) { nc++; ci = j; } + if (nc == 1) { + char *r = ks[ci]; + for (int j = 0; j < u; j++) if (j != ci) free(ks[j]); + return r; + } + } + size_t len = strlen(fs_prim[n->kind]) + 3; + for (int j = 0; j < u; j++) len += strlen(ks[j]) + 1; + char *r = malloc(len); + if (!r) { perror("famsig"); exit(2); } + strcpy(r, fs_prim[n->kind]); + strcat(r, "("); + for (int j = 0; j < u; j++) { if (j) strcat(r, ","); strcat(r, ks[j]); free(ks[j]); } + strcat(r, ")"); + return r; +} + +/* "gc1:" into out (FS_SIGMAX + 4 bytes) */ +static void fs_coarse(const FsShape *sh, char *out) +{ + char *c = fs_coarse_node(sh, sh->root); + snprintf(out, FS_SIGMAX + 4, "gc1:%s", c); + free(c); +} + +/* coarse composition edges: as fs_edges with `nested` read as `arith`; unique, sorted */ +static int fs_cedges(const FsShape *sh, char out[][16], int max) +{ + int ne = fs_edges(sh, out, max); + for (int q = 0; q < ne; q++) { + char *gt = strchr(out[q], '>'); + if (gt && !strcmp(gt + 1, "nested")) strcpy(gt + 1, "arith"); + } + int u = 0; + for (int a = 1; a < ne; a++) + for (int b = a; b > 0 && strcmp(out[b - 1], out[b]) > 0; b--) { char t[16]; strcpy(t, out[b]); strcpy(out[b], out[b - 1]); strcpy(out[b - 1], t); } + for (int q = 0; q < ne; q++) if (!u || strcmp(out[q], out[u - 1])) { if (u != q) strcpy(out[u], out[q]); u++; } + return u; +} + +/* ---- metadata lines ---------------------------------------------------------------------------- */ + +enum { FS_CORPUS, FS_DEV, FS_HELDOUT, FS_NSPLIT }; +static const char *const fs_split[FS_NSPLIT] = {"corpus", "dev", "heldout"}; + +typedef struct { + char id[FS_MAXID + 1]; + int split; + int count[FS_NPRIM]; + char shape[FS_MAXLEN + 1]; + char sig[FS_SIGMAX + 4]; /* "gs1:" + canonical */ + char coarse[FS_SIGMAX + 4]; /* "gc1:" + coarse form (R6) */ + int distinct; /* distinct primitives in the shape */ + char src[256]; /* file:line, for messages */ +} FsMeta; + +static int fs_task_id_ok(const char *s) +{ + size_t n = strlen(s); + if (n < 1 || n > FS_MAXID) return 0; + for (size_t i = 0; i < n; i++) + if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_' || s[i] == '-')) return 0; + return 1; +} + +static int fs_count_ok(const char *s, int *v) +{ + size_t n = strlen(s); + if (n < 1 || n > 2 || (n == 2 && s[0] == '0')) return 0; + for (size_t i = 0; i < n; i++) if (s[i] < '0' || s[i] > '9') return 0; + *v = atoi(s); + return *v <= FS_MAXCOUNT; +} + +/* one metadata line: " ", + * fields separated by exactly one space. 0 ok, -1 with err. */ +static int fs_meta_line(const char *line, FsMeta *m, char *err, size_t errn) +{ + char buf[1024]; + size_t len = strlen(line); + if (len >= sizeof buf) { snprintf(err, errn, "line too long"); return -1; } + memcpy(buf, line, len + 1); + char *f[10]; + int nf = 0; + char *s = buf; + for (;;) { + if (nf == 10) { snprintf(err, errn, "more than 9 fields"); return -1; } + f[nf++] = s; + char *sp = strchr(s, ' '); + if (!sp) break; + *sp = 0; + s = sp + 1; + } + if (nf != 9) { snprintf(err, errn, "%d fields, need 9: task split arith branch loop array call nested shape", nf); return -1; } + for (int i = 0; i < nf; i++) if (!*f[i]) { snprintf(err, errn, "empty field %d (fields are separated by exactly one space)", i + 1); return -1; } + memset(m, 0, sizeof *m); + if (!fs_task_id_ok(f[0])) { snprintf(err, errn, "task id '%s' is not [a-z0-9_-]{1,40}", f[0]); return -1; } + strcpy(m->id, f[0]); + m->split = -1; + for (int k = 0; k < FS_NSPLIT; k++) if (!strcmp(f[1], fs_split[k])) m->split = k; + if (m->split < 0) { snprintf(err, errn, "split '%s' is not corpus, dev or heldout", f[1]); return -1; } + for (int k = 0; k < FS_NPRIM; k++) + if (!fs_count_ok(f[2 + k], &m->count[k])) { snprintf(err, errn, "%s count '%s' is not a decimal 0..%d without leading zeros", fs_prim[k], f[2 + k], FS_MAXCOUNT); return -1; } + FsShape sh; + if (fs_shape(f[8], &sh)) { snprintf(err, errn, "shape: %s", sh.err); return -1; } + for (int k = 0; k < FS_NPRIM; k++) + if (sh.count[k] != m->count[k]) { snprintf(err, errn, "declared %s=%d but the shape has %d", fs_prim[k], m->count[k], sh.count[k]); return -1; } + strcpy(m->shape, f[8]); + snprintf(m->sig, sizeof m->sig, "gs1:%s", sh.canon); + fs_coarse(&sh, m->coarse); + for (int k = 0; k < FS_NPRIM; k++) if (m->count[k]) m->distinct++; + return 0; +} + +/* ---- files ------------------------------------------------------------------------------------- */ + +static char *fs_slurp(const char *path, size_t *len) +{ + FILE *fp = fopen(path, "rb"); + if (!fp) return NULL; + size_t cap = 4096, n = 0; + char *b = malloc(cap + 1); + if (!b) { fclose(fp); return NULL; } + size_t r; + while ((r = fread(b + n, 1, cap - n, fp)) > 0) { + n += r; + if (n == cap) { cap *= 2; char *nb = realloc(b, cap + 1); if (!nb) { free(b); fclose(fp); return NULL; } b = nb; } + } + fclose(fp); + b[n] = 0; + *len = n; + return b; +} + +static void fs_hex(const unsigned char *d, char out[65]) +{ + for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", d[i]); + out[64] = 0; +} + +/* family token: "gs1-" + first 16 hex of sha256(signature), signature = "gs1:" + canonical (no newline); + * matches [a-z0-9_-]{1,64}, the record "family" field of tools/gen/retrieval/CONTRACT.md */ +static void fs_token(const char *sig, char out[24]) +{ + unsigned char d[32]; + char hex[65]; + sha256_hash((const unsigned char *)sig, strlen(sig), d); + fs_hex(d, hex); + snprintf(out, 24, "gs1-%.16s", hex); +} + +static int fs_hex64_ok(const char *s) +{ + if (strlen(s) != 64) return 0; + for (int i = 0; i < 64; i++) if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return 0; + return 1; +} + +typedef struct { + FsMeta *m; + int n, cap; + int is_split; /* last file read was a gen-split file */ + char salt[65]; + char split_sha256[65]; +} FsSet; + +static void fs_push(FsSet *set, const FsMeta *m) +{ + if (set->n == set->cap) { + set->cap = set->cap ? 2 * set->cap : 64; + set->m = realloc(set->m, (size_t)set->cap * sizeof *set->m); + if (!set->m) { perror("famsig"); exit(2); } + } + set->m[set->n++] = *m; +} + +/* Reads a "gen-meta 1" file (public: corpus/dev lines; '#' comments and blank lines allowed; heldout + * lines refused) or a "gen-split 1" file (sealed: "salt <64 hex>", then heldout lines only, ids strictly + * ascending, no comments or blank lines). Bytes: '\n' and 0x20..0x7E only, last byte '\n'. + * Appends to set; 0 ok, -1 with err. */ +static int fs_read(const char *path, FsSet *set, char *err, size_t errn) +{ + size_t len; + char *b = fs_slurp(path, &len); + if (!b) { snprintf(err, errn, "%s: cannot read", path); return -1; } + if (len == 0 || b[len - 1] != '\n') { snprintf(err, errn, "%s: empty or no final newline", path); free(b); return -1; } + for (size_t i = 0; i < len; i++) + if (b[i] != '\n' && (b[i] < 0x20 || b[i] > 0x7e)) { snprintf(err, errn, "%s: byte %zu is not \\n or printable ASCII", path, i); free(b); return -1; } + int split_file = !strncmp(b, "gen-split 1\n", 12); + if (!split_file && strncmp(b, "gen-meta 1\n", 11)) { snprintf(err, errn, "%s: first line must be 'gen-meta 1' or 'gen-split 1'", path); free(b); return -1; } + set->is_split = split_file; + if (split_file) { + unsigned char d[32]; + sha256_hash((const unsigned char *)b, len, d); + fs_hex(d, set->split_sha256); + } + char *line = strchr(b, '\n') + 1; + int ln = 1, first = 1; + char prev[FS_MAXID + 1] = ""; + int rc = 0; + while (*line) { + char *nl = strchr(line, '\n'); + *nl = 0; + ln++; + if (split_file && ln == 2) { + if (strncmp(line, "salt ", 5) || !fs_hex64_ok(line + 5)) { snprintf(err, errn, "%s:2: must be 'salt <64 lowercase hex>'", path); rc = -1; break; } + snprintf(set->salt, sizeof set->salt, "%s", line + 5); + } else if (!split_file && (line[0] == 0 || line[0] == '#')) { + /* comment or blank line */ + } else { + FsMeta m; + char e[200]; + if (fs_meta_line(line, &m, e, sizeof e)) { snprintf(err, errn, "%s:%d: %s", path, ln, e); rc = -1; break; } + snprintf(m.src, sizeof m.src, "%s:%d", path, ln); + if (split_file && m.split != FS_HELDOUT) { snprintf(err, errn, "%s:%d: a gen-split file holds heldout lines only", path, ln); rc = -1; break; } + if (!split_file && m.split == FS_HELDOUT) { snprintf(err, errn, "%s:%d: heldout metadata belongs only in the sealed gen-split file", path, ln); rc = -1; break; } + if (split_file && !first && strcmp(prev, m.id) >= 0) { snprintf(err, errn, "%s:%d: task ids must be strictly ascending (byte order)", path, ln); rc = -1; break; } + strcpy(prev, m.id); + first = 0; + fs_push(set, &m); + } + line = nl + 1; + } + if (!rc && split_file && ln < 2) { snprintf(err, errn, "%s: missing salt line", path); rc = -1; } + free(b); + return rc; +} + +/* FAMLIST bytes of a split: "gen-famlist 1\n" "salt \n" then every distinct heldout signature, + * sorted by byte order, one per line. Returns malloc'd string. */ +static char *fs_famlist(const FsSet *set, int *nfam) +{ + char **sig = malloc((size_t)(set->n ? set->n : 1) * sizeof *sig); + int ns = 0; + for (int i = 0; i < set->n; i++) if (set->m[i].split == FS_HELDOUT) sig[ns++] = set->m[i].sig; + qsort(sig, (size_t)ns, sizeof sig[0], fs_cmpstr); + size_t cap = 128 + (size_t)ns * (FS_SIGMAX + 8); /* header "gen-famlist 1\nsalt <64 hex>\n" is 84 bytes */ + char *out = malloc(cap); + if (!out || !sig) { perror("famsig"); exit(2); } + int n = snprintf(out, cap, "gen-famlist 1\nsalt %s\n", set->salt); + int nf = 0; + for (int i = 0; i < ns; i++) { + if (i && !strcmp(sig[i], sig[i - 1])) continue; + int w = snprintf(out + n, cap - (size_t)n, "%s\n", sig[i]); + if (w < 0 || (size_t)w >= cap - (size_t)n) { fprintf(stderr, "famsig: family list overflow\n"); exit(2); } + n += w; + nf++; + } + free(sig); + *nfam = nf; + return out; +} + +static int fs_cmpmeta_sig(const void *a, const void *b) +{ + const FsMeta *x = a, *y = b; + int c = strcmp(x->sig, y->sig); + if (c) return c; + if (x->split != y->split) return x->split < y->split ? -1 : 1; + return strcmp(x->id, y->id); +} + +/* The disjointness rule (FAMSIG_V1.md section 4) over every line read. Prints a report to out + * (heldout family signatures only when show_heldout); returns the number of rule failures. */ +/* composition edges of one shape that are absent from the corpus edge list */ +static int fs_novel(const char *shape, char cedge[][16], int nce) +{ + FsShape sh; + if (fs_shape(shape, &sh)) return 0; + char e[64][16]; + int ne = fs_cedges(&sh, e, 64), novel = 0; + for (int q = 0; q < ne; q++) { int seen = 0; for (int r = 0; r < nce; r++) if (!strcmp(cedge[r], e[q])) seen = 1; novel += !seen; } + return novel; +} + +static int fs_rule(FsSet *set, FILE *out, int show_heldout) +{ + int fails = 0; + FsMeta *m = set->m; + int n = set->n; + /* R2 unique task ids across every input */ + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (!strcmp(m[i].id, m[j].id)) { fprintf(out, "FAIL R2 duplicate task id %s (%s, %s)\n", m[i].id, m[i].src, m[j].src); fails++; } + /* R3 a signature belongs to one split */ + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (m[i].split != m[j].split && !strcmp(m[i].sig, m[j].sig)) { + int ho = m[i].split == FS_HELDOUT || m[j].split == FS_HELDOUT; + fprintf(out, "FAIL R3 family shared by %s and %s: %s %s and %s\n", fs_split[m[i].split], fs_split[m[j].split], + ho && !show_heldout ? "(heldout signature hidden)" : m[i].sig, m[i].id, m[j].id); + fails++; + } + /* R6 coarse signatures are pairwise disjoint across splits */ + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (m[i].split != m[j].split && !strcmp(m[i].coarse, m[j].coarse) && strcmp(m[i].sig, m[j].sig)) { + int ho = m[i].split == FS_HELDOUT || m[j].split == FS_HELDOUT; + fprintf(out, "FAIL R6 coarse family shared by %s and %s: %s %s and %s\n", fs_split[m[i].split], fs_split[m[j].split], + ho && !show_heldout ? "(heldout signature hidden)" : m[i].coarse, m[i].id, m[j].id); + fails++; + } + /* R4 every heldout primitive appears in some corpus task; R5 heldout shapes use >= 2 distinct primitives */ + int corpus_has[FS_NPRIM] = {0}, ncorpus = 0; + for (int i = 0; i < n; i++) if (m[i].split == FS_CORPUS) { ncorpus++; for (int k = 0; k < FS_NPRIM; k++) if (m[i].count[k]) corpus_has[k] = 1; } + for (int i = 0; i < n; i++) { + if (m[i].split != FS_HELDOUT) continue; + for (int k = 0; k < FS_NPRIM; k++) + if (m[i].count[k] && !corpus_has[k]) { fprintf(out, "FAIL R4 heldout task %s uses %s, which no corpus task uses%s\n", m[i].id, fs_prim[k], ncorpus ? "" : " (no corpus metadata given)"); fails++; } + if (m[i].distinct < 2) { fprintf(out, "FAIL R5 heldout task %s uses fewer than 2 distinct primitives\n", m[i].id); fails++; } + } + /* corpus composition edges, for the novelty report */ + static char cedge[256][16]; + int nce = 0; + for (int i = 0; i < n; i++) { + if (m[i].split != FS_CORPUS) continue; + FsShape sh; + if (fs_shape(m[i].shape, &sh)) continue; + char e[64][16]; + int ne = fs_cedges(&sh, e, 64); + for (int q = 0; q < ne; q++) { + int dup = 0; + for (int r = 0; r < nce; r++) if (!strcmp(cedge[r], e[q])) dup = 1; + if (!dup && nce < 256) strcpy(cedge[nce++], e[q]); + } + } + /* R7 every heldout shape has at least one coarse composition edge that no corpus shape has */ + for (int i = 0; i < n; i++) { + if (m[i].split != FS_HELDOUT) continue; + if (fs_novel(m[i].shape, cedge, nce) < 1) { fprintf(out, "FAIL R7 heldout task %s has no composition edge absent from the corpus (novel_edges=0)\n", m[i].id); fails++; } + } + /* R8 no top-level step of a dev or heldout shape (after seq flattening) is a container subtree whose coarse form + * equals the coarse signature of a corpus task. Leaf steps are not compared (leaf reuse is what R4 requires); a + * shape whose whole coarse signature already equals the corpus one is left to R3/R6. */ + for (int i = 0; i < n; i++) { + if (m[i].split == FS_CORPUS) continue; + FsShape sh; + if (fs_shape(m[i].shape, &sh) || sh.node[sh.root].kind != FS_SEQ) continue; + int kids[FS_MAXNODES], nk = 0; + fs_flat(&sh, sh.root, kids, &nk); + for (int q = 0; q < nk; q++) { + if (!fs_is_container(sh.node[kids[q]].kind)) continue; + char *c = fs_coarse_node(&sh, kids[q]); + for (int j = 0; j < n; j++) + if (m[j].split == FS_CORPUS && !strncmp(m[j].coarse, "gc1:", 4) && !strcmp(m[j].coarse + 4, c) && strcmp(m[j].coarse, m[i].coarse)) { + int ho = m[i].split == FS_HELDOUT; + fprintf(out, "FAIL R8 %s task %s has a top-level step equal to corpus family %s of %s: %s\n", fs_split[m[i].split], m[i].id, + m[j].sig, m[j].id, ho && !show_heldout ? "(heldout step hidden)" : c); + fails++; + break; + } + free(c); + } + } + /* report: per split counts, then families in signature order */ + FsMeta *s = malloc((size_t)(n ? n : 1) * sizeof *s); + if (!s) { perror("famsig"); exit(2); } + memcpy(s, m, (size_t)n * sizeof *s); + qsort(s, (size_t)n, sizeof *s, fs_cmpmeta_sig); + for (int sp = 0; sp < FS_NSPLIT; sp++) { + int t = 0, f = 0; + for (int i = 0; i < n; i++) if (s[i].split == sp) { t++; if (i == 0 || strcmp(s[i].sig, s[i - 1].sig) || s[i - 1].split != sp) f++; } + fprintf(out, "split %s tasks=%d families=%d\n", fs_split[sp], t, f); + } + for (int i = 0; i < n;) { + int j = i; + while (j < n && !strcmp(s[j].sig, s[i].sig) && s[j].split == s[i].split) j++; + if (s[i].split != FS_HELDOUT || show_heldout) { + fprintf(out, "family %s %s tasks=%d", fs_split[s[i].split], s[i].sig, j - i); + if (s[i].split == FS_HELDOUT) { + int novel = fs_novel(s[i].shape, cedge, nce); + fprintf(out, " novel_edges=%d", novel); + } + fputc('\n', out); + } + i = j; + } + free(s); + return fails; +} + +#endif diff --git a/tools/gen/protocol/gencheck.c b/tools/gen/protocol/gencheck.c new file mode 100644 index 000000000..949463184 --- /dev/null +++ b/tools/gen/protocol/gencheck.c @@ -0,0 +1,833 @@ +/* + * gencheck.c -- strict validator of the GEN protocol manifest (aien-spark/omega#476). + * Manifest: tests/one_layer/generalization/protocol.json (schema omega.gen.protocol.v1), the machine copy of + * docs/one_layer/GEN_PROTOCOL_V1.md. Family rules: tests/one_layer/generalization/families/FAMSIG_V1.md, + * through famsig_lib.h (the same code as famsig). Deterministic: no clock, no randomness, no environment. + * + * gencheck manifest + * Parse the manifest with a strict JSON reader (ASCII only; duplicate keys, unknown keys, missing keys + * and wrong types are refused), check every frozen v1 constant, re-hash every pinned file under , + * check the dev split (task list = META ids = case directories, families = META signatures) and, once + * the corpus META is frozen, apply the disjointness rule R1..R8 over dev + corpus. + * Record-set manifests (C and D) are re-hashed once their paths and digests are set. + * gencheck run-ready + * Everything `manifest` checks with nothing pending: any null field, any interface file missing from + * , or no `interfaces` entry named by retrieval.contract (genret-cli-v3) refuses. Required before + * the seal is opened and before the run; there is no "pending merge" continue. + * gencheck open + * The seal-open check (custodian, in a clean checkout of the OPEN commit; reducer, on protocol.json taken + * from that commit): everything `manifest` checks with nothing pending (every interface file present, + * corpus META, prefix, both record sets, HELDOUT.pub and tasks digests set), and the OPEN receipt + * ("gen-open 1", omega_commit, protocol_sha256, tasks_sha256, heldout_pub_sha256) matches protocol.json. + * gencheck split + * Everything `manifest` checks, then (reviewer, after the seal): SPLIT parses (gen-split 1), HELDOUT.pub + * parses (gen-heldout-pub 1) and matches SPLIT (split_sha256, famlist_sha256, task ids, counts), the + * held-out count and minimum family count of the manifest hold, and R1..R8 pass over dev + corpus + + * SPLIT. Fails closed when splits.heldout.pub_sha256 or tasks_sha256 is null in the manifest. Held-out + * signatures are never printed. + * Output: "ok ..." lines on stdout, then "GENCHECK PASS" (exit 0). A refusal prints "gencheck: FAIL " + * on stderr and "GENCHECK FAIL" on stdout (exit 1). Usage errors exit 2. + */ +#include "famsig_lib.h" +#include +#include +#include + +#define GC_MAXENT 1024 +#define GC_PATH 192 +#define GC_DEPTH 8 + +typedef struct { + char path[GC_PATH]; /* e.g. splits.dev.tasks[3] */ + char norm[GC_PATH]; /* indices replaced: splits.dev.tasks[] */ + char type; /* o a s i n b z (object array string integer number bool null) */ + char *raw; /* string contents, number text, true/false/null; NULL for o and a */ +} Ent; + +static Ent ent[GC_MAXENT]; +static int nent; + +static void gc_fail(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn)); +static void gc_fail(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fputs("gencheck: FAIL ", stderr); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); + va_end(ap); + printf("GENCHECK FAIL\n"); + exit(1); +} + +/* ---------- strict JSON reader ---------- */ + +typedef struct { const char *s; size_t i, n; } Jp; + +static void j_ws(Jp *p) { while (p->i < p->n && (p->s[p->i] == ' ' || p->s[p->i] == '\n' || p->s[p->i] == '\t')) p->i++; } + +static Ent *j_add(const char *path, const char *norm, char type, char *raw) +{ + if (nent >= GC_MAXENT) gc_fail("json: more than %d values", GC_MAXENT); + Ent *e = &ent[nent++]; + snprintf(e->path, sizeof e->path, "%s", path); + snprintf(e->norm, sizeof e->norm, "%s", norm); + e->type = type; + e->raw = raw; + return e; +} + +static char *j_string(Jp *p) +{ + if (p->i >= p->n || p->s[p->i] != '"') gc_fail("json: expected string at byte %zu", p->i); + p->i++; + char *out = malloc(p->n + 1); + size_t k = 0; + for (;;) { + if (p->i >= p->n) gc_fail("json: unterminated string"); + char c = p->s[p->i++]; + if (c == '"') break; + if (c == '\\') { + if (p->i >= p->n) gc_fail("json: bad escape"); + char e = p->s[p->i++]; + if (e != '"' && e != '\\' && e != '/') gc_fail("json: escape \\%c not allowed (only \\\" \\\\ \\/)", e); + out[k++] = e; + continue; + } + if ((unsigned char)c < 0x20 || (unsigned char)c > 0x7e) gc_fail("json: non-printable byte in string at %zu", p->i - 1); + out[k++] = c; + } + out[k] = 0; + return out; +} + +static int j_isdig(char c) { return c >= '0' && c <= '9'; } + +static void j_value(Jp *p, const char *path, const char *norm, int depth); + +static void j_object(Jp *p, const char *path, const char *norm, int depth) +{ + j_add(path, norm, 'o', NULL); + p->i++; + char *keys[128]; + int nk = 0; + j_ws(p); + if (p->i < p->n && p->s[p->i] == '}') gc_fail("json: empty object at %s", path[0] ? path : "(root)"); + for (;;) { + j_ws(p); + char *k = j_string(p); + size_t kl = strlen(k); + if (kl < 1 || kl > 40) gc_fail("json: key length at %s", path); + for (size_t i = 0; i < kl; i++) + if (!((k[i] >= 'a' && k[i] <= 'z') || j_isdig(k[i]) || k[i] == '_')) gc_fail("json: key \"%s\" is not [a-z0-9_]", k); + for (int i = 0; i < nk; i++) + if (!strcmp(keys[i], k)) gc_fail("json: duplicate key \"%s\" in %s", k, path[0] ? path : "(root)"); + if (nk == 128) gc_fail("json: too many keys in %s", path); + keys[nk++] = k; + j_ws(p); + if (p->i >= p->n || p->s[p->i] != ':') gc_fail("json: expected ':' after \"%s\"", k); + p->i++; + char cp[GC_PATH], cn[GC_PATH]; + if (snprintf(cp, sizeof cp, "%s%s%s", path, path[0] ? "." : "", k) >= (int)sizeof cp || + snprintf(cn, sizeof cn, "%s%s%s", norm, norm[0] ? "." : "", k) >= (int)sizeof cn) gc_fail("json: path too long"); + j_value(p, cp, cn, depth + 1); + j_ws(p); + if (p->i < p->n && p->s[p->i] == ',') { p->i++; continue; } + if (p->i < p->n && p->s[p->i] == '}') { p->i++; break; } + gc_fail("json: expected ',' or '}' in %s", path[0] ? path : "(root)"); + } + for (int i = 0; i < nk; i++) free(keys[i]); +} + +static void j_array(Jp *p, const char *path, const char *norm, int depth) +{ + j_add(path, norm, 'a', NULL); + p->i++; + j_ws(p); + if (p->i < p->n && p->s[p->i] == ']') gc_fail("json: empty array at %s", path); + char cn[GC_PATH]; + if (snprintf(cn, sizeof cn, "%s[]", norm) >= (int)sizeof cn) gc_fail("json: path too long"); + for (int idx = 0;; idx++) { + char cp[GC_PATH]; + if (snprintf(cp, sizeof cp, "%s[%d]", path, idx) >= (int)sizeof cp) gc_fail("json: path too long"); + j_ws(p); + j_value(p, cp, cn, depth + 1); + j_ws(p); + if (p->i < p->n && p->s[p->i] == ',') { p->i++; continue; } + if (p->i < p->n && p->s[p->i] == ']') { p->i++; break; } + gc_fail("json: expected ',' or ']' in %s", path); + } +} + +static void j_value(Jp *p, const char *path, const char *norm, int depth) +{ + if (depth > GC_DEPTH) gc_fail("json: nesting deeper than %d", GC_DEPTH); + j_ws(p); + if (p->i >= p->n) gc_fail("json: unexpected end"); + char c = p->s[p->i]; + if (c == '{') { j_object(p, path, norm, depth); return; } + if (c == '[') { j_array(p, path, norm, depth); return; } + if (c == '"') { j_add(path, norm, 's', j_string(p)); return; } + static const char *const lit[3] = {"true", "false", "null"}; + for (int i = 0; i < 3; i++) { + size_t l = strlen(lit[i]); + if (p->n - p->i >= l && !memcmp(p->s + p->i, lit[i], l)) { + p->i += l; + j_add(path, norm, i < 2 ? 'b' : 'z', strdup(lit[i])); + return; + } + } + /* number: -?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)? */ + size_t st = p->i; + int isint = 1; + if (p->s[p->i] == '-') p->i++; + if (p->i < p->n && p->s[p->i] == '0') p->i++; + else if (p->i < p->n && p->s[p->i] >= '1' && p->s[p->i] <= '9') while (p->i < p->n && j_isdig(p->s[p->i])) p->i++; + else gc_fail("json: bad value at %s", path); + if (p->i < p->n && p->s[p->i] == '.') { + isint = 0; p->i++; + if (p->i >= p->n || !j_isdig(p->s[p->i])) gc_fail("json: bad number at %s", path); + while (p->i < p->n && j_isdig(p->s[p->i])) p->i++; + } + if (p->i < p->n && (p->s[p->i] == 'e' || p->s[p->i] == 'E')) { + isint = 0; p->i++; + if (p->i < p->n && (p->s[p->i] == '+' || p->s[p->i] == '-')) p->i++; + if (p->i >= p->n || !j_isdig(p->s[p->i])) gc_fail("json: bad number at %s", path); + while (p->i < p->n && j_isdig(p->s[p->i])) p->i++; + } + char *raw = strndup(p->s + st, p->i - st); + j_add(path, norm, isint ? 'i' : 'n', raw); +} + +/* ---------- schema omega.gen.protocol.v1 ---------- */ + +typedef struct { const char *norm, *types; } Key; + +static const Key schema[] = { + {"", "o"}, + {"schema", "s"}, {"revision", "i"}, {"issue", "s"}, {"doc", "s"}, {"question", "s"}, + {"config", "o"}, {"config.name", "s"}, {"config.params", "s"}, {"config.masking", "b"}, + {"config.harness_kind", "s"}, {"config.writer", "s"}, {"config.reader", "s"}, + {"conditions", "a"}, {"conditions[]", "o"}, {"conditions[].name", "s"}, + {"conditions[].prefix_examples", "i"}, {"conditions[].retrieval", "s"}, + {"retrieval", "o"}, {"retrieval.contract", "s"}, {"retrieval.record_schema", "s"}, {"retrieval.k", "i"}, + {"retrieval.cap_bytes", "i"}, {"retrieval.mode", "s"}, {"retrieval.query", "s"}, + {"retrieval.c_pool_excludes_prefix", "b"}, {"retrieval.length_match_abs_bytes", "i"}, + {"retrieval.length_match_frac", "n"}, {"retrieval.length_match_min_matched_frac", "n"}, + {"generation", "o"}, {"generation.primary", "s"}, {"generation.max_tokens", "i"}, + {"generation.max_rounds", "i"}, {"generation.attempts", "i"}, {"generation.attempt1_temperature", "i"}, + {"generation.sampled_temperature", "n"}, {"generation.sampled_top_p", "n"}, {"generation.seed_base", "i"}, + {"generation.seed_attempt_stride", "i"}, {"generation.retries", "i"}, {"generation.retry_pause_s", "i"}, + {"generation.order", "s"}, {"generation.sets", "a"}, {"generation.sets[]", "s"}, + {"gates", "o"}, {"gates.greedy", "o"}, {"gates.greedy.tasks", "s"}, {"gates.greedy.condition", "s"}, + {"gates.greedy.repeats", "i"}, {"gates.g0", "o"}, {"gates.g0.script", "s"}, {"gates.g0.task", "s"}, + {"gates.g0.condition", "s"}, {"gates.g0.seeds", "i"}, {"gates.g0.repeat", "i"}, {"gates.g0.otherwise", "s"}, + {"gates.g0.seed_values", "a"}, {"gates.g0.seed_values[]", "i"}, {"generation.in_flight", "i"}, + {"decision", "o"}, {"decision.schema", "s"}, {"decision.primary", "s"}, {"decision.controls", "a"}, + {"decision.controls[]", "s"}, {"decision.delta", "n"}, {"decision.alpha", "n"}, {"decision.test", "s"}, + {"decision.ci", "s"}, {"decision.z95", "n"}, {"decision.z90", "n"}, {"decision.fail_control", "s"}, + {"decision.sanity_ab_max", "i"}, {"decision.verdicts", "a"}, {"decision.verdicts[]", "s"}, + {"decision.vectors", "s"}, {"decision.floor_max", "i"}, {"decision.ceiling_min", "i"}, + {"splits", "o"}, + {"splits.dev", "o"}, {"splits.dev.count", "i"}, {"splits.dev.meta", "s"}, {"splits.dev.meta_sha256", "s"}, + {"splits.dev.cases", "s"}, {"splits.dev.pack_sha256", "s"}, {"splits.dev.tasks", "a"}, + {"splits.dev.tasks[]", "s"}, {"splits.dev.families", "a"}, {"splits.dev.families[]", "s"}, + {"splits.corpus", "o"}, {"splits.corpus.count_target", "i"}, {"splits.corpus.meta", "s"}, + {"splits.corpus.meta_sha256", "sz"}, + {"splits.heldout", "o"}, {"splits.heldout.count", "i"}, {"splits.heldout.min_families", "i"}, + {"splits.heldout.pub_sha256", "sz"}, {"splits.heldout.tasks_sha256", "sz"}, + {"splits.heldout.near_dup_threshold", "n"}, + {"record_sets", "o"}, {"record_sets.prefix_ids", "az"}, {"record_sets.prefix_ids[]", "s"}, + {"record_sets.prefix_sha256", "sz"}, {"record_sets.raw_manifest_sha256", "sz"}, + {"record_sets.abstraction_manifest_sha256", "sz"}, {"record_sets.raw_manifest", "sz"}, + {"record_sets.abstraction_manifest", "sz"}, + {"pins", "a"}, {"pins[]", "o"}, {"pins[].path", "s"}, {"pins[].sha256", "s"}, + {"interfaces", "a"}, {"interfaces[]", "o"}, {"interfaces[].name", "s"}, {"interfaces[].path", "s"}, + {"interfaces[].commit", "s"}, {"interfaces[].sha256", "s"}, +}; +#define NSCHEMA ((int)(sizeof schema / sizeof schema[0])) + +/* frozen v1 values: any other value is a change that needs a new revision (protocol section 15) */ +typedef struct { const char *path, *raw; } Fixed; +static const Fixed fixed[] = { + {"schema", "omega.gen.protocol.v1"}, {"revision", "1"}, {"issue", "aien-spark/omega#476"}, + {"doc", "docs/one_layer/GEN_PROTOCOL_V1.md"}, + {"config.name", "A"}, {"config.params", "tools/exp445/PARAMS"}, {"config.masking", "false"}, + {"config.harness_kind", "oscir"}, {"config.writer", "omegese"}, {"config.reader", "tools/exp445/reader.sh"}, + {"conditions[0].name", "A"}, {"conditions[0].prefix_examples", "1"}, {"conditions[0].retrieval", "none"}, + {"conditions[1].name", "B"}, {"conditions[1].prefix_examples", "14"}, {"conditions[1].retrieval", "none"}, + {"conditions[2].name", "C"}, {"conditions[2].prefix_examples", "14"}, {"conditions[2].retrieval", "raw"}, + {"conditions[3].name", "D"}, {"conditions[3].prefix_examples", "14"}, {"conditions[3].retrieval", "abstraction"}, + {"retrieval.contract", "genret-cli-v3"}, {"retrieval.record_schema", "gen-record-1"}, {"retrieval.k", "3"}, + {"retrieval.cap_bytes", "4096"}, {"retrieval.mode", "normal"}, {"retrieval.query", "requirement+prompt"}, + {"retrieval.c_pool_excludes_prefix", "true"}, {"retrieval.length_match_abs_bytes", "256"}, + {"retrieval.length_match_frac", "0.15"}, {"retrieval.length_match_min_matched_frac", "0.5"}, {"generation.in_flight", "1"}, + {"generation.primary", "attempt1_round1_functional"}, {"generation.max_tokens", "3072"}, + {"generation.max_rounds", "2"}, {"generation.attempts", "3"}, {"generation.attempt1_temperature", "0"}, + {"generation.sampled_temperature", "0.7"}, {"generation.sampled_top_p", "0.8"}, + {"generation.seed_base", "476000"}, {"generation.seed_attempt_stride", "1000"}, {"generation.retries", "2"}, + {"generation.retry_pause_s", "5"}, {"generation.order", "attempt,task,condition"}, + {"generation.sets[0]", "-"}, {"generation.sets[1]", "--final"}, + {"gates.greedy.tasks", "dev"}, {"gates.greedy.condition", "B"}, {"gates.greedy.repeats", "2"}, + {"gates.g0.script", "tools/exp445/sampling-gate.sh"}, {"gates.g0.task", "devgen01"}, {"gates.g0.condition", "B"}, + {"gates.g0.seeds", "8"}, {"gates.g0.repeat", "1"}, {"gates.g0.otherwise", "UNAVAILABLE"}, + {"gates.g0.seed_values[0]", "476901"}, {"gates.g0.seed_values[1]", "476902"}, {"gates.g0.seed_values[2]", "476903"}, + {"gates.g0.seed_values[3]", "476904"}, {"gates.g0.seed_values[4]", "476905"}, {"gates.g0.seed_values[5]", "476906"}, + {"gates.g0.seed_values[6]", "476907"}, {"gates.g0.seed_values[7]", "476908"}, + {"decision.schema", "omega.gen.decision.v1"}, {"decision.primary", "attempt1_round1_functional"}, + {"decision.controls[0]", "A"}, {"decision.controls[1]", "B"}, {"decision.controls[2]", "C"}, + {"decision.delta", "0.10"}, {"decision.alpha", "0.05"}, {"decision.test", "mcnemar_exact_one_sided"}, + {"decision.ci", "newcombe10"}, {"decision.z95", "1.959963984540054"}, {"decision.z90", "1.644853626951472"}, + {"decision.fail_control", "C"}, {"decision.sanity_ab_max", "2"}, + {"decision.verdicts[0]", "INVALID"}, {"decision.verdicts[1]", "PASS"}, {"decision.verdicts[2]", "FAIL"}, + {"decision.verdicts[3]", "INCONCLUSIVE"}, + {"decision.vectors", "tests/one_layer/generalization/stats_vectors.tsv"}, {"decision.floor_max", "3"}, + {"decision.ceiling_min", "57"}, + {"splits.dev.count", "10"}, {"splits.dev.meta", "tests/one_layer/generalization/dev/META"}, + {"splits.dev.cases", "tests/one_layer/generalization/dev/tasks/cases"}, + {"splits.corpus.count_target", "48"}, {"splits.corpus.meta", "tests/one_layer/generalization/corpus/META"}, + {"splits.heldout.count", "60"}, {"splits.heldout.min_families", "6"}, {"splits.heldout.near_dup_threshold", "0.5"}, +}; +#define NFIXED ((int)(sizeof fixed / sizeof fixed[0])) + +/* exact array lengths */ +static const struct { const char *path; int len; } fixed_len[] = { + {"conditions", 4}, {"generation.sets", 2}, {"decision.controls", 3}, {"decision.verdicts", 4}, + {"gates.g0.seed_values", 8}, +}; + +/* files that must be pinned (protocol section 13); more may be pinned */ +static const char *const must_pin[] = { + "docs/one_layer/GEN_PROTOCOL_V1.md", + "docs/one_layer/OMEGESE.md", + "tests/one_layer/acceptance/HARNESS_INTERFACE_V2.md", + "tests/one_layer/generalization/dev/META", + "tests/one_layer/generalization/dev/gen_expected.c", + "tests/one_layer/generalization/dev/pack_dev.sh", + "tests/one_layer/generalization/families/FAMSIG_V1.md", + "tests/one_layer/generalization/stats_vectors.tsv", + "tools/exp445/PARAMS", + "tools/exp445/arms/common.md", + "tools/exp445/arms/omegese.md", + "tools/exp445/reader.sh", + "tools/exp445/sampling-gate.sh", + "tools/exp445/weights.A.sha256", + "tools/gen/protocol/famsig.c", + "tools/gen/protocol/famsig_lib.h", + "tools/gen/protocol/gencheck.c", + "tools/gen/protocol/genjac.c", + "tools/gen/protocol/genstats.c", +}; + +static Ent *get(const char *path) +{ + for (int i = 0; i < nent; i++) + if (!strcmp(ent[i].path, path)) return &ent[i]; + return NULL; +} + +static const char *str(const char *path) +{ + Ent *e = get(path); + if (!e || e->type != 's') gc_fail("%s: missing or not a string", path); + return e->raw; +} + +static int count_elems(const char *path) +{ + char pre[GC_PATH + 2]; + snprintf(pre, sizeof pre, "%s[", path); + size_t pl = strlen(pre); + int n = 0; + for (int i = 0; i < nent; i++) + if (!strncmp(ent[i].path, pre, pl) && strchr(ent[i].path + pl, ']') && !strchr(strchr(ent[i].path + pl, ']') + 1, '.') && + !strchr(strchr(ent[i].path + pl, ']') + 1, '[')) + n++; + return n; +} + +static int hex64(const char *s) { return fs_hex64_ok(s); } + +static int safe_rel(const char *p) +{ + size_t n = strlen(p); + if (n < 1 || n > 160 || p[0] == '/') return 0; + const char *c = p; + while (*c) { + const char *e = strchr(c, '/'); + size_t l = e ? (size_t)(e - c) : strlen(c); + if (l == 0 || (l == 1 && c[0] == '.') || (l == 2 && c[0] == '.' && c[1] == '.')) return 0; + for (size_t i = 0; i < l; i++) { + char ch = c[i]; + if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || j_isdig(ch) || ch == '.' || ch == '_' || ch == '-')) return 0; + } + c += l; + if (*c == '/') c++; + } + return 1; +} + +static const char *root; +static int strict; /* run-ready and open modes: nothing may be pending */ + +static int file_sha(const char *rel, char out[65]) +{ + char full[4096]; + snprintf(full, sizeof full, "%s/%s", root, rel); + struct stat st; + if (stat(full, &st) || !S_ISREG(st.st_mode)) return -1; + size_t len; + char *b = fs_slurp(full, &len); + if (!b) return -1; + unsigned char d[32]; + sha256_hash((const unsigned char *)b, len, d); + fs_hex(d, out); + free(b); + return 0; +} + +static int cmp_cstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } + +static void read_meta(const char *rel, FsSet *set, int want_split) +{ + char full[4096], err[512]; + snprintf(full, sizeof full, "%s/%s", root, rel); + if (fs_read(full, set, err, sizeof err)) gc_fail("%s", err); + if (set->is_split) gc_fail("%s: a gen-split file where gen-meta was expected", rel); + for (int i = 0; i < set->n; i++) + if (set->m[i].split != want_split) gc_fail("%s: task %s has split %s, expected %s", rel, set->m[i].id, fs_split[set->m[i].split], fs_split[want_split]); +} + +static int split_index(const char *s) +{ + for (int i = 0; i < FS_NSPLIT; i++) + if (!strcmp(fs_split[i], s)) return i; + return -1; +} + +/* ---------- manifest ---------- */ + +static FsSet dev, corpus; +static int corpus_frozen; + +static void check_manifest(const char *json_path) +{ + size_t len; + char *b = fs_slurp(json_path, &len); + if (!b) gc_fail("cannot read %s", json_path); + if (strlen(b) != len) gc_fail("json: NUL byte"); + for (size_t i = 0; i < len; i++) + if (b[i] != '\n' && b[i] != '\t' && ((unsigned char)b[i] < 0x20 || (unsigned char)b[i] > 0x7e)) gc_fail("json: byte 0x%02x at %zu (ASCII only)", (unsigned char)b[i], i); + Jp p = {b, 0, len}; + j_ws(&p); + if (p.i >= p.n || b[p.i] != '{') gc_fail("json: top level must be an object"); + j_value(&p, "", "", 0); + j_ws(&p); + if (p.i != p.n) gc_fail("json: trailing bytes after the top-level object"); + + /* every value is a known key of the right type */ + for (int i = 0; i < nent; i++) { + int k; + for (k = 0; k < NSCHEMA; k++) + if (!strcmp(schema[k].norm, ent[i].norm)) break; + if (k == NSCHEMA) gc_fail("unknown key %s", ent[i].path); + if (!strchr(schema[k].types, ent[i].type)) gc_fail("%s: type %c, expected %s", ent[i].path, ent[i].type, schema[k].types); + } + /* every object has every key its schema lists */ + for (int i = 0; i < nent; i++) { + if (ent[i].type != 'o') continue; + size_t nl = strlen(ent[i].norm); + for (int k = 0; k < NSCHEMA; k++) { + const char *kn = schema[k].norm; + const char *rest; + if (nl == 0) rest = kn; + else if (!strncmp(kn, ent[i].norm, nl) && kn[nl] == '.') rest = kn + nl + 1; + else continue; + if (!*rest || strchr(rest, '.') || strchr(rest, '[')) continue; + char want[GC_PATH * 2]; + if (snprintf(want, sizeof want, "%s%s%.*s", ent[i].path, nl ? "." : "", GC_PATH, rest) >= (int)sizeof want) gc_fail("json: path too long"); + if (!get(want)) gc_fail("missing key %s", want); + } + } + printf("ok schema omega.gen.protocol.v1: %d values, no unknown, missing or duplicate key\n", nent); + + for (int i = 0; i < NFIXED; i++) { + Ent *e = get(fixed[i].path); + if (!e) gc_fail("missing frozen value %s", fixed[i].path); + if (!e->raw || strcmp(e->raw, fixed[i].raw)) gc_fail("%s changed: \"%s\", frozen v1 value \"%s\" (a change is a new revision)", fixed[i].path, e->raw ? e->raw : "(container)", fixed[i].raw); + } + for (size_t i = 0; i < sizeof fixed_len / sizeof fixed_len[0]; i++) + if (count_elems(fixed_len[i].path) != fixed_len[i].len) gc_fail("%s: %d elements, frozen %d", fixed_len[i].path, count_elems(fixed_len[i].path), fixed_len[i].len); + printf("ok %d frozen v1 values\n", NFIXED); + + /* pins: ascending unique safe paths, each file present with that sha256 */ + int np = count_elems("pins"); + char prev[GC_PATH] = ""; + for (int i = 0; i < np; i++) { + char kp[64], ks[64], sha[65]; + snprintf(kp, sizeof kp, "pins[%d].path", i); + snprintf(ks, sizeof ks, "pins[%d].sha256", i); + const char *path = str(kp), *want = str(ks); + if (!safe_rel(path)) gc_fail("%s: unsafe path \"%s\"", kp, path); + if (i && strcmp(prev, path) >= 0) gc_fail("pins: \"%s\" not strictly ascending (duplicate or out of order)", path); + snprintf(prev, sizeof prev, "%s", path); + if (!hex64(want)) gc_fail("%s: not 64 lowercase hex", ks); + if (file_sha(path, sha)) gc_fail("pinned file missing: %s", path); + if (strcmp(sha, want)) gc_fail("digest drift: %s is %s, pinned %s", path, sha, want); + } + for (size_t k = 0; k < sizeof must_pin / sizeof must_pin[0]; k++) { + int found = 0; + for (int i = 0; i < np && !found; i++) { + char kp[64]; + snprintf(kp, sizeof kp, "pins[%d].path", i); + found = !strcmp(str(kp), must_pin[k]); + } + if (!found) gc_fail("pins: required file %s is not pinned", must_pin[k]); + } + printf("ok %d pinned files re-hashed, no drift\n", np); + + /* interfaces of other GEN parts: verified when the file is present in this tree */ + int ni = count_elems("interfaces"); + for (int i = 0; i < ni; i++) { + char k[64], sha[65]; + snprintf(k, sizeof k, "interfaces[%d].name", i); + const char *name = str(k); + for (int j = 0; j < i; j++) { + char k2[64]; + snprintf(k2, sizeof k2, "interfaces[%d].name", j); + if (!strcmp(str(k2), name)) gc_fail("interfaces: duplicate name %s", name); + } + snprintf(k, sizeof k, "interfaces[%d].path", i); + const char *path = str(k); + if (!safe_rel(path)) gc_fail("%s: unsafe path", k); + snprintf(k, sizeof k, "interfaces[%d].commit", i); + const char *commit = str(k); + if (strlen(commit) != 40 || strspn(commit, "0123456789abcdef") != 40) gc_fail("%s: not a 40-hex commit", k); + snprintf(k, sizeof k, "interfaces[%d].sha256", i); + const char *want = str(k); + if (!hex64(want)) gc_fail("%s: not 64 lowercase hex", k); + if (file_sha(path, sha)) { + if (strict) gc_fail("interface %s: %s is not in this tree (every interface must be present at seal-open)", name, path); + printf("ok interface %s: %s not in this tree (pending merge), pinned %.16s...\n", name, path, want); + continue; + } + if (strcmp(sha, want)) gc_fail("digest drift: interface %s file %s is %s, pinned %s", name, path, sha, want); + printf("ok interface %s: %s matches\n", name, path); + } + + /* dev split */ + const char *devmeta = str("splits.dev.meta"); + char sha[65]; + if (file_sha(devmeta, sha)) gc_fail("dev META missing: %s", devmeta); + const char *dms = str("splits.dev.meta_sha256"); + if (!hex64(dms)) gc_fail("splits.dev.meta_sha256: not 64 lowercase hex"); + if (strcmp(sha, dms)) gc_fail("digest drift: %s is %s, splits.dev.meta_sha256 %s", devmeta, sha, dms); + if (!hex64(str("splits.dev.pack_sha256"))) gc_fail("splits.dev.pack_sha256: not 64 lowercase hex"); + read_meta(devmeta, &dev, split_index("dev")); + int nt = count_elems("splits.dev.tasks"); + if (nt != 10) gc_fail("splits.dev.tasks: %d tasks, frozen 10", nt); + for (int i = 0; i < nt; i++) { + char k[64]; + snprintf(k, sizeof k, "splits.dev.tasks[%d]", i); + const char *id = str(k); + if (!fs_task_id_ok(id)) gc_fail("%s: bad task id", k); + if (i) { + char k2[64]; + snprintf(k2, sizeof k2, "splits.dev.tasks[%d]", i - 1); + if (strcmp(str(k2), id) >= 0) gc_fail("splits.dev.tasks: duplicate or unsorted task id %s", id); + } + } + /* META ids, sorted */ + if (dev.n != nt) gc_fail("dev META has %d tasks, splits.dev.tasks %d", dev.n, nt); + char **mids = malloc(sizeof(char *) * (size_t)dev.n); + for (int i = 0; i < dev.n; i++) mids[i] = dev.m[i].id; + qsort(mids, (size_t)dev.n, sizeof *mids, cmp_cstr); + for (int i = 0; i < nt; i++) { + char k[64]; + snprintf(k, sizeof k, "splits.dev.tasks[%d]", i); + if (strcmp(mids[i], str(k))) gc_fail("dev META task ids differ from splits.dev.tasks (%s vs %s)", mids[i], str(k)); + if (i && !strcmp(mids[i], mids[i - 1])) gc_fail("dev META: duplicate task id %s", mids[i]); + } + free(mids); + /* case directories */ + char full[4096]; + snprintf(full, sizeof full, "%s/%s", root, str("splits.dev.cases")); + DIR *d = opendir(full); + if (!d) gc_fail("cannot open %s", str("splits.dev.cases")); + char *cases[256]; + int nc = 0; + struct dirent *de; + while ((de = readdir(d))) { + if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue; + if (nc == 256) gc_fail("too many case directories"); + cases[nc++] = strdup(de->d_name); + } + closedir(d); + qsort(cases, (size_t)nc, sizeof *cases, cmp_cstr); + if (nc != nt) gc_fail("dev cases: %d directories, splits.dev.tasks %d", nc, nt); + for (int i = 0; i < nc; i++) { + char k[64]; + snprintf(k, sizeof k, "splits.dev.tasks[%d]", i); + if (strcmp(cases[i], str(k))) gc_fail("dev case directory %s does not match splits.dev.tasks (%s)", cases[i], str(k)); + free(cases[i]); + } + /* families = distinct META signatures, ascending */ + char *sigs[64]; + int ns = 0; + for (int i = 0; i < dev.n; i++) { + int seen = 0; + for (int j = 0; j < ns; j++) seen |= !strcmp(sigs[j], dev.m[i].sig); + if (!seen) { if (ns == 64) gc_fail("too many dev families"); sigs[ns++] = dev.m[i].sig; } + } + qsort(sigs, (size_t)ns, sizeof *sigs, cmp_cstr); + int nf = count_elems("splits.dev.families"); + if (nf != ns) gc_fail("splits.dev.families: %d listed, dev META has %d families", nf, ns); + for (int i = 0; i < nf; i++) { + char k[64]; + snprintf(k, sizeof k, "splits.dev.families[%d]", i); + if (strcmp(str(k), sigs[i])) gc_fail("splits.dev.families[%d] is %s, dev META gives %s", i, str(k), sigs[i]); + } + printf("ok dev split: %d tasks = META ids = case directories, %d families = META signatures\n", nt, nf); + for (int i = 0; i < nf; i++) { char tok[24]; fs_token(sigs[i], tok); printf("ok dev family %s %s\n", tok, sigs[i]); } + + /* corpus split: frozen once meta_sha256 is set */ + Ent *cm = get("splits.corpus.meta_sha256"); + if (cm->type == 'z') { + corpus_frozen = 0; + const char *pre[] = {"record_sets.prefix_ids", "record_sets.prefix_sha256", "record_sets.raw_manifest_sha256", + "record_sets.abstraction_manifest_sha256", "record_sets.raw_manifest", "record_sets.abstraction_manifest"}; + for (int i = 0; i < 6; i++) + if (get(pre[i])->type != 'z') gc_fail("%s is set while the corpus META is not frozen", pre[i]); + FsSet both = {0}; + char err[512]; + snprintf(full, sizeof full, "%s/%s", root, devmeta); + if (fs_read(full, &both, err, sizeof err)) gc_fail("%s", err); + if (fs_rule(&both, stdout, 0)) gc_fail("disjointness rule fails on the dev split"); + printf("ok corpus split: pending (splits.corpus.meta_sha256 null until GEN-3 freezes it)\n"); + } else { + corpus_frozen = 1; + const char *cmeta = str("splits.corpus.meta"); + if (!hex64(cm->raw)) gc_fail("splits.corpus.meta_sha256: not 64 lowercase hex"); + if (file_sha(cmeta, sha)) gc_fail("corpus META missing: %s", cmeta); + if (strcmp(sha, cm->raw)) gc_fail("digest drift: %s is %s, splits.corpus.meta_sha256 %s", cmeta, sha, cm->raw); + read_meta(cmeta, &corpus, split_index("corpus")); + FsSet both = {0}; + char err[512]; + snprintf(full, sizeof full, "%s/%s", root, devmeta); + if (fs_read(full, &both, err, sizeof err)) gc_fail("%s", err); + snprintf(full, sizeof full, "%s/%s", root, cmeta); + if (fs_read(full, &both, err, sizeof err)) gc_fail("%s", err); + if (fs_rule(&both, stdout, 0)) gc_fail("disjointness rule (R1..R8) fails on dev + corpus"); + printf("ok corpus split: %d tasks, dev + corpus pass R1..R8\n", corpus.n); + Ent *pi = get("record_sets.prefix_ids"); + if (pi->type == 'a') { + int n = count_elems("record_sets.prefix_ids"); + if (n != 14) gc_fail("record_sets.prefix_ids: %d ids, frozen 14", n); + for (int i = 0; i < n; i++) { + char k[64]; + snprintf(k, sizeof k, "record_sets.prefix_ids[%d]", i); + const char *id = str(k); + int in = 0; + for (int j = 0; j < corpus.n; j++) in |= !strcmp(corpus.m[j].id, id); + if (!in) gc_fail("%s: %s is not a corpus task", k, id); + for (int j = 0; j < i; j++) { + char k2[64]; + snprintf(k2, sizeof k2, "record_sets.prefix_ids[%d]", j); + if (!strcmp(str(k2), id)) gc_fail("record_sets.prefix_ids: duplicate %s", id); + } + } + } + } + const char *opt[] = {"splits.heldout.pub_sha256", "splits.heldout.tasks_sha256", "record_sets.prefix_sha256", + "record_sets.raw_manifest_sha256", "record_sets.abstraction_manifest_sha256"}; + for (int i = 0; i < 5; i++) { + Ent *e = get(opt[i]); + if (e->type == 's' && !hex64(e->raw)) gc_fail("%s: not 64 lowercase hex", opt[i]); + } + /* C and D record-set manifests: path and digest both null (not frozen) or both set and re-hashed */ + static const char *const rs[2][2] = {{"record_sets.raw_manifest", "record_sets.raw_manifest_sha256"}, + {"record_sets.abstraction_manifest", "record_sets.abstraction_manifest_sha256"}}; + for (int i = 0; i < 2; i++) { + Ent *p = get(rs[i][0]), *h = get(rs[i][1]); + if ((p->type == 'z') != (h->type == 'z')) gc_fail("%s and %s must both be null or both be set", rs[i][0], rs[i][1]); + if (p->type == 'z') continue; + if (!safe_rel(p->raw)) gc_fail("%s: unsafe path", rs[i][0]); + if (file_sha(p->raw, sha)) gc_fail("record-set manifest missing: %s", p->raw); + if (strcmp(sha, h->raw)) gc_fail("digest drift: record-set manifest %s is %s, %s %s", p->raw, sha, rs[i][1], h->raw); + printf("ok record set %s re-hashed\n", p->raw); + } +} + +/* ---------- split (reviewer, after the seal) ---------- */ + +static void check_split(const char *split_path, const char *pub_path) +{ + if (!corpus_frozen) gc_fail("split check needs the frozen corpus META (splits.corpus.meta_sha256 is null)"); + if (get("splits.heldout.pub_sha256")->type != 's' || get("splits.heldout.tasks_sha256")->type != 's') + gc_fail("split check needs splits.heldout.pub_sha256 and tasks_sha256 anchored in protocol.json (fail closed on null)"); + FsSet hs = {0}; + char err[512]; + if (fs_read(split_path, &hs, err, sizeof err)) gc_fail("%s", err); + if (!hs.is_split || hs.n == 0) gc_fail("%s: not a gen-split file with task lines", split_path); + int nfam; + char *fl = fs_famlist(&hs, &nfam); + unsigned char dg[32]; + char famsha[65]; + sha256_hash((const unsigned char *)fl, strlen(fl), dg); + fs_hex(dg, famsha); + free(fl); + + size_t len; + char *b = fs_slurp(pub_path, &len); + if (!b) gc_fail("cannot read %s", pub_path); + if (strlen(b) != len || len == 0 || b[len - 1] != '\n') gc_fail("HELDOUT.pub: must be text ending in a newline"); + for (size_t i = 0; i < len; i++) + if (b[i] != '\n' && ((unsigned char)b[i] < 0x20 || (unsigned char)b[i] > 0x7e)) gc_fail("HELDOUT.pub: byte 0x%02x", (unsigned char)b[i]); + unsigned char pd[32]; + char pubsha[65]; + sha256_hash((const unsigned char *)b, len, pd); + fs_hex(pd, pubsha); + char *lines[4096]; + int nl = 0; + for (char *s = b; *s;) { + char *e = strchr(s, '\n'); + *e = 0; + if (nl == 4096) gc_fail("HELDOUT.pub: too many lines"); + lines[nl++] = s; + s = e + 1; + } + static const char *const head[] = {"tasks_sha256 ", "split_sha256 ", "famlist_sha256 "}; + if (nl < 6 || strcmp(lines[0], "gen-heldout-pub 1")) gc_fail("HELDOUT.pub: line 1 must be \"gen-heldout-pub 1\""); + for (int i = 0; i < 3; i++) + if (strncmp(lines[1 + i], head[i], strlen(head[i])) || !hex64(lines[1 + i] + strlen(head[i]))) + gc_fail("HELDOUT.pub: line %d must be \"%s<64 hex>\"", 2 + i, head[i]); + const char *p_tasks = lines[1] + 13, *p_split = lines[2] + 13, *p_fam = lines[3] + 15; + int n_tasks, n_fams; + char tail; + if (sscanf(lines[4], "tasks %d%c", &n_tasks, &tail) != 1 || n_tasks < 1) gc_fail("HELDOUT.pub: line 5 must be \"tasks \""); + if (sscanf(lines[5], "families %d%c", &n_fams, &tail) != 1 || n_fams < 1) gc_fail("HELDOUT.pub: line 6 must be \"families \""); + char chk[64]; + snprintf(chk, sizeof chk, "tasks %d", n_tasks); + if (strcmp(chk, lines[4])) gc_fail("HELDOUT.pub: line 5 not canonical"); + snprintf(chk, sizeof chk, "families %d", n_fams); + if (strcmp(chk, lines[5])) gc_fail("HELDOUT.pub: line 6 not canonical"); + if (nl != 6 + n_tasks) gc_fail("HELDOUT.pub: %d task lines, tasks %d", nl - 6, n_tasks); + for (int i = 0; i < n_tasks; i++) { + const char *l = lines[6 + i]; + if (strncmp(l, "task ", 5) || !fs_task_id_ok(l + 5)) gc_fail("HELDOUT.pub: line %d must be \"task \"", 7 + i); + if (i && strcmp(lines[5 + i] + 5, l + 5) >= 0) gc_fail("HELDOUT.pub: task ids not strictly ascending at %s", l + 5); + } + + if (strcmp(p_split, hs.split_sha256)) gc_fail("split_sha256 mismatch: SPLIT is %s, HELDOUT.pub says %s", hs.split_sha256, p_split); + if (strcmp(p_fam, famsha)) gc_fail("famlist_sha256 mismatch: SPLIT gives %s, HELDOUT.pub says %s", famsha, p_fam); + if (n_tasks != hs.n) gc_fail("HELDOUT.pub tasks %d, SPLIT has %d", n_tasks, hs.n); + if (n_fams != nfam) gc_fail("HELDOUT.pub families %d, SPLIT has %d", n_fams, nfam); + for (int i = 0; i < hs.n; i++) + if (strcmp(hs.m[i].id, lines[6 + i] + 5)) gc_fail("HELDOUT.pub task %s differs from SPLIT task %s", lines[6 + i] + 5, hs.m[i].id); + int want_n = atoi(get("splits.heldout.count")->raw), want_f = atoi(get("splits.heldout.min_families")->raw); + if (hs.n != want_n) gc_fail("held-out split has %d tasks, protocol requires %d", hs.n, want_n); + if (nfam < want_f) gc_fail("held-out split has %d families, protocol requires at least %d", nfam, want_f); + Ent *e = get("splits.heldout.pub_sha256"); + if (strcmp(e->raw, pubsha)) gc_fail("digest drift: HELDOUT.pub is %s, splits.heldout.pub_sha256 %s", pubsha, e->raw); + e = get("splits.heldout.tasks_sha256"); + if (strcmp(e->raw, p_tasks)) gc_fail("tasks_sha256 mismatch: HELDOUT.pub %s, protocol %s", p_tasks, e->raw); + printf("ok HELDOUT.pub matches SPLIT: %d tasks, %d families, split and famlist digests\n", hs.n, nfam); + + FsSet all = {0}; + char full[4096]; + snprintf(full, sizeof full, "%s/%s", root, str("splits.dev.meta")); + if (fs_read(full, &all, err, sizeof err)) gc_fail("%s", err); + snprintf(full, sizeof full, "%s/%s", root, str("splits.corpus.meta")); + if (fs_read(full, &all, err, sizeof err)) gc_fail("%s", err); + if (fs_read(split_path, &all, err, sizeof err)) gc_fail("%s", err); + if (fs_rule(&all, stdout, 0)) gc_fail("disjointness rule (R1..R8) fails on dev + corpus + SPLIT"); + printf("ok dev + corpus + SPLIT pass R1..R8\n"); +} + + +/* ---------- open (custodian, seal-open step; reducer, before reducing) ---------- */ + +static void check_frozen(void) +{ + for (int i = 0; i < nent; i++) + if (ent[i].type == 'z') gc_fail("not run-ready: %s is null", ent[i].path); + int found = 0, ni = count_elems("interfaces"); + for (int i = 0; i < ni; i++) { + char k[64]; + snprintf(k, sizeof k, "interfaces[%d].name", i); + found |= !strcmp(str(k), str("retrieval.contract")); + } + if (!found) gc_fail("interfaces: the retrieval contract %s is not pinned", str("retrieval.contract")); + + printf("ok run-ready: no null field, every interface present and matching, %s pinned\n", str("retrieval.contract")); +} + +static void check_open(const char *json_path, const char *open_path) +{ + size_t jl; + char *jb = fs_slurp(json_path, &jl); + if (!jb) gc_fail("cannot read %s", json_path); + unsigned char d[32]; + char jsha[65]; + sha256_hash((const unsigned char *)jb, jl, d); + fs_hex(d, jsha); + free(jb); + + size_t len; + char *b = fs_slurp(open_path, &len); + if (!b) gc_fail("cannot read %s", open_path); + if (strlen(b) != len || len == 0 || b[len - 1] != '\n') gc_fail("OPEN: must be text ending in a newline"); + char *ln[8]; + int nl = 0; + for (char *s = b; *s;) { + char *e = strchr(s, '\n'); + *e = 0; + if (nl == 8) gc_fail("OPEN: more than 5 lines"); + ln[nl++] = s; + s = e + 1; + } + if (nl != 5 || strcmp(ln[0], "gen-open 1")) gc_fail("OPEN: must be exactly 5 lines starting \"gen-open 1\""); + static const char *const key[4] = {"omega_commit ", "protocol_sha256 ", "tasks_sha256 ", "heldout_pub_sha256 "}; + const char *val[4]; + for (int i = 0; i < 4; i++) { + size_t kl = strlen(key[i]); + if (strncmp(ln[1 + i], key[i], kl)) gc_fail("OPEN: line %d must start \"%s\"", 2 + i, key[i]); + val[i] = ln[1 + i] + kl; + } + if (strlen(val[0]) != 40 || strspn(val[0], "0123456789abcdef") != 40) gc_fail("OPEN: omega_commit must be 40 lowercase hex"); + for (int i = 1; i < 4; i++) + if (!hex64(val[i])) gc_fail("OPEN: %smust be 64 lowercase hex", key[i]); + if (strcmp(val[1], jsha)) gc_fail("OPEN: protocol_sha256 %s, protocol.json is %s", val[1], jsha); + if (strcmp(val[2], get("splits.heldout.tasks_sha256")->raw)) gc_fail("OPEN: tasks_sha256 differs from splits.heldout.tasks_sha256"); + if (strcmp(val[3], get("splits.heldout.pub_sha256")->raw)) gc_fail("OPEN: heldout_pub_sha256 differs from splits.heldout.pub_sha256"); + printf("ok OPEN matches protocol.json %.16s...\n", jsha); + free(b); +} + +int main(int argc, char **argv) +{ + if (argc == 4 && !strcmp(argv[1], "manifest")) { + root = argv[3]; + check_manifest(argv[2]); + } else if (argc == 4 && !strcmp(argv[1], "run-ready")) { + root = argv[3]; + strict = 1; + check_manifest(argv[2]); + check_frozen(); + } else if (argc == 5 && !strcmp(argv[1], "open")) { + root = argv[3]; + strict = 1; + check_manifest(argv[2]); + check_frozen(); + check_open(argv[2], argv[4]); + } else if (argc == 6 && !strcmp(argv[1], "split")) { + root = argv[3]; + check_manifest(argv[2]); + check_split(argv[4], argv[5]); + } else { + fprintf(stderr, "usage: gencheck manifest | run-ready | open | split \n"); + return 2; + } + printf("GENCHECK PASS\n"); + return 0; +} diff --git a/tools/gen/protocol/genjac.c b/tools/gen/protocol/genjac.c new file mode 100644 index 000000000..0ad073c36 --- /dev/null +++ b/tools/gen/protocol/genjac.c @@ -0,0 +1,199 @@ +/* + * genjac.c -- word-3-gram Jaccard near-duplicate check for GEN held-out prompts (aien-spark/omega#476, + * docs/one_layer/GEN_PROTOCOL_V1.md section 2). Deterministic: no clock, no randomness, no environment. + * + * genjac pair + * print "grams_a= grams_b= inter= union= jaccard=<6 decimals>" + * genjac scan [--threshold ] [--show] + * every held-out file against every corpus file; a list file names one path per line (no blank + * lines, no comments). Prints "heldout= corpus= threshold= max_jaccard=<6 decimals> + * above=" where above = the number of held-out files with at least one corpus file whose Jaccard + * is strictly greater than t. Nothing about any held-out file is printed unless --show is given + * (custodian only): then one "above " line per such pair. + * Exit 0 when above = 0, 1 otherwise, 2 on usage or read errors. Default t = 0.5. + * + * Words: bytes A-Z are lowercased; a word is a maximal run of [a-z0-9]; every other byte separates (the + * genret tokenization, tools/gen/retrieval/CONTRACT.md section 4). Grams: the SET of consecutive word + * triples. Jaccard = |A n B| / |A u B|; 0 when both sets are empty (a file with fewer than 3 words has no + * gram). The comparison "> t" is exact: t is a decimal with at most 6 fraction digits, read as an integer + * ratio, and compared by integer cross-multiplication. + */ +#include +#include +#include +#include + +typedef struct { char **g; int n; } Grams; + +static char *slurp(const char *path) +{ + FILE *fp = fopen(path, "rb"); + if (!fp) { fprintf(stderr, "genjac: cannot read %s\n", path); exit(2); } + size_t cap = 4096, n = 0, r; + char *b = malloc(cap + 1); + while (b && (r = fread(b + n, 1, cap - n, fp)) > 0) { + n += r; + if (n == cap) { cap *= 2; b = realloc(b, cap + 1); } + } + fclose(fp); + if (!b) { perror("genjac"); exit(2); } + b[n] = 0; + for (size_t i = 0; i < n; i++) + if (!b[i]) { fprintf(stderr, "genjac: %s: NUL byte\n", path); exit(2); } + return b; +} + +static int cmp(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } + +static Grams grams(const char *path) +{ + char *s = slurp(path); + size_t len = strlen(s); + char **w = malloc((len / 2 + 2) * sizeof *w); + int nw = 0; + for (size_t i = 0; i < len;) { + unsigned char c = (unsigned char)s[i]; + if (c >= 'A' && c <= 'Z') s[i] = (char)(c + 32), c = (unsigned char)s[i]; + if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { + size_t j = i; + while (j < len) { + unsigned char d = (unsigned char)s[j]; + if (d >= 'A' && d <= 'Z') s[j] = (char)(d + 32), d = (unsigned char)s[j]; + if (!((d >= 'a' && d <= 'z') || (d >= '0' && d <= '9'))) break; + j++; + } + w[nw++] = strndup(s + i, j - i); + i = j; + } else i++; + } + Grams G = {NULL, 0}; + if (nw >= 3) { + G.g = malloc((size_t)(nw - 2) * sizeof *G.g); + for (int i = 0; i + 2 < nw; i++) { + size_t l = strlen(w[i]) + strlen(w[i + 1]) + strlen(w[i + 2]) + 3; + G.g[i] = malloc(l); + snprintf(G.g[i], l, "%s %s %s", w[i], w[i + 1], w[i + 2]); + } + qsort(G.g, (size_t)(nw - 2), sizeof *G.g, cmp); + int u = 0; + for (int i = 0; i < nw - 2; i++) + if (u == 0 || strcmp(G.g[i], G.g[u - 1])) G.g[u++] = G.g[i]; + G.n = u; + } + for (int i = 0; i < nw; i++) free(w[i]); + free(w); + free(s); + return G; +} + +static void jac(const Grams *a, const Grams *b, int64_t *inter, int64_t *uni) +{ + int i = 0, j = 0; + int64_t in = 0; + while (i < a->n && j < b->n) { + int c = strcmp(a->g[i], b->g[j]); + if (c == 0) { in++; i++; j++; } else if (c < 0) i++; else j++; + } + *inter = in; + *uni = a->n + b->n - in; +} + +/* t = num / den, den = 10^digits */ +static int parse_t(const char *s, int64_t *num, int64_t *den) +{ + int64_t n = 0, d = 1; + int dot = 0, digits = 0, any = 0; + for (const char *p = s; *p; p++) { + if (*p == '.' && !dot) { dot = 1; continue; } + if (*p < '0' || *p > '9') return -1; + any = 1; + n = n * 10 + (*p - '0'); + if (dot) { d *= 10; if (++digits > 6) return -1; } + if (n > 10000000) return -1; + } + if (!any || n > d) return -1; /* 0 <= t <= 1 */ + *num = n; + *den = d; + return 0; +} + +static char **read_list(const char *path, int *n) +{ + char *s = slurp(path); + size_t len = strlen(s); + if (len == 0 || s[len - 1] != '\n') { fprintf(stderr, "genjac: %s: empty or no final newline\n", path); exit(2); } + char **v = malloc((len + 1) * sizeof *v); + int k = 0; + for (char *p = s; *p;) { + char *e = strchr(p, '\n'); + *e = 0; + if (!*p) { fprintf(stderr, "genjac: %s: blank line\n", path); exit(2); } + v[k++] = p; + p = e + 1; + } + *n = k; + return v; +} + +static void print6(int64_t in, int64_t un) +{ + /* exact rounding of in/un to 6 decimals, half up */ + if (un == 0) { printf("0.000000"); return; } + int64_t q = (in * 2000000 + un) / (2 * un); + printf("%lld.%06lld", (long long)(q / 1000000), (long long)(q % 1000000)); +} + +static int usage(void) +{ + fprintf(stderr, "usage: genjac pair | scan [--threshold ] [--show] \n"); + return 2; +} + +int main(int argc, char **argv) +{ + if (argc == 4 && !strcmp(argv[1], "pair")) { + Grams a = grams(argv[2]), b = grams(argv[3]); + int64_t in, un; + jac(&a, &b, &in, &un); + printf("grams_a=%d grams_b=%d inter=%lld union=%lld jaccard=", a.n, b.n, (long long)in, (long long)un); + print6(in, un); + putchar('\n'); + return 0; + } + if (argc < 4 || strcmp(argv[1], "scan")) return usage(); + const char *ts = "0.5"; + int show = 0, i = 2; + for (; i < argc - 2; i++) { + if (!strcmp(argv[i], "--threshold") && i + 1 < argc - 2) ts = argv[++i]; + else if (!strcmp(argv[i], "--show")) show = 1; + else return usage(); + } + if (i != argc - 2) return usage(); + int64_t tn, td; + if (parse_t(ts, &tn, &td)) { fprintf(stderr, "genjac: threshold must be a decimal in [0,1] with at most 6 fraction digits\n"); return 2; } + int nh, nc; + char **hl = read_list(argv[argc - 2], &nh), **cl = read_list(argv[argc - 1], &nc); + Grams *cg = malloc((size_t)nc * sizeof *cg); + for (int c = 0; c < nc; c++) cg[c] = grams(cl[c]); + int64_t bi = 0, bu = 1; /* max Jaccard as a ratio, starts at 0/1 */ + int above = 0; + for (int h = 0; h < nh; h++) { + Grams g = grams(hl[h]); + int hit = 0; + for (int c = 0; c < nc; c++) { + int64_t in, un; + jac(&g, &cg[c], &in, &un); + if (un == 0) continue; + if (in * bu > bi * un) { bi = in; bu = un; } + if (in * td > tn * un) { /* in/un > tn/td, exactly */ + hit = 1; + if (show) { printf("above %s %s ", hl[h], cl[c]); print6(in, un); putchar('\n'); } + } + } + above += hit; + } + printf("heldout=%d corpus=%d threshold=%s max_jaccard=", nh, nc, ts); + print6(bi, bu); + printf(" above=%d\n", above); + return above ? 1 : 0; +} diff --git a/tools/gen/protocol/genstats.c b/tools/gen/protocol/genstats.c new file mode 100644 index 000000000..4d4b3cffc --- /dev/null +++ b/tools/gen/protocol/genstats.c @@ -0,0 +1,450 @@ +/* + * genstats.c -- reference statistics of the GEN decision rule (aien-spark/omega#476, + * docs/one_layer/GEN_PROTOCOL_V1.md section 9). Writes tests/one_layer/generalization/stats_vectors.tsv, + * the test vectors the GEN reducer (tools/gen/reducer, omega#477) must reproduce. + * + * genstats wilson <95|90> "p lo hi" + * genstats mcnemar1 "p_num p_den p": exact one-sided McNemar, + * P(Bin(b+c, 1/2) >= b) = p_num / p_den, p_den = 2^(b+c) + * genstats newcombe <95|90> [8|10] + * "theta lo hi": Newcombe (1998, Stat Med 17:2635) paired + * interval for p1 - p2, method 10 (default) or 8; a = both, + * b = first only, c = second only, d = neither + * genstats verdict the full decision from four 0/1 strings (one char per task, n = 60 only); + * order INVALID > floor/ceiling INCONCLUSIVE > PASS > FAIL > INCONCLUSIVE + * genstats lenmatch :... length-matched ablation classes (section 10): per task gap, + * tolerance, matched|unmatched; then matched count and status + * genstats vectors print the whole stats_vectors.tsv + * genstats selftest check Newcombe methods 8 and 10 against every row of the + * paper's Table III (4 decimals), and internal identities + * Exact integer arithmetic for McNemar (unsigned __int128, b + c <= 120); IEEE double elsewhere. + * Deterministic: no clock, no randomness, no environment. + */ +#include +#include +#include +#include + +typedef unsigned __int128 u128; + +/* standard normal quantiles, pinned (GEN_PROTOCOL_V1 section 9) */ +#define Z95 1.959963984540054 /* z(0.975): two-sided 95% */ +#define Z90 1.644853626951472 /* z(0.95): two-sided 90% */ + +static double zof(int conf) { return conf == 95 ? Z95 : Z90; } + +/* Wilson score interval without continuity correction; exact 0 / 1 at k = 0 / k = n */ +static void wilson(long k, long n, double z, double *p, double *lo, double *hi) +{ + double pp = (double)k / (double)n, z2 = z * z, nn = (double)n, den = 1 + z2 / nn; + double c = (pp + z2 / (2 * nn)) / den, h = z * sqrt(pp * (1 - pp) / nn + z2 / (4 * nn * nn)) / den; + *p = pp; + *lo = k == 0 ? 0.0 : c - h; + *hi = k == n ? 1.0 : c + h; +} + +static void u128str(u128 v, char *out) +{ + char t[64]; + int n = 0; + do { t[n++] = (char)('0' + (int)(v % 10)); v /= 10; } while (v); + for (int i = 0; i < n; i++) out[i] = t[n - 1 - i]; + out[n] = 0; +} + +/* P(Y >= b), Y ~ Bin(b + c, 1/2), as num / 2^(b+c) */ +static void mcnemar1(int b, int c, u128 *num, u128 *den) +{ + int n = b + c; + u128 s = 0, binom = 1; /* binom = C(n, i) */ + for (int i = 0; i <= n; i++) { + if (i >= b) s += binom; + binom = binom * (u128)(n - i) / (u128)(i + 1); + } + *num = s; + *den = (u128)1 << n; +} + +/* Newcombe paired interval for theta = (b - c) / n. method 10: phi numerator a*d - b*c replaced by + * max(a*d - b*c - n/2, 0) when a*d > b*c; method 8: plain phi. phi = 0 when any marginal is 0. */ +static void newcombe(long a, long b, long c, long d, double z, int method, double *theta, double *lo, double *hi) +{ + long n = a + b + c + d; + double p1, l1, u1, p2, l2, u2; + wilson(a + b, n, z, &p1, &l1, &u1); + wilson(a + c, n, z, &p2, &l2, &u2); + double num = (double)a * (double)d - (double)b * (double)c; + if (method == 10 && num > 0) { num -= (double)n / 2.0; if (num < 0) num = 0; } + double dprod = (double)(a + b) * (double)(c + d) * (double)(a + c) * (double)(b + d); + double phi = dprod == 0 ? 0.0 : num / sqrt(dprod); + double dl1 = p1 - l1, du1 = u1 - p1, dl2 = p2 - l2, du2 = u2 - p2; + double s1 = dl1 * dl1 - 2 * phi * dl1 * du2 + du2 * du2; + double s2 = du1 * du1 - 2 * phi * du1 * dl2 + dl2 * dl2; + double delta = sqrt(s1 > 0 ? s1 : 0), eps = sqrt(s2 > 0 ? s2 : 0); + *theta = (double)(b - c) / (double)n; + *lo = *theta - delta; + *hi = *theta + eps; + if (*lo < -1) *lo = -1; + if (*hi > 1) *hi = 1; +} + +/* ---- the decision ---------------------------------------------------------------------------- */ + +typedef struct { + int n, s[4]; /* successes of A B C D */ + int both[3], donly[3], xonly[3], neither[3]; + char pnum[3][48], pden[3][48]; + double p[3]; + int lift_ok[3], p_ok[3]; + double lo95, hi95, lo90, hi90; /* Newcombe method 10, D - C */ + int a_minus_b; + int floor, ceiling; + const char *verdict; + char reasons[256]; +} Decision; + +static int decide(int valid, char *const v[4], Decision *r, char *err, size_t errn) +{ + memset(r, 0, sizeof *r); + size_t n = strlen(v[0]); + if (n != 60) { snprintf(err, errn, "vector length %zu, the verdict is defined for n = 60 only (GEN_PROTOCOL_V1 section 9)", n); return -1; } + for (int x = 0; x < 4; x++) { + if (strlen(v[x]) != n) { snprintf(err, errn, "vectors differ in length (incomplete paired matrix)"); return -1; } + for (size_t i = 0; i < n; i++) if (v[x][i] != '0' && v[x][i] != '1') { snprintf(err, errn, "vector %c holds a byte other than 0/1", "ABCD"[x]); return -1; } + } + r->n = (int)n; + for (int x = 0; x < 4; x++) for (size_t i = 0; i < n; i++) r->s[x] += v[x][i] == '1'; + for (int x = 0; x < 3; x++) { + for (size_t i = 0; i < n; i++) { + int d = v[3][i] == '1', o = v[x][i] == '1'; + if (d && o) r->both[x]++; else if (d) r->donly[x]++; else if (o) r->xonly[x]++; else r->neither[x]++; + } + u128 num, den; + mcnemar1(r->donly[x], r->xonly[x], &num, &den); + u128str(num, r->pnum[x]); + u128str(den, r->pden[x]); + r->p[x] = (double)num / (double)den; + r->p_ok[x] = (u128)20 * num < den; /* p < 0.05, exactly */ + r->lift_ok[x] = 10 * (r->donly[x] - r->xonly[x]) >= r->n; /* (d_only - x_only) / n >= 0.10, exactly */ + } + double th; + newcombe(r->both[2], r->donly[2], r->xonly[2], r->neither[2], Z95, 10, &th, &r->lo95, &r->hi95); + newcombe(r->both[2], r->donly[2], r->xonly[2], r->neither[2], Z90, 10, &th, &r->lo90, &r->hi90); + r->a_minus_b = r->s[0] - r->s[1]; + int mx = r->s[0], mn = r->s[0]; + for (int x = 1; x < 4; x++) { if (r->s[x] > mx) mx = r->s[x]; if (r->s[x] < mn) mn = r->s[x]; } + r->floor = mx <= 3; /* max successes over A..D <= 3 */ + r->ceiling = mn >= r->n - 3; /* min successes over A..D >= n - 3 (57 at n = 60) */ + int pass = 1; + for (int x = 0; x < 3; x++) pass &= r->lift_ok[x] && r->p_ok[x]; + if (!valid || r->a_minus_b > 2) { + r->verdict = "INVALID"; + snprintf(r->reasons, sizeof r->reasons, "%s%s%s", !valid ? "validity_failure" : "", !valid && r->a_minus_b > 2 ? "," : "", + r->a_minus_b > 2 ? "sanity_a_minus_b_gt_2" : ""); + } else if (r->floor || r->ceiling) { + r->verdict = "INCONCLUSIVE"; /* the tasks were too hard or too easy to compare conditions */ + snprintf(r->reasons, sizeof r->reasons, "%s%s%s", r->floor ? "floor" : "", r->floor && r->ceiling ? "," : "", r->ceiling ? "ceiling" : ""); + } else if (pass) { + r->verdict = "PASS"; + snprintf(r->reasons, sizeof r->reasons, "all_controls_lift_and_p"); + } else if (r->hi95 < 0.10) { /* the full double, never the 6-decimal receipt value */ + r->verdict = "FAIL"; + snprintf(r->reasons, sizeof r->reasons, "newcombe95_hi_D-C_below_delta"); + } else { + r->verdict = "INCONCLUSIVE"; + int k = 0; + for (int x = 0; x < 3; x++) { + int w = 0; + if (!r->lift_ok[x]) w = snprintf(r->reasons + k, sizeof r->reasons - (size_t)k, "%slift_%c", k ? "," : "", "ABC"[x]); + if (w < 0 || (size_t)w >= sizeof r->reasons - (size_t)k) break; + k += w; + w = 0; + if (!r->p_ok[x]) w = snprintf(r->reasons + k, sizeof r->reasons - (size_t)k, "%sp_%c", k ? "," : "", "ABC"[x]); + if (w < 0 || (size_t)w >= sizeof r->reasons - (size_t)k) break; + k += w; + } + } + return 0; +} + +/* ---- vectors --------------------------------------------------------------------------------- */ + +/* a verdict case: groups of (ABCD pattern, count), concatenated in order */ +typedef struct { const char *id; int valid; const char *groups; const char *note; } VCase; + +static const VCase vcases[] = { + {"v01_pass", 1, "1111:18 0001:16 0011:4 0111:2 0000:20", "D far ahead of every control"}, + {"v02_fail_equal", 1, "1111:20 0010:3 0001:3 0000:34", "D = C exactly; upper bound below 10pp"}, + {"v03_inconclusive_small_lift", 1, "1111:20 0011:5 0001:4 0010:1 0000:30", "D - C = 3 tasks: neither PASS nor FAIL"}, + {"v04_inconclusive_c_p", 1, "1111:15 0011:10 0001:8 0010:2 0000:25", "lift >= 10pp vs C (6 tasks) but p vs C = 0.0547"}, + {"v05_invalid_validity", 0, "1111:18 0001:16 0011:4 0111:2 0000:20", "v01 with a validity failure"}, + {"v06_invalid_sanity", 1, "1111:15 1001:3 0011:1 0001:20 0000:21", "A - B = 3 tasks"}, + {"v07_sanity_boundary_pass", 1, "1111:15 1011:2 0001:20 0000:23", "A - B = 2 tasks: allowed"}, + {"v08_boundary_lift6_p", 1, "1111:20 0001:6 0000:34", "d_only = 6, x_only = 0 vs every control: lift exactly 10pp, p = 1/64"}, + {"v09_boundary_lift5", 1, "1111:20 0001:5 0000:35", "lift 5/60 < 10pp vs every control"}, + {"v10_boundary_8_2", 1, "1111:20 0001:8 1110:2 0000:30", "d_only 8, x_only 2: lift 10pp, p = 56/1024 = 0.0547 (not < 0.05)"}, + {"v11_boundary_7_1", 1, "1111:20 0001:7 1110:1 0000:32", "d_only 7, x_only 1: lift 10pp, p = 9/256"}, + {"v12_all_fail", 1, "0000:60", "nothing succeeds: INCONCLUSIVE floor (was FAIL before the floor rule)"}, + {"v13_all_pass", 1, "1111:60", "everything succeeds: INCONCLUSIVE ceiling"}, + {"v14_d_worse", 1, "1111:20 1110:8 0001:1 0000:31", "D behind every control"}, + /* v15 (n = 10, dev size) removed 2026-10-16: the verdict is defined for n = 60 only; other n stay in the CI rows */ + {"v16_pass_vs_ab_not_c", 1, "1111:10 0011:12 0001:4 0000:34", "D ahead of A and B, tied-ish with C"}, + {"v17_floor_boundary", 1, "0001:3 0000:57", "max successes = 3 (D): INCONCLUSIVE floor"}, + {"v18_floor_above", 1, "0001:4 0000:56", "max successes = 4: not floor (INCONCLUSIVE on lift and p)"}, + {"v19_ceiling_boundary", 1, "1111:57 1110:3", "min successes = 57 (D): INCONCLUSIVE ceiling"}, + {"v20_ceiling_below", 1, "1111:56 1110:4", "min successes = 56: not ceiling"}, + {"v21_floor_overrides_fail", 1, "1111:2 0000:58", "everything at 2 successes: floor, not FAIL"}, +}; + +static int build(const char *groups, char *v[4]) +{ + static char buf[4][128]; + int n = 0; + const char *s = groups; + while (*s) { + char pat[5]; + int cnt, used; + if (sscanf(s, "%4[01]:%d%n", pat, &cnt, &used) != 2 || strlen(pat) != 4) return -1; + for (int i = 0; i < cnt; i++) { if (n >= 120) return -1; for (int x = 0; x < 4; x++) buf[x][n] = pat[x]; n++; } + s += used; + while (*s == ' ') s++; + } + for (int x = 0; x < 4; x++) { buf[x][n] = 0; v[x] = buf[x]; } + return n; +} + +static void print_decision_cols(const Decision *r) +{ + printf("%d\t%d\t%d\t%d", r->s[0], r->s[1], r->s[2], r->s[3]); + for (int x = 0; x < 3; x++) + printf("\t%d\t%d\t%d\t%d\t%s\t%s\t%.10f\t%s\t%s", r->both[x], r->donly[x], r->xonly[x], r->neither[x], r->pnum[x], r->pden[x], r->p[x], + r->lift_ok[x] ? "yes" : "no", r->p_ok[x] ? "yes" : "no"); + printf("\t%.10f\t%.10f\t%.10f\t%.10f\t%d\t%s\t%s", r->lo95, r->hi95, r->lo90, r->hi90, r->a_minus_b, r->verdict, r->reasons); +} + +/* Table III of Newcombe (1998): e f g h, method 8 lo hi, method 10 lo hi (95%, 4 decimals as printed) */ +static const double table3[][8] = { + {36, 12, 2, 0, 0.0569, 0.3404, 0.0569, 0.3404}, + {20, 12, 2, 16, 0.0618, 0.3242, 0.0562, 0.3292}, + {18, 12, 2, 18, 0.0618, 0.3239, 0.0562, 0.3290}, + {36, 14, 0, 0, 0.1528, 0.4167, 0.1528, 0.4167}, + {35, 14, 0, 1, 0.1573, 0.4149, 0.1461, 0.4175}, + {18, 14, 0, 18, 0.1504, 0.3910, 0.1441, 0.3963}, + {2, 97, 1, 0, 0.8721, 0.9854, 0.8721, 0.9854}, + {1, 97, 1, 1, 0.8737, 0.9850, 0.8736, 0.9850}, + {0, 29, 1, 0, 0.6666, 0.9882, 0.6666, 0.9882}, + {2, 98, 0, 0, 0.9178, 0.9945, 0.9178, 0.9945}, + {1, 98, 0, 1, 0.9174, 0.9916, 0.9171, 0.9916}, + {0, 30, 0, 0, 0.8395, 1.0, 0.8395, 1.0}, + {54, 0, 0, 0, -0.0664, 0.0664, -0.0664, 0.0664}, + {53, 0, 0, 1, -0.0640, 0.0640, -0.0729, 0.0729}, + {30, 0, 0, 24, -0.0074, 0.0074, -0.0358, 0.0358}, + {29, 0, 0, 25, -0.0049, 0.0049, -0.0354, 0.0354}, + {28, 0, 0, 26, -0.0025, 0.0025, -0.0352, 0.0352}, + {27, 0, 0, 27, 0.0, 0.0, -0.0351, 0.0351}, +}; +#define NT3 (sizeof table3 / sizeof table3[0]) + +/* ---- length-matched ablation (GEN_PROTOCOL_V1 section 10; dev-only diagnostic, never in the verdict) ---- + * gap = padded C suffix_bytes - target_bytes (D's suffix_bytes), signed (genret length_gap). + * A task is matched iff |gap| <= max(256 bytes, 15% of target), exactly: 100 |gap| <= max(25600, 15 target). + * A set is AVAILABLE iff at least half its tasks are matched (2 matched >= tasks), else UNAVAILABLE. */ +static int lm_matched(long target, long suffix, long *gap) +{ + *gap = suffix - target; + long ag = *gap < 0 ? -*gap : *gap, lim = 15 * target > 25600 ? 15 * target : 25600; + return 100 * ag <= lim; +} +static const char *lm_status(int matched, int tasks) { return 2 * matched >= tasks ? "AVAILABLE" : "UNAVAILABLE"; } +/* tolerance in hundredths of a byte: max(25600, 15 target) */ +static void lm_tol(long target, char *out, size_t n) +{ + long t = 15 * target > 25600 ? 15 * target : 25600; + snprintf(out, n, "%ld.%02ld", t / 100, t % 100); +} + +static int vectors(void) +{ + printf("# GEN decision statistics test vectors v2 (aien-spark/omega#476). Generated by tools/gen/protocol/genstats.c\n"); + printf("# (`genstats vectors`); never edit by hand. Rules: docs/one_layer/GEN_PROTOCOL_V1.md sections 9 and 10.\n"); + printf("# z95 = %.15f (z 0.975), z90 = %.15f (z 0.95). Reals printed %%.10f; a reader must agree within 1e-9,\n", Z95, Z90); + printf("# and its 6-decimal receipt values must equal these rounded to 6 decimals. Counts, p_num, p_den, yes/no and verdicts exactly.\n"); + printf("# One record per line, TAB-separated; field 1 names the record kind; each kind has a #cols line.\n"); + printf("# Verdict order: INVALID (validity, A - B > 2) > INCONCLUSIVE floor (max succ <= 3) / ceiling (min succ >= n - 3) > PASS > FAIL\n"); + printf("# (D-C newcombe95_hi < 0.10, compared on the full double) > INCONCLUSIVE. v2 = v1 + the floor/ceiling rule and the length-match records (review of PR #486).\n"); + printf("#cols\twilson\tid\tk\tn\tconf\tp\tlo\thi\n"); + static const int wk[][2] = {{0, 60}, {1, 60}, {6, 60}, {20, 60}, {30, 60}, {48, 60}, {59, 60}, {60, 60}, {3, 10}, {0, 10}, {10, 10}}; + for (size_t i = 0; i < sizeof wk / sizeof wk[0]; i++) + for (int conf = 95; conf >= 90; conf -= 5) { + double p, lo, hi; + wilson(wk[i][0], wk[i][1], zof(conf), &p, &lo, &hi); + printf("wilson\tw%02zu_%d\t%d\t%d\t%d\t%.10f\t%.10f\t%.10f\n", i + 1, conf, wk[i][0], wk[i][1], conf, p, lo, hi); + } + printf("#cols\tmcnemar1\tid\tb\tc\tp_num\tp_den\tp\tp_lt_0.05\n"); + static const int mc[][2] = {{0, 0}, {1, 0}, {0, 1}, {4, 0}, {5, 0}, {6, 0}, {7, 1}, {8, 2}, {9, 3}, {10, 4}, {12, 6}, {3, 9}, {15, 15}, {30, 30}, {60, 0}, {0, 60}, {16, 2}, {45, 15}}; + for (size_t i = 0; i < sizeof mc / sizeof mc[0]; i++) { + u128 num, den; + char ns[48], ds[48]; + mcnemar1(mc[i][0], mc[i][1], &num, &den); + u128str(num, ns); + u128str(den, ds); + printf("mcnemar1\tm%02zu\t%d\t%d\t%s\t%s\t%.10f\t%s\n", i + 1, mc[i][0], mc[i][1], ns, ds, (double)num / (double)den, (u128)20 * num < den ? "yes" : "no"); + } + printf("#cols\tnewcombe\tid\ta_both\tb_first_only\tc_second_only\td_neither\tconf\tmethod\ttheta\tlo\thi\tsource\n"); + int id = 0; + for (size_t i = 0; i < NT3; i++) { + double th, lo, hi; + newcombe((long)table3[i][0], (long)table3[i][1], (long)table3[i][2], (long)table3[i][3], Z95, 10, &th, &lo, &hi); + printf("newcombe\tn%02d\t%.0f\t%.0f\t%.0f\t%.0f\t95\t10\t%.10f\t%.10f\t%.10f\tNewcombe1998_TableIII_published_%.4f_%.4f\n", ++id, + table3[i][0], table3[i][1], table3[i][2], table3[i][3], th, lo, hi, table3[i][6], table3[i][7]); + } + static const int gc[][4] = {{20, 6, 0, 34}, {20, 8, 2, 30}, {20, 3, 3, 34}, {0, 0, 0, 60}, {60, 0, 0, 0}, {25, 12, 6, 17}, {20, 4, 1, 35}, {3, 4, 0, 3}}; + for (size_t i = 0; i < sizeof gc / sizeof gc[0]; i++) + for (int conf = 95; conf >= 90; conf -= 5) { + double th, lo, hi; + newcombe(gc[i][0], gc[i][1], gc[i][2], gc[i][3], zof(conf), 10, &th, &lo, &hi); + printf("newcombe\tn%02d\t%d\t%d\t%d\t%d\t%d\t10\t%.10f\t%.10f\t%.10f\tgen\n", ++id, gc[i][0], gc[i][1], gc[i][2], gc[i][3], conf, th, lo, hi); + } + printf("#cols\tlenmatch\tid\ttarget_bytes\tsuffix_bytes\tlength_gap\ttolerance_bytes\tclass\n"); + static const long lmv[][2] = {{295, 945}, {300, 738}, {300, 556}, {300, 557}, {300, 44}, {300, 43}, + {2000, 2300}, {2000, 2301}, {2000, 1700}, {2000, 1699}, {1000, 1000}, {1706, 1962}}; + for (size_t i = 0; i < sizeof lmv / sizeof lmv[0]; i++) { + long gap; + char tol[32]; + int m = lm_matched(lmv[i][0], lmv[i][1], &gap); + lm_tol(lmv[i][0], tol, sizeof tol); + printf("lenmatch\tl%02zu\t%ld\t%ld\t%ld\t%s\t%s\n", i + 1, lmv[i][0], lmv[i][1], gap, tol, m ? "matched" : "unmatched"); + } + printf("#cols\tlenset\tid\ttasks\tmatched\tunmatched\tstatus\n"); + static const int lsv[][2] = {{10, 10}, {10, 5}, {10, 4}, {10, 0}, {9, 5}, {9, 4}, {1, 1}}; + for (size_t i = 0; i < sizeof lsv / sizeof lsv[0]; i++) + printf("lenset\ts%02zu\t%d\t%d\t%d\t%s\n", i + 1, lsv[i][0], lsv[i][1], lsv[i][0] - lsv[i][1], lm_status(lsv[i][1], lsv[i][0])); + printf("#cols\tverdict\tid\tvalid\tvec_A\tvec_B\tvec_C\tvec_D\tsucc_A\tsucc_B\tsucc_C\tsucc_D"); + for (int x = 0; x < 3; x++) + printf("\tD-%c.both\tD-%c.d_only\tD-%c.x_only\tD-%c.neither\tD-%c.p_num\tD-%c.p_den\tD-%c.p\tD-%c.lift_ok\tD-%c.p_ok", "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x]); + printf("\tD-C.newcombe95_lo\tD-C.newcombe95_hi\tD-C.newcombe90_lo\tD-C.newcombe90_hi\ta_minus_b\tverdict\treasons\n"); + for (size_t i = 0; i < sizeof vcases / sizeof vcases[0]; i++) { + char *v[4]; + if (build(vcases[i].groups, v) < 0) { fprintf(stderr, "genstats: bad case %s\n", vcases[i].id); return 1; } + Decision r; + char err[160]; + if (decide(vcases[i].valid, v, &r, err, sizeof err)) { fprintf(stderr, "genstats: %s: %s\n", vcases[i].id, err); return 1; } + printf("verdict\t%s\t%d\t%s\t%s\t%s\t%s\t", vcases[i].id, vcases[i].valid, v[0], v[1], v[2], v[3]); + print_decision_cols(&r); + putchar('\n'); + } + return 0; +} + +static int selftest(void) +{ + int bad = 0; + for (size_t i = 0; i < NT3; i++) + for (int m = 0; m < 2; m++) { + double th, lo, hi; + newcombe((long)table3[i][0], (long)table3[i][1], (long)table3[i][2], (long)table3[i][3], Z95, m ? 10 : 8, &th, &lo, &hi); + double plo = table3[i][4 + 2 * m], phi = table3[i][5 + 2 * m]; + /* printed to 4 decimals: the exact value must round to the printed one. One known exception: + * row 1 97 1 1 has a*d < b*c, so methods 8 and 10 are the same formula, yet the paper prints + * lower limits 0.8737 (method 8) and 0.8736 (method 10); the computed 0.873672 matches the method 8 + * cell, so that single method-10 cell is allowed one unit in the 4th decimal. */ + double tol = (m && table3[i][0] == 1 && table3[i][1] == 97 && table3[i][3] == 1) ? 0.0001 + 1e-12 : 0.00005 + 1e-12; + if (fabs(lo - plo) > tol || fabs(hi - phi) > 0.00005 + 1e-12) { + printf("FAIL newcombe method %d %.0f %.0f %.0f %.0f: got %.6f %.6f, paper %.4f %.4f\n", m ? 10 : 8, table3[i][0], table3[i][1], table3[i][2], table3[i][3], lo, hi, plo, phi); + bad++; + } + } + /* McNemar: the one-sided tails of b and of c sum to 1 + P(Y = b) */ + for (int b = 0; b <= 30; b++) + for (int c = 0; c <= 30; c++) { + u128 n1, d1, n2, d2, pb = 1; + mcnemar1(b, c, &n1, &d1); + mcnemar1(c, b, &n2, &d2); + for (int i = 0; i < b; i++) pb = pb * (u128)(b + c - i) / (u128)(i + 1); + if (d1 != d2 || n1 + n2 != d1 + pb) { printf("FAIL mcnemar identity b=%d c=%d\n", b, c); bad++; } + } + /* Wilson: the 95% interval contains the 90% one and the point */ + for (int k = 0; k <= 60; k++) { + double p, l95, h95, l90, h90; + wilson(k, 60, Z95, &p, &l95, &h95); + wilson(k, 60, Z90, &p, &l90, &h90); + if (!(l95 <= l90 && l90 <= p && p <= h90 && h90 <= h95)) { printf("FAIL wilson nesting k=%d\n", k); bad++; } + } + printf(bad ? "SELFTEST FAIL (%d)\n" : "SELFTEST PASS (Newcombe Table III: %d rows x methods 8 and 10; McNemar tail identity 31x31; Wilson nesting 61)\n", bad ? bad : (int)NT3); + return bad ? 1 : 0; +} + +static long num(const char *s, long lo, long hi) +{ + char *e; + long v = strtol(s, &e, 10); + if (!*s || *e || v < lo || v > hi) { fprintf(stderr, "genstats: '%s' is not an integer in %ld..%ld\n", s, lo, hi); exit(2); } + return v; +} + +static int conf_arg(const char *s) +{ + if (!strcmp(s, "95")) return 95; + if (!strcmp(s, "90")) return 90; + fprintf(stderr, "genstats: confidence must be 95 or 90\n"); + exit(2); +} + +int main(int argc, char **argv) +{ + if (argc == 5 && !strcmp(argv[1], "wilson")) { + long n = num(argv[3], 1, 1000000), k = num(argv[2], 0, n); + double p, lo, hi; + wilson(k, n, zof(conf_arg(argv[4])), &p, &lo, &hi); + printf("%.10f %.10f %.10f\n", p, lo, hi); + return 0; + } + if (argc == 4 && !strcmp(argv[1], "mcnemar1")) { + int b = (int)num(argv[2], 0, 120), c = (int)num(argv[3], 0, 120 - b); + u128 n, d; + char ns[48], ds[48]; + mcnemar1(b, c, &n, &d); + u128str(n, ns); + u128str(d, ds); + printf("%s %s %.10f\n", ns, ds, (double)n / (double)d); + return 0; + } + if ((argc == 7 || argc == 8) && !strcmp(argv[1], "newcombe")) { + long a = num(argv[2], 0, 1000000), b = num(argv[3], 0, 1000000), c = num(argv[4], 0, 1000000), d = num(argv[5], 0, 1000000); + if (a + b + c + d == 0) { fprintf(stderr, "genstats: n = 0\n"); return 2; } + int m = argc == 8 ? (int)num(argv[7], 8, 10) : 10; + if (m == 9) { fprintf(stderr, "genstats: method 8 or 10\n"); return 2; } + double th, lo, hi; + newcombe(a, b, c, d, zof(conf_arg(argv[6])), m, &th, &lo, &hi); + printf("%.10f %.10f %.10f\n", th, lo, hi); + return 0; + } + if (argc == 7 && !strcmp(argv[1], "verdict")) { + Decision r; + char err[160]; + if (decide((int)num(argv[2], 0, 1), argv + 3, &r, err, sizeof err)) { fprintf(stderr, "genstats: %s\n", err); return 1; } + print_decision_cols(&r); + putchar('\n'); + return 0; + } + if (argc >= 3 && !strcmp(argv[1], "lenmatch")) { + int matched = 0; + for (int i = 2; i < argc; i++) { + char *colon = strchr(argv[i], ':'); + if (!colon) { fprintf(stderr, "genstats: lenmatch takes : pairs\n"); return 2; } + *colon = 0; + long t = num(argv[i], 1, 1000000), s = num(colon + 1, 0, 1000000), gap; + char tol[32]; + int m = lm_matched(t, s, &gap); + lm_tol(t, tol, sizeof tol); + printf("task %d target_bytes=%ld suffix_bytes=%ld length_gap=%ld tolerance_bytes=%s class=%s\n", i - 1, t, s, gap, tol, m ? "matched" : "unmatched"); + matched += m; + } + printf("tasks=%d matched=%d unmatched=%d status=%s\n", argc - 2, matched, argc - 2 - matched, lm_status(matched, argc - 2)); + return 0; + } + if (argc == 2 && !strcmp(argv[1], "vectors")) return vectors(); + if (argc == 2 && !strcmp(argv[1], "selftest")) return selftest(); + fprintf(stderr, "usage: genstats wilson <95|90> | mcnemar1 | newcombe <95|90> [8|10] | verdict | lenmatch :... | vectors | selftest\n"); + return 2; +} diff --git a/tools/gen/protocol/test_gen_protocol.sh b/tools/gen/protocol/test_gen_protocol.sh new file mode 100755 index 000000000..056b67a2d --- /dev/null +++ b/tools/gen/protocol/test_gen_protocol.sh @@ -0,0 +1,394 @@ +#!/usr/bin/env bash +# tools/gen/protocol/test_gen_protocol.sh -- tests of the GEN protocol tools (aien-spark/omega#476). +# CPU only; reads only public files of this repository; writes only under and a mktemp dir. +# 1. genstats: selftest (Newcombe 1998 Table III, McNemar, Wilson), byte-identical regeneration of +# tests/one_layer/generalization/stats_vectors.tsv, length-match classes; genjac known answers +# 2. famsig: known answers (canonical forms, the reserved dev family tokens), RULE PASS on the dev META, +# coarse forms, mutants (bad shapes and files, R2..R8 violations) that must each be refused, and +# famsig compare (blind re-declaration) +# 3. gencheck: PASS on the real protocol.json, then mutants of the manifest (unknown, duplicate, missing +# keys, missing or changed splits, duplicate task ids, digest drift, changed constants, bad types) and +# of the tree (shared signature across splits, extra case directory) that must each be refused; then +# the reviewer's split check on a SYNTHETIC held-out split (generated here, test-only, not a task) and +# its mutants (including fail-closed on null digests); run-ready and open on a fully frozen SYNTHETIC +# manifest (interface files taken from git at their pinned commits) and their mutants +# 4. dev pack: pack_dev.sh twice (byte-identical, sha256 = splits.dev.pack_sha256) and a negative control +# (one wrong hidden/expected line must refuse) +# GEN_TASKS_CHECK=1 also runs ola.sh tasks-check on the dev pack inside a private git worktree of HEAD (under the +# test temp dir, removed afterwards), so the shared evidence/ONE-LAYER-2 tree is never written or deleted. +set -uo pipefail +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" +B="${1:?usage: test_gen_protocol.sh }" +mkdir -p "$B"; B="$(realpath -e "$B")" +T="$(mktemp -d "${TMPDIR:-/tmp}/gen-proto-test.XXXXXXXX")"; trap 'rm -rf "$T"' EXIT +CFLAGS="-std=gnu11 -Wall -Wextra -Werror -O2" +G="$ROOT/tests/one_layer/generalization" +P="$G/protocol.json" +ok=0; bad=0 +pass() { ok=$((ok + 1)); echo "ok $1"; } +fail() { bad=$((bad + 1)); echo "FAIL $1"; } +expect_out() { # name expected command... + local n="$1" e="$2"; shift 2 + local got; got="$("$@" 2>/dev/null)" + if [ "$got" = "$e" ]; then pass "$n"; else fail "$n (got '$got')"; fi +} +refuse() { # name command... : must exit non-zero + local n="$1"; shift + if "$@" >/dev/null 2>&1; then fail "mutant not caught: $n"; else pass "refused: $n"; fi +} +refuse_why() { # name expected-output-substring command... : must exit non-zero and say why + local n="$1" w="$2"; shift 2 + if "$@" > "$T/rw.out" 2>&1; then fail "mutant not caught: $n"; elif grep -qF -- "$w" "$T/rw.out"; then pass "refused: $n"; else fail "refused for another reason: $n ($(tail -2 "$T/rw.out" | tr "\n" " "))"; fi +} + +${CC:-cc} $CFLAGS -I"$ROOT/src" -o "$B/famsig" "$ROOT/tools/gen/protocol/famsig.c" "$ROOT/src/omega_om_units.c" || exit 1 +${CC:-cc} $CFLAGS -I"$ROOT/src" -o "$B/gencheck" "$ROOT/tools/gen/protocol/gencheck.c" "$ROOT/src/omega_om_units.c" || exit 1 +${CC:-cc} $CFLAGS -o "$B/genstats" "$ROOT/tools/gen/protocol/genstats.c" -lm || exit 1 +${CC:-cc} $CFLAGS -o "$B/genjac" "$ROOT/tools/gen/protocol/genjac.c" || exit 1 +FS="$B/famsig"; GC="$B/gencheck"; GS="$B/genstats"; JC="$B/genjac" + +# ---- 1. statistics ---- +if "$GS" selftest | tail -1 | grep -q '^SELFTEST PASS'; then pass "genstats selftest"; else fail "genstats selftest"; fi +if "$GS" vectors | cmp -s - "$G/stats_vectors.tsv"; then pass "stats_vectors.tsv regenerates byte-identical"; else fail "stats_vectors.tsv regenerates byte-identical"; fi +z59=$(printf '%059d' 0); o59=$(printf '1%.0s' $(seq 59)); z60="0$z59"; o60="1$o59"; z61="0$z60" +refuse_why "genstats verdict with n = 59" "n = 60 only" "$GS" verdict 1 "$o59" "$z59" "$z59" "$o59" +refuse_why "genstats verdict with n = 61" "n = 60 only" "$GS" verdict 1 "${o60}1" "$z61" "$z61" "${o60}1" +if "$GS" verdict 1 "$o60" "$z60" "$z60" "$o60" >/dev/null 2>&1; then pass "genstats verdict with n = 60 runs"; else fail "genstats verdict with n = 60"; fi + +expect_out "lenmatch reviewer fixture 295 vs 945: unmatched" "task 1 target_bytes=295 suffix_bytes=945 length_gap=650 tolerance_bytes=256.00 class=unmatched" sh -c "\"$GS\" lenmatch 295:945 | head -1" +expect_out "lenmatch 256 B boundary: matched" "tasks=2 matched=1 unmatched=1 status=AVAILABLE" sh -c "\"$GS\" lenmatch 300:556 300:557 | tail -1" +expect_out "lenmatch 15% boundary, C shorter: matched" "tasks=2 matched=1 unmatched=1 status=AVAILABLE" sh -c "\"$GS\" lenmatch 2000:1700 2000:1699 | tail -1" +expect_out "lenmatch 4 of 10 matched: UNAVAILABLE" "tasks=10 matched=4 unmatched=6 status=UNAVAILABLE" sh -c "\"$GS\" lenmatch 300:300 300:301 300:302 300:303 300:945 300:946 300:947 300:948 300:949 300:950 | tail -1" +expect_out "lenmatch 5 of 10 matched: AVAILABLE" "tasks=10 matched=5 unmatched=5 status=AVAILABLE" sh -c "\"$GS\" lenmatch 300:300 300:301 300:302 300:303 300:304 300:946 300:947 300:948 300:949 300:950 | tail -1" +if grep -q $'^lenset\ts03\t10\t4\t6\tUNAVAILABLE$' "$G/stats_vectors.tsv" && grep -q $'^lenmatch\tl03\t300\t556\t256\t256.00\tmatched$' "$G/stats_vectors.tsv"; then pass "stats_vectors.tsv carries lenmatch and lenset records"; else fail "stats_vectors.tsv lenmatch/lenset records"; fi +refuse "lenmatch bad pair" "$GS" lenmatch 300-556 +# genjac: word 3-gram Jaccard +printf 'a b c d\n' > "$T/j1"; printf 'a b c e\n' > "$T/j2"; printf 'A, b; c d e\n' > "$T/j3"; printf 'a b c d f\n' > "$T/j4"; printf 'x y\n' > "$T/j5" +expect_out "genjac pair 1/3" "grams_a=2 grams_b=2 inter=1 union=3 jaccard=0.333333" "$JC" pair "$T/j1" "$T/j2" +expect_out "genjac pair 0.5 exact, case and punctuation folded" "grams_a=3 grams_b=3 inter=2 union=4 jaccard=0.500000" "$JC" pair "$T/j3" "$T/j4" +expect_out "genjac pair identical" "grams_a=2 grams_b=2 inter=2 union=2 jaccard=1.000000" "$JC" pair "$T/j1" "$T/j1" +expect_out "genjac pair under 3 words" "grams_a=0 grams_b=2 inter=0 union=2 jaccard=0.000000" "$JC" pair "$T/j5" "$T/j1" +printf '%s\n' "$T/j3" > "$T/hl"; printf '%s\n' "$T/j4" "$T/j5" > "$T/cl" +expect_out "genjac scan: 0.5 is not above 0.5" "heldout=1 corpus=2 threshold=0.5 max_jaccard=0.500000 above=0" "$JC" scan "$T/hl" "$T/cl" +if "$JC" scan --threshold 0.49 "$T/hl" "$T/cl" > "$T/jc.out"; then fail "genjac scan above threshold must exit 1"; elif grep -qx 'heldout=1 corpus=2 threshold=0.49 max_jaccard=0.500000 above=1' "$T/jc.out" && ! grep -q "$T/j3" "$T/jc.out"; then pass "genjac scan above=1 exits 1 and names no file without --show"; else fail "genjac scan above=1 output ($(cat "$T/jc.out"))"; fi +expect_out "genjac scan --show names the pair" "above $T/j3 $T/j4 0.500000" sh -c "\"$JC\" scan --threshold 0.49 --show \"$T/hl\" \"$T/cl\" | head -1" +"$JC" scan --threshold 1.5 "$T/hl" "$T/cl" >/dev/null 2>&1; [ $? = 2 ] && pass "genjac refuses threshold > 1 (exit 2)" || fail "genjac threshold > 1" +"$JC" scan --threshold 0.1234567 "$T/hl" "$T/cl" >/dev/null 2>&1; [ $? = 2 ] && pass "genjac refuses 7 fraction digits (exit 2)" || fail "genjac 7 fraction digits" +printf '%s\n\n' "$T/j3" > "$T/hlb"; "$JC" scan "$T/hlb" "$T/cl" >/dev/null 2>&1; [ $? = 2 ] && pass "genjac refuses a blank list line (exit 2)" || fail "genjac blank list line" +# ---- 2. famsig ---- +expect_out "canon flattens seq and sorts" "gs1:seq(array,loop(array,branch(arith)))" "$FS" canon 'seq(loop(branch(arith),array),array)' +expect_out "canon loop(seq(a,b)) = loop(a,b)" "gs1:loop(arith,array)" "$FS" canon 'loop(seq(array,arith))' +expect_out "nesting order matters" "gs1:branch(loop(arith))" "$FS" canon 'branch(loop(arith))' +expect_out "token devgen01 family" "gs1-bd9c105e5ae45397" "$FS" token 'loop(call(nested))' +expect_out "token devgen04 family" "gs1-e05d1c5dffd08b06" "$FS" token 'branch(loop(arith),nested)' +expect_out "token devgen09 family" "gs1-f63e09ec03a454ee" "$FS" token 'loop(loop(branch(arith)))' +expect_out "token devgen07 family" "gs1-30938c5c7504ef0e" "$FS" token 'seq(array,loop(array,branch(arith)))' +if "$FS" rule "$G/dev/META" >/dev/null 2>&1; then pass "famsig rule dev/META: RULE PASS"; else fail "famsig rule dev/META"; fi + +refuse "shape unknown primitive" "$FS" canon 'loop(sort)' +refuse "shape leaf with children" "$FS" canon 'arith(loop)' +refuse "shape container without child" "$FS" canon 'loop()' +refuse "shape seq with one child" "$FS" canon 'seq(arith)' +refuse "shape with a space" "$FS" canon 'loop(arith, array)' +refuse "shape deeper than 8" "$FS" canon 'loop(loop(loop(loop(loop(loop(loop(loop(loop(arith)))))))))' +m() { printf '%s\n' "$@" > "$T/m.txt"; echo "$T/m.txt"; } +refuse "meta count mismatch (R1)" "$FS" meta "$(m 'gen-meta 1' 'c1 corpus 2 0 1 0 0 0 loop(arith)')" +refuse "meta heldout line in a public file" "$FS" meta "$(m 'gen-meta 1' 'h1 heldout 1 1 0 0 0 0 branch(arith)')" +refuse "meta bad task id" "$FS" meta "$(m 'gen-meta 1' 'C1 corpus 1 0 1 0 0 0 loop(arith)')" +refuse "meta leading zero count" "$FS" meta "$(m 'gen-meta 1' 'c1 corpus 01 0 1 0 0 0 loop(arith)')" +refuse "meta double space" "$FS" meta "$(m 'gen-meta 1' 'c1 corpus 1 0 1 0 0 0 loop(arith)')" +printf 'gen-meta 1\nc1 corpus 1 0 1 0 0 0 loop(arith)' > "$T/nonl"; refuse "meta no final newline" "$FS" meta "$T/nonl" +printf 'gen-meta 1\nc1 corpus 1 0 1 0 0 0 loop(arith)\n# caf\xc3\xa9\n' > "$T/nonascii"; refuse "meta non-ASCII byte" "$FS" meta "$T/nonascii" +SALT=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef +refuse "split ids not ascending" "$FS" split "$(m 'gen-split 1' "salt $SALT" 'h2 heldout 1 1 0 0 0 0 branch(arith)' 'h1 heldout 1 1 0 0 0 0 branch(arith)')" +refuse "split with a comment" "$FS" split "$(m 'gen-split 1' "salt $SALT" '# x' 'h1 heldout 1 1 0 0 0 0 branch(arith)')" +refuse "split bad salt" "$FS" split "$(m 'gen-split 1' 'salt 0123' 'h1 heldout 1 1 0 0 0 0 branch(arith)')" +refuse "split corpus line" "$FS" split "$(m 'gen-split 1' "salt $SALT" 'c1 corpus 1 0 1 0 0 0 loop(arith)')" +printf 'gen-meta 1\nc1 corpus 1 1 1 0 0 0 loop(branch(arith))\nc2 corpus 0 0 0 1 0 0 array\n' > "$T/corpus.ok" +refuse "R2 duplicate id across files" "$FS" rule "$G/dev/META" "$(m 'gen-meta 1' 'devgen01 corpus 1 0 1 0 0 0 loop(arith)')" +refuse "R3 corpus uses a dev signature" "$FS" rule "$G/dev/META" "$(m 'gen-meta 1' 'c9 corpus 0 0 1 0 1 1 loop(call(nested))')" +printf 'gen-split 1\nsalt %s\nh1 heldout 0 0 0 0 1 1 call(nested)\n' "$SALT" > "$T/r4" +refuse "R4 held-out primitive absent from corpus" "$FS" rule "$T/corpus.ok" "$T/r4" +printf 'gen-split 1\nsalt %s\nh1 heldout 0 0 0 2 0 0 seq(array,array)\n' "$SALT" > "$T/r5" +refuse "R5 held-out family with one primitive" "$FS" rule "$T/corpus.ok" "$T/r5" +printf 'gen-split 1\nsalt %s\nh1 heldout 1 1 1 0 0 0 loop(branch(arith))\n' "$SALT" > "$T/r3h" +refuse "R3 held-out shares a corpus signature" "$FS" rule "$T/corpus.ok" "$T/r3h" +printf 'gen-split 1\nsalt %s\nh1 heldout 1 1 1 1 0 0 seq(array,branch(loop(arith)))\n' "$SALT" > "$T/hok" +if "$FS" rule "$T/corpus.ok" "$T/hok" >/dev/null 2>&1; then pass "rule positive control (valid corpus + split)"; else fail "rule positive control"; fi +if ! "$FS" rule "$T/corpus.ok" "$T/hok" 2>/dev/null | grep -q 'seq(array,branch'; then pass "rule hides held-out signatures without --show-heldout"; else fail "rule leaks held-out signatures"; fi +expect_out "coarse: nested read as arith" "gc1:loop(branch(arith))" "$FS" coarse 'loop(branch(nested))' +expect_out "coarse: duplicate siblings collapsed" "gc1:loop(branch(arith))" "$FS" coarse 'loop(branch(arith,arith))' +expect_out "coarse: top-level seq wrapper with leaves dropped" "gc1:loop(branch(arith))" "$FS" coarse 'seq(arith,loop(branch(arith)))' +expect_out "coarse: leaf inside a container kept" "gc1:loop(arith,branch(arith))" "$FS" coarse 'loop(branch(arith),arith)' +expect_out "coarse: two containers keep the seq" "gc1:seq(call(array),loop(arith))" "$FS" coarse 'seq(call(array),loop(nested))' +expect_out "coarse devgen family branch(loop(arith),nested)" "gc1:branch(arith,loop(arith))" "$FS" coarse 'branch(loop(arith),nested)' +printf 'gen-split 1\nsalt %s\nh1 heldout 0 1 1 0 0 1 loop(branch(nested))\n' "$SALT" > "$T/r6" +refuse_why "R6 held-out = corpus family with nested for arith" "FAIL R6" "$FS" rule "$T/corpus.ok" "$T/r6" +printf 'gen-split 1\nsalt %s\nh1 heldout 2 1 1 0 0 0 seq(arith,loop(branch(arith)))\n' "$SALT" > "$T/r6b" +refuse_why "R6 held-out = corpus family inside a seq wrapper" "FAIL R6" "$FS" rule "$T/corpus.ok" "$T/r6b" +if ! "$FS" rule "$T/corpus.ok" "$T/r6" 2>/dev/null | grep -q 'gc1:loop'; then pass "R6 failure hides the held-out coarse signature"; else fail "R6 failure leaks the held-out coarse signature"; fi +refuse_why "R6 corpus = dev family with arith for nested" "FAIL R6" "$FS" rule "$G/dev/META" "$(m 'gen-meta 1' 'c9 corpus 1 0 1 0 1 0 loop(call(arith))')" +printf 'gen-meta 1\nc1 corpus 1 1 1 0 0 0 loop(branch(arith))\nc2 corpus 0 0 0 1 0 0 array\nc3 corpus 1 0 1 1 0 0 seq(array,loop(arith))\n' > "$T/corpus.ok2" +printf 'gen-split 1\nsalt %s\nh1 heldout 2 1 1 0 0 0 loop(branch(arith),arith)\n' "$SALT" > "$T/r7" +refuse_why "R7 held-out with no novel coarse edge" "FAIL R7" "$FS" rule "$T/corpus.ok2" "$T/r7" +if "$FS" rule "$T/corpus.ok" "$T/r7" >/dev/null 2>&1; then pass "R6/R7 known limit: loop(branch(arith),arith) passes when loop>arith is new"; else fail "R7 limit control"; fi +printf 'gen-split 1\nsalt %s\nh1 heldout 1 1 2 1 0 0 seq(loop(branch(arith)),loop(array))\n' "$SALT" > "$T/r8" +refuse_why "R8 held-out top-level step = corpus family loop(branch(arith))" "FAIL R8" "$FS" rule "$T/corpus.ok" "$T/r8" +if ! "$FS" rule "$T/corpus.ok" "$T/r8" 2>/dev/null | grep -q 'seq(loop'; then pass "R8 failure hides the held-out shape"; else fail "R8 failure leaks the held-out shape"; fi +printf 'gen-split 1\nsalt %s\nh1 heldout 0 1 2 2 0 0 seq(loop(branch(array)),loop(array))\n' "$SALT" > "$T/r8ok" +if "$FS" rule "$T/corpus.ok" "$T/r8ok" >/dev/null 2>&1; then pass "R8 control: top-level steps that are no corpus family pass"; else fail "R8 positive control"; fi +refuse_why "R8 dev top-level step = corpus family loop(branch(arith))" "FAIL R8" "$FS" rule "$T/corpus.ok" "$(m 'gen-meta 1' 'd1 dev 1 1 1 1 1 0 seq(loop(branch(arith)),call(array))')" +if "$FS" rule "$T/corpus.ok" "$G/dev/META" >/dev/null 2>&1; then pass "R8 control: dev/META against a corpus with loop(branch(arith))"; else fail "R8 dev control"; fi +# blind re-declaration +printf 'gen-split 1\nsalt %s\nh1 heldout 1 1 1 1 0 0 seq(array,branch(loop(arith)))\nh2 heldout 0 0 1 1 1 0 loop(call(array))\n' "$SALT" > "$T/cmpA" +printf 'gen-split 1\nsalt %s\nh1 heldout 1 1 1 1 0 0 seq(branch(loop(arith)),array)\nh2 heldout 0 0 1 1 1 0 loop(call(array))\n' "$(printf '%s' "$SALT" | tr '0-9a-f' '1-9a-f0')" > "$T/cmpB" +expect_out "compare: same families, other salt and sibling order" "tasks=2 mismatches=0" "$FS" compare "$T/cmpA" "$T/cmpB" +sed 's/^h2 .*/h2 heldout 0 0 1 1 1 0 call(loop(array))/' "$T/cmpB" > "$T/cmpC" +if "$FS" compare "$T/cmpA" "$T/cmpC" > "$T/cmp.out" 2>&1; then fail "compare mismatch must exit 1"; elif [ "$(cat "$T/cmp.out")" = "tasks=2 mismatches=1" ]; then pass "compare: one mismatch, exit 1, no signature printed"; else fail "compare mismatch output ($(cat "$T/cmp.out"))"; fi +sed 's/^h2 /h3 /' "$T/cmpB" > "$T/cmpD"; refuse "compare: different task ids" "$FS" compare "$T/cmpA" "$T/cmpD" +head -3 "$T/cmpB" > "$T/cmpE"; refuse "compare: different task counts" "$FS" compare "$T/cmpA" "$T/cmpE" + +# ---- 3. gencheck ---- +# The GEN-3 corpus files that protocol.json pins (corpus META, record-set manifests) arrive with GEN-3's own +# PR. Until that is merged, they are taken from git at GEN3_COMMIT into a private overlay tree, and the real +# protocol.json is checked there; once they exist in this tree, this tree is checked directly. +GEN3_COMMIT=e521e818e42421884d5fd9826426bba56ad10531 +GEN3_FILES="tests/one_layer/generalization/corpus/META tests/one_layer/generalization/records/C.MANIFEST tests/one_layer/generalization/records/D.MANIFEST" +gen3_into() { # dest-root: copy the GEN-3 files from this tree, else from git at GEN3_COMMIT + local p ok=1 + for p in $GEN3_FILES; do + mkdir -p "$1/$(dirname "$p")" + if [ -f "$ROOT/$p" ]; then cp "$ROOT/$p" "$1/$p"; else git -C "$ROOT" show "$GEN3_COMMIT:$p" > "$1/$p" 2>/dev/null || ok=0; fi + done + [ "$ok" = 1 ] +} +tree_copy() { # dest-root: a private copy of every file gencheck reads + mkdir -p "$1" + (cd "$ROOT" && tar -cf - docs/one_layer/GEN_PROTOCOL_V1.md docs/one_layer/OMEGESE.md tests/one_layer/acceptance/HARNESS_INTERFACE_V2.md \ + tests/one_layer/acceptance/ola.sh tests/one_layer/generalization tools/exp445/PARAMS tools/exp445/arms tools/exp445/reader.sh \ + tools/exp445/sampling-gate.sh tools/exp445/weights.A.sha256 tools/gen/protocol) | (cd "$1" && tar -xf -) +} +LR="$ROOT" +if [ ! -f "$ROOT/tests/one_layer/generalization/corpus/META" ]; then + LR="$T/live"; tree_copy "$LR" + if gen3_into "$LR"; then pass "GEN-3 corpus files taken from git at ${GEN3_COMMIT:0:8} (not yet merged here)"; else fail "GEN-3 files not available from git (fetch origin gen/abstract)"; fi +fi +# P0: protocol.json with the corpus and record sets unfrozen, the base of the synthetic-corpus tests below +sed -E 's/("corpus": \{[^}]*"meta_sha256": )"[0-9a-f]{64}"/\1null/; s/"record_sets": \{[^}]*\}/"record_sets": {"prefix_ids": null, "prefix_sha256": null, "raw_manifest_sha256": null, "abstraction_manifest_sha256": null, "raw_manifest": null, "abstraction_manifest": null}/' "$P" > "$T/P0.json" +if "$GC" manifest "$P" "$LR" > "$T/gc.out" 2>&1 && tail -1 "$T/gc.out" | grep -qx 'GENCHECK PASS'; then pass "gencheck manifest protocol.json"; else fail "gencheck manifest protocol.json"; cat "$T/gc.out"; fi +if "$FS" rule "$LR/tests/one_layer/generalization/dev/META" "$LR/tests/one_layer/generalization/corpus/META" > "$T/dc.out" 2>&1 && grep -qx 'RULE PASS' "$T/dc.out"; then pass "famsig rule dev/META + GEN-3 corpus/META (R1..R8)"; else fail "famsig rule dev/META + GEN-3 corpus/META"; grep FAIL "$T/dc.out"; fi + +gmut() { # name expected-stderr-substring sed-or-perl-expression [root] + local n="$1" want="$2" ex="$3" r="${4:-$LR}" + sed -zE "$ex" "$P" > "$T/mut.json" + if cmp -s "$T/mut.json" "$P"; then fail "mutant did not apply: $n"; return; fi + if "$GC" manifest "$T/mut.json" "$r" > /dev/null 2> "$T/mut.err"; then fail "mutant not caught: $n"; return; fi + if grep -qF -- "$want" "$T/mut.err"; then pass "refused: $n"; else fail "refused for another reason: $n ($(cat "$T/mut.err"))"; fi +} +gmut "unknown top-level key" "unknown key extra" 's/^\{\n/{\n "extra": 1,\n/' +gmut "unknown nested key" "unknown key retrieval.top_n" 's/"k": 3,/"k": 3, "top_n": 3,/' +gmut "duplicate key" "duplicate key \"revision\"" 's/"revision": 1,/"revision": 1, "revision": 1,/' +gmut "missing key" "missing key issue" 's/ "issue": "aien-spark\/omega#476",\n//' +gmut "missing split heldout" "missing key splits.heldout" 's/,\n "heldout": \{[^}]*\}//' +gmut "missing split dev" "missing key splits.dev" 's/ "dev": \{[^]]*\][^]]*\]\n \},\n//' +gmut "changed split: heldout count" "splits.heldout.count changed" 's/"count": 60/"count": 59/' +gmut "changed split: min families" "splits.heldout.min_families changed" 's/"min_families": 6/"min_families": 5/' +gmut "changed split: dev task renamed" "devgen11" 's/"devgen10"\]/"devgen11"]/' +gmut "changed split: dev task dropped" "splits.dev.tasks: 9 tasks" 's/, "devgen10"\]/]/' +gmut "duplicate dev task id" "duplicate or unsorted task id devgen09" 's/"devgen10"\]/"devgen09"]/' +gmut "dev family list changed" "splits.dev.families" 's/"gs1:loop\(call\(nested\)\)", //' +gmut "digest drift: pinned file" "digest drift: tools/exp445/PARAMS" 's/("path": "tools\/exp445\/PARAMS", "sha256": ")a/\1b/' +gmut "digest drift: dev META" "digest drift: tests/one_layer/generalization/dev/META" 's/("meta_sha256": ")1/\12/' +gmut "pin removed" "required file tools/exp445/PARAMS is not pinned" 's/ \{"path": "tools\/exp445\/PARAMS"[^\n]*\n//' +gmut "pins out of order" "not strictly ascending" 's/( \{"path": "docs\/one_layer\/GEN_PROTOCOL_V1.md"[^\n]*\n)( \{"path": "docs\/one_layer\/OMEGESE.md"[^\n]*\n)/\2\1/' +gmut "unsafe pin path" "unsafe path" 's/"path": "tools\/exp445\/PARAMS"/"path": "..\/omega\/tools\/exp445\/PARAMS"/' +gmut "changed constant: delta" "decision.delta changed" 's/"delta": 0.10/"delta": 0.20/' +gmut "changed constant: k" "retrieval.k changed" 's/"k": 3/"k": 5/' +gmut "changed constant: max_tokens" "generation.max_tokens changed" 's/"max_tokens": 3072/"max_tokens": 4096/' +gmut "changed constant: masking on" "config.masking changed" 's/"masking": false/"masking": true/' +gmut "changed constant: floor_max" "decision.floor_max changed" 's/"floor_max": 3/"floor_max": 4/' +gmut "changed constant: G0 seed s8" "gates.g0.seed_values[7] changed" 's/476908\]/476909]/' +gmut "G0 seed list shortened" "missing frozen value gates.g0.seed_values[7]" 's/, 476908\]/]/' +gmut "changed constant: length-match tolerance" "retrieval.length_match_frac changed" 's/"length_match_frac": 0.15/"length_match_frac": 0.25/' +gmut "changed constant: near-duplicate threshold" "splits.heldout.near_dup_threshold changed" 's/"near_dup_threshold": 0.5/"near_dup_threshold": 0.6/' +gmut "changed constant: one request in flight" "generation.in_flight changed" 's/"in_flight": 1/"in_flight": 4/' +gmut "condition order changed" "conditions[0].name changed" 's/\{"name": "A", "prefix_examples": 1/{"name": "B", "prefix_examples": 1/' +gmut "extra condition" "conditions: 5 elements" 's/(\{"name": "D"[^\n]*)\n/\1,\n {"name": "E", "prefix_examples": 14, "retrieval": "raw"}\n/' +gmut "wrong type" "revision: type s" 's/"revision": 1/"revision": "1"/' +gmut "null where not allowed" "splits.dev.meta_sha256: type z" 's/"meta_sha256": "[0-9a-f]{64}"/"meta_sha256": null/' +gmut "record set frozen before corpus" "is set while the corpus META is not frozen" 's/("corpus": \{[^}]*"meta_sha256": )"[0-9a-f]{64}"/\1null/' +gmut "trailing bytes" "trailing bytes" 's/\}\n$/}\n}\n/' +gmut "non-ASCII byte" "ASCII only" 's/"question": "Does/"question": "D\xc3\xb6es/' +gmut "bad escape" "escape" 's/"issue": "aien-spark\/omega#476"/"issue": "aien-spark\\u002fomega#476"/' +gmut "empty array" "empty array" 's/"controls": \["A", "B", "C"\]/"controls": []/' + +# tree mutants: a private copy of every file gencheck reads +R="$T/root" +mkdir -p "$R" +(cd "$ROOT" && tar -cf - docs/one_layer/GEN_PROTOCOL_V1.md docs/one_layer/OMEGESE.md tests/one_layer/acceptance/HARNESS_INTERFACE_V2.md \ + tests/one_layer/acceptance/ola.sh tests/one_layer/generalization tools/exp445/PARAMS tools/exp445/arms tools/exp445/reader.sh \ + tools/exp445/sampling-gate.sh tools/exp445/weights.A.sha256 tools/gen/protocol) | (cd "$R" && tar -xf -) +gen3_into "$R"; if "$GC" manifest "$P" "$R" >/dev/null 2>&1; then pass "gencheck on the private tree copy"; else fail "gencheck on the private tree copy"; fi +mkdir "$R/tests/one_layer/generalization/dev/tasks/cases/devgen11" +if "$GC" manifest "$P" "$R" >/dev/null 2>"$T/e"; then fail "extra dev case directory not caught" +elif grep -q 'dev cases: 11 directories' "$T/e"; then pass "refused: extra dev case directory" +else fail "extra dev case directory: other reason ($(cat "$T/e"))"; fi +rmdir "$R/tests/one_layer/generalization/dev/tasks/cases/devgen11" +mkdir -p "$R/tests/one_layer/generalization/corpus" +CM="$R/tests/one_layer/generalization/corpus/META" +corpus_json() { # write $T/c.json with the corpus META digest set + local s; s="$(sha256sum "$CM" | cut -c1-64)" + sed "s/\"meta_sha256\": null/\"meta_sha256\": \"$s\"/" "$T/P0.json" > "$T/c.json" +} +printf 'gen-meta 1\nc9 corpus 0 0 1 0 1 1 loop(call(nested))\n' > "$CM"; corpus_json +if "$GC" manifest "$T/c.json" "$R" >/dev/null 2>"$T/e"; then fail "mutant not caught: corpus shares a dev signature"; else grep -qF 'R1..R8' "$T/e" && pass "refused: corpus family shared with dev (R3)" || fail "corpus/dev R3: other reason"; fi +printf 'gen-meta 1\ndevgen01 corpus 1 0 1 0 0 0 loop(arith)\n' > "$CM"; corpus_json +if "$GC" manifest "$T/c.json" "$R" >/dev/null 2>"$T/e"; then fail "mutant not caught: corpus reuses a dev task id"; else grep -qF 'R1..R8' "$T/e" && pass "refused: duplicate task id across splits (R2)" || fail "R2: other reason"; fi +printf 'gen-meta 1\nc1 dev 1 0 1 0 0 0 loop(arith)\n' > "$CM"; corpus_json +refuse "corpus META with a dev line" "$GC" manifest "$T/c.json" "$R" +# synthetic valid corpus: every primitive, no dev signature +{ + echo 'gen-meta 1' + echo 'c01 corpus 1 1 1 0 0 0 loop(branch(arith))' + echo 'c02 corpus 0 0 0 1 0 0 array' + echo 'c03 corpus 0 0 0 0 1 1 call(nested)' + echo 'c04 corpus 1 0 1 1 0 0 seq(array,loop(arith))' +} > "$CM"; corpus_json +if "$GC" manifest "$T/c.json" "$R" >/dev/null 2>&1; then pass "gencheck with a frozen synthetic corpus"; else fail "gencheck with a frozen synthetic corpus"; fi +s="$(sha256sum "$CM" | cut -c1-64)"; d="$(printf '%s' "$s" | tr '0-9a-f' '1-9a-f0')" +sed "s/$s/$d/" "$T/c.json" > "$T/c2.json" +refuse "corpus META digest drift" "$GC" manifest "$T/c2.json" "$R" + +# reviewer split check on a SYNTHETIC held-out split (test-only) +SP="$T/SPLIT"; PUB="$T/HELDOUT.pub" +shapes='branch(call(nested)) loop(call(array)) seq(array,branch(loop(nested))) branch(array,loop(nested)) call(loop(branch(arith))) seq(call(array),branch(nested))' +{ + echo 'gen-split 1'; echo "salt $SALT" + i=0 + for k in $(seq 1 10); do for sh in $shapes; do + i=$((i + 1)); id="$(printf 'q%02d' "$i")" + c="$(for p in arith branch loop array call nested; do printf '%s' "$sh" | grep -o "$p" | wc -l; done | tr '\n' ' ' | sed 's/ $//')" + echo "$id heldout $c $sh" + done; done +} > "$SP.unsorted" +{ head -2 "$SP.unsorted"; tail -n +3 "$SP.unsorted" | LC_ALL=C sort; } > "$SP" +mkpub() { # tasks-sha split-file + local out; out="$("$FS" split "$2")" || return 1 + printf 'gen-heldout-pub 1\ntasks_sha256 %s\nsplit_sha256 %s\nfamlist_sha256 %s\ntasks %s\nfamilies %s\n' "$1" \ + "$(echo "$out" | sed -n 's/^split_sha256=//p')" "$(echo "$out" | sed -n 's/^famlist_sha256=//p')" \ + "$(echo "$out" | sed -n 's/^tasks=//p')" "$(echo "$out" | sed -n 's/^families=//p')" + tail -n +3 "$2" | cut -d' ' -f1 | sed 's/^/task /' +} +TS=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +mkpub "$TS" "$SP" > "$PUB" +anchor() { # in-json out-json pub-file tasks-sha: anchor the held-out digests in a manifest copy + sed "s/\"pub_sha256\": null/\"pub_sha256\": \"$(sha256sum "$3" | cut -c1-64)\"/; s/\"tasks_sha256\": null/\"tasks_sha256\": \"$4\"/" "$1" > "$2" +} +spl() { # SPLIT HELDOUT.pub: gencheck split with the digests of that HELDOUT.pub anchored + anchor "$T/c.json" "$T/cs.json" "$2" "$TS" + "$GC" split "$T/cs.json" "$R" "$1" "$2" +} +if spl "$SP" "$PUB" > "$T/sp.out" 2>&1 && tail -1 "$T/sp.out" | grep -qx 'GENCHECK PASS'; then pass "gencheck split (synthetic 60 tasks, 6 families, R1..R8)"; else fail "gencheck split positive"; cat "$T/sp.out"; fi +if ! grep -q 'branch(call' "$T/sp.out"; then pass "gencheck split prints no held-out signature"; else fail "gencheck split leaks a held-out signature"; fi +refuse_why "split check with null held-out digests (fail closed)" "fail closed on null" "$GC" split "$T/c.json" "$R" "$SP" "$PUB" +anchor "$T/c.json" "$T/cn.json" "$PUB" "$TS"; sed -i 's/"tasks_sha256": "a\{64\}"/"tasks_sha256": null/' "$T/cn.json" +refuse_why "split check with only tasks_sha256 null (fail closed)" "fail closed on null" "$GC" split "$T/cn.json" "$R" "$SP" "$PUB" +anchor "$P" "$T/pn.json" "$PUB" "$TS"; refuse "split check without a frozen corpus" "$GC" split "$T/pn.json" "$R" "$SP" "$PUB" +sed 's/^famlist_sha256 ./famlist_sha256 0/' "$PUB" > "$T/p1"; cmp -s "$T/p1" "$PUB" && sed -i 's/^famlist_sha256 ./famlist_sha256 1/' "$T/p1" +refuse_why "HELDOUT.pub famlist digest wrong" "famlist" spl "$SP" "$T/p1" +sed '$d' "$PUB" > "$T/p2"; refuse "HELDOUT.pub missing a task line" spl "$SP" "$T/p2" +sed 's/^families 6$/families 7/' "$PUB" > "$T/p3"; refuse "HELDOUT.pub families count wrong" spl "$SP" "$T/p3" +sed 's/^task q60$/task q61/' "$PUB" > "$T/p4"; refuse "HELDOUT.pub task id differs" spl "$SP" "$T/p4" +sed "s/^salt .*/salt $(printf '%s' "$SALT" | tr '0-9a-f' '1-9a-f0')/" "$SP" > "$T/s5"; refuse "SPLIT salt changed after publication" spl "$T/s5" "$PUB" +head -61 "$SP" > "$T/s6"; mkpub "$TS" "$T/s6" > "$T/p6"; refuse "held-out split with 59 tasks" spl "$T/s6" "$T/p6" +{ head -2 "$SP"; tail -n +3 "$SP" | sed 's/ 0 1 0 1 1 1 seq(call(array),branch(nested))$/ 0 1 0 0 1 1 branch(call(nested))/'; } > "$T/s7" +mkpub "$TS" "$T/s7" > "$T/p7"; refuse_why "held-out split with 5 families" "famil" spl "$T/s7" "$T/p7" +{ head -2 "$SP"; tail -n +3 "$SP" | sed 's/^q01 .*/q01 heldout 1 1 1 0 0 0 loop(branch(arith))/'; } > "$T/s8" +mkpub "$TS" "$T/s8" > "$T/p8"; refuse_why "held-out family shared with corpus (R3)" "FAIL R3" spl "$T/s8" "$T/p8" +{ head -2 "$SP"; tail -n +3 "$SP" | sed 's/^q01 .*/q01 heldout 0 1 1 0 0 1 loop(branch(nested))/'; } > "$T/s9" +mkpub "$TS" "$T/s9" > "$T/p9"; refuse_why "held-out coarse family shared with corpus (R6)" "FAIL R6" spl "$T/s9" "$T/p9" +{ head -2 "$SP"; tail -n +3 "$SP" | sed 's/^q01 .*/q01 heldout 2 1 1 0 0 0 loop(branch(arith),arith)/'; } > "$T/s10" +mkpub "$TS" "$T/s10" > "$T/p10"; refuse_why "held-out family with no novel coarse edge (R7)" "FAIL R7" spl "$T/s10" "$T/p10" +{ head -2 "$SP"; tail -n +3 "$SP" | sed 's/^q01 .*/q01 heldout 1 1 2 1 0 0 seq(loop(branch(arith)),loop(array))/'; } > "$T/s11" +mkpub "$TS" "$T/s11" > "$T/p11"; refuse_why "held-out top-level step equal to a corpus family (R8)" "FAIL R8" spl "$T/s11" "$T/p11" +anchor "$T/c.json" "$T/c3.json" "$PUB" "$(printf '%s' "$TS" | tr a b)" +refuse_why "HELDOUT.pub tasks_sha256 differs from protocol" "tasks_sha256 mismatch" "$GC" split "$T/c3.json" "$R" "$SP" "$PUB" +anchor "$T/c.json" "$T/c4.json" "$PUB" "$TS"; sed -i 's/"pub_sha256": "\([0-9a-f]\)/"pub_sha256": "f\1/; s/"pub_sha256": "\([0-9a-f]\{64\}\)[0-9a-f]"/"pub_sha256": "\1"/' "$T/c4.json" +refuse_why "HELDOUT.pub differs from the anchored pub_sha256" "digest drift: HELDOUT.pub" "$GC" split "$T/c4.json" "$R" "$SP" "$PUB" + +# run-ready and seal-open on a fully frozen SYNTHETIC manifest (test-only values) +refuse_why "run-ready on the committed protocol.json (nulls pending)" "GENCHECK FAIL" "$GC" run-ready "$P" "$LR" +{ + echo 'gen-meta 1' + echo 'c01 corpus 1 1 1 0 0 0 loop(branch(arith))' + echo 'c02 corpus 0 0 0 1 0 0 array' + echo 'c03 corpus 0 0 0 0 1 1 call(nested)' + echo 'c04 corpus 1 0 1 1 0 0 seq(array,loop(arith))' + for i in 05 06 07 08 09 10 11 12 13 14; do echo "c$i corpus 1 1 1 0 0 0 loop(branch(arith))"; done +} > "$CM"; corpus_json +mkdir -p "$R/records"; printf 'raw manifest (test)\n' > "$R/records/MANIFEST.C"; printf 'abstraction manifest (test)\n' > "$R/records/MANIFEST.D" +ifok=1 +ic="" +for ic in $(sed -nE 's/.*"path": "([^"]*)", "commit": "([0-9a-f]{40})".*/\2:\1/p' "$P"); do + mkdir -p "$R/$(dirname "${ic#*:}")"; git -C "$ROOT" show "$ic" > "$R/${ic#*:}" 2>/dev/null || ifok=0 +done +[ "$ifok" = 1 ] && [ -n "$ic" ] && pass "interface files available from git at their pinned commits (read from protocol.json)" || fail "interface files not available from git (fetch origin)" +ids="$(for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do printf '"c%s", ' "$i"; done | sed 's/, $//')" +hC="$(sha256sum "$R/records/MANIFEST.C" | cut -c1-64)"; hD="$(sha256sum "$R/records/MANIFEST.D" | cut -c1-64)" +sed -E "s|\"record_sets\": \{[^}]*\}|\"record_sets\": {\"prefix_ids\": [$ids], \"prefix_sha256\": \"$(printf '%064d' 7)\", \"raw_manifest_sha256\": \"$hC\", \"abstraction_manifest_sha256\": \"$hD\", \"raw_manifest\": \"records/MANIFEST.C\", \"abstraction_manifest\": \"records/MANIFEST.D\"}|" "$T/c.json" > "$T/f0.json" +anchor "$T/f0.json" "$T/F.json" "$PUB" "$TS" +if "$GC" run-ready "$T/F.json" "$R" > "$T/rr.out" 2>&1 && tail -1 "$T/rr.out" | grep -qx 'GENCHECK PASS'; then pass "run-ready on a fully frozen synthetic manifest"; else fail "run-ready positive"; tail -3 "$T/rr.out"; fi +fmut() { # name expected sed-expression [cmd]: mutate F.json, expect run-ready refusal with that reason + sed -zE "$3" "$T/F.json" > "$T/fm.json" + if cmp -s "$T/fm.json" "$T/F.json"; then fail "mutant did not apply: $1"; return; fi + refuse_why "$1" "$2" "$GC" "${4:-run-ready}" "$T/fm.json" "$R" +} +fmut "run-ready with prefix_sha256 null" "not run-ready: record_sets.prefix_sha256 is null" 's/"prefix_sha256": "[0-9]{64}"/"prefix_sha256": null/' +fmut "run-ready without the genret-cli-v3 interface" "retrieval contract genret-cli-v3 is not pinned" 's/,\n? \{"name": "genret-cli-v3"[^}]*\}//' +fmut "record-set path set, digest null" "must both be null or both be set" 's/"raw_manifest_sha256": "[0-9a-f]{64}"/"raw_manifest_sha256": null/' manifest +fmut "record-set manifest digest drift" "digest drift: record-set manifest records/MANIFEST.D" "s/\"abstraction_manifest_sha256\": \"$hD\"/\"abstraction_manifest_sha256\": \"$hC\"/" manifest +fmut "record-set manifest path missing" "record-set manifest missing" 's|"raw_manifest": "records/MANIFEST.C"|"raw_manifest": "records/MANIFEST.X"|' manifest +fmut "prefix id not a corpus task" "is not a corpus task" 's/"c14"\]/"c99"]/' manifest +mv "$R/tools/gen/retrieval/CONTRACT.md" "$T/contract.keep" +refuse_why "run-ready with an interface file missing from the tree" "is not in this tree" "$GC" run-ready "$T/F.json" "$R" +if "$GC" manifest "$T/F.json" "$R" 2>/dev/null | grep -q 'pending merge'; then pass "manifest mode still allows a pending interface (run-ready does not)"; else fail "manifest mode pending interface"; fi +mv "$T/contract.keep" "$R/tools/gen/retrieval/CONTRACT.md" +mkopen() { # protocol-sha tasks-sha pub-sha > OPEN + printf 'gen-open 1\nomega_commit %s\nprotocol_sha256 %s\ntasks_sha256 %s\nheldout_pub_sha256 %s\n' "$(printf '%040d' 1)" "$1" "$2" "$3" +} +hF="$(sha256sum "$T/F.json" | cut -c1-64)"; hP="$(sha256sum "$PUB" | cut -c1-64)" +mkopen "$hF" "$TS" "$hP" > "$T/OPEN" +if "$GC" open "$T/F.json" "$R" "$T/OPEN" > "$T/op.out" 2>&1 && tail -1 "$T/op.out" | grep -qx 'GENCHECK PASS'; then pass "gencheck open: OPEN receipt matches the frozen manifest"; else fail "gencheck open positive"; tail -3 "$T/op.out"; fi +mkopen "$(printf '%s' "$hF" | tr '0-9a-f' '1-9a-f0')" "$TS" "$hP" > "$T/O1"; refuse_why "OPEN with another protocol.json sha256" "protocol_sha256" "$GC" open "$T/F.json" "$R" "$T/O1" +mkopen "$hF" "$(printf '%s' "$TS" | tr a b)" "$hP" > "$T/O2"; refuse_why "OPEN with another tasks_sha256" "OPEN: tasks_sha256 differs" "$GC" open "$T/F.json" "$R" "$T/O2" +mkopen "$hF" "$TS" "$(printf '%s' "$hP" | tr '0-9a-f' '1-9a-f0')" > "$T/O3"; refuse_why "OPEN with another heldout_pub_sha256" "OPEN: heldout_pub_sha256 differs" "$GC" open "$T/F.json" "$R" "$T/O3" +head -4 "$T/OPEN" > "$T/O4"; refuse_why "OPEN with 4 lines" "exactly 5 lines" "$GC" open "$T/F.json" "$R" "$T/O4" +sed 's/^gen-open 1$/gen-open 2/' "$T/OPEN" > "$T/O5"; refuse_why "OPEN with another header" "exactly 5 lines" "$GC" open "$T/F.json" "$R" "$T/O5" +sed -E 's/"prefix_sha256": "[0-9]{64}"/"prefix_sha256": null/' "$T/F.json" > "$T/Fn.json"; mkopen "$(sha256sum "$T/Fn.json" | cut -c1-64)" "$TS" "$hP" > "$T/O6" +refuse_why "OPEN over a manifest with a null digest" "is null" "$GC" open "$T/Fn.json" "$R" "$T/O6" + +# ---- 4. dev pack ---- +PK="$ROOT/tests/one_layer/generalization/dev/pack_dev.sh" +if bash "$PK" "$T/pack1" >/dev/null && bash "$PK" "$T/pack2" >/dev/null && cmp -s "$T/pack1/tasks.tar" "$T/pack2/tasks.tar"; then pass "dev pack reproducible"; else fail "dev pack reproducible"; fi +want="$(sed -n 's/.*"pack_sha256": "\([0-9a-f]\{64\}\)".*/\1/p' "$P")" +if [ "$(cut -c1-64 "$T/pack1/TASKS.sha256")" = "$want" ]; then pass "dev pack sha256 = splits.dev.pack_sha256"; else fail "dev pack sha256 differs from protocol.json"; fi +cp -r "$ROOT/tests/one_layer/generalization/dev" "$T/devneg" +sed -i '1s/.*/1/' "$T/devneg/tasks/cases/devgen04/hidden/expected" +if bash "$T/devneg/pack_dev.sh" "$T/packneg" >/dev/null 2>&1; then fail "wrong hidden/expected not refused"; else pass "refused: dev pack with one wrong expected line (oracle check)"; fi +if [ "${GEN_TASKS_CHECK:-0}" = 1 ]; then + # ola.sh writes evidence under its own repo root and OLA_EVIDENCE_ROOT forces RUN INVALID (ola.sh line 27), so it + # runs in a private detached git worktree of HEAD under $T; that worktree (with its evidence and build output) is + # removed afterwards. Nothing in this checkout's evidence/ tree is written or deleted. + WT="$T/wt" + if git -C "$ROOT" worktree add -q --detach "$WT" HEAD 2>"$T/wt.err"; then + # OUT_DIR from an outer make call would reach ola.sh's own make through MAKEFLAGS; it is dropped so ola + # builds into the worktree's build/one_layer, where it looks for its tools. + if env -u MAKEFLAGS -u MFLAGS -u MAKELEVEL -u OUT_DIR bash "$WT/tests/one_layer/acceptance/ola.sh" tasks-check "$T/pack1/tasks.tar" "$T/pack1/TASKS.sha256" 2>"$T/tc.err" | grep -q 'RUN VALID'; then pass "ola.sh tasks-check dev pack in a private worktree of HEAD: RUN VALID"; else fail "ola.sh tasks-check dev pack (private worktree)"; tail -5 "$T/tc.err"; fi + git -C "$ROOT" worktree remove --force "$WT" || fail "private worktree not removed: $WT" + else + fail "git worktree add for the tasks-check"; cat "$T/wt.err" + fi +fi + +echo "test-gen-protocol: $ok ok, $bad failed" +[ "$bad" = 0 ]