From f4ba2b7930db3b85fc7363d4897445ec144d0c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E7=8C=AB?= Date: Thu, 5 Nov 2026 18:18:18 +0900 Subject: [PATCH] =?UTF-8?q?fix(sidebar):=20=E5=90=8C=E6=BA=90=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E9=A1=B5=E9=9D=A2=E5=86=99=E5=BC=95=E7=94=A8=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E4=B8=8D=E5=86=8D=E4=BA=92=E7=9B=B8=E8=A6=86=E7=9B=96?= =?UTF-8?q?=EF=BC=88=E5=90=A6=E5=88=99=E5=AF=B9=E6=96=B9=E7=9A=84=20chip?= =?UTF-8?q?=20=E5=8F=91=E9=80=81=E8=A2=AB=E6=8B=92=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit insertQuoteChip 只在首次读存储,saveStore 却用本实例内存整体重写 key。两个 标签页交替引用时,后写的一方把另一方的记录抹掉,那枚 chip 发送时 codec 报 no longer available,宿主拒发。写之前先把存储并进内存,编号也随之不再相撞。 只影响浏览器访问(同 origin 多页面);桌面端只有一个主窗口,不会触发。 测试:selection-quote.spec 新增 2 例(两个 fresh 模块共用一个 Storage)。 --- src/client/selection-quote.ts | 4 +++ tests/selection-quote.spec.ts | 53 ++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/client/selection-quote.ts b/src/client/selection-quote.ts index f6169b14..99368cfb 100644 --- a/src/client/selection-quote.ts +++ b/src/client/selection-quote.ts @@ -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 diff --git a/tests/selection-quote.spec.ts b/tests/selection-quote.spec.ts index 8f60db1a..6dc13c83 100644 --- a/tests/selection-quote.spec.ts +++ b/tests/selection-quote.spec.ts @@ -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() {