pub struct MemoryStore { /* private fields */ }Expand description
Store for generated memory state and memory extraction/consolidation jobs.
Implementations§
Source§impl MemoryStore
impl MemoryStore
Sourcepub async fn clear_memory_data(&self) -> Result<()>
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.
Sourcepub async fn record_stage1_output_usage(
&self,
thread_ids: &[ThreadId],
) -> Result<usize>
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.
Sourcepub async fn claim_stage1_jobs_for_startup(
&self,
current_thread_id: ThreadId,
params: Stage1StartupClaimParams<'_>,
) -> Result<Vec<Stage1JobClaim>>
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
threadsfiltered 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_atis in the age window - checks memory staleness against the memories DB
- orders by
updated_at_ms DESCand appliesscan_limitto 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.
Sourcepub async fn list_stage1_outputs_for_global(
&self,
n: usize,
) -> Result<Vec<Stage1Output>>
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_memoryandrollout_summaryare blank - hydrates thread
cwd,rollout_path, andgit_branchfrom the state DB - filters out missing or non-enabled threads
- orders by
source_updated_at DESC, thread_id DESC - returns the first
nvisible outputs
Sourcepub async fn prune_stage1_outputs_for_retention(
&self,
max_unused_days: i64,
limit: usize,
) -> Result<usize>
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
limitrows ordered from stalest to newest
Sourcepub async fn get_phase2_input_selection(
&self,
n: usize,
max_unused_days: i64,
) -> Result<Vec<Stage1Output>>
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_usageis withinmax_unused_days, or whosesource_updated_atis 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 ASCorder
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.
Sourcepub async fn mark_thread_memory_mode_polluted(
&self,
thread_id: ThreadId,
) -> Result<bool>
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.
Sourcepub 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>
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, orjobs.last_success_watermark >= source_updated_at
- inserts or updates a
jobsrow torunningonly when:- global running job count for
memory_stage1is belowmax_running_jobs - existing row is not actively running with a valid lease
- retry backoff (if present) has elapsed, or
source_updated_atadvanced - retries remain, or
source_updated_atadvanced (which resets retries)
- global running job count for
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).
Sourcepub 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>
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
jobsonly for the currently owned running row - sets
status='done'andlast_success_watermark = input_watermark - upserts
stage1_outputsfor the thread, replacing existing output only whensource_updated_atis newer or equal - preserves any existing
selected_for_phase2baseline until the next successful phase-2 run rewrites the baseline selection, including the snapshot timestamp chosen during that run - persists optional
rollout_slugfor rollout summary artifact naming - enqueues/advances the global phase-2 job watermark using
source_updated_at
Sourcepub async fn mark_stage1_job_succeeded_no_output(
&self,
thread_id: ThreadId,
ownership_token: &str,
) -> Result<bool>
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
jobsonly for the currently owned running row - sets
status='done'andlast_success_watermark = input_watermark - deletes any existing
stage1_outputsrow for the thread - enqueues/advances the global phase-2 job watermark using the claimed
input_watermarkonly when deleting an existingstage1_outputsrow
Sourcepub async fn mark_stage1_job_failed(
&self,
thread_id: ThreadId,
ownership_token: &str,
failure_reason: &str,
retry_delay_seconds: i64,
) -> Result<bool>
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, writeslast_error - decrements
retry_remaining - sets
retry_at = now + retry_delay_seconds
Sourcepub async fn enqueue_global_consolidation(
&self,
input_watermark: i64,
) -> Result<()>
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.
Sourcepub async fn try_claim_global_phase2_job(
&self,
worker_id: ThreadId,
lease_seconds: i64,
) -> Result<Phase2JobClaimOutcome>
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
SkippedRetryUnavailablewhen retry backoff is active - returns
SkippedRunningwhen an active running lease exists - returns
SkippedCooldownwhen the latest successful run finished within the phase-2 success cooldown - otherwise updates the row to
running, sets ownership + lease, and returnsClaimed
Sourcepub async fn heartbeat_global_phase2_job(
&self,
ownership_token: &str,
lease_seconds: i64,
) -> Result<bool>
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 matchingownership_token
Sourcepub async fn mark_global_phase2_job_succeeded(
&self,
ownership_token: &str,
completed_watermark: i64,
selected_outputs: &[Stage1Output],
) -> Result<bool>
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_watermarktomax(existing_last_success_watermark, completed_watermark) - rewrites
selected_for_phase2so only the exact selected stage-1 snapshots remain marked as part of the latest successful phase-2 selection, and persists each selected snapshot’ssource_updated_at
Sourcepub async fn mark_global_phase2_job_failed(
&self,
ownership_token: &str,
failure_reason: &str,
retry_delay_seconds: i64,
) -> Result<bool>
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_remainingwithout going below zero
Sourcepub async fn mark_global_phase2_job_failed_if_unowned(
&self,
ownership_token: &str,
failure_reason: &str,
retry_delay_seconds: i64,
) -> Result<bool>
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:
- same state transition as
Self::mark_global_phase2_job_failed - matches rows where
ownership_token = ? OR ownership_token IS NULL - allows recovering a stuck unowned running row
Trait Implementations§
Source§impl Clone for MemoryStore
impl Clone for MemoryStore
Source§fn clone(&self) -> MemoryStore
fn clone(&self) -> MemoryStore
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for MemoryStore
impl !UnwindSafe for MemoryStore
impl Freeze for MemoryStore
impl Send for MemoryStore
impl Sync for MemoryStore
impl Unpin for MemoryStore
impl UnsafeUnpin for MemoryStore
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
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>
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>
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