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
+26 -14
View File
@@ -33,6 +33,7 @@ use crate::context::RecommendedPluginsInstructions;
use crate::context::world_state::Placement;
use crate::context::world_state::WorldState;
use crate::context::world_state::WorldStateSnapshot;
use crate::context::world_state::WorldStateUpdate;
use crate::context::world_state::WorldStateUpdateContent;
use crate::context::world_state::split_prefix_updates;
use crate::current_time::TimeProvider;
@@ -4182,13 +4183,12 @@ impl Session {
}
/// `step_context` and `world_state` must come from the same captured step.
/// If more callers need this pair, bundle them into a captured-context struct
/// so callers cannot mix settings and WorldState from different steps.
/// Returns rendered items with placement preserved for the caller's history layout.
pub(crate) async fn build_initial_context_with_world_state(
&self,
step_context: &StepContext,
world_state: &WorldState,
) -> (Vec<ResponseItem>, WorldStateSnapshot) {
) -> (Vec<WorldStateUpdate>, WorldStateSnapshot) {
let turn_context = step_context.turn.as_ref();
let mut developer_sections = Vec::<RenderedFragment>::with_capacity(8);
let mut contextual_user_sections = Vec::<RenderedFragment>::with_capacity(2);
@@ -4331,8 +4331,9 @@ impl Session {
.render_fragment(),
);
}
let (world_state_snapshot, updates) = world_state.render_full();
let (mut items, context) = split_prefix_updates(updates);
let (snapshot, updates) = world_state.render_full();
let (mut prefix, context) = split_prefix_updates(updates);
let mut items = Vec::with_capacity(4);
let mut context = context.into_iter();
// Keep the initial-context grouping within each fragment run, but never across an item.
loop {
@@ -4445,10 +4446,18 @@ impl Session {
}
}
// New context windows and compaction install these items directly into replacement history.
for item in &mut items {
for item in prefix.iter_mut().chain(&mut items) {
item.set_turn_id_if_missing(&turn_context.sub_id);
}
(items, world_state_snapshot)
let updates = prefix
.into_iter()
.map(WorldStateUpdate::prefix_item)
.chain(items.into_iter().map(|item| WorldStateUpdate {
placement: Placement::Standalone,
content: WorldStateUpdateContent::Item(Box::new(item)),
}))
.collect();
(updates, snapshot)
}
#[tracing::instrument(level = "trace", skip_all, fields(item_count = items.len()))]
@@ -4543,14 +4552,15 @@ impl Session {
state.start_new_context_window()
};
let (window_number, window_ids) = window;
let (context_items, world_state_snapshot) = self
let (context_updates, world_state_snapshot) = self
.build_initial_context_with_world_state(step_context, world_state.as_ref())
.await;
let context_items = context_items
.into_iter()
.map(ResponseItemEnvelope::new)
.chain(retained_client_developer_messages)
.collect();
let context_items =
crate::context_manager::updates::merge_world_state_updates(context_updates)
.into_iter()
.map(ResponseItemEnvelope::new)
.chain(retained_client_developer_messages)
.collect();
let turn_context_item = step_context.to_turn_context_item();
self.replace_compacted_history(
context_items,
@@ -4605,9 +4615,11 @@ impl Session {
let world_state = Arc::new(self.build_world_state_for_step(step_context).await?);
// Full initial context resets the baseline; later turns persist only its changes.
let (mut context_items, world_state_item) = if should_inject_full_context {
let (context_items, snapshot) = self
let (context_updates, snapshot) = self
.build_initial_context_with_world_state(step_context, world_state.as_ref())
.await;
let context_items =
crate::context_manager::updates::merge_world_state_updates(context_updates);
self.state
.lock()
.await