Documentation menu

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 fuel budget 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 explicit every >= <duration> cadence alongside periodic on 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 W or 60s > 200ms normalise deterministically at compile time.
  • Dimensional mismatch rejection: Adding or comparing incompatible dimensions (such as 0.5 bar + 60s or comparing degC against kW) fails static analysis.
  • Least-privilege capabilities: Scripts must declare requires capability::ReadTelemetry to read live telemetry, requires capability::ShadowSimulate to run shadow verification, and requires 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");
            }
        }
    }
}

Built-in namespaces and compiler diagnostics

Symbol / DiagnosticSignature / TriggerBehaviour
twin::point<"key">.read()-> Quantity | f64Reads the current point value from deterministic runtime state. Requires capability::ReadTelemetry.
guardrail::plan_mutation(...)(target, value, reason) -> plan_idStages an auditable write intent without touching field equipment.
twin::shadow_qualify(...)(plan_id, horizon) -> boolSimulates 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.
TWS001Unknown point keyCompile error when twin::point<"..."> is absent from the facility catalogue.
TWS002UCUM unit mismatchCompile error when arithmetic or comparison mixes incompatible physical dimensions.
TWS003Unguarded writeCompile and runtime block if guardrail::apply is invoked without prior mutation planning and scoped write capability.
TWS004Unbounded while loopCompile error rejecting unbounded while loops; handlers must use bounded for ... in 0..N iteration and explicit fuel budgets.
TWS005Forbidden ambient authorityCompile 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 --strict