Skip to content

feat: Multi-Chat Awareness & Cross-Thread State Sharing#1610

Open
gdeyoung wants to merge 1 commit into
agent0ai:mainfrom
gdeyoung:feature/multi-chat-awareness
Open

feat: Multi-Chat Awareness & Cross-Thread State Sharing#1610
gdeyoung wants to merge 1 commit into
agent0ai:mainfrom
gdeyoung:feature/multi-chat-awareness

Conversation

@gdeyoung
Copy link
Copy Markdown
Contributor

@gdeyoung gdeyoung commented May 7, 2026

Multi-Chat Awareness & Cross-Thread State Sharing

Problem

When running multiple concurrent chat threads in Agent Zero, agents have no visibility into what other threads are doing. This leads to:

  • Silent conflicts — Multiple threads modifying the same files simultaneously
  • Duplicate work — No way to see if another thread is already working on a task
  • Lost context — Tasks created by one thread are invisible to all others
  • No task provenance — No way to know which chat thread created which scheduled task

Solution

This PR adds automatic cross-thread state sharing with minimal surface area:

1. Task Source Chat (Core: 7 lines across 2 files)

  • Adds source_chat: Optional[str] = Field(default=None) to BaseTask in task_scheduler.py
  • Captures self.agent.context.id in all 3 task creation methods in scheduler.py
  • Enables task provenance — every task knows which chat created it

2. Three Framework Extensions (208 lines across 3 files)

Extension Hook Purpose
_20_register_chat_state.py agent_init Auto-registers each new chat thread in shared state
_20_inject_cross_thread_state.py message_loop_start Injects other threads activity into agent context every turn
_20_log_cross_thread_turn.py message_loop_end Auto-logs each turn for cross-thread visibility

3. Thread-Safe State File

Extensions use ~/.a0_cross_thread_state.json with fcntl.flock for atomic reads/writes. No external dependencies — pure stdlib.

Architecture

NEW CHAT THREAD STARTS
└── agent_init extension → registers chat in shared state

EVERY MESSAGE LOOP TURN
├── message_loop_start → reads shared state, injects summary
│ Shows: active chats, recent changes, open issues
└── message_loop_end → logs turn activity to shared state
Records: chat_id, action summary, timestamp

SCHEDULER CREATES TASK
└── task.source_chat = agent.context.id
Every task knows its originating chat

Backward Compatibility

  • 100% backward compatible — source_chat defaults to None
  • No core behavior changes — extensions are additive
  • No new dependencies — uses only Python stdlib (json, fcntl, os)
  • Opt-in at runtime — extensions only activate if state file exists

Testing

Running in production with 19 concurrent scheduled tasks and 34 active chat threads:

  • Thread-safe concurrent writes verified (10 parallel writes, 0 corruption)
  • Auto-registration working for new chats
  • Cross-thread state injection working every turn
  • Task source_chat captured for all new tasks

Files Changed

helpers/task_scheduler.py | 1 +
tools/scheduler.py | 6 ++
extensions/python/agent_init/_20_register_chat_state.py | 73 +++++++++
extensions/python/message_loop_start/_20_inject_cross_thread_state.py | 52 ++++++
extensions/python/message_loop_end/_20_log_cross_thread_turn.py | 83 ++++++++++
5 files changed, 215 insertions(+)

Adds cross-thread state sharing for multi-chat deployments:

1. source_chat field in BaseTask (task provenance)
   - Optional[str] field records which chat created each task
   - Captured automatically in all 3 task creation methods

2. Three framework extensions for automatic state sharing:
   - agent_init: registers chat thread in shared state
   - message_loop_start: injects other threads' state into context
   - message_loop_end: logs turn activity for cross-thread visibility

3. Thread-safe file-based state with flock for concurrent access

All changes are backward-compatible. New fields default to None.
Extensions use file-based state with flock for thread safety.
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.

1 participant