Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

19.2 KB
Raw Blame
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
import { createGate, isAllowed, SESSION_COOKIE, WAKE_STARTED_PARAM, WORKER_START_GIVE_UP_SECONDS, WORKER_STARTING_RELOAD_SECONDS } from '../src/gate.js';
import { signToken, verifyToken } from '../src/token.js';

const SECRET = 'x'.repeat(40);
const OWNER = 'user-owner';
const PUBLIC_ORIGIN = 'https://viewer.example';
const SIGN_IN = 'https://cmem.example/work';
const now = () => Math.floor(Date.now() / 1000);

interface Seen { method: string; path: string; headers: Record<string, string>; body: string }
let seen: Seen[] = [];
let upstream: ReturnType<typeof Bun.serve>;

beforeAll(() => {
  upstream = Bun.serve({
    port: 0,
    hostname: '127.0.0.1',
    async fetch(request) {
      const url = new URL(request.url);
      const headers: Record<string, string> = {};
      request.headers.forEach((value, name) => { headers[name] = value; });
      seen.push({ method: request.method, path: `${url.pathname}${url.search}`, headers, body: await request.text() });
      if (url.pathname === '/api/health') return Response.json({ status: 'ok' });
      if (url.pathname === '/api/work-view/events') {
        return new Response(new ReadableStream({
          start(controller) {
            controller.enqueue(new TextEncoder().encode('event: heartbeat\ndata: {}\n\n'));
            controller.close();
          },
        }), { headers: { 'Content-Type': 'text/event-stream' } });
      }
      return new Response(JSON.stringify({ ok: true }), {
        headers: { 'Content-Type': 'application/json', 'Set-Cookie': 'worker=leak', 'X-Upstream': 'kept' },
      });
    },
  });
});

afterAll(() => upstream.stop(true));

function gate() {
  return createGate({ ownerUserId: OWNER, secret: SECRET, upstreamOrigin: `http://127.0.0.1:${upstream.port}`, publicOrigin: PUBLIC_ORIGIN, signInUrl: SIGN_IN });
}

function sessionCookie(sub = OWNER, exp = now() + 3600) {
  return `${SESSION_COOKIE}=${signToken(SECRET, { sub, aud: 'session', exp })}`;
}

describe('tokens', () => {
  it('round-trips and rejects tampering, another audience, another secret and expiry', () => {
    const token = signToken(SECRET, { sub: OWNER, aud: 'link', exp: now() + 60 });
    expect(verifyToken(SECRET, token, 'link')?.sub).toBe(OWNER);
    expect(verifyToken(SECRET, token, 'session')).toBeNull();
    expect(verifyToken('y'.repeat(40), token, 'link')).toBeNull();
    const [v, payload, sig] = token.split('.');
    const forged = Buffer.from(JSON.stringify({ sub: 'someone-else', aud: 'link', exp: now() + 60 })).toString('base64url');
    expect(verifyToken(SECRET, `${v}.${forged}.${sig}`, 'link')).toBeNull();
    expect(verifyToken(SECRET, `${v}.${payload}`, 'link')).toBeNull();
    expect(verifyToken(SECRET, signToken(SECRET, { sub: OWNER, aud: 'link', exp: now() - 1 }), 'link')).toBeNull();
  });
});

describe('token interop', () => {
  // The same vector is in claude-mem-pro scripts/test-viewer-link.ts: cmem.ai's links must verify here.
  it('verifies the link cmem.ai mints for the same claims', () => {
    expect(signToken(SECRET, { sub: 'user-1', aud: 'link', exp: 2_000_000_000 })).toBe('v1.eyJzdWIiOiJ1c2VyLTEiLCJhdWQiOiJsaW5rIiwiZXhwIjoyMDAwMDAwMDAwfQ.61EKU4CPNr1riCae6rUoImSRPkc1jYS1v7jZhrb0E6E');
    expect(verifyToken(SECRET, 'v1.eyJzdWIiOiJ1c2VyLTEiLCJhdWQiOiJsaW5rIiwiZXhwIjoyMDAwMDAwMDAwfQ.61EKU4CPNr1riCae6rUoImSRPkc1jYS1v7jZhrb0E6E', 'link', 1_900_000_000)?.sub).toBe('user-1');
  });
});

describe('policy', () => {
  it('reads pass except machine configuration; only My Work saves may write', () => {
    expect(isAllowed('GET', '/')).toBe(true);
    expect(isAllowed('GET', '/api/work-view')).toBe(true);
    expect(isAllowed('GET', '/viewer')).toBe(true);
    expect(isAllowed('GET', '/api/settings')).toBe(false);
    expect(isAllowed('GET', '/api/settings/dependency-health')).toBe(false);
    expect(isAllowed('GET', '/api/logs')).toBe(false);
    expect(isAllowed('GET', '/api/setup/state')).toBe(false);
    expect(isAllowed('GET', '/restart')).toBe(false);
    for (const path of ['inventory', 'jobs', 'jobs/some-job', 'conflicts']) {
      expect(isAllowed('GET', `/api/sync/recovery/${path}`)).toBe(false);
    }
    expect(isAllowed('POST', '/api/work-view/review')).toBe(true);
    expect(isAllowed('POST', '/api/work-view/suggestions')).toBe(true);
    expect(isAllowed('POST', '/api/work-view/focus')).toBe(true);
    expect(isAllowed('POST', '/api/work-view/rules/load-in-claude-code')).toBe(false);
    expect(isAllowed('POST', '/api/settings')).toBe(false);
    expect(isAllowed('POST', '/api/admin/shutdown')).toBe(false);
    expect(isAllowed('DELETE', '/api/observation/1')).toBe(false);
    // The worker's router ignores case and decodes escapes; the policy reads the path the same way.
    expect(isAllowed('GET', '/API/SETTINGS')).toBe(false);
    expect(isAllowed('GET', '/Api/Logs')).toBe(false);
    expect(isAllowed('GET', '/api/%73ettings')).toBe(false);
    expect(isAllowed('GET', '/api/%zz')).toBe(false);
    expect(isAllowed('GET', '//127.0.0.1:37777/api/settings')).toBe(false);
    expect(isAllowed('GET', '/api/work-view/%2e%2e/settings')).toBe(false);
    expect(isAllowed('POST', '/API/WORK-VIEW/REVIEW')).toBe(true);
    for (const path of ['/api/work-state', '/api/work-view/shared-ledger', '/api/work-view/shared-state', '/api/work-view/file/work-state/1/0', '/api/work-view/deliverables']) {
      expect(isAllowed('POST', path)).toBe(false);
    }
  });
});

describe('gate', () => {
  it('sends a page visit without a session to cmem.ai to sign in, and an API call gets 401', async () => {
    const page = await gate()(new Request(`${PUBLIC_ORIGIN}/`, { headers: { Accept: 'text/html' } }));
    expect(page.status).toBe(302);
    expect(page.headers.get('Location')).toBe(SIGN_IN);
    const api = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view`));
    expect(api.status).toBe(401);
    expect(seen.filter(request => request.path === '/api/work-view')).toHaveLength(0);
  });

  it("trades the owner's link for a session cookie, and refuses anyone else's", async () => {
    const link = signToken(SECRET, { sub: OWNER, aud: 'link', exp: now() + 120 });
    const response = await gate()(new Request(`${PUBLIC_ORIGIN}/auth?t=${link}`));
    expect(response.status).toBe(303);
    expect(response.headers.get('Location')).toBe('/');
    const cookie = response.headers.get('Set-Cookie') ?? '';
    expect(cookie).toContain(`${SESSION_COOKIE}=`);
    expect(cookie).toContain('HttpOnly');
    expect(cookie).toContain('Secure');
    expect(cookie).toContain('SameSite=Lax');
    const session = cookie.split(';')[0].split('=').slice(1).join('=');
    expect(verifyToken(SECRET, session, 'session')?.sub).toBe(OWNER);

    const stranger = signToken(SECRET, { sub: 'someone-else', aud: 'link', exp: now() + 120 });
    expect((await gate()(new Request(`${PUBLIC_ORIGIN}/auth?t=${stranger}`))).status).toBe(403);
    // A session token cannot be used as a link.
    expect((await gate()(new Request(`${PUBLIC_ORIGIN}/auth?t=${session}`))).status).toBe(403);
  });

  it('opens one session per link', async () => {
    const g = gate();
    const link = signToken(SECRET, { sub: OWNER, aud: 'link', exp: now() + 120 });
    expect((await g(new Request(`${PUBLIC_ORIGIN}/auth?t=${link}`))).status).toBe(303);
    expect((await g(new Request(`${PUBLIC_ORIGIN}/auth?t=${link}`))).status).toBe(403);
  });

  it("refuses another user's session", async () => {
    const response = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view`, { headers: { Cookie: sessionCookie('someone-else') } }));
    expect(response.status).toBe(401);
  });

  it('forwards a read as a loopback request: worker Host, no proxy headers, no cookies', async () => {
    seen = [];
    const response = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view?x=1`, {
      headers: {
        Cookie: `${sessionCookie()}; other=1`,
        'X-Forwarded-For': '203.0.113.9',
        'X-Forwarded-Host': 'viewer.example',
        Forwarded: 'for=203.0.113.9',
        'X-Real-IP': '203.0.113.9',
        'Fly-Client-IP': '203.0.113.9',
        Via: '1.1 fly.io',
        Accept: 'application/json',
      },
    }));
    expect(response.status).toBe(200);
    expect(response.headers.get('X-Upstream')).toBe('kept');
    expect(response.headers.get('Set-Cookie')).toBeNull();
    expect(response.headers.get('X-Frame-Options')).toBe('DENY');
    expect(seen).toHaveLength(1);
    const forwarded = seen[0];
    expect(forwarded.path).toBe('/api/work-view?x=1');
    expect(forwarded.headers.host).toBe(`127.0.0.1:${upstream.port}`);
    expect(forwarded.headers.accept).toBe('application/json');
    for (const name of ['cookie', 'x-forwarded-for', 'x-forwarded-host', 'forwarded', 'x-real-ip', 'fly-client-ip', 'via', 'origin']) {
      expect(forwarded.headers[name]).toBeUndefined();
    }
  });

  it('blocks machine configuration even for the owner', async () => {
    seen = [];
    const response = await gate()(new Request(`${PUBLIC_ORIGIN}/api/settings`, { headers: { Cookie: sessionCookie() } }));
    expect(response.status).toBe(403);
    expect(seen).toHaveLength(0);
  });

  it('passes a My Work save from its own page with a loopback Origin, and refuses one from anywhere else', async () => {
    seen = [];
    const body = JSON.stringify({ requestId: 'r1', expectedFingerprint: 'a'.repeat(64), checked: true });
    const ok = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view/review`, {
      method: 'POST',
      headers: { Cookie: sessionCookie(), Origin: PUBLIC_ORIGIN, 'Sec-Fetch-Site': 'same-origin', 'Content-Type': 'application/json' },
      body,
    }));
    expect(ok.status).toBe(200);
    expect(seen).toHaveLength(1);
    expect(seen[0].method).toBe('POST');
    expect(seen[0].headers.origin).toBe(`http://127.0.0.1:${upstream.port}`);
    expect(seen[0].headers['content-type']).toBe('application/json');
    expect(seen[0].body).toBe(body);

    const foreign = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view/review`, {
      method: 'POST',
      headers: { Cookie: sessionCookie(), Origin: 'https://evil.example', 'Content-Type': 'application/json' },
      body,
    }));
    expect(foreign.status).toBe(403);
    const noOrigin = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view/review`, {
      method: 'POST',
      headers: { Cookie: sessionCookie(), 'Content-Type': 'application/json' },
      body,
    }));
    expect(noOrigin.status).toBe(403);
    expect(seen).toHaveLength(1);
  });

  it.each([
    ['action', { action: 'seen', cardId: 'card', expectedReviewId: null }],
    ['import', { cards: [{ key: 'original' }] }],
  ] as const)('forwards an owner’s Review Stack %s with its retry key', async (operation, input) => {
    seen = [];
    const path = `/api/work-view/review-stack/${operation}`;
    const key = crypto.randomUUID();
    const body = JSON.stringify(input);
    const post = (cookie: string, origin: string) => gate()(new Request(PUBLIC_ORIGIN + path, {
      method: 'POST', headers: { Cookie: cookie, Origin: origin, 'Content-Type': 'application/json', 'Idempotency-Key': key }, body,
    }));
    expect((await post(sessionCookie('another-owner'), PUBLIC_ORIGIN)).status).toBe(401);
    expect((await post(sessionCookie(), 'https://other.example')).status).toBe(403);
    expect(seen).toHaveLength(0);
    expect((await post(sessionCookie(), PUBLIC_ORIGIN)).status).toBe(200);
    expect(seen).toHaveLength(1);
    expect(seen[0]).toMatchObject({ path, method: 'POST', body, headers: {
      origin: `http://127.0.0.1:${upstream.port}`, 'idempotency-key': key,
    } });
  });

  it('streams the live feed through', async () => {
    const response = await gate()(new Request(`${PUBLIC_ORIGIN}/api/work-view/events`, { headers: { Cookie: sessionCookie() } }));
    expect(response.headers.get('Content-Type')).toBe('text/event-stream');
    expect(await response.text()).toContain('event: heartbeat');
  });

  it('reports health from the worker without a session', async () => {
    const response = await gate()(new Request(`${PUBLIC_ORIGIN}/health`));
    expect(response.status).toBe(200);
    expect(await response.json()).toEqual({ ok: true, worker: 'ready' });
  });

  it('stays healthy while the worker is too busy to answer', async () => {
    const slow: typeof fetch = (_input, init) => new Promise((_resolve, reject) => {
      init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
    });
    const busy = createGate({ ownerUserId: OWNER, secret: SECRET, upstreamOrigin: 'http://127.0.0.1:37777', publicOrigin: PUBLIC_ORIGIN, signInUrl: SIGN_IN, fetchImpl: slow });
    const started = Date.now();
    const response = await busy(new Request(`${PUBLIC_ORIGIN}/health`));
    expect(response.status).toBe(200);
    expect(await response.json()).toEqual({ ok: true, worker: 'busy' });
    expect(Date.now() - started).toBeLessThan(3000);
  });

  it('says the viewer is starting while the worker is down', async () => {
    const down = createGate({ ownerUserId: OWNER, secret: SECRET, upstreamOrigin: 'http://127.0.0.1:1', publicOrigin: PUBLIC_ORIGIN, signInUrl: SIGN_IN });
    const health = await down(new Request(`${PUBLIC_ORIGIN}/health`));
    expect(health.status).toBe(200);
    expect(await health.json()).toEqual({ ok: true, worker: 'down' });
    const page = await down(new Request(`${PUBLIC_ORIGIN}/`, { headers: { Cookie: sessionCookie(), Accept: 'text/html' } }));
    expect(page.status).toBe(503);
    expect(await page.text()).toContain('starting');
  });

  it('a full-page wake reloads itself until the worker answers; JSON callers keep the JSON 503', async () => {
    // A port nothing listens on yet: the worker of a machine that has just woken.
    const probe = Bun.serve({ hostname: '127.0.0.1', port: 0, fetch: () => new Response() });
    const workerPort = probe.port;
    probe.stop(true);
    const waking = createGate({ ownerUserId: OWNER, secret: SECRET, upstreamOrigin: `http://127.0.0.1:${workerPort}`, publicOrigin: PUBLIC_ORIGIN, signInUrl: SIGN_IN });
    const navigation = () => new Request(`${PUBLIC_ORIGIN}/`, { headers: { Cookie: sessionCookie(), Accept: 'text/html,application/xhtml+xml' } });

    const starting = await waking(navigation());
    expect(starting.status).toBe(503);
    expect(starting.headers.get('cache-control')).toBe('no-store');
    const html = await starting.text();
    // The reload carries when this wake started, so the count survives each reload.
    const refresh = html.match(/<meta http-equiv="refresh" content="(\d+);url=([^"]+)">/);
    expect(html).toContain(`http-equiv="refresh" content="${WORKER_STARTING_RELOAD_SECONDS};url=/?${WAKE_STARTED_PARAM}=`);
    expect(refresh).not.toBeNull();
    expect(html).not.toContain('<script');
    const reloadPath = refresh![2];

    const api = await waking(new Request(`${PUBLIC_ORIGIN}/api/work-view`, { headers: { Cookie: sessionCookie(), Accept: 'application/json' } }));
    expect(api.status).toBe(503);
    expect(api.headers.get('content-type')).toBe('application/json');
    expect(await api.json()).toEqual({ error: 'Your viewer is starting. Try again in a moment.' });

    // The worker binds its port. The reload the refresh sends lands on the clean address, then the page.
    const worker = Bun.serve({ hostname: '127.0.0.1', port: workerPort, fetch: () => new Response('<!doctype html><title>My Work</title>', { headers: { 'Content-Type': 'text/html' } }) });
    try {
      const reloaded = await waking(new Request(`${PUBLIC_ORIGIN}${reloadPath}`, { headers: { Cookie: sessionCookie(), Accept: 'text/html' } }));
      expect(reloaded.status).toBe(303);
      expect(reloaded.headers.get('location')).toBe('/');
      const landed = await waking(navigation());
      expect(landed.status).toBe(200);
      const body = await landed.text();
      expect(body).toContain('My Work');
      expect(body).not.toContain('http-equiv="refresh"');
    } finally {
      worker.stop(true);
    }
  });

  it('keeps reloading inside the start deadline and stops with a failure page past it', async () => {
    const startedSeconds = 1_900_000_000;
    let nowMs = startedSeconds * 1000;
    const down = createGate({ ownerUserId: OWNER, secret: SECRET, upstreamOrigin: 'http://127.0.0.1:1', publicOrigin: PUBLIC_ORIGIN, signInUrl: SIGN_IN, now: () => nowMs });
    const cookie = `${SESSION_COOKIE}=${signToken(SECRET, { sub: OWNER, aud: 'session', exp: startedSeconds + 3600 })}`;
    const reload = (path: string) => down(new Request(`${PUBLIC_ORIGIN}${path}`, { headers: { Cookie: cookie, Accept: 'text/html' } }));
    expect(WORKER_START_GIVE_UP_SECONDS).toBe(120);

    // Other query parameters are kept; the marker keeps the first attempt's time.
    nowMs = (startedSeconds + WORKER_START_GIVE_UP_SECONDS - 1) * 1000;
    const inside = await reload(`/viewer?tab=work&${WAKE_STARTED_PARAM}=${startedSeconds}`);
    expect(inside.status).toBe(503);
    const insideHtml = await inside.text();
    expect(insideHtml).toContain('Your viewer is starting');
    expect(insideHtml).toContain(`http-equiv="refresh" content="${WORKER_STARTING_RELOAD_SECONDS};url=/viewer?tab=work&amp;${WAKE_STARTED_PARAM}=${startedSeconds}"`);

    nowMs = (startedSeconds + WORKER_START_GIVE_UP_SECONDS) * 1000;
    const past = await reload(`/viewer?tab=work&${WAKE_STARTED_PARAM}=${startedSeconds}`);
    expect(past.status).toBe(503);
    const pastHtml = await past.text();
    expect(pastHtml).toContain('Your viewer could not start');
    expect(pastHtml).not.toContain('http-equiv="refresh"');
    expect(pastHtml).toContain(`<a href="${SIGN_IN}">`);

    // A marker that cannot be the gate's own (unreadable, or in the future) starts a new count.
    for (const marker of ['abc', String(startedSeconds + 10_000)]) {
      const fresh = await (await reload(`/?${WAKE_STARTED_PARAM}=${marker}`)).text();
      expect(fresh).toContain(`url=/?${WAKE_STARTED_PARAM}=${startedSeconds + WORKER_START_GIVE_UP_SECONDS}"`);
    }
  });

  it('logs why the worker is unreachable once per kind, not on every reload', async () => {
    const logged: string[] = [];
    const original = console.error;
    console.error = (...parts: unknown[]) => { logged.push(parts.join(' ')); };
    try {
      let failWith: Error = Object.assign(new Error('Unable to connect'), { code: 'ConnectionRefused' });
      let workerUp = false;
      const flaky: typeof fetch = async () => {
        if (workerUp) return new Response('ok');
        throw failWith;
      };
      const gateWithFlakyWorker = createGate({ ownerUserId: OWNER, secret: SECRET, upstreamOrigin: 'http://127.0.0.1:37777', publicOrigin: PUBLIC_ORIGIN, signInUrl: SIGN_IN, fetchImpl: flaky });
      const reload = () => gateWithFlakyWorker(new Request(`${PUBLIC_ORIGIN}/`, { headers: { Cookie: sessionCookie(), Accept: 'text/html' } }));
      for (let i = 0; i < 5; i++) await reload();
      expect(logged).toHaveLength(1);
      expect(logged[0]).toContain('worker unreachable (ConnectionRefused): Unable to connect');
      failWith = Object.assign(new Error('socket closed'), { code: 'ECONNRESET' });
      await reload();
      await reload();
      expect(logged).toHaveLength(2);
      expect(logged[1]).toContain('(ECONNRESET)');
      // Once the worker answers, the next outage is logged again.
      workerUp = true;
      await reload();
      workerUp = false;
      await reload();
      expect(logged).toHaveLength(3);
    } finally {
      console.error = original;
    }
  });
});