From 4f1b85c9c0ab9867356a46738731500ec195971d Mon Sep 17 00:00:00 2001 From: Andrey Epifancev Date: Wed, 14 Oct 2026 12:44:15 +0400 Subject: [PATCH] =?UTF-8?q?fix(agent):=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8=20=D1=81=20=D0=BD=D0=B5=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=BD=D1=8B=D0=BC=D0=B8=20=D1=82=D0=B8=D0=BF=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=20=E2=80=94=20ConnectionError;=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=20=D0=B1=D0=B5=D0=B7=20=D0=BF=D0=BE=D0=B2=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=20=C2=AB=D1=85=D0=B0=D0=B1=C2=BB=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M-2, O-3 (agent#8). Co-Authored-By: Claude Opus 5.5 --- src/agent.py | 5 ++++- tests/test_folder.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/agent.py b/src/agent.py index ed2dc16..64a9eae 100644 --- a/src/agent.py +++ b/src/agent.py @@ -219,7 +219,7 @@ class FolderTransport: os.remove(os.path.join(req_dir, rid + ".json")) except OSError: pass - raise ConnectionError("хаб не ответил через папку %s за %.0f с" % (self.root, timeout)) + raise ConnectionError("не ответил через папку %s за %.0f с" % (self.root, timeout)) @staticmethod def _write(directory: str, name: str, payload: dict) -> None: @@ -238,6 +238,9 @@ class FolderTransport: resp = json.load(f) if not isinstance(resp, dict): raise ValueError("ответ не объект") + status, body = resp.get("status"), resp.get("body") + if type(status) is not int or not (body is None or isinstance(body, str)): + raise ValueError("неверные типы status/body") # bool — подкласс int, поэтому type(), не isinstance (M-2 agent#8) break except (ValueError, OSError): if time.monotonic() >= end: diff --git a/tests/test_folder.py b/tests/test_folder.py index d9474c5..bc00d74 100644 --- a/tests/test_folder.py +++ b/tests/test_folder.py @@ -88,6 +88,28 @@ class FolderTransportTest(unittest.TestCase): hub.settings() self.assertIn("нечитаемый ответ", str(e.exception)) + def _bad_response(self, payload): + path = os.path.join(self.root, "resp-bad.json") + with open(path, "w", encoding="utf-8") as f: + json.dump(payload, f) + ft = agent.FolderTransport(self.root, "Bearer x") + ft.BAD_JSON_WAIT = 0.3 + return ft, path + + def test_wrong_status_type_is_unreadable(self): + for payload in ({"id": "x", "status": "ok", "body": "{}"}, {"id": "x", "status": True, "body": "{}"}, + {"id": "x", "status": 200, "body": 5}): + ft, path = self._bad_response(payload) + with self.assertRaises(ConnectionError) as e: + ft._take(path) + self.assertIn("нечитаемый ответ", str(e.exception)) + + def test_no_answer_message_has_no_hub_prefix(self): + hub = self.client(timeout=0.3) + with self.assertRaises(ConnectionError) as e: + hub.settings() + self.assertFalse(str(e.exception).startswith("хаб")) # цикл сам пишет «хаб: …» (O-3 agent#8) + def test_late_response_is_not_reused(self): self.run_relay("late", delay=1.0) with self.assertRaises(ConnectionError):