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,233 @@
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 TestPathPatch < 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 register_patchy_kind(name = "text", capabilities = {})
32
+ @agent.register_path_kind(
33
+ name,
34
+ "roots" => {
35
+ "project" => @tmpdir,
36
+ "tmp" => File.join(@tmpdir, "tmp")
37
+ },
38
+ "capabilities" => capabilities
39
+ )
40
+ end
41
+
42
+ def simple_patch(original_line, replacement_line, line = 1)
43
+ <<~PATCH
44
+ --- a/f.txt
45
+ +++ b/f.txt
46
+ @@ -#{line} +#{line} @@
47
+ -#{original_line}
48
+ +#{replacement_line}
49
+ PATCH
50
+ end
51
+
52
+ def test_patch_applies_simple_diff_and_preserves_untouched_bytes
53
+ register_patchy_kind("text", {"patch" => true})
54
+ @agent.path_write("kind" => "text", "path" => "f.txt",
55
+ "content" => "one\ntwo\nthree\n")
56
+ result = @agent.path_patch(
57
+ "kind" => "text", "path" => "f.txt",
58
+ "patch" => simple_patch("two", "TWO", 2)
59
+ )
60
+ assert_equal "one\nTWO\nthree\n", result["content"]
61
+ assert_equal "one\nTWO\nthree\n", File.read(File.join(@tmpdir, "f.txt"))
62
+ end
63
+
64
+ def test_patch_context_mismatch_writes_nothing
65
+ register_patchy_kind("text", {"patch" => true})
66
+ @agent.path_write("kind" => "text", "path" => "f.txt", "content" => "aaa\nbbb\n")
67
+ assert_raise(ParameterException) do
68
+ @agent.path_patch("kind" => "text", "path" => "f.txt",
69
+ "patch" => simple_patch("zzz", "y"))
70
+ end
71
+ assert_equal "aaa\nbbb\n", File.read(File.join(@tmpdir, "f.txt"))
72
+ end
73
+
74
+ def test_patch_malformed_text_rejected_before_mutation
75
+ register_patchy_kind("text", {"patch" => true})
76
+ @agent.path_write("kind" => "text", "path" => "f.txt", "content" => "a\n")
77
+ ["", "not a diff", "--- a/f.txt\n+++ b/f.txt\ngarbage\n"].each do |bad|
78
+ exception = assert_raise(ParameterException) do
79
+ @agent.path_patch("kind" => "text", "path" => "f.txt", "patch" => bad)
80
+ end
81
+ assert exception.message.length > 0
82
+ end
83
+ assert_equal "a\n", File.read(File.join(@tmpdir, "f.txt"))
84
+ end
85
+
86
+ def test_patch_multi_file_rejected_before_mutation
87
+ register_patchy_kind("text", {"patch" => true})
88
+ @agent.path_write("kind" => "text", "path" => "f.txt", "content" => "a\n")
89
+ multi = <<~PATCH
90
+ --- a/f.txt
91
+ +++ b/f.txt
92
+ @@ -1 +1 @@
93
+ -a
94
+ +b
95
+ --- a/other.txt
96
+ +++ b/other.txt
97
+ @@ -1 +1 @@
98
+ -x
99
+ +y
100
+ PATCH
101
+ exception = assert_raise(ParameterException) do
102
+ @agent.path_patch("kind" => "text", "path" => "f.txt", "patch" => multi)
103
+ end
104
+ assert exception.message.include?("multi-file"), exception.message
105
+ assert_equal "a\n", File.read(File.join(@tmpdir, "f.txt"))
106
+ refute File.exist?(File.join(@tmpdir, "other.txt"))
107
+ end
108
+
109
+ def test_patch_header_filename_cannot_redirect_target
110
+ register_patchy_kind("text", {"patch" => true})
111
+ @agent.path_write("kind" => "text", "path" => "real.txt", "content" => "a\n")
112
+ result = @agent.path_patch(
113
+ "kind" => "text", "path" => "real.txt",
114
+ "patch" => simple_patch("a", "b").gsub("f.txt", "evil.txt")
115
+ )
116
+ assert_equal "b\n", result["content"]
117
+ assert_equal "b\n", File.read(File.join(@tmpdir, "real.txt"))
118
+ refute File.exist?(File.join(@tmpdir, "evil.txt"))
119
+ end
120
+
121
+ def test_patch_multi_hunk_is_all_or_nothing
122
+ register_patchy_kind("text", {"patch" => true})
123
+ @agent.path_write("kind" => "text", "path" => "f.txt",
124
+ "content" => "one\ntwo\nthree\nfour\nfive\n")
125
+ patch = <<~PATCH
126
+ --- a/f.txt
127
+ +++ b/f.txt
128
+ @@ -1 +1 @@
129
+ -one
130
+ +ONE
131
+ @@ -3 +3 @@
132
+ -NOPE
133
+ +X
134
+ PATCH
135
+ assert_raise(ParameterException) do
136
+ @agent.path_patch("kind" => "text", "path" => "f.txt", "patch" => patch)
137
+ end
138
+ assert_equal "one\ntwo\nthree\nfour\nfive\n",
139
+ File.read(File.join(@tmpdir, "f.txt"))
140
+ end
141
+
142
+ def test_patch_final_newline_preserved_in_both_directions
143
+ register_patchy_kind("text", {"patch" => true})
144
+
145
+ @agent.path_write("kind" => "text", "path" => "plain.txt", "content" => "a\nb")
146
+ @agent.path_patch("kind" => "text", "path" => "plain.txt",
147
+ "patch" => simple_patch("b", "x", 2))
148
+ assert_equal "a\nx", File.read(File.join(@tmpdir, "plain.txt"))
149
+
150
+ @agent.path_write("kind" => "text", "path" => "term.txt", "content" => "a\nb\n")
151
+ @agent.path_patch("kind" => "text", "path" => "term.txt",
152
+ "patch" => simple_patch("b", "x", 2))
153
+ assert_equal "a\nx\n", File.read(File.join(@tmpdir, "term.txt"))
154
+ end
155
+
156
+ def test_patch_no_newline_marker_adds_and_removes_final_newline
157
+ register_patchy_kind("text", {"patch" => true})
158
+
159
+ @agent.path_write("kind" => "text", "path" => "gain.txt", "content" => "a\nb")
160
+ add_nl = <<~PATCH
161
+ --- a/gain.txt
162
+ +++ b/gain.txt
163
+ @@ -2 +2 @@
164
+ -b
165
+ \
166
+ +b
167
+ PATCH
168
+ @agent.path_patch("kind" => "text", "path" => "gain.txt", "patch" => add_nl)
169
+ assert_equal "a\nb\n", File.read(File.join(@tmpdir, "gain.txt"))
170
+
171
+ @agent.path_write("kind" => "text", "path" => "drop.txt", "content" => "a\nb\n")
172
+ drop_nl = <<~PATCH
173
+ --- a/drop.txt
174
+ +++ b/drop.txt
175
+ @@ -2 +2 @@
176
+ -b
177
+ +b
178
+ \
179
+ PATCH
180
+ @agent.path_patch("kind" => "text", "path" => "drop.txt", "patch" => drop_nl)
181
+ assert_equal "a\nb", File.read(File.join(@tmpdir, "drop.txt"))
182
+ end
183
+
184
+ def test_patch_default_off_and_capability_enables
185
+ register_patchy_kind("text")
186
+ @agent.path_write("kind" => "text", "path" => "f.txt", "content" => "a\n")
187
+ assert_equal false, @agent.path_operation_support?("text", "patch")
188
+ assert_raise(ParameterException) do
189
+ @agent.path_patch("kind" => "text", "path" => "f.txt",
190
+ "patch" => simple_patch("a", "b"))
191
+ end
192
+
193
+ register_patchy_kind("patchy", {"patch" => true})
194
+ assert_equal true, @agent.path_operation_support?("patchy", "patch")
195
+ @agent.path_write("kind" => "patchy", "path" => "f.txt", "content" => "a\n")
196
+ @agent.path_patch("kind" => "patchy", "path" => "f.txt",
197
+ "patch" => simple_patch("a", "b"))
198
+ assert_equal "b\n", File.read(File.join(@tmpdir, "f.txt"))
199
+ end
200
+
201
+ def test_patch_content_override_runs_with_authorization
202
+ register_patchy_kind("text", {"patch" => true})
203
+ @agent.register_path_kind(
204
+ "fancy",
205
+ "roots" => {"project" => @tmpdir},
206
+ "capabilities" => {"patch" => true},
207
+ "overrides" => {
208
+ "patch" => ->(content, args) { "OVERRIDDEN:#{content}" }
209
+ }
210
+ )
211
+ @agent.path_write("kind" => "fancy", "path" => "f.txt", "content" => "a\n")
212
+ result = @agent.path_patch(
213
+ "kind" => "fancy", "path" => "f.txt", "patch" => simple_patch("a", "b")
214
+ )
215
+ assert_equal "OVERRIDDEN:a\n", result["content"]
216
+ assert_equal "OVERRIDDEN:a\n", File.read(File.join(@tmpdir, "f.txt"))
217
+ end
218
+
219
+ def test_patch_on_unauthorized_target_writes_nothing
220
+ @agent.register_path_kind(
221
+ "locked",
222
+ "roots" => {"project" => "/nonexistent-root-xyz"},
223
+ "capabilities" => {"patch" => true}
224
+ )
225
+ assert_raise(ParameterException) do
226
+ @agent.path_patch("kind" => "locked", "path" => "f.txt",
227
+ "patch" => simple_patch("a", "b"))
228
+ end
229
+ refute File.exist?("/nonexistent-root-xyz/f.txt")
230
+ end
231
+
232
+
233
+ end
@@ -0,0 +1,386 @@
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 TestPathPromotion < 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_path_promote
32
+ tmp_root = File.join(@tmpdir, "promote_tmp")
33
+ project_root = File.join(@tmpdir, "promote_project")
34
+ [tmp_root, project_root].each { |dir| FileUtils.mkdir_p(dir) }
35
+ File.write(File.join(tmp_root, "show.slim"), "content h1 Hello\n")
36
+
37
+ @agent.register_path_kind(
38
+ "view",
39
+ "locations" => ["tmp", "project"],
40
+ "default_location" => "tmp",
41
+ "roots" => {"tmp" => tmp_root, "project" => project_root},
42
+ "capabilities" => {"promote" => true}
43
+ )
44
+
45
+ result = @agent.path_promote(
46
+ "kind" => "view",
47
+ "path" => "show.slim"
48
+ )
49
+
50
+ assert_equal true, result["promoted"]
51
+ assert_equal "show.slim", result["path"]
52
+ assert_equal "tmp", result["location"]
53
+ assert_equal({"location" => "project", "path" => "show.slim"},
54
+ result["destination"])
55
+ assert_equal "content h1 Hello\n", File.read(File.join(project_root, "show.slim"))
56
+ assert !File.exist?(File.join(tmp_root, "show.slim"))
57
+ end
58
+
59
+ def test_path_promote_requires_capability
60
+ register_text_kind
61
+
62
+ assert_raise(ParameterException) do
63
+ @agent.path_promote(
64
+ "kind" => "text",
65
+ "path" => "hello.txt"
66
+ )
67
+ end
68
+ end
69
+
70
+ def test_path_promote_promotes_without_a_mover_callback
71
+ # Engine-controlled promotion: no "promote" mover callback is required or
72
+ # consulted; the ENGINE performs the move to the authorized destination.
73
+ definition = register_promotable_kind("promote" => nil)
74
+ tmp_root = definition["roots"]["tmp"]
75
+ project_root = definition["roots"]["project"]
76
+ File.write(File.join(tmp_root, "show.slim"), "payload\n")
77
+
78
+ result = @agent.path_promote("kind" => "view", "path" => "show.slim")
79
+
80
+ assert_equal true, result["promoted"]
81
+ assert_equal "payload\n", File.read(File.join(project_root, "show.slim"))
82
+ assert !File.exist?(File.join(tmp_root, "show.slim"))
83
+ end
84
+
85
+ def test_path_promote_legacy_mover_callback_is_not_invoked
86
+ # A legacy "promote" mover callback must NOT be invoked by the engine;
87
+ # the engine is the only writer. Any callback content must be absent.
88
+ called = false
89
+ definition = register_promotable_kind(
90
+ "promote" => lambda do |_agent, _path, _location, _args|
91
+ called = true
92
+ {"promoted" => true}
93
+ end
94
+ )
95
+ tmp_root = definition["roots"]["tmp"]
96
+ File.write(File.join(tmp_root, "show.slim"), "payload\n")
97
+
98
+ @agent.path_promote("kind" => "view", "path" => "show.slim")
99
+
100
+ assert !called
101
+ assert_equal "payload\n",
102
+ File.read(File.join(definition["roots"]["project"], "show.slim"))
103
+ end
104
+
105
+ def test_path_promote_callback_redirect_attack_writes_only_at_authorized_destination
106
+ # REDIRECT ATTACK: a kind whose legacy mover callback tries to write
107
+ # somewhere else. Content must land ONLY at the engine-authorized
108
+ # destination; the redirect target must not be created.
109
+ outside = Dir.mktmpdir("path-outside")
110
+ redirect_target = File.join(outside, "hijacked.txt")
111
+ definition = register_promotable_kind(
112
+ "promote" => lambda do |_agent, _path, _location, args|
113
+ File.write(redirect_target, "hijacked")
114
+ FileUtils.rm(args["promotion_source_target"])
115
+ {"promoted" => true, "destination" => redirect_target}
116
+ end
117
+ )
118
+ tmp_root = definition["roots"]["tmp"]
119
+ project_root = definition["roots"]["project"]
120
+ source = File.join(tmp_root, "show.slim")
121
+ File.write(source, "legitimate\n")
122
+
123
+ result = @agent.path_promote("kind" => "view", "path" => "show.slim")
124
+
125
+ authorized = File.join(project_root, "show.slim")
126
+ assert_equal true, result["promoted"]
127
+ # The engine's authorized endpoint wins over any callback claim.
128
+ assert_equal authorized, result["promotion_target"]
129
+ assert_equal({"location" => "project", "path" => "show.slim"},
130
+ result["destination"])
131
+ assert_equal "legitimate\n", File.read(authorized)
132
+ assert !File.exist?(redirect_target)
133
+ assert !File.exist?(source)
134
+ # Nothing else was mutated: only the two authorized endpoints changed.
135
+ assert_equal ["show.slim"], Dir.children(project_root).sort
136
+ ensure
137
+ FileUtils.remove_entry(outside) if outside && File.exist?(outside)
138
+ end
139
+
140
+ def test_path_promote_post_move_hook_receives_authorized_endpoints
141
+ hook_calls = []
142
+ definition = register_promotable_kind(
143
+ "promotion_post_move" => lambda do |agent, path, location, args|
144
+ hook_calls << [agent, path, location, args]
145
+ {"extra" => "cleanup-done"}
146
+ end
147
+ )
148
+ tmp_root = definition["roots"]["tmp"]
149
+ File.write(File.join(tmp_root, "show.slim"), "payload\n")
150
+
151
+ result = @agent.path_promote("kind" => "view", "path" => "show.slim")
152
+
153
+ assert_equal 1, hook_calls.length
154
+ agent, path, location, args = hook_calls.first
155
+ assert_equal @agent, agent
156
+ assert_equal "show.slim", path
157
+ assert_equal "tmp", location
158
+ assert_equal File.join(definition["roots"]["project"], "show.slim"),
159
+ args["promotion_target"]
160
+ assert_equal File.join(tmp_root, "show.slim"),
161
+ args["promotion_source_target"]
162
+ assert_equal({"location" => "project", "path" => "show.slim"},
163
+ args["promotion_destination"])
164
+ assert_equal false, args["overwrite"]
165
+ # Hook extras merge into the result without overriding engine fields.
166
+ assert_equal "cleanup-done", result["extra"]
167
+ assert_equal true, result["promoted"]
168
+ end
169
+
170
+ def register_promotable_kind(overrides = {})
171
+ tmp_root = File.join(@tmpdir, "ptmp")
172
+ project_root = File.join(@tmpdir, "pproject")
173
+ outside_root = File.join(@tmpdir, "poutside")
174
+ [tmp_root, project_root, outside_root].each { |dir| FileUtils.mkdir_p(dir) }
175
+
176
+ definition = {
177
+ "locations" => %w[tmp project],
178
+ "default_location" => "tmp",
179
+ "roots" => {"tmp" => tmp_root, "project" => project_root},
180
+ "capabilities" => {"promote" => true},
181
+ "promotion_destination" => {"location" => "project"}
182
+ }.merge(overrides)
183
+
184
+ @agent.register_path_kind("view", definition)
185
+ definition
186
+ end
187
+
188
+ def test_promotion_moves_content_to_project_location
189
+ definition = register_promotable_kind
190
+ tmp_root = definition["roots"]["tmp"]
191
+ project_root = definition["roots"]["project"]
192
+ File.write(File.join(tmp_root, "show.slim"), "content h1 Hello\n")
193
+
194
+ result = @agent.path_promote("kind" => "view", "path" => "show.slim")
195
+
196
+ assert_equal true, result["promoted"]
197
+ assert_equal File.join(project_root, "show.slim"), result["promotion_target"]
198
+ assert_equal({"location" => "project", "path" => "show.slim"},
199
+ result["destination"])
200
+ assert_equal "content h1 Hello\n", File.read(File.join(project_root, "show.slim"))
201
+ assert !File.exist?(File.join(tmp_root, "show.slim"))
202
+ end
203
+
204
+ def test_promotion_denies_source_outside_roots
205
+ definition = register_promotable_kind
206
+ outside = File.join(@tmpdir, "poutside", "rogue.txt")
207
+ File.write(outside, "rogue")
208
+
209
+ error = assert_raise(ParameterException) do
210
+ @agent.path_promote("kind" => "view", "path" => outside)
211
+ end
212
+ assert_match(/Path access denied by sandbox/, error.message)
213
+
214
+ assert_equal "rogue", File.read(outside)
215
+ assert !File.exist?(File.join(definition["roots"]["project"], "rogue.txt"))
216
+ end
217
+
218
+ def test_promotion_destination_location_without_configured_root_is_rejected_at_registration
219
+ error = assert_raise(ParameterException) do
220
+ register_promotable_kind(
221
+ "locations" => %w[tmp elsewhere],
222
+ "promotion_destination" => {"location" => "elsewhere"}
223
+ )
224
+ end
225
+ assert_match(/no root configured for location elsewhere/i, error.message)
226
+ assert_false @agent.path_kind_registry.key?("view")
227
+ assert_equal({}, @agent.instance_variable_get(:@other_options))
228
+ end
229
+
230
+ def test_promotion_denies_destination_traversal
231
+ definition = register_promotable_kind(
232
+ "promotion_destination" => {"location" => "project", "path" => "../escape.txt"}
233
+ )
234
+ source = File.join(definition["roots"]["tmp"], "show.slim")
235
+ File.write(source, "content")
236
+
237
+ error = assert_raise(ParameterException) do
238
+ @agent.path_promote("kind" => "view", "path" => "show.slim")
239
+ end
240
+ assert_match(/Path access denied by sandbox/, error.message)
241
+
242
+ assert File.exist?(source)
243
+ assert !File.exist?(File.join(@tmpdir, "escape.txt"))
244
+ end
245
+
246
+ def test_promotion_denies_absolute_destination_outside_roots
247
+ definition = register_promotable_kind(
248
+ "promotion_destination" => {
249
+ "location" => "project",
250
+ "path" => File.join(@tmpdir, "poutside", "absolute.txt")
251
+ }
252
+ )
253
+ source = File.join(definition["roots"]["tmp"], "show.slim")
254
+ File.write(source, "content")
255
+
256
+ error = assert_raise(ParameterException) do
257
+ @agent.path_promote("kind" => "view", "path" => "show.slim")
258
+ end
259
+ assert_match(/Path access denied by sandbox/, error.message)
260
+
261
+ assert File.exist?(source)
262
+ assert !File.exist?(File.join(@tmpdir, "poutside", "absolute.txt"))
263
+ end
264
+
265
+ def test_promotion_denies_symlinked_destination_escape
266
+ definition = register_promotable_kind(
267
+ "promotion_destination" => {"location" => "project", "path" => "link.slim"}
268
+ )
269
+ source = File.join(definition["roots"]["tmp"], "show.slim")
270
+ File.write(source, "content")
271
+ escape_target = File.join(@tmpdir, "poutside", "escape_target.txt")
272
+ File.symlink(escape_target, File.join(definition["roots"]["project"], "link.slim"))
273
+
274
+ error = assert_raise(ParameterException) do
275
+ @agent.path_promote("kind" => "view", "path" => "show.slim")
276
+ end
277
+ assert_match(/Path access denied by sandbox/, error.message)
278
+
279
+ assert File.exist?(source)
280
+ assert !File.exist?(escape_target)
281
+ end
282
+
283
+ def test_promotion_denies_existing_destination_by_default
284
+ definition = register_promotable_kind
285
+ source = File.join(definition["roots"]["tmp"], "show.slim")
286
+ File.write(source, "new content")
287
+ destination = File.join(definition["roots"]["project"], "show.slim")
288
+ File.write(destination, "old content")
289
+
290
+ error = assert_raise(ParameterException) do
291
+ @agent.path_promote("kind" => "view", "path" => "show.slim")
292
+ end
293
+ assert_match(/Promotion destination already exists/, error.message)
294
+
295
+ assert_equal "old content", File.read(destination)
296
+ assert_equal "new content", File.read(source)
297
+ end
298
+
299
+ def test_promotion_overwrite_replaces_existing_destination
300
+ definition = register_promotable_kind
301
+ source = File.join(definition["roots"]["tmp"], "show.slim")
302
+ File.write(source, "new content")
303
+ destination = File.join(definition["roots"]["project"], "show.slim")
304
+ File.write(destination, "old content")
305
+
306
+ result = @agent.path_promote(
307
+ "kind" => "view", "path" => "show.slim", "overwrite" => true
308
+ )
309
+
310
+ assert_equal true, result["promoted"]
311
+ assert_equal "new content", File.read(destination)
312
+ assert !File.exist?(source)
313
+ end
314
+
315
+ def test_promotion_numeric_destination_specification_is_rejected_at_registration
316
+ error = assert_raise(ParameterException) do
317
+ register_promotable_kind("promotion_destination" => 42)
318
+ end
319
+ assert_match(/Invalid promotion destination for path kind view/, error.message)
320
+ assert_false @agent.path_kind_registry.key?("view")
321
+ assert_equal({}, @agent.instance_variable_get(:@other_options))
322
+ end
323
+
324
+ def test_promotion_callable_destination_spec_is_rejected_at_registration
325
+ called = false
326
+ error = assert_raise(ParameterException) do
327
+ register_promotable_kind(
328
+ "promotion_destination" => lambda do |_agent, _path, _location, _args|
329
+ called = true
330
+ {"location" => "project"}
331
+ end
332
+ )
333
+ end
334
+ assert_match(/Invalid promotion destination for path kind view/, error.message)
335
+ assert_false called
336
+ assert_false @agent.path_kind_registry.key?("view")
337
+ assert_equal({}, @agent.instance_variable_get(:@other_options))
338
+ end
339
+
340
+ def test_promotion_string_destination_spec_selects_location
341
+ definition = register_promotable_kind("promotion_destination" => "project")
342
+ File.write(File.join(definition["roots"]["tmp"], "show.slim"), "content")
343
+
344
+ result = @agent.path_promote("kind" => "view", "path" => "show.slim")
345
+
346
+ assert_equal({"location" => "project", "path" => "show.slim"},
347
+ result["destination"])
348
+ assert File.exist?(File.join(definition["roots"]["project"], "show.slim"))
349
+ assert !File.exist?(File.join(definition["roots"]["tmp"], "show.slim"))
350
+ end
351
+
352
+ def test_promotion_destination_path_renames_target
353
+ definition = register_promotable_kind(
354
+ "promotion_destination" => {"location" => "project", "path" => "release.slim"}
355
+ )
356
+ File.write(File.join(definition["roots"]["tmp"], "show.slim"), "content")
357
+
358
+ result = @agent.path_promote("kind" => "view", "path" => "show.slim")
359
+
360
+ assert_equal File.join(definition["roots"]["project"], "release.slim"),
361
+ result["promotion_target"]
362
+ assert File.exist?(File.join(definition["roots"]["project"], "release.slim"))
363
+ assert !File.exist?(File.join(definition["roots"]["tmp"], "show.slim"))
364
+ end
365
+
366
+ def test_promotion_missing_source_is_rejected_before_anything_moves
367
+ definition = register_promotable_kind
368
+
369
+ error = assert_raise(ParameterException) do
370
+ @agent.path_promote("kind" => "view", "path" => "ghost.slim")
371
+ end
372
+ assert_match(/Path does not exist or is not readable/, error.message)
373
+
374
+ assert !File.exist?(File.join(definition["roots"]["project"], "ghost.slim"))
375
+ end
376
+
377
+ # ------------------------------------------------------------------
378
+ # Selector semantics regression matrix (lines + chars)
379
+ #
380
+ # Lines contract: 0-based start, exclusive end; a line includes its
381
+ # terminator; untouched lines byte-identical; final newline preserved IFF
382
+ # it existed before the edit; empty replacement deletes the span.
383
+ # ------------------------------------------------------------------
384
+
385
+
386
+ end