From b8f9cd32efa30a2d4b3f06d0592fe2b5dd4241bd Mon Sep 17 00:00:00 2001 From: haosenwang1018 Date: Tue, 24 Feb 2026 22:01:47 +0000 Subject: [PATCH] fix: use context managers for file reads in agent resource loading open().read() without context manager leaks file descriptors until GC. Use 'with open()' to ensure prompt cleanup in _load_resources(). --- agents/recode/agent.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/agents/recode/agent.py b/agents/recode/agent.py index 59b3fa6..3fda371 100644 --- a/agents/recode/agent.py +++ b/agents/recode/agent.py @@ -71,16 +71,20 @@ def reset(self, running_config: dict, init_info: dict=None) -> None: def _load_resources(self): resources_path = Path("agents/recode/resources/prompts") / self.env_name - self.available_actions = open(resources_path / "actions.txt", "r").read() + with open(resources_path / "actions.txt", "r") as f: + self.available_actions = f.read() fewshots_path = Path("agents/recode/resources/fewshots") / self.env_name if self.env_name == "alfworld": - self.fewshots = open(fewshots_path / f"{self.task_type}.txt", "r").read() + with open(fewshots_path / f"{self.task_type}.txt", "r") as f: + self.fewshots = f.read() elif self.env_name == "webshop": - self.fewshots = open(fewshots_path / "base.txt", "r").read() + with open(fewshots_path / "base.txt", "r") as f: + self.fewshots = f.read() # self.fewshots = "(No Examples)" elif self.env_name == "sciworld": - self.fewshots = open(fewshots_path / "base.txt", "r").read() + with open(fewshots_path / "base.txt", "r") as f: + self.fewshots = f.read() else: raise ValueError(f"Unsupported environment in _load_resources: {self.env_name}")