Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

daemon: transcript pages walk only their window under the core lock (#3705)

Sebastian Müllerd3a11eecce7b
diff --git a/crates/pa-daemon/src/session_store.rs b/crates/pa-daemon/src/session_store.rs
index 67c62316a9..46d0691186 100644
--- a/crates/pa-daemon/src/session_store.rs
+++ b/crates/pa-daemon/src/session_store.rs
@@ -112,8 +112,6 @@ pub struct TranscriptPageRows {
/// Rows exist past the page's far side: older ones for an end page, newer
/// ones before `until` for a start page.
pub more: bool,
- /// The newest user prompt's time anywhere on the branch.
- pub last_user_prompt_ms: Option<u64>,
}
/// The windowed sequence's summary scalars (newest timestamp, message count),
@@ -133,6 +131,8 @@ pub struct SessionFile {
pub header: SessionHeader,
pub(crate) entries: Vec<SessionEntry>,
pub(crate) by_id: HashMap<String, usize>,
+ /// Parallel to `entries`: each entry's parent-chain link (maintained with `by_id`).
+ chain: Vec<index::ChainLink>,
pub(crate) leaf_id: Option<String>,
pub(crate) window: Option<SessionWindow>,
pub(crate) trace_upload: Option<std::sync::Arc<pa_core::agent_traces::ContinuousTraceUpload>>,
diff --git a/crates/pa-daemon/src/session_store/index.rs b/crates/pa-daemon/src/session_store/index.rs
index 9f55ac795c..ff3716353a 100644
--- a/crates/pa-daemon/src/session_store/index.rs
+++ b/crates/pa-daemon/src/session_store/index.rs
@@ -68,6 +68,26 @@ pub(super) fn fold_child_usage_attributions(entries: &mut [SessionEntry]) {
}
}
+/// An entry's place on its parent chain, fixed when it joins the index: entries are
+/// append-only and parent links never change, so links need no invalidation.
+#[derive(Debug, Clone, Copy)]
+pub(crate) struct ChainLink {
+ /// A chain root links to itself.
+ parent: usize,
+ depth: usize,
+ jump: usize,
+}
+
+impl ChainLink {
+ pub(super) fn parent(self) -> Option<usize> {
+ (self.depth > 0).then_some(self.parent)
+ }
+
+ pub(super) fn depth(self) -> usize {
+ self.depth
+ }
+}
+
impl SessionFile {
pub(super) fn push_index(&mut self, entry: SessionEntry) {
self.push_index_inner(entry, true);
@@ -84,11 +104,76 @@ impl SessionFile {
if fold_live && entry.type_ == "child_usage_attributed" {
self.fold_live_attribution(&entry);
}
- self.by_id.insert(entry.id.clone(), self.entries.len());
+ self.index_entry(entry);
+ }
+
+ /// The one place an entry joins the index: its `by_id` slot, its chain link, and the leaf.
+ /// The link resolves the parent at push time; a parent not indexed yet starts a new chain
+ /// (strict [`Self::branch`] stops at a missing parent too).
+ pub(crate) fn index_entry(&mut self, entry: SessionEntry) {
+ let position = self.entries.len();
+ let link = match entry
+ .parent_id
+ .as_deref()
+ .and_then(|id| self.by_id.get(id).copied())
+ {
+ Some(parent) => {
+ let up = self.chain[parent];
+ let jump = self.chain[up.jump];
+ // Skew-binary jump pointers (Myers 1983): O(1) to add, O(log N) ancestor lookup.
+ let jump = if up.depth - jump.depth == jump.depth - self.chain[jump.jump].depth {
+ jump.jump
+ } else {
+ parent
+ };
+ ChainLink {
+ parent,
+ depth: up.depth + 1,
+ jump,
+ }
+ }
+ None => ChainLink {
+ parent: position,
+ depth: 0,
+ jump: position,
+ },
+ };
+ self.chain.push(link);
+ self.by_id.insert(entry.id.clone(), position);
self.leaf_id = Some(entry.id.clone());
self.entries.push(entry);
}
+ /// Undo the newest [`Self::index_entry`] (a failed rewrite-persist).
+ pub(super) fn unindex_last(&mut self) {
+ if let Some(entry) = self.entries.pop() {
+ self.by_id.remove(&entry.id);
+ self.chain.pop();
+ }
+ }
+
+ pub(crate) fn clear_index(&mut self) {
+ self.entries.clear();
+ self.by_id.clear();
+ self.chain.clear();
+ self.leaf_id = None;
+ }
+
+ /// The ancestor of `position` at chain depth `depth` (at most the position's own depth).
+ pub(super) fn ancestor_at(&self, mut position: usize, depth: usize) -> usize {
+ while self.chain[position].depth > depth {
+ #[cfg(test)]
+ super::view::CHAIN_STEPS.with(|steps| steps.set(steps.get() + 1));
+ let link = self.chain[position];
+ position = if self.chain[link.jump].depth >= depth {
+ link.jump
+ } else {
+ link.parent
+ };
+ }
+ position
+ }
+
/// Fold one already-indexed attribution entry's aggregate (the rewrite-persist path
/// calls this after the durable write succeeds).
pub(super) fn fold_attribution_id(&mut self, id: &str) {
diff --git a/crates/pa-daemon/src/session_store/page_walk_tests.rs b/crates/pa-daemon/src/session_store/page_walk_tests.rs
new file mode 100644
index 0000000000..1e2152a1e2
--- /dev/null
+++ b/crates/pa-daemon/src/session_store/page_walk_tests.rs
@@ -0,0 +1,387 @@
+//! The transcript page reads only its window: a step counter pins its cost, and a
+//! full-branch reference (the pre-index walk) pins its rows.
+
+use super::*;
+
+fn user(text: &str) -> Value {
+ json!({ "role": "user", "content": text, "timestamp": 1000 })
+}
+
+fn assistant(text: &str) -> Value {
+ json!({
+ "role": "assistant", "provider": "p", "model": "m", "timestamp": 10,
+ "content": [{ "type": "text", "text": text }],
+ })
+}
+
+fn call(id: &str) -> Value {
+ json!({
+ "role": "assistant", "provider": "p", "model": "m", "timestamp": 10,
+ "content": [{ "type": "toolCall", "id": id, "name": "bash", "arguments": {} }],
+ })
+}
+
+fn result(id: &str) -> Value {
+ json!({
+ "role": "toolResult", "toolCallId": id, "toolName": "bash", "timestamp": 12,
+ "content": [{ "type": "text", "text": "out" }], "isError": false,
+ })
+}
+
+fn bash_row() -> Value {
+ json!({
+ "role": "bashExecution", "command": "echo", "output": "", "exitCode": 0,
+ "cancelled": false, "truncated": false, "timestamp": 11,
+ })
+}
+
+/// One turn of varied rows: tool pairs (interleaved, duplicate ids, orphans), custom
+/// rows, and bookkeeping entries that are not display rows.
+fn append_turn(store: &mut SessionFile, turn: usize) {
+ store.append_message(&user(&format!("prompt {turn}")));
+ if turn.is_multiple_of(5) {
+ store.append_model_change("provider", "model");
+ }
+ match turn % 6 {
+ 0 => {
+ store.append_message(&call("a"));
+ store.append_message(&result("a"));
+ }
+ 1 => {
+ let earlier = format!("e{turn}");
+ store.append_message(&call(&earlier));
+ store.append_message(&call("boundary"));
+ store.append_message(&result(&earlier));
+ store.append_message(&bash_row());
+ store.append_message(&result("boundary"));
+ }
+ 2 => {
+ for _ in 0..2 {
+ store.append_entry(
+ "custom_message",
+ json!({ "customType": "note", "content": "c", "display": true }),
+ );
+ }
+ }
+ 3 => {
+ store.append_entry("custom", json!({ "customType": "marker", "data": {} }));
+ store.append_message(&result("orphan"));
+ }
+ 4 if turn == 16 => {
+ for _ in 0..12 {
+ store.append_message(&result("orphan-run"));
+ }
+ }
+ _ => {}
+ }
+ store.append_message(&assistant(&format!("reply {turn}")));
+}
+
+/// Branches, a mid-session compaction, and an abandoned sibling branch.
+fn mixed_store() -> SessionFile {
+ let mut store = SessionFile::create("/tmp", None, 0);
+ let mut kept = None;
+ let mut fork_point = None;
+ for turn in 0..40 {
+ append_turn(&mut store, turn);
+ if turn == 15 {
+ kept.clone_from(&store.leaf_id);
+ }
+ if turn == 20 {
+ store.append_entry(
+ "compaction",
+ json!({ "summary": "s", "firstKeptEntryId": kept, "tokensBefore": 10 }),
+ );
+ }
+ if turn == 30 {
+ fork_point.clone_from(&store.leaf_id);
+ }
+ }
+ store.leaf_id = fork_point;
+ for turn in 40..55 {
+ append_turn(&mut store, turn);
+ }
+ store
+}
+
+/// A compacted file reopened through the window, with rows appended after it.
+fn windowed_store(dir: &std::path::Path) -> SessionFile {
+ let path = dir.join("windowed.jsonl");
+ let mut store = SessionFile::create("/tmp", None, 0);
+ store.set_path(path.clone());
+ let mut kept = None;
+ for turn in 0..60 {
+ append_turn(&mut store, turn);
+ if turn == 45 {
+ kept.clone_from(&store.leaf_id);
+ }
+ }
+ store.append_entry(
+ "compaction",
+ json!({ "summary": "s", "firstKeptEntryId": kept, "tokensBefore": 10 }),
+ );
+ for turn in 60..64 {
+ append_turn(&mut store, turn);
+ }
+ store.rewrite().unwrap();
+ let mut windowed = SessionFile::open_windowed(&path).unwrap();
+ assert!(windowed.window.is_some());
+ for turn in 64..67 {
+ append_turn(&mut windowed, turn);
+ }
+ windowed
+}
+
+/// The pre-index branch: parent-id lookups from the leaf, stopping at a cycle or the
+/// window's excluded rows.
+fn reference_branch(store: &SessionFile) -> Vec<&SessionEntry> {
+ let mut path = Vec::new();
+ let mut visited = std::collections::HashSet::new();
+ let mut current = store.leaf_id.as_deref().and_then(|id| store.entry(id));
+ while let Some(entry) = current {
+ if !visited.insert(entry.id.as_str()) {
+ break;
+ }
+ if let Some(window) = &store.window {
+ let index = store.by_id[&entry.id];
+ if index < window.loaded_entries && !window.retained_ids.contains(&entry.id) {
+ break;
+ }
+ }
+ path.push(entry);
+ current = entry.parent_id.as_deref().and_then(|id| store.entry(id));
+ }
+ path.reverse();
+ path
+}
+
+type PageView = (Vec<Value>, Option<String>, Option<String>, bool);
+
+fn page_view(rows: TranscriptPageRows) -> PageView {
+ (
+ rows.messages,
+ rows.first_entry_id,
+ rows.last_entry_id,
+ rows.more,
+ )
+}
+
+/// The pre-index page: every display row of the whole branch, anchors found by scan,
+/// and the cut settled over all rows.
+fn reference_page(store: &SessionFile, page: &TranscriptPage) -> Option<PageView> {
+ let mut rows: Vec<DisplayRow<'_>> = reference_branch(store)
+ .into_iter()
+ .filter_map(|entry| {
+ let message = if entry.type_ == "compaction" {
+ std::borrow::Cow::Owned(compaction_summary_message(entry, None))
+ } else {
+ entry_message(entry)?
+ };
+ Some((entry.id.as_str(), message))
+ })
+ .collect();
+ let position = |rows: &[DisplayRow<'_>], id: &str| rows.iter().position(|(row, _)| *row == id);
+ let (start, end, more) = match page {
+ TranscriptPage::End { before, limit } => {
+ let end = match before {
+ Some(id) => position(&rows, id)?,
+ None => rows.len(),
+ };
+ let start = settle_cut(&rows, end.saturating_sub(*limit));
+ (start, end, start > 0 || store.window.is_some())
+ }
+ TranscriptPage::Start {
+ after,
+ until,
+ limit,
+ } => {
+ let until = match until {
+ Some(id) => position(&rows, id)?,
+ None => rows.len(),
+ };
+ let start = match after {
+ Some(id) => position(&rows, id)? + 1,
+ None => 0,
+ }
+ .min(until);
+ let end = match start.saturating_add(*limit) {
+ end if end >= until => until,
+ end if end == start => start,
+ end => Some(settle_cut(&rows, end))
+ .filter(|cut| *cut > start)
+ .unwrap_or(until),
+ };
+ (start, end, end < until)
+ }
+ };
+ rows.truncate(end);
+ let page_rows = rows.split_off(start);
+ Some((
+ page_rows
+ .iter()
+ .map(|(_, message)| message.clone().into_owned())
+ .collect(),
+ page_rows.first().map(|(id, _)| (*id).to_string()),
+ page_rows.last().map(|(id, _)| (*id).to_string()),
+ more,
+ ))
+}
+
+fn assert_page(store: &SessionFile, page: &TranscriptPage) -> Option<PageView> {
+ let actual = store.transcript_page(page).map(page_view);
+ assert_eq!(actual, reference_page(store, page), "{page:?}");
+ actual
+}
+
+/// Every page boundary a client reaches (tail-first End pages, head-first Start pages
+/// with and without `until`), plus every entry as an anchor and as `historyLeafId`.
+fn assert_matches_reference(store: &SessionFile) {
+ for entry in store.entries() {
+ assert_eq!(
+ store.branch_contains(&entry.id),
+ reference_branch(store).iter().any(|row| row.id == entry.id),
+ "{}",
+ entry.id
+ );
+ }
+ for limit in [3, 5, 8, 64] {
+ let (_, tail_first, _, mut more) = assert_page(
+ store,
+ &TranscriptPage::End {
+ before: None,
+ limit,
+ },
+ )
+ .unwrap();
+ let mut before = tail_first.clone();
+ while let Some(id) = before.filter(|_| more) {
+ let (_, first, _, older) = assert_page(
+ store,
+ &TranscriptPage::End {
+ before: Some(id),
+ limit,
+ },
+ )
+ .unwrap();
+ (before, more) = (first, older);
+ }
+ for until in [None, tail_first.clone()] {
+ let mut after = None;
+ loop {
+ let (_, _, last, more) = assert_page(
+ store,
+ &TranscriptPage::Start {
+ after: after.clone(),
+ until: until.clone(),
+ limit,
+ },
+ )
+ .unwrap();
+ if !more {
+ break;
+ }
+ after = last;
+ }
+ }
+ for anchor in store.entries().iter().map(|entry| Some(entry.id.clone())) {
+ assert_page(
+ store,
+ &TranscriptPage::End {
+ before: anchor.clone(),
+ limit,
+ },
+ );
+ for until in [None, tail_first.clone()] {
+ assert_page(
+ store,
+ &TranscriptPage::Start {
+ after: anchor.clone(),
+ until,
+ limit,
+ },
+ );
+ }
+ }
+ }
+}
+
+#[test]
+fn pages_match_the_full_branch_reference() {
+ assert_matches_reference(&mixed_store());
+ let dir = tempfile::tempdir().unwrap();
+ assert_matches_reference(&windowed_store(dir.path()));
+}
+
+/// The one rule change: tool calls pair only inside the slice around the cut, so a call
+/// far from its result no longer drags the page back to it.
+#[test]
+fn a_call_more_than_a_page_from_its_result_does_not_move_the_cut() {
+ let mut store = SessionFile::create("/tmp", None, 0);
+ store.append_message(&call("slow"));
+ for turn in 0..100 {
+ store.append_message(&assistant(&format!("reply {turn}")));
+ }
+ store.append_message(&result("slow"));
+ for turn in 0..10 {
+ store.append_message(&assistant(&format!("after {turn}")));
+ }
+ let page = store
+ .transcript_page(&TranscriptPage::End {
+ before: None,
+ limit: 20,
+ })
+ .unwrap();
+ assert_eq!(page.messages.len(), 20);
+ assert!(page.more);
+}
+
+fn steps_for<T>(read: impl FnOnce() -> T) -> (T, usize) {
+ CHAIN_STEPS.with(|steps| steps.set(0));
+ let value = read();
+ (value, CHAIN_STEPS.with(std::cell::Cell::get))
+}
+
+/// A page on a long session walks a number of entries bounded by the page, not the session.
+#[test]
+fn a_page_walks_only_its_window() {
+ let mut store = SessionFile::create("/tmp", None, 0);
+ let mut deep = String::new();
+ let mut turn = 0;
+ while store.entries().len() < 120_000 {
+ append_turn(&mut store, turn % 15 + 50);
+ if turn == 4_000 {
+ deep.clone_from(store.leaf_id.as_ref().unwrap());
+ }
+ turn += 1;
+ }
+ let limit = 64;
+ let bound = 8 * limit + 512;
+ let pages = [
+ TranscriptPage::End {
+ before: None,
+ limit,
+ },
+ TranscriptPage::End {
+ before: Some(deep.clone()),
+ limit,
+ },
+ TranscriptPage::Start {
+ after: Some(deep.clone()),
+ until: None,
+ limit,
+ },
+ TranscriptPage::Start {
+ after: None,
+ until: None,
+ limit,
+ },
+ ];
+ for page in pages {
+ let (rows, steps) = steps_for(|| store.transcript_page(&page));
+ assert!(rows.is_some_and(|rows| rows.messages.len() >= limit / 2));
+ assert!(steps <= bound, "{page:?} took {steps} steps");
+ }
+ let (contains, steps) = steps_for(|| store.branch_contains(&deep));
+ assert!(contains);
+ assert!(steps <= 128, "historyLeafId check took {steps} steps");
+}
diff --git a/crates/pa-daemon/src/session_store/read.rs b/crates/pa-daemon/src/session_store/read.rs
index 272932fc37..35133affb9 100644
--- a/crates/pa-daemon/src/session_store/read.rs
+++ b/crates/pa-daemon/src/session_store/read.rs
@@ -153,6 +153,7 @@ impl SessionFile {
header,
entries: Vec::new(),
by_id: HashMap::new(),
+ chain: Vec::new(),
leaf_id: None,
window: None,
lease: None,
@@ -213,6 +214,7 @@ impl SessionFile {
header,
entries: Vec::new(),
by_id: HashMap::new(),
+ chain: Vec::new(),
leaf_id: None,
window: None,
lease: None,
@@ -313,6 +315,7 @@ impl SessionFile {
header,
entries: Vec::new(),
by_id: HashMap::new(),
+ chain: Vec::new(),
leaf_id: None,
window: None,
lease: None,
diff --git a/crates/pa-daemon/src/session_store/view.rs b/crates/pa-daemon/src/session_store/view.rs
index 9b22105bca..497210c26f 100644
--- a/crates/pa-daemon/src/session_store/view.rs
+++ b/crates/pa-daemon/src/session_store/view.rs
@@ -4,6 +4,19 @@
use super::{json, MessageWindowScalars, SessionEntry, SessionFile, TranscriptPageRows, Value};
use pa_types::daemon::TranscriptPage;
+/// One display-transcript row: its entry id and wire message.
+type DisplayRow<'a> = (&'a str, std::borrow::Cow<'a, Value>);
+
+#[cfg(test)]
+thread_local! {
+ /// Steps the branch walk and the ancestor lookup took on this thread (the page-cost pin).
+ pub(super) static CHAIN_STEPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
+}
+
+#[cfg(test)]
+#[path = "page_walk_tests.rs"]
+mod page_walk_tests;
+
/// Entry types that represent user intent (vs daemon bookkeeping).
const CONTENT_ENTRY_TYPES: &[&str] = &[
"message",
@@ -86,30 +99,92 @@ impl SessionFile {
.and_then(|depth| u32::try_from(depth).ok())
}
- /// Walk the leaf-to-root entry path (the active branch). A corrupt file can
- /// hold a parent cycle; the walk must terminate anyway.
+ /// Walk the leaf-to-root entry path (the active branch), root first.
#[must_use]
pub fn branch(&self) -> Vec<&SessionEntry> {
- let mut path = Vec::new();
- let mut visited = std::collections::HashSet::new();
- let mut current = self.leaf_id.as_deref().and_then(|id| self.entry(id));
- while let Some(entry) = current {
- if !visited.insert(entry.id.as_str()) {
- break;
- }
- if let Some(window) = &self.window {
- let index = self.by_id[&entry.id];
- if index < window.loaded_entries && !window.retained_ids.contains(&entry.id) {
- break;
- }
- }
- path.push(entry);
- current = entry.parent_id.as_deref().and_then(|id| self.entry(id));
- }
+ let mut path: Vec<&SessionEntry> = self
+ .walk_back(self.branch_leaf())
+ .map(|position| &self.entries[position])
+ .collect();
path.reverse();
path
}
+ /// Whether a windowed store's branch walk stops at `position`: a loaded row
+ /// outside the retained window.
+ fn window_excludes(&self, position: usize) -> bool {
+ self.window.as_ref().is_some_and(|window| {
+ position < window.loaded_entries
+ && !window.retained_ids.contains(&self.entries[position].id)
+ })
+ }
+
+ /// The active branch's newest position.
+ fn branch_leaf(&self) -> Option<usize> {
+ self.leaf_id
+ .as_deref()
+ .and_then(|id| self.by_id.get(id).copied())
+ .filter(|&position| !self.window_excludes(position))
+ }
+
+ /// The branch walk's one step: the parent, unless it starts a new chain or the window
+ /// excludes it.
+ fn branch_parent(&self, position: usize) -> Option<usize> {
+ #[cfg(test)]
+ CHAIN_STEPS.with(|steps| steps.set(steps.get() + 1));
+ self.chain[position]
+ .parent()
+ .filter(|&parent| !self.window_excludes(parent))
+ }
+
+ /// Branch positions from `from` back to the branch start, newest first.
+ fn walk_back(&self, from: Option<usize>) -> impl Iterator<Item = usize> + '_ {
+ std::iter::successors(from, move |&position| self.branch_parent(position))
+ }
+
+ /// Branch positions after `after` (exclusive; the branch start when `None`) through
+ /// `last`, oldest first. `after` must be an ancestor of `last`. Reads `last`'s
+ /// ancestors in chunks of doubling size, so the cost follows the rows consumed.
+ fn walk_forward(&self, after: Option<usize>, last: usize) -> impl Iterator<Item = usize> + '_ {
+ let top = self.chain[last].depth();
+ let mut low = after.map_or(0, |after| self.chain[after].depth() + 1);
+ let mut chunk = 64;
+ std::iter::from_fn(move || {
+ if low > top {
+ return None;
+ }
+ let high = top.min(low + chunk - 1);
+ let start = Some(self.ancestor_at(last, high))
+ .filter(|&position| !self.window_excludes(position));
+ let mut positions: Vec<usize> = self.walk_back(start).take(high + 1 - low).collect();
+ positions.reverse();
+ low = high + 1;
+ chunk *= 2;
+ Some(positions)
+ })
+ .flatten()
+ }
+
+ /// Whether `position` is on the active branch: the leaf's ancestor at its depth. A
+ /// window's excluded rows precede every row it keeps, and parents precede children,
+ /// so no excluded row sits between a kept row and the leaf.
+ fn on_branch(&self, position: usize) -> bool {
+ self.branch_leaf().is_some_and(|leaf| {
+ let depth = self.chain[position].depth();
+ depth <= self.chain[leaf].depth()
+ && self.ancestor_at(leaf, depth) == position
+ && !self.window_excludes(position)
+ })
+ }
+
+ /// Whether entry `id` is on the active branch.
+ #[must_use]
+ pub fn branch_contains(&self, id: &str) -> bool {
+ self.by_id
+ .get(id)
+ .is_some_and(|&position| self.on_branch(position))
+ }
+
/// The leaf-to-root walk with parent gaps bridged: a parent id minted but
/// never persisted continues from the gap entry's file predecessor. The strict
/// [`Self::branch`] stays the model-facing truth; this walk serves the usage
@@ -372,82 +447,158 @@ impl SessionFile {
}
}
- /// The display transcript: the whole branch in order, each row with its entry id.
- /// A compaction is its summary row at its own position, so every entry shows once.
- fn display_rows(&self) -> Vec<(&str, std::borrow::Cow<'_, Value>)> {
- self.branch()
- .into_iter()
- .filter_map(|entry| {
- let message = if entry.type_ == "compaction" {
- std::borrow::Cow::Owned(compaction_summary_message(entry, None))
- } else {
- entry_message(entry)?
- };
- Some((entry.id.as_str(), message))
- })
- .collect()
+ /// One display-transcript row: a message-bearing entry, or a compaction as its summary
+ /// row in place, so every entry shows once.
+ fn display_row(&self, position: usize) -> Option<DisplayRow<'_>> {
+ let entry = &self.entries[position];
+ let message = if entry.type_ == "compaction" {
+ std::borrow::Cow::Owned(compaction_summary_message(entry, None))
+ } else {
+ entry_message(entry)?
+ };
+ Some((entry.id.as_str(), message))
+ }
+
+ /// The position of page anchor `id`: a display row on the active branch.
+ fn display_anchor(&self, id: &str) -> Option<usize> {
+ let position = *self.by_id.get(id)?;
+ let entry = &self.entries[position];
+ ((entry.type_ == "compaction" || entry_bears_message(entry)) && self.on_branch(position))
+ .then_some(position)
}
/// One page of the display transcript; `None` when an anchor row is not on the branch.
+ /// The page walks from its anchor, so its cost follows the page, not the session.
#[must_use]
pub fn transcript_page(&self, page: &TranscriptPage) -> Option<TranscriptPageRows> {
- let mut rows = self.display_rows();
- let position = |rows: &[(&str, std::borrow::Cow<'_, Value>)], id: &str| {
- rows.iter().position(|(row_id, _)| *row_id == id)
- };
- let (start, end, more) = match page {
- TranscriptPage::End { before, limit } => {
- let end = match before {
- Some(id) => position(&rows, id)?,
- None => rows.len(),
- };
- let start = settle_cut(&rows, end.saturating_sub(*limit));
- // A windowed store's unloaded prefix is older history too.
- (start, end, start > 0 || self.window.is_some())
- }
+ let (rows, more) = match page {
+ TranscriptPage::End { before, limit } => self.end_page(before.as_deref(), *limit)?,
TranscriptPage::Start {
after,
until,
limit,
- } => {
- let until = match until {
- Some(id) => position(&rows, id)?,
- None => rows.len(),
- };
- let start = match after {
- Some(id) => position(&rows, id)? + 1,
- None => 0,
- }
- .min(until);
- let end = match start.saturating_add(*limit) {
- end if end >= until => until,
- end if end == start => start,
- end => Some(settle_cut(&rows, end))
- .filter(|cut| *cut > start)
- .unwrap_or(until),
- };
- (start, end, end < until)
- }
+ } => self.start_page(after.as_deref(), until.as_deref(), *limit)?,
};
- let last_user_prompt_ms = rows
- .iter()
- .rev()
- .find(|(_, message)| message.get("role").and_then(Value::as_str) == Some("user"))
- .and_then(|(_, message)| prompt_timestamp_ms(message));
- rows.truncate(end);
- let page_rows = rows.split_off(start);
Some(TranscriptPageRows {
- first_entry_id: page_rows.first().map(|(id, _)| (*id).to_string()),
- last_entry_id: page_rows.last().map(|(id, _)| (*id).to_string()),
- messages: page_rows
+ first_entry_id: rows.first().map(|(id, _)| (*id).to_string()),
+ last_entry_id: rows.last().map(|(id, _)| (*id).to_string()),
+ messages: rows
.into_iter()
.map(|(_, message)| message.into_owned())
.collect(),
more,
- last_user_prompt_ms,
})
}
+ /// The newest `limit` rows before the `before` row (the tail without it).
+ fn end_page(&self, before: Option<&str>, limit: usize) -> Option<(Vec<DisplayRow<'_>>, bool)> {
+ let before = before.map_or(Some(None), |id| self.display_anchor(id).map(Some))?;
+ let from = match before {
+ Some(position) => self.branch_parent(position),
+ None => self.branch_leaf(),
+ };
+ let mut older = self
+ .walk_back(from)
+ .filter_map(|position| self.display_row(position));
+ // Newest first: the `before` row, the page, the run that moves its cut back,
+ // then the rows the cut's tool calls may pair with.
+ let mut slice: Vec<DisplayRow<'_>> = before
+ .and_then(|position| self.display_row(position))
+ .into_iter()
+ .collect();
+ slice.extend(older.by_ref().take(limit));
+ while slice
+ .last()
+ .is_some_and(|(_, message)| continues_run(message))
+ {
+ let Some(row) = older.next() else {
+ break;
+ };
+ slice.push(row);
+ }
+ let from_cut = slice.len();
+ slice.extend(older.by_ref().take(limit));
+ slice.reverse();
+ let end = slice.len() - usize::from(before.is_some());
+ let start = settle_cut(&slice, slice.len() - from_cut);
+ // A windowed store's unloaded prefix is older history too.
+ let more = start > 0 || self.window.is_some() || older.next().is_some();
+ slice.truncate(end);
+ slice.drain(..start);
+ Some((slice, more))
+ }
+
+ /// The oldest `limit` rows after the `after` row (the head without it), stopping
+ /// before the `until` row.
+ fn start_page(
+ &self,
+ after: Option<&str>,
+ until: Option<&str>,
+ limit: usize,
+ ) -> Option<(Vec<DisplayRow<'_>>, bool)> {
+ let until = until.map_or(Some(None), |id| self.display_anchor(id).map(Some))?;
+ let after = after.map_or(Some(None), |id| self.display_anchor(id).map(Some))?;
+ let Some(last) = until.or_else(|| self.branch_leaf()) else {
+ return Some((Vec::new(), false));
+ };
+ // The page, then up to `limit` rows past it (through the `until` row) for pairing.
+ let mut newer = self
+ .walk_forward(after, last)
+ .filter_map(|position| Some((position, self.display_row(position)?)));
+ let rows: Vec<(usize, DisplayRow<'_>)> = newer
+ .by_ref()
+ .take(limit.saturating_mul(2).saturating_add(1))
+ .collect();
+ let reached_until = rows
+ .last()
+ .is_some_and(|(position, _)| Some(*position) == until);
+ let available = rows.len() - usize::from(reached_until);
+ let mut rows: Vec<DisplayRow<'_>> = rows.into_iter().map(|(_, row)| row).collect();
+ if available <= limit {
+ rows.truncate(available);
+ return Some((rows, false));
+ }
+ if limit == 0 {
+ return Some((Vec::new(), true));
+ }
+ // The `limit` rows before the page, for calls whose results the cut would split.
+ let mut slice: Vec<DisplayRow<'_>> = self
+ .walk_back(after)
+ .filter_map(|position| self.display_row(position))
+ .take(limit)
+ .collect();
+ slice.reverse();
+ let page_start = slice.len();
+ slice.append(&mut rows);
+ let cut = settle_cut(&slice, page_start + limit);
+ let mut page = slice.split_off(page_start);
+ if cut > page_start {
+ page.truncate(cut - page_start);
+ return Some((page, true));
+ }
+ // The cut fell back to the page start: the page runs to the `until` row.
+ page.extend(newer.map(|(_, row)| row));
+ if until.is_some() {
+ page.pop();
+ }
+ Some((page, false))
+ }
+
+ /// The newest user prompt's time on the branch (the attach snapshot's marker); the walk
+ /// reaches back only as far as that prompt.
+ #[must_use]
+ pub fn last_user_prompt_ms(&self) -> Option<u64> {
+ self.walk_back(self.branch_leaf())
+ .find_map(|position| {
+ let entry = &self.entries[position];
+ (entry.type_ == "message")
+ .then(|| entry.fields.get("message"))
+ .flatten()
+ .filter(|message| message_role(message) == Some("user"))
+ })
+ .and_then(prompt_timestamp_ms)
+ }
+
/// The durable entry id the compaction cut keeps: the engine's
/// `firstKeptEntryId` references in-memory ids that never exist in the file —
/// the re-cut pins the boundary the file read recognizes.
@@ -591,11 +742,21 @@ fn prompt_timestamp_ms(message: &Value) -> Option<u64> {
}
}
+/// Whether a row continues the run before it: a page never starts at one.
+fn continues_run(message: &Value) -> bool {
+ matches!(message_role(message), Some("toolResult" | "custom"))
+}
+
/// Move a page boundary earlier until the page below it starts at neither a tool
/// result nor a custom row, and no tool call is split from its result.
-fn settle_cut(rows: &[(&str, std::borrow::Cow<'_, Value>)], mut cut: usize) -> usize {
- let role = |index: usize| rows[index].1.get("role").and_then(Value::as_str);
- while cut > 0 && cut < rows.len() && matches!(role(cut), Some("toolResult" | "custom")) {
+///
+/// `rows` is a local slice of the branch around the cut, not the whole branch: the
+/// page, the run before its first row, and up to `limit` rows past each side, the
+/// side toward the request's other anchor stopping at that anchor row. Calls pair
+/// with results only inside the slice, so a call more than a page from its result
+/// does not move the cut (else an orphan or aborted call drags every page to the root).
+fn settle_cut(rows: &[DisplayRow<'_>], mut cut: usize) -> usize {
+ while cut > 0 && cut < rows.len() && continues_run(&rows[cut].1) {
cut -= 1;
}
// User bash and other message rows can land between a call and its
diff --git a/crates/pa-daemon/src/session_store/write.rs b/crates/pa-daemon/src/session_store/write.rs
index 7cb17d43fc..b297325bd0 100644
--- a/crates/pa-daemon/src/session_store/write.rs
+++ b/crates/pa-daemon/src/session_store/write.rs
@@ -294,8 +294,7 @@ impl SessionFile {
Ok(())
});
if let Err(error) = rewritten {
- self.by_id.remove(&id);
- self.entries.pop();
+ self.unindex_last();
self.leaf_id = previous_leaf;
return Err(error);
}
diff --git a/crates/pa-daemon/src/session_tree.rs b/crates/pa-daemon/src/session_tree.rs
index a74568b828..7c4f37644d 100644
--- a/crates/pa-daemon/src/session_tree.rs
+++ b/crates/pa-daemon/src/session_tree.rs
@@ -254,7 +254,7 @@ impl SessionFile {
}
for entry in &path_without_labels {
- forked.adopt_entry((*entry).clone());
+ forked.index_entry((*entry).clone());
}
// Re-record the carried labels after the path (TS appends them
// parented onto the path tail, keeping their original timestamps).
@@ -273,7 +273,7 @@ impl SessionFile {
fields: json!({ "targetId": id, "label": label }),
};
parent = Some(entry.id.clone());
- forked.adopt_entry(entry);
+ forked.index_entry(entry);
}
}
forked.hydrate_anthropic_warning_flag();
@@ -301,13 +301,6 @@ impl SessionFile {
(!path.is_empty()).then_some(path)
}
- /// Adopt an entry as-is (used by the in-memory fork variant).
- fn adopt_entry(&mut self, entry: SessionEntry) {
- self.by_id.insert(entry.id.clone(), self.entries.len());
- self.leaf_id = Some(entry.id.clone());
- self.entries.push(entry);
- }
-
/// In-memory fork: replace this store's entries with the root-to-`leaf_id`
/// path, carrying the labels of the kept entries.
///
@@ -330,11 +323,9 @@ impl SessionFile {
.iter()
.map(|entry| entry.id.clone())
.collect();
- self.entries.clear();
- self.by_id.clear();
- self.leaf_id = None;
+ self.clear_index();
for entry in path_without_labels {
- self.adopt_entry(entry);
+ self.index_entry(entry);
}
let mut parent = self.leaf_id.clone();
for id in &path_ids {
@@ -351,7 +342,7 @@ impl SessionFile {
fields: json!({ "targetId": id, "label": label }),
};
parent = Some(entry.id.clone());
- self.adopt_entry(entry);
+ self.index_entry(entry);
}
}
// The kept path decides the gate from here on (the in-memory fork
diff --git a/crates/pa-daemon/src/worker/commands.rs b/crates/pa-daemon/src/worker/commands.rs
index aace373496..8aef0523ff 100644
--- a/crates/pa-daemon/src/worker/commands.rs
+++ b/crates/pa-daemon/src/worker/commands.rs
@@ -497,9 +497,7 @@ impl Worker {
// Validate ancestry atomically with the page read. Appends keep the
// attached leaf on this branch; navigation to another branch does not.
let history_leaf = match payload.get("historyLeafId").map(Value::as_str) {
- Some(Some(leaf)) if store.branch().iter().any(|entry| entry.id == leaf) => {
- leaf.to_string()
- }
+ Some(Some(leaf)) if store.branch_contains(leaf) => leaf.to_string(),
Some(_) => {
return response_failure(None, "get_messages", "History branch changed", None);
}
diff --git a/crates/pa-daemon/src/worker/connection.rs b/crates/pa-daemon/src/worker/connection.rs
index 995f7c9d9f..3166cd6f93 100644
--- a/crates/pa-daemon/src/worker/connection.rs
+++ b/crates/pa-daemon/src/worker/connection.rs
@@ -846,10 +846,13 @@ impl Worker {
limit: ATTACH_TAIL_MESSAGE_BUDGET,
})
.map_or((Vec::new(), None, None), |page| {
+ // The resume marker only rides a snapshot with older history.
+ let last_user_prompt_ms =
+ page.more.then(|| store.last_user_prompt_ms()).flatten();
(
page.messages,
page.first_entry_id.filter(|_| page.more),
- page.last_user_prompt_ms,
+ last_user_prompt_ms,
)
}),
Some(store) => (store.messages(), None, None),