@allansantos-dev/smart-tool 0.9.20 → 0.9.22

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,30 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.22 - beta
4
+
5
+ - The review of an Edit or Write (docstring reminder, copied-function check) is about twice as fast on a warm daemon
6
+ (a whole-file Write of the 130 KB `smart_tool_daemon.py`: 1.7 s to 0.8 s), so it stays within the hook's 4 s on large
7
+ files instead of being skipped: the scan of pending changes runs once per review instead of once per use, the
8
+ functions of each changed file are remembered until the file is written again, and the impact lookup reads the cached
9
+ graph instead of copying it. The first review after the daemon starts still waits for the graph to load and may be
10
+ skipped; the work finishes in the background and the next one is fast.
11
+
12
+ ## 0.9.21 - beta
13
+
14
+ - The PreToolUse hook can no longer hold a tool call for long. Its decision has 4 s; past that it answers without one
15
+ (the tool runs) and the daemon log gets one line, `PreToolUse hook over 4 s for Write <file>: answered without a
16
+ decision`; an answer slower than 1 s is logged too. Before, Write and Edit reviews (docstring reminder, copied
17
+ function check) could take longer than the client's 15 s timeout on large TypeScript or Java files and large
18
+ repositories (75 cancelled hook calls in 9 days, 62 of them Write or Edit) and Bash calls of other sessions waited
19
+ behind them.
20
+ - Edit and Write reviews run on their own two workers: a third simultaneous edit goes through unreviewed (logged) instead
21
+ of queueing, and Bash, Read and Grep never wait for them.
22
+ - The Node code analyzer called by the edit hook stops after 10 s, and an analysis that failed or timed out is no
23
+ longer remembered as "this file has no functions".
24
+ - 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
25
+ written by an older version shows as outdated on the setup screen until you accept the hook update (preview and
26
+ backup first).
27
+
3
28
  ## 0.9.20 - beta
4
29
 
5
30
  - 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.20 beta. Windows only. What changed in each version: [CHANGELOG.md](CHANGELOG.md).
18
+ **Status:** 0.9.22 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
@@ -15,7 +15,7 @@ AUDIT_MARKER = "session_audit.py"
15
15
  AUDIT_MATCHER = "startup|resume|clear"
16
16
  AUDIT_TIMEOUT_S = 10
17
17
  HTTP_PATH = "/hooks/pretool"
18
- HTTP_TIMEOUT_S = 15
18
+ HTTP_TIMEOUT_S = 6
19
19
  HOME = os.path.expanduser("~")
20
20
  PREVIEW_TTL_S = 600
21
21
  CLIENTS = {
package/code_graph.py CHANGED
@@ -3,6 +3,7 @@ import ast
3
3
  import collections
4
4
  import copy
5
5
  import gzip
6
+ import contextlib
6
7
  import contextvars
7
8
  import hashlib
8
9
  import json
@@ -233,14 +234,16 @@ def _python_graph(sources):
233
234
 
234
235
 
235
236
  NODE_HEAP_MB = (1024, 4096)
237
+ ANALYZER_TIMEOUT_S = contextvars.ContextVar("analyzer_timeout_s", default=120)
236
238
 
237
239
 
238
240
  def _run_analyzer(script, payload, failure):
239
241
  """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)."""
242
+ needed more than the former fixed 512 MB). The run is killed after `ANALYZER_TIMEOUT_S` seconds (a context
243
+ variable: the edit hook lowers it so a slow file cannot hold its worker)."""
241
244
  for heap in NODE_HEAP_MB:
242
245
  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=120,
246
+ input=payload,capture_output=True,text=True,encoding='utf-8',timeout=ANALYZER_TIMEOUT_S.get(),
244
247
  creationflags=getattr(subprocess,'CREATE_NO_WINDOW',0))
245
248
  if process.returncode==0:
246
249
  return json.loads(process.stdout)
@@ -502,7 +505,32 @@ def working_tree_changes(root):
502
505
  return changes
503
506
 
504
507
 
508
+ _PENDING_SCOPE=contextvars.ContextVar('pending_scope',default=None)
509
+
510
+
511
+ @contextlib.contextmanager
512
+ def pending_scope():
513
+ """Inside the block, `pending_changes` of one (root, view) is computed once: an edit review asks for it from several
514
+ places (copy pool, impact graph) and each answer costs a git status, a git ls-files and a stat of every indexed
515
+ file. The memo ends with the block, so the next review reads the working tree again."""
516
+ token=_PENDING_SCOPE.set({})
517
+ try:
518
+ yield
519
+ finally:
520
+ _PENDING_SCOPE.reset(token)
521
+
522
+
505
523
  def pending_changes(root,view_path):
524
+ """`_pending_changes`, memoized while a `pending_scope` is open."""
525
+ memo=_PENDING_SCOPE.get()
526
+ if memo is None:
527
+ return _pending_changes(root,view_path)
528
+ if (root,view_path) not in memo:
529
+ memo[(root,view_path)]=_pending_changes(root,view_path)
530
+ return dict(memo[(root,view_path)])
531
+
532
+
533
+ def _pending_changes(root,view_path):
506
534
  """Files the indexed view does not reflect yet: working-tree changes, indexed files whose size or modification time
507
535
  differs on disk, and files tracked by git inside the index scope that were never indexed (commits made after the
508
536
  last indexing). Path -> 'deleted' or 'changed'."""
@@ -535,21 +563,22 @@ def pending_changes(root,view_path):
535
563
  return changes
536
564
 
537
565
 
538
- def build_current(root,view_id=None):
566
+ def build_current(root,view_id=None,shared=False):
539
567
  """The graph an agent should see while editing: the indexed view plus every file it does not reflect yet (see
540
568
  pending_changes and build_overlay) when that view is the current one; a pinned or older view is returned as
541
- indexed."""
569
+ indexed. shared=True hands back the cached graph without copying it: the caller must only read it."""
542
570
  inventory=index_inventory.inspect(root,view_id)
543
571
  selected=inventory.get('selected') or {}
544
572
  changes=pending_changes(root,selected['path']) if selected.get('current') else {}
545
- return build_overlay(root,changes,view_id) if changes else build(root,view_id)
573
+ return build_overlay(root,changes,view_id,shared) if changes else build(root,view_id)
546
574
 
547
575
 
548
- def build_overlay(root,changes,view_id=None):
576
+ def build_overlay(root,changes,view_id=None,shared=False):
549
577
  """Graph of the indexed view with the given changed files read from the working tree instead of the snapshot
550
578
  (changes: project-relative path -> 'deleted' or any other status), so a caller right after an edit sees new files,
551
579
  new imports and current line numbers without reindexing or embeddings. A deleted file keeps its indexed copy so
552
- the files that imported it stay linked to it. Cached per snapshot and file contents."""
580
+ the files that imported it stay linked to it. Cached per snapshot and file contents; shared=True returns a new top-level
581
+ dict over the cached lists instead of a deep copy (read-only for the caller)."""
553
582
  inventory=index_inventory.inspect(root,view_id)
554
583
  if not inventory['selected']:
555
584
  return {**inventory,'symbols':[],'dependencies':[],'calls':[],'diagnostics':[],'counts':{},'files':[]}
@@ -584,6 +613,6 @@ def build_overlay(root,changes,view_id=None):
584
613
  with _LOCK:
585
614
  _CACHE[key]=cached
586
615
  while len(_CACHE)>3:_CACHE.popitem(last=False)
587
- data=copy.deepcopy(cached)
616
+ data=dict(cached) if shared else copy.deepcopy(cached)
588
617
  data.update(selected=inventory['selected'],source='indexed_snapshot_with_working_tree',overlaid=[r for r,_h in overlaid])
589
618
  return data
package/code_impact.py CHANGED
@@ -80,7 +80,7 @@ def impact(root, symbol, view_id=None, depth=3, limit=30):
80
80
  raise ValueError("Pass symbol: a function, method (Class.method) or class name.")
81
81
  if type(depth) is not int or not 1 <= depth <= MAX_DEPTH:
82
82
  raise ValueError(f"depth must be an integer from 1 to {MAX_DEPTH}.")
83
- data = code_graph.build_current(root, view_id)
83
+ data = code_graph.build_current(root, view_id, shared=True)
84
84
  found = _matches(data.get("symbols") or [], symbol)
85
85
  diagnostics = [d.get("reason") for d in data.get("diagnostics") or [] if d.get("reason")]
86
86
  if not found:
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
- _PARSED.update(found)
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
- return [_PARSED[key] if key in _PARSED else [] for key in keys]
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/duplicates.py CHANGED
@@ -199,10 +199,15 @@ def _entry(path, name, start, end, file_lines, ext):
199
199
  "hash": hashlib.sha1(normalized.encode("utf-8")).hexdigest(), **_features(body, ext, short)}
200
200
 
201
201
 
202
+ _WORKING_TREE_FUNCTIONS = {}
203
+ _WORKING_TREE_FUNCTIONS_MAX = 2000
204
+
205
+
202
206
  def _working_tree_functions(root, changes, profile, include_tests=False):
203
207
  """Functions of the files the index does not reflect yet, read from disk (production code, plus tests when
204
208
  include_tests), with the same identity as indexed ones, so a copy written earlier in the session is caught and
205
- a function removed since the last indexing is not reported."""
209
+ a function removed since the last indexing is not reported. The functions of a file are remembered per
210
+ (modification time, size), so only files written since the last call are read and parsed again."""
206
211
  found = []
207
212
  kinds = ("code", "test") if include_tests else ("code",)
208
213
  for rel, status in changes.items():
@@ -211,12 +216,20 @@ def _working_tree_functions(root, changes, profile, include_tests=False):
211
216
  if status == "deleted" or ext not in _LANG or kind not in kinds:
212
217
  continue
213
218
  try:
219
+ stat = os.stat(os.path.join(root, rel))
220
+ key = (root, rel, stat.st_mtime_ns, stat.st_size, kind)
221
+ with _LOCK:
222
+ cached = _WORKING_TREE_FUNCTIONS.get(key)
223
+ if cached is not None:
224
+ found += cached
225
+ continue
214
226
  with open(os.path.join(root, rel), encoding="utf-8") as stream:
215
227
  source = stream.read()
216
228
  except (OSError, UnicodeDecodeError):
217
229
  continue
218
230
  if _GENERATED.search(source[:4000]):
219
231
  continue
232
+ before = len(found)
220
233
  lines = source.splitlines()
221
234
  functions = doc_check.functions(rel, source)
222
235
  occurrences = {}
@@ -230,6 +243,10 @@ def _working_tree_functions(root, changes, profile, include_tests=False):
230
243
  occurrence = occurrences[entry["hash"]] = occurrences.get(entry["hash"], 0) + 1
231
244
  found.append({**entry, "kind": kind, "fp": hashlib.sha1(
232
245
  f"{rel}\0{entry['hash']}\0{occurrence}".encode("utf-8")).hexdigest()})
246
+ with _LOCK:
247
+ if len(_WORKING_TREE_FUNCTIONS) >= _WORKING_TREE_FUNCTIONS_MAX:
248
+ _WORKING_TREE_FUNCTIONS.pop(next(iter(_WORKING_TREE_FUNCTIONS)))
249
+ _WORKING_TREE_FUNCTIONS[key] = found[before:]
233
250
  return found
234
251
 
235
252
 
package/hook_decision.py CHANGED
@@ -224,6 +224,12 @@ def _edit_review(payload, doc_mode, duplicate_mode):
224
224
  docs and files outside a project are left out, as in the docs coverage): missing ones deny the edit in require mode and become a reminder in remind mode; a
225
225
  documented function whose signature changed always gets a reminder. Copies of indexed functions follow
226
226
  duplicate_mode and only add context, never deny."""
227
+ with code_graph.pending_scope():
228
+ return _edit_review_scoped(payload, doc_mode, duplicate_mode)
229
+
230
+
231
+ def _edit_review_scoped(payload, doc_mode, duplicate_mode):
232
+ """`_edit_review` inside one `code_graph.pending_scope`, so the pending-changes scan runs once per review."""
227
233
  doc_findings, duplicate_findings, failures = [], [], []
228
234
  for path, before, after in edit_preview.preview(payload.get("tool_name"), payload.get("tool_input"), payload.get("cwd")):
229
235
  if not doc_check.supported(path):
@@ -15,7 +15,7 @@ sys.stdout.reconfigure(encoding="utf-8")
15
15
  sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
16
16
  import daemon_launcher
17
17
 
18
- TIMEOUT_S = 14
18
+ TIMEOUT_S = 5
19
19
 
20
20
 
21
21
  def _client():
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allansantos-dev/smart-tool",
3
- "version": "0.9.20",
3
+ "version": "0.9.22",
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",
@@ -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 = hook_decision.decide(payload, client or None, _web_route)
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
@@ -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.20"
4
+ VERSION = "0.9.22"
5
5
  _CONTACT_RE = re.compile(r"^(?:[^\s@()<>;]+@[^\s@()<>;]+\.[^\s@()<>;]+|https?://[^\s()<>;]+)$")
6
6
 
7
7