← Back to Pull requests

perf(daemon): rig ps resolves each seat's address once, and the lookup costs a third #4150

Closed
unknown closed this pull request without merging on Nov 29, 2026 16:14 UTC
View on GitHub
unknown unknown commented on Nov 29, 2026 10:18 UTC
Author

What a user gets


rig ps answers faster on a busy daemon, most of all when the daemon's CPU is limited. Its projection now does about a third of the work it did.

What changed


  • One ps projection asked for each seat's address two or three times: for its hooks, for its provider-failure record, and for its queue rows. Every ask ran the current-address lookup again. On a 27-rig, 86-seat database that was 226 lookups for 86 names, 72% of the projection's time.

  • getEntries now runs inside withEachAddressResolvedOnce. Each address resolves once in that synchronous pass. Nothing it learned outlives the pass, so the next read still sees a rebind.

  • resolveCurrentSessionAddress uses one reused statement: main's WITH exact … UNION ALL alias … NOT EXISTS lookup, with exact marked NOT MATERIALIZED. It reads the exact row first, and the alias rows only when there's none, in one snapshot without opening a transaction. So it also works inside an open cursor and on read models that offer only prepare. It returns the same rows as main's statement.

  • reusedStatement (from #3214) moves to db/reused-statement.ts, so the lookup can use it. queue-waiting.ts re-exports it.

  • What rig ps reports is unchanged. There's no new refusal or error code, and no cache that outlives a read.
  • Cost


  • Source: +74 −39, net +35, of which 19 lines moved from queue-waiting.ts.

  • Tests: +117 −4.
  • Measured


    The setup:
  • a test database of 27 rigs, 86 seats and about 62,000 queue rows, every seat stopped;

  • the daemon in its own scope under a 28% CPU quota (cpu.max 28000 100000, read back before and after each leg);

  • diagnosis enabled;

  • six legs on a 4-core Linux box, each a fresh copy: published 0.7.25, main without this change (b85c95aa), this branch, this branch, main, 0.7.25;

  • each leg: a 600 s settle, then /api/ps sent 150 ms into a health read and into an attention read (10 times each), then 30 sequential /api/ps reads.
  • | Build | ps behind a slow read (40 reads): median / p90 / max | Over 1 s | 60 sequential reads: median / p90 / max |
    |---|---|---|---|
    | 0.7.25 | 4,751 / 11,955 / 15,546 ms | 30 | 4,181 / 10,365 / 12,163 ms |
    | main | 557 / 740 / 1,043 ms | 1 | 571 / 682 / 856 ms |
    | this branch | 233 / 363 / 460 ms | 0 | 187 / 284 / 575 ms |

  • These daemon legs ran the first version of this branch (769d2af2, two statements in a read transaction). The final single-statement lookup is faster in-process. On a copy of the same database, with no daemon and no background work, one getEntries takes a median of 32-34 ms (20 reps, ABBA), against 43-59 ms for 769d2af2 and 134-142 ms on main.

  • Every variant returned byte-identical output for the default, includeArchived, archivedOnly and seats views.
  • What's left: on this database the idle daemon still uses most of its 28% on background sweeps (event-loop utilization about 0.95). A ps that arrives behind one of them waits for it, which this change doesn't address.

    Tests


  • New ps-resolves-each-address-once.test.ts:

  • - one projection looks each address up once (main: 3 per address);
    - a pass forgets its answers when it ends, including when it throws;
    - the lookup answers as before for an exact name, a derived name, an inactive seat, an archived rig, and inside a caller's transaction;
    - it answers the same inside a caller's open iterate() cursor and on a read model that offers only prepare.
  • session-address-read-plans.test.ts: checks that the lookup is one statement and reads sessions by index.

  • Daemon tsc: passes.

  • The 351 daemon test files that touch the session registry, the waiting view or the ps projection: 348 pass and 2 skip.

  • - startup.test.ts's omp/0 launch case timed out once at 30 s while the box was loaded.
    - Alone it passes on this branch (14.6 s, 16.2 s) and on main (17.4 s).

    🤖 Generated with Claude Code

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Improvements
    Rig status views and session lookups now avoid repeating address resolution and database reads during a single operation, improving efficiency when handling multiple entries.
    Address-resolution behavior remains consistent across transactions, inactive seats, and archived rigs.
    Repeated database queries are reused more efficiently, while address results are scoped to each read operation.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 576 lines (+432 / -144) across 7 files Reviewer: mvschwarz Review duration: 356m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.55) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 576 lines diff across 7 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.380
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #4150"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.8% (MEDIUM) · Higher = larger blast radius
    54.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.6% · Safe to merge
    21.6%