aia 2.0.0.0.pre.beta2 → 2.0.1

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 (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +152 -223
  3. data/README.md +41 -18
  4. data/bin/aia +8 -3
  5. data/lib/aia/aia_completion.bash +2 -3
  6. data/lib/aia/aia_completion.fish +6 -5
  7. data/lib/aia/aia_completion.zsh +2 -3
  8. data/lib/aia/chat_loop.rb +16 -14
  9. data/lib/aia/config/cli_parser.rb +123 -91
  10. data/lib/aia/config/defaults.yml +18 -40
  11. data/lib/aia/config/mcp_parser.rb +30 -36
  12. data/lib/aia/config/model_spec.rb +8 -14
  13. data/lib/aia/config/validator.rb +102 -103
  14. data/lib/aia/config/value_caster.rb +76 -0
  15. data/lib/aia/config.rb +70 -53
  16. data/lib/aia/crew.rb +29 -19
  17. data/lib/aia/debate_handler.rb +27 -10
  18. data/lib/aia/directives/configuration_directives.rb +55 -14
  19. data/lib/aia/directives/context_directives.rb +24 -12
  20. data/lib/aia/directives/execution_directives.rb +18 -23
  21. data/lib/aia/directives/model_directives.rb +41 -104
  22. data/lib/aia/directives/utility_directives.rb +16 -17
  23. data/lib/aia/directives/web_and_file_directives.rb +21 -14
  24. data/lib/aia/fact_asserter.rb +3 -3
  25. data/lib/aia/history_transfer.rb +64 -24
  26. data/lib/aia/logger.rb +14 -46
  27. data/lib/aia/mcp_config_normalizer.rb +27 -14
  28. data/lib/aia/mcp_connection_manager.rb +181 -184
  29. data/lib/aia/mcp_discovery.rb +43 -15
  30. data/lib/aia/mcp_grouper.rb +2 -1
  31. data/lib/aia/mcp_server_config.rb +20 -2
  32. data/lib/aia/mcp_utility.rb +24 -19
  33. data/lib/aia/mention_router.rb +15 -2
  34. data/lib/aia/modality_query.rb +36 -0
  35. data/lib/aia/model_alias_registry.rb +11 -11
  36. data/lib/aia/model_registry.rb +75 -0
  37. data/lib/aia/network_builder.rb +8 -7
  38. data/lib/aia/patches/mcp_stdio_stderr.rb +102 -0
  39. data/lib/aia/patches/ruby_llm_streaming_error.rb +21 -4
  40. data/lib/aia/patches/ruby_llm_tool_param_compat.rb +18 -0
  41. data/lib/aia/pipeline_orchestrator.rb +71 -38
  42. data/lib/aia/prompt_decomposer.rb +1 -1
  43. data/lib/aia/prompt_handler.rb +56 -29
  44. data/lib/aia/provider_registry.rb +105 -0
  45. data/lib/aia/robot_builder.rb +1 -1
  46. data/lib/aia/robot_factory.rb +67 -63
  47. data/lib/aia/robot_sync.rb +79 -0
  48. data/lib/aia/role_catalog.rb +89 -0
  49. data/lib/aia/session.rb +25 -11
  50. data/lib/aia/skill_utils.rb +60 -19
  51. data/lib/aia/spawn_handler.rb +16 -4
  52. data/lib/aia/spawn_spec_parser.rb +6 -7
  53. data/lib/aia/special_mode_handler.rb +28 -25
  54. data/lib/aia/speech.rb +70 -34
  55. data/lib/aia/startup_coordinator.rb +17 -92
  56. data/lib/aia/streaming_runner.rb +10 -2
  57. data/lib/aia/system_prompt_assembler.rb +12 -11
  58. data/lib/aia/task_board.rb +60 -0
  59. data/lib/aia/tool_approver.rb +205 -0
  60. data/lib/aia/tool_filter.rb +1 -10
  61. data/lib/aia/tool_filter_strategy.rb +12 -1
  62. data/lib/aia/tool_introspection.rb +9 -1
  63. data/lib/aia/tool_loader.rb +67 -33
  64. data/lib/aia/tool_utility.rb +6 -5
  65. data/lib/aia/tools/delegate_to_foreman_tool.rb +6 -4
  66. data/lib/aia/tools/recruit_robot_tool.rb +12 -12
  67. data/lib/aia/tools/reskill_robot_tool.rb +7 -7
  68. data/lib/aia/tools/task_board_tool.rb +16 -16
  69. data/lib/aia/trakflow_bridge.rb +14 -8
  70. data/lib/aia/turn_state.rb +31 -8
  71. data/lib/aia/ui_presenter.rb +16 -0
  72. data/lib/aia/utility.rb +6 -24
  73. data/lib/aia/variable_input_collector.rb +0 -3
  74. data/lib/aia/verification_network.rb +9 -10
  75. data/lib/aia/version.rb +1 -9
  76. data/lib/aia.rb +34 -8
  77. metadata +20 -161
  78. data/.better-commits.json +0 -150
  79. data/.config/tocer/configuration.yml +0 -6
  80. data/.envrc +0 -19
  81. data/.loki +0 -11
  82. data/.reek.yml +0 -160
  83. data/.rubocop.yml +0 -116
  84. data/.rubocop_strict.yml +0 -15
  85. data/.version +0 -1
  86. data/COMMITS.md +0 -196
  87. data/IMPLEMENTATION_PLAN.md +0 -506
  88. data/Rakefile +0 -47
  89. data/_typos.toml +0 -10
  90. data/aia.gemspec +0 -92
  91. data/architecture_review.md +0 -314
  92. data/config/aia.yml +0 -13
  93. data/docs/AGENTS.md +0 -40
  94. data/docs/advanced-prompting.md +0 -814
  95. data/docs/assets/images/aia.png +0 -0
  96. data/docs/cli-reference.md +0 -1241
  97. data/docs/configuration.md +0 -747
  98. data/docs/contributing.md +0 -386
  99. data/docs/directives-reference.md +0 -1145
  100. data/docs/examples/index.md +0 -277
  101. data/docs/examples/mcp/index.md +0 -475
  102. data/docs/examples/prompts/analysis/index.md +0 -78
  103. data/docs/examples/prompts/automation/index.md +0 -109
  104. data/docs/examples/prompts/development/index.md +0 -125
  105. data/docs/examples/prompts/index.md +0 -333
  106. data/docs/examples/prompts/learning/index.md +0 -127
  107. data/docs/examples/prompts/writing/index.md +0 -62
  108. data/docs/examples/tools/index.md +0 -282
  109. data/docs/faq.md +0 -578
  110. data/docs/guides/available-models.md +0 -366
  111. data/docs/guides/basic-usage.md +0 -478
  112. data/docs/guides/chat.md +0 -680
  113. data/docs/guides/crew.md +0 -239
  114. data/docs/guides/executable-prompts.md +0 -331
  115. data/docs/guides/first-prompt.md +0 -454
  116. data/docs/guides/getting-started.md +0 -455
  117. data/docs/guides/image-generation.md +0 -507
  118. data/docs/guides/index.md +0 -48
  119. data/docs/guides/local-models.md +0 -306
  120. data/docs/guides/migrate-prompts.md +0 -335
  121. data/docs/guides/models.md +0 -1017
  122. data/docs/guides/shared-tools.md +0 -254
  123. data/docs/guides/tools.md +0 -831
  124. data/docs/index.md +0 -259
  125. data/docs/installation.md +0 -274
  126. data/docs/mcp-integration.md +0 -603
  127. data/docs/prompt_management.md +0 -665
  128. data/docs/security.md +0 -674
  129. data/docs/special_projects_guide.md +0 -386
  130. data/docs/tools-and-mcp-examples.md +0 -1211
  131. data/docs/workflows-and-pipelines.md +0 -636
  132. data/examples/.gitignore +0 -2
  133. data/examples/00_setup_aia.sh +0 -111
  134. data/examples/01_basic_usage.sh +0 -23
  135. data/examples/02_frontmatter.sh +0 -33
  136. data/examples/03_shell_integration.sh +0 -55
  137. data/examples/04_erb_templating.sh +0 -56
  138. data/examples/05_shell_then_erb.sh +0 -38
  139. data/examples/06_prompt_chaining.sh +0 -59
  140. data/examples/07_pipeline.sh +0 -46
  141. data/examples/08_context_files.sh +0 -43
  142. data/examples/09_roles.sh +0 -67
  143. data/examples/10_stdin_piping.sh +0 -49
  144. data/examples/11_multi_model.sh +0 -65
  145. data/examples/12_token_usage.sh +0 -52
  146. data/examples/13_cost_tracking.sh +0 -68
  147. data/examples/14_output_file.sh +0 -91
  148. data/examples/15_parameters.sh +0 -68
  149. data/examples/16_directives.sh +0 -113
  150. data/examples/17_require_and_conditionals.sh +0 -68
  151. data/examples/18_tools.sh +0 -61
  152. data/examples/19_local_tools.sh +0 -34
  153. data/examples/20_mcp_servers.sh +0 -102
  154. data/examples/21_executable_prompts.sh +0 -78
  155. data/examples/22_chat_mode.sh +0 -217
  156. data/examples/23_verify.sh +0 -139
  157. data/examples/24_decompose.sh +0 -139
  158. data/examples/25_spawn.sh +0 -139
  159. data/examples/26_debate.sh +0 -97
  160. data/examples/27_mention_routing.sh +0 -157
  161. data/examples/28_model_switching.sh +0 -106
  162. data/examples/29_agent_harness.sh +0 -177
  163. data/examples/README.md +0 -321
  164. data/examples/advanced_multi_robot_capabilities_without_examples.md +0 -106
  165. data/examples/aia_config.yml +0 -22
  166. data/examples/aia_config_orchestrator.yml +0 -45
  167. data/examples/aia_config_with_mcp.yml +0 -21
  168. data/examples/common.sh +0 -39
  169. data/examples/context/project_goals.md +0 -6
  170. data/examples/context/tech_stack.md +0 -7
  171. data/examples/directives/timestamp_directive.rb +0 -22
  172. data/examples/mcp/filesystem.json +0 -9
  173. data/examples/prompts_dir/analyze.md +0 -2
  174. data/examples/prompts_dir/brainstorm.md +0 -2
  175. data/examples/prompts_dir/conditional.md +0 -17
  176. data/examples/prompts_dir/creative.md +0 -4
  177. data/examples/prompts_dir/erbinfo.md +0 -5
  178. data/examples/prompts_dir/erbinfo_raw.md +0 -8
  179. data/examples/prompts_dir/evaluate.md +0 -2
  180. data/examples/prompts_dir/explain_joke.md +0 -1
  181. data/examples/prompts_dir/explain_recursion.md +0 -1
  182. data/examples/prompts_dir/fun_fact +0 -3
  183. data/examples/prompts_dir/greeting.md +0 -8
  184. data/examples/prompts_dir/greeting_required.md +0 -7
  185. data/examples/prompts_dir/hello.md +0 -1
  186. data/examples/prompts_dir/includes/ruby_facts.md +0 -4
  187. data/examples/prompts_dir/limerick.md +0 -1
  188. data/examples/prompts_dir/list_with_mcp.md +0 -3
  189. data/examples/prompts_dir/pick_best.md +0 -2
  190. data/examples/prompts_dir/project_summary +0 -12
  191. data/examples/prompts_dir/quiz_with_include.md +0 -6
  192. data/examples/prompts_dir/require_demo.md +0 -5
  193. data/examples/prompts_dir/roles/formal.md +0 -3
  194. data/examples/prompts_dir/roles/orchestrator.md +0 -21
  195. data/examples/prompts_dir/roles/pirate.md +0 -3
  196. data/examples/prompts_dir/shell_then_erb.md +0 -6
  197. data/examples/prompts_dir/sort_compare.md +0 -2
  198. data/examples/prompts_dir/summarize_context.md +0 -2
  199. data/examples/prompts_dir/sysinfo.md +0 -5
  200. data/examples/prompts_dir/sysinfo_raw.md +0 -8
  201. data/examples/prompts_dir/tagline.md +0 -1
  202. data/examples/prompts_dir/tell_joke.md +0 -1
  203. data/examples/prompts_dir/tell_joke_with_next.md +0 -4
  204. data/examples/prompts_dir/use_local_tool.md +0 -8
  205. data/examples/prompts_dir/use_tools.md +0 -8
  206. data/examples/prompts_dir/weather.md +0 -2
  207. data/examples/prompts_dir/whats_new.md +0 -2
  208. data/examples/prompts_dir/with_timestamp.md +0 -5
  209. data/examples/requirements/sinatra_taskflow_app.md +0 -139
  210. data/examples/rules/01_classify_ruby.rb +0 -16
  211. data/examples/rules/02_prefer_claude_for_code.rb +0 -19
  212. data/examples/rules/03_gate_prompt_length.rb +0 -19
  213. data/examples/rules/04_tool_selection.rb +0 -41
  214. data/examples/rules/README.md +0 -30
  215. data/examples/run_all.sh +0 -294
  216. data/examples/tools/word_count_tool.rb +0 -30
  217. data/lib/AGENTS.md +0 -57
  218. data/lib/aia/model_switch_handler.rb +0 -100
  219. data/mcp_servers/README.md +0 -110
  220. data/mcp_servers/apple-mcp.json +0 -8
  221. data/mcp_servers/filesystem.json +0 -9
  222. data/mcp_servers/mcp_server_chart.json +0 -11
  223. data/mcp_servers/playwright_one.json +0 -8
  224. data/mcp_servers/playwright_server_definition.json +0 -9
  225. data/mcp_servers/playwright_two.json +0 -8
  226. data/mcp_servers/tavily_mcp_server.json +0 -8
  227. data/mkdocs.yml +0 -169
@@ -2,9 +2,11 @@
2
2
 
3
3
  # lib/aia/startup_coordinator.rb
4
4
  #
5
- # Handles all session startup tasks: MCP connection, tool loading,
6
- # filter initialization, task coordination, and bus attachment.
7
- # Extracted from Session to give Session a single responsibility.
5
+ # Handles the session startup tasks that need the built robot: tool loading,
6
+ # filter initialization, task coordination, and bus attachment. MCP servers
7
+ # are connected before the robot is built (MCPConnectionManager.start) so
8
+ # every robot is handed the live servers. Extracted from Session to give
9
+ # Session a single responsibility.
8
10
 
9
11
  require "fileutils"
10
12
  require "tty-spinner"
@@ -12,20 +14,19 @@ require "tty-spinner"
12
14
  module AIA
13
15
  class StartupCoordinator
14
16
  attr_reader :filters, :mcp_manager
17
+ attr_writer :robot
15
18
 
16
- def initialize(robot:, ui_presenter:)
19
+ def initialize(robot:, ui_presenter:, mcp_manager: nil)
17
20
  @robot = robot
18
21
  @ui = ui_presenter
19
22
  @filters = {}
20
- @mcp_manager = nil
23
+ @mcp_manager = mcp_manager
21
24
  end
22
25
 
23
26
  # Run all startup coordination tasks.
24
27
  #
25
28
  # @param config [AIA::Config]
26
29
  def run(config)
27
- connect_mcp_servers(config)
28
-
29
30
  tools = all_available_tools(config)
30
31
 
31
32
  spinner = TTY::Spinner.new(" [:spinner] Initializing tool filters...", format: :dots)
@@ -33,80 +34,20 @@ module AIA
33
34
  @filters = ToolFilterRegistry.build_from_config(config, tools)
34
35
  spinner.success
35
36
 
36
- initialize_task_coordinator if trakflow_available?
37
-
38
37
  attach_bus_if_network
39
38
  end
40
39
 
41
- private
42
-
43
- # Connect ALL configured MCP servers eagerly at startup, then absorb any
44
- # MCP clients registered via --require (e.g., shared_tools/mcp/* files).
45
- # Uses MCPDiscovery to determine which servers to connect (respects
46
- # --mcp-use, --mcp-skip, and KBS mcp_activate decisions), then normalizes
47
- # each server config via MCPConfigNormalizer before handing off to
48
- # MCPConnectionManager.
49
- # :reek:TooManyStatements -- MCP wiring sequence: discovery, validation, normalize fallback, connect, absorb, inject
50
- def connect_mcp_servers(config)
51
- return if config.flags.no_mcp
52
-
53
- discovered = MCPDiscovery.new.discover(config)
54
- validate_mcp_use_names(config, discovered) if Array(config.mcp_use).any?
55
-
56
- # Prefer the robot's pre-built mcp_config (already normalized) when
57
- # present, but always apply --mcp-skip so that flag is honoured.
58
- # Fall back to the MCPDiscovery result (which includes both filters).
59
- servers = if @robot.respond_to?(:mcp_config) && @robot.mcp_config.is_a?(Array)
60
- skip_list = Array(config.mcp_skip)
61
- @robot.mcp_config.reject { |s| skip_list.include?(AIA::Utility.server_name(s)) }
62
- else
63
- discovered.map { |s| MCPConfigNormalizer.normalize(s) }
64
- end
40
+ # Load the task-board tools. Call this before the robot is built, since
41
+ # the robot collects its tools when it is built. The board itself
42
+ # (TaskBoard) starts only when something records a task.
43
+ def prepare_task_board
44
+ return unless TaskBoard.gem_available?
65
45
 
66
- @mcp_manager = MCPConnectionManager.new
67
- @mcp_manager.connect_all(servers) if servers.is_a?(Array) && servers.any?
68
- @mcp_manager.absorb_ruby_llm_mcp_clients
69
- return unless @mcp_manager.connected?
70
-
71
- @mcp_manager.update_config
72
- @mcp_manager.inject_into(@robot) if @mcp_manager.any_tools?
73
- end
74
-
75
- # Initialize TaskCoordinator, auto-creating the TrakFlow project if needed.
76
- def initialize_task_coordinator
77
- require_relative 'task_coordinator'
78
- require_relative 'trakflow_bridge'
79
46
  require_relative 'tools/task_board_tool'
80
47
  require_relative 'tools/delegate_to_foreman_tool'
81
-
82
- ensure_trakflow_initialized unless TrakFlow.initialized?
83
-
84
- AIA.task_coordinator = TaskCoordinator.new
85
- rescue StandardError => e
86
- AIA.debug_warn("TaskCoordinator initialization failed (non-fatal): #{e.message}", exc: e)
87
48
  end
88
49
 
89
- # Return true if the trak_flow gem is available; false if it cannot be loaded.
90
- def trakflow_available?
91
- defined?(TrakFlow) || begin
92
- require 'trak_flow'
93
- true
94
- rescue LoadError
95
- false
96
- end
97
- end
98
-
99
- # Bootstrap a TrakFlow project so the task board is always available.
100
- # Uses the default database path (~/.config/trak_flow/tf.db).
101
- def ensure_trakflow_initialized
102
- trakflow_dir = TrakFlow.trak_flow_dir
103
- FileUtils.mkdir_p(trakflow_dir)
104
-
105
- db = TrakFlow::Storage::Database.new
106
- db.connect
107
-
108
- TrakFlow.reset_root!
109
- end
50
+ private
110
51
 
111
52
  # Attach a shared TypedBus to multi-model networks.
112
53
  def attach_bus_if_network
@@ -117,20 +58,6 @@ module AIA
117
58
  AIA.debug_warn("Bus attachment failed (non-fatal): #{e.message}", exc: e)
118
59
  end
119
60
 
120
- # Warn when --mcp-use names don't match any configured server.
121
- # Helps users catch typos before a silent empty-tools session.
122
- def validate_mcp_use_names(config, discovered_servers)
123
- requested = Array(config.mcp_use)
124
- available = Array(config.mcp_servers).map { |s| AIA::Utility.server_name(s) }
125
- found = discovered_servers.map { |s| AIA::Utility.server_name(s) }
126
- missing = requested - found
127
-
128
- return if missing.empty?
129
-
130
- $stderr.puts "WARNING: --mcp-use specified server(s) not found in config: #{missing.join(', ')}"
131
- $stderr.puts " Available servers: #{available.join(', ')}" if available.any?
132
- end
133
-
134
61
  # Collect all tools available to the robot: local + MCP.
135
62
  def all_available_tools(config)
136
63
  local = Array(config.loaded_tools)
@@ -138,13 +65,11 @@ module AIA
138
65
  local + mcp
139
66
  end
140
67
 
141
- # Retrieve MCP tools from the connection manager.
142
- # MCPConnectionManager builds RobotLab::Tool instances via connect_one and
143
- # absorb_ruby_llm_mcp_clients — those are what get injected into the robot
144
- # and what robot_lab filters by name. RubyLLM::MCP.clients is a different
145
- # registry and does not contain the tools connected here.
68
+ # MCP tools (RubyLLM::MCP::Tool) from the connection manager — the
69
+ # servers every robot is built with.
146
70
  def collect_mcp_tools
147
71
  return [] unless @mcp_manager
72
+
148
73
  @mcp_manager.connected_tools
149
74
  end
150
75
  end
@@ -65,7 +65,7 @@ module AIA
65
65
  tools_param = resolve_tools_param(tools, robot)
66
66
 
67
67
  begin
68
- result = robot.run(prompt, mcp: :inherit, tools: tools_param, &streaming_block)
68
+ result = with_approval_spinner { robot.run(prompt, mcp: :inherit, tools: tools_param, &streaming_block) }
69
69
  rescue Exception # rubocop:disable Lint/RescueException
70
70
  @spinner.stop unless header_printed
71
71
  raise
@@ -79,6 +79,14 @@ module AIA
79
79
 
80
80
  private
81
81
 
82
+ # Let ToolApprover pause this turn's spinner while an approval prompt is on screen.
83
+ def with_approval_spinner
84
+ ToolApprover.spinner = @spinner
85
+ yield
86
+ ensure
87
+ ToolApprover.spinner = nil
88
+ end
89
+
82
90
  # Translate the resolved tool list into robot_lab's ToolConfig vocabulary,
83
91
  # then clamp it to the provider's limit:
84
92
  # nil -> :inherit (no filter active / filter errored — use the full set)
@@ -132,7 +140,7 @@ module AIA
132
140
  def robot_tool_names(robot)
133
141
  local = robot.respond_to?(:local_tools) ? Array(robot.local_tools) : []
134
142
  mcp = robot.respond_to?(:mcp_tools) ? Array(robot.mcp_tools) : []
135
- (local + mcp).map { |t| t.respond_to?(:name) ? t.name : t.class.name }
143
+ (local + mcp).map { |t| ToolIntrospection.tool_name(t) }
136
144
  end
137
145
 
138
146
  def show_thinking?
@@ -6,7 +6,9 @@
6
6
  # Extracted from RobotFactory to isolate the prompt assembly concern.
7
7
  # Completely stateless — no module ivars.
8
8
 
9
+ require 'pm'
9
10
  require_relative 'skill_utils'
11
+ require_relative 'role_catalog'
10
12
 
11
13
  module AIA
12
14
  module SystemPromptAssembler
@@ -68,22 +70,21 @@ module AIA
68
70
  lines.join("\n")
69
71
  end
70
72
 
71
- # Load role file content.
73
+ # Load a role for the system prompt. Named roles come from the roles
74
+ # folder of the prompts directory; path-based roles from anywhere. The
75
+ # file is processed like a prompt: front matter removed, shell and ERB
76
+ # expanded.
72
77
  #
73
78
  # @param config [AIA::Config] the AIA configuration
74
- # @param role_id [String] the role identifier
75
- # @return [String, nil] the role file content or nil
79
+ # @param role_id [String] the role identifier or path
80
+ # @return [String, nil] the rendered role, or nil if it is missing or fails
76
81
  def load_role_content(config, role_id)
77
- prompts_cfg = config.prompts
78
- roles_prefix = prompts_cfg.roles_prefix
79
- unless role_id.start_with?(roles_prefix)
80
- role_id = "#{roles_prefix}/#{role_id}"
81
- end
82
-
83
- role_file = File.join(prompts_cfg.dir, "#{role_id}#{prompts_cfg.extname}")
82
+ prompts_cfg = config.prompts
83
+ role_file = AIA::RoleCatalog.role_file(role_id, dir: prompts_cfg.dir, prefix: prompts_cfg.roles_prefix,
84
+ extname: prompts_cfg.extname)
84
85
  return nil unless File.exist?(role_file)
85
86
 
86
- File.read(role_file)
87
+ PM.parse(Pathname.new(role_file)).to_s
87
88
  rescue => e
88
89
  $stderr.puts "Warning: Could not load role '#{role_id}': #{e.message}"
89
90
  nil
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/aia/task_board.rb
4
+ #
5
+ # The TrakFlow task board, started on first use. Nothing touches the disk
6
+ # until a feature that records tasks (/task, /delegate, /spawn tracking,
7
+ # --track-pipeline, the task-board tools) actually needs it, so a plain
8
+ # session leaves no .trak_flow directory behind.
9
+
10
+ require 'fileutils'
11
+
12
+ module AIA
13
+ module TaskBoard
14
+ module_function
15
+
16
+ # Whether the trak_flow gem can be loaded.
17
+ #
18
+ # @return [Boolean]
19
+ def gem_available?
20
+ return true if defined?(TrakFlow)
21
+
22
+ require 'trak_flow'
23
+ true
24
+ rescue LoadError
25
+ false
26
+ end
27
+
28
+ # Make sure a TrakFlow project exists, creating .trak_flow in the current
29
+ # directory when none is found here or in a parent. The tasks themselves
30
+ # live in TrakFlow's database (~/.config/trak_flow/tf.db by default).
31
+ #
32
+ # @return [Boolean] true when the project is ready
33
+ def ensure_project!
34
+ return false unless gem_available?
35
+ return true if TrakFlow.initialized?
36
+
37
+ FileUtils.mkdir_p(TrakFlow.trak_flow_dir)
38
+ TrakFlow::Storage::Database.new.connect
39
+ TrakFlow.reset_root!
40
+ TrakFlow.initialized?
41
+ rescue StandardError => e
42
+ AIA.debug_warn("TrakFlow initialization failed (non-fatal): #{e.message}", exc: e)
43
+ false
44
+ end
45
+
46
+ # A TaskCoordinator on a ready project, or nil when TrakFlow can't run.
47
+ #
48
+ # @return [AIA::TaskCoordinator, nil]
49
+ def coordinator
50
+ return nil unless ensure_project!
51
+
52
+ require_relative 'trakflow_bridge'
53
+ require_relative 'task_coordinator'
54
+ TaskCoordinator.new
55
+ rescue StandardError => e
56
+ AIA.debug_warn("TaskCoordinator initialization failed (non-fatal): #{e.message}", exc: e)
57
+ nil
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/aia/tool_approver.rb
4
+ #
5
+ # Settles what robot_lab pauses a run for: tool calls that require approval
6
+ # (an MCP server's `requires_approval:`, or a RubyLLM::Tool that declares
7
+ # it) and MCP input requests (elicitation: a server asking the user for
8
+ # values, or to visit a URL).
9
+ #
10
+ # The mode comes from `tools.approval` (CLI: --tool-approval, env:
11
+ # AIA_TOOLS__APPROVAL):
12
+ #
13
+ # ask -- prompt on the terminal; deny when there is no terminal (default)
14
+ # approve -- allow every tool call that requires approval
15
+ # deny -- refuse them all and decline every input request
16
+ #
17
+ # Prompts go to $stderr so they never mix with the AI's answer on $stdout,
18
+ # and the turn's spinner is paused while one is shown.
19
+
20
+ module AIA
21
+ class ToolApprover
22
+ MODES = %w[ask approve deny].freeze
23
+ DEFAULT_MODE = 'ask'
24
+ YES = %w[y yes].freeze
25
+
26
+ # One prompt at a time: robots in a parallel network run on their own threads.
27
+ PROMPT_LOCK = Mutex.new
28
+
29
+ class << self
30
+ # The spinner of the turn in progress (set by StreamingRunner), paused
31
+ # while a prompt is on screen.
32
+ attr_accessor :spinner
33
+
34
+ # The approval settings for RobotLab.build / RobotLab::RunConfig.
35
+ #
36
+ # @param config [AIA::Config]
37
+ # @return [Hash] tool_approval:, plus on_tool_approval: / on_input_request: when they apply
38
+ def options(config)
39
+ new(mode: mode_for(config)).options
40
+ end
41
+
42
+ # @param config [AIA::Config]
43
+ # @return [String] one of MODES
44
+ def mode_for(config)
45
+ tools = config.tools if config.respond_to?(:tools)
46
+ mode = (tools.approval if tools.respond_to?(:approval)).to_s.strip.downcase
47
+ mode = DEFAULT_MODE if mode.empty?
48
+ return mode if MODES.include?(mode)
49
+
50
+ AIA.debug_warn("Unknown tools.approval '#{mode}' (expected #{MODES.join(', ')}); using '#{DEFAULT_MODE}'")
51
+ DEFAULT_MODE
52
+ end
53
+ end
54
+
55
+ attr_reader :mode
56
+
57
+ # @param mode [String] one of MODES
58
+ # @param input [IO] where answers are read from
59
+ # @param output [IO] where prompts are written
60
+ # @param interactive [Boolean, nil] whether a person can answer; defaults to input.tty?
61
+ def initialize(mode: DEFAULT_MODE, input: $stdin, output: $stderr, interactive: nil)
62
+ @mode = mode.to_s
63
+ @input = input
64
+ @output = output
65
+ @interactive = interactive
66
+ end
67
+
68
+ # @return [Hash] the robot_lab settings for this mode
69
+ def options
70
+ case mode
71
+ when 'approve' then { tool_approval: :approve, on_input_request: method(:answer_input) }
72
+ when 'deny' then { tool_approval: :deny }
73
+ else { tool_approval: :deny, on_tool_approval: method(:approve?), on_input_request: method(:answer_input) }
74
+ end
75
+ end
76
+
77
+ # Ask whether a tool call that requires approval may run.
78
+ #
79
+ # @param tool_call [RubyLLM::ToolCall]
80
+ # @return [Boolean]
81
+ def approve?(tool_call)
82
+ return refuse("tool '#{tool_call.name}' requires approval and there is no terminal to ask") unless interactive?
83
+
84
+ prompting do
85
+ @output.puts "\nTool '#{tool_call.name}' requires approval."
86
+ @output.puts " arguments: #{format_arguments(tool_call.arguments)}"
87
+ YES.include?(ask(' Allow it? [y/N] ').downcase)
88
+ end
89
+ end
90
+
91
+ # Answer an MCP server's request for input.
92
+ #
93
+ # @param request [RubyLLM::MCP::InputRequest]
94
+ # @return [Hash, true, nil] field values, true to accept a URL request, nil to decline
95
+ def answer_input(request)
96
+ return refuse("an MCP server asked for input (#{request.message}) and there is no terminal to ask") unless interactive?
97
+
98
+ prompting do
99
+ @output.puts "\nAn MCP server needs your input: #{request.message}"
100
+ request.url? ? answer_url(request) : answer_form(request)
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def interactive?
107
+ @interactive.nil? ? @input.respond_to?(:tty?) && @input.tty? : @interactive
108
+ end
109
+
110
+ def refuse(reason)
111
+ AIA.logger&.warn("Tool approval: #{reason}; refusing")
112
+ @output.puts "[AIA] Refused: #{reason}. Set tools.approval (--tool-approval) to approve or deny."
113
+ nil
114
+ end
115
+
116
+ # Pause the spinner and hold the prompt lock while a question is on screen.
117
+ def prompting
118
+ PROMPT_LOCK.synchronize do
119
+ spinner = self.class.spinner
120
+ was_spinning = spinner&.spinning?
121
+ spinner&.pause if was_spinning
122
+ begin
123
+ yield
124
+ ensure
125
+ spinner&.resume if was_spinning
126
+ end
127
+ end
128
+ end
129
+
130
+ def ask(question)
131
+ @output.print question
132
+ @output.flush if @output.respond_to?(:flush)
133
+ @input.gets.to_s.strip
134
+ end
135
+
136
+ def answer_url(request)
137
+ @output.puts " Open this page: #{request.url}"
138
+ true if YES.include?(ask(' Continue once you have visited it? [y/N] ').downcase)
139
+ end
140
+
141
+ # Each field is asked in turn. A blank answer keeps the server's default;
142
+ # a blank answer to a required field without one declines the request.
143
+ def answer_form(request)
144
+ request.fields.each_with_object({}) do |field, values|
145
+ value = ask_field(field)
146
+ return nil if value == :decline
147
+
148
+ values[field.name] = value unless value.nil?
149
+ end
150
+ end
151
+
152
+ # @return [Object, nil, :decline] the typed value, nil to keep the default, :decline to give up
153
+ def ask_field(field)
154
+ loop do
155
+ raw = ask(field_question(field))
156
+ return blank_answer(field) if raw.empty?
157
+
158
+ value = cast(raw, field)
159
+ return value unless value.nil?
160
+
161
+ @output.puts " '#{raw}' is not a valid #{field.choices ? "choice (#{field.choices.join(', ')})" : field.type}."
162
+ end
163
+ end
164
+
165
+ def blank_answer(field)
166
+ field.required? && field.default.nil? ? :decline : nil
167
+ end
168
+
169
+ def field_question(field)
170
+ label = field.title || field.name.to_s
171
+ parts = [" #{label}"]
172
+ parts << "(#{field.description})" if field.description
173
+ parts << "[#{field.choices.join('/')}]" if field.choices
174
+ parts << "default: #{field.default}" unless field.default.nil?
175
+ parts << '*' if field.required?
176
+ "#{parts.join(' ')}: "
177
+ end
178
+
179
+ # @return [Object, nil] nil when +raw+ does not fit the field
180
+ def cast(raw, field)
181
+ choices = field.choices
182
+ return choices.find { it.to_s == raw } if choices
183
+
184
+ case field.type
185
+ when 'integer' then Integer(raw, exception: false)
186
+ when 'number' then Float(raw, exception: false)
187
+ when 'boolean' then boolean(raw)
188
+ else raw
189
+ end
190
+ end
191
+
192
+ def boolean(raw)
193
+ return true if %w[y yes true 1].include?(raw.downcase)
194
+ return false if %w[n no false 0].include?(raw.downcase)
195
+
196
+ nil
197
+ end
198
+
199
+ def format_arguments(arguments)
200
+ text = arguments.to_h.map { |key, value| "#{key}=#{value.inspect}" }.join(', ')
201
+ text = '(none)' if text.empty?
202
+ text.length > 300 ? "#{text[0, 300]}..." : text
203
+ end
204
+ end
205
+ end
@@ -13,12 +13,8 @@ module AIA
13
13
  class ToolFilter
14
14
  attr_reader :tool_count, :prep_ms, :label
15
15
 
16
- # :reek:BooleanParameter -- load_db:/save_db: toggle embedding-cache persistence per strategy; mode objects would multiply constructors
17
- def initialize(label:, db_dir: nil, load_db: false, save_db: false)
16
+ def initialize(label:)
18
17
  @label = label
19
- @db_dir = db_dir
20
- @load_db = load_db
21
- @save_db = save_db
22
18
  @tool_count = 0
23
19
  @prep_ms = 0.0
24
20
  end
@@ -49,11 +45,6 @@ module AIA
49
45
  @tool_count.positive?
50
46
  end
51
47
 
52
- # True when this filter supports database persistence (--load / --save).
53
- def persistable?
54
- false
55
- end
56
-
57
48
  protected
58
49
 
59
50
  def do_prep
@@ -19,6 +19,17 @@ module AIA
19
19
 
20
20
  DEFAULT_TIMEOUT_S = 10
21
21
 
22
+ # The per-filter timeout from config (tool_filter.timeout_s), or the default
23
+ # when it is unset or not a positive number.
24
+ #
25
+ # @param config [#tool_filter]
26
+ # @return [Numeric]
27
+ def self.timeout_from(config)
28
+ section = config.respond_to?(:tool_filter) ? config.tool_filter : nil
29
+ value = section.respond_to?(:timeout_s) ? section.timeout_s : nil
30
+ value.is_a?(Numeric) && value.positive? ? value : DEFAULT_TIMEOUT_S
31
+ end
32
+
22
33
  # @param filters [Hash{Symbol => ToolFilter}] e.g. {tfidf: tfidf_filter}
23
34
  # @param ui_presenter [UIPresenter, nil] for display (unused currently, reserved)
24
35
  # @param timeout_s [Numeric] per-filter wall-clock timeout in seconds
@@ -95,7 +106,7 @@ module AIA
95
106
  logger = AIA.logger
96
107
 
97
108
  if scored.empty?
98
- logger.debug "[#{label}] No tools matched (all tools available)"
109
+ logger.debug "[#{label}] No tools matched (no tools sent this turn)"
99
110
  return
100
111
  end
101
112
 
@@ -6,8 +6,16 @@ module AIA
6
6
  module ToolIntrospection
7
7
  module_function
8
8
 
9
+ # The name the model calls the tool by. A RubyLLM::Tool class answers
10
+ # with its +tool_name+ ("dns"), not its Ruby constant name
11
+ # ("SharedTools::Tools::DnsTool"): robot_lab instantiates tool classes,
12
+ # so allowlists and per-turn tool filters must use that name to match.
9
13
  def tool_name(tool)
10
- tool.respond_to?(:name) ? tool.name : tool.class.name
14
+ if tool.is_a?(Class)
15
+ tool.respond_to?(:tool_name) ? tool.tool_name : tool.name
16
+ else
17
+ tool.respond_to?(:name) ? tool.name : tool.class.name
18
+ end
11
19
  end
12
20
 
13
21
  def tool_description(tool)