Heuristic:777genius Agent teams ai Cache Invalidation Generations
| Knowledge Sources | |
|---|---|
| Domains | Caching, Concurrency |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Use monotonically increasing generation counters to reject stale in-flight cache results when the underlying data has changed during an async read.
Description
When async cache reads are in-flight and the underlying data is invalidated (e.g., config file changes), the in-flight result must not be cached because it reflects stale data. A generation counter is bumped on every invalidation. The in-flight request captures the generation at start; if the generation has changed by the time the read completes, the result is discarded rather than cached. This prevents stale data from poisoning the cache and causing inconsistent state.
Usage
Use this heuristic whenever an async read with caching can become stale due to concurrent writes. This applies to team config reading, team list enumeration, and notification context generation. The generation pattern ensures that even under concurrent read/write pressure, the cache always reflects the most recent valid state.
The Insight (Rule of Thumb)
- Action: Bump a generation counter on every write/invalidation. Capture the generation at the start of each async read. After the read completes, only cache the result if the generation is unchanged.
- Value: `generation` is a monotonically increasing integer, bumped via `bumpConfigGeneration()`.
- Trade-off: Extra reads may occur if invalidations happen frequently during a read. This is acceptable — correctness over efficiency.
- Compatibility: Works for any async read pattern where the data source can change concurrently.
Reasoning
Without generation checks, a slow async read that started before a write could complete after the write and cache stale data. The next cache hit would return the stale result, causing the app to display or use outdated state. The generation counter creates a cheap "version check" that detects this race: if the counter changed during the read, the result is from an old version and must be discarded.
Code Evidence
Generation check on cache write from `TeamConfigReader.ts:318-329`:
// generation checks on cache writes prevent stale in-flight results from being
// stored. The generation check below prevents the stale in-flight result from being
// cached.
Generation bump on write from `TeamConfigReader.ts:353`:
const generation = TeamConfigReader.bumpConfigGeneration(configPath);
In-flight dedup with generation from `TeamConfigReader.ts:388-407`:
if (existingRequest?.generationAtStart === TeamConfigReader.listTeamsGeneration) {
return existingRequest.promise;
}
const generationAtStart = TeamConfigReader.listTeamsGeneration;
// ...
if (TeamConfigReader.listTeamsGeneration === generationAtStart) {
// cache the result — generation unchanged
}
Notification context generation from `TeamDataService.ts:3326-3340`:
const generation = this.getNotificationContextGeneration(teamName);
// ...
if (existing?.generation === generation) {
// cache hit
}
const promise = this.readTeamNotificationContext(teamName, generation, now)