Preserve paginated history for spawned subagents (#33432)

## What changed

- Inherit paginated history mode when spawning or forking a subagent from a paginated parent.
- Load the parent's model context when forking and persist it as an inherited rollout prefix, while excluding that prefix from the child's projected turns, items, and metadata.
- Record the boundary between inherited context and child history, and reject partially initialized paginated subagent rollouts on resume.
- Resume paginated subagents from their stored model context instead of legacy rollout history.

## Testing

Added coverage for fresh and forked paginated subagents, compacted-history forks, model-context resume, inherited-prefix projection, metadata isolation, and incomplete-prefix detection.

GitOrigin-RevId: e57dc37c29aa4aaaf57b052b991be591d730e5ba
This commit is contained in:
Owen Lin
2026-07-15 21:25:24 +00:00
committed by copyberry
parent cb79b3cdf9
commit 2be648ba4a
36 changed files with 954 additions and 73 deletions
+66 -21
View File
@@ -21,6 +21,7 @@ use crate::StoredThread;
use crate::StoredThreadHistory;
use crate::ThreadMetadataPatch;
use crate::ThreadStore;
use crate::ThreadStoreError;
use crate::ThreadStoreResult;
use crate::UpdateThreadMetadataParams;
use crate::thread_metadata_sync::ThreadMetadataSync;
@@ -106,6 +107,42 @@ impl LiveThread {
})
}
/// Create a child thread with inherited model context already durable.
///
/// The boundary belongs in session metadata before the copied prefix is written so history
/// projection can distinguish inherited context from the child's own records immediately.
pub async fn create_with_inherited_model_context(
thread_store: Arc<dyn ThreadStore>,
mut params: CreateThreadParams,
inherited_model_context: &[RolloutItem],
) -> ThreadStoreResult<Self> {
let persisted_prefix_item_count =
persisted_rollout_items(inherited_model_context, params.history_mode).len();
params.subagent_history_start_ordinal = Some(
u64::try_from(persisted_prefix_item_count)
.map_err(|_| ThreadStoreError::Internal {
message: "inherited model context is too large".to_string(),
})?
.checked_add(1)
.ok_or_else(|| ThreadStoreError::Internal {
message: "inherited model context is too large".to_string(),
})?,
);
let live_thread = Self::create(thread_store, params).await?;
if let Err(err) = live_thread
.persist_appended_items(inherited_model_context)
.await
{
if let Err(discard_err) = live_thread.discard().await {
warn!(
"failed to discard thread persistence after inherited context append failed: {discard_err}"
);
}
return Err(err);
}
Ok(live_thread)
}
pub async fn resume(
thread_store: Arc<dyn ThreadStore>,
history_mode: ThreadHistoryMode,
@@ -150,27 +187,7 @@ impl LiveThread {
fields(item_count = raw_items.len())
)]
pub async fn append_items(&self, raw_items: &[RolloutItem]) -> ThreadStoreResult<()> {
// Empty appends are intentionally ignored rather than represented as zero-sized batches.
if raw_items.is_empty() {
return Ok(());
}
let (items, measurement) = if self.persistence_telemetry.is_enabled() {
let (items, measurement) =
measure_and_filter_rollout_items(raw_items, self.history_mode);
(items, Some(measurement))
} else {
(persisted_rollout_items(raw_items, self.history_mode), None)
};
self.thread_store
.append_items(AppendThreadItemsParams {
thread_id: self.thread_id,
items: raw_items.to_vec(),
})
.await?;
if let Some(measurement) = measurement.as_ref() {
self.persistence_telemetry
.record_batch(raw_items, measurement);
}
let items = self.persist_appended_items(raw_items).await?;
if items.is_empty() {
return Ok(());
}
@@ -195,6 +212,34 @@ impl LiveThread {
Ok(())
}
async fn persist_appended_items(
&self,
raw_items: &[RolloutItem],
) -> ThreadStoreResult<Vec<RolloutItem>> {
// Empty appends are intentionally ignored rather than represented as zero-sized batches.
if raw_items.is_empty() {
return Ok(Vec::new());
}
let (items, measurement) = if self.persistence_telemetry.is_enabled() {
let (items, measurement) =
measure_and_filter_rollout_items(raw_items, self.history_mode);
(items, Some(measurement))
} else {
(persisted_rollout_items(raw_items, self.history_mode), None)
};
self.thread_store
.append_items(AppendThreadItemsParams {
thread_id: self.thread_id,
items: raw_items.to_vec(),
})
.await?;
if let Some(measurement) = measurement.as_ref() {
self.persistence_telemetry
.record_batch(raw_items, measurement);
}
Ok(items)
}
pub async fn persist(&self) -> ThreadStoreResult<()> {
self.thread_store.persist_thread(self.thread_id).await?;
self.flush_pending_metadata_update().await