From ad21196c756e9bcdfc2053c87de88309f21a0785 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 6 Nov 2026 23:18:00 +0100 Subject: [PATCH] self_run resume: public surface, sequence doc status, container-lane test across two real processes --- crates/mind-body/tests/public_surface.txt | 9 +- crates/mind-body/tests/self_run.rs | 91 +++++++++++++++++-- .../2026-10-31-live-mind-sessions-sequence.md | 2 +- 3 files changed, 92 insertions(+), 10 deletions(-) diff --git a/crates/mind-body/tests/public_surface.txt b/crates/mind-body/tests/public_surface.txt index 254996f..cc76419 100644 --- a/crates/mind-body/tests/public_surface.txt +++ b/crates/mind-body/tests/public_surface.txt @@ -220,7 +220,7 @@ mind_body::recall: impl Recalled mind_body::self_run: # [derive (Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord)] pub enum Stop { Interrupt , Terminate , Kill , } mind_body::self_run: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum Cause { Own , FlushRequested , Signalled (Stop) , Unstoppable , } mind_body::self_run: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Read { Line (String) , Timeout , Eof , Exited (Option < i32 >) , } -mind_body::self_run: # [derive (Clone , Debug , PartialEq , Eq)] pub enum SelfRunOutcome { NoRun , LeaseRefused { kind : LeaseRefusalKind } , Ended { cause : Cause , code : Option < i32 > , requests : u64 , prefix : u64 } , } +mind_body::self_run: # [derive (Clone , Debug , PartialEq , Eq)] pub enum SelfRunOutcome { NoRun , LeaseRefused { kind : LeaseRefusalKind } , Ended { cause : Cause , code : Option < i32 > , requests : u64 , prefix : u64 , warm_until : Option < DateTime < Utc > > } , } mind_body::self_run: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Usage { Request { id : String , prefix : u64 } , Echo , Result { ok : bool , blocked : bool } , Other , Unreadable , } mind_body::self_run: const AGENT : & str mind_body::self_run: const CEILING_TOKENS : u64 @@ -237,11 +237,13 @@ mind_body::self_run: const RUN_CAP_VARIABLE : & str mind_body::self_run: const SCRATCH_VARIABLE : & str mind_body::self_run: const SEED_VARIABLE : & str mind_body::self_run: const SESSION_NAMESPACE : Uuid +mind_body::self_run: const STATE_DIRECTORY_VARIABLE : & str mind_body::self_run: const TTL_1H_S : i64 mind_body::self_run: const TTL_5M_S : i64 mind_body::self_run: const TTL_MARGIN_S : i64 mind_body::self_run: fn carry (mind : & dyn MindPort , clock : & dyn Clock , lender : & dyn Lender , spawner : & dyn Spawner , run_cap_s : u64 , out : & mut dyn Write) -> Result < SelfRunOutcome > -mind_body::self_run: fn claude_args (session : Uuid) -> Vec < String > +mind_body::self_run: fn claude_args (session : Uuid , resume : Option < & Path >) -> Vec < String > +mind_body::self_run: fn continue_message (run : & PipelineRef , plan_quota : & [PlanQuota] , now_ms : u64) -> String mind_body::self_run: fn read_usage (line : & str) -> Usage mind_body::self_run: fn session_id (run : & PipelineRef) -> Uuid mind_body::self_run: fn start_message (instance : & Slug , run : & PipelineRef , plan_quota : & [PlanQuota] , now_ms : u64) -> String @@ -264,8 +266,9 @@ mind_body::self_run: impl std :: error :: Error for SeedLinkRefused mind_body::self_run: impl< 'a > SelfInbox < 'a > mind_body::self_run: mod inbox mind_body::self_run: pub trait Child { fn write_line (& mut self , line : & str) ; fn close_input (& mut self) ; fn next (& mut self , timeout : StdDuration) -> Read ; fn stop (& mut self , how : Stop) -> bool ; } -mind_body::self_run: pub trait Spawner { fn spawn (& self , args : & [String] , token : & TokenFd) -> Result < Box < dyn Child > > ; } +mind_body::self_run: pub trait Spawner { fn spawn (& self , args : & [String] , token : & TokenFd) -> Result < Box < dyn Child > > ; fn kept (& self) -> Option < Kept > { None } fn keep (& self , _keep : Option < (Uuid , DateTime < Utc >) >) -> Result < () > { Ok (()) } } mind_body::self_run: struct ClaudeSpawner pub-fields[] +mind_body::self_run: struct Kept # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[session: Uuid, transcript: PathBuf, warm_until: DateTime < Utc >, same_seed: bool] mind_body::self_run::decide: impl State mind_body::self_run::inbox: const OWNER : & str mind_body::self_run::inbox: fn backlog (mind : & dyn MindPort) -> Result < Vec < (Brief , String) > > diff --git a/crates/mind-body/tests/self_run.rs b/crates/mind-body/tests/self_run.rs index 2300795..e536718 100644 --- a/crates/mind-body/tests/self_run.rs +++ b/crates/mind-body/tests/self_run.rs @@ -185,10 +185,17 @@ mod lane { /// The daemon over a store seeded with `documents`, and the endpoint it listens on. /// It needs a built `huginn-daemon` beside the binary (`cargo build -p huginn-daemon`). fn serve(documents: Vec) -> (Daemon, TempDir, String) { - let slug = Slug(INSTANCE.into()); let state = TempDir::new().unwrap(); + admit(state.path(), documents); + let (daemon, endpoint) = daemon_over(state.path()); + (daemon, state, endpoint) + } + + /// Admits `documents` into the store at `state`, which no daemon holds open. + fn admit(state: &Path, documents: Vec) { + let slug = Slug(INSTANCE.into()); { - let mut mind = Mind::open(state.path(), &slug).unwrap(); + let mut mind = Mind::open(state, &slug).unwrap(); let batch = PipelineAdmissionBatch { instance: slug.clone(), provenance: PipelineProvenance { faculty: Faculty::Hands, agent: "test".into(), session: "test".into(), tool: "test".into() }, @@ -196,29 +203,42 @@ mod lane { }; assert!(matches!(mind.admit(batch, Utc::now()), PipelineAdmissionOutcome::Committed { .. })); } + } + + /// A daemon over the store at `state`, and the endpoint it listens on. + fn daemon_over(state: &Path) -> (Daemon, String) { let port = UdpSocket::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); let exe = Path::new(env!("CARGO_BIN_EXE_mind-self-run")); let daemon_exe = exe.with_file_name("huginn-daemon"); assert!(daemon_exe.exists(), "build huginn-daemon first: {}", daemon_exe.display()); let daemon = Daemon( Command::new(&daemon_exe) - .args(["--state-root", state.path().to_str().unwrap(), "--instance", INSTANCE, "--bind", &format!("127.0.0.1:{port}")]) + .args(["--state-root", state.to_str().unwrap(), "--instance", INSTANCE, "--bind", &format!("127.0.0.1:{port}")]) .args(["--qdrant-url", "http://127.0.0.1:1", "--ollama-url", "http://127.0.0.1:1", "--embedding-model", "x"]) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() .unwrap(), ); - (daemon, state, format!("rudp://127.0.0.1:{port}")) + (daemon, format!("rudp://127.0.0.1:{port}")) } /// The binary with `bin` first on `PATH`, until the daemon answers (exit 2 before that /// has started nothing). `SELF_RUN_ENV_CANARY` is a variable of the unit's environment. fn ask(endpoint: &str, bin: &Path, socket: &Path) -> Output { + ask_in(endpoint, bin, socket, None) + } + + /// The same, in a unit whose `StateDirectory=` is `state_directory`. + fn ask_in(endpoint: &str, bin: &Path, socket: &Path, state_directory: Option<&Path>) -> Output { let exe = Path::new(env!("CARGO_BIN_EXE_mind-self-run")); let path = format!("{}:/usr/bin:/bin", bin.display()); for _ in 0..40 { - let attempt = Command::new(exe) + let mut command = Command::new(exe); + if let Some(state_directory) = state_directory { + command.env("STATE_DIRECTORY", state_directory); + } + let attempt = command .args(["--instance", INSTANCE, "--endpoint", endpoint]) .env("PATH", &path) .env("SELF_RUN_ENV_CANARY", ENV_CANARY) @@ -336,7 +356,7 @@ echo '{first}'\necho '{second}'\nread -t 5 start\nread -t 5 flush\necho \"$start let stdout = String::from_utf8_lossy(&output.stdout).to_string(); assert_eq!(output.status.code(), Some(0), "{stdout}\n{}", String::from_utf8_lossy(&output.stderr)); - let printed: String = stdout.lines().filter(|line| !line.starts_with("decision ") && !line.starts_with("result")).map(|line| format!("{line}\n")).collect(); + let printed: String = stdout.lines().filter(|line| !line.starts_with("decision ") && !line.starts_with("result") && !line.starts_with("session ") && !line.starts_with("STATE_DIRECTORY")).map(|line| format!("{line}\n")).collect(); assert_eq!(printed, "request 1 prefix 101\nrequest 2 prefix 300000\nended: flush-requested code 0 requests 2 prefix 300000\n"); assert!(!all_output(&output).contains(ENV_CANARY), "the unit environment is not echoed"); assert!(String::from_utf8_lossy(&output.stderr).contains("journal-line"), "the child's stderr is the unit's journal"); @@ -370,4 +390,63 @@ echo '{first}'\necho '{second}'\nread -t 5 start\nread -t 5 flush\necho \"$start assert_eq!(String::from_utf8_lossy(&output.stdout), "no run: she has no single open Self run\n"); assert!(!record.exists(), "claude was started"); } + + /// Two real `mind-self-run` processes share one `STATE_DIRECTORY`. The fake `claude` + /// writes its transcript where the CLI would, prints a request that wrote an hour's + /// cache and an ok result, and exits 0: a clean end inside the cache's life. The second + /// process, under a second run, resumes the first's kept transcript by its path, forked. + #[test] + #[ignore = "container only: a built huginn-daemon beside the binary, and a POSIX shell"] + fn two_real_processes_sharing_a_state_directory_resume_across_the_unit_boundary() { + use eureka_pipeline::{PipelineKind, PipelineRef}; + use mind_body::self_run::session_id; + + let (daemon, state, endpoint) = serve(seed()); + let units_state = TempDir::new().unwrap(); + let bin = TempDir::new().unwrap(); + let argv_log = bin.path().join("argv-log"); + let body = format!( + "sid=''; prev=''; for a in \"$@\"; do [ \"$prev\" = --session-id ] && sid=$a; prev=$a; done +echo \"$@\" >> {argv_log} +mkdir -p \"$CLAUDE_CONFIG_DIR/projects/p\"; echo '{{}}' > \"$CLAUDE_CONFIG_DIR/projects/p/$sid.jsonl\" +read -t 5 start +echo '{request}'\necho '{echo}'\necho '{result}'\nexit 0", + argv_log = argv_log.display(), + request = r#"{"type":"assistant","parent_tool_use_id":null,"message":{"id":"msg_1","usage":{"input_tokens":1,"cache_read_input_tokens":100,"cache_creation_input_tokens":0,"cache_creation":{"ephemeral_1h_input_tokens":5}}}}"#, + echo = r#"{"type":"user","isReplay":true,"message":{"role":"user","content":"x"}}"#, + result = r#"{"type":"result","subtype":"success","is_error":false,"result":""}"#, + ); + fake_claude(bin.path(), &body); + let connector = fake_connector(); + + let first = ask_in(&endpoint, bin.path(), &connector.path, Some(units_state.path())); + assert_eq!(first.status.code(), Some(0), "{}", all_output(&first)); + let kept = units_state.path().join("session"); + let mut names: Vec = std::fs::read_dir(&kept).unwrap().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect(); + names.sort(); + assert_eq!(names, [format!("{LANE_SESSION}.jsonl"), "kept.json".to_string()], "the first process kept its conversation"); + + // Her second run: the first closed itself, so the store is quiet while the next is admitted. + drop(daemon); + let mut second_run = seed().pop().unwrap(); + let PipelineDocument::Run(run) = &mut second_run else { unreachable!() }; + run.label = eureka_pipeline::Label("selfrun-lane-two".into()); + admit(state.path(), vec![second_run]); + let (daemon, endpoint) = daemon_over(state.path()); + let second = ask_in(&endpoint, bin.path(), &connector.path, Some(units_state.path())); + drop(daemon); + assert_eq!(second.status.code(), Some(0), "{}", all_output(&second)); + + let second_session = session_id(&PipelineRef { kind: PipelineKind::Run, id: format!("{INSTANCE}:run:selfrun-lane-two").as_str().into() }); + let argvs = std::fs::read_to_string(&argv_log).unwrap(); + let second_argv = argvs.lines().nth(1).expect("the second process started"); + assert!(second_argv.contains(&format!("--session-id {second_session}")), "{second_argv}"); + assert!(second_argv.contains(&format!("--resume {}/{LANE_SESSION}.jsonl --fork-session", kept.display())), "{second_argv}"); + assert!(!argvs.lines().next().unwrap().contains("--resume"), "the first process had nothing to resume"); + let stdout = String::from_utf8_lossy(&second.stdout).to_string(); + assert!(stdout.contains(&format!("session resumed {LANE_SESSION}")), "{stdout}"); + let mut names: Vec = std::fs::read_dir(&kept).unwrap().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect(); + names.sort(); + assert_eq!(names, [format!("{second_session}.jsonl"), "kept.json".to_string()], "one transcript, the newer"); + } } diff --git a/docs/research/2026-10-31-live-mind-sessions-sequence.md b/docs/research/2026-10-31-live-mind-sessions-sequence.md index 7324b11..1b21c32 100644 --- a/docs/research/2026-10-31-live-mind-sessions-sequence.md +++ b/docs/research/2026-10-31-live-mind-sessions-sequence.md @@ -133,7 +133,7 @@ Live first. Memory-write runs beside cut 1 with no shared file. | 1 | **`self-session-live`** r2 (spec admitted): her Self stays alive across briefs; a brief is closed by its result; briefs to a live Self are told between tool calls; it ends at the task boundary, at the cache TTL, near the cap, or at the ceiling; a PreCompact block | Huginn | `decide.rs` `Result => CloseStdin`. A Self process cannot end at its first result, and `Busy` no longer drops a brief. | 2 | | 2 | **`persona-memory-write` r3** (spec admitted): r2 without its two `self_run.rs` changes (they move to cut 5) | Huginn | `admit_pulse` | 1 | | 3 | `plan-readout-live`: lease socket `plan` request; the Self appends a fresh plan line per batch | ModelConnector + Huginn | the start-message-only plan line | 1, 2 | -| 4 | `self-session-resume`: transcripts in StateDirectory, `--resume ` when warm, rotation under the cap, rental rule; Self capture of memory (from r2) | Huginn | `claude_args` "no resume" and the "fresh session per run" `session_id(run)` | after 1, 2 | +| 4 | `self-session-resume`: transcripts in StateDirectory, `--resume ` when warm, rotation under the cap, rental rule; Self capture of memory (from r2) | Huginn | `claude_args` "no resume" and the "fresh session per run" `session_id(run)` Status: cut `self-session-resume` builds resume and clear (the kept transcript, `--resume --fork-session`, `decide::warm_until`); the rental rule is follow-up `cut-self-session-rental`, the rotation successor is `self-rotation-successor`, and this row's memory capture is not in that cut. | after 1, 2 | | 5 | `self-reports-to-persona`: Self capture paragraph and claim/ask view (moved from r2); keeper wakes her Persona on her Self's new memory or self-run resolution | Huginn | heartbeat's `self_end > last_turn` as the only Self-to-Persona signal | after 2 | | 6 | **`persona-session`** r1 (LANDED, ruling `persona-session-landed`, deployed 2026-11-01; do not re-dispatch; spec admitted, section 4): `mind-persona` carries a leased, long-lived claude under bwrap, one conversation at a time, through the Self's own `drive`/`decide`; full shell, `huginn` and the web in `/var/lib/mind-/sandbox`; chat written as stdin lines mid-turn or idle; a constant `--json-schema`, each result decoded into one `PersonaReply` and acted on; unit cap 4 h; the gamecult-ops unit, seed and connector lease row in the same cut | Huginn + gamecult-ops | `persona.rs` `TurnRequest`, the `Connector` port and `MAX_TURNS_PER_START`; `connector.rs` `KitConnector`; the unit's connector-key credential; the per-Mind connector `--caller` rows | after 2 | | 6b | `persona-hears-her-self`: the Persona actuator polls her mind every 30 s for resolutions of the briefs she gave and memories her Self admitted, and tells them like chat ("replies when it finishes"); her Self's start message asks a first-turn acknowledgement memory ("replies within a minute"). Folds cut 5's keeper wake into the live Persona's own poll | Huginn | heartbeat's `self_end > last_turn` as the Self-to-Persona signal | after 6 (same files) |