Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Validate health probe tool-call arguments (#5181)

Andy Wang9388b6d03c51
diff --git a/app/models/ai/transport/ruby_llm_adapter.rb b/app/models/ai/transport/ruby_llm_adapter.rb
index ded66206a8..65a5478f48 100644
--- a/app/models/ai/transport/ruby_llm_adapter.rb
+++ b/app/models/ai/transport/ruby_llm_adapter.rb
@@ -54,7 +54,9 @@ module Ai::Transport::RubyLlmAdapter
chat.add_message(role: :user, content: Ai::Transport::Base::PROBE_TOOL_PROMPT)
message = mapping_errors { chat.generate }
check_answering_model!(model.name, message.model)
- tool_calls(message).any? { _1.name == Ai::Transport::Base::PROBE_TOOL_NAME }
+ tool_calls(message).any? do |call|
+ call.name == Ai::Transport::Base::PROBE_TOOL_NAME && call.arguments == { "status" => "ok" }
+ end
end
# A Hash keyed by call id on 2.1.1, a collection of ToolCall on 2.2 (RubyLLM #1380).
diff --git a/test/architecture/ai_transport_contract_test.rb b/test/architecture/ai_transport_contract_test.rb
index 2fa6701d81..9561c404c2 100644
--- a/test/architecture/ai_transport_contract_test.rb
+++ b/test/architecture/ai_transport_contract_test.rb
@@ -259,7 +259,7 @@ class AiTransportContractTest < ActiveSupport::TestCase
methods.each { assert owner.respond_to?(_1), "RubyLLM.#{_1}" }
end
{ RubyLLM::Context => %i[chat embed], RubyLLM::Chat => %i[generate add_message with_headers with_max_output_tokens with_tools],
- RubyLLM::Message => %i[content model tokens cost tool_calls], RubyLLM::Embedding => %i[vectors],
+ RubyLLM::Message => %i[content model tokens cost tool_calls], RubyLLM::ToolCall => %i[name arguments], RubyLLM::Embedding => %i[vectors],
RubyLLM::Tokens => %i[input output cache_read cache_write reported_cost] }.each do |klass, methods|
methods.each { assert klass.public_method_defined?(_1), "#{klass}##{_1}" }
end
diff --git a/test/models/ai_health/probe_test.rb b/test/models/ai_health/probe_test.rb
index 24769295e9..3684a8d682 100644
--- a/test/models/ai_health/probe_test.rb
+++ b/test/models/ai_health/probe_test.rb
@@ -590,7 +590,7 @@ class AiHealth::ProbeTest < ActiveSupport::TestCase
message: {
role: "assistant",
tool_calls: [
- { id: "call_1", type: "function", function: { name: "sure_health_check", arguments: "{}" } }
+ { id: "call_1", type: "function", function: { name: "sure_health_check", arguments: '{"status":"ok"}' } }
]
}
}
@@ -712,7 +712,7 @@ class AiHealth::ProbeTest < ActiveSupport::TestCase
status: 200,
headers: { "Content-Type" => "application/json" },
body: { id: "resp_1", model: "gpt-4.1", status: "completed",
- output: [ { type: "function_call", id: "fc_1", call_id: "call_1", name: "sure_health_check", arguments: "{}" } ] }.to_json
+ output: [ { type: "function_call", id: "fc_1", call_id: "call_1", name: "sure_health_check", arguments: '{"status":"ok"}' } ] }.to_json
)
result = @probe.function_calling(
diff --git a/test/models/ai_health/probe_tool_arguments_test.rb b/test/models/ai_health/probe_tool_arguments_test.rb
new file mode 100644
index 0000000000..87ff871d9a
--- /dev/null
+++ b/test/models/ai_health/probe_tool_arguments_test.rb
@@ -0,0 +1,86 @@
+require "test_helper"
+
+class AiHealth::ProbeToolArgumentsTest < ActiveSupport::TestCase
+ PROTOCOLS = %i[chat_completions responses anthropic].freeze
+
+ setup do
+ @probe = AiHealth::Probe.new(cache: ActiveSupport::Cache::MemoryStore.new)
+ Rails.logger.stubs(:error)
+ DebugLogEntry.stubs(:capture)
+ end
+
+ PROTOCOLS.each do |protocol|
+ {
+ valid: [ [ "sure_health_check", { status: "ok" } ] ],
+ missing_status: [ [ "sure_health_check", {} ] ],
+ wrong_status: [ [ "sure_health_check", { status: "bad" } ] ],
+ null_status: [ [ "sure_health_check", { status: nil } ] ],
+ extra_argument: [ [ "sure_health_check", { status: "ok", unexpected: true } ] ],
+ non_object: [ [ "sure_health_check", [ "ok" ] ] ],
+ wrong_name: [ [ "another_tool", { status: "ok" } ] ],
+ multiple_invalid: [ [ "sure_health_check", {} ], [ "another_tool", { status: "ok" } ] ],
+ one_valid: [ [ "sure_health_check", {} ], [ "sure_health_check", { status: "ok" } ] ]
+ }.each do |scenario, calls|
+ test "#{protocol}: tool probe validates #{scenario} arguments" do
+ request = stub_tool_response(protocol, calls)
+
+ result = probe_tools(protocol)
+
+ assert_requested request
+ if %i[valid one_valid].include?(scenario)
+ assert result.passing?
+ else
+ assert result.failing?
+ assert_equal :no_tool_call, result.failure_code
+ end
+ end
+ end
+ end
+
+ %i[chat_completions responses].each do |protocol|
+ test "#{protocol}: malformed tool arguments fail the probe" do
+ request = stub_tool_response(protocol, [ [ "sure_health_check", '{"status":' ] ], serialized: true)
+
+ result = probe_tools(protocol)
+
+ assert_requested request
+ assert result.failing?
+ end
+ end
+
+ private
+ def probe_tools(protocol)
+ @probe.function_calling(
+ provider: protocol == :anthropic ? :anthropic : :openai,
+ endpoint: protocol == :anthropic ? "https://api.anthropic.com" : "https://llm.example.test/v1",
+ access_token: "test-token", model: "test-model", use_responses_endpoint: protocol == :responses
+ )
+ end
+
+ def stub_tool_response(protocol, calls, serialized: false)
+ body = { model: "test-model" }
+ case protocol
+ when :chat_completions
+ url = "https://llm.example.test/v1/chat/completions"
+ tool_calls = calls.each_with_index.map do |(name, arguments), index|
+ { id: "call_#{index}", type: "function", function: { name:, arguments: serialized ? arguments : arguments.to_json } }
+ end
+ body.merge!(id: "c1", choices: [ { index: 0, finish_reason: "tool_calls", message: { role: "assistant", tool_calls: } } ])
+ when :responses
+ url = "https://llm.example.test/v1/responses"
+ output = calls.each_with_index.map do |(name, arguments), index|
+ { type: "function_call", id: "fc_#{index}", call_id: "call_#{index}", name:, arguments: serialized ? arguments : arguments.to_json }
+ end
+ body.merge!(id: "r1", status: "completed", output:)
+ when :anthropic
+ url = "https://api.anthropic.com/v1/messages"
+ content = calls.each_with_index.map do |(name, arguments), index|
+ { type: "tool_use", id: "toolu_#{index}", name:, input: arguments }
+ end
+ body.merge!(id: "m1", type: "message", role: "assistant", stop_reason: "tool_use", content:,
+ usage: { input_tokens: 20, output_tokens: 10 })
+ end
+
+ stub_request(:post, url).to_return(status: 200, headers: { "Content-Type" => "application/json" }, body: body.to_json)
+ end
+end