Heuristic:777genius Agent teams ai In Flight Deduplication
| Knowledge Sources | |
|---|---|
| Domains | Performance, Concurrency |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Share a single in-flight Promise for concurrent identical async requests so only one underlying operation executes, with all callers receiving the same result.
Description
When multiple callers request the same async operation concurrently (e.g., multiple UI components polling CLI status), the app stores the in-flight Promise in a Map keyed by request identity. Subsequent callers join the existing Promise instead of starting a new operation. Once the Promise resolves, it is removed from the Map. This prevents redundant work (e.g., spawning multiple CLI status probes) while ensuring all callers get a result.
Usage
Use this heuristic when concurrent callers may request the same expensive async operation simultaneously. This applies to CLI installer status checks, provider status probes, team notification context reads, and team data worker requests. The pattern is especially valuable for rapid UI polls that would otherwise trigger redundant I/O.
The Insight (Rule of Thumb)
- Action: Store the Promise (not the result) in a `Map<key, Promise<T>>`. On a cache hit, return the existing Promise. On completion, delete the entry.
- Value: Key should uniquely identify the request (e.g., `providerId + mode`, `teamName + scope`).
- Trade-off: All callers share the same result — if the operation fails, all callers fail. This is usually desirable (no retry storm).
- Compatibility: Works with any Promise-based async. Combine with generation counters for stale-result rejection.
Reasoning
Without in-flight deduplication, N concurrent callers spawn N identical operations (N process spawns, N file reads, N network requests). This wastes resources and can cause thundering-herd problems. By sharing a single Promise, only one operation runs, and all callers resolve together. The Map is cleaned up on completion to avoid memory leaks.
Code Evidence
CLI installer in-flight dedup from `cliInstaller.ts:51,445-447`:
const providerStatusInFlight = new Map<string, Promise<CliProviderStatus | null>>();
// ...
const inFlight = providerStatusInFlight.get(requestKey);
if (inFlight) {
const status = await inFlight;
return status;
}
Team data worker dedup from `team-data-worker.ts:37`:
// In-flight dedup: concurrent calls for the same task piggyback on one request
Team config in-flight dedup with generation from `TeamConfigReader.ts:388-402`:
if (existingRequest?.generationAtStart === TeamConfigReader.listTeamsGeneration) {
return existingRequest.promise;
}
const generationAtStart = TeamConfigReader.listTeamsGeneration;
const request = { promise: readAllTeamConfigs(generationAtStart), generationAtStart };
this.listTeamsInFlight = request;