GitLabgitlab-org

cli

main
ply9 git calls276 ms102 KiB6 cachedprotocol v2
459 lines13 KiBRaw
1package sync
3import (
4 "context"
5 "crypto/sha256"
6 "encoding/hex"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "os"
11 "path/filepath"
12 "runtime"
13 "slices"
14 "strings"
15 "time"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/opencodeplugin"
19 "gitlab.com/gitlab-org/cli/internal/dbg"
22// openCode syncs OpenCode sessions. Their transcript locator is the path of
23// the opencode binary, because the scheduled job runs with a minimal PATH
24// that usually does not include it.
25type openCode struct {
26 executor cmdutils.Executor
29func (openCode) name() string { return "opencode" }
31func (openCode) sessionID() string { return os.Getenv("OPENCODE_SESSION_ID") }
33func (o openCode) current(sessionID string) (transcript, string, error) {
34 binary, err := o.executor.LookPath("opencode")
35 if err != nil {
36 return nil, "", fmt.Errorf("could not find opencode: %w", err)
37 }
38 return &openCodeTranscript{sessionID: sessionID, binary: binary, executor: o.executor}, binary, nil
41func (o openCode) recorded(sessionID, binary string) (transcript, string, error) {
42 use := binary
43 if _, err := os.Stat(binary); err != nil {
44 // Fall back to PATH, for example after opencode moved on upgrade.
45 use = ""
46 }
47 // The recorded locator is kept, so that rewriting the metadata does not
48 // reset how long ago a hook last recorded the session.
49 return &openCodeTranscript{sessionID: sessionID, binary: use, executor: o.executor}, binary, nil
52// canForget allows forgetting a completed OpenCode session only a fixed time
53// after completion. Checking whether the session still exists would need a
54// full opencode export, and its "Session not found" may mean the job sees a
55// different OpenCode data directory.
56func (openCode) canForget(_ transcript, completedAt time.Time) (bool, error) {
57 return time.Since(completedAt) > forgetUnfoundAfter, nil
60func (openCode) discover(context.Context, func(string) bool) ([]discoveredSession, error) {
61 return nil, nil
64func (openCode) pausedReason() (string, error) {
65 return openCodePausedReason(runtime.GOOS)
68func openCodePausedReason(goos string) (string, error) {
69 if goos == "windows" {
70 return "the OpenCode plugin is not supported on Windows", nil
71 }
72 path, err := opencodeplugin.Path()
73 if err != nil {
74 return "", err
75 }
76 state, err := opencodeplugin.Check(path)
77 if err != nil {
78 return "", err
79 }
80 if state == opencodeplugin.NotInstalled || state == opencodeplugin.Foreign {
81 return "the glab OpenCode plugin is not installed in " + opencodeplugin.Display(path), nil
82 }
83 return "", nil
86// openCodeTranscript reads an OpenCode session through its export command
87// rather than OpenCode's storage directly. The storage moved from JSON files
88// to SQLite in OpenCode 1.2 and its schema keeps changing, while the export
89// format is the documented interface.
90//
91// Its cursor counts the tool calls already synced, because the export has no
92// byte offsets.
93type openCodeTranscript struct {
94 sessionID string
95 binary string
96 executor cmdutils.Executor
97 // timeout overrides openCodeExportTimeout in tests.
98 timeout time.Duration
100 raw []byte
101 export *openCodeExport
104// openCodeExport holds what glab reads from either export format. OpenCode 1
105// keeps each message's role and parts under info and parts. OpenCode 2 gives
106// each message a type, with the user's text or the assistant's content.
107type openCodeExport struct {
108 Info struct {
109 Time openCodeTime `json:"time"`
110 Version string `json:"version"`
111 } `json:"info"`
112 Messages []openCodeMessage `json:"messages"`
115type openCodeMessage struct {
116 Info struct {
117 Role string `json:"role"`
118 Time openCodeTime `json:"time"`
119 } `json:"info"`
120 Parts []openCodePart `json:"parts"`
122 Type string `json:"type"`
123 Time openCodeTime `json:"time"`
124 Text string `json:"text"`
125 // Content is decoded one block at a time in tools, so that a block in a
126 // shape glab does not know does not fail the whole export.
127 Content []json.RawMessage `json:"content"`
130// openCodeTime holds Unix timestamps in milliseconds.
131type openCodeTime struct {
132 Created int64 `json:"created"`
133 Updated int64 `json:"updated"`
136type openCodePart struct {
137 ID string `json:"id"`
138 Type string `json:"type"`
139 Text string `json:"text"`
140 Synthetic bool `json:"synthetic"`
141 CallID string `json:"callID"`
142 Tool string `json:"tool"`
143 State struct {
144 Status string `json:"status"`
145 Input json.RawMessage `json:"input"`
146 Error string `json:"error"`
147 Time struct {
148 Start int64 `json:"start"`
149 End int64 `json:"end"`
150 } `json:"time"`
151 } `json:"state"`
154// openCodeContent is an OpenCode 2 assistant content block.
155type openCodeContent struct {
156 Type string `json:"type"`
157 ID string `json:"id"`
158 Name string `json:"name"`
159 State struct {
160 Status string `json:"status"`
161 Input json.RawMessage `json:"input"`
162 Error json.RawMessage `json:"error"`
163 } `json:"state"`
164 Time struct {
165 Created int64 `json:"created"`
166 Ran int64 `json:"ran"`
167 Completed int64 `json:"completed"`
168 } `json:"time"`
171// errorText returns a tool error's message, or its type when it has none.
172func (c openCodeContent) errorText() string {
173 var structured struct {
174 Type string `json:"type"`
175 Message string `json:"message"`
176 }
177 if json.Unmarshal(c.State.Error, &structured) == nil {
178 if structured.Message != "" {
179 return structured.Message
180 }
181 return structured.Type
182 }
183 var text string
184 _ = json.Unmarshal(c.State.Error, &text)
185 return text
188// openCodeTool is a tool call from either export format, with times in Unix
189// milliseconds. start and end are zero until the call has started or ended.
190type openCodeTool struct {
191 id, name string
192 input json.RawMessage
193 status string
194 err string
195 created int64
196 start int64
197 end int64
200func (e *openCodeExport) goal() string {
201 for _, msg := range e.Messages {
202 if msg.Type == "user" {
203 if goal := goalFromText(msg.Text); goal != "" {
204 return goal
205 }
206 }
207 if msg.Info.Role != "user" {
208 continue
209 }
210 for _, part := range msg.Parts {
211 if part.Type != "text" || part.Synthetic {
212 continue
213 }
214 if goal := goalFromText(part.Text); goal != "" {
215 return goal
216 }
217 }
218 }
219 return ""
222func (e *openCodeExport) tools() []openCodeTool {
223 var tools []openCodeTool
224 for _, msg := range e.Messages {
225 for _, part := range msg.Parts {
226 if part.Type != "tool" {
227 continue
228 }
229 id := part.CallID
230 if id == "" {
231 id = part.ID
232 }
233 tools = append(tools, openCodeTool{
234 id: id,
235 name: part.Tool,
236 input: part.State.Input,
237 status: part.State.Status,
238 err: part.State.Error,
239 created: msg.Info.Time.Created,
240 start: part.State.Time.Start,
241 end: part.State.Time.End,
242 })
243 }
244 for _, raw := range msg.Content {
245 var block openCodeContent
246 if err := json.Unmarshal(raw, &block); err != nil || block.Type != "tool" {
247 continue
248 }
249 input := block.State.Input
250 // A streaming call's input is a string of partial JSON, and the
251 // call is not synced again once it has been counted.
252 if block.State.Status == "streaming" {
253 input = nil
254 }
255 tools = append(tools, openCodeTool{
256 id: block.ID,
257 name: block.Name,
258 input: input,
259 status: block.State.Status,
260 err: block.errorText(),
261 created: block.Time.Created,
262 start: block.Time.Ran,
263 end: block.Time.Completed,
264 })
265 }
266 }
267 return tools
270func (t *openCodeTranscript) String() string {
271 return "OpenCode session " + t.sessionID
274// openCodeExportCommands are the commands that print a session, newest
275// OpenCode first. OpenCode 2 moved export under session, and --standalone
276// reads it without OpenCode's background service, which the scheduled job
277// would otherwise start.
278var openCodeExportCommands = [][]string{{"session", "export", "--standalone"}, {"export"}}
280var errOpenCodeExportTimedOut = errors.New("timed out")
282func (t *openCodeTranscript) load(ctx context.Context) error {
283 if t.export != nil {
284 return nil
285 }
287 binary := t.binary
288 if binary == "" {
289 path, err := t.executor.LookPath("opencode")
290 if err != nil {
291 return fmt.Errorf("could not find opencode: %w", err)
292 }
293 binary = path
294 }
296 var failures []error
297 for _, command := range openCodeExportCommands {
298 if err := ctx.Err(); err != nil {
299 return err
300 }
301 args := append(slices.Clone(command), t.sessionID)
302 stdout, stderr, err := t.runExport(ctx, binary, args)
303 switch {
304 // opencode reports a deleted session only through this message. It is
305 // not proof that the session was deleted: the job may see a different
306 // OpenCode data directory from the agent.
307 case err != nil && strings.Contains(string(stderr), "Session not found"):
308 return fmt.Errorf("opencode %s: %w", strings.Join(args, " "), errAgentSessionNotFound)
309 case errors.Is(err, errOpenCodeExportTimedOut):
310 return fmt.Errorf("opencode %s: %w", strings.Join(args, " "), err)
311 case err != nil:
312 // OpenCode 1 answers session export with its whole help text.
313 reason, _, _ := strings.Cut(strings.TrimSpace(string(stderr)), "\n")
314 failures = append(failures, fmt.Errorf("opencode %s: %w: %s", strings.Join(args, " "), err, reason))
315 continue
316 }
318 var export openCodeExport
319 if err := json.Unmarshal(stdout, &export); err != nil {
320 return fmt.Errorf("could not parse opencode export: %w", err)
321 }
322 t.raw = stdout
323 t.export = &export
324 return nil
325 }
326 return errors.Join(failures...)
329// openCodeExportTimeout bounds one opencode export. launchd and systemd do not
330// start a run while the previous one is still going, so a hung export would
331// otherwise stop all later syncing.
332const openCodeExportTimeout = 2 * time.Minute
334// runExport runs an opencode export command with its output in temporary
335// files rather than buffers. With a buffer, exec copies the output through a
336// pipe and waits for the pipe to close, so a child process of opencode that
337// inherits it keeps the wait blocked even after opencode is killed.
338func (t *openCodeTranscript) runExport(ctx context.Context, binary string, args []string) ([]byte, []byte, error) {
339 timeout := t.timeout
340 if timeout == 0 {
341 timeout = openCodeExportTimeout
342 }
343 ctx, cancel := context.WithTimeout(ctx, timeout)
344 defer cancel()
346 dir, err := os.MkdirTemp("", "glab-opencode-export-*")
347 if err != nil {
348 return nil, nil, err
349 }
350 defer func() {
351 if err := os.RemoveAll(dir); err != nil {
352 dbg.Debugf("could not remove %s: %v", dir, err)
353 }
354 }()
355 outPath, errPath := filepath.Join(dir, "stdout"), filepath.Join(dir, "stderr")
357 runErr := t.exportToFiles(ctx, binary, args, outPath, errPath)
358 if runErr != nil && errors.Is(ctx.Err(), context.DeadlineExceeded) {
359 runErr = fmt.Errorf("%w after %s", errOpenCodeExportTimedOut, timeout)
360 }
362 stdout, err := os.ReadFile(outPath)
363 if err != nil {
364 return nil, nil, err
365 }
366 stderr, err := os.ReadFile(errPath)
367 if err != nil {
368 return nil, nil, err
369 }
370 return stdout, stderr, runErr
373func (t *openCodeTranscript) exportToFiles(ctx context.Context, binary string, args []string, outPath, errPath string) (err error) {
374 outFile, err := os.Create(outPath)
375 if err != nil {
376 return err
377 }
378 defer func() { err = errors.Join(err, outFile.Close()) }()
379 errFile, err := os.Create(errPath)
380 if err != nil {
381 return err
382 }
383 defer func() { err = errors.Join(err, errFile.Close()) }()
385 return t.executor.ExecWithIO(ctx, binary, args, nil, nil, outFile, errFile)
388func (t *openCodeTranscript) read(ctx context.Context, cursor int64) (*sessionData, int64, error) {
389 if err := t.load(ctx); err != nil {
390 return nil, cursor, err
391 }
393 data := &sessionData{
394 SessionID: t.sessionID,
395 StartedAt: time.UnixMilli(t.export.Info.Time.Created),
396 AgentVersion: t.export.Info.Version,
397 }
398 if cursor == 0 {
399 data.Goal = t.export.goal()
400 }
401 tools := t.export.tools()
402 for _, tool := range tools[min(cursor, int64(len(tools))):] {
403 // Pending tool calls have no start time yet.
404 started := tool.start
405 if started == 0 {
406 started = tool.created
407 }
408 data.ToolCalls = append(data.ToolCalls, toolCall{
409 ID: tool.id,
410 Name: tool.name,
411 Input: tool.input,
412 Timestamp: time.UnixMilli(started),
413 })
414 // The cursor counts tool calls, so a result is only recorded when the
415 // call has finished by the time it is first synced, which is usual as
416 // syncs follow each turn.
417 if tool.status != "completed" && tool.status != "error" {
418 continue
419 }
420 result := toolResult{
421 CallID: tool.id,
422 Name: tool.name,
423 IsError: tool.status == "error",
424 Error: tool.err,
425 Timestamp: time.UnixMilli(tool.end),
426 }
427 if tool.end > 0 && tool.start > 0 {
428 result.Duration = time.Duration(tool.end-tool.start) * time.Millisecond
429 } else {
430 result.Timestamp = time.UnixMilli(started)
431 }
432 data.ToolResults = append(data.ToolResults, result)
433 }
434 return data, max(int64(len(tools)), cursor), nil
437func (t *openCodeTranscript) lastActivity(ctx context.Context) (time.Time, error) {
438 if err := t.load(ctx); err != nil {
439 return time.Time{}, err
440 }
441 last := t.export.Info.Time.Updated
442 for _, msg := range t.export.Messages {
443 last = max(last, msg.Info.Time.Created, msg.Time.Created)
444 }
445 if last == 0 {
446 // Without any timestamp the session must not look idle, or --all
447 // would mark a live session completed.
448 return time.Now(), nil
449 }
450 return time.UnixMilli(last), nil
453func (t *openCodeTranscript) checksum(ctx context.Context) (string, error) {
454 if err := t.load(ctx); err != nil {
455 return "", err
456 }
457 sum := sha256.Sum256(t.raw)
458 return hex.EncodeToString(sum[:]), nil
Diagnostics9 git calls · 276 ms
CallStatusSizeTime0276 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 KiB3 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=UPDATING
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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
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 · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=5
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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=261
git.response.cache=HIT
git.response.duration_ms=4
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 · 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=139
git.response.cache=HIT
git.response.duration_ms=3
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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=638
git.response.cache=HIT
git.response.duration_ms=4
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
fetch2004.4 KiB257 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=257
git.response.bytes=4515
git.response.cache=MISS
git.response.duration_ms=257
git.fetch.wants=54506dd8f60f177f647194b95c4e503d51d186b3
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=4515
git.pkt.payload.max=4488
git.sideband.band1.bytes=4488
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
a5e2d0fe1ed6cf3a
Request
f481fe09…
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