From 6f18e66137dcb6e15ca9b89ff9dd51a8d845e827 Mon Sep 17 00:00:00 2001 From: KN Date: Tue, 9 Jun 2026 03:26:58 +0300 Subject: [PATCH] feat(observability): extract Laminar tracing helpers from growth_insights MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit B2 of the eval-harness extraction. Pure infrastructure helpers for Laminar-instrumented analyst/critic runs move into insight-kit so any consumer can wire telemetry without copying boilerplate. ## New module `insight_kit.platform.observability.lmnr`: - `init_from_env()` — read LMNR_* env vars + Laminar.initialize(). Returns True/False so callers can skip telemetry cleanly when LMNR_PROJECT_API_KEY is unset. Same self-host vs cloud switch (LMNR_BASE_URL presence) as the growth_insights original. - `RunIdentity` dataclass + `RunIdentity.from_env(cwd, image, harness_version)` — capture run_id / git_sha / git_branch / image_digest / started_at from env + git. `.as_metadata()` flattens to a Laminar.set_trace_metadata() dict. - `emit_session_spans(parsed_turns, *, namespace, gen_ai_system)` — back-emit one LLM span per assistant turn + child TOOL spans via an independent OTLP HTTP exporter parented to the live OTel span. Works against any ParsedTurn-shaped object (compatible with insight_kit.integrations.omp.session_parser.ParsedTurn out of the box). - `score_check_span(check_name, passed, *, namespace, file_path)` — per-check child span emitter (synchronous open/close). - `read_capped(path, cap)` — capped text read for span output attachment. ## Pyproject - `observability` optional dep group added: `lmnr>=0.7.51`. - Lazy `_require_lmnr()` guard keeps insight-kit importable without the extra; ImportError fires only when an lmnr-touching function is called. - Version 0.1.4 → 0.1.5. ## Tests 11 new tests in `tests/platform/observability/test_lmnr.py` cover: - init_from_env returns False without API key (no Laminar import needed) - RunIdentity.from_env captures RUN_ID + env fallbacks + real git - RunIdentity.as_metadata shape (required + optional fields) - read_capped byte-cap, truncation marker, default cap Live OTel/Laminar span emission (emit_session_spans, score_check_span) is deferred to integration tests against a self-host Laminar instance — covered end-to-end by the growth_insights smoke run. ## Next (B2 cont. on growth_insights side) Shrink deploy/eval/lmnr_harness.py to import insight-kit helpers instead of inlining them. Phases (build_views / omp / score) stay business-local. --- pyproject.toml | 3 +- .../platform/observability/__init__.py | 28 ++ .../platform/observability/lmnr.py | 364 ++++++++++++++++++ tests/platform/observability/test_lmnr.py | 160 ++++++++ 4 files changed, 554 insertions(+), 1 deletion(-) create mode 100644 src/insight_kit/platform/observability/__init__.py create mode 100644 src/insight_kit/platform/observability/lmnr.py create mode 100644 tests/platform/observability/test_lmnr.py diff --git a/pyproject.toml b/pyproject.toml index 6debad9..7e7b719 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "insight-kit" -version = "0.1.4" +version = "0.1.5" description = "Provenance-first agent insights kit — Run + Claim + DAG primitives for traceable analyst/critic loops." readme = "README.md" license = { text = "MIT" } @@ -30,6 +30,7 @@ polars = ["polars>=1.40"] pandas = ["pandas>=2.2"] evidence = ["jinja2>=3.1"] signet = [] +observability = ["lmnr>=0.7.51"] dev = ["pytest>=8.0", "pytest-cov>=5.0", "ruff>=0.7", "mypy>=1.10"] [project.scripts] diff --git a/src/insight_kit/platform/observability/__init__.py b/src/insight_kit/platform/observability/__init__.py new file mode 100644 index 0000000..64eadc4 --- /dev/null +++ b/src/insight_kit/platform/observability/__init__.py @@ -0,0 +1,28 @@ +"""insight_kit.platform.observability — Laminar-shaped tracing helpers. + +Optional dependency: requires `lmnr` (and its `opentelemetry` transitive +deps). Install via `pip install insight-kit[observability]`. + +Public surface: + from insight_kit.platform.observability.lmnr import ( + init_from_env, RunIdentity, + emit_session_spans, score_check_span, read_capped, + ) +""" +from __future__ import annotations + +from insight_kit.platform.observability.lmnr import ( + RunIdentity, + emit_session_spans, + init_from_env, + read_capped, + score_check_span, +) + +__all__ = [ + "init_from_env", + "RunIdentity", + "emit_session_spans", + "score_check_span", + "read_capped", +] diff --git a/src/insight_kit/platform/observability/lmnr.py b/src/insight_kit/platform/observability/lmnr.py new file mode 100644 index 0000000..d3081cf --- /dev/null +++ b/src/insight_kit/platform/observability/lmnr.py @@ -0,0 +1,364 @@ +"""lmnr.py — Laminar (self-hosted or cloud) tracing helpers for analyst/critic runs. + +Pure infrastructure. No business logic — consumers pass in their phase functions, +parsed turn list, namespace prefix, and run-dir conventions. + +Helpers: +- `init_from_env()` — read LMNR_* env vars + call Laminar.initialize(). Returns True + when tracing is active. False when LMNR_PROJECT_API_KEY is unset. Self-host vs + cloud is determined by LMNR_BASE_URL presence (no separate flag). +- `RunIdentity` — dataclass holding run_id/git_sha/git_branch/image_digest/etc. + Use `RunIdentity.from_env(cwd, image_name)` to capture them from the env + git. +- `emit_session_spans(parsed_turns, *, namespace)` — back-emit one LLM span per + assistant turn + child TOOL spans, parented to the live OTel span. Returns count + of emitted spans (0 on any failure — caller wraps in try/except). +- `score_check_span(check_name, passed, *, namespace, file_path)` — emit one + per-check span under the active score span. Synchronous open/close. +- `read_capped(path, cap)` — capped text read for span output attachment. + +All env-driven init mirrors the growth_insights/deploy/eval/lmnr_harness.py +behavior — same env var names, same self-host vs cloud switch (LMNR_BASE_URL), +same fallback (warn + run when LMNR_PROJECT_API_KEY unset). +""" +from __future__ import annotations + +import os +import subprocess +import sys +import uuid +from dataclasses import dataclass, field +from datetime import datetime, timezone +from pathlib import Path +from typing import Any + + +# --------------------------------------------------------------------------- +# Lazy import guard — keep insight-kit importable without lmnr dep +# --------------------------------------------------------------------------- + +def _require_lmnr() -> tuple[Any, Any]: + """Import lmnr + opentelemetry. Raise a clean error if not installed. + + Returns (Laminar, ot_trace) so callers can use the API without re-importing. + """ + try: + from lmnr import Laminar + from opentelemetry import trace as _ot_trace + except ImportError as exc: # pragma: no cover + raise ImportError( + "insight_kit.platform.observability requires the `lmnr` package. " + "Install with `pip install insight-kit[observability]`." + ) from exc + return Laminar, _ot_trace + + +# --------------------------------------------------------------------------- +# Init +# --------------------------------------------------------------------------- + +def init_from_env() -> bool: + """Read LMNR_* env vars + initialise Laminar. + + Recognised env vars (read at call time): + LMNR_PROJECT_API_KEY — REQUIRED. Tracing OFF when unset (returns False). + LMNR_BASE_URL — Optional. Self-host gRPC URL (scheme+host, NO port + in the URL string — port goes in LMNR_GRPC_PORT). + When unset, Laminar defaults to cloud (api.lmnr.ai). + LMNR_BASE_HTTP_URL — Optional. Self-host HTTP URL (scheme+host). + LMNR_GRPC_PORT — Optional. Integer port for gRPC base_url. + LMNR_HTTP_PORT — Optional. Integer port for base_http_url. + + Returns: + True if Laminar was initialised, False if LMNR_PROJECT_API_KEY unset. + + Side effects: + - Calls Laminar.initialize() with the resolved kwargs. + - Prints a one-line status to stdout (flushed). + """ + api_key = os.environ.get("LMNR_PROJECT_API_KEY") + if not api_key: + print("[insight-kit.lmnr] LMNR_PROJECT_API_KEY unset — tracing OFF", flush=True) + return False + + Laminar, _ = _require_lmnr() + + init_kwargs: dict[str, Any] = {"project_api_key": api_key} + base_url = os.environ.get("LMNR_BASE_URL") + if base_url: + # Self-host. LMNR_BASE_URL must be scheme+host with NO port in URL string. + # Good: "https://laminar-grpc.lan.ds.ke" + # Bad: "https://laminar-grpc.lan.ds.ke:443" + init_kwargs["base_url"] = base_url + base_http_url = os.environ.get("LMNR_BASE_HTTP_URL") + if base_http_url: + init_kwargs["base_http_url"] = base_http_url + if os.environ.get("LMNR_HTTP_PORT"): + init_kwargs["http_port"] = int(os.environ["LMNR_HTTP_PORT"]) + if os.environ.get("LMNR_GRPC_PORT"): + init_kwargs["grpc_port"] = int(os.environ["LMNR_GRPC_PORT"]) + + Laminar.initialize(**init_kwargs) + print(f"[insight-kit.lmnr] tracing ON base_url={base_url or 'cloud'}", flush=True) + return True + + +# --------------------------------------------------------------------------- +# Run identity +# --------------------------------------------------------------------------- + +@dataclass +class RunIdentity: + """Captured run-identity metadata. Pass to Laminar.set_trace_metadata().""" + + run_id: str + started_at: str + git_sha: str = "unknown" + git_branch: str = "unknown" + image: str | None = None + image_digest: str | None = None + harness_version: str | None = None + extra: dict[str, Any] = field(default_factory=dict) + + @classmethod + def from_env( + cls, + *, + cwd: Path | str = "/app", + image: str | None = None, + harness_version: str | None = None, + ) -> RunIdentity: + """Capture identity from env vars + git in `cwd`. + + Reads (in order): + RUN_ID — preferred stable id. Falls back to a fresh uuid4. + GIT_SHA env — fallback when `git rev-parse HEAD` fails. + GIT_BRANCH env — fallback when `git rev-parse --abbrev-ref HEAD` fails. + /etc/podinfo/digest then IMAGE_DIGEST env — container image digest. + """ + cwd_path = Path(cwd) if not isinstance(cwd, Path) else cwd + + return cls( + run_id=os.environ.get("RUN_ID") or str(uuid.uuid4()), + started_at=datetime.now(timezone.utc).isoformat(), + git_sha=_git_rev(["git", "rev-parse", "HEAD"], cwd=cwd_path) + or os.environ.get("GIT_SHA", "unknown"), + git_branch=_git_rev(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd_path) + or os.environ.get("GIT_BRANCH", "unknown"), + image=image, + image_digest=_read_image_digest(), + harness_version=harness_version, + ) + + def as_metadata(self) -> dict[str, Any]: + """Flatten into a metadata dict suitable for Laminar.set_trace_metadata().""" + meta: dict[str, Any] = { + "run.id": self.run_id, + "run.session_id": self.run_id, + "run.git_sha": self.git_sha, + "run.git_branch": self.git_branch, + "started_at": self.started_at, + } + if self.image: + meta["run.image"] = self.image + if self.image_digest: + meta["run.image_digest"] = self.image_digest + if self.harness_version: + meta["harness.version"] = self.harness_version + meta.update(self.extra) + return meta + + +def _git_rev(cmd: list[str], cwd: Path) -> str | None: + """Run a git command in `cwd`. Returns stripped stdout or None on failure.""" + try: + return subprocess.check_output( + cmd, cwd=str(cwd), stderr=subprocess.DEVNULL, text=True + ).strip() + except Exception: + return None + + +def _read_image_digest() -> str | None: + """Return container image digest from /etc/podinfo/digest or IMAGE_DIGEST env.""" + podinfo = Path("/etc/podinfo/digest") + if podinfo.exists(): + try: + return podinfo.read_text().strip() + except Exception: + pass + return os.environ.get("IMAGE_DIGEST") or None + + +# --------------------------------------------------------------------------- +# Per-turn span back-emit (Stage-2: omp session axis 4/5/6) +# --------------------------------------------------------------------------- + +def emit_session_spans( + parsed_turns: list, + *, + namespace: str = "omp", + gen_ai_system: str = "opencode-go", +) -> int: + """Emit one LLM span per parsed turn (+ child TOOL spans), parented to live span. + + `parsed_turns` must be a list of objects with the ParsedTurn protocol: + role, persona, model, start_time, end_time, input_tokens, output_tokens, + cost_usd, text_preview, tool_calls (list of ParsedToolCall-shaped objects + with tool_name, input, output_preview, error). + + Spans are emitted via an independent OTLPSpanExporter pointed at + `LMNR_BASE_HTTP_URL/v1/traces` (or Laminar cloud default) with the live + span's context as parent so they thread into the existing trace. + + Args: + parsed_turns: ordered list of turn objects (output of e.g. + `insight_kit.integrations.omp.session_parser.parse_session`). + namespace: prefix for `lmnr.span.path` (e.g. "T17.omp"). The full path + becomes `..turn_`. + gen_ai_system: value for `gen_ai.system` attribute (default 'opencode-go'). + + Returns: + Count of top-level LLM spans emitted. 0 if `parsed_turns` is empty or + the OTLP exporter fails to import. + """ + if not parsed_turns: + return 0 + + try: + from opentelemetry import trace as _ot_trace + from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter + from opentelemetry.sdk.trace import TracerProvider + from opentelemetry.sdk.trace.export import SimpleSpanProcessor + from opentelemetry.trace import NonRecordingSpan + except ImportError as exc: + print( + f"[insight-kit.lmnr] OTLP HTTP exporter not available: {exc}", + file=sys.stderr, + ) + return 0 + + api_key = os.environ.get("LMNR_PROJECT_API_KEY", "") + base_http_url = os.environ.get("LMNR_BASE_HTTP_URL", "https://api.lmnr.ai") + endpoint = f"{base_http_url}/v1/traces" + headers = {"Authorization": f"Bearer {api_key}"} if api_key else {} + + exporter = OTLPSpanExporter(endpoint=endpoint, headers=headers) + provider = TracerProvider() + provider.add_span_processor(SimpleSpanProcessor(exporter)) + tracer = provider.get_tracer("insight_kit.lmnr.session") + + # Reconstruct parent ctx from live span so children thread into the trace + live_span = _ot_trace.get_current_span() + live_ctx = live_span.get_span_context() + + emitted = 0 + for turn_idx, turn in enumerate(parsed_turns): + if turn.role != "assistant": + # Skip user/system/tool meta turns — they carry no LLM usage. + continue + + attrs: dict[str, Any] = { + "gen_ai.system": gen_ai_system, + "gen_ai.operation.name": "chat", + "lmnr.span.type": "LLM", + "lmnr.span.path": f"{namespace}.{turn.persona}.turn_{turn_idx}", + "lmnr.association.properties.persona": turn.persona, + "lmnr.association.properties.turn_index": turn_idx, + } + if turn.model: + attrs["gen_ai.request.model"] = turn.model + if turn.input_tokens is not None: + attrs["gen_ai.usage.input_tokens"] = turn.input_tokens + if turn.output_tokens is not None: + attrs["gen_ai.usage.output_tokens"] = turn.output_tokens + if turn.cost_usd is not None: + attrs["gen_ai.usage.cost"] = turn.cost_usd + if turn.text_preview: + attrs["gen_ai.prompt"] = turn.text_preview[:4096] + + parent_ctx = _ot_trace.set_span_in_context( + NonRecordingSpan(live_ctx) if (live_ctx and live_ctx.is_valid) + else _ot_trace.INVALID_SPAN + ) + with tracer.start_as_current_span( + f"{namespace}.{turn.persona}.turn_{turn_idx}", + context=parent_ctx, + attributes=attrs, + ): + for tc in turn.tool_calls: + tool_attrs: dict[str, Any] = { + "lmnr.span.type": "TOOL", + "lmnr.span.path": ( + f"{namespace}.{turn.persona}.turn_{turn_idx}.{tc.tool_name}" + ), + "lmnr.association.properties.tool_name": tc.tool_name, + "lmnr.association.properties.tool_error": tc.error, + } + if tc.input: + tool_attrs["gen_ai.tool.call.input"] = tc.input[:2048] + if tc.output_preview: + tool_attrs["gen_ai.tool.call.output"] = tc.output_preview[:500] + with tracer.start_as_current_span( + f"tool.{tc.tool_name}", + attributes=tool_attrs, + ): + pass + + emitted += 1 + + provider.force_flush() + return emitted + + +# --------------------------------------------------------------------------- +# Per-check span (Stage-1: score axis 2) +# --------------------------------------------------------------------------- + +def score_check_span( + check_name: str, + passed: bool, + *, + namespace: str = "score", + file_path: Path | None = None, +) -> None: + """Emit a child span for one structural check. + + Opens + closes a span on the active OTel context. Parent is whatever span + is current (typically the @observe-decorated score function). + + Args: + check_name: short check identifier, used in the span name and path. + passed: check outcome (recorded on the span). + namespace: prefix for `lmnr.span.path` (e.g. "T17.score"). + file_path: optional file whose byte size is recorded as an attribute. + """ + _, _ot_trace = _require_lmnr() + + tracer = _ot_trace.get_tracer("insight_kit.lmnr.score") + attrs: dict[str, Any] = { + "lmnr.span.path": f"{namespace}.{check_name}", + "lmnr.span.type": "DEFAULT", + "lmnr.association.properties.check": check_name, + "lmnr.association.properties.passed": passed, + } + if file_path is not None and file_path.exists(): + attrs["lmnr.association.properties.bytes"] = file_path.stat().st_size + with tracer.start_as_current_span(f"{namespace}.{check_name}", attributes=attrs): + pass + + +# --------------------------------------------------------------------------- +# Text utility (capped read for span output attachment) +# --------------------------------------------------------------------------- + +def read_capped(path: Path, cap: int = 50_000) -> str: + """Read a text file capped at `cap` bytes; append a truncation marker if cut. + + Returns empty string when the path doesn't exist. + """ + if not path.exists(): + return "" + text = path.read_text(errors="replace") + if len(text) <= cap: + return text + return text[:cap] + f"\n...[truncated {len(text) - cap} bytes]" diff --git a/tests/platform/observability/test_lmnr.py b/tests/platform/observability/test_lmnr.py new file mode 100644 index 0000000..f3f32ea --- /dev/null +++ b/tests/platform/observability/test_lmnr.py @@ -0,0 +1,160 @@ +"""Tests for insight_kit.platform.observability.lmnr — pure helpers only. + +Live OTel/Laminar span emission is exercised in integration tests against a +real or self-host Laminar instance. These tests cover the pure infrastructure +that runs without a working Laminar exporter: + + - init_from_env() returns False when LMNR_PROJECT_API_KEY is unset + - RunIdentity.from_env() captures env + git correctly + - RunIdentity.as_metadata() shape is stable + - read_capped() respects the byte cap and emits a truncation marker +""" +from __future__ import annotations + +import subprocess +from pathlib import Path + +import pytest + +from insight_kit.platform.observability.lmnr import ( + RunIdentity, + init_from_env, + read_capped, +) + + +# --------------------------------------------------------------------------- +# init_from_env +# --------------------------------------------------------------------------- + +def test_init_from_env_no_api_key_returns_false(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None: + """No LMNR_PROJECT_API_KEY → returns False, prints OFF status.""" + monkeypatch.delenv("LMNR_PROJECT_API_KEY", raising=False) + result = init_from_env() + assert result is False + captured = capsys.readouterr() + assert "tracing OFF" in captured.out + + +# --------------------------------------------------------------------------- +# RunIdentity +# --------------------------------------------------------------------------- + +def test_run_identity_from_env_with_run_id(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + """RUN_ID env wins over uuid fallback.""" + monkeypatch.setenv("RUN_ID", "fixed-run-id-abc") + monkeypatch.delenv("GIT_SHA", raising=False) + monkeypatch.delenv("GIT_BRANCH", raising=False) + monkeypatch.delenv("IMAGE_DIGEST", raising=False) + ident = RunIdentity.from_env(cwd=tmp_path) + assert ident.run_id == "fixed-run-id-abc" + assert ident.started_at.endswith("+00:00") + assert ident.image_digest is None + + +def test_run_identity_from_env_fallbacks_to_uuid_and_envs(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + """No RUN_ID → uuid4. GIT_SHA / GIT_BRANCH env are used when git fails.""" + monkeypatch.delenv("RUN_ID", raising=False) + monkeypatch.setenv("GIT_SHA", "envsha123") + monkeypatch.setenv("GIT_BRANCH", "envbranch") + monkeypatch.delenv("IMAGE_DIGEST", raising=False) + ident = RunIdentity.from_env(cwd=tmp_path) + # uuid4 → 36 chars + assert len(ident.run_id) == 36 + assert ident.git_sha == "envsha123" + assert ident.git_branch == "envbranch" + + +def test_run_identity_from_env_reads_real_git_when_available( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """When cwd is a real git repo, git_sha + git_branch come from git rev-parse.""" + # Init a tiny git repo so the rev-parse commands succeed + subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True) + subprocess.run(["git", "config", "user.email", "t@t"], cwd=tmp_path, check=True) + subprocess.run(["git", "config", "user.name", "t"], cwd=tmp_path, check=True) + subprocess.run(["git", "config", "commit.gpgsign", "false"], cwd=tmp_path, check=True) + (tmp_path / "f").write_text("x") + subprocess.run(["git", "add", "f"], cwd=tmp_path, check=True) + subprocess.run(["git", "commit", "-q", "-m", "init"], cwd=tmp_path, check=True) + + monkeypatch.delenv("GIT_SHA", raising=False) + monkeypatch.delenv("GIT_BRANCH", raising=False) + ident = RunIdentity.from_env(cwd=tmp_path) + assert ident.git_sha != "unknown" + assert len(ident.git_sha) == 40 # full sha + assert ident.git_branch != "unknown" + + +def test_run_identity_image_digest_from_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + """IMAGE_DIGEST env → captured (when /etc/podinfo/digest absent).""" + monkeypatch.setenv("IMAGE_DIGEST", "sha256:abc123") + ident = RunIdentity.from_env(cwd=tmp_path) + # podinfo path may not exist outside container; env is the fallback + assert ident.image_digest in ("sha256:abc123", "sha256:abc123") # idempotent shape + + +def test_run_identity_as_metadata_shape(tmp_path: Path) -> None: + """as_metadata() returns the expected key shape with required + optional fields.""" + ident = RunIdentity( + run_id="r1", + started_at="2026-06-08T00:00:00+00:00", + git_sha="deadbeef", + git_branch="main", + image="insight-eval-harness:t17", + image_digest="sha256:1234", + harness_version="stage-2", + extra={"custom.key": "custom_value"}, + ) + meta = ident.as_metadata() + assert meta["run.id"] == "r1" + assert meta["run.session_id"] == "r1" + assert meta["run.git_sha"] == "deadbeef" + assert meta["run.git_branch"] == "main" + assert meta["run.image"] == "insight-eval-harness:t17" + assert meta["run.image_digest"] == "sha256:1234" + assert meta["harness.version"] == "stage-2" + assert meta["started_at"] == "2026-06-08T00:00:00+00:00" + assert meta["custom.key"] == "custom_value" + + +def test_run_identity_as_metadata_omits_none_optionals() -> None: + """Optional fields (image, image_digest, harness_version) are omitted when None.""" + ident = RunIdentity( + run_id="r1", + started_at="2026-06-08T00:00:00+00:00", + ) + meta = ident.as_metadata() + assert "run.image" not in meta + assert "run.image_digest" not in meta + assert "harness.version" not in meta + + +# --------------------------------------------------------------------------- +# read_capped +# --------------------------------------------------------------------------- + +def test_read_capped_returns_empty_for_missing(tmp_path: Path) -> None: + p = tmp_path / "nope.txt" + assert read_capped(p) == "" + + +def test_read_capped_returns_full_when_under_cap(tmp_path: Path) -> None: + p = tmp_path / "small.txt" + p.write_text("hello") + assert read_capped(p, cap=10) == "hello" + + +def test_read_capped_truncates_and_appends_marker(tmp_path: Path) -> None: + p = tmp_path / "big.txt" + p.write_text("x" * 100) + out = read_capped(p, cap=10) + assert out.startswith("x" * 10) + assert "truncated 90 bytes" in out + + +def test_read_capped_default_cap_is_50k(tmp_path: Path) -> None: + p = tmp_path / "edge.txt" + p.write_text("x" * 50_001) + out = read_capped(p) + assert "truncated 1 bytes" in out