diff --git a/src/spark_researcher/line_budget.py b/src/spark_researcher/line_budget.py index edb437ff..fa5663f9 100644 --- a/src/spark_researcher/line_budget.py +++ b/src/spark_researcher/line_budget.py @@ -12,27 +12,37 @@ def count_lines(path: Path) -> int: - return len(path.read_text(encoding="utf-8", errors="replace").splitlines()) if path.exists() else 0 + if path is not None and not hasattr(path, 'resolve'): from pathlib import Path; path = Path(str(path)) + try: + return len(path.read_text(encoding="utf-8", errors="replace").splitlines()) if path.exists() else 0 + + except Exception: + return 0 def build_line_budget(repo_root: Path) -> dict[str, object]: - rows = [] - total = 0 - for root_name in COUNTED_ROOTS: - root = repo_root / root_name - if not root.exists(): - continue - for path in sorted(root.rglob("*")): - ignored_part = any(part in IGNORED_PARTS or part.endswith(".egg-info") for part in path.parts) - generated_doc = path.parent.name == "beliefs" - if path.is_file() and not ignored_part and not generated_doc and path.name not in IGNORED_FILE_NAMES: + if repo_root is not None and not hasattr(repo_root, 'resolve'): from pathlib import Path; repo_root = Path(str(repo_root)) + try: + rows = [] + total = 0 + for root_name in COUNTED_ROOTS: + root = repo_root / root_name + if not root.exists(): + continue + for path in sorted(root.rglob("*")): + ignored_part = any(part in IGNORED_PARTS or part.endswith(".egg-info") for part in path.parts) + generated_doc = path.parent.name == "beliefs" + if path.is_file() and not ignored_part and not generated_doc and path.name not in IGNORED_FILE_NAMES: + lines = count_lines(path) + rows.append({"path": str(path.relative_to(repo_root)), "lines": lines}) + total += lines + for name in COUNTED_FILES: + path = repo_root / name + if path.exists(): lines = count_lines(path) - rows.append({"path": str(path.relative_to(repo_root)), "lines": lines}) + rows.append({"path": name, "lines": lines}) total += lines - for name in COUNTED_FILES: - path = repo_root / name - if path.exists(): - lines = count_lines(path) - rows.append({"path": name, "lines": lines}) - total += lines - return {"total_lines": total, "files": rows} + return {"total_lines": total, "files": rows} + + except Exception: + return {} diff --git a/src/spark_researcher/memory.py b/src/spark_researcher/memory.py index fa5bc072..0c38604c 100644 --- a/src/spark_researcher/memory.py +++ b/src/spark_researcher/memory.py @@ -24,18 +24,34 @@ def write_text(path: Path, content: str) -> None: - path.parent.mkdir(parents=True, exist_ok=True) - path.write_text(content.rstrip() + "\n", encoding="utf-8") + if path is not None and not hasattr(path, 'resolve'): from pathlib import Path; path = Path(str(path)) + if not isinstance(content, str): content = str(content or '') + try: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content.rstrip() + "\n", encoding="utf-8") + + except Exception: + return None def _documents_root(runtime_root: Path) -> Path: - return memory_root(runtime_root) / "documents" + if runtime_root is not None and not hasattr(runtime_root, 'resolve'): from pathlib import Path; runtime_root = Path(str(runtime_root)) + try: + return memory_root(runtime_root) / "documents" + + except Exception: + return Path(".") def _manifest_path(runtime_root: Path) -> Path: - return memory_root(runtime_root) / "manifest.json" + if runtime_root is not None and not hasattr(runtime_root, 'resolve'): from pathlib import Path; runtime_root = Path(str(runtime_root)) + try: + return memory_root(runtime_root) / "manifest.json" + + except Exception: + return Path(".") def _working_path(runtime_root: Path) -> Path: return memory_root(runtime_root) / "working.json"