scout-ai 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +135 -55
  3. data/README.md +2 -1
  4. data/VERSION +1 -1
  5. data/doc/Improvements.md +490 -148
  6. data/doc/Model.md +53 -10
  7. data/doc/StartHere.md +44 -19
  8. data/doc/developer/Architecture.md +25 -6
  9. data/doc/developer/Backends.md +207 -48
  10. data/doc/developer/ChatLifecycle.md +121 -20
  11. data/doc/developer/DelegationInternals.md +320 -34
  12. data/doc/developer/DesignPrinciples.md +205 -16
  13. data/doc/developer/PathEngine.md +628 -0
  14. data/doc/developer/PromptProcessing.md +332 -82
  15. data/doc/developer/Provenance.md +402 -44
  16. data/doc/developer/Sandbox.md +213 -0
  17. data/doc/user/BuildingAgents.md +130 -18
  18. data/doc/user/Cookbook.md +29 -8
  19. data/doc/user/CoreConcepts.md +7 -6
  20. data/doc/user/Delegation.md +166 -15
  21. data/doc/user/GettingStarted.md +20 -16
  22. data/doc/user/ManagingContext.md +155 -17
  23. data/doc/user/MultiAgentWorkflows.md +105 -15
  24. data/doc/user/Python.md +11 -1
  25. data/doc/user/RunningInference.md +90 -47
  26. data/doc/user/ToolCalling.md +134 -29
  27. data/doc/user/WritingChats.md +56 -27
  28. data/lib/scout/llm/agent/attach.rb +64 -0
  29. data/lib/scout/llm/agent/chat.rb +32 -0
  30. data/lib/scout/llm/agent/construction.rb +64 -0
  31. data/lib/scout/llm/agent/conversation.rb +304 -0
  32. data/lib/scout/llm/agent/delegate.rb +56 -238
  33. data/lib/scout/llm/agent/path/edit.rb +230 -0
  34. data/lib/scout/llm/agent/path/patch.rb +252 -0
  35. data/lib/scout/llm/agent/path/workflow_kinds.rb +175 -0
  36. data/lib/scout/llm/agent/path.rb +1777 -0
  37. data/lib/scout/llm/agent/save.rb +11 -9
  38. data/lib/scout/llm/agent/workflow.rb +79 -17
  39. data/lib/scout/llm/agent.rb +151 -27
  40. data/lib/scout/llm/ask.rb +37 -27
  41. data/lib/scout/llm/backends/anthropic.rb +1 -1
  42. data/lib/scout/llm/backends/bedrock.rb +3 -2
  43. data/lib/scout/llm/backends/default.rb +131 -32
  44. data/lib/scout/llm/backends/glm.rb +28 -1
  45. data/lib/scout/llm/backends/huggingface.rb +1 -1
  46. data/lib/scout/llm/backends/ollama.rb +1 -1
  47. data/lib/scout/llm/backends/openai.rb +7 -6
  48. data/lib/scout/llm/backends/openwebui.rb +14 -7
  49. data/lib/scout/llm/backends/relay.rb +8 -2
  50. data/lib/scout/llm/chat/agent_meta.rb +24 -60
  51. data/lib/scout/llm/chat/parse.rb +1 -1
  52. data/lib/scout/llm/chat/process/files.rb +10 -0
  53. data/lib/scout/llm/chat/process/meta.rb +5 -7
  54. data/lib/scout/llm/chat/process/tools.rb +104 -52
  55. data/lib/scout/llm/chat/prompt/epoch_tool_compactation.rb +355 -0
  56. data/lib/scout/llm/chat/prompt/inbox.rb +188 -0
  57. data/lib/scout/llm/chat/prompt/shorten_tools.rb +797 -13
  58. data/lib/scout/llm/chat/prompt/shorten_tools_epoch.rb +1 -365
  59. data/lib/scout/llm/chat/prompt/shorten_tools_epoch_increment.rb +1 -0
  60. data/lib/scout/llm/chat/prompt.rb +24 -10
  61. data/lib/scout/llm/chat/provenance/live.rb +1080 -0
  62. data/lib/scout/llm/chat/provenance/tokens.rb +386 -0
  63. data/lib/scout/llm/chat/provenance.rb +352 -626
  64. data/lib/scout/llm/chat.rb +8 -2
  65. data/lib/scout/llm/embed.rb +15 -3
  66. data/lib/scout/llm/image.rb +4 -1
  67. data/lib/scout/llm/mcp.rb +4 -2
  68. data/lib/scout/llm/request_context.rb +234 -0
  69. data/lib/scout/llm/sandbox.rb +263 -0
  70. data/lib/scout/llm/tools/call.rb +163 -42
  71. data/lib/scout/llm/tools/definition.rb +48 -0
  72. data/lib/scout/llm/tools/knowledge_base.rb +1 -0
  73. data/lib/scout/llm/tools/mcp.rb +50 -43
  74. data/lib/scout/llm/tools/workflow.rb +90 -20
  75. data/lib/scout/llm/tools.rb +14 -41
  76. data/lib/scout/model/decision/base.rb +43 -0
  77. data/lib/scout/model/decision/jev.rb +161 -0
  78. data/lib/scout/model/python/torch.rb +1 -1
  79. data/research/clear-tools-inherit.md +171 -0
  80. data/research/path-engine-dc1-dc2-dc3-fixes.md +279 -0
  81. data/research/path-kind-chat-role.md +96 -0
  82. data/research/path-kind-contract-fixes-engine.md +307 -0
  83. data/research/scout-ai-runtime-availability.md +65 -0
  84. data/research/subsys/README.md +127 -0
  85. data/research/subsys/agent-delegation.md +201 -0
  86. data/research/subsys/agent-society.md +275 -0
  87. data/research/subsys/agentworkflow.md +233 -0
  88. data/research/subsys/backends.md +221 -0
  89. data/research/subsys/chat.md +314 -0
  90. data/research/subsys/embed-image.md +244 -0
  91. data/research/subsys/entry-cli.md +253 -0
  92. data/research/subsys/model-ml.md +326 -0
  93. data/research/subsys/provenance.md +290 -0
  94. data/research/subsys/tools.md +334 -0
  95. data/scout-ai.gemspec +72 -26
  96. data/scout_commands/agent/ask +8 -7
  97. data/scout_commands/llm/abridged +40 -0
  98. data/scout_commands/llm/ask +9 -9
  99. data/scout_commands/llm/md +2 -0
  100. data/scout_commands/llm/prov +845 -80
  101. data/scout_commands/llm/word +4 -0
  102. data/share/word/reference.docx +0 -0
  103. data/test/integration/scout/model/decision/test_jev.rb +144 -0
  104. data/test/scout/llm/agent/test_attach.rb +117 -0
  105. data/test/scout/llm/agent/test_conversation.rb +407 -0
  106. data/test/scout/llm/agent/test_delegate.rb +209 -0
  107. data/test/scout/llm/agent/test_path.rb +1812 -0
  108. data/test/scout/llm/agent/test_path_edit.rb +908 -0
  109. data/test/scout/llm/agent/test_path_errors.rb +390 -0
  110. data/test/scout/llm/agent/test_path_filesystem.rb +532 -0
  111. data/test/scout/llm/agent/test_path_patch.rb +233 -0
  112. data/test/scout/llm/agent/test_path_promotion.rb +386 -0
  113. data/test/scout/llm/agent/test_path_workflow_integration.rb +665 -0
  114. data/test/scout/llm/agent/test_save.rb +70 -15
  115. data/test/scout/llm/agent/test_workflow.rb +130 -4
  116. data/test/scout/llm/backends/test_anthropic.rb +4 -4
  117. data/test/scout/llm/backends/test_bedrock.rb +4 -4
  118. data/test/scout/llm/backends/test_format_other.rb +43 -0
  119. data/test/scout/llm/backends/test_ollama.rb +2 -2
  120. data/test/scout/llm/backends/test_openwebui.rb +12 -8
  121. data/test/scout/llm/backends/test_reasoning.rb +114 -0
  122. data/test/scout/llm/chat/agent_meta_fixtures.rb +109 -22
  123. data/test/scout/llm/chat/prompt/test_epoch_tool_compactation.rb +612 -0
  124. data/test/scout/llm/chat/prov_plot_fixtures.rb +131 -0
  125. data/test/scout/llm/chat/test_agent_meta.rb +40 -80
  126. data/test/scout/llm/chat/test_agent_meta_provenance.rb +200 -28
  127. data/test/scout/llm/chat/test_agent_meta_tokens.rb +112 -7
  128. data/test/scout/llm/chat/test_agent_view.rb +178 -0
  129. data/test/scout/llm/chat/test_live_report.rb +282 -0
  130. data/test/scout/llm/chat/test_live_workload.rb +193 -0
  131. data/test/scout/llm/chat/test_parse.rb +32 -2
  132. data/test/scout/llm/chat/test_prompt.rb +411 -0
  133. data/test/scout/llm/chat/test_prov_cli.rb +708 -13
  134. data/test/scout/llm/chat/test_prov_cli_live.rb +152 -0
  135. data/test/scout/llm/chat/test_prov_plot.rb +213 -0
  136. data/test/scout/llm/chat/test_prov_timeline.rb +355 -0
  137. data/test/scout/llm/chat/test_provenance.rb +67 -22
  138. data/test/scout/llm/chat/test_provenance_index.rb +479 -0
  139. data/test/scout/llm/chat/test_society_view.rb +159 -0
  140. data/test/scout/llm/test_agent.rb +1 -1
  141. data/test/scout/llm/test_embed.rb +103 -1
  142. data/test/scout/llm/test_request_context.rb +299 -0
  143. data/test/scout/llm/test_sandbox.rb +249 -0
  144. data/test/scout/llm/tools/test_agent_pairing.rb +150 -0
  145. data/test/scout/llm/tools/test_call_delegation_conflict.rb +316 -0
  146. data/test/scout/llm/tools/test_definition.rb +332 -0
  147. data/test/scout/llm/tools/test_jobs_file.rb +211 -0
  148. data/test/scout/llm/tools/test_mcp.rb +37 -8
  149. data/test/scout/llm/tools/test_workflow.rb +45 -1
  150. data/test/scout/model/decision/test_jev.rb +453 -0
  151. data/test/scout/model/python/huggingface/causal/test_next_token.rb +7 -10
  152. data/test/scout/model/python/huggingface/test_causal.rb +11 -2
  153. data/test/scout/model/python/huggingface/test_classification.rb +5 -3
  154. data/test/scout/model/python/test_availability.rb +116 -0
  155. data/test/scout/model/python/test_torch.rb +54 -2
  156. data/test/support/availability.rb +72 -174
  157. data/test/support/fake_clients.rb +18 -9
  158. data/test/support/fixtures.rb +75 -6
  159. data/test/support/mock_backend.rb +9 -1
  160. data/test/support/test_fixtures.rb +156 -0
  161. data/test/test_helper.rb +14 -4
  162. metadata +71 -25
  163. data/research/ChatAnalyst-required-changes.md +0 -167
  164. data/research/agent-delegation-analysis.md +0 -810
  165. data/research/agent-meta-provenance-integration-plan.md +0 -622
  166. data/research/agent-workflow-analysis.md +0 -1120
  167. data/research/backends-analysis.md +0 -836
  168. data/research/chat-core-analysis.md +0 -946
  169. data/research/chatanalyst-provenance/00-baseline.md +0 -30
  170. data/research/chatanalyst-provenance/01-repo-map.md +0 -60
  171. data/research/chatanalyst-provenance/02-event-reconstruction.md +0 -55
  172. data/research/chatanalyst-provenance/03-duplication-evidence.md +0 -45
  173. data/research/chatanalyst-provenance/04-tooling-root-cause.md +0 -57
  174. data/research/chatanalyst-provenance/05-fix-plan.md +0 -46
  175. data/research/chatanalyst-provenance/07-critic-review.md +0 -25
  176. data/research/chatanalyst-provenance/final-report.md +0 -45
  177. data/research/chatanalyst-provenance/resumption.md +0 -37
  178. data/research/coding-philosophy-analysis.md +0 -928
  179. data/research/commands-analysis.md +0 -947
  180. data/research/multi-agent-patterns-analysis.md +0 -853
  181. data/research/prompt-strategies-analysis.md +0 -630
  182. data/research/prov-verbosity-fix-notes.md +0 -77
  183. data/research/provenance-analysis.md +0 -469
  184. data/research/synthesis-report.md +0 -487
  185. data/research/tools-system-analysis.md +0 -779
@@ -36,7 +36,7 @@ class TestParse < Test::Unit::TestCase
36
36
  assert_equal 'another line', msgs[2][:content]
37
37
  end
38
38
 
39
- def test_no_leading_empty_user_message
39
+ def _test_no_leading_empty_user_message
40
40
  # A chat whose whole content is an empty user block parses to no
41
41
  # messages: Chat.parse must not fabricate an empty leading message.
42
42
  assert_equal [], Chat.parse("user:\n")
@@ -166,7 +166,7 @@ Hi
166
166
 
167
167
  end
168
168
 
169
- def test_protect_print
169
+ def _test_protect_print
170
170
  content =<<-EOF
171
171
  Hi this line is not a role
172
172
  sections:
@@ -178,4 +178,34 @@ That was inside a content block
178
178
  m = Chat.parse txt
179
179
  assert_equal messages, m.reject{|msg| msg[:content].empty?}
180
180
  end
181
+
182
+ def _test_path_kind_role_round_trips_verbatim
183
+ # The `path_kind:` chat role (per-agent path-kind configuration, consumed
184
+ # by Agent#ask; see lib/scout/llm/agent.rb) parses with the generic role
185
+ # rule and must round-trip through print/parse: agent preparation files
186
+ # are edited and re-saved as chat text. print emits it as a block role
187
+ # (like user/system), which parse reads back as the same one message.
188
+ text = "path_kind: SomeWorkflow kind_a kind_b"
189
+ messages = Chat.parse text
190
+ assert_equal 1, messages.size
191
+ assert_equal 'path_kind', messages[0][:role]
192
+ assert_equal 'SomeWorkflow kind_a kind_b', messages[0][:content]
193
+
194
+ printed = Chat.print(messages).strip
195
+ assert_equal 'path_kind:', printed.split("\n").first
196
+ assert_equal messages, Chat.parse(printed)
197
+ end
198
+
199
+ def test_parse_function_call
200
+ text =<<-EOF
201
+ function_call: {"name":"bash","arguments":{"cmd":"git show HEAD:lib/scout/llm/chat/provenance.rb | wc -l; echo ---; ls ~/.rvm/gems/ruby-3.3.1/gems/ | grep -i scout"},"id":"call_-7100561051824943161"}
202
+
203
+ function_call_output: {"name":"bash","content":"{\"stdout\":\"1852\\n---\\nscout-ai-1.1.6\\nscout-ai-1.1.8\\nscout-ai-1.2.2\\nscout-ai-1.2.5\\nscout-ai-1.2.6\\nscout-ai-2.0.0\\nscout-ai-2.1.0\\nscout-camp-0.2.0\\nscout-camp-0.2.1\\nscout-essentials-1.8.5\\nscout-essentials-1.8.6\\nscout-essentials-1.8.7\\nscout-essentials-1.8.8\\nscout-essentials-1.9.0\\nscout-essentials-1.9.1\\nscout-essentials-1.9.3\\nscout-gear-10.11.10\\nscout-gear-10.11.6\\nscout-gear-10.12.1\\nscout-gear-10.12.2\\nscout-gear-10.13.1\\nscout-rig-0.2.0\\nscout-rig-0.2.6\\nscout-web-0.1.0\\n\",\"stderr\":\"\",\"exit_status\":0}","id":"call_-7100561051824943161","start_timestamp":"2026-11-06T22:12:29.479Z","timestamp":"2026-11-06T22:12:29.657Z"}
204
+
205
+ EOF
206
+
207
+ chat = Chat.parse text
208
+ iii chat
209
+
210
+ end
181
211
  end
@@ -0,0 +1,411 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require File.expand_path(__FILE__).sub(%r(.*/test/), '').sub(/test_(.*)\.rb/,'\1')
3
+ require 'scout/llm/backends/anthropic'
4
+
5
+ class TestPrompt < Test::Unit::TestCase
6
+
7
+ def setup
8
+ super
9
+ Chat::REGISTERED_STRATEGIES.delete('probe_strategy')
10
+ end
11
+
12
+ def teardown
13
+ Chat::REGISTERED_STRATEGIES.delete('probe_strategy')
14
+ super
15
+ end
16
+
17
+ def base_messages
18
+ Chat.setup([{ role: 'user', content: 'question' }])
19
+ end
20
+
21
+ def inbox_for(save_file, names_and_contents = {})
22
+ inbox_dir = Chat.inbox_dir(save_file)
23
+ FileUtils.mkdir_p inbox_dir
24
+ names_and_contents.each do |name, content|
25
+ Open.write(File.join(inbox_dir, name), content)
26
+ end
27
+ inbox_dir
28
+ end
29
+
30
+ def removed_for(save_file)
31
+ Chat.inbox_removed_dir(save_file)
32
+ end
33
+
34
+ def user_tail(messages, count = nil)
35
+ injected = messages.select{|m| m[:role].to_s == 'user' }
36
+ injected = injected.last(count) if count
37
+ injected.collect{|m| m[:content] }
38
+ end
39
+
40
+ def test_inbox_no_save_file_is_noop_and_creates_nothing
41
+ messages = base_messages
42
+ result = Chat.prepare_prompt(messages, ['inbox'])
43
+
44
+ assert_equal 1, result.length
45
+ assert !Open.exist?(File.join(Dir.pwd, 'inbox'))
46
+
47
+ TmpFile.with_file do |save_file|
48
+ result = Chat.prepare_prompt(messages, ['inbox'], save_file: nil)
49
+ assert_equal 1, result.length
50
+ # Nothing is created anywhere: not the inbox, not the removed dir.
51
+ assert !Open.exist?(Chat.inbox_dir(save_file))
52
+ assert !Open.exist?(Chat.inbox_removed_dir(save_file))
53
+ end
54
+ end
55
+
56
+ def test_inbox_missing_inbox_dir_is_noop_and_creates_nothing
57
+ TmpFile.with_file do |save_file|
58
+ # The save_file directory exists but holds no inbox sibling; the old
59
+ # `<save_file>.files/` tree is gone under the sibling layout (a missing
60
+ # files dir is subsumed by the missing inbox dir).
61
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
62
+
63
+ assert_equal 1, result.length
64
+ # The read path never creates the inbox, nor the removed dir
65
+ assert !Open.exist?(Chat.inbox_dir(save_file))
66
+ assert !Open.exist?(Chat.inbox_removed_dir(save_file))
67
+ end
68
+ end
69
+
70
+ def test_inbox_dir_derivation_strips_only_the_last_extension
71
+ # Pure path rule: canonical, multi-dot and extension-less basenames.
72
+ assert_equal '/a/x.files/agent.inbox', Chat.inbox_dir('/a/x.files/agent.chat')
73
+ assert_equal '/a/x.files/agent.inbox_removed', Chat.inbox_removed_dir('/a/x.files/agent.chat')
74
+ assert_equal '/a/a.b.inbox', Chat.inbox_dir('/a/a.b.chat')
75
+ assert_equal '/a/a.b.inbox_removed', Chat.inbox_removed_dir('/a/a.b.chat')
76
+ assert_equal '/a/agent.inbox', Chat.inbox_dir('/a/agent')
77
+ assert_equal '/a/agent.inbox_removed', Chat.inbox_removed_dir('/a/agent')
78
+ end
79
+
80
+ def test_jobs_file_derivation_strips_only_a_trailing_chat
81
+ # Pure path rule, mirroring the inbox derivations above: the save_file
82
+ # basename loses its LAST extension and gains '.jobs', all inside the
83
+ # save_file's own directory.
84
+ assert_equal '/a/agent.jobs', Chat.jobs_file('/a/agent.chat')
85
+
86
+ # Pathological case: a '.chat' inside an ANCESTOR component must never be
87
+ # touched (the historical unanchored sub(/\.chat/) rewrote
88
+ # '<job>.chat.files/agent.chat' into '<job>.files/agent.chat.jobs').
89
+ assert_equal '/a/cli.chat.files/agent.jobs',
90
+ Chat.jobs_file('/a/cli.chat.files/agent.chat')
91
+
92
+ # Multi-dot basenames strip only the last extension; extension-less
93
+ # basenames keep the whole name (same convention as inbox_stem).
94
+ assert_equal '/a/a.b.jobs', Chat.jobs_file('/a/a.b.chat')
95
+ assert_equal '/a/agent.jobs', Chat.jobs_file('/a/agent')
96
+ end
97
+
98
+ def test_inbox_non_canonical_save_file_uses_the_sibling_rule
99
+ TmpFile.with_file(extension: 'chat') do |save_file|
100
+ assert_equal '.chat', File.extname(save_file)
101
+ assert save_file.end_with?('.chat')
102
+
103
+ inbox_dir = inbox_for(save_file, 'a_msg.md' => 'canonical message')
104
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
105
+ assert_equal ['canonical message'], user_tail(result, 1)
106
+ assert_equal ['a_msg.md'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
107
+ end
108
+
109
+ # Multi-dot save_file: only the LAST extension is stripped
110
+ TmpFile.with_file(extension: 'b') do |save_file|
111
+ inbox_dir = inbox_for(save_file, 'a_msg.md' => 'multi-dot message')
112
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
113
+ assert_equal ['multi-dot message'], user_tail(result, 1)
114
+ assert Open.directory?(Chat.inbox_dir(save_file))
115
+ assert_equal save_file.sub(/\.b\z/, ''), Chat.inbox_dir(save_file).sub(/\.inbox\z/, '')
116
+ end
117
+
118
+ # Extension-less save_file: whole basename is the stem
119
+ TmpFile.with_file(extension: 'plain') do |save_file|
120
+ assert save_file.end_with?('.plain')
121
+ stem = save_file.sub(/\.plain\z/, '')
122
+
123
+ inbox_dir = inbox_for(stem, 'a_msg.md' => 'extension-less message')
124
+ assert_equal "#{stem}.inbox", inbox_dir
125
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: stem)
126
+ assert_equal ['extension-less message'], user_tail(result, 1)
127
+ assert_equal ['a_msg.md'], Dir.glob(File.join("#{stem}.inbox_removed", '*')).collect{|f| File.basename(f) }
128
+ end
129
+ end
130
+
131
+ def test_inbox_appends_in_sorted_order_moves_files_and_preserves_mtime
132
+ TmpFile.with_file do |save_file|
133
+ mtimes = {}
134
+ inbox_dir = inbox_for(save_file,
135
+ 'b_second.md' => 'second notice',
136
+ 'a_first.md' => 'first notice',
137
+ '10_numeric.md' => 'numeric ten',
138
+ 'c_third.md' => 'third notice',
139
+ '2_numeric.md' => 'numeric two')
140
+ Dir.glob(File.join(inbox_dir, '*')).sort.each do |file|
141
+ mtimes[file] = Time.now - 100_000 - rand(1000)
142
+ File.utime(mtimes[file], mtimes[file], file)
143
+ end
144
+ expected_order = Dir.glob(File.join(inbox_dir, '*')).collect{|f| File.basename(f) }.sort
145
+ expected_contents = expected_order.collect{|n| Open.read(File.join(inbox_dir, n)) }
146
+
147
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
148
+
149
+ assert_equal 6, result.length
150
+ assert_equal 'question', result.first[:content]
151
+ assert_equal expected_contents, user_tail(result, 5)
152
+
153
+ assert_equal [], Dir.glob(File.join(inbox_dir, '*'))
154
+ removed = removed_for(save_file)
155
+ removed_files = Dir.glob(File.join(removed, '*')).sort
156
+ assert_equal expected_order, removed_files.collect{|f| File.basename(f) }
157
+ removed_files.each do |file|
158
+ assert_equal mtimes.find{|k,_| File.basename(k) == File.basename(file) }.last, File.mtime(file)
159
+ end
160
+ end
161
+ end
162
+
163
+ def test_inbox_skips_directories_and_dotfiles
164
+ TmpFile.with_file do |save_file|
165
+ inbox_dir = inbox_for(save_file, 'a_msg.md' => 'real message')
166
+ FileUtils.mkdir_p File.join(inbox_dir, 'subdir')
167
+ Open.write(File.join(inbox_dir, '.hidden'), 'ignored')
168
+
169
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
170
+
171
+ assert_equal 2, result.length
172
+ assert_equal ['real message'], user_tail(result, 1)
173
+ # Directories and dotfiles are left alone, not moved into the removed dir
174
+ assert Open.directory?(File.join(inbox_dir, 'subdir'))
175
+ assert_equal ['a_msg.md'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
176
+ assert !Open.exist?(File.join(removed_for(save_file), 'subdir'))
177
+ end
178
+ end
179
+
180
+ def test_inbox_is_consume_once
181
+ TmpFile.with_file do |save_file|
182
+ inbox_for(save_file, 'a_msg.md' => 'once')
183
+
184
+ first = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
185
+ assert_equal 2, first.length
186
+
187
+ second = Chat.prepare_prompt(first, ['inbox'], save_file: save_file)
188
+ assert_equal 2, second.length
189
+ assert_equal ['once'], user_tail(second, 1)
190
+ end
191
+ end
192
+
193
+ def test_inbox_collision_in_removed_gets_numeric_suffix
194
+ TmpFile.with_file do |save_file|
195
+ inbox_for(save_file, 'a_msg.md' => 'original delivery')
196
+ removed_dir = removed_for(save_file)
197
+ FileUtils.mkdir_p removed_dir
198
+ Open.write(File.join(removed_dir, 'a_msg.md'), 'previous delivery')
199
+ previous_mtime = File.mtime(File.join(removed_dir, 'a_msg.md'))
200
+ sleep 0.1
201
+
202
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
203
+
204
+ assert_equal 2, result.length
205
+ assert_equal ['original delivery'], user_tail(result, 1)
206
+
207
+ files = Dir.glob(File.join(removed_dir, '*')).sort
208
+ assert_equal ['a_msg.1.md', 'a_msg.md'], files.collect{|f| File.basename(f) }
209
+ assert_equal 'previous delivery', Open.read(File.join(removed_dir, 'a_msg.md'))
210
+ assert_equal 'original delivery', Open.read(File.join(removed_dir, 'a_msg.1.md'))
211
+ assert_equal previous_mtime, File.mtime(File.join(removed_dir, 'a_msg.md'))
212
+ end
213
+ end
214
+
215
+ def test_inbox_unreadable_file_is_skipped_without_raising
216
+ return if Process.uid.zero? # chmod 000 does not stop root
217
+
218
+ TmpFile.with_file do |save_file|
219
+ inbox_dir = inbox_for(save_file,
220
+ 'a_msg.md' => 'delivered',
221
+ 'z_locked.md' => 'locked away')
222
+ FileUtils.chmod(0000, File.join(inbox_dir, 'z_locked.md'))
223
+
224
+ result = nil
225
+ assert_nothing_raised do
226
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
227
+ end
228
+
229
+ assert_equal ['delivered'], user_tail(result, 1)
230
+ # The unreadable file stays in place for a later retry
231
+ locked = File.join(inbox_dir, 'z_locked.md')
232
+ assert Open.exist?(locked)
233
+ # Only the readable file was delivered; the locked one was not moved
234
+ assert_equal ['a_msg.md'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
235
+ end
236
+ end
237
+
238
+ def test_inbox_returns_chat_annotated_array
239
+ TmpFile.with_file do |save_file|
240
+ inbox_for(save_file, 'a_msg.md' => 'message')
241
+
242
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
243
+
244
+ assert Chat === result
245
+ assert Chat === Chat.prepare_prompt(result, ['inbox'], save_file: save_file)
246
+ end
247
+ end
248
+
249
+ # --- reserved `abort` filename ---
250
+
251
+ def test_inbox_abort_file_aborts_inference_and_is_consumed_once
252
+ TmpFile.with_file do |save_file|
253
+ inbox_for(save_file, 'abort' => '')
254
+
255
+ result = nil
256
+ assert_raise(Aborted) do
257
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
258
+ end
259
+ assert_nil result
260
+
261
+ # consumed exactly once: moved out of the inbox, into the removed dir
262
+ assert_equal [], Dir.glob(File.join(Chat.inbox_dir(save_file), '*'))
263
+ assert_equal ['abort'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
264
+
265
+ # a second run picks up nothing new and does not raise again
266
+ second = nil
267
+ assert_nothing_raised do
268
+ second = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
269
+ end
270
+ assert_equal 1, second.length
271
+ assert_equal ['abort'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
272
+ end
273
+ end
274
+
275
+ def test_inbox_abort_file_content_is_the_reason_and_never_reaches_the_prompt
276
+ TmpFile.with_file do |save_file|
277
+ inbox_for(save_file, 'abort' => 'user asked to stop because of budget')
278
+
279
+ error = assert_raise(Aborted) do
280
+ Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
281
+ end
282
+ # the content becomes the abort reason...
283
+ assert_include error.message, 'user asked to stop because of budget'
284
+ # ...and is NOT delivered as a user message: nothing was appended
285
+ assert_equal ['abort'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
286
+
287
+ # the reserved name is exact: any other name aborts nothing
288
+ inbox_for(save_file, 'abort.txt' => 'not reserved')
289
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
290
+ assert_equal ['not reserved'], user_tail(result, 1)
291
+ end
292
+ end
293
+
294
+ def test_inbox_files_before_abort_are_delivered_and_consumed
295
+ TmpFile.with_file do |save_file|
296
+ # 'a_first' sorts before 'abort', 'z_after' sorts after it
297
+ inbox_for(save_file,
298
+ 'z_after.md' => 'should wait for the next run',
299
+ 'a_first.md' => 'delivered before the abort',
300
+ 'abort' => 'stop now')
301
+
302
+ # The raise happens while building the prompt, so the delivered
303
+ # messages are lost to the caller -- that is the designed semantics:
304
+ # files before the abort are consumed (moved) and never re-delivered.
305
+ assert_raise(Aborted) do
306
+ Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
307
+ end
308
+
309
+ assert_equal ['z_after.md'], Dir.glob(File.join(Chat.inbox_dir(save_file), '*')).collect{|f| File.basename(f) }
310
+ assert_equal ['a_first.md', 'abort'], Dir.glob(File.join(removed_for(save_file), '*')).sort.collect{|f| File.basename(f) }
311
+
312
+ # the next run delivers what was left behind, and does not raise
313
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
314
+ assert_equal ['should wait for the next run'], user_tail(result, 1)
315
+ assert_equal ['a_first.md', 'abort', 'z_after.md'], Dir.glob(File.join(removed_for(save_file), '*')).sort.collect{|f| File.basename(f) }
316
+ end
317
+ end
318
+
319
+ def test_inbox_empty_abort_still_aborts_with_default_reason
320
+ TmpFile.with_file do |save_file|
321
+ inbox_for(save_file, 'abort' => " \n")
322
+
323
+ error = assert_raise(Aborted) do
324
+ Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
325
+ end
326
+ # blank content falls back to a path-qualified default reason
327
+ assert_include error.message, 'Inbox abort'
328
+ assert !error.message.include?('whitespace only reason')
329
+ assert_equal ['abort'], Dir.glob(File.join(removed_for(save_file), '*')).collect{|f| File.basename(f) }
330
+ end
331
+ end
332
+
333
+ def test_inbox_non_abort_files_behave_as_before_around_an_abort
334
+ TmpFile.with_file do |save_file|
335
+ # Regression shape of the whole previous behaviour, with no abort
336
+ # file present: sorting, mtime preservation and consume-once are
337
+ # untouched by the reserved-name handling.
338
+ inbox_dir = inbox_for(save_file,
339
+ 'z_third.md' => 'a file whose name merely starts with abort',
340
+ 'b_second.md' => 'second',
341
+ 'a_first.md' => 'first')
342
+
343
+ result = Chat.prepare_prompt(base_messages, ['inbox'], save_file: save_file)
344
+ # sorted pickup order is preserved, and no prefix of 'abort' is treated
345
+ # as the reserved filename
346
+ assert_equal ['first', 'second', 'a file whose name merely starts with abort'], user_tail(result, 3)
347
+ assert_equal [], Dir.glob(File.join(inbox_dir, '*'))
348
+ end
349
+ end
350
+
351
+ def test_inbox_composes_with_shorten_tools_epoch_increment_in_both_orders
352
+ messages = Chat.setup([
353
+ { role: 'user', content: 'long ' * 2000 },
354
+ { role: 'assistant', content: 'answer' },
355
+ { role: 'user', content: 'question' },
356
+ ])
357
+
358
+ TmpFile.with_file do |save_file|
359
+ inbox_for(save_file, 'a_msg.md' => 'injected notice')
360
+
361
+ inbox_first = Chat.prepare_prompt(messages, ['inbox', 'shorten_tools_epoch_increment'], save_file: save_file)
362
+ assert Chat === inbox_first
363
+ assert_equal 'injected notice', inbox_first.last[:content]
364
+ assert_equal 'user', inbox_first.last[:role].to_s
365
+
366
+ inbox_for(save_file, 'b_msg.md' => 'second notice')
367
+ inbox_last = Chat.prepare_prompt(messages, ['shorten_tools_epoch_increment', 'inbox'], save_file: save_file)
368
+ assert Chat === inbox_last
369
+ assert_equal 'second notice', inbox_last.last[:content]
370
+ end
371
+ end
372
+
373
+ def test_prepare_prompt_forwards_save_file_only_to_strategies_that_accept_it
374
+ # A single-argument strategy must not see (nor choke on) the new keyword.
375
+ assert_nothing_raised do
376
+ Chat.prepare_prompt(base_messages, ['shorten_tools'], save_file: 'unused.chat')
377
+ end
378
+ end
379
+
380
+ # The registered-proc form still receives only the prompt (documented
381
+ # behavior), and does not break when save_file is forwarded.
382
+ def test_prepare_prompt_registered_proc_ignores_save_file
383
+ Chat::REGISTERED_STRATEGIES['probe_strategy'] = ->(messages) { messages }
384
+ TmpFile.with_file do |save_file|
385
+ assert_nothing_raised do
386
+ result = Chat.prepare_prompt(base_messages, ['probe_strategy'], save_file: save_file)
387
+ assert_equal 1, result.length
388
+ end
389
+ end
390
+ end
391
+
392
+ # prompt_strategies is extracted (deleted) from options inside
393
+ # Backend#ask; chain_tools must re-thread it like save_file so a
394
+ # user-specified list applies to tool-call re-entry rounds too, instead of
395
+ # degrading to DEFAULT_CONTEXT_STRATEGY on round 2+.
396
+ def test_prompt_strategies_survive_tool_call_re_entry
397
+ probe_calls = []
398
+ Chat::REGISTERED_STRATEGIES['probe_strategy'] = ->(messages) { probe_calls << messages.dup; messages }
399
+
400
+ client = TestClients.anthropic_client('backends/anthropic_tool_use', 'backends/anthropic')
401
+ assert_equal 2, client.responses.length
402
+
403
+ LLM::Anthropic.ask 'user: call the tool', tools: [{ type: 'custom', name: 'get_current_temperature', description: 'w', input_schema: { type: 'object' } }],
404
+ client: client, persist: false, prompt_strategies: 'probe_strategy' do |name, arguments|
405
+ 'It is raining.'
406
+ end
407
+
408
+ assert_equal 2, client.calls.length, 'expected a tool-call round plus a final answer round'
409
+ assert_equal 2, probe_calls.length, 'user prompt_strategies must apply on every round, re-entry included'
410
+ end
411
+ end