Skip to content

The design language that makes your AI harness better at design.

JavaScript

Fix: keep checkpoint revisions ahead of storage across tabs (#903) (#907)

Abdul Wahab51cdf426c2ac
diff --git a/scripts/test-suites.mjs b/scripts/test-suites.mjs
index 38d7c3c0e..7fca290d7 100644
--- a/scripts/test-suites.mjs
+++ b/scripts/test-suites.mjs
@@ -157,6 +157,7 @@ export const SUITES = {
'tests/live-agent-target.test.mjs',
'tests/live-boot-fastpath.test.mjs',
'tests/live-browser-ignores.test.mjs',
+ 'tests/live-browser-session.test.mjs',
'tests/live-browser-source.test.mjs',
'tests/live-e2e-agent-output.test.mjs',
'tests/live-e2e-cli-options.test.mjs',
diff --git a/skill/scripts/live-browser-session.js b/skill/scripts/live-browser-session.js
index 1514bf472..ad9cdf008 100644
--- a/skill/scripts/live-browser-session.js
+++ b/skill/scripts/live-browser-session.js
@@ -44,6 +44,10 @@
function saveSession(session) {
if (!session || !session.id) return;
+ // Adopt a revision another tab stored so this write does not lower it.
+ // localStorage has no compare-and-set, so a save from another tab that
+ // lands between this read and the write below can still be overwritten.
+ loadSession();
const payload = {
...session,
checkpointRevision,
@@ -56,8 +60,8 @@
}
function nextCheckpointRevision() {
- checkpointRevision += 1;
const existing = loadSession();
+ checkpointRevision += 1;
if (existing?.id) saveSession(existing);
return checkpointRevision;
}
diff --git a/tests/live-browser-session.test.mjs b/tests/live-browser-session.test.mjs
new file mode 100644
index 000000000..34a4254fd
--- /dev/null
+++ b/tests/live-browser-session.test.mjs
@@ -0,0 +1,51 @@
+import { describe, it } from 'node:test';
+import assert from 'node:assert/strict';
+import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import vm from 'node:vm';
+
+const SCRIPT = fileURLToPath(new URL('../skill/scripts/live-browser-session.js', import.meta.url));
+
+function createMemoryStorage() {
+ const values = new Map();
+ return {
+ getItem(key) { return values.has(key) ? values.get(key) : null; },
+ setItem(key, value) { values.set(key, String(value)); },
+ };
+}
+
+function loadFactory() {
+ const context = vm.createContext({});
+ vm.runInContext(readFileSync(SCRIPT, 'utf-8'), context);
+ return context.__IMPECCABLE_LIVE_SESSION__.createLiveBrowserSessionState;
+}
+
+describe('live-browser-session state helper', () => {
+ it('issues a fresh checkpoint revision when storage is ahead of this instance', () => {
+ const createState = loadFactory();
+ const storage = createMemoryStorage();
+ const first = createState({ prefix: 'impeccable-live', storage });
+ const second = createState({ prefix: 'impeccable-live', storage });
+
+ first.saveSession({ id: 'session-a' });
+ assert.equal(first.nextCheckpointRevision(), 1);
+ assert.equal(first.nextCheckpointRevision(), 2);
+
+ assert.equal(second.nextCheckpointRevision(), 3);
+ assert.equal(second.loadSession().checkpointRevision, 3);
+ });
+
+ it('keeps the stored checkpoint revision when an instance behind storage saves', () => {
+ const createState = loadFactory();
+ const storage = createMemoryStorage();
+ const first = createState({ prefix: 'impeccable-live', storage });
+ const second = createState({ prefix: 'impeccable-live', storage });
+
+ first.saveSession({ id: 'session-a' });
+ first.nextCheckpointRevision();
+ first.nextCheckpointRevision();
+
+ second.saveSession({ id: 'session-a' });
+ assert.equal(second.loadSession().checkpointRevision, 2);
+ });
+});