pub struct PersistentBuffer { /* private fields */ }Expand description
Persistent buffer for crash recovery
This buffer continuously stores session context entries. If all other detection methods fail, we can recover from this buffer.
§Buffer Lifecycle
- Start buffering when session starts
- Continuously append context entries
- Periodically flush to disk
- Recover after crash
- Clear buffer after successful storage
§Example
use nexus_memory_hooks::buffer::PersistentBuffer;
use nexus_memory_hooks::session::SessionContext;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut buffer = PersistentBuffer::new(None)?;
// Start buffering for an agent
buffer.start_buffering("claude-code").await?;
// Buffer context periodically
let ctx = SessionContext::new("claude-code");
buffer.buffer_context("claude-code", ctx, "checkpoint").await?;
// Recover after crash
if let Some(data) = buffer.recover_buffer("claude-code").await? {
println!("Recovered {} entries", data.entries.len());
}
// Clear after successful storage
buffer.clear_buffer("claude-code").await?;
Ok(())
}Implementations§
Source§impl PersistentBuffer
impl PersistentBuffer
Sourcepub fn new(buffer_dir: Option<PathBuf>) -> Result<Self>
pub fn new(buffer_dir: Option<PathBuf>) -> Result<Self>
Create a new persistent buffer
§Arguments
buffer_dir- Directory for buffer files (default: ~/.local/share/nexus/buffer)
Sourcepub fn with_flush_interval(self, secs: u64) -> Self
pub fn with_flush_interval(self, secs: u64) -> Self
Set flush interval
Sourcepub fn with_max_entries(self, max: usize) -> Self
pub fn with_max_entries(self, max: usize) -> Self
Set max entries before auto-flush
Sourcepub async fn start_buffering(&self, agent_type: &str) -> Result<()>
pub async fn start_buffering(&self, agent_type: &str) -> Result<()>
Start buffering for an agent
Sourcepub async fn buffer_context(
&self,
agent_type: &str,
context: SessionContext,
context_type: &str,
) -> Result<()>
pub async fn buffer_context( &self, agent_type: &str, context: SessionContext, context_type: &str, ) -> Result<()>
Buffer a context entry
Sourcepub async fn flush_to_disk(&self, agent_type: &str) -> Result<()>
pub async fn flush_to_disk(&self, agent_type: &str) -> Result<()>
Flush buffer to disk
Sourcepub async fn recover_buffer(
&self,
agent_type: &str,
) -> Result<Option<BufferData>>
pub async fn recover_buffer( &self, agent_type: &str, ) -> Result<Option<BufferData>>
Recover buffered context after crash
Sourcepub async fn clear_buffer(&self, agent_type: &str) -> Result<()>
pub async fn clear_buffer(&self, agent_type: &str) -> Result<()>
Clear buffer after successful storage
Sourcepub async fn get_buffer_status(&self, agent_type: &str) -> Option<BufferStatus>
pub async fn get_buffer_status(&self, agent_type: &str) -> Option<BufferStatus>
Get buffer status
Sourcepub async fn list_buffers(&self) -> Vec<BufferStatus>
pub async fn list_buffers(&self) -> Vec<BufferStatus>
List all active buffers
Sourcepub async fn has_buffer(&self, agent_type: &str) -> bool
pub async fn has_buffer(&self, agent_type: &str) -> bool
Check if buffer exists for agent
Auto Trait Implementations§
impl Freeze for PersistentBuffer
impl !RefUnwindSafe for PersistentBuffer
impl Send for PersistentBuffer
impl Sync for PersistentBuffer
impl Unpin for PersistentBuffer
impl UnsafeUnpin for PersistentBuffer
impl !UnwindSafe for PersistentBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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