Preserve harness metadata across conversation history (#38058)

## What changed

- Wrap response items with optional harness-owned metadata while keeping the persisted response payload shape backward compatible.
- Store compacted-history metadata in an aligned sidecar and reject malformed sidecars.
- Carry metadata through history normalization, compaction, resume, fork, truncation, migration, and rollout recording without sending it to model providers.

## Testing

- Cover legacy rollout compatibility, metadata serialization and validation, compaction retention, and resume/fork reconstruction.

GitOrigin-RevId: 17a6127c9048ff984cb4df4d97c0751a38c29f5f
This commit is contained in:
rka-oai authored and copyberry committed 2026-08-11 19:30:12 +00:00
1 parent 44d992c14e
commit 3a6f747d77
67 files changed
+2075 -655

No files matched your search

+44 -4
View File
@@ -1110,12 +1110,43 @@ fn strip_legacy_ghost_snapshot_rollout_line(value: &mut Value) -> bool {
.get("payload")
.is_some_and(is_legacy_ghost_snapshot_response_item),
Some("compacted") => {
if let Some(replacement_history) = value
.get_mut("payload")
.and_then(|payload| payload.get_mut("replacement_history"))
let Some(payload) = value.get_mut("payload").and_then(Value::as_object_mut) else {
return false;
};
let Some(replacement_history) =
payload.get("replacement_history").and_then(Value::as_array)
else {
return false;
};
let remove = replacement_history
.iter()
.map(is_legacy_ghost_snapshot_response_item)
.collect::<Vec<_>>();
if !remove.contains(&true) {
return false;
}
// Legacy checkpoints have no sidecar. If a sidecar is present, only filter a
// full-length array; malformed shapes should remain intact for typed deserialization
// to reject instead of silently shifting metadata onto a different history item.
match payload.get("replacement_history_metadata") {
None => {}
Some(Value::Array(metadata)) if metadata.len() == remove.len() => {}
Some(_) => return false,
}
let Some(replacement_history) = payload
.get_mut("replacement_history")
.and_then(Value::as_array_mut)
else {
return false;
};
retain_entries_not_marked(replacement_history, &remove);
if let Some(metadata) = payload
.get_mut("replacement_history_metadata")
.and_then(Value::as_array_mut)
{
replacement_history.retain(|item| !is_legacy_ghost_snapshot_response_item(item));
retain_entries_not_marked(metadata, &remove);
}
false
}
@@ -1123,6 +1154,15 @@ fn strip_legacy_ghost_snapshot_rollout_line(value: &mut Value) -> bool {
}
}
fn retain_entries_not_marked(entries: &mut Vec<Value>, remove: &[bool]) {
let mut index = 0;
entries.retain(|_| {
let retain = !remove[index];
index += 1;
retain
});
}
fn is_legacy_ghost_snapshot_response_item(value: &Value) -> bool {
value.get("type").and_then(Value::as_str) == Some("ghost_snapshot")
}