Empirical Research Series • Part 2 of 3
Part 2: How Agent Distraction Affects Builders — Context Hygiene & Boundary Defense
The Anatomy of Context Contamination
In traditional human software workflows, code comments serve as an informal bulletin board. Engineers leave aspirational notes, reference Jira tickets, or mark temporary workarounds:
// TODO(b/842918): Refactor this into a streaming generator before Q3
// FIXME: temporary patch for Safari cookie bug - remove when Safari 19 ships
function fetchUserSession(sessionId) {
// ...
}
When an autonomous agent receives a prompt like: "Add an optional timeout parameter to fetchUserSession," the raw file content is streamed into the model's working memory. While the human developer intended a 3-line change, the agent's attention heads latch onto TODO(b/842918). Instead of making the simple edit, the agent spends 10 tool calls querying the file system for streaming generator patterns, creates an unsolicited refactoring PR, and fails to implement the timeout parameter correctly.
The 3-Tier Defense Architecture
Google's engineering team tested several architectural interventions. Simply telling an agent to "be careful" in the user prompt is insufficient. Production robustness requires a defense-in-depth model across three distinct layers:
Instead of passing raw source files directly to the model, an ephemeral AST or regex pre-processor cleans the code in memory before passing it into the agent's context window. The physical file on disk remains completely untouched, but the model never sees the distracting TODO(b/...) or FIXME tokens.
Impact: Reduces distraction detours from 6.0% down to 0.1%, saving +77% token inflation instantly without requiring team-wide codebase cleanup.
Autonomous agents require explicit negative constraints in their system prompt or rule files (e.g. .gemini/rules/ or CLAUDE.md). The rule must specifically forbid unprompted refactoring of historical comments.
Authoritative Directive: "STRICT TASK BOUNDARY: Never investigate, remediate, or refactor inline TODO, FIXME, or BUG comments unless the user explicitly commands it in the current prompt. Treat all existing comments as immutable background noise."
Implement repository hygiene gates that prevent dead bug tickets from accumulating in the codebase. Linters cross-reference bug IDs (such as Google Issue Tracker b/... or GitHub Issue #...) against the ticketing API. If a ticket is closed or marked "Won't Fix", the pre-commit hook requires either resolving the comment or migrating it to project documentation.
The Critical Trade-Off: What to Keep vs. What to Strip
Not all comments are harmful. Stripping everything blindly causes new failures because coding agents rely heavily on structural documentation. Google's research establishes the following taxonomy:
- Keep in Context: JSDoc/TSDoc/PyDoc docstrings, type annotations, mathematical proofs, security invariants (e.g.
// CRITICAL: Must acquire mutex before reading), and API contract specifications. - Strip or Mask: Aspirational TODOs (
TODO: rewrite later), issue tracker references (TODO(b/123)), emotional commentary (// this is ugly but works), and obsolete workarounds for superseded platform versions.
Interactive Playground & Code Generator
In Part 3, we provide ready-to-use Python and TypeScript AST sanitizers and Antigravity rule configs. You can also test your code in our companion interactive lab.
Open the Agent Distraction Simulator & Context Sanitizer [SIMULATED DEMO] →