Update the external agent session API to use ai_governance_sessions

What does this MR do and why?

Update the external agent session API to write to the new ai_governance_sessions

The existing parameters like are being kept like json sha, goal etc as those are what the client side passes, the intent is to move the sessions table towards this so that these can be stored there as well - but the changes are functional without them.

Database Review

All queries are against ai_governance_sessions, this is a new empty prod table.

lookups

Session lookup by ID (PATCH /ai_agent/sessions/:id, POST /ai_agent/audit_events)

SELECT * FROM ai_governance_sessions
WHERE source = 2 AND namespace_id = ? AND id = ?

Plan: PK lookup on ai_governance_sessions_pkey, source/namespace_id filtered as post-conditions on a single row.

Index Scan using ai_governance_sessions_pkey
  Index Cond: (id = ?)
  Filter: ((source = 2) AND (namespace_id = ?))
Planning Time: 0.055ms  Execution Time: 0.010ms

Session lookup by external_xid (POST /ai_agent/sessions idempotency check)

SELECT * FROM ai_governance_sessions
WHERE source = 2 AND namespace_id = ? AND external_xid = ? AND user_id = ?

Plan: unique index i_ai_governance_sessions_on_namespace_source_external_xid on (namespace_id, source, external_xid). user_id is a post-index filter on 0–1 rows.

Index Scan using i_ai_governance_sessions_on_namespace_source_external_xid
  Index Cond: ((namespace_id = ?) AND (source = 2) AND (external_xid = ?))
  Filter: (user_id = ?)
Planning Time: 0.057ms  Execution Time: 0.009ms

Listing endpoint

Session list (GET /ai_agent/sessions)

SELECT * FROM ai_governance_sessions
WHERE source = 2 AND namespace_id = ? AND project_id = ? AND user_id = ?
-- optional filters:
AND agent_type = ?
AND status = ?
AND session_started_at >= ?
AND session_started_at <= ?
ORDER BY id DESC

Plan: user_id index scan, remaining conditions filtered post-index. In-memory quicksort (25kB). Acceptable for Stage 1 where each user has few sessions.

Sort (cost=3.21..3.21 rows=1 width=172)
  Sort Key: id DESC
  Sort Method: quicksort  Memory: 25kB
  -> Index Scan using index_ai_governance_sessions_on_user_id
       Index Cond: (user_id = ?)
       Filter: ((namespace_id = ?) AND (source = 2) AND (project_id = ?))
Planning Time: 1.694ms  Execution Time: 0.046ms

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #630013

Edited by Jean van der Walt

Merge request reports

Loading
Loading