diff options
| author | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-10-11 23:28:39 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-11 23:28:39 +0530 |
| commit | 7833e2ecd5dc5fd9d7e63322680711d341d885ee (patch) | |
| tree | c54f93f6311eab70516e41637988751f12c9a513 /src/index.ts | |
| parent | ff449d9f807bc2aa9b6d13aaa8602c37a6a72352 (diff) | |
fix: append browser state to the session only when it changes (#37)
persistState appended a browser-harness-state entry on every ownership callback, session_tree
and shutdown, so the append-only session file and Pi Web's "+N custom" chain grew without bound.
A snapshot is now written only when it differs from the last one persisted or restored.
Fixes #26
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/index.ts b/src/index.ts index 0133f65..d69940a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { getBrowserSystemPrompt } from "./prompt"; import { registerSetupCommand } from "./setup"; import { registerProfileCommand } from "./profile/command"; import { readPin } from "./profile/store"; -import { type BrowserState, defaultState, persistState, restoreState } from "./state"; +import { type BrowserState, createStatePersister, defaultState, restoreState } from "./state"; import { registerAllTools } from "./registry"; import { cleanupTempDirs } from "./util/truncate"; import { createDaemonTransport } from "./daemon/transport"; @@ -16,6 +16,7 @@ export default function browserHarnessExtension(pi: ExtensionAPI): void { const namespace = flagNs ?? `pi-${Math.random().toString(36).slice(2, 10)}`; let state: BrowserState = defaultState(namespace); + const persister = createStatePersister(pi); let client: BrowserClient | null = null; let toolsRegistered = false; @@ -83,6 +84,7 @@ export default function browserHarnessExtension(pi: ExtensionAPI): void { pi.on("session_start", async (_event, ctx) => { state = restoreState(ctx, state.namespace); + persister.markPersisted(state); setDebugClicks(asBoolean(pi.getFlag("browser-debug-clicks")) ?? false); // Re-read the on-disk profile pin every session start: another pi session may have changed it since this client was created. @@ -112,7 +114,7 @@ export default function browserHarnessExtension(pi: ExtensionAPI): void { ? { harnessWindowId: snap.harnessWindowId } : {}), }; - try { persistState(pi, state); } catch {} + try { persister.persist(state); } catch {} }, }); } else { @@ -137,13 +139,14 @@ export default function browserHarnessExtension(pi: ExtensionAPI): void { } // Do NOT stop the client or null it out: the surviving daemon connection is what eliminates the per-session "Allow Remote Debugging" prompt. } - persistState(pi, state); + persister.persist(state); await cleanupTempDirs(); }); + // The branch already holds the restored snapshot: re-appending it only grows the session file. pi.on("session_tree", async (_event, ctx) => { state = restoreState(ctx, client?.namespace); - persistState(pi, state); + persister.markPersisted(state); }); pi.on("before_agent_start", async (event) => { |
