Skip to content

Bug: send_file_to_agent crashes with UnboundLocalError on AgentAgentRelationship #296

@39499740

Description

@39499740

Bug: send_file_to_agent crashes with cannot access local variable 'AgentAgentRelationship'

Environment

  • Clawith version: latest (deployed from source)
  • Python 3.x
  • OS: Ubuntu 22.04

Description

When an agent calls send_file_to_agent to transfer a file to another agent, the tool crashes with:

cannot access local variable 'AgentAgentRelationship' where it is not associated with a value

This happens only when the target agent is found by name (the normal path). When the target agent is NOT found, the error does not occur because the import happens inside the same branch.

Root Cause

In backend/app/services/agent_tools.py, the _send_file_to_agent function has a scoping issue:

# Line ~4084: inside "if not target_agent:" branch
if not target_agent:
    from app.models.org import AgentAgentRelationship  # import only here
    rel_r = await db.execute(...)
    rel_names = [...]
    return f"No agent found..."

# Line ~4098: OUTSIDE the if branch - normal execution path
rel_check = await db.execute(
    select(AgentAgentRelationship.id).where(...)  # NameError!
)

The from app.models.org import AgentAgentRelationship statement is placed inside the if not target_agent: block. When the target agent IS found (which is the common case), the import is never executed, but AgentAgentRelationship is referenced later in the relationship enforcement check, causing Python to raise an UnboundLocalError.

Steps to Reproduce

  1. Create two agents (e.g., Agent A and Agent B) with a colleague relationship
  2. Agent A writes a file to its workspace: write_file(path="workspace/test.txt", content="hello")
  3. Agent A calls: send_file_to_agent(agent_name="Agent B", file_path="workspace/test.txt")
  4. Error occurs: cannot access local variable 'AgentAgentRelationship'

Expected Behavior

The file should be copied to Agent B's workspace/inbox/files/ directory and a delivery note should be created.

Actual Behavior

The tool returns an error and no file is transferred.

Suggested Fix

Move the import statement outside the conditional block. Add from app.models.org import AgentAgentRelationship before the relationship enforcement check (around line 4098):

# Before the relationship check
from app.models.org import AgentAgentRelationship

rel_check = await db.execute(
    select(AgentAgentRelationship.id).where(
        ((AgentAgentRelationship.agent_id == from_agent_id) & (AgentAgentRelationship.target_agent_id == target_agent.id))
        | ((AgentAgentRelationship.agent_id == target_agent.id) & (AgentAgentRelationship.target_agent_id == from_agent_id))
    ).limit(1)
)

Impact

  • Agent-to-agent file transfer is completely broken for all agents
  • Agents can only exchange information via send_message_to_agent (text only)
  • File-based workflows (e.g., sharing scan results, reports, data files) are blocked

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions