Empirical Research Series • Part 3 of 3
Part 3: Concrete Playbooks, Code Sanitizers & Agile Labs Studio
Blueprint 1: Line-Preserving Ephemeral Sanitizer (TypeScript / Node.js)
When building an agent tool that reads files (such as read_file), sanitize the payload in memory before returning it to the LLM. Replacing comment text with whitespace or blank comments keeps source map and line number parity:
/**
* Sanitizes code context before streaming into agent context.
* Preserves newlines and line indices so patch edits match exact line numbers.
*/
export function sanitizeAgentContext(sourceCode: string): string {
// Regex matches TODO(b/...), FIXME, BUG(#...), and HACK: comments
const DISTRACTION_REGEX = /(\/\/|\/\*|\#)\s*(TODO|FIXME|BUG|HACK)(?:\([^\)]+\))?:?.*?(?=\r?\n|\*\/|$)/gi;
return sourceCode.replace(DISTRACTION_REGEX, (match, prefix) => {
// Replace the distracting directive with a neutral placeholder preserving length
return `${prefix} [historical-ref-sanitized]`;
});
}
Blueprint 2: Line-Preserving Python AST Token Sanitizer
For Python codebases, the standard tokenize library provides exact comment classification without corrupting strings or docstrings:
import io
import re
import tokenize
DISTRACTION_PAT = re.compile(r'(TODO|FIXME|BUG|HACK)', re.IGNORECASE)
def sanitize_python_for_agent(source_code: str) -> str:
"""
Masks TODO/FIXME comments while keeping physical file untouched.
"""
output_lines = source_code.splitlines(keepends=True)
reader = io.StringIO(source_code).readline
tokens = tokenize.generate_tokens(reader)
modifications = []
for tok_type, tok_str, (srow, scol), (erow, ecol), _ in tokens:
if tok_type == tokenize.COMMENT and DISTRACTION_PAT.search(tok_str):
modifications.append((srow - 1, scol, ecol))
for row, scol, ecol in reversed(modifications):
line = output_lines[row]
sanitized = line[:scol] + "# [ref-sanitized]" + line[ecol:]
output_lines[row] = sanitized
return "".join(output_lines)
Blueprint 3: Antigravity Rule Template (Zero-Code System Boundary)
Add this rule to your .gemini/rules/anti-distraction.md or repo system prompt to establish a strict cognitive boundary for coding agents:
# Rule: Strict Scope & Anti-Distraction Boundary
1. **Primary Objective Only:** Execute ONLY the explicit instruction requested by the user.
2. **Comment Immutability:** Never investigate, fix, or refactor inline `TODO`, `FIXME`, `BUG`, or ticket references found in existing codebase files unless the user explicitly demands it in the current prompt.
3. **No Unsolicited Scope Creep:** If you encounter obsolete or broken code adjacent to your edit that is outside the task scope, leave it intact. Document it in your final message as an optional note, but do NOT make unprompted file edits.
Blueprint 4: CI/CD Dead-TODO Audit Linter
Prevent dead bug tickets from sitting in your codebase for years. This GitHub Actions step checks whether referenced bug tickets are obsolete:
name: Agent Hygiene Check
on: [pull_request]
jobs:
audit-todos:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Scan for unlinked or obsolete TODO references
run: |
echo "Scanning for bare TODO comments without active tracker IDs..."
! grep -rn "TODO:" src/ || (echo "Error: Found untracked TODO without ticket ID" && exit 1)
Interactive Companion: Agile Labs Distraction Sanitizer
Experience the difference in real time! Our companion Agile Labs application lets you paste your own code, calculate distraction risk scores based on Google's empirical formula, test interactive AST sanitizers, and export ready-to-use rule files.
Open the Agent Distraction Simulator & Context Sanitizer [SIMULATED DEMO] →