feat: use cache key to persist klavis client - #480
feat: use cache key to persist klavis client#480Dani Akash (DaniAkash) wants to merge 5 commits into
Conversation
Greptile SummaryThis PR adds TTL-based response caching (5-minute window) and concurrent-request deduplication to Two concerns remain:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant createStrata
participant strataCache
participant pendingRequests
participant KlavisAPI
Caller->>createStrata: createStrata(userId, servers)
createStrata->>createStrata: buildStrataCacheKey(userId, servers)
createStrata->>strataCache: get(cacheKey)
alt Cache hit (not expired)
strataCache-->>createStrata: CachedStrata
createStrata-->>Caller: return cached response
else Cache miss
createStrata->>pendingRequests: get(cacheKey)
alt In-flight request exists
pendingRequests-->>createStrata: existing Promise
createStrata-->>Caller: return shared Promise
else No pending request
createStrata->>KlavisAPI: POST /mcp-server/strata/create
createStrata->>pendingRequests: set(cacheKey, promise)
KlavisAPI-->>createStrata: StrataCreateResponse
createStrata->>strataCache: set(cacheKey, {response, expiresAt})
createStrata->>pendingRequests: delete(cacheKey)
createStrata-->>Caller: return response
end
end
Prompt To Fix All With AIThis is a comment left during a code review.
Path: packages/browseros-agent/apps/server/src/lib/clients/klavis/klavis-client.ts
Line: 197
Comment:
**Cache invalidation only clears the single-server entry**
`removeServer` invalidates only the exact `(userId, [serverName])` cache key. However, if `createStrata` was previously called with a multi-server combination that includes `serverName` (e.g., `createStrata(userId, ['serverA', 'serverB'])`), that cache entry retains the old `StrataCreateResponse` — including the now-stale `addedServers` list and OAuth/API-key URLs.
Depending on whether the Klavis API considers each server-combination a separate strata object or shares one physical strata per user, the multi-server cache entry may reflect an inconsistent server state after `removeServer` is called. At a minimum, any entry whose server list contains `serverName` should be invalidated:
```typescript
// Instead of a single targeted delete, clear all entries for this user
// that contain the removed server
for (const [key, _] of this.strataCache) {
const parsed: [string, ...string[]] = JSON.parse(key)
if (parsed[0] === userId && parsed.slice(1).includes(serverName)) {
this.strataCache.delete(key)
}
}
```
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: packages/browseros-agent/apps/server/src/lib/clients/klavis/klavis-client.ts
Line: 38
Comment:
**No active eviction — expired entries accumulate indefinitely**
`strataCache` is a plain `Map` with no background sweep. Expired entries (past `expiresAt`) are only displaced when `createStrata` is called again for the exact same key. In a long-running server process with many unique `(userId, servers)` combinations, stale entries will accumulate in memory until the process restarts.
Consider a lightweight eviction approach: either schedule a periodic cleanup, or evict inline on every `createStrata` call:
```typescript
private evictExpiredStrataCache(): void {
const now = Date.now()
for (const [key, entry] of this.strataCache) {
if (entry.expiresAt <= now) {
this.strataCache.delete(key)
}
}
}
```
Call `this.evictExpiredStrataCache()` at the top of `createStrata` before the cache lookup to keep memory bounded without requiring a timer.
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: "fix: code reviews" |
|
All three findings addressed:
|
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
|
The caching layer didn't add significant performance gains - so closing the ticket |
This pull request adds caching and request deduplication to the
createStratamethod in theKlavisClientclass to improve efficiency and prevent redundant network calls. Now, repeated or concurrent requests for the same user and server combination within a 5-minute window will return cached results or share the same in-flight request.Caching and deduplication improvements:
strataCachewith a 5-minute TTL to storeStrataCreateResponseobjects, preventing unnecessary API calls for the same(userId, servers)combination. [1] [2]pendingRequestsmap to deduplicate concurrent requests for the same key, ensuring only one network request is made and shared among callers. [1] [2]buildStrataCacheKey) that uniquely identifies requests byuserIdand a sorted list of servers.createStratamethod to use the new caching and deduplication logic, and to clean up pending requests after completion or error.