Skip to main content
Glama

Auto Heal Hallucinated Code and Phantom Symbols

heal_hallucinated_code
Read-onlyIdempotent

Detect and repair hallucinated imports, identifiers, and method calls in AI-generated Python code by comparing candidate code against workspace AST symbol tables and applying AST corrections.

Instructions

Statically inspects candidate code against verified workspace AST symbol tables, detects phantom imports, misspelled identifiers, and hallucinated method calls, and rewrites the AST to the correct valid local identifier, distinguishing from 'verify_security_guard' and 'audit_code_quality' by actively applying AST node corrections rather than merely reporting violations or secret leaks.

Parameters:

  • code (str, required): The candidate Python source code string containing potential AI hallucinations or phantom methods.

  • workspace_dir (str, optional, default='.'): Target repository root directory containing ground-truth AST symbol definitions.

Behavior:

  • Mutability: Non-destructive in-memory analysis and AST transformation; does not alter disk files.

  • Idempotency: Returns identical corrected code and diffs for identical candidate code input.

  • Performance & Latency: Completes AST inspection and healing in <100ms; consumes <35MB RAM.

  • Privacy: 100% offline local AST parsing; zero external network transmission.

  • Auth & Limits: Zero external credentials or API keys required; zero rate limits.

  • Error Handling: Emits clean syntax error diagnostic if candidate code fails initial AST parsing.

Usage Guidelines:

  • When to use: Use when repairing AI-generated code that contains hallucinated method names, phantom imports, or misspelled identifiers against real workspace AST symbol tables.

  • When NOT to use: Do not use for general codebase static linting (use 'audit_code_quality' instead); do not use when merely checking if code is safe to execute without applying AST repairs (use 'verify_security_guard' instead).

Returns: Formatted Markdown report containing total repaired symbols, confidence percentages, unified AST diff, and healed code block.

Input Schema

NameRequiredDescriptionDefault
codeYesCandidate Python code to inspect and repair.
workspace_dirNoTarget repository root directory (default: current directory)..

Output Schema

NameRequiredDescriptionDefault
resultYes

Schema Changelog

Changes observed during successful MCP inspections.

  1. Addedv10.6.0

TDQS

A4.6/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnly, idempotent, non-destructive, and closed-world, so the Mutability and Idempotency bullets largely restate structured data. The description still adds genuinely new context: in-memory only (no disk writes), <100ms/<35MB resource envelope, fully offline with no credentials or rate limits, and clean syntax-error diagnostics on parse failure.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Well organized with labeled sections (Parameters, Behavior, Usage Guidelines, Returns) and front-loaded purpose, so an agent can skim. Some sentences are run-ons and the Mutability/Idempotency/Privacy bullets partially duplicate annotations or restate each other, costing a little tightness.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Despite an output schema existing, the description still summarizes the return shape (repair count, confidence, unified diff, healed code), and it covers failure modes, resource envelope, and safety profile. For a two-parameter repair tool, nothing an agent needs to call it correctly is missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so baseline is 3. The description goes further by clarifying that workspace_dir is the repository root containing ground-truth AST symbol definitions — the semantic role of that path, which the schema does not state — and that code is a candidate string that may contain phantom methods.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Names a specific verb+resource pair ('statically inspects candidate code against verified workspace AST symbol tables' ... 'rewrites the AST') and explicitly distinguishes itself from two named siblings by saying it actively applies corrections rather than reporting. An agent can route to it without opening any schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Has explicit 'When to use' and 'When NOT to use' sections that name the alternative sibling for each excluded case (audit_code_quality for linting, verify_security_guard for safety checks without repair). No inference required.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.