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

6.6 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0

import type { JsonObject, JsonValue, PostgresQueryable } from './utils.js';
import {
  assertProjectOwnership,
  assertSessionOwnership,
  canonicalJson,
  deterministicKey,
  newId,
  queryOne,
  toEpoch,
  stripNul,
  toJsonObject
} from './utils.js';
import { normalizePlatformSourceOrNull } from '../../shared/platform-source.js';
import { redactJsonStrings } from '../../utils/redaction.js';
import { stripMemoryTagsFromJsonStrings } from '../../utils/tag-stripping.js';

export interface PostgresAgentEvent {
  id: string;
  projectId: string;
  teamId: string;
  serverSessionId: string | null;
  sourceAdapter: string;
  sourceEventId: string | null;
  idempotencyKey: string;
  eventType: string;
  platformSource: string | null;
  payload: JsonValue;
  metadata: JsonObject;
  occurredAtEpoch: number;
  receivedAtEpoch: number;
  createdAtEpoch: number;
}

export interface CreatePostgresAgentEventInput {
  id?: string;
  projectId: string;
  teamId: string;
  serverSessionId?: string | null;
  contentSessionId?: string | null;
  sourceAdapter: string;
  sourceEventId?: string | null;
  eventType: string;
  // #2560 — which platform produced the event (claude-code, opencode, ...).
  // Persisted on agent_events for plan-09 scoping. Optional; null when unknown.
  platformSource?: string | null;
  payload?: JsonValue;
  metadata?: JsonObject;
  occurredAt: Date | string | number;
}

interface AgentEventRow {
  id: string;
  project_id: string;
  team_id: string;
  server_session_id: string | null;
  source_adapter: string;
  source_event_id: string | null;
  idempotency_key: string;
  event_type: string;
  platform_source: string | null;
  payload: unknown;
  metadata: unknown;
  occurred_at: Date;
  received_at: Date;
  created_at: Date;
}

export class PostgresAgentEventsRepository {
  constructor(private client: PostgresQueryable) {}

  async create(input: CreatePostgresAgentEventInput): Promise<PostgresAgentEvent> {
    await assertProjectOwnership(this.client, input.projectId, input.teamId);
    if (input.serverSessionId) {
      await assertSessionOwnership(this.client, input.serverSessionId, input.projectId, input.teamId);
    }
    const idempotencyKey = buildAgentEventIdempotencyKey(input);
    const platformSource = normalizePlatformSourceOrNull(input.platformSource);
    const row = await queryOne<AgentEventRow>(
      this.client,
      `
        INSERT INTO agent_events (
          id, project_id, team_id, server_session_id, source_adapter,
          source_event_id, idempotency_key, event_type, platform_source, payload, metadata, occurred_at
        )
        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::jsonb, $11::jsonb, $12)
        ON CONFLICT (idempotency_key) DO UPDATE SET
          server_session_id = COALESCE(agent_events.server_session_id, excluded.server_session_id),
          metadata = agent_events.metadata || excluded.metadata,
          platform_source = COALESCE(excluded.platform_source, agent_events.platform_source)
        RETURNING *
      `,
      [
        input.id ?? newId(),
        input.projectId,
        input.teamId,
        input.serverSessionId ?? null,
        input.sourceAdapter,
        input.sourceEventId ?? null,
        idempotencyKey,
        input.eventType,
        platformSource,
        // <private> sections are removed and opt-in secret redaction (#2616)
        // applied to the raw event body, at the one write every ingest path
        // goes through, so a client that did not strip still stores none.
        JSON.stringify(redactJsonStrings(stripMemoryTagsFromJsonStrings(stripNul(input.payload ?? {})))),
        JSON.stringify(redactJsonStrings(stripMemoryTagsFromJsonStrings(stripNul(input.metadata ?? {})))),
        new Date(input.occurredAt)
      ]
    );
    return mapAgentEventRow(row!);
  }

  async getByIdForScope(input: {
    id: string;
    projectId: string;
    teamId: string;
  }): Promise<PostgresAgentEvent | null> {
    const row = await queryOne<AgentEventRow>(
      this.client,
      'SELECT * FROM agent_events WHERE id = $1 AND project_id = $2 AND team_id = $3',
      [input.id, input.projectId, input.teamId]
    );
    return row ? mapAgentEventRow(row) : null;
  }

  async listByProject(input: {
    projectId: string;
    teamId: string;
    serverSessionId?: string | null;
    limit?: number;
  }): Promise<PostgresAgentEvent[]> {
    const result = await this.client.query<AgentEventRow>(
      `
        SELECT * FROM agent_events
        WHERE project_id = $1
          AND team_id = $2
          AND ($3::text IS NULL OR server_session_id = $3)
        ORDER BY occurred_at DESC
        LIMIT $4
      `,
      [input.projectId, input.teamId, input.serverSessionId ?? null, input.limit ?? 100]
    );
    return result.rows.map(mapAgentEventRow);
  }
}

export function buildAgentEventIdempotencyKey(input: {
  teamId: string;
  projectId: string;
  sourceAdapter: string;
  sourceEventId?: string | null;
  serverSessionId?: string | null;
  contentSessionId?: string | null;
  eventType: string;
  platformSource?: string | null;
  occurredAt: Date | string | number;
  payload?: JsonValue;
}): string {
  const platformSource = normalizePlatformSourceOrNull(input.platformSource);
  const platformScope = platformSource ? [platformSource] : [];

  if (input.sourceEventId) {
    return `agent_event:v1:${deterministicKey([
      input.teamId,
      input.projectId,
      input.sourceAdapter,
      ...platformScope,
      input.sourceEventId
    ])}`;
  }

  // Use contentSessionId (stable, client-provided) over serverSessionId, which is
  // resolved lazily at ingest and can be NULL on a first delivery but non-NULL on a
  // retry — that would drift the key and create duplicate events. Falls back to
  // serverSessionId for events that don't carry a contentSessionId.
  return `agent_event:v1:${deterministicKey([
    input.teamId,
    input.projectId,
    input.sourceAdapter,
    ...platformScope,
    input.contentSessionId ?? input.serverSessionId ?? null,
    input.eventType,
    new Date(input.occurredAt).toISOString(),
    canonicalJson(input.payload ?? {})
  ])}`;
}

function mapAgentEventRow(row: AgentEventRow): PostgresAgentEvent {
  return {
    id: row.id,
    projectId: row.project_id,
    teamId: row.team_id,
    serverSessionId: row.server_session_id,
    sourceAdapter: row.source_adapter,
    sourceEventId: row.source_event_id,
    idempotencyKey: row.idempotency_key,
    eventType: row.event_type,
    platformSource: row.platform_source,
    payload: row.payload,
    metadata: toJsonObject(row.metadata),
    occurredAtEpoch: toEpoch(row.occurred_at),
    receivedAtEpoch: toEpoch(row.received_at),
    createdAtEpoch: toEpoch(row.created_at)
  };
}