This document provides a deep dive into the internal mechanics of the Cortex Swarm Control system, tracing a command from the user interface to the remote agent node execution and back.
RagPipeline calls the LLM with the current mesh context. The LLM decides to use mesh_terminal_control.ToolService intercepts the request. It initializes a Sub-Agent to manage the lifecycle of this specific terminal task.AssistantService generates a unique task_id and registers it in the TaskJournal. It signs the command payload using an RSA-PSS signature to ensure integrity.node.queue).AgentOrchestrator service (running a bidirectional gRPC stream) pops the message and sends a ServerTaskMessage (Protobuf) to the connected Agent Node.ShellSkill initializes a pseudo-terminal (PTY) for the command. This provides a stateful session where environment variables and working directories persist.TaskResult messages.grpc_server.py on the Hub receives the output chunks and updates the TaskJournal. It also broadcasts these via WebSockets for the UI's real-time terminal display.thought_history and current stdout of the task.FINISH, WAIT, ABORT, or EXECUTE. If EXECUTE is chosen, the Sub-Agent can launch new commands on any node in the swarm (e.g., pivot to Node-B after Node-A is ready).RagPipeline receives the Sub-Agent's final report and feeds it back to the main LLM to generate the final user-facing answer.sequenceDiagram
participant User as 💻 User (UI)
participant Hub as 🧠AI Hub (Orchestrator)
participant Sub as 🤖 Sub-Agent (Monitor)
participant Node as 📡 Agent Node (PTY)
User->>Hub: "List files on Node-A"
Hub->>Hub: LLM: Use mesh_terminal_control
Hub->>Sub: Instantiate Sub-Agent
Sub->>Hub: dispatch_single(cmd, node_a)
Hub->>Hub: TaskJournal.register(tid)
Hub->>Node: gRPC: TaskRequest (Signed)
loop Monitoring Loop
Node-->>Hub: gRPC: TaskResult (stdout)
Hub-->>User: WS: task_stdout (Live Terminal)
Sub->>Hub: peek_journal(tid)
Sub->>Sub: LLM Decision: WAIT/FINISH/EXECUTE
opt Branching Action
Sub->>Hub: dispatch_single(new_cmd, node_b)
Hub->>Node: gRPC: New Task (Signed)
end
end
Sub->>Hub: Task Done
Hub->>User: "Here is the list of files..."
ThreadPoolExecutor in dispatch_swarm may encounter GIL contention or network I/O limits on the Hub.RagPipeline currently blocks while waiting for the ToolService to return. This prevents the user from asking follow-up questions while a background task is running, unless no_abort=True is used for asynchronous polling.TaskCancel message fails to reach it.dispatch_swarm, results are returned in a dictionary keyed by node_id. If multiple tasks are dispatched to the same node in very rapid succession, session_id conflicts must be avoided (mitigated by using unique PTY sessions).TaskJournal state to Redis or a database to allow Hub restarts without losing task monitoring state.