-
Notifications
You must be signed in to change notification settings - Fork 453
Bug: send_file_to_agent crashes with UnboundLocalError on AgentAgentRelationship #296
Description
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
- Create two agents (e.g., Agent A and Agent B) with a colleague relationship
- Agent A writes a file to its workspace:
write_file(path="workspace/test.txt", content="hello") - Agent A calls:
send_file_to_agent(agent_name="Agent B", file_path="workspace/test.txt") - 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