Fix ClickHouse traversal_path duplication for AI audit events

What does this MR do and why?

AI audit events (GitLab Duo agent actions) are written to the ClickHouse table ai_audit_events. Before this MR, the row did not include traversal_path. ClickHouse filled it with a column default. That default looks up the path in a dictionary (an in-memory lookup table) by project_id, then group_id, else '0/'.

If the dictionary does not have the project yet, the row gets '0/'. This can happen for a new project, before the dictionary refreshes. A later write of the same event gets the real path. traversal_path is part of the table's sort key, and the table only removes duplicates when the sort key matches. So both copies are kept, and the audit events finder shows the same event twice.

The fix computes traversal_path in Ruby and sends it with the row:

  • Project event: the project namespace's traversal path.
  • Group event: the group's traversal path.
  • Otherwise: '0/', same as the ClickHouse default.

The same event now always gets the same value. The project is batch-loaded with its project namespace preloaded, so there is no extra query per event.

Specs cover a project, a group, both ids blank, and a project id that no longer exists (gives '0/').

References

Screenshots or screen recordings

Not applicable. This is a backend-only change.

How to set up and validate locally

  1. In a Rails console, build an event and check the row value:

    project = Project.last
    event = AuditEvents::AiAuditEvent.new(project_id: project.id, event_name: 'test', created_at: Time.current)
    event.to_clickhouse_csv_row[:traversal_path]
    # => same as project.project_namespace.traversal_path(with_organization: true)
  2. For a group event, check that this equals Group.last.traversal_path(with_organization: true):

    AuditEvents::AiAuditEvent.new(namespace_id: Group.last.id, event_name: 'test', created_at: Time.current).to_clickhouse_csv_row[:traversal_path]
  3. Run the spec:

    bin/rspec ee/spec/models/audit_events/ai_audit_event_spec.rb

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.

Edited by Hitesh Raghuvanshi

Merge request reports

Loading
Loading