2026-10-09 20:51:07 +02:00
//! The one way her organs open a run and start its unit.
//!
2026-10-10 05:32:19 +02:00
//! `open_and_launch` is the whole path, in this order: (0) the in-force runs of
2026-11-01 04:11:38 +01:00
//! hers of the request's turn are read through `slot`, which tells a live one
//! (its unit active, or admitted within `START_GRACE_S`: it answers Busy) from a
2026-11-01 12:29:55 +01:00
//! dead one (to be closed Recorded); (1) every cut-spec claim must be in the queue; (2) no claim may be held by an open question (the gate reads the hold itself, once, after the breaker reads and just before the admission, and takes no caller's word for it), and none may
2026-11-01 02:09:49 +01:00
//! trip the repetition breaker (a trip raises one question in the run whose end
//! tripped it, which holds that run's claims); (3) the run is admitted, in the one batch that
2026-10-10 05:32:19 +02:00
//! closes the dead holders, so liveness is decided by the in-force record alone
//! and the unit table is only the evidence for a close; (4) the unit is started,
//! only after that batch commits. The run is the grant (target invariant
//! run-is-the-grant), so there is no pressure file, lease or breaker state
//! beside it, and a unit never starts without a committed run. A run opens
2026-10-10 06:54:29 +02:00
//! only against a `Grant`, which `read_effective` alone makes and which names
//! the instance it was read for: a Grant of another instance is refused. A
//! Persona turn's run opens with no claims, so a request that carries any is
//! refused. The primitive
2026-10-12 03:48:49 +02:00
//! owns no file and stops no unit through the `Launcher` port: stopping her
2026-11-01 19:11:21 +01:00
//! units is either brake's hold (one Mind's, or the swarm's for every Mind),
//! through `SystemdLauncher::stop`.
2026-10-09 20:51:07 +02:00
2026-10-30 02:49:35 +01:00
2026-10-09 20:51:07 +02:00
use anyhow ::{ Result , anyhow } ;
2026-10-10 05:32:19 +02:00
use chrono ::{ DateTime , Duration , Utc } ;
use cultnet_rs ::Selection ;
2026-10-09 20:51:07 +02:00
use eureka_pipeline ::{
2026-10-10 05:32:19 +02:00
Date , Label , PipelineDocument , PipelineKind , PipelineQuestion , PipelineRef , PipelineResolution , PipelineRun , QuestionOption , ResolutionOutcome ,
2026-11-08 11:01:39 +01:00
RunOperator , RunSlot , RunTurn , Slug , pipeline_key ,
2026-10-09 20:51:07 +02:00
} ;
2026-11-01 04:11:38 +01:00
use huginn_mind ::{
2026-11-01 12:29:55 +01:00
MindRefusal , PipelineAdmissionOutcome , PipelineDocumentSummary , PipelineFacts ,
2026-11-01 04:11:38 +01:00
} ;
2026-10-09 20:51:07 +02:00
2026-10-10 05:32:19 +02:00
use crate ::control ::Grant ;
2026-11-01 04:11:38 +01:00
use crate ::queue ::{
2026-11-01 12:29:55 +01:00
Breaker , MindPort , any_of , breaker , citing , claims_of , headers , in_force , of_kind , on_hold , queue ,
2026-11-01 04:11:38 +01:00
} ;
use eureka_state ::hold ::RETIRE ;
2026-10-10 05:32:19 +02:00
/// How long a run just admitted may have no active unit before its slot is
/// treated as dead: `systemctl start --no-block` returns before the unit runs.
pub const START_GRACE_S : i64 = 120 ;
2026-10-09 20:51:07 +02:00
/// Starts the unit that carries a run, and says whether it is still running.
/// Nothing here stops a unit.
pub trait Launcher {
2026-10-10 06:54:29 +02:00
/// Units are named by instance and turn alone (ruling mind-unit-names), so
/// liveness is keyed the same way.
fn start ( & self , instance : & Slug , turn : RunTurn ) -> Result < ( ) > ;
fn alive ( & self , instance : & Slug , turn : RunTurn ) -> bool ;
2026-10-09 20:51:07 +02:00
}
/// The time the primitive reads: run labels, dates and receipts.
pub trait Clock {
fn now ( & self ) -> DateTime < Utc > ;
}
pub struct SystemClock ;
impl Clock for SystemClock {
fn now ( & self ) -> DateTime < Utc > {
Utc ::now ( )
}
}
2026-10-12 03:48:49 +02:00
/// What `systemctl` answered: whether it succeeded, its exit code (none when a
/// signal ended it), and what it printed.
2026-10-09 20:51:07 +02:00
pub struct SystemctlReply {
pub success : bool ,
2026-10-12 03:48:49 +02:00
pub code : Option < i32 > ,
2026-10-09 20:51:07 +02:00
pub stdout : String ,
}
/// The one door to `systemctl`, so tests never reach a real unit.
pub trait Systemctl {
fn run ( & self , args : & [ & str ] ) -> Result < SystemctlReply > ;
}
struct RealSystemctl ;
impl Systemctl for RealSystemctl {
fn run ( & self , args : & [ & str ] ) -> Result < SystemctlReply > {
let output = std ::process ::Command ::new ( " systemctl " ) . args ( args ) . output ( ) . map_err ( | _ | anyhow! ( " systemctl could not be run " ) ) ? ;
2026-10-12 03:48:49 +02:00
Ok ( SystemctlReply {
success : output . status . success ( ) ,
code : output . status . code ( ) ,
stdout : String ::from_utf8_lossy ( & output . stdout ) . into_owned ( ) ,
} )
2026-10-09 20:51:07 +02:00
}
}
/// Her units under systemd, templated by instance:
2026-10-10 05:38:22 +02:00
/// `mind-persona@<instance>.service` and
/// `mind-self@<instance>.service` (ruling mind-unit-names: one live run per
/// instance and turn, so the run label names no unit). The launcher holds no instance: the
2026-10-10 05:32:19 +02:00
/// caller names it on every call, and `open_and_launch` always names the
/// mind's own.
2026-10-09 20:51:07 +02:00
pub struct SystemdLauncher {
systemctl : Box < dyn Systemctl > ,
}
impl SystemdLauncher {
2026-10-10 05:32:19 +02:00
pub fn new ( ) -> Self {
Self ::over ( Box ::new ( RealSystemctl ) )
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:32:19 +02:00
pub ( crate ) fn over ( systemctl : Box < dyn Systemctl > ) -> Self {
Self { systemctl }
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:32:19 +02:00
}
2026-10-09 20:51:07 +02:00
2026-10-10 05:32:19 +02:00
impl Default for SystemdLauncher {
fn default ( ) -> Self {
Self ::new ( )
2026-10-09 20:51:07 +02:00
}
}
2026-11-08 11:01:39 +01:00
/// The unit template of one slot, named only through `unit()`, which start,
2026-10-12 06:43:48 +02:00
/// alive and stop all call.
2026-10-12 03:48:49 +02:00
fn template ( turn : RunTurn ) -> & 'static str {
2026-11-08 11:01:39 +01:00
match turn . slot ( ) {
RunSlot ::Persona = > " mind-persona " ,
RunSlot ::SelfRun = > " mind-self " ,
2026-10-12 03:48:49 +02:00
}
}
2026-11-11 11:37:34 +00:00
/// The stream's unit template: not a run slot, so only a brake hold names it.
const STREAM_TEMPLATE : & str = " mind-stream " ;
/// A template's unit for one instance, the instance held to the leaf's grammar
/// first so it cannot escape the name. The error names nothing of the value.
fn named ( template : & str , instance : & Slug ) -> Result < String > {
2026-10-10 05:32:19 +02:00
instance . validate_slug ( ) . map_err ( | _ | anyhow! ( " the instance is not a unit name " ) ) ? ;
2026-11-11 11:37:34 +00:00
Ok ( format! ( " {template} @ {} .service " , instance . 0 ) )
}
/// The unit of one turn: the template by turn, then the instance. Start and
/// alive both name the unit here.
pub ( crate ) fn unit ( instance : & Slug , turn : RunTurn ) -> Result < String > {
named ( template ( turn ) , instance )
2026-10-12 03:48:49 +02:00
}
2026-11-11 11:37:34 +00:00
/// What one stop reaches: the three units of one Mind, or every Mind's.
2026-11-01 19:11:21 +01:00
#[ derive(Clone, Copy, Debug, PartialEq, Eq) ]
pub enum StopScope < ' a > {
Mind ( & ' a Slug ) ,
Swarm ,
}
/// The arguments of the one synchronous stop. A Mind's: the exact unit `unit()`
/// names for each turn of this instance, the same names start and alive use,
2026-11-11 11:37:34 +00:00
/// and its stream's, with no glob (a prefix glob also matches another instance, `eureka*` matches
2026-11-01 19:11:21 +01:00
/// `eureka-two`). The swarm's: each template's glob over the loaded units,
/// which is exactly every running Mind and exits 0 when none is loaded. No
2026-10-12 06:43:48 +02:00
/// `--no-block`: `systemctl stop` returns when the units are down. The
2026-11-11 11:58:12 +00:00
/// instance is validated by `named()`.
2026-11-01 19:11:21 +01:00
pub ( crate ) fn stop_args ( scope : StopScope < '_ > ) -> Result < Vec < String > > {
2026-10-12 03:48:49 +02:00
let mut args = vec! [ " stop " . to_string ( ) , " -- " . to_string ( ) ] ;
2026-11-11 11:37:34 +00:00
args . extend ( held_templates ( ) . into_iter ( ) . map ( | template | match scope {
StopScope ::Mind ( instance ) = > named ( template , instance ) ,
StopScope ::Swarm = > Ok ( format! ( " {template} @*.service " ) ) ,
} ) . collect ::< Result < Vec < _ > > > ( ) ? ) ;
2026-10-12 06:43:48 +02:00
Ok ( args )
2026-10-12 03:48:49 +02:00
}
2026-11-11 11:37:34 +00:00
/// The templates a hold stops: both run slots and the stream.
fn held_templates ( ) -> [ & 'static str ; 3 ] {
[ template ( RunTurn ::SelfRun ) , template ( RunTurn ::PersonaTurn ) , STREAM_TEMPLATE ]
}
2026-10-12 03:48:49 +02:00
impl SystemdLauncher {
2026-11-01 19:11:21 +01:00
/// Stops the units of one Mind, or of every Mind, and waits for them. An
/// inherent method, not on `Launcher`: only a brake hold calls it, through
/// `mind-control`. A unit that is not installed is systemctl's exit 5, and
/// the others still stop; when a Mind's stop fails the error names each
/// unit's state, because systemd may have stopped one and not the other,
/// and a failed swarm stop names systemctl's exit status only.
pub fn stop ( & self , scope : StopScope < '_ > ) -> Result < ( ) > {
let args = stop_args ( scope ) ? ;
2026-10-12 03:48:49 +02:00
let args : Vec < & str > = args . iter ( ) . map ( String ::as_str ) . collect ( ) ;
let reply = self . systemctl . run ( & args ) ? ;
if reply . success {
return Ok ( ( ) ) ;
}
2026-11-01 19:11:21 +01:00
let states = match scope {
StopScope ::Mind ( instance ) = > {
2026-11-11 11:37:34 +00:00
let states = held_templates ( )
2026-11-01 19:11:21 +01:00
. into_iter ( )
2026-11-11 11:37:34 +00:00
. map ( | template | {
let unit = named ( template , instance ) ? ;
2026-11-01 19:11:21 +01:00
let state = self . unit_state ( & unit ) ;
Ok ( format! ( " {unit} is {state} " ) )
} )
. collect ::< Result < Vec < _ > > > ( ) ?
. join ( " , " ) ;
format! ( " ; {states} " )
}
StopScope ::Swarm = > String ::new ( ) ,
} ;
2026-10-12 03:48:49 +02:00
match reply . code {
2026-11-01 19:11:21 +01:00
Some ( code ) = > Err ( anyhow! ( " systemctl stop failed with exit status {code}{states} " ) ) ,
None = > Err ( anyhow! ( " systemctl stop was ended by a signal{states} " ) ) ,
2026-10-22 17:37:08 +02:00
}
}
/// One unit's state after a failed stop: systemd's own word when it is one
/// of the three a failed stop can leave, else `unknown`. Nothing else the query
/// prints is carried.
fn unit_state ( & self , unit : & str ) -> & 'static str {
let Ok ( reply ) = self . systemctl . run ( & [ " is-active " , unit ] ) else {
return " unknown " ;
} ;
match reply . stdout . trim ( ) {
" active " = > " active " ,
" inactive " = > " inactive " ,
" failed " = > " failed " ,
_ = > " unknown " ,
2026-10-12 03:48:49 +02:00
}
}
2026-10-10 05:32:19 +02:00
}
2026-10-09 20:51:07 +02:00
impl Launcher for SystemdLauncher {
2026-10-10 06:54:29 +02:00
fn start ( & self , instance : & Slug , turn : RunTurn ) -> Result < ( ) > {
2026-10-10 05:38:22 +02:00
let reply = self . systemctl . run ( & [ " start " , " --no-block " , & unit ( instance , turn ) ? ] ) ? ;
2026-10-09 20:51:07 +02:00
if reply . success { Ok ( ( ) ) } else { Err ( anyhow! ( " systemctl did not start the unit " ) ) }
}
2026-10-10 06:54:29 +02:00
fn alive ( & self , instance : & Slug , turn : RunTurn ) -> bool {
2026-10-10 05:38:22 +02:00
let Ok ( unit ) = unit ( instance , turn ) else {
2026-10-10 05:32:19 +02:00
return false ;
} ;
self . systemctl . run ( & [ " is-active " , & unit ] ) . is_ok_and ( | reply | matches! ( reply . stdout . trim ( ) , " active " | " activating " | " reloading " ) )
2026-10-09 20:51:07 +02:00
}
}
/// A run's label from the instant it opens: UTC, to the millisecond. Two
/// launches in one millisecond collide on the run's key, admission answers
/// Conflict, and nothing launches.
pub fn run_label ( now : DateTime < Utc > ) -> Label {
Label ( now . format ( " mind-%Y%m%dT%H%M%S%3fZ " ) . to_string ( ) )
}
pub struct Ports < ' a > {
pub mind : & ' a dyn MindPort ,
pub launcher : & ' a dyn Launcher ,
pub clock : & ' a dyn Clock ,
/// The `host` the run records.
pub host : & ' a str ,
}
pub struct LaunchRequest {
pub turn : RunTurn ,
pub claims : Vec < PipelineRef > ,
/// The admitting agent's name on the receipt.
pub agent : String ,
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
/// The reading of a released instance brake and a released swarm brake.
/// Taken by value, so one reading opens at most one run.
2026-10-10 05:32:19 +02:00
pub grant : Grant ,
2026-10-09 20:51:07 +02:00
}
/// Why admission did not open the run: its own refusal, or one of the two
/// answers that mean the run's key is taken.
#[ derive(Clone, Debug, PartialEq, Eq) ]
pub enum Declined {
Admission ( MindRefusal ) ,
Conflict ,
AlreadyAdmitted ,
/// The run committed, its unit did not start, and the run was withdrawn.
StartFailed ,
2026-10-10 06:54:29 +02:00
/// The Grant was read for another instance than the mind's own.
GrantForOtherInstance ,
/// A Persona turn's run opens with no claims (ruling wake-target).
PersonaRunTakesNoClaims ,
2026-10-09 20:51:07 +02:00
}
#[ derive(Clone, Debug, PartialEq, Eq) ]
pub enum LaunchOutcome {
2026-10-10 05:32:19 +02:00
Launched {
run : PipelineRef ,
} ,
NotQueued {
item : PipelineRef ,
} ,
2026-11-01 02:09:49 +01:00
/// The trip belongs to `run`, the run whose end tripped it; `question` is raised in it.
2026-10-10 05:32:19 +02:00
Tripped {
2026-11-01 02:09:49 +01:00
run : PipelineRef ,
2026-10-10 05:32:19 +02:00
question : PipelineRef ,
} ,
/// The live holder of the request's turn, from step 0 or from admission.
Busy {
run : PipelineRef ,
} ,
2026-10-09 20:51:07 +02:00
Refused ( Declined ) ,
}
fn date ( now : DateTime < Utc > ) -> Date {
Date ( now . format ( " %Y-%m-%d " ) . to_string ( ) )
}
/// The root of a full pipeline id: the campaign slug of a campaign's document.
fn root_of ( item : & PipelineRef ) -> & str {
item . id . 0. split ( ':' ) . next ( ) . unwrap_or_default ( )
}
2026-10-10 05:32:19 +02:00
fn key_of ( document : & PipelineDocument ) -> Result < String > {
pipeline_key ( document ) . map_err ( | _ | anyhow! ( " the document could not be keyed " ) )
}
/// Whether a run admitted at `admitted_at` has had `START_GRACE_S` to start
/// its unit. A stamp that does not parse has not.
fn past_grace ( admitted_at : & str , now : DateTime < Utc > ) -> bool {
DateTime ::parse_from_rfc3339 ( admitted_at ) . is_ok_and ( | at | now . signed_duration_since ( at ) > = Duration ::seconds ( START_GRACE_S ) )
}
/// The resolution that closes a dead run of hers: Recorded, not Withdrawn, so
2026-10-30 00:15:14 +01:00
/// a unit that keeps dying counts as an empty run and trips the breaker.
2026-10-10 05:32:19 +02:00
fn close_dead ( mind : & dyn MindPort , run : & PipelineRef , now : DateTime < Utc > ) -> Result < PipelineDocument > {
2026-10-30 00:15:14 +01:00
closure ( mind , run , now , ResolutionOutcome ::Recorded { reason : " unit not active " . into ( ) } , " The run's unit was not active when its turn's slot was next wanted. " )
}
/// The closing resolution of `run` with `outcome`. Its sequence is the run's
/// next, counting a closure that was itself withdrawn. The one builder of a
/// closure for a run that ended without its own flush: a dead unit
/// (`close_dead`, Recorded: it counts as an empty run) and a Self run whose
/// credential lease was refused (`self_run`, Recorded for a refusal, Withdrawn
/// for a misconfigured length: no run happened and none is counted).
pub ( crate ) fn closure ( mind : & dyn MindPort , run : & PipelineRef , now : DateTime < Utc > , outcome : ResolutionOutcome , rationale : & str ) -> Result < PipelineDocument > {
2026-10-10 05:32:19 +02:00
let closures = Selection { cites : Some ( citing ( run , Some ( " subject " ) ) ) , .. of_kind ( PipelineKind ::Resolution ) } ;
let latest = headers ( mind , & closures ) ?
. iter ( )
. filter_map ( | header | match header . facts {
PipelineFacts ::Resolution { sequence , .. } = > Some ( sequence ) ,
_ = > None ,
} )
. max ( )
. unwrap_or ( 0 ) ;
Ok ( PipelineDocument ::Resolution ( PipelineResolution {
subject : run . clone ( ) ,
sequence : latest + 1 ,
2026-10-30 00:15:14 +01:00
outcome ,
rationale : rationale . into ( ) ,
2026-10-10 05:32:19 +02:00
resolved_on : date ( now ) ,
} ) )
}
2026-10-10 05:46:16 +02:00
/// The in-force runs of hers (operated by the mind) of `turn` on `instance`,
/// read from typed facts: the holders of that turn's slot. The one reader of
2026-10-22 23:26:31 +02:00
/// the slot, for step 0 and for checking a holder admission names. `self_run`
/// reads her open Self run through it too.
pub ( crate ) fn holders ( mind : & dyn MindPort , instance : & Slug , turn : RunTurn ) -> Result < Vec < PipelineDocumentSummary > > {
2026-10-10 05:46:16 +02:00
let runs = headers ( mind , & in_force ( any_of ( of_kind ( PipelineKind ::Run ) , " root " , & [ & instance . 0 ] ) ) ) ? ;
Ok ( runs
. into_iter ( )
2026-11-08 11:01:39 +01:00
. filter ( | run | matches! ( run . facts , PipelineFacts ::Run { turn : held , operated_by : RunOperator ::Mind , .. } if held . slot ( ) = = turn . slot ( ) ) )
2026-10-10 05:46:16 +02:00
. collect ( ) )
}
2026-11-01 04:11:38 +01:00
/// The holders of a turn's slot, told apart: the live one, if any (its unit
/// active, or admitted within `START_GRACE_S`), and the dead, which the next
/// launch closes. The one reader of whether a holder lives: step 0 and the
/// heartbeat's claim of briefs both ask here.
pub ( crate ) struct Slot {
pub live : Option < PipelineRef > ,
pub dead : Vec < PipelineRef > ,
}
pub ( crate ) fn slot (
ports : & Ports ,
instance : & Slug ,
turn : RunTurn ,
now : DateTime < Utc > ,
) -> Result < Slot > {
let mut found = Slot {
live : None ,
dead : Vec ::new ( ) ,
} ;
for holder in holders ( ports . mind , instance , turn ) ? {
if ports . launcher . alive ( instance , turn ) | | ! past_grace ( & holder . admission . admitted_at , now ) {
found . live = Some ( holder . id ) ;
return Ok ( found ) ;
}
found . dead . push ( holder . id ) ;
}
Ok ( found )
}
2026-10-09 20:51:07 +02:00
pub fn open_and_launch ( ports : & Ports , request : LaunchRequest ) -> Result < LaunchOutcome > {
2026-11-01 04:11:38 +01:00
let LaunchRequest {
turn ,
claims ,
agent ,
grant ,
} = request ;
2026-10-09 20:51:07 +02:00
let instance = ports . mind . instance ( ) . clone ( ) ;
2026-10-10 06:54:29 +02:00
if grant . instance ( ) ! = & instance {
return Ok ( LaunchOutcome ::Refused ( Declined ::GrantForOtherInstance ) ) ;
}
2026-11-08 11:01:39 +01:00
if turn ! = RunTurn ::SelfRun & & ! claims . is_empty ( ) {
2026-10-10 06:54:29 +02:00
return Ok ( LaunchOutcome ::Refused ( Declined ::PersonaRunTakesNoClaims ) ) ;
}
2026-10-09 20:51:07 +02:00
let now = ports . clock . now ( ) ;
let label = run_label ( now ) ;
2026-10-10 05:32:19 +02:00
// Step 0: a live holder of this turn's slot answers Busy; a dead one is closed
// in the batch that admits whatever comes next.
let mut batch : Vec < PipelineDocument > = Vec ::new ( ) ;
let mut ending : Vec < PipelineRef > = Vec ::new ( ) ;
2026-11-01 04:11:38 +01:00
let found = slot ( ports , & instance , turn , now ) ? ;
if let Some ( run ) = found . live {
return Ok ( LaunchOutcome ::Busy { run } ) ;
}
for dead in found . dead {
batch . push ( close_dead ( ports . mind , & dead , now ) ? ) ;
ending . push ( dead ) ;
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:32:19 +02:00
if turn = = RunTurn ::SelfRun {
2026-11-01 12:29:55 +01:00
// Every read of the breaker comes first, so the hold snapshot below is the
// last read of the mind before the admission; a refusal by a hold still
// outranks a trip.
let mut tripped : Option < ( & PipelineRef , PipelineRef ) > = None ;
2026-10-10 05:32:19 +02:00
for item in & claims {
2026-11-01 12:29:55 +01:00
if let Breaker ::Trip { run } = breaker ( ports . mind , item , & ending ) ? {
tripped = Some ( ( item , run ) ) ;
break ;
2026-10-10 05:32:19 +02:00
}
2026-10-09 20:51:07 +02:00
}
2026-11-01 12:29:55 +01:00
let held = on_hold ( ports . mind ) ? ;
2026-11-01 04:11:38 +01:00
let open = queue ( ports . mind , & instance , & ending , & held ) ? ;
if let Some ( item ) = claims . iter ( ) . find ( | claim | {
held . contains ( claim ) | | ( claim . kind = = PipelineKind ::CutSpec & & ! open . contains ( claim ) )
} ) {
2026-10-10 05:32:19 +02:00
return Ok ( LaunchOutcome ::NotQueued { item : item . clone ( ) } ) ;
}
2026-11-01 12:29:55 +01:00
if let Some ( ( item , run ) ) = tripped {
let count = claims_of ( ports . mind , & run ) ? . len ( ) ;
let question =
PipelineDocument ::Question ( breaker_question ( item , & run , count , & label , now ) ) ;
let id = PipelineRef {
kind : PipelineKind ::Question ,
id : key_of ( & question ) ? . as_str ( ) . into ( ) ,
} ;
batch . push ( question ) ;
return Ok ( match committed ( ports . mind . admit ( & agent , & label . 0 , batch ) ? ) {
Ok ( ( ) ) = > LaunchOutcome ::Tripped { run , question : id } ,
Err ( declined ) = > LaunchOutcome ::Refused ( declined ) ,
} ) ;
2026-10-09 20:51:07 +02:00
}
}
let mut campaigns : Vec < Slug > = Vec ::new ( ) ;
for claim in & claims {
let root = Slug ( root_of ( claim ) . to_string ( ) ) ;
if ! campaigns . contains ( & root ) {
campaigns . push ( root ) ;
}
}
2026-10-10 05:32:19 +02:00
let run = PipelineDocument ::Run ( PipelineRun {
2026-10-09 20:51:07 +02:00
instance : instance . clone ( ) ,
label : label . clone ( ) ,
operated_by : RunOperator ::Mind ,
turn ,
host : ports . host . into ( ) ,
started_on : date ( now ) ,
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
// The field is dead record: nothing reads it, and the schema revision that drops it is a follow-up.
budget_usd : " 0 " . into ( ) ,
2026-10-09 20:51:07 +02:00
claims ,
campaigns ,
2026-10-10 05:32:19 +02:00
} ) ;
let opened = PipelineRef { kind : PipelineKind ::Run , id : key_of ( & run ) ? . as_str ( ) . into ( ) } ;
batch . push ( run ) ;
match ports . mind . admit ( & agent , & label . 0 , batch ) ? {
2026-10-10 05:46:16 +02:00
// The holder is a claim until the mind shows it: a run of this instance's own, of this turn, in force.
PipelineAdmissionOutcome ::Refused ( MindRefusal ::AlreadyLive { run : named } ) = > {
let held = named . to_ref ( ) ;
return Ok ( if holders ( ports . mind , & instance , turn ) ? . iter ( ) . any ( | holder | holder . id = = held ) {
LaunchOutcome ::Busy { run : held }
} else {
LaunchOutcome ::Refused ( Declined ::Admission ( MindRefusal ::AlreadyLive { run : named } ) )
} ) ;
2026-10-10 05:32:19 +02:00
}
answer = > {
if let Err ( declined ) = committed ( answer ) {
return Ok ( LaunchOutcome ::Refused ( declined ) ) ;
}
}
2026-10-09 20:51:07 +02:00
}
2026-10-10 06:54:29 +02:00
if ports . launcher . start ( & instance , turn ) . is_err ( ) {
2026-10-09 20:51:07 +02:00
let withdrawal = PipelineResolution {
2026-10-10 05:32:19 +02:00
subject : opened ,
2026-10-09 20:51:07 +02:00
sequence : 1 ,
outcome : ResolutionOutcome ::Withdrawn { reason : " the unit failed to start " . into ( ) } ,
rationale : " The run committed and its unit did not start, so it holds no work. " . into ( ) ,
resolved_on : date ( now ) ,
} ;
return match committed ( ports . mind . admit ( & agent , & label . 0 , vec! [ PipelineDocument ::Resolution ( withdrawal ) ] ) ? ) {
Ok ( ( ) ) = > Ok ( LaunchOutcome ::Refused ( Declined ::StartFailed ) ) ,
Err ( _ ) = > Err ( anyhow! ( " the run could not be withdrawn after its unit failed to start " ) ) ,
} ;
}
2026-10-10 05:32:19 +02:00
Ok ( LaunchOutcome ::Launched { run : opened } )
2026-10-09 20:51:07 +02:00
}
/// Whether admission landed the batch; if not, why.
fn committed ( outcome : PipelineAdmissionOutcome ) -> Result < ( ) , Declined > {
match outcome {
PipelineAdmissionOutcome ::Committed { .. } = > Ok ( ( ) ) ,
PipelineAdmissionOutcome ::Refused ( refusal ) = > Err ( Declined ::Admission ( refusal ) ) ,
PipelineAdmissionOutcome ::Conflict { .. } = > Err ( Declined ::Conflict ) ,
PipelineAdmissionOutcome ::AlreadyAdmitted { .. } = > Err ( Declined ::AlreadyAdmitted ) ,
}
}
2026-10-10 05:32:19 +02:00
/// `breaker-<run label>`: one question per trip, since each trip is a launch
/// with a label of its own.
fn breaker_label ( label : & Label ) -> Label {
Label ( format! ( " breaker- {} " , label . 0 ) )
2026-10-09 20:51:07 +02:00
}
2026-11-01 02:09:49 +01:00
/// The question a trip raises, in the campaign of the claim that tripped: one
/// per trip, raised in `run` and holding the `held` claims it claimed.
fn breaker_question ( claim : & PipelineRef , run : & PipelineRef , held : usize , label : & Label , now : DateTime < Utc > ) -> PipelineQuestion {
2026-10-09 20:51:07 +02:00
PipelineQuestion {
2026-11-01 02:09:49 +01:00
campaign : Slug ( root_of ( claim ) . to_string ( ) ) ,
2026-10-10 05:32:19 +02:00
label : breaker_label ( label ) ,
2026-11-01 02:09:49 +01:00
title : " Repeated runs on the same work admitted nothing " . into ( ) ,
question : format ! (
2026-11-01 12:29:55 +01:00
" Run {} and the runs before it left nothing that cites the work it held (claims held: {held}), so none of it launches again. Answering with reopen, or withdrawing this, reopens every claim of that run with a fresh count. To stop working them, resolve the claims, then answer with retire. " ,
2026-11-01 02:09:49 +01:00
run . id . 0
2026-11-01 02:27:11 +01:00
)
2026-11-01 02:27:59 +01:00
. as_str ( )
2026-11-01 02:27:11 +01:00
. into ( ) ,
2026-10-09 20:51:07 +02:00
options : vec ! [
2026-11-01 02:09:49 +01:00
QuestionOption { label : " reopen " . into ( ) , text : " Answer to reopen every claim of the run with a fresh count. " . into ( ) } ,
2026-11-01 12:29:55 +01:00
QuestionOption { label : RETIRE . into ( ) , text : " Resolve the claims so they are no longer worked, and answer with this option. " . into ( ) } ,
2026-10-09 20:51:07 +02:00
] ,
recommended : " reopen " . into ( ) ,
depends : vec ! [ ] ,
2026-11-01 02:09:49 +01:00
raised_in : Some ( run . clone ( ) ) ,
2026-10-09 20:51:07 +02:00
asked_on : date ( now ) ,
}
}
#[ cfg(test) ]
mod tests {
use std ::cell ::{ Cell , RefCell } ;
use eureka_pipeline ::PipelineKind as K ;
2026-10-10 22:59:21 +02:00
use huginn_mind ::{ PipelineAdmissionOutcome as Outcome , PipelineSelectionPage , PipelineStatus } ;
2026-10-09 20:51:07 +02:00
use super ::* ;
use crate ::testkit ::* ;
struct Rig {
mind : TestMind ,
launcher : Recording ,
clock : Fixed ,
}
impl Rig {
fn new ( ) -> Self {
Self { mind : TestMind ::seeded ( ) , launcher : Recording ::default ( ) , clock : Fixed ( Cell ::new ( at ( 0 ) ) ) }
}
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
fn launch ( & self , turn : RunTurn , claims : & [ PipelineRef ] ) -> LaunchOutcome {
self . launch_through ( & self . mind , turn , claims )
2026-10-09 20:51:07 +02:00
}
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
fn launch_through ( & self , mind : & dyn MindPort , turn : RunTurn , claims : & [ PipelineRef ] ) -> LaunchOutcome {
self . try_launch_through ( mind , turn , claims ) . unwrap ( )
2026-10-10 05:32:19 +02:00
}
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
fn try_launch_through ( & self , mind : & dyn MindPort , turn : RunTurn , claims : & [ PipelineRef ] ) -> Result < LaunchOutcome > {
2026-10-09 20:51:07 +02:00
let ports = Ports { mind , launcher : & self . launcher , clock : & self . clock , host : " yggdrasil-host " } ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let grant = Grant ::for_test ( Slug ( INSTANCE . into ( ) ) ) ;
2026-11-01 04:11:38 +01:00
open_and_launch (
& ports ,
LaunchRequest {
turn ,
claims : claims . to_vec ( ) ,
agent : " agent-x " . into ( ) ,
grant ,
} ,
)
2026-10-09 20:51:07 +02:00
}
fn at ( & self , milliseconds : i64 ) {
self . clock . 0. set ( at ( milliseconds ) ) ;
}
2026-10-10 05:32:19 +02:00
/// The clock `seconds` after the run with this id was admitted.
fn age ( & self , run : & str , seconds : i64 ) {
let admitted = self . mind . view ( K ::Run , run ) . unwrap ( ) . admission . admitted_at ;
let admitted = DateTime ::parse_from_rfc3339 ( & admitted ) . unwrap ( ) . with_timezone ( & Utc ) ;
self . clock . 0. set ( admitted + Duration ::seconds ( seconds ) ) ;
}
2026-10-09 20:51:07 +02:00
fn started ( & self ) -> usize {
self . launcher . started . borrow ( ) . len ( )
}
fn runs ( & self ) -> usize {
self . mind . ids ( K ::Run ) . len ( )
}
2026-10-10 05:32:19 +02:00
/// A run of hers in force, with a unit that is running.
fn seed ( & self , label : & str , turn : RunTurn , claims : & [ PipelineRef ] ) {
self . mind . committed ( vec! [ run ( label , turn , RunOperator ::Mind , claims ) ] ) ;
2026-10-10 06:54:29 +02:00
self . launcher . revive ( turn ) ;
2026-10-10 05:32:19 +02:00
}
fn in_force ( & self , run : & str ) -> bool {
self . mind . view ( K ::Run , & run_ref ( run ) . id . 0 ) . unwrap ( ) . status = = PipelineStatus ::InForce
}
2026-10-09 20:51:07 +02:00
}
fn the_run ( view : & huginn_mind ::PipelineDocumentView ) -> & PipelineRun {
let PipelineDocument ::Run ( run ) = & view . document else { panic! ( " not a run " ) } ;
run
}
2026-10-10 05:32:19 +02:00
fn the_closure ( rig : & Rig , run : & str , sequence : u32 ) -> ( PipelineResolution , String ) {
let view = rig . mind . view ( K ::Resolution , & format! ( " {INSTANCE} :resolution:run. {run} .n {sequence} " ) ) . expect ( " the closure " ) ;
let PipelineDocument ::Resolution ( closure ) = view . document else { panic! ( " not a resolution " ) } ;
( closure , view . admission . receipt_id )
}
2026-10-09 20:51:07 +02:00
#[ test ]
fn a_queued_spec_opens_a_run_that_records_its_launch_and_then_its_unit_starts ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Launched { run } = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) else { panic! ( " not launched " ) } ;
2026-10-09 20:51:07 +02:00
assert_eq! ( run . id . 0 , " yggdrasil:run:mind-20261009T131500000Z " ) ;
let view = rig . mind . view ( K ::Run , & run . id . 0 ) . unwrap ( ) ;
let held = the_run ( & view ) ;
assert_eq! ( ( held . operated_by , held . turn ) , ( RunOperator ::Mind , RunTurn ::SelfRun ) ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( ( held . host . 0. as_str ( ) , held . started_on . 0. as_str ( ) , held . budget_usd . 0. as_str ( ) ) , ( " yggdrasil-host " , " 2026-10-09 " , " 0 " ) ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( held . claims , vec! [ spec_ref ( " x " ) ] ) ;
assert_eq! ( held . campaigns , vec! [ Slug ( CAMPAIGN . into ( ) ) ] ) ;
assert_eq! ( view . admission . provenance . agent . 0 , " agent-x " ) ;
assert_eq! ( view . admission . provenance . session . 0 , " mind-20261009T131500000Z " ) ;
2026-10-10 06:54:29 +02:00
assert_eq! ( * rig . launcher . started . borrow ( ) , vec! [ ( Slug ( INSTANCE . into ( ) ) , RunTurn ::SelfRun ) ] ) ;
2026-10-10 05:32:19 +02:00
}
2026-11-08 11:01:39 +01:00
/// A Sleep run and a Persona session are one slot of hers: one unit, one live holder, in either order.
#[ test ]
fn a_sleep_run_and_a_persona_run_share_one_slot ( ) {
let rig = Rig ::new ( ) ;
let LaunchOutcome ::Launched { run } = rig . launch ( RunTurn ::Sleep , & [ ] ) else { panic! ( " not launched " ) } ;
rig . at ( 1 ) ;
assert_eq! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Busy { run : run . clone ( ) } , " a live Sleep holds the Persona slot " ) ;
assert_eq! ( rig . launch ( RunTurn ::Sleep , & [ ] ) , LaunchOutcome ::Busy { run : run . clone ( ) } ) ;
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Launched { .. } ) , " a Self run is the other slot " ) ;
let ports = Ports { mind : & rig . mind , launcher : & rig . launcher , clock : & rig . clock , host : " yggdrasil-host " } ;
assert! ( slot ( & ports , rig . mind . instance ( ) , RunTurn ::PersonaTurn , rig . clock . now ( ) ) . unwrap ( ) . live . is_some ( ) ) ;
assert_eq! ( unit ( & eureka ( ) , RunTurn ::Sleep ) . unwrap ( ) , " mind-persona@eureka.service " , " the night runs in the Persona unit " ) ;
rig . mind . committed ( vec! [ close ( run , recorded ( ) ) ] ) ;
rig . at ( 2 ) ;
let LaunchOutcome ::Launched { run : persona } = rig . launch ( RunTurn ::PersonaTurn , & [ ] ) else { panic! ( " not launched " ) } ;
rig . at ( 3 ) ;
assert_eq! ( rig . launch ( RunTurn ::Sleep , & [ ] ) , LaunchOutcome ::Busy { run : persona } , " a live Persona session holds the slot a night needs " ) ;
}
#[ test ]
fn a_sleep_run_takes_no_claims ( ) {
let rig = Rig ::new ( ) ;
assert_eq! ( rig . launch ( RunTurn ::Sleep , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::Refused ( Declined ::PersonaRunTakesNoClaims ) ) ;
assert_eq! ( rig . runs ( ) , 0 ) ;
}
2026-10-10 05:32:19 +02:00
#[ test ]
fn the_unit_started_names_the_minds_instance ( ) {
let rig = Rig ::new ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
rig . launch ( RunTurn ::PersonaTurn , & [ ] ) ;
2026-10-10 05:32:19 +02:00
let started = rig . launcher . started . borrow ( ) ;
assert_eq! ( started [ 0 ] . 0 , * rig . mind . instance ( ) ) ;
2026-10-09 20:51:07 +02:00
}
#[ test ]
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
fn a_run_carries_no_budget_and_each_persona_turn_opens_after_the_last_ended ( ) {
2026-10-09 20:51:07 +02:00
let rig = Rig ::new ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
for n in 0 .. 3 {
rig . at ( n ) ;
let LaunchOutcome ::Launched { run } = rig . launch ( RunTurn ::PersonaTurn , & [ ] ) else { panic! ( " not launched " ) } ;
2026-10-09 20:51:07 +02:00
let view = rig . mind . view ( K ::Run , & run . id . 0 ) . unwrap ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( the_run ( & view ) . budget_usd . 0 , " 0 " , " the dead record field is written zero " ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( the_run ( & view ) . turn , RunTurn ::PersonaTurn ) ;
assert! ( the_run ( & view ) . claims . is_empty ( ) & & the_run ( & view ) . campaigns . is_empty ( ) ) ;
2026-10-10 05:32:19 +02:00
// One live Persona turn of hers at a time: end this one.
2026-10-09 23:11:20 +02:00
rig . mind . committed ( vec! [ close ( run , recorded ( ) ) ] ) ;
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:17:11 +02:00
assert_eq! ( rig . started ( ) , 3 , " each Persona turn ended before the next opened, so none was refused " ) ;
}
#[ test ]
2026-10-10 05:32:19 +02:00
fn a_live_persona_turn_is_busy_to_a_persona_launch_and_does_not_block_a_self_run ( ) {
2026-10-10 05:17:11 +02:00
let rig = Rig ::new ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Launched { run } = rig . launch ( RunTurn ::PersonaTurn , & [ ] ) else { panic! ( " not launched " ) } ;
2026-10-10 05:17:11 +02:00
rig . at ( 1 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Busy { run } ) ;
2026-10-10 05:32:19 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 1 ) , " Busy admits and starts nothing " ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-09 20:51:07 +02:00
}
#[ test ]
fn run_labels_are_utc_to_the_millisecond ( ) {
assert_eq! ( run_label ( at ( 123 ) ) . 0 , " mind-20261009T131500123Z " ) ;
assert_ne! ( run_label ( at ( 123 ) ) , run_label ( at ( 124 ) ) ) ;
}
#[ test ]
fn two_launches_in_one_millisecond_open_one_run ( ) {
let rig = Rig ::new ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 05:32:19 +02:00
// A Self run holds another slot, so only the key (instance and label) collides.
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let second = rig . launch ( RunTurn ::SelfRun , & [ ] ) ;
2026-10-09 20:51:07 +02:00
assert! ( matches! ( second , LaunchOutcome ::Refused ( _ ) ) , " {second:?} " ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 1 ) ) ;
}
#[ test ]
fn a_refused_run_never_starts_a_unit ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , follow_up ( " later " , & spec_ref ( " x " ) ) ] ) ;
let later = reference ( K ::FollowUp , & id ( " follow_up " , " later " ) ) ;
rig . mind . committed ( vec! [ run ( " op " , RunTurn ::SelfRun , RunOperator ::Operator , std ::slice ::from_ref ( & later ) ) ] ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch ( RunTurn ::SelfRun , std ::slice ::from_ref ( & later ) ) ;
2026-10-09 20:51:07 +02:00
assert! ( matches! ( outcome , LaunchOutcome ::Refused ( Declined ::Admission ( MindRefusal ::AlreadyClaimed { .. } ) ) ) , " {outcome:?} " ) ;
for ( answer , declined ) in [
( Outcome ::Conflict { identities : vec ! [ ] } , Declined ::Conflict ) ,
( Outcome ::AlreadyAdmitted { receipt_id : " r " . into ( ) } , Declined ::AlreadyAdmitted ) ,
] {
let mind = Answering { inner : & rig . mind , outcome : answer } ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch_through ( & mind , RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::Refused ( declined ) ) ;
2026-10-09 20:51:07 +02:00
}
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 ) ) ;
}
#[ test ]
fn a_start_failure_withdraws_the_run_it_opened_and_only_that_run ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , spec ( " y " ) ] ) ;
rig . mind . committed ( vec! [ run ( " other " , RunTurn ::SelfRun , RunOperator ::Operator , & [ spec_ref ( " y " ) ] ) ] ) ;
rig . launcher . fail . set ( true ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( outcome , LaunchOutcome ::Refused ( Declined ::StartFailed ) ) ;
let opened = format! ( " {INSTANCE} :run:mind-20261009T131500000Z " ) ;
let status = | id : & str | rig . mind . view ( K ::Run , id ) . unwrap ( ) . status ;
2026-10-10 05:32:19 +02:00
assert! ( matches! ( status ( & opened ) , PipelineStatus ::Resolved { record , .. } if matches! ( record . outcome , ResolutionOutcome ::Withdrawn { .. } ) ) ) ;
assert_eq! ( status ( & run_ref ( " other " ) . id . 0 ) , PipelineStatus ::InForce ) ;
2026-10-09 20:51:07 +02:00
// The spec is free again.
rig . launcher . fail . set ( false ) ;
rig . at ( 1 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-09 20:51:07 +02:00
}
#[ test ]
fn a_spec_that_is_reported_blocked_or_claimed_is_not_queued_and_admits_nothing ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " done " ) , spec ( " blocked " ) , spec ( " held " ) , spec ( " free " ) ] ) ;
rig . mind . committed ( vec! [ report ( " done " ) ] ) ;
rig . mind . committed ( vec! [ question_in ( " fork " , & spec_ref ( " blocked " ) ) ] ) ;
rig . mind . committed ( vec! [ run ( " op " , RunTurn ::SelfRun , RunOperator ::Operator , & [ spec_ref ( " held " ) ] ) ] ) ;
for item in [ " done " , " blocked " , " held " ] {
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( item ) ] ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( outcome , LaunchOutcome ::NotQueued { item : spec_ref ( item ) } ) ;
}
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let mixed = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " free " ) , spec_ref ( " done " ) ] ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( mixed , LaunchOutcome ::NotQueued { item : spec_ref ( " done " ) } , " one unqueued claim refuses the whole request " ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 ) ) ;
}
#[ test ]
fn a_second_live_self_run_of_hers_is_busy_and_other_runs_do_not_count ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , spec ( " y " ) ] ) ;
// None of these is a live Self run of hers.
rig . mind . committed ( vec! [ run ( " op " , RunTurn ::SelfRun , RunOperator ::Operator , & [ ] ) ] ) ;
rig . mind . committed ( vec! [ run ( " gone " , RunTurn ::SelfRun , RunOperator ::Mind , & [ ] ) ] ) ;
rig . mind . committed ( vec! [ close ( run_ref ( " gone " ) , withdrawn ( ) ) ] ) ;
2026-10-10 05:32:19 +02:00
rig . seed ( " persona " , RunTurn ::PersonaTurn , & [ ] ) ;
2026-10-09 20:51:07 +02:00
rig . at ( 1 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Launched { run : first } = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) else {
2026-10-09 20:51:07 +02:00
panic! ( " blocked by a run that is not hers " )
} ;
let ( runs , started ) = ( rig . runs ( ) , rig . started ( ) ) ;
rig . at ( 2 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " y " ) ] ) , LaunchOutcome ::Busy { run : first . clone ( ) } ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( runs , started ) , " Busy admits and starts nothing " ) ;
// Ending her run frees the slot.
rig . mind . committed ( vec! [ close ( first , recorded ( ) ) ] ) ;
rig . at ( 3 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " y " ) ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:32:19 +02:00
#[ test ]
fn an_alive_run_is_busy_however_old_and_is_not_closed ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
rig . seed ( " old " , RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) ;
rig . age ( & run_ref ( " old " ) . id . 0 , 24 * 3600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Busy { run : run_ref ( " old " ) } ) ;
2026-10-10 05:32:19 +02:00
assert! ( rig . in_force ( " old " ) ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 ) ) ;
}
#[ test ]
fn a_claimless_alive_run_is_busy ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
rig . seed ( " bare " , RunTurn ::SelfRun , & [ ] ) ;
rig . age ( & run_ref ( " bare " ) . id . 0 , 3600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::Busy { run : run_ref ( " bare " ) } ) ;
2026-10-10 05:32:19 +02:00
assert! ( rig . in_force ( " bare " ) ) ;
}
#[ test ]
fn a_dead_run_is_closed_past_the_grace_in_the_batch_that_opens_the_next ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
// Her run on x, admitted, and its unit never ran.
rig . mind . committed ( vec! [ run ( " dead " , RunTurn ::SelfRun , RunOperator ::Mind , & [ spec_ref ( " x " ) ] ) ] ) ;
let dead = run_ref ( " dead " ) . id . 0 ;
rig . age ( & dead , START_GRACE_S - 1 ) ;
assert_eq! (
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
rig . launch ( RunTurn ::SelfRun , & [ ] ) ,
2026-10-10 05:32:19 +02:00
LaunchOutcome ::Busy { run : run_ref ( " dead " ) } ,
" inside the grace the unit may still be starting "
) ;
assert! ( rig . in_force ( " dead " ) ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 ) ) ;
rig . age ( & dead , START_GRACE_S ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Launched { run : next } = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) else {
2026-10-10 05:32:19 +02:00
panic! ( " the dead run's claim stayed held " )
} ;
assert! ( ! rig . in_force ( " dead " ) ) ;
let ( closure , closed_in ) = the_closure ( & rig , " dead " , 1 ) ;
assert_eq! ( closure . outcome , ResolutionOutcome ::Recorded { reason : " unit not active " . into ( ) } ) ;
assert_eq! ( closure . subject , run_ref ( " dead " ) ) ;
assert_eq! (
closed_in ,
rig . mind . view ( K ::Run , & next . id . 0 ) . unwrap ( ) . admission . receipt_id ,
" one admission closes the dead run and opens the next "
) ;
assert_eq! ( rig . started ( ) , 1 ) ;
}
#[ test ]
fn a_dead_runs_closure_takes_the_next_sequence_of_its_subject ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ run ( " dead " , RunTurn ::SelfRun , RunOperator ::Mind , & [ ] ) ] ) ;
rig . mind . committed ( vec! [ close ( run_ref ( " dead " ) , recorded ( ) ) ] ) ;
// Withdrawing that closure puts the run back in force.
let first = reference ( K ::Resolution , & format! ( " {INSTANCE} :resolution:run.dead.n1 " ) ) ;
rig . mind . committed ( vec! [ close ( first , withdrawn ( ) ) ] ) ;
assert! ( rig . in_force ( " dead " ) ) ;
rig . age ( & run_ref ( " dead " ) . id . 0 , 600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 05:32:19 +02:00
assert! ( ! rig . in_force ( " dead " ) ) ;
assert_eq! ( the_closure ( & rig , " dead " , 2 ) . 0. sequence , 2 ) ;
}
#[ test ]
fn only_a_dead_holder_of_the_requests_turn_is_closed ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ run ( " op " , RunTurn ::SelfRun , RunOperator ::Operator , & [ ] ) ] ) ;
rig . mind . committed ( vec! [ run ( " persona " , RunTurn ::PersonaTurn , RunOperator ::Mind , & [ ] ) ] ) ;
rig . mind . committed ( vec! [ run ( " dead " , RunTurn ::SelfRun , RunOperator ::Mind , & [ ] ) ] ) ;
rig . age ( & run_ref ( " dead " ) . id . 0 , 3600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 05:32:19 +02:00
assert! ( ! rig . in_force ( " dead " ) ) ;
assert! ( rig . in_force ( " op " ) & & rig . in_force ( " persona " ) , " an Operator run and a run of the other turn are never closed by this launch " ) ;
}
#[ test ]
fn a_run_whose_admit_answer_was_lost_is_closed_by_the_next_launch_of_its_turn ( ) {
struct LosesAnswer < ' a > ( & ' a TestMind ) ;
impl MindPort for LosesAnswer < '_ > {
fn instance ( & self ) -> & Slug {
self . 0. instance ( )
}
fn query ( & self , selection : & Selection ) -> Result < huginn_mind ::PipelineSelectionPage > {
self . 0. query ( selection )
}
fn admit ( & self , agent : & str , session : & str , documents : Vec < PipelineDocument > ) -> Result < Outcome > {
self . 0. admit ( agent , session , documents ) ? ;
Err ( anyhow! ( " the answer was lost " ) )
}
}
let rig = Rig ::new ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( rig . try_launch_through ( & LosesAnswer ( & rig . mind ) , RunTurn ::SelfRun , & [ ] ) . is_err ( ) ) ;
2026-10-10 05:32:19 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 ) , " committed, and no unit was started " ) ;
let lost = rig . mind . ids ( K ::Run ) . remove ( 0 ) ;
rig . age ( & lost , START_GRACE_S ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 05:32:19 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 2 , 1 ) ) ;
let lost_label = lost . rsplit ( ':' ) . next ( ) . unwrap ( ) . to_string ( ) ;
assert! ( ! rig . in_force ( & lost_label ) ) ;
}
#[ test ]
fn a_port_that_admits_a_rival_live_run_between_step_0_and_the_admission_is_busy_and_starts_nothing ( ) {
struct Racing < ' a > {
inner : & ' a TestMind ,
raced : Cell < bool > ,
}
impl MindPort for Racing < '_ > {
fn instance ( & self ) -> & Slug {
self . inner . instance ( )
}
fn query ( & self , selection : & Selection ) -> Result < huginn_mind ::PipelineSelectionPage > {
self . inner . query ( selection )
}
fn admit ( & self , agent : & str , session : & str , documents : Vec < PipelineDocument > ) -> Result < Outcome > {
if ! self . raced . replace ( true ) {
self . inner . committed ( vec! [ run ( " rival " , RunTurn ::SelfRun , RunOperator ::Mind , & [ ] ) ] ) ;
}
self . inner . admit ( agent , session , documents )
}
}
let rig = Rig ::new ( ) ;
let racing = Racing { inner : & rig . mind , raced : Cell ::new ( false ) } ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch_through ( & racing , RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Busy { run : run_ref ( " rival " ) } ) ;
2026-10-10 05:32:19 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 ) ) ;
}
2026-10-10 05:46:16 +02:00
#[ test ]
fn an_already_live_holder_the_mind_does_not_show_as_hers_is_refused_not_trusted ( ) {
struct Forged < ' a > ( & ' a TestMind , & ' a str ) ;
impl MindPort for Forged < '_ > {
fn instance ( & self ) -> & Slug {
self . 0. instance ( )
}
fn query ( & self , selection : & Selection ) -> Result < huginn_mind ::PipelineSelectionPage > {
self . 0. query ( selection )
}
fn admit ( & self , _agent : & str , _session : & str , _documents : Vec < PipelineDocument > ) -> Result < Outcome > {
let run = huginn_mind ::RunId ::try_from ( self . 1. to_string ( ) ) . unwrap ( ) ;
Ok ( Outcome ::Refused ( MindRefusal ::AlreadyLive { run } ) )
}
}
let rig = Rig ::new ( ) ;
2026-10-10 05:46:25 +02:00
// A Persona turn of hers is live, and a Self run of hers is not: a real Self holder named by admission is the race test's Busy.
2026-10-10 05:46:16 +02:00
rig . seed ( " persona " , RunTurn ::PersonaTurn , & [ ] ) ;
for foreign in
[ " other:run:mind-20261010T010203004Z " , " eureka-body:run:mind-20261010T010203004Z " , " yggdrasil:run:nobody " , " yggdrasil:run:persona " ]
{
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch_through ( & Forged ( & rig . mind , foreign ) , RunTurn ::SelfRun , & [ ] ) ;
2026-10-10 05:46:16 +02:00
assert! ( matches! ( outcome , LaunchOutcome ::Refused ( Declined ::Admission ( MindRefusal ::AlreadyLive { .. } ) ) ) , " {foreign}: {outcome:?} " ) ;
}
assert_eq! ( rig . started ( ) , 0 ) ;
}
#[ test ]
fn a_run_whose_unit_died_is_closed_by_the_next_launch_of_its_turn ( ) {
let rig = Rig ::new ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Launched { run : first } = rig . launch ( RunTurn ::SelfRun , & [ ] ) else { panic! ( " not launched " ) } ;
2026-10-10 05:46:16 +02:00
let first_label = first . id . 0. rsplit ( ':' ) . next ( ) . unwrap ( ) . to_string ( ) ;
rig . age ( & first . id . 0 , 3600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Busy { run : first . clone ( ) } , " its unit is still running " ) ;
2026-10-10 06:54:29 +02:00
rig . launcher . kill ( RunTurn ::SelfRun ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 05:46:16 +02:00
assert! ( ! rig . in_force ( & first_label ) ) ;
assert_eq! ( rig . started ( ) , 2 ) ;
}
2026-10-09 20:51:07 +02:00
#[ test ]
fn a_tripped_item_raises_one_question_launches_nothing_and_a_ruling_reopens_it ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
history ( & rig . mind , & spec_ref ( " x " ) , & [ false , false , false ] ) ;
let before = rig . runs ( ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) ;
2026-10-10 05:32:19 +02:00
let question = id ( " question " , " breaker-mind-20261009T131500000Z " ) ;
2026-11-01 02:09:49 +01:00
assert_eq! ( outcome , LaunchOutcome ::Tripped { run : run_ref ( " h2 " ) , question : reference ( K ::Question , & question ) } ) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( before , 0 ) ) ;
assert_eq! ( rig . mind . ids ( K ::Question ) , vec! [ question . clone ( ) ] ) ;
let view = rig . mind . view ( K ::Question , & question ) . unwrap ( ) ;
let PipelineDocument ::Question ( asked ) = & view . document else { panic! ( " not a question " ) } ;
2026-11-01 02:09:49 +01:00
assert_eq! ( ( asked . raised_in . clone ( ) , asked . campaign . 0. as_str ( ) ) , ( Some ( run_ref ( " h2 " ) ) , CAMPAIGN ) , " raised in the run whose end tripped it " ) ;
2026-11-01 02:27:11 +01:00
assert! ( asked . question . 0. contains ( & run_ref ( " h2 " ) . id . 0 ) & & asked . question . 0. contains ( " claims held: 1 " ) , " {} " , asked . question . 0 ) ;
2026-11-01 12:29:55 +01:00
let texts : Vec < ( & str , & str ) > = asked . options . iter ( ) . map ( | option | ( option . label . 0. as_str ( ) , option . text . 0. as_str ( ) ) ) . collect ( ) ;
assert_eq! (
texts ,
[
( " reopen " , " Answer to reopen every claim of the run with a fresh count. " ) ,
( " retire " , " Resolve the claims so they are no longer worked, and answer with this option. " )
] ,
" retire is resolved by its answerer: no option promises the machine will "
) ;
2026-10-09 20:51:07 +02:00
assert_eq! ( view . admission . provenance . session . 0 , " mind-20261009T131500000Z " ) ;
// Open, the question blocks the item.
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::NotQueued { item : spec_ref ( " x " ) } ) ;
2026-10-10 05:32:19 +02:00
// The ruling that answers it opens the item, and the count starts afresh.
2026-10-09 20:51:07 +02:00
rig . mind . committed ( vec! [ ruling ( " reopen-x " , Some ( ( & question , " reopen " ) ) ) ] ) ;
rig . at ( 1 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:32:19 +02:00
#[ test ]
fn launching_a_tripped_item_with_another_queued_trips_and_launches_nothing ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , spec ( " y " ) ] ) ;
history_as ( & rig . mind , & spec_ref ( " x " ) , " x " , & [ false , false , false ] ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) ;
2026-10-10 05:32:19 +02:00
assert! ( matches! ( outcome , LaunchOutcome ::Tripped { .. } ) , " {outcome:?} " ) ;
assert_eq! ( rig . started ( ) , 0 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " y " ) ] ) , LaunchOutcome ::Launched { .. } ) , " the other item still launches " ) ;
2026-10-10 05:32:19 +02:00
}
#[ test ]
fn a_second_trip_raises_a_second_question_with_its_own_key ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
history ( & rig . mind , & spec_ref ( " x " ) , & [ false , false , false ] ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Tripped { question : first , .. } = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) else { panic! ( " no first trip " ) } ;
2026-10-10 05:32:19 +02:00
rig . mind . committed ( vec! [ ruling ( " reopen " , Some ( ( & first . id . 0 , " reopen " ) ) ) ] ) ;
history_as ( & rig . mind , & spec_ref ( " x " ) , " g " , & [ false , false , false ] ) ;
rig . at ( 1 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let LaunchOutcome ::Tripped { question : second , .. } = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) else {
2026-10-10 05:32:19 +02:00
panic! ( " no second trip " )
} ;
assert_ne! ( first , second ) ;
assert_eq! ( rig . mind . ids ( K ::Question ) . len ( ) , 2 ) ;
assert_eq! ( rig . started ( ) , 0 ) ;
}
#[ test ]
fn nine_empty_runs_of_fifty_before_the_answer_do_not_trip_after_it ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
let item = spec_ref ( " x " ) ;
history ( & rig . mind , & item , & [ false , false , true , false , false , true , false , false , true , false , false , true , false ] ) ;
rig . mind . committed ( vec! [ question_in ( " fork " , & item ) ] ) ;
rig . mind . committed ( vec! [ ruling ( " answer " , Some ( ( & id ( " question " , " fork " ) , " A " ) ) ) ] ) ;
history_as ( & rig . mind , & item , " g " , & [ false ] ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ item ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 05:32:19 +02:00
}
#[ test ]
fn a_dead_run_is_an_ended_empty_run_for_the_breaker_and_its_claim_is_free ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
history ( & rig . mind , & spec_ref ( " x " ) , & [ false , false ] ) ;
rig . mind . committed ( vec! [ run ( " dead " , RunTurn ::SelfRun , RunOperator ::Mind , & [ spec_ref ( " x " ) ] ) ] ) ;
rig . age ( & run_ref ( " dead " ) . id . 0 , 600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let outcome = rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) ;
2026-10-10 05:32:19 +02:00
assert! ( matches! ( outcome , LaunchOutcome ::Tripped { .. } ) , " {outcome:?} " ) ;
assert! ( ! rig . in_force ( " dead " ) , " the trip's batch closes the dead run too " ) ;
2026-11-01 02:09:49 +01:00
let question = rig . mind . ids ( K ::Question ) . pop ( ) . expect ( " the trip asked " ) ;
let PipelineDocument ::Question ( asked ) = rig . mind . view ( K ::Question , & question ) . unwrap ( ) . document else { panic! ( " not a question " ) } ;
assert_eq! ( asked . raised_in , Some ( run_ref ( " dead " ) ) , " the dead holder, closed in the same batch, is the run the question is raised in " ) ;
2026-10-10 05:32:19 +02:00
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 3 , 0 ) ) ;
}
2026-11-01 12:29:55 +01:00
/// A caller's earlier read of the hold (the heartbeat's, choosing its claims)
/// is advisory: a question that lands after it and before the gate is seen.
#[ test ]
fn a_question_landing_before_the_gate_reads_is_seen_by_the_run_it_would_have_opened ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , follow_up ( " brief " , & spec_ref ( " x " ) ) ] ) ;
let brief = reference ( K ::FollowUp , & id ( " follow_up " , " brief " ) ) ;
let landing = Landing {
inner : & rig . mind ,
question : RefCell ::new ( Some ( vec! [ question_in ( " fork " , & brief ) ] ) ) ,
hold_reads : Cell ::new ( 0 ) ,
lands_on : reads_anything ,
} ;
let outcome = rig . launch_through ( & landing , RunTurn ::SelfRun , std ::slice ::from_ref ( & brief ) ) ;
assert_eq! ( outcome , LaunchOutcome ::NotQueued { item : brief } ) ;
assert_eq! ( ( landing . hold_reads . get ( ) , rig . runs ( ) , rig . started ( ) ) , ( 1 , 0 , 0 ) , " one read, nothing admitted " ) ;
}
#[ test ]
fn a_brief_an_open_question_is_raised_in_cannot_be_launched_on ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , follow_up ( " brief " , & spec_ref ( " x " ) ) ] ) ;
let brief = reference ( K ::FollowUp , & id ( " follow_up " , " brief " ) ) ;
rig . mind . committed ( vec! [ question_in ( " fork " , & brief ) ] ) ;
assert_eq! ( rig . launch ( RunTurn ::SelfRun , std ::slice ::from_ref ( & brief ) ) , LaunchOutcome ::NotQueued { item : brief } ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 0 , 0 ) ) ;
}
2026-11-01 02:09:49 +01:00
#[ test ]
fn the_gate_refuses_a_claim_an_open_question_holds_whatever_its_kind ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) , follow_up ( " brief " , & spec_ref ( " x " ) ) ] ) ;
let brief = reference ( K ::FollowUp , & id ( " follow_up " , " brief " ) ) ;
// A question raised in a run holds the follow-up that run claimed, though the follow-up is no cut spec.
rig . mind . committed ( vec! [ run ( " failed " , RunTurn ::SelfRun , RunOperator ::Mind , & [ brief . clone ( ) , spec_ref ( " x " ) ] ) ] ) ;
rig . mind . committed ( vec! [ close ( run_ref ( " failed " ) , recorded ( ) ) ] ) ;
rig . mind . committed ( vec! [ question_in ( " fork " , & run_ref ( " failed " ) ) ] ) ;
let runs = rig . runs ( ) ;
assert_eq! ( rig . launch ( RunTurn ::SelfRun , std ::slice ::from_ref ( & brief ) ) , LaunchOutcome ::NotQueued { item : brief . clone ( ) } ) ;
assert_eq! ( rig . launch ( RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) , LaunchOutcome ::NotQueued { item : spec_ref ( " x " ) } , " a cut spec held by a run-raised question " ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( runs , 0 ) , " nothing admitted, nothing started " ) ;
rig . mind . committed ( vec! [ close ( reference ( K ::Question , & id ( " question " , " fork " ) ) , withdrawn ( ) ) ] ) ;
assert! ( matches! ( rig . launch ( RunTurn ::SelfRun , & [ brief ] ) , LaunchOutcome ::Launched { .. } ) , " withdrawn, the hold lifts " ) ;
}
2026-11-01 12:29:55 +01:00
/// A mind where a question lands the moment `lands_on` reads a selection.
2026-11-01 04:11:38 +01:00
struct Landing < ' a > {
inner : & ' a TestMind ,
question : RefCell < Option < Vec < PipelineDocument > > > ,
hold_reads : Cell < u32 > ,
2026-11-01 12:29:55 +01:00
lands_on : fn ( & Selection ) -> bool ,
}
fn reads_the_hold ( selection : & Selection ) -> bool {
selection . schemas . as_deref ( ) = = Some ( & [ K ::Question . type_id ( ) . to_string ( ) ] [ .. ] )
& & selection . fields . iter ( ) . flatten ( ) . any ( | predicate | predicate . index = = " in_force " )
}
fn reads_anything ( _ : & Selection ) -> bool {
true
2026-11-01 04:11:38 +01:00
}
impl MindPort for Landing < '_ > {
fn instance ( & self ) -> & Slug {
self . inner . instance ( )
}
fn query ( & self , selection : & Selection ) -> Result < huginn_mind ::PipelineSelectionPage > {
let answer = self . inner . query ( selection ) ;
2026-11-01 12:29:55 +01:00
if reads_the_hold ( selection ) {
2026-11-01 04:11:38 +01:00
self . hold_reads . set ( self . hold_reads . get ( ) + 1 ) ;
2026-11-01 12:29:55 +01:00
}
if ( self . lands_on ) ( selection )
& & let Some ( documents ) = self . question . take ( )
{
self . inner . committed ( documents ) ;
2026-11-01 04:11:38 +01:00
}
answer
}
fn admit (
& self ,
agent : & str ,
session : & str ,
documents : Vec < PipelineDocument > ,
) -> Result < Outcome > {
self . inner . admit ( agent , session , documents )
}
}
#[ test ]
fn a_launch_reads_the_hold_once_and_a_question_landing_after_it_changes_no_answer_within_the_launch ( )
{
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ spec ( " x " ) ] ) ;
let landing = Landing {
inner : & rig . mind ,
question : RefCell ::new ( Some ( vec! [ question_in ( " fork " , & spec_ref ( " x " ) ) ] ) ) ,
hold_reads : Cell ::new ( 0 ) ,
2026-11-01 12:29:55 +01:00
lands_on : reads_the_hold ,
2026-11-01 04:11:38 +01:00
} ;
2026-11-01 12:29:55 +01:00
// The gate's own snapshot is read first (the question lands behind it); the queue and the gate use that one.
2026-11-01 04:11:38 +01:00
let outcome = rig . launch_through ( & landing , RunTurn ::SelfRun , & [ spec_ref ( " x " ) ] ) ;
assert! (
matches! ( outcome , LaunchOutcome ::Launched { .. } ) ,
" {outcome:?}: queue and gate answered from one snapshot "
) ;
assert_eq! (
landing . hold_reads . get ( ) ,
1 ,
" the hold was read once for the whole launch "
) ;
assert_eq! (
2026-11-01 04:12:34 +01:00
crate ::queue ::on_hold ( & rig . mind ) . unwrap ( ) . items ( ) ,
2026-11-01 04:11:38 +01:00
[ spec_ref ( " x " ) ] ,
" the question did land "
) ;
}
2026-10-10 22:59:21 +02:00
/// The one source of every drawn case: splitmix64 from a fixed seed, so a
/// failure names a case that runs again.
struct Draw ( u64 ) ;
impl Draw {
fn next ( & mut self ) -> u64 {
self . 0 = self . 0. wrapping_add ( 0x9e37_79b9_7f4a_7c15 ) ;
let mut z = self . 0 ;
z = ( z ^ ( z > > 30 ) ) . wrapping_mul ( 0xbf58_476d_1ce4_e5b9 ) ;
z = ( z ^ ( z > > 27 ) ) . wrapping_mul ( 0x94d0_49bb_1331_11eb ) ;
z ^ ( z > > 31 )
}
fn below ( & mut self , bound : usize ) -> usize {
( self . next ( ) % bound as u64 ) as usize
}
}
const LENGTH_EDGES : [ usize ; 13 ] = [ 1 , 2 , 3 , 8 , 9 , 15 , 16 , 17 , 31 , 32 , 33 , 63 , 64 ] ;
const ALPHABETS : [ & str ; 6 ] = [
" abcdefghijklmnopqrstuvwxyz " ,
" abcdefghijklmnopqrstuvwxyz0123456789 " ,
" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " ,
" ab-_ " ,
" a-b_c-d_e0 " ,
" abcdefghijklmnopqrstuvwxyz-_ " ,
] ;
/// A valid slug of `length` characters, over an alphabet the draw picks, with
/// both separators (`-`, `_`) mixed in at random positions.
fn slug_of ( draw : & mut Draw , length : usize ) -> String {
loop {
let alphabet : Vec < char > = ALPHABETS [ draw . below ( ALPHABETS . len ( ) ) ] . chars ( ) . collect ( ) ;
let name : String = ( 0 .. length )
. map ( | _ | match draw . below ( 6 ) {
0 = > '-' ,
1 = > '_' ,
_ = > alphabet [ draw . below ( alphabet . len ( ) ) ] ,
} )
. collect ( ) ;
if Slug ( name . clone ( ) ) . validate_slug ( ) . is_ok ( ) {
return name ;
}
}
}
/// Valid slugs that differ from `name`: edits of it at every position (a
/// drawn character, the other separator, the other case), its prefixes, the
/// same name with every separator swapped, and independent draws.
fn distinct_valid ( name : & str , draw : & mut Draw ) -> Vec < String > {
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
let chars : Vec < char > = name . chars ( ) . collect ( ) ;
2026-10-10 22:59:21 +02:00
let pool : Vec < char > = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_ " . chars ( ) . collect ( ) ;
let swapped = | c : char | match c {
'-' = > '_' ,
'_' = > '-' ,
other = > other ,
} ;
let mut out = Vec ::new ( ) ;
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
for i in 0 .. chars . len ( ) {
2026-10-10 22:59:21 +02:00
let mut edited = chars . clone ( ) ;
edited [ i ] = swapped ( chars [ i ] ) ;
out . push ( edited . iter ( ) . collect ::< String > ( ) ) ;
let mut edited = chars . clone ( ) ;
edited [ i ] = if chars [ i ] . is_ascii_uppercase ( ) { chars [ i ] . to_ascii_lowercase ( ) } else { chars [ i ] . to_ascii_uppercase ( ) } ;
out . push ( edited . iter ( ) . collect ::< String > ( ) ) ;
for _ in 0 .. 3 {
let mut edited = chars . clone ( ) ;
edited [ i ] = pool [ draw . below ( pool . len ( ) ) ] ;
out . push ( edited . iter ( ) . collect ::< String > ( ) ) ;
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 22:59:21 +02:00
out . push ( chars [ .. i ] . iter ( ) . collect ::< String > ( ) ) ;
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 22:59:21 +02:00
out . push ( chars . iter ( ) . map ( | & c | swapped ( c ) ) . collect ::< String > ( ) ) ;
out . push ( name . to_uppercase ( ) ) ;
out . push ( name . to_lowercase ( ) ) ;
if chars . len ( ) < 64 {
out . push ( format! ( " {name} - " ) ) ;
out . push ( format! ( " {name} _ " ) ) ;
out . push ( format! ( " {name} 0 " ) ) ;
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 22:59:21 +02:00
for _ in 0 .. 8 {
let length = 1 + draw . below ( 64 ) ;
out . push ( slug_of ( draw , length ) ) ;
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 22:59:21 +02:00
out . retain ( | other | other ! = name & & Slug ( other . clone ( ) ) . validate_slug ( ) . is_ok ( ) ) ;
out
}
/// A mind that knows only its instance. Any read or write is an error that
/// says so and is counted, so a gate that lets a launch through is seen by
/// the error it gets back, and a gate that refuses is seen to have touched
/// nothing.
struct Named {
instance : Slug ,
reads : Cell < usize > ,
}
impl Named {
fn new ( instance : & str ) -> Self {
Self { instance : Slug ( instance . into ( ) ) , reads : Cell ::new ( 0 ) }
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 22:59:21 +02:00
}
impl MindPort for Named {
fn instance ( & self ) -> & Slug {
& self . instance
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 22:59:21 +02:00
fn query ( & self , _ : & Selection ) -> Result < PipelineSelectionPage > {
self . reads . set ( self . reads . get ( ) + 1 ) ;
Err ( anyhow! ( " reached the mind " ) )
}
fn admit ( & self , _ : & str , _ : & str , _ : Vec < PipelineDocument > ) -> Result < Outcome > {
self . reads . set ( self . reads . get ( ) + 1 ) ;
Err ( anyhow! ( " reached the mind " ) )
}
}
/// One launch by `turn` on a mind named `mind`, holding a Grant read for `grant`.
fn gated ( mind : & Named , grant : & str , turn : RunTurn , claims : Vec < PipelineRef > ) -> Result < LaunchOutcome > {
let ( launcher , clock ) = ( Recording ::default ( ) , Fixed ( Cell ::new ( at ( 0 ) ) ) ) ;
let ports = Ports { mind , launcher : & launcher , clock : & clock , host : " yggdrasil-host " } ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
let grant = Grant ::for_test ( Slug ( grant . into ( ) ) ) ;
2026-11-01 04:11:38 +01:00
let outcome = open_and_launch (
& ports ,
LaunchRequest {
turn ,
claims ,
agent : " agent-x " . into ( ) ,
grant ,
} ,
) ;
2026-10-10 22:59:21 +02:00
assert! ( launcher . started . borrow ( ) . is_empty ( ) ) ;
outcome
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 06:54:29 +02:00
#[ test ]
2026-10-10 22:59:21 +02:00
fn a_grant_passes_for_exactly_the_minds_own_instance_and_for_no_other_valid_slug ( ) {
let mut draw = Draw ( 0x6a09_e667_f3bc_c908 ) ;
let mut names : Vec < String > = Vec ::new ( ) ;
for i in 0 .. 780 {
names . push ( slug_of ( & mut draw , LENGTH_EDGES [ i % LENGTH_EDGES . len ( ) ] ) ) ;
}
for _ in 0 .. 220 {
let length = 1 + draw . below ( 64 ) ;
names . push ( slug_of ( & mut draw , length ) ) ;
}
let mut distinct = 0 ;
for name in & names {
2026-10-10 09:05:22 +02:00
for turn in [ RunTurn ::SelfRun , RunTurn ::PersonaTurn ] {
let claims = if turn = = RunTurn ::SelfRun { vec! [ spec_ref ( " x " ) ] } else { vec! [ ] } ;
2026-10-10 22:59:21 +02:00
let mind = Named ::new ( name ) ;
let through = gated ( & mind , name , turn , claims . clone ( ) ) . unwrap_err ( ) ;
assert_eq! ( format! ( " {through:#} " ) , " reached the mind " , " {name:?} {turn:?}: the mind's own Grant passes the gate " ) ;
for other in distinct_valid ( name , & mut draw ) {
let mind = Named ::new ( name ) ;
let outcome = gated ( & mind , & other , turn , claims . clone ( ) ) . unwrap ( ) ;
assert_eq! ( outcome , LaunchOutcome ::Refused ( Declined ::GrantForOtherInstance ) , " mind {name:?}, grant {other:?}, {turn:?} " ) ;
assert_eq! ( mind . reads . get ( ) , 0 , " a refused launch reads nothing " ) ;
distinct + = 1 ;
}
2026-10-10 09:05:22 +02:00
}
2026-10-10 06:54:29 +02:00
}
2026-10-10 22:59:21 +02:00
assert! ( distinct > 100_000 , " {distinct} " ) ;
// The families the property exists for are in the draw: long names, and both separators.
assert! ( names . iter ( ) . any ( | n | n . len ( ) > 32 ) & & names . iter ( ) . any ( | n | n . contains ( '-' ) ) & & names . iter ( ) . any ( | n | n . contains ( '_' ) ) ) ;
2026-10-10 06:54:29 +02:00
}
2026-10-22 17:37:38 +02:00
/// A claim id from the same generator as instance names: varied alphabets,
/// both separators and the edge lengths, so a refusal keyed on id text
/// cannot pass.
fn claim_id ( draw : & mut Draw ) -> String {
let length = LENGTH_EDGES [ draw . below ( LENGTH_EDGES . len ( ) ) ] ;
slug_of ( draw , length )
}
2026-10-10 06:54:29 +02:00
#[ test ]
2026-10-10 22:59:21 +02:00
fn a_persona_turn_with_any_claims_at_all_is_refused_and_touches_nothing ( ) {
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
let kinds = PipelineKind ::ALL ;
2026-10-10 22:59:21 +02:00
let mut draw = Draw ( 0xbb67_ae85_84ca_a73b ) ;
let mut sets : Vec < Vec < PipelineRef > > = Vec ::new ( ) ;
// Every rotation of the kinds, so no kind or position is the one that starts, at every small size.
for start in 0 .. kinds . len ( ) {
for size in 1 ..= 40 {
2026-10-22 17:37:38 +02:00
sets . push ( ( 0 .. size ) . map ( | i | reference ( kinds [ ( start + i ) % kinds . len ( ) ] , & claim_id ( & mut draw ) ) ) . collect ( ) ) ;
let one = claim_id ( & mut draw ) ;
sets . push ( ( 0 .. size ) . map ( | i | reference ( kinds [ ( start + i ) % kinds . len ( ) ] , & one ) ) . collect ( ) ) ;
2026-10-10 22:59:21 +02:00
}
2026-10-10 20:21:17 +02:00
}
2026-10-10 22:59:21 +02:00
// Every kind alone, repeated.
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
for kind in kinds {
for size in 1 ..= 40 {
2026-10-22 17:37:38 +02:00
let one = claim_id ( & mut draw ) ;
sets . push ( ( 0 .. size ) . map ( | _ | reference ( * kind , & one ) ) . collect ( ) ) ;
mind-body launch tests: the gates are observed by property, not by example
The instance gate is checked against names generated from the real one:
every single-character substitution, deletion and insertion, every
transposition and permutation, rotations, prefixes and suffixes, case flips,
NUL and whitespace padding, repetitions, and seeded random names of the same
and other lengths. The Persona refusal is checked at every claim-set size
from one to 300 in four shapes, duplicates included, and for every kind
alone. A compare or bound weaker than equality passes neither.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:15:20 +02:00
}
2026-10-10 19:19:14 +02:00
}
2026-10-10 22:59:21 +02:00
// Drawn: every position a kind and an id from a pool of any size (one id repeated, to all distinct),
// at sizes from one to well past any bound a refusal could be written with.
let sizes = [ 1 , 2 , 3 , 4 , 5 , 12 , 13 , 14 , 26 , 27 , 64 , 65 , 100 , 255 , 256 , 300 ] ;
for round in 0 .. 3_000 {
let size = if round < 600 { sizes [ round % sizes . len ( ) ] } else { 1 + draw . below ( 300 ) } ;
2026-10-22 17:37:38 +02:00
let pool : Vec < String > = ( 0 .. 1 + draw . below ( size ) ) . map ( | _ | claim_id ( & mut draw ) ) . collect ( ) ;
sets . push ( ( 0 .. size ) . map ( | _ | reference ( kinds [ draw . below ( kinds . len ( ) ) ] , & pool [ draw . below ( pool . len ( ) ) ] ) ) . collect ( ) ) ;
2026-10-10 22:59:21 +02:00
}
for size in [ 1_000 , 10_000 , 100_000 ] {
2026-10-22 17:37:38 +02:00
sets . push ( ( 0 .. size ) . map ( | _ | reference ( kinds [ draw . below ( kinds . len ( ) ) ] , & claim_id ( & mut draw ) ) ) . collect ( ) ) ;
2026-10-10 22:59:21 +02:00
}
for claims in sets {
2026-10-10 23:03:30 +02:00
let length = LENGTH_EDGES [ draw . below ( LENGTH_EDGES . len ( ) ) ] ;
let name = slug_of ( & mut draw , length ) ;
2026-10-10 22:59:21 +02:00
let mind = Named ::new ( & name ) ;
let outcome = gated ( & mind , & name , RunTurn ::PersonaTurn , claims . clone ( ) ) . unwrap ( ) ;
assert_eq! ( outcome , LaunchOutcome ::Refused ( Declined ::PersonaRunTakesNoClaims ) , " {} claims, first {:?} " , claims . len ( ) , claims . first ( ) ) ;
assert_eq! ( mind . reads . get ( ) , 0 ) ;
}
// Without claims the same launch goes on to the mind.
let mind = Named ::new ( " yggdrasil " ) ;
assert_eq! ( format! ( " {:#} " , gated ( & mind , " yggdrasil " , RunTurn ::PersonaTurn , vec! [ ] ) . unwrap_err ( ) ) , " reached the mind " ) ;
2026-10-10 06:54:29 +02:00
}
#[ test ]
fn a_persona_launch_closes_a_dead_persona_holder_past_the_grace ( ) {
let rig = Rig ::new ( ) ;
rig . mind . committed ( vec! [ run ( " pdead " , RunTurn ::PersonaTurn , RunOperator ::Mind , & [ ] ) ] ) ;
rig . age ( & run_ref ( " pdead " ) . id . 0 , START_GRACE_S - 1 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert_eq! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Busy { run : run_ref ( " pdead " ) } , " inside the grace " ) ;
2026-10-10 06:54:29 +02:00
rig . age ( & run_ref ( " pdead " ) . id . 0 , START_GRACE_S ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 06:54:29 +02:00
assert! ( ! rig . in_force ( " pdead " ) ) ;
assert_eq! ( the_closure ( & rig , " pdead " , 1 ) . 0. outcome , ResolutionOutcome ::Recorded { reason : " unit not active " . into ( ) } ) ;
assert_eq! ( ( rig . runs ( ) , rig . started ( ) ) , ( 2 , 1 ) ) ;
}
#[ test ]
2026-10-10 08:11:46 +02:00
fn a_live_self_run_does_not_make_a_persona_launch_busy ( ) {
2026-10-10 06:54:29 +02:00
let rig = Rig ::new ( ) ;
rig . seed ( " alive-self " , RunTurn ::SelfRun , & [ ] ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 08:11:46 +02:00
assert! ( rig . in_force ( " alive-self " ) ) ;
}
#[ test ]
fn a_dead_self_run_is_not_closed_by_a_persona_launch ( ) {
let rig = Rig ::new ( ) ;
2026-10-10 06:54:29 +02:00
rig . mind . committed ( vec! [ run ( " dead-self " , RunTurn ::SelfRun , RunOperator ::Mind , & [ ] ) ] ) ;
rig . age ( & run_ref ( " dead-self " ) . id . 0 , 3600 ) ;
mind-body: tear out dollar metering; a shared tempo heartbeat; usage-only connector (WIP, tests follow)
Delete the classifier, initiative, burn, base-rate dial, shares, Envelope dollars,
persona turn max_cost, --max-budget-usd and the Self-run settle. Add tempo::tally
and tempo::pick, self-declared urgency from the reply as a Pulse, the usage line in
Persona input, and the wire-v4 connector (lease v2, usage_readout).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 05:24:54 +01:00
assert! ( matches! ( rig . launch ( RunTurn ::PersonaTurn , & [ ] ) , LaunchOutcome ::Launched { .. } ) ) ;
2026-10-10 08:11:46 +02:00
assert! ( rig . in_force ( " dead-self " ) , " a Persona launch closes only Persona holders " ) ;
2026-10-10 06:54:29 +02:00
}
2026-10-09 20:51:07 +02:00
struct Spy {
calls : RefCell < Vec < Vec < String > > > ,
success : Cell < bool > ,
2026-10-12 03:48:49 +02:00
code : Cell < Option < i32 > > ,
2026-10-09 20:51:07 +02:00
stdout : RefCell < String > ,
2026-10-22 17:37:08 +02:00
/// Per-unit answers to `is-active`; a unit not listed gets `stdout`.
states : RefCell < Vec < ( & 'static str , & 'static str ) > > ,
2026-10-09 20:51:07 +02:00
}
impl Systemctl for & 'static Spy {
fn run ( & self , args : & [ & str ] ) -> Result < SystemctlReply > {
self . calls . borrow_mut ( ) . push ( args . iter ( ) . map ( | arg | arg . to_string ( ) ) . collect ( ) ) ;
2026-10-22 17:37:08 +02:00
let said = match args {
[ " is-active " , unit ] = > self . states . borrow ( ) . iter ( ) . find ( | ( named , _ ) | named = = unit ) . map ( | ( _ , state ) | state . to_string ( ) ) ,
_ = > None ,
} ;
Ok ( SystemctlReply { success : self . success . get ( ) , code : self . code . get ( ) , stdout : said . unwrap_or_else ( | | self . stdout . borrow ( ) . clone ( ) ) } )
2026-10-09 20:51:07 +02:00
}
}
2026-10-10 05:32:19 +02:00
fn spied ( ) -> ( SystemdLauncher , & 'static Spy ) {
2026-10-12 03:48:49 +02:00
let spy : & 'static Spy =
2026-10-22 17:37:08 +02:00
Box ::leak ( Box ::new ( Spy { calls : RefCell ::default ( ) , success : Cell ::new ( true ) , code : Cell ::new ( Some ( 0 ) ) , stdout : RefCell ::default ( ) , states : RefCell ::default ( ) } ) ) ;
2026-10-10 05:32:19 +02:00
( SystemdLauncher ::over ( Box ::new ( spy ) ) , spy )
}
fn eureka ( ) -> Slug {
Slug ( " eureka " . into ( ) )
2026-10-09 20:51:07 +02:00
}
#[ test ]
fn units_are_templated_by_instance_and_turn ( ) {
2026-10-10 05:32:19 +02:00
let ( launcher , spy ) = spied ( ) ;
2026-10-10 05:38:22 +02:00
assert_eq! ( unit ( & eureka ( ) , RunTurn ::PersonaTurn ) . unwrap ( ) , " mind-persona@eureka.service " ) ;
assert_eq! ( unit ( & eureka ( ) , RunTurn ::SelfRun ) . unwrap ( ) , " mind-self@eureka.service " ) ;
2026-10-10 06:54:29 +02:00
launcher . start ( & eureka ( ) , RunTurn ::SelfRun ) . unwrap ( ) ;
launcher . start ( & Slug ( " eureka.test " . into ( ) ) , RunTurn ::PersonaTurn ) . unwrap ( ) ;
2026-10-09 20:51:07 +02:00
assert_eq! (
* spy . calls . borrow ( ) ,
2026-10-10 05:38:22 +02:00
vec! [ vec! [ " start " , " --no-block " , " mind-self@eureka.service " ] , vec! [ " start " , " --no-block " , " mind-persona@eureka.test.service " ] , ]
2026-10-09 20:51:07 +02:00
) ;
}
#[ test ]
fn a_unit_is_alive_while_active_activating_or_reloading ( ) {
2026-10-10 05:32:19 +02:00
let ( launcher , spy ) = spied ( ) ;
for ( said , alive ) in
[ ( " active \n " , true ) , ( " activating \n " , true ) , ( " reloading \n " , true ) , ( " inactive \n " , false ) , ( " failed \n " , false ) , ( " " , false ) ]
{
2026-10-09 20:51:07 +02:00
* spy . stdout . borrow_mut ( ) = said . to_string ( ) ;
2026-10-10 06:54:29 +02:00
assert_eq! ( launcher . alive ( & eureka ( ) , RunTurn ::SelfRun ) , alive , " {said:?} " ) ;
2026-10-09 20:51:07 +02:00
}
2026-10-10 05:38:22 +02:00
assert_eq! ( spy . calls . borrow ( ) [ 0 ] , vec! [ " is-active " , " mind-self@eureka.service " ] ) ;
2026-10-09 20:51:07 +02:00
}
2026-10-12 03:48:49 +02:00
#[ test ]
2026-10-12 06:43:48 +02:00
fn stop_args_is_one_synchronous_stop_of_exactly_the_units_start_names ( ) {
for name in [ " eureka " , " eureka.test " , " eureka-two " ] {
let instance = Slug ( name . into ( ) ) ;
2026-11-01 19:11:21 +01:00
let args = stop_args ( StopScope ::Mind ( & instance ) ) . unwrap ( ) ;
2026-10-12 06:43:48 +02:00
assert_eq! ( args [ .. 2 ] , [ " stop " , " -- " ] , " one synchronous stop, no --no-block " ) ;
let ( launcher , spy ) = spied ( ) ;
let mut started = Vec ::new ( ) ;
for turn in [ RunTurn ::SelfRun , RunTurn ::PersonaTurn ] {
launcher . start ( & instance , turn ) . unwrap ( ) ;
started . push ( spy . calls . borrow ( ) . last ( ) . unwrap ( ) . last ( ) . unwrap ( ) . clone ( ) ) ;
}
2026-11-11 11:37:34 +00:00
assert_eq! ( args [ 2 .. 4 ] , started [ .. ] , " stop names exactly the units start names, for both turns " ) ;
assert_eq! ( args [ 4 ] , format! ( " mind-stream@ {name} .service " ) , " and the stream of that instance " ) ;
assert_eq! ( args . len ( ) , 5 ) ;
2026-10-12 06:43:48 +02:00
assert! ( args [ 2 .. ] . iter ( ) . all ( | unit | ! unit . contains ( [ '*' , '?' , '[' ] ) ) , " a glob can match another instance or none: {args:?} " ) ;
2026-10-12 03:48:49 +02:00
}
2026-11-11 11:37:34 +00:00
assert_eq! ( stop_args ( StopScope ::Mind ( & eureka ( ) ) ) . unwrap ( ) , [ " stop " , " -- " , " mind-self@eureka.service " , " mind-persona@eureka.service " , " mind-stream@eureka.service " ] ) ;
2026-11-01 19:11:21 +01:00
}
#[ test ]
fn stop_runs_the_one_stop_and_waits ( ) {
let ( launcher , spy ) = spied ( ) ;
launcher . stop ( StopScope ::Mind ( & eureka ( ) ) ) . unwrap ( ) ;
assert_eq! ( * spy . calls . borrow ( ) , vec! [ stop_args ( StopScope ::Mind ( & eureka ( ) ) ) . unwrap ( ) ] ) ;
2026-10-12 03:48:49 +02:00
}
#[ test ]
2026-11-01 19:11:21 +01:00
fn the_swarm_stop_is_one_stop_of_both_templates_globs ( ) {
2026-10-12 03:48:49 +02:00
let ( launcher , spy ) = spied ( ) ;
2026-11-01 19:11:21 +01:00
launcher . stop ( StopScope ::Swarm ) . unwrap ( ) ;
2026-11-11 11:37:34 +00:00
assert_eq! ( * spy . calls . borrow ( ) , vec! [ vec! [ " stop " , " -- " , " mind-self@*.service " , " mind-persona@*.service " , " mind-stream@*.service " ] ] ) ;
2026-11-01 19:11:21 +01:00
}
#[ test ]
fn a_failed_swarm_stop_names_the_exit_status_and_asks_no_state ( ) {
let ( launcher , spy ) = spied ( ) ;
spy . success . set ( false ) ;
spy . code . set ( Some ( 5 ) ) ;
* spy . stdout . borrow_mut ( ) = " canary-output-7f3a91
" .to_string();
let shown = format! ( " {:#} " , launcher . stop ( StopScope ::Swarm ) . unwrap_err ( ) ) ;
assert! ( shown . contains ( " exit status 5 " ) & & ! shown . contains ( " canary " ) , " {shown} " ) ;
assert_eq! ( spy . calls . borrow ( ) . len ( ) , 1 , " no per-unit query for the swarm " ) ;
spy . code . set ( None ) ;
assert! ( format! ( " {:#} " , launcher . stop ( StopScope ::Swarm ) . unwrap_err ( ) ) . contains ( " signal " ) ) ;
2026-10-12 03:48:49 +02:00
}
#[ test ]
2026-11-01 19:11:21 +01:00
fn stop_with_an_invalid_instance_is_an_error_and_runs_no_systemctl ( ) {
2026-10-12 03:48:49 +02:00
let ( launcher , spy ) = spied ( ) ;
for bad in [ " canary-../evil-7f3a91 " , " canary instance " , " " , " canary* " ] {
2026-11-01 19:11:21 +01:00
let error = launcher . stop ( StopScope ::Mind ( & Slug ( bad . into ( ) ) ) ) . unwrap_err ( ) ;
2026-10-12 03:48:49 +02:00
assert! ( ! format! ( " {error:#} " ) . contains ( " canary " ) , " {error:#} " ) ;
}
assert! ( spy . calls . borrow ( ) . is_empty ( ) ) ;
}
#[ test ]
2026-10-22 17:37:08 +02:00
fn a_failed_stop_names_the_exit_status_and_each_units_state_and_carries_no_other_output ( ) {
2026-10-12 03:48:49 +02:00
let ( launcher , spy ) = spied ( ) ;
spy . success . set ( false ) ;
spy . code . set ( Some ( 5 ) ) ;
2026-11-11 11:37:34 +00:00
spy . states . borrow_mut ( ) . extend ( [ ( " mind-self@eureka.service " , " inactive \n " ) , ( " mind-persona@eureka.service " , " active \n " ) , ( " mind-stream@eureka.service " , " failed \n " ) ] ) ;
2026-11-01 19:11:21 +01:00
let shown = format! ( " {:#} " , launcher . stop ( StopScope ::Mind ( & eureka ( ) ) ) . unwrap_err ( ) ) ;
2026-10-22 17:37:08 +02:00
assert! ( shown . contains ( " exit status 5 " ) , " {shown} " ) ;
2026-11-11 11:37:34 +00:00
assert! ( shown . contains ( " mind-self@eureka.service is inactive " ) & & shown . contains ( " mind-persona@eureka.service is active " ) & & shown . contains ( " mind-stream@eureka.service is failed " ) , " {shown} " ) ;
2026-10-22 17:37:08 +02:00
let asked : Vec < Vec < String > > = spy . calls . borrow ( ) . iter ( ) . skip ( 1 ) . cloned ( ) . collect ( ) ;
2026-11-11 11:37:34 +00:00
assert_eq! ( asked , [ [ " is-active " , " mind-self@eureka.service " ] , [ " is-active " , " mind-persona@eureka.service " ] , [ " is-active " , " mind-stream@eureka.service " ] ] , " one query per unit, after the one stop " ) ;
2026-10-22 17:37:08 +02:00
// A state word outside the three, and anything systemctl prints with it, is not carried.
spy . states . borrow_mut ( ) . clear ( ) ;
* spy . stdout . borrow_mut ( ) = " canary-output-7f3a91 \n " . to_string ( ) ;
2026-10-12 03:48:49 +02:00
spy . code . set ( None ) ;
2026-11-01 19:11:21 +01:00
let shown = format! ( " {:#} " , launcher . stop ( StopScope ::Mind ( & eureka ( ) ) ) . unwrap_err ( ) ) ;
2026-10-22 17:37:08 +02:00
assert! ( shown . contains ( " signal " ) & & shown . contains ( " is unknown " ) & & ! shown . contains ( " canary " ) , " {shown} " ) ;
spy . states . borrow_mut ( ) . push ( ( " mind-self@eureka.service " , " failed \n " ) ) ;
2026-11-01 19:11:21 +01:00
let shown = format! ( " {:#} " , launcher . stop ( StopScope ::Mind ( & eureka ( ) ) ) . unwrap_err ( ) ) ;
2026-10-22 17:37:08 +02:00
assert! ( shown . contains ( " mind-self@eureka.service is failed " ) , " {shown} " ) ;
2026-10-12 03:48:49 +02:00
}
2026-10-09 20:51:07 +02:00
#[ test ]
2026-10-10 06:54:29 +02:00
fn a_failed_start_names_no_instance ( ) {
2026-10-10 05:32:19 +02:00
let ( launcher , spy ) = spied ( ) ;
2026-10-09 20:51:07 +02:00
spy . success . set ( false ) ;
2026-10-10 06:54:29 +02:00
let error = launcher . start ( & Slug ( " canary-instance-7f3a91 " . into ( ) ) , RunTurn ::SelfRun ) . unwrap_err ( ) ;
2026-10-09 20:51:07 +02:00
let shown = format! ( " {error:#} " ) ;
assert! ( ! shown . contains ( " canary " ) , " {shown} " ) ;
}
2026-10-10 05:32:19 +02:00
#[ test ]
2026-10-10 05:38:22 +02:00
fn hostile_instances_make_start_fail_and_alive_false_with_no_systemctl_call ( ) {
2026-10-10 05:32:19 +02:00
let ( launcher , spy ) = spied ( ) ;
2026-10-10 05:38:22 +02:00
* spy . stdout . borrow_mut ( ) = " active
"
. into ( ) ;
for hostile in [
" " ,
" . " ,
" .. " ,
" a/b " ,
" a@b " ,
" a:b " ,
" a b " ,
" a
b " ,
" eureka/../x
" ,
" x
- - now " ,
" canary/9 " ,
] {
let instance = Slug ( hostile . into ( ) ) ;
2026-10-10 06:54:29 +02:00
let error = launcher . start ( & instance , RunTurn ::SelfRun ) . unwrap_err ( ) ;
2026-10-10 05:32:19 +02:00
assert! ( ! format! ( " {error:#} " ) . contains ( " canary " ) , " {error:#} " ) ;
2026-10-10 06:54:29 +02:00
assert! ( ! launcher . alive ( & instance , RunTurn ::PersonaTurn ) , " instance {hostile:?} " ) ;
2026-10-10 05:32:19 +02:00
}
assert! ( spy . calls . borrow ( ) . is_empty ( ) , " no hostile name reached systemctl: {:?} " , spy . calls . borrow ( ) ) ;
}
2026-10-10 05:38:22 +02:00
#[ test ]
fn a_unit_is_named_by_instance_and_turn_alone ( ) {
let ( launcher , spy ) = spied ( ) ;
2026-10-10 06:54:29 +02:00
launcher . start ( & eureka ( ) , RunTurn ::SelfRun ) . unwrap ( ) ;
launcher . start ( & eureka ( ) , RunTurn ::SelfRun ) . unwrap ( ) ;
2026-10-10 05:38:22 +02:00
let calls = spy . calls . borrow ( ) ;
assert_eq! ( calls [ 0 ] , calls [ 1 ] , " two runs of one turn share a unit name " ) ;
2026-10-10 06:54:29 +02:00
assert_eq! ( calls [ 0 ] [ 2 ] , " mind-self@eureka.service " ) ;
2026-10-10 05:38:22 +02:00
}
2026-10-09 20:51:07 +02:00
}