oxedyne/daimond/dev/verify_diamondwalk.mjs
7.2 KiB, 1 run
created by r2519314175:369, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // verify_diamondwalk.mjs — the store is never walked twice at once. |
| 2 | // |
| 3 | // WHY. `list_diamonds` walks the whole Diamond root and reads each Diamond's |
| 4 | // metadata: one OPFS round trip per Diamond. On a desktop each is a few |
| 5 | // milliseconds and nobody notices. On the iPhone that looped for four sessions |
| 6 | // each was 100-500ms across FIFTEEN Diamonds, and the durable trail from the |
| 7 | // device showed this: |
| 8 | // |
| 9 | // 16:37:46.046 list_diamonds start |
| 10 | // 16:37:46.126 list 15 entries |
| 11 | // 16:37:46.126 list entry 19fb11892cdc |
| 12 | // 16:37:47.185 list entry 19fad130b973 <- still on the fourth |
| 13 | // 16:37:47.345 list_diamonds start <- and a SECOND walk begins |
| 14 | // |
| 15 | // Fourteen callers reach `loadDiamonds` and most are event-driven, so on a slow |
| 16 | // store they overlap by construction rather than by accident. Every walk holds |
| 17 | // the whole list, redraws the rail and reloads the triggers, and they pile up. |
| 18 | // |
| 19 | // WHAT IS LOCKED DOWN. |
| 20 | // |
| 21 | // A. Concurrent asks produce ONE walk, not one each. |
| 22 | // B. No two walks ever overlap — measured from the trail's own start/finish |
| 23 | // markers, not from a count, because a count cannot tell three sequential |
| 24 | // walks from three simultaneous ones and simultaneous is the fault. |
| 25 | // C. An ask that arrives DURING a walk still gets fresh data. A caller that |
| 26 | // has just written to the store must not be handed a walk that began before |
| 27 | // its write, so a trailing walk runs afterwards. |
| 28 | // D. The rail ends up right: every Diamond in the store is on it. |
| 29 | // |
| 30 | // PROVED RED with `--break`, which restores the un-coalesced function. |
| 31 | // |
| 32 | // node dev/verify_diamondwalk.mjs |
| 33 | // node dev/verify_diamondwalk.mjs --break # must fail, loudly |
| 34 | // |
| 35 | // Needs dev/serve.mjs. No gateway, no model. |
| 36 | |
| 37 | import fs from 'node:fs'; |
| 38 | import { open, scratch } from './harness.mjs'; |
| 39 | |
| 40 | const BREAK = process.argv.includes('--break'); |
| 41 | |
| 42 | const out = []; |
| 43 | let bad = 0; |
| 44 | const check = (ok, what, detail) => { |
| 45 | out.push(`${ok ? 'PASS' : 'FAIL'} ${what}${detail != null ? ' — ' + detail : ''}`); |
| 46 | if (!ok) bad++; |
| 47 | return ok; |
| 48 | }; |
| 49 | |
| 50 | const PROFILE = scratch('pw', 'diamondwalk-' + process.pid); |
| 51 | fs.rmSync(PROFILE, { recursive: true, force: true }); |
| 52 | const s = await open({ name: 'diamondwalk', connect: false, profile: PROFILE }); |
| 53 | const p = s.page; |
| 54 | |
| 55 | // Enough Diamonds that a walk takes long enough to be overlapped. The phone had |
| 56 | // fifteen; eight is enough to make the window real without making the test slow. |
| 57 | const NAMES = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel']; |
| 58 | await p.evaluate(async (names) => { |
| 59 | const m = await import('/pkg/oxedyne_daimond.js'); |
| 60 | const app = new m.DaimondApp('http://127.0.0.1/v1/chat/completions', '', 'none', 4096, '', true); |
| 61 | for (const n of names) { try { await app.create_diamond(n); } catch (e) { /* enough */ } } |
| 62 | }, NAMES); |
| 63 | await p.waitForTimeout(600); |
| 64 | |
| 65 | if (BREAK) { |
| 66 | // The un-coalesced original, put back on the page: every ask starts a walk. |
| 67 | await p.evaluate(() => { |
| 68 | window.__origLoad = window.DaimondCore.loadDiamonds; |
| 69 | window.DaimondCore.loadDiamonds = async function () { |
| 70 | var m = await import('/pkg/oxedyne_daimond.js'); |
| 71 | var app = new m.DaimondApp('http://127.0.0.1/v1/chat/completions', '', 'none', 4096, '', true); |
| 72 | try { await app.list_diamonds(); } catch (e) { /* the walk is the point */ } |
| 73 | }; |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | // ── A + B. Five asks at once ──────────────────────────────────────── |
| 78 | await p.evaluate(() => { try { DaimondTrail.clear(); } catch (e) {} }); |
| 79 | await p.evaluate(() => { |
| 80 | var f = window.DaimondCore.loadDiamonds; |
| 81 | for (var i = 0; i < 5; i++) { try { f(); } catch (e) {} } |
| 82 | }); |
| 83 | await p.waitForTimeout(4000); |
| 84 | |
| 85 | const rows = await p.evaluate(() => { |
| 86 | try { return window.DaimondTrail.rows() || []; } catch (e) { return []; } |
| 87 | }); |
| 88 | |
| 89 | const starts = rows.filter((r) => r.w === 'list_diamonds'); |
| 90 | check(starts.length > 0, 'the walk reports itself at all', `${starts.length} start(s)`); |
| 91 | check(starts.length <= 2, |
| 92 | 'FIVE asks at once produce at most TWO walks — one now, one after, never five', |
| 93 | `${starts.length} walks`); |
| 94 | |
| 95 | // Overlap, measured properly: between one walk's start and the next walk's |
| 96 | // start there must be a "N entries" line AND the last entry of that walk. A |
| 97 | // second start arriving before the first walk's entries are done is the fault. |
| 98 | { |
| 99 | let overlapped = null; |
| 100 | for (let i = 0; i < starts.length - 1; i++) { |
| 101 | const a = starts[i].t, b = starts[i + 1].t; |
| 102 | const between = rows.filter((r) => r.t >= a && r.t < b && r.w === 'list entry').length; |
| 103 | const total = Number(((rows.find((r) => r.t >= a && r.w === 'list' && / entries$/.test(r.d || '')) || {}).d || '0').split(' ')[0]); |
| 104 | if (total > 0 && between < total) { |
| 105 | overlapped = { walk: i, sawEntries: between, of: total }; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | check(overlapped === null, |
| 110 | 'and no walk begins while another is still reading entries', |
| 111 | overlapped ? JSON.stringify(overlapped) : `${starts.length} walk(s), each finished before the next`); |
| 112 | } |
| 113 | |
| 114 | // ── C. An ask during a walk still gets fresh data ─────────────────── |
| 115 | if (!BREAK) { |
| 116 | await p.evaluate(() => { try { DaimondTrail.clear(); } catch (e) {} }); |
| 117 | const seen = await p.evaluate(async () => { |
| 118 | var f = window.DaimondCore.loadDiamonds; |
| 119 | var first = f(); // a walk is now running |
| 120 | // Write DURING it, then ask again: the second ask must not be handed the |
| 121 | // walk that began before the write. |
| 122 | var m = await import('/pkg/oxedyne_daimond.js'); |
| 123 | var app = new m.DaimondApp('http://127.0.0.1/v1/chat/completions', '', 'none', 4096, '', true); |
| 124 | await app.create_diamond('Written-Mid-Walk'); |
| 125 | await f(); |
| 126 | await first; |
| 127 | return [...document.querySelectorAll('#diamond-list .diamond-box')] |
| 128 | .map((b) => (b.querySelector('.session-box-name') || {}).textContent); |
| 129 | }); |
| 130 | check(seen.indexOf('Written-Mid-Walk') !== -1, |
| 131 | 'a Diamond written DURING a walk is on the rail afterwards — the ask is coalesced, not dropped', |
| 132 | JSON.stringify(seen.slice(0, 4)) + ` (${seen.length} tiles)`); |
| 133 | |
| 134 | // ── D. And the rail agrees with the store ─────────────────────── |
| 135 | const store = await p.evaluate(async () => { |
| 136 | const m = await import('/pkg/oxedyne_daimond.js'); |
| 137 | const app = new m.DaimondApp('http://127.0.0.1/v1/chat/completions', '', 'none', 4096, '', true); |
| 138 | return JSON.parse(await app.list_diamonds()).length; |
| 139 | }); |
| 140 | check(seen.length === store, 'and the rail shows every Diamond the store holds', |
| 141 | `${seen.length} tiles, ${store} in the store`); |
| 142 | } |
| 143 | |
| 144 | const noise = /favicon|401|402|502|Unauthorized|Payment|Bad Gateway/i; |
| 145 | const errs = s.errs.filter((e) => !noise.test(e)); |
| 146 | check(errs.length === 0, 'no console errors', JSON.stringify(errs.slice(0, 2))); |
| 147 | |
| 148 | await s.close(); |
| 149 | try { fs.rmSync(PROFILE, { recursive: true, force: true }); } catch (e) { /* gone */ } |
| 150 | |
| 151 | console.log(out.join('\n')); |
| 152 | const total = out.filter((l) => /^(PASS|FAIL)/.test(l)).length; |
| 153 | if (BREAK) { |
| 154 | console.log(`\nBROKEN RUN: ${bad} of ${total} failed. ` |
| 155 | + (bad > 0 ? 'Good — coalescing is what stops the pile-up.' : 'BAD — a check that cannot fail is not evidence.')); |
| 156 | process.exit(bad > 0 ? 0 : 1); |
| 157 | } |
| 158 | console.log(bad === 0 ? `\nALL ${total} CHECKS PASSED` : `\n${bad} of ${total} FAILED`); |
| 159 | process.exit(bad === 0 ? 0 : 1); |