Heuristic:A9T9 RPA Generation Counter Orphan Turns
| Knowledge Sources | |
|---|---|
| Domains | AI_Agent, Concurrency |
| Last Updated | 2026-10-22 17:48 GMT |
Overview
A generation counter orphans abandoned AI conversation turns — a new chat or newer run bumps the generation, and every await in the old turn bails out if it no longer matches.
Description
The MacroAgentService maintains a generation counter that is bumped by every run() call and by createNewChat(). Each run captures its own generation value at entry and checks it after every await point. If the generation no longer matches (because the user started a new chat or a newer run superseded this one), the old turn returns silently — it cannot log into the fresh conversation, push into the cleared history, or clear the tab marker. This prevents an abandoned turn's late API response from corrupting the new conversation or surfacing stale errors.
Usage
Use this heuristic whenever the AI macro agent runs conversations that can be interrupted — the user can click "new chat" or send a new prompt while a previous turn is still awaiting an API response. The generation counter is the core cancellation mechanism for the sampling loop.
The Insight (Rule of Thumb)
- Action: Bump
this.generationon everyrun()andcreateNewChat(). Captureconst gen = ++this.generationat entry. Checkif (gen !== this.generation) returnafter everyawait. - Value: N/A — integer counter, no special value.
- Trade-off: Abandoned turns may leave the tab marker uncleared (it lost the generation) —
createNewChat()explicitly clears it as compensation. - Compatibility: Works with both Anthropic and OpenAI-compatible provider loops. Error handlers also check the generation before surfacing errors (an abandoned turn's failure is nobody's news).
Reasoning
Without the generation counter, an abandoned turn's late API response could push messages into the freshly-cleared history, log stale results into the new chat, or clear the tab marker that a newer run set. The counter is checked at these critical points:
- After every API call in the sampling loop (before processing the response)
- After the XModule/OCR availability probes (before entering the sampling loop)
- In the
finallyblock before clearing the tab marker (only the current generation may clear it) - In the error handler before throwing (an abandoned turn's error is silently dropped)
The createNewChat() method explicitly clears the tab marker because the abandoned turn skips its own unmark (it lost the generation race).