GitLabgitlab-org

cli

main
ply9 git calls246 ms100 KiB7 cachedprotocol v2
157 lines5.1 KiBRaw
1package sync
3import (
4 "encoding/json"
5 "io"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "slices"
10 "strings"
12 "gitlab.com/gitlab-org/cli/internal/dbg"
15// The hook passes the session to the glab it starts through these variables,
16// the way the OpenCode plugin passes OPENCODE_SESSION_ID.
17const (
18 hookAgentEnv = "GLAB_MINDER_HOOK_AGENT"
19 hookSessionEnv = "GLAB_MINDER_HOOK_SESSION_ID"
20 hookTranscriptEnv = "GLAB_MINDER_HOOK_TRANSCRIPT"
23// maxHookInputBytes bounds what a hook reads from stdin. Codex also sends the
24// last response, so the input can be large.
25const maxHookInputBytes = 16 << 20
27// hookAgents are the agents whose hooks pass the session on stdin.
28var hookAgents = []string{"codex", "cursor"}
30// hookInput is the JSON that a Codex Stop hook, or a Cursor stop or
31// sessionEnd hook, gets on stdin.
32type hookInput struct {
33 SessionID string `json:"session_id"`
34 TranscriptPath string `json:"transcript_path"`
35 CWD string `json:"cwd"`
36 WorkspaceRoots []string `json:"workspace_roots"`
39// hookTarget returns the session ID, transcript and directory of the session
40// named in the hook input in, for agent.
41func hookTarget(agent string, in hookInput) (string, string, string) {
42 if agent != "cursor" {
43 dir := in.CWD
44 if dir == "" {
45 // The Codex hook runs in the session's directory.
46 dir = "."
47 }
48 return in.SessionID, in.TranscriptPath, dir
49 }
50 if in.TranscriptPath == "" || len(in.WorkspaceRoots) == 0 {
51 return "", "", ""
52 }
53 // Discovery takes the session ID from the transcript path, so the hook
54 // does too, so that a session is not created twice. A user-level hook runs
55 // in ~/.cursor, so the directory comes from the workspace.
56 dir := cursorWorkspaceRoot(in.TranscriptPath, in.WorkspaceRoots)
57 if dir == "" {
58 return "", "", ""
59 }
60 id := strings.TrimSuffix(filepath.Base(in.TranscriptPath), filepath.Ext(in.TranscriptPath))
61 return id, in.TranscriptPath, dir
64// hookSession returns the session and transcript that a hook for agent passed
65// to this glab, or empty strings when it was not started by one.
66func hookSession(agent string) (string, string) {
67 if os.Getenv(hookAgentEnv) != agent {
68 return "", ""
69 }
70 return os.Getenv(hookSessionEnv), os.Getenv(hookTranscriptEnv)
73// runHook reads the session from the hook's stdin, and syncs it in a glab
74// started in the background, so the agent doesn't wait for the upload and an
75// agent that exits doesn't stop it. A shell & can't do this, because a command
76// it starts gets an empty stdin. runHook prints nothing and never fails,
77// because the agent reads a Stop hook's output as a reason to keep going.
78func runHook(opts *options) error {
79 var in hookInput
80 if err := json.NewDecoder(io.LimitReader(opts.io.In, maxHookInputBytes)).Decode(&in); err != nil {
81 dbg.Debugf("could not read the %s hook input: %v", opts.hook, err)
82 return nil
83 }
84 // --all skips the sessions of an agent that is not enabled, so its hook
85 // does not upload them either.
86 if ag := agentByName(supportedAgents(opts.executor), opts.hook); ag != nil {
87 reason, err := ag.pausedReason()
88 if err != nil || reason != "" {
89 if err != nil {
90 reason = err.Error()
91 }
92 dbg.Debugf("not syncing the %s session: %s", opts.hook, reason)
93 return nil
94 }
95 }
96 sessionID, transcriptPath, dir := hookTarget(opts.hook, in)
97 if sessionID == "" || transcriptPath == "" {
98 dbg.Debugf("the %s hook input has no session, transcript or workspace", opts.hook)
99 return nil
100 }
101 // The glab started below runs in another directory, and records the path
102 // for --all to reopen.
103 transcriptPath, err := absolutePath(transcriptPath)
104 if err != nil {
105 dbg.Debugf("could not resolve the %s transcript path: %v", opts.hook, err)
106 return nil
107 }
109 env := slices.DeleteFunc(os.Environ(), func(kv string) bool {
110 // A Claude Code or OpenCode session that started the agent must not
111 // be synced in its place.
112 return strings.HasPrefix(kv, "CLAUDE_CODE_SESSION_ID=") || strings.HasPrefix(kv, "OPENCODE_SESSION_ID=")
113 })
114 env = append(env,
115 hookAgentEnv+"="+opts.hook,
116 hookSessionEnv+"="+sessionID,
117 hookTranscriptEnv+"="+transcriptPath,
118 )
119 start := opts.startDetached
120 if start == nil {
121 start = startDetached
122 }
123 if err := start(dir, env, "minder", "audit", "sync", "--silent"); err != nil {
124 dbg.Debugf("could not start glab for the %s hook: %v", opts.hook, err)
125 }
126 return nil
129// absolutePath returns path with a leading ~ expanded, relative to the current
130// directory.
131func absolutePath(path string) (string, error) {
132 if rest, ok := strings.CutPrefix(path, "~"+string(filepath.Separator)); ok || path == "~" {
133 home, err := os.UserHomeDir()
134 if err != nil {
135 return "", err
136 }
137 path = filepath.Join(home, rest)
138 }
139 return filepath.Abs(path)
142// startDetached starts this glab with args in dir, in its own session with
143// no standard streams, and doesn't wait for it.
144func startDetached(dir string, env []string, args ...string) error {
145 self, err := os.Executable()
146 if err != nil {
147 return err
148 }
149 cmd := exec.Command(self, args...)
150 cmd.Dir = dir
151 cmd.Env = env
152 cmd.SysProcAttr = detachedProcAttr()
153 if err := cmd.Start(); err != nil {
154 return err
155 }
156 return cmd.Process.Release()
Diagnostics9 git calls · 246 ms
CallStatusSizeTime0246 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=2
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 · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=1
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 · hit3 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=261
git.response.cache=HIT
git.response.duration_ms=3
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=2
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=3
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
fetch2002.1 KiB232 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=232
git.response.bytes=2146
git.response.cache=MISS
git.response.duration_ms=232
git.fetch.wants=3587389c6ba7f3024469f2cc271949a2884b151b
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=2146
git.pkt.payload.max=2119
git.sideband.band1.bytes=2119
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
a5e2d5cd0ab3689d
Request
85dc088d…
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