rails_console_ai 0.38.0 → 0.39.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +6 -3
- data/lib/rails_console_ai/agent_runner.rb +11 -3
- data/lib/rails_console_ai/conversation_engine.rb +189 -102
- data/lib/rails_console_ai/guard_state.rb +160 -0
- data/lib/rails_console_ai/providers/anthropic.rb +29 -1
- data/lib/rails_console_ai/providers/base.rb +8 -1
- data/lib/rails_console_ai/providers/bedrock.rb +49 -1
- data/lib/rails_console_ai/safety_guards.rb +109 -79
- data/lib/rails_console_ai/slack_bot.rb +3 -3
- data/lib/rails_console_ai/sql_writes.rb +188 -0
- data/lib/rails_console_ai/tools/registry.rb +9 -4
- data/lib/rails_console_ai/version.rb +1 -1
- data/lib/rails_console_ai.rb +16 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1276d74ba6eeea883ccf48b17263043162178abc0cd2debd6f8856091583d315
|
|
4
|
+
data.tar.gz: 1bb29fe7d940d3e97dc3e0438b6df916aca22d31dbf20adfd782d48ef681c98d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 54a2eb0cfe46b38e881d8c9b3981db75060302b83173213a1d191c91e25c07a6c87b08fb6d5adbb9b09ebac254d742b0606b2acd886f2c3b28bfbe636c637729
|
|
7
|
+
data.tar.gz: 5926071096ec5a3f28014658703b808bb5717b4a8494de2a7ce017e58b33e91cdaddc9fa869c80cc26faa919fb0d717eb3064a0e1d120036f6cb43740c3b6134
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [0.39.0]
|
|
6
|
+
|
|
7
|
+
- Keep safety guards active in threads and fibers spawned by model code
|
|
8
|
+
- Detect database writes hidden behind comments, CTEs or multiple statements
|
|
9
|
+
- Fix write blocking for bulk inserts and select methods on Rails 7.1+
|
|
10
|
+
- Preserve model thinking across tool calls
|
|
11
|
+
- Add a skills option to run_agent for background runs
|
|
12
|
+
- Show tool calls in logged background run sessions
|
|
13
|
+
- Fix model code seeing internal variables in background runs and Slack
|
|
14
|
+
|
|
5
15
|
## [0.38.0]
|
|
6
16
|
|
|
7
17
|
- Support the Claude Sonnet 5.5 model family
|
data/README.md
CHANGED
|
@@ -132,7 +132,8 @@ Say "think harder" in any query to auto-upgrade to the thinking model for that s
|
|
|
132
132
|
- **App guide** — `ai_init` generates a guide injected into every system prompt
|
|
133
133
|
- **Sessions** — name, list, and resume interactive conversations (`ai_db_setup` to enable)
|
|
134
134
|
- **History compaction** — `/compact` summarizes long conversations to reduce cost and latency
|
|
135
|
-
- **Output trimming** —
|
|
135
|
+
- **Output trimming** — large execution outputs are previewed, then replaced with references once the turn that produced them is over; the LLM can recall them on demand via `recall_output`, and you can `/expand <id>` to see them
|
|
136
|
+
- **Thinking replay** — on models that think between tool calls, each round's thinking is sent back unchanged, so the model keeps its reasoning through a tool loop instead of starting over every round
|
|
136
137
|
- **Debug mode** — `/debug` shows context breakdown, token counts, and per-call cost estimates before and after each LLM call
|
|
137
138
|
- **Artifacts** — reports, exports and pages are saved and served at `/rails_console_ai/artifacts` instead of scrolling past in the console
|
|
138
139
|
- **Sub-agents** — delegate multi-step investigations to a separate LLM context that returns only a concise summary, keeping the main conversation lean
|
|
@@ -661,17 +662,19 @@ RailsConsoleAi.abort_agent(id)
|
|
|
661
662
|
|
|
662
663
|
### Per-run options
|
|
663
664
|
|
|
664
|
-
`run_agent` accepts
|
|
665
|
+
`run_agent` accepts extra keyword arguments to tune individual runs:
|
|
665
666
|
|
|
666
667
|
```ruby
|
|
667
668
|
RailsConsoleAi.run_agent(
|
|
668
669
|
"Trace why nightly billing is double-charging some accounts",
|
|
669
670
|
use_thinking_model: true, # run on the thinking tier (Opus 5.5 at high effort)
|
|
670
|
-
max_wall_clock_seconds: 1800
|
|
671
|
+
max_wall_clock_seconds: 1800, # hard kill after 30 minutes; pass nil for no cap
|
|
672
|
+
skills: ['Send Slack message to team member or channel'] # activate before the first turn
|
|
671
673
|
)
|
|
672
674
|
```
|
|
673
675
|
|
|
674
676
|
- `use_thinking_model:` (default `false`) — switches the run to the thinking tier (`config.thinking_model` at `config.thinking_effort`) for the duration of the agent. Useful for harder, multi-step problems.
|
|
677
|
+
- `skills:` (default none) — skill names (or one name) to activate before the agent's first turn, exactly as if it had called `activate_skill`: each skill's recipe is put in front of the query and its `bypass_guards_for_methods` are switched on. Use it for scheduled runs that need a skill's side effects, so the run doesn't depend on the model choosing the skill. Only approved DB skills and file skills qualify; an unknown or unapproved name fails the run (`status='failed'`) before the agent starts.
|
|
675
678
|
- `max_wall_clock_seconds:` (default `600`) — hard ceiling on wall-clock time. If the run exceeds the cap, the worker thread is killed and the session is marked `status='failed'` with `error_message: "exceeded max_wall_clock_seconds (Ns)"`. Pass `nil` to opt out of any cap.
|
|
676
679
|
|
|
677
680
|
These (along with any future per-run options) are stored in a JSON `options` column on the session row, so they survive the handoff to the background runner.
|
|
@@ -93,6 +93,7 @@ module RailsConsoleAi
|
|
|
93
93
|
opts = parse_options(session)
|
|
94
94
|
banner_extras = []
|
|
95
95
|
banner_extras << 'thinking' if opts['use_thinking_model']
|
|
96
|
+
banner_extras << "skills=#{Array(opts['skills']).join('|')}" unless Array(opts['skills']).empty?
|
|
96
97
|
if (cap = opts['max_wall_clock_seconds'])
|
|
97
98
|
banner_extras << "cap=#{cap}s"
|
|
98
99
|
end
|
|
@@ -125,12 +126,19 @@ module RailsConsoleAi
|
|
|
125
126
|
opts ||= parse_options(session)
|
|
126
127
|
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
127
128
|
channel = Channel::Api.new(user_name: session.user_name)
|
|
128
|
-
|
|
129
|
-
engine = ConversationEngine.new(binding_context: sandbox_binding, channel: channel)
|
|
129
|
+
engine = ConversationEngine.new(binding_context: RailsConsoleAi.sandbox_binding, channel: channel)
|
|
130
130
|
engine.upgrade_to_thinking_model if opts['use_thinking_model']
|
|
131
|
+
skills = Array(opts['skills'])
|
|
132
|
+
# Resolved outside one_shot (which swallows errors) so an unknown or
|
|
133
|
+
# unapproved skill fails the run instead of running without its bypasses.
|
|
134
|
+
skill_records = skills.empty? ? nil : engine.activate_skills!(skills)
|
|
131
135
|
|
|
132
136
|
exec_result = run_with_deadline(opts['max_wall_clock_seconds']) do
|
|
133
|
-
|
|
137
|
+
if skill_records
|
|
138
|
+
engine.one_shot(session.query, existing_session_id: session.id, skills: skill_records)
|
|
139
|
+
else
|
|
140
|
+
engine.one_shot(session.query, existing_session_id: session.id)
|
|
141
|
+
end
|
|
134
142
|
end
|
|
135
143
|
result_text = compose_result(channel.captured_output, exec_result)
|
|
136
144
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
require 'rails_console_ai/slash_commands'
|
|
2
2
|
|
|
3
3
|
module RailsConsoleAi
|
|
4
|
+
class SkillUnavailableError < StandardError; end
|
|
5
|
+
|
|
4
6
|
class ConversationEngine
|
|
5
7
|
attr_reader :history, :total_input_tokens, :total_output_tokens,
|
|
6
8
|
:total_cache_read_tokens, :total_cache_write_tokens,
|
|
@@ -41,23 +43,26 @@ module RailsConsoleAi
|
|
|
41
43
|
@last_interactive_executed = false
|
|
42
44
|
@compact_warned = false
|
|
43
45
|
@prior_duration_ms = 0
|
|
44
|
-
@
|
|
46
|
+
@prefix_fingerprint = nil
|
|
47
|
+
@thinking_replay_off = false
|
|
45
48
|
end
|
|
46
49
|
|
|
47
50
|
# --- Public API for channels ---
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
# skills: records from #activate_skills! — their recipes are prepended to
|
|
53
|
+
# the turn. The logged `query` stays the caller's original text.
|
|
54
|
+
def one_shot(query, existing_session_id: nil, skills: nil)
|
|
50
55
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
51
56
|
console_capture = StringIO.new
|
|
52
57
|
exec_result = with_console_capture(console_capture) do
|
|
53
|
-
|
|
58
|
+
turn = skills && !skills.empty? ? preactivated_skills_prompt(skills, query) : query
|
|
59
|
+
conversation = [{ role: :user, content: user_turn(turn) }]
|
|
54
60
|
exec_result, code, executed = one_shot_round(conversation)
|
|
55
61
|
|
|
56
62
|
if executed && @executor.last_error && !@executor.last_safety_error
|
|
57
63
|
error_msg = "Code execution failed with error: #{@executor.last_error}"
|
|
58
64
|
error_msg = error_msg[0..1000] + '...' if error_msg.length > 1000
|
|
59
65
|
error_msg += "\n\n#{@executor.last_error_hint}" if @executor.last_error_hint
|
|
60
|
-
conversation << { role: :assistant, content: @_last_result_text }
|
|
61
66
|
conversation << { role: :user, content: error_msg }
|
|
62
67
|
|
|
63
68
|
@channel.display_status(" Ran into an issue, trying a different approach...")
|
|
@@ -213,6 +218,10 @@ module RailsConsoleAi
|
|
|
213
218
|
|
|
214
219
|
def restore_session(session)
|
|
215
220
|
@history = JSON.parse(session.conversation, symbolize_names: true)
|
|
221
|
+
# A restored session has lost its stored tool outputs, so messages that
|
|
222
|
+
# were being sent as "[Output omitted]" go out with their content again.
|
|
223
|
+
# That rewrites the conversation the thinking blocks were signed against.
|
|
224
|
+
strip_thinking!(@history)
|
|
216
225
|
@interactive_session_id = session.id
|
|
217
226
|
@interactive_query = session.query
|
|
218
227
|
@session_name = session.name
|
|
@@ -275,6 +284,48 @@ module RailsConsoleAi
|
|
|
275
284
|
# land on the live executor — so it runs as a normal turn with the recipe
|
|
276
285
|
# prepended, not in a sub-agent. Returns the user-turn text to send.
|
|
277
286
|
def skill_command_prompt(skill, request)
|
|
287
|
+
activate_skill_record(skill)
|
|
288
|
+
|
|
289
|
+
request = request.to_s.strip
|
|
290
|
+
task = request.empty? ? "Follow this skill now." : request
|
|
291
|
+
|
|
292
|
+
"The user invoked the \"#{skill['name']}\" skill. Follow its procedure.\n\n" \
|
|
293
|
+
"#{skill_recipe_block(skill)}\n\n" \
|
|
294
|
+
"Their request: #{task}"
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Resolves and activates skills named up front (RailsConsoleAi.run_agent
|
|
298
|
+
# skills:), before the first turn, so a scheduled run doesn't depend on the
|
|
299
|
+
# model choosing to call activate_skill. Raises SkillUnavailableError —
|
|
300
|
+
# rather than running without the bypasses — when a name is unknown or the
|
|
301
|
+
# skill isn't approved. Returns the skill records for #one_shot.
|
|
302
|
+
def activate_skills!(names)
|
|
303
|
+
require 'rails_console_ai/skill_loader'
|
|
304
|
+
loader = RailsConsoleAi::SkillLoader.new
|
|
305
|
+
skills = Array(names).map do |name|
|
|
306
|
+
skill = loader.find_skill(name)
|
|
307
|
+
next skill if skill
|
|
308
|
+
proposed = loader.find_any_skill(name)
|
|
309
|
+
if proposed && proposed['source'] == :db && proposed['status'] != 'approved'
|
|
310
|
+
raise SkillUnavailableError, "Skill \"#{name}\" is awaiting approval at /rails_console_ai/skills"
|
|
311
|
+
end
|
|
312
|
+
raise SkillUnavailableError, "Skill not found: \"#{name}\""
|
|
313
|
+
end
|
|
314
|
+
skills.each { |skill| activate_skill_record(skill) }
|
|
315
|
+
skills
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def preactivated_skills_prompt(skills, request)
|
|
319
|
+
names = skills.map { |s| "\"#{s['name']}\"" }.join(', ')
|
|
320
|
+
recipes = skills.map { |s| skill_recipe_block(s) }.join("\n\n")
|
|
321
|
+
"#{skills.size == 1 ? 'This skill is' : 'These skills are'} already activated for this run, " \
|
|
322
|
+
"with #{skills.size == 1 ? 'its' : 'their'} guard bypasses on: #{names}. " \
|
|
323
|
+
"Follow #{skills.size == 1 ? 'its procedure' : 'their procedures'}.\n\n" \
|
|
324
|
+
"#{recipes}\n\n" \
|
|
325
|
+
"The request: #{request.to_s.strip}"
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def activate_skill_record(skill)
|
|
278
329
|
bypass_methods = Array(skill['bypass_guards_for_methods'])
|
|
279
330
|
@executor.activate_skill_bypasses(bypass_methods) unless bypass_methods.empty?
|
|
280
331
|
|
|
@@ -283,13 +334,10 @@ module RailsConsoleAi
|
|
|
283
334
|
end
|
|
284
335
|
|
|
285
336
|
@channel.display_status(" Activated skill: #{skill['name']}")
|
|
337
|
+
end
|
|
286
338
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
"The user invoked the \"#{skill['name']}\" skill. Follow its procedure.\n\n" \
|
|
291
|
-
"--- SKILL: #{skill['name']} ---\n#{skill['body']}\n--- END SKILL ---\n\n" \
|
|
292
|
-
"Their request: #{task}"
|
|
339
|
+
def skill_recipe_block(skill)
|
|
340
|
+
"--- SKILL: #{skill['name']} ---\n#{skill['body']}\n--- END SKILL ---"
|
|
293
341
|
end
|
|
294
342
|
|
|
295
343
|
# An agent is a separate context by definition, so this mirrors delegate_task:
|
|
@@ -358,7 +406,7 @@ module RailsConsoleAi
|
|
|
358
406
|
output_id = @executor.store_output(result_str)
|
|
359
407
|
if result_str.length > LARGE_OUTPUT_THRESHOLD
|
|
360
408
|
preview = result_str[0, LARGE_OUTPUT_PREVIEW_CHARS]
|
|
361
|
-
context_msg += "\n#{preview}\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{result_str.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to
|
|
409
|
+
context_msg += "\n#{preview}\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{result_str.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to read it in full]"
|
|
362
410
|
elsif !output_parts.empty?
|
|
363
411
|
context_msg += "\n#{result_str}"
|
|
364
412
|
end
|
|
@@ -458,7 +506,7 @@ module RailsConsoleAi
|
|
|
458
506
|
context_msg = "Code was executed (safety override). "
|
|
459
507
|
if result_str.length > LARGE_OUTPUT_THRESHOLD
|
|
460
508
|
context_msg += result_str[0, LARGE_OUTPUT_PREVIEW_CHARS]
|
|
461
|
-
context_msg += "\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{result_str.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to
|
|
509
|
+
context_msg += "\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{result_str.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to read it in full]"
|
|
462
510
|
else
|
|
463
511
|
context_msg += result_str
|
|
464
512
|
end
|
|
@@ -487,7 +535,7 @@ module RailsConsoleAi
|
|
|
487
535
|
context_msg = "Code was executed. "
|
|
488
536
|
if result_str.length > LARGE_OUTPUT_THRESHOLD
|
|
489
537
|
context_msg += result_str[0, LARGE_OUTPUT_PREVIEW_CHARS]
|
|
490
|
-
context_msg += "\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{result_str.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to
|
|
538
|
+
context_msg += "\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{result_str.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to read it in full]"
|
|
491
539
|
else
|
|
492
540
|
context_msg += result_str
|
|
493
541
|
end
|
|
@@ -686,7 +734,7 @@ module RailsConsoleAi
|
|
|
686
734
|
Do NOT include any preamble — just output the summary directly.
|
|
687
735
|
PROMPT
|
|
688
736
|
|
|
689
|
-
history_text = @history.map { |m| "#{m[:role]}: #{m[:content]}" }.join("\n\n")
|
|
737
|
+
history_text = @history.map { |m| "#{m[:role]}: #{without_thinking(m[:content])}" }.join("\n\n")
|
|
690
738
|
messages = [{ role: :user, content: "Summarize this conversation history:\n\n#{history_text}" }]
|
|
691
739
|
|
|
692
740
|
begin
|
|
@@ -861,8 +909,17 @@ module RailsConsoleAi
|
|
|
861
909
|
end
|
|
862
910
|
end
|
|
863
911
|
|
|
912
|
+
# Appends the round's tool calls/results and final answer to `conversation`,
|
|
913
|
+
# so the logged session shows what the agent actually did (activate_skill,
|
|
914
|
+
# execute_code, ...) and a retry round sees the full context.
|
|
864
915
|
def one_shot_round(conversation)
|
|
865
|
-
result,
|
|
916
|
+
result, tool_messages, last_llm_stats = send_query(nil, conversation: conversation)
|
|
917
|
+
conversation.concat(tool_messages) if tool_messages && !tool_messages.empty?
|
|
918
|
+
unless result.text.to_s.strip.empty?
|
|
919
|
+
entry = { role: :assistant, content: result.text }
|
|
920
|
+
entry[:llm_stats] = last_llm_stats if last_llm_stats
|
|
921
|
+
conversation << entry
|
|
922
|
+
end
|
|
866
923
|
track_usage(result)
|
|
867
924
|
code = @executor.display_response(result.text)
|
|
868
925
|
display_usage(result)
|
|
@@ -977,13 +1034,12 @@ module RailsConsoleAi
|
|
|
977
1034
|
RailsConsoleAi.configuration.validate!
|
|
978
1035
|
|
|
979
1036
|
messages = if conversation
|
|
980
|
-
conversation
|
|
1037
|
+
collapse_large_outputs!(conversation)
|
|
1038
|
+
trim_large_outputs(conversation)
|
|
981
1039
|
else
|
|
982
1040
|
[{ role: :user, content: query }]
|
|
983
1041
|
end
|
|
984
1042
|
|
|
985
|
-
messages = trim_large_outputs(messages) if conversation
|
|
986
|
-
|
|
987
1043
|
send_query_with_tools(messages)
|
|
988
1044
|
end
|
|
989
1045
|
|
|
@@ -1022,6 +1078,18 @@ module RailsConsoleAi
|
|
|
1022
1078
|
msg
|
|
1023
1079
|
end
|
|
1024
1080
|
|
|
1081
|
+
# Thinking blocks are signed against the system prompt and the tool set as
|
|
1082
|
+
# well as the messages before them. Both are meant to be fixed for a
|
|
1083
|
+
# session, but safe mode can be toggled and a skill or agent saved mid-way,
|
|
1084
|
+
# and a block replayed after either is rejected. Only the session's own
|
|
1085
|
+
# conversation is tracked — a call that brings its own prompt or tools is a
|
|
1086
|
+
# separate conversation.
|
|
1087
|
+
if system_prompt.nil? && tools_override.nil?
|
|
1088
|
+
fingerprint = [active_system_prompt, tools.definitions].hash
|
|
1089
|
+
strip_thinking!(messages) if @prefix_fingerprint && @prefix_fingerprint != fingerprint
|
|
1090
|
+
@prefix_fingerprint = fingerprint
|
|
1091
|
+
end
|
|
1092
|
+
|
|
1025
1093
|
exhausted = false
|
|
1026
1094
|
wrap_up_reason = nil
|
|
1027
1095
|
truncation_nudges = 0
|
|
@@ -1053,9 +1121,9 @@ module RailsConsoleAi
|
|
|
1053
1121
|
end
|
|
1054
1122
|
end
|
|
1055
1123
|
|
|
1056
|
-
#
|
|
1057
|
-
#
|
|
1058
|
-
|
|
1124
|
+
# Nothing already sent is rewritten between rounds. Large outputs keep the
|
|
1125
|
+
# preview the model first saw until the turn is over — see
|
|
1126
|
+
# #collapse_large_outputs!.
|
|
1059
1127
|
|
|
1060
1128
|
if round > 0
|
|
1061
1129
|
req_tokens = estimate_request_tokens(messages)
|
|
@@ -1071,7 +1139,9 @@ module RailsConsoleAi
|
|
|
1071
1139
|
provider.chat_with_tools(messages, tools: tools, system_prompt: active_system_prompt)
|
|
1072
1140
|
end
|
|
1073
1141
|
rescue Providers::ProviderError => e
|
|
1074
|
-
raise
|
|
1142
|
+
raise unless thinking_rejected?(e) && drop_thinking_replay!(messages)
|
|
1143
|
+
@channel.display_warning(" The provider rejected the replayed thinking — continuing this session without it.")
|
|
1144
|
+
retry
|
|
1075
1145
|
end
|
|
1076
1146
|
total_input += result.input_tokens || 0
|
|
1077
1147
|
total_output += result.output_tokens || 0
|
|
@@ -1117,6 +1187,7 @@ module RailsConsoleAi
|
|
|
1117
1187
|
last_thinking = (result.text && !result.text.strip.empty?) ? result.text.strip : nil
|
|
1118
1188
|
|
|
1119
1189
|
assistant_msg = provider.format_assistant_message(result)
|
|
1190
|
+
strip_thinking!([assistant_msg]) if @thinking_replay_off
|
|
1120
1191
|
assistant_msg[:llm_stats] = format_llm_stats(result)
|
|
1121
1192
|
messages << assistant_msg
|
|
1122
1193
|
new_messages << assistant_msg
|
|
@@ -1125,27 +1196,6 @@ module RailsConsoleAi
|
|
|
1125
1196
|
result.tool_calls.each do |tc|
|
|
1126
1197
|
break if @channel.cancelled?
|
|
1127
1198
|
|
|
1128
|
-
# Intercept recall_output/recall_outputs: expand in place instead of adding large messages
|
|
1129
|
-
if tc[:name] == 'recall_output' || tc[:name] == 'recall_outputs'
|
|
1130
|
-
ids = if tc[:name] == 'recall_outputs'
|
|
1131
|
-
Array(tc[:arguments]['ids']).map(&:to_i)
|
|
1132
|
-
else
|
|
1133
|
-
[tc[:arguments]['id'].to_i]
|
|
1134
|
-
end
|
|
1135
|
-
@channel.display_tool_call("#{tc[:name]}(#{ids.join(', ')})")
|
|
1136
|
-
expanded = expand_outputs_in_place(messages, ids)
|
|
1137
|
-
tool_result = if expanded.any?
|
|
1138
|
-
"Expanded #{expanded.length} output(s) in conversation. The full content is now visible in the original message(s) above."
|
|
1139
|
-
else
|
|
1140
|
-
"No matching outputs found with id(s) #{ids.join(', ')}."
|
|
1141
|
-
end
|
|
1142
|
-
@channel.display_status(" #{tool_result}")
|
|
1143
|
-
tool_msg = provider.format_tool_result(tc[:id], tool_result)
|
|
1144
|
-
messages << tool_msg
|
|
1145
|
-
new_messages << tool_msg
|
|
1146
|
-
next
|
|
1147
|
-
end
|
|
1148
|
-
|
|
1149
1199
|
# Display any pending LLM text before executing the tool
|
|
1150
1200
|
if last_thinking
|
|
1151
1201
|
last_thinking.split("\n").each { |line| @channel.display_thinking(" #{line}") }
|
|
@@ -1182,13 +1232,18 @@ module RailsConsoleAi
|
|
|
1182
1232
|
|
|
1183
1233
|
tool_msg = provider.format_tool_result(tc[:id], tool_result)
|
|
1184
1234
|
full_text = tool_result.to_s
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
tool_msg = provider.format_tool_result(tc[:id], truncated)
|
|
1235
|
+
# A recalled output is the model asking for the whole thing: it is
|
|
1236
|
+
# neither truncated nor stored again under a second id, and with no
|
|
1237
|
+
# output_id it is never collapsed later.
|
|
1238
|
+
unless %w[recall_output recall_outputs].include?(tc[:name])
|
|
1239
|
+
output_id = @executor.store_output(full_text)
|
|
1191
1240
|
tool_msg[:output_id] = output_id
|
|
1241
|
+
if full_text.length > LARGE_OUTPUT_THRESHOLD
|
|
1242
|
+
truncated = full_text[0, LARGE_OUTPUT_PREVIEW_CHARS]
|
|
1243
|
+
truncated += "\n\n[Output truncated at #{LARGE_OUTPUT_PREVIEW_CHARS} of #{full_text.length} chars — use explore_output with output_id=#{output_id} for focused queries, or recall_output to read it in full]"
|
|
1244
|
+
tool_msg = provider.format_tool_result(tc[:id], truncated)
|
|
1245
|
+
tool_msg[:output_id] = output_id
|
|
1246
|
+
end
|
|
1192
1247
|
end
|
|
1193
1248
|
tool_msg[:do_not_trim] = true if %w[recall_memory recall_memories activate_skill
|
|
1194
1249
|
describe_model describe_table list_models list_tables].include?(tc[:name])
|
|
@@ -1396,6 +1451,7 @@ module RailsConsoleAi
|
|
|
1396
1451
|
when 'save_skill' then "(\"#{args['name']}\")"
|
|
1397
1452
|
when 'delete_skill' then "(\"#{args['name']}\")"
|
|
1398
1453
|
when 'recall_output' then "(#{args['id']})"
|
|
1454
|
+
when 'recall_outputs' then "(#{Array(args['ids']).join(', ')})"
|
|
1399
1455
|
when 'explore_output'
|
|
1400
1456
|
task_preview = args['task'].to_s[0, 80]
|
|
1401
1457
|
task_preview += '...' if args['task'].to_s.length > 80
|
|
@@ -1549,6 +1605,16 @@ module RailsConsoleAi
|
|
|
1549
1605
|
block['type'] == 'tool_use' || block.key?(:tool_use)
|
|
1550
1606
|
end
|
|
1551
1607
|
|
|
1608
|
+
# Anthropic: { 'type' => 'thinking' } / { 'type' => 'redacted_thinking' }
|
|
1609
|
+
# Bedrock: { reasoning_content: { ... } }
|
|
1610
|
+
# Keys are symbols throughout once a session has been restored from JSON.
|
|
1611
|
+
def thinking_block?(block)
|
|
1612
|
+
return false unless block.is_a?(Hash)
|
|
1613
|
+
type = block['type'] || block[:type]
|
|
1614
|
+
type == 'thinking' || type == 'redacted_thinking' ||
|
|
1615
|
+
block.key?(:reasoning_content) || block.key?('reasoning_content')
|
|
1616
|
+
end
|
|
1617
|
+
|
|
1552
1618
|
def tool_result_content(block)
|
|
1553
1619
|
if block['type'] == 'tool_result'
|
|
1554
1620
|
block['content'].to_s
|
|
@@ -1587,7 +1653,7 @@ module RailsConsoleAi
|
|
|
1587
1653
|
messages.each do |msg|
|
|
1588
1654
|
content_str = msg[:content].is_a?(Array) ? msg[:content].to_s : msg[:content].to_s
|
|
1589
1655
|
total_content_chars += content_str.length
|
|
1590
|
-
is_expanded = msg[:expanded]
|
|
1656
|
+
is_expanded = msg[:expanded]
|
|
1591
1657
|
|
|
1592
1658
|
role = msg[:role].to_s
|
|
1593
1659
|
if role == 'tool'
|
|
@@ -1688,6 +1754,8 @@ module RailsConsoleAi
|
|
|
1688
1754
|
elsif tool_use_block?(block)
|
|
1689
1755
|
has_tool_use = true
|
|
1690
1756
|
parts << "tool_use: #{tool_use_name(block)}"
|
|
1757
|
+
elsif thinking_block?(block)
|
|
1758
|
+
parts << "thinking"
|
|
1691
1759
|
elsif block['type'] == 'text' || block.key?(:text)
|
|
1692
1760
|
text = block['text'] || block[:text]
|
|
1693
1761
|
parts << "text(#{text.to_s.length} chars)" if text.to_s.length > 0
|
|
@@ -1735,7 +1803,7 @@ module RailsConsoleAi
|
|
|
1735
1803
|
def debug_output_flags(content_text, msg)
|
|
1736
1804
|
flags = []
|
|
1737
1805
|
flags << "output ##{msg[:output_id]}" if msg[:output_id]
|
|
1738
|
-
if msg[:expanded]
|
|
1806
|
+
if msg[:expanded]
|
|
1739
1807
|
flags << "expanded"
|
|
1740
1808
|
elsif content_text.include?('Output omitted')
|
|
1741
1809
|
flags << "omitted"
|
|
@@ -1787,23 +1855,82 @@ module RailsConsoleAi
|
|
|
1787
1855
|
|
|
1788
1856
|
# --- Conversation context management ---
|
|
1789
1857
|
|
|
1858
|
+
# Thinking blocks are signed against the conversation that precedes them, so
|
|
1859
|
+
# rewriting a message that has already been sent invalidates every thinking
|
|
1860
|
+
# block after it — and the API rejects a request that replays one. Within a
|
|
1861
|
+
# turn, then, the conversation only ever grows: a large output keeps the
|
|
1862
|
+
# preview the model was first shown for as long as the tool loop runs.
|
|
1863
|
+
#
|
|
1864
|
+
# The start of a turn is the one point where earlier messages are rewritten.
|
|
1865
|
+
# Large outputs from the turns before are collapsed to a one-line reference
|
|
1866
|
+
# (the model can get them back with explore_output or recall_output), and
|
|
1867
|
+
# the thinking that followed the first of them goes with it. By then that
|
|
1868
|
+
# reasoning has done its work: the turn it belonged to has been answered.
|
|
1869
|
+
#
|
|
1870
|
+
# The decision is recorded on the message (:trimmed) rather than recomputed
|
|
1871
|
+
# per request, so what is sent for a message never changes mid-turn.
|
|
1872
|
+
def collapse_large_outputs!(history)
|
|
1873
|
+
first = nil
|
|
1874
|
+
history.each_with_index do |msg, i|
|
|
1875
|
+
next if msg[:trimmed] || !collapsible?(msg)
|
|
1876
|
+
msg[:trimmed] = true
|
|
1877
|
+
first ||= i
|
|
1878
|
+
end
|
|
1879
|
+
strip_thinking!(history, after: first) if first
|
|
1880
|
+
end
|
|
1881
|
+
|
|
1882
|
+
def collapsible?(msg)
|
|
1883
|
+
return false unless msg[:output_id] && !msg[:do_not_trim] && !msg[:expanded]
|
|
1884
|
+
stored = @executor.recall_output(msg[:output_id])
|
|
1885
|
+
!stored.nil? && stored.length > LARGE_OUTPUT_THRESHOLD
|
|
1886
|
+
end
|
|
1887
|
+
|
|
1888
|
+
# The conversation as it is sent: collapsed outputs rendered as references.
|
|
1889
|
+
# An output that is no longer stored (a session restored in a new process)
|
|
1890
|
+
# is sent as it stands, since the reference would lead nowhere.
|
|
1790
1891
|
def trim_large_outputs(messages)
|
|
1791
1892
|
messages.map do |msg|
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1893
|
+
msg[:trimmed] && @executor.recall_output(msg[:output_id]) ? trim_message(msg) : msg
|
|
1894
|
+
end
|
|
1895
|
+
end
|
|
1896
|
+
|
|
1897
|
+
# Removes thinking blocks from every message after index `after` (all of
|
|
1898
|
+
# them by default). Mutates the message hashes, which the request array and
|
|
1899
|
+
# the persisted history share.
|
|
1900
|
+
def strip_thinking!(messages, after: -1)
|
|
1901
|
+
messages.each_with_index do |msg, i|
|
|
1902
|
+
next if i <= after || !msg[:content].is_a?(Array)
|
|
1903
|
+
next unless msg[:content].any? { |block| thinking_block?(block) }
|
|
1904
|
+
msg[:content] = msg[:content].reject { |block| thinking_block?(block) }
|
|
1802
1905
|
end
|
|
1803
1906
|
end
|
|
1804
1907
|
|
|
1908
|
+
def without_thinking(content)
|
|
1909
|
+
content.is_a?(Array) ? content.reject { |block| thinking_block?(block) } : content
|
|
1910
|
+
end
|
|
1911
|
+
|
|
1912
|
+
# A provider turning down a request because of a thinking block it was sent
|
|
1913
|
+
# back: a prefix this harness changed without noticing, a block from another
|
|
1914
|
+
# account or model the provider won't quietly drop, a gateway that doesn't
|
|
1915
|
+
# pass thinking through. Whatever the cause, the conversation is still good
|
|
1916
|
+
# without the blocks.
|
|
1917
|
+
def thinking_rejected?(error)
|
|
1918
|
+
error.message.match?(/thinking|reasoning|signature/i)
|
|
1919
|
+
end
|
|
1920
|
+
|
|
1921
|
+
# Strips thinking from the request and stops replaying it for the rest of
|
|
1922
|
+
# the session. Returns false when there was none to strip — the error was
|
|
1923
|
+
# about something else.
|
|
1924
|
+
def drop_thinking_replay!(messages)
|
|
1925
|
+
return false unless messages.any? { |m| m[:content].is_a?(Array) && m[:content].any? { |b| thinking_block?(b) } }
|
|
1926
|
+
|
|
1927
|
+
strip_thinking!(messages)
|
|
1928
|
+
@thinking_replay_off = true
|
|
1929
|
+
true
|
|
1930
|
+
end
|
|
1931
|
+
|
|
1805
1932
|
def trim_message(msg)
|
|
1806
|
-
ref = "[Output omitted — use explore_output with output_id=#{msg[:output_id]} for focused queries, or recall_output to
|
|
1933
|
+
ref = "[Output omitted — use explore_output with output_id=#{msg[:output_id]} for focused queries, or recall_output to read it in full]"
|
|
1807
1934
|
|
|
1808
1935
|
if msg[:content].is_a?(Array)
|
|
1809
1936
|
trimmed_content = msg[:content].map do |block|
|
|
@@ -1828,46 +1955,6 @@ module RailsConsoleAi
|
|
|
1828
1955
|
end
|
|
1829
1956
|
end
|
|
1830
1957
|
|
|
1831
|
-
def expand_outputs_in_place(messages, ids)
|
|
1832
|
-
expanded = []
|
|
1833
|
-
messages.each do |msg|
|
|
1834
|
-
next unless msg[:output_id] && ids.include?(msg[:output_id])
|
|
1835
|
-
next unless expand_message_in_place(msg)
|
|
1836
|
-
expanded << msg[:output_id]
|
|
1837
|
-
end
|
|
1838
|
-
expanded
|
|
1839
|
-
end
|
|
1840
|
-
|
|
1841
|
-
def expand_message_in_place(msg)
|
|
1842
|
-
full_output = @executor.recall_output(msg[:output_id])
|
|
1843
|
-
return false unless full_output
|
|
1844
|
-
# Replace content with full output (handle Anthropic, Bedrock, and user message formats)
|
|
1845
|
-
if msg[:content].is_a?(Array)
|
|
1846
|
-
msg[:content] = msg[:content].map do |block|
|
|
1847
|
-
if block.is_a?(Hash) && tool_result_block?(block)
|
|
1848
|
-
if block.key?(:tool_result)
|
|
1849
|
-
# Bedrock format
|
|
1850
|
-
block.merge(tool_result: block[:tool_result].merge(content: [{ text: full_output }]))
|
|
1851
|
-
else
|
|
1852
|
-
# Anthropic format
|
|
1853
|
-
block.merge('content' => full_output)
|
|
1854
|
-
end
|
|
1855
|
-
else
|
|
1856
|
-
block
|
|
1857
|
-
end
|
|
1858
|
-
end
|
|
1859
|
-
elsif msg[:role].to_s == 'tool'
|
|
1860
|
-
msg[:content] = full_output
|
|
1861
|
-
else
|
|
1862
|
-
# User messages (e.g., direct execution) — preserve first line, replace rest
|
|
1863
|
-
first_line = msg[:content].to_s.lines.first&.chomp || ''
|
|
1864
|
-
msg[:content] = "#{first_line}\n#{full_output}"
|
|
1865
|
-
end
|
|
1866
|
-
msg[:expanded] = true
|
|
1867
|
-
@expanded_output_ids.add(msg[:output_id])
|
|
1868
|
-
true
|
|
1869
|
-
end
|
|
1870
|
-
|
|
1871
1958
|
def extract_executed_code(history)
|
|
1872
1959
|
code_blocks = []
|
|
1873
1960
|
history.each_cons(2) do |msg, next_msg|
|