From 631be67965d883a16ff4ab245d9227ade72b3855 Mon Sep 17 00:00:00 2001 From: David Corvoysier Date: Fri, 13 Nov 2026 12:46:28 +0000 Subject: [PATCH] feat: exit 75 when the memory is busy A command that found another funes holding the memory exited 1, like any failure. It now exits 75, sysexits' EX_TEMPFAIL, so a hook can tell a run the next turn catches up from one that failed. The lock's error is a typed Busy, which index returns too, and main maps it to the exit status. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 4 +++- docs/automation.md | 4 ++-- src/commands/index.rs | 4 +--- src/main.rs | 13 ++++++++++++- src/memory/lock.rs | 17 +++++++++++++++-- tests/store_lock.rs | 15 +++++++++++++++ 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c9a8ec77..a2f73322 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -174,7 +174,9 @@ and what it holds `funes sessions` says, each row closing on the last indexed tu a seed can skip a session whose source has not grown since; one file per session, named from the session id, so a re-emit overwrites its own file; write a temporary name in the same directory and rename it into place, with the temporary name off `.jsonl` (see [funes-jsonl.md](docs/funes-jsonl.md)). -The hook then runs `funes index --harness `, one budgeted step over the spool. +The hook then runs `funes index --harness `, one budgeted step over the spool. It exits 75 +(`EX_TEMPFAIL`) when another funes holds the memory: the next turn's run catches up, so that exit +is no failure. The maintained four register `$FUNES_BIN mcp [MEMORY]` as the agent's MCP server, convert the agent's history into the spool at install, and install automation that converts each finished diff --git a/docs/automation.md b/docs/automation.md index f7eb3a7e..249b5715 100644 --- a/docs/automation.md +++ b/docs/automation.md @@ -56,8 +56,8 @@ An agent funes has no parser for joins through the same shape: its per-turn hook that writes the session as a [`.funes.jsonl` turns file](funes-jsonl.md), then `funes index ` — or, as an installed [integration](add.md#the-integration-contract), into the agent's spool followed by `funes index --harness `. Three things to know when writing one: an explicit path is indexed in full, unbudgeted — a -single session is small, so that is what you want; a run that finds the memory lock busy fails fast, -and the next turn's run catches up (indexing is idempotent); and `funes index --check ` +single session is small, so that is what you want; a run that finds the memory lock busy fails fast +with exit code 75, and the next turn's run catches up (indexing is idempotent); and `funes index --check ` validates a producer's output without writing anything, so run it before wiring the hook. ## How it behaves diff --git a/src/commands/index.rs b/src/commands/index.rs index 8d5d413a..b524f409 100644 --- a/src/commands/index.rs +++ b/src/commands/index.rs @@ -45,9 +45,7 @@ async fn acquire_lock(interactive: bool) -> Result { tokio::time::sleep(Duration::from_secs(5)).await; } } - Err(anyhow!( - "another funes memory operation is in progress; retry in a moment" - )) + Err(lock::Busy.into()) } /// What `funes index` was asked, as the CLI help describes each. diff --git a/src/main.rs b/src/main.rs index 447e8a68..9732f69b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -313,9 +313,20 @@ impl MemoryOpts { } } +/// The exit status of a command that found the memory busy: sysexits' EX_TEMPFAIL, a temporary +/// failure. +const EX_TEMPFAIL: i32 = 75; + fn main() -> Result<()> { hub::configure_user_agent_origin(); - tokio::runtime::Runtime::new()?.block_on(run()) + let ran = tokio::runtime::Runtime::new()?.block_on(run()); + if let Err(e) = &ran { + if e.downcast_ref::().is_some() { + eprintln!("Error: {e:?}"); + std::process::exit(EX_TEMPFAIL); + } + } + ran } async fn run() -> Result<()> { diff --git a/src/memory/lock.rs b/src/memory/lock.rs index 50e243e7..dc93df96 100644 --- a/src/memory/lock.rs +++ b/src/memory/lock.rs @@ -11,7 +11,7 @@ use std::fs::{File, TryLockError}; use std::path::{Path, PathBuf}; -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use crate::home; @@ -79,9 +79,22 @@ impl MemoryLock { } fn in_progress() -> anyhow::Error { - anyhow!("another funes memory operation is in progress; retry once it finishes") + Busy.into() } +/// Another memory operation holds the lock: a temporary failure, which the same call gets past +/// once that operation finishes. +#[derive(Debug)] +pub struct Busy; + +impl std::fmt::Display for Busy { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("another funes memory operation is in progress; retry once it finishes") + } +} + +impl std::error::Error for Busy {} + /// Whether a funes up to 1.6 is writing the home's memory: those lock `store.lock` in the funes /// home rather than in the memory. pub(crate) fn older_writer_running() -> Result { diff --git a/tests/store_lock.rs b/tests/store_lock.rs index f03af6e1..836f076c 100644 --- a/tests/store_lock.rs +++ b/tests/store_lock.rs @@ -22,6 +22,21 @@ async fn memory_lock_fails_loudly_on_contention() { "acquire should report contention, got: {err}" ); + // `funes index` off a terminal, as a hook runs it, exits 75 (EX_TEMPFAIL) rather than 1. + std::fs::create_dir_all(home.path().join("spool/pi")).unwrap(); + let out = std::process::Command::new(env!("CARGO_BIN_EXE_funes")) + .args(["index", "--harness", "pi"]) + .env("HOME", home.path()) + .stdin(std::process::Stdio::piped()) + .output() + .unwrap(); + let stderr = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(75), "{stderr}"); + assert!( + stderr.contains("another funes memory operation is in progress"), + "{stderr}" + ); + // scrub refuses while the lock is held (its guard is the same acquire). let err = funes::commands::scrub::run().await.unwrap_err(); assert!(