[chat] ai_interaction.py uses module-level mutable globals for all manager singletons #729

Closed
opened 2026-06-03 00:08:47 +02:00 by sleepy · 0 comments
Owner

Problem

src/ai_interaction.py lines 26-31 define module-level mutable globals for all manager references:

_session_manager = None
_memory_manager = None
_memory_vector = None
_rag_manager = None
_personal_docs_manager = None

With setter functions (set_session_manager, set_memory_manager, set_rag_manager) that mutate them. Every tool function in the file accesses these globals directly.

This creates:

  1. Testing difficulty — cannot inject mocks without monkeypatching module globals
  2. Race conditions — concurrent coroutines read/write these globals
  3. Tight coupling — every function in this 1807-line file depends on global state
  4. No type safety — callers cannot verify managers are initialized

The same pattern was flagged in #671 for the tool system.

AGENTS.md violations

  • Build extensible systems with explicit extension points
  • Features communicate through narrow, typed interfaces

Suggested fix

Replace globals with an explicit AIToolContext class that receives managers via constructor injection. Functions take the context as a parameter instead of reading globals.

## Problem `src/ai_interaction.py` lines 26-31 define module-level mutable globals for all manager references: ```python _session_manager = None _memory_manager = None _memory_vector = None _rag_manager = None _personal_docs_manager = None ``` With setter functions (`set_session_manager`, `set_memory_manager`, `set_rag_manager`) that mutate them. Every tool function in the file accesses these globals directly. This creates: 1. **Testing difficulty** — cannot inject mocks without monkeypatching module globals 2. **Race conditions** — concurrent coroutines read/write these globals 3. **Tight coupling** — every function in this 1807-line file depends on global state 4. **No type safety** — callers cannot verify managers are initialized The same pattern was flagged in #671 for the tool system. ## AGENTS.md violations - Build extensible systems with explicit extension points - Features communicate through narrow, typed interfaces ## Suggested fix Replace globals with an explicit `AIToolContext` class that receives managers via constructor injection. Functions take the context as a parameter instead of reading globals.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
sleepy/odysseus#729
No description provided.