"""Current native-product guides and version-matched source delivery."""
from html import escape


def intro(label, name, heading, copy):
    return f'<section class="page-intro wrap"><a class="breadcrumb" href="/lab/">← Inside the lab</a><p class="eyebrow">{label}</p><p class="product-title">{name}</p><h1>{heading}</h1><p>{copy}</p></section>'


def build_release_pages(write_page, root):
    morpho = intro('NATIVE WORKBENCH / PYTORCH + PYGAME', 'Morpho', 'Return to the<br><em>same living world.</em>', 'Grow synthetic tissue, make an intervention, and keep the exact state that follows. Save the world, its neural update rule, and its random stream together.')
    morpho += '''<section class="wrap product-capture"><img src="/assets/morpho-workbench.png" width="946" height="704" alt="Actual Morpho workbench at step 160 showing a synthetic organism, live metrics, and intervention controls"><p class="fine-print">Actual CUDA workbench capture · step 160 · untrained neural update rule with structured growth prior.</p></section>
<section class="wrap release-features"><p class="eyebrow">WHAT YOU CAN DO</p><ul><li>Pause, single-step, inspect seven state layers, and cut, burn, delete, feed, or poison an organism.</li><li>Save and restore the complete session, then continue the same future.</li><li>Resume genome evolution from a checkpoint and verify archived organisms against their original rollout.</li></ul></section>
<section class="wrap section guide"><div><p class="eyebrow">YOUR FIRST SESSION</p><h2>A local petri dish.<br><em>A recoverable experiment.</em></h2><a class="button button-primary" href="https://cyberelf.link/downloads/morpho-source.zip">Download source ↓</a></div><div><p>Extract the release, install Python 3.11 or newer, and open a terminal in its folder. Start on CPU; use a compatible PyTorch CUDA build for your GPU.</p><pre class="run-command"><code>python -m venv .venv
.venv/Scripts/python.exe -m pip install -e ".[dev]"
.venv/Scripts/python.exe scripts/run_viewer.py --config configs/tiny.yaml --device cpu</code></pre><p>F5 saves the session; F9 restores it. The README includes evolution, replay, and CPU/CUDA validation commands.</p></div></section>
<section class="wrap evidence-panel"><p class="eyebrow">OBSERVED / CONTINUITY CHECK</p><h2>The saved world<br><em>really resumes.</em></h2><p>On the tested CPU and CUDA backends, restored sessions and evolution runs matched their continuous controls. All three archived organisms on each backend replayed exactly. Changing the recorded update rule made the negative control fail.</p><a class="text-link" href="/evidence/morpho-replay.json">Inspect the replay report ↓</a><p class="fine-print">PyTorch 2.9.0+cu130 · RTX 5060 · identical-source and same-backend checks. This is continuity evidence, not evidence of learned biology. Cross-version and cross-hardware replay is not promised.</p></section>'''
    write_page('lab/morpho', 'Morpho — native simulation workbench', 'Save, restore, evolve, and verify the actual state of a native synthetic-biology experiment.', morpho)

    substrate = intro('LOCAL RESEARCH RIG / SYNTHETIC LIFETIMES', 'Substrate Search', 'Search the mechanism.<br><em>Protect the test.</em>', 'Explore memory, consolidation, context routing, trust gates, and contradiction repair. Keep the final test worlds outside the decisions that choose a candidate.')
    substrate += '''<section class="wrap protocol-flow" aria-label="Evaluation protocol"><article><span class="eyebrow">01 / TRAIN</span><h2>Develop</h2><p>Build candidate mechanisms across the declared training worlds.</p></article><article><span class="eyebrow">02 / VALIDATE</span><h2>Select & freeze</h2><p>Choose the candidate, baseline, and ablations. Seal the complete selection.</p></article><article><span class="eyebrow">03 / TEST</span><h2>Measure once</h2><p>Evaluate the frozen comparison on independent worlds. Retain the result.</p></article></section>
<section class="wrap evidence-panel"><p class="eyebrow">CURRENT RESULT / 48 SEARCH TRIALS</p><h2>A better experiment.<br><em>No winning candidate.</em></h2><p>The selected candidate did not beat the validation-selected baseline on the three final test worlds. No discovery cleared promotion.</p><div class="result-table"><table><caption>Independent-test objective · higher is better</caption><thead><tr><th scope="col">Frozen comparison</th><th scope="col">Score</th></tr></thead><tbody><tr><th scope="row">Selected candidate</th><td>0.944390</td></tr><tr><th scope="row">Uniform consolidation baseline</th><td>0.975217</td></tr><tr><th scope="row">Candidate minus baseline</th><td>−0.030827</td></tr></tbody></table></div><p>A run paused after trial 17 resumed to exactly the same sealed result as the uninterrupted 48-trial control.</p><div class="actions"><a class="text-link" href="/evidence/substrate-evaluation.json">Result ↓</a><a class="text-link" href="/evidence/substrate-protocol.json">Declared protocol ↓</a><a class="text-link" href="/evidence/substrate-selection.json">Frozen selection ↓</a></div></section>
<section class="wrap section guide"><div><p class="eyebrow">REPRODUCE OR EXPLORE</p><h2>Pause the search.<br><em>Keep the experiment.</em></h2><a class="button button-primary" href="https://cyberelf.link/downloads/substrate-search-source.zip">Download source ↓</a></div><div><p>The substrate discovery command uses Python 3.11 or newer and needs no GPU or external model. From the extracted folder:</p><pre class="run-command"><code>python -m pip install -e .
python -m simagotchi substrate-discover --trials 48 --seed 149 --out runs/demo --stop-after 17
python -m simagotchi substrate-discover --trials 48 --seed 149 --out runs/demo --resume</code></pre><p>The journal records trial state and RNG. Resume rejects changed protocols and complete-record corruption. A completed run returns its sealed result without silently rerunning the test.</p></div></section>
<section class="wrap boundary"><p class="eyebrow">EVIDENCE BOUNDARY</p><h2>Synthetic results stay synthetic.</h2><p>The older discovery loop used its “hidden” worlds during selection. Those old scores are selection evidence. This corrected protocol governs substrate-discover; the repository’s other historical search methods retain their own protocols. None of these results demonstrates model-scale transfer or a universal architecture.</p></section>'''
    write_page('lab/substrate-search', 'Substrate Search — independent testing and replay', 'A resumable local search rig with separate selection and final testing, including its latest negative result.', substrate)

    entoptic = intro('BROWSER WORKSTATION / WEBGL2', 'Entoptic', 'Patch the signal.<br><em>Keep the experiment.</em>', 'Build a visual rack from typed signal devices. Inspect the image as you work, undo a change, and preserve a patch you can return to.')
    entoptic += '''<section class="wrap product-capture"><img src="/assets/entoptic-workbench.png" width="1440" height="1000" alt="Actual Entoptic browser workstation with typed devices and rendered visual output" loading="lazy"></section><section class="wrap section guide"><div><p class="eyebrow">YOUR FIRST PATCH</p><h2>Open a rack.<br><em>Follow the image.</em></h2><div class="actions"><a class="button button-primary" href="https://entoptic.cyberelf.link/">Open Entoptic ↗</a><a class="text-link" href="https://cyberelf.link/downloads/entoptic-source.zip">Download source ↓</a></div></div><ol><li><span>01</span><p>Open the workstation in a browser with WebGL2. Start from a preset and inspect its device chain.</p></li><li><span>02</span><p>Change a device or add a track. Use undo and redo to compare the patch before and after your edit.</p></li><li><span>03</span><p>Export the project. Import it again to restore the rack; an unsupported device rejects the import without replacing the active project.</p></li></ol></section><section class="wrap boundary"><p class="eyebrow">RUNTIME BOUNDARY</p><h2>The browser is the instrument.</h2><p>Rendering uses the client GPU. Camera devices require the browser’s permission and a working camera. Browser operation does not imply parity with the desktop Digital Vision Workstation. Project edits have transactional loading and cleanup; shared shader compilation is checked separately from media-device access.</p></section>'''
    write_page('lab/entoptic', 'Entoptic — visual signal workstation', 'Patch typed visual devices, undo changes, and restore projects safely in the browser.', entoptic)

    names = [('whetstone','Whetstone','Paired evaluation'), ('saliencelean','SalienceLean','Lean proof receipts'), ('foundry','Foundry','Evidence admission and adoption'), ('scopewrit','ScopeWrit','Explicit run authority'), ('backplane','Backplane','Governed runtime'), ('coherence','Coherence','Browser-local reconciliation'), ('entoptic','Entoptic','Visual signal workstation'), ('morpho','Morpho','Native simulation workbench'), ('substrate-search','Substrate Search','Continual-learning research rig')]
    downloads = '<section class="page-intro wrap"><p class="eyebrow">RELEASE SOURCE</p><h1>Take the tools<br><em>into your environment.</em></h1><p>These archives contain the reviewed source for this release, setup instructions, and the matching tests. Each includes a source identity and file manifest.</p></section><section class="wrap section download-grid">'
    downloads += ''.join(f'<article><p class="eyebrow">{escape(role)}</p><h2>{escape(name)}</h2><a class="text-link" href="https://cyberelf.link/downloads/{slug}-source.zip">Download source ZIP ↓</a></article>' for slug,name,role in names)
    downloads += '</section><section class="wrap boundary"><p class="eyebrow">CHECK THE DELIVERY</p><h2>Source, with its identity attached.</h2><p>Runtime dependencies install locally using each project’s instructions. The archives exclude credentials, private evaluation banks, model weights, and runtime databases. Checksums establish byte identity; they do not authenticate a publisher independently.</p><a class="text-link" href="https://cyberelf.link/downloads/manifest.json">Archive hashes and source revisions ↓</a></section>'
    write_page('downloads', 'Source releases', 'Download the Cyberelf tool source matching this release, with setup, tests, and checksums.', downloads)
    return ['lab/morpho', 'lab/substrate-search', 'lab/entoptic', 'downloads']
