thedotmack/claude-mem · error

agent_event_id must belong to project_id and team_id

Error message

agent_event_id must belong to project_id and team_id

What it means

Thrown by assertAgentEventOwnership (private helper) when addSource is called with sourceType 'agent_event'. It looks up agent_events by (id=sourceId, project_id, team_id); a missing row means the referenced event is not in scope for that tenant.

Source

Thrown at src/storage/postgres/observations.ts:372

  }
  if (input.sourceType === 'agent_event' && row.agent_event_id !== input.agentEventId) {
    throw new Error('generation_job_id agent_event_id must match observation source');
  }
}

async function assertAgentEventOwnership(
  client: PostgresQueryable,
  agentEventId: string,
  projectId: string,
  teamId: string
): Promise<void> {
  const row = await queryOne<{ id: string }>(
    client,
    'SELECT id FROM agent_events WHERE id = $1 AND project_id = $2 AND team_id = $3',
    [agentEventId, projectId, teamId]
  );
  if (!row) {
    throw new Error('agent_event_id must belong to project_id and team_id');
  }
}

async function assertObservationOwnership(
  client: PostgresQueryable,
  observationId: string,
  projectId: string,
  teamId: string
): Promise<void> {
  const row = await queryOne<{ id: string }>(
    client,
    'SELECT id FROM observations WHERE id = $1 AND project_id = $2 AND team_id = $3',
    [observationId, projectId, teamId]
  );
  if (!row) {
    throw new Error('observation_reindex source_id must belong to project_id and team_id');
  }
}

View on GitHub (pinned to d768ba3643)

Solutions

  1. Verify the agent_event exists in scope before adding it as a source.
  2. Confirm the event was ingested under the same project/team.
  3. Use the agent_event id exactly as stored in agent_events.

Example fix

// before
await sources.addSource({ observationId, projectId, teamId, sourceType:'agent_event', sourceId: maybeWrongId });
// after
const evt = await agentEvents.getByIdForScope({ id: sourceId, projectId, teamId });
if (!evt) throw new Error('agent_event not in scope');
await sources.addSource({ observationId, projectId, teamId, sourceType:'agent_event', sourceId });
Defensive patterns

Strategy: validation

Validate before calling

const evt = await agentEvents.getByIdForScope({ id: sourceId, projectId, teamId });
if (!evt) throw new Error('agent_event not in scope');

Prevention

When it happens

Trigger: Calling addSource({sourceType:'agent_event', sourceId}) where the agent_event does not exist for the supplied project/team. Fires after the sourceId==agentEventId invariant check passes.

Common situations: Referencing an event from another tenant; event expired/deleted; wrong project/team id threaded through; using an external id instead of the stored agent_event id.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d768ba3643 (2026-08-12). Data as JSON: /api/errors/66cb2af93a9fbb5f. Report an issue: GitHub.