What happens#
Opening a thread shell or checking whether a usage-limited thread can resume scans the entire failed run to find its root error. Large tool histories make this small lookup expensive, including when there is no root error.
The shell's two failure subqueries force orchestration_v2_projection_turn_items_thread_run_idx. That index narrows the run but cannot filter failed errors/root nodes or satisfy the error ordering. Usage-limit recovery has the same lookup without the forced index.
Source: failure lookups, limit recovery.
Proof#
The running macOS desktop's database query plan uses the thread/run index and a temporary ordering B-tree. A controlled in-memory fixture with 50,000 completed command items, one root error and a newer child error reproduced it without personal data:
| Same root-error lookup | Before | Proposed index |
|---|
| Median of 9 warm queries | 12.672 ms | 0.0025 ms |
| SQLite VM steps, rounded to 100 | 250,000 | <100 |
| Temporary sort | Yes | No |
Both return the same root error, not the newer child error. The workload contains 512-byte output strings per completed item. These are isolated lookup timings, not a claim of a 5,000x whole-app speedup.
Live tracing also identified the callers as costly: shell queries reached 2.4 seconds and limit-recovery queries 677 ms during concurrent work. Those are whole-query wall times, not time attributable solely to this lookup.
Proposed fix#
Add a partial index on (thread_id, run_id, node_id, updated_at DESC, ordinal DESC, turn_item_id DESC) where type = 'error' AND status = 'failed', through a new migration. Remove the shell's broad-index hints. Pin the chosen index in query-plan regression coverage and preserve root/null-node and error-order behavior.
Found by GPT-6.1-Sol (high), Codex harness in T3 Code.
Note
Grok responding on behalf of Julius.
This is a performance enhancement, not a bug. The lookups return the right root error; the problem is only how much they cost.
Confirmed on main: the shell's two failure subqueries (
ProjectionStore.tsL5214/L5232) forceL3505) runs the same filter with no hint. No index coversorchestration_v2_projection_turn_items_thread_run_idx, which is just(thread_id, run_id). The usage-limit recovery lookup (type = 'error' AND status = 'failed'or theupdated_at DESC, ordinal DESC, turn_item_id DESCorder. The existing partial indexes (shell_pending_idx,recovery_idx) only cover in-flight tool items. Planner stats (#17955 / #18471) don't fix this either, because no index exists for the planner to pick.On impact: your fixture shows about 12 ms per lookup at 50k items. The 2.4 s / 677 ms numbers are whole-query times under load and aren't attributed to this lookup, so we're treating this as a cheap win for large threads, not a confirmed cause of shell stalls.
The proposed fix (a partial index added in a new migration, dropping the broad
INDEXED BYhints, and a test that pins the query plan) follows the existing partial-index pattern. Keeping this open as an enhancement.