Skip to content

Engine & TUI fixes: clientdb writes, dagql fan-out, high-verbosity traces - #13632

Merged
vito merged 6 commits into
mainfrom
llm-workspace-engine-tui-fixes
Jul 15, 2026
Merged

vito merged 6 commits into
mainfrom
llm-workspace-engine-tui-fixes

Conversation

@vito

@vito vito commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Note

The catalyst for these fixes was something like Workspace.search(".").{{ filePath, lineNumber, matchedLines }} with #13633 - i.e. a nested multi-field selection across a TON of search results, which made the engine crash via thread exhaustion. Alternative fixes could be applied elsewhere but these seem like reasonable boundaries to set to avoid a crash.

Note

Stack 1/5, stacked on #13600 — each PR is based on the one below it, so the diff shows only this PR's own changes. Merge bottom-up; when a PR below merges and its branch is deleted, GitHub retargets this one automatically and the rest of the stack gets rebased.

  1. Engine & TUI fixes: clientdb writes, dagql fan-out, high-verbosity traces #13632 — Engine & TUI fixes 👈 this PR
  2. Workspace file APIs: glob and search #13633 — Workspace file APIs: glob and search
  3. LLM foundation: providers, config, session CLI, and TUI #13634 — LLM foundation: providers, config, session CLI, and TUI
  4. LLM ⇄ Workspace: object tools and Env removal #13635 — LLM ⇄ Workspace: object tools, Env removal, and @agent plugins
  5. llm-workspace dev tooling: skills, tui-qa agent, sparse-read design doc #13636 — llm-workspace dev tooling

Independent engine and TUI fixes, stacked at the bottom of the llm-workspace series to keep it linear (they don't depend on #13600's changes and could also target main):

  • fix(clientdb): cap SQLite pool at one connection
  • fix(engine): batch client DB log writes
  • fix(dagql): bound parallel resolution fan-out
  • fix(dagui): let verbosity expand rolled-up spans
  • feat(trace): fetch whole trace at high verbosity

🤖 Generated with Claude Code

@vito
vito changed the base branch from workspace-overlay-export to main July 13, 2026 21:21
@vito
vito force-pushed the llm-workspace-engine-tui-fixes branch from a5c391f to 320d61a Compare July 13, 2026 21:27
@vito
vito changed the base branch from main to workspace-overlay-export July 13, 2026 21:34
@vito
vito force-pushed the llm-workspace-engine-tui-fixes branch from 320d61a to a0981f4 Compare July 13, 2026 21:57
@vito
vito marked this pull request as ready for review July 13, 2026 22:58
@vito
vito requested a review from sipsma July 13, 2026 22:59
Base automatically changed from workspace-overlay-export to main July 13, 2026 23:01
vito added 5 commits July 14, 2026 01:56
database/sql had no MaxOpenConns limit, so each concurrent query got
its own SQLite connection. SQLite only allows one writer at a time;
under write contention every extra connection spins in the busy
handler, and in modernc.org/sqlite that wait is a raw nanosleep
syscall pinning an OS thread per connection. During a burst of
concurrent telemetry inserts this exhausted the Go runtime's
10000-thread limit and crashed the engine.

With a single connection, concurrent queries queue in database/sql's
pool (plain goroutine parking) rather than busy-waiting inside SQLite.
This trades away WAL-mode read/write concurrency, which is acceptable
now that log writes are batched and reads are periodic bounded drains.

Signed-off-by: Alex Suraci <[email protected]>
clientLogs was registered directly as a log Processor, so every log
record emitted anywhere in the engine did a synchronous InsertLog into
the per-client SQLite DB on the emitting goroutine (plus one more per
parent client). A single query fanning out into thousands of parallel
resolutions (e.g. a large Workspace.search result with all fields
selected) piled thousands of concurrent writers onto SQLite's write
lock, each busy-waiting in a raw nanosleep syscall that pins an OS
thread in modernc.org/sqlite, crashing the engine with "fatal error:
thread exhaustion" once the runtime hit its 10000-thread limit.

Wrap the exporter in a BatchProcessor with the same NearlyImmediate
interval already used for live span batching, so records are queued
and inserted by one background goroutine per client instead. The
synchronous OnEmit path is removed so it can't be reintroduced by
accident.

Signed-off-by: Alex Suraci <[email protected]>
Resolve and the list-element path in resolvePath used unbounded conc
pools: one goroutine per sibling selection and one per list element,
recursively. A single query selecting several fields on a large list
(e.g. thousands of Workspace.search results) exploded into tens of
thousands of concurrent resolutions, each emitting telemetry, which
contributed to a thread-exhaustion engine crash.

Cap each pool at 100 goroutines. Pools are per-node, so this bounds
fan-out width rather than total request concurrency, and each pool's
limiter is independent, so a blocked parent pool cannot deadlock a
child pool. Legitimate parallel workloads still run 100 wide per
level; pathological fan-outs are throttled instead of unbounded.

Signed-off-by: Alex Suraci <[email protected]>
IsExpanded gated tool-call and rolled-up spans behind an unconditional
neverExpand check, so no verbosity level could ever open them. Zooming a
slow LLM tool call with 'dagger trace --span <id> -vvvvv' showed the same
collapsed rollup as with no flag, hiding the call tree beneath it.

Let a verbosity at or above the expand-completed threshold punch through
the rollup boundary, matching the existing alwaysExpand gate, and honor
per-span verbosity like the sibling ShouldShow checks. Below that
threshold the guts stay hidden as before.

Signed-off-by: Alex Suraci <[email protected]>
The trace loader always streamed priority spans and backfilled subtrees
lazily on expand. But at a verbosity that expands every completed span
(or --debug/--expand), the whole tree renders open, so lazy loading just
leaves never-expanded spans unfetched in report mode's single render and
costs a round-trip per expand interactively.

Fetch the entire trace up front (Incremental: false) when the tree is
going to be fully expanded anyway, keyed on the same threshold IsExpanded
uses. Lower verbosities keep the incremental behavior.

Signed-off-by: Alex Suraci <[email protected]>
@grouville
grouville force-pushed the llm-workspace-engine-tui-fixes branch from a0981f4 to e38e693 Compare July 14, 2026 08:56
Comment thread engine/server/session.go Resolved
@grouville
grouville self-requested a review July 14, 2026 13:33

@grouville grouville left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there may be a meaningful regression in execution time for test-module? Saw a few runs and they were all way longer than it used to be. Not entirely sure but the concurrent resolvers could become a bottleneck for the type introspection queries that module loading especially does.

Could try just bumping the max concurrent resolvers. But I am also wondering if having a cap is really necessary if the fix is around thread-exhaustion. Goroutines don't force thread creation unless runtime.LockOSThread() is used and you are supposed to be able to run millions of goroutines on a single thread.

@vito

vito commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@sipsma Had an agent chew through different permutations with --cloud and it's hard to determine anything conclusive; there was a ton of drift in CI timing, even for main. I also reran test-split:test-modules and it went from 22m to 12m but I don't have a good idea of what's normal there.

I agree though that fe763ea is probably redundant with e0e911e whichs lock the SQLite connection pool to 1, which should fix that source of OS thread locking (which is the real root cause of the crash).

I opted to just include it anyway because it seemed like capping it at 100 per node (not the entire request) would be unlikely to affect normal paths and serve as a last line of defense for weird cases like this, but I have really low conviction there so I'll just drop it until we're sure it's needed; if we had it prior to this PR it may have just obfuscated the root cause.

@sipsma

sipsma commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

@sipsma Had an agent chew through different permutations with --cloud and it's hard to determine anything conclusive; there was a ton of drift in CI timing, even for main. I also reran test-split:test-modules and it went from 22m to 12m but I don't have a good idea of what's normal there.

I agree though that fe763ea is probably redundant with e0e911e whichs lock the SQLite connection pool to 1, which should fix that source of OS thread locking (which is the real root cause of the crash).

I opted to just include it anyway because it seemed like capping it at 100 per node (not the entire request) would be unlikely to affect normal paths and serve as a last line of defense for weird cases like this, but I have really low conviction there so I'll just drop it until we're sure it's needed; if we had it prior to this PR it may have just obfuscated the root cause.

@vito Okay yeah I just saw a run last night that was slow and when I checked again this morning a new one was slow, so I assumed n=2 was statistically significant 🙂 Thanks for the extra checking, if it falls within noise range then no worries, we can iterate on it if anything concrete comes up

@vito

vito commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Cool np - the agent is still working away and trying to make sense of things, letting it ride for now. None of my PRs are stacked on this and I only hit it from an LLM being an agent of chaos, so might as well let it cook.

edit: Here's the report:

Investigation: test-split:test-modules slowdown on PR #13632

I bisected the three engine knobs (the other two commits are TUI-only and can't affect test execution) by cherry-picking them incrementally onto main and running the real check 8× via dagger --x-release 1.0.0-beta.6 --cloud call test-split test-modules. Metric = the go-test package self-timer for core/integration (isolates test execution on the engine-under-test; excludes build/connect). The one failing test (TestPrivateGitRepoArgCaching, needs private-git creds I don't have) is constant and ignored.

The trap (first pass, sequential)

run knobs (cumulative) go-test
1 main 420s
2 +SQLite pool cap=1 451s
3 +batch log writes 484s
4 +dagql fan-out cap=100 577s

This looked like a clean per-knob creep with the fan-out cap as the culprit (+93s) — exactly sipsma's hypothesis. But it's an artifact.

The control that broke it

A 5th run of plain main came back at 522s, not ~420s. Identical code, +102s. The shared cloud engine drifts — each successive run gets slower regardless of code.

Drift-controlled bracket (cap vs no-cap in adjacent runs)

run state cap? go-test
6 state2 no cap 577.6s
7 state3 cap 554.9s
8 state2 no cap 519.3s

Fan-out cap effect = 554.9 − (577.6+519.3)/2 = +6.4s → noise. The adjacent deltas even straddle zero (cap −22s vs run6, +36s vs run8).

All 8 no-cap measurements: 420, 451, 484, 522, 577, 519 — a 158s spread on identical code. That ±90s variance swamps any per-knob effect. Per-operation type definitions (introspection) span durations tell the same story: cap runs sit ~2-3s higher only at p90, medians identical, all within the run-order drift.

Verdict

None of the three knobs produces a slowdown in test-modules that's separable from cloud run-to-run drift. The naive bisect was fooled by the engine getting ~25s/run slower on its own (main alone drifted 420→522; no-cap state2 went 484→577→519). The fan-out cap — the prime suspect — adds ~+6s in a drift-controlled test.

For sipsma specifically:

  • His mechanistic worry (cap bottlenecks introspection) doesn't materialize: the cap only engages when one resolution level exceeds 100 parallel elements. Normal module introspection stays well under that, so it rarely bites — the >100 fan-out it targets is the pathological Workspace.search-over-thousands case (PR Workspace file APIs: glob and search #13633), which isn't in test-modules. (Resolve even has a no-pool fast path for single selections.)
  • His "is the cap even necessary?" point stands as a design question — the thread-exhaustion root cause was SQLite's busy-handler nanosleep pinning OS threads, fixed by the SQLite-pool + batched-log knobs — but relaxing the cap wouldn't recover a test-modules regression, because there isn't one attributable to it.

Bottom line: the "way longer" runs are most consistent with CI/cloud variance, not these knobs. I'd caution against tuning the cap to chase this specific regression.

Caveat: I couldn't get a stable baseline (main = 420–522s), so this cloud setup is too noisy to confirm or deny a small (<~90s) real regression. If you want certainty, the clean way is interleaved cap/no-cap runs on a fresh engine each time (or I can write a pure-resolution introspection micro-benchmark to bound the cap's cost directly, ~2 runs). Want me to do either?

(Bisect states preserved as tags bisect-state{0,1,2,3}-*; full log at /tmp/bisect-results.md.)

This reverts commit fe763ea.

Should be redundant with the SQL conn pool cap.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 65ae06f into main Jul 15, 2026
87 checks passed
@vito
vito deleted the llm-workspace-engine-tui-fixes branch July 15, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants