The on-box UI shipped as a half-migrated shell: the schema settings rendered with no submit form, the flare-ui nav tabs shadowed the legacy pages with empty cards, and --serve stood the UI up with no runtime so every value read "not identified" and nothing could be controlled. This makes it work end to end, driven by flare-ui, and fully exercisable on a workstation with no hashboard. Live backend (alkane-os --serve-sim): run the reference HAL in-process on a private unix socket, boot the runtime against it, and run a control loop that applies the operator intent every period and publishes a live snapshot. On this workstation the console shows the chain frequency climbing toward the target and the controls taking effect; --serve keeps serving the same console with no backend for a board whose HAL is not written yet. Operator control channel (control.rs): the one mutable path into the runtime - a mode/target and a run switch, plus a per-process CSRF token. ControlMode maps one-to-one onto the real TuningTarget variants (power / ambient / hashrate / max-fan), so the UI never offers a behaviour the controller lacks. runtime.rs / tuning.rs gain set_target/target so a mode change applies in place, keeping the PID state instead of snapping to the envelope floor. flare-ui console (console.rs): every nav tab rendered from flare-ui primitives (shell, render::card, readings, actions, components) with live data - overview hero + state, mining hashboards/power/fans, a Control page with Start/Stop action buttons, a working Tuning form that posts to /settings, and the system/ security/logs/files/terminal pages wired to their existing data. The tuning form submit persists the setting AND switches the live target; Start/Stop and the form are CSRF-checked. /api/control exposes the operator state as JSON, the interface of record behind the page. Wiring: app::start and web::serve_listener/handle/route carry Arc<Control>; main.rs gains --serve-sim and threads a Control through the existing serve/boot paths. README updated: the "no web frontend" note is no longer true. Verified on this workstation: --serve-sim boots (s19jpro-bm1362-pic, 3 chains), frequency converges toward power:2400, and the flare-ui pages drive stop -> switch to ambient:18000 -> start, with a stale CSRF token rejected (403). Tests: 183 lib + integration green; clippy -D warnings clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdKQBPjAXpCwmigUov5eQk
330 lines
10 KiB
Rust
330 lines
10 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
//! Mining runtime: one state machine over the detection plan, the fault ladder and the tuner.
|
|
//!
|
|
//! Each tick takes a measurement, lets the tuner move the operating point, and lets the fault
|
|
//! supervisor veto the machine. The pieces are tested in isolation elsewhere; this module is the
|
|
//! place where their order is decided, and the order matters: a stopped machine must not be tuned,
|
|
//! and a disabled chain must not be dispatched to.
|
|
|
|
use crate::detect::{HardwarePlan, OperatingPoint};
|
|
use crate::fault::{FaultAction, FaultEvent, FaultPolicy, FaultSupervisor, StopReason};
|
|
use crate::tuning::{Command, Observation, PidConfig, TuningError, TuningLoop, TuningTarget};
|
|
|
|
/// One control period's measurements.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Tick {
|
|
pub power_w: Option<f32>,
|
|
pub hashrate_ths: Option<f32>,
|
|
pub hottest_mc: Option<i32>,
|
|
pub ambient_mc: Option<i32>,
|
|
pub dt_s: f32,
|
|
}
|
|
|
|
/// What the runtime decided for this period.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum TickOutcome {
|
|
/// Apply this command to the hardware.
|
|
Run(Command),
|
|
/// Measurement missing; hold the previous operating point.
|
|
Hold(Command),
|
|
/// The machine is stopped and must not hash.
|
|
Stopped(StopReason),
|
|
}
|
|
|
|
/// Runtime state for one machine.
|
|
#[derive(Clone, Debug)]
|
|
pub struct MinerRuntime {
|
|
plan: HardwarePlan,
|
|
faults: FaultSupervisor,
|
|
tuning: TuningLoop,
|
|
last_command: Command,
|
|
}
|
|
|
|
impl MinerRuntime {
|
|
pub fn new(
|
|
plan: HardwarePlan,
|
|
policy: FaultPolicy,
|
|
target: TuningTarget,
|
|
pid: PidConfig,
|
|
ceiling_mc: i32,
|
|
) -> Self {
|
|
let chains: Vec<u8> = plan.chains.iter().map(|chain| chain.index).collect();
|
|
let last_command = Command {
|
|
frequency_mhz: plan.operating_point.frequency_mhz,
|
|
fan_pwm: None,
|
|
};
|
|
Self {
|
|
faults: FaultSupervisor::new(&chains, policy),
|
|
tuning: TuningLoop::new(target, plan.envelope, pid, ceiling_mc),
|
|
plan,
|
|
last_command,
|
|
}
|
|
}
|
|
|
|
pub fn plan(&self) -> &HardwarePlan {
|
|
&self.plan
|
|
}
|
|
|
|
pub fn operating_point(&self) -> OperatingPoint {
|
|
OperatingPoint {
|
|
voltage_mv: self
|
|
.plan
|
|
.envelope
|
|
.voltage_for(self.last_command.frequency_mhz),
|
|
frequency_mhz: self.last_command.frequency_mhz,
|
|
confirmed: self.plan.operating_point.confirmed,
|
|
}
|
|
}
|
|
|
|
pub fn active_chains(&self) -> Vec<u8> {
|
|
self.faults.active_chains()
|
|
}
|
|
|
|
/// The operator target currently in force.
|
|
pub fn target(&self) -> TuningTarget {
|
|
self.tuning.target()
|
|
}
|
|
|
|
/// Switch the operator target (one of the MPC modes) without a restart. The
|
|
/// PID state carries over, so the operating point moves from where it is
|
|
/// rather than snapping back to the envelope floor.
|
|
pub fn set_target(&mut self, target: TuningTarget) {
|
|
self.tuning.set_target(target);
|
|
}
|
|
|
|
pub fn stop_reason(&self) -> Option<StopReason> {
|
|
self.faults.stop_reason()
|
|
}
|
|
|
|
/// Feed one health observation; returns what the supervisor decided.
|
|
pub fn on_event(&mut self, event: FaultEvent) -> FaultAction {
|
|
self.faults.observe(event)
|
|
}
|
|
|
|
/// Advance one control period.
|
|
pub fn tick(&mut self, tick: &Tick) -> TickOutcome {
|
|
if let Some(reason) = self.faults.stop_reason() {
|
|
return TickOutcome::Stopped(reason);
|
|
}
|
|
let observation = Observation {
|
|
power_w: tick.power_w,
|
|
hashrate_ths: tick.hashrate_ths,
|
|
hottest_mc: tick.hottest_mc,
|
|
ambient_mc: tick.ambient_mc,
|
|
};
|
|
match self.tuning.step(observation, tick.dt_s) {
|
|
Ok(command) => {
|
|
self.last_command = command;
|
|
TickOutcome::Run(command)
|
|
}
|
|
Err(TuningError::NoMeasurement) => TickOutcome::Hold(self.last_command),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::detect::{ChainReport, ChipModel, EepromState, Envelope, FanZone};
|
|
|
|
fn plan() -> HardwarePlan {
|
|
HardwarePlan {
|
|
profile: "s19jpro-bm1362-pic".into(),
|
|
chip: ChipModel::Bm1362,
|
|
chains: (0..3)
|
|
.map(|index| ChainReport {
|
|
index,
|
|
chip: ChipModel::Bm1362,
|
|
chips_found: 126,
|
|
eeprom: EepromState::Absent,
|
|
response_per_mille: Some(950),
|
|
})
|
|
.collect(),
|
|
envelope: Envelope {
|
|
v_min_mv: 1100,
|
|
v_max_mv: 1500,
|
|
f_min_mhz: 300,
|
|
f_max_mhz: 700,
|
|
},
|
|
operating_point: OperatingPoint {
|
|
voltage_mv: 1200,
|
|
frequency_mhz: 300,
|
|
confirmed: false,
|
|
},
|
|
psu: "APW12".into(),
|
|
fan_zones: vec![FanZone {
|
|
fan: 0,
|
|
zones: vec![0],
|
|
}],
|
|
}
|
|
}
|
|
|
|
fn runtime() -> MinerRuntime {
|
|
MinerRuntime::new(
|
|
plan(),
|
|
FaultPolicy::default(),
|
|
TuningTarget::PowerW(2_400.0),
|
|
PidConfig {
|
|
kp: 0.2,
|
|
ki: 0.05,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 0.0,
|
|
max_step_per_s: 50.0,
|
|
deadband: 0.0,
|
|
},
|
|
95_000,
|
|
)
|
|
}
|
|
|
|
fn tick(power: f32) -> Tick {
|
|
Tick {
|
|
power_w: Some(power),
|
|
hashrate_ths: None,
|
|
hottest_mc: Some(70_000),
|
|
ambient_mc: None,
|
|
dt_s: 1.0,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_fresh_runtime_exposes_every_chain() {
|
|
assert_eq!(runtime().active_chains(), vec![0, 1, 2]);
|
|
assert_eq!(runtime().stop_reason(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn ticks_move_frequency_inside_the_envelope() {
|
|
let mut runtime = runtime();
|
|
let mut frequency = 300;
|
|
for _ in 0..60 {
|
|
let power = 4.0 * f32::from(frequency);
|
|
if let TickOutcome::Run(command) = runtime.tick(&tick(power)) {
|
|
frequency = command.frequency_mhz;
|
|
assert!((300..=700).contains(&frequency));
|
|
}
|
|
}
|
|
assert!(frequency > 300, "frequency never moved: {frequency}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_missing_measurement_holds_the_last_command() {
|
|
let mut runtime = runtime();
|
|
let empty = Tick {
|
|
power_w: None,
|
|
hashrate_ths: None,
|
|
hottest_mc: None,
|
|
ambient_mc: None,
|
|
dt_s: 1.0,
|
|
};
|
|
match runtime.tick(&empty) {
|
|
TickOutcome::Hold(command) => assert_eq!(command.frequency_mhz, 300),
|
|
other => panic!("unexpected outcome: {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_dead_chain_is_removed_from_dispatch_without_stopping_the_machine() {
|
|
let mut runtime = runtime();
|
|
assert_eq!(
|
|
runtime.on_event(FaultEvent::ChainError { chain: 1 }),
|
|
FaultAction::Retry { chain: 1 }
|
|
);
|
|
assert_eq!(
|
|
runtime.on_event(FaultEvent::ChainError { chain: 1 }),
|
|
FaultAction::Retry { chain: 1 }
|
|
);
|
|
assert_eq!(
|
|
runtime.on_event(FaultEvent::ChainError { chain: 1 }),
|
|
FaultAction::DisableChain { chain: 1 }
|
|
);
|
|
assert_eq!(runtime.active_chains(), vec![0, 2]);
|
|
assert!(matches!(runtime.tick(&tick(2_000.0)), TickOutcome::Run(_)));
|
|
}
|
|
|
|
#[test]
|
|
fn a_temperature_fault_stops_the_machine_for_good() {
|
|
let mut runtime = runtime();
|
|
assert_eq!(
|
|
runtime.on_event(FaultEvent::TemperatureFault { zone: 1 }),
|
|
FaultAction::Stop {
|
|
reason: StopReason::Temperature
|
|
}
|
|
);
|
|
assert_eq!(
|
|
runtime.tick(&tick(2_400.0)),
|
|
TickOutcome::Stopped(StopReason::Temperature)
|
|
);
|
|
assert_eq!(
|
|
runtime.tick(&tick(2_400.0)),
|
|
TickOutcome::Stopped(StopReason::Temperature)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn losing_every_chain_stops_the_machine() {
|
|
let mut runtime = MinerRuntime::new(
|
|
plan(),
|
|
FaultPolicy {
|
|
retries: 0,
|
|
..FaultPolicy::default()
|
|
},
|
|
TuningTarget::PowerW(2_400.0),
|
|
PidConfig::default(),
|
|
95_000,
|
|
);
|
|
for chain in 0..3 {
|
|
runtime.on_event(FaultEvent::ChainError { chain });
|
|
}
|
|
assert_eq!(runtime.stop_reason(), Some(StopReason::NoChainsLeft));
|
|
assert!(runtime.active_chains().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn switching_target_keeps_the_operating_point_and_takes_effect() {
|
|
let mut runtime = runtime();
|
|
// Climb under a power target so the frequency leaves the floor.
|
|
for _ in 0..30 {
|
|
let power = 4.0 * f32::from(runtime.operating_point().frequency_mhz);
|
|
runtime.tick(&tick(power));
|
|
}
|
|
let climbed = runtime.operating_point().frequency_mhz;
|
|
assert!(climbed > 300, "frequency never left the floor: {climbed}");
|
|
assert!(matches!(runtime.target(), TuningTarget::PowerW(_)));
|
|
// Switch to an ambient target in place: the target changes and the
|
|
// operating point carries over (no snap back to the floor).
|
|
runtime.set_target(TuningTarget::AmbientC { margin_mc: 20_000 });
|
|
assert!(matches!(
|
|
runtime.target(),
|
|
TuningTarget::AmbientC { margin_mc: 20_000 }
|
|
));
|
|
assert_eq!(runtime.operating_point().frequency_mhz, climbed);
|
|
// The ambient target now drives: feed ambient + hottest and it still runs.
|
|
let outcome = runtime.tick(&Tick {
|
|
power_w: None,
|
|
hashrate_ths: None,
|
|
hottest_mc: Some(60_000),
|
|
ambient_mc: Some(40_000),
|
|
dt_s: 1.0,
|
|
});
|
|
assert!(matches!(outcome, TickOutcome::Run(_)));
|
|
}
|
|
|
|
#[test]
|
|
fn the_operating_point_reflects_the_last_command() {
|
|
let mut runtime = runtime();
|
|
for _ in 0..10 {
|
|
runtime.tick(&tick(1_500.0));
|
|
}
|
|
let point = runtime.operating_point();
|
|
assert!(point.frequency_mhz > 300);
|
|
assert!(!point.confirmed);
|
|
// Voltage follows the frequency across the envelope instead of staying at the boot value.
|
|
assert_eq!(
|
|
point.voltage_mv,
|
|
runtime.plan().envelope.voltage_for(point.frequency_mhz)
|
|
);
|
|
assert!(point.voltage_mv > 1100);
|
|
}
|
|
}
|