@allansantos-dev/smart-tool 0.9.21 → 0.9.23
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.
- package/CHANGELOG.md +36 -0
- package/README.md +10 -4
- package/client_hooks.py +68 -37
- package/code_graph.py +33 -6
- package/code_impact.py +1 -1
- package/dossier.py +130 -0
- package/duplicates.py +18 -1
- package/endpoint_sync.py +10 -6
- package/hook_decision.py +120 -46
- package/hooks/pretool_router.py +2 -1
- package/integration_metrics.py +5 -2
- package/package.json +1 -1
- package/redirect_rule.py +75 -9
- package/setup_ui.py +1 -1
- package/smart_tool_daemon.py +88 -15
- package/tool_redirect.py +103 -0
- package/version.py +1 -1
package/hook_decision.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
"""Decisão do PreToolUse do Smart Tool, tomada dentro do daemon já aquecido.
|
|
1
|
+
"""Decisão do PreToolUse e do PostToolUse do Smart Tool, tomada dentro do daemon já aquecido.
|
|
2
2
|
|
|
3
|
-
O Claude Code chama
|
|
4
|
-
hook de comando `hooks/pretool_router.py`, que só repassa o payload para
|
|
3
|
+
O Claude Code chama as rotas POST /hooks/pretool e /hooks/posttool como hooks `type: "http"` (sem processo por
|
|
4
|
+
chamada); o Codex usa o hook de comando `hooks/pretool_router.py`, que só repassa o payload para /hooks/pretool.
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
Uma busca nativa que a regra redireciona, e WebFetch/WebSearch, não são negadas: o Smart Tool responde no lugar e o
|
|
7
|
+
texto volta como saída da própria chamada (`tool_redirect`). INVARIANTE: a saída do PreToolUse é `{}`, `updatedInput`
|
|
8
|
+
sem `permissionDecision` (as regras de permissão do usuário julgam a chamada reescrita), `allow` só junto do comando
|
|
9
|
+
que o próprio hook escreveu para imprimir a resposta no Codex (`tool_redirect.is_codex_command`), ou `deny` quando a
|
|
10
|
+
chamada não dá para montar. Nenhuma decisão aqui é sobre periculosidade, só sobre custo de contexto. Diagnóstico vai em
|
|
8
11
|
`systemMessage`, que não altera permissão.
|
|
9
12
|
"""
|
|
10
|
-
import json
|
|
11
13
|
import os
|
|
12
14
|
|
|
13
15
|
import code_graph
|
|
@@ -20,9 +22,18 @@ import index_profile
|
|
|
20
22
|
import index_scope
|
|
21
23
|
import endpoint_sync
|
|
22
24
|
import project_store
|
|
25
|
+
import redirect_rule
|
|
23
26
|
import router
|
|
27
|
+
import tool_redirect
|
|
24
28
|
|
|
25
29
|
WEB_TOOLS = ("WebSearch", "WebFetch")
|
|
30
|
+
# A warm smart_search takes 4 to 13 s with the reranker, and in Codex a late answer is a lost one (no output
|
|
31
|
+
# replacement): a redirected shell search waits this long within the daemon's HOOK_SHELL_BUDGET_S. The PostToolUse
|
|
32
|
+
# entry has its own client timeout (60 s).
|
|
33
|
+
PRE_WAIT_S = 10.0
|
|
34
|
+
SEARCH_POST_WAIT_S = 15
|
|
35
|
+
WEB_POST_WAIT_S = 50
|
|
36
|
+
SINGLE_LANGUAGE_REASON = "redirected native search: its pattern is the only query there is"
|
|
26
37
|
EDIT_TOOLS = ("Edit", "MultiEdit", "Write", "apply_patch")
|
|
27
38
|
MAX_DOC_FINDINGS = 5
|
|
28
39
|
MAX_DUPLICATE_FINDINGS = 3
|
|
@@ -102,13 +113,16 @@ def agent_has_smart_tool(agent_type, cwd):
|
|
|
102
113
|
|
|
103
114
|
|
|
104
115
|
def _search_root(target, cwd):
|
|
105
|
-
"""project_root for smart_search: the registered project holding the folder the redirected search reads, else
|
|
106
|
-
folder
|
|
116
|
+
"""project_root for smart_search: the registered project holding the folder the redirected search reads, else the
|
|
117
|
+
session folder when it holds that folder (a subfolder is not indexed on its own), else that folder."""
|
|
107
118
|
target = os.path.abspath(target or cwd or ".")
|
|
108
119
|
key = os.path.normcase(target) + os.sep
|
|
109
120
|
roots = [p["root"] for p in project_store.all_projects()
|
|
110
121
|
if key.startswith(os.path.normcase(os.path.abspath(p["root"])).rstrip(os.sep) + os.sep)]
|
|
111
|
-
|
|
122
|
+
if roots:
|
|
123
|
+
return max(roots, key=len)
|
|
124
|
+
session = os.path.abspath(cwd) if cwd else None
|
|
125
|
+
return session if session and key.startswith(os.path.normcase(session).rstrip(os.sep) + os.sep) else target
|
|
112
126
|
|
|
113
127
|
|
|
114
128
|
def _tool_name(client, tool):
|
|
@@ -146,24 +160,6 @@ def web_message(client, tool_name, url, why):
|
|
|
146
160
|
f"{_load_hint(client, tool)} Do not fetch it through {fetchers}: that bypasses the rule.")
|
|
147
161
|
|
|
148
162
|
|
|
149
|
-
def smart_tool_in_session(cwd):
|
|
150
|
-
"""Claude Code loads MCP servers from ~/.claude.json (user and per project) and the project's .mcp.json."""
|
|
151
|
-
with open(endpoint_sync.claude_json(), encoding="utf-8") as stream:
|
|
152
|
-
data = json.load(stream)
|
|
153
|
-
key = os.path.normcase(os.path.normpath(cwd or ""))
|
|
154
|
-
project = next((value for path, value in (data.get("projects") or {}).items()
|
|
155
|
-
if os.path.normcase(os.path.normpath(path)) == key), {})
|
|
156
|
-
if SMART_TOOL_SERVER in (project.get("disabledMcpServers") or []):
|
|
157
|
-
return False
|
|
158
|
-
if SMART_TOOL_SERVER in (data.get("mcpServers") or {}) or SMART_TOOL_SERVER in (project.get("mcpServers") or {}):
|
|
159
|
-
return True
|
|
160
|
-
try:
|
|
161
|
-
with open(os.path.join(cwd or "", ".mcp.json"), encoding="utf-8") as stream:
|
|
162
|
-
return SMART_TOOL_SERVER in (json.load(stream).get("mcpServers") or {})
|
|
163
|
-
except (OSError, ValueError):
|
|
164
|
-
return False
|
|
165
|
-
|
|
166
|
-
|
|
167
163
|
def advise(reason):
|
|
168
164
|
"""Same routing advice without blocking: the agent sees it next to the tool result and decides next time."""
|
|
169
165
|
return {"hookSpecificOutput": {"hookEventName": "PreToolUse",
|
|
@@ -224,6 +220,12 @@ def _edit_review(payload, doc_mode, duplicate_mode):
|
|
|
224
220
|
docs and files outside a project are left out, as in the docs coverage): missing ones deny the edit in require mode and become a reminder in remind mode; a
|
|
225
221
|
documented function whose signature changed always gets a reminder. Copies of indexed functions follow
|
|
226
222
|
duplicate_mode and only add context, never deny."""
|
|
223
|
+
with code_graph.pending_scope():
|
|
224
|
+
return _edit_review_scoped(payload, doc_mode, duplicate_mode)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _edit_review_scoped(payload, doc_mode, duplicate_mode):
|
|
228
|
+
"""`_edit_review` inside one `code_graph.pending_scope`, so the pending-changes scan runs once per review."""
|
|
227
229
|
doc_findings, duplicate_findings, failures = [], [], []
|
|
228
230
|
for path, before, after in edit_preview.preview(payload.get("tool_name"), payload.get("tool_input"), payload.get("cwd")):
|
|
229
231
|
if not doc_check.supported(path):
|
|
@@ -249,10 +251,12 @@ def _edit_review(payload, doc_mode, duplicate_mode):
|
|
|
249
251
|
return {"hookSpecificOutput": {"hookEventName": "PreToolUse", "additionalContext": message}}
|
|
250
252
|
|
|
251
253
|
|
|
252
|
-
def decide(payload, client, web_route):
|
|
254
|
+
def decide(payload, client, web_route, jobs):
|
|
253
255
|
"""Hook output for one PreToolUse payload: edits follow doc_mode (require | remind | off) and duplicate_mode
|
|
254
|
-
(warn | off), the other tools follow hook_mode (redirect | advise | off).
|
|
255
|
-
|
|
256
|
+
(warn | off), the other tools follow hook_mode (redirect | advise | off). In redirect mode the Smart Tool call that
|
|
257
|
+
replaces a redirected one starts in `jobs` under the call's tool_use_id (see `_redirect`); only a search whose
|
|
258
|
+
query cannot be built, or a client that sends no tool_use_id, is denied with the exact call to make, and never in a
|
|
259
|
+
subagent whose tool list has no Smart Tool MCP tool: it has nothing to switch to."""
|
|
256
260
|
cfg = config.load_config()
|
|
257
261
|
if isinstance(payload, dict) and payload.get("tool_name") in EDIT_TOOLS:
|
|
258
262
|
doc_mode, duplicate_mode = config.doc_mode(cfg), config.duplicate_mode(cfg)
|
|
@@ -268,30 +272,96 @@ def decide(payload, client, web_route):
|
|
|
268
272
|
mode = config.hook_mode(cfg)
|
|
269
273
|
if mode == "off" or not isinstance(payload, dict):
|
|
270
274
|
return {}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
+
plan = _route(payload, client, web_route, cfg)
|
|
276
|
+
if "kind" not in plan:
|
|
277
|
+
return plan
|
|
278
|
+
if mode == "advise":
|
|
279
|
+
return advise(plan["message"])
|
|
280
|
+
if plan["kind"] and payload.get("tool_use_id"):
|
|
281
|
+
return _redirect(payload, client, plan, jobs)
|
|
282
|
+
if not agent_has_smart_tool(payload.get("agent_type") if payload.get("agent_id") else None, payload.get("cwd")):
|
|
283
|
+
return {}
|
|
284
|
+
return deny(plan["message"])
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _redirect(payload, client, plan, jobs):
|
|
288
|
+
"""Starts the Smart Tool call of a redirected one. A shell search whose answer comes within PRE_WAIT_S is rewritten
|
|
289
|
+
so the answer is all it prints: a no-op whose output the PostToolUse replaces in Claude Code, a `cat` of a file
|
|
290
|
+
holding the answer in Codex (which has no output replacement and applies a rewrite only with `allow`). Anything
|
|
291
|
+
else runs as the agent wrote it and, in Claude Code, `after` swaps its output for the answer."""
|
|
292
|
+
key, tool_name = payload["tool_use_id"], payload["tool_name"]
|
|
293
|
+
tool_input = payload.get("tool_input") if isinstance(payload.get("tool_input"), dict) else {}
|
|
294
|
+
jobs.start(key, plan["kind"], plan["arguments"])
|
|
295
|
+
if tool_name not in tool_redirect.NO_OP:
|
|
296
|
+
return {}
|
|
297
|
+
text = _answer(jobs, key, PRE_WAIT_S, tool_name)
|
|
298
|
+
if text is None:
|
|
299
|
+
return {}
|
|
300
|
+
if client != "codex":
|
|
301
|
+
return {"hookSpecificOutput": {"hookEventName": "PreToolUse",
|
|
302
|
+
"updatedInput": {**tool_input, "command": tool_redirect.NO_OP[tool_name]}}}
|
|
303
|
+
jobs.forget(key)
|
|
304
|
+
_log_served(client, payload)
|
|
305
|
+
return {"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "allow",
|
|
306
|
+
"permissionDecisionReason": "Smart Tool answered this search",
|
|
307
|
+
"updatedInput": {**tool_input, "command": tool_redirect.codex_command(text)}}}
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _answer(jobs, key, timeout, tool_name):
|
|
311
|
+
"""The Smart Tool text of key within timeout; None when it is not ready or the call failed (logged: the native call
|
|
312
|
+
then runs as written)."""
|
|
313
|
+
try:
|
|
314
|
+
text = jobs.wait(key, timeout)
|
|
315
|
+
except Exception as exc:
|
|
316
|
+
jobs.forget(key)
|
|
317
|
+
router._log_error(f"redirect of {tool_name} not served: {type(exc).__name__}: {exc}")
|
|
318
|
+
return None
|
|
319
|
+
return text if isinstance(text, str) and text.strip() else None
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _log_served(client, payload):
|
|
323
|
+
router.CLIENT.set(client)
|
|
324
|
+
router.SESSION.set(payload.get("session_id"))
|
|
325
|
+
router._log_decision(payload.get("tool_name"), {}, "served", "Smart Tool answer returned as the call's output")
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def after(payload, client, jobs):
|
|
329
|
+
"""PostToolUse output: the call's own response with the Smart Tool answer started for its tool_use_id in the field
|
|
330
|
+
the model reads (`updatedToolOutput`), or `{}` when there is none, it did not come in time or the response has an
|
|
331
|
+
unknown shape (the native output stays)."""
|
|
332
|
+
if not isinstance(payload, dict) or not payload.get("tool_use_id"):
|
|
333
|
+
return {}
|
|
334
|
+
key, tool_name = payload["tool_use_id"], payload.get("tool_name")
|
|
335
|
+
text = _answer(jobs, key, WEB_POST_WAIT_S if tool_name in WEB_TOOLS else SEARCH_POST_WAIT_S, tool_name)
|
|
336
|
+
jobs.forget(key)
|
|
337
|
+
output = tool_redirect.output_for(tool_name, payload.get("tool_response"), text) if text else None
|
|
338
|
+
if output is None:
|
|
275
339
|
return {}
|
|
276
|
-
|
|
340
|
+
_log_served(client, payload)
|
|
341
|
+
return {"hookSpecificOutput": {"hookEventName": "PostToolUse", "updatedToolOutput": output}}
|
|
277
342
|
|
|
278
343
|
|
|
279
344
|
def _route(payload, client, web_route, cfg):
|
|
345
|
+
"""`{}` or a `systemMessage` output when the call runs untouched, else the redirect plan: `kind` and `arguments`
|
|
346
|
+
of the Smart Tool call (kind None when no query can be built from the call) and `message`, the exact call for an
|
|
347
|
+
agent that has to make it itself."""
|
|
280
348
|
tool_name = payload.get("tool_name", "")
|
|
281
349
|
tool_input = payload.get("tool_input") if isinstance(payload.get("tool_input"), dict) else {}
|
|
282
350
|
cwd = payload.get("cwd")
|
|
283
351
|
if tool_name in WEB_TOOLS:
|
|
284
|
-
if not cfg.get("router_model"):
|
|
285
|
-
return {}
|
|
286
|
-
try:
|
|
287
|
-
present = smart_tool_in_session(cwd)
|
|
288
|
-
except (OSError, ValueError):
|
|
289
|
-
present = False
|
|
290
|
-
if not present:
|
|
352
|
+
if not cfg.get("router_model") or tool_input.get("allowed_domains") or tool_input.get("blocked_domains"):
|
|
291
353
|
return {}
|
|
292
354
|
url = str(tool_input.get("url") or "")
|
|
293
355
|
decision = web_route(tool_name, url)
|
|
294
|
-
|
|
356
|
+
if not decision.get("redirect"):
|
|
357
|
+
return {}
|
|
358
|
+
if tool_name == "WebFetch":
|
|
359
|
+
kind, arguments = "web_fetch", {"url": url, "prompt": str(tool_input.get("prompt") or "")}
|
|
360
|
+
else:
|
|
361
|
+
query = str(tool_input.get("query") or "")
|
|
362
|
+
kind, arguments = "web_search", {"query": query, "query_en": query}
|
|
363
|
+
return {"kind": kind if all(arguments.values()) else None, "arguments": arguments,
|
|
364
|
+
"message": web_message(client, tool_name, url, clean_reason(decision.get("reason")))}
|
|
295
365
|
router.CLIENT.set(client)
|
|
296
366
|
router.SESSION.set(payload.get("session_id"))
|
|
297
367
|
try:
|
|
@@ -302,6 +372,10 @@ def _route(payload, client, web_route, cfg):
|
|
|
302
372
|
except Exception:
|
|
303
373
|
pass
|
|
304
374
|
return {"systemMessage": f"Smart Tool unavailable, routing skipped: {type(exc).__name__}"}
|
|
305
|
-
if decision
|
|
306
|
-
return
|
|
307
|
-
|
|
375
|
+
if decision != "redirect" or (tool_name in tool_redirect.NO_OP and tool_input.get("run_in_background")):
|
|
376
|
+
return {}
|
|
377
|
+
terms = redirect_rule.search_terms(tool_name, tool_input)
|
|
378
|
+
arguments = {"project_root": _search_root(target, cwd), "query_identifiers": terms,
|
|
379
|
+
"single_language_reason": SINGLE_LANGUAGE_REASON}
|
|
380
|
+
return {"kind": "smart_search" if terms else None, "arguments": arguments,
|
|
381
|
+
"message": redirect_message(client, tool_name, target, reason, cwd)}
|
package/hooks/pretool_router.py
CHANGED
|
@@ -15,7 +15,8 @@ sys.stdout.reconfigure(encoding="utf-8")
|
|
|
15
15
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
|
16
16
|
import daemon_launcher
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
# Above the daemon's budget for a redirected shell search (smart_tool_daemon.HOOK_SHELL_BUDGET_S), below the entry's 15 s.
|
|
19
|
+
TIMEOUT_S = 14
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
def _client():
|
package/integration_metrics.py
CHANGED
|
@@ -97,8 +97,9 @@ def _next_in_session(rows, index):
|
|
|
97
97
|
|
|
98
98
|
|
|
99
99
|
def redirects(days=7):
|
|
100
|
-
"""Redirects of native searches in the last days and what the same session
|
|
101
|
-
|
|
100
|
+
"""Redirects of native searches in the last days and what came next in the same session: the Smart Tool answer
|
|
101
|
+
returned as the call's output (served), a smart_search call (followed), an allowed native call such as reading a
|
|
102
|
+
file it found (native), another redirect (blocked_again), or no other
|
|
102
103
|
call within FOLLOW_WINDOW_S. Redirects logged without a session (before 0.9.15, or a client that sends none) count
|
|
103
104
|
as unknown. tokens_per_call holds the measured context of each side, so the panel never claims a saving it did
|
|
104
105
|
not measure."""
|
|
@@ -120,6 +121,8 @@ def redirects(days=7):
|
|
|
120
121
|
after = _next_in_session(own, own.index(row))
|
|
121
122
|
if after is None:
|
|
122
123
|
outcomes["no_other_call"] += 1
|
|
124
|
+
elif after.get("decision") == "served":
|
|
125
|
+
outcomes["served"] += 1
|
|
123
126
|
elif str(after.get("tool_name", "")).startswith("mcp__smart-tool__"):
|
|
124
127
|
outcomes["followed"] += 1
|
|
125
128
|
elif after.get("decision") == "redirect":
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allansantos-dev/smart-tool",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.23",
|
|
4
4
|
"description": "Local MCP server that gives coding agents (Claude Code, Codex) cheaper, sharper tools than their built-in search. Windows.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Allan Santos",
|
package/redirect_rule.py
CHANGED
|
@@ -76,6 +76,10 @@ _CWD_WALK = re.compile(r"os\.getcwd\(\)|process\.cwd\(\)|Path\(\)|os\.walk\(\s*\
|
|
|
76
76
|
_SINGLE_QUOTED = re.compile(r"'([^'\n]+)'")
|
|
77
77
|
_DOUBLE_QUOTED = re.compile(r'"([^"\n]+)"')
|
|
78
78
|
_PS_READ = re.compile(r"\b(?:get-content|gc|select-string|sls|cat|type)\b|\[io\.file\]", re.IGNORECASE)
|
|
79
|
+
_MATCHED_LITERAL = re.compile(r"""(?:\bre\.(?:search|findall|finditer|match|fullmatch|compile)\(\s*r?|"""
|
|
80
|
+
r"""\.(?:includes|indexOf|match|matchAll|search|test)\(\s*)(['"])(.+?)\1|"""
|
|
81
|
+
r"""(['"])([^'"\n]+)\3\s+in\s+\w+""")
|
|
82
|
+
QUERY_MAX_CHARS = 400
|
|
79
83
|
_ASSIGN = re.compile(r"^([A-Za-z_][A-Za-z0-9_]*)=(.*)$")
|
|
80
84
|
_VARIABLE = re.compile(r"\$\{(\w+)\}|\$(\w+)")
|
|
81
85
|
|
|
@@ -318,29 +322,35 @@ def _inner_script(tokens):
|
|
|
318
322
|
|
|
319
323
|
|
|
320
324
|
def _search_args(verb, tokens):
|
|
321
|
-
"""
|
|
325
|
+
"""(path arguments, recursive, patterns) of a grep-like command: the paths without the pattern and flag values,
|
|
326
|
+
whether it walks folders, and the text it looks for (`-e`/`-Pattern`/`/c:` values, else the first argument)."""
|
|
322
327
|
if verb in ("select-string", "sls"):
|
|
323
328
|
recursive = False
|
|
324
329
|
else:
|
|
325
330
|
recursive = verb in ALWAYS_RECURSIVE or any(
|
|
326
331
|
t in RECURSIVE_FLAGS or (re.fullmatch(r"-[a-zA-Z]+", t) and ("r" in t or "R" in t)) for t in tokens)
|
|
327
|
-
args, paths, skip, path_next, pattern_given = [], [],
|
|
332
|
+
args, paths, patterns, skip, path_next, pattern_given = [], [], [], None, False, False
|
|
328
333
|
for token in tokens:
|
|
329
334
|
low = token.lower()
|
|
330
335
|
if skip:
|
|
331
|
-
skip
|
|
336
|
+
if skip == "pattern":
|
|
337
|
+
patterns.append(token)
|
|
338
|
+
skip = None
|
|
332
339
|
elif path_next:
|
|
333
340
|
paths.append(token)
|
|
334
341
|
path_next = False
|
|
335
342
|
elif token in PATTERN_FLAGS or low == "-pattern":
|
|
336
|
-
skip, pattern_given =
|
|
343
|
+
skip, pattern_given = ("value" if token in ("-f", "--file") else "pattern"), True
|
|
337
344
|
elif low in ("-path", "-literalpath"):
|
|
338
345
|
path_next = True
|
|
339
346
|
elif token in VALUE_FLAGS or low in ("-include", "-exclude", "-context", "-encoding"):
|
|
340
|
-
skip =
|
|
347
|
+
skip = "value"
|
|
348
|
+
elif verb == "findstr" and low.startswith("/c:"):
|
|
349
|
+
patterns.append(token[3:])
|
|
350
|
+
pattern_given = True
|
|
341
351
|
elif not token.startswith("-") and not (verb == "findstr" and token.startswith("/")):
|
|
342
352
|
args.append(token)
|
|
343
|
-
return paths + (args if pattern_given else args[1:]), recursive
|
|
353
|
+
return paths + (args if pattern_given else args[1:]), recursive, patterns if pattern_given else args[:1]
|
|
344
354
|
|
|
345
355
|
|
|
346
356
|
def _reader_files(verb, tokens):
|
|
@@ -484,13 +494,19 @@ def _shell(command, cwd, state, powershell, depth):
|
|
|
484
494
|
return verdict
|
|
485
495
|
|
|
486
496
|
|
|
487
|
-
def
|
|
488
|
-
"""The
|
|
497
|
+
def _xargs_tokens(tokens):
|
|
498
|
+
"""The command xargs runs, past its own flags."""
|
|
489
499
|
rest = tokens[1:]
|
|
490
500
|
while rest and rest[0].startswith("-"):
|
|
491
501
|
flag = rest.pop(0)
|
|
492
502
|
if flag in ("-I", "-n", "-P", "-d", "-L", "-s", "-E", "-a", "--delimiter", "--max-args", "--max-procs") and rest:
|
|
493
503
|
rest.pop(0)
|
|
504
|
+
return rest
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def _xargs_command(tokens):
|
|
508
|
+
"""The verb xargs runs, past its own flags."""
|
|
509
|
+
rest = _xargs_tokens(tokens)
|
|
494
510
|
return rest[0].lower() if rest else None
|
|
495
511
|
|
|
496
512
|
|
|
@@ -519,7 +535,7 @@ def _git(tokens, variables, base, cwd):
|
|
|
519
535
|
|
|
520
536
|
def _search(verb, tokens, variables, base, cwd, verdict):
|
|
521
537
|
"""(redirect or None, verdict so far) for a grep-like command."""
|
|
522
|
-
args, recursive = _search_args(verb, tokens[1:])
|
|
538
|
+
args, recursive, _patterns = _search_args(verb, tokens[1:])
|
|
523
539
|
if LISTING in args:
|
|
524
540
|
return (("redirect", base, "content search over the files of a listing") if _folder(base, cwd) else None), verdict
|
|
525
541
|
if not args:
|
|
@@ -545,6 +561,56 @@ def _search(verb, tokens, variables, base, cwd, verdict):
|
|
|
545
561
|
return None, verdict
|
|
546
562
|
|
|
547
563
|
|
|
564
|
+
def _command_patterns(command, powershell, depth):
|
|
565
|
+
"""Patterns of the grep-like commands, `git grep`, `xargs grep` and `find -exec grep` of a shell command, inner
|
|
566
|
+
scripts and substitutions included."""
|
|
567
|
+
text, inner = _substitutions(_strip_heredocs(command), powershell)
|
|
568
|
+
found = []
|
|
569
|
+
for script in inner if depth < MAX_NESTING else ():
|
|
570
|
+
found += _command_patterns(script, powershell, depth + 1)
|
|
571
|
+
for segment, _after_pipe in segments(text):
|
|
572
|
+
tokens = _unwrap(_split(segment, powershell), {})
|
|
573
|
+
if not tokens:
|
|
574
|
+
continue
|
|
575
|
+
verb = tokens[0].lower()
|
|
576
|
+
script = _inner_script(tokens) if depth < MAX_NESTING else None
|
|
577
|
+
if script:
|
|
578
|
+
found += _command_patterns(script, re.sub(r"\.exe$", "", verb) in ("powershell", "pwsh", "cmd"), depth + 1)
|
|
579
|
+
continue
|
|
580
|
+
if verb == "xargs":
|
|
581
|
+
tokens = _xargs_tokens(tokens)
|
|
582
|
+
elif verb == "find":
|
|
583
|
+
start = next((i + 1 for i, t in enumerate(tokens) if t in ("-exec", "-execdir", "-ok", "-okdir")), None)
|
|
584
|
+
tokens = tokens[start:] if start else []
|
|
585
|
+
elif verb == "git":
|
|
586
|
+
tokens = tokens[tokens.index("grep"):] if "grep" in tokens else []
|
|
587
|
+
tokens = tokens[:tokens.index("--")] if "--" in tokens else tokens
|
|
588
|
+
if tokens and tokens[0].lower() in SEARCHERS | {"grep"}:
|
|
589
|
+
found += _search_args(tokens[0].lower(), tokens[1:])[2]
|
|
590
|
+
return found
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def _regex_words(pattern):
|
|
594
|
+
"""The literal words of a regular expression: classes, quantifiers, escapes and operators become spaces."""
|
|
595
|
+
text = re.sub(r"\[[^\]]*\]|\{\d*,?\d*\}|\\[a-zA-Z]|\(\?[:=!<]*", " ", pattern)
|
|
596
|
+
return re.sub(r"[\\^$.*+?()|{}\[\]]", " ", text)
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
def search_terms(tool_name, tool_input):
|
|
600
|
+
"""The smart_search query of a redirected call: the words of the Grep pattern, of the patterns a shell command
|
|
601
|
+
searches for, or else of the string literals an inline script matches against. Empty when none is found."""
|
|
602
|
+
if tool_name == "Grep":
|
|
603
|
+
patterns = [str(tool_input.get("pattern") or "")]
|
|
604
|
+
else:
|
|
605
|
+
command = str(tool_input.get("command") or "")
|
|
606
|
+
patterns = _command_patterns(command, tool_name == "PowerShell", 0) or [
|
|
607
|
+
match.group(2) or match.group(4) for match in _MATCHED_LITERAL.finditer(command)]
|
|
608
|
+
words = []
|
|
609
|
+
for pattern in patterns:
|
|
610
|
+
words += [word for word in _regex_words(pattern).split() if word not in words]
|
|
611
|
+
return " ".join(words)[:QUERY_MAX_CHARS]
|
|
612
|
+
|
|
613
|
+
|
|
548
614
|
def decide(tool_name, tool_input, cwd):
|
|
549
615
|
"""(decision, target, reason) for a Grep, Glob or Bash call: decision is redirect or allow, target the folder that
|
|
550
616
|
would be searched (None when allowed)."""
|
package/setup_ui.py
CHANGED
|
@@ -1281,7 +1281,7 @@ function agentCard(item) {
|
|
|
1281
1281
|
const plan = await post("/setup/integration", { client: item.client, action: "preview" });
|
|
1282
1282
|
setMsg(msg, "", "");
|
|
1283
1283
|
if (plan.installed) { await loadIntegration(); return; }
|
|
1284
|
-
confirmFile.textContent = (item.hook_state === "outdated" ? "Replaces Smart Tool's
|
|
1284
|
+
confirmFile.textContent = (item.hook_state === "outdated" ? "Replaces Smart Tool's entries with these, by event, in " : "Adds these entries, by event, in ") + plan.file;
|
|
1285
1285
|
preview.textContent = JSON.stringify(plan.change, null, 2);
|
|
1286
1286
|
confirm.hidden = false;
|
|
1287
1287
|
start.hidden = true;
|
package/smart_tool_daemon.py
CHANGED
|
@@ -50,6 +50,7 @@ import index_views
|
|
|
50
50
|
import index_inventory
|
|
51
51
|
import usage_meter
|
|
52
52
|
import document_text
|
|
53
|
+
import dossier
|
|
53
54
|
import embedding_cache
|
|
54
55
|
import model_defaults
|
|
55
56
|
import project_identity
|
|
@@ -73,6 +74,7 @@ import gateway
|
|
|
73
74
|
import code_graph
|
|
74
75
|
import hook_decision
|
|
75
76
|
import endpoint_sync
|
|
77
|
+
import tool_redirect
|
|
76
78
|
|
|
77
79
|
RUN_DIR = paths.DATA_DIR
|
|
78
80
|
REGISTRY_PATH = os.path.join(RUN_DIR, "daemon.json")
|
|
@@ -331,18 +333,15 @@ def _smart_search_job_payload(job_id):
|
|
|
331
333
|
return payload
|
|
332
334
|
|
|
333
335
|
# Claude Code defers MCP tools behind tool search; the tools the hook redirects to (and their job polling) load
|
|
334
|
-
# upfront, so
|
|
336
|
+
# upfront, so an agent denied by the hook already has the replacement in its tool list.
|
|
335
337
|
ALWAYS_LOAD = {"anthropic/alwaysLoad": True}
|
|
336
338
|
SERVER_INSTRUCTIONS = (
|
|
337
339
|
"Smart Tool serves an indexed semantic search over the user's projects and a cheaper web reader. Use smart_search "
|
|
338
340
|
"FIRST to explore code in the user's projects (where something is implemented, how a flow works, what already "
|
|
339
341
|
"exists to reuse) before reading files one by one; Grep/Read are for an exact string or a file you already know. "
|
|
340
|
-
"Use web_search/web_fetch for any web research.
|
|
341
|
-
"
|
|
342
|
-
"
|
|
343
|
-
"message names (smart_search with project_root and query_identifiers, web_fetch with url and prompt, web_search "
|
|
344
|
-
"with query). Never retry the same search or download through Bash, python, node, PowerShell, curl or wget: that "
|
|
345
|
-
"bypasses the rule the user set. Reading one known file, or searching inside one known file, is never blocked. "
|
|
342
|
+
"Use web_search/web_fetch for any web research. If a call is ever denied by Smart Tool, that is a routing rule the "
|
|
343
|
+
"user set, not a failure: make the call the message names, never the same search or download through Bash, python, "
|
|
344
|
+
"node, PowerShell, curl or wget. "
|
|
346
345
|
"Before changing a function, project_manage action=graph with symbol lists its callers and tests; after editing, "
|
|
347
346
|
"project_manage action=affected_tests lists the tests to run. When smart_search returns nothing, retry smart_search with "
|
|
348
347
|
"other terms; do not switch to Grep, Read of folders or shell search. A helper agent (smart-scout, a cheap model) is "
|
|
@@ -423,6 +422,12 @@ TOOLS = [
|
|
|
423
422
|
"test": {"type": "integer", "minimum": 1, "maximum": 20},
|
|
424
423
|
"doc": {"type": "integer", "minimum": 1, "maximum": 20}}}],
|
|
425
424
|
"description": "Results per block: one number for all blocks or {code, test, doc}; each 1-20, total at most 20. Defaults to the project's search_limits, else 5"},
|
|
425
|
+
"mode": {"type": "string", "enum": ["search", "dossier"], "default": "search",
|
|
426
|
+
"description": "dossier answers only with ids and a short excerpt, by keyword from disk and without an index, from docs/research, the backlog, CLAUDE.md and AGENTS.md; it takes query_identifiers or doc_query, timeout_ms and max_hits and ignores the other fields"},
|
|
427
|
+
"timeout_ms": {"type": "integer", "minimum": 1, "maximum": 5000, "default": 800,
|
|
428
|
+
"description": "mode=dossier only: scan budget; past it the answer is marked degraded"},
|
|
429
|
+
"max_hits": {"type": "integer", "minimum": 1, "maximum": 10, "default": 5,
|
|
430
|
+
"description": "mode=dossier only: hits returned"},
|
|
426
431
|
},
|
|
427
432
|
"required": ["project_root"],
|
|
428
433
|
},
|
|
@@ -1443,6 +1448,7 @@ def _optional_text(arguments, name):
|
|
|
1443
1448
|
|
|
1444
1449
|
_SMART_SEARCH_FIELDS = {"project_root", "query_identifiers", "query_comments", "doc_query",
|
|
1445
1450
|
"single_language_reason", "group", "top_k"}
|
|
1451
|
+
_DOSSIER_FIELDS = {"project_root", "query_identifiers", "query_comments", "doc_query", "mode", "timeout_ms", "max_hits"}
|
|
1446
1452
|
|
|
1447
1453
|
|
|
1448
1454
|
def _reject_kind(message):
|
|
@@ -1455,6 +1461,17 @@ def _reject_kind(message):
|
|
|
1455
1461
|
|
|
1456
1462
|
|
|
1457
1463
|
def _handle_smart_search(arguments):
|
|
1464
|
+
mode = arguments.get("mode", "search") if isinstance(arguments, dict) else "search"
|
|
1465
|
+
if mode == "dossier":
|
|
1466
|
+
unknown = set(arguments) - _DOSSIER_FIELDS
|
|
1467
|
+
if unknown:
|
|
1468
|
+
raise ValueError(f"Unknown fields in smart_search mode=dossier: {', '.join(sorted(unknown))}. "
|
|
1469
|
+
f"Accepted: {', '.join(sorted(_DOSSIER_FIELDS))}.")
|
|
1470
|
+
_metric("search", group="dossier")
|
|
1471
|
+
return dossier.answer(arguments)
|
|
1472
|
+
if mode != "search":
|
|
1473
|
+
raise ValueError("mode must be search or dossier.")
|
|
1474
|
+
arguments = {key: value for key, value in arguments.items() if key != "mode"}
|
|
1458
1475
|
try:
|
|
1459
1476
|
arguments = _smart_search_arguments(arguments)
|
|
1460
1477
|
except ValueError as exc:
|
|
@@ -3580,10 +3597,14 @@ HOOK_MAX_BODY_BYTES = 4 * 1024 * 1024
|
|
|
3580
3597
|
# without one and says so in the log. Edit reviews (docs, copies) are the slow ones, so they have their own small
|
|
3581
3598
|
# pool: at most HOOK_EDIT_SLOTS run at once and a further edit is let through unreviewed instead of queueing.
|
|
3582
3599
|
HOOK_BUDGET_S = 4.0
|
|
3600
|
+
# A redirected shell search waits up to hook_decision.PRE_WAIT_S for its answer; any other Bash decision takes
|
|
3601
|
+
# milliseconds, so the larger budget only matters for those.
|
|
3602
|
+
HOOK_SHELL_BUDGET_S = 11.0
|
|
3583
3603
|
HOOK_ANALYZER_TIMEOUT_S = 10
|
|
3584
3604
|
HOOK_SLOW_LOG_S = 1.0
|
|
3585
3605
|
HOOK_EDIT_SLOTS = 2
|
|
3586
|
-
|
|
3606
|
+
# A redirected shell search holds its worker while it waits for the answer (hook_decision.PRE_WAIT_S).
|
|
3607
|
+
_HOOK_POOL = concurrent.futures.ThreadPoolExecutor(8, thread_name_prefix="hook")
|
|
3587
3608
|
_HOOK_EDIT_POOL = concurrent.futures.ThreadPoolExecutor(HOOK_EDIT_SLOTS, thread_name_prefix="hook-edit")
|
|
3588
3609
|
_HOOK_EDIT_SLOTS = threading.BoundedSemaphore(HOOK_EDIT_SLOTS)
|
|
3589
3610
|
|
|
@@ -3593,20 +3614,22 @@ def _hook_work(payload, client, slot):
|
|
|
3593
3614
|
(the worker may outlive the request that gave up on it)."""
|
|
3594
3615
|
code_graph.ANALYZER_TIMEOUT_S.set(HOOK_ANALYZER_TIMEOUT_S)
|
|
3595
3616
|
try:
|
|
3596
|
-
return hook_decision.decide(payload, client, _web_route)
|
|
3617
|
+
return hook_decision.decide(payload, client, _web_route, _REDIRECTS)
|
|
3597
3618
|
finally:
|
|
3598
3619
|
if slot:
|
|
3599
3620
|
slot.release()
|
|
3600
3621
|
|
|
3601
3622
|
|
|
3602
3623
|
def _decide_within_budget(payload, client):
|
|
3603
|
-
"""The hook decision of `payload`, or None when it did not come within HOOK_BUDGET_S
|
|
3624
|
+
"""The hook decision of `payload`, or None when it did not come within HOOK_BUDGET_S (HOOK_SHELL_BUDGET_S for a shell
|
|
3625
|
+
call) or no edit slot was free (the
|
|
3604
3626
|
tool then runs as if the hook had nothing to say). Both cases, and any answer slower than HOOK_SLOW_LOG_S, leave one
|
|
3605
3627
|
line in the daemon log with the tool, the file or command and the time."""
|
|
3606
3628
|
tool = payload.get("tool_name") if isinstance(payload, dict) else None
|
|
3607
3629
|
tool_input = payload.get("tool_input") if isinstance(payload, dict) else None
|
|
3608
3630
|
target = str((tool_input or {}).get("file_path") or (tool_input or {}).get("command") or "")[:80]
|
|
3609
3631
|
edit = tool in hook_decision.EDIT_TOOLS
|
|
3632
|
+
budget = HOOK_SHELL_BUDGET_S if tool in tool_redirect.NO_OP else HOOK_BUDGET_S
|
|
3610
3633
|
if edit and not _HOOK_EDIT_SLOTS.acquire(blocking=False):
|
|
3611
3634
|
_log(f"PreToolUse hook skipped the review of {tool} {target}: {HOOK_EDIT_SLOTS} edit reviews already running, "
|
|
3612
3635
|
"the edit goes through without one")
|
|
@@ -3620,9 +3643,9 @@ def _decide_within_budget(payload, client):
|
|
|
3620
3643
|
_HOOK_EDIT_SLOTS.release()
|
|
3621
3644
|
raise
|
|
3622
3645
|
try:
|
|
3623
|
-
output = future.result(timeout=
|
|
3646
|
+
output = future.result(timeout=budget)
|
|
3624
3647
|
except concurrent.futures.TimeoutError:
|
|
3625
|
-
_log(f"PreToolUse hook over {
|
|
3648
|
+
_log(f"PreToolUse hook over {budget:g} s for {tool} {target}: answered without a decision, the work "
|
|
3626
3649
|
"finishes in the background")
|
|
3627
3650
|
return None
|
|
3628
3651
|
took = time.monotonic() - started
|
|
@@ -3631,9 +3654,51 @@ def _decide_within_budget(payload, client):
|
|
|
3631
3654
|
return output
|
|
3632
3655
|
|
|
3633
3656
|
|
|
3657
|
+
def _redirected_search(arguments):
|
|
3658
|
+
"""smart_search text for a redirected native search, waiting up to SMART_SEARCH_SYNC_WAIT_S; raises when the search
|
|
3659
|
+
is rejected, fails or is still running then. The hook's single_language_reason only passes the language check: the
|
|
3660
|
+
answer stands for the native call's output and does not repeat it as a warning."""
|
|
3661
|
+
checked = _smart_search_arguments(arguments)
|
|
3662
|
+
checked.pop("single_language_reason", None)
|
|
3663
|
+
job_id, _future = _submit_project_job("smart_search", checked)
|
|
3664
|
+
deadline = time.monotonic() + SMART_SEARCH_SYNC_WAIT_S
|
|
3665
|
+
payload = _smart_search_job_payload(job_id)
|
|
3666
|
+
while payload["status"] == "pending" and time.monotonic() < deadline:
|
|
3667
|
+
time.sleep(0.1)
|
|
3668
|
+
payload = _smart_search_job_payload(job_id)
|
|
3669
|
+
if payload["status"] != "done":
|
|
3670
|
+
raise RuntimeError(f"smart_search job {job_id} is {payload['status']}: {payload.get('error') or ''}"[:300])
|
|
3671
|
+
return _agent_search_answer(payload)
|
|
3672
|
+
|
|
3673
|
+
|
|
3674
|
+
def _redirected_text(handler):
|
|
3675
|
+
"""A tool handler whose answer is always text (web_search may answer with a JSON object)."""
|
|
3676
|
+
def run(arguments):
|
|
3677
|
+
result = handler(arguments)
|
|
3678
|
+
return result if isinstance(result, str) else _to_json_sanitized(result)
|
|
3679
|
+
return run
|
|
3680
|
+
|
|
3681
|
+
|
|
3682
|
+
_REDIRECTS = tool_redirect.Jobs({"smart_search": _redirected_search, "web_fetch": _redirected_text(_handle_web_fetch),
|
|
3683
|
+
"web_search": _redirected_text(_handle_web_search)})
|
|
3684
|
+
|
|
3685
|
+
|
|
3686
|
+
def _posttool_hook(handler):
|
|
3687
|
+
"""PostToolUse output for the Claude Code hook: the Smart Tool answer of a redirected call as the call's output, or
|
|
3688
|
+
`{}`. Always 200, like the PreToolUse route."""
|
|
3689
|
+
client = (urllib.parse.parse_qs(urllib.parse.urlsplit(handler.path).query).get("client") or [""])[0]
|
|
3690
|
+
try:
|
|
3691
|
+
payload = json.loads(handler._read_body(HOOK_MAX_BODY_BYTES) or b"{}")
|
|
3692
|
+
return hook_decision.after(payload, client or None, _REDIRECTS)
|
|
3693
|
+
except Exception as exc:
|
|
3694
|
+
_log(f"PostToolUse hook without an answer: {type(exc).__name__}: {exc}")
|
|
3695
|
+
return {}
|
|
3696
|
+
|
|
3697
|
+
|
|
3634
3698
|
def _pretool_hook(handler):
|
|
3635
|
-
"""PreToolUse decision for the hook (Claude Code `type: "http"`, Codex via the thin command). Always 200 with `{}
|
|
3636
|
-
|
|
3699
|
+
"""PreToolUse decision for the hook (Claude Code `type: "http"`, Codex via the thin command). Always 200 with `{}`,
|
|
3700
|
+
`updatedInput`, `deny`, or `allow` only for the command the hook wrote to print a Smart Tool answer in Codex: a
|
|
3701
|
+
non-2xx answer would show as a hook error on every tool call, and any other `allow` would skip the user's
|
|
3637
3702
|
permission prompt."""
|
|
3638
3703
|
client = (urllib.parse.parse_qs(urllib.parse.urlsplit(handler.path).query).get("client") or [""])[0]
|
|
3639
3704
|
try:
|
|
@@ -3649,7 +3714,10 @@ def _pretool_hook(handler):
|
|
|
3649
3714
|
if output is None:
|
|
3650
3715
|
return {}
|
|
3651
3716
|
specific = output.get("hookSpecificOutput") or {}
|
|
3652
|
-
|
|
3717
|
+
decision = specific.get("permissionDecision", "deny")
|
|
3718
|
+
if decision == "allow" and tool_redirect.is_codex_command((specific.get("updatedInput") or {}).get("command")):
|
|
3719
|
+
return output
|
|
3720
|
+
if decision != "deny":
|
|
3653
3721
|
return {}
|
|
3654
3722
|
return output
|
|
3655
3723
|
|
|
@@ -3964,6 +4032,8 @@ class MCPHandler(BaseHTTPRequestHandler):
|
|
|
3964
4032
|
self._write_json(200, result)
|
|
3965
4033
|
|
|
3966
4034
|
def do_POST(self):
|
|
4035
|
+
"""Routes a POST: setup actions, the hook routes (/hooks/pretool, /hooks/posttool, /hooks/session-audit) and
|
|
4036
|
+
the MCP endpoint (/mcp); anything else is 404."""
|
|
3967
4037
|
if self._reject_foreign_origin():
|
|
3968
4038
|
return
|
|
3969
4039
|
if self.path == "/setup/projects":
|
|
@@ -3983,6 +4053,9 @@ class MCPHandler(BaseHTTPRequestHandler):
|
|
|
3983
4053
|
if urllib.parse.urlsplit(self.path).path == "/hooks/pretool":
|
|
3984
4054
|
self._write_json(200, _pretool_hook(self))
|
|
3985
4055
|
return
|
|
4056
|
+
if urllib.parse.urlsplit(self.path).path == "/hooks/posttool":
|
|
4057
|
+
self._write_json(200, _posttool_hook(self))
|
|
4058
|
+
return
|
|
3986
4059
|
if urllib.parse.urlsplit(self.path).path == "/hooks/session-audit":
|
|
3987
4060
|
self._write_json(200, _session_audit_hook(self))
|
|
3988
4061
|
return
|