diff --git a/CHANGELOG.md b/CHANGELOG.md index cb7679b..ffd0d86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- **Two windows no longer lose an edit in the store.** When window B was typing as window A saved, B saved its words on top of A's model, but A's older save could reach localStorage after B's. Both windows looked right and the store held A's model without B's words, so a reload lost them. A CI probe caught this on Firefox in 12 of 180 runs under load and 0 of 60 alone. A window now watches the `storage` event: when the store holds a model it already built on, it writes its newer model back, and it writes the log back when entries are missing. This was the Firefox two-window failure on `main`. - **`validate --suggest`: the layouts and templates that fit a slide's content (#207).** `node bin/validate.mjs --suggest "three or four numbers with movement"` prints the top five of the 32 library layouts and 115 templates with kind, density and slot or fill keys. `lib/suggest.mjs` scores each by idf-weighted word overlap with its name, its `use` or `note`, and the SKILL.md routing table, which it reads at run time so the two cannot disagree. SKILL.md START HERE names it. Gate: test/suggest.test.mjs (every routing-table row finds its layout in the top three, a template is found by its note, unknown words still answer five, the CLI prints the ranking). - **JSON Schema for model.json and style.json (#203).** `schemas/model.schema.json` and `schemas/style.schema.json` (draft 2020-12) carry the structure and every enum the engine checks: formats, pages, densities, the 115 template ids, anims, arrows, heads, connector styles, plates, chart marks and the eight roles. `node schemas/build.mjs` writes both from the constants in code (`lib/chart.mjs` now exports `MARKS`), so an enum cannot drift. `bin/new.mjs` writes `$schema` as the model's first key, relative to the model, so editors complete and flag props; `create` strips it before hashing or embedding, so no local path reaches a deck and the deck id is unchanged. `validate` still owns fit, air, density and coverage. The package ships `schemas/`. Gate: test/schema.test.mjs (committed files equal the build output, every enum equals its constant, every example model and style kit passes, the `new.mjs` round trip, the package list). - **Coded validate diagnostics (#201, validate half).** Every one of `validate`'s 134 findings carries a code (`fit/nowrap-width`, `air/gap`, `coverage/text-only`, seventeen families in all, catalogued in docs/verify.md) and lands in a new `diagnostics` array beside the unchanged `errors` and `warnings` strings: `{code, severity, message, subject: {slide, el, id}, evidence?, fixes}`. `fit/nowrap-width` carries `{need, have}` and offers `w: `. `node bin/validate.mjs model.json --json` prints them, and the `finalize` receipt carries them per stage. Gate: test/diagnostics.test.mjs (a static check that every call site passes a code, the catalogue covers every family, the strings are unchanged on every example, `--json` matches the in-process result, and the receipt carries them). The verify half follows. diff --git a/deck.html b/deck.html index e236bb5..732664a 100644 --- a/deck.html +++ b/deck.html @@ -632,15 +632,16 @@ addEventListener('beforeunload',e=>{commitEdit();if(route()==='tab'&&unsynced){e.preventDefault();e.returnValue=''}}); // ── sync: the other windows and tabs of this browser showing this deck. Every save is told to them — a `storage` event on the // localStorage tier, a BroadcastChannel on any tier that has one (Safari 15.4+) — and a window that hears one takes the model as -// its own: last write wins, the slide you were on is kept by id, an edit in flight lands on top of theirs, and NOTHING is written -// in response (no echo: a second window is a reader of what it hears). Logs merge by entry, so no window's edits go missing from +// its own: last write wins, the slide you were on is kept by id, an edit in flight lands on top of theirs. Logs merge by entry, so no window's edits go missing from // the replay; a file another agent version wrote (a different rev) is left to the reload path, which reconciles by log. const bc=(()=>{try{return new BroadcastChannel(NS)}catch{return null}})(); const tell=m=>{if(bc)bc.postMessage(m)}; if(bc)bc.onmessage=e=>receive(e.data); -addEventListener('storage',e=>{if(e.key===KEY)receive({model:e.newValue});else if(e.key===LKEY)receive({log:e.newValue})}); +addEventListener('storage',e=>{if(e.key===KEY){receive({model:e.newValue});restoreModel(e.newValue)}else if(e.key===LKEY){receive({log:e.newValue});restoreLog()}}); let heard=null;const ekey=e=>JSON.stringify({...e,rev:undefined}); // an entry's identity: what changed and when — never the rev a later write stamped on it function mergeLog(rl){const mine=new Map(log.map(e=>[ekey(e),e]));let ch=false;for(const e of rl){const k=ekey(e),m=mine.get(k);if(!m){log.push(e);mine.set(k,e);ch=true}else if(e.rev&&!m.rev){m.rev=e.rev;ch=true}}if(ch){log.sort((a,b)=>(a.t||'').localeCompare(b.t||''));unsynced=log.filter(e=>!e.rev).length}return ch} +const restoreModel=v=>{if(LS&&v!=null&&v===heard&&base!==heard&&LS.getItem(KEY)===v)store.set(KEY,base)}; +const restoreLog=()=>{if(!LS)return;let held;try{held=new Set(JSON.parse(LS.getItem(LKEY)).map(ekey))}catch{return}if(log.some(e=>!held.has(ekey(e))))store.set(LKEY,JSON.stringify(log))}; function receive(m){ if(!m)return;if(m.log)try{mergeLog(JSON.parse(m.log))}catch{} if(m.model==null||m.model===base||m.model===heard)return;heard=m.model;let d;try{d=JSON.parse(m.model)}catch{return}if(!d||d.rev!==DECK.rev)return; // one save reaches a window twice on the localStorage tier (the storage event and the channel): the second copy is ignored diff --git a/docs/editor.md b/docs/editor.md index 0f81370..ab03653 100644 --- a/docs/editor.md +++ b/docs/editor.md @@ -30,7 +30,7 @@ This manifest is a contract: the gate compares it against the template, so the H ## Persistence, honestly -**Persistence, honestly.** Every edit autosaves to the browser's storage under `deck.id` — localStorage; IndexedDB where localStorage is refused (a cross-site iframe); memory as the last resort — and is appended to the in-file **edit log** (slide id, row id, keys before → after). This holds on a desktop, on a phone (a touch edit persists; switching apps commits the edit in flight) and inside an iframe (same origin, srcdoc, or cross-site on whichever tier the browser allows). Every window or tab of the same browser showing the deck picks up an edit live (`storage` events + a BroadcastChannel): last write wins, each tab keeps its own slide, an edit in flight lands on top of what arrived, and a window that hears an edit never writes back. **The deck file writes itself:** in Chrome/Edge the first ⌘S (or a tap on the dot) asks for the file once (File System Access, remembered per browser), and from then on every edit rewrites it in place on an 800 ms trailing debounce — ⌘S, the dot, a hidden tab or an unload flush the pending write now — with the model and the log inside, so an agent reading the file sees exactly what the human did. The dot is green only when the file has everything; amber counts the edits that have not reached it. Safari (and any browser without File System Access) cannot write the file: there ⌘S downloads a self-contained copy. Before each write the page compares the file's `lastModified` with the value from its last read or write; when another program (an agent's `create --from`, another editor) changed the file, the page does not write, reads the new file, and replays its unwritten edits on top, exactly as the hosted page does. A live text edit or a drag in flight is committed when the tab hides or unloads, so a refresh mid-edit loses nothing. +**Persistence, honestly.** Every edit autosaves to the browser's storage under `deck.id` — localStorage; IndexedDB where localStorage is refused (a cross-site iframe); memory as the last resort — and is appended to the in-file **edit log** (slide id, row id, keys before → after). This holds on a desktop, on a phone (a touch edit persists; switching apps commits the edit in flight) and inside an iframe (same origin, srcdoc, or cross-site on whichever tier the browser allows). Every window or tab of the same browser showing the deck picks up an edit live (`storage` events + a BroadcastChannel): last write wins, each tab keeps its own slide, an edit in flight lands on top of what arrived, and a window that hears an edit writes nothing back. One exception: when an older save from another window lands in the store after this window saved on top of it, this window puts its newer model and any missing log entries back. **The deck file writes itself:** in Chrome/Edge the first ⌘S (or a tap on the dot) asks for the file once (File System Access, remembered per browser), and from then on every edit rewrites it in place on an 800 ms trailing debounce — ⌘S, the dot, a hidden tab or an unload flush the pending write now — with the model and the log inside, so an agent reading the file sees exactly what the human did. The dot is green only when the file has everything; amber counts the edits that have not reached it. Safari (and any browser without File System Access) cannot write the file: there ⌘S downloads a self-contained copy. Before each write the page compares the file's `lastModified` with the value from its last read or write; when another program (an agent's `create --from`, another editor) changed the file, the page does not write, reads the new file, and replays its unwritten edits on top, exactly as the hosted page does. A live text edit or a drag in flight is committed when the tab hides or unloads, so a refresh mid-edit loses nothing. **Save routes.** The page picks one at load by what the browser can do, never by its name: the host (a `bin/serve.mjs` probe answered), then a file handle (File System Access, with a handle this browser still trusts), then a durable store (localStorage, or IndexedDB), then the tab. The tab route covers Safari on `file://`, sandboxed frames and embedded browsers that refuse storage: the page keeps the model and the unsaved log in `window.name`, with `history.state` as a second copy, both tagged with the deck id, so a reload in the same tab restores the edits and a copy another deck left in the tab is ignored. The save button reads **Not saving** in amber, its tooltip and a banner shown once at load (dismissed, it stays gone for that tab) say `This browser cannot save here. Edits survive reload in this tab only. Host it: node bin/serve.mjs `, and leaving the page asks first while edits exist only in the tab (embedded browsers may suppress that prompt). Closing the tab loses them; ⌘S still downloads a copy where downloads work. **Position:** a new window opens on slide 1; a refresh, and a new version of the file, keep the slide you were on — by slide id, so inserting slides above does not move you. A stored working copy is trusted only on the same `rev`; a newer file wins as the base and the browser replays its own edit log onto it (human wins, conflicts flagged on the entries), so an agent's rewrite neither hides its changes nor drops the human's. **Versions:** none in the file since 0.9.0. Version history is weave's job; decklet keeps the edit log, which `create --from` replays. diff --git a/library.html b/library.html index e243906..ae5b984 100644 --- a/library.html +++ b/library.html @@ -631,15 +631,16 @@ addEventListener('beforeunload',e=>{commitEdit();if(route()==='tab'&&unsynced){e.preventDefault();e.returnValue=''}}); // ── sync: the other windows and tabs of this browser showing this deck. Every save is told to them — a `storage` event on the // localStorage tier, a BroadcastChannel on any tier that has one (Safari 15.4+) — and a window that hears one takes the model as -// its own: last write wins, the slide you were on is kept by id, an edit in flight lands on top of theirs, and NOTHING is written -// in response (no echo: a second window is a reader of what it hears). Logs merge by entry, so no window's edits go missing from +// its own: last write wins, the slide you were on is kept by id, an edit in flight lands on top of theirs. Logs merge by entry, so no window's edits go missing from // the replay; a file another agent version wrote (a different rev) is left to the reload path, which reconciles by log. const bc=(()=>{try{return new BroadcastChannel(NS)}catch{return null}})(); const tell=m=>{if(bc)bc.postMessage(m)}; if(bc)bc.onmessage=e=>receive(e.data); -addEventListener('storage',e=>{if(e.key===KEY)receive({model:e.newValue});else if(e.key===LKEY)receive({log:e.newValue})}); +addEventListener('storage',e=>{if(e.key===KEY){receive({model:e.newValue});restoreModel(e.newValue)}else if(e.key===LKEY){receive({log:e.newValue});restoreLog()}}); let heard=null;const ekey=e=>JSON.stringify({...e,rev:undefined}); // an entry's identity: what changed and when — never the rev a later write stamped on it function mergeLog(rl){const mine=new Map(log.map(e=>[ekey(e),e]));let ch=false;for(const e of rl){const k=ekey(e),m=mine.get(k);if(!m){log.push(e);mine.set(k,e);ch=true}else if(e.rev&&!m.rev){m.rev=e.rev;ch=true}}if(ch){log.sort((a,b)=>(a.t||'').localeCompare(b.t||''));unsynced=log.filter(e=>!e.rev).length}return ch} +const restoreModel=v=>{if(LS&&v!=null&&v===heard&&base!==heard&&LS.getItem(KEY)===v)store.set(KEY,base)}; +const restoreLog=()=>{if(!LS)return;let held;try{held=new Set(JSON.parse(LS.getItem(LKEY)).map(ekey))}catch{return}if(log.some(e=>!held.has(ekey(e))))store.set(LKEY,JSON.stringify(log))}; function receive(m){ if(!m)return;if(m.log)try{mergeLog(JSON.parse(m.log))}catch{} if(m.model==null||m.model===base||m.model===heard)return;heard=m.model;let d;try{d=JSON.parse(m.model)}catch{return}if(!d||d.rev!==DECK.rev)return; // one save reaches a window twice on the localStorage tier (the storage event and the channel): the second copy is ignored diff --git a/template.html b/template.html index 5c6bf2d..9b4cd70 100644 --- a/template.html +++ b/template.html @@ -631,15 +631,16 @@ addEventListener('beforeunload',e=>{commitEdit();if(route()==='tab'&&unsynced){e.preventDefault();e.returnValue=''}}); // ── sync: the other windows and tabs of this browser showing this deck. Every save is told to them — a `storage` event on the // localStorage tier, a BroadcastChannel on any tier that has one (Safari 15.4+) — and a window that hears one takes the model as -// its own: last write wins, the slide you were on is kept by id, an edit in flight lands on top of theirs, and NOTHING is written -// in response (no echo: a second window is a reader of what it hears). Logs merge by entry, so no window's edits go missing from +// its own: last write wins, the slide you were on is kept by id, an edit in flight lands on top of theirs. Logs merge by entry, so no window's edits go missing from // the replay; a file another agent version wrote (a different rev) is left to the reload path, which reconciles by log. const bc=(()=>{try{return new BroadcastChannel(NS)}catch{return null}})(); const tell=m=>{if(bc)bc.postMessage(m)}; if(bc)bc.onmessage=e=>receive(e.data); -addEventListener('storage',e=>{if(e.key===KEY)receive({model:e.newValue});else if(e.key===LKEY)receive({log:e.newValue})}); +addEventListener('storage',e=>{if(e.key===KEY){receive({model:e.newValue});restoreModel(e.newValue)}else if(e.key===LKEY){receive({log:e.newValue});restoreLog()}}); let heard=null;const ekey=e=>JSON.stringify({...e,rev:undefined}); // an entry's identity: what changed and when — never the rev a later write stamped on it function mergeLog(rl){const mine=new Map(log.map(e=>[ekey(e),e]));let ch=false;for(const e of rl){const k=ekey(e),m=mine.get(k);if(!m){log.push(e);mine.set(k,e);ch=true}else if(e.rev&&!m.rev){m.rev=e.rev;ch=true}}if(ch){log.sort((a,b)=>(a.t||'').localeCompare(b.t||''));unsynced=log.filter(e=>!e.rev).length}return ch} +const restoreModel=v=>{if(LS&&v!=null&&v===heard&&base!==heard&&LS.getItem(KEY)===v)store.set(KEY,base)}; +const restoreLog=()=>{if(!LS)return;let held;try{held=new Set(JSON.parse(LS.getItem(LKEY)).map(ekey))}catch{return}if(log.some(e=>!held.has(ekey(e))))store.set(LKEY,JSON.stringify(log))}; function receive(m){ if(!m)return;if(m.log)try{mergeLog(JSON.parse(m.log))}catch{} if(m.model==null||m.model===base||m.model===heard)return;heard=m.model;let d;try{d=JSON.parse(m.model)}catch{return}if(!d||d.rev!==DECK.rev)return; // one save reaches a window twice on the localStorage tier (the storage event and the channel): the second copy is ignored diff --git a/test/autosave.test.mjs b/test/autosave.test.mjs index 2168e69..353176c 100644 --- a/test/autosave.test.mjs +++ b/test/autosave.test.mjs @@ -57,7 +57,7 @@ test('storage is a tier chain — localStorage → IndexedDB → memory — and assert.match(tpl, /let TIER=LS\?'local':'mem';/); assert.match(tpl, /indexedDB\.open\('decklet',2\)/, 'one db, bumped to v2 for the kv store'); assert.match(tpl, /for\(const s of \['h','kv'\]\)if\(!r\.result\.objectStoreNames\.contains\(s\)\)r\.result\.createObjectStore\(s\)/, 'the handle store and the kv store'); assert.match(tpl, /async function idbLoad\(\)/); assert.match(tpl, /if\(LS\)settle\(\);else\{autosave\('busy','Loading…'\);idbLoad\(\)\.then\(settle\)\}/, 'the deck draws from DECK first, the IndexedDB copy lands after'); - assert.match(tpl, /new BroadcastChannel\(NS\)/); assert.match(tpl, /addEventListener\('storage',e=>\{if\(e\.key===KEY\)receive/); + assert.match(tpl, /new BroadcastChannel\(NS\)/); assert.match(tpl, /addEventListener\('storage',e=>\{if\(e\.key===KEY\)\{receive/); const recv = tpl.slice(tpl.indexOf('function receive(m){'), tpl.indexOf('const slide=()=>')); assert.ok(!/store\.set\(|LS\.setItem|localStorage\.setItem|tell\(/.test(recv), 'receive() writes nothing and tells no one: ' + recv); assert.match(recv, /commitEdit\(\)/, 'an edit in flight is committed on top of what was heard'); @@ -163,3 +163,23 @@ for (const bn of projects()) live(`two windows (${bn}): an edit in one shows in assert.deepEqual(JSON.parse(await A.evaluate(() => localStorage.getItem(KEY))).slides[0].els.map(e => [e.text, e.x, e.y]).slice(0, 2), [['typed in B', 60, 80], ['body one', 123, 250]], 'the store has both windows\' edits'); assert.deepEqual([...A.errs, ...B.errs], []); })); +for (const bn of projects()) live(`two windows (${bn}): an older save landing after an edit on top of it never replaces that edit in the store`, async () => withProject(pw, bn, async ({context}) => { + const url = `http://127.0.0.1:${server.address().port}/late-${bn}.html`; const ctx = await context(); + const A = watch(await ctx.newPage()); await A.goto(url); await A.evaluate(() => localStorage.clear()); await A.reload(); await settled(A); + const B = watch(await ctx.newPage()); await B.goto(url); await settled(B); + await B.evaluate(() => { sel.clear(); sel.add(0); render(); edit(0); }); await B.keyboard.type('typed in B'); + await A.evaluate(() => { snap(); slide().els[1].y = 250; save(); window.__older = [localStorage.getItem(KEY), localStorage.getItem(LKEY)]; }); + await until(B, () => deck.slides[0].els[1].y === 250); + await until(A, () => deck.slides[0].els[0].text === 'typed in B'); + await until(A, () => JSON.parse(localStorage.getItem(KEY)).slides[0].els[0].text === 'typed in B'); + const count = p => p.evaluate(() => { window.__sets = 0; const o = Storage.prototype.setItem; Storage.prototype.setItem = function (...a) { if (this === localStorage) window.__sets++; return o.apply(this, a); }; }); + await count(B); + await A.evaluate(() => { localStorage.setItem(KEY, window.__older[0]); localStorage.setItem(LKEY, window.__older[1]); }); + await until(A, () => JSON.parse(localStorage.getItem(KEY)).slides[0].els[0].text === 'typed in B'); + await until(A, () => JSON.parse(localStorage.getItem(LKEY)).length === 2); + assert.deepEqual(JSON.parse(await A.evaluate(() => localStorage.getItem(KEY))).slides[0].els.map(e => [e.text, e.y]).slice(0, 2), [['typed in B', 80], ['body one', 250]], 'the store has both windows\' edits again'); + assert.equal(await B.evaluate(() => window.__sets), 2, 'B wrote the model and the log once each'); + await A.reload(); await settled(A); + assert.equal(await A.evaluate(() => slide().els[0].text), 'typed in B', 'a reload keeps the words'); + assert.deepEqual([...A.errs, ...B.errs], []); +}));