@allansantos-dev/smart-tool 0.9.14 → 0.9.16

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/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