refactor(server): read workspace state, briefings and workspaces from the memory contract (P1.5)

- Workspace state, the "Workspace Now" block, the home briefing and the
  proactive handlers read the workspace's memory through server.memory:
  decisions are memories of kind 'decision', critical ones or ones worded
  as decisions; summaries list memories most important first. They no
  longer open workspace .mind files, so the builders are async and
  ProactiveContext carries `memory` instead of `getWorkspaceMindDb`.
- The home briefing counts extracted personal facts that are neither
  confirmed nor archived as awaiting review (memory-status.ts).
- Workspace routes count memories through the contract, seed template
  starter memories with remember() in the background (origin
  'workspace-template'), export memories through list(), and deleting a
  workspace forgets its memory scope and clears its awareness items before
  the directory is removed; the delete is refused if forgetting fails.
  agentState.releaseWorkspace() replaces closeWorkspaceMind for callers.
- The command palette searches personal memory and the active workspace's
  memory (UF-033).
- chat.ts drops the MindDB-flake diagnostics; the mind cache they guarded
  no longer serves chat turns.
This commit is contained in:
shumeiko.it
2026-10-24 12:50:07 +02:00
parent 4a1c4c4c8e
commit e9ed8c0765
23 changed files with 823 additions and 857 deletions

View File

@@ -2,43 +2,47 @@ import { describe, it, expect, vi } from 'vitest';
import os from 'node:os';
import fs from 'node:fs';
import path from 'node:path';
import { WaggleDB } from '@waggle/core';
import { createInMemoryMemoryStore } from '@waggle/memory-contract';
import { buildWorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
/**
* R6-006 path-traversal regression.
*
* `buildWorkspaceNowBlock` builds session-directory paths from an unvalidated
* `workspaceId` (getMindPath + dataDir/workspaces/<workspaceId>/sessions),
* `workspaceId` (dataDir/workspaces/<workspaceId>/sessions),
* enabling an existence/count probe outside the workspaces root. A guard
* (assertSafeSegment) now runs at the entry of the helper and THROWS a
* { statusCode: 400 } error before the value touches the filesystem; the
* calling route handlers propagate that to Fastify's default error handler.
*
* These are unit tests against the exported helper directly (a full route
* inject is impractical — the route needs heavy mind/wsManager decorators).
* inject is impractical — the route needs heavy memory/wsManager decorators).
*/
describe('R6-006 workspace-context path traversal', () => {
// A throwaway data dir; the guard fires before this is ever read for the
// malicious cases, and the valid case short-circuits on a null wsManager.get.
const tmpDataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wctx-traversal-'));
const waggleDb = new WaggleDB(':memory:');
const maliciousIds = ['../evil', '..%2f..', '../../etc', 'ws/../../secret', '..\\evil'];
for (const bad of maliciousIds) {
it(`rejects traversal workspaceId ${JSON.stringify(bad)} with a 400 error before any fs access`, () => {
it(`rejects traversal workspaceId ${JSON.stringify(bad)} with a 400 error before any fs access`, async () => {
const wsManager = {
get: vi.fn(() => ({ id: bad, name: 'evil' })),
getMindPath: vi.fn(() => path.join(tmpDataDir, 'mind.sqlite')),
};
const activateWorkspaceMind = vi.fn(() => true);
const memory = createInMemoryMemoryStore();
const count = vi.spyOn(memory, 'count');
let thrown: unknown;
try {
buildWorkspaceNowBlock({
await buildWorkspaceNowBlock({
dataDir: tmpDataDir,
workspaceId: bad,
wsManager,
activateWorkspaceMind,
waggleDb,
memory,
});
} catch (e) {
thrown = e;
@@ -46,32 +50,27 @@ describe('R6-006 workspace-context path traversal', () => {
expect(thrown).toBeInstanceOf(Error);
expect((thrown as { statusCode?: number }).statusCode).toBe(400);
// Guard short-circuits BEFORE the path-building dependencies are touched.
// Guard short-circuits BEFORE the workspace and its memory are touched.
expect(wsManager.get).not.toHaveBeenCalled();
expect(wsManager.getMindPath).not.toHaveBeenCalled();
expect(activateWorkspaceMind).not.toHaveBeenCalled();
expect(count).not.toHaveBeenCalled();
});
}
it('does NOT reject a normal valid workspaceId', () => {
it('does NOT reject a normal valid workspaceId', async () => {
const validId = 'workspace-123_AB';
const wsManager = {
// Return null so the helper cleanly returns null after the guard passes —
// proving the guard did not reject a valid segment.
get: vi.fn(() => null),
getMindPath: vi.fn(() => path.join(tmpDataDir, 'mind.sqlite')),
};
const activateWorkspaceMind = vi.fn(() => true);
let result: unknown;
expect(() => {
result = buildWorkspaceNowBlock({
dataDir: tmpDataDir,
workspaceId: validId,
wsManager,
activateWorkspaceMind,
});
}).not.toThrow();
const result = await buildWorkspaceNowBlock({
dataDir: tmpDataDir,
workspaceId: validId,
wsManager,
waggleDb,
memory: createInMemoryMemoryStore(),
});
// Guard passed, so execution proceeded to the (mocked) wsManager.get.
expect(wsManager.get).toHaveBeenCalledWith(validId);