thedotmack/claude-mem · critical

InternalError

InternalError

Error message

Failed to persist event

What it means

A 500 InternalError returned when persisting an agent/event fails for any reason other than the recognized tenant-scope violations handled at line 1379. The raw error is logged server-side (logger.error) and a generic message is returned so internal details are not leaked.

Solutions

  1. Check server logs for the `${action} failed` entry to get the underlying DB error.
  2. Retry the request — transient connection/pool failures usually resolve on retry with backoff.
  3. Verify the DB schema matches the deployed code version (run pending migrations).
  4. If persistent, check Postgres health, connection limits, and pool configuration.

Example fix

// client-side: retry transient 500s with backoff
for (const delay of [100, 500, 2000]) {
  const res = await post('/v1/events', payload);
  if (res.status !== 500) return res;
  await sleep(delay);
}
Defensive patterns

Strategy: retry

Validate before calling

null

Type guard

null

Try / catch

async function postEventWithRetry(payload: unknown, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    const res = await post('/v1/events', payload);
    if (res.status !== 500) return res;
    await new Promise(r => setTimeout(r, 2 ** i * 250));
  }
  throw new Error('event persist failed after retries — check server logs for `${action} failed`');
}

Prevention

When it happens

Trigger: Any DB failure during an event write that is not an ownership-constraint message: connection loss, constraint violation outside the three known messages, schema mismatch, pool exhaustion, serialization/deadlock failure during event INSERT.

Common situations: Postgres restart or network blip mid-request; schema migration mismatch between deployed code and DB; connection pool exhausted under load; invalid enum value for event type added by a newer client against an older schema.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/2295accf207a5c20. Report an issue: GitHub.

Appendix: source

Thrown at src/server/routes/v1/ServerV1PostgresRoutes.ts:1383

    const byTeam = await this.options.pool.query(
      `DELETE FROM observations WHERE id = $1 AND team_id = $2`,
      [id, teamId],
    );
    return (byTeam.rowCount ?? 0) > 0;
  }

  private handleDbError(error: unknown, res: Response, action: string): void {
    const message = error instanceof Error ? error.message : String(error);
    if (
      message.includes('project_id must belong to team_id')
      || message.includes('server_session_id must belong')
      || message.includes('agent_event source_id must belong')
    ) {
      res.status(403).json({ error: 'Forbidden', message });
      return;
    }
    logger.error('SYSTEM', `${action} failed`, { error: message });
    res.status(500).json({ error: 'InternalError', message: 'Failed to persist event' });
  }

  private async auditWrite(
    req: Request,
    action: string,
    targetId: string | null,
    projectId: string | null,
    details?: Record<string, unknown>,
  ): Promise<void> {
    const repo = new PostgresAuthRepository(this.options.pool);
    const actorId = await this.resolveActorId(req);
    // Phase 12 — every audit row carries request_id when one was minted
    // so dashboards and incident triage can pivot from a single HTTP
    // request to every ingest/job/audit row it produced. Caller-supplied
    // details win on key conflict so explicit overrides still work.
    const detailsWithRequestId: Record<string, unknown> = {
      ...(req.requestId ? { requestId: req.requestId } : {}),
      ...(details ?? {}),

View on GitHub (pinned to d8bc9755e7)