//go:build !integration
package sync
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"gitlab.com/gitlab-org/cli/internal/commands/govern/internal/fallbacksync"
"gitlab.com/gitlab-org/cli/internal/commands/govern/internal/syncstate"
)
// writesStdout mocks Executor.ExecWithIO for a command that prints out.
func writesStdout(out string) func(context.Context, string, []string, []string, io.Reader, io.Writer, io.Writer) error {
return func(_ context.Context, _ string, _, _ []string, _ io.Reader, stdout, _ io.Writer) error {
_, err := io.WriteString(stdout, out)
return err
}
}
// jsonLines encodes records as JSONL.
func jsonLines(t *testing.T, records ...any) string {
t.Helper()
var b strings.Builder
for _, r := range records {
line, err := json.Marshal(r)
require.NoError(t, err)
b.Write(line)
b.WriteByte('\n')
}
return b.String()
}
func codexSessionMetaLine(id, cwd, remote string) map[string]any {
payload := map[string]any{"id": id, "session_id": id, "timestamp": "2026-10-09T01:00:00Z", "cwd": cwd, "cli_version": "0.163.0"}
if remote != "" {
payload["git"] = map[string]any{"repository_url": remote}
}
return map[string]any{"timestamp": "2026-10-09T01:00:00Z", "type": "session_meta", "payload": payload}
}
// recentTimestamp is within the 90 days GitLab accepts events from, for
// tests that sync through --all.
func recentTimestamp() string {
return time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
}
func codexItem(ts string, payload map[string]any) map[string]any {
return map[string]any{"timestamp": ts, "type": "response_item", "payload": payload}
}
// writeCodexRollout writes a Codex rollout under HOME/.codex/sessions and
// returns its path.
func writeCodexRollout(t *testing.T, home, id, content string) string {
t.Helper()
path := filepath.Join(home, ".codex", "sessions", "2026", "10", "09", "rollout-2026-10-09T01-00-00-"+id+".jsonl")
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
return path
}
func TestCodexTranscript_Read(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "rollout.jsonl")
complete := jsonLines(t,
codexSessionMetaLine("th-1", "/work", ""),
codexItem("2026-10-09T01:00:01Z", map[string]any{"type": "message", "role": "user", "content": []any{map[string]any{"type": "input_text", "text": "# AGENTS.md instructions"}}}),
map[string]any{"timestamp": "2026-10-09T01:00:01Z", "type": "event_msg", "payload": map[string]any{"type": "user_message", "message": "fix the flaky test"}},
codexItem("2026-10-09T01:00:02Z", map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1", "arguments": "{}"}),
codexItem("2026-10-09T01:00:03Z", map[string]any{"type": "function_call_output", "call_id": "call_1"}),
codexItem("2026-10-09T01:00:04Z", map[string]any{"type": "custom_tool_call", "name": "apply_patch", "call_id": "call_2", "input": ""}),
codexItem("2026-10-09T01:00:05Z", map[string]any{"type": "local_shell_call", "id": "ls_1", "status": "completed"}),
)
partial := `{"timestamp":"2026-10-09T01:00:06Z","type":"response_item","payload":{"type":"function_call","name":"later","call_id":"call_3"`
require.NoError(t, os.WriteFile(path, []byte(complete+partial), 0o644))
src := codexTranscript{path: path}
data, offset, err := src.read(t.Context(), 0)
require.NoError(t, err)
assert.Equal(t, "fix the flaky test", data.Goal, "the typed prompt, not injected instructions")
assert.Equal(t, time.Date(2026, 10, 9, 1, 0, 0, 0, time.UTC), data.StartedAt)
require.Len(t, data.ToolCalls, 3)
assert.Equal(t, toolCall{ID: "call_1", Name: "exec_command", Input: json.RawMessage(`{}`), Timestamp: time.Date(2026, 10, 9, 1, 0, 2, 0, time.UTC), CWD: "/work"}, data.ToolCalls[0])
assert.Equal(t, "apply_patch", data.ToolCalls[1].Name)
assert.Equal(t, toolCall{ID: "ls_1", Name: "shell", Timestamp: time.Date(2026, 10, 9, 1, 0, 5, 0, time.UTC), CWD: "/work"}, data.ToolCalls[2])
assert.Equal(t, int64(len(complete)), offset, "a line still being written is left for the next read")
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o644)
require.NoError(t, err)
_, err = f.WriteString("}}\n")
require.NoError(t, err)
require.NoError(t, f.Close())
data, _, err = src.read(t.Context(), offset)
require.NoError(t, err)
require.Len(t, data.ToolCalls, 1)
assert.Equal(t, "call_3", data.ToolCalls[0].ID)
}
func TestCursorTranscript_Read(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "conv-1.jsonl")
content := jsonLines(t,
map[string]any{"role": "user", "message": map[string]any{"content": []any{map[string]any{"type": "text", "text": "Thursday\n\nlist the files\n"}}}},
map[string]any{"role": "assistant", "message": map[string]any{"content": []any{
map[string]any{"type": "text", "text": "Listing."},
map[string]any{"type": "tool_use", "name": "Shell", "input": map[string]any{"command": "ls"}},
map[string]any{"type": "tool_use", "name": "ReadFile", "input": map[string]any{}},
}}},
map[string]any{"type": "turn_ended", "status": "success"},
)
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
modified := time.Date(2026, 10, 9, 2, 0, 0, 0, time.UTC)
require.NoError(t, os.Chtimes(path, modified, modified))
src := cursorTranscript{sessionID: "conv-1", path: path}
data, offset, err := src.read(t.Context(), 0)
require.NoError(t, err)
assert.Equal(t, "list the files", data.Goal)
require.Len(t, data.ToolCalls, 2)
assert.Equal(t, "Shell", data.ToolCalls[0].Name)
assert.Equal(t, "ReadFile", data.ToolCalls[1].Name)
assert.NotEqual(t, data.ToolCalls[0].ID, data.ToolCalls[1].ID)
assert.True(t, modified.Equal(data.ToolCalls[0].Timestamp), "Cursor records no timestamps, so the transcript's modification time is used")
assert.Equal(t, int64(len(content)), offset)
again, _, err := src.read(t.Context(), 0)
require.NoError(t, err)
assert.Equal(t, data.ToolCalls[0].ID, again.ToolCalls[0].ID, "IDs are stable, so re-posting is idempotent")
}
func TestWorkspaceCandidates(t *testing.T) {
t.Parallel()
root := t.TempDir()
for _, dir := range []string{"Users/jean/code/ai-tracking", "Users/jean/code/my.repo_v2", "Users/jean/other"} {
require.NoError(t, os.MkdirAll(filepath.Join(root, dir), 0o755))
}
got := workspaceCandidates(root, "Users-jean-code-ai-tracking")
require.Len(t, got, 1)
assert.Equal(t, filepath.Join(root, "Users", "jean", "code", "ai-tracking"), got[0])
got = workspaceCandidates(root, "Users-jean-code-my-repo-v2")
require.Len(t, got, 1, "dots and underscores are encoded as dashes")
assert.Equal(t, filepath.Join(root, "Users", "jean", "code", "my.repo_v2"), got[0])
assert.Empty(t, workspaceCandidates(root, "Users-jean-code-missing"))
}
func TestWorkspaceCandidates_FollowsSymlinks(t *testing.T) {
t.Parallel()
root := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(root, "private", "var", "repo"), 0o755))
require.NoError(t, os.Symlink(filepath.Join(root, "private", "var"), filepath.Join(root, "var")))
got := workspaceCandidates(root, "var-repo")
require.Len(t, got, 1, "/var is a symlink on macOS")
assert.Equal(t, filepath.Join(root, "var", "repo"), got[0])
}
func TestSyncAll_DiscoversCodexSession(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
path := writeCodexRollout(t, env.home, "th-1", jsonLines(t,
codexSessionMetaLine("th-1", "/work", "git@"+testHost+":my-group/my-project.git"),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"}),
))
_, err := env.exec("--all")
require.NoError(t, err)
assert.Equal(t, []string{"th-1"}, env.fake.sessionsCreated)
assert.Equal(t, []string{"codex"}, env.fake.agentTypes)
require.Len(t, env.fake.sessionRequests, 1)
assert.Equal(t, "0.163.0", env.fake.sessionRequests[0].AgentVersion)
assert.Equal(t, 1, env.fake.eventsPosted)
meta, err := readSessionMeta("th-1")
require.NoError(t, err)
assert.Equal(t, sessionMeta{AgentType: "codex", PathWithNamespace: "my-group/my-project", Host: testHost, Transcript: path}, *meta)
_, err = env.exec("--all")
require.NoError(t, err)
assert.Len(t, env.fake.sessionsCreated, 1, "a discovered session is synced once")
}
func TestSyncAll_CodexSessionWithoutRemoteUsesGit(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
for _, id := range []string{"th-1", "th-2"} {
writeCodexRollout(t, env.home, id, jsonLines(t,
codexSessionMetaLine(id, "/work/repo", ""),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_" + id}),
))
}
env.mExec.EXPECT().
ExecWithIO(gomock.Any(), "git", []string{"-C", "/work/repo", "config", "--get", "remote.origin.url"}, nil, nil, gomock.Any(), gomock.Any()).
DoAndReturn(writesStdout("https://" + testHost + "/my-group/my-project.git\n")).
Times(1)
_, err := env.exec("--all")
require.NoError(t, err)
assert.ElementsMatch(t, []string{"th-1", "th-2"}, env.fake.sessionsCreated, "git runs once for sessions in the same directory")
}
func TestSyncAll_SkipsDiscoveredSessionsOnOtherHosts(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
writeCodexRollout(t, env.home, "th-gh", jsonLines(t,
codexSessionMetaLine("th-gh", "/work", "git@github.com:someone/repo.git"),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"}),
))
_, err := env.exec("--all")
require.NoError(t, err)
assert.Empty(t, env.fake.projectLookups, "a repository on a host glab is not logged in to is not uploaded")
_, err = readSessionMeta("th-gh")
require.ErrorIs(t, err, errNoSessionMeta)
status := readStatus(t)
assert.Empty(t, status.Errors)
assert.Equal(t, []string{withoutRemoteReason("codex")}, status.Skipped)
}
func TestSyncAll_DoesNotReportIdleDiscoveredSessionOnOtherHost(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
path := writeCodexRollout(t, env.home, "th-gh", jsonLines(t,
codexSessionMetaLine("th-gh", "/work", "git@github.com:someone/repo.git"),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"}),
))
idle := time.Now().Add(-2 * idleCompleteAfter)
require.NoError(t, os.Chtimes(path, idle, idle))
_, err := env.exec("--all")
require.NoError(t, err)
assert.Empty(t, readStatus(t).Skipped, "a session discovered on every run is not reported once idle")
}
func TestSyncAll_SkipsCodexSubagentThreads(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
meta := codexSessionMetaLine("th-child", "/work", "git@"+testHost+":my-group/my-project.git")
meta["payload"].(map[string]any)["parent_thread_id"] = "th-parent" //nolint:forcetypeassert // built above
writeCodexRollout(t, env.home, "th-child", jsonLines(t, meta))
_, err := env.exec("--all")
require.NoError(t, err)
_, err = readSessionMeta("th-child")
require.ErrorIs(t, err, errNoSessionMeta)
}
func TestSyncAll_DoesNotDiscoverAgentsThatAreNotEnabled(t *testing.T) {
env := newSyncAllEnv(t, true)
writeCodexRollout(t, env.home, "th-1", jsonLines(t,
codexSessionMetaLine("th-1", "/work", "git@"+testHost+":my-group/my-project.git"),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"}),
))
_, err := env.exec("--all")
require.NoError(t, err)
assert.Empty(t, env.fake.sessionsCreated)
_, err = readSessionMeta("th-1")
require.ErrorIs(t, err, errNoSessionMeta)
}
func TestSyncAll_PausesDiscoveredAgentOnceDisabled(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
writeCodexRollout(t, env.home, "th-1", jsonLines(t,
codexSessionMetaLine("th-1", "/work", "git@"+testHost+":my-group/my-project.git"),
))
_, err := env.exec("--all")
require.NoError(t, err)
_, err = readSessionMeta("th-1")
require.NoError(t, err, "recorded on the first run")
require.NoError(t, fallbacksync.SetDiscoveredAgents(nil))
_, err = env.exec("--all")
require.NoError(t, err)
assert.Equal(t, []string{"codex: codex is not enabled; run 'glab minder setup --agents codex' to enable it"}, readStatus(t).Paused)
}
func TestSyncAll_DiscoversCursorSession(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"cursor"}))
workspace := filepath.Join(t.TempDir(), "my.repo")
require.NoError(t, os.MkdirAll(workspace, 0o755))
slug := nonAlphanumeric.ReplaceAllString(strings.TrimPrefix(workspace, string(filepath.Separator)), "-")
path := filepath.Join(env.home, ".cursor", "projects", slug, "agent-transcripts", "conv-1", "conv-1.jsonl")
require.NoError(t, os.MkdirAll(filepath.Join(filepath.Dir(path), "subagents"), 0o755))
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
map[string]any{"role": "user", "message": map[string]any{"content": []any{map[string]any{"type": "text", "text": "list the files"}}}},
map[string]any{"role": "assistant", "message": map[string]any{"content": []any{map[string]any{"type": "tool_use", "name": "Shell"}}}},
)), 0o644))
env.mExec.EXPECT().
ExecWithIO(gomock.Any(), "git", gomock.Any(), nil, nil, gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, _ string, args, _ []string, _ io.Reader, stdout, _ io.Writer) error {
resolved, err := filepath.EvalSymlinks(args[1])
if err != nil {
return err
}
want, err := filepath.EvalSymlinks(workspace)
if err != nil || resolved != want {
return errors.New("not the workspace")
}
_, err = io.WriteString(stdout, "git@"+testHost+":my-group/my-project.git\n")
return err
})
_, err := env.exec("--all")
require.NoError(t, err)
assert.Equal(t, []string{"conv-1"}, env.fake.sessionsCreated)
assert.Equal(t, []string{"cursor"}, env.fake.agentTypes)
require.Len(t, env.fake.sessionRequests, 1)
assert.Empty(t, env.fake.sessionRequests[0].AgentVersion, "Cursor does not record its version")
assert.Equal(t, []string{eventUserInput, eventToolInvoked}, eventNames(env.fake.events))
}
func TestCodexTranscript_NamespacedToolNames(t *testing.T) {
t.Parallel()
tests := []struct {
name string
item map[string]any
wantName string
wantSpace string
}{
{
name: "a built-in tool has no namespace and keeps its name",
item: map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"},
wantName: "exec_command",
},
{
name: "an MCP tool is qualified the way Codex names it for the model",
item: map[string]any{"type": "function_call", "name": "click", "namespace": "mcp__computer_use", "call_id": "call_1"},
wantName: "mcp__computer_use__click",
wantSpace: "mcp__computer_use",
},
{
name: "older rollouts end the namespace with the separator, which both the name and the namespace drop",
item: map[string]any{"type": "function_call", "name": "js", "namespace": "mcp__node_repl__", "call_id": "call_1"},
wantName: "mcp__node_repl__js",
wantSpace: "mcp__node_repl",
},
{
name: "a server name that ends in an underscore keeps it",
item: map[string]any{"type": "function_call", "name": "js", "namespace": "mcp__foo_", "call_id": "call_1"},
wantName: "mcp__foo___js",
wantSpace: "mcp__foo_",
},
{
name: "older rollouts only lose the separator after such a server name",
item: map[string]any{"type": "function_call", "name": "js", "namespace": "mcp__foo___", "call_id": "call_1"},
wantName: "mcp__foo___js",
wantSpace: "mcp__foo_",
},
{
name: "a custom tool call carries its namespace too",
item: map[string]any{"type": "custom_tool_call", "name": "spawn_agent", "namespace": "collaboration", "call_id": "call_1", "input": ""},
wantName: "collaboration__spawn_agent",
wantSpace: "collaboration",
},
{
name: "a namespace that is only separators counts as none",
item: map[string]any{"type": "function_call", "name": "js", "namespace": "__", "call_id": "call_1"},
wantName: "js",
wantSpace: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "rollout.jsonl")
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
codexSessionMetaLine("th-1", "/work", ""),
codexItem("2026-10-09T01:00:02Z", tt.item),
codexItem("2026-10-09T01:00:03Z", map[string]any{"type": "function_call_output", "call_id": "call_1"}),
)), 0o644))
data, _, err := codexTranscript{path: path}.read(t.Context(), 0)
require.NoError(t, err)
require.Len(t, data.ToolCalls, 1)
assert.Equal(t, tt.wantName, data.ToolCalls[0].Name)
assert.Equal(t, tt.wantSpace, data.ToolCalls[0].Namespace)
require.Len(t, data.ToolResults, 1)
assert.Equal(t, tt.wantName, data.ToolResults[0].Name, "the result names the call it answers")
assert.Equal(t, tt.wantSpace, data.ToolResults[0].Namespace)
})
}
}
func TestCodexTranscript_RichEvents(t *testing.T) {
t.Parallel()
ts := func(s int) string { return time.Date(2026, 10, 9, 1, 0, s, 0, time.UTC).Format(time.RFC3339) }
path := filepath.Join(t.TempDir(), "rollout.jsonl")
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
codexSessionMetaLine("th-1", "/work", ""),
map[string]any{"timestamp": ts(0), "type": "turn_context", "payload": map[string]any{"model": "gpt-5.5-codex"}},
map[string]any{"timestamp": ts(1), "type": "event_msg", "payload": map[string]any{"type": "user_message", "message": "run the tests"}},
codexItem(ts(2), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1", "arguments": `{"cmd":"go test ./..."}`}),
codexItem(ts(5), map[string]any{"type": "function_call_output", "call_id": "call_1", "output": "ok"}),
codexItem(ts(6), map[string]any{"type": "custom_tool_call", "name": "apply_patch", "call_id": "call_2", "input": "*** Begin Patch"}),
map[string]any{"timestamp": ts(7), "type": "event_msg", "payload": map[string]any{"type": "token_count", "info": map[string]any{
"last_token_usage": map[string]any{"input_tokens": 120, "cached_input_tokens": 100, "output_tokens": 30},
}}},
map[string]any{"timestamp": ts(8), "type": "event_msg", "payload": map[string]any{"type": "token_count", "info": nil}},
)), 0o644))
data, _, err := codexTranscript{sessionID: "th-1", path: path}.read(t.Context(), 0)
require.NoError(t, err)
require.Len(t, data.Prompts, 1)
assert.Equal(t, "run the tests", data.Prompts[0].Text)
require.Len(t, data.ToolCalls, 2)
assert.JSONEq(t, `{"cmd":"go test ./..."}`, string(data.ToolCalls[0].Input))
assert.JSONEq(t, `{"input":"*** Begin Patch"}`, string(data.ToolCalls[1].Input), "custom tool input is free text")
require.Len(t, data.ToolResults, 1)
assert.Equal(t, toolResult{
CallID: "call_1", Name: "exec_command", Outcome: "completed",
Duration: 3 * time.Second, Timestamp: time.Date(2026, 10, 9, 1, 0, 5, 0, time.UTC), CWD: "/work",
}, data.ToolResults[0], "Codex does not record whether a call succeeded")
require.Len(t, data.Responses, 1, "a token count without usage is not a response")
assert.Equal(t, "gpt-5.5-codex", data.Responses[0].Model)
assert.Equal(t, tokenUsage{InputTokens: 120, OutputTokens: 30, CacheReadInputTokens: 100}, data.Responses[0].Usage)
}
func TestCodexTranscript_Dirs(t *testing.T) {
t.Parallel()
ts := func(s int) string { return time.Date(2026, 10, 9, 1, 0, s, 0, time.UTC).Format(time.RFC3339) }
path := filepath.Join(t.TempDir(), "rollout.jsonl")
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
codexSessionMetaLine("th-1", "/work/a/", ""),
map[string]any{"timestamp": ts(1), "type": "event_msg", "payload": map[string]any{"type": "user_message", "message": "clone b"}},
codexItem(ts(2), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_abs", "arguments": `{"cmd":"git status","workdir":"/work/x/../b"}`}),
codexItem(ts(3), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_rel", "arguments": `{"cmd":"ls","workdir":"sub"}`}),
codexItem(ts(4), map[string]any{"type": "local_shell_call", "id": "ls_1", "action": map[string]any{"type": "exec", "working_directory": "/work/c"}}),
codexItem(ts(5), map[string]any{"type": "function_call_output", "call_id": "call_abs"}),
map[string]any{"timestamp": ts(6), "type": "turn_context", "payload": map[string]any{"model": "gpt-5.5-codex", "cwd": "/work/b/"}},
map[string]any{"timestamp": ts(7), "type": "event_msg", "payload": map[string]any{"type": "user_message", "message": "now in b"}},
map[string]any{"timestamp": ts(8), "type": "event_msg", "payload": map[string]any{"type": "token_count", "info": map[string]any{
"last_token_usage": map[string]any{"input_tokens": 1, "output_tokens": 1},
}}},
)), 0o644))
data, _, err := codexTranscript{sessionID: "th-1", path: path}.read(t.Context(), 0)
require.NoError(t, err)
require.Len(t, data.ToolCalls, 3)
assert.Equal(t, "/work/b", data.ToolCalls[0].CWD, "an absolute workdir, cleaned")
assert.Equal(t, "/work/a/sub", data.ToolCalls[1].CWD, "a workdir relative to the turn")
assert.Equal(t, "/work/c", data.ToolCalls[2].CWD, "a local shell call's working directory")
require.Len(t, data.ToolResults, 1)
assert.Equal(t, "/work/b", data.ToolResults[0].CWD, "a result runs where its call did")
require.Len(t, data.Prompts, 2)
assert.Equal(t, "/work/a", data.Prompts[0].CWD, "folders are cleaned, so a trailing slash makes no difference")
assert.Equal(t, "/work/b", data.Prompts[1].CWD, "a later turn's folder")
require.Len(t, data.Responses, 1)
assert.Equal(t, "/work/b", data.Responses[0].CWD)
}
func TestCodexTranscript_DirUnknownBeforeTurnWhenReadingMidRollout(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "rollout.jsonl")
head := jsonLines(t, codexSessionMetaLine("th-1", "/work/a", ""))
require.NoError(t, os.WriteFile(path, []byte(head+jsonLines(t,
map[string]any{"timestamp": "2026-10-09T01:00:01Z", "type": "event_msg", "payload": map[string]any{"type": "user_message", "message": "go on"}},
codexItem("2026-10-09T01:00:02Z", map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1", "arguments": `{"cmd":"ls","workdir":"sub"}`}),
)), 0o644))
data, _, err := codexTranscript{sessionID: "th-1", path: path}.read(t.Context(), int64(len(head)))
require.NoError(t, err)
require.Len(t, data.Prompts, 1)
assert.Empty(t, data.Prompts[0].CWD)
require.Len(t, data.ToolCalls, 1)
assert.Empty(t, data.ToolCalls[0].CWD, "a relative workdir without the turn's folder is no folder")
}
func TestCodexTranscript_PaginatedPrompts(t *testing.T) {
t.Parallel()
meta := codexSessionMetaLine("th-1", "/work", "")
meta["payload"].(map[string]any)["history_mode"] = "paginated" //nolint:forcetypeassert // built above
user := func(text string) map[string]any {
return codexItem("2026-10-09T01:00:01Z", map[string]any{"type": "message", "role": "user", "content": []any{map[string]any{"type": "input_text", "text": text}}})
}
path := filepath.Join(t.TempDir(), "rollout.jsonl")
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
meta,
user("# AGENTS.md instructions for /work"),
user("\n/work\n"),
user("fix the flaky test"),
)), 0o644))
data, _, err := codexTranscript{sessionID: "th-1", path: path}.read(t.Context(), 0)
require.NoError(t, err)
require.Len(t, data.Prompts, 1, "paginated rollouts record prompts as user messages, after injected context")
assert.Equal(t, "fix the flaky test", data.Prompts[0].Text)
assert.Equal(t, "fix the flaky test", data.Goal)
}
func TestCursorTranscript_RichEvents(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "conv-1.jsonl")
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
map[string]any{"role": "user", "message": map[string]any{"content": []any{map[string]any{"type": "text", "text": "list the files"}}}},
map[string]any{"role": "assistant", "message": map[string]any{"content": []any{map[string]any{"type": "tool_use", "name": "Shell", "input": map[string]any{"command": "ls"}}}}},
map[string]any{"role": "user", "message": map[string]any{"content": []any{map[string]any{"type": "text", "text": "now count them"}}}},
)), 0o644))
data, _, err := cursorTranscript{sessionID: "conv-1", path: path}.read(t.Context(), 0)
require.NoError(t, err)
require.Len(t, data.Prompts, 2)
assert.Equal(t, "list the files", data.Prompts[0].Text)
assert.Equal(t, "now count them", data.Prompts[1].Text)
assert.NotEqual(t, data.Prompts[0].ID, data.Prompts[1].ID)
require.Len(t, data.ToolCalls, 1)
assert.JSONEq(t, `{"command":"ls"}`, string(data.ToolCalls[0].Input))
assert.Empty(t, data.ToolResults, "Cursor records no tool results")
}
func TestSyncAll_CompletesOldSyncedSessionOnGitLab(t *testing.T) {
env := newSyncAllEnv(t, true)
path := env.writeClaudeSession(t, "sess-1", "my-group/my-project", "toolu_1", "toolu_2")
content, err := os.ReadFile(path)
require.NoError(t, err)
firstLine := int64(bytes.IndexByte(content, '\n') + 1)
require.NoError(t, syncstate.WritePosition("sess-1", firstLine))
old := time.Now().Add(-100 * 24 * time.Hour)
require.NoError(t, os.Chtimes(path, old, old))
_, err = env.exec("--all")
require.NoError(t, err)
assert.Equal(t, 1, env.fake.completed, "a session that already exists on GitLab is still completed there")
}
func TestSyncAll_SkipsSessionsTooOldForGitLab(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
old := time.Now().Add(-100 * 24 * time.Hour)
path := writeCodexRollout(t, env.home, "th-old", jsonLines(t,
codexSessionMetaLine("th-old", "/work", "git@"+testHost+":my-group/my-project.git"),
codexItem(old.Format(time.RFC3339), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"}),
))
require.NoError(t, os.Chtimes(path, old, old))
_, err := env.exec("--all")
require.NoError(t, err)
assert.Empty(t, env.fake.sessionsCreated, "a session GitLab would accept no events for is not created empty")
meta, err := readSessionMeta("th-old")
require.NoError(t, err)
assert.NotNil(t, meta.CompletedAt, "it is not checked again")
}
func TestWorkspaceCandidates_DotAndUnderscoreDirectories(t *testing.T) {
t.Parallel()
root := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(root, "Users", "jean", ".config", "proj"), 0o755))
require.NoError(t, os.MkdirAll(filepath.Join(root, "Users", "jean", "_work", "app"), 0o755))
got := workspaceCandidates(root, "Users-jean--config-proj")
require.Len(t, got, 1, "a leading dot inside the path is encoded as a dash")
assert.Equal(t, filepath.Join(root, "Users", "jean", ".config", "proj"), got[0])
got = workspaceCandidates(root, "Users-jean--work-app")
require.Len(t, got, 1, "so is a leading underscore")
assert.Equal(t, filepath.Join(root, "Users", "jean", "_work", "app"), got[0])
}
func TestWorkspaceCandidates_Ambiguous(t *testing.T) {
t.Parallel()
root := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(root, "code", "cli", "docs"), 0o755))
require.NoError(t, os.MkdirAll(filepath.Join(root, "code", "cli-docs"), 0o755))
got := workspaceCandidates(root, "code-cli-docs")
assert.ElementsMatch(t, []string{filepath.Join(root, "code", "cli", "docs"), filepath.Join(root, "code", "cli-docs")}, got)
}
// writeAmbiguousCursorSession creates code/cli/docs and code/cli-docs, which
// Cursor encodes to the same folder name, and a session in that folder whose
// tool call reads readPath. It returns the sibling directory, code/cli-docs.
func (e *syncAllEnv) writeAmbiguousCursorSession(t *testing.T, readPath func(base string) string) string {
t.Helper()
base := t.TempDir()
nested := filepath.Join(base, "code", "cli", "docs")
sibling := filepath.Join(base, "code", "cli-docs")
require.NoError(t, os.MkdirAll(nested, 0o755))
require.NoError(t, os.MkdirAll(sibling, 0o755))
resolvedBase, err := filepath.EvalSymlinks(base)
require.NoError(t, err)
slug := nonAlphanumeric.ReplaceAllString(strings.TrimPrefix(filepath.Join(resolvedBase, "code", "cli-docs"), string(filepath.Separator)), "-")
path := filepath.Join(e.home, ".cursor", "projects", slug, "agent-transcripts", "conv-1", "conv-1.jsonl")
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
map[string]any{"role": "user", "message": map[string]any{"content": []any{map[string]any{"type": "text", "text": "update the docs"}}}},
map[string]any{"role": "assistant", "message": map[string]any{"content": []any{map[string]any{"type": "tool_use", "name": "Read", "input": map[string]any{"path": readPath(resolvedBase)}}}}},
)), 0o644))
return filepath.Join(resolvedBase, "code", "cli-docs")
}
func TestSyncAll_SkipsAmbiguousCursorWorkspace(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"cursor"}))
env.writeAmbiguousCursorSession(t, func(string) string { return "README.md" })
// The executor has no expectations: no remote is read for an ambiguous workspace.
_, err := env.exec("--all")
require.NoError(t, err)
assert.Empty(t, env.fake.sessionsCreated, "a session must not be uploaded to a project that may not be its own")
status := readStatus(t)
assert.Empty(t, status.Errors)
require.Len(t, status.Skipped, 1)
assert.Contains(t, status.Skipped[0], "matches 2 directories")
}
func TestSyncAll_ConfirmsAmbiguousCursorWorkspaceFromToolPaths(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"cursor"}))
sibling := env.writeAmbiguousCursorSession(t, func(base string) string {
return filepath.Join(base, "code", "cli-docs", "README.md")
})
env.mExec.EXPECT().
ExecWithIO(gomock.Any(), "git", []string{"-C", sibling, "config", "--get", "remote.origin.url"}, nil, nil, gomock.Any(), gomock.Any()).
DoAndReturn(writesStdout("git@" + testHost + ":my-group/my-project.git\n"))
_, err := env.exec("--all")
require.NoError(t, err)
assert.Equal(t, []string{"conv-1"}, env.fake.sessionsCreated, "a path the session read confirms which directory it ran in")
}
func TestSyncAll_AgentTypeNotSupportedByGitLab(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
// What GitLab versions without the agent type answer.
env.fake.identityStatus = http.StatusBadRequest
env.fake.identityBody = `{"error":"agent_type does not have a valid value"}`
for _, id := range []string{"th-1", "th-2"} {
writeCodexRollout(t, env.home, id, jsonLines(t,
codexSessionMetaLine(id, "/work", "git@"+testHost+":my-group/my-project.git"),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_" + id}),
))
}
_, err := env.exec("--all")
require.NoError(t, err)
status := readStatus(t)
assert.Empty(t, status.Errors, "an agent the GitLab version doesn't support yet is expected, not a failure")
assert.Equal(t, []string{"codex on " + testHost}, status.AgentTypeNotSupported)
assert.Equal(t, 1, env.fake.identityRequests)
for _, id := range []string{"th-1", "th-2"} {
meta, err := readSessionMeta(id)
require.NoError(t, err)
assert.NotNil(t, meta.CompletedAt, "the session is not retried")
}
_, err = env.exec("--all")
require.NoError(t, err)
assert.Equal(t, 1, env.fake.identityRequests, "and not uploaded in a burst after an upgrade")
}
// writeCursorSessionFor writes a Cursor session for the workspace slug of
// workspace, whose tool call reads readPath, after creating existing.
func (e *syncAllEnv) writeCursorSessionFor(t *testing.T, workspace, existing, readPath string) {
t.Helper()
require.NoError(t, os.MkdirAll(existing, 0o755))
slug := nonAlphanumeric.ReplaceAllString(strings.TrimPrefix(workspace, string(filepath.Separator)), "-")
path := filepath.Join(e.home, ".cursor", "projects", slug, "agent-transcripts", "conv-1", "conv-1.jsonl")
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
map[string]any{"role": "user", "message": map[string]any{"content": []any{map[string]any{"type": "text", "text": "update the docs"}}}},
map[string]any{"role": "assistant", "message": map[string]any{"content": []any{map[string]any{"type": "tool_use", "name": "Read", "input": map[string]any{"path": readPath}}}}},
)), 0o644))
}
func TestSyncAll_SkipsCursorWorkspaceThatMoved(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"cursor"}))
base, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)
// code/cli-docs was moved away; code/cli/docs, which encodes the same, remains.
moved := filepath.Join(base, "code", "cli-docs")
env.writeCursorSessionFor(t, moved, filepath.Join(base, "code", "cli", "docs"), filepath.Join(moved, "README.md"))
// The executor has no expectations: the remaining directory's remote is not read.
_, err = env.exec("--all")
require.NoError(t, err)
assert.Empty(t, env.fake.sessionsCreated, "the only matching directory is not the one the session used")
status := readStatus(t)
require.Len(t, status.Skipped, 1)
assert.Contains(t, status.Skipped[0], "may have moved")
}
func TestSyncAll_SyncsCursorWorkspaceConfirmedByToolPaths(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"cursor"}))
base, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)
workspace := filepath.Join(base, "code", "cli-docs")
env.writeCursorSessionFor(t, workspace, workspace, filepath.Join(workspace, "README.md"))
env.mExec.EXPECT().
ExecWithIO(gomock.Any(), "git", []string{"-C", workspace, "config", "--get", "remote.origin.url"}, nil, nil, gomock.Any(), gomock.Any()).
DoAndReturn(writesStdout("git@" + testHost + ":my-group/my-project.git\n"))
_, err = env.exec("--all")
require.NoError(t, err)
assert.Equal(t, []string{"conv-1"}, env.fake.sessionsCreated)
}
func TestCodexTranscript_ObjectArguments(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "rollout.jsonl")
require.NoError(t, os.WriteFile(path, []byte(jsonLines(t,
codexSessionMetaLine("th-1", "/work", ""),
codexItem("2026-10-09T01:00:02Z", map[string]any{"type": "tool_search_call", "call_id": "call_1", "execution": "client", "arguments": map[string]any{"query": "deploy"}}),
)), 0o644))
data, _, err := codexTranscript{sessionID: "th-1", path: path}.read(t.Context(), 0)
require.NoError(t, err)
require.Len(t, data.ToolCalls, 1, "a call whose arguments are an object is not dropped")
assert.Equal(t, "tool_search", data.ToolCalls[0].Name)
assert.JSONEq(t, `{"query":"deploy"}`, string(data.ToolCalls[0].Input))
}
func TestSyncAll_CodexDiscoverySkipsUnreadableDirectories(t *testing.T) {
env := newSyncAllEnv(t, true)
require.NoError(t, fallbacksync.SetDiscoveredAgents([]string{"codex"}))
writeCodexRollout(t, env.home, "th-1", jsonLines(t,
codexSessionMetaLine("th-1", "/work", "git@"+testHost+":my-group/my-project.git"),
codexItem(recentTimestamp(), map[string]any{"type": "function_call", "name": "exec_command", "call_id": "call_1"}),
))
unreadable := filepath.Join(env.home, ".codex", "sessions", "2026", "10", "08")
require.NoError(t, os.MkdirAll(unreadable, 0o755))
require.NoError(t, os.Chmod(unreadable, 0o000))
t.Cleanup(func() { _ = os.Chmod(unreadable, 0o755) })
_, err := env.exec("--all")
require.NoError(t, err)
assert.Equal(t, []string{"th-1"}, env.fake.sessionsCreated, "one unreadable directory does not stop discovery")
assert.Empty(t, readStatus(t).Errors)
}