Skip to content

perf(server): workflow questions resolve run threads in one query - #47734

Merged
juliusmarminge merged 1 commit into
mainfrom
perf/workflow-latest-thread
Dec 2, 2026
Merged

juliusmarminge merged 1 commit into
mainfrom
perf/workflow-latest-thread

Conversation

@juliusmarminge

@juliusmarminge juliusmarminge commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Problem

Every list of open workflow questions looked up each question's latest agent thread with its own SQL statement (WorkflowStore.latestThread). On nucbox-1 (~44k runs, 1,600 open questions) Tempo showed 288,527 WorkflowStore.latestThread spans in 24h, about 1,600 per t3_workflow call (tools/call t3_workflow → Workflows.questions → latestThread). mcp.tool.t3_workflow p95 was 268 ms. The Workflows watch snapshot uses the same questions effect, so it ran the N+1 after every debounced change.

Fix

WorkflowStore.latestThread(runId) is now WorkflowStore.latestThreads(runIds). It returns a map with one entry per run that has had an agent step, found with a single statement:

SELECT wanted.value AS run_id, step.thread_id, step.environment_id
FROM json_each(?) AS wanted
JOIN workflow_steps AS step ON step.rowid = (
  SELECT rowid FROM workflow_steps
  WHERE run_id = wanted.value AND thread_id IS NOT NULL
  ORDER BY started_at DESC, rowid DESC LIMIT 1
)

The ordering is unchanged (started_at DESC, rowid DESC). Runs with no thread still give thread: null. Nothing changes in the output, the schema or the indexes.

Measurements

nucbox-1 production DB, opened read-only with node:sqlite: 43,958 runs, 271,499 steps, 1,600 open questions, 373 of which have a thread. 20 samples after 3 warm-ups:

p50 min max
before: 1,600 statements 18.95 ms 18.18 ms 23.13 ms
after: 1 statement 14.76 ms 14.14 ms 15.86 ms

Both versions returned the same thread for all 1,600 runs (0 mismatches).

Through the server's SqlClient and Effect.fn, which is what production pays: one span and one Effect statement per lookup. Temp DB with 1,600 runs × 13 steps:

p50 min max
before: 1,600 latestThread calls 56.21 ms 46.28 ms 74.59 ms
after: one latestThreads call 5.19 ms 4.07 ms 15.28 ms

The raw SQLite time drops only modestly. Most of the saving is the per-statement Effect and span overhead, plus 1,600 fewer spans exported to Tempo per call.

EXPLAIN QUERY PLAN on the nucbox-1 DB:

SCAN wanted VIRTUAL TABLE INDEX 1:
SEARCH step USING INTEGER PRIMARY KEY (rowid=?)
CORRELATED SCALAR SUBQUERY 1
  SEARCH workflow_steps USING INDEX sqlite_autoindex_workflow_steps_1 (run_id=?)
  USE TEMP B-TREE FOR ORDER BY

This is the same per-run index probe as before, now done inside one statement.

Tests

  • WorkflowStore.test.ts now covers the batched read: the latest thread by start time, a null environment, runs without a thread, a duplicate id and an unknown id.
  • Workflows.test.ts (76 tests) and mcp/toolkits/workflow/handlers.test.ts pass.
  • vp run check:changed passes.

Opus 5.5 via Claude Code (daily perf audit)

🤖 Generated with Claude Code

Listing open workflow questions looked up each question's latest agent
thread with its own statement and span, so t3_workflow and the Workflows
watch ran one query per open question (~1,600 on a busy host).
WorkflowStore.latestThreads now answers every run in one statement.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Dec 2, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 68648ce

Macroscope's review found this PR approvable — Workflow questions now resolve their latest run threads in one batched query rather than one query per question. This is a read-only optimization with no product-default or stored-data changes.

You can add or adjust custom eligibility rules. Learn more.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

ℹ️ The exact PR base did not have a successful artifact. Baseline uses the latest successful main measurement shown below.

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

Other scenarios

All 64 scenario measurements match main.

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

Baseline: 9b419ae · PR result: 68648ce · 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.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The workflow store now retrieves latest threads for multiple runs in one call. The questions flow uses this lookup to associate each question with its run’s thread or null.

Changes: Workflow thread lookup

Layer / File(s) Summary
Batch latest-thread lookup
apps/server/src/workflows/WorkflowStore.ts
latestThreads accepts multiple run IDs and returns a map of runs with matching threads. It deduplicates IDs and omits runs without a matching thread.
Questions flow integration and validation
apps/server/src/workflows/WorkflowEngine.ts, apps/server/src/workflows/WorkflowStore.test.ts
The questions flow uses one batch lookup for runs with non-null requests. Tests cover multiple runs, duplicate and missing IDs, and runs without threads.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: saphid, ndycode

Merge Risk: Minimal · up to 68648

This change batches thread lookups for open questions into a single query to reduce query and span overhead, without changing output or schema. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "perf(server): workflow questions resolve run threads in one query" matches the main change: open-question lookups of each run's latest thread were batched from one query per question into a…
Description check Passed The description follows the template. It has Problem, Fix (Change), Measurements, and Tests sections, and it explains the N+1 issue with concrete reproduction data. The Scope section is missing. The d…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "perf(server): workflow questions resolve run threads in one query" matches the main change: open-question lookups of each run's latest thread were batched from one query per question into a single latestThreads call. It follows the conventional commit format the template recommends.

Full details: Description check

Explanation

The description follows the template. It has Problem, Fix (Change), Measurements, and Tests sections, and it explains the N+1 issue with concrete reproduction data. The Scope section is missing. The description does not link an issue or state why no issue is needed, and it does not say whether a maintainer requested the PR. The Verification section gives focused test names, timing results, and an EXPLAIN QUERY PLAN. It also names the model and harness used, as the template asks. The description also gives production-scale numbers, which support the verification requirement.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 297b9cc and 68648ce.

Files selected for processing: 3

  • apps/server/src/workflows/WorkflowEngine.ts
  • apps/server/src/workflows/WorkflowStore.test.ts
  • apps/server/src/workflows/WorkflowStore.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@juliusmarminge
juliusmarminge merged commit 5440750 into main Dec 2, 2026
39 checks passed
@juliusmarminge
juliusmarminge deleted the perf/workflow-latest-thread branch December 2, 2026 19:35
AIdoesmyjob pushed a commit to AIdoesmyjob/t3code that referenced this pull request Dec 3, 2026
aorwall added a commit to aorwall/t3code that referenced this pull request Dec 3, 2026
Merges upstream `pingdotgg/t3code` `3067c49847` (301 commits since
`fc5ddf3d3c`).

> **Merge this PR with "Create a merge commit". Do not squash it.**
> pingdotgg#323 was squash-merged, so `fc5ddf3d3c` stopped being an ancestor of
`main` and this merge's base fell back to `c16ad0ce17`: 1013 commits and
180 forecast conflicts. The first commit here, `git merge -s ours
fc5ddf3` with the tree unchanged, puts the ancestry back. A squash
would throw that repair away along with this merge's own parent.

## Commits (read in order)
1. `chore: record upstream fc5ddf3 as merged`: the ancestry repair,
with no content change (`git diff f455503 HEAD^` is empty).
2. `chore: merge upstream t3code to 3067c49`: the merge itself, the
`threads.moveWorkspace` union entry, and the docs/fork updates.

## Numbers
- Landed 817 files against 819 in the upstream range. The two missing
files are `apps/server/src/cli/pair.ts` and its test, which the fork
deletes on purpose.
- Fork delta: 802 files.

## Conflicts (5, each resolved by its verdict)
- `cli/pair.ts` and `cli/pair.test.ts`: upstream modified them and the
fork deletes them, so they stay deleted.
- `ui/textarea.tsx`: upstream's class string, plus the fork's `font`
prop.
- `agentContextBudget.json`: upstream's values, with
`mcpToolListJsonBytes` set to upstream + 108 (63684).
`AgentContextBudget.test.ts` passes.
- `pnpm-lock.yaml`: re-derived from upstream's copy.

`duplicate-adds` and `resolution-check` are clean. The owned-concern
sweep matched 6 new files: four vendored libghostty headers and two e2e
scenarios, all false positives.

## Feature classification
**Usable as-is**
- Effect 4.2.0 (pingdotgg#47767) and libghostty-vt 1.0.2 (pingdotgg#47842).
- Several hundred web, mobile and shared fixes: footnotes, previews, the
workflow panel, PR UI and more.

**Unsupported in Moatless / needs implementation**
- `threads.moveWorkspace` (pingdotgg#47234: "Move to project…", the palette
folder pick, and recovery from a missing workspace). The union entry is
added. Every entry point checks the `threadWorkspaceMoves` capability,
which Moatless does not report, so the surface is hidden with no fork
gate. Recorded in gaps.md under the methods and capabilities entries.
- Blume docs site at t3.codes/docs (pingdotgg#44630): upstream publishing, not
relevant to the fork.

**Backend behavior to consider reproducing in Moatless** (added to
gaps.md under runtime fixes)
- Worktree recovery and setup reruns after a stopped turn, a retry, or a
failed hook (pingdotgg#47694, pingdotgg#47681, pingdotgg#47679, pingdotgg#47685).
- Commits stop when the checkout moves under them (pingdotgg#47617, pingdotgg#47526,
pingdotgg#47438), and reused PR worktrees stay on the recorded head (pingdotgg#47596,
pingdotgg#47528, pingdotgg#47799).
- Scheduled runs resume watching their turn after a restart (pingdotgg#47732) and
don't fire twice for one slot (pingdotgg#47722).
- Codex resumes and forks keep the selected reasoning effort (pingdotgg#47811).
- Perf: shell snapshot queries (pingdotgg#47780), workflow questions answered in
one query (pingdotgg#47734), bounded history reads (pingdotgg#47544), event-log pruning
(pingdotgg#47862).

## Verification
- `verify.mjs`: the parallel pass got the pod evicted, so it was re-run
with `--sequential`. Everything except tests is green: duplicate-adds,
tripwires, resolution-check, unsupported-methods (after the ADD),
lockfile, fmt, lint, typecheck and build.
- Tests: 39 of 40 completed packages pass. `@t3tools/shared` fails only
`AgentScope`, a known OOM-score sandbox gap.
- **Not fully verified: `t3` (apps/server) did not finish.** The
container stopped after about 3 hours, 84 files in. Every failure up to
that point is a known sandbox gap:
  - git `insteadOf`, Chromium, and the Codex `/tmp` sticky bit.
- `ProjectionStore` and `Orchestrator.control-reads`, which call Node
26's `StatementSync.stat` while the sandbox runs Node 24.
- The `t3` tests beside the fork's server delta were run separately: 12
files, 366 tests, all passing. CI should run the rest.
- The commit skipped the known knip pre-commit failure.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---
Moatless task:
https://moatless.soaplabstest.com/tasks/bc02b1b2-62c6-4ed0-9429-49bb17da1c4c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). 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