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

4.4 KB
Raw Blame
import { afterEach, describe, expect, it } from 'bun:test';
import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import path from 'path';
import { validateWorkerPidFile, type ValidateWorkerPidStatus } from '../../src/supervisor/index.js';
import { captureProcessStartToken } from '../../src/supervisor/process-registry.js';

function makeTempDir(): string {
  const dir = path.join(tmpdir(), `claude-mem-index-${Date.now()}-${Math.random().toString(36).slice(2)}`);
  mkdirSync(dir, { recursive: true });
  return dir;
}

const tempDirs: string[] = [];

describe('validateWorkerPidFile', () => {
  afterEach(() => {
    while (tempDirs.length > 0) {
      const dir = tempDirs.pop();
      if (dir) {
        rmSync(dir, { recursive: true, force: true });
      }
    }
  });

  it('returns "missing" when PID file does not exist', () => {
    const tempDir = makeTempDir();
    tempDirs.push(tempDir);
    const pidFilePath = path.join(tempDir, 'worker.pid');

    const status = validateWorkerPidFile({ logAlive: false, pidFilePath });
    expect(status).toBe('missing');
  });

  it('returns "invalid" when PID file contains bad JSON', () => {
    const tempDir = makeTempDir();
    tempDirs.push(tempDir);
    const pidFilePath = path.join(tempDir, 'worker.pid');
    writeFileSync(pidFilePath, 'not-json!!!');

    const status = validateWorkerPidFile({ logAlive: false, pidFilePath });
    expect(status).toBe('invalid');
  });

  it('returns "stale" when PID file references a dead process', () => {
    const tempDir = makeTempDir();
    tempDirs.push(tempDir);
    const pidFilePath = path.join(tempDir, 'worker.pid');
    writeFileSync(pidFilePath, JSON.stringify({
      pid: 2147483647,
      port: 37777,
      startedAt: new Date().toISOString()
    }));

    const status = validateWorkerPidFile({ logAlive: false, pidFilePath });
    expect(status).toBe('stale');
  });

  it('returns "alive" when PID file references the current process', () => {
    const tempDir = makeTempDir();
    tempDirs.push(tempDir);
    const pidFilePath = path.join(tempDir, 'worker.pid');
    // With its start token, as every worker since v12.3.8 writes it. A
    // token-less record must also name worker-service.cjs on Linux (#4270),
    // which the test runner's command line does not.
    writeFileSync(pidFilePath, JSON.stringify({
      pid: process.pid,
      port: 37777,
      startedAt: new Date().toISOString(),
      startToken: captureProcessStartToken(process.pid) ?? undefined
    }));

    const status = validateWorkerPidFile({ logAlive: false, pidFilePath });
    expect(status).toBe('alive');
  });

  const tokenSupported = process.platform === 'linux' || process.platform === 'darwin';
  it.if(tokenSupported)('returns "stale" when startToken does not match the live PID (PID reused)', () => {
    const tempDir = makeTempDir();
    tempDirs.push(tempDir);
    const pidFilePath = path.join(tempDir, 'worker.pid');
    writeFileSync(pidFilePath, JSON.stringify({
      pid: process.pid,
      port: 37777,
      startedAt: new Date().toISOString(),
      startToken: 'token-from-a-different-incarnation'
    }));

    const status = validateWorkerPidFile({ logAlive: false, pidFilePath });
    expect(status).toBe('stale');
  });
});

describe('Supervisor assertCanSpawn behavior', () => {
  it('assertCanSpawn throws when stopPromise is active (shutdown in progress)', async () => {
    const { getSupervisor } = require('../../src/supervisor/index.js');
    const supervisor = getSupervisor();

    const stopping = supervisor.stop();
    try {
      expect(() => supervisor.assertCanSpawn('test')).toThrow(/shutting down/);
    } finally {
      await stopping;
    }
  });

  it('registerProcess and unregisterProcess delegate to the registry', () => {
    const { getSupervisor } = require('../../src/supervisor/index.js');
    const supervisor = getSupervisor();
    const registry = supervisor.getRegistry();

    const testId = `test-${Date.now()}`;
    supervisor.registerProcess(testId, {
      pid: process.pid,
      type: 'test',
      startedAt: new Date().toISOString()
    });

    const found = registry.getAll().find((r: { id: string }) => r.id === testId);
    expect(found).toBeDefined();
    expect(found?.type).toBe('test');

    supervisor.unregisterProcess(testId);
    const afterUnregister = registry.getAll().find((r: { id: string }) => r.id === testId);
    expect(afterUnregister).toBeUndefined();
  });
});