GitLabgitlab-org

cli

main
ply9 git calls260 ms101 KiB6 cachedprotocol v2
312 lines9.9 KiBRaw
1package sync
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8 "path/filepath"
9 "regexp"
10 "slices"
11 "strings"
12 "time"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/gaig"
16 "gitlab.com/gitlab-org/cli/internal/dbg"
19// cursor syncs Cursor agent sessions, which its stop and sessionEnd hooks
20// record and the scheduled job also finds by scanning Cursor's agent
21// transcripts. Their transcript locator is the transcript path.
22type cursor struct {
23 executor cmdutils.Executor
26func (cursor) name() string { return "cursor" }
28func (cursor) sessionID() string {
29 id, _ := hookSession("cursor")
30 return id
33// current opens the transcript that the hook passed. Subagent transcripts, in
34// <id>/subagents, are skipped, as discover skips them.
35func (cursor) current(sessionID string) (transcript, string, error) {
36 _, path := hookSession("cursor")
37 if filepath.Base(filepath.Dir(path)) != sessionID {
38 return nil, "", fmt.Errorf("%s is not the main transcript of a Cursor session", path)
39 }
40 return cursorTranscript{sessionID: sessionID, path: path}, path, nil
43func (cursor) recorded(sessionID, path string) (transcript, string, error) {
44 return cursorTranscript{sessionID: sessionID, path: path}, path, nil
47func (cursor) canForget(src transcript, _ time.Time) (bool, error) {
48 t, ok := src.(cursorTranscript)
49 if !ok {
50 return false, fmt.Errorf("unexpected Cursor transcript type %T", src)
51 }
52 return fileDeleted(t.path)
55func (cursor) pausedReason() (string, error) {
56 return discoveryPausedReason("cursor")
59// discover finds the transcripts of Cursor sessions. Subagent transcripts are
60// not synced in this version.
61func (c cursor) discover(ctx context.Context, known func(string) bool) ([]discoveredSession, error) {
62 transcripts, err := gaig.CursorTranscripts()
63 if err != nil && transcripts == nil {
64 return nil, err
65 }
66 if err != nil {
67 // One unreadable workspace must not stop discovery of the others.
68 dbg.Debugf("%v", err)
69 }
70 byWorkspace := map[string][]discoveredSession{}
71 var workspaces []string
72 for _, t := range transcripts {
73 if known(t.ID) {
74 continue
75 }
76 if _, ok := byWorkspace[t.Workspace]; !ok {
77 workspaces = append(workspaces, t.Workspace)
78 }
79 byWorkspace[t.Workspace] = append(byWorkspace[t.Workspace], discoveredSession{id: t.ID, locator: t.Path})
80 }
82 var found []discoveredSession
83 for _, workspace := range workspaces {
84 sessions := byWorkspace[workspace]
85 remote, skip := c.workspaceRemote(ctx, workspace, sessions)
86 for i := range sessions {
87 sessions[i].remote = remote
88 sessions[i].skip = skip
89 }
90 found = append(found, sessions...)
91 }
92 return found, nil
95// workspaceRemote returns the origin remote of the workspace Cursor named
96// slug, or a reason to skip its sessions when the workspace is ambiguous.
97func (c cursor) workspaceRemote(ctx context.Context, slug string, sessions []discoveredSession) (string, string) {
98 candidates := workspaceCandidates(string(filepath.Separator), strings.TrimPrefix(slug, "-"))
99 if len(candidates) == 0 {
100 dbg.Debugf("could not find the Cursor workspace for %s", slug)
101 return "", ""
102 }
104 // The folder name cannot be decoded with certainty: several directories
105 // can share it, such as code/cli-docs and code/cli/docs, and a matching
106 // directory can be a different one than the workspace if the workspace
107 // moved. Uploading to the wrong directory's project would send the
108 // sessions to another project, so the absolute paths in the sessions'
109 // tool calls must point at the directory. Sessions without such paths
110 // are trusted only when one directory matches.
111 var paths []string
112 for _, s := range sessions {
113 paths = append(paths, toolCallPaths(s.locator)...)
114 }
115 confirmed := slices.DeleteFunc(slices.Clone(candidates), func(c string) bool { return !anyUnder(paths, c) })
116 dir := ""
117 switch {
118 case len(confirmed) == 1:
119 dir = confirmed[0]
120 case len(paths) == 0 && len(candidates) == 1:
121 dir = candidates[0]
122 case len(candidates) == 1:
123 return "", fmt.Sprintf("the Cursor workspace %s matches %s, but its sessions' files are elsewhere, so it may have moved", slug, candidates[0])
124 default:
125 return "", fmt.Sprintf("the Cursor workspace %s matches %d directories (%s), and its sessions do not show which", slug, len(candidates), strings.Join(candidates, ", "))
126 }
128 remote, err := gaig.OriginRemote(ctx, c.executor, dir)
129 if err != nil {
130 dbg.Debugf("cursor workspace %s: %v", dir, err)
131 }
132 return remote, ""
135// anyUnder reports whether any of paths is dir or inside it.
136func anyUnder(paths []string, dir string) bool {
137 return slices.ContainsFunc(paths, func(p string) bool {
138 return p == dir || strings.HasPrefix(p, dir+string(filepath.Separator))
139 })
142// toolCallPaths returns the absolute paths in a Cursor transcript's tool call
143// arguments.
144func toolCallPaths(transcriptPath string) []string {
145 var paths []string
146 _, err := scanJSONL(transcriptPath, 0, func(line []byte, _ int64) {
147 var entry struct {
148 Message struct {
149 Content []struct {
150 Type string `json:"type"`
151 Input map[string]any `json:"input"`
152 } `json:"content"`
153 } `json:"message"`
154 }
155 if json.Unmarshal(line, &entry) != nil {
156 return
157 }
158 for _, block := range entry.Message.Content {
159 if block.Type != "tool_use" {
160 continue
161 }
162 for _, v := range block.Input {
163 if s, ok := v.(string); ok && filepath.IsAbs(s) {
164 paths = append(paths, filepath.Clean(s))
165 }
166 }
167 }
168 })
169 if err != nil {
170 dbg.Debugf("could not read %s: %v", transcriptPath, err)
171 }
172 return paths
175var nonAlphanumeric = regexp.MustCompile(`[^A-Za-z0-9]`)
177// cursorWorkspaceRoot returns the folder of a multi-root workspace that the
178// transcript at path belongs to, the way workspaceRemote decides: the one root
179// that Cursor encodes as the workspace slug in path, unless the transcript's
180// tool calls point elsewhere, or when no single root matches the slug, the one
181// root the tool calls point into. Every later turn of a session then returns
182// the same root or "", so a session never moves to another project.
183func cursorWorkspaceRoot(path string, roots []string) string {
184 if len(roots) == 1 {
185 return roots[0]
186 }
187 var named []string
188 sessionDir := filepath.Dir(path)
189 if filepath.Base(filepath.Dir(sessionDir)) == "agent-transcripts" {
190 slug := strings.TrimPrefix(filepath.Base(filepath.Dir(filepath.Dir(sessionDir))), "-")
191 for _, root := range roots {
192 if strings.TrimPrefix(nonAlphanumeric.ReplaceAllString(filepath.Clean(root), "-"), "-") == slug {
193 named = append(named, root)
194 }
195 }
196 }
197 paths := toolCallPaths(path)
198 used := slices.DeleteFunc(slices.Clone(roots), func(r string) bool { return !anyUnder(paths, filepath.Clean(r)) })
199 if len(named) == 1 {
200 if len(paths) == 0 || slices.Contains(used, named[0]) {
201 return named[0]
202 }
203 return ""
204 }
205 if len(used) == 1 {
206 return used[0]
207 }
208 return ""
211// workspaceCandidates returns every directory under dir that Cursor could
212// have named slug. Cursor replaces every non-alphanumeric character of the
213// workspace path with "-", which cannot be reversed, so this walks the
214// directories that exist and keeps the paths whose encoding matches.
215func workspaceCandidates(dir, slug string) []string {
216 entries, err := os.ReadDir(dir)
217 if err != nil {
218 return nil
219 }
220 var found []string
221 for _, entry := range entries {
222 child := filepath.Join(dir, entry.Name())
223 if ok, err := gaig.IsDir(child); err != nil || !ok {
224 continue
225 }
226 encoded := nonAlphanumeric.ReplaceAllString(entry.Name(), "-")
227 if slug == encoded {
228 found = append(found, child)
229 continue
230 }
231 if rest, ok := strings.CutPrefix(slug, encoded+"-"); ok {
232 found = append(found, workspaceCandidates(child, rest)...)
233 }
234 }
235 return found
238// cursorTranscript is a Cursor agent transcript. Its cursor is a byte offset.
239// Cursor records neither tool call IDs nor timestamps, so a call's ID is
240// derived from its position in the file, and its time is the transcript's
241// last modification.
242type cursorTranscript struct {
243 sessionID string
244 path string
247func (t cursorTranscript) String() string { return t.path }
249func (t cursorTranscript) read(_ context.Context, from int64) (*sessionData, int64, error) {
250 modified, err := fileModTime(t.path)
251 if err != nil {
252 return nil, from, err
253 }
254 data := &sessionData{}
255 offset, err := scanJSONL(t.path, from, func(line []byte, lineOffset int64) {
256 t.parseLine(line, lineOffset, modified, data, from > 0)
257 })
258 return data, offset, err
261var userQueryRE = regexp.MustCompile(`(?s)<user_query>\s*(.*?)\s*</user_query>`)
263func (t cursorTranscript) parseLine(line []byte, lineOffset int64, modified time.Time, data *sessionData, goalFound bool) {
264 var entry struct {
265 Role string `json:"role"`
266 Message struct {
267 Content []struct {
268 Type string `json:"type"`
269 Text string `json:"text"`
270 Name string `json:"name"`
271 Input json.RawMessage `json:"input"`
272 } `json:"content"`
273 } `json:"message"`
274 }
275 if json.Unmarshal(line, &entry) != nil {
276 return
277 }
278 for i, block := range entry.Message.Content {
279 id := fmt.Sprintf("cursor:%s:%d:%d", t.sessionID, lineOffset, i)
280 switch {
281 case entry.Role == "user" && block.Type == "text":
282 text := block.Text
283 // Cursor wraps the prompt in <user_query> after injected context.
284 if m := userQueryRE.FindStringSubmatch(text); m != nil {
285 text = m[1]
286 }
287 text = strings.TrimSpace(text)
288 if text == "" || skipPrompt(text) {
289 continue
290 }
291 data.Prompts = append(data.Prompts, prompt{ID: id, Text: text, Timestamp: modified})
292 if !goalFound && data.Goal == "" {
293 data.Goal = goalFromText(text)
294 }
295 case entry.Role == "assistant" && block.Type == "tool_use":
296 data.ToolCalls = append(data.ToolCalls, toolCall{
297 ID: id,
298 Name: block.Name,
299 Input: block.Input,
300 Timestamp: modified,
301 })
302 }
303 }
306func (t cursorTranscript) lastActivity(context.Context) (time.Time, error) {
307 return fileModTime(t.path)
310func (t cursorTranscript) checksum(context.Context) (string, error) {
311 return sha256File(t.path)
Diagnostics9 git calls · 260 ms
CallStatusSizeTime0260 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200261 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=261
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=3bb9946301aa8c0dc8d202b83bc9f84bf0400f6d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=261
git.pkt.payload.max=234
git.sideband.band1.bytes=234
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=234
git.pack.trailer=e0a52f164f79506da6b0dcc275ed62911f5383bf
Open pack map
fetchtree:0200139 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=f91da6c620e9fe39086f689d1ae91b87356cda50
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=a35549813b2a01d473947912b5773e993ce7c2e2
Open pack map
fetchtree:0200638 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=247
git.response.bytes=638
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=02089b627a9895becfd23127f25e46df629ce9b2
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=638
git.pkt.payload.max=611
git.sideband.band1.bytes=611
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=611
git.pack.trailer=f2a6246e346c2fa34e54ecb04cb1dd1c524f65bc
Open pack map
fetch2003.5 KiB247 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3566
git.response.cache=MISS
git.response.duration_ms=247
git.fetch.wants=9d405220c34cee459187c22b87e4b6d9545ed9e2
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3566
git.pkt.payload.max=3539
git.sideband.band1.bytes=3539
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2d22c88c7cf3a
Request
e06bb238…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format