Skip to main content

MemoryStore

Struct MemoryStore 

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

Store for generated memory state and memory extraction/consolidation jobs.

Implementations§

Source§

impl MemoryStore

Source

pub async fn clear_memory_data(&self) -> Result<()>

Deletes all persisted memory state in one transaction.

This removes every stage1_outputs row and all jobs rows for the stage-1 (memory_stage1) and phase-2 (memory_consolidate_global) memory pipelines.

Source

pub async fn record_stage1_output_usage( &self, thread_ids: &[ThreadId], ) -> Result<usize>

Record usage for cited stage-1 outputs.

Each thread id increments usage_count by one and sets last_usage to the current Unix timestamp. Missing rows are ignored.

Source

pub async fn claim_stage1_jobs_for_startup( &self, current_thread_id: ThreadId, params: Stage1StartupClaimParams<'_>, ) -> Result<Vec<Stage1JobClaim>>

Selects and claims stage-1 startup jobs for stale threads.

Query behavior:

  • starts from threads filtered to active threads and allowed sources (push_thread_filters)
  • excludes threads with memory_mode != 'enabled'
  • excludes the current thread id
  • keeps only threads whose millisecond updated_at is in the age window
  • checks memory staleness against the memories DB
  • orders by updated_at_ms DESC and applies scan_limit to bound state-DB work before probing the memories DB

For each selected thread, this function calls Self::try_claim_stage1_job with source_updated_at = thread.updated_at.timestamp() and returns up to max_claimed successful claims.

Source

pub async fn list_stage1_outputs_for_global( &self, n: usize, ) -> Result<Vec<Stage1Output>>

Lists the most recent non-empty stage-1 outputs for global consolidation.

Query behavior:

  • filters out rows where both raw_memory and rollout_summary are blank
  • hydrates thread cwd, rollout_path, and git_branch from the state DB
  • filters out missing or non-enabled threads
  • orders by source_updated_at DESC, thread_id DESC
  • returns the first n visible outputs
Source

pub async fn prune_stage1_outputs_for_retention( &self, max_unused_days: i64, limit: usize, ) -> Result<usize>

Prunes stale stage-1 outputs while preserving the latest phase-2 baseline and stage-1 job watermarks.

Query behavior:

  • considers only rows with selected_for_phase2 = 0
  • keeps recency as COALESCE(last_usage, source_updated_at)
  • removes rows older than max_unused_days
  • prunes at most limit rows ordered from stalest to newest
Source

pub async fn get_phase2_input_selection( &self, n: usize, max_unused_days: i64, ) -> Result<Vec<Stage1Output>>

Returns the current phase-2 input set.

Query behavior:

  • current selection keeps only non-empty stage-1 outputs whose last_usage is within max_unused_days, or whose source_updated_at is within that window when the memory has never been used
  • eligible rows are ranked by usage_count DESC, COALESCE(last_usage, source_updated_at) DESC, source_updated_at DESC, thread_id DESC
  • the selected top-N rows are returned in stable thread_id ASC order

The returned rows are the complete Phase 2 filesystem input. Phase 2 syncs these rows directly; deletions are represented by the workspace diff against the previous successful memory baseline.

Source

pub async fn mark_thread_memory_mode_polluted( &self, thread_id: ThreadId, ) -> Result<bool>

Marks a thread as polluted and enqueues phase-2 forgetting when the thread participated in the last successful phase-2 baseline.

Source

pub async fn try_claim_stage1_job( &self, thread_id: ThreadId, worker_id: ThreadId, source_updated_at: i64, lease_seconds: i64, max_running_jobs: usize, ) -> Result<Stage1JobClaimOutcome>

Attempts to claim a stage-1 job for a thread at source_updated_at.

Claim semantics:

  • skips as up-to-date when either:
    • stage1_outputs.source_updated_at >= source_updated_at, or
    • jobs.last_success_watermark >= source_updated_at
  • inserts or updates a jobs row to running only when:
    • global running job count for memory_stage1 is below max_running_jobs
    • existing row is not actively running with a valid lease
    • retry backoff (if present) has elapsed, or source_updated_at advanced
    • retries remain, or source_updated_at advanced (which resets retries)

The update path refreshes ownership token, lease, and input_watermark. If claiming fails, a follow-up read maps current row state to a precise skip outcome (SkippedRunning, SkippedRetryBackoff, or SkippedRetryExhausted).

Source

pub async fn mark_stage1_job_succeeded( &self, thread_id: ThreadId, ownership_token: &str, source_updated_at: i64, raw_memory: &str, rollout_summary: &str, rollout_slug: Option<&str>, ) -> Result<bool>

Marks a claimed stage-1 job successful and upserts generated output.

Transaction behavior:

  • updates jobs only for the currently owned running row
  • sets status='done' and last_success_watermark = input_watermark
  • upserts stage1_outputs for the thread, replacing existing output only when source_updated_at is newer or equal
  • preserves any existing selected_for_phase2 baseline until the next successful phase-2 run rewrites the baseline selection, including the snapshot timestamp chosen during that run
  • persists optional rollout_slug for rollout summary artifact naming
  • enqueues/advances the global phase-2 job watermark using source_updated_at
Source

pub async fn mark_stage1_job_succeeded_no_output( &self, thread_id: ThreadId, ownership_token: &str, ) -> Result<bool>

Marks a claimed stage-1 job successful when extraction produced no output.

Transaction behavior:

  • updates jobs only for the currently owned running row
  • sets status='done' and last_success_watermark = input_watermark
  • deletes any existing stage1_outputs row for the thread
  • enqueues/advances the global phase-2 job watermark using the claimed input_watermark only when deleting an existing stage1_outputs row
Source

pub async fn mark_stage1_job_failed( &self, thread_id: ThreadId, ownership_token: &str, failure_reason: &str, retry_delay_seconds: i64, ) -> Result<bool>

Marks a claimed stage-1 job as failed and schedules retry backoff.

Query behavior:

  • updates only the owned running row for (kind='memory_stage1', job_key)
  • sets status='error', clears lease, writes last_error
  • decrements retry_remaining
  • sets retry_at = now + retry_delay_seconds
Source

pub async fn enqueue_global_consolidation( &self, input_watermark: i64, ) -> Result<()>

Enqueues or advances the global phase-2 consolidation job watermark.

The underlying upsert keeps the job running when already running, resets pending/error jobs to pending, and advances input_watermark as bookkeeping even when source_updated_at is older than prior maxima. Phase 2 does not use this watermark as a dirty check; git workspace diffing decides whether consolidation work exists after the lock is claimed.

Source

pub async fn try_claim_global_phase2_job( &self, worker_id: ThreadId, lease_seconds: i64, ) -> Result<Phase2JobClaimOutcome>

Attempts to claim the global phase-2 consolidation lock.

Claim semantics:

  • reads the singleton global job row (kind='memory_consolidate_global')
  • creates and claims the singleton row when it does not exist yet
  • does not use DB watermarks to decide whether Phase 2 has work; git workspace dirtiness is the source of truth after the caller materializes inputs
  • returns SkippedRetryUnavailable when retry backoff is active
  • returns SkippedRunning when an active running lease exists
  • returns SkippedCooldown when the latest successful run finished within the phase-2 success cooldown
  • otherwise updates the row to running, sets ownership + lease, and returns Claimed
Source

pub async fn heartbeat_global_phase2_job( &self, ownership_token: &str, lease_seconds: i64, ) -> Result<bool>

Extends the lease for an owned running phase-2 global job.

Query behavior:

  • UPDATE jobs SET lease_until = ? for the singleton global row
  • requires status='running' and matching ownership_token
Source

pub async fn mark_global_phase2_job_succeeded( &self, ownership_token: &str, completed_watermark: i64, selected_outputs: &[Stage1Output], ) -> Result<bool>

Marks the owned running global phase-2 job as succeeded.

Query behavior:

  • updates only the owned running singleton global row
  • sets status='done', clears lease/errors
  • advances last_success_watermark to max(existing_last_success_watermark, completed_watermark)
  • rewrites selected_for_phase2 so only the exact selected stage-1 snapshots remain marked as part of the latest successful phase-2 selection, and persists each selected snapshot’s source_updated_at
Source

pub async fn mark_global_phase2_job_failed( &self, ownership_token: &str, failure_reason: &str, retry_delay_seconds: i64, ) -> Result<bool>

Marks the owned running global phase-2 job as failed and schedules retry.

Query behavior:

  • updates only the owned running singleton global row
  • sets status='error', clears lease
  • writes failure reason and retry time
  • decrements retry_remaining without going below zero
Source

pub async fn mark_global_phase2_job_failed_if_unowned( &self, ownership_token: &str, failure_reason: &str, retry_delay_seconds: i64, ) -> Result<bool>

Fallback failure finalization when ownership may have been lost.

Query behavior:

Trait Implementations§

Source§

impl Clone for MemoryStore

Source§

fn clone(&self) -> MemoryStore

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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> 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, U> RamaFrom<T> for U
where U: From<T>,

Source§

fn rama_from(value: T) -> U

Source§

impl<T, U, CrateMarker> RamaInto<U, CrateMarker> for T
where U: RamaFrom<T, CrateMarker>,

Source§

fn rama_into(self) -> U

Source§

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

Source§

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

Source§

fn rama_try_from(value: T) -> Result<U, <U as RamaTryFrom<T>>::Error>

Source§

impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for T
where U: RamaTryFrom<T, CrateMarker>,

Source§

type Error = <U as RamaTryFrom<T, CrateMarker>>::Error

Source§

fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T, CrateMarker>>::Error>

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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