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
+12 -6
View File
@@ -560,7 +560,10 @@ fn build_compacted_history_preserves_user_message_passthrough_metadata() {
}
#[test]
fn insert_initial_context_before_last_real_user_or_summary_keeps_summary_last() {
fn assemble_compaction_history_keeps_prefix_first_and_summary_last() {
let prefix = ContextualUserFragment::into(crate::context::BaseInstructionsFragment(
"base instructions".to_string(),
));
let agent_completion = ResponseItem::AgentMessage {
id: None,
author: "child".to_string(),
@@ -610,11 +613,13 @@ fn insert_initial_context_before_last_real_user_or_summary_keeps_summary_last()
internal_chat_message_metadata_passthrough: None,
}];
let refreshed = raw(insert_initial_context_before_last_real_user_or_summary(
let refreshed = raw(assemble_compaction_history(
annotated(compacted_history),
annotated(initial_context),
vec![prefix.clone()],
initial_context,
));
let expected = vec![
prefix,
ResponseItem::Message {
id: None,
role: "user".to_string(),
@@ -657,7 +662,7 @@ fn insert_initial_context_before_last_real_user_or_summary_keeps_summary_last()
}
#[test]
fn insert_initial_context_before_last_real_user_or_summary_keeps_compaction_last() {
fn assemble_compaction_history_keeps_compaction_last() {
let agent_task = ResponseItem::AgentMessage {
id: None,
author: "parent".to_string(),
@@ -683,9 +688,10 @@ fn insert_initial_context_before_last_real_user_or_summary_keeps_compaction_last
internal_chat_message_metadata_passthrough: None,
}];
let refreshed = raw(insert_initial_context_before_last_real_user_or_summary(
let refreshed = raw(assemble_compaction_history(
annotated(compacted_history),
annotated(initial_context),
Vec::new(),
initial_context,
));
let expected = vec![
ResponseItem::Message {