From 2aff855ce9cbc7727f6f946244341ea0d3efe069 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Wed, 4 Feb 2026 18:42:58 -0300 Subject: [PATCH] atlasbot: cap kb catalog lines by size --- atlasbot/knowledge/loader.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/atlasbot/knowledge/loader.py b/atlasbot/knowledge/loader.py index 9e0a09d..8310ed2 100644 --- a/atlasbot/knowledge/loader.py +++ b/atlasbot/knowledge/loader.py @@ -106,7 +106,7 @@ class KnowledgeBase: except Exception: return lines.append("KB: atlas.json") - lines.extend(atlas_json.splitlines()) + self._extend_with_limit(lines, atlas_json.splitlines(), max_chars) def _append_runbooks(self, lines: list[str]) -> None: if not self._runbooks: @@ -134,8 +134,19 @@ class KnowledgeBase: if not text: continue lines.append(f"KB File: {path.relative_to(self._base)}") - lines.extend(text.splitlines()) + if not self._extend_with_limit(lines, text.splitlines(), max_chars): + break @staticmethod def _within_limit(lines: list[str], max_chars: int) -> bool: return sum(len(line) for line in lines) < max_chars + + @staticmethod + def _extend_with_limit(lines: list[str], new_lines: list[str], max_chars: int) -> bool: + total = sum(len(line) for line in lines) + for line in new_lines: + if total + len(line) >= max_chars: + return False + lines.append(line) + total += len(line) + return True