Files
Netcatty/application/syncPayload.aiSessions.test.ts
T
f5fccc0d44 fix(ai): persist AI sessions per record in IndexedDB instead of a 2MB localStorage blob (#4125)
* fix(ai): persist AI sessions per record in IndexedDB instead of a 2MB localStorage blob

Closes #4123.

The legacy store serialized every session into one localStorage JSON key
under a 2MB byte budget. Each write re-serialized the whole store, and the
degradation ladder dropped whole oldest sessions when the budget was
exceeded — silently destroying capped chats (~1MB each) on every write.

- add aiSessionIdbStorage: db netcatty_ai_sessions, one record per session
  (keyPath id) plus a meta store (schemaVersion, lastWriteAt, mirrored
  active-session map); BroadcastChannel replaces the localStorage storage
  event for cross-window sync
- one-time boot migration from netcatty_ai_sessions_v1; the legacy key
  stays untouched for one release as a read-only fallback
- writeSessionsForStorage now diffs against the last persisted set and
  writes only changed records through a serialized queue; the 2MB budget
  and its degradation ladder are removed (the global re-serialization cost
  they defended no longer exists)
- retention becomes a pure session-count cap (raised 50 -> 1000); the
  per-session 200-message cap is unchanged
- when the cap evicts sessions, a pre-eviction backup generation is
  written before the records are dropped, closing the #4018 mirror blind
  spot; explicit deletions and cloud-authoritative removals do not
  trigger it
- boot restore chain: IndexedDB -> legacy localStorage (migration) ->
  #4018 disk backup generations; a corrupt/unavailable IndexedDB falls
  back to legacy localStorage writes
- tests: IDB roundtrip/migration/active-map mirror via a minimal fake
  indexedDB, per-record put/delete diffing, count-cap eviction with
  pre-eviction generation, explicit-delete suppression, synced-history
  no-prune, corrupt-IDB fallback; byte-ladder tests removed with the
  ladder

* test(ai): update fork/undo reload tests to the IndexedDB persistence contract

Issue #4123: session persistence moved to IndexedDB per-record writes; the
persisted set no longer lands in the legacy localStorage key (read-only
fallback for one release) and the retention cap is a session count of 1000
instead of 50.

- fork/undo reload tests read the persisted set via
  getPersistedAiSessionsSnapshot() instead of parsing localStorage
- cap-boundary assertions (49/50 newer chats) become preservation
  assertions (newerCount + 2 records survive; the 1000-cap boundary is
  covered by aiStateSnapshots.test.ts)
- the reload step seeds the read-only legacy store with the persisted set,
  the stand-in for what the migration persists on a profile without
  IndexedDB

* feat(ai): make the per-session message cap user-configurable

* fix(ai): sequence session sync after boot hydration

Cloud-sync collection (collectAiSessionsForSync) could run before the
IndexedDB boot hydration settled and writeSessionsForStorage() the stale
legacy localStorage list over newer per-record IndexedDB data. The
collection now skips the sync round until the boot load settles; the next
auto-sync picks up the hydrated store.

The hydration signal lives in aiStateSnapshots (isAiSessionsHydrationComplete
/ markAiSessionsHydrationComplete) and is shared with useAIState's boot
effect and the cross-window refresh handler.

* fix(ai): make the legacy session migration one-shot via a persisted marker

The migration keyed on 'IndexedDB empty + legacy populated'. Because the
legacy key is intentionally kept for one release as a read-only fallback,
any boot whose store was empty re-ran the migration and resurrected chats
the user had deleted (P1②).

A migrationCompleted marker is now persisted in the meta store after the
migration data is durable. Once present, the legacy key is never consulted
again: an empty store is authoritative. The marker also records a store
that was already populated by a pre-marker build, and an uninitialized
empty store, so 'intentionally empty' is distinguishable from 'uninitialized'.

* fix(ai): harden boot persistence failure paths

Four review findings around the boot load's failure modes:

- P2①: when IndexedDB fails at boot and sessions come from the legacy
  store, the diff baseline was seeded even though nothing was persisted,
  so a later edit diff-wrote only the edited record and the rest were
  lost after recovery. The legacy set is now persisted best-effort (one
  re-open retry) before seeding; if that fails the baseline stays unset
  so the next edit rewrites every record.
- P2②: the active-pointer validation effect ran against the
  initially-empty in-memory session array and nulled every pointer
  before the async load finished. It now waits for the hydration signal.
- P2③: when IndexedDB stayed unopenable and the legacy store was
  missing/corrupt, the unconditional recheck threw into the swallowing
  outer catch and the #4018 disk backup was never applied. The recheck
  failure is now contained (one retry already happens inside open()),
  the newest backup generation is applied as the recovery source, and
  the error is surfaced via console.warn.
- P2④: a cross-window empty getAll (another window deleted the last
  chat) hit an early return, kept the deleted session alive locally and
  resurrected it on the next edit. After hydration an empty store is
  adopted; the early-return only covers the pre-hydration window.

Scenario regression tests live in useAIState.bootHydration.test.ts;
fakeIndexedDb gains failOpenTimes for counted open failures.

* fix(ai): gate session maintenance and migrate legacy overflow history

* fix(ai): skip backup restore of empty IDB and resume failed overflow migration

* ci: retrigger lint-and-test after runner shutdown

The previous Actions runner received a shutdown signal during plugin-contract
verification; lint had already passed and unit tests never started.

---------

Co-authored-by: GOUKI9999 <GOUKI9999@users.noreply.github.com>
Co-authored-by: 陈大猫 <16399091+binaricat@users.noreply.github.com>
2026-11-05 22:20:24 +08:00

443 lines
24 KiB
TypeScript

import { installBrowserLockFixture } from "./testSupport/browserLockFixture";
import test from 'node:test';
import assert from 'node:assert/strict';
import type { AISession } from '../infrastructure/ai/types';
import type { SyncPayload } from '../domain/sync';
import { withSyncReliabilityMeta } from '../domain/syncReliability';
import React from 'react';
import { act, create, type ReactTestRenderer } from 'react-test-renderer';
import { getAgentRuntime } from '../infrastructure/ai/harness/globalAgentRuntime';
import { useAIChatStreaming, type UseAIChatStreamingReturn } from './state/useAIChatStreaming';
import { installFakeIndexedDb } from './state/testSupport/fakeIndexedDb';
import { idbPutSessions, openAiSessionsIdb } from './state/aiSessionIdbStorage';
installBrowserLockFixture();
const storage = new Map<string, string>();
const events = new EventTarget();
Object.defineProperty(globalThis, 'localStorage', { configurable: true, value: {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => storage.set(key, value),
removeItem: (key: string) => storage.delete(key),
} });
Object.defineProperty(globalThis, 'window', { configurable: true, value: events });
const keys = await import('../infrastructure/config/storageKeys');
const snapshots = await import('./state/aiStateSnapshots');
const { getAISessionsSnapshot } = await import('./state/aiSessionsStore');
const {
buildCloudSyncPayload, buildLocalVaultPayload, collectAiSessionsForSync,
applySyncPayload, applyLocalVaultPayload, hasMeaningfulSyncData,
setAiSessionsSyncPreference,
hasCloudSyncEntityData, hasMeaningfulCloudSyncData, shouldPromptCloudVaultRecovery,
} = await import('./syncPayload');
const vault = { hosts: [], keys: [], identities: [], snippets: [], customGroups: [], knownHosts: [] };
const remotePayload = (aiSessions: AISession[]): SyncPayload => ({ ...vault, aiSessions, syncedAt: 1 });
const session = (): AISession => ({
id: 'chat', title: 'Chat', agentId: 'catty', scope: { type: 'global' }, createdAt: 1, updatedAt: 2,
messages: [{ id: 'message', role: 'user', content: 'Inspect', timestamp: 1, attachments: [{
filename: 'terminal.log', mediaType: 'text/plain', terminalSelection: true,
base64Data: 'cHJpdmF0ZQ==', previewText: 'private', filePath: '/private/device/file', lineCount: 1,
}] }],
});
function save(sessions: AISession[], enabled = true) {
snapshots.setLatestAISessionsSnapshot(sessions);
storage.set(keys.STORAGE_KEY_AI_SESSIONS, JSON.stringify(sessions));
storage.set(keys.STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS, JSON.stringify({ enabled }));
}
test.beforeEach(() => {
storage.clear();
save([], false);
snapshots.__resetIdbPersistenceForTests();
});
test.afterEach(() => {
snapshots.__resetIdbPersistenceForTests();
});
// Issue #4125 review P1①: a sync round that runs before the boot hydration
// has settled must not collect (and persist) the stale legacy store — it
// would overwrite newer IndexedDB records with pre-migration data.
test('a sync round before boot hydration skips chat collection and never overwrites IndexedDB with stale legacy data', async () => {
const idb = installFakeIndexedDb();
try {
snapshots.__resetIdbPersistenceForTests();
snapshots.__resetAiSessionsHydrationForTests(); // boot load still pending
const newer = { ...session(), title: 'Newer IndexedDB copy', updatedAt: 9 };
await idbPutSessions(await openAiSessionsIdb(), [newer]);
// Stale pre-migration copy left in the read-only legacy key.
storage.set(keys.STORAGE_KEY_AI_SESSIONS, JSON.stringify([{ ...session(), title: 'Stale legacy copy', updatedAt: 1 }]));
storage.set(keys.STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS, JSON.stringify({ enabled: true }));
snapshots.setLatestAISessionsSnapshot(null as unknown as AISession[]);
// Pre-hydration collection skips the round entirely.
assert.equal(collectAiSessionsForSync(), undefined);
await snapshots.__flushIdbWritesForTests();
assert.equal(
idb.db.dumpStore('sessions').find((s) => (s as AISession).id === 'chat')?.title,
'Newer IndexedDB copy',
);
// After hydration the collector returns the live snapshot again.
snapshots.markAiSessionsHydrationComplete();
const hydrated = [newer];
snapshots.seedLastPersistedSessions(hydrated);
snapshots.setLatestAISessionsSnapshot(hydrated);
assert.equal(collectAiSessionsForSync()?.[0].title, 'Newer IndexedDB copy');
} finally {
idb.restore();
snapshots.__resetIdbPersistenceForTests();
}
});
test('actual manual upload and auto collector honor consent and explicit empty', async () => {
save([session()], false);
const options = { collectPluginSidecars: () => undefined };
assert.equal((await buildCloudSyncPayload(vault, [], options)).aiSessions, undefined);
assert.equal(collectAiSessionsForSync(), undefined);
save([], true);
assert.deepEqual((await buildCloudSyncPayload(vault, [], options)).aiSessions, []);
assert.deepEqual(collectAiSessionsForSync(), []);
save([session()], true);
const cloud = await buildCloudSyncPayload(vault, [], options);
assert.equal(cloud.aiSessions?.length, 1);
assert.deepEqual(cloud.aiSessions, collectAiSessionsForSync());
assert.equal(JSON.stringify(cloud).includes('cHJpdmF0ZQ=='), false);
assert.equal(JSON.stringify(cloud).includes('/private/device/file'), false);
assert.equal(JSON.stringify(cloud.aiSessions?.[0].messages[0].attachments).includes('private'), false);
});
test('sync persistence survives restart beyond the local-only history limits', () => {
const many = Array.from({ length: 51 }, (_, index) => ({ ...session(), id: `chat-${index}`, updatedAt: index,
messages: Array.from({ length: 201 }, (_, turn) => ({ id: `m-${turn}`, role: 'user' as const, content: 'History', timestamp: turn })) }));
save(many);
assert.equal(snapshots.writeSessionsForStorage(many), true);
assert.equal(collectAiSessionsForSync()?.length, 51);
snapshots.setLatestAISessionsSnapshot(null as unknown as AISession[]);
const afterRestart = collectAiSessionsForSync()!;
assert.equal(afterRestart.length, 51);
assert.equal(afterRestart[0].messages.length, 201);
save(many, false);
assert.equal(snapshots.writeSessionsForStorage(many), true);
// Issue #4123: the persisted set now lives in IndexedDB (per-record
// writes); the legacy localStorage key is a read-only fallback. The
// session-count cap rose to 1000, so all 51 chats survive retention —
// only the per-session message cap (200) still trims.
const localOnly = snapshots.getPersistedAiSessionsSnapshot();
assert.equal(localOnly.length, 51);
assert.equal(localOnly[0].messages.length, 200);
});
test('legacy localStorage quota failures no longer block sync upload', async () => {
save([session()]);
const original = globalThis.localStorage.setItem;
globalThis.localStorage.setItem = (key, value) => {
if (key === keys.STORAGE_KEY_AI_SESSIONS) throw new DOMException('Full', 'QuotaExceededError');
original(key, value);
};
snapshots.setLatestAISessionsSnapshot([{ ...session(), title: 'Unsaved change' }]);
try {
// Persistence moved to IndexedDB (issue #4123), so the legacy key's
// quota can no longer fail the collect step; the in-memory snapshot is
// the source of truth for the upload.
const cloud = await buildCloudSyncPayload(vault, [], { collectPluginSidecars: () => undefined });
assert.equal(cloud.aiSessions?.[0].title, 'Unsaved change');
assert.equal(JSON.stringify(cloud).includes('cHJpdmF0ZQ=='), false);
const backup = buildLocalVaultPayload(vault);
assert.equal(backup.aiSessions?.[0].title, 'Unsaved change');
assert.equal(backup.aiSessions?.[0].messages[0].attachments?.[0].base64Data, 'cHJpdmF0ZQ==');
} finally { globalThis.localStorage.setItem = original; }
});
test('failed opt-in preference commit leaves local chats and native threads untouched', async () => {
const local = { ...session(), externalSessionId: 'native-thread' };
save([local], false);
const original = globalThis.localStorage.setItem;
const cleanups: string[] = [];
Object.assign(window, { netcatty: { aiSdkAgentCleanup: async (id: string) => { cleanups.push(id); return { ok: true }; } } });
globalThis.localStorage.setItem = (key, value) => {
if (key === keys.STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS) throw new DOMException('Full', 'QuotaExceededError');
original(key, value);
};
const incoming = { ...session(), updatedAt: 3, messages: [...session().messages,
{ id: 'remote', role: 'assistant' as const, content: 'Remote turn', timestamp: 3 }] };
try {
await assert.rejects(setAiSessionsSyncPreference(true, async () => [remotePayload([incoming])]), /preference|enable/i);
assert.deepEqual(snapshots.latestAISessionsSnapshot, [local]);
assert.deepEqual(JSON.parse(storage.get(keys.STORAGE_KEY_AI_SESSIONS)!), [local]);
assert.deepEqual(cleanups, []);
assert.equal(JSON.parse(storage.get(keys.STORAGE_KEY_CLOUD_SYNC_AI_SESSIONS)!).enabled, false);
} finally { globalThis.localStorage.setItem = original; delete (window as unknown as { netcatty?: unknown }).netcatty; }
});
test('local backup retains chat and attachment bytes with cloud consent off', async () => {
const local = session();
save([local], false);
const backup = buildLocalVaultPayload(vault);
assert.deepEqual(backup.aiSessions, [local]);
assert.equal(hasMeaningfulSyncData({ ...vault, aiSessions: [local], syncedAt: 1 }), true);
save([], false);
await applyLocalVaultPayload(backup, { importVaultData: () => undefined }, {
prepareConvergentRestore: async () => async () => {},
});
assert.equal(getAISessionsSnapshot().sessions[0].messages[0].attachments?.[0].base64Data, 'cHJpdmF0ZQ==');
});
test('chat-only data is syncable but does not suppress empty-vault recovery', () => {
const local = { ...vault, aiSessions: [session()], syncedAt: 1 };
assert.equal(hasMeaningfulCloudSyncData(local), true);
assert.equal(hasCloudSyncEntityData(local), false);
const host = { id: 'remote', label: 'Remote', hostname: 'remote.example', username: 'test', port: 22, protocol: 'ssh' as const, tags: [], os: 'linux' as const };
for (const hosts of [[host], [host, { ...host, id: 'second' }]]) {
assert.equal(shouldPromptCloudVaultRecovery(local, { ...vault, hosts, syncedAt: 2 }), true);
}
assert.equal(shouldPromptCloudVaultRecovery({ ...vault, syncedAt: 1 }, local), false);
});
test('download publishes immediately and keeps local attachment bytes on a newer message', async () => {
save([session()]);
const incoming = session();
incoming.updatedAt = 3;
incoming.messages[0].content = 'Updated';
incoming.messages[0].attachments = [{ base64Data: '', mediaType: 'text/plain', filename: 'terminal.log', terminalSelection: true, contentUnavailable: true }];
const payload: SyncPayload = { ...vault, aiSessions: [incoming], syncedAt: 2 };
await applySyncPayload(payload, { importVaultData: () => undefined });
const current = getAISessionsSnapshot().sessions[0];
assert.equal(current.messages[0].content, 'Updated');
assert.equal(current.messages[0].attachments?.[0].base64Data, 'cHJpdmF0ZQ==');
assert.equal(snapshots.latestAISessionsSnapshot?.[0], current);
assert.equal(snapshots.getPersistedAiSessionsSnapshot()[0].messages[0].content, 'Updated');
});
test('restoring an old backup without chat leaves current chats alone', async () => {
const local = session();
save([local], false);
await applyLocalVaultPayload({ ...vault, syncedAt: 1 }, { importVaultData: () => undefined }, {
prepareConvergentRestore: async () => async () => {},
});
assert.deepEqual(snapshots.latestAISessionsSnapshot, [local]);
assert.deepEqual(snapshots.getPersistedAiSessionsSnapshot(), [local]);
});
test('authoritative cloud session removal stays removed in the next upload', async () => {
save([session()]);
await applySyncPayload({ ...vault, aiSessions: [], syncedAt: 3 }, { importVaultData: () => undefined });
assert.deepEqual(getAISessionsSnapshot().sessions, []);
assert.deepEqual(collectAiSessionsForSync(), []);
assert.deepEqual((await buildCloudSyncPayload(vault, [], { collectPluginSidecars: () => undefined })).aiSessions, []);
});
test('replacement stops active turns and waits for finalizers before persisting history', async (context) => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let hook: UseAIChatStreamingReturn;
function Harness() {
hook = useAIChatStreaming({ maxIterations: 1, addMessageToSession: () => {}, updateLastMessage: () => {}, updateMessageById: () => {} });
return null;
}
let renderer: ReactTestRenderer;
await act(async () => { renderer = create(React.createElement(Harness)); });
const original = globalThis.localStorage.setItem;
try {
for (const remove of [false, true]) {
const order: string[] = [];
const controller = new AbortController();
const local = { ...session(), messages: [...session().messages,
{ id: 'reply', role: 'assistant' as const, content: 'Partial', timestamp: 2, executionStatus: 'running' as const }] };
save([local]);
// Seed the per-record diff baseline so the only 'persist' marker in
// this scenario is the final replacement write (issue #4123).
snapshots.seedLastPersistedSessions([local]);
hook!.abortControllersRef.current.set('chat', controller);
await act(async () => { hook!.setStreamingForScope('chat', true); });
Object.assign(window, { netcatty: {
aiCattyCancelExec: async () => { order.push('cancel'); },
aiSdkAgentCancel: async () => ({ ok: true }),
aiSdkAgentCleanup: async () => { order.push('cleanup'); return { ok: true }; },
} });
const wait = context.mock.method(getAgentRuntime(), 'waitForActiveTurn', async () => {
assert.equal(controller.signal.aborted, true);
await new Promise(resolve => setTimeout(resolve, 0));
snapshots.setLatestAISessionsSnapshot([{ ...local, messages: [local.messages[0],
{ ...local.messages[1], content: 'Settled', executionStatus: 'cancelled' }] }]);
order.push('settled');
});
// Issue #4123: session persistence is an IndexedDB commit now; the
// 'persist' marker fires when the record lands in the store.
globalThis.localStorage.setItem = original;
const idb = installFakeIndexedDb({ onOperation: (store, _op) => {
// Both puts and deletes are persistence commits for the sessions store.
if (store === 'sessions') order.push('persist');
} });
const incoming = { ...session(), updatedAt: 3, messages: [...session().messages,
{ id: 'remote', role: 'user' as const, content: 'Remote', timestamp: 3 }] };
await applyLocalVaultPayload(remotePayload(remove ? [] : [incoming]), { importVaultData: () => undefined }, {
prepareConvergentRestore: async () => async () => {},
});
await snapshots.__flushIdbWritesForTests();
idb.restore();
snapshots.__resetIdbPersistenceForTests();
assert.ok(order.indexOf('cancel') < order.indexOf('settled'));
assert.ok(order.indexOf('settled') < order.indexOf('persist'));
assert.ok(order.includes('cleanup'));
assert.equal(hook!.abortControllersRef.current.has('chat'), false);
if (remove) assert.deepEqual(getAISessionsSnapshot().sessions, []);
else assert.equal(getAISessionsSnapshot().sessions[0].messages.at(-1)?.content, 'Remote');
wait.mock.restore();
globalThis.localStorage.setItem = original;
await act(async () => { hook!.setStreamingForScope('chat', false); });
}
} finally {
context.mock.restoreAll(); globalThis.localStorage.setItem = original;
delete (window as unknown as { netcatty?: unknown }).netcatty;
await act(async () => { renderer!.unmount(); });
}
});
test('cloud sync preserves an active trimmed conversation and its native thread', async () => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let hook: UseAIChatStreamingReturn;
function Harness() {
hook = useAIChatStreaming({ maxIterations: 1, addMessageToSession: () => {}, updateLastMessage: () => {}, updateMessageById: () => {} });
return null;
}
let renderer: ReactTestRenderer;
await act(async () => { renderer = create(React.createElement(Harness)); });
const calls: string[] = [];
const controller = new AbortController();
const messages = Array.from({ length: 501 }, (_, i) => ({ id: `m-${i}`, role: 'user' as const, content: `Message ${i}`, timestamp: i }));
const remote = { ...session(), agentId: 'codex', messages };
// Appending the 501st message trims the oldest message locally.
const local = { ...remote, messages: messages.slice(-500), externalSessionId: 'native-thread' };
save([local]);
hook!.abortControllersRef.current.set('chat', controller);
await act(async () => { hook!.setStreamingForScope('chat', true); });
Object.assign(window, { netcatty: {
aiCattyCancelExec: async () => { calls.push('cancel'); },
aiSdkAgentCancel: async () => { calls.push('sdk-cancel'); return { ok: true }; },
aiSdkAgentCleanup: async () => { calls.push('cleanup'); return { ok: true }; },
} });
try {
for (const incoming of [[remote], []]) {
await applySyncPayload(remotePayload(incoming), { importVaultData: () => undefined });
assert.equal(controller.signal.aborted, false);
assert.deepEqual(calls, []);
assert.deepEqual(getAISessionsSnapshot().sessions, [local]);
assert.equal(snapshots.getPersistedAiSessionsSnapshot()[0].externalSessionId, 'native-thread');
}
} finally {
hook!.abortControllersRef.current.delete('chat');
await act(async () => { hook!.setStreamingForScope('chat', false); renderer!.unmount(); });
delete (window as unknown as { netcatty?: unknown }).netcatty;
}
});
test('local restore replaces later sessions and later turns with the backup snapshot', async () => {
const backedUp = session();
const later = { ...session(), updatedAt: 3, messages: [...session().messages,
{ id: 'later', role: 'assistant' as const, content: 'After backup', timestamp: 3 }] };
save([later, { ...session(), id: 'created-after-backup' }]);
await applyLocalVaultPayload({ ...vault, aiSessions: [backedUp], syncedAt: 1 }, { importVaultData: () => undefined }, {
prepareConvergentRestore: async () => async () => {},
});
assert.deepEqual(getAISessionsSnapshot().sessions, [backedUp]);
await applyLocalVaultPayload({ ...vault, aiSessions: [], syncedAt: 1 }, { importVaultData: () => undefined }, {
prepareConvergentRestore: async () => async () => {},
});
assert.deepEqual(getAISessionsSnapshot().sessions, []);
});
test('cloud downloads respect local chat opt-in while still applying vault data', async () => {
save([], false);
let vaultImports = 0;
const downloaded = { ...vault, aiSessions: [session()], syncedAt: 2 };
await applySyncPayload(downloaded, { importVaultData: () => { vaultImports++; } });
assert.equal(vaultImports, 1);
assert.deepEqual(snapshots.latestAISessionsSnapshot, []);
assert.deepEqual(snapshots.getPersistedAiSessionsSnapshot(), []);
save([], true);
await applySyncPayload(downloaded, { importVaultData: () => { vaultImports++; } });
assert.equal(vaultImports, 2);
assert.equal(snapshots.latestAISessionsSnapshot?.[0].id, 'chat');
});
test('enabling sync imports existing cloud chats before an empty local list can be uploaded', async () => {
save([], false);
let loads = 0;
await setAiSessionsSyncPreference(true, async () => {
loads++;
assert.equal(collectAiSessionsForSync(), undefined);
return [remotePayload([session()])];
});
assert.equal(loads, 1);
assert.equal(collectAiSessionsForSync()?.[0].id, 'chat');
assert.equal(getAISessionsSnapshot().sessions[0].id, 'chat');
await setAiSessionsSyncPreference(false, async () => { throw new Error('Disable must not load'); });
assert.equal(collectAiSessionsForSync(), undefined);
});
test('an unsuccessful initial cloud read leaves chat sync disabled and local history unchanged', async () => {
save([], false);
await assert.rejects(setAiSessionsSyncPreference(true, async () => { throw new Error('offline'); }), /offline/);
assert.equal(collectAiSessionsForSync(), undefined);
assert.deepEqual(snapshots.latestAISessionsSnapshot, []);
});
test('enabling from multiple mirrored providers retains the newer turns from either copy', async () => {
save([], false);
const newer = session();
newer.updatedAt = 3;
newer.messages.push({ id: 'new-turn', role: 'assistant', content: 'Newer provider turn', timestamp: 3 });
await setAiSessionsSyncPreference(true, async () => [remotePayload([newer]), remotePayload([session()])]);
assert.deepEqual(collectAiSessionsForSync()?.[0].messages.map((message) => message.id), ['message', 'new-turn']);
});
test('initial enable retains local-only chats but respects remote deletions and preservation', async () => {
const localOnly = { ...session(), id: 'local-only' };
save([localOnly], false);
await setAiSessionsSyncPreference(true, async () => [remotePayload([session()])]);
assert.deepEqual(collectAiSessionsForSync()?.map(item => item.id).sort(), ['chat', 'local-only']);
const removed = withSyncReliabilityMeta(remotePayload([]), remotePayload([session()]), { now: 3 });
for (const preserve of [false, true]) {
save([session(), localOnly], false);
await setAiSessionsSyncPreference(true, async () => [removed, remotePayload([session()])], preserve);
assert.deepEqual(collectAiSessionsForSync()?.map(item => item.id).sort(), preserve ? ['chat', 'local-only'] : ['local-only']);
}
});
test('backup rollback resets the native thread that still contains later turns', async () => {
const cleanups: string[] = [];
Object.assign(window, { netcatty: { aiSdkAgentCleanup: async (id: string) => { cleanups.push(id); return { ok: true }; } } });
const backedUp = { ...session(), agentId: 'codex', externalSessionId: 'native-thread' };
save([{ ...backedUp, updatedAt: 4, messages: [...backedUp.messages,
{ id: 'later', role: 'assistant', content: 'After backup', timestamp: 4 }] }]);
await applyLocalVaultPayload(remotePayload([backedUp]), { importVaultData: () => undefined }, {
prepareConvergentRestore: async () => async () => {},
});
const restored = getAISessionsSnapshot().sessions[0];
assert.equal(restored.externalSessionId, undefined);
assert.deepEqual(restored.messages, backedUp.messages);
assert.deepEqual(cleanups, ['chat']);
delete (window as unknown as { netcatty?: unknown }).netcatty;
});
test('cloud restore never resumes a source device SDK thread, and preserves matching local threads', async () => {
const local = { ...session(), externalSessionId: 'local-native-thread' };
save([local]);
const incoming = { ...session(), updatedAt: 3, externalSessionId: 'remote-native-thread' };
const fresh = { ...incoming, id: 'new-chat' };
await applySyncPayload({ ...vault, aiSessions: [incoming, fresh], syncedAt: 2 }, { importVaultData: () => undefined });
const current = getAISessionsSnapshot().sessions;
assert.equal(current.find((s) => s.id === 'chat')?.externalSessionId, 'local-native-thread');
assert.equal(current.find((s) => s.id === 'new-chat')?.externalSessionId, undefined);
});
test('changed remote transcript resets the local native SDK thread before continuing', async () => {
const cleanups: string[] = [];
Object.assign(window, { netcatty: { aiSdkAgentCleanup: async (id: string) => { cleanups.push(id); return { ok: true }; } } });
save([{ ...session(), agentId: 'codex', externalSessionId: 'local-native-thread' }]);
const incoming = { ...session(), agentId: 'codex', updatedAt: 3, externalSessionId: 'remote-native-thread' };
incoming.messages.push({ id: 'remote-turn', role: 'user', content: 'Added on another device', timestamp: 3 });
await applySyncPayload({ ...vault, aiSessions: [incoming], syncedAt: 2 }, { importVaultData: () => undefined });
assert.deepEqual(cleanups, ['chat']);
assert.equal(getAISessionsSnapshot().sessions[0].externalSessionId, undefined);
assert.equal(getAISessionsSnapshot().sessions[0].messages.at(-1)?.content, 'Added on another device');
delete (window as unknown as { netcatty?: unknown }).netcatty;
});