Skip to content

perf(core): parallelize resolveTier's independent storage reads - #6441

Open
pedrofrxncx wants to merge 1 commit into
mainfrom
perf/resolve-tier-parallel-reads-w4
Open

perf(core): parallelize resolveTier's independent storage reads#6441
pedrofrxncx wants to merge 1 commit into
mainfrom
perf/resolve-tier-parallel-reads-w4

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Source: storage-ports latency-investigation hunt (issue #2995 — "DB latency amplifies significantly across MCP tool calls"). resolveTier (apps/api/src/core/resolve-tier.ts) is on the model-resolution path for interactive chat and every automation/task-board/review-judge call that picks a tier — it runs on essentially every agent turn.

Payoff: the function issued three independent DB reads back-to-back with await: org settings, the per-user model-preference override, and the AI provider key list. None of the three depends on another's result, so serializing them adds up to two extra round-trips of latency to every tier resolution for no reason. This change fetches all three concurrently with Promise.all, same conditional skip for the user-prefs read (still not issued when applyUserPrefs is off).

Behavior: unchanged — same three conditions gate the user-prefs read, same values feed orgSlot/userSlot/keys afterward, only the await ordering changed from serial to parallel.

How a reviewer confirms: run bun test apps/api/src/core/resolve-tier.test.ts (all 10 cases still pass, including the one asserting userModelPreferences.get is NOT called when applyUserPrefs is unset).

Checks run locally: bun run fmt, cd apps/api && bunx tsc --noEmit (clean), bun test apps/api/src/core/resolve-tier.test.ts (10/10 pass), bunx oxlint apps/api/src/core/resolve-tier.ts (0 warnings/errors). Full CI validates the rest.


Summary by cubic

Parallelizes resolveTier’s independent storage reads to reduce per-turn latency in model resolution. Previously, org settings, user model preferences (conditionally), and AI provider keys were read sequentially; they now load in parallel with unchanged behavior, including skipping the user-preferences read when it does not apply. This supports issue #2995 on DB latency across MCP tool calls.

  • Review notes
    • The gating for user preferences is unchanged: it applies only when applyUserPrefs is set, the tier is a chat tier, and a userId exists; the read is skipped otherwise.
    • Run tests: bun test apps/api/src/core/resolve-tier.test.ts (ensures the skip behavior and overall resolution logic remain intact).
    • No API or schema changes; no migration or config updates required.

Written for commit f1f9afa. Summary will update on new commits.

Review in cubic

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.

1 participant