File size: 7,972 Bytes
90077e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Multi-Agent Architecture for HuggingClaw World

## Overview

The HuggingClaw World multi-agent system enables coordinated operation of multiple specialized agents (Adam, Eve, Cain) with role-based access control (RBAC) and inter-agent communication via A2A protocol.

## Architecture

### Agent Roles

| Agent | Role | Responsibilities |
|-------|------|-------------------|
| **Adam** | Infrastructure | System-level operations, deployment, monitoring, HuggingFace Space management |
| **Eve** | UI Logic | Frontend rendering, display management, user interface |
| **Cain** | Interaction | User message processing, conversation handling, memory management |

### File Structure

```
.openclaw/
β”œβ”€β”€ agents/
β”‚   β”œβ”€β”€ registry.json          # Agent registry with capabilities and status
β”‚   β”œβ”€β”€ rbac.py                # Role-based access control implementation
β”‚   └── main/
β”‚       └── sessions/          # Agent session data
β”œβ”€β”€ workspace/
β”‚   β”œβ”€β”€ state.json             # Workspace state with multi-agent configuration
β”‚   └── MULTI_AGENT.md         # This documentation
β”œβ”€β”€ cron/
β”‚   β”œβ”€β”€ executor.py            # Cron job executor with tool registry
β”‚   └── jobs.json              # Job configurations
└── openclaw.json              # OpenClaw configuration
```

## State Management

### workspace/state.json

The workspace state file tracks:

- **Multi-agent mode**: Enable/disable multi-agent operation
- **Agent states**: Current status of each agent (active, idle, offline, error)
- **Active agent**: Which agent is currently running
- **Agent communication**: A2A peer endpoints
- **Permissions**: Role-based access control settings

### agents/registry.json

The agent registry tracks:

- **Agent metadata**: Name, role, workspace, description
- **Capabilities**: Allowed and forbidden tools per agent
- **Health status**: Last check time, status, failure count
- **Communication endpoints**: A2A JSON-RPC endpoints

## Role-Based Access Control

### TOOL_REGISTRY

The central `TOOL_REGISTRY` in `rbac.py` defines:

- **Tool metadata**: Description, required authentication level
- **Role mapping**: Which roles can use each tool
- **Permission level**: ALLOWED, FORBIDDEN, or ROLE_SPECIFIC

### Tool Categories

#### Infrastructure Tools (Adam only)
- `hf_space_status` - Check HuggingFace Space status
- `hf_restart_space` - Restart a HuggingFace Space
- `hf_create_space` - Create a new Space
- `hf_delete_space` - Delete a Space
- `deploy_agent` - Deploy a new agent instance
- `system_monitor` - Monitor system health
- `log_viewer` - View system logs

#### UI Tools (Eve only)
- `ui_render` - Render UI components
- `frontend_update` - Update frontend content
- `bubble_set` - Set speech bubble text
- `chatlog_post` - Post chat log entries
- `display_manage` - Manage display settings
- `theme_update` - Update UI theme

#### Interaction Tools (Cain only)
- `message_send` - Send messages via A2A
- `conversation_process` - Process conversation input
- `memory_read` - Read from agent memory
- `memory_write` - Write to agent memory
- `session_archive` - Archive old sessions
- `context_manage` - Manage conversation context

#### Shared Tools (All agents)
- `health_check` - Perform health check
- `get_status` - Get agent status
- `agent_ping` - Ping another agent

## Usage

### Initialize the RBAC System

```python
from .openclaw.agents.rbac import MultiAgentSystem, check_permission, filter_tools_for_agent

# Get the RBAC system instance
rbac = MultiAgentSystem()

# Check if current agent can use a tool
if rbac.check_tool_permission("hf_restart_space"):
    # Execute the tool
    pass

# Get allowed tools for current agent
allowed_tools = rbac.get_allowed_tools()

# Get peer agents for A2A communication
peers = rbac.get_peers()
```

### Permission Checking

```python
# Check permission for a specific agent
from .openclaw.agents.rbac import check_permission

# Check if Cain can use a tool
can_use = check_permission("message_send", "cain")

# Check if Adam can use a tool
can_restart = check_permission("hf_restart_space", "adam")
```

### Filter Tools for Agent

```python
from .openclaw.agents.rbac import filter_tools_for_agent

all_tools = ["hf_restart_space", "ui_render", "message_send"]

# Filter for Cain
cain_tools = filter_tools_for_agent(all_tools, "cain")
# Returns: ["message_send"]

# Filter for Eve
eve_tools = filter_tools_for_agent(all_tools, "eve")
# Returns: ["ui_render"]
```

## A2A Communication

Agents communicate via A2A (Agent-to-Agent) JSON-RPC protocol:

```python
import requests

def send_a2a_message(agent_url, message):
    """Send message to another agent via A2A"""
    payload = {
        "jsonrpc": "2.0",
        "id": f"msg-{int(time.time())}",
        "method": "message/send",
        "params": {
            "message": {
                "messageId": f"msg-{int(time.time())}",
                "role": "user",
                "parts": [{"type": "text", "text": message}]
            }
        }
    }
    response = requests.post(f"{agent_url}/a2a/jsonrpc", json=payload, timeout=90)
    return response.json()
```

## Backward Compatibility

The system maintains full backward compatibility with single-agent workflows:

### Legacy Mode

```python
# Enable legacy mode for single-agent operation
rbac = MultiAgentSystem(legacy_mode=True)

# In legacy mode, all tools are available
allowed_tools = rbac.get_allowed_tools()  # Returns all tools
```

### Convenience Functions

```python
from .openclaw.agents.rbac import can_use_tool, get_available_tools

# Single-agent compatible API
if can_use_tool("some_tool"):
    # Execute tool
    pass

# Get all available tools for current agent
tools = get_available_tools()
```

## Configuration

### Environment Variables

- `AGENT_NAME` - Name of the current agent (adam, eve, cain)
- `SPACE_ID` - HuggingFace Space ID (used for auto-detection)
- `A2A_PEERS` - Comma-separated list of peer agent URLs

### Multi-Agent Enable/Disable

Set in `workspace/state.json`:

```json
{
  "multi_agent_enabled": true,
  "legacy_mode": false
}
```

## Agent Detection

The system auto-detects the current agent based on:

1. `AGENT_NAME` environment variable
2. `SPACE_ID` environment variable
3. Defaults to "cain" if not found

## Health Monitoring

Agent health is tracked in the registry:

```json
{
  "health": {
    "last_check": "2026-03-14T00:00:00Z",
    "status": "healthy",
    "failures": 0
  }
}
```

Status values: `healthy`, `degraded`, `error`, `unknown`

## Error Handling

- Permission denied: Tool execution is blocked with log message
- Unknown tool: Warning logged, execution blocked
- Agent not found: Returns empty info dict
- Invalid role: Defaults to INTERACTION role

## Security

- Infrastructure tools require authentication
- Dangerous tools (restart, delete) marked in registry
- Role-based isolation prevents cross-role tool access
- A2A communication uses token-based auth

## Future Extensions

Potential additions:

- Dynamic tool registration
- Agent capability discovery
- Load balancing across agents
- Agent migration and failover
- Resource quota management
- Audit logging

## Migration Guide

To migrate from single-agent to multi-agent:

1. Create `workspace/state.json` with multi-agent configuration
2. Create `agents/registry.json` with agent definitions
3. Update code to use `rbac.py` for permission checks
4. Add A2A communication for inter-agent messages
5. Test in legacy mode first, then enable multi-agent

## Troubleshooting

### Agent not detected

Check environment variables:
```bash
echo $AGENT_NAME
echo $SPACE_ID
```

### Permission denied

Verify tool is in agent's allowed list in `registry.json`

### A2A communication fails

Check peer endpoints are reachable:
```bash
curl https://peer-agent.hf.space/a2a/jsonrpc
```

### Multi-agent mode not working

Verify `multi_agent_enabled: true` in `workspace/state.json`