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,1812 @@
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
+ class TestPathContracts < Test::Unit::TestCase
7
+ def setup
8
+ @tmpdir = Scout.tmp.agent_path_test.find
9
+ FileUtils.mkdir_p(@tmpdir)
10
+ @agent = LLM.agent
11
+ end
12
+
13
+ def teardown
14
+ FileUtils.remove_entry(@tmpdir) if @tmpdir && File.exist?(@tmpdir)
15
+ end
16
+
17
+ def register_text_kind(name = "text", capabilities = {})
18
+ @agent.register_path_kind(
19
+ name,
20
+ "description" => "Test text paths",
21
+ "locations" => ["project", "tmp"],
22
+ "default_location" => "project",
23
+ "roots" => {
24
+ "project" => @tmpdir,
25
+ "tmp" => File.join(@tmpdir, "tmp")
26
+ },
27
+ "capabilities" => capabilities
28
+ )
29
+ end
30
+
31
+ def test_plugin_loading_installs_tools_on_llm_agent
32
+ agent = LLM::Agent.new
33
+ plugin = File.join(@tmpdir, "path_plugin.rb")
34
+ FileUtils.mkdir_p(File.dirname(plugin))
35
+ File.write(plugin, <<~RUBY)
36
+ register_path_kind :plugin_text,
37
+ "description" => "Plugin text paths",
38
+ "locations" => ["project"],
39
+ "roots" => {"project" => #{@tmpdir.inspect}}
40
+ RUBY
41
+
42
+ agent.load_path_plugins(plugin)
43
+
44
+ tools = agent.other_options[:tools]
45
+ assert tools.key?("path_kinds")
46
+ assert tools.key?("path_read")
47
+ callable, definition = tools.fetch("path_kinds")
48
+ assert_equal "plugin_text", callable.call("path_kinds", {}).keys.first
49
+ assert_equal "path_kinds", definition.dig(:function, :name)
50
+ end
51
+
52
+ def test_register_path_kind
53
+ definition = register_text_kind
54
+
55
+ assert_equal "text", definition["name"]
56
+ assert_equal "Test text paths", definition["description"]
57
+ assert_equal ["project", "tmp"], definition["locations"]
58
+ assert_equal "project", definition["default_location"]
59
+
60
+ assert_equal true, definition["capabilities"]["read"]
61
+ assert_equal true, definition["capabilities"]["write"]
62
+ assert_equal true, definition["capabilities"]["edit"]
63
+ assert_equal true, definition["capabilities"]["list"]
64
+ assert_equal true, definition["capabilities"]["move"]
65
+ assert_equal true, definition["capabilities"]["rename"]
66
+ assert_equal true, definition["capabilities"]["delete"]
67
+ assert_equal false, definition["capabilities"]["validate"]
68
+ assert_equal false, definition["capabilities"]["promote"]
69
+ end
70
+
71
+ def test_register_path_kind_rejects_empty_name
72
+ assert_raise(ParameterException) do
73
+ @agent.register_path_kind("")
74
+ end
75
+ end
76
+
77
+ def test_register_path_kind_rejects_non_boolean_sandbox_setting
78
+ assert_raise(ParameterException) do
79
+ @agent.register_path_kind("invalid", "sandbox" => "false")
80
+ end
81
+ end
82
+
83
+ def test_register_path_kind_rejects_invalid_default_location
84
+ assert_raise(ParameterException) do
85
+ @agent.register_path_kind(
86
+ "text",
87
+ "locations" => ["project"],
88
+ "default_location" => "tmp"
89
+ )
90
+ end
91
+ end
92
+
93
+ def registration_state
94
+ registry_present = @agent.instance_variable_defined?(:@path_kinds)
95
+ registry = @agent.instance_variable_get(:@path_kinds)
96
+ options_present = @agent.instance_variable_defined?(:@other_options)
97
+ options = @agent.instance_variable_get(:@other_options)
98
+ tools_present = options.is_a?(Hash) && options.key?(:tools)
99
+ tools = tools_present ? options[:tools] : nil
100
+ {
101
+ registry_present: registry_present,
102
+ registry_object: registry,
103
+ registry_entries: registry.is_a?(Hash) ? registry.dup : nil,
104
+ options_present: options_present,
105
+ options_object: options,
106
+ options_entries: options.is_a?(Hash) ? options.dup : nil,
107
+ tools_present: tools_present,
108
+ tools_object: tools,
109
+ tool_entries: tools.is_a?(Hash) ? tools.dup : nil
110
+ }
111
+ end
112
+
113
+ def assert_registration_state_unchanged(before)
114
+ after = registration_state
115
+ assert_equal before[:registry_present], after[:registry_present]
116
+ assert_same before[:registry_object], after[:registry_object]
117
+ assert_equal before[:registry_entries], after[:registry_entries]
118
+ assert_equal before[:options_present], after[:options_present]
119
+ assert_same before[:options_object], after[:options_object]
120
+ assert_equal before[:options_entries], after[:options_entries]
121
+ assert_equal before[:tools_present], after[:tools_present]
122
+ assert_same before[:tools_object], after[:tools_object]
123
+ assert_equal before[:tool_entries], after[:tool_entries]
124
+ if before[:tool_entries]
125
+ before[:tool_entries].each do |name, pair|
126
+ assert_same pair, after[:tool_entries][name], "tool entry #{name} should be unchanged"
127
+ end
128
+ end
129
+ end
130
+
131
+ # ------------------------------------------------------------------
132
+ # Path-map configuration for kind roots
133
+ # ------------------------------------------------------------------
134
+
135
+ def test_relative_roots_are_pinned_against_the_default_current_map
136
+ @agent.register_path_kind("pinned_kind",
137
+ "locations" => ["tmp"],
138
+ "roots" => {"tmp" => "tmp/pathmap_pinned_default"})
139
+
140
+ pinned = @agent.path_kind_definition("pinned_kind")["roots"]["tmp"]
141
+ expected = File.join(Path.setup("tmp/pathmap_pinned_default").follow(:current).to_s)
142
+ assert_equal expected, pinned
143
+ assert_equal String, pinned.class
144
+ assert pinned.start_with?(File::SEPARATOR)
145
+
146
+ # Writes through the engine land under the checkout, not ~/.scout
147
+ target = @agent.resolve_path("pinned_kind", "draft.rb", "tmp")
148
+ assert_equal File.join(expected, "draft.rb"), target.to_s
149
+ @agent.path_write({"kind" => "pinned_kind", "path" => "draft.rb", "content" => "x"})
150
+ assert File.exist?(File.join(expected, "draft.rb"))
151
+ end
152
+
153
+ def test_per_kind_path_map_and_agent_level_option
154
+ Path.add_path :pathmap_probe_map, File.join(tmpdir.to_s, 'pathmap_probe', '{TOPLEVEL}/{SUBPATH}')
155
+
156
+ @agent.register_path_kind("map_kind",
157
+ "locations" => ["tmp"], "path_map" => :pathmap_probe_map,
158
+ "roots" => {"tmp" => "some/root"})
159
+ assert_equal File.join(tmpdir.to_s, 'pathmap_probe', 'some', 'root'),
160
+ @agent.path_kind_definition("map_kind")["roots"]["tmp"]
161
+
162
+ other = LLM.agent(path_map: :pathmap_probe_map)
163
+ other.register_path_kind("agent_map_kind",
164
+ "locations" => ["tmp"],
165
+ "roots" => {"tmp" => "other/root"})
166
+ assert_equal File.join(tmpdir.to_s, 'pathmap_probe', 'other', 'root'),
167
+ other.path_kind_definition("agent_map_kind")["roots"]["tmp"]
168
+
169
+ # The per-kind map wins over the agent-level default
170
+ third = LLM.agent(path_map: :pathmap_probe_map)
171
+ third.register_path_kind("explicit_wins",
172
+ "locations" => ["tmp"], "path_map" => :current,
173
+ "roots" => {"tmp" => "explicit/root"})
174
+ assert_equal Path.setup("explicit/root").follow(:current).to_s,
175
+ third.path_kind_definition("explicit_wins")["roots"]["tmp"]
176
+ end
177
+
178
+ def test_absolute_roots_pass_through_untouched_even_with_a_map
179
+ absolute = File.join(tmpdir.to_s, 'abs_passthrough')
180
+ @agent.register_path_kind("abs_kind",
181
+ "locations" => ["tmp"], "path_map" => :user,
182
+ "roots" => {"tmp" => absolute})
183
+ assert_equal absolute, @agent.path_kind_definition("abs_kind")["roots"]["tmp"]
184
+
185
+ path_like = Path.setup(File.join(tmpdir.to_s, 'abs_path_like'))
186
+ @agent.register_path_kind("abs_path_kind",
187
+ "locations" => ["tmp"], "path_map" => :user,
188
+ "roots" => {"tmp" => path_like})
189
+ assert_equal path_like.to_s, @agent.path_kind_definition("abs_path_kind")["roots"]["tmp"].to_s
190
+ end
191
+
192
+ def test_path_map_validation_raises_parameter_exception
193
+ e = assert_raise(ParameterException) do
194
+ @agent.register_path_kind("bad_map_kind",
195
+ "locations" => ["tmp"], "path_map" => :no_such_map_here,
196
+ "roots" => {"tmp" => File.join(tmpdir.to_s, 'x')})
197
+ end
198
+ assert_match(/not a resolvable Scout path map/, e.message)
199
+
200
+ e2 = assert_raise(ParameterException) do
201
+ @agent.register_path_kind("bad_type_map_kind",
202
+ "locations" => ["tmp"], "path_map" => 42,
203
+ "roots" => {"tmp" => File.join(tmpdir.to_s, 'x')})
204
+ end
205
+ assert_match(/must be a String or Symbol/, e2.message)
206
+
207
+ bad_agent = LLM.agent(path_map: 7)
208
+ e3 = assert_raise(ParameterException) do
209
+ bad_agent.register_path_kind("agent_map_bad",
210
+ "locations" => ["tmp"],
211
+ "roots" => {"tmp" => File.join(tmpdir.to_s, 'x')})
212
+ end
213
+ assert_match(/must be a String or Symbol/, e3.message)
214
+ end
215
+
216
+ def test_implicit_project_root_honors_path_map
217
+ @agent.register_path_kind("project_map_kind",
218
+ "locations" => %w[tmp project],
219
+ "path_map" => :current,
220
+ "roots" => {"tmp" => File.join(tmpdir.to_s, 'project_map')})
221
+
222
+ target = @agent.resolve_path("project_map_kind", "x.rb", "project")
223
+ assert target.to_s.start_with?(File::SEPARATOR)
224
+ assert_equal Path.setup("x.rb").follow(:current).to_s, target.to_s
225
+ end
226
+
227
+ def test_invalid_location_root_and_default_declarations_are_atomic
228
+ invalid_definitions = {
229
+ "non-array locations" => {"locations" => "project"},
230
+ "empty locations" => {"locations" => []},
231
+ "duplicate locations" => {"locations" => %w[project project]},
232
+ "empty location name" => {"locations" => [""]},
233
+ "roots are not a mapping" => {"roots" => []},
234
+ # NOTE: a relative root is no longer invalid — it is pinned against
235
+ # the effective path map at registration (see
236
+ # test_relative_roots_are_pinned_against_the_path_map).
237
+ "missing declared root" => {"locations" => %w[project tmp],
238
+ "roots" => {"project" => @tmpdir}},
239
+ "unsupported default" => {"locations" => ["project"],
240
+ "default_location" => "tmp",
241
+ "roots" => {"project" => @tmpdir}},
242
+ "non-string default" => {"locations" => ["project"],
243
+ "default_location" => :project,
244
+ "roots" => {"project" => @tmpdir}}
245
+ }
246
+
247
+ invalid_definitions.each do |label, definition|
248
+ before = registration_state
249
+ assert_raise(ParameterException, label) do
250
+ @agent.register_path_kind("invalid_#{label.gsub(/\W+/, '_')}", definition)
251
+ end
252
+ assert_registration_state_unchanged(before)
253
+ end
254
+ end
255
+
256
+ def test_invalid_capability_and_custom_operation_values_are_atomic
257
+ valid_root = {"project" => @tmpdir}
258
+ invalid_definitions = {
259
+ "unknown capability" => {"capabilities" => {"teleport" => true}},
260
+ "non-boolean capability" => {"capabilities" => {"read" => "yes"}},
261
+ "capabilities wrong shape" => {"capabilities" => "read"},
262
+ "invalid custom authorization value" => {
263
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "authorization" => 42}}
264
+ },
265
+ "invalid custom authorization mode" => {
266
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "authorization" => :admin}}
267
+ },
268
+ "false operations mapping" => {"operations" => false},
269
+ "false overrides mapping" => {"overrides" => false},
270
+ "false custom authorization" => {
271
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "authorization" => false}}
272
+ },
273
+ "invalid custom operation type value" => {
274
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "type" => 42}}
275
+ },
276
+ "false custom operation type" => {
277
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "type" => false}}
278
+ },
279
+ "false custom operation parameters" => {
280
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "parameters" => false}}
281
+ },
282
+ "unknown custom operation type" => {
283
+ "operations" => {"custom_op" => {"implementation" => ->(*) {}, "type" => :mystery}}
284
+ }
285
+ }
286
+
287
+ invalid_definitions.each do |label, definition|
288
+ before = registration_state
289
+ assert_raise(ParameterException, label) do
290
+ @agent.register_path_kind("invalid_#{label.gsub(/\W+/, '_')}",
291
+ {"roots" => valid_root}.merge(definition))
292
+ end
293
+ assert_registration_state_unchanged(before)
294
+ end
295
+ end
296
+
297
+ def test_invalid_hook_and_promotion_destination_declarations_are_atomic
298
+ invalid_definitions = {
299
+ "non-callable hook" => {"resolve" => "not callable"},
300
+ "numeric promotion destination" => {"capabilities" => {"promote" => true},
301
+ "promotion_destination" => 42},
302
+ "callable promotion destination" => {"capabilities" => {"promote" => true},
303
+ "promotion_destination" => ->(*) { "project" }},
304
+ "undeclared promotion destination" => {"locations" => ["project"],
305
+ "capabilities" => {"promote" => true},
306
+ "promotion_destination" => "missing"},
307
+ "invalid promotion location" => {"locations" => ["project"],
308
+ "capabilities" => {"promote" => true},
309
+ "promotion_destination" => {"location" => 12}},
310
+ "promotion enabled without destination location" => {"locations" => ["tmp"],
311
+ "default_location" => "tmp",
312
+ "roots" => {"tmp" => @tmpdir},
313
+ "capabilities" => {"promote" => true}}
314
+ }
315
+
316
+ invalid_definitions.each do |label, definition|
317
+ before = registration_state
318
+ assert_raise(ParameterException, label) do
319
+ @agent.register_path_kind("invalid_#{label.gsub(/\W+/, '_')}",
320
+ {"roots" => {"project" => @tmpdir}}.merge(definition))
321
+ end
322
+ assert_registration_state_unchanged(before)
323
+ end
324
+ end
325
+
326
+ def test_registering_multiple_kinds_in_sequence
327
+ {
328
+ "text" => {
329
+ "locations" => ["project"],
330
+ "roots" => {"project" => @tmpdir}
331
+ },
332
+ "other" => {
333
+ "locations" => ["project"],
334
+ "roots" => {"project" => @tmpdir}
335
+ }
336
+ }.each do |name, definition|
337
+ @agent.register_path_kind(name, definition)
338
+ end
339
+
340
+ assert @agent.path_kinds.key?("text")
341
+ assert @agent.path_kinds.key?("other")
342
+ end
343
+
344
+ def test_path_kind_definition
345
+ register_text_kind
346
+
347
+ definition = @agent.path_kind_definition("text")
348
+
349
+ assert_equal "text", definition["name"]
350
+ end
351
+
352
+ def test_path_kind_definition_rejects_unknown_kind
353
+ assert_raise(ParameterException) do
354
+ @agent.path_kind_definition("missing")
355
+ end
356
+ end
357
+
358
+ def test_path_kind_capable
359
+ register_text_kind(
360
+ "text",
361
+ "validate" => true,
362
+ "promote" => true
363
+ )
364
+
365
+ assert @agent.path_kind_capable?("text", "read")
366
+ assert @agent.path_kind_capable?("text", "write")
367
+ assert @agent.path_kind_capable?("text", "validate")
368
+ assert @agent.path_kind_capable?("text", "promote")
369
+ assert_equal true, @agent.path_kind_capable?("text", "promote")
370
+ end
371
+
372
+ def test_path_rename_checks_rename_capability_independently
373
+ @agent.register_path_kind(
374
+ "text",
375
+ "locations" => ["project"],
376
+ "roots" => {"project" => @tmpdir},
377
+ "capabilities" => {"move" => false, "rename" => true}
378
+ )
379
+ @agent.path_write(
380
+ "kind" => "text",
381
+ "path" => "one.txt",
382
+ "content" => "hello"
383
+ )
384
+
385
+ result = @agent.path_rename(
386
+ "kind" => "text",
387
+ "path" => "one.txt",
388
+ "name" => "two.txt"
389
+ )
390
+
391
+ assert_equal true, result["renamed"]
392
+ assert !File.exist?(File.join(@tmpdir, "one.txt"))
393
+ assert_equal "hello", File.read(File.join(@tmpdir, "two.txt"))
394
+ end
395
+
396
+ def test_path_rename_rejects_missing_rename_capability_even_when_move_is_supported
397
+ register_text_kind("text", "rename" => false, "move" => true)
398
+
399
+ assert_raise(ParameterException) do
400
+ @agent.path_rename(
401
+ "kind" => "text",
402
+ "path" => "one.txt",
403
+ "name" => "two.txt"
404
+ )
405
+ end
406
+ end
407
+
408
+ def test_custom_operation_registration_stores_normalized_spec
409
+ register_text_kind
410
+
411
+ @agent.register_path_kind(
412
+ "fancy",
413
+ "roots" => {"project" => @tmpdir},
414
+ "operations" => {
415
+ "precise_edit" => {
416
+ "description" => "Precise edit",
417
+ "parameters" => {"properties" => {"selector" => {"type" => "string"}}, "required" => ["selector"]},
418
+ "implementation" => ->(_agent, _target, _args) { "ok" },
419
+ "authorization" => :read,
420
+ "type" => :content
421
+ }
422
+ }
423
+ )
424
+
425
+ spec = @agent.path_kind_definition("fancy")["operations"]["precise_edit"]
426
+ assert_equal "precise_edit", spec["name"]
427
+ assert_equal "Precise edit", spec["description"]
428
+ assert_equal ["selector"], spec["parameters"]["required"]
429
+ assert_equal :read, spec["authorization"]
430
+ assert_equal :content, spec["type"]
431
+ assert spec["implementation"].respond_to?(:call)
432
+
433
+ assert_equal ["precise_edit"],
434
+ @agent.path_kind_registry.values
435
+ .flat_map { |d| d.fetch("operations", {}).keys }.uniq.sort
436
+ assert @agent.path_operation_support?("fancy", "precise_edit")
437
+ assert_equal :read,
438
+ @agent.path_kind_definition("fancy")["operation_table"]["precise_edit"]["authorization"]
439
+ assert !@agent.path_operation_support?("text", "precise_edit")
440
+ end
441
+
442
+ def test_custom_operation_defaults_fail_closed_to_write_and_full_handler
443
+ register_text_kind
444
+
445
+ @agent.register_path_kind(
446
+ "plain",
447
+ "roots" => {"project" => @tmpdir},
448
+ "operations" => {"noop" => ->(_a, _t, _args) { "ok" }}
449
+ )
450
+
451
+ spec = @agent.path_kind_definition("plain")["operations"]["noop"]
452
+ assert_equal :write, spec["authorization"]
453
+ assert_equal :full, spec["type"]
454
+ assert_equal({}, spec["parameters"])
455
+ assert_equal({}, @agent.path_kind_definition("plain")["overrides"])
456
+ assert_equal :write,
457
+ @agent.path_kind_definition("plain")["operation_table"]["noop"]["authorization"]
458
+ end
459
+
460
+ # ------------------------------------------------------------------
461
+ # Step 4 — tool aggregation
462
+ # ------------------------------------------------------------------
463
+
464
+ def register_two_kinds_with_same_operation(first_description: "First kind operation", second_description: nil)
465
+ @agent.register_path_kind(
466
+ "alpha",
467
+ "roots" => {"project" => @tmpdir},
468
+ "operations" => {
469
+ "precise_edit" => {
470
+ "description" => first_description,
471
+ "parameters" => {
472
+ "properties" => {
473
+ "selector" => {"type" => "object"},
474
+ "replacement" => {"type" => "string"}
475
+ },
476
+ "required" => ["replacement"]
477
+ },
478
+ "implementation" => ->(content, args) { "alpha:#{content}" },
479
+ "authorization" => :write,
480
+ "type" => :content
481
+ }
482
+ }
483
+ )
484
+ return if second_description.nil?
485
+
486
+ @agent.register_path_kind(
487
+ "beta",
488
+ "roots" => {"project" => @tmpdir},
489
+ "operations" => {
490
+ "precise_edit" => {
491
+ "description" => second_description,
492
+ "parameters" => {
493
+ "properties" => {
494
+ "selector" => {"type" => "object"},
495
+ "replacement" => {"type" => "string"}
496
+ },
497
+ "required" => ["replacement"]
498
+ },
499
+ "implementation" => ->(content, args) { "beta:#{content}" },
500
+ "authorization" => :write,
501
+ "type" => :content
502
+ }
503
+ }
504
+ )
505
+ end
506
+
507
+ def step4_tools
508
+ @agent.instance_variable_get(:@other_options)[:tools]
509
+ end
510
+
511
+ def test_two_kinds_with_same_operation_install_exactly_one_shared_tool
512
+ register_two_kinds_with_same_operation(
513
+ first_description: "Alpha precise edit",
514
+ second_description: "Beta precise edit"
515
+ )
516
+
517
+ tools = step4_tools
518
+ assert tools.key?("precise_edit")
519
+ assert_equal 1, tools.keys.count("precise_edit")
520
+
521
+ schema = tools["precise_edit"][1][:function][:parameters]
522
+ assert_equal %w[alpha beta], schema[:properties]["kind"]["enum"].sort
523
+ assert_equal ["kind", "path", "replacement"].sort, schema[:required].sort
524
+ end
525
+
526
+ def test_conflicting_contracts_are_rejected_at_installation
527
+ @agent.register_path_kind(
528
+ "alpha",
529
+ "roots" => {"project" => @tmpdir},
530
+ "operations" => {
531
+ "precise_edit" => {
532
+ "description" => "Alpha precise edit",
533
+ "parameters" => {
534
+ "properties" => {"replacement" => {"type" => "string"}},
535
+ "required" => ["replacement"]
536
+ },
537
+ "implementation" => ->(content, args) { content }
538
+ }
539
+ }
540
+ )
541
+
542
+ # This failure occurs in tool aggregation after path_kinds and built-in
543
+ # tools have been replaced, so compare the full observable state first.
544
+ before = registration_state
545
+ error = assert_raise(ParameterException) do
546
+ @agent.register_path_kind(
547
+ "beta",
548
+ "roots" => {"project" => @tmpdir},
549
+ "operations" => {
550
+ "precise_edit" => {
551
+ "description" => "Beta precise edit",
552
+ "parameters" => {
553
+ "properties" => {"replacement" => {"type" => "integer"}},
554
+ "required" => ["replacement"]
555
+ },
556
+ "implementation" => ->(content, args) { content }
557
+ }
558
+ }
559
+ )
560
+ end
561
+ assert error.message.include?("precise_edit")
562
+ assert error.message.include?("alpha")
563
+ assert error.message.include?("beta")
564
+ assert error.message.include?("replacement")
565
+ assert_registration_state_unchanged(before)
566
+
567
+ error2 = assert_raise(ParameterException) do
568
+ @agent.register_path_kind(
569
+ "gamma",
570
+ "roots" => {"project" => @tmpdir},
571
+ "operations" => {
572
+ "precise_edit" => {
573
+ "description" => "Gamma precise edit",
574
+ "parameters" => {
575
+ "properties" => {"replacement" => {"type" => "string"}},
576
+ "required" => []
577
+ },
578
+ "implementation" => ->(content, args) { content }
579
+ }
580
+ }
581
+ )
582
+ end
583
+ assert error2.message.include?("required")
584
+ assert_registration_state_unchanged(before)
585
+ end
586
+
587
+ def test_failed_kind_registration_rolls_back_registry_and_tool_surface_recovers
588
+ @agent.register_path_kind(
589
+ "alpha",
590
+ "roots" => {"project" => @tmpdir},
591
+ "operations" => {
592
+ "a_partial" => {
593
+ "description" => "Alpha partial operation",
594
+ "parameters" => {"properties" => {}, "required" => []},
595
+ "implementation" => ->(*) { "ok" }
596
+ },
597
+ "precise_edit" => {
598
+ "description" => "Alpha precise edit",
599
+ "parameters" => {
600
+ "properties" => {"replacement" => {"type" => "string"}},
601
+ "required" => ["replacement"]
602
+ },
603
+ "implementation" => ->(*) { "ok" }
604
+ }
605
+ }
606
+ )
607
+ before = registration_state
608
+
609
+ assert_raise(ParameterException) do
610
+ @agent.register_path_kind(
611
+ "beta",
612
+ "roots" => {"project" => @tmpdir},
613
+ "operations" => {
614
+ "a_partial" => {
615
+ "description" => "Beta partial operation",
616
+ "parameters" => {"properties" => {}, "required" => []},
617
+ "implementation" => ->(*) { "ok" }
618
+ },
619
+ "precise_edit" => {
620
+ "description" => "Conflicting precise edit",
621
+ "parameters" => {
622
+ "properties" => {"replacement" => {"type" => "integer"}},
623
+ "required" => ["replacement"]
624
+ },
625
+ "implementation" => ->(*) { "ok" }
626
+ }
627
+ }
628
+ )
629
+ end
630
+
631
+ # Installation processes a_partial before discovering the later conflict.
632
+ # The earlier tool update and candidate kind must both be rolled back.
633
+ assert_registration_state_unchanged(before)
634
+ assert_equal %w[alpha], @agent.path_kind_registry.keys
635
+ assert_false step4_tools.key?("beta")
636
+
637
+ @agent.register_path_kind("gamma", "roots" => {"project" => @tmpdir})
638
+ assert_equal %w[alpha gamma], @agent.path_kind_registry.keys.sort
639
+ enum = step4_tools["path_kinds"][1][:function][:parameters][:properties]["kind"][:enum]
640
+ assert_equal %w[alpha gamma], enum.sort
641
+ end
642
+
643
+ def test_failed_replacement_restores_same_name_kind_and_tool_definitions
644
+ register_two_kinds_with_same_operation(
645
+ first_description: "Alpha precise edit",
646
+ second_description: "Beta precise edit"
647
+ )
648
+ old_alpha = @agent.path_kind_definition("alpha")
649
+ before = registration_state
650
+
651
+ error = assert_raise(ParameterException) do
652
+ @agent.register_path_kind(
653
+ "alpha",
654
+ "roots" => {"project" => @tmpdir},
655
+ "operations" => {
656
+ "precise_edit" => {
657
+ "description" => "Replacement with incompatible contract",
658
+ "parameters" => {
659
+ "properties" => {"replacement" => {"type" => "integer"}},
660
+ "required" => ["replacement"]
661
+ },
662
+ "implementation" => ->(*) { "replacement" }
663
+ }
664
+ }
665
+ )
666
+ end
667
+
668
+ assert_include error.message, "precise_edit"
669
+ assert_same old_alpha, @agent.path_kind_definition("alpha")
670
+ assert_registration_state_unchanged(before)
671
+ end
672
+
673
+ def test_description_resolves_by_registration_order
674
+ register_two_kinds_with_same_operation(
675
+ first_description: "Alpha precise edit",
676
+ second_description: "Beta precise edit"
677
+ )
678
+ tools = step4_tools
679
+ assert tools["precise_edit"][1][:function][:description].include?("Beta precise edit")
680
+
681
+ # deterministic: swap the registration order in a fresh agent and the
682
+ # other description wins.
683
+ other = LLM.agent
684
+ other.register_path_kind(
685
+ "zeta",
686
+ "roots" => {"project" => @tmpdir},
687
+ "operations" => {
688
+ "precise_edit" => {
689
+ "description" => "Zeta precise edit",
690
+ "parameters" => {
691
+ "properties" => {"replacement" => {"type" => "string"}},
692
+ "required" => ["replacement"]
693
+ },
694
+ "implementation" => ->(content, args) { content }
695
+ }
696
+ }
697
+ )
698
+ other.register_path_kind(
699
+ "alpha",
700
+ "roots" => {"project" => @tmpdir},
701
+ "operations" => {
702
+ "precise_edit" => {
703
+ "description" => "Alpha precise edit",
704
+ "parameters" => {
705
+ "properties" => {"replacement" => {"type" => "string"}},
706
+ "required" => ["replacement"]
707
+ },
708
+ "implementation" => ->(content, args) { content }
709
+ }
710
+ }
711
+ )
712
+ other_tools = other.instance_variable_get(:@other_options)[:tools]
713
+ assert other_tools["precise_edit"][1][:function][:description].include?("Alpha precise edit")
714
+ end
715
+
716
+ def test_aggregated_tool_fails_cleanly_for_unsupported_kind
717
+ register_two_kinds_with_same_operation(first_description: "Alpha precise edit")
718
+
719
+ tools = step4_tools
720
+ result = tools["precise_edit"][0].call(
721
+ "precise_edit",
722
+ {"kind" => "text", "path" => "a.txt", "replacement" => "x"}
723
+ )
724
+ payload = JSON.parse(result)
725
+ assert payload["exception"].include?("Unknown Path kind")
726
+ end
727
+
728
+ def test_custom_tool_visible_immediately_and_not_dropped_by_later_registration
729
+ register_two_kinds_with_same_operation(first_description: "Alpha precise edit")
730
+
731
+ tools = step4_tools
732
+ assert tools.key?("precise_edit")
733
+ assert tools.key?("path_read")
734
+
735
+ @agent.register_path_kind(
736
+ "extra",
737
+ "roots" => {"project" => @tmpdir},
738
+ "operations" => {
739
+ "other_op" => {
740
+ "description" => "Other",
741
+ "parameters" => {"properties" => {}, "required" => []},
742
+ "implementation" => ->(content, args) { content }
743
+ }
744
+ }
745
+ )
746
+
747
+ tools = step4_tools
748
+ assert tools.key?("precise_edit")
749
+ assert tools.key?("other_op")
750
+ assert tools.key?("path_read")
751
+ end
752
+
753
+ def test_builtin_schemas_unchanged_by_custom_registration
754
+ before = {}
755
+ @agent.register_path_kind("text", "roots" => {"project" => @tmpdir})
756
+ @agent.install_path_tools
757
+ step4_tools.each do |name, pair|
758
+ before[name] = pair[1][:function]
759
+ end
760
+
761
+ @agent.register_path_kind(
762
+ "fancy",
763
+ "roots" => {"project" => @tmpdir},
764
+ "overrides" => {"edit" => ->(a, t, args) { "overridden" }},
765
+ "operations" => {
766
+ "precise_edit" => {
767
+ "description" => "Fancy precise edit",
768
+ "parameters" => {
769
+ "properties" => {"replacement" => {"type" => "string"}},
770
+ "required" => ["replacement"]
771
+ },
772
+ "implementation" => ->(content, args) { content }
773
+ }
774
+ }
775
+ )
776
+
777
+ after = {}
778
+ step4_tools.each do |name, pair|
779
+ after[name] = pair[1][:function]
780
+ end
781
+
782
+ %w[path_kinds path_read path_write path_edit path_list path_move path_rename path_delete path_validate path_promote path_smoke].each do |builtin|
783
+ next unless before.key?(builtin)
784
+ assert_equal before[builtin][:name], after[builtin][:name], builtin
785
+ assert_equal before[builtin][:description], after[builtin][:description], builtin
786
+ assert_equal before[builtin][:parameters].reject { |k, _| k == :properties },
787
+ after[builtin][:parameters].reject { |k, _| k == :properties },
788
+ builtin
789
+ before_props = before[builtin][:parameters][:properties].dup
790
+ after_props = after[builtin][:parameters][:properties].dup
791
+ # kind property enum legitimately grows with new kinds; compare the rest.
792
+ assert_equal before_props["kind"]["type"], after_props["kind"]["type"]
793
+ before_props.delete("kind")
794
+ after_props.delete("kind")
795
+ assert_equal before_props, after_props, builtin
796
+ end
797
+ end
798
+
799
+ def test_builtin_tools_still_call_engine_methods_directly
800
+ register_text_kind
801
+ @agent.path_write("kind" => "text", "path" => "hello.txt", "content" => "hello")
802
+
803
+ tools = step4_tools
804
+ result = tools["path_read"][0].call("path_read", {"kind" => "text", "path" => "hello.txt"})
805
+ assert_equal "hello", result["content"]
806
+ end
807
+
808
+
809
+ # ------------------------------------------------------------------
810
+ # Step 5: centralized dispatch with authorization
811
+ # ------------------------------------------------------------------
812
+
813
+ def step5_spy(calls)
814
+ ->(*args) { calls << args; "SPY" }
815
+ end
816
+
817
+ def register_fancy_with_operation(operation_name, spec_extra = {}, &)
818
+ spec = {
819
+ "parameters" => {"properties" => {}, "required" => []},
820
+ "implementation" => ->(content, args) { content }
821
+ }.merge(spec_extra)
822
+ @agent.register_path_kind(
823
+ "fancy",
824
+ "roots" => {"project" => @tmpdir},
825
+ "operations" => {operation_name => spec}
826
+ )
827
+ end
828
+
829
+ def test_dispatch_custom_content_operation_reads_transforms_and_writes
830
+ register_text_kind
831
+ register_fancy_with_operation(
832
+ "precise_edit",
833
+ "implementation" => ->(content, args) { content + args.fetch("marker", "!") },
834
+ "type" => :content
835
+ )
836
+ @agent.path_write("kind" => "fancy", "path" => "a.txt", "content" => "hello")
837
+
838
+ result = @agent.path_dispatch(
839
+ kind: "fancy", operation: "precise_edit",
840
+ "path" => "a.txt", "marker" => "!!"
841
+ )
842
+
843
+ assert_equal "hello!!", result["content"]
844
+ assert_equal "hello!!", File.read(File.join(@tmpdir, "a.txt"))
845
+ assert_equal true, result["changed"]
846
+ end
847
+
848
+ def test_dispatch_custom_full_operation_receives_agent_and_target
849
+ register_text_kind
850
+ seen = []
851
+ register_fancy_with_operation(
852
+ "audit",
853
+ "implementation" => ->(agent, target, args) {
854
+ seen << [agent.class, target.to_s, args["path"]]
855
+ "AUDIT #{File.basename(target.to_s)}"
856
+ },
857
+ "type" => :full
858
+ )
859
+
860
+ result = @agent.path_dispatch(
861
+ kind: "fancy", operation: "audit", "path" => "x.txt"
862
+ )
863
+
864
+ assert_equal "AUDIT x.txt", result
865
+ assert_equal 1, seen.length
866
+ assert_equal LLM::Agent, seen.first.first
867
+ end
868
+
869
+ def test_dispatch_unsupported_kind_and_operation_fail_cleanly
870
+ register_text_kind
871
+
872
+ assert_raise(ParameterException) do
873
+ @agent.path_dispatch(kind: "nope", operation: "read", "path" => "a")
874
+ end
875
+
876
+ assert_raise(ParameterException) do
877
+ @agent.path_dispatch(kind: "text", operation: "smoke", "path" => "a")
878
+ end
879
+
880
+ # no cross-kind fallback: "precise_edit" on fancy is not reachable
881
+ # through kind "text"
882
+ register_fancy_with_operation("precise_edit")
883
+ assert_raise(ParameterException) do
884
+ @agent.path_dispatch(kind: "text", operation: "precise_edit", "path" => "a")
885
+ end
886
+ end
887
+
888
+ def test_dispatch_authorization_failure_runs_no_callback_and_mutates_nothing
889
+ register_text_kind
890
+ calls = []
891
+ register_fancy_with_operation(
892
+ "rewrite",
893
+ "implementation" => step5_spy(calls),
894
+ "type" => :full
895
+ )
896
+ outside = File.join(Dir.tmpdir, "step5_outside_#{Process.pid}.txt")
897
+ File.write(outside, "KEEP")
898
+
899
+ assert_raise(ParameterException) do
900
+ @agent.path_dispatch(
901
+ kind: "fancy", operation: "rewrite",
902
+ "path" => "../#{File.basename(outside)}"
903
+ )
904
+ end
905
+
906
+ assert_empty calls
907
+ assert_equal "KEEP", File.read(outside)
908
+ ensure
909
+ File.delete(outside) if outside && File.exist?(outside)
910
+ end
911
+
912
+ def test_dispatch_content_op_outside_sandbox_invokes_no_callback
913
+ register_text_kind
914
+ calls = []
915
+ register_fancy_with_operation(
916
+ "rewrite",
917
+ "implementation" => step5_spy(calls),
918
+ "type" => :content
919
+ )
920
+
921
+ assert_raise(ParameterException) do
922
+ @agent.path_dispatch(
923
+ kind: "fancy", operation: "rewrite",
924
+ "path" => "../../etc/hostname"
925
+ )
926
+ end
927
+
928
+ assert_empty calls
929
+ end
930
+
931
+ def test_dispatch_edit_override_authorization_failure_runs_no_override
932
+ register_text_kind
933
+ calls = []
934
+ @agent.register_path_kind(
935
+ "fancy",
936
+ "roots" => {"project" => @tmpdir},
937
+ "overrides" => {"edit" => step5_spy(calls)}
938
+ )
939
+ @agent.path_write("kind" => "fancy", "path" => "keep.txt", "content" => "KEEP")
940
+ before = File.read(File.join(@tmpdir, "keep.txt"))
941
+
942
+ assert_raise(ParameterException) do
943
+ @agent.path_edit(
944
+ "kind" => "fancy",
945
+ "path" => "../../etc/hostname",
946
+ "selector" => {"type" => "lines", "start" => 1},
947
+ "replacement" => "x"
948
+ )
949
+ end
950
+
951
+ assert_empty calls
952
+ assert_equal before, File.read(File.join(@tmpdir, "keep.txt"))
953
+ end
954
+
955
+ def test_dispatch_content_op_cannot_redirect_write_to_other_file
956
+ register_text_kind
957
+ register_fancy_with_operation(
958
+ "stubborn_edit",
959
+ "implementation" => ->(content, args) { "smuggled" },
960
+ "type" => :content
961
+ )
962
+ @agent.path_write("kind" => "fancy", "path" => "one.txt", "content" => "original")
963
+
964
+ @agent.path_dispatch(
965
+ kind: "fancy", operation: "stubborn_edit", "path" => "one.txt"
966
+ )
967
+
968
+ assert_equal "smuggled", File.read(File.join(@tmpdir, "one.txt"))
969
+ files = Dir.glob(File.join(@tmpdir, "**", "*")).select { |f| File.file?(f) }
970
+ assert_equal ["#{File.join(@tmpdir.to_s, 'one.txt')}"], files.sort.map { |f| f }
971
+ end
972
+
973
+ def test_dispatch_edit_content_override_preserves_final_newline
974
+ register_text_kind
975
+ @agent.register_path_kind(
976
+ "fancy",
977
+ "roots" => {"project" => @tmpdir},
978
+ "overrides" => {
979
+ # replaces the final line wherever it is; deliberately returns a
980
+ # value with a trailing newline to prove engine-side preservation
981
+ "edit" => ->(content, args) {
982
+ content.sub(/b\n?\z/, "x\n")
983
+ }
984
+ }
985
+ )
986
+
987
+ @agent.path_write("kind" => "fancy", "path" => "plain.txt", "content" => "a\nb")
988
+ @agent.path_edit(
989
+ "kind" => "fancy", "path" => "plain.txt",
990
+ "selector" => {"type" => "lines", "start" => 2},
991
+ "replacement" => "x"
992
+ )
993
+ assert_equal "a\nx", File.read(File.join(@tmpdir, "plain.txt"))
994
+
995
+ @agent.path_write("kind" => "fancy", "path" => "term.txt", "content" => "a\nb\n")
996
+ @agent.path_edit(
997
+ "kind" => "fancy", "path" => "term.txt",
998
+ "selector" => {"type" => "lines", "start" => 2},
999
+ "replacement" => "x"
1000
+ )
1001
+ # terminated original keeps its final newline even though the override
1002
+ # return value itself ends in one
1003
+ assert_equal "a\nx\n", File.read(File.join(@tmpdir, "term.txt"))
1004
+ end
1005
+
1006
+ def test_dispatch_custom_op_authorization_modes
1007
+ register_text_kind
1008
+ write_calls = []
1009
+ @agent.register_path_kind(
1010
+ "fancy",
1011
+ "roots" => {"project" => @tmpdir},
1012
+ "operations" => {
1013
+ "no_mode" => {
1014
+ "parameters" => {"properties" => {}, "required" => []},
1015
+ "implementation" => step5_spy(write_calls),
1016
+ "type" => :full
1017
+ },
1018
+ "read_mode" => {
1019
+ "parameters" => {"properties" => {}, "required" => []},
1020
+ "implementation" => step5_spy(write_calls),
1021
+ "authorization" => :read,
1022
+ "type" => :full
1023
+ }
1024
+ }
1025
+ )
1026
+
1027
+ table = @agent.path_kind_definition("fancy")["operation_table"]
1028
+ assert_equal :write, table["no_mode"]["authorization"]
1029
+ assert_equal :read, table["read_mode"]["authorization"]
1030
+
1031
+ @agent.path_write("kind" => "fancy", "path" => "ro.txt", "content" => "ok")
1032
+ result = @agent.path_dispatch(
1033
+ kind: "fancy", operation: "read_mode", "path" => "ro.txt"
1034
+ )
1035
+ assert_equal "SPY", result
1036
+ end
1037
+
1038
+ def test_dispatch_builtin_without_override_uses_engine_default
1039
+ register_text_kind
1040
+ @agent.path_write("kind" => "text", "path" => "one.txt", "content" => "x")
1041
+
1042
+ result = @agent.path_dispatch(
1043
+ kind: "text", operation: "read", "path" => "one.txt"
1044
+ )
1045
+ assert_equal "x", result["content"]
1046
+ end
1047
+ def test_override_does_not_resurrect_capability_disabled_builtin
1048
+ # The "kind" tool is agent-global and the capability gate lives inside the
1049
+ # engine method, so an override can never re-enable edit for a kind that
1050
+ # has the edit capability off. The direct engine call must still refuse.
1051
+ register_text_kind("locked", "edit" => false)
1052
+ @agent.register_path_kind(
1053
+ "fancy",
1054
+ "roots" => {"project" => @tmpdir},
1055
+ "capabilities" => {"edit" => false},
1056
+ "overrides" => {
1057
+ "edit" => ->(agent, target, args) { "OVERRIDDEN" }
1058
+ }
1059
+ )
1060
+
1061
+ assert_equal false, @agent.path_kind_capable?("fancy", "edit")
1062
+
1063
+ exception = assert_raise(ParameterException) do
1064
+ @agent.path_edit(
1065
+ "kind" => "fancy",
1066
+ "path" => "a.txt",
1067
+ "selector" => {"type" => "lines", "start" => 1},
1068
+ "replacement" => "x"
1069
+ )
1070
+ end
1071
+ assert exception.message.include?("does not support edit")
1072
+ end
1073
+
1074
+ def test_custom_operation_parameter_colliding_with_path_arguments_is_rejected
1075
+ error = assert_raise(ParameterException) do
1076
+ @agent.register_path_kind(
1077
+ "alpha",
1078
+ "roots" => {"project" => @tmpdir},
1079
+ "operations" => {
1080
+ "weird_op" => {
1081
+ "description" => "Weird",
1082
+ "parameters" => {
1083
+ "properties" => {"kind" => {"type" => "string"}},
1084
+ "required" => []
1085
+ },
1086
+ "implementation" => ->(content, args) { content }
1087
+ }
1088
+ }
1089
+ )
1090
+ end
1091
+ assert error.message.include?("weird_op")
1092
+ assert error.message.include?("kind")
1093
+ end
1094
+
1095
+ def test_custom_operation_name_collision_with_builtin_is_rejected
1096
+ register_text_kind
1097
+
1098
+ assert_raise(ParameterException) do
1099
+ @agent.register_path_kind(
1100
+ "bad",
1101
+ "roots" => {"project" => @tmpdir},
1102
+ "operations" => {"edit" => ->(_a, _t, _args) { "x" }}
1103
+ )
1104
+ end
1105
+
1106
+ assert !@agent.path_kind_registry.key?("bad")
1107
+ end
1108
+
1109
+ def test_custom_operation_without_callable_is_rejected
1110
+ register_text_kind
1111
+
1112
+ assert_raise(ParameterException) do
1113
+ @agent.register_path_kind(
1114
+ "bad",
1115
+ "roots" => {"project" => @tmpdir},
1116
+ "operations" => {"noop" => {"implementation" => :not_callable}}
1117
+ )
1118
+ end
1119
+ end
1120
+
1121
+ # ------------------------------------------------------------------
1122
+ # Step 2 (DC-1) — write override dispatch and override-type plumbing
1123
+ # ------------------------------------------------------------------
1124
+
1125
+ def register_write_override_kind(spec, name = "fancy")
1126
+ @agent.register_path_kind(
1127
+ name,
1128
+ "roots" => {"project" => @tmpdir},
1129
+ "overrides" => {"write" => spec}
1130
+ )
1131
+ end
1132
+
1133
+ def test_write_content_override_transforms_content_and_keeps_result_shape
1134
+ calls = []
1135
+ register_write_override_kind(
1136
+ lambda { |content, args|
1137
+ calls << [content, args["path"]]
1138
+ "TRANSFORMED:" + content
1139
+ }
1140
+ )
1141
+
1142
+ result = @agent.path_write(
1143
+ "kind" => "fancy", "path" => "note.txt", "content" => "body"
1144
+ )
1145
+
1146
+ assert_equal [["body", "note.txt"]], calls
1147
+ target = File.join(@tmpdir, "note.txt")
1148
+ assert_equal "TRANSFORMED:body", File.read(target)
1149
+ assert_equal %w[doc_id kind location path target_file written],
1150
+ result.keys.sort
1151
+ assert_equal target, result["target_file"]
1152
+ assert_equal true, result["written"]
1153
+ end
1154
+
1155
+ def test_write_content_override_default_type_is_content
1156
+ register_write_override_kind(lambda { |content, args| content })
1157
+
1158
+ table = @agent.path_kind_definition("fancy")["operation_table"]
1159
+ overrides = @agent.path_kind_definition("fancy")["overrides"]
1160
+ assert_equal :content, overrides["write"]["type"]
1161
+ assert_equal :content, table["write"]["type"]
1162
+ end
1163
+
1164
+ def test_write_full_override_fires_with_agent_target_and_args
1165
+ calls = []
1166
+ register_write_override_kind(
1167
+ "implementation" => lambda { |agent, target, args|
1168
+ calls << [agent, target, args["path"]]
1169
+ FileUtils.mkdir_p(File.dirname(target.to_s))
1170
+ File.write(target.to_s, "HANDLER WROTE\n")
1171
+ {"bytes" => 13, "target_file" => "handler-lie.txt"}
1172
+ },
1173
+ "type" => :full
1174
+ )
1175
+
1176
+ result = @agent.path_write(
1177
+ "kind" => "fancy", "path" => "full.txt", "content" => "ignored"
1178
+ )
1179
+
1180
+ target = File.join(@tmpdir, "full.txt")
1181
+ assert_equal 1, calls.length
1182
+ assert_same @agent, calls.first.first
1183
+ assert_equal target, calls.first[1]
1184
+ assert_equal "full.txt", calls.first[2]
1185
+ assert_equal "HANDLER WROTE\n", File.read(target)
1186
+ assert_equal target, result["target_file"]
1187
+ assert_equal 13, result["bytes"]
1188
+ # Canonical keys are engine-authoritative: the handler's attempt to
1189
+ # redirect target_file is ignored, and written reflects reality.
1190
+ assert_equal true, result["written"]
1191
+ end
1192
+
1193
+ def test_write_content_override_non_string_return_is_parameter_error
1194
+ register_write_override_kind(
1195
+ lambda { |content, args| {"nope" => content} }
1196
+ )
1197
+
1198
+ error = assert_raise(ParameterException) do
1199
+ @agent.path_write(
1200
+ "kind" => "fancy", "path" => "note.txt", "content" => "body"
1201
+ )
1202
+ end
1203
+ assert_match(/must return a String/, error.message)
1204
+
1205
+ refute File.exist?(File.join(@tmpdir, "note.txt"))
1206
+ end
1207
+
1208
+ def test_write_content_override_non_string_return_serializes_through_tool
1209
+ register_write_override_kind(
1210
+ lambda { |content, args| [content] }
1211
+ )
1212
+
1213
+ tools = @agent.instance_variable_get(:@other_options)[:tools]
1214
+ result = JSON.parse(
1215
+ tools["path_write"].first.call(
1216
+ "path_write",
1217
+ {"kind" => "fancy", "path" => "note.txt", "content" => "body"}
1218
+ )
1219
+ )
1220
+
1221
+ assert_match(/must return a String/, result["exception"])
1222
+ assert result.key?("exception_line")
1223
+ end
1224
+
1225
+ def test_write_override_invalid_type_rejected_at_registration
1226
+ error = assert_raise(ParameterException) do
1227
+ register_write_override_kind(
1228
+ "implementation" => ->(c, a) { c },
1229
+ "type" => :partial
1230
+ )
1231
+ end
1232
+ assert_match(/type must be one of :content, :full; got :partial/,
1233
+ error.message)
1234
+ end
1235
+
1236
+ def test_patch_full_override_rejected_at_registration
1237
+ error = assert_raise(ParameterException) do
1238
+ @agent.register_path_kind(
1239
+ "patchy",
1240
+ "roots" => {"project" => @tmpdir},
1241
+ "capabilities" => {"patch" => true},
1242
+ "overrides" => {
1243
+ "patch" => {
1244
+ "implementation" => ->(agent, target, args) { target },
1245
+ "type" => :full
1246
+ }
1247
+ }
1248
+ )
1249
+ end
1250
+ assert_match(/patch.*type must be one of :content; got :full/,
1251
+ error.message)
1252
+ assert_match(/dispatches its override as a content transform/,
1253
+ error.message)
1254
+ end
1255
+
1256
+ def test_override_on_non_dispatching_builtin_rejected_at_registration
1257
+ register_text_kind
1258
+
1259
+ error = assert_raise(ParameterException) do
1260
+ @agent.register_path_kind(
1261
+ "dead",
1262
+ "roots" => {"project" => @tmpdir},
1263
+ "overrides" => {"read" => ->(content, args) { content }}
1264
+ )
1265
+ end
1266
+
1267
+ assert_match(/Override "read" cannot be dispatched/, error.message)
1268
+ assert_match(/only consults overrides for write, edit, patch/,
1269
+ error.message)
1270
+ assert_match(/definition\["list"\]/, error.message)
1271
+ end
1272
+
1273
+ def test_write_override_authorization_failure_runs_no_handler
1274
+ %i[content full].each do |type|
1275
+ calls = []
1276
+ spec = {"implementation" => ->(*) { calls << type }}
1277
+ spec["type"] = type if type == :full
1278
+ @agent.register_path_kind(
1279
+ "locked_#{type}",
1280
+ "roots" => {"project" => @tmpdir},
1281
+ "overrides" => {"write" => spec}
1282
+ )
1283
+
1284
+ assert_raise(ParameterException) do
1285
+ @agent.path_write(
1286
+ "kind" => "locked_#{type}",
1287
+ "path" => "../../etc/hostname",
1288
+ "content" => "denied"
1289
+ )
1290
+ end
1291
+
1292
+ assert_empty calls
1293
+ refute File.exist?("/nonexistent-root-f.xyz")
1294
+ end
1295
+ end
1296
+
1297
+ def test_override_registration_selects_override_implementation
1298
+ override_calls = []
1299
+ register_text_kind
1300
+
1301
+ @agent.register_path_kind(
1302
+ "fancy",
1303
+ "roots" => {"project" => @tmpdir},
1304
+ "overrides" => {
1305
+ "edit" => ->(_agent, _target, _args) { override_calls << _args; "overridden" }
1306
+ }
1307
+ )
1308
+
1309
+ assert @agent.path_kind_definition("fancy")["overrides"].key?("edit")
1310
+ assert !@agent.path_kind_definition("text").fetch("overrides", {}).key?("edit")
1311
+ assert_equal "overridden", @agent.path_operation_implementation("fancy", "edit").call(nil, nil, nil)
1312
+ impl = @agent.path_operation_implementation("fancy", "edit")
1313
+ assert_same impl, @agent.path_kind_definition("fancy")["overrides"]["edit"]["implementation"]
1314
+ assert_equal :write,
1315
+ @agent.path_kind_definition("fancy")["operation_table"]["edit"]["authorization"]
1316
+ assert @agent.path_operation_support?("fancy", "edit")
1317
+ end
1318
+
1319
+ def test_override_must_target_builtin_and_requires_callable
1320
+ register_text_kind
1321
+
1322
+ assert_raise(ParameterException) do
1323
+ @agent.register_path_kind(
1324
+ "bad",
1325
+ "roots" => {"project" => @tmpdir},
1326
+ "overrides" => {"precise_edit" => ->(_a, _t, _args) { "x" }}
1327
+ )
1328
+ end
1329
+
1330
+ assert_raise(ParameterException) do
1331
+ @agent.register_path_kind(
1332
+ "bad2",
1333
+ "roots" => {"project" => @tmpdir},
1334
+ "overrides" => {"edit" => :not_callable}
1335
+ )
1336
+ end
1337
+ end
1338
+
1339
+ def test_built_in_capability_queries_follow_defaults
1340
+ register_text_kind
1341
+
1342
+ assert @agent.path_operation_support?("text", "read")
1343
+ assert @agent.path_operation_support?("text", "write")
1344
+ assert @agent.path_operation_support?("text", "edit")
1345
+ assert @agent.path_operation_support?("text", "list")
1346
+ assert @agent.path_operation_support?("text", "move")
1347
+ assert @agent.path_operation_support?("text", "rename")
1348
+ assert @agent.path_operation_support?("text", "delete")
1349
+ assert !@agent.path_operation_support?("text", "validate")
1350
+ assert !@agent.path_operation_support?("text", "promote")
1351
+ assert !@agent.path_operation_support?("text", "smoke")
1352
+ assert !@agent.path_operation_support?("text", "precise_edit")
1353
+ table = @agent.path_kind_definition("text")["operation_table"]
1354
+ assert_equal :read, table["read"]["authorization"]
1355
+ assert_equal :read, table["list"]["authorization"]
1356
+ assert_equal :write, table["write"]["authorization"]
1357
+ assert_equal :write, table["delete"]["authorization"]
1358
+ assert_nil table["validate"]
1359
+ end
1360
+
1361
+ def test_registry_is_agent_local
1362
+ register_text_kind
1363
+
1364
+ other = LLM.agent
1365
+ @agent.register_path_kind(
1366
+ "fancy",
1367
+ "roots" => {"project" => @tmpdir},
1368
+ "operations" => {"precise_edit" => ->(_a, _t, _args) { "ok" }}
1369
+ )
1370
+
1371
+ assert @agent.path_kind_registry.key?("fancy")
1372
+ assert !other.path_kind_registry.key?("fancy")
1373
+ assert_equal ["precise_edit"],
1374
+ @agent.path_kind_registry.values
1375
+ .flat_map { |d| d.fetch("operations", {}).keys }.uniq.sort
1376
+ assert other.path_kind_registry.values
1377
+ .flat_map { |d| d.fetch("operations", {}).keys }.empty?
1378
+
1379
+ assert_raise(ParameterException) do
1380
+ other.path_kind_definition("fancy")
1381
+ end
1382
+ end
1383
+
1384
+ def test_path_kinds
1385
+ register_text_kind
1386
+
1387
+ result = @agent.path_kinds
1388
+
1389
+ assert result.key?("text")
1390
+ assert_equal "Test text paths", result["text"]["description"]
1391
+ assert_equal ["project", "tmp"], result["text"]["locations"]
1392
+ assert_equal "project", result["text"]["default_location"]
1393
+ assert_equal true, result["text"]["capabilities"]["rename"]
1394
+ end
1395
+
1396
+ # Fix 5: the path_kinds kind input is an opt-in filter. With more than
1397
+ # one kind registered, supplying one kind returns exactly that kind's
1398
+ # entry, in the same per-kind shape the full map uses (test_plugin_loading_
1399
+ # installs_tools_on_llm_agent pins the omitted/{} full-map case).
1400
+ def test_path_kinds_kind_input_returns_only_that_kind
1401
+ register_text_kind("text")
1402
+ register_text_kind("other")
1403
+
1404
+ result = @agent.path_kinds({"kind" => "other"})
1405
+
1406
+ assert_equal ["other"], result.keys
1407
+ assert_equal "Test text paths", result["other"]["description"]
1408
+
1409
+ full = @agent.path_kinds
1410
+ assert_equal ["text", "other"].sort, full.keys.sort
1411
+ # Same per-kind shape as a full-map entry.
1412
+ assert_equal full["other"].keys.sort, result["other"].keys.sort
1413
+ assert_equal full["other"]["capabilities"], result["other"]["capabilities"]
1414
+ end
1415
+
1416
+ # Fix 3, path_list side: "." and "./" are root selectors, not file paths.
1417
+ # On a populated extension-bearing kind they list exactly what an omitted
1418
+ # (or empty) prefix lists: the whole kind root.
1419
+ def test_path_list_root_selector_lists_the_kind_root
1420
+ @agent.register_path_kind(
1421
+ "rbkind",
1422
+ "description" => "Extension-bearing kind",
1423
+ "locations" => ["project"],
1424
+ "default_location" => "project",
1425
+ "roots" => {"project" => File.join(@tmpdir, "rbkind")},
1426
+ "extension" => "rb",
1427
+ "capabilities" => {"list" => true}
1428
+ )
1429
+ root = File.join(@tmpdir, "rbkind")
1430
+ FileUtils.mkdir_p(File.join(root, "sub"))
1431
+ File.write(File.join(root, "alpha.rb"), "x\n")
1432
+ File.write(File.join(root, "sub", "beta.rb"), "y\n")
1433
+
1434
+ listing = ->(path) { @agent.path_list({"kind" => "rbkind"}.merge(path || {})) }
1435
+
1436
+ expected = ["alpha.rb", "sub/beta.rb"]
1437
+ assert_equal expected, listing.call(nil), "omitted path lists the root"
1438
+ assert_equal expected, listing.call("path" => ""), "empty prefix lists the root"
1439
+ assert_equal expected, listing.call("path" => "."), "dot selector lists the root"
1440
+ assert_equal expected, listing.call("path" => "./"), "dot-slash selector lists the root"
1441
+ end
1442
+
1443
+ # Fix 3, dispatch exemption: path_dispatch applies no root-selector
1444
+ # grammar of its own. A kind-defined custom operation legitimately
1445
+ # receives "." (an enumeration client's suite scope does exactly that),
1446
+ # so the raw value must reach the implementation untouched.
1447
+ def test_path_dispatch_passes_root_selector_to_custom_operation
1448
+ seen = []
1449
+ @agent.register_path_kind(
1450
+ "custom",
1451
+ "description" => "Custom-operation kind",
1452
+ "locations" => ["project"],
1453
+ "default_location" => "project",
1454
+ "roots" => {"project" => File.join(@tmpdir, "custom")},
1455
+ "operations" => {
1456
+ "peek" => {
1457
+ "description" => "Record the raw path argument.",
1458
+ "parameters" => {"properties" => {}, "required" => []},
1459
+ "implementation" => lambda { |_agent, _target, args|
1460
+ seen << args["path"]
1461
+ "PEEK #{args["path"]}"
1462
+ },
1463
+ "type" => :full,
1464
+ "authorization" => :read
1465
+ }
1466
+ }
1467
+ )
1468
+ FileUtils.mkdir_p(File.join(@tmpdir, "custom"))
1469
+
1470
+ result = @agent.path_dispatch(
1471
+ kind: "custom", operation: "peek", "path" => "."
1472
+ )
1473
+
1474
+ assert_equal "PEEK .", result
1475
+ assert_equal ["."], seen, "the custom operation must receive the root selector as given"
1476
+ end
1477
+
1478
+ def test_path_kind_specific_operation
1479
+ calls = []
1480
+
1481
+ @agent.register_path_kind(
1482
+ "text",
1483
+ "locations" => ["project"],
1484
+ "default_location" => "project",
1485
+ "roots" => {"project" => @tmpdir},
1486
+ "capabilities" => {"validate" => true},
1487
+ "validate" => lambda do |agent, path, location, args|
1488
+ calls << [agent, path, location, args]
1489
+ {"valid" => true, "path" => path}
1490
+ end
1491
+ )
1492
+
1493
+ result = @agent.path_validate(
1494
+ "kind" => "text",
1495
+ "path" => "hello.txt"
1496
+ )
1497
+
1498
+ assert_equal true, result["valid"]
1499
+ assert_equal "hello.txt", result["path"]
1500
+ assert_equal @agent, calls.first[0]
1501
+ assert_equal "hello.txt", calls.first[1]
1502
+ assert_equal "project", calls.first[2]
1503
+ end
1504
+
1505
+ def test_path_kind_specific_operation_requires_capability
1506
+ register_text_kind
1507
+
1508
+ assert_raise(ParameterException) do
1509
+ @agent.path_validate(
1510
+ "kind" => "text",
1511
+ "path" => "hello.txt"
1512
+ )
1513
+ end
1514
+ end
1515
+
1516
+ def test_path_kind_specific_operation_requires_implementation
1517
+ @agent.register_path_kind(
1518
+ "text",
1519
+ "locations" => ["project"],
1520
+ "roots" => {"project" => @tmpdir},
1521
+ "capabilities" => {"validate" => true}
1522
+ )
1523
+
1524
+ assert_raise(ParameterException) do
1525
+ @agent.path_validate(
1526
+ "kind" => "text",
1527
+ "path" => "hello.txt"
1528
+ )
1529
+ end
1530
+ end
1531
+
1532
+ def test_load_path_plugin
1533
+ plugin = File.join(@tmpdir, "plugin.rb")
1534
+ FileUtils.mkdir_p(File.dirname(plugin))
1535
+
1536
+ File.write(
1537
+ plugin,
1538
+ <<~RUBY
1539
+ register_path_kind "plugin",
1540
+ "locations" => ["project"],
1541
+ "roots" => {"project" => #{@tmpdir.inspect}}
1542
+ RUBY
1543
+ )
1544
+
1545
+ @agent.load_path_plugins(plugin)
1546
+
1547
+ assert @agent.path_kinds.key?("plugin")
1548
+ tools = @agent.instance_variable_get(:@other_options)[:tools]
1549
+ assert tools.key?("path_kinds")
1550
+ assert_equal ["plugin"], tools["path_kinds"][1]["parameters"]["properties"]["kind"]["enum"]
1551
+ end
1552
+
1553
+ def test_path_tool_definitions_are_installed
1554
+ register_text_kind
1555
+
1556
+ tools = @agent.instance_variable_get(:@other_options)[:tools]
1557
+
1558
+ assert tools.key?("path_kinds")
1559
+ assert tools.key?("path_read")
1560
+ assert tools.key?("path_write")
1561
+ assert tools.key?("path_edit")
1562
+ assert tools.key?("path_list")
1563
+ assert tools.key?("path_move")
1564
+ assert tools.key?("path_rename")
1565
+ assert tools.key?("path_delete")
1566
+
1567
+ # These should not be installed until at least one kind supports them.
1568
+ assert !tools.key?("path_validate")
1569
+ assert !tools.key?("path_test")
1570
+ assert !tools.key?("path_smoke")
1571
+ assert !tools.key?("path_promote")
1572
+ end
1573
+
1574
+ def test_special_tool_definitions_are_installed_when_supported
1575
+ @agent.register_path_kind(
1576
+ "text",
1577
+ "locations" => ["project"],
1578
+ "roots" => {"project" => @tmpdir},
1579
+ "capabilities" => {
1580
+ "validate" => true,
1581
+ "promote" => true
1582
+ },
1583
+ "validate" => ->(*args) { true },
1584
+ "promote" => ->(*args) { true }
1585
+ )
1586
+
1587
+ tools = @agent.instance_variable_get(:@other_options)[:tools]
1588
+
1589
+ assert tools.key?("path_validate")
1590
+ assert !tools.key?("path_test")
1591
+ assert tools.key?("path_promote")
1592
+ end
1593
+
1594
+ def test_path_tool_definition_contains_registered_kind_enum
1595
+ register_text_kind
1596
+
1597
+ definition = @agent.path_tool_definition("read")
1598
+
1599
+ assert_equal ["text"],
1600
+ definition["parameters"]["properties"]["kind"]["enum"]
1601
+ end
1602
+
1603
+ def test_path_tool_definition_list_path_is_optional
1604
+ register_text_kind
1605
+
1606
+ definition = @agent.path_tool_definition("list")
1607
+
1608
+ assert_equal ["kind"], definition["parameters"]["required"]
1609
+ assert definition["parameters"]["properties"].key?("path")
1610
+ assert_match(/Optional subdirectory/,
1611
+ definition["parameters"]["properties"]["path"]["description"])
1612
+ end
1613
+
1614
+ def test_path_tool_definition_does_not_add_location_to_list
1615
+ register_text_kind
1616
+
1617
+ definition = @agent.path_tool_definition("list")
1618
+ properties = definition["parameters"]["properties"]
1619
+
1620
+ assert properties.key?("kind")
1621
+ assert properties.key?("path")
1622
+ assert !properties.key?("location")
1623
+ end
1624
+
1625
+ def test_path_tool_definition_write_contains_content
1626
+ register_text_kind
1627
+
1628
+ definition = @agent.path_tool_definition("write")
1629
+ properties = definition["parameters"]["properties"]
1630
+
1631
+ assert properties.key?("content")
1632
+ end
1633
+
1634
+ def test_path_tool_definition_edit_contains_selector_and_replacement
1635
+ register_text_kind
1636
+
1637
+ definition = @agent.path_tool_definition("edit")
1638
+ properties = definition["parameters"]["properties"]
1639
+
1640
+ assert properties.key?("selector")
1641
+ assert properties.key?("replacement")
1642
+ end
1643
+
1644
+ def test_path_tool_definition_move_contains_destination
1645
+ register_text_kind
1646
+
1647
+ definition = @agent.path_tool_definition("move")
1648
+ properties = definition["parameters"]["properties"]
1649
+
1650
+ assert properties.key?("destination")
1651
+ end
1652
+
1653
+ def test_path_tool_definition_rename_contains_name
1654
+ register_text_kind
1655
+
1656
+ definition = @agent.path_tool_definition("rename")
1657
+ properties = definition["parameters"]["properties"]
1658
+
1659
+ assert properties.key?("name")
1660
+ end
1661
+
1662
+ def test_path_tool_definition_promote_contains_optional_defer_reload
1663
+ register_text_kind("text", {"promote" => true})
1664
+
1665
+ definition = @agent.path_tool_definition("promote")
1666
+ parameters = definition["parameters"]
1667
+ properties = parameters["properties"]
1668
+
1669
+ # The ScoutCoder promotion hook consumes this caller argument; engine
1670
+ # dispatch passes caller keys to the hook verbatim, so the schema must
1671
+ # advertise it for schema-validating model callers to be able to emit it.
1672
+ assert properties.key?("defer_reload")
1673
+ assert_equal "boolean", properties["defer_reload"]["type"]
1674
+
1675
+ # Omittable: omitting the flag keeps the default immediate reload.
1676
+ assert !parameters["required"].include?("defer_reload")
1677
+
1678
+ # The closed-parameter contract is retained.
1679
+ assert_equal false, parameters["additionalProperties"]
1680
+
1681
+ # Scope: promote-branch only; no other operation grows the property.
1682
+ read_definition = @agent.path_tool_definition("read")
1683
+ assert !read_definition["parameters"]["properties"].key?("defer_reload")
1684
+ end
1685
+
1686
+ # ------------------------------------------------------------------
1687
+ # Error normalization matrix
1688
+ #
1689
+ # Every expected invalid-input state must surface as the framework's
1690
+ # ParameterException, never as an incidental KeyError/ArgumentError
1691
+ # raised by an implementation detail.
1692
+ # ------------------------------------------------------------------
1693
+
1694
+ def test_registry_starts_empty_on_fresh_agent
1695
+ agent = LLM.agent
1696
+ assert_equal({}, agent.path_kind_registry)
1697
+ end
1698
+
1699
+ def test_registry_registration_is_agent_local
1700
+ first = LLM.agent
1701
+ second = LLM.agent
1702
+
1703
+ first.register_path_kind(
1704
+ "isolated",
1705
+ "description" => "Registered only on one agent",
1706
+ "locations" => ["project"],
1707
+ "roots" => {"project" => @tmpdir}
1708
+ )
1709
+
1710
+ assert first.path_kind_registry.key?("isolated")
1711
+ refute second.path_kind_registry.key?("isolated")
1712
+ end
1713
+
1714
+ def test_registry_has_no_class_level_surface_left
1715
+ assert !LLM::Agent.respond_to?(:register_path_kind)
1716
+ assert !LLM::Agent.const_defined?(:PATH_KINDS, false)
1717
+ end
1718
+
1719
+ def test_registry_path_kinds_tool_reflects_instance_registrations
1720
+ register_text_kind
1721
+
1722
+ kinds = @agent.path_kinds({})
1723
+
1724
+ assert kinds.key?("text")
1725
+ assert_equal "Test text paths", kinds["text"]["description"]
1726
+ end
1727
+
1728
+ # ------------------------------------------------------------------
1729
+ # smoke operation surface
1730
+ # ------------------------------------------------------------------
1731
+
1732
+ def test_smoke_tool_absent_when_no_kind_enables_smoke
1733
+ register_text_kind
1734
+
1735
+ tools = @agent.instance_variable_get(:@other_options)[:tools]
1736
+
1737
+ assert !tools.key?("path_smoke")
1738
+ end
1739
+
1740
+ def test_smoke_tool_present_when_kind_enables_smoke
1741
+ register_text_kind("smoke", {"smoke" => true})
1742
+
1743
+ tools = @agent.instance_variable_get(:@other_options)[:tools]
1744
+
1745
+ assert tools.key?("path_smoke")
1746
+ end
1747
+
1748
+ def test_smoke_operation_runs_kind_callback
1749
+ seen = []
1750
+ @agent.register_path_kind(
1751
+ "view",
1752
+ "description" => "Smoke views",
1753
+ "locations" => ["project"],
1754
+ "roots" => {"project" => @tmpdir},
1755
+ "capabilities" => {"smoke" => true},
1756
+ "smoke" => ->(agent, path, location, args) { seen << [path, location, args["kind"]] }
1757
+ )
1758
+
1759
+ @agent.path_smoke("kind" => "view", "path" => "show.slim")
1760
+
1761
+ assert_equal [["show.slim", "project", "view"]], seen
1762
+ end
1763
+
1764
+ def test_smoke_is_denied_outside_grants
1765
+ called = false
1766
+ outside = Dir.mktmpdir("path-outside")
1767
+ @agent.register_path_kind(
1768
+ "smoke", "locations" => ["tmp"], "default_location" => "tmp",
1769
+ "roots" => {"tmp" => @tmpdir}, "capabilities" => {"smoke" => true},
1770
+ "resolve" => ->(_agent, _path, _location) { outside },
1771
+ "smoke" => ->(*) { called = true }
1772
+ )
1773
+
1774
+ assert_raise(ParameterException) do
1775
+ @agent.path_smoke("kind" => "smoke", "path" => "item")
1776
+ end
1777
+ assert_false called
1778
+ ensure
1779
+ FileUtils.remove_entry(outside) if outside && File.exist?(outside)
1780
+ end
1781
+
1782
+ def test_test_capability_is_rejected_after_test_removal
1783
+ # `test` is no longer a built-in operation: the registration-time
1784
+ # capability validation (BUILT_IN_OPERATIONS membership) rejects it
1785
+ # with ParameterException, and no path_test wrapper method exists.
1786
+ error = assert_raise(ParameterException) do
1787
+ @agent.register_path_kind(
1788
+ "both",
1789
+ "description" => "Smoke kind",
1790
+ "locations" => ["project"],
1791
+ "roots" => {"project" => @tmpdir},
1792
+ "capabilities" => {"smoke" => true, "test" => true}
1793
+ )
1794
+ end
1795
+ assert_match(/Unknown Path kind capability "test"/, error.message)
1796
+
1797
+ assert !@agent.respond_to?(:path_test)
1798
+ assert_raise(NoMethodError) do
1799
+ @agent.path_test("kind" => "text", "path" => "show.slim")
1800
+ end
1801
+ end
1802
+ # ------------------------------------------------------------------
1803
+
1804
+ # ------------------------------------------------------------------
1805
+ # Step 7 — edit-operation distinctness and compatibility
1806
+ # ------------------------------------------------------------------
1807
+
1808
+ # Compare a built-in tool schema ignoring the kind property, which
1809
+ # legitimately grows as kinds register (enum, supported-kinds
1810
+ # description). Everything else must be identical.
1811
+
1812
+ end