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 | |
| 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')
| -rw-r--r-- | src/index.ts | 11 | ||||
| -rw-r--r-- | src/state.ts | 34 |
2 files changed, 39 insertions, 6 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) => { diff --git a/src/state.ts b/src/state.ts index 8f25dc4..0620e11 100644 --- a/src/state.ts +++ b/src/state.ts @@ -13,8 +13,38 @@ export const defaultState = (namespace = "default"): BrowserState => ({ ownedTargetIds: [], }); -export const persistState = (pi: ExtensionAPI, state: BrowserState): void => { - pi.appendEntry<BrowserState>("browser-harness-state", state); +// Pi session JSONL is append-only and every custom entry shows up in the branch navigator, so a +// snapshot is only worth appending when it differs from the one the branch already holds (#26). +// Tab order is not a difference: ownership is a set. +const stateKey = (state: BrowserState): string => + JSON.stringify({ + namespace: state.namespace, + ownedTargetIds: [...(state.ownedTargetIds ?? [])].sort(), + harnessWindowTargetId: state.harnessWindowTargetId ?? null, + harnessWindowId: state.harnessWindowId ?? null, + }); + +export type StatePersister = { + // Appends the snapshot unless it equals the last one persisted or restored. Returns whether it wrote. + readonly persist: (state: BrowserState) => boolean; + // Records a snapshot that already lives in the session (restored from the branch) without writing it. + readonly markPersisted: (state: BrowserState) => void; +}; + +export const createStatePersister = (pi: ExtensionAPI): StatePersister => { + let last: string | undefined; + return { + persist: (state) => { + const key = stateKey(state); + if (key === last) return false; + pi.appendEntry<BrowserState>("browser-harness-state", state); + last = key; + return true; + }, + markPersisted: (state) => { + last = stateKey(state); + }, + }; }; const asPersistedState = (v: unknown): Partial<BrowserState> | undefined => { |
