Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id>`, one budgeted step over the spool.
The hook then runs `funes index --harness <id>`, 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
Expand Down
4 changes: 2 additions & 2 deletions docs/automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <that
file>` — or, as an installed [integration](add.md#the-integration-contract), into the agent's spool
followed by `funes index --harness <id>`. 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 <file>`
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 <file>`
validates a producer's output without writing anything, so run it before wiring the hook.

## How it behaves
Expand Down
4 changes: 1 addition & 3 deletions src/commands/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ async fn acquire_lock(interactive: bool) -> Result<lock::MemoryLock> {
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.
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<memory::lock::Busy>().is_some() {
eprintln!("Error: {e:?}");
std::process::exit(EX_TEMPFAIL);
}
}
ran
}

async fn run() -> Result<()> {
Expand Down
17 changes: 15 additions & 2 deletions src/memory/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<bool> {
Expand Down
15 changes: 15 additions & 0 deletions tests/store_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
Loading