diff --git a/CHANGELOG.md b/CHANGELOG.md index eee18bb..04137be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `lemant report` normalizes metrics (credit, cost, steps, and tokens) within each task individually before combining them in the final report. - `lemans report`: add credit to summary lines. - `lemans report --model x [--model y]` filters runs by model (full id or short name). +- miniswen: improve Muse caching on OpenRouter with a unique `prompt_cache_key` for each session, preserved on recovery. ## [1.5.1] - 2026-10-06 diff --git a/lib/miniswen/agent.rb b/lib/miniswen/agent.rb index 307da94..5739395 100644 --- a/lib/miniswen/agent.rb +++ b/lib/miniswen/agent.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "json" +require "securerandom" require "time" require "miniswen/version" require "miniswen/ruby_llm" @@ -188,7 +189,9 @@ def name = "bash" Result = Data.define(:status, :submission, :messages, :steps, :cost_source, :input_tokens, :output_tokens, :cached_tokens, :thinking_tokens, :cost_usd, - :error) do + :error, :cache_id) do + def initialize(cache_id: nil, **attributes) = super(cache_id:, **attributes) + def success? = status == :submitted def to_h = super.merge(cost_source: cost_source&.to_h, version: Miniswen::VERSION) @@ -206,7 +209,7 @@ def self.from_h(payload) input_tokens: data[:input_tokens], output_tokens: data[:output_tokens], cached_tokens: data[:cached_tokens], thinking_tokens: data[:thinking_tokens], cost_usd: data[:cost_usd], - error: data[:error] + error: data[:error], cache_id: data[:cache_id] ) end @@ -231,7 +234,7 @@ def to_h = { name:, model:, priced_as:, registry: }.compact attr_reader :messages, :environment private attr_reader :max_steps, :max_time, :max_cost, :exec_timeout, :max_output_tokens, - :clock, :reporter + :cache_id, :clock, :reporter # `model` is a litellm-style name ("openrouter/z-ai/glm-5.2"), optionally # suffixed with a reasoning effort ("openrouter/openai/gpt-5.6-luna#xhigh"). @@ -303,7 +306,7 @@ def partial_result(error) Result.new( status: :error, submission: nil, messages: @messages || [], steps: @steps.to_i, cost_source: cost_source, cost_usd: @cost_known == false ? nil : @cost.to_f, - error: error, + error: error, cache_id:, **(@totals || { input_tokens: 0, output_tokens: 0, cached_tokens: 0, thinking_tokens: 0 }) ) end @@ -323,6 +326,7 @@ def provider_env private def start(instruction) + @cache_id = SecureRandom.uuid uname = execute("uname -srvm").output.to_s.strip @messages = [ { role: "system", content: SYSTEM_TEMPLATE }, @@ -340,6 +344,7 @@ def start(instruction) end def resume(history) + @cache_id = history.cache_id || SecureRandom.uuid @messages = answered_messages(history.messages) @steps = history.steps.to_i @@ -535,7 +540,7 @@ def truncate(output) def finish(status, submission: nil) Result.new( status: status, submission: submission, messages: @messages, steps: @steps, - cost_source: cost_source, cost_usd: @cost_known ? @cost : nil, error: nil, **@totals + cost_source: cost_source, cost_usd: @cost_known ? @cost : nil, error: nil, cache_id:, **@totals ) end @@ -579,10 +584,14 @@ def with_cache_breakpoints(messages) end def routing_params - order = provider_order - return {} unless order && @provider == "openrouter" + return {} unless @provider == "openrouter" - { provider: { order: order.split(",").map(&:strip), allow_fallbacks: false } } + params = {} + params[:prompt_cache_key] = cache_id if @id.start_with?("meta/muse-") + if (order = provider_order) + params[:provider] = { order: order.split(",").map(&:strip), allow_fallbacks: false } + end + params end def provider_order = ENV["LEMANS_PROVIDER_ORDER"] || ENV["OPENROUTER_PROVIDER_ORDER"] diff --git a/test/miniswen/agent_test.rb b/test/miniswen/agent_test.rb index ce6c189..49ac2b8 100644 --- a/test/miniswen/agent_test.rb +++ b/test/miniswen/agent_test.rb @@ -336,6 +336,38 @@ def test_provider_order_pins_openrouter_routing ENV.delete("OPENROUTER_PROVIDER_ORDER") end + def test_muse_cache_key + { + "openrouter/meta/muse-wonder#xhigh" => true, + "openrouter/meta/muse-spark-1.3#max" => true, + "openrouter/meta/llama-3.3-70b-instruct" => false + }.each do |model, cached| + with_api_key(:openrouter) { build_agent(model:) } + stub_llm("true", SUBMIT) + result = agent.run("task") + requests = RubyLLM::Test.requests.last(2) + + if cached + refute_empty result.cache_id + assert_equal [ result.cache_id ] * 2, requests.map { it.params[:prompt_cache_key] } + + stub_llm(SUBMIT) + fresh = agent.run("task") + + refute_equal result.cache_id, fresh.cache_id + assert_equal fresh.cache_id, RubyLLM::Test.last_request.params[:prompt_cache_key] + else + assert(requests.none? { it.params.key?(:prompt_cache_key) }) + end + end + + build_agent(model: "test/meta/muse-wonder") + stub_llm(SUBMIT) + agent.run("task") + + refute RubyLLM::Test.last_request.params.key?(:prompt_cache_key) + end + def test_the_output_cap_is_sent_only_when_set_and_clamped_to_the_advertised_maximum with_api_key(:openrouter) { build_agent(model: "openrouter/anthropic/test") } stub_llm(SUBMIT)