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
@@ -1117,20 +1117,23 @@ async fn compaction_initial_context_preserves_separate_guardian_developer_messag
.await
.expect("world state should build"),
);
let (refreshed, _) =
crate::compact::build_compaction_initial_context(&session, &step_context, &world_state)
.await;
let (refreshed, _) = crate::compact::build_compaction_replacement_history(
&session,
&step_context,
&world_state,
Vec::new(),
)
.await;
let developer_messages = refreshed
.iter()
.filter_map(|envelope| match &envelope.item {
.map(|item| &item.item)
.filter_map(|item| match item {
ResponseItem::Message { role, content, .. } if role == "developer" => {
crate::content_items_to_text(content).map(|text| {
(
text,
envelope
.item
.executed_tool_call_metadata()
item.executed_tool_call_metadata()
.and_then(|metadata| metadata.content_item_kinds.clone()),
)
})