Files
openchamber/packages/web/server/lib/agent-memory/project-resolution.test.js
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

86 lines
3.1 KiB
JavaScript

import { describe, expect, test } from 'bun:test';
import { createMemoryProjectResolver } from './project-resolution.js';
import { createProjectIdFromPath } from '../projects/project-id.js';
const PROJECT = '/Users/x/projects/openchamber';
const WORKTREE = '/Users/x/.local/share/opencode/worktree/abc/jammy-koala';
const createResolver = (overrides = {}) => createMemoryProjectResolver({
listProjectPaths: async () => [PROJECT],
resolvePrimaryWorktreeRoot: async (directory) => (
directory === WORKTREE ? { root: PROJECT } : { root: directory }
),
...overrides,
});
describe('resolving a session directory to its project', () => {
test('a worktree resolves to the project it belongs to', async () => {
const resolve = createResolver();
// The bug this exists for: keyed by its own path, a worktree wrote memory
// into a project the panel never reads.
expect(await resolve(WORKTREE)).toBe(createProjectIdFromPath(PROJECT));
});
test('the project directory resolves to itself', async () => {
const resolve = createResolver();
expect(await resolve(PROJECT)).toBe(createProjectIdFromPath(PROJECT));
});
test('every worktree of one repository shares a store', async () => {
const second = '/Users/x/.local/share/opencode/worktree/abc/other';
const resolve = createResolver({
resolvePrimaryWorktreeRoot: async () => ({ root: PROJECT }),
});
expect(await resolve(WORKTREE)).toBe(await resolve(second));
});
test('a worktree registered as a project in its own right keeps its own store', async () => {
// The user's explicit choice wins over the git topology.
const resolve = createResolver({ listProjectPaths: async () => [PROJECT, WORKTREE] });
expect(await resolve(WORKTREE)).toBe(createProjectIdFromPath(WORKTREE));
});
test('a directory outside any repository keys by itself', async () => {
const resolve = createResolver();
expect(await resolve('/tmp/loose')).toBe(createProjectIdFromPath('/tmp/loose'));
});
test('no directory resolves to nothing rather than to some default project', async () => {
const resolve = createResolver();
expect(await resolve('')).toBe('');
expect(await resolve(null)).toBe('');
});
test('trailing slashes and relative segments do not fork the store', async () => {
const resolve = createResolver();
expect(await resolve(`${PROJECT}/`)).toBe(createProjectIdFromPath(PROJECT));
expect(await resolve(`${PROJECT}/packages/..`)).toBe(createProjectIdFromPath(PROJECT));
});
});
describe('when something is unavailable', () => {
test('an unreadable project list still converges worktrees on the repository', async () => {
const resolve = createResolver({
listProjectPaths: async () => { throw new Error('settings unreadable'); },
});
expect(await resolve(WORKTREE)).toBe(createProjectIdFromPath(PROJECT));
});
test('git being unavailable falls back to the directory instead of failing', async () => {
const resolve = createResolver({
resolvePrimaryWorktreeRoot: async () => { throw new Error('git missing'); },
});
expect(await resolve(WORKTREE)).toBe(createProjectIdFromPath(WORKTREE));
});
});