Files
openchamber/packages/ui/src/stores/useAgentMemoryStore.test.ts
T
Bohdan Triapitsyn 34e8a24b20 feat(knowledge): rebuild the project notes panel as Project knowledge (#2973)
The panel stored notes, todos and plans inside one shared JSON file that
six unrelated domains also wrote to, synchronised itself through window
CustomEvents, and could only read plans. It is now Project knowledge:
server-owned storage with explicit routes, a store with rollback, a
section sidebar, plans that open and edit in place, and search across
all of it.

Notes and plans the user pins travel with every message sent in that
project. Pinning is project state, not an attachment to one message, so
it holds until unpinned and the work status panel names what is riding
along and can detach it.

Agent memory is added alongside, in two scopes: what is true about the
user, and what is true about this codebase. The split is not cosmetic —
a wrong project fact costs one project and is noticed, while a wrong
global fact quietly shapes every session everywhere and the user has no
code to check it against. It stays separate from notes so an agent
mistake cannot land in what the user wrote. Sessions receive an index of
titles only; bodies are read on demand, because an index carrying full
text grows until it crowds out the conversation.

Deciding what a session must be told, and whether it has been told, now
lives on the server. The client owned it before, which meant sessions
started without a UI — scheduled tasks, sessions the agent dispatches —
received nothing at all, and a tab's record of what it had sent outlived
the conversation: after compaction the agent no longer held the block
while the tab went on believing it did. What was delivered is recorded
in the session's own metadata, and compaction restores it through the
runtime that already restores pinned messages, in the same turn.

Agent memory ships dark behind OPENCHAMBER_MEMORY_ENABLE: unset, there
is no tool, no routes, no session index, no settings row and no panel
tab. Absent rather than switched off, so nothing invites turning on a
feature that has not been announced. Pinned notes and plans are
unaffected and ship as normal.
2026-08-18 02:59:04 +03:00

214 lines
7.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
import { AgentMemoryDisabledError, type AgentMemoryEntry } from '@/lib/agentMemoryApi';
function entry(overrides: Partial<AgentMemoryEntry> = {}): AgentMemoryEntry {
return {
id: 'mem-1',
title: 'Uses bun',
body: 'Tests run with bun test.',
type: 'fact',
createdAt: 1,
updatedAt: 1,
...overrides,
};
}
interface MemoryReadResult {
global: AgentMemoryEntry[];
project: AgentMemoryEntry[];
globalFailed: boolean;
projectFailed: boolean;
}
/**
* Swappable implementations rather than mock helpers: each test states the one
* behaviour it needs.
*/
let readImpl: () => Promise<MemoryReadResult>;
let deleteImpl: () => Promise<void>;
let updateImpl: (memoryId: string, patch: Record<string, unknown>) => Promise<AgentMemoryEntry>;
let lastPatch: Record<string, unknown> | null = null;
mock.module('@/lib/agentMemoryApi', () => ({
AgentMemoryDisabledError,
fetchAgentMemory: () => readImpl(),
deleteAgentMemory: () => deleteImpl(),
updateAgentMemory: (
_scope: string,
_projectPath: string | null,
memoryId: string,
patch: Record<string, unknown>,
) => {
lastPatch = patch;
return updateImpl(memoryId, patch);
},
}));
const { useAgentMemoryStore } = await import('./useAgentMemoryStore');
beforeEach(() => {
useAgentMemoryStore.getState().reset();
readImpl = async () => ({
global: [entry({ id: 'g1', title: 'About user' })],
project: [entry({ id: 'p1', title: 'About project' })],
globalFailed: false,
projectFailed: false,
});
deleteImpl = async () => undefined;
updateImpl = async (memoryId, patch) => ({ ...entry({ id: memoryId }), ...patch });
lastPatch = null;
});
afterEach(() => {
useAgentMemoryStore.getState().reset();
});
describe('load', () => {
test('holds both scopes', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
const state = useAgentMemoryStore.getState();
expect(state.global.map((item) => item.id)).toEqual(['g1']);
expect(state.project.map((item) => item.id)).toEqual(['p1']);
expect(state.loaded).toBe(true);
});
test('a failed load keeps what was already held', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
readImpl = async () => { throw new Error('offline'); };
await useAgentMemoryStore.getState().load('/tmp/project');
const state = useAgentMemoryStore.getState();
// Blanking here would read as the agent having forgotten everything.
expect(state.global).toHaveLength(1);
expect(state.error).toBe('offline');
});
test('a disabled feature clears the lists rather than reporting an error', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
readImpl = async () => { throw new AgentMemoryDisabledError(); };
await useAgentMemoryStore.getState().load('/tmp/project');
const state = useAgentMemoryStore.getState();
expect(state.disabled).toBe(true);
expect(state.global).toHaveLength(0);
expect(state.error).toBeNull();
});
test('a partly failed read is recorded as failed, not as empty', async () => {
readImpl = async () => ({ global: [], project: [], globalFailed: true, projectFailed: false });
await useAgentMemoryStore.getState().load('/tmp/project');
expect(useAgentMemoryStore.getState().globalFailed).toBe(true);
});
});
describe('delete', () => {
test('removes the entry', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
const ok = await useAgentMemoryStore.getState().deleteEntry('project', 'p1');
expect(ok).toBe(true);
expect(useAgentMemoryStore.getState().project).toHaveLength(0);
});
test('restores the entry when the delete fails', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
deleteImpl = async () => { throw new Error('offline'); };
const ok = await useAgentMemoryStore.getState().deleteEntry('project', 'p1');
expect(ok).toBe(false);
expect(useAgentMemoryStore.getState().project).toHaveLength(1);
});
});
describe('user corrections', () => {
test('sends only what changed and adopts the saved entry', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
const ok = await useAgentMemoryStore.getState().saveEntry('project', 'p1', { body: 'Reworded.' });
expect(ok).toBe(true);
expect(lastPatch).toEqual({ body: 'Reworded.' });
expect(useAgentMemoryStore.getState().project[0].body).toBe('Reworded.');
});
test('a failed save leaves the entry as it was', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
updateImpl = async () => { throw new Error('offline'); };
const ok = await useAgentMemoryStore.getState().saveEntry('project', 'p1', { body: 'Reworded.' });
expect(ok).toBe(false);
expect(useAgentMemoryStore.getState().project[0].body).toBe('Tests run with bun test.');
expect(useAgentMemoryStore.getState().error).toBe('offline');
});
test('touches only the scope it was given', async () => {
await useAgentMemoryStore.getState().load('/tmp/project');
await useAgentMemoryStore.getState().saveEntry('project', 'p1', { title: 'Clearer' });
expect(useAgentMemoryStore.getState().global[0].title).toBe('About user');
});
});
describe('turning the feature off and on', () => {
test('a successful load clears the disabled flag', async () => {
readImpl = async () => { throw new AgentMemoryDisabledError(); };
await useAgentMemoryStore.getState().load('/tmp/project');
expect(useAgentMemoryStore.getState().disabled).toBe(true);
readImpl = async () => ({
global: [entry({ id: 'g1' })], project: [], globalFailed: false, projectFailed: false,
});
await useAgentMemoryStore.getState().load('/tmp/project');
expect(useAgentMemoryStore.getState().disabled).toBe(false);
});
test('refresh re-reads the store the last load used', async () => {
readImpl = async () => { throw new AgentMemoryDisabledError(); };
await useAgentMemoryStore.getState().load('/tmp/project');
let requestedPath: string | null = 'unset';
readImpl = async () => {
requestedPath = useAgentMemoryStore.getState().projectPath;
return { global: [], project: [], globalFailed: false, projectFailed: false };
};
await useAgentMemoryStore.getState().refresh();
// The disabled answer must not lose the path, or refresh reads the wrong store.
expect(requestedPath).toBe('/tmp/project');
});
test('a stale disabled answer cannot latch the feature off again', async () => {
// Re-enabling fires a load before the setting has finished being written,
// so the server truthfully answers "disabled" to a request that is already
// out of date by the time it lands.
const gate: { release?: () => void } = {};
readImpl = () => new Promise((_resolve, reject) => {
gate.release = () => reject(new AgentMemoryDisabledError());
});
const stale = useAgentMemoryStore.getState().load('/tmp/project');
readImpl = async () => ({
global: [entry({ id: 'g1' })], project: [], globalFailed: false, projectFailed: false,
});
await useAgentMemoryStore.getState().load('/tmp/project');
gate.release?.();
await stale;
expect(useAgentMemoryStore.getState().disabled).toBe(false);
expect(useAgentMemoryStore.getState().global).toHaveLength(1);
});
});