From b355bd50d2b770530e19f80bf299986a230db222 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 31 Oct 2026 14:42:22 +0100 Subject: [PATCH] wip: live self session --- crates/mind-body/src/bin/mind-self-run.rs | 26 +- crates/mind-body/src/self_run.rs | 195 +++++++- crates/mind-body/src/self_run/decide.rs | 456 +++++++++++------- crates/mind-body/src/self_run/inbox.rs | 305 ++++++++++++ crates/mind-body/src/self_run/tests.rs | 46 +- .../mind-body/src/self_run/tests/closure.rs | 6 +- crates/mind-body/src/self_run/tests/lease.rs | 2 +- .../src/self_run/tests/process/lease_child.rs | 10 +- 8 files changed, 831 insertions(+), 215 deletions(-) create mode 100644 crates/mind-body/src/self_run/inbox.rs diff --git a/crates/mind-body/src/bin/mind-self-run.rs b/crates/mind-body/src/bin/mind-self-run.rs index 52322cf..3a1e9b8 100644 --- a/crates/mind-body/src/bin/mind-self-run.rs +++ b/crates/mind-body/src/bin/mind-self-run.rs @@ -1,3 +1,8 @@ +//! `mind-self-run inject --spool [--pre-compact]` is the hook `claude` runs +//! inside the session: it prints the briefs waiting in the spool as context for +//! the next request, and with `--pre-compact` blocks an auto-compaction (exit 2). +//! It reads the hook's JSON on stdin and the spool, nothing else. +//! //! `mind-self-run --instance [--endpoint rudp://:]`: carries //! her one open Self run (`mind_body::self_run::carry`) inside //! `mind-self@`. This reads the process's arguments and environment, @@ -13,6 +18,8 @@ //! The connector's lease socket is `CLAUDE_LEASE_SOCKET`: the token itself is //! never in this process's environment, only the descriptor the lease grants. +use std::io::Read; +use std::path::PathBuf; use std::time::Duration; use anyhow::{Result, anyhow}; @@ -21,6 +28,7 @@ use mind_body::cli::instance_and_endpoint; use mind_body::launch::SystemClock; use mind_body::lease_client::{LEASE_SOCKET_VARIABLE, SocketLender}; use mind_body::queue::HuginnMind; +use mind_body::self_run::inbox::inject; use mind_body::self_run::{ClaudeSpawner, RUN_CAP_VARIABLE, carry}; /// How long one call to the mind may take. @@ -33,6 +41,9 @@ const USAGE: &str = "usage: mind-self-run --instance [--endpoint rudp://< #[allow(clippy::disallowed_methods, reason = "the binary reads its own arguments and environment, and hands them to the library")] fn run() -> Result { let args: Vec = std::env::args().skip(1).collect(); + if args.first().map(String::as_str) == Some("inject") { + return hook(&args[1..]); + } let (instance, address) = instance_and_endpoint(&args, std::env::var("HUGINN_ENDPOINT").ok(), USAGE)?; let socket = std::env::var_os(LEASE_SOCKET_VARIABLE).ok_or_else(|| anyhow!("{LEASE_SOCKET_VARIABLE} is not set"))?; let run_cap_s: u64 = std::env::var(RUN_CAP_VARIABLE) @@ -41,11 +52,24 @@ fn run() -> Result { .filter(|seconds| *seconds > 0) .ok_or_else(|| anyhow!("{RUN_CAP_VARIABLE} is not set to the unit's RuntimeMaxSec in seconds"))?; let mind = HuginnMind::new(HuginnClient::new(address, instance, TIMEOUT)); - let outcome = carry(&mind, &SystemClock, &SocketLender::new(socket), &ClaudeSpawner::new(std::env::vars_os())?, run_cap_s, &mut std::io::stdout())?; + let outcome = carry(&mind, &SystemClock, &SocketLender::new(socket), &ClaudeSpawner::new(std::env::vars_os(), std::env::current_exe()?)?, run_cap_s, &mut std::io::stdout())?; println!("{outcome}"); Ok(outcome.exit_status()) } +/// The hook: the spool named by `--spool`, the hook's input on stdin. +fn hook(args: &[String]) -> Result { + let spool = args.iter().position(|arg| arg == "--spool").and_then(|at| args.get(at + 1)).map(PathBuf::from).ok_or_else(|| anyhow!("inject needs --spool "))?; + let mut input = String::new(); + std::io::stdin().read_to_string(&mut input)?; + let injection = inject(&spool, &input, args.iter().any(|arg| arg == "--pre-compact"))?; + if injection.exit == 2 { + eprintln!("compaction blocked: the actuator asks for the flush"); + } + print!("{}", injection.stdout); + Ok(injection.exit) +} + fn main() { match run() { Ok(status) => std::process::exit(status), diff --git a/crates/mind-body/src/self_run.rs b/crates/mind-body/src/self_run.rs index fe9cfcf..ff082b8 100644 --- a/crates/mind-body/src/self_run.rs +++ b/crates/mind-body/src/self_run.rs @@ -6,13 +6,21 @@ //! stream-json output. It counts the main thread's requests, asks for the flush //! once when the context prefix reaches the ceiling (ruling //! `eureka-body:ruling:self-lifecycle-flush-rehydrate`: a Self run admits what -//! the mind lacks and ends, it never summarises), closes the child's input at -//! the first result, and stops a child that outlives its flush: an interrupt +//! the mind lacks and ends, it never summarises), and stops a child that outlives its flush: an interrupt //! after the grace, or once the requests of the flush's own turn have grown the //! prefix by the headroom beyond the prefix the flush was asked at (ruling //! `eureka-body:ruling:flush-always-gets-a-turn`), then SIGTERM and SIGKILL if //! the child ignores that. Signals go to the child's process group. //! +//! One run is one live session (ruling `eureka-body:ruling:live-mind-sessions`). +//! A result ends a turn, not the session. What her Self is asked is derived from +//! her mind (the `inbox` module): an open follow-up with owner `self-run` is a +//! brief not yet answered. The actuator polls for briefs and tells them to the +//! session, between tool calls through the PostToolBatch hook and between turns +//! as a user line; a result closes every brief told to that turn. The session +//! ends where its cache would be evicted, near the unit's cap, at the context +//! ceiling, or after the flush each of those asks. +//! //! Every timing decision is one pure function, `decide` (the `decide` module, //! ruling `eureka-body:ruling:self-run-decider`). `carry` is the loop that turns //! the operating system's facts into events and executes the actions. @@ -29,7 +37,7 @@ //! `RuntimeMaxSec` and the context-ceiling flush. The grant carries the plan usage //! windows, which go to the child in its start message. //! -//! The actuator admits only closures of Self runs, nothing else. Every exit of +//! The actuator admits run closures and brief closures, nothing else. Every exit of //! `carry` that leaves her run open resolves it Recorded exactly once (the //! child's result in the rationale, or that it gave none in the reason). It reads neither the //! brake nor the environment and stops no unit: the gate and the brake hold own those. @@ -38,6 +46,7 @@ //! foreign owner's JSON: it is parsed into `serde_json::Value` here and nothing //! parsed is stored. +use std::collections::VecDeque; use std::ffi::{OsStr, OsString}; use std::fmt; use std::io::{BufRead, BufReader, Write}; @@ -47,7 +56,7 @@ use std::sync::mpsc::{Receiver, RecvTimeoutError, channel}; use std::time::Duration as StdDuration; use anyhow::{Context, Result, anyhow, bail}; -use chrono::{DateTime, Utc}; +use chrono::{DateTime, Duration as ChronoDuration, Utc}; use cultnet_rs::Selection; use eureka_pipeline::{PipelineDocument, PipelineRef, ResolutionOutcome, RunTurn, Slug}; use huginn_mind::{PipelineAdmissionOutcome, PipelinePageItems}; @@ -61,6 +70,7 @@ use model_connector::PlanQuota; use crate::queue::MindPort; use decide::{Action, Event, State, decide, next_deadline}; +use inbox::{Brief, Hooks}; /// The measured Self ceiling: at this prefix the flush is asked for. pub const CEILING_TOKENS: u64 = 300_000; @@ -74,6 +84,16 @@ pub const ESCALATE_AFTER_S: i64 = 120; /// How long a stream may stay open after the child has exited (a descendant /// holding its stdout) before the run is over without it. pub const EXIT_DRAIN_S: i64 = 2; +/// How long before the unit's cap an idle session is closed for rotation. +pub const ROTATE_MARGIN_S: i64 = 20 * 60; +/// How long before the unit's cap a working session is asked for its flush. +pub const FLUSH_MARGIN_S: i64 = 10 * 60; +/// How often the mind is asked for new briefs. +pub const INBOX_POLL_S: i64 = 30; +/// The prompt cache's lifetimes, and how long before one runs out the session ends. +pub const TTL_1H_S: i64 = 3600; +pub const TTL_5M_S: i64 = 300; +pub const TTL_MARGIN_S: i64 = 60; /// A backstop `claude` enforces; the ceiling is reached first. pub const MAX_TURNS: u32 = 500; /// Names the variable the Self unit sets to its own `RuntimeMaxSec`, in seconds: @@ -101,13 +121,13 @@ pub const AGENT: &str = "mind-self-run"; pub const SESSION_NAMESPACE: Uuid = Uuid::NAMESPACE_OID; /// The longest the loop waits on the child when no deadline is nearer. -const POLL: StdDuration = StdDuration::from_secs(60); +const POLL: StdDuration = StdDuration::from_secs(INBOX_POLL_S as u64); /// What the child is told at the start. The contract with the `eureka` /// skill: it names the skill, the instance and the run id. pub fn start_message(instance: &Slug, run: &PipelineRef, plan_quota: &[PlanQuota], now_ms: u64) -> String { format!( - "Use the eureka skill. You are Self for the instance `{}`; your run is `{}`. Admit as faculty SelfFaculty, agent {AGENT}.\n\n{}", + "Use the eureka skill. You are Self for the instance `{}`; your run is `{}`. Admit as faculty SelfFaculty, agent {AGENT}.\n\nThis session stays open after you answer: briefs from your Persona arrive while you work and between tasks, and each answer you give closes the brief it answers. Work you take on beyond an answer is a follow-up of your own.\n\n{}", instance.0, run.id.0, render_plan(plan_quota, now_ms) @@ -160,7 +180,9 @@ pub fn session_id(run: &PipelineRef) -> Uuid { } /// The argument list of the child, and nothing else: no resume, continue or -/// fork, no model and no settings of its own belong to this actuator. It fixes +/// fork, no model and no settings of its own belong to this actuator, except the +/// hooks the actuator builds on its own argv (`--settings`, never a file in her +/// sandbox): the tell between tool calls and the block on auto-compaction. It fixes /// the permission mode: print mode starts in Manual when nothing on the command /// line sets a mode (the managed `defaultMode` was not applied in print mode, /// 2026-10-31), and `--permission-prompts none` then denies every tool that @@ -168,7 +190,7 @@ pub fn session_id(run: &PipelineRef) -> Uuid { /// user's settings load (the fresh configuration directory /// the seed fills); the working directory's project and local settings, which a /// run could have written, do not. -pub fn claude_args(session: Uuid) -> Vec { +pub fn claude_args(session: Uuid, hooks: &Hooks) -> Vec { [ "-p", "--input-format", @@ -186,6 +208,8 @@ pub fn claude_args(session: Uuid) -> Vec { "none", "--setting-sources", "user", + "--settings", + &hooks.settings(), ] .map(String::from) .to_vec() @@ -204,6 +228,18 @@ pub enum Usage { Unreadable, } +/// Whether a main-thread request wrote its prompt cache for an hour (the split +/// `cache_creation.ephemeral_1h_input_tokens`), else for five minutes. None for +/// any other line. +pub fn writes_hour_cache(line: &str) -> Option { + let Ok(Value::Object(event)) = serde_json::from_str::(line) else { return None }; + if event.get("type").and_then(Value::as_str) != Some("assistant") || !event.get("parent_tool_use_id").is_none_or(Value::is_null) { + return None; + } + let hour = event.get("message")?.get("usage")?.get("cache_creation")?.get("ephemeral_1h_input_tokens").and_then(Value::as_u64); + Some(hour.unwrap_or(0) > 0) +} + /// Reads one stream line. pub fn read_usage(line: &str) -> Usage { let Ok(Value::Object(event)) = serde_json::from_str::(line) else { return Usage::Unreadable }; @@ -264,6 +300,8 @@ impl Stop { } pub trait Spawner { + /// The spool the child's hooks drain and the program they run. + fn hooks(&self) -> Hooks; /// Starts the child with `token` as its credential descriptor. fn spawn(&self, args: &[String], token: &TokenFd) -> Result>; } @@ -357,6 +395,11 @@ impl Heard { } } +/// The label of a run: the name its closure and its briefs' closures are admitted under. +fn session_label(run: &PipelineRef) -> &str { + run.id.0.rsplit(':').next().unwrap_or(&run.id.0) +} + fn unix_ms(at: DateTime) -> u64 { u64::try_from(at.timestamp_millis()).unwrap_or(0) } @@ -426,7 +469,23 @@ fn withdraw_misconfigured(mind: &dyn MindPort, clock: &dyn Clock, run: &Pipeline /// error, and the launch that opens the next run closes the run left open. pub fn carry(mind: &dyn MindPort, clock: &dyn Clock, lender: &dyn Lender, spawner: &dyn Spawner, run_cap_s: u64, out: &mut dyn Write) -> Result { let Some((run, claims)) = her_run(mind)? else { return Ok(SelfRunOutcome::NoRun) }; - let task = task_section(mind, &claims)?; + let hooks = spawner.hooks(); + // The briefs a closed run claimed were answered by that run: closed now, before anything is told. + inbox::reset(&hooks.spool)?; + inbox::close_past(mind, clock.now(), session_label(&run))?; + let mut task = task_section(mind, &claims)?; + let open = inbox::derive(mind, &hooks.spool)?; + let also: Vec<&Brief> = open.iter().filter(|brief| !claims.iter().any(|claim| claim.id.0 == brief.id)).collect(); + if !also.is_empty() { + task.push_str(" + +Also open for you:"); + for brief in also { + task.push_str(&format!(" +- follow-up `{}` (campaign `{}`, repo `{}`): {}", brief.id, brief.campaign, brief.repo, brief.item)); + } + } + inbox::mark_told(&hooks.spool, &open)?; let mut heard = Heard::default(); let ran = lend_and_drive(mind, clock, lender, spawner, run_cap_s, out, (&run, &task), &mut heard); let closed = close_if_open(mind, clock, &run, &ran, &heard); @@ -452,8 +511,9 @@ fn lend_and_drive(mind: &dyn MindPort, clock: &dyn Clock, lender: &dyn Lender, s } Lent::Refused(refused) => return close_refused(mind, clock, run, refused.kind), }; - let child = spawner.spawn(&claude_args(session_id(run)), lease.token())?; - drive(mind, clock, child, (run, task), (lease.plan_quota(), started_ms), heard, out) + let hooks = spawner.hooks(); + let child = spawner.spawn(&claude_args(session_id(run), &hooks), lease.token())?; + drive(mind, clock, child, &hooks, (run, task), (lease.plan_quota(), started_ms, run_cap_s), heard, out) } /// The most of a result's text that reaches the journal. @@ -503,22 +563,65 @@ fn write_result(line: &str, out: &mut dyn Write) -> std::io::Result<()> { writeln!(out, "{}", describe_result(&event, RESULT_TEXT_LIMIT)) } +/// What a brief's closing resolution says when the result had no text. +const NO_ANSWER_TEXT: &str = "The turn ended with no answer text."; + +/// Resolves Recorded every brief told to the session that this result answers. +/// A failed close leaves them told: the next result tries again. +fn close_told(mind: &dyn MindPort, clock: &dyn Clock, hooks: &Hooks, session: &str, heard: &Heard, out: &mut dyn Write) -> Result { + let rationale = heard.concluded.as_ref().map(|(_, text)| text.as_str()).filter(|text| !text.is_empty()).unwrap_or(NO_ANSWER_TEXT); + let closed = inbox::told(&hooks.spool).and_then(|told| { + let answered: Vec<(Brief, String)> = told.iter().map(|brief| (brief.clone(), rationale.to_string())).collect(); + let count = inbox::close(mind, clock.now(), session, &answered, "answered by the turn's result")?; + inbox::forget(&hooks.spool, &told); + Ok(count) + }); + match closed { + Ok(count) => Ok(count), + Err(_) => { + writeln!(out, "briefs could not be closed: the next result tries again")?; + Ok(0) + } + } +} + /// The loop: facts in, one clock read per fact, actions out. -fn drive(mind: &dyn MindPort, clock: &dyn Clock, mut child: Box, (run, task): (&PipelineRef, &str), (plan_quota, now_ms): (&[PlanQuota], u64), heard: &mut Heard, out: &mut dyn Write) -> Result { +#[allow(clippy::too_many_arguments, reason = "the loop's ports and the run's facts, passed on whole")] +fn drive( + mind: &dyn MindPort, + clock: &dyn Clock, + mut child: Box, + hooks: &Hooks, + (run, task): (&PipelineRef, &str), + (plan_quota, now_ms, run_cap_s): (&[PlanQuota], u64, u64), + heard: &mut Heard, + out: &mut dyn Write, +) -> Result { let user_line = |text: &str| json!({"type": "user", "message": {"role": "user", "content": text}}).to_string(); + let plan = render_plan(plan_quota, now_ms); + inbox::write_plan(&hooks.spool, &plan)?; child.write_line(&user_line(&format!("{}{task}", start_message(mind.instance(), run, plan_quota, now_ms)))); - let mut state = State::default(); + let session = session_label(run); let mut now = clock.now(); + let mut state = State::started(now, run_cap_s); + let mut polled = now; + let mut ttl_1h = false; + let mut closed = false; loop { - let wait = next_deadline(&state).map_or(POLL, |at| (at - now).to_std().unwrap_or_default().min(POLL)); + let poll_due = polled + ChronoDuration::seconds(INBOX_POLL_S); + let wake = next_deadline(&state).into_iter().chain((!closed).then_some(poll_due)).min(); + let wait = wake.map_or(POLL, |at| (at - now).to_std().unwrap_or_default().min(POLL)); let fact = match child.next(wait) { Read::Line(line) => { heard.hear(&line); write_result(&line, out)?; + if let Some(hour) = writes_hour_cache(&line) { + ttl_1h = hour; + } match read_usage(&line) { Usage::Request { id, prefix } => Event::Request { id, prefix }, - Usage::Result => Event::Result, + Usage::Result => Event::Result { ttl_1h }, Usage::Other | Usage::Unreadable => Event::Tick, } } @@ -527,20 +630,53 @@ fn drive(mind: &dyn MindPort, clock: &dyn Clock, mut child: Box, (run Read::Exited(code) => Event::ChildExited(code), }; now = clock.now(); - let mut event = Some(fact); - while let Some(heard) = event.take() { + let mut events = VecDeque::from([fact]); + if !closed && now >= poll_due { + polled = now; + match inbox::derive(mind, &hooks.spool).and_then(|briefs| inbox::spool(&hooks.spool, &briefs)).and_then(|()| inbox::pending(&hooks.spool)) { + Ok(pending) => events.push_back(Event::Inbox { pending }), + Err(_) => writeln!(out, "inbox poll failed")?, + } + } + if inbox::take_pre_compact(&hooks.spool) { + events.push_back(Event::PreCompact); + } + while let Some(heard_event) = events.pop_front() { let counted = state.requests; - let (next, action) = decide(state, heard, now); + let (next, action) = decide(state, heard_event, now); state = next; if state.requests != counted { writeln!(out, "request {} prefix {}", state.requests, state.prefix)?; } - match action { + let mut briefs_closed = 0; + match &action { Action::None => {} Action::WriteFlush => child.write_line(&user_line(FLUSH_MESSAGE)), - Action::CloseStdin => child.close_input(), - Action::Signal(how) => event = Some(Event::Signalled { how, delivered: child.stop(how) }), - Action::Finish(outcome) => return Ok(outcome), + Action::WriteTells => match inbox::claim(&hooks.spool) { + Ok(briefs) if !briefs.is_empty() => { + child.write_line(&user_line(&inbox::render(&briefs, &plan))); + events.push_front(Event::Told); + } + Ok(_) => events.push_front(Event::Inbox { pending: false }), + Err(_) => { + writeln!(out, "inbox claim failed")?; + events.push_front(Event::Inbox { pending: false }); + } + }, + Action::CloseBriefs => briefs_closed = close_told(mind, clock, hooks, session, heard, out)?, + Action::CloseStdin => { + child.close_input(); + closed = true; + } + Action::Signal(how) => events.push_front(Event::Signalled { how: *how, delivered: child.stop(*how) }), + Action::Finish(_) => {} + } + if !matches!(action, Action::None | Action::Finish(_)) { + let idle = state.idle_s(now).map_or_else(|| "working".to_string(), |seconds| seconds.to_string()); + writeln!(out, "decision {action:?} prefix {} ttl_s {} idle_s {idle} inbox {} briefs_closed {briefs_closed}", state.prefix, state.ttl_s, state.inbox)?; + } + if let Action::Finish(outcome) = action { + return Ok(outcome); } } } @@ -555,21 +691,31 @@ pub struct ClaudeSpawner { unit_environment: Vec<(OsString, OsString)>, scratch: PathBuf, seed: Option, + /// The program the child's hooks run: this actuator's own binary. + injector: PathBuf, } impl ClaudeSpawner { /// From the unit's environment: it must name [`SCRATCH_VARIABLE`], and may name [`SEED_VARIABLE`]. - pub fn new(unit_environment: impl IntoIterator) -> Result { + /// `injector` is the absolute path of `mind-self-run`, which the hooks run as `inject`. + pub fn new(unit_environment: impl IntoIterator, injector: PathBuf) -> Result { let unit_environment: Vec<(OsString, OsString)> = unit_environment.into_iter().collect(); let named = |variable: &str| unit_environment.iter().find(|(name, _)| name == variable).map(|(_, value)| PathBuf::from(value)); // systemd lists several runtime directories colon-separated; the unit names one. let scratch = named(SCRATCH_VARIABLE).filter(|path| !path.as_os_str().is_empty()).ok_or_else(|| anyhow!("{SCRATCH_VARIABLE} is not set: the unit needs RuntimeDirectory="))?; let seed = named(SEED_VARIABLE).filter(|path| !path.as_os_str().is_empty()); - Ok(Self { unit_environment, scratch, seed }) + Ok(Self { unit_environment, scratch, seed, injector }) } } +/// The directory name of the spool inside the scratch directory. +const SPOOL_DIR_NAME: &str = "inbox"; + impl Spawner for ClaudeSpawner { + fn hooks(&self) -> Hooks { + Hooks { spool: self.scratch.join(SPOOL_DIR_NAME), injector: self.injector.clone() } + } + fn spawn(&self, args: &[String], token: &TokenFd) -> Result> { let config = FreshConfig::create(&self.scratch, self.seed.as_deref())?; spawn_program(Path::new("claude"), args, token, &self.unit_environment, config) @@ -778,6 +924,7 @@ impl Child for ClaudeProcess { } mod decide; +pub mod inbox; #[cfg(test)] mod tests; diff --git a/crates/mind-body/src/self_run/decide.rs b/crates/mind-body/src/self_run/decide.rs index e9f96e8..cdc96ed 100644 --- a/crates/mind-body/src/self_run/decide.rs +++ b/crates/mind-body/src/self_run/decide.rs @@ -1,78 +1,121 @@ -//! The Self-run actuator's lifecycle decision, and nothing else. -//! -//! `decide(state, event, now) -> (state, action)` is the one owner of when the -//! flush is asked for, when the stdin closes, when a signal goes out and when -//! the run is over (ruling `eureka-body:ruling:self-run-decider`). It performs -//! no I/O and reads no clock: the loop in `carry` turns operating-system facts -//! into events, reads the clock once per event, and executes the action. A -//! timing rule is pinned here, by tables and by an enumeration of event orders. -//! -//! The flush's turn begins at the first result after the flush was written (the -//! turn already running ends there; the queued flush is the next one). The -//! headroom counts only requests after that boundary, measured from the prefix -//! the flush was written at, and the grace from the flush write (ruling -//! `eureka-body:ruling:flush-always-gets-a-turn`, refined by the decider ruling). -//! At most one action comes out of an event; a deadline that is already due -//! yields its signal at the next event, and `next_deadline` says it is now. - -use std::collections::HashSet; - -use chrono::{DateTime, Duration, Utc}; - -use super::{CEILING_TOKENS, Cause, ESCALATE_AFTER_S, EXIT_DRAIN_S, FLUSH_GRACE_S, FLUSH_HEADROOM_TOKENS, SelfRunOutcome, Stop}; - -/// What the loop saw, already turned into a fact the decision can use. -#[derive(Clone, Debug, PartialEq, Eq)] -pub(super) enum Event { - /// A request of the main thread: its message id and the context it read. - Request { id: String, prefix: u64 }, - Result, - /// `waitpid` returned: the child's exit code, none when a signal ended it. - ChildExited(Option), - StdoutEof, - /// Time passed, or a line that no rule reads. - Tick, - /// What the operating system said of the signal the last action sent. - Signalled { how: Stop, delivered: bool }, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub(super) enum Action { - None, - WriteFlush, - CloseStdin, - /// To the child's whole process group. - Signal(Stop), - Finish(SelfRunOutcome), -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct Flush { - /// The prefix at which the flush was written: the headroom's base. - prefix: u64, - written: DateTime, - /// A result has come since the write: the flush is the next turn. - turn_begun: bool, - /// A request of the flush's turn reached the headroom. - exhausted: bool, -} - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub(super) struct State { - seen: HashSet, - pub(super) requests: u64, - pub(super) prefix: u64, - result_seen: bool, - flush: Option, - /// The last signal attempted and when. - stopping: Option<(Stop, DateTime)>, - /// The strongest signal delivered. - delivered: Option, - exited: Option<(Option, DateTime)>, - eof: bool, - finished: bool, -} - +//! The Self-run actuator's lifecycle decision, and nothing else. +//! +//! `decide(state, event, now) -> (state, action)` is the one owner of when the +//! flush is asked for, when the stdin closes, when a signal goes out and when +//! the run is over (ruling `eureka-body:ruling:self-run-decider`). It performs +//! no I/O and reads no clock: the loop in `carry` turns operating-system facts +//! into events, reads the clock once per event, and executes the action. A +//! timing rule is pinned here, by tables and by an enumeration of event orders. +//! +//! The flush's turn begins at the first result after the flush is written (the +//! turn already running ends there; the queued flush is the next one). The +//! headroom counts only requests after that boundary, measured from the prefix +//! the flush was written at, and the grace from the flush write (ruling +//! `eureka-body:ruling:flush-always-gets-a-turn`, refined by the decider ruling). +//! At most one action comes out of an event; a deadline that is already due +//! yields its action at the next event, and `next_deadline` says it is now. +//! +//! A result ends a turn, never the session: it marks the session idle and asks +//! for the briefs it answered to be closed. A brief waiting while idle is told. +//! The session ends where its cache would be evicted (ruling +//! `eureka-body:ruling:live-session-floor-cache-evicts`): at the last request's +//! cache TTL less a margin, while no brief waits, the flush is asked, and stdin +//! closes after the flush's own result. Under the run cap it closes at an idle +//! rotation deadline; the PreCompact hook and the cap's margin ask the same +//! flush the ceiling asks. + +use std::collections::HashSet; + +use chrono::{DateTime, Duration, Utc}; + +use super::{ + CEILING_TOKENS, Cause, ESCALATE_AFTER_S, EXIT_DRAIN_S, FLUSH_GRACE_S, FLUSH_HEADROOM_TOKENS, FLUSH_MARGIN_S, ROTATE_MARGIN_S, SelfRunOutcome, Stop, TTL_1H_S, TTL_5M_S, + TTL_MARGIN_S, +}; + +/// What the loop saw, already turned into a fact the decision can use. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum Event { + /// A request of the main thread: its message id and the context it read. + Request { id: String, prefix: u64 }, + /// A turn ended. `ttl_1h`: the last request wrote its cache for an hour, else for five minutes. + Result { ttl_1h: bool }, + /// A poll of the inbox: whether a brief waits in the spool. + Inbox { pending: bool }, + /// A user line carrying briefs was written: a turn has begun. + Told, + /// The PreCompact hook blocked an auto-compaction. + PreCompact, + /// `waitpid` returned: the child's exit code, none when a signal ended it. + ChildExited(Option), + StdoutEof, + /// Time passed, or a line that no rule reads. + Tick, + /// What the operating system said of the signal the last action sent. + Signalled { how: Stop, delivered: bool }, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum Action { + None, + WriteFlush, + /// Claim the spool and write its briefs as one user line (the session is idle). + WriteTells, + /// Resolve the briefs told up to this result. + CloseBriefs, + CloseStdin, + /// To the child's whole process group. + Signal(Stop), + Finish(SelfRunOutcome), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +struct Flush { + /// The prefix at which the flush was written: the headroom's base. + prefix: u64, + written: DateTime, + /// A result has come since the write: the flush is the next turn. + turn_begun: bool, + /// A request of the flush's turn reached the headroom. + exhausted: bool, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(super) struct State { + seen: HashSet, + pub(super) requests: u64, + pub(super) prefix: u64, + /// A turn is running or queued: the start message, a tell and the flush begin one; a result ends it. + busy: bool, + last_result_at: Option>, + /// The cache lifetime the last result left, in seconds. + pub(super) ttl_s: i64, + pub(super) inbox: bool, + /// When the process started and the unit's cap in seconds; none where no cap is known. + cap: Option<(DateTime, i64)>, + stdin_closed: bool, + flush: Option, + /// The last signal attempted and when. + stopping: Option<(Stop, DateTime)>, + /// The strongest signal delivered. + delivered: Option, + exited: Option<(Option, DateTime)>, + eof: bool, + finished: bool, +} + +impl State { + /// A session whose start message was just written: its first turn is running. + pub(super) fn started(at: DateTime, run_cap_s: u64) -> Self { + Self { busy: true, cap: Some((at, i64::try_from(run_cap_s).unwrap_or(i64::MAX))), ..Self::default() } + } + + /// Seconds idle since the last result, none while a turn runs. + pub(super) fn idle_s(&self, now: DateTime) -> Option { + (!self.busy).then(|| self.last_result_at.map_or(0, |at| (now - at).num_seconds())) + } +} + fn outcome(state: &State, cause: Cause, code: Option) -> SelfRunOutcome { SelfRunOutcome::Ended { cause, code, requests: state.requests, prefix: state.prefix } } @@ -86,100 +129,173 @@ fn cause(state: &State) -> Cause { } } -/// The moment the loop must next be heard at, if any. A moment already past -/// means an action is due now. -pub(super) fn next_deadline(state: &State) -> Option> { - if state.finished { - return None; - } - if let Some((_, exited)) = state.exited { - return (!state.eof).then(|| exited + Duration::seconds(EXIT_DRAIN_S)); - } - match (&state.stopping, &state.flush) { - (Some((_, sent)), _) => Some(*sent + Duration::seconds(ESCALATE_AFTER_S)), - (None, Some(flush)) => Some(flush.written + Duration::seconds(FLUSH_GRACE_S)), - (None, None) => None, - } -} - -pub(super) fn decide(mut state: State, event: Event, now: DateTime) -> (State, Action) { - if state.finished { - return (state, Action::None); - } - let mut action = Action::None; - match event { - Event::Request { id, prefix } => { - if state.seen.insert(id) { - state.requests += 1; - state.prefix = prefix; - if let Some(flush) = state.flush.as_mut() - && flush.turn_begun - && flush.prefix.checked_add(FLUSH_HEADROOM_TOKENS).is_some_and(|limit| prefix >= limit) - { - flush.exhausted = true; - } - if state.flush.is_none() && !state.result_seen && state.exited.is_none() && prefix >= CEILING_TOKENS { - state.flush = Some(Flush { prefix, written: now, turn_begun: false, exhausted: false }); - action = Action::WriteFlush; - } - } - } - Event::Result => { - if !state.result_seen { - state.result_seen = true; - if let Some(flush) = state.flush.as_mut() { - flush.turn_begun = true; - } - action = Action::CloseStdin; - } - } - Event::ChildExited(code) => { - state.exited.get_or_insert((code, now)); - } - Event::StdoutEof => state.eof = true, - Event::Tick => {} - Event::Signalled { how, delivered } => { - if delivered { - state.delivered = Some(how); - } - } - } - - // A child that has exited is never written to or signalled: the run is over - // once its stream has ended, or once the stream has had its drain. - if let Some((code, exited)) = state.exited { - if state.eof || now >= exited + Duration::seconds(EXIT_DRAIN_S) { - state.finished = true; - let outcome = outcome(&state, cause(&state), code); - return (state, Action::Finish(outcome)); - } - return (state, Action::None); - } - if action != Action::None { - return (state, action); - } - if let Some((Stop::Kill, sent)) = state.stopping { - // SIGKILL went out and the child still has not been reaped: stop waiting for it. - if now >= sent + Duration::seconds(ESCALATE_AFTER_S) { - state.finished = true; - let cause = if state.delivered == Some(Stop::Kill) { Cause::Signalled(Stop::Kill) } else { Cause::Unstoppable }; - let outcome = outcome(&state, cause, None); - return (state, Action::Finish(outcome)); - } - return (state, Action::None); - } - let how = match (&state.stopping, &state.flush) { - (Some((last, sent)), _) => (now >= *sent + Duration::seconds(ESCALATE_AFTER_S)).then(|| last.next()), - (None, Some(flush)) => (flush.exhausted || now >= flush.written + Duration::seconds(FLUSH_GRACE_S)).then_some(Stop::Interrupt), - (None, None) => None, - }; - match how { - Some(how) => { - state.stopping = Some((how, now)); - (state, Action::Signal(how)) - } - None => (state, Action::None), - } -} +/// The moment the loop must next be heard at, if any. A moment already past +/// means an action is due now. +pub(super) fn next_deadline(state: &State) -> Option> { + if state.finished { + return None; + } + if let Some((_, exited)) = state.exited { + return (!state.eof).then(|| exited + Duration::seconds(EXIT_DRAIN_S)); + } + let signals = match (&state.stopping, &state.flush) { + (Some((_, sent)), _) => return Some(*sent + Duration::seconds(ESCALATE_AFTER_S)), + (None, Some(flush)) => Some(flush.written + Duration::seconds(FLUSH_GRACE_S)), + (None, None) => None, + }; + signals.into_iter().chain(lifecycle_deadline(state)).min() +} + +/// The deadline of the next lifecycle action: the flush margin while a turn +/// runs; while idle, now for a pending tell or a finished flush, else the +/// earlier of the rotation and the cache. +fn lifecycle_deadline(state: &State) -> Option> { + if state.stdin_closed { + return None; + } + let rotation = |margin: i64| state.cap.map(|(started, cap)| started + Duration::seconds(cap - margin)); + if state.busy { + return if state.flush.is_none() { rotation(FLUSH_MARGIN_S) } else { None }; + } + let last = state.last_result_at?; + if state.flush.as_ref().is_some_and(|flush| flush.turn_begun) || state.inbox { + return Some(last); + } + [rotation(ROTATE_MARGIN_S), Some(last + Duration::seconds(state.ttl_s - TTL_MARGIN_S))].into_iter().flatten().min() +} + +/// The flush is asked once, by the ceiling, the PreCompact hook, the cap's +/// margin or the cache's end; never to a child whose input is closed or that has exited. +fn ask_flush(state: &mut State, now: DateTime) -> bool { + if state.flush.is_some() || state.stdin_closed || state.exited.is_some() { + return false; + } + // Written while idle, its turn begins at once; written mid-turn, it is the next one. + state.flush = Some(Flush { prefix: state.prefix, written: now, turn_begun: !state.busy, exhausted: false }); + state.busy = true; + true +} + +/// What the session does next on its own. Mid-turn: the flush at the cap's +/// margin. Idle: close after the flush's own result or at the rotation; tell a +/// waiting brief; ask the flush where the cache would be evicted. +fn lifecycle(state: &mut State, now: DateTime) -> Action { + if state.stdin_closed || state.exited.is_some() { + return Action::None; + } + if state.busy { + let due = state.cap.is_some_and(|(started, cap)| now >= started + Duration::seconds(cap - FLUSH_MARGIN_S)); + return if due && ask_flush(state, now) { Action::WriteFlush } else { Action::None }; + } + let last = state.last_result_at.unwrap_or(now); + let rotate = state.cap.is_some_and(|(started, cap)| now >= started + Duration::seconds(cap - ROTATE_MARGIN_S)); + if rotate || state.flush.as_ref().is_some_and(|flush| flush.turn_begun) { + state.stdin_closed = true; + return Action::CloseStdin; + } + if state.inbox { + return Action::WriteTells; + } + if now >= last + Duration::seconds(state.ttl_s - TTL_MARGIN_S) && ask_flush(state, now) { + return Action::WriteFlush; + } + Action::None +} + +pub(super) fn decide(mut state: State, event: Event, now: DateTime) -> (State, Action) { + if state.finished { + return (state, Action::None); + } + let mut action = Action::None; + match event { + Event::Request { id, prefix } => { + if state.seen.insert(id) { + state.requests += 1; + state.prefix = prefix; + if let Some(flush) = state.flush.as_mut() + && flush.turn_begun + && flush.prefix.checked_add(FLUSH_HEADROOM_TOKENS).is_some_and(|limit| prefix >= limit) + { + flush.exhausted = true; + } + if prefix >= CEILING_TOKENS && ask_flush(&mut state, now) { + action = Action::WriteFlush; + } + } + } + Event::Result { ttl_1h } => { + state.busy = false; + state.last_result_at = Some(now); + state.ttl_s = if ttl_1h { TTL_1H_S } else { TTL_5M_S }; + // The turn already running ends here; a queued flush is the next one. + if state.flush.as_mut().is_some_and(|flush| !std::mem::replace(&mut flush.turn_begun, true)) { + state.busy = true; + } + action = Action::CloseBriefs; + } + Event::Inbox { pending } => state.inbox = pending, + Event::Told => { + state.inbox = false; + state.busy = true; + } + Event::PreCompact => { + if ask_flush(&mut state, now) { + action = Action::WriteFlush; + } + } + Event::ChildExited(code) => { + state.exited.get_or_insert((code, now)); + } + Event::StdoutEof => state.eof = true, + Event::Tick => {} + Event::Signalled { how, delivered } => { + if delivered { + state.delivered = Some(how); + } + } + } + + // A child that has exited is never written to or signalled: the run is over + // once its stream has ended, or once the stream has had its drain. + if let Some((code, exited)) = state.exited { + if state.eof || now >= exited + Duration::seconds(EXIT_DRAIN_S) { + state.finished = true; + let outcome = outcome(&state, cause(&state), code); + return (state, Action::Finish(outcome)); + } + return (state, Action::None); + } + if action != Action::None { + return (state, action); + } + if let Some((Stop::Kill, sent)) = state.stopping { + // SIGKILL went out and the child still has not been reaped: stop waiting for it. + if now >= sent + Duration::seconds(ESCALATE_AFTER_S) { + state.finished = true; + let cause = if state.delivered == Some(Stop::Kill) { Cause::Signalled(Stop::Kill) } else { Cause::Unstoppable }; + let outcome = outcome(&state, cause, None); + return (state, Action::Finish(outcome)); + } + return (state, Action::None); + } + if state.stopping.is_none() { + let next = lifecycle(&mut state, now); + if next != Action::None { + return (state, next); + } + } + let how = match (&state.stopping, &state.flush) { + (Some((last, sent)), _) => (now >= *sent + Duration::seconds(ESCALATE_AFTER_S)).then(|| last.next()), + (None, Some(flush)) => (flush.exhausted || now >= flush.written + Duration::seconds(FLUSH_GRACE_S)).then_some(Stop::Interrupt), + (None, None) => None, + }; + match how { + Some(how) => { + state.stopping = Some((how, now)); + (state, Action::Signal(how)) + } + None => (state, Action::None), + } +} #[cfg(test)] mod tests; diff --git a/crates/mind-body/src/self_run/inbox.rs b/crates/mind-body/src/self_run/inbox.rs new file mode 100644 index 0000000..e161547 --- /dev/null +++ b/crates/mind-body/src/self_run/inbox.rs @@ -0,0 +1,305 @@ +//! Her Self's inbox: the briefs her Persona has given her Self, derived from the +//! mind, spooled for the session, claimed exactly once, and closed by the answer. +//! +//! The mind owns what her Self is asked: an in-force follow-up with owner +//! `self-run` is a brief not yet answered. `derive` lists those that no closed +//! run claims and that the session does not hold yet; `spool` writes each as +//! `/-