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
@@ -47,13 +47,16 @@ module LLM
47
47
  File.join(File.dirname(p), File.basename(p).sub(/\.chat\z/, '.society'))
48
48
  end
49
49
 
50
- # ScoutCoder: LEGACY root society directory (`<path>.files/log/society`)
51
- # from before the flat layout change. Kept read-only: nothing writes
52
- # here anymore, but provenance traversal must still glob it so chats
53
- # saved by older versions remain visible (see
54
- # Chat.direct_chat_sidecar_files).
55
- def legacy_society_dir_for(chat_path)
56
- "#{chat_path}.files/log/#{SOCIETY_DIR}"
50
+ # Canonical chat file of an agent run inside a workflow job (or any
51
+ # other files_dir owner): `<files_dir>/<agent_name || 'agent'>.chat`.
52
+ #
53
+ # ScoutCoder: this rule was duplicated in AgentWorkflow#log_agent and
54
+ # restated in a comment at scout_commands/agent/ask; it lives here now,
55
+ # beside the other layout rules, as the single source of truth. Both
56
+ # Path and String files_dir are accepted and the result is always a
57
+ # plain String.
58
+ def canonical_chat_file(files_dir, agent_name = nil)
59
+ File.join(files_dir.to_s, "#{agent_name || 'agent'}.chat")
57
60
  end
58
61
 
59
62
  # Is `path` the chat file of a NESTED conversation, i.e. does it already
@@ -134,8 +137,7 @@ module LLM
134
137
  # useful contract is "these files now hold this state").
135
138
  def save(path = nil, visited: nil, seen_agents: nil, depth: 0)
136
139
  path ||= save_file
137
- raise ScoutException,
138
- "No save file for agent. Pass a path or configure agent.save_file = <chat file>" if path.nil?
140
+ return if path.nil?
139
141
 
140
142
  path = path.to_s
141
143
  visited ||= Set.new
@@ -3,6 +3,39 @@ require 'scout/workflow'
3
3
  module AgentWorkflow
4
4
  extend Workflow
5
5
 
6
+ # Job-directory pinning (path-map leak fix, hypothesis H2).
7
+ #
8
+ # Workflow.directory defaults to Path.setup('var/jobs') — a RELATIVE,
9
+ # unlocated Scout path. Task directories derived from it
10
+ # (lib/scout/workflow/definition.rb: `directory: directory[name]`) and any
11
+ # later Path#find on the resulting job path resolve lazily through Scout's
12
+ # map_order, which ends at :user (~/.scout) whenever the target does not
13
+ # exist under :current first. That sent agent-run workflow jobs to
14
+ # ~/.scout/var/jobs while the checkout var/jobs stayed empty.
15
+ #
16
+ # chat_task is scout-ai's own task-declaration DSL and the single point
17
+ # where this library derives a job directory for agent-run tasks, so the
18
+ # pin happens here, once per workflow module, before `directory[name]`
19
+ # is evaluated. Only the unlocated default is upgraded in place; any
20
+ # caller that already set Workflow.directory (tests, CLI, other repos)
21
+ # keeps its setting untouched.
22
+ #
23
+ # THIS IS PROBABLY WRONG
24
+ #
25
+ #def self.pin_workflow_job_directory!(workflow)
26
+ # dir = workflow.directory rescue nil
27
+ # return dir if dir.nil? || dir.to_s.start_with?(File::SEPARATOR)
28
+
29
+ # global = Workflow.directory
30
+ # if global.nil? || global.to_s.empty?
31
+ # Workflow.directory = global = Path.setup(Scout.root.find(:current))['var']['jobs']
32
+ # elsif !global.to_s.start_with?(File::SEPARATOR)
33
+ # Workflow.directory = global = Path.setup(Scout.root.find(:current))[global.to_s]
34
+ # end
35
+ # workflow.directory = global[workflow.name]
36
+ # workflow.directory
37
+ #end
38
+
6
39
  helper :chat do |chat=nil|
7
40
  @chat ||= begin
8
41
  chat = recursive_inputs[:chat]
@@ -35,28 +68,50 @@ module AgentWorkflow
35
68
  self.tooling.select { |msg| msg[:role] == 'introduce' }
36
69
  end
37
70
 
38
- helper :agent do |name = nil, chat: nil, options: nil, tooling: nil, files: nil, **kwargs|
71
+ # Build the workflow job's own agent, anchored at creation. The layout rule
72
+ # (`Agent.canonical_chat_file`, same as `log_agent`) and the `job` wiring
73
+ # both happen BEFORE the first chat/start call, so the per-round auto-save
74
+ # in Agent#chat and the restart snapshots in Agent#start are in effect from
75
+ # the very first round, and any specialist socialized mid-round derives
76
+ # its society layout from a real parent anchor instead of nil. The
77
+ # `job_path_message` chooses to add a messages that informs the agent
78
+ # about the location of the job path and it's files_directory, a useful
79
+ # message in multi-step inference workflows but which may interfere with
80
+ # cache mechanisms.
81
+ helper :agent do |name = nil, chat: nil, options: nil, tooling: nil, job_path_message: true, files: nil, **kwargs|
39
82
  options = self.options if options.nil?
40
83
  tooling = self.tooling if tooling.nil?
41
84
  options = IndiferentHash.add_defaults kwargs, options
42
85
 
43
- agent = LLM.load_agent name, agent_options(options)
86
+ # Resolve the workflow template, then use the same independent clone
87
+ # mechanics as social conversations. The seed additions below remain
88
+ # workflow policy (tooling, job messages, files, and dependent chat).
89
+ template = LLM.load_agent name, agent_options(options)
90
+ agent = LLM::Agent::Construction.clone_agent(template)
44
91
  agent.job = self
45
92
  agent.start_chat.follow tooling if tooling && !tooling.empty?
46
-
47
- agent.start_chat.system <<-EOF
93
+ agent.save_file = LLM::Agent.canonical_chat_file(files_dir, name)
94
+
95
+ if job_path_message
96
+ # ScoutCoder: agent.start_chat.system ... in this helper runs in the
97
+ # JOB process (fork), not the caller's process, so any test observing
98
+ # the system prompt must read the persisted <job>.files/<name>.chat
99
+ # after produce, or stub the helper; in-process instrumentation placed
100
+ # around job.produce never sees these lines.
101
+ agent.start_chat.system <<-EOF
48
102
  Your current working directory is #{Dir.pwd}.
49
- You are working through an ask job with path #{self.path} and files_dir #{self.files_dir}.
50
- EOF
103
+ You are working through an workflow inference job with path #{self.path} and files_dir #{self.files_dir}.
104
+ EOF
51
105
 
52
- if dependencies.any?
53
- agent.start_chat.system <<-EOF
106
+ if dependencies.any?
107
+ agent.start_chat.system <<-EOF
54
108
  This workflow job has the following depencencies:
55
109
 
56
110
  #{rec_dependencies.collect(&:path) * "\n"}
57
111
 
58
112
  Their input should already be incorporated, but they might have artifacts
59
- EOF
113
+ EOF
114
+ end
60
115
  end
61
116
 
62
117
  if chat && !chat.empty?
@@ -98,13 +153,14 @@ There are other jobs found in this chat:
98
153
  # every nested conversation under
99
154
  # <files_dir>/<agent_name>.society/<agent>/<conversation>/agent.chat.
100
155
  #
101
- # ScoutCoder: the save_file MUST be assigned BEFORE the first chat/start
102
- # call (chat_task does it for us here, but any caller must too): the
103
- # Agent#chat ensure-hook and the restart-snapshot hook in Agent#start
104
- # both write through save_file, so assigning it late loses the
105
- # intermediate turns. `agent_name` defaults to 'agent' and names the
156
+ # The `agent` helper anchors the agents it builds at creation time
157
+ # (save_file assigned there), so per-round auto-saves and restart
158
+ # snapshots already write through it by the time we get here. This is
159
+ # the end-of-task sweep for such agents and the fallback anchor for any
160
+ # agent constructed directly inside the block (LLM::Agent.new), which
161
+ # has no save_file yet. `agent_name` defaults to 'agent' and names the
106
162
  # file, never a directory: worker.chat -> worker.society, no log/ level.
107
- agent.save_file = files_dir["#{agent_name || 'agent'}.chat"] if agent.save_file.nil?
163
+ agent.save_file = LLM::Agent.canonical_chat_file(files_dir, agent_name) if agent.save_file.nil?
108
164
  agent.save
109
165
 
110
166
  update_info :dependencies, dependencies.collect { |dependency| dependency.path.find }
@@ -114,6 +170,9 @@ end
114
170
 
115
171
  module Workflow
116
172
  def chat_task(task_name, &block)
173
+ # Pin the job directory of this workflow module before `directory[name]`
174
+ # is evaluated inside `task` (see AgentWorkflow.pin_workflow_job_directory!).
175
+ AgentWorkflow.pin_workflow_job_directory!(self) if AgentWorkflow.respond_to?(:pin_workflow_job_directory!)
117
176
  input :chat, :text, 'Chat in Scout-AI chat-file format'
118
177
  task task_name => :chat do |chat|
119
178
  begin
@@ -152,7 +211,9 @@ module Workflow
152
211
  require_workflow_old(name, ...)
153
212
  rescue => e
154
213
  begin
155
- LLM.load_agent(name).workflow
214
+ wf = LLM.load_agent(name).workflow
215
+ raise e unless wf.name.to_s == name.to_s
216
+ wf
156
217
  rescue
157
218
  raise e
158
219
  end
@@ -10,7 +10,7 @@ module LLM
10
10
  end
11
11
 
12
12
  class Agent
13
- attr_accessor :workflow, :knowledge_base, :start_chat, :process_exception, :other_options, :path, :job
13
+ attr_accessor :workflow, :knowledge_base, :start_chat, :process_exception, :other_options, :path, :job, :request_context, :start_chat_file
14
14
 
15
15
  def initialize(workflow: nil, knowledge_base: nil, start_chat: nil, **kwargs)
16
16
  @workflow = workflow
@@ -20,6 +20,18 @@ module LLM
20
20
  @start_chat = start_chat
21
21
  end
22
22
 
23
+ # Whether this agent carries a start_chat that represents deliberate
24
+ # preparation: a start_chat file found by LLM::Agent.load_agent, or any
25
+ # chat explicitly supplied at construction. load_agent's synthetic
26
+ # `introduce` fallback (workflow documentation with no start_chat file)
27
+ # sets this to false. Agent#ask gates implicit path-kind loading on
28
+ # this signal: a prepared agent registers kinds ONLY through explicit
29
+ # `path_kind:` chat lines.
30
+ def start_chat_file?
31
+ return !!@start_chat_file unless @start_chat_file.nil?
32
+ !@start_chat.nil? && !@start_chat.empty?
33
+ end
34
+
23
35
  def workflow(&block)
24
36
  if block_given?
25
37
  workflow = self.workflow
@@ -31,6 +43,11 @@ module LLM
31
43
  m.extend Workflow
32
44
  m.name = 'ScoutAgent'
33
45
  m.tasks = {}
46
+ m.libdir = nil
47
+ # Pin the anonymous agent workflow's job directory to
48
+ # the checkout var/jobs (path-map leak fix, H2); see
49
+ # AgentWorkflow.pin_workflow_job_directory!.
50
+ #AgentWorkflow.pin_workflow_job_directory!(m) if AgentWorkflow.respond_to?(:pin_workflow_job_directory!)
34
51
  m
35
52
  end
36
53
  end
@@ -42,6 +59,10 @@ module LLM
42
59
  end * "\n"
43
60
  end
44
61
 
62
+ def agent_name
63
+ workflow.to_s
64
+ end
65
+
45
66
  #def system_prompt
46
67
  # system = @system
47
68
  # system = [] if system.nil?
@@ -64,6 +85,12 @@ module LLM
64
85
  # function: takes an array of messages and calls LLM.ask with them
65
86
  def ask(messages = nil, options = {})
66
87
  messages, options = nil, messages if options.empty? && Hash === messages
88
+ save_file = self.save_file
89
+
90
+ request_context = options.delete(:request_context) || @request_context || {}
91
+ request_context[:caller] = save_file if save_file
92
+ request_context = IndiferentHash.add_defaults request_context, main_chat: request_context[:caller]
93
+
67
94
  messages = current_chat if messages.nil?
68
95
  messages = [messages] unless messages.is_a? Array
69
96
  model ||= @model if model
@@ -83,14 +110,23 @@ module LLM
83
110
  end
84
111
 
85
112
  job = workflow.job(:ask, chat: Chat.print(messages))
86
- self.job = job
87
- Open.mkdir job.files_dir
88
- Chat.allow_job job
113
+ if request_context
114
+ job.extend(RequestContext::StepExtension)
115
+ job.register_request_context(request_context)
116
+ end
117
+
118
+ self.message(:meta, Chat.serialize_meta(job: job.short_path))
119
+ self.save
120
+
89
121
  job.clean if ENV['SCOUT_NO_ASK_CACHE'] == 'true'
90
122
  job.recursive_clean if ENV['SCOUT_NO_ASK_CACHE'] == 'recursive'
123
+
124
+ Open.mkdir job.files_dir
125
+ Chat.allow_job job
126
+
91
127
  job.produce
92
128
 
93
- messages = Chat.project(job.short_path, LLM.chat(job.path))
129
+ messages = Chat.load(job.path)
94
130
  if options[:return_messages]
95
131
  Chat.setup(messages)
96
132
  else
@@ -98,18 +134,78 @@ module LLM
98
134
  end
99
135
  else
100
136
 
101
- if (list = messages.select{|info| info[:role] == 'socialize'}).any?
102
- socialize = list.last[:content]
103
- messages.delete_if{|info| info[:role] == 'socialize' }
104
- self.socialize(options.dup) if socialize && %w(true TRUE True T 1).include?(socialize.to_s)
137
+ # Path-kind configuration. Two loading rules meet here:
138
+ #
139
+ # 1. Explicit `path_kind: <Workflow> [<kind>...]` lines are honored
140
+ # wherever they appear (start_chat or user chat): first token the
141
+ # workflow, the rest kind names; multiple kinds per line and
142
+ # multiple lines accumulate. An empty kind list means every
143
+ # kind of that workflow. Resolution reuses Chat.load_workflow;
144
+ # an unloadable workflow or an unknown kind raises loudly
145
+ # (ParameterException, the same step-1 behavior), and registration
146
+ # happens from the FULL message list BEFORE any stripping, so
147
+ # clear_tools or role removal never bypasses it.
148
+ #
149
+ # 2. Implicit loading happens ONLY when the agent carries no
150
+ # deliberate start_chat (`start_chat_file?` false): then the
151
+ # agent's own workflow loads its PATH_KINDS (today's fallback
152
+ # loaded none), and whole-workflow `tool:` lines keep their
153
+ # historical kind incorporation as back-compat. With a start_chat
154
+ # present, `tool: W` configures tools only -- zero kinds load
155
+ # unless a `path_kind:` line says otherwise.
156
+ path_kind_lines = Chat.find_role(messages, :path_kind)
157
+
158
+ unless start_chat_file?
159
+ # Own workflow first: when the agent's workflow declares
160
+ # PATH_KINDS, an unprepared agent gets all of them (the
161
+ # synthetic-introduce fallback used to load none).
162
+ integrate_workflow_path_kinds(@workflow) if @workflow
163
+
164
+ Chat.find_role(messages, :tool).each do |message|
165
+ workflow_name, task_name, *_inputs = Chat.content_tokens(message)
166
+ next if task_name
167
+ next if workflow_name.nil? || workflow_name.empty?
168
+ next if Open.remote? workflow_name
169
+
170
+ begin
171
+ workflow = Chat.load_workflow workflow_name
172
+ rescue
173
+ next
174
+ end
175
+ integrate_workflow_path_kinds(workflow)
176
+ end
105
177
  end
106
178
 
107
- if (list = messages.select{|info| info[:role] == 'attachments'}).any?
108
- attachments = list.last[:content]
109
- messages.delete_if{|info| info[:role] == 'attachments' }
110
- self.attachments if attachments && %w(true TRUE True T 1).include?(attachments.to_s)
179
+ path_kind_lines.each do |message|
180
+ tokens = Chat.content_tokens(message)
181
+ next if tokens.empty?
182
+ workflow_name = tokens.shift
183
+ next if workflow_name.nil? || workflow_name.empty?
184
+ # No kind names on the line: every kind of the workflow, the
185
+ # same all-kinds semantics as a whole-workflow `tool:` line.
186
+ selection = tokens.empty? ? nil : tokens
187
+
188
+ begin
189
+ workflow = Chat.load_workflow workflow_name
190
+ rescue Exception
191
+ raise ParameterException, "The workflow #{workflow_name.inspect} named by the path_kind line could not be loaded: #{$!.message}"
192
+ end
193
+
194
+ integrate_workflow_path_kinds(workflow, only: selection)
111
195
  end
112
196
 
197
+ messages.remove_role(:path_kind)
198
+
199
+ %w(socialize attachments).each do |capability|
200
+ if (list = messages.remove_role(capability)).any?
201
+ status = list.last[:content]
202
+ next if status.nil?
203
+ status = status.to_s
204
+ next if %w(false FALSE False F 0).include?(status)
205
+ next unless %w(true TRUE True T 1).include?(status)
206
+ self.send(capability.to_sym, options.dup)
207
+ end
208
+ end
113
209
 
114
210
  tools = options[:tools] || {}
115
211
  if other_tools = @other_options[:tools]
@@ -117,14 +213,23 @@ module LLM
117
213
  tools = tools.merge other_tools
118
214
  end
119
215
 
120
-
121
216
  if workflow || knowledge_base
122
217
  tools.merge!(LLM.workflow_tools(workflow)) if workflow && workflow.tasks.any?
123
218
  tools.merge!(LLM.knowledge_base_tool_definition(knowledge_base)) if knowledge_base and knowledge_base.all_databases.any?
124
219
  end
125
220
 
126
221
  options[:tools] = tools
127
- LLM.ask messages, @other_options.except(:no_ask_override).merge(log_errors: true, save_file: self.save_file).merge(options).merge(agent: false)
222
+
223
+ ask_options = @other_options.except(:no_ask_override)
224
+ IndiferentHash.setup(ask_options)
225
+ ask_options= ask_options.merge(log_errors: true, save_file: save_file)
226
+ ask_options = ask_options.merge(options)
227
+ ask_options = ask_options.merge(request_context: request_context, agent: false)
228
+
229
+ config_endpoint = Scout::Config.get('endpoint', agent_name)
230
+ ask_options[:endpoint] = config_endpoint if config_endpoint
231
+
232
+ LLM.ask messages, ask_options
128
233
  end
129
234
  rescue
130
235
  exception = $!
@@ -137,7 +242,7 @@ module LLM
137
242
  end
138
243
  else
139
244
  begin
140
- self.save if self.save_file
245
+ self.save if save_file
141
246
  rescue
142
247
  Log.exception $!
143
248
  ensure
@@ -167,9 +272,9 @@ module LLM
167
272
 
168
273
  def self.load_agent(agent_name = nil, options = {})
169
274
  agent_name = agent_name.to_s if Symbol === agent_name
170
- if agent_name && Path.is_filename?(agent_name)
275
+ if agent_name && ::Path.is_filename?(agent_name)
171
276
  if File.directory?(agent_name)
172
- dir = Path.setup(agent_name) unless Path === agent_name
277
+ dir = Path.setup(agent_name)
173
278
  if dir.agent.find_with_extension("rb").exists?
174
279
  return load dir.agent.find_with_extension("rb")
175
280
  end
@@ -180,8 +285,8 @@ module LLM
180
285
 
181
286
  if agent_name
182
287
 
183
- workflow_path = Scout.workflows[agent_name]
184
288
  agent_path = Scout.Agent[agent_name]
289
+ workflow_path = agent_path['workflow.rb'].exists? ? agent_path : Scout.workflows[agent_name]
185
290
  agent_path = Scout.var.Agent[agent_name] unless agent_path.exists?
186
291
  agent_path = Scout.chats.Agent[agent_name] unless agent_path.exists?
187
292
  agent_path = Scout.chats[agent_name] unless agent_path.exists?
@@ -189,11 +294,11 @@ module LLM
189
294
  raise ScoutException, "No agent found with name #{agent_name}" unless workflow_path.exists? || agent_path.exists?
190
295
 
191
296
  @@agent_workflow ||= {}
192
- workflow = @@agent_workflow[agent_name] ||= if workflow_path.exists?
297
+ workflow = @@agent_workflow[agent_name] ||= if agent_path.workflow.find_with_extension("rb").exists?
298
+ Workflow.require_workflow agent_path.workflow.find_with_extension("rb")
299
+ elsif workflow_path.exists?
193
300
  agent_path = workflow_path
194
301
  Workflow.require_workflow agent_name
195
- elsif agent_path.workflow.find_with_extension("rb").exists?
196
- Workflow.require_workflow_file agent_path.workflow.find_with_extension("rb")
197
302
  elsif agent_path.python.exists? && agent_path.python.glob('*.py').any?
198
303
  require 'scout/workflow/python'
199
304
  PythonWorkflow.load_directory agent_path.python, 'ScoutAgent'
@@ -205,27 +310,38 @@ module LLM
205
310
  KnowledgeBase.load workflow_path.knowledge_base.find
206
311
  end
207
312
 
208
- chat = if agent_path.start_chat.exists?
209
- Chat.setup LLM.chat(agent_path.start_chat.find)
210
- elsif workflow_path.start_chat.exists?
211
- Chat.setup LLM.chat(workflow_path.start_chat.find)
212
- elsif agent_path.start_chat.exists?
213
- Chat.setup LLM.chat(agent_path.start_chat.find)
214
- elsif workflow && workflow.documentation[:description]
215
- Chat.setup([ {role: 'introduce', content: workflow.name} ])
216
- end
313
+ chat = nil
314
+ # Whether `chat` came from a real start_chat FILE (deliberate agent
315
+ # preparation) or from the synthetic `introduce` documentation
316
+ # fallback below. Agent#ask gates implicit path-kind loading on
317
+ # this (start_chat_file?): only an unprepared agent loads kinds
318
+ # implicitly.
319
+ start_chat_file = false
320
+ if agent_path.start_chat.exists?
321
+ chat = Chat.setup LLM.chat(agent_path.start_chat.find)
322
+ start_chat_file = true
323
+ elsif workflow_path.start_chat.exists?
324
+ chat = Chat.setup LLM.chat(workflow_path.start_chat.find)
325
+ start_chat_file = true
326
+ elsif workflow && workflow.documentation[:description]
327
+ chat = Chat.setup([ {role: 'introduce', content: workflow.name} ])
328
+ end
217
329
  end
218
330
 
219
331
  agent = LLM::Agent.new **options.merge(workflow: workflow, knowledge_base: knowledge_base, start_chat: chat)
220
332
  agent.path = agent_path.find if agent_path
333
+ agent.start_chat_file = start_chat_file if agent_name
221
334
  agent
222
335
  end
223
336
  end
224
337
  end
225
338
 
226
339
  require_relative 'agent/chat'
340
+ require_relative 'agent/construction'
227
341
  require_relative 'agent/iterate'
342
+ require_relative 'agent/conversation'
228
343
  require_relative 'agent/delegate'
229
344
  require_relative 'agent/attach'
230
345
  require_relative 'agent/save'
231
346
  require_relative 'agent/workflow'
347
+ require_relative 'agent/path'
data/lib/scout/llm/ask.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'scout'
2
2
  require_relative 'chat'
3
+ require_relative 'request_context'
3
4
 
4
5
  module LLM
5
6
 
@@ -10,22 +11,32 @@ module LLM
10
11
  end
11
12
 
12
13
  def self.ask(question, options = {}, &block)
13
- messages = LLM.chat(question)
14
+ options = options.dup
15
+ request_context = RequestContext.capture(options.delete(:request_context), endpoint: options[:endpoint], backend: options[:backend], model: options[:model])
16
+ messages = LLM.chat(question, request_context: request_context)
14
17
  options = IndiferentHash.add_defaults options, LLM.options(messages)
18
+ request_context = RequestContext.capture(request_context, endpoint: options[:endpoint], backend: options[:backend], model: options[:model])
15
19
 
16
20
  agent_name, agent_save_file = IndiferentHash.process_options options, :agent, :agent_save_file
17
21
  agent_name = nil if %(none false nil).include?(agent_name.to_s)
18
22
  if agent_name
19
23
  agent = LLM::Agent.load_agent agent_name
24
+ agent.request_context = request_context
20
25
  agent.save_file = agent_save_file if agent_save_file
21
26
  agent.follow messages
22
- res = agent.chat options
27
+ res = agent.chat options.merge(request_context: request_context)
23
28
  return res
24
29
  end
25
30
 
26
- endpoint, persist = IndiferentHash.process_options options, :endpoint, :persist, persist: true
31
+ endpoint, persist = IndiferentHash.process_options options, :endpoint, :persist
27
32
 
28
- persist ||= Scout::Config.get :persist, :ask, :llm, env: 'ASK_PERSIST,LLM_PERSIST,PERSIST'
33
+ # ScoutCoder: an EXPLICIT persist:false must survive the config merge.
34
+ # `persist ||= config_lookup` is falsy for false, so an explicit opt-out
35
+ # was silently discarded and the round persisted anyway — into the SHARED
36
+ # Scout.var.cache.ask store. Only fill in from config when the caller said
37
+ # nothing (nil). This is what makes test-level `persist: false` hermetic
38
+ # and keeps unit tests out of the account-wide cache.
39
+ persist = Scout::Config.get :persist, :ask, :llm, env: 'ASK_PERSIST,LLM_PERSIST,PERSIST', default: true if persist.nil?
29
40
  endpoint ||= Scout::Config.get :endpoint, :ask, :llm, env: 'ASK_ENDPOINT,LLM_ENDPOINT,ENDPOINT,LLM,ASK'
30
41
  if endpoint && Scout.etc.AI[endpoint].find_with_extension(:yaml).exists?
31
42
  options = IndiferentHash.add_defaults options, Scout.etc.AI[endpoint].yaml
@@ -80,38 +91,38 @@ module LLM
80
91
  case backend
81
92
  when :openai, "openai"
82
93
  require_relative 'backends/openai'
83
- LLM::OpenAI.ask(messages, options, &block)
94
+ LLM::OpenAI.ask(messages, options.merge(request_context: request_context), &block)
84
95
  when :anthropic, "anthropic"
85
96
  require_relative 'backends/anthropic'
86
- LLM::Anthropic.ask(messages, options, &block)
97
+ LLM::Anthropic.ask(messages, options.merge(request_context: request_context), &block)
87
98
  when :responses, "responses"
88
99
  require_relative 'backends/responses'
89
- LLM::Responses.ask(messages, options, &block)
100
+ LLM::Responses.ask(messages, options.merge(request_context: request_context), &block)
90
101
  when :ollama, "ollama"
91
102
  require_relative 'backends/ollama'
92
- LLM::OLlama.ask(messages, options, &block)
103
+ LLM::OLlama.ask(messages, options.merge(request_context: request_context), &block)
93
104
  when :vllm, "vllm"
94
105
  require_relative 'backends/vllm'
95
- LLM::VLLM.ask(messages, options, &block)
106
+ LLM::VLLM.ask(messages, options.merge(request_context: request_context), &block)
96
107
  when :openwebui, "openwebui"
97
108
  require_relative 'backends/openwebui'
98
- LLM::OpenWebUI.ask(messages, options, &block)
109
+ LLM::OpenWebUI.ask(messages, options.merge(request_context: request_context), &block)
99
110
  when :huggingface, "huggingface"
100
111
  require_relative 'backends/huggingface'
101
- LLM::Huggingface.ask(messages, options, &block)
112
+ LLM::Huggingface.ask(messages, options.merge(request_context: request_context), &block)
102
113
  when :relay, "relay"
103
114
  require_relative 'backends/relay'
104
- LLM::Relay.ask(messages, options, &block)
115
+ LLM::Relay.ask(messages, options.merge(request_context: request_context), &block)
105
116
  when :bedrock, "bedrock"
106
117
  require_relative 'backends/bedrock'
107
- LLM::Bedrock.ask(messages, options, &block)
118
+ LLM::Bedrock.ask(messages, options.merge(request_context: request_context), &block)
108
119
  when :glm, "glm"
109
120
  require_relative 'backends/glm'
110
- LLM::GLM.ask(messages, options, &block)
121
+ LLM::GLM.ask(messages, options.merge(request_context: request_context), &block)
111
122
  else
112
123
  mod = BACKENDS[backend]
113
124
  raise "Unknown backend: #{backend}" if mod.nil?
114
- mod.ask(messages, options, &block)
125
+ mod.ask(messages, options.merge(request_context: request_context), &block)
115
126
  end
116
127
  end
117
128
 
@@ -124,8 +135,8 @@ module LLM
124
135
 
125
136
  def self.workflow_ask(workflow, question, options = {})
126
137
  workflow_tools = LLM.workflow_tools(workflow)
127
- self.ask(question, options.merge(tools: workflow_tools)) do |task_name,parameters|
128
- workflow.job(task_name, parameters).run
138
+ self.ask(question, options.merge(tools: workflow_tools)) do |task_name,parameters,request_context|
139
+ LLM.call_workflow(workflow, task_name, parameters, request_context: request_context).run
129
140
  end
130
141
  end
131
142
 
@@ -87,7 +87,7 @@ module LLM
87
87
  next
88
88
  when 'tool_use'
89
89
  tool_call = parse_tool_call(output)
90
- LLM.process_calls(tools, [tool_call], &block)
90
+ LLM.process_calls(tools, [tool_call], save_file: options[:save_file], request_context: options[:request_context], &block)
91
91
  when 'web_search_call'
92
92
  next
93
93
  else
@@ -1,6 +1,7 @@
1
1
  require 'scout'
2
2
  require 'aws-sdk-bedrockruntime'
3
3
  require_relative '../chat'
4
+ require_relative '../request_context'
4
5
 
5
6
  module LLM
6
7
  module Bedrock
@@ -37,7 +38,7 @@ module LLM
37
38
  end
38
39
 
39
40
  def self.ask(question, options = {}, &block)
40
- client, region, access_key, secret_key, type = IndiferentHash.process_options options, :client, :region, :access_key, :secret_key, :type
41
+ client, region, access_key, secret_key, type, request_context = IndiferentHash.process_options options, :client, :region, :access_key, :secret_key, :type, :request_context
41
42
 
42
43
  model_options = IndiferentHash.pull_keys options, :model
43
44
  model = IndiferentHash.process_options model_options, :model
@@ -91,7 +92,7 @@ module LLM
91
92
 
92
93
  cpus = Scout::Config.get :cpus, :tool_calling, default: 3
93
94
  tool_calls.each do |tool_call|
94
- response_message = LLM.tool_response(tool_call, &block)
95
+ response_message = LLM.tool_response(tool_call, request_context: request_context, &block)
95
96
  messages << response_message
96
97
  end
97
98