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
chore: files changed crates/openhuman-core/src/memory/tool_writes.rs,crates/openhuman-core/src/memor
Steven Enamakel3c99d5aba312
diff --git a/crates/openhuman-core/src/memory/tool_writes.rs b/crates/openhuman-core/src/memory/tool_writes.rsindex ff43e09299..7c4ec7a739 100644--- a/crates/openhuman-core/src/memory/tool_writes.rs+++ b/crates/openhuman-core/src/memory/tool_writes.rs@@ -26,6 +26,8 @@ use tinymemory_tools::pending::{Ack, Binding, Entry, Epoch, FenceToken, Limits, PendingError, PendingWrites, WriteExecutor,WriteGate, WriteOperation,};+use tinymemory_tools::single::WriteSink;+use tinymemory_tools::ToolScope;use super::tools::CallFacts;use super::types::{ForgetParams, LearnParams};@@ -231,6 +233,79 @@ pub(super) fn enqueue(})}+/// The host policy and queue adapter used by TinyMemory's single tool.+pub(super) struct HostWriteSink {+ config: Arc<Config>,+}++impl HostWriteSink {+ pub(super) fn new(config: Arc<Config>) -> Self {+ Self { config }+ }+}++impl WriteSink for HostWriteSink {+ fn preflight(&self, _scope: &ToolScope) -> Result<()> {+ if super::engine::switched_off(&self.config) {+ return Err(Error::Off("memory is disabled".into()));+ }+ super::ops::saas_confinement(&self.config, Policy::Clamp)?;+ Ok(())+ }++ fn enqueue(&self, operation: WriteOperation, scope: &ToolScope) -> Result<Ack> {+ enqueue_operation(&self.config, operation, scope)+ }+}++fn enqueue_operation(config: &Config, operation: WriteOperation, scope: &ToolScope) -> Result<Ack> {+ if super::engine::switched_off(config) {+ return Err(Error::Off("memory is disabled".into()));+ }+ let confinement = super::ops::saas_confinement(config, Policy::Clamp)?;+ let mut reach = scope+ .reach+ .clone()+ .unwrap_or_else(|| Reach::subtree(scope.place.clone()));+ if let Some(confinement) = &confinement {+ reach = confinement.reach(Some(reach))?;+ }+ let operation = match operation {+ WriteOperation::Learn { mut item } => {+ if let Some(confinement) = &confinement {+ confinement.item(&mut item)?;+ }+ if reach.inherit || !Reach::subtree(reach.at.clone()).admits(&item.meta().namespace) {+ return Err(Error::InvalidRequest(+ "learning namespace is outside the trusted reach".into(),+ ));+ }+ WriteOperation::Learn {+ item: tinymemory_integrations::safety::scrub_item_with(+ item,+ crate::security::scrub::host_policy(),+ )+ .value,+ }+ }+ WriteOperation::Forget { ids, .. } => WriteOperation::Forget { ids, reach },+ };+ let scope_root = reach.at.clone();+ let file = queue_path(config, &scope_root);+ let _guard = LOCK+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if !file.exists() && partitions(config)?.len() >= MAX_PENDING {+ return Err(Error::Unavailable(+ "memory write queue full; nothing was queued".into(),+ ));+ }+ let owner = owner_state(config);+ queue(&file)+ .enqueue(operation, scope_root, binding(config), &owner.gate)+ .map_err(map_pending)+}+/// A [`ListStore`] over one queue file: owner-only, written through a/// temporary file and a rename, with the file and its directory fsynced.struct FileStore {diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rsindex c243456a54..be3f9e92f1 100644--- a/crates/openhuman-core/src/memory/tools.rs+++ b/crates/openhuman-core/src/memory/tools.rs@@ -1,66 +1,30 @@-//! The single `memory` agent tool: `recall | fetch | learn | forget`.-//!-//! Registered only while memory is on (see [`crate::tools::ops`]). `learn`-//! stamps the stored learning with what the host knows and the model does-//! not get to choose:-//!-//! - `workspace` — the agent's working folder (`action_dir`, or the turn's-//! isolated workspace when one is scoped);-//! - `thread_id` and `agent_id` (the memory agent id the turn runs as,-//! [`super::scope`]);-//! - `namespace` — the layout's learnings node: the root of the acting-//! identity's layout, which every agent under that root shares;-//! - `tool_call` — this call's name and provider-assigned id;-//! - `source.kind = agent`.-//!-//! `recall`, `fetch` and `forget` are confined to the identity's layout:-//! everything under its root, never another root's (another tenant's or-//! team's). A `reach` in the model's filter is overwritten.+//! The host adapter for TinyMemory's single `memory` agent tool.-use crate::tools::schema_cache::static_schema;+use std::future::Future;+use std::pin::Pin;use std::sync::Arc;+use std::time::Duration;use async_trait::async_trait;use serde_json::{json, Value};-use tinymemory_api::{- MemoryMeta, Namespace, Reach, RecallMode, SourceKind, SourceRef, ThreadWindow, ToolCallRef,-};+use tinymemory_api::{MemoryMeta, Namespace, Reach, SourceKind, SourceRef, ThreadWindow, ToolCallRef};use tinytools::{PermissionLevel, Tool, ToolCallOptions, ToolResult, ToolRunContext};+use tinymemory_tools::{+ CallContext, ConcurrencyClass, MemoryTools, PermissionClass, Refusal, RunBudget, SingleTool,+ ToolScope, concurrency_for, is_write_action, permission_for,+};use crate::config::Config;use crate::core::bus::BUS;use crate::core::events::DomainEvent;use crate::core::runtime::DispatchCtx;-use super::ops;-pub use super::tool_budget::is_budget_refusal;-use super::types::{FetchParams, ForgetParams, LearnParams, RecallParams, RecallView};-use tinymemory_api::Error;--/// How many matches one `fetch` returns; the model pages on with `cursor`.-const FETCH_TOP: usize = 5;-/// The tool's name.pub const MEMORY_TOOL_NAME: &str = "memory";+const REFUSAL_METADATA_KEY: &str = "openhuman.memory_budget";+const CONTINUE_WITHOUT_MEMORY: &str = "Continue answering the user's request without memory. Do not retry memory in this run. A timed-out write may already have been accepted; do not claim it failed or repeat it.";-/// The `memory` tool.-pub struct MemoryTool {- config: Arc<Config>,- budget: super::tool_budget::ToolBudget,-}--impl MemoryTool {- /// A tool bound to `config`'s engine.- #[must_use]- pub fn new(config: Arc<Config>) -> Self {- Self {- config,- budget: super::tool_budget::ToolBudget::default(),- }- }-}--/// What the host knows about the call that the model does not set.+/// What the host knows about a call that the model does not set.#[derive(Debug, Clone, Default, PartialEq, Eq)]pub struct CallFacts {/// The agent's working folder.@@ -71,58 +35,43 @@ pub struct CallFacts {pub agent_id: Option<String>,/// The provider-assigned tool-call id.pub tool_call_id: Option<String>,- /// The calling thread's turns still verbatim in the run's prompt, when- /// the run knows them (a turn with memory on). Reads leave these out.+ /// The calling thread's turns still verbatim in the run's prompt.pub in_prompt: Option<ThreadWindow>,- /// The chat the call's turn is surfaced in, so the memory activity it- /// raises reaches that chat. `None` outside a chat turn.+ /// The chat the call's turn is surfaced in.pub chat: Option<crate::security::approval::ApprovalChatContext>,- /// Where the agent's learnings go: its layout's learnings node.+ /// Where the agent's learnings go.pub namespace: Namespace,- /// What the agent may read and forget: its whole layout.+ /// What the agent may read and forget.pub reach: Reach,}impl CallFacts {- /// Gathers the facts from the run context,- /// falling back to `config.action_dir` for the workspace.+ /// Gathers trusted call facts, falling back to `config.action_dir`.#[must_use]pub fn gather(config: &Config, context: Option<&dyn ToolRunContext>) -> Self {let workspace = context.and_then(ToolRunContext::workspace_root).map(std::path::Path::to_path_buf).unwrap_or_else(|| config.action_dir.clone());- // The acting identity is the run's own; direct dispatch (CLI, tests)- // has no run, and falls back to the spawning agent, else the root.- let identity =- crate::agent::tinyagents::host::run_context::memory_identity_for_tool(context)- .unwrap_or_else(|| {- crate::agent::tinyagents::host::run_context::parent_agent_id_for_tool(context)- .map_or_else(- tinymemory_tools::layout::identity::Identity::root,- |parent_agent_id| {- tinymemory_tools::layout::identity::Identity::agent(- &parent_agent_id,- )- },- )- });+ let identity = crate::agent::tinyagents::host::run_context::memory_identity_for_tool(context)+ .unwrap_or_else(|| {+ crate::agent::tinyagents::host::run_context::parent_agent_id_for_tool(context)+ .map_or_else(+ tinymemory_tools::layout::identity::Identity::root,+ |parent_agent_id| tinymemory_tools::layout::identity::Identity::agent(&parent_agent_id),+ )+ });Self {workspace: Some(workspace.display().to_string()),- thread_id: context- .and_then(ToolRunContext::thread_id)- .map(str::to_string),+ thread_id: context.and_then(ToolRunContext::thread_id).map(str::to_string),tool_call_id: crate::tools::host_extensions::tool_call_id(context),chat: crate::agent::tinyagents::host::run_context::approval_for_tool(context).chat,- in_prompt: crate::agent::tinyagents::host::run_context::memory_in_prompt_for_tool(- context,- ),+ in_prompt: crate::agent::tinyagents::host::run_context::memory_in_prompt_for_tool(context),..Self::of(config, &identity)}}- /// The facts of a call made by `identity` under `config`, with no run- /// context.+ /// The facts of a call made by `identity`, without a run context.#[must_use]pub fn of(config: &Config, identity: &tinymemory_tools::layout::identity::Identity) -> Self {let resolved = identity.resolve(&crate::memory::scope::identity_pins(config));@@ -134,11 +83,12 @@ impl CallFacts {}}- /// The metadata `learn` stamps on the stored item.- #[must_use]- pub fn learn_meta(&self) -> MemoryMeta {- MemoryMeta {- namespace: self.namespace.clone(),+ fn native_scope(&self, config: &Config) -> ToolScope {+ ToolScope {+ place: self.namespace.clone(),+ reach: Some(self.reach.clone()),+ writes: true,+ zone: Some(config.time_zone()),workspace: self.workspace.clone(),thread_id: self.thread_id.clone(),agent_id: self.agent_id.clone(),@@ -146,284 +96,111 @@ impl CallFacts {name: MEMORY_TOOL_NAME.to_string(),id: self.tool_call_id.clone(),}),- source: SourceRef {- kind: SourceKind::Agent,- id: None,- },- ..MemoryMeta::default()}}}-/// What a `learn` result tells the model: the write was accepted, and-/// recall may not find it for a moment.-pub const LEARN_STATUS: &str = "saved; searchable in recall shortly";--fn arg_str(args: &Value, key: &str) -> Option<String> {- args.get(key)- .and_then(Value::as_str)- .map(str::trim)- .filter(|value| !value.is_empty())- .map(str::to_string)+/// OpenHuman's monotonic Tokio clock for TinyMemory's runtime-neutral budget.+struct TokioClock {+ origin: tokio::time::Instant,}-fn parse<T: serde::de::DeserializeOwned>(args: &Value) -> std::result::Result<T, String> {- serde_json::from_value(args.clone()).map_err(|error| format!("invalid arguments: {error}"))+impl Default for TokioClock {+ fn default() -> Self {+ Self {+ origin: tokio::time::Instant::now(),+ }+ }}-/// The turns a read inside a thread leaves out: those of the calling thread-/// the agent's prompt still holds verbatim. Turns compaction folded into a-/// checkpoint are readable again, so a long thread can recall what it said-/// before; when the run does not know its window, the whole thread is left-/// out.-fn calling_thread(facts: &CallFacts) -> Option<ThreadWindow> {- let thread_id = facts.thread_id.as_deref()?;- let window = facts- .in_prompt- .clone()- .filter(|window| window.thread_id == thread_id)- .unwrap_or_else(|| ThreadWindow::whole(thread_id));- tracing::debug!(- from_turn = window.from_turn,- "[memory:tool] reads leave out the thread's turns still in the prompt"- );- Some(window)-}+impl tinymemory_tools::BudgetClock for TokioClock {+ fn now(&self) -> Duration {+ tokio::time::Instant::now().duration_since(self.origin)+ }-/// What `recall` shows the model: the evidence to answer from and how many-/// items it holds (their ids are kept as the turn's citations), or a note-/// when nothing was recalled. The citations' snippets repeat the evidence,-/// so they are left out.-fn evidence_json(view: &RecallView) -> Value {- if view.answer.trim().is_empty() {- json!({ "evidence": "", "note": "Nothing relevant is stored in memory." })- } else {- json!({ "evidence": capped_evidence(&view.answer), "items": view.citations.len() })+ fn sleep_until(&self, deadline: Duration) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {+ Box::pin(tokio::time::sleep_until(self.origin + deadline))}}-/// About how many tokens of evidence `recall` hands the model (counted as-/// four characters a token). The rest stays reachable through `fetch`.-const EVIDENCE_TOKENS: usize = 2_500;--/// The engine's closing note when a recall searched memory only in part.-const PARTLY_SEARCHED: &str = "\n\n(Memory was only partly searched";+/// The single `memory` tool exposed to the agent.+pub struct MemoryTool {+ config: Arc<Config>,+ budget: RunBudget,+}-/// `evidence` cut to about [`EVIDENCE_TOKENS`] tokens, only ever at the end-/// of a line: whole `---`-separated entries in rank order, then as many whole-/// lines of the next entry as fit (marked `[…]`), then a note on what was-/// left out. A line is never split, so a single line longer than the budget-/// is kept whole when it is the first. The engine's closing note (memory-/// only partly searched) is always kept.-fn capped_evidence(evidence: &str) -> String {- const SEPARATOR: &str = "\n\n---\n\n";- let budget = EVIDENCE_TOKENS * 4;- if evidence.len() <= budget {- return evidence.to_string();- }- let (body, closing) = match evidence.rfind(PARTLY_SEARCHED) {- Some(at) => (&evidence[..at], &evidence[at..]),- None => (evidence, ""),- };- let entries: Vec<&str> = body.split(SEPARATOR).collect();- let mut out = String::new();- let mut whole = 0;- let mut cut_short = false;- for entry in &entries {- let joint = if out.is_empty() { "" } else { SEPARATOR };- if out.len() + joint.len() + entry.len() <= budget {- out.push_str(joint);- out.push_str(entry);- whole += 1;- continue;- }- // The entry does not fit: keep the whole lines of it that do.- let mut lines = 0;- for line in entry.split('\n') {- let piece = if lines == 0 { joint.len() } else { 1 } + line.len();- if out.len() + piece > budget && !out.is_empty() {- break;- }- out.push_str(if lines == 0 { joint } else { "\n" });- out.push_str(line);- lines += 1;- }- if lines == entry.split('\n').count() {- whole += 1;- } else if lines > 0 {- out.push_str("\n[…]");- cut_short = true;+impl MemoryTool {+ /// A tool bound to `config`'s configured memory engine.+ #[must_use]+ pub fn new(config: Arc<Config>) -> Self {+ Self {+ config,+ budget: RunBudget::new(Arc::new(TokioClock::default())),}- break;}- let left = entries.len() - whole - usize::from(cut_short);- if left > 0 || cut_short {- let shortened = if cut_short {- ", the last one shortened,"- } else {- ""- };- out.push_str(&format!(- "\n\n({left} more memory entries left out{shortened} to keep this under about \- {EVIDENCE_TOKENS} tokens; search again with more specific words to see \- others.)"- ));- }- tracing::debug!(- entries = entries.len(),- whole,- cut_short,- "[memory:tool] recall evidence capped"- );- out.push_str(closing);- out-}-/// Runs one action. Errors become a model-visible tool error, never a hard-/// failure, so the model can adapt (memory off, unsupported mode, …).-pub async fn run_action(- ctx: &DispatchCtx,- config: &Config,- args: &Value,- facts: &CallFacts,-) -> ToolResult {- let action = arg_str(args, "action").unwrap_or_default();- tracing::debug!(action = %action, "[memory:tool] dispatch");- let result: std::result::Result<Value, String> = match action.as_str() {- // The agent answers from the evidence itself, so recall writes no- // answer of its own; and the thread the call runs in is left out of- // recall and fetch, since its user turn is logged before the tool- // reads and would otherwise come back as a memory.- "recall" => match parse::<RecallParams>(args) {- Ok(mut params) => {- confine(&mut params.filter, facts);- // How much recall reads is the host's call, not the model's.- params.limit = None;- let question = params.question.clone();- ops::recall_with(config, params, RecallMode::Evidence, calling_thread(facts))- .await- .map(|view| {- if let Some(thread_id) = facts.thread_id.as_deref() {- crate::web_chat::citations::record_recall_citations(- ctx,- thread_id,- &view.citations,- );- }- BUS.publish(DomainEvent::MemoryRecalled {- query: question,- hit_count: view.citations.len(),- thread_id: facts.chat.as_ref().map(|chat| chat.thread_id.clone()),- client_id: facts.chat.as_ref().map(|chat| chat.client_id.clone()),- });- evidence_json(&view)- })- .map_err(render_error)- }- Err(error) => Err(error),- },- "fetch" => match parse::<FetchParams>(args) {- Ok(mut params) => {- confine(&mut params.filter, facts);- // Five at a time, always; more come through `cursor`.- params.limit = Some(FETCH_TOP);- ops::fetch_with(config, params, calling_thread(facts))- .await- .map(|view| {- let mut out =- json!({ "matches": fetch_page(&view), "items": view.hits.len() });- if let Some(cursor) = view.next_cursor {- out["next_cursor"] = json!(cursor);- }- out- })- .map_err(render_error)- }- Err(error) => Err(error),- },- "learn" => match parse::<LearnParams>(args) {- Ok(mut params) => {- params.meta = None;- let kind = params.kind.unwrap_or(tinymemory_api::LearningKind::Fact);- // Accepted, not visible: indexing can take minutes on a- // hosted engine and the turn must not wait for it.- ops::learn_with(- config,- params,- Some(facts.learn_meta()),- tinymemory_api::WriteOptions::accepted(),+ async fn call_with_facts(+ &self,+ args: Value,+ dispatch: DispatchCtx,+ facts: CallFacts,+ run_id: Option<&str>,+ ) -> ToolResult {+ let call = async {+ let bound = match super::engine::resolve(&self.config).engine() {+ Ok(bound) => bound,+ Err(error) => return ToolResult::error(render_error(error)),+ };+ let tools = MemoryTools::with_scope(bound.engine, facts.native_scope(&self.config));+ let single = SingleTool::new(MEMORY_TOOL_NAME, tools).with_write_sink(Arc::new(+ super::tool_writes::HostWriteSink::new(self.config.clone()),+ ));+ let outcome = match single+ .call_with_details(+ args.clone(),+ CallContext {+ thread_window: facts.in_prompt.clone(),+ },).await- .map(|view| {- BUS.publish(DomainEvent::MemoryStored {- key: view.id.clone(),- category: kind.as_str().to_string(),- namespace: "learnings".to_string(),- thread_id: facts.chat.as_ref().map(|chat| chat.thread_id.clone()),- client_id: facts.chat.as_ref().map(|chat| chat.client_id.clone()),- });- json!({"id": view.id, "status": LEARN_STATUS})- })- .map_err(render_error)+ {+ Ok(outcome) => outcome,+ Err(error) => return ToolResult::error(render_error(error)),+ };+ if let Some(recall) = outcome.recall {+ if let Some(thread_id) = facts.thread_id.as_deref() {+ crate::web_chat::citations::record_recall_citations(+ &dispatch,+ thread_id,+ &recall.citations,+ );+ }+ BUS.publish(DomainEvent::MemoryRecalled {+ query: recall.question,+ hit_count: recall.citations.len(),+ thread_id: facts.chat.as_ref().map(|chat| chat.thread_id.clone()),+ client_id: facts.chat.as_ref().map(|chat| chat.client_id.clone()),+ });}- Err(error) => Err(error),- },- "forget" => match parse::<ForgetParams>(args) {- Ok(mut params) => {- params.reach = Some(facts.reach.clone());- ops::forget(config, params)- .await- .map(|view| json!(view))- .map_err(render_error)+ if is_write_action(&args) {+ super::tool_writes::schedule(+ &dispatch,+ self.config.clone(),+ &facts.reach.at,+ );}- Err(error) => Err(error),- },- other => Err(format!(- "unknown action `{other}`; use one of recall, fetch, learn, forget"- )),- };- match result {- Ok(value) => ToolResult::success(value.to_string()),- Err(message) => ToolResult::error(message),- }-}+ ToolResult::success(outcome.value.to_string())+ };-/// What `fetch` shows the model: its top matches as dated lines, best first,-/// each with the id `forget` takes. The engine's `next_cursor`, when there-/// is one, rides beside this text; the tool description says how to use it.-fn fetch_page(view: &super::types::FetchView) -> String {- let mut out = match view.hits.len() {- 0 => "Nothing in memory matches.".to_string(),- 1 => "1 match.".to_string(),- n => format!("Top {n} matches, best first."),- };- for hit in &view.hits {- out.push_str("\n\n");- if let Some(at) = hit.meta.observed_at {- out.push_str(&format!("[{}] ", at.format("%Y-%m-%d %H:%M UTC")));- }- match hit.kind {- tinymemory_api::ItemKind::Learning => out.push_str("learning: "),- tinymemory_api::ItemKind::Document => out.push_str("document: "),- tinymemory_api::ItemKind::Conversation => {}+ if is_write_action(&args) {+ call.await+ } else {+ match self.budget.run(run_id, call).await {+ Ok(result) => result,+ Err(refusal) => budget_refusal(refusal),+ }}- out.push_str(hit.text.trim());- out.push_str(&format!("\n(id {})", hit.id));}- tracing::debug!(- hits = view.hits.len(),- "[memory:tool] fetch matches rendered"- );- out-}--/// Confines a model-supplied filter to the calling agent's reach.-fn confine(filter: &mut Option<tinymemory_api::MetaFilter>, facts: &CallFacts) {- filter.get_or_insert_with(Default::default).reach = Some(facts.reach.clone());-}--fn render_error(error: Error) -> String {- format!("{} ({})", error, error.code())}#[async_trait]@@ -433,30 +210,11 @@ impl Tool for MemoryTool {}fn description(&self) -> &str {- "Long-term memory across conversations, documents and learnings. \- `recall` searches memory for a question and returns what it holds (facts and \- dated conversation lines) for you to answer from; it does not answer for you. \- Search matches meaning and wording: if a search misses something, search again \- with different words before concluding it is not in memory, and search \- separately for each part of a question that combines several things. `fetch` \- returns matching items five at a time, best first, each with its id (filter by \- metadata such as workspace, repo, file_path, kinds). When it also returns \- `next_cursor`, more items match: for the next five of the same search, call fetch \- again with the same query and filter and `cursor` set to it. A cursor only \- continues the search it came from; for different words, fetch without one. \- `learn` queues a durable fact, preference, procedure or correction about the \- user or their work, shared with every agent working alongside you; `forget` \- queues removal of items by id. Write acknowledgements mean locally queued, \- not yet saved or removed remotely; do not repeat them to force indexing. \- Cached memory may be added as <memory-context>; use `recall` or `fetch` \- for current information, and set `refers_to` when \- the question is about a particular time. Recall before asking \- the user something they may already have told you; learn things worth \- remembering next time."+ "Long-term memory across conversations, documents and learnings. `recall` searches memory for a question and returns what it holds (facts and dated conversation lines) for you to answer from; it does not answer for you. Search matches meaning and wording: if a search misses something, search again with different words before concluding it is not in memory, and search separately for each part of a question that combines several things. `fetch` returns matching items five at a time, best first, each with its id (filter by metadata such as workspace, repo, file_path, kinds). When it also returns `next_cursor`, more items match: for the next five of the same search, call fetch again with the same query and filter and `cursor` set to it. A cursor only continues the search it came from; for different words, fetch without one. `learn` queues a durable fact, preference, procedure or correction about the user or their work, shared with every agent working alongside you; `forget` queues removal of items by id. Write acknowledgements mean locally queued, not yet saved or removed remotely; do not repeat them to force indexing. Cached memory may be added as <memory-context>; use `recall` or `fetch` for current information, and set `refers_to` when the question is about a particular time. Recall before asking the user something they may already have told you; learn things worth remembering next time."}fn parameters_schema(&self) -> Value {- static_schema!(include_str!("parameters/memory.json"))+ crate::tools::schema_cache::static_schema!(include_str!("parameters/memory.json"))}fn permission_level(&self) -> PermissionLevel {@@ -464,17 +222,14 @@ impl Tool for MemoryTool {}fn permission_level_with_args(&self, args: &Value) -> PermissionLevel {- match args.get("action").and_then(Value::as_str) {- Some("recall" | "fetch") => PermissionLevel::ReadOnly,- _ => PermissionLevel::Write,+ match permission_for(args) {+ PermissionClass::ReadOnly => PermissionLevel::ReadOnly,+ PermissionClass::Write => PermissionLevel::Write,}}fn is_concurrency_safe(&self, args: &Value) -> bool {- matches!(- args.get("action").and_then(Value::as_str),- Some("recall" | "fetch")- )+ matches!(concurrency_for(args), ConcurrencyClass::Safe)}async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {@@ -494,57 +249,67 @@ impl Tool for MemoryTool {.and_then(ToolRunContext::host_extension).and_then(|any| any.downcast_ref::<tinyagents_harness::tool::ToolExecutionContext>()).map(|ctx| ctx.run_id.as_str().to_string());- // Writes are local queue entries, bounded by the outbox (`tool_writes`),- // so only engine reads spend the run's call and latency budget.- if matches!(- args.get("action").and_then(Value::as_str),- Some("learn" | "forget")- ) {- let config = self.config.clone();- let write_args = args.clone();- let queued_root = facts.reach.at.clone();- let queued = dispatch- .spawn_blocking(move || super::tool_writes::enqueue(&config, &write_args, &facts))- .await;- return Ok(- match queued.unwrap_or_else(|_| {- Err(Error::Unavailable("memory write queue unavailable".into()))- }) {- Ok(value) => {- super::tool_writes::schedule(&dispatch, self.config.clone(), &queued_root);- ToolResult::success(value.to_string())- }- Err(error) => ToolResult::error(render_error(error)),- },- );+ Ok(self.call_with_facts(args, dispatch, facts, run_id.as_deref()).await)+ }+}++fn render_error(error: tinymemory_api::Error) -> String {+ format!("{} ({})", error, error.code())+}++fn budget_refusal(refusal: Refusal) -> ToolResult {+ match refusal {+ Refusal::Denied(reason) => {+ let message = match reason {+ tinymemory_tools::DenialReason::TrackingFull => {+ format!("Memory is busy with other runs. {CONTINUE_WITHOUT_MEMORY}")+ }+ _ => format!("Memory budget exhausted for this run. {CONTINUE_WITHOUT_MEMORY}"),+ };+ ToolResult::error(message)+ .with_metadata(json!({ REFUSAL_METADATA_KEY: "refused" }))}- Ok(self- .budget- .run(run_id, run_action(&dispatch, &self.config, &args, &facts))- .await)+ Refusal::TimedOut => ToolResult::error(format!(+ "Memory did not finish within its latency budget and is unavailable for this run. {CONTINUE_WITHOUT_MEMORY}"+ )),}}+/// Whether a call was refused before TinyMemory started its action.+pub fn is_budget_refusal(result: &ToolResult) -> bool {+ result+ .metadata+ .as_ref()+ .is_some_and(|metadata| metadata.get(REFUSAL_METADATA_KEY).is_some())+}++#[cfg(test)]+async fn run_action(+ ctx: &DispatchCtx,+ config: &Config,+ args: &Value,+ facts: &CallFacts,+) -> ToolResult {+ MemoryTool::new(Arc::new(config.clone()))+ .call_with_facts(args.clone(), ctx.clone(), facts.clone(), None)+ .await+}+#[cfg(test)]#[path = "tools_tests.rs"]mod tests;-#[cfg(test)]#[path = "tools_agent_tests.rs"]mod agent_tests;-#[cfg(test)]#[path = "tools_schema_tests.rs"]mod schema_tests;-#[cfg(test)]#[path = "tools_budget_tests.rs"]mod budget_tests;-#[cfg(test)]#[path = "tools_activity_tests.rs"]mod activity_tests;-#[cfg(test)]#[path = "tools_read_tests.rs"]mod read_tests;