Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(memory): remove unused lifecycle job helpers

Steven Enamakelb990fc8352c1
diff --git a/crates/openhuman-core/src/memory/lifecycle/jobs.rs b/crates/openhuman-core/src/memory/lifecycle/jobs.rs
index 2a719b6bb4..d1bd950299 100644
--- a/crates/openhuman-core/src/memory/lifecycle/jobs.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/jobs.rs
@@ -1,5 +1,10 @@
//! The background job queue: the slow memory work TinyMemory hands back.
//!
+//! The queue itself (dedupe, retry, history) is `tinymemory_tools::queue`;
+//! this module is the host adapter: one queue per workspace over a file
+//! store (`<workspace>/memory/jobs.json`), the engine and `[memory.recall]
+//! build_delay_secs` it runs against, and the `memory_background` cron hook.
+//!
//! `post_turn` and brain ingests return `BackgroundJob` values (belief builds,
//! deferred ingests) instead of running them. They are queued here, persisted
//! in `<workspace>/memory/jobs.json` so a restart loses none, de-duplicated
@@ -12,15 +17,15 @@
//! on every engine; the hosted engine builds on its own schedule
//! (`Scheduled`).
+use std::collections::HashMap;
use std::path::{Path, PathBuf};
-use std::sync::LazyLock;
+use std::sync::{Arc, LazyLock, Mutex};
-use chrono::{DateTime, Duration, Utc};
-use serde::{Deserialize, Serialize};
-use sha2::{Digest, Sha256};
use tinymemory_api::Namespace;
-use tinymemory_tools::{BackgroundJob, BackgroundRunner, JobOutcome, MemoryLayout};
-use tokio::sync::Mutex;
+use tinymemory_tools::queue::{JobQueue as Queue, QueueError, QueueState, QueueStore};
+use tinymemory_tools::BackgroundJob;
+
+pub use tinymemory_tools::queue::{JobRun, QueuedJob, Selection, MAX_ATTEMPTS, MAX_HISTORY};
use crate::config::Config;
use crate::memory::engine;
@@ -32,281 +37,86 @@ pub const BACKGROUND_JOB: &str = "memory_background";
/// Minutes between queue runs.
pub const BACKGROUND_INTERVAL_MINS: u64 = 5;
-/// Most finished runs kept for the UI.
-const MAX_HISTORY: usize = 50;
-
-/// Attempts before a failing job is dropped.
-const MAX_ATTEMPTS: u32 = 5;
-
-/// Serialises queue read-modify-writes in this process. Held across a run,
-/// so two runs never take the same job.
-static LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
-
-/// One queued job.
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
-pub struct QueuedJob {
- /// Stable id: a digest of the job and its root, so a duplicate merges.
- pub id: String,
- /// The layout root it runs under.
- pub root: String,
- /// The work.
- pub job: BackgroundJob,
- /// When it was first queued.
- pub queued_at: DateTime<Utc>,
- /// Failed runs so far.
- #[serde(default)]
- pub attempts: u32,
- /// The last failure.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub last_error: Option<String>,
-}
-
-/// One finished run.
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
-pub struct JobRun {
- /// The job's id.
- pub id: String,
- /// `build_beliefs` or `ingest_brain`.
- pub job: String,
- /// The layout root.
- pub root: String,
- /// When it ran.
- pub ran_at: DateTime<Utc>,
- /// `done`, `started`, `scheduled`, `skipped` or `failed`.
- pub outcome: String,
- /// Why it was skipped or failed.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub reason: Option<String>,
- /// Beliefs built, when the engine says.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub built: Option<usize>,
- /// Items stored by an ingest.
- #[serde(default)]
- pub stored: usize,
-}
-
-/// The persisted queue.
-#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
-pub struct JobQueue {
- /// Jobs waiting to run.
- #[serde(default)]
- pub pending: Vec<QueuedJob>,
- /// The latest finished runs, newest first.
- #[serde(default)]
- pub history: Vec<JobRun>,
-}
+/// The persisted queue as the UI reads it.
+pub type JobQueue = QueueState;
+/// The queue file of a workspace.
fn path(workspace_dir: &Path) -> PathBuf {
workspace_dir.join("memory").join("jobs.json")
}
-fn read(workspace_dir: &Path) -> JobQueue {
- std::fs::read_to_string(path(workspace_dir))
- .ok()
- .and_then(|text| serde_json::from_str(&text).ok())
- .unwrap_or_default()
+/// A [`QueueStore`] over `<workspace>/memory/jobs.json`.
+struct FileQueueStore {
+ file: PathBuf,
}
-fn write(workspace_dir: &Path, queue: &JobQueue) {
- let file = path(workspace_dir);
- let result = serde_json::to_vec_pretty(queue)
- .map_err(std::io::Error::other)
- .and_then(|json| crate::memory::files::write_private(&file, &json));
- if let Err(error) = result {
- tracing::warn!(%error, "[memory:jobs] writing the job queue failed");
+impl QueueStore for FileQueueStore {
+ fn load(&self) -> QueueState {
+ std::fs::read_to_string(&self.file)
+ .ok()
+ .and_then(|text| serde_json::from_str(&text).ok())
+ .unwrap_or_default()
+ }
+
+ fn save(&self, state: &QueueState) -> Result<(), String> {
+ serde_json::to_vec_pretty(state)
+ .map_err(|error| error.to_string())
+ .and_then(|json| {
+ crate::memory::files::write_private(&self.file, &json)
+ .map_err(|error| error.to_string())
+ })
}
}
-fn job_id(root: &str, job: &BackgroundJob) -> String {
- let json = serde_json::to_vec(job).unwrap_or_default();
- let mut digest = Sha256::new();
- digest.update(root.as_bytes());
- digest.update([0]);
- digest.update(&json);
- digest
- .finalize()
- .iter()
- .take(8)
- .map(|byte| format!("{byte:02x}"))
- .collect()
+type WorkspaceQueue = Queue<FileQueueStore>;
+
+/// One queue per workspace, so its lock serialises every run in the process.
+static QUEUES: LazyLock<Mutex<HashMap<PathBuf, Arc<WorkspaceQueue>>>> =
+ LazyLock::new(|| Mutex::new(HashMap::new()));
+
+fn queue_of(workspace_dir: &Path) -> Arc<WorkspaceQueue> {
+ let mut queues = QUEUES.lock().unwrap_or_else(|e| e.into_inner());
+ queues
+ .entry(workspace_dir.to_path_buf())
+ .or_insert_with(|| {
+ Arc::new(Queue::new(FileQueueStore {
+ file: path(workspace_dir),
+ }))
+ })
+ .clone()
}
/// Queues `jobs` under `root`; a job already queued is kept once, at its
/// first queue time.
pub async fn enqueue(config: &Config, root: &Namespace, jobs: Vec<BackgroundJob>) {
- if jobs.is_empty() {
- return;
- }
- let _guard = LOCK.lock().await;
- let mut queue = read(&config.workspace_dir);
- let root = root.to_string();
- let mut added = 0;
- for job in jobs {
- let id = job_id(&root, &job);
- if queue.pending.iter().any(|queued| queued.id == id) {
- continue;
- }
- tracing::debug!(id = %id, job = job.name(), root = %root, "[memory:jobs] queued");
- queue.pending.push(QueuedJob {
- id,
- root: root.clone(),
- job,
- queued_at: Utc::now(),
- attempts: 0,
- last_error: None,
- });
- added += 1;
- }
- if added > 0 {
- write(&config.workspace_dir, &queue);
- }
+ queue_of(&config.workspace_dir).enqueue(root, jobs).await;
}
/// Whether `workspace_dir` has queued jobs waiting. A cheap, lock-free read
/// for a host deciding which workspaces are worth a run.
pub fn has_pending(workspace_dir: &Path) -> bool {
- !read(workspace_dir).pending.is_empty()
+ queue_of(workspace_dir).has_pending()
}
/// The queue as it stands.
pub async fn snapshot(config: &Config) -> JobQueue {
- let _guard = LOCK.lock().await;
- read(&config.workspace_dir)
-}
-
-/// Which jobs a run takes.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum Selection {
- /// Those older than `[memory.recall] build_delay_secs`.
- Due,
- /// Every pending job, now.
- All,
- /// One job by id, now.
- One(String),
+ queue_of(&config.workspace_dir).snapshot().await
}
-/// Runs the selected jobs. Returns the runs it recorded. A job that fails is
-/// kept with its error and retried on a later run, up to a few attempts; an
-/// account-wide refusal does not count as one. The run that gives up says so
-/// in its reason.
+/// Runs the selected jobs on the configured engine. Returns the runs it
+/// recorded; see `tinymemory_tools::queue::JobQueue::run` for the retry rules.
pub async fn run(config: &Config, selection: Selection) -> MemoryResult<Vec<JobRun>> {
let bound = engine::resolve(config).engine()?;
- let _guard = LOCK.lock().await;
- let mut queue = read(&config.workspace_dir);
- let cutoff = Utc::now()
- - Duration::seconds(i64::try_from(config.memory.recall.build_delay_secs).unwrap_or(0));
- let (take, keep): (Vec<QueuedJob>, Vec<QueuedJob>) =
- queue
- .pending
- .drain(..)
- .partition(|queued| match &selection {
- Selection::Due => queued.queued_at <= cutoff,
- Selection::All => true,
- Selection::One(id) => &queued.id == id,
- });
- if let Selection::One(id) = &selection {
- if take.is_empty() {
- queue.pending = keep;
- return Err(MemoryError::invalid(format!("no queued job `{id}`")));
- }
- }
- queue.pending = keep;
- let mut runs = Vec::new();
- for mut queued in take {
- let layout = queued
- .root
- .parse::<Namespace>()
- .ok()
- .and_then(|root| MemoryLayout::new(root).ok())
- .unwrap_or_default();
- let runner = BackgroundRunner::new(bound.engine.clone(), layout);
- let name = queued.job.name().to_string();
- let mut record = JobRun {
- id: queued.id.clone(),
- job: name.clone(),
- root: queued.root.clone(),
- ran_at: Utc::now(),
- outcome: String::new(),
- reason: None,
- built: None,
- stored: 0,
- };
- match runner.run(queued.job.clone()).await {
- Ok(report) => {
- record.outcome = match &report.outcome {
- JobOutcome::Done => "done",
- JobOutcome::Started => "started",
- JobOutcome::Scheduled => "scheduled",
- JobOutcome::Skipped { .. } => "skipped",
- }
- .to_string();
- if let JobOutcome::Skipped { reason } = &report.outcome {
- record.reason = Some(reason.clone());
- }
- record.built = report
- .consolidation
- .as_ref()
- .and_then(|receipt| receipt.built);
- record.stored = report.stored.len();
- for follow_up in report.follow_ups {
- let id = job_id(&queued.root, &follow_up);
- if !queue.pending.iter().any(|pending| pending.id == id) {
- queue.pending.push(QueuedJob {
- id,
- root: queued.root.clone(),
- job: follow_up,
- queued_at: Utc::now(),
- attempts: 0,
- last_error: None,
- });
- }
- }
- tracing::info!(
- id = %queued.id,
- job = %name,
- outcome = %record.outcome,
- built = ?record.built,
- "[memory:jobs] ran"
- );
- }
- Err(error) => {
- // A refusal of the whole account (no credits, a rejected
- // credential, an unreachable engine) says nothing about the
- // job, so it keeps its attempts and waits: dropping it would
- // leave a permanent gap in the beliefs (#6718).
- let account_wide = MemoryError::from(error.clone()).is_account_wide();
- if !account_wide {
- queued.attempts += 1;
- }
- queued.last_error = Some(error.to_string());
- record.outcome = "failed".to_string();
- tracing::warn!(
- id = %queued.id,
- job = %name,
- attempts = queued.attempts,
- account_wide,
- %error,
- "[memory:jobs] run failed"
- );
- if queued.attempts < MAX_ATTEMPTS {
- record.reason = Some(error.to_string());
- queue.pending.push(queued);
- } else {
- record.reason = Some(format!(
- "dropped after {MAX_ATTEMPTS} failed attempts: {error}"
- ));
- }
- }
- }
- runs.push(record);
- }
- let mut history = runs.clone();
- history.reverse();
- history.append(&mut queue.history);
- history.truncate(MAX_HISTORY);
- queue.history = history;
- write(&config.workspace_dir, &queue);
- Ok(runs)
+ queue_of(&config.workspace_dir)
+ .run(
+ bound.engine,
+ selection,
+ config.memory.recall.build_delay_secs,
+ )
+ .await
+ .map_err(|error| match error {
+ QueueError::NoSuchJob(_) => MemoryError::invalid(error.to_string()),
+ })
}
/// The cron entry point: runs due jobs unless background work is paused.