Skip to content

perf(server): stop calling our own API over HTTP during render, execution, and tool runs - #6660

Open
waleedlatif1 wants to merge 12 commits into
stagingfrom
perf/in-process-server-reads
Open

perf(server): stop calling our own API over HTTP during render, execution, and tool runs#6660
waleedlatif1 wants to merge 12 commits into
stagingfrom
perf/in-process-server-reads

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

Server-side work that was being done over HTTP against our own API now runs in-process, plus a set of query-layer correctness fixes found while auditing the same code.

Self-HTTP hops removed — each one cost a round trip through the load balancer plus a full re-authentication to re-derive identity the caller already held:

  • Router and evaluator blocks called POST /api/providers per block. They now call the provider runtime directly through a shared executeBlockProviderRequest, which reproduces the two admission checks that route owned.
  • Credential-using tool executions minted an internal JWT and POSTed it to POST /api/auth/oauth/token on every call, including retries. The route body moved to lib/oauth/token-resolution.ts; the route and the executor now run one identical authorization path. The browser path still goes over HTTP with the session cookie.
  • Copilot checkpoint revert PUT'd to /api/workflows/[id]/state, forwarding cookies so the other side could re-verify the session it had just verified. The PUT body moved to lib/workflows/persistence/save-normalized-state.ts, which owns authorization, the lock check, the row-locked write, custom-tool extraction, and the socket notification — so no surface can skip a step by going through a different door.

Payload and query work

  • The workspace file list is seeded into the document on every workspace route. It is now budgeted: over the budget it seeds nothing rather than a prefix (a truncated seed would silently hide files), and the read now stops before the share join and contract parse, so the large workspaces the budget protects pay least.
  • The two workspace-wide file reads project only the columns the mapper uses instead of select().
  • getWorkspaceWithOwner's request memoization was keyed on includeArchived, so the gates that disagree about archived visibility each got their own entry and it deduped nothing. It now reads the superset once and filters per caller.

Correctness fixes

  • A second row-cache walk still used a prefix shared with the search-results entry, whose shape has no rows — a cell edit with a search view open threw in onMutate and rejected the mutation. Both walks now use an allowlist prefix.
  • useCloudStorageConfigured combined an infinite staleTime with retry: false under the global retryOnMount: false, so one transient error disabled cloud-backed uploads for the tab's lifetime with no way to recover.
  • CloudWatch selector lists forwarded search into the fetch without it appearing in the query key, so different searches shared one cache entry.
  • Optimistic temp row ids now come from generateId().
  • Broke a new import cycle between the subscription and workspace-usage query modules by moving both key factories to hooks/queries/utils/, matching the existing convention.

Type of Change

  • Bug fix
  • Performance improvement

Testing

1,511 tests pass across the touched areas, including new coverage for the extracted modules, the seed budget, and the row-cache prefix. Verified the new tests can fail: disabling the workspace-access guard turns the router suite red, and restoring the removed HTTP hop in the revert route turns four tests red.

Four test files fail to load in this worktree on a pre-existing postcss/CSS-module resolution issue unrelated to these changes (one of them is a file this branch never touches).

type-check, biome, check:api-validation, check:react-query, check:client-boundary, and check:tool-registry-boundary all pass.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

The audit found this key was the workspaceFilesKeys bug waiting to recur. The
manager's record type and workspaceFileFolderSchema are two independent
declarations that agree today by coincidence; the seed had no parse, so adding a
column to one would have silently cached a shape a client fetch strips — and
three of its fields are z.coerce.date(), the exact divergence that put ISO
strings under the file-list key.

Its sibling is immune because listWorkspaceFilesWithShares parses at the data
layer. This does the same at the seed, and adds the shape-parity assertion the
key never had. Verified falsifiable: removing the parse turns it red. Doing so
also exposed the existing folder test as fixture-thin — a folder with only an
id, which the contract rightly rejects — so it now uses a real row.

Also points the credential block's fetchQuery at the exported staleTime
constant instead of restating 60 * 1000; it was a fifth producer on that key
free to drift from the four that share it.
…ling shut

Two functional bugs found auditing the query layer.

patchCachedRows walked tableKeys.rowsRoot non-exact, but rowsRoot is a prefix:
the find (search results) and write (pending writes) subtrees hang off it with
non-paged shapes, and the updater's old.pages.map threw on them. It runs inside
onMutate, so the whole cell edit rejected before reaching the server — reachable
as soon as a find entry exists, i.e. after the user searches the table once. The
sibling isDefaultOrderRowsQuery already excluded those subtrees and its docstring
claimed they "never match"; that was only true of the sibling. Both now share one
isRowListQueryKey helper so they cannot drift apart again.

useCloudStorageConfigured combined staleTime: Infinity, retry: false, and the
global retryOnMount: false on a workspace-independent key, so one transient
failure left it errored for the tab's lifetime with no way back — navigating or
switching workspace cannot change the key, and the upload path fails closed, so
cloud-backed uploads stayed disabled until a full reload. useVoiceSettings
carries the same three options and already escapes this with retryOnMount: true;
this one now matches.

Note: hooks/queries/workspace-files.test.tsx cannot load in a git worktree
(pre-existing postcss resolution failure), so CI is the first place that file
runs against this change.
The workspace row was read ~3x per workspace route and ~5x on settings, and the
same Max-tier entitlement was resolved twice on one render.

Memoization is deliberately partial. getWorkspaceWithOwner accepts a
transaction and forUpdate, and live callers use both, so only the plain
no-options read routes through the memo; a row read inside one caller's
transaction or under a lock it alone holds can never be served to a later
caller. includeArchived is part of the key so the two variants cannot alias.

Three substitutions were considered and rejected as behavior changes, not
optimizations: hostContext.ownerBilling resolves subscriptions differently from
hasWorkspaceTierAccess and exposes no Max tier, so it cannot answer the
Inbox/Sandbox gates; isOrganizationOnEnterprisePlan carries self-host
short-circuits ownerBilling has no equivalent for; and widening
WorkspaceHostContext to carry the full row would push owner and org ids onto
the wire for every viewer to save a server-side read, since that type is a
response contract rather than an internal struct.
…sing the credential gate

The CloudWatch log-group and log-stream selectors forwarded `search` into the
request as `prefix` but left it out of the query key, so every keystroke
resolved to the same fresh entry and no refetch fired. Server-side filtering
was dead: a log group outside the first page could not be reached. An audit of
all 69 selector definitions found these two and no others.

useSelectorOptions resolved `args.enabled ?? definition.enabled(...)`, so a
caller supplying its own gate replaced the definition's precondition rather
than narrowing it. useSelectorDisplayName knows nothing about credentials, so a
card holding a saved value with no credential context ran a query that could
only reject. The two are now conjoined. The detail hooks keep the override
deliberately — resolving one known id needs less context than listing, which
their TSDoc already documents.

The list-key fix has a test, proven to fail without it. The `enabled` change
has none: loading use-selector-query pulls the selector registry and emcn CSS,
which cannot resolve in a git worktree.
generateTempId used Date.now(), so two rows created in the same millisecond
shared an id and the first server response overwrote both — leaving one row
duplicated and the other's real id lost until a refetch. Now uses generateId(),
matching what the workflow mutations already do. Reachable by double-clicking
create, or by any scripted or bulk create.

Also documents the contract of fetchOAuthConnections, which reports an unknown
connection state as disconnected. No consumer reads that field today — both
read names and icons, and connection state comes from useWorkspaceCredentials —
so letting the query reject would blank the suggested-action rows and drop the
credential page to raw provider ids. The note is what stops a future consumer
branching on it silently.
Each was verified against the mutation that changes the data and the keys that
expose it, not taken on report.

- Workspace usage/credits were invalidated nowhere in the app. Six sites already
  refreshed subscriptionKeys after credits moved — post-run, post-wand, limit
  edits, upgrades, top-ups — and none touched workspace usage, so the credits
  chip and the run gate held their page-load values until a reload. Adds one
  shared invalidateWorkspaceUsage and calls it from all six.
- Knowledge-base list doc counts went stale: document upload, delete, and bulk
  delete invalidated only the detail key, though the list carries docCount.
- Plan switches that do not redirect refreshed only the host context, leaving
  subscription and credit state showing the previous plan.
- The copilot tool-event handler invalidated a raw workflowKeys.list, which
  covers only the active scope and skips the selector prefix; it now uses the
  shared invalidateWorkflowLists like the other thirteen call sites.
- scheduleKeys.byId was a strict prefix of scheduleKeys.schedule, so the two
  addressings aliased, and nothing invalidated byId. De-aliased and invalidated.

Not changed: the CSV preview key already folds in the file version and storage
key, so a content update addresses a different cache entry — version-in-key is
the mechanism there, not a missing invalidation.

Tests added for the usage and knowledge fixes, both proven to fail without them.
The other four live in files that cannot load in a git worktree (pre-existing
postcss resolution failure), so CI is where they first run.
…are the usage refresh

Two corrections from reviewing the previous commits.

patchCachedRows was fixed with a predicate naming the sibling subtrees to skip —
a denylist that rots the moment a fifth subtree is added under rowsRoot. The key
factory already separated row lists under an 'infinite' segment; it just had no
prefix accessor, so every caller reached for the parent and subtracted. Adding
infiniteRowsRoot lets the walk be an allowlist by construction and deletes the
predicate, the helper, and both docblocks explaining the subtraction.

The searched-rows view is consequently no longer patched by a cell edit and is
left to its own refetch — it holds a flat result, not pages. That is recorded on
the function rather than left to be rediscovered.

The delayed usage refresh was written out three times across two files, a
duplication the previous commit enlarged rather than introduced. It is now one
scheduleUsageRefresh beside the keys it invalidates, which also gives the bare
1000ms a name and one place to change it.
@vercel

vercel Bot commented Aug 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Aug 13, 2026 8:59am

Request Review

@cursor

cursor Bot commented Aug 13, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes consolidate credential authorization, workflow persistence, and provider execution—security-sensitive paths—but preserve the same admission checks and add tests; risk is mainly regression in edge-case error codes and cache invalidation timing rather than new exposure.

Overview
Server paths that used to call the app’s own API now run in-process, with shared modules so HTTP routes and internal callers stay aligned.

Router and evaluator blocks no longer POST /api/providers; they call executeBlockProviderRequest, which keeps the same user and workspace admission checks the route enforced. OAuth token resolution lives in lib/oauth/token-resolution.ts with authorizeCredentialUseForAuth, so the token route and credential tools share one authorization path instead of minting internal JWTs per tool retry. Copilot checkpoint revert no longer PUTs workflow state over HTTP with forwarded cookies; it validates checkpoint blobs and writes through saveWorkflowNormalizedState, the same path as PUT /api/workflows/[id]/state.

Workspace file list hydration is capped at WORKSPACE_FILE_SEED_MAX (300) on every workspace route: workspaces above the budget seed nothing (not a truncated list), and file-folder prefetch parses through the route contract so seeded cache shape matches client fetches.

React Query / UI consistency: table row optimistic updates walk infiniteRowsRoot instead of a prefix shared with find-results; CloudWatch selector keys include search; optimistic temp ids use generateId(); shared subscription-keys, workspace-usage-keys, and invalidateWorkspaceUsage / scheduleUsageRefresh break import cycles and refresh billing reads after runs and plan changes; knowledge list invalidation on uploads/deletes; schedule byId key segment avoids cache aliasing.

Reviewed by Cursor Bugbot for commit 140b928. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR replaces several server-to-server HTTP calls with shared in-process execution paths while preserving authorization and persistence boundaries. It also corrects query caching, workspace-file prefetching, and optimistic table-update behavior.

  • Routes OAuth token resolution through a shared authorization and token-resolution module.
  • Executes router and evaluator provider requests directly in-process.
  • Extracts normalized workflow-state persistence for both checkpoint reverts and the workflow-state API.
  • Refines workspace-file projections, prefetch budgeting, and React Query cache keys.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
apps/sim/executor/handlers/evaluator/evaluator-handler.test.ts Replaces the previously reported loosely typed provider-request helper with a concrete ProviderRequest-based return type.
apps/sim/executor/utils/provider-request.ts Centralizes in-process provider execution and its admission checks for executor handlers.
apps/sim/lib/oauth/token-resolution.ts Extracts the shared credential authorization, token refresh, provider metadata, audit, and error-mapping flow.
apps/sim/lib/workflows/persistence/save-normalized-state.ts Consolidates authorized, lock-aware normalized workflow persistence and post-write notification.
apps/sim/hooks/queries/tables.ts Narrows optimistic cache traversal to compatible table-row query shapes.
apps/sim/lib/workspace-files/queries.ts Projects only mapped file columns and supports budget-aware workspace-file loading.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Executor[Router / Evaluator handlers] --> ProviderRuntime[Provider runtime]
  ToolExecutor[Credential-using tools] --> TokenResolution[Shared token resolution]
  OAuthRoute[OAuth token route] --> TokenResolution
  RevertRoute[Checkpoint revert route] --> StatePersistence[Normalized state persistence]
  WorkflowStateRoute[Workflow state route] --> StatePersistence
  StatePersistence --> Database[(Postgres)]
  StatePersistence --> Realtime[Socket notification]
Loading

Reviews (2): Last reviewed commit: "chore(test): type the evaluator provider..." | Re-trigger Greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 140b928. Configure here.

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