Files
Rosea/.agents/features/two-tabs.md
T

4.2 KiB

Two tabs, one garden

Status: built on fix/two-tabs. CLAUDE.md §8, Two tabs, one database (predicted): two event streams into one store; derived state drifts. Use a BroadcastChannel lock. Review: fix-two-tabs.md.

1. Why and what, in the player's terms

A player may have the garden open twice: two browser tabs, or the installed app and a tab. Whatever they do in one is now seen in the other within a moment, without a reload: care given, a petal opened, a rose brought back from a file, a town chosen.

Measured first (2026-11-07). Two e2e tests, written before the fix, failed: a tab already open did not see care given in the other, nor a town chosen there. The prediction was half right:

  • No stored state drifts. There is no derived store yet; the log is append-only and every write is its own IndexedDB transaction (auto-increment ids), so two writers cannot overwrite each other. The identity's first write already wins in a transaction (rose.md). No lock is needed.
  • The views drift. Each tab read the log once, at start. The harm: she is watered in one tab, the other still shows her thirsty, the player waters again, and she is too full — a mistake the game made, blamed on her care.

2. Technical architecture

flowchart LR
  a["tab A<br/>packages/storage: appendCare"] -->|"IndexedDB write, then post 'log'"| bc(("BroadcastChannel<br/>rosea-changes"))
  bc -->|"'log' | 'place'"| b["tab B<br/>packages/storage: onChange"]
  b --> app["apps/rosea wiring"] --> ui["packages/ui<br/>Rose / Shell reload from storage"]
# Decision Why
W1 packages/storage announces its own writes on a BroadcastChannel named after the database, after the write commits: 'log' (care, a petal, a rose brought back) or 'place' (the town). One module owns every write, so no write path can forget to announce. After the commit, so the other tab reads what was written.
W2 The message is only what changed, never the data. The other tab reads storage again. Storage stays the truth; a message can be lost or arrive twice and nothing breaks.
W3 A tab does not hear itself (a BroadcastChannel never delivers to the object that posted). No double reload.
W4 No lock. §1: nothing a lock would protect exists yet. When a derived store arrives, its feature doc decides again.
W5 Where BroadcastChannel does not exist, nothing is announced and each tab is as before. Safari has it since 15.4; an older browser loses only the live update, never data.
W6 Nothing on screen says another tab acted. The garden simply is as it is. Calm; no "updated elsewhere" notices.

3. Risk analysis

Risk Answer
Blame for the game's mistake (watered twice, too full). The fix itself.
Privacy: a message leaves the tab. A BroadcastChannel reaches only this origin's own tabs and workers in this browser profile; it carries one word, never data. Nothing leaves the device.
A message from another origin or a malformed one. The channel is same-origin by definition; anything but 'log' or 'place' is ignored.
Reachability, offline Not affected; works offline.

4. Gap analysis

Closes CLAUDE.md §8's two-tabs trap (measured, answered) and the review checklist's two tabs open for the log and the town. Not covered: a tab that is mid-pour when the other tab's care arrives keeps pouring against the new water level (that is the correct level).

5. Verification matrix

Case Kind Where
Each write announces after it commits: care, petal, rose brought back → 'log'; town → 'place' unit storage/test/changes.test.ts
A failed write announces nothing; a tab does not hear itself; a malformed message is ignored; unsubscribe stops it; close() closes the channel unit same
Without BroadcastChannel, storage still works unit same
The rose reloads her log on 'log'; the shell reloads the town on 'place' unit ui/test/two-tabs.test.ts
Built app, two tabs: care in A seen in B without reload; a town chosen in A shown in B e2e e2e/shell.spec.ts (failed before the fix: the measurement)