Skip to content

Commit ffa500a

Browse files
answer the 2414 rows on the view-state host double
Threads 4575030437 and 4575030442 asked the same thing of two different members of makeViewHost: the double answered for production instead of running it. - getPersistedViewStates, prunePersistedViewStates and isViewStateGenerationLive are bound to the prototype now, the way postViewStateClaimToWebview and enqueueViewStateWrite already were. Only storage and the message transport stay mocked, as the thread asked. - The restore-failure case stores the key and value production actually sent instead of installing a fixed record, so a wrong re-key payload cannot pass it. - The rollback case is split. While the entry still carries the minted token the rollback is the second write and restores what the key held before. With the later binding installed before the rollback's fresh read, the ownership check refuses: no rollback write happens at all, the later entry survives unchanged, and the claim this view minted is reported as dropped. The own-token case stays separate. - One more row pins the read itself: a member of the stored map that is not an object is gone before the write is composed, because the read is production's. What each answer proves, and what the run would have failed on beforehand: - generation predicate without its equality: kills 3 rows here, 0 at this head - the double supplied its own predicate, so the answered tests could not see the change. - rollback ownership check removed: kills 1 row here, 0 at this head. - read without its entry-shape filter: kills 1 row here, 0 at this head. - prunePersistedViewStates without its updatedAt ordering: killed by neither, because every fixture here holds one or two entries against a cap of 50. Named, not papered over. Run from the src package on a real pnpm install --frozen-lockfile: 362 passed (362) (361 before), src tsc --noEmit clean, prettier 3.9.10 check clean, eslint with the shared prune switch left off clean, and src/eslint-suppressions.json byte identical to its head blob ae3d504. No changeset, no changelog.
1 parent 70cd843 commit ffa500a

1 file changed

Lines changed: 71 additions & 26 deletions

File tree

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8536,11 +8536,15 @@ describe("view state claims driven at the method boundary (PR 2414 asks)", () =>
85368536
storage[key] = value
85378537
},
85388538
},
8539-
getPersistedViewStates: () =>
8540-
Object.assign(Object.create(null) as Record<string, unknown>, storage["viewStates"] ?? {}),
8541-
prunePersistedViewStates: (states: Record<string, unknown>) => ({ ...states }),
8539+
// The read, the pruning and the generation predicate are the real prototype helpers driven
8540+
// over the double, exactly as the post and enqueue members below are: a hand-written copy
8541+
// of production logic here cannot fail when production changes.
8542+
getPersistedViewStates: (options?: { fresh?: boolean }) =>
8543+
proto["getPersistedViewStates"].call(host, options),
8544+
prunePersistedViewStates: (states: Record<string, unknown>) =>
8545+
proto["prunePersistedViewStates"].call(host, states),
85428546
isViewStateGenerationLive: (generation: number) =>
8543-
host["_disposed"] !== true && host["viewStateGeneration"] === generation,
8547+
proto["isViewStateGenerationLive"].call(host, generation),
85448548
postViewStateClaimToWebview: (viewStateId: string, claimToken: string) =>
85458549
proto["postViewStateClaimToWebview"].call(host, viewStateId, claimToken),
85468550
enqueueViewStateWrite: (run: (provider: ClineProvider) => Promise<void>) =>
@@ -8616,45 +8620,84 @@ describe("view state claims driven at the method boundary (PR 2414 asks)", () =>
86168620
expect(persisted()["sidebar-host-0"]?.claimToken).not.toBe("chosen-by-the-webview")
86178621
})
86188622

8619-
it.each([
8620-
{ label: "still carries its own binding", later: false },
8621-
{ label: "was re-bound by a later write", later: true },
8622-
])("rolls back a persisted claim whose view died while $label", async ({ later }) => {
8623+
it("drops an entry that is not an object when the real read rebuilds the map", async () => {
8624+
const { provider, storage, persisted } = makeViewHost()
8625+
storage["viewStates"] = {
8626+
"sidebar-host-0": { mode: "code", claimToken: "token-a", updatedAt: 1 },
8627+
toString: "a hand-edited record that is not an entry",
8628+
}
8629+
8630+
await drive<{ persisted: boolean; claim: string }>("savePersistedViewState", provider, { mode: "code" })
8631+
8632+
// The read is production's, so the corrupt member is gone before the write is composed:
8633+
// a double that copied the read here would carry it through to storage unchanged.
8634+
expect(Object.keys(persisted()).sort()).toEqual(["sidebar-host-0"])
8635+
expect(persisted()["sidebar-host-0"]?.claimToken).not.toBe("token-a")
8636+
})
8637+
it("rolls back a persisted claim whose view died while it still carries its own binding", async () => {
86238638
const prior = { mode: "architect", claimToken: "prior-token", updatedAt: 1 }
8639+
const writes: { key: string; value: unknown }[] = []
86248640
let minted: string | undefined
86258641
const { provider, storage, persisted } = makeViewHost(
86268642
{ viewStateGeneration: 5 },
8627-
(storage2, calls, _key, value) => {
8628-
const written = value as Record<string, { claimToken?: string }>
8643+
(storage2, calls, key, value) => {
8644+
writes.push({ key, value })
8645+
const written = value as Record<string, { claimToken?: string } | undefined>
86298646
if (calls.n === 1) {
86308647
minted = written["sidebar-host-0"]?.claimToken
8631-
storage2["viewStates"] = value
8648+
storage2[key] = value
86328649
provider["viewStateGeneration"] = 6
86338650
return
86348651
}
8635-
if (later) {
8636-
storage2["viewStates"] = {
8637-
"sidebar-host-0": { mode: "code", claimToken: "later-token", updatedAt: 2 },
8638-
}
8652+
storage2[key] = value
8653+
},
8654+
)
8655+
storage["viewStates"] = { "sidebar-host-0": { ...prior } }
8656+
8657+
const result = await drive<{ persisted: boolean; claim: string }>("savePersistedViewState", provider, {
8658+
mode: "code",
8659+
})
8660+
expect(minted).toBeDefined()
8661+
expect(result).toEqual({ persisted: false, claim: "none" })
8662+
// The durable entry still carried the token this write minted, so undoing it is the second
8663+
// write, and what it restores is exactly what the key held before.
8664+
expect(writes.map((write: { key: string; value: unknown }) => write.key)).toEqual(["viewStates", "viewStates"])
8665+
expect(persisted()["sidebar-host-0"]).toEqual(prior)
8666+
})
8667+
8668+
it("leaves a later re-binding alone and writes nothing when the dead claim was already replaced", async () => {
8669+
const prior = { mode: "architect", claimToken: "prior-token", updatedAt: 1 }
8670+
const laterEntry = { mode: "code", claimToken: "later-token", updatedAt: 2 }
8671+
const writes: { key: string; value: unknown }[] = []
8672+
let minted: string | undefined
8673+
const { provider, storage, persisted } = makeViewHost(
8674+
{ viewStateGeneration: 5 },
8675+
(storage2, calls, key, value) => {
8676+
writes.push({ key, value })
8677+
const written = value as Record<string, { claimToken?: string } | undefined>
8678+
if (calls.n === 1) {
8679+
minted = written["sidebar-host-0"]?.claimToken
8680+
// A different provider re-bound this key before the rollback read below could see it,
8681+
// so the durable entry no longer carries the token this write minted.
8682+
storage2[key] = { ...written, "sidebar-host-0": { ...laterEntry } }
8683+
provider["viewStateGeneration"] = 6
86398684
return
86408685
}
8641-
storage2["viewStates"] = value
8686+
storage2[key] = value
86428687
},
86438688
)
8644-
void storage
86458689
storage["viewStates"] = { "sidebar-host-0": { ...prior } }
86468690

86478691
const result = await drive<{ persisted: boolean; claim: string }>("savePersistedViewState", provider, {
86488692
mode: "code",
86498693
})
86508694
expect(minted).toBeDefined()
8651-
expect(result).toEqual({ persisted: false, claim: "none" })
8652-
if (later) {
8653-
expect(persisted()["sidebar-host-0"]?.claimToken).toBe("later-token")
8654-
expect(persisted()["sidebar-host-0"]?.mode).toBe("code")
8655-
} else {
8656-
expect(persisted()["sidebar-host-0"]).toEqual(prior)
8657-
}
8695+
// The ownership check refused to undo someone else's binding, so there is no rollback
8696+
// write at all: the later entry survives unchanged and the claim this view minted is
8697+
// reported as dropped rather than delivered or undone.
8698+
expect(writes).toHaveLength(1)
8699+
expect(persisted()["sidebar-host-0"]).toEqual(laterEntry)
8700+
expect(result).toEqual({ persisted: true, claim: "dropped" })
86588701
})
86598702

86608703
it("flushes a queued claim on the next launch and re-queues it when that post fails", async () => {
@@ -8728,12 +8771,14 @@ describe("view state claims driven at the method boundary (PR 2414 asks)", () =>
87288771
it("leaves the entry under the stable id with authorship set when the restore write fails", async () => {
87298772
const { provider, storage, persisted } = makeViewHost(
87308773
{ wroteUnderTemporaryViewStateId: true },
8731-
(storage2, calls) => {
8774+
(storage2, calls, key, value) => {
87328775
if (calls.n === 2) {
87338776
provider["viewStateGeneration"] = 1
87348777
throw new Error("restore write failed")
87358778
}
8736-
storage2["viewStates"] = { "stable-view": { mode: "code", claimToken: "token-a", updatedAt: 1 } }
8779+
// What production actually wrote, rather than a hand-written copy of the re-keyed
8780+
// record: a wrong re-key payload has to fail the assertions below.
8781+
storage2[key] = value
87378782
provider["viewStateGeneration"] = 1
87388783
},
87398784
)

0 commit comments

Comments
 (0)