@allansantos-dev/smart-tool 0.9.21 → 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,14 @@
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
+
3
12
  ## 0.9.21 - beta
4
13
 
5
14
  - The PreToolUse hook can no longer hold a tool call for long. Its decision has 4 s; past that it answers without one
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.21 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/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
@@ -504,7 +505,32 @@ def working_tree_changes(root):
504
505
  return changes
505
506
 
506
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
+
507
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):
508
534
  """Files the indexed view does not reflect yet: working-tree changes, indexed files whose size or modification time
509
535
  differs on disk, and files tracked by git inside the index scope that were never indexed (commits made after the
510
536
  last indexing). Path -> 'deleted' or 'changed'."""
@@ -537,21 +563,22 @@ def pending_changes(root,view_path):
537
563
  return changes
538
564
 
539
565
 
540
- def build_current(root,view_id=None):
566
+ def build_current(root,view_id=None,shared=False):
541
567
  """The graph an agent should see while editing: the indexed view plus every file it does not reflect yet (see
542
568
  pending_changes and build_overlay) when that view is the current one; a pinned or older view is returned as
543
- indexed."""
569
+ indexed. shared=True hands back the cached graph without copying it: the caller must only read it."""
544
570
  inventory=index_inventory.inspect(root,view_id)
545
571
  selected=inventory.get('selected') or {}
546
572
  changes=pending_changes(root,selected['path']) if selected.get('current') else {}
547
- 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)
548
574
 
549
575
 
550
- def build_overlay(root,changes,view_id=None):
576
+ def build_overlay(root,changes,view_id=None,shared=False):
551
577
  """Graph of the indexed view with the given changed files read from the working tree instead of the snapshot
552
578
  (changes: project-relative path -> 'deleted' or any other status), so a caller right after an edit sees new files,
553
579
  new imports and current line numbers without reindexing or embeddings. A deleted file keeps its indexed copy so
554
- 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)."""
555
582
  inventory=index_inventory.inspect(root,view_id)
556
583
  if not inventory['selected']:
557
584
  return {**inventory,'symbols':[],'dependencies':[],'calls':[],'diagnostics':[],'counts':{},'files':[]}
@@ -586,6 +613,6 @@ def build_overlay(root,changes,view_id=None):
586
613
  with _LOCK:
587
614
  _CACHE[key]=cached
588
615
  while len(_CACHE)>3:_CACHE.popitem(last=False)
589
- data=copy.deepcopy(cached)
616
+ data=dict(cached) if shared else copy.deepcopy(cached)
590
617
  data.update(selected=inventory['selected'],source='indexed_snapshot_with_working_tree',overlaid=[r for r,_h in overlaid])
591
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/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):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allansantos-dev/smart-tool",
3
- "version": "0.9.21",
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",
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.21"
4
+ VERSION = "0.9.22"
5
5
  _CONTACT_RE = re.compile(r"^(?:[^\s@()<>;]+@[^\s@()<>;]+\.[^\s@()<>;]+|https?://[^\s()<>;]+)$")
6
6
 
7
7