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,249 @@
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
+ require 'fileutils'
5
+ require 'tmpdir'
6
+ # sandbox is loaded through scout/llm/chat/process/tools in the public API path.
7
+ require 'scout/llm/chat'
8
+
9
+ class TestSandbox < Test::Unit::TestCase
10
+ def setup
11
+ super
12
+ @root = Dir.mktmpdir('scout-ai-sandbox-')
13
+ %w[root/nested read_grant write_grant outside].each do |directory|
14
+ FileUtils.mkdir_p(File.join(@root, directory))
15
+ end
16
+ end
17
+
18
+ def teardown
19
+ FileUtils.rm_rf(@root) if @root
20
+ super
21
+ end
22
+
23
+ def test_grant_registration_and_chat_compatibility
24
+ Thread.current['allowed_paths'] = nil
25
+ Thread.current['allowed_read_paths'] = nil
26
+
27
+ Chat.allow_path('raw/path')
28
+ Chat.allow_path('raw/path')
29
+ Chat.allow_read_path('read/raw')
30
+ Chat.allow_read_path('read/raw')
31
+
32
+ assert_equal ['raw/path'], Thread.current['allowed_paths']
33
+ assert_equal ['read/raw'], Thread.current['allowed_read_paths']
34
+
35
+ job = Struct.new(:path, :info_file, :files_dir).new('job/path', 'job/info', 'job/files')
36
+ Chat.allow_job(job)
37
+ Chat.allow_read_job(job)
38
+ assert_equal ['raw/path', 'job/path', 'job/info', 'job/files'], Thread.current['allowed_paths']
39
+ assert_equal ['read/raw', 'job/path', 'job/info', 'job/files'], Thread.current['allowed_read_paths']
40
+ ensure
41
+ Thread.current['allowed_paths'] = nil
42
+ Thread.current['allowed_read_paths'] = nil
43
+ end
44
+
45
+ def test_authorization_uses_root_and_nested_boundary_not_sibling_prefix
46
+ root = File.join(@root, 'root')
47
+ assert_allowed File.join(root, 'nested'), root: root
48
+ assert_allowed root, root: root
49
+ decision = LLM::Sandbox.authorize_path(File.join(@root, 'root-sibling'), root: root)
50
+ assert_false decision.allowed
51
+ assert_equal :outside_grants, decision.reason
52
+ end
53
+
54
+ def test_relative_paths_require_an_explicit_base
55
+ assert_raise(ParameterException) do
56
+ LLM::Sandbox.authorize_path('root/nested', root: File.join(@root, 'root'), mode: :write)
57
+ end
58
+
59
+ decision = LLM::Sandbox.authorize_path('nested', root: File.join(@root, 'root'),
60
+ base_path: File.join(@root, 'root'), mode: :write)
61
+ assert_true decision.allowed
62
+ end
63
+
64
+ def test_read_and_write_grants_have_distinct_semantics
65
+ target = File.join(@root, 'read_grant', 'document.txt')
66
+ File.write(target, 'read me')
67
+ read_decision = LLM::Sandbox.authorize_path(target, root: File.join(@root, 'root'),
68
+ read_paths: [File.join(@root, 'read_grant')], mode: :read)
69
+ write_decision = LLM::Sandbox.authorize_path(target, root: File.join(@root, 'root'),
70
+ read_paths: [File.join(@root, 'read_grant')], mode: :write)
71
+ assert_true read_decision.allowed
72
+ assert_equal :read, read_decision.grant
73
+ assert_false write_decision.allowed
74
+ end
75
+
76
+ def test_existing_symlink_cannot_escape_a_grant
77
+ grant = File.join(@root, 'write_grant')
78
+ link = File.join(grant, 'escape')
79
+ File.symlink(File.join(@root, 'outside'), link)
80
+
81
+ decision = LLM::Sandbox.authorize_path(File.join(link, 'secret.txt'), root: File.join(@root, 'root'),
82
+ writable_paths: [grant], mode: :write)
83
+ assert_false decision.allowed
84
+ assert_equal File.join(@root, 'outside', 'secret.txt'), decision.canonical_path
85
+ end
86
+
87
+ def test_missing_target_under_symlinked_parent_is_resolved_before_authorization
88
+ grant = File.join(@root, 'write_grant')
89
+ link = File.join(grant, 'to_outside')
90
+ File.symlink(File.join(@root, 'outside'), link)
91
+ target = File.join(link, 'new.txt')
92
+
93
+ decision = LLM::Sandbox.authorize_path(target, root: File.join(@root, 'root'),
94
+ writable_paths: [grant], mode: :write)
95
+ assert_false decision.allowed
96
+ assert_false decision.exists
97
+ assert_equal File.join(@root, 'outside', 'new.txt'), decision.canonical_path
98
+ end
99
+
100
+ def test_missing_descendant_within_grant_is_allowed_and_marked_missing
101
+ root = File.join(@root, 'root')
102
+ target = File.join(root, 'nested', 'not-yet-created')
103
+ decision = LLM::Sandbox.authorize_path(target, root: root, mode: :write)
104
+ assert_true decision.allowed
105
+ assert_false decision.exists
106
+ assert_equal :root, decision.grant
107
+ end
108
+
109
+ def test_parent_component_requires_existing_preceding_directory
110
+ root = File.join(@root, 'root')
111
+ file = File.join(root, 'regular-file')
112
+ File.write(file, 'not a directory')
113
+
114
+ invalid = LLM::Sandbox.authorize_path(File.join(file, '..', 'child'), root: root, mode: :write)
115
+ assert_false invalid.allowed
116
+ assert_nil invalid.canonical_path
117
+ assert_equal :unresolvable, invalid.reason
118
+
119
+ valid = LLM::Sandbox.authorize_path(File.join(root, 'nested', '..', 'child'), root: root, mode: :write)
120
+ assert_true valid.allowed
121
+ assert_equal File.join(root, 'child'), valid.canonical_path
122
+ end
123
+
124
+ def test_parent_component_checks_preceding_directory_without_rechecking_parent
125
+ root = File.join(@root, 'root')
126
+ nested = File.join(root, 'nested')
127
+ directory_checks = []
128
+ original_directory_check = Open.method(:directory?)
129
+ Open.define_singleton_method(:directory?) do |path|
130
+ directory_checks << path
131
+ original_directory_check.call(path)
132
+ end
133
+
134
+ result = LLM::Sandbox.resolve_path(File.join(nested, '..', 'child'))
135
+
136
+ assert_equal File.join(root, 'child'), result[:canonical_path]
137
+ assert_equal [nested], directory_checks
138
+ ensure
139
+ Open.define_singleton_method(:directory?, original_directory_check) if original_directory_check
140
+ end
141
+
142
+ def test_missing_component_before_parent_component_is_not_collapsed
143
+ root = File.join(@root, 'root')
144
+ result = LLM::Sandbox.resolve_path(File.join(root, 'missing', '..', 'child'))
145
+
146
+ assert_nil result[:canonical_path]
147
+ assert_false result[:exists]
148
+ end
149
+
150
+ def test_unresolvable_dangling_symlink_is_denied
151
+ grant = File.join(@root, 'write_grant')
152
+ dangling = File.join(grant, 'dangling')
153
+ File.symlink(File.join(@root, 'missing-target'), dangling)
154
+ decision = LLM::Sandbox.authorize_path(dangling, root: File.join(@root, 'root'),
155
+ writable_paths: [grant], mode: :write)
156
+ assert_false decision.allowed
157
+ assert_equal :unresolvable, decision.reason
158
+ end
159
+
160
+ # The grant-source layer composes the same lists the Workspace workflow
161
+ # composes today. Pinned with raw thread/config manipulation so the
162
+ # assertion holds regardless of the ambient environment.
163
+ def test_grant_sources_compose_defaults_thread_and_config
164
+ saved_allowed = Thread.current['allowed_paths']
165
+ saved_read = Thread.current['allowed_read_paths']
166
+ Thread.current['allowed_paths'] = ['/thread/grant']
167
+ Thread.current['allowed_read_paths'] = ['/thread/read']
168
+
169
+ writable = LLM::Sandbox.writable_grants(scope: 'Workspace')
170
+ readable = LLM::Sandbox.readable_grants(scope: 'Workspace')
171
+
172
+ assert_include writable, '/thread/grant'
173
+ assert_include writable, '/tmp'
174
+ assert_include readable, '/thread/read'
175
+ assert_include readable, '/bin'
176
+ assert_not_include writable, '/thread/read'
177
+ assert_not_include readable, '/thread/grant'
178
+
179
+ # Defaults resolve unlocated Scout spellings through the path maps. The
180
+ # :home expansion is computed live as the oracle: when an earlier test
181
+ # file in the same process has required 'rbbt' (test_chat.rb,
182
+ # test_tools.rb), Scout's PKGDIR switches and the expansion spelling
183
+ # changes with it. The grant composition follows the live maps either
184
+ # way, which is the property under test.
185
+ assert_include writable.map(&:to_s), Path.setup('tmp').find(:home).to_s
186
+ ensure
187
+ Thread.current['allowed_paths'] = saved_allowed
188
+ Thread.current['allowed_read_paths'] = saved_read
189
+ end
190
+
191
+ def test_grant_scope_tokens_and_expand
192
+ assert_equal ['Workspace', 'workspace', 'sandbox'], LLM::Sandbox.grant_scope_tokens('Workspace')
193
+ assert_equal ['sandbox'], LLM::Sandbox.grant_scope_tokens(nil)
194
+
195
+ located = LLM::Sandbox.expand_grant_path('/tmp')
196
+ assert_true Path === located || String === located
197
+ assert_equal '/tmp', located.to_s
198
+
199
+ expanded = LLM::Sandbox.expand_grant_path('/tmp')
200
+ assert_kind_of Array, LLM::Sandbox.expand_grant_path('etc')
201
+ end
202
+
203
+ def test_grant_paths_reads_config_and_env_overrides
204
+ Scout::Config.with_config do
205
+ Scout::Config.set({'dirs' => '/plain/dirs'})
206
+ Scout::Config.set({'dirs' => '/workspace/dirs'}, 'workspace')
207
+ assert_equal ['/workspace/dirs'], LLM::Sandbox.grant_paths('dirs', scope: 'Workspace')
208
+ end
209
+
210
+ Scout::Config.with_config do
211
+ ENV['DIRS'] = '/env/dirs'
212
+ assert_equal ['/env/dirs'], LLM::Sandbox.grant_paths('dirs', scope: 'Workspace')
213
+ end
214
+ ensure
215
+ ENV.delete('DIRS')
216
+ end
217
+
218
+ def test_grant_origin_reports_the_first_source_listing_a_path
219
+ saved = Thread.current['allowed_paths']
220
+ Thread.current['allowed_paths'] = nil
221
+ LLM::Sandbox.allow_path('/tmp/grant-origin-job')
222
+
223
+ assert_equal :thread, LLM::Sandbox.grant_origin('/tmp/grant-origin-job', scope: 'Workspace')
224
+ assert_equal :exec_paths, LLM::Sandbox.grant_origin('/bin', scope: 'Workspace')
225
+ assert_nil LLM::Sandbox.grant_origin('/definitely/not/a/grant', scope: 'Workspace')
226
+ ensure
227
+ Thread.current['allowed_paths'] = saved
228
+ end
229
+
230
+ def test_describe_grants_annotates_and_includes_hint
231
+ text = LLM::Sandbox.describe_grants(scope: 'Workspace', root: '/some/root',
232
+ hint: "Run task 'sandbox_paths' for details")
233
+ assert_include text, '/some/root (read-write)'
234
+ assert_include text, '/tmp (read-write)'
235
+ assert_include text, '/bin (read-only)'
236
+ assert_include text, "Run task 'sandbox_paths' for details"
237
+ # read-only lines come after writable ones, and root heads the list
238
+ assert_true text.index('/bin (read-only)') > text.index('/tmp (read-write)')
239
+ assert_equal 0, text.index('/some/root (read-write)')
240
+ end
241
+
242
+ private
243
+
244
+ def assert_allowed(path, root:)
245
+ decision = LLM::Sandbox.authorize_path(path, root: root, mode: :write)
246
+ assert_true decision.allowed
247
+ assert_equal :root, decision.grant
248
+ end
249
+ end
@@ -0,0 +1,150 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/llm/agent'
3
+ require 'scout/llm/tools/call'
4
+ require 'json'
5
+
6
+ # Regression guards for answer attribution when one tool round returns the
7
+ # SAME LLM::Agent object from several tool calls (two `ask` calls to one
8
+ # registered conversation, two delegate hand-offs to one specialist slot).
9
+ #
10
+ # Historical bug (call.rb ~184): outputs were paired with answers via
11
+ # `agent_answers[agents.index(content)]`; Array#index finds the FIRST
12
+ # occurrence, so every duplicate-agent call embedded the FIRST answer, and
13
+ # current_chat.follow(res) followed the wrong chat.
14
+ class TestLLMToolCallAgentPairing < Test::Unit::TestCase
15
+ # ScoutCoder: LLM.process_answers asks agents through Open.traverse with
16
+ # cpus from Scout::Config.get(:cpus, :agent_ask, :agents, env:
17
+ # 'ASK_AGENTS', default: 3). LLM::Mock's response counter (@index) is
18
+ # shared mutable state, so concurrent rounds race on it and answers get
19
+ # duplicated/lost nondeterministically. Pin the collection to sequential
20
+ # execution so pairing assertions are deterministic; the race is a test
21
+ # harness limitation, not the behavior under test.
22
+ setup do
23
+ Scout::Config.set({cpus: 1}, :agent_ask, :agents)
24
+ end
25
+
26
+ def shared_worker
27
+ LLM::Agent.new(start_chat: Chat.setup([{role: :system, content: 'worker helper'}]), endpoint: :mock)
28
+ end
29
+
30
+ def call(id, prompt)
31
+ IndiferentHash.setup({
32
+ 'id' => id, 'type' => 'function',
33
+ 'function' => {'name' => 'ask', 'arguments' => {prompt: prompt}.to_json}
34
+ })
35
+ end
36
+
37
+ # One tool whose block appends the prompt to the SHARED agent and returns
38
+ # that same agent object from every call.
39
+ def shared_agent_tool(worker)
40
+ Proc.new do |_name, args|
41
+ worker.user args[:prompt]
42
+ worker
43
+ end
44
+ end
45
+
46
+ def outputs_for(messages)
47
+ messages.select { |m| m[:role].to_s == 'function_call_output' }
48
+ end
49
+
50
+ def output_content(message)
51
+ JSON.parse(message[:content])['content']
52
+ end
53
+
54
+ def test_duplicate_agent_answers_pair_by_call_position
55
+ TmpFile.with_dir do |_dir|
56
+ LLM::Mock.script('first answer', 'second answer')
57
+
58
+ worker = shared_worker
59
+
60
+ result = LLM.process_calls({'ask' => [shared_agent_tool(worker), nil]}, [
61
+ call('call_1', 'first question'),
62
+ call('call_2', 'second question')
63
+ ])
64
+
65
+ outputs = outputs_for(result)
66
+ assert_equal 2, outputs.length
67
+
68
+ first_content = output_content(outputs[0])
69
+ second_content = output_content(outputs[1])
70
+
71
+ # Each function_call_output carries its OWN round's answer: call_1
72
+ # answered "first answer", call_2 answered "second answer". The old
73
+ # agents.index(content) pairing made BOTH carry "first answer".
74
+ assert_equal 'first answer', first_content
75
+ assert_equal 'second answer', second_content
76
+ assert_not_equal first_content, second_content
77
+
78
+ # The shared conversation itself followed BOTH rounds in order, so the
79
+ # final answer (content.answer = last assistant message) is the second
80
+ # round's.
81
+ assert_equal 'second answer', worker.current_chat.answer
82
+
83
+ # Both prompts reached the shared agent, in call order.
84
+ user_messages = worker.current_chat.select { |m| m[:role].to_s == 'user' }.collect { |m| m[:content] }
85
+ assert_equal ['first question', 'second question'], user_messages
86
+ end
87
+ end
88
+
89
+ # The general case stays intact: DISTINCT agents in one round each get
90
+ # their own answer (no cross-talk in either direction).
91
+ def test_distinct_agents_pair_by_call_position
92
+ TmpFile.with_dir do |_dir|
93
+ LLM::Mock.script('left answer', 'right answer')
94
+
95
+ left = shared_worker
96
+ right = shared_worker
97
+
98
+ tool = Proc.new do |_name, args|
99
+ target = args[:prompt].include?('left') ? left : right
100
+ target.user args[:prompt]
101
+ target
102
+ end
103
+
104
+ result = LLM.process_calls({'ask' => [tool, nil]}, [
105
+ call('call_1', 'left question'),
106
+ call('call_2', 'right question')
107
+ ])
108
+
109
+ outputs = outputs_for(result)
110
+ assert_equal 2, outputs.length
111
+ assert_equal 'left answer', output_content(outputs[0])
112
+ assert_equal 'right answer', output_content(outputs[1])
113
+
114
+ assert_equal 'left answer', left.current_chat.answer
115
+ assert_equal 'right answer', right.current_chat.answer
116
+ end
117
+ end
118
+
119
+ # Mixed round: one agent-returning call and one plain String call. The
120
+ # agent answer must still pair with the agent call, not shift into the
121
+ # string output's position.
122
+ def test_mixed_agent_and_string_outputs_pair_correctly
123
+ TmpFile.with_dir do |_dir|
124
+ LLM::Mock.script('agent answer')
125
+
126
+ worker = shared_worker
127
+ seen = nil
128
+ tool = Proc.new do |_name, args|
129
+ if args[:prompt].include?('string')
130
+ seen = 'plain result'
131
+ else
132
+ worker.user args[:prompt]
133
+ worker
134
+ end
135
+ end
136
+
137
+ result = LLM.process_calls({'ask' => [tool, nil]}, [
138
+ call('call_1', 'agent question'),
139
+ call('call_2', 'string question')
140
+ ])
141
+
142
+ outputs = outputs_for(result)
143
+ assert_equal 2, outputs.length
144
+ assert_equal 'agent answer', output_content(outputs[0])
145
+ assert_equal 'plain result', output_content(outputs[1])
146
+ assert_equal 'agent answer', worker.current_chat.answer
147
+ assert_equal 'plain result', seen
148
+ end
149
+ end
150
+ end