fix: prevent session restore snapshot loss

This commit is contained in:
Zheng Liu committed 2026-06-17 23:41:29 +08:00
1 parent 04906a80ff
commit 93079bb576
11 files changed
+120 -18

No files matched your search

@@ -65,7 +65,7 @@ test("session restore persistence re-arms when restore previous session is enabl
const revisionStateIndex = source.indexOf("restorePreviousSessionRevision");
const keyGuardIndex = source.indexOf("detail?.key !== STORAGE_KEY_RESTORE_PREVIOUS_SESSION");
const listenerIndex = source.indexOf("addEventListener(LOCAL_STORAGE_ADAPTER_CHANGED_EVENT");
const effectDependencyIndex = source.indexOf("restorePreviousSessionRevision]", source.indexOf("beforeunload"));
const effectDependencyIndex = source.indexOf("restorePreviousSessionRevision, persistSessionRestore]", source.indexOf("beforeunload"));
assert.notEqual(adapterEventImportIndex, -1);
assert.notEqual(revisionStateIndex, -1);
@@ -74,6 +74,27 @@ test("session restore persistence re-arms when restore previous session is enabl
assert.notEqual(effectDependencyIndex, -1);
});
test("session restore persistence can be disabled for non-main windows", () => {
const hookSource = readFileSync(new URL("./useSessionState.ts", import.meta.url), "utf8");
const traySource = readFileSync(new URL("../../components/TrayPanel.tsx", import.meta.url), "utf8");
const appSource = readFileSync(new URL("../../App.tsx", import.meta.url), "utf8");
const indexSource = readFileSync(new URL("../../index.tsx", import.meta.url), "utf8");
const registerBridgesSource = readFileSync(new URL("../../electron/main/registerBridges.cjs", import.meta.url), "utf8");
const mainWindowSource = readFileSync(new URL("../../electron/bridges/windowManager/mainWindow.cjs", import.meta.url), "utf8");
assert.match(hookSource, /persistSessionRestore\?: boolean/);
assert.match(hookSource, /restoreEnabled: persistSessionRestore && resolveRestorePreviousSessionSetting/);
assert.match(hookSource, /if \(!persistSessionRestore\) return;/);
assert.match(traySource, /useSessionState\(\{ persistSessionRestore: false \}\)/);
assert.match(appSource, /window\.location\.hash\.startsWith\('#\/session-window'\)/);
assert.match(appSource, /persistSessionRestore: !isPeerSessionWindow/);
assert.match(indexSource, /hash === '#\/session-window'/);
assert.match(registerBridgesSource, /route: "session-window"/);
assert.match(mainWindowSource, /const rendererHash = typeof route === "string"/);
assert.match(mainWindowSource, /loadURL\(`\$\{getDevRendererBaseUrl\(devServerUrl\)\}\$\{rendererHash\}`\)/);
assert.match(mainWindowSource, /loadURL\(`app:\/\/netcatty\/index\.html\$\{rendererHash\}`\)/);
});
test("restore terminal cwd setting participates in cross-window settings sync", () => {
const storageSyncSource = readFileSync(new URL("./settingsStorageSync.ts", import.meta.url), "utf8");
const ipcSyncSource = readFileSync(new URL("./settingsIpcSync.ts", import.meta.url), "utf8");
@@ -189,3 +189,29 @@ test("session restore flush clears storage instead of writing when restore is di
assert.equal(clearCount, 1);
assert.equal(writes.length, 0);
});
test("session restore flush skips empty transient windows without clearing existing snapshots", () => {
const writes: SessionRestorePayload[] = [];
let clearCount = 0;
const storage = {
write: (next: SessionRestorePayload) => {
writes.push(next);
return true;
},
clear: () => {
clearCount += 1;
},
};
const wrote = buildAndWriteSessionRestorePayload({
sessions: [],
workspaces: [],
tabOrder: [],
activeTabId: "vault",
storage,
});
assert.equal(wrote, false);
assert.equal(clearCount, 0);
assert.equal(writes.length, 0);
});
-1
View File
@@ -100,7 +100,6 @@ export function buildAndWriteSessionRestorePayload({
now,
});
if (!payload) {
storage.clear();
return false;
}
return storage.write(payload);
+10 -4
View File
@@ -43,13 +43,17 @@ import {
import { resolveRestorePreviousSessionSetting } from './sessionRestoreSettings';
export const useSessionState = () => {
export const useSessionState = ({
persistSessionRestore = true,
}: {
persistSessionRestore?: boolean;
} = {}) => {
const initialRestoreState = useMemo(() => createInitialRestoredSessionState({
restoreEnabled: resolveRestorePreviousSessionSetting(
restoreEnabled: persistSessionRestore && resolveRestorePreviousSessionSetting(
localStorageAdapter.readBoolean(STORAGE_KEY_RESTORE_PREVIOUS_SESSION),
),
payload: sessionRestoreStorage.read(),
}), []);
}), [persistSessionRestore]);
const [sessions, setSessions] = useState<TerminalSession[]>(initialRestoreState.sessions);
const [workspaces, setWorkspaces] = useState<Workspace[]>(initialRestoreState.workspaces);
// Latest workspaces snapshot for synchronous existence checks outside
@@ -102,6 +106,8 @@ export const useSessionState = () => {
}, []);
useEffect(() => {
if (!persistSessionRestore) return;
const restoreEnabled = resolveRestorePreviousSessionSetting(
localStorageAdapter.readBoolean(STORAGE_KEY_RESTORE_PREVIOUS_SESSION),
);
@@ -140,7 +146,7 @@ export const useSessionState = () => {
window.removeEventListener("pagehide", handlePageHide);
window.removeEventListener("beforeunload", handlePageHide);
};
}, [sessions, workspaces, tabOrder, activeTabRevision, restorePreviousSessionRevision]);
}, [sessions, workspaces, tabOrder, activeTabRevision, restorePreviousSessionRevision, persistSessionRestore]);
const updateSessionRestoreCwd = useCallback((sessionId: string, cwd: string | null) => {
setSessions((prev) => updateSessionRestoreCwdState(prev, sessionId, cwd));