MatrixSwarm agents don’t wait — they react. Reflex threads are lightweight, continuous loops that scan for events like dropped files, message triggers, or mission updates. They are the Swarm's nervous system.
🔁 What is a Reflex Thread?
A reflex thread is a daemon loop that runs as long as agent.running = True
. It usually watches a folder and acts on incoming `.msg`, `.cmd`, or `.spawn` files.
def reflex_loop():
while agent.running:
scan_folder()
time.sleep(2)
These threads are usually defined inside factories and launched from attach()
.
📂 Example: Alert Listener
In alert.subscriber
, the reflex loop looks like:
def reflex_loop():
inbox = "/comm/telegram-relay-1/incoming/critical"
while agent.running:
for fname in os.listdir(inbox):
if fname.endswith(".msg"):
with open(fname) as f:
payload = json.load(f)
send_alert(payload)
time.sleep(2)
🧠 Why Use Reflex Threads?
- They don’t block — they scan.
- They don’t listen on ports — they monitor folders.
- They run in the background, watching and reacting to anything the Swarm drops into place.
🔧 Launching Reflex Threads
Launch them inside attach()
:
threading.Thread(
target=reflex_loop,
daemon=True,
name="telegram-relay-1_reflex"
).start()
You can run multiple reflex threads per agent, each watching a different folder or stack.
Reflex threads are what make MatrixSwarm feel alive. They are the impulse behind every action, the silent heartbeat beneath every folder.