Repository navigation
Commit d3c875f
feat(web): scope unsent composer drafts per window
## What Changed
Unsent composer drafts (draft sessions, their composer drafts, the project-to-session map) persist per window in `<key>:window:<token>`; the token lives in `sessionStorage`. Thread drafts and sticky model choices stay in the shared `<key>` bucket. All logic lives in `apps/web/src/windowDraftScope.fork.ts`; `composerDraftStore.ts` carries four marked hooks and loses no upstream line.
| Concern | Behaviour |
| --- | --- |
| Shared writes | only the slices this window changed since it last read or wrote; deletions honoured, other windows' edits kept |
| Flush | one flush (debounce or `beforeunload`) writes both buckets |
| Ownership | each live window holds a Web Lock named for its token for its lifetime |
| Adoption | at startup, under an exclusive adopt lock, a window adopts legacy draft sessions in the shared bucket and every window bucket whose token lock is not held (closed tab, previous Electron run) and whose `touchedAt` is older than 5s |
| Reload grace | every write and flush, the unload one included, stamps `touchedAt`; an unlocked fresh bucket is re-checked once after 5s with a fresh `query()`, so a reloading window keeps its bucket and a closed one is still recovered |
| Live store | adopted entries pass through the store's own hydrate `migrate`/`merge`, then `setState` adds only keys the store lacks; no rehydrate, so unsaved in-memory state survives |
| Write order | adopting window's bucket first, then the sources are cleared or removed |
| Conflicts | this window's entries win; sources fill gaps; two sources mapping one project keep the newer session by `createdAt`, first source on a tie; the other session keeps its text unmapped |
## Why
Two windows on one route shared and clobbered unsent drafts. Electron uses hash routes, so `pathname` is always `/` and every project there resolves to the hub key.
## Checklist
- [x] This PR is small and focused
- [x] I explained what changed and why
- [x] No UI changes (persistence scoping only, no screenshots)
- [x] No animation/interaction changes (no video)
## Close conditions
- [x] **Settle:** test proves today's hash-route key
- [x] **Unsent:** drafts are per window
- [x] **Durable:** thread drafts stay shared
- [x] **Migration:** never deletes draft text
## Notes
Tests use happy-dom `Storage` and a FIFO lock manager with `query()`: simultaneous upgrade, write order, adoption mid-debounce, reload after adopt, durable clear, interleaved edits, unload flush, closed-window recovery, relaunch with two orphans, live bucket untouched, reload inside the grace window, fresh-then-closed re-check, adopted drafts merged into a real zustand persist store beside an unsaved in-memory image; a controlled clock drives every timer.
- **No Web Locks API:** ownership is unknowable, so orphan buckets are left in place and not adopted.
- **Duplicated web tab:** copies `sessionStorage` and shares the token.
Closes #1346
## Fork trailers
Squashes:
- a2a5342 feat(web): scope unsent composer drafts per window
- cbb2302 fixup! feat(web): scope unsent composer drafts per window
Fork-Domain: multi-window
Fork-Tier: core1 parent f2151b6 commit d3c875f
3 files changed
Lines changed: 1100 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
158 | 159 | | |
159 | 160 | | |
160 | 161 | | |
| 162 | + | |
161 | 163 | | |
162 | 164 | | |
163 | 165 | | |
| |||
2228 | 2230 | | |
2229 | 2231 | | |
2230 | 2232 | | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
2231 | 2236 | | |
2232 | 2237 | | |
2233 | 2238 | | |
| |||
4247 | 4252 | | |
4248 | 4253 | | |
4249 | 4254 | | |
| 4255 | + | |
4250 | 4256 | | |
4251 | 4257 | | |
4252 | 4258 | | |
| |||
0 commit comments