Compare commits

...
Author SHA1 Message Date
陈大猫 acabe0bbc2 chore: retrigger CI after runner cancel on plugin contract step
lint-and-test job 126516665951 was canceled mid plugin-contract verify
(annotation: operation canceled); lint had already passed. Empty commit
only — no code change.
2026-11-04 18:25:48 +08:00
binaricat 084ed81309 fix(ai): clear count-cap-evicted disk recovery markers
Resolve disk-overflow recovery IDs that were present in the live write
input but dropped by the 50-session cap, so they no longer block backup
mirroring indefinitely. Keep markers only for IDs still missing from the
live input (awaiting recovery), minus explicit deletions.
2026-11-04 18:12:38 +08:00
陈大猫 c0c98c7c37 fix: retain disk-only imported AI sessions in live state 2026-11-04 18:05:36 +08:00
陈大猫 e9bd347c23 fix(ai): preserve unresolved disk-only recovery across live writes
Startup mutations could clear disk-only recovery IDs before the asynchronous backup read finished, then replace the full recovery backup with partial loaded history.

Retain unresolved marker IDs across successful local primary/overflow writes and defer partial backup mirroring. Pass explicit deletion IDs from session CRUD so intentional removals stay authoritative without deleting other pending recovery entries; synced writes remain authoritative.

Bound overflow chunks to a 32 KiB minimum independently of tiny primary budgets, preserving existing sub-50 KiB per-key quota support. Reclaim/write the fallback primary before recovery metadata and roll back if metadata cannot persist. Preserve active-session pointers and panel views for pending disk-only IDs. All three requested P2s are addressed.

Tests: all 101 tests passed in the seven requested suites, including live rename/create/delete races, explicit pending-chat deletion, marker subset/cache/sync handling, bounded aggregate-quota writes, metadata reclamation, and rollback. Targeted ESLint: zero errors, one pre-existing hook dependency warning. git diff --check clean; no conflict markers in changed files.
2026-11-04 17:53:07 +08:00
Codex dd4e19974d fix: preserve disk recovery during orphan cleanup 2026-11-04 17:38:46 +08:00
陈大猫 a59d35274a fix: recover disk-only AI chats and reclaim primary quota 2026-11-04 17:30:59 +08:00
陈大猫 5d22b2da0c fix(ai): preserve recent chats under aggregate storage quota
Overflow chunks share the origin quota with the primary history, so splitting omitted chats alone can leave every later update blocked behind an old valid primary. Apply the existing replay-bulk degradation ladder to each overflow chat at tighter retry budgets, including retries whose primary JSON is unchanged.

If the complete retained set still cannot fit, schedule the full count-capped disk backup and persist the newest primary without overflow. Remember the actual successful primary JSON, restore prior chunks on failed writes, and clear partial overflow so it never remains as torn JSON.

Also free overflow quota before an authoritative synced write and restore it on failure, resolving the sync opt-in quota issue. Add aggregate 5 MiB regressions for degradation, primary fallback, full backup, later-turn reload, sync opt-in, rollback, and overflow cache invalidation. The required seven-file suite passes all 85 tests; targeted ESLint and git diff --check pass.
2026-11-04 17:17:52 +08:00
Codex 39f69caa57 fix(ai): keep sessions past 2MB localStorage budget via overflow (#4123)
Retain count-capped local chat history across primary and overflow chunks
instead of silently evicting older chats at the primary JSON budget. Back
up the full history before splitting it and recover from the merged store.

Preserve main's last-write cache and synced writeString path during rebase.
Remember the primary JSON actually written and bypass the cache whenever
overflow exists so a matching primary cannot hide missing overflow data.
2026-11-04 17:14:25 +08:00
7 changed files with 1167 additions and 171 deletions

No files matched your search

+475 -87
View File
@@ -1,10 +1,16 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import test, { type TestContext } from 'node:test';
import { createAiPrivacyService } from '../../electron/shared/aiPrivacyService.cjs';
import {
hasReversibleModelPrivacy,
setReversibleModelPrivacy,
} from '../../infrastructure/ai/harness/modelSecretRedaction';
import {
STORAGE_KEY_AI_SESSIONS,
STORAGE_KEY_AI_SESSIONS_OVERFLOW,
STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW,
STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS,
} from '../../infrastructure/config/storageKeys';
import type { AISession } from '../../infrastructure/ai/types';
import { localStorageAdapter } from '../../infrastructure/persistence/localStorageAdapter';
import {
@@ -17,6 +23,11 @@ import {
setLatestAIActiveSessionMapSnapshot,
setLatestAISessionsSnapshot,
rebindActiveSessionMap,
readSessionsFromStorage,
prewarmAIStateStorageSnapshots,
writeSessionsForStorage,
flushBackupMirror,
__resetBackupMirrorForTests,
} from './aiStateSnapshots';
test('orphan cleanup keeps durable Catty output while explicit deletion removes it', async () => {
@@ -73,6 +84,424 @@ function makeSession(id: string, updatedAt: number, messages: unknown[]): AISess
} as never;
}
function installSessionStorage(t: TestContext, aggregateQuota = Number.POSITIVE_INFINITY) {
const storage = new Map<string, string>();
const previousStorage = globalThis.localStorage;
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => {
const storedBytes = [...storage].reduce((total, [storedKey, storedValue]) => (
total + (storedKey === key ? 0 : Buffer.byteLength(storedKey + storedValue))
), 0);
if (storedBytes + Buffer.byteLength(key + value) > aggregateQuota) {
throw new DOMException('aggregate quota exceeded', 'QuotaExceededError');
}
storage.set(key, value);
},
removeItem: (key: string) => { storage.delete(key); },
},
});
__resetBackupMirrorForTests();
t.after(() => {
__resetBackupMirrorForTests();
Object.defineProperty(globalThis, 'localStorage', { configurable: true, value: previousStorage });
});
storage.set(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS, JSON.stringify({ enabled: false }));
return storage;
}
function makeLargeSessions(count = 4) {
return Array.from({ length: count }, (_, index) => makeSession(
`large-${index}`, count - index,
Array.from({ length: 200 }, (_, messageIndex) => ({
id: `message-${messageIndex}`,
role: messageIndex % 2 ? 'assistant' : 'user',
content: 'x'.repeat(5 * 1024 + 128),
timestamp: messageIndex,
})),
));
}
test('local-only writes retain four full large chats across overflow chunks and prewarming', (t) => {
const storage = installSessionStorage(t);
const sessions = freezeHistory(makeLargeSessions());
assert.ok(JSON.stringify(sessions).length > 2 * 1024 * 1024);
assert.equal(writeSessionsForStorage(sessions), true);
const primary = JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS)!) as AISession[];
assert.ok(primary.length < sessions.length);
assert.ok(storage.has(`${STORAGE_KEY_AI_SESSIONS_OVERFLOW}_2`));
for (const [key, value] of storage) {
if (key.startsWith(STORAGE_KEY_AI_SESSIONS_OVERFLOW) || key === STORAGE_KEY_AI_SESSIONS) {
assert.ok(value.length <= 2 * 1024 * 1024);
}
}
assert.deepEqual(readSessionsFromStorage(), sessions);
// A later write of the reloaded history must retain the overflow again.
assert.equal(writeSessionsForStorage(readSessionsFromStorage()), true);
assert.deepEqual(readSessionsFromStorage(), sessions);
const previousSessions = latestAISessionsSnapshot;
try {
setLatestAISessionsSnapshot(null as unknown as AISession[]);
prewarmAIStateStorageSnapshots();
assert.deepEqual(latestAISessionsSnapshot, sessions);
} finally {
setLatestAISessionsSnapshot(previousSessions as AISession[]);
}
});
test('backup includes overflow chats before the primary blob is replaced', (t) => {
installSessionStorage(t);
const backupWrites: string[] = [];
const previousWindow = globalThis.window;
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: { netcatty: { aiSessionsBackupWrite: async ({ json }: { json: string }) => {
backupWrites.push(json);
return { ok: true };
} } },
});
t.after(() => Object.defineProperty(globalThis, 'window', { configurable: true, value: previousWindow }));
const sessions = makeLargeSessions();
const setItem = globalThis.localStorage.setItem;
t.mock.method(globalThis.localStorage, 'setItem', (key: string, value: string) => {
if (key === STORAGE_KEY_AI_SESSIONS) {
flushBackupMirror();
assert.deepEqual(JSON.parse(backupWrites[0]), sessions);
}
setItem(key, value);
});
assert.equal(writeSessionsForStorage(sessions), true);
flushBackupMirror();
assert.equal(backupWrites.length, 1);
const primary = JSON.parse(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS)!) as AISession[];
const primaryIds = new Set(primary.map(session => session.id));
assert.ok(sessions.some(session => !primaryIds.has(session.id)));
});
test('shrinking local history removes stale overflow chunks and deleted chats', (t) => {
const storage = installSessionStorage(t);
const sessions = makeLargeSessions();
assert.equal(writeSessionsForStorage(sessions), true);
assert.equal(writeSessionsForStorage(sessions.slice(0, 2)), true);
assert.ok(storage.has(STORAGE_KEY_AI_SESSIONS_OVERFLOW));
assert.equal(storage.has(`${STORAGE_KEY_AI_SESSIONS_OVERFLOW}_2`), false);
assert.deepEqual(readSessionsFromStorage(), sessions.slice(0, 2));
assert.equal(writeSessionsForStorage([sessions[0]]), true);
assert.equal(storage.has(STORAGE_KEY_AI_SESSIONS_OVERFLOW), false);
assert.deepEqual(readSessionsFromStorage(), [sessions[0]]);
assert.equal(writeSessionsForStorage([]), true);
assert.deepEqual(readSessionsFromStorage(), []);
});
test('merged storage deduplicates primary and overflow by the newer updatedAt', (t) => {
const storage = installSessionStorage(t);
const primary = [makeSession('primary-newer', 3, []), makeSession('overflow-newer', 1, [])];
const overflow = [makeSession('primary-newer', 2, []), makeSession('overflow-newer', 4, [])];
storage.set(STORAGE_KEY_AI_SESSIONS, JSON.stringify(primary));
storage.set(STORAGE_KEY_AI_SESSIONS_OVERFLOW, JSON.stringify(overflow));
assert.deepEqual(readSessionsFromStorage(), [overflow[1], primary[0]]);
});
test('the 50-session count cap still applies to primary plus overflow', (t) => {
installSessionStorage(t);
const sessions = Array.from({ length: 51 }, (_, index) => makeSession(`session-${index}`, index, [{
id: 'm', role: 'user', content: 'x'.repeat(50 * 1024), timestamp: 0,
}]));
assert.equal(writeSessionsForStorage(sessions), true);
const loaded = readSessionsFromStorage();
assert.equal(loaded.length, 50);
assert.deepEqual(loaded.map(session => session.id), sessions.slice(1).reverse().map(session => session.id));
});
test('cloud sync restores overflow if its full authoritative write fails', (t) => {
const storage = installSessionStorage(t);
assert.equal(writeSessionsForStorage(makeLargeSessions()), true);
const originalOverflow = storage.get(STORAGE_KEY_AI_SESSIONS_OVERFLOW);
storage.set(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS, JSON.stringify({ enabled: true }));
const before = new Map(storage);
const sessions = [makeSession('synced', 5, [])];
const writeString = localStorageAdapter.writeString;
const write = t.mock.method(localStorageAdapter, 'writeString', (key: string, value: string) => (
key === STORAGE_KEY_AI_SESSIONS ? false : writeString(key, value)
));
assert.equal(writeSessionsForStorage(sessions), false);
assert.equal(storage.get(STORAGE_KEY_AI_SESSIONS_OVERFLOW), originalOverflow);
assert.deepEqual(storage, before);
const error = new Error('unexpected primary write failure');
write.mock.mockImplementation((key: string, value: string) => {
if (key === STORAGE_KEY_AI_SESSIONS) throw error;
return writeString(key, value);
});
assert.throws(() => writeSessionsForStorage(sessions), candidate => candidate === error);
assert.deepEqual(storage, before);
write.mock.restore();
assert.equal(writeSessionsForStorage(sessions), true);
assert.equal([...storage.keys()].some(key => key.startsWith(STORAGE_KEY_AI_SESSIONS_OVERFLOW)), false);
assert.deepEqual(readSessionsFromStorage(), sessions);
});
test('enabling cloud sync frees overflow quota for the authoritative primary', (t) => {
const storage = installSessionStorage(t, 5 * 1024 * 1024);
const sessions = freezeHistory(makeLargeSessions());
assert.equal(writeSessionsForStorage(sessions), true);
assert.ok(storage.has(STORAGE_KEY_AI_SESSIONS_OVERFLOW));
storage.set(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS, JSON.stringify({ enabled: true }));
assert.equal(writeSessionsForStorage(sessions), true);
assert.equal([...storage.keys()].some(key => key.startsWith(STORAGE_KEY_AI_SESSIONS_OVERFLOW)), false);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS)!), sessions);
assert.deepEqual(readSessionsFromStorage(), sessions);
});
test('aggregate quota retries shed replay bulk from overflow and retain every chat', (t) => {
const storage = installSessionStorage(t, 5 * 1024 * 1024);
const sessions = freezeHistory(Array.from({ length: 6 }, (_, index) => makeSession(
`bulk-${index}`, 6 - index, [{
id: `message-${index}`,
role: 'assistant',
content: 'x'.repeat(700 * 1024),
timestamp: index,
toolResults: [{ toolCallId: `call-${index}`, content: 'z'.repeat(300 * 1024) }],
providerContinuation: {
reasoningParts: [{ text: '', providerOptions: { openai: {
reasoningEncryptedContent: 'c'.repeat(100 * 1024),
} } }],
},
}],
)));
assert.ok(JSON.stringify(sessions).length > 5 * 1024 * 1024);
const writes = t.mock.method(localStorageAdapter, 'writeString');
assert.equal(writeSessionsForStorage(sessions), true);
assert.ok(writes.mock.calls.some(call => call.result === false));
const primary = JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS)!) as AISession[];
assert.equal(primary[0].id, sessions[0].id);
const overflow = JSON.parse([...storage]
.filter(([key]) => key.startsWith(STORAGE_KEY_AI_SESSIONS_OVERFLOW))
.map(([, value]) => value).join('')) as AISession[];
assert.ok(overflow.length > 0);
assert.ok(overflow.every(session => {
const message = session.messages[0];
return (message.toolResults?.[0].content.length ?? 0) <= 8 * 1024
&& !JSON.stringify(message.providerContinuation).includes('reasoningEncryptedContent');
}));
const loaded = readSessionsFromStorage();
assert.deepEqual(loaded.map(session => session.id), sessions.map(session => session.id));
assert.deepEqual(loaded.map(session => session.messages[0].content), sessions.map(session => session.messages[0].content));
});
test('a smaller primary reclaims quota before overflow when the final history fits', (t) => {
const storage = installSessionStorage(t, 5 * 1024 * 1024);
const chat = (id: string, updatedAt: number, mib: number) => makeSession(id, updatedAt, [{
id: 'm', role: 'user', content: 'x'.repeat(Math.floor(mib * 1024 * 1024)), timestamp: 0,
}]);
const previous = [chat('old-primary', 2, 1.9), chat('old-overflow', 1, 1.5)];
assert.equal(writeSessionsForStorage(previous), true);
const next = [chat('new-primary', 3, 1.1), ...previous];
assert.equal(writeSessionsForStorage(next), true);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS)!), [next[0]]);
assert.deepEqual(readSessionsFromStorage(), next);
assert.equal(storage.has(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), false);
});
test('overflow failure rolls back a replaced primary and the previous overflow', (t) => {
const storage = installSessionStorage(t, 5 * 1024 * 1024);
const previous = makeLargeSessions();
assert.equal(writeSessionsForStorage(previous), true);
const before = new Map(storage);
const writeString = localStorageAdapter.writeString;
const error = new Error('overflow write failed');
let failOnce = true;
t.mock.method(localStorageAdapter, 'writeString', (key: string, value: string) => {
if (failOnce && key === STORAGE_KEY_AI_SESSIONS_OVERFLOW) {
failOnce = false;
assert.notEqual(storage.get(STORAGE_KEY_AI_SESSIONS), before.get(STORAGE_KEY_AI_SESSIONS));
throw error;
}
return writeString(key, value);
});
assert.throws(() => writeSessionsForStorage([
{ ...previous[0], title: 'New primary' }, ...previous.slice(1),
]), candidate => candidate === error);
assert.deepEqual(storage, before);
});
test('aggregate quota falls back to a current primary with full backup and persists later turns', (t) => {
const storage = installSessionStorage(t, 5 * 1024 * 1024);
assert.equal(writeSessionsForStorage([makeSession('previous-primary', 0, [])]), true);
__resetBackupMirrorForTests();
const backupWrites: string[] = [];
const previousWindow = globalThis.window;
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: { netcatty: { aiSessionsBackupWrite: async ({ json }: { json: string }) => {
backupWrites.push(json);
return { ok: true };
} } },
});
t.after(() => Object.defineProperty(globalThis, 'window', { configurable: true, value: previousWindow }));
const sessions = freezeHistory(makeLargeSessions(6));
assert.ok(JSON.stringify(sessions).length > 5 * 1024 * 1024);
const setItem = globalThis.localStorage.setItem;
t.mock.method(globalThis.localStorage, 'setItem', (key: string, value: string) => {
if (key === STORAGE_KEY_AI_SESSIONS) {
// The full intended history must be scheduled before replacing the old
// valid primary, including on the subsequent mutation of the same chat.
flushBackupMirror();
assert.equal((JSON.parse(backupWrites.at(-1)!) as AISession[]).length, 6);
}
setItem(key, value);
});
assert.equal(writeSessionsForStorage(sessions), true);
flushBackupMirror();
assert.deepEqual(JSON.parse(backupWrites.at(-1)!), sessions);
assert.deepEqual(readSessionsFromStorage(), [sessions[0]]);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), sessions.slice(1).map(session => session.id));
assert.equal([...storage.keys()].some(key => key.startsWith(STORAGE_KEY_AI_SESSIONS_OVERFLOW)), false);
const updated = sessions.map((session, index) => index === 0 ? {
...session,
updatedAt: session.updatedAt + 1,
messages: [...session.messages.slice(1), {
id: 'new-turn', role: 'user' as const, content: 'Latest turn survives reload', timestamp: 1000,
}],
} : session);
assert.equal(writeSessionsForStorage(updated), true);
flushBackupMirror();
assert.deepEqual(JSON.parse(backupWrites.at(-1)!), updated);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS)!), [updated[0]]);
assert.deepEqual(readSessionsFromStorage(), [updated[0]]);
});
test('live writes resolve only the disk-only IDs included in primary or overflow', (t) => {
const storage = installSessionStorage(t);
const sessions = makeLargeSessions();
storage.set(STORAGE_KEY_AI_SESSIONS, JSON.stringify([sessions[0]]));
storage.set(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify([
sessions[0].id, sessions[1].id, 'unrecovered', 'deleted',
]));
assert.equal(writeSessionsForStorage(sessions, { deletedSessionIds: ['deleted'] }), true);
assert.deepEqual(readSessionsFromStorage(), sessions);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), ['unrecovered']);
// Reusing the same immutable array must still process explicit deletion,
// including when the primary itself has not changed.
assert.equal(writeSessionsForStorage(sessions, { deletedSessionIds: ['unrecovered'] }), true);
assert.equal(storage.has(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), false);
});
test('same-array primary writes preserve pending recovery until explicit deletion or sync', (t) => {
const storage = installSessionStorage(t);
const sessions = [makeSession('primary', 1, [])];
assert.equal(writeSessionsForStorage(sessions), true);
storage.set(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify(['unrecovered']));
assert.equal(writeSessionsForStorage(sessions), true);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), ['unrecovered']);
assert.equal(writeSessionsForStorage(sessions, { deletedSessionIds: ['unrecovered'] }), true);
assert.equal(storage.has(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), false);
storage.set(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify(['unrecovered']));
storage.set(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS, JSON.stringify({ enabled: true }));
assert.equal(writeSessionsForStorage(sessions), true);
assert.equal(storage.has(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), false);
});
test('tiny newest chat bounds overflow writes under aggregate quota and preserves unresolved IDs', (t) => {
const storage = installSessionStorage(t, 512 * 1024);
const sessions = [makeSession('empty-newest', 10, []), ...Array.from({ length: 3 }, (_, index) => (
makeSession(`huge-older-${index}`, 3 - index, [{
id: 'm', role: 'user', content: 'x'.repeat(768 * 1024), timestamp: 0,
}])
))];
storage.set(STORAGE_KEY_AI_SESSIONS, JSON.stringify([sessions[0]]));
storage.set(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify(['unrecovered']));
const writes = t.mock.method(globalThis.localStorage, 'setItem');
assert.equal(writeSessionsForStorage(sessions), true);
assert.ok(writes.mock.callCount() < 150, `bounded writes: ${writes.mock.callCount()}`);
assert.deepEqual(readSessionsFromStorage(), [sessions[0]]);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), [
'unrecovered', ...sessions.slice(1).map(session => session.id),
]);
});
test('primary-only fallback frees primary quota before writing recovery metadata', (t) => {
const storage = installSessionStorage(t);
const previous = [makeSession('old-primary', 1, [{
id: 'm', role: 'user', content: 'x'.repeat(64 * 1024), timestamp: 0,
}])];
const previousJson = JSON.stringify(previous);
const sessions = [makeSession('empty-newest', 10, []), ...makeLargeSessions(2)];
storage.set(STORAGE_KEY_AI_SESSIONS, previousJson);
// The old primary fills the quota. Metadata fits only after it is replaced.
const aggregateQuota = [...storage].reduce((total, [key, value]) => total + key.length + value.length, 0);
const setItem = globalThis.localStorage.setItem;
t.mock.method(globalThis.localStorage, 'setItem', (key: string, value: string) => {
const total = [...storage].reduce((sum, [storedKey, storedValue]) => (
sum + (storedKey === key ? 0 : storedKey.length + storedValue.length)
), key.length + value.length);
if (total > aggregateQuota) throw new DOMException('aggregate quota exceeded', 'QuotaExceededError');
setItem(key, value);
});
assert.equal(writeSessionsForStorage(sessions), true);
assert.deepEqual(readSessionsFromStorage(), [sessions[0]]);
assert.deepEqual(JSON.parse(storage.get(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), sessions.slice(1).map(session => session.id));
});
test('primary-only fallback rolls back when recovery metadata cannot be written', (t) => {
const storage = installSessionStorage(t);
const previous = [makeSession('previous', 0, [])];
assert.equal(writeSessionsForStorage(previous), true);
storage.set(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify(['unrecovered']));
const before = new Map(storage);
const setItem = globalThis.localStorage.setItem;
t.mock.method(globalThis.localStorage, 'setItem', (key: string, value: string) => {
if (key.startsWith(STORAGE_KEY_AI_SESSIONS_OVERFLOW) || key === STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) {
throw new DOMException('quota exceeded', 'QuotaExceededError');
}
setItem(key, value);
});
assert.equal(writeSessionsForStorage([makeSession('new', 1, []), ...makeLargeSessions(2)]), false);
assert.deepEqual(storage, before);
});
test('unchanged primary does not hide modified overflow from the write cache', (t) => {
const storage = installSessionStorage(t);
const sessions = freezeHistory(makeLargeSessions());
assert.equal(writeSessionsForStorage(sessions), true);
storage.set(STORAGE_KEY_AI_SESSIONS_OVERFLOW, 'corrupt overflow');
assert.equal(writeSessionsForStorage(sessions), true);
assert.deepEqual(readSessionsFromStorage(), sessions);
});
test('quota failures retain the previous overflow and back up the intended full set', (t) => {
const storage = installSessionStorage(t);
const original = makeLargeSessions();
assert.equal(writeSessionsForStorage(original), true);
__resetBackupMirrorForTests();
const before = new Map(storage);
const next = original.map(session => ({ ...session, title: 'Changed', updatedAt: session.updatedAt + 10 }));
t.mock.method(localStorageAdapter, 'writeString', (key: string, value: string) => {
if (key === STORAGE_KEY_AI_SESSIONS) return false;
storage.set(key, value);
return true;
});
const backupWrites: string[] = [];
const previousWindow = globalThis.window;
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: { netcatty: { aiSessionsBackupWrite: async ({ json }: { json: string }) => {
backupWrites.push(json);
return { ok: true };
} } },
});
t.after(() => Object.defineProperty(globalThis, 'window', { configurable: true, value: previousWindow }));
assert.equal(writeSessionsForStorage(next), false);
assert.deepEqual(storage, before);
assert.deepEqual(readSessionsFromStorage(), original);
flushBackupMirror();
assert.deepEqual(JSON.parse(backupWrites[0]), next);
});
test('orphan cleanup preserves resumable privacy maps until explicit chat deletion', (t) => {
const service = createAiPrivacyService({ maxSessions: 3 });
service.configure({ enabled: true });
@@ -92,6 +521,7 @@ test('orphan cleanup preserves resumable privacy maps until explicit chat deleti
const previousSessions = latestAISessionsSnapshot;
const previousActiveSessionMap = latestAIActiveSessionMapSnapshot;
t.mock.method(localStorageAdapter, 'read', () => null);
t.mock.method(localStorageAdapter, 'readString', () => null);
t.mock.method(localStorageAdapter, 'write', () => true);
t.mock.method(localStorageAdapter, 'writeString', () => true);
Object.defineProperty(globalThis, 'window', {
@@ -181,6 +611,7 @@ test('orphan cleanup marks dangling chats instead of stripping them, and clears
const previousSessions = latestAISessionsSnapshot;
const previousActiveSessionMap = latestAIActiveSessionMapSnapshot;
const read = t.mock.method(localStorageAdapter, 'read', () => null);
t.mock.method(localStorageAdapter, 'readString', () => null);
t.mock.method(localStorageAdapter, 'write', () => true);
const writeString = t.mock.method(localStorageAdapter, 'writeString', () => true);
const previousWindow = globalThis.window;
@@ -236,6 +667,7 @@ test('orphan cleanup with an empty live-target set marks all scoped chats dangli
const previousSessions = latestAISessionsSnapshot;
const previousActiveSessionMap = latestAIActiveSessionMapSnapshot;
t.mock.method(localStorageAdapter, 'read', () => null);
t.mock.method(localStorageAdapter, 'readString', () => null);
t.mock.method(localStorageAdapter, 'write', () => true);
t.mock.method(localStorageAdapter, 'writeString', () => true);
const previousWindow = globalThis.window;
@@ -626,49 +1058,26 @@ test('writeSessionsForStorage retries below nominal budgets after a quota failur
}
});
test('writeSessionsForStorage reduces several small sessions to fit a sub-512 KB quota', async () => {
const { writeSessionsForStorage } = await import('./aiStateSnapshots');
test('writeSessionsForStorage splits overflow to fit a sub-512 KB per-key quota', (t) => {
const storage = installSessionStorage(t);
const writes: string[] = [];
let stored: string | undefined;
const previousLocalStorage = globalThis.localStorage;
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: {
getItem: () => null,
setItem: (_key: string, value: string) => {
writes.push(value);
if (value.length > 300 * 1024) {
throw new DOMException('quota exceeded', 'QuotaExceededError');
}
stored = value;
},
removeItem: () => {},
},
t.mock.method(globalThis.localStorage, 'setItem', (key: string, value: string) => {
writes.push(value);
if (value.length > 300 * 1024) {
throw new DOMException('quota exceeded', 'QuotaExceededError');
}
storage.set(key, value);
});
try {
const sessions = Array.from({ length: 10 }, (_, index) => makeSession(
`session-${index}`,
10 - index,
[{
id: `message-${index}`,
role: 'user' as const,
content: 'x'.repeat(40 * 1024),
timestamp: index,
}],
));
assert.equal(writeSessionsForStorage(sessions), true);
assert.ok(writes[0].length > 300 * 1024);
assert.ok(stored);
assert.ok(stored.length <= 300 * 1024);
assert.ok((JSON.parse(stored) as AISession[]).length < sessions.length);
} finally {
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: previousLocalStorage,
});
}
const sessions = Array.from({ length: 10 }, (_, index) => makeSession(
`session-${index}`, 10 - index,
[{ id: `message-${index}`, role: 'user', content: 'x'.repeat(40 * 1024), timestamp: index }],
));
assert.equal(writeSessionsForStorage(sessions), true);
assert.ok(writes[0].length > 300 * 1024);
const primary = storage.get(STORAGE_KEY_AI_SESSIONS)!;
assert.ok(primary.length <= 300 * 1024);
assert.ok((JSON.parse(primary) as AISession[]).length < sessions.length);
assert.deepEqual(readSessionsFromStorage(), sessions);
});
test('serializeSessionsForStorage truncates slightly-over-cap tool results to strictly smaller sizes', async () => {
@@ -780,50 +1189,26 @@ test('writeSessionsForStorage keeps every chat by shedding tool output under quo
}
});
test('writeSessionsForStorage makes a final attempt with only the newest session', async () => {
const { writeSessionsForStorage } = await import('./aiStateSnapshots');
test('writeSessionsForStorage makes a final attempt with the newest primary and keeps overflow', (t) => {
const storage = installSessionStorage(t);
const writes: string[] = [];
let stored: string | undefined;
const previousLocalStorage = globalThis.localStorage;
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: {
getItem: () => null,
setItem: (_key: string, value: string) => {
writes.push(value);
if (value.length > 50 * 1024) {
throw new DOMException('quota exceeded', 'QuotaExceededError');
}
stored = value;
},
removeItem: () => {},
},
t.mock.method(globalThis.localStorage, 'setItem', (key: string, value: string) => {
writes.push(value);
if (value.length > 50 * 1024) {
throw new DOMException('quota exceeded', 'QuotaExceededError');
}
storage.set(key, value);
});
try {
const sessions = Array.from({ length: 10 }, (_, index) => makeSession(
`session-${index}`,
10 - index,
[{
id: `message-${index}`,
role: 'user' as const,
content: 'x'.repeat(40 * 1024),
timestamp: index,
}],
));
assert.equal(writeSessionsForStorage(sessions), true);
assert.equal(writes.length, 6);
assert.ok(stored);
const persisted = JSON.parse(stored) as AISession[];
assert.deepEqual(persisted.map(session => session.id), ['session-0']);
assert.ok(stored.length <= 50 * 1024);
} finally {
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: previousLocalStorage,
});
}
const sessions = Array.from({ length: 10 }, (_, index) => makeSession(
`session-${index}`, 10 - index,
[{ id: `message-${index}`, role: 'user', content: 'x'.repeat(40 * 1024), timestamp: index }],
));
assert.equal(writeSessionsForStorage(sessions), true);
assert.ok(writes.some(value => value.length > 50 * 1024));
const primary = storage.get(STORAGE_KEY_AI_SESSIONS)!;
assert.deepEqual((JSON.parse(primary) as AISession[]).map(session => session.id), ['session-0']);
assert.ok(primary.length <= 50 * 1024);
assert.deepEqual(readSessionsFromStorage(), sessions);
});
function freezeHistory<T>(value: T): T {
@@ -899,6 +1284,7 @@ test('synced storage bypasses fit-first pruning and non-quota failures still pro
const { writeSessionsForStorage } = await import('./aiStateSnapshots');
const sessions = freezeHistory(makePersistenceHistory(51, 201));
const read = t.mock.method(localStorageAdapter, 'read', () => ({ enabled: true }));
t.mock.method(localStorageAdapter, 'readString', () => null);
const writeString = t.mock.method(localStorageAdapter, 'writeString', () => false);
assert.equal(writeSessionsForStorage(sessions), false);
// Synced: one write of the complete, unpruned history (serialized once).
@@ -944,6 +1330,7 @@ test('successful store writes schedule a throttled disk backup mirror', async (t
const sessions = freezeHistory(makePersistenceHistory(3, 10));
const writeString = t.mock.method(localStorageAdapter, 'writeString', () => true);
t.mock.method(localStorageAdapter, 'read', () => null);
t.mock.method(localStorageAdapter, 'readString', () => null);
try {
assert.equal(writeSessionsForStorage(sessions), true);
// Throttled: nothing has been written to disk yet.
@@ -952,11 +1339,12 @@ test('successful store writes schedule a throttled disk backup mirror', async (t
assert.equal(backupWrites.length, 1);
// The mirror carries exactly the JSON that landed in localStorage.
assert.equal(backupWrites[0], writeString.mock.calls[0].arguments[1]);
// Failed store writes never schedule a mirror.
// A failed shrinking write still mirrors the full count-capped history.
writeString.mock.mockImplementation(() => false);
assert.equal(writeSessionsForStorage(sessions), false);
flushBackupMirror();
assert.equal(backupWrites.length, 1);
assert.equal(backupWrites.length, 2);
assert.deepEqual(JSON.parse(backupWrites[1]), sessions);
} finally {
__resetBackupMirrorForTests();
Object.defineProperty(globalThis, 'window', {
@@ -996,7 +1384,7 @@ test('rewriting the same sessions array skips re-serialization while storage sti
stored = value;
return true;
});
t.mock.method(localStorageAdapter, 'readString', () => stored);
t.mock.method(localStorageAdapter, 'readString', (key: string) => key === STORAGE_KEY_AI_SESSIONS ? stored : null);
assert.equal(writeSessionsForStorage(sessions), true);
const stringify = t.mock.method(JSON, 'stringify');
// Cloud-sync collection writes the same immutable array again.
+219 -38
View File
@@ -2,6 +2,8 @@ import { localStorageAdapter } from '../../infrastructure/persistence/localStora
import {
STORAGE_KEY_AI_ACTIVE_SESSION_MAP,
STORAGE_KEY_AI_SESSIONS,
STORAGE_KEY_AI_SESSIONS_OVERFLOW,
STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW,
STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS,
} from '../../infrastructure/config/storageKeys';
import type {
@@ -24,7 +26,7 @@ import {
} from './aiScopeCleanup';
import { emitAIStateChanged } from './aiStateEvents';
import { getAgentRuntime } from '../../infrastructure/ai/harness/globalAgentRuntime';
import { DebouncedBackupMirror, readLiveSessionsArray } from './aiSessionsBackup';
import { DebouncedBackupMirror, mergeImportedSessions, readLiveSessionsArray } from './aiSessionsBackup';
/** Typed accessor for the Electron IPC bridge exposed on `window.netcatty`. */
export interface AIBridge {
@@ -94,7 +96,7 @@ export let latestAIActiveTargetIdsSnapshot: Set<string> = new Set();
export function cleanupOrphanedAISessions(activeTargetIds: Set<string>) {
latestAIActiveTargetIdsSnapshot = activeTargetIds;
const currentSessions = latestAISessionsSnapshot
?? readLiveSessionsArray(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS));
?? readSessionsFromStorage();
// Sessions shown by a still-live scope must be protected from cleanup
// even when their own `scope.targetId` points at a closed terminal —
@@ -145,7 +147,14 @@ export function cleanupOrphanedAISessions(activeTargetIds: Set<string>) {
if (sessionsMarked) {
setLatestAISessionsSnapshot(markedSessions);
writeSessionsForStorage(markedSessions);
// Startup cleanup can run before the disk backup read resolves. Defer
// persistence until those chats are merged, preserving their recovery
// marker and avoiding a partial backup of only the loaded sessions.
const diskOnlyIds = localStorageAdapter.read<string[]>(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) ?? [];
const loadedIds = new Set(markedSessions.map(session => session.id));
if (!diskOnlyIds.some(id => !loadedIds.has(id))) {
writeSessionsForStorage(markedSessions);
}
emitAIStateChanged(STORAGE_KEY_AI_SESSIONS);
}
@@ -186,10 +195,9 @@ const MAX_STORED_SESSIONS = 50;
/** Maximum number of messages per session when persisting to localStorage. */
const MAX_SESSION_MESSAGES = 200;
/**
* Byte budget for the serialized sessions JSON. The localStorage quota is
* ~5-10 MB across all keys, and Responses reasoning ciphertext can add tens
* of KB per turn, so keep the sessions blob well under the quota with
* headroom for the rest of the app's storage keys.
* String-length budget for the primary sessions JSON and each overflow chunk.
* Replay bulk is shed from the primary blob first; whole sessions that still
* do not fit are retained in overflow rather than silently evicted.
*/
const MAX_SESSIONS_JSON_BYTES = 2 * 1024 * 1024;
/** Retry budgets used when the primary budget still fails to persist. */
@@ -492,16 +500,17 @@ export function serializeSessionsForStorage(
}
/**
* Persist sessions to localStorage with byte-budgeted pruning and retries.
* Returns true when the write succeeded; a false result means the payload
* could not be persisted even after escalation (it stays memory-only).
* Persist sessions to localStorage with byte-budgeted primary/overflow slices.
* Returns true when local history was saved, possibly as a newest-primary
* fallback with the full history scheduled for disk backup under quota pressure.
*/
// Durability mirror (issue #4015 follow-up): every successful store write
// also schedules a debounced disk backup so a wiped/recreated profile does
// not take the chat history with it. The exact JSON that landed in
// localStorage is mirrored (already byte-budgeted), throttled to at most one
// write per mutation batch.
// not take the chat history with it. Persisted history is mirrored, including
// overflow sessions, throttled to at most one write per mutation batch.
// Before a byte-budget drop, mirror the full
// count-capped set even if the subsequent localStorage writes fail.
const backupMirror = new DebouncedBackupMirror((json) => {
try {
void getAIBridge()?.aiSessionsBackupWrite?.({ json })?.catch(() => {});
@@ -549,7 +558,61 @@ function storedSessionsJsonEquals(json: string): boolean {
}
}
export function writeSessionsForStorage(sessions: AISession[]): boolean {
function overflowChunkKey(index: number): string {
return index === 0 ? STORAGE_KEY_AI_SESSIONS_OVERFLOW : `${STORAGE_KEY_AI_SESSIONS_OVERFLOW}_${index + 1}`;
}
function readOverflowChunks(): string[] {
const chunks: string[] = [];
for (let index = 0; ; index += 1) {
const chunk = localStorageAdapter.readString(overflowChunkKey(index));
if (chunk == null) return chunks;
chunks.push(chunk);
}
}
function clearOverflowChunks(startIndex = 0) {
for (let index = startIndex; localStorageAdapter.readString(overflowChunkKey(index)) != null; index += 1) {
localStorageAdapter.remove(overflowChunkKey(index));
}
}
function writeDiskOverflowIds(ids: string[]): boolean {
if (ids.length > 0) {
return localStorageAdapter.write(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, ids);
}
if (localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) != null) {
localStorageAdapter.remove(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW);
}
return true;
}
/** Replace the entire overflow blob; never leave a partially written JSON array. */
function replaceOverflowChunks(chunks: string[]): boolean {
clearOverflowChunks();
let complete = false;
try {
complete = chunks.every((chunk, index) => (
localStorageAdapter.writeString(overflowChunkKey(index), chunk)
));
return complete;
} finally {
if (!complete) clearOverflowChunks();
}
}
/** Read both local retention slices; newer copies win if a session is duplicated. */
export function readSessionsFromStorage(): AISession[] {
return mergeImportedSessions(
readLiveSessionsArray(readOverflowChunks().join('')),
readLiveSessionsArray(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS)),
);
}
export function writeSessionsForStorage(
sessions: AISession[],
options: { deletedSessionIds?: readonly string[] } = {},
): boolean {
const synced = Boolean(
localStorageAdapter.read<{ enabled?: boolean }>(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS)?.enabled,
);
@@ -559,6 +622,9 @@ export function writeSessionsForStorage(sessions: AISession[]): boolean {
lastSessionsWrite?.sessions === sessions
&& lastSessionsWrite.synced === synced
&& storedSessionsJsonEquals(lastSessionsWrite.json)
// A matching primary alone cannot establish that overflow is intact.
&& localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS_OVERFLOW) == null
&& localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) == null
) {
return true;
}
@@ -566,13 +632,94 @@ export function writeSessionsForStorage(sessions: AISession[]): boolean {
// session or turn here would turn local retention into cloud deletion.
if (synced) {
const json = JSON.stringify(sessions);
const syncedOk = localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, json);
if (syncedOk) {
scheduleBackupMirror(json);
rememberSessionsWrite(sessions, true, json);
const previousChunks = readOverflowChunks();
// The authoritative primary replaces overflow, so free its quota first.
clearOverflowChunks();
let syncedOk = false;
try {
syncedOk = localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, json);
if (syncedOk) {
clearOverflowChunks();
if (localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) != null) {
localStorageAdapter.remove(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW);
}
scheduleBackupMirror(json);
rememberSessionsWrite(sessions, true, json);
}
return syncedOk;
} finally {
if (!syncedOk) replaceOverflowChunks(previousChunks);
}
return syncedOk;
}
const inputIds = new Set(sessions.map(session => session.id));
const pruned = pruneSessionsForStorage(sessions);
const retainedIds = new Set(pruned.map(session => session.id));
const deletedIds = new Set(options.deletedSessionIds);
// A live write can race startup's backup read. Preserve marker IDs absent
// from the live input unless explicitly deleted. IDs present in the input
// are resolved even if the count cap evicts them; pending IDs block mirroring.
const diskOverflowIds = localStorageAdapter.read<unknown>(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW);
const unresolvedDiskIds: string[] = Array.isArray(diskOverflowIds)
? diskOverflowIds.filter((id): id is string => (
typeof id === 'string' && !retainedIds.has(id) && !deletedIds.has(id) && !inputIds.has(id)
))
: [];
let fullBackupScheduled = false;
const ensureFullBackup = () => {
if (!fullBackupScheduled && unresolvedDiskIds.length === 0) {
scheduleBackupMirror(JSON.stringify(pruned));
fullBackupScheduled = true;
}
};
const persistCandidate = (
candidate: { json: string; sessions: AISession[] },
budget: number,
chunkBudget = budget,
): boolean => {
const primaryIds = new Set(candidate.sessions.map(session => session.id));
const omitted = pruned.filter(session => !primaryIds.has(session.id));
if (omitted.length > 0) ensureFullBackup();
// Apply the same replay-bulk ladder to every overflow chat as budgets
// tighten. Chunking alone cannot reduce an origin's aggregate quota use.
const overflow = omitted.map(session => serializeSessionsForStorage([session], budget).sessions[0]);
const overflowJson = overflow.length > 0 ? JSON.stringify(overflow) : '';
// Split the array's JSON string, so even one oversized session can span
// chunks without losing messages. Concatenate all chunks before parsing.
const chunks: string[] = [];
// Primary budgets can shrink to a tiny empty chat. Keep overflow writes
// bounded independently while still supporting small per-key quotas.
const chunkSize = Math.max(32 * 1024, chunkBudget);
for (let offset = 0; offset < overflowJson.length; offset += chunkSize) {
chunks.push(overflowJson.slice(offset, offset + chunkSize));
}
const previousChunks = readOverflowChunks();
const previousPrimary = localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS);
let primaryWritten = false;
let persisted = false;
try {
// Reclaim both old slices before writing overflow: the final history may
// fit even when overflow plus the old, larger primary would exceed quota.
clearOverflowChunks();
if (!localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, candidate.json)) return false;
primaryWritten = true;
if (!replaceOverflowChunks(chunks)) return false;
if (!writeDiskOverflowIds(unresolvedDiskIds)) return false;
persisted = true;
if (!fullBackupScheduled && unresolvedDiskIds.length === 0) scheduleBackupMirror(candidate.json);
rememberSessionsWrite(sessions, false, candidate.json);
return true;
} finally {
if (!persisted) {
clearOverflowChunks();
if (primaryWritten) {
if (previousPrimary == null) localStorageAdapter.remove(STORAGE_KEY_AI_SESSIONS);
else localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, previousPrimary);
}
replaceOverflowChunks(previousChunks);
}
}
};
const primaryCandidates: { json: string; sessions: AISession[] }[] = [];
let previousLength = Number.POSITIVE_INFINITY;
for (const configuredBudget of [MAX_SESSIONS_JSON_BYTES, ...RETRY_SESSIONS_JSON_BYTES]) {
// A real quota failure means the next attempt must be smaller even when
@@ -584,26 +731,60 @@ export function writeSessionsForStorage(sessions: AISession[]): boolean {
: configuredBudget;
const budget = Math.min(configuredBudget, reducedBudget);
if (budget < 2) break;
const candidate = serializeSessionsForStorage(sessions, budget);
if (candidate.json.length >= previousLength) continue;
if (localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, candidate.json)) {
scheduleBackupMirror(candidate.json);
rememberSessionsWrite(sessions, false, candidate.json);
return true;
}
const candidate = serializeSessionsForStorage(pruned, budget);
primaryCandidates.push(candidate);
// Even an unchanged primary can succeed with smaller overflow chunks or
// more degraded overflow replay data at this retry budget.
if (persistCandidate(candidate, budget)) return true;
previousLength = candidate.json.length;
}
// Last resort: attempt the smallest representation this serializer can
// produce (the newest session with replay-only ciphertext removed). A very
// small amount of shared quota may still be enough to preserve that chat.
const minimalCandidate = serializeSessionsForStorage(sessions, 0);
if (
minimalCandidate.json.length < previousLength
&& localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, minimalCandidate.json)
) {
scheduleBackupMirror(minimalCandidate.json);
rememberSessionsWrite(sessions, false, minimalCandidate.json);
return true;
// Last resort: use the smallest primary representation (the newest chat
// with replay-only bulk removed), retaining other chats in overflow.
const minimalCandidate = serializeSessionsForStorage(pruned, 0);
const minimalChunkBudget = Math.min(minimalCandidate.json.length, RETRY_SESSIONS_JSON_BYTES[RETRY_SESSIONS_JSON_BYTES.length - 1]);
primaryCandidates.push(minimalCandidate);
if (persistCandidate(minimalCandidate, 0, minimalChunkBudget)) return true;
// If the complete retained set cannot fit, save the newest primary anyway.
// Schedule the full disk mirror before discarding overflow; do not let an
// old valid primary block persistence of every subsequent conversation turn.
ensureFullBackup();
const previousChunks = readOverflowChunks();
const previousPrimary = localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS);
const previousDiskOverflow = localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW);
clearOverflowChunks();
let primaryWritten = false;
let primaryPersisted = false;
try {
for (const candidate of primaryCandidates) {
const primaryIds = new Set(candidate.sessions.map(session => session.id));
const diskOnlyIds = [...new Set([
...unresolvedDiskIds,
...pruned.filter(session => !primaryIds.has(session.id)).map(session => session.id),
])];
// Replacing a large old primary can free the space needed by metadata.
// Roll it back below if the recovery marker still cannot be persisted.
if (!localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, candidate.json)) continue;
primaryWritten = true;
if (writeDiskOverflowIds(diskOnlyIds)) {
primaryPersisted = true;
rememberSessionsWrite(sessions, false, candidate.json);
return true;
}
}
} finally {
if (!primaryPersisted) {
if (primaryWritten) {
if (previousPrimary == null) localStorageAdapter.remove(STORAGE_KEY_AI_SESSIONS);
else localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS, previousPrimary);
}
if (previousDiskOverflow != null) {
localStorageAdapter.writeString(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, previousDiskOverflow);
} else if (localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) != null) {
localStorageAdapter.remove(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW);
}
replaceOverflowChunks(previousChunks);
}
}
console.warn(
'[AIState] Failed to persist AI sessions within the storage quota; recent chat history may not survive a restart.',
@@ -652,7 +833,7 @@ export function prewarmAIStateStorageSnapshots() {
try {
if (latestAISessionsSnapshot === null) {
latestAISessionsSnapshot =
localStorageAdapter.read<AISession[]>(STORAGE_KEY_AI_SESSIONS) ?? [];
readSessionsFromStorage();
}
if (latestAIActiveSessionMapSnapshot === null) {
latestAIActiveSessionMapSnapshot =
@@ -4,10 +4,10 @@ import React, { act } from 'react';
import { createRoot } from 'react-dom/client';
import { JSDOM } from 'jsdom';
import type { AISession } from '../../infrastructure/ai/types';
import { STORAGE_KEY_AI_SESSIONS, STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS } from '../../infrastructure/config/storageKeys';
import { STORAGE_KEY_AI_ACTIVE_SESSION_MAP, STORAGE_KEY_AI_SESSIONS, STORAGE_KEY_AI_SESSIONS_OVERFLOW, STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS } from '../../infrastructure/config/storageKeys';
import { useAIState } from './useAIState';
import { useAISessionsStore } from './aiSessionsStore';
import { setLatestAISessionsSnapshot } from './aiStateSnapshots';
import { __resetBackupMirrorForTests, flushBackupMirror, readSessionsFromStorage, setLatestAIActiveSessionMapSnapshot, setLatestAISessionsSnapshot, writeSessionsForStorage } from './aiStateSnapshots';
for (const enabled of [true, false]) {
test(`appending to long history preserves the synced prefix (chat sync ${enabled})`, async () => {
@@ -53,3 +53,404 @@ for (const enabled of [true, false]) {
}
});
}
test('startup and cross-window updates load overflow chats and mirror the merged history', async () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' });
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
const primary: AISession = {
id: 'primary', title: 'Primary', agentId: 'catty', scope: { type: 'global' },
createdAt: 1, updatedAt: 2, messages: [],
};
const overflow = { ...primary, id: 'overflow', title: 'Overflow', updatedAt: 1 };
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS, JSON.stringify([primary]));
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS_OVERFLOW, JSON.stringify([overflow]));
setLatestAISessionsSnapshot(null as unknown as AISession[]);
__resetBackupMirrorForTests();
const backups: string[] = [];
Object.assign(dom.window, { netcatty: { aiSessionsBackupWrite: async ({ json }: { json: string }) => {
backups.push(json);
return { ok: true };
} } });
let sessions: readonly AISession[] = [];
function Harness() {
useAIState();
sessions = useAISessionsStore().sessions;
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
try {
await act(async () => { root.render(React.createElement(Harness)); });
assert.deepEqual(sessions, [primary, overflow]);
flushBackupMirror();
assert.deepEqual(JSON.parse(backups[0]), [primary, overflow]);
const updatedOverflow = { ...overflow, title: 'Updated elsewhere', updatedAt: 3 };
await act(async () => {
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS_OVERFLOW, JSON.stringify([updatedOverflow]));
dom.window.dispatchEvent(new dom.window.StorageEvent('storage', { key: STORAGE_KEY_AI_SESSIONS_OVERFLOW }));
});
assert.deepEqual(sessions, [updatedOverflow, primary]);
} finally {
await act(async () => { root.unmount(); });
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot([]);
container.remove();
dom.window.close();
}
});
for (const scopeType of ['global', 'terminal'] as const) {
test(`import retains disk-only chats in live history across rename and deletion (${scopeType})`, async () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', {
url: 'http://localhost', storageQuota: 5 * 1024 * 1024,
});
const previousDOMException = globalThis.DOMException;
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event', 'DOMException'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
const source: AISession[] = Array.from({ length: 6 }, (_, index) => ({
id: `imported-${index}`, title: `Imported ${index}`, agentId: 'catty',
scope: scopeType === 'global' ? { type: 'global' } : { type: 'terminal', targetId: 'closed-terminal' },
createdAt: 1, updatedAt: 6 - index,
messages: [{ id: 'm', role: 'user', content: 'x'.repeat(Math.floor(1.1 * 1024 * 1024)), timestamp: 1 }],
}));
const ids = source.map(session => session.id);
const backups: string[] = [];
Object.assign(dom.window, { netcatty: {
aiSessionsBackupImport: async () => ({ success: true, json: JSON.stringify(source) }),
aiSessionsBackupWrite: async ({ json }: { json: string }) => { backups.push(json); return { ok: true }; },
} });
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS, '[]');
setLatestAISessionsSnapshot([]);
setLatestAIActiveSessionMapSnapshot({});
__resetBackupMirrorForTests();
let ai!: ReturnType<typeof useAIState>;
let sessions: readonly AISession[] = [];
function Harness() {
ai = useAIState();
sessions = useAISessionsStore().sessions;
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
try {
await act(async () => { root.render(React.createElement(Harness)); });
await act(async () => { ai.cleanupOrphanedSessions(new Set()); });
let importedCount = 0;
await act(async () => { importedCount = await ai.importSessionsBackup(); });
assert.equal(importedCount, source.length);
assert.deepEqual(sessions.map(session => session.id), ids);
assert.deepEqual(readSessionsFromStorage().map(session => session.id), [ids[0]]);
assert.deepEqual(JSON.parse(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), ids.slice(1));
assert.equal(sessions.every(session => !!session.scopeDangling === (scopeType === 'terminal')), true);
flushBackupMirror();
assert.deepEqual(JSON.parse(backups.at(-1)!), sessions);
await act(async () => { ai.updateSessionTitle(ids[0], 'Renamed after import'); });
assert.deepEqual(sessions.map(session => session.id), ids);
assert.equal(sessions[0].title, 'Renamed after import');
assert.deepEqual(JSON.parse(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), ids.slice(1));
flushBackupMirror();
assert.deepEqual(JSON.parse(backups.at(-1)!), sessions);
await act(async () => { ai.deleteSession(ids[5]); });
assert.deepEqual(sessions.map(session => session.id), ids.slice(0, 5));
assert.deepEqual(JSON.parse(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), ids.slice(1, 5));
flushBackupMirror();
assert.deepEqual(JSON.parse(backups.at(-1)!), sessions);
} finally {
await act(async () => { root.unmount(); });
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot([]);
setLatestAIActiveSessionMapSnapshot({});
container.remove();
dom.window.close();
Object.defineProperty(globalThis, 'DOMException', { configurable: true, value: previousDOMException });
}
});
}
test('startup recovers disk-only chats before mirroring a valid partial primary', async () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', {
url: 'http://localhost', storageQuota: 5 * 1024 * 1024,
});
const previousDOMException = globalThis.DOMException;
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event', 'DOMException'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
const source: AISession[] = Array.from({ length: 6 }, (_, index) => ({
id: `chat-${index}`, title: `Chat ${index}`, agentId: 'catty', scope: { type: 'global' },
createdAt: 1, updatedAt: 6 - index,
messages: [{ id: 'm', role: 'user', content: 'x'.repeat(Math.floor(1.1 * 1024 * 1024)), timestamp: 1 }],
}));
const backups: string[] = [];
let reads = 0;
let resolveRead!: (backup: { ok: boolean; json: string }) => void;
Object.assign(dom.window, { netcatty: {
aiSessionsBackupWrite: async ({ json }: { json: string }) => { backups.push(json); return { ok: true }; },
aiSessionsBackupReadLatest: () => {
reads += 1;
return new Promise(resolve => { resolveRead = resolve; });
},
} });
__resetBackupMirrorForTests();
assert.equal(writeSessionsForStorage(source), true);
flushBackupMirror();
assert.deepEqual(JSON.parse(backups[0]), source);
assert.deepEqual(readSessionsFromStorage(), [source[0]]);
assert.deepEqual(JSON.parse(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), source.slice(1).map(session => session.id));
// A newer local copy must win, and an old deleted chat in the backup must
// stay deleted: only the explicitly recorded disk-only IDs are recovered.
const primary = { ...source[0], title: 'Newer primary', updatedAt: 10 };
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS, JSON.stringify([primary]));
const expected = [primary, ...source.slice(1)];
const staleBackup = JSON.stringify([...source, { ...source[5], id: 'deleted-chat' }]);
backups.length = 0;
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot(null as unknown as AISession[]);
let sessions: readonly AISession[] = [];
function Harness() {
useAIState();
sessions = useAISessionsStore().sessions;
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
try {
await act(async () => { root.render(React.createElement(Harness)); });
assert.equal(reads, 1);
flushBackupMirror();
assert.deepEqual(backups, []);
await act(async () => { resolveRead({ ok: true, json: staleBackup }); });
assert.deepEqual(sessions, expected);
flushBackupMirror();
assert.deepEqual(JSON.parse(backups.at(-1)!), expected);
assert.deepEqual(readSessionsFromStorage(), [primary]);
} finally {
await act(async () => { root.unmount(); });
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot([]);
container.remove();
dom.window.close();
Object.defineProperty(globalThis, 'DOMException', { configurable: true, value: previousDOMException });
}
});
for (const scopeType of ['terminal', 'workspace'] as const) {
test(`startup orphan cleanup preserves pending disk-only recovery (${scopeType})`, async () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' });
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
const primary: AISession = {
id: 'closed-primary', title: 'Closed primary', agentId: 'catty',
scope: { type: scopeType, targetId: 'closed-target' },
createdAt: 1, updatedAt: 2, messages: [],
};
const diskOnly: AISession = {
...primary, id: 'disk-only', title: 'Disk only', updatedAt: 1,
scope: { type: scopeType, targetId: 'another-closed-target' },
};
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS, JSON.stringify([primary]));
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify([diskOnly.id]));
setLatestAISessionsSnapshot(null as unknown as AISession[]);
__resetBackupMirrorForTests();
const backups: string[] = [];
let reads = 0;
let resolveRead!: (backup: { ok: boolean; json: string }) => void;
Object.assign(dom.window, { netcatty: {
aiSessionsBackupWrite: async ({ json }: { json: string }) => { backups.push(json); return { ok: true }; },
aiSessionsBackupReadLatest: () => {
reads += 1;
return new Promise(resolve => { resolveRead = resolve; });
},
} });
let sessions: readonly AISession[] = [];
function Harness() {
const ai = useAIState();
sessions = useAISessionsStore().sessions;
// TerminalLayerSupport performs this cleanup on mount, while the disk
// backup IPC read can still be pending.
React.useEffect(() => { ai.cleanupOrphanedSessions(new Set()); }, [ai.cleanupOrphanedSessions]);
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
try {
await act(async () => { root.render(React.createElement(Harness)); });
assert.equal(reads, 1);
assert.deepEqual(sessions, [{ ...primary, scopeDangling: true }]);
assert.deepEqual(JSON.parse(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)!), [diskOnly.id]);
flushBackupMirror();
assert.deepEqual(backups, []);
await act(async () => { resolveRead({ ok: true, json: JSON.stringify([primary, diskOnly]) }); });
const expected = [primary, diskOnly].map(session => ({ ...session, scopeDangling: true }));
assert.deepEqual(sessions, expected);
assert.deepEqual(readSessionsFromStorage(), expected);
assert.equal(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), null);
flushBackupMirror();
assert.deepEqual(JSON.parse(backups.at(-1)!), expected);
} finally {
await act(async () => { root.unmount(); });
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot([]);
container.remove();
dom.window.close();
}
});
}
for (const mutation of ['rename', 'create', 'delete', 'delete-pending'] as const) {
test(`live ${mutation} preserves intended history across pending disk recovery`, async () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' });
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
const primary: AISession = {
id: 'primary', title: 'Primary', agentId: 'catty', scope: { type: 'global' },
createdAt: 1, updatedAt: 2, messages: [],
};
const diskOnly = { ...primary, id: 'disk-only', updatedAt: 1 };
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS, JSON.stringify([primary]));
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify([diskOnly.id]));
dom.window.localStorage.setItem(STORAGE_KEY_AI_ACTIVE_SESSION_MAP, JSON.stringify({ 'global:': diskOnly.id }));
setLatestAIActiveSessionMapSnapshot({ 'global:': diskOnly.id });
setLatestAISessionsSnapshot(null as unknown as AISession[]);
__resetBackupMirrorForTests();
const backups: string[] = [];
let resolveRead!: (backup: { ok: boolean; json: string }) => void;
Object.assign(dom.window, { netcatty: {
aiSessionsBackupWrite: async ({ json }: { json: string }) => { backups.push(json); return { ok: true }; },
aiSessionsBackupReadLatest: () => new Promise(resolve => { resolveRead = resolve; }),
} });
let ai!: ReturnType<typeof useAIState>;
let snapshot!: ReturnType<typeof useAISessionsStore>;
function Harness() {
ai = useAIState();
snapshot = useAISessionsStore();
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
try {
await act(async () => { root.render(React.createElement(Harness)); });
assert.equal(snapshot.activeSessionIdMap['global:'], diskOnly.id);
await act(async () => {
if (mutation === 'rename') ai.updateSessionTitle(primary.id, 'Renamed before recovery');
else if (mutation === 'create') ai.createSession({ type: 'global' }, 'catty');
else ai.deleteSession(mutation === 'delete-pending' ? diskOnly.id : primary.id);
});
const liveBeforeRecovery = [...snapshot.sessions];
const expectedActiveId = mutation === 'create' ? liveBeforeRecovery[0].id
: mutation === 'delete-pending' ? null : diskOnly.id;
assert.equal(snapshot.activeSessionIdMap['global:'], expectedActiveId);
assert.deepEqual(readSessionsFromStorage(), liveBeforeRecovery);
const recoveryMarker = dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW);
assert.deepEqual(recoveryMarker == null ? null : JSON.parse(recoveryMarker), mutation === 'delete-pending' ? null : [diskOnly.id]);
flushBackupMirror();
assert.deepEqual(backups.map(json => JSON.parse(json)), mutation === 'delete-pending' ? [liveBeforeRecovery] : []);
await act(async () => { resolveRead({ ok: true, json: JSON.stringify([primary, diskOnly]) }); });
const expected = mutation === 'delete-pending' ? liveBeforeRecovery : [...liveBeforeRecovery, diskOnly];
assert.deepEqual(snapshot.sessions, expected);
assert.deepEqual(readSessionsFromStorage(), expected);
assert.equal(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), null);
flushBackupMirror();
assert.deepEqual(JSON.parse(backups.at(-1)!), expected);
} finally {
await act(async () => { root.unmount(); });
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot([]);
setLatestAIActiveSessionMapSnapshot({});
container.remove();
dom.window.close();
}
});
}
test('count-cap eviction clears a loaded disk-only marker and resumes backup mirroring', () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' });
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
const source: AISession[] = Array.from({ length: 51 }, (_, index) => ({
id: `chat-${index}`, title: `Chat ${index}`, agentId: 'catty', scope: { type: 'global' },
createdAt: 1, updatedAt: 51 - index, messages: [],
}));
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify([source[50].id]));
const backups: string[] = [];
Object.assign(dom.window, { netcatty: { aiSessionsBackupWrite: async ({ json }: { json: string }) => {
backups.push(json);
return { ok: true };
} } });
__resetBackupMirrorForTests();
try {
assert.equal(writeSessionsForStorage(source), true);
assert.deepEqual(readSessionsFromStorage(), source.slice(0, 50));
assert.equal(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), null);
flushBackupMirror();
assert.deepEqual(backups.map(json => JSON.parse(json)), [source.slice(0, 50)]);
} finally {
__resetBackupMirrorForTests();
dom.window.close();
}
});
for (const keepPrimary of [true, false]) {
test(`deletion clears disk-only recovery and remains authoritative (keep primary ${keepPrimary})`, async () => {
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' });
for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event'] as const) {
Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] });
}
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
const primary: AISession = {
id: 'primary', title: 'Primary', agentId: 'catty', scope: { type: 'global' },
createdAt: 1, updatedAt: 2, messages: [],
};
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS, JSON.stringify([primary]));
dom.window.localStorage.setItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW, JSON.stringify(['deleted-chat']));
const remaining = keepPrimary ? [primary] : [];
assert.equal(writeSessionsForStorage(remaining, { deletedSessionIds: ['deleted-chat'] }), true);
assert.equal(dom.window.localStorage.getItem(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW), null);
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot(null as unknown as AISession[]);
let reads = 0;
Object.assign(dom.window, { netcatty: { aiSessionsBackupReadLatest: async () => {
reads += 1;
return { ok: true, json: JSON.stringify([primary, { ...primary, id: 'deleted-chat' }]) };
} } });
let sessions: readonly AISession[] = [];
function Harness() {
useAIState();
sessions = useAISessionsStore().sessions;
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
try {
await act(async () => { root.render(React.createElement(Harness)); });
assert.equal(reads, 0);
assert.deepEqual(sessions, remaining);
} finally {
await act(async () => { root.unmount(); });
__resetBackupMirrorForTests();
setLatestAISessionsSnapshot([]);
container.remove();
dom.window.close();
}
});
}
+53 -42
View File
@@ -14,6 +14,8 @@ import {
STORAGE_KEY_AI_RESPONSE_IDLE_TIMEOUT,
STORAGE_KEY_AI_MAX_ITERATIONS,
STORAGE_KEY_AI_SESSIONS,
STORAGE_KEY_AI_SESSIONS_OVERFLOW,
STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW,
STORAGE_KEY_AI_ACTIVE_SESSION_MAP,
STORAGE_KEY_AI_AGENT_MODEL_MAP,
STORAGE_KEY_AI_AGENT_PROVIDER_MAP,
@@ -73,6 +75,7 @@ import {
latestAIPanelViewByScopeSnapshot,
latestAISessionsSnapshot,
prepareRestoredSessions,
readSessionsFromStorage,
scheduleBackupMirror,
rebindActiveSessionMap,
writeSessionsForStorage,
@@ -88,7 +91,6 @@ import {
parseSessionsStore,
mergeImportedSessions,
parseBackupSessions,
readLiveSessionsArray,
} from './aiSessionsBackup';
import { AI_STATE_CHANGED_EVENT, emitAIStateChanged } from './aiStateEvents';
import {
@@ -181,10 +183,10 @@ export function useAIState() {
// ── Sessions ──
const [sessions, setSessionsRaw] = useState<AISession[]>(() =>
latestAISessionsSnapshot
?? readLiveSessionsArray(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS))
?? readSessionsFromStorage()
);
// An explicit empty array is authoritative, even if the backup is stale.
// Only a missing or corrupt store can be restored from disk.
// Disk-only overflow IDs also permit recovery, without reviving deleted chats.
useEffect(() => {
window.addEventListener('beforeunload', flushBackupMirror);
return () => window.removeEventListener('beforeunload', flushBackupMirror);
@@ -192,39 +194,44 @@ export function useAIState() {
useEffect(() => {
const liveRaw = localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS);
const liveStore = parseSessionsStore(liveRaw);
if (liveStore.valid) {
if (liveStore.sessions.length > 0 && liveRaw != null) scheduleBackupMirror(liveRaw);
const liveSessions = readSessionsFromStorage();
const diskOnlyRecovery = (localStorageAdapter.read<string[]>(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)?.length ?? 0) > 0;
if (!diskOnlyRecovery && (liveStore.valid || liveSessions.length > 0)) {
if (liveSessions.length > 0) scheduleBackupMirror(JSON.stringify(liveSessions));
return;
}
if (latestAISessionsSnapshot && latestAISessionsSnapshot.length > 0) return;
if (!diskOnlyRecovery && latestAISessionsSnapshot && latestAISessionsSnapshot.length > 0) return;
const bridge = getAIBridge();
if (!bridge?.aiSessionsBackupReadLatest) return;
let cancelled = false;
const canRestore = () => diskOnlyRecovery
? (localStorageAdapter.read<string[]>(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW)?.length ?? 0) > 0
: isMissingOrCorruptSessionsStore(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS))
&& readSessionsFromStorage().length === 0
&& (latestAISessionsSnapshot?.length ?? 0) === 0
&& sessionsRef.current.length === 0;
void (async () => {
try {
const liveRaw = localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS);
if (!isMissingOrCorruptSessionsStore(liveRaw)) return;
if (!canRestore()) return;
const backup = await bridge.aiSessionsBackupReadLatest?.();
if (cancelled || !backup?.ok) return;
// Re-check after the async gap: a session may have been created
// while the backup was being read; never restore over live state.
// Cover both persisted storage and in-memory snapshot/React state —
// createSession updates the snapshot before the debounced disk write.
if (!isMissingOrCorruptSessionsStore(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS))) {
return;
}
if ((latestAISessionsSnapshot?.length ?? 0) > 0) return;
// Re-check after the async gap so live mutations and deletions win.
if (!canRestore()) return;
const backupSessions = parseBackupSessions(backup.json);
if (!backupSessions) return;
const diskOnlyIds = new Set(localStorageAdapter.read<string[]>(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) ?? []);
const recovered = diskOnlyRecovery
? mergeImportedSessions(
backupSessions.filter(session => diskOnlyIds.has(session.id)),
latestAISessionsSnapshot ?? readSessionsFromStorage(),
)
: backupSessions;
const syncEnabled = !!localStorageAdapter.read<{ enabled?: boolean }>(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS)?.enabled;
const restored = prepareRestoredSessions(parseBackupSessions(backup.json) ?? [], syncEnabled);
const restored = prepareRestoredSessions(recovered, syncEnabled);
if (restored.length === 0) return;
// Apply restore synchronously (not inside a setState updater). A
// deferred updater would leave didRestore false and skip dangling
// marking even when the records later land in React state.
if ((latestAISessionsSnapshot?.length ?? 0) > 0) return;
if (sessionsRef.current.length > 0) return;
if (!isMissingOrCorruptSessionsStore(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS))) {
return;
}
setLatestAISessionsSnapshot(restored);
writeSessionsForStorage(restored);
emitAIStateChanged(STORAGE_KEY_AI_SESSIONS);
@@ -326,6 +333,11 @@ export function useAIState() {
useEffect(() => {
const validSessionIds = new Set<string>(sessions.map((session) => session.id));
// Disk-only chats may not have arrived from the startup backup read yet.
// Keep their active pointers and panel views until recovery or deletion.
for (const id of localStorageAdapter.read<string[]>(STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW) ?? []) {
validSessionIds.add(id);
}
let changed = false;
const nextActiveSessionIdMap: Record<string, string | null> = {};
@@ -545,7 +557,9 @@ export function useAIState() {
useEffect(() => {
const handleStorage = (e: StorageEvent) => {
try {
switch (e.key) {
const key = e.key === STORAGE_KEY_AI_SESSIONS_OVERFLOW || e.key?.startsWith(`${STORAGE_KEY_AI_SESSIONS_OVERFLOW}_`)
? STORAGE_KEY_AI_SESSIONS : e.key;
switch (key) {
case STORAGE_KEY_AI_PROVIDERS: {
const parsed = localStorageAdapter.read<ProviderConfig[]>(STORAGE_KEY_AI_PROVIDERS);
if (parsed != null && !Array.isArray(parsed)) {
@@ -637,7 +651,7 @@ export function useAIState() {
break;
}
case STORAGE_KEY_AI_SESSIONS: {
const nextSessions = localStorageAdapter.read<AISession[]>(STORAGE_KEY_AI_SESSIONS) ?? [];
const nextSessions = readSessionsFromStorage();
setLatestAISessionsSnapshot(nextSessions);
setSessionsRaw(nextSessions);
break;
@@ -679,8 +693,7 @@ export function useAIState() {
case STORAGE_KEY_AI_SESSIONS:
setSessionsRaw(
latestAISessionsSnapshot
?? localStorageAdapter.read<AISession[]>(STORAGE_KEY_AI_SESSIONS)
?? [],
?? readSessionsFromStorage(),
);
return;
case STORAGE_KEY_AI_ACTIVE_SESSION_MAP:
@@ -732,8 +745,8 @@ export function useAIState() {
}, []);
// ── Session CRUD ──
const persistSessions = useCallback((next: AISession[]) => {
writeSessionsForStorage(next);
const persistSessions = useCallback((next: AISession[], deletedSessionIds?: readonly string[]) => {
writeSessionsForStorage(next, { deletedSessionIds });
}, []);
// Debounced version of persistSessions for high-frequency updates (e.g. streaming)
@@ -878,7 +891,7 @@ export function useAIState() {
setSessionsRaw(prev => {
const next = prev.filter(s => s.id !== sessionId);
setLatestAISessionsSnapshot(next);
persistSessions(next);
persistSessions(next, [sessionId]);
return next;
});
if (scopeKey) {
@@ -922,7 +935,7 @@ export function useAIState() {
return !(s.scope.type === scopeType && s.scope.targetId === targetId);
});
setLatestAISessionsSnapshot(next);
persistSessions(next);
persistSessions(next, removedSessionIds);
return next;
});
const scopeKey = `${scopeType}:${targetId}`;
@@ -1018,11 +1031,12 @@ export function useAIState() {
const syncEnabled = !!localStorageAdapter.read<{ enabled?: boolean }>(STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS)?.enabled;
const next = prepareRestoredSessions(mergeImportedSessions(sessionsRef.current, imported), syncEnabled);
if (!writeSessionsForStorage(next)) return 0;
const persisted = readLiveSessionsArray(localStorageAdapter.readString(STORAGE_KEY_AI_SESSIONS));
// Local readback can omit disk-only overflow chats. Keep the complete
// prepared history live so cleanup and later writes retain those chats.
const importedIds = new Set(imported.map(session => session.id));
sessionsRef.current = persisted;
setLatestAISessionsSnapshot(persisted);
setSessionsRaw(persisted);
sessionsRef.current = next;
setLatestAISessionsSnapshot(next);
setSessionsRaw(next);
emitAIStateChanged(STORAGE_KEY_AI_SESSIONS);
// Import does not change live terminals/workspaces, so the orphan-cleanup
// effect will not run. Reuse the last known live target set (empty is
@@ -1031,7 +1045,7 @@ export function useAIState() {
cleanupOrphanedAISessions(latestAIActiveTargetIdsSnapshot);
const nextSessions =
latestAISessionsSnapshot
?? persisted;
?? next;
sessionsRef.current = nextSessions;
setSessionsRaw(nextSessions);
setActiveSessionIdMapRaw(
@@ -1039,7 +1053,7 @@ export function useAIState() {
?? localStorageAdapter.read<Record<string, string | null>>(STORAGE_KEY_AI_ACTIVE_SESSION_MAP)
?? {},
);
return persisted.filter(session => importedIds.has(session.id)).length;
return nextSessions.filter(session => importedIds.has(session.id)).length;
}, []);
const updateSessionExternalSessionId = useCallback((sessionId: string, externalSessionId: string | undefined) => {
@@ -1311,8 +1325,7 @@ export function useAIState() {
const nextSessions =
latestAISessionsSnapshot
?? localStorageAdapter.read<AISession[]>(STORAGE_KEY_AI_SESSIONS)
?? [];
?? readSessionsFromStorage();
sessionsRef.current = nextSessions;
setSessionsRaw(nextSessions);
setActiveSessionIdMapRaw(
@@ -1364,8 +1377,7 @@ export function useAIState() {
?? {};
const currentSessions =
latestAISessionsSnapshot
?? localStorageAdapter.read<AISession[]>(STORAGE_KEY_AI_SESSIONS)
?? [];
?? readSessionsFromStorage();
const currentPanelViewByScope = latestAIPanelViewByScopeSnapshot ?? {};
const result = handoffDissolvedWorkspaceAIScope({
activeSessionIdMap: currentMap,
@@ -1410,8 +1422,7 @@ export function useAIState() {
?? {};
const currentSessions =
latestAISessionsSnapshot
?? localStorageAdapter.read<AISession[]>(STORAGE_KEY_AI_SESSIONS)
?? [];
?? readSessionsFromStorage();
const result = retargetWorkspaceActiveChatAfterMemberLoss({
activeSessionIdMap: currentMap,
sessions: currentSessions,
@@ -79,6 +79,17 @@ test('sync persistence survives restart beyond the local-only history limits', (
assert.equal(localOnly[0].messages.length, 200);
});
test('sync collection after restart includes overflow before clearing it', () => {
const primary = session();
const overflow = { ...session(), id: 'overflow-chat', updatedAt: 1 };
save([primary], true);
storage.set(keys.STORAGE_KEY_AI_SESSIONS_OVERFLOW, JSON.stringify([overflow]));
snapshots.setLatestAISessionsSnapshot(null as unknown as AISession[]);
assert.deepEqual(collectAiSessionsForSync()?.map(value => value.id), [primary.id, overflow.id]);
assert.equal(storage.has(keys.STORAGE_KEY_AI_SESSIONS_OVERFLOW), false);
assert.deepEqual(JSON.parse(storage.get(keys.STORAGE_KEY_AI_SESSIONS)!).map((value: AISession) => value.id), [primary.id, overflow.id]);
});
test('failed history persistence refuses upload without pruning the cloud representation', async () => {
save([session()]);
const original = globalThis.localStorage.setItem;
+2 -2
View File
@@ -51,6 +51,7 @@ import { sanitizeQuickMessages } from '../infrastructure/ai/quickMessages';
import { emitAIStateChanged } from './state/aiStateEvents';
import {
latestAISessionsSnapshot,
readSessionsFromStorage,
setLatestAISessionsSnapshot,
writeSessionsForStorage,
cleanupSdkAgentSessions,
@@ -1073,8 +1074,7 @@ export function collectAiSessionsForSync(): SyncPayload['aiSessions'] {
function collectLocalAiSessions(): NonNullable<SyncPayload['aiSessions']> {
return latestAISessionsSnapshot
?? localStorageAdapter.read<SyncPayload['aiSessions']>(STORAGE_KEY_AI_SESSIONS)
?? [];
?? readSessionsFromStorage();
}
/** Build the portable payload; device-local data is added by the backup builder. */
+4
View File
@@ -218,6 +218,10 @@ export const STORAGE_KEY_AI_COMMAND_TIMEOUT = 'netcatty_ai_command_timeout_v1';
export const STORAGE_KEY_AI_RESPONSE_IDLE_TIMEOUT = 'netcatty_ai_response_idle_timeout_v1';
export const STORAGE_KEY_AI_MAX_ITERATIONS = 'netcatty_ai_max_iterations_v1';
export const STORAGE_KEY_AI_SESSIONS = 'netcatty_ai_sessions_v1';
/** Byte-budget overflow JSON; further chunks use suffixes _2, _3, etc. */
export const STORAGE_KEY_AI_SESSIONS_OVERFLOW = 'netcatty_ai_sessions_overflow_v1';
/** IDs intentionally retained only in the disk backup after aggregate quota exhaustion. */
export const STORAGE_KEY_AI_SESSIONS_DISK_OVERFLOW = 'netcatty_ai_sessions_disk_overflow_v1';
/** Opt-in flag: include AI chat sessions in cloud sync payloads. */
export const STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS = 'netcatty_cloud_sync_ai_sessions_v1';
export const STORAGE_KEY_AI_ACTIVE_SESSION_MAP = 'netcatty_ai_active_session_map_v1';