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
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");
}
}
}script GridFaultRideThrough {
requires capability::ReadTelemetry;
requires capability::ShadowSimulate;
requires capability::ProposeWrite(scope: "bess-01.*");
fn droop_power(freq: Quantity<Hz>, nominal: Quantity<Hz>) -> Quantity<kW> {
let err = nominal - freq;
return (err / 0.5 Hz) * 25000.0 kW;
}
on change(twin::point<"gis-400kv.bus1_freq">) every >= 20ms (fuel: 200) {
let f_grid = twin::point<"gis-400kv.bus1_freq">.read();
let v_pu = twin::point<"gis-400kv.bus1_voltage_pu">.read();
if f_grid < 49.5 Hz || v_pu < 0.85 {
let support_kw = droop_power(f_grid, 50.0 Hz);
let plan = guardrail::plan_mutation(
"bess-01.active_power_cmd",
support_kw,
"NERC PRC-024 low-frequency ride-through injection"
);
if twin::shadow_qualify(plan, 15s) {
guardrail::apply(plan);
twin::event::emit("grid.frt.active", "bess_injected");
}
}
}
}script Class1eLoopSequencer {
requires capability::ReadTelemetry;
requires capability::ShadowSimulate;
requires capability::ProposeWrite(scope: "div1.edg.*");
on change(twin::point<"offsite.bus_voltage_pu">) every >= 50ms (fuel: 300) {
let offsite_v = twin::point<"offsite.bus_voltage_pu">.read();
let pzr_press = twin::point<"primary.pressuriser_pressure">.read();
let pzr_level = twin::point<"primary.pressuriser_level">.read();
let afw_flow = twin::point<"secondary.afw_flow">.read();
if offsite_v < 0.70 && pzr_press < 152.0 bar && pzr_level > 4.2 m {
let plan = guardrail::plan_mutation(
"div1.edg.auto_start_cmd",
1.0,
"LOOP detected: start Division I EDG and stage AFW load block"
);
if twin::shadow_qualify(plan, 120s) {
guardrail::apply(plan);
twin::event::emit("nuclear.loop.div1", afw_flow);
}
}
}
on tick(500ms) (fuel: 100) {
let hot_leg = twin::point<"primary.hot_leg_temp">.read();
if hot_leg > 325.0 degC {
twin::event::emit("nuclear.core.thermal_margin", "monitor");
}
}
}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 gate | Static invariant | Operational guarantee |
|---|---|---|
| TWS001 · Asset & Point Resolution | Every 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 Check | Dimensional 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 Isolation | Scripts 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 Bounds | Every 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/api/v1/twincode/lsp/analyzeBearer token · IDE clearanceAnalyse .tws source for syntax, TWS001–TWS005 diagnostics, UCUM units, and capability scopes.
| Parameter | In | Type | Description |
|---|---|---|---|
language_id * | body | string | Language identifier ("twinscript"). |
uri * | body | string | Document URI (for example "twin://scripts/cdu_trim.tws"). |
source * | body | string | Full TwinScript (.tws) document text. |
known_symbols | body | object | Optional 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": []
}