The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(memory): add context bus for memory event propagation
Steven Enamakel52344207ea02
diff --git a/crates/openhuman-core/src/memory/bus.rs b/crates/openhuman-core/src/memory/bus.rsindex fdc6ddb378..00e7e30ea4 100644--- a/crates/openhuman-core/src/memory/bus.rs+++ b/crates/openhuman-core/src/memory/bus.rs@@ -132,9 +132,8 @@ pub async fn run_system_job(config: &crate::config::Config, job: &str) {tracing::debug!("[memory:bus] context disabled; refresh skipped");return;}- if let Err(error) = super::context::refresh(config).await {- tracing::debug!(code = error.code(), "[memory:bus] context refresh skipped");- }+ let compiled = super::context::refresh_all(config).await;+ tracing::debug!(compiled, "[memory:bus] context documents refreshed");}SOURCES_SYNC_JOB => {let started = super::sources::sync_due(config, Utc::now());diff --git a/crates/openhuman-core/src/memory/context.rs b/crates/openhuman-core/src/memory/context.rsindex 10b2359d84..d8b62cdba7 100644--- a/crates/openhuman-core/src/memory/context.rs+++ b/crates/openhuman-core/src/memory/context.rs@@ -1,21 +1,33 @@-//! `context.md`: the compiled memory brief, and its injection.+//! `context.md`: the compiled memory brief, one per memory node, and its+//! injection.//!-//! [`refresh`] compiles a brief from the bound engine with+//! [`refresh_for`] compiles a brief for one node from the bound engine with//! `tinymemory-context` (four default briefs plus recent learnings, trimmed to-//! `[memory.context] budget_tokens`) and writes it to-//! `<workspace>/memory/context.md`, with its timestamp and token count in-//! `context_state.json` beside it. The `memory_context_refresh` cron job calls-//! it every `interval_mins`; `memory_context_refresh` calls it on demand.+//! `[memory.context] budget_tokens`), reading only that node's reach — its own+//! memory and the nodes it inherits — and writes it beside a+//! `context_state.json` holding its timestamp and token count:+//!+//! - the root (the main agent, shared by everyone): `<workspace>/memory/context.md`;+//! - any other node: `<workspace>/memory/context/<kind>-<id>/…/context.md`,+//! one directory per segment (`context/agent-researcher/context.md`).+//!+//! The `memory_context_refresh` cron job refreshes the root and every node+//! that has a document or a `[memory.agents.<id>]` entry ([`refresh_all`]);+//! `memory_context_refresh` refreshes one node on demand.//!//! [`injection_block`] is what the session host prepends to the first user-//! message of a **new** session, wrapped in `<memory-context>…</memory-context>`.-//! A resumed session never gets it again: its transcript is frozen.+//! message of a **new** session, wrapped in `<memory-context>…</memory-context>`:+//! the acting agent's own document ([`super::scope::current`]), or, until+//! that is first compiled (which it then starts in the background), the+//! nearest ancestor's. A resumed session never gets it again: its transcript+//! is frozen.use std::path::{Path, PathBuf};use chrono::{DateTime, Utc};use serde::{Deserialize, Serialize};use tinymemory::context::{Brief, ContextCompiler, ContextSpec, DEFAULT_LEARNINGS_LIMIT};+use tinymemory::{Namespace, Reach, Segment, SegmentKind};use crate::config::Config;@@ -48,34 +60,64 @@ fn memory_dir(workspace_dir: &Path) -> PathBuf {workspace_dir.join("memory")}-/// Where `context.md` lives.+/// The directory holding every non-root node's document.+fn nodes_dir(workspace_dir: &Path) -> PathBuf {+ memory_dir(workspace_dir).join("context")+}++/// The directory `namespace`'s document lives in.+fn node_dir(workspace_dir: &Path, namespace: &Namespace) -> PathBuf {+ if namespace.is_root() {+ return memory_dir(workspace_dir);+ }+ let mut dir = nodes_dir(workspace_dir);+ for segment in namespace.segments() {+ dir.push(format!("{}-{}", segment.kind().as_str(), segment.id()));+ }+ dir+}++/// Where the root's `context.md` lives.#[must_use]pub fn context_path(workspace_dir: &Path) -> PathBuf {- memory_dir(workspace_dir).join("context.md")+ context_path_for(workspace_dir, &Namespace::ROOT)+}++/// Where `namespace`'s `context.md` lives.+#[must_use]+pub fn context_path_for(workspace_dir: &Path, namespace: &Namespace) -> PathBuf {+ node_dir(workspace_dir, namespace).join("context.md")}-fn state_path(workspace_dir: &Path) -> PathBuf {- memory_dir(workspace_dir).join("context_state.json")+fn state_path(workspace_dir: &Path, namespace: &Namespace) -> PathBuf {+ node_dir(workspace_dir, namespace).join("context_state.json")}-fn read_state(workspace_dir: &Path) -> ContextState {- std::fs::read_to_string(state_path(workspace_dir))+fn read_state(workspace_dir: &Path, namespace: &Namespace) -> ContextState {+ std::fs::read_to_string(state_path(workspace_dir, namespace)).ok().and_then(|text| serde_json::from_str(&text).ok()).unwrap_or_default()}-fn read_markdown(workspace_dir: &Path) -> String {- std::fs::read_to_string(context_path(workspace_dir)).unwrap_or_default()+fn read_markdown(workspace_dir: &Path, namespace: &Namespace) -> String {+ std::fs::read_to_string(context_path_for(workspace_dir, namespace)).unwrap_or_default()}-/// `memory_context_get`.+/// `memory_context_get` for the root.#[must_use]pub fn view(config: &Config) -> ContextView {- let state = read_state(&config.workspace_dir);+ view_for(config, &Namespace::ROOT)+}++/// `memory_context_get` for `namespace`.+#[must_use]+pub fn view_for(config: &Config, namespace: &Namespace) -> ContextView {+ let state = read_state(&config.workspace_dir, namespace);let settings = &config.memory.context;ContextView {- markdown: read_markdown(&config.workspace_dir),+ namespace: namespace.to_string(),+ markdown: read_markdown(&config.workspace_dir, namespace),tokens: state.tokens,generated_at: state.generated_at,interval_mins: settings.interval_mins,@@ -84,20 +126,87 @@ pub fn view(config: &Config) -> ContextView {}}-/// The spec compiled for `config`.+/// Every node with a compiled document, root first.+#[must_use]+pub fn compiled_nodes(workspace_dir: &Path) -> Vec<Namespace> {+ let mut found = vec![Namespace::ROOT];+ collect_nodes(&nodes_dir(workspace_dir), &Namespace::ROOT, &mut found);+ found+}++fn collect_nodes(dir: &Path, parent: &Namespace, found: &mut Vec<Namespace>) {+ let Ok(entries) = std::fs::read_dir(dir) else {+ return;+ };+ let mut children: Vec<(Namespace, PathBuf)> = entries+ .flatten()+ .filter(|entry| entry.path().is_dir())+ .filter_map(|entry| {+ let name = entry.file_name().to_string_lossy().to_string();+ let (kind, id) = name.split_once('-')?;+ let kind = SegmentKind::ALL.into_iter().find(|k| k.as_str() == kind)?;+ let node = parent.child(Segment::new(kind, id).ok()?).ok()?;+ Some((node, entry.path()))+ })+ .collect();+ children.sort();+ for (node, path) in children {+ if path.join("context.md").is_file() {+ found.push(node.clone());+ }+ collect_nodes(&path, &node, found);+ }+}++/// The spec compiled for the root.#[must_use]pub fn spec_for(config: &Config) -> ContextSpec {+ spec_for_node(config, &Namespace::ROOT)+}++/// The spec compiled for `namespace`: the node's own memory and the nodes it+/// inherits.+#[must_use]+pub fn spec_for_node(config: &Config, namespace: &Namespace) -> ContextSpec {ContextSpec {budget_tokens: config.memory.context.budget_tokens.max(MIN_BUDGET_TOKENS) as usize,briefs: Brief::defaults(),learnings_limit: DEFAULT_LEARNINGS_LIMIT,+ reach: Some(Reach::of(namespace.clone())),}}-/// Compiles `context.md` from the bound engine and writes it.+/// Compiles the root's `context.md` from the bound engine and writes it.pub async fn refresh(config: &Config) -> MemoryResult<ContextView> {+ refresh_for(config, &Namespace::ROOT).await+}++/// Compiles `namespace`'s `context.md` from the bound engine and writes it.+pub async fn refresh_for(config: &Config, namespace: &Namespace) -> MemoryResult<ContextView> {let bound = engine::resolve(config).engine()?;- refresh_with(config, &*bound.engine, ContextCompiler::new()).await+ refresh_node_with(config, namespace, &*bound.engine, ContextCompiler::new()).await+}++/// Refreshes the root and every node with a document or a configured agent,+/// skipping agents whose context is off. Returns how many were compiled.+pub async fn refresh_all(config: &Config) -> usize {+ let mut nodes = compiled_nodes(&config.workspace_dir);+ for (agent_id, settings) in &config.memory.agents {+ let node = super::scope::namespace_for(config, agent_id, None);+ if settings.context != Some(false) && !nodes.contains(&node) {+ nodes.push(node);+ }+ }+ let mut compiled = 0;+ for node in nodes {+ match refresh_for(config, &node).await {+ Ok(_) => compiled += 1,+ Err(error) => {+ tracing::debug!(namespace = %node, code = error.code(), "[memory:context] refresh skipped");+ }+ }+ }+ compiled}/// [`refresh`] against an explicit engine and compiler.@@ -105,30 +214,46 @@ pub async fn refresh_with(config: &Config,engine: &dyn tinymemory::MemoryEngine,compiler: ContextCompiler,+) -> MemoryResult<ContextView> {+ refresh_node_with(config, &Namespace::ROOT, engine, compiler).await+}++/// [`refresh_for`] against an explicit engine and compiler.+pub async fn refresh_node_with(+ config: &Config,+ namespace: &Namespace,+ engine: &dyn tinymemory::MemoryEngine,+ compiler: ContextCompiler,) -> MemoryResult<ContextView> {let doc = compiler- .compile(engine, &spec_for(config))+ .compile(engine, &spec_for_node(config, namespace)).await.map_err(|error| MemoryError::invalid(error.to_string()))?;- let dir = memory_dir(&config.workspace_dir);+ let dir = node_dir(&config.workspace_dir, namespace);let state = ContextState {generated_at: Some(doc.generated_at),tokens: doc.tokens,};let write = std::fs::create_dir_all(&dir)- .and_then(|()| std::fs::write(context_path(&config.workspace_dir), &doc.markdown))+ .and_then(|()| {+ std::fs::write(+ context_path_for(&config.workspace_dir, namespace),+ &doc.markdown,+ )+ }).and_then(|()| {let json = serde_json::to_vec_pretty(&state).map_err(std::io::Error::other)?;- std::fs::write(state_path(&config.workspace_dir), json)+ std::fs::write(state_path(&config.workspace_dir, namespace), json)});write.map_err(|error| MemoryError::Engine(format!("writing context.md failed: {error}")))?;tracing::info!(engine = %doc.engine,+ namespace = %namespace,tokens = doc.tokens,refs = doc.refs.len(),"[memory:context] context.md compiled");- Ok(view(config))+ Ok(view_for(config, namespace))}/// Applies `memory_context_set` to `config`; the caller persists it.@@ -167,26 +292,74 @@ fn strip_frontmatter(markdown: &str) -> &str {}}+/// Whether the acting agent gets a compiled context: `[memory.context]+/// enabled`, overridden by `[memory.agents.<id>] context`.+fn context_enabled(config: &Config, agent_id: Option<&str>) -> bool {+ agent_id+ .and_then(|id| config.memory.agents.get(id))+ .and_then(|settings| settings.context)+ .unwrap_or(config.memory.context.enabled)+}+/// The block a new session's first user message is prefixed with, or `None`/// when context is disabled, memory is off, or there is nothing compiled.+///+/// The document is the acting agent's ([`super::scope::current`]; the root's+/// outside any agent). A node without one yet falls back to its nearest+/// ancestor's and has its own compiled in the background, for its next+/// session.#[must_use]pub fn injection_block(config: &Config) -> Option<String> {- if !config.memory.context.enabled {+ let identity = super::scope::current().unwrap_or_else(super::scope::MemoryIdentity::root);+ if !context_enabled(config, identity.agent_id.as_deref()) {return None;}if !engine::is_on(config) {tracing::debug!("[memory:context] memory off; no context injected");return None;}- let markdown = read_markdown(&config.workspace_dir);+ let own = &identity.namespace;+ if !own.is_root() && !context_path_for(&config.workspace_dir, own).is_file() {+ compile_in_background(config, own);+ }+ let mut node = Some(own.clone());+ let markdown = loop {+ let Some(current) = node else {+ break String::new();+ };+ let markdown = read_markdown(&config.workspace_dir, ¤t);+ if !markdown.trim().is_empty() || !identity.inherit {+ break markdown;+ }+ node = current.parent();+ };let body = strip_frontmatter(&markdown).trim();if body.is_empty() {return None;}- tracing::debug!(chars = body.len(), "[memory:context] context.md injected");+ tracing::debug!(+ namespace = %own,+ chars = body.len(),+ "[memory:context] context.md injected"+ );Some(format!("{OPEN_TAG}\n{body}\n{CLOSE_TAG}"))}+/// Starts compiling `namespace`'s first document, when a runtime is there.+fn compile_in_background(config: &Config, namespace: &Namespace) {+ let Ok(handle) = tokio::runtime::Handle::try_current() else {+ return;+ };+ let config = config.clone();+ let namespace = namespace.clone();+ tracing::debug!(namespace = %namespace, "[memory:context] compiling a first context.md");+ handle.spawn(async move {+ if let Err(error) = refresh_for(&config, &namespace).await {+ tracing::debug!(namespace = %namespace, code = error.code(), "[memory:context] first compile skipped");+ }+ });+}+/// Prepends [`injection_block`] (when there is one) to a new session's first/// user message.#[must_use]diff --git a/crates/openhuman-core/src/memory/types.rs b/crates/openhuman-core/src/memory/types.rsindex 0d8e886e80..b3be57c86b 100644--- a/crates/openhuman-core/src/memory/types.rs+++ b/crates/openhuman-core/src/memory/types.rs@@ -353,6 +353,8 @@ pub struct SourcesSyncView {/// `memory_context_*` result.#[derive(Debug, Clone, Serialize)]pub struct ContextView {+ /// The memory node the document is about (`root` for the shared one).+ pub namespace: String,/// The compiled document (empty when none).pub markdown: String,/// Its estimated tokens.