TwinScript Language
TwinScript (.tws) is TwinControl's statically typed, deterministic automation and test scripting language. Every script executes inside a bounded register virtual machine with compile-time physical unit verification, explicit capability declarations, and mandatory shadow-plant qualification before hardware actuation.
Language structure and safety invariants
A TwinScript module starts with a script block declaring its required capabilities, helper functions (fn), and event handlers (on change or on tick). Control flow uses let bindings, if / else branches, bounded for ... in range loops, and explicit return statements.
- Guaranteed termination: Every handler declares an explicit instruction
fuelbudget and uses bounded range iteration—unbounded loops are rejected at compile time so a script cannot stall a scan cycle. - Debounce and rate limits: Event-driven
on change(...)handlers enforce an explicitevery >= <duration>cadence alongside periodicon tick(<duration>)loops. - Point-key verification: All
twin::point<"...">references are resolved statically against the active project point catalogue before execution.
UCUM physical units and capability declarations
Physical quantities carry compile-time Unified Code for Units of Measure (UCUM) dimensions—including temperature (degC), power (kW, W), pressure (bar), frequency (Hz), volumetric flow (L/s, m3/h), distance (m), and duration (ms, s).
- Automatic compatible normalisation: Expressions such as
1.5 kW + 500 Wor60s > 200msnormalise deterministically at compile time. - Dimensional mismatch rejection: Adding or comparing incompatible dimensions (such as
0.5 bar + 60sor comparingdegCagainstkW) fails static analysis. - Least-privilege capabilities: Scripts must declare
requires capability::ReadTelemetryto read live telemetry,requires capability::ShadowSimulateto run shadow verification, andrequires capability::ProposeWrite(scope: "...")to stage guarded writes within an explicit point prefix.
Runnable domain examples
Select a sector below to inspect complete TwinScript handlers across Data Centres, Power Stations, and Nuclear plants. Each example reads live state, stages a guarded mutation plan, qualifies the transition over a shadow-plant horizon, and applies the change only if physical invariants hold.
script ChillerAndCduTrim {
requires capability::ReadTelemetry;
requires capability::ShadowSimulate;
requires capability::ProposeWrite(scope: "cdu-01.*");
fn compute_target(base_temp: Quantity<degC>, delta: Quantity<degC>) -> Quantity<degC> {
let adjusted = base_temp - delta;
return adjusted;
}
on change(twin::point<"cdu-01.supply_temp">) every >= 200ms (fuel: 200) {
let supply = twin::point<"cdu-01.supply_temp">.read();
let flow = twin::point<"cdu-01.coolant_flow">.read();
let limit = 28.5 degC;
if supply > limit && flow >= 3.0 L/s {
let next_sp = compute_target(28.5 degC, 1.5 degC);
let plan = guardrail::plan_mutation(
"cdu-01.valve_setpoint",
next_sp,
"AI step-load cooling trim",
);
if twin::shadow_qualify(plan, 60s) {
guardrail::apply(plan);
twin::event::emit("cdu.trim", "applied");
}
}
}
}script GridDroopDispatch {
requires capability::ReadTelemetry;
requires capability::ShadowSimulate;
requires capability::ProposeWrite(scope: "gtg-01.*");
on change(twin::point<"bus-400kv.frequency">) every >= 100ms (fuel: 150) {
let freq = twin::point<"bus-400kv.frequency">.read();
let load = twin::point<"gtg-01.active_power">.read();
if freq < 49.85 Hz && load < 180000.0 kW {
let target_kw = load + 12500.0 kW;
let plan = guardrail::plan_mutation(
"gtg-01.gov_setpoint",
target_kw,
"under-frequency primary reserve response",
);
if twin::shadow_qualify(plan, 30s) {
guardrail::apply(plan);
twin::event::emit("governor.droop", "dispatched");
}
}
}
on tick(1s) (fuel: 80) {
let bus_kv = twin::point<"bus-400kv.voltage">.read();
twin::event::emit("substation.bus_monitor", bus_kv);
}
}script PressuriserSpraySurveillance {
requires capability::ReadTelemetry;
requires capability::ShadowSimulate;
requires capability::ProposeWrite(scope: "pzr-01.*");
on change(twin::point<"pzr-01.pressure">) every >= 200ms (fuel: 200) {
let p_bar = twin::point<"pzr-01.pressure">.read();
let level_m = twin::point<"pzr-01.level">.read();
if p_bar > 156.5 bar && level_m > 4.2 m {
let plan = guardrail::plan_mutation(
"pzr-01.pressure_setpoint",
155.0 bar,
"primary pressure setpoint qualification",
);
if twin::shadow_qualify(plan, 120s) {
guardrail::apply(plan);
twin::event::emit("pzr.spray", "qualified_and_applied");
} else {
twin::event::emit("pzr.spray", "blocked_by_shadow");
}
}
}
on tick(5s) (fuel: 100) {
for bank in 0..3 {
twin::event::emit("pzr.heater_surveillance", bank);
}
}
}Built-in namespaces and compiler diagnostics
| Symbol / Diagnostic | Signature / Trigger | Behaviour |
|---|---|---|
twin::point<"key">.read() | -> Quantity | f64 | Reads the current point value from deterministic runtime state. Requires capability::ReadTelemetry. |
guardrail::plan_mutation(...) | (target, value, reason) -> plan_id | Stages an auditable write intent without touching field equipment. |
twin::shadow_qualify(...) | (plan_id, horizon) -> bool | Simulates the staged mutation against the live shadow twin over the given duration and returns pass/fail qualification. |
guardrail::apply(plan_id) | (plan_id) -> () | Commits a qualified plan through the fail-closed guardrail boundary. Requires capability::ProposeWrite. |
TWS001 | Unknown point key | Compile error when twin::point<"..."> is absent from the facility catalogue. |
TWS002 | UCUM unit mismatch | Compile error when arithmetic or comparison mixes incompatible physical dimensions. |
TWS003 | Unguarded write | Compile and runtime block if guardrail::apply is invoked without prior mutation planning and scoped write capability. |
TWS004 | Unbounded while loop | Compile error rejecting unbounded while loops; handlers must use bounded for ... in 0..N iteration and explicit fuel budgets. |
TWS005 | Forbidden ambient authority | Compile error rejecting unsafe, std::fs, or std::net so scripts cannot bypass the deterministic register VM. |
Headless execution and CI integration
The twinctl CLI executes TwinScript commissioning suites and fault scenarios headlessly at 100x+ stepped virtual fast-forward with bit-exact reproducibility, allowing engineering teams to gate pull requests on full-facility L1–L5 checks.
# Run deterministic 100x+ stepped virtual commissioning suite
twinctl control warp-sim --seconds 600 --open-breaker mvb-01 --json
# Execute Integrated Systems Test (IST) procedures against the twin
twinctl commission ist dc-east-01 --run --json
# Verify facility specifications and scenario packs strictly in CI
twinctl check --config ./facility.toml --strictname: twinscript-ist-gate
on: [pull_request]
jobs:
qualify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Validate facility packs and TwinScript invariants
run: twinctl check --config ./facility.toml --strict
- name: Run 100x+ virtual commissioning scenarios
run: twinctl commission ist dc-east-01 --run --json > ist-verdict.json