From 4439942722349bf40ecea43670c57e1423cc7325 Mon Sep 17 00:00:00 2001
From: Even Solbraa <41290109+EvenSol@users.noreply.github.com>
Date: Sun, 30 Aug 2026 15:31:51 +0200
Subject: [PATCH 1/4] feat(game): add native throughput challenge engine
---
process_chat/plant_operator_game.py | 555 ++++++++++++++++++++++++++++
tests/test_plant_operator_game.py | 116 ++++++
2 files changed, 671 insertions(+)
create mode 100644 process_chat/plant_operator_game.py
create mode 100644 tests/test_plant_operator_game.py
diff --git a/process_chat/plant_operator_game.py b/process_chat/plant_operator_game.py
new file mode 100644
index 0000000..dcce30a
--- /dev/null
+++ b/process_chat/plant_operator_game.py
@@ -0,0 +1,555 @@
+"""Native NeqSim simulation and scoring for Plant Operator challenges.
+
+The game layer deliberately stays thin: NeqSim owns the thermodynamics,
+equipment calculations, process convergence, and conservation evidence. This
+module only defines a reproducible training case and translates its solved
+engineering evidence into a transparent challenge score.
+"""
+
+from __future__ import annotations
+
+import math
+from dataclasses import dataclass
+from time import perf_counter
+from typing import Any
+
+from .process_builder import ProcessBuilder
+from .process_model import ModelRunResult, NeqSimProcessModel, ProcessRunTimeoutError
+
+
+CHALLENGE_NAME = "The 10% Throughput Challenge"
+BASELINE_FLOW_KG_HR = 100_000.0
+TARGET_FLOW_KG_HR = 110_000.0
+MIN_EXPORT_PRESSURE_BARA = 128.0
+MAX_EXPORT_TEMPERATURE_C = 45.0
+MAX_COMPRESSOR_DISCHARGE_TEMPERATURE_C = 120.0
+MAX_TOTAL_POWER_KW = 4_500.0
+MAX_SPECIFIC_POWER_KWH_PER_TONNE = 41.0
+MAX_COOLING_DUTY_KW = 7_000.0
+MAX_BALANCE_ERROR_PCT = 0.10
+CHALLENGE_TIMEOUT_MS = 180_000
+
+
+@dataclass(frozen=True)
+class ChallengeControls:
+ """Player-adjustable operating decisions for the first challenge."""
+
+ feed_flow_kg_hr: float = BASELINE_FLOW_KG_HR
+ stage_1_pressure_bara: float = 80.0
+ stage_2_pressure_bara: float = 130.0
+ intercooler_temperature_c: float = 35.0
+ export_temperature_c: float = 40.0
+
+
+@dataclass(frozen=True)
+class ChallengeEvidence:
+ """Solved evidence used by the score; all values come from NeqSim."""
+
+ feed_flow_kg_hr: float
+ export_pressure_bara: float
+ export_temperature_c: float
+ stage_1_discharge_temperature_c: float
+ stage_2_discharge_temperature_c: float
+ total_power_kw: float
+ total_cooling_duty_kw: float
+ specific_power_kwh_per_tonne: float
+ mass_balance_error_pct: float
+ energy_balance_error_pct: float
+ native_violations: tuple[str, ...] = ()
+
+
+@dataclass(frozen=True)
+class ChallengeCheck:
+ """One objective engineering acceptance check."""
+
+ name: str
+ passed: bool
+ actual: str
+ requirement: str
+
+
+@dataclass(frozen=True)
+class ChallengeAssessment:
+ """Scored assessment returned to the game interface."""
+
+ score: int
+ won: bool
+ grade: str
+ checks: tuple[ChallengeCheck, ...]
+ guidance: tuple[str, ...]
+
+
+@dataclass
+class ChallengeRun:
+ """A reproducible NeqSim run plus the game-facing assessment."""
+
+ controls: ChallengeControls
+ spec: dict[str, Any]
+ builder: ProcessBuilder
+ model: NeqSimProcessModel
+ result: ModelRunResult
+ evidence: ChallengeEvidence
+ assessment: ChallengeAssessment
+ elapsed_seconds: float
+
+
+def _finite_between(value: float, name: str, minimum: float, maximum: float) -> float:
+ """Return a validated finite float inside an inclusive range."""
+ if isinstance(value, bool):
+ raise ValueError(f"{name} must be numeric.")
+ try:
+ numeric_value = float(value)
+ except (TypeError, ValueError) as exc:
+ raise ValueError(f"{name} must be numeric.") from exc
+ if not math.isfinite(numeric_value):
+ raise ValueError(f"{name} must be finite.")
+ if not minimum <= numeric_value <= maximum:
+ raise ValueError(
+ f"{name} must be between {minimum:g} and {maximum:g}."
+ )
+ return numeric_value
+
+
+def validate_controls(controls: ChallengeControls) -> ChallengeControls:
+ """Validate the public game input contract before starting the JVM."""
+ if not isinstance(controls, ChallengeControls):
+ raise ValueError("Challenge controls must use ChallengeControls.")
+ values = {
+ "feed_flow_kg_hr": _finite_between(
+ controls.feed_flow_kg_hr,
+ "Feed flow",
+ 90_000.0,
+ 125_000.0,
+ ),
+ "stage_1_pressure_bara": _finite_between(
+ controls.stage_1_pressure_bara,
+ "Stage 1 pressure",
+ 60.0,
+ 110.0,
+ ),
+ "stage_2_pressure_bara": _finite_between(
+ controls.stage_2_pressure_bara,
+ "Stage 2 pressure",
+ 115.0,
+ 145.0,
+ ),
+ "intercooler_temperature_c": _finite_between(
+ controls.intercooler_temperature_c,
+ "Intercooler temperature",
+ 20.0,
+ 60.0,
+ ),
+ "export_temperature_c": _finite_between(
+ controls.export_temperature_c,
+ "Export temperature",
+ 20.0,
+ 55.0,
+ ),
+ }
+ if values["stage_1_pressure_bara"] >= values["stage_2_pressure_bara"]:
+ raise ValueError("Stage 1 pressure must be below Stage 2 pressure.")
+ return ChallengeControls(**values)
+
+
+def build_challenge_spec(controls: ChallengeControls) -> dict[str, Any]:
+ """Build the minimum reproducible ProcessBuilder specification."""
+ controls = validate_controls(controls)
+ fluid_spec = {
+ "eos_model": "srk",
+ "mixing_rule": 2,
+ "components": {
+ "nitrogen": 0.010,
+ "CO2": 0.020,
+ "methane": 0.850,
+ "ethane": 0.060,
+ "propane": 0.030,
+ "i-butane": 0.008,
+ "n-butane": 0.012,
+ "i-pentane": 0.004,
+ "n-pentane": 0.003,
+ "n-hexane": 0.003,
+ },
+ "composition_basis": "mole_fraction",
+ "temperature_C": 30.0,
+ "pressure_bara": 50.0,
+ "total_flow": controls.feed_flow_kg_hr,
+ "flow_unit": "kg/hr",
+ }
+ process = [
+ {
+ "name": "feed gas",
+ "type": "stream",
+ "params": {
+ "temperature_C": 30.0,
+ "pressure_bara": 50.0,
+ "flow_rate": controls.feed_flow_kg_hr,
+ "flow_unit": "kg/hr",
+ },
+ },
+ {
+ "name": "inlet scrubber",
+ "type": "separator",
+ "outlet": "gas",
+ },
+ {
+ "name": "compressor stage 1",
+ "type": "compressor",
+ "params": {
+ "outlet_pressure_bara": controls.stage_1_pressure_bara,
+ "isentropic_efficiency": 0.78,
+ },
+ },
+ {
+ "name": "intercooler",
+ "type": "cooler",
+ "params": {
+ "outlet_temperature_C": controls.intercooler_temperature_c,
+ "pressure_drop_bar": 0.0,
+ },
+ },
+ {
+ "name": "interstage scrubber",
+ "type": "separator",
+ "outlet": "gas",
+ },
+ {
+ "name": "compressor stage 2",
+ "type": "compressor",
+ "params": {
+ "outlet_pressure_bara": controls.stage_2_pressure_bara,
+ "isentropic_efficiency": 0.78,
+ },
+ },
+ {
+ "name": "export cooler",
+ "type": "cooler",
+ "params": {
+ "outlet_temperature_C": controls.export_temperature_c,
+ "pressure_drop_bar": 0.0,
+ },
+ },
+ ]
+ return {
+ "name": CHALLENGE_NAME,
+ "description": (
+ "Synthetic gas-compression training case. Increase throughput by "
+ "10% while respecting export, power, temperature, and conservation "
+ "constraints."
+ ),
+ "assumptions": [
+ "Steady-state training scenario using the SRK equation of state.",
+ "Pressures are absolute (bara).",
+ "Flow is mass flow in kg/hr.",
+ "Both compressor isentropic efficiencies are fixed at 0.78.",
+ "The score is educational and is not a design certification.",
+ ],
+ "fluid": fluid_spec,
+ "process": process,
+ }
+
+
+def _outlet_state(model: NeqSimProcessModel, unit_name: str) -> tuple[float, float]:
+ """Return temperature in degC and pressure in bara for one unit outlet."""
+ unit = model.get_unit(unit_name)
+ outlet = None
+ for getter_name in ("getOutletStream", "getOutStream", "getGasOutStream"):
+ if not hasattr(unit, getter_name):
+ continue
+ try:
+ outlet = getattr(unit, getter_name)()
+ except Exception:
+ continue
+ if outlet is not None:
+ break
+ if outlet is None:
+ raise RuntimeError(f"{unit_name} did not expose a solved outlet stream.")
+ return (
+ float(outlet.getTemperature("C")),
+ float(outlet.getPressure("bara")),
+ )
+
+
+def collect_evidence(
+ controls: ChallengeControls,
+ model: NeqSimProcessModel,
+ result: ModelRunResult,
+) -> ChallengeEvidence:
+ """Collect only solved NeqSim values used by the challenge score."""
+ stage_1_temperature_c, _ = _outlet_state(model, "compressor stage 1")
+ stage_2_temperature_c, _ = _outlet_state(model, "compressor stage 2")
+ export_temperature_c, export_pressure_bara = _outlet_state(
+ model,
+ "export cooler",
+ )
+
+ total_power_kw = abs(float(result.kpis["total_power_kW"].value))
+ total_cooling_duty_kw = abs(float(result.kpis["total_duty_kW"].value))
+ specific_power = total_power_kw * 1000.0 / controls.feed_flow_kg_hr
+ mass_balance = result.kpis.get("mass_balance_pct")
+ energy_balance = result.kpis.get("energy_balance_pct")
+ native_violations = tuple(
+ constraint.name
+ for constraint in result.constraints
+ if str(constraint.status).upper() == "VIOLATION"
+ and constraint.name not in {"mass_balance", "energy_balance"}
+ )
+ return ChallengeEvidence(
+ feed_flow_kg_hr=controls.feed_flow_kg_hr,
+ export_pressure_bara=export_pressure_bara,
+ export_temperature_c=export_temperature_c,
+ stage_1_discharge_temperature_c=stage_1_temperature_c,
+ stage_2_discharge_temperature_c=stage_2_temperature_c,
+ total_power_kw=total_power_kw,
+ total_cooling_duty_kw=total_cooling_duty_kw,
+ specific_power_kwh_per_tonne=specific_power,
+ mass_balance_error_pct=(
+ float(mass_balance.value) if mass_balance is not None else math.inf
+ ),
+ energy_balance_error_pct=(
+ float(energy_balance.value) if energy_balance is not None else math.inf
+ ),
+ native_violations=native_violations,
+ )
+
+
+def _bounded_score(value: float, best: float, worst: float, points: float) -> float:
+ """Scale lower-is-better evidence into a bounded point contribution."""
+ if not math.isfinite(value) or worst <= best:
+ return 0.0
+ fraction = (worst - value) / (worst - best)
+ return points * min(1.0, max(0.0, fraction))
+
+
+def assess_challenge(evidence: ChallengeEvidence) -> ChallengeAssessment:
+ """Evaluate one solved operating strategy with a transparent score."""
+ maximum_discharge_temperature_c = max(
+ evidence.stage_1_discharge_temperature_c,
+ evidence.stage_2_discharge_temperature_c,
+ )
+ checks = (
+ ChallengeCheck(
+ "Throughput target",
+ evidence.feed_flow_kg_hr >= TARGET_FLOW_KG_HR,
+ f"{evidence.feed_flow_kg_hr:,.0f} kg/hr",
+ f"at least {TARGET_FLOW_KG_HR:,.0f} kg/hr",
+ ),
+ ChallengeCheck(
+ "Export pressure",
+ evidence.export_pressure_bara >= MIN_EXPORT_PRESSURE_BARA,
+ f"{evidence.export_pressure_bara:.2f} bara",
+ f"at least {MIN_EXPORT_PRESSURE_BARA:.0f} bara",
+ ),
+ ChallengeCheck(
+ "Export temperature",
+ evidence.export_temperature_c <= MAX_EXPORT_TEMPERATURE_C,
+ f"{evidence.export_temperature_c:.2f} °C",
+ f"at most {MAX_EXPORT_TEMPERATURE_C:.0f} °C",
+ ),
+ ChallengeCheck(
+ "Compressor discharge temperature",
+ maximum_discharge_temperature_c
+ <= MAX_COMPRESSOR_DISCHARGE_TEMPERATURE_C,
+ f"{maximum_discharge_temperature_c:.2f} °C maximum",
+ (
+ "at most "
+ f"{MAX_COMPRESSOR_DISCHARGE_TEMPERATURE_C:.0f} °C"
+ ),
+ ),
+ ChallengeCheck(
+ "Compression power",
+ evidence.total_power_kw <= MAX_TOTAL_POWER_KW,
+ f"{evidence.total_power_kw:,.0f} kW",
+ f"at most {MAX_TOTAL_POWER_KW:,.0f} kW",
+ ),
+ ChallengeCheck(
+ "Specific compression energy",
+ evidence.specific_power_kwh_per_tonne
+ <= MAX_SPECIFIC_POWER_KWH_PER_TONNE,
+ f"{evidence.specific_power_kwh_per_tonne:.2f} kWh/tonne",
+ f"at most {MAX_SPECIFIC_POWER_KWH_PER_TONNE:.0f} kWh/tonne",
+ ),
+ ChallengeCheck(
+ "Cooling-system load",
+ evidence.total_cooling_duty_kw <= MAX_COOLING_DUTY_KW,
+ f"{evidence.total_cooling_duty_kw:,.0f} kW",
+ f"at most {MAX_COOLING_DUTY_KW:,.0f} kW",
+ ),
+ ChallengeCheck(
+ "Mass balance",
+ evidence.mass_balance_error_pct <= MAX_BALANCE_ERROR_PCT,
+ f"{evidence.mass_balance_error_pct:.6g}% error",
+ f"at most {MAX_BALANCE_ERROR_PCT:.2f}% error",
+ ),
+ ChallengeCheck(
+ "Energy balance",
+ evidence.energy_balance_error_pct <= MAX_BALANCE_ERROR_PCT,
+ f"{evidence.energy_balance_error_pct:.6g}% error",
+ f"at most {MAX_BALANCE_ERROR_PCT:.2f}% error",
+ ),
+ ChallengeCheck(
+ "Native NeqSim checks",
+ not evidence.native_violations,
+ (
+ "No violations"
+ if not evidence.native_violations
+ else ", ".join(evidence.native_violations)
+ ),
+ "no native constraint violations",
+ ),
+ )
+
+ production_fraction = min(
+ 1.0,
+ max(0.0, evidence.feed_flow_kg_hr / TARGET_FLOW_KG_HR),
+ )
+ production_points = 400.0 * production_fraction
+ stretch_bonus = 50.0 * min(
+ 1.0,
+ max(
+ 0.0,
+ (evidence.feed_flow_kg_hr - TARGET_FLOW_KG_HR)
+ / (TARGET_FLOW_KG_HR * 0.10),
+ ),
+ )
+ energy_points = _bounded_score(
+ evidence.specific_power_kwh_per_tonne,
+ best=38.5,
+ worst=MAX_SPECIFIC_POWER_KWH_PER_TONNE,
+ points=250.0,
+ )
+ thermal_points = _bounded_score(
+ maximum_discharge_temperature_c,
+ best=70.0,
+ worst=MAX_COMPRESSOR_DISCHARGE_TEMPERATURE_C,
+ points=100.0,
+ )
+ cooling_points = _bounded_score(
+ evidence.total_cooling_duty_kw,
+ best=6_000.0,
+ worst=MAX_COOLING_DUTY_KW,
+ points=50.0,
+ )
+ integrity_checks = checks[-3:]
+ integrity_points = 150.0 * (
+ sum(check.passed for check in integrity_checks)
+ / len(integrity_checks)
+ )
+ score = int(
+ round(
+ min(
+ 1000.0,
+ production_points
+ + stretch_bonus
+ + energy_points
+ + thermal_points
+ + cooling_points
+ + integrity_points,
+ )
+ )
+ )
+ won = all(check.passed for check in checks)
+ if won and score >= 900:
+ grade = "Outstanding operation"
+ elif won:
+ grade = "Challenge completed"
+ elif score >= 700:
+ grade = "Close — one more adjustment"
+ elif score >= 500:
+ grade = "Developing strategy"
+ else:
+ grade = "Plant constraints not controlled"
+
+ failed_names = {check.name for check in checks if not check.passed}
+ guidance: list[str] = []
+ if "Throughput target" in failed_names:
+ guidance.append("Increase feed flow to at least 110,000 kg/hr.")
+ if "Export pressure" in failed_names:
+ guidance.append("Raise the second-stage discharge-pressure setpoint.")
+ if "Export temperature" in failed_names:
+ guidance.append("Lower the export-cooler temperature setpoint.")
+ if {
+ "Compression power",
+ "Specific compression energy",
+ }.intersection(failed_names):
+ ideal_interstage_pressure = math.sqrt(
+ 50.0 * evidence.export_pressure_bara
+ )
+ guidance.append(
+ "Move the interstage pressure toward the equal-ratio value "
+ f"of about {ideal_interstage_pressure:.0f} bara and keep the "
+ "intercooler effective."
+ )
+ if "Compressor discharge temperature" in failed_names:
+ guidance.append(
+ "Reduce the intercooler setpoint or rebalance the pressure ratio."
+ )
+ if "Cooling-system load" in failed_names:
+ guidance.append(
+ "Reduce avoidable cooling load while keeping compression energy and "
+ "discharge temperatures inside their limits."
+ )
+ if {"Mass balance", "Energy balance", "Native NeqSim checks"}.intersection(
+ failed_names
+ ):
+ guidance.append(
+ "Treat the run as invalid until the native validation evidence passes."
+ )
+ if not guidance:
+ guidance.append(
+ "All constraints pass. Try a higher throughput or lower specific "
+ "energy to improve the score."
+ )
+ return ChallengeAssessment(
+ score=score,
+ won=won,
+ grade=grade,
+ checks=checks,
+ guidance=tuple(guidance),
+ )
+
+
+def run_challenge(
+ controls: ChallengeControls,
+ *,
+ timeout_ms: int = CHALLENGE_TIMEOUT_MS,
+) -> ChallengeRun:
+ """Build, solve, validate, and score one strategy within one time budget."""
+ controls = validate_controls(controls)
+ if isinstance(timeout_ms, bool) or int(timeout_ms) <= 0:
+ raise ValueError("Challenge timeout must be a positive integer.")
+ timeout_ms = int(timeout_ms)
+ started = perf_counter()
+ deadline = started + timeout_ms / 1000.0
+
+ def remaining_budget_ms() -> int:
+ remaining_ms = int((deadline - perf_counter()) * 1000.0)
+ if remaining_ms <= 0:
+ raise ProcessRunTimeoutError(
+ f"Plant Operator run exceeded its {timeout_ms} ms budget; "
+ "discard the partial model."
+ )
+ return remaining_ms
+
+ spec = build_challenge_spec(controls)
+ builder = ProcessBuilder()
+ model = builder.build_from_spec_bounded(
+ spec,
+ timeout_ms=remaining_budget_ms(),
+ )
+ result = model.run_bounded(timeout_ms=remaining_budget_ms())
+ evidence = model.run_bounded_operation(
+ lambda: collect_evidence(controls, model, result),
+ timeout_ms=remaining_budget_ms(),
+ operation="Plant Operator evidence collection",
+ )
+ assessment = assess_challenge(evidence)
+ return ChallengeRun(
+ controls=controls,
+ spec=spec,
+ builder=builder,
+ model=model,
+ result=result,
+ evidence=evidence,
+ assessment=assessment,
+ elapsed_seconds=perf_counter() - started,
+ )
diff --git a/tests/test_plant_operator_game.py b/tests/test_plant_operator_game.py
new file mode 100644
index 0000000..66184c5
--- /dev/null
+++ b/tests/test_plant_operator_game.py
@@ -0,0 +1,116 @@
+"""Unit tests for the NeqSim Plant Operator challenge contract."""
+
+import math
+import unittest
+
+from process_chat.plant_operator_game import (
+ ChallengeControls,
+ ChallengeEvidence,
+ TARGET_FLOW_KG_HR,
+ assess_challenge,
+ build_challenge_spec,
+ validate_controls,
+)
+
+
+class PlantOperatorGameTest(unittest.TestCase):
+ """Protect reproducibility, validation, and transparent scoring."""
+
+ def test_builds_minimum_reproducible_native_process_spec(self):
+ controls = ChallengeControls(
+ feed_flow_kg_hr=112_000.0,
+ stage_1_pressure_bara=82.0,
+ stage_2_pressure_bara=131.0,
+ intercooler_temperature_c=32.0,
+ export_temperature_c=39.0,
+ )
+
+ spec = build_challenge_spec(controls)
+
+ self.assertEqual(spec["fluid"]["eos_model"], "srk")
+ self.assertEqual(spec["fluid"]["flow_unit"], "kg/hr")
+ self.assertAlmostEqual(sum(spec["fluid"]["components"].values()), 1.0)
+ self.assertEqual(spec["fluid"]["total_flow"], 112_000.0)
+ self.assertEqual(
+ [step["type"] for step in spec["process"]],
+ [
+ "stream",
+ "separator",
+ "compressor",
+ "cooler",
+ "separator",
+ "compressor",
+ "cooler",
+ ],
+ )
+ self.assertEqual(
+ spec["process"][2]["params"]["outlet_pressure_bara"],
+ 82.0,
+ )
+ self.assertEqual(
+ spec["process"][5]["params"]["outlet_pressure_bara"],
+ 131.0,
+ )
+
+ def test_rejects_invalid_or_nonphysical_controls_before_native_execution(self):
+ with self.assertRaisesRegex(ValueError, "Feed flow must be finite"):
+ validate_controls(ChallengeControls(feed_flow_kg_hr=math.nan))
+ with self.assertRaisesRegex(ValueError, "Stage 1 pressure must be between"):
+ validate_controls(ChallengeControls(stage_1_pressure_bara=140.0))
+ with self.assertRaisesRegex(ValueError, "Export temperature must be between"):
+ validate_controls(ChallengeControls(export_temperature_c=80.0))
+
+ def test_winning_evidence_requires_every_engineering_check(self):
+ evidence = ChallengeEvidence(
+ feed_flow_kg_hr=TARGET_FLOW_KG_HR,
+ export_pressure_bara=130.0,
+ export_temperature_c=40.0,
+ stage_1_discharge_temperature_c=90.0,
+ stage_2_discharge_temperature_c=105.0,
+ total_power_kw=4_300.0,
+ total_cooling_duty_kw=6_500.0,
+ specific_power_kwh_per_tonne=39.1,
+ mass_balance_error_pct=0.001,
+ energy_balance_error_pct=0.002,
+ native_violations=(),
+ )
+
+ assessment = assess_challenge(evidence)
+
+ self.assertTrue(assessment.won)
+ self.assertTrue(all(check.passed for check in assessment.checks))
+ self.assertGreaterEqual(assessment.score, 700)
+ self.assertLessEqual(assessment.score, 1000)
+
+ def test_failed_evidence_returns_specific_operating_guidance(self):
+ evidence = ChallengeEvidence(
+ feed_flow_kg_hr=100_000.0,
+ export_pressure_bara=120.0,
+ export_temperature_c=50.0,
+ stage_1_discharge_temperature_c=155.0,
+ stage_2_discharge_temperature_c=160.0,
+ total_power_kw=12_000.0,
+ total_cooling_duty_kw=11_000.0,
+ specific_power_kwh_per_tonne=120.0,
+ mass_balance_error_pct=2.0,
+ energy_balance_error_pct=2.0,
+ native_violations=("compressor_map.stage 2",),
+ )
+
+ assessment = assess_challenge(evidence)
+
+ self.assertFalse(assessment.won)
+ failed = {check.name for check in assessment.checks if not check.passed}
+ self.assertIn("Throughput target", failed)
+ self.assertIn("Export pressure", failed)
+ self.assertIn("Compression power", failed)
+ self.assertTrue(
+ any("equal-ratio" in guidance for guidance in assessment.guidance)
+ )
+ self.assertTrue(
+ any("invalid" in guidance for guidance in assessment.guidance)
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()
From 1bd7754401e4a3a5f1b72e1896b535d290084986 Mon Sep 17 00:00:00 2001
From: Even Solbraa <41290109+EvenSol@users.noreply.github.com>
Date: Sun, 30 Aug 2026 15:32:13 +0200
Subject: [PATCH 2/4] feat(game): add Plant Operator Studio workflow
---
pages/36_NeqSim_Plant_Operator.py | 493 ++++++++++++++++++++++++++++++
studio/navigation.py | 12 +
tests/test_plant_operator_page.py | 70 +++++
tests/test_studio_navigation.py | 9 +
4 files changed, 584 insertions(+)
create mode 100644 pages/36_NeqSim_Plant_Operator.py
create mode 100644 tests/test_plant_operator_page.py
diff --git a/pages/36_NeqSim_Plant_Operator.py b/pages/36_NeqSim_Plant_Operator.py
new file mode 100644
index 0000000..859a426
--- /dev/null
+++ b/pages/36_NeqSim_Plant_Operator.py
@@ -0,0 +1,493 @@
+"""NeqSim Plant Operator — an engineering training game."""
+
+from __future__ import annotations
+
+import json
+import os
+import sys
+
+import pandas as pd
+import streamlit as st
+
+
+_JVM_OPENS = (
+ "--add-opens=java.base/java.util=ALL-UNNAMED "
+ "--add-opens=java.base/java.lang=ALL-UNNAMED "
+ "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED "
+ "--add-opens=java.base/java.io=ALL-UNNAMED"
+)
+if "add-opens" not in os.environ.get("JAVA_TOOL_OPTIONS", ""):
+ os.environ["JAVA_TOOL_OPTIONS"] = _JVM_OPENS
+
+_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+if _PROJECT_ROOT not in sys.path:
+ sys.path.insert(0, _PROJECT_ROOT)
+
+from process_chat.plant_operator_game import ( # noqa: E402
+ BASELINE_FLOW_KG_HR,
+ CHALLENGE_NAME,
+ CHALLENGE_TIMEOUT_MS,
+ ChallengeControls,
+ ChallengeRun,
+ run_challenge,
+)
+from theme import apply_theme, theme_toggle # noqa: E402
+
+
+LAST_RUN_STATE_KEY = "plant_operator_last_run"
+ATTEMPT_COUNT_STATE_KEY = "plant_operator_attempt_count"
+BEST_SCORE_STATE_KEY = "plant_operator_best_score"
+CONTROL_DEFAULTS = {
+ "plant_operator_feed_flow": BASELINE_FLOW_KG_HR,
+ "plant_operator_stage_1_pressure": 80.0,
+ "plant_operator_stage_2_pressure": 125.0,
+ "plant_operator_intercooler_temperature": 40.0,
+ "plant_operator_export_temperature": 45.0,
+}
+
+
+def _initialize_state() -> None:
+ """Create stable widget and progress state for this session."""
+ for key, value in CONTROL_DEFAULTS.items():
+ st.session_state.setdefault(key, value)
+ st.session_state.setdefault(ATTEMPT_COUNT_STATE_KEY, 0)
+ st.session_state.setdefault(BEST_SCORE_STATE_KEY, 0)
+
+
+def _reset_challenge() -> None:
+ """Reset controls and session-local progress to the original plant state."""
+ for key, value in CONTROL_DEFAULTS.items():
+ st.session_state[key] = value
+ st.session_state.pop(LAST_RUN_STATE_KEY, None)
+ st.session_state[ATTEMPT_COUNT_STATE_KEY] = 0
+ st.session_state[BEST_SCORE_STATE_KEY] = 0
+
+
+def _controls_from_widgets() -> ChallengeControls:
+ """Return the current player decisions as the engine input contract."""
+ return ChallengeControls(
+ feed_flow_kg_hr=float(st.session_state["plant_operator_feed_flow"]),
+ stage_1_pressure_bara=float(
+ st.session_state["plant_operator_stage_1_pressure"]
+ ),
+ stage_2_pressure_bara=float(
+ st.session_state["plant_operator_stage_2_pressure"]
+ ),
+ intercooler_temperature_c=float(
+ st.session_state["plant_operator_intercooler_temperature"]
+ ),
+ export_temperature_c=float(
+ st.session_state["plant_operator_export_temperature"]
+ ),
+ )
+
+
+def _evidence_dataframe(run: ChallengeRun) -> pd.DataFrame:
+ """Format the score evidence with explicit units."""
+ evidence = run.evidence
+ return pd.DataFrame(
+ [
+ {
+ "Metric": "Feed throughput",
+ "Value": evidence.feed_flow_kg_hr,
+ "Unit": "kg/hr",
+ },
+ {
+ "Metric": "Export pressure",
+ "Value": evidence.export_pressure_bara,
+ "Unit": "bara",
+ },
+ {
+ "Metric": "Export temperature",
+ "Value": evidence.export_temperature_c,
+ "Unit": "°C",
+ },
+ {
+ "Metric": "Stage 1 discharge temperature",
+ "Value": evidence.stage_1_discharge_temperature_c,
+ "Unit": "°C",
+ },
+ {
+ "Metric": "Stage 2 discharge temperature",
+ "Value": evidence.stage_2_discharge_temperature_c,
+ "Unit": "°C",
+ },
+ {
+ "Metric": "Compression power",
+ "Value": evidence.total_power_kw,
+ "Unit": "kW",
+ },
+ {
+ "Metric": "Specific compression energy",
+ "Value": evidence.specific_power_kwh_per_tonne,
+ "Unit": "kWh/tonne",
+ },
+ {
+ "Metric": "Cooling duty",
+ "Value": evidence.total_cooling_duty_kw,
+ "Unit": "kW",
+ },
+ {
+ "Metric": "Mass-balance error",
+ "Value": evidence.mass_balance_error_pct,
+ "Unit": "%",
+ },
+ {
+ "Metric": "Energy-balance error",
+ "Value": evidence.energy_balance_error_pct,
+ "Unit": "%",
+ },
+ ]
+ )
+
+
+st.set_page_config(
+ page_title="NeqSim Plant Operator",
+ page_icon="images/neqsimlogocircleflat.png",
+ layout="wide",
+)
+apply_theme()
+theme_toggle()
+_initialize_state()
+
+st.markdown(
+ """
+
+""",
+ unsafe_allow_html=True,
+)
+
+with st.sidebar:
+ st.markdown("### Plant Operator")
+ st.caption("Native NeqSim engineering challenge")
+ if st.button("← NeqSim Studio", use_container_width=True):
+ st.switch_page("pages/00_NeqSim_Studio.py")
+ if st.button("Reset challenge", use_container_width=True):
+ _reset_challenge()
+ st.rerun()
+ st.divider()
+ attempt_metric_placeholder = st.empty()
+ best_score_metric_placeholder = st.empty()
+ st.caption(
+ "Synthetic educational case. Results are engineering-screening "
+ "evidence, not design certification."
+ )
+
+st.markdown(
+ f"""
+ NeqSim Plant Operator · Challenge 1
+ Raise production from 100,000 to 110,000 kg/hr. Balance the two-stage
+ compressor train, satisfy export conditions, and keep the native NeqSim
+ conservation checks trustworthy.
+ {CHALLENGE_NAME}
+