omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / gen / retrieval / CONTRACT.md
| 1 | # genret: file-backed retrieval provider, CLI contract `genret-cli-v3` |
| 2 | |
| 3 | GEN-4 (aien-spark/omega#479, master #475). Owner: Agent E. Consumers: the GEN runner (Agent C, through this |
| 4 | CLI only) and the Cortex provider (aien-spark/aien-sovereign-core#556), which must reproduce `selected.tsv`, |
| 5 | `excluded.tsv` and `suffix.txt` byte for byte from the same corpus and query. Every rule below is integer |
| 6 | arithmetic or byte comparison so a second implementation can match it exactly. Any change to an observable |
| 7 | output is a new contract version. |
| 8 | |
| 9 | Version history: |
| 10 | - v1 (commit c0c55920) read a provisional record format `gen-record v0`. It was never used on a real corpus. |
| 11 | - v2 (commit a4878d69) reads Agent D's record schema v1 (`gen-record 1`, tools/gen/abstract/RECORD_SCHEMA.md, |
| 12 | sha256 9be17b6bc6f97c7420ced722bb258c420944bd87461097163df0992112ae6b53) and validates every record with D's |
| 13 | `recheck`. |
| 14 | - v3 (this file), after the PR #485 review: |
| 15 | - One heading for both kinds, so C and D suffixes differ only in record content (section 6). |
| 16 | - Length-matched pads with as many whole records as it takes to reach the target, and reports why it |
| 17 | stopped (section 6). |
| 18 | - New required `--recheck-sha256`. genret refuses any other validator binary (new fatal reason |
| 19 | `recheck_untrusted`) and names the validator in the receipt. |
| 20 | - Re-review fold-in, with outputs for valid inputs unchanged: genret is bound to the recheck binary |
| 21 | built in the same make run (section 2), and directories are fsynced around the final rename. |
| 22 | - Task files are opened once and read from the same descriptor. |
| 23 | - Outputs appear atomically. |
| 24 | - New fatal reason `internal`. |
| 25 | - New receipt keys `recheck_sha256`, `recheck_source_sha256`, `pad_reason`, `unpadded_bytes` and |
| 26 | `length_gap`. `pad` is now a count. |
| 27 | |
| 28 | Scoring, ranking, tokenization, eligibility and the record block format are unchanged from v2. |
| 29 | |
| 30 | Arms C and D call the same command with the same `--k`, `--cap-bytes`, mode and rendering. The only |
| 31 | difference is `--kind raw` (C) versus `--kind abstraction` (D), and therefore which records are indexed. |
| 32 | |
| 33 | ## 1. Command |
| 34 | |
| 35 | ``` |
| 36 | genret query --corpus <records/MANIFEST> --manifest-sha256 <64 lowercase hex> |
| 37 | --recheck <recheck binary> --recheck-sha256 <64 lowercase hex> |
| 38 | --kind raw|abstraction --k <1..64> --cap-bytes <1..1048576> |
| 39 | --requirement <file> --prompt <file> --out <new dir> |
| 40 | [--exclude-family <family>]... [--mode normal|irrelevant|empty|length-matched] |
| 41 | [--target-bytes <1..cap>] (required with, and only with, --mode length-matched) |
| 42 | genret version prints "genret-cli-v3" |
| 43 | ``` |
| 44 | |
| 45 | - Exit 0: success (an empty selection is a success). Exit 2: fatal (section 7). `--out` is not created. |
| 46 | - `--out` must not exist. genret writes all files into a new sibling directory `<out>.tmp.<pid>` and |
| 47 | renames it to `--out` without replacing anything (Linux `renameat2` with `RENAME_NOREPLACE`), so `--out` |
| 48 | either does not exist or holds every output file. Each file, the temp directory, and after the rename the |
| 49 | parent directory are fsynced. If that last fsync fails, `--out` is removed again and the run is fatal |
| 50 | `write_failed`. If genret is killed before the rename, a `<out>.tmp.<pid>` directory may remain. It is |
| 51 | never `--out`. |
| 52 | - Default mode: `normal`. Each option except `--exclude-family` at most once. Unknown options are fatal. |
| 53 | - `--recheck`: Agent D's validator. `make gen-retrieval` builds `build/gen_retrieval/recheck` from the |
| 54 | pinned `recheck.c`, and writes its sha256 to `build/gen_retrieval/recheck.sha256`. That value is what |
| 55 | callers pass as `--recheck-sha256`. The genret from the same make run accepts only that binary |
| 56 | (section 2). |
| 57 | |
| 58 | ## 2. Corpus input |
| 59 | |
| 60 | - Layout: RECORD_SCHEMA.md section 5. `--corpus` is `records/MANIFEST`; `--manifest-sha256` is the frozen |
| 61 | record-set digest (`records/SET.sha256`). Record paths and `receipts/` resolve against the MANIFEST's |
| 62 | directory (made absolute). |
| 63 | - `MANIFEST`: one line per record, `<64 lowercase hex sha256 of the record file> <relative path>\n` (two |
| 64 | spaces, `sha256sum` format), at least 1 and at most 65536 lines, final newline required, no blank lines, no |
| 65 | comments. Path components match `[A-Za-z0-9._-]+`, none is `.` or `..`, no leading `/`. Duplicate paths are |
| 66 | fatal. Files not listed are never read. |
| 67 | - sha256 of the MANIFEST bytes must equal `--manifest-sha256`; every record's bytes must equal its manifest |
| 68 | digest (checked when read, and again after recheck has read the file). |
| 69 | - Validator pin: before reading any task or record file, genret opens `--recheck` once and hashes its |
| 70 | bytes. If the sha256 differs from `--recheck-sha256`, the run stops with fatal `recheck_untrusted`. If the |
| 71 | file is missing or unreadable, the reason is `recheck_failed`. Every batch executes that same open file |
| 72 | (`fexecve`), so the program that runs is the one that was hashed. |
| 73 | - Validator binding: the make run that builds genret first checks `recheck.c` against its pinned sha256, |
| 74 | builds `build/gen_retrieval/recheck` from it, and then compiles both digests into genret: the source digest |
| 75 | and the digest of that binary. genret also refuses (`recheck_untrusted`) any `--recheck` whose sha256 is |
| 76 | not the built-in binary digest, even when it matches `--recheck-sha256`. The receipt records |
| 77 | `recheck_sha256` (the binary that ran) and `recheck_source_sha256` (the verified `recheck.c` it was built |
| 78 | from), so both are true claims. |
| 79 | - Test builds: `genret_anyrecheck` is compiled without the binary binding so the tests can drive fake |
| 80 | validators. Its receipt always says `recheck_source_sha256=unverified`. Its other outputs equal the |
| 81 | production build's, and the tests check that. Experiment runs use `genret` only. |
| 82 | - Every listed record is passed to `recheck --receipts <MANIFEST dir>/receipts <record>...` (batches of 128, |
| 83 | manifest order). recheck checks schema v1 sections 1 to 3 and that the receipt file exists, is this |
| 84 | record's and hashes to its id. It must exit 0 and print exactly `OK <id>` per record, in order, with the id |
| 85 | genret read; exit 1 is fatal `invalid_record` with recheck's first REFUSED line; anything else is fatal |
| 86 | `recheck_failed`. `source` digests (`recheck --root`) are not checked: the manifest pin covers the record |
| 87 | bytes, and D's `verify` covers derivation. |
| 88 | - Fields genret reads from a record: `id kind split family validity entry params ret desc` (first line |
| 89 | `gen-record 1`, each of these once) and the body (the bytes after the line `body\n` to end of file). All |
| 90 | other key lines (signature, receipt, ancestry, source, pre, post, example, limit, counter, fingerprint, |
| 91 | differs) are neither indexed nor rendered, so C and D show the same skeleton. |
| 92 | |
| 93 | ## 3. Query (public requirement + prompt only) |
| 94 | |
| 95 | - `--requirement`: at most 4096 bytes, no NUL; exactly one line each matching `^entry( |$)`, `^params( |$)`, |
| 96 | `^ret( |$)` (the same lines `tools/exp445/reader.sh user_msg` shows the model). Query text from it = the |
| 97 | three values (the line after the key and one space), nothing else from the file. |
| 98 | - `--prompt`: 1..4096 bytes, only `\n` and 0x20..0x7E (HARNESS_INTERFACE_V2 section 3). Query text = all of it. |
| 99 | - Each task file is opened once. genret takes the resolved absolute path of that open file |
| 100 | (`/proc/self/fd/N`), refuses it if any component is exactly `hidden` (fatal `hidden_path`), and refuses |
| 101 | anything but a regular file (`requirement_invalid` / `prompt_invalid`). It then reads from the same |
| 102 | descriptor, so a symlink swapped in after the check changes nothing. |
| 103 | - The match is byte-exact and case-sensitive: `Hidden` is not refused. |
| 104 | - A hard link to a hidden file placed outside `hidden/` cannot be detected this way. |
| 105 | - genret opens no other task file. |
| 106 | - Query terms = the DISTINCT tokens (section 4) of the requirement values and the prompt, sorted by byte |
| 107 | order; written to `query.txt`, one per line. |
| 108 | |
| 109 | ## 4. Tokenization |
| 110 | |
| 111 | Lowercase ASCII `A-Z` to `a-z`; a token is a maximal run of bytes in `[a-z0-9]`; every other byte separates. |
| 112 | Record document text = the values of entry, params, ret, desc, then the body (each tokenized separately and |
| 113 | the token lists concatenated; field boundaries separate tokens). `dl` = number of tokens (with repeats). |
| 114 | |
| 115 | ## 5. Eligibility and indexing |
| 116 | |
| 117 | For every manifest record, in this order, the first failing check is its exclusion reason: |
| 118 | |
| 119 | 1. `kind:<kind>`: kind differs from `--kind` |
| 120 | 2. `split:<split>`: split is not `corpus` (schema v1: `probe` records are never eligible) |
| 121 | 3. `validity:<validity>`: validity is not `verified` (schema v1: `unverified`) |
| 122 | 4. `family_excluded:<family>`: one of its families is named by `--exclude-family` (first such, record order) |
| 123 | 5. `poisoned:fence`: desc or body contains the three bytes "```" (could escape its code block) |
| 124 | |
| 125 | Records that pass all five form the index: `N` records, `T` = sum of their `dl`, `df(t)` = number of index |
| 126 | records containing term `t`. Excluded records do not affect any score. A record is eligible only through a |
| 127 | MANIFEST whose sha256 is the pinned one (RECORD_SCHEMA.md section 5). |
| 128 | |
| 129 | Only the fence is screened here. `desc` is rendered as plain prose outside the code block (section 6), so |
| 130 | its wording reaches the model unfenced. Its content is owned by the record producers and by recheck's |
| 131 | schema checks (one printable line), not by genret. |
| 132 | |
| 133 | ## 6. Scoring, ranking, selection |
| 134 | |
| 135 | Integer BM25, k1 = 6/5, b = 3/4, log base 2, scores in units of 2^-16. For index record r and query term t |
| 136 | with `tf` = occurrences of t in r (only terms with tf > 0 and therefore df >= 1 contribute): |
| 137 | |
| 138 | ``` |
| 139 | idf(t) = log2q16(2N + 2, 2 df(t) + 1) |
| 140 | part(t,r) = floor( idf(t) * tf * 44 * T / (20 * T * tf + 6 * T + 18 * dl(r) * N) ) (128-bit intermediate) |
| 141 | score(r) = sum of part(t,r) over the distinct query terms t |
| 142 | |
| 143 | log2q16(a, b): requires a >= b >= 1, a < 2^32; 64-bit unsigned arithmetic |
| 144 | k = 0; while a >= 2b: b = 2b, k = k + 1 |
| 145 | y = floor(a * 2^30 / b) (2^30 <= y < 2^31) |
| 146 | r = k * 2^16 |
| 147 | for i = 15 down to 0: y = floor(y * y / 2^30); if y >= 2^31: y = floor(y / 2), r = r + 2^i |
| 148 | return r |
| 149 | ``` |
| 150 | |
| 151 | (This is BM25 with idf = ln(1 + (N - df + 0.5)/(df + 0.5)), rescaled by 1/ln 2 and multiplied out by 20T.) |
| 152 | |
| 153 | Rendering (exact bytes, the same for both kinds: a C suffix and a D suffix with the same number of records |
| 154 | differ only in the entry, params, ret, desc and body values): |
| 155 | |
| 156 | ``` |
| 157 | H = "\nVerified reference code from earlier tasks, most relevant first.\n" (66 bytes) |
| 158 | block(i) = "\nRecord " i ".\n\n" "entry " entry "\n" "params" (" " param)* "\n" "ret " ret "\n" "\n" desc "\n\n```\n" body "```\n" |
| 159 | suffix = "" when nothing is selected, else H block(1) ... block(n) |
| 160 | ``` |
| 161 | |
| 162 | `i` is the decimal position in the suffix (1-based). `bytes` of a record = length of its block. |
| 163 | |
| 164 | Order and selection by mode: |
| 165 | |
| 166 | - `normal`: index records with score 0 are excluded (`no_match`). The rest are ordered by score descending, |
| 167 | then id ascending (unsigned byte compare, a proper prefix sorts first). Walk that order; stop at the first |
| 168 | record that would make the count exceed k or the suffix length exceed cap-bytes. That record and every |
| 169 | later one are excluded with `beyond_k` (count already k) or `cap_bytes`. Records are never truncated. |
| 170 | - `irrelevant` (negative control): order ALL index records by score ascending, then id ascending; the same |
| 171 | walk, k and cap. Score-0 records are not excluded. |
| 172 | - `empty` (negative control): nothing selected; every index record is excluded with `ablation_empty`. |
| 173 | Corpus checks and fatal rules are unchanged. |
| 174 | - `length-matched` (preregistered ablation: "C with more raw solutions, length-matched"). |
| 175 | `--target-bytes` is the other arm's realised `suffix_bytes`, normally D's at the same k and cap, and must |
| 176 | be <= `--cap-bytes`. First the `normal` walk; call its suffix length U (`unpadded_bytes`). |
| 177 | - If U >= target: nothing is added. The receipt shows `pad=none` and `pad_reason=c_not_shorter`, and the |
| 178 | selection and suffix are exactly `normal`'s. |
| 179 | - If U < target: walk the records logged `beyond_k` or `cap_bytes` in rank order (rank k+1, k+2, ...). |
| 180 | Skip any whose block at the next position (with H if nothing is selected yet) would push the suffix |
| 181 | past `--cap-bytes`; a skipped record keeps its reason. Append the others whole, with role `pad`. Stop |
| 182 | as soon as the suffix length is >= target (`pad_reason=reached`), or when the candidates run out |
| 183 | (`pad_reason=exhausted`, still short of the target). `pad` = the number appended, or `none`. |
| 184 | - The receipt always gives `unpadded_bytes`, `target_bytes`, the final `suffix_bytes` and |
| 185 | `length_gap` = suffix_bytes - target_bytes, signed. When reached, `length_gap` is the overshoot (>= 0, |
| 186 | smaller than the last pad's block). For c_not_shorter it is >= 0. For exhausted it is < 0. |
| 187 | - Known property, by design: the pads are C's next-most-relevant raw records. Padded C therefore gets |
| 188 | more records and more on-topic content, not only more bytes. Its gain cannot be put down to length |
| 189 | alone. That bias favours C, which keeps a "D still wins" result conservative. The length-neutral |
| 190 | control is `irrelevant`. |
| 191 | |
| 192 | ## 7. Fatal (exit 2, one stderr line `genret: FATAL <reason> <detail>`, no output) |
| 193 | |
| 194 | - `usage`, `manifest_unreadable`, `manifest_malformed`, `manifest_digest_mismatch` |
| 195 | - `record_missing`: a listed file is absent or unreadable |
| 196 | - `stale_digest`: record bytes differ from the manifest digest |
| 197 | - `invalid_record`: recheck refused the record, or genret cannot read a field it needs |
| 198 | - `recheck_failed`: recheck is missing, crashed, or its output does not match the records |
| 199 | - `recheck_untrusted`: the `--recheck` file's sha256 is not `--recheck-sha256` |
| 200 | - `duplicate_id`, `requirement_invalid`, `prompt_invalid`, `hidden_path`, `out_exists`, `write_failed` |
| 201 | - `internal`: out of memory, or an internal consistency check failed |
| 202 | |
| 203 | A record is never replaced by another one: integrity failures stop the run. |
| 204 | |
| 205 | ## 8. Outputs in `--out` |
| 206 | |
| 207 | | file | content | |
| 208 | |---|---| |
| 209 | | `selected.tsv` | header `rank\tid\tdigest\tscore\tbytes\trole`; one row per selected record in suffix order; role `top` or `pad` | |
| 210 | | `excluded.tsv` | header `id\tdigest\tscore\treason`; every manifest record not selected, sorted by id (byte order); score `-` when not indexed | |
| 211 | | `suffix.txt` | the exact bytes the runner appends in the variable suffix (may be empty) | |
| 212 | | `query.txt` | the query terms (section 3) | |
| 213 | | `receipt.txt` | `key=value` lines in a fixed order (listed below) | |
| 214 | | `SHA256SUMS` | `sha256sum` lines of the five files above, in that file order | |
| 215 | |
| 216 | `receipt.txt` keys, in this order: |
| 217 | 1. `contract=genret-cli-v3` |
| 218 | 2. `mode` |
| 219 | 3. `kind` |
| 220 | 4. `k` |
| 221 | 5. `cap_bytes` |
| 222 | 6. `target_bytes` (`-` if unused) |
| 223 | 7. `exclude_family` (comma list or `-`) |
| 224 | 8. `manifest_sha256` |
| 225 | 9. `recheck_sha256` |
| 226 | 10. `recheck_source_sha256` |
| 227 | 11. `requirement_sha256` |
| 228 | 12. `prompt_sha256` |
| 229 | 13. `query_sha256` |
| 230 | 14. `records` |
| 231 | 15. `indexed` |
| 232 | 16. `total_tokens` (T) |
| 233 | 17. `selected` |
| 234 | 18. `pad` (`-` unless length-matched; else `none` or the count of pad records) |
| 235 | 19. `pad_reason` (`-`, `c_not_shorter`, `reached` or `exhausted`) |
| 236 | 20. `unpadded_bytes` (`-` unless length-matched) |
| 237 | 21. `suffix_bytes` |
| 238 | 22. `length_gap` (`-` unless length-matched; else signed decimal) |
| 239 | 23. `suffix_sha256` |
| 240 | 24. `selected_sha256` |
| 241 | 25. `excluded_sha256` |
| 242 | |
| 243 | Digests are lowercase hex sha256. No timestamps, paths or host data in any output. |