mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 12:01:13 +00:00
* 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>
236 lines
11 KiB
TypeScript
236 lines
11 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import React, { act } from 'react';
|
|
import { createRoot } from 'react-dom/client';
|
|
import { JSDOM } from 'jsdom';
|
|
|
|
import type { AISession, UploadedFile } from '../../infrastructure/ai/types.ts';
|
|
import { getAgentRuntime } from '../../infrastructure/ai/harness/globalAgentRuntime.ts';
|
|
import { useAIState } from './useAIState.ts';
|
|
import { useAISessionsStore } from './aiSessionsStore.ts';
|
|
import { getSessionScopeMatchRank } from '../../components/ai/sessionScopeMatch.ts';
|
|
import { getPersistedAiSessionsSnapshot, setLatestAISessionsSnapshot } from './aiStateSnapshots.ts';
|
|
|
|
const SCOPE_KEY = 'terminal:terminal-a';
|
|
|
|
const SOURCE_SESSION: AISession = {
|
|
id: 'chat-source',
|
|
title: 'Undo source',
|
|
agentId: 'catty',
|
|
scope: { type: 'terminal', targetId: 'terminal-a' },
|
|
messages: [
|
|
{ id: 'user-1', role: 'user', content: 'first prompt', timestamp: 1 },
|
|
{ id: 'assistant-1', role: 'assistant', content: 'first answer', timestamp: 2 },
|
|
{
|
|
id: 'user-2',
|
|
role: 'user',
|
|
content: 'bad last prompt',
|
|
timestamp: 3,
|
|
attachments: [
|
|
{ base64Data: 'QUJD', mediaType: 'text/plain', filename: 'note.txt', lineCount: 2 },
|
|
],
|
|
},
|
|
{ id: 'assistant-2', role: 'assistant', content: 'bad answer', timestamp: 4 },
|
|
],
|
|
createdAt: 1,
|
|
updatedAt: 4,
|
|
};
|
|
|
|
type HookCapture = {
|
|
ai: ReturnType<typeof useAIState> | null;
|
|
sessions: AISession[];
|
|
activeSessionIdMap: Record<string, string | null>;
|
|
};
|
|
|
|
function UndoHarness({ capture }: { capture: HookCapture }) {
|
|
capture.ai = useAIState();
|
|
const store = useAISessionsStore();
|
|
capture.sessions = store.sessions as AISession[];
|
|
capture.activeSessionIdMap = store.activeSessionIdMap as Record<string, string | null>;
|
|
return null;
|
|
}
|
|
|
|
async function setupAiState(sessions: AISession[]) {
|
|
const dom = new JSDOM('<!doctype html><html><body></body></html>', {
|
|
url: 'http://localhost',
|
|
});
|
|
Object.defineProperty(globalThis, 'window', { configurable: true, value: dom.window });
|
|
Object.defineProperty(globalThis, 'document', { configurable: true, value: dom.window.document });
|
|
Object.defineProperty(globalThis, 'localStorage', { configurable: true, value: dom.window.localStorage });
|
|
Object.defineProperty(globalThis, 'CustomEvent', { configurable: true, value: dom.window.CustomEvent });
|
|
Object.defineProperty(globalThis, 'Event', { configurable: true, value: dom.window.Event });
|
|
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true });
|
|
|
|
setLatestAISessionsSnapshot(sessions);
|
|
dom.window.localStorage.setItem('netcatty_ai_sessions_v1', JSON.stringify(sessions));
|
|
|
|
const container = document.createElement('div');
|
|
document.body.appendChild(container);
|
|
const root = createRoot(container);
|
|
const capture: HookCapture = { ai: null, sessions: [], activeSessionIdMap: {} };
|
|
await act(async () => {
|
|
root.render(React.createElement(UndoHarness, { capture }));
|
|
});
|
|
return { dom, container, root, capture };
|
|
}
|
|
|
|
test('undoLastTurnInSession branches and restores the last user turn', async () => {
|
|
const { dom, container, root, capture } = await setupAiState([SOURCE_SESSION]);
|
|
assert.ok(capture.ai);
|
|
|
|
let result: Awaited<ReturnType<typeof runUndo>> | null = null;
|
|
async function runUndo() {
|
|
return capture.ai!.undoLastTurnInSession('chat-source');
|
|
}
|
|
|
|
await act(async () => {
|
|
result = await runUndo();
|
|
});
|
|
|
|
assert.ok(result);
|
|
assert.notEqual(result!.sessionId, 'chat-source');
|
|
|
|
const sessions = capture.sessions;
|
|
const branch = sessions.find((session) => session.id === result!.sessionId);
|
|
assert.ok(branch);
|
|
// Branch keeps the conversation up to the boundary before the last turn.
|
|
assert.deepEqual(
|
|
branch!.messages.map((message) => message.content),
|
|
['first prompt', 'first answer'],
|
|
);
|
|
// Original session stays intact (non-destructive).
|
|
const original = sessions.find((session) => session.id === 'chat-source');
|
|
assert.equal(original?.messages.length, 4);
|
|
// Branch becomes the active session for the scope.
|
|
assert.equal(capture.activeSessionIdMap[SCOPE_KEY], result!.sessionId);
|
|
|
|
const attachments = result!.restored.attachments as UploadedFile[];
|
|
assert.equal(result!.restored.text, 'bad last prompt');
|
|
assert.equal(attachments.length, 1);
|
|
assert.equal(attachments[0].filename, 'note.txt');
|
|
assert.equal(attachments[0].dataUrl, 'data:text/plain;base64,QUJD');
|
|
|
|
await act(async () => root.unmount());
|
|
container.remove();
|
|
dom.window.close();
|
|
});
|
|
|
|
test('undoLastTurnInSession returns null when there is nothing to undo', async () => {
|
|
const emptySession: AISession = {
|
|
...SOURCE_SESSION,
|
|
id: 'chat-empty',
|
|
messages: [],
|
|
};
|
|
const { dom, container, root, capture } = await setupAiState([emptySession]);
|
|
assert.ok(capture.ai);
|
|
|
|
let result: unknown;
|
|
await act(async () => {
|
|
result = await capture.ai!.undoLastTurnInSession('chat-empty');
|
|
});
|
|
assert.equal(result, null);
|
|
|
|
await act(async () => root.unmount());
|
|
container.remove();
|
|
dom.window.close();
|
|
});
|
|
|
|
test('undo copies chat only, preserving original outputs and job ownership', async () => {
|
|
const runtime = getAgentRuntime();
|
|
const outputStore = runtime.getToolOutputStore('chat-source');
|
|
const handle = outputStore.store({
|
|
chatSessionId: 'chat-source', capabilityId: 'terminal.exec', content: 'original saved output',
|
|
});
|
|
const stateStore = runtime.getSessionStateStore();
|
|
stateStore.updateFromToolResult('chat-source', 'terminal_start',
|
|
{ sessionId: 'terminal-a', command: 'long-running-command' },
|
|
JSON.stringify({ jobId: 'original-job', status: 'running', nextOffset: 7 }), false);
|
|
const source = structuredClone(SOURCE_SESSION);
|
|
source.messages[1].content = `Preview with handleId=${handle.id}`;
|
|
const { dom, container, root, capture } = await setupAiState([source]);
|
|
const original = JSON.stringify(source);
|
|
let result: { sessionId: string } | null = null;
|
|
await act(async () => { result = await capture.ai!.undoLastTurnInSession(source.id); });
|
|
assert.ok(result);
|
|
assert.equal(outputStore.read({ handleId: handle.id }, result!.sessionId), null);
|
|
assert.equal(outputStore.read({ handleId: handle.id }, source.id), 'original saved output');
|
|
assert.deepEqual(stateStore.get(result!.sessionId).activeJobs, {});
|
|
assert.ok(stateStore.get(source.id).activeJobs['original-job']);
|
|
assert.equal(JSON.stringify({ ...capture.sessions.find(item => item.id === source.id), updatedAt: source.updatedAt }), original);
|
|
assert.ok(capture.sessions.find(item => item.id === result!.sessionId)!.messages[1].content.includes('Preview'));
|
|
stateStore.clear(source.id);
|
|
outputStore.prune(source.id);
|
|
await act(async () => root.unmount());
|
|
container.remove(); dom.window.close();
|
|
});
|
|
|
|
for (const newerCount of [49, 50]) {
|
|
test(`undo retains an old source and its branch after reload with ${newerCount} newer chats`, async () => {
|
|
const newer = Array.from({ length: newerCount }, (_, index) => ({
|
|
...structuredClone(SOURCE_SESSION), id: `history-${index}`, updatedAt: 100 + index,
|
|
}));
|
|
const { dom, container, root, capture } = await setupAiState([...newer, SOURCE_SESSION]);
|
|
let branchId: string | undefined;
|
|
await act(async () => {
|
|
branchId = (await capture.ai!.undoLastTurnInSession(SOURCE_SESSION.id))?.sessionId;
|
|
});
|
|
assert.ok(branchId);
|
|
// Issue #4123: the persisted set lives in IndexedDB (count cap 1000, so
|
|
// nothing is evicted at these sizes) — not in a 50-capped localStorage blob.
|
|
const saved: AISession[] = getPersistedAiSessionsSnapshot();
|
|
assert.equal(saved.length, newerCount + 2);
|
|
const original = saved.find(item => item.id === SOURCE_SESSION.id);
|
|
assert.ok(original, 'the original must survive updatedAt-based history pruning');
|
|
assert.deepEqual({ ...original, updatedAt: SOURCE_SESSION.updatedAt }, SOURCE_SESSION);
|
|
assert.deepEqual(saved.find(item => item.id === branchId)?.messages, SOURCE_SESSION.messages.slice(0, 2));
|
|
assert.ok(original.updatedAt >= newer.at(-1)!.updatedAt);
|
|
await act(async () => root.unmount());
|
|
container.remove();
|
|
// Seed the read-only legacy store with the persisted set (the stand-in
|
|
// for what the migration persists on a profile without IndexedDB).
|
|
dom.window.localStorage.setItem('netcatty_ai_sessions_v1', JSON.stringify(saved));
|
|
dom.window.close();
|
|
const reloaded = await setupAiState(saved);
|
|
assert.deepEqual(reloaded.capture.sessions.find(item => item.id === SOURCE_SESSION.id)?.messages, SOURCE_SESSION.messages);
|
|
assert.deepEqual(reloaded.capture.sessions.find(item => item.id === branchId)?.messages, SOURCE_SESSION.messages.slice(0, 2));
|
|
await act(async () => reloaded.root.unmount());
|
|
reloaded.container.remove(); reloaded.dom.window.close();
|
|
});
|
|
}
|
|
|
|
for (const viewingScope of [
|
|
{ type: 'terminal' as const, targetId: 'terminal-b' },
|
|
{ type: 'workspace' as const, targetId: 'workspace-b' },
|
|
]) {
|
|
test(`undo stays visible when a ${viewingScope.type} views a chat from a closed terminal`, async () => {
|
|
const source = {
|
|
...structuredClone(SOURCE_SESSION),
|
|
scope: { type: 'terminal' as const, targetId: 'terminal-closed', hostIds: ['host-a'] },
|
|
};
|
|
const { dom, container, root, capture } = await setupAiState([source]);
|
|
const sourceBefore = structuredClone(source);
|
|
let branchId: string | undefined;
|
|
await act(async () => {
|
|
capture.ai!.setActiveSessionId('terminal:terminal-b', source.id);
|
|
branchId = (await capture.ai!.undoLastTurnInSession(source.id, viewingScope))?.sessionId;
|
|
});
|
|
assert.ok(branchId);
|
|
const branch = capture.sessions.find(item => item.id === branchId)!;
|
|
assert.deepEqual(branch.messages, source.messages.slice(0, 2));
|
|
assert.deepEqual(branch.scope, { ...source.scope, ...viewingScope });
|
|
assert.equal(capture.activeSessionIdMap[`${viewingScope.type}:${viewingScope.targetId}`], branch.id);
|
|
// The real history predicate must still include the selected branch after
|
|
// the source terminal no longer belongs to the current workspace.
|
|
assert.equal(getSessionScopeMatchRank(
|
|
branch, viewingScope.type, viewingScope.targetId, ['host-a'],
|
|
new Set([branch.id]), new Set(['terminal-b']), new Set([source.id]),
|
|
), 3);
|
|
const original = capture.sessions.find(item => item.id === source.id)!;
|
|
assert.deepEqual({ ...original, updatedAt: sourceBefore.updatedAt }, sourceBefore);
|
|
const saved = getPersistedAiSessionsSnapshot();
|
|
assert.deepEqual(saved.find(item => item.id === branchId)?.scope, branch.scope);
|
|
await act(async () => root.unmount());
|
|
container.remove(); dom.window.close();
|
|
});
|
|
}
|