fix(sidebar): 同源多个页面写引用记录不再互相覆盖(否则对方的 chip 发送被拒)

insertQuoteChip 只在首次读存储,saveStore 却用本实例内存整体重写 key。两个
标签页交替引用时,后写的一方把另一方的记录抹掉,那枚 chip 发送时 codec 报
no longer available,宿主拒发。写之前先把存储并进内存,编号也随之不再相撞。

只影响浏览器访问(同 origin 多页面);桌面端只有一个主窗口,不会触发。
测试:selection-quote.spec 新增 2 例(两个 fresh 模块共用一个 Storage)。
This commit is contained in:
晴猫 committed 2026-11-05 18:54:03 +09:00
1 parent 11e6c8e63a
commit f4ba2b7930
2 files changed
+56 -1

No files matched your search

+4
View File
@@ -402,6 +402,10 @@ export function insertQuoteChip(ctx: Context, sessionId: string, sel: QuoteSelec
}
}
loadStore()
// `saveStore` rewrites the whole key from this instance's memory, and other
// pages of the origin write the same key: merge their records in first, or
// the write erases them and their chips block the send in the codec.
readStoreIntoMemory()
const n = nextN++
const clipboardText = headerOf(sel.path, sel.cwd, sel.lines)
// Built ONCE and kept in its size-reduced form (a raw selection is
+52 -1
View File
@@ -4,7 +4,8 @@
* (record + localStorage persistence, structured reference minted through the
* #1052 composer facade), the codec (send-time serialization back to the
* fenced block, header-line clipboard text), and the click-through (open back
* at the selection's line span, session-scoped).
* at the selection's line span, session-scoped), and the store shared by
* several pages of one origin.
*
* Everything under test lives in a module with cross-test state (the
* registry, the record store), so every test pulls a FRESH module instance
@@ -312,6 +313,56 @@ describe('lifecycle self-heal (a disposed fiber must not kill the feature silent
})
})
describe('several pages of one origin (one shared store, one memory per page)', () => {
/** One browser tab: its own module instance and host, the same localStorage. */
async function openPage() {
const mod = await freshModule()
const triggers = fakeTriggers()
const composer = fakeComposer()
const ctx = fakeCtx({ triggers, composer })
/** Commit one selection and return the ref of the chip it minted. */
const quote = (): string => {
expect(mod.insertQuoteChip(ctx, 's1', SELECTION)).toBe(true)
return (composer.insertReference.mock.calls.at(-1)?.[0] as { ref: string }).ref
}
return { mod, triggers, ctx, quote }
}
it('keeps the other page\'s records when a page rewrites the store (a chip must never go dead)', async () => {
const pageA = await openPage()
const pageB = await openPage()
const refs = [pageA.quote(), pageB.quote(), pageA.quote(), pageB.quote()]
const stored = storedJson(pageA.mod) as { id: string; n: number }[]
expect(stored.map(record => record.id)).toEqual(refs)
// The numbering keeps growing across pages instead of colliding.
expect(stored.map(record => record.n)).toEqual([1, 2, 3, 4])
// A reloaded page has nothing but the store: every chip must still send.
const reloaded = await openPage()
reloaded.mod.registerQuoteSource(reloaded.ctx)
const codec = reloaded.triggers.captured[0]?.codec
for (const ref of refs) {
await expect(codec?.serialize(ref, new AbortController().signal)).resolves.toBe(
'```src/a.ts:2-3\nconst x = 1\n```',
)
}
})
it('still caps the merged store FIFO', async () => {
const pageA = await openPage()
const pageB = await openPage()
const oldest = pageA.quote()
for (let i = 0; i < pageB.mod.QUOTE_STORE_CAP; i++) pageB.quote()
const newest = pageA.quote()
const ids = (storedJson(pageA.mod) as { id: string }[]).map(record => record.id)
expect(ids).toHaveLength(pageA.mod.QUOTE_STORE_CAP)
expect(ids).not.toContain(oldest)
expect(ids.at(-1)).toBe(newest)
})
})
describe('the registered source', () => {
/** Register, commit one selection, and return the captured source + record. */
async function withChip() {