@allansantos-dev/smart-tool 0.9.67 → 0.9.69
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 +19 -0
- package/NOTICE +1 -1
- package/README.md +2 -2
- package/atomic_io.py +39 -0
- package/browser_control.py +3 -2
- package/client_hooks.py +29 -20
- package/codex_config.py +5 -2
- package/doc_insights.py +29 -1
- package/hook_decision.py +50 -14
- package/install.py +2 -1
- package/locales/pt-BR.json +5 -1
- package/package.json +2 -2
- package/project_identity.py +19 -1
- package/setup_audit.py +43 -2
- package/smart_tool_daemon.py +31 -7
- package/version.py +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.9.69 - beta
|
|
4
|
+
|
|
5
|
+
- `smart_search` answers leaner by default: `detail=compact` cuts each excerpt to whole lines within 240 characters (was 500)
|
|
6
|
+
and leaves out the usual values of `ranking` (`rerank`), `location_kind` (`source_line`) and an empty `symbols`; path:lines,
|
|
7
|
+
score and name:line symbols stay, and the ranked places are the same. `detail=full` returns the previous answer. Measured on
|
|
8
|
+
8 fixed queries with `tests/search_size_probe.py`: see `docs/research/data/search-size-before.json` and `-after.json`.
|
|
9
|
+
Test: `tests/test_search_detail.py`.
|
|
10
|
+
|
|
11
|
+
## 0.9.68 - beta
|
|
12
|
+
|
|
13
|
+
- A routed search is answered, not denied, when the host sends no `tool_use_id` or the query cannot be read from the
|
|
14
|
+
command: the Smart Tool answer goes into the call's context (Codex shell: the command prints it) and, without an
|
|
15
|
+
answer in time or without a query, the context names the exact `smart_search` call. `deny` stays only for edits the
|
|
16
|
+
docs and comment rules require redone.
|
|
17
|
+
- Another tool's marked block in Codex's `config.toml` survives Smart Tool's writes byte for byte. The in-place edit shipped in
|
|
18
|
+
0.9.67 is now covered for the hook approval record too (`tests/test_codex_audit_hook.py`), the `set_codex_disabled`
|
|
19
|
+
docstring describes the one-line insert, and `docs/research/2026-11-08-codex-config-marker-who-rewrites.md` lists every
|
|
20
|
+
writer of that file and the isolated proof that none of Smart Tool's current paths drops the marker. No behavior change.
|
|
21
|
+
|
|
3
22
|
## 0.9.67 - beta
|
|
4
23
|
|
|
5
24
|
- The idle daemon no longer rewrites its own registry in a loop. The folder where Smart Tool keeps its state was a
|
package/NOTICE
CHANGED
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ registered as the `playwright` server where this machine lets a browser be drive
|
|
|
17
17
|
An optional hook routes the agent's native Grep/Glob/Read/Bash/WebSearch/WebFetch calls to these tools when they
|
|
18
18
|
are cheaper, or just tells the agent what Smart Tool would do.
|
|
19
19
|
|
|
20
|
-
**Status:** 0.9.
|
|
20
|
+
**Status:** 0.9.69 beta. Windows only. What changed in each version: [CHANGELOG.md](CHANGELOG.md).
|
|
21
21
|
|
|
22
22
|
## How it works
|
|
23
23
|
|
|
@@ -83,7 +83,7 @@ gateway such as Ollama, nothing about your code leaves the machine.
|
|
|
83
83
|
## Install
|
|
84
84
|
|
|
85
85
|
```powershell
|
|
86
|
-
npx @allansantos-dev/smart-tool@0.9.
|
|
86
|
+
npx @allansantos-dev/smart-tool@0.9.69 install
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
Updates install themselves, on by default: the tray checks npm when it starts (at logon) and every 24 h, and when there
|
package/atomic_io.py
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
do Smart Tool."""
|
|
4
4
|
import glob
|
|
5
5
|
import os
|
|
6
|
+
import re
|
|
7
|
+
import shutil
|
|
6
8
|
import time
|
|
7
9
|
import uuid
|
|
8
10
|
|
|
@@ -98,3 +100,40 @@ def write_text(final_path, text, encoding="utf-8"):
|
|
|
98
100
|
"""Atomic text write of a file other programs and people read and edit (project files): the same temp, fsync and
|
|
99
101
|
replace as write_secret_text, with the permissions the user's umask gives a new file."""
|
|
100
102
|
_write_and_replace(final_path, text, encoding=encoding, permissions=0o666)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
BACKUP_INFIX = ".smart-tool-backup-"
|
|
106
|
+
_BACKUP_STAMP = re.compile(r"^(\d{8}-\d{6})(?:-(\d+))?$")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def backup_file(path, keep=None):
|
|
110
|
+
"""Copies path to a timestamped `.smart-tool-backup-` file beside it and returns that path, None when path does not
|
|
111
|
+
exist. A second backup in the same second gets a numbered name, so it never overwrites the copy of an earlier step.
|
|
112
|
+
With keep, only that many of the newest copies of path stay; other files with a similar name are left alone."""
|
|
113
|
+
if not os.path.exists(path):
|
|
114
|
+
return None
|
|
115
|
+
moment = time.strftime("%Y%m%d-%H%M%S")
|
|
116
|
+
number = max((order[1] for _, order in _backups(path, moment)), default=0) + 1
|
|
117
|
+
backup = f"{path}{BACKUP_INFIX}{moment}" + (f"-{number}" if number > 1 else "")
|
|
118
|
+
shutil.copy2(path, backup)
|
|
119
|
+
if keep is not None:
|
|
120
|
+
_prune_backups(path, keep)
|
|
121
|
+
return backup
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _backups(path, stamp=None):
|
|
125
|
+
"""(path, number) of the `.smart-tool-backup-` copies of path, only those of one stamp when given; the copy named
|
|
126
|
+
with the bare stamp is number 1."""
|
|
127
|
+
copies = []
|
|
128
|
+
for candidate in glob.glob(glob.escape(path + BACKUP_INFIX) + "*"):
|
|
129
|
+
found = _BACKUP_STAMP.match(candidate[len(path + BACKUP_INFIX):])
|
|
130
|
+
if found and stamp in (None, found.group(1)):
|
|
131
|
+
copies.append((candidate, (found.group(1), int(found.group(2) or 1))))
|
|
132
|
+
return copies
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _prune_backups(path, keep):
|
|
136
|
+
"""Deletes all but the `keep` newest `.smart-tool-backup-` copies of path (newest = latest stamp, then number)."""
|
|
137
|
+
copies = sorted(_backups(path), key=lambda copy: copy[1])
|
|
138
|
+
for candidate, _ in copies[:max(len(copies) - keep, 0)]:
|
|
139
|
+
os.remove(candidate)
|
package/browser_control.py
CHANGED
|
@@ -279,8 +279,9 @@ def set_claude_deny(present):
|
|
|
279
279
|
|
|
280
280
|
|
|
281
281
|
def set_codex_disabled():
|
|
282
|
-
"""Adds `disabled_tools` = DENIED_TOOLS
|
|
283
|
-
|
|
282
|
+
"""Adds `disabled_tools` = DENIED_TOOLS inside the [mcp_servers.playwright] table of Codex's config.toml, inserting
|
|
283
|
+
one line and leaving every other byte as it was, and checks the file still parses with that value. Raises
|
|
284
|
+
ValueError otherwise."""
|
|
284
285
|
path = _codex_config()
|
|
285
286
|
with open(path, encoding="utf-8", newline="") as stream:
|
|
286
287
|
text = stream.read()
|
package/client_hooks.py
CHANGED
|
@@ -7,6 +7,8 @@ import sys
|
|
|
7
7
|
import time
|
|
8
8
|
import tomllib
|
|
9
9
|
|
|
10
|
+
import atomic_io
|
|
11
|
+
import codex_config
|
|
10
12
|
import daemon_launcher
|
|
11
13
|
import endpoint_sync
|
|
12
14
|
|
|
@@ -282,24 +284,10 @@ def install(client_info, token, confirm):
|
|
|
282
284
|
"after_install": spec.get("after_install")}
|
|
283
285
|
|
|
284
286
|
|
|
285
|
-
def backup_file(path):
|
|
286
|
-
"""Copies path to a timestamped `.smart-tool-backup-` file beside it and returns that path, None when path does not
|
|
287
|
-
exist. A second backup in the same second gets a numbered name, so it never overwrites the copy of an earlier step."""
|
|
288
|
-
if not os.path.exists(path):
|
|
289
|
-
return None
|
|
290
|
-
stamp = f"{path}.smart-tool-backup-{time.strftime('%Y%m%d-%H%M%S')}"
|
|
291
|
-
backup, number = stamp, 1
|
|
292
|
-
while os.path.exists(backup):
|
|
293
|
-
number += 1
|
|
294
|
-
backup = f"{stamp}-{number}"
|
|
295
|
-
shutil.copy2(path, backup)
|
|
296
|
-
return backup
|
|
297
|
-
|
|
298
|
-
|
|
299
287
|
def _write_checked(path, data, verify, what):
|
|
300
288
|
"""Writes data as JSON to path through a temp file after a timestamped backup; when verify() fails on the written
|
|
301
289
|
file, restores the backup (or removes the new file) and raises. Returns the backup path (None for a new file)."""
|
|
302
|
-
backup = backup_file(path)
|
|
290
|
+
backup = atomic_io.backup_file(path)
|
|
303
291
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
304
292
|
temporary = path + ".smart-tool-tmp"
|
|
305
293
|
with open(temporary, "w", encoding="utf-8") as stream:
|
|
@@ -393,13 +381,13 @@ def _codex_states(path=None):
|
|
|
393
381
|
return states if isinstance(states, dict) else {}
|
|
394
382
|
|
|
395
383
|
|
|
396
|
-
def _codex_trusted(event, groups, is_ours):
|
|
384
|
+
def _codex_trusted(event, groups, is_ours, hooks_path=None, config_path=None):
|
|
397
385
|
"""Our handler under `event` is trusted in Codex's config.toml: the entry keyed by hooks.json path, event, group
|
|
398
386
|
and handler index holds the hash of the definition as it is now (a missing or different hash means Codex does not
|
|
399
|
-
run it). An unreadable config.toml raises ValueError."""
|
|
400
|
-
states = _codex_states()
|
|
387
|
+
run it). The paths default to Codex's own. An unreadable config.toml raises ValueError."""
|
|
388
|
+
states = _codex_states(config_path)
|
|
401
389
|
name = _codex_event(event)
|
|
402
|
-
path = CLIENTS["codex"]["files"][0]
|
|
390
|
+
path = hooks_path or CLIENTS["codex"]["files"][0]
|
|
403
391
|
for group_index, group in enumerate(groups):
|
|
404
392
|
if not _ours(group, is_ours):
|
|
405
393
|
continue
|
|
@@ -511,7 +499,7 @@ def record_codex_trust(wanted, path=None):
|
|
|
511
499
|
lines = text.splitlines(keepends=True)
|
|
512
500
|
for key, value in wanted.items():
|
|
513
501
|
lines = _with_trusted_hash(lines, key, value, newline)
|
|
514
|
-
backup = backup_file(path)
|
|
502
|
+
backup = atomic_io.backup_file(path, keep=codex_config.BACKUPS_KEPT)
|
|
515
503
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
516
504
|
temporary = path + ".smart-tool-tmp"
|
|
517
505
|
with open(temporary, "w", encoding="utf-8", newline="") as stream:
|
|
@@ -550,6 +538,27 @@ def audit_state(client="claude"):
|
|
|
550
538
|
return "current" if client != "codex" or _codex_trusted("SessionStart", groups, _audit_hook) else AUDIT_WAITING
|
|
551
539
|
|
|
552
540
|
|
|
541
|
+
def codex_unapproved(hooks_path, config_path):
|
|
542
|
+
"""Events whose hook of ours is written in the Codex hooks.json at hooks_path while the config.toml at config_path
|
|
543
|
+
holds no matching approval (`[hooks.state]` trusted_hash), so Codex does not run it. Reads both files, writes
|
|
544
|
+
nothing; [] when hooks.json is missing or holds none of ours. An unreadable config.toml raises ValueError."""
|
|
545
|
+
try:
|
|
546
|
+
with open(hooks_path, encoding="utf-8") as stream:
|
|
547
|
+
data = json.load(stream)
|
|
548
|
+
except FileNotFoundError:
|
|
549
|
+
return []
|
|
550
|
+
hooks = data.get("hooks") if isinstance(data, dict) else None
|
|
551
|
+
if not isinstance(hooks, dict):
|
|
552
|
+
return []
|
|
553
|
+
unapproved = []
|
|
554
|
+
for event, is_ours in [*((event, _our_hook) for event in _entries("codex")), ("SessionStart", _audit_hook)]:
|
|
555
|
+
groups = hooks.get(event)
|
|
556
|
+
if isinstance(groups, list) and any(_ours(group, is_ours) for group in groups) \
|
|
557
|
+
and not _codex_trusted(event, groups, is_ours, hooks_path, config_path):
|
|
558
|
+
unapproved.append(event)
|
|
559
|
+
return unapproved
|
|
560
|
+
|
|
561
|
+
|
|
553
562
|
def audit_hook_installed(client):
|
|
554
563
|
"""The session-start hook is on and running (Codex: trusted in config.toml), so a search need not repeat the redirect
|
|
555
564
|
proposals it already carries.
|
package/codex_config.py
CHANGED
|
@@ -11,6 +11,7 @@ import tomllib
|
|
|
11
11
|
import atomic_io
|
|
12
12
|
|
|
13
13
|
_BARE_KEY = re.compile(r"^[A-Za-z0-9_-]+$")
|
|
14
|
+
BACKUPS_KEPT = 10
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
def _header_path(line):
|
|
@@ -83,8 +84,9 @@ def _others(text, name):
|
|
|
83
84
|
|
|
84
85
|
|
|
85
86
|
def _write(path, before, after, name, fields):
|
|
86
|
-
"""Writes after (the edited text) when it parses, holds exactly `fields` for the server and changed nothing else
|
|
87
|
-
Raises ValueError otherwise, leaving the
|
|
87
|
+
"""Writes after (the edited text) when it parses, holds exactly `fields` for the server and changed nothing else,
|
|
88
|
+
after a numbered backup beside the file (the newest BACKUPS_KEPT stay). Raises ValueError otherwise, leaving the
|
|
89
|
+
file alone."""
|
|
88
90
|
try:
|
|
89
91
|
parsed = tomllib.loads(after)
|
|
90
92
|
unchanged = _others(after, name) == _others(before, name)
|
|
@@ -95,6 +97,7 @@ def _write(path, before, after, name, fields):
|
|
|
95
97
|
raise ValueError(f"Editing [mcp_servers.{name}] in {path} would change more than that table "
|
|
96
98
|
f"(a layout this editor does not handle); edit the file by hand.")
|
|
97
99
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
100
|
+
atomic_io.backup_file(path, keep=BACKUPS_KEPT)
|
|
98
101
|
atomic_io.write_text(path, after)
|
|
99
102
|
|
|
100
103
|
|
package/doc_insights.py
CHANGED
|
@@ -17,6 +17,8 @@ from collections import Counter, defaultdict
|
|
|
17
17
|
|
|
18
18
|
import affected_tests
|
|
19
19
|
import doc_scope
|
|
20
|
+
import index_views
|
|
21
|
+
import project_identity
|
|
20
22
|
|
|
21
23
|
MAX_COMMITS = 3000
|
|
22
24
|
MAX_ROWS = 25
|
|
@@ -31,6 +33,8 @@ SIMPLE_TERMS = 12
|
|
|
31
33
|
ADR_CANDIDATES = 25
|
|
32
34
|
ADR_DRAFTS = 5
|
|
33
35
|
_FIELD, _RECORD = "\x1f", "\x1e"
|
|
36
|
+
HISTORIES_KEPT = 8
|
|
37
|
+
_histories = {}
|
|
34
38
|
_MANIFEST = re.compile(r"(^|/)(package\.json|requirements[\w.-]*\.txt|go\.mod|pom\.xml|pyproject\.toml|build\.gradle(\.kts)?)$")
|
|
35
39
|
_DECISION = re.compile(r"\b(decid\w*|replac\w*|switch\w*|migrat\w*|adopt\w*|drop\w*|instead of|introduc\w*|because)\b", re.I)
|
|
36
40
|
_HTTP_VERB = re.compile(r"^do_[A-Z]+$")
|
|
@@ -44,9 +48,33 @@ _STOP = frozenset("""get set add is has are was the and for not with from into t
|
|
|
44
48
|
def history(root):
|
|
45
49
|
"""Commits of the project's git history, newest first, with the project-relative files each touched.
|
|
46
50
|
|
|
51
|
+
One `git log` of up to MAX_COMMITS commits is the costliest step of a documentation run, and a run reads the history
|
|
52
|
+
more than once (prose writing generates the scope before and after), so the answer is reused while the Git view's
|
|
53
|
+
stamp (HEAD, index, reflog, packed-refs, branch ref) has not moved; checking it is a stat, no process.
|
|
54
|
+
|
|
47
55
|
Returns: (commits, note): commits is a list of {hash, time, subject, body, files}; note is None, or the reason no
|
|
48
|
-
history could be read (not a git repository, git missing), which the page shows in place of the table.
|
|
56
|
+
history could be read (not a git repository, git missing), which the page shows in place of the table. The
|
|
57
|
+
returned lists are shared with later calls and must not be changed.
|
|
49
58
|
"""
|
|
59
|
+
try:
|
|
60
|
+
view = index_views.describe(root, fresh=True)
|
|
61
|
+
except (RuntimeError, ValueError, OSError):
|
|
62
|
+
view = None
|
|
63
|
+
if not view or not view.get("git"):
|
|
64
|
+
return _read_history(root)
|
|
65
|
+
key = project_identity.canonical_root(root)
|
|
66
|
+
saved = _histories.get(key)
|
|
67
|
+
if saved and saved[0] == view["stamp"]:
|
|
68
|
+
return saved[1]
|
|
69
|
+
result = _read_history(root)
|
|
70
|
+
if not index_views.stamp_changed(view):
|
|
71
|
+
if len(_histories) >= HISTORIES_KEPT:
|
|
72
|
+
_histories.clear()
|
|
73
|
+
_histories[key] = (view["stamp"], result)
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _read_history(root):
|
|
50
78
|
try:
|
|
51
79
|
raw = affected_tests._git(root, "-c", "core.quotepath=false", "log", "--no-merges", f"-n{MAX_COMMITS}", "--relative",
|
|
52
80
|
"--name-status", "-M", f"--pretty=format:{_RECORD}%h{_FIELD}%ct{_FIELD}%s{_FIELD}%b{_FIELD}", "--", ".")
|
package/hook_decision.py
CHANGED
|
@@ -7,10 +7,12 @@ Uma busca nativa que a regra redireciona, e WebFetch/WebSearch, não são negada
|
|
|
7
7
|
texto volta como saída da própria chamada (`tool_redirect`). INVARIANTE: a saída do PreToolUse é `{}`, `updatedInput`
|
|
8
8
|
sem `permissionDecision` (as regras de permissão do usuário julgam a chamada reescrita), `allow` só junto do comando
|
|
9
9
|
que o próprio hook escreveu para imprimir a resposta no Codex (`tool_redirect.is_codex_command`), ou `deny` quando a
|
|
10
|
-
|
|
11
|
-
`systemMessage`, que
|
|
10
|
+
edição que a regra de documentação ou de comentário exige refazer (`_edit_review`); uma busca sem `tool_use_id` ou sem
|
|
11
|
+
consulta legível nunca é negada: é respondida no contexto (`_answer_in_place`). Diagnóstico vai em `systemMessage`, que
|
|
12
|
+
não altera permissão.
|
|
12
13
|
"""
|
|
13
14
|
import os
|
|
15
|
+
import uuid
|
|
14
16
|
|
|
15
17
|
import affected_tests
|
|
16
18
|
import code_graph
|
|
@@ -372,7 +374,50 @@ def decide(payload, client, web_route, jobs):
|
|
|
372
374
|
return _redirect(payload, client, plan, jobs)
|
|
373
375
|
if not agent_has_smart_tool(payload.get("agent_type") if payload.get("agent_id") else None, payload.get("cwd")):
|
|
374
376
|
return {}
|
|
375
|
-
return
|
|
377
|
+
return _answer_in_place(payload, client, plan, jobs)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _start_job(payload, client, plan, jobs, key):
|
|
381
|
+
"""Starts the Smart Tool call of a redirected one under key (web calls carry the requester of the hook)."""
|
|
382
|
+
who = requester.CURRENT.set(requester.from_hook(client, payload)) if payload["tool_name"] in WEB_TOOLS else None
|
|
383
|
+
try:
|
|
384
|
+
jobs.start(key, plan["kind"], plan["arguments"])
|
|
385
|
+
finally:
|
|
386
|
+
if who is not None:
|
|
387
|
+
requester.CURRENT.reset(who)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def _codex_print(payload, client, jobs, key, text):
|
|
391
|
+
"""Codex shell call rewritten to print the answer; `allow` is what Codex needs to apply the rewrite."""
|
|
392
|
+
tool_input = payload.get("tool_input") if isinstance(payload.get("tool_input"), dict) else {}
|
|
393
|
+
jobs.forget(key)
|
|
394
|
+
_log_served(client, payload)
|
|
395
|
+
return {"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "allow",
|
|
396
|
+
"permissionDecisionReason": "Smart Tool answered this search",
|
|
397
|
+
"updatedInput": {**tool_input, "command": tool_redirect.codex_command(text)}}}
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _answer_in_place(payload, client, plan, jobs):
|
|
401
|
+
"""A redirected call that cannot be swapped by `tool_use_id` (no id, or no query to run) is still not denied: the
|
|
402
|
+
Smart Tool answer goes into the context of the call, which runs as written, and a codex shell search prints it.
|
|
403
|
+
Without an answer in time, or without a query, the context names the exact Smart Tool call to make."""
|
|
404
|
+
if not agent_has_smart_tool(payload.get("agent_type") if payload.get("agent_id") else None, payload.get("cwd")):
|
|
405
|
+
return {}
|
|
406
|
+
if not plan["kind"]:
|
|
407
|
+
return advise(plan["message"])
|
|
408
|
+
key = f"noid-{uuid.uuid4().hex}"
|
|
409
|
+
_start_job(payload, client, plan, jobs, key)
|
|
410
|
+
text = _answer(jobs, key, PRE_WAIT_S, payload["tool_name"])
|
|
411
|
+
if text is None:
|
|
412
|
+
jobs.forget(key)
|
|
413
|
+
return advise(plan["message"])
|
|
414
|
+
if client == "codex" and payload["tool_name"] in tool_redirect.NO_OP:
|
|
415
|
+
return _codex_print(payload, client, jobs, key, text)
|
|
416
|
+
jobs.forget(key)
|
|
417
|
+
_log_served(client, payload)
|
|
418
|
+
return {"hookSpecificOutput": {"hookEventName": "PreToolUse",
|
|
419
|
+
"additionalContext": f"Smart Tool answered this {payload['tool_name']} call with "
|
|
420
|
+
f"smart_search (the call was not blocked):\n{text}"}}
|
|
376
421
|
|
|
377
422
|
|
|
378
423
|
def _redirect(payload, client, plan, jobs):
|
|
@@ -382,12 +427,7 @@ def _redirect(payload, client, plan, jobs):
|
|
|
382
427
|
else runs as the agent wrote it and, in Claude Code, `after` swaps its output for the answer."""
|
|
383
428
|
key, tool_name = payload["tool_use_id"], payload["tool_name"]
|
|
384
429
|
tool_input = payload.get("tool_input") if isinstance(payload.get("tool_input"), dict) else {}
|
|
385
|
-
|
|
386
|
-
try:
|
|
387
|
-
jobs.start(key, plan["kind"], plan["arguments"])
|
|
388
|
-
finally:
|
|
389
|
-
if who is not None:
|
|
390
|
-
requester.CURRENT.reset(who)
|
|
430
|
+
_start_job(payload, client, plan, jobs, key)
|
|
391
431
|
if tool_name not in tool_redirect.NO_OP:
|
|
392
432
|
return {}
|
|
393
433
|
text = _answer(jobs, key, PRE_WAIT_S, tool_name)
|
|
@@ -396,11 +436,7 @@ def _redirect(payload, client, plan, jobs):
|
|
|
396
436
|
if client != "codex":
|
|
397
437
|
return {"hookSpecificOutput": {"hookEventName": "PreToolUse",
|
|
398
438
|
"updatedInput": {**tool_input, "command": tool_redirect.NO_OP[tool_name]}}}
|
|
399
|
-
jobs
|
|
400
|
-
_log_served(client, payload)
|
|
401
|
-
return {"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "allow",
|
|
402
|
-
"permissionDecisionReason": "Smart Tool answered this search",
|
|
403
|
-
"updatedInput": {**tool_input, "command": tool_redirect.codex_command(text)}}}
|
|
439
|
+
return _codex_print(payload, client, jobs, key, text)
|
|
404
440
|
|
|
405
441
|
|
|
406
442
|
def _answer(jobs, key, timeout, tool_name):
|
package/install.py
CHANGED
|
@@ -13,6 +13,7 @@ import urllib.request
|
|
|
13
13
|
import venv
|
|
14
14
|
from pathlib import Path
|
|
15
15
|
|
|
16
|
+
import atomic_io
|
|
16
17
|
import client_hooks
|
|
17
18
|
import paths
|
|
18
19
|
import version
|
|
@@ -199,7 +200,7 @@ def remove_other_installs(target):
|
|
|
199
200
|
text = _repointed(original, folder, target)
|
|
200
201
|
if text != original:
|
|
201
202
|
trust = client_hooks.repoint_trust(str(path), original, text) if path == codex else {}
|
|
202
|
-
|
|
203
|
+
atomic_io.backup_file(str(path))
|
|
203
204
|
path.write_text(text, encoding="utf-8")
|
|
204
205
|
client_hooks.record_codex_trust(trust, str(path.with_name("config.toml")))
|
|
205
206
|
print(f"Hooks in {path} now point to {target}.")
|
package/locales/pt-BR.json
CHANGED
|
@@ -954,6 +954,10 @@
|
|
|
954
954
|
"In {0}, remove `|| true`, `exit 0` and `2>/dev/null` from the command of this {1} hook so its failures show.",
|
|
955
955
|
"Register MCP server '{0}' for this agent, or delete this {1} hook from {2}.",
|
|
956
956
|
", or list the duplicated tools in disabled_tools of one of them.",
|
|
957
|
-
"Start the service that listens at {0}, or delete this {1} hook from {2}."
|
|
957
|
+
"Start the service that listens at {0}, or delete this {1} hook from {2}.",
|
|
958
|
+
"Smart Tool's {0} hook is written in hooks.json, but {1} holds no matching approval ([hooks.state] trusted_hash): Codex does not run the hook. The approval is lost when something rewrites config.toml or when the hook changes.",
|
|
959
|
+
"Open /hooks in Codex and approve the Smart Tool entry, or turn the Smart Tool hook off and on again in the setup screen, which records the approval.",
|
|
960
|
+
"{0} holds a block another tool keeps between `# >>> {1} >>>` markers. Smart Tool edits only its own tables there and leaves the block as it is.",
|
|
961
|
+
"Nothing to do. If the markers or the block disappear, the tool that manages '{0}' rewrote {1}; restore it from the newest `{2}.smart-tool-backup-*` copy or run that tool's own repair."
|
|
958
962
|
]
|
|
959
963
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allansantos-dev/smart-tool",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.69",
|
|
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
|
-
"author": "
|
|
6
|
+
"author": "Smart Tool contributors",
|
|
7
7
|
"homepage": "https://allansantos-dv.github.io/smart-tool/",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
package/project_identity.py
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
"""Identidade única de raízes e contenção de caminhos, inclusive junctions Windows."""
|
|
2
2
|
import hashlib
|
|
3
3
|
import os
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
RESOLVED_TTL_S = 3
|
|
7
|
+
RESOLVED_MAX = 4096
|
|
8
|
+
_resolved = {}
|
|
4
9
|
|
|
5
10
|
|
|
6
11
|
def display_root(root):
|
|
12
|
+
"""Absolute path of `root` with symlinks and junctions resolved. Resolving costs one filesystem call per path
|
|
13
|
+
component on Windows, and the daemon asks for the same few paths thousands of times in a row (per file of an
|
|
14
|
+
index pass, per project of every monitor tick), so an answer is reused for RESOLVED_TTL_S seconds: a junction
|
|
15
|
+
retargeted inside that window is seen after it."""
|
|
7
16
|
if not isinstance(root, (str, os.PathLike)) or not str(root).strip():
|
|
8
17
|
raise ValueError("Provide the project folder.")
|
|
9
|
-
|
|
18
|
+
absolute = os.path.abspath(os.path.expanduser(os.fspath(root)))
|
|
19
|
+
now = time.monotonic()
|
|
20
|
+
hit = _resolved.get(absolute)
|
|
21
|
+
if hit and now - hit[0] < RESOLVED_TTL_S:
|
|
22
|
+
return hit[1]
|
|
23
|
+
real = os.path.realpath(absolute)
|
|
24
|
+
if len(_resolved) >= RESOLVED_MAX:
|
|
25
|
+
_resolved.clear()
|
|
26
|
+
_resolved[absolute] = (now, real)
|
|
27
|
+
return real
|
|
10
28
|
|
|
11
29
|
|
|
12
30
|
def canonical_root(root):
|
package/setup_audit.py
CHANGED
|
@@ -30,6 +30,7 @@ import urllib.request
|
|
|
30
30
|
import yaml
|
|
31
31
|
|
|
32
32
|
import atomic_io
|
|
33
|
+
import client_hooks
|
|
33
34
|
import endpoint_sync
|
|
34
35
|
import paths
|
|
35
36
|
|
|
@@ -345,7 +346,9 @@ def _collect_codex_plugins(config, snapshot):
|
|
|
345
346
|
|
|
346
347
|
def collect_codex(project, snapshot):
|
|
347
348
|
"""Codex's effective set for project: config.toml (MCP, hooks), hooks.json, skills, AGENTS.md chain."""
|
|
348
|
-
|
|
349
|
+
config_path = os.path.join(codex_dir(), "config.toml")
|
|
350
|
+
config = snapshot.toml(config_path) or {}
|
|
351
|
+
config_text = snapshot.read(config_path)
|
|
349
352
|
mcp = _mcp_entries(config.get("mcp_servers"), "user", os.path.join(codex_dir(), "config.toml"))
|
|
350
353
|
hooks = _hooks_from(snapshot.json(os.path.join(codex_dir(), "hooks.json")), "hooks.json",
|
|
351
354
|
os.path.join(codex_dir(), "hooks.json"))
|
|
@@ -375,7 +378,13 @@ def collect_codex(project, snapshot):
|
|
|
375
378
|
instructions.append({"path": os.path.join(folder, name), "kind": "project",
|
|
376
379
|
"bytes": len(text.encode("utf-8")), "_text": text})
|
|
377
380
|
break
|
|
378
|
-
|
|
381
|
+
try:
|
|
382
|
+
unapproved = client_hooks.codex_unapproved(os.path.join(codex_dir(), "hooks.json"), config_path)
|
|
383
|
+
except ValueError:
|
|
384
|
+
unapproved = []
|
|
385
|
+
return {"plugins": plugins, "hooks": hooks, "mcp": mcp, "skills": skills, "instructions": instructions,
|
|
386
|
+
"configs": [{"path": config_path, "_text": config_text}] if config_text is not None else [],
|
|
387
|
+
"unapproved": unapproved}
|
|
379
388
|
|
|
380
389
|
|
|
381
390
|
def _finding(check, severity, harness, subject, detail, source, action, evidence=()):
|
|
@@ -589,6 +598,37 @@ def check_instructions(project, claude, codex):
|
|
|
589
598
|
return findings
|
|
590
599
|
|
|
591
600
|
|
|
601
|
+
_MARKED_BLOCK_RE = re.compile(r"^[ \t]*#[ \t]*>>>[ \t]*(.+?)[ \t]*>>>[ \t]*$", re.MULTILINE)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def check_codex_config(codex):
|
|
605
|
+
"""Blocks other tools keep between marker comments in Codex's config.toml (listed, nothing to fix) and Smart Tool
|
|
606
|
+
hooks written in hooks.json that config.toml holds no approval for (`[hooks.state]` trusted_hash), which Codex then
|
|
607
|
+
does not run."""
|
|
608
|
+
findings = []
|
|
609
|
+
for config in codex["configs"]:
|
|
610
|
+
for name in dict.fromkeys(_MARKED_BLOCK_RE.findall(config["_text"])):
|
|
611
|
+
findings.append(_finding("codex_config_foreign_block", "info", "codex", name,
|
|
612
|
+
f"{config['path']} holds a block another tool keeps between `# >>> {name} >>>` "
|
|
613
|
+
"markers. Smart Tool edits only its own tables there and leaves the block as it is.",
|
|
614
|
+
DOCS["codex_hooks"],
|
|
615
|
+
f"Nothing to do. If the markers or the block disappear, the tool that manages "
|
|
616
|
+
f"'{name}' rewrote {config['path']}; restore it from the newest "
|
|
617
|
+
f"`{config['path']}.smart-tool-backup-*` copy or run that tool's own repair.",
|
|
618
|
+
[config["path"]]))
|
|
619
|
+
for event in codex["unapproved"]:
|
|
620
|
+
path = os.path.join(codex_dir(), "config.toml")
|
|
621
|
+
findings.append(_finding("codex_hook_unapproved", "warn", "codex", event,
|
|
622
|
+
f"Smart Tool's {event} hook is written in hooks.json, but {path} holds no matching "
|
|
623
|
+
"approval ([hooks.state] trusted_hash): Codex does not run the hook. The approval is "
|
|
624
|
+
"lost when something rewrites config.toml or when the hook changes.",
|
|
625
|
+
DOCS["codex_hooks"],
|
|
626
|
+
"Open /hooks in Codex and approve the Smart Tool entry, or turn the Smart Tool hook off "
|
|
627
|
+
"and on again in the setup screen, which records the approval.",
|
|
628
|
+
[path]))
|
|
629
|
+
return findings
|
|
630
|
+
|
|
631
|
+
|
|
592
632
|
def _rpc_stdio(entry, cwd, timeout):
|
|
593
633
|
"""initialize + tools/list over stdio; {"tools": [...]} or {"error", "kind": missing|down|timeout}."""
|
|
594
634
|
command = entry["command"]
|
|
@@ -810,6 +850,7 @@ def run(project, probe=True, save=True, probe_max_age=0, trigger="manual"):
|
|
|
810
850
|
findings += check_mcp(harness, data["mcp"], results)
|
|
811
851
|
findings += check_agents(claude)
|
|
812
852
|
findings += check_instructions(project, claude, codex)
|
|
853
|
+
findings += check_codex_config(codex)
|
|
813
854
|
unverified = list(snapshot.unverified)
|
|
814
855
|
if not probe:
|
|
815
856
|
unverified.append("MCP servers and HTTP hook endpoints were not contacted (probe off)")
|
package/smart_tool_daemon.py
CHANGED
|
@@ -450,6 +450,8 @@ TOOLS = [
|
|
|
450
450
|
"test": {"type": "integer", "minimum": 1, "maximum": 20},
|
|
451
451
|
"doc": {"type": "integer", "minimum": 1, "maximum": 20}}}],
|
|
452
452
|
"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"},
|
|
453
|
+
"detail": {"type": "string", "enum": ["compact", "full"], "default": "compact",
|
|
454
|
+
"description": "compact: 240-character excerpts, and ranking (when rerank) and location_kind (when source_line) left out of each result; full: 500-character excerpts with every field"},
|
|
453
455
|
"mode": {"type": "string", "enum": ["search", "dossier"], "default": "search",
|
|
454
456
|
"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"},
|
|
455
457
|
"timeout_ms": {"type": "integer", "minimum": 1, "maximum": 5000, "default": 800,
|
|
@@ -1321,6 +1323,7 @@ def _perform_smart_search_isolated(arguments, job_id):
|
|
|
1321
1323
|
elif symbol_note or (symbol_index or {}).get("note"):
|
|
1322
1324
|
warnings.append(symbol_note or symbol_index["note"])
|
|
1323
1325
|
query_terms = _symbol_terms(" ".join(queries[group] for group in groups if group != "doc"))
|
|
1326
|
+
detail = arguments.get("detail", "compact")
|
|
1324
1327
|
_update_job(job_id, phase="searching")
|
|
1325
1328
|
found = {group: indexer.search(root, vectors.get(queries[group]), top_k=max(limits[group] * 4, candidate_pool),
|
|
1326
1329
|
query=queries[group], model_id=search_model, overlay=overlay,
|
|
@@ -1348,10 +1351,9 @@ def _perform_smart_search_isolated(arguments, job_id):
|
|
|
1348
1351
|
pinned = [(c[0], "overlay", c[1:]) for c in candidates if tuple(c[1:4]) in exact][:2]
|
|
1349
1352
|
pinned_keys = {tuple(item[2][:3]) for item in pinned}
|
|
1350
1353
|
ordered = pinned + [item for item in ordered if tuple(item[2][:3]) not in pinned_keys]
|
|
1351
|
-
rows =[
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
if symbol_index and group != "doc" else {})}
|
|
1354
|
+
rows = [_result_row(detail, path, start, end, score, ranking, text,
|
|
1355
|
+
_chunk_symbols(symbol_index, path, start, end, text, query_terms)
|
|
1356
|
+
if symbol_index and group != "doc" else None)
|
|
1355
1357
|
for score, ranking, (path, start, end, text) in ordered[:top_k]]
|
|
1356
1358
|
return rows, problem
|
|
1357
1359
|
|
|
@@ -1508,6 +1510,25 @@ def _submit_project_job_locked(kind, arguments):
|
|
|
1508
1510
|
|
|
1509
1511
|
SEARCH_LIMIT_TOTAL = 20
|
|
1510
1512
|
SEARCH_LIMIT_DEFAULT = 5
|
|
1513
|
+
SEARCH_DETAILS = ("compact", "full")
|
|
1514
|
+
SNIPPET_CHARS = {"compact": 240, "full": 500}
|
|
1515
|
+
|
|
1516
|
+
|
|
1517
|
+
def _result_row(detail, path, start, end, score, ranking, text, symbols):
|
|
1518
|
+
"""One smart_search result. `full` carries every field; `compact` shortens the excerpt to whole lines and leaves out
|
|
1519
|
+
what is the usual value (ranking `rerank`, location_kind `source_line`) and an empty symbol list."""
|
|
1520
|
+
location_kind = document_text.location_kind(path)
|
|
1521
|
+
snippet = text[:SNIPPET_CHARS[detail]]
|
|
1522
|
+
if detail == "compact" and len(text) > len(snippet) and "\n" in snippet.lstrip("\r\n"):
|
|
1523
|
+
snippet = snippet[:snippet.rindex("\n")].rstrip("\r")
|
|
1524
|
+
row = {"path": path, "lines": f"{start}-{end}", "score": round(score, 6), "snippet": snippet}
|
|
1525
|
+
if detail == "full" or ranking != "rerank":
|
|
1526
|
+
row["ranking"] = ranking
|
|
1527
|
+
if detail == "full" or location_kind != "source_line":
|
|
1528
|
+
row["location_kind"] = location_kind
|
|
1529
|
+
if symbols is not None and (detail == "full" or symbols):
|
|
1530
|
+
row["symbols"] = symbols
|
|
1531
|
+
return row
|
|
1511
1532
|
|
|
1512
1533
|
|
|
1513
1534
|
def _limit_value(raw, field_name):
|
|
@@ -1588,7 +1609,7 @@ def _optional_text(arguments, name):
|
|
|
1588
1609
|
|
|
1589
1610
|
|
|
1590
1611
|
_SMART_SEARCH_FIELDS = {"project_root", "query_identifiers", "query_comments", "doc_query",
|
|
1591
|
-
"single_language_reason", "group", "top_k"}
|
|
1612
|
+
"single_language_reason", "group", "top_k", "detail"}
|
|
1592
1613
|
_DOSSIER_FIELDS = {"project_root", "query_identifiers", "query_comments", "doc_query", "mode", "timeout_ms", "max_hits"}
|
|
1593
1614
|
|
|
1594
1615
|
|
|
@@ -1792,8 +1813,11 @@ def _smart_search_arguments(arguments):
|
|
|
1792
1813
|
"To search in a single language, explain why in single_language_reason.")
|
|
1793
1814
|
project = project_store.get(project_identity.project_id(project_root)) or {}
|
|
1794
1815
|
limits = _search_limits(arguments.get("top_k"), groups, project.get("search_limits"))
|
|
1816
|
+
detail = arguments.get("detail", "compact")
|
|
1817
|
+
if detail not in SEARCH_DETAILS:
|
|
1818
|
+
raise ValueError("detail must be compact or full.")
|
|
1795
1819
|
query = " ".join(part for part in (identifiers, comments) if part) or doc_query
|
|
1796
|
-
arguments = {"project_root": project_root, "query": query, "top_k": limits, "group": group,
|
|
1820
|
+
arguments = {"project_root": project_root, "query": query, "top_k": limits, "group": group, "detail": detail,
|
|
1797
1821
|
"doc_query": doc_query or comments or identifiers,
|
|
1798
1822
|
**({"single_language_reason": reason} if reason else {})}
|
|
1799
1823
|
session, client = _session_client()
|
|
@@ -1944,7 +1968,7 @@ def _scout_search(root, query, question, max_files):
|
|
|
1944
1968
|
path runs at once, outside the per-folder queue, as for a redirected search."""
|
|
1945
1969
|
limits = {"code": min(SEARCH_LIMIT_TOTAL - 6, max_files * 2), "test": 3, "doc": 3}
|
|
1946
1970
|
search = {"project_root": root, "query": query, "doc_query": question, "group": "all", "top_k": limits,
|
|
1947
|
-
"_structured": True}
|
|
1971
|
+
"detail": "full", "_structured": True}
|
|
1948
1972
|
if _cold_index_building(root):
|
|
1949
1973
|
return _perform_smart_search(search, "")
|
|
1950
1974
|
job_id, future = _submit_project_job("smart_search", search)
|
package/version.py
CHANGED