atlasbot: cap kb catalog lines by size

This commit is contained in:
Brad Stein 2026-02-04 18:42:58 -03:00
parent c0ce6335a7
commit 2aff855ce9

View File

@ -106,7 +106,7 @@ class KnowledgeBase:
except Exception: except Exception:
return return
lines.append("KB: atlas.json") 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: def _append_runbooks(self, lines: list[str]) -> None:
if not self._runbooks: if not self._runbooks:
@ -134,8 +134,19 @@ class KnowledgeBase:
if not text: if not text:
continue continue
lines.append(f"KB File: {path.relative_to(self._base)}") 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 @staticmethod
def _within_limit(lines: list[str], max_chars: int) -> bool: def _within_limit(lines: list[str], max_chars: int) -> bool:
return sum(len(line) for line in lines) < max_chars 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