discovery
code_context_agent.tools.discovery ¶
Discovery tools for codebase analysis.
This module provides tools for file discovery, manifest creation, and context bundling using external tools like ripgrep and repomix.
create_file_manifest ¶
Create ignore-aware file manifest using ripgrep.
USE THIS TOOL: As the FIRST step in any codebase analysis workflow. Creates a safe inventory of files without reading contents.
DO NOT USE: - If you already have a manifest from a previous call in this session - If .code-context/files.all.txt exists and is recent
Generates a list of all files in the repository, respecting .gitignore and skipping hidden/binary files. Output is written to .code-context/files.all.txt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path | str | Absolute path to the repository root. | required |
Returns:
| Type | Description |
|---|---|
str | JSON with: |
str |
|
str |
|
Output Size: ~100 bytes JSON + manifest file (~50 bytes per file path)
Common Errors
- "rg not found": ripgrep not installed (install with: cargo install ripgrep)
- Empty manifest: Check if repo_path is correct and contains files
- Permission denied: Ensure read access to the repository
Example success
{"status": "success", "manifest_path": "/repo/.code-context/files.all.txt", "file_count": 847}
Source code in src/code_context_agent/tools/discovery.py
repomix_orientation ¶
Generate token-aware orientation snapshot without file contents.
USE THIS TOOL: After create_file_manifest to understand codebase structure and identify high-complexity areas via token distribution.
DO NOT USE: - If repo has >10K files (will auto-skip with recommendation) - If you only need to find specific files (use rg_search instead) - If .code-context/CONTEXT.orientation.md exists and repo hasn't changed
Uses repomix to create a metadata overview including directory structure and token distribution tree. Helps identify where code complexity lies without bundling actual content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path | str | Absolute path to the repository root. | required |
token_threshold | int | Minimum tokens to show in tree (filters noise). | 300 |
max_file_count | int | Maximum files allowed before skipping (default 10000). | 10000 |
Returns:
| Type | Description |
|---|---|
str | JSON with output path and status, or skipped status for large repos. |
Output Size
- Small repos (<500 files): ~5-20KB markdown
- Medium repos (500-2000 files): ~20-100KB markdown
- Large repos (2000-10000 files): ~100-500KB markdown
- Execution time: 5-60 seconds depending on repo size
Common Errors
- "repomix not found": Install with npm install -g repomix
- "skipped" status: Repo exceeds max_file_count, use --include patterns
- Timeout after 180s: Repo too large, reduce scope with glob patterns
Example success
{"status": "success", "output_path": "/repo/.code-context/CONTEXT.orientation.md"}
Example skipped
{"status": "skipped", "reason": "Repository has 15000 files (max: 10000)"}
Source code in src/code_context_agent/tools/discovery.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
repomix_bundle ¶
repomix_bundle(
file_list_path,
output_path,
compress=True,
include_diffs=False,
include_logs=False,
include_logs_count=50,
split_size=None,
truncate_base64=True,
remove_comments=False,
)
Pack curated files into markdown context bundle.
USE THIS TOOL: When you have a curated list of file paths and want to bundle their contents into a single markdown file for analysis.
DO NOT USE: - For initial exploration (use repomix_orientation first) - If you don't have a file list yet (use write_file_list first)
Takes a list of file paths and bundles their contents into a single markdown file using repomix. The --stdin flag reads paths from the provided file list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list_path | str | Path to file containing paths to pack (one per line). | required |
output_path | str | Output markdown file path. | required |
compress | bool | Use tree-sitter compression to reduce size. | True |
include_diffs | bool | Include git working tree + staged changes in the bundle. | False |
include_logs | bool | Include recent git commit history in the bundle. | False |
include_logs_count | int | Number of recent commits to include (only when include_logs=True). | 50 |
split_size | str | None | Split output into chunks of this size (e.g., "500kb", "2mb"). Useful for very large bundles that exceed context windows. | None |
truncate_base64 | bool | Truncate base64-encoded data to reduce token waste (default True). | True |
remove_comments | bool | Strip comments from source code for minimal structural output. | False |
Returns:
| Type | Description |
|---|---|
str | JSON with output path, file size, and status. |
Output Size: Varies by file count and content. Compressed bundles are ~30-50% smaller.
Common Errors
- "File list not found": Ensure file_list_path exists and has content
- Timeout after 300s: Too many/large files, reduce scope or use split_size
- "repomix not found": Install with npm install -g repomix
Example
result = repomix_bundle(".code-context/files.targeted.txt", ".code-context/CONTEXT.bundle.md") result = repomix_bundle( ... ".code-context/files.targeted.txt", ... ".code-context/CONTEXT.bundle.md", ... include_diffs=True, ... include_logs=True, ... include_logs_count=20, ... )
Source code in src/code_context_agent/tools/discovery.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
repomix_bundle_with_context ¶
repomix_bundle_with_context(
repo_path,
output_path,
include_patterns=None,
compress=True,
include_diffs=True,
include_logs=True,
include_logs_count=50,
truncate_base64=True,
)
Bundle repository files with git context (diffs and logs).
USE THIS TOOL: When you need a comprehensive snapshot of a repository that includes both file contents and recent git activity. Combines file bundling with git diffs and commit history in a single call.
DO NOT USE: - For initial exploration (use repomix_orientation first) - If you only need file contents without git context (use repomix_bundle) - For very large repos without include_patterns (will be slow/huge)
Unlike repomix_bundle which reads from a file list via --stdin, this tool operates directly on a repo path with optional glob include patterns. It always includes git context (diffs and/or logs) to provide a change-aware view of the codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path | str | Absolute path to the repository root. | required |
output_path | str | Output markdown file path. | required |
include_patterns | str | None | Comma-separated glob patterns to include (e.g., "src//*.py,tests//*.py"). If None, includes all files (respecting .gitignore). | None |
compress | bool | Use tree-sitter compression to reduce size. | True |
include_diffs | bool | Include git working tree + staged changes (default True). | True |
include_logs | bool | Include recent git commit history (default True). | True |
include_logs_count | int | Number of recent commits to include (only when include_logs=True). | 50 |
truncate_base64 | bool | Truncate base64-encoded data to reduce token waste (default True). | True |
Returns:
| Type | Description |
|---|---|
str | JSON with output path, file size, and status. |
Output Size
- Small repos with few changes: ~50-200KB
- Medium repos with active changes: ~200KB-1MB
- Execution time: 10-120 seconds depending on repo size and history
Common Errors
- "repomix not found": Install with npm install -g repomix
- Timeout after 300s: Use include_patterns to narrow scope
- Large output: Reduce include_logs_count or use include_patterns
Example
result = repomix_bundle_with_context( ... "/repo", ... ".code-context/CONTEXT.git-aware.md", ... include_patterns="src/**/*.py", ... include_logs_count=20, ... )
Source code in src/code_context_agent/tools/discovery.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
repomix_json_export ¶
Export repository structure as JSON for programmatic analysis.
USE THIS TOOL: When you need structured data about the repository rather than a human-readable markdown bundle. Useful for getting exact file counts, token distributions, and directory structure as machine-parseable data.
DO NOT USE: - For reading file contents (use repomix_bundle or read_file_bounded) - For initial high-level overview (use repomix_orientation) - If you only need file paths (use create_file_manifest)
Uses repomix --style json to produce structured output that can be parsed programmatically. The output includes file metadata without file contents (--no-files), keeping the output compact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path | str | Absolute path to the repository root. | required |
include_patterns | str | None | Comma-separated glob patterns to include (e.g., "src//*.py,tests//*.py"). | None |
Returns:
| Type | Description |
|---|---|
str | JSON with output_path and parsed metadata (total_files, total_tokens). |
Output Size: ~200 bytes JSON response + JSON file on disk (~1-50KB depending on repo).
Common Errors
- "repomix not found": Install with npm install -g repomix
- Timeout after 180s: Use include_patterns to narrow scope
- JSON parse error: repomix output format may have changed
Example success
{"status": "success", "output_path": "/repo/.code-context/structure.json", "total_files": 247, "total_tokens": 185420}
Example
result = repomix_json_export("/repo", include_patterns="src//*.py,tests//*.py")
Source code in src/code_context_agent/tools/discovery.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
repomix_compressed_signatures ¶
Extract code signatures and types from a repository using Tree-sitter compression.
Produces a minimal structural view: function/method signatures, class declarations, interface/type definitions, imports — with implementation bodies stripped. Also removes comments and empty lines for maximum token efficiency.
Supported languages: JavaScript, TypeScript, Python, Go, Rust, Java, C#, Ruby, PHP, Swift, C, C++, CSS, Solidity, Vue, Dart.
USE THIS TOOL: - For a quick structural overview of specific directories or file patterns - When you need to understand the API surface without reading implementations - To identify function signatures and types across a large codebase efficiently
DO NOT USE: - If you need full implementation details (use repomix_bundle) - For initial codebase overview (use repomix_orientation first) - For non-code files (compression only works on supported languages)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path | str | Absolute path to the repository root. | required |
include_patterns | str | None | Comma-separated glob patterns to include (e.g., "src//*.py,lib//*.ts"). | None |
output_path | str | None | Output path. Defaults to .code-context/CONTEXT.signatures.md | None |
Returns:
| Type | Description |
|---|---|
str | JSON with output path, file size, and status. |
Output Size
- Typically 60-80% smaller than full bundles due to body stripping + comment removal
- Small repos: ~5-30KB
- Medium repos: ~30-150KB
- Execution time: 5-60 seconds
Common Errors
- "repomix not found": Install with npm install -g repomix
- Timeout after 180s: Use include_patterns to narrow scope
- Empty output: No supported language files matched
Example
result = repomix_compressed_signatures("/repo", include_patterns="src/**/*.py") result = repomix_compressed_signatures("/repo") # All files
Source code in src/code_context_agent/tools/discovery.py
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
repomix_split_bundle ¶
Pack files into multiple split bundles for large codebases.
When a codebase is too large for a single context window, this tool splits the output into numbered files (e.g., output.1.md, output.2.md).
USE THIS TOOL: - When a previous repomix_bundle call produced output exceeding context limits - For large codebases where you want to process files in manageable chunks - When you need to parallelize analysis across multiple context windows
DO NOT USE: - For small repos that fit in a single bundle (use repomix_bundle) - For initial exploration (use repomix_orientation first) - If you don't have a file list yet (use write_file_list first)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list_path | str | Path to file containing paths to pack (one per line). | required |
output_dir | str | Directory for split output files. | required |
max_size | str | Maximum size per file (e.g., "500kb", "1mb", "2mb"). | '500kb' |
compress | bool | Use tree-sitter compression. | True |
Returns:
| Type | Description |
|---|---|
str | JSON with output directory, file count, and individual file paths. |
Output Size: Each split file will be at most max_size. Total output depends on input.
Common Errors
- "File list not found": Ensure file_list_path exists and has content
- Timeout after 300s: Reduce the number of files in the list
- "repomix not found": Install with npm install -g repomix
Example
result = repomix_split_bundle(".code-context/files.all.txt", ".code-context/splits/", max_size="1mb")
Source code in src/code_context_agent/tools/discovery.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
rg_search ¶
rg_search(
pattern,
repo_path,
glob=None,
file_type=None,
max_count=100,
context_lines=0,
count_only=False,
)
Search for pattern in repository using ripgrep.
USE THIS TOOL: - To find entrypoints (e.g., "def main", "createServer", "app.listen") - To locate specific functions, classes, or patterns - To discover imports and dependencies - When you know WHAT to search for but not WHERE - With count_only=True for precise occurrence counts across the entire codebase
DO NOT USE: - For listing all files (use create_file_manifest instead) - For reading file contents (use read_file_bounded instead) - For structural analysis (use lsp_document_symbols instead)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern | str | Regex pattern to search for. | required |
repo_path | str | Repository root path. | required |
glob | str | None | Optional glob filter (e.g., ".py", "src/**/.ts"). | None |
file_type | str | None | Optional file type (e.g., "py", "ts", "js"). | None |
max_count | int | Maximum matches to return per file (default 100). | 100 |
context_lines | int | Lines of context around matches (0-5 recommended). | 0 |
count_only | bool | Return only match counts per file (no match details). Uses rg --count for exact totals without truncation. | False |
Returns:
| Type | Description |
|---|---|
str | JSON with matches array containing path, line_number, and lines. |
str | When count_only=True: JSON with total_count and per-file counts. |
~200 bytes per match. Results capped at 500 lines.
count_only mode: ~50 bytes per file, no cap.
Pattern Tips
- Literal strings: "createServer" (no regex escaping needed)
- Function definitions: "def \w+(" or "function \w+("
- Class definitions: "class \w+"
- Imports: "import|from .* import"
- Case insensitive: Use "(?i)pattern"
Common Errors
- "rg not found": ripgrep not installed
- Empty matches with valid pattern: Try broader glob or check file_type
- Regex syntax error: Escape special chars like ( ) [ ] { }
Example success
{"status": "success", "pattern": "def main", "matches": [...], "match_count": 3}
Example count_only
{"status": "success", "pattern": "TODO", "total_count": 42, "files": {"src/main.py": 12, "src/utils.py": 30}, "file_count": 2}
Example searches
rg_search("def main", "/repo", glob="*.py") # Python entrypoints rg_search("createServer", "/repo", file_type="ts") # TS server setup rg_search("TODO|FIXME", "/repo", count_only=True) # Exact count across repo
Source code in src/code_context_agent/tools/discovery.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 | |
write_file_list ¶
Write a list of file paths to a file for repomix bundling.
Use this to create the curated file list before calling repomix_bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths | list[str] | List of file paths to include in the bundle. | required |
output_path | str | Path to write the file list. | required |
Returns:
| Type | Description |
|---|---|
str | JSON with output path and file count. |
Example
result = write_file_list(["src/main.ts", "src/utils.ts"], ".code-context/files.targeted.txt")
Source code in src/code_context_agent/tools/discovery.py
write_file ¶
Write content to a file in the output directory.
USE THIS TOOL: - To write CONTEXT.md and other analysis output files - To save narrated context, summaries, or generated documentation - For any file that needs to be created or overwritten in .code-context/
DO NOT USE: - For writing file lists (use write_file_list instead) - For writing to paths outside the analysis output directory
Security: Only allows writing to paths within the .code-context/ output directory to prevent unintended modifications to the analyzed repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path | str | Absolute path to the file to write. Must be within a .code-context/ directory. | required |
content | str | String content to write to the file. | required |
Returns:
| Type | Description |
|---|---|
str | JSON with status, path, and bytes written. |
Example
write_file("/repo/.code-context/CONTEXT.md", "# Project Context\n\n## Summary\n...")
Source code in src/code_context_agent/tools/discovery.py
read_file_bounded ¶
Read a file with bounded output for safe analysis.
USE THIS TOOL: - To deeply read and understand business logic files identified by graph analysis, LSP, or AST-grep. Essential for Phase 6.5 (Deep Read). - To read a SINGLE specific file when you know the exact path - To inspect implementation details after finding via rg_search - To read configuration files (package.json, pyproject.toml, etc.) - When you need line numbers for subsequent LSP calls - For files >500 lines, paginate using start_line (e.g., read 1-500, then 501-1000)
DO NOT USE: - To read multiple files at once (use repomix_bundle instead) - For initial exploration before Phase 3 (use repomix_orientation first) - For files >500 lines without specifying start_line for pagination
Reads file contents with line limits to prevent token overflow. Includes line numbers formatted as " 123| code here".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path | str | Absolute path to the file. | required |
max_lines | int | Maximum lines to read (default 500, reduce for large files). | 500 |
start_line | int | Starting line number (1-indexed, use for pagination). | 1 |
Returns:
| Type | Description |
|---|---|
str | JSON with content (with line numbers), path, lines_read, and truncated flag. |
Output Size: ~80 bytes per line average. 500 lines = ~40KB.
Common Errors
- "File not found": Check path is absolute and file exists
- "truncated": true: File has more lines, use start_line to paginate
- UnicodeDecodeError: File is binary, not suitable for text reading
Example success
{"status": "success", "path": "/repo/src/main.py", "content": " 1| ...", "start_line": 1, "lines_read": 150, "truncated": false}
Example pagination (reading lines 500-1000): >>> read_file_bounded("/repo/large_file.py", max_lines=500, start_line=500)
Source code in src/code_context_agent/tools/discovery.py
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 | |