Skip to main content

SessionEventLog

Struct SessionEventLog 

Source
pub struct SessionEventLog { /* private fields */ }
Expand description

Append-only JSONL log for one conversation-session’s events.jsonl.

§Examples

use tempfile::tempdir;
use zeph_session::event::SessionEvent;
use zeph_session::log::SessionEventLog;

let dir = tempdir().unwrap();
let log = SessionEventLog::open(dir.path()).await.unwrap();
log.append(None, None, SessionEvent::SessionEnded { reason: "user_quit".to_owned() })
    .await
    .unwrap();
assert_eq!(log.last_seq(), Some(0));

Implementations§

Source§

impl SessionEventLog

Source

pub async fn open(session_dir: &Path) -> Result<Self, SessionError>

Open (creating if absent) the events.jsonl log under session_dir.

Validates the existing file per INV-SP-2, dropping a torn trailing line from the in-memory result, then opens the file in append mode for subsequent writes. Sets file/directory permissions to 0o700/0o600 on Unix (spec §4.1); a no-op on other platforms.

Does not take the cross-process advisory lock, and never physically truncates the file (even if a torn tail is found) — safe for read-only tooling that may run alongside a live writer whose in-flight, not-yet-fsynced line could otherwise be mistaken for “torn” and destroyed (#5487 Finding B). The session’s owning actor/agent process should use Self::open_exclusive instead, which does perform the physical repair.

§Errors

Returns SessionError::Io if the directory or file cannot be created, or SessionError::Serde surfaces only via Self::read_all, never here (torn lines are discarded, not treated as fatal).

Source

pub async fn open_exclusive(session_dir: &Path) -> Result<Self, SessionError>

Open the events.jsonl log under session_dir like Self::open, but additionally take a non-blocking, exclusive advisory lock (flock(2) on Unix, mirroring zeph-scheduler’s PidFile) enforcing INV-D2’s single-writer invariant.

Intended for the session’s owning actor/agent process. On non-Unix targets the lock is a no-op (the workspace has no vetted cross-platform advisory-locking primitive), so this degrades to Self::open’s behavior there.

§Errors

Returns SessionError::AlreadyLocked if another process already holds the session’s write lock, or any error Self::open can return.

Source

pub fn path(&self) -> &Path

The path to this session’s events.jsonl file.

Source

pub fn last_seq(&self) -> Option<u64>

The highest seq durably appended so far, or None if the log is empty.

Source

pub async fn append( &self, turn_id: Option<u64>, parent_seq: Option<u64>, kind: SessionEvent, ) -> Result<SessionEventEnvelope, SessionError>

Append one event, assigning it the next monotonic seq, and fsync before returning.

The single write_all + sync_all pair is the atomicity boundary INV-SP-2 relies on: a crash mid-write can only ever corrupt this one trailing line.

§Errors

Returns SessionError::Serde if the event cannot be JSON-encoded, or SessionError::Io if the write or fsync fails.

Source

pub async fn read_all(&self) -> Result<Vec<SessionEventEnvelope>, SessionError>

Read and validate every event currently in the log, dropping a torn trailing line from the result (INV-SP-2). Only physically repairs the file if this handle was opened via Self::open_exclusive — see that method’s doc comment.

§Errors

Returns SessionError::Io if the file cannot be read.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more