Skip to content

Commit 1fe72dd

Browse files
committed
review: add comments for decisions
1 parent 3195ca5 commit 1fe72dd

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

server/lib/cdpmonitor/computed.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/onkernel/kernel-images/server/lib/events"
99
)
1010
const (
11+
// networkIdleDebounce matches Playwright's networkidle heuristic: fire after
12+
// 500 ms with no in-flight network requests.
1113
networkIdleDebounce = 500 * time.Millisecond
1214
layoutSettledDebounce = 1 * time.Second
1315
)

server/lib/cdpmonitor/handlers.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ func (m *Monitor) handleTimelineEvent(params json.RawMessage, sessionID string)
142142
m.computed.onLayoutShift()
143143
}
144144

145+
// handleNetworkRequest publishes network_request events.
146+
// NOTE: events include raw headers, post_data, and (on response) truncated
147+
// bodies which may contain cookies, bearer tokens, or other credentials.
148+
// This mirrors what CDP/DevTools itself exposes. Consumers should treat the
149+
// event stream as privileged data; opt-in redaction can be added later.
145150
func (m *Monitor) handleNetworkRequest(params json.RawMessage, sessionID string) {
146151
var p cdpNetworkRequestParams
147152
if err := json.Unmarshal(params, &p); err != nil {
@@ -156,7 +161,12 @@ func (m *Monitor) handleNetworkRequest(params json.RawMessage, sessionID string)
156161
initiatorType = raw.Type
157162
}
158163

164+
// Redirects reuse the same requestId and fire additional requestWillBeSent
165+
// events, but only a single loadingFinished fires per chain. Only increment
166+
// netPending for genuinely new requests to avoid permanently inflating the
167+
// counter and blocking network_idle.
159168
m.pendReqMu.Lock()
169+
_, isRedirect := m.pendingRequests[p.RequestID]
160170
m.pendingRequests[p.RequestID] = networkReqState{
161171
sessionID: sessionID,
162172
method: p.Request.Method,
@@ -180,7 +190,9 @@ func (m *Monitor) handleNetworkRequest(params json.RawMessage, sessionID string)
180190
}
181191
data, _ := json.Marshal(ev)
182192
m.publishEvent(EventNetworkRequest, events.DetailStandard, events.Source{Kind: events.KindCDP}, "Network.requestWillBeSent", data, sessionID)
183-
m.computed.onRequest()
193+
if !isRedirect {
194+
m.computed.onRequest()
195+
}
184196
}
185197

186198
func (m *Monitor) handleResponseReceived(params json.RawMessage, sessionID string) {

server/lib/cdpmonitor/monitor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ func (m *Monitor) failPendingCommands() {
187187
}
188188

189189
// readLoop reads CDP messages, routing responses to pending callers and dispatching events.
190+
// On read error (WS drop) this goroutine returns. Reconnection is driven by
191+
// subscribeToUpstream: the UpstreamProvider always pushes a fresh devtools URL
192+
// when the browser process restarts, so same-URL redial is not needed here.
190193
func (m *Monitor) readLoop(ctx context.Context) {
191194
m.lifeMu.Lock()
192195
done := m.done

server/lib/events/filewriter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"sync"
88
)
99

10+
// maxLogFileSize is the per-category file size cap. Writes beyond this limit
11+
// are silently dropped to prevent unbounded disk usage on constrained instances.
12+
const maxLogFileSize = 64 << 20 // 64 MB
13+
1014
// fileWriter is a JSONL appender keyed by filename. It opens each file lazily
1115
// on first write (O_APPEND|O_CREATE|O_WRONLY) and serialises all concurrent
1216
// writes with a single mutex.
@@ -45,6 +49,14 @@ func (fw *fileWriter) Write(filename string, data []byte) error {
4549
fw.files[filename] = f
4650
}
4751

52+
info, err := f.Stat()
53+
if err != nil {
54+
return fmt.Errorf("filewriter: stat: %w", err)
55+
}
56+
if info.Size() >= maxLogFileSize {
57+
return nil // silently drop; cap reached
58+
}
59+
4860
if _, err := f.Write(data); err != nil {
4961
return fmt.Errorf("filewriter: write: %w", err)
5062
}

0 commit comments

Comments
 (0)