Skip to content

refactor(ai): reduce ai/common_tools.py to tavily_search and delete agents/guardrails.py #1854

Closed

Description

@dan-petty

From the open-library audit of 2026-10-22 (ranked R04, about 1900 lines of source and tests; risk L (after R01 and R02)). ai-core-6. Spot-checked: the only live importer outside dead code is roadmap/refine.py (tavily_search); other importers are harness/agents.py and the dead WebSearch/WebFetch capabilities, both removed by R01. Depends on R01.

What the code is (ai-core-6): common_tools.py holds a hand-written DuckDuckGo HTML scraper (regex over html.duckduckgo.com), a custom web_fetch with its own MarkdownConverter subclass, about 180 lines of try/except fallback shims for Exa, XSearch and ImageGeneration, mirrored TypedDicts and tavily_search_tool. The only live consumer in src is roadmap/refine.py calling tavily_search (a 30-line POST). web_fetch_tool/duckduckgo_search_tool are called only by harness Coder/Researcher and the dead WebSearch/WebFetch capabilities; PromptInjectionDefender (guardrails.py) is called only from render_untrusted_page.

Files: src/devops_cli/ai/common_tools.py (690), src/devops_cli/ai/agents/guardrails.py (246)

Existing tool considered: pydantic_ai.common_tools (web_fetch with SSRF-safe safe_download, duckduckgo, tavily, exa) when a feature needs them. Keep tavily_search as is. Behaviour gap: Project web_fetch wraps output in <untrusted_web_page>, runs sanitize_prompt_boundary_tags and a prompt-injection scan, and goes through the egress broker; the native tool does not do the wrapping or the scan. Irrelevant now because nothing calls it; re-add those two calls around the native tool if it is wired.

Evidence: grep of web_fetch_tool|duckduckgo_search_tool|render_untrusted_page|tavily_search outside common_tools.py: only roadmap/refine.py:51,438 (tavily_search) plus dead callers.

Tests affected: tests/test_common_tools.py (628); tests/test_native_common_tools.py (135); tests/test_ai_agent_guardrails.py (236); tests/test_http_egress.py; tests/test_runtime_security_and_ssrf_hardening.py

Risk (L): Order-dependent: do after candidates 1 and 3 remove the dead callers.

Land after #1836 and the facade deletion (the issue titled 'delete the dead pydantic_ai re-export facades ...'); the only live importer outside dead code is roadmap/refine.py (tavily_search).

Acceptance criteria:

  • The listed modules, symbols and their tests are deleted; none is skipped or xfailed; a grep of src, tests, AGENTS.md and docs (outside docs/agent history and CHANGELOG) for the deleted paths and names returns nothing.
  • Every shared list is updated (CONST_ lists, invariants, ceilings fixture, conftest, generated docs) and devops docs check is clean.
  • devops ci passes with coverage at or above 90%.

Proposed design

Problem statement

The ai/common_tools.py module contains redundant and overly complex code for web fetching and search tools, including a hand-written DuckDuckGo scraper, custom Markdown conversion logic, and fallback implementations for various AI tools. The only live consumer is tavily_search, while other tools are unused or deprecated. Additionally, the guardrails.py module provides prompt injection defense that is no longer used.

Acceptance criteria

  • Criterion 1: The ai/common_tools.py file is deleted without any remaining references in source code, tests, documentation, or configuration files.
    Verification: A grep of src, tests, AGENTS.md and docs for 'common_tools' returns nothing.
  • Criterion 2: The ai/agents/guardrails.py file is deleted without any remaining references in source code, tests, documentation, or configuration files.
    Verification: A grep of src, tests, AGENTS.md and docs for 'guardrails' returns nothing.
  • Criterion 3: The tavily_search function remains functional and is not affected by the refactor.
    Verification: The roadmap/refine.py module can still call tavily_search and get expected results.
  • Criterion 4: All related tests are updated to reflect the deletion of the modules.
    Verification: devops ci passes with coverage at or above 90%.
  • Criterion 5: Shared lists and configuration files are updated to remove references to deleted code.
    Verification: devops docs check is clean after the refactor.

Key questions

  • What is the only live consumer of ai/common_tools.py outside of dead code?: The only live consumer in src is roadmap/refine.py calling tavily_search. [sources: src/devops_cli/ai/common_tools.py:480]
  • What are the specific tools and functions that will be deleted from ai/common_tools.py?: The module contains a hand-written DuckDuckGo HTML scraper, a custom web_fetch with its own MarkdownConverter subclass, about 180 lines of try/except fallback shims for Exa, XSearch and ImageGeneration, mirrored TypedDicts and tavily_search_tool. The only live consumer is roadmap/refine.py calling tavily_search. [sources: src/devops_cli/ai/common_tools.py:13]
  • What is the behavior gap mentioned in the issue for web_fetch?: Project web_fetch wraps output in , runs sanitize_prompt_boundary_tags and a prompt-injection scan, and goes through the egress broker; the native tool does not do the wrapping or the scan. [sources: src/devops_cli/ai/common_tools.py:249]
  • Should the behavior gap be addressed by re-adding the wrapping and scanning calls around the native tool?: Yes, if it is wired back in, those two calls should be added around the native tool. (proposed) [sources: src/devops_cli/ai/common_tools.py:249]
  • What tests are affected by this deletion?: Tests affected include tests/test_common_tools.py (628), tests/test_native_common_tools.py (135), tests/test_ai_agent_guardrails.py (236), tests/test_http_egress.py, and tests/test_runtime_security_and_ssrf_hardening.py [sources: src/devops_cli/ai/common_tools.py:480]
  • What is the dependency order for this change?: The change must be done after candidates 1 and 3 remove the dead callers. [sources: src/devops_cli/ai/common_tools.py:480]
  • What is the final condition for this issue to be considered complete?: The listed modules, symbols and their tests are deleted; none is skipped or xfailed; a grep of src, tests, AGENTS.md and docs (outside docs/agent history and CHANGELOG) for the deleted paths and names returns nothing. [sources: src/devops_cli/ai/common_tools.py:480]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/refactor
    on Oct 22, 2026
  2. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Type: type/refactor. The candidate proposes a refactor to reduce code and delete unused files.
    • Priority: P2-Medium. It is part of a larger audit and depends on other items, suggesting it's important but not critical.
    • Value: Medium. The change improves code quality by removing dead code and simplifying the architecture.
    • Effort: High. It involves deleting significant code and updating multiple tests and documentation.
  3. added this to the v0.2.39 milestone on Oct 22, 2026
  4. added
    status/in-progress
    on Oct 22, 2026
  5. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at ae8c2d9 (see proposed design in issue body).

  6. added a commit that references this issue on Oct 22, 2026
    5bb1097
  7. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1893, merged into release/v0.2.39 as 5bb1097.

    Cause: after #1836 deleted the harness agents, nothing in src called anything in ai/common_tools.py except tavily_search (roadmap/refine.py) or anything in ai/agents/guardrails.py (whose only caller was the page renderer inside common_tools). The module still carried a web fetch tool with its own untrusted-page renderer, a DuckDuckGo HTML scraper, Exa, X search and image generation wrappers with about 180 lines of try/except fallbacks, mirrored TypedDicts and a Tavily tool wrapper, plus the guardrail capability and PromptInjectionDefender. An AST import graph from commands, server and main confirms it: common_tools is reachable only through roadmap/refine.py:51, guardrails only through common_tools:444, and the graph reaches 544 of 553 modules at the release tip and 543 of 552 after the change, and the same 9 modules stay unreached.

    Change: ai/common_tools.py keeps tavily_search unchanged and goes from 731 lines to 44, and ai/agents/guardrails.py is deleted. What only that code used goes with it: fifteen DEFAULT_* constants (web fetch, DuckDuckGo, Exa, guardrail names and messages, prompt-injection patterns, capability ids) and CONST_WEB_FETCH_CHROME_TAGS with their config exports, the untrusted_web_page entry in the sanitizer's boundary tags, the markdownify, beautifulsoup4 and soupsieve dependencies in pyproject.toml and uv.lock (deptry stays clean), the ai/common_tools.py key in the swallowed-failures ceilings, the src type: ignore ceiling, which falls from 64 to 51 (the…

    19 file(s) changed.

    How it was verified

    Check runs at 18f61e3:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • Rule Pack Tests: pass
    • Changed-Test Independence: pass
    • Tests & Coverage: pass
    • Analyze (python): pass
    • Fail-to-Pass Check: pass
    • Analyze (actions): pass
    • Static Analysis: pass
    • Fail-to-Pass Self-Check: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1854-common-tools-tavily-only.md:

    • ai/common_tools.py defines tavily_search and nothing else, and agents/guardrails.py is gone. A search of src, tests, AGENTS.md, README.md, ARCHITECTURE.md and docs (outside docs/agent and CHANGELOG.md) for the deleted module paths and names finds nothing. No test is skipped or xfailed.
    • tests/test_common_tools.py covers every line of tavily_search (src/devops_cli/ai/common_tools.py 20 of 20 statements). tests/test_roadmap_refine.py and tests/test_http_egress.py still pass with tavily_search as their only search caller.
    • The import graph from commands/, server/ and main reaches no deleted module and leaves no module newly unreached. uv run deptry src finds no unused or missing dependency.
    • No constant, export, boundary tag, dependency, ceiling key or documentation line that only the deleted code used remains. uv run devops docs check reports every file up to date.
    • ruff check and ruff format --check pass over src and tests, and mypy passes over src. These tests pass: tests/test_architectural_invariants.py, tests/test_swallowed_failures_ratchet.py, tests/test_agent_task_files.py, tests/test_agents_md_claims.py, tests/test_public_export_surface.py, tests/test_all_commands_help_dryrun.py and tests/test_mcp.py.
    • Pending a person: uv run devops ci passes on the delivering tree. This change ran the targeted tests, not the gate.
  8. added a commit that references this issue on Oct 23, 2026
    f209f6c
  9. removed
    status/in-progress
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source/agenttype/refactor

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions