[tools] Replace global mutable state with contextvars #902

Closed
sleepy wants to merge 0 commits from fix/global-state-contextvars-671 into dev
Owner
No description provided.
- _active_document_id and _active_model: replaced module-level globals
  with contextvars.ContextVar so concurrent agent runs get their own
  isolated copy. Updated all readers (do_create_document, do_update_document,
  do_edit_document, do_suggest_document, do_manage_documents) to call
  .get() / .set() instead of reading/writing bare globals.
- _mcp_manager: kept as-is (singleton set once at startup, not a race
  condition). Added comment explaining the design choice.
- _cached_base_prompt / _cached_base_prompt_key: replaced bare module
  globals with a _PromptCache class that encapsulates check+assign in
  a single object, eliminating the non-atomic two-field read/write.
- Updated test cache-busting to use the new _PromptCache.set() API.

All 694 tests pass; 9 pre-existing failures unchanged.
sleepy closed this pull request 2026-06-04 03:24:15 +02:00

Pull request closed

Sign in to join this conversation.
No description provided.