Skip to content

Commit 174c38e

Browse files
wesbillmanCarl
andauthored
fix(desktop): recover full local storage on startup (#3182)
## Summary - recover already-full Buzz installs before desktop initialization without deleting healthy caches - enforce a global 2 MiB UTF-16 byte budget across disposable message, channel, timeline-skeleton, and sidebar-skeleton caches, regardless of relay count - route all disposable cache writes through quota recovery and reserve roughly 3 MiB of WebKit's observed ~5 MiB quota for durable state - preserve communities, identities, preferences, drafts, and read state; match only delimiter-qualified disposable namespaces ## Context WebKit enforces an approximately 5 MiB per-origin localStorage quota and Tauri does not expose an app-level knob to raise it to 50 MiB. Buzz 0.4.26 shipped reactive recovery for selected durable writes, but disposable writers swallowed quota failures and their existing limits were count-based per relay rather than byte-based per origin. This PR handles both halves: upgrade recovery for already-wedged origins and proactive global headroom so disposable snapshots cannot drive the origin back to the cliff. ## Safety - startup first probes a one-byte marker; healthy installs retain their caches - only if the marker write fails are the four relay-rehydratable cache namespaces removed - namespace matching requires the `v1:` delimiter, preventing future `v10` or similarly named durable keys from matching - oversized individual snapshots are rejected; crossing the global budget evicts disposable snapshots only - failed recovery leaves the marker absent, so the next launch retries ## Verification - `pnpm test` — 3,670 passed - `pnpm check` - `pnpm typecheck` - push hooks: branch-skew, desktop-check, desktop-test passed - byte-budget tests cover UTF-16 accounting, multiple relays, oversized writes, durable-state preservation, healthy startup, full startup, marker retry, and namespace near misses --------- Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent 01c2381 commit 174c38e

8 files changed

Lines changed: 299 additions & 14 deletions

File tree

‎desktop/src/features/channels/channelSnapshot.test.mjs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ test("remove clears the snapshot for that relay", () => {
8787
assert.equal(readChannelSnapshot(RELAY), null);
8888
});
8989

90+
test("cache write evicts disposable entries and retries at quota", () => {
91+
const original = window.localStorage;
92+
const storage = new Map([
93+
["buzz-channel-messages.v1:relay:old", "big"],
94+
["buzz-timeline-skeleton-shape.v1:old", "small"],
95+
]);
96+
window.localStorage = {
97+
get length() {
98+
return storage.size;
99+
},
100+
key: (index) => [...storage.keys()][index] ?? null,
101+
getItem: (key) => storage.get(key) ?? null,
102+
setItem(key, value) {
103+
if (!storage.has(key) && storage.size >= 2) {
104+
throw new Error("quota exceeded");
105+
}
106+
storage.set(key, value);
107+
},
108+
removeItem: (key) => storage.delete(key),
109+
};
110+
try {
111+
writeChannelSnapshot(RELAY, [makeChannel()]);
112+
assert.deepEqual(readChannelSnapshot(RELAY), [makeChannel()]);
113+
assert.equal(storage.has("buzz-channel-messages.v1:relay:old"), false);
114+
assert.equal(storage.has("buzz-timeline-skeleton-shape.v1:old"), false);
115+
} finally {
116+
window.localStorage = original;
117+
}
118+
});
119+
90120
test("write is tolerant of storage failures", () => {
91121
const original = window.localStorage.setItem;
92122
window.localStorage.setItem = () => {

‎desktop/src/features/channels/channelSnapshot.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import type { Channel } from "@/shared/api/types";
1515
import { normalizeRelayUrl } from "@/features/profile/lib/selfProfileStorage";
16+
import { setLocalStorageItemWithRecovery } from "@/shared/lib/localStorageQuota";
1617

1718
const STORAGE_KEY_PREFIX = "buzz-channels.v1";
1819

@@ -51,9 +52,22 @@ export function writeChannelSnapshot(
5152
): void {
5253
try {
5354
const key = channelSnapshotKey(relayUrl);
54-
const serialized = JSON.stringify({ version: 1, channels });
55-
if (window.localStorage.getItem(key) === serialized) return;
56-
window.localStorage.setItem(key, serialized);
55+
const previous = window.localStorage.getItem(key);
56+
if (previous) {
57+
try {
58+
const parsed = parseChannelSnapshot(JSON.parse(previous));
59+
if (parsed && JSON.stringify(parsed) === JSON.stringify(channels))
60+
return;
61+
} catch {
62+
// Malformed snapshots are replaced below.
63+
}
64+
}
65+
const serialized = JSON.stringify({
66+
version: 1,
67+
updatedAt: Date.now(),
68+
channels,
69+
});
70+
setLocalStorageItemWithRecovery(key, serialized);
5771
} catch {
5872
// Storage access failures are non-fatal.
5973
}

‎desktop/src/features/messages/lib/messageSnapshot.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { mergeTimelineHistoryMessages } from "@/features/messages/lib/messageQueryKeys";
1818
import { normalizeRelayUrl } from "@/features/profile/lib/selfProfileStorage";
1919
import type { RelayEvent } from "@/shared/api/types";
20+
import { setLocalStorageItemWithRecovery } from "@/shared/lib/localStorageQuota";
2021

2122
const STORAGE_KEY_PREFIX = "buzz-channel-messages.v1";
2223

@@ -147,7 +148,7 @@ export function writeMessageSnapshot(
147148
}
148149

149150
evictOldestSnapshots(relayPrefix(relayUrl), key);
150-
window.localStorage.setItem(
151+
setLocalStorageItemWithRecovery(
151152
key,
152153
JSON.stringify({
153154
version: 1,

‎desktop/src/features/messages/ui/TimelineSkeleton.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from "react";
22

33
import type { TimelineMessage } from "@/features/messages/types";
44
import { cn } from "@/shared/lib/cn";
5+
import { setLocalStorageItemWithRecovery } from "@/shared/lib/localStorageQuota";
56
import { Skeleton } from "@/shared/ui/skeleton";
67

78
const TIMELINE_SKELETON_CACHE_PREFIX = "buzz-timeline-skeleton-shape.v1";
@@ -35,6 +36,7 @@ export type TimelineSkeletonRowShape = {
3536

3637
type TimelineSkeletonCachePayload = {
3738
rows: TimelineSkeletonRowShape[];
39+
updatedAt: number;
3840
version: 1;
3941
};
4042

@@ -133,11 +135,12 @@ function writeTimelineSkeletonRows(
133135

134136
const payload: TimelineSkeletonCachePayload = {
135137
rows: rows.slice(0, 4),
138+
updatedAt: Date.now(),
136139
version: 1,
137140
};
138141

139142
try {
140-
window.localStorage.setItem(cacheKey, JSON.stringify(payload));
143+
setLocalStorageItemWithRecovery(cacheKey, JSON.stringify(payload));
141144
} catch {
142145
// localStorage can be unavailable or full in embedded webviews.
143146
}

‎desktop/src/features/sidebar/ui/sidebarLoadingSkeleton.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from "react";
22

33
import type { Channel } from "@/shared/api/types";
44
import { cn } from "@/shared/lib/cn";
5+
import { setLocalStorageItemWithRecovery } from "@/shared/lib/localStorageQuota";
56
import {
67
SidebarGroup,
78
SidebarGroupContent,
@@ -36,6 +37,7 @@ type SidebarLoadingShape = {
3637
};
3738

3839
type SidebarLoadingCachePayload = SidebarLoadingShape & {
40+
updatedAt: number;
3941
version: 1;
4042
};
4143

@@ -130,11 +132,12 @@ function writeSidebarLoadingShape(
130132
const payload: SidebarLoadingCachePayload = {
131133
channels: shape.channels.slice(0, 3),
132134
directMessages: shape.directMessages.slice(0, 2),
135+
updatedAt: Date.now(),
133136
version: 1,
134137
};
135138

136139
try {
137-
window.localStorage.setItem(cacheKey, JSON.stringify(payload));
140+
setLocalStorageItemWithRecovery(cacheKey, JSON.stringify(payload));
138141
} catch {
139142
// localStorage can be unavailable or full in embedded webviews.
140143
}

‎desktop/src/main.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { EmojiBurstProvider } from "@/shared/ui/EmojiBurstProvider";
1313
import { PoofBurstProvider } from "@/shared/ui/PoofBurstProvider";
1414
import { Toaster } from "@/shared/ui/sonner";
1515
import { TooltipProvider } from "@/shared/ui/tooltip";
16+
import { recoverLocalStorageQuotaOnStartup } from "@/shared/lib/localStorageQuota";
1617

1718
type E2eWindow = Window & {
1819
__BUZZ_E2E__?: unknown;
@@ -110,6 +111,7 @@ async function installE2eBridgeIfConfigured() {
110111
async function bootstrap() {
111112
resetDevWebviewStateFromUrl();
112113
configureDevE2eBridgeFromUrl();
114+
recoverLocalStorageQuotaOnStartup();
113115
await installE2eBridgeIfConfigured();
114116
await migrateLegacyCommunityStorageBeforeRender();
115117
renderApp();

‎desktop/src/shared/lib/localStorageQuota.test.mjs‎

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { setLocalStorageItemWithRecovery } from "./localStorageQuota.ts";
4+
import {
5+
recoverLocalStorageQuotaOnStartup,
6+
setLocalStorageItemWithRecovery,
7+
} from "./localStorageQuota.ts";
58

69
function makeQuotaLocalStorage({ maxEntries }) {
710
const store = new Map();
@@ -30,6 +33,144 @@ function install(ls) {
3033
globalThis.localStorage = ls;
3134
}
3235

36+
test("startup recovery removes disposable caches but preserves user state", () => {
37+
const ls = makeQuotaLocalStorage({ maxEntries: 5 });
38+
install(ls);
39+
ls.store.set("buzz-channel-messages.v1:relay:chan", "big");
40+
ls.store.set("buzz-channels.v1:relay", "big");
41+
ls.store.set("buzz-timeline-skeleton-shape.v1:chan", "small");
42+
ls.store.set("buzz-sidebar-skeleton-shape.v1:community:user", "small");
43+
ls.store.set("buzz-communities", "keep");
44+
45+
recoverLocalStorageQuotaOnStartup();
46+
47+
assert.equal(ls.getItem("buzz-channel-messages.v1:relay:chan"), null);
48+
assert.equal(ls.getItem("buzz-channels.v1:relay"), null);
49+
assert.equal(ls.getItem("buzz-timeline-skeleton-shape.v1:chan"), null);
50+
assert.equal(
51+
ls.getItem("buzz-sidebar-skeleton-shape.v1:community:user"),
52+
null,
53+
);
54+
assert.equal(ls.getItem("buzz-communities"), "keep");
55+
assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), "1");
56+
});
57+
58+
test("healthy startup preserves disposable caches", () => {
59+
const ls = makeQuotaLocalStorage({ maxEntries: 10 });
60+
install(ls);
61+
ls.store.set("buzz-channel-messages.v1:relay:new", "snapshot");
62+
63+
recoverLocalStorageQuotaOnStartup();
64+
65+
assert.equal(ls.getItem("buzz-channel-messages.v1:relay:new"), "snapshot");
66+
assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), "1");
67+
});
68+
69+
test("startup recovery does not remove namespace near misses", () => {
70+
const ls = makeQuotaLocalStorage({ maxEntries: 2 });
71+
install(ls);
72+
ls.store.set("buzz-channels.v10:durable", "keep");
73+
ls.store.set("buzz-channel-messages.v1-durable", "keep");
74+
75+
recoverLocalStorageQuotaOnStartup();
76+
77+
assert.equal(ls.getItem("buzz-channels.v10:durable"), "keep");
78+
assert.equal(ls.getItem("buzz-channel-messages.v1-durable"), "keep");
79+
assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), null);
80+
});
81+
82+
test("startup recovery runs only once", () => {
83+
const ls = makeQuotaLocalStorage({ maxEntries: 10 });
84+
install(ls);
85+
86+
recoverLocalStorageQuotaOnStartup();
87+
ls.store.set("buzz-channel-messages.v1:relay:new", "new snapshot");
88+
recoverLocalStorageQuotaOnStartup();
89+
90+
assert.equal(
91+
ls.getItem("buzz-channel-messages.v1:relay:new"),
92+
"new snapshot",
93+
);
94+
});
95+
96+
test("startup recovery retries after marker write fails", () => {
97+
const ls = makeQuotaLocalStorage({ maxEntries: 1 });
98+
install(ls);
99+
ls.store.set("buzz-communities", "keep");
100+
101+
recoverLocalStorageQuotaOnStartup();
102+
assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), null);
103+
104+
ls.store.delete("buzz-communities");
105+
ls.store.set("buzz-channel-messages.v1:relay:chan", "big");
106+
recoverLocalStorageQuotaOnStartup();
107+
108+
assert.equal(ls.getItem("buzz-channel-messages.v1:relay:chan"), null);
109+
assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), "1");
110+
});
111+
112+
test("global cache byte budget evicts only oldest entries needed", () => {
113+
const ls = makeQuotaLocalStorage({ maxEntries: 20 });
114+
install(ls);
115+
ls.store.set("buzz-communities", "keep");
116+
const snapshot = (updatedAt) =>
117+
JSON.stringify({ updatedAt, payload: "x".repeat(400_000) });
118+
const oldestKey = "buzz-channel-messages.v1:relay:oldest";
119+
const newerKey = "buzz-channels.v1:relay-newer";
120+
const newestKey = "buzz-channel-messages.v1:relay:newest";
121+
122+
assert.equal(setLocalStorageItemWithRecovery(oldestKey, snapshot(1)), true);
123+
assert.equal(setLocalStorageItemWithRecovery(newerKey, snapshot(2)), true);
124+
assert.equal(setLocalStorageItemWithRecovery(newestKey, snapshot(3)), true);
125+
126+
assert.equal(ls.getItem(oldestKey), null);
127+
assert.notEqual(ls.getItem(newerKey), null);
128+
assert.notEqual(ls.getItem(newestKey), null);
129+
assert.equal(ls.getItem("buzz-communities"), "keep");
130+
});
131+
132+
test("global cache byte budget spans relays and preserves durable state", () => {
133+
const ls = makeQuotaLocalStorage({ maxEntries: 20 });
134+
install(ls);
135+
ls.store.set("buzz-communities", "keep");
136+
const largeSnapshot = "x".repeat(600_000);
137+
138+
assert.equal(
139+
setLocalStorageItemWithRecovery(
140+
"buzz-channel-messages.v1:relay-one:chan",
141+
largeSnapshot,
142+
),
143+
true,
144+
);
145+
assert.equal(
146+
setLocalStorageItemWithRecovery(
147+
"buzz-channel-messages.v1:relay-two:chan",
148+
largeSnapshot,
149+
),
150+
true,
151+
);
152+
153+
assert.equal(ls.getItem("buzz-channel-messages.v1:relay-one:chan"), null);
154+
assert.equal(
155+
ls.getItem("buzz-channel-messages.v1:relay-two:chan"),
156+
largeSnapshot,
157+
);
158+
assert.equal(ls.getItem("buzz-communities"), "keep");
159+
});
160+
161+
test("rejects a single cache entry larger than the global byte budget", () => {
162+
const ls = makeQuotaLocalStorage({ maxEntries: 10 });
163+
install(ls);
164+
const key = "buzz-channel-messages.v1:relay:oversized";
165+
ls.store.set(key, "previous snapshot");
166+
167+
assert.equal(
168+
setLocalStorageItemWithRecovery(key, "x".repeat(1_100_000)),
169+
false,
170+
);
171+
assert.equal(ls.getItem(key), null);
172+
});
173+
33174
test("writes normally when under quota", () => {
34175
const ls = makeQuotaLocalStorage({ maxEntries: 10 });
35176
install(ls);

0 commit comments

Comments
 (0)