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
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""Rebuild tables, plots, experiment records and website data from trusted result files."""

from __future__ import annotations

from collections import Counter
from pathlib import Path
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
from .analysis import summary
from .data import dump, load
from .paper import make_paper

TITLE = "Editable expansion-parent selection in the Red Queen Gödel Machine"
COLORS = {
    "editable": "#087d76",
    "native": "#a54c32",
    "uniform": "#536889",
    "handcrafted": "#80549b",
    "fork-native": "#895811",
    "root": "#444444",
}


def write(path: Path, text: str):
    """Use UTF-8 consistently for Markdown viewed on Windows, GitHub and Pages."""
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(text.rstrip() + "\n", encoding="utf-8")


def table(head, rows):
    """Render a GitHub table from the same cells consumed by the procedural report."""
    return "\n".join(
        "| "
        + " | ".join(str(c).replace("|", "\\|").replace("\n", " ") for c in row)
        + " |"
        for row in [head, ["---"] * len(head), *rows]
    )


def evidence(run):
    """Count execution events separately from code changes and hypothetical replay draws."""
    chosen = run["selection"]
    return dict(
        nodes=len(run["nodes"]),
        edits=sum(bool(s["child"]) for s in chosen),
        invalid=sum(s["child"] is None for s in chosen),
        fallbacks=sum(len(s["failures"]) for s in chosen),
        selector_edits=sum("selector" in s["changed"] for s in chosen),
        older=sum(
            s.get("owner") is not None and s["owner"] != s["parent"] for s in chosen
        ),
        replay_diff=sum(s["published"] != s["released"] for s in chosen),
        draws=len(chosen),
        replacements=sum(bool(c["replacements"]) for c in run["checkpoints"]),
        erased=sum(c["erased_records"] for c in run["checkpoints"]),
    )


def example_rows(runs, evals):
    """Use the first recorded outputs per role/arm in file order to avoid picking highlights."""
    examples = {}
    for run in runs:
        group = examples.setdefault(run["arm"], {})
        for record in run["records"]:
            m = record["metadata"]
            group.setdefault(
                record["role_id"],
                dict(
                    id=m["paper"],
                    decision=m["decision"],
                    text=m.get("draft") or m["evidence"],
                    run=run["name"],
                ),
            )
    for e in evals:
        if e["rows"]:
            m = e["rows"][0]
            examples.setdefault(e["arm"], {}).setdefault(
                "endpoint",
                dict(
                    id=m["id"],
                    decision=m["decision"],
                    text=m["evidence"],
                    run=e["name"],
                ),
            )
    return examples


def architecture(path):
    """Draw the inheritance and scoring paths explicitly, including the moving evaluator."""
    fig, ax = plt.subplots(figsize=(10.7, 4.0), layout="constrained")
    ax.set(xlim=(0, 10), ylim=(0, 3.8))
    ax.axis("off")
    boxes = [
        (1.05, 2.85, "Current archive"),
        (3.55, 2.85, "Newest selector\nor fixed rule"),
        (6.1, 2.85, "Chosen parent's\nfour programs"),
        (8.7, 2.85, "Editor proposes\na child"),
        (1.05, 0.72, "Own-node\nvalidation"),
        (3.55, 0.72, "Manuscript +\nepoch reviewer"),
        (6.1, 0.72, "Human decision\nanchor"),
        (8.7, 0.72, "Checkpoint\nelection"),
    ]
    for x, y, label in boxes:
        ax.text(
            x,
            y,
            label,
            ha="center",
            va="center",
            color="#14343a",
            fontsize=11,
            bbox=dict(
                boxstyle="round,pad=.65", facecolor="#eff6f4", edgecolor="#5d8684"
            ),
        )
    edges = [
        ((1.85, 2.85), (2.73, 2.85)),
        ((4.43, 2.85), (5.15, 2.85)),
        ((7.02, 2.85), (7.85, 2.85)),
        ((1.05, 2.56), (1.05, 1.14)),
        ((1.8, 0.72), (2.6, 0.72)),
        ((7.06, 0.72), (7.88, 0.72)),
    ]
    for a, b in edges:
        ax.annotate(
            "",
            xy=b,
            xytext=a,
            arrowprops=dict(arrowstyle="->", color="#476363", lw=1.6),
        )
    ax.annotate(
        "",
        xy=(1.05, 3.18),
        xytext=(8.7, 3.18),
        arrowprops=dict(
            arrowstyle="->", connectionstyle="bar,fraction=.052", color="#476363"
        ),
    )
    ax.text(4.9, 3.62, "Admit child and retain ancestry", ha="center", fontsize=9)
    ax.annotate(
        "",
        xy=(3.55, 1.16),
        xytext=(8.7, 1.16),
        arrowprops=dict(
            arrowstyle="->", connectionstyle="bar,fraction=.09", color="#476363"
        ),
    )
    ax.text(
        6.1, 1.77, "Promoted reviewer judges future drafts", ha="center", fontsize=9
    )
    fig.savefig(path, dpi=130, bbox_inches="tight")
    plt.close(fig)


def plots(folder, runs, evals):
    """Show binary reward and accuracy on their full zero-to-one scale, marking epoch changes."""
    folder.mkdir(parents=True, exist_ok=True)
    architecture(folder / "architecture.png")
    for run in runs:
        name = run["name"]
        fig, (ax, tree) = plt.subplots(
            1,
            2,
            figsize=(10, 3.45),
            layout="constrained",
            gridspec_kw={"width_ratios": [1.25, 1]},
        )
        for label, s, t, color in (
            ("Reviewer accuracy", "review_success", "review_total", "#087d76"),
            ("Writer reward", "writer_success", "writer_total", "#ad5839"),
        ):
            points = [(r["n"], r[s] / r[t]) for r in run["trajectory"] if r[t]]
            if points:
                x, y = zip(*points)
                ax.step(x, y, where="post", label=label, color=color)
        for cp in run["checkpoints"]:
            if cp["replacements"]:
                ax.axvline(cp["checkpoint"], ls=":", color="#666")
        ax.set(
            xlabel="Scheduled validation evaluations",
            ylabel="Fraction of valid records",
            ylim=(-0.02, 1.02),
            title=name,
        )
        if ax.lines:
            ax.legend(fontsize=8, loc="best")
        positions = {}
        for i, node in enumerate(run["nodes"]):
            parent = node["parent"]
            depth = positions[parent][0] + 1 if parent in positions else 0
            positions[node["id"]] = (depth, i)
            if parent:
                px, py = positions[parent]
                tree.plot([px, depth], [py, i], lw=1, color="#a4b8b5")
            tree.scatter(depth, i, s=17, color=COLORS[run["arm"]])
            if len(run["nodes"]) < 22:
                tree.annotate(
                    node["id"],
                    (depth, i),
                    xytext=(4, 0),
                    textcoords="offset points",
                    va="center",
                    fontsize=6,
                )
        tree.set(
            xlabel="Generations from root",
            ylabel="Admission order",
            title="Workspace lineage",
        )
        tree.margins(x=0.2, y=0.08)
        fig.savefig(folder / (name + ".png"), dpi=115)
        plt.close(fig)
    complete = [e for e in evals if e["complete"] and not e["name"].endswith("-tokens")]
    if complete:
        fig, axes = plt.subplots(
            1, 2, figsize=(9, 3.5), layout="constrained", sharey=True
        )
        for ax, split in zip(axes, ("pilot", "test")):
            ax.set(
                title=split.capitalize() + " endpoint accuracy",
                ylim=(0, 1.02),
                ylabel="Correct fraction",
            )
            for j, (arm, color) in enumerate(COLORS.items()):
                values = [
                    sum(r["correct"] for r in e["rows"]) / len(e["rows"])
                    for e in complete
                    if e["split"] == split and e["arm"] == arm
                ]
                if values:
                    ax.scatter([j] * len(values), values, color=color, alpha=0.7)
            ax.set_xticks(
                range(len(COLORS)), list(COLORS), rotation=32, ha="right", fontsize=8
            )
        fig.savefig(folder / "endpoints.png", dpi=120)
        plt.close(fig)


def comparison_text(contrasts):
    """Apply the registered rule to the primary interval only after scoring."""
    result = contrasts.get("primary")
    if result is None:
        return "The sealed editable–native comparison is pending."
    return (
        f"The mean paired difference is {result['mean_pp']:+.2f} percentage points "
        f"(95% interval {result['low']:+.2f} to {result['high']:+.2f}, {result['pairs']} seed pairs). "
        f"The registered interpretation is {result['interpretation']}."
    )


def report(project):
    """A single entry point rebuilds human-facing releases without calling a model."""
    runs, evals, scored, contrasts = summary(project)
    reports = project / "reports"
    docs = project / "docs"
    docs.mkdir(exist_ok=True)
    examples = example_rows(runs, evals)
    completed = sum(r["complete"] for r in runs)
    complete_evals = sum(e["complete"] for e in evals)
    status = comparison_text(contrasts)
    scores = [
        [
            e["split"],
            e["name"],
            e["n"],
            f"{e['correct']}/{e['n']}",
            f"{100 * e['correct'] / e['n']:.1f}%",
            "complete" if e["complete"] else "partial",
        ]
        for e in scored
        if e["n"]
    ]
    score_table = (
        table(["Split", "Endpoint", "Papers", "Correct", "Accuracy", "Status"], scores)
        if scores
        else "No endpoint predictions have been recorded."
    )
    searches = []
    for run in runs:
        m = evidence(run)
        searches.append(
            [
                run["name"],
                f"{run['n']}/{run['budget']}",
                m["nodes"],
                m["selector_edits"],
                m["invalid"],
                m["fallbacks"],
                m["replacements"],
                m["erased"],
                f"{m['replay_diff']}/{m['draws']}",
            ]
        )
    search_table = (
        table(
            [
                "Search",
                "Validation",
                "Nodes",
                "Selector edits",
                "Invalid edits",
                "Fallback errors",
                "Replacements",
                "Erased",
                "Formula disagreements",
            ],
            searches,
        )
        if searches
        else "No search trajectory has been recorded."
    )
    computes = []
    for r in [*runs, *scored]:
        c = r.get("compute", {})
        if c:
            computes.append(
                [
                    r["name"] if "split" not in r else r["split"] + "-" + r["name"],
                    c["calls"],
                    c["actual_calls"],
                    c["input"],
                    c["output"],
                    c["actual_input"],
                    c["actual_output"],
                    f"{c['service_seconds'] / 3600:.4f}",
                    c["failed_attempts"],
                ]
            )
    compute_table = (
        table(
            [
                "Run",
                "Calls",
                "Actual calls",
                "Input",
                "Output",
                "Actual input",
                "Actual output",
                "GPU service h",
                "Service failures",
            ],
            computes,
        )
        if computes
        else "Compute measurements begin with the first search or endpoint call."
    )
    route_rows = [
        [e["split"] + " " + e["name"], route, v["n"], f"{v['accuracy']:.3f}"]
        for e in scored
        for route, v in e["routes"].items()
    ]
    route_table = (
        table(["Endpoint", "Route", "Papers", "Accuracy"], route_rows)
        if route_rows
        else "Reading-route results are pending."
    )
    pair_rows = [
        [panel, seed, f"{pp:+.2f}", f"{value['low']:+.2f} to {value['high']:+.2f}"]
        for panel, value in contrasts.items()
        for seed, pp in zip(value["seeds"], value["per_seed_pp"])
    ]
    pair_table = (
        table(
            ["Comparison", "Seed", "Difference (pp)", "Cohort interval (pp)"], pair_rows
        )
        if pair_rows
        else "Paired effects require both frozen endpoints and a shared set of scored papers."
    )
    figures = docs / "figures"
    plots(figures, runs, evals)
    dump(
        project / "results" / "analysis.json",
        dict(
            scores=scored,
            contrasts=contrasts,
            searches={r["name"]: evidence(r) for r in runs},
        ),
    )
    # The local dashboard polls a compact live snapshot. Long histories load only on demand.
    dump(
        docs / "data.json",
        dict(
            scores=scored,
            contrasts=contrasts,
            examples=examples,
            runs=[
                dict(
                    name=r["name"],
                    arm=r["arm"],
                    stage=r["stage"],
                    seed=r["seed"],
                    n=r["n"],
                    budget=r["budget"],
                    counts=evidence(r),
                    trajectory=r["trajectory"],
                    compute=r["compute"],
                    checkpoints=[
                        c["checkpoint"] for c in r["checkpoints"] if c["replacements"]
                    ],
                    nodes=[dict(id=v["id"], parent=v["parent"]) for v in r["nodes"]],
                    selection=[
                        {
                            k: s.get(k)
                            for k in (
                                "n",
                                "owner",
                                "parent",
                                "child",
                                "changed",
                                "error",
                                "source_hash",
                                "source",
                            )
                        }
                        for s in r["selection"]
                    ],
                )
                for r in runs
            ],
        ),
    )
    if not (docs / "live.json").exists():
        dump(docs / "live.json", {})
    summary_text = f"# Study summary\n\n{status}\n\n## Endpoint accuracy\n\n{score_table}\n\n## Paired effects\n\n{pair_table}\n\n## Search and selector behavior\n\n{search_table}\n\n## Reading routes\n\n{route_table}\n\n## Measured compute\n\n{compute_table}\n\nGPU service hours sum Ollama service durations of actual requests to the GPU-resident model. Cached requests retain their logical token counts. The fork includes the shared prefix in its comparison budget.\n\n[Registered interpretation](preregistration.md) · [Machine-readable analysis](../results/analysis.json)"
    write(reports / "summary.md", summary_text)
    progress = f"# Study progress\n\n{completed} completed searches and {complete_evals} completed endpoint evaluations. {status}\n\n{search_table}\n\n"
    budget_file = project / "results" / "budget.json"
    if budget_file.exists():
        b = load(budget_file)
        progress += f"The pilot timing rule selected {b['budget']} validation evaluations per search. Projected time from the pilot was {b['projections_hours'][str(b['budget'])]:.1f} hours. The measured search and evaluation timing inputs are in [the budget decision](../results/budget.json)."
    else:
        progress += "The first pilot block has a 14-hour target. The remaining-week estimate becomes available after all four pilot timings."
    write(
        reports / "progress.md",
        progress
        + "\n\n[Run matrix](planned-runs.md) · [Live dashboard](../docs/index.html)",
    )
    histories = reports / "experiments"
    index = []
    lessons = []
    for r in runs:
        name = r["name"]
        folder = histories / name
        m = evidence(r)
        index.append(f"- [{name}]({name}/experiment.md)")
        write(
            folder / "experiment.md",
            f"# {name}\n\n{r['arm']} expansion, seed {r['seed']}, frozen Qwen3.5-4B, {r['context']} tokens of context. The writer seeks acceptance by the epoch reviewer, and reviewer accuracy uses historical decisions. The endpoint follows the registered fifth-percentile rule.\n\n"
            + table(["Measure", "Observed"], [[k, v] for k, v in m.items()])
            + f"\n\n![Validation trajectory and lineage](../../../docs/figures/{name}.png)\n\n[Full raw archive, workspaces and examples](../../../results/runs/{name}.json) · [Run parameters](../../preregistration.md)",
        )
        observation = f"{name} admitted {m['edits']} edits in {m['draws']} windows. The selector source changed on {m['selector_edits']} edges, and the two child penalties chose different parents on {m['replay_diff']} snapshots. {m['replacements']} reviewer replacements erased {m['erased']} dependent records."
        lessons.append("- " + observation)
        write(
            folder / "learnings.md",
            "# Observations\n\n"
            + observation
            + "\n\nThe formula replay holds the archive and draw fixed. Its disagreements measure local decisions. The midpoint intervention measures downstream consequences.",
        )
    write(
        histories / "experiments.md",
        "# Experiment index\n\n"
        + status
        + "\n\n"
        + score_table
        + "\n\n"
        + search_table
        + "\n\n"
        + "\n".join(index)
        + "\n\n[Compute, reading routes and paired effects](../summary.md) · [Fixed settings and their rationale](../preregistration.md)",
    )
    write(
        histories / "learnings.md",
        "# Study observations\n\n"
        + (
            "\n".join(lessons)
            or "The initial source audit identified different child-count penalties in the released code and printed formula. The registered replay tests their effect on the same archives."
        )
        + "\n\n[Source audit](../state-of-the-art-review.md) · [Interpretation limits](../reviews/methods.md)",
    )
    make_paper(
        project,
        status,
        scores,
        searches,
        contrasts,
        examples,
        computes,
        route_rows,
        runs,
    )
    readme = project / "README.md"
    base = (
        readme.read_text(encoding="utf-8")
        .split("<!-- GENERATED: outcomes -->")[0]
        .rstrip()
    )
    write(
        readme,
        base
        + "\n\n<!-- GENERATED: outcomes -->\n## Observed results\n\n"
        + status
        + "\n\n"
        + score_table
        + "\n\n[Full report, compute and uncertainty](reports/summary.md) · [Examples and lineage explorer](https://burnycoder.github.io/red-queen-godel-machine-with-evolvable-parent-selection/) · [Paper PDF](reports/paper.pdf)\n",
    )
    return dict(
        completed_searches=completed,
        completed_evaluations=complete_evals,
        primary=contrasts.get("primary"),
    )