swarm_sdk 2.7.12 → 2.7.13
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/lib/swarm_sdk/agent/chat.rb +13 -6
- data/lib/swarm_sdk/agent/llm_instrumentation_middleware.rb +1 -0
- data/lib/swarm_sdk/ruby_llm_patches/configuration_patch.rb +14 -1
- data/lib/swarm_sdk/ruby_llm_patches/tool_concurrency_patch.rb +8 -4
- data/lib/swarm_sdk/swarm.rb +0 -4
- data/lib/swarm_sdk/tools/delegate.rb +92 -47
- data/lib/swarm_sdk/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 427d736e32c386d76df7481b50b95e272045b88eb93290353528fd48c2525d91
|
|
4
|
+
data.tar.gz: fc3cf69eaa253871f0db9f6332dd62669af2941082f622e266f65e035f6b2b21
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d08958fe437094ce91bdae63e6e20290158417270d1d39446fbb9cc45b2ea02bfbbae99dccfb36097fb053a2190f4cee1fd101c876eda1a388c19ba3ba4cdf4c
|
|
7
|
+
data.tar.gz: cf06e09fe354ca95035d27e5ee4d710be461757829c1448c5d178b9f8a9b9372419e27bc036101d9ad0add625459784fa766413695f3f030efcba8fa97ee8467
|
data/lib/swarm_sdk/agent/chat.rb
CHANGED
|
@@ -526,17 +526,24 @@ module SwarmSDK
|
|
|
526
526
|
#
|
|
527
527
|
# This method:
|
|
528
528
|
# 1. Serializes concurrent asks via @ask_semaphore
|
|
529
|
-
# 2.
|
|
530
|
-
# 3.
|
|
531
|
-
# 4.
|
|
532
|
-
# 5.
|
|
533
|
-
# 6.
|
|
529
|
+
# 2. Optionally clears conversation context (inside semaphore for safety)
|
|
530
|
+
# 3. Adds CLEAN user message to history (no reminders)
|
|
531
|
+
# 4. Injects system reminders as ephemeral content (sent to LLM but not stored)
|
|
532
|
+
# 5. Triggers user_prompt hooks
|
|
533
|
+
# 6. Acquires global semaphore for LLM call
|
|
534
|
+
# 7. Delegates to RubyLLM::Chat for actual execution
|
|
534
535
|
#
|
|
535
536
|
# @param prompt [String] User prompt
|
|
537
|
+
# @param clear_context [Boolean] When true, clears conversation history before
|
|
538
|
+
# processing. Clearing happens inside the ask_semaphore, making it safe for
|
|
539
|
+
# concurrent callers (e.g., parallel delegations to the same agent).
|
|
536
540
|
# @param options [Hash] Additional options (source: for hooks)
|
|
537
541
|
# @return [RubyLLM::Message] LLM response
|
|
538
|
-
def ask(prompt, **options)
|
|
542
|
+
def ask(prompt, clear_context: false, **options)
|
|
539
543
|
@ask_semaphore.acquire do
|
|
544
|
+
# Clear inside semaphore so concurrent callers don't corrupt each other's messages
|
|
545
|
+
clear_conversation if clear_context
|
|
546
|
+
|
|
540
547
|
if @turn_timeout
|
|
541
548
|
execute_with_turn_timeout(prompt, options)
|
|
542
549
|
else
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
# Extends RubyLLM::Configuration with additional options:
|
|
4
4
|
# - anthropic_api_base: Configurable Anthropic API base URL
|
|
5
5
|
# - read_timeout, open_timeout, write_timeout: Granular timeout configuration
|
|
6
|
+
# - Fixes Anthropic completion_url leading slash that breaks proxy base URLs
|
|
6
7
|
#
|
|
7
8
|
# Fork Reference: Commits da6144b, 3daa4fb
|
|
8
9
|
|
|
@@ -29,13 +30,25 @@ module RubyLLM
|
|
|
29
30
|
end
|
|
30
31
|
end
|
|
31
32
|
|
|
32
|
-
# Patch Anthropic provider to use configurable base URL
|
|
33
|
+
# Patch Anthropic provider to use configurable base URL and fix completion_url
|
|
33
34
|
module Providers
|
|
34
35
|
class Anthropic
|
|
35
36
|
# Override api_base to use configurable base URL
|
|
36
37
|
def api_base
|
|
37
38
|
@config.anthropic_api_base || "https://api.anthropic.com"
|
|
38
39
|
end
|
|
40
|
+
|
|
41
|
+
# Fix completion_url to use relative path (no leading slash).
|
|
42
|
+
# The leading slash causes Faraday to discard the base URL path component,
|
|
43
|
+
# breaking proxy configurations where api_base includes a path segment
|
|
44
|
+
# (e.g., https://proxy.dev/apis/anthropic/v1/messages → https://proxy.dev/v1/messages).
|
|
45
|
+
# stream_url delegates to completion_url, so this fixes both sync and streaming.
|
|
46
|
+
# Can be removed once RubyLLM releases a version including upstream fix (commit da6144b).
|
|
47
|
+
module Chat
|
|
48
|
+
def completion_url
|
|
49
|
+
"v1/messages"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
39
52
|
end
|
|
40
53
|
end
|
|
41
54
|
end
|
|
@@ -88,11 +88,15 @@ module RubyLLM
|
|
|
88
88
|
"Add `gem 'async'` to your Gemfile. Original error: #{e.message}"
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
-
def run_with_sync(&)
|
|
92
|
-
if
|
|
93
|
-
|
|
91
|
+
def run_with_sync(&block)
|
|
92
|
+
if Async::Task.current?
|
|
93
|
+
# Already inside an async reactor (SwarmSDK always runs in one).
|
|
94
|
+
# Just yield — no Sync, no nested reactor, no Promise mutex issues.
|
|
95
|
+
yield
|
|
94
96
|
else
|
|
95
|
-
|
|
97
|
+
# Outside async context (e.g., standalone RubyLLM usage).
|
|
98
|
+
# Sync handles reactor creation and cleanup.
|
|
99
|
+
Sync(&block)
|
|
96
100
|
end
|
|
97
101
|
end
|
|
98
102
|
|
data/lib/swarm_sdk/swarm.rb
CHANGED
|
@@ -73,7 +73,6 @@ module SwarmSDK
|
|
|
73
73
|
DEFAULT_TOOLS = ToolConfigurator::DEFAULT_TOOLS
|
|
74
74
|
|
|
75
75
|
attr_reader :name, :agents, :lead_agent, :mcp_clients, :delegation_instances, :agent_definitions, :swarm_id, :parent_swarm_id, :swarm_registry, :scratchpad_storage, :allow_filesystem_tools, :hook_registry, :global_semaphore, :plugin_storages, :config_for_hooks, :observer_configs, :execution_timeout
|
|
76
|
-
attr_accessor :delegation_call_stack
|
|
77
76
|
|
|
78
77
|
# Check if scratchpad tools are enabled
|
|
79
78
|
#
|
|
@@ -174,9 +173,6 @@ module SwarmSDK
|
|
|
174
173
|
# Swarm registry for managing sub-swarms (initialized later if needed)
|
|
175
174
|
@swarm_registry = nil
|
|
176
175
|
|
|
177
|
-
# Delegation call stack for circular dependency detection
|
|
178
|
-
@delegation_call_stack = []
|
|
179
|
-
|
|
180
176
|
# Shared semaphore for all agents
|
|
181
177
|
@global_semaphore = Async::Semaphore.new(@global_concurrency)
|
|
182
178
|
|
|
@@ -45,7 +45,7 @@ module SwarmSDK
|
|
|
45
45
|
# @param delegate_description [String] Description of the delegate agent
|
|
46
46
|
# @param delegate_chat [AgentChat, nil] The chat instance for the delegate agent (nil if delegating to swarm)
|
|
47
47
|
# @param agent_name [Symbol, String] Name of the agent using this tool
|
|
48
|
-
# @param swarm [Swarm] The swarm instance (provides hook_registry,
|
|
48
|
+
# @param swarm [Swarm] The swarm instance (provides hook_registry, swarm_registry)
|
|
49
49
|
# @param delegating_chat [Agent::Chat, nil] The chat instance of the agent doing the delegating (for accessing hooks)
|
|
50
50
|
# @param custom_tool_name [String, nil] Optional custom tool name (overrides auto-generated name)
|
|
51
51
|
# @param preserve_context [Boolean] Whether to preserve conversation context between delegations (default: true)
|
|
@@ -72,6 +72,16 @@ module SwarmSDK
|
|
|
72
72
|
# Use custom tool name if provided, otherwise generate using canonical method
|
|
73
73
|
@tool_name = custom_tool_name || self.class.tool_name_for(delegate_name)
|
|
74
74
|
@delegate_target = delegate_name.to_s
|
|
75
|
+
|
|
76
|
+
# Track concurrent delegations to this target.
|
|
77
|
+
# When multiple parallel tool calls target the same delegate, only the first
|
|
78
|
+
# preserves context; subsequent concurrent calls always clear context to
|
|
79
|
+
# prevent cross-contamination between independent parallel work.
|
|
80
|
+
#
|
|
81
|
+
# No Mutex needed: Async Fibers run on a single thread and only switch at
|
|
82
|
+
# explicit yield points (IO, sleep, semaphore.acquire). Integer increment
|
|
83
|
+
# and decrement never yield, so they are inherently atomic.
|
|
84
|
+
@active_count = 0
|
|
75
85
|
end
|
|
76
86
|
|
|
77
87
|
# Override description to return dynamic string based on delegate
|
|
@@ -122,19 +132,32 @@ module SwarmSDK
|
|
|
122
132
|
|
|
123
133
|
# Execute delegation with pre/post hooks
|
|
124
134
|
#
|
|
135
|
+
# Uses Fiber-local path tracking for circular dependency detection.
|
|
136
|
+
# Each concurrent delegation runs in its own Fiber (via Async), so the path
|
|
137
|
+
# is isolated per execution path. This correctly distinguishes parallel fan-out
|
|
138
|
+
# (A→B, A→B) from true circular dependencies (A→B→A).
|
|
139
|
+
#
|
|
125
140
|
# @param message [String] Message to send to the agent
|
|
126
141
|
# @param reset_context [Boolean] Whether to reset the agent's conversation history before delegation
|
|
127
142
|
# @return [String] Result from delegate agent or error message
|
|
128
143
|
def execute(message:, reset_context: false)
|
|
144
|
+
# Save the current delegation path so we can restore it after execution.
|
|
145
|
+
# The extended path (with our target) is only needed during chat.ask() so
|
|
146
|
+
# child Fibers (nested delegations) inherit it. After delegation returns,
|
|
147
|
+
# this Fiber's path should be unchanged.
|
|
148
|
+
saved_delegation_path = Fiber[:delegation_path]
|
|
149
|
+
|
|
129
150
|
# Access swarm infrastructure
|
|
130
|
-
call_stack = @swarm.delegation_call_stack
|
|
131
151
|
hook_registry = @swarm.hook_registry
|
|
132
152
|
swarm_registry = @swarm.swarm_registry
|
|
133
153
|
|
|
134
|
-
# Check for circular dependency
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
154
|
+
# Check for circular dependency using Fiber-local path
|
|
155
|
+
# Each Fiber inherits the parent's path, so nested delegations
|
|
156
|
+
# accumulate the full chain while parallel siblings remain isolated
|
|
157
|
+
delegation_path = saved_delegation_path || []
|
|
158
|
+
if delegation_path.include?(@delegate_target)
|
|
159
|
+
emit_circular_warning(delegation_path)
|
|
160
|
+
return "Error: Circular delegation detected: #{delegation_path.join(" -> ")} -> #{@delegate_target}. " \
|
|
138
161
|
"Please restructure your delegation to avoid infinite loops."
|
|
139
162
|
end
|
|
140
163
|
|
|
@@ -172,10 +195,10 @@ module SwarmSDK
|
|
|
172
195
|
# Determine delegation type and proceed
|
|
173
196
|
delegation_result = if @delegate_chat
|
|
174
197
|
# Delegate to agent
|
|
175
|
-
delegate_to_agent(message,
|
|
198
|
+
delegate_to_agent(message, reset_context: reset_context)
|
|
176
199
|
elsif swarm_registry&.registered?(@delegate_target)
|
|
177
200
|
# Delegate to registered swarm
|
|
178
|
-
delegate_to_swarm(message,
|
|
201
|
+
delegate_to_swarm(message, swarm_registry, reset_context: reset_context)
|
|
179
202
|
else
|
|
180
203
|
raise ConfigurationError, "Unknown delegation target: #{@delegate_target}"
|
|
181
204
|
end
|
|
@@ -246,6 +269,11 @@ module SwarmSDK
|
|
|
246
269
|
# Return error string for LLM
|
|
247
270
|
backtrace_str = backtrace_array.join("\n ")
|
|
248
271
|
"Error: #{@tool_name} encountered an error: #{e.class.name}: #{e.message}\nBacktrace:\n #{backtrace_str}"
|
|
272
|
+
ensure
|
|
273
|
+
# Restore the calling Fiber's delegation path.
|
|
274
|
+
# The extended path was only needed during chat.ask() so child Fibers
|
|
275
|
+
# (spawned for nested tool calls) could inherit it for circular detection.
|
|
276
|
+
Fiber[:delegation_path] = saved_delegation_path
|
|
249
277
|
end
|
|
250
278
|
|
|
251
279
|
private
|
|
@@ -254,28 +282,41 @@ module SwarmSDK
|
|
|
254
282
|
#
|
|
255
283
|
# Handles both eager Agent::Chat instances and lazy-loaded delegates.
|
|
256
284
|
# LazyDelegateChat instances are initialized on first access.
|
|
285
|
+
# Sets Fiber-local delegation path so child Fibers (nested delegations)
|
|
286
|
+
# inherit the full chain for circular dependency detection.
|
|
287
|
+
#
|
|
288
|
+
# Tracks concurrent delegations to this target. When multiple parallel
|
|
289
|
+
# tool calls target the same delegate (fan-out), only the first call
|
|
290
|
+
# preserves context; subsequent concurrent calls always clear context
|
|
291
|
+
# to prevent cross-contamination between independent parallel work.
|
|
292
|
+
# Context clearing happens inside Agent::Chat's ask_semaphore for safety.
|
|
257
293
|
#
|
|
258
294
|
# @param message [String] Message to send to the agent
|
|
259
|
-
# @param call_stack [Array] Delegation call stack for circular dependency detection
|
|
260
295
|
# @param reset_context [Boolean] Whether to reset the agent's conversation history before delegation
|
|
261
296
|
# @return [String] Result from agent
|
|
262
|
-
def delegate_to_agent(message,
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
297
|
+
def delegate_to_agent(message, reset_context: false)
|
|
298
|
+
@active_count += 1
|
|
299
|
+
concurrent = @active_count > 1
|
|
300
|
+
|
|
301
|
+
# Set Fiber-local delegation path for this execution path
|
|
302
|
+
# Child Fibers (from nested delegations) inherit this path automatically
|
|
303
|
+
# We create a new array to avoid mutating the parent Fiber's reference
|
|
304
|
+
Fiber[:delegation_path] = (Fiber[:delegation_path] || []) + [@delegate_target]
|
|
305
|
+
|
|
306
|
+
# Resolve the chat instance (handles lazy loading)
|
|
307
|
+
chat = resolve_delegate_chat
|
|
308
|
+
|
|
309
|
+
# Determine if context should be cleared:
|
|
310
|
+
# - reset_context: explicit caller request
|
|
311
|
+
# - !preserve_context: agent configuration
|
|
312
|
+
# - concurrent: parallel fan-out to same delegate (always isolate)
|
|
313
|
+
# Clearing is done inside chat.ask's semaphore to avoid race conditions
|
|
314
|
+
should_clear = reset_context || !@preserve_context || concurrent
|
|
315
|
+
|
|
316
|
+
response = chat.ask(message, source: "delegation", clear_context: should_clear)
|
|
317
|
+
response.content
|
|
318
|
+
ensure
|
|
319
|
+
@active_count -= 1
|
|
279
320
|
end
|
|
280
321
|
|
|
281
322
|
# Resolve the delegate chat instance
|
|
@@ -294,48 +335,52 @@ module SwarmSDK
|
|
|
294
335
|
|
|
295
336
|
# Delegate to a registered swarm
|
|
296
337
|
#
|
|
338
|
+
# Sets Fiber-local delegation path so child Fibers (nested delegations)
|
|
339
|
+
# inherit the full chain for circular dependency detection.
|
|
340
|
+
# Tracks concurrent delegations the same way as delegate_to_agent.
|
|
341
|
+
#
|
|
297
342
|
# @param message [String] Message to send to the swarm
|
|
298
|
-
# @param call_stack [Array] Delegation call stack for circular dependency detection
|
|
299
343
|
# @param swarm_registry [SwarmRegistry] Registry for sub-swarms
|
|
300
344
|
# @param reset_context [Boolean] Whether to reset the swarm's conversation history before delegation
|
|
301
345
|
# @return [String] Result from swarm's lead agent
|
|
302
|
-
def delegate_to_swarm(message,
|
|
346
|
+
def delegate_to_swarm(message, swarm_registry, reset_context: false)
|
|
347
|
+
@active_count += 1
|
|
348
|
+
concurrent = @active_count > 1
|
|
349
|
+
|
|
350
|
+
# Set Fiber-local delegation path for this execution path
|
|
351
|
+
Fiber[:delegation_path] = (Fiber[:delegation_path] || []) + [@delegate_target]
|
|
352
|
+
|
|
303
353
|
# Load sub-swarm (lazy load + cache)
|
|
304
354
|
subswarm = swarm_registry.load_swarm(@delegate_target)
|
|
305
355
|
|
|
306
|
-
#
|
|
307
|
-
|
|
308
|
-
begin
|
|
309
|
-
# Reset swarm if reset_context is true
|
|
310
|
-
swarm_registry.reset(@delegate_target) if reset_context
|
|
356
|
+
# Reset swarm context if explicitly requested or concurrent fan-out
|
|
357
|
+
swarm_registry.reset(@delegate_target) if reset_context || concurrent
|
|
311
358
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
359
|
+
# Execute sub-swarm's lead agent (uses agent() to trigger lazy initialization)
|
|
360
|
+
lead_agent = subswarm.agent(subswarm.lead_agent)
|
|
361
|
+
response = lead_agent.ask(message, source: "delegation")
|
|
362
|
+
result = response.content
|
|
316
363
|
|
|
317
|
-
|
|
318
|
-
|
|
364
|
+
# Reset if keep_context: false (standard behavior)
|
|
365
|
+
swarm_registry.reset_if_needed(@delegate_target)
|
|
319
366
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
call_stack.pop
|
|
324
|
-
end
|
|
367
|
+
result
|
|
368
|
+
ensure
|
|
369
|
+
@active_count -= 1
|
|
325
370
|
end
|
|
326
371
|
|
|
327
372
|
# Emit circular dependency warning event
|
|
328
373
|
#
|
|
329
|
-
# @param
|
|
374
|
+
# @param delegation_path [Array<String>] Current Fiber-local delegation path
|
|
330
375
|
# @return [void]
|
|
331
|
-
def emit_circular_warning(
|
|
376
|
+
def emit_circular_warning(delegation_path)
|
|
332
377
|
LogStream.emit(
|
|
333
378
|
type: "delegation_circular_dependency",
|
|
334
379
|
agent: @agent_name,
|
|
335
380
|
swarm_id: @swarm.swarm_id,
|
|
336
381
|
parent_swarm_id: @swarm.parent_swarm_id,
|
|
337
382
|
target: @delegate_target,
|
|
338
|
-
|
|
383
|
+
delegation_path: delegation_path,
|
|
339
384
|
timestamp: Time.now.utc.iso8601,
|
|
340
385
|
)
|
|
341
386
|
end
|
data/lib/swarm_sdk/version.rb
CHANGED