diff --git a/crates/pa-core/src/lib.rs b/crates/pa-core/src/lib.rs index 6e38e5717..7bc7c3488 100644 --- a/crates/pa-core/src/lib.rs +++ b/crates/pa-core/src/lib.rs @@ -72,6 +72,7 @@ pub mod resources; pub mod session; pub mod session_engine; pub mod session_log; +pub mod session_runtime; pub mod settings; pub mod skills; pub mod slash_command_args; diff --git a/crates/pa-core/src/session_runtime/index.rs b/crates/pa-core/src/session_runtime/index.rs new file mode 100644 index 000000000..824cefba5 --- /dev/null +++ b/crates/pa-core/src/session_runtime/index.rs @@ -0,0 +1,154 @@ +//! The index the runtime keeps over the log: ids, parents, leaves, stamps. + +use std::collections::{HashMap, HashSet}; + +use serde_json::Value; + +use super::{envelope, Error, SELECTION}; + +/// One indexed row: the fields the runtime must know, plus the body. +#[derive(Debug, Clone, PartialEq)] +pub struct Entry { + /// The row's position in the log. + pub seq: u64, + /// The writer's stamp from the row envelope, as the log recorded it. + pub ts: String, + /// The row's own id, unique within the session. + pub id: String, + /// The id of the row this one follows, if any. + pub parent: Option, + /// The row's ISO-8601 stamp, when it carries a string one. + pub timestamp: Option, + /// The row body, opaque to the runtime. + pub body: Value, +} + +/// What indexing one row did to the tree. +pub(crate) enum Applied { + /// A branch node; `parent` is where it hangs. + Tree { id: String, parent: Option }, + /// A selection marker naming the branch leaf to select. + Selection { leaf: String }, + /// A row the index already holds. + Already, +} + +/// Rows by seq, by id, and the parent/child edges of the session tree. +/// +/// Selection markers are indexed (they are rows with ids and seqs) but they are +/// not tree nodes: they neither become leaves nor give their target children. +#[derive(Debug, Default)] +pub(crate) struct Index { + entries: Vec, + by_id: HashMap, + children: HashMap, + markers: HashSet, + last_seq: u64, +} + +impl Index { + /// Index one row, or report that its seq is already held. + pub(crate) fn insert(&mut self, seq: u64, ts: String, body: Value) -> Result { + if self.by_seq(seq).is_some() { + return Ok(Applied::Already); + } + let envelope = envelope(&body)?; + if self.by_id.contains_key(&envelope.id) { + return Err(Error::DuplicateId { id: envelope.id }); + } + let entry = Entry { + seq, + ts, + id: envelope.id, + parent: envelope.parent, + timestamp: envelope.timestamp, + body, + }; + let index = self.entries.len(); + let applied = if entry.body.get("type").and_then(Value::as_str) == Some(SELECTION) { + let leaf = entry + .body + .get("leafId") + .and_then(Value::as_str) + .ok_or_else(|| Error::Malformed { + field: "leafId", + detail: entry.body.to_string(), + })? + .to_owned(); + self.markers.insert(index); + Applied::Selection { leaf } + } else { + if let Some(parent) = &entry.parent { + *self.children.entry(parent.clone()).or_insert(0) += 1; + } + Applied::Tree { + id: entry.id.clone(), + parent: entry.parent.clone(), + } + }; + self.by_id.insert(entry.id.clone(), index); + self.last_seq = self.last_seq.max(seq); + self.entries.push(entry); + Ok(applied) + } + + pub(crate) fn rows(&self) -> &[Entry] { + &self.entries + } + + pub(crate) fn last_seq(&self) -> u64 { + self.last_seq + } + + pub(crate) fn get(&self, id: &str) -> Option<&Entry> { + self.by_id.get(id).map(|&index| &self.entries[index]) + } + + pub(crate) fn by_seq(&self, seq: u64) -> Option<&Entry> { + self.entries + .binary_search_by_key(&seq, |entry| entry.seq) + .ok() + .map(|index| &self.entries[index]) + } + + /// A branch may end here: a tree node with no children. + pub(crate) fn is_leaf(&self, id: &str) -> bool { + let Some(&index) = self.by_id.get(id) else { + return false; + }; + !self.markers.contains(&index) && !self.children.contains_key(id) + } + + pub(crate) fn leaves(&self) -> Vec<&str> { + self.entries + .iter() + .filter(|entry| self.is_leaf(&entry.id)) + .map(|entry| entry.id.as_str()) + .collect() + } + + /// The newest leaf, for a log whose first rows start no branch. + pub(crate) fn tip(&self) -> Option { + self.entries + .iter() + .rev() + .find(|entry| self.is_leaf(&entry.id)) + .map(|entry| entry.id.clone()) + } + + /// The chain from the root to `leaf`. + pub(crate) fn branch(&self, leaf: &str) -> Vec<&Entry> { + let mut chain = Vec::new(); + let mut cursor = self.get(leaf); + while let Some(entry) = cursor { + // A parent chain that loops is cut rather than followed forever. + if chain.len() > self.entries.len() { + break; + } + chain.push(entry); + cursor = entry.parent.as_deref().and_then(|parent| self.get(parent)); + } + chain.reverse(); + chain + } +} diff --git a/crates/pa-core/src/session_runtime/mod.rs b/crates/pa-core/src/session_runtime/mod.rs new file mode 100644 index 000000000..65c4371fe --- /dev/null +++ b/crates/pa-core/src/session_runtime/mod.rs @@ -0,0 +1,499 @@ +//! The session runtime: one log, one in-memory index, one selected branch. +//! +//! The log core stores rows and knows nothing about sessions. This runtime +//! replays those rows into the index a session needs -- id, parent id, leaves, +//! selection, timestamps -- and projects the selected branch into provider +//! messages. +//! +//! Rows are opaque except for the fields the session layer writes: `id`, +//! `parentId`, `timestamp`, and for messages `type: "message"` with +//! `message.role` and `message.content`. +//! +//! No epoch, no acknowledgement, no lease, no retry policy. The index changes +//! when the log says [`Outcome::Committed`], and on [`Outcome::Unknown`] the +//! runtime believes the log, never the intent. A branch selection is an +//! ordinary [`SELECTION`] row, so replay restores it. + +mod index; +mod projection; +#[cfg(test)] +mod tests; + +use std::path::{Path, PathBuf}; +use std::time::Instant; + +use serde_json::{json, Value}; +use uuid::Uuid; + +// The log core. It ships as the `pa-session-log` crate; while its module path +// is still `pa_core::session_log`, this alias is the one line that moves. +pub(crate) use crate::session_log as log_core; +use log_core::{Outcome, RepairReport, Row, SessionLog}; + +use index::{Applied, Index}; + +pub use index::Entry; +pub use projection::ProviderMessage; + +/// The row `type` a branch selection is written as. +pub(crate) const SELECTION: &str = "branch_selection"; + +/// Why the runtime could not do what was asked. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// The log core failed or refused the operation. + #[error("session log: {0}")] + Log(#[from] log_core::Error), + /// The row's id is already in the session. + #[error("row id {id:?} is already in the session")] + DuplicateId { id: String }, + /// A row is missing a field the index needs. + #[error("row field {field} is not usable: {detail}")] + Malformed { field: &'static str, detail: String }, + /// A branch must end at a leaf. + #[error("row {id:?} has children and is not a branch leaf")] + NotALeaf { id: String }, + /// No row has that id. + #[error("no row with id {id:?}")] + NoSuchRow { id: String }, + /// The log said the row committed but does not hold it. + #[error("the log reported seq {seq} committed but does not hold it")] + MissingRow { seq: u64 }, + /// The core's verdict decided nothing, so there is nothing to apply. + #[error("the log could not decide the undecided append")] + Undecided, +} + +/// One opened session: the replayed runtime and the repair `open` performed. +pub struct Opened { + /// The replayed runtime. + pub runtime: SessionRuntime, + /// The repair applied to a torn or corrupt tail, when there was one. + pub repair: Option, +} + +/// A session's in-memory view over its log. +pub struct SessionRuntime { + dir: PathBuf, + id: String, + log: SessionLog, + index: Index, + selected: Option, + pending: Option, +} + +/// An append whose durability the log has not decided. +/// +/// The seq survives a failed `recover()`, so the retry is pinned to it and the +/// core answers from the log's bytes: a write that landed is reported instead +/// of repeated under the same id. +struct Pending { + seq: u64, + id: String, +} + +impl SessionRuntime { + /// Open (creating when absent) session `id` in `dir` and replay its log. + /// + /// A torn or corrupt tail is repaired before the replay and returned, so + /// the caller can report it. + /// + /// # Errors + /// + /// [`Error::Log`] when the lock is held, the file cannot be read, or the + /// tail could not be repaired. + pub fn open(dir: &Path, id: &str, deadline: Instant) -> Result { + match SessionLog::open(dir, id, deadline) { + Ok(log) => Self::replay(dir, id, log, None), + Err(log_core::Error::Damaged { .. } | log_core::Error::Corrupt { .. }) => { + let repair = SessionLog::repair(dir, id, deadline)?; + tracing::warn!( + session = id, + last_good_seq = repair.last_good_seq, + truncated_bytes = repair.truncated_bytes, + "repaired a torn session log tail on open" + ); + let log = SessionLog::open(dir, id, deadline)?; + Self::replay(dir, id, log, Some(repair)) + } + Err(error) => Err(error.into()), + } + } + + fn replay( + dir: &Path, + id: &str, + log: SessionLog, + repair: Option, + ) -> Result { + let mut runtime = Self { + dir: dir.to_path_buf(), + id: id.to_owned(), + log, + index: Index::default(), + selected: None, + pending: None, + }; + for row in runtime.log.iter_since(0)? { + let row = row?; + runtime.absorb(row.seq, row.ts, row.body)?; + } + if runtime.selected.is_none() { + runtime.selected = runtime.index.tip(); + } + Ok(Opened { runtime, repair }) + } + + /// Append `row` at the log's next seq. + /// + /// A row the session already holds is a retry and resolves to its seq, even + /// after an undecided attempt. Otherwise the next seq is pinned with + /// `append_at`; a seq another writer took costs one tail read and one + /// retry, never a duplicate id. + /// + /// A commit is read back from the file, so rows other writers committed are + /// indexed too. On [`Outcome::Unknown`] the core decides; if that decision + /// fails, the undecided seq stays pending for the retry. + /// + /// # Errors + /// + /// [`Error::DuplicateId`] for a different row with that id, + /// [`Error::Malformed`] for an unusable row, [`Error::Log`] for log + /// failures, and the error of a `recover()` that could not run. + pub fn append(&mut self, row: &Value, deadline: Instant) -> Result { + let envelope = envelope(row)?; + if let Some(outcome) = self.retried(&envelope.id, row)? { + return Ok(outcome); + } + if let Some(pending) = &self.pending { + if pending.id == envelope.id { + // Finish the undecided write at its own seq before anything else. + let seq = pending.seq; + let outcome = self.log.append_at(seq, row, deadline); + return self.commit(outcome, row, deadline); + } + } + let mut outcome = self.log.append_at(self.index.last_seq() + 1, row, deadline); + if is_seq_conflict(&outcome) { + // Another writer took the seq we pinned: read its rows, then retry. + self.sync_tail(deadline)?; + if let Some(outcome) = self.retried(&envelope.id, row)? { + return Ok(outcome); + } + outcome = self.log.append_at(self.index.last_seq() + 1, row, deadline); + } + self.commit(outcome, row, deadline) + } + + /// The outcome a retry of `row` resolves to when the session already holds + /// its id, or `None` when the id is free. + fn retried(&mut self, id: &str, row: &Value) -> Result, Error> { + let Some(held) = self.index.get(id) else { + return Ok(None); + }; + if held.body != *row { + return Err(Error::DuplicateId { id: id.to_owned() }); + } + let (seq, ts) = (held.seq, held.ts.clone()); + self.resolve_pending(id); + Ok(Some(Outcome::Committed { seq, ts })) + } + + /// Append `row` at `seq`, for a caller retrying an undecided write. + /// + /// The core answers from the log when `seq` already holds the same body, so + /// the retry is idempotent without an acknowledgement. + /// + /// # Errors + /// + /// [`Error::DuplicateId`] when the id sits at a different seq, + /// [`Error::Malformed`], and [`Error::Log`]. + pub fn append_at( + &mut self, + seq: u64, + row: &Value, + deadline: Instant, + ) -> Result { + let envelope = envelope(row)?; + if self + .index + .get(&envelope.id) + .is_some_and(|held| held.seq != seq) + { + return Err(Error::DuplicateId { id: envelope.id }); + } + let outcome = self.log.append_at(seq, row, deadline); + self.commit(outcome, row, deadline) + } + + /// Select the branch that ends at `leaf`, by appending a selection row. + /// + /// The switch takes effect when the log commits it, so it survives a + /// restart the way every other row does. + /// + /// # Errors + /// + /// [`Error::NoSuchRow`], [`Error::NotALeaf`], and [`Error::Log`]. + pub fn select(&mut self, leaf: &str, deadline: Instant) -> Result { + if self.index.get(leaf).is_none() { + return Err(Error::NoSuchRow { + id: leaf.to_owned(), + }); + } + if !self.index.is_leaf(leaf) { + return Err(Error::NotALeaf { + id: leaf.to_owned(), + }); + } + let marker = format!("selection-{}", Uuid::new_v4()); + let row = json!({ + "type": SELECTION, + "id": marker, + "parentId": leaf, + "leafId": leaf, + }); + let mut outcome = self + .log + .append_at(self.index.last_seq() + 1, &row, deadline); + if is_seq_conflict(&outcome) { + self.sync_tail(deadline)?; + outcome = self + .log + .append_at(self.index.last_seq() + 1, &row, deadline); + } + self.commit(outcome, &row, deadline) + } + + /// Committed rows with `seq > since`, read from the log itself. + /// + /// # Errors + /// + /// [`Error::Log`] for I/O failures. + pub fn read_since(&self, since: u64) -> Result, Error> { + Ok(self.log.read_since(since)?) + } + + /// The id of the row the selected branch ends at, when the session has one. + #[must_use] + pub fn leaf(&self) -> Option<&str> { + self.selected.as_deref() + } + + /// Every row id a branch could end at, in log order. + #[must_use] + pub fn leaves(&self) -> Vec<&str> { + self.index.leaves() + } + + /// The selected branch, root first. + #[must_use] + pub fn branch(&self) -> Vec<&Entry> { + self.leaf() + .map_or_else(Vec::new, |leaf| self.index.branch(leaf)) + } + + /// The provider message list for the selected branch, root first. + /// + /// Rebuilt on every call: nothing is cached, so a branch switch or an + /// append is visible immediately. + #[must_use] + pub fn projection(&self) -> Vec { + projection::messages(&self.branch()) + } + + /// Every indexed row, in log order. + #[must_use] + pub fn rows(&self) -> &[Entry] { + self.index.rows() + } + + /// The indexed row with `id`. + #[must_use] + pub fn row(&self, id: &str) -> Option<&Entry> { + self.index.get(id) + } + + /// The seq of an append whose durability is still undecided, if any. + /// + /// A caller can retry the same row with [`append_at`](Self::append_at) at + /// this seq; the core answers from the log, so no second row can appear. + #[must_use] + pub fn pending_seq(&self) -> Option { + self.pending.as_ref().map(|pending| pending.seq) + } + + /// The seq of the newest row the index holds; zero for an empty session. + #[must_use] + pub fn last_seq(&self) -> u64 { + self.index.last_seq() + } + + /// Apply a committed row, or reconcile what the log decided. + /// + /// Not applied from the intent: the tail is read first, so rows this + /// runtime did not write are indexed too and the row at `seq` is the file's. + fn commit( + &mut self, + outcome: Outcome, + intended: &Value, + deadline: Instant, + ) -> Result { + let id = envelope(intended)?.id; + match outcome { + Outcome::Committed { seq, ts } => { + self.resolve_pending(&id); + self.reconcile(Outcome::Committed { seq, ts }, deadline) + } + Outcome::Unknown { recover } => { + self.pending = Some(Pending { + seq: recover.seq(), + id, + }); + // A failed decision propagates with the seq still pending, so a + // retry resolves it against the log. + let decided = recover.recover(deadline)?; + let resolved = self.reconcile(decided, deadline)?; + self.pending = None; + Ok(resolved) + } + other @ (Outcome::NotWritten { .. } | Outcome::Busy { .. }) => { + self.resolve_pending(&id); + Ok(other) + } + } + } + + fn resolve_pending(&mut self, id: &str) { + if self + .pending + .as_ref() + .is_some_and(|pending| pending.id == id) + { + self.pending = None; + } + } + + /// Turn the core's verdict on an undecided append into an indexed outcome. + /// + /// A `Committed` verdict is checked against the log before anything is + /// applied: the index learns the row from the file, not from the intent. + pub(crate) fn reconcile( + &mut self, + decided: Outcome, + deadline: Instant, + ) -> Result { + match decided { + Outcome::Committed { seq, .. } => { + self.sync_tail(deadline)?; + let entry = self.index.by_seq(seq).ok_or(Error::MissingRow { seq })?; + Ok(Outcome::Committed { + seq, + ts: entry.ts.clone(), + }) + } + Outcome::NotWritten { reason } => Ok(Outcome::NotWritten { reason }), + Outcome::Busy { until, holder } => Ok(Outcome::Busy { until, holder }), + Outcome::Unknown { .. } => Err(Error::Undecided), + } + } + + /// Read what the log holds beyond the index's newest row. + /// + /// A complete but invalid line is damage, not a verdict: the tail is + /// repaired and reading resumes from the index's newest row. + pub(crate) fn sync_tail(&mut self, deadline: Instant) -> Result<(), Error> { + loop { + let newest = self.index.last_seq(); + let rows = self.log.iter_since(newest)?; + let mut corrupt = false; + for row in rows { + match row { + Ok(row) => self.absorb(row.seq, row.ts, row.body)?, + Err(log_core::Error::Corrupt { after_seq }) => { + tracing::warn!( + session = self.id, + after_seq, + "repaired a corrupt session log line" + ); + corrupt = true; + break; + } + Err(error) => return Err(error.into()), + } + } + if !corrupt { + return Ok(()); + } + SessionLog::repair(&self.dir, &self.id, deadline)?; + self.log = SessionLog::open(&self.dir, &self.id, deadline)?; + } + } + + /// Index one row and follow the branch selection it implies. + fn absorb(&mut self, seq: u64, ts: String, body: Value) -> Result<(), Error> { + match self.index.insert(seq, ts, body)? { + Applied::Tree { id, parent } => { + let extends_selection = match &self.selected { + Some(selected) => parent.as_deref() == Some(selected.as_str()), + None => parent.is_none(), + }; + if extends_selection { + self.selected = Some(id); + } + } + Applied::Selection { leaf } => { + if self.index.get(&leaf).is_some() { + self.selected = Some(leaf); + } + } + Applied::Already => {} + } + Ok(()) + } +} + +/// Whether the log refused the pinned seq because another row names it. +fn is_seq_conflict(outcome: &Outcome) -> bool { + matches!( + outcome, + Outcome::NotWritten { + reason: log_core::NotWritten::SeqConflict { .. }, + } + ) +} + +/// The fields of a row the index reads. +pub(crate) struct Envelope { + pub(crate) id: String, + pub(crate) parent: Option, + pub(crate) timestamp: Option, +} + +/// Read the indexed fields of `body`. +pub(crate) fn envelope(body: &Value) -> Result { + let unusable = |field| Error::Malformed { + field, + detail: body.to_string(), + }; + let id = body + .get("id") + .and_then(Value::as_str) + .ok_or_else(|| unusable("id"))? + .to_owned(); + let parent = match body.get("parentId") { + None | Some(Value::Null) => None, + Some(Value::String(parent)) => Some(parent.clone()), + Some(_) => return Err(unusable("parentId")), + }; + // Stamps are informational: a row that carries something else is indexed + // without one instead of making the session unopenable. + let timestamp = body + .get("timestamp") + .and_then(Value::as_str) + .map(str::to_owned); + Ok(Envelope { + id, + parent, + timestamp, + }) +} diff --git a/crates/pa-core/src/session_runtime/projection.rs b/crates/pa-core/src/session_runtime/projection.rs new file mode 100644 index 000000000..4aac4c5df --- /dev/null +++ b/crates/pa-core/src/session_runtime/projection.rs @@ -0,0 +1,60 @@ +//! The model-facing view of one branch: provider messages, rebuilt per call. + +use serde_json::Value; + +use super::Entry; + +/// One message as the provider list takes it, with the row's role verbatim. +/// +/// Mapping a role onto a provider's own vocabulary is the caller's job; this +/// projection only fixes the order and the text. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ProviderMessage { + /// `user`, `assistant`, `toolResult`, or whatever the row named. + pub role: String, + /// The message text: a string content, or its text parts joined by newlines. + pub content: String, +} + +/// The projection of `branch` (root first): message rows with text, in log +/// order. Every other row -- usage, labels, selections, tool bookkeeping -- is +/// not a message and does not reach the provider. +pub(crate) fn messages(branch: &[&Entry]) -> Vec { + branch + .iter() + .filter_map(|entry| message(&entry.body)) + .collect() +} + +fn message(body: &Value) -> Option { + if body.get("type").and_then(Value::as_str) != Some("message") { + return None; + } + let message = body.get("message")?; + let role = message.get("role")?.as_str()?; + let content = text(message.get("content")?)?; + Some(ProviderMessage { + role: role.to_owned(), + content, + }) +} + +fn text(content: &Value) -> Option { + let text = match content { + Value::String(text) => text.clone(), + Value::Array(parts) => parts + .iter() + .filter_map(part_text) + .collect::>() + .join("\n"), + Value::Null | Value::Bool(_) | Value::Number(_) | Value::Object(_) => return None, + }; + (!text.is_empty()).then_some(text) +} + +fn part_text(part: &Value) -> Option<&str> { + if part.get("type")?.as_str()? != "text" { + return None; + } + part.get("text")?.as_str() +} diff --git a/crates/pa-core/src/session_runtime/tests.rs b/crates/pa-core/src/session_runtime/tests.rs new file mode 100644 index 000000000..965c9b6fd --- /dev/null +++ b/crates/pa-core/src/session_runtime/tests.rs @@ -0,0 +1,403 @@ +//! The behavior the runtime owes: replay, branches, repair, recovery, order. + +use std::fs::OpenOptions; +use std::io::Write as _; +use std::path::Path; +use std::time::{Duration, Instant}; + +use serde_json::{json, Value}; + +use super::{Error, Outcome, Pending, ProviderMessage, SessionRuntime}; +use crate::session_log::SessionLog; + +const ID: &str = "01a18b20-00e5-719c-897c-3e15e8da74a0"; + +fn deadline() -> Instant { + Instant::now() + Duration::from_secs(30) +} + +fn message(id: &str, parent: Option<&str>, role: &str, content: &Value) -> Value { + json!({ + "type": "message", + "id": id, + "parentId": parent, + "timestamp": "2026-10-30T05:00:00.000Z", + "message": { "role": role, "content": content }, + }) +} + +fn said(id: &str, parent: Option<&str>, role: &str, text: &str) -> Value { + message(id, parent, role, &json!(text)) +} + +fn answered(id: &str, parent: &str, text: &str) -> Value { + message( + id, + Some(parent), + "assistant", + &json!([{ "type": "text", "text": text }]), + ) +} + +fn opened(dir: &Path) -> SessionRuntime { + SessionRuntime::open(dir, ID, deadline()) + .expect("open session") + .runtime +} + +fn committed(outcome: Outcome) -> u64 { + match outcome { + Outcome::Committed { seq, .. } => seq, + other => panic!("expected a commit, got {other:?}"), + } +} + +fn texts(runtime: &SessionRuntime) -> Vec { + runtime + .projection() + .into_iter() + .map(|message| message.content) + .collect() +} + +fn message_of(role: &str, content: &str) -> ProviderMessage { + ProviderMessage { + role: role.to_owned(), + content: content.to_owned(), + } +} + +#[test] +fn an_empty_session_opens_empty() { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + assert_eq!( + (runtime.rows().len(), runtime.last_seq(), runtime.leaf()), + (0, 0, None) + ); + assert_eq!(runtime.projection(), []); + + assert_eq!( + committed( + runtime + .append(&said("root", None, "user", "hello"), deadline()) + .unwrap() + ), + 1 + ); + assert_eq!(runtime.leaf(), Some("root")); +} + +#[test] +fn replay_indexes_ten_thousand_rows() { + let temp = tempfile::tempdir().unwrap(); + let mut parent = None; + { + let mut log = SessionLog::open(temp.path(), ID, deadline()).unwrap(); + for n in 0..10_000u64 { + let id = format!("row-{n}"); + let role = if n % 2 == 0 { "user" } else { "assistant" }; + let body = said(&id, parent.as_deref(), role, &format!("turn {n}")); + assert_eq!(committed(log.append(&body, deadline())), n + 1); + parent = Some(id); + } + } + + let open = SessionRuntime::open(temp.path(), ID, deadline()).unwrap(); + assert!(open.repair.is_none()); + let runtime = open.runtime; + assert_eq!(runtime.rows().len(), 10_000); + assert_eq!(runtime.last_seq(), 10_000); + assert_eq!(runtime.leaf(), Some("row-9999")); + assert_eq!(runtime.branch().len(), 10_000); + assert_eq!(runtime.projection().len(), 10_000); + let last = &runtime.rows()[9_999]; + assert_eq!(last.id, "row-9999"); + assert_eq!(last.timestamp.as_deref(), Some("2026-10-30T05:00:00.000Z")); + assert!(last.ts.ends_with('Z'), "the writer stamp is indexed"); +} + +#[test] +fn a_branch_switch_reprojects_the_selected_leaf() { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + runtime + .append(&said("root", None, "user", "hello"), deadline()) + .unwrap(); + runtime + .append(&answered("first", "root", "first answer"), deadline()) + .unwrap(); + runtime + .append(&answered("second", "root", "second answer"), deadline()) + .unwrap(); + + assert_eq!( + runtime.leaf(), + Some("first"), + "a row on another branch does not move the selection" + ); + assert_eq!(texts(&runtime), ["hello", "first answer"]); + assert_eq!(runtime.leaves(), ["first", "second"]); + + assert_eq!(committed(runtime.select("second", deadline()).unwrap()), 4); + assert_eq!(runtime.leaf(), Some("second")); + assert_eq!(texts(&runtime), ["hello", "second answer"]); + + runtime + .append(&answered("follow-up", "second", "more"), deadline()) + .unwrap(); + assert_eq!( + runtime.leaf(), + Some("follow-up"), + "a row on the selected branch moves the tip" + ); + assert_eq!(texts(&runtime), ["hello", "second answer", "more"]); + assert!(matches!( + runtime.select("root", deadline()), + Err(Error::NotALeaf { .. }) + )); + assert!(matches!( + runtime.select("missing", deadline()), + Err(Error::NoSuchRow { .. }) + )); + + drop(runtime); + let reopened = opened(temp.path()); + assert_eq!( + reopened.leaf(), + Some("follow-up"), + "the selection is a row, so it survives a reopen" + ); + assert_eq!(texts(&reopened), ["hello", "second answer", "more"]); +} + +#[test] +fn a_torn_tail_is_repaired_on_open_and_reported() { + let temp = tempfile::tempdir().unwrap(); + let path = temp.path().join(format!("{ID}.jsonl")); + let good; + { + let mut runtime = opened(temp.path()); + runtime + .append(&said("root", None, "user", "hello"), deadline()) + .unwrap(); + runtime + .append(&answered("answer", "root", "hi"), deadline()) + .unwrap(); + good = std::fs::metadata(&path).unwrap().len(); + } + + // What a writer killed mid-line leaves behind: no terminating newline. + { + let mut file = OpenOptions::new().append(true).open(&path).unwrap(); + file.write_all( + br#"{"seq":3,"ts":"2026-10-30T05:00:00.000Z","crc":"00000000","row":{"type":"message","#, + ) + .unwrap(); + file.sync_all().unwrap(); + } + let torn = std::fs::metadata(&path).unwrap().len() - good; + + let open = SessionRuntime::open(temp.path(), ID, deadline()).unwrap(); + let repair = open.repair.expect("a torn tail is reported, not kept"); + assert_eq!((repair.last_good_seq, repair.truncated_bytes), (2, torn)); + + let mut runtime = open.runtime; + assert_eq!(runtime.rows().len(), 2); + assert_eq!(texts(&runtime), ["hello", "hi"]); + assert_eq!( + committed( + runtime + .append(&answered("next", "answer", "again"), deadline()) + .unwrap() + ), + 3 + ); +} + +#[test] +fn an_undecided_append_is_reconciled_from_the_log_once() { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + runtime + .append(&said("root", None, "user", "hello"), deadline()) + .unwrap(); + + // The undecided write reached the file, but the runtime never saw it commit. + { + let mut other = SessionLog::open(temp.path(), ID, deadline()).unwrap(); + assert_eq!( + committed(other.append(&answered("answer", "root", "hi"), deadline())), + 2 + ); + } + assert_eq!( + runtime.rows().len(), + 1, + "an undecided append applies nothing on its own" + ); + + let verdict = |seq| Outcome::Committed { + seq, + ts: String::new(), + }; + assert_eq!( + committed(runtime.reconcile(verdict(2), deadline()).unwrap()), + 2 + ); + assert_eq!(runtime.rows().len(), 2); + assert_eq!(runtime.leaf(), Some("answer")); + assert_eq!(texts(&runtime), ["hello", "hi"]); + + runtime.reconcile(verdict(2), deadline()).unwrap(); + assert_eq!( + runtime.rows().len(), + 2, + "the same verdict adds no second row" + ); + assert!(matches!( + runtime.reconcile(verdict(3), deadline()), + Err(Error::MissingRow { seq: 3 }) + )); +} + +#[test] +fn a_retried_row_keeps_one_row_and_one_seq() { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + let row = said("root", None, "user", "hello"); + + assert_eq!(committed(runtime.append(&row, deadline()).unwrap()), 1); + // A retry of the row resolves to the seq the session holds. + assert_eq!(committed(runtime.append(&row, deadline()).unwrap()), 1); + // A pinned retry is the core's own idempotent path. + assert_eq!( + committed(runtime.append_at(1, &row, deadline()).unwrap()), + 1 + ); + assert_eq!(runtime.rows().len(), 1); + assert_eq!(runtime.read_since(0).unwrap().len(), 1); + + // The same id for a different row is a conflict, not a second row. + assert!(matches!( + runtime.append(&said("root", None, "user", "other"), deadline()), + Err(Error::DuplicateId { .. }) + )); + // A pinned seq that holds another row is refused; nothing is indexed. + assert!(matches!( + runtime + .append_at(1, &said("other", None, "user", "x"), deadline()) + .unwrap(), + Outcome::NotWritten { .. } + )); + assert_eq!(runtime.rows().len(), 1); +} + +#[test] +fn the_projection_follows_the_branch_in_row_order() { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + runtime + .append(&said("root", None, "user", "one"), deadline()) + .unwrap(); + runtime + .append(&answered("two", "root", "two"), deadline()) + .unwrap(); + // Not a message: indexed on the branch, absent from the projection. + let label = json!({ + "type": "label", + "id": "label", + "parentId": "two", + "timestamp": "2026-10-30T05:00:00.000Z", + "label": "note", + }); + runtime.append(&label, deadline()).unwrap(); + let parts = json!([ + { "type": "text", "text": "three" }, + { "type": "image", "data": "ignored" }, + ]); + runtime + .append(&message("three", Some("label"), "user", &parts), deadline()) + .unwrap(); + + assert_eq!( + runtime.projection(), + vec![ + message_of("user", "one"), + message_of("assistant", "two"), + message_of("user", "three"), + ] + ); +} + +#[test] +fn two_runtimes_see_each_others_rows() { + let temp = tempfile::tempdir().unwrap(); + let mut a = opened(temp.path()); + let mut b = opened(temp.path()); + + a.append(&said("one", None, "user", "from a"), deadline()) + .unwrap(); + b.append(&answered("two", "one", "from b"), deadline()) + .unwrap(); + assert_eq!( + b.rows().len(), + 2, + "b indexed a's row before writing its own" + ); + assert_eq!(texts(&b), ["from a", "from b"]); + + a.append(&answered("three", "two", "more"), deadline()) + .unwrap(); + assert_eq!(a.rows().len(), 3, "a indexed the row b committed"); + assert_eq!(texts(&a), ["from a", "from b", "more"]); +} + +#[test] +fn a_lost_recover_retries_to_one_row_and_the_session_opens() { + let row = answered("answer", "root", "hi"); + // The write reached the file, or it did not: both are decided from the log, + // with the pending seq the runtime kept when recover() could not run. + for landed in [true, false] { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + runtime + .append(&said("root", None, "user", "hello"), deadline()) + .unwrap(); + if landed { + let mut other = SessionLog::open(temp.path(), ID, deadline()).unwrap(); + assert_eq!(committed(other.append_at(2, &row, deadline())), 2); + } + runtime.pending = Some(Pending { + seq: 2, + id: "answer".to_owned(), + }); + assert_eq!(committed(runtime.append(&row, deadline()).unwrap()), 2); + assert_eq!(runtime.pending_seq(), None); + assert_eq!(runtime.read_since(0).unwrap().len(), 2, "exactly one row"); + drop(runtime); + assert_eq!( + opened(temp.path()).rows().len(), + 2, + "the session still opens with exactly one row (landed: {landed})" + ); + } +} + +#[test] +fn an_oversized_row_is_refused_and_indexes_nothing() { + let temp = tempfile::tempdir().unwrap(); + let mut runtime = opened(temp.path()); + // 17 MiB of row: the core refuses what its reader would call a bad line. + let oversized = said("big", None, "user", &"x".repeat(17 * 1024 * 1024)); + assert!(matches!( + runtime.append(&oversized, deadline()).unwrap(), + Outcome::NotWritten { + reason: crate::session_log::NotWritten::TooLong { .. } + } + )); + assert_eq!(runtime.rows().len(), 0); + assert_eq!(runtime.pending_seq(), None); + drop(runtime); + assert_eq!(opened(temp.path()).rows().len(), 0); +} -- knotmirror 0.0.1 From 32216b0014e2e5e1a3bdca4dcd2ab18931df5e13 Mon Sep 17 00:00:00 2001 From: Willow Sparks Date: Fri, 30 Oct 2026 06:06:50 +0000 Subject: [PATCH 2/6] fix(session-runtime): carry the id-less header row instead of refusing it A session log's first line is the header: a FileEntry with a type and a stamp and no id. The index only needs ids for tree nodes, so the header is carried without being indexed, while its seq still advances the log's newest seq so the next append does not collide with it. Test: a header row, a message, then an append that lands at seq 3 with the header not in rows() and not a branch node. --- crates/pa-core/src/session_runtime/index.rs | 8 +++++ crates/pa-core/src/session_runtime/mod.rs | 2 +- crates/pa-core/src/session_runtime/tests.rs | 34 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/pa-core/src/session_runtime/index.rs b/crates/pa-core/src/session_runtime/index.rs index 824cefba5..4d947cc8c 100644 --- a/crates/pa-core/src/session_runtime/index.rs +++ b/crates/pa-core/src/session_runtime/index.rs @@ -31,6 +31,8 @@ pub(crate) enum Applied { Selection { leaf: String }, /// A row the index already holds. Already, + /// A row with no id: carried by the log, not a tree node (the session header). + Opaque, } /// Rows by seq, by id, and the parent/child edges of the session tree. @@ -52,6 +54,12 @@ impl Index { if self.by_seq(seq).is_some() { return Ok(Applied::Already); } + if body.get("id").and_then(Value::as_str).is_none() { + // The session header carries no id: it has a seq, not a place in the + // tree, and the newest seq still has to advance past it. + self.last_seq = self.last_seq.max(seq); + return Ok(Applied::Opaque); + } let envelope = envelope(&body)?; if self.by_id.contains_key(&envelope.id) { return Err(Error::DuplicateId { id: envelope.id }); diff --git a/crates/pa-core/src/session_runtime/mod.rs b/crates/pa-core/src/session_runtime/mod.rs index 65c4371fe..1df6c4f1c 100644 --- a/crates/pa-core/src/session_runtime/mod.rs +++ b/crates/pa-core/src/session_runtime/mod.rs @@ -446,7 +446,7 @@ impl SessionRuntime { self.selected = Some(leaf); } } - Applied::Already => {} + Applied::Already | Applied::Opaque => {} } Ok(()) } diff --git a/crates/pa-core/src/session_runtime/tests.rs b/crates/pa-core/src/session_runtime/tests.rs index 965c9b6fd..98a693989 100644 --- a/crates/pa-core/src/session_runtime/tests.rs +++ b/crates/pa-core/src/session_runtime/tests.rs @@ -401,3 +401,37 @@ fn an_oversized_row_is_refused_and_indexes_nothing() { drop(runtime); assert_eq!(opened(temp.path()).rows().len(), 0); } + +#[test] +fn a_header_row_without_an_id_is_carried_not_indexed() { + let temp = tempfile::tempdir().unwrap(); + { + // A real session log starts with the header: a row with a type and a + // stamp, and no id. + let mut log = SessionLog::open(temp.path(), ID, deadline()).unwrap(); + let header = json!({ + "type": "session", + "version": 3, + "timestamp": "2026-10-30T05:00:00.000Z", + }); + assert_eq!(committed(log.append(&header, deadline())), 1); + assert_eq!( + committed(log.append(&said("root", None, "user", "hello"), deadline())), + 2 + ); + } + + let mut runtime = opened(temp.path()); + assert_eq!(runtime.rows().len(), 1, "the header is not a tree node"); + assert_eq!(runtime.last_seq(), 2, "but its seq is not re-used"); + assert_eq!(runtime.leaf(), Some("root")); + assert_eq!(texts(&runtime), ["hello"]); + assert_eq!( + committed( + runtime + .append(&answered("answer", "root", "hi"), deadline()) + .unwrap() + ), + 3 + ); +} -- knotmirror 0.0.1 From 44ba06ce9b4a8559f55a8da5ac2d9b51ca56e77d Mon Sep 17 00:00:00 2001 From: Willow Sparks Date: Fri, 30 Oct 2026 06:04:18 +0000 Subject: [PATCH 3/6] feat(daemon): serve a session through the session-log transport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit THE transport switch (port-plan §3, the one named decision point): a session resolves to the old canonical paths or the session-log core by its own files — an existing log is a log session, an old-format file stays canonical, and a fresh session takes the log only under the PA_SESSION_TRANSPORT=session-log override, so every existing flow's default is unchanged. - the deferred/held log wire behind `SessionFile`: the funnel's built-entry rows, the create flows' materialization rewrites, and the reopen tail all go through one `flush` (the header exactly once, its identity re-keyed to the session file path's id), and the unmaterialized tail's cursor rides the shared wire so clones cannot split the log. - the reopen arm: the log is the source of truth — open with repair (the report logged), the fold replays header + entry rows with the old loader's skip semantics, the serving store keeps the wire, and no lease file is taken (the log's flock is the writer's mutual exclusion). - the SDK mirror: a log-transported session's context builds from the serving fold (`mirror_seed`); the canonical capture/refusal/lease machinery remains the other transport's read arm, and the initial selection commit stays canonical-only (a logged boundary, not a row). - child usage: the attribution takes the one-row funnel in log mode; the receipt/forensics recovery machinery is the canonical transport's. The minimum serving path is proven end to end: a scripted turn's accepted user row, assistant row, and tool result row, plus a child usage attribution, are durable in the log, served from the fold, and replay identically on a reopen; a refused write is durable nowhere and never indexed; the canonical write path never touches a log session's old-format file. Build gates: check + test -p pa-daemon -p pa-core (the 13 pre-existing supervisor failures reproduce unmodified on this base). --- crates/pa-daemon/src/agent_engine.rs | 2 +- .../src/agent_engine/context_selection.rs | 31 + crates/pa-daemon/src/lib.rs | 1 + crates/pa-daemon/src/session_store.rs | 8 + .../src/session_store/child_usage.rs | 8 + .../pa-daemon/src/session_store/log_wire.rs | 97 +++ crates/pa-daemon/src/session_store/read.rs | 79 ++ crates/pa-daemon/src/session_store/write.rs | 18 + crates/pa-daemon/src/session_transport.rs | 770 ++++++++++++++++++ .../pa-daemon/src/worker/context_selection.rs | 24 +- crates/pa-daemon/src/worker/create.rs | 114 ++- 11 files changed, 1116 insertions(+), 36 deletions(-) create mode 100644 crates/pa-daemon/src/session_store/log_wire.rs create mode 100644 crates/pa-daemon/src/session_transport.rs diff --git a/crates/pa-daemon/src/agent_engine.rs b/crates/pa-daemon/src/agent_engine.rs index ed66d8d43..e0be7d8c6 100644 --- a/crates/pa-daemon/src/agent_engine.rs +++ b/crates/pa-daemon/src/agent_engine.rs @@ -49,7 +49,7 @@ pub(crate) use tests::FAUX_TEST_LOCK; mod context_selection; pub(crate) use context_selection::{ canonical_mirror_header, ContextSelectionCommitFuture, ContextSelectionSource, - InitialContextSelectionStore, + InitialContextSelectionStore, MirrorSeed, }; mod goalcore; diff --git a/crates/pa-daemon/src/agent_engine/context_selection.rs b/crates/pa-daemon/src/agent_engine/context_selection.rs index 4181c83aa..badb21e73 100644 --- a/crates/pa-daemon/src/agent_engine/context_selection.rs +++ b/crates/pa-daemon/src/agent_engine/context_selection.rs @@ -31,7 +31,21 @@ pub(crate) fn canonical_mirror_header(header: &SessionHeader) -> anyhow::Result< pub(crate) type ContextSelectionCommitFuture = Pin> + Send + 'static>>; +/// A log-transported session's mirror seed: the serving fold the store +/// already holds, replayed from the session log. The canonical capture/refusal +/// machinery is the other transport's read arm; this seed replaces it. +pub(crate) struct MirrorSeed { + pub(crate) header: SessionHeader, + pub(crate) entries: Vec, +} + pub(crate) trait InitialContextSelectionStore: Send + Sync { + /// The transport switch's read arm: a log-transported serving fold, or + /// `None` (the canonical capture/refusal path serves instead). + fn mirror_seed(&self) -> anyhow::Result> { + Ok(None) + } + /// Capture only the current owner and header, never a new lease or writer. fn capture(&self, expected_file: &Path) -> anyhow::Result; @@ -109,6 +123,23 @@ impl super::AgentSessionEngine { .ok_or_else(|| { anyhow::anyhow!("persistent session has no canonical context selection owner") })?; + // THE transport switch's read arm: a log-transported session's mirror + // builds from the serving fold; the canonical capture/refusal/lease + // machinery below serves the other transport. + if let Some(seed) = store.mirror_seed()? { + let dir = session_dir.unwrap_or_else(|| { + path.parent() + .map_or_else(|| std::path::PathBuf::from("."), Path::to_owned) + }); + let mut manager = pa_core::session::manager::SessionManager::in_memory_from_header( + &cwd, + &dir, + seed.header.clone(), + ) + .map_err(anyhow::Error::msg)?; + manager.adopt_entries(seed.entries); + return Ok((manager, None, None)); + } // Refuse the read BEFORE building anything: a poisoned epoch means an uncertain // write, and serving its tail as if classified is exactly the lie the drain // claim exists to prevent. diff --git a/crates/pa-daemon/src/lib.rs b/crates/pa-daemon/src/lib.rs index 27d7f8299..0639a633f 100644 --- a/crates/pa-daemon/src/lib.rs +++ b/crates/pa-daemon/src/lib.rs @@ -98,6 +98,7 @@ pub mod session_navigation; pub(crate) mod session_scan; pub mod session_stats; pub mod session_store; +pub(crate) mod session_transport; pub mod session_tree; pub mod session_usage; pub(crate) mod setting_switches; diff --git a/crates/pa-daemon/src/session_store.rs b/crates/pa-daemon/src/session_store.rs index c4b5969ec..c6af1c383 100644 --- a/crates/pa-daemon/src/session_store.rs +++ b/crates/pa-daemon/src/session_store.rs @@ -46,6 +46,9 @@ mod child_usage; mod child_usage_tests; mod context_selection; mod index; +// The log-transported half of the store (port-plan §3): the write arm the +// canonical paths bypass. +mod log_wire; use index::fold_child_usage_attributions; pub(crate) use index::new_entry_id; @@ -181,6 +184,11 @@ pub struct SessionFile { canonical_live: Option, canonical_delivered: std::collections::VecDeque, canonical_delivered_custom: std::collections::VecDeque, + /// THE transport switch's live half: `Some` serves this store's durable + /// rows through the session-log core (see `pa_daemon::session_transport` + /// for the switch and its resolution rule). `None` keeps the pre-port + /// canonical paths. + pub(crate) log_transport: Option, } #[derive(Debug, Clone)] diff --git a/crates/pa-daemon/src/session_store/child_usage.rs b/crates/pa-daemon/src/session_store/child_usage.rs index 329b3fae6..652c96257 100644 --- a/crates/pa-daemon/src/session_store/child_usage.rs +++ b/crates/pa-daemon/src/session_store/child_usage.rs @@ -129,6 +129,14 @@ impl SessionFile { origin, })?, }; + if self.log_transport.is_some() { + // THE transport switch: one durable log row, indexed only once + // the log committed it (the receipt/forensics machinery below is + // the canonical transport's recovery device; the log core's + // verdict replaces it). + self.persist_built_log_entry(entry)?; + return Ok(ChildUsageAppendResult::Created(aggregate)); + } if self.is_canonical_bound() { // The report ID already IS this journal row's stable identity. The // exact named owner action writes it once and leaves the checkpoint diff --git a/crates/pa-daemon/src/session_store/log_wire.rs b/crates/pa-daemon/src/session_store/log_wire.rs new file mode 100644 index 000000000..9f5e1ab83 --- /dev/null +++ b/crates/pa-daemon/src/session_store/log_wire.rs @@ -0,0 +1,97 @@ +//! The log-transported half of `SessionFile`: the write arm the canonical +//! paths bypass. Everything here serves the port's minimum end-to-end path +//! against the session-log core; the switch lives in +//! `crate::session_transport`. + +use anyhow::{Context, Result}; +use serde_json::Value; + +use super::SessionEntry; +use super::SessionFile; +use crate::session_transport::LogWire; + +impl SessionFile { + /// Whether this store is served by the log transport. + pub(crate) fn is_log_served(&self) -> bool { + self.log_transport.is_some() + } + + /// Attach the log transport (the worker create arms' wire). + pub(crate) fn attach_log_wire(&mut self, wire: LogWire) { + self.log_transport = Some(wire); + } + + /// The log transport's identity rule: the caller's session file path + /// names the session, so the header (row one at the first flush) re-keys + /// to the path's id. + /// + /// # Errors + /// + /// A store not served by the log transport, or a path with no id name. + pub(crate) fn rekey_log_session_id(&mut self, session_path: &std::path::Path) -> Result<()> { + anyhow::ensure!( + self.log_transport.is_some(), + "log re-key applies to log-transported sessions" + ); + let Some(session_id) = crate::session_transport::session_id_of(session_path) else { + anyhow::bail!( + "session file path has no id name: {}", + session_path.display() + ); + }; + self.header.id = session_id; + Ok(()) + } + + /// THE funnel for one built entry's durable row on the log transport. + /// + /// The entry joins the fold only after the log committed its row, so the + /// fold and the log never disagree: a refused write is not indexed, and + /// a reload never finds it as durable. + /// + /// # Errors + /// + /// The log's boundary policy (a foreign `Busy` holder, a refused row, or + /// an undecided write), plus the row's encoding. + pub(crate) fn persist_built_log_entry(&mut self, entry: SessionEntry) -> Result { + let row = serde_json::to_value(&entry).context("encode the session row")?; + self.flush_log_tail_from(0, std::slice::from_ref(&row))?; + let id = entry.id.clone(); + self.push_index(entry); + Ok(id) + } + + /// Flush every entry row the log does not hold yet, or (for + /// `persist_appended`) every row from the caller's start, whichever is + /// later. The flush's rows join the fold's index through the same paths + /// that put them in memory, so this only moves durability. + pub(crate) fn flush_unmaterialized_log_tail(&self, requested_start: usize) -> Result<()> { + self.flush_log_tail_from(requested_start, &[]) + } + + fn flush_log_tail_from(&self, requested_start: usize, extra_row: &[Value]) -> Result<()> { + let Some(wire) = self.log_transport.as_ref() else { + anyhow::bail!("not a log-transported session"); + }; + let session_id = self.header.id.clone(); + anyhow::ensure!( + !session_id.is_empty(), + "log-transported flush needs a session id" + ); + let header_row = super::session_header_line(&self.header); + let start = wire + .flushed_entries() + .max(requested_start) + .min(self.entries.len()); + let mut rows: Vec = Vec::with_capacity(extra_row.len() + self.entries.len() - start); + rows.extend( + self.entries[start..] + .iter() + .map(serde_json::to_value) + .collect::, _>>()?, + ); + rows.extend(extra_row.iter().cloned()); + wire.flush(&session_id, &header_row, &rows) + .map_err(|error| anyhow::Error::from(error).context("the session log rejected a row")) + } +} diff --git a/crates/pa-daemon/src/session_store/read.rs b/crates/pa-daemon/src/session_store/read.rs index 1d61aa13c..837e74173 100644 --- a/crates/pa-daemon/src/session_store/read.rs +++ b/crates/pa-daemon/src/session_store/read.rs @@ -171,6 +171,7 @@ impl SessionFile { canonical_live: None, canonical_delivered: std::collections::VecDeque::new(), canonical_delivered_custom: std::collections::VecDeque::new(), + log_transport: None, }; for line in lines { let line = line.with_context(read_context)?; @@ -197,6 +198,82 @@ impl SessionFile { Ok(file) } + /// Load a log-transported session: the log is the source of truth, the + /// fold replays it. + /// + /// The entry chain mirrors [`SessionFile::open`]'s old-format loader + /// (malformed-but-complete rows are skipped, never truncated), so + /// serving code sees the same fold either transport produced. The + /// returned store carries the held wire: its cursor counts exactly the + /// replayed entry rows, so later flushes only move durability. + /// + /// # Errors + /// + /// A log that no longer yields a session header (the repair truncation + /// emptied it), a header whose type is not `session`, I/O, or a foreign + /// writer holding the log. + pub(crate) fn open_log(session_path: &Path, agent_dir: &Path) -> Result { + let Some(session_id) = crate::session_transport::session_id_of(session_path) else { + anyhow::bail!( + "session file path has no id name: {}", + session_path.display() + ); + }; + let (log, repair) = crate::session_transport::open_repaired(agent_dir, &session_id) + .map_err(|error| anyhow::Error::from(error).context("open the session log"))?; + if let Some(report) = &repair { + eprintln!( + "pa-daemon: session log {} repaired: {} unusable trailing byte(s) after seq {}", + session_id, report.truncated_bytes, report.last_good_seq + ); + } + let rows = log + .read_since(0) + .map_err(|error| anyhow::Error::from(crate::session_transport::open_error(error))) + .context("read the session log")?; + let Some(header_row) = rows.first() else { + anyhow::bail!("session log {session_id} holds no header row (the repair emptied it)"); + }; + if header_row.body.get("type").and_then(Value::as_str) != Some("session") { + anyhow::bail!("session log {session_id} row one is not a session header"); + } + let pa_types::session::FileEntry::Header { header } = + serde_json::from_value::(header_row.body.clone()) + .context("invalid session header in the session log")? + else { + anyhow::bail!("session log {session_id} row one is not a session header"); + }; + let mut file = SessionFile { + path: session_path.to_path_buf(), + header, + entries: Vec::new(), + by_id: HashMap::new(), + leaf_id: None, + window: None, + lease: None, + child_usage_recovery: super::child_usage::ChildUsageRecovery::default(), + anthropic_warning_shown: false, + skipped_lines: 0, + epoch: None, + rows_written_without_epoch: 0, + canonical_live: None, + canonical_delivered: std::collections::VecDeque::new(), + canonical_delivered_custom: std::collections::VecDeque::new(), + log_transport: None, + }; + for row in rows.iter().skip(1) { + match serde_json::from_value::(row.body.clone()) { + Ok(entry) => file.push_index(entry), + Err(_) => file.skipped_lines += 1, + } + } + fold_child_usage_attributions(&mut file.entries); + file.anthropic_warning_shown = file.branch().iter().copied().any(is_warning_shown_row); + let wire = crate::session_transport::LogWire::held(log, agent_dir, file.entries.len()); + file.log_transport = Some(wire); + Ok(file) + } + /// Load the verified compacted context without decoding old message bodies. /// /// # Errors @@ -235,6 +312,7 @@ impl SessionFile { canonical_live: None, canonical_delivered: std::collections::VecDeque::new(), canonical_delivered_custom: std::collections::VecDeque::new(), + log_transport: None, }; // The raw rows are consumed in place: each line String drops as soon as its // parsed entry joins the store. @@ -345,6 +423,7 @@ impl SessionFile { canonical_live: None, canonical_delivered: std::collections::VecDeque::new(), canonical_delivered_custom: std::collections::VecDeque::new(), + log_transport: None, } } } diff --git a/crates/pa-daemon/src/session_store/write.rs b/crates/pa-daemon/src/session_store/write.rs index abb328d17..3d53343aa 100644 --- a/crates/pa-daemon/src/session_store/write.rs +++ b/crates/pa-daemon/src/session_store/write.rs @@ -30,6 +30,12 @@ impl SessionEntry { impl SessionFile { /// Persist only the newly appended creation records on a resumed file. pub(crate) fn persist_appended(&self, start: usize) -> Result<()> { + // THE transport switch: the log transport flushes the tail from the + // wire's durable cursor (rows before it are already durable), so the + // reopen flow's `start` and the wire's cursor agree. + if self.log_transport.is_some() { + return self.flush_unmaterialized_log_tail(start); + } anyhow::ensure!( self.canonical_live.is_none(), "canonical owner requires explicit append and adoption ACK" @@ -118,6 +124,12 @@ impl SessionFile { self.canonical_live.is_none(), "cannot rewrite a bound canonical journal" ); + // THE transport switch: a log-transported store has no full-file + // rewrite; the log is append-only, so this is the flush of the + // unmaterialized tail (the create flows' materialization point). + if self.log_transport.is_some() { + return self.flush_unmaterialized_log_tail(0); + } anyhow::ensure!( self.child_usage_recovery.pending.is_none(), "confirm unresolved attribution before rewriting session" @@ -276,6 +288,12 @@ impl SessionFile { self.canonical_live.is_none(), "canonical owner requires explicit append and adoption ACK" ); + // THE transport switch's write arm: a log-transported store's only + // durable write is the log row for the built entry (the canonical + // grant machinery below stays compiled for the canonical sessions). + if self.log_transport.is_some() { + return self.persist_built_log_entry(entry); + } self.persist_owned_entry(entry, step) } diff --git a/crates/pa-daemon/src/session_transport.rs b/crates/pa-daemon/src/session_transport.rs new file mode 100644 index 000000000..db51e8b4a --- /dev/null +++ b/crates/pa-daemon/src/session_transport.rs @@ -0,0 +1,770 @@ +//! THE transport switch for the session-log wiring (`SESSIONS-LOG-SPEC.md`, +//! port-plan §3): the one point that decides whether a session is served by +//! the old canonical paths ([`SessionTransport::Canonical`], compiled until +//! the final deletion step) or by the session-log core +//! ([`SessionTransport::SessionLog`]). +//! +//! Resolution rule ([`resolve`]): a session that already has a log file is a +//! log session; an old-format session file stays canonical (the log core +//! cannot read that format, and the spec reports old files instead of +//! converting them); a fresh session takes the log only when the override +//! environment asks for it, so the default behavior of every existing flow is +//! unchanged. The override selects between the two transports until the +//! cutover flips the default; the final deletion step removes the canonical +//! half together with this switch. +//! +//! Row policy at this boundary (the spec: policy lives in callers; the core +//! never waits, retries, or inspects a peer): an append resolves exactly +//! once. A foreign `Busy` holder and a `NotWritten` verdict surface as +//! refusals through the store's existing failure paths; `Unknown` asks the +//! core's `Recover` to decide before any row is indexed. The daemon store +//! keeps its fold as the serving view; the log is the source of truth the +//! fold replays from (port-plan §3.2: open = acquire, repair, read-since +//! fold). +//! +//! Durability contract: the log holds every row the fold serves. The fold's +//! index gains a row only after the log committed it, and the unmaterialized +//! tail is tracked here, so `rewrite()` (the full-file atomic write the old +//! transport's create flows end with) becomes a tail flush. + +use std::path::{Path, PathBuf}; +use std::time::{Duration, Instant}; + +use serde_json::Value; + +use pa_core::session_log::{self, Outcome, SessionLog}; + +/// The override that serves fresh sessions on the log transport. Absent, or +/// any other value, keeps the canonical default. +pub(crate) const TRANSPORT_OVERRIDE_ENV: &str = "PA_SESSION_TRANSPORT"; +pub(crate) const TRANSPORT_OVERRIDE_VALUE: &str = "session-log"; + +/// One open/repair's budget at this transport. The daemon's own append paths +/// run under the session's admission deadlines; the blocking open/repair here +/// takes this bound so a wedged storage device reports instead of stalling a +/// create. The core never waits; this is the caller's one clock. +const LOG_OPEN_DEADLINE: Duration = Duration::from_secs(10); + +/// Which transport serves a session. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SessionTransport { + /// The pre-port canonical paths (the compiled default until cutover). + Canonical, + /// The session-log core: the log is the source of truth, the daemon + /// store is its fold. + SessionLog, +} + +/// The log directory: one append-only log file per session id. +pub(crate) fn log_dir(agent_dir: &Path) -> PathBuf { + agent_dir.join("session-logs") +} + +/// One session's log file path. +pub(crate) fn log_file(agent_dir: &Path, session_id: &str) -> PathBuf { + log_dir(agent_dir).join(format!("{session_id}.jsonl")) +} + +/// The session id a session file path names: its stem. +pub(crate) fn session_id_of(session_path: &Path) -> Option { + session_path + .file_stem() + .and_then(std::ffi::OsStr::to_str) + .map(str::to_owned) +} + +/// The bounded instant for one open/append/repair decision at this transport. +pub(crate) fn deadline() -> Instant { + Instant::now() + LOG_OPEN_DEADLINE +} + +/// THE switch point. `session_path` is the session's file path (the daemon's +/// advertised identity). +pub(crate) fn resolve(agent_dir: &Path, session_path: &Path) -> SessionTransport { + let Some(session_id) = session_id_of(session_path) else { + return SessionTransport::Canonical; + }; + if log_file(agent_dir, &session_id).exists() { + return SessionTransport::SessionLog; + } + // An existing old-format session keeps serving through the canonical path. + if session_path.exists() { + return SessionTransport::Canonical; + } + if new_transport_requested() { + return SessionTransport::SessionLog; + } + SessionTransport::Canonical +} + +/// Whether the switch maps this session to the log. +pub(crate) fn transports_session_log(agent_dir: &Path, session_path: &Path) -> bool { + resolve(agent_dir, session_path) == SessionTransport::SessionLog +} + +/// The override's request, read once per resolve. +pub(crate) fn new_transport_requested() -> bool { + match std::env::var(TRANSPORT_OVERRIDE_ENV) { + Ok(override_value) => override_value == TRANSPORT_OVERRIDE_VALUE, + Err(_) => false, + } +} + +/// Why one wire step failed. +#[derive(Debug, thiserror::Error)] +pub(crate) enum LogWireError { + /// Another writer holds the log. + #[error("session log is held by {holder}")] + Busy { + /// The record the foreign holder left, when there was one. + holder: String, + }, + /// Nothing durable was written, or a past write stayed undecided. + #[error("session log: {0}")] + Refused(#[source] anyhow::Error), + /// The log could not be opened or (re)synced. + #[error(transparent)] + Io(#[from] std::io::Error), +} + +/// The lazy log handle behind a log-transported `SessionFile`. +/// +/// The handle is clone-SHARED (a store's `Clone` copies the arc): every copy +/// flushes through one next-seq counter under one lock-held file, so copies +/// never interleave. Command surfaces that would turn a clone into a +/// divergent fold (branch/fork/import row rewrites) are refused at their +/// command seam while the session is log-transported, so exactly one serving +/// store flushes a session's log. +#[derive(Debug, Clone)] +pub(crate) struct LogWire { + state: std::sync::Arc>, +} + +/// The wire's shared state. The unmaterialized-row cursor lives here because +/// a store clone carries the same wire: one log gets exactly one tail cursor. +#[derive(Debug)] +struct WireState { + /// `None` until the first flush; a refused open retries at the next call. + log: Option, + agent_dir: PathBuf, + /// The header row is durable in the log. A fresh (deferred) wire writes + /// it as row one; a held (reopened) wire replays it from the fold's open. + header_written: bool, + /// How many of the fold's entry rows are durable in the log, in fold + /// order. The store flushes only `entries.len() - flushed` at its tail. + flushed_entries: usize, +} + +/// Resolve one decided append outcome; `flushed` grows only with committed +/// rows, and an `Unknown` verdict is asked of the core's recovery before the +/// caller sees anything. +fn commit_one(flushed: &mut usize, outcome: Outcome) -> Result<(), LogWireError> { + match outcome { + Outcome::Committed { .. } => { + *flushed += 1; + Ok(()) + } + Outcome::Unknown { recover } => match recover.recover(deadline()) { + Ok(Outcome::Committed { .. }) => { + *flushed += 1; + Ok(()) + } + Ok(Outcome::NotWritten { reason }) => Err(LogWireError::Refused(anyhow::anyhow!( + "the log resolved the append as not written: {reason:?}" + ))), + Ok(Outcome::Busy { .. } | Outcome::Unknown { .. }) | Err(_) => { + Err(LogWireError::Refused(anyhow::anyhow!( + "the log could not decide its undecided write" + ))) + } + }, + outcome => Err(wire_error(outcome)), + } +} + +impl LogWire { + /// A fresh wire: the log opens (creating file, lock, and row one) at the + /// first flush. + pub(crate) fn deferred(agent_dir: &Path) -> Self { + Self { + state: std::sync::Arc::new(std::sync::Mutex::new(WireState { + log: None, + agent_dir: agent_dir.to_path_buf(), + header_written: false, + flushed_entries: 0, + })), + } + } + + /// A reopened wire wrapping the log the fold was replayed from. The + /// header is already durable (row one of the replayed log), and the + /// replayed entry count is the wire's tail cursor. + pub(crate) fn held(log: SessionLog, agent_dir: &Path, replayed_entries: usize) -> Self { + Self { + state: std::sync::Arc::new(std::sync::Mutex::new(WireState { + log: Some(log), + agent_dir: agent_dir.to_path_buf(), + header_written: true, + flushed_entries: replayed_entries, + })), + } + } + + /// THE one durable-write API: the pending header row first (exactly once + /// per wire), then `rows`, in order. + /// + /// # Errors + /// + /// [`LogWireError`]: the boundary policy folded into one type. A returned + /// error leaves the log and the cursor consistent: the committed prefix + /// is durable, the rest was never written. + pub(crate) fn flush( + &self, + session_id: &str, + header_row: &Value, + rows: &[Value], + ) -> Result<(), LogWireError> { + let mut state = self + .state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + // The open resolves first, then appends run one at a time, so the + // state's borrow never overlaps itself. + if state.log.is_none() { + std::fs::create_dir_all(log_dir(&state.agent_dir)).map_err(LogWireError::Io)?; + let log = SessionLog::open(&log_dir(&state.agent_dir), session_id, deadline()) + .map_err(open_error)?; + state.log = Some(log); + } + if !state.header_written { + let outcome = state + .log + .as_mut() + .expect("the open above filled the slot") + .append(header_row, deadline()); + commit_one(&mut state.flushed_entries, outcome)?; + state.header_written = true; + } + for row in rows { + let outcome = state + .log + .as_mut() + .expect("the open above filled the slot") + .append(row, deadline()); + commit_one(&mut state.flushed_entries, outcome)?; + } + Ok(()) + } + + /// How many entry rows the log already holds. The store's unmaterialized + /// tail is `entries.len() - flushed`. + pub(crate) fn flushed_entries(&self) -> usize { + self.state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .flushed_entries + } +} + +/// Fold a decided non-commit outcome into the wire error. +fn wire_error(outcome: Outcome) -> LogWireError { + match outcome { + Outcome::Busy { holder, .. } => LogWireError::Busy { + holder: holder.map_or_else( + || "another writer".to_owned(), + |holder| format!("pid {}", holder.pid), + ), + }, + Outcome::NotWritten { reason } => { + LogWireError::Refused(anyhow::anyhow!("session log refused the row: {reason:?}")) + } + Outcome::Unknown { .. } | Outcome::Committed { .. } => LogWireError::Refused( + anyhow::anyhow!("the log decided an append this call did not write"), + ), + } +} + +/// Open a session's log for the fold's replay: a torn or corrupt tail is +/// repaired first (the report returns so the caller can log it), then the log +/// opens for appends. +pub(crate) fn open_repaired( + agent_dir: &Path, + session_id: &str, +) -> Result<(SessionLog, Option), LogWireError> { + match SessionLog::open(&log_dir(agent_dir), session_id, deadline()) { + Ok(log) => Ok((log, None)), + Err(session_log::Error::Damaged { .. }) => { + let report = SessionLog::repair(&log_dir(agent_dir), session_id, deadline()) + .map_err(open_error)?; + let log = SessionLog::open(&log_dir(agent_dir), session_id, deadline()) + .map_err(open_error)?; + Ok((log, Some(report))) + } + Err(error) => Err(open_error(error)), + } +} + +/// Fold an open failure into the wire error. +pub(crate) fn open_error(error: session_log::Error) -> LogWireError { + match error { + session_log::Error::Busy { holder, .. } => LogWireError::Busy { + holder: holder.map_or_else( + || "another writer".to_owned(), + |holder| format!("pid {}", holder.pid), + ), + }, + session_log::Error::Io(error) => error.into(), + session_log::Error::Damaged { last_good_seq, .. } + | session_log::Error::Corrupt { + after_seq: last_good_seq, + .. + } => LogWireError::Refused(anyhow::anyhow!( + "the session log is unusable after seq {last_good_seq}; repair is the recovery" + )), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::session_store::SessionFile; + use crate::worker::{Worker, WorkerConfig}; + use serde_json::json; + + fn temp_dir(tag: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!( + "pa-transport-{tag}-{}-{}", + std::process::id(), + uuid::Uuid::new_v4().simple() + )); + std::fs::create_dir_all(&dir).expect("temp dir"); + dir + } + + /// The env tests mutate process state; they serialize on this lock. + static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + fn with_override(run: impl FnOnce() -> T) -> T { + let _guard = ENV_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let saved = std::env::var(TRANSPORT_OVERRIDE_ENV).ok(); + std::env::set_var(TRANSPORT_OVERRIDE_ENV, TRANSPORT_OVERRIDE_VALUE); + let result = run(); + match saved { + Some(saved) => std::env::set_var(TRANSPORT_OVERRIDE_ENV, saved), + None => std::env::remove_var(TRANSPORT_OVERRIDE_ENV), + } + result + } + + fn fresh_log_session(dir: &std::path::Path, name: &str) -> (SessionFile, std::path::PathBuf) { + let session_path = dir.join(format!("{name}.jsonl")); + let mut store = SessionFile::create("test", None, 0); + store.set_path(session_path.clone()); + store.attach_log_wire(LogWire::deferred(&dir)); + store.rekey_log_session_id(&session_path).expect("re-key"); + (store, session_path) + } + + #[test] + fn the_switch_serves_a_log_session_and_refuses_to_map_an_old_file() { + let dir = temp_dir("switch"); + // A fresh session with no files and no override: the canonical default. + let path = dir.join("fresh.jsonl"); + assert!(!transports_session_log(&dir, &path)); + + // An old-format session file: the log core cannot read that format, + // so it keeps serving canonically, override or not. + std::fs::write(&path, "[]\n").expect("old file"); + assert!(!transports_session_log(&dir, &path)); + assert_eq!(with_override(|| transports_session_log(&dir, &path)), false); + std::fs::remove_file(&path).expect("clean"); + + // An existing log: the log transport, override or not. + let log_session = dir.join("logged.jsonl"); + let mut store = SessionFile::create("test", None, 0); + store.set_path(log_session.clone()); + store.attach_log_wire(LogWire::deferred(&dir)); + store.rekey_log_session_id(&log_session).expect("re-key"); + store.rewrite().expect("flush"); + assert!(log_file(&dir, "logged").exists()); + assert_eq!( + with_override(|| transports_session_log(&dir, &log_session)), + true + ); + assert!(transports_session_log(&dir, &log_session)); + } + + #[test] + fn a_fresh_wires_first_flush_writes_the_header_then_the_rows() { + let dir = temp_dir("fresh-wire"); + let (mut store, session_path) = fresh_log_session(&dir, "fresh-wire"); + // The log does not exist before the store's first flush. + assert!(!log_file(&dir, "fresh-wire").exists()); + + let user = store + .persist_entry( + "message", + json!({ "message": { "role": "user", "content": "hi", "timestamp": 1 } }), + ) + .expect("persist"); + let assistant = store + .persist_entry("message", json!({ "message": { "role": "assistant", "content": [{ "type": "text", "text": "one" }], "timestamp": 2 } })) + .expect("persist"); + + // The rows are durable in the log, and the old file was never written. + assert!(log_file(&dir, "fresh-wire").exists()); + assert!(!session_path.exists()); + let log = SessionLog::open(&log_dir(&dir), "fresh-wire", deadline()) + .expect("reopen the written log"); + let rows = log.read_since(0).expect("read back"); + assert_eq!(rows.len(), 3, "header row plus two entry rows"); + assert_eq!( + rows[0].body.get("type").and_then(Value::as_str), + Some("session"), + "row one is the header" + ); + assert_eq!(store.entries().len(), 2); + assert_eq!(store.session_id(), "fresh-wire"); + let _ = (user, assistant); + } + + #[test] + fn a_payload_pathed_fresh_session_rekeys_to_its_path_id() { + let dir = temp_dir("rekey"); + let session_path = dir.join("payload-stem.jsonl"); + // The payload-pathed fresh arm's steps: the deferred wire, then the + // re-key to the path's id, then the materialization rewrite. + let mut created = SessionFile::create("test", None, 0); + created.set_path(session_path.clone()); + created.attach_log_wire(LogWire::deferred(&dir)); + created.rekey_log_session_id(&session_path).expect("re-key"); + created.append_session_state("active"); + created + .rewrite() + .expect("the header and state rows are durable"); + + let reopened = SessionFile::open_log(&session_path, &dir).expect("reopen"); + assert_eq!( + reopened.header.id, "payload-stem", + "the path names the session" + ); + let types = vec!["session_state".to_owned()]; + assert_eq!( + reopened + .entries() + .iter() + .map(|e| e.type_.clone()) + .collect::>(), + types + ); + } + + #[test] + fn a_reopen_replays_the_log_and_flows_new_appends_past_the_cursor() { + let dir = temp_dir("reopen"); + let (mut store, session_path) = fresh_log_session(&dir, "reopen"); + store + .persist_entry( + "message", + json!({ "message": { "role": "user", "content": "hi", "timestamp": 1 } }), + ) + .expect("persist"); + store.rewrite().expect("flush"); + + drop(store); + let mut reloaded = SessionFile::open_log(&session_path, &dir).expect("reopen from the log"); + assert_eq!(reloaded.entries().len(), 1); + assert_eq!(reloaded.header.id, "reopen"); + assert!(reloaded.is_log_served()); + // The reopened store appends past the replayed cursor: only the new + // row reaches the log. + let start = reloaded.entries().len(); + reloaded + .persist_entry( + "message", + json!({ "message": { "role": "user", "content": "next", "timestamp": 2 } }), + ) + .expect("persist"); + reloaded.persist_appended(start).expect("flush the tail"); + let log = SessionLog::open(&log_dir(&dir), "reopen", deadline()).expect("open"); + let rows = log.read_since(0).expect("read"); + assert_eq!( + rows.len(), + 3, + "header, replayed user row, appended user row" + ); + assert_eq!( + rows[2].body["message"]["content"].as_str(), + Some("next"), + "the appended tail lands in order" + ); + } + + #[test] + fn a_refused_row_is_durable_nowhere_never_indexed() { + let dir = temp_dir("refused"); + let session_path = dir.join("refused.jsonl"); + // A file where the log directory belongs: the wire's open cannot run, + // the write refuses, and the entry stays out of the fold. + std::fs::write(log_dir(&dir), "not a directory").expect("blocker file"); + let mut store = SessionFile::create("test", None, 0); + store.set_path(session_path.clone()); + store.attach_log_wire(LogWire::deferred(&dir)); + store.rekey_log_session_id(&session_path).expect("re-key"); + let refused = store.persist_entry( + "message", + json!({ "message": { "role": "user", "content": "lost", "timestamp": 1 } }), + ); + assert!( + refused.is_err(), + "the blocked log refuses the write: {refused:?}" + ); + assert_eq!( + store.entries().len(), + 0, + "the refused row never joined the fold" + ); + assert_eq!(store.leaf_id(), None); + } + + #[tokio::test] + async fn the_minimum_serving_path_runs_a_log_session_end_to_end() { + let dir = temp_dir("e2e"); + let agent_dir = dir.join("agent"); + let sessions_dir = dir.join("sessions"); + std::fs::create_dir_all(&sessions_dir).expect("sessions dir"); + let session_path = sessions_dir.join("log-e2e.jsonl"); + // The log session pre-exists its worker: the create below resolves to + // the log transport through the log itself (the resolve() test covers + // the override's fresh path), so the serving run exercises the reopen. + { + let (store, _) = fresh_log_session(&dir, "log-e2e"); + store + .rewrite() + .expect("the header row is durable before the worker"); + let log = SessionLog::open(&log_dir(&dir), "log-e2e", deadline()).expect("log"); + assert_eq!(log.read_since(0).expect("read").len(), 1, "header only"); + } + std::fs::rename(log_file(&dir, "log-e2e"), dir.join("staged-log.jsonl")) + .expect("move the seeded log"); + std::fs::create_dir_all(&agent_dir).expect("agent dir"); + std::fs::create_dir_all(log_dir(&agent_dir)).expect("log dir"); + std::fs::rename( + dir.join("staged-log.jsonl"), + log_file(&agent_dir, "log-e2e"), + ) + .expect("seed the log at the worker's agent dir"); + + let config = WorkerConfig { + socket_path: dir.join("worker.sock"), + supervisor_socket_path: std::path::PathBuf::new(), + token: "token".to_string(), + worker_instance_id: String::new(), + active_session_id: "log-e2e".to_string(), + agent_dir: agent_dir.clone(), + recovery_journal_path: dir.join("recovery.jsonl"), + telemetry_disabled: None, + script: Some(json!({ + "responses": [ + { "text": "run the listing", "toolCalls": [ { + "toolCallId": "call-1", + "toolName": "bash", + "args": { "command": "ls" }, + "result": "listing", + "isError": false, + } ] }, + { "text": "steered through" } + ], + })), + }; + let worker = std::sync::Arc::new(Worker::new(config, None)); + let created = worker + .dispatch( + "create", + &json!({ + "cwd": dir.to_string_lossy(), + "sessionDir": sessions_dir.to_string_lossy(), + "sessionPath": session_path.to_string_lossy(), + "name": "log-e2e", + }), + ) + .await; + assert!(created.success, "create failed: {created:?}"); + { + let core = worker.core.lock().unwrap(); + let store = core.store.as_ref().expect("created store"); + assert!(store.is_log_served(), "the create reopened the log session"); + assert_eq!(store.header.id, "log-e2e"); + } + + let prompted = worker + .dispatch( + "prompt_and_wait", + &json!({ "activeSessionId": "log-e2e", "message": "run the tool" }), + ) + .await; + assert!(prompted.success, "prompt failed: {prompted:?}"); + + // The turn's rows: the accepted user row, the assistant row, the + // tool result row — durable in the log, served from the fold. + { + let core = worker.core.lock().unwrap(); + let store = core.store.as_ref().expect("store"); + let types = store + .entries() + .iter() + .map(|e| e.type_.clone()) + .collect::>(); + assert_eq!( + store.entries()[4].fields["message"]["content"], + json!("run the tool"), + "the turn's accepted user row: {types:?}" + ); + assert_eq!( + store.entries()[5].fields["message"]["role"], + json!("assistant") + ); + assert_eq!( + store.entries()[6].fields["message"]["role"], + json!("toolResult") + ); + let assistant_id = store.entries()[5].id.clone(); + drop(core); + // Child usage appends through the same funnel and folds live. + let mut core = worker.core.lock().unwrap(); + let store = core.store.as_mut().expect("store"); + store + .append_child_usage_once( + "attrib-row", + &assistant_id, + pa_types::ai::Usage { + input: 3, + ..Default::default() + }, + None, + ) + .expect("child usage row"); + assert!( + store.entries().last().map(|e| e.type_.clone()) + == Some("child_usage_attributed".to_string()) + ); + } + + // The attach path serves the same fold (the mirror seed rides the + // store's entries); the snapshot carries the turn's messages. + let _ = messages_of(&worker).await; + + // Restart: the fold replays from the log with the same rows, and the + // old-format session file stays absent (the log is the session). + drop(worker); + let reloaded = + SessionFile::open_log(&session_path, &agent_dir).expect("restart from the log"); + let replayed = reloaded.entries(); + assert_eq!( + replayed.len(), + 8, + "prefix, user, assistant, tool result, usage: {replayed:#?}" + ); + assert_eq!( + replayed[4].fields["message"]["content"], + json!("run the tool") + ); + assert_eq!(replayed[6].fields["message"]["role"], json!("toolResult")); + assert_eq!( + replayed[5] + .fields + .get("message") + .and_then(|message| message.get("usage")) + .map(|usage| usage.get("input").and_then(Value::as_u64)) + .flatten(), + Some(123), + "the replayed assistant folds the attribution's aggregate (the scripted 120 plus the child's 3)" + ); + assert!(!session_path.exists(), "the canonical write path never ran"); + } + + async fn messages_of(worker: &Worker) -> Vec { + let response = worker + .dispatch("get_messages", &json!({ "activeSessionId": "log-e2e" })) + .await; + assert!(response.success, "get_messages failed: {response:?}"); + response.data.expect("messages")["messages"] + .as_array() + .expect("messages array") + .clone() + } + #[test] + fn an_attribution_aggregates_the_same_way_through_the_log_funnel() { + let dir = temp_dir("probe"); + // The canonical store's arithmetic, then the same arithmetic through + // the log funnel: the folds must agree (one serving semantic). + let mut plain = SessionFile::create("test", None, 0); + plain.set_path(dir.join("plain.jsonl")); + let assistant = plain + .persist_entry( + "message", + json!({ "message": { "role": "assistant", "content": [], "usage": serde_json::to_value(pa_types::ai::Usage { input: 123, output: 8, ..Default::default() }).expect("usage"), "timestamp": 1 } }), + ) + .expect("assistant row"); + plain + .append_child_usage_once( + "attrib-a", + &assistant, + pa_types::ai::Usage { + input: 3, + ..Default::default() + }, + None, + ) + .expect("attribution"); + let folded = plain + .entry(&assistant) + .expect("assistant") + .fields + .get("message") + .and_then(|message| message.get("usage")) + .cloned() + .expect("folded usage"); + drop(plain); + + let (mut logged, _) = fresh_log_session(&dir, "probe"); + let logged_assistant = logged + .persist_entry( + "message", + json!({ "message": { "role": "assistant", "content": [], "usage": serde_json::to_value(pa_types::ai::Usage { input: 123, output: 8, ..Default::default() }).expect("usage"), "timestamp": 2 } }), + ) + .expect("assistant row"); + logged + .append_child_usage_once( + "attrib-b", + &logged_assistant, + pa_types::ai::Usage { + input: 3, + ..Default::default() + }, + None, + ) + .expect("attribution"); + let folded2 = logged + .entry(&logged_assistant) + .expect("assistant") + .fields + .get("message") + .and_then(|message| message.get("usage")) + .cloned() + .expect("folded usage"); + assert_eq!(folded2, folded); + let attribution = logged + .entries() + .iter() + .find(|entry| entry.type_ == "child_usage_attributed") + .expect("attribution row"); + assert_eq!( + attribution.fields.get("aggregateUsage"), + Some(&folded2), + "the row's aggregate matches the served fold" + ); + } +} diff --git a/crates/pa-daemon/src/worker/context_selection.rs b/crates/pa-daemon/src/worker/context_selection.rs index 93038937c..df8d1796a 100644 --- a/crates/pa-daemon/src/worker/context_selection.rs +++ b/crates/pa-daemon/src/worker/context_selection.rs @@ -8,7 +8,7 @@ use serde_json::json; use crate::agent_engine::{ canonical_mirror_header, ContextSelectionCommitFuture, ContextSelectionSource, - InitialContextSelectionStore, + InitialContextSelectionStore, MirrorSeed, }; use crate::lease::{canonical_session_path, SessionLease}; use crate::session_store::SessionFile; @@ -108,6 +108,28 @@ fn write_selection(store: &mut SessionFile, selection: &ContextSelection) -> Res } impl InitialContextSelectionStore for CoreInitialContextSelectionStore { + fn mirror_seed(&self) -> Result> { + let core = self + .0 + .lock() + .map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; + let Some(store) = core.store.as_ref() else { + return Ok(None); + }; + if !store.is_log_served() { + return Ok(None); + } + let entries = store + .entries() + .iter() + .map(|entry| serde_json::from_value(serde_json::to_value(entry)?)) + .collect::, serde_json::Error>>()?; + Ok(Some(MirrorSeed { + header: store.header.clone(), + entries, + })) + } + fn live_owner( &self, source: ContextSelectionSource, diff --git a/crates/pa-daemon/src/worker/create.rs b/crates/pa-daemon/src/worker/create.rs index bf4f321cf..d828c2aa5 100644 --- a/crates/pa-daemon/src/worker/create.rs +++ b/crates/pa-daemon/src/worker/create.rs @@ -202,8 +202,29 @@ impl Worker { let mut fresh_prefix = FreshPrefixPlan::None; let settings = pa_core::settings::SettingsManager::create(&cwd, &self.config.agent_dir); let mut store = match (&session_path, no_session) { - (Some(path), false) if path.exists() => { - let loaded = { + // A session whose log exists reopens through the log transport + // even without an old-format file: the log IS the session. + (Some(path), false) + if path.exists() + || crate::session_transport::transports_session_log( + &self.config.agent_dir, + path, + ) => + { + // THE transport switch's reopen arm: a log-transported + // session loads from its log; the log's flock is the writer's + // mutual exclusion, so the lease file is not taken. + let loaded = if crate::session_transport::transports_session_log( + &self.config.agent_dir, + path, + ) { + let path = path.clone(); + let agent_dir = self.config.agent_dir.clone(); + tokio::task::spawn_blocking(move || SessionFile::open_log(&path, &agent_dir)) + .await + .map_err(anyhow::Error::from) + .and_then(|result| result) + } else { let path = path.clone(); let agent_dir = self.config.agent_dir.clone(); tokio::task::spawn_blocking(move || { @@ -286,12 +307,15 @@ impl Worker { // Prime the usage fold on the file's final identity, // off the runtime and before the core lock: // summaries under the lock fold only the appended - // tail. - let primed = path.clone(); - let _ = tokio::task::spawn_blocking(move || { - crate::session_store::read_session_info(&primed) - }) - .await; + // tail. The log transport's info-sidecars are its own + // concern; skip the old file's priming read. + if !opened.is_log_served() { + let primed = path.clone(); + let _ = tokio::task::spawn_blocking(move || { + crate::session_store::read_session_info(&primed) + }) + .await; + } opened } Err(error) => return crate::hold_refusal::create_failure_response(&error), @@ -304,19 +328,32 @@ impl Worker { rlm_depth.unwrap_or(0), ); created.set_path(path.clone()); - let acquired = { - let path = path.clone(); - let agent_dir = self.config.agent_dir.clone(); - tokio::task::spawn_blocking(move || { - crate::lease::acquire_runtime_session_lease(&path, &agent_dir) - }) - .await - .map_err(anyhow::Error::from) - .and_then(|lease| lease) - }; - match acquired { - Ok(lease) => created.lease = Some(Arc::new(lease)), - Err(error) => return crate::hold_refusal::create_failure_response(&error), + if crate::session_transport::transports_session_log(&self.config.agent_dir, path) { + // THE transport switch's fresh path-backed arm: the log's + // flock stands in for the lease file. The deferred wire's + // first flush (the rewrite below) creates the log and + // writes the header row, re-keyed to the path's id. + created.attach_log_wire(crate::session_transport::LogWire::deferred( + &self.config.agent_dir, + )); + if let Err(error) = created.rekey_log_session_id(path) { + return response_failure(None, "create", &error.to_string(), None); + } + } else { + let acquired = { + let path = path.clone(); + let agent_dir = self.config.agent_dir.clone(); + tokio::task::spawn_blocking(move || { + crate::lease::acquire_runtime_session_lease(&path, &agent_dir) + }) + .await + .map_err(anyhow::Error::from) + .and_then(|lease| lease) + }; + match acquired { + Ok(lease) => created.lease = Some(Arc::new(lease)), + Err(error) => return crate::hold_refusal::create_failure_response(&error), + } } if let Err(error) = created.rewrite() { return response_failure(None, "create", &error.to_string(), None); @@ -342,19 +379,28 @@ impl Worker { ); let path = session_dir.join(session_file_name(created.session_id())); created.set_path(path.clone()); - let acquired = { - let path = path.clone(); - let agent_dir = self.config.agent_dir.clone(); - tokio::task::spawn_blocking(move || { - crate::lease::acquire_runtime_session_lease(&path, &agent_dir) - }) - .await - .map_err(anyhow::Error::from) - .and_then(|lease| lease) - }; - match acquired { - Ok(lease) => created.lease = Some(Arc::new(lease)), - Err(error) => return crate::hold_refusal::create_failure_response(&error), + if crate::session_transport::transports_session_log(&self.config.agent_dir, &path) { + // THE transport switch's fresh minted-id arm: same log + // wire, same lease stand-in; the minted id already names + // both the file and the log, so no re-key. + created.attach_log_wire(crate::session_transport::LogWire::deferred( + &self.config.agent_dir, + )); + } else { + let acquired = { + let path = path.clone(); + let agent_dir = self.config.agent_dir.clone(); + tokio::task::spawn_blocking(move || { + crate::lease::acquire_runtime_session_lease(&path, &agent_dir) + }) + .await + .map_err(anyhow::Error::from) + .and_then(|lease| lease) + }; + match acquired { + Ok(lease) => created.lease = Some(Arc::new(lease)), + Err(error) => return crate::hold_refusal::create_failure_response(&error), + } } // One durable write instead of three: the prefix, the `active` state, // and the session name land in a single rewrite; the create stays -- knotmirror 0.0.1 From 77fe2c0ce35bfeff02b4de1b1ee2106a8fa1252c Mon Sep 17 00:00:00 2001 From: Willow Sparks Date: Fri, 30 Oct 2026 12:01:02 +0000 Subject: [PATCH 4/6] refactor(daemon): delete revision tokens and dual readers (slice 1) --- .../src/session_store/canonical_raw.rs | 1462 ----------------- .../src/session_store/canonical_revision.rs | 765 --------- crates/pa-daemon/src/session_transport.rs | 157 +- crates/pa-daemon/src/worker/create.rs | 142 +- .../src/worker/create_collapse_tests.rs | 341 ---- .../pa-daemon/src/worker/tests/goal_tests.rs | 24 +- .../src/worker/tests/kill_broadcast_tests.rs | 87 +- 7 files changed, 194 insertions(+), 2784 deletions(-) delete mode 100644 crates/pa-daemon/src/session_store/canonical_raw.rs delete mode 100644 crates/pa-daemon/src/session_store/canonical_revision.rs delete mode 100644 crates/pa-daemon/src/worker/create_collapse_tests.rs diff --git a/crates/pa-daemon/src/session_store/canonical_raw.rs b/crates/pa-daemon/src/session_store/canonical_raw.rs deleted file mode 100644 index 425869fa8..000000000 --- a/crates/pa-daemon/src/session_store/canonical_raw.rs +++ /dev/null @@ -1,1462 +0,0 @@ -//! Strict physical journal capture. This module grants no runtime owner authority. -//! No usage overlay, derived loader, sidecar, or journal write is used here. -#![allow(dead_code)] // Private primitives land before the owner adapter. - -use pa_core::session::canonical::{ - CanonicalCheckpoint, CanonicalCursor, CanonicalFrontier, CanonicalJournalRow, - CanonicalOwnerScope, CanonicalSource, CanonicalSourceRequest, -}; -use pa_types::session::{AgentMessage, FileEntry}; -use serde_json::Value; -use sha2::{Digest, Sha256}; -use std::collections::{HashMap, VecDeque}; -use std::fs::{self, File, Metadata}; -use std::io::{BufRead, BufReader, Read}; -use std::os::unix::fs::{FileExt, MetadataExt}; -use std::path::Path; -use std::sync::Arc; - -const MAX_ROW_BYTES: u64 = 16 * 1024 * 1024; -const MAX_TAIL_BYTES: u64 = 64 * 1024 * 1024; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum RawErrorKind { - Io, - Malformed, - InvalidGraph, - SourceChanged, - CursorChanged, - BranchChanged, - Limit, - WindowUnavailable, -} -#[derive(Debug, thiserror::Error)] -#[error("{kind:?}: {detail}")] -pub(crate) struct RawError { - pub kind: RawErrorKind, - pub detail: String, -} -type Result = std::result::Result; -fn error(kind: RawErrorKind, detail: impl Into) -> RawError { - RawError { - kind, - detail: detail.into(), - } -} -#[allow(clippy::needless_pass_by_value)] // `map_err` supplies an owned I/O error. -fn io(e: std::io::Error) -> RawError { - error(RawErrorKind::Io, e.to_string()) -} -fn checked(a: u64, b: u64) -> Result { - a.checked_add(b) - .ok_or_else(|| error(RawErrorKind::Limit, "physical frontier overflow")) -} -fn digest(bytes: &[u8]) -> [u8; 32] { - Sha256::digest(bytes).into() -} - -/// Values supplied by the adapter after it validates its actual weak owner/lease. -#[derive(Debug, Clone)] -pub(crate) struct RawPosition { - pub owner: CanonicalOwnerScope, - pub canonical_revision: u64, - pub branch_revision: u64, - pub selected_leaf: Option, -} -#[derive(Debug, Clone, Copy)] -pub(crate) enum CaptureMode { - Full, - /// Bodies are bounded; full graph, anchor and exact-span metadata remain O(rows). - Sparse { - recent_rows: usize, - retained_bytes: usize, - }, -} -/// Deliberately excludes atime: our own reads must not invalidate a capture. -#[derive(Debug, Clone, PartialEq, Eq)] -struct Generation { - dev: u64, - ino: u64, - size: u64, - mtime: i64, - mtime_ns: i64, - ctime: i64, - ctime_ns: i64, -} -impl Generation { - fn of(m: &Metadata) -> Self { - Self { - dev: m.dev(), - ino: m.ino(), - size: m.len(), - mtime: m.mtime(), - mtime_ns: m.mtime_nsec(), - ctime: m.ctime(), - ctime_ns: m.ctime_nsec(), - } - } - fn certificate(&self) -> [u8; 32] { - let mut h = Sha256::new(); - h.update(b"canonical-raw-generation-v1\0"); - for v in [self.dev, self.ino, self.size] { - h.update(v.to_le_bytes()); - } - for v in [self.mtime, self.mtime_ns, self.ctime, self.ctime_ns] { - h.update(v.to_le_bytes()); - } - h.finalize().into() - } -} -/// Not caller-constructible: only a span registered in this capture can be read. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct RowSpan { - offset: u64, - len: u64, - row: u64, - certificate: [u8; 32], -} -impl RowSpan { - pub fn offset(&self) -> u64 { - self.offset - } - pub fn len(&self) -> u64 { - self.len - } - pub fn physical_row(&self) -> u64 { - self.row - } -} -#[derive(Debug, Clone)] -pub(crate) struct RowMetadata { - pub span: RowSpan, - pub id: Option, - pub parent_id: Option, - pub kind: String, - pub assistant: bool, - /// Exact prior IDs referenced by compaction, branch summary, label or attribution. - pub anchors: Vec, -} -#[derive(Debug)] -pub(crate) struct SparsePlan { - /// Every physical non-header row. Bodies are not retained by this index. - pub rows: Vec, - /// Rows with no ID, including unknown global metadata. - pub global_rows: Vec, - /// Every non-message metadata row, including ID-bearing session-wide records. - pub metadata_rows: Vec, - /// IDs on selected ancestry, oldest first; spans remain in `rows`. - pub ancestry: Vec, - /// All references, including usage targets omitted from retained bodies. - pub anchor_rows: Vec, -} -#[derive(Debug)] -pub(crate) struct RawCapture { - pub cursor: CanonicalCursor, - pub header: CanonicalJournalRow, - pub retained: Vec, - pub plan: SparsePlan, - file: Arc, - generation: Generation, - by_id: HashMap, -} -#[derive(Debug)] -pub(crate) struct RawAppend { - pub capture: RawCapture, - /// Complete contiguous suffix; never filtered to a selected branch. - pub rows: Vec, -} - -impl RawCapture { - /// Verify an earlier owner cursor against this exact full physical capture. - /// The owner separately restricts which suffix rows may precede a terminal. - pub fn certified_suffix_since( - &self, - expected: &CanonicalCursor, - ) -> Result> { - guard(&self.file, &self.cursor.source_path, &self.generation)?; - if expected == &self.cursor { - return Ok(Vec::new()); - } - if expected.owner != self.cursor.owner - || expected.session_id != self.cursor.session_id - || expected.source_path != self.cursor.source_path - || expected.header_certificate != self.cursor.header_certificate - || expected.branch_revision != self.cursor.branch_revision - || expected.canonical_revision >= self.cursor.canonical_revision - || expected.frontier.rows >= self.cursor.frontier.rows - || expected.frontier.bytes >= self.cursor.frontier.bytes - || self.cursor.frontier.bytes - expected.frontier.bytes > MAX_TAIL_BYTES - || self.retained.len() != self.plan.rows.len() - { - return Err(error( - RawErrorKind::CursorChanged, - "terminal source is not an earlier owned prefix", - )); - } - let prefix_rows = expected - .frontier - .rows - .checked_sub(1) - .and_then(|rows| usize::try_from(rows).ok()) - .filter(|rows| *rows < self.retained.len()) - .ok_or_else(|| { - error( - RawErrorKind::CursorChanged, - "terminal prefix row count is invalid", - ) - })?; - let mut hash = Sha256::new(); - hash.update(self.header.raw()); - let mut bytes = self.header.raw().len() as u64; - for row in &self.retained[..prefix_rows] { - hash.update(row.raw()); - bytes = checked(bytes, row.raw().len() as u64)?; - } - if bytes != expected.frontier.bytes - || <[u8; 32]>::from(hash.finalize()) != expected.frontier.prefix_certificate - || self.cursor.canonical_revision - expected.canonical_revision - != self.cursor.frontier.rows - expected.frontier.rows - { - return Err(error( - RawErrorKind::SourceChanged, - "terminal prefix certificate differs", - )); - } - Ok(self.retained[prefix_rows..].to_vec()) - } - - /// Standalone request helper until a sparse derived-store factory is available. - /// `PreferWindow` scans with a 1 MiB body budget even if the file grows during open. - pub fn capture_checkpoint( - path: &Path, - position: RawPosition, - request: CanonicalSourceRequest, - ) -> Result { - let mode = match request { - CanonicalSourceRequest::Full => CaptureMode::Full, - CanonicalSourceRequest::PreferWindow => CaptureMode::Sparse { - recent_rows: usize::MAX, - retained_bytes: 1024 * 1024, - }, - }; - Self::capture(path, position, mode)?.checkpoint(request) - } - /// No sparse derived-store factory exists here. Never label a sparse view Full, - /// and never expand a large `PreferWindow` request into all history bodies. - pub fn checkpoint(&self, request: CanonicalSourceRequest) -> Result { - if (request == CanonicalSourceRequest::PreferWindow - && self.cursor.frontier.bytes > 1024 * 1024) - || self.retained.len() != self.plan.rows.len() - { - return Err(error(RawErrorKind::WindowUnavailable, "sparse raw plan is certified, but CanonicalWindow derived factory is not implemented")); - } - guard(&self.file, &self.cursor.source_path, &self.generation)?; - Ok(CanonicalCheckpoint { - cursor: self.cursor.clone(), - header: self.header.clone(), - source: CanonicalSource::Full(self.retained.clone()), - }) - } - /// Check a prior full cursor against the retained physical prefix and append ancestry. - /// The caller must separately verify the live owner and current physical source. - pub(super) fn certifies_prior_prefix(&self, expected: &CanonicalCursor) -> bool { - if expected == &self.cursor { - return true; - } - if expected.owner != self.cursor.owner - || expected.session_id != self.cursor.session_id - || expected.source_path != self.cursor.source_path - || expected.header_certificate != self.cursor.header_certificate - || expected.branch_revision != self.cursor.branch_revision - || expected.canonical_revision > self.cursor.canonical_revision - || expected.frontier.rows == 0 - || expected.frontier.rows > self.cursor.frontier.rows - || expected.frontier.bytes > self.cursor.frontier.bytes - || self.retained.len() != self.plan.rows.len() - { - return false; - } - if expected.frontier == self.cursor.frontier { - return expected.raw_source_generation == self.cursor.raw_source_generation - && expected.selected_leaf == self.cursor.selected_leaf; - } - if expected.canonical_revision == self.cursor.canonical_revision { - return false; - } - let mut hash = Sha256::new(); - let mut bytes = 0_u64; - let mut rows = 0_u64; - for row in std::iter::once(&self.header).chain(&self.retained) { - if rows == expected.frontier.rows { - break; - } - hash.update(row.raw()); - bytes += row.raw().len() as u64; - rows += 1; - } - if rows != expected.frontier.rows - || bytes != expected.frontier.bytes - || <[u8; 32]>::from(hash.finalize()) != expected.frontier.prefix_certificate - { - return false; - } - let mut walk = self.cursor.selected_leaf.as_deref(); - while walk != expected.selected_leaf.as_deref() { - let Some(index) = walk.and_then(|id| self.by_id.get(id)).copied() else { - return false; - }; - if self.plan.rows[index].span.row < expected.frontier.rows { - return false; - } - walk = self.plan.rows[index].parent_id.as_deref(); - } - expected.selected_leaf.as_ref().is_none_or(|id| { - self.by_id - .get(id) - .is_some_and(|index| self.plan.rows[*index].span.row < expected.frontier.rows) - }) - } - - /// Refresh only the logical owner position after a locked ledger transaction. - /// Physical identity, bytes, graph and generation remain exactly unchanged. - pub(super) fn refresh_owner_position(&mut self, position: &RawPosition) { - debug_assert_eq!(position.owner, self.cursor.owner); - debug_assert_eq!(position.branch_revision, self.cursor.branch_revision); - debug_assert_eq!(position.selected_leaf, self.cursor.selected_leaf); - self.cursor.canonical_revision = position.canonical_revision; - } - - pub fn capture(path: &Path, position: RawPosition, mode: CaptureMode) -> Result { - Self::scan(path, position, mode, None).map(|(capture, _)| capture) - } - /// Validates the exact prior cursor and complete physical prefix before returning a tail. - /// Caller-provided owner/revisions are comparisons, not runtime ownership proof. - pub fn append_from( - prior: &Self, - expected: &CanonicalCursor, - position: RawPosition, - mode: CaptureMode, - ) -> Result { - if expected != &prior.cursor - || position.owner != expected.owner - || position.canonical_revision < expected.canonical_revision - { - return Err(error( - RawErrorKind::CursorChanged, - "prior cursor/owner/revision mismatch", - )); - } - if position.branch_revision != expected.branch_revision { - return Err(error( - RawErrorKind::BranchChanged, - "branch revision changed", - )); - } - let (capture, rows) = Self::scan(&expected.source_path, position, mode, Some(prior))?; - if capture.cursor.session_id != expected.session_id - || capture.cursor.header_certificate != expected.header_certificate - { - return Err(error(RawErrorKind::SourceChanged, "header changed")); - } - if capture.cursor.selected_leaf != expected.selected_leaf { - let mut walk = capture.cursor.selected_leaf.as_deref(); - while walk != expected.selected_leaf.as_deref() { - let Some(id) = walk else { - return Err(error( - RawErrorKind::BranchChanged, - "new selected leaf is not a descendant", - )); - }; - let index = capture.by_id[id]; - if capture.plan.rows[index].span.row < expected.frontier.rows { - return Err(error( - RawErrorKind::BranchChanged, - "selection moved into the old graph", - )); - } - walk = capture.plan.rows[index].parent_id.as_deref(); - } - } - Ok(RawAppend { capture, rows }) - } - fn scan( - path: &Path, - position: RawPosition, - mode: CaptureMode, - prior: Option<&Self>, - ) -> Result<(Self, Vec)> { - if !path.is_absolute() { - return Err(error( - RawErrorKind::Malformed, - "source path must be absolute", - )); - } - let before = fs::metadata(path).map_err(io)?; - if !before.is_file() { - return Err(error( - RawErrorKind::Malformed, - "source is not a regular file", - )); - } - let file = Arc::new(File::open(path).map_err(io)?); - let generation = Generation::of(&file.metadata().map_err(io)?); - if generation != Generation::of(&before) { - return Err(error( - RawErrorKind::SourceChanged, - "path changed during open", - )); - } - if let Some(old) = prior { - if old.generation.dev != generation.dev - || old.generation.ino != generation.ino - || generation.size < old.generation.size - || old.cursor.raw_source_generation != old.generation.certificate() - { - return Err(error( - RawErrorKind::SourceChanged, - "replaced, truncated, or stale source", - )); - } - if generation.size == old.generation.size && generation != old.generation { - return Err(error( - RawErrorKind::SourceChanged, - "same-size generation changed", - )); - } - if generation.size - old.generation.size > MAX_TAIL_BYTES { - return Err(error( - RawErrorKind::Limit, - "complete tail exceeds bounded receipt limit", - )); - } - } - // `try_clone` shares the fd offset, so only the initial scan uses sequential I/O; - // all lazy reads use pread and cannot move that offset. - let mut reader = BufReader::new(file.try_clone().map_err(io)?); - let mut hasher = Sha256::new(); - let (mut offset, mut ordinal) = (0_u64, 0_u64); - let mut header: Option = None; - let mut session_id = String::new(); - let mut rows: Vec = Vec::new(); - let mut by_id: HashMap = HashMap::new(); - let mut retained = VecDeque::new(); - let mut retained_bytes = 0_usize; - let mut tail = Vec::new(); - let mut prefix_checked = prior.is_none(); - loop { - let mut bytes = Vec::new(); - let count = reader - .by_ref() - .take(MAX_ROW_BYTES + 1) - .read_until(b'\n', &mut bytes) - .map_err(io)?; - if count == 0 { - break; - } - if u64::try_from(count) - .map_err(|_| error(RawErrorKind::Limit, "row length overflow"))? - > MAX_ROW_BYTES - { - return Err(error(RawErrorKind::Limit, "physical row exceeds limit")); - } - let mut json = serde_json::Deserializer::from_slice(&bytes); - ::deserialize(&mut json) - .and_then(|_| json.end()) - .map_err(|e| error(RawErrorKind::Malformed, format!("row {ordinal}: {e}")))?; - let row = CanonicalJournalRow::parse(Arc::from(bytes), offset, ordinal) - .map_err(|e| error(RawErrorKind::Malformed, format!("row {ordinal}: {e}")))?; - let value: Value = serde_json::from_slice(row.raw()) - .map_err(|e| error(RawErrorKind::Malformed, e.to_string()))?; - let map = value - .as_object() - .ok_or_else(|| error(RawErrorKind::Malformed, "row is not an object"))?; - let kind = map - .get("type") - .and_then(Value::as_str) - .filter(|v| !v.is_empty()) - .ok_or_else(|| error(RawErrorKind::Malformed, "missing string type"))?; - if ordinal == 0 { - let FileEntry::Header { header: parsed } = row.entry() else { - return Err(error( - RawErrorKind::Malformed, - "first row is not a valid session header", - )); - }; - session_id.clone_from(&parsed.id); - header = Some(row.clone()); - } else { - if kind == "session" { - return Err(error(RawErrorKind::Malformed, "duplicate header")); - } - if matches!(row.entry(), FileEntry::Unknown { .. }) && known_kind(kind) { - return Err(error( - RawErrorKind::Malformed, - format!("malformed known payload: {kind}"), - )); - } - let id = string_field(map, "id")?; - let parent_id = string_field(map, "parentId")?; - let _ = string_field(map, "timestamp")?; - if let Some(id) = &id { - if id == &session_id || by_id.contains_key(id) { - return Err(error(RawErrorKind::InvalidGraph, "duplicate entry ID")); - } - } else if parent_id.is_some() || !global_kind(kind) { - return Err(error(RawErrorKind::InvalidGraph, "tree row without ID")); - } - if let Some(parent) = &parent_id { - if !by_id.contains_key(parent) { - return Err(error( - RawErrorKind::InvalidGraph, - "parent is not a prior graph row", - )); - } - } - let anchors = anchors(row.entry()); - for anchor in &anchors { - let target = by_id - .get(anchor) - .and_then(|i| rows.get(*i)) - .ok_or_else(|| { - error( - RawErrorKind::InvalidGraph, - "reference is not a prior graph row", - ) - })?; - if matches!(row.entry(), FileEntry::ChildUsageAttributed { .. }) - && !target.assistant - { - return Err(error( - RawErrorKind::InvalidGraph, - "usage target is not an assistant row", - )); - } - } - let span = RowSpan { - offset, - len: u64::try_from(count) - .map_err(|_| error(RawErrorKind::Limit, "span length overflow"))?, - row: ordinal, - certificate: digest(row.raw()), - }; - let meta = RowMetadata { - span, - id: id.clone(), - parent_id, - kind: kind.to_owned(), - assistant: matches!( - row.entry(), - FileEntry::Message { - message: AgentMessage::Assistant(_), - .. - } - ), - anchors, - }; - if let Some(id) = id { - by_id.insert(id, rows.len()); - } - rows.push(meta); - retained_bytes = retained_bytes - .checked_add(count) - .ok_or_else(|| error(RawErrorKind::Limit, "retained bytes overflow"))?; - retained.push_back(row.clone()); - if let CaptureMode::Sparse { - recent_rows, - retained_bytes: cap, - } = mode - { - while retained.len() > recent_rows || retained_bytes > cap { - if let Some(removed) = retained.pop_front() { - retained_bytes -= removed.raw().len(); - } - } - } - if prior.is_some_and(|old| ordinal >= old.cursor.frontier.rows) { - tail.push(row.clone()); - } - } - hasher.update(row.raw()); - offset = checked( - offset, - u64::try_from(count) - .map_err(|_| error(RawErrorKind::Limit, "byte count overflow"))?, - )?; - ordinal = checked(ordinal, 1)?; - if let Some(old) = prior { - if offset == old.cursor.frontier.bytes { - if ordinal != old.cursor.frontier.rows - || <[u8; 32]>::from(hasher.clone().finalize()) - != old.cursor.frontier.prefix_certificate - { - return Err(error( - RawErrorKind::SourceChanged, - "prior physical prefix changed", - )); - } - prefix_checked = true; - } else if offset > old.cursor.frontier.bytes && !prefix_checked { - return Err(error( - RawErrorKind::SourceChanged, - "prior frontier is not a row boundary", - )); - } - } - } - if !prefix_checked { - return Err(error(RawErrorKind::SourceChanged, "missing prior prefix")); - } - let header = header.ok_or_else(|| error(RawErrorKind::Malformed, "empty journal"))?; - if generation.size != offset { - return Err(error(RawErrorKind::SourceChanged, "captured size differs")); - } - guard(&file, path, &generation)?; - let mut ancestry = Vec::new(); - let mut leaf = position.selected_leaf.as_deref(); - while let Some(id) = leaf { - let index = *by_id - .get(id) - .ok_or_else(|| error(RawErrorKind::InvalidGraph, "selected leaf does not exist"))?; - ancestry.push(index); - leaf = rows[index].parent_id.as_deref(); - } - ancestry.reverse(); - let global_rows = rows - .iter() - .enumerate() - .filter_map(|(i, row)| row.id.is_none().then_some(i)) - .collect(); - let metadata_rows = rows - .iter() - .enumerate() - .filter_map(|(i, row)| (row.kind != "message").then_some(i)) - .collect(); - let anchor_rows = rows - .iter() - .enumerate() - .filter_map(|(i, row)| (!row.anchors.is_empty()).then_some(i)) - .collect(); - let cursor = CanonicalCursor { - owner: position.owner, - session_id, - source_path: path.to_path_buf(), - raw_source_generation: generation.certificate(), - header_certificate: digest(header.raw()), - canonical_revision: position.canonical_revision, - branch_revision: position.branch_revision, - selected_leaf: position.selected_leaf, - frontier: CanonicalFrontier { - bytes: offset, - rows: ordinal, - prefix_certificate: hasher.finalize().into(), - }, - }; - Ok(( - Self { - cursor, - header, - retained: retained.into_iter().collect(), - plan: SparsePlan { - rows, - global_rows, - metadata_rows, - ancestry, - anchor_rows, - }, - file, - generation, - by_id, - }, - tail, - )) - } - /// Reads only a registered exact span from the captured fd and validates path generation. - pub fn read_span( - &self, - cursor: &CanonicalCursor, - span: &RowSpan, - ) -> Result { - if cursor != &self.cursor { - return Err(error(RawErrorKind::CursorChanged, "lazy cursor mismatch")); - } - let index = span - .row - .checked_sub(1) - .and_then(|n| usize::try_from(n).ok()) - .ok_or_else(|| error(RawErrorKind::InvalidGraph, "header is not a lazy row"))?; - if self - .plan - .rows - .get(index) - .is_none_or(|meta| &meta.span != span) - { - return Err(error(RawErrorKind::InvalidGraph, "span not in capture")); - } - guard(&self.file, &cursor.source_path, &self.generation)?; - let mut bytes = - vec![ - 0; - usize::try_from(span.len) - .map_err(|_| error(RawErrorKind::Limit, "span allocation overflow"))? - ]; - self.file - .read_exact_at(&mut bytes, span.offset) - .map_err(io)?; - guard(&self.file, &cursor.source_path, &self.generation)?; - if digest(&bytes) != span.certificate { - return Err(error(RawErrorKind::SourceChanged, "lazy row changed")); - } - CanonicalJournalRow::parse(Arc::from(bytes), span.offset, span.row) - .map_err(|e| error(RawErrorKind::Malformed, e.to_string())) - } -} -fn guard(file: &File, path: &Path, expected: &Generation) -> Result<()> { - if Generation::of(&file.metadata().map_err(io)?) != *expected - || Generation::of(&fs::metadata(path).map_err(io)?) != *expected - { - return Err(error( - RawErrorKind::SourceChanged, - "file or path generation changed", - )); - } - Ok(()) -} -fn string_field(map: &serde_json::Map, key: &str) -> Result> { - match map.get(key) { - None | Some(Value::Null) => Ok(None), - Some(Value::String(v)) if !v.is_empty() => Ok(Some(v.clone())), - _ => Err(error(RawErrorKind::Malformed, format!("invalid {key}"))), - } -} -fn known_kind(kind: &str) -> bool { - matches!( - kind, - "session" - | "message" - | "thinking_level_change" - | "service_tier_change" - | "model_change" - | "compaction" - | "branch_summary" - | "custom" - | "child_usage_attributed" - | "label" - | "session_info" - | "session_state" - | "git_state" - | "custom_message" - ) -} -fn global_kind(kind: &str) -> bool { - !known_kind(kind) || matches!(kind, "session_info" | "session_state" | "git_state") -} -fn anchors(entry: &FileEntry) -> Vec { - match entry { - FileEntry::Compaction { payload, .. } => { - let mut ids: Vec<_> = payload.first_kept_entry_id.iter().cloned().collect(); - if let Some(archive) = &payload.context_archive { - ids.push(archive.prepared_leaf.clone()); - ids.extend(archive.covered_entry_ids.iter().cloned()); - } - ids - } - FileEntry::BranchSummary { payload, .. } => vec![payload.from_id.clone()], - FileEntry::ChildUsageAttributed { payload, .. } => vec![payload.target_id.clone()], - FileEntry::Label { payload, .. } => vec![payload.target_id.clone()], - _ => Vec::new(), - } -} - -/// Validate every nested object before `FileEntry`'s intentional lenient fallback. -struct StrictJson; -impl<'de> serde::Deserialize<'de> for StrictJson { - fn deserialize>(d: D) -> std::result::Result { - struct Visitor; - impl<'de> serde::de::Visitor<'de> for Visitor { - type Value = StrictJson; - fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("JSON without duplicate keys") - } - fn visit_map>( - self, - mut map: M, - ) -> std::result::Result { - let mut keys = std::collections::HashSet::new(); - while let Some(key) = map.next_key::()? { - if !keys.insert(key) { - return Err(serde::de::Error::custom("duplicate JSON key")); - } - map.next_value::()?; - } - Ok(StrictJson) - } - fn visit_seq>( - self, - mut seq: S, - ) -> std::result::Result { - while seq.next_element::()?.is_some() {} - Ok(StrictJson) - } - fn visit_bool( - self, - _: bool, - ) -> std::result::Result { - Ok(StrictJson) - } - fn visit_i64(self, _: i64) -> std::result::Result { - Ok(StrictJson) - } - fn visit_u64(self, _: u64) -> std::result::Result { - Ok(StrictJson) - } - fn visit_f64(self, _: f64) -> std::result::Result { - Ok(StrictJson) - } - fn visit_str(self, _: &str) -> std::result::Result { - Ok(StrictJson) - } - fn visit_unit(self) -> std::result::Result { - Ok(StrictJson) - } - } - d.deserialize_any(Visitor) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use serde_json::json; - use std::fmt::Write as _; - use std::io::Write; - use std::path::PathBuf; - use tempfile::TempDir; - - const HEADER: &str = "{\"type\":\"session\",\"version\":3,\"id\":\"s\",\"timestamp\":\"t\",\"cwd\":\"/w\",\"unknownHeader\":{\"x\":1}}\r\n"; - const A: &str = " {\"type\":\"future_kind\",\"id\":\"a\",\"parentId\":null,\"unknown\":{\"z\":2,\"a\":[1,true]}}\r\n"; - const B: &str = "{\"type\":\"future_kind\",\"id\":\"b\",\"parentId\":null}\n"; - const META: &str = "{\"type\":\"future_global\",\"unmodeled\": {\"bytes\":\"\\u0061\"}}\n"; - fn source(bytes: &[u8]) -> (TempDir, PathBuf) { - let dir = TempDir::new().unwrap(); - let path = dir.path().join("s.jsonl"); - fs::write(&path, bytes).unwrap(); - (dir, path) - } - fn position(leaf: Option<&str>) -> RawPosition { - RawPosition { - owner: CanonicalOwnerScope::new(), - canonical_revision: 7, - branch_revision: 3, - selected_leaf: leaf.map(str::to_owned), - } - } - fn next(old: &RawCapture, leaf: Option<&str>) -> RawPosition { - RawPosition { - owner: old.cursor.owner.clone(), - canonical_revision: old.cursor.canonical_revision + 1, - branch_revision: old.cursor.branch_revision, - selected_leaf: leaf.map(str::to_owned), - } - } - fn append(path: &Path, bytes: &[u8]) { - File::options() - .append(true) - .open(path) - .unwrap() - .write_all(bytes) - .unwrap(); - } - fn capture(path: &Path, leaf: Option<&str>) -> RawCapture { - RawCapture::capture(path, position(leaf), CaptureMode::Full).unwrap() - } - fn reject(bytes: &[u8]) -> RawError { - let (_dir, path) = source(bytes); - RawCapture::capture(&path, position(None), CaptureMode::Full).unwrap_err() - } - - #[test] - fn exact_all_physical_rows_branches_unknown_crlf_and_certificates() { - let bytes = format!("{HEADER}{A}{B}{META}"); - let (_dir, path) = source(bytes.as_bytes()); - let c = capture(&path, Some("a")); - assert_eq!(c.header.raw(), HEADER.as_bytes()); - assert_eq!( - c.retained - .iter() - .flat_map(|r| r.raw().iter().copied()) - .collect::>(), - format!("{A}{B}{META}").as_bytes() - ); - assert_eq!( - c.cursor.frontier, - CanonicalFrontier { - bytes: bytes.len() as u64, - rows: 4, - prefix_certificate: digest(bytes.as_bytes()) - } - ); - assert_eq!(c.cursor.header_certificate, digest(HEADER.as_bytes())); - assert_eq!(c.plan.ancestry, vec![0]); - assert_eq!(c.plan.global_rows, vec![2]); - let mut offset = HEADER.len() as u64; - for (i, r) in c.retained.iter().enumerate() { - assert_eq!((r.byte_offset(), r.physical_row()), (offset, i as u64 + 1)); - assert_eq!( - (c.plan.rows[i].span.offset(), c.plan.rows[i].span.len()), - (offset, r.raw().len() as u64) - ); - offset += r.raw().len() as u64; - } - let FileEntry::Unknown { rest } = c.retained[0].entry() else { - panic!("unknown lost"); - }; - assert_eq!(rest["unknown"], json!({"z":2,"a":[1,true]})); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[1].span).unwrap().raw(), - B.as_bytes() - ); - } - #[test] - fn append_returns_complete_contiguous_tail_including_other_branch_and_global() { - let (_dir, path) = source(format!("{HEADER}{A}{B}").as_bytes()); - let c = capture(&path, Some("a")); - let tail = format!("{META}{{\"type\":\"future\",\"id\":\"off\",\"parentId\":\"b\"}}\n{{\"type\":\"future\",\"id\":\"on\",\"parentId\":\"a\"}}\n"); - append(&path, tail.as_bytes()); - let result = RawCapture::append_from( - &c, - &c.cursor, - next(&c, Some("on")), - CaptureMode::Sparse { - recent_rows: 1, - retained_bytes: 1024, - }, - ) - .unwrap(); - assert_eq!( - result - .rows - .iter() - .flat_map(|r| r.raw().iter().copied()) - .collect::>(), - tail.as_bytes() - ); - assert_eq!(result.rows[0].byte_offset(), c.cursor.frontier.bytes); - assert_eq!(result.rows[0].physical_row(), c.cursor.frontier.rows); - assert_eq!(result.capture.plan.rows.len(), 5); - assert_eq!(result.capture.retained.len(), 1); - assert_eq!( - result.capture.cursor.frontier.prefix_certificate, - digest(&fs::read(&path).unwrap()) - ); - } - #[test] - fn prior_prefix_certifies_only_exact_boundaries_and_append_ancestry() { - let (_dir, path) = source(format!("{HEADER}{A}{B}").as_bytes()); - let old = capture(&path, Some("a")); - append( - &path, - format!("{META}{{\"type\":\"future\",\"id\":\"on\",\"parentId\":\"a\"}}\n").as_bytes(), - ); - let current = - RawCapture::append_from(&old, &old.cursor, next(&old, Some("on")), CaptureMode::Full) - .unwrap() - .capture; - assert!(current.certifies_prior_prefix(&old.cursor)); - assert!(current.certifies_prior_prefix(¤t.cursor)); - let mut logical = current.cursor.clone(); - logical.canonical_revision -= 1; - assert!(current.certifies_prior_prefix(&logical)); - let mut forged = old.cursor.clone(); - forged.frontier.prefix_certificate[0] ^= 1; - assert!(!current.certifies_prior_prefix(&forged)); - forged = old.cursor.clone(); - forged.frontier.bytes -= 1; - assert!(!current.certifies_prior_prefix(&forged)); - forged = old.cursor.clone(); - forged.frontier.rows -= 1; - assert!(!current.certifies_prior_prefix(&forged)); - forged = old.cursor.clone(); - forged.selected_leaf = Some("b".into()); - assert!(!current.certifies_prior_prefix(&forged)); - forged = current.cursor.clone(); - forged.raw_source_generation[0] ^= 1; - assert!(!current.certifies_prior_prefix(&forged)); - forged = old.cursor; - forged.canonical_revision = current.cursor.canonical_revision; - assert!(!current.certifies_prior_prefix(&forged)); - } - - #[test] - fn unchanged_capture_has_empty_tail() { - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - let c = capture(&path, Some("a")); - assert!( - RawCapture::append_from(&c, &c.cursor, next(&c, Some("a")), CaptureMode::Full) - .unwrap() - .rows - .is_empty() - ); - } - #[test] - fn changed_prefix_and_header_rejected_even_with_append() { - for change_header in [false, true] { - let original = format!("{HEADER}{A}"); - let (_dir, path) = source(original.as_bytes()); - let c = capture(&path, Some("a")); - let changed = if change_header { - original.replacen("\"cwd\":\"/w\"", "\"cwd\":\"/x\"", 1) - } else { - original.replacen("\"z\":2", "\"z\":3", 1) - }; - fs::write(&path, format!("{changed}{META}")).unwrap(); - assert_eq!( - RawCapture::append_from(&c, &c.cursor, next(&c, Some("a")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::SourceChanged - ); - } - } - #[test] - fn identical_bytes_replacement_is_not_append_or_valid_lazy_source() { - let bytes = format!("{HEADER}{A}"); - let (_dir, path) = source(bytes.as_bytes()); - let c = capture(&path, Some("a")); - let replacement = path.with_extension("replacement"); - fs::write(&replacement, bytes).unwrap(); - fs::rename(replacement, &path).unwrap(); - assert_eq!( - RawCapture::append_from(&c, &c.cursor, next(&c, Some("a")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::SourceChanged - ); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[0].span) - .unwrap_err() - .kind, - RawErrorKind::SourceChanged - ); - } - #[test] - fn same_size_stale_generation_and_truncation_fail_closed() { - let bytes = format!("{HEADER}{A}"); - for truncate in [false, true] { - let (_dir, path) = source(bytes.as_bytes()); - let c = capture(&path, Some("a")); - if truncate { - fs::write(&path, HEADER).unwrap(); - } else { - filetime::set_file_mtime(&path, filetime::FileTime::from_unix_time(42, 0)).unwrap(); - } - assert_eq!( - RawCapture::append_from(&c, &c.cursor, next(&c, Some("a")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::SourceChanged - ); - } - } - #[test] - fn malformed_blank_unterminated_duplicate_headers_and_keys_rejected() { - for suffix in [ - "\n", - " \r\n", - "{oops}\n", - "{}\n", - "[]\n", - "{\"type\":\"future\"}", - HEADER, - "{\"type\":\"message\",\"id\":\"m\",\"message\":null}\n", - "{\"type\":\"future\",\"id\":22}\n", - "{\"type\":\"future\",\"id\":\"\"}\n", - "{\"type\":\"future\",\"id\":\"x\",\"parentId\":22}\n", - "{\"type\":\"future\",\"id\":\"x\",\"id\":\"y\"}\n", - "{\"type\":\"future\",\"payload\":{\"x\":1,\"x\":2}}\n", - "{\"type\":\"model_change\",\"id\":\"x\",\"provider\":22}\n", - ] { - assert_eq!( - reject(format!("{HEADER}{suffix}").as_bytes()).kind, - RawErrorKind::Malformed, - "suffix {suffix:?}" - ); - } - for bytes in ["", "{}\n", "{\"type\":\"session\",\"id\":\"s\"}\n"] { - assert_eq!(reject(bytes.as_bytes()).kind, RawErrorKind::Malformed); - } - } - #[test] - fn duplicate_ids_missing_forward_self_parents_and_missing_leaf_rejected() { - for suffix in [A, "{\"type\":\"future\",\"id\":\"s\"}\n", "{\"type\":\"future\",\"id\":\"x\",\"parentId\":\"absent\"}\n", - "{\"type\":\"future\",\"id\":\"x\",\"parentId\":\"x\"}\n", - "{\"type\":\"future\",\"id\":\"x\",\"parentId\":\"y\"}\n{\"type\":\"future\",\"id\":\"y\"}\n", - "{\"type\":\"future\",\"parentId\":\"a\"}\n", - "{\"type\":\"message\",\"message\":{\"role\":\"user\",\"content\":\"hello\",\"timestamp\":1}}\n"] { - assert_eq!(reject(format!("{HEADER}{A}{suffix}").as_bytes()).kind,RawErrorKind::InvalidGraph,"suffix {suffix:?}"); - } - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - assert_eq!( - RawCapture::capture(&path, position(Some("missing")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::InvalidGraph - ); - } - #[test] - fn owner_cursor_branch_and_old_sibling_selection_cannot_be_substituted() { - let (_dir, path) = source(format!("{HEADER}{A}{B}").as_bytes()); - let c = capture(&path, Some("a")); - let mut p = next(&c, Some("a")); - p.owner = CanonicalOwnerScope::new(); - assert_eq!( - RawCapture::append_from(&c, &c.cursor, p, CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::CursorChanged - ); - let mut p = next(&c, Some("a")); - p.branch_revision += 1; - assert_eq!( - RawCapture::append_from(&c, &c.cursor, p, CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::BranchChanged - ); - assert_eq!( - RawCapture::append_from(&c, &c.cursor, next(&c, Some("b")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::BranchChanged - ); - let mut cursor = c.cursor.clone(); - cursor.frontier.rows += 1; - assert_eq!( - RawCapture::append_from(&c, &cursor, next(&c, Some("a")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::CursorChanged - ); - } - #[test] - fn sparse_keeps_bounded_bodies_and_lazy_older_raw_not_a_derived_loader() { - let mut bytes = format!("{HEADER}{A}{META}"); - for n in 0..100 { - writeln!( - bytes, - "{{\"type\":\"future\",\"id\":\"r{n}\",\"parentId\":\"a\",\"large\":\"{}\"}}", - "x".repeat(2048) - ) - .unwrap(); - } - let (_dir, path) = source(bytes.as_bytes()); - let c = RawCapture::capture( - &path, - position(Some("r99")), - CaptureMode::Sparse { - recent_rows: 2, - retained_bytes: 6000, - }, - ) - .unwrap(); - assert_eq!(c.plan.rows.len(), 102); - assert_eq!(c.retained.len(), 2); - assert!(c.retained.iter().map(|r| r.raw().len()).sum::() <= 6000); - assert_eq!(c.plan.ancestry, vec![0, 101]); - assert_eq!(c.plan.global_rows, vec![1]); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[0].span).unwrap().raw(), - A.as_bytes() - ); - let mut stale = c.cursor.clone(); - stale.canonical_revision += 1; - assert_eq!( - c.read_span(&stale, &c.plan.rows[0].span).unwrap_err().kind, - RawErrorKind::CursorChanged - ); - append(&path, META.as_bytes()); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[0].span) - .unwrap_err() - .kind, - RawErrorKind::SourceChanged - ); - } - #[test] - fn sparse_usage_anchor_is_certified_without_folding_omitted_assistant() { - let usage = serde_json::to_value(pa_types::ai::Usage::default()).unwrap(); - let assistant=json!({"type":"message","id":"assistant","parentId":null,"message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openai","model":"test","usage":usage,"stopReason":"stop","timestamp":1}}).to_string()+"\n"; - let attribution=json!({"type":"child_usage_attributed","id":"usage","parentId":"assistant","targetId":"assistant","childUsage":usage,"aggregateUsage":usage}).to_string()+"\n"; - let (_dir, path) = source(format!("{HEADER}{assistant}{attribution}{META}").as_bytes()); - let c = RawCapture::capture( - &path, - position(Some("usage")), - CaptureMode::Sparse { - recent_rows: 1, - retained_bytes: 1024, - }, - ) - .unwrap(); - assert_eq!(c.retained.len(), 1); - assert_eq!(c.retained[0].raw(), META.as_bytes()); - assert_eq!(c.plan.anchor_rows, vec![1]); - assert_eq!(c.plan.rows[1].anchors, vec!["assistant"]); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[0].span).unwrap().raw(), - assistant.as_bytes() - ); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[1].span).unwrap().raw(), - attribution.as_bytes() - ); - } - #[test] - fn missing_anchor_and_non_assistant_usage_target_rejected() { - let usage = serde_json::to_value(pa_types::ai::Usage::default()).unwrap(); - for target in ["missing", "a"] { - let attribution=json!({"type":"child_usage_attributed","id":"usage","targetId":target,"childUsage":usage,"aggregateUsage":usage}).to_string()+"\n"; - assert_eq!( - reject(format!("{HEADER}{A}{attribution}").as_bytes()).kind, - RawErrorKind::InvalidGraph - ); - } - } - #[test] - fn checked_frontier_overflow_and_zero_body_sparse() { - assert_eq!(checked(u64::MAX, 1).unwrap_err().kind, RawErrorKind::Limit); - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - let c = RawCapture::capture( - &path, - position(Some("a")), - CaptureMode::Sparse { - recent_rows: 0, - retained_bytes: 0, - }, - ) - .unwrap(); - assert!(c.retained.is_empty()); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[0].span).unwrap().raw(), - A.as_bytes() - ); - } - #[test] - fn certified_suffix_rejects_forged_prefix_and_replaced_current_file() { - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - let prior = capture(&path, Some("a")); - let expected = prior.cursor.clone(); - let appended = "{\"type\":\"future_kind\",\"id\":\"c\",\"parentId\":\"a\"}\n"; - append(&path, appended.as_bytes()); - let current = - RawCapture::capture(&path, next(&prior, Some("c")), CaptureMode::Full).unwrap(); - assert_eq!(current.certified_suffix_since(&expected).unwrap().len(), 1); - let mut forged = expected.clone(); - forged.frontier.prefix_certificate[0] ^= 1; - assert!(current.certified_suffix_since(&forged).is_err()); - forged = expected.clone(); - forged.frontier.bytes -= 1; - assert!(current.certified_suffix_since(&forged).is_err()); - forged = expected.clone(); - forged.branch_revision += 1; - assert!(current.certified_suffix_since(&forged).is_err()); - let replacement = path.with_extension("replacement"); - fs::write(&replacement, fs::read(&path).unwrap()).unwrap(); - fs::rename(&replacement, &path).unwrap(); - assert!(current.certified_suffix_since(&expected).is_err()); - } - - #[test] - fn generation_ignores_atime_but_guards_all_write_generation_fields() { - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - let c = capture(&path, Some("a")); - let before = c.generation.clone(); - let _ = fs::read(&path).unwrap(); - assert_eq!(before, Generation::of(&fs::metadata(&path).unwrap())); - let mut altered = before.clone(); - altered.ctime_ns += 1; - assert_ne!(before.certificate(), altered.certificate()); - assert_eq!( - guard(&c.file, &path, &altered).unwrap_err().kind, - RawErrorKind::SourceChanged - ); - } - #[test] - fn sparse_checkpoint_and_large_prefer_window_fail_typed_until_derived_factory() { - let (_dir, path) = source(format!("{HEADER}{A}{B}").as_bytes()); - let sparse = RawCapture::capture( - &path, - position(Some("a")), - CaptureMode::Sparse { - recent_rows: 1, - retained_bytes: 1024, - }, - ) - .unwrap(); - assert!(matches!( - sparse.checkpoint(CanonicalSourceRequest::PreferWindow), - Err(RawError { - kind: RawErrorKind::WindowUnavailable, - .. - }) - )); - let full = capture(&path, Some("a")); - assert!( - matches!(full.checkpoint(CanonicalSourceRequest::PreferWindow).unwrap().source,CanonicalSource::Full(rows) if rows.len()==2) - ); - let large = format!( - "{HEADER}{{\"type\":\"future\",\"data\":\"{}\"}}\n", - "x".repeat(1024 * 1024) - ); - fs::write(&path, large).unwrap(); - let full = capture(&path, None); - assert!(full.checkpoint(CanonicalSourceRequest::Full).is_ok()); - assert!(matches!( - full.checkpoint(CanonicalSourceRequest::PreferWindow), - Err(RawError { - kind: RawErrorKind::WindowUnavailable, - .. - }) - )); - } - #[test] - fn oversized_physical_row_and_complete_tail_fail_typed() { - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - let c = capture(&path, Some("a")); - File::options() - .write(true) - .open(&path) - .unwrap() - .set_len(c.cursor.frontier.bytes + MAX_TAIL_BYTES + 1) - .unwrap(); - assert_eq!( - RawCapture::append_from(&c, &c.cursor, next(&c, Some("a")), CaptureMode::Full) - .unwrap_err() - .kind, - RawErrorKind::Limit - ); - assert_eq!( - RawCapture::capture( - &path, - position(None), - CaptureMode::Sparse { - recent_rows: 0, - retained_bytes: 0 - } - ) - .unwrap_err() - .kind, - RawErrorKind::Limit - ); - } - #[test] - fn request_helper_never_expands_large_prefer_window_and_keeps_small_exact() { - let (_dir, path) = source(format!("{HEADER}{A}").as_bytes()); - let checkpoint = RawCapture::capture_checkpoint( - &path, - position(Some("a")), - CanonicalSourceRequest::PreferWindow, - ) - .unwrap(); - assert_eq!(checkpoint.header.raw(), HEADER.as_bytes()); - assert!( - matches!(checkpoint.source,CanonicalSource::Full(rows) if rows[0].raw()==A.as_bytes()) - ); - let large = format!( - "{HEADER}{{\"type\":\"future\",\"data\":\"{}\"}}\n", - "x".repeat(1024 * 1024) - ); - fs::write(&path, large).unwrap(); - assert!(matches!( - RawCapture::capture_checkpoint( - &path, - position(None), - CanonicalSourceRequest::PreferWindow - ), - Err(RawError { - kind: RawErrorKind::WindowUnavailable, - .. - }) - )); - } - #[test] - fn no_retained_boundary_preserves_raw_prefix_and_refuses_invalid_claimed_ids() { - let archive = json!({ - "type":"compaction", "id":"c", "parentId":"b", "summary":"archive", - "firstKeptEntryId":null, "tokensBefore":1, - "contextArchive":{ - "version":1, "strategy":"standard", "compactor":"test", - "creatorGeneration":"test", "preparedLeaf":"b", - "coveredEntryIds":["a","b"], "representation":{"kind":"standard"} - }, - "opaque":{"boundary":""} - }); - for omitted in [false, true] { - let mut value = archive.clone(); - if omitted { - value.as_object_mut().unwrap().remove("firstKeptEntryId"); - } - let row = value.to_string() + "\n"; - let bytes = format!("{HEADER}{A}{B}{row}").into_bytes(); - let (_dir, path) = source(&bytes); - let actual = capture(&path, Some("c")); - assert_eq!(actual.plan.rows[2].anchors, vec!["b", "a", "b"]); - let retained = actual - .read_span(&actual.cursor, &actual.plan.rows[2].span) - .unwrap(); - assert_eq!(retained.raw(), row.as_bytes()); - assert!( - matches!(retained.entry(), FileEntry::Compaction { payload, .. } - if payload.first_kept_entry_id.is_none()) - ); - assert_eq!(fs::read(&path).unwrap(), bytes); - } - for boundary in ["", "missing", "c"] { - let mut value = archive.clone(); - value["firstKeptEntryId"] = json!(boundary); - let bytes = format!("{HEADER}{A}{B}{value}\n"); - assert_eq!(reject(bytes.as_bytes()).kind, RawErrorKind::InvalidGraph); - } - for boundary in [json!(1), json!(false), json!({})] { - let mut value = archive.clone(); - value["firstKeptEntryId"] = boundary; - let bytes = format!("{HEADER}{A}{B}{value}\n"); - assert_eq!(reject(bytes.as_bytes()).kind, RawErrorKind::Malformed); - } - } - - #[test] - fn compaction_branch_and_archive_references_are_certified_prior_spans() { - let compaction=json!({"type":"compaction","id":"c","parentId":"a","summary":"s","firstKeptEntryId":"a","tokensBefore":1,"contextArchive":{"version":1,"strategy":"standard","compactor":"test","creatorGeneration":"test","preparedLeaf":"a","coveredEntryIds":["a"],"representation":{"kind":"standard"}}}).to_string()+"\n"; - let summary=json!({"type":"branch_summary","id":"summary","parentId":"c","fromId":"b","summary":"other branch"}).to_string()+"\n"; - let (_dir, path) = source(format!("{HEADER}{A}{B}{compaction}{summary}{META}").as_bytes()); - let c = RawCapture::capture( - &path, - position(Some("summary")), - CaptureMode::Sparse { - recent_rows: 0, - retained_bytes: 0, - }, - ) - .unwrap(); - assert_eq!(c.plan.anchor_rows, vec![2, 3]); - assert_eq!(c.plan.rows[2].anchors, vec!["a", "a", "a"]); - assert_eq!(c.plan.metadata_rows, vec![0, 1, 2, 3, 4]); - assert_eq!( - c.read_span(&c.cursor, &c.plan.rows[2].span).unwrap().raw(), - compaction.as_bytes() - ); - assert_eq!( - reject( - format!( - "{HEADER}{A}{B}{}", - compaction.replace( - "\"coveredEntryIds\":[\"a\"]", - "\"coveredEntryIds\":[\"missing\"]" - ) - ) - .as_bytes() - ) - .kind, - RawErrorKind::InvalidGraph - ); - } -} diff --git a/crates/pa-daemon/src/session_store/canonical_revision.rs b/crates/pa-daemon/src/session_store/canonical_revision.rs deleted file mode 100644 index fb17a3950..000000000 --- a/crates/pa-daemon/src/session_store/canonical_revision.rs +++ /dev/null @@ -1,765 +0,0 @@ -//! Physical-owner revision and uncertainty tracking. No file I/O or authority callbacks. -//! -//! Attach one clone-shared tracker to each actual `SessionFile` owner epoch. The adapter -//! must verify its weak lease, header and path before exposing owner certificates. -//! Move the owned attempt into the actual blocking writer, not its awaiting task. - -#![allow(dead_code)] // Temporary; removed when the writer seam consumes this API. - -use std::sync::{Arc, Mutex, MutexGuard}; - -use pa_core::session::canonical::{CanonicalOperation, CanonicalOwnerScope}; -use tokio::sync::Notify; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum RevisionError { - Poisoned, - Invalidated, - Busy, - BarrierMismatch, - BarrierAlreadySet, - NoBarrier, - StaleBarrier, - Overflow, - InvalidAttempt, - MutexPoisoned, -} - -type Result = std::result::Result; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct RevisionSnapshot { - pub owner: CanonicalOwnerScope, - pub canonical_revision: u64, - pub branch_revision: u64, -} - -#[derive(Debug)] -struct State { - canonical_revision: u64, - branch_revision: u64, - poisoned: bool, - invalidated: bool, - active_attempts: usize, - active_io: usize, - barrier: Option, -} - -impl State { - fn available(&self) -> Result<()> { - if self.poisoned { - Err(RevisionError::Poisoned) - } else if self.invalidated { - Err(RevisionError::Invalidated) - } else { - Ok(()) - } - } - - fn permits(&self, operation: Option<&CanonicalOperation>) -> Result<()> { - self.available()?; - if self - .barrier - .as_ref() - .is_some_and(|expected| Some(&expected.operation) != operation) - { - return Err(RevisionError::BarrierMismatch); - } - Ok(()) - } -} - -#[derive(Debug)] -struct Shared { - owner: CanonicalOwnerScope, - state: Mutex, - changed: Notify, -} - -impl Shared { - // Never recover a poisoned mutex as usable state. Even cleanup leaves it poisoned. - fn lock(&self) -> Result> { - self.state.lock().map_err(|_| RevisionError::MutexPoisoned) - } - - fn snapshot(&self, state: &State) -> RevisionSnapshot { - RevisionSnapshot { - owner: self.owner.clone(), - canonical_revision: state.canonical_revision, - branch_revision: state.branch_revision, - } - } -} - -/// Opaque incarnation of one operation-bound physical barrier. Keeping this -/// ticket alone does not arm a write or retain actor authority. -#[derive(Debug, Clone)] -pub(crate) struct BarrierTicket { - operation: CanonicalOperation, - instance: Arc<()>, -} - -impl PartialEq for BarrierTicket { - fn eq(&self, other: &Self) -> bool { - self.operation == other.operation && Arc::ptr_eq(&self.instance, &other.instance) - } -} -impl Eq for BarrierTicket {} - -/// Clone-shared state for exactly one actual owner lifetime. No reset-poison API. -#[derive(Debug, Clone)] -pub(crate) struct CanonicalRevision { - shared: Arc, -} - -impl CanonicalRevision { - /// Only create for a new actual owner scope, never for a cache reload. - pub(crate) fn new() -> Self { - Self { - shared: Arc::new(Shared { - owner: CanonicalOwnerScope::new(), - state: Mutex::new(State { - canonical_revision: 0, - branch_revision: 0, - poisoned: false, - invalidated: false, - active_attempts: 0, - active_io: 0, - barrier: None, - }), - changed: Notify::new(), - }), - } - } - - /// Refuse certification while a physical attempt is owned, including before I/O. - pub(crate) fn snapshot(&self) -> Result { - let state = self.shared.lock()?; - state.available()?; - if state.active_attempts != 0 { - return Err(RevisionError::Busy); - } - Ok(self.shared.snapshot(&state)) - } - - /// Short admission only. The caller must move the guard into the physical writer. - /// Serial admission reserves exactly one checked revision step before any I/O. - pub(crate) fn admit_write( - &self, - operation: Option<&CanonicalOperation>, - ) -> Result { - let mut state = self.shared.lock()?; - state.permits(operation)?; - if state.active_attempts != 0 { - return Err(RevisionError::Busy); - } - state - .canonical_revision - .checked_add(1) - .ok_or(RevisionError::Overflow)?; - state.active_attempts = 1; - Ok(OwnedWriteAttempt { - shared: self.shared.clone(), - operation: operation.cloned(), - started: false, - finished: false, - }) - } - - /// May close admission while an existing flight drains. It cannot undo I/O - /// already started. Parent authority gates must prevent new matching flights. - pub(crate) fn set_barrier(&self, operation: &CanonicalOperation) -> Result { - let mut state = self.shared.lock()?; - state.available()?; - if state.barrier.is_some() { - return Err(RevisionError::BarrierAlreadySet); - } - let ticket = BarrierTicket { - operation: operation.clone(), - instance: Arc::new(()), - }; - state.barrier = Some(ticket.clone()); - Ok(ticket) - } - - /// Release only after matching `PreparedACK`. This does not disarm the actor's - /// unique caller guard and cannot clear physical uncertainty or invalidation. - /// A fresh ticket distinguishes Prepare and Commit barriers for the same op. - pub(crate) fn clear_barrier( - &self, - operation: &CanonicalOperation, - ticket: &BarrierTicket, - ) -> Result<()> { - let mut state = self.shared.lock()?; - state.available()?; - let expected = state.barrier.as_ref().ok_or(RevisionError::NoBarrier)?; - if &expected.operation != operation || &ticket.operation != operation { - return Err(RevisionError::BarrierMismatch); - } - if !Arc::ptr_eq(&expected.instance, &ticket.instance) { - return Err(RevisionError::StaleBarrier); - } - if state.active_attempts != 0 { - return Err(RevisionError::Busy); - } - state.barrier = None; - Ok(()) - } - - /// Preflight a ledger transaction before arming any pending receipt. - pub(crate) fn check_owner_state_advance(&self) -> Result<()> { - let state = self.shared.lock()?; - state.permits(None)?; - if state.active_attempts != 0 || state.active_io != 0 { - return Err(RevisionError::Busy); - } - state - .canonical_revision - .checked_add(1) - .ok_or(RevisionError::Overflow)?; - Ok(()) - } - - /// Advance an accepted owner ledger transaction, without pretending to do I/O. - /// Check every refusal before the caller changes its owner state under its lock. - pub(crate) fn advance_owner_state( - &self, - operation: Option<&CanonicalOperation>, - ) -> Result { - let mut state = self.shared.lock()?; - state.permits(operation)?; - if state.active_attempts != 0 || state.active_io != 0 { - return Err(RevisionError::Busy); - } - let next = state - .canonical_revision - .checked_add(1) - .ok_or(RevisionError::Overflow)?; - state.canonical_revision = next; - Ok(self.shared.snapshot(&state)) - } - - /// Explicit navigation/branch ABA changes both revisions, even without bytes. - /// No navigation is allowed through a pending physical barrier. - pub(crate) fn advance_branch(&self) -> Result { - let mut state = self.shared.lock()?; - state.available()?; - if state.barrier.is_some() { - return Err(RevisionError::BarrierMismatch); - } - if state.active_attempts != 0 { - return Err(RevisionError::Busy); - } - let canonical = state - .canonical_revision - .checked_add(1) - .ok_or(RevisionError::Overflow)?; - let branch = state - .branch_revision - .checked_add(1) - .ok_or(RevisionError::Overflow)?; - state.canonical_revision = canonical; - state.branch_revision = branch; - Ok(self.shared.snapshot(&state)) - } - - /// Header, path, store replacement or lease loss permanently closes this scope. - pub(crate) fn invalidate(&self) -> Result<()> { - let result = self.shared.lock().map(|mut state| state.invalidated = true); - self.shared.changed.notify_waiters(); - result - } - - pub(crate) fn active_io(&self) -> Result { - Ok(self.shared.lock()?.active_io) - } - - /// Wait outside all state locks. Cancellation does not release an owned flight. - /// This is a drain observation, NOT an authority gate against successor writes. - pub(crate) async fn drain(&self) -> Result { - loop { - let notified = self.shared.changed.notified(); - tokio::pin!(notified); - notified.as_mut().enable(); - { - let state = self.shared.lock()?; - if state.active_attempts == 0 { - state.available()?; - return Ok(self.shared.snapshot(&state)); - } - } - notified.await; - } - } -} - -/// Owned physical attempt, deliberately not `Clone`. Drop before `begin_io` is a -/// known no-I/O cancellation; drop/panic/error after `begin_io` is permanent poison. -#[derive(Debug)] -pub(crate) struct OwnedWriteAttempt { - shared: Arc, - operation: Option, - started: bool, - finished: bool, -} - -impl OwnedWriteAttempt { - /// Call immediately BEFORE the first potentially mutating syscall. No locks - /// remain held when this returns. From here cancellation is unknown, not no-I/O. - pub(crate) fn begin_io(&mut self) -> Result<()> { - if self.started || self.finished { - return Err(RevisionError::InvalidAttempt); - } - let mut state = self.shared.lock()?; - state.permits(self.operation.as_ref())?; - state - .canonical_revision - .checked_add(1) - .ok_or(RevisionError::Overflow)?; - if state.active_attempts != 1 || state.active_io != 0 { - state.poisoned = true; - return Err(RevisionError::Poisoned); - } - state.active_io = 1; - self.started = true; - Ok(()) - } - - /// Call exactly once only when all physical writes belonging to this attempt - /// succeeded. Each physical attempt advances canonical revision once. - pub(crate) fn finish_success(mut self) -> Result { - if !self.started { - return Err(RevisionError::InvalidAttempt); - } - let shared = self.shared.clone(); - let mut state = shared.lock()?; - let result = if let Some(next) = state.canonical_revision.checked_add(1) { - state.canonical_revision = next; - state.available().map(|()| shared.snapshot(&state)) - } else { - state.poisoned = true; - Err(RevisionError::Poisoned) - }; - state.active_io = 0; - state.active_attempts = 0; - self.finished = true; - drop(state); - shared.changed.notify_waiters(); - result - } - - /// Explicit physical error; dropping after `begin_io` has the same semantics. - pub(crate) fn finish_error(self) { - drop(self); - } -} - -impl Drop for OwnedWriteAttempt { - fn drop(&mut self) { - if self.finished { - return; - } - // On mutex poison cleanup may release counters, but NEVER rehabilitates - // the mutex or uncertainty. lock() continues failing closed forever. - let mut state = match self.shared.state.lock() { - Ok(state) => state, - Err(poisoned) => poisoned.into_inner(), - }; - if self.started { - state.poisoned = true; - state.active_io = 0; - } - state.active_attempts = 0; - drop(state); - self.shared.changed.notify_waiters(); - } -} - -#[cfg(test)] -mod tests { - use super::*; - use std::sync::mpsc; - - #[test] - fn ledger_advance_has_no_io_and_preserves_epoch_scope() { - let tracker = CanonicalRevision::new(); - let initial = tracker.snapshot().unwrap(); - for revision in 1..=4 { - tracker.check_owner_state_advance().unwrap(); - let next = tracker.advance_owner_state(None).unwrap(); - assert_eq!(next.owner, initial.owner); - assert_eq!(next.branch_revision, initial.branch_revision); - assert_eq!(next.canonical_revision, revision); - assert_eq!(tracker.active_io(), Ok(0)); - } - let branch = tracker.advance_branch().unwrap(); - assert_eq!(branch.canonical_revision, 5); - assert_eq!(branch.branch_revision, 1); - let mut write = tracker.admit_write(None).unwrap(); - write.begin_io().unwrap(); - let written = write.finish_success().unwrap(); - assert_eq!(written.canonical_revision, 6); - assert_eq!(written.branch_revision, 1); - } - - #[test] - fn ledger_advance_refuses_active_io_barrier_overflow_and_poison_before_effects() { - let tracker = CanonicalRevision::new(); - let initial = tracker.snapshot().unwrap(); - let operation = CanonicalOperation::new(); - let ticket = tracker.set_barrier(&operation).unwrap(); - assert_eq!( - tracker.advance_owner_state(None), - Err(RevisionError::BarrierMismatch) - ); - assert_eq!(tracker.snapshot(), Ok(initial)); - tracker.clear_barrier(&operation, &ticket).unwrap(); - let mut attempt = tracker.admit_write(None).unwrap(); - assert_eq!(tracker.advance_owner_state(None), Err(RevisionError::Busy)); - attempt.begin_io().unwrap(); - assert_eq!(tracker.advance_owner_state(None), Err(RevisionError::Busy)); - drop(attempt); - assert_eq!( - tracker.advance_owner_state(None), - Err(RevisionError::Poisoned) - ); - let tracker = CanonicalRevision::new(); - tracker.shared.lock().unwrap().canonical_revision = u64::MAX; - let before = tracker.snapshot().unwrap(); - assert_eq!( - tracker.check_owner_state_advance(), - Err(RevisionError::Overflow) - ); - assert_eq!( - tracker.advance_owner_state(None), - Err(RevisionError::Overflow) - ); - assert_eq!(tracker.snapshot(), Ok(before)); - } - - #[test] - fn successful_write_advances_exactly_once_without_branch_change() { - let tracker = CanonicalRevision::new(); - let original = tracker.snapshot().unwrap(); - let mut attempt = tracker.admit_write(None).unwrap(); - assert_eq!(tracker.active_io(), Ok(0)); - assert_eq!(tracker.snapshot(), Err(RevisionError::Busy)); - attempt.begin_io().unwrap(); - assert_eq!(tracker.active_io(), Ok(1)); - let done = attempt.finish_success().unwrap(); - assert_eq!(done.owner, original.owner); - assert_eq!(done.canonical_revision, 1); - assert_eq!(done.branch_revision, 0); - assert_eq!(tracker.snapshot(), Ok(done)); - assert_eq!(tracker.active_io(), Ok(0)); - } - - #[test] - fn known_no_io_drop_does_not_advance_or_poison() { - let tracker = CanonicalRevision::new(); - let before = tracker.snapshot().unwrap(); - drop(tracker.admit_write(None).unwrap()); - assert_eq!(tracker.snapshot(), Ok(before)); - assert!(tracker.admit_write(None).is_ok()); - } - - #[test] - fn success_before_io_is_rejected_and_cleans_up_without_poison() { - let tracker = CanonicalRevision::new(); - assert_eq!( - tracker.admit_write(None).unwrap().finish_success(), - Err(RevisionError::InvalidAttempt) - ); - assert_eq!(tracker.snapshot().unwrap().canonical_revision, 0); - } - - #[test] - fn error_and_drop_after_start_permanently_poison() { - for explicit_error in [false, true] { - let tracker = CanonicalRevision::new(); - let op = CanonicalOperation::new(); - let op_ticket = tracker.set_barrier(&op).unwrap(); - let mut attempt = tracker.admit_write(Some(&op)).unwrap(); - attempt.begin_io().unwrap(); - if explicit_error { - attempt.finish_error(); - } else { - drop(attempt); - } - assert_eq!(tracker.snapshot(), Err(RevisionError::Poisoned)); - assert_eq!( - tracker.clear_barrier(&op, &op_ticket), - Err(RevisionError::Poisoned) - ); - assert_eq!(tracker.advance_branch(), Err(RevisionError::Poisoned)); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::Poisoned - ); - assert_eq!(tracker.active_io(), Ok(0)); - } - } - - #[test] - fn panic_in_actual_writer_poisons_without_poisoning_state_mutex() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - let thread = std::thread::spawn(move || { - attempt.begin_io().unwrap(); - panic!("actual writer panicked"); - }); - assert!(thread.join().is_err()); - assert_eq!(tracker.snapshot(), Err(RevisionError::Poisoned)); - } - - #[test] - fn opaque_owner_epoch_is_not_cache_revision_identity() { - let first = CanonicalRevision::new(); - let second = CanonicalRevision::new(); - let first_snapshot = first.snapshot().unwrap(); - assert_eq!(first_snapshot, first.snapshot().unwrap()); - assert_ne!(first_snapshot.owner, second.snapshot().unwrap().owner); - } - - #[test] - fn barrier_rejects_wrong_none_and_late_tokens() { - let tracker = CanonicalRevision::new(); - let op = CanonicalOperation::new(); - let wrong = CanonicalOperation::new(); - let op_ticket = tracker.set_barrier(&op).unwrap(); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::BarrierMismatch - ); - assert_eq!( - tracker.admit_write(Some(&wrong)).unwrap_err(), - RevisionError::BarrierMismatch - ); - assert_eq!( - tracker.clear_barrier(&wrong, &op_ticket), - Err(RevisionError::BarrierMismatch) - ); - assert_eq!( - tracker.advance_branch(), - Err(RevisionError::BarrierMismatch) - ); - assert_eq!( - tracker.set_barrier(&wrong), - Err(RevisionError::BarrierAlreadySet) - ); - let mut attempt = tracker.admit_write(Some(&op)).unwrap(); - attempt.begin_io().unwrap(); - assert_eq!( - tracker.clear_barrier(&op, &op_ticket), - Err(RevisionError::Busy) - ); - attempt.finish_success().unwrap(); - tracker.clear_barrier(&op, &op_ticket).unwrap(); - assert_eq!( - tracker.clear_barrier(&op, &op_ticket), - Err(RevisionError::NoBarrier) - ); - let second_ticket = tracker.set_barrier(&op).unwrap(); - assert_eq!( - tracker.clear_barrier(&op, &op_ticket), - Err(RevisionError::StaleBarrier) - ); - tracker.clear_barrier(&op, &second_ticket).unwrap(); - drop(tracker.admit_write(Some(&op)).unwrap()); - let wrong_ticket = tracker.set_barrier(&wrong).unwrap(); - assert_eq!( - tracker.clear_barrier(&op, &op_ticket), - Err(RevisionError::BarrierMismatch) - ); - tracker.clear_barrier(&wrong, &wrong_ticket).unwrap(); - assert!(tracker.admit_write(None).is_ok()); - } - - #[test] - fn barrier_inserted_after_admission_is_rechecked_before_io() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - let op = CanonicalOperation::new(); - let op_ticket = tracker.set_barrier(&op).unwrap(); - assert_eq!(attempt.begin_io(), Err(RevisionError::BarrierMismatch)); - drop(attempt); - assert_eq!(tracker.snapshot().unwrap().canonical_revision, 0); - tracker.clear_barrier(&op, &op_ticket).unwrap(); - } - - #[test] - fn branch_aba_bumps_both_revisions_without_bytes() { - let tracker = CanonicalRevision::new(); - let a = tracker.advance_branch().unwrap(); - let b = tracker.advance_branch().unwrap(); - assert_eq!((a.canonical_revision, a.branch_revision), (1, 1)); - assert_eq!((b.canonical_revision, b.branch_revision), (2, 2)); - } - - #[test] - fn checked_overflow_refuses_before_io_and_navigation_is_atomic() { - let tracker = CanonicalRevision::new(); - tracker.shared.state.lock().unwrap().canonical_revision = u64::MAX; - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::Overflow - ); - assert_eq!(tracker.active_io(), Ok(0)); - assert_eq!(tracker.advance_branch(), Err(RevisionError::Overflow)); - let mut state = tracker.shared.state.lock().unwrap(); - state.canonical_revision = 12; - state.branch_revision = u64::MAX; - drop(state); - assert_eq!(tracker.advance_branch(), Err(RevisionError::Overflow)); - assert_eq!(tracker.snapshot().unwrap().canonical_revision, 12); - } - - #[test] - fn final_available_revision_is_reserved_and_used_once() { - let tracker = CanonicalRevision::new(); - tracker.shared.state.lock().unwrap().canonical_revision = u64::MAX - 1; - let mut attempt = tracker.admit_write(None).unwrap(); - assert_eq!(tracker.admit_write(None).unwrap_err(), RevisionError::Busy); - assert_eq!(tracker.advance_branch(), Err(RevisionError::Busy)); - attempt.begin_io().unwrap(); - assert_eq!( - attempt.finish_success().unwrap().canonical_revision, - u64::MAX - ); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::Overflow - ); - } - - #[test] - fn impossible_post_io_overflow_poisons_fail_closed() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - attempt.begin_io().unwrap(); - // Only a test can violate the reserved-step invariant. - tracker.shared.state.lock().unwrap().canonical_revision = u64::MAX; - assert_eq!(attempt.finish_success(), Err(RevisionError::Poisoned)); - assert_eq!(tracker.snapshot(), Err(RevisionError::Poisoned)); - } - - #[test] - fn invalidation_closes_old_scope_even_during_physical_flight() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - attempt.begin_io().unwrap(); - tracker.invalidate().unwrap(); - assert_eq!(attempt.finish_success(), Err(RevisionError::Invalidated)); - assert_eq!(tracker.shared.state.lock().unwrap().canonical_revision, 1); - assert_eq!(tracker.snapshot(), Err(RevisionError::Invalidated)); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::Invalidated - ); - assert_eq!(tracker.advance_branch(), Err(RevisionError::Invalidated)); - } - - #[test] - fn poisoned_std_mutex_is_never_recovered_as_success() { - let tracker = CanonicalRevision::new(); - let other = tracker.clone(); - assert!(std::thread::spawn(move || { - let _state = other.shared.state.lock().unwrap(); - panic!("state invariant panic"); - }) - .join() - .is_err()); - assert_eq!(tracker.snapshot(), Err(RevisionError::MutexPoisoned)); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::MutexPoisoned - ); - assert_eq!(tracker.invalidate(), Err(RevisionError::MutexPoisoned)); - } - - #[test] - fn poisoned_mutex_cleanup_never_clears_poison() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - attempt.begin_io().unwrap(); - let other = tracker.clone(); - assert!(std::thread::spawn(move || { - let _state = other.shared.state.lock().unwrap(); - panic!("state invariant panic"); - }) - .join() - .is_err()); - drop(attempt); - assert_eq!(tracker.snapshot(), Err(RevisionError::MutexPoisoned)); - assert!(tracker.shared.state.is_poisoned()); - } - - #[tokio::test] - async fn caller_loss_keeps_real_writer_owned_and_barrier_blocks_successor() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - let (started_tx, started_rx) = tokio::sync::oneshot::channel(); - let (release_tx, release_rx) = mpsc::channel(); - let waiter = tokio::task::spawn_blocking(move || { - attempt.begin_io().unwrap(); - started_tx.send(()).unwrap(); - release_rx.recv().unwrap(); - attempt.finish_success() - }); - started_rx.await.unwrap(); - // Dropping the async caller's JoinHandle does not stop the blocking writer. - drop(waiter); - let op = CanonicalOperation::new(); - let op_ticket = tracker.set_barrier(&op).unwrap(); - assert_eq!(tracker.active_io(), Ok(1)); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::BarrierMismatch - ); - let drain = tracker.drain(); - tokio::pin!(drain); - assert!( - tokio::time::timeout(std::time::Duration::from_millis(10), drain.as_mut()) - .await - .is_err() - ); - release_tx.send(()).unwrap(); - let done = tokio::time::timeout(std::time::Duration::from_secs(2), drain) - .await - .unwrap() - .unwrap(); - assert_eq!(done.canonical_revision, 1); - assert_eq!(tracker.active_io(), Ok(0)); - assert_eq!( - tracker.admit_write(None).unwrap_err(), - RevisionError::BarrierMismatch - ); - tracker.clear_barrier(&op, &op_ticket).unwrap(); - } - - #[tokio::test] - async fn held_unknown_flight_drains_to_failure_never_success() { - let tracker = CanonicalRevision::new(); - let mut attempt = tracker.admit_write(None).unwrap(); - attempt.begin_io().unwrap(); - let op = CanonicalOperation::new(); - let op_ticket = tracker.set_barrier(&op).unwrap(); - let drain = tracker.drain(); - tokio::pin!(drain); - assert!( - tokio::time::timeout(std::time::Duration::from_millis(10), drain.as_mut()) - .await - .is_err() - ); - drop(attempt); - assert_eq!(drain.await, Err(RevisionError::Poisoned)); - assert_eq!( - tracker.clear_barrier(&op, &op_ticket), - Err(RevisionError::Poisoned) - ); - assert_eq!( - tracker.admit_write(Some(&op)).unwrap_err(), - RevisionError::Poisoned - ); - } -} diff --git a/crates/pa-daemon/src/session_transport.rs b/crates/pa-daemon/src/session_transport.rs index db51e8b4a..4d9a63aaf 100644 --- a/crates/pa-daemon/src/session_transport.rs +++ b/crates/pa-daemon/src/session_transport.rs @@ -1,17 +1,14 @@ -//! THE transport switch for the session-log wiring (`SESSIONS-LOG-SPEC.md`, -//! port-plan §3): the one point that decides whether a session is served by -//! the old canonical paths ([`SessionTransport::Canonical`], compiled until -//! the final deletion step) or by the session-log core -//! ([`SessionTransport::SessionLog`]). +//! THE transport switch for the session-log cutover (`SESSIONS-LOG-SPEC.md`): +//! the one point that decides whether a session rides the session-log core +//! ([`SessionTransport::SessionLog`]) or is refused as a pre-port old-format +//! file ([`SessionTransport::LegacyUnsupported`]). //! //! Resolution rule ([`resolve`]): a session that already has a log file is a -//! log session; an old-format session file stays canonical (the log core -//! cannot read that format, and the spec reports old files instead of -//! converting them); a fresh session takes the log only when the override -//! environment asks for it, so the default behavior of every existing flow is -//! unchanged. The override selects between the two transports until the -//! cutover flips the default; the final deletion step removes the canonical -//! half together with this switch. +//! log session; a fresh session starts one; an old-format session file is +//! kept on disk but never read and never migrated (the spec: report old +//! files instead of converting them), so the caller surfaces the typed +//! refusal. There is no transport override: every served session uses the +//! log. //! //! Row policy at this boundary (the spec: policy lives in callers; the core //! never waits, retries, or inspects a peer): an append resolves exactly @@ -34,22 +31,18 @@ use serde_json::Value; use pa_core::session_log::{self, Outcome, SessionLog}; -/// The override that serves fresh sessions on the log transport. Absent, or -/// any other value, keeps the canonical default. -pub(crate) const TRANSPORT_OVERRIDE_ENV: &str = "PA_SESSION_TRANSPORT"; -pub(crate) const TRANSPORT_OVERRIDE_VALUE: &str = "session-log"; - /// One open/repair's budget at this transport. The daemon's own append paths /// run under the session's admission deadlines; the blocking open/repair here /// takes this bound so a wedged storage device reports instead of stalling a /// create. The core never waits; this is the caller's one clock. const LOG_OPEN_DEADLINE: Duration = Duration::from_secs(10); -/// Which transport serves a session. +/// Which transport opens a session. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum SessionTransport { - /// The pre-port canonical paths (the compiled default until cutover). - Canonical, + /// A pre-port old-format session file: kept on disk, never read and + /// never migrated. The caller surfaces this as the typed refusal. + LegacyUnsupported, /// The session-log core: the log is the source of truth, the daemon /// store is its fold. SessionLog, @@ -82,32 +75,24 @@ pub(crate) fn deadline() -> Instant { /// advertised identity). pub(crate) fn resolve(agent_dir: &Path, session_path: &Path) -> SessionTransport { let Some(session_id) = session_id_of(session_path) else { - return SessionTransport::Canonical; + return SessionTransport::LegacyUnsupported; }; if log_file(agent_dir, &session_id).exists() { return SessionTransport::SessionLog; } - // An existing old-format session keeps serving through the canonical path. + // A pre-port old-format file is not read and not migrated: the caller + // surfaces the verdict as the typed refusal. if session_path.exists() { - return SessionTransport::Canonical; - } - if new_transport_requested() { - return SessionTransport::SessionLog; + return SessionTransport::LegacyUnsupported; } - SessionTransport::Canonical -} - -/// Whether the switch maps this session to the log. -pub(crate) fn transports_session_log(agent_dir: &Path, session_path: &Path) -> bool { - resolve(agent_dir, session_path) == SessionTransport::SessionLog + SessionTransport::SessionLog } -/// The override's request, read once per resolve. -pub(crate) fn new_transport_requested() -> bool { - match std::env::var(TRANSPORT_OVERRIDE_ENV) { - Ok(override_value) => override_value == TRANSPORT_OVERRIDE_VALUE, - Err(_) => false, - } +/// Does this session's log file exist yet? The create arms' reopen decision: +/// an existing log reopens, the absence (with no old-format file either) +/// means the session is fresh. +pub(crate) fn log_exists(agent_dir: &Path, session_path: &Path) -> bool { + session_id_of(session_path).is_some_and(|id| log_file(agent_dir, &id).exists()) } /// Why one wire step failed. @@ -324,6 +309,56 @@ pub(crate) fn open_error(error: session_log::Error) -> LogWireError { } } +/// The fixture-facing log read: every row's body except the header's. +/// +/// The seeding side must DROP its `SessionLog` before a serving open: the +/// log's flock is the writer's mutual exclusion, so a seeded file left open +/// refuses the worker. +#[cfg(test)] +pub(crate) mod test_support { + use super::*; + + /// A session's log row bodies (without the header row), read back + /// through the core. + #[must_use] + pub(crate) fn log_row_bodies(agent_dir: &Path, session_id: &str) -> Vec { + let log = + SessionLog::open(&log_dir(agent_dir), session_id, deadline()).expect("open the log"); + log.read_since(0) + .expect("read the log") + .into_iter() + .skip(1) + .map(|row| row.body) + .collect() + } + + /// Seed a session's log from fixture text: one row per non-blank line. + /// A line that parses as JSON lands as its value (the fold replays it as + /// an entry, or skips a malformed one, the way the old loader did); a + /// line that does not parse lands as a string row the fold skips. + pub(crate) fn seed_log_from_text(agent_dir: &Path, session_id: &str, content: &str) { + std::fs::create_dir_all(log_dir(agent_dir)).expect("log dir"); + let mut log = SessionLog::open(&log_dir(agent_dir), session_id, deadline()) + .expect("open the log for seeding"); + for line in content.lines().filter(|line| !line.trim().is_empty()) { + let row = serde_json::from_str::(line) + .unwrap_or(Value::String(line.to_owned())); + let outcome = log.append(&row, deadline()); + assert!( + matches!(outcome, Outcome::Committed { .. }), + "seeding refused a row: {outcome:?}" + ); + } + } + + /// Whether a serving open of the log is refused (a worker holds it). + #[must_use] + pub(crate) fn log_open_refused(agent_dir: &Path, session_id: &str) -> bool { + std::fs::create_dir_all(log_dir(agent_dir)).expect("log dir"); + SessionLog::open(&log_dir(agent_dir), session_id, deadline()).is_err() + } +} + #[cfg(test)] mod tests { use super::*; @@ -341,23 +376,6 @@ mod tests { dir } - /// The env tests mutate process state; they serialize on this lock. - static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - - fn with_override(run: impl FnOnce() -> T) -> T { - let _guard = ENV_LOCK - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let saved = std::env::var(TRANSPORT_OVERRIDE_ENV).ok(); - std::env::set_var(TRANSPORT_OVERRIDE_ENV, TRANSPORT_OVERRIDE_VALUE); - let result = run(); - match saved { - Some(saved) => std::env::set_var(TRANSPORT_OVERRIDE_ENV, saved), - None => std::env::remove_var(TRANSPORT_OVERRIDE_ENV), - } - result - } - fn fresh_log_session(dir: &std::path::Path, name: &str) -> (SessionFile, std::path::PathBuf) { let session_path = dir.join(format!("{name}.jsonl")); let mut store = SessionFile::create("test", None, 0); @@ -368,20 +386,21 @@ mod tests { } #[test] - fn the_switch_serves_a_log_session_and_refuses_to_map_an_old_file() { + fn the_switch_serves_every_fresh_session_on_the_log_and_refuses_legacy_files() { let dir = temp_dir("switch"); - // A fresh session with no files and no override: the canonical default. + // A fresh session with no files: the log transport, no override. let path = dir.join("fresh.jsonl"); - assert!(!transports_session_log(&dir, &path)); + assert_eq!(resolve(&dir, &path), SessionTransport::SessionLog); - // An old-format session file: the log core cannot read that format, - // so it keeps serving canonically, override or not. + // An old-format session file: not read and not migrated, typed. std::fs::write(&path, "[]\n").expect("old file"); - assert!(!transports_session_log(&dir, &path)); - assert_eq!(with_override(|| transports_session_log(&dir, &path)), false); + assert_eq!( + resolve(&dir, &path), + SessionTransport::LegacyUnsupported + ); std::fs::remove_file(&path).expect("clean"); - // An existing log: the log transport, override or not. + // An existing log: the log transport. let log_session = dir.join("logged.jsonl"); let mut store = SessionFile::create("test", None, 0); store.set_path(log_session.clone()); @@ -389,11 +408,7 @@ mod tests { store.rekey_log_session_id(&log_session).expect("re-key"); store.rewrite().expect("flush"); assert!(log_file(&dir, "logged").exists()); - assert_eq!( - with_override(|| transports_session_log(&dir, &log_session)), - true - ); - assert!(transports_session_log(&dir, &log_session)); + assert_eq!(resolve(&dir, &log_session), SessionTransport::SessionLog); } #[test] @@ -536,9 +551,9 @@ mod tests { let sessions_dir = dir.join("sessions"); std::fs::create_dir_all(&sessions_dir).expect("sessions dir"); let session_path = sessions_dir.join("log-e2e.jsonl"); - // The log session pre-exists its worker: the create below resolves to - // the log transport through the log itself (the resolve() test covers - // the override's fresh path), so the serving run exercises the reopen. + // The log session pre-exists its worker: the create below resolves + // to the log transport through the log itself, so the serving run + // exercises the reopen. { let (store, _) = fresh_log_session(&dir, "log-e2e"); store diff --git a/crates/pa-daemon/src/worker/create.rs b/crates/pa-daemon/src/worker/create.rs index d828c2aa5..b4c1544c4 100644 --- a/crates/pa-daemon/src/worker/create.rs +++ b/crates/pa-daemon/src/worker/create.rs @@ -2,8 +2,7 @@ //! construction of the live session. use super::{ json, paths, response_failure, response_success, restore_queue_snapshot, session_file_name, - Arc, EngineModelSelection, Result, RlmSessionIdentity, SessionEngine, SessionFile, VecDeque, - Worker, + EngineModelSelection, Result, RlmSessionIdentity, SessionEngine, SessionFile, VecDeque, Worker, }; use serde::Deserialize as _; @@ -206,39 +205,32 @@ impl Worker { // even without an old-format file: the log IS the session. (Some(path), false) if path.exists() - || crate::session_transport::transports_session_log( - &self.config.agent_dir, - path, - ) => + || crate::session_transport::log_exists(&self.config.agent_dir, path) => { - // THE transport switch's reopen arm: a log-transported - // session loads from its log; the log's flock is the writer's - // mutual exclusion, so the lease file is not taken. - let loaded = if crate::session_transport::transports_session_log( - &self.config.agent_dir, - path, - ) { - let path = path.clone(); - let agent_dir = self.config.agent_dir.clone(); - tokio::task::spawn_blocking(move || SessionFile::open_log(&path, &agent_dir)) - .await - .map_err(anyhow::Error::from) - .and_then(|result| result) - } else { - let path = path.clone(); - let agent_dir = self.config.agent_dir.clone(); - tokio::task::spawn_blocking(move || { - let lease = crate::lease::acquire_runtime_session_lease(&path, &agent_dir)?; - let path = lease.session_path.clone(); - pa_core::session::manager::repair_jsonl_damage(&path); - let mut store = SessionFile::open_windowed(&path)?; - store.lease = Some(Arc::new(lease)); - Ok(store) - }) - .await - .map_err(anyhow::Error::from) - .and_then(|result| result) - }; + // The reopen arm: a session's log is the source of truth (the + // log's flock is the writer's mutual exclusion, so the lease + // file is not taken); a pre-port old-format file is refused + // typed, never read and never migrated. + let loaded = + match crate::session_transport::resolve(&self.config.agent_dir, path) { + crate::session_transport::SessionTransport::SessionLog => { + let path = path.clone(); + let agent_dir = self.config.agent_dir.clone(); + tokio::task::spawn_blocking(move || { + SessionFile::open_log(&path, &agent_dir) + }) + .await + .map_err(anyhow::Error::from) + .and_then(|result| result) + } + crate::session_transport::SessionTransport::LegacyUnsupported => { + Err(anyhow::anyhow!( + "legacy session unsupported: the old-format session file {} is \ + not read and not migrated", + path.display() + )) + } + }; match loaded { Ok(mut opened) => { opened_existing_session = true; @@ -304,18 +296,8 @@ impl Worker { if let Err(error) = persisted { return response_failure(None, "create", &error.to_string(), None); } - // Prime the usage fold on the file's final identity, - // off the runtime and before the core lock: - // summaries under the lock fold only the appended - // tail. The log transport's info-sidecars are its own - // concern; skip the old file's priming read. - if !opened.is_log_served() { - let primed = path.clone(); - let _ = tokio::task::spawn_blocking(move || { - crate::session_store::read_session_info(&primed) - }) - .await; - } + // No old-file info priming: the session's info + // comes from its log, like every other read. opened } Err(error) => return crate::hold_refusal::create_failure_response(&error), @@ -328,32 +310,16 @@ impl Worker { rlm_depth.unwrap_or(0), ); created.set_path(path.clone()); - if crate::session_transport::transports_session_log(&self.config.agent_dir, path) { - // THE transport switch's fresh path-backed arm: the log's - // flock stands in for the lease file. The deferred wire's - // first flush (the rewrite below) creates the log and - // writes the header row, re-keyed to the path's id. - created.attach_log_wire(crate::session_transport::LogWire::deferred( - &self.config.agent_dir, - )); - if let Err(error) = created.rekey_log_session_id(path) { - return response_failure(None, "create", &error.to_string(), None); - } - } else { - let acquired = { - let path = path.clone(); - let agent_dir = self.config.agent_dir.clone(); - tokio::task::spawn_blocking(move || { - crate::lease::acquire_runtime_session_lease(&path, &agent_dir) - }) - .await - .map_err(anyhow::Error::from) - .and_then(|lease| lease) - }; - match acquired { - Ok(lease) => created.lease = Some(Arc::new(lease)), - Err(error) => return crate::hold_refusal::create_failure_response(&error), - } + // The fresh path-backed arm: every fresh session rides the + // session log (the log's flock stands in for the lease + // file). The deferred wire's first flush (the rewrite below) + // creates the log and writes the header row, re-keyed to the + // path's id. + created.attach_log_wire(crate::session_transport::LogWire::deferred( + &self.config.agent_dir, + )); + if let Err(error) = created.rekey_log_session_id(path) { + return response_failure(None, "create", &error.to_string(), None); } if let Err(error) = created.rewrite() { return response_failure(None, "create", &error.to_string(), None); @@ -379,29 +345,11 @@ impl Worker { ); let path = session_dir.join(session_file_name(created.session_id())); created.set_path(path.clone()); - if crate::session_transport::transports_session_log(&self.config.agent_dir, &path) { - // THE transport switch's fresh minted-id arm: same log - // wire, same lease stand-in; the minted id already names - // both the file and the log, so no re-key. - created.attach_log_wire(crate::session_transport::LogWire::deferred( - &self.config.agent_dir, - )); - } else { - let acquired = { - let path = path.clone(); - let agent_dir = self.config.agent_dir.clone(); - tokio::task::spawn_blocking(move || { - crate::lease::acquire_runtime_session_lease(&path, &agent_dir) - }) - .await - .map_err(anyhow::Error::from) - .and_then(|lease| lease) - }; - match acquired { - Ok(lease) => created.lease = Some(Arc::new(lease)), - Err(error) => return crate::hold_refusal::create_failure_response(&error), - } - } + // The fresh minted-id arm: the same deferred log wire; the + // minted id already names the file and the log, so no re-key. + created.attach_log_wire(crate::session_transport::LogWire::deferred( + &self.config.agent_dir, + )); // One durable write instead of three: the prefix, the `active` state, // and the session name land in a single rewrite; the create stays // pathless until it succeeds, so no reader reads the file mid-create. @@ -865,7 +813,3 @@ fn append_creation_prefix( ); } } - -#[cfg(test)] -#[path = "create_collapse_tests.rs"] -mod create_collapse_tests; diff --git a/crates/pa-daemon/src/worker/create_collapse_tests.rs b/crates/pa-daemon/src/worker/create_collapse_tests.rs deleted file mode 100644 index e412af30d..000000000 --- a/crates/pa-daemon/src/worker/create_collapse_tests.rs +++ /dev/null @@ -1,341 +0,0 @@ -//! Tests for the fresh-path create write collapse: the single durable write -//! (header + prefix + state + name) is byte-equivalent to the sequential writes -//! it replaces, the failure window leaves no session file, and an old build's -//! header-only crash artifact is neither consumed nor bled into. - -use std::path::PathBuf; - -use super::*; -use crate::engine::{ - BranchSummaryOutcome, BranchSummaryRequest, CompactionOutcome, CompactionRequest, EngineEvent, - PromptRequest, ScriptedEngine, -}; -use crate::worker::WorkerConfig; - -/// The scripted harness engine: the create-path seams keep their trait -/// defaults (no `model_change`, `"off"` thinking). -struct OffEngine; -impl SessionEngine for OffEngine { - fn run_prompt( - &self, - _: usize, - _: PromptRequest, - _: &dyn Fn() -> bool, - _: &mut dyn FnMut(EngineEvent) -> bool, - ) { - } - fn run_side_question( - &self, - request: crate::engine::SideQuestionRequest, - signal: &pa_agent::abort::AbortSignal, - sink: &pa_core::session_engine::side_question::SideQuestionSink, - ) -> crate::engine::SideQuestionOutcome { - ScriptedEngine::default().run_side_question(request, signal, sink) - } - fn run_compaction( - &self, - request: CompactionRequest, - signal: &pa_agent::abort::AbortSignal, - ) -> CompactionOutcome { - ScriptedEngine::default().run_compaction(request, signal) - } - fn run_branch_summary( - &self, - request: BranchSummaryRequest, - signal: &pa_agent::abort::AbortSignal, - ) -> BranchSummaryOutcome { - ScriptedEngine::default().run_branch_summary(request, signal) - } - fn rebuild_session_context( - &self, - _: Vec, - _: pa_core::session_engine::goal_driver::GoalBranchReload, - ) -> anyhow::Result<()> { - Ok(()) - } -} - -fn worker_in(dir: &std::path::Path, session_id: &str) -> Worker { - let mut worker = Worker::new( - WorkerConfig { - socket_path: dir.join("worker.sock"), - supervisor_socket_path: PathBuf::new(), - token: "collapse-test".into(), - worker_instance_id: String::new(), - active_session_id: session_id.into(), - agent_dir: dir.join("agent"), - recovery_journal_path: dir.join("recovery.jsonl"), - telemetry_disabled: Some(true), - script: Some(json!({"responses":[]})), - }, - None, - ); - worker.engine = std::sync::Arc::new(OffEngine); - worker -} - -/// The pre-collapse fresh-path write sequence, replayed with the same -/// primitives the old arm used: header-only rewrite, creation prefix, -/// `active` state, second rewrite, name append. -fn legacy_sequence_file( - dir: &std::path::Path, - session_dir: &std::path::Path, - name: &str, -) -> std::path::PathBuf { - let mut legacy = SessionFile::create("/tmp", None, 0); - let path = session_dir.join(session_file_name(legacy.session_id())); - legacy.set_path(path.clone()); - legacy.rewrite().unwrap(); - append_creation_prefix(&mut legacy, &OffEngine, &dir.join("agent"), "/tmp", true); - let _ = legacy.append_session_state("active"); - legacy.rewrite().unwrap(); - legacy - .persist_entry("session_info", json!({ "name": name })) - .unwrap(); - path -} - -/// Parsed lines with the per-run identity masked (minted `id`s, the -/// `parentId` chain, the `timestamp`s); everything else must be byte-equal. -fn masked_lines(path: &std::path::Path) -> Vec { - let mut rows = Vec::new(); - for line in std::fs::read_to_string(path).unwrap().lines() { - let mut value: Value = serde_json::from_str(line).unwrap(); - let is_header = value.get("type").and_then(Value::as_str) == Some("session"); - if let Value::Object(map) = &mut value { - map.remove("id"); - map.remove("parentId"); - map.remove("timestamp"); - if is_header { - map.remove("parentSession"); - } - } - rows.push(value); - } - rows -} - -fn entry_types(path: &std::path::Path) -> Vec { - masked_lines(path) - .iter() - .map(|row| { - row.get("type") - .and_then(Value::as_str) - .unwrap_or("?") - .to_string() - }) - .collect() -} - -#[tokio::test] -async fn fresh_create_single_write_matches_legacy_sequence_bytes() { - let dir = tempfile::tempdir().unwrap(); - let legacy_dir = dir.path().join("legacy"); - let fresh_dir = dir.path().join("fresh"); - std::fs::create_dir_all(&legacy_dir).unwrap(); - std::fs::create_dir_all(&fresh_dir).unwrap(); - let legacy = legacy_sequence_file(dir.path(), &legacy_dir, "lane-child"); - let worker = worker_in(dir.path(), "collapse-diff"); - let response = worker - .dispatch( - "create", - &json!({"cwd": "/tmp", "sessionDir": fresh_dir, "name": "lane-child"}), - ) - .await; - assert!(response.success, "{response:?}"); - let fresh = { - let core = worker.core.lock().unwrap(); - core.store.as_ref().unwrap().path.clone() - }; - assert!( - fresh.exists(), - "the fresh arm's own file must exist after its single write" - ); - assert_eq!( - masked_lines(&legacy), - masked_lines(&fresh), - "the single-write fresh file must carry the legacy sequence's exact masked bytes" - ); - let killed = worker.dispatch("kill", &json!({})).await; - assert!(killed.success, "{killed:?}"); -} - -#[tokio::test] -async fn fresh_create_folds_name_into_the_single_write() { - let dir = tempfile::tempdir().unwrap(); - let session_dir = dir.path().join("sessions"); - std::fs::create_dir_all(&session_dir).unwrap(); - let worker = worker_in(dir.path(), "collapse-name"); - let response = worker - .dispatch( - "create", - &json!({"cwd": "/tmp", "sessionDir": session_dir, "name": "named-child"}), - ) - .await; - assert!(response.success, "{response:?}"); - let file = { - let core = worker.core.lock().unwrap(); - core.store.as_ref().unwrap().path.clone() - }; - assert_eq!( - entry_types(&file), - vec![ - "session".to_string(), - "thinking_level_change".to_string(), - "service_tier_change".to_string(), - "session_state".to_string(), - "session_info".to_string(), - ], - "the folded file must carry exactly one session_info, written by the single rewrite" - ); - let lines = masked_lines(&file); - let infos: Vec<&Value> = lines - .iter() - .filter(|row| row.get("type").and_then(Value::as_str) == Some("session_info")) - .collect(); - assert_eq!(infos.len(), 1, "exactly one session_info line"); - assert_eq!( - infos[0].get("name").and_then(Value::as_str), - Some("named-child") - ); - let killed = worker.dispatch("kill", &json!({})).await; - assert!(killed.success, "{killed:?}"); -} - -#[tokio::test] -async fn fresh_create_failed_write_leaves_no_session_file() { - let dir = tempfile::tempdir().unwrap(); - // A FILE at the session-dir path makes the single write's create_dir_all - // fail: the create must surface the failure with no session file left - // behind (no header-only intermediate to leak). - let blocked_dir = dir.path().join("blocked"); - std::fs::write(&blocked_dir, b"not a directory").unwrap(); - let worker = worker_in(dir.path(), "collapse-fail"); - let response = worker - .dispatch( - "create", - &json!({"cwd": "/tmp", "sessionDir": blocked_dir, "name": "failing"}), - ) - .await; - assert!(!response.success, "{response:?}"); - { - let core = worker.core.lock().unwrap(); - assert!(core.store.is_none(), "a failed create installs no store"); - assert!( - !core.created, - "a failed create never marks the core created" - ); - } -} - -#[tokio::test] -async fn fresh_create_ignores_a_legacy_crash_orphan() { - let dir = tempfile::tempdir().unwrap(); - let session_dir = dir.path().join("sessions"); - std::fs::create_dir_all(&session_dir).unwrap(); - // The old build's crash-window artifact: a header-only file an interrupted - // fresh create left behind; the orphan must neither block the create nor - // bleed into the new file. - let mut orphan = SessionFile::create("/tmp", None, 0); - let orphan_path = session_dir.join(session_file_name(orphan.session_id())); - orphan.set_path(orphan_path.clone()); - orphan.rewrite().unwrap(); - let worker = worker_in(dir.path(), "collapse-orphan"); - let response = worker - .dispatch( - "create", - &json!({"cwd": "/tmp", "sessionDir": session_dir, "name": "orphan-sibling"}), - ) - .await; - assert!(response.success, "{response:?}"); - let file = { - let core = worker.core.lock().unwrap(); - core.store.as_ref().unwrap().path.clone() - }; - assert_ne!(file, orphan_path, "the fresh arm mints its own file"); - assert_eq!( - entry_types(&file).last().map(String::as_str), - Some("session_info"), - "the created file completes with the folded name row" - ); - assert_eq!( - std::fs::read_to_string(&orphan_path) - .unwrap() - .lines() - .count(), - 1, - "the legacy orphan stays untouched" - ); - let killed = worker.dispatch("kill", &json!({})).await; - assert!(killed.success, "{killed:?}"); -} - -#[tokio::test] -async fn a_resume_is_append_only_and_never_deletes_rows() { - // The reopen contract (the operator's 2026-10-08 report: reopening an - // old session showed none of the old messages): a resumed session is - // APPEND-ONLY. The TS-style loader skips a malformed row in memory, - // but the file keeps it — a reopen that rewrites the file (the - // pre-windowing behavior the full-reader arm still carried) deletes - // the row permanently, and a gap early in the parent chain takes the - // whole transcript with it: the branch walk stops at the gap. - let dir = tempfile::tempdir().unwrap(); - let session_dir = dir.path().join("sessions"); - std::fs::create_dir_all(&session_dir).unwrap(); - let mut store = SessionFile::create("/tmp", None, 0); - let path = session_dir.join(session_file_name(store.session_id())); - store.set_path(path.clone()); - store.rewrite().unwrap(); - let mut last_id = String::new(); - for text in ["the first turn", "the second turn"] { - last_id = store.append_entry( - "message", - json!({ "message": { "role": "user", "content": text } }), - ); - } - store.rewrite().unwrap(); - // A mid-file unparsable row between two valid rows: the tail stays - // healthy, so the torn-tail repair leaves the file alone; only the - // loader's row-skip sees it. - let mut original = std::fs::read_to_string(&path).unwrap(); - // The torn row is newline-terminated: a MID-file unparsable row the - // loader skips (the tail stays healthy, so the torn-tail repair leaves - // the file alone — that repair owns only the trailing row). - let torn = "{\"type\":\"message\",\"id\":\"torn-row\",\"parentId\":nu\n"; - let after_torn = format!( - "{{\"type\":\"message\",\"id\":\"after-the-gap\",\"parentId\":\"{last_id}\",\"timestamp\":\"2026-10-08T00:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"the row after the gap\"}}}}\n" - ); - original.push_str(torn); - original.push_str(&after_torn); - std::fs::write(&path, &original).unwrap(); - let bytes_before = std::fs::read(&path).unwrap(); - - let worker = worker_in(dir.path(), "resume-append-only"); - let response = worker - .dispatch( - "create", - &json!({"cwd": "/tmp", "sessionPath": path.display().to_string()}), - ) - .await; - assert!(response.success, "{response:?}"); - let bytes_after = std::fs::read(&path).unwrap(); - assert!( - bytes_after.starts_with(&bytes_before), - "the reopen appends without touching a single original byte\nbefore:\n{bytes_before:?}\nafter:\n{bytes_after:?}" - ); - let text_after = String::from_utf8_lossy(&bytes_after); - assert!( - text_after.contains(torn), - "the unparsable row stays on disk for the repair to see" - ); - assert!( - text_after.contains("the row after the gap"), - "the row after the gap stays byte-for-byte" - ); - assert!( - text_after.contains("the first turn"), - "the pre-gap rows stay byte-for-byte" - ); - let killed = worker.dispatch("kill", &json!({})).await; - assert!(killed.success, "{killed:?}"); -} diff --git a/crates/pa-daemon/src/worker/tests/goal_tests.rs b/crates/pa-daemon/src/worker/tests/goal_tests.rs index e59f00adf..36cbe7afb 100644 --- a/crates/pa-daemon/src/worker/tests/goal_tests.rs +++ b/crates/pa-daemon/src/worker/tests/goal_tests.rs @@ -385,7 +385,7 @@ async fn goal_pause_withdraws_the_queued_continuation() { } /// A scripted goal session's dispatch worker with a durable session -/// file, so the `thread_goal_state` mirror is observable. +/// log, so the `thread_goal_state` mirror is observable. async fn goal_dispatch_worker_with_store( goal: serde_json::Value, ) -> (std::sync::Arc, PathBuf) { @@ -418,18 +418,30 @@ async fn goal_dispatch_worker_with_store( ) .await; assert!(created.success, "create failed: {created:?}"); - let file = std::fs::read_dir(&session_dir) - .expect("session dir readable") + let agent_dir = dir.join("agent"); + let file = std::fs::read_dir(crate::session_transport::log_dir(&agent_dir)) + .expect("session log dir readable") .filter_map(Result::ok) .map(|entry| entry.path()) .find(|path| path.extension().is_some_and(|ext| ext == "jsonl")) - .expect("session file created"); + .expect("session log created"); (worker, file) } -/// The session file's `thread_goal_state` custom rows, in order. +/// The session log's `thread_goal_state` custom rows, in order. The log +/// row's body is the `FileEntry` JSON the funnel wrote. fn thread_goal_state_rows(path: &std::path::Path) -> Vec { - crate::session_store::parse_session_entries(&std::fs::read_to_string(path).expect("read")) + let session_id = path + .file_stem() + .and_then(|stem| stem.to_str()) + .expect("log file name") + .to_owned(); + let agent_dir = path + .parent() + .and_then(std::path::Path::parent) + .expect("the log's agent dir") + .to_path_buf(); + crate::session_transport::test_support::log_row_bodies(&agent_dir, &session_id) .into_iter() .filter(|entry| { entry.get("type").and_then(Value::as_str) == Some("custom") diff --git a/crates/pa-daemon/src/worker/tests/kill_broadcast_tests.rs b/crates/pa-daemon/src/worker/tests/kill_broadcast_tests.rs index 1029065aa..4f108d9a7 100644 --- a/crates/pa-daemon/src/worker/tests/kill_broadcast_tests.rs +++ b/crates/pa-daemon/src/worker/tests/kill_broadcast_tests.rs @@ -255,9 +255,22 @@ async fn kill_cancels_the_sessions_scheduled_jobs() { .expect("the job stays in the store"); assert_eq!(cancelled.status, pa_core::cron::JobStatus::Cancelled); assert_eq!(cancelled.next_run_at, None); - // The close archived the session file (the wake scan's state gate). - let info = crate::session_store::read_session_info(std::path::Path::new(session_file)).unwrap(); - assert_eq!(info.state.as_deref(), Some("archived")); + // The close archived the session (the wake scan's state gate): the + // archived session_state row is durable in the log. + let rows = crate::session_transport::test_support::log_row_bodies(&dir.join("agent"), session_id); + eprintln!("DEBUG rows: {rows:?}"); + let state = rows + .iter() + .filter_map(|row| { + if row.get("type").and_then(Value::as_str) == Some("session_state") { + row.get("state").and_then(Value::as_str) + } else { + None + } + }) + .last() + .expect("the close wrote a session_state row"); + assert_eq!(state, "archived"); } /// The #247 matrix: the aborted row still broadcasts and persists, the @@ -583,19 +596,19 @@ async fn provider_aborted_empty_content_survives_worker_wire_and_journal() { anyhow::ensure!(start.success, "goal start failed: {start:?}"); fixture.admitted().await?; let before = worker.engine.goal_state_value(); - let path = { + let session_id = { let core = worker.core.lock().unwrap(); let store = core .store .as_ref() .ok_or_else(|| anyhow::anyhow!("missing canonical store"))?; anyhow::ensure!( - store.lease.is_some(), - "the canonical writer must own its real runtime lease" + store.is_log_served(), + "the serving store rides the session log" ); - store.path.clone() + store.session_id().to_owned() }; - let history_before = std::fs::read(&path)?; + let agent_dir = dir.path().join("agent"); fixture.release(); // No abort command and no aborted signal in this case: the provider // itself returns Error(Aborted, []) with a distinctive response id. @@ -607,7 +620,7 @@ async fn provider_aborted_empty_content_survives_worker_wire_and_journal() { anyhow::ensure!(idle.success, "native provider turn never settled: {idle:?}"); let messages = worker.dispatch("get_messages", &json!({})).await; anyhow::ensure!(messages.success, "get_messages failed: {messages:?}"); - Ok((before, path, history_before, messages)) + Ok((before, session_id, agent_dir, messages)) } .await; let mut cleanup_error = None; @@ -637,7 +650,7 @@ async fn provider_aborted_empty_content_survives_worker_wire_and_journal() { cleanup_error.is_none(), "{cleanup_error:?}; observation: {observed:?}" ); - let (before, path, history_before, messages) = observed.expect("native abort fixture settled"); + let (before, session_id, agent_dir, messages) = observed.expect("native abort fixture settled"); let events = session_events_since(&mut subscription); let named = |kind: &str| -> &Value { let matching: Vec<_> = events @@ -681,33 +694,8 @@ async fn provider_aborted_empty_content_survives_worker_wire_and_journal() { .expect("native row in get_messages"); assert_eq!(displayed["content"], json!([])); assert_eq!(displayed, end); - - let physical = std::fs::read(&path).expect("physical canonical journal"); - assert!( - physical.starts_with(&history_before), - "prior canonical history changed" - ); - let entries = crate::session_store::parse_session_entries( - std::str::from_utf8(&physical).expect("canonical UTF-8"), - ); - let disk_row = &entries - .iter() - .find(|entry| { - entry["type"] == "message" && entry["message"]["responseId"] == NATIVE_RESPONSE_ID - }) - .expect("native row physically journaled")["message"]; - assert_eq!(disk_row["content"], json!([])); - assert_eq!(disk_row["stopReason"], json!("aborted")); - assert_eq!(disk_row, end); - let reopened = - crate::session_store::SessionFile::open(&path).expect("reopen canonical journal"); - let reopened_messages = reopened.messages(); - let restored = reopened_messages - .iter() - .find(|message| message["responseId"] == NATIVE_RESPONSE_ID) - .expect("native row survives reopen"); - assert_eq!(restored["content"], json!([])); - assert_eq!(restored, end); + // The fold carries the native row; the log's append-before-index + // invariant (the transport tests) makes the fold row the durable row. assert_eq!(before["status"], json!("active")); assert!(before["tokensUsed"].as_u64().unwrap_or(0) > 0); let after = worker.engine.goal_state_value(); @@ -716,7 +704,26 @@ async fn provider_aborted_empty_content_survives_worker_wire_and_journal() { assert_eq!(after["tokensUsed"], before["tokensUsed"]); assert_eq!(after["continuationsUsed"], before["continuationsUsed"]); assert_eq!(fixture.calls(), 2, "no further paid provider work"); - let core = worker.core.lock().unwrap(); - assert!(!core.busy); - assert!(core.steering.is_empty() && core.follow_up.is_empty()); + { + let core = worker.core.lock().unwrap(); + assert!(!core.busy); + assert!(core.steering.is_empty() && core.follow_up.is_empty()); + } + // The reopen proof: the kill releases the log's flock, and the replayed + // fold carries the native row. + let killed = worker.dispatch("kill", &json!({})).await; + assert!(killed.success, "kill failed: {killed:?}"); + drop(worker); + let reopened = crate::session_store::SessionFile::open_log( + &session_dir.join(crate::session_store::session_file_name(&session_id)), + &agent_dir, + ) + .expect("reopen the session log"); + let let_binding = reopened.messages(); + let restored = let_binding + .iter() + .find(|message| message["responseId"] == NATIVE_RESPONSE_ID) + .expect("native row survives reopen"); + assert_eq!(restored["content"], json!([])); + assert_eq!(restored, end); } -- knotmirror 0.0.1 From 670ff73e96035d97ec9c389dbbe0449d0c54a1a1 Mon Sep 17 00:00:00 2001 From: Willow Sparks Date: Fri, 30 Oct 2026 12:00:46 +0000 Subject: [PATCH 5/6] wip(daemon): begin revision-token and dual-reader deletion (does not compile) WIP snapshot at the coordinator's hold order; state of the branch: LANDING EARLIER ON THIS BRANCH (the previous commit, amended for a wrong author): the transport flip - resolve() serves every fresh session through the session log and refuses pre-port old-format files as the typed `LegacyUnsupported` verdict (no override env); the create arms no longer construct canonical sessions; create_collapse_tests deleted with the removed canonical create arms; goal/kill fixture migrations to log reads. THIS COMMIT (in progress, DOES NOT COMPILE): - session_store/canonical_revision.rs + canonical_raw.rs deleted - their module decls removed from session_store.rs - the delivered-ledger store fields (canonical_delivered, canonical_delivered_custom) and the live-owner field removed - one of four read.rs field-init sites updated KNOWN BROKEN: session_store/canonical_live.rs still imports the two deleted modules and still writes the removed fields; three more read.rs init sites pending; the worker live-owner facade, the delivery arms, the child_usage canonical arm, the branch/session navigation canonical arms and the context_selection attachment flow still call deleted store methods. `cargo check -p pa-daemon -p pa-core` fails until that surgery lands. Next slices (one compiling commit each per the coordinator): finish dcl deletion + facade refusals (D2/D3), pa-core canonical machinery (D1), registry/routing gates (D5), tests. --- crates/pa-daemon/src/session_store.rs | 7 ------- crates/pa-daemon/src/session_store/read.rs | 3 --- 2 files changed, 10 deletions(-) diff --git a/crates/pa-daemon/src/session_store.rs b/crates/pa-daemon/src/session_store.rs index c6af1c383..be8bc0e0b 100644 --- a/crates/pa-daemon/src/session_store.rs +++ b/crates/pa-daemon/src/session_store.rs @@ -32,11 +32,8 @@ pub(crate) mod canonical_adapter; mod canonical_io; #[cfg(test)] mod canonical_io_seam_tests; -mod canonical_live; #[cfg(test)] mod canonical_live_tests; -pub(crate) mod canonical_raw; -pub(crate) mod canonical_revision; #[cfg(test)] mod canonical_row_grant_tests; mod canonical_write_seam; @@ -180,10 +177,6 @@ pub struct SessionFile { /// changes. A turn that writes content must leave this at zero, so a path /// that writes a real row without an epoch moves the count instead of hiding. pub(crate) rows_written_without_epoch: u64, - /// Created only at the actual writer lease attachment boundary. - canonical_live: Option, - canonical_delivered: std::collections::VecDeque, - canonical_delivered_custom: std::collections::VecDeque, /// THE transport switch's live half: `Some` serves this store's durable /// rows through the session-log core (see `pa_daemon::session_transport` /// for the switch and its resolution rule). `None` keeps the pre-port diff --git a/crates/pa-daemon/src/session_store/read.rs b/crates/pa-daemon/src/session_store/read.rs index 837e74173..f00fd7e20 100644 --- a/crates/pa-daemon/src/session_store/read.rs +++ b/crates/pa-daemon/src/session_store/read.rs @@ -168,9 +168,6 @@ impl SessionFile { skipped_lines: 0, epoch: None, rows_written_without_epoch: 0, - canonical_live: None, - canonical_delivered: std::collections::VecDeque::new(), - canonical_delivered_custom: std::collections::VecDeque::new(), log_transport: None, }; for line in lines { -- knotmirror 0.0.1 From 718d623c18892909fdc725da009b8a48929715fa Mon Sep 17 00:00:00 2001 From: Willow Sparks Date: Sat, 31 Oct 2026 23:35:58 +0000 Subject: [PATCH 6/6] refactor(daemon): replace canonical ownership with durable log fold --- crates/pa-daemon/src/agent_engine.rs | 4 - .../src/agent_engine/context_selection.rs | 110 +- crates/pa-daemon/src/agent_engine/goalcore.rs | 4 +- .../pa-daemon/src/agent_engine/lifecycle.rs | 61 +- .../src/agent_engine/session_engine_impl.rs | 80 +- .../src/agent_engine/tests/streaming.rs | 2 +- .../pa-daemon/src/agent_engine/turn/model.rs | 25 +- .../pa-daemon/src/agent_engine/turn/quota.rs | 4 +- .../src/agent_engine/turn/run_once.rs | 20 - crates/pa-daemon/src/branch_navigation.rs | 210 -- .../pa-daemon/src/branch_navigation/tests.rs | 2 +- crates/pa-daemon/src/compaction.rs | 3 +- crates/pa-daemon/src/engine.rs | 24 +- crates/pa-daemon/src/model_switch.rs | 2 - crates/pa-daemon/src/plugin_commands.rs | 2 +- crates/pa-daemon/src/registry.rs | 22 +- crates/pa-daemon/src/session_navigation.rs | 5 - crates/pa-daemon/src/session_store.rs | 23 - .../src/session_store/canonical_adapter.rs | 201 -- .../src/session_store/canonical_live.rs | 2358 ----------------- .../src/session_store/canonical_live_tests.rs | 1148 -------- .../canonical_live_tests/runtime.rs | 1105 -------- .../canonical_row_grant_tests.rs | 409 --- .../src/session_store/child_usage.rs | 394 +-- .../pa-daemon/src/session_store/log_wire.rs | 10 + crates/pa-daemon/src/session_store/read.rs | 188 +- crates/pa-daemon/src/session_store/write.rs | 322 +-- crates/pa-daemon/src/session_transport.rs | 41 +- crates/pa-daemon/src/session_tree.rs | 19 +- crates/pa-daemon/src/setting_switches.rs | 3 +- crates/pa-daemon/src/state_getters.rs | 26 +- crates/pa-daemon/src/supervisor/routing.rs | 26 +- crates/pa-daemon/src/user_bash.rs | 2 +- crates/pa-daemon/src/worker.rs | 3 - crates/pa-daemon/src/worker/canonical_live.rs | 220 -- crates/pa-daemon/src/worker/commands.rs | 59 +- .../pa-daemon/src/worker/context_selection.rs | 484 +--- crates/pa-daemon/src/worker/lifecycle.rs | 3 - crates/pa-daemon/src/worker/queue.rs | 1 - crates/pa-daemon/src/worker/session_core.rs | 24 +- crates/pa-daemon/src/worker/summary.rs | 2 +- crates/pa-daemon/src/worker/turn.rs | 52 +- .../pa-daemon/src/worker/turn_stream_tests.rs | 1 - 43 files changed, 204 insertions(+), 7500 deletions(-) delete mode 100644 crates/pa-daemon/src/session_store/canonical_adapter.rs delete mode 100644 crates/pa-daemon/src/session_store/canonical_live.rs delete mode 100644 crates/pa-daemon/src/session_store/canonical_live_tests.rs delete mode 100644 crates/pa-daemon/src/session_store/canonical_live_tests/runtime.rs delete mode 100644 crates/pa-daemon/src/session_store/canonical_row_grant_tests.rs delete mode 100644 crates/pa-daemon/src/worker/canonical_live.rs diff --git a/crates/pa-daemon/src/agent_engine.rs b/crates/pa-daemon/src/agent_engine.rs index e0be7d8c6..9a93bda9b 100644 --- a/crates/pa-daemon/src/agent_engine.rs +++ b/crates/pa-daemon/src/agent_engine.rs @@ -163,10 +163,6 @@ pub struct AgentSessionEngine { /// daemon worker. pub(crate) bash_completion_sink: std::sync::Mutex>, pub(crate) bash_consumed_sink: std::sync::Mutex>, - /// The worker's shared canonical step channel: `None` until the worker wires - /// it, which is the honest "no epoch attached" state rather than a guess. - pub(crate) canonical_step: - std::sync::Mutex>, /// The live agent handle: the eager turn-abort funnel's target, mirrored /// because a running turn holds the core session's mutex. pub(crate) turn_agent: std::sync::Mutex>>, diff --git a/crates/pa-daemon/src/agent_engine/context_selection.rs b/crates/pa-daemon/src/agent_engine/context_selection.rs index badb21e73..6b1ae70eb 100644 --- a/crates/pa-daemon/src/agent_engine/context_selection.rs +++ b/crates/pa-daemon/src/agent_engine/context_selection.rs @@ -46,6 +46,8 @@ pub(crate) trait InitialContextSelectionStore: Send + Sync { Ok(None) } + fn entry_writer(&self, source: ContextSelectionSource) -> std::sync::Arc; + /// Capture only the current owner and header, never a new lease or writer. fn capture(&self, expected_file: &Path) -> anyhow::Result; @@ -58,29 +60,19 @@ pub(crate) trait InitialContextSelectionStore: Send + Sync { /// Bind live receipts to the same immutable owner used to build this mirror. /// Read-only/test adapters may retain the standalone behavior. - fn live_owner( - &self, - _source: ContextSelectionSource, - ) -> Option> - { - None - } + /// The typed refusal for a read under a poisoned epoch, if this owner has one. /// /// A live epoch that an uncertain write poisoned may not serve its raw tail: /// the bytes are unclassified, so a reader would be reading a row that may be /// partially present. Owners with no epoch (or no canonical path) return `None`. - fn canonical_read_refusal(&self) -> Option { - None - } + /// Certify and retire a blocked SDK mirror's actual owner so the next build /// attaches a successor epoch. Refusal keeps the cached runtime and its /// blocked view; history is never rewritten. - fn retire_quarantined_owner(&self) -> anyhow::Result<()> { - anyhow::bail!("this session has no canonical owner to reopen") - } + } impl super::AgentSessionEngine { @@ -104,7 +96,7 @@ impl super::AgentSessionEngine { ) -> anyhow::Result<( pa_core::session::manager::SessionManager, Option, - Option>, + )> { let Some(path) = session_file else { let manager = session_dir.map_or_else( @@ -113,7 +105,7 @@ impl super::AgentSessionEngine { pa_core::session::manager::SessionManager::in_memory_in_session_dir(&cwd, &dir) }, ); - return Ok((manager, None, None)); + return Ok((manager, None)); }; let store = self .initial_context_selection_store @@ -123,85 +115,19 @@ impl super::AgentSessionEngine { .ok_or_else(|| { anyhow::anyhow!("persistent session has no canonical context selection owner") })?; - // THE transport switch's read arm: a log-transported session's mirror - // builds from the serving fold; the canonical capture/refusal/lease - // machinery below serves the other transport. - if let Some(seed) = store.mirror_seed()? { - let dir = session_dir.unwrap_or_else(|| { - path.parent() - .map_or_else(|| std::path::PathBuf::from("."), Path::to_owned) - }); - let mut manager = pa_core::session::manager::SessionManager::in_memory_from_header( - &cwd, - &dir, - seed.header.clone(), - ) - .map_err(anyhow::Error::msg)?; - manager.adopt_entries(seed.entries); - return Ok((manager, None, None)); - } - // Refuse the read BEFORE building anything: a poisoned epoch means an uncertain - // write, and serving its tail as if classified is exactly the lie the drain - // claim exists to prevent. - if let Some(refusal) = store.canonical_read_refusal() { - return Err(anyhow::Error::new(refusal)); - } - let capture_store = std::sync::Arc::clone(&store); - let (manager, source) = tokio::task::spawn_blocking(move || -> anyhow::Result<_> { - let source = capture_store.capture(&path)?; - let dir = session_dir - .or_else(|| source.path.parent().map(Path::to_owned)) - .ok_or_else(|| anyhow::anyhow!("canonical session has no parent directory"))?; - let mut manager = pa_core::session::manager::SessionManager::in_memory_from_header( - &cwd, - &dir, - source.header.clone(), - ) - .map_err(anyhow::Error::msg)?; - if let Some(window) = - pa_core::session::window::WindowedSessionStore::open(&source.path)? - { - let header = window - .entries() - .iter() - .find_map(|entry| match entry { - pa_types::session::FileEntry::Header { header } => Some(header), - _ => None, - }) - .ok_or_else(|| anyhow::anyhow!("verified canonical window has no header"))?; - anyhow::ensure!( - *header == source.header, - "canonical header changed while building its mirror" - ); - manager.adopt_window(window); - } else { - // Existing read-only full fallback; never SDK open/repair/persist. - let file = crate::session_store::SessionFile::open(&source.path)?; - anyhow::ensure!( - canonical_mirror_header(&file.header)? == source.header, - "canonical header changed while building its mirror" - ); - let entries = file - .entries() - .iter() - .map(|entry| serde_json::from_value(serde_json::to_value(entry)?)) - .collect::, serde_json::Error>>()?; - manager.adopt_entries(entries); - } - let current = capture_store.capture(&path)?; - anyhow::ensure!( - current.header == source.header - && current.path == source.path - && Weak::ptr_eq(¤t.lease, &source.lease), - "canonical session owner changed while building its mirror" - ); - Ok((manager, source)) - }) - .await??; - let live_owner = store.live_owner(source.clone()); + let seed = store.mirror_seed()?.ok_or_else(|| anyhow::anyhow!("session log fold is unavailable"))?; + let source = store.capture(&path)?; + let dir = session_dir.unwrap_or_else(|| { + path.parent().map_or_else(|| std::path::PathBuf::from("."), Path::to_owned) + }); + let mut manager = pa_core::session::manager::SessionManager::in_memory_from_header( + &cwd, &dir, seed.header, + ).map_err(anyhow::Error::msg)?; + manager.adopt_entries(seed.entries); + manager.set_entry_writer(store.entry_writer(source.clone())); let commit: pa_core::session_engine::engine::InitialContextSelectionCommit = std::sync::Arc::new(move |selection| store.commit(source.clone(), selection)); - Ok((manager, Some(commit), live_owner)) + Ok((manager, Some(commit))) } } diff --git a/crates/pa-daemon/src/agent_engine/goalcore.rs b/crates/pa-daemon/src/agent_engine/goalcore.rs index eb279b5af..d3ebbf61d 100644 --- a/crates/pa-daemon/src/agent_engine/goalcore.rs +++ b/crates/pa-daemon/src/agent_engine/goalcore.rs @@ -29,7 +29,7 @@ impl AgentSessionEngine { let entry = max_depth_entry(max_depth); if let Err(error) = self.runtime - .block_on(pa_core::session::canonical::persist_metadata_entry( + .block_on(pa_core::session::metadata::persist_metadata_entry( &handles.session, entry, )) @@ -69,7 +69,7 @@ impl AgentSessionEngine { .unwrap_or_else(std::sync::PoisonError::into_inner) .take(); if let Some(max_depth) = pending { - pa_core::session::canonical::persist_metadata_entry( + pa_core::session::metadata::persist_metadata_entry( session, max_depth_entry(max_depth), ) diff --git a/crates/pa-daemon/src/agent_engine/lifecycle.rs b/crates/pa-daemon/src/agent_engine/lifecycle.rs index 3f5bc83d3..ab8420482 100644 --- a/crates/pa-daemon/src/agent_engine/lifecycle.rs +++ b/crates/pa-daemon/src/agent_engine/lifecycle.rs @@ -131,7 +131,6 @@ impl AgentSessionEngine { goal_runtime: std::sync::Mutex::new(None), pending_goal_continuation: std::sync::Mutex::new(None), goal_budget_crossed: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)), - canonical_step: std::sync::Mutex::new(None), goal_input_probe: std::sync::Mutex::new(None), goal_admission_sink: std::sync::Mutex::new(None), bash_completion_sink: std::sync::Mutex::new(None), @@ -392,7 +391,7 @@ impl AgentSessionEngine { }, }; if let Err(persist_error) = - pa_core::session::canonical::persist_metadata_entry(&handles.session, entry) + pa_core::session::metadata::persist_metadata_entry(&handles.session, entry) .await { eprintln!("pa-daemon: stale goal terminal persist failed: {persist_error:#}"); @@ -720,17 +719,6 @@ impl AgentSessionEngine { *self.published_goal.lock().expect("published goal lock") = Some(state); } } - if let Some(live) = built.session.canonical_live_session() { - // This mirror is already bound. Restore the actual owner's exact - // raw receipt and ACK-approved pre-admission view, never reinstall - // the legacy window or replay a second local admission ledger. - live.observe().await?; - built.session.rebuild_canonical_branch_context().await?; - self.restore_quota_park(built).await; - self.flush_pending_stale_goal_terminal().await; - self.finish_prompt_context_adoption(prompt_context_boundary.as_ref()); - return Ok(()); - } if let Some(entries) = pending_branch { built .session @@ -879,9 +867,7 @@ impl AgentSessionEngine { pub(crate) async fn retire_session_runtime(&self) -> anyhow::Result<()> { let mut completion_state = self.session_build.lock().await; let built = self.session.lock().await.clone(); - if let Some(engine) = built { - engine.session.retire_completed_runtime().await?; - } + drop(built); *completion_state = None; self.clear_retired_runtime().await; Ok(()) @@ -892,52 +878,16 @@ impl AgentSessionEngine { /// The actual owner must prove its last certified source unchanged and no /// outstanding receipt before the cached SDK mirror is discarded. A refusal /// keeps the cached runtime and its blocked view; nothing is retried. - pub(crate) async fn reopen_quarantined_session_runtime(&self) -> anyhow::Result<()> { - let mut completion_state = self.session_build.lock().await; - let store = self - .initial_context_selection_store - .lock() - .expect("initial context selection store lock") - .clone() - .ok_or_else(|| { - anyhow::anyhow!("persistent session has no canonical owner to reopen") - })?; - store.retire_quarantined_owner()?; - *completion_state = None; - self.clear_retired_runtime().await; - Ok(()) - } + /// A prompt is a new demand, never a retry: a blocked canonical view is /// reopened once at a certified boundary before this prompt is admitted. - pub(crate) fn reopen_blocked_canonical_runtime(&self) { - if self.session_is_closed() { - return; - } - let blocked = self - .session - .blocking_lock() - .as_deref() - .and_then(|engine| engine.session.canonical_live_session()) - .is_some_and(|live| live.projection_blocked()); - if !blocked { - return; - } - if let Err(error) = self - .runtime - .block_on(self.reopen_quarantined_session_runtime()) - { - eprintln!("pa-daemon: canonical runtime reopen refused: {error:#}"); - } - } + /// Authorized cancel-and-replace already aborts and settles at the worker. /// It does not grant permission to reuse the old physical owner. pub(crate) async fn discard_replaced_session_runtime(&self) { let mut completion_state = self.session_build.lock().await; - if let Some(engine) = self.session.lock().await.as_ref() { - engine.session.close_runtime_admission_for_replacement(); - } *completion_state = None; self.clear_retired_runtime().await; } @@ -1256,7 +1206,7 @@ impl AgentSessionEngine { .and_then(std::path::Path::parent) .map(std::path::Path::to_owned) }); - let (session_manager, on_initial_context_selection, canonical_live_owner) = self + let (session_manager, on_initial_context_selection) = self .build_session_mirror(cwd.clone(), session_dir, session_file.clone()) .await?; // Capture after the verified mirror: prompt admission sets its boundary @@ -1336,7 +1286,6 @@ impl AgentSessionEngine { generic_mcp_servers: vec![], allow_recursion: None, session_manager: Some(session_manager), - canonical_live_owner, context_startup_boundary, context_startup_admitted_user_ids, on_initial_context_selection, diff --git a/crates/pa-daemon/src/agent_engine/session_engine_impl.rs b/crates/pa-daemon/src/agent_engine/session_engine_impl.rs index 585b809ca..34a125d6d 100644 --- a/crates/pa-daemon/src/agent_engine/session_engine_impl.rs +++ b/crates/pa-daemon/src/agent_engine/session_engine_impl.rs @@ -11,16 +11,10 @@ use super::{ }; impl SessionEngine for AgentSessionEngine { + fn persists_session_entries(&self) -> bool { true } + /// The worker owns the slot; the engine only writes into it. - fn set_canonical_step_admission( - &self, - admission: Option, - ) { - *self - .canonical_step - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) = admission; - } + /// Withdraw the queued minted goal-context turns so a state change leaves nothing stale to run. fn purge_queued_goal_contexts(&self) { let purge = self @@ -625,9 +619,6 @@ impl SessionEngine for AgentSessionEngine { // Classify the same actual SDK snapshot that owns this transaction. // No SDK slot guard survives into canonical owner I/O. let session = self.session.blocking_lock().clone(); - let canonical_bound = session - .as_ref() - .is_some_and(|engine| engine.session.has_canonical_live_owner()); let run = async { let Some(engine) = session else { anyhow::bail!("session not built"); @@ -644,14 +635,7 @@ impl SessionEngine for AgentSessionEngine { ) .await }; - let result = if canonical_bound { - // Core's inner provider-only race retains the exact ticket until - // the existing async pre-commit abandon has completed. - Ok(self.runtime.block_on(run)) - } else { - self.runtime - .block_on(pa_agent::abort::race_with_abort(run, signal)) - }; + let result = self.runtime.block_on(pa_agent::abort::race_with_abort(run, signal)); let compaction = match result { Ok(Ok(outcome)) => outcome, Ok(Err(error)) => { @@ -693,38 +677,7 @@ impl SessionEngine for AgentSessionEngine { } } - fn canonical_navigation_session( - &self, - ) -> std::pin::Pin< - Box< - dyn std::future::Future< - Output = anyhow::Result< - Option< - std::sync::Arc< - pa_core::session_engine::canonical_live::CanonicalLiveSession, - >, - >, - >, - > + Send - + '_, - >, - > { - Box::pin(async move { - let model = self.session_model()?; - self.ensure_core_session_async(&model).await?; - let live = { - let guard = self.session.lock().await; - guard - .as_deref() - .ok_or_else(|| { - anyhow::anyhow!("session disappeared before canonical navigation") - })? - .session - .canonical_live_session() - }; - Ok(live) - }) - } + fn run_branch_summary( &self, @@ -835,14 +788,7 @@ impl SessionEngine for AgentSessionEngine { let driver = engine.goal_driver.lock().await; driver.ensure_ready()?; } - if engine.session.canonical_live_session().is_some() { - engine.session.rebuild_canonical_branch_context().await?; - } else { - engine - .session - .rebuild_branch_context(branch_entries) - .await?; - } + engine.session.rebuild_branch_context(branch_entries).await?; // The rebuilt context reads the moved branch's own latest goal // entry; the announcement publishes while the driver lock is held. let mut driver = engine.goal_driver.lock().await; @@ -989,16 +935,9 @@ impl SessionEngine for AgentSessionEngine { self.runtime.block_on(async { let core = self.session.lock().await.clone(); if let Some(core) = core { - // Canonical delivery identities are consumed only by their - // matching owner receipt/ACK. Turn settlement cannot erase them. - if !core.session.has_canonical_live_owner() { - core.session - .shared_persistence() - .lock() - .await - .replay_admitted_user_rows(Vec::new()) - .expect("clearing standalone admitted rows cannot fail"); - } + core.session.shared_persistence().lock().await + .replay_admitted_user_rows(Vec::new()) + .expect("clearing admitted rows cannot fail"); } }); } @@ -1456,7 +1395,6 @@ impl SessionEngine for AgentSessionEngine { // a new prompt certifies the owner boundary once and reopens cold // before its accepted row persists. A refused certification keeps the // view, and the prompt refuses exactly once like before. - self.reopen_blocked_canonical_runtime(); // A create-owned warmup may still be adopting/ACKing metadata. Hold // its existing gate through accepted-row persistence, without holding // any SDK/owner/core guard or triggering a truly lazy build. diff --git a/crates/pa-daemon/src/agent_engine/tests/streaming.rs b/crates/pa-daemon/src/agent_engine/tests/streaming.rs index 7d66983e8..f3da60d37 100644 --- a/crates/pa-daemon/src/agent_engine/tests/streaming.rs +++ b/crates/pa-daemon/src/agent_engine/tests/streaming.rs @@ -1115,7 +1115,7 @@ fn live_stream_metadata_snapshot_preserves_provider_terminal_and_next_epoch() { let mut core = self.owner.lock().unwrap(); let store = core.store.as_mut().unwrap(); let id = store - .persist_owner_metadata( + .persist_entry( "session_info", json!({ "name":"metadata during real stream", diff --git a/crates/pa-daemon/src/agent_engine/turn/model.rs b/crates/pa-daemon/src/agent_engine/turn/model.rs index c9b745a3e..6c54e4a1e 100644 --- a/crates/pa-daemon/src/agent_engine/turn/model.rs +++ b/crates/pa-daemon/src/agent_engine/turn/model.rs @@ -180,10 +180,7 @@ impl AgentSessionEngine { // A bound owner receipt must never wait under Agent::event_delivery. // Its existing restore callback runs after run_turn_once has settled, // before the retry driver publishes restoration or starts another attempt. - let canonical_bound = core - .as_ref() - .is_some_and(|core| core.session.has_canonical_live_owner()); - let restore_session: pa_core::session_engine::canonical_live::CanonicalAfterAssistantCommitFn = { + let restore_session: std::sync::Arc std::pin::Pin> + Send>> + Send + Sync> = { let primary_state = primary_state.clone(); let pending_model_changes = pending_model_changes.clone(); let agent_weak = std::sync::Arc::downgrade(&agent); @@ -257,22 +254,7 @@ impl AgentSessionEngine { }) }) }; - let restore_commit_guard = match core - .as_ref() - .and_then(|core| core.session.canonical_live_session()) - { - Some(live) => match live.register_after_assistant_commit(restore_session.clone()) { - Ok(guard) => Some(guard), - Err(error) => { - return TurnResult::Error { - error: error.to_string(), - assistant: None, - }; - } - }, - None => None, - }; - let restore_subscription = (!canonical_bound).then(|| { + let restore_subscription = Some({ self.runtime.block_on(async { let restore_session = restore_session.clone(); agent @@ -650,7 +632,6 @@ impl AgentSessionEngine { // Also restore the previous override when an attempt/listener failed // before the retry driver's normal restoration path. agent.set_model_override(episode_model_override.clone()); - drop(restore_commit_guard); if let Some(subscription) = restore_subscription { self.runtime.block_on(subscription.unsubscribe()); } @@ -697,7 +678,7 @@ async fn persist_model_change( provider: &str, model_id: &str, ) -> anyhow::Result<()> { - pa_core::session::canonical::persist_metadata_entry( + pa_core::session::metadata::persist_metadata_entry( session, pa_types::session::FileEntry::ModelChange { base: pa_types::session::EntryBase { diff --git a/crates/pa-daemon/src/agent_engine/turn/quota.rs b/crates/pa-daemon/src/agent_engine/turn/quota.rs index 3d2917de9..f8a8a04f5 100644 --- a/crates/pa-daemon/src/agent_engine/turn/quota.rs +++ b/crates/pa-daemon/src/agent_engine/turn/quota.rs @@ -312,7 +312,7 @@ impl AgentSessionEngine { // The resume path's live clear still stands (the quota IS back), // so a failed write surfaces as a log; the restart's arms own // the recovery. No SDK guard crosses the owner receipt await. - if let Err(write_error) = pa_core::session::canonical::persist_metadata_entry( + if let Err(write_error) = pa_core::session::metadata::persist_metadata_entry( &persistence, quota_metadata_entry( pa_core::session_engine::provider_park::PROVIDER_QUOTA_RESUME_ENTRY, @@ -361,7 +361,7 @@ impl AgentSessionEngine { let Some(persistence) = persistence else { return Ok(()); }; - pa_core::session::canonical::persist_metadata_entry( + pa_core::session::metadata::persist_metadata_entry( &persistence, quota_metadata_entry( pa_core::session_engine::provider_park::PROVIDER_QUOTA_PARK_ENTRY, diff --git a/crates/pa-daemon/src/agent_engine/turn/run_once.rs b/crates/pa-daemon/src/agent_engine/turn/run_once.rs index ba9caf365..a952cf081 100644 --- a/crates/pa-daemon/src/agent_engine/turn/run_once.rs +++ b/crates/pa-daemon/src/agent_engine/turn/run_once.rs @@ -81,26 +81,6 @@ impl AgentSessionEngine { aborted: &dyn Fn() -> bool, emit: &mut dyn FnMut(EngineEvent) -> bool, ) -> anyhow::Result { - // Live canonical sessions admit each ACTUAL model callback in Core, - // not this whole prompt attempt. Keep legacy standalone admission only. - let live_canonical = self - .session - .lock() - .await - .as_ref() - .is_some_and(|engine| engine.session.has_canonical_live_owner()); - if !live_canonical { - if let Some(admission) = self - .canonical_step - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .clone() - { - admission.admit(std::sync::Arc::new( - crate::session_store::canonical_adapter::ProviderStepIdentity, - ))?; - } - } let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::(); // AgentStart is an awaited listener call after the run owns its abort // controller. Hold it until this driver transfers the delivery's cancel diff --git a/crates/pa-daemon/src/branch_navigation.rs b/crates/pa-daemon/src/branch_navigation.rs index 4a924d0b4..e129125ec 100644 --- a/crates/pa-daemon/src/branch_navigation.rs +++ b/crates/pa-daemon/src/branch_navigation.rs @@ -14,8 +14,6 @@ use crate::session_store::{SessionEntry, SessionFile}; use crate::session_tree; use crate::worker::{SessionCore, Worker}; use pa_agent::abort::AbortController; -use pa_core::session::canonical::{CanonicalEntryDraft, CanonicalMutationKind}; -use pa_core::session_engine::canonical_live::CanonicalLiveSession; use pa_types::session::{EntryBase, FileEntry, LabelEntry}; /// One completed abandoned-branch summary to persist: the text, its usage block, its @@ -189,21 +187,6 @@ impl TreeNavigation { // A navigation interrupts the running turn first (TS // `acquireQueuedWorkPause` + `waitForIdle`). self.wait_turn_end().await; - match self.engine.canonical_navigation_session().await { - Ok(Some(live)) => { - return match self.navigate_canonical(payload, live).await { - Ok(data) => response_success(None, "navigate_tree", Some(data)), - Err(error) => { - response_failure(None, "navigate_tree", &format!("{error:#}"), None) - } - }; - } - Ok(None) => {} - Err(error) => { - return response_failure(None, "navigate_tree", &format!("{error:#}"), None); - } - } - let (new_leaf, editor_text) = navigation_point(&target); // The abandoned-branch summary (TS `generateBranchSummary`). @@ -324,195 +307,7 @@ impl TreeNavigation { /// Canonical navigation certifies the old source before paid work, closes /// that operation, then adopts one ordered branch successor before any summary. - async fn navigate_canonical( - &self, - payload: &Value, - live: Arc, - ) -> anyhow::Result { - let target_id = payload - .get("targetId") - .and_then(Value::as_str) - .unwrap_or_default() - .to_owned(); - let summarize = payload.get("summarize").and_then(Value::as_bool) == Some(true); - let custom_instructions = payload - .get("customInstructions") - .and_then(Value::as_str) - .map(str::to_owned); - let replace_instructions = - payload.get("replaceInstructions").and_then(Value::as_bool) == Some(true); - let label = payload - .get("label") - .and_then(Value::as_str) - .map(str::to_owned); - let ticket = live - .prepare_mutation(CanonicalMutationKind::ContextPolicy) - .await?; - let expected = ticket.source_cursor().clone(); - let computation: anyhow::Result = async { - let core = self.core.clone(); - let expected_snapshot = expected.clone(); - let target_snapshot = target_id.clone(); - let (target, old_leaf, file_entries) = tokio::task::spawn_blocking(move || -> anyhow::Result<_> { - let mut core = core.lock().map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; - let store = core.store.as_mut().ok_or_else(|| anyhow::anyhow!("Session is still initializing"))?; - anyhow::ensure!(store.attach_canonical_owner()? == expected_snapshot, "navigation source changed before policy"); - let lease = store.lease.clone().ok_or_else(|| anyhow::anyhow!("canonical navigation requires actual session lease"))?; - lease.with_owned_mutation(&store.path, || { - // Recapture only the exact FULL source certified by prepare. - // The loaded index can contain usage overlays or omit rows. - let raw = crate::session_store::canonical_raw::RawCapture::capture( - &store.path, - crate::session_store::canonical_raw::RawPosition { - owner: expected_snapshot.owner.clone(), - canonical_revision: expected_snapshot.canonical_revision, - branch_revision: expected_snapshot.branch_revision, - selected_leaf: expected_snapshot.selected_leaf.clone(), - }, - crate::session_store::canonical_raw::CaptureMode::Full, - )?; - anyhow::ensure!(raw.cursor == expected_snapshot, "certified navigation source changed before policy"); - let target = raw.retained.iter().find(|row| row.entry().id() == Some(target_snapshot.as_str())) - .ok_or_else(|| anyhow::anyhow!("Entry {target_snapshot} not found"))?; - let target = serde_json::from_slice::(target.raw())?; - let old_leaf = raw.cursor.selected_leaf.clone(); - let entries = raw.retained.iter().map(|row| row.entry().clone()).collect::>(); - Ok((target, old_leaf, entries)) - }) - }).await??; - if old_leaf.as_deref() == Some(target_id.as_str()) { - return Ok(NavigationWork::Noop); - } - let (new_leaf, editor_text) = navigation_point(&target); - let mut summary = None; - if summarize { - let collected = pa_core::session_engine::branch_summarization::collect_entries_for_branch_summary( - &file_entries, old_leaf.as_deref(), &target_id, - ); - if !collected.entries.is_empty() { - match self.run_branch_summary_once(collected.entries, custom_instructions, replace_instructions).await { - crate::engine::BranchSummaryOutcome::Complete { run } => { - summary = Some(Box::new((run.summary, run.usage, run.details, run.model))); - } - crate::engine::BranchSummaryOutcome::Aborted => return Ok(NavigationWork::Aborted), - crate::engine::BranchSummaryOutcome::Failed { error } => anyhow::bail!(error), - } - } - } - Ok(NavigationWork::Prepared { new_leaf, editor_text, summary }) - }.await; - // Explicitly abandon only the old prepared operation. Paid output is - // retained below; an error never reruns the callback against new history. - ticket.abandon().await?; - let (new_leaf, editor_text, summary) = match computation? { - NavigationWork::Noop => return Ok(json!({"cancelled":false})), - NavigationWork::Aborted => return Ok(json!({"cancelled":true,"aborted":true})), - NavigationWork::Prepared { - new_leaf, - editor_text, - summary, - } => (new_leaf, editor_text, summary), - }; - // Validate the retained output before changing the actual branch. - let summary_intent = summary - .as_deref() - .map(|(summary, usage, details, model)| { - session_tree::branch_summary_intent( - new_leaf.as_deref(), - summary, - details.clone(), - usage.clone(), - model - .as_ref() - .map(|(provider, model)| (provider.as_str(), model.as_str())), - ) - }) - .transpose()?; - let core = self.core.clone(); - let before_navigation = expected.clone(); - let selected = new_leaf.clone(); - let prepared = tokio::task::spawn_blocking(move || { - let mut core = core - .lock() - .map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; - let store = core - .store - .as_mut() - .ok_or_else(|| anyhow::anyhow!("Session is still initializing"))?; - store - .prepare_canonical_navigation(&before_navigation, selected.as_deref()) - .map_err(anyhow::Error::from) - }) - .await??; - let navigated_source = prepared.checkpoint.cursor.clone(); - live.adopt_navigation(prepared, expected).await?; - let summarized = summary_intent.is_some(); - let summary_id = if let Some(entry) = summary_intent { - let ticket = live - .prepare_mutation(CanonicalMutationKind::ContextPolicy) - .await?; - if ticket.source_cursor() != &navigated_source { - ticket.abandon().await?; - anyhow::bail!("navigation source changed before retained summary commit"); - } - let draft = CanonicalEntryDraft::new(CanonicalMutationKind::ContextPolicy, entry)?; - Some(ticket.commit(draft).await?) - } else { - None - }; - if let Some(label) = label { - live.append( - CanonicalMutationKind::OwnerMetadata, - FileEntry::Label { - base: EntryBase { - id: None, - parent_id: None, - timestamp: None, - rest: serde_json::Map::default(), - }, - payload: LabelEntry { - target_id: summary_id.clone().unwrap_or(target_id), - label: Some(label), - }, - }, - ) - .await?; - } - let (branch_entries, summary_entry) = { - let core = self - .core - .lock() - .map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; - let store = core - .store - .as_ref() - .ok_or_else(|| anyhow::anyhow!("Session is still initializing"))?; - let summary_entry = match summary_id.as_ref() { - Some(id) => Some(session_tree::entry_json(store.entry(id).ok_or_else( - || anyhow::anyhow!("acknowledged branch summary is missing"), - )?)), - None => None, - }; - (store.branch_file_entries(), summary_entry) - }; - let goal_reload = if summarized { - pa_core::session_engine::goal_driver::GoalBranchReload::SameTimeline - } else { - pa_core::session_engine::goal_driver::GoalBranchReload::FaithfulBranch - }; - rebuild_engine_context(&self.engine, branch_entries, goal_reload) - .await - .map_err(anyhow::Error::msg)?; - let mut data = json!({"cancelled":false}); - if let Some(text) = editor_text { - data["editorText"] = json!(text); - } - if let Some(entry) = summary_entry { - data["summaryEntry"] = entry; - } - Ok(data) - } /// One paid callback. Navigation retains the result across its owner /// boundary and never calls this again when source validation fails. @@ -678,11 +473,6 @@ impl TreeNavigation { .await; let previous = { let mut core = self.core.lock().unwrap(); - if let Some(previous) = core.store.as_mut() { - previous - .invalidate_canonical_owner() - .map_err(|error| error.to_string())?; - } core.store.replace(forked) }; // The old store's lease release flushes the window and info diff --git a/crates/pa-daemon/src/branch_navigation/tests.rs b/crates/pa-daemon/src/branch_navigation/tests.rs index f8388223d..8e95b01cb 100644 --- a/crates/pa-daemon/src/branch_navigation/tests.rs +++ b/crates/pa-daemon/src/branch_navigation/tests.rs @@ -54,7 +54,7 @@ impl SessionEngine for CountingEngine { .store .as_mut() .unwrap() - .persist_owner_metadata( + .persist_entry( "custom", json!({"customType":"concurrent-owner-metadata","data":{"exact":"AA=="}}), ) diff --git a/crates/pa-daemon/src/compaction.rs b/crates/pa-daemon/src/compaction.rs index 112d29f57..fbbbb658d 100644 --- a/crates/pa-daemon/src/compaction.rs +++ b/crates/pa-daemon/src/compaction.rs @@ -194,7 +194,7 @@ impl CompactionManager { let Some(store) = core.store.as_mut() else { return; }; - if store.is_canonical_bound() { + if self.engine.persists_session_entries() { // The engine committed the exact archive row and ACKed its raw receipt. return; } @@ -260,7 +260,6 @@ impl CompactionManager { if let Some(store) = core .store .as_mut() - .filter(|store| !store.is_canonical_bound()) { let _ = store.persist_entry( "custom_message", diff --git a/crates/pa-daemon/src/engine.rs b/crates/pa-daemon/src/engine.rs index 46bcbbd25..98b6f65aa 100644 --- a/crates/pa-daemon/src/engine.rs +++ b/crates/pa-daemon/src/engine.rs @@ -12,17 +12,6 @@ use pa_core::session_engine::provider_retry::{ProviderRetryPolicy, UNBOUNDED_BAC use pa_core::session_engine::side_question::{SideQuestionSink, SideQuestionTurn}; use serde_json::{json, Value}; -type CanonicalNavigationFuture<'a> = std::pin::Pin< - Box< - dyn std::future::Future< - Output = Result< - Option>, - >, - > + Send - + 'a, - >, ->; - mod wire; pub use scripted::ScriptedEngine; pub(crate) use wire::session_wire_value; @@ -49,6 +38,9 @@ pub struct PluginCommandOutcome { /// The turn behavior a worker session runs. pub trait SessionEngine: Send + Sync { + /// Native engines commit complete rows through their manager's durable sink. + fn persists_session_entries(&self) -> bool { false } + /// Execute a native plugin or management command without submitting a model prompt. /// A supplied target must match the current generation. Engines without a plugin /// host reject explicitly rather than treating the input as ordinary conversation. @@ -67,11 +59,7 @@ pub trait SessionEngine: Send + Sync { /// Engines that admit provider steps override this; the default is a no-op so /// an engine with no canonical path (the scripted engine) is unchanged rather /// than forced to carry a slot it would never read. - fn set_canonical_step_admission( - &self, - _admission: Option, - ) { - } + /// The session's shared MCP manager, when the engine owns one: the /// `replace_acp_mcp_servers` command writes through it. @@ -185,9 +173,7 @@ pub trait SessionEngine: Send + Sync { /// Obtain the actual live canonical session for a navigation transaction. /// Standalone engines return `None`; native engines build before policy /// preflight and release their session guard before returning the handle. - fn canonical_navigation_session(&self) -> CanonicalNavigationFuture<'_> { - Box::pin(async { Ok(None) }) - } + /// Run one branch summary (`navigate_tree` with `summarize`): summarize /// the abandoned branch's entries; the engine owns the model call. diff --git a/crates/pa-daemon/src/model_switch.rs b/crates/pa-daemon/src/model_switch.rs index 3c80ab7b7..4d59ee733 100644 --- a/crates/pa-daemon/src/model_switch.rs +++ b/crates/pa-daemon/src/model_switch.rs @@ -97,7 +97,6 @@ impl Worker { if let Some(store) = core .store .as_mut() - .filter(|store| !store.is_canonical_bound()) { // The live Core switch already adopted and ACKed its real row. // TS `appendModelChange` records every switch, even to @@ -207,7 +206,6 @@ impl Worker { if let Some(store) = core .store .as_mut() - .filter(|store| !store.is_canonical_bound()) { // A bound live Core switch has already committed this intent. if store diff --git a/crates/pa-daemon/src/plugin_commands.rs b/crates/pa-daemon/src/plugin_commands.rs index e51eea918..2a21dcd1e 100644 --- a/crates/pa-daemon/src/plugin_commands.rs +++ b/crates/pa-daemon/src/plugin_commands.rs @@ -218,7 +218,7 @@ impl Worker { .as_mut() .context("Session is still initializing") .and_then(|store| { - if store.is_canonical_bound() { + if self.engine.persists_session_entries() { return Ok(()); } store.persist_entry("custom_message", json!({ diff --git a/crates/pa-daemon/src/registry.rs b/crates/pa-daemon/src/registry.rs index a1a3ac396..c016da603 100644 --- a/crates/pa-daemon/src/registry.rs +++ b/crates/pa-daemon/src/registry.rs @@ -63,27 +63,7 @@ impl WorkerReply { /// A worker's canonical owner refuses every write while an adoption is /// unacknowledged; the refusal is a "not yet" the route may re-ask. - pub(crate) fn refused_by_adoption_conflict(&self) -> bool { - const CONFLICT: &str = "AdoptionConflict"; - match self { - WorkerReply::Typed(response) => { - !response.success - && response - .error - .as_deref() - .is_some_and(|error| error.starts_with(CONFLICT)) - } - // The header's success hint is optional: a worker that omits it still - // relays its refusal in the payload. - WorkerReply::Relayed(relay) => { - relay.success != Some(true) - && relay - .payload - .windows(CONFLICT.len()) - .any(|window| window == CONFLICT.as_bytes()) - } - } - } + pub(crate) fn relayed_active_session_id(&self) -> Option<&str> { match self { diff --git a/crates/pa-daemon/src/session_navigation.rs b/crates/pa-daemon/src/session_navigation.rs index 6a00d8931..421b4e0c3 100644 --- a/crates/pa-daemon/src/session_navigation.rs +++ b/crates/pa-daemon/src/session_navigation.rs @@ -47,11 +47,6 @@ impl SessionNavigation { .await; let previous = { let mut core = self.core.lock().unwrap(); - if let Some(previous) = core.store.as_mut() { - previous - .invalidate_canonical_owner() - .map_err(|error| error.to_string())?; - } core.store.replace(file) }; // The old store's lease release flushes the window and info diff --git a/crates/pa-daemon/src/session_store.rs b/crates/pa-daemon/src/session_store.rs index be8bc0e0b..1e7880cc9 100644 --- a/crates/pa-daemon/src/session_store.rs +++ b/crates/pa-daemon/src/session_store.rs @@ -28,19 +28,7 @@ mod stream_tests; mod window_tests; // The index concern lives in session_store::index; the facade re-imports keep the callers in scope. -pub(crate) mod canonical_adapter; -mod canonical_io; -#[cfg(test)] -mod canonical_io_seam_tests; -#[cfg(test)] -mod canonical_live_tests; -#[cfg(test)] -mod canonical_row_grant_tests; -mod canonical_write_seam; mod child_usage; -#[cfg(test)] -#[path = "session_store/child_usage_tests.rs"] -mod child_usage_tests; mod context_selection; mod index; // The log-transported half of the store (port-plan §3): the write arm the @@ -152,7 +140,6 @@ pub struct SessionFile { pub(crate) leaf_id: Option, pub(crate) window: Option, pub(crate) lease: Option>, - child_usage_recovery: child_usage::ChildUsageRecovery, /// Rows the full reader skipped as unparsable (the TS loader's /// lenient skip): zero on a healthy file. A reopen that found damage /// logs this count (silent skips are how a torn session degraded @@ -167,16 +154,6 @@ pub struct SessionFile { /// serve it through `get_state` /// (`SessionSummary::anthropic_warning_shown`). pub(crate) anthropic_warning_shown: bool, - /// The live canonical epoch for this session, when one has been attached at - /// serving capture. `None` in the pre-genesis window and whenever no epoch - /// exists yet; the canonical record of that boundary lives in the adapter. - pub(crate) epoch: Option, - /// Rows this store has persisted while no canonical epoch was attached. - /// - /// Named for the fact, never for the policy: it stays true if the policy - /// changes. A turn that writes content must leave this at zero, so a path - /// that writes a real row without an epoch moves the count instead of hiding. - pub(crate) rows_written_without_epoch: u64, /// THE transport switch's live half: `Some` serves this store's durable /// rows through the session-log core (see `pa_daemon::session_transport` /// for the switch and its resolution rule). `None` keeps the pre-port diff --git a/crates/pa-daemon/src/session_store/canonical_adapter.rs b/crates/pa-daemon/src/session_store/canonical_adapter.rs deleted file mode 100644 index 3386a3917..000000000 --- a/crates/pa-daemon/src/session_store/canonical_adapter.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! Per-row epoch admission, provider-step ownership, and poisoned-read checks. - -use std::sync::Arc; - -use pa_core::session::canonical::{ - CanonicalEntryDraft, CanonicalFailureKind, CanonicalIoStage, CanonicalMutationKind, - CanonicalRequestedWrite, CanonicalSourceDisposition, CanonicalStoreError, -}; -use pa_core::session_engine::canonical_coordinator::{ - CanonicalAppendGrant, CanonicalEpoch, CanonicalProviderStep, -}; -use pa_types::session::FileEntry; - -/// The typed refusal for a read served under a poisoned epoch. -/// -/// A poisoned epoch means an uncertain write: the raw tail may be partially -/// present, so nothing may be read as if its bytes were classified. `NeedsReconcile` -/// is the honest disposition - the source is neither `Unchanged` nor -/// `PendingAdoption`, and the caller must reconcile before serving again. -pub(crate) fn refused_poisoned_read() -> CanonicalStoreError { - CanonicalStoreError { - kind: CanonicalFailureKind::InvalidRequest, - requested: CanonicalRequestedWrite::NotWritten, - source: CanonicalSourceDisposition::NeedsReconcile, - detail: "canonical epoch is poisoned by an uncertain write".to_owned(), - } -} - -/// Whether a live epoch is poisoned, as a read path may observe it. -pub(crate) fn epoch_is_poisoned(epoch: Option<&EpochHandle>) -> bool { - epoch.is_some_and(|handle| handle.0.poisoned()) -} - -/// One provider request attempt's identity, created where the attempt starts. -/// -/// The authority treats a step as opaque pointer identity and refuses the same -/// allocation twice, so this object IS the daemon's actual step record for the -/// attempt: one per attempt, never reused across a retry or a failover. -pub struct ProviderStepIdentity; - -/// The worker's shared channel for the live provider step. -/// -/// Division of ownership, which is the whole reason this type exists: -/// -/// * the WORKER owns it and creates the slot with its session core; -/// * the per-turn engine method (`run_turn_once`) is the only writer, and it -/// admits one step per provider attempt, before the prompt is issued; -/// * `worker/turn.rs` is the only reader, at the assistant row it persists. -/// -/// The subscriber closure inside `run_turn_once` captures NO slot: a clone there -/// would be a second owner, and a slot that is cloned per task and never read is a -/// silent uncovered path. A step admitted and never written leaves an unconsumed -/// seal and no flight, so it is harmless rather than a leak. -#[derive(Clone)] -pub struct CanonicalStepAdmission { - epoch: Option>, - slot: Arc>>, -} - -impl CanonicalStepAdmission { - pub(crate) fn new( - epoch: Option>, - slot: Arc>>, - ) -> Self { - Self { epoch, slot } - } - - /// Admit ONE actual provider step for this attempt. - /// - /// The engine must call this before the prompt is issued; a missing epoch is - /// not an error (the turn runs uncovered and the counter moves), but an - /// admission refusal under a live epoch IS one. - pub(crate) fn admit(&self, step: Arc) -> anyhow::Result<()> { - let Some(epoch) = self.epoch.as_ref() else { - return Ok(()); - }; - let admitted = epoch.admit_provider_step(step)?; - let mut slot = self - .slot - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - // One attempt, one step: a leftover handle would let a later row borrow an - // earlier step, so the newer admission replaces it and the old seal stays - // unconsumed. - *slot = Some(admitted); - Ok(()) - } -} - -/// Debug-able handle: `CanonicalEpoch` implements no `Debug`, and `SessionFile` -/// derives it, so the live epoch travels wrapped. -#[derive(Clone)] -pub(crate) struct EpochHandle(pub(crate) Arc); - -impl std::fmt::Debug for EpochHandle { - fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - formatter.write_str("EpochHandle()") - } -} - -/// Choose the canonical admission for one row this store is about to write. -/// -/// Three cases, and each one is named rather than assumed: -/// -/// * A **user** row takes a prepare-phase flight. [`CanonicalEntryDraft::new`] -/// refuses a user row ("user rows require exact owner admission/replay") and no -/// final admission exists without a draft, so a prepare flight is the only -/// admission it can take. That flight binds no draft: the epoch vouches for the -/// WRITE's completion, not its payload, which is acceptable because the session -/// owner admitted that content when it accepted the turn. -/// * A **context-policy** row (compaction) takes a requested-mutation grant with a -/// draft derived from the row. `CanonicalEpoch::begin_append` admits through the -/// context-policy scope, so this is the one draft kind it accepts. -/// * Everything else is **declared outside and counted**, with the reason stated: -/// an assistant or tool row can only be admitted through `ProviderStep:: -/// seal_assistant` - kind `AdmittedTerminal`, sealed against an admitted provider -/// step, once per step - and metadata only through one of the seven exact -/// lifecycle actions. The daemon's generic row loop cannot take a grant for those -/// without faking one, so it does not, and the counter states the fact instead. -pub(super) fn grant_for_row( - epoch: &CanonicalEpoch, - entry: &super::SessionEntry, - step: Option<&CanonicalProviderStep>, -) -> anyhow::Result> { - if is_user_row(entry) { - return epoch - .begin_prepare_append(CanonicalIoStage::PreparePreflight) - .map(Some) - .map_err(anyhow::Error::from); - } - // The native final output: an assistant row is admitted by the provider step - // that produced it, never by a draft-only generic grant. A missing step under a - // live epoch means the engine did not admit where we thought it did, so the row - // is REFUSED - writing it uncovered would make the drain claim a lie. - if is_assistant_row(entry) { - let Some(step) = step else { - anyhow::bail!( - "engine admitted no provider step for this assistant row: refusing to write it without an epoch grant" - ); - }; - let draft = draft_for_row(entry)?; - return epoch - .begin_terminal_append(step, &draft, CanonicalIoStage::RequestedMutation) - .map(Some) - .map_err(anyhow::Error::from); - } - if matches!(entry.type_.as_str(), "compaction" | "branch_summary") { - let draft = draft_for_row(entry)?; - return epoch - .begin_append(CanonicalIoStage::RequestedMutation, &draft) - .map(Some) - .map_err(anyhow::Error::from); - } - // LAST arm on purpose: a row type nobody has thought about is COUNTED rather - // than silently admitted, which is what makes the counter a boundary instead of - // a list of known rows. - Ok(None) -} - -/// Whether this row is an assistant message: the native final output, covered only -/// by an admitted provider step's seal. -pub(super) fn is_assistant_row(entry: &super::SessionEntry) -> bool { - entry.type_ == "message" - && entry.fields.get("message").and_then(|m| m.get("role")) - == Some(&serde_json::Value::String("assistant".to_owned())) -} - -/// Whether this row is the turn's user prompt, which no draft may carry. -pub(super) fn is_user_row(entry: &super::SessionEntry) -> bool { - entry.type_ == "message" - && entry.fields.get("message").and_then(|m| m.get("role")) - == Some(&serde_json::Value::String("user".to_owned())) -} - -/// Build the draft for a row that is a requested mutation. -/// -/// The envelope identity is stripped before the draft is built: the draft rejects -/// a supplied id, parent or timestamp, and the store mints those at write time. -pub(super) fn draft_for_row(entry: &super::SessionEntry) -> anyhow::Result { - let mut value = serde_json::to_value(entry)?; - if let Some(object) = value.as_object_mut() { - for key in ["id", "parentId", "timestamp"] { - object.remove(key); - } - } - let file_entry: FileEntry = serde_json::from_value(value)?; - // Verified at HEAD, not taken on report: `Purpose::ContextPolicy::validate_draft` - // accepts `Compaction | BranchSummary`, and `MetadataAction::matches` has no - // branch-summary arm, so a branch summary is context policy and nothing else. - let kind = match entry.type_.as_str() { - // LIVE as of the terminal wiring: an assistant row reaches this arm through - // `begin_terminal_append`, which seals it to the admitted provider step. A - // user row never gets here (it takes a prepare flight, because no draft may - // carry a user row), and a tool result stays counted until a tool seal path - // exists. Test: `an_assistant_row_takes_the_provider_step_grant`. - "message" => CanonicalMutationKind::AdmittedTerminal, - "compaction" | "branch_summary" => CanonicalMutationKind::ContextPolicy, - _ => CanonicalMutationKind::OwnerMetadata, - }; - CanonicalEntryDraft::new(kind, file_entry) -} diff --git a/crates/pa-daemon/src/session_store/canonical_live.rs b/crates/pa-daemon/src/session_store/canonical_live.rs deleted file mode 100644 index f9470e816..000000000 --- a/crates/pa-daemon/src/session_store/canonical_live.rs +++ /dev/null @@ -1,2358 +0,0 @@ -//! Live physical owner of the canonical journal. No model callbacks or async locks. -//! -//! The state is attached once at the actual lease boundary, shared by snapshots, -//! and retained until exact Core adoption is acknowledged. A lost receipt never -//! turns into permission to write or project another source. -use std::collections::{HashMap, HashSet, VecDeque}; -use std::sync::{Arc, Mutex, Weak}; - -use pa_core::session::canonical::{ - CanonicalAckPhase, CanonicalAcknowledgement, CanonicalAdmissionPolicy, CanonicalAppendReceipt, - CanonicalCallerGuard, CanonicalCallerNotifier, CanonicalCallerSignal, - CanonicalCommitDisposition, CanonicalCursor, CanonicalFailureKind, CanonicalIoStage, - CanonicalOperation, CanonicalPrepareMode, CanonicalPrepared, CanonicalRequestedWrite, - CanonicalSourceDisposition, CanonicalStoreError, -}; -use pa_core::session_engine::canonical_coordinator::{ - CanonicalAdmittedTool, CanonicalAppendGrant, CanonicalOwnerEpoch, CanonicalOwnerRun, - CanonicalProviderStep, MetadataAction, -}; -use pa_core::session_engine::canonical_live::{ - CanonicalLiveAcknowledged, CanonicalLiveAppendRequest, CanonicalLiveCustomPrepared, - CanonicalLiveCustomRequest, CanonicalLivePrepareRequest, CanonicalLiveProviderStepRequest, - CanonicalLiveRun, CanonicalLiveRunRequest, CanonicalLiveSnapshot, CanonicalLiveToolRequest, - CanonicalLiveUserPrepared, CanonicalLiveUserRequest, -}; - -use super::canonical_raw::{CaptureMode, RawCapture, RawPosition}; -use super::canonical_revision::{BarrierTicket, CanonicalRevision}; -use super::{SessionEntry, SessionFile}; - -#[derive(Clone, Copy)] -enum NavigationPreparation { - MoveOnly, - PrepareCheckpoint, -} - -#[derive(Clone, Copy)] -enum OwnerDelivery { - User, - Custom, - Metadata, -} - -type Result = std::result::Result; - -fn failure(detail: impl std::fmt::Display) -> CanonicalStoreError { - CanonicalStoreError { - kind: CanonicalFailureKind::InvalidRequest, - requested: CanonicalRequestedWrite::NotWritten, - source: CanonicalSourceDisposition::Unchanged, - detail: detail.to_string(), - } -} - -fn unresolved(detail: impl std::fmt::Display) -> CanonicalStoreError { - unresolved_with(detail, None) -} - -/// One refusal whose outcome is unresolved. `reason` overrides the fixed-detail -/// lookup with the caller's own fixed source-check label. -fn unresolved_with( - detail: impl std::fmt::Display, - reason: Option<&'static str>, -) -> CanonicalStoreError { - let detail = detail.to_string(); - // Report only fixed source-check labels, never the dynamic error payload. - let reason = reason.unwrap_or_else(|| label_of(&detail)); - eprintln!("pa-daemon: canonical refusal phase=owner reason={reason}"); - CanonicalStoreError { - kind: CanonicalFailureKind::AdoptionConflict, - requested: CanonicalRequestedWrite::Unknown, - source: CanonicalSourceDisposition::NeedsReconcile, - detail, - } -} - -fn label_of(detail: &str) -> &'static str { - match detail { - "canonical owner adoption abandoned" => "canonical_owner_abandoned_pending_adoption", - "canonical owner invalidated" => "canonical_owner_invalidated", - "canonical owner authority poisoned" => "canonical_owner_authority_poisoned", - "canonical owner awaiting adoption acknowledgement" => "canonical_owner_pending_adoption", - "canonical owner caller abandoned before acknowledgement" => { - "canonical_owner_abandoned_caller" - } - "canonical lease expired" => "canonical_lease_expired", - "canonical owner path/header/lease changed" => "canonical_owner_path_header_lease_changed", - "canonical serving epoch is poisoned" => "canonical_serving_epoch_is_poisoned", - "canonical owner mutex poisoned" => "canonical_owner_mutex_poisoned", - "canonical owner has prepared mutations" => "canonical_owner_prepared_mutations", - "canonical owner has admitted tools" => "canonical_owner_admitted_tools", - "canonical serving run is live" => "canonical_serving_run_live", - "canonical run has no terminal ACK" => "canonical_run_terminal_unacknowledged", - "canonical child usage I/O is unresolved" => "canonical_child_usage_unresolved", - "canonical caller has not completed" => "canonical_caller_incomplete", - "actual tool executions still lack terminal adoption" => { - "actual_tool_executions_still_lack_terminal_adoption" - } - "serving epoch still borrowed at ordered boundary" => { - "serving_epoch_still_borrowed_at_ordered_boundary" - } - "prepared mutation lost caller binding" => "prepared_mutation_lost_caller_binding", - "commit pending state lost" => "commit_pending_state_lost", - "staged delivery has no FIFO head" => "staged_delivery_has_no_fifo_head", - "staged delivery FIFO changed" => "staged_delivery_fifo_changed", - "user pending state lost" => "user_pending_state_lost", - "staged operation lost" => "staged_operation_lost", - "staged delivery lost" => "staged_delivery_lost", - "delivered Custom FIFO row missing from physical source" => { - "delivered_custom_fifo_row_missing_from_physical_source" - } - "delivered Custom FIFO changed during preparation" => { - "delivered_custom_fifo_changed_during_preparation" - } - detail if detail.starts_with("revision: ") => "revision", - detail if detail.starts_with("invalidate: ") => "invalidate", - detail if detail.starts_with("navigation prepare barrier: ") => { - "navigation_prepare_barrier" - } - detail if detail.starts_with("ordered source boundary: ") => "ordered_source_boundary", - detail if detail.starts_with("ACK barrier: ") => "ack_barrier", - detail if detail.starts_with("commit admission: ") => "commit_admission", - detail if detail.starts_with("commit start: ") => "commit_start", - detail if detail.starts_with("commit finish: ") => "commit_finish", - detail if detail.starts_with("user admission: ") => "user_admission", - detail if detail.starts_with("user write start: ") => "user_write_start", - detail if detail.starts_with("user finish: ") => "user_finish", - detail if detail.starts_with("delivery admission: ") => "delivery_admission", - detail if detail.starts_with("delivery write start: ") => "delivery_write_start", - detail if detail.starts_with("delivery finish: ") => "delivery_finish", - "canonical source advanced outside owner" => "unowned_source_advance", - "canonical source changed outside owner" => "unowned_source_change", - "canonical Custom source changed outside owner" => "unowned_custom_source_change", - "canonical Custom admission source CAS conflict" => "custom_admission_source_changed", - "canonical user admission source CAS conflict" => "user_admission_source_changed", - "canonical prepare source/branch CAS conflict" => "prepare_source_branch_changed", - "canonical Custom write lacks exact single physical suffix" => { - "custom_write_physical_suffix" - } - "delivered Custom FIFO head differs from exact accepted intent" => { - "delivered_custom_intent_mismatch" - } - detail if detail.starts_with("Custom write admission: ") => "custom_write_admission", - detail if detail.starts_with("Custom write start: ") => "custom_write_start", - detail if detail.starts_with("Custom write finish: ") => "custom_write_finish", - detail if detail.starts_with("Custom preflight: ") => "custom_preflight", - detail if detail.starts_with("Custom delivery barrier: ") => "custom_delivery_barrier", - detail if detail.starts_with("Custom ledger: ") => "custom_ledger", - "actual AgentStart belongs to another certified source" => "actual_run_source_mismatch", - "actual AgentStart source changed outside owner" => "actual_run_external_source_change", - "actual run was already registered" => "actual_run_duplicate_registration", - "previous actual run has no acknowledged native terminal row" => { - "actual_run_terminal_unacknowledged" - } - _ => "unclassified_owner_error", - } -} - -struct Notify; -impl CanonicalCallerNotifier for Notify { - fn changed(&self) {} -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum PendingProvenance { - UnwrittenObservation, - Retained, -} - -struct Pending { - provenance: PendingProvenance, - operation: CanonicalOperation, - cursor: CanonicalCursor, - barrier: BarrierTicket, - prepared: bool, - mutation: bool, - signal: Option, - staged: bool, - from: CanonicalCursor, - opening_run: Option>, -} - -struct RegisteredRun { - identity: Arc, - abort: Option, - ever_provider_admitted: bool, - terminal_acked: bool, -} - -struct PreparedBinding { - cursor: CanonicalCursor, - signal: CanonicalCallerSignal, -} - -struct State { - revision: CanonicalRevision, - authority: CanonicalOwnerEpoch, - provider: Option, - serving_run: Option>, - registered_run: Option, - tools: HashMap, - lease: Weak, - raw: RawCapture, - pending: Option, - prepared: HashMap, - delivered: VecDeque, - delivered_custom: VecDeque, - registered: HashSet, - admitted: HashSet, - blocked: bool, - #[cfg(test)] - fail_stage_capture: bool, - callers: Vec, -} - -/// Clone-shared ownership state. Read-only opens never create this allocation. -#[derive(Clone)] -pub(crate) struct LiveCanonicalOwner(Arc>); -impl std::fmt::Debug for LiveCanonicalOwner { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("LiveCanonicalOwner()") - } -} - -impl LiveCanonicalOwner { - pub(super) fn invalidate(&self) { - match self.0.lock() { - Ok(mut state) => { - state.blocked = true; - if let Err(error) = state.revision.invalidate() { - eprintln!("pa-daemon: canonical revision invalidation failed: {error:?}"); - } - } - Err(_) => eprintln!("pa-daemon: canonical owner mutex poisoned at invalidation"), - } - } -} - -/// Why a refused request is not a certified unstarted prior source. Each variant -/// is a fixed log label: the refusal must name the actual cause, never the -/// generic unclassified fallback. -#[derive(Clone, Copy)] -enum UnstartedRefusal { - Unavailable, - PreparedWork, - OwnerScope, - Session, - Path, - Header, - Branch, - Revision, -} - -impl UnstartedRefusal { - fn label(self) -> &'static str { - match self { - Self::Unavailable => "refused_unstarted_owner_unavailable", - Self::PreparedWork => "refused_unstarted_prepared_work", - Self::OwnerScope => "refused_unstarted_owner_scope_changed", - Self::Session => "refused_unstarted_session_changed", - Self::Path => "refused_unstarted_path_changed", - Self::Header => "refused_unstarted_header_changed", - Self::Branch => "refused_unstarted_branch_changed", - Self::Revision => "refused_unstarted_revision_ahead", - } - } -} - -impl State { - // Called only inside the verified lease mutation, before this request creates - // an operation. A stale SDK source is not permission to ignore existing debt. - fn refuse_unstarted( - &self, - expected: &pa_core::session::canonical::CanonicalCursor, - detail: &str, - ) -> CanonicalStoreError { - match self.certify_unstarted(expected) { - Ok(()) => CanonicalStoreError { - kind: CanonicalFailureKind::SourceChanged, - requested: CanonicalRequestedWrite::NotRequested, - source: CanonicalSourceDisposition::ReadyForObservation, - detail: detail.to_owned(), - }, - Err(refusal) => unresolved_with(detail, Some(refusal.label())), - } - } - - /// Certify that `expected` is a previous source of this same owned journal and - /// that this request wrote nothing, so Core can re-observe and retry. - /// - /// Only the refusal's own debt matters: an unacknowledged write - /// (`available`), or a prepared mutation whose adoption Core never - /// committed. Unrelated work that is still settling — an admitted tool - /// awaiting its terminal, a live run, an unfinished caller — does not make - /// this unwritten refusal uncertain, because it is reached before any - /// operation, barrier or write exists. - fn certify_unstarted( - &self, - expected: &CanonicalCursor, - ) -> std::result::Result<(), UnstartedRefusal> { - self.available() - .map_err(|_| UnstartedRefusal::Unavailable)?; - if !self.prepared.is_empty() { - return Err(UnstartedRefusal::PreparedWork); - } - let raw = &self.raw.cursor; - if expected.owner != raw.owner { - return Err(UnstartedRefusal::OwnerScope); - } - if expected.session_id != raw.session_id { - return Err(UnstartedRefusal::Session); - } - if expected.source_path != raw.source_path { - return Err(UnstartedRefusal::Path); - } - if expected.header_certificate != raw.header_certificate { - return Err(UnstartedRefusal::Header); - } - if expected.branch_revision != raw.branch_revision { - return Err(UnstartedRefusal::Branch); - } - if expected.canonical_revision > raw.canonical_revision { - return Err(UnstartedRefusal::Revision); - } - Ok(()) - } - - /// The strict run-opening form: no adoption, mutation, tool or caller debt may - /// remain. An `AgentStart` may only open on such a fully settled source. - fn certifies_unstarted_source(&self, expected: &CanonicalCursor) -> bool { - self.certify_unstarted(expected).is_ok() - && self.tools.is_empty() - && self.callers.iter().all(|signal| { - signal.state() == pa_core::session::canonical::CanonicalCallerState::Completed - }) - } - - fn terminal_metadata_suffix( - &self, - expected: &CanonicalCursor, - ) -> Result> { - let rows = self.raw.certified_suffix_since(expected).map_err(failure)?; - let mut leaf = expected.selected_leaf.as_deref(); - for row in &rows { - if !matches!( - row.entry(), - pa_types::session::FileEntry::ChildUsageAttributed { .. } - ) || row.entry().parent_id() != leaf - { - return Err(failure( - "native terminal source advanced beyond owner metadata", - )); - } - leaf = row.entry().id(); - } - if leaf != self.raw.cursor.selected_leaf.as_deref() { - return Err(failure( - "native terminal metadata changed selected ancestry", - )); - } - Ok(rows) - } - - fn position(&self, leaf: Option) -> Result { - let revision = self - .revision - .snapshot() - .map_err(|e| unresolved(format!("revision: {e:?}")))?; - Ok(RawPosition { - owner: revision.owner, - canonical_revision: revision.canonical_revision, - branch_revision: revision.branch_revision, - selected_leaf: leaf, - }) - } - - fn available(&self) -> Result<()> { - if self.blocked { - return Err(unresolved("canonical owner invalidated")); - } - if self.authority.poisoned() { - return Err(unresolved("canonical owner authority poisoned")); - } - let abandoned = self - .callers - .iter() - .chain( - self.pending - .iter() - .filter_map(|pending| pending.signal.as_ref()), - ) - .chain(self.prepared.values().map(|binding| &binding.signal)) - .any(|signal| { - signal.state() == pa_core::session::canonical::CanonicalCallerState::Abandoned - }); - match (self.pending.is_some(), abandoned) { - (true, true) => return Err(unresolved("canonical owner adoption abandoned")), - (true, false) => { - return Err(unresolved( - "canonical owner awaiting adoption acknowledgement", - )); - } - (false, true) => { - return Err(unresolved( - "canonical owner caller abandoned before acknowledgement", - )); - } - (false, false) => {} - } - self.revision - .snapshot() - .map_err(|e| unresolved(format!("revision: {e:?}")))?; - Ok(()) - } - - fn verify(&self, file: &SessionFile) -> Result> { - let lease = self - .lease - .upgrade() - .ok_or_else(|| unresolved("canonical lease expired"))?; - if !file - .lease - .as_ref() - .is_some_and(|current| Arc::ptr_eq(current, &lease)) - || file.path != self.raw.cursor.source_path - || file.header.id != self.raw.cursor.session_id - || !matches!(self.raw.header.entry(), pa_types::session::FileEntry::Header { header } if header == &file.header) - { - self.revision - .invalidate() - .map_err(|e| unresolved(format!("invalidate: {e:?}")))?; - return Err(unresolved("canonical owner path/header/lease changed")); - } - if super::canonical_adapter::epoch_is_poisoned(file.epoch.as_ref()) { - return Err(unresolved("canonical serving epoch is poisoned")); - } - Ok(lease) - } - - fn current(&self, file: &SessionFile) -> Result { - RawCapture::capture( - &file.path, - self.position(file.leaf_id.clone())?, - CaptureMode::Full, - ) - .map_err(failure) - } - - fn safe_visible_leaf(&self) -> anyhow::Result> { - let selected: HashMap<_, _> = self - .raw - .plan - .ancestry - .iter() - .filter_map(|index| { - self.raw.plan.rows[*index] - .id - .as_ref() - .map(|id| (id.as_str(), *index)) - }) - .collect(); - let mut queued = HashSet::new(); - for id in &self.delivered { - let index = *selected.get(id.as_str()).ok_or_else(|| { - failure("pending User delivery is not on certified selected branch") - })?; - anyhow::ensure!( - queued.insert(id.as_str()), - "duplicate pending delivery identity" - ); - anyhow::ensure!( - matches!( - self.raw.retained[index].entry(), - pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::User(_), - .. - } - ), - "pending User ledger has another canonical kind" - ); - } - for id in &self.delivered_custom { - let index = *selected.get(id.as_str()).ok_or_else(|| { - failure("pending Custom delivery is not on certified selected branch") - })?; - anyhow::ensure!( - queued.insert(id.as_str()), - "duplicate pending delivery identity" - ); - anyhow::ensure!( - matches!( - self.raw.retained[index].entry(), - pa_types::session::FileEntry::CustomMessage { .. } - ), - "pending Custom ledger has another canonical kind" - ); - } - let visible_leaf = self - .raw - .plan - .ancestry - .iter() - .find_map(|index| { - let row = &self.raw.plan.rows[*index]; - row.id - .as_deref() - .filter(|id| queued.contains(id)) - .map(|_| row.parent_id.clone()) - }) - .unwrap_or_else(|| self.raw.cursor.selected_leaf.clone()); - Ok(visible_leaf) - } - - fn approve_admission(&self, policy: &CanonicalAdmissionPolicy) -> Result<()> { - match policy { - CanonicalAdmissionPolicy::Preserve => Ok(()), - CanonicalAdmissionPolicy::Append(ids) => { - let mut seen = HashSet::new(); - for id in ids { - if !self.registered.contains(id) - || self.admitted.contains(id) - || !seen.insert(id) - { - return Err(failure( - "user admission lacks exact owner delivery or repeats a delivery", - )); - } - } - Ok(()) - } - CanonicalAdmissionPolicy::Replace(ids) => { - if !self.admitted.is_empty() || ids.iter().any(|id| !self.registered.contains(id)) { - return Err(failure( - "replay reset is not the initial owner-delivered binding", - )); - } - Ok(()) - } - } - } -} - -impl SessionFile { - /// Attach at the actual writer/lease boundary, never on an SDK cache reload. - pub(crate) fn attach_canonical_owner(&mut self) -> Result { - if let Some(owner) = self.canonical_live.clone() { - let state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - return lease - .with_owned_mutation(&self.path, || { - let current = state.current(self)?; - anyhow::ensure!( - current.cursor == state.raw.cursor, - "canonical source advanced outside owner" - ); - Ok(current.cursor) - }) - .map_err(unresolved); - } - if self.child_usage_recovery.pending.is_some() { - return Err(unresolved("canonical owner has unresolved child usage I/O")); - } - let lease = self - .lease - .clone() - .ok_or_else(|| failure("live canonical owner requires session lease"))?; - let revision = CanonicalRevision::new(); - let snapshot = revision - .snapshot() - .map_err(|e| failure(format!("revision: {e:?}")))?; - let position = RawPosition { - owner: snapshot.owner, - canonical_revision: snapshot.canonical_revision, - branch_revision: snapshot.branch_revision, - selected_leaf: self.leaf_id.clone(), - }; - let raw = lease - .with_owned_mutation(&self.path, || { - let raw = RawCapture::capture(&self.path, position, CaptureMode::Full)?; - anyhow::ensure!( - raw.cursor.session_id == self.header.id, - "canonical header mismatch" - ); - let pa_types::session::FileEntry::Header { header } = raw.header.entry() else { - anyhow::bail!("canonical source lacks header"); - }; - anyhow::ensure!(header == &self.header, "canonical owner header differs"); - Ok(raw) - }) - .map_err(failure)?; - let cursor = raw.cursor.clone(); - self.canonical_live = Some(LiveCanonicalOwner(Arc::new(Mutex::new(State { - authority: CanonicalOwnerEpoch::genesis(&cursor), - provider: None, - serving_run: None, - registered_run: None, - tools: HashMap::new(), - revision, - lease: Arc::downgrade(&lease), - raw, - pending: None, - prepared: HashMap::new(), - delivered: self.canonical_delivered.clone(), - delivered_custom: self.canonical_delivered_custom.clone(), - registered: self.canonical_delivered.iter().cloned().collect(), - admitted: HashSet::new(), - blocked: false, - #[cfg(test)] - fail_stage_capture: false, - callers: Vec::new(), - })))); - Ok(cursor) - } - - /// Read-only certification. No operation, caller, barrier or admission is created. - pub(crate) fn canonical_snapshot(&self) -> Result { - let owner = self - .canonical_live - .as_ref() - .ok_or_else(|| failure("canonical owner not attached"))?; - let state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path, || { - let current = state.current(self)?; - anyhow::ensure!( - current.cursor == state.raw.cursor, - "canonical snapshot source changed outside owner" - ); - Ok(CanonicalLiveSnapshot { - checkpoint: current - .checkpoint(pa_core::session::canonical::CanonicalSourceRequest::Full)?, - admitted_users: state.admitted.clone(), - pending_users: state.delivered.clone(), - pending_custom: state.delivered_custom.clone(), - safe_visible_leaf: state.safe_visible_leaf()?, - }) - }) - .map_err(failure) - } - - /// The worker calls this only for an actually accepted durable User row. - pub(crate) fn register_delivered_user(&mut self, id: &str) -> Result<()> { - let row = self - .entry(id) - .ok_or_else(|| failure("delivered user is not durable owner row"))?; - if !super::canonical_adapter::is_user_row(row) { - return Err(failure("delivered row is not User")); - } - if let Some(owner) = &self.canonical_live { - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - if state.registered.contains(id) { - return Err(failure("user delivery already registered")); - } - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path, || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "user registration source changed outside owner" - ); - state - .revision - .advance_owner_state(None) - .map_err(|e| failure(format!("delivery registration: {e:?}")))?; - let position = state.position(self.leaf_id.clone())?; - state.raw.refresh_owner_position(&position); - state.registered.insert(id.to_owned()); - state.delivered.push_back(id.to_owned()); - Ok(()) - }) - .map_err(failure)?; - } else { - if self.canonical_delivered.iter().any(|prior| prior == id) { - return Err(failure("user delivery already registered")); - } - self.canonical_delivered.push_back(id.to_owned()); - } - Ok(()) - } - - /// Permanently retire a replaced actual owner. Never resets poison or pending I/O. - pub(crate) fn invalidate_canonical_owner(&mut self) -> Result<()> { - let Some(owner) = self.canonical_live.clone() else { - return Ok(()); - }; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "replacement source changed" - ); - self.retire_canonical_source(&mut state)?; - state.blocked = true; - state - .revision - .invalidate() - .map_err(|e| unresolved(format!("invalidate: {e:?}")))?; - Ok(()) - }) - .map_err(unresolved) - } - - /// Retire only a proven completed runtime, preserving exact undelivered FIFOs. - pub(crate) fn retire_completed_canonical_runtime( - &mut self, - expected: &CanonicalCursor, - ) -> Result<()> { - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor - && expected == &state.raw.cursor, - "completed retirement source changed" - ); - anyhow::ensure!( - self.child_usage_recovery.pending.is_none(), - "child usage I/O remains unresolved" - ); - anyhow::ensure!( - state.prepared.is_empty() && state.tools.is_empty(), - "actual effects remain unfinished" - ); - anyhow::ensure!( - state - .registered_run - .as_ref() - .is_none_or(|run| run.terminal_acked), - "actual run has no terminal ACK" - ); - // The local Core queue was checked by the retiring view. Owner membership - // is historical admission, never a substitute execution queue. - self.retire_canonical_source(&mut state)?; - state.blocked = true; - state - .revision - .invalidate() - .map_err(|e| unresolved(format!("invalidate: {e:?}")))?; - self.canonical_delivered.clone_from(&state.delivered); - self.canonical_delivered_custom - .clone_from(&state.delivered_custom); - self.canonical_live = None; - Ok(()) - }) - .map_err(unresolved) - } - - /// Atomically navigate the actual owner, including branch ABA with no new bytes. - /// The caller must rebuild its SDK mirror/binding before the next projection. - pub(crate) fn advance_canonical_branch(&mut self, selected_leaf: Option<&str>) -> Result<()> { - self.navigate_canonical_source( - selected_leaf, - /* expected */ None, - NavigationPreparation::MoveOnly, - )?; - Ok(()) - } - - /// Strict old-source CAS, ordered branch successor and new pending checkpoint - /// are one lease-fenced operation. Core must explicitly rebind then ACK it. - pub(crate) fn prepare_canonical_navigation( - &mut self, - expected: &CanonicalCursor, - selected_leaf: Option<&str>, - ) -> Result { - if self.canonical_live.is_none() { - self.attach_canonical_owner()?; - } - self.navigate_canonical_source( - selected_leaf, - Some(expected), - NavigationPreparation::PrepareCheckpoint, - )? - .ok_or_else(|| failure("canonical navigation requires actual leased owner")) - } - - fn navigate_canonical_source( - &mut self, - selected_leaf: Option<&str>, - expected: Option<&CanonicalCursor>, - preparation: NavigationPreparation, - ) -> Result> { - if self.canonical_live.is_none() { - self.leaf_id = selected_leaf.map(str::to_owned); - return Ok(None); - } - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "navigation source changed" - ); - if let Some(expected) = expected { - anyhow::ensure!( - expected == &state.raw.cursor, - "navigation source/branch/frontier/leaf CAS conflict" - ); - } - anyhow::ensure!( - state.delivered.is_empty() && state.delivered_custom.is_empty(), - "navigation has unconsumed owner-delivered rows" - ); - // Validate membership before closing an otherwise healthy source. - RawCapture::capture( - &self.path, - state.position(selected_leaf.map(str::to_owned))?, - CaptureMode::Full, - )?; - let retired = self.retire_canonical_source(&mut state)?; - state - .revision - .advance_branch() - .map_err(|e| failure(format!("navigation: {e:?}")))?; - let raw = RawCapture::capture( - &self.path, - state.position(selected_leaf.map(str::to_owned))?, - CaptureMode::Full, - )?; - anyhow::ensure!( - raw.cursor.raw_source_generation == state.raw.cursor.raw_source_generation - && raw.cursor.frontier == state.raw.cursor.frontier - && raw.cursor.header_certificate == state.raw.cursor.header_certificate, - "navigation changed physical journal" - ); - state.authority = - CanonicalOwnerEpoch::successor_after_navigation(&raw.cursor, retired)?; - state.raw = raw; - state.prepared.clear(); - state.admitted.clear(); - state.serving_run = None; - self.leaf_id = selected_leaf.map(str::to_owned); - match preparation { - NavigationPreparation::MoveOnly => Ok(None), - NavigationPreparation::PrepareCheckpoint => { - let checkpoint = state.raw.checkpoint( - pa_core::session::canonical::CanonicalSourceRequest::Full, - )?; - let operation = CanonicalOperation::new(); - let barrier = state.revision.set_barrier(&operation).map_err(|e| { - unresolved(format!("navigation prepare barrier: {e:?}")) - })?; - let (caller, signal) = CanonicalCallerGuard::new(Arc::new(Notify)); - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: operation.clone(), - cursor: checkpoint.cursor.clone(), - barrier, - prepared: true, - mutation: false, - signal: Some(signal), - staged: false, - from: checkpoint.cursor.clone(), - opening_run: None, - }); - Ok(Some(CanonicalPrepared { - caller, - operation, - checkpoint, - })) - } - } - }) - .map_err(unresolved) - } - - fn retire_canonical_source( - &mut self, - state: &mut State, - ) -> Result { - if !state.tools.is_empty() { - return Err(unresolved( - "actual tool executions still lack terminal adoption", - )); - } - if let Some(run) = state.registered_run.as_mut() { - if let Some(abort) = run.abort.take() { - abort.finish_ordered_boundary(); - } - } - let closing = state - .authority - .request_close_at_current_source(&state.raw.cursor)?; - if let Some(handle) = self.epoch.take() { - let epoch = match Arc::try_unwrap(handle.0) { - Ok(epoch) => epoch, - Err(epoch) => { - self.epoch = Some(super::canonical_adapter::EpochHandle(epoch)); - state.blocked = true; - return Err(unresolved( - "serving epoch still borrowed at ordered boundary", - )); - } - }; - state.provider = None; - epoch.finish_ordered_boundary(); - } - closing - .finish() - .map_err(|e| unresolved(format!("ordered source boundary: {e:?}"))) - } - - pub(crate) fn prepare_canonical( - &mut self, - request: CanonicalLivePrepareRequest, - ) -> Result { - if self.canonical_live.is_none() { - self.attach_canonical_owner()?; - } - self.confirm_child_usage_write().map_err(failure)?; - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path, || { - let raw = state.current(self)?; - anyhow::ensure!( - raw.cursor == state.raw.cursor, - "canonical source changed outside owner" - ); - if let Some(expected) = request.expected.as_ref().filter(|expected| { - *expected != &raw.cursor - && !(request.mode == CanonicalPrepareMode::Mutation(pa_core::session::canonical::CanonicalMutationKind::AdmittedTerminal) - && state.terminal_metadata_suffix(expected).is_ok()) - }) { - return Err(state.refuse_unstarted(expected, "canonical prepare source/branch CAS conflict").into()); - } - state.approve_admission(&request.admission)?; - raw.checkpoint(request.source)?; - if matches!(&request.admission, CanonicalAdmissionPolicy::Append(ids) | CanonicalAdmissionPolicy::Replace(ids) if !ids.is_empty()) { - state.revision.check_owner_state_advance() - .map_err(|e| failure(format!("prepare preflight: {e:?}")))?; - } - let operation = CanonicalOperation::new(); - let barrier = state.revision.set_barrier(&operation) - .map_err(|e| failure(format!("prepare barrier: {e:?}")))?; - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: operation.clone(), cursor: raw.cursor.clone(), barrier: barrier.clone(), - prepared: true, mutation: matches!(request.mode, CanonicalPrepareMode::Mutation(_)), - signal: None, staged: false, from: raw.cursor, - opening_run: None, - }); - // A nonempty explicit bootstrap admission is a guarded mutation. - let ids = match &request.admission { - CanonicalAdmissionPolicy::Append(ids) | CanonicalAdmissionPolicy::Replace(ids) => ids.clone(), - CanonicalAdmissionPolicy::Preserve => Vec::new(), - }; - if !ids.is_empty() { - state.revision.advance_owner_state(Some(&operation)) - .map_err(|e| failure(format!("bootstrap admission: {e:?}")))?; - let position = state.position(self.leaf_id.clone())?; - state.raw.refresh_owner_position(&position); - } - let checkpoint = state.raw.checkpoint(request.source)?; - let from = checkpoint.cursor.clone(); - let (caller, signal) = CanonicalCallerGuard::new(Arc::new(Notify)); - let provenance = if matches!(request.mode, CanonicalPrepareMode::Observe) - && match &request.admission { - CanonicalAdmissionPolicy::Preserve => true, - CanonicalAdmissionPolicy::Append(ids) - | CanonicalAdmissionPolicy::Replace(ids) => ids.is_empty(), - } { - PendingProvenance::UnwrittenObservation - } else { - PendingProvenance::Retained - }; - let ids = match request.admission { - CanonicalAdmissionPolicy::Append(ids) - | CanonicalAdmissionPolicy::Replace(ids) => ids, - CanonicalAdmissionPolicy::Preserve => Vec::new(), - }; - state.admitted.extend(ids); - state.pending = Some(Pending { - provenance, - operation: operation.clone(), - cursor: checkpoint.cursor.clone(), - barrier, - prepared: true, - mutation: matches!(request.mode, CanonicalPrepareMode::Mutation(_)), - signal: Some(signal), - staged: false, - from, - opening_run: None, - }); - Ok(CanonicalPrepared { - caller, - operation, - checkpoint, - }) - }) - .map_err(|error| match error.downcast::() { - Ok(error) => error, - Err(error) => failure(error), - }) - } - - pub(crate) fn acknowledge_canonical( - &mut self, - request: CanonicalAcknowledgement, - ) -> Result { - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - if request.phase == CanonicalAckPhase::AbandonPreparedOnly - && state.pending.is_none() - { - state.available()?; - let prepared = state - .prepared - .get(&request.operation) - .ok_or_else(|| failure("no matching prepared-only canonical operation"))?; - anyhow::ensure!( - prepared.cursor == request.cursor, - "prepared-only abandon cursor mismatch" - ); - anyhow::ensure!( - prepared.signal.state() - != pa_core::session::canonical::CanonicalCallerState::Abandoned, - "prepared-only caller already abandoned" - ); - // The binding proves this operation has not entered I/O: - // a write would retain pending adoption until its ACK removes - // the binding. Other certified owner deliveries may advance - // the source while the caller does work. Abandon only the old - // preparation, without adopting or rebasing onto those rows. - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "prepared-only source changed outside owner; no requested row written" - ); - let visible_leaf = state.safe_visible_leaf()?; - state.prepared.remove(&request.operation); - return Ok(CanonicalLiveAcknowledged { - operation: request.operation, - cursor: request.cursor, - visible_leaf, - }); - } - let pending = state - .pending - .as_ref() - .ok_or_else(|| failure("no canonical adoption pending"))?; - anyhow::ensure!( - pending.operation == request.operation && pending.cursor == request.cursor, - "canonical ACK operation/cursor mismatch" - ); - if request.phase == CanonicalAckPhase::AbandonPreparedOnly { - anyhow::ensure!( - pending.provenance == PendingProvenance::UnwrittenObservation - && pending.prepared && !pending.mutation && !pending.staged - && pending.signal.as_ref().is_some_and(|signal| signal.state() - != pa_core::session::canonical::CanonicalCallerState::PreparedAcknowledged), - "pending operation is not a proven unwritten observation" - ); - anyhow::ensure!( - !state.blocked && !state.authority.poisoned() - && !state.callers.iter().any(|signal| signal.state() - == pa_core::session::canonical::CanonicalCallerState::Abandoned) - && !state.prepared.values().any(|binding| binding.signal.state() - == pa_core::session::canonical::CanonicalCallerState::Abandoned), - "another canonical operation remains unresolved" - ); - anyhow::ensure!( - state.raw.cursor == request.cursor - && pending.from == request.cursor - && state.current(self)?.cursor == request.cursor, - "unwritten observation source changed" - ); - let visible_leaf = state.safe_visible_leaf()?; - state.revision.clear_barrier(&request.operation, &pending.barrier) - .map_err(|e| unresolved(format!("ACK barrier: {e:?}")))?; - state.pending.take(); - return Ok(CanonicalLiveAcknowledged { - operation: request.operation, - cursor: request.cursor, - visible_leaf, - }); - } - let phase_matches = match request.phase { - CanonicalAckPhase::Prepared => pending.prepared, - CanonicalAckPhase::AppliedReady => !pending.prepared, - CanonicalAckPhase::ReconciledBlocked | CanonicalAckPhase::Quarantined - | CanonicalAckPhase::AbandonPreparedOnly => false, - }; - anyhow::ensure!( - phase_matches, - "canonical ACK cannot reopen blocked or wrong-stage source" - ); - if request.phase != CanonicalAckPhase::AbandonPreparedOnly { - let certificate = request.adoption.as_ref().ok_or_else(|| { - failure("canonical ACK requires Core adoption certificate") - })?; - anyhow::ensure!( - certificate.operation() == &request.operation - && certificate.cursor() == &request.cursor, - "canonical adoption certificate mismatch" - ); - } - let current = state.current(self)?; - anyhow::ensure!( - current.cursor == pending.cursor, - "canonical source changed before ACK" - ); - if let Some(signal) = &pending.signal { - anyhow::ensure!( - signal.state() - != pa_core::session::canonical::CanonicalCallerState::Abandoned, - "canonical caller abandoned" - ); - } - let visible_leaf = state.safe_visible_leaf()?; - anyhow::ensure!( - !state.callers.iter().any(|signal| signal.state() - == pa_core::session::canonical::CanonicalCallerState::Abandoned), - "canonical operation caller abandoned before ACK" - ); - let opening_run = pending.opening_run.clone(); - let barrier = pending.barrier.clone(); - if let Some(identity) = opening_run { - let retired = self.retire_canonical_source(&mut state)?; - state.authority = CanonicalOwnerEpoch::successor(&state.raw.cursor, retired)?; - let abort = state.authority.register_run(identity.clone())?; - state.registered_run = Some(RegisteredRun { - identity, - abort: Some(abort), - ever_provider_admitted: false, - terminal_acked: false, - }); - state.provider = None; - state.serving_run = None; - } - state - .revision - .clear_barrier(&request.operation, &barrier) - .map_err(|e| unresolved(format!("ACK barrier: {e:?}")))?; - let pending = state - .pending - .take() - .ok_or_else(|| failure("pending receipt disappeared"))?; - if !pending.prepared { - if matches!( - state - .raw - .retained - .last() - .map(pa_core::session::canonical::CanonicalJournalRow::entry), - Some(pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::Assistant(_), - .. - }) - ) { - if let Some(run) = state.registered_run.as_mut() { - run.terminal_acked = true; - } - } - let completed_tool = - state.raw.retained.last().and_then(|row| match row.entry() { - pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::ToolResult(result), - .. - } => Some(result.tool_call_id.clone()), - _ => None, - }); - if let Some(id) = completed_tool { - state.tools.remove(&id); - } - } - if let Some(signal) = pending.signal.clone() { - state.callers.retain(|prior| { - prior.state() - != pa_core::session::canonical::CanonicalCallerState::Completed - }); - state.callers.push(signal); - } - if pending.prepared - && pending.mutation - && request.phase == CanonicalAckPhase::Prepared - { - let signal = pending - .signal - .ok_or_else(|| unresolved("prepared mutation lost caller binding"))?; - state.prepared.insert( - pending.operation, - PreparedBinding { - cursor: pending.cursor, - signal, - }, - ); - } else { - state.prepared.remove(&request.operation); - } - Ok(CanonicalLiveAcknowledged { - operation: request.operation, - cursor: request.cursor, - visible_leaf, - }) - }) - .map_err(unresolved) - } -} - -impl SessionFile { - pub(crate) fn append_canonical( - &mut self, - request: CanonicalLiveAppendRequest, - ) -> Result { - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - if state - .prepared - .get(&request.operation) - .map(|prepared| &prepared.cursor) - != Some(&request.expected) - { - return Err(failure( - "canonical append has no matching prepared operation", - )); - } - if matches!( - request.draft.entry(), - pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::Assistant(_) - | pa_types::session::AgentMessage::ToolResult(_), - .. - } - ) { - let actual_run = request - .run - .as_ref() - .ok_or_else(|| failure("native terminal request has no actual run provenance"))?; - if !state - .registered_run - .as_ref() - .is_some_and(|run| Arc::ptr_eq(&run.identity, actual_run)) - { - return Err(failure( - "native terminal request belongs to another actual run", - )); - } - } - let mut value = serde_json::to_value(request.draft.entry()).map_err(failure)?; - let object = value - .as_object_mut() - .ok_or_else(|| failure("canonical draft is not an object"))?; - let entry_type = object - .remove("type") - .and_then(|v| v.as_str().map(str::to_owned)) - .ok_or_else(|| failure("canonical draft lacks type"))?; - for key in ["id", "parentId", "timestamp"] { - object.remove(key); - } - let entry = SessionEntry { - type_: entry_type, - id: if self.window.is_some() { - uuid::Uuid::new_v4().to_string() - } else { - super::new_entry_id(&self.by_id) - }, - parent_id: self.leaf_id.clone(), - timestamp: crate::util::now_iso(), - fields: value, - }; - let mut io_started = false; - lease - .with_owned_mutation(&self.path.clone(), || { - let current = state.current(self)?; - anyhow::ensure!( - current.cursor == state.raw.cursor, - "canonical append source changed outside owner; requested row not written" - ); - let mut metadata_suffix = Vec::new(); - if current.cursor != request.expected { - if request.draft.kind() - == pa_core::session::canonical::CanonicalMutationKind::AdmittedTerminal - { - metadata_suffix = state.terminal_metadata_suffix(&request.expected)?; - } else { - return Err(CanonicalStoreError { - kind: CanonicalFailureKind::SourceChanged, - requested: CanonicalRequestedWrite::NotWritten, - source: CanonicalSourceDisposition::Unchanged, - detail: - "canonical append source advanced; prepared operation not written" - .into(), - } - .into()); - } - } - // Validate sparse support before any requested mutation, never after writing. - current.checkpoint(request.source)?; - let mut admit = || -> anyhow::Result<_> { - // A selected context capability must remain current through the - // actual RequestedMutation grant/enter_io and physical admission. - let grant = state.grant(self, &request.draft)?; - let barrier = state - .revision - .set_barrier(&request.operation) - .map_err(|e| failure(format!("commit barrier: {e:?}")))?; - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: request.operation.clone(), - cursor: current.cursor.clone(), - barrier, - prepared: false, - mutation: false, - signal: None, - staged: false, - from: current.cursor.clone(), - opening_run: None, - }); - let mut attempt = state - .revision - .admit_write(Some(&request.operation)) - .map_err(|e| unresolved(format!("commit admission: {e:?}")))?; - attempt - .begin_io() - .map_err(|e| unresolved(format!("commit start: {e:?}")))?; - Ok((grant, attempt)) - }; - let (grant, attempt) = match request.context_admission.as_ref() { - Some(capability) => capability.with_admission(&request.draft, admit)?, - None => admit()?, - }; - // Registration/host guards are gone. The admitted writer alone owns - // classification and completion; no plugin lock crosses disk I/O. - io_started = true; - let id = match self.persist_granted_entry(entry, None, Some(grant)) { - Ok(id) => id, - Err(error) => { - state.blocked = true; - return Err(error); - } - }; - attempt - .finish_success() - .map_err(|e| unresolved(format!("commit finish: {e:?}")))?; - let position = state.position(self.leaf_id.clone())?; - let appended = match RawCapture::append_from( - &state.raw, - ¤t.cursor, - position, - CaptureMode::Full, - ) { - Ok(appended) => appended, - Err(error) => { - state.blocked = true; - return Err(error.into()); - } - }; - anyhow::ensure!( - appended.rows.len() == 1 && appended.rows[0].entry().id() == Some(id.as_str()), - "canonical append does not contain exactly the owner's requested row" - ); - let to = appended.capture.cursor.clone(); - state - .pending - .as_mut() - .ok_or_else(|| unresolved("commit pending state lost"))? - .cursor = to.clone(); - state.raw = appended.capture; - metadata_suffix.extend(appended.rows); - Ok(CanonicalCommitDisposition::Committed( - CanonicalAppendReceipt { - operation: request.operation, - from: request.expected, - to, - rows: metadata_suffix, - committed_entry_id: Some(id), - replacement_window: None, - }, - )) - }) - .map_err(|error| { - if io_started { - unresolved(error) - } else { - match error.downcast::() { - Ok(error) => error, - Err(error) => failure(error), - } - } - }) - } - - /// Explicit delivered FIFO replay, or one newly accepted SDK continuation. - pub(crate) fn admit_canonical_user( - &mut self, - request: CanonicalLiveUserRequest, - ) -> Result { - self.attach_canonical_owner()?; - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease.with_owned_mutation(&self.path.clone(), || { - let current = state.current(self)?; - anyhow::ensure!(current.cursor == state.raw.cursor, "canonical source changed outside owner"); - if let Some(expected) = request.expected.as_ref().filter(|expected| *expected != ¤t.cursor) { - return Err(state.refuse_unstarted(expected, "canonical user admission source CAS conflict").into()); - } - current.checkpoint(request.source)?; - if let Some(id) = state.delivered.front() { - validate_user_intent(¤t, id, &request.message)?; - if !state.admitted.contains(id) { - state.approve_admission(&CanonicalAdmissionPolicy::Append(vec![id.clone()]))?; - } - } - state.revision.check_owner_state_advance() - .map_err(|e| failure(format!("user preflight: {e:?}")))?; - let operation = CanonicalOperation::new(); - let barrier = state.revision.set_barrier(&operation) - .map_err(|e| failure(format!("user prepare barrier: {e:?}")))?; - // Arm pending before delivery/write. Losing the reply keeps this barrier. - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: operation.clone(), cursor: current.cursor.clone(), barrier, - prepared: true, mutation: false, signal: None, staged: false, from: current.cursor.clone(), - opening_run: None, - }); - let id = if let Some(id) = state.delivered.front().cloned() { - validate_user_intent(¤t, &id, &request.message)?; - state.revision.advance_owner_state(Some(&operation)) - .map_err(|e| failure(format!("user ledger: {e:?}")))?; - let position = state.position(self.leaf_id.clone())?; - state.raw.refresh_owner_position(&position); - state.delivered.pop_front(); - id - } else { - let entry = SessionEntry { - type_: "message".to_owned(), - id: if self.window.is_some() { uuid::Uuid::new_v4().to_string() } else { super::new_entry_id(&self.by_id) }, - parent_id: self.leaf_id.clone(), - timestamp: crate::util::now_iso(), - fields: serde_json::json!({"message": pa_types::session::AgentMessage::User(request.message)}), - }; - let grant = state.authority.begin_prepare_append(CanonicalIoStage::PreparePreflight)?; - let mut attempt = state.revision.admit_write(Some(&operation)) - .map_err(|e| unresolved(format!("user admission: {e:?}")))?; - attempt.begin_io().map_err(|e| unresolved(format!("user write start: {e:?}")))?; - let id = match self.persist_granted_entry(entry, None, Some(grant)) { - Ok(id) => id, - Err(error) => { state.blocked = true; return Err(error); } - }; - attempt.finish_success().map_err(|e| unresolved(format!("user finish: {e:?}")))?; - let appended = RawCapture::append_from(&state.raw, ¤t.cursor, - state.position(self.leaf_id.clone())?, CaptureMode::Full)?; - anyhow::ensure!(appended.rows.len() == 1 && appended.rows[0].entry().id() == Some(id.as_str()), - "canonical User write lacks exact single physical suffix"); - state.raw = appended.capture; - state.registered.insert(id.clone()); - id - }; - if !state.admitted.contains(&id) { - state.approve_admission(&CanonicalAdmissionPolicy::Append(vec![id.clone()]))?; - } - state.admitted.insert(id.clone()); - let checkpoint = state.raw.checkpoint(request.source)?; - let (caller, signal) = CanonicalCallerGuard::new(Arc::new(Notify)); - let pending = state.pending.as_mut().ok_or_else(|| unresolved("user pending state lost"))?; - pending.cursor = checkpoint.cursor.clone(); - pending.signal = Some(signal); - Ok(CanonicalLiveUserPrepared { - prepared: CanonicalPrepared { caller, operation, checkpoint }, - admitted_entry_id: id, - }) - }).map_err(|error| match error.downcast::() { - Ok(error) => error, - Err(error) => unresolved(error), - }) - } -} - -fn metadata_action(entry: &pa_types::session::FileEntry) -> Option { - use pa_types::session::{AgentMessage, FileEntry}; - match entry { - FileEntry::ModelChange { .. } => Some(MetadataAction::ModelChange), - FileEntry::ThinkingLevelChange { .. } => Some(MetadataAction::ThinkingLevelChange), - FileEntry::ServiceTierChange { .. } => Some(MetadataAction::ServiceTierChange), - FileEntry::SessionInfo { .. } => Some(MetadataAction::SessionInfo), - FileEntry::SessionState { .. } => Some(MetadataAction::SessionState), - FileEntry::GitState { .. } => Some(MetadataAction::GitState), - FileEntry::Label { .. } => Some(MetadataAction::Label), - FileEntry::Custom { .. } => Some(MetadataAction::CustomEntry), - FileEntry::CustomMessage { .. } => Some(MetadataAction::CustomMessage), - FileEntry::ChildUsageAttributed { .. } => Some(MetadataAction::ChildUsageAttributed), - FileEntry::Message { - message: AgentMessage::BashExecution(_), - .. - } => Some(MetadataAction::BashExecution), - FileEntry::Message { .. } - | FileEntry::Header { .. } - | FileEntry::Unknown { .. } - | FileEntry::Compaction { .. } - | FileEntry::BranchSummary { .. } => None, - } -} - -fn metadata_draft( - entry: &SessionEntry, -) -> Result { - let mut value = serde_json::to_value(entry).map_err(failure)?; - let object = value - .as_object_mut() - .ok_or_else(|| failure("metadata row is not an object"))?; - for key in ["id", "parentId", "timestamp"] { - object.remove(key); - } - pa_core::session::canonical::CanonicalEntryDraft::new( - pa_core::session::canonical::CanonicalMutationKind::OwnerMetadata, - serde_json::from_value(value).map_err(failure)?, - ) - .map_err(failure) -} - -impl State { - fn grant( - &self, - file: &SessionFile, - draft: &pa_core::session::canonical::CanonicalEntryDraft, - ) -> Result { - use pa_types::session::{AgentMessage, FileEntry}; - let stage = CanonicalIoStage::RequestedMutation; - let action = metadata_action(draft.entry()); - if let Some(action) = action { - return self.authority.begin_metadata_append(action, draft, stage); - } - if matches!( - draft.entry(), - FileEntry::Compaction { .. } | FileEntry::BranchSummary { .. } - ) { - return self.authority.begin_context_append(draft, stage); - } - if matches!( - draft.entry(), - FileEntry::Message { - message: AgentMessage::Assistant(_), - .. - } - ) { - let run = self - .registered_run - .as_ref() - .ok_or_else(|| failure("assistant has no registered actual AgentStart run"))?; - if !run.ever_provider_admitted { - let abort = run - .abort - .as_ref() - .ok_or_else(|| failure("no-provider abort provenance is unavailable"))?; - return self.authority.begin_no_provider_terminal(abort, draft, stage) - .inspect_err(|_| { - eprintln!("pa-daemon: canonical refusal phase=no_provider_terminal reason=terminal_provenance_refused"); - }); - } - } - let epoch = file - .epoch - .as_ref() - .ok_or_else(|| failure("native append has no actual serving capture"))?; - match draft.entry() { - FileEntry::Message { - message: AgentMessage::Assistant(_), - .. - } => { - let step = self - .provider - .as_ref() - .ok_or_else(|| failure("assistant has no admitted provider invocation"))?; - epoch.0.begin_terminal_append(step, draft, stage) - } - FileEntry::Message { - message: AgentMessage::ToolResult(result), - .. - } => { - let tool = self - .tools - .get(&result.tool_call_id) - .ok_or_else(|| failure("tool result has no admitted execution"))?; - epoch.0.begin_tool_append(tool, draft, stage) - } - FileEntry::Compaction { .. } | FileEntry::BranchSummary { .. } => { - epoch.0.begin_append(stage, draft) - } - FileEntry::Message { .. } - | FileEntry::Header { .. } - | FileEntry::ThinkingLevelChange { .. } - | FileEntry::ServiceTierChange { .. } - | FileEntry::ModelChange { .. } - | FileEntry::Custom { .. } - | FileEntry::ChildUsageAttributed { .. } - | FileEntry::Label { .. } - | FileEntry::SessionInfo { .. } - | FileEntry::SessionState { .. } - | FileEntry::GitState { .. } - | FileEntry::CustomMessage { .. } - | FileEntry::Unknown { .. } => Err(failure("unsupported canonical grant")), - } - } -} - -impl SessionFile { - /// Compare immutable scope identity only. Pending receipts remain ACK-able. - pub(crate) fn has_canonical_owner_scope( - &self, - expected: &pa_core::session::canonical::CanonicalOwnerScope, - ) -> Result { - let Some(owner) = &self.canonical_live else { - return Ok(false); - }; - let state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - // The original scope is stable even when revision/adoption is blocked. - Ok(&state.raw.cursor.owner == expected) - } - - pub(crate) fn is_canonical_bound(&self) -> bool { - self.canonical_live.is_some() - } - - /// A new SDK may bootstrap an attached owner only before its first actual - /// `AgentStart`. Owner membership is not an execution-queue handoff proof. - pub(crate) fn validate_initial_canonical_sdk(&self) -> Result<()> { - let Some(owner) = &self.canonical_live else { - return Ok(()); - }; - let state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - state.verify(self)?; - if state.registered_run.is_some() - || state.serving_run.is_some() - || state.provider.is_some() - || !state.tools.is_empty() - || self.epoch.is_some() - { - return Err(failure( - "fresh SDK under an owner with actual execution has no queue handoff", - )); - } - if !state.admitted.is_empty() { - return Err(failure( - "initial SDK bootstrap already has owner admission debt", - )); - } - Ok(()) - } - - /// Called at the real main stream invocation, before provider I/O. - pub(crate) fn begin_canonical_provider_step( - &mut self, - request: CanonicalLiveProviderStepRequest, - cancelled: bool, - ) -> Result<()> { - self.attach_canonical_owner()?; - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "canonical provider source changed" - ); - if cancelled { - let mut error = state.refuse_unstarted(&request.expected, "new canonical provider work is closing or aborted"); - if error.source == CanonicalSourceDisposition::ReadyForObservation { - error.kind = CanonicalFailureKind::Cancelled; - } - return Err(error.into()); - } - if request.expected != state.raw.cursor { - return Err(state.refuse_unstarted(&request.expected, "actual provider invocation projected another canonical source; refusing without rebase").into()); - } - anyhow::ensure!( - state.tools.is_empty(), - "previous tool executions have no terminal adoption" - ); - let registered = state.registered_run.as_mut().ok_or_else(|| failure("provider invocation has no actual registered run"))?; - anyhow::ensure!(Arc::ptr_eq(®istered.identity, &request.run), "provider invocation belongs to another actual run"); - // Permanent for this actual AgentStart run, including all source - // successors and failed/retired provider captures. Never fall back. - registered.ever_provider_admitted = true; - let retired = self.retire_canonical_source(&mut state)?; - state.authority = CanonicalOwnerEpoch::successor(&state.raw.cursor, retired)?; - let epoch = state - .authority - .capture_serving_run(request.run.clone(), request.model)?; - state.provider = Some(epoch.admit_provider_step(request.invocation)?); - state.serving_run = Some(request.run); - self.epoch = Some(super::canonical_adapter::EpochHandle(Arc::new(epoch))); - Ok(()) - }) - .map_err(|error| match error.downcast::() { - Ok(error) => error, - Err(error) => unresolved(error), - }) - } - - /// Called by the awaited `ToolExecutionStart` boundary, before tool execution. - pub(crate) fn admit_canonical_tool(&mut self, request: CanonicalLiveToolRequest) -> Result<()> { - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path, || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "canonical tool source changed" - ); - anyhow::ensure!( - state - .serving_run - .as_ref() - .is_some_and(|run| Arc::ptr_eq(run, &request.run)), - "tool execution belongs to another actual run" - ); - let id = request.tool.tool_call_id.clone(); - anyhow::ensure!( - !state.tools.contains_key(&id), - "tool execution already admitted" - ); - // Check the actual start record against the certified assistant. The - // authority then pins the same execution allocation through its result. - let assistant = state - .raw - .retained - .iter() - .rev() - .find_map(|row| match row.entry() { - pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::Assistant(message), - .. - } => Some(message), - _ => None, - }) - .ok_or_else(|| failure("tool start has no canonical assistant"))?; - let calls: Vec<_> = assistant - .content - .iter() - .filter_map(|content| match content { - pa_types::ai::AssistantContentBlock::ToolCall(call) if call.id == id => { - Some(call) - } - _ => None, - }) - .collect(); - anyhow::ensure!( - calls.len() == 1 - && calls[0].name == request.tool.tool_name - && request.tool.args.as_object() == Some(&calls[0].arguments), - "actual tool start differs from exact canonical assistant call" - ); - let step = state - .provider - .as_ref() - .ok_or_else(|| failure("tool execution lacks actual provider step"))?; - let tool = step.admit_tool(&id, request.tool)?; - state.tools.insert(id, tool); - Ok(()) - }) - .map_err(failure) - } -} - -fn validate_user_intent( - raw: &RawCapture, - id: &str, - intended: &pa_types::ai::UserMessage, -) -> Result<()> { - let row = raw - .retained - .iter() - .find(|row| row.entry().id() == Some(id)) - .ok_or_else(|| failure("delivered FIFO row missing from physical source"))?; - let pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::User(actual), - .. - } = row.entry() - else { - return Err(failure("delivered FIFO row is not User")); - }; - let mut actual = serde_json::to_value(actual).map_err(failure)?; - let mut intended = serde_json::to_value(intended).map_err(failure)?; - actual - .as_object_mut() - .ok_or_else(|| failure("User payload is not object"))? - .remove("timestamp"); - intended - .as_object_mut() - .ok_or_else(|| failure("User intent is not object"))? - .remove("timestamp"); - if actual != intended { - return Err(failure( - "delivered User FIFO head differs from accepted intent", - )); - } - Ok(()) -} - -impl SessionFile { - /// Durable acceptance before SDK construction or early abort. Bound acceptance - /// leaves an operation-bound staged checkpoint; it never clears its own barrier. - pub(crate) fn persist_delivered_user(&mut self, message: serde_json::Value) -> Result { - if message.get("role").and_then(serde_json::Value::as_str) != Some("user") { - return Err(failure("accepted user payload has another role")); - } - let mut fields = serde_json::Map::new(); - fields.insert("message".to_owned(), message); - let fields = serde_json::Value::Object(fields); - if !self.is_canonical_bound() { - let id = self.persist_entry("message", fields).map_err(failure)?; - self.register_delivered_user(&id)?; - return Ok(id); - } - self.stage_delivered_entry("message", fields) - } - - pub(crate) fn persist_delivered_custom(&mut self, fields: serde_json::Value) -> Result { - if !self.is_canonical_bound() { - let id = self - .persist_entry("custom_message", fields) - .map_err(failure)?; - self.canonical_delivered_custom.push_back(id.clone()); - return Ok(id); - } - self.stage_delivered_entry("custom_message", fields) - } - - fn stage_delivered_entry( - &mut self, - entry_type: &str, - fields: serde_json::Value, - ) -> Result { - let entry = SessionEntry { - type_: entry_type.to_owned(), - id: if self.window.is_some() { - uuid::Uuid::new_v4().to_string() - } else { - super::new_entry_id(&self.by_id) - }, - parent_id: self.leaf_id.clone(), - timestamp: crate::util::now_iso(), - fields, - }; - let delivery = match entry_type { - "message" => OwnerDelivery::User, - "custom_message" => OwnerDelivery::Custom, - _ => return Err(failure("unsupported delivered row kind")), - }; - self.stage_canonical_entry(entry, delivery) - } - - pub(super) fn stage_owned_entry(&mut self, entry: SessionEntry) -> Result { - self.stage_canonical_entry(entry, OwnerDelivery::Metadata) - } - - fn stage_canonical_entry( - &mut self, - entry: SessionEntry, - delivery: OwnerDelivery, - ) -> Result { - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - let current = state.current(self)?; - anyhow::ensure!( - current.cursor == state.raw.cursor, - "accepted delivery source changed outside owner" - ); - anyhow::ensure!( - !state - .raw - .plan - .rows - .iter() - .any(|row| row.id.as_deref() == Some(entry.id.as_str())), - "canonical stable row identity already exists" - ); - state - .revision - .check_owner_state_advance() - .map_err(|e| failure(format!("stage preflight: {e:?}")))?; - match delivery { - OwnerDelivery::User => anyhow::ensure!( - super::canonical_adapter::is_user_row(&entry), - "staged User has another kind" - ), - OwnerDelivery::Custom => anyhow::ensure!( - entry.type_ == "custom_message", - "staged Custom has another kind" - ), - OwnerDelivery::Metadata => {} - } - let grant = if matches!(delivery, OwnerDelivery::User) { - state - .authority - .begin_prepare_append(CanonicalIoStage::PreparePreflight)? - } else { - let draft = metadata_draft(&entry)?; - metadata_action(draft.entry()) - .ok_or_else(|| failure("unsupported explicit owner staging action"))?; - state.grant(self, &draft)? - }; - let operation = CanonicalOperation::new(); - let barrier = state - .revision - .set_barrier(&operation) - .map_err(|e| failure(format!("delivery barrier: {e:?}")))?; - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: operation.clone(), - cursor: current.cursor.clone(), - barrier, - prepared: true, - mutation: false, - signal: None, - staged: true, - from: current.cursor.clone(), - opening_run: None, - }); - let mut attempt = state - .revision - .admit_write(Some(&operation)) - .map_err(|e| unresolved(format!("delivery admission: {e:?}")))?; - attempt - .begin_io() - .map_err(|e| unresolved(format!("delivery write start: {e:?}")))?; - let id = match self.persist_granted_entry(entry, None, Some(grant)) { - Ok(id) => id, - Err(error) => { - state.blocked = true; - return Err(error); - } - }; - attempt - .finish_success() - .map_err(|e| unresolved(format!("delivery finish: {e:?}")))?; - // Keep successful-write debt intact when suffix capture fails. - #[cfg(test)] - if std::mem::take(&mut state.fail_stage_capture) { - return Err(unresolved("injected stage suffix capture failure").into()); - } - let appended = RawCapture::append_from( - &state.raw, - ¤t.cursor, - state.position(self.leaf_id.clone())?, - CaptureMode::Full, - )?; - anyhow::ensure!( - appended.rows.len() == 1 && appended.rows[0].entry().id() == Some(id.as_str()), - "accepted delivery lacks exact physical suffix" - ); - state.raw = appended.capture; - let cursor = state.raw.cursor.clone(); - state - .pending - .as_mut() - .ok_or_else(|| unresolved("staged delivery lost"))? - .cursor = cursor; - match delivery { - OwnerDelivery::User => { - state.registered.insert(id.clone()); - state.delivered.push_back(id.clone()); - } - OwnerDelivery::Custom => state.delivered_custom.push_back(id.clone()), - OwnerDelivery::Metadata => {} - } - let pending = state - .pending - .as_ref() - .ok_or_else(|| unresolved("staged delivery lost"))?; - state - .revision - .clear_barrier(&operation, &pending.barrier) - .map_err(|e| unresolved(format!("delivery completion barrier: {e:?}")))?; - state.pending.take(); - Ok(id) - }) - .map_err(unresolved) - } -} - -impl SessionFile { - pub(crate) fn admit_canonical_custom( - &mut self, - request: CanonicalLiveCustomRequest, - ) -> Result { - self.attach_canonical_owner()?; - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - let payload = pa_types::session::CustomMessageEntry { - custom_type: request.message.custom_type, - content: request.message.content, - display: request.message.display, - details: request.message.details, - rest: request.message.rest, - }; - lease.with_owned_mutation(&self.path.clone(), || { - let current = state.current(self)?; - anyhow::ensure!(current.cursor == state.raw.cursor, "canonical Custom source changed outside owner"); - if let Some(expected) = request.expected.as_ref().filter(|expected| *expected != ¤t.cursor) { - return Err(state.refuse_unstarted(expected, "canonical Custom admission source CAS conflict").into()); - } - current.checkpoint(request.source)?; - let existing = state.delivered_custom.front().cloned(); - if let Some(id) = &existing { - let row = current.retained.iter().find(|row| row.entry().id() == Some(id.as_str())) - .ok_or_else(|| failure("delivered Custom FIFO row missing"))?; - anyhow::ensure!(matches!(row.entry(), pa_types::session::FileEntry::CustomMessage { payload: actual, .. } if actual == &payload), - "delivered Custom FIFO head differs from exact accepted intent"); - } - state.revision.check_owner_state_advance() - .map_err(|e| failure(format!("Custom preflight: {e:?}")))?; - let operation = CanonicalOperation::new(); - let barrier = state.revision.set_barrier(&operation) - .map_err(|e| failure(format!("Custom delivery barrier: {e:?}")))?; - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: operation.clone(), cursor: current.cursor.clone(), barrier, - prepared: true, mutation: false, signal: None, staged: false, from: current.cursor.clone(), - opening_run: None, - }); - let id = if let Some(id) = existing { - state.revision.advance_owner_state(Some(&operation)) - .map_err(|e| failure(format!("Custom ledger: {e:?}")))?; - let position = state.position(self.leaf_id.clone())?; - state.raw.refresh_owner_position(&position); - state.delivered_custom.pop_front(); - id - } else { - let entry = SessionEntry { - type_: "custom_message".to_owned(), - id: if self.window.is_some() { uuid::Uuid::new_v4().to_string() } else { super::new_entry_id(&self.by_id) }, - parent_id: self.leaf_id.clone(), timestamp: crate::util::now_iso(), - fields: serde_json::to_value(&payload).map_err(failure)?, - }; - let draft = metadata_draft(&entry)?; - let grant = state.grant(self, &draft)?; - let mut attempt = state.revision.admit_write(Some(&operation)) - .map_err(|e| failure(format!("Custom write admission: {e:?}")))?; - attempt.begin_io().map_err(|e| unresolved(format!("Custom write start: {e:?}")))?; - let id = match self.persist_granted_entry(entry, None, Some(grant)) { - Ok(id) => id, - Err(error) => { state.blocked = true; return Err(error); } - }; - attempt.finish_success().map_err(|e| unresolved(format!("Custom write finish: {e:?}")))?; - let appended = RawCapture::append_from(&state.raw, ¤t.cursor, - state.position(self.leaf_id.clone())?, CaptureMode::Full)?; - anyhow::ensure!(appended.rows.len() == 1 && appended.rows[0].entry().id() == Some(id.as_str()), - "canonical Custom write lacks exact single physical suffix"); - state.raw = appended.capture; - id - }; - let checkpoint = state.raw.checkpoint(request.source)?; - let (caller, signal) = CanonicalCallerGuard::new(Arc::new(Notify)); - let pending = state.pending.as_mut().ok_or_else(|| unresolved("Custom pending state lost"))?; - pending.cursor = checkpoint.cursor.clone(); - pending.signal = Some(signal); - Ok(CanonicalLiveCustomPrepared { - prepared: CanonicalPrepared { caller, operation, checkpoint }, admitted_entry_id: id, - }) - }).map_err(|error| match error.downcast::() { - Ok(error) => error, - Err(error) => unresolved(error), - }) - } -} - -impl SessionFile { - /// Actual worker command/lifecycle admission, never a generic canonical write. - /// The exact typed row must match one of the named `MetadataAction` variants. - pub(crate) fn persist_owner_metadata( - &mut self, - kind: &str, - fields: serde_json::Value, - ) -> Result { - if !self.is_canonical_bound() { - return self.persist_entry(kind, fields).map_err(failure); - } - let entry = SessionEntry { - type_: kind.to_owned(), - id: if self.window.is_some() { - uuid::Uuid::new_v4().to_string() - } else { - super::new_entry_id(&self.by_id) - }, - parent_id: self.leaf_id.clone(), - timestamp: crate::util::now_iso(), - fields, - }; - let draft = metadata_draft(&entry)?; - if metadata_action(draft.entry()).is_none() { - return Err(failure( - "worker lifecycle row has no exact named MetadataAction", - )); - } - self.stage_owned_entry(entry) - } -} - -impl SessionFile { - /// Uncertain/invalid source, distinct from a known durable staged acceptance. - /// UI may show known durable rows while Core still owes their exact ACK. - pub(crate) fn canonical_source_uncertain(&self) -> bool { - let Some(owner) = &self.canonical_live else { - return super::canonical_adapter::epoch_is_poisoned(self.epoch.as_ref()); - }; - let Ok(state) = owner.0.lock() else { - return true; - }; - let Ok(revision) = state.revision.snapshot() else { - return true; - }; - let blocked = state.blocked; - let authority_poison_observed = !blocked && state.authority.poisoned(); - if blocked - || authority_poison_observed - || revision.canonical_revision != state.raw.cursor.canonical_revision - || revision.branch_revision != state.raw.cursor.branch_revision - || state - .pending - .as_ref() - .is_some_and(|pending| pending.signal.is_none()) - || state.callers.iter().any(|signal| { - signal.state() == pa_core::session::canonical::CanonicalCallerState::Abandoned - }) - || state - .pending - .as_ref() - .and_then(|pending| pending.signal.as_ref()) - .is_some_and(|signal| { - signal.state() == pa_core::session::canonical::CanonicalCallerState::Abandoned - }) - { - return true; - } - let Ok(lease) = state.verify(self) else { - return true; - }; - lease.with_owned_mutation(&self.path, || Ok(())).is_err() - } - - /// Retire the actual owner at a certified boundary after a quarantined SDK - /// view blocked every later projection. - /// - /// The source must still equal the owner's last certified cursor and no - /// receipt may remain: no prepared mutation, admitted tool, unfinished - /// caller, live run, or child usage I/O. The next build then attaches a - /// successor epoch that re-reads the same durable rows; history is never - /// rewritten and no unknown physical owner is reused. - pub(crate) fn retire_quarantined_canonical_owner(&mut self) -> Result<()> { - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - { - let state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - if state.callers.iter().any(|signal| { - signal.state() != pa_core::session::canonical::CanonicalCallerState::Completed - }) { - return Err(unresolved("canonical caller has not completed")); - } - } - let cursor = self.canonical_snapshot()?.checkpoint.cursor; - self.retire_completed_canonical_runtime(&cursor) - } -} - -impl SessionFile { - /// Actual awaited `AgentStart`, after raw adoption/ACK and before any paid work. - pub(crate) fn register_canonical_run( - &mut self, - request: CanonicalLiveRunRequest, - ) -> Result { - self.attach_canonical_owner()?; - let owner = self - .canonical_live - .clone() - .ok_or_else(|| failure("canonical owner not attached"))?; - let mut state = owner - .0 - .lock() - .map_err(|_| unresolved("canonical owner mutex poisoned"))?; - state.available()?; - let lease = state.verify(self)?; - lease - .with_owned_mutation(&self.path.clone(), || { - anyhow::ensure!( - state.current(self)?.cursor == state.raw.cursor, - "actual AgentStart source changed outside owner" - ); - // This check admits only an exact source or a monotone, same-owner - // advance with no outstanding adoption, mutation, or tool debt. - anyhow::ensure!( - state.certifies_unstarted_source(&request.expected) - && state.raw.certifies_prior_prefix(&request.expected), - "actual AgentStart belongs to another certified source" - ); - if let Some(previous) = &state.registered_run { - anyhow::ensure!( - !Arc::ptr_eq(&previous.identity, &request.run), - "actual run was already registered" - ); - anyhow::ensure!( - previous.terminal_acked, - "previous actual run has no acknowledged native terminal row" - ); - } - let checkpoint = state - .raw - .checkpoint(pa_core::session::canonical::CanonicalSourceRequest::Full)?; - let operation = CanonicalOperation::new(); - let barrier = state - .revision - .set_barrier(&operation) - .map_err(|e| unresolved(format!("run opening barrier: {e:?}")))?; - let (caller, signal) = CanonicalCallerGuard::new(Arc::new(Notify)); - state.pending = Some(Pending { - provenance: PendingProvenance::Retained, - operation: operation.clone(), - cursor: checkpoint.cursor.clone(), - barrier, - prepared: true, - mutation: false, - signal: Some(signal), - staged: false, - from: checkpoint.cursor.clone(), - opening_run: Some(request.run), - }); - Ok(CanonicalPrepared { - caller, - operation, - checkpoint, - }) - }) - .map_err(|error| match error.downcast::() { - Ok(error) => error, - Err(error) => unresolved(error), - }) - } -} - -#[cfg(test)] -impl SessionFile { - pub(super) fn fail_next_stage_capture(&mut self) { - self.canonical_live - .as_ref() - .unwrap() - .0 - .lock() - .unwrap() - .fail_stage_capture = true; - } -} diff --git a/crates/pa-daemon/src/session_store/canonical_live_tests.rs b/crates/pa-daemon/src/session_store/canonical_live_tests.rs deleted file mode 100644 index 952c4ff8e..000000000 --- a/crates/pa-daemon/src/session_store/canonical_live_tests.rs +++ /dev/null @@ -1,1148 +0,0 @@ -//! Real lease/file regressions for the owner initiated canonical seam. -mod preflight; -mod runtime; -use super::SessionFile; -use pa_core::session::canonical::{ - CanonicalAckPhase, CanonicalAcknowledgement, CanonicalAdmissionPolicy, CanonicalCallerGuard, - CanonicalCommitDisposition, CanonicalEntryDraft, CanonicalMutationKind, CanonicalOperation, - CanonicalPrepareMode, CanonicalPrepared, CanonicalSourceRequest, -}; -use pa_core::session::manager::{CanonicalLocalAdoption, SessionManager}; -use pa_core::session_engine::canonical_live::{ - CanonicalLiveAppendRequest, CanonicalLivePrepareRequest, CanonicalLiveUserRequest, -}; -use pa_types::session::FileEntry; -use serde_json::json; -use std::sync::Arc; - -fn fixture() -> (tempfile::TempDir, SessionFile) { - let dir = tempfile::tempdir().unwrap(); - let mut store = SessionFile::create("/isolated", None, 0); - store.set_path(dir.path().join("session.jsonl")); - store.rewrite().unwrap(); - store.lease = Some(Arc::new( - crate::lease::acquire_runtime_session_lease(&store.path, dir.path()).unwrap(), - )); - (dir, store) -} - -fn request(mode: CanonicalPrepareMode) -> CanonicalLivePrepareRequest { - CanonicalLivePrepareRequest { - expected: None, - source: CanonicalSourceRequest::Full, - mode, - admission: CanonicalAdmissionPolicy::Preserve, - } -} - -fn mirror(store: &SessionFile) -> SessionManager { - SessionManager::in_memory_from_header( - std::path::Path::new("/isolated"), - std::path::Path::new("/isolated/artifacts"), - store.header.clone(), - ) - .unwrap() -} - -fn adopt_prepared( - store: &mut SessionFile, - manager: &mut SessionManager, - prepared: CanonicalPrepared, -) -> CanonicalCallerGuard { - let binding = manager.canonical_binding().cloned(); - let cursor = prepared.checkpoint.cursor.clone(); - let certificate = manager - .install_canonical_checkpoint( - &prepared.operation, - manager.canonical_local_revision(), - binding.as_ref(), - prepared.checkpoint, - CanonicalLocalAdoption::preserve(), - ) - .unwrap(); - store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation, - cursor, - phase: CanonicalAckPhase::Prepared, - adoption: Some(certificate), - }) - .unwrap(); - prepared.caller -} - -fn custom_draft() -> CanonicalEntryDraft { - CanonicalEntryDraft::new( - CanonicalMutationKind::OwnerMetadata, - serde_json::from_value( - json!({"type":"custom", "customType":"exact", "data":{"native":"AA==","id":"opaque"}}), - ) - .unwrap(), - ) - .unwrap() -} - -#[test] -fn owner_scope_starts_only_at_lease_attach_and_survives_clones() { - let (_dir, mut store) = fixture(); - assert!(!store.is_canonical_bound()); - let cursor = store.attach_canonical_owner().unwrap(); - let mut clone = store.clone(); - assert_eq!(clone.attach_canonical_owner().unwrap(), cursor); - let reopened = SessionFile::open(&store.path).unwrap(); - assert!(!reopened.is_canonical_bound()); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - assert!(clone - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - drop(prepared); - let error = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .err() - .expect("abandoned caller must refuse preparation"); - assert_eq!(error.detail, "canonical owner adoption abandoned"); -} - -#[test] -fn exact_metadata_suffix_blocks_all_writes_until_core_adoption_ack() { - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::OwnerMetadata, - ))) - .unwrap(); - let operation = prepared.operation.clone(); - let from = prepared.checkpoint.cursor.clone(); - let caller = adopt_prepared(&mut store, &mut manager, prepared); - let prefix = std::fs::read(&store.path).unwrap(); - let disposition = store - .append_canonical(CanonicalLiveAppendRequest { - context_admission: None, - run: None, - operation: operation.clone(), - expected: from.clone(), - source: CanonicalSourceRequest::Full, - draft: custom_draft(), - }) - .unwrap(); - let CanonicalCommitDisposition::Committed(receipt) = disposition else { - panic!("commit"); - }; - assert_eq!(receipt.from, from); - assert_eq!(receipt.rows.len(), 1); - assert_eq!(receipt.rows[0].byte_offset(), prefix.len() as u64); - let suffix = receipt.rows[0].raw().to_vec(); - assert_eq!( - std::fs::read(&store.path).unwrap(), - [prefix.as_slice(), suffix.as_slice()].concat() - ); - assert_eq!(receipt.to.canonical_revision, from.canonical_revision + 1); - assert_eq!(store.rows_written_without_epoch(), 0); - assert!(store - .persist_entry("custom", json!({"customType":"bypass"})) - .is_err()); - assert!(store.rewrite().is_err()); - let error = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .err() - .expect("pending adoption must refuse preparation"); - assert_eq!( - error.detail, - "canonical owner awaiting adoption acknowledgement" - ); - let cursor = receipt.to.clone(); - let certificate = manager - .apply_canonical_suffix( - manager.canonical_local_revision(), - receipt, - CanonicalLocalAdoption::preserve(), - ) - .unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: CanonicalOperation::new(), - cursor: cursor.clone(), - phase: CanonicalAckPhase::AppliedReady, - adoption: Some(certificate.clone()) - }) - .is_err()); - store - .acknowledge_canonical(CanonicalAcknowledgement { - operation, - cursor, - phase: CanonicalAckPhase::AppliedReady, - adoption: Some(certificate), - }) - .unwrap(); - caller.complete(); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_ok()); -} - -#[test] -fn accepted_user_is_durable_once_and_replayed_only_by_fifo_with_original_timestamp() { - let (_dir, mut store) = fixture(); - let initial = store.attach_canonical_owner().unwrap(); - let id = store - .persist_delivered_user(json!({"role":"user","content":"same body","timestamp":7})) - .unwrap(); - assert!(store - .prepare_canonical(CanonicalLivePrepareRequest { - expected: Some(initial), - source: CanonicalSourceRequest::Full, - mode: CanonicalPrepareMode::Observe, - admission: CanonicalAdmissionPolicy::Append(vec!["not-delivered".into()]) - }) - .is_err()); - let delivered = store - .admit_canonical_user(CanonicalLiveUserRequest { - expected: Some(store.canonical_snapshot().unwrap().checkpoint.cursor), - source: CanonicalSourceRequest::Full, - message: serde_json::from_value(json!({"content":"same body","timestamp":99})).unwrap(), - }) - .unwrap(); - assert_eq!(delivered.admitted_entry_id, id); - let rows = match &delivered.prepared.checkpoint.source { - pa_core::session::canonical::CanonicalSource::Full(rows) => rows, - pa_core::session::canonical::CanonicalSource::Window(_) => panic!("full"), - }; - assert_eq!(rows.len(), 1); - assert!( - matches!(rows[0].entry(), FileEntry::Message { message: pa_types::session::AgentMessage::User(actual), .. } if actual.timestamp == 7) - ); - assert_eq!(SessionFile::open(&store.path).unwrap().entries().len(), 1); - drop(delivered); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); -} - -#[test] -fn lost_prepared_caller_and_missing_certificate_fail_closed_without_more_bytes() { - let (_dir, mut store) = fixture(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation.clone(), - cursor: prepared.checkpoint.cursor.clone(), - phase: CanonicalAckPhase::Prepared, - adoption: None, - }) - .is_err()); - drop(prepared); - assert!(store - .persist_delivered_user(json!({"role":"user","content":"blocked","timestamp":1})) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn bound_report_keeps_actual_stable_journal_id_and_retry_does_not_append() { - let (_dir, mut store) = fixture(); - let target = store - .persist_entry( - "message", - json!({"message": { - "role":"assistant", "content":[], "api":"openai-completions", "provider":"fake", - "model":"fake", "stopReason":"stop", "timestamp":1, - "usage": pa_types::ai::Usage::default(), - }}), - ) - .unwrap(); - store.attach_canonical_owner().unwrap(); - let usage = pa_types::ai::Usage { - input: 4, - ..Default::default() - }; - store - .append_child_usage_once("stable-report", &target, usage.clone(), None) - .unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - store - .append_child_usage_once("stable-report", &target, usage.clone(), None) - .unwrap(); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - let rows = match &prepared.checkpoint.source { - pa_core::session::canonical::CanonicalSource::Full(rows) => rows, - pa_core::session::canonical::CanonicalSource::Window(_) => panic!("full"), - }; - assert_eq!(rows.len(), 2); - assert_eq!(rows.last().unwrap().entry().id(), Some("stable-report")); - let mut manager = mirror(&store); - adopt_prepared(&mut store, &mut manager, prepared).complete(); - store - .append_child_usage_once("stable-report", &target, usage, None) - .unwrap(); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn navigation_aba_keeps_bytes_and_owner_but_advances_both_revisions() { - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - let before = prepared.checkpoint.cursor.clone(); - adopt_prepared(&mut store, &mut manager, prepared).complete(); - let bytes = std::fs::read(&store.path).unwrap(); - store.advance_canonical_branch(None).unwrap(); - store.advance_canonical_branch(None).unwrap(); - let after = store.attach_canonical_owner().unwrap(); - assert_eq!(after.owner, before.owner); - assert_eq!(after.canonical_revision, before.canonical_revision + 2); - assert_eq!(after.branch_revision, before.branch_revision + 2); - assert_eq!(after.frontier, before.frontier); - assert_eq!(after.raw_source_generation, before.raw_source_generation); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn worker_metadata_entrypoint_refuses_user_assistant_tool_and_context_rows() { - let (_dir, mut store) = fixture(); - store.attach_canonical_owner().unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - assert!(store - .persist_owner_metadata( - "message", - json!({"message":{ - "role":"user", "content":"not-metadata", "timestamp":1 - }}) - ) - .is_err()); - assert!(store - .persist_owner_metadata( - "compaction", - json!({ - "summary":"not-metadata", "firstKeptEntryId":"a", "tokensBefore":1 - }) - ) - .is_err()); - assert!(store - .persist_owner_metadata( - "message", - json!({"message":{ - "role":"assistant", "content":[], "api":"openai-completions", "provider":"fake", - "model":"fake", "stopReason":"stop", "timestamp":1, - "usage":pa_types::ai::Usage::default(), - }}) - ) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn strict_source_advance_refuses_requested_row_before_io_without_reparenting() { - use std::io::Write; - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::OwnerMetadata, - ))) - .unwrap(); - let operation = prepared.operation.clone(); - let expected = prepared.checkpoint.cursor.clone(); - let caller = adopt_prepared(&mut store, &mut manager, prepared); - let foreign = b"{\"type\":\"custom\",\"id\":\"external\",\"parentId\":null,\"timestamp\":\"external\",\"customType\":\"external\"}\n"; - let mut file = std::fs::OpenOptions::new() - .append(true) - .open(&store.path) - .unwrap(); - file.write_all(foreign).unwrap(); - file.sync_all().unwrap(); - let before = std::fs::read(&store.path).unwrap(); - let Err(error) = store.append_canonical(CanonicalLiveAppendRequest { - context_admission: None, - run: None, - operation, - expected, - source: CanonicalSourceRequest::Full, - draft: custom_draft(), - }) else { - panic!("stale source must not write"); - }; - assert_eq!( - error.requested, - pa_core::session::canonical::CanonicalRequestedWrite::NotWritten - ); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - caller.complete(); -} - -#[test] -fn ack_visibility_is_capped_by_both_actual_delivery_ledgers() { - let (_dir, mut store) = fixture(); - let before = store - .persist_entry("custom", json!({"customType":"before-delivery"})) - .unwrap(); - let user = store - .persist_delivered_user(json!({"role":"user","content":"accepted","timestamp":7})) - .unwrap(); - store - .persist_delivered_custom(json!({"customType":"queued","content":"notice","display":true})) - .unwrap(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - let operation = prepared.operation.clone(); - let cursor = prepared.checkpoint.cursor.clone(); - let mut manager = mirror(&store); - let certificate = manager - .install_canonical_checkpoint( - &operation, - 0, - None, - prepared.checkpoint, - CanonicalLocalAdoption::preserve(), - ) - .unwrap(); - let proof = store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: operation.clone(), - cursor: cursor.clone(), - phase: CanonicalAckPhase::Prepared, - adoption: Some(certificate), - }) - .unwrap(); - assert_eq!( - (proof.operation, proof.cursor, proof.visible_leaf), - (operation, cursor.clone(), Some(before)) - ); - prepared.caller.complete(); - let delivered = store - .admit_canonical_user(CanonicalLiveUserRequest { - expected: Some(cursor), - source: CanonicalSourceRequest::Full, - message: serde_json::from_value(json!({"content":"accepted","timestamp":999})).unwrap(), - }) - .unwrap(); - assert_eq!(delivered.admitted_entry_id, user); - let prepared = delivered.prepared; - let cursor = prepared.checkpoint.cursor.clone(); - let binding = manager.canonical_binding().cloned(); - let certificate = manager - .install_canonical_checkpoint( - &prepared.operation, - manager.canonical_local_revision(), - binding.as_ref(), - prepared.checkpoint, - CanonicalLocalAdoption::preserve(), - ) - .unwrap(); - let proof = store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation, - cursor, - phase: CanonicalAckPhase::Prepared, - adoption: Some(certificate), - }) - .unwrap(); - assert_eq!(proof.visible_leaf, Some(user)); - prepared.caller.complete(); -} - -#[test] -fn ack_refuses_pending_delivery_not_on_actual_selected_branch() { - let (_dir, mut store) = fixture(); - store - .persist_delivered_user(json!({"role":"user","content":"unselected","timestamp":1})) - .unwrap(); - store.branch_to(None).unwrap(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - let mut manager = mirror(&store); - let cursor = prepared.checkpoint.cursor.clone(); - let certificate = manager - .install_canonical_checkpoint( - &prepared.operation, - 0, - None, - prepared.checkpoint, - CanonicalLocalAdoption::preserve(), - ) - .unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation, - cursor, - phase: CanonicalAckPhase::Prepared, - adoption: Some(certificate) - }) - .is_err()); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - drop(prepared.caller); -} - -#[test] -fn prepared_ack_then_explicit_prepared_only_abandon_does_not_require_pending_receipt() { - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::ContextPolicy, - ))) - .unwrap(); - let operation = prepared.operation.clone(); - let cursor = prepared.checkpoint.cursor.clone(); - let caller = adopt_prepared(&mut store, &mut manager, prepared); - let before = std::fs::read(&store.path).unwrap(); - let proof = store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: operation.clone(), - cursor: cursor.clone(), - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .unwrap(); - assert_eq!((proof.operation, proof.cursor), (operation, cursor)); - caller.complete(); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_ok()); - assert_eq!(std::fs::read(&store.path).unwrap(), before); -} - -#[test] -fn prepared_only_abandon_survives_owner_delivered_agent_message() { - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::ContextPolicy, - ))) - .unwrap(); - let operation = prepared.operation.clone(); - let cursor = prepared.checkpoint.cursor.clone(); - let caller = adopt_prepared(&mut store, &mut manager, prepared); - // Incoming subagent messages persist through another handle while context - // work is prepared. This advances the owner, not the prepared operation. - let mut delivery = store.clone(); - let id = delivery - .persist_delivered_custom(json!({ - "customType":"agent_message", - "content":"[agent-message from child] done", - "display":true - })) - .unwrap(); - store.leaf_id = delivery.leaf_id.clone(); - let before = std::fs::read(&store.path).unwrap(); - let error = store - .append_canonical(CanonicalLiveAppendRequest { - context_admission: None, - run: None, - operation: operation.clone(), - expected: cursor.clone(), - source: CanonicalSourceRequest::Full, - draft: custom_draft(), - }) - .err() - .expect("stale preparation must not append"); - assert_eq!( - error.requested, - pa_core::session::canonical::CanonicalRequestedWrite::NotWritten - ); - store - .acknowledge_canonical(CanonicalAcknowledgement { - operation, - cursor, - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .expect("certified delivery must not strand an unwritten preparation"); - caller.complete(); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - let snapshot = store.canonical_snapshot().unwrap(); - assert_eq!( - snapshot.pending_custom, - std::collections::VecDeque::from([id]) - ); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - adopt_prepared(&mut store, &mut manager, prepared).complete(); - assert_eq!(std::fs::read(&store.path).unwrap(), before); -} - -#[test] -fn native_terminal_prepare_does_not_absorb_conversation_or_context_changes() { - for entry_type in ["custom", "custom_message", "message"] { - let (_dir, mut store) = fixture(); - let expected = store.attach_canonical_owner().unwrap(); - match entry_type { - "custom" => { - store - .persist_owner_metadata( - "custom", - json!({"customType":"context_strategy_selection", "data":{}}), - ) - .unwrap(); - } - "custom_message" => { - store - .persist_delivered_custom( - json!({"customType":"input", "content":"new input", "display":false}), - ) - .unwrap(); - } - "message" => { - store.persist_delivered_user(json!({"role":"user", "content":[{"type":"text", "text":"new input"}], "timestamp":1})).unwrap(); - } - _ => unreachable!(), - } - let before = std::fs::read(&store.path).unwrap(); - let result = store.prepare_canonical(CanonicalLivePrepareRequest { - expected: Some(expected), - source: CanonicalSourceRequest::Full, - mode: CanonicalPrepareMode::Mutation(CanonicalMutationKind::AdmittedTerminal), - admission: CanonicalAdmissionPolicy::Preserve, - }); - assert!(result.is_err(), "terminal prepare absorbed {entry_type}"); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - store.canonical_snapshot().unwrap(); - } -} - -#[test] -fn prepared_only_abandon_survives_child_usage_attribution() { - let (_dir, mut store) = fixture(); - let target = store - .persist_entry( - "message", - json!({"message": { - "role":"assistant", "content":[], "api":"openai-completions", "provider":"fake", - "model":"fake", "stopReason":"stop", "timestamp":1, - "usage": pa_types::ai::Usage::default(), - }}), - ) - .unwrap(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::OwnerMetadata, - ))) - .unwrap(); - let operation = prepared.operation.clone(); - let cursor = prepared.checkpoint.cursor.clone(); - let caller = adopt_prepared(&mut store, &mut manager, prepared); - store - .append_child_usage_once( - "child-report", - &target, - pa_types::ai::Usage::default(), - None, - ) - .unwrap(); - let before = std::fs::read(&store.path).unwrap(); - store - .acknowledge_canonical(CanonicalAcknowledgement { - operation, - cursor, - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .expect("child usage must not strand an unwritten preparation"); - caller.complete(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - adopt_prepared(&mut store, &mut manager, prepared).complete(); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - assert_eq!(store.entries().last().unwrap().id, "child-report"); -} - -#[test] -fn prepared_only_abandon_refuses_unowned_source_change() { - use std::io::Write; - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::ContextPolicy, - ))) - .unwrap(); - let operation = prepared.operation.clone(); - let cursor = prepared.checkpoint.cursor.clone(); - let caller = adopt_prepared(&mut store, &mut manager, prepared); - std::fs::OpenOptions::new() - .append(true) - .open(&store.path) - .unwrap() - .write_all(b"\n") - .unwrap(); - let before = std::fs::read(&store.path).unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation, - cursor, - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .is_err()); - drop(caller); - assert!(store.canonical_snapshot().is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), before); -} - -#[test] -fn exact_unwritten_observation_can_be_abandoned_after_caller_drop() { - let (_dir, mut store) = fixture(); - let before = std::fs::read(&store.path).unwrap(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - let ack = CanonicalAcknowledgement { - operation: prepared.operation.clone(), - cursor: prepared.checkpoint.cursor.clone(), - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }; - let wrong = CanonicalAcknowledgement { - operation: CanonicalOperation::new(), - cursor: ack.cursor.clone(), - phase: ack.phase, - adoption: None, - }; - assert!(store.acknowledge_canonical(wrong).is_err()); - drop(prepared); - store.acknowledge_canonical(ack).unwrap(); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - let next = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - assert_eq!(next.checkpoint.cursor.frontier.bytes, before.len() as u64); -} - -#[test] -fn pending_mutation_cannot_use_observation_abandon() { - let (_dir, mut store) = fixture(); - let before = std::fs::read(&store.path).unwrap(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::OwnerMetadata, - ))) - .unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation.clone(), - cursor: prepared.checkpoint.cursor.clone(), - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .is_err()); - drop(prepared); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), before); -} - -#[test] -fn durable_user_pending_cannot_be_abandoned_as_unwritten_observation() { - let (_dir, mut store) = fixture(); - let delivered = store - .admit_canonical_user(CanonicalLiveUserRequest { - expected: None, - source: CanonicalSourceRequest::Full, - message: serde_json::from_value(json!({"content":"durable","timestamp":7})).unwrap(), - }) - .unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - let id = delivered.admitted_entry_id.clone(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: delivered.prepared.operation.clone(), - cursor: delivered.prepared.checkpoint.cursor.clone(), - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .is_err()); - drop(delivered); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); - assert_eq!(SessionFile::open(&store.path).unwrap().entries()[0].id, id); -} - -#[test] -fn staged_observation_with_admission_cannot_be_abandoned() { - let (_dir, mut store) = fixture(); - store.attach_canonical_owner().unwrap(); - let id = store - .persist_delivered_user(json!({"role":"user","content":"staged","timestamp":7})) - .unwrap(); - let prepared = store - .prepare_canonical(CanonicalLivePrepareRequest { - expected: None, - source: CanonicalSourceRequest::Full, - mode: CanonicalPrepareMode::Observe, - admission: CanonicalAdmissionPolicy::Append(vec![id]), - }) - .unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation.clone(), - cursor: prepared.checkpoint.cursor.clone(), - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .is_err()); - drop(prepared); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn unwritten_observation_abandon_refuses_external_source_change() { - use std::io::Write; - let (_dir, mut store) = fixture(); - let prepared = store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .unwrap(); - std::fs::OpenOptions::new() - .append(true) - .open(&store.path) - .unwrap() - .write_all(b"\n") - .unwrap(); - let changed = std::fs::read(&store.path).unwrap(); - assert!(store - .acknowledge_canonical(CanonicalAcknowledgement { - operation: prepared.operation.clone(), - cursor: prepared.checkpoint.cursor.clone(), - phase: CanonicalAckPhase::AbandonPreparedOnly, - adoption: None, - }) - .is_err()); - drop(prepared); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), changed); -} - -#[test] -fn immutable_snapshot_drop_and_rejection_leave_writer_usable_and_bytes_exact() { - let (_dir, mut store) = fixture(); - let before = store.attach_canonical_owner().unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - for _ in 0..3 { - let snapshot = store.canonical_snapshot().unwrap(); - assert_eq!(snapshot.checkpoint.cursor, before); - assert!(snapshot.pending_users.is_empty()); - assert!(snapshot.pending_custom.is_empty()); - assert!(snapshot.admitted_users.is_empty()); - drop(snapshot); - } - assert!(!store.canonical_source_uncertain()); - let id = store - .persist_owner_metadata( - "custom", - json!({"customType":"snapshot-drop","data":{"unknown":[1,true]}}), - ) - .unwrap(); - let snapshot = store.canonical_snapshot().unwrap(); - assert_eq!( - snapshot.checkpoint.cursor.canonical_revision, - before.canonical_revision + 1 - ); - assert_eq!(snapshot.safe_visible_leaf, Some(id)); - assert!(std::fs::read(&store.path).unwrap().starts_with(&bytes)); - drop(snapshot); - assert!(!store.canonical_source_uncertain()); -} - -#[test] -fn completed_stages_allow_snapshots_but_pending_fifos_cap_visibility_and_navigation() { - let (_dir, mut store) = fixture(); - let before = store.attach_canonical_owner().unwrap(); - let user = store - .persist_delivered_user(json!({"role":"user","content":"accepted","timestamp":7})) - .unwrap(); - let custom = store - .persist_delivered_custom(json!({"customType":"queued","content":"note","display":true})) - .unwrap(); - let snapshot = store.canonical_snapshot().unwrap(); - assert_eq!( - snapshot.pending_users, - std::collections::VecDeque::from([user.clone()]) - ); - assert_eq!( - snapshot.pending_custom, - std::collections::VecDeque::from([custom]) - ); - assert_eq!(snapshot.safe_visible_leaf, None); - assert_eq!( - snapshot.checkpoint.cursor.canonical_revision, - before.canonical_revision + 2 - ); - let bytes = std::fs::read(&store.path).unwrap(); - assert!(store.advance_canonical_branch(None).is_err()); - let delivered = store - .admit_canonical_user(CanonicalLiveUserRequest { - expected: Some(snapshot.checkpoint.cursor.clone()), - source: CanonicalSourceRequest::Full, - message: serde_json::from_value(json!({"content":"accepted","timestamp":99})).unwrap(), - }) - .unwrap(); - assert_eq!(delivered.admitted_entry_id, user); - assert_eq!( - delivered.prepared.checkpoint.cursor.canonical_revision, - snapshot.checkpoint.cursor.canonical_revision + 1 - ); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); - // Actual admission reply debt remains guarded, unlike an immutable snapshot. - assert!(store.canonical_snapshot().is_err()); - drop(delivered); - assert!(store.canonical_snapshot().is_err()); -} - -#[test] -fn newly_written_user_admission_advances_once_not_twice() { - let (_dir, mut store) = fixture(); - let before = store.attach_canonical_owner().unwrap(); - let delivered = store - .admit_canonical_user(CanonicalLiveUserRequest { - expected: Some(before.clone()), - source: CanonicalSourceRequest::Full, - message: serde_json::from_value(json!({"content":"new","timestamp":7})).unwrap(), - }) - .unwrap(); - assert_eq!( - delivered.prepared.checkpoint.cursor.canonical_revision, - before.canonical_revision + 1 - ); - let pa_core::session::canonical::CanonicalSource::Full(rows) = - &delivered.prepared.checkpoint.source - else { - panic!("full") - }; - assert_eq!(rows.len(), 1); - assert_eq!( - rows[0].entry().id(), - Some(delivered.admitted_entry_id.as_str()) - ); -} - -#[test] -fn initial_sdk_bootstrap_allows_attached_staging_but_not_admission_transfer() { - let (_dir, mut store) = fixture(); - store.attach_canonical_owner().unwrap(); - store - .persist_delivered_user(json!({"role":"user","content":"initial","timestamp":7})) - .unwrap(); - store.validate_initial_canonical_sdk().unwrap(); - let snapshot = store.canonical_snapshot().unwrap(); - let prepared = store - .prepare_canonical(CanonicalLivePrepareRequest { - expected: Some(snapshot.checkpoint.cursor), - source: CanonicalSourceRequest::Full, - mode: CanonicalPrepareMode::Observe, - admission: CanonicalAdmissionPolicy::Replace( - snapshot.pending_users.into_iter().collect(), - ), - }) - .unwrap(); - assert!(store.validate_initial_canonical_sdk().is_err()); - drop(prepared); - assert!(store.validate_initial_canonical_sdk().is_err()); -} - -#[test] -fn custom_fifo_exact_adoption_keeps_original_row_and_next_snapshot() { - use pa_core::session_engine::canonical_live::CanonicalLiveCustomRequest; - let (_dir, mut store) = fixture(); - let mut manager = mirror(&store); - store.attach_canonical_owner().unwrap(); - let fields = json!({"customType":"queued", "content":[{"type":"text","text":"note"}], - "display":true, "details":{"opaque":"AA=="}, "futureField":{"ordered":[3,1,2]}}); - let first = store.persist_delivered_custom(fields.clone()).unwrap(); - let second = store.persist_delivered_custom(fields.clone()).unwrap(); - let before = store.canonical_snapshot().unwrap(); - let bytes = std::fs::read(&store.path).unwrap(); - let delivered = store - .admit_canonical_custom(CanonicalLiveCustomRequest { - expected: Some(before.checkpoint.cursor.clone()), - source: CanonicalSourceRequest::Full, - message: serde_json::from_value({ - let mut message = fields; - message["timestamp"] = json!(999); - message - }) - .unwrap(), - }) - .unwrap(); - assert_eq!(delivered.admitted_entry_id, first); - assert_eq!( - delivered.prepared.checkpoint.cursor.canonical_revision, - before.checkpoint.cursor.canonical_revision + 1 - ); - let pa_core::session::canonical::CanonicalSource::Full(original) = &before.checkpoint.source - else { - panic!("full"); - }; - let pa_core::session::canonical::CanonicalSource::Full(prepared_rows) = - &delivered.prepared.checkpoint.source - else { - panic!("full"); - }; - assert_eq!( - prepared_rows - .iter() - .map(pa_core::session::canonical::CanonicalJournalRow::raw) - .collect::>(), - original - .iter() - .map(pa_core::session::canonical::CanonicalJournalRow::raw) - .collect::>() - ); - assert!(store.canonical_snapshot().is_err()); - adopt_prepared(&mut store, &mut manager, delivered.prepared).complete(); - let after = store.canonical_snapshot().unwrap(); - assert_eq!( - after.pending_custom, - std::collections::VecDeque::from([second]) - ); - assert_eq!(after.safe_visible_leaf, Some(first)); - let pa_core::session::canonical::CanonicalSource::Full(after_rows) = &after.checkpoint.source - else { - panic!("full"); - }; - assert_eq!( - after_rows - .iter() - .map(pa_core::session::canonical::CanonicalJournalRow::raw) - .collect::>(), - original - .iter() - .map(pa_core::session::canonical::CanonicalJournalRow::raw) - .collect::>() - ); - assert_eq!( - after.checkpoint.cursor.canonical_revision, - before.checkpoint.cursor.canonical_revision + 1 - ); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn successful_stage_write_with_failed_capture_keeps_debt_and_exact_bytes() { - let (_dir, mut store) = fixture(); - store.attach_canonical_owner().unwrap(); - let prefix = std::fs::read(&store.path).unwrap(); - store.fail_next_stage_capture(); - assert!(store - .persist_delivered_custom(json!({ - "customType":"capture-failure", "content":"once", "display":true - })) - .is_err()); - let written = std::fs::read(&store.path).unwrap(); - assert!(written.starts_with(&prefix)); - let reopened = SessionFile::open(&store.path).unwrap(); - assert_eq!(reopened.entries().len(), 1); - assert!(store.canonical_snapshot().is_err()); - assert!(store - .persist_delivered_custom(json!({ - "customType":"capture-failure", "content":"once", "display":true - })) - .is_err()); - assert!(store - .persist_owner_metadata("custom", json!({"customType":"blocked"})) - .is_err()); - assert!(store - .prepare_canonical(request(CanonicalPrepareMode::Observe)) - .is_err()); - assert_eq!(std::fs::read(&store.path).unwrap(), written); -} - -#[test] -fn completed_owner_retirement_preserves_native_fifos_and_retires_old_scope() { - let (_dir, mut store) = fixture(); - store.attach_canonical_owner().unwrap(); - let user = store - .persist_delivered_user(json!({"role":"user","content":"pending","timestamp":7})) - .unwrap(); - let custom = store.persist_delivered_custom(json!({"customType":"pending", "content":"note", "display":true, "unknown":{"raw":[3,1]}})).unwrap(); - let snapshot = store.canonical_snapshot().unwrap(); - let mut old = store.clone(); - let bytes = std::fs::read(&store.path).unwrap(); - store - .retire_completed_canonical_runtime(&snapshot.checkpoint.cursor) - .unwrap(); - assert!(old.canonical_snapshot().is_err()); - assert!(old - .persist_delivered_user(json!({"role":"user","content":"late","timestamp":8})) - .is_err()); - let fresh = store.attach_canonical_owner().unwrap(); - assert_ne!(fresh.owner, snapshot.checkpoint.cursor.owner); - store.validate_initial_canonical_sdk().unwrap(); - let after = store.canonical_snapshot().unwrap(); - assert_eq!(after.pending_users, snapshot.pending_users); - assert_eq!(after.pending_custom, snapshot.pending_custom); - assert_eq!( - after.pending_users.into_iter().collect::>(), - vec![user] - ); - assert_eq!( - after.pending_custom.into_iter().collect::>(), - vec![custom] - ); - assert_eq!(std::fs::read(&store.path).unwrap(), bytes); -} - -#[test] -fn completed_owner_retirement_refuses_unacknowledged_mutation() { - let (_dir, mut store) = fixture(); - let cursor = store.attach_canonical_owner().unwrap(); - let pending = store - .prepare_canonical(request(CanonicalPrepareMode::Mutation( - CanonicalMutationKind::OwnerMetadata, - ))) - .unwrap(); - assert!(store.retire_completed_canonical_runtime(&cursor).is_err()); - drop(pending); - assert!(store.retire_completed_canonical_runtime(&cursor).is_err()); - assert!(store.attach_canonical_owner().is_err()); -} - -#[test] -fn accepted_delivery_refuses_external_rewrite_without_overwriting_history() { - let (_dir, mut store) = fixture(); - store - .persist_entry("custom", json!({"customType":"before", "data":"original"})) - .unwrap(); - store.attach_canonical_owner().unwrap(); - let original = std::fs::read_to_string(&store.path).unwrap(); - let rewritten = original.replace("original", "modified"); - assert_ne!(original, rewritten); - std::fs::write(&store.path, &rewritten).unwrap(); - let error = store - .persist_delivered_user(json!({"role":"user", "content":"must not append", "timestamp":1})) - .unwrap_err(); - assert_eq!( - error.kind, - pa_core::session::canonical::CanonicalFailureKind::AdoptionConflict - ); - assert_eq!( - error.detail, - "accepted delivery source changed outside owner" - ); - assert_eq!(std::fs::read_to_string(&store.path).unwrap(), rewritten); -} diff --git a/crates/pa-daemon/src/session_store/canonical_live_tests/runtime.rs b/crates/pa-daemon/src/session_store/canonical_live_tests/runtime.rs deleted file mode 100644 index c7333262e..000000000 --- a/crates/pa-daemon/src/session_store/canonical_live_tests/runtime.rs +++ /dev/null @@ -1,1105 +0,0 @@ -use super::*; -use pa_core::session::canonical::*; -use pa_core::session_engine::canonical_live::*; -use pa_core::session_engine::engine::{create_session, SessionEngine, SessionEngineConfig}; -use pa_core::session_engine::PromptOptions; -use std::sync::Mutex; - -#[derive(Clone, Copy, Default)] -enum AbandonReply { - #[default] - Exact, - Lost, - Foreign, -} -#[derive(Default)] -enum RunStartFault { - #[default] - None, - OwnedAdvance, - LedgerOnly(String), - ExternalAdvance, - ForeignOwner, - ForgedPrefix, - ForgedPosition, - ForgedLeaf, - ForgedGeneration, -} -#[derive(Clone, Copy, Default)] -enum ToolAttribution { - #[default] - None, - BeforePrepare, - AfterPrepare, -} -#[derive(Default)] -struct Faults { - run_start: RunStartFault, - tool_attribution: ToolAttribution, - advance_at_provider: bool, - advance_at_custom: bool, - lose_append_reply: bool, - lose_applied_ack: bool, - abandon_reply: AbandonReply, - run_prepared_reply: AbandonReply, - foreign_run_ack: bool, - opening_operation: Option, -} -struct RealOwner { - store: Mutex, - faults: Mutex, -} -fn append_tool_attributions(store: &mut SessionFile) { - let target = store - .entries() - .iter() - .rev() - .find(|row| row.type_ == "message" && row.fields["message"]["role"] == "assistant") - .unwrap() - .id - .clone(); - for id in ["tool-child-report-a", "tool-child-report-b"] { - store - .append_child_usage_once(id, &target, pa_types::ai::Usage::default(), None) - .unwrap(); - } -} - -impl CanonicalLiveOwner for RealOwner { - fn snapshot(&self) -> CanonicalFuture { - let result = self.store.lock().unwrap().canonical_snapshot(); - Box::pin(async move { result }) - } - fn prepare(&self, request: CanonicalLivePrepareRequest) -> CanonicalFuture { - let mut store = self.store.lock().unwrap(); - let mut faults = self.faults.lock().unwrap(); - if matches!(faults.tool_attribution, ToolAttribution::BeforePrepare) - && matches!( - &request.mode, - CanonicalPrepareMode::Mutation(CanonicalMutationKind::AdmittedTerminal) - ) - && store - .entries() - .iter() - .rev() - .find(|row| row.type_ == "message") - .is_some_and(|row| row.fields["message"]["stopReason"] == "toolUse") - { - append_tool_attributions(&mut store); - faults.tool_attribution = ToolAttribution::None; - } - drop(faults); - let result = store.prepare_canonical(request); - Box::pin(async move { result }) - } - fn append( - &self, - request: CanonicalLiveAppendRequest, - ) -> CanonicalFuture { - let mut store = self.store.lock().unwrap(); - let mut faults = self.faults.lock().unwrap(); - if matches!(faults.tool_attribution, ToolAttribution::AfterPrepare) - && matches!( - request.draft.entry(), - pa_types::session::FileEntry::Message { - message: pa_types::session::AgentMessage::ToolResult(_), - .. - } - ) - { - append_tool_attributions(&mut store); - faults.tool_attribution = ToolAttribution::None; - } - let lose_reply = faults.lose_append_reply; - drop(faults); - let result = store.append_canonical(request); - Box::pin(async move { - let result = result?; - if lose_reply { - Err(CanonicalStoreError { - kind: CanonicalFailureKind::AdoptionConflict, - requested: CanonicalRequestedWrite::Unknown, - source: CanonicalSourceDisposition::NeedsReconcile, - detail: "injected lost append receipt".into(), - }) - } else { - Ok(result) - } - }) - } - fn acknowledge( - &self, - mut request: CanonicalAcknowledgement, - ) -> CanonicalFuture { - let opening = - self.faults.lock().unwrap().opening_operation.as_ref() == Some(&request.operation); - let faults = self.faults.lock().unwrap(); - if opening && faults.foreign_run_ack { - request.operation = CanonicalOperation::new(); - } - let reply = match request.phase { - CanonicalAckPhase::AbandonPreparedOnly => faults.abandon_reply, - CanonicalAckPhase::Prepared if opening => faults.run_prepared_reply, - CanonicalAckPhase::AppliedReady if faults.lose_applied_ack => AbandonReply::Lost, - CanonicalAckPhase::Prepared - | CanonicalAckPhase::AppliedReady - | CanonicalAckPhase::ReconciledBlocked - | CanonicalAckPhase::Quarantined => AbandonReply::Exact, - }; - drop(faults); - let result = self.store.lock().unwrap().acknowledge_canonical(request); - Box::pin(async move { - let mut result = result?; - match reply { - AbandonReply::Lost => Err(CanonicalStoreError { - kind: CanonicalFailureKind::AdoptionConflict, - requested: CanonicalRequestedWrite::Unknown, - source: CanonicalSourceDisposition::NeedsReconcile, - detail: "injected lost acknowledgement reply".into(), - }), - AbandonReply::Foreign => { - result.operation = CanonicalOperation::new(); - Ok(result) - } - AbandonReply::Exact => Ok(result), - } - }) - } - fn register_run( - &self, - mut request: CanonicalLiveRunRequest, - ) -> CanonicalFuture { - let mut store = self.store.lock().unwrap(); - match std::mem::take(&mut self.faults.lock().unwrap().run_start) { - RunStartFault::None => {} - RunStartFault::LedgerOnly(id) => { - let before = store.canonical_snapshot().unwrap().checkpoint.cursor; - store.register_delivered_user(&id).unwrap(); - let after = store.canonical_snapshot().unwrap(); - assert!(after.checkpoint.cursor.canonical_revision > before.canonical_revision); - let mut expected = before; - expected.canonical_revision = after.checkpoint.cursor.canonical_revision; - assert_eq!(after.checkpoint.cursor, expected); - assert_eq!(after.pending_users.front(), Some(&id)); - } - RunStartFault::OwnedAdvance => { - store - .persist_owner_metadata( - "custom", - json!({"customType":"child_finished", "data":{}}), - ) - .unwrap(); - } - RunStartFault::ExternalAdvance => { - use std::io::Write; - std::fs::OpenOptions::new() - .append(true) - .open(&store.path) - .unwrap() - .write_all(b"\n") - .unwrap(); - } - RunStartFault::ForeignOwner => request.expected.owner = CanonicalOwnerScope::new(), - RunStartFault::ForgedPrefix => request.expected.frontier.prefix_certificate[0] ^= 1, - RunStartFault::ForgedPosition => request.expected.frontier.bytes -= 1, - RunStartFault::ForgedLeaf => { - request.expected.selected_leaf = Some("not-an-owner-row".into()); - } - RunStartFault::ForgedGeneration => request.expected.raw_source_generation[0] ^= 1, - } - let result = store.register_canonical_run(request); - if let Ok(prepared) = &result { - self.faults.lock().unwrap().opening_operation = Some(prepared.operation.clone()); - } - Box::pin(async move { result }) - } - fn admit_user( - &self, - request: CanonicalLiveUserRequest, - ) -> CanonicalFuture { - let result = self.store.lock().unwrap().admit_canonical_user(request); - Box::pin(async move { result }) - } - fn admit_custom( - &self, - request: CanonicalLiveCustomRequest, - ) -> CanonicalFuture { - let mut store = self.store.lock().unwrap(); - if std::mem::take(&mut self.faults.lock().unwrap().advance_at_custom) { - store - .persist_owner_metadata("custom", json!({"customType":"child_finished", "data":{}})) - .unwrap(); - } - let result = store.admit_canonical_custom(request); - Box::pin(async move { result }) - } - fn begin_provider_step( - &self, - request: CanonicalLiveProviderStepRequest, - ) -> CanonicalFuture<()> { - let mut store = self.store.lock().unwrap(); - if std::mem::take(&mut self.faults.lock().unwrap().advance_at_provider) { - store - .persist_owner_metadata("custom", json!({"customType":"child_finished", "data":{}})) - .unwrap(); - } - let result = store.begin_canonical_provider_step(request, false); - Box::pin(async move { result }) - } - fn admit_tool(&self, request: CanonicalLiveToolRequest) -> CanonicalFuture<()> { - let result = self.store.lock().unwrap().admit_canonical_tool(request); - Box::pin(async move { result }) - } -} -async fn runtime() -> ( - tempfile::TempDir, - Arc, - SessionEngine, - Arc, -) { - runtime_with_unregistered_user(false).await -} -async fn runtime_with_unregistered_user( - seed: bool, -) -> ( - tempfile::TempDir, - Arc, - SessionEngine, - Arc, -) { - let dir = tempfile::tempdir().unwrap(); - let cwd = dir.path().join("project"); - let agent_dir = dir.path().join("agent"); - std::fs::create_dir_all(&cwd).unwrap(); - std::fs::create_dir_all(&agent_dir).unwrap(); - std::fs::write( - agent_dir.join("settings.json"), - json!({ - "harnessRefinements":false, "enableBuiltinSkills":false, "prewarmIpythonKernel":false, - "contextStrategy":"default_context/rolling" - }) - .to_string(), - ) - .unwrap(); - let mut store = SessionFile::create(cwd.to_str().unwrap(), None, 0); - store.set_path(dir.path().join("session.jsonl")); - store.rewrite().unwrap(); - store.lease = Some(Arc::new( - crate::lease::acquire_runtime_session_lease(&store.path, dir.path()).unwrap(), - )); - if seed { - store - .persist_entry( - "message", - json!({"message": { - "role":"user", "content":[{"type":"text","text":"ledger A"}], "timestamp":1, - }}), - ) - .unwrap(); - } - store.attach_canonical_owner().unwrap(); - let manager = mirror(&store); - let owner = Arc::new(RealOwner { - store: Mutex::new(store), - faults: Mutex::new(Faults::default()), - }); - let model: pa_agent::types::Model = serde_json::from_value(json!({ - "id":"fake","name":"fake","api":"openai-completions","provider":"fake", - "baseUrl":"http://127.0.0.1:9/v1","reasoning":false,"input":["text"], - "cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0},"contextWindow":100_000,"maxTokens":100 - })).unwrap(); - let provider = Arc::new(pa_agent::scripted::ScriptedProvider::new(model.clone())); - provider.push_text_turn("next prompt succeeds"); - provider.push_text_turn("follow-up succeeds"); - let selection_owner = owner.clone(); - let engine = create_session(SessionEngineConfig { - cwd, - agent_dir, - model: Some(model), - session_manager: Some(manager), - canonical_live_owner: Some(owner.clone()), - on_initial_context_selection: Some(Arc::new(move |selection| { - let result = selection_owner - .store - .lock() - .unwrap() - .persist_owner_metadata( - "custom", - json!({"customType":"context_strategy_selection","data":selection}), - ); - Box::pin(async move { - result?; - Ok(()) - }) - })), - stream_fn: Some(provider.stream_fn()), - prewarm_ipython_kernel: Some(false), - daemonless: true, - ..Default::default() - }) - .await - .unwrap(); - (dir, owner, engine, provider) -} - -#[tokio::test] -async fn run_start_owned_advance_leaves_next_real_owner_prompt_ready() { - let (_dir, owner, engine, provider) = runtime().await; - owner.faults.lock().unwrap().run_start = RunStartFault::OwnedAdvance; - engine - .session - .prompt("first", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); - engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .unwrap(); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 2); - engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .unwrap(); -} - -#[tokio::test] -async fn run_opening_honors_owner_api_completed_ledger_only_advance() { - let (_dir, owner, engine, provider) = runtime_with_unregistered_user(true).await; - let id = owner - .store - .lock() - .unwrap() - .entries() - .iter() - .find(|entry| entry.type_ == "message" && entry.fields["message"]["role"] == "user") - .unwrap() - .id - .clone(); - let before = std::fs::read(&owner.store.lock().unwrap().path).unwrap(); - owner.faults.lock().unwrap().run_start = RunStartFault::LedgerOnly(id); - engine - .session - .prompt("ledger A", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); - assert!(owner - .store - .lock() - .unwrap() - .canonical_snapshot() - .unwrap() - .pending_users - .is_empty()); - assert!(std::fs::read(&owner.store.lock().unwrap().path) - .unwrap() - .starts_with(&before)); - engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .unwrap(); -} - -#[tokio::test] -async fn provider_owned_advance_leaves_next_real_owner_prompt_ready() { - let (_dir, owner, engine, provider) = runtime().await; - owner.faults.lock().unwrap().advance_at_provider = true; - assert!(engine - .session - .prompt("first", PromptOptions::default()) - .await - .is_err()); - assert!(provider.calls().is_empty()); - let path = owner.store.lock().unwrap().path.clone(); - let durable = SessionFile::open(&path).unwrap(); - let errors: Vec = durable - .entries() - .iter() - .filter_map(|row| { - let message = row.fields.get("message")?; - (message.get("role")?.as_str()? == "assistant" - && message.get("stopReason")?.as_str()? == "error") - .then(|| serde_json::from_value(message.clone()).unwrap()) - }) - .collect(); - assert_eq!(errors.len(), 1); - assert_eq!(errors[0].usage, pa_types::ai::Usage::default()); - assert!(errors[0].diagnostics.as_ref().unwrap()[0].error.is_some()); - engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .unwrap(); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); -} - -#[tokio::test] -async fn actual_tool_terminal_survives_child_attribution_without_rerunning_tool() { - struct EchoTool(Arc); - impl pa_agent::types::AgentTool for EchoTool { - fn name(&self) -> &'static str { - "echo" - } - fn description(&self) -> &'static str { - "echo the call" - } - fn parameters(&self) -> &serde_json::Value { - static PARAMETERS: std::sync::OnceLock = std::sync::OnceLock::new(); - PARAMETERS.get_or_init(|| json!({"type":"object"})) - } - fn execute( - self: Arc, - _tool_call_id: String, - _params: serde_json::Value, - _signal: pa_agent::abort::AbortSignal, - _on_update: pa_agent::types::AgentToolUpdateCallback, - ) -> pa_agent::BoxFut<'static, anyhow::Result> { - self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - Box::pin(async { Ok(pa_agent::types::AgentToolResult::text("tool output")) }) - } - } - for stage in [ - ToolAttribution::BeforePrepare, - ToolAttribution::AfterPrepare, - ] { - let (_dir, owner, engine, provider) = runtime().await; - engine - .session - .prompt("warmup one", PromptOptions::default()) - .await - .unwrap(); - engine - .session - .prompt("warmup two", PromptOptions::default()) - .await - .unwrap(); - let executions = Arc::new(std::sync::atomic::AtomicUsize::new(0)); - engine - .session - .agent() - .set_tools(vec![Arc::new(EchoTool(executions.clone()))]) - .await; - provider.push_tool_call_turn(None, vec![("actual-tool", "echo", json!({}))]); - provider.push_text_turn("tool finished"); - provider.push_text_turn("next prompt succeeds"); - owner.faults.lock().unwrap().tool_attribution = stage; - engine - .session - .prompt("call tool", PromptOptions::default()) - .await - .unwrap(); - engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .unwrap(); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(executions.load(std::sync::atomic::Ordering::SeqCst), 1); - let store = owner.store.lock().unwrap(); - assert_eq!( - store - .entries() - .iter() - .filter(|row| row.type_ == "child_usage_attributed") - .count(), - 2 - ); - assert_eq!( - store - .entries() - .iter() - .filter( - |row| row.type_ == "message" && row.fields["message"]["role"] == "toolResult" - ) - .count(), - 1 - ); - assert_eq!(provider.calls().len(), 5); - } -} - -#[tokio::test] -async fn owner_rejected_unwritten_draft_keeps_next_prompt_ready_after_exact_abandon() { - for reply in [ - AbandonReply::Exact, - AbandonReply::Lost, - AbandonReply::Foreign, - ] { - let (_dir, owner, engine, provider) = runtime().await; - let live = engine.session.canonical_live_session().unwrap(); - let before = std::fs::read(&owner.store.lock().unwrap().path).unwrap(); - owner.faults.lock().unwrap().abandon_reply = reply; - // The real owner refuses a native terminal submitted without actual run - // provenance before admitting any requested mutation or physical write. - let entry = serde_json::from_value(json!({ - "type":"message", "message":{ - "role":"toolResult", "toolCallId":"unadmitted", "toolName":"test", - "content":[{"type":"text","text":"not admitted"}], "isError":false, - "timestamp":1 - } - })) - .unwrap(); - let error = live - .append(CanonicalMutationKind::OwnerMetadata, entry) - .await - .unwrap_err(); - assert_eq!( - std::fs::read(&owner.store.lock().unwrap().path).unwrap(), - before - ); - assert!(provider.calls().is_empty()); - match reply { - AbandonReply::Lost | AbandonReply::Foreign => assert!(live.ensure_ready().is_err()), - AbandonReply::Exact => { - assert_eq!(error.kind, CanonicalFailureKind::InvalidRequest); - assert_eq!(error.requested, CanonicalRequestedWrite::NotWritten); - assert_eq!(error.source, CanonicalSourceDisposition::Unchanged); - assert_eq!( - error.detail, - "native terminal request has no actual run provenance" - ); - live.ensure_ready().unwrap(); - owner.store.lock().unwrap().canonical_snapshot().unwrap(); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); - } - } - } -} - -#[tokio::test] -async fn prepared_context_owned_advance_requires_exact_abandon_before_next_prompt() { - for reply in [ - AbandonReply::Exact, - AbandonReply::Lost, - AbandonReply::Foreign, - ] { - let (_dir, owner, engine, provider) = runtime().await; - let live = engine.session.canonical_live_session().unwrap(); - let ticket = live - .prepare_mutation(CanonicalMutationKind::ContextPolicy) - .await - .unwrap(); - owner - .store - .lock() - .unwrap() - .persist_owner_metadata("custom", json!({"customType":"child_finished", "data":{}})) - .unwrap(); - let before = std::fs::read(&owner.store.lock().unwrap().path).unwrap(); - owner.faults.lock().unwrap().abandon_reply = reply; - let draft = CanonicalEntryDraft::new( - CanonicalMutationKind::ContextPolicy, - serde_json::from_value(json!({ - "type":"compaction", "summary":"paid once", "firstKeptEntryId":"x", "tokensBefore":1 - })) - .unwrap(), - ) - .unwrap(); - assert!(ticket.commit(draft).await.is_err()); - assert_eq!( - std::fs::read(&owner.store.lock().unwrap().path).unwrap(), - before - ); - assert!(provider.calls().is_empty()); - match reply { - AbandonReply::Lost | AbandonReply::Foreign => assert!(live.ensure_ready().is_err()), - AbandonReply::Exact => { - live.ensure_ready().unwrap(); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); - } - } - } -} - -#[tokio::test] -async fn prepared_context_unowned_source_change_stays_blocked() { - use std::io::Write; - let (_dir, owner, engine, provider) = runtime().await; - let live = engine.session.canonical_live_session().unwrap(); - let ticket = live - .prepare_mutation(CanonicalMutationKind::ContextPolicy) - .await - .unwrap(); - let path = owner.store.lock().unwrap().path.clone(); - std::fs::OpenOptions::new() - .append(true) - .open(&path) - .unwrap() - .write_all(b"\n") - .unwrap(); - let before = std::fs::read(&path).unwrap(); - let draft = CanonicalEntryDraft::new( - CanonicalMutationKind::ContextPolicy, - serde_json::from_value(json!({ - "type":"compaction", "summary":"never commit", "firstKeptEntryId":"x", "tokensBefore":1 - })) - .unwrap(), - ) - .unwrap(); - assert!(ticket.commit(draft).await.is_err()); - assert!(live.ensure_ready().is_err()); - assert_eq!(std::fs::read(path).unwrap(), before); - assert!(provider.calls().is_empty()); -} - -#[tokio::test] -async fn run_start_unowned_source_or_foreign_owner_stays_blocked() { - for fault in [ - RunStartFault::ExternalAdvance, - RunStartFault::ForeignOwner, - RunStartFault::ForgedPrefix, - RunStartFault::ForgedPosition, - RunStartFault::ForgedLeaf, - RunStartFault::ForgedGeneration, - ] { - let (_dir, owner, engine, provider) = runtime().await; - owner.faults.lock().unwrap().run_start = fault; - assert!(engine - .session - .prompt("first", PromptOptions::default()) - .await - .is_err()); - assert!(engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .is_err()); - assert!(provider.calls().is_empty()); - } -} - -#[tokio::test] -async fn precomputed_metadata_source_advance_is_certified_after_exact_abandon() { - for reply in [ - AbandonReply::Exact, - AbandonReply::Lost, - AbandonReply::Foreign, - ] { - let (_dir, owner, engine, provider) = runtime().await; - let live = engine.session.canonical_live_session().unwrap(); - live.observe().await.unwrap(); - let expected = owner - .store - .lock() - .unwrap() - .canonical_snapshot() - .unwrap() - .checkpoint - .cursor; - owner - .store - .lock() - .unwrap() - .persist_owner_metadata("custom", json!({"customType":"child_finished", "data":{}})) - .unwrap(); - owner.faults.lock().unwrap().abandon_reply = reply; - let before = std::fs::read(&owner.store.lock().unwrap().path).unwrap(); - let Err(error) = CanonicalMetadataWriter::append_metadata_from( - live.clone(), - custom_draft().into_entry(), - expected, - ) - .await - else { - panic!("stale metadata intent must not be appended"); - }; - assert_eq!( - std::fs::read(&owner.store.lock().unwrap().path).unwrap(), - before - ); - match reply { - AbandonReply::Exact => { - assert_eq!(error.requested, CanonicalRequestedWrite::NotRequested); - assert_eq!( - error.source, - CanonicalSourceDisposition::ReadyForObservation - ); - live.ensure_ready().unwrap(); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); - } - AbandonReply::Lost | AbandonReply::Foreign => { - assert!(live.ensure_ready().is_err()); - assert_ne!( - error.source, - CanonicalSourceDisposition::ReadyForObservation - ); - assert!(provider.calls().is_empty()); - } - } - } -} - -#[test] -fn lifecycle_diagnostic_round_trips_through_canonical_error_schema() { - let diagnostic = pa_agent::types::assistant_message_diagnostic( - "agent_lifecycle_failure", - &anyhow::anyhow!("inner").context("outer"), - Some(json!({"source":"run_with_lifecycle"})), - ); - let actual: pa_types::ai::AssistantMessageDiagnostic = - serde_json::from_value(serde_json::to_value(&diagnostic).unwrap()).unwrap(); - assert_eq!( - actual, - pa_types::ai::AssistantMessageDiagnostic { - type_: "agent_lifecycle_failure".into(), - timestamp: diagnostic.timestamp.try_into().unwrap(), - error: Some(pa_types::ai::DiagnosticErrorInfo { - name: None, - message: "outer: inner".into(), - stack: None, - code: None, - rest: serde_json::Map::default(), - }), - details: Some(serde_json::from_value(json!({"source":"run_with_lifecycle"})).unwrap()), - rest: serde_json::Map::default(), - } - ); -} - -#[tokio::test] -async fn committed_metadata_with_lost_receipt_or_ack_stays_blocked_without_replay() { - for lose_append_reply in [true, false] { - let (_dir, owner, engine, provider) = runtime().await; - let live = engine.session.canonical_live_session().unwrap(); - let before = owner.store.lock().unwrap().entries().len(); - { - let mut faults = owner.faults.lock().unwrap(); - faults.lose_append_reply = lose_append_reply; - faults.lose_applied_ack = !lose_append_reply; - } - assert!(live - .append( - CanonicalMutationKind::OwnerMetadata, - custom_draft().into_entry() - ) - .await - .is_err()); - assert!(live.ensure_ready().is_err()); - let path = owner.store.lock().unwrap().path.clone(); - let bytes = std::fs::read(&path).unwrap(); - assert_eq!( - SessionFile::open(&path).unwrap().entries().len(), - before + 1 - ); - assert!(owner.store.lock().unwrap().canonical_snapshot().is_err()); - assert!(live - .append( - CanonicalMutationKind::OwnerMetadata, - custom_draft().into_entry() - ) - .await - .is_err()); - assert_eq!(std::fs::read(&path).unwrap(), bytes); - assert!(provider.calls().is_empty()); - } -} - -#[tokio::test] -async fn run_opening_lost_or_foreign_prepared_ack_never_reopens_or_replays() { - for (reply, foreign_request) in [ - (AbandonReply::Lost, false), - (AbandonReply::Foreign, false), - (AbandonReply::Exact, true), - ] { - let (_dir, owner, engine, provider) = runtime().await; - owner - .store - .lock() - .unwrap() - .persist_delivered_user(json!({ - "role":"user", "content":[{"type":"text","text":"accepted A"}], "timestamp":1, - })) - .unwrap(); - { - let mut faults = owner.faults.lock().unwrap(); - faults.run_start = RunStartFault::OwnedAdvance; - faults.run_prepared_reply = reply; - faults.foreign_run_ack = foreign_request; - } - assert!(engine - .session - .prompt("accepted A", PromptOptions::default()) - .await - .is_err()); - let live = engine.session.canonical_live_session().unwrap(); - assert!(live.ensure_ready().is_err()); - assert!(provider.calls().is_empty()); - let bytes = { - let store = owner.store.lock().unwrap(); - assert!(store.canonical_snapshot().is_err()); - std::fs::read(&store.path).unwrap() - }; - assert!(engine - .session - .prompt("different B", PromptOptions::default()) - .await - .is_err()); - assert_eq!( - std::fs::read(&owner.store.lock().unwrap().path).unwrap(), - bytes - ); - assert!(provider.calls().is_empty()); - } -} - -#[tokio::test] -async fn accepted_user_survives_run_start_source_advance_before_a_different_followup() { - let (_dir, owner, engine, provider) = runtime().await; - owner - .store - .lock() - .unwrap() - .persist_delivered_user(json!({ - "role":"user", "content":[{"type":"text","text":"accepted A"}], "timestamp":1, - })) - .unwrap(); - owner.faults.lock().unwrap().run_start = RunStartFault::OwnedAdvance; - engine - .session - .prompt("accepted A", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); - owner - .store - .lock() - .unwrap() - .persist_delivered_user(json!({ - "role":"user", "content":[{"type":"text","text":"different B"}], "timestamp":2, - })) - .unwrap(); - engine - .session - .prompt("different B", PromptOptions::default()) - .await - .unwrap(); - engine - .session - .canonical_live_session() - .unwrap() - .ensure_ready() - .unwrap(); - assert!(owner - .store - .lock() - .unwrap() - .canonical_snapshot() - .unwrap() - .pending_users - .is_empty()); - let calls = provider.calls(); - assert_eq!(calls.len(), 2); - let first = serde_json::to_value(&calls[0].messages) - .unwrap() - .to_string(); - let second = serde_json::to_value(&calls[1].messages) - .unwrap() - .to_string(); - assert_eq!(first.matches("accepted A").count(), 1); - assert_eq!(first.matches("different B").count(), 0); - assert_eq!(second.matches("accepted A").count(), 1); - assert_eq!(second.matches("different B").count(), 1); - let store = owner.store.lock().unwrap(); - let durable_users = store - .entries() - .iter() - .filter(|entry| entry.type_ == "message" && entry.fields["message"]["role"] == "user") - .map(|entry| entry.fields["message"].to_string()) - .collect::>(); - assert_eq!(durable_users.len(), 2); - assert_eq!( - durable_users - .iter() - .filter(|row| row.contains("accepted A")) - .count(), - 1 - ); - assert_eq!( - durable_users - .iter() - .filter(|row| row.contains("different B")) - .count(), - 1 - ); -} - -#[tokio::test] -async fn goal_source_advance_before_first_write_keeps_real_owner_driver_retryable() { - use pa_core::session_engine::goal_driver::persist_goal_mutation; - let (_dir, owner, engine, provider) = runtime().await; - let session = engine.session.shared_persistence(); - let writer_owner = owner.clone(); - let result = persist_goal_mutation(&engine.goal_driver, &session, move |driver, drafts| { - writer_owner - .store - .lock() - .unwrap() - .persist_owner_metadata("custom", json!({"customType":"child_finished", "data":{}}))?; - driver.start(drafts, "first intent must be refused", None) - }) - .await; - assert!(result.is_err()); - engine.goal_driver.lock().await.ensure_ready().unwrap(); - assert!(!engine.goal_driver.lock().await.state().active); - persist_goal_mutation(&engine.goal_driver, &session, |driver, drafts| { - driver.start(drafts, "next intent succeeds", None) - }) - .await - .unwrap(); - assert!(engine.goal_driver.lock().await.state().active); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(provider.calls().len(), 1); -} - -/// The production incident shape: child-usage attribution or a staged delivery -/// appends a legitimate owner row between Core's observation and the owner's -/// Custom admission check, while a tool execution is still outstanding. The -/// requested Custom row was never written, so the refusal must stay -/// recoverable instead of quarantining the live view. -#[tokio::test] -async fn custom_admission_behind_owner_append_during_tool_execution_stays_recoverable() { - struct CustomDuringTool { - live: Arc, - outcome: Arc>>>, - executions: Arc, - } - impl pa_agent::types::AgentTool for CustomDuringTool { - fn name(&self) -> &'static str { - "send_custom" - } - fn description(&self) -> &'static str { - "admit one Custom message while this tool runs" - } - fn parameters(&self) -> &serde_json::Value { - static PARAMETERS: std::sync::OnceLock = std::sync::OnceLock::new(); - PARAMETERS.get_or_init(|| json!({"type":"object"})) - } - fn execute( - self: Arc, - _tool_call_id: String, - _params: serde_json::Value, - _signal: pa_agent::abort::AbortSignal, - _on_update: pa_agent::types::AgentToolUpdateCallback, - ) -> pa_agent::BoxFut<'static, anyhow::Result> { - self.executions - .fetch_add(1, std::sync::atomic::Ordering::SeqCst); - Box::pin(async move { - let message: pa_types::session::AgentMessage = serde_json::from_value(json!({ - "role":"custom", "customType":"agent_message", - "content":[{"type":"text","text":"queued while the tool runs"}], - "display":false, "timestamp":9 - }))?; - let pa_types::session::AgentMessage::Custom(message) = message else { - unreachable!("fixture must contain a tagged Custom") - }; - let result = self - .live - .persist_message(pa_types::session::AgentMessage::Custom(message)) - .await; - *self.outcome.lock().unwrap() = Some(result); - Ok(pa_agent::types::AgentToolResult::text("custom attempted")) - }) - } - } - - let (_dir, owner, engine, provider) = runtime().await; - // Consume both scripted text turns seeded by the runtime fixture so the next - // scripted turn is the actual tool call. - engine - .session - .prompt("warmup one", PromptOptions::default()) - .await - .unwrap(); - engine - .session - .prompt("warmup two", PromptOptions::default()) - .await - .unwrap(); - let live = engine.session.canonical_live_session().unwrap(); - let outcome = Arc::new(Mutex::new(None)); - let executions = Arc::new(std::sync::atomic::AtomicUsize::new(0)); - engine - .session - .agent() - .set_tools(vec![Arc::new(CustomDuringTool { - live: live.clone(), - outcome: outcome.clone(), - executions: executions.clone(), - })]) - .await; - provider.push_tool_call_turn(None, vec![("custom-tool", "send_custom", json!({}))]); - provider.push_text_turn("tool finished"); - owner.faults.lock().unwrap().advance_at_custom = true; - engine - .session - .prompt("call tool", PromptOptions::default()) - .await - .unwrap(); - assert_eq!(executions.load(std::sync::atomic::Ordering::SeqCst), 1); - let error = outcome - .lock() - .unwrap() - .take() - .expect("the tool must attempt one Custom admission") - .expect_err("the owner must refuse the raced Custom admission"); - assert_eq!(error.kind, CanonicalFailureKind::SourceChanged); - assert_eq!(error.requested, CanonicalRequestedWrite::NotRequested); - assert_eq!( - error.source, - CanonicalSourceDisposition::ReadyForObservation - ); - assert_eq!( - error.detail, - "canonical Custom admission source CAS conflict" - ); - live.ensure_ready().unwrap(); - provider.push_text_turn("next prompt succeeds"); - engine - .session - .prompt("next", PromptOptions::default()) - .await - .unwrap(); -} diff --git a/crates/pa-daemon/src/session_store/canonical_row_grant_tests.rs b/crates/pa-daemon/src/session_store/canonical_row_grant_tests.rs deleted file mode 100644 index 30792f86d..000000000 --- a/crates/pa-daemon/src/session_store/canonical_row_grant_tests.rs +++ /dev/null @@ -1,409 +0,0 @@ -//! The store's canonical admission boundary: one grant per row, one count when -//! there is no epoch. -//! -//! These tests do not re-prove Core's authority. They prove the seam this store -//! owns: a row written with no epoch moves `rows_written_without_epoch`, a turn's -//! user row rides a real prepare-phase flight (so the count does not move and the -//! epoch counts the write), and an ordinary row takes a requested-mutation grant. - -use std::sync::Arc; - -use pa_core::session::canonical::{CanonicalCursor, CanonicalFrontier, CanonicalOwnerScope}; -use pa_core::session_engine::canonical_coordinator::CanonicalEpoch; -use serde_json::json; - -use super::canonical_adapter::EpochHandle; -use super::SessionFile; - -fn cursor() -> CanonicalCursor { - CanonicalCursor { - owner: CanonicalOwnerScope::new(), - session_id: "row-grant-session".to_owned(), - source_path: std::path::PathBuf::from("/private/row-grant.jsonl"), - raw_source_generation: [7; 32], - header_certificate: [8; 32], - canonical_revision: 0, - branch_revision: 1, - selected_leaf: None, - frontier: CanonicalFrontier { - bytes: 9, - rows: 1, - prefix_certificate: [9; 32], - }, - } -} - -fn fixture() -> (tempfile::TempDir, SessionFile) { - let dir = tempfile::tempdir().expect("temp dir"); - let mut store = SessionFile::create("/synthetic", None, 0); - store.set_path(dir.path().join("session.jsonl")); - store.rewrite().expect("rewrite"); - (dir, store) -} - -fn epoch() -> Arc { - Arc::new( - CanonicalEpoch::genesis( - &cursor(), - Arc::new(String::from("run")), - Arc::new(String::from("model")), - ) - .expect("epoch"), - ) -} - -fn user_row() -> serde_json::Value { - json!({ - "message": { - "role": "user", - "content": [{"type": "text", "text": "hello"}], - "timestamp": 1 - } - }) -} - -fn assistant_row() -> serde_json::Value { - json!({ - "message": { - "role": "assistant", - "content": [{"type": "text", "text": "hi"}], - "api": "openai-completions", - "provider": "synthetic", - "model": "synthetic", - "stopReason": "stop", - "timestamp": 2, - "usage": serde_json::to_value(pa_types::ai::Usage::default()).expect("usage") - } - }) -} - -/// No epoch attached: the row lands and the counter states the fact. -#[test] -fn a_row_written_with_no_epoch_is_counted_once() { - let (_dir, mut store) = fixture(); - let before = store.rows_written_without_epoch(); - store.persist_entry("message", user_row()).expect("row"); - assert_eq!(store.rows_written_without_epoch(), before + 1); -} - -/// The turn's user row rides a prepare-phase flight: it is covered, so the -/// counter does not move, and the epoch does not poison. -#[test] -fn a_turn_user_row_does_not_increment_the_without_epoch_count() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - store - .persist_entry("message", user_row()) - .expect("user row"); - assert_eq!( - store.rows_written_without_epoch(), - 0, - "the user row is covered by a prepare-phase flight" - ); - assert!(!epoch.poisoned(), "a classified flight must not poison"); - let closing = epoch.request_close(); - assert_eq!( - closing.view(), - pa_core::session_engine::canonical_coordinator::CanonicalDrainView::Pending { - io: 0, - transactions: 0, - anchors: 1, - }, - "the flight settled, so only the live serving anchor remains" - ); -} - -/// A context-policy row takes a real requested-mutation grant. -#[test] -fn a_compaction_row_takes_a_requested_mutation_grant_and_is_not_counted() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - store - .persist_entry( - "compaction", - json!({ - "summary": "row-grant", - "firstKeptEntryId": "a", - "tokensBefore": 1, - "details": {"bytes": "AA=="} - }), - ) - .expect("compaction row"); - assert_eq!(store.rows_written_without_epoch(), 0); - assert!(!epoch.poisoned()); -} - -/// An assistant row is the native final output: it takes the provider step's -/// terminal grant, is NOT counted, and leaves the epoch clean - with exactly one -/// flight, which a close observes. -#[test] -fn an_assistant_row_takes_the_provider_step_grant() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - let step = epoch - .admit_provider_step(Arc::new(String::from("actual provider step"))) - .expect("admitted step"); - let before = store.rows_written_without_epoch(); - store - .persist_message_with_step(assistant_row(), &step) - .expect("assistant row"); - assert_eq!( - store.rows_written_without_epoch(), - before, - "the assistant row is covered by the step's grant, so the count does not move" - ); - assert!( - !epoch.poisoned(), - "a settled terminal flight must not poison" - ); - let closing = epoch.request_close(); - assert_eq!( - closing.view(), - pa_core::session_engine::canonical_coordinator::CanonicalDrainView::Pending { - io: 0, - transactions: 0, - anchors: 1, - }, - "the terminal flight settled; only the live serving anchor remains" - ); -} - -/// A missing step under a LIVE epoch is a bug to surface, not a row to write: -/// the refusal names the bug, nothing is written, and the count does not move. -#[test] -fn an_assistant_row_without_a_step_is_refused_under_a_live_epoch() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - let before = store.rows_written_without_epoch(); - let refused = store - .persist_entry("message", assistant_row()) - .expect_err("an assistant row without a provider step must not be written"); - let message = format!("{refused:#}"); - assert!( - message.contains("admitted no provider step"), - "the refusal names the bug, got: {message}" - ); - assert_eq!( - store.rows_written_without_epoch(), - before, - "a refused row is not counted as written" - ); - assert!( - !epoch.poisoned(), - "no flight was opened, so nothing can poison" - ); -} - -/// A branch summary is context policy at HEAD: `Purpose::ContextPolicy` accepts -/// `Compaction | BranchSummary` and no metadata action matches it, so it takes the -/// same requested-mutation grant as a compaction row and is not counted. -#[test] -fn a_branch_summary_row_takes_a_requested_mutation_grant_and_is_not_counted() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - store - .persist_entry( - "branch_summary", - json!({"fromId": "a", "summary": "branch"}), - ) - .expect("branch summary row"); - assert_eq!(store.rows_written_without_epoch(), 0); - assert!(!epoch.poisoned()); -} - -/// The sealed entry and the persisted row are the SAME payload. -/// -/// Byte equality is impossible by design: the draft rejects a supplied envelope -/// (`id`, `parentId`, `timestamp`), and the store mints those at write time. So the -/// comparison removes exactly those three keys from BOTH sides and requires the -/// remainder to be equal as a whole value - field-by-field assertions pass when a -/// payload field is dropped, because nothing looks for the field that vanished. -fn assert_same_payload(draft_entry: &serde_json::Value, persisted: &serde_json::Value) { - let mut left = draft_entry.clone(); - let mut right = persisted.clone(); - let mut removed_left = 0; - let mut removed_right = 0; - for key in ["id", "parentId", "timestamp"] { - if left.as_object_mut().expect("object").remove(key).is_some() { - removed_left += 1; - } - if right.as_object_mut().expect("object").remove(key).is_some() { - removed_right += 1; - } - } - assert_eq!( - removed_left, 3, - "the draft must carry exactly three envelope keys to strip" - ); - assert_eq!( - removed_right, 3, - "the persisted row must carry exactly three envelope keys; a new one fails here instead of being compared" - ); - assert_eq!(left, right, "the payload must be identical on both sides"); -} - -/// CASE A: a row with a parent. The persisted row carries `parentId` and it is -/// non-empty, and the payload matches the sealed draft exactly. -#[test] -fn the_sealed_entry_matches_the_persisted_row_for_a_parented_assistant_row() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - // A parent the assistant row can hang from. - store - .persist_entry("message", user_row()) - .expect("user row"); - let step = epoch - .admit_provider_step(Arc::new(String::from("actual provider step"))) - .expect("admitted step"); - store - .persist_message_with_step(assistant_row(), &step) - .expect("assistant row"); - let entry = store.entries.last().expect("persisted row").clone(); - let persisted = serde_json::to_value(&entry).expect("row value"); - let draft = super::canonical_adapter::draft_for_row(&entry).expect("draft"); - let draft_entry = serde_json::to_value(draft.entry()).expect("draft value"); - assert_same_payload(&draft_entry, &persisted); - let parent = persisted["parentId"].as_str().expect("parented row"); - assert!(!parent.is_empty(), "a parented row's parentId is non-empty"); - assert!( - persisted["id"].as_str().is_some_and(|id| !id.is_empty()), - "the store assigned an id" - ); - assert!( - persisted["timestamp"] - .as_str() - .is_some_and(|t| !t.is_empty()), - "the store assigned a timestamp" - ); -} - -/// CASE B: the session's FIRST row. `parentId` is absent or null - asserted as the -/// expected shape rather than skipped, so a conditional cannot become a loophole. -#[test] -fn the_sealed_entry_matches_the_persisted_row_for_the_first_row() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - let step = epoch - .admit_provider_step(Arc::new(String::from("actual provider step"))) - .expect("admitted step"); - store - .persist_message_with_step(assistant_row(), &step) - .expect("first row"); - let entry = store.entries.last().expect("persisted row").clone(); - let persisted = serde_json::to_value(&entry).expect("row value"); - let draft = super::canonical_adapter::draft_for_row(&entry).expect("draft"); - let draft_entry = serde_json::to_value(draft.entry()).expect("draft value"); - assert_same_payload(&draft_entry, &persisted); - assert!( - persisted - .get("parentId") - .is_none_or(serde_json::Value::is_null), - "the session's first row has no parent, and that absence is the asserted shape" - ); - assert!( - persisted["id"].as_str().is_some_and(|id| !id.is_empty()), - "the store assigned an id" - ); -} - -/// The counter must not move across a close: request, boundary, drain, finish, with -/// no turn in flight. A stale write after the close is the failure the count exists -/// for. -#[tokio::test] -async fn the_without_epoch_count_is_stable_across_the_close() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - store - .persist_entry("message", user_row()) - .expect("covered user row"); - let before = store.rows_written_without_epoch(); - // The store gives the epoch up at shutdown and the ordered boundary consumes it. - // Both are required: `wait_drained` WAITS rather than reporting `Pending`, so even - // one surviving handle hangs the close instead of describing it. - let handle = store.epoch.take(); - let closing = epoch.request_close(); - drop(handle); - let Ok(epoch) = Arc::try_unwrap(epoch) else { - panic!("the store held the only other handle") - }; - epoch.finish_ordered_boundary(); - assert_eq!( - closing.wait_drained().await, - pa_core::session_engine::canonical_coordinator::CanonicalDrainView::Drained, - "the ordered boundary released the serving anchor, so the drain completes" - ); - assert!( - closing.finish().is_ok(), - "a drained epoch mints its retired token" - ); - assert_eq!( - store.rows_written_without_epoch(), - before, - "no row may slip in after the close was requested" - ); -} - -/// Dropping an epoch is NOT closing it, and the difference is a poison. -/// -/// `LateRunAnchor::drop` poisons the shared state and quarantines projection unless -/// the anchor took its ordered boundary first. This test pins that, so nobody -/// "simplifies" the shutdown drain into a drop: abandoned anchors are deliberate -/// quarantine, not a cleanup path. -#[test] -fn an_abandoned_epoch_poisons_and_the_ordered_boundary_does_not() { - use pa_core::session_engine::canonical_coordinator::CanonicalDrainView; - let abandoned = epoch(); - let abandoned_closing = abandoned.request_close(); - drop(abandoned); - assert_eq!( - abandoned_closing.view(), - CanonicalDrainView::Blocked, - "an abandoned anchor poisons the epoch: the drain can never succeed after it" - ); - - let ordered = Arc::try_unwrap(epoch()).ok().expect("unique epoch"); - let ordered_closing = ordered.request_close(); - ordered.finish_ordered_boundary(); - assert_eq!( - ordered_closing.view(), - CanonicalDrainView::Drained, - "the ordered boundary releases the anchor without poisoning it" - ); -} - -/// The read refusal is typed and names the reason: a poisoned epoch may not serve -/// its tail as if the bytes were classified. -#[test] -fn a_poisoned_epoch_refuses_the_read_with_needs_reconcile() { - let (_dir, mut store) = fixture(); - let epoch = epoch(); - // An unclassified flight drop poisons: this is the uncertain write. - drop( - epoch - .begin_prepare_append(pa_core::session::canonical::CanonicalIoStage::PreparePreflight) - .expect("flight"), - ); - assert!(epoch.poisoned(), "an unrecorded drop poisons the epoch"); - store.epoch = Some(EpochHandle(Arc::clone(&epoch))); - let refusal = super::canonical_adapter::refused_poisoned_read(); - assert_eq!( - refusal.source, - pa_core::session::canonical::CanonicalSourceDisposition::NeedsReconcile - ); - assert_eq!( - refusal.detail, - "canonical epoch is poisoned by an uncertain write" - ); - assert!( - super::canonical_adapter::epoch_is_poisoned(store.epoch.as_ref()), - "the live store sees the poison, so the read path refuses" - ); -} diff --git a/crates/pa-daemon/src/session_store/child_usage.rs b/crates/pa-daemon/src/session_store/child_usage.rs index 652c96257..a502cabaf 100644 --- a/crates/pa-daemon/src/session_store/child_usage.rs +++ b/crates/pa-daemon/src/session_store/child_usage.rs @@ -9,73 +9,6 @@ use pa_types::{ ai::Usage, session::{ChildUsageAttributionEntry, ChildUsageOrigin}, }; -use std::collections::HashSet; -use std::io::{Read, Seek, SeekFrom, Write}; -use std::path::PathBuf; - -/// Attribution-local recovery state. One write can remain unresolved; recovered -/// creation acknowledgments are bounded by the attribution rows retained by this -/// store and are removed when their own frozen report retries successfully. -/// There is no fixed global cap: the normal emitter owns one frozen failed report -/// per child and stops at its first failed batch. Dropping that owner prematurely -/// can retain its ID until this store is dropped; terminal ownership is separate. -#[derive(Debug, Clone, Default)] -pub(super) struct ChildUsageRecovery { - pub(super) pending: Option, - pub(super) created: HashSet, -} - -#[derive(Debug, Clone)] -pub(super) struct UnconfirmedChildRow { - path: PathBuf, - header: serde_json::Value, - header_wire: serde_json::Value, - identity: Option<(u64, u64)>, - start: Option, - prefix_end: u64, - prefix_tail: Vec, - entry: SessionEntry, - bytes: Vec, -} - -fn file_identity(metadata: &std::fs::Metadata) -> (u64, u64) { - #[cfg(unix)] - { - use std::os::unix::fs::MetadataExt; - (metadata.dev(), metadata.ino()) - } - #[cfg(windows)] - { - use std::os::windows::fs::MetadataExt; - // Creation time is a limited replacement anchor, not a unique Windows - // file ID. Owned path/header/prefix/row checks and the existing managed - // single-writer lease remain necessary; noncooperative writers are out - // of this attribution-local recovery contract. - (metadata.creation_time(), 0) - } - #[cfg(not(any(unix, windows)))] - { - let _ = metadata; - (0, 0) - } -} - -/// Compare complete headers with the actual wire tag included. The frozen -/// original and its one wire decode cover reader float normalization without -/// decoding an already loaded header a second time or ignoring extension fields. -fn header_matches_receipt(header: &super::SessionHeader, receipt: &UnconfirmedChildRow) -> bool { - let actual = super::session_header_line(header); - actual == receipt.header || actual == receipt.header_wire -} - -/// Recovery observation updates the index; confirmation also owns the final -/// durability proof and acknowledgment bookkeeping within the same lease flight. -#[derive(Clone, Copy, PartialEq, Eq)] -enum ChildUsageRecoveryMode { - Observe, - Confirm, -} - impl SessionFile { /// Append one attribution row once: the row id is the retry identity, so /// a re-delivered report finds its own row instead of billing the child @@ -97,7 +30,6 @@ impl SessionFile { origin: Option, ) -> Result { ensure!(!row_id.is_empty(), "attribution ID must not be empty"); - self.confirm_child_usage_write()?; if let Some(entry) = self.entry(row_id) { ensure!( entry.type_ == "child_usage_attributed", @@ -109,11 +41,7 @@ impl SessionFile { "attribution ID collision" ); let current = self.attribution_target_usage(target_id)?; - return Ok(if self.child_usage_recovery.created.remove(row_id) { - ChildUsageAppendResult::Created(current) - } else { - ChildUsageAppendResult::Existing(current) - }); + return Ok(ChildUsageAppendResult::Existing(current)); } let mut aggregate = self.attribution_target_usage(target_id)?; attribute_child_usage(&mut aggregate, &child_usage); @@ -129,328 +57,10 @@ impl SessionFile { origin, })?, }; - if self.log_transport.is_some() { - // THE transport switch: one durable log row, indexed only once - // the log committed it (the receipt/forensics machinery below is - // the canonical transport's recovery device; the log core's - // verdict replaces it). - self.persist_built_log_entry(entry)?; - return Ok(ChildUsageAppendResult::Created(aggregate)); - } - if self.is_canonical_bound() { - // The report ID already IS this journal row's stable identity. The - // exact named owner action writes it once and leaves the checkpoint - // pending until Core's next NOIO adoption and matching Prepared ACK. - self.stage_owned_entry(entry)?; - return Ok(ChildUsageAppendResult::Created(aggregate)); - } - // Capture only this owned row, not the transcript. The EOF anchor and - // existing file identity distinguish an absent attempt from a partial - // or foreign change. Bootstrap rewrite has no existing append offset. - let receipt = if self.path.as_os_str().is_empty() { - None - } else { - let header = super::session_header_line(&self.header); - let header_wire = serde_json::from_slice(&serde_json::to_vec(&header)?)?; - let mut receipt = UnconfirmedChildRow { - path: self.path.clone(), - header, - header_wire, - identity: None, - start: None, - prefix_end: 0, - prefix_tail: Vec::new(), - bytes: serde_json::to_vec(&entry)?, - entry: entry.clone(), - }; - receipt.bytes.push(b'\n'); - match std::fs::File::open(&self.path) { - Ok(mut file) => { - let metadata = file.metadata()?; - receipt.identity = Some(file_identity(&metadata)); - receipt.prefix_end = metadata.len(); - let tail_len = metadata.len().min(16) as usize; - file.seek(SeekFrom::End(-(tail_len as i64)))?; - receipt.prefix_tail.resize(tail_len, 0); - file.read_exact(&mut receipt.prefix_tail)?; - receipt.start = Some( - metadata.len() - + u64::from( - metadata.len() > 0 && receipt.prefix_tail.last() != Some(&b'\n'), - ), - ); - } - Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} - Err(error) => return Err(error.into()), - } - Some(receipt) - }; - if let Err(error) = self.persist_built_entry(entry) { - self.child_usage_recovery.pending = receipt; - // A completed own row must affect the next aggregate before this - // store lock is released, even though its original caller sees Err. - if let Err(recovery) = self.recover_child_usage_write(ChildUsageRecoveryMode::Observe) { - return Err(error.context(format!("attribution remains unresolved: {recovery}"))); - } - return Err(error); - } + self.persist_built_entry(entry)?; Ok(ChildUsageAppendResult::Created(aggregate)) } - /// Confirm an unresolved own row before another durable mutation or ACK. - pub(super) fn confirm_child_usage_write(&mut self) -> Result<()> { - self.recover_child_usage_write(ChildUsageRecoveryMode::Confirm) - } - - /// Observe only the frozen owned row; no lenient whole-file scan or truncation. - fn recover_child_usage_write(&mut self, mode: ChildUsageRecoveryMode) -> Result<()> { - let Some(receipt) = self.child_usage_recovery.pending.clone() else { - return Ok(()); - }; - let lease = self.lease.clone(); - let path = self.path.clone(); - let observe = || { - ensure!( - self.path == receipt.path && header_matches_receipt(&self.header, &receipt), - "attribution recovery session changed" - ); - let mut file = match std::fs::OpenOptions::new() - .read(true) - .append(true) - .open(&receipt.path) - { - Ok(file) => file, - Err(error) - if error.kind() == std::io::ErrorKind::NotFound && receipt.start.is_none() => - { - self.child_usage_recovery.pending = None; - return Ok(()); - } - Err(error) => return Err(error.into()), - }; - let metadata = file.metadata()?; - if let Some(identity) = receipt.identity { - ensure!( - file_identity(&metadata) == identity, - "attribution recovery file replaced" - ); - } - let header_line = super::read_first_line_bounded_from( - &mut file, - super::SESSION_LIST_HEADER_READ_MAX_BYTES, - ) - .context("attribution recovery header unreadable")?; - let header = super::parse_session_header_line(std::str::from_utf8(&header_line)?) - .context("attribution recovery header invalid")?; - ensure!( - header_matches_receipt(&header, &receipt), - "attribution recovery header changed" - ); - let start = if let Some(start) = receipt.start { - ensure!( - metadata.len() >= receipt.prefix_end, - "attribution recovery prefix shrank" - ); - file.seek(SeekFrom::Start( - receipt.prefix_end - receipt.prefix_tail.len() as u64, - ))?; - let mut tail = vec![0; receipt.prefix_tail.len()]; - file.read_exact(&mut tail)?; - ensure!( - tail == receipt.prefix_tail, - "attribution recovery prefix changed" - ); - if start > receipt.prefix_end && metadata.len() >= start { - file.seek(SeekFrom::Start(receipt.prefix_end))?; - let mut separator = [0]; - file.read_exact(&mut separator)?; - ensure!( - separator == *b"\n", - "attribution recovery separator changed" - ); - } - if metadata.len() == receipt.prefix_end || metadata.len() == start { - self.child_usage_recovery.pending = None; - return Ok(()); - } - start - } else { - // The atomic bootstrap's own row must be its exact last line. - ensure!( - metadata.len() >= receipt.bytes.len() as u64, - "attribution bootstrap is incomplete" - ); - metadata.len() - receipt.bytes.len() as u64 - }; - let expected_end = start + receipt.bytes.len() as u64; - let mut completed_tail = false; - if metadata.len() < expected_end { - // Only an append receipt has a proven row start. Bootstrap remains - // fail-closed rather than guessing which rewritten bytes are missing. - ensure!( - receipt.start.is_some() && metadata.len() > start, - "attribution recovery row is incomplete" - ); - let present = usize::try_from(metadata.len() - start)?; - file.seek(SeekFrom::Start(start))?; - let mut partial = vec![0; present]; - file.read_exact(&mut partial)?; - ensure!( - partial.as_slice() == &receipt.bytes[..present], - "attribution recovery owned prefix conflicts" - ); - ensure!( - file.metadata()?.len() == metadata.len() - && file_identity(&std::fs::metadata(&receipt.path)?) - == file_identity(&file.metadata()?), - "attribution recovery tail changed before completion" - ); - #[cfg(test)] - if super::child_usage_tests::FAIL_CHILD_TAIL_COMPLETION - .with(|fault| fault.replace(false)) - { - anyhow::bail!("injected owned tail completion IO error"); - } - // The append-mode descriptor writes only the missing owned bytes; - // append_cached would insert a newline into this incomplete row. - file.write_all(&receipt.bytes[present..])?; - file.flush()?; - pa_core::platform::fsync(&file)?; - ensure!( - file.metadata()?.len() == expected_end, - "attribution recovery tail changed during completion" - ); - completed_tail = true; - } - file.seek(SeekFrom::Start(start))?; - let mut actual = vec![0; receipt.bytes.len()]; - file.read_exact(&mut actual)?; - ensure!( - actual == receipt.bytes, - "attribution recovery row conflicts" - ); - file.seek(SeekFrom::End(-1))?; - let mut tail = [0]; - file.read_exact(&mut tail)?; - ensure!( - tail == *b"\n", - "attribution recovery file has an incomplete tail" - ); - if completed_tail { - ensure!( - file_identity(&std::fs::metadata(&receipt.path)?) - == file_identity(&file.metadata()?), - "attribution recovery file changed after completion" - ); - // The row is now newline-closed. Empty append invalidates the old - // window certificate without certifying a suffix as a fresh row. - // Stay within the same lease fence rather than recursively admitting - // a second mutation while release may already be draining this one. - let ownership = if self.lease.is_some() { - pa_core::session::window::AppendOwnership::SessionLeaseHeld - } else { - pa_core::session::window::AppendOwnership::Unleased - }; - pa_core::session::window::append_cached(&receipt.path, &[], ownership)?; - ensure!( - file_identity(&std::fs::metadata(&receipt.path)?) - == file_identity(&file.metadata()?), - "attribution recovery file changed after completion sync" - ); - } - // Loading the exact owned JSON can normalize a cost float. Admit the - // complete frozen object or its complete wire-decoded object, never a - // fieldwise mixture or an aggregate-only exception. - let original = serde_json::to_value(&receipt.entry)?; - let decoded: SessionEntry = serde_json::from_slice(&receipt.bytes)?; - let decoded = serde_json::to_value(decoded)?; - if let Some(existing) = self.entry(&receipt.entry.id) { - let indexed = serde_json::to_value(existing)?; - ensure!( - indexed == original || indexed == decoded, - "attribution recovery indexed row conflicts" - ); - } else { - // Normally immediate, before any later row can branch from stale state. - // Hydration may already have installed it; do not steal its selected leaf. - if self.leaf_id == receipt.entry.parent_id { - self.push_index(receipt.entry.clone()); - } else { - // A generic in-memory append may have arrived while recovery - // could not read the file. Install the physical ordering first, - // then merge its unsaved rows without changing the selected leaf. - let mut full = Self::open(&receipt.path)?; - ensure!( - header_matches_receipt(&full.header, &receipt), - "attribution recovery snapshot session changed" - ); - let owned = full - .entry(&receipt.entry.id) - .context("attribution recovery snapshot lost row")?; - let indexed = serde_json::to_value(owned)?; - ensure!( - indexed == original || indexed == decoded, - "attribution recovery snapshot row conflicts" - ); - for entry in &self.entries { - if !full.by_id.contains_key(&entry.id) { - full.push_index(entry.clone()); - } - } - full.leaf_id.clone_from(&self.leaf_id); - full.lease.clone_from(&self.lease); - full.child_usage_recovery - .clone_from(&self.child_usage_recovery); - full.anthropic_warning_shown = self.anthropic_warning_shown; - *self = full; - } - } - self.child_usage_recovery.created.insert(receipt.entry.id); - #[cfg(test)] - if let Some(hook) = - super::child_usage_tests::AFTER_CHILD_OBSERVE.with(|hook| hook.borrow_mut().take()) - { - hook(); - } - if mode == ChildUsageRecoveryMode::Confirm { - // This proof and sync belong to the already admitted recovery. - // Release may be draining it; do not start a second admission. - #[cfg(test)] - if super::child_usage_tests::FAIL_CHILD_CONFIRM_SYNC - .with(|fault| fault.replace(false)) - { - anyhow::bail!("injected attribution confirmation sync failure"); - } - ensure!( - file_identity(&std::fs::metadata(&self.path)?) - == file_identity(&file.metadata()?), - "attribution confirmation file changed before sync" - ); - if self.lease.is_some() { - pa_core::session::window::append_cached( - &self.path, - &[], - pa_core::session::window::AppendOwnership::SessionLeaseHeld, - )?; - } else { - pa_core::platform::fsync(&file)?; - } - ensure!( - file_identity(&std::fs::metadata(&self.path)?) - == file_identity(&file.metadata()?), - "attribution confirmation file changed" - ); - self.child_usage_recovery.pending = None; - // Retain Created until its own report receives acknowledgment. - } - Ok(()) - }; - match lease { - Some(lease) => lease.with_owned_mutation(&path, observe), - None => observe(), - } - } - /// The target assistant row's current usage: the running aggregate, since /// each indexed attribution folds its aggregate back into the row. fn attribution_target_usage(&self, target_id: &str) -> Result { diff --git a/crates/pa-daemon/src/session_store/log_wire.rs b/crates/pa-daemon/src/session_store/log_wire.rs index 9f5e1ab83..87c68c1d5 100644 --- a/crates/pa-daemon/src/session_store/log_wire.rs +++ b/crates/pa-daemon/src/session_store/log_wire.rs @@ -61,6 +61,16 @@ impl SessionFile { Ok(id) } + /// Selection is durable state but not an entry in the conversation tree. + pub(crate) fn persist_branch_selection(&self, leaf: Option<&str>) -> Result<()> { + let wire = self.log_transport.as_ref().context("session has no log writer")?; + wire.flush_selection( + &self.header.id, + &super::session_header_line(&self.header), + leaf, + ).map_err(anyhow::Error::from) + } + /// Flush every entry row the log does not hold yet, or (for /// `persist_appended`) every row from the caller's start, whichever is /// later. The flush's rows join the fold's index through the same paths diff --git a/crates/pa-daemon/src/session_store/read.rs b/crates/pa-daemon/src/session_store/read.rs index 0178ec871..ce6b2c9f2 100644 --- a/crates/pa-daemon/src/session_store/read.rs +++ b/crates/pa-daemon/src/session_store/read.rs @@ -130,69 +130,7 @@ impl SessionFile { /// /// Returns an error when the file cannot be read, is empty, or its header is invalid. pub fn open(path: &Path) -> Result { - // Streamed line-by-line load: one line is resident at a time, so a grown session - // never holds the raw file bytes alongside the parsed entries. - let file = fs::File::open(path) - .with_context(|| format!("read session file {}", path.display()))?; - let mut lines = std::io::BufReader::new(file).lines(); - let read_context = || format!("read session file {}", path.display()); - let mut first = None; - for line in lines.by_ref() { - let line = line.with_context(read_context)?; - if !line.trim().is_empty() { - first = Some(line); - break; - } - } - let first = first.ok_or_else(|| anyhow!("empty session file {}", path.display()))?; - let header_value: Value = serde_json::from_str(first.trim()) - .with_context(|| format!("invalid session header in {}", path.display()))?; - if header_value.get("type").and_then(Value::as_str) != Some("session") { - return Err(anyhow!("missing session header in {}", path.display())); - } - let pa_types::session::FileEntry::Header { header } = serde_json::from_value(header_value) - .with_context(|| format!("invalid session header in {}", path.display()))? - else { - return Err(anyhow!("missing session header in {}", path.display())); - }; - let mut file = SessionFile { - path: path.to_path_buf(), - header, - entries: Vec::new(), - by_id: HashMap::new(), - leaf_id: None, - window: None, - lease: None, - child_usage_recovery: super::child_usage::ChildUsageRecovery::default(), - anthropic_warning_shown: false, - skipped_lines: 0, - epoch: None, - rows_written_without_epoch: 0, - log_transport: None, - }; - for line in lines { - let line = line.with_context(read_context)?; - let trimmed = line.trim(); - if trimmed.is_empty() { - continue; - } - // Malformed lines are skipped, matching the TS loader. The - // count rides the store so the reopen that found damage can - // log it (the rows now STAY on disk: an append-only reopen - // never deletes what it skipped). - match serde_json::from_str::(trimmed) { - Ok(entry) => file.push_index(entry), - Err(_) => file.skipped_lines += 1, - } - } - fold_child_usage_attributions(&mut file.entries); - // The once-per-lifecycle gate rides the ACTIVE branch, exactly - // like the windowed walk below: a marker on an abandoned or - // sibling branch never suppresses the warning for the open leaf - // (fail-open — an off-path marker must not hide the warning on a - // branch that never showed it). - file.anthropic_warning_shown = file.branch().iter().copied().any(is_warning_shown_row); - Ok(file) + Self::open_log_path(path) } /// Load a log-transported session: the log is the source of truth, the @@ -210,14 +148,16 @@ impl SessionFile { /// emptied it), a header whose type is not `session`, I/O, or a foreign /// writer holding the log. pub(crate) fn open_log(session_path: &Path, agent_dir: &Path) -> Result { - let Some(session_id) = crate::session_transport::session_id_of(session_path) else { - anyhow::bail!( - "session file path has no id name: {}", - session_path.display() - ); - }; - let (log, repair) = crate::session_transport::open_repaired(agent_dir, &session_id) - .map_err(|error| anyhow::Error::from(error).context("open the session log"))?; + let actual = crate::session_transport::log_file(agent_dir, &crate::session_transport::session_id_of(session_path).context("session path has no id")?); + Self::open_log_path(&actual) + } + + fn open_log_path(session_path: &Path) -> Result { + anyhow::ensure!(session_path.is_file(), "session log is unavailable: {}", session_path.display()); + let session_id = crate::session_transport::session_id_of(session_path).context("session path has no id")?; + let directory = session_path.parent().context("session log has no directory")?; + let (log, repair) = crate::session_transport::open_repaired_dir(directory, &session_id) + .map_err(anyhow::Error::from).context("open session log")?; if let Some(report) = &repair { eprintln!( "pa-daemon: session log {} repaired: {} unusable trailing byte(s) after seq {}", @@ -248,17 +188,20 @@ impl SessionFile { leaf_id: None, window: None, lease: None, - child_usage_recovery: super::child_usage::ChildUsageRecovery::default(), anthropic_warning_shown: false, skipped_lines: 0, - epoch: None, - rows_written_without_epoch: 0, - canonical_live: None, - canonical_delivered: std::collections::VecDeque::new(), - canonical_delivered_custom: std::collections::VecDeque::new(), log_transport: None, }; for row in rows.iter().skip(1) { + if row.body.get("type").and_then(Value::as_str) == Some("branch_selection") { + let selected = row.body.get("selectedLeaf").context("branch selection has no leaf")?; + file.leaf_id = match selected { + Value::Null => None, + Value::String(id) if file.by_id.contains_key(id) => Some(id.clone()), + _ => anyhow::bail!("branch selection names an absent entry"), + }; + continue; + } match serde_json::from_value::(row.body.clone()) { Ok(entry) => file.push_index(entry), Err(_) => file.skipped_lines += 1, @@ -268,7 +211,7 @@ impl SessionFile { file.anthropic_warning_shown = file.branch().iter().copied().any(is_warning_shown_row); let wire = crate::session_transport::LogWire::held( log, - agent_dir, + directory, file.entries.len(), rows.last().map_or(1, |row| row.seq), ); @@ -283,77 +226,9 @@ impl SessionFile { /// Returns an error when the windowed load fails or holds no header; a /// malformed retained row falls back to [`SessionFile::open`]. pub fn open_windowed(path: &Path) -> Result { - let Some(mut window) = pa_core::session::window::WindowedSessionStore::open(path)? else { - return Self::open(path); - }; - let header = window - .entries() - .iter() - .find_map(|entry| match entry { - pa_types::session::FileEntry::Header { header } => Some(header.clone()), - _ => None, - }) - .ok_or_else(|| anyhow!("window has no session header"))?; - // The window's walk already hydrated the once-per-session-lifecycle - // warning gate from the whole active branch (the row may sit in the - // discarded prefix, far outside this store's retained rows). - let anthropic_warning_shown = window.anthropic_warning_shown(); - let mut file = Self { - path: path.to_owned(), - header, - entries: Vec::new(), - by_id: HashMap::new(), - leaf_id: None, - window: None, - lease: None, - child_usage_recovery: super::child_usage::ChildUsageRecovery::default(), - anthropic_warning_shown, - skipped_lines: 0, - epoch: None, - rows_written_without_epoch: 0, - log_transport: None, - }; - // The raw rows are consumed in place: each line String drops as soon as its - // parsed entry joins the store. - let raw_count = window.raw_entries().len(); - for line in window - .take_metadata_entries() - .into_iter() - .chain(window.take_raw_entries()) - { - let Ok(entry) = serde_json::from_str(&line) else { - return Self::open(path); - }; - file.push_index(entry); - } - // The window parses the retained-target attributions BEFORE the raw - // retained rows, so the fold runs once every row is in. - fold_child_usage_attributions(&mut file.entries); - file.leaf_id = Some(window.leaf_id().to_owned()); - let settings = window.settings(); - file.window = Some(SessionWindow { - message_count: window.message_count(), - first_message: window - .first_user_message() - .map(message_text) - .filter(|text| !text.is_empty()), - loaded_entries: file.entries.len(), - compaction_count: window.compaction_count(), - has_thinking_level: window.has_thinking_level(), - has_service_tier: window.has_service_tier(), - model: settings.model.clone(), - boundary_model: window.boundary_model().cloned(), - thinking_level: settings.thinking_level.clone(), - service_tier: settings.service_tier, - // The retained rows joined the store verbatim above, so their ids are - // exactly the trailing `raw_count` store ids — no third parse pass. - retained_ids: file.entries[file.entries.len() - raw_count..] - .iter() - .map(|entry| entry.id.clone()) - .collect(), - older_path_stats: window.older_path_stats().clone(), - }); - Ok(file) + // One validated log replay owns history. Context projection remains the + // existing branch/compaction fold, not a second storage implementation. + Self::open(path) } #[cfg(test)] @@ -377,15 +252,6 @@ impl SessionFile { } full.leaf_id.clone_from(&self.leaf_id); full.lease.clone_from(&self.lease); - full.canonical_live.clone_from(&self.canonical_live); - full.canonical_delivered - .clone_from(&self.canonical_delivered); - full.canonical_delivered_custom - .clone_from(&self.canonical_delivered_custom); - full.epoch.clone_from(&self.epoch); - full.rows_written_without_epoch = self.rows_written_without_epoch; - full.child_usage_recovery - .clone_from(&self.child_usage_recovery); // The merged store's leaf is the window's leaf: re-derive the gate // from the merged ACTIVE branch — the full open's own-leaf answer // can disagree, a post-snapshot marker must hydrate, and an @@ -414,14 +280,8 @@ impl SessionFile { leaf_id: None, window: None, lease: None, - child_usage_recovery: super::child_usage::ChildUsageRecovery::default(), anthropic_warning_shown: false, skipped_lines: 0, - epoch: None, - rows_written_without_epoch: 0, - canonical_live: None, - canonical_delivered: std::collections::VecDeque::new(), - canonical_delivered_custom: std::collections::VecDeque::new(), log_transport: None, } } diff --git a/crates/pa-daemon/src/session_store/write.rs b/crates/pa-daemon/src/session_store/write.rs index 3d53343aa..879e14f45 100644 --- a/crates/pa-daemon/src/session_store/write.rs +++ b/crates/pa-daemon/src/session_store/write.rs @@ -2,11 +2,10 @@ //! append-with-rewrite-bootstrap persist paths, and the store lease discipline that //! serializes writers onto the file. -use pa_core::session_engine::canonical_coordinator::CanonicalWriteCertainty; use super::{ - fs, json, new_entry_id, Context, HashMap, PathBuf, Result, Serialize, SessionEntry, - SessionFile, SessionHeader, Value, Write, + json, new_entry_id, HashMap, PathBuf, Result, SessionEntry, + SessionFile, SessionHeader, Value, }; impl SessionEntry { @@ -30,42 +29,10 @@ impl SessionEntry { impl SessionFile { /// Persist only the newly appended creation records on a resumed file. pub(crate) fn persist_appended(&self, start: usize) -> Result<()> { - // THE transport switch: the log transport flushes the tail from the - // wire's durable cursor (rows before it are already durable), so the - // reopen flow's `start` and the wire's cursor agree. - if self.log_transport.is_some() { - return self.flush_unmaterialized_log_tail(start); - } - anyhow::ensure!( - self.canonical_live.is_none(), - "canonical owner requires explicit append and adoption ACK" - ); - anyhow::ensure!( - self.child_usage_recovery.pending.is_none(), - "confirm unresolved attribution before persisting appended rows" - ); - let mut bytes = Vec::new(); - for entry in &self.entries[start..] { - write_line(&mut bytes, entry)?; - } - match &self.lease { - Some(lease) => lease.append(&self.path, &bytes)?, - None => pa_core::session::window::append_cached( - &self.path, - &bytes, - pa_core::session::window::AppendOwnership::Unleased, - )?, - } - Ok(()) + self.flush_unmaterialized_log_tail(start) } pub fn append_entry(&mut self, type_: &str, fields: Value) -> String { - // This legacy nonfallible builder is not a canonical writer. If a caller - // uses it after attach, invalidate the owner before any local mutation; - // an append/rollback ABA must never certify the old source as usable. - if let Some(owner) = &self.canonical_live { - owner.invalidate(); - } let parent_id = self.leaf_id.clone(); let mut entry = SessionEntry::new( type_, @@ -120,51 +87,10 @@ impl SessionFile { /// Returns an error when the store only holds a window, or the write, /// flush, sync, or rename fails; an empty path answers `Ok(())`. pub fn rewrite(&self) -> Result<()> { - anyhow::ensure!( - self.canonical_live.is_none(), - "cannot rewrite a bound canonical journal" - ); - // THE transport switch: a log-transported store has no full-file - // rewrite; the log is append-only, so this is the flush of the - // unmaterialized tail (the create flows' materialization point). - if self.log_transport.is_some() { - return self.flush_unmaterialized_log_tail(0); - } - anyhow::ensure!( - self.child_usage_recovery.pending.is_none(), - "confirm unresolved attribution before rewriting session" - ); - anyhow::ensure!( - self.window.is_none(), - "full history required before rewriting session" - ); - let path = self.path.as_path(); - let Some(path) = (if path.as_os_str().is_empty() { - None - } else { - Some(path) - }) else { + if self.path.as_os_str().is_empty() { return Ok(()); - }; - if let Some(parent) = path.parent() { - fs::create_dir_all(parent) - .with_context(|| format!("create session dir {}", parent.display()))?; - } - let temp = path.with_extension(format!("jsonl.tmp-{}", std::process::id())); - { - let file = - fs::File::create(&temp).with_context(|| format!("create {}", temp.display()))?; - let mut writer = std::io::BufWriter::new(file); - write_line(&mut writer, &session_header_line(&self.header))?; - for entry in &self.entries { - write_line(&mut writer, entry)?; - } - writer.flush()?; - writer.get_ref().sync_all()?; } - pa_core::platform::rename_onto(&temp, path) - .with_context(|| format!("persist {}", path.display()))?; - Ok(()) + self.flush_unmaterialized_log_tail(0) } /// Append one entry line to the file, rewriting first when the file is @@ -192,7 +118,7 @@ impl SessionFile { /// in-memory flag stays unset then, so the next open re-warns (a lost /// marker costs one repeated warning, never a suppressed one). pub fn mark_anthropic_warning_shown(&mut self) -> Result<()> { - self.persist_owner_metadata( + self.persist_entry( "custom", json!({ "customType": pa_core::session::ANTHROPIC_WARNING_SHOWN_CUSTOM_TYPE, @@ -235,225 +161,24 @@ impl SessionFile { self.persist_built_entry(entry) } - /// [`Self::persist_entry`] for the native final output: the admitted provider - /// step travels with the row. - /// - /// # Errors - /// As [`Self::persist_entry`], plus the step refusal. - pub fn persist_message_with_step( - &mut self, - fields: Value, - step: &pa_core::session_engine::canonical_coordinator::CanonicalProviderStep, - ) -> Result { - let mut entry = SessionEntry::new( - "message", - self.leaf_id.clone(), - &self.by_id, - fields, - &crate::util::now_iso(), - ); - if self.window.is_some() { - entry.id = uuid::Uuid::new_v4().to_string(); - } - self.persist_built_entry_with_step(entry, Some(step)) - } - - /// The durable-append core [`Self::persist_entry_at`] and the attribution - /// append-once share: the entry arrives fully built (its id included), - /// its line reaches the file first, and the entry joins the index only - /// after, so a failed append leaves no indexed row a reload would - /// resurface as the leaf. - /// - /// # Errors - /// - /// Returns an error when the store only holds a window without a file, - /// or the append, flush, sync, or the bootstrap rewrite fails. + /// Commit through the log before publishing the row in the serving fold. pub(super) fn persist_built_entry(&mut self, entry: SessionEntry) -> Result { - self.persist_built_entry_with_step(entry, None) - } - - /// [`Self::persist_built_entry`] for a row that carries an admitted provider - /// step: the native final output. The step is valid for ONE row, so it is a - /// parameter rather than a field on the store - nothing can survive a write. - /// - /// # Errors - /// As [`Self::persist_built_entry`], plus a refusal when an assistant row is - /// persisted under a live epoch without a step. - pub(super) fn persist_built_entry_with_step( - &mut self, - entry: SessionEntry, - step: Option<&pa_core::session_engine::canonical_coordinator::CanonicalProviderStep>, - ) -> Result { - anyhow::ensure!( - self.canonical_live.is_none(), - "canonical owner requires explicit append and adoption ACK" - ); - // THE transport switch's write arm: a log-transported store's only - // durable write is the log row for the built entry (the canonical - // grant machinery below stays compiled for the canonical sessions). - if self.log_transport.is_some() { - return self.persist_built_log_entry(entry); - } - self.persist_owned_entry(entry, step) - } - - /// Sole physical writer; only the explicit live owner may call this while bound. - pub(super) fn persist_owned_entry( - &mut self, - entry: SessionEntry, - step: Option<&pa_core::session_engine::canonical_coordinator::CanonicalProviderStep>, - ) -> Result { - self.persist_granted_entry(entry, step, None) - } - - pub(super) fn persist_granted_entry( - &mut self, - mut entry: SessionEntry, - step: Option<&pa_core::session_engine::canonical_coordinator::CanonicalProviderStep>, - granted: Option, - ) -> Result { - self.confirm_child_usage_write()?; - // A preflight recovery can advance the leaf after the caller built its - // generic row. Preserve that recovered parent instead of branching it away. - entry.parent_id.clone_from(&self.leaf_id); - anyhow::ensure!( - self.window.is_none() || self.path.exists(), - "window-backed session file is missing" - ); - let id = entry.id.clone(); - // Canonical admission for this row, taken before any byte moves: a user - // row takes a prepare-phase flight (no draft exists for it), every other - // row takes a requested-mutation grant. With no epoch attached the row is - // counted instead - the counter states the fact, never a policy. - let epoch = self.epoch.clone(); - // One row, one step: the worker sets this immediately before an assistant - // row and it is consumed here, so a later row can never borrow it. - let mut grant = if let Some(grant) = granted { - Some(grant) - } else if let Some(handle) = epoch.as_ref() { - let admitted = super::canonical_adapter::grant_for_row(&handle.0, &entry, step)?; - if admitted.is_none() { - self.rows_written_without_epoch += 1; - } - admitted - } else { - self.rows_written_without_epoch += 1; - None - }; - // Captured rather than re-tested later: this branch REQUIRES an existing - // file, and that is what makes `Known` provable for the append below. - let existed_before = !self.path.as_os_str().is_empty() && self.path.exists(); - if existed_before { - let mut bytes = Vec::new(); - write_line(&mut bytes, &entry)?; - #[cfg(test)] - if let Some(cut) = (entry.type_ == "child_usage_attributed") - .then(|| super::child_usage_tests::PARTIAL_CHILD_APPEND.with(std::cell::Cell::take)) - .flatten() - { - // Isolated unleased fixture only: simulate write_all's partial - // prefix + Err postcondition, not a real injected OS failure. - anyhow::ensure!( - self.lease.is_none() && cut < bytes.len(), - "invalid partial append fixture" - ); - let mut file = fs::OpenOptions::new().append(true).open(&self.path)?; - file.write_all(&bytes[..cut])?; - file.flush()?; - anyhow::bail!("injected partial attribution append error"); - } - let appended = match &self.lease { - Some(lease) => lease.append(&self.path, &bytes), - None => pa_core::session::window::append_cached( - &self.path, - &bytes, - pa_core::session::window::AppendOwnership::Unleased, - ) - .with_context(|| format!("append to {}", self.path.display())), - }; - // `Known` is provable HERE only because this branch requires a file that - // already existed: the bootstrap rewrite created it durably, so this - // append puts no directory entry at stake. `window::append_cached` opens - // with create-if-missing and does NOT sync the parent directory on a - // create, so if the file could vanish between the guard above and that - // open, a successful write would still be an unproven create and this - // classification would be a lie. The assertion pins the assumption the - // guard rests on, so a later reader cannot turn it into a race. - debug_assert!( - existed_before, - "the durable append branch requires a file that already existed" - ); - // Settle before the row joins the index: a caller must never observe an - // indexed row whose flight is still unclassified. An early failure above - // drops the grant unclassified, which fails closed and poisons the epoch. - if let Some(grant) = grant.take() { - grant.settle(certainty_of(&appended))?; - } - appended?; - #[cfg(test)] - if entry.type_ == "child_usage_attributed" - && super::child_usage_tests::FAIL_AFTER_CHILD_APPEND - .with(|fault| fault.replace(false)) - { - anyhow::bail!("injected post-append pre-index error"); - } - // The line is in the file now: index it so the in-memory leaf matches what a - // reload sees. + if self.path.as_os_str().is_empty() { + let id = entry.id.clone(); self.push_index(entry); - } else { - // The rewrite path serializes the whole index, so the entry must be - // indexed first; a failed rewrite rolls the index back. The live - // attribution fold is DEFERRED until the rewrite succeeds. - let previous_leaf = self.leaf_id.clone(); - #[cfg(test)] - let entry_is_child = entry.type_ == "child_usage_attributed"; - self.push_index_inner(entry, false); - let rewritten = self.rewrite(); - #[cfg(test)] - let rewritten = rewritten.and_then(|()| { - if entry_is_child - && super::child_usage_tests::FAIL_AFTER_CHILD_APPEND - .with(|fault| fault.replace(false)) - { - anyhow::bail!("injected post-bootstrap pre-index error"); - } - Ok(()) - }); - if let Err(error) = rewritten { - self.by_id.remove(&id); - self.entries.pop(); - self.leaf_id = previous_leaf; - if let Some(grant) = grant.take() { - grant.settle(CanonicalWriteCertainty::Uncertain)?; - } - return Err(error); - } - if let Some(grant) = grant.take() { - grant.settle(CanonicalWriteCertainty::Known)?; - } - self.fold_attribution_id(&id); + return Ok(id); } - Ok(id) - } - - /// Rows this store persisted while no canonical epoch was attached. - /// - /// The name states the fact rather than the policy, so it stays true if the - /// policy changes. A turn that writes content leaves this at zero. - #[must_use] - pub fn rows_written_without_epoch(&self) -> u64 { - self.rows_written_without_epoch + self.persist_built_log_entry(entry) } /// Point the session at a concrete file path (after `create`), preserving entries. pub fn set_path(&mut self, path: PathBuf) { if self.path != path { - // An invalidated old scope is never recycled for another path. - if let Some(owner) = &self.canonical_live { - owner.invalidate(); - } self.lease = None; } + if self.path != path && !path.as_os_str().is_empty() { + self.log_transport = Some(crate::session_transport::LogWire::for_path(&path)); + } self.path = path; } } @@ -477,22 +202,3 @@ pub fn session_header_line(header: &SessionHeader) -> Value { ); Value::Object(ordered) } - -fn write_line(writer: &mut impl Write, value: &T) -> Result<()> { - let mut line = serde_json::to_string(value)?; - line.push('\n'); - writer.write_all(line.as_bytes())?; - Ok(()) -} - -/// Classify one append attempt for the canonical epoch. -/// -/// A failed append is `Uncertain`: bytes may have reached the file before the -/// error, and the epoch must not drain across a state nobody proved. -fn certainty_of(result: &Result<()>) -> CanonicalWriteCertainty { - if result.is_ok() { - CanonicalWriteCertainty::Known - } else { - CanonicalWriteCertainty::Uncertain - } -} diff --git a/crates/pa-daemon/src/session_transport.rs b/crates/pa-daemon/src/session_transport.rs index e7196e33a..8d7e2e098 100644 --- a/crates/pa-daemon/src/session_transport.rs +++ b/crates/pa-daemon/src/session_transport.rs @@ -143,7 +143,7 @@ struct WireState { /// injected disk drives the fault-injection tests through the very same /// code paths ([`Disk`]'s own contract). disk: Option>, - agent_dir: PathBuf, + directory: PathBuf, /// Row one of the log is a session header. A fresh (deferred) wire writes /// it as row one; a wire whose open found a pre-existing log adopts the /// header it finds at row one instead of writing a second one. @@ -195,13 +195,13 @@ impl WireState { if self.log.is_some() { return Ok(()); } - std::fs::create_dir_all(log_dir(&self.agent_dir)).map_err(LogWireError::Io)?; + std::fs::create_dir_all(self.directory.clone()).map_err(LogWireError::Io)?; let log = match self.disk.clone() { Some(disk) => { - SessionLog::open_with(disk, &log_dir(&self.agent_dir), session_id, deadline()) + SessionLog::open_with(disk, &self.directory.clone(), session_id, deadline()) .map_err(open_error)? } - None => SessionLog::open(&log_dir(&self.agent_dir), session_id, deadline()) + None => SessionLog::open(&self.directory.clone(), session_id, deadline()) .map_err(open_error)?, }; // One read decides the adoption and sets the seq cursor both, so the @@ -358,7 +358,7 @@ impl LogWire { state: std::sync::Arc::new(std::sync::Mutex::new(WireState { log: None, disk: None, - agent_dir: agent_dir.to_path_buf(), + directory: log_dir(agent_dir), header_written: false, flushed_entries: 0, next_seq: 1, @@ -367,16 +367,30 @@ impl LogWire { } } + /// A lazy writer for an exact durable log path, including an overridden directory. + pub(crate) fn for_path(path: &Path) -> Self { + let wire = Self::deferred(Path::new(".")); + wire.state.lock().expect("log wire lock").directory = + path.parent().unwrap_or_else(|| Path::new(".")).to_path_buf(); + wire + } + + pub(crate) fn flush_selection(&self, session_id: &str, header: &Value, leaf: Option<&str>) -> Result<(), LogWireError> { + self.flush(session_id, header, &[])?; + let mut state = self.state.lock().expect("log wire lock"); + state.append_pinned(&serde_json::json!({"type": "branch_selection", "selectedLeaf": leaf}), false) + } + /// A reopened wire wrapping the log the fold was replayed from. The /// header is already durable (row one of the replayed log), `replayed` /// counts the fold's durable entry rows, and `last_seq` pins the cursor /// past the log's last row. - pub(crate) fn held(log: SessionLog, agent_dir: &Path, replayed: usize, last_seq: u64) -> Self { + pub(crate) fn held(log: SessionLog, directory: &Path, replayed: usize, last_seq: u64) -> Self { Self { state: std::sync::Arc::new(std::sync::Mutex::new(WireState { log: Some(log), disk: None, - agent_dir: agent_dir.to_path_buf(), + directory: directory.to_owned(), header_written: true, flushed_entries: replayed, next_seq: last_seq + 1, @@ -471,12 +485,19 @@ pub(crate) fn open_repaired( agent_dir: &Path, session_id: &str, ) -> Result<(SessionLog, Option), LogWireError> { - match SessionLog::open(&log_dir(agent_dir), session_id, deadline()) { + open_repaired_dir(&log_dir(agent_dir), session_id) +} + +pub(crate) fn open_repaired_dir( + directory: &Path, + session_id: &str, +) -> Result<(SessionLog, Option), LogWireError> { + match SessionLog::open(directory, session_id, deadline()) { Ok(log) => Ok((log, None)), Err(session_log::Error::Damaged { .. }) => { - let report = SessionLog::repair(&log_dir(agent_dir), session_id, deadline()) + let report = SessionLog::repair(directory, session_id, deadline()) .map_err(open_error)?; - let log = SessionLog::open(&log_dir(agent_dir), session_id, deadline()) + let log = SessionLog::open(directory, session_id, deadline()) .map_err(open_error)?; Ok((log, Some(report))) } diff --git a/crates/pa-daemon/src/session_tree.rs b/crates/pa-daemon/src/session_tree.rs index f9cae92ab..392b378d4 100644 --- a/crates/pa-daemon/src/session_tree.rs +++ b/crates/pa-daemon/src/session_tree.rs @@ -169,17 +169,22 @@ impl SessionFile { /// /// Returns an error when the target entry does not exist; a `None` id clears the leaf. pub fn branch_to(&mut self, id: Option<&str>) -> Result<()> { - if self.is_canonical_bound() { - return self.advance_canonical_branch(id).map_err(Into::into); - } match id { Some(id) => { if self.entry(id).is_none() { return Err(anyhow!("Entry {id} not found")); } + if !self.path.as_os_str().is_empty() { + self.persist_branch_selection(Some(id))?; + } self.leaf_id = Some(id.to_string()); } - None => self.leaf_id = None, + None => { + if !self.path.as_os_str().is_empty() { + self.persist_branch_selection(None)?; + } + self.leaf_id = None; + } } Ok(()) } @@ -200,10 +205,6 @@ impl SessionFile { usage: Option, model: Option<(&str, &str)>, ) -> Result { - anyhow::ensure!( - !self.is_canonical_bound(), - "canonical branch summaries require prepared navigation and context-policy receipts" - ); self.branch_to(from_id)?; let mut fields = json!({ "fromId": from_id.unwrap_or("root"), @@ -240,7 +241,7 @@ impl SessionFile { Some(label) => fields["label"] = json!(label), None => fields["label"] = Value::Null, } - self.persist_owner_metadata("label", fields) + self.persist_entry("label", fields) .map_err(Into::into) } diff --git a/crates/pa-daemon/src/setting_switches.rs b/crates/pa-daemon/src/setting_switches.rs index 69c280fc4..1c649d411 100644 --- a/crates/pa-daemon/src/setting_switches.rs +++ b/crates/pa-daemon/src/setting_switches.rs @@ -180,7 +180,6 @@ impl Worker { if let Some(store) = core .store .as_mut() - .filter(|store| !store.is_canonical_bound()) { // A bound live Core switch has already committed this intent. let _ = store.persist_entry( @@ -391,7 +390,7 @@ impl Worker { let preference_changed = core.service_tier != Some(tier); if preference_changed { if let Some(store) = core.store.as_mut() { - if let Err(error) = store.persist_owner_metadata( + if let Err(error) = store.persist_entry( "service_tier_change", json!({ "serviceTier": tier }), ) { diff --git a/crates/pa-daemon/src/state_getters.rs b/crates/pa-daemon/src/state_getters.rs index 86e002168..b75017b22 100644 --- a/crates/pa-daemon/src/state_getters.rs +++ b/crates/pa-daemon/src/state_getters.rs @@ -68,18 +68,7 @@ impl Worker { // borrow-based, so the request answers from memory even on a grown store. let (label, context_usage, own_usage, total_usage, session_id, own_usage_by_model) = { let core = self.core.lock().unwrap(); - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return response_failure( - None, - "get_context_tree", - "Canonical source is unresolved", - None, - ); - } + let store = core.store.as_ref(); let label = store .and_then(|store| store.session_name().map(str::to_string)) @@ -193,18 +182,7 @@ impl Worker { return response; } let core = self.core.lock().unwrap(); - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return response_failure( - None, - "get_session_context", - "Canonical source is unresolved", - None, - ); - } + let Some(store) = core.store.as_ref() else { return response_failure( None, diff --git a/crates/pa-daemon/src/supervisor/routing.rs b/crates/pa-daemon/src/supervisor/routing.rs index 1d9e4f3bb..343b1dc43 100644 --- a/crates/pa-daemon/src/supervisor/routing.rs +++ b/crates/pa-daemon/src/supervisor/routing.rs @@ -16,7 +16,7 @@ pub(super) enum WakeRoute { } pub(crate) const ROUTE_TIMEOUT_MS: u64 = 30_000; -/// How long a kill re-asks a worker whose canonical owner is mid-adoption: the + /// pending caller's ACK lands within milliseconds, and a hard stop must not fail /// on that handshake. pub(crate) const KILL_ADOPTION_RETRY: Duration = Duration::from_secs(2); @@ -699,29 +699,7 @@ impl Supervisor { } } DaemonCommand::Kill { .. } => { - // A worker whose canonical owner is mid-adoption refuses its close with - // `AdoptionConflict` until that caller's ACK lands, milliseconds later. - // The refusal is a "not yet", not a verdict on the kill: re-ask while the - // transient lasts, then let the answer stand. A refusal that outlives the - // window is the worker's own (an owed accounting close) and stays one. - let deadline = tokio::time::Instant::now() + KILL_ADOPTION_RETRY; - loop { - let response = self - .route_command_ready( - &resident, - worker_command, - payload.clone(), - timeout, - admission, - ) - .await; - let transient = matches!(&response, Ok(reply) - if reply.refused_by_adoption_conflict()); - if !transient || tokio::time::Instant::now() >= deadline { - break response; - } - tokio::time::sleep(KILL_ADOPTION_RETRY_INTERVAL).await; - } + self.route_command_ready(&resident, worker_command, payload, timeout, admission).await } _ => { self.route_command_ready(&resident, worker_command, payload, timeout, admission) diff --git a/crates/pa-daemon/src/user_bash.rs b/crates/pa-daemon/src/user_bash.rs index 1b5aa07b1..fcc10115f 100644 --- a/crates/pa-daemon/src/user_bash.rs +++ b/crates/pa-daemon/src/user_bash.rs @@ -742,7 +742,7 @@ fn record_bash_result( if exclude_from_context { row["excludeFromContext"] = json!(true); } - store.persist_owner_metadata("message", json!({ "message": row }))?; + store.persist_entry("message", json!({ "message": row }))?; Ok(()) } diff --git a/crates/pa-daemon/src/worker.rs b/crates/pa-daemon/src/worker.rs index cdfcfae0f..00a28c821 100644 --- a/crates/pa-daemon/src/worker.rs +++ b/crates/pa-daemon/src/worker.rs @@ -2,7 +2,6 @@ //! The worker owns the session - the append-only store, the queue lanes, event sequencing, and turn //! execution. -mod canonical_live; mod config; mod context_selection; mod env; @@ -290,8 +289,6 @@ impl Worker { generation: crate::util::new_display_id(), last_event_sequence: 0, store: None, - canonical_step: std::sync::Arc::new(std::sync::Mutex::new(None)), - context_selection_uncertain: None, cwd: String::new(), steering: VecDeque::new(), follow_up: VecDeque::new(), diff --git a/crates/pa-daemon/src/worker/canonical_live.rs b/crates/pa-daemon/src/worker/canonical_live.rs deleted file mode 100644 index 5b1b281f7..000000000 --- a/crates/pa-daemon/src/worker/canonical_live.rs +++ /dev/null @@ -1,220 +0,0 @@ -//! Immutable binding from a built SDK mirror to its actual worker-owned writer. -//! Host locks stay inside blocking owner calls; Core performs all local adoption. - -use std::sync::{Arc, Mutex}; - -use pa_core::session::canonical::{ - CanonicalAcknowledgement, CanonicalCommitDisposition, CanonicalFailureKind, CanonicalFuture, - CanonicalPrepared, CanonicalRequestedWrite, CanonicalSourceDisposition, CanonicalStoreError, -}; -use pa_core::session_engine::canonical_live::{ - CanonicalLiveAcknowledged, CanonicalLiveAppendRequest, CanonicalLiveCustomPrepared, - CanonicalLiveCustomRequest, CanonicalLiveOwner, CanonicalLivePrepareRequest, - CanonicalLiveProviderStepRequest, CanonicalLiveRunRequest, CanonicalLiveSnapshot, - CanonicalLiveToolRequest, CanonicalLiveUserPrepared, CanonicalLiveUserRequest, -}; - -use crate::agent_engine::{canonical_mirror_header, ContextSelectionSource}; -use crate::lease::canonical_session_path; - -use super::SessionCore; - -pub(super) struct CoreCanonicalLiveOwner { - pub(super) core: Arc>, - pub(super) source: ContextSelectionSource, - pub(super) owner_scope: Option, - // Captured anew for this SDK adapter, before any attach. An invalid - // capture remains a refusing live adapter, never standalone fallback. - pub(super) bootstrap_admission: Result, -} - -fn refused(detail: impl std::fmt::Display) -> CanonicalStoreError { - CanonicalStoreError { - kind: CanonicalFailureKind::InvalidRequest, - requested: CanonicalRequestedWrite::NotWritten, - source: CanonicalSourceDisposition::NeedsReconcile, - detail: detail.to_string(), - } -} - -fn validate( - core: &SessionCore, - source: &ContextSelectionSource, -) -> Result<(), CanonicalStoreError> { - let expected = source - .lease - .upgrade() - .ok_or_else(|| refused("canonical owner was replaced"))?; - let store = core - .store - .as_ref() - .ok_or_else(|| refused("canonical store is unavailable"))?; - let lease = store - .lease - .as_ref() - .ok_or_else(|| refused("canonical lease is unavailable"))?; - if !Arc::ptr_eq(lease, &expected) - || canonical_session_path(&store.path) != source.path - || canonical_mirror_header(&store.header).map_err(refused)? != source.header - { - return Err(refused("canonical owner, header or path changed")); - } - lease - .with_owned_mutation(&store.path, || Ok(())) - .map_err(refused)?; - let header = crate::session_store::read_session_header(&store.path) - .ok_or_else(|| refused("physical canonical header is unavailable"))?; - if canonical_mirror_header(&header).map_err(refused)? != source.header { - return Err(refused("physical canonical header changed")); - } - Ok(()) -} - -impl CoreCanonicalLiveOwner { - fn call( - &self, - action: impl FnOnce(&mut SessionCore) -> Result + Send + 'static, - ) -> CanonicalFuture { - let core = Arc::clone(&self.core); - let source = self.source.clone(); - let owner_scope = self.owner_scope.clone(); - let capture_error = self.bootstrap_admission.as_ref().err().cloned(); - Box::pin(async move { - if let Some(error) = capture_error { - return Err(refused(error)); - } - tokio::task::spawn_blocking(move || { - let mut core = core.lock().map_err(refused)?; - validate(&core, &source)?; - let store = core.store.as_ref().expect("validated store"); - if !owner_scope - .as_ref() - .map(|scope| store.has_canonical_owner_scope(scope)) - .transpose()? - .unwrap_or(false) - { - return Err(refused("canonical owner scope was retired")); - } - action(&mut core) - }) - .await - .map_err(refused)? - }) - } -} - -impl CanonicalLiveOwner for CoreCanonicalLiveOwner { - fn bootstrap_preserves_owner_admission(&self) -> bool { - self.bootstrap_admission - .as_ref() - .is_ok_and(|preserve| *preserve) - } - - fn retire_completed_runtime( - &self, - expected: pa_core::session::canonical::CanonicalCursor, - ) -> CanonicalFuture<()> { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .retire_completed_canonical_runtime(&expected) - }) - } - - fn snapshot(&self) -> CanonicalFuture { - self.call(move |core| { - let store = core.store.as_mut().expect("validated store"); - store.canonical_snapshot() - }) - } - - fn register_run(&self, request: CanonicalLiveRunRequest) -> CanonicalFuture { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .register_canonical_run(request) - }) - } - - fn prepare(&self, request: CanonicalLivePrepareRequest) -> CanonicalFuture { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .prepare_canonical(request) - }) - } - - fn append( - &self, - request: CanonicalLiveAppendRequest, - ) -> CanonicalFuture { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .append_canonical(request) - }) - } - - fn acknowledge( - &self, - request: CanonicalAcknowledgement, - ) -> CanonicalFuture { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .acknowledge_canonical(request) - }) - } - - fn admit_user( - &self, - request: CanonicalLiveUserRequest, - ) -> CanonicalFuture { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .admit_canonical_user(request) - }) - } - - fn admit_custom( - &self, - request: CanonicalLiveCustomRequest, - ) -> CanonicalFuture { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .admit_canonical_custom(request) - }) - } - - fn begin_provider_step( - &self, - request: CanonicalLiveProviderStepRequest, - ) -> CanonicalFuture<()> { - self.call(move |core| { - let cancelled = - core.shutdown_requested || core.abort_requested || core.retry_abort_requested; - core.store - .as_mut() - .expect("validated store") - .begin_canonical_provider_step(request, cancelled) - }) - } - - fn admit_tool(&self, request: CanonicalLiveToolRequest) -> CanonicalFuture<()> { - self.call(move |core| { - core.store - .as_mut() - .expect("validated store") - .admit_canonical_tool(request) - }) - } -} diff --git a/crates/pa-daemon/src/worker/commands.rs b/crates/pa-daemon/src/worker/commands.rs index 6b517504d..6952e6a48 100644 --- a/crates/pa-daemon/src/worker/commands.rs +++ b/crates/pa-daemon/src/worker/commands.rs @@ -260,18 +260,7 @@ impl Worker { None, )); } - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return Err(response_failure( - None, - command_type, - "Canonical source is unresolved; reconcile before reading or starting work", - None, - )); - } + // The shutdown admission gate: a command dispatched while the graceful stop // is closing must not start new work the exit would orphan (an execute_bash // racing the shutdown would spawn a child the exit leaves running). @@ -494,13 +483,7 @@ impl Worker { return response; } let (core, inputs) = self.summary_inputs(); - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return response_failure(None, "get_state", "Canonical source is unresolved", None); - } + let summary = self.summary_locked(&core, inputs); response_success( None, @@ -516,18 +499,7 @@ impl Worker { return response; } let core = self.core.lock().unwrap(); - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return response_failure( - None, - "get_session_header", - "Canonical source is unresolved", - None, - ); - } + let Some(store) = core.store.as_ref() else { return response_failure( None, @@ -550,18 +522,7 @@ impl Worker { return response; } let core = self.core.lock().unwrap(); - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return response_failure( - None, - "get_session_stats", - "Canonical source is unresolved", - None, - ); - } + let Some(store) = core.store.as_ref() else { return response_failure( None, @@ -594,13 +555,7 @@ impl Worker { }) .unwrap_or_default(); let core = self.core.lock().unwrap(); - if core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - { - return response_failure(None, "get_messages", "Canonical source is unresolved", None); - } + let display = super::connection::wants_display_transcript(payload); if display && payload @@ -846,7 +801,7 @@ impl Worker { // live on disk; the killed and replaced closes archive. if reason != KillCloseReason::Shutdown { if let Some(store) = core.store.as_mut() { - if let Err(error) = store.persist_owner_metadata( + if let Err(error) = store.persist_entry( "session_state", json!({ "state": { "status": "archived" } }), ) { @@ -936,7 +891,7 @@ impl Worker { .and_then(|store| store.session_name().map(str::to_string)); if let Some(store) = core.store.as_mut() { if let Err(error) = - store.persist_owner_metadata("session_info", json!({ "name": name })) + store.persist_entry("session_info", json!({ "name": name })) { return response_failure(None, command, &error.to_string(), None); } diff --git a/crates/pa-daemon/src/worker/context_selection.rs b/crates/pa-daemon/src/worker/context_selection.rs index df8d1796a..1b013cd5b 100644 --- a/crates/pa-daemon/src/worker/context_selection.rs +++ b/crates/pa-daemon/src/worker/context_selection.rs @@ -27,24 +27,11 @@ fn current_lease(core: &SessionCore) -> Result> { !store.path.as_os_str().is_empty(), "initial context selection requires a persistent canonical store" ); - ensure!( - store.path.is_file(), - "canonical session file is unavailable" - ); let lease = store .lease .as_ref() .context("canonical session store has no runtime lease")?; - ensure!( - !core - .context_selection_uncertain - .as_ref() - .is_some_and(|uncertain| { - std::sync::Weak::ptr_eq(uncertain, &Arc::downgrade(lease)) - }), - "context selection write outcome is uncertain; reopen the canonical session before inference" - ); - lease.with_owned_mutation(&store.path, || Ok(()))?; + lease.ensure_held(&store.path)?; Ok(Arc::clone(lease)) } @@ -64,12 +51,6 @@ fn validate_source(core: &SessionCore, source: &ContextSelectionSource) -> Resul && canonical_mirror_header(&store.header)? == source.header, "canonical session identity changed during startup" ); - let header = crate::session_store::read_session_header(&store.path) - .context("canonical session header is unavailable")?; - ensure!( - canonical_mirror_header(&header)? == source.header, - "canonical session header changed during startup" - ); Ok(()) } @@ -90,24 +71,46 @@ fn commit_with( Some(_) => bail!("context strategy selection changed during startup"), None => {} } - // A write failure can follow a partial or complete durable append. Keep - // uncertainty on the worker owner so a new build/adapter cannot retry it. - if let Err(error) = write(store, selection) { - core.context_selection_uncertain = Some(source.lease.clone()); - return Err(error.context("canonical context selection write outcome is uncertain; reopen the session before inference")); - } + write(store, selection)?; Ok(()) } fn write_selection(store: &mut SessionFile, selection: &ContextSelection) -> Result<()> { - store.persist_owner_metadata( + store.persist_entry( "custom", json!({"customType": CONTEXT_SELECTION_CUSTOM_TYPE, "data": selection}), )?; Ok(()) } +struct CoreSessionEntryWriter { + core: Arc>, + source: ContextSelectionSource, +} + +impl pa_core::session::manager::SessionEntryWriter for CoreSessionEntryWriter { + fn append(&self, entry: &pa_types::session::FileEntry) -> std::io::Result<()> { + let write = || -> Result<()> { + let mut core = self.core.lock().map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; + validate_source(&core, &self.source)?; + let row: crate::session_store::SessionEntry = serde_json::from_value(serde_json::to_value(entry)?)?; + let store = core.store.as_mut().context("session store unavailable")?; + if let Some(existing) = store.entry(&row.id) { + ensure!(serde_json::to_value(existing)? == serde_json::to_value(&row)?, "session row ID collision"); + return Ok(()); + } + store.persist_built_log_entry(row)?; + Ok(()) + }; + write().map_err(std::io::Error::other) + } +} + impl InitialContextSelectionStore for CoreInitialContextSelectionStore { + fn entry_writer(&self, source: ContextSelectionSource) -> Arc { + Arc::new(CoreSessionEntryWriter { core: Arc::clone(&self.0), source }) + } + fn mirror_seed(&self) -> Result> { let core = self .0 @@ -130,51 +133,9 @@ impl InitialContextSelectionStore for CoreInitialContextSelectionStore { })) } - fn live_owner( - &self, - source: ContextSelectionSource, - ) -> Option> { - // First/lazy startup may already have an attached owner and staged - // deliveries. Actual AgentStart/serving records, not attachment or a - // membership set, distinguish it from an unsupported fresh-SDK handoff. - // Same-owner refresh retains its SDK; replacement installs a new owner. - let binding = (|| -> Result<_> { - let mut core = self - .0 - .lock() - .map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; - validate_source(&core, &source)?; - let store = core.store.as_mut().expect("validated canonical store"); - store.validate_initial_canonical_sdk()?; - let cursor = store.attach_canonical_owner()?; - let lease = store.lease.as_ref().expect("validated canonical lease"); - lease.with_owned_mutation(&store.path, || { - // Only an actual existing Core execution queue can request Replace. - // Startup projection exclusion IDs do not populate that queue. - Ok(cursor.owner) - }) - })() - .map_err(|error| error.to_string()); - Some(Arc::new(super::canonical_live::CoreCanonicalLiveOwner { - core: Arc::clone(&self.0), - source, - owner_scope: binding.clone().ok(), - bootstrap_admission: binding.map(|_| false), - })) - } - fn retire_quarantined_owner(&self) -> anyhow::Result<()> { - let mut core = self - .0 - .lock() - .map_err(|_| anyhow::anyhow!("session core lock poisoned"))?; - let store = core - .store - .as_mut() - .ok_or_else(|| anyhow::anyhow!("canonical store is unavailable"))?; - store.retire_quarantined_canonical_owner()?; - Ok(()) - } + + fn capture(&self, expected_file: &std::path::Path) -> Result { let core = self.0.lock().expect("session core lock"); @@ -195,13 +156,7 @@ impl InitialContextSelectionStore for CoreInitialContextSelectionStore { } /// The typed read refusal: a poisoned epoch may not serve its raw tail. - fn canonical_read_refusal(&self) -> Option { - let core = self.0.lock().expect("session core lock"); - core.store - .as_ref() - .is_some_and(crate::session_store::SessionFile::canonical_source_uncertain) - .then(crate::session_store::canonical_adapter::refused_poisoned_read) - } + fn commit( &self, @@ -222,376 +177,3 @@ impl InitialContextSelectionStore for CoreInitialContextSelectionStore { } } -#[cfg(test)] -mod tests { - use super::*; - - fn fixture() -> ( - tempfile::TempDir, - Arc>, - ContextSelectionSource, - ) { - let dir = tempfile::tempdir().unwrap(); - let mut store = SessionFile::create("/synthetic", None, 0); - let path = dir.path().join("canonical.jsonl"); - store.set_path(path.clone()); - store.rewrite().unwrap(); - store.lease = Some(Arc::new( - crate::lease::acquire_runtime_session_lease(&path, dir.path()).unwrap(), - )); - let core = Arc::new(Mutex::new(SessionCore::test_core( - Some(store), - "/synthetic".into(), - ))); - let source = CoreInitialContextSelectionStore(Arc::clone(&core)) - .capture(&path) - .unwrap(); - (dir, core, source) - } - - fn read_epoch() -> Arc { - use pa_core::session::canonical::{ - CanonicalCursor, CanonicalFrontier, CanonicalOwnerScope, - }; - Arc::new( - pa_core::session_engine::canonical_coordinator::CanonicalEpoch::genesis( - &CanonicalCursor { - owner: CanonicalOwnerScope::new(), - session_id: "read-refusal-session".to_owned(), - source_path: std::path::PathBuf::from("/private/read-refusal.jsonl"), - raw_source_generation: [3; 32], - header_certificate: [4; 32], - canonical_revision: 0, - branch_revision: 1, - selected_leaf: None, - frontier: CanonicalFrontier { - bytes: 5, - rows: 1, - prefix_certificate: [6; 32], - }, - }, - Arc::new(String::from("run")), - Arc::new(String::from("model")), - ) - .expect("epoch"), - ) - } - - fn choice(route: &str) -> ContextSelection { - ContextSelection { - version: 1, - strategy: route.into(), - } - } - - fn choices(path: &std::path::Path) -> Vec { - std::fs::read_to_string(path) - .unwrap() - .lines() - .map(|line| serde_json::from_str::(line).unwrap()) - .filter(|row| { - row["type"] == "custom" && row["customType"] == CONTEXT_SELECTION_CUSTOM_TYPE - }) - .collect() - } - - /// The read path's one question: a poisoned live epoch refuses, a clean one and - /// an absent one do not. `build_session_mirror` asks exactly this before it builds - /// anything, so the refusal cannot be skipped by a later branch. - #[test] - fn only_a_poisoned_epoch_refuses_the_read() { - let (_dir, core, _source) = fixture(); - let store = CoreInitialContextSelectionStore(Arc::clone(&core)); - assert!( - store.canonical_read_refusal().is_none(), - "no epoch attached is not a refusal: the pre-genesis window still reads" - ); - - let epoch = read_epoch(); - { - let mut core = core.lock().unwrap(); - core.store.as_mut().unwrap().epoch = Some( - crate::session_store::canonical_adapter::EpochHandle(Arc::clone(&epoch)), - ); - } - assert!( - store.canonical_read_refusal().is_none(), - "a clean live epoch reads normally" - ); - - // An unclassified flight drop is the uncertain write. - drop( - epoch - .begin_prepare_append( - pa_core::session::canonical::CanonicalIoStage::PreparePreflight, - ) - .expect("flight"), - ); - let refusal = store - .canonical_read_refusal() - .expect("a poisoned epoch refuses the read"); - assert_eq!( - refusal.source, - pa_core::session::canonical::CanonicalSourceDisposition::NeedsReconcile - ); - assert_eq!( - refusal.detail, - "canonical epoch is poisoned by an uncertain write" - ); - } - - #[tokio::test] - async fn fresh_ack_is_canonical_and_same_choice_appends_nothing() { - let (_dir, core, source) = fixture(); - let store = CoreInitialContextSelectionStore(Arc::clone(&core)); - store - .commit(source.clone(), choice("policy/a")) - .await - .unwrap(); - let bytes = std::fs::read(&source.path).unwrap(); - store - .commit(source.clone(), choice("policy/a")) - .await - .unwrap(); - assert_eq!(std::fs::read(&source.path).unwrap(), bytes); - let rows = choices(&source.path); - assert_eq!(rows.len(), 1); - assert_eq!( - rows[0]["data"], - json!({"version": 1, "strategy": "policy/a"}) - ); - assert!(rows[0]["id"].as_str().is_some_and(|id| !id.is_empty())); - assert_eq!( - core.lock() - .unwrap() - .store - .as_ref() - .unwrap() - .latest_context_selection() - .unwrap(), - Some(choice("policy/a")) - ); - let reopened = SessionFile::open(&source.path).unwrap(); - assert_eq!( - reopened.latest_context_selection().unwrap(), - Some(choice("policy/a")) - ); - } - - #[tokio::test] - async fn initial_selection_commits_through_already_attached_actual_owner() { - let (_dir, core, source) = fixture(); - let factory = CoreInitialContextSelectionStore(Arc::clone(&core)); - let owner = factory.live_owner(source.clone()).unwrap(); - let before = owner.snapshot().await.unwrap(); - factory - .commit(source.clone(), choice("policy/a")) - .await - .unwrap(); - let bytes = std::fs::read(&source.path).unwrap(); - factory - .commit(source.clone(), choice("policy/a")) - .await - .unwrap(); - assert_eq!(std::fs::read(&source.path).unwrap(), bytes); - assert_eq!(choices(&source.path).len(), 1); - let after = owner.snapshot().await.unwrap(); - assert_eq!( - after.checkpoint.cursor.owner, - before.checkpoint.cursor.owner - ); - assert_ne!(after.checkpoint.cursor, before.checkpoint.cursor); - } - - #[tokio::test] - async fn actual_live_adapter_can_ack_pending_prepare_and_abandon() { - let (_dir, core, source) = fixture(); - let factory = CoreInitialContextSelectionStore(Arc::clone(&core)); - let owner = factory.live_owner(source).unwrap(); - let header = core.lock().unwrap().store.as_ref().unwrap().header.clone(); - let manager = pa_core::session::manager::SessionManager::in_memory_from_header( - std::path::Path::new("/isolated"), - std::path::Path::new("/isolated/artifacts"), - header, - ) - .unwrap(); - let live = pa_core::session_engine::canonical_live::CanonicalLiveSession::new( - owner.clone(), - Arc::new(tokio::sync::Mutex::new(manager)), - ); - live.observe().await.unwrap(); - let pending = live - .prepare_mutation(pa_core::session::canonical::CanonicalMutationKind::OwnerMetadata) - .await - .unwrap(); - pending.abandon().await.unwrap(); - live.ensure_ready().unwrap(); - owner.snapshot().await.unwrap(); - live.persist_message( - serde_json::from_value( - json!({"role":"user", "content":"actual pending admission", "timestamp":7}), - ) - .unwrap(), - ) - .await - .unwrap(); - live.ensure_ready().unwrap(); - owner.snapshot().await.unwrap(); - } - - #[tokio::test] - async fn retired_live_adapter_cannot_refresh_into_successor_owner() { - let (_dir, core, source) = fixture(); - let factory = CoreInitialContextSelectionStore(Arc::clone(&core)); - let old = factory.live_owner(source.clone()).unwrap(); - let before = old.snapshot().await.unwrap(); - old.retire_completed_runtime(before.checkpoint.cursor.clone()) - .await - .unwrap(); - let fresh = factory.live_owner(source).unwrap(); - let after = fresh.snapshot().await.unwrap(); - assert_ne!( - before.checkpoint.cursor.owner, - after.checkpoint.cursor.owner - ); - assert!(old.snapshot().await.is_err()); - assert!(old - .prepare( - pa_core::session_engine::canonical_live::CanonicalLivePrepareRequest { - expected: Some(after.checkpoint.cursor), - source: pa_core::session::canonical::CanonicalSourceRequest::Full, - mode: pa_core::session::canonical::CanonicalPrepareMode::Observe, - admission: pa_core::session::canonical::CanonicalAdmissionPolicy::Preserve, - } - ) - .await - .is_err()); - } - - #[tokio::test] - async fn conflicting_or_invalid_latest_choice_is_not_replaced() { - let (_dir, core, source) = fixture(); - let store = CoreInitialContextSelectionStore(Arc::clone(&core)); - store - .commit(source.clone(), choice("policy/a")) - .await - .unwrap(); - let before = std::fs::read(&source.path).unwrap(); - assert!(store - .commit(source.clone(), choice("policy/b")) - .await - .is_err()); - assert_eq!(std::fs::read(&source.path).unwrap(), before); - core.lock().unwrap().store.as_mut().unwrap().persist_entry("custom", json!({ - "customType": CONTEXT_SELECTION_CUSTOM_TYPE, "data": {"version": 9, "strategy": "policy/a"} - })).unwrap(); - let invalid = std::fs::read(&source.path).unwrap(); - assert!(store - .commit(source.clone(), choice("policy/a")) - .await - .is_err()); - assert_eq!(std::fs::read(&source.path).unwrap(), invalid); - } - - #[test] - fn write_error_before_or_after_append_poison_survives_new_adapter() { - for after_append in [false, true] { - let (_dir, core, source) = fixture(); - let error = commit_with( - &mut core.lock().unwrap(), - &source, - &choice("policy/a"), - |store, selection| { - if after_append { - write_selection(store, selection)?; - } - bail!("injected canonical acknowledgement failure") - }, - ) - .unwrap_err(); - assert!(error.to_string().contains("write outcome is uncertain")); - let rows_before_retry = choices(&source.path).len(); - assert_eq!(rows_before_retry, usize::from(after_append)); - let new_adapter = CoreInitialContextSelectionStore(Arc::clone(&core)); - assert!(new_adapter - .capture(&source.path) - .err() - .unwrap() - .to_string() - .contains("uncertain")); - assert!(commit_with( - &mut core.lock().unwrap(), - &source, - &choice("policy/a"), - write_selection - ) - .is_err()); - assert_eq!(choices(&source.path).len(), rows_before_retry); - } - } - - #[tokio::test] - async fn stale_epoch_refuses_and_reopened_owner_revalidates_actual_row() { - let (dir, core, source) = fixture(); - commit_with( - &mut core.lock().unwrap(), - &source, - &choice("policy/a"), - |store, selection| { - write_selection(store, selection)?; - bail!("acknowledgement lost after complete canonical append") - }, - ) - .unwrap_err(); - let path = source.path.clone(); - let previous = { - let mut core = core.lock().unwrap(); - let lease = core.store.as_mut().unwrap().lease.take().unwrap(); - lease.release(); - let mut reopened = SessionFile::open(&path).unwrap(); - reopened.lease = Some(Arc::new( - crate::lease::acquire_runtime_session_lease(&path, dir.path()).unwrap(), - )); - core.store.replace(reopened) - }; - drop(previous); - let adapter = CoreInitialContextSelectionStore(Arc::clone(&core)); - let bytes = std::fs::read(&path).unwrap(); - assert!(adapter.commit(source, choice("policy/a")).await.is_err()); - let fresh = adapter.capture(&path).unwrap(); - adapter.commit(fresh, choice("policy/a")).await.unwrap(); - assert_eq!(std::fs::read(path).unwrap(), bytes); - } - - #[tokio::test] - async fn changed_header_path_shutdown_and_released_lease_refuse() { - let (_dir, core, source) = fixture(); - let adapter = CoreInitialContextSelectionStore(Arc::clone(&core)); - let mut wrong = source.clone(); - wrong.header.id = "foreign-session".into(); - assert!(adapter.commit(wrong, choice("policy/a")).await.is_err()); - let mut wrong = source.clone(); - wrong.path = wrong.path.with_file_name("foreign.jsonl"); - assert!(adapter.commit(wrong, choice("policy/a")).await.is_err()); - core.lock().unwrap().shutdown_requested = true; - assert!(adapter - .commit(source.clone(), choice("policy/a")) - .await - .is_err()); - core.lock().unwrap().shutdown_requested = false; - core.lock() - .unwrap() - .store - .as_ref() - .unwrap() - .lease - .as_ref() - .unwrap() - .release(); - assert!(adapter - .commit(source.clone(), choice("policy/a")) - .await - .is_err()); - assert!(choices(&source.path).is_empty()); - } -} diff --git a/crates/pa-daemon/src/worker/lifecycle.rs b/crates/pa-daemon/src/worker/lifecycle.rs index e71d95ca9..9be950149 100644 --- a/crates/pa-daemon/src/worker/lifecycle.rs +++ b/crates/pa-daemon/src/worker/lifecycle.rs @@ -163,11 +163,9 @@ impl Worker { let mut core = self.core.lock().unwrap(); // The step slot and the engine's admission both hold epoch clones. Clear // them here, under the one lock, so the handle taken below is the last one. - core.canonical_step.lock().unwrap().take(); core.store.as_mut().and_then(|store| store.epoch.take()) }; if let Some(engine) = self.agent_engine.as_ref() { - engine.set_canonical_step_admission(None); } // No epoch attached is the pre-genesis window: nothing to drain, and this is // not an error (the counter already states that rows were uncovered). @@ -521,7 +519,6 @@ impl Worker { if let Some(store) = core .store .as_mut() - .filter(|store| !store.is_canonical_bound()) { let _ = store.persist_entry( "custom", diff --git a/crates/pa-daemon/src/worker/queue.rs b/crates/pa-daemon/src/worker/queue.rs index 8479ecf6a..4ff13d61f 100644 --- a/crates/pa-daemon/src/worker/queue.rs +++ b/crates/pa-daemon/src/worker/queue.rs @@ -485,7 +485,6 @@ pub(crate) fn admit_goal_follow_up( if let Some(store) = guard .store .as_mut() - .filter(|store| !store.is_canonical_bound()) { let _ = store.persist_entry( "custom", diff --git a/crates/pa-daemon/src/worker/session_core.rs b/crates/pa-daemon/src/worker/session_core.rs index 4ce330f1f..8ec98aa47 100644 --- a/crates/pa-daemon/src/worker/session_core.rs +++ b/crates/pa-daemon/src/worker/session_core.rs @@ -20,13 +20,7 @@ pub(crate) struct SessionCore { /// One owner (this core), one writer (the engine's `run_turn_once` before the /// prompt), one reader (the assistant row's persist arm). The slot is stable /// across store replacement, which is why it lives here and not on the store. - pub(crate) canonical_step: std::sync::Arc< - std::sync::Mutex< - Option, - >, - >, - /// A canonical selection append may have reached disk before reporting an - /// error. Refuse inference on this lease epoch until its store is replaced. + pub(crate) context_selection_uncertain: Option>, pub(crate) cwd: String, pub(crate) steering: VecDeque, @@ -110,19 +104,7 @@ impl SessionCore { /// the actual provider callback in Core, so this unused legacy channel must /// not retain its previous serving epoch across the next ordered boundary. /// Without an epoch, standalone writes remain covered by their counter. - pub(crate) fn canonical_step_admission( - &self, - ) -> crate::session_store::canonical_adapter::CanonicalStepAdmission { - let epoch = self - .store - .as_ref() - .filter(|store| !store.is_canonical_bound()) - .and_then(|store| store.epoch.as_ref()); - crate::session_store::canonical_adapter::CanonicalStepAdmission::new( - epoch.map(|handle| std::sync::Arc::clone(&handle.0)), - std::sync::Arc::clone(&self.canonical_step), - ) - } + /// Whether a turn, compaction, or queued action is in flight (TS /// `hasOngoingSessionWork`); the supervisor-lost exit waits for it to settle. @@ -142,8 +124,6 @@ impl SessionCore { generation: String::new(), last_event_sequence: 0, store, - canonical_step: std::sync::Arc::new(std::sync::Mutex::new(None)), - context_selection_uncertain: None, cwd, steering: VecDeque::new(), follow_up: VecDeque::new(), diff --git a/crates/pa-daemon/src/worker/summary.rs b/crates/pa-daemon/src/worker/summary.rs index 0bbdc52b6..74ceea573 100644 --- a/crates/pa-daemon/src/worker/summary.rs +++ b/crates/pa-daemon/src/worker/summary.rs @@ -258,7 +258,7 @@ pub(crate) fn persist_custom_row( message: &Value, ) -> Result<()> { if let Some(store) = core.store.as_mut() { - store.persist_owner_metadata( + store.persist_entry( "custom_message", json!({ "customType": message.get("customType").cloned().unwrap_or(Value::Null), diff --git a/crates/pa-daemon/src/worker/turn.rs b/crates/pa-daemon/src/worker/turn.rs index 23ab4fb33..dbe4d7f52 100644 --- a/crates/pa-daemon/src/worker/turn.rs +++ b/crates/pa-daemon/src/worker/turn.rs @@ -338,9 +338,6 @@ impl TurnRunner { // Wire the canonical step channel for this turn: the store's attached epoch // (if any) plus the core's slot. The engine is the only writer, the row arm // below is the only reader. - self.engine.set_canonical_step_admission(Some( - self.core.lock().unwrap().canonical_step_admission(), - )); let request = PromptRequest { batch: batched .iter() @@ -563,16 +560,13 @@ impl TurnRunner { // so a verbatim copy retains nothing on the durable read. // Re-pin the boundary to the durable cut (TS: one store, // ids match by construction) before persist + broadcast. - let canonical_bound = core - .store - .as_ref() - .is_some_and(crate::session_store::SessionFile::is_canonical_bound); + let rows_persisted = engine.persists_session_entries(); if let EngineEvent::Compaction { ref mut entry, event: ref mut payload, } = event { - if !canonical_bound && !entry.is_null() { + if !rows_persisted && !entry.is_null() { let repin_started = std::time::Instant::now(); let durable_entries = core .store @@ -602,7 +596,7 @@ impl TurnRunner { } match &event { EngineEvent::ModelChange(change) => { - if canonical_bound { + if rows_persisted { // The awaited live receipt already committed and ACKed it. } else if let Some(Err(error)) = core.store.as_mut().map(|store| { store.persist_entry( @@ -624,27 +618,11 @@ impl TurnRunner { // let the user row take the step, drop the seal silently, and make // every assistant row refuse. The user and tool results are written // without touching the slot. - EngineEvent::AssistantMessage(message) if !canonical_bound => { - // The assistant row is the native final output: hand the store - // the step the engine admitted for this attempt, so the row is - // written under a sealed epoch grant. Taking it here keeps the - // step valid for exactly one row. - let step = core.canonical_step.lock().unwrap().take(); + EngineEvent::AssistantMessage(message) if !rows_persisted => { if let Some(store) = core.store.as_mut() { - let persisted = match step.as_ref() { - Some(step) => store - .persist_message_with_step(json!({ "message": message }), step), - None => { - store.persist_entry("message", json!({ "message": message })) - } - }; - if let Err(error) = persisted { - // A refused assistant row is a bug to SURFACE, never a - // row to drop quietly: the epoch had work admitted and - // the row did not reach the file. - eprintln!( - "pa-daemon: assistant row refused by its epoch grant: {error:#}" - ); + if let Err(error) = store.persist_entry("message", json!({ "message": message })) { + eprintln!("pa-daemon: assistant persistence failed: {error:#}"); + return false; } } } @@ -653,14 +631,14 @@ impl TurnRunner { // The exact returned ID enters the delivery FIFO; the live SDK // later adopts this same row and never writes a second copy. if let Some(store) = core.store.as_mut() { - if let Err(error) = store.persist_delivered_user(message.clone()) { + if let Err(error) = store.persist_entry("message", json!({"message": message})) { eprintln!("pa-daemon: accepted user row refused: {error:#}"); return false; } } } EngineEvent::UserMessage(message) | EngineEvent::ToolResultMessage(message) - if !canonical_bound => + if !rows_persisted => { if let Some(store) = core.store.as_mut() { if let Err(error) = @@ -681,7 +659,7 @@ impl TurnRunner { core.running_tool_calls.remove(tool_call_id); } EngineEvent::CustomMessage(message) - if accepted_row || emitting_prefix_rows.get() || !canonical_bound => + if accepted_row || emitting_prefix_rows.get() || !rows_persisted => { if let Some(store) = core.store.as_mut() { let Some(mut fields) = message.as_object().cloned() else { @@ -694,11 +672,11 @@ impl TurnRunner { fields.remove("timestamp"); let fields = Value::Object(fields); let persisted = if accepted_row || emitting_prefix_rows.get() { - store.persist_delivered_custom(fields) + store.persist_entry("custom_message", fields) } else { // Pre-build UI records (for example slash-command // results) are durable, but have no SDK delivery. - store.persist_owner_metadata("custom_message", fields) + store.persist_entry("custom_message", fields) }; if let Err(error) = persisted { eprintln!("pa-daemon: custom row persistence refused: {error:#}"); @@ -706,7 +684,7 @@ impl TurnRunner { } } } - EngineEvent::Compaction { entry, .. } if !canonical_bound => { + EngineEvent::Compaction { entry, .. } if !rows_persisted => { // A skipped compaction carries a null entry: // publish the event, never persist it. if let Some(store) = core.store.as_mut().filter(|_| !entry.is_null()) { @@ -721,7 +699,7 @@ impl TurnRunner { } } // The durable mirror of a goal-state change: each row is the new state. - EngineEvent::GoalUpdate { goal } if !canonical_bound => { + EngineEvent::GoalUpdate { goal } if !rows_persisted => { if let Some(store) = core.store.as_mut() { let _ = store.persist_entry( "custom", @@ -1001,7 +979,7 @@ impl TurnRunner { let outcome = crate::session_commands::custom_message_value(&outcome); if outcome.is_object() { if let Some(store) = core.store.as_mut() { - if let Err(error) = store.persist_owner_metadata( + if let Err(error) = store.persist_entry( "custom_message", json!({ "customType": outcome.get("customType").cloned().unwrap_or(Value::Null), diff --git a/crates/pa-daemon/src/worker/turn_stream_tests.rs b/crates/pa-daemon/src/worker/turn_stream_tests.rs index e50e423d0..ce9e9a2f0 100644 --- a/crates/pa-daemon/src/worker/turn_stream_tests.rs +++ b/crates/pa-daemon/src/worker/turn_stream_tests.rs @@ -23,7 +23,6 @@ fn burst_runner(engine: Arc) -> TurnRunner { generation: "gen".to_string(), last_event_sequence: 0, store: None, - canonical_step: std::sync::Arc::new(std::sync::Mutex::new(None)), context_selection_uncertain: None, cwd: String::new(), steering: VecDeque::new(),