Skip to content

Commit b6d207f

Browse files
committed
chore(perf): trim verbose comments to terse why-notes
1 parent 980aaf9 commit b6d207f

8 files changed

Lines changed: 29 additions & 70 deletions

File tree

apps/sim/lib/api-key/byok.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ function nextRotationIndex(poolKey: string, poolSize: number): number {
3737
* creation order. A key that fails to decrypt is skipped in favor of the next
3838
* one in the pool.
3939
*
40-
* The key list is read fresh from the database on every call rather than
41-
* cached: BYOK lookups are not a hot database query, and reading fresh keeps
42-
* key revocation/rotation effective immediately across every ECS task with no
43-
* cross-instance cache-coherence concern.
40+
* The key list is read fresh every call (not cached): BYOK is not a hot query,
41+
* and reading fresh keeps revocation immediate across ECS tasks.
4442
*/
4543
export async function getBYOKKey(
4644
workspaceId: string | undefined | null,

apps/sim/lib/billing/calculations/usage-monitor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,9 @@ export async function checkOrgMemberUsageLimit(
467467
return { isExceeded: false, currentUsage: 0, limit: null }
468468
}
469469

470-
// Resolve the member cap first and short-circuit when none is set — the
471-
// common case. Computing usage is only worthwhile once a cap exists, so the
472-
// two queries stay sequential rather than racing (parallelizing would add a
473-
// usage query on every uncapped member's execution).
470+
// Resolve the cap first and short-circuit when unset (the common case); only
471+
// then is computing usage worthwhile. Kept sequential, not raced, to avoid a
472+
// usage query on every uncapped member's execution.
474473
const limit = await getOrgMemberUsageLimit(organizationId, userId)
475474
if (limit === null) {
476475
return { isExceeded: false, currentUsage: 0, limit: null }

apps/sim/lib/billing/core/plan.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ describe('getHighestPrioritySubscription', () => {
127127

128128
const result = await getHighestPrioritySubscription('user-1')
129129

130-
// Enterprise (org) always wins over Pro (personal).
131130
expect(result?.id).toBe('sub-org-enterprise')
132131
})
133132

@@ -147,7 +146,7 @@ describe('getHighestPrioritySubscription', () => {
147146

148147
it('returns the personal sub and skips org follow-ups when there are no memberships', async () => {
149148
queue('subscription', [personalPro('user-1')])
150-
queue('member', []) // no org memberships
149+
queue('member', [])
151150

152151
const result = await getHighestPrioritySubscription('user-1')
153152

@@ -159,18 +158,18 @@ describe('getHighestPrioritySubscription', () => {
159158
})
160159

161160
it('returns null when neither personal nor org subscriptions exist', async () => {
162-
queue('subscription', []) // no personal subs
163-
queue('member', []) // no memberships
161+
queue('subscription', [])
162+
queue('member', [])
164163

165164
const result = await getHighestPrioritySubscription('user-1')
166165

167166
expect(result).toBeNull()
168167
})
169168

170169
it('excludes orphaned org memberships whose organization row no longer exists', async () => {
171-
queue('subscription', []) // no personal subs
170+
queue('subscription', [])
172171
queue('member', [{ organizationId: 'ghost-org' }]) // membership points at a deleted org
173-
queue('organization', []) // org-existence returns nothing -> orphaned
172+
queue('organization', [])
174173

175174
const result = await getHighestPrioritySubscription('user-1')
176175

@@ -184,7 +183,7 @@ describe('getHighestPrioritySubscription', () => {
184183
it('falls back to the personal sub when the only org is orphaned', async () => {
185184
queue('subscription', [personalPro('user-1')])
186185
queue('member', [{ organizationId: 'ghost-org' }])
187-
queue('organization', []) // orphaned org
186+
queue('organization', [])
188187

189188
const result = await getHighestPrioritySubscription('user-1')
190189

apps/sim/lib/execution/preprocessing.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,9 @@ export async function preprocessExecution(
323323
}
324324

325325
// ========== STEPS 3.5–6: Preflight Gates ==========
326-
// The read-only gates run concurrently to cut latency: ban + subscription
327-
// together, then usage (which needs the subscription). The rate-limit gate is
328-
// stateful — it debits a token — so it runs sequentially only after ban and
329-
// usage pass. Failures apply in fixed precedence (ban 403 → usage 402 → rate
330-
// 429), and the sole write (the STEP 7 reservation) stays last.
326+
// Read-only gates run concurrently (ban + subscription, then usage). The
327+
// rate-limit gate debits a token, so it runs sequentially only after ban and
328+
// usage pass. Failures apply in fixed precedence: ban 403 → usage 402 → rate 429.
331329

332330
/**
333331
* A failing gate's deferred outcome: the response to return, plus an optional

apps/sim/lib/workflows/persistence/utils.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,39 +100,21 @@ export async function blockExistsInDeployment(
100100
}
101101
}
102102

103-
/**
104-
* Each entry is keyed by an immutable `deploymentVersionId` and holds a
105-
* fully-migrated {@link DeployedWorkflowData} snapshot (tens of KB to ~1MB);
106-
* the bound keeps worst-case memory within a sane envelope.
107-
*/
108103
const DEPLOYED_STATE_CACHE_MAX_ENTRIES = 500
109104
const DEPLOYED_STATE_CACHE_TTL_MS = 5 * 60 * 1000
110105

111106
/**
112-
* Process-local cache of fully-loaded, post-migration deployed workflow state,
113-
* keyed by the immutable `deploymentVersionId`.
114-
*
115-
* The id is unique per deploy — a redeploy mints a new id and a rollback
116-
* reactivates an existing id — so the active-version lookup naturally selects a
117-
* different (or already-cached) key whenever the active deployment changes,
118-
* making the cache self-invalidating across redeploy/rollback.
119-
*
120-
* The TTL is absolute (not reset on read) on purpose: it bounds the one piece
121-
* of the cached state that is not strictly immutable — `applyBlockMigrations`
122-
* resolves legacy credential references via a live lookup — so a credential
123-
* change propagates across ECS tasks even for a continuously-running workflow.
107+
* Caches post-migration deployed state by the immutable `deploymentVersionId`, so
108+
* a redeploy/rollback (which changes the active id) self-invalidates. The TTL is
109+
* absolute on purpose — it bounds the one non-immutable part, the live credential
110+
* remap in `applyBlockMigrations` — so credential changes still propagate.
124111
*/
125112
const deployedStateCache = new LRUCache<string, DeployedWorkflowData>({
126113
max: DEPLOYED_STATE_CACHE_MAX_ENTRIES,
127114
ttl: DEPLOYED_STATE_CACHE_TTL_MS,
128115
})
129116

130-
/**
131-
* Drop cached deployed state. Pass a `deploymentVersionId` to evict a single
132-
* entry, or omit it to clear the entire cache. Explicit invalidation is not
133-
* required for correctness — redeploy/rollback change the active id and key the
134-
* cache anew — but the helper is exported for completeness and testing.
135-
*/
117+
/** Evicts one deployed-state entry, or clears the cache when no id is given. */
136118
export function invalidateDeployedStateCache(deploymentVersionId?: string): void {
137119
if (deploymentVersionId) {
138120
deployedStateCache.delete(deploymentVersionId)

apps/sim/providers/bedrock/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ export const bedrockProvider: ProviderConfig = {
139139
}
140140
}
141141

142-
// Memoized: each BedrockRuntimeClient owns its own connection pool (AWS SDK
143-
// best practice is to reuse the client), so reusing it keeps connections warm
144-
// across requests. Keyed by region + credential identity (a rotated key pair
145-
// changes the access key id and so yields a fresh client).
142+
// AWS SDK clients own a per-client connection pool and are meant to be reused.
143+
// Keyed by region + credential identity (a rotated key pair yields a new key).
146144
const client = getCachedProviderClient(
147145
`bedrock::${region}::${request.bedrockAccessKeyId ?? 'default-chain'}`,
148146
() => new BedrockRuntimeClient(clientConfig)

apps/sim/providers/client-cache.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
import { LRUCache } from 'lru-cache'
22

3-
/**
4-
* Shared, bounded, idle-expiring cache of provider SDK clients. Reusing a client
5-
* across requests lets the underlying HTTP agent keep connections alive, avoiding
6-
* a fresh TLS handshake (and client construction) on every request.
7-
*
8-
* Provider SDK clients (Anthropic, OpenAI, Groq, …) hold no per-request mutable
9-
* state — abort signals and timeouts are passed at the call site, not on the
10-
* client — so a single instance is safe to share across concurrent requests.
11-
*
12-
* Keys must be namespaced per provider and must encode every input that varies
13-
* the constructed client. The API key is always part of the key, making it the
14-
* tenant security boundary: clients are never shared across different keys.
15-
*/
16-
173
const CLIENT_CACHE_MAX_ENTRIES = 1_000
184
const CLIENT_CACHE_TTL_MS = 30 * 60 * 1_000
195

6+
// updateAgeOnGet makes the TTL idle-based, so a continuously-used client keeps
7+
// its warm keep-alive connections while idle keys age out.
208
const clientCache = new LRUCache<string, object>({
219
max: CLIENT_CACHE_MAX_ENTRIES,
2210
ttl: CLIENT_CACHE_TTL_MS,
23-
// Idle expiry: the TTL resets on every hit so a continuously-used client
24-
// (and its warm keep-alive connections) survives, while idle keys age out.
2511
updateAgeOnGet: true,
2612
})
2713

2814
/**
29-
* Returns a cached provider client for the given key, constructing and storing
30-
* one via `factory` on a miss. The key must encode every input that varies the
31-
* constructed client (provider namespace + API key at minimum); identical keys
32-
* safely share a single client instance.
15+
* Memoizes provider SDK clients so connections stay warm across requests rather
16+
* than re-handshaking per call. The key must be namespaced per provider and
17+
* encode every input that varies the client; the API key is always part of it,
18+
* making it the tenant boundary (clients are never shared across keys).
3319
*/
3420
export function getCachedProviderClient<T extends object>(key: string, factory: () => T): T {
3521
const existing = clientCache.get(key)

apps/sim/providers/vllm/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ export const vllmProvider: ProviderConfig = {
135135
}
136136

137137
const apiKey = request.apiKey || env.VLLM_API_KEY || 'empty'
138-
// Memoized: a pinned endpoint gets its own undici Agent per call, so reusing
139-
// the client (keyed by the resolved IP) keeps its connections warm. DNS
140-
// re-validation still runs every request; a new IP yields a new key/client.
138+
// A pinned endpoint gets its own undici Agent, so reuse keeps connections
139+
// warm. DNS re-validation still runs every request; a new IP rekeys.
141140
const vllm = getCachedProviderClient(
142141
`vllm::${apiKey}::${baseUrl}::${pinnedIP ?? 'no-pin'}`,
143142
() =>

0 commit comments

Comments
 (0)