GitLabgitlab-org

cli

main
ply9 git calls295 ms101 KiB6 cachedprotocol v2
238 lines7.8 KiBRaw
1package sync
3import (
4 "bytes"
5 "encoding/json"
6 "math"
7 "time"
8 "unicode/utf8"
10 "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/gaig"
11 "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/redact"
12 "gitlab.com/gitlab-org/cli/internal/dbg"
15// Event names and the content and token keys follow the audit events GitLab
16// Duo Agent Platform records (duo_workflow_service/audit_events/event_types.py
17// in gitlab-org/modelops/applied-ml/code-suggestions/ai-assist), so the audit
18// event views show glab's events the same way. DAP records no tool outcome, so
19// outcome, agent_initiated and duration_s follow GitLab's MCP server
20// (ee/app/services/mcp/audit/capture_event_service.rb).
21const (
22 eventToolInvoked = "ai_tool_invoked"
23 eventToolResponse = "ai_tool_response_received"
24 eventToolFailed = "ai_tool_execution_failed"
25 eventUserInput = "ai_user_input_received"
26 eventResponseReceived = "ai_llm_response_received"
28 // GitLab rejects an event whose encoded details exceed 10 KB, which makes
29 // it reject the whole batch. Large values are capped below that, leaving
30 // room for the other details, and the details as a whole are checked too.
31 // Sizes are measured on the JSON encoding, because escaping lengthens
32 // quotes, backslashes, newlines, and the <, > and & that GitLab escapes.
33 maxValueBytes = 8 * 1024
34 maxDetailsBytes = 10*1024 - 512
35 // errorMessageLimit matches the MCP server's limit.
36 errorMessageLimit = 255
39var argumentsOmitted = map[string]string{"omitted": "arguments exceeded size limit"}
41// auditEvents returns the audit events for data. Each event's ID is derived
42// from its source, so posting the same data again is deduplicated by GitLab.
43func auditEvents(agentType string, data *sessionData, now time.Time) []auditEventRequest {
44 var events []auditEventRequest
45 add := func(name, id string, at time.Time, details map[string]any) {
46 if now.Sub(at) > gaig.MaxEventAge {
47 dbg.Debugf("dropping %s event %s from %s, older than GitLab accepts", name, id, at)
48 return
49 }
50 details["agent_name"] = agentType
51 if encodedLen(details) > maxDetailsBytes {
52 if _, ok := details["tool_args"]; ok {
53 details["tool_args"] = argumentsOmitted
54 }
55 if _, ok := details["content"]; ok {
56 details["content"], details["content_truncated"] = "", true
57 }
58 }
59 events = append(events, auditEventRequest{
60 EventName: name,
61 CloudEventID: deterministicUUID(id),
62 OccurredAt: at.UTC().Format(time.RFC3339),
63 Details: details,
64 })
65 }
67 for _, p := range data.Prompts {
68 text, truncated := truncateEncoded(p.Text, maxValueBytes)
69 details := map[string]any{
70 "input_type": "message",
71 "content": text,
72 "content_length": utf8.RuneCountInString(p.Text),
73 }
74 if truncated {
75 details["content_truncated"] = true
76 }
77 add(eventUserInput, "prompt:"+p.ID, p.Timestamp, details)
78 }
79 for _, call := range data.ToolCalls {
80 details := map[string]any{
81 "tool_name": call.Name,
82 "agent_initiated": true,
83 "outcome": "pending",
84 "tool_call_id": call.ID,
85 }
86 if call.Namespace != "" {
87 details["tool_namespace"] = call.Namespace
88 }
89 if arguments := cappedArguments(call.Input); arguments != nil {
90 details["tool_args"] = arguments
91 }
92 // The ID is the tool call ID alone, as before results were recorded,
93 // so calls synced by an earlier glab version are not duplicated.
94 add(eventToolInvoked, call.ID, call.Timestamp, details)
95 }
96 for _, r := range data.ToolResults {
97 name, outcome := eventToolResponse, "success"
98 details := map[string]any{"agent_initiated": true, "tool_call_id": r.CallID}
99 if r.IsError {
100 name, outcome = eventToolFailed, "error"
101 details["error_message"], _ = truncateRunes(r.Error, errorMessageLimit)
102 }
103 if r.Outcome != "" {
104 outcome = r.Outcome
105 }
106 details["outcome"] = outcome
107 if r.Name != "" {
108 details["tool_name"] = r.Name
109 }
110 if r.Namespace != "" {
111 details["tool_namespace"] = r.Namespace
112 }
113 if r.Duration > 0 {
114 details["duration_s"] = r.Duration.Seconds()
115 }
116 add(name, r.CallID+":result", r.Timestamp, details)
117 }
118 for _, r := range data.Responses {
119 details := map[string]any{"prompt_token_count": r.Usage.InputTokens, "completion_token_count": r.Usage.OutputTokens}
120 if r.Model != "" {
121 details["model_name"] = r.Model
122 }
123 if r.Usage.CacheReadInputTokens > 0 {
124 details["cache_read_input_tokens"] = r.Usage.CacheReadInputTokens
125 }
126 if r.Usage.CacheCreationInputTokens > 0 {
127 details["cache_creation_input_tokens"] = r.Usage.CacheCreationInputTokens
128 }
129 add(eventResponseReceived, "response:"+r.ID, r.Timestamp, details)
130 }
131 return events
134// cappedArguments returns a tool call's arguments with their secrets
135// redacted, or a marker when they are too large or not valid JSON. The size is
136// checked before redaction. Arguments that redaction lengthens past GitLab's
137// limit are omitted by the check on the whole event. DAP's tool_args is an
138// object, so other values are wrapped in one.
139func cappedArguments(input json.RawMessage) any {
140 if len(input) == 0 || string(input) == "null" {
141 return nil
142 }
143 if !json.Valid(input) || encodedLen(input) > maxValueBytes {
144 return argumentsOmitted
145 }
146 redacted := redact.JSON(input)
147 if trimmed := bytes.TrimSpace(redacted); trimmed[0] != '{' {
148 return map[string]json.RawMessage{"input": trimmed}
149 }
150 return redacted
153// encodedLen returns the length of v encoded as JSON with <, > and & escaped,
154// as GitLab measures it, or a length over every limit if v cannot be encoded.
155func encodedLen(v any) int {
156 encoded, err := json.Marshal(v) //nolint:forbidigo // measuring, not output
157 if err != nil {
158 // Over any limit, with room for callers to add to it without overflowing.
159 return math.MaxInt / 2
160 }
161 return len(encoded)
164// auditEventsRequestElements is the number of JSON elements an
165// auditEventsRequest has before its events: the request object and its two
166// members.
167const auditEventsRequestElements = 5
169// auditEventsRequestBytes is the most bytes an auditEventsRequest takes
170// without its events: {"session_id":N,"events":[]} with the largest N.
171const auditEventsRequestBytes = 64
173// auditEventElements returns the number of JSON elements event adds to a
174// request, or a number over every limit if it cannot be encoded.
175func auditEventElements(event auditEventRequest) int {
176 encoded, err := json.Marshal(event) //nolint:forbidigo // measuring, not output
177 if err != nil {
178 return math.MaxInt / 2
179 }
180 var v any
181 if err := json.Unmarshal(encoded, &v); err != nil {
182 return math.MaxInt / 2
183 }
184 return 1 + jsonElements(v)
187// jsonElements counts the elements in v the way GitLab's JSON validation
188// does: two for each object member and one for each array item, plus the
189// elements in each value.
190func jsonElements(v any) int {
191 n := 0
192 switch v := v.(type) {
193 case map[string]any:
194 for _, child := range v {
195 n += 2 + jsonElements(child)
196 }
197 case []any:
198 for _, child := range v {
199 n += 1 + jsonElements(child)
200 }
201 }
202 return n
205// truncateEncoded shortens s, without splitting a character, until its JSON
206// encoding is at most limit bytes, and reports whether it did.
207func truncateEncoded(s string, limit int) (string, bool) {
208 if encodedLen(s) <= limit {
209 return s, false
210 }
211 runeStart := func(cut int) int {
212 for cut > 0 && cut < len(s) && !utf8.RuneStart(s[cut]) {
213 cut--
214 }
215 return cut
216 }
217 // The encoded length only grows as the cut moves right, so the longest
218 // prefix that fits is found by binary search.
219 lo, hi := 0, len(s)
220 for lo < hi {
221 mid := (lo + hi + 1) / 2
222 if encodedLen(s[:runeStart(mid)]) <= limit {
223 lo = mid
224 } else {
225 hi = mid - 1
226 }
227 }
228 return s[:runeStart(lo)], true
231// truncateRunes shortens s to at most limit characters, and reports whether
232// it did.
233func truncateRunes(s string, limit int) (string, bool) {
234 if utf8.RuneCountInString(s) <= limit {
235 return s, false
236 }
237 return string([]rune(s)[:limit]), true
Diagnostics9 git calls · 295 ms
CallStatusSizeTime0295 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 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 · hit2 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=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=3
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 · 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=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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=281
git.response.bytes=638
git.response.cache=HIT
git.response.duration_ms=2
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.0 KiB281 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3067
git.response.cache=MISS
git.response.duration_ms=281
git.fetch.wants=24e879420a0d20cb82850422b1ff9dbe5f872942
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=3067
git.pkt.payload.max=3040
git.sideband.band1.bytes=3040
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
a5e2f6447f193a24
Request
78051cf0…
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