From a1360f3e35c579d7feca63489ff1f297d181dda4 Mon Sep 17 00:00:00 2001 From: JimmyL83 <80265841+JimmyL83@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:52:36 +0100 Subject: [PATCH] fix(observation): map the "None" research sentinel back in from_dict Observation.to_dict() writes the string "None" for an empty research `progress` and for a missing `current_research`. from_dict() iterated that string and raised `TypeError: string indices must be integers`, so `Observation.from_dict(env.reset(...)[0])` crashed in any fresh game where nothing has been researched yet (e.g. open_play). from_dict() now treats a non-list `progress` as empty and maps `current_research == "None"` back to None. to_dict() is unchanged. Adds server-free round-trip tests in tests/unit. Co-Authored-By: Claude Opus 5.5 --- fle/env/gym_env/observation.py | 19 +++++-- tests/unit/test_observation_roundtrip.py | 72 ++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_observation_roundtrip.py diff --git a/fle/env/gym_env/observation.py b/fle/env/gym_env/observation.py index 52d812189..dd00b4053 100644 --- a/fle/env/gym_env/observation.py +++ b/fle/env/gym_env/observation.py @@ -75,7 +75,17 @@ def from_dict(cls, obs_dict: Dict[str, Any]) -> "Observation": for item in obs_dict.get("inventory", []): inventory[item["type"]] = item["quantity"] - # Convert research state + # Convert research state. to_dict() writes the string "None" for a missing + # current_research and for an empty progress list (e.g. a fresh game where nothing has + # been researched), so map that sentinel back instead of iterating the string. + research_dict = obs_dict.get("research", {}) + current_research = research_dict.get("current_research") + if current_research == "None": + current_research = None + progress_items = research_dict.get("progress", []) + if not isinstance(progress_items, (list, tuple)): + progress_items = [] + research = ResearchState( technologies={ tech["name"]: TechnologyState( @@ -92,15 +102,12 @@ def from_dict(cls, obs_dict: Dict[str, Any]) -> "Observation": ) for tech in obs_dict.get("research", {}).get("technologies", []) }, - current_research=obs_dict.get("research", {}).get("current_research"), + current_research=current_research, research_progress=obs_dict.get("research", {}).get( "research_progress", 0.0 ), research_queue=obs_dict.get("research", {}).get("research_queue", []), - progress={ - item["name"]: item["value"] - for item in obs_dict.get("research", {}).get("progress", []) - }, + progress={item["name"]: item["value"] for item in progress_items}, ) # Convert game info diff --git a/tests/unit/test_observation_roundtrip.py b/tests/unit/test_observation_roundtrip.py new file mode 100644 index 000000000..86c065b2e --- /dev/null +++ b/tests/unit/test_observation_roundtrip.py @@ -0,0 +1,72 @@ +"""Observation.to_dict() -> Observation.from_dict() must round-trip without a live server. + +to_dict() writes the string "None" for an empty research ``progress`` and for a missing +``current_research``. from_dict() used to iterate that string and crash with +``TypeError: string indices must be integers``, which happens on the observation returned by +``FactorioGymEnv.reset()`` in a fresh game with nothing researched. +""" + +from fle.agents.models import TaskResponse +from fle.commons.models.achievements import ProductionFlows +from fle.commons.models.research_state import ResearchState +from fle.env.gym_env.observation import GameInfo, Observation + + +def _observation(research: ResearchState) -> Observation: + return Observation( + raw_text="", + entities=[], + inventory={"coal": 5}, + research=research, + game_info=GameInfo(tick=0, time=0.0, speed=1.0), + score=0.0, + automated_score=0.0, + flows=ProductionFlows( + input={}, + output={}, + crafted=[], + harvested={}, + price_list={}, + static_items={}, + ), + task_verification=TaskResponse(success=False, meta={}), + messages=[], + serialized_functions=[], + task_info=None, + map_image="", + character_positions=[], + ) + + +def test_round_trip_with_nothing_researched(): + """A fresh game: no current research and no progress entries.""" + research = ResearchState( + technologies={}, + current_research=None, + research_progress=0.0, + research_queue=[], + progress={}, + ) + obs_dict = _observation(research).to_dict() + assert obs_dict["research"]["progress"] == "None" # the sentinel to_dict() emits + + restored = Observation.from_dict(obs_dict) + + assert restored.research.progress == {} + assert restored.research.current_research is None + assert restored.inventory["coal"] == 5 + + +def test_round_trip_with_research_in_progress(): + """The populated case keeps working.""" + research = ResearchState( + technologies={}, + current_research="automation", + research_progress=0.5, + research_queue=["automation"], + progress={"automation": 0.5}, + ) + restored = Observation.from_dict(_observation(research).to_dict()) + + assert restored.research.progress == {"automation": 0.5} + assert restored.research.current_research == "automation"