@allansantos-dev/smart-tool 0.9.14 → 0.9.15

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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.15 - beta
4
+
5
+ - The hook redirects the folder searches that slipped past it in a shell: wrapped (`bash -c`, `sh -lc`,
6
+ `powershell -Command`, `cmd /c`, `timeout 60`, `env X=1`, `time`, `nice`, a `( ... )` subshell), looped
7
+ (`for f in src/*.py; do grep x "$f"`), fed by a listing (`find -exec grep|cat`, `find | xargs -0 grep`,
8
+ `grep x $(find src -name '*.py')`, `find | while read f`, `Get-ChildItem -Recurse | Select-String` or
9
+ `| % { Get-Content $_ }`), `git --no-pager grep`, `cat` over more than 20 files, and inline python/node scripts
10
+ (`-c`, `-e`, heredoc) that walk a project folder, read the files and match their text. A listing capped by `head`,
11
+ `find -name` of one exact file and scripts that write files still run. Replayed over 3 days of real transcripts
12
+ (14,580 Grep and shell calls), 37 more calls are redirected, all folder searches; none of the previous redirects
13
+ changed.
14
+ - Claude Code's PowerShell tool goes through the same rule as Bash; it was not in the hook matcher. Reinstall the
15
+ hook from the setup screen (it shows as outdated): the matcher now has `PowerShell` and `smart_search`.
16
+ - Setup screen, Agents: the redirects of the last 7 days by tool and reason, and what the same session did next
17
+ (called `smart_search`, ran an allowed native call, was blocked again, or nothing within 15 minutes). The hook log
18
+ now records the session, and `smart_search` calls pass through the hook only to be counted. Next to it, the
19
+ measured context per call: a native call of the kinds redirected ~510 tokens, a `smart_search` round trip ~2,160
20
+ tokens; a redirect saves context only when one search replaces several native ones, which is not measured yet.
21
+
3
22
  ## 0.9.14 - beta
4
23
 
5
24
  - The setup audit runs on its own: the daemon audits every registered project two minutes after it starts and then
package/README.md CHANGED
@@ -15,7 +15,7 @@ registered as the `playwright` server where this machine lets a browser be drive
15
15
  An optional hook routes the agent's native Grep/Glob/Read/Bash/WebSearch/WebFetch calls to these tools when they
16
16
  are cheaper, or just tells the agent what Smart Tool would do.
17
17
 
18
- **Status:** 0.9.14 beta. Windows only. What changed in each version: [CHANGELOG.md](CHANGELOG.md).
18
+ **Status:** 0.9.15 beta. Windows only. What changed in each version: [CHANGELOG.md](CHANGELOG.md).
19
19
 
20
20
  ## How it works
21
21
 
@@ -116,8 +116,12 @@ Open the setup screen from the tray icon ("Open settings") or at `http://127.0.0
116
116
  The hook behaviors:
117
117
  - **Redirect** (default): the native tool is denied with the reason and the Smart Tool tool to use. The decision
118
118
  is a fixed rule, under a millisecond and the same every time: a content search over a project folder (`grep -r`,
119
- `rg`, `git grep`, the Grep tool on a folder) or over more than 20 listed files goes to `smart_search`; reading or
120
- searching known files, listings, Glob and commands that change files run. With **Block patterns proposed by a
119
+ `rg`, `git grep`, the Grep tool on a folder) or over more than 20 listed files goes to `smart_search`, in Bash and
120
+ PowerShell alike and also when it is wrapped (`bash -c`, `powershell -Command`, `timeout`, a subshell), looped
121
+ (`for f in src/*.py`), fed by a listing (`find -exec grep`, `xargs`, `Get-ChildItem | Select-String`) or written
122
+ as an inline python/node script that walks a folder and matches text; reading or searching known files,
123
+ listings, Glob and commands that change files run. The setup screen counts the redirects and what each session
124
+ did next. With **Block patterns proposed by a
121
125
  model** on (default), the scope model reviews, in the background, bulk reads the rule could not measure and
122
126
  proposes regex patterns, each checked against the logged calls; a pattern blocks only after you accept it.
123
127
  - **Advise only**: the native tool runs; the agent receives the same advice next to the result and decides.
package/client_hooks.py CHANGED
@@ -16,7 +16,7 @@ HOME = os.path.expanduser("~")
16
16
  PREVIEW_TTL_S = 600
17
17
  CLIENTS = {
18
18
  "claude": {"label": "Claude Code", "names": {"claude-code"}, "files": [os.path.join(endpoint_sync.claude_dir(), "settings.json")],
19
- "docs": "https://code.claude.com/docs/en/hooks", "event": "PreToolUse", "matcher": "Grep|Glob|Read|Bash|WebSearch|WebFetch|Edit|MultiEdit|Write", "http": True},
19
+ "docs": "https://code.claude.com/docs/en/hooks", "event": "PreToolUse", "matcher": "Grep|Glob|Read|Bash|PowerShell|WebSearch|WebFetch|Edit|MultiEdit|Write|mcp__smart-tool__smart_search", "http": True},
20
20
  "codex": {"label": "Codex", "prefix": "codex", "files": [os.path.join(HOME, ".codex", "hooks.json"), os.path.join(HOME, ".codex", "config.toml")],
21
21
  "docs": "https://learn.chatgpt.com/docs/hooks", "event": "PreToolUse", "matcher": "Bash|apply_patch",
22
22
  "after_install": "Codex only runs new hooks after the user approves the definition in /hooks."},
package/hook_decision.py CHANGED
@@ -27,6 +27,7 @@ EDIT_TOOLS = ("Edit", "MultiEdit", "Write", "apply_patch")
27
27
  MAX_DOC_FINDINGS = 5
28
28
  MAX_DUPLICATE_FINDINGS = 3
29
29
  SMART_TOOL_SERVER = "smart-tool"
30
+ SMART_SEARCH_TOOL = f"mcp__{SMART_TOOL_SERVER}__smart_search"
30
31
  REASON_MAX_CHARS = 200
31
32
 
32
33
 
@@ -260,6 +261,12 @@ def decide(payload, client, web_route):
260
261
  if doc_mode == "off" and duplicate_mode == "off":
261
262
  return {}
262
263
  return _edit_review(payload, doc_mode, duplicate_mode)
264
+ if isinstance(payload, dict) and str(payload.get("tool_name")).startswith(f"mcp__{SMART_TOOL_SERVER}__"):
265
+ if payload["tool_name"] == SMART_SEARCH_TOOL:
266
+ router.CLIENT.set(client)
267
+ router.SESSION.set(payload.get("session_id"))
268
+ router._log_decision(SMART_SEARCH_TOOL, {}, "allow", "Smart Tool search, logged to measure followed redirects")
269
+ return {}
263
270
  mode = config.hook_mode(cfg)
264
271
  if mode == "off" or not isinstance(payload, dict):
265
272
  return {}
@@ -288,6 +295,7 @@ def _route(payload, client, web_route, cfg):
288
295
  decision = web_route(tool_name, url)
289
296
  return deny(web_message(client, tool_name, url, clean_reason(decision.get("reason")))) if decision.get("redirect") else {}
290
297
  router.CLIENT.set(client)
298
+ router.SESSION.set(payload.get("session_id"))
291
299
  try:
292
300
  decision, reason, target = router.decide(tool_name, tool_input, cwd=cwd)
293
301
  except Exception as exc:
@@ -2,6 +2,7 @@ import collections
2
2
  import json
3
3
  import os
4
4
  import paths
5
+ import re
5
6
  import threading
6
7
  import time
7
8
 
@@ -11,6 +12,12 @@ import router
11
12
  PATH = os.path.join(paths.DATA_DIR, "integration-metrics.jsonl")
12
13
  MAX_BYTES = 20 * 1024 * 1024
13
14
  _LOCK = threading.Lock()
15
+ FOLLOW_WINDOW_S = 900
16
+ # Measured on 2026-10-12 with docs/spikes/bash_bypass_replay.py over 3 days of Claude Code transcripts (14,580 Grep and
17
+ # shell calls), at ~4 characters per token: the 310 calls the rule redirects put 2,031 characters on average in the
18
+ # context when they ran; a smart_search round trip (pending reply 716 + result 7,924) puts 8,640.
19
+ NATIVE_REDIRECTED_TOKENS = 510
20
+ SMART_SEARCH_TOKENS = 2160
14
21
 
15
22
 
16
23
  def record(event, **fields):
@@ -78,5 +85,48 @@ def summary(days=7):
78
85
  return {"days": days, "events_by_client": {k: dict(v) for k, v in by_client.items()},
79
86
  "rejects_by_kind": dict(rejects), "single_language_reasons": dict(reasons.most_common(20)),
80
87
  "hook_decisions_by_client": {k: dict(v) for k, v in decisions.items()},
88
+ "redirects": redirects(days),
81
89
  "web_search_cache": web_cache,
82
90
  "files": {"integration": PATH, "router": router.METRICS_PATH}}
91
+
92
+
93
+ def _next_in_session(rows, index):
94
+ """The session's next logged call within FOLLOW_WINDOW_S of rows[index], or None."""
95
+ start = rows[index]["ts"]
96
+ return next((row for row in rows[index + 1:] if row["ts"] - start <= FOLLOW_WINDOW_S), None)
97
+
98
+
99
+ def redirects(days=7):
100
+ """Redirects of native searches in the last days and what the same session did next: called smart_search
101
+ (followed), ran an allowed native call such as reading a file it found (native), was blocked again, or made no other
102
+ call within FOLLOW_WINDOW_S. Redirects logged without a session (before 0.9.15, or a client that sends none) count
103
+ as unknown. tokens_per_call holds the measured context of each side, so the panel never claims a saving it did
104
+ not measure."""
105
+ rows = sorted(_read(router.METRICS_PATH, time.time() - days * 86400), key=lambda row: row["ts"])
106
+ sessions = collections.defaultdict(list)
107
+ for row in rows:
108
+ if row.get("session"):
109
+ sessions[row["session"]].append(row)
110
+ by_tool, by_reason, outcomes = collections.Counter(), collections.Counter(), collections.Counter()
111
+ for row in rows:
112
+ if row.get("decision") != "redirect":
113
+ continue
114
+ by_tool[row.get("tool_name") or "?"] += 1
115
+ by_reason[re.sub(r"\d+", "N", str(row.get("reason") or "?"))[:80]] += 1
116
+ own = sessions.get(row.get("session"))
117
+ if not own:
118
+ outcomes["unknown"] += 1
119
+ continue
120
+ after = _next_in_session(own, own.index(row))
121
+ if after is None:
122
+ outcomes["no_other_call"] += 1
123
+ elif str(after.get("tool_name", "")).startswith("mcp__smart-tool__"):
124
+ outcomes["followed"] += 1
125
+ elif after.get("decision") == "redirect":
126
+ outcomes["blocked_again"] += 1
127
+ else:
128
+ outcomes["native"] += 1
129
+ return {"days": days, "total": sum(by_tool.values()), "by_tool": dict(by_tool),
130
+ "by_reason": dict(by_reason.most_common(8)), "outcomes": dict(outcomes),
131
+ "tokens_per_call": {"native_redirected": NATIVE_REDIRECTED_TOKENS, "smart_search": SMART_SEARCH_TOKENS,
132
+ "measured": "2026-10-12, 3 days of Claude Code transcripts"}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allansantos-dev/smart-tool",
3
- "version": "0.9.14",
3
+ "version": "0.9.15",
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
@@ -3,7 +3,12 @@
3
3
  A content search over a directory of a project (grep -r, rg, git grep, the Grep tool on a folder) or over more than
4
4
  LISTED_MAX files goes to smart_search; reading listed files, listings, Glob and commands that change files run. Only
5
5
  the part of a Bash command that searches is measured, with its own paths: `cd` is carried, `$VAR` set in the command or
6
- in the environment, `~` and globs are expanded, and a path still holding an unresolved variable is left alone.
6
+ in the environment, `~` and globs are expanded, and a path still holding an unresolved variable is left alone. Shell
7
+ shapes that run the same search are decided as the search itself: wrappers and subshells, `bash -c`/`powershell
8
+ -Command`/`cmd /c` scripts, `$(...)` substitutions, loop variables, listings fed to a reader (`find -exec`, `xargs`,
9
+ `while read`, PowerShell pipelines), `cat` over many files and inline python/node scripts that walk a folder and match
10
+ text. Replayed on 2026-10-12 over 3 days of transcripts (14,580 Grep and shell calls): 37 more redirects, all folder
11
+ searches, and none of the previous ones lost.
7
12
 
8
13
  Measured on 2026-10-09 against 2,789 real decisions of the LLM router it replaced (session cwd recovered from the
9
14
  transcripts): the model redirected 315, this rule 463 (134 shared); in a sample of 40 redirected by the rule alone, 40
@@ -34,6 +39,43 @@ VALUE_FLAGS = frozenset({"-A", "-B", "-C", "-m", "--max-count", "--include", "--
34
39
  "--glob", "-t", "--type", "-T", "--type-not", "--max-depth", "-d", "--context",
35
40
  "--after-context", "--before-context"})
36
41
  RECURSIVE_FLAGS = frozenset({"-r", "-R", "--recursive", "-Recurse", "/s", "/S"})
42
+ READERS = frozenset({"cat", "head", "tail", "type", "get-content", "gc", "less", "more", "bat", "nl", "sed", "awk"})
43
+ READER_VALUE_FLAGS = {"head": frozenset({"-n", "-c", "--lines", "--bytes"}),
44
+ "tail": frozenset({"-n", "-c", "--lines", "--bytes", "-s", "--pid", "--sleep-interval"}),
45
+ "sed": frozenset({"-l", "--line-length"}), "awk": frozenset({"-v", "-F", "--assign"}),
46
+ "get-content": frozenset({"-totalcount", "-tail", "-head", "-first", "-last", "-encoding",
47
+ "-readcount", "-delimiter", "-stream"})}
48
+ READER_VALUE_FLAGS["gc"] = READER_VALUE_FLAGS["get-content"]
49
+ READER_SCRIPT_FLAGS = {"sed": frozenset({"-e", "-f", "--expression", "--file"}), "awk": frozenset({"-f", "--file"})}
50
+ # PowerShell cmdlets that read the content of the files piped into them (a listing piped to grep only filters names).
51
+ PIPED_CONTENT_READERS = frozenset({"select-string", "sls", "get-content", "gc"})
52
+ CAPPING_VERBS = frozenset({"head", "tail", "select-object", "select"})
53
+ LISTER_VALUE_FLAGS = frozenset({"-filter", "-include", "-exclude", "-depth", "-attributes", "-e", "--extension", "-t",
54
+ "--type", "-d", "--max-depth", "--exclude"})
55
+ PREFIX_WORDS = frozenset({"do", "then", "else", "elif", "if", "while", "until", "!", "time", "exec", "nohup", "sudo",
56
+ "command", "builtin", "{", "("})
57
+ GIT_FLAGS = frozenset({"--no-pager", "-P", "--paginate", "--no-replace-objects", "--literal-pathspecs", "--bare"})
58
+ MAX_NESTING = 3
59
+ LISTING = "__smart_tool_listing__"
60
+ SUBSTITUTION = "$__smart_tool_substitution__"
61
+ _HEREDOC = re.compile(r"<<-?\s*(['\"]?)(\w+)\1[^\n]*\n.*?\n[ \t]*\2[ \t]*(?=\n|$)", re.DOTALL)
62
+ _INLINE_INTERPRETER = re.compile(r"(?:^|[\s;&|(`/\\])(?:python[\d.]*|py|node|deno|bun)(?:\.exe)?\b[^\n]*?"
63
+ r"(?:\s-[A-Za-z]*[ce]\s|\s--eval\b|\s-p\s|\s-(?:\s|$)|<<)", re.IGNORECASE)
64
+ _WALK = re.compile(r"os\.walk\(|\.rglob\(|glob\.i?glob\([^)]*recursive\s*=\s*True|['\"][^'\"\n]*\*\*[/\\]|"
65
+ r"readdirSync\([^)]*recursive|readdir\([^)]*recursive|\bwalk(?:Sync)?\(|globSync\(|"
66
+ r"Get-ChildItem[^|\n]*-Recurse", re.IGNORECASE)
67
+ _CONTENT_READ = re.compile(r"\bopen\(|read_text\(|read_bytes\(|readFileSync\(|readFile\(|\.read\(\)|Get-Content",
68
+ re.IGNORECASE)
69
+ _TEXT_MATCH = re.compile(r"\bre\.(?:search|findall|finditer|match|fullmatch|compile)\(|\.includes\(|\.indexOf\(|"
70
+ r"\.find\(|\.match(?:All)?\(|\.test\(|\.search\(|['\"][^'\"\n]+['\"]\s+in\s+\w+|Select-String",
71
+ re.IGNORECASE)
72
+ _SCRIPT_WRITES = re.compile(r"\.write\(|write_text\(|writeFileSync|writeFile\(|appendFileSync|os\.remove|os\.rename|"
73
+ r"shutil\.|unlink\(|Set-Content|Out-File", re.IGNORECASE)
74
+ _CWD_WALK = re.compile(r"os\.getcwd\(\)|process\.cwd\(\)|Path\(\)|os\.walk\(\s*\)")
75
+ # Scanned apart: a script quoted one way holds its string literals quoted the other way.
76
+ _SINGLE_QUOTED = re.compile(r"'([^'\n]+)'")
77
+ _DOUBLE_QUOTED = re.compile(r'"([^"\n]+)"')
78
+ _PS_READ = re.compile(r"\b(?:get-content|gc|select-string|sls|cat|type)\b|\[io\.file\]", re.IGNORECASE)
37
79
  _ASSIGN = re.compile(r"^([A-Za-z_][A-Za-z0-9_]*)=(.*)$")
38
80
  _VARIABLE = re.compile(r"\$\{(\w+)\}|\$(\w+)")
39
81
 
@@ -122,93 +164,385 @@ def _paths(raw, base):
122
164
  return [path] if path and os.path.exists(path) else []
123
165
 
124
166
 
167
+ def _folder(target, cwd):
168
+ """Whether target is a folder of a project holding files: the reach a native search should not take."""
169
+ return bool(target) and os.path.isdir(target) and _in_project(target, cwd) and _has_files(target)
170
+
171
+
172
+ def _split(text, powershell):
173
+ """Words of one simple command. PowerShell keeps backslashes (Windows paths), so its quotes are only stripped."""
174
+ try:
175
+ if powershell:
176
+ return [t[1:-1] if len(t) > 1 and t[0] == t[-1] and t[0] in "'\"" else t
177
+ for t in shlex.split(text, posix=False)]
178
+ return shlex.split(text, posix=True)
179
+ except ValueError:
180
+ return text.split()
181
+
182
+
183
+ def _unwrap(tokens, variables):
184
+ """The command a prefix runs: subshell and group brackets, loop keywords, wrappers (sudo, time, timeout, nice,
185
+ env, nohup...) and leading `NAME=value` assignments, which are stored in variables."""
186
+ tokens = list(tokens)
187
+ while tokens:
188
+ head, low = tokens[0], tokens[0].lower()
189
+ if len(head) > 1 and head[0] in "({" and head != "{}":
190
+ tokens[0] = head.lstrip("({")
191
+ elif low in PREFIX_WORDS:
192
+ tokens = tokens[1:]
193
+ elif _ASSIGN.match(head):
194
+ name, value = _ASSIGN.match(head).groups()
195
+ variables[name] = _expand(value, variables)
196
+ tokens = tokens[1:]
197
+ elif low in ("env", "timeout", "nice", "ionice", "stdbuf"):
198
+ tokens = tokens[1:]
199
+ while tokens and tokens[0].startswith("-"):
200
+ flag = tokens.pop(0)
201
+ if flag in ("-s", "-k", "-n", "-c", "-u", "--signal", "--kill-after", "--unset") and tokens:
202
+ tokens.pop(0)
203
+ if low == "timeout" and tokens:
204
+ tokens.pop(0)
205
+ else:
206
+ break
207
+ while tokens and tokens[-1] in (")", "}", "done", "fi", "esac"):
208
+ tokens = tokens[:-1]
209
+ if tokens and tokens[-1].endswith(")") and tokens[-1].count(")") > tokens[-1].count("("):
210
+ tokens[-1] = tokens[-1].rstrip(")") or "."
211
+ return tokens
212
+
213
+
214
+ def _strip_heredocs(command):
215
+ """The command without heredoc bodies: their lines are data for the command that reads them, not commands."""
216
+ return _HEREDOC.sub(lambda m: m.group(0)[:m.group(0).index("\n") + 1], command)
217
+
218
+
219
+ def _substitutions(command, powershell):
220
+ """(command with each `$(...)` and backtick substitution replaced by one word, inner commands). The word is LISTING
221
+ when the inner command lists the files of a tree (its output names files to read), else an unresolved variable."""
222
+ out, inner, i, quote = [], [], 0, None
223
+ while i < len(command):
224
+ ch = command[i]
225
+ if quote == "'":
226
+ quote = None if ch == "'" else quote
227
+ elif ch == "'" and quote is None:
228
+ quote = "'"
229
+ elif ch == '"':
230
+ quote = None if quote == '"' else '"'
231
+ if quote != "'" and command.startswith("$(", i) and not command.startswith("$((", i):
232
+ depth, j = 1, i + 2
233
+ while j < len(command) and depth:
234
+ depth += {"(": 1, ")": -1}.get(command[j], 0)
235
+ j += 1
236
+ text = command[i + 2:j - 1]
237
+ elif quote != "'" and ch == "`" and not powershell:
238
+ j = command.find("`", i + 1)
239
+ j = len(command) if j < 0 else j + 1
240
+ text = command[i + 1:j - 1]
241
+ else:
242
+ out.append(ch)
243
+ i += 1
244
+ continue
245
+ inner.append(text)
246
+ out.append(LISTING if _lists_tree(text, powershell) else SUBSTITUTION)
247
+ i = j
248
+ return "".join(out), inner
249
+
250
+
251
+ def _lister_path(verb, tokens):
252
+ """The folder a listing command walks (`.` when none is named)."""
253
+ rest = tokens[1:]
254
+ if verb == "find":
255
+ return next((t for t in rest[:1] if not t.startswith("-") and t not in ("(", "!")), ".")
256
+ positional, skip = [], False
257
+ for index, token in enumerate(rest):
258
+ low = token.lower()
259
+ if skip:
260
+ skip = False
261
+ elif low in ("-path", "-literalpath") and index + 1 < len(rest):
262
+ return rest[index + 1]
263
+ elif low in LISTER_VALUE_FLAGS:
264
+ skip = True
265
+ elif not token.startswith("-"):
266
+ positional.append(token)
267
+ if verb == "fd":
268
+ positional = positional[1:]
269
+ return positional[0] if positional else "."
270
+
271
+
272
+ def _lister_recursive(verb, tokens):
273
+ lows = [t.lower() for t in tokens[1:]]
274
+ if verb in ("find", "fd", "tree"):
275
+ return True
276
+ if verb in ("get-childitem", "gci") and any(len(t) > 1 and "-recurse".startswith(t) for t in lows):
277
+ return True
278
+ return "-recurse" in lows or "/s" in lows or any(re.fullmatch(r"-[a-zA-Z]*R[a-zA-Z]*", t) for t in tokens[1:])
279
+
280
+
281
+ def _lists_tree(text, powershell):
282
+ """Whether a command prints the files of a whole tree, uncapped: a recursive listing, `git ls-files`, `rg --files`."""
283
+ parts = segments(_strip_heredocs(text))
284
+ if not parts:
285
+ return False
286
+ tokens = _unwrap(_split(parts[0][0], powershell), {})
287
+ if not tokens:
288
+ return False
289
+ verb = tokens[0].lower()
290
+ tree = (verb in LISTERS and _lister_recursive(verb, tokens)) or (verb in ("rg", "ripgrep") and "--files" in tokens) \
291
+ or (verb == "git" and "ls-files" in tokens)
292
+ capped = any(_split(part, powershell)[:1] and _split(part, powershell)[0].lower() in CAPPING_VERBS
293
+ for part, _pipe in parts[1:]) or (verb == "find" and any(
294
+ flag in ("-name", "-iname") and not any(ch in value for ch in "*?[")
295
+ for flag, value in zip(tokens, tokens[1:])))
296
+ return tree and not capped
297
+
298
+
299
+ def _with_lists(tokens, lists):
300
+ """Tokens with each `$name` of a loop variable replaced by the words the loop runs over."""
301
+ out = []
302
+ for token in tokens:
303
+ match = re.fullmatch(r"\$\{?(\w+)\}?", token)
304
+ out.extend(lists[match.group(1)] if match and match.group(1) in lists else [token])
305
+ return out
306
+
307
+
308
+ def _inner_script(tokens):
309
+ """The script a shell invoker runs: `bash -c S`, `sh -lc S`, `powershell -Command S`, `cmd /c S`; else None."""
310
+ verb, rest = re.sub(r"\.exe$", "", tokens[0].lower()), tokens[1:]
311
+ for index, token in enumerate(rest):
312
+ low = token.lower()
313
+ if verb in ("bash", "sh", "zsh", "dash", "ksh") and re.fullmatch(r"-[a-z]*c[a-z]*", token):
314
+ return rest[index + 1] if index + 1 < len(rest) else None
315
+ if verb in ("powershell", "pwsh") and low in ("-command", "-c") or verb == "cmd" and low in ("/c", "/k"):
316
+ return " ".join(rest[index + 1:]) or None
317
+ return None
318
+
319
+
125
320
  def _search_args(verb, tokens):
126
321
  """Path arguments of a grep-like command (the pattern and flag values dropped) and whether it is recursive."""
127
- recursive = verb in ALWAYS_RECURSIVE or any(
128
- t in RECURSIVE_FLAGS or (re.fullmatch(r"-[a-zA-Z]+", t) and ("r" in t or "R" in t)) for t in tokens)
129
- args, skip, pattern_given = [], False, False
322
+ if verb in ("select-string", "sls"):
323
+ recursive = False
324
+ else:
325
+ recursive = verb in ALWAYS_RECURSIVE or any(
326
+ 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 = [], [], False, False, False
130
328
  for token in tokens:
329
+ low = token.lower()
131
330
  if skip:
132
331
  skip = False
133
- elif token in PATTERN_FLAGS:
332
+ elif path_next:
333
+ paths.append(token)
334
+ path_next = False
335
+ elif token in PATTERN_FLAGS or low == "-pattern":
134
336
  skip, pattern_given = True, True
135
- elif token in VALUE_FLAGS:
337
+ elif low in ("-path", "-literalpath"):
338
+ path_next = True
339
+ elif token in VALUE_FLAGS or low in ("-include", "-exclude", "-context", "-encoding"):
136
340
  skip = True
137
341
  elif not token.startswith("-") and not (verb == "findstr" and token.startswith("/")):
138
342
  args.append(token)
139
- return (args if pattern_given else args[1:]), recursive
140
-
141
-
142
- def _bash(command, cwd):
143
- base, variables, verdict = cwd, {}, ("allow", "No content search over a folder.")
144
- for text, after_pipe in segments(command):
145
- try:
146
- tokens = shlex.split(text, posix=True)
147
- except ValueError:
148
- tokens = text.split()
149
- while tokens and _ASSIGN.match(tokens[0]):
150
- name, value = _ASSIGN.match(tokens[0]).groups()
151
- variables[name] = _expand(value, variables)
152
- tokens = tokens[1:]
343
+ return paths + (args if pattern_given else args[1:]), recursive
344
+
345
+
346
+ def _reader_files(verb, tokens):
347
+ """File arguments of a command that prints file contents (cat, head, tail, sed -n, awk, Get-Content...)."""
348
+ files, skip, path_next, script_given = [], False, False, False
349
+ values = READER_VALUE_FLAGS.get(verb, frozenset())
350
+ for token in tokens:
351
+ low = token.lower()
352
+ if skip:
353
+ skip = False
354
+ elif path_next:
355
+ files.append(token)
356
+ path_next = False
357
+ elif low in ("-path", "-literalpath") and verb in ("get-content", "gc", "type"):
358
+ path_next = True
359
+ elif token in READER_SCRIPT_FLAGS.get(verb, ()):
360
+ skip, script_given = True, True
361
+ elif token in values or (verb in ("get-content", "gc") and low in values):
362
+ skip = True
363
+ elif not token.startswith("-") and not token.startswith("+"):
364
+ files.append(token)
365
+ return files[1:] if verb in ("sed", "awk") and not script_given else files
366
+
367
+
368
+ def _inline_script(command, base, cwd):
369
+ """Redirect for an inline python/node script (`-c`, `-e`, heredoc) that walks a folder of a project, reads the
370
+ files and matches their text: a content search written as a script."""
371
+ if not (_INLINE_INTERPRETER.search(command) and _WALK.search(command) and _CONTENT_READ.search(command)
372
+ and _TEXT_MATCH.search(command)) or _SCRIPT_WRITES.search(command):
373
+ return None
374
+ for literal in _SINGLE_QUOTED.findall(command) + _DOUBLE_QUOTED.findall(command):
375
+ prefix = re.split(r"[*?\[]", literal, maxsplit=1)[0]
376
+ if not prefix or prefix in (".", "./"):
377
+ target = base
378
+ elif "\n" in prefix or len(prefix) > 260:
379
+ continue
380
+ else:
381
+ target = resolve_path(_expand(prefix, {}), base)
382
+ if _folder(target, cwd):
383
+ return "redirect", target, "content search over a folder in an inline script"
384
+ if _CWD_WALK.search(command) and _folder(base, cwd):
385
+ return "redirect", base, "content search over a folder in an inline script"
386
+ return None
387
+
388
+
389
+ def _bash(command, cwd, powershell=False):
390
+ found = _inline_script(command, cwd, cwd)
391
+ if found:
392
+ return found
393
+ return _shell(command, cwd, {"base": cwd, "variables": {}, "lists": {}}, powershell, 0)
394
+
395
+
396
+ def _shell(command, cwd, state, powershell, depth):
397
+ """(decision, target, reason) of one shell script; substitutions and `bash -c`/`powershell -Command` scripts are
398
+ decided with the same state (folder, variables, loop variables)."""
399
+ verdict = ("allow", None, "No content search over a folder.")
400
+ text, inner = _substitutions(_strip_heredocs(command), powershell)
401
+ for script in inner if depth < MAX_NESTING else ():
402
+ found = _shell(script, cwd, state, powershell, depth + 1)
403
+ if found[0] == "redirect":
404
+ return found
405
+ variables, lists, listing = state["variables"], state["lists"], None
406
+ for segment, after_pipe in segments(text):
407
+ words = _split(segment, powershell)
408
+ if words[:2] == ["while", "read"]:
409
+ name = next((t for t in words[2:] if not t.startswith("-")), None)
410
+ if name and listing and listing[1] and not listing[2]:
411
+ lists[name] = [LISTING]
412
+ continue
413
+ tokens = _unwrap(words, variables)
153
414
  if not tokens:
154
415
  continue
155
- verb = tokens[0].lower()
416
+ if len(tokens) >= 3 and re.fullmatch(r"\$\w+", tokens[0]) and tokens[1] == "=":
417
+ variables[tokens[0][1:]] = _expand(tokens[2], variables)
418
+ continue
419
+ tokens = _with_lists(tokens, lists)
420
+ verb, base = tokens[0].lower(), state["base"]
421
+ if not after_pipe:
422
+ listing = None
423
+ if verb == "for" and len(tokens) > 2 and tokens[2] == "in":
424
+ lists[tokens[1]] = [_expand(t, variables) for t in tokens[3:]]
425
+ continue
426
+ script = _inner_script(tokens) if depth < MAX_NESTING else None
427
+ if script:
428
+ windows = re.sub(r"\.exe$", "", verb) in ("powershell", "pwsh", "cmd")
429
+ found = _shell(script, cwd, state, windows, depth + 1)
430
+ if found[0] == "redirect":
431
+ return found
432
+ continue
156
433
  if verb in CD_VERBS and len(tokens) > 1:
157
- base = resolve_path(_expand(tokens[1], variables), base) or base
434
+ state["base"] = resolve_path(_expand(tokens[1], variables), base) or base
158
435
  continue
159
- xargs = verb == "xargs" and len(tokens) > 1 and tokens[1].lower() in SEARCHERS
160
- if after_pipe and not xargs:
436
+ if verb == "xargs":
437
+ consumer = _xargs_command(tokens)
438
+ source = listing[0] if listing else base
439
+ if consumer in SEARCHERS and _folder(source, cwd):
440
+ return "redirect", source, "content search over the files of a listing"
441
+ if consumer in READERS and listing and listing[1] and not listing[2] and _folder(source, cwd):
442
+ return "redirect", source, "reading every file of a listing"
161
443
  continue
162
- if xargs:
163
- if base and os.path.isdir(base) and _in_project(base, cwd) and _has_files(base):
164
- return "redirect", base, "content search over the files of a listing"
444
+ if after_pipe:
445
+ if listing and verb in CAPPING_VERBS:
446
+ listing = (listing[0], listing[1], True)
447
+ elif listing and not listing[2] and (verb in PIPED_CONTENT_READERS or (
448
+ verb in ("foreach-object", "%", "foreach") and _PS_READ.search(segment))) and \
449
+ _folder(listing[0], cwd):
450
+ return "redirect", listing[0], "content search over the files of a listing"
165
451
  continue
166
452
  if verb == "git":
167
- rest, repo = tokens[1:], base
168
- while len(rest) > 1 and rest[0] == "-C":
169
- repo = resolve_path(_expand(rest[1], variables), repo) or repo
170
- rest = rest[2:]
171
- if rest and rest[0] == "grep":
172
- specs = rest[rest.index("--") + 1:] if "--" in rest else []
173
- found = [_paths(_expand(raw, variables), repo) for raw in specs]
174
- only_files = specs and all(f and all(os.path.isfile(p) for p in f) for f in found)
175
- listed = sum(len(f) for f in found if f) if only_files else 0
176
- if (not only_files or listed > LISTED_MAX) and repo and os.path.isdir(repo) and \
177
- _in_project(repo, cwd) and _has_files(repo):
178
- return "redirect", repo, "git grep over the repository"
453
+ found = _git(tokens, variables, base, cwd)
454
+ if found:
455
+ return found
179
456
  continue
180
457
  if verb in LISTERS:
181
- recursive = verb in ("find", "fd", "tree") or "-recurse" in (t.lower() for t in tokens) or any(
182
- re.fullmatch(r"-[a-zA-Z]*R[a-zA-Z]*", t) for t in tokens[1:])
183
- first = next((t for t in tokens[1:] if not t.startswith("-")), ".")
184
- for target in (_paths(_expand(first, variables), base) or []) if recursive else []:
458
+ recursive = _lister_recursive(verb, tokens)
459
+ targets = _paths(_expand(_lister_path(verb, tokens), variables), base) or []
460
+ listing = (targets[0], recursive, False) if len(targets) == 1 and os.path.isdir(targets[0]) else None
461
+ execs = [i for i, t in enumerate(tokens) if t in ("-exec", "-execdir", "-ok", "-okdir")]
462
+ if verb == "find" and listing and any(i + 1 < len(tokens) and tokens[i + 1].lower() in SEARCHERS | READERS
463
+ for i in execs) and _folder(listing[0], cwd):
464
+ return "redirect", listing[0], "content search over the files of a listing"
465
+ for target in targets if recursive else []:
185
466
  if os.path.isdir(target) and _in_project(target, cwd) and count_files(target)[1]:
186
467
  return "redirect", target, "recursive listing of a very large tree"
187
468
  continue
188
- if verb not in SEARCHERS:
469
+ if verb in SEARCHERS:
470
+ if verb in ("rg", "ripgrep") and "--files" in tokens:
471
+ continue
472
+ found, verdict = _search(verb, tokens, variables, base, cwd, verdict)
473
+ if found:
474
+ return found
189
475
  continue
190
- args, recursive = _search_args(verb, tokens[1:])
191
- if not args:
192
- if recursive and base and os.path.isdir(base) and _in_project(base, cwd) and _has_files(base):
193
- return "redirect", base, "recursive content search of the working folder"
476
+ if verb in READERS:
477
+ files = _reader_files(verb, tokens[1:])
478
+ if LISTING in files and _folder(base, cwd):
479
+ return "redirect", base, "reading every file of a listing"
480
+ listed = sum(len([p for p in found if os.path.isfile(p)])
481
+ for found in (_paths(_expand(raw, variables), base) for raw in files) if found)
482
+ if listed > LISTED_MAX:
483
+ return "redirect", base, f"reading {listed} files"
484
+ return verdict
485
+
486
+
487
+ def _xargs_command(tokens):
488
+ """The verb xargs runs, past its own flags."""
489
+ rest = tokens[1:]
490
+ while rest and rest[0].startswith("-"):
491
+ flag = rest.pop(0)
492
+ if flag in ("-I", "-n", "-P", "-d", "-L", "-s", "-E", "-a", "--delimiter", "--max-args", "--max-procs") and rest:
493
+ rest.pop(0)
494
+ return rest[0].lower() if rest else None
495
+
496
+
497
+ def _git(tokens, variables, base, cwd):
498
+ """Redirect for `git grep` over the repository (global options such as -C, -c and --no-pager skipped)."""
499
+ rest, repo = tokens[1:], base
500
+ while rest:
501
+ if rest[0] in ("-C", "-c") and len(rest) > 1:
502
+ if rest[0] == "-C":
503
+ repo = resolve_path(_expand(rest[1], variables), repo) or repo
504
+ rest = rest[2:]
505
+ elif rest[0] in GIT_FLAGS or rest[0].startswith(("--git-dir=", "--work-tree=", "--namespace=", "-c")):
506
+ rest = rest[1:]
507
+ else:
508
+ break
509
+ if not rest or rest[0] != "grep":
510
+ return None
511
+ specs = rest[rest.index("--") + 1:] if "--" in rest else []
512
+ found = [_paths(_expand(raw, variables), repo) for raw in specs]
513
+ only_files = specs and all(f and all(os.path.isfile(p) for p in f) for f in found)
514
+ listed = sum(len(f) for f in found if f) if only_files else 0
515
+ if (not only_files or listed > LISTED_MAX) and _folder(repo, cwd):
516
+ return "redirect", repo, "git grep over the repository"
517
+ return None
518
+
519
+
520
+ def _search(verb, tokens, variables, base, cwd, verdict):
521
+ """(redirect or None, verdict so far) for a grep-like command."""
522
+ args, recursive = _search_args(verb, tokens[1:])
523
+ if LISTING in args:
524
+ return (("redirect", base, "content search over the files of a listing") if _folder(base, cwd) else None), verdict
525
+ if not args:
526
+ if recursive and _folder(base, cwd):
527
+ return ("redirect", base, "recursive content search of the working folder"), verdict
528
+ return None, verdict
529
+ listed = 0
530
+ for raw in args:
531
+ found = _paths(_expand(raw, variables), base)
532
+ if found is None:
533
+ verdict = ("allow", None, "The searched path uses a variable the hook cannot resolve.")
194
534
  continue
195
- listed = 0
196
- for raw in args:
197
- found = _paths(_expand(raw, variables), base)
198
- if found is None:
199
- verdict = ("allow", "The searched path uses a variable the hook cannot resolve.")
200
- continue
201
- for target in found:
202
- if os.path.isdir(target):
203
- if recursive and _in_project(target, cwd) and _has_files(target):
204
- return "redirect", target, "recursive content search over a folder"
205
- else:
206
- listed += 1
207
- if listed > LISTED_MAX:
208
- return "redirect", base, f"content search over {listed} files"
209
- if listed:
210
- verdict = ("allow", "Search inside listed files.")
211
- return verdict[0], None, verdict[1]
535
+ for target in found:
536
+ if os.path.isdir(target):
537
+ if recursive and _in_project(target, cwd) and _has_files(target):
538
+ return ("redirect", target, "recursive content search over a folder"), verdict
539
+ else:
540
+ listed += 1
541
+ if listed > LISTED_MAX:
542
+ return ("redirect", base, f"content search over {listed} files"), verdict
543
+ if listed:
544
+ verdict = ("allow", None, "Search inside listed files.")
545
+ return None, verdict
212
546
 
213
547
 
214
548
  def decide(tool_name, tool_input, cwd):
@@ -227,6 +561,6 @@ def decide(tool_name, tool_input, cwd):
227
561
  if not _has_files(target):
228
562
  return "allow", None, "The folder has no files."
229
563
  return "redirect", target, "content search over a folder"
230
- if tool_name == "Bash":
231
- return _bash(tool_input.get("command") or "", cwd)
564
+ if tool_name in ("Bash", "PowerShell"):
565
+ return _bash(tool_input.get("command") or "", cwd, powershell=tool_name == "PowerShell")
232
566
  return "allow", None, "Tool not redirected."
package/router.py CHANGED
@@ -20,6 +20,7 @@ import web_search_adapters
20
20
 
21
21
  # Per request: the daemon serves each hook call on its own thread, so a module global mixed up clients.
22
22
  CLIENT = contextvars.ContextVar("router_client", default=None)
23
+ SESSION = contextvars.ContextVar("router_session", default=None)
23
24
  METRICS_PATH = os.path.join(paths.DATA_DIR, "router-metrics.jsonl")
24
25
 
25
26
  METRICS_MAX_BYTES = 4 * 1024 * 1024
@@ -61,6 +62,7 @@ def _log_decision(tool_name, tool_input, decision, reason, extra=None):
61
62
  "decision": decision,
62
63
  "reason": reason,
63
64
  **({"client": CLIENT.get()} if CLIENT.get() else {}),
65
+ **({"session": SESSION.get()} if SESSION.get() else {}),
64
66
  }
65
67
  record.update(extra or {})
66
68
  try:
@@ -85,21 +87,25 @@ def log_unavailable(tool_name, tool_input, exc):
85
87
 
86
88
 
87
89
  # Search/read verbs count only where they start a command: at the beginning, after `;`, `&&`, `||`, `$(`, a backtick,
88
- # a loop/if keyword or a shell invoker (`bash -c`, `powershell -Command`, `cmd /c`). After a single `|` they filter the
90
+ # a loop/if keyword, a subshell bracket, a wrapper (`timeout 60`, `env X=1`, `nice`) or a shell invoker (`bash -c`,
91
+ # `powershell -Command`, `cmd /c`). After a single `|` they filter the
89
92
  # output of the command before them (`python x.py | tail -5`), which reads no source code.
90
93
  _SEARCH_VERB_RE = re.compile(
91
- r"(?:^|[;&]|\|\||\$\(|`|-c\s|-Command\s|/c\s|/k\s)\s*[\"']?\s*(?:(?:do|then|else)\s+)?(?:sudo\s+|command\s+)?"
92
- r"(grep|egrep|fgrep|rg|ripgrep|find|findstr|fd|cat|type|head|tail|less|more|"
94
+ r"(?:^|[;&\n({]|\|\||\$\(|`|-c\s|-Command\s|/c\s|/k\s)\s*[\"']?\s*(?:(?:do|then|else)\s+)?"
95
+ r"(?:(?:sudo|command|time|nice|nohup|exec|env|timeout|stdbuf)(?:\s+-\S+)*(?:\s+\d\S*)?\s+|\w+=\S*\s+)*"
96
+ r"(grep|egrep|fgrep|rg|ripgrep|find|findstr|fd|cat|type|head|tail|less|more|bat|nl|"
93
97
  r"ls|dir|awk|sed|ack|ag|select-string|sls|get-content|gc|get-childitem|gci)\b",
94
98
  re.IGNORECASE,
95
99
  )
96
100
  # Commands that read code or pages without a search verb at their start: their pipe filters still search content.
97
101
  _READ_PRODUCER_RE = re.compile(
98
- r"\bgit\s+(?:-C\s+\S+\s+)?(grep|ls-files|show|log\s+-p|diff)\b|\b(curl|wget|iwr|invoke-webrequest)\b|\bgh\s+api\b|"
99
- r"readFileSync|read_text\(|open\(",
102
+ r"\bgit\s+(?:(?:-C|-c)\s+\S+\s+|--no-pager\s+|-P\s+)*(grep|ls-files|show|log\s+-p|diff)\b|"
103
+ r"\b(curl|wget|iwr|invoke-webrequest)\b|\bgh\s+api\b|"
104
+ r"readFileSync|read_text\(|open\(|os\.walk\(|\.rglob\(|readdirSync|globSync\(",
100
105
  re.IGNORECASE,
101
106
  )
102
107
  _READ_TOOLS = frozenset({"Read", "NotebookRead"})
108
+ SHELL_TOOLS = frozenset({"Bash", "PowerShell"})
103
109
  # Commands that change files, the repository or dependencies are never redirected (smart_search cannot do the change).
104
110
  # Verbs are matched outside quoted strings, so a grep pattern like "pip install" is not one; file writes inside an
105
111
  # inline script (node -e, python -c) are matched anywhere.
@@ -132,7 +138,7 @@ def _mechanical_decision(tool_name, tool_input):
132
138
  errados: o agente contornava o bloqueio) e 566 liberações."""
133
139
  if tool_name in _READ_TOOLS:
134
140
  return "allow", "Reading a specific file is never replaced by semantic search."
135
- if tool_name == "Bash":
141
+ if tool_name in SHELL_TOOLS:
136
142
  command = tool_input.get("command")
137
143
  if not isinstance(command, str) or not command.strip():
138
144
  return "allow", "Bash without a readable command."
@@ -158,8 +164,9 @@ def decide(tool_name, tool_input, cwd=None):
158
164
  _log_decision(tool_name, tool_input, decision, reason, extra={"mechanical": True})
159
165
  return decision, reason, None
160
166
  decision, target, reason = redirect_rule.decide(tool_name, safe_input, cwd)
161
- learned = (block_patterns.matching(tool_name, safe_input)
162
- if decision == "allow" and block_patterns.reviewable(tool_name, safe_input, reason) else None)
167
+ pattern_tool = "Bash" if tool_name in SHELL_TOOLS else tool_name
168
+ learned = (block_patterns.matching(pattern_tool, safe_input)
169
+ if decision == "allow" and block_patterns.reviewable(pattern_tool, safe_input, reason) else None)
163
170
  if learned:
164
171
  reason = f"accepted block pattern {learned['id']} (approved by the user in Smart Tool's setup screen)"
165
172
  _log_decision(tool_name, tool_input, "redirect", reason, extra={"pattern": learned["id"]})
@@ -167,7 +174,7 @@ def decide(tool_name, tool_input, cwd=None):
167
174
  _log_decision(tool_name, tool_input, decision, reason, extra={"rule": True})
168
175
  if decision == "allow":
169
176
  try:
170
- block_patterns.review_later(tool_name, safe_input, reason, METRICS_PATH, _redacted_input, _log_error)
177
+ block_patterns.review_later(pattern_tool, safe_input, reason, METRICS_PATH, _redacted_input, _log_error)
171
178
  except (OSError, ValueError) as exc:
172
179
  _log_error(f"block pattern review not queued: {exc}")
173
180
  return decision, reason, target
package/setup_ui.py CHANGED
@@ -32,6 +32,7 @@ import local_embedder
32
32
  import project_store
33
33
  import secret_store
34
34
  import setup_audit
35
+ import integration_metrics
35
36
  import version
36
37
  import web_search_external
37
38
 
@@ -208,7 +209,8 @@ def handle_integration_get():
208
209
  "duplicate_mode_error": duplicate_error, "duplicate_modes": list(config.DUPLICATE_MODES),
209
210
  "pattern_proposals": proposals_mode, "pattern_proposals_error": proposals_error,
210
211
  "block_patterns": {"proposals": stored["proposals"], "accepted": stored["accepted"],
211
- "rejected": len(stored["rejected"]), "error": patterns_error}}
212
+ "rejected": len(stored["rejected"]), "error": patterns_error},
213
+ "redirects": integration_metrics.redirects()}
212
214
 
213
215
 
214
216
  def handle_integration_save(body):
@@ -690,6 +692,7 @@ _PAGE_TEMPLATE = """<!doctype html>
690
692
  <span class="hint">The hook decides with a fixed rule: content searches over a project folder go to smart_search. The scope model (else the router model) never delays a call: it reviews, afterwards, bulk reads the rule could not measure and proposes patterns, each checked against the logged calls. Nothing blocks until you accept it here.</span></div>
691
693
  <div class="actions"><button type="button" id="btn-save-pattern-proposals">Save proposals setting</button></div>
692
694
  <div id="patterns-list"></div>
695
+ <div id="redirects-panel" class="pattern-panel"></div>
693
696
  <div class="msg" id="agents-msg" role="status"></div>
694
697
  <div id="agents-list"></div>
695
698
  </section>
@@ -1002,6 +1005,7 @@ async function loadAgents() {
1002
1005
  if (document.activeElement !== $("sel-pattern-proposals")) $("sel-pattern-proposals").value = data.pattern_proposals;
1003
1006
  if (data.pattern_proposals_error) setMsg("agents-msg", "error", data.pattern_proposals_error);
1004
1007
  renderPatterns(data.block_patterns);
1008
+ renderRedirects(data.redirects);
1005
1009
  const list = $("agents-list");
1006
1010
  list.textContent = "";
1007
1011
  for (const item of data.clients) {
@@ -1149,6 +1153,23 @@ function patternRow(item, actions, selectable) {
1149
1153
  row.appendChild(main);
1150
1154
  return row;
1151
1155
  }
1156
+ function renderRedirects(stats) {
1157
+ const panel = $("redirects-panel");
1158
+ panel.textContent = "";
1159
+ panel.append(patternEl("h3", "", "Native searches redirected (last " + stats.days + " days): " + stats.total));
1160
+ const out = stats.outcomes, tokens = stats.tokens_per_call;
1161
+ const lines = [
1162
+ "By tool: " + (Object.entries(stats.by_tool).map(([k, v]) => k + " " + v).join(", ") || "none"),
1163
+ "Next call of the same session: smart_search " + (out.followed || 0) + ", allowed native call " + (out.native || 0)
1164
+ + ", blocked again " + (out.blocked_again || 0) + ", none within 15 min " + (out.no_other_call || 0)
1165
+ + ", unknown (no session logged) " + (out.unknown || 0),
1166
+ "Context per call (measured " + tokens.measured + "): a native call of the kinds redirected ~" + tokens.native_redirected
1167
+ + " tokens; a smart_search round trip ~" + tokens.smart_search + " tokens. A redirect saves context only when one"
1168
+ + " smart_search replaces several native searches and reads; that part is not measured yet.",
1169
+ ];
1170
+ for (const line of lines) panel.append(patternEl("p", "hint", line));
1171
+ for (const [reason, count] of Object.entries(stats.by_reason)) panel.append(patternEl("p", "hint mono", count + " × " + reason));
1172
+ }
1152
1173
  function renderPatterns(patterns) {
1153
1174
  lastPatterns = patterns;
1154
1175
  const box = $("patterns-list");
package/version.py CHANGED
@@ -1,7 +1,7 @@
1
1
  """Single source of the Smart Tool version and of the User-Agent sent to public APIs."""
2
2
  import re
3
3
 
4
- VERSION = "0.9.14"
4
+ VERSION = "0.9.15"
5
5
  _CONTACT_RE = re.compile(r"^(?:[^\s@()<>;]+@[^\s@()<>;]+\.[^\s@()<>;]+|https?://[^\s()<>;]+)$")
6
6
 
7
7