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,665 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/lib/scout/llm/agent/path.rb')
3
+
4
+ require "tmpdir"
5
+
6
+
7
+ # ---------------------------------------------------------------------------
8
+ # Whole-workflow PATH_KINDS incorporation (`tool: Workflow` chat semantic).
9
+ #
10
+ # Mirrors lib/scout/llm/agent.rb (the `tool:` hook inside Agent#ask) and
11
+ # lib/scout/llm/agent/path.rb (`integrate_workflow_path_kinds`). Test-local
12
+ # workflow modules are defined at the bottom of the file and reused across
13
+ # these tests; the mock backend drives Agent#ask end to end offline
14
+ # (LLM::Mock, test/support/mock_backend.rb, backend :mock from
15
+ # test/test_helper.rb). Every test uses a fresh agent so no kind ever leaks
16
+ # between tests.
17
+ # ---------------------------------------------------------------------------
18
+ class TestPathWorkflowKindIntegration < Test::Unit::TestCase
19
+ def setup
20
+ @tmpdir = Scout.tmp.agent_path_kind_test.find
21
+ FileUtils.mkdir_p(@tmpdir)
22
+ end
23
+
24
+ def teardown
25
+ FileUtils.remove_entry(@tmpdir) if @tmpdir && File.exist?(@tmpdir)
26
+ end
27
+
28
+ def fresh_agent
29
+ LLM.agent
30
+ end
31
+
32
+ def ask_with(agent, chat_text)
33
+ LLM::Mock.script('ok')
34
+ agent.prompt LLM.chat(chat_text), persist: false, endpoint: 'mock', backend: :mock
35
+ end
36
+
37
+ # -- registration through whole-workflow `tool:` ---------------------------
38
+
39
+ def test_whole_workflow_tool_line_registers_kinds_and_installs_tools
40
+ # Back-compat, re-scoped (step 2 of the path_kind role): the whole-workflow
41
+ # `tool:` kind incorporation now happens only for an UNPREPARED agent (no
42
+ # start_chat file). With a prepared start_chat the same line configures
43
+ # tools only -- see test_start_chat_present_zero_kinds_from_tool_lines.
44
+ agent = LLM::Agent.new(workflow: TestPathKindWorkflowB)
45
+ assert !agent.start_chat_file?
46
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\ndo things")
47
+
48
+ assert agent.path_kind_registry.key?('wfa_kind')
49
+ assert_equal 'TestPathKindWorkflowA', agent.path_kind_sources['wfa_kind']
50
+
51
+ tool_names = (agent.other_options[:tools] || {}).keys
52
+ %w[path_kinds path_read path_write].each do |tool|
53
+ assert tool_names.include?(tool), "expected #{tool} in #{tool_names.inspect}"
54
+ end
55
+ end
56
+
57
+ def test_task_level_tool_line_registers_nothing
58
+ agent = fresh_agent
59
+ ask_with(agent, "tool: TestPathKindWorkflowB t\nuser\ndo things")
60
+
61
+ assert_empty agent.path_kind_registry
62
+ assert_empty agent.path_kind_sources
63
+ assert_empty (agent.other_options[:tools] || {}).keys
64
+ .select { |name| name.start_with?('path_') }
65
+ end
66
+
67
+ def test_workflow_without_path_kinds_is_silent_noop
68
+ agent = fresh_agent
69
+ assert_nil agent.integrate_workflow_path_kinds(TestPathKindWorkflowNone)
70
+
71
+ ask_with(agent, "tool: TestPathKindWorkflowNone\nuser\ndo things")
72
+ assert_empty agent.path_kind_registry
73
+ assert_empty agent.path_kind_sources
74
+ end
75
+
76
+ def test_merely_defining_the_module_registers_nothing
77
+ # Negative baseline: TestPathKindWorkflowA's PATH_KINDS exists in the
78
+ # constant table, but a fresh agent that never incorporates the workflow
79
+ # has an empty registry.
80
+ agent = fresh_agent
81
+ assert TestPathKindWorkflowA.const_defined?(:PATH_KINDS, false)
82
+ assert_empty agent.path_kind_registry
83
+ assert_empty agent.path_kind_sources
84
+ assert_empty (agent.other_options[:tools] || {}).keys
85
+ end
86
+
87
+ # -- conflict / idempotence -------------------------------------------------
88
+
89
+ def test_conflict_between_two_workflows_raises_naming_both
90
+ agent = fresh_agent
91
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\nfirst")
92
+
93
+ error = assert_raise(ParameterException) do
94
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowConflict)
95
+ end
96
+
97
+ assert_include error.message, 'wfa_kind'
98
+ assert_include error.message, 'TestPathKindWorkflowA'
99
+ assert_include error.message, 'TestPathKindWorkflowConflict'
100
+ # the first registration survives the rejected second one
101
+ assert_equal 'TestPathKindWorkflowA', agent.path_kind_sources['wfa_kind']
102
+ end
103
+
104
+ def test_same_workflow_repeated_is_idempotent
105
+ agent = fresh_agent
106
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\nfirst")
107
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\nsecond")
108
+
109
+ assert_equal ['wfa_kind'], agent.path_kind_registry.keys
110
+ assert_equal 'TestPathKindWorkflowA', agent.path_kind_sources['wfa_kind']
111
+
112
+ tool_names = (agent.other_options[:tools] || {}).keys
113
+ assert_equal tool_names, tool_names.uniq
114
+ assert_equal 1, tool_names.count('path_kinds')
115
+ end
116
+
117
+ # -- shape validation --------------------------------------------------------
118
+
119
+ def test_shape_error_names_workflow_and_class
120
+ agent = fresh_agent
121
+ error = assert_raise(ParameterException) do
122
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowBadShape)
123
+ end
124
+
125
+ assert_include error.message, 'TestPathKindWorkflowBadShape'
126
+ assert_include error.message, 'String'
127
+ end
128
+
129
+ def test_array_of_definition_hashes_is_accepted
130
+ agent = fresh_agent
131
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray)
132
+
133
+ assert agent.path_kind_registry.key?('arr_first')
134
+ assert agent.path_kind_registry.key?('arr_second')
135
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_first']
136
+ end
137
+
138
+ def test_entry_without_hash_definition_is_rejected
139
+ agent = fresh_agent
140
+ error = assert_raise(ParameterException) do
141
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowBadEntry)
142
+ end
143
+ assert_include error.message, 'TestPathKindWorkflowBadEntry'
144
+ end
145
+
146
+ # -- subset selection (`only:`) -----------------------------------------------
147
+
148
+ def test_without_selection_registers_every_kind_unchanged
149
+ # The all-kinds contract, pinned beside the subset behavior: nil and an
150
+ # empty list are both the historical call.
151
+ %w[nil empty_array].each do |variant|
152
+ agent = fresh_agent
153
+ case variant
154
+ when 'nil'
155
+ result = agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: nil)
156
+ when 'empty_array'
157
+ result = agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: [])
158
+ end
159
+ assert_equal %w[arr_first arr_second], result.keys.sort
160
+ assert_equal %w[arr_first arr_second], agent.path_kind_registry.keys.sort
161
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_first']
162
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_second']
163
+ end
164
+ end
165
+
166
+ def test_subset_registers_exactly_the_named_kinds
167
+ agent = fresh_agent
168
+ result = agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: ['arr_second'])
169
+
170
+ assert_equal ['arr_second'], agent.path_kind_registry.keys
171
+ assert_equal ['arr_second'], result.keys
172
+ assert_nil agent.path_kind_sources['arr_first']
173
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_second']
174
+
175
+ tool_names = (agent.other_options[:tools] || {}).keys
176
+ assert_include tool_names, 'path_read'
177
+ # the kind enum on the installed tools reflects the subset
178
+ kind_enum = (agent.other_options[:tools]['path_kinds'].last)[:parameters][:properties][:kind][:enum]
179
+ assert_equal ['arr_second'], kind_enum
180
+ end
181
+
182
+ def test_subset_accepts_a_single_name_and_symbols
183
+ agent = fresh_agent
184
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: :arr_first)
185
+
186
+ assert_equal ['arr_first'], agent.path_kind_registry.keys
187
+ end
188
+
189
+ def test_unknown_kind_in_selection_raises_naming_workflow_and_kind
190
+ agent = fresh_agent
191
+ error = assert_raise(ParameterException) do
192
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: ['arr_first', 'nope'])
193
+ end
194
+
195
+ assert_include error.message, 'TestPathKindWorkflowArray'
196
+ assert_include error.message, 'nope'
197
+ # cheap, welcome diagnostics: the valid names are listed
198
+ assert_include error.message, 'arr_second'
199
+ # the failed call registered nothing
200
+ assert_empty agent.path_kind_registry
201
+ assert_empty agent.path_kind_sources
202
+ end
203
+
204
+ def test_workflow_without_path_kinds_with_selection_raises
205
+ agent = fresh_agent
206
+ error = assert_raise(ParameterException) do
207
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowNone, only: ['anything'])
208
+ end
209
+
210
+ assert_include error.message, 'TestPathKindWorkflowNone'
211
+ assert_include error.message, 'anything'
212
+ assert_empty agent.path_kind_registry
213
+ end
214
+
215
+ def test_workflow_without_path_kinds_without_selection_stays_noop
216
+ # Existing contract restated beside the selection behavior: no
217
+ # PATH_KINDS, no selection -> silent nil, nothing registered.
218
+ agent = fresh_agent
219
+ assert_nil agent.integrate_workflow_path_kinds(TestPathKindWorkflowNone)
220
+ assert_nil agent.integrate_workflow_path_kinds(TestPathKindWorkflowNone, only: nil)
221
+ assert_nil agent.integrate_workflow_path_kinds(TestPathKindWorkflowNone, only: [])
222
+ assert_empty agent.path_kind_registry
223
+ assert_empty agent.path_kind_sources
224
+ end
225
+
226
+ def test_subset_repeated_is_idempotent_and_conflict_aware
227
+ # selection keeps the conflict bookkeeping: the same subset from the
228
+ # same workflow is idempotent, and a second workflow's kind still
229
+ # conflicts even when it is the selected one.
230
+ agent = fresh_agent
231
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: ['arr_first'])
232
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowArray, only: ['arr_first'])
233
+ assert_equal ['arr_first'], agent.path_kind_registry.keys
234
+
235
+ # unrelated names do not interfere: the unselected arr_first does not
236
+ # block another workflow's wfa_kind
237
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowA, only: ['wfa_kind'])
238
+ assert_equal %w[arr_first wfa_kind], agent.path_kind_registry.keys.sort
239
+
240
+ error = assert_raise(ParameterException) do
241
+ agent.integrate_workflow_path_kinds(TestPathKindWorkflowConflict, only: ['wfa_kind'])
242
+ end
243
+ assert_include error.message, 'wfa_kind'
244
+ assert_equal %w[arr_first wfa_kind], agent.path_kind_registry.keys.sort
245
+ end
246
+
247
+ # -- `path_kind:` chat role --------------------------------------------------
248
+
249
+ def test_path_kind_all_kinds_line_registers_and_installs_tools
250
+ agent = fresh_agent
251
+ ask_with(agent, "path_kind: TestPathKindWorkflowArray\nuser\ndo things")
252
+
253
+ assert_equal %w[arr_first arr_second], agent.path_kind_registry.keys.sort
254
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_first']
255
+
256
+ tool_names = (agent.other_options[:tools] || {}).keys
257
+ %w[path_kinds path_read path_write].each do |tool|
258
+ assert tool_names.include?(tool), "expected #{tool} in #{tool_names.inspect}"
259
+ end
260
+ end
261
+
262
+ def test_path_kind_subset_line_registers_only_the_named_kind
263
+ agent = fresh_agent
264
+ ask_with(agent, "path_kind: TestPathKindWorkflowArray arr_second\nuser\ndo things")
265
+
266
+ assert_equal ['arr_second'], agent.path_kind_registry.keys
267
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_second']
268
+ assert_nil agent.path_kind_sources['arr_first']
269
+ end
270
+
271
+ def test_path_kind_multi_kind_line_and_separate_lines_accumulate
272
+ agent = fresh_agent
273
+ ask_with(agent, "path_kind: TestPathKindWorkflowArray arr_first arr_second\npath_kind: TestPathKindWorkflowA\nuser\ndo things")
274
+
275
+ assert_equal %w[arr_first arr_second wfa_kind], agent.path_kind_registry.keys.sort
276
+ assert_equal 'TestPathKindWorkflowA', agent.path_kind_sources['wfa_kind']
277
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_first']
278
+ end
279
+
280
+ def test_path_kind_unknown_kind_raises_parameter_exception
281
+ agent = fresh_agent
282
+ error = assert_raise(ParameterException) do
283
+ ask_with(agent, "path_kind: TestPathKindWorkflowArray nope\nuser\ndo things")
284
+ end
285
+
286
+ assert_include error.message, 'TestPathKindWorkflowArray'
287
+ assert_include error.message, 'nope'
288
+ assert_empty agent.path_kind_registry
289
+ end
290
+
291
+ def test_path_kind_unloadable_workflow_raises
292
+ agent = fresh_agent
293
+ error = assert_raise(ParameterException) do
294
+ ask_with(agent, "path_kind: NoSuchWorkflowForPathKinds kind\nuser\ndo things")
295
+ end
296
+
297
+ assert_include error.message, 'NoSuchWorkflowForPathKinds'
298
+ assert_empty agent.path_kind_registry
299
+ end
300
+
301
+ def test_path_kind_line_honored_from_start_chat
302
+ # start_chat path: an agent built with an explicit prepared start_chat
303
+ # (start_chat_file? true) still honors an explicit path_kind line in it,
304
+ # while its whole-workflow `tool:` line stays tools-only.
305
+ agent = fresh_agent
306
+ agent.start_chat = LLM.chat("tool: TestPathKindWorkflowA\npath_kind: TestPathKindWorkflowArray arr_first\nsystem\nprepared")
307
+ assert agent.start_chat_file?
308
+
309
+ ask_with(agent, "user\ndo things")
310
+
311
+ assert_equal ['arr_first'], agent.path_kind_registry.keys
312
+ assert_equal 'TestPathKindWorkflowArray', agent.path_kind_sources['arr_first']
313
+ end
314
+
315
+ def test_start_chat_present_zero_kinds_from_tool_lines
316
+ agent = fresh_agent
317
+ agent.start_chat = LLM.chat("tool: TestPathKindWorkflowA\nsystem\nprepared")
318
+ assert agent.start_chat_file?
319
+
320
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\ndo things")
321
+
322
+ assert_empty agent.path_kind_registry
323
+ assert_empty agent.path_kind_sources
324
+ end
325
+
326
+ def test_start_chat_file_false_falls_back_to_load_all
327
+ # Unprepared agent (no start_chat at all): its own workflow's PATH_KINDS
328
+ # load implicitly.
329
+ agent = LLM::Agent.new(workflow: TestPathKindWorkflowA)
330
+ assert !agent.start_chat_file?
331
+
332
+ ask_with(agent, "user\ndo things")
333
+
334
+ assert_equal ['wfa_kind'], agent.path_kind_registry.keys
335
+ assert_equal 'TestPathKindWorkflowA', agent.path_kind_sources['wfa_kind']
336
+ end
337
+
338
+ def test_synthetic_introduce_start_chat_still_loads_all
339
+ # load_agent's synthetic `introduce` fallback is NOT a start_chat file:
340
+ # implicit loading stays on (the flag load_agent sets is false).
341
+ agent = LLM::Agent.new(workflow: TestPathKindWorkflowA,
342
+ start_chat: Chat.setup([{role: 'introduce', content: 'TestPathKindWorkflowA'}]))
343
+ agent.start_chat_file = false
344
+ assert !agent.start_chat_file?
345
+
346
+ ask_with(agent, "user\ndo things")
347
+
348
+ assert_equal ['wfa_kind'], agent.path_kind_registry.keys
349
+ end
350
+
351
+ def test_path_kind_content_never_reaches_the_model
352
+ # Mock-backend proof: registration happened AND no path_kind content is
353
+ # model-visible in any round the backend recorded.
354
+ agent = fresh_agent
355
+ ask_with(agent, "path_kind: TestPathKindWorkflowArray arr_first\nuser\ndo things")
356
+
357
+ assert_equal ['arr_first'], agent.path_kind_registry.keys
358
+ LLM::Mock.calls.each do |round_messages, _options|
359
+ assert !round_messages.any? { |m| m[:role].to_s == 'path_kind' },
360
+ "path_kind content reached the model: #{round_messages.collect{|m| m[:role]}.inspect}"
361
+ end
362
+ end
363
+
364
+ def test_clear_tools_does_not_bypass_path_kind_registration
365
+ # clear_tools strips earlier tool-call traces for the MODEL, but path_kind
366
+ # registration is driven from the full message list before any stripping.
367
+ agent = fresh_agent
368
+ ask_with(agent, "clear_tools\npath_kind: TestPathKindWorkflowArray arr_second\nuser\ndo things")
369
+
370
+ assert_equal ['arr_second'], agent.path_kind_registry.keys
371
+ end
372
+
373
+ # -- namespace locality -------------------------------------------------------
374
+
375
+ def test_nested_module_does_not_inherit_parent_path_kinds
376
+ agent = fresh_agent
377
+ assert TestPathKindParent::PATH_KINDS.is_a?(Hash)
378
+ # nesting: child has no namespace-local PATH_KINDS
379
+ assert !TestPathKindParent::Child.const_defined?(:PATH_KINDS, false)
380
+
381
+ assert_nil agent.integrate_workflow_path_kinds(TestPathKindParent::Child)
382
+ assert_empty agent.path_kind_registry
383
+ end
384
+
385
+ def test_subclass_does_not_inherit_parent_path_kinds
386
+ agent = fresh_agent
387
+ # const_defined? without the inherit flag WOULD see the parent constant
388
+ assert TestPathKindSubclassed.const_defined?(:PATH_KINDS)
389
+ assert !TestPathKindSubclassed.const_defined?(:PATH_KINDS, false)
390
+
391
+ assert_nil agent.integrate_workflow_path_kinds(TestPathKindSubclassed)
392
+ assert_empty agent.path_kind_registry
393
+ end
394
+
395
+ def test_remote_workflow_namespace_returns_nil
396
+ require 'rbbt'
397
+ require 'scout/offsite/ssh'
398
+ require 'rbbt/workflow/remote_workflow'
399
+
400
+ agent = fresh_agent
401
+ assert_nil agent.integrate_workflow_path_kinds(RemoteWorkflow)
402
+ assert_empty agent.path_kind_registry
403
+ end
404
+
405
+ def test_anonymous_module_returns_nil
406
+ agent = fresh_agent
407
+ anonymous = Module.new { extend Workflow }
408
+ assert_nil agent.integrate_workflow_path_kinds(anonymous)
409
+ assert_empty agent.path_kind_registry
410
+ end
411
+
412
+ def test_non_module_object_returns_nil
413
+ agent = fresh_agent
414
+ assert_nil agent.integrate_workflow_path_kinds('TestPathKindWorkflowA')
415
+ assert_nil agent.integrate_workflow_path_kinds(nil)
416
+ assert_empty agent.path_kind_registry
417
+ end
418
+
419
+ # -- frozen constant isolation --------------------------------------------------
420
+
421
+ def test_frozen_path_kinds_constant_is_never_mutated
422
+ agent = fresh_agent
423
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\ndo things")
424
+
425
+ constant = TestPathKindWorkflowA::PATH_KINDS
426
+ # --- nested-structure isolation (both directions) ---
427
+ # Mutating the CONSTANT's nested structures after registration must not
428
+ # reach the agent's registered copy...
429
+ constant['wfa_kind']['description'] = 'CONST-NESTED-MUTATION'
430
+ registered = agent.path_kind_registry['wfa_kind']
431
+ assert_equal 'Kind exported by TestPathKindWorkflowA', registered['description']
432
+ assert !registered['roots'].equal?(constant['wfa_kind']['roots'])
433
+ assert !registered['locations'].equal?(constant['wfa_kind']['locations'])
434
+ original = constant['wfa_kind'].dup
435
+
436
+ registered = agent.path_kind_registry['wfa_kind']
437
+ refute_same constant['wfa_kind'], registered
438
+ refute_same constant, registered
439
+
440
+ # mutating the registered copy leaves the constant untouched
441
+ registered['description'] = 'mutated through the registry'
442
+ assert_equal original['description'], constant['wfa_kind']['description']
443
+
444
+ # re-registration does not write "name" or normalization back either
445
+ ask_with(agent, "tool: TestPathKindWorkflowA\nuser\nagain")
446
+ assert_nil constant['wfa_kind']['name']
447
+ assert_equal original, constant['wfa_kind']
448
+ end
449
+
450
+ # -- `path:` chat role removal ---------------------------------------------------
451
+
452
+ def test_path_role_no_longer_installs_path_tools
453
+ agent = fresh_agent
454
+ ask_with(agent, "path: true\nuser\nhello")
455
+
456
+ assert_empty agent.path_kind_registry
457
+ assert_empty (agent.other_options[:tools] || {}).keys
458
+ end
459
+
460
+ def test_path_role_survives_in_messages_unconsumed
461
+ # Observed behavior after the role removal: the capability loop ignores
462
+ # `path:` lines, so the message stays in the chat and is delivered to the
463
+ # backend together with the user message (the mock records every round).
464
+ agent = fresh_agent
465
+ LLM::Mock.script('ok')
466
+ agent.prompt LLM.chat("path: true\nuser\nhello"),
467
+ persist: false, endpoint: 'mock', backend: :mock
468
+
469
+ roles = LLM::Mock.calls.first.first.collect { |m| m[:role].to_s }
470
+ assert_include roles, 'path'
471
+ end
472
+
473
+ def test_agent_path_is_a_plain_accessor_again
474
+ agent = fresh_agent
475
+ agent.path = File.join(@tmpdir, 'agent_path')
476
+ assert_equal File.join(@tmpdir, 'agent_path'), agent.path
477
+ end
478
+
479
+ def test_capability_loop_still_handles_socialize_and_attachments
480
+ # the array shrunk to exactly two capabilities; the dispatch helper
481
+ # `path` is gone and `Agent#path` is the plain accessor again (its
482
+ # source is the attr_accessor at agent.rb, not agent/path.rb)
483
+ assert_equal ['lib/scout/llm/agent.rb'],
484
+ LLM::Agent.instance_method(:path).source_location[0..0]
485
+ .collect { |f| f.sub(%r{.*/scout-ai/}, '') }
486
+ assert LLM::Agent.instance_methods.include?(:socialize)
487
+ assert LLM::Agent.instance_methods.include?(:attachments)
488
+ end
489
+
490
+ # -- skipped incorporations --------------------------------------------------
491
+
492
+ def test_unresolvable_workflow_name_is_skipped_silently
493
+ # The hook itself rescues the lookup and skips PATH_KINDS handling; the
494
+ # later backend tool resolution then legitimately raises because no such
495
+ # workflow exists anywhere. We drive only the hook (the same block
496
+ # Agent#ask runs, lifted verbatim) to pin the skip behavior.
497
+ agent = fresh_agent
498
+ messages = Chat.setup(LLM.chat("tool: NoSuchWorkflowForPathKinds\nuser\ndo things"))
499
+ hook = lambda do |msgs|
500
+ Chat.find_role(msgs, :tool).each do |message|
501
+ workflow_name, task_name, *_inputs = Chat.content_tokens(message)
502
+ next if task_name
503
+ next if workflow_name.nil? || workflow_name.empty?
504
+ next if Open.remote? workflow_name
505
+ begin
506
+ workflow = Chat.load_workflow workflow_name
507
+ rescue
508
+ next
509
+ end
510
+ agent.integrate_workflow_path_kinds(workflow)
511
+ end
512
+ end
513
+ assert_nothing_raised { hook.call(messages) }
514
+
515
+ assert_empty agent.path_kind_registry
516
+ assert_empty agent.path_kind_sources
517
+ end
518
+
519
+ def test_remote_tool_line_is_skipped_by_path_kinds_hook
520
+ agent = fresh_agent
521
+ messages = Chat.setup(LLM.chat("tool: https://127.0.0.1:1/WF\nuser\ndo things"))
522
+ hook = lambda do |msgs|
523
+ Chat.find_role(msgs, :tool).each do |message|
524
+ workflow_name, task_name, *_inputs = Chat.content_tokens(message)
525
+ next if task_name
526
+ next if workflow_name.nil? || workflow_name.empty?
527
+ next if Open.remote? workflow_name
528
+ begin
529
+ workflow = Chat.load_workflow workflow_name
530
+ rescue
531
+ next
532
+ end
533
+ agent.integrate_workflow_path_kinds(workflow)
534
+ end
535
+ end
536
+ assert_nothing_raised { hook.call(messages) }
537
+
538
+ assert_empty agent.path_kind_registry
539
+ assert_empty agent.path_kind_sources
540
+ end
541
+ end
542
+
543
+ # Test-local workflow modules used above. They are defined OUTSIDE the test
544
+ # class (same technique as TestBaking in test/scout/llm/agent/test_workflow.rb
545
+ # and test/scout/llm/test_chat.rb): `tool:`/`introduce:` resolve names through
546
+ # Kernel.const_get first, so a named module is reachable without any file on
547
+ # disk and without touching the real workflow search path.
548
+ $test_path_kind_root ||= Path.setup(File.join(Scout.tmp.agent_path_kind_test.find.to_s, 'roots')).find
549
+
550
+ module TestPathKindWorkflowA
551
+ extend Workflow
552
+ self.name = 'TestPathKindWorkflowA'
553
+
554
+ PATH_KINDS = {
555
+ 'wfa_kind' => {
556
+ 'description' => 'Kind exported by TestPathKindWorkflowA',
557
+ 'locations' => ['tmp'],
558
+ 'roots' => {'tmp' => $test_path_kind_root},
559
+ 'capabilities' => {'read' => true, 'write' => true}
560
+ }
561
+ }.freeze
562
+ end
563
+
564
+ module TestPathKindWorkflowB
565
+ extend Workflow
566
+ self.name = 'TestPathKindWorkflowB'
567
+
568
+ desc 'Sample task'
569
+ input :value, :string
570
+ task :t => :string do |value| value end
571
+
572
+ PATH_KINDS = {
573
+ 'wfb_kind' => {
574
+ 'description' => 'Kind exported by TestPathKindWorkflowB',
575
+ 'locations' => ['tmp'],
576
+ 'roots' => {'tmp' => $test_path_kind_root}
577
+ }
578
+ }.freeze
579
+ end
580
+
581
+ module TestPathKindWorkflowNone
582
+ extend Workflow
583
+ self.name = 'TestPathKindWorkflowNone'
584
+
585
+ desc 'Sample task'
586
+ input :value, :string
587
+ task :t => :string do |value| value end
588
+ end
589
+
590
+ module TestPathKindWorkflowConflict
591
+ extend Workflow
592
+ self.name = 'TestPathKindWorkflowConflict'
593
+
594
+ PATH_KINDS = {
595
+ 'wfa_kind' => {
596
+ 'description' => 'Conflicting kind from another workflow',
597
+ 'locations' => ['tmp'],
598
+ 'roots' => {'tmp' => $test_path_kind_root}
599
+ }
600
+ }.freeze
601
+ end
602
+
603
+ module TestPathKindWorkflowBadShape
604
+ extend Workflow
605
+ self.name = 'TestPathKindWorkflowBadShape'
606
+
607
+ PATH_KINDS = 'not-a-mapping'
608
+ end
609
+
610
+ module TestPathKindWorkflowBadEntry
611
+ extend Workflow
612
+ self.name = 'TestPathKindWorkflowBadEntry'
613
+
614
+ PATH_KINDS = {'broken' => 'not-a-hash'}
615
+ end
616
+
617
+ module TestPathKindWorkflowArray
618
+ extend Workflow
619
+ self.name = 'TestPathKindWorkflowArray'
620
+
621
+ PATH_KINDS = [
622
+ {
623
+ 'name' => 'arr_first',
624
+ 'description' => 'First array entry',
625
+ 'locations' => ['tmp'],
626
+ 'roots' => {'tmp' => $test_path_kind_root}
627
+ },
628
+ {
629
+ 'name' => 'arr_second',
630
+ 'description' => 'Second array entry',
631
+ 'locations' => ['tmp'],
632
+ 'roots' => {'tmp' => $test_path_kind_root}
633
+ }
634
+ ].freeze
635
+ end
636
+
637
+ module TestPathKindParent
638
+ PATH_KINDS = {
639
+ 'parent_kind' => {
640
+ 'description' => 'Parent constant, must not be discovered',
641
+ 'locations' => ['tmp'],
642
+ 'roots' => {'tmp' => $test_path_kind_root}
643
+ }
644
+ }.freeze
645
+
646
+ module Child
647
+ extend Workflow
648
+ self.name = 'TestPathKindParent::Child'
649
+ end
650
+ end
651
+
652
+ class TestPathKindBase
653
+ PATH_KINDS = {
654
+ 'base_kind' => {
655
+ 'description' => 'Base class constant, must not be discovered',
656
+ 'locations' => ['tmp'],
657
+ 'roots' => {'tmp' => $test_path_kind_root}
658
+ }
659
+ }.freeze
660
+ end
661
+
662
+ class TestPathKindSubclassed < TestPathKindBase
663
+ extend Workflow
664
+ self.name = 'TestPathKindSubclassed'
665
+ end