Conditional integration takes the caller's single saturation report (the last beat was the floor, or launched nothing because every eligible Mind was busy) instead of estimating the high limit from its own output; the slow limit is judged on the integral's own answer, so the vote carry no longer masks it. A call after a long silence integrates MAX_STEP_MS, not the outage. The sim gains the swarm's throughput ceiling: per-Mind busy time from the measured turn duration, a beat that finds every Mind busy launches nothing, and the controller is told; scenarios cheap-then-x3 and one-slow-mind exercise it; the band-crossing bound is per scenario. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
298 lines
12 KiB
Rust
298 lines
12 KiB
Rust
//! The tempo rule of the shared heartbeat, pure: no clock, no mind, no I/O
|
|
//! (rulings `eureka-body:ruling:shared-heartbeat-joint-lever`,
|
|
//! `self-declared-urgency-etiquette`, `swarm-shares-log-bound`).
|
|
//!
|
|
//! The Minds jointly hold the lever to one heartbeat. `tally` turns their votes
|
|
//! into the beat; `pick` chooses who takes the turn when a beat falls. The
|
|
//! heartbeat owns when a beat falls and what it launches; this module owns only
|
|
//! the decisions: the swarm's tempo, who takes the turn, and the third, the
|
|
//! beat when no vote is cast (`fallback_beat`, rulings
|
|
//! `tempo-default-is-usage-fitter`, `tempo-fallback-pid-tuned-by-sim`).
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
/// The beat, in seconds, when no vote is cast.
|
|
pub const DEFAULT_BEAT_S: u32 = 900;
|
|
|
|
/// The shortest and longest beat the fallback controller may answer, in seconds.
|
|
pub const FALLBACK_MIN_S: u32 = 60;
|
|
pub const FALLBACK_MAX_S: u32 = 3600;
|
|
|
|
const WINDOW_MS: i64 = 7 * 24 * 3_600_000;
|
|
const HOUR_MS: f64 = 3_600_000.0;
|
|
/// A reading observed longer ago than this is no evidence of where usage is now:
|
|
/// twice the longest beat, since every turn refreshes it.
|
|
const STALE_AFTER_MS: i64 = 2 * FALLBACK_MAX_S as i64 * 1000;
|
|
/// Two `resets_at` values this close name one window: the connector reports
|
|
/// whole unix seconds, and a new window starts a week on.
|
|
const RESETS_JITTER_MS: u64 = 60_000;
|
|
/// How fast, in hours, the controller takes the wheel back from a released
|
|
/// vote: the sensor's own poll period, since the vote's effect cannot show in a
|
|
/// reading sooner.
|
|
const CARRY_FADE_H: f64 = 0.25;
|
|
/// The longest stretch of time one call may integrate: the longest beat, the
|
|
/// longest wait between two calls of a running heartbeat. A longer silence (a
|
|
/// restart, an outage) is not integrated as if the swarm had run through it.
|
|
const MAX_STEP_MS: i64 = FALLBACK_MAX_S as i64 * 1000;
|
|
/// The turn rate, in beats per hour, that `DEFAULT_BEAT_S` stands for: the
|
|
/// controller's bias.
|
|
const BIAS_PER_H: f64 = 3600.0 / DEFAULT_BEAT_S as f64;
|
|
|
|
/// The seven-day plan window as the connector last read it.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct Reading {
|
|
pub used_permille: u32,
|
|
pub observed_at_ms: i64,
|
|
pub resets_at_ms: i64,
|
|
}
|
|
|
|
/// PID gains. The actuator is the turn rate in beats per hour and the error is
|
|
/// in permille of the plan, so `kp` is beats/h per permille, `ki` beats/h per
|
|
/// permille-hour and `kd` beats/h per (permille/h).
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Gains {
|
|
pub kp: f64,
|
|
pub ki: f64,
|
|
pub kd: f64,
|
|
}
|
|
|
|
/// Chosen by the committed simulation (`docs/research/2026-11-01-tempo-pid-sim.md`),
|
|
/// never by a run-time search.
|
|
pub const GAINS: Gains = Gains {
|
|
kp: 0.02,
|
|
ki: 0.01,
|
|
kd: 0.0,
|
|
};
|
|
|
|
/// The fallback controller's memory. The heartbeat owns persisting it; it is
|
|
/// never published as truth. `rate_per_h` is the last output as a turn rate
|
|
/// (zero: none yet); while a vote decides the caller sets it to the voted beat's
|
|
/// rate, and the controller turns the gap between that and its own answer into
|
|
/// `carry_per_h`, which fades after release, over the sensor's poll period
|
|
/// whatever the vote's length.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct Fallback {
|
|
pub integral: f64,
|
|
pub last_used_permille: u32,
|
|
pub last_observed_at_ms: i64,
|
|
pub last_now_ms: i64,
|
|
pub carry_per_h: f64,
|
|
pub window_resets_at_ms: i64,
|
|
pub rate_per_h: f64,
|
|
}
|
|
|
|
/// The beat when no vote is cast: a PID on the gap between the seven-day usage
|
|
/// trajectory to `target_permille` and what was used, acting on the turn rate
|
|
/// (behind the trajectory raises it). Derivative on the measurement, only
|
|
/// between distinct observations; conditional integration: it does not
|
|
/// integrate error in the direction of a saturated actuator. `saturated` is the
|
|
/// caller's one report that a shorter beat cannot buy more spend: the last beat
|
|
/// was the floor, or launched nothing because every eligible Mind was busy
|
|
/// (ruling `tempo-controller-knows-the-ceiling`); the slow limit needs no report,
|
|
/// since the integral's own answer at it is the controller's. One call
|
|
/// integrates at most `MAX_STEP_MS` of time.
|
|
/// While `voted` nothing integrates and the controller notes how far the voted
|
|
/// beat (held inside the beat bounds) is from its own answer, so release starts
|
|
/// at the voted beat, to within the one integration step of the first free call,
|
|
/// and the controller takes the wheel back within a poll period or two. The
|
|
/// carry fades on that period whatever the vote's length, so a vote of a minute
|
|
/// costs the extra turns of a poll period or two of fast beat. A new window clears the integral and the derivative memory;
|
|
/// with no usable reading (none, an expired window, or one observed more than
|
|
/// `STALE_AFTER_MS` ago, which a connector replaying its last answer keeps
|
|
/// producing) the last beat holds (`DEFAULT_BEAT_S` before any).
|
|
pub fn fallback_beat(
|
|
state: &mut Fallback,
|
|
reading: Option<&Reading>,
|
|
now_ms: i64,
|
|
target_permille: u32,
|
|
voted: bool,
|
|
saturated: bool,
|
|
) -> u32 {
|
|
control(
|
|
&GAINS,
|
|
state,
|
|
reading,
|
|
now_ms,
|
|
target_permille,
|
|
voted,
|
|
saturated,
|
|
)
|
|
}
|
|
|
|
fn beat_of(rate_per_h: f64) -> u32 {
|
|
if rate_per_h > 0.0 {
|
|
((3600.0 / rate_per_h)
|
|
.round()
|
|
.clamp(f64::from(FALLBACK_MIN_S), f64::from(FALLBACK_MAX_S))) as u32
|
|
} else {
|
|
FALLBACK_MAX_S
|
|
}
|
|
}
|
|
|
|
fn held(state: &Fallback) -> u32 {
|
|
if state.rate_per_h == 0.0 {
|
|
DEFAULT_BEAT_S
|
|
} else {
|
|
beat_of(state.rate_per_h)
|
|
}
|
|
}
|
|
|
|
/// The one controller; `fallback_beat` is it with `GAINS`, the simulation is it
|
|
/// with candidates.
|
|
fn control(
|
|
gains: &Gains,
|
|
state: &mut Fallback,
|
|
reading: Option<&Reading>,
|
|
now_ms: i64,
|
|
target_permille: u32,
|
|
voted: bool,
|
|
saturated: bool,
|
|
) -> u32 {
|
|
let dt_h = if state.last_now_ms > 0 {
|
|
(now_ms - state.last_now_ms).clamp(0, MAX_STEP_MS) as f64 / HOUR_MS
|
|
} else {
|
|
0.0
|
|
};
|
|
state.last_now_ms = now_ms;
|
|
let Some(r) = reading.filter(|r| {
|
|
now_ms < r.resets_at_ms
|
|
&& r.observed_at_ms >= r.resets_at_ms - WINDOW_MS
|
|
&& now_ms - r.observed_at_ms <= STALE_AFTER_MS
|
|
}) else {
|
|
return held(state);
|
|
};
|
|
let rolled = r.resets_at_ms.abs_diff(state.window_resets_at_ms) > RESETS_JITTER_MS;
|
|
if rolled {
|
|
state.integral = 0.0;
|
|
state.last_used_permille = r.used_permille;
|
|
state.last_observed_at_ms = r.observed_at_ms;
|
|
}
|
|
state.window_resets_at_ms = r.resets_at_ms;
|
|
let elapsed = (now_ms - (r.resets_at_ms - WINDOW_MS)).clamp(0, WINDOW_MS) as f64;
|
|
let error =
|
|
f64::from(target_permille) * elapsed / WINDOW_MS as f64 - f64::from(r.used_permille);
|
|
let fresh = r.observed_at_ms > state.last_observed_at_ms;
|
|
let slope = if fresh {
|
|
(f64::from(r.used_permille) - f64::from(state.last_used_permille))
|
|
/ ((r.observed_at_ms - state.last_observed_at_ms) as f64 / HOUR_MS)
|
|
} else {
|
|
0.0
|
|
};
|
|
if fresh {
|
|
state.last_used_permille = r.used_permille;
|
|
state.last_observed_at_ms = r.observed_at_ms;
|
|
}
|
|
let rate =
|
|
|integral: f64| BIAS_PER_H + gains.kp * error + gains.ki * integral - gains.kd * slope;
|
|
let (low, high) = (
|
|
3600.0 / f64::from(FALLBACK_MAX_S),
|
|
3600.0 / f64::from(FALLBACK_MIN_S),
|
|
);
|
|
if voted {
|
|
// Nothing integrates. The carry is the gap between the voted rate (held
|
|
// inside the beat bounds, which are all the controller can answer) and
|
|
// what the controller would answer now, so release starts at the voted
|
|
// beat; it fades once the vote is gone.
|
|
state.carry_per_h = if state.rate_per_h > 0.0 {
|
|
state.rate_per_h.clamp(low, high) - rate(state.integral)
|
|
} else {
|
|
0.0
|
|
};
|
|
return held(state);
|
|
}
|
|
// The slow limit is judged on the integral's own answer: the carry is a
|
|
// transient that fades and must not decide what the integral absorbs.
|
|
let winding = (saturated && error > 0.0) || (rate(state.integral) <= low && error < 0.0);
|
|
if !winding && !rolled {
|
|
state.integral += error * dt_h;
|
|
}
|
|
let beat = beat_of(rate(state.integral) + state.carry_per_h);
|
|
state.rate_per_h = 3600.0 / f64::from(beat);
|
|
state.carry_per_h *= (-dt_h / CARRY_FADE_H).exp();
|
|
beat
|
|
}
|
|
|
|
/// How many orders of magnitude a full urgency may multiply a Mind's wait by.
|
|
/// Interim, per `swarm-shares-log-bound`, until the swarm's governance sets it.
|
|
pub const LOG_BOUND_ORDERS: f64 = 1.0;
|
|
|
|
/// The swarm's tempo (ruling `eureka-body:ruling:tempo-silence-is-abstention`):
|
|
/// the lower median of the votes actually cast, each a Mind's newest standing
|
|
/// vote in seconds per beat. Silence is abstention: a Mind with no standing vote
|
|
/// (never voted, or withdrew) or whose mind cannot be read is `None` and is not
|
|
/// counted, so the beat follows those who care to vote. With no vote cast it is
|
|
/// `DEFAULT_BEAT_S`. On one voter that voter decides; on two the lower of them.
|
|
pub fn tally(votes: &[Option<u32>]) -> u32 {
|
|
let mut cast: Vec<u32> = votes.iter().flatten().copied().collect();
|
|
cast.sort_unstable();
|
|
match cast.len() {
|
|
0 => DEFAULT_BEAT_S,
|
|
n => cast[(n - 1) / 2],
|
|
}
|
|
}
|
|
|
|
/// What the pick sees of one Mind.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Standing {
|
|
pub slug: String,
|
|
/// Her brake is released and she is not in a turn.
|
|
pub eligible: bool,
|
|
/// Seconds since her last Persona turn; none when she has never had one.
|
|
pub waited_s: Option<u64>,
|
|
/// When her last Persona turn began, in unix milliseconds; none when never.
|
|
pub last_turn_ms: Option<i64>,
|
|
/// The urgency she declared for herself and no turn has consumed, in thousandths.
|
|
pub self_permille: u16,
|
|
/// The urgency her subscriptions give her pending deliveries, in thousandths.
|
|
pub topic_permille: u16,
|
|
/// A member (not the operator) has a pending message that names or tags her.
|
|
pub addressed_by_member: bool,
|
|
}
|
|
|
|
/// The urgency in force for a Mind, in thousandths: the strongest of what she
|
|
/// declared, what her topics say and a member addressing her (which is 1000).
|
|
pub fn urgency_permille(standing: &Standing) -> u16 {
|
|
let addressed = if standing.addressed_by_member {
|
|
1000
|
|
} else {
|
|
0
|
|
};
|
|
standing
|
|
.self_permille
|
|
.max(standing.topic_permille)
|
|
.max(addressed)
|
|
.min(1000)
|
|
}
|
|
|
|
/// `(waited / beat) x 10^(LOG_BOUND_ORDERS x urgency / 1000)`. A Mind that has
|
|
/// never had a turn has waited forever: infinite, so she is first, and the tie
|
|
/// rule orders the never-acted among themselves.
|
|
pub fn score(standing: &Standing, beat_s: u32) -> f64 {
|
|
let waited = standing
|
|
.waited_s
|
|
.map_or(f64::INFINITY, |waited| waited as f64);
|
|
let exponent = LOG_BOUND_ORDERS * f64::from(urgency_permille(standing)) / 1000.0;
|
|
waited / f64::from(beat_s.max(1)) * 10f64.powf(exponent)
|
|
}
|
|
|
|
/// The winner of a beat, as an index into `rows`: the highest score among the
|
|
/// eligible; a tie goes to the oldest last turn (never is oldest), then to the
|
|
/// lowest slug. None when nobody is eligible. A braked or busy Mind is never picked.
|
|
pub fn pick(rows: &[Standing], beat_s: u32) -> Option<usize> {
|
|
let order = |a: &(usize, &Standing), b: &(usize, &Standing)| -> Ordering {
|
|
score(b.1, beat_s)
|
|
.total_cmp(&score(a.1, beat_s))
|
|
.then_with(|| a.1.last_turn_ms.cmp(&b.1.last_turn_ms))
|
|
.then_with(|| a.1.slug.cmp(&b.1.slug))
|
|
};
|
|
rows.iter()
|
|
.enumerate()
|
|
.filter(|(_, row)| row.eligible)
|
|
.min_by(order)
|
|
.map(|(index, _)| index)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod sim;
|
|
#[cfg(test)]
|
|
mod tests;
|