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
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
module RailsConsoleAi
|
|
2
|
+
# Decides whether a SQL string changes anything, and which table each change
|
|
3
|
+
# targets, for the :database_writes guard.
|
|
4
|
+
#
|
|
5
|
+
# The guard used to ask one question: does the string *start* with INSERT,
|
|
6
|
+
# UPDATE, DELETE, DROP, CREATE, ALTER or TRUNCATE. That let through a write
|
|
7
|
+
# behind a leading comment, a write inside or after a CTE
|
|
8
|
+
# (`WITH d AS (DELETE ... RETURNING id) SELECT ...`), a second statement after
|
|
9
|
+
# a `;`, and every write that starts with another word (REPLACE, MERGE, GRANT,
|
|
10
|
+
# CALL, ...). It also took the allowlist's table from the first FROM/INTO in
|
|
11
|
+
# the string, which for `WITH a AS (SELECT * FROM sessions) DELETE FROM users`
|
|
12
|
+
# is the table being read, not the one being written.
|
|
13
|
+
#
|
|
14
|
+
# This is still pattern matching, not a parser. It blocks statements it
|
|
15
|
+
# recognizes as writes and lets the rest through, and it cannot see a write
|
|
16
|
+
# made by a function a SELECT calls (`SELECT setval(...)`) or one that goes
|
|
17
|
+
# around the adapter (`raw_connection`). It is there to catch the writes the
|
|
18
|
+
# model makes by accident, not to contain code that is trying to get past it.
|
|
19
|
+
module SqlWrites
|
|
20
|
+
# table / qualified_table are nil when the write cannot be pinned on exactly
|
|
21
|
+
# one table — such a write can never be let through by the allowlist.
|
|
22
|
+
Write = Struct.new(:keyword, :table, :qualified_table, :summary)
|
|
23
|
+
|
|
24
|
+
# Statements that change data, schema or privileges, run arbitrary stored
|
|
25
|
+
# code, or move data in bulk — everything that isn't plain DML, which is
|
|
26
|
+
# looked for anywhere in a statement rather than only at its start.
|
|
27
|
+
NON_DML = %w[
|
|
28
|
+
DROP CREATE ALTER TRUNCATE RENAME GRANT REVOKE COMMENT
|
|
29
|
+
CALL DO EXECUTE COPY LOAD IMPORT
|
|
30
|
+
VACUUM REINDEX CLUSTER REFRESH ANALYZE OPTIMIZE REPAIR
|
|
31
|
+
].freeze
|
|
32
|
+
|
|
33
|
+
# DML that can be written without the INTO the anywhere-scan requires.
|
|
34
|
+
BARE_DML = %w[REPLACE MERGE UPSERT].freeze
|
|
35
|
+
|
|
36
|
+
module_function
|
|
37
|
+
|
|
38
|
+
# Every write the SQL would make; empty when it only reads. `mysql` selects
|
|
39
|
+
# MySQL's lexical rules (see .skeleton).
|
|
40
|
+
def find(sql, mysql: false)
|
|
41
|
+
skeleton(sql.to_s, mysql).split(';').flat_map { |statement| statement_writes(statement) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# --- lexing -----------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
STRING = /'(?:[^']|'')*'/
|
|
47
|
+
ESCAPE_STRING = /'(?:[^'\\]|\\.|'')*'/m
|
|
48
|
+
BACKTICKED = /`(?:[^`]|``)*`/
|
|
49
|
+
DOUBLE_QUOTED = /"(?:[^"]|"")*"/
|
|
50
|
+
BLOCK_COMMENT = %r{/\*.*?\*/}m
|
|
51
|
+
|
|
52
|
+
# Where the dialects disagree, each rule set follows its own database, and
|
|
53
|
+
# the default set is the one for everything that isn't MySQL:
|
|
54
|
+
# - backslash escapes a quote inside '...' on MySQL; elsewhere only in E'...'
|
|
55
|
+
# - "..." is a string on MySQL and an identifier elsewhere
|
|
56
|
+
# - `#` starts a comment on MySQL and is an operator on PostgreSQL
|
|
57
|
+
# - `--` needs trailing whitespace to be a comment on MySQL
|
|
58
|
+
# - $tag$...$tag$ is a string on PostgreSQL
|
|
59
|
+
STANDARD_TOKENS = Regexp.union(
|
|
60
|
+
/--[^\n]*/, BLOCK_COMMENT, /\b[eE]#{ESCAPE_STRING}/, STRING,
|
|
61
|
+
/\$([A-Za-z_]\w*|)\$.*?\$\1\$/m, DOUBLE_QUOTED, BACKTICKED
|
|
62
|
+
)
|
|
63
|
+
MYSQL_TOKENS = Regexp.union(
|
|
64
|
+
/--(?=\s|\z)[^\n]*/, /#[^\n]*/, BLOCK_COMMENT, ESCAPE_STRING,
|
|
65
|
+
/"(?:[^"\\]|\\.|"")*"/m, BACKTICKED
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# MySQL and MariaDB execute the contents of /*! ... */ and /*M! ... */.
|
|
69
|
+
EXECUTABLE_COMMENT = %r{\A/\*M?!\d*(.*)\*/\z}m
|
|
70
|
+
|
|
71
|
+
# The SQL with everything that isn't code taken out: comments become a
|
|
72
|
+
# space, string literals become '', and quoted identifiers become "name"
|
|
73
|
+
# (or "?" when the name isn't a plain word). What is left can be searched
|
|
74
|
+
# for keywords and split on `;` without tripping over a comment that says
|
|
75
|
+
# "update" or a string that contains a semicolon.
|
|
76
|
+
def skeleton(sql, mysql)
|
|
77
|
+
sql.gsub(mysql ? MYSQL_TOKENS : STANDARD_TOKENS) do |token|
|
|
78
|
+
case token[0]
|
|
79
|
+
when '`'
|
|
80
|
+
identifier(token)
|
|
81
|
+
when '"'
|
|
82
|
+
mysql ? "''" : identifier(token)
|
|
83
|
+
when '/'
|
|
84
|
+
mysql && (inner = token[EXECUTABLE_COMMENT, 1]) ? " #{skeleton(inner, mysql)} " : ' '
|
|
85
|
+
when '-', '#'
|
|
86
|
+
' '
|
|
87
|
+
else
|
|
88
|
+
"''"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def identifier(token)
|
|
94
|
+
name = token[1..-2]
|
|
95
|
+
name.match?(/\A[\w$]+\z/) ? %("#{name}") : '"?"'
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# --- classifying one statement ----------------------------------------
|
|
99
|
+
|
|
100
|
+
IDENT = /(?:"[\w$?]++"|[\w$]++)/
|
|
101
|
+
QUALIFIED = /(?>(?:#{IDENT}\.)*#{IDENT})/
|
|
102
|
+
ONE_TABLE = /(#{QUALIFIED})(?!\s*,)/
|
|
103
|
+
|
|
104
|
+
# INSERT, UPDATE and DELETE also appear inside clauses that don't start a
|
|
105
|
+
# write of their own: row locks, upsert and MERGE actions, foreign key rules.
|
|
106
|
+
CLAUSE_KEYWORDS = /\b(?:FOR(?:\s+NO\s+KEY)?|ON\s+DUPLICATE\s+KEY|DO|ON|THEN)\s+(?:INSERT|UPDATE|DELETE)\b/i
|
|
107
|
+
|
|
108
|
+
# DML anywhere in a statement. INSERT( is MySQL's string function; MERGE and
|
|
109
|
+
# REPLACE are only keywords here when INTO follows, because `merge` is a
|
|
110
|
+
# legal column name and REPLACE( is a function everywhere.
|
|
111
|
+
DML = /
|
|
112
|
+
(?<!["\w$.])
|
|
113
|
+
(?: INSERT (?!\s*\()
|
|
114
|
+
| UPDATE
|
|
115
|
+
| DELETE
|
|
116
|
+
| MERGE (?=\s+INTO\b)
|
|
117
|
+
| REPLACE (?=\s+(?:(?:LOW_PRIORITY|DELAYED)\s+)*INTO\b)
|
|
118
|
+
)
|
|
119
|
+
(?!["\w$])
|
|
120
|
+
/ix
|
|
121
|
+
|
|
122
|
+
DML_TARGETS = {
|
|
123
|
+
'INSERT' => /\AINSERT\s+(?:(?:LOW_PRIORITY|DELAYED|HIGH_PRIORITY|IGNORE|OR\s+\w+)\s+)*(?:INTO\s+)?#{ONE_TABLE}/i,
|
|
124
|
+
'REPLACE' => /\AREPLACE\s+(?:(?:LOW_PRIORITY|DELAYED)\s+)*INTO\s+#{ONE_TABLE}/i,
|
|
125
|
+
# Only the single-table form. `UPDATE a JOIN b SET b.x = ...` writes a
|
|
126
|
+
# table other than the first one named, so it gets no target at all.
|
|
127
|
+
'UPDATE' => /\AUPDATE\s+(?:(?:LOW_PRIORITY|IGNORE|ONLY|OR\s+\w+)\s+)*#{ONE_TABLE}(?=\s+(?:AS\s+)?(?:\w+\s+)?SET\b)/i,
|
|
128
|
+
'DELETE' => /\ADELETE\s+(?:(?:LOW_PRIORITY|QUICK|IGNORE)\s+)*FROM\s+(?:ONLY\s+)?#{ONE_TABLE}/i,
|
|
129
|
+
'MERGE' => /\AMERGE\s+INTO\s+(?:ONLY\s+)?#{ONE_TABLE}/i
|
|
130
|
+
}.freeze
|
|
131
|
+
|
|
132
|
+
DDL_TARGET = /
|
|
133
|
+
\A(?: (?:CREATE|ALTER|DROP|RENAME) \s+
|
|
134
|
+
(?:(?:GLOBAL|LOCAL|TEMPORARY|TEMP|UNLOGGED|FOREIGN|VIRTUAL|OR\s+REPLACE)\s+)*
|
|
135
|
+
TABLE
|
|
136
|
+
| TRUNCATE (?:\s+TABLE)?
|
|
137
|
+
) \s+
|
|
138
|
+
(?:IF\s+(?:NOT\s+)?EXISTS\s+)?
|
|
139
|
+
(?:ONLY\s+)?
|
|
140
|
+
#{ONE_TABLE}
|
|
141
|
+
/ix
|
|
142
|
+
|
|
143
|
+
def statement_writes(statement)
|
|
144
|
+
statement = statement.sub(/\A[\s(]+/, '')
|
|
145
|
+
keyword = statement[/\A[A-Za-z]+/].to_s.upcase
|
|
146
|
+
return [] if keyword.empty?
|
|
147
|
+
# EXPLAIN only runs the statement it explains when asked to ANALYZE it.
|
|
148
|
+
return [] if %w[EXPLAIN DESCRIBE DESC].include?(keyword) && !statement.match?(/\bANALYZE\b/i)
|
|
149
|
+
|
|
150
|
+
writes = dml_writes(statement)
|
|
151
|
+
|
|
152
|
+
if non_dml?(keyword, statement)
|
|
153
|
+
writes << write(keyword, statement[DDL_TARGET, 1], statement)
|
|
154
|
+
elsif writes.empty? && BARE_DML.include?(keyword)
|
|
155
|
+
writes << write(keyword, nil, statement)
|
|
156
|
+
elsif writes.empty? && statement.match?(/\bINTO\s+(?!@)/i)
|
|
157
|
+
# SELECT ... INTO new_table (PostgreSQL) or INTO OUTFILE (MySQL).
|
|
158
|
+
writes << write('SELECT INTO', nil, statement)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
writes
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def dml_writes(statement)
|
|
165
|
+
scannable = statement.gsub(CLAUSE_KEYWORDS) { |clause| clause.gsub(/\s+/, '_') }
|
|
166
|
+
writes = []
|
|
167
|
+
scannable.scan(DML) do
|
|
168
|
+
keyword = Regexp.last_match[0].upcase
|
|
169
|
+
from_keyword = scannable[Regexp.last_match.begin(0)..-1]
|
|
170
|
+
writes << write(keyword, from_keyword[DML_TARGETS.fetch(keyword), 1], from_keyword)
|
|
171
|
+
end
|
|
172
|
+
writes
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def non_dml?(keyword, statement)
|
|
176
|
+
return false unless NON_DML.include?(keyword)
|
|
177
|
+
# COPY ... TO STDOUT is an export; any DML inside it is found by the scan.
|
|
178
|
+
!(keyword == 'COPY' && statement.match?(/\bTO\s+STDOUT\b/i))
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def write(keyword, qualified, text)
|
|
182
|
+
qualified = nil if qualified && qualified.include?('?')
|
|
183
|
+
qualified = qualified.delete('"') if qualified
|
|
184
|
+
table = qualified && qualified.split('.').last
|
|
185
|
+
Write.new(keyword, table, qualified, text.strip.split(/\s+/).first(3).join(' '))
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
@@ -188,7 +188,7 @@ module RailsConsoleAi
|
|
|
188
188
|
if @executor
|
|
189
189
|
register(
|
|
190
190
|
name: 'recall_output',
|
|
191
|
-
description: '
|
|
191
|
+
description: 'Return a previously omitted/truncated output in full. It is added to this conversation and stays in context for the rest of the session. Prefer `explore_output` if you only need a specific answer about the output — that keeps this conversation lean. Use `recall_output` only when you need the full content alongside other context here. Use the output id shown in the "[Output omitted]" or "[Output truncated]" placeholder.',
|
|
192
192
|
parameters: {
|
|
193
193
|
'type' => 'object',
|
|
194
194
|
'properties' => {
|
|
@@ -204,7 +204,7 @@ module RailsConsoleAi
|
|
|
204
204
|
|
|
205
205
|
register(
|
|
206
206
|
name: 'recall_outputs',
|
|
207
|
-
description: '
|
|
207
|
+
description: 'Return multiple previously omitted outputs in full; they are added to this conversation and stay in context. Prefer `explore_output` per-id for focused queries. Use the output ids shown in "[Output omitted]" or "[Output truncated]" placeholders.',
|
|
208
208
|
parameters: {
|
|
209
209
|
'type' => 'object',
|
|
210
210
|
'properties' => {
|
|
@@ -212,7 +212,12 @@ module RailsConsoleAi
|
|
|
212
212
|
},
|
|
213
213
|
'required' => ['ids']
|
|
214
214
|
},
|
|
215
|
-
handler: ->(args) {
|
|
215
|
+
handler: ->(args) {
|
|
216
|
+
Array(args['ids']).map { |id|
|
|
217
|
+
output = @executor.recall_output(id.to_i)
|
|
218
|
+
output ? "--- output #{id} ---\n#{output}" : "No output found with id #{id}"
|
|
219
|
+
}.join("\n\n")
|
|
220
|
+
}
|
|
216
221
|
)
|
|
217
222
|
|
|
218
223
|
if @mode != :sub_agent
|
|
@@ -457,7 +462,7 @@ module RailsConsoleAi
|
|
|
457
462
|
tail = if output_id
|
|
458
463
|
"\n\n[Truncated at #{ARTIFACT_READ_LIMIT} of #{body.length} chars — " \
|
|
459
464
|
"use explore_output with output_id=#{output_id} to query the rest, " \
|
|
460
|
-
"or recall_output to
|
|
465
|
+
"or recall_output to read it in full]"
|
|
461
466
|
else
|
|
462
467
|
"\n\n[Truncated at #{ARTIFACT_READ_LIMIT} of #{body.length} chars]"
|
|
463
468
|
end
|
data/lib/rails_console_ai.rb
CHANGED
|
@@ -197,19 +197,34 @@ module RailsConsoleAi
|
|
|
197
197
|
nil
|
|
198
198
|
end
|
|
199
199
|
|
|
200
|
+
# A fresh, empty binding for code run by a non-console channel (background
|
|
201
|
+
# agents, Slack). It has to be built here, in a method with no locals: a
|
|
202
|
+
# `binding` taken inside a block captures the enclosing method's locals, so
|
|
203
|
+
# building it inline would hand the caller's internals (session, engine,
|
|
204
|
+
# channel, ...) to the model's code.
|
|
205
|
+
def sandbox_binding
|
|
206
|
+
Object.new.instance_eval { binding }
|
|
207
|
+
end
|
|
208
|
+
|
|
200
209
|
# Enqueue an agent run. Returns the Integer session id immediately;
|
|
201
210
|
# the actual work is picked up by `rake rails_console_ai:agents`.
|
|
202
211
|
#
|
|
203
212
|
# use_thinking_model: run on the configured thinking-tier model
|
|
204
213
|
# max_wall_clock_seconds: hard kill the run after N seconds (nil = no cap)
|
|
214
|
+
# skills: skill names to activate before the first turn
|
|
215
|
+
# (recipe + guard bypasses); the run fails if any
|
|
216
|
+
# is unknown or unapproved
|
|
205
217
|
def run_agent(query, name: nil, user_name: nil,
|
|
206
218
|
use_thinking_model: false,
|
|
207
|
-
max_wall_clock_seconds: 600
|
|
219
|
+
max_wall_clock_seconds: 600,
|
|
220
|
+
skills: nil)
|
|
208
221
|
require 'rails_console_ai/session_logger'
|
|
209
222
|
options = {
|
|
210
223
|
'use_thinking_model' => !!use_thinking_model,
|
|
211
224
|
'max_wall_clock_seconds' => max_wall_clock_seconds
|
|
212
225
|
}
|
|
226
|
+
skill_names = Array(skills).map(&:to_s).reject(&:empty?)
|
|
227
|
+
options['skills'] = skill_names unless skill_names.empty?
|
|
213
228
|
id = SessionLogger.log(
|
|
214
229
|
query: query,
|
|
215
230
|
conversation: [],
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_console_ai
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.39.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Cortfr
|
|
@@ -170,6 +170,7 @@ files:
|
|
|
170
170
|
- lib/rails_console_ai/conversation_engine.rb
|
|
171
171
|
- lib/rails_console_ai/engine.rb
|
|
172
172
|
- lib/rails_console_ai/executor.rb
|
|
173
|
+
- lib/rails_console_ai/guard_state.rb
|
|
173
174
|
- lib/rails_console_ai/line_editor.rb
|
|
174
175
|
- lib/rails_console_ai/prefixed_io.rb
|
|
175
176
|
- lib/rails_console_ai/providers/anthropic.rb
|
|
@@ -185,6 +186,7 @@ files:
|
|
|
185
186
|
- lib/rails_console_ai/skill_loader.rb
|
|
186
187
|
- lib/rails_console_ai/slack_bot.rb
|
|
187
188
|
- lib/rails_console_ai/slash_commands.rb
|
|
189
|
+
- lib/rails_console_ai/sql_writes.rb
|
|
188
190
|
- lib/rails_console_ai/storage/base.rb
|
|
189
191
|
- lib/rails_console_ai/storage/database_storage.rb
|
|
190
192
|
- lib/rails_console_ai/storage/file_storage.rb
|