scout-ai 2.1.0 → 2.2.0

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.
Files changed (186) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +133 -58
  3. data/README.md +2 -1
  4. data/VERSION +1 -1
  5. data/doc/Improvements.md +490 -148
  6. data/doc/Model.md +53 -10
  7. data/doc/StartHere.md +44 -19
  8. data/doc/developer/Architecture.md +25 -6
  9. data/doc/developer/Backends.md +207 -48
  10. data/doc/developer/ChatLifecycle.md +121 -20
  11. data/doc/developer/DelegationInternals.md +319 -33
  12. data/doc/developer/DesignPrinciples.md +205 -16
  13. data/doc/developer/PathEngine.md +628 -0
  14. data/doc/developer/PromptProcessing.md +332 -82
  15. data/doc/developer/Provenance.md +388 -40
  16. data/doc/developer/Sandbox.md +213 -0
  17. data/doc/user/BuildingAgents.md +130 -18
  18. data/doc/user/Cookbook.md +29 -8
  19. data/doc/user/CoreConcepts.md +7 -6
  20. data/doc/user/Delegation.md +166 -15
  21. data/doc/user/GettingStarted.md +20 -16
  22. data/doc/user/ManagingContext.md +155 -17
  23. data/doc/user/MultiAgentWorkflows.md +105 -15
  24. data/doc/user/Python.md +11 -1
  25. data/doc/user/RunningInference.md +90 -47
  26. data/doc/user/ToolCalling.md +134 -29
  27. data/doc/user/WritingChats.md +56 -27
  28. data/lib/scout/llm/agent/attach.rb +13 -17
  29. data/lib/scout/llm/agent/chat.rb +32 -0
  30. data/lib/scout/llm/agent/construction.rb +64 -0
  31. data/lib/scout/llm/agent/conversation.rb +304 -0
  32. data/lib/scout/llm/agent/delegate.rb +56 -238
  33. data/lib/scout/llm/agent/path/edit.rb +230 -0
  34. data/lib/scout/llm/agent/path/patch.rb +252 -0
  35. data/lib/scout/llm/agent/path/workflow_kinds.rb +175 -0
  36. data/lib/scout/llm/agent/path.rb +1777 -0
  37. data/lib/scout/llm/agent/save.rb +11 -9
  38. data/lib/scout/llm/agent/workflow.rb +77 -16
  39. data/lib/scout/llm/agent.rb +147 -31
  40. data/lib/scout/llm/ask.rb +28 -17
  41. data/lib/scout/llm/backends/anthropic.rb +1 -1
  42. data/lib/scout/llm/backends/bedrock.rb +3 -2
  43. data/lib/scout/llm/backends/default.rb +127 -34
  44. data/lib/scout/llm/backends/glm.rb +28 -1
  45. data/lib/scout/llm/backends/huggingface.rb +1 -1
  46. data/lib/scout/llm/backends/ollama.rb +1 -1
  47. data/lib/scout/llm/backends/openai.rb +7 -6
  48. data/lib/scout/llm/backends/openwebui.rb +14 -7
  49. data/lib/scout/llm/backends/relay.rb +8 -2
  50. data/lib/scout/llm/chat/agent_meta.rb +24 -60
  51. data/lib/scout/llm/chat/parse.rb +1 -1
  52. data/lib/scout/llm/chat/process/files.rb +10 -0
  53. data/lib/scout/llm/chat/process/meta.rb +5 -7
  54. data/lib/scout/llm/chat/process/tools.rb +104 -49
  55. data/lib/scout/llm/chat/prompt/epoch_tool_compactation.rb +355 -0
  56. data/lib/scout/llm/chat/prompt/inbox.rb +188 -0
  57. data/lib/scout/llm/chat/prompt/shorten_tools.rb +797 -13
  58. data/lib/scout/llm/chat/prompt/shorten_tools_epoch.rb +1 -365
  59. data/lib/scout/llm/chat/prompt/shorten_tools_epoch_increment.rb +1 -0
  60. data/lib/scout/llm/chat/prompt.rb +24 -10
  61. data/lib/scout/llm/chat/provenance/live.rb +1080 -0
  62. data/lib/scout/llm/chat/provenance/tokens.rb +386 -0
  63. data/lib/scout/llm/chat/provenance.rb +343 -676
  64. data/lib/scout/llm/chat.rb +8 -2
  65. data/lib/scout/llm/embed.rb +15 -3
  66. data/lib/scout/llm/image.rb +4 -1
  67. data/lib/scout/llm/mcp.rb +4 -2
  68. data/lib/scout/llm/request_context.rb +234 -0
  69. data/lib/scout/llm/sandbox.rb +263 -0
  70. data/lib/scout/llm/tools/call.rb +163 -42
  71. data/lib/scout/llm/tools/definition.rb +48 -0
  72. data/lib/scout/llm/tools/knowledge_base.rb +1 -0
  73. data/lib/scout/llm/tools/mcp.rb +50 -43
  74. data/lib/scout/llm/tools/workflow.rb +90 -20
  75. data/lib/scout/llm/tools.rb +14 -41
  76. data/lib/scout/model/decision/base.rb +43 -0
  77. data/lib/scout/model/decision/jev.rb +161 -0
  78. data/lib/scout/model/python/torch.rb +1 -1
  79. data/research/clear-tools-inherit.md +171 -0
  80. data/research/path-engine-dc1-dc2-dc3-fixes.md +279 -0
  81. data/research/path-kind-chat-role.md +96 -0
  82. data/research/path-kind-contract-fixes-engine.md +307 -0
  83. data/research/scout-ai-runtime-availability.md +65 -0
  84. data/research/subsys/README.md +127 -0
  85. data/research/subsys/agent-delegation.md +201 -0
  86. data/research/subsys/agent-society.md +275 -0
  87. data/research/subsys/agentworkflow.md +233 -0
  88. data/research/subsys/backends.md +221 -0
  89. data/research/subsys/chat.md +314 -0
  90. data/research/subsys/embed-image.md +244 -0
  91. data/research/subsys/entry-cli.md +253 -0
  92. data/research/subsys/model-ml.md +326 -0
  93. data/research/subsys/provenance.md +290 -0
  94. data/research/subsys/tools.md +334 -0
  95. data/scout-ai.gemspec +71 -27
  96. data/scout_commands/agent/ask +8 -7
  97. data/scout_commands/llm/abridged +40 -0
  98. data/scout_commands/llm/ask +9 -9
  99. data/scout_commands/llm/md +2 -0
  100. data/scout_commands/llm/prov +724 -59
  101. data/scout_commands/llm/word +4 -0
  102. data/share/word/reference.docx +0 -0
  103. data/test/integration/scout/model/decision/test_jev.rb +144 -0
  104. data/test/scout/llm/agent/test_attach.rb +117 -0
  105. data/test/scout/llm/agent/test_conversation.rb +407 -0
  106. data/test/scout/llm/agent/test_delegate.rb +209 -0
  107. data/test/scout/llm/agent/test_path.rb +1812 -0
  108. data/test/scout/llm/agent/test_path_edit.rb +908 -0
  109. data/test/scout/llm/agent/test_path_errors.rb +390 -0
  110. data/test/scout/llm/agent/test_path_filesystem.rb +532 -0
  111. data/test/scout/llm/agent/test_path_patch.rb +233 -0
  112. data/test/scout/llm/agent/test_path_promotion.rb +386 -0
  113. data/test/scout/llm/agent/test_path_workflow_integration.rb +665 -0
  114. data/test/scout/llm/agent/test_save.rb +70 -15
  115. data/test/scout/llm/agent/test_workflow.rb +130 -4
  116. data/test/scout/llm/backends/test_anthropic.rb +4 -4
  117. data/test/scout/llm/backends/test_bedrock.rb +4 -4
  118. data/test/scout/llm/backends/test_format_other.rb +43 -0
  119. data/test/scout/llm/backends/test_ollama.rb +2 -2
  120. data/test/scout/llm/backends/test_openwebui.rb +12 -8
  121. data/test/scout/llm/backends/test_reasoning.rb +114 -0
  122. data/test/scout/llm/chat/agent_meta_fixtures.rb +28 -27
  123. data/test/scout/llm/chat/prompt/test_epoch_tool_compactation.rb +612 -0
  124. data/test/scout/llm/chat/prov_plot_fixtures.rb +131 -0
  125. data/test/scout/llm/chat/test_agent_meta.rb +40 -80
  126. data/test/scout/llm/chat/test_agent_meta_provenance.rb +67 -28
  127. data/test/scout/llm/chat/test_agent_meta_tokens.rb +59 -8
  128. data/test/scout/llm/chat/test_agent_view.rb +178 -0
  129. data/test/scout/llm/chat/test_live_report.rb +282 -0
  130. data/test/scout/llm/chat/test_live_workload.rb +193 -0
  131. data/test/scout/llm/chat/test_parse.rb +32 -2
  132. data/test/scout/llm/chat/test_prompt.rb +411 -0
  133. data/test/scout/llm/chat/test_prov_cli.rb +310 -15
  134. data/test/scout/llm/chat/test_prov_cli_live.rb +152 -0
  135. data/test/scout/llm/chat/test_prov_plot.rb +213 -0
  136. data/test/scout/llm/chat/test_prov_timeline.rb +355 -0
  137. data/test/scout/llm/chat/test_provenance.rb +67 -22
  138. data/test/scout/llm/chat/test_provenance_index.rb +479 -0
  139. data/test/scout/llm/chat/test_society_view.rb +159 -0
  140. data/test/scout/llm/test_agent.rb +1 -1
  141. data/test/scout/llm/test_embed.rb +103 -1
  142. data/test/scout/llm/test_request_context.rb +299 -0
  143. data/test/scout/llm/test_sandbox.rb +249 -0
  144. data/test/scout/llm/tools/test_agent_pairing.rb +150 -0
  145. data/test/scout/llm/tools/test_call_delegation_conflict.rb +316 -0
  146. data/test/scout/llm/tools/test_definition.rb +332 -0
  147. data/test/scout/llm/tools/test_jobs_file.rb +211 -0
  148. data/test/scout/llm/tools/test_mcp.rb +37 -8
  149. data/test/scout/llm/tools/test_workflow.rb +45 -1
  150. data/test/scout/model/decision/test_jev.rb +453 -0
  151. data/test/scout/model/python/huggingface/causal/test_next_token.rb +7 -10
  152. data/test/scout/model/python/huggingface/test_causal.rb +11 -2
  153. data/test/scout/model/python/huggingface/test_classification.rb +5 -3
  154. data/test/scout/model/python/test_availability.rb +116 -0
  155. data/test/scout/model/python/test_torch.rb +54 -2
  156. data/test/support/availability.rb +72 -174
  157. data/test/support/fake_clients.rb +18 -9
  158. data/test/support/fixtures.rb +75 -6
  159. data/test/support/mock_backend.rb +9 -1
  160. data/test/support/test_fixtures.rb +156 -0
  161. data/test/test_helper.rb +14 -4
  162. metadata +70 -26
  163. data/research/ChatAnalyst-required-changes.md +0 -167
  164. data/research/agent-delegation-analysis.md +0 -810
  165. data/research/agent-meta-provenance-integration-plan.md +0 -622
  166. data/research/agent-workflow-analysis.md +0 -1120
  167. data/research/backends-analysis.md +0 -836
  168. data/research/chat-core-analysis.md +0 -946
  169. data/research/chatanalyst-provenance/00-baseline.md +0 -30
  170. data/research/chatanalyst-provenance/01-repo-map.md +0 -60
  171. data/research/chatanalyst-provenance/02-event-reconstruction.md +0 -55
  172. data/research/chatanalyst-provenance/03-duplication-evidence.md +0 -45
  173. data/research/chatanalyst-provenance/04-tooling-root-cause.md +0 -57
  174. data/research/chatanalyst-provenance/05-fix-plan.md +0 -46
  175. data/research/chatanalyst-provenance/07-critic-review.md +0 -25
  176. data/research/chatanalyst-provenance/final-report.md +0 -45
  177. data/research/chatanalyst-provenance/resumption.md +0 -37
  178. data/research/coding-philosophy-analysis.md +0 -928
  179. data/research/commands-analysis.md +0 -947
  180. data/research/multi-agent-patterns-analysis.md +0 -853
  181. data/research/prompt-strategies-analysis.md +0 -630
  182. data/research/prov-verbosity-fix-notes.md +0 -77
  183. data/research/provenance-analysis.md +0 -469
  184. data/research/provenance-continuation-accounting.md +0 -255
  185. data/research/synthesis-report.md +0 -487
  186. data/research/tools-system-analysis.md +0 -779
@@ -0,0 +1,316 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
3
+ require 'scout/llm/agent'
4
+
5
+ class TestCallDelegationConflict < Test::Unit::TestCase
6
+ def setup
7
+ super
8
+ end
9
+
10
+ # `call(id, name, args)` and `ask_call(id, args)` helpers: providers send
11
+ # the function name and a JSON arguments string; process_calls receives
12
+ # that provider shape unchanged.
13
+ def call(id, name, arguments)
14
+ IndiferentHash.setup(
15
+ 'id' => id,
16
+ 'type' => 'function',
17
+ 'function' => {
18
+ 'name' => name.to_s,
19
+ 'arguments' => arguments.to_json
20
+ }
21
+ )
22
+ end
23
+
24
+ def ask_call(id, arguments)
25
+ call(id, 'ask', arguments)
26
+ end
27
+
28
+ def hand_off_call(id, arguments)
29
+ call(id, 'hand_off_to_Worker', arguments)
30
+ end
31
+
32
+ # Register the real socialize ask tool on a parent agent so the delegated
33
+ # agents are produced by the production path, then dispatch calls through
34
+ # LLM.process_calls with a save-file-bearing parent so conversations are
35
+ # persisted (anchor save files are derived from it).
36
+ def dispatch(parent, tool_calls)
37
+ tools = parent.other_options[:tools] || {}
38
+ LLM.process_calls(tools, tool_calls)
39
+ end
40
+
41
+ def parent_with_ask(dir)
42
+ parent = LLM::Agent.new(
43
+ start_chat: Chat.setup([{role: :system, content: 'orchestrator'}])
44
+ )
45
+ parent.save_file = File.join(dir, 'root.chat')
46
+ # The ask tool and a pre-registered Worker template, so the delegated
47
+ # agents are produced by the production path (socialize + society).
48
+ parent.society = {'Worker' => worker_agent}
49
+ parent.socialize
50
+ parent
51
+ end
52
+
53
+ def worker_agent(content = 'worker helper')
54
+ LLM::Agent.new(
55
+ start_chat: Chat.setup([{role: :system, content: content}])
56
+ )
57
+ end
58
+
59
+ # Tool-round outputs are function_call_output messages whose content is
60
+ # the JSON envelope {id, name, content, error} (no error key when the
61
+ # call succeeded). Parse them into a convenient array of hashes.
62
+ def outputs_of(result)
63
+ result
64
+ .select { |m| m[:role].to_s == 'function_call_output' }
65
+ .collect { |m| JSON.parse(m[:content]) }
66
+ end
67
+
68
+ # A mixed tool round: one ordinary side-effecting Proc tool runs before
69
+ # the delegations. Its result must survive a delegation conflict: the
70
+ # conflict invalidates the agent sub-batch, not the whole round.
71
+ def parent_with_ask_and_write(dir)
72
+ parent = parent_with_ask(dir)
73
+ tools = parent.other_options[:tools]
74
+ tools[:write] = [
75
+ Proc.new do |_name, args|
76
+ Open.write(File.join(dir, args[:path]), args[:text])
77
+ "written #{args[:path]}"
78
+ end,
79
+ LLM.tool_definition(:write, 'Write a file',
80
+ {path: {type: 'string'}, text: {type: 'string'}},
81
+ required: [:path, :text])
82
+ ]
83
+ parent.other_options[:tools] = tools
84
+ parent
85
+ end
86
+
87
+ # The original hang: two delegations of one round, no conversation name,
88
+ # both resolving onto Worker/default. The round now completes with two
89
+ # error envelopes instead of blocking forever; Timeout remains the
90
+ # honest bound on the regression.
91
+ def test_two_unnamed_delegations_of_one_round_return_errors_instead_of_hanging
92
+ Timeout.timeout(30) do
93
+ TmpFile.with_dir do |dir|
94
+ parent = parent_with_ask(dir)
95
+
96
+ outputs = nil
97
+ assert_nothing_raised do
98
+ outputs = outputs_of dispatch(parent, [
99
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X'}),
100
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y'})
101
+ ])
102
+ end
103
+
104
+ assert_equal 2, outputs.length
105
+ outputs.each do |out|
106
+ assert_equal 'error', out['error']
107
+ assert_match(/Conflicting delegations/, out['content']
108
+ .sub(/^\{"exception":/, '')
109
+ .sub(/"\}$/, ''))
110
+ end
111
+ # No specialist was started and nothing was persisted.
112
+ assert_equal 0, LLM::Mock.index
113
+ assert !Open.exist?(File.join(dir, 'root.society'))
114
+ end
115
+ end
116
+ end
117
+
118
+ # Same shape with an explicit conversation name.
119
+ def test_two_named_delegations_of_one_round_return_errors
120
+ Timeout.timeout(30) do
121
+ TmpFile.with_dir do |dir|
122
+ parent = parent_with_ask(dir)
123
+
124
+ outputs = outputs_of dispatch(parent, [
125
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'task_1'}),
126
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y', conversation: 'task_1'})
127
+ ])
128
+
129
+ assert_equal 2, outputs.length
130
+ outputs.each do |out|
131
+ assert_equal 'error', out['error']
132
+ assert_match(/task_1/, out['content'])
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ # The whole agent sub-batch is rejected, including delegations that
139
+ # target a distinct conversation: all-or-nothing keeps the rule
140
+ # predictable for the model.
141
+ def test_partial_conflict_rejects_all_delegations_of_the_round
142
+ Timeout.timeout(30) do
143
+ TmpFile.with_dir do |dir|
144
+ parent = parent_with_ask(dir)
145
+
146
+ outputs = outputs_of dispatch(parent, [
147
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X'}),
148
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y'}),
149
+ ask_call('call_3', {agent: 'Worker', prompt: 'do Z', conversation: 'other'})
150
+ ])
151
+
152
+ assert_equal 3, outputs.length
153
+ outputs.each do |out|
154
+ assert_equal 'error', out['error']
155
+ end
156
+ # Even the delegation to a distinct conversation did not run.
157
+ assert_equal 0, LLM::Mock.index
158
+ other = parent.conversation_agent('Worker/other')
159
+ assert other.nil? || other.current_chat.none? { |m| m[:role].to_s == 'user' }
160
+ end
161
+ end
162
+ end
163
+
164
+ # Ordinary tool calls of the same round are untouched: their side
165
+ # effects have already happened and the model must see their results.
166
+ def test_side_effecting_tool_of_the_same_round_keeps_its_result
167
+ Timeout.timeout(30) do
168
+ TmpFile.with_dir do |dir|
169
+ parent = parent_with_ask_and_write(dir)
170
+
171
+ outputs = outputs_of dispatch(parent, [
172
+ call('call_1', 'write', {path: 'out.txt', text: 'hello'}),
173
+ ask_call('call_2', {agent: 'Worker', prompt: 'do X'}),
174
+ ask_call('call_3', {agent: 'Worker', prompt: 'do Y'})
175
+ ])
176
+
177
+ assert_equal 3, outputs.length
178
+ assert_equal 'written out.txt', outputs.first['content']
179
+ assert !outputs.first.key?('error')
180
+ assert_equal 'write', outputs.first['name']
181
+
182
+ assert File.read(File.join(dir, 'out.txt')) == 'hello'
183
+ outputs[1..-1].each do |out|
184
+ assert_equal 'error', out['error']
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ # ask and a hand_off slot resolve onto the same conversation holder
191
+ # (Worker/Worker: the slot named after the specialist) and conflict
192
+ # inside one round.
193
+ def test_ask_and_hand_off_onto_the_same_conversation_conflict
194
+ LLM::Mock.script('worker answer')
195
+ Timeout.timeout(30) do
196
+ TmpFile.with_dir do |dir|
197
+ parent = parent_with_ask(dir)
198
+ parent.delegate(parent.society['Worker'], 'Worker', 'Hand work off')
199
+
200
+ # `hand_off_to_Worker` asks the conversation named after its slot,
201
+ # so an `ask` naming that same conversation collides with it.
202
+ outputs = outputs_of dispatch(parent, [
203
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'Worker'}),
204
+ hand_off_call('call_2', {message: 'do Y'})
205
+ ])
206
+
207
+ assert_equal 2, outputs.length
208
+ outputs.each do |out|
209
+ assert_equal 'error', out['error']
210
+ assert_match(/Conflicting delegations/, out['content'])
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ # A conflicting batch restores the live conversation EXACTLY, whatever
217
+ # mixture of delegation forms staged it: plain continuation prompts, a
218
+ # clear_tools continuation (which staged a context-only marker before
219
+ # its prompt), and a hand_off new_conversation RESTART (which had
220
+ # already re-branched the chat in place before the conflict was even
221
+ # detected). Both delegations of the same slot hold the SAME holder
222
+ # object (the `@chats` registry returns one agent per conversation
223
+ # key), so this also covers one chat mutated through two agent
224
+ # references. Compare the complete message list, roles and contents,
225
+ # before and after.
226
+ def test_rejected_batch_restores_the_conversation_exactly
227
+ LLM::Mock.script('worker answer')
228
+ Timeout.timeout(30) do
229
+ TmpFile.with_dir do |dir|
230
+ parent = parent_with_ask(dir)
231
+ parent.delegate(parent.society['Worker'], 'Worker', 'Hand work off')
232
+
233
+ # Round 0: establish Worker/Worker with one completed turn.
234
+ outputs_of dispatch(parent, [
235
+ hand_off_call('call_0', {message: 'seed turn'})
236
+ ])
237
+ holder = parent.conversation_agent('Worker/Worker')
238
+ before = holder.current_chat.collect { |m| [m[:role], m[:content]] }
239
+
240
+ # Round 1: three staged delegations onto the same conversation:
241
+ # plain ask, clear_tools ask, and a restarting hand_off.
242
+ outputs = outputs_of dispatch(parent, [
243
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'Worker'}),
244
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y', conversation: 'Worker', inherit: 'clear_tools'}),
245
+ hand_off_call('call_3', {message: 'do Z', new_conversation: true})
246
+ ])
247
+
248
+ assert_equal 3, outputs.length
249
+ outputs.each do |out|
250
+ assert_equal 'error', out['error']
251
+ assert_match(/Conflicting delegations/, out['content'])
252
+ end
253
+
254
+ after = holder.current_chat.collect { |m| [m[:role], m[:content]] }
255
+ assert_equal before, after
256
+
257
+ # The undo left no restart bookkeeping behind either.
258
+ assert_nil holder.restart_undo_stash
259
+ end
260
+ end
261
+ end
262
+
263
+ # A valid batch still works after a rejected one, and the rejected
264
+ # prompts do not leak into the conversation: preparation appends them
265
+ # in memory, and a rejected batch rolls them back before the round
266
+ # finishes.
267
+ def test_valid_delegation_after_rejection_starts_clean
268
+ LLM::Mock.script('worker answer')
269
+ Timeout.timeout(30) do
270
+ TmpFile.with_dir do |dir|
271
+ parent = parent_with_ask(dir)
272
+
273
+ outputs_of dispatch(parent, [
274
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X'}),
275
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y'})
276
+ ])
277
+
278
+ second = outputs_of dispatch(parent, [
279
+ ask_call('call_3', {agent: 'Worker', prompt: 'do W'})
280
+ ])
281
+
282
+ assert_equal 1, second.length
283
+ assert !second.first.key?('error')
284
+ assert_equal 'worker answer', second.first['content']
285
+
286
+ child = parent.conversation_agent('Worker/default')
287
+ user_turns = child.current_chat
288
+ .select { |m| m[:role].to_s == 'user' }
289
+ .collect { |m| m[:content] }
290
+ assert_equal ['do W'], user_turns
291
+ end
292
+ end
293
+ end
294
+
295
+ # Distinct conversations of one round execute normally and their answers
296
+ # are paired to the original calls. All specialists share one scripted
297
+ # answer, so the observable fact is that both calls were answered.
298
+ def test_distinct_conversations_of_one_round_still_work
299
+ LLM::Mock.script('worker answer')
300
+ Timeout.timeout(30) do
301
+ TmpFile.with_dir do |dir|
302
+ parent = parent_with_ask(dir)
303
+
304
+ outputs = outputs_of dispatch(parent, [
305
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'one'}),
306
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y', conversation: 'two'})
307
+ ])
308
+
309
+ assert_equal 2, outputs.length
310
+ contents = outputs.collect { |out| out['content'] }
311
+ assert contents.all? { |c| c == 'worker answer' }
312
+ outputs.each { |out| assert !out.key?('error') }
313
+ end
314
+ end
315
+ end
316
+ end
@@ -0,0 +1,332 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require File.expand_path(__FILE__).sub(%r(.*/test/), '').sub(/test_(.*)\.rb/,'\1')
3
+
4
+ class TestLLMToolDefinition < Test::Unit::TestCase
5
+ INHERIT_ENUM = %w[none tools conversation clear_tools].freeze
6
+
7
+ def setup_workflow_module
8
+ Module.new do
9
+ extend Workflow
10
+ self.name = "RecipeWorkflow"
11
+
12
+ desc "List the steps to cook a recipe"
13
+ input :recipe, :string, "Recipe for which to extract steps"
14
+ task :recipe_steps => :array do |recipe|
15
+ ["prepare batter", "bake"]
16
+ end
17
+
18
+ desc "Calculate time spent in each step of the recipe"
19
+ input :step, :string, "Cooking step"
20
+ input :minutes, :integer, "Minutes", nil, required: true
21
+ task :step_time => :string do |step|
22
+ "30 minutes"
23
+ end
24
+ end
25
+ end
26
+
27
+ def setup_agent
28
+ require 'scout/llm/agent'
29
+ LLM::Agent.new
30
+ end
31
+
32
+ ## Attachments site (previously strict)
33
+
34
+ def test_attach_definition
35
+ agent = setup_agent
36
+ agent.attachments
37
+
38
+ assert_equal true, agent.other_options[:tools][:attach].first.is_a?(Proc)
39
+
40
+ definition = agent.other_options[:tools][:attach].last
41
+ assert_equal :attach, definition[:name]
42
+ assert_equal 'function', definition[:type]
43
+ assert_equal({
44
+ type: 'string',
45
+ description: 'Path to the file'
46
+ }, definition[:function][:parameters][:properties][:file])
47
+ assert_equal({
48
+ type: 'string',
49
+ description: 'File type of the file (e.g image, pdf), auto by default',
50
+ enum: %w(auto image pdf png jpeg),
51
+ default: 'auto'
52
+ }, definition[:function][:parameters][:properties][:file_type])
53
+ assert_equal [:file], definition[:function][:parameters][:required]
54
+ assert_equal false, definition[:function][:parameters][:additionalProperties]
55
+
56
+ assert_equal definition[:name], definition[:function][:name]
57
+ assert_equal definition[:description], definition[:function][:description]
58
+ assert_equal definition[:parameters], definition[:function][:parameters]
59
+ end
60
+
61
+ ## Socialize site (previously strict)
62
+
63
+ def test_socialize_definition
64
+ agent = setup_agent
65
+ agent.socialize
66
+
67
+ definition = agent.other_options[:tools][:ask].last
68
+ assert_equal :ask, definition[:name]
69
+ assert_equal 'function', definition[:type]
70
+
71
+ properties = definition[:function][:parameters][:properties]
72
+ assert_equal({
73
+ type: 'string',
74
+ description: 'Name of the specialist agent to ask'
75
+ }, properties[:agent])
76
+ assert_equal({
77
+ type: 'string',
78
+ description: 'Plain-text prompt sent as one user message to the specialist'
79
+ }, properties[:prompt])
80
+ assert_equal({
81
+ type: 'string',
82
+ pattern: '^[A-Za-z0-9][A-Za-z0-9_.-]*$',
83
+ description: 'Optional conversation or chat identifier; reuse it with the same agent to continue that conversation across several calls'
84
+ }, properties[:conversation])
85
+ assert_equal({
86
+ type: 'string',
87
+ enum: INHERIT_ENUM,
88
+ default: 'tools',
89
+ description: "Context copied only when starting the call or named conversation: 'none' uses only the specialist start_chat, 'tools' also copies caller task tooling, and 'conversation' copies the caller task conversation; 'clear_tools' copies the caller conversation too and, when continuing an existing conversation, drops earlier tool-call traces from the model context only (the stored chat keeps them)"
90
+ }, properties[:inherit])
91
+
92
+ assert_equal [:agent, :prompt], definition[:function][:parameters][:required]
93
+ assert_equal false, definition[:function][:parameters][:additionalProperties]
94
+
95
+ assert_equal definition[:name], definition[:function][:name]
96
+ assert_equal definition[:description], definition[:function][:description]
97
+ assert_equal definition[:parameters], definition[:function][:parameters]
98
+ end
99
+
100
+ ## Delegate site (previously NON-strict: strict only after the refactor)
101
+
102
+ def test_delegate_definition
103
+ agent = setup_agent
104
+ specialist = setup_agent
105
+ agent.delegate(specialist, 'Worker', 'desc here')
106
+
107
+ definition = agent.other_options[:tools][:hand_off_to_Worker].last
108
+ assert_equal :hand_off_to_Worker, definition[:name]
109
+ assert_equal 'desc here', definition[:description]
110
+ assert_equal 'function', definition[:type]
111
+
112
+ properties = definition[:function][:parameters][:properties]
113
+ assert_equal({
114
+ type: :string,
115
+ description: 'Message to pass to the agent'
116
+ }, properties[:message])
117
+ assert_equal({
118
+ type: :boolean,
119
+ description: 'Erase conversation history and start a new conversation with this message',
120
+ default: false
121
+ }, properties[:new_conversation])
122
+
123
+ assert_equal [:message], definition[:function][:parameters][:required]
124
+ assert_equal false, definition[:function][:parameters][:additionalProperties]
125
+
126
+ assert_equal definition[:name], definition[:function][:name]
127
+ assert_equal definition[:description], definition[:function][:description]
128
+ assert_equal definition[:parameters], definition[:function][:parameters]
129
+ end
130
+
131
+ ## Workflow task site (previously NON-strict: strict only after the refactor)
132
+
133
+ def test_task_tool_definition_no_inputs
134
+ m = setup_workflow_module
135
+ definition = LLM.task_tool_definition(m, :recipe_steps)
136
+
137
+ assert_equal :recipe_steps, definition[:name]
138
+ assert_equal 'List the steps to cook a recipe', definition[:description]
139
+ assert_equal({
140
+ type: 'object',
141
+ properties: {
142
+ recipe: { type: :string, description: 'Recipe for which to extract steps' },
143
+ return_path: {
144
+ default: false,
145
+ type: 'boolean',
146
+ description: 'Instead of returning the result, return the path where the result is persisted. Use this when you want to pass the result to another tool or script, move it, or process it as a file without loading its contents into the conversation.'
147
+ },
148
+ refresh: {
149
+ default: 'false',
150
+ type: 'string',
151
+ enum: ['false', 'refresh', 'deep_refresh'],
152
+ description: 'Control whether cached results may be reused. Use "refresh" to recompute this task; use "deep_refresh" when results used by this task may also be stale and should be refreshed.'
153
+ }
154
+ },
155
+ required: [],
156
+ additionalProperties: false
157
+ }, definition[:parameters])
158
+ # Bare definition: no function envelope for workflow task tools.
159
+ assert_equal false, definition.include?(:function)
160
+ end
161
+
162
+ def test_task_tool_definition_required
163
+ m = setup_workflow_module
164
+ definition = LLM.task_tool_definition(m, :step_time)
165
+
166
+ assert_equal :step_time, definition[:name]
167
+ assert_equal [:minutes], definition[:parameters][:required]
168
+ assert_equal :integer, definition[:parameters][:properties][:minutes][:type]
169
+ # Previously non-strict: the builder now makes the schema strict.
170
+ assert_equal false, definition[:parameters][:additionalProperties]
171
+ end
172
+
173
+ def test_task_tool_definition_filtered_inputs
174
+ m = setup_workflow_module
175
+ definition = LLM.task_tool_definition(m, :step_time, ['step'])
176
+
177
+ assert_equal %i(step return_path refresh), definition[:parameters][:properties].keys
178
+ # The empty defaults Hash recorded when inputs are given is preserved.
179
+ assert_equal({}, definition[:parameters][:defaults])
180
+ assert_equal false, definition[:parameters][:additionalProperties]
181
+ end
182
+
183
+ def test_task_tool_definition_pinned_default
184
+ m = setup_workflow_module
185
+ definition = LLM.task_tool_definition(m, :recipe_steps, ['recipe=lasagna'])
186
+
187
+ assert_equal %i(return_path refresh), definition[:parameters][:properties].keys
188
+ assert_equal({ 'recipe' => 'lasagna' }, definition[:parameters][:defaults])
189
+ assert_equal false, definition[:parameters][:additionalProperties]
190
+ end
191
+
192
+ ## Replay side-channel: process_calls merges parameters[:defaults]
193
+
194
+ def test_defaults_are_applied_on_replay_for_proc_tools
195
+ m = setup_workflow_module
196
+ definition = LLM.task_tool_definition(m, :recipe_steps, ['recipe=lasagna'])
197
+ seen = nil
198
+ handler = Proc.new do |_name, args|
199
+ seen = args
200
+ 'done'
201
+ end
202
+
203
+ calls = [
204
+ IndiferentHash.setup({
205
+ 'id' => 'call_1', 'type' => 'function',
206
+ 'function' => { 'name' => 'recipe_steps', 'arguments' => '{}' }
207
+ })
208
+ ]
209
+ LLM.process_calls({ 'recipe_steps' => [handler, definition] }, calls) do |_name, _args|
210
+ 'unused'
211
+ end
212
+ assert_equal({ 'recipe' => 'lasagna' }, seen)
213
+ end
214
+
215
+ def test_defaults_are_applied_on_replay_for_workflow_tools
216
+ m = Module.new do
217
+ extend Workflow
218
+ self.name = "RecipeWorkflow"
219
+
220
+ desc "List the steps to cook a recipe"
221
+ input :recipe, :string, "Recipe for which to extract steps"
222
+ task :recipe_steps => :array do |recipe|
223
+ ["prepare batter", "bake", recipe]
224
+ end
225
+ end
226
+ definition = LLM.task_tool_definition(m, :recipe_steps, ['recipe=lasagna'])
227
+
228
+ calls = [
229
+ IndiferentHash.setup({
230
+ 'id' => 'call_1', 'type' => 'function',
231
+ 'function' => { 'name' => 'recipe_steps', 'arguments' => '{}' }
232
+ })
233
+ ]
234
+ result = LLM.process_calls({ 'recipe_steps' => [m, definition] }, calls) do |_name, _args|
235
+ 'done'
236
+ end
237
+ # The workflow job itself must observe the pinned default: the task
238
+ # returns the recipe string as its third element.
239
+ assert_equal 'lasagna', JSON.parse(JSON.parse(result.last[:content])['content']).last
240
+ end
241
+
242
+ ## Builder primitives
243
+
244
+ def test_builder_defaults
245
+ definition = LLM.tool_definition(:task, 'A task', {
246
+ 'a' => { type: :string },
247
+ 'b' => { type: :integer, default: 1 }
248
+ })
249
+
250
+ assert_equal({
251
+ name: :task,
252
+ description: 'A task',
253
+ parameters: {
254
+ type: 'object',
255
+ properties: {
256
+ 'a' => { type: :string },
257
+ 'b' => { type: :integer, default: 1 }
258
+ },
259
+ required: [],
260
+ additionalProperties: false
261
+ },
262
+ type: 'function',
263
+ function: {
264
+ name: :task,
265
+ description: 'A task',
266
+ parameters: {
267
+ type: 'object',
268
+ properties: {
269
+ 'a' => { type: :string },
270
+ 'b' => { type: :integer, default: 1 }
271
+ },
272
+ required: [],
273
+ additionalProperties: false
274
+ }
275
+ }
276
+ }, definition)
277
+ end
278
+
279
+ def test_builder_no_defaults_key_without_argument_defaults
280
+ definition = LLM.tool_definition(:task, 'A task', { 'a' => { type: :string } })
281
+
282
+ assert_equal false, definition[:parameters].include?(:defaults)
283
+ assert_equal false, definition[:parameters][:additionalProperties]
284
+ end
285
+
286
+ def test_builder_omits_default_when_nil
287
+ definition = LLM.tool_definition(:task, 'A task', { 'a' => { type: :string } }, defaults: nil)
288
+
289
+ assert_equal false, definition[:parameters].include?(:defaults)
290
+ end
291
+
292
+ def test_builder_keeps_empty_hash_defaults
293
+ definition = LLM.tool_definition(:task, 'A task', { 'a' => { type: :string } }, defaults: {})
294
+
295
+ assert_equal({}, definition[:parameters][:defaults])
296
+ end
297
+
298
+ def test_builder_optional_strictness
299
+ definition = LLM.tool_definition(:task, 'A task', { 'a' => { type: :string } }, strict: false)
300
+
301
+ assert_equal false, definition[:parameters].include?(:additionalProperties)
302
+ assert_equal false, definition[:function][:parameters].include?(:additionalProperties)
303
+ end
304
+
305
+ def test_builder_bare_option
306
+ definition = LLM.tool_definition(:task, 'A task', { 'a' => { type: :string } }, envelope: false)
307
+
308
+ assert_equal({
309
+ name: :task,
310
+ description: 'A task',
311
+ parameters: {
312
+ type: 'object',
313
+ properties: { 'a' => { type: :string } },
314
+ required: [],
315
+ additionalProperties: false
316
+ }
317
+ }, definition)
318
+ end
319
+
320
+ def test_builder_wraps_single_required_name
321
+ definition = LLM.tool_definition(:task, 'A task', { 'a' => { type: :string } }, required: 'a')
322
+
323
+ assert_equal ['a'], definition[:function][:parameters][:required]
324
+ end
325
+
326
+ def test_builder_accepts_string_names
327
+ definition = LLM.tool_definition('task', 'A task', { 'a' => { type: :string } })
328
+
329
+ assert_equal 'task', definition[:name]
330
+ assert_equal 'task', definition[:function][:name]
331
+ end
332
+ end