pub struct Transcript { /* private fields */ }Expand description
Running conversation history for one agent-loop run.
Why: Centralises message accumulation so the loop body stays focused on
control flow rather than ChatMessage plumbing, and (#2070) centralises
the compaction state machine so the loop only has to call
maybe_compact at its turn boundary.
What: A thin wrapper over Vec<TranscriptEntry> with append helpers, a
raw-history accessor, a compacted model-facing view, and an
assistant-text accessor. compaction_triggered records whether ANY
compaction has happened yet in this run — once true, to_messages
starts appending the last-user-message replay (§5.4 item 3) on every
subsequent call, re-anchoring the model on every following turn, not just
the turn compaction fired on.
Test: transcript::tests::*.
Implementations§
Source§impl Transcript
impl Transcript
Sourcepub fn seed(system: &str, task: &str) -> Self
pub fn seed(system: &str, task: &str) -> Self
Seed a transcript with a system prompt and the user task.
Why: Every run starts from exactly these two turns; bundling the seed in
a constructor prevents the loop from forgetting either.
What: Pushes a system message then a user message, both starting
uncompacted. maybe_compact (#2070) never marks system or user
role entries — see its docs — so these two seed turns are preserved
forever regardless of how long the run continues.
Test: transcript::tests::seed_has_two_messages.
Sourcepub fn push_assistant(&mut self, text: Option<String>, tool_calls: &[ToolCall])
pub fn push_assistant(&mut self, text: Option<String>, tool_calls: &[ToolCall])
Append the assistant turn from a model response.
Why: The model’s turn (text and any tool calls) must be added to history
before the tool results, so the next request reflects the real ordering
the API expects.
What: Reconstructs a ChatMessage with role = "assistant", carrying the
optional text content and the emitted tool calls; records the text (if
any) for the final answer.
Test: transcript::tests::push_assistant_records_text.
Sourcepub fn push_tool_result(
&mut self,
tool_call_id: &str,
name: &str,
content: &str,
)
pub fn push_tool_result( &mut self, tool_call_id: &str, name: &str, content: &str, )
Append a tool role result message answering a specific tool call.
Why: The API requires each tool call to be answered by a tool message
referencing its tool_call_id, or the next assistant turn errors.
(#2070) A use_skill result is the loaded body of a skill the model
deliberately chose to invoke — §5.4 item 5 requires it survive
compaction forever, the same guarantee system/user turns get.
What: Pushes a ChatMessage::tool_result with the call id, function
name, and textual result — as a permanently pinned entry when
name == USE_SKILL_TOOL_NAME, otherwise as an ordinary compaction-
eligible entry.
Test: transcript::tests::push_tool_result_sets_call_id,
transcript::tests::maybe_compact_never_compacts_pinned_skill_output,
transcript::tests::maybe_compact_still_compacts_non_skill_tool_results.
Sourcepub fn messages(&self) -> Vec<ChatMessage>
pub fn messages(&self) -> Vec<ChatMessage>
Return the complete, uncompacted raw message history.
Why: (#2070) Non-destructive compaction is only meaningful if the full
history stays retrievable for audit even after to_messages starts
collapsing older turns; this is that escape hatch.
What: Clones every entry’s original ChatMessage, in order, regardless
of its compacted flag.
Test: transcript::tests::messages_reflects_appends,
transcript::tests::compacted_entries_remain_in_raw_history.
Sourcepub fn to_messages(&self) -> Vec<ChatMessage>
pub fn to_messages(&self) -> Vec<ChatMessage>
Render the model-facing view: compacted spans collapsed to a summary, plus a replayed last user message once compaction has occurred.
Why: This is what ChatRequest.messages actually sends — it must
shrink once compaction fires (the whole point of §5.4) while staying
a valid, coherently-ordered conversation.
What: Walks entries in order; a run of one-or-more contiguous
compacted entries is replaced by ONE synthetic system-role message
(super::compaction::summarize_span’s one-line text) in its place;
uncompacted entries pass through unchanged. After the walk, if
compaction_triggered is true (i.e. compaction has fired at least
once in this transcript’s life), the LAST user-role entry’s message
is cloned and appended again at the very end — §5.4 item 3’s
“re-anchor the model” replay. A transcript with no user entry (never
happens via seed, but guarded rather than assumed) simply skips the
replay.
Test: transcript::tests::to_messages_collapses_compacted_span,
transcript::tests::to_messages_replays_last_user_message_after_compaction,
transcript::tests::to_messages_is_passthrough_before_compaction.
Sourcepub fn maybe_compact(&mut self, cfg: &CompactionConfig) -> bool
pub fn maybe_compact(&mut self, cfg: &CompactionConfig) -> bool
Apply the compaction policy: mark eligible older entries compacted.
Why: (#2070) This is the turn-boundary hook the agent loop calls —
gated by the caller on HarnessMode::DailyDriver (Parity must never
compact, per §5.9’s D2 reconciliation) — so growth is bounded without
the loop body needing to know compaction’s internals.
What: No-ops (returns false) unless the CURRENT model-facing view
(to_messages(), not the ever-growing raw history — raw size would
re-trigger every turn even after compaction shrank what’s actually
sent) exceeds cfg.token_threshold. Otherwise, marks every entry
before the trailing cfg.keep_last_messages (the active work zone,
§5.4 item 4) as compacted = true, EXCEPT entries already compacted
(a one-way transform), pinned entries (§5.4 item 5’s skill-output
half — see push_tool_result), and system/user role entries
(§5.4 item 5’s “preserve… user requests forever” half). Sets
compaction_triggered and returns true only if at least one entry
was newly marked (an already-fully-compacted or entirely-preserved
prefix is a no-op, not a spurious trigger).
Test: transcript::tests::maybe_compact_marks_middle_span_only,
transcript::tests::maybe_compact_is_noop_below_threshold,
transcript::tests::maybe_compact_never_touches_system_or_user,
transcript::tests::maybe_compact_never_compacts_pinned_skill_output,
transcript::tests::maybe_compact_still_compacts_non_skill_tool_results.
Sourcepub fn compacted_count(&self) -> usize
pub fn compacted_count(&self) -> usize
Number of entries currently marked compacted.
Why: Test/observability accessor proving compaction happened without
exposing the private TranscriptEntry type.
What: Counts entries with compacted == true.
Test: transcript::tests::maybe_compact_marks_middle_span_only.
Sourcepub fn assistant_text(&self) -> String
pub fn assistant_text(&self) -> String
Join all recorded assistant text turns into the final answer string.
Why: The AgentOutput.content should be the model’s prose across the
run, not the raw JSON transcript; joining the text turns yields that.
What: Joins the recorded non-empty assistant texts with blank lines.
Test: transcript::tests::assistant_text_joins_turns.
Sourcepub fn push_response(&mut self, resp: &ChatResponse)
pub fn push_response(&mut self, resp: &ChatResponse)
Convenience accessor: push the assistant turn straight from a response.
Why: Callers usually have a ChatResponse in hand; this saves them
destructuring first_text/first_tool_calls at the call site.
What: Extracts the first choice’s text and tool calls and delegates to
push_assistant.
Test: transcript::tests::push_response_appends_assistant.
Trait Implementations§
Source§impl Clone for Transcript
impl Clone for Transcript
Source§fn clone(&self) -> Transcript
fn clone(&self) -> Transcript
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Transcript
impl Debug for Transcript
Source§impl Default for Transcript
impl Default for Transcript
Source§fn default() -> Transcript
fn default() -> Transcript
Auto Trait Implementations§
impl Freeze for Transcript
impl RefUnwindSafe for Transcript
impl Send for Transcript
impl Sync for Transcript
impl Unpin for Transcript
impl UnsafeUnpin for Transcript
impl UnwindSafe for Transcript
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more