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
@@ -0,0 +1,150 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/llm/agent'
3
+ require 'scout/llm/tools/call'
4
+ require 'json'
5
+
6
+ # Regression guards for answer attribution when one tool round returns the
7
+ # SAME LLM::Agent object from several tool calls (two `ask` calls to one
8
+ # registered conversation, two delegate hand-offs to one specialist slot).
9
+ #
10
+ # Historical bug (call.rb ~184): outputs were paired with answers via
11
+ # `agent_answers[agents.index(content)]`; Array#index finds the FIRST
12
+ # occurrence, so every duplicate-agent call embedded the FIRST answer, and
13
+ # current_chat.follow(res) followed the wrong chat.
14
+ class TestLLMToolCallAgentPairing < Test::Unit::TestCase
15
+ # ScoutCoder: LLM.process_answers asks agents through Open.traverse with
16
+ # cpus from Scout::Config.get(:cpus, :agent_ask, :agents, env:
17
+ # 'ASK_AGENTS', default: 3). LLM::Mock's response counter (@index) is
18
+ # shared mutable state, so concurrent rounds race on it and answers get
19
+ # duplicated/lost nondeterministically. Pin the collection to sequential
20
+ # execution so pairing assertions are deterministic; the race is a test
21
+ # harness limitation, not the behavior under test.
22
+ setup do
23
+ Scout::Config.set({cpus: 1}, :agent_ask, :agents)
24
+ end
25
+
26
+ def shared_worker
27
+ LLM::Agent.new(start_chat: Chat.setup([{role: :system, content: 'worker helper'}]), endpoint: :mock)
28
+ end
29
+
30
+ def call(id, prompt)
31
+ IndiferentHash.setup({
32
+ 'id' => id, 'type' => 'function',
33
+ 'function' => {'name' => 'ask', 'arguments' => {prompt: prompt}.to_json}
34
+ })
35
+ end
36
+
37
+ # One tool whose block appends the prompt to the SHARED agent and returns
38
+ # that same agent object from every call.
39
+ def shared_agent_tool(worker)
40
+ Proc.new do |_name, args|
41
+ worker.user args[:prompt]
42
+ worker
43
+ end
44
+ end
45
+
46
+ def outputs_for(messages)
47
+ messages.select { |m| m[:role].to_s == 'function_call_output' }
48
+ end
49
+
50
+ def output_content(message)
51
+ JSON.parse(message[:content])['content']
52
+ end
53
+
54
+ def test_duplicate_agent_answers_pair_by_call_position
55
+ TmpFile.with_dir do |_dir|
56
+ LLM::Mock.script('first answer', 'second answer')
57
+
58
+ worker = shared_worker
59
+
60
+ result = LLM.process_calls({'ask' => [shared_agent_tool(worker), nil]}, [
61
+ call('call_1', 'first question'),
62
+ call('call_2', 'second question')
63
+ ])
64
+
65
+ outputs = outputs_for(result)
66
+ assert_equal 2, outputs.length
67
+
68
+ first_content = output_content(outputs[0])
69
+ second_content = output_content(outputs[1])
70
+
71
+ # Each function_call_output carries its OWN round's answer: call_1
72
+ # answered "first answer", call_2 answered "second answer". The old
73
+ # agents.index(content) pairing made BOTH carry "first answer".
74
+ assert_equal 'first answer', first_content
75
+ assert_equal 'second answer', second_content
76
+ assert_not_equal first_content, second_content
77
+
78
+ # The shared conversation itself followed BOTH rounds in order, so the
79
+ # final answer (content.answer = last assistant message) is the second
80
+ # round's.
81
+ assert_equal 'second answer', worker.current_chat.answer
82
+
83
+ # Both prompts reached the shared agent, in call order.
84
+ user_messages = worker.current_chat.select { |m| m[:role].to_s == 'user' }.collect { |m| m[:content] }
85
+ assert_equal ['first question', 'second question'], user_messages
86
+ end
87
+ end
88
+
89
+ # The general case stays intact: DISTINCT agents in one round each get
90
+ # their own answer (no cross-talk in either direction).
91
+ def test_distinct_agents_pair_by_call_position
92
+ TmpFile.with_dir do |_dir|
93
+ LLM::Mock.script('left answer', 'right answer')
94
+
95
+ left = shared_worker
96
+ right = shared_worker
97
+
98
+ tool = Proc.new do |_name, args|
99
+ target = args[:prompt].include?('left') ? left : right
100
+ target.user args[:prompt]
101
+ target
102
+ end
103
+
104
+ result = LLM.process_calls({'ask' => [tool, nil]}, [
105
+ call('call_1', 'left question'),
106
+ call('call_2', 'right question')
107
+ ])
108
+
109
+ outputs = outputs_for(result)
110
+ assert_equal 2, outputs.length
111
+ assert_equal 'left answer', output_content(outputs[0])
112
+ assert_equal 'right answer', output_content(outputs[1])
113
+
114
+ assert_equal 'left answer', left.current_chat.answer
115
+ assert_equal 'right answer', right.current_chat.answer
116
+ end
117
+ end
118
+
119
+ # Mixed round: one agent-returning call and one plain String call. The
120
+ # agent answer must still pair with the agent call, not shift into the
121
+ # string output's position.
122
+ def test_mixed_agent_and_string_outputs_pair_correctly
123
+ TmpFile.with_dir do |_dir|
124
+ LLM::Mock.script('agent answer')
125
+
126
+ worker = shared_worker
127
+ seen = nil
128
+ tool = Proc.new do |_name, args|
129
+ if args[:prompt].include?('string')
130
+ seen = 'plain result'
131
+ else
132
+ worker.user args[:prompt]
133
+ worker
134
+ end
135
+ end
136
+
137
+ result = LLM.process_calls({'ask' => [tool, nil]}, [
138
+ call('call_1', 'agent question'),
139
+ call('call_2', 'string question')
140
+ ])
141
+
142
+ outputs = outputs_for(result)
143
+ assert_equal 2, outputs.length
144
+ assert_equal 'agent answer', output_content(outputs[0])
145
+ assert_equal 'plain result', output_content(outputs[1])
146
+ assert_equal 'agent answer', worker.current_chat.answer
147
+ assert_equal 'plain result', seen
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,316 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
3
+ require 'scout/llm/agent'
4
+
5
+ class TestCallDelegationConflict < Test::Unit::TestCase
6
+ def setup
7
+ super
8
+ end
9
+
10
+ # `call(id, name, args)` and `ask_call(id, args)` helpers: providers send
11
+ # the function name and a JSON arguments string; process_calls receives
12
+ # that provider shape unchanged.
13
+ def call(id, name, arguments)
14
+ IndiferentHash.setup(
15
+ 'id' => id,
16
+ 'type' => 'function',
17
+ 'function' => {
18
+ 'name' => name.to_s,
19
+ 'arguments' => arguments.to_json
20
+ }
21
+ )
22
+ end
23
+
24
+ def ask_call(id, arguments)
25
+ call(id, 'ask', arguments)
26
+ end
27
+
28
+ def hand_off_call(id, arguments)
29
+ call(id, 'hand_off_to_Worker', arguments)
30
+ end
31
+
32
+ # Register the real socialize ask tool on a parent agent so the delegated
33
+ # agents are produced by the production path, then dispatch calls through
34
+ # LLM.process_calls with a save-file-bearing parent so conversations are
35
+ # persisted (anchor save files are derived from it).
36
+ def dispatch(parent, tool_calls)
37
+ tools = parent.other_options[:tools] || {}
38
+ LLM.process_calls(tools, tool_calls)
39
+ end
40
+
41
+ def parent_with_ask(dir)
42
+ parent = LLM::Agent.new(
43
+ start_chat: Chat.setup([{role: :system, content: 'orchestrator'}])
44
+ )
45
+ parent.save_file = File.join(dir, 'root.chat')
46
+ # The ask tool and a pre-registered Worker template, so the delegated
47
+ # agents are produced by the production path (socialize + society).
48
+ parent.society = {'Worker' => worker_agent}
49
+ parent.socialize
50
+ parent
51
+ end
52
+
53
+ def worker_agent(content = 'worker helper')
54
+ LLM::Agent.new(
55
+ start_chat: Chat.setup([{role: :system, content: content}])
56
+ )
57
+ end
58
+
59
+ # Tool-round outputs are function_call_output messages whose content is
60
+ # the JSON envelope {id, name, content, error} (no error key when the
61
+ # call succeeded). Parse them into a convenient array of hashes.
62
+ def outputs_of(result)
63
+ result
64
+ .select { |m| m[:role].to_s == 'function_call_output' }
65
+ .collect { |m| JSON.parse(m[:content]) }
66
+ end
67
+
68
+ # A mixed tool round: one ordinary side-effecting Proc tool runs before
69
+ # the delegations. Its result must survive a delegation conflict: the
70
+ # conflict invalidates the agent sub-batch, not the whole round.
71
+ def parent_with_ask_and_write(dir)
72
+ parent = parent_with_ask(dir)
73
+ tools = parent.other_options[:tools]
74
+ tools[:write] = [
75
+ Proc.new do |_name, args|
76
+ Open.write(File.join(dir, args[:path]), args[:text])
77
+ "written #{args[:path]}"
78
+ end,
79
+ LLM.tool_definition(:write, 'Write a file',
80
+ {path: {type: 'string'}, text: {type: 'string'}},
81
+ required: [:path, :text])
82
+ ]
83
+ parent.other_options[:tools] = tools
84
+ parent
85
+ end
86
+
87
+ # The original hang: two delegations of one round, no conversation name,
88
+ # both resolving onto Worker/default. The round now completes with two
89
+ # error envelopes instead of blocking forever; Timeout remains the
90
+ # honest bound on the regression.
91
+ def test_two_unnamed_delegations_of_one_round_return_errors_instead_of_hanging
92
+ Timeout.timeout(30) do
93
+ TmpFile.with_dir do |dir|
94
+ parent = parent_with_ask(dir)
95
+
96
+ outputs = nil
97
+ assert_nothing_raised do
98
+ outputs = outputs_of dispatch(parent, [
99
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X'}),
100
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y'})
101
+ ])
102
+ end
103
+
104
+ assert_equal 2, outputs.length
105
+ outputs.each do |out|
106
+ assert_equal 'error', out['error']
107
+ assert_match(/Conflicting delegations/, out['content']
108
+ .sub(/^\{"exception":/, '')
109
+ .sub(/"\}$/, ''))
110
+ end
111
+ # No specialist was started and nothing was persisted.
112
+ assert_equal 0, LLM::Mock.index
113
+ assert !Open.exist?(File.join(dir, 'root.society'))
114
+ end
115
+ end
116
+ end
117
+
118
+ # Same shape with an explicit conversation name.
119
+ def test_two_named_delegations_of_one_round_return_errors
120
+ Timeout.timeout(30) do
121
+ TmpFile.with_dir do |dir|
122
+ parent = parent_with_ask(dir)
123
+
124
+ outputs = outputs_of dispatch(parent, [
125
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'task_1'}),
126
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y', conversation: 'task_1'})
127
+ ])
128
+
129
+ assert_equal 2, outputs.length
130
+ outputs.each do |out|
131
+ assert_equal 'error', out['error']
132
+ assert_match(/task_1/, out['content'])
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ # The whole agent sub-batch is rejected, including delegations that
139
+ # target a distinct conversation: all-or-nothing keeps the rule
140
+ # predictable for the model.
141
+ def test_partial_conflict_rejects_all_delegations_of_the_round
142
+ Timeout.timeout(30) do
143
+ TmpFile.with_dir do |dir|
144
+ parent = parent_with_ask(dir)
145
+
146
+ outputs = outputs_of dispatch(parent, [
147
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X'}),
148
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y'}),
149
+ ask_call('call_3', {agent: 'Worker', prompt: 'do Z', conversation: 'other'})
150
+ ])
151
+
152
+ assert_equal 3, outputs.length
153
+ outputs.each do |out|
154
+ assert_equal 'error', out['error']
155
+ end
156
+ # Even the delegation to a distinct conversation did not run.
157
+ assert_equal 0, LLM::Mock.index
158
+ other = parent.conversation_agent('Worker/other')
159
+ assert other.nil? || other.current_chat.none? { |m| m[:role].to_s == 'user' }
160
+ end
161
+ end
162
+ end
163
+
164
+ # Ordinary tool calls of the same round are untouched: their side
165
+ # effects have already happened and the model must see their results.
166
+ def test_side_effecting_tool_of_the_same_round_keeps_its_result
167
+ Timeout.timeout(30) do
168
+ TmpFile.with_dir do |dir|
169
+ parent = parent_with_ask_and_write(dir)
170
+
171
+ outputs = outputs_of dispatch(parent, [
172
+ call('call_1', 'write', {path: 'out.txt', text: 'hello'}),
173
+ ask_call('call_2', {agent: 'Worker', prompt: 'do X'}),
174
+ ask_call('call_3', {agent: 'Worker', prompt: 'do Y'})
175
+ ])
176
+
177
+ assert_equal 3, outputs.length
178
+ assert_equal 'written out.txt', outputs.first['content']
179
+ assert !outputs.first.key?('error')
180
+ assert_equal 'write', outputs.first['name']
181
+
182
+ assert File.read(File.join(dir, 'out.txt')) == 'hello'
183
+ outputs[1..-1].each do |out|
184
+ assert_equal 'error', out['error']
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ # ask and a hand_off slot resolve onto the same conversation holder
191
+ # (Worker/Worker: the slot named after the specialist) and conflict
192
+ # inside one round.
193
+ def test_ask_and_hand_off_onto_the_same_conversation_conflict
194
+ LLM::Mock.script('worker answer')
195
+ Timeout.timeout(30) do
196
+ TmpFile.with_dir do |dir|
197
+ parent = parent_with_ask(dir)
198
+ parent.delegate(parent.society['Worker'], 'Worker', 'Hand work off')
199
+
200
+ # `hand_off_to_Worker` asks the conversation named after its slot,
201
+ # so an `ask` naming that same conversation collides with it.
202
+ outputs = outputs_of dispatch(parent, [
203
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'Worker'}),
204
+ hand_off_call('call_2', {message: 'do Y'})
205
+ ])
206
+
207
+ assert_equal 2, outputs.length
208
+ outputs.each do |out|
209
+ assert_equal 'error', out['error']
210
+ assert_match(/Conflicting delegations/, out['content'])
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ # A conflicting batch restores the live conversation EXACTLY, whatever
217
+ # mixture of delegation forms staged it: plain continuation prompts, a
218
+ # clear_tools continuation (which staged a context-only marker before
219
+ # its prompt), and a hand_off new_conversation RESTART (which had
220
+ # already re-branched the chat in place before the conflict was even
221
+ # detected). Both delegations of the same slot hold the SAME holder
222
+ # object (the `@chats` registry returns one agent per conversation
223
+ # key), so this also covers one chat mutated through two agent
224
+ # references. Compare the complete message list, roles and contents,
225
+ # before and after.
226
+ def test_rejected_batch_restores_the_conversation_exactly
227
+ LLM::Mock.script('worker answer')
228
+ Timeout.timeout(30) do
229
+ TmpFile.with_dir do |dir|
230
+ parent = parent_with_ask(dir)
231
+ parent.delegate(parent.society['Worker'], 'Worker', 'Hand work off')
232
+
233
+ # Round 0: establish Worker/Worker with one completed turn.
234
+ outputs_of dispatch(parent, [
235
+ hand_off_call('call_0', {message: 'seed turn'})
236
+ ])
237
+ holder = parent.conversation_agent('Worker/Worker')
238
+ before = holder.current_chat.collect { |m| [m[:role], m[:content]] }
239
+
240
+ # Round 1: three staged delegations onto the same conversation:
241
+ # plain ask, clear_tools ask, and a restarting hand_off.
242
+ outputs = outputs_of dispatch(parent, [
243
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'Worker'}),
244
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y', conversation: 'Worker', inherit: 'clear_tools'}),
245
+ hand_off_call('call_3', {message: 'do Z', new_conversation: true})
246
+ ])
247
+
248
+ assert_equal 3, outputs.length
249
+ outputs.each do |out|
250
+ assert_equal 'error', out['error']
251
+ assert_match(/Conflicting delegations/, out['content'])
252
+ end
253
+
254
+ after = holder.current_chat.collect { |m| [m[:role], m[:content]] }
255
+ assert_equal before, after
256
+
257
+ # The undo left no restart bookkeeping behind either.
258
+ assert_nil holder.restart_undo_stash
259
+ end
260
+ end
261
+ end
262
+
263
+ # A valid batch still works after a rejected one, and the rejected
264
+ # prompts do not leak into the conversation: preparation appends them
265
+ # in memory, and a rejected batch rolls them back before the round
266
+ # finishes.
267
+ def test_valid_delegation_after_rejection_starts_clean
268
+ LLM::Mock.script('worker answer')
269
+ Timeout.timeout(30) do
270
+ TmpFile.with_dir do |dir|
271
+ parent = parent_with_ask(dir)
272
+
273
+ outputs_of dispatch(parent, [
274
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X'}),
275
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y'})
276
+ ])
277
+
278
+ second = outputs_of dispatch(parent, [
279
+ ask_call('call_3', {agent: 'Worker', prompt: 'do W'})
280
+ ])
281
+
282
+ assert_equal 1, second.length
283
+ assert !second.first.key?('error')
284
+ assert_equal 'worker answer', second.first['content']
285
+
286
+ child = parent.conversation_agent('Worker/default')
287
+ user_turns = child.current_chat
288
+ .select { |m| m[:role].to_s == 'user' }
289
+ .collect { |m| m[:content] }
290
+ assert_equal ['do W'], user_turns
291
+ end
292
+ end
293
+ end
294
+
295
+ # Distinct conversations of one round execute normally and their answers
296
+ # are paired to the original calls. All specialists share one scripted
297
+ # answer, so the observable fact is that both calls were answered.
298
+ def test_distinct_conversations_of_one_round_still_work
299
+ LLM::Mock.script('worker answer')
300
+ Timeout.timeout(30) do
301
+ TmpFile.with_dir do |dir|
302
+ parent = parent_with_ask(dir)
303
+
304
+ outputs = outputs_of dispatch(parent, [
305
+ ask_call('call_1', {agent: 'Worker', prompt: 'do X', conversation: 'one'}),
306
+ ask_call('call_2', {agent: 'Worker', prompt: 'do Y', conversation: 'two'})
307
+ ])
308
+
309
+ assert_equal 2, outputs.length
310
+ contents = outputs.collect { |out| out['content'] }
311
+ assert contents.all? { |c| c == 'worker answer' }
312
+ outputs.each { |out| assert !out.key?('error') }
313
+ end
314
+ end
315
+ end
316
+ end