Reshabh K Sharma
5 min
Abstract
As Large Language Model (LLM) agents increasingly execute complex, autonomous software engineering tasks, developers rely on natural language Agent Instructions (e.g., AGENTS.md) to enforce project-specific coding conventions, tooling, and architectural boundaries. However, these instructions are passive text. Agents frequently deviate from them due to context limitations or conflicting legacy code, a phenomenon we term Context Drift. Because agents operate without real-time human supervision, these silent violations rapidly compound into technical debt. To bridge this gap, we introduce ContextCov, a framework that transforms passive Agent Instructions into active, executable guardrails. ContextCov extracts natural language constraints and synthesizes enforcement checks across three domains: static AST analysis for code patterns, runtime shell shims that intercept prohibited commands, and architectural validators for structural and semantic constraints. Evaluations on 723 open-source repositories demonstrate that ContextCov successfully extracts over 46,000 executable checks with 99.997% syntax validity, providing a necessary automated compliance layer for safe, agent-driven development. Source code and evaluation results are available at https://anonymous.4open.science/r/ContextCov-4510/.
Sam: Refined rules get sorted by category: commands, code style, or structure. A classifier picks the fit and sends it to a specialist tool-maker. That outputs ready-to-run Python code—like a custom inspector. This domain-routed synthesis ensures high-quality checks.
Alex: No one-size-fits-all generator. Now, at runtime, how do these checks catch mistakes?
Sam: Three enforcers match rule types. For commands, it adds wrapper scripts to the PATH—like gatekeepers that check shell orders before they run, blocking forbidden ones. For code style, like extra parentheses on functions, it uses Tree-sitter: a parser that breaks files into trees for scanning patterns across languages, with line numbers and fixes.
Alex: Process gets intercepted upfront, source code gets tree-scanned. And architecture?
Sam: For structure, it builds graphs of files and dependencies—like "utilities only in the base layer." That catches wrong folders using graph checks. Trickier semantic rules, like not grabbing storage keys directly, use AI to flag for review. Across 723 repositories, it extracted over 46,000 checks with near-perfect validity, detecting violations in most codebases examined.
Alex: That's the full loop—from document tree to live guards. A meaningful way to close the reality gap.
Sam: Checks save to a JSON file with details: original rule, refined version, category, and Python code. Developers review, edit, or disable before use. This keeps runtime fast, no AI calls needed each time.
Alex: Like an editable checklist, not a black box. For fuzzier rules?
Sam: Graphs map file connections: nodes for modules, arrows for imports. Semantic ones prompt AI on snippets for warnings, not blocks. It catches patterns agents copy from messy code. Violations appeared in 81% of codebases, over 500,000 total.
Alex: Graphs for links, AI for intent. Solid utility.
Sam: When a check blocks, the error feeds back to the agent, letting it suggest instruction tweaks. This evolves rules without constant human input. It assumes good agents and prevents drift.
Alex: Agents help refine their rulebook. Smart feedback.
Sam: This turns passive text into checks that catch slips early. Evaluations show it works reliably on real code from hundreds of repos, preventing common violations. A step toward AI that respects project design.
Alex: Well put. Thanks for breaking it down, Sam. That's it for this look at ContextCov. Thanks for listening to ResearchPod.