feat(minder): split agent sessions by the project they worked in
Description
This change splits an agent session into one GitLab session for each project the agent worked in. glab sends each prompt, tool call and response to the project of the folder the agent was working in when it recorded the entry. A session that works in several projects shows up in each of them, with only the activity from that project. Each GitLab session uses the agent session ID and the project ID as its idempotency key, and the agent session ID as its parent key. Activity from before an agent was set up is left out before the split, so it never reaches any project.
A folder a hook ran in, and its subfolders in the same Git repository, keep the project the hook resolved for it. A session that stays in one repository syncs to the same project as before. A repository cloned inside it, and any other folder, uses its origin remote. Entries without a folder go to the session's project. Activity in a folder without a GitLab project is not uploaded. A tool result goes with its call, even when the call was synced earlier.
A session's activity stays within the top-level group of the first project the session was recorded with, or, for a session recorded by an earlier glab, the project it was recorded with. glab keeps that project in the session record and never changes it. Every entry, including entries in folders where the hooks ran, goes to its folder's project only when that project is on the same GitLab host and in the same top-level group. Otherwise its activity is not uploaded, and it is listed as not synced. This means a session in one organization's project cannot upload prompts or tool calls to another organization's project because the agent changed to a clone of it, even though Claude Code's hooks follow the agent there. The setup prompt now says that a session can be split across projects in that group, and that the local record also holds the folders the hooks ran in and their projects.
Sessions recorded by an earlier glab keep their existing GitLab session, keyed by the agent session ID, for the project they were syncing to. The session record lists the GitLab session created for each project.
The sync cursor moves once every project's events are posted. A project refuses its events when AI governance is off there, or when GitLab answers 403 and another project on the same host was posted. A refused project's events are dropped only when another project's events were posted. A session that no project accepts waits, as before, so it uploads once governance is turned on. A folder whose remote names a missing project other than the session's own is dropped. An instance that does not accept the agent type stops only that project's part. When a session ends or goes idle, glab completes the GitLab session of every project it synced to. The errors in glab minder doctor and in a sync run by hand name the project that was not synced.
The help for glab minder audit sync and glab minder setup explains how the project of each entry is chosen. It also notes that the folder is where the agent's shell was, not always the project it acted on.
It builds on !4528 (merged), which added the router, the locked session records and the Codex folder lookup. Reusing each project's GitLab session is in !4529 (merged).
Related to https://gitlab.com/gitlab-org/cli/-/work_items/8722
How has this been tested?
Unit tests run against a fake GitLab, on both the hook path and the --all path. They cover:
- A Claude Code session that moves between projects.
- A subfolder of the folder a hook ran in, and a repository cloned inside it.
- Codex shell calls in other folders.
- A folder with no project, and a folder whose project does not exist.
- A project that refuses its part while another one syncs, and a session that no project accepts.
- A tool result synced after its call, and an instance without the agent type.
- A session recorded by an earlier glab.
- Completion of every project, and a session with no activity.
go test ./internal/commands/govern/..., the race detector on the sync package, golangci-lint, the comment overlap check and make gen-docs all pass.