scout-ai 2.0.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 (185) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +135 -55
  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 +320 -34
  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 +402 -44
  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 +64 -0
  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 +79 -17
  39. data/lib/scout/llm/agent.rb +151 -27
  40. data/lib/scout/llm/ask.rb +37 -27
  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 +131 -32
  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 -52
  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 +352 -626
  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 +72 -26
  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 +845 -80
  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 +109 -22
  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 +200 -28
  127. data/test/scout/llm/chat/test_agent_meta_tokens.rb +112 -7
  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 +708 -13
  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 +71 -25
  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/synthesis-report.md +0 -487
  185. data/research/tools-system-analysis.md +0 -779
@@ -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
@@ -0,0 +1,211 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/llm/tools/call'
3
+
4
+ # Lifecycle guards for the live-workload sidecar `<base>.jobs` written by
5
+ # LLM.process_calls while workflow jobs are in flight under a chat save_file.
6
+ #
7
+ # Semantics under test (canonical, fixed):
8
+ # * the file is a SIBLING of the save_file, derived by Chat.jobs_file
9
+ # (trailing '.chat' strip only);
10
+ # * it is WRITTEN just before Workflow.produce with the short paths of the
11
+ # jobs not yet done (newline-joined), one write per round: a snapshot of
12
+ # the current in-flight set, not an append log;
13
+ # * it is REMOVED in an ensure once Workflow.produce returns -- whether the
14
+ # jobs finished or one of them failed;
15
+ # * no file is created at all when save_file is nil.
16
+ #
17
+ # The observation trick: the child task runs in a fork under
18
+ # Workflow::LocalExecutor, so in-process globals do NOT propagate from it.
19
+ # Instead the task block itself appends a line to an observations file that
20
+ # lives next to the save_file; the writer has necessarily run before the task
21
+ # block starts (it runs before Workflow.produce).
22
+ class TestLLMToolCallJobsFile < Test::Unit::TestCase
23
+ # SlowChildWF: a real (mock-free) workflow whose tasks sleep, then record
24
+ # what the sidecar looked like from inside produce. Path.join is used so the
25
+ # observations file does not depend on the workflow jobs directory.
26
+ # Observation directory for the child tasks: set as a PROCESS GLOBAL
27
+ # before run_round. The task block may run in a fork under
28
+ # Workflow::LocalExecutor, but a fork copies the parent's memory, so the
29
+ # child sees it; the reverse (child writing memory) does not propagate,
30
+ # which is why observations go to FILES. A workflow INPUT would not work
31
+ # here: Scout path-resolves plain string inputs and relocates absolute
32
+ # tmpdir values under the workflow's file lookup paths.
33
+ def self.obs_dir; @obs_dir; end
34
+ def self.obs_dir=(v); @obs_dir = v; end
35
+
36
+ SlowChildWF = Module.new do
37
+ extend Workflow
38
+ self.name = "SlowChildWF"
39
+
40
+ task :slow => :string do
41
+ sleep 0.2
42
+ SlowChildWF.record_sidecar_observation(TestLLMToolCallJobsFile.obs_dir, 'slow')
43
+ 'done'
44
+ end
45
+
46
+ task :fail => :string do
47
+ SlowChildWF.record_sidecar_observation(TestLLMToolCallJobsFile.obs_dir, 'fail')
48
+ raise 'child failed'
49
+ end
50
+
51
+ # Each child writes its own marker (no lost-update race) snapshotting the
52
+ # sidecar content it can see; it runs under Workflow.produce, i.e. after
53
+ # the writer fired.
54
+ def self.record_sidecar_observation(dir, name)
55
+ dir = dir.to_s
56
+ return if dir.empty?
57
+ save_file = File.join(dir, 'agent.chat')
58
+ jobs = Open.exists?(Chat.jobs_file(save_file)) ? Open.read(Chat.jobs_file(save_file)) : ''
59
+ Open.write(File.join(dir, name.to_s + '.obs'), jobs)
60
+ Open.write(File.join(dir, name.to_s + '.seen'), jobs)
61
+ end
62
+ end
63
+
64
+ # `job` tool shape used by LLM.process_calls: {name => [obj, definition]}.
65
+ # The Proc returns a Step (not-yet-done), exactly like LLM.call_workflow
66
+ # for a non-exec workflow task; process_calls then produces them.
67
+ def slow_child_tool(wf, task)
68
+ proc do |_name, args|
69
+ wf.job(task.to_sym, args[:jobname], {})
70
+ end
71
+ end
72
+
73
+ # Point the fork-inherited observation variable at dir and yield. A class
74
+ # ivar (not a const) so reassignment does not warn; forks still copy it.
75
+ def with_obs_dir(dir)
76
+ old = TestLLMToolCallJobsFile.obs_dir
77
+ TestLLMToolCallJobsFile.obs_dir = dir
78
+ yield
79
+ ensure
80
+ TestLLMToolCallJobsFile.obs_dir = old
81
+ end
82
+
83
+ def tool_call(id, name, arguments)
84
+ IndiferentHash.setup({
85
+ 'id' => id, 'type' => 'function',
86
+ 'function' => {'name' => name, 'arguments' => arguments.to_json}
87
+ })
88
+ end
89
+
90
+ def read_sidecar(save_file)
91
+ path = Chat.jobs_file(save_file)
92
+ Open.exists?(path) ? Open.read(path) : nil
93
+ end
94
+
95
+ # Drive one process_calls round directly (no LLM.ask, no mock backend): the
96
+ # sidecar writer is inside process_calls, which is exactly the unit here.
97
+ def run_round(save_file, tools, calls)
98
+ LLM.process_calls(tools, calls, save_file: save_file)
99
+ end
100
+
101
+ def test_jobs_file_exists_while_child_in_flight_and_lists_its_short_path
102
+ save_dir = File.join(tmpdir, 'jobs_single')
103
+ FileUtils.mkdir_p save_dir
104
+ save_file = File.join(save_dir, 'agent.chat')
105
+
106
+ with_obs_dir(save_dir) do
107
+ run_round(save_file,
108
+ {'slow' => [slow_child_tool(SlowChildWF, :slow), nil]},
109
+ [tool_call('c1', 'slow', {jobname: 'single'})])
110
+ end
111
+
112
+ # The child OBSERVED the sidecar from inside produce: a marker file was
113
+ # written by the forked task (named after its Step, hence the glob).
114
+ seen = Dir.glob(File.join(save_dir, '*.seen'))
115
+ assert !seen.empty?, 'child task never ran (no *.seen marker)'
116
+
117
+ # ... and it saw exactly the child's short path (newline-separated set,
118
+ # single entry here, no extra decoration).
119
+ assert_equal [SlowChildWF.job(:slow, 'single', {}).short_path],
120
+ Open.read(seen.first).split("\n")
121
+ end
122
+
123
+ def test_jobs_file_removed_after_children_complete
124
+ save_dir = File.join(tmpdir, 'jobs_after')
125
+ FileUtils.mkdir_p save_dir
126
+ save_file = File.join(save_dir, 'agent.chat')
127
+
128
+ with_obs_dir(save_dir) do
129
+ run_round(save_file,
130
+ {'slow' => [slow_child_tool(SlowChildWF, :slow), nil]},
131
+ [tool_call('c1', 'slow', {jobname: 'after'})])
132
+ end
133
+
134
+ assert !Open.exists?(Chat.jobs_file(save_file)),
135
+ "<base>.jobs must be removed once Workflow.produce returns"
136
+ end
137
+
138
+ def test_jobs_file_removed_when_produce_raises
139
+ save_dir = File.join(tmpdir, 'jobs_fail')
140
+ FileUtils.mkdir_p save_dir
141
+ save_file = File.join(save_dir, 'agent.chat')
142
+
143
+ # process_calls rescues around produce, so the round returns normally;
144
+ # the child raised inside the task block (the failure surfaces as the
145
+ # error envelope in the function_call_output).
146
+ result = with_obs_dir(save_dir) do
147
+ run_round(save_file,
148
+ {'fail' => [slow_child_tool(SlowChildWF, :fail), nil]},
149
+ [tool_call('c1', 'fail', {jobname: 'boom'})])
150
+ end
151
+
152
+ # The failing child still ran (and observed) before raising.
153
+ assert !Dir.glob(File.join(save_dir, '*.seen')).empty?,
154
+ 'failing child never ran'
155
+
156
+ # The ensure removed the sidecar even though produce ended in an error.
157
+ assert !Open.exists?(Chat.jobs_file(save_file)),
158
+ "<base>.jobs must be removed when Workflow.produce fails"
159
+
160
+ assert_equal 1, result.select{|m| m[:role].to_s == 'function_call_output' }.length
161
+ end
162
+
163
+ def test_jobs_file_lists_two_in_flight_children
164
+ save_dir = File.join(tmpdir, 'jobs_two')
165
+ FileUtils.mkdir_p save_dir
166
+ save_file = File.join(save_dir, 'agent.chat')
167
+
168
+ with_obs_dir(save_dir) do
169
+ run_round(save_file,
170
+ {'slow' => [slow_child_tool(SlowChildWF, :slow), nil]},
171
+ [tool_call('c1', 'slow', {jobname: 'pair_a'}),
172
+ tool_call('c2', 'slow', {jobname: 'pair_b'})])
173
+ end
174
+
175
+ expected = [SlowChildWF.job(:slow, 'pair_a', {}).short_path,
176
+ SlowChildWF.job(:slow, 'pair_b', {}).short_path].sort
177
+
178
+ # BOTH children ran under produce (both marker files exist) and the
179
+ # snapshot the LAST one to observe saw listed BOTH in-flight jobs (the
180
+ # two forks write the same file name 'slow.seen'; each sees the full
181
+ # pre-produce snapshot, which already contains both short paths).
182
+ seen_files = Dir.glob(File.join(save_dir, '*.seen'))
183
+ assert !seen_files.empty?, 'no child task ever ran (no *.seen marker)'
184
+ seen_files.each do |file|
185
+ assert_equal expected, Open.read(file).split("\n").sort,
186
+ "child snapshot #{File.basename(file)} did not list both in-flight jobs"
187
+ end
188
+ end
189
+
190
+ def test_no_jobs_file_created_when_save_file_is_nil
191
+ wf_dir = Workflow.directory['SlowChildWF']
192
+ before = Open.exists?(wf_dir) ? Dir.glob(File.join(wf_dir, '**', '*.jobs')) : []
193
+
194
+ obs = File.join(tmpdir, 'obs_nil')
195
+ FileUtils.mkdir_p obs
196
+ with_obs_dir(obs) do
197
+ run_round(nil,
198
+ {'slow' => [slow_child_tool(SlowChildWF, :slow), nil]},
199
+ [tool_call('c1', 'slow', {jobname: 'nil_save'})])
200
+ end
201
+
202
+ after = Dir.glob(File.join(wf_dir, '**', '*.jobs'))
203
+
204
+ # The child still ran (its marker file was written) but no .jobs file
205
+ # was ever created anywhere in the jobs tree, since the writer is a
206
+ # no-op without a save_file.
207
+ assert !Dir.glob(File.join(obs, '*.obs')).empty?, 'child never ran'
208
+ assert_equal before.sort, after.sort
209
+ assert(!after.any?{|p| p.end_with?('.jobs') })
210
+ end
211
+ end
@@ -1,13 +1,42 @@
1
1
  require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
2
  require File.expand_path(__FILE__).sub(%r(.*/test/), '').sub(/test_(.*)\.rb/,'\1')
3
3
 
4
- class TestClass < Test::Unit::TestCase
5
- # Real remote MCP server version moved to
6
- # test/integration/scout/llm/tools/test_mcp.rb: LLM.mcp_tools needs a live
7
- # MCP server (no seam to fake). Stdio coverage stays unit-side in
8
- # test/scout/llm/test_mcp.rb.
9
- def _test_client
10
- c = LLM.mcp_tools("https://api.githubcopilot.com/mcp/")
11
- assert_include c.keys, "get_me"
4
+ require 'scout/llm/mcp'
5
+ require 'json'
6
+
7
+ module TestMCPServingWorkflow
8
+ extend Workflow
9
+ self.name = "TestMCPServingWorkflow"
10
+
11
+ desc "Greet someone"
12
+ input :name, :string, "string", "Who to greet"
13
+ task :greet => :string do |name| "hello #{name}" end
14
+ export :greet
15
+ end
16
+
17
+ class TestLLMToolMCP < Test::Unit::TestCase
18
+ def setup
19
+ @server = TestMCPServingWorkflow.mcp(:greet)
20
+ end
21
+
22
+ def request(method, params)
23
+ JSON.parse @server.handle_json({'jsonrpc' => '2.0', 'id' => 1, 'method' => method, 'params' => params}.to_json)
24
+ end
25
+
26
+ def test_mcp_serves_task_as_string_named_tool
27
+ response = request('tools/list', {})
28
+ assert_equal ['greet'], response['result']['tools'].collect{|t| t['name']}
29
+ end
30
+
31
+ def test_mcp_tool_call_dispatches_to_workflow
32
+ response = request('tools/call', {'name' => 'greet', 'arguments' => {'name' => 'World'}})
33
+ assert_equal 'hello World', response['result']['content'].first['text']
34
+ assert ! response['result']['isError']
35
+ end
36
+
37
+ def test_mcp_default_task_set_registers_string_names
38
+ server = TestMCPServingWorkflow.mcp
39
+ tool = server.instance_variable_get(:@tools)['greet']
40
+ assert(tool, "default task set should register the greet tool by string name")
12
41
  end
13
42
  end
@@ -56,5 +56,49 @@ class TestLLMToolWorkflow < Test::Unit::TestCase
56
56
 
57
57
  assert_equal "30 minutes", LLM.call_workflow(m, :step_time, step: 'bake', exec_type: 'exec')
58
58
  end
59
- end
60
59
 
60
+ def test_refresh_applies_to_ordinary_tasks_before_return_semantics
61
+ calls = 0
62
+ workflow = Module.new do
63
+ extend Workflow
64
+ self.name = "RefreshWorkflow"
65
+ input :value, :string
66
+ task :count => :integer do |_value|
67
+ calls += 1
68
+ end
69
+ end
70
+
71
+ first = LLM.call_workflow(workflow, :count, value: 'same')
72
+ first.produce
73
+ assert_equal 1, first.load
74
+
75
+ refreshed = LLM.call_workflow(workflow, :count, value: 'same', refresh: 'refresh')
76
+ assert(Step === refreshed)
77
+ refreshed.produce
78
+ assert_equal 2, refreshed.load
79
+
80
+ refreshed_path = LLM.call_workflow(workflow, :count, value: 'same', return_path: true, refresh: 'refresh')
81
+ assert_equal 3, Open.read(refreshed_path).to_i
82
+
83
+ deeply_refreshed_path = LLM.call_workflow(workflow, :count, value: 'same', return_path: true, refresh: 'deep_refresh')
84
+ assert_equal 4, Open.read(deeply_refreshed_path).to_i
85
+ end
86
+
87
+ def test_exported_task_return_semantics
88
+ workflow = Module.new do
89
+ extend Workflow
90
+ self.name = "ExportedWorkflow"
91
+ input :value, :string
92
+ task :exported_value => :string do |value|
93
+ "exported-#{value}"
94
+ end
95
+ export_exec :exported_value
96
+ end
97
+
98
+ assert_equal "exported-result", LLM.call_workflow(workflow, :exported_value, value: 'result')
99
+
100
+ path = LLM.call_workflow(workflow, :exported_value, value: 'result', return_path: true)
101
+ assert_kind_of String, path
102
+ assert_equal "exported-result", Open.read(path)
103
+ end
104
+ end