interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tools / gen / protocol / famsig_lib.h

574 lines · 24887 bytes Raw Blame History
1/*
2 * famsig_lib.h -- GEN family-signature contract v1 (aien-spark/omega#476), shared by famsig.c and
3 * gencheck.c. The contract is tests/one_layer/generalization/families/FAMSIG_V1.md; this file is its
4 * only implementation. Header-only (static functions) so both tools compile from one source.
5 *
6 * Shape grammar (no spaces, at most FS_MAXLEN bytes, depth at most FS_MAXDEPTH, at most FS_MAXNODES nodes):
7 * node := leaf | container "(" list ")" | "seq(" node "," list ")"
8 * list := node ("," node)*
9 * leaf := "arith" | "array" | "nested"
10 * container := "branch" | "call" | "loop"
11 * Canonical form: a "seq" directly inside a container or inside another "seq" is flattened into it;
12 * every child list is sorted by byte order of the children's canonical strings (duplicates kept).
13 * Signature = "gs1:" + canonical form.
14 */
15#ifndef GEN_FAMSIG_LIB_H
16#define GEN_FAMSIG_LIB_H
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include "sha256.h"
21
22#define FS_NPRIM 6
23#define FS_SEQ 6
24#define FS_MAXLEN 512
25#define FS_MAXDEPTH 8
26#define FS_MAXNODES 64
27#define FS_MAXCOUNT 64
28#define FS_MAXID 40
29#define FS_SIGMAX (FS_MAXLEN + 8)
30
31/* fixed order: the order of the six count fields of a metadata line */
32static const char *const fs_prim[FS_NPRIM + 1] = {"arith", "branch", "loop", "array", "call", "nested", "seq"};
33
34static int fs_is_container(int k) { return k == 1 || k == 2 || k == 4; }
35static int fs_is_leaf(int k) { return k == 0 || k == 3 || k == 5; }
36
37typedef struct {
38 int kind;
39 int nkid;
40 int kid[FS_MAXNODES];
41} FsNode;
42
43typedef struct {
44 FsNode node[FS_MAXNODES];
45 int nnode;
46 int root;
47 int count[FS_NPRIM]; /* primitive occurrences (seq not counted) */
48 char canon[FS_SIGMAX]; /* canonical form, no "gs1:" prefix */
49 char err[160];
50} FsShape;
51
52typedef struct {
53 const char *s;
54 size_t i;
55 FsShape *sh;
56} FsParser;
57
58static int fs_word(FsParser *p)
59{
60 for (int k = 0; k <= FS_NPRIM; k++) {
61 size_t n = strlen(fs_prim[k]);
62 if (!strncmp(p->s + p->i, fs_prim[k], n)) {
63 char c = p->s[p->i + n];
64 if (c == '(' || c == ',' || c == ')' || c == 0) { p->i += n; return k; }
65 }
66 }
67 return -1;
68}
69
70static int fs_node(FsParser *p, int depth)
71{
72 FsShape *sh = p->sh;
73 if (depth > FS_MAXDEPTH) { snprintf(sh->err, sizeof sh->err, "nesting deeper than %d", FS_MAXDEPTH); return -1; }
74 size_t at = p->i;
75 int k = fs_word(p);
76 if (k < 0) { snprintf(sh->err, sizeof sh->err, "byte %zu: expected one of arith array nested branch call loop seq", at); return -1; }
77 if (sh->nnode >= FS_MAXNODES) { snprintf(sh->err, sizeof sh->err, "more than %d nodes", FS_MAXNODES); return -1; }
78 int me = sh->nnode++;
79 FsNode *n = &sh->node[me];
80 n->kind = k;
81 n->nkid = 0;
82 if (k < FS_NPRIM) sh->count[k]++;
83 if (fs_is_leaf(k)) {
84 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; }
85 return me;
86 }
87 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; }
88 p->i++;
89 for (;;) {
90 int c = fs_node(p, depth + 1);
91 if (c < 0) return -1;
92 sh->node[me].kid[sh->node[me].nkid++] = c;
93 if (p->s[p->i] == ',') { p->i++; continue; }
94 if (p->s[p->i] == ')') { p->i++; break; }
95 snprintf(sh->err, sizeof sh->err, "byte %zu: expected ',' or ')'", p->i);
96 return -1;
97 }
98 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; }
99 return me;
100}
101
102/* flattened children of node v (a seq child of a container or of a seq is replaced by its own flattened children) */
103static void fs_flat(const FsShape *sh, int v, int *out, int *nout)
104{
105 const FsNode *n = &sh->node[v];
106 for (int j = 0; j < n->nkid; j++) {
107 int c = n->kid[j];
108 if (sh->node[c].kind == FS_SEQ) fs_flat(sh, c, out, nout);
109 else out[(*nout)++] = c;
110 }
111}
112
113static int fs_cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
114
115/* canonical string of node v, malloc'd */
116static char *fs_canon_node(const FsShape *sh, int v)
117{
118 const FsNode *n = &sh->node[v];
119 if (fs_is_leaf(n->kind)) return strdup(fs_prim[n->kind]);
120 int kids[FS_MAXNODES], nk = 0;
121 fs_flat(sh, v, kids, &nk);
122 char *ks[FS_MAXNODES];
123 size_t len = strlen(fs_prim[n->kind]) + 2;
124 for (int j = 0; j < nk; j++) { ks[j] = fs_canon_node(sh, kids[j]); len += strlen(ks[j]) + 1; }
125 qsort(ks, (size_t)nk, sizeof ks[0], fs_cmpstr);
126 char *r = malloc(len + 1);
127 if (!r) { perror("famsig"); exit(2); }
128 strcpy(r, fs_prim[n->kind]);
129 strcat(r, "(");
130 for (int j = 0; j < nk; j++) { if (j) strcat(r, ","); strcat(r, ks[j]); free(ks[j]); }
131 strcat(r, ")");
132 return r;
133}
134
135/* parse + canonicalise; 0 ok, -1 with sh->err set */
136static int fs_shape(const char *s, FsShape *sh)
137{
138 memset(sh, 0, sizeof *sh);
139 size_t len = strlen(s);
140 if (len == 0 || len > FS_MAXLEN) { snprintf(sh->err, sizeof sh->err, "shape length %zu, must be 1..%d", len, FS_MAXLEN); return -1; }
141 FsParser p = {s, 0, sh};
142 sh->root = fs_node(&p, 1);
143 if (sh->root < 0) return -1;
144 if (s[p.i]) { snprintf(sh->err, sizeof sh->err, "byte %zu: extra text after the shape", p.i); return -1; }
145 char *c = fs_canon_node(sh, sh->root);
146 if (strlen(c) >= sizeof sh->canon) { free(c); snprintf(sh->err, sizeof sh->err, "canonical form too long"); return -1; }
147 strcpy(sh->canon, c);
148 free(c);
149 return 0;
150}
151
152/* composition edges "parent>child" (container -> head primitive of each flattened child); unique, sorted */
153static int fs_edges(const FsShape *sh, char out[][16], int max)
154{
155 int ne = 0;
156 for (int v = 0; v < sh->nnode; v++) {
157 if (!fs_is_container(sh->node[v].kind)) continue;
158 int kids[FS_MAXNODES], nk = 0;
159 fs_flat(sh, v, kids, &nk);
160 for (int j = 0; j < nk; j++) {
161 char e[16];
162 snprintf(e, sizeof e, "%s>%s", fs_prim[sh->node[v].kind], fs_prim[sh->node[kids[j]].kind]);
163 int dup = 0;
164 for (int q = 0; q < ne; q++) if (!strcmp(out[q], e)) dup = 1;
165 if (!dup && ne < max) strcpy(out[ne++], e);
166 }
167 }
168 for (int a = 1; a < ne; a++)
169 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); }
170 return ne;
171}
172
173/* ---- coarse form (rule R6) and coarse edges (rule R7) ----------------------------------------- *
174 * Coarse form of a shape: (1) `nested` is read as `arith`; (2) seq is flattened as in the canonical form;
175 * (3) every child list is sorted and duplicate siblings are collapsed to one; a seq left with one child is
176 * that child; (4) a top-level seq whose children are exactly one container plus only leaves is replaced by
177 * that container. Coarse signature = "gc1:" + coarse form. */
178static char *fs_coarse_node(const FsShape *sh, int v)
179{
180 const FsNode *n = &sh->node[v];
181 if (fs_is_leaf(n->kind)) return strdup(n->kind == 5 ? "arith" : fs_prim[n->kind]);
182 int kids[FS_MAXNODES], nk = 0;
183 fs_flat(sh, v, kids, &nk);
184 char *ks[FS_MAXNODES];
185 for (int j = 0; j < nk; j++) ks[j] = fs_coarse_node(sh, kids[j]);
186 qsort(ks, (size_t)nk, sizeof ks[0], fs_cmpstr);
187 int u = 0;
188 for (int j = 0; j < nk; j++) { if (u && !strcmp(ks[j], ks[u - 1])) free(ks[j]); else ks[u++] = ks[j]; }
189 if (n->kind == FS_SEQ && u == 1) return ks[0];
190 if (n->kind == FS_SEQ && v == sh->root) {
191 int nc = 0, ci = -1;
192 for (int j = 0; j < u; j++) if (strchr(ks[j], '(')) { nc++; ci = j; }
193 if (nc == 1) {
194 char *r = ks[ci];
195 for (int j = 0; j < u; j++) if (j != ci) free(ks[j]);
196 return r;
197 }
198 }
199 size_t len = strlen(fs_prim[n->kind]) + 3;
200 for (int j = 0; j < u; j++) len += strlen(ks[j]) + 1;
201 char *r = malloc(len);
202 if (!r) { perror("famsig"); exit(2); }
203 strcpy(r, fs_prim[n->kind]);
204 strcat(r, "(");
205 for (int j = 0; j < u; j++) { if (j) strcat(r, ","); strcat(r, ks[j]); free(ks[j]); }
206 strcat(r, ")");
207 return r;
208}
209
210/* "gc1:<coarse form>" into out (FS_SIGMAX + 4 bytes) */
211static void fs_coarse(const FsShape *sh, char *out)
212{
213 char *c = fs_coarse_node(sh, sh->root);
214 snprintf(out, FS_SIGMAX + 4, "gc1:%s", c);
215 free(c);
216}
217
218/* coarse composition edges: as fs_edges with `nested` read as `arith`; unique, sorted */
219static int fs_cedges(const FsShape *sh, char out[][16], int max)
220{
221 int ne = fs_edges(sh, out, max);
222 for (int q = 0; q < ne; q++) {
223 char *gt = strchr(out[q], '>');
224 if (gt && !strcmp(gt + 1, "nested")) strcpy(gt + 1, "arith");
225 }
226 int u = 0;
227 for (int a = 1; a < ne; a++)
228 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); }
229 for (int q = 0; q < ne; q++) if (!u || strcmp(out[q], out[u - 1])) { if (u != q) strcpy(out[u], out[q]); u++; }
230 return u;
231}
232
233/* ---- metadata lines ---------------------------------------------------------------------------- */
234
235enum { FS_CORPUS, FS_DEV, FS_HELDOUT, FS_NSPLIT };
236static const char *const fs_split[FS_NSPLIT] = {"corpus", "dev", "heldout"};
237
238typedef struct {
239 char id[FS_MAXID + 1];
240 int split;
241 int count[FS_NPRIM];
242 char shape[FS_MAXLEN + 1];
243 char sig[FS_SIGMAX + 4]; /* "gs1:" + canonical */
244 char coarse[FS_SIGMAX + 4]; /* "gc1:" + coarse form (R6) */
245 int distinct; /* distinct primitives in the shape */
246 char src[256]; /* file:line, for messages */
247} FsMeta;
248
249static int fs_task_id_ok(const char *s)
250{
251 size_t n = strlen(s);
252 if (n < 1 || n > FS_MAXID) return 0;
253 for (size_t i = 0; i < n; i++)
254 if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_' || s[i] == '-')) return 0;
255 return 1;
256}
257
258static int fs_count_ok(const char *s, int *v)
259{
260 size_t n = strlen(s);
261 if (n < 1 || n > 2 || (n == 2 && s[0] == '0')) return 0;
262 for (size_t i = 0; i < n; i++) if (s[i] < '0' || s[i] > '9') return 0;
263 *v = atoi(s);
264 return *v <= FS_MAXCOUNT;
265}
266
267/* one metadata line: "<task_id> <split> <arith> <branch> <loop> <array> <call> <nested> <shape>",
268 * fields separated by exactly one space. 0 ok, -1 with err. */
269static int fs_meta_line(const char *line, FsMeta *m, char *err, size_t errn)
270{
271 char buf[1024];
272 size_t len = strlen(line);
273 if (len >= sizeof buf) { snprintf(err, errn, "line too long"); return -1; }
274 memcpy(buf, line, len + 1);
275 char *f[10];
276 int nf = 0;
277 char *s = buf;
278 for (;;) {
279 if (nf == 10) { snprintf(err, errn, "more than 9 fields"); return -1; }
280 f[nf++] = s;
281 char *sp = strchr(s, ' ');
282 if (!sp) break;
283 *sp = 0;
284 s = sp + 1;
285 }
286 if (nf != 9) { snprintf(err, errn, "%d fields, need 9: task split arith branch loop array call nested shape", nf); return -1; }
287 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; }
288 memset(m, 0, sizeof *m);
289 if (!fs_task_id_ok(f[0])) { snprintf(err, errn, "task id '%s' is not [a-z0-9_-]{1,40}", f[0]); return -1; }
290 snprintf(m->id, sizeof m->id, "%s", f[0]);
291 m->split = -1;
292 for (int k = 0; k < FS_NSPLIT; k++) if (!strcmp(f[1], fs_split[k])) m->split = k;
293 if (m->split < 0) { snprintf(err, errn, "split '%s' is not corpus, dev or heldout", f[1]); return -1; }
294 for (int k = 0; k < FS_NPRIM; k++)
295 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; }
296 FsShape sh;
297 if (fs_shape(f[8], &sh)) { snprintf(err, errn, "shape: %s", sh.err); return -1; }
298 for (int k = 0; k < FS_NPRIM; k++)
299 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; }
300 snprintf(m->shape, sizeof m->shape, "%s", f[8]);
301 snprintf(m->sig, sizeof m->sig, "gs1:%s", sh.canon);
302 fs_coarse(&sh, m->coarse);
303 for (int k = 0; k < FS_NPRIM; k++) if (m->count[k]) m->distinct++;
304 return 0;
305}
306
307/* ---- files ------------------------------------------------------------------------------------- */
308
309static char *fs_slurp(const char *path, size_t *len)
310{
311 FILE *fp = fopen(path, "rb");
312 if (!fp) return NULL;
313 size_t cap = 4096, n = 0;
314 char *b = malloc(cap + 1);
315 if (!b) { fclose(fp); return NULL; }
316 size_t r;
317 while ((r = fread(b + n, 1, cap - n, fp)) > 0) {
318 n += r;
319 if (n == cap) { cap *= 2; char *nb = realloc(b, cap + 1); if (!nb) { free(b); fclose(fp); return NULL; } b = nb; }
320 }
321 fclose(fp);
322 b[n] = 0;
323 *len = n;
324 return b;
325}
326
327static void fs_hex(const unsigned char *d, char out[65])
328{
329 for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", d[i]);
330 out[64] = 0;
331}
332
333/* family token: "gs1-" + first 16 hex of sha256(signature), signature = "gs1:" + canonical (no newline);
334 * matches [a-z0-9_-]{1,64}, the record "family" field of tools/gen/retrieval/CONTRACT.md */
335static void fs_token(const char *sig, char out[24])
336{
337 unsigned char d[32];
338 char hex[65];
339 sha256_hash((const unsigned char *)sig, strlen(sig), d);
340 fs_hex(d, hex);
341 snprintf(out, 24, "gs1-%.16s", hex);
342}
343
344static int fs_hex64_ok(const char *s)
345{
346 if (strlen(s) != 64) return 0;
347 for (int i = 0; i < 64; i++) if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return 0;
348 return 1;
349}
350
351typedef struct {
352 FsMeta *m;
353 int n, cap;
354 int is_split; /* last file read was a gen-split file */
355 char salt[65];
356 char split_sha256[65];
357} FsSet;
358
359static void fs_push(FsSet *set, const FsMeta *m)
360{
361 if (set->n == set->cap) {
362 set->cap = set->cap ? 2 * set->cap : 64;
363 set->m = realloc(set->m, (size_t)set->cap * sizeof *set->m);
364 if (!set->m) { perror("famsig"); exit(2); }
365 }
366 set->m[set->n++] = *m;
367}
368
369/* Reads a "gen-meta 1" file (public: corpus/dev lines; '#' comments and blank lines allowed; heldout
370 * lines refused) or a "gen-split 1" file (sealed: "salt <64 hex>", then heldout lines only, ids strictly
371 * ascending, no comments or blank lines). Bytes: '\n' and 0x20..0x7E only, last byte '\n'.
372 * Appends to set; 0 ok, -1 with err. */
373static int fs_read(const char *path, FsSet *set, char *err, size_t errn)
374{
375 size_t len;
376 char *b = fs_slurp(path, &len);
377 if (!b) { snprintf(err, errn, "%s: cannot read", path); return -1; }
378 if (len == 0 || b[len - 1] != '\n') { snprintf(err, errn, "%s: empty or no final newline", path); free(b); return -1; }
379 for (size_t i = 0; i < len; i++)
380 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; }
381 int split_file = !strncmp(b, "gen-split 1\n", 12);
382 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; }
383 set->is_split = split_file;
384 if (split_file) {
385 unsigned char d[32];
386 sha256_hash((const unsigned char *)b, len, d);
387 fs_hex(d, set->split_sha256);
388 }
389 char *line = strchr(b, '\n') + 1;
390 int ln = 1, first = 1;
391 char prev[FS_MAXID + 1] = "";
392 int rc = 0;
393 while (*line) {
394 char *nl = strchr(line, '\n');
395 *nl = 0;
396 ln++;
397 if (split_file && ln == 2) {
398 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; }
399 snprintf(set->salt, sizeof set->salt, "%s", line + 5);
400 } else if (!split_file && (line[0] == 0 || line[0] == '#')) {
401 /* comment or blank line */
402 } else {
403 FsMeta m;
404 char e[200];
405 if (fs_meta_line(line, &m, e, sizeof e)) { snprintf(err, errn, "%s:%d: %s", path, ln, e); rc = -1; break; }
406 snprintf(m.src, sizeof m.src, "%s:%d", path, ln);
407 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; }
408 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; }
409 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; }
410 snprintf(prev, sizeof prev, "%s", m.id);
411 first = 0;
412 fs_push(set, &m);
413 }
414 line = nl + 1;
415 }
416 if (!rc && split_file && ln < 2) { snprintf(err, errn, "%s: missing salt line", path); rc = -1; }
417 free(b);
418 return rc;
419}
420
421/* FAMLIST bytes of a split: "gen-famlist 1\n" "salt <hex>\n" then every distinct heldout signature,
422 * sorted by byte order, one per line. Returns malloc'd string. */
423static char *fs_famlist(const FsSet *set, int *nfam)
424{
425 char **sig = malloc((size_t)(set->n ? set->n : 1) * sizeof *sig);
426 int ns = 0;
427 for (int i = 0; i < set->n; i++) if (set->m[i].split == FS_HELDOUT) sig[ns++] = set->m[i].sig;
428 qsort(sig, (size_t)ns, sizeof sig[0], fs_cmpstr);
429 size_t cap = 128 + (size_t)ns * (FS_SIGMAX + 8); /* header "gen-famlist 1\nsalt <64 hex>\n" is 84 bytes */
430 char *out = malloc(cap);
431 if (!out || !sig) { perror("famsig"); exit(2); }
432 int n = snprintf(out, cap, "gen-famlist 1\nsalt %s\n", set->salt);
433 int nf = 0;
434 for (int i = 0; i < ns; i++) {
435 if (i && !strcmp(sig[i], sig[i - 1])) continue;
436 int w = snprintf(out + n, cap - (size_t)n, "%s\n", sig[i]);
437 if (w < 0 || (size_t)w >= cap - (size_t)n) { fprintf(stderr, "famsig: family list overflow\n"); exit(2); }
438 n += w;
439 nf++;
440 }
441 free(sig);
442 *nfam = nf;
443 return out;
444}
445
446static int fs_cmpmeta_sig(const void *a, const void *b)
447{
448 const FsMeta *x = a, *y = b;
449 int c = strcmp(x->sig, y->sig);
450 if (c) return c;
451 if (x->split != y->split) return x->split < y->split ? -1 : 1;
452 return strcmp(x->id, y->id);
453}
454
455/* The disjointness rule (FAMSIG_V1.md section 4) over every line read. Prints a report to out
456 * (heldout family signatures only when show_heldout); returns the number of rule failures. */
457/* composition edges of one shape that are absent from the corpus edge list */
458static int fs_novel(const char *shape, char cedge[][16], int nce)
459{
460 FsShape sh;
461 if (fs_shape(shape, &sh)) return 0;
462 char e[64][16];
463 int ne = fs_cedges(&sh, e, 64), novel = 0;
464 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; }
465 return novel;
466}
467
468static int fs_rule(FsSet *set, FILE *out, int show_heldout)
469{
470 int fails = 0;
471 FsMeta *m = set->m;
472 int n = set->n;
473 /* R2 unique task ids across every input */
474 for (int i = 0; i < n; i++)
475 for (int j = i + 1; j < n; j++)
476 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++; }
477 /* R3 a signature belongs to one split */
478 for (int i = 0; i < n; i++)
479 for (int j = i + 1; j < n; j++)
480 if (m[i].split != m[j].split && !strcmp(m[i].sig, m[j].sig)) {
481 int ho = m[i].split == FS_HELDOUT || m[j].split == FS_HELDOUT;
482 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],
483 ho && !show_heldout ? "(heldout signature hidden)" : m[i].sig, m[i].id, m[j].id);
484 fails++;
485 }
486 /* R6 coarse signatures are pairwise disjoint across splits */
487 for (int i = 0; i < n; i++)
488 for (int j = i + 1; j < n; j++)
489 if (m[i].split != m[j].split && !strcmp(m[i].coarse, m[j].coarse) && strcmp(m[i].sig, m[j].sig)) {
490 int ho = m[i].split == FS_HELDOUT || m[j].split == FS_HELDOUT;
491 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],
492 ho && !show_heldout ? "(heldout signature hidden)" : m[i].coarse, m[i].id, m[j].id);
493 fails++;
494 }
495 /* R4 every heldout primitive appears in some corpus task; R5 heldout shapes use >= 2 distinct primitives */
496 int corpus_has[FS_NPRIM] = {0}, ncorpus = 0;
497 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; }
498 for (int i = 0; i < n; i++) {
499 if (m[i].split != FS_HELDOUT) continue;
500 for (int k = 0; k < FS_NPRIM; k++)
501 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++; }
502 if (m[i].distinct < 2) { fprintf(out, "FAIL R5 heldout task %s uses fewer than 2 distinct primitives\n", m[i].id); fails++; }
503 }
504 /* corpus composition edges, for the novelty report */
505 static char cedge[256][16];
506 int nce = 0;
507 for (int i = 0; i < n; i++) {
508 if (m[i].split != FS_CORPUS) continue;
509 FsShape sh;
510 if (fs_shape(m[i].shape, &sh)) continue;
511 char e[64][16];
512 int ne = fs_cedges(&sh, e, 64);
513 for (int q = 0; q < ne; q++) {
514 int dup = 0;
515 for (int r = 0; r < nce; r++) if (!strcmp(cedge[r], e[q])) dup = 1;
516 if (!dup && nce < 256) strcpy(cedge[nce++], e[q]);
517 }
518 }
519 /* R7 every heldout shape has at least one coarse composition edge that no corpus shape has */
520 for (int i = 0; i < n; i++) {
521 if (m[i].split != FS_HELDOUT) continue;
522 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++; }
523 }
524 /* R8 no top-level step of a dev or heldout shape (after seq flattening) is a container subtree whose coarse form
525 * equals the coarse signature of a corpus task. Leaf steps are not compared (leaf reuse is what R4 requires); a
526 * shape whose whole coarse signature already equals the corpus one is left to R3/R6. */
527 for (int i = 0; i < n; i++) {
528 if (m[i].split == FS_CORPUS) continue;
529 FsShape sh;
530 if (fs_shape(m[i].shape, &sh) || sh.node[sh.root].kind != FS_SEQ) continue;
531 int kids[FS_MAXNODES], nk = 0;
532 fs_flat(&sh, sh.root, kids, &nk);
533 for (int q = 0; q < nk; q++) {
534 if (!fs_is_container(sh.node[kids[q]].kind)) continue;
535 char *c = fs_coarse_node(&sh, kids[q]);
536 for (int j = 0; j < n; j++)
537 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)) {
538 int ho = m[i].split == FS_HELDOUT;
539 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,
540 m[j].sig, m[j].id, ho && !show_heldout ? "(heldout step hidden)" : c);
541 fails++;
542 break;
543 }
544 free(c);
545 }
546 }
547 /* report: per split counts, then families in signature order */
548 FsMeta *s = malloc((size_t)(n ? n : 1) * sizeof *s);
549 if (!s) { perror("famsig"); exit(2); }
550 memcpy(s, m, (size_t)n * sizeof *s);
551 qsort(s, (size_t)n, sizeof *s, fs_cmpmeta_sig);
552 for (int sp = 0; sp < FS_NSPLIT; sp++) {
553 int t = 0, f = 0;
554 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++; }
555 fprintf(out, "split %s tasks=%d families=%d\n", fs_split[sp], t, f);
556 }
557 for (int i = 0; i < n;) {
558 int j = i;
559 while (j < n && !strcmp(s[j].sig, s[i].sig) && s[j].split == s[i].split) j++;
560 if (s[i].split != FS_HELDOUT || show_heldout) {
561 fprintf(out, "family %s %s tasks=%d", fs_split[s[i].split], s[i].sig, j - i);
562 if (s[i].split == FS_HELDOUT) {
563 int novel = fs_novel(s[i].shape, cedge, nce);
564 fprintf(out, " novel_edges=%d", novel);
565 }
566 fputc('\n', out);
567 }
568 i = j;
569 }
570 free(s);
571 return fails;
572}
573
574#endif