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
@@ -78,7 +78,7 @@ require_relative 'agent_meta_fixtures'
78
78
  #
79
79
  # direct delegated chat <chat>.files/agent.chat (or <name>.chat)
80
80
  # society chats <chat>.files/agent.society/<Agent>/<conv>/agent.chat
81
- # legacy (read-only) <chat>.files/log/... and <chat>.files/log/society/...
81
+ # The legacy `.files/log/**` tree is no longer read.
82
82
  #
83
83
  # These tests only exercise the traversal/read side; writing the new layout is
84
84
  # covered by test/scout/llm/agent/test_save.rb.
@@ -124,18 +124,17 @@ class TestNewLayoutProvenance < Test::Unit::TestCase
124
124
  end
125
125
  end
126
126
 
127
- # (b) new-layout root copy not traversed; legacy root copy still excluded
127
+ # (b) canonical root copy not traversed; legacy .files/log tree invisible
128
128
  def test_new_layout_root_copy_is_not_traversed
129
129
  TmpFile.with_dir do |dir|
130
130
  chat = write_chat(dir, 'saved.chat', ROOT_CHAT)
131
131
 
132
132
  files = File.expand_path(chat + '.files')
133
133
  FileUtils.mkdir_p(File.join(files, 'agent.society', 'Direct', 'harness_test'))
134
+ # Leftover legacy tree: full root copy plus an old society projection.
134
135
  FileUtils.mkdir_p(File.join(files, 'log', 'society', 'Worker', 'default'))
135
- # Root copies in BOTH layouts: full copy of the root conversation.
136
136
  File.write(File.join(files, 'agent.chat'), ROOT_CHAT)
137
137
  File.write(File.join(files, 'log', 'agent.chat'), ROOT_CHAT)
138
- # Independent society conversations in both layouts.
139
138
  File.write(File.join(files, 'agent.society', 'Direct', 'harness_test', 'agent.chat'),
140
139
  "user: work\nmeta: pt=10 ct=5 tt=15 inference_id=w1\nassistant: ok\n")
141
140
  File.write(File.join(files, 'log', 'society', 'Worker', 'default', 'agent.chat'),
@@ -144,22 +143,72 @@ class TestNewLayoutProvenance < Test::Unit::TestCase
144
143
  paths = visits_paths(chat)
145
144
 
146
145
  new_root_copy = File.join(files, 'agent.chat')
147
- legacy_root_copy = File.join(files, 'log', 'agent.chat')
148
146
  assert_not_include paths, new_root_copy,
149
- 'new-layout root copy must not become its own node'
150
- assert_not_include paths, legacy_root_copy,
151
- 'legacy root copy must stay excluded'
147
+ 'canonical root copy must not become its own node'
152
148
  assert_include paths, File.join(files, 'agent.society', 'Direct', 'harness_test', 'agent.chat')
153
- assert_include paths, File.join(files, 'log', 'society', 'Worker', 'default', 'agent.chat')
149
+
150
+ # The legacy tree is no longer read at all: neither its root copy nor
151
+ # its society projections appear anywhere in the traversal.
152
+ assert_empty paths.select { |path| path.include?(File.join(files, 'log')) },
153
+ 'legacy .files/log tree must be invisible to traversal'
154
154
 
155
155
  edges = Chat.provenance_edges(chat).collect { |e| [e[:from].to_s, e[:to].to_s, e[:relation]] }
156
156
  assert_empty edges.select { |_from, to, _r| to == new_root_copy }
157
- assert_empty edges.select { |_from, to, _r| to == legacy_root_copy }
158
157
  end
159
158
  end
160
159
 
161
- # (c) legacy log/ sidecar still traversed (compat) and (d) no duplicate
162
- # visits when both layouts coexist.
160
+ # (b2) the Lean regression: the top-level file is the agent TRANSCRIPT
161
+ # (differs from the root conversation) and carries the meta job=
162
+ # references of delegated work. A chat-root traversal must include
163
+ # the transcript itself and follow its reference into the job tree.
164
+ def test_new_layout_top_level_transcript_is_traversed_from_chat_root
165
+ TmpFile.with_dir do |dir|
166
+ job = make_job(dir, 'Planned/work/Default_1',
167
+ logs: {'agent.chat' =>
168
+ "user: work\nmeta: pt=10 ct=5 tt=15 inference_id=w1\nassistant: ok\n"})
169
+ chat = write_chat(dir, 'session.chat',
170
+ "user: hi\nmeta: pt=2 ct=1 tt=3 inference_id=s0\nassistant: done\n")
171
+
172
+ files = File.expand_path(chat + '.files')
173
+ FileUtils.mkdir_p(files)
174
+ # Transcript: same session but not a copy -- the agent conversation
175
+ # carries the delegated job reference the root file does not.
176
+ File.write(File.join(files, 'agent.chat'),
177
+ "system: you are the harness\n" \
178
+ "user: hi\nmeta: pt=2 ct=1 tt=3 inference_id=s0\nassistant: done\n" \
179
+ "user: go\nmeta: job=#{job}\nassistant: done\n")
180
+
181
+ visits = Chat.traverse_provenance(chat).to_a
182
+ paths = visits.collect { |_k, object, _pk, _p, _r, _f| object.to_s }
183
+ job_paths = visits.select { |kind, *_| kind == :job }
184
+ .collect { |_k, object, *_| object.path.to_s }
185
+
186
+ transcript = File.join(files, 'agent.chat')
187
+ assert_include paths, transcript,
188
+ 'the top-level agent transcript must be traversed from the chat root'
189
+ assert_include job_paths, job,
190
+ 'the job referenced by the transcript must be reached'
191
+ assert_include paths, File.join(job + '.files', 'agent.chat'),
192
+ 'the job log chat must be reached through the transcript ref'
193
+
194
+ edges = Chat.provenance_edges(chat)
195
+ .collect { |e| [e[:from].to_s,
196
+ e[:to].respond_to?(:path) ? e[:to].path.to_s : e[:to].to_s,
197
+ e[:relation]] }
198
+ assert_include edges, [File.expand_path(chat), transcript, :log],
199
+ 'the transcript must be linked to its chat root with a :log edge'
200
+ assert_include edges, [transcript, job, :job],
201
+ 'the transcript job reference must produce a chat->job edge'
202
+
203
+ # The transcript meta (s0 duplicated, plus w1 through the job log) still
204
+ # aggregates without double counting: identity grouping keeps s0 once.
205
+ assert_equal({pt: 12, ct: 6, tt: 18, cct: 0, cwt: 0, rt: 0},
206
+ Chat.provenance_token_totals(chat))
207
+ end
208
+ end
209
+
210
+ # (c) a leftover legacy tree contributes nothing: no duplicate visits and
211
+ # no tokens from the legacy files.
163
212
  def test_both_layouts_coexist_without_duplicates
164
213
  TmpFile.with_dir do |dir|
165
214
  chat = write_chat(dir, 'saved.chat', ROOT_CHAT)
@@ -175,17 +224,13 @@ class TestNewLayoutProvenance < Test::Unit::TestCase
175
224
 
176
225
  visits = Chat.traverse_provenance(chat).to_a
177
226
  paths = visits.collect { |_k, object, _pk, _p, _r, _f| object.to_s }
178
- assert_equal paths.uniq.length, paths.length, 'duplicate visits when layouts coexist'
227
+ assert_equal paths.uniq.length, paths.length, 'duplicate visits'
179
228
 
180
- # Legacy relation is unchanged.
181
- legacy = visits.find do |_kind, object, _pk, _parent, relation, _first|
182
- relation == :log && object.to_s.end_with?('log/society/Worker/default/agent.chat')
183
- end
184
- assert legacy, 'legacy log/ sidecar must keep being traversed'
185
- assert_equal :chat, legacy[0]
186
-
187
- # Tokens are counted exactly once.
188
- assert_equal({pt: 32, ct: 16, tt: 48, cct: 0, cwt: 0, rt: 0},
229
+ # Only the canonical society chat was traversed; the legacy file was
230
+ # never swept and contributes no tokens.
231
+ assert_include paths, File.join(files, 'agent.society', 'Direct', 'harness_test', 'agent.chat')
232
+ assert_not_include paths, File.join(files, 'log', 'society', 'Worker', 'default', 'agent.chat')
233
+ assert_equal({pt: 12, ct: 6, tt: 18, cct: 0, cwt: 0, rt: 0},
189
234
  Chat.provenance_token_totals(chat))
190
235
  end
191
236
  end
@@ -0,0 +1,479 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/llm/chat'
3
+ require_relative 'agent_meta_fixtures'
4
+
5
+ # Focused tests for the compact path-based provenance index
6
+ # (`Chat.provenance_graph`): one node per call, lightweight data only,
7
+ # zero-based persisted message indexes, live paths listed but never
8
+ # traversed. Recursive behavior stays in `provenance_closure` and is
9
+ # covered by the other provenance suites.
10
+ class TestChatProvenanceIndex < Test::Unit::TestCase
11
+ include AgentMetaFixtures
12
+
13
+ def graph(root, **options)
14
+ Chat.provenance_graph(root, **options)
15
+ end
16
+
17
+ ## (1) planned invocation triplets: job anchored at the META index
18
+ def test_triplet_chats_are_anchored_at_the_meta_message_index
19
+ TmpFile.with_dir do |dir|
20
+ job = make_job(dir, 'W/ask/Default_w')
21
+ chat = write_chat(dir, 'root.chat', [
22
+ 'user: first', "meta: job=#{job}", 'assistant: a1',
23
+ 'user: second', "meta: job=#{job}", 'assistant: a2'
24
+ ] * "\n" + "\n")
25
+
26
+ index = graph(chat)
27
+ assert_equal :chat, index[:type]
28
+ # Zero-based array indexes of the two meta messages.
29
+ assert_equal({1 => job, 4 => job}, index[:chats])
30
+ end
31
+ end
32
+
33
+ ## (2) standalone meta reference is enough
34
+ def test_standalone_meta_job_reference
35
+ TmpFile.with_dir do |dir|
36
+ job = make_job(dir, 'W/ask/Default_w')
37
+ chat = write_chat(dir, 'root.chat',
38
+ "user: run\nmeta: job=#{job}\nassistant: done\n")
39
+
40
+ assert_equal({1 => job}, graph(chat)[:chats])
41
+ end
42
+ end
43
+
44
+ ## (3) receipt reference anchored at the OUTER output message index
45
+ def test_receipt_reference_is_anchored_at_outer_output_index
46
+ TmpFile.with_dir do |dir|
47
+ job = make_job(dir, 'W/ask/Default_w')
48
+ text = receipt_chat_text({'c1' => [meta_receipt("job=#{job}")]})
49
+ # indexes: 0 user, 1 function_call, 2 function_call_output, 3 assistant
50
+ chat = write_chat(dir, 'root.chat', text)
51
+
52
+ assert_equal({2 => job}, graph(chat)[:chats])
53
+ end
54
+ end
55
+
56
+ ## (4) both reference forms in one transcript, same map
57
+ def test_both_reference_forms_merge_into_one_map
58
+ TmpFile.with_dir do |dir|
59
+ a = make_job(dir, 'A/load/Default_a')
60
+ b = make_job(dir, 'B/load/Default_b')
61
+ text = receipt_chat_text({'c1' => [meta_receipt("job=#{a}")]},
62
+ extra: ["meta: job=#{b}"])
63
+ chat = write_chat(dir, 'root.chat', text)
64
+
65
+ assert_equal({2 => a, 3 => b}, graph(chat)[:chats])
66
+ end
67
+ end
68
+
69
+ ## (5) multiple references at one index are preserved, not overwritten
70
+ def test_multiple_references_at_one_index_become_an_array
71
+ TmpFile.with_dir do |dir|
72
+ worker = make_job(dir, 'W/ask/Default_w')
73
+ critic = make_job(dir, 'C/ask/Default_c')
74
+ # One receipt envelope listing two job references: both anchor at the
75
+ # same outer output index.
76
+ text = receipt_chat_text(
77
+ {'c1' => [meta_receipt("job=#{worker}"), meta_receipt("job=#{critic}")]})
78
+ chat = write_chat(dir, 'root.chat', text)
79
+
80
+ assert_equal({2 => [worker, critic]}, graph(chat)[:chats])
81
+ end
82
+ end
83
+
84
+ ## (6) ask call: index of the call message and Agent#conversation value
85
+ def test_ask_call_records_delegation_at_call_index
86
+ TmpFile.with_dir do |dir|
87
+ chat = write_chat(dir, 'root.chat',
88
+ "user: go\n" \
89
+ 'function_call: {"name":"ask","arguments":{"agent":"Workspace","conversation":"plan"},"id":"c1"}' + "\n" \
90
+ 'function_call_output: {"name":"ask","content":"ok","id":"c1"}' + "\n" \
91
+ "assistant: done\n")
92
+
93
+ index = graph(chat)
94
+ assert_equal({1 => 'Workspace#plan'}, index[:delegation])
95
+ # An ask with no persisted job= metadata never enters chats.
96
+ assert_empty index[:chats]
97
+ end
98
+ end
99
+
100
+ ## (6b) missing conversation argument defaults to 'default'
101
+ def test_ask_without_conversation_defaults
102
+ TmpFile.with_dir do |dir|
103
+ chat = write_chat(dir, 'root.chat',
104
+ "user: go\n" \
105
+ 'function_call: {"name":"ask","arguments":{"agent":"Analyst"},"id":"c1"}' + "\n" \
106
+ 'function_call_output: {"name":"ask","content":"ok","id":"c1"}' + "\n")
107
+
108
+ assert_equal({1 => 'Analyst#default'}, graph(chat)[:delegation])
109
+ end
110
+ end
111
+
112
+ ## (7) a plain reply creates no provenance branch
113
+ def test_plain_response_has_no_provenance
114
+ TmpFile.with_dir do |dir|
115
+ chat = write_chat(dir, 'root.chat',
116
+ "user: explain the code\nassistant: plain ScoutCoder reply\n")
117
+ index = graph(chat)
118
+
119
+ assert_empty index[:chats]
120
+ assert_empty index[:delegation]
121
+ assert_empty index[:dependencies]
122
+ assert_empty index[:live]
123
+ assert_empty index[:warnings]
124
+ assert_equal :chat, index[:type]
125
+ end
126
+ end
127
+
128
+ ## (8) job root: references inside the OUTPUT chat
129
+ def test_job_root_reads_references_from_its_output_chat
130
+ TmpFile.with_dir do |dir|
131
+ nested = make_job(dir, 'N/load/Default_n')
132
+ output = "user: run\nmeta: job=#{nested}\nassistant: done\n"
133
+ job = make_job(dir, 'W/ask/Default_w', result: output,
134
+ info: {type: 'chat', status: 'done'})
135
+
136
+ assert_equal({1 => nested}, graph(job, root_type: :job)[:chats])
137
+ end
138
+ end
139
+
140
+ ## (9) the job's INPUT chat is never treated as generated output
141
+ def test_job_root_ignores_its_input_chat
142
+ TmpFile.with_dir do |dir|
143
+ input_only = make_job(dir, 'Other/ask/Default_o')
144
+ # The CALLER's chat references input_only; it is the job's input
145
+ # context, persisted next to it, not the job's output.
146
+ write_chat(dir, 'caller.chat',
147
+ "user: call\nmeta: job=#{input_only}\nassistant: done\n")
148
+ job = make_job(dir, 'W/ask/Default_w', result: 'plain text result',
149
+ info: {type: 'tsv', status: 'done'})
150
+
151
+ index = graph(job, root_type: :job)
152
+ assert_empty index[:chats]
153
+ assert_equal :job, index[:type]
154
+ end
155
+ end
156
+
157
+ ## (10) non-chat job: dependencies and live files, no chats
158
+ def test_non_chat_job_has_dependencies_but_no_chats
159
+ TmpFile.with_dir do |dir|
160
+ dep = make_job(dir, 'D/load/Default_d')
161
+ job = make_job(dir, 'W/load/Default_w', dependencies: [dep])
162
+
163
+ index = graph(job, root_type: :job)
164
+ assert_equal :job, index[:type]
165
+ assert_empty index[:chats]
166
+ assert_empty index[:delegation]
167
+ end
168
+ end
169
+
170
+ ## (11) dependency order preserved; only direct dependencies
171
+ def test_dependencies_are_direct_and_ordered
172
+ TmpFile.with_dir do |dir|
173
+ first = make_job(dir, 'P/one/Default_1')
174
+ second = make_job(dir, 'P/two/Default_2', dependencies: [first])
175
+ third = make_job(dir, 'P/three/Default_3')
176
+ job = make_job(dir, 'W/ask/Default_w',
177
+ dependencies: [third, second, first],
178
+ result: "user: hi\nassistant: done\n",
179
+ info: {type: 'chat'})
180
+
181
+ assert_equal [third, second, first], graph(job, root_type: :job)[:dependencies]
182
+ end
183
+ end
184
+
185
+ ## (12) a byte-identical top-level sidecar is still listed in live
186
+ def test_byte_identical_sidecar_is_still_live
187
+ TmpFile.with_dir do |dir|
188
+ text = "user: hi\nassistant: done\n"
189
+ chat = write_chat(dir, 'root.chat', text)
190
+ FileUtils.mkdir_p(chat + '.files')
191
+ File.write(File.join(chat + '.files', 'agent.chat'), text)
192
+
193
+ live = graph(chat)[:live].collect { |path| File.basename(path) }
194
+ assert_equal ['agent.chat'], live
195
+ end
196
+ end
197
+
198
+ ## (13) society agent.chat is live, but not authoritative provenance
199
+ def test_society_chat_is_live_but_not_provenance
200
+ TmpFile.with_dir do |dir|
201
+ chat = write_chat(dir, 'root.chat',
202
+ "user: hi\nassistant: done\n")
203
+ society = File.join(chat + '.files', 'agent.society', 'ScoutCoder', 'default')
204
+ FileUtils.mkdir_p(society)
205
+ # Society chat carries its OWN job reference: still not traversed.
206
+ File.write(File.join(society, 'agent.chat'),
207
+ "user: work\nmeta: job=Society/Only/Default_x\nassistant: ok\n")
208
+
209
+ index = graph(chat)
210
+ assert_equal [File.join(society, 'agent.chat')], index[:live]
211
+ assert_empty index[:chats]
212
+ assert_empty index[:delegation]
213
+ end
214
+ end
215
+
216
+ ## (14) missing sidecar and society directories give empty live
217
+ def test_missing_files_dirs_give_empty_live
218
+ TmpFile.with_dir do |dir|
219
+ chat = write_chat(dir, 'root.chat', "user: hi\nassistant: done\n")
220
+ assert_empty graph(chat)[:live]
221
+
222
+ job = make_job(dir, 'W/load/Default_w')
223
+ assert_empty graph(job, root_type: :job)[:live]
224
+ end
225
+ end
226
+
227
+ ## (15) lightweight data only: no Chat/Step instances, no message bodies
228
+ def test_return_value_contains_no_objects_or_message_bodies
229
+ TmpFile.with_dir do |dir|
230
+ dep = make_job(dir, 'D/load/Default_d')
231
+ output = "user: run\nmeta: job=#{dep}\nassistant: the actual reply text\n"
232
+ job = make_job(dir, 'W/ask/Default_w', result: output,
233
+ dependencies: [dep], info: {type: 'chat'})
234
+ society = File.join(job + '.files', 'agent.society', 'Critic', 'default')
235
+ FileUtils.mkdir_p(society)
236
+ File.write(File.join(society, 'agent.chat'),
237
+ "user: c\nassistant: the actual reply text\n")
238
+
239
+ index = graph(job, root_type: :job)
240
+ collect = lambda do |value, &block|
241
+ case value
242
+ when Hash then value.each_value { |v| collect.call(v, &block) }
243
+ when Array then value.each { |v| collect.call(v, &block) }
244
+ else block.call(value)
245
+ end
246
+ end
247
+ leaves = []
248
+ collect.call(index) { |leaf| leaves << leaf }
249
+ assert(leaves.all? { |leaf| [String, Symbol, Integer, Float, TrueClass, FalseClass, NilClass].any? { |k| k === leaf } },
250
+ "non-scalar leaves: #{leaves.reject { |l| [String, Symbol, Integer, Float, TrueClass, FalseClass, NilClass].any? { |k| k === l } }.inspect}")
251
+ assert_not_include leaves, 'the actual reply text'
252
+ end
253
+ end
254
+
255
+ ## Invariants V1/V2/V5/V7 restated as behavior
256
+ def test_live_paths_never_reenter_the_index
257
+ TmpFile.with_dir do |dir|
258
+ chat = write_chat(dir, 'root.chat', "user: hi\nassistant: done\n")
259
+ society = File.join(chat + '.files', 'agent.society', 'W', 'c')
260
+ FileUtils.mkdir_p(society)
261
+ File.write(File.join(society, 'agent.chat'),
262
+ "user: work\nmeta: job=Live/Ref/Default_l\n" \
263
+ 'function_call: {"name":"ask","arguments":{"agent":"Analyst"},"id":"k1"}' + "\n")
264
+
265
+ index = graph(chat)
266
+ # The references live inside a live sidecar; the index must not have
267
+ # loaded that chat, so neither entry appears.
268
+ assert_empty index[:chats]
269
+ assert_empty index[:delegation]
270
+ end
271
+ end
272
+
273
+ def test_chat_root_has_no_dependencies
274
+ TmpFile.with_dir do |dir|
275
+ chat = write_chat(dir, 'root.chat',
276
+ "user: hi\nmeta: job=X/Y/Default_x\nassistant: done\n")
277
+ assert_empty graph(chat)[:dependencies]
278
+ end
279
+ end
280
+
281
+ ## Malformed receipts follow the on_error policy and warn
282
+ def test_malformed_receipt_warns_and_keeps_other_provenance
283
+ TmpFile.with_dir do |dir|
284
+ good = make_job(dir, 'G/load/Default_g')
285
+ broken = File.join(dir, 'broken.chat')
286
+ # One standalone meta plus one receipt whose entry is not a Hash.
287
+ File.write(broken,
288
+ "user: run\nmeta: job=#{good}\n" \
289
+ 'function_call: {"name":"ask","arguments":{},"id":"c1"}' + "\n" \
290
+ 'function_call_output: {"name":"ask","content":"ok","id":"c1","meta":"not-an-array"}' + "\n" \
291
+ "assistant: done\n")
292
+
293
+ index = graph(broken)
294
+ assert_equal({1 => good}, index[:chats])
295
+ assert_equal 1, index[:warnings].length
296
+ assert_equal :agent_meta, index[:warnings].first[:kind]
297
+ end
298
+ end
299
+
300
+ def test_unresolved_receipt_reference_warns_and_is_not_listed
301
+ TmpFile.with_dir do |dir|
302
+ chat = write_chat(dir, 'root.chat',
303
+ "user: run\n" \
304
+ 'function_call: {"name":"ask","arguments":{},"id":"c1"}' + "\n" \
305
+ 'function_call_output: {"name":"ask","content":"ok","id":"c1","meta":[{"job":"Nope/None/Default_z"}]}' + "\n")
306
+
307
+ index = graph(chat)
308
+ assert_empty index[:chats]
309
+ assert_equal 1, index[:warnings].length
310
+ assert_match(/unresolved_job_reference/, index[:warnings].first[:error].to_s)
311
+ end
312
+ end
313
+
314
+ def test_strict_mode_raises_on_unresolved_receipt
315
+ TmpFile.with_dir do |dir|
316
+ chat = write_chat(dir, 'root.chat',
317
+ "user: run\n" \
318
+ 'function_call: {"name":"ask","arguments":{},"id":"c1"}' + "\n" \
319
+ 'function_call_output: {"name":"ask","content":"ok","id":"c1","meta":[{"job":"Nope/None/Default_z"}]}' + "\n")
320
+
321
+ assert_raise ScoutException do
322
+ graph(chat, on_error: :raise)
323
+ end
324
+ end
325
+ end
326
+
327
+ ## ROUND 4 — resolved, canonical absolute paths everywhere
328
+
329
+ # A SHORT metadata reference resolves to the job's canonical absolute
330
+ # path. The real spelling is RELATIVE-with-.chat (e.g.
331
+ # 'Planned/ask/Default_x.chat'); load_job_reference resolves it by
332
+ # joining the job fallback bases, so the test stubs the bases to a
333
+ # TmpFile base instead of writing into the user's live var/jobs.
334
+ def test_short_reference_resolves_to_canonical_absolute_job_path
335
+ TmpFile.with_dir do |dir|
336
+ base = File.join(dir, 'var', 'jobs')
337
+ worker = File.join(base, 'W', 'ask', 'Default_w.chat')
338
+ Open.mkdir(File.dirname(worker))
339
+ Open.write(worker, "user: run\nassistant: done\n")
340
+ Open.write(worker + '.info', {}.to_json)
341
+
342
+ Chat.singleton_class.send(:alias_method, :job_reference_fallback_bases_original, :job_reference_fallback_bases)
343
+ Chat.singleton_class.send(:define_method, :job_reference_fallback_bases) { [base] }
344
+ short = 'W/ask/Default_w.chat'
345
+ chat = write_chat(dir, 'root.chat',
346
+ "user: run\nmeta: job=#{short}\nassistant: done\n")
347
+ index = graph(chat)
348
+ assert_equal({1 => worker}, index[:chats])
349
+ assert_empty index[:warnings]
350
+ assert Step === Step.load(index[:chats].values.first)
351
+ ensure
352
+ Chat.singleton_class.send(:remove_method, :job_reference_fallback_bases)
353
+ Chat.singleton_class.send(:alias_method, :job_reference_fallback_bases, :job_reference_fallback_bases_original)
354
+ Chat.singleton_class.send(:remove_method, :job_reference_fallback_bases_original)
355
+ end
356
+ end
357
+
358
+ # Follow-up workflow from the round-4 specification, executed end to end.
359
+ def test_follow_up_workflow_with_canonical_paths
360
+ TmpFile.with_dir do |dir|
361
+ dep = make_job(dir, 'D/prepare/Default_d')
362
+ worker = make_job(dir, 'W/ask/Default_w', dependencies: [dep],
363
+ result: "user: run\nassistant: done\n",
364
+ info: {type: 'chat'})
365
+ chat = write_chat(dir, 'root.chat',
366
+ "user: go\nmeta: job=#{worker}\nassistant: done\n")
367
+
368
+ graph = Chat.provenance_graph(chat)
369
+ job_path = graph[:chats].values.flatten.first
370
+ assert File.absolute_path?(job_path)
371
+ assert_equal worker, job_path
372
+
373
+ job = Step.load(job_path)
374
+ job_graph = Chat.provenance_graph(job)
375
+ assert_equal :job, job_graph[:type]
376
+
377
+ job_graph[:dependencies].each do |dependency_path|
378
+ assert File.absolute_path?(dependency_path)
379
+ assert Step === Step.load(dependency_path)
380
+ end
381
+ assert_equal [dep], job_graph[:dependencies]
382
+
383
+ # (5) the job root still inspects its OUTPUT chat for references and
384
+ # delegations found there.
385
+ assert_equal :chat, Chat.load(job_result_output(job)).messages.first[:role] rescue nil
386
+ nested = make_job(dir, 'N/load/Default_n')
387
+ Open.write(job.path.to_s,
388
+ "user: run\nmeta: job=#{nested}\nassistant: done\n")
389
+ assert_equal({1 => nested}, Chat.provenance_graph(job)[:chats])
390
+ end
391
+ end
392
+
393
+ # (2) receipt-derived references get the same treatment.
394
+ def test_receipt_reference_resolves_to_canonical_absolute_job_path
395
+ TmpFile.with_dir do |dir|
396
+ worker = make_job(dir, 'W/ask/Default_w')
397
+ text = receipt_chat_text({'c1' => [meta_receipt("job=#{worker}")]})
398
+ chat = write_chat(dir, 'root.chat', text)
399
+
400
+ assert_equal({2 => worker}, graph(chat)[:chats])
401
+ end
402
+ end
403
+
404
+ # (3) multiple references at one index remain intact, as canonical paths.
405
+ def test_multiple_resolved_references_stay_an_array
406
+ TmpFile.with_dir do |dir|
407
+ worker = make_job(dir, 'W/ask/Default_w')
408
+ critic = make_job(dir, 'C/ask/Default_c')
409
+ text = receipt_chat_text(
410
+ {'c1' => [meta_receipt("job=#{worker}"), meta_receipt("job=#{critic}")]})
411
+ chat = write_chat(dir, 'root.chat', text)
412
+
413
+ assert_equal({2 => [worker, critic]}, graph(chat)[:chats])
414
+ end
415
+ end
416
+
417
+ # Repeated references to the SAME job at one index deduplicate.
418
+ def test_repeated_same_job_reference_at_one_index_deduplicates
419
+ TmpFile.with_dir do |dir|
420
+ worker = make_job(dir, 'W/ask/Default_w')
421
+ text = receipt_chat_text(
422
+ {'c1' => [meta_receipt("job=#{worker}"), meta_receipt("job=#{worker}")]})
423
+ chat = write_chat(dir, 'root.chat', text)
424
+
425
+ assert_equal({2 => worker}, graph(chat)[:chats])
426
+ end
427
+ end
428
+
429
+ # A standalone meta reference that cannot resolve warns (or raises) and
430
+ # is not listed, mirroring the receipt behavior.
431
+ def test_unresolved_standalone_meta_warns
432
+ TmpFile.with_dir do |dir|
433
+ chat = write_chat(dir, 'root.chat',
434
+ "user: run\nmeta: job=Nowhere/None/Default_n\nassistant: done\n")
435
+
436
+ index = graph(chat)
437
+ assert_empty index[:chats]
438
+ assert_equal 1, index[:warnings].length
439
+ assert_match(/unresolved_job_reference/, index[:warnings].first[:error].to_s)
440
+
441
+ assert_raise ScoutException do
442
+ graph(chat, on_error: :raise)
443
+ end
444
+ end
445
+ end
446
+
447
+ # (7)/(8) a Step object is a job root; the same path WITHOUT an explicit
448
+ # root_type stays a CHAT root. Both must inspect the job.
449
+ def test_step_object_and_explicit_root_type_identify_job_root
450
+ TmpFile.with_dir do |dir|
451
+ dep = make_job(dir, 'D/prepare/Default_d')
452
+ output = "user: hi\nassistant: done\n"
453
+ worker = make_job(dir, 'W/ask/Default_w', result: output,
454
+ dependencies: [dep], info: {type: 'chat'})
455
+ step = Step.load(worker)
456
+
457
+ [Chat.provenance_graph(step),
458
+ Chat.provenance_graph(step.path, root_type: :job)].each do |job_index|
459
+ assert_equal :job, job_index[:type]
460
+ assert_equal [dep], job_index[:dependencies]
461
+ # The output chat exists and is readable at step.path; its presence
462
+ # must not flip the path-only call to a chat root... it does not:
463
+ end
464
+
465
+ # Without root_type, the same path is a chat root (the output
466
+ # transcript itself), with no dependencies.
467
+ chat_index = Chat.provenance_graph(step.path)
468
+ assert_equal :chat, chat_index[:type]
469
+ assert_empty chat_index[:dependencies]
470
+ end
471
+ end
472
+
473
+ private
474
+
475
+ # The job's persisted result file when it is a chat-producing job.
476
+ def job_result_output(job)
477
+ Chat.job_result_chat_file(job).to_s
478
+ end
479
+ end