pub fn reconstruct_context(entries: &[SessionEntry]) -> Vec<AgentMessage>Expand description
Reconstruct agent messages from session entries for resume.
Two modes:
-
Active-branch mode (with
Leafentries). The session file holdsleafpointer entries that record the active branch tip. When one or moreLeafentries are present, this function uses the lastLeaf’sentry_idas the tip and walks the parent chain backward viaparent_id, collecting only the entries on that branch. This is required for any session that contains branches — file-order replay would otherwise interleave messages from sibling branches into the reconstructed context. -
Legacy linear mode (no
Leafentries). Sessions written by the current runtime do not yet emitLeafmarkers; for those the entire file is one linear branch and we replay everyMessage/Compactionentry in file order.
Compaction entries are honored in both modes by replaying their
semantics: when a Compaction entry is encountered, every previously
collected message that precedes the entry whose id equals
first_kept_entry_id is dropped, the compaction summary is inserted in
its place, and the kept tail (already persisted before the marker) is
preserved. Messages written after the compaction marker are then
appended as usual.
Defensive fallback: if a Compaction entry’s first_kept_entry_id does
not match any collected entry (corrupt or forward-incompatible session),
the pre-summary buffer is dropped entirely so the summary still appears
and post-compaction entries continue to accumulate.