From a424c1c4d51a62e27ee833826a7508abc6d35ac2 Mon Sep 17 00:00:00 2001 From: Eureka Date: Tue, 17 Nov 2026 02:02:25 +0000 Subject: [PATCH] huginn-sources: every run takes the lock (chat-off included) (#68) --- .../huginn-daemon/src/bin/huginn-sources.rs | 52 +++++++++++++------ docs/chat-retrieval-map.md | 6 ++- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/crates/huginn-daemon/src/bin/huginn-sources.rs b/crates/huginn-daemon/src/bin/huginn-sources.rs index e75e608..5347c04 100644 --- a/crates/huginn-daemon/src/bin/huginn-sources.rs +++ b/crates/huginn-daemon/src/bin/huginn-sources.rs @@ -4,9 +4,9 @@ //! nonzero if either failed or the collection could not be used. The //! `chat-purge` subcommand writes a steward's purge tombstone and nothing else. //! -//! Exit status: 0 clean, 1 a run failed, 2 bad arguments, 3 another run holds -//! the lock. The Matrix token is read from a file (a systemd credential), -//! never from the environment or argv. +//! Exit status: 0 clean, 1 a run failed, 2 bad arguments, 3 another run (or a +//! purge) holds the lock, which every run and every purge takes. The Matrix +//! token is read from a file (a systemd credential), never from the environment or argv. use std::collections::{BTreeMap, BTreeSet}; use std::fs::{File, OpenOptions, TryLockError}; @@ -43,7 +43,8 @@ struct Options { embedding_model: String, forge_url: String, chat: Option, - lock_file: Option, + /// Every run takes it: the cursors object is replaced whole, with or without chat. + lock_file: PathBuf, } enum Command { @@ -150,8 +151,7 @@ fn parse(mut args: impl Iterator, now: DateTime) -> Result Result { } } -fn lock_if(path: &Option) -> Result, Stop> { - path.as_deref().map(take_lock).transpose() -} - fn execute(command: Command) -> Result { match command { Command::Purge { qdrant_url, lock_file, purge } => { @@ -196,7 +192,7 @@ fn execute(command: Command) -> Result { }) } Command::Run(options) => { - let _lock = lock_if(&options.lock_file)?; + let _lock = take_lock(&options.lock_file)?; let mut embedder = pool(&options.ollama_urls, &options.reserve_urls, &options.embedding_model); let mut index = QdrantIndex::new(&options.qdrant_url); let mut forge = HttpForge::new(&options.forge_url); @@ -257,13 +253,15 @@ mod tests { fn chat_flags_come_all_or_none() { let (_dir, token) = with_token(" sekret-token\n"); let lock = "--lock-file /l"; + let base = format!("{BASE} {lock}"); let full = format!("{BASE} --matrix-url http://localhost --matrix-token-file {token} {IDS} {lock}"); let Ok(Command::Run(options)) = parse(args(&full), now()) else { panic!("full chat flags must parse") }; assert_eq!(options.chat.as_ref().unwrap().token, "sekret-token", "trimmed"); - assert!(matches!(parse(args(BASE), now()), Ok(Command::Run(Options { chat: None, lock_file: None, .. })))); - assert!(error(&format!("{BASE} --matrix-url http://localhost {lock}")).contains("--matrix-token-file is missing")); - assert!(error(&format!("{BASE} --matrix-token-file {token} {IDS} {lock}")).contains("--matrix-url is missing")); - assert!(error(&format!("{BASE} --matrix-url http://localhost --matrix-token-file {token} {lock}")).contains("--chat-exclude-room is missing")); + assert!(matches!(parse(args(&base), now()), Ok(Command::Run(Options { chat: None, .. })))); + assert!(error(BASE).contains("--lock-file is required"), "a chat-off run needs the lock too"); + assert!(error(&format!("{base} --matrix-url http://localhost")).contains("--matrix-token-file is missing")); + assert!(error(&format!("{base} --matrix-token-file {token} {IDS}")).contains("--matrix-url is missing")); + assert!(error(&format!("{base} --matrix-url http://localhost --matrix-token-file {token}")).contains("--chat-exclude-room is missing")); assert!(error(&format!("{BASE} --matrix-url http://localhost --matrix-token-file {token} {IDS}")).contains("--lock-file is required")); } @@ -336,9 +334,33 @@ mod tests { let (_t, token) = with_token("sekret-token"); let run = parse(args(&format!("{BASE} --matrix-url http://localhost --matrix-token-file {token} {IDS} --lock-file {lock}")), now()).unwrap(); assert!(matches!(execute(run), Err(Stop::Locked))); + let off = parse(args(&format!("{BASE} --lock-file {lock}")), now()).unwrap(); + assert!(matches!(execute(off), Err(Stop::Locked)), "a chat-off run must not write cursors under a held lock"); drop(held); } + #[test] + fn the_lock_is_held_for_the_whole_run() { + let dir = tempfile::tempdir().unwrap(); + let lock = dir.path().join("lock"); + // A qdrant that accepts the run's first request and says nothing: the run is mid-flight while we probe the lock. + let qdrant = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}", qdrant.local_addr().unwrap()); + let run = parse(args(&format!("--state-root /s --qdrant-url {url} --ollama-url http://127.0.0.1:1 --embedding-model m --lock-file {}", lock.display())), now()).unwrap(); + let worker = std::thread::spawn(move || execute(run)); + let (connection, _) = qdrant.accept().unwrap(); + assert!(matches!(take_lock(&lock), Err(Stop::Locked)), "the run must hold the lock while it works"); + drop(connection); + // Refuse any retry the same way until the run gives up. + qdrant.set_nonblocking(true).unwrap(); + while !worker.is_finished() { + drop(qdrant.accept()); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(worker.join().unwrap().is_ok()); + assert!(take_lock(&lock).is_ok(), "the lock is released when the run ends"); + } + #[test] fn a_held_lock_stops_the_run_untouched() { let dir = tempfile::tempdir().unwrap(); diff --git a/docs/chat-retrieval-map.md b/docs/chat-retrieval-map.md index 02627a2..e205b28 100644 --- a/docs/chat-retrieval-map.md +++ b/docs/chat-retrieval-map.md @@ -125,9 +125,11 @@ data we lack). Built: cut 1 (`texts` on `SourceQuery`), chat search and window, the chat ingest core (`sources/chat.rs`, `room_scope.rs`, `chat_purge.rs`) and the ingest job. The job is `huginn-sources`: with `--matrix-url`, `--matrix-token-file`, three -`--chat-exclude-room` ids (the estate rooms) and `--lock-file` it runs the forge +`--chat-exclude-room` ids (the estate rooms) it runs the forge refresh and then `refresh_chat` through `sources/matrix.rs` (GET only, bearer -from a systemd credential file); without them it retires every chat point. The +from a systemd credential file); without them it retires every chat point. Every run, chat or not, requires +`--lock-file` and holds it for the run (exit 3 if held), as `chat-purge` does, +so a chat-off run cannot overwrite a concurrent purge tombstone. The rooms indexed are the reader's memberships; no flag lists rooms. `huginn-sources chat-purge` writes a steward's purge tombstone under the same lock. What the operator still has to do (reader account, credential, roster, unit lines) is the