Skip to content

Fix #2: Implement per-agent namespace isolation for Python REPL#7

Merged
getOffIt merged 1 commit intomainfrom
fix/issue-2-python-repl-namespace-isolation
Dec 28, 2025
Merged

Fix #2: Implement per-agent namespace isolation for Python REPL#7
getOffIt merged 1 commit intomainfrom
fix/issue-2-python-repl-namespace-isolation

Conversation

@getOffIt
Copy link
Copy Markdown
Owner

Summary

Fixes #2 by implementing per-agent namespace isolation for the Python REPL tool. This solves both the original NameError issue and prevents variable conflicts between agents.

Problem

The original issue reported that variables defined in one python_repl_tool call were not available in subsequent calls, causing NameError exceptions like:

  • NameError: name 'assessments' is not defined
  • NameError: name 'research_content' is not defined
  • NameError: name 'research_file' is not defined

Additionally, the shared namespace approach could cause variable conflicts between different agents.

Solution

Implemented a persistent Python REPL with per-agent namespaces:

  1. Persistent subprocess: Maintains a single Python subprocess that stays alive between calls
  2. Per-agent namespaces: Each agent (coder, reporter, validator, researcher) has its own isolated namespace dictionary
  3. Thread-local storage: Agent context is stored in thread-local storage and automatically passed to the REPL
  4. Variable persistence: Variables defined by an agent persist within that agent's execution
  5. Complete isolation: Different agents can use the same variable names without conflicts

Changes

Core Implementation

  • python_repl_tool.py:
    • Refactored PythonREPL class to use persistent subprocess with per-agent namespaces
    • Added thread-local storage for agent context
    • Updated tool description to mention variable persistence

Agent Integration

  • coder_agent_tool.py: Sets agent name before execution
  • reporter_agent_tool.py: Sets agent name (reporter-1 or reporter-2) before execution
  • validator_agent_tool.py: Sets agent name before execution
  • researcher_agent_tool.py: Sets agent name before execution

Testing

  • test_python_repl.py: Basic functionality tests
  • test_repl_integration.py: Integration tests simulating actual agent usage
  • test_repl_agent_isolation.py: Comprehensive isolation verification tests
  • TESTING_REPL.md: Testing guide and documentation
  • PERSISTENT_REPL_RISKS.md: Risk analysis and design decisions

Benefits

Fixes original issue: Variables persist within agent execution (no more NameError)
Prevents conflicts: Agents are isolated from each other
Maintains backward compatibility: Existing code continues to work
Thread-safe: Uses locks to prevent concurrent execution issues
Automatic recovery: Reinitializes subprocess if it dies

Testing

All tests pass:

  • ✅ Variable persistence within agent
  • ✅ Per-agent namespace isolation
  • ✅ Error handling
  • ✅ Integration with actual agent usage patterns

Run tests:

python test_python_repl.py
python test_repl_integration.py
python test_repl_agent_isolation.py

Impact

  • Before: Agents had to re-execute code blocks, adding ~5-10 minutes of execution time
  • After: Variables persist, eliminating re-execution overhead
  • Risk mitigation: Per-agent isolation prevents state pollution and variable conflicts

Related

Closes #2

- Implement persistent Python REPL with per-agent namespaces
- Variables persist within each agent's execution (fixes NameError)
- Agents are isolated from each other (prevents variable conflicts)
- Add thread-local storage for agent context
- Update all agent tools to set agent name before execution
- Add comprehensive test suite for isolation verification

Fixes: #2
@getOffIt getOffIt merged commit 54f7de2 into main Dec 28, 2025
@getOffIt getOffIt deleted the fix/issue-2-python-repl-namespace-isolation branch December 28, 2025 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MODERATE] Python Variable Scope Errors in REPL Tool Execution

2 participants