feat:[ENG-2073] add short-lived result cache in SwarmCoordinator#390
feat:[ENG-2073] add short-lived result cache in SwarmCoordinator#390danhdoan merged 2 commits intoproj/swarm-memoryfrom
Conversation
- Add resultCacheTtlMs (default 10s) to performance config schema - Cache check at start of execute() returns instantly on hit - LRU semantics via delete+re-set on cache hit (Map insertion order) - Cache store after successful execution with max 20 entries - Invalidation on store() (content changed) and refreshHealth() (provider availability changed) - TTL 0 disables caching entirely - 11 test cases covering hits, misses, TTL, invalidation, LRU eviction
|
Claude finished @cuongdo-byterover's task in 2m 29s —— View job PR Review: feat:[ENG-2073] add short-lived result cache in SwarmCoordinator
SummaryOverall Score: 4 / 5 The implementation is clean, well-tested, and transparent to callers. The LRU semantics are correctly implemented via Map insertion-order, mutation safety is addressed with shallow cloning, and the config schema change is consistent with existing patterns. The cache invalidation strategy (on Two correctness bugs in Issuessuggestion: Cache key missing
|
- Add request.type and request.timeRange to cache key to prevent cross-type and cross-timerange cache collisions - Deep-clone QueryResult items (including metadata) on both cache store and cache hit to prevent nested object mutation - Skip cache store when resultCacheTtlMs=0 to avoid dead writes and unnecessary LRU eviction runs - Mark healthCache and resultCache as readonly (mutated in-place via .set/.delete/.clear, never reassigned)
|
Claude finished @cuongdo-byterover's task in 3m 0s —— View job PR Review: feat:[ENG-2073] add short-lived result cache in SwarmCoordinator
SummaryOverall Score: 5 / 5 All five issues from the previous review are resolved. The implementation is correct, well-tested, and transparent to callers. Two new minor observations below — neither is a blocker. Previous issues — all fixed:
Issuessuggestion: Missing tests for
|
Problem:** The agent calls
swarm_querymultiple times during a single conversation, and each call re-queries all providers from scratch (340ms+). GBrain subprocess alone takes 200-300ms. Identical queries seconds apart produce identical results — wasted work.SwarmCoordinator. Identical queries within a 10-second TTL window return instantly from cache. Cache is invalidated onstore()(content changed) andrefreshHealth()(provider availability changed). Configurable viaperformance.result_cache_ttl_ms(default 10s, 0 disables).SwarmQueryResulttype, same JSON output structure.Type of change
Scope (select all touched areas)
Linked issues
Root cause (bug fixes only, otherwise write
N/A)N/A
Test plan
test/unit/agent/swarm/swarm-coordinator.test.ts(+12 tests)test/unit/agent/swarm/provider-factory.test.ts(type alignment)store()clears cache on successful writerefreshHealth()clears cacheUser-visible changes
performance.result_cache_ttl_ms(default: 10000, 0 to disable)swarm_queryreturns faster on repeated identical queries within 10sEvidence
In-process manual test:
Checklist
npm test) — 5892 passingnpm run lint) — pre-commit hook verifiednpm run typecheck)npm run build)mainRisks and mitigations
Risk: Cached result returned by reference could be mutated by downstream consumers, corrupting the cache.
{...result, results: [...result.results]}). Verified bymutation-safetest.Risk: Cache key collision if query text contains the delimiter character.
JSON.stringify([query, scope, maxResults])— no delimiter ambiguity.Risk: Stale TTL-expired entries could accumulate in memory.
Risk:
totalQueriescounter could undercount when cache hits bypass the main execute path.totalQueries++is called on both cache hit and cache miss paths.