From 2ebefe1ca270f85f6259759b48ed5588875665bb Mon Sep 17 00:00:00 2001 From: cyre Date: Fri, 17 Jul 2026 02:06:52 +0000 Subject: [PATCH] fix(io): use a context manager in _lesson_already_appended The read-only probe opened lessons.jsonl with a bare open() and relied on GC to close the handle. Wrap it in a with-statement (and add explicit encoding="utf-8" for cross-platform consistency) so the file descriptor is released deterministically. Behavior is otherwise unchanged. Found and applied downstream in Perpetua-Tools; contributing back. Stacked on top of atomic-02 (UTF-8 fixes). --- .agent/tools/learn.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.agent/tools/learn.py b/.agent/tools/learn.py index 2b9cbec..8cb2620 100644 --- a/.agent/tools/learn.py +++ b/.agent/tools/learn.py @@ -44,16 +44,17 @@ def _lesson_already_appended(cid): return False target = f"lesson_{cid}" try: - for line in open(lessons_path): - line = line.strip() - if not line: - continue - try: - row = json.loads(line) - except json.JSONDecodeError: - continue - if row.get("id") == target: - return True + with open(lessons_path, encoding="utf-8") as f: + for line in f: + line = line.strip() + if not line: + continue + try: + row = json.loads(line) + except json.JSONDecodeError: + continue + if row.get("id") == target: + return True except OSError: return False return False