Skip to content

fix(server): preserve stall traces and move sync xcrun and SQLite reads off the event loop - #62

Merged
syz51 merged 2 commits into
forkfrom
task/t3code-30
Nov 25, 2026
Merged

syz51 merged 2 commits into
forkfrom
task/t3code-30

Conversation

@syz51

@syz51 syz51 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Problem

The T3 server's server.trace.ndjson* (11 × 10 MB) rotates completely in ~4.7 minutes on an active host (~930 spans/s; 91% of bytes are <10 ms spans), so whenever EventLoopMonitor detects a stall (such as the residual ~2.1 s stalls on 2026-11-24 after upstream PR pingdotgg#35728 moved primary SQLite writes off the loop), no trace remains minutes later and event-loop-stalls.ndjson contains no span attribution. Several synchronous operations also remained on the server event loop:

  • packages/shared/src/shell.ts: execFileSync("/usr/bin/xcrun", ["--find", "git"], { timeout: 5_000 }) runs every 30 s on macOS when the resolution cache expires.
  • apps/server/src/provider/Drivers/antigravityUsageReader.ts: opened and queried Antigravity .db files (451 files / 1.7 GB on Air) synchronously on the main thread with PRAGMA busy_timeout = 100.
  • apps/server/src/project/AgentSessionScanner.ts and packages/provider-opencode/src/server/usageLimits.ts: opened Codex state_5.sqlite and OpenCode opencode.db synchronously with DatabaseSync on the main thread.
  • apps/server/src/observability/EventLoopMonitor.ts: wrote event-loop-stalls.ndjson using synchronous RotatingFileSink.

Changes

  • Preserve stall traces & attribute spans (apps/server/src/observability/StallTraceCapture.ts, Observability.ts, EventLoopMonitor.ts):
    • Track currently open spans and recent notable spans (>= 50 ms) in bounded memory and attach activeSpans and slowSpans to server.eventLoop.stall and event-loop-stalls.ndjson.
    • Fork an uninterruptible background snapshot on stall that flushes the trace sink, streams server.trace.ndjson{.1,} asynchronously, and atomically preserves up to 4 MB covering the stall window in logs/event-loop-stall-traces/ (retaining up to 10 snapshots with a 30 s cooldown).
    • Switch EventLoopMonitor from synchronous RotatingFileSink to makeAsyncRotatingFileSink.
  • Move sync xcrun lookup off the event loop (packages/shared/src/shell.ts):
    • Replace execFileSync with async execFile in locateXcodeGitWithXcrun, handle synchronous throws safely, and deduplicate concurrent in-flight lookups per cache key.
  • Move remaining sync SQLite readers onto worker threads (apps/server/src/provider/Drivers/antigravityUsageReader.ts, apps/server/src/project/AgentSessionScanner.ts, packages/provider-opencode/src/server/usageLimits.ts, packages/shared/src/nodeSqliteClient.ts):
    • Run antigravityUsageReader SQLite reads on a single lazy scoped worker thread per scan (per-worker pending maps and termination on error).
    • Add optional busyTimeoutMs to NodeSqliteClient so worker startup respects the caller's 100 ms fail-fast lock budget, and switch AgentSessionScanner.readCodexProjectRoots and usageLimits.readStoredApiKeys to NodeSqliteClient.layer({ readonly: true, writerWorker: true, busyTimeoutMs: 100 }) with safe URL parsing.

Verification

  • Before/after measurement on a locked SQLite .db (50 ms BEGIN EXCLUSIVE lock, PRAGMA busy_timeout = 100): synchronous DatabaseSync stalled the event loop for 123 ms and failed with SQLITE_BUSY; worker-backed readAntigravityUsage succeeded (errors: 0) with 2 ms max timer lag (60× lower). Real Pro Antigravity scan (141 .db files / 534 MB): 1,025 ms wall, 2 ms max timer lag, 7 ms max event-loop delay.
  • vp run check:changed: format, lint, knip 1/2, knip 2/2, graphql, and typecheck (37 packages) all pass; all affected tests pass (StallTraceCapture, EventLoopMonitor, antigravityUsageReader, AgentSessionScanner, nodeSqliteClient, status, shell macOS git suite).
  • Remaining macOS posix_spawn offload tracked in board sub-task t3code-327 (Size: M).

@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:XL labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

ℹ️ No successful main baseline artifact is available yet. This run establishes the initial measurement.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire — 5.0 KiB — 6.8 KiB ✅
Codex Thread snapshot wire — 3.8 KiB — 4.9 KiB ✅
Codex Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅
Codex Live turn WebSocket decoded — 21.5 KiB — 24.4 KiB ✅
Codex Live turn messages — 1 — 8 ✅
Claude Total thread wire — 5.0 KiB — 6.8 KiB ✅
Claude Thread snapshot wire — 3.8 KiB — 4.9 KiB ✅
Claude Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅
Claude Live turn WebSocket decoded — 21.9 KiB — 24.4 KiB ✅
Claude Live turn messages — 1 — 8 ✅

Other scenarios

No main baseline exists yet for these 64 scenario measurements.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded — 10.5 KiB — 12.7 KiB ✅
Codex short turn resume WebSocket messages — 2 — 2 ✅
Codex long turn running, cold open HTTP decoded — 281.7 KiB — 302.7 KiB ✅
Codex long turn running, cold open rows — 75 — 75 ✅
Codex long turn running, history page HTTP decoded — 92.4 KiB — 107.4 KiB ✅
Codex long turn running, history page rows — 75 — 75 ✅
Codex long turn finished, cold open HTTP decoded — 381.5 KiB — 410.2 KiB ✅
Codex long turn finished, cold open rows — 151 — 151 ✅
Codex long turn finished, socket cold open WebSocket decoded — 381.6 KiB — 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages — 2 — 2 ✅
Codex long turn finished, socket cold open rows — 151 — 151 ✅
Codex long turn finished, work page HTTP decoded — 186.2 KiB — 200.2 KiB ✅
Codex long turn finished, work page rows — 150 — 150 ✅
Codex idle thread resume WebSocket decoded — 65 B — 200 B ✅
Codex idle thread resume WebSocket messages — 1 — 1 ✅
Codex fleet shell snapshot HTTP decoded — 42.4 KiB — 53.7 KiB ✅
Codex fleet shell snapshot rows — 27 — 27 ✅
Codex fleet shell subscribe WebSocket decoded — 65 B — 200 B ✅
Codex fleet shell subscribe WebSocket messages — 1 — 1 ✅
Codex fleet shell resume check HTTP decoded — 18 B — 100 B ✅
Codex fleet shell resume check rows — 0 — 0 ✅
Codex stale shell reload HTTP decoded — 42.4 KiB — 53.7 KiB ✅
Codex stale shell reload rows — 27 — 27 ✅
Codex stale shell resume WebSocket decoded — 140 B — 400 B ✅
Codex stale shell resume WebSocket messages — 2 — 2 ✅
Codex stale todo list cold open HTTP decoded — 72.4 KiB — 83.0 KiB ✅
Codex stale todo list cold open rows — 75 — 75 ✅
Codex stale todo list cold open nodes — 2 — 4 ✅
Codex stale todo list cold open subagents — 2 — 4 ✅
Codex stale todo list cold open plans — 1 — 2 ✅
Codex stale todo list cold open providerTurns — 1 — 2 ✅
Codex stale todo list cold open checkpoints — 1 — 2 ✅
Claude short turn resume WebSocket decoded — 10.6 KiB — 12.7 KiB ✅
Claude short turn resume WebSocket messages — 2 — 2 ✅
Claude long turn running, cold open HTTP decoded — 283.3 KiB — 302.7 KiB ✅
Claude long turn running, cold open rows — 75 — 75 ✅
Claude long turn running, history page HTTP decoded — 93.4 KiB — 107.4 KiB ✅
Claude long turn running, history page rows — 75 — 75 ✅
Claude long turn finished, cold open HTTP decoded — 384.2 KiB — 410.2 KiB ✅
Claude long turn finished, cold open rows — 151 — 151 ✅
Claude long turn finished, socket cold open WebSocket decoded — 384.3 KiB — 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages — 2 — 2 ✅
Claude long turn finished, socket cold open rows — 151 — 151 ✅
Claude long turn finished, work page HTTP decoded — 188.1 KiB — 200.2 KiB ✅
Claude long turn finished, work page rows — 150 — 150 ✅
Claude idle thread resume WebSocket decoded — 66 B — 200 B ✅
Claude idle thread resume WebSocket messages — 1 — 1 ✅
Claude fleet shell snapshot HTTP decoded — 43.0 KiB — 53.7 KiB ✅
Claude fleet shell snapshot rows — 27 — 27 ✅
Claude fleet shell subscribe WebSocket decoded — 66 B — 200 B ✅
Claude fleet shell subscribe WebSocket messages — 1 — 1 ✅
Claude fleet shell resume check HTTP decoded — 18 B — 100 B ✅
Claude fleet shell resume check rows — 0 — 0 ✅
Claude stale shell reload HTTP decoded — 43.0 KiB — 53.7 KiB ✅
Claude stale shell reload rows — 27 — 27 ✅
Claude stale shell resume WebSocket decoded — 140 B — 400 B ✅
Claude stale shell resume WebSocket messages — 2 — 2 ✅
Claude stale todo list cold open HTTP decoded — 73.4 KiB — 83.0 KiB ✅
Claude stale todo list cold open rows — 75 — 75 ✅
Claude stale todo list cold open nodes — 2 — 4 ✅
Claude stale todo list cold open subagents — 2 — 4 ✅
Claude stale todo list cold open plans — 1 — 2 ✅
Claude stale todo list cold open providerTurns — 1 — 2 ✅
Claude stale todo list cold open checkpoints — 1 — 2 ✅

Baseline: unavailable · PR result: 705b9b5 · Source CI: success

Scenario and decoded snapshot size

10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

  • Codex decoded thread snapshot: 108.5 KiB
  • Claude decoded thread snapshot: 108.8 KiB

Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

@syz51
syz51 marked this pull request as ready for review November 24, 2026 22:03
@syz51
syz51 merged commit 95350b3 into fork Nov 25, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant