TwinScript

A statically typed, unit-aware .tws scripting language for orchestrating facility tests, fault cascades, and guardrail-qualified control actions across one deterministic runtime.

Unit-safe automation with zero direct plant writes

TwinScript replaces fragile Python glue and untyped SCADA macros with compiled register bytecode bound directly to facility points and physical units. Every write exits through guardrail::plan_mutation, twin::shadow_qualify, and guardrail::apply.

  • degC
  • kW
  • bar
  • Hz
  • L/s & m3/h
  • ms / s
  • m
  • fuel-bounded
TwinControl Studio — TwinCode & TwinScript IDEstudio://twincode/twinscript
TwinControl Studio TwinCode editor in dark engineering theme displaying statically typed control logic, unit annotations, and live diagnostics.
Static unit checks, point bindings, and deterministic scan oscilloscope traces inside the TwinCode IDE.

Cross-domain scenario and protection scripts

Author reusable fn helpers, debounced on change event handlers, and fixed-period on tick watchdogs using identical syntax across data centres, power stations, and nuclear plants.

script AiClusterThermalRideThrough {
    requires capability::ReadTelemetry;
    requires capability::ShadowSimulate;
    requires capability::ProposeWrite(scope: "cdu-hall-a.*");

    fn target_cdu_flow(load_delta: Quantity<kW>, supply: Quantity<degC>) {
        let base_flow = 24.0 L/s;
        let trim = (load_delta / 1000.0 kW) * 3.1 L/s;
        if supply > 24.5 degC {
            return base_flow + trim + 2.0 L/s;
        }
        return base_flow + trim;
    }

    on change(twin::point<"pod-a.it_load">) every >= 100ms (fuel: 240) {
        let load_kw = twin::point<"pod-a.it_load">.read();
        let supply_temp = twin::point<"cdu-hall-a.supply_temp">.read();
        let step_delta = load_kw - 6400.0 kW;

        if step_delta > 1200.0 kW {
            let next_flow = target_cdu_flow(step_delta, supply_temp);
            let plan = guardrail::plan_mutation(
                "cdu-hall-a.flow_setpoint",
                next_flow,
                "synchronised AI cluster step-load pre-cool"
            );
            if twin::shadow_qualify(plan, 30s) {
                guardrail::apply(plan);
                twin::event::emit("dc.cdu.pre_cool", "qualified");
            }
        }
    }

    on tick(1s) (fuel: 80) {
        let dp = twin::point<"cdu-hall-a.manifold_dp">.read();
        if dp < 1.2 bar {
            twin::event::emit("dc.cdu.dp_low", "standby_pump_ready");
        }
    }
}

Compile-time verification before a scan executes

Static analysis runs continuously in Studio and headless CI so point references, physical dimensions, capability boundaries, and instruction fuel limits are proven before deployment.

Verification gateStatic invariantOperational guarantee
TWS001 · Asset & Point ResolutionEvery twin::point<"key"> binding resolves against the active facility graph and signed domain pack before bytecode emission.Missing or unmapped tags fail compilation immediately with an explicit missing-value error — never silently defaulted to zero.
TWS002 · Physical Unit & Dimension CheckDimensional arithmetic validates degC, kW, bar, Hz, L/s, m3/h, ms, s, and m across expressions, functions, and setpoints.Adding kW to degC or comparing bar against L/s fails at edit and CI time inside TwinCode Studio and twinctl check.
Capability Scope IsolationScripts declare requires capability::ReadTelemetry, capability::ShadowSimulate, and scoped capability::ProposeWrite(scope: "...").Unauthorised point writes outside the declared glob scope are rejected statically and at the guardrail boundary.
Deterministic Fuel & Debounce BoundsEvery on change and on tick handler declares an explicit cadence (every >= ...) and maximum VM instruction budget (fuel: N).Handlers terminate deterministically inside one lockstep scan with zero heap allocation and zero unbounded loops.

CI runner and LSP verification API

Run twinctl check --strict and twinctl control warp-sim in CI pipelines at 100x+ stepped virtual fast-forward with bit-exact reproducibility, or query the server-side LSP and guardrail endpoints directly from external automation tooling.

# Verify facility specs and scenario invariants in CI
twinctl check \
  --config ./facility.toml \
  --strict

# Run 100x+ stepped virtual sweep on a live twin fork
twinctl control warp-sim \
  --seconds 600 \
  --open-breaker mvb-01 \
  --json

# Execute Integrated Systems Test (IST) procedures
twinctl commission ist dc-east-01 \
  --run \
  --json
POST/api/v1/twincode/lsp/analyzeBearer token · IDE clearance

Analyse .tws source for syntax, TWS001–TWS005 diagnostics, UCUM units, and capability scopes.

ParameterInTypeDescription
language_id *bodystringLanguage identifier ("twinscript").
uri *bodystringDocument URI (for example "twin://scripts/cdu_trim.tws").
source *bodystringFull TwinScript (.tws) document text.
known_symbolsbodyobjectOptional known facility symbols object (point_keys, protocol_slugs, overlay_ids, device_skus, vplc_symbols, extra_symbols).

Request body

{
  "language_id": "twinscript",
  "uri": "twin://scripts/cdu_trim.tws",
  "source": "script ChillerTrim { requires capability::ReadTelemetry; ... }",
  "known_symbols": {}
}

Response

{
  "kind": "twinscript",
  "uri": "twin://scripts/cdu_trim.tws",
  "pack_id": "com.twinctl.twinscript",
  "language_id": "twinscript",
  "diagnostics": [],
  "symbols": [],
  "completions": []
}