lawsynth/services/worker/tests/stability_test.rs
caelum0x e3068ed15c feat: MPC + forecast uncertainty + CLI analysis commands + platform analysis job
Batch 9 of expansion-v2. Makes the analysis layer user-reachable, adds two
model-use capabilities, and threads an analysis job through the hosted platform.
Deterministic/offline/std-only.

crates/lawsynth-mpc (new): successive-linearization LQR-MPC. Relinearizes each
step (analytic A=∂f/∂x, B=∂f/∂u), solves a local LQR (lawsynth-feedback),
applies the first clamped move, advances the nonlinear plant by RK4. Regulates a
forced pendulum to <1e-3, respects saturation, stabilizes a Van der Pol limit
cycle; per-step gain bit-identical to direct LQR on a linear plant. Honest that
it's LQR-MPC, not constrained QP-MPC. 18 tests.

crates/lawsynth-propagate (new): coefficient→forecast uncertainty. Delta method
Cov(x(t))≈S·Cov(θ)·Sᵀ via forward sensitivities, plus seeded Monte-Carlo over the
bootstrap ensemble. Delta matches the (t·x0·e^-θt)²·s² closed form to <1e-9;
delta≈MC at small σ and honestly diverges (asymmetric MC band) at large σ.
covariance_from_ensemble closes the bootstrap→bands pipeline. 34 tests.

crates/lawsynth-cli: new analysis subcommands bifurcation/sensitivity/estimate/
reduce (shared linearize-at-fixed-point helper derives A/C/B from a world), with
honest guardrails (bifurcation needs a parameterized law; reduce needs a Hurwitz
point; estimate pole placement is single-output). 115 lib + 7 integration tests.

services/worker + scheduler: Job::AnalyzeStability + JobOutput::Stability threaded
through every worker match site (job/execute/artifacts/errors/http_error) and
proven first-class in the scheduler via an end-to-end submit→lease→execute test.
Gateway HTTP→Job decode noted as an honest follow-up. Worker 64 + scheduler tests.

specs/{model-predictive-control,forecast-uncertainty}: boundary specs.
2026-08-18 16:58:56 +03:00

66 lines
2.6 KiB
Rust

//! End-to-end worker dispatch of a stability-analysis job.
//!
//! Mirrors `execute_test.rs`, but drives the real `lawsynth-stability` engine
//! through the public [`Worker::execute_at`] path: a stable-node world is
//! analysed over a search box and the worker must return a
//! [`JobOutput::Stability`] with the located fixed point classified as a stable
//! node, proving the new variant flows through admission, execution, and the
//! artifact handoff.
use lawsynth_core::Identifier;
use lawsynth_expr::{Expr, UnaryOperator};
use lawsynth_runner::{CancellationToken, ResourceRequest};
use lawsynth_stability::{Classification, StabilityConfig};
use lawsynth_store::{LocalStore, StoreConfig};
use lawsynth_worker::{Job, JobEnvelope, JobOutput, Worker, WorkerConfig};
use lawsynth_world::{ContinuousLaw, Variable, VariableRole, World};
fn id(value: &str) -> Identifier {
Identifier::new(value).unwrap()
}
/// A linear stable node at the origin: `x' = -x`, `y' = -2y`.
fn stable_node_world() -> World {
World::new(
[Variable::new(id("x"), VariableRole::State), Variable::new(id("y"), VariableRole::State)],
[],
[
ContinuousLaw::new(id("x"), Expr::unary(UnaryOperator::Negate, Expr::symbol(id("x")))),
ContinuousLaw::new(id("y"), Expr::product(Expr::constant(-2.0), Expr::symbol(id("y")))),
],
)
.unwrap()
}
#[test]
fn executes_real_stability_analysis_and_classifies_the_origin() {
let root =
std::env::temp_dir().join(format!("lawsynth-worker-stability-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let envelope = JobEnvelope::new(
"stability-1",
1,
1,
10_000,
ResourceRequest::new(250, 1 << 20, 1 << 20).unwrap(),
Job::AnalyzeStability {
world: stable_node_world(),
config: StabilityConfig::new(vec![(-1.0, 1.0), (-1.0, 1.0)]),
},
)
.unwrap();
assert_eq!(envelope.work.kind, "analyze-stability");
let worker = Worker::new(
WorkerConfig::new(ResourceRequest::new(1_000, 2 << 20, 2 << 20).unwrap(), 1024).unwrap(),
LocalStore::open(&root, StoreConfig::default()).unwrap(),
)
.unwrap();
let output = worker.execute_at(&envelope, &CancellationToken::default(), 2).unwrap();
let JobOutput::Stability(report) = output else {
panic!("worker dispatched stability analysis to the wrong executor")
};
assert_eq!(report.fixed_points.len(), 1);
assert_eq!(report.fixed_points[0].classification, Classification::StableNode);
let _ = std::fs::remove_dir_all(root);
}