A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Context packing: select ranked files within a token budget and write markdown.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from dataclasses import dataclass, field |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from agent_context.config import Config, DEFAULT_MAX_TOKENS |
| 9 | from agent_context.scanner import FileInfo, FileKind, ScanResult, scan_repository |
| 10 | from agent_context.scoring import ScoredFile, format_verbose_score, rank_files |
| 11 | from agent_context.security import redact_secrets, should_exclude_from_context |
| 12 | from agent_context.tokens import estimate_tokens_from_text, format_int |
| 13 | |
| 14 | DEFAULT_OUTPUT = Path(".agent-context/context.md") |
| 15 | |
| 16 | # Overhead reserved for markdown framing / headers. |
| 17 | _FRAME_OVERHEAD = 400 |
| 18 | # Soft cap for any single file as a fraction of the budget. |
| 19 | _MAX_FILE_FRACTION = 0.35 |
| 20 | # Absolute floor / ceiling for per-file inclusion. |
| 21 | _MIN_FILE_TOKENS = 80 |
| 22 | _MAX_FILE_TOKENS = 6_000 |
| 23 | |
| 24 | _FENCE_LANG = { |
| 25 | ".py": "python", |
| 26 | ".pyi": "python", |
| 27 | ".js": "javascript", |
| 28 | ".jsx": "jsx", |
| 29 | ".ts": "typescript", |
| 30 | ".tsx": "tsx", |
| 31 | ".mjs": "javascript", |
| 32 | ".cjs": "javascript", |
| 33 | ".go": "go", |
| 34 | ".rs": "rust", |
| 35 | ".java": "java", |
| 36 | ".kt": "kotlin", |
| 37 | ".rb": "ruby", |
| 38 | ".php": "php", |
| 39 | ".c": "c", |
| 40 | ".h": "c", |
| 41 | ".cpp": "cpp", |
| 42 | ".hpp": "cpp", |
| 43 | ".cs": "csharp", |
| 44 | ".swift": "swift", |
| 45 | ".sh": "bash", |
| 46 | ".bash": "bash", |
| 47 | ".sql": "sql", |
| 48 | ".md": "markdown", |
| 49 | ".json": "json", |
| 50 | ".yml": "yaml", |
| 51 | ".yaml": "yaml", |
| 52 | ".toml": "toml", |
| 53 | ".xml": "xml", |
| 54 | ".html": "html", |
| 55 | ".css": "css", |
| 56 | ".vue": "vue", |
| 57 | ".svelte": "svelte", |
| 58 | } |
| 59 | |
| 60 | |
| 61 | @dataclass |
| 62 | class PackedFile: |
| 63 | """A file selected for the context package.""" |
| 64 | |
| 65 | scored: ScoredFile |
| 66 | content: str |
| 67 | included_tokens: int |
| 68 | original_tokens: int |
| 69 | truncated: bool = False |
| 70 | |
| 71 | @property |
| 72 | def rel_path(self) -> str: |
| 73 | return self.scored.rel_path |
| 74 | |
| 75 | @property |
| 76 | def score(self) -> int: |
| 77 | return self.scored.score |
| 78 | |
| 79 | def to_dict(self) -> dict: |
| 80 | return { |
| 81 | "path": self.rel_path, |
| 82 | "score": self.score, |
| 83 | "kind": self.scored.file.kind.value, |
| 84 | "included_tokens": self.included_tokens, |
| 85 | "original_tokens": self.original_tokens, |
| 86 | "truncated": self.truncated, |
| 87 | } |
| 88 | |
| 89 | |
| 90 | @dataclass |
| 91 | class PackResult: |
| 92 | """Result of packing a repository for a task.""" |
| 93 | |
| 94 | task: str |
| 95 | root: Path |
| 96 | selected: list[PackedFile] = field(default_factory=list) |
| 97 | ranked: list[ScoredFile] = field(default_factory=list) |
| 98 | total_repo_tokens: int = 0 |
| 99 | total_included_tokens: int = 0 |
| 100 | budget: int = DEFAULT_MAX_TOKENS |
| 101 | output_path: Path | None = None |
| 102 | markdown: str = "" |
| 103 | warnings: list[str] = field(default_factory=list) |
| 104 | stack: list[str] = field(default_factory=list) |
| 105 | structure: str = "" |
| 106 | |
| 107 | @property |
| 108 | def reduction_pct(self) -> float | None: |
| 109 | if self.total_repo_tokens <= 0: |
| 110 | return None |
| 111 | saved = self.total_repo_tokens - self.total_included_tokens |
| 112 | return round(100.0 * saved / self.total_repo_tokens, 1) |
| 113 | |
| 114 | def to_dict(self) -> dict: |
| 115 | return { |
| 116 | "task": self.task, |
| 117 | "root": str(self.root), |
| 118 | "budget": self.budget, |
| 119 | "repository_tokens": self.total_repo_tokens, |
| 120 | "included_tokens": self.total_included_tokens, |
| 121 | "reduction_percent": self.reduction_pct, |
| 122 | "selected_files": [p.to_dict() for p in self.selected], |
| 123 | "top_matches": [ |
| 124 | {"score": s.score, "path": s.rel_path} |
| 125 | for s in self.ranked[:15] |
| 126 | ], |
| 127 | "output": str(self.output_path) if self.output_path else None, |
| 128 | "warnings": list(self.warnings), |
| 129 | "stack": list(self.stack), |
| 130 | } |
| 131 | |
| 132 | |
| 133 | def pack_context( |
| 134 | root: Path, |
| 135 | task: str, |
| 136 | *, |
| 137 | config: Config | None = None, |
| 138 | token_budget: int | None = None, |
| 139 | scan: ScanResult | None = None, |
| 140 | redact: bool | None = None, |
| 141 | ) -> PackResult: |
| 142 | """Rank files and select content within ``token_budget``.""" |
| 143 | config = config or Config.defaults() |
| 144 | budget = token_budget or config.max_tokens |
| 145 | if budget <= 0: |
| 146 | raise ValueError("token budget must be positive") |
| 147 | |
| 148 | do_redact = config.redact_secrets if redact is None else redact |
| 149 | result_scan = scan or scan_repository(root, config=config) |
| 150 | root = result_scan.root |
| 151 | |
| 152 | candidates = [ |
| 153 | f |
| 154 | for f in result_scan.context_candidates |
| 155 | if not should_exclude_from_context(f.path) |
| 156 | ] |
| 157 | |
| 158 | ranked = rank_files(candidates, task) |
| 159 | result = PackResult( |
| 160 | task=task, |
| 161 | root=root, |
| 162 | ranked=ranked, |
| 163 | total_repo_tokens=result_scan.total_tokens, |
| 164 | budget=budget, |
| 165 | warnings=list(result_scan.warnings), |
| 166 | stack=_detect_stack(result_scan), |
| 167 | structure=_structure_outline(result_scan), |
| 168 | ) |
| 169 | |
| 170 | remaining = max(0, budget - _FRAME_OVERHEAD) |
| 171 | selected: list[PackedFile] = [] |
| 172 | |
| 173 | for scored in ranked: |
| 174 | if remaining < _MIN_FILE_TOKENS: |
| 175 | break |
| 176 | |
| 177 | max_for_file = min( |
| 178 | _MAX_FILE_TOKENS, |
| 179 | max(_MIN_FILE_TOKENS, int(budget * _MAX_FILE_FRACTION)), |
| 180 | remaining, |
| 181 | ) |
| 182 | |
| 183 | packed = _load_and_trim( |
| 184 | scored, |
| 185 | max_tokens=max_for_file, |
| 186 | keywords=_task_keywords(task), |
| 187 | redact=do_redact, |
| 188 | ) |
| 189 | if packed is None: |
| 190 | continue |
| 191 | if packed.included_tokens <= 0: |
| 192 | continue |
| 193 | |
| 194 | selected.append(packed) |
| 195 | remaining -= packed.included_tokens |
| 196 | |
| 197 | result.selected = selected |
| 198 | result.total_included_tokens = sum(p.included_tokens for p in selected) + _FRAME_OVERHEAD |
| 199 | # Cap reported included tokens at something sensible. |
| 200 | result.total_included_tokens = min(result.total_included_tokens, budget + _FRAME_OVERHEAD) |
| 201 | result.markdown = render_context_markdown(result) |
| 202 | return result |
| 203 | |
| 204 | |
| 205 | def _task_keywords(task: str) -> list[str]: |
| 206 | from agent_context.scoring import tokenize_task |
| 207 | |
| 208 | return tokenize_task(task) |
| 209 | |
| 210 | |
| 211 | def _load_and_trim( |
| 212 | scored: ScoredFile, |
| 213 | *, |
| 214 | max_tokens: int, |
| 215 | keywords: list[str], |
| 216 | redact: bool, |
| 217 | ) -> PackedFile | None: |
| 218 | info = scored.file |
| 219 | try: |
| 220 | raw = info.path.read_text(encoding="utf-8", errors="replace") |
| 221 | except OSError: |
| 222 | return None |
| 223 | |
| 224 | original_tokens = estimate_tokens_from_text(raw) or info.estimated_tokens |
| 225 | text = redact_secrets(raw) if redact else raw |
| 226 | |
| 227 | if estimate_tokens_from_text(text) <= max_tokens: |
| 228 | return PackedFile( |
| 229 | scored=scored, |
| 230 | content=text, |
| 231 | included_tokens=estimate_tokens_from_text(text), |
| 232 | original_tokens=original_tokens, |
| 233 | truncated=False, |
| 234 | ) |
| 235 | |
| 236 | # Prefer keyword-relevant windows when truncating. |
| 237 | trimmed = _extract_relevant_sections(text, keywords, max_tokens) |
| 238 | return PackedFile( |
| 239 | scored=scored, |
| 240 | content=trimmed, |
| 241 | included_tokens=estimate_tokens_from_text(trimmed), |
| 242 | original_tokens=original_tokens, |
| 243 | truncated=True, |
| 244 | ) |
| 245 | |
| 246 | |
| 247 | def _extract_relevant_sections(text: str, keywords: list[str], max_tokens: int) -> str: |
| 248 | """Extract lines near keyword hits, or head+tail fallback.""" |
| 249 | lines = text.splitlines(keepends=True) |
| 250 | if not lines: |
| 251 | return text |
| 252 | |
| 253 | target_chars = max(200, int(max_tokens * 4 * 0.95)) |
| 254 | lower_keywords = [k.lower() for k in keywords] |
| 255 | |
| 256 | hit_indices: list[int] = [] |
| 257 | if lower_keywords: |
| 258 | for i, line in enumerate(lines): |
| 259 | low = line.lower() |
| 260 | if any(k in low for k in lower_keywords): |
| 261 | hit_indices.append(i) |
| 262 | |
| 263 | if hit_indices: |
| 264 | window = 8 |
| 265 | keep: set[int] = set() |
| 266 | for idx in hit_indices: |
| 267 | for j in range(max(0, idx - window), min(len(lines), idx + window + 1)): |
| 268 | keep.add(j) |
| 269 | |
| 270 | ordered = sorted(keep) |
| 271 | parts: list[str] = [] |
| 272 | chars = 0 |
| 273 | last = -2 |
| 274 | notice = ( |
| 275 | "[File truncated for context budget — showing sections matching task keywords]\n\n" |
| 276 | ) |
| 277 | parts.append(notice) |
| 278 | chars += len(notice) |
| 279 | |
| 280 | for idx in ordered: |
| 281 | if chars >= target_chars: |
| 282 | break |
| 283 | if idx != last + 1: |
| 284 | sep = "\n# ...\n\n" |
| 285 | parts.append(sep) |
| 286 | chars += len(sep) |
| 287 | parts.append(lines[idx]) |
| 288 | chars += len(lines[idx]) |
| 289 | last = idx |
| 290 | |
| 291 | body = "".join(parts) |
| 292 | footer = ( |
| 293 | f"\n\n[File truncated for context budget]\n" |
| 294 | f"Original estimated tokens: {format_int(estimate_tokens_from_text(text))}\n" |
| 295 | f"Included estimated tokens: {format_int(estimate_tokens_from_text(body))}\n" |
| 296 | ) |
| 297 | return body + footer |
| 298 | |
| 299 | # Fallback: head + tail |
| 300 | head_chars = target_chars * 2 // 3 |
| 301 | tail_chars = target_chars - head_chars |
| 302 | head = text[:head_chars] |
| 303 | tail = text[-tail_chars:] if tail_chars > 0 else "" |
| 304 | body = ( |
| 305 | head |
| 306 | + "\n\n[File truncated for context budget]\n\n" |
| 307 | + "# ...\n\n" |
| 308 | + tail |
| 309 | ) |
| 310 | footer = ( |
| 311 | f"\n\nOriginal estimated tokens: {format_int(estimate_tokens_from_text(text))}\n" |
| 312 | f"Included estimated tokens: {format_int(estimate_tokens_from_text(body))}\n" |
| 313 | ) |
| 314 | return body + footer |
| 315 | |
| 316 | |
| 317 | def render_context_markdown(result: PackResult) -> str: |
| 318 | """Render the packed context as markdown.""" |
| 319 | lines: list[str] = [ |
| 320 | "# Repository Context", |
| 321 | "", |
| 322 | "## Task", |
| 323 | "", |
| 324 | result.task.strip().capitalize() |
| 325 | if result.task and not result.task[:1].isupper() |
| 326 | else result.task.strip(), |
| 327 | "", |
| 328 | "## Repository", |
| 329 | "", |
| 330 | ] |
| 331 | |
| 332 | if result.stack: |
| 333 | for tech in result.stack: |
| 334 | lines.append(f"- {tech}") |
| 335 | else: |
| 336 | lines.append("- (stack not confidently detected)") |
| 337 | lines.append("") |
| 338 | |
| 339 | lines.extend( |
| 340 | [ |
| 341 | "## Structure", |
| 342 | "", |
| 343 | "```text", |
| 344 | result.structure.rstrip() or "(no structure outline)", |
| 345 | "```", |
| 346 | "", |
| 347 | "## Relevant Files", |
| 348 | "", |
| 349 | ] |
| 350 | ) |
| 351 | |
| 352 | main_files = [p for p in result.selected if p.scored.file.kind != FileKind.TEST] |
| 353 | test_files = [p for p in result.selected if p.scored.file.kind == FileKind.TEST] |
| 354 | |
| 355 | if not main_files and not test_files: |
| 356 | lines.append("_No relevant files selected within the token budget._") |
| 357 | lines.append("") |
| 358 | else: |
| 359 | for packed in main_files: |
| 360 | lines.extend(_render_file_section(packed)) |
| 361 | |
| 362 | if test_files: |
| 363 | lines.extend(["## Related Tests", ""]) |
| 364 | for packed in test_files: |
| 365 | lines.extend(_render_file_section(packed)) |
| 366 | |
| 367 | notes = _important_notes(result) |
| 368 | if notes: |
| 369 | lines.extend(["## Important Notes", ""]) |
| 370 | for note in notes: |
| 371 | lines.append(f"* {note}") |
| 372 | lines.append("") |
| 373 | |
| 374 | lines.extend( |
| 375 | [ |
| 376 | "## Context Statistics", |
| 377 | "", |
| 378 | f"Selected files: {len(result.selected)}", |
| 379 | f"Estimated tokens: {format_int(result.total_included_tokens)}", |
| 380 | f"Repository tokens: {format_int(result.total_repo_tokens)}", |
| 381 | ] |
| 382 | ) |
| 383 | if result.reduction_pct is not None: |
| 384 | lines.append(f"Reduction: {result.reduction_pct}%") |
| 385 | lines.append("") |
| 386 | return "\n".join(lines) |
| 387 | |
| 388 | |
| 389 | def _render_file_section(packed: PackedFile) -> list[str]: |
| 390 | lang = _FENCE_LANG.get(Path(packed.rel_path).suffix.lower(), "") |
| 391 | lines = [ |
| 392 | f"### {packed.rel_path}", |
| 393 | "", |
| 394 | f"Score: {packed.score}", |
| 395 | "", |
| 396 | ] |
| 397 | if packed.truncated: |
| 398 | lines.append( |
| 399 | f"_Truncated: original ~{format_int(packed.original_tokens)} tokens, " |
| 400 | f"included ~{format_int(packed.included_tokens)}._" |
| 401 | ) |
| 402 | lines.append("") |
| 403 | fence = lang |
| 404 | lines.append(f"```{fence}") |
| 405 | # Avoid breaking fences if content has triple backticks. |
| 406 | safe = packed.content.replace("```", "``\u200b`") |
| 407 | lines.append(safe.rstrip()) |
| 408 | lines.append("```") |
| 409 | lines.append("") |
| 410 | return lines |
| 411 | |
| 412 | |
| 413 | def _important_notes(result: PackResult) -> list[str]: |
| 414 | notes: list[str] = [] |
| 415 | dirs: set[str] = set() |
| 416 | for p in result.selected: |
| 417 | parent = str(Path(p.rel_path).parent) |
| 418 | if parent and parent != ".": |
| 419 | dirs.add(parent) |
| 420 | if dirs: |
| 421 | # Mention the most common parent dirs among top matches. |
| 422 | top_dirs = sorted(dirs)[:3] |
| 423 | for d in top_dirs: |
| 424 | notes.append(f"Related code appears under `{d}/`.") |
| 425 | tests = [p for p in result.selected if p.scored.file.kind == FileKind.TEST] |
| 426 | if tests: |
| 427 | notes.append( |
| 428 | "Related tests: " + ", ".join(f"`{t.rel_path}`" for t in tests[:5]) + "." |
| 429 | ) |
| 430 | if any(p.truncated for p in result.selected): |
| 431 | notes.append("One or more large files were truncated to fit the token budget.") |
| 432 | notes.append("Token counts are approximate (characters ÷ 4).") |
| 433 | return notes |
| 434 | |
| 435 | |
| 436 | def _detect_stack(scan: ScanResult) -> list[str]: |
| 437 | """Lightweight stack detection from filenames / content markers.""" |
| 438 | names = {f.path.name.lower() for f in scan.files} |
| 439 | rels = {f.rel_path.lower() for f in scan.files} |
| 440 | exts = {f.extension for f in scan.files} |
| 441 | stack: list[str] = [] |
| 442 | |
| 443 | def add(label: str) -> None: |
| 444 | if label not in stack: |
| 445 | stack.append(label) |
| 446 | |
| 447 | if "package.json" in names: |
| 448 | add("Node.js") |
| 449 | # Peek package.json for frameworks. |
| 450 | pkg = next((f for f in scan.files if f.path.name == "package.json"), None) |
| 451 | if pkg: |
| 452 | try: |
| 453 | text = pkg.path.read_text(encoding="utf-8", errors="replace")[:50_000] |
| 454 | except OSError: |
| 455 | text = "" |
| 456 | low = text.lower() |
| 457 | if "next" in low: |
| 458 | add("Next.js") |
| 459 | if "react" in low: |
| 460 | add("React") |
| 461 | if "vue" in low: |
| 462 | add("Vue") |
| 463 | if "express" in low: |
| 464 | add("Express") |
| 465 | if "nestjs" in low or "@nestjs" in low: |
| 466 | add("NestJS") |
| 467 | if "mongodb" in low or "mongoose" in low: |
| 468 | add("MongoDB") |
| 469 | if "redis" in low: |
| 470 | add("Redis") |
| 471 | if "prisma" in low: |
| 472 | add("Prisma") |
| 473 | if "pyproject.toml" in names or "requirements.txt" in names or "setup.py" in names: |
| 474 | add("Python") |
| 475 | if any("django" in r for r in rels) or "manage.py" in names: |
| 476 | add("Django") |
| 477 | if any("flask" in r for r in rels): |
| 478 | add("Flask") |
| 479 | if any("fastapi" in r for r in rels): |
| 480 | add("FastAPI") |
| 481 | if "cargo.toml" in names: |
| 482 | add("Rust") |
| 483 | if "go.mod" in names: |
| 484 | add("Go") |
| 485 | if "pom.xml" in names or "build.gradle" in names or "build.gradle.kts" in names: |
| 486 | add("Java") |
| 487 | if ".tsx" in exts or ".ts" in exts: |
| 488 | add("TypeScript") |
| 489 | elif ".jsx" in exts or ".js" in exts: |
| 490 | add("JavaScript") |
| 491 | if any(n.startswith("dockerfile") for n in names): |
| 492 | add("Docker") |
| 493 | |
| 494 | return stack |
| 495 | |
| 496 | |
| 497 | def _structure_outline(scan: ScanResult, *, max_dirs: int = 24) -> str: |
| 498 | """Shallow directory outline from discovered files.""" |
| 499 | dirs: set[str] = set() |
| 500 | for f in scan.files: |
| 501 | parts = Path(f.rel_path).parts |
| 502 | # Only real directory prefixes (file basename is never a "dir"). |
| 503 | if len(parts) >= 2: |
| 504 | dirs.add(parts[0] + "/") |
| 505 | if len(parts) >= 3: |
| 506 | dirs.add(f"{parts[0]}/{parts[1]}/") |
| 507 | |
| 508 | interesting = sorted( |
| 509 | d |
| 510 | for d in dirs |
| 511 | if d.rstrip("/").lower() |
| 512 | in { |
| 513 | "src", "lib", "app", "apps", "packages", "tests", "test", "docs", |
| 514 | "scripts", "cmd", "internal", "pkg", "components", "pages", |
| 515 | "services", "models", "api", "auth", "config", "utils", "hooks", |
| 516 | } |
| 517 | or any( |
| 518 | x in d.lower() |
| 519 | for x in ("src/", "tests/", "test/", "docs/", "app/", "api/", "auth/") |
| 520 | ) |
| 521 | ) |
| 522 | if not interesting: |
| 523 | interesting = sorted(dirs)[:max_dirs] |
| 524 | else: |
| 525 | interesting = interesting[:max_dirs] |
| 526 | |
| 527 | # Render as indented tree-ish text. |
| 528 | lines: list[str] = [] |
| 529 | seen_top: set[str] = set() |
| 530 | for d in interesting: |
| 531 | top = d.split("/")[0] |
| 532 | if top not in seen_top: |
| 533 | lines.append(f"{top}/") |
| 534 | seen_top.add(top) |
| 535 | rest = d[len(top) :].strip("/") |
| 536 | if rest: |
| 537 | first = rest.split("/")[0] |
| 538 | lines.append(f" {first}/") |
| 539 | # Deduplicate while preserving order. |
| 540 | out: list[str] = [] |
| 541 | seen: set[str] = set() |
| 542 | for line in lines: |
| 543 | if line not in seen: |
| 544 | seen.add(line) |
| 545 | out.append(line) |
| 546 | return "\n".join(out) |
| 547 | |
| 548 | |
| 549 | def write_context( |
| 550 | result: PackResult, |
| 551 | *, |
| 552 | output: Path | None = None, |
| 553 | stdout: bool = False, |
| 554 | ) -> Path | None: |
| 555 | """Write markdown to disk (atomically) or return None if stdout-only.""" |
| 556 | if stdout: |
| 557 | result.output_path = None |
| 558 | return None |
| 559 | |
| 560 | out = output or (result.root / DEFAULT_OUTPUT) |
| 561 | if not out.is_absolute(): |
| 562 | out = (Path.cwd() / out).resolve() |
| 563 | |
| 564 | out.parent.mkdir(parents=True, exist_ok=True) |
| 565 | tmp = out.with_suffix(out.suffix + ".tmp") |
| 566 | try: |
| 567 | tmp.write_text(result.markdown, encoding="utf-8") |
| 568 | tmp.replace(out) |
| 569 | finally: |
| 570 | if tmp.exists(): |
| 571 | try: |
| 572 | tmp.unlink() |
| 573 | except OSError: |
| 574 | pass |
| 575 | |
| 576 | result.output_path = out |
| 577 | return out |
| 578 | |
| 579 | |
| 580 | def format_pack_summary(result: PackResult, *, verbose: bool = False) -> str: |
| 581 | """Human-readable pack command summary.""" |
| 582 | lines = [ |
| 583 | "agent-context", |
| 584 | "", |
| 585 | "Task", |
| 586 | f" {result.task}", |
| 587 | "", |
| 588 | "Repository", |
| 589 | f" {format_int(result.total_repo_tokens)} estimated tokens", |
| 590 | "", |
| 591 | "Selected", |
| 592 | f" {len(result.selected)} files", |
| 593 | f" {format_int(result.total_included_tokens)} estimated tokens", |
| 594 | "", |
| 595 | "Top matches", |
| 596 | ] |
| 597 | for s in result.ranked[:8]: |
| 598 | lines.append(f" {s.score:>3} {s.rel_path}") |
| 599 | |
| 600 | if result.reduction_pct is not None: |
| 601 | lines.extend( |
| 602 | [ |
| 603 | "", |
| 604 | "Context reduction", |
| 605 | f" ~{result.reduction_pct}%", |
| 606 | ] |
| 607 | ) |
| 608 | |
| 609 | if result.output_path: |
| 610 | lines.extend(["", "Written to", f" {result.output_path}"]) |
| 611 | else: |
| 612 | lines.extend(["", "Output", " (stdout)"]) |
| 613 | |
| 614 | if verbose: |
| 615 | lines.append("") |
| 616 | lines.append("Score details") |
| 617 | lines.append("") |
| 618 | for s in result.ranked[:10]: |
| 619 | lines.append(format_verbose_score(s).rstrip()) |
| 620 | lines.append("") |
| 621 | |
| 622 | return "\n".join(lines) + "\n" |