Skip to content

Commit 4534890

Browse files
fix(desktop): harden the V1 Local Storage import from review
- Pick the V1 profile by its newest LevelDB file, not the directory mtime. - Skip a corrupt log block and keep reading, as LevelDB does, instead of dropping everything after it. - Fail the read (and retry next launch) if the database changed mid-read. - Only mark the import done after every write succeeded and storage was flushed; taking the items clears them so a reload cannot merge twice. - Cap the merged stash at 20 entries; skip the snap-shot setup resume key. - Keep a leading U+FEFF in UTF-16 values. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent bfcd0f4 commit 4534890

6 files changed

Lines changed: 102 additions & 35 deletions

File tree

‎apps/desktop/src/app/DesktopLegacyLocalStorage.ts‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import * as DesktopEnvironment from "./DesktopEnvironment.ts";
1616
* `t3code-v2` profile. The V1 profile is only read, never opened by Chromium,
1717
* so this works while V1 is still running.
1818
*
19-
* `load` runs before the window opens; the preload takes the items, merges
20-
* them into the new profile, and calls `complete`, which writes a marker so
21-
* later launches skip the read. A failed read, or a window that never
22-
* completes, leaves no marker and retries next launch.
19+
* `load` runs before the window opens; the preload takes the items once,
20+
* merges them into the new profile, and calls `complete`, which writes a
21+
* marker so later launches skip the read. A failed read or merge, or a window
22+
* that never completes, leaves no marker and retries next launch.
2323
*/
2424
export class DesktopLegacyLocalStorage extends Context.Service<
2525
DesktopLegacyLocalStorage,
2626
{
2727
readonly load: (userDataPath: string) => Effect.Effect<void>;
28-
readonly pending: Effect.Effect<Option.Option<Readonly<Record<string, string>>>>;
28+
readonly take: Effect.Effect<Option.Option<Readonly<Record<string, string>>>>;
2929
readonly complete: Effect.Effect<void>;
3030
}
3131
>()("@t3tools/desktop/app/DesktopLegacyLocalStorage") {}
@@ -41,18 +41,30 @@ const make = Effect.gen(function* () {
4141
const items = yield* Ref.make(Option.none<Readonly<Record<string, string>>>());
4242
const markerPath = yield* Ref.make(Option.none<string>());
4343

44+
/** Newest file mtime in a directory; appending to a log leaves the directory's own mtime alone. */
45+
const newestFileMtime = (directory: string) =>
46+
Effect.gen(function* () {
47+
let newest = 0;
48+
for (const name of yield* fs.readDirectory(directory)) {
49+
const info = yield* fs.stat(path.join(directory, name)).pipe(Effect.option);
50+
if (Option.isNone(info)) continue;
51+
const mtime = Option.match(info.value.mtime, {
52+
onNone: () => 0,
53+
onSome: (date) => date.getTime(),
54+
});
55+
newest = Math.max(newest, mtime);
56+
}
57+
return newest;
58+
}).pipe(Effect.option);
59+
4460
/** The V1 profile whose Local Storage was written most recently, if any. */
4561
const findV1LocalStorage = Effect.gen(function* () {
4662
let newest: { readonly directory: string; readonly mtime: number } | null = null;
4763
for (const name of V1_PROFILE_NAMES) {
4864
const directory = path.join(environment.appDataDirectory, name, "Local Storage", "leveldb");
49-
const info = yield* fs.stat(directory).pipe(Effect.option);
50-
if (Option.isNone(info) || info.value.type !== "Directory") continue;
51-
const mtime = Option.match(info.value.mtime, {
52-
onNone: () => 0,
53-
onSome: (date) => date.getTime(),
54-
});
55-
if (newest === null || mtime > newest.mtime) newest = { directory, mtime };
65+
const mtime = yield* newestFileMtime(directory);
66+
if (Option.isNone(mtime)) continue;
67+
if (newest === null || mtime.value > newest.mtime) newest = { directory, mtime: mtime.value };
5668
}
5769
return newest?.directory ?? null;
5870
});
@@ -96,8 +108,9 @@ const make = Effect.gen(function* () {
96108

97109
return DesktopLegacyLocalStorage.of({
98110
load,
99-
pending: Ref.get(items),
100-
complete: Ref.set(items, Option.none()).pipe(Effect.andThen(writeMarker)),
111+
// Taking clears the items, so a reload before `complete` cannot merge twice.
112+
take: Ref.getAndSet(items, Option.none()),
113+
complete: writeMarker,
101114
});
102115
});
103116

‎apps/desktop/src/app/chromiumLocalStorage.ts‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const fail = (detail: string): never => {
2828
throw new LevelDbFormatError({ detail });
2929
};
3030

31-
const textDecoderUtf16 = new TextDecoder("utf-16le");
31+
// ignoreBOM keeps a leading U+FEFF that is part of the stored string.
32+
const textDecoderUtf16 = new TextDecoder("utf-16le", { ignoreBOM: true });
3233
const textDecoderUtf8 = new TextDecoder();
3334

3435
/** True ISO-8859-1. WHATWG's "latin1" label is windows-1252, which remaps 0x80-0x9F. */
@@ -148,8 +149,10 @@ function decompressSnappy(input: Uint8Array): Uint8Array {
148149
}
149150

150151
/**
151-
* Splits a LevelDB log (write-ahead log or MANIFEST) into its records. A torn
152-
* or corrupt record ends the log, the same point LevelDB's own recovery stops.
152+
* Splits a LevelDB log (write-ahead log or MANIFEST) into its records. Like
153+
* LevelDB's own reader, a corrupt record or zero padding skips the rest of its
154+
* block, since a reused log keeps appending after a torn write, and a record
155+
* cut off by the end of the file ends the log.
153156
*/
154157
function readLogRecords(bytes: Uint8Array): Uint8Array[] {
155158
const records: Uint8Array[] = [];
@@ -164,10 +167,18 @@ function readLogRecords(bytes: Uint8Array): Uint8Array[] {
164167
const length = bytes[offset + 4]! | (bytes[offset + 5]! << 8);
165168
const type = bytes[offset + 6]!;
166169
const end = offset + LOG_HEADER_SIZE + length;
167-
// Type 0 is preallocated zero padding: nothing was written past here.
168-
if (type === 0 || end > bytes.length || LOG_HEADER_SIZE + length > blockRemaining) break;
170+
const fitsBlock = LOG_HEADER_SIZE + length <= blockRemaining;
171+
if (fitsBlock && end > bytes.length) break;
169172
const typeAndPayload = bytes.subarray(offset + 6, end);
170-
if (crc32c(typeAndPayload) !== unmaskCrc(readUint32(bytes, offset))) break;
173+
if (
174+
type === 0 ||
175+
!fitsBlock ||
176+
crc32c(typeAndPayload) !== unmaskCrc(readUint32(bytes, offset))
177+
) {
178+
offset += blockRemaining;
179+
pending = null;
180+
continue;
181+
}
171182
const payload = typeAndPayload.subarray(1);
172183
offset = end;
173184
if (type === 1) {
@@ -391,10 +402,8 @@ export const readChromiumLocalStorage = Effect.fn("desktop.chromiumLocalStorage.
391402
cause: new LevelDbFormatError({ detail: "bad CURRENT" }),
392403
});
393404
}
394-
const live = yield* readFile(current).pipe(
395-
Effect.flatMap((bytes) => decode(() => readManifest(bytes))),
396-
wrap,
397-
);
405+
const manifest = yield* readFile(current).pipe(wrap);
406+
const live = yield* decode(() => readManifest(manifest)).pipe(wrap);
398407
const logs = (yield* fs.readDirectory(directory).pipe(wrap))
399408
.map((name) => /^(\d+)\.log$/.exec(name))
400409
.filter((match) => match !== null && Number(match[1]) >= live.minimumLogNumber)
@@ -420,6 +429,18 @@ export const readChromiumLocalStorage = Effect.fn("desktop.chromiumLocalStorage.
420429
}).pipe(wrap);
421430
}
422431

432+
// A flush while another process has the database open can move entries
433+
// from a log we read into a table our MANIFEST copy predates. Fail so the
434+
// caller retries rather than import a snapshot missing those entries.
435+
const currentAfter = textDecoderUtf8.decode(yield* readFile("CURRENT").pipe(wrap)).trim();
436+
const manifestAfter = yield* fs.stat(path.join(directory, current)).pipe(wrap);
437+
if (currentAfter !== current || Number(manifestAfter.size) !== manifest.length) {
438+
return yield* new ChromiumLocalStorageReadError({
439+
directory,
440+
cause: new LevelDbFormatError({ detail: "database changed while reading" }),
441+
});
442+
}
443+
423444
const prefix = `_${origin}\u0000`;
424445
const items = new Map<string, string>();
425446
for (const [id, { key: rawKey, entry }] of entries) {

‎apps/desktop/src/ipc/methods/legacyLocalStorage.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Electron from "electron";
12
import * as Effect from "effect/Effect";
23
import * as Option from "effect/Option";
34
import * as Schema from "effect/Schema";
@@ -11,16 +12,19 @@ export const takeLegacyLocalStorage = makeSyncIpcMethod({
1112
result: Schema.NullOr(Schema.Record(Schema.String, Schema.String)),
1213
handler: Effect.fn("desktop.ipc.legacyLocalStorage.take")(function* () {
1314
const legacy = yield* DesktopLegacyLocalStorage.DesktopLegacyLocalStorage;
14-
return Option.getOrNull(yield* legacy.pending);
15+
return Option.getOrNull(yield* legacy.take);
1516
}),
1617
});
1718

1819
export const completeLegacyLocalStorage = makeIpcMethod({
1920
channel: IpcChannels.COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL,
2021
payload: Schema.Void,
2122
result: Schema.Void,
22-
handler: Effect.fn("desktop.ipc.legacyLocalStorage.complete")(function* () {
23+
handler: Effect.fn("desktop.ipc.legacyLocalStorage.complete")(function* (_, event) {
2324
const legacy = yield* DesktopLegacyLocalStorage.DesktopLegacyLocalStorage;
25+
// Chromium writes localStorage to disk lazily; persist the merge before
26+
// the marker stops later launches from retrying it.
27+
if (event) Electron.webContents.fromId(event.sender.id)?.session.flushStorageData();
2428
yield* legacy.complete;
2529
}),
2630
});

‎apps/desktop/src/legacyLocalStorageMerge.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,29 @@ describe("mergeLegacyLocalStorage", () => {
7474
assert.equal(storage.getItem("t3code:composer-drafts:v1"), current);
7575
});
7676

77-
it("keeps importing other keys after one write fails", () => {
77+
it("caps the merged stash at the store's entry limit, dropping the oldest V1 entries", () => {
78+
const v2Ids = Array.from({ length: 15 }, (_, index) => `v2-${index}`);
79+
const v1Ids = Array.from({ length: 10 }, (_, index) => `v1-${index}`);
80+
const storage = memoryStorage({ "t3code:prompt-stash:v2": stash(...v2Ids) });
81+
mergeLegacyLocalStorage(storage, { "t3code:prompt-stash:v2": stash(...v1Ids) });
82+
const ids = JSON.parse(storage.getItem("t3code:prompt-stash:v2")!).state.entries.map(
83+
(entry: { id: string }) => entry.id,
84+
);
85+
assert.deepStrictEqual(ids, [...v2Ids, ...v1Ids.slice(0, 5)]);
86+
});
87+
88+
it("keeps importing other keys after one write fails, and reports the failure", () => {
7889
const storage = memoryStorage();
7990
const setItem = storage.setItem;
8091
storage.setItem = (key, value) => {
8192
if (key === "t3code:theme") throw new DOMException("full", "QuotaExceededError");
8293
setItem(key, value);
8394
};
84-
mergeLegacyLocalStorage(storage, { "t3code:theme": "dark", "t3code:last-editor": "zed" });
95+
const complete = mergeLegacyLocalStorage(storage, {
96+
"t3code:theme": "dark",
97+
"t3code:last-editor": "zed",
98+
});
8599
assert.equal(storage.getItem("t3code:last-editor"), "zed");
100+
assert.isFalse(complete);
86101
});
87102
});

‎apps/desktop/src/legacyLocalStorageMerge.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ interface MergeStorage {
1212

1313
const PROMPT_STASH_KEY = "t3code:prompt-stash:v2";
1414
const COMPOSER_DRAFTS_KEY = "t3code:composer-drafts:v1";
15-
/** Identifies one running install to the server; two apps must not share it. */
16-
const NOT_IMPORTED_KEYS = new Set(["t3.backgroundActivity.clientId"]);
15+
const NOT_IMPORTED_KEYS = new Set([
16+
// Identifies one running install to the server; two apps must not share it.
17+
"t3.backgroundActivity.clientId",
18+
// Resumes a permission setup V1 left mid-flow; V2 would redirect to it on boot.
19+
"t3code:snap-shot-setup-resume:v1",
20+
]);
21+
/** Matches MAX_STASH_ENTRIES in apps/web/src/promptStashStore.ts. */
22+
const MAX_STASH_ENTRIES = 20;
1723
const DRAFT_RECORD_FIELDS = [
1824
"draftsByThreadKey",
1925
"draftThreadsByThreadKey",
@@ -34,7 +40,7 @@ function parseObject(raw: string): JsonObject | null {
3440
}
3541
}
3642

37-
/** V2 entries first, then V1 entries V2 does not already have. */
43+
/** V2 entries first, then V1 entries V2 does not already have, up to the stash cap. */
3844
function mergePromptStash(current: JsonObject, legacy: JsonObject): JsonObject | null {
3945
const currentEntries = isObject(current.state) ? current.state.entries : null;
4046
const legacyEntries = isObject(legacy.state) ? legacy.state.entries : null;
@@ -43,7 +49,10 @@ function mergePromptStash(current: JsonObject, legacy: JsonObject): JsonObject |
4349
const ids = new Set(currentEntries.map((entry) => (isObject(entry) ? entry.id : undefined)));
4450
const added = legacyEntries.filter((entry) => isObject(entry) && !ids.has(entry.id));
4551
const state = isObject(current.state) ? current.state : {};
46-
return { ...current, state: { ...state, entries: [...currentEntries, ...added] } };
52+
return {
53+
...current,
54+
state: { ...state, entries: [...currentEntries, ...added].slice(0, MAX_STASH_ENTRIES) },
55+
};
4756
}
4857

4958
/** Adds V1 drafts for threads V2 has no draft for. Shapes must share a version. */
@@ -77,10 +86,12 @@ function mergeValue(key: string, current: string, legacy: string): string | null
7786
return merged === null ? null : JSON.stringify(merged);
7887
}
7988

89+
/** Returns false when a write failed, so the caller can retry on a later launch. */
8090
export function mergeLegacyLocalStorage(
8191
storage: MergeStorage,
8292
legacyItems: Readonly<Record<string, string>>,
83-
): void {
93+
): boolean {
94+
let complete = true;
8495
// Stash and drafts first, so a full quota costs layout state rather than prompts.
8596
const keys = Object.keys(legacyItems).sort(
8697
(left, right) => Number(isMergedKey(right)) - Number(isMergedKey(left)),
@@ -95,6 +106,8 @@ export function mergeLegacyLocalStorage(
95106
storage.setItem(key, next);
96107
} catch {
97108
// Over quota: skip this key rather than abandon the rest.
109+
complete = false;
98110
}
99111
}
112+
return complete;
100113
}

‎apps/desktop/src/preload.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ exposeClerkBridge({ passkeys: true });
3535
try {
3636
const legacyItems: unknown = ipcRenderer.sendSync(IpcChannels.TAKE_LEGACY_LOCAL_STORAGE_CHANNEL);
3737
if (typeof legacyItems === "object" && legacyItems !== null) {
38-
mergeLegacyLocalStorage(window.localStorage, legacyItems as Record<string, string>);
39-
void ipcRenderer.invoke(IpcChannels.COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL);
38+
if (mergeLegacyLocalStorage(window.localStorage, legacyItems as Record<string, string>)) {
39+
void ipcRenderer.invoke(IpcChannels.COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL);
40+
}
4041
}
4142
} catch {
4243
// Best effort: the app still starts on the V2 profile's own storage.

0 commit comments

Comments
 (0)