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

20.5 KB
Raw Blame
import { describe, it, expect, beforeEach, afterEach, afterAll, spyOn } from 'bun:test';
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import type { ActiveSession } from '../../src/services/worker-types.js';
import { logger } from '../../src/utils/logger.js';
import {
  QUOTA_EXHAUSTED_RECHECK_COOLDOWN_MS,
  RATE_LIMIT_RECHECK_COOLDOWN_MS,
  resolveQuotaCooldownMs,
  clearQuotaCooldown,
  getQuotaCooldown,
  recordQuotaExhausted,
  resetQuotaCooldownsForTesting,
} from '../../src/shared/quota-cooldown.js';
import { resetDependencyStatusesForTesting } from '../../src/shared/dependency-health.js';
import { claudeQuotaStandIn, hasClaudeSubscriptionLogin, setClaudeFallbackReadinessForTesting } from '../../src/shared/claude-fallback-readiness.js';
import { SessionManager } from '../../src/services/worker/SessionManager.js';
import type { DatabaseManager } from '../../src/services/worker/DatabaseManager.js';
import { ClassifiedProviderError } from '../../src/services/worker/provider-errors.js';
import { readObserverHealth, renderObserverQuotaCooldownNotice } from '../../src/shared/observer-health.js';
import {
  quotaServingProvider,
  resetQuotaFallbackStateForTesting,
  selectProviderForGenerator,
} from '../../src/services/worker/provider-dispatch.js';

const { SessionRoutes } = await import('../../src/services/worker/http/routes/SessionRoutes.js');

/**
 * CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD: while the primary Claude credential
 * is held for quota, the observer runs on the other one ('subscription' for a
 * spent API key, 'api-key' for a spent or guard-paused login), and goes back
 * once the primary's probe succeeds.
 *
 * The login is a real `.credentials.json` in a temp profile dir
 * (CLAUDE_MEM_CLAUDE_CONFIG_DIR), the same file hasClaudeSubscriptionLogin
 * reads; the fallback key is a real .env (CLAUDE_MEM_ENV_FILE).
 */
const ENV_KEYS = [
  'CLAUDE_MEM_PROVIDER',
  'CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER',
  'CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD',
  'CLAUDE_MEM_CLAUDE_CONFIG_DIR',
  'CLAUDE_MEM_ENV_FILE',
  'CLAUDE_MEM_GEMINI_API_KEY',
  'CLAUDE_MEM_TIER_ROUTING_ENABLED',
  'CLAUDE_CODE_OAUTH_TOKEN',
] as const;

const SPENT_KEY = 'Your credit balance is too low to access the Anthropic API.';
const SPENT_LOGIN = 'Memory capture paused by the Claude usage reserve guard.';

let savedEnv: Record<string, string | undefined>;
let previousClaudeReadiness: (() => boolean) | null;
let spies: Array<ReturnType<typeof spyOn>>;
let tempDir: string;

function signIn(): void {
  writeFileSync(join(tempDir, '.credentials.json'), '{}');
}

function setFallbackKey(): void {
  writeFileSync(join(tempDir, '.env'), 'ANTHROPIC_FALLBACK_API_KEY=sk-ant-fallback\n', { mode: 0o600 });
}

function setUp(): void {
  savedEnv = {};
  for (const key of ENV_KEYS) {
    savedEnv[key] = process.env[key];
    delete process.env[key];
  }
  tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-credential-fallback-'));
  Object.assign(process.env, {
    CLAUDE_MEM_PROVIDER: 'claude',
    CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD: 'subscription',
    CLAUDE_MEM_CLAUDE_CONFIG_DIR: tempDir,
    CLAUDE_MEM_ENV_FILE: join(tempDir, '.env'),
    CLAUDE_MEM_TIER_ROUTING_ENABLED: 'false',
  });
  resetQuotaCooldownsForTesting();
  resetDependencyStatusesForTesting();
  resetQuotaFallbackStateForTesting();
  previousClaudeReadiness = setClaudeFallbackReadinessForTesting(() => true);
  spies = [
    spyOn(logger, 'info').mockImplementation(() => {}),
    spyOn(logger, 'warn').mockImplementation(() => {}),
    spyOn(logger, 'debug').mockImplementation(() => {}),
    spyOn(logger, 'error').mockImplementation(() => {}),
  ];
}

function tearDown(): void {
  spies.forEach(spy => spy.mockRestore());
  setClaudeFallbackReadinessForTesting(previousClaudeReadiness);
  resetQuotaCooldownsForTesting();
  resetQuotaFallbackStateForTesting();
  for (const key of ENV_KEYS) {
    if (savedEnv[key] === undefined) delete process.env[key];
    else process.env[key] = savedEnv[key];
  }
  rmSync(tempDir, { recursive: true, force: true });
}

function makeSession(): ActiveSession {
  return {
    sessionDbId: 92,
    contentSessionId: 'content-92',
    memorySessionId: 'memory-92',
    project: 'project',
    platformSource: 'claude',
    userPrompt: 'prompt',
    abortController: new AbortController(),
    generatorPromise: null,
    lastPromptNumber: 3,
    startTime: Date.now(),
    cumulativeInputTokens: 0,
    cumulativeOutputTokens: 0,
    earliestPendingTimestamp: null,
    claimedMessageIds: [],
    conversationHistory: [],
    currentProvider: null,
    consecutiveRestarts: 0,
    consecutiveInvalidOutputs: 0,
    consecutiveContextOverflows: 0,
    lastGeneratorActivity: Date.now(),
  };
}

function hang(): Promise<void> {
  return new Promise<void>(() => {});
}

function nextTick(): Promise<void> {
  return new Promise(resolve => setTimeout(resolve, 5));
}

function makeSessionManager(): SessionManager {
  return new SessionManager({
    getSessionById: () => ({
      content_session_id: 'content-handback',
      project: 'proj',
      platform_source: 'claude',
      user_prompt: 'prompt',
      memory_session_id: null,
    }),
    getSessionStore: () => ({ getPromptNumberFromUserPrompts: () => 1 }),
    getChromaSync: () => undefined,
  } as unknown as DatabaseManager);
}

function buildRoutes(session: ActiveSession, claude: (s: ActiveSession) => Promise<void>) {
  let active: ActiveSession | undefined = session;
  let pending = 1;
  const sessionManager = {
    getSession: () => active,
    getMessageBuffer: () => ({ getPendingCount: () => pending, peekTypes: () => [] }),
    discardBufferedWork: () => { const discarded = pending; pending = 0; return discarded; },
    removeSessionImmediate: () => { active = undefined; },
    // The sweep's view: buffered work, no generator, and a pause time heals
    // (quota and auth are not; their work was discarded).
    getResumableSessionIds: () => (
      active && !active.generatorPromise && pending > 0
        && [null, undefined, 'rate_limit', 'overflow', 'provider_switch', 'response_stall'].includes(active.pausedReason)
        ? [active.sessionDbId]
        : []
    ),
  };
  const routes = new SessionRoutes(
    sessionManager as any,
    {} as any,
    { startSession: claude } as any,
    { startSession: async () => {} } as any,
    { startSession: async () => {} } as any,
    {} as any,
    {} as any,
    { finalizeSession: async () => {} } as any,
  );
  return Object.assign(routes, { pendingCount: () => pending });
}

/** Routes whose Claude agent records the credential override each start ran with. */
function recordingRoutes(session: ActiveSession) {
  const starts: Array<string | null | undefined> = [];
  const routes = buildRoutes(session, async (s) => { starts.push(s.claudeAuthOverride); await hang(); });
  return { routes, starts };
}

async function startOnce(session: ActiveSession) {
  const { routes, starts } = recordingRoutes(session);
  await routes.ensureGeneratorRunning(session.sessionDbId, 'observation');
  await nextTick();
  return starts;
}

describe('Claude credential fallback: admission', () => {
  beforeEach(setUp);
  afterEach(tearDown);
  afterAll(() => resetQuotaCooldownsForTesting());

  it("runs on the subscription login while the API key's breaker holds", async () => {
    signIn();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, 'api-key');

    expect(await startOnce(makeSession())).toEqual(['subscription']);
    // The key's breaker is untouched: it still decides when the key is re-probed.
    expect(getQuotaCooldown('claude')?.auth).toBe('api-key');
  });

  it("runs on the fallback API key while the login's breaker holds", async () => {
    process.env.CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD = 'api-key';
    setFallbackKey();
    recordQuotaExhausted('claude', SPENT_LOGIN, 'five_hour', undefined, undefined, 'subscription');

    expect(await startOnce(makeSession())).toEqual(['api-key']);
    expect(getQuotaCooldown('claude')?.auth).toBe('subscription');
  });

  it('goes back to the primary once its window elapses, taking the probe without the override', async () => {
    signIn();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, Date.now() - QUOTA_EXHAUSTED_RECHECK_COOLDOWN_MS - 1, undefined, 'api-key');
    const session = makeSession();
    session.claudeAuthOverride = 'subscription';

    expect(await startOnce(session)).toEqual([null]);
    expect(getQuotaCooldown('claude')?.probeInFlightSinceMs).not.toBeNull();
  });

  it.each([
    ['the setting is off', () => { process.env.CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD = ''; signIn(); }, 'api-key'],
    ['there is no Claude Code login', () => {}, 'api-key'],
    ['the breaker was not armed by the primary credential', () => { signIn(); }, undefined],
    ['the setting names the held credential itself', () => { signIn(); }, 'subscription'],
    ['there is no fallback API key', () => { process.env.CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD = 'api-key'; }, 'subscription'],
    ['a gateway is configured', () => {
      signIn();
      writeFileSync(join(tempDir, '.env'), 'ANTHROPIC_BASE_URL=https://gateway.example\nANTHROPIC_API_KEY=sk-gw\n');
    }, 'api-key'],
  ] as const)('starts nothing and skips the buffered work when %s', async (_label, arrange, auth) => {
    arrange();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, auth);
    const session = makeSession();
    const { routes, starts } = recordingRoutes(session);

    await routes.ensureGeneratorRunning(session.sessionDbId, 'observation');
    await nextTick();
    expect(starts).toEqual([]);
    // A spent allowance does not hold the work for the reset.
    expect(routes.pendingCount()).toBe(0);
    expect(session.pausedReason).toBe('quota');
  });

  it('the sweep runs buffered work on the stand-in while the primary is held', () => {
    signIn();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, 'api-key');
    const { routes } = recordingRoutes(makeSession());
    expect(routes.resumePendingSessions('periodic-resume')).toBe(1);
  });
});

describe('Claude credential fallback: order and booking', () => {
  beforeEach(setUp);
  afterEach(tearDown);
  afterAll(() => resetQuotaCooldownsForTesting());

  it.each([
    ['subscription', 'auth:observer_text'],
    ['subscription', 'auth:observer_org_policy'],
    ['subscription', 'auth:observer_verification_hold'],
    ['api-key', 'auth:auth_invalid'],
  ] as const)('discards the work of a refused %s fallback (%s) and probes the primary with fresh work', async (target, reason) => {
    process.env.CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD = target;
    if (target === 'subscription') signIn();
    else setFallbackKey();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined,
      target === 'subscription' ? 'api-key' : 'subscription');
    const sm = makeSessionManager();
    const session = sm.initializeSession(304, 'prompt', 1);
    sm.queueObservation(304, { tool_name: 'Read', tool_input: {}, tool_response: {}, prompt_number: 1, toolUseId: 'auth-recovery' });
    const starts: Array<string | null> = [];
    const routes = new SessionRoutes(
      sm, {} as any,
      { startSession: async (s: ActiveSession) => {
        starts.push(s.claudeAuthOverride ?? null);
        if (starts.length > 1) { await hang(); return; }
        s.abortReason = reason;
        s.abortController.abort();
        if (reason === 'auth:auth_invalid') {
          throw new ClassifiedProviderError('Invalid API key', { kind: 'auth_invalid', cause: null });
        }
      } } as any,
      {} as any, {} as any, {} as any, {} as any,
      { finalizeSession: async () => {} } as any,
    );

    await routes.ensureGeneratorRunning(304, 'observation');
    await session.generatorPromise;
    expect(starts).toEqual([target]);
    expect(session.fallbackFrom).toBeNull();
    expect(getQuotaCooldown('claude')?.cause).toBe('auth');
    // The refused batch is discarded; nothing is left for a later resume.
    expect(sm.getMessageBuffer().getPendingCount(304)).toBe(0);
    expect(routes.resumePendingSessions('periodic-resume')).toBe(0);

    // A fresh event while the refusal holds starts nothing and is skipped too.
    sm.queueObservation(304, { tool_name: 'Read', tool_input: {}, tool_response: {}, prompt_number: 1, toolUseId: 'while-held' });
    await routes.ensureGeneratorRunning(304, 'observation');
    expect(starts).toEqual([target]);
    expect(sm.getMessageBuffer().getPendingCount(304)).toBe(0);

    // Once the window elapses the sweep still has nothing to revive; the next
    // captured event takes the single probe on the primary.
    getQuotaCooldown('claude')!.armedAtMs -= QUOTA_EXHAUSTED_RECHECK_COOLDOWN_MS + 1;
    expect(routes.resumePendingSessions('periodic-resume')).toBe(0);
    sm.queueObservation(304, { tool_name: 'Read', tool_input: {}, tool_response: {}, prompt_number: 1, toolUseId: 'after-window' });
    await routes.ensureGeneratorRunning(304, 'observation');
    await nextTick();
    expect(starts).toEqual([target, null]);
    expect(getQuotaCooldown('claude')?.probeInFlightSinceMs).not.toBeNull();
  });

  it('tries the other credential before the provider fallback, and the provider fallback once both are held', () => {
    signIn();
    process.env.CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER = 'gemini';
    process.env.CLAUDE_MEM_GEMINI_API_KEY = 'gemini-key';
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, 'api-key');
    expect(selectProviderForGenerator().provider).toBe('claude');

    // The login's own run hit its limit: re-armed untagged, so both are spent.
    recordQuotaExhausted('claude', 'Weekly limit reached', 'weekly');
    expect(selectProviderForGenerator().provider).toBe('gemini');
  });

  it.each([
    ['an API-key run spending its allowance', 'api-key', null, 'quota:observer_text', 'api-key'],
    ['a login run paused by the quota guard', 'subscription', null, 'quota:five_hour:quota_guard', 'subscription'],
    ['a quota-fallback run', 'api-key', 'subscription', 'quota:observer_text', undefined],
  ] as const)('tags the breaker armed by %s', async (_label, observerAuth, override, abortReason, expectedAuth) => {
    const session = makeSession();
    // Quota prose or the guard: the run aborts without throwing.
    const routes = buildRoutes(session, async (s) => {
      s.observerAuth = observerAuth;
      s.claudeAuthOverride = override;
      s.abortReason = abortReason;
      s.abortController.abort();
    });

    await routes.ensureGeneratorRunning(session.sessionDbId, 'observation');
    await session.generatorPromise;
    await nextTick();
    clearTimeout(session.scheduledResumeTimer);

    expect(getQuotaCooldown('claude')?.window).toBe(abortReason.split(':')[1]);
    expect(getQuotaCooldown('claude')?.auth).toBe(expectedAuth);
  });

  it.each([
    ['no fallback configured', false],
    ['a provider fallback that can serve', true],
  ] as const)('books a 429 reply as the short rate-limit window, not the quota cooldown, with %s (#5428)', async (_label, withFallback) => {
    if (withFallback) {
      process.env.CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER = 'gemini';
      process.env.CLAUDE_MEM_GEMINI_API_KEY = 'gemini-key';
    }
    const session = makeSession();
    // What ResponseProcessor leaves for a 429 written as the reply: the batch
    // back in the queue and the run aborted without throwing.
    const routes = buildRoutes(session, async (s) => {
      s.abortReason = 'rate_limit:observer_text';
      s.abortController.abort();
    });

    await routes.ensureGeneratorRunning(session.sessionDbId, 'observation');
    await session.generatorPromise;
    await nextTick();
    clearTimeout(session.scheduledResumeTimer);

    expect(getQuotaCooldown('claude')?.window).toBe('rate_limit');
    expect(resolveQuotaCooldownMs(getQuotaCooldown('claude')?.window)).toBe(RATE_LIMIT_RECHECK_COOLDOWN_MS);
    // With a fallback that can serve, the work moves there at once (the pause clears).
    if (!withFallback) expect(session.pausedReason).toBe('rate_limit');
    expect(readObserverHealth()?.lastErrorKind).toBe('rate_limit');
  });

  it.each([
    ['subscription', 'api-key', () => {
      signIn();
      writeFileSync(join(tempDir, '.env'), 'ANTHROPIC_API_KEY=sk-ant-primary\n', { mode: 0o600 });
    }, 'claude-mem is using your Claude Code login to save memories'],
    ['api-key', 'subscription', () => setFallbackKey(), 'claude-mem is using your backup API key (ANTHROPIC_FALLBACK_API_KEY) to save memories'],
  ] as const)('tells the session start which credential memory runs on (%s), until both are spent', (target, held, arrange, notice) => {
    process.env.CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD = target;
    arrange();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, held);
    expect(claudeQuotaStandIn()).toBe(target);
    expect(quotaServingProvider('claude')).toBe('claude');
    expect(renderObserverQuotaCooldownNotice(readObserverHealth()!)).toContain(notice);

    recordQuotaExhausted('claude', 'Weekly limit reached', 'weekly');
    expect(claudeQuotaStandIn()).toBeNull();
    expect(quotaServingProvider('claude')).toBeNull();
  });
});

describe('Claude credential fallback: readiness', () => {
  beforeEach(setUp);
  afterEach(tearDown);
  afterAll(() => resetQuotaCooldownsForTesting());

  it('counts a headless login (CLAUDE_CODE_OAUTH_TOKEN, no login files) as a subscription that can stand in', () => {
    process.env.CLAUDE_CODE_OAUTH_TOKEN = 'oauth-from-env';
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, 'api-key');
    expect(claudeQuotaStandIn()).toBe('subscription');
  });

  it('counts a long-lived CLAUDE_CODE_OAUTH_TOKEN in ~/.claude-mem/.env (no login files) as a subscription login', () => {
    expect(hasClaudeSubscriptionLogin()).toBe(false);
    writeFileSync(join(tempDir, '.env'), 'CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-long-lived\n', { mode: 0o600 });
    expect(hasClaudeSubscriptionLogin()).toBe(true);
  });

});

describe('Claude credential fallback: a running stand-in hands back to the primary', () => {
  beforeEach(setUp);
  afterEach(tearDown);
  afterAll(() => resetQuotaCooldownsForTesting());

  /** A stand-in run on the subscription login, one batch answered, one more queued. */
  async function standInBetweenBatches(sessionDbId: number) {
    signIn();
    recordQuotaExhausted('claude', SPENT_KEY, undefined, undefined, undefined, 'api-key');
    const sm = makeSessionManager();
    const session = sm.initializeSession(sessionDbId, 'prompt', 1);
    for (const toolUseId of [`tu-${sessionDbId}-a`, `tu-${sessionDbId}-b`]) {
      sm.queueObservation(sessionDbId, { tool_name: 'Read', tool_input: {}, tool_response: {}, prompt_number: 1, toolUseId });
    }
    session.claudeAuthOverride = 'subscription';
    const iterator = sm.getMessageIterator(sessionDbId);
    expect((await iterator.next()).done).toBe(false);
    await sm.confirmClaimedMessages(sessionDbId);
    return { sm, session, iterator };
  }

  it('keeps the stand-in running while the primary is still held', async () => {
    const { session, iterator } = await standInBetweenBatches(301);
    const second = await iterator.next();
    expect(second.done).toBe(false);
    expect(session.abortReason ?? null).toBeNull();
    session.abortController.abort();
    await iterator.return?.(undefined);
  });

  it("ends at the next batch once another session's success clears the breaker, and the next run is on the primary", async () => {
    const { sm, session, iterator } = await standInBetweenBatches(302);
    clearQuotaCooldown('claude');

    const second = await iterator.next();
    expect(second.done).toBe(true);
    expect(session.abortReason).toBe('provider_switch:boundary');
    expect(sm.getMessageBuffer().getPendingCount(302)).toBe(1);

    expect(await startOnce(makeSession())).toEqual([null]);
  });

  it('ends at the next batch once the primary window elapses, and exactly one run probes the primary', async () => {
    const { sm, session, iterator } = await standInBetweenBatches(303);
    getQuotaCooldown('claude')!.armedAtMs -= QUOTA_EXHAUSTED_RECHECK_COOLDOWN_MS + 1;

    const second = await iterator.next();
    expect(second.done).toBe(true);
    expect(session.abortReason).toBe('provider_switch:boundary');
    expect(sm.getMessageBuffer().getPendingCount(303)).toBe(1);

    // The first admission takes the single probe on the primary; any other
    // session meanwhile re-enters the stand-in instead of a second probe.
    expect(await startOnce(makeSession())).toEqual([null]);
    expect(getQuotaCooldown('claude')?.probeInFlightSinceMs).not.toBeNull();
    expect(await startOnce(makeSession())).toEqual(['subscription']);
  });
});