-
-
Notifications
You must be signed in to change notification settings - Fork 0
Agent Communication
Multi-agent coordination and message passing.
- Overview
- Loading the Library
- Agent Registration
- Message Passing
- Task Coordination
- Shared State
- Locking and Synchronization
- Configuration
- Patterns
The Agent Communication library (agent_comm.sh) enables multiple AI agents to:
| Feature | Description |
|---|---|
| Registration | Agents announce their presence |
| Messaging | Send and receive messages |
| Task Coordination | Claim and complete tasks |
| Shared State | Read and write shared variables |
| Locking | Prevent concurrent execution |
source "$MAINFRAME_ROOT/lib/agent_comm.sh"Register an agent with the system.
agent_register "worker-1"
# Creates: $AGENT_BASE_DIR/worker-1/Remove an agent from the system.
agent_unregister "worker-1"List all registered agents.
agents=$(agent_list)
# Returns JSON array: ["worker-1", "worker-2"]Check agent status.
status=$(agent_status "worker-1")
# Returns: {"ok":true,"data":{"id":"worker-1","status":"active","last_seen":"..."}}Update agent's last-seen timestamp.
agent_heartbeat "worker-1"Send a message to another agent.
agent_send "worker-2" "task" '{"action":"process","file":"data.csv"}'Parameters:
-
$1- Target agent ID -
$2- Message type -
$3- Message payload (JSON)
Receive messages for an agent.
messages=$(agent_receive "worker-1" 10)
# Returns array of messages, max 10Message Format:
{
"id": "msg-uuid",
"from": "sender-id",
"to": "receiver-id",
"type": "task",
"payload": {"action": "process"},
"timestamp": "2024-01-15T10:30:00"
}Receive messages of specific type.
tasks=$(agent_receive_type "worker-1" "task" 5)Send message to all agents.
agent_broadcast "announcement" '{"message":"System maintenance in 5 minutes"}'Acknowledge a message (marks as processed).
agent_ack "msg-uuid"Post a task for any agent to claim.
task_id=$(agent_post_task "process" '{"file":"data.csv"}')
# Returns task IDClaim an unclaimed task.
if agent_claim_task "$task_id" "worker-1"; then
# Task claimed successfully
process_task "$task_id"
else
# Already claimed by another agent
fiMark a task as complete.
agent_complete_task "$task_id" '{"result":"success","rows":1000}'Mark a task as failed.
agent_fail_task "$task_id" '{"error":"File not found"}'List tasks by status.
pending=$(agent_list_tasks "pending")
active=$(agent_list_tasks "active")
completed=$(agent_list_tasks "completed")Get task details.
task=$(agent_get_task "$task_id")
# Returns: {"id":"...","type":"process","status":"active","claimed_by":"worker-1",...}Set a shared state variable.
agent_set_state "progress" "50%"
agent_set_state "config" '{"timeout":30}'Get a shared state variable.
progress=$(agent_get_state "progress")
# Returns: 50%
config=$(agent_get_state "config")
# Returns: {"timeout":30}Delete a shared state variable.
agent_delete_state "temp_value"List all state keys.
keys=$(agent_list_state)
# Returns: ["progress", "config"]Atomically increment a numeric state.
agent_set_state "counter" "0"
agent_increment_state "counter" # Now 1
agent_increment_state "counter" # Now 2Acquire a named lock.
if agent_lock "deploy" "worker-1" 30; then
# Lock acquired, valid for 30 seconds
do_deployment
agent_unlock "deploy" "worker-1"
else
echo "Lock held by another agent"
fiRelease a lock.
agent_unlock "deploy" "worker-1"Execute with lock held.
agent_with_lock "critical_section" "worker-1" 60 \
do_critical_workWait for a condition.
# Wait for another agent to set a state
agent_wait_for "initialization_complete" "true" 30| Variable | Default | Description |
|---|---|---|
AGENT_BASE_DIR |
/tmp/mainframe_agents |
Base directory for agent data |
AGENT_MESSAGE_TTL |
3600 | Message time-to-live (seconds) |
AGENT_TASK_TTL |
86400 | Task time-to-live (seconds) |
AGENT_LOCK_TTL |
300 | Default lock TTL (seconds) |
$AGENT_BASE_DIR/
├── agents/
│ ├── worker-1/
│ │ ├── status.json
│ │ └── inbox/
│ └── worker-2/
│ ├── status.json
│ └── inbox/
├── tasks/
│ ├── pending/
│ ├── active/
│ └── completed/
├── state/
│ ├── progress
│ └── config
└── locks/
└── deploy
#!/bin/bash
# Worker agent
source "$MAINFRAME_ROOT/lib/agent_comm.sh"
WORKER_ID="worker-$$"
agent_register "$WORKER_ID"
while true; do
# Get pending task
task_id=$(agent_list_tasks "pending" | head -1)
if [[ -n "$task_id" ]] && agent_claim_task "$task_id" "$WORKER_ID"; then
task=$(agent_get_task "$task_id")
# Process task
if result=$(process_task "$task"); then
agent_complete_task "$task_id" "$result"
else
agent_fail_task "$task_id" "$result"
fi
fi
agent_heartbeat "$WORKER_ID"
sleep 1
done# Producer
for file in /data/*.csv; do
agent_post_task "process" "$(json_object "file=$file")"
done
# Consumer (in worker)
while task_id=$(agent_list_tasks "pending" | head -1); do
agent_claim_task "$task_id" "$WORKER_ID"
# Process...
done# Leader
agent_set_state "phase" "initialization"
# ... do initialization ...
agent_set_state "phase" "ready"
# Workers
phase=$(agent_get_state "phase")
while [[ "$phase" != "ready" ]]; do
sleep 1
phase=$(agent_get_state "phase")
done
# Now proceed| Function | Description |
|---|---|
agent_register |
Register agent |
agent_unregister |
Remove agent |
agent_list |
List all agents |
agent_status |
Get agent status |
agent_heartbeat |
Update last-seen |
agent_send |
Send message |
agent_receive |
Receive messages |
agent_broadcast |
Broadcast to all |
agent_post_task |
Create task |
agent_claim_task |
Claim task |
agent_complete_task |
Complete task |
agent_fail_task |
Fail task |
agent_set_state |
Set shared state |
agent_get_state |
Get shared state |
agent_lock |
Acquire lock |
agent_unlock |
Release lock |
MAINFRAME · The AI-Native Bash Runtime
MAINFRAME · The AI-Native Bash Runtime
2,000+ Functions · Zero Dependencies · Safe by Default
GitHub · Discussions · Issues
"Knowing Your Shell is half the battle."