claude_swarm 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/Rakefile +4 -4
- data/docs/v2/CHANGELOG.swarm_cli.md +19 -0
- data/docs/v2/CHANGELOG.swarm_memory.md +19 -0
- data/docs/v2/CHANGELOG.swarm_sdk.md +92 -0
- data/docs/v2/README.md +56 -22
- data/docs/v2/guides/MEMORY_DEFRAG_GUIDE.md +811 -0
- data/docs/v2/guides/complete-tutorial.md +115 -3
- data/docs/v2/guides/getting-started.md +6 -6
- data/docs/v2/guides/rails-integration.md +6 -6
- data/docs/v2/reference/architecture-flow.md +407 -0
- data/docs/v2/reference/event_payload_structures.md +471 -0
- data/docs/v2/reference/execution-flow.md +600 -0
- data/docs/v2/reference/ruby-dsl.md +138 -5
- data/docs/v2/reference/swarm_memory_technical_details.md +2090 -0
- data/examples/v2/swarm_with_hooks.yml +1 -1
- data/lib/claude_swarm/cli.rb +9 -11
- data/lib/claude_swarm/commands/ps.rb +1 -2
- data/lib/claude_swarm/configuration.rb +2 -3
- data/lib/claude_swarm/mcp_generator.rb +4 -10
- data/lib/claude_swarm/orchestrator.rb +43 -44
- data/lib/claude_swarm/system_utils.rb +4 -4
- data/lib/claude_swarm/version.rb +1 -1
- data/lib/claude_swarm.rb +4 -9
- data/lib/swarm_cli/commands/mcp_serve.rb +2 -2
- data/lib/swarm_cli/commands/mcp_tools.rb +3 -3
- data/lib/swarm_cli/config_loader.rb +14 -13
- data/lib/swarm_cli/version.rb +1 -1
- data/lib/swarm_cli.rb +2 -0
- data/lib/swarm_memory/adapters/base.rb +4 -4
- data/lib/swarm_memory/adapters/filesystem_adapter.rb +0 -12
- data/lib/swarm_memory/core/storage.rb +66 -6
- data/lib/swarm_memory/integration/sdk_plugin.rb +14 -0
- data/lib/swarm_memory/optimization/defragmenter.rb +4 -0
- data/lib/swarm_memory/tools/memory_edit.rb +1 -0
- data/lib/swarm_memory/tools/memory_glob.rb +24 -1
- data/lib/swarm_memory/tools/memory_write.rb +2 -2
- data/lib/swarm_memory/version.rb +1 -1
- data/lib/swarm_memory.rb +2 -0
- data/lib/swarm_sdk/agent/chat.rb +1 -1
- data/lib/swarm_sdk/agent/definition.rb +18 -21
- data/lib/swarm_sdk/configuration.rb +34 -10
- data/lib/swarm_sdk/mcp.rb +16 -0
- data/lib/swarm_sdk/node/agent_config.rb +7 -2
- data/lib/swarm_sdk/node/builder.rb +130 -35
- data/lib/swarm_sdk/node_context.rb +75 -0
- data/lib/swarm_sdk/node_orchestrator.rb +219 -12
- data/lib/swarm_sdk/plugin.rb +73 -1
- data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +0 -126
- data/lib/swarm_sdk/result.rb +32 -6
- data/lib/swarm_sdk/swarm/builder.rb +1 -0
- data/lib/swarm_sdk/swarm.rb +32 -50
- data/lib/swarm_sdk/tools/delegate.rb +2 -2
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_list.rb +23 -2
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_read.rb +23 -2
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_write.rb +21 -4
- data/lib/swarm_sdk/tools/stores/storage.rb +4 -4
- data/lib/swarm_sdk/tools/think.rb +4 -1
- data/lib/swarm_sdk/tools/todo_write.rb +20 -8
- data/lib/swarm_sdk/version.rb +1 -1
- data/lib/swarm_sdk.rb +332 -27
- data/swarm_sdk.gemspec +1 -1
- metadata +9 -3
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
# SwarmSDK Execution Flow
|
|
2
|
+
|
|
3
|
+
This document shows the **runtime execution flow** - what actually happens when you execute a prompt through SwarmSDK.
|
|
4
|
+
|
|
5
|
+
## Complete Execution Flow
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
flowchart TD
|
|
9
|
+
START([User sends prompt]) --> LOAD{How is swarm created?}
|
|
10
|
+
|
|
11
|
+
LOAD --> |CLI command| CLI_PARSE[CLI parses arguments]
|
|
12
|
+
LOAD --> |SDK code| SDK_CREATE[SDK creates swarm]
|
|
13
|
+
|
|
14
|
+
CLI_PARSE --> LOAD_CONFIG[Load configuration file]
|
|
15
|
+
SDK_CREATE --> LOAD_CONFIG
|
|
16
|
+
|
|
17
|
+
LOAD_CONFIG --> PARSE_CONFIG[Parse YAML or execute Ruby DSL]
|
|
18
|
+
PARSE_CONFIG --> VALIDATE_CONFIG{Valid configuration?}
|
|
19
|
+
|
|
20
|
+
VALIDATE_CONFIG --> |No| ERROR_RETURN[Return validation errors]
|
|
21
|
+
VALIDATE_CONFIG --> |Yes| CREATE_SWARM[Create Swarm instance]
|
|
22
|
+
|
|
23
|
+
ERROR_RETURN --> END_ERROR([End with errors])
|
|
24
|
+
|
|
25
|
+
CREATE_SWARM --> STORE_AGENTS[Store agent definitions]
|
|
26
|
+
STORE_AGENTS --> SETUP_HOOKS[Setup hook registry]
|
|
27
|
+
SETUP_HOOKS --> READY[Swarm ready]
|
|
28
|
+
|
|
29
|
+
READY --> EXECUTE[swarm.execute called]
|
|
30
|
+
EXECUTE --> SETUP_LOGGING{Logging enabled?}
|
|
31
|
+
|
|
32
|
+
SETUP_LOGGING --> |Yes| INIT_LOGGER[Initialize LogStream & LogCollector]
|
|
33
|
+
SETUP_LOGGING --> |No| HOOK_SWARM_START
|
|
34
|
+
INIT_LOGGER --> HOOK_SWARM_START[Trigger swarm_start hooks]
|
|
35
|
+
|
|
36
|
+
HOOK_SWARM_START --> HOOK_START_RESULT{Hook result?}
|
|
37
|
+
HOOK_START_RESULT --> |halt| END_ERROR
|
|
38
|
+
HOOK_START_RESULT --> |replace| MODIFY_PROMPT[Append hook output to prompt]
|
|
39
|
+
HOOK_START_RESULT --> |continue| CHECK_FIRST
|
|
40
|
+
MODIFY_PROMPT --> CHECK_FIRST
|
|
41
|
+
|
|
42
|
+
CHECK_FIRST{First message?} --> |Yes| HOOK_FIRST[Trigger first_message hooks]
|
|
43
|
+
CHECK_FIRST --> |No| INIT_CHECK
|
|
44
|
+
HOOK_FIRST --> INIT_CHECK
|
|
45
|
+
|
|
46
|
+
INIT_CHECK{Agents initialized?} --> |No| INIT_AGENTS[Initialize agents - 5 passes]
|
|
47
|
+
INIT_CHECK --> |Yes| EXEC_LOOP
|
|
48
|
+
|
|
49
|
+
INIT_AGENTS --> PASS1[Pass 1: Create Agent::Chat instances]
|
|
50
|
+
PASS1 --> CREATE_TOOLS[Create tools with permissions]
|
|
51
|
+
CREATE_TOOLS --> REGISTER_MCP[Register MCP servers]
|
|
52
|
+
REGISTER_MCP --> PLUGIN_INIT[Plugin initialization]
|
|
53
|
+
PLUGIN_INIT --> CREATE_STORAGE{Memory enabled?}
|
|
54
|
+
|
|
55
|
+
CREATE_STORAGE --> |Yes| MEMORY_STORAGE[Create memory storage]
|
|
56
|
+
CREATE_STORAGE --> |No| PASS2
|
|
57
|
+
MEMORY_STORAGE --> REGISTER_MEMORY_TOOLS[Register memory tools]
|
|
58
|
+
REGISTER_MEMORY_TOOLS --> PASS2
|
|
59
|
+
|
|
60
|
+
PASS2[Pass 2: Register delegation tools]
|
|
61
|
+
PASS2 --> PASS3[Pass 3: Setup agent contexts]
|
|
62
|
+
PASS3 --> PASS4[Pass 4: Configure hook callbacks]
|
|
63
|
+
PASS4 --> PASS5[Pass 5: Apply YAML hooks]
|
|
64
|
+
PASS5 --> EMIT_AGENT_START[Emit agent_start events]
|
|
65
|
+
EMIT_AGENT_START --> EXEC_LOOP
|
|
66
|
+
|
|
67
|
+
EXEC_LOOP[Execution Loop Start]
|
|
68
|
+
EXEC_LOOP --> USER_PROMPT[Lead agent receives prompt]
|
|
69
|
+
|
|
70
|
+
USER_PROMPT --> HOOK_USER_PROMPT[Trigger user_prompt hooks]
|
|
71
|
+
HOOK_USER_PROMPT --> MEMORY_DISCOVER{Memory enabled?}
|
|
72
|
+
|
|
73
|
+
MEMORY_DISCOVER --> |Yes| SKILL_DISCOVERY[Semantic skill discovery]
|
|
74
|
+
MEMORY_DISCOVER --> |No| RATE_LIMIT
|
|
75
|
+
|
|
76
|
+
SKILL_DISCOVERY --> CHECK_SKILLS{Skills found?}
|
|
77
|
+
CHECK_SKILLS --> |Yes| LOAD_SKILLS[Auto-load skills as tools]
|
|
78
|
+
CHECK_SKILLS --> |No| RATE_LIMIT
|
|
79
|
+
LOAD_SKILLS --> RATE_LIMIT
|
|
80
|
+
|
|
81
|
+
RATE_LIMIT[Acquire rate limit semaphore]
|
|
82
|
+
RATE_LIMIT --> SEND_LLM[Send to LLM API]
|
|
83
|
+
|
|
84
|
+
SEND_LLM --> LLM_RESPONSE{Response type?}
|
|
85
|
+
|
|
86
|
+
LLM_RESPONSE --> |Text only| FINAL_RESPONSE
|
|
87
|
+
LLM_RESPONSE --> |Tool calls| EMIT_STEP[Emit agent_step event]
|
|
88
|
+
|
|
89
|
+
EMIT_STEP --> TOOL_LOOP[Process each tool call]
|
|
90
|
+
|
|
91
|
+
TOOL_LOOP --> HOOK_PRE[Trigger pre_tool_use hook]
|
|
92
|
+
HOOK_PRE --> HOOK_PRE_RESULT{Hook result?}
|
|
93
|
+
|
|
94
|
+
HOOK_PRE_RESULT --> |halt| STOP_EXECUTION[Stop execution]
|
|
95
|
+
HOOK_PRE_RESULT --> |replace| MODIFY_TOOL[Modify tool parameters]
|
|
96
|
+
HOOK_PRE_RESULT --> |continue| CHECK_TOOL_TYPE
|
|
97
|
+
MODIFY_TOOL --> CHECK_TOOL_TYPE
|
|
98
|
+
|
|
99
|
+
CHECK_TOOL_TYPE{Tool type?}
|
|
100
|
+
|
|
101
|
+
CHECK_TOOL_TYPE --> |Delegation| DELEGATE_START[Call delegate agent]
|
|
102
|
+
CHECK_TOOL_TYPE --> |Memory| MEMORY_OP
|
|
103
|
+
CHECK_TOOL_TYPE --> |File| FILE_OP
|
|
104
|
+
CHECK_TOOL_TYPE --> |Bash| BASH_OP
|
|
105
|
+
CHECK_TOOL_TYPE --> |Default| DEFAULT_OP
|
|
106
|
+
CHECK_TOOL_TYPE --> |Scratchpad| SCRATCHPAD_OP
|
|
107
|
+
|
|
108
|
+
%% Delegation flow
|
|
109
|
+
DELEGATE_START --> HOOK_PRE_DELEGATE[Trigger pre_delegation hook]
|
|
110
|
+
HOOK_PRE_DELEGATE --> RECURSIVE_ASK[Recursively call agent.ask]
|
|
111
|
+
RECURSIVE_ASK --> HOOK_POST_DELEGATE[Trigger post_delegation hook]
|
|
112
|
+
HOOK_POST_DELEGATE --> TOOL_RESULT
|
|
113
|
+
|
|
114
|
+
%% Memory operations
|
|
115
|
+
MEMORY_OP{Memory operation?}
|
|
116
|
+
MEMORY_OP --> |MemoryWrite| EXTRACT_META[Extract frontmatter metadata]
|
|
117
|
+
MEMORY_OP --> |MemoryRead| FETCH_ENTRY[Fetch entry from storage]
|
|
118
|
+
MEMORY_OP --> |MemoryGrep| SEMANTIC_SEARCH[Perform semantic search]
|
|
119
|
+
MEMORY_OP --> |MemoryEdit| EDIT_ENTRY[Edit existing entry]
|
|
120
|
+
MEMORY_OP --> |LoadSkill| LOAD_SKILL_EXEC[Load skill and swap tools]
|
|
121
|
+
|
|
122
|
+
EXTRACT_META --> GENERATE_EMBED[Generate embedding with ONNX]
|
|
123
|
+
GENERATE_EMBED --> UPDATE_INDEX[Update FAISS vector index]
|
|
124
|
+
UPDATE_INDEX --> PERSIST[Persist to filesystem]
|
|
125
|
+
PERSIST --> TOOL_RESULT
|
|
126
|
+
|
|
127
|
+
FETCH_ENTRY --> FOLLOW_STUB{Is stub?}
|
|
128
|
+
FOLLOW_STUB --> |Yes| REDIRECT[Follow redirect]
|
|
129
|
+
FOLLOW_STUB --> |No| RETURN_CONTENT
|
|
130
|
+
REDIRECT --> RETURN_CONTENT[Return content]
|
|
131
|
+
RETURN_CONTENT --> TOOL_RESULT
|
|
132
|
+
|
|
133
|
+
SEMANTIC_SEARCH --> EMBED_QUERY[Embed search query]
|
|
134
|
+
EMBED_QUERY --> FAISS_SEARCH[Search FAISS index]
|
|
135
|
+
FAISS_SEARCH --> RANK_RESULTS[Rank by similarity]
|
|
136
|
+
RANK_RESULTS --> TOOL_RESULT
|
|
137
|
+
|
|
138
|
+
EDIT_ENTRY --> TOOL_RESULT
|
|
139
|
+
LOAD_SKILL_EXEC --> SWAP_TOOLS[Replace agent tools]
|
|
140
|
+
SWAP_TOOLS --> TOOL_RESULT
|
|
141
|
+
|
|
142
|
+
%% File operations
|
|
143
|
+
FILE_OP{File operation?}
|
|
144
|
+
FILE_OP --> |Read/Glob/Grep| CHECK_READ_PERMS[Check allowed paths]
|
|
145
|
+
FILE_OP --> |Write/Edit| CHECK_WRITE_PERMS[Check allowed/denied paths]
|
|
146
|
+
|
|
147
|
+
CHECK_READ_PERMS --> PERMS_OK_READ{Permitted?}
|
|
148
|
+
PERMS_OK_READ --> |Yes| EXEC_READ[Execute file operation]
|
|
149
|
+
PERMS_OK_READ --> |No| PERM_ERROR[Permission denied error]
|
|
150
|
+
|
|
151
|
+
CHECK_WRITE_PERMS --> PERMS_OK_WRITE{Permitted?}
|
|
152
|
+
PERMS_OK_WRITE --> |Yes| EXEC_WRITE[Execute file operation]
|
|
153
|
+
PERMS_OK_WRITE --> |No| PERM_ERROR
|
|
154
|
+
|
|
155
|
+
EXEC_READ --> TOOL_RESULT
|
|
156
|
+
EXEC_WRITE --> TOOL_RESULT
|
|
157
|
+
PERM_ERROR --> TOOL_RESULT
|
|
158
|
+
|
|
159
|
+
%% Bash operations
|
|
160
|
+
BASH_OP --> CHECK_BASH_PERMS[Check denied commands]
|
|
161
|
+
CHECK_BASH_PERMS --> BASH_OK{Permitted?}
|
|
162
|
+
BASH_OK --> |Yes| RUN_BASH[Execute shell command]
|
|
163
|
+
BASH_OK --> |No| BASH_ERROR[Permission denied]
|
|
164
|
+
RUN_BASH --> TOOL_RESULT
|
|
165
|
+
BASH_ERROR --> TOOL_RESULT
|
|
166
|
+
|
|
167
|
+
%% Default operations
|
|
168
|
+
DEFAULT_OP{Tool type?}
|
|
169
|
+
DEFAULT_OP --> |Think| THINK_EXEC[Record reasoning]
|
|
170
|
+
DEFAULT_OP --> |TodoWrite| TODO_EXEC[Update task list]
|
|
171
|
+
DEFAULT_OP --> |Clock| CLOCK_EXEC[Return current time]
|
|
172
|
+
DEFAULT_OP --> |WebFetch| WEB_EXEC[Fetch and process URL]
|
|
173
|
+
|
|
174
|
+
THINK_EXEC --> TOOL_RESULT
|
|
175
|
+
TODO_EXEC --> TOOL_RESULT
|
|
176
|
+
CLOCK_EXEC --> TOOL_RESULT
|
|
177
|
+
WEB_EXEC --> TOOL_RESULT
|
|
178
|
+
|
|
179
|
+
%% Scratchpad operations
|
|
180
|
+
SCRATCHPAD_OP{Operation?}
|
|
181
|
+
SCRATCHPAD_OP --> |Write| SCRATCH_WRITE[Store in volatile memory]
|
|
182
|
+
SCRATCHPAD_OP --> |Read| SCRATCH_READ[Retrieve from memory]
|
|
183
|
+
SCRATCHPAD_OP --> |List| SCRATCH_LIST[List all entries]
|
|
184
|
+
|
|
185
|
+
SCRATCH_WRITE --> TOOL_RESULT
|
|
186
|
+
SCRATCH_READ --> TOOL_RESULT
|
|
187
|
+
SCRATCH_LIST --> TOOL_RESULT
|
|
188
|
+
|
|
189
|
+
%% Tool result handling
|
|
190
|
+
TOOL_RESULT[Tool result collected]
|
|
191
|
+
TOOL_RESULT --> HOOK_POST[Trigger post_tool_use hook]
|
|
192
|
+
HOOK_POST --> HOOK_POST_RESULT{Hook result?}
|
|
193
|
+
|
|
194
|
+
HOOK_POST_RESULT --> |halt| STOP_EXECUTION
|
|
195
|
+
HOOK_POST_RESULT --> |replace| MODIFY_RESULT[Modify tool result]
|
|
196
|
+
HOOK_POST_RESULT --> |continue| MORE_TOOLS
|
|
197
|
+
MODIFY_RESULT --> MORE_TOOLS
|
|
198
|
+
|
|
199
|
+
MORE_TOOLS{More tools?} --> |Yes| TOOL_LOOP
|
|
200
|
+
MORE_TOOLS --> |No| SEND_RESULTS[Send all results to LLM]
|
|
201
|
+
|
|
202
|
+
SEND_RESULTS --> LLM_CONTINUES{LLM continues?}
|
|
203
|
+
LLM_CONTINUES --> |More tool calls| EMIT_STEP
|
|
204
|
+
LLM_CONTINUES --> |Final response| FINAL_RESPONSE
|
|
205
|
+
|
|
206
|
+
FINAL_RESPONSE[LLM returns final text response]
|
|
207
|
+
FINAL_RESPONSE --> EMIT_AGENT_STOP[Emit agent_stop event]
|
|
208
|
+
EMIT_AGENT_STOP --> HOOK_SWARM_STOP[Trigger swarm_stop hooks]
|
|
209
|
+
|
|
210
|
+
HOOK_SWARM_STOP --> HOOK_STOP_RESULT{Hook result?}
|
|
211
|
+
HOOK_STOP_RESULT --> |reprompt| MODIFY_REPROMPT[Modify prompt]
|
|
212
|
+
HOOK_STOP_RESULT --> |finish_swarm| BUILD_RESULT
|
|
213
|
+
HOOK_STOP_RESULT --> |continue| BUILD_RESULT
|
|
214
|
+
|
|
215
|
+
MODIFY_REPROMPT --> EXEC_LOOP
|
|
216
|
+
|
|
217
|
+
BUILD_RESULT[Build Result object]
|
|
218
|
+
BUILD_RESULT --> CALC_COST[Calculate total cost & tokens from logs]
|
|
219
|
+
CALC_COST --> CLEANUP[Cleanup MCP clients]
|
|
220
|
+
CLEANUP --> RESET_LOGGING{Logging was enabled?}
|
|
221
|
+
|
|
222
|
+
RESET_LOGGING --> |Yes| RESET_STREAMS[Reset LogStream & LogCollector]
|
|
223
|
+
RESET_LOGGING --> |No| RETURN_RESULT
|
|
224
|
+
RESET_STREAMS --> RETURN_RESULT
|
|
225
|
+
|
|
226
|
+
RETURN_RESULT[Return Result to user]
|
|
227
|
+
RETURN_RESULT --> FORMAT{CLI or SDK?}
|
|
228
|
+
|
|
229
|
+
FORMAT --> |CLI| RENDER_OUTPUT[Render formatted output]
|
|
230
|
+
FORMAT --> |SDK| DIRECT_RETURN[Return Result object]
|
|
231
|
+
|
|
232
|
+
RENDER_OUTPUT --> DISPLAY[Display to terminal]
|
|
233
|
+
DIRECT_RETURN --> CODE[Return to calling code]
|
|
234
|
+
|
|
235
|
+
DISPLAY --> END_SUCCESS([Execution complete])
|
|
236
|
+
CODE --> END_SUCCESS
|
|
237
|
+
STOP_EXECUTION --> END_ERROR
|
|
238
|
+
|
|
239
|
+
%% Styling
|
|
240
|
+
classDef userAction fill:#e1f5ff,stroke:#0366d6,stroke-width:3px
|
|
241
|
+
classDef config fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
|
|
242
|
+
classDef initialization fill:#fff3e0,stroke:#ff9800,stroke-width:2px
|
|
243
|
+
classDef hooks fill:#fce4ec,stroke:#e91e63,stroke-width:2px
|
|
244
|
+
classDef llm fill:#f3e5f5,stroke:#9c27b0,stroke-width:2px
|
|
245
|
+
classDef tools fill:#e0f2f1,stroke:#009688,stroke-width:2px
|
|
246
|
+
classDef memory fill:#fff3e0,stroke:#ff6f00,stroke-width:2px
|
|
247
|
+
classDef result fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px
|
|
248
|
+
classDef decision fill:#fff9c4,stroke:#fbc02d,stroke-width:2px
|
|
249
|
+
|
|
250
|
+
class START,END_SUCCESS,END_ERROR userAction
|
|
251
|
+
class LOAD_CONFIG,PARSE_CONFIG,VALIDATE_CONFIG config
|
|
252
|
+
class CREATE_SWARM,STORE_AGENTS,SETUP_HOOKS,READY,INIT_AGENTS,PASS1,CREATE_TOOLS,REGISTER_MCP,PLUGIN_INIT,PASS2,PASS3,PASS4,PASS5,EMIT_AGENT_START initialization
|
|
253
|
+
class HOOK_SWARM_START,HOOK_FIRST,HOOK_USER_PROMPT,HOOK_PRE,HOOK_POST,HOOK_PRE_DELEGATE,HOOK_POST_DELEGATE,HOOK_SWARM_STOP,HOOK_START_RESULT,HOOK_PRE_RESULT,HOOK_POST_RESULT,HOOK_STOP_RESULT hooks
|
|
254
|
+
class SEND_LLM,LLM_RESPONSE,SEND_RESULTS,LLM_CONTINUES,FINAL_RESPONSE,RATE_LIMIT llm
|
|
255
|
+
class TOOL_LOOP,CHECK_TOOL_TYPE,FILE_OP,BASH_OP,DEFAULT_OP,SCRATCHPAD_OP,TOOL_RESULT,DELEGATE_START,RECURSIVE_ASK,EXEC_READ,EXEC_WRITE,RUN_BASH,THINK_EXEC,TODO_EXEC,CLOCK_EXEC,WEB_EXEC,SCRATCH_WRITE,SCRATCH_READ,SCRATCH_LIST,CHECK_READ_PERMS,CHECK_WRITE_PERMS,CHECK_BASH_PERMS tools
|
|
256
|
+
class MEMORY_DISCOVER,SKILL_DISCOVERY,LOAD_SKILLS,CREATE_STORAGE,MEMORY_STORAGE,REGISTER_MEMORY_TOOLS,MEMORY_OP,EXTRACT_META,GENERATE_EMBED,UPDATE_INDEX,PERSIST,FETCH_ENTRY,SEMANTIC_SEARCH,EMBED_QUERY,FAISS_SEARCH,RANK_RESULTS,EDIT_ENTRY,LOAD_SKILL_EXEC,SWAP_TOOLS,FOLLOW_STUB,REDIRECT,RETURN_CONTENT memory
|
|
257
|
+
class BUILD_RESULT,CALC_COST,CLEANUP,RETURN_RESULT,RENDER_OUTPUT,DISPLAY,DIRECT_RETURN,CODE result
|
|
258
|
+
class LOAD,VALIDATE_CONFIG,SETUP_LOGGING,HOOK_START_RESULT,CHECK_FIRST,INIT_CHECK,CREATE_STORAGE,LLM_RESPONSE,HOOK_PRE_RESULT,CHECK_TOOL_TYPE,MEMORY_OP,FILE_OP,BASH_OP,DEFAULT_OP,SCRATCHPAD_OP,HOOK_POST_RESULT,MORE_TOOLS,LLM_CONTINUES,HOOK_STOP_RESULT,RESET_LOGGING,FORMAT,PERMS_OK_READ,PERMS_OK_WRITE,BASH_OK,CHECK_SKILLS,FOLLOW_STUB decision
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Step-by-Step Execution
|
|
262
|
+
|
|
263
|
+
### Phase 1: Configuration & Initialization
|
|
264
|
+
|
|
265
|
+
1. **User Input**
|
|
266
|
+
- CLI: `swarm run config.yml -p "Build authentication"`
|
|
267
|
+
- SDK: `swarm.execute("Build authentication")`
|
|
268
|
+
|
|
269
|
+
2. **Load Configuration**
|
|
270
|
+
- Parse YAML file or Ruby DSL
|
|
271
|
+
- Resolve agent file references
|
|
272
|
+
- Validate configuration structure
|
|
273
|
+
- Return errors if invalid
|
|
274
|
+
|
|
275
|
+
3. **Create Swarm**
|
|
276
|
+
- Create Swarm instance with name and settings
|
|
277
|
+
- Store agent definitions (not yet initialized)
|
|
278
|
+
- Setup hook registry with default logging hooks
|
|
279
|
+
- Apply YAML hooks to registry if present
|
|
280
|
+
|
|
281
|
+
### Phase 2: Execution Start
|
|
282
|
+
|
|
283
|
+
4. **Execute Called**
|
|
284
|
+
- `swarm.execute("prompt")` is called
|
|
285
|
+
- Setup logging if callback block provided
|
|
286
|
+
- Record start time for duration tracking
|
|
287
|
+
|
|
288
|
+
5. **Swarm Start Hooks**
|
|
289
|
+
- Trigger `swarm_start` hooks
|
|
290
|
+
- Can halt execution or append context to prompt
|
|
291
|
+
- Default hook emits `swarm_start` event to logs
|
|
292
|
+
|
|
293
|
+
6. **First Message Hooks** (first execution only)
|
|
294
|
+
- Trigger `first_message` hooks
|
|
295
|
+
- Can halt before any LLM interaction
|
|
296
|
+
|
|
297
|
+
### Phase 3: Agent Initialization (Lazy, First Execution Only)
|
|
298
|
+
|
|
299
|
+
7. **5-Pass Initialization**
|
|
300
|
+
|
|
301
|
+
**Pass 1: Create Agents**
|
|
302
|
+
- Create `Agent::Chat` instance for each agent
|
|
303
|
+
- Register explicit tools (from config)
|
|
304
|
+
- Register default tools (Read, Grep, Glob, Think, TodoWrite, etc.)
|
|
305
|
+
- Wrap tools with permissions validators
|
|
306
|
+
- Connect to MCP servers for external tools
|
|
307
|
+
- Initialize plugins (create memory storage if enabled)
|
|
308
|
+
- Register plugin tools (memory tools if memory enabled)
|
|
309
|
+
|
|
310
|
+
**Pass 2: Delegation Tools**
|
|
311
|
+
- Create delegation tools for inter-agent communication
|
|
312
|
+
- Each delegation tool wraps target agent's `ask()` method
|
|
313
|
+
|
|
314
|
+
**Pass 3: Agent Contexts**
|
|
315
|
+
- Create `Agent::Context` for tracking delegations
|
|
316
|
+
- Setup logging callbacks if logging enabled
|
|
317
|
+
- Emit validation warnings for model mismatches
|
|
318
|
+
|
|
319
|
+
**Pass 4: Hook System**
|
|
320
|
+
- Configure hook callbacks for each agent
|
|
321
|
+
- Link to swarm's hook registry
|
|
322
|
+
|
|
323
|
+
**Pass 5: YAML Hooks**
|
|
324
|
+
- Apply YAML shell command hooks if present
|
|
325
|
+
- Convert to Ruby hook callbacks
|
|
326
|
+
|
|
327
|
+
**Emit agent_start events**
|
|
328
|
+
|
|
329
|
+
### Phase 4: Lead Agent Execution
|
|
330
|
+
|
|
331
|
+
8. **Send Prompt to Lead Agent**
|
|
332
|
+
- Lead agent receives the user's prompt
|
|
333
|
+
- Enters Async reactor for parallel execution
|
|
334
|
+
|
|
335
|
+
9. **User Prompt Hooks**
|
|
336
|
+
- Trigger `user_prompt` hooks
|
|
337
|
+
- Can modify or validate the prompt
|
|
338
|
+
- Default hook emits `user_prompt` event
|
|
339
|
+
|
|
340
|
+
10. **Memory Semantic Skill Discovery** (if memory enabled)
|
|
341
|
+
- Search memory for skills matching prompt
|
|
342
|
+
- Use semantic search (embeddings + FAISS)
|
|
343
|
+
- Auto-load matching skills as tools (dynamic tool swapping)
|
|
344
|
+
|
|
345
|
+
11. **Rate Limiting**
|
|
346
|
+
- Acquire global semaphore (max concurrent LLM calls across swarm)
|
|
347
|
+
- Prevents API quota exhaustion in large swarms
|
|
348
|
+
|
|
349
|
+
12. **Send to LLM**
|
|
350
|
+
- Send messages + tools to configured LLM API
|
|
351
|
+
- Wait for response (streaming or blocking)
|
|
352
|
+
|
|
353
|
+
### Phase 5: Tool Execution Loop
|
|
354
|
+
|
|
355
|
+
13. **LLM Response**
|
|
356
|
+
- **Text only**: Go to final response
|
|
357
|
+
- **Tool calls**: Emit `agent_step` event and process tools
|
|
358
|
+
|
|
359
|
+
14. **For Each Tool Call** (parallel execution)
|
|
360
|
+
|
|
361
|
+
**Pre-Tool Hook**
|
|
362
|
+
- Trigger `pre_tool_use` hook with matcher pattern
|
|
363
|
+
- Can halt, modify parameters, or continue
|
|
364
|
+
|
|
365
|
+
**Acquire Local Semaphore**
|
|
366
|
+
- Limit concurrent tool calls for this agent
|
|
367
|
+
- Prevents overwhelming single agent
|
|
368
|
+
|
|
369
|
+
**Check Permissions**
|
|
370
|
+
- File tools: Validate allowed/denied paths
|
|
371
|
+
- Bash: Validate denied command patterns
|
|
372
|
+
- Block if permissions deny
|
|
373
|
+
|
|
374
|
+
**Execute Tool** (depends on type):
|
|
375
|
+
|
|
376
|
+
- **Delegation Tool**:
|
|
377
|
+
- Trigger `pre_delegation` hook
|
|
378
|
+
- Recursively call target agent's `ask()` method
|
|
379
|
+
- Trigger `post_delegation` hook
|
|
380
|
+
- Return delegate's response
|
|
381
|
+
|
|
382
|
+
- **Memory Tools**:
|
|
383
|
+
- **MemoryWrite**: Extract metadata → Generate embedding → Update FAISS index → Persist
|
|
384
|
+
- **MemoryRead**: Fetch from storage → Follow redirects if stub → Return content
|
|
385
|
+
- **MemoryGrep**: Embed query → Search FAISS → Rank by similarity → Return matches
|
|
386
|
+
- **MemoryEdit**: Update existing entry → Re-index if needed
|
|
387
|
+
- **LoadSkill**: Search for skill → Load into memory → Swap tools dynamically
|
|
388
|
+
|
|
389
|
+
- **File Tools** (Read/Write/Edit/Glob/Grep):
|
|
390
|
+
- Resolve paths relative to agent's directory
|
|
391
|
+
- Execute file operation
|
|
392
|
+
- Return content/results
|
|
393
|
+
|
|
394
|
+
- **Bash Tool**:
|
|
395
|
+
- Execute shell command in agent's directory
|
|
396
|
+
- Capture stdout/stderr
|
|
397
|
+
- Return output
|
|
398
|
+
|
|
399
|
+
- **Default Tools**:
|
|
400
|
+
- **Think**: Record reasoning (creates attention sink)
|
|
401
|
+
- **TodoWrite**: Update task list state
|
|
402
|
+
- **Clock**: Return current timestamp
|
|
403
|
+
- **WebFetch**: Fetch URL → Convert to markdown → Process with LLM → Return
|
|
404
|
+
|
|
405
|
+
- **Scratchpad Tools**:
|
|
406
|
+
- **ScratchpadWrite**: Store in volatile shared memory
|
|
407
|
+
- **ScratchpadRead**: Retrieve from shared memory
|
|
408
|
+
- **ScratchpadList**: List all entries
|
|
409
|
+
|
|
410
|
+
**Post-Tool Hook**
|
|
411
|
+
- Trigger `post_tool_use` hook
|
|
412
|
+
- Can halt, modify result, or continue
|
|
413
|
+
- Emit `tool_result` event
|
|
414
|
+
|
|
415
|
+
15. **More Tools?**
|
|
416
|
+
- If more tool calls: Continue parallel execution
|
|
417
|
+
- If all done: Send results back to LLM
|
|
418
|
+
|
|
419
|
+
16. **LLM Continues**
|
|
420
|
+
- LLM processes tool results
|
|
421
|
+
- May request more tools (loop back to step 13)
|
|
422
|
+
- Or return final text response
|
|
423
|
+
|
|
424
|
+
### Phase 6: Response Completion
|
|
425
|
+
|
|
426
|
+
17. **Final Response**
|
|
427
|
+
- LLM returns text response (no more tool calls)
|
|
428
|
+
- Emit `agent_stop` event with usage stats
|
|
429
|
+
|
|
430
|
+
18. **Swarm Stop Hooks**
|
|
431
|
+
- Trigger `swarm_stop` hooks
|
|
432
|
+
- Can request reprompt (loop back to step 8 with new prompt)
|
|
433
|
+
- Can finish swarm early
|
|
434
|
+
- Default hook emits `swarm_stop` event with summary
|
|
435
|
+
|
|
436
|
+
19. **Build Result**
|
|
437
|
+
- Create `Result` object with response content
|
|
438
|
+
- Calculate total cost from usage logs
|
|
439
|
+
- Calculate total tokens from usage logs
|
|
440
|
+
- Collect all logs from execution
|
|
441
|
+
- Record total duration
|
|
442
|
+
|
|
443
|
+
20. **Cleanup**
|
|
444
|
+
- Stop all MCP client connections
|
|
445
|
+
- Reset logging streams if logging was enabled
|
|
446
|
+
- Release semaphores
|
|
447
|
+
|
|
448
|
+
21. **Return Result**
|
|
449
|
+
- CLI: Format output (Markdown, JSON, or quiet mode) → Display
|
|
450
|
+
- SDK: Return Result object directly to calling code
|
|
451
|
+
|
|
452
|
+
### Node Workflow Variation
|
|
453
|
+
|
|
454
|
+
If the swarm uses **Node Workflows** (multi-stage execution):
|
|
455
|
+
|
|
456
|
+
1. Build execution order from node dependencies (topological sort)
|
|
457
|
+
2. For each node in order:
|
|
458
|
+
- Apply input transformer (Bash/Ruby) to previous node's output
|
|
459
|
+
- Create mini-swarm with node's agents
|
|
460
|
+
- Execute mini-swarm with transformed input
|
|
461
|
+
- NodeContext can:
|
|
462
|
+
- `goto_node(name)`: Jump to different node
|
|
463
|
+
- `halt_workflow()`: Stop entire workflow
|
|
464
|
+
- `skip_execution()`: Skip LLM and use provided content
|
|
465
|
+
- Collect node result
|
|
466
|
+
3. Pass node output to dependent nodes
|
|
467
|
+
4. Return final node's result
|
|
468
|
+
|
|
469
|
+
## Parallel Execution
|
|
470
|
+
|
|
471
|
+
Multiple operations happen **concurrently**:
|
|
472
|
+
|
|
473
|
+
- **Tool calls**: Execute in parallel within semaphore limits
|
|
474
|
+
- **LLM requests**: Multiple agents can call LLMs simultaneously (global semaphore)
|
|
475
|
+
- **Delegation**: Recursive agent calls run independently
|
|
476
|
+
- **File I/O**: Non-blocking with Async fiber scheduler
|
|
477
|
+
|
|
478
|
+
## Key Decision Points
|
|
479
|
+
|
|
480
|
+
1. **Configuration valid?** → Continue or return errors
|
|
481
|
+
2. **Logging enabled?** → Setup LogStream or skip
|
|
482
|
+
3. **First message?** → Trigger first_message hooks or skip
|
|
483
|
+
4. **Agents initialized?** → Run 5-pass init or skip
|
|
484
|
+
5. **Memory enabled?** → Skill discovery or skip
|
|
485
|
+
6. **Hook results** → Halt, modify, or continue
|
|
486
|
+
7. **Tool type?** → Route to appropriate handler
|
|
487
|
+
8. **Permissions ok?** → Execute or deny
|
|
488
|
+
9. **More tools?** → Continue loop or send to LLM
|
|
489
|
+
10. **LLM continues?** → More tools or final response
|
|
490
|
+
11. **Swarm stop hook?** → Reprompt, finish, or continue
|
|
491
|
+
|
|
492
|
+
## Event Timeline
|
|
493
|
+
|
|
494
|
+
```
|
|
495
|
+
Time →
|
|
496
|
+
|
|
497
|
+
[User] ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━→ [Result]
|
|
498
|
+
│ │
|
|
499
|
+
├─ swarm_start event │
|
|
500
|
+
│ │
|
|
501
|
+
├─ first_message event (if first time) │
|
|
502
|
+
│ │
|
|
503
|
+
├─ agent_start events (all agents) │
|
|
504
|
+
│ │
|
|
505
|
+
├─ user_prompt event │
|
|
506
|
+
│ │
|
|
507
|
+
├─ tool_call events ┐ │
|
|
508
|
+
├─ tool_result events┘ (repeated) │
|
|
509
|
+
│ │
|
|
510
|
+
├─ agent_step events (each LLM turn) │
|
|
511
|
+
│ │
|
|
512
|
+
├─ agent_stop event (final response) │
|
|
513
|
+
│ │
|
|
514
|
+
└─ swarm_stop event ─────────────────────────────────┘
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
## Memory Operation Details
|
|
518
|
+
|
|
519
|
+
### MemoryWrite Flow
|
|
520
|
+
```
|
|
521
|
+
Content → Extract frontmatter → Generate embedding (ONNX) →
|
|
522
|
+
Update FAISS index → Persist to JSON → Return confirmation
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
### MemoryGrep/Semantic Search Flow
|
|
526
|
+
```
|
|
527
|
+
Query → Embed query (ONNX) → Search FAISS index →
|
|
528
|
+
Calculate cosine similarity → Rank results →
|
|
529
|
+
Filter by threshold → Return top matches
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
### LoadSkill Flow
|
|
533
|
+
```
|
|
534
|
+
Skill name → Semantic search + keyword match →
|
|
535
|
+
Load skill content → Parse tool definitions →
|
|
536
|
+
Register new tools → Remove old tools (except immutable) →
|
|
537
|
+
Set active skill → Return confirmation
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
## Error Handling
|
|
541
|
+
|
|
542
|
+
At any point, errors can occur:
|
|
543
|
+
|
|
544
|
+
- **Configuration errors**: Stop before execution, return structured errors
|
|
545
|
+
- **Hook halt**: Stop execution immediately, return hook message
|
|
546
|
+
- **Permission denied**: Return error to LLM, continues execution
|
|
547
|
+
- **Tool errors**: Return error to LLM, continues execution
|
|
548
|
+
- **LLM errors**: Build Result with error, trigger swarm_stop, return to user
|
|
549
|
+
- **MCP errors**: Log warning, continue without external tools
|
|
550
|
+
|
|
551
|
+
## Concurrent Execution Example
|
|
552
|
+
|
|
553
|
+
When lead agent delegates to 3 agents simultaneously:
|
|
554
|
+
|
|
555
|
+
```
|
|
556
|
+
Lead Agent sends prompt
|
|
557
|
+
│
|
|
558
|
+
├─ Acquires global semaphore (1/50)
|
|
559
|
+
└─ Sends to LLM
|
|
560
|
+
│
|
|
561
|
+
└─ LLM returns 3 delegation tool calls
|
|
562
|
+
│
|
|
563
|
+
├─────────┬─────────┬─────────┐
|
|
564
|
+
│ │ │ │
|
|
565
|
+
▼ ▼ ▼ ▼
|
|
566
|
+
Tool 1 Tool 2 Tool 3 (parallel)
|
|
567
|
+
│ │ │
|
|
568
|
+
▼ ▼ ▼
|
|
569
|
+
Agent A Agent B Agent C
|
|
570
|
+
│ │ │
|
|
571
|
+
├─────────┼─────────┤
|
|
572
|
+
│ Each acquires global semaphore (4/50 total)
|
|
573
|
+
├─────────┼─────────┤
|
|
574
|
+
│ │ │
|
|
575
|
+
▼ ▼ ▼
|
|
576
|
+
Results collected (parallel)
|
|
577
|
+
│
|
|
578
|
+
└──────── Back to Lead Agent LLM
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
## Reprompting Flow
|
|
582
|
+
|
|
583
|
+
Swarm stop hooks can request reprompting:
|
|
584
|
+
|
|
585
|
+
```
|
|
586
|
+
swarm_stop hook returns reprompt("Try again with more detail")
|
|
587
|
+
│
|
|
588
|
+
└─ Loop back to step 8 (Lead agent execution)
|
|
589
|
+
│
|
|
590
|
+
└─ Lead agent receives new prompt
|
|
591
|
+
│
|
|
592
|
+
└─ Execution continues...
|
|
593
|
+
│
|
|
594
|
+
└─ Eventually reaches swarm_stop again
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
This enables:
|
|
598
|
+
- Validation loops (hook validates output, requests retry)
|
|
599
|
+
- Iterative refinement (hook checks quality, asks for improvements)
|
|
600
|
+
- Multi-turn workflows (hook orchestrates conversation)
|