From be44185c67a4a8168e3b0603179c113fcb9d49f4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 31 Oct 2026 16:32:35 +0100 Subject: [PATCH] self-session-live: a brief is closed Recorded by the result that answers it; the briefs a closed run claimed are closed at session start Lands the parked closure (eureka-body/self-session-live-closure-parked 13de7f0) on the Recorded outcome of ruling brief-closure-recorded. Co-Authored-By: Claude Sonnet 5.5 --- crates/mind-body/src/self_run.rs | 43 +++++++++-- crates/mind-body/src/self_run/decide.rs | 7 +- crates/mind-body/src/self_run/decide/tests.rs | 9 ++- crates/mind-body/src/self_run/inbox.rs | 66 +++++++++++++++-- crates/mind-body/src/self_run/inbox/tests.rs | 31 ++++++-- crates/mind-body/src/self_run/tests.rs | 73 ++++++++++++++----- 6 files changed, 189 insertions(+), 40 deletions(-) diff --git a/crates/mind-body/src/self_run.rs b/crates/mind-body/src/self_run.rs index 72a891c..67193ca 100644 --- a/crates/mind-body/src/self_run.rs +++ b/crates/mind-body/src/self_run.rs @@ -17,8 +17,9 @@ //! 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. 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. +//! 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 @@ -36,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. @@ -126,7 +127,7 @@ const POLL: StdDuration = StdDuration::from_secs(INBOX_POLL_S as u64); /// 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\nThis session stays open after you answer: briefs from your Persona arrive while you work and between tasks. Work you take on beyond an answer is a follow-up of your own.\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) @@ -394,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) } @@ -464,7 +470,9 @@ fn withdraw_misconfigured(mind: &dyn MindPort, clock: &dyn Clock, run: &Pipeline 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 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(); @@ -555,6 +563,28 @@ 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. #[allow(clippy::too_many_arguments, reason = "the loop's ports and the run's facts, passed on whole")] fn drive( @@ -572,6 +602,7 @@ fn drive( inbox::write_plan(&hooks.spool, &plan)?; child.write_line(&user_line(&format!("{}{task}", start_message(mind.instance(), run, plan_quota, now_ms)))); + let session = session_label(run); let mut now = clock.now(); let mut state = State::started(now, run_cap_s); let mut polled = now; @@ -619,6 +650,7 @@ fn drive( if state.requests != counted { writeln!(out, "request {} prefix {}", state.requests, state.prefix)?; } + let mut briefs_closed = 0; match &action { Action::None => {} Action::WriteFlush => child.write_line(&user_line(FLUSH_MESSAGE)), @@ -633,6 +665,7 @@ fn drive( 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; @@ -642,7 +675,7 @@ fn drive( } if !matches!(action, Action::None | Action::Finish(_)) { let idle = idle_s.map_or_else(|| "working".to_string(), |seconds| seconds.to_string()); - writeln!(out, "decision {action:?} prefix {} ttl_s {ttl_s} idle_s {idle} inbox {inbox}", state.prefix)?; + writeln!(out, "decision {action:?} prefix {} ttl_s {ttl_s} idle_s {idle} inbox {inbox} briefs_closed {briefs_closed}", state.prefix)?; } if let Action::Finish(outcome) = action { return Ok(outcome); diff --git a/crates/mind-body/src/self_run/decide.rs b/crates/mind-body/src/self_run/decide.rs index 5403d3b..cdc96ed 100644 --- a/crates/mind-body/src/self_run/decide.rs +++ b/crates/mind-body/src/self_run/decide.rs @@ -15,8 +15,8 @@ //! 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. A brief -//! waiting while idle is told. +//! 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 @@ -61,6 +61,8 @@ pub(super) enum Action { 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), @@ -229,6 +231,7 @@ pub(super) fn decide(mut state: State, event: Event, now: DateTime) -> (Sta 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 => { diff --git a/crates/mind-body/src/self_run/decide/tests.rs b/crates/mind-body/src/self_run/decide/tests.rs index 615bd01..f8cfbd9 100644 --- a/crates/mind-body/src/self_run/decide/tests.rs +++ b/crates/mind-body/src/self_run/decide/tests.rs @@ -74,7 +74,7 @@ fn the_flush_is_asked_once_at_the_ceiling_and_never_after_a_result_or_an_exit_or #[test] fn a_result_ends_a_turn_and_never_closes_the_input_while_a_brief_waits_or_the_cache_is_warm() { // The result asks for its briefs to be closed and nothing else; the next pass has nothing to do. - assert_eq!(last(vec![(RESULT, 0)]), Action::None); + assert_eq!(last(vec![(RESULT, 0)]), Action::CloseBriefs); assert_eq!(last(vec![(RESULT, 0), (Event::Tick, 0)]), Action::None); assert_eq!(last(vec![(RESULT, 0), (Event::Tick, 3000)]), Action::None); // A waiting brief is told, however long the session has been idle, and the input stays open. @@ -111,7 +111,8 @@ fn the_session_ends_where_the_cache_would_be_evicted_with_a_flush_and_closes_aft let flushed = vec![(RESULT, 0), (Event::Tick, TTL_1H_S - TTL_MARGIN_S)]; assert_eq!(last([flushed.clone(), vec![(Event::Tick, TTL_1H_S)]].concat()), Action::None); let ended_turn = [flushed, vec![(SHORT_RESULT, TTL_1H_S)]].concat(); - assert_eq!(last(ended_turn), Action::CloseStdin); + assert_eq!(last(ended_turn.clone()), Action::CloseBriefs); + assert_eq!(last([ended_turn, vec![(Event::Tick, TTL_1H_S)]].concat()), Action::CloseStdin); } #[test] @@ -320,6 +321,7 @@ fn check_step(oracle: &mut Oracle, event: &Event, now: i64, action: &Action) { assert!(!oracle.finished || *action == Action::None, "an action after the run ended: {action:?} on {event:?}"); let flush_before = oracle.flush; let was_exited = oracle.exited.is_some(); + let was_idle = oracle.idle; match event { Event::Request { id, prefix } => { if !oracle.ids.contains(id) { @@ -367,12 +369,13 @@ fn check_step(oracle: &mut Oracle, event: &Event, now: i64, action: &Action) { Action::WriteTells => { assert!(oracle.idle && oracle.inbox && !oracle.closed && !was_exited, "a tell outside idle with a brief waiting"); } + Action::CloseBriefs => assert!(matches!(event, Event::Result { .. }), "briefs closed on {event:?}"), Action::CloseStdin => { oracle.closes += 1; oracle.closed = true; let flushed = oracle.flush.is_some() && oracle.boundary; let rotation = now >= i64::try_from(CAP_S).unwrap() - ROTATE_MARGIN_S; - assert!(oracle.idle && !was_exited && (flushed || rotation), "closed on {event:?} flushed {flushed} rotation {rotation}"); + assert!(was_idle && !was_exited && (flushed || rotation), "closed on {event:?} idle {was_idle} flushed {flushed} rotation {rotation}"); } Action::Signal(how) => { let (flushed_at, _) = flush_before.expect("a signal before the flush was written"); diff --git a/crates/mind-body/src/self_run/inbox.rs b/crates/mind-body/src/self_run/inbox.rs index 92a4381..e161547 100644 --- a/crates/mind-body/src/self_run/inbox.rs +++ b/crates/mind-body/src/self_run/inbox.rs @@ -1,5 +1,5 @@ //! Her Self's inbox: the briefs her Persona has given her Self, derived from the -//! mind, spooled for the session, and claimed exactly once. +//! 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 @@ -7,18 +7,23 @@ //! `/-