MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / prune_session

Method prune_session

crates/opencode-session/src/compaction.rs:717–780  ·  view source on GitHub ↗

Prune tool results for a session using `SessionOps` for persistence. This is the high-level async version that mirrors the TS `prune()` function which fetches messages, prunes, and persists updates.

(
        &self,
        session_id: &str,
        session_ops: &S,
    )

Source from the content-addressed store, hash-verified

715 /// This is the high-level async version that mirrors the TS `prune()`
716 /// function which fetches messages, prunes, and persists updates.
717 pub async fn prune_session<S: SessionOps>(
718 &self,
719 session_id: &str,
720 session_ops: &S,
721 ) -> anyhow::Result<Vec<String>> {
722 if !self.config.prune {
723 return Ok(vec![]);
724 }
725
726 let msgs = session_ops.messages(session_id).await?;
727 let mut prune_msgs = messages_to_prune_format(&msgs);
728 let pruned_ids = self.prune(&mut prune_msgs);
729
730 // Persist the compacted timestamps back via session ops.
731 // We need to map pruned IDs back to their message_id + part for update.
732 for msg in &msgs {
733 let message_id = match &msg.info {
734 MessageInfo::User { id, .. } => id,
735 MessageInfo::Assistant { id, .. } => id,
736 };
737 for part in &msg.parts {
738 if let Part::Tool(tool_part) = part {
739 if pruned_ids.contains(&tool_part.id) {
740 // Re-create the part with the compacted timestamp set.
741 if let ToolState::Completed {
742 input,
743 output,
744 title,
745 metadata,
746 time,
747 attachments,
748 } = &tool_part.state
749 {
750 let updated_part = Part::Tool(crate::message_v2::ToolPart {
751 id: tool_part.id.clone(),
752 session_id: tool_part.session_id.clone(),
753 message_id: tool_part.message_id.clone(),
754 call_id: tool_part.call_id.clone(),
755 tool: tool_part.tool.clone(),
756 state: ToolState::Completed {
757 input: input.clone(),
758 output: output.clone(),
759 title: title.clone(),
760 metadata: metadata.clone(),
761 time: CompletedTime {
762 start: time.start,
763 end: time.end,
764 compacted: Some(Utc::now().timestamp_millis()),
765 },
766 attachments: attachments.clone(),
767 },
768 metadata: tool_part.metadata.clone(),
769 });
770 let _ = session_ops
771 .update_part(session_id, message_id, updated_part)
772 .await;
773 }
774 }

Callers

nothing calls this directly

Calls 7

messages_to_prune_formatFunction · 0.85
ToolInterface · 0.85
messagesMethod · 0.80
pruneMethod · 0.80
containsMethod · 0.80
cloneMethod · 0.45
update_partMethod · 0.45

Tested by

no test coverage detected