Record base instructions in incremental tool history (#51188)

## What changed

- Record nonempty base instructions as a developer message after tool declarations at the start of each incremental tool context window, and omit the request-level `instructions` field.
- Preserve this prefix when rebuilding history after local or remote compaction, while keeping the compaction summary last.
- Count recorded base instructions once when estimating context usage and trimming tool outputs.

## Testing

Add regression coverage for instruction placement, omission of request-level instructions, compaction ordering, and token accounting. Exercise remote compaction trimming with both request-level and recorded base instructions.

GitOrigin-RevId: 6a6fda92c5c6eb33852a77b38d34c028cf78b164
This commit is contained in:
pakrym-oai
2026-10-05 22:20:38 +00:00
committed by copyberry
parent 5cbcf810e5
commit 062439b2f8
21 changed files with 285 additions and 75 deletions
+8 -7
View File
@@ -7,9 +7,8 @@ use crate::client_common::ResponseEvent;
use crate::compact::CompactedHistoryMetadata;
use crate::compact::CompactionAnalyticsAttempt;
use crate::compact::CompactionAnalyticsDetails;
use crate::compact::build_compaction_initial_context;
use crate::compact::build_compaction_replacement_history;
use crate::compact::compaction_status_from_result;
use crate::compact::insert_initial_context_before_last_real_user_or_summary;
use crate::compact_model_fallback::record_model_fallback;
use crate::compact_model_fallback::should_retry_with_current_model;
use crate::compact_remote_history::HistoryItemGroup;
@@ -313,11 +312,13 @@ async fn run_remote_compact_task_inner_impl(
);
analytics_details.retained_image_count = Some(retained_images);
let (new_window_number, new_window_ids) = sess.advance_auto_compact_window().await;
let (initial_context, world_state_baseline) =
build_compaction_initial_context(sess.as_ref(), replacement_step_context, &world_state)
.await;
let new_history =
insert_initial_context_before_last_real_user_or_summary(compacted_history, initial_context);
let (new_history, world_state_baseline) = build_compaction_replacement_history(
sess.as_ref(),
replacement_step_context,
&world_state,
compacted_history,
)
.await;
if let Some(trace_input_history) = trace_input_history.as_deref() {
let replacement_history = new_history