codebase-index 2.1.2__py3-none-any.whl → 2.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codebase_index/__init__.py +1 -1
- codebase_index/cli.py +6 -5
- codebase_index/hooks.py +80 -18
- codebase_index/scaffold.py +10 -3
- codebase_index/storage/schema.sql +4 -0
- {codebase_index-2.1.2.dist-info → codebase_index-2.1.3.dist-info}/METADATA +2 -1
- {codebase_index-2.1.2.dist-info → codebase_index-2.1.3.dist-info}/RECORD +10 -10
- {codebase_index-2.1.2.dist-info → codebase_index-2.1.3.dist-info}/WHEEL +0 -0
- {codebase_index-2.1.2.dist-info → codebase_index-2.1.3.dist-info}/entry_points.txt +0 -0
- {codebase_index-2.1.2.dist-info → codebase_index-2.1.3.dist-info}/licenses/LICENSE +0 -0
codebase_index/__init__.py
CHANGED
codebase_index/cli.py
CHANGED
|
@@ -1074,10 +1074,11 @@ def install_hooks(
|
|
|
1074
1074
|
) -> None:
|
|
1075
1075
|
"""Make Claude Code search through the index by default.
|
|
1076
1076
|
|
|
1077
|
-
Adds a SessionStart note and a PreToolUse guard on Grep
|
|
1078
|
-
search of a session, before any codebase-index command, is sent back
|
|
1079
|
-
index command to run instead; repeating it lets it through. Both do
|
|
1080
|
-
a directory without an index. Other hooks in the settings file are
|
|
1077
|
+
Adds a SessionStart note and a PreToolUse guard on Grep, Bash and PowerShell: the
|
|
1078
|
+
first code search of a session, before any codebase-index command, is sent back
|
|
1079
|
+
with the index command to run instead; repeating it lets it through. Both do
|
|
1080
|
+
nothing in a directory without an index. Other hooks in the settings file are
|
|
1081
|
+
kept. Run it again after an upgrade to refresh an installed guard.
|
|
1081
1082
|
"""
|
|
1082
1083
|
from .config import find_root
|
|
1083
1084
|
from . import scaffold
|
|
@@ -1092,7 +1093,7 @@ def install_hooks(
|
|
|
1092
1093
|
verb = "removed from"
|
|
1093
1094
|
else:
|
|
1094
1095
|
changed = scaffold.install_guard_hooks(path)
|
|
1095
|
-
verb = "added to"
|
|
1096
|
+
verb = "added to or refreshed in"
|
|
1096
1097
|
if changed:
|
|
1097
1098
|
typer.echo(f"[install-hooks] {', '.join(changed)} {verb} {path}")
|
|
1098
1099
|
else:
|
codebase_index/hooks.py
CHANGED
|
@@ -4,17 +4,17 @@ Two events, both no-ops outside a repository that has an index:
|
|
|
4
4
|
|
|
5
5
|
- ``session-start`` adds one short note to the session context: this repository
|
|
6
6
|
is indexed, and which commands answer code questions.
|
|
7
|
-
- ``guard`` (PreToolUse on Grep and
|
|
7
|
+
- ``guard`` (PreToolUse on Grep, Bash and PowerShell) intercepts a code search the first time
|
|
8
8
|
in a session, before any ``codebase-index`` command has run, and names the
|
|
9
9
|
index command to use instead. It is a nudge, not a wall: searching for the
|
|
10
10
|
same term again lets it through, the first ``codebase-index`` command in the
|
|
11
11
|
session turns the guard off (even one that has yet to build the index), and
|
|
12
|
-
searches over docs, logs or config, or inside heredoc bodies,
|
|
13
|
-
intercepted.
|
|
12
|
+
searches over docs, logs or config, or inside heredoc and here-string bodies,
|
|
13
|
+
are never intercepted.
|
|
14
14
|
|
|
15
15
|
Measured on a 5.9k-file monorepo, an agent spent 19% fewer tokens with the index
|
|
16
16
|
than with grep (tests/eval/results/2026-09-24-agent-pilot.md), but agents reach
|
|
17
|
-
for Grep out of habit. The guard runs on every
|
|
17
|
+
for Grep out of habit. The guard runs on every shell call, so this module imports
|
|
18
18
|
only the standard library and does no index work beyond one small SQLite read.
|
|
19
19
|
|
|
20
20
|
Entry point: ``codebase-index-hook <event>`` reads the hook JSON on stdin and
|
|
@@ -39,6 +39,7 @@ from typing import Any, Optional
|
|
|
39
39
|
INDEX_REL = Path(".claude") / "cache" / "codebase-index" / "index.sqlite"
|
|
40
40
|
_STATE_DIR = "codebase-index-hooks"
|
|
41
41
|
_STATE_TTL_S = 2 * 24 * 3600
|
|
42
|
+
SHELL_TOOLS = ("Bash", "PowerShell")
|
|
42
43
|
|
|
43
44
|
# A shell segment that searches file contents: the first command of the segment
|
|
44
45
|
# (after `&&`, `||`, `;`, a newline or the start), so `ps aux | grep x` is not a
|
|
@@ -56,13 +57,22 @@ _HEREDOC = re.compile(
|
|
|
56
57
|
r".*?(?:^[ \t]*\2[ \t]*$|\Z)",
|
|
57
58
|
re.DOTALL | re.MULTILINE,
|
|
58
59
|
)
|
|
60
|
+
# PowerShell's heredoc: the closing `'@` or `"@` starts its own line.
|
|
61
|
+
_HERE_STRING = re.compile(r"""@(['"])[ \t]*\r?\n.*?^\1@""", re.DOTALL | re.MULTILINE)
|
|
59
62
|
_INDEX_CMD = re.compile(r"(?:^|[\s;&|/\\\"'])(?:codebase-index|cbx)(?:\.exe|\.ps1)?\s+\w")
|
|
60
|
-
# Searches over prose, logs and
|
|
63
|
+
# Searches over prose, logs, config, binaries and git internals are what grep is for;
|
|
64
|
+
# leave them alone.
|
|
61
65
|
_NON_CODE = re.compile(
|
|
62
|
-
r"\.(?:md|mdx|rst|txt|log|ya?ml|json|toml|ini|cfg|conf|csv|xml|properties|lock|env|html?
|
|
63
|
-
r"
|
|
66
|
+
r"\.(?:md|mdx|rst|txt|log|ya?ml|json|toml|ini|cfg|conf|csv|xml|properties|lock|env|html?"
|
|
67
|
+
r"|exe|dll|so|jar|class|bin|sqlite|db)\b"
|
|
68
|
+
r"|--include=\*?\.(?:md|txt|log|ya?ml|json|toml)"
|
|
69
|
+
r"|(?:^|[\s'\"/\\])\.git[\\/]",
|
|
64
70
|
re.IGNORECASE,
|
|
65
71
|
)
|
|
72
|
+
# Shell words of a search's arguments: a quoted string or a bare word.
|
|
73
|
+
_WORD = re.compile(r""""([^"]*)"|'([^']*)'|(\S+)""")
|
|
74
|
+
# An absolute path: a drive (C:\, C:/), an MSYS drive or POSIX root (/c/, /), or home.
|
|
75
|
+
_ABS_PATH = re.compile(r"^(?:[A-Za-z]:[\\/]|/|~(?:[\\/]|$)|\$HOME\b|\$\{HOME\})")
|
|
66
76
|
_NON_CODE_TYPES = {"md", "markdown", "txt", "log", "yaml", "yml", "json", "toml", "xml",
|
|
67
77
|
"csv", "ini", "config"}
|
|
68
78
|
|
|
@@ -144,25 +154,34 @@ def guard(payload: dict) -> Optional[dict]:
|
|
|
144
154
|
return None
|
|
145
155
|
tool = payload.get("tool_name") or ""
|
|
146
156
|
tool_input = payload.get("tool_input") or {}
|
|
147
|
-
if tool
|
|
157
|
+
if tool != "Grep" and tool not in SHELL_TOOLS:
|
|
148
158
|
return None
|
|
149
159
|
state = _State(str(payload.get("session_id") or "default"))
|
|
150
160
|
|
|
151
|
-
if tool
|
|
161
|
+
if tool in SHELL_TOOLS:
|
|
152
162
|
command = _HEREDOC.sub(lambda m: "<<" + m.group("rest"), str(tool_input.get("command") or ""))
|
|
163
|
+
if tool == "PowerShell":
|
|
164
|
+
command = _HERE_STRING.sub("''", command)
|
|
153
165
|
if _INDEX_CMD.search(command):
|
|
154
166
|
# Checked before the index exists: the first search is what builds it.
|
|
155
167
|
state.mark_used()
|
|
156
168
|
return None
|
|
157
|
-
|
|
169
|
+
root = find_index_root(_cwd(payload))
|
|
170
|
+
if root is None:
|
|
171
|
+
return None
|
|
172
|
+
pattern = _shell_search_pattern(command, root)
|
|
158
173
|
if pattern is None:
|
|
159
174
|
return None
|
|
160
175
|
else:
|
|
161
|
-
|
|
176
|
+
root = find_index_root(_cwd(payload))
|
|
177
|
+
if root is None or _grep_targets_non_code(tool_input):
|
|
178
|
+
return None
|
|
179
|
+
target = str(tool_input.get("path") or "")
|
|
180
|
+
if target and _outside_repo([target], root):
|
|
162
181
|
return None
|
|
163
182
|
pattern = str(tool_input.get("pattern") or "")
|
|
164
183
|
|
|
165
|
-
if state.used
|
|
184
|
+
if state.used:
|
|
166
185
|
return None
|
|
167
186
|
# Keyed by the search term, not the whole call: a retry rarely repeats the
|
|
168
187
|
# command byte for byte (a new description, another pipe, a regenerated script).
|
|
@@ -179,20 +198,63 @@ def guard(payload: dict) -> Optional[dict]:
|
|
|
179
198
|
}
|
|
180
199
|
|
|
181
200
|
|
|
182
|
-
def _shell_search_pattern(command: str) -> Optional[str]:
|
|
201
|
+
def _shell_search_pattern(command: str, root: Path) -> Optional[str]:
|
|
183
202
|
"""The search term of a content search in `command`, or None if there is none."""
|
|
184
203
|
for match in _SEARCH_CMD.finditer(command):
|
|
185
204
|
args = match.group("args")
|
|
186
205
|
if _NON_CODE.search(args):
|
|
187
206
|
continue
|
|
188
|
-
|
|
189
|
-
if
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
207
|
+
words = [next(g for g in m.groups() if g is not None) for m in _WORD.finditer(args)]
|
|
208
|
+
if match.group("cmd").lower() == "select-string":
|
|
209
|
+
pattern, paths = _select_string_terms(words)
|
|
210
|
+
else:
|
|
211
|
+
positional = [w for w in words if not w.startswith("-") and not w.isdigit()]
|
|
212
|
+
# The first positional word is the pattern, the rest are the paths searched.
|
|
213
|
+
paths = positional[1:]
|
|
214
|
+
quoted = re.search(r"""(['"])(.+?)\1""", args)
|
|
215
|
+
pattern = quoted.group(2) if quoted else (positional[0] if positional else "")
|
|
216
|
+
if not _outside_repo(paths, root):
|
|
217
|
+
return pattern
|
|
193
218
|
return None
|
|
194
219
|
|
|
195
220
|
|
|
221
|
+
def _select_string_terms(words: list[str]) -> tuple[str, list[str]]:
|
|
222
|
+
"""The pattern and paths of a Select-String call, named or positional."""
|
|
223
|
+
pattern: list[str] = []
|
|
224
|
+
paths: list[str] = []
|
|
225
|
+
positional: list[str] = []
|
|
226
|
+
target = None
|
|
227
|
+
for word in words:
|
|
228
|
+
if target is not None:
|
|
229
|
+
target.extend(word.split(",") if target is paths else [word])
|
|
230
|
+
target = None
|
|
231
|
+
elif word.lower() in ("-path", "-literalpath", "-lp"):
|
|
232
|
+
target = paths
|
|
233
|
+
elif word.lower() == "-pattern":
|
|
234
|
+
target = pattern
|
|
235
|
+
elif not word.startswith("-") and not word.isdigit():
|
|
236
|
+
positional.append(word)
|
|
237
|
+
if not pattern and positional:
|
|
238
|
+
pattern.append(positional.pop(0))
|
|
239
|
+
return (pattern[0] if pattern else ""), paths or positional
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _outside_repo(paths: list[str], root: Path) -> bool:
|
|
243
|
+
"""True when every path is absolute and none lies inside the indexed repository."""
|
|
244
|
+
if not paths or not all(_ABS_PATH.match(p) for p in paths):
|
|
245
|
+
return False
|
|
246
|
+
base = os.path.normcase(os.path.abspath(root))
|
|
247
|
+
for raw in paths:
|
|
248
|
+
path = re.sub(r"^(?:~|\$HOME\b|\$\{HOME\})", lambda _m: str(Path.home()), raw)
|
|
249
|
+
msys = re.match(r"^/([a-zA-Z])(/.*)?$", path)
|
|
250
|
+
if os.name == "nt" and msys:
|
|
251
|
+
path = f"{msys.group(1)}:{msys.group(2) or '/'}"
|
|
252
|
+
full = os.path.normcase(os.path.abspath(path))
|
|
253
|
+
if full == base or full.startswith(base.rstrip(os.sep) + os.sep):
|
|
254
|
+
return False
|
|
255
|
+
return True
|
|
256
|
+
|
|
257
|
+
|
|
196
258
|
def _grep_targets_non_code(tool_input: dict) -> bool:
|
|
197
259
|
kind = str(tool_input.get("type") or "").lower()
|
|
198
260
|
if kind in _NON_CODE_TYPES:
|
codebase_index/scaffold.py
CHANGED
|
@@ -292,7 +292,7 @@ GUARD_HOOKS: dict[str, dict] = {
|
|
|
292
292
|
"timeout": 5}],
|
|
293
293
|
},
|
|
294
294
|
"PreToolUse": {
|
|
295
|
-
"matcher": "Grep|Bash",
|
|
295
|
+
"matcher": "Grep|Bash|PowerShell",
|
|
296
296
|
"hooks": [{"type": "command", "command": "codebase-index-hook guard", "timeout": 5}],
|
|
297
297
|
},
|
|
298
298
|
}
|
|
@@ -305,7 +305,9 @@ def _is_guard_entry(entry: dict) -> bool:
|
|
|
305
305
|
def install_guard_hooks(settings_path: Path) -> list[str]:
|
|
306
306
|
"""Merge the index-first hooks into a Claude Code settings file.
|
|
307
307
|
|
|
308
|
-
Idempotent, and leaves every other hook in place.
|
|
308
|
+
Idempotent, and leaves every other hook in place. An installed guard gets the
|
|
309
|
+
current matcher, so upgrading only takes running this again. Returns the events
|
|
310
|
+
added or refreshed.
|
|
309
311
|
"""
|
|
310
312
|
settings: dict = {}
|
|
311
313
|
if settings_path.exists():
|
|
@@ -314,9 +316,14 @@ def install_guard_hooks(settings_path: Path) -> list[str]:
|
|
|
314
316
|
added = []
|
|
315
317
|
for event, entry in GUARD_HOOKS.items():
|
|
316
318
|
entries = hooks.setdefault(event, [])
|
|
317
|
-
|
|
319
|
+
installed = [e for e in entries if _is_guard_entry(e)]
|
|
320
|
+
if not installed:
|
|
318
321
|
entries.append(json.loads(json.dumps(entry)))
|
|
319
322
|
added.append(event)
|
|
323
|
+
elif "matcher" in entry and any(e.get("matcher") != entry["matcher"] for e in installed):
|
|
324
|
+
for existing in installed:
|
|
325
|
+
existing["matcher"] = entry["matcher"]
|
|
326
|
+
added.append(event)
|
|
320
327
|
if added:
|
|
321
328
|
settings_path.parent.mkdir(parents=True, exist_ok=True)
|
|
322
329
|
settings_path.write_text(json.dumps(settings, indent=2) + "\n", encoding="utf-8")
|
|
@@ -35,6 +35,9 @@ CREATE TABLE IF NOT EXISTS symbols (
|
|
|
35
35
|
CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name);
|
|
36
36
|
CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(file_id);
|
|
37
37
|
CREATE INDEX IF NOT EXISTS idx_symbols_kind ON symbols(kind);
|
|
38
|
+
-- Every deleted symbol looks up its children for ON DELETE SET NULL here and on
|
|
39
|
+
-- chunks.symbol_id; without an index each lookup is a full table scan.
|
|
40
|
+
CREATE INDEX IF NOT EXISTS idx_symbols_parent ON symbols(parent_id);
|
|
38
41
|
|
|
39
42
|
CREATE TABLE IF NOT EXISTS chunks (
|
|
40
43
|
id INTEGER PRIMARY KEY,
|
|
@@ -52,6 +55,7 @@ CREATE TABLE IF NOT EXISTS chunks (
|
|
|
52
55
|
symbol_names TEXT NOT NULL DEFAULT ''
|
|
53
56
|
);
|
|
54
57
|
CREATE INDEX IF NOT EXISTS idx_chunks_file ON chunks(file_id);
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_chunks_symbol ON chunks(symbol_id);
|
|
55
59
|
|
|
56
60
|
CREATE TABLE IF NOT EXISTS edges (
|
|
57
61
|
id INTEGER PRIMARY KEY,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: codebase-index
|
|
3
|
-
Version: 2.1.
|
|
3
|
+
Version: 2.1.3
|
|
4
4
|
Summary: A precise local codebase map for AI coding agents: find, trace, and predict with file-line evidence.
|
|
5
5
|
Project-URL: Homepage, https://github.com/denfry/codebase-index
|
|
6
6
|
Project-URL: Documentation, https://github.com/denfry/codebase-index/tree/main/docs
|
|
@@ -69,6 +69,7 @@ Description-Content-Type: text/markdown
|
|
|
69
69
|
<img src="https://img.shields.io/badge/MCP-stdio%20server-3fb950" alt="MCP stdio server">
|
|
70
70
|
<img src="https://img.shields.io/badge/network-off%20by%20default-3fb950" alt="No network by default">
|
|
71
71
|
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-8b949e" alt="MIT license"></a>
|
|
72
|
+
<a href="https://discord.gg/mVZcNUAGZt"><img src="https://img.shields.io/badge/Discord-Join%20the%20community-5865F2?logo=discord&logoColor=white" alt="Discord"></a>
|
|
72
73
|
</p>
|
|
73
74
|
|
|
74
75
|
<p align="center">
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
codebase_index/__init__.py,sha256=
|
|
1
|
+
codebase_index/__init__.py,sha256=x5gE26frKk40PgodIftahl3Lvt5624iepMeOo_w8F4c,240
|
|
2
2
|
codebase_index/__main__.py,sha256=wcCrL4PjG51r5wVKqJhcoJPTLfHW0wNbD31DrUN0MWI,28
|
|
3
|
-
codebase_index/cli.py,sha256=
|
|
3
|
+
codebase_index/cli.py,sha256=tCaPM0jl-OX50Jsz_5AMmMkEPXgW4_FWECeboAUB7Wk,45149
|
|
4
4
|
codebase_index/config.py,sha256=piyZ6zr2guEgMOPTJOabMLfk97be7sisTyXRVTdHvEk,4322
|
|
5
5
|
codebase_index/doctor.py,sha256=ely35q-NNfGYtRbnyyDjM0Y2UZgJ5tl8iw1KCP2wzKM,6517
|
|
6
|
-
codebase_index/hooks.py,sha256=
|
|
6
|
+
codebase_index/hooks.py,sha256=YgeTrDr79d1RNV32WFqEYAloYWxSRfLuCwxx7STL0Ps,13238
|
|
7
7
|
codebase_index/models.py,sha256=v9dkcNYz2IsgSmuWqopp8JQ38seqWic9mNpO-nRpqCQ,5734
|
|
8
|
-
codebase_index/scaffold.py,sha256=
|
|
8
|
+
codebase_index/scaffold.py,sha256=my2Ct559JviIN7eoOGfWbBARpatdFUwfVxkImQrxCtA,17244
|
|
9
9
|
codebase_index/service.py,sha256=k9zm2IqF0qs0DVZau0P8s3FrgvosyiMsSrixpgrXIFs,18024
|
|
10
10
|
codebase_index/skill_update.py,sha256=h8h_sDBXFklw5BSYbj4tuLp4O297nZWSXvEXsf7hiD8,4763
|
|
11
11
|
codebase_index/discovery/__init__.py,sha256=ktlylA5XvkPsDtPDjaw8E4PVByedhfNY186B7T0bUmk,518
|
|
@@ -72,11 +72,11 @@ codebase_index/skill_template/scripts/cbx.ps1,sha256=CweBQoixzm0S8dxe3IlA3rp5dbe
|
|
|
72
72
|
codebase_index/storage/__init__.py,sha256=KBsRFladZrQ9Wtux0cAGbMX6obRska5_sfmyA8NITrw,391
|
|
73
73
|
codebase_index/storage/db.py,sha256=mifzD_3hKf1CEN5Ey0vSRDKZYlUPqCG9ZDKrcQiVWQE,4278
|
|
74
74
|
codebase_index/storage/repo.py,sha256=F6MQDo23zwXECb0lI40oZ6Mz-uH9EKwUvIsBEGK4C9w,28129
|
|
75
|
-
codebase_index/storage/schema.sql,sha256=
|
|
75
|
+
codebase_index/storage/schema.sql,sha256=8d1X5lTMw1n9JU4uWoWc_pweZj1H-2Vfr7APPBjBDZs,5833
|
|
76
76
|
codebase_index/watch/__init__.py,sha256=Mc--hdTGZlf7s7UrtZ8zAf35EGvxBY9paXcuVVzA-j4,242
|
|
77
77
|
codebase_index/watch/watcher.py,sha256=f895qNBZkC6fCbEUWnxdpTH0Gy4RfFdg_hxLWJ9HZlc,3135
|
|
78
|
-
codebase_index-2.1.
|
|
79
|
-
codebase_index-2.1.
|
|
80
|
-
codebase_index-2.1.
|
|
81
|
-
codebase_index-2.1.
|
|
82
|
-
codebase_index-2.1.
|
|
78
|
+
codebase_index-2.1.3.dist-info/METADATA,sha256=cpVE4ikOLG8e6WBSCQzH8gWP9IRy0fltJqnu1dG2_Kc,17610
|
|
79
|
+
codebase_index-2.1.3.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
80
|
+
codebase_index-2.1.3.dist-info/entry_points.txt,sha256=TOy1PPDDP2vv0sd6skbkZFBxEDmEiSx0YspY0mF4fbI,186
|
|
81
|
+
codebase_index-2.1.3.dist-info/licenses/LICENSE,sha256=UkoUuqQfpFu4eib8UkTVAraPFC44A7pAIS7Jb23fNu8,1084
|
|
82
|
+
codebase_index-2.1.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|