Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed src/openhuman/agent/harness/session/turn/core_session.rs

Steven Enamakelc11b2e95fd3b
diff --git a/src/openhuman/agent/harness/session/turn/core_session.rs b/src/openhuman/agent/harness/session/turn/core_session.rs
index 8b81785260..4502509e24 100644
--- a/src/openhuman/agent/harness/session/turn/core_session.rs
+++ b/src/openhuman/agent/harness/session/turn/core_session.rs
@@ -29,19 +29,65 @@ impl Agent {
// again. When a cached transcript prefix is present (a resumed session's
// KV-cache warm-up), prepend it and clear it so the first request reuses
// the cached prefix exactly once.
- let mut messages = self.tool_dispatcher.to_provider_messages(&self.history);
if let Some(cached) = self.cached_transcript_messages.take() {
- // The cached prefix already carries the system prompt + prior
- // conversation, so drop the freshly-rendered leading system
- // message(s) and append only this turn's new (user) messages.
- let tail = messages
+ // A resumed session's replayed prefix is **absorbed into
+ // `self.history`**, not merely prepended to this one request.
+ //
+ // It used to be prepended: the request was built from `history`,
+ // the cached prefix was `take()`n and spliced in front, and
+ // `history` was left holding only the new turn. That made the
+ // replayed conversation visible to exactly one request, and it cost
+ // the conversation twice over — both observed on the wire, not
+ // reasoned about:
+ //
+ // 1. **The next turn lost it.** `take()` empties the field, and
+ // nothing else ever re-read the transcript, so turn 2 after a
+ // restart went out with only its own messages. A ten-message
+ // thread answered its next question from four. That is
+ // conversational amnesia first and a cache miss second.
+ // 2. **The transcript written afterwards lost it.** Persistence
+ // serializes `history` (see `run_turn_impl`'s
+ // `persist_session_transcript` call), so the file produced after
+ // a resume held only the new turn — and `latest_for_agent` hands
+ // that file to the *next* resume. Each restart truncated the
+ // thread a little further.
+ //
+ // Absorbing it makes `history` the single source of truth again, so
+ // the request, the following turn and the durable record cannot
+ // disagree. The bytes on the wire for *this* request are unchanged:
+ // the prefix's entries are already provider-shaped `ChatMessage`s
+ // and `Chat` is rendered verbatim, exactly as the splice did.
+ //
+ // The leading system message(s) built for this turn are dropped in
+ // favour of the prefix's own, which is the whole point of resuming —
+ // the stored prompt is the KV-cache prefix the backend has already
+ // tokenised, and re-rendering it is what this path exists to avoid.
+ //
+ // `Chat` rather than the structured `AssistantToolCalls` /
+ // `ToolResults` variants is deliberate: these entries have already
+ // been through `bound_cached_transcript_messages`, which snaps the
+ // window past an orphaned lead and strips a trailing unpaired
+ // opener, so the cycle-pairing guard has nothing left to do. Lifting
+ // them into the structured variants would mean re-deriving a pairing
+ // that was already decided, with a second chance to get it wrong;
+ // the native envelope they carry is parsed back into real tool calls
+ // at the provider seam either way (see
+ // `message_convert::parse_native_assistant_envelope`, which exists
+ // for precisely this seeded-transcript case).
+ let tail: Vec<ConversationMessage> = self
+ .history
+ .drain(..)
+ .skip_while(|entry| {
+ matches!(entry, ConversationMessage::Chat(chat) if chat.role == "system")
+ })
+ .collect();
+ self.history = cached
.into_iter()
- .skip_while(|m| m.role == "system")
- .collect::<Vec<_>>();
- let mut combined = cached;
- combined.extend(tail);
- messages = combined;
+ .map(ConversationMessage::Chat)
+ .chain(tail)
+ .collect();
}
+ let messages = self.tool_dispatcher.to_provider_messages(&self.history);
// Multimodal prep (parity with the legacy engine): rehydrate image
// placeholders for vision-capable providers, then expand `[IMAGE:…]` /