A platform for community discussion. Free, open, simple.
FIX: Drop unsummarizable AI history safely (#44404)
Sam074b8462da98
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 22ea3dbfb95..f8127136710 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -887,7 +887,7 @@ en:no_llm_selected: "No language model selected"use_parent_llm: "Use agent's language model"max_turn_tokens: "Turn work allowance"- max_turn_tokens_help: "Limits new output and tool results per reply, shared with child agents. Previous conversation does not count. This is not a spending limit. Leave empty to use the model’s context window."+ max_turn_tokens_help: "Limits new output and tool results per reply, shared with child agents. Previous conversation does not count. This is not a spending limit. Leave empty to use 500,000 tokens."compression_threshold: "Compression threshold"compression_threshold_help: "Percentage of context usage (20–99) at which automatic compression kicks in. Lower values trigger compression earlier, preserving more headroom."vision_enabled: Include image uploadsdiff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rbindex 3ff5891ae93..0fcff1d6941 100644--- a/plugins/discourse-ai/lib/agents/bot.rb+++ b/plugins/discourse-ai/lib/agents/bot.rb@@ -5,7 +5,7 @@ module DiscourseAiclass BotBOT_NOT_FOUND = Class.new(StandardError)- FALLBACK_MAX_TURN_TOKENS = 32_000+ DEFAULT_MAX_TURN_TOKENS = 500_000MAX_DISCOVERED_IMAGE_REFERENCES = 20MAX_TOOL_CALLS_PER_COMPLETION = 50MAX_FINAL_ANSWER_TOKENS = DiscourseAi::Completions::TurnWorkBudget::MAX_FINAL_ANSWER_TOKENS@@ -64,15 +64,8 @@ module DiscourseAiinject_token_budget_final_answer_hint(prompt)end- def self.default_max_turn_tokens(llm, compression_threshold: 80)- effective_max_turn_tokens(llm)- end-- def self.effective_max_turn_tokens(llm, requested = nil, **_options)- return requested.to_i if requested.to_i > 0-- context_window = llm&.max_prompt_tokens.to_i- context_window > 0 ? context_window : FALLBACK_MAX_TURN_TOKENS+ def self.effective_max_turn_tokens(requested = nil)+ requested.to_i > 0 ? requested.to_i : DEFAULT_MAX_TURN_TOKENSenddef self.as(bot_user, agent: DiscourseAi::Agents::General.new, model: nil)@@ -175,7 +168,6 @@ module DiscourseAicurrent_llm = llmtoken_budget =self.class.effective_max_turn_tokens(- current_llm,context.turn_token_budget.presence || agent.class.max_turn_tokens.presence,)execution_context ||=diff --git a/plugins/discourse-ai/lib/agents/subagent_runner.rb b/plugins/discourse-ai/lib/agents/subagent_runner.rbindex ba1e2c9dd07..15642afa366 100644--- a/plugins/discourse-ai/lib/agents/subagent_runner.rb+++ b/plugins/discourse-ai/lib/agents/subagent_runner.rb@@ -57,7 +57,7 @@ module DiscourseAiyield(@prompt) if block_given?child_agent = child_class.new- child_context = build_child_context(child_record, model)+ child_context = build_child_context(child_record)response = collect_response(child_agent, child_context, model)return error("completion_limit", child_record) if child_context.completion_limit_reached@@ -111,7 +111,7 @@ module DiscourseAirequired_tools.empty? || (required_tools - Agent.all_available_tools).empty?end- def build_child_context(child_record, model)+ def build_child_context(child_record)child_depth = @context.subagent_depth.to_i + 1feature_context =@context.feature_context.to_h.merge(@@ -119,11 +119,7 @@ module DiscourseAi"subagent_agent_id" => child_record.id,"subagent_depth" => child_depth,)- child_budget =- Bot.effective_max_turn_tokens(- DiscourseAi::Completions::Llm.proxy(model),- child_record.max_turn_tokens,- )+ child_budget = Bot.effective_max_turn_tokens(child_record.max_turn_tokens)effective_budget = [child_budget, @context.subagent_execution_state.remaining_tokens].minchild_execution = @context.execution_context.dupdiff --git a/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb b/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rbindex f0371ac4c6c..0f014073ee0 100644--- a/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb+++ b/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb@@ -308,7 +308,7 @@ module DiscourseAi@work_budget ||=DiscourseAi::Completions::TurnWorkBudget.new(- limit: DiscourseAi::Agents::Bot.effective_max_turn_tokens(llm, resolve_token_budget),+ limit: DiscourseAi::Agents::Bot.effective_max_turn_tokens(resolve_token_budget),)token_usage_tracker =DiscourseAi::Completions::TokenUsageTracker.new(diff --git a/plugins/discourse-ai/lib/completions/context_preparation.rb b/plugins/discourse-ai/lib/completions/context_preparation.rbindex e8b7bc79b52..31478f83284 100644--- a/plugins/discourse-ai/lib/completions/context_preparation.rb+++ b/plugins/discourse-ai/lib/completions/context_preparation.rb@@ -7,6 +7,14 @@ module DiscourseAiMAX_OUTPUT_TOKENS = 2048MAX_SECONDS = 90MAX_ALLOWANCE_TOKENS = 1_000_000+ RETAINED_ATTACHMENTS_ACK = "The preceding attachments are retained historical evidence."+ HISTORY_DROPPED_NOTICE = <<~TEXT.squish+ Part of the earlier conversation was omitted because it could not be summarized to fit+ the context window. Tool actions in the omitted part may already have been completed;+ do not repeat them unless the user asks.+ TEXT+ PREVIOUS_SUMMARY_LABEL =+ "\nSummary from an earlier checkpoint (it does not cover messages omitted after it):\n"class Budgetattr_reader :calls, :spent_tokens, :elapsed_seconds@@ -118,6 +126,10 @@ module DiscourseAi# when retaining it would prevent hard fit.source = prompt.messages[1...protected_user_index]tail = prompt.messages[protected_user_index..]+ # Captured before completed current-turn evidence can move into the summary+ # source: dropping may only cut earlier turns, never actions that already ran.+ history = source.reject { |message| transient_hint?(message) }+ current_turn = tailif size > capacity && !fits?(prompt, [prompt.messages.first, *tail], **options)pending_ids =prompt.messages.filter_map { |message| message[:id] if message[:type] == :tool_call }@@ -144,13 +156,11 @@ module DiscourseAimessage[:type] == :user &&Array(message[:content]).first.to_s.start_with?("Retained historical attachments",- ) &&- acknowledgement&.dig(:content) ==- "The preceding attachments are retained historical evidence."+ ) && acknowledgement&.dig(:content) == RETAINED_ATTACHMENTS_ACKendif source.empty? || checkpoint_only- raise Error.new("hard_overflow") if size > capacity- return :not_needed+ return :not_needed if size <= capacity+ return drop_oldest_history!(prompt, history, current_turn, "hard_overflow", **options)endbegin@@ -167,21 +177,8 @@ module DiscourseAi)messages = [prompt.messages.first,- {- type: :user,- content:- "#{PromptMessagesBuilder::COMPRESSED_CONTEXT_PREFIX}#{summary}#{PromptMessagesBuilder::COMPRESSED_CONTEXT_SUFFIX}",- },- { type: :model, content: PromptMessagesBuilder::COMPRESSED_CONTEXT_ACK },- *attachments.flat_map do |message|- [- message,- {- type: :model,- content: "The preceding attachments are retained historical evidence.",- },- ]- end,+ *checkpoint_messages(summary),+ *attachment_messages(attachments),*tail,]candidate = copy_prompt(prompt, messages)@@ -198,16 +195,99 @@ module DiscourseAirescue => errorreason = error.is_a?(Error) ? error.reason : "summary_failed"if !error.is_a?(Error)- Rails.logger.warn("DiscourseAi: context_preparation reason=#{reason}")+ Rails.logger.warn(+ "DiscourseAi: context_preparation reason=#{reason} error=#{error.class}: #{error.message}",+ )endreturn :cancelled if cancel_manager&.cancelled?- raise Error.new(reason) if size > capacity- :skipped+ return :skipped if size <= capacity+ drop_oldest_history!(prompt, history, current_turn, reason, **options)endendprivate+ # Last resort when summarizing cannot make the prompt fit. The notice is+ # written as a checkpoint so later replies resume after the dropped history.+ # Priority: recent turns, then the previous summary, then dropped attachments.+ def drop_oldest_history!(prompt, history, tail, reason, **options)+ raise Error.new(reason) if history.empty?++ build =+ lambda do |notice, start, attachments = []|+ [+ prompt.messages.first,+ *checkpoint_messages(notice),+ *attachment_messages(attachments),+ *history[start..],+ *tail,+ ]+ end+ # Agent examples precede a restored checkpoint, so only cut after it; any+ # user message there keeps tool calls with their results.+ checkpoint_index = PromptMessagesBuilder.compression_checkpoint_index(history)+ first_cut = checkpoint_index ? checkpoint_index + 2 : 0+ starts =+ (first_cut...history.length).select do |index|+ history[index][:type] == :user && !transient_hint?(history[index])+ end+ starts << history.length+ start =+ starts.bsearch do |candidate|+ fits?(prompt, build.call(HISTORY_DROPPED_NOTICE, candidate), **options)+ end+ raise Error.new(reason) if start.nil?++ notice = HISTORY_DROPPED_NOTICE+ if checkpoint_index+ # Unwrap an earlier drop notice so repeated drops don't nest notices.+ previous_summary =+ PromptMessagesBuilder+ .message_text(history[checkpoint_index])+ .delete_prefix(PromptMessagesBuilder::COMPRESSED_CONTEXT_PREFIX)+ .delete_suffix(PromptMessagesBuilder::COMPRESSED_CONTEXT_SUFFIX)+ .delete_prefix(HISTORY_DROPPED_NOTICE)+ .delete_prefix(PREVIOUS_SUMMARY_LABEL)+ if previous_summary.present?+ with_summary = "#{HISTORY_DROPPED_NOTICE}#{PREVIOUS_SUMMARY_LABEL}#{previous_summary}"+ notice = with_summary if fits?(prompt, build.call(with_summary, start), **options)+ end+ end++ retained = []+ @llm+ .context_attachments(history[0...start])+ .reverse_each do |attachment|+ candidate = [attachment, *retained]+ retained = candidate if fits?(prompt, build.call(notice, start, candidate), **options)+ end++ Rails.logger.warn(+ "DiscourseAi: context_preparation reason=history_dropped original_reason=#{reason} dropped_history_messages=#{start}",+ )+ messages = build.call(notice, start, retained)+ @protected_user_index = messages.length - tail.length+ prompt.messages.replace(messages)+ :compressed+ end++ def checkpoint_messages(summary)+ [+ {+ type: :user,+ content:+ "#{PromptMessagesBuilder::COMPRESSED_CONTEXT_PREFIX}#{summary}#{PromptMessagesBuilder::COMPRESSED_CONTEXT_SUFFIX}",+ },+ { type: :model, content: PromptMessagesBuilder::COMPRESSED_CONTEXT_ACK },+ ]+ end++ def attachment_messages(attachments)+ attachments.flat_map do |message|+ [message, { type: :model, content: RETAINED_ATTACHMENTS_ACK }]+ end+ end+def fits?(prompt, messages, **options)size, capacity = @llm.prompt_capacity(copy_prompt(prompt, messages), **options)size <= capacitydiff --git a/plugins/discourse-ai/lib/summarization/fold_content.rb b/plugins/discourse-ai/lib/summarization/fold_content.rbindex 146ae48f05f..6d1992d2253 100644--- a/plugins/discourse-ai/lib/summarization/fold_content.rb+++ b/plugins/discourse-ai/lib/summarization/fold_content.rb@@ -136,10 +136,7 @@ module DiscourseAiwork_budget =DiscourseAi::Completions::TurnWorkBudget.new(limit:- DiscourseAi::Agents::Bot.effective_max_turn_tokens(- llm,- bot.agent.class.max_turn_tokens,- ),+ DiscourseAi::Agents::Bot.effective_max_turn_tokens(bot.agent.class.max_turn_tokens),)output_limit =work_budget.generation_options(diff --git a/plugins/discourse-ai/lib/translation/base_translator.rb b/plugins/discourse-ai/lib/translation/base_translator.rbindex 3e5fb8b2aa8..4e6c02af721 100644--- a/plugins/discourse-ai/lib/translation/base_translator.rb+++ b/plugins/discourse-ai/lib/translation/base_translator.rb@@ -77,8 +77,7 @@ module DiscourseAiavailable_tokens = capacity * threshold / 100 - size - PROMPT_TOKEN_RESERVEwork_budget =DiscourseAi::Completions::TurnWorkBudget.new(- limit:- DiscourseAi::Agents::Bot.effective_max_turn_tokens(llm, agent.class.max_turn_tokens),+ limit: DiscourseAi::Agents::Bot.effective_max_turn_tokens(agent.class.max_turn_tokens),)output_limit =work_budget.generation_options(diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rbindex f07bb333966..4e47276fa1c 100644--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb@@ -25,27 +25,11 @@ RSpec.describe DiscourseAi::Agents::Bot dolet(:llm_responses) { [function_call, response] }describe ".effective_max_turn_tokens" do- it "defaults to the model context window while keeping explicit allowances exact" do- [4096, 200_000, nil].each do |context_window|- model = instance_double(DiscourseAi::Completions::Llm, max_prompt_tokens: context_window)- default = context_window || described_class::FALLBACK_MAX_TURN_TOKENS- expect(described_class.effective_max_turn_tokens(model, 8000)).to eq(8000)- expect(described_class.effective_max_turn_tokens(model, 60_000)).to eq(60_000)- expect(described_class.effective_max_turn_tokens(model, 10)).to eq(10)- expect(described_class.default_max_turn_tokens(model)).to eq(default)- expect(- described_class.effective_max_turn_tokens(- model,- 4000,- compression_threshold: 50,- max_tokens: 1,- thinking_effort: "high",- ),- ).to eq(4000)- expect(described_class.default_max_turn_tokens(model, compression_threshold: 50)).to eq(- default,- )- end+ it "defaults to a fixed allowance while keeping explicit allowances exact" do+ expect(described_class.effective_max_turn_tokens(8000)).to eq(8000)+ expect(described_class.effective_max_turn_tokens(10)).to eq(10)+ expect(described_class.effective_max_turn_tokens(nil)).to eq(500_000)+ expect(described_class.effective_max_turn_tokens(0)).to eq(500_000)endend@@ -630,7 +614,7 @@ RSpec.describe DiscourseAi::Agents::Bot doit "allows a final response when the tracker starts at the token budget" dotracker =DiscourseAi::Completions::TokenUsageTracker.new(- base_request: described_class.effective_max_turn_tokens(bot.llm, 5000),+ base_request: described_class.effective_max_turn_tokens(5000),base_response: 0,)execution_context =@@ -665,7 +649,7 @@ RSpec.describe DiscourseAi::Agents::Bot do)end- it "defaults the work allowance to the model context window" do+ it "defaults the work allowance to DEFAULT_MAX_TURN_TOKENS" dono_budget_agent =Fabricate(:ai_agent,@@ -676,8 +660,6 @@ RSpec.describe DiscourseAi::Agents::Bot doklass = no_budget_agent.class_instance- expect(described_class.default_max_turn_tokens(bot.llm)).to eq(gpt_4.max_prompt_tokens)-tool_call =DiscourseAi::Completions::ToolCall.new(id: "call_1", name: "categories", parameters: {})@@ -698,7 +680,7 @@ RSpec.describe DiscourseAi::Agents::Bot doif (tracker = kwargs[:execution_context]&.token_usage_tracker)tracker.add_effective(request: 90_000, response: 30_000)kwargs[:execution_context].work_budget.debit(- gpt_4.max_prompt_tokens,+ described_class::DEFAULT_MAX_TURN_TOKENS,event_id: SecureRandom.uuid,)end@@ -726,9 +708,6 @@ RSpec.describe DiscourseAi::Agents::Bot do)expect(canned.completions).to eq(0)end- expect(described_class.default_max_turn_tokens(nil)).to eq(- described_class::FALLBACK_MAX_TURN_TOKENS,- )endit "retains a tool-heavy previous turn despite a small work allowance" do@@ -821,7 +800,7 @@ RSpec.describe DiscourseAi::Agents::Bot doagent_bot.reply(context)llm = agent_bot.llminput, _, output = llm.prompt_capacity(prompts.last)- baseline = described_class.effective_max_turn_tokens(llm, 8000)+ baseline = described_class.effective_max_turn_tokens(8000)expect(options.map { |option| option[:feature_name] }).to eq(%w[context_compression bot])expect(prompts.last.tool_choice).not_to eq(:none)diff --git a/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb b/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rbindex e01fd3d0804..6bac250e2db 100644--- a/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb@@ -24,6 +24,17 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation do)end+ def expect_history_dropped(prompt, retained: [])+ expect(prompt.messages.map { |message| message[:type] }).to eq(+ [:system, :user, :model, *retained.map { |message| message[:type] }, :user],+ )+ expect(prompt.messages[1][:content]).to start_with(+ "<compressed_context>#{described_class::HISTORY_DROPPED_NOTICE}",+ )+ expect(prompt.messages[3...-1]).to eq(retained)+ expect(prompt.messages.last[:content]).to eq("What codeword?")+ end+describe "#prepare!" doit "shares maintenance bounds across contexts forked before their first preparation" dostub_const(described_class, :MAX_CALLS, 2) do@@ -98,20 +109,166 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation doendend- it "fails hard overflow without publishing a partial checkpoint or answering" do+ it "drops the oldest history when an unusable summary leaves the prompt over capacity" doprompt = history_prompt(repetitions: 4000)- original = prompt.messages.deep_dupDiscourseAi::Completions::Llm.with_prepared_responses([""]) do |canned|llm = DiscourseAi::Completions::Llm.proxy(model)+ preparation = described_class.new(llm)++ expect(preparation.prepare!(prompt)).to eq(:compressed)+ expect(canned.completions).to eq(1)+ expect_history_dropped(prompt)+ expect(preparation.protected_user_index).to eq(prompt.messages.length - 1)+ end+ end++ it "keeps the newest complete earlier turns that fit when dropping history" do+ prompt = history_prompt(repetitions: 4000)+ # Merged consecutive user messages lose their id but are still turn boundaries.+ recent_turn = [+ { type: :user, content: "Also remember AMBER-FOX-42" },+ { type: :tool_call, id: "recent", name: "read", content: '{"arguments":{}}' },+ { type: :tool, id: "recent", name: "read", content: "AMBER-FOX-42 stored" },+ { type: :model, content: "Noted." },+ ]+ prompt.messages.insert(-2, *recent_turn.deep_dup)+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)++ expect(described_class.new(llm).prepare!(prompt)).to eq(:compressed)+ expect_history_dropped(prompt, retained: recent_turn)+ end+ end++ it "carries a previous checkpoint summary restored behind agent examples" do+ prompt = history_prompt(repetitions: 4000)+ prompt.messages.insert(+ 1,+ { type: :user, content: "Example question" },+ { type: :model, content: "Example answer" },+ { type: :user, content: "<compressed_context>Earlier summary</compressed_context>" },+ {+ type: :model,+ content: DiscourseAi::Completions::PromptMessagesBuilder::COMPRESSED_CONTEXT_ACK,+ },+ )+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)++ expect(described_class.new(llm).prepare!(prompt)).to eq(:compressed)+ expect_history_dropped(prompt)+ expect(prompt.messages[1][:content]).to include("Earlier summary")+ expect(prompt.messages.to_s).not_to include("Example question")+ expect(+ DiscourseAi::Completions::PromptMessagesBuilder.compression_checkpoint_index(+ prompt.messages,+ ),+ ).to eq(1)+ end+ end++ it "does not nest notices when dropping history again" do+ prompt = history_prompt(repetitions: 4000)+ previous =+ "#{described_class::HISTORY_DROPPED_NOTICE}#{described_class::PREVIOUS_SUMMARY_LABEL}Earlier summary"+ prompt.messages.insert(+ 1,+ { type: :user, content: "<compressed_context>#{previous}</compressed_context>" },+ {+ type: :model,+ content: DiscourseAi::Completions::PromptMessagesBuilder::COMPRESSED_CONTEXT_ACK,+ },+ )+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)++ expect(described_class.new(llm).prepare!(prompt)).to eq(:compressed)+ expect(prompt.messages[1][:content]).to eq(+ "<compressed_context>#{previous}</compressed_context>",+ )+ end+ end++ it "prefers recent turns over a previous summary when both do not fit" do+ prompt = history_prompt(repetitions: 4000)+ prompt.messages.insert(+ 1,+ {+ type: :user,+ content: "<compressed_context>#{"summary detail " * 4000}</compressed_context>",+ },+ {+ type: :model,+ content: DiscourseAi::Completions::PromptMessagesBuilder::COMPRESSED_CONTEXT_ACK,+ },+ )+ recent_turn = [+ { type: :user, content: "recent evidence " * 4000, id: "alice" },+ { type: :model, content: "Noted." },+ ]+ prompt.messages.insert(-2, *recent_turn.deep_dup)+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)++ expect(described_class.new(llm).prepare!(prompt)).to eq(:compressed)+ expect_history_dropped(prompt, retained: recent_turn)+ expect(prompt.messages[1][:content]).not_to include("summary detail")+ end+ end++ it "keeps the current turn's completed tool batch while dropping earlier history" do+ prompt = history_prompt(repetitions: 4000)+ current_batch = [+ { type: :tool_call, id: "now", name: "read", content: '{"arguments":{}}' },+ { type: :tool, id: "now", name: "read", content: "Side effect completed" },+ ]+ prompt.messages.concat(current_batch.deep_dup)+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)+ preparation = described_class.new(llm)++ expect(preparation.prepare!(prompt)).to eq(:compressed)+ expect(prompt.messages.map { |message| message[:type] }).to eq(+ %i[system user model user tool_call tool],+ )+ expect(prompt.messages.last(2)).to eq(current_batch)+ expect(preparation.protected_user_index).to eq(3)+ end+ end++ it "fails rather than dropping current-turn evidence that cannot fit without history" do+ prompt = history_prompt(repetitions: 10)+ prompt.push(type: :tool_call, id: "large", name: "read", content: '{"arguments":{}}')+ prompt.push(type: :tool, id: "large", name: "read", content: "large result " * 20_000)+ original = prompt.messages.deep_dup+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)+expect { described_class.new(llm).prepare!(prompt) }.to raise_error(described_class::Error,/unusable_summary/,)- expect(canned.completions).to eq(1)expect(prompt.messages).to eq(original)endend+ it "retains attachments from dropped history when they fit" do+ prompt = history_prompt(repetitions: 4000)+ image = { encoded_upload: { kind: :image, mime_type: "image/png", base64: "image-data" } }+ prompt.messages[1][:content] = ["Remember this image", image]+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do+ llm = DiscourseAi::Completions::Llm.proxy(model)++ expect(described_class.new(llm).prepare!(prompt)).to eq(:compressed)+ expect(prompt.messages.map { |message| message[:type] }).to eq(+ %i[system user model user model user],+ )+ expect(prompt.messages[3][:content]).to include(image)+ expect(prompt.messages[4][:content]).to eq(described_class::RETAINED_ATTACHMENTS_ACK)+ expect(prompt.messages.last[:content]).to eq("What codeword?")+ end+ end+it "preserves every fragment of source larger than the model window through bounded merges" doprompt = history_prompt(repetitions: 5000)original_result = prompt.messages[3][:content]@@ -163,16 +320,12 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation doexpect(logs.last.prev_log_id).to eq(logs[-2].id)end- it "bounds maintenance calls and leaves hard-overflow history intact on exhaustion" do+ it "bounds maintenance calls and drops hard-overflow history on exhaustion" doprompt = history_prompt(repetitions: 5000)- original = prompt.messages.deep_dupDiscourseAi::Completions::Llm.with_prepared_responses(["summary"]) do |canned|llm = DiscourseAi::Completions::Llm.proxy(model)- expect { described_class.new(llm, calls: 8).prepare!(prompt) }.to raise_error(- described_class::Error,- /preparation_budget_exhausted/,- )- expect(prompt.messages).to eq(original)+ expect(described_class.new(llm, calls: 8).prepare!(prompt)).to eq(:compressed)+ expect_history_dropped(prompt)expect(canned.completions).to eq(0)endend@@ -256,7 +409,7 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation doendend- it "falls back intact when completion reservation fails softly and rejects hard overflow" do+ it "falls back intact when completion reservation fails softly and drops history on hard overflow" dotracker = DiscourseAi::Completions::TokenUsageTracker.newexecution = DiscourseAi::Completions::ExecutionContext.new(token_usage_tracker: tracker)state =@@ -277,12 +430,11 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation do).to eq(:skipped)expect(soft_prompt.messages).to eq(original)expect(soft_prompt.skip_trim).to eq(true)- expect {- described_class.new(llm).prepare!(- history_prompt(repetitions: 4000),- subagent_execution_state: state,- )- }.to raise_error(described_class::Error, /completion_limit/)+ hard_prompt = history_prompt(repetitions: 4000)+ expect(+ described_class.new(llm).prepare!(hard_prompt, subagent_execution_state: state),+ ).to eq(:compressed)+ expect_history_dropped(hard_prompt)expect(canned.completions).to eq(0)expect(state.reserve_root_final_completion).to eq(true)end@@ -417,14 +569,10 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation dochild_preparation = described_class.new(llm, threshold: 20)prompt = history_prompt(repetitions: 4000)- original = prompt.messages.deep_dup- expect { child_preparation.prepare!(prompt, execution_context: execution) }.to raise_error(- described_class::Error,- /preparation_budget_exhausted/,- )+ expect(child_preparation.prepare!(prompt, execution_context: execution)).to eq(:compressed)expect(child_preparation.calls).to eq(8)expect(child_preparation.spent_tokens).to eq(preparation.spent_tokens)- expect(prompt.messages).to eq(original)+ expect_history_dropped(prompt)expect(canned.completions).to eq(1)endenddiff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rbindex 38e59511447..6cb237258a7 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb@@ -175,6 +175,112 @@ RSpec.describe DiscourseAi::AiBot::Playground do)endend++ it "drops unsummarizable history, then resumes from that checkpoint on the next turn" do+ first_post.update!(raw: "Remember QUARTZ-OTTER-731")+ large_result = "Record: amber inventory checked. " * 4000+ PostCustomPrompt.create!(+ post_id: second_post.id,+ custom_prompt: [+ %w[{"arguments":{}} read tool_call read],+ [large_result, "read", "tool", "read"],+ ["DATA READ", bot_user.username],+ ],+ )+ claude_2.update!(max_prompt_tokens: 16_000)+ agent_record =+ Fabricate(:ai_agent, max_turn_tokens: 4000, compression_threshold: 50, tools: [])+ dropping_playground =+ described_class.new(+ DiscourseAi::Agents::Bot.as(+ bot_user,+ agent: agent_record.class_instance.new,+ model: claude_2,+ ),+ )++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["", "Answer without the old read", "Follow-up answer"],+ ) do |_, _, prompts, options|+ reply = dropping_playground.reply_to(third_post, stream_reply: false, auto_set_title: false)++ expect(reply.raw).to eq("Answer without the old read")+ checkpoint = reply.post_custom_prompt.custom_prompt.first+ expect(checkpoint[0]).to include(+ DiscourseAi::Completions::ContextPreparation::HISTORY_DROPPED_NOTICE,+ )+ expect(checkpoint[6]).to include("source_id" => third_post.id)++ followup = Fabricate(:post, topic: pm, user: user, raw: "Repeat the codeword")+ dropping_playground.reply_to(followup, stream_reply: false, auto_set_title: false)++ expect(options.map { |option| option[:feature_name] }).to eq(+ %w[context_compression bot bot],+ )+ [prompts[1], prompts.last].each do |prompt|+ contents = prompt.messages.map { |message| message[:content].to_s }+ expect(contents).to include(checkpoint[0])+ expect(contents.join).not_to include(large_result)+ end+ expect(prompts.last.messages.map { |message| message[:content] }).to include(followup.raw)+ end+ end++ it "persists a partially retained earlier turn once across the next turn" do+ large_result = "Record: amber inventory checked. " * 4000+ PostCustomPrompt.create!(+ post_id: second_post.id,+ custom_prompt: [+ %w[{"arguments":{}} read tool_call read],+ [large_result, "read", "tool", "read"],+ ["DATA READ", bot_user.username],+ ],+ )+ third_post.update!(raw: "Also remember AMBER-FOX-42")+ recent_reply = Fabricate(:post, topic: pm, user: bot_user, raw: "Noted")+ PostCustomPrompt.create!(+ post_id: recent_reply.id,+ custom_prompt: [+ %w[{"arguments":{}} recent tool_call read],+ ["AMBER-FOX-42 stored", "recent", "tool", "read"],+ ["Noted", bot_user.username],+ ],+ )+ question = Fabricate(:post, topic: pm, user: user, raw: "Which codeword is stored?")+ claude_2.update!(max_prompt_tokens: 16_000)+ agent_record =+ Fabricate(:ai_agent, max_turn_tokens: 4000, compression_threshold: 50, tools: [])+ dropping_playground =+ described_class.new(+ DiscourseAi::Agents::Bot.as(+ bot_user,+ agent: agent_record.class_instance.new,+ model: claude_2,+ ),+ )++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["", "AMBER-FOX-42", "Still AMBER-FOX-42"],+ ) do |_, _, prompts, options|+ dropping_playground.reply_to(question, stream_reply: false, auto_set_title: false)+ followup = Fabricate(:post, topic: pm, user: user, raw: "Repeat it")+ dropping_playground.reply_to(followup, stream_reply: false, auto_set_title: false)++ expect(options.map { |option| option[:feature_name] }).to eq(+ %w[context_compression bot bot],+ )+ [prompts[1], prompts.last].each do |prompt|+ transcript = prompt.messages.to_s+ expect(transcript).to include(+ DiscourseAi::Completions::ContextPreparation::HISTORY_DROPPED_NOTICE,+ )+ expect(transcript.scan(third_post.raw).size).to eq(1)+ expect(transcript.scan("AMBER-FOX-42 stored").size).to eq(1)+ expect(transcript).not_to include(large_result)+ end+ expect(prompts.last.messages.to_s).to include(question.raw, followup.raw)+ end+ endenddescribe "#title_playground with a multipart model response" dodiff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rbindex b469196dacf..be5551e8221 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb@@ -1156,7 +1156,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession doendend- it "defaults work to the model context window" do+ it "defaults work to DEFAULT_MAX_TURN_TOKENS" doai_agent.update!(max_turn_tokens: nil)tool_call =@@ -1181,7 +1181,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession doif (tracker = kwargs[:execution_context]&.token_usage_tracker)tracker.add_effective(request: 90_000, response: 30_000)kwargs[:execution_context].work_budget.debit(- llm.max_prompt_tokens,+ DiscourseAi::Agents::Bot::DEFAULT_MAX_TURN_TOKENS,event_id: SecureRandom.uuid,)end