When adding telemetry, previews, or fallback paths, ensure you don’t (a) repeat expensive work, (b) fetch/scan unbounded amounts of data, or (c) hard-fail when size/time limits are exceeded.
Apply these rules: 1) Reuse captured results
Example pattern (fetch once, reuse):
// Fetch once
const terminalSnapshot = await getLatestTerminalOutput();
// Emit based on the single snapshot
await returnCurrentTerminalContentsFromSnapshot(terminalSnapshot);
// Telemetry based on the same snapshot
telemetryService.captureTerminalOutputFailure(
terminalSnapshot.state === "ok" ? /*...*/ : /*...*/,
);
(If the existing API can’t take a snapshot, refactor so the snapshot is computed once and shared.)
2) Bound “wide” Git/remote operations
3) Degrade gracefully under limits
{ state: "partial" } (preserving critical fields like HEAD/branch) when limits are hit, so downstream can still function.4) Gate unnecessary work during transitional UI states
These practices reduce latency, avoid side-effect duplication, and prevent expensive tasks from causing { state: "unavailable" } or UI churn.