@allansantos-dev/smart-tool 0.9.20 → 0.9.21
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 +16 -0
- package/README.md +1 -1
- package/client_hooks.py +1 -1
- package/code_graph.py +4 -2
- package/doc_check.py +7 -3
- package/hooks/pretool_router.py +1 -1
- package/package.json +1 -1
- package/smart_tool_daemon.py +58 -1
- package/version.py +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.9.21 - beta
|
|
4
|
+
|
|
5
|
+
- The PreToolUse hook can no longer hold a tool call for long. Its decision has 4 s; past that it answers without one
|
|
6
|
+
(the tool runs) and the daemon log gets one line, `PreToolUse hook over 4 s for Write <file>: answered without a
|
|
7
|
+
decision`; an answer slower than 1 s is logged too. Before, Write and Edit reviews (docstring reminder, copied
|
|
8
|
+
function check) could take longer than the client's 15 s timeout on large TypeScript or Java files and large
|
|
9
|
+
repositories (75 cancelled hook calls in 9 days, 62 of them Write or Edit) and Bash calls of other sessions waited
|
|
10
|
+
behind them.
|
|
11
|
+
- Edit and Write reviews run on their own two workers: a third simultaneous edit goes through unreviewed (logged) instead
|
|
12
|
+
of queueing, and Bash, Read and Grep never wait for them.
|
|
13
|
+
- The Node code analyzer called by the edit hook stops after 10 s, and an analysis that failed or timed out is no
|
|
14
|
+
longer remembered as "this file has no functions".
|
|
15
|
+
- The timeout written in the client hook entry drops from 15 s to 6 s (Codex's thin command 14 s to 5 s). An entry
|
|
16
|
+
written by an older version shows as outdated on the setup screen until you accept the hook update (preview and
|
|
17
|
+
backup first).
|
|
18
|
+
|
|
3
19
|
## 0.9.20 - beta
|
|
4
20
|
|
|
5
21
|
- The first search of a folder that has no index yet no longer waits for the index. It answers at once with the files
|
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.
|
|
18
|
+
**Status:** 0.9.21 beta. Windows only. What changed in each version: [CHANGELOG.md](CHANGELOG.md).
|
|
19
19
|
|
|
20
20
|
## How it works
|
|
21
21
|
|
package/client_hooks.py
CHANGED
package/code_graph.py
CHANGED
|
@@ -233,14 +233,16 @@ def _python_graph(sources):
|
|
|
233
233
|
|
|
234
234
|
|
|
235
235
|
NODE_HEAP_MB = (1024, 4096)
|
|
236
|
+
ANALYZER_TIMEOUT_S = contextvars.ContextVar("analyzer_timeout_s", default=120)
|
|
236
237
|
|
|
237
238
|
|
|
238
239
|
def _run_analyzer(script, payload, failure):
|
|
239
240
|
"""Runs a Node analyzer, retrying once with a larger heap when it runs out of memory (634 TypeScript files, 2.9 MB,
|
|
240
|
-
needed more than the former fixed 512 MB).
|
|
241
|
+
needed more than the former fixed 512 MB). The run is killed after `ANALYZER_TIMEOUT_S` seconds (a context
|
|
242
|
+
variable: the edit hook lowers it so a slow file cannot hold its worker)."""
|
|
241
243
|
for heap in NODE_HEAP_MB:
|
|
242
244
|
process=subprocess.run([web_search_adapters.NODE_EXECUTABLE,f'--max-old-space-size={heap}',str(Path(__file__).with_name(script))],
|
|
243
|
-
input=payload,capture_output=True,text=True,encoding='utf-8',timeout=
|
|
245
|
+
input=payload,capture_output=True,text=True,encoding='utf-8',timeout=ANALYZER_TIMEOUT_S.get(),
|
|
244
246
|
creationflags=getattr(subprocess,'CREATE_NO_WINDOW',0))
|
|
245
247
|
if process.returncode==0:
|
|
246
248
|
return json.loads(process.stdout)
|
package/doc_check.py
CHANGED
|
@@ -166,11 +166,13 @@ def functions(path, source, symbols=None):
|
|
|
166
166
|
|
|
167
167
|
def analyze(path, *sources):
|
|
168
168
|
"""Analyzer symbols of several versions of one TypeScript/JavaScript/Java file, from one analyzer run for the
|
|
169
|
-
versions not seen yet (an edit hook needs the file before and after; the next edit's 'before' is this 'after').
|
|
169
|
+
versions not seen yet (an edit hook needs the file before and after; the next edit's 'before' is this 'after').
|
|
170
|
+
An analysis that failed or timed out is returned empty but not cached, so the next call tries again."""
|
|
170
171
|
lower, name = path.lower(), os.path.basename(path)
|
|
171
172
|
keys = [hashlib.sha1(f"{name}\0{source}".encode("utf-8")).hexdigest() for source in sources]
|
|
172
173
|
with _PARSED_LOCK:
|
|
173
174
|
missing = {key: source for key, source in zip(keys, sources) if key not in _PARSED}
|
|
175
|
+
found = {}
|
|
174
176
|
if missing:
|
|
175
177
|
batch = {f"v{i}/{name}": source for i, source in enumerate(missing.values())}
|
|
176
178
|
analysis = (code_graph._javascript_graph(batch) if lower.endswith(SCRIPT)
|
|
@@ -178,11 +180,13 @@ def analyze(path, *sources):
|
|
|
178
180
|
found = {key: [dict(s, path=name) for s in analysis.get("symbols") or [] if s.get("path") == f"v{i}/{name}"]
|
|
179
181
|
for i, key in enumerate(missing)}
|
|
180
182
|
with _PARSED_LOCK:
|
|
181
|
-
|
|
183
|
+
if not analysis.get("retryable"):
|
|
184
|
+
_PARSED.update(found)
|
|
182
185
|
while len(_PARSED) > MAX_PARSED:
|
|
183
186
|
_PARSED.popitem(last=False)
|
|
184
187
|
with _PARSED_LOCK:
|
|
185
|
-
|
|
188
|
+
cached = {key: _PARSED[key] for key in keys if key in _PARSED}
|
|
189
|
+
return [cached.get(key, found.get(key, [])) for key in keys]
|
|
186
190
|
|
|
187
191
|
|
|
188
192
|
def _touched_lines(before, after):
|
package/hooks/pretool_router.py
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allansantos-dev/smart-tool",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.21",
|
|
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/smart_tool_daemon.py
CHANGED
|
@@ -70,6 +70,7 @@ import web_fetch
|
|
|
70
70
|
import version
|
|
71
71
|
import update_check
|
|
72
72
|
import gateway
|
|
73
|
+
import code_graph
|
|
73
74
|
import hook_decision
|
|
74
75
|
import endpoint_sync
|
|
75
76
|
|
|
@@ -3574,6 +3575,60 @@ def _web_route(tool, url):
|
|
|
3574
3575
|
|
|
3575
3576
|
|
|
3576
3577
|
HOOK_MAX_BODY_BYTES = 4 * 1024 * 1024
|
|
3578
|
+
# The client cancels a PreToolUse hook at its own timeout and lets the tool run, but the agent has waited all of it
|
|
3579
|
+
# (75 cancellations of 15 s in 9 days, 62 of them Write/Edit). The decision gets this much; past it the hook answers
|
|
3580
|
+
# without one and says so in the log. Edit reviews (docs, copies) are the slow ones, so they have their own small
|
|
3581
|
+
# pool: at most HOOK_EDIT_SLOTS run at once and a further edit is let through unreviewed instead of queueing.
|
|
3582
|
+
HOOK_BUDGET_S = 4.0
|
|
3583
|
+
HOOK_ANALYZER_TIMEOUT_S = 10
|
|
3584
|
+
HOOK_SLOW_LOG_S = 1.0
|
|
3585
|
+
HOOK_EDIT_SLOTS = 2
|
|
3586
|
+
_HOOK_POOL = concurrent.futures.ThreadPoolExecutor(4, thread_name_prefix="hook")
|
|
3587
|
+
_HOOK_EDIT_POOL = concurrent.futures.ThreadPoolExecutor(HOOK_EDIT_SLOTS, thread_name_prefix="hook-edit")
|
|
3588
|
+
_HOOK_EDIT_SLOTS = threading.BoundedSemaphore(HOOK_EDIT_SLOTS)
|
|
3589
|
+
|
|
3590
|
+
|
|
3591
|
+
def _hook_work(payload, client, slot):
|
|
3592
|
+
"""`hook_decision.decide` on a hook worker, with the analyzer timeout lowered and the edit slot released at the end
|
|
3593
|
+
(the worker may outlive the request that gave up on it)."""
|
|
3594
|
+
code_graph.ANALYZER_TIMEOUT_S.set(HOOK_ANALYZER_TIMEOUT_S)
|
|
3595
|
+
try:
|
|
3596
|
+
return hook_decision.decide(payload, client, _web_route)
|
|
3597
|
+
finally:
|
|
3598
|
+
if slot:
|
|
3599
|
+
slot.release()
|
|
3600
|
+
|
|
3601
|
+
|
|
3602
|
+
def _decide_within_budget(payload, client):
|
|
3603
|
+
"""The hook decision of `payload`, or None when it did not come within HOOK_BUDGET_S or no edit slot was free (the
|
|
3604
|
+
tool then runs as if the hook had nothing to say). Both cases, and any answer slower than HOOK_SLOW_LOG_S, leave one
|
|
3605
|
+
line in the daemon log with the tool, the file or command and the time."""
|
|
3606
|
+
tool = payload.get("tool_name") if isinstance(payload, dict) else None
|
|
3607
|
+
tool_input = payload.get("tool_input") if isinstance(payload, dict) else None
|
|
3608
|
+
target = str((tool_input or {}).get("file_path") or (tool_input or {}).get("command") or "")[:80]
|
|
3609
|
+
edit = tool in hook_decision.EDIT_TOOLS
|
|
3610
|
+
if edit and not _HOOK_EDIT_SLOTS.acquire(blocking=False):
|
|
3611
|
+
_log(f"PreToolUse hook skipped the review of {tool} {target}: {HOOK_EDIT_SLOTS} edit reviews already running, "
|
|
3612
|
+
"the edit goes through without one")
|
|
3613
|
+
return None
|
|
3614
|
+
started = time.monotonic()
|
|
3615
|
+
try:
|
|
3616
|
+
future = (_HOOK_EDIT_POOL if edit else _HOOK_POOL).submit(contextvars.copy_context().run, _hook_work, payload,
|
|
3617
|
+
client, _HOOK_EDIT_SLOTS if edit else None)
|
|
3618
|
+
except BaseException:
|
|
3619
|
+
if edit:
|
|
3620
|
+
_HOOK_EDIT_SLOTS.release()
|
|
3621
|
+
raise
|
|
3622
|
+
try:
|
|
3623
|
+
output = future.result(timeout=HOOK_BUDGET_S)
|
|
3624
|
+
except concurrent.futures.TimeoutError:
|
|
3625
|
+
_log(f"PreToolUse hook over {HOOK_BUDGET_S:g} s for {tool} {target}: answered without a decision, the work "
|
|
3626
|
+
"finishes in the background")
|
|
3627
|
+
return None
|
|
3628
|
+
took = time.monotonic() - started
|
|
3629
|
+
if took > HOOK_SLOW_LOG_S:
|
|
3630
|
+
_log(f"PreToolUse hook slow: {tool} {target} took {took:.1f} s")
|
|
3631
|
+
return output
|
|
3577
3632
|
|
|
3578
3633
|
|
|
3579
3634
|
def _pretool_hook(handler):
|
|
@@ -3587,10 +3642,12 @@ def _pretool_hook(handler):
|
|
|
3587
3642
|
_note_edit(payload)
|
|
3588
3643
|
except Exception as exc:
|
|
3589
3644
|
_log(f"edit index update skipped: {type(exc).__name__}: {exc}")
|
|
3590
|
-
output =
|
|
3645
|
+
output = _decide_within_budget(payload, client or None)
|
|
3591
3646
|
except Exception as exc:
|
|
3592
3647
|
_log(f"PreToolUse hook without a decision: {type(exc).__name__}: {exc}")
|
|
3593
3648
|
return {}
|
|
3649
|
+
if output is None:
|
|
3650
|
+
return {}
|
|
3594
3651
|
specific = output.get("hookSpecificOutput") or {}
|
|
3595
3652
|
if specific.get("permissionDecision", "deny") != "deny":
|
|
3596
3653
|
return {}
|
package/version.py
CHANGED