Coverage for session_buddy / memory / file_context.py: 92.00%
19 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-04 00:43 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-04 00:43 -0800
1from __future__ import annotations
3from pathlib import Path
4from typing import Any
7def build_file_context(file_path: str, max_lines: int = 50) -> dict[str, Any]:
8 """Build lightweight file context for extraction triggers.
10 Returns metadata and a snippet (first N lines) for significance scoring.
11 """
12 p = Path(file_path)
13 meta = {
14 "file_path": str(p),
15 "file_name": p.name,
16 "file_extension": p.suffix,
17 "size": p.stat().st_size if p.exists() else 0,
18 }
20 snippet = ""
21 try:
22 if p.exists() and p.is_file():
23 with p.open("r", encoding="utf-8", errors="ignore") as f:
24 lines: list[str] = []
25 for i, line in enumerate(f):
26 if i >= max_lines:
27 break
28 lines.append(line.rstrip("\n"))
29 snippet = "\n".join(lines)
30 except Exception:
31 snippet = ""
33 return {"metadata": meta, "snippet": snippet}