Server-beta: Postgres storage + independent runtime + BullMQ queue (Phases 1–3) (#2351)
* Add server beta runtime foundation * Address server beta review findings * Resolve server beta review comments * Tighten server beta review follow-ups * Harden server beta auth and search * Avoid unnecessary FTS rebuilds * Block scoped keys from creating projects * Release BullMQ claims best effort on close * Address server beta review blockers * Reset BullMQ claims best effort * Add Postgres observation storage foundation * feat(server-beta): add independent runtime service Introduce src/server/runtime/ as a self-contained server-beta runtime that owns its lifecycle, Postgres bootstrap, and HTTP boundary without depending on WorkerService. ServerBetaService wraps the existing Server class, exposes /healthz and /v1/info with runtime="server-beta", and persists state to dedicated paths (.server-beta.pid|.port|.runtime.json). The four boundary managers (queue, generation worker, provider registry, event broadcaster) are intentionally disabled in this phase and report their status through /v1/info; later phases activate them. Adds plans/2026-05-07-finish-bullmq-branch-ship-plan.md to track the remaining work for this branch. Phase 2 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(server-beta): route CLI lifecycle and bundle separate runtime scripts/build-hooks.js now produces plugin/scripts/server-beta-service.cjs as a separate Node CJS bundle, alongside the existing worker-service bundle. The server-beta runtime is now installable independently. src/npx-cli/commands/server.ts routes start|stop|restart|status to the server-beta lifecycle instead of the legacy worker. The worker keeps its own start|stop|restart|status under the worker namespace; the two runtimes can be operated independently. src/services/worker-service.ts adds a server-* command parser branch that delegates to the sibling server-beta-service.cjs bundle so direct worker-service invocations still route to the right runtime. tests/npx-cli-server-namespace.test.ts updated to expect server-beta lifecycle routing. Includes rebuilt plugin/scripts/*.cjs bundles produced by build-and-sync. Phase 2 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(server-beta): add BullMQ job queue primitives Introduce src/server/jobs/ as the queue-side primitives that Phase 3 of the server-beta runtime needs to operate. types.ts defines a discriminated union over the four job kinds (event, event-batch, summary, reindex) and maps each to a per-kind BullMQ queue name and deterministic-ID prefix. job-id.ts builds deterministic, colon-free BullMQ jobIds from (kind, team, project, source). The colon ban exists because BullMQ uses ':' as a Redis key separator internally; embedding ':' in jobIds breaks scan and state lookups. ServerJobQueue.ts is a thin wrapper over BullMQ Queue + Worker that enforces autorun:false, default concurrency 1, and an attached error listener — all per BullMQ docs requirements. Test seams accept queue and worker factories so unit tests do not need Redis. outbox.ts publishes through the Postgres ObservationGenerationJob repository as canonical history. enqueueOutbox writes the row first, then publishes to BullMQ; if BullMQ throws, the row is transitioned to failed and a failed event is appended. reconcileOnStartup re-enqueues queued + processing rows after a restart, replacing terminal BullMQ jobs that may still be holding the deterministic ID slot. markCompleted and markFailed wrap transitionStatus and append the matching event row. Includes 20 unit tests covering deterministic ID stability, colon-free output, queue lifecycle, error-listener attachment, double-start refusal, idempotent enqueue, BullMQ failure rollback, startup reconciliation, max-attempts skipping, and completion / failure / retry transitions. Phase 3 commit 1 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(server-beta): activate queue boundary in runtime service Wire ActiveServerBetaQueueManager into the server-beta runtime graph. The active manager owns one ServerJobQueue per generation kind (event, event-batch, summary, reindex) and surfaces lane metadata through boundary health. Selection is opt-in and fail-fast: if CLAUDE_MEM_QUEUE_ENGINE is set to bullmq the active manager is constructed (and any Redis/config error throws — no silent fallback to SQLite, per Phase 3 anti-pattern guard). For any other engine the disabled boundary remains so worker-era and test setups stay compatible. Widens ServerBetaBoundaryHealth.status to a discriminated union ('disabled' | 'active' | 'errored') with optional details. The disabled adapter still emits status='disabled', which keeps the existing server-beta-service test green. ServerBetaService receives the manager through a new optional queueManager field on CreateServerBetaServiceOptions so test graphs and Phase 4 wiring can inject custom managers. Adds tests/server/runtime/active-queue-manager.test.ts covering bullmq guard, active health shape, per-kind queue access, close behavior, and post-close errored health. Phase 3 commit 2 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(server-beta): cap /v1/events/batch at 500 events Prevents unbounded array DoS surface flagged in PR review. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
||||
import { Database } from 'bun:sqlite';
|
||||
import {
|
||||
createServerApiKey,
|
||||
hashServerApiKey,
|
||||
revokeServerApiKey,
|
||||
verifyServerApiKey,
|
||||
} from '../../src/server/auth/api-key-service.js';
|
||||
import { requireServerAuth } from '../../src/server/middleware/auth.js';
|
||||
import { ProjectsRepository, TeamsRepository } from '../../src/storage/sqlite/index.js';
|
||||
|
||||
describe('server API key auth', () => {
|
||||
let db: Database;
|
||||
|
||||
beforeEach(() => {
|
||||
db = new Database(':memory:');
|
||||
db.run('PRAGMA foreign_keys = ON');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('creates raw keys once while storing only a hash', () => {
|
||||
const created = createServerApiKey(db, {
|
||||
name: 'Team key',
|
||||
teamId: null,
|
||||
projectId: null,
|
||||
scopes: ['memories:read'],
|
||||
});
|
||||
|
||||
expect(created.rawKey).toStartWith('cmem_');
|
||||
expect(created.record.keyHash).toBe(hashServerApiKey(created.rawKey));
|
||||
expect(created.record.keyHash).not.toContain(created.rawKey);
|
||||
expect(created.record.prefix).toBe(created.rawKey.slice(0, 10));
|
||||
});
|
||||
|
||||
it('verifies required scopes and rejects revoked keys', () => {
|
||||
const created = createServerApiKey(db, {
|
||||
name: 'Scoped key',
|
||||
scopes: ['memories:read'],
|
||||
});
|
||||
|
||||
expect(verifyServerApiKey(db, created.rawKey, ['memories:read'])?.record.id).toBe(created.record.id);
|
||||
expect(verifyServerApiKey(db, created.rawKey, ['memories:write'])).toBeNull();
|
||||
|
||||
revokeServerApiKey(db, created.record.id);
|
||||
expect(verifyServerApiKey(db, created.rawKey, ['memories:read'])).toBeNull();
|
||||
});
|
||||
|
||||
it('middleware allows localhost local-dev without a bearer token', () => {
|
||||
const middleware = requireServerAuth(() => db, { authMode: 'local-dev', allowLocalDevBypass: true });
|
||||
const req: any = {
|
||||
ip: '127.0.0.1',
|
||||
socket: {},
|
||||
header: (name: string) => name.toLowerCase() === 'host' ? '127.0.0.1:37777' : undefined,
|
||||
};
|
||||
const res: any = {
|
||||
status: () => res,
|
||||
json: () => {},
|
||||
};
|
||||
let calledNext = false;
|
||||
|
||||
middleware(req, res, () => {
|
||||
calledNext = true;
|
||||
});
|
||||
|
||||
expect(calledNext).toBe(true);
|
||||
expect(req.authContext).toMatchObject({ mode: 'local-dev', scopes: ['local-dev'] });
|
||||
});
|
||||
|
||||
it('middleware requires explicit opt-in before local-dev bypass is honored', () => {
|
||||
const middleware = requireServerAuth(() => db, { authMode: 'local-dev' });
|
||||
const req: any = {
|
||||
ip: '127.0.0.1',
|
||||
socket: { remoteAddress: '127.0.0.1' },
|
||||
header: (name: string) => name.toLowerCase() === 'host' ? 'localhost:37777' : undefined,
|
||||
};
|
||||
const res: any = {
|
||||
statusCode: 200,
|
||||
status(code: number) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
json: () => {},
|
||||
};
|
||||
let calledNext = false;
|
||||
|
||||
middleware(req, res, () => {
|
||||
calledNext = true;
|
||||
});
|
||||
|
||||
expect(calledNext).toBe(false);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('middleware blocks local-dev bypass when forwarded proxy headers are present', () => {
|
||||
const middleware = requireServerAuth(() => db, { authMode: 'local-dev', allowLocalDevBypass: true });
|
||||
const req: any = {
|
||||
ip: '127.0.0.1',
|
||||
socket: { remoteAddress: '127.0.0.1' },
|
||||
header: (name: string) => {
|
||||
const normalized = name.toLowerCase();
|
||||
if (normalized === 'host') return 'claude-mem.example.com';
|
||||
if (normalized === 'x-forwarded-for') return '203.0.113.10';
|
||||
return undefined;
|
||||
},
|
||||
};
|
||||
const res: any = {
|
||||
statusCode: 200,
|
||||
status(code: number) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
json: () => {},
|
||||
};
|
||||
let calledNext = false;
|
||||
|
||||
middleware(req, res, () => {
|
||||
calledNext = true;
|
||||
});
|
||||
|
||||
expect(calledNext).toBe(false);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('middleware accepts bracketed IPv6 loopback host headers in explicit local-dev mode', () => {
|
||||
const middleware = requireServerAuth(() => db, { authMode: 'local-dev', allowLocalDevBypass: true });
|
||||
const req: any = {
|
||||
ip: '::1',
|
||||
socket: { remoteAddress: '::1' },
|
||||
header: (name: string) => name.toLowerCase() === 'host' ? '[::1]:37777' : undefined,
|
||||
};
|
||||
const res: any = {
|
||||
status: () => res,
|
||||
json: () => {},
|
||||
};
|
||||
let calledNext = false;
|
||||
|
||||
middleware(req, res, () => {
|
||||
calledNext = true;
|
||||
});
|
||||
|
||||
expect(calledNext).toBe(true);
|
||||
expect(req.authContext).toMatchObject({ mode: 'local-dev', scopes: ['local-dev'] });
|
||||
});
|
||||
|
||||
it('middleware defaults to API-key auth when auth mode is not explicitly set', () => {
|
||||
const originalAuthMode = process.env.CLAUDE_MEM_AUTH_MODE;
|
||||
delete process.env.CLAUDE_MEM_AUTH_MODE;
|
||||
try {
|
||||
const middleware = requireServerAuth(() => db);
|
||||
const req: any = {
|
||||
ip: '127.0.0.1',
|
||||
socket: { remoteAddress: '127.0.0.1' },
|
||||
header: (name: string) => name.toLowerCase() === 'host' ? 'localhost:37777' : undefined,
|
||||
};
|
||||
const res: any = {
|
||||
statusCode: 200,
|
||||
body: null,
|
||||
status(code: number) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
json(body: unknown) {
|
||||
this.body = body;
|
||||
},
|
||||
};
|
||||
let calledNext = false;
|
||||
|
||||
middleware(req, res, () => {
|
||||
calledNext = true;
|
||||
});
|
||||
|
||||
expect(calledNext).toBe(false);
|
||||
expect(res.statusCode).toBe(401);
|
||||
expect(res.body).toMatchObject({ error: 'Unauthorized' });
|
||||
} finally {
|
||||
if (originalAuthMode === undefined) {
|
||||
delete process.env.CLAUDE_MEM_AUTH_MODE;
|
||||
} else {
|
||||
process.env.CLAUDE_MEM_AUTH_MODE = originalAuthMode;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('middleware requires a scoped bearer API key outside local-dev fallback', () => {
|
||||
const team = new TeamsRepository(db).create({ name: 'Core' });
|
||||
const project = new ProjectsRepository(db).create({ name: 'Project' });
|
||||
const created = createServerApiKey(db, {
|
||||
name: 'Write key',
|
||||
teamId: team.id,
|
||||
projectId: project.id,
|
||||
scopes: ['memories:write'],
|
||||
});
|
||||
const middleware = requireServerAuth(() => db, {
|
||||
authMode: 'api-key',
|
||||
requiredScopes: ['memories:write'],
|
||||
});
|
||||
const req: any = {
|
||||
ip: '10.0.0.5',
|
||||
socket: {},
|
||||
header: (name: string) => name.toLowerCase() === 'authorization' ? `Bearer ${created.rawKey}` : undefined,
|
||||
};
|
||||
const res: any = {
|
||||
status: () => res,
|
||||
json: () => {},
|
||||
};
|
||||
let calledNext = false;
|
||||
|
||||
middleware(req, res, () => {
|
||||
calledNext = true;
|
||||
});
|
||||
|
||||
expect(calledNext).toBe(true);
|
||||
expect(req.authContext).toMatchObject({
|
||||
mode: 'api-key',
|
||||
apiKeyId: created.record.id,
|
||||
teamId: team.id,
|
||||
projectId: project.id,
|
||||
scopes: ['memories:write'],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { buildServerJobId } from '../../../src/server/jobs/job-id.js';
|
||||
|
||||
const baseParts = {
|
||||
kind: 'event' as const,
|
||||
team_id: 'team_abc',
|
||||
project_id: 'project_xyz',
|
||||
source_type: 'agent_event',
|
||||
source_id: 'evt_001'
|
||||
};
|
||||
|
||||
describe('buildServerJobId', () => {
|
||||
it('produces deterministic IDs across invocations', () => {
|
||||
const a = buildServerJobId(baseParts);
|
||||
const b = buildServerJobId(baseParts);
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
|
||||
it('changes the digest when any scope field changes', () => {
|
||||
const baseId = buildServerJobId(baseParts);
|
||||
const variants = [
|
||||
{ ...baseParts, team_id: 'team_other' },
|
||||
{ ...baseParts, project_id: 'project_other' },
|
||||
{ ...baseParts, source_type: 'observation_reindex' },
|
||||
{ ...baseParts, source_id: 'evt_002' },
|
||||
{ ...baseParts, kind: 'reindex' as const }
|
||||
];
|
||||
for (const variant of variants) {
|
||||
expect(buildServerJobId(variant)).not.toBe(baseId);
|
||||
}
|
||||
});
|
||||
|
||||
it('emits IDs without colons so BullMQ key separators stay safe', () => {
|
||||
const id = buildServerJobId(baseParts);
|
||||
expect(id.includes(':')).toBe(false);
|
||||
});
|
||||
|
||||
it('uses a kind-prefixed sha256 hex format', () => {
|
||||
const id = buildServerJobId(baseParts);
|
||||
expect(id).toMatch(/^evt_[0-9a-f]{64}$/);
|
||||
});
|
||||
|
||||
it('uses different prefixes per kind', () => {
|
||||
const event = buildServerJobId({ ...baseParts, kind: 'event' });
|
||||
const batch = buildServerJobId({ ...baseParts, kind: 'event-batch' });
|
||||
const summary = buildServerJobId({ ...baseParts, kind: 'summary' });
|
||||
const reindex = buildServerJobId({ ...baseParts, kind: 'reindex' });
|
||||
expect(event.startsWith('evt_')).toBe(true);
|
||||
expect(batch.startsWith('evtb_')).toBe(true);
|
||||
expect(summary.startsWith('sum_')).toBe(true);
|
||||
expect(reindex.startsWith('rdx_')).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,413 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import {
|
||||
enqueueOutbox,
|
||||
markCompleted,
|
||||
markFailed,
|
||||
reconcileOnStartup,
|
||||
type SingleSourceJobPayload
|
||||
} from '../../../src/server/jobs/outbox.js';
|
||||
import type { ServerJobQueue } from '../../../src/server/jobs/ServerJobQueue.js';
|
||||
import type {
|
||||
ObservationGenerationJobStatus,
|
||||
PostgresObservationGenerationJob,
|
||||
PostgresObservationGenerationJobEvent,
|
||||
PostgresObservationGenerationJobEventsRepository,
|
||||
PostgresObservationGenerationJobRepository
|
||||
} from '../../../src/storage/postgres/generation-jobs.js';
|
||||
|
||||
interface CreateInput {
|
||||
id?: string;
|
||||
projectId: string;
|
||||
teamId: string;
|
||||
sourceType: PostgresObservationGenerationJob['sourceType'];
|
||||
sourceId: string;
|
||||
agentEventId?: string | null;
|
||||
serverSessionId?: string | null;
|
||||
jobType: string;
|
||||
status?: ObservationGenerationJobStatus;
|
||||
bullmqJobId?: string | null;
|
||||
maxAttempts?: number;
|
||||
payload?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface StubJobRepoState {
|
||||
rows: Map<string, PostgresObservationGenerationJob>;
|
||||
counter: number;
|
||||
}
|
||||
|
||||
function buildStubJobRepo(state: StubJobRepoState): PostgresObservationGenerationJobRepository {
|
||||
const rowId = () => `job_${++state.counter}`;
|
||||
const ts = () => Date.now();
|
||||
|
||||
return {
|
||||
async create(input: CreateInput): Promise<PostgresObservationGenerationJob> {
|
||||
const idempotencyKey = `idem:${input.teamId}:${input.projectId}:${input.sourceType}:${input.sourceId}:${input.jobType}`;
|
||||
const existing = [...state.rows.values()].find(r => r.idempotencyKey === idempotencyKey);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
const id = input.id ?? rowId();
|
||||
const row: PostgresObservationGenerationJob = {
|
||||
id,
|
||||
projectId: input.projectId,
|
||||
teamId: input.teamId,
|
||||
agentEventId: input.agentEventId ?? null,
|
||||
sourceType: input.sourceType,
|
||||
sourceId: input.sourceId,
|
||||
serverSessionId: input.serverSessionId ?? null,
|
||||
jobType: input.jobType,
|
||||
status: input.status ?? 'queued',
|
||||
idempotencyKey,
|
||||
bullmqJobId: input.bullmqJobId ?? null,
|
||||
attempts: 0,
|
||||
maxAttempts: input.maxAttempts ?? 3,
|
||||
nextAttemptAtEpoch: null,
|
||||
lockedAtEpoch: null,
|
||||
lockedBy: null,
|
||||
completedAtEpoch: null,
|
||||
failedAtEpoch: null,
|
||||
cancelledAtEpoch: null,
|
||||
lastError: null,
|
||||
payload: (input.payload as PostgresObservationGenerationJob['payload']) ?? {},
|
||||
createdAtEpoch: ts(),
|
||||
updatedAtEpoch: ts()
|
||||
};
|
||||
state.rows.set(id, row);
|
||||
return row;
|
||||
},
|
||||
|
||||
async getByIdForScope(input) {
|
||||
const row = state.rows.get(input.id);
|
||||
if (!row || row.projectId !== input.projectId || row.teamId !== input.teamId) {
|
||||
return null;
|
||||
}
|
||||
return row;
|
||||
},
|
||||
|
||||
async transitionStatus(input) {
|
||||
const row = state.rows.get(input.id);
|
||||
if (!row || row.projectId !== input.projectId || row.teamId !== input.teamId) {
|
||||
return null;
|
||||
}
|
||||
const next: PostgresObservationGenerationJob = {
|
||||
...row,
|
||||
status: input.status,
|
||||
attempts: input.status === 'processing' ? row.attempts + 1 : row.attempts,
|
||||
lastError: input.lastError ?? null,
|
||||
nextAttemptAtEpoch: input.nextAttemptAt ? input.nextAttemptAt.getTime() : null,
|
||||
completedAtEpoch: input.status === 'completed' ? ts() : null,
|
||||
failedAtEpoch: input.status === 'failed' ? ts() : null,
|
||||
cancelledAtEpoch: input.status === 'cancelled' ? ts() : null,
|
||||
updatedAtEpoch: ts()
|
||||
};
|
||||
state.rows.set(input.id, next);
|
||||
return next;
|
||||
},
|
||||
|
||||
async listByStatusForScope(input) {
|
||||
return [...state.rows.values()].filter(
|
||||
r => r.status === input.status && r.projectId === input.projectId && r.teamId === input.teamId
|
||||
);
|
||||
}
|
||||
} as unknown as PostgresObservationGenerationJobRepository;
|
||||
}
|
||||
|
||||
interface EventLogEntry {
|
||||
generationJobId: string;
|
||||
eventType: PostgresObservationGenerationJobEvent['eventType'];
|
||||
statusAfter: ObservationGenerationJobStatus;
|
||||
attempt: number;
|
||||
details?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
function buildStubEventsRepo(log: EventLogEntry[]): PostgresObservationGenerationJobEventsRepository {
|
||||
return {
|
||||
async append(input) {
|
||||
log.push({
|
||||
generationJobId: input.generationJobId,
|
||||
eventType: input.eventType,
|
||||
statusAfter: input.statusAfter,
|
||||
attempt: input.attempt ?? 0,
|
||||
details: input.details ?? {}
|
||||
});
|
||||
return {
|
||||
id: `evt_${log.length}`,
|
||||
generationJobId: input.generationJobId,
|
||||
eventType: input.eventType,
|
||||
statusAfter: input.statusAfter,
|
||||
attempt: input.attempt ?? 0,
|
||||
details: input.details ?? {},
|
||||
createdAtEpoch: Date.now()
|
||||
};
|
||||
},
|
||||
async listByJobForScope() {
|
||||
return [];
|
||||
}
|
||||
} as unknown as PostgresObservationGenerationJobEventsRepository;
|
||||
}
|
||||
|
||||
interface StubQueueState {
|
||||
added: Array<{ jobId: string; payload: SingleSourceJobPayload }>;
|
||||
removed: string[];
|
||||
failOnAdd: boolean;
|
||||
}
|
||||
|
||||
function buildStubQueue(state: StubQueueState): ServerJobQueue<SingleSourceJobPayload> {
|
||||
return {
|
||||
name: 'stub',
|
||||
add: async (jobId: string, payload: SingleSourceJobPayload) => {
|
||||
if (state.failOnAdd) {
|
||||
throw new Error('redis unavailable');
|
||||
}
|
||||
state.added.push({ jobId, payload });
|
||||
},
|
||||
remove: async (jobId: string) => {
|
||||
state.removed.push(jobId);
|
||||
},
|
||||
getJob: async () => null,
|
||||
getCounts: async () => ({ waiting: 0, active: 0, delayed: 0, failed: 0, completed: 0 }),
|
||||
start: () => {},
|
||||
isStarted: () => false,
|
||||
close: async () => {}
|
||||
} as unknown as ServerJobQueue<SingleSourceJobPayload>;
|
||||
}
|
||||
|
||||
const eventPayload: SingleSourceJobPayload = {
|
||||
kind: 'event',
|
||||
team_id: 'team_1',
|
||||
project_id: 'project_1',
|
||||
source_type: 'agent_event',
|
||||
source_id: 'evt_1',
|
||||
generation_job_id: 'gen_1',
|
||||
agent_event_id: 'evt_1'
|
||||
};
|
||||
|
||||
describe('outbox.enqueueOutbox', () => {
|
||||
it('writes the row, records two events, and publishes to BullMQ', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const queueState: StubQueueState = { added: [], removed: [], failOnAdd: false };
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
const queue = buildStubQueue(queueState);
|
||||
|
||||
const { row, bullmqJobId } = await enqueueOutbox(jobRepo, eventsRepo, queue, {
|
||||
payload: eventPayload
|
||||
});
|
||||
|
||||
expect(row.status).toBe('queued');
|
||||
expect(row.agentEventId).toBe('evt_1');
|
||||
expect(row.jobType).toBe('observation_generate_for_event');
|
||||
expect(bullmqJobId.startsWith('evt_')).toBe(true);
|
||||
expect(bullmqJobId.includes(':')).toBe(false);
|
||||
expect(queueState.added).toHaveLength(1);
|
||||
expect(queueState.added[0]!.jobId).toBe(bullmqJobId);
|
||||
expect(log.map(e => e.eventType)).toEqual(['queued', 'enqueued']);
|
||||
});
|
||||
|
||||
it('suppresses duplicate enqueues by returning the same idempotency-keyed row', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const queueState: StubQueueState = { added: [], removed: [], failOnAdd: false };
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
const queue = buildStubQueue(queueState);
|
||||
|
||||
const first = await enqueueOutbox(jobRepo, eventsRepo, queue, { payload: eventPayload });
|
||||
const second = await enqueueOutbox(jobRepo, eventsRepo, queue, { payload: eventPayload });
|
||||
|
||||
expect(second.row.id).toBe(first.row.id);
|
||||
expect(second.bullmqJobId).toBe(first.bullmqJobId);
|
||||
expect(repoState.rows.size).toBe(1);
|
||||
});
|
||||
|
||||
it('marks the row failed when BullMQ publish throws', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const queueState: StubQueueState = { added: [], removed: [], failOnAdd: true };
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
const queue = buildStubQueue(queueState);
|
||||
|
||||
await expect(
|
||||
enqueueOutbox(jobRepo, eventsRepo, queue, { payload: eventPayload })
|
||||
).rejects.toThrow(/redis unavailable/);
|
||||
|
||||
const row = [...repoState.rows.values()][0]!;
|
||||
expect(row.status).toBe('failed');
|
||||
expect(row.lastError?.source).toBe('bullmq_publish');
|
||||
const eventTypes = log.map(e => e.eventType);
|
||||
expect(eventTypes).toContain('queued');
|
||||
expect(eventTypes).toContain('failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('outbox.reconcileOnStartup', () => {
|
||||
it('replaces terminal BullMQ jobs and re-enqueues queued + processing rows', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const queueState: StubQueueState = { added: [], removed: [], failOnAdd: false };
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
const queue = buildStubQueue(queueState);
|
||||
|
||||
await enqueueOutbox(jobRepo, eventsRepo, queue, { payload: eventPayload });
|
||||
queueState.added.length = 0;
|
||||
log.length = 0;
|
||||
|
||||
const result = await reconcileOnStartup(jobRepo, eventsRepo, queue, {
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1'
|
||||
});
|
||||
|
||||
expect(result.requeued).toBe(1);
|
||||
expect(result.skipped).toBe(0);
|
||||
expect(queueState.removed).toHaveLength(1);
|
||||
expect(queueState.added).toHaveLength(1);
|
||||
expect(log.some(e => e.eventType === 'enqueued')).toBe(true);
|
||||
});
|
||||
|
||||
it('skips rows that have hit max_attempts', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const queueState: StubQueueState = { added: [], removed: [], failOnAdd: false };
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
const queue = buildStubQueue(queueState);
|
||||
|
||||
const created = await jobRepo.create({
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
sourceType: 'agent_event',
|
||||
sourceId: 'evt_1',
|
||||
agentEventId: 'evt_1',
|
||||
jobType: 'observation_generate_for_event',
|
||||
payload: {},
|
||||
maxAttempts: 1
|
||||
});
|
||||
repoState.rows.set(created.id, { ...created, attempts: 1 });
|
||||
|
||||
const result = await reconcileOnStartup(jobRepo, eventsRepo, queue, {
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1'
|
||||
});
|
||||
|
||||
expect(result.requeued).toBe(0);
|
||||
expect(result.skipped).toBe(1);
|
||||
expect(queueState.added).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('demotes processing rows back to queued before re-enqueue', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const queueState: StubQueueState = { added: [], removed: [], failOnAdd: false };
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
const queue = buildStubQueue(queueState);
|
||||
|
||||
const created = await jobRepo.create({
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
sourceType: 'agent_event',
|
||||
sourceId: 'evt_1',
|
||||
agentEventId: 'evt_1',
|
||||
jobType: 'observation_generate_for_event',
|
||||
payload: {}
|
||||
});
|
||||
repoState.rows.set(created.id, { ...created, status: 'processing', attempts: 1 });
|
||||
|
||||
await reconcileOnStartup(jobRepo, eventsRepo, queue, {
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1'
|
||||
});
|
||||
|
||||
const row = repoState.rows.get(created.id)!;
|
||||
expect(row.status).toBe('queued');
|
||||
expect(queueState.added).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('outbox.markCompleted / markFailed', () => {
|
||||
it('transitions to completed and appends a completed event', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
|
||||
const created = await jobRepo.create({
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
sourceType: 'agent_event',
|
||||
sourceId: 'evt_1',
|
||||
agentEventId: 'evt_1',
|
||||
jobType: 'observation_generate_for_event'
|
||||
});
|
||||
|
||||
await markCompleted(jobRepo, eventsRepo, {
|
||||
id: created.id,
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1'
|
||||
});
|
||||
|
||||
expect(repoState.rows.get(created.id)!.status).toBe('completed');
|
||||
expect(log[0]!.eventType).toBe('completed');
|
||||
});
|
||||
|
||||
it('transitions to failed and records the error', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
|
||||
const created = await jobRepo.create({
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
sourceType: 'agent_event',
|
||||
sourceId: 'evt_1',
|
||||
agentEventId: 'evt_1',
|
||||
jobType: 'observation_generate_for_event'
|
||||
});
|
||||
|
||||
await markFailed(jobRepo, eventsRepo, {
|
||||
id: created.id,
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
error: { message: 'provider 500', source: 'processor' }
|
||||
});
|
||||
|
||||
expect(repoState.rows.get(created.id)!.status).toBe('failed');
|
||||
expect(repoState.rows.get(created.id)!.lastError).toEqual({
|
||||
message: 'provider 500',
|
||||
source: 'processor'
|
||||
});
|
||||
expect(log[0]!.eventType).toBe('failed');
|
||||
});
|
||||
|
||||
it('schedules a retry by transitioning to queued when nextAttemptAt is given', async () => {
|
||||
const repoState: StubJobRepoState = { rows: new Map(), counter: 0 };
|
||||
const log: EventLogEntry[] = [];
|
||||
const jobRepo = buildStubJobRepo(repoState);
|
||||
const eventsRepo = buildStubEventsRepo(log);
|
||||
|
||||
const created = await jobRepo.create({
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
sourceType: 'agent_event',
|
||||
sourceId: 'evt_1',
|
||||
agentEventId: 'evt_1',
|
||||
jobType: 'observation_generate_for_event'
|
||||
});
|
||||
|
||||
const retryAt = new Date(Date.now() + 60_000);
|
||||
await markFailed(jobRepo, eventsRepo, {
|
||||
id: created.id,
|
||||
projectId: 'project_1',
|
||||
teamId: 'team_1',
|
||||
error: { message: 'transient', source: 'processor' },
|
||||
nextAttemptAt: retryAt
|
||||
});
|
||||
|
||||
expect(repoState.rows.get(created.id)!.status).toBe('queued');
|
||||
expect(log[0]!.eventType).toBe('retry_scheduled');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,190 @@
|
||||
import { afterEach, describe, expect, it, mock } from 'bun:test';
|
||||
import type { Job, Processor, QueueOptions, WorkerOptions } from 'bullmq';
|
||||
import { ServerJobQueue } from '../../../src/server/jobs/ServerJobQueue.js';
|
||||
import type { RedisQueueConfig } from '../../../src/server/queue/redis-config.js';
|
||||
|
||||
const fakeConfig: RedisQueueConfig = {
|
||||
engine: 'bullmq',
|
||||
mode: 'managed',
|
||||
url: 'redis://test/0',
|
||||
host: 'test',
|
||||
port: 6379,
|
||||
prefix: 'cmem-test',
|
||||
connection: { host: 'test', port: 6379, lazyConnect: true }
|
||||
};
|
||||
|
||||
interface FakeQueueState {
|
||||
added: Array<{ name: string; payload: unknown; jobId?: string }>;
|
||||
removed: string[];
|
||||
closed: boolean;
|
||||
}
|
||||
|
||||
interface FakeWorkerState {
|
||||
processor: Processor<unknown> | null;
|
||||
options: WorkerOptions | null;
|
||||
errorHandlers: Array<(error: unknown) => void>;
|
||||
ranWith: 'autorun-false' | 'autorun-true' | null;
|
||||
closed: boolean;
|
||||
}
|
||||
|
||||
function buildFakeQueue(state: FakeQueueState) {
|
||||
return (_name: string, _options: QueueOptions) => ({
|
||||
add: async (name: string, payload: unknown, opts?: { jobId?: string }) => {
|
||||
state.added.push({ name, payload, jobId: opts?.jobId });
|
||||
return { id: opts?.jobId ?? 'job_anon' } as Job<unknown>;
|
||||
},
|
||||
getJob: async (_id: string) => null,
|
||||
getJobCounts: async (..._states: string[]) => ({
|
||||
waiting: 1,
|
||||
active: 0,
|
||||
delayed: 0,
|
||||
failed: 0,
|
||||
completed: 0
|
||||
}),
|
||||
remove: async (id: string) => {
|
||||
state.removed.push(id);
|
||||
},
|
||||
close: async () => {
|
||||
state.closed = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function buildFakeWorker(state: FakeWorkerState) {
|
||||
return (_name: string, processor: Processor<unknown> | null, options: WorkerOptions) => {
|
||||
state.processor = processor;
|
||||
state.options = options;
|
||||
return {
|
||||
on: (event: string, handler: (error: unknown) => void) => {
|
||||
if (event === 'error') {
|
||||
state.errorHandlers.push(handler);
|
||||
}
|
||||
},
|
||||
run: () => {
|
||||
state.ranWith = options.autorun === false ? 'autorun-false' : 'autorun-true';
|
||||
},
|
||||
close: async () => {
|
||||
state.closed = true;
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
describe('ServerJobQueue', () => {
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
it('rejects jobIds that contain colons (BullMQ key separator)', async () => {
|
||||
const queueState: FakeQueueState = { added: [], removed: [], closed: false };
|
||||
const sjq = new ServerJobQueue<{ x: number }>({
|
||||
name: 'q',
|
||||
config: fakeConfig,
|
||||
queueFactory: buildFakeQueue(queueState)
|
||||
});
|
||||
await expect(sjq.add('bad:id', { x: 1 })).rejects.toThrow(/must not contain ':'/);
|
||||
expect(queueState.added.length).toBe(0);
|
||||
await sjq.close();
|
||||
});
|
||||
|
||||
it('passes the jobId through to BullMQ Queue.add', async () => {
|
||||
const queueState: FakeQueueState = { added: [], removed: [], closed: false };
|
||||
const sjq = new ServerJobQueue<{ x: number }>({
|
||||
name: 'q',
|
||||
config: fakeConfig,
|
||||
queueFactory: buildFakeQueue(queueState)
|
||||
});
|
||||
await sjq.add('evt_abc', { x: 1 });
|
||||
expect(queueState.added).toHaveLength(1);
|
||||
expect(queueState.added[0]!.jobId).toBe('evt_abc');
|
||||
expect(queueState.added[0]!.payload).toEqual({ x: 1 });
|
||||
await sjq.close();
|
||||
});
|
||||
|
||||
it('starts the worker with autorun: false and attaches an error listener', () => {
|
||||
const queueState: FakeQueueState = { added: [], removed: [], closed: false };
|
||||
const workerState: FakeWorkerState = {
|
||||
processor: null,
|
||||
options: null,
|
||||
errorHandlers: [],
|
||||
ranWith: null,
|
||||
closed: false
|
||||
};
|
||||
const sjq = new ServerJobQueue<{ x: number }>({
|
||||
name: 'q',
|
||||
config: fakeConfig,
|
||||
queueFactory: buildFakeQueue(queueState),
|
||||
workerFactory: buildFakeWorker(workerState)
|
||||
});
|
||||
sjq.start(async () => {});
|
||||
|
||||
expect(workerState.options?.autorun).toBe(false);
|
||||
expect(workerState.options?.concurrency).toBe(1);
|
||||
expect(workerState.errorHandlers.length).toBeGreaterThanOrEqual(1);
|
||||
expect(workerState.ranWith).toBe('autorun-false');
|
||||
expect(sjq.isStarted()).toBe(true);
|
||||
});
|
||||
|
||||
it('refuses double-start to avoid duplicate Worker instances', () => {
|
||||
const queueState: FakeQueueState = { added: [], removed: [], closed: false };
|
||||
const workerState: FakeWorkerState = {
|
||||
processor: null,
|
||||
options: null,
|
||||
errorHandlers: [],
|
||||
ranWith: null,
|
||||
closed: false
|
||||
};
|
||||
const sjq = new ServerJobQueue<{ x: number }>({
|
||||
name: 'q',
|
||||
config: fakeConfig,
|
||||
queueFactory: buildFakeQueue(queueState),
|
||||
workerFactory: buildFakeWorker(workerState)
|
||||
});
|
||||
sjq.start(async () => {});
|
||||
expect(() => sjq.start(async () => {})).toThrow(/already started/);
|
||||
});
|
||||
|
||||
it('error listener absorbs worker errors without throwing', () => {
|
||||
const queueState: FakeQueueState = { added: [], removed: [], closed: false };
|
||||
const workerState: FakeWorkerState = {
|
||||
processor: null,
|
||||
options: null,
|
||||
errorHandlers: [],
|
||||
ranWith: null,
|
||||
closed: false
|
||||
};
|
||||
const sjq = new ServerJobQueue<{ x: number }>({
|
||||
name: 'q',
|
||||
config: fakeConfig,
|
||||
queueFactory: buildFakeQueue(queueState),
|
||||
workerFactory: buildFakeWorker(workerState)
|
||||
});
|
||||
sjq.start(async () => {});
|
||||
expect(() =>
|
||||
workerState.errorHandlers[0]!(new Error('worker crashed'))
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it('closes worker and queue on close()', async () => {
|
||||
const queueState: FakeQueueState = { added: [], removed: [], closed: false };
|
||||
const workerState: FakeWorkerState = {
|
||||
processor: null,
|
||||
options: null,
|
||||
errorHandlers: [],
|
||||
ranWith: null,
|
||||
closed: false
|
||||
};
|
||||
const sjq = new ServerJobQueue<{ x: number }>({
|
||||
name: 'q',
|
||||
config: fakeConfig,
|
||||
queueFactory: buildFakeQueue(queueState),
|
||||
workerFactory: buildFakeWorker(workerState)
|
||||
});
|
||||
sjq.start(async () => {});
|
||||
await sjq.add('evt_test', { x: 1 });
|
||||
await sjq.close();
|
||||
expect(workerState.closed).toBe(true);
|
||||
expect(queueState.closed).toBe(true);
|
||||
expect(sjq.isStarted()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { getServerMcpSurface } from '../../src/server/mcp/register.js';
|
||||
|
||||
describe('server MCP surface', () => {
|
||||
it('declares memory tools with concrete input schemas', () => {
|
||||
const surface = getServerMcpSurface();
|
||||
const names = surface.tools.map(tool => tool.name);
|
||||
|
||||
expect(names).toEqual([
|
||||
'memory_add',
|
||||
'memory_search',
|
||||
'memory_context',
|
||||
'memory_forget',
|
||||
'memory_list_recent',
|
||||
'memory_record_decision',
|
||||
]);
|
||||
|
||||
for (const tool of surface.tools) {
|
||||
expect(tool.inputSchema.type).toBe('object');
|
||||
expect(Object.keys(tool.inputSchema.properties).length).toBeGreaterThan(0);
|
||||
expect(tool.inputSchema.required?.length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps resources and prompts available without Bun-only imports', () => {
|
||||
const surface = getServerMcpSurface();
|
||||
|
||||
expect(surface.resources[0].uri).toStartWith('claude-mem://server/');
|
||||
expect(surface.prompts[0].name).toBe('record_decision');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { afterEach, describe, expect, it, mock } from 'bun:test';
|
||||
import { ActiveServerBetaQueueManager } from '../../../src/server/runtime/ActiveServerBetaQueueManager.js';
|
||||
import { ServerJobQueue } from '../../../src/server/jobs/ServerJobQueue.js';
|
||||
import type {
|
||||
ServerGenerationJobKind,
|
||||
ServerGenerationJobPayload,
|
||||
} from '../../../src/server/jobs/types.js';
|
||||
import type { RedisQueueConfig } from '../../../src/server/queue/redis-config.js';
|
||||
|
||||
const bullmqConfig: RedisQueueConfig = {
|
||||
engine: 'bullmq',
|
||||
mode: 'managed',
|
||||
url: null,
|
||||
host: '127.0.0.1',
|
||||
port: 6379,
|
||||
prefix: 'cmem-test',
|
||||
connection: { host: '127.0.0.1', port: 6379, lazyConnect: true },
|
||||
};
|
||||
|
||||
const sqliteConfig: RedisQueueConfig = {
|
||||
...bullmqConfig,
|
||||
engine: 'sqlite',
|
||||
};
|
||||
|
||||
function buildStubQueues(): {
|
||||
queues: Map<ServerGenerationJobKind, ServerJobQueue<ServerGenerationJobPayload>>;
|
||||
closedNames: string[];
|
||||
} {
|
||||
const closedNames: string[] = [];
|
||||
const make = (name: string) => ({
|
||||
name,
|
||||
add: async () => {},
|
||||
remove: async () => {},
|
||||
getJob: async () => null,
|
||||
getCounts: async () => ({ waiting: 0, active: 0, delayed: 0, failed: 0, completed: 0 }),
|
||||
start: () => {},
|
||||
isStarted: () => false,
|
||||
close: async () => {
|
||||
closedNames.push(name);
|
||||
},
|
||||
}) as unknown as ServerJobQueue<ServerGenerationJobPayload>;
|
||||
|
||||
const queues = new Map<ServerGenerationJobKind, ServerJobQueue<ServerGenerationJobPayload>>();
|
||||
queues.set('event', make('event'));
|
||||
queues.set('event-batch', make('event-batch'));
|
||||
queues.set('summary', make('summary'));
|
||||
queues.set('reindex', make('reindex'));
|
||||
return { queues, closedNames };
|
||||
}
|
||||
|
||||
describe('ActiveServerBetaQueueManager', () => {
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
it('refuses construction when engine is not bullmq', () => {
|
||||
expect(() => new ActiveServerBetaQueueManager(sqliteConfig)).toThrow(/CLAUDE_MEM_QUEUE_ENGINE=bullmq/);
|
||||
});
|
||||
|
||||
it('reports active health with all four lanes when constructed against bullmq', () => {
|
||||
const { queues } = buildStubQueues();
|
||||
const manager = new ActiveServerBetaQueueManager(bullmqConfig, queues);
|
||||
const health = manager.getHealth();
|
||||
expect(health.status).toBe('active');
|
||||
expect(health.details?.engine).toBe('bullmq');
|
||||
const lanes = health.details?.lanes as Array<{ kind: string; name: string }> | undefined;
|
||||
expect(lanes?.map((l) => l.kind).sort()).toEqual(['event', 'event-batch', 'reindex', 'summary']);
|
||||
});
|
||||
|
||||
it('exposes per-kind queues via getQueue', () => {
|
||||
const { queues } = buildStubQueues();
|
||||
const manager = new ActiveServerBetaQueueManager(bullmqConfig, queues);
|
||||
expect(manager.getQueue('event')).toBe(queues.get('event'));
|
||||
expect(manager.getQueue('summary')).toBe(queues.get('summary'));
|
||||
});
|
||||
|
||||
it('closes every queue on close() and reports errored health afterwards', async () => {
|
||||
const { queues, closedNames } = buildStubQueues();
|
||||
const manager = new ActiveServerBetaQueueManager(bullmqConfig, queues);
|
||||
await manager.close();
|
||||
expect(closedNames.sort()).toEqual(['event', 'event-batch', 'reindex', 'summary']);
|
||||
expect(manager.getHealth().status).toBe('errored');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
import { afterEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import { ServerBetaService } from '../../src/server/runtime/ServerBetaService.js';
|
||||
import {
|
||||
DisabledServerBetaEventBroadcaster,
|
||||
DisabledServerBetaGenerationWorkerManager,
|
||||
DisabledServerBetaProviderRegistry,
|
||||
DisabledServerBetaQueueManager,
|
||||
type ServerBetaServiceGraph,
|
||||
} from '../../src/server/runtime/types.js';
|
||||
import { logger } from '../../src/utils/logger.js';
|
||||
|
||||
const loggerSpies: ReturnType<typeof spyOn>[] = [];
|
||||
|
||||
describe('ServerBetaService', () => {
|
||||
let service: ServerBetaService | null = null;
|
||||
|
||||
afterEach(async () => {
|
||||
if (service) {
|
||||
await service.stop();
|
||||
service = null;
|
||||
}
|
||||
loggerSpies.splice(0).forEach(spy => spy.mockRestore());
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
it('serves server-beta runtime labels from independent runtime routes', async () => {
|
||||
loggerSpies.push(
|
||||
spyOn(logger, 'info').mockImplementation(() => {}),
|
||||
spyOn(logger, 'debug').mockImplementation(() => {}),
|
||||
spyOn(logger, 'warn').mockImplementation(() => {}),
|
||||
spyOn(logger, 'error').mockImplementation(() => {}),
|
||||
);
|
||||
|
||||
service = new ServerBetaService({
|
||||
graph: createTestGraph(),
|
||||
port: 0,
|
||||
host: '127.0.0.1',
|
||||
persistRuntimeState: false,
|
||||
});
|
||||
await service.start();
|
||||
const address = service.getRuntimeState();
|
||||
|
||||
const health = await fetch(`http://127.0.0.1:${address.port}/api/health`);
|
||||
expect(health.status).toBe(200);
|
||||
expect((await health.json()).runtime).toBe('server-beta');
|
||||
|
||||
const info = await fetch(`http://127.0.0.1:${address.port}/v1/info`);
|
||||
expect(info.status).toBe(200);
|
||||
const body = await info.json();
|
||||
expect(body.runtime).toBe('server-beta');
|
||||
expect(body.boundaries.queueManager.status).toBe('disabled');
|
||||
});
|
||||
});
|
||||
|
||||
function createTestGraph(): ServerBetaServiceGraph {
|
||||
return {
|
||||
runtime: 'server-beta',
|
||||
postgres: {
|
||||
pool: {
|
||||
end: mock(() => Promise.resolve()),
|
||||
} as any,
|
||||
bootstrap: {
|
||||
initialized: true,
|
||||
schemaVersion: 1,
|
||||
appliedAt: new Date(0).toISOString(),
|
||||
},
|
||||
},
|
||||
authMode: 'local-dev',
|
||||
queueManager: new DisabledServerBetaQueueManager('test'),
|
||||
generationWorkerManager: new DisabledServerBetaGenerationWorkerManager('test'),
|
||||
providerRegistry: new DisabledServerBetaProviderRegistry('test'),
|
||||
eventBroadcaster: new DisabledServerBetaEventBroadcaster('test'),
|
||||
storage: {} as any,
|
||||
};
|
||||
}
|
||||
@@ -1,12 +1,6 @@
|
||||
import { describe, it, expect, mock, beforeEach, afterEach, spyOn } from 'bun:test';
|
||||
import { logger } from '../../src/utils/logger.js';
|
||||
|
||||
mock.module('../../src/services/worker/http/middleware.js', () => ({
|
||||
createMiddleware: () => [],
|
||||
requireLocalhost: (_req: any, _res: any, next: any) => next(),
|
||||
summarizeRequestBody: () => 'test body',
|
||||
}));
|
||||
|
||||
import { Server } from '../../src/services/server/Server.js';
|
||||
import type { RouteHandler, ServerOptions } from '../../src/services/server/Server.js';
|
||||
|
||||
@@ -67,6 +61,40 @@ describe('Server', () => {
|
||||
|
||||
expect(typeof server.app.listen).toBe('function');
|
||||
});
|
||||
|
||||
it('should register pre-body-parser routes before normal middleware', async () => {
|
||||
server = new Server({
|
||||
...mockOptions,
|
||||
preBodyParserRoutes: [{
|
||||
setupRoutes(app) {
|
||||
app.post('/api/auth/*splat', (req, res) => {
|
||||
res.json({
|
||||
bodyParsed: req.body !== undefined,
|
||||
});
|
||||
});
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
const testPort = 40000 + Math.floor(Math.random() * 10000);
|
||||
|
||||
await server.listen(testPort, '127.0.0.1');
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:${testPort}/api/auth/session`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Origin: 'http://localhost:37777',
|
||||
},
|
||||
body: JSON.stringify({ ok: true }),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get('access-control-allow-origin')).toBe('http://localhost:37777');
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.bodyParsed).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listen', () => {
|
||||
@@ -286,6 +314,33 @@ describe('Server', () => {
|
||||
expect(body.pid).toBeDefined();
|
||||
expect(typeof body.pid).toBe('number');
|
||||
});
|
||||
|
||||
it('should return degraded health when BullMQ Redis health is errored', async () => {
|
||||
server = new Server({
|
||||
...mockOptions,
|
||||
getQueueHealth: () => ({
|
||||
engine: 'bullmq',
|
||||
redis: {
|
||||
status: 'error',
|
||||
mode: 'external',
|
||||
host: '127.0.0.1',
|
||||
port: 6379,
|
||||
prefix: 'test_prefix',
|
||||
error: 'connection refused',
|
||||
},
|
||||
}),
|
||||
});
|
||||
const testPort = 40000 + Math.floor(Math.random() * 10000);
|
||||
|
||||
await server.listen(testPort, '127.0.0.1');
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:${testPort}/api/health`);
|
||||
const body = await response.json();
|
||||
|
||||
expect(response.status).toBe(503);
|
||||
expect(body.status).toBe('degraded');
|
||||
expect(body.queue.redis.status).toBe('error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('readiness endpoint', () => {
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import { Database } from 'bun:sqlite';
|
||||
import { Server, type ServerOptions } from '../../src/services/server/Server.js';
|
||||
import { ServerV1Routes } from '../../src/server/routes/v1/ServerV1Routes.js';
|
||||
import { createServerApiKey } from '../../src/server/auth/api-key-service.js';
|
||||
import { logger } from '../../src/utils/logger.js';
|
||||
|
||||
let loggerSpies: ReturnType<typeof spyOn>[] = [];
|
||||
|
||||
describe('server REST API v1 routes', () => {
|
||||
let db: Database;
|
||||
let server: Server;
|
||||
let port: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
loggerSpies = [
|
||||
spyOn(logger, 'info').mockImplementation(() => {}),
|
||||
spyOn(logger, 'debug').mockImplementation(() => {}),
|
||||
spyOn(logger, 'warn').mockImplementation(() => {}),
|
||||
spyOn(logger, 'error').mockImplementation(() => {}),
|
||||
];
|
||||
db = new Database(':memory:');
|
||||
db.run('PRAGMA foreign_keys = ON');
|
||||
const options: ServerOptions = {
|
||||
getInitializationComplete: () => true,
|
||||
getMcpReady: () => true,
|
||||
onShutdown: mock(() => Promise.resolve()),
|
||||
onRestart: mock(() => Promise.resolve()),
|
||||
workerPath: '/test/worker-service.cjs',
|
||||
getAiStatus: () => ({
|
||||
provider: 'claude',
|
||||
authMethod: 'cli',
|
||||
lastInteraction: null,
|
||||
}),
|
||||
};
|
||||
server = new Server(options);
|
||||
server.registerRoutes(new ServerV1Routes({
|
||||
getDatabase: () => db,
|
||||
authMode: 'local-dev',
|
||||
allowLocalDevBypass: true,
|
||||
}));
|
||||
server.finalizeRoutes();
|
||||
await server.listen(0, '127.0.0.1');
|
||||
const address = server.getHttpServer()?.address();
|
||||
if (!address || typeof address === 'string') {
|
||||
throw new Error('Expected server to bind to an ephemeral TCP port');
|
||||
}
|
||||
port = address.port;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
try {
|
||||
await server.close();
|
||||
} catch (error: any) {
|
||||
if (error?.code !== 'ERR_SERVER_NOT_RUNNING') {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
db.close();
|
||||
loggerSpies.forEach(spy => spy.mockRestore());
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
it('creates projects, sessions, events, memories, and searchable context', async () => {
|
||||
const projectResponse = await post('/v1/projects', {
|
||||
name: 'Claude Mem',
|
||||
rootPath: '/tmp/claude-mem',
|
||||
});
|
||||
expect(projectResponse.status).toBe(201);
|
||||
const { project } = await projectResponse.json();
|
||||
|
||||
const sessionResponse = await post('/v1/sessions/start', {
|
||||
projectId: project.id,
|
||||
memorySessionId: 'memory-1',
|
||||
});
|
||||
expect(sessionResponse.status).toBe(201);
|
||||
const { session } = await sessionResponse.json();
|
||||
|
||||
const eventResponse = await post('/v1/events', {
|
||||
projectId: project.id,
|
||||
serverSessionId: session.id,
|
||||
sourceType: 'api',
|
||||
eventType: 'observation.created',
|
||||
payload: { type: 'learned' },
|
||||
occurredAtEpoch: Date.now(),
|
||||
});
|
||||
expect(eventResponse.status).toBe(201);
|
||||
|
||||
const memoryResponse = await post('/v1/memories', {
|
||||
projectId: project.id,
|
||||
serverSessionId: session.id,
|
||||
kind: 'manual',
|
||||
type: 'note',
|
||||
title: 'Queue backend',
|
||||
narrative: 'BullMQ keeps deployable server queues in Valkey.',
|
||||
facts: ['BullMQ mode requires Redis or Valkey'],
|
||||
});
|
||||
expect(memoryResponse.status).toBe(201);
|
||||
const { memory } = await memoryResponse.json();
|
||||
|
||||
const searchResponse = await post('/v1/search', {
|
||||
projectId: project.id,
|
||||
query: 'BullMQ',
|
||||
});
|
||||
expect(searchResponse.status).toBe(200);
|
||||
const search = await searchResponse.json();
|
||||
expect(search.memories.map((item: any) => item.id)).toContain(memory.id);
|
||||
|
||||
const stemmedSearchResponse = await post('/v1/search', {
|
||||
projectId: project.id,
|
||||
query: 'queue',
|
||||
});
|
||||
expect(stemmedSearchResponse.status).toBe(200);
|
||||
const stemmedSearch = await stemmedSearchResponse.json();
|
||||
expect(stemmedSearch.memories.map((item: any) => item.id)).toContain(memory.id);
|
||||
|
||||
const contextResponse = await post('/v1/context', {
|
||||
projectId: project.id,
|
||||
query: 'Valkey',
|
||||
});
|
||||
expect(contextResponse.status).toBe(200);
|
||||
const context = await contextResponse.json();
|
||||
expect(context.context).toContain('Valkey');
|
||||
|
||||
const endResponse = await post(`/v1/sessions/${session.id}/end`, {});
|
||||
expect(endResponse.status).toBe(200);
|
||||
expect((await endResponse.json()).session.status).toBe('completed');
|
||||
});
|
||||
|
||||
it('denies writes when an API key lacks write scope', async () => {
|
||||
const key = createServerApiKey(db, {
|
||||
name: 'read only',
|
||||
scopes: ['memories:read'],
|
||||
});
|
||||
const response = await fetch(`http://127.0.0.1:${port}/v1/projects`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${key.rawKey}`,
|
||||
},
|
||||
body: JSON.stringify({ name: 'Denied' }),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
});
|
||||
|
||||
it('denies project creation when an API key is scoped to an existing project', async () => {
|
||||
const projectResponse = await post('/v1/projects', { name: 'Owner Project' });
|
||||
expect(projectResponse.status).toBe(201);
|
||||
const { project } = await projectResponse.json();
|
||||
const key = createServerApiKey(db, {
|
||||
name: 'project scoped writer',
|
||||
projectId: project.id,
|
||||
scopes: ['memories:write'],
|
||||
});
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:${port}/v1/projects`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${key.rawKey}`,
|
||||
},
|
||||
body: JSON.stringify({ name: 'Forbidden Project' }),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
const row = db.prepare('SELECT COUNT(*) AS count FROM projects').get() as { count: number };
|
||||
expect(row.count).toBe(1);
|
||||
});
|
||||
|
||||
it('limits project listing to the API key project scope', async () => {
|
||||
const projectAResponse = await post('/v1/projects', { name: 'Scoped Project A' });
|
||||
const projectBResponse = await post('/v1/projects', { name: 'Scoped Project B' });
|
||||
expect(projectAResponse.status).toBe(201);
|
||||
expect(projectBResponse.status).toBe(201);
|
||||
const { project: projectA } = await projectAResponse.json();
|
||||
await projectBResponse.json();
|
||||
const key = createServerApiKey(db, {
|
||||
name: 'project A reader',
|
||||
projectId: projectA.id,
|
||||
scopes: ['memories:read'],
|
||||
});
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:${port}/v1/projects`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${key.rawKey}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const body = await response.json();
|
||||
expect(body.projects.map((project: any) => project.id)).toEqual([projectA.id]);
|
||||
});
|
||||
|
||||
it('rejects mixed-project event batches without partial writes', async () => {
|
||||
const projectAResponse = await post('/v1/projects', { name: 'Project A' });
|
||||
const projectBResponse = await post('/v1/projects', { name: 'Project B' });
|
||||
expect(projectAResponse.status).toBe(201);
|
||||
expect(projectBResponse.status).toBe(201);
|
||||
const { project: projectA } = await projectAResponse.json();
|
||||
const { project: projectB } = await projectBResponse.json();
|
||||
const key = createServerApiKey(db, {
|
||||
name: 'project A writer',
|
||||
projectId: projectA.id,
|
||||
scopes: ['memories:write'],
|
||||
});
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:${port}/v1/events/batch`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${key.rawKey}`,
|
||||
},
|
||||
body: JSON.stringify([
|
||||
{
|
||||
projectId: projectA.id,
|
||||
sourceType: 'api',
|
||||
eventType: 'observation.created',
|
||||
payload: { index: 1 },
|
||||
occurredAtEpoch: Date.now(),
|
||||
},
|
||||
{
|
||||
projectId: projectB.id,
|
||||
sourceType: 'api',
|
||||
eventType: 'observation.created',
|
||||
payload: { index: 2 },
|
||||
occurredAtEpoch: Date.now(),
|
||||
},
|
||||
]),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
const row = db.prepare('SELECT COUNT(*) AS count FROM agent_events').get() as { count: number };
|
||||
expect(row.count).toBe(0);
|
||||
});
|
||||
|
||||
it('rejects memory updates that move records across projects', async () => {
|
||||
const projectAResponse = await post('/v1/projects', { name: 'Memory Project A' });
|
||||
const projectBResponse = await post('/v1/projects', { name: 'Memory Project B' });
|
||||
expect(projectAResponse.status).toBe(201);
|
||||
expect(projectBResponse.status).toBe(201);
|
||||
const { project: projectA } = await projectAResponse.json();
|
||||
const { project: projectB } = await projectBResponse.json();
|
||||
const memoryResponse = await post('/v1/memories', {
|
||||
projectId: projectA.id,
|
||||
kind: 'manual',
|
||||
type: 'note',
|
||||
title: 'Pinned project',
|
||||
});
|
||||
expect(memoryResponse.status).toBe(201);
|
||||
const { memory } = await memoryResponse.json();
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:${port}/v1/memories/${memory.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
projectId: projectB.id,
|
||||
kind: 'manual',
|
||||
type: 'note',
|
||||
}),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
const stored = db.prepare('SELECT project_id FROM memory_items WHERE id = ?').get(memory.id) as { project_id: string };
|
||||
expect(stored.project_id).toBe(projectA.id);
|
||||
});
|
||||
|
||||
async function post(path: string, body: unknown): Promise<Response> {
|
||||
return fetch(`http://127.0.0.1:${port}${path}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user