ask-ruby-harness 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb7b07e97747d9737683a53429df81442e810b0ff9825724a02e64c49bb49c0b
4
- data.tar.gz: 27b471f9c55911b41636bd3e81d0ea68fc4d168322fffdfaea387211fc58c13d
3
+ metadata.gz: d70bd6e8f4bbf2d01ad1e3b68a49ca668edb25730dc7473690815a4416de02bd
4
+ data.tar.gz: c3e5f670a81444bc2022a32d57ea64f0ab09435b6d49a2e31333607b0d758850
5
5
  SHA512:
6
- metadata.gz: 943ae9fcc4698ff7a6c79feba5d93f30b31975417c6355a38394e146308545ecef270ad70a0675addd2929361c8043fbe8bc6f24b6e439f416fa689e4b5b0861
7
- data.tar.gz: ae876247273fa9e6936d0f817ac42f6b57c250abdc4fd7671403a7c289f9da0b03d20b7fb48b6685d7f168db854017a5790f2130430e20f6240b22ae79d477ff
6
+ metadata.gz: 5f973f3223d742de9f530bd2eaf91f70ef5ed2e6ec0376a5103d9ea6683401bc5002934dd1aa5ffa0731bf4e6fffe387d4f11a4e180258d61439bfbc7955bd73
7
+ data.tar.gz: b8326c07280c08a863073d8c01db0e90688799eef058dc63a5ecb44d268c6f153fb70de3db28795c2265a10602a7ad3a3dcc0cb609eec0aa007ee448895bba96
data/CHANGELOG.md CHANGED
@@ -1,3 +1,32 @@
1
+ ## [0.3.0] — 2026-08-10
2
+
3
+ ### Added
4
+
5
+ - **Multi-database support in `QueryDatabase`** — new `database:` param to
6
+ target any named database: a `config/database.yml` key (resolved first
7
+ through the host app's own configurations registry, so Rails multi-DB
8
+ apps work with credential-resolved configs) or a full connection URL.
9
+ The result reports which database was queried. Write guards apply to all
10
+ databases.
11
+
12
+ ### Fixed
13
+
14
+ - **The gem now requires `active_record` itself** — previously the harness
15
+ only loaded AR when the host did, so standalone processes (the MCP
16
+ server) failed `ASK_DATABASE_URL` connections with "uninitialized
17
+ constant ActiveRecord".
18
+
19
+ ## [0.2.1] — 2026-08-10
20
+
21
+ ### Fixed
22
+
23
+ - **Standalone DB connection guard** — `QueryDatabase` now checks whether a
24
+ connection spec is *defined* (pool presence) instead of `connected?`
25
+ (which stays false until the first checkout), so `establish_connection`
26
+ from `ASK_DATABASE_URL`/`database.yml` is actually honored.
27
+ - **Relative sqlite paths in `database.yml`** — resolved against the app
28
+ root (like Rails does) instead of the harness's cwd.
29
+
1
30
  ## [0.2.0] — 2026-08-10
2
31
 
3
32
  ### Added
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "time"
4
+ require "uri"
4
5
 
5
6
  module Ask
6
7
  module Ruby
@@ -8,14 +9,18 @@ module Ask
8
9
  module Tools
9
10
  class QueryDatabase < Ask::Ruby::Harness::Tool
10
11
  description "Run a read-only SQL query against the application database. " \
11
- "Returns columns and rows. Only SELECT queries are allowed in production."
12
+ "Returns columns and rows. Only SELECT queries are allowed in production. " \
13
+ "Multi-database apps can target a named database (a config/database.yml " \
14
+ "key or a full connection URL) with the database param."
12
15
 
13
16
  param :sql, type: :string, desc: "SQL query (SELECT only in production)", required: true
14
17
  param :limit, type: :integer, desc: "Max rows to return (default 50)", required: false
18
+ param :database, type: :string, desc: "Named database from config/database.yml or a full connection URL (default: primary)", required: false
15
19
 
16
20
  WRITE_STATEMENTS = /\A\s*(INSERT|UPDATE|DELETE|DROP|TRUNCATE|ALTER|CREATE|GRANT|REVOKE)\b/i
21
+ URL_PATTERN = %r{\A[a-z][a-z0-9+]*://}
17
22
 
18
- def execute(sql:, limit: 50)
23
+ def execute(sql:, limit: 50, database: nil)
19
24
  sql = sql.strip
20
25
 
21
26
  if WRITE_STATEMENTS.match?(sql)
@@ -30,38 +35,86 @@ module Ask
30
35
  )
31
36
  end
32
37
 
33
- unless Ask::Ruby::Harness.database_connected?
34
- Ask::Ruby::Harness.connect_database!
35
- end
36
- unless Ask::Ruby::Harness.database_connected?
37
- return Ask::Result.failure(
38
- "Database not connected. Set ASK_DATABASE_URL or provide a config/database.yml."
39
- )
40
- end
38
+ pool = resolve_pool(database)
39
+ return pool if pool.is_a?(Ask::Result)
41
40
 
42
- pool = ActiveRecord::Base.connection_pool
43
- pool.with_connection do |conn|
41
+ result = pool.with_connection do |conn|
44
42
  limited_sql = sql.match?(/\bLIMIT\b/i) ? sql : "#{sql.chomp(';')} LIMIT #{limit.to_i}"
45
- result = conn.exec_query(limited_sql)
46
- columns = result.columns
47
- rows = result.rows.first(limit.to_i).map { |row| build_row(row, columns) }
43
+ query_result = conn.exec_query(limited_sql)
44
+ columns = query_result.columns
45
+ rows = query_result.rows.first(limit.to_i).map { |row| build_row(row, columns) }
48
46
  {
49
47
  columns: columns,
50
48
  rows: rows,
51
49
  count: rows.size,
52
- truncated: result.rows.size > limit.to_i
50
+ truncated: query_result.rows.size > limit.to_i
53
51
  }
54
52
  end
53
+ result.merge(database: resolved_database_name(database))
55
54
  rescue ActiveRecord::StatementInvalid => e
56
55
  Ask::Result.failure("SQL error: #{e.message}")
57
56
  rescue ActiveRecord::ConnectionNotEstablished => e
58
57
  Ask::Result.failure(
59
- "Database not connected: #{e.message}. Set ASK_DATABASE_URL or config/database.yml."
58
+ "Database not connected: #{e.message}. Set ASK_DATABASE_URL or provide a config/database.yml."
60
59
  )
61
60
  end
62
61
 
63
62
  private
64
63
 
64
+ # Resolve the connection pool for the target database:
65
+ # - nil/"primary" → the default pool (connecting standalone when needed)
66
+ # - a URL → a pool established from that URL
67
+ # - a name → an existing pool (Rails multi-DB), else a pool
68
+ # established from config/database.yml
69
+ def resolve_pool(database)
70
+ if database.nil? || database == "primary"
71
+ unless Ask::Ruby::Harness.database_configured?
72
+ Ask::Ruby::Harness.connect_database!
73
+ end
74
+ unless Ask::Ruby::Harness.database_configured?
75
+ return Ask::Result.failure(
76
+ "Database not connected. Set ASK_DATABASE_URL or provide a config/database.yml."
77
+ )
78
+ end
79
+ return ActiveRecord::Base.connection_pool
80
+ end
81
+
82
+ name = database.to_s
83
+ pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(name)
84
+ return pool if pool
85
+
86
+ config = url_config(database) || Ask::Ruby::Harness.database_config_for(name)
87
+ unless config
88
+ return Ask::Result.failure(
89
+ "Database '#{database}' not found. Add it to config/database.yml or pass a full connection URL."
90
+ )
91
+ end
92
+
93
+ owner = name.match?(URL_PATTERN) ? "ask_url_#{name.hash.abs}" : name.to_sym
94
+ ActiveRecord::Base.connection_handler.establish_connection(config, owner_name: owner)
95
+ ActiveRecord::Base.connection_handler.retrieve_connection_pool(owner.to_s) ||
96
+ Ask::Result.failure("Could not connect to database '#{database}'.")
97
+ end
98
+
99
+ def url_config(database)
100
+ database if database.to_s.match?(URL_PATTERN)
101
+ end
102
+
103
+ # What to report back as `database`: the config key as passed, or
104
+ # the database name parsed from a URL (never the full URL — it may
105
+ # carry credentials).
106
+ def resolved_database_name(database)
107
+ return "primary" if database.nil? || database == "primary"
108
+ return database unless database.to_s.match?(URL_PATTERN)
109
+
110
+ uri = URI.parse(database)
111
+ name = uri.path.to_s.delete_prefix("/")
112
+ name = File.basename(name) if name.include?("/")
113
+ name.empty? ? uri.host.to_s : name
114
+ rescue URI::InvalidURIError
115
+ database
116
+ end
117
+
65
118
  def build_row(row, columns)
66
119
  columns.each_with_index.each_with_object({}) do |(col, i), hash|
67
120
  value = row[i]
@@ -3,7 +3,7 @@
3
3
  module Ask
4
4
  module Ruby
5
5
  module Harness
6
- VERSION = "0.2.0"
6
+ VERSION = "0.3.0"
7
7
  end
8
8
  end
9
9
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_record"
3
4
  require "ask/agent"
4
5
  require "time"
5
6
  require "yaml"
@@ -72,7 +73,7 @@ module Ask
72
73
  #
73
74
  # Connection sources, in order: ASK_DATABASE_URL, config/database.yml.
74
75
  def connect_database!
75
- return if database_connected?
76
+ return if database_configured?
76
77
 
77
78
  config = ENV["ASK_DATABASE_URL"] || database_config_from_yaml
78
79
  ActiveRecord::Base.establish_connection(config) if config
@@ -80,8 +81,33 @@ module Ask
80
81
  warn "[ask-ruby-harness] database connect failed: #{e.message}"
81
82
  end
82
83
 
83
- def database_connected?
84
- defined?(ActiveRecord::Base) && ActiveRecord::Base.connected?
84
+ # Whether a connection spec is defined. Uses the pool presence, not
85
+ # `connected?` ActiveRecord's connected? stays false until a
86
+ # connection is actually checked out, while pool.with_connection
87
+ # establishes lazily on first use.
88
+ def database_configured?
89
+ defined?(ActiveRecord::Base) &&
90
+ ActiveRecord::Base.connection_handler.retrieve_connection_pool(
91
+ ActiveRecord::Base.connection_specification_name
92
+ )
93
+ rescue StandardError
94
+ false
95
+ end
96
+
97
+ # Config for a NAMED database. Resolution order:
98
+ # 1. the host app's own configurations registry (Rails multi-DB —
99
+ # resolves credentials/ENV for us),
100
+ # 2. config/database.yml, the environment section's key.
101
+ def database_config_for(name)
102
+ name = name.to_s
103
+ if ActiveRecord::Base.respond_to?(:configurations)
104
+ db = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name)
105
+ return sanitize_database_config(db.configuration_hash.transform_keys(&:to_s)) if db
106
+ end
107
+
108
+ section = database_yaml_section
109
+ config = section[name] if section.is_a?(Hash) && section[name].is_a?(Hash)
110
+ sanitize_database_config(config)
85
111
  end
86
112
 
87
113
  private
@@ -168,21 +194,39 @@ module Ask
168
194
  tools
169
195
  end
170
196
 
197
+ # Config for the primary database: the `primary` section (Rails
198
+ # multi-DB style) or the whole environment section of database.yml.
171
199
  def database_config_from_yaml
200
+ section = database_yaml_section
201
+ config = section["primary"].is_a?(Hash) ? section["primary"] : section
202
+ sanitize_database_config(config)
203
+ end
204
+
205
+ private
206
+
207
+ def database_yaml_section
172
208
  path = app_root.join("config", "database.yml")
173
209
  return nil unless path.exist?
174
210
 
175
211
  yaml = YAML.safe_load(path.read, aliases: true) || {}
176
- section = yaml[env] || yaml["development"] || {}
177
- # Rails-style multi-database config nests the primary under a key.
178
- section = section["primary"] if section.is_a?(Hash) && section["primary"].is_a?(Hash)
179
- return nil unless section.is_a?(Hash)
180
-
181
- section.slice(*Configuration::DATABASE_CONFIG_KEYS).presence
212
+ yaml[env] || yaml["development"] || {}
182
213
  rescue Psych::Exception
183
214
  nil
184
215
  end
185
216
 
217
+ def sanitize_database_config(config)
218
+ return nil unless config.is_a?(Hash)
219
+
220
+ config = config.slice(*Configuration::DATABASE_CONFIG_KEYS)
221
+ # Resolve relative sqlite paths against app_root, like Rails does —
222
+ # the harness may run with a different cwd than the project root.
223
+ if config["adapter"].to_s.include?("sqlite") &&
224
+ config["database"] && !config["database"].start_with?("/", ":")
225
+ config["database"] = app_root.join(config["database"]).to_s
226
+ end
227
+ config.presence
228
+ end
229
+
186
230
  def default_system_prompt
187
231
  <<~PROMPT
188
232
  You are a Ruby software engineer.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ask-ruby-harness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaka Ruto