Heuristic:AIPexStudio AIPex Snapshot Caching
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Browser_Automation |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Snapshot lookups check the DOM cache first and fall back to the CDP cache, avoiding redundant accessibility tree collection on repeated element lookups within the same automation cycle.
Description
The snapshot provider maintains two caches: a DOM snapshot cache (`domSnapshotCache`, a `Map<number, UnifiedSnapshot>` keyed by tab ID) for background mode, and the CDP snapshot manager's internal cache for focus mode. When `getSnapshot` or `getNodeByUid` is called, the DOM cache is checked first. If a DOM cached snapshot exists for the tab, it is returned immediately without any CDP calls. Only when the DOM cache is empty does the system fall back to the CDP snapshot manager. This dual-cache strategy ensures that element lookups during a multi-step automation task (e.g., click → snapshot → fill → snapshot) reuse the same snapshot data, avoiding repeated CDP `Accessibility.getFullAXTree` calls or content-script message round-trips.
Usage
Use this heuristic when optimizing automation performance. After calling `createSnapshot` once, subsequent `getSnapshot` or `getNodeByUid` calls will read from cache. Call `createSnapshot` again only when you know the page DOM has changed (e.g., after navigation or dynamic content update). Call `clearSnapshot` when switching automation modes or when a tab is closed.
The Insight (Rule of Thumb)
- Action: Call `createSnapshot(tabId)` once per page state. Reuse via `getSnapshot(tabId)` or `getNodeByUid(tabId, uid)` for subsequent lookups.
- Value: Two caches: `domSnapshotCache` (Map, background mode) and `snapshotManager` internal cache (focus mode). DOM cache checked first.
- Trade-off: Cached snapshots may be stale if the page changed without calling `createSnapshot` again. The trade-off favors speed over freshness.
- Compatibility: Caches are cleared when switching modes (entering background clears CDP cache; entering focus clears DOM cache).
Reasoning
Accessibility tree collection via CDP (`Accessibility.getFullAXTree`) or content-script DOM traversal is expensive — it involves serializing the entire page's AX tree. In a typical automation task, the agent takes a snapshot, identifies a target element by UID, performs an action (click/fill), and then takes another snapshot to verify the result. By caching the snapshot, the second lookup (e.g., for error handling or result verification) can reuse the same data without re-traversing the DOM. The dual-cache design also ensures that mode switches don't leak stale data from the previous mode.
Code Evidence
Dual-cache lookup in `getSnapshot` from `packages/browser-runtime/src/automation/snapshot-provider.ts:217-235`:
export function getSnapshot(tabId: number): UnifiedSnapshot | null {
// Check DOM cache first (if background mode was used)
const domCached = domSnapshotCache.get(tabId);
if (domCached) {
return domCached;
}
// Fall back to CDP cache
const cdpSnapshot = snapshotManager.getSnapshot(tabId);
return cdpSnapshot;
}
Dual-cache lookup in `getNodeByUid` from `packages/browser-runtime/src/automation/snapshot-provider.ts:240-262`:
export function getNodeByUid(
tabId: number,
uid: string,
): TextSnapshotNode | null {
// Check DOM cache first
const domCached = domSnapshotCache.get(tabId);
if (domCached) {
const node = domCached.idToNode.get(uid) || null;
return node;
}
// Fall back to CDP
const node = snapshotManager.getNodeByUid(tabId, uid);
return node;
}
Cache clearing on mode switch from `packages/browser-runtime/src/automation/snapshot-provider.ts:190-210`:
domSnapshotCache.set(tabId, unified);
// Clear CDP cache to avoid confusion
snapshotManager.clearSnapshot(tabId);
// ...
// When entering focus mode:
domSnapshotCache.delete(tabId);
return snapshotManager.createSnapshot(tabId);