@allansantos-dev/smart-tool 0.9.81 → 0.9.82
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 +64 -0
- package/NOTICE +1 -1
- package/README.md +41 -13
- package/agent_redirect.py +32 -0
- package/autostart.py +32 -25
- package/browser_bridge.py +320 -0
- package/browser_control.py +76 -39
- package/browser_service_client.py +1 -7
- package/client_hooks.py +41 -42
- package/code_graph.py +56 -15
- package/code_graph_java.mjs +21 -12
- package/code_health.py +14 -9
- package/codex_config.py +3 -0
- package/daemon_launcher.py +37 -5
- package/doc_candidates.py +63 -0
- package/doc_check.py +9 -3
- package/doc_extras.py +2 -3
- package/doc_insights.py +26 -2
- package/doc_prose.py +59 -11
- package/doc_registry.py +241 -0
- package/doc_scope.py +205 -39
- package/doc_turn.py +205 -0
- package/doc_validate.py +52 -9
- package/document_read.py +4 -2
- package/edit_shape.py +119 -0
- package/endpoint_sync.py +3 -21
- package/engine_home.py +95 -0
- package/hook_call.py +132 -0
- package/hook_decision.py +100 -6
- package/hooks/pretool_router.py +36 -30
- package/hooks/turn_end.py +40 -0
- package/index_views.py +5 -0
- package/install.py +24 -9
- package/lazy_mcp_proxy.py +10 -9
- package/local_embedder.py +8 -1
- package/locales/pt-BR.json +1 -2
- package/model_breaker.py +6 -5
- package/package.json +2 -2
- package/process_limits.py +17 -3
- package/project_identity.py +19 -1
- package/project_scan.py +98 -0
- package/scout_access.py +48 -0
- package/shell_reads.py +67 -0
- package/smart_tool_daemon.py +144 -39
- package/tray.py +31 -0
- package/venv_launch.py +67 -0
- package/version.py +1 -1
- package/web_adapters/node/engine-home.mjs +42 -0
- package/web_adapters/node/package-lock.json +13 -0
- package/web_adapters/node/package.json +1 -0
package/hook_decision.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""Decisão do PreToolUse e do PostToolUse do Smart Tool, tomada dentro do daemon já aquecido.
|
|
2
2
|
|
|
3
|
-
O Claude Code
|
|
4
|
-
|
|
3
|
+
O Claude Code e o Codex chamam as rotas POST /hooks/pretool, /hooks/posttool e /hooks/subagent-start pelo hook de
|
|
4
|
+
comando `hooks/pretool_router.py`, que lê o endereço do daemon no arquivo de execução a cada chamada, espera um
|
|
5
|
+
reinício e só repassa o payload (`hook_call`).
|
|
5
6
|
|
|
6
7
|
Uma busca nativa que a regra redireciona, e WebFetch/WebSearch, não são negadas: o Smart Tool responde no lugar e o
|
|
7
8
|
texto volta como saída da própria chamada (`tool_redirect`). INVARIANTE: a saída do PreToolUse é `{}`, `updatedInput`
|
|
@@ -17,6 +18,7 @@ import time
|
|
|
17
18
|
import uuid
|
|
18
19
|
|
|
19
20
|
import affected_tests
|
|
21
|
+
import agent_redirect
|
|
20
22
|
import code_graph
|
|
21
23
|
import code_impact
|
|
22
24
|
import comment_check
|
|
@@ -28,6 +30,7 @@ import doc_scope
|
|
|
28
30
|
import dossier
|
|
29
31
|
import duplicates
|
|
30
32
|
import edit_preview
|
|
33
|
+
import edit_shape
|
|
31
34
|
import index_profile
|
|
32
35
|
import index_scope
|
|
33
36
|
import layer_check
|
|
@@ -39,6 +42,8 @@ import redirect_rule
|
|
|
39
42
|
import removed_callers
|
|
40
43
|
import requester
|
|
41
44
|
import router
|
|
45
|
+
import scout_access
|
|
46
|
+
import shell_reads
|
|
42
47
|
import tool_redirect
|
|
43
48
|
|
|
44
49
|
WEB_TOOLS = ("WebSearch", "WebFetch")
|
|
@@ -332,6 +337,35 @@ def _credential_lines(findings):
|
|
|
332
337
|
"variable or a secret store. If it is a fake value for a test, go ahead.")
|
|
333
338
|
|
|
334
339
|
|
|
340
|
+
def _cycle_lines(findings):
|
|
341
|
+
"""The note that names the import cycles an edit's added imports close.
|
|
342
|
+
|
|
343
|
+
Parameters: findings (list of (rel, item)) the `edit_shape.added_cycles` items with the file that was edited.
|
|
344
|
+
Returns: str, one sentence per import, cut at MAX_DOC_FINDINGS.
|
|
345
|
+
"""
|
|
346
|
+
lines = [f"{rel}:{item['line']} now imports `{item['imports']}`, which already leads back to it "
|
|
347
|
+
f"({' -> '.join([rel, *item['chain']])}): the import closes a cycle" for rel, item in findings[:MAX_DOC_FINDINGS]]
|
|
348
|
+
if len(findings) > MAX_DOC_FINDINGS:
|
|
349
|
+
lines.append(f"{len(findings) - MAX_DOC_FINDINGS} more import(s) in the same situation")
|
|
350
|
+
return ("Smart Tool cycles: " + "; ".join(lines) + ". Move what both files need into a third module, or import it where "
|
|
351
|
+
"it is used; if the cycle is intended, go ahead.")
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _history_lines(hotspots, coupled):
|
|
355
|
+
"""The note that names the hotspots among the edited files and the files that usually change with them.
|
|
356
|
+
|
|
357
|
+
Parameters: hotspots, coupled (lists) the two lists of `edit_shape.history_findings`.
|
|
358
|
+
Returns: str, one sentence per file, cut at MAX_DOC_FINDINGS.
|
|
359
|
+
"""
|
|
360
|
+
lines = [f"`{item['path']}` is a hotspot of the project (rank {item['rank']}, {item['commits']} commits): keep the edit "
|
|
361
|
+
"small and run its tests" for item in hotspots]
|
|
362
|
+
lines += [f"`{item['path']}` usually changes with `{item['with']}` ({item['shared']} shared commits, {item['degree']}%) "
|
|
363
|
+
"and is not in this edit: check whether it needs the same change" for item in coupled]
|
|
364
|
+
if len(lines) > MAX_DOC_FINDINGS:
|
|
365
|
+
lines[MAX_DOC_FINDINGS:] = [f"{len(lines) - MAX_DOC_FINDINGS} more file(s) in the same situation"]
|
|
366
|
+
return "Smart Tool history: " + "; ".join(lines) + "."
|
|
367
|
+
|
|
368
|
+
|
|
335
369
|
def _link_lines(broken, unchecked):
|
|
336
370
|
"""The link notes: the links that point to nothing, and the ones that could not be checked with the reason."""
|
|
337
371
|
parts = []
|
|
@@ -418,21 +452,47 @@ def _dependency_part(before, after, ecosystem):
|
|
|
418
452
|
return f"{dependency_notice.PREFIX}dependency check skipped: {exc}"
|
|
419
453
|
|
|
420
454
|
|
|
455
|
+
def _history_findings(edited, failures):
|
|
456
|
+
"""The hotspot and change-partner facts of the edited files, per project, from the cached history only.
|
|
457
|
+
|
|
458
|
+
Parameters: edited (dict) project folder -> project-relative paths of the edit; failures (list) where a failed lookup
|
|
459
|
+
adds its line.
|
|
460
|
+
Returns: tuple (hotspots, coupled) as `edit_shape.history_findings` lists, over every project of the edit.
|
|
461
|
+
"""
|
|
462
|
+
hotspots, coupled = [], []
|
|
463
|
+
for root, rels in edited.items():
|
|
464
|
+
try:
|
|
465
|
+
found = edit_shape.history_findings(root, rels)
|
|
466
|
+
except (ValueError, RuntimeError, OSError) as exc:
|
|
467
|
+
failures.append(f"Smart Tool history check skipped: {exc}")
|
|
468
|
+
continue
|
|
469
|
+
hotspots += found["hotspots"]
|
|
470
|
+
coupled += found["coupled"]
|
|
471
|
+
return hotspots, coupled
|
|
472
|
+
|
|
473
|
+
|
|
421
474
|
def _review_changes(changes, doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode="off", link_mode="off",
|
|
422
|
-
decision_mode="off"):
|
|
475
|
+
decision_mode="off", shape=False):
|
|
423
476
|
"""The checks of an edit over (path, before, after) triples: docstrings, comments, documentation anchors, copies,
|
|
424
477
|
dependency advisories, local links, layers and the decisions recorded about the touched functions, each following its mode.
|
|
478
|
+
With `shape`, two more notes read only the cached code graph and history: the import cycle an added Python import
|
|
479
|
+
closes and the hotspots or usual change partners among the edited files.
|
|
425
480
|
|
|
426
|
-
Parameters: changes (iterable of (absolute path, text before or None, text after)); the modes (`off` skips a check)
|
|
481
|
+
Parameters: changes (iterable of (absolute path, text before or None, text after)); the modes (`off` skips a check);
|
|
482
|
+
shape (bool) whether the cycle and history notes run: the edit hook asks for them, a review of a finished diff does not
|
|
483
|
+
(`pr_impact` reports both from the whole graph and history).
|
|
427
484
|
Returns: tuple (message parts, doc findings, comment findings); the parts are empty when nothing is worth saying.
|
|
428
485
|
"""
|
|
429
486
|
doc_findings, scope_findings, duplicate_findings, failures, dependency_parts = [], [], [], [], []
|
|
430
487
|
comment_findings, layer_findings, credential_findings, caller_answers = [], [], [], []
|
|
431
488
|
broken_links, unchecked_links = [], []
|
|
432
489
|
decision_findings, decisions_asked = [], 0
|
|
490
|
+
cycle_findings, edited_files = [], {}
|
|
433
491
|
for path, before, after in changes:
|
|
434
492
|
root = _project_of(path)
|
|
435
493
|
rel = os.path.relpath(os.path.realpath(path), os.path.realpath(root)).replace(os.sep, "/") if root else os.path.basename(path)
|
|
494
|
+
if shape and root:
|
|
495
|
+
edited_files.setdefault(root, []).append(rel)
|
|
436
496
|
credential_findings += [(rel, item) for item in credential_check.review(before, after)]
|
|
437
497
|
if link_mode != "off":
|
|
438
498
|
try:
|
|
@@ -447,6 +507,11 @@ def _review_changes(changes, doc_mode, duplicate_mode, dependency_mode, comment_
|
|
|
447
507
|
continue
|
|
448
508
|
if not doc_check.supported(path):
|
|
449
509
|
continue
|
|
510
|
+
if shape and root:
|
|
511
|
+
try:
|
|
512
|
+
cycle_findings += [(rel, item) for item in edit_shape.added_cycles(root, rel, before, after)]
|
|
513
|
+
except (ValueError, RuntimeError, OSError) as exc:
|
|
514
|
+
failures.append(f"Smart Tool cycles check skipped: {exc}")
|
|
450
515
|
edited = doc_check.written(path, before, after)
|
|
451
516
|
profile = index_profile.current((index_scope.load_scope(root) or {}).get("profile")) if root else None
|
|
452
517
|
if doc_mode != "off" and root and index_profile.kind(rel, profile) == "code":
|
|
@@ -480,14 +545,17 @@ def _review_changes(changes, doc_mode, duplicate_mode, dependency_mode, comment_
|
|
|
480
545
|
caller_answers.append(answer)
|
|
481
546
|
except (ValueError, RuntimeError, OSError) as exc:
|
|
482
547
|
failures.append(f"Smart Tool callers check skipped: {exc}")
|
|
548
|
+
hotspots, coupled = _history_findings(edited_files, failures)
|
|
483
549
|
parts = ([_doc_lines(doc_findings)] if doc_findings else []) + \
|
|
484
550
|
([_comment_lines(comment_findings)] if comment_findings else []) + \
|
|
485
551
|
([_scope_lines(scope_findings)] if scope_findings else []) + \
|
|
486
552
|
([_duplicate_lines(duplicate_findings)] if duplicate_findings else []) + \
|
|
487
553
|
([_layer_lines(layer_findings)] if layer_findings else []) + \
|
|
554
|
+
([_cycle_lines(cycle_findings)] if cycle_findings else []) + \
|
|
488
555
|
([_decision_lines(decision_findings)] if decision_findings else []) + \
|
|
489
556
|
([_callers_lines(caller_answers)] if caller_answers else []) + \
|
|
490
557
|
([_credential_lines(credential_findings)] if credential_findings else []) + \
|
|
558
|
+
([_history_lines(hotspots, coupled)] if hotspots or coupled else []) + \
|
|
491
559
|
_link_lines(broken_links, unchecked_links) + failures + dependency_parts
|
|
492
560
|
return parts, doc_findings, comment_findings
|
|
493
561
|
|
|
@@ -607,7 +675,7 @@ def _edit_review_scoped(payload, doc_mode, duplicate_mode, dependency_mode, comm
|
|
|
607
675
|
"""`_edit_review` inside one `code_graph.pending_scope`, so the pending-changes scan runs once per review."""
|
|
608
676
|
parts, doc_findings, comment_findings = _review_changes(
|
|
609
677
|
edit_preview.preview(payload.get("tool_name"), payload.get("tool_input"), payload.get("cwd")),
|
|
610
|
-
doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode, link_mode, decision_mode)
|
|
678
|
+
doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode, link_mode, decision_mode, shape=True)
|
|
611
679
|
if not parts:
|
|
612
680
|
return {}
|
|
613
681
|
message = " ".join(parts)
|
|
@@ -646,12 +714,14 @@ def decide(payload, client, web_route, jobs):
|
|
|
646
714
|
requester.note_hook(payload["tool_name"].removeprefix(f"mcp__{SMART_TOOL_SERVER}__"), payload.get("tool_input"),
|
|
647
715
|
payload.get("session_id"), payload.get("cwd"))
|
|
648
716
|
return {}
|
|
717
|
+
if isinstance(payload, dict) and payload.get("tool_name") in agent_redirect.LAUNCH_TOOLS:
|
|
718
|
+
return _launch_decision(payload, config.hook_mode(cfg))
|
|
649
719
|
mode = config.hook_mode(cfg)
|
|
650
720
|
if mode == "off" or not isinstance(payload, dict):
|
|
651
721
|
return {}
|
|
652
722
|
plan = _route(payload, client, web_route, cfg)
|
|
653
723
|
if "kind" not in plan:
|
|
654
|
-
return plan
|
|
724
|
+
return plan or _shell_read_tip(payload)
|
|
655
725
|
if mode == "advise":
|
|
656
726
|
return advise(plan["message"])
|
|
657
727
|
if plan["kind"] and payload.get("tool_use_id"):
|
|
@@ -661,6 +731,30 @@ def decide(payload, client, web_route, jobs):
|
|
|
661
731
|
return _answer_in_place(payload, client, plan, jobs)
|
|
662
732
|
|
|
663
733
|
|
|
734
|
+
def _shell_read_tip(payload):
|
|
735
|
+
"""The tip that goes with the third source file read through the shell in a session (`shell_reads`), `{}` otherwise
|
|
736
|
+
and where smart_scout is not callable."""
|
|
737
|
+
if payload.get("tool_name") != "Bash":
|
|
738
|
+
return {}
|
|
739
|
+
tool_input = payload.get("tool_input") if isinstance(payload.get("tool_input"), dict) else {}
|
|
740
|
+
message = shell_reads.note(payload.get("session_id"), tool_input.get("command"), payload.get("cwd"))
|
|
741
|
+
if message is None or not agent_has_smart_tool(payload.get("agent_type") if payload.get("agent_id") else None,
|
|
742
|
+
payload.get("cwd")) or not scout_access.callable_in(payload):
|
|
743
|
+
return {}
|
|
744
|
+
return advise(message)
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
def _launch_decision(payload, mode):
|
|
748
|
+
"""A subagent launch that `agent_redirect` matches carries the smart_scout call to make as a tip in redirect and
|
|
749
|
+
advise modes (the launch always runs); nothing in off mode, where the launching agent has no Smart Tool and where
|
|
750
|
+
smart_scout is not callable without a permission prompt (see `scout_access`)."""
|
|
751
|
+
message = agent_redirect.reason(payload) if mode != "off" else None
|
|
752
|
+
if message is None or not agent_has_smart_tool(payload.get("agent_type") if payload.get("agent_id") else None,
|
|
753
|
+
payload.get("cwd")) or not scout_access.callable_in(payload):
|
|
754
|
+
return {}
|
|
755
|
+
return advise(message)
|
|
756
|
+
|
|
757
|
+
|
|
664
758
|
def _start_job(payload, client, plan, jobs, key):
|
|
665
759
|
"""Starts the Smart Tool call of a redirected one under key (web calls carry the requester of the hook)."""
|
|
666
760
|
who = requester.CURRENT.set(requester.from_hook(client, payload)) if payload["tool_name"] in WEB_TOOLS else None
|
package/hooks/pretool_router.py
CHANGED
|
@@ -1,47 +1,53 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
"""Command hook of Claude Code and Codex: thin client that forwards the payload to the Smart Tool daemon
|
|
3
|
+
(`--route pretool|posttool|subagent-start`), where the decision runs already warm (`hook_decision.py`). The daemon
|
|
4
|
+
address comes from its run file on every call, a restart is waited out with backoff (`hook_call`), and a daemon that
|
|
5
|
+
stays unreachable fails open: the call runs, neutral output and one plain message the first time.
|
|
5
6
|
"""
|
|
6
7
|
import json
|
|
7
8
|
import os
|
|
8
9
|
import sys
|
|
9
|
-
import urllib.parse
|
|
10
|
-
import urllib.request
|
|
11
|
-
|
|
12
|
-
sys.stdin.reconfigure(encoding="utf-8")
|
|
13
|
-
sys.stdout.reconfigure(encoding="utf-8")
|
|
14
10
|
|
|
15
11
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
|
16
|
-
import
|
|
12
|
+
import hook_call
|
|
13
|
+
|
|
14
|
+
EVENTS = {"pretool": "PreToolUse", "posttool": "PostToolUse", "subagent-start": "SubagentStart"}
|
|
15
|
+
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
def _arg(argv, name):
|
|
18
|
+
return argv[argv.index(name) + 1] if name in argv and argv.index(name) + 1 < len(argv) else ""
|
|
20
19
|
|
|
21
20
|
|
|
22
|
-
def
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
def run(argv, raw):
|
|
22
|
+
"""The JSON object the hook prints for the payload `raw` (bytes) under the command line `argv`."""
|
|
23
|
+
route = _arg(argv, "--route")
|
|
24
|
+
if route not in EVENTS:
|
|
25
|
+
return {"systemMessage": hook_call.message(
|
|
26
|
+
"command", "this hook entry is from an older Smart Tool version (no --route)",
|
|
27
|
+
"The call ran without Smart Tool routing.", "Install the hooks again from the Smart Tool setup screen.")}
|
|
28
|
+
output, failure = hook_call.call(route, raw, _arg(argv, "--client"))
|
|
29
|
+
if failure is None:
|
|
30
|
+
return output
|
|
31
|
+
tool = ""
|
|
32
|
+
try:
|
|
33
|
+
tool = json.loads(raw or b"{}").get("tool_name") or ""
|
|
34
|
+
except ValueError:
|
|
35
|
+
pass
|
|
36
|
+
effect = f"The {tool + ' ' if tool else ''}call was let through without Smart Tool routing."
|
|
37
|
+
return {"systemMessage": hook_call.message(EVENTS[route], failure, effect)}
|
|
25
38
|
|
|
26
39
|
|
|
27
40
|
def main():
|
|
41
|
+
"""Reads the payload from stdin and prints the hook's JSON answer; any failure prints a message and lets the call run."""
|
|
42
|
+
sys.stdout.reconfigure(encoding="utf-8")
|
|
28
43
|
raw = sys.stdin.buffer.read()
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
route = "posttool" if "--post" in sys.argv[1:] else "pretool"
|
|
36
|
-
request = urllib.request.Request(f"{base}/hooks/{route}?{urllib.parse.urlencode({'client': _client()})}",
|
|
37
|
-
data=raw, headers={"Content-Type": "application/json"}, method="POST")
|
|
38
|
-
with urllib.request.urlopen(request, timeout=TIMEOUT_S) as response:
|
|
39
|
-
output = json.loads(response.read() or b"{}")
|
|
40
|
-
print(json.dumps(output if isinstance(output, dict) else {}, ensure_ascii=False))
|
|
44
|
+
try:
|
|
45
|
+
output = run(sys.argv[1:], raw)
|
|
46
|
+
except Exception as exc:
|
|
47
|
+
output = {"systemMessage": hook_call.message(
|
|
48
|
+
"command", f"the hook failed ({type(exc).__name__}: {exc})", "The call was let through.")}
|
|
49
|
+
print(json.dumps(output, ensure_ascii=False))
|
|
41
50
|
|
|
42
51
|
|
|
43
52
|
if __name__ == "__main__":
|
|
44
|
-
|
|
45
|
-
main()
|
|
46
|
-
except BaseException:
|
|
47
|
-
print("{}")
|
|
53
|
+
main()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Stop hook of Claude Code (type command): forwards the payload to the Smart Tool daemon (POST /hooks/stop), whose
|
|
3
|
+
address it reads on every call from the run file the daemon writes (`hook_call`), and prints the daemon's answer (a block
|
|
4
|
+
with the docstrings to review, or nothing). A continuation (stop_hook_active) exits at once without calling the daemon.
|
|
5
|
+
A restart is waited out; a daemon that stays unreachable prints one plain message and the step stays pending.
|
|
6
|
+
"""
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
|
12
|
+
import hook_call
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def run(raw):
|
|
16
|
+
"""The JSON object the hook prints for the Stop payload `raw` (bytes); {} for a continuation or an empty answer."""
|
|
17
|
+
if json.loads(raw or b"{}").get("stop_hook_active"):
|
|
18
|
+
return {}
|
|
19
|
+
output, failure = hook_call.call("stop", raw)
|
|
20
|
+
if failure is None:
|
|
21
|
+
return output
|
|
22
|
+
return {"systemMessage": hook_call.message(
|
|
23
|
+
"Stop", failure, "The end-of-turn documentation step did not run and its work stays pending.")}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def main():
|
|
27
|
+
"""Reads the Stop payload from stdin and prints the answer when it is not empty; a failure prints a message."""
|
|
28
|
+
sys.stdout.reconfigure(encoding="utf-8")
|
|
29
|
+
raw = sys.stdin.buffer.read()
|
|
30
|
+
try:
|
|
31
|
+
output = run(raw)
|
|
32
|
+
except Exception as exc:
|
|
33
|
+
output = {"systemMessage": hook_call.message(
|
|
34
|
+
"Stop", f"the hook failed ({type(exc).__name__}: {exc})", "The end-of-turn documentation step did not run.")}
|
|
35
|
+
if output:
|
|
36
|
+
print(json.dumps(output, ensure_ascii=False))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
if __name__ == "__main__":
|
|
40
|
+
main()
|
package/index_views.py
CHANGED
|
@@ -73,6 +73,11 @@ def _validated(key):
|
|
|
73
73
|
return old[1] if time.monotonic()-old[0] < 1 else None
|
|
74
74
|
|
|
75
75
|
|
|
76
|
+
def cached_view(root):
|
|
77
|
+
"""The view `describe` holds for `root` while it is still the current one, else None; checks by stat and never runs git."""
|
|
78
|
+
return _validated(project_identity.canonical_root(project_identity.display_root(root)))
|
|
79
|
+
|
|
80
|
+
|
|
76
81
|
def describe(root, fresh=False):
|
|
77
82
|
"""Git view of `root`. `fresh` asks for a check, not for a new resolution: a cached Git view whose stamp (HEAD, index,
|
|
78
83
|
logs/HEAD, packed-refs, the ref file) did not move is still the current one and costs no git process. Concurrent
|
package/install.py
CHANGED
|
@@ -16,8 +16,10 @@ from pathlib import Path
|
|
|
16
16
|
import atomic_io
|
|
17
17
|
import client_hooks
|
|
18
18
|
import config
|
|
19
|
+
import daemon_launcher
|
|
19
20
|
import i18n
|
|
20
21
|
import paths
|
|
22
|
+
import venv_launch
|
|
21
23
|
import version
|
|
22
24
|
|
|
23
25
|
SOURCE = Path(__file__).resolve().parent
|
|
@@ -27,6 +29,7 @@ ENTRY_SCRIPTS = ("tray.py", "smart_tool_daemon.py", "pretool_router.py", "sessio
|
|
|
27
29
|
HOOK_SCRIPTS = ("pretool_router.py", "session_audit.py")
|
|
28
30
|
STARTUP_SCRIPT = Path(os.environ.get("APPDATA", str(Path.home() / "AppData" / "Roaming"))) / "Microsoft" / "Windows" / \
|
|
29
31
|
"Start Menu" / "Programs" / "Startup" / "SmartTool-Tray.vbs"
|
|
32
|
+
TASK_SCRIPT = Path(paths.DATA_DIR) / "tray-start.vbs"
|
|
30
33
|
MANIFEST = ".install-manifest.json"
|
|
31
34
|
DEPENDENCY_INPUTS = ("requirements.txt", "install_runtime.py", "node_locator.py", "web_adapters/node/package.json",
|
|
32
35
|
"web_adapters/node/package-lock.json", "web_adapters/browser/requirements.txt",
|
|
@@ -113,11 +116,15 @@ def _same_folder(path, folder):
|
|
|
113
116
|
|
|
114
117
|
|
|
115
118
|
def stop_processes(target):
|
|
119
|
+
"""Closes every process running a script of the installation in `target` (daemon, tray, browser proxies); a closed
|
|
120
|
+
daemon gets its reason in the daemon log, since a forced stop leaves it no chance to write one."""
|
|
116
121
|
def running():
|
|
117
|
-
return [pid for pid, line in _processes() for script in _script_paths(line)
|
|
122
|
+
return [(pid, Path(script).name) for pid, line in _processes() for script in _script_paths(line)
|
|
118
123
|
if Path(script).name not in HOOK_SCRIPTS and _same_folder(_script_folder(script), target)]
|
|
119
124
|
|
|
120
|
-
for pid in running():
|
|
125
|
+
for pid, script in running():
|
|
126
|
+
if script == Path(daemon_launcher.DAEMON_SCRIPT).name:
|
|
127
|
+
daemon_launcher.note_stop(f"daemon pid {pid} closed by the installer to update {target}")
|
|
121
128
|
powershell(f"Stop-Process -Id {pid} -Force -ErrorAction SilentlyContinue")
|
|
122
129
|
deadline = time.monotonic() + 10
|
|
123
130
|
while True:
|
|
@@ -192,12 +199,19 @@ def _task_definition():
|
|
|
192
199
|
return result.stdout if result.returncode == 0 else ""
|
|
193
200
|
|
|
194
201
|
|
|
202
|
+
def _script_text(path):
|
|
203
|
+
"""Text of a launch script or hook file, "" when absent: VBScript files are written in UTF-16, the rest in UTF-8."""
|
|
204
|
+
if not path.is_file():
|
|
205
|
+
return ""
|
|
206
|
+
data = path.read_bytes()
|
|
207
|
+
return data.decode("utf-16") if data[:2] in (b"\xff\xfe", b"\xfe\xff") else data.decode("utf-8", errors="replace")
|
|
208
|
+
|
|
209
|
+
|
|
195
210
|
def other_installs(target):
|
|
196
211
|
"""Installer-made Smart Tool folders other than target, found through what still runs or points at them: tray and
|
|
197
|
-
daemon processes, the logon task
|
|
212
|
+
daemon processes, the logon task and the script it runs, the Startup script, and agent hook commands."""
|
|
198
213
|
texts = [line for _pid, line in _processes()] + [_task_definition()]
|
|
199
|
-
texts += [path
|
|
200
|
-
if path.is_file()]
|
|
214
|
+
texts += [_script_text(path) for path in (TASK_SCRIPT, STARTUP_SCRIPT, *hook_files())]
|
|
201
215
|
folders = {_install_folder(script) for text in texts for script in _script_paths(text)}
|
|
202
216
|
return sorted(folder for folder in folders
|
|
203
217
|
if folder and not _same_folder(folder, target) and not _same_folder(folder, SOURCE))
|
|
@@ -231,9 +245,9 @@ def remove_other_installs(target):
|
|
|
231
245
|
path.write_text(text, encoding="utf-8")
|
|
232
246
|
client_hooks.record_codex_trust(trust, str(path.with_name("config.toml")))
|
|
233
247
|
print(t("Hooks in {path} now point to {target}.", path=path, target=target))
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
248
|
+
for script_path in (TASK_SCRIPT, STARTUP_SCRIPT):
|
|
249
|
+
if any(_same_folder(_script_folder(script), folder) for script in _script_paths(_script_text(script_path))):
|
|
250
|
+
script_path.unlink()
|
|
237
251
|
shutil.rmtree(folder)
|
|
238
252
|
removed.append(folder)
|
|
239
253
|
return removed
|
|
@@ -368,7 +382,8 @@ def install_dependencies(target, python):
|
|
|
368
382
|
def start(target, python, pythonw):
|
|
369
383
|
subprocess.run([str(python), "-c", "import autostart, json; print(json.dumps(autostart.install()))"],
|
|
370
384
|
cwd=target, check=True, capture_output=True, text=True, timeout=60)
|
|
371
|
-
|
|
385
|
+
interpreter, additions = venv_launch.direct(pythonw if pythonw.exists() else python)
|
|
386
|
+
subprocess.Popen([interpreter, str(target / "tray.py")], cwd=target, env=venv_launch.environment(additions),
|
|
372
387
|
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
373
388
|
creationflags=getattr(subprocess, "DETACHED_PROCESS", 0) | getattr(subprocess, "CREATE_NO_WINDOW", 0))
|
|
374
389
|
|
package/lazy_mcp_proxy.py
CHANGED
|
@@ -37,6 +37,15 @@ def load_handshake(path):
|
|
|
37
37
|
return data
|
|
38
38
|
|
|
39
39
|
|
|
40
|
+
def local_initialize(handshake, params):
|
|
41
|
+
"""The cached initialize result, speaking the protocol version the client asked for when it is a known one."""
|
|
42
|
+
result = dict(handshake["initialize"])
|
|
43
|
+
asked = (params or {}).get("protocolVersion")
|
|
44
|
+
if asked in KNOWN_PROTOCOLS:
|
|
45
|
+
result["protocolVersion"] = asked
|
|
46
|
+
return result
|
|
47
|
+
|
|
48
|
+
|
|
40
49
|
class Proxy:
|
|
41
50
|
"""One client conversation: local answers until a request needs the real server, then a pass-through."""
|
|
42
51
|
|
|
@@ -58,14 +67,6 @@ class Proxy:
|
|
|
58
67
|
def answer(self, request_id, result):
|
|
59
68
|
self.send({"jsonrpc": "2.0", "id": request_id, "result": result})
|
|
60
69
|
|
|
61
|
-
def local_initialize(self, params):
|
|
62
|
-
"""The cached initialize result, speaking the protocol version the client asked for when it is a known one."""
|
|
63
|
-
result = dict(self.handshake["initialize"])
|
|
64
|
-
asked = (params or {}).get("protocolVersion")
|
|
65
|
-
if asked in KNOWN_PROTOCOLS:
|
|
66
|
-
result["protocolVersion"] = asked
|
|
67
|
-
return result
|
|
68
|
-
|
|
69
70
|
def start_child(self):
|
|
70
71
|
"""Starts the real server and replays the client's initialize to it; its answer is not forwarded (the client
|
|
71
72
|
already got the cached one). RuntimeError when it exits or answers with an error."""
|
|
@@ -141,7 +142,7 @@ class Proxy:
|
|
|
141
142
|
if method == "initialize":
|
|
142
143
|
self.client_init = message.get("params")
|
|
143
144
|
if self.child is None and request_id is not None and method == "initialize":
|
|
144
|
-
return self.answer(request_id, self.
|
|
145
|
+
return self.answer(request_id, local_initialize(self.handshake, message.get("params")))
|
|
145
146
|
if self.child is None and request_id is not None and method == "ping":
|
|
146
147
|
return self.answer(request_id, {})
|
|
147
148
|
if self.child is None and request_id is not None and method == "tools/list":
|
package/local_embedder.py
CHANGED
|
@@ -25,6 +25,7 @@ MIN_API_VERSION = 1
|
|
|
25
25
|
BATCH = 16
|
|
26
26
|
INFO_TTL_S = 60
|
|
27
27
|
MAX_ATTEMPTS = 3
|
|
28
|
+
HOME_FAILURE_CODES = (*model_breaker.BUSY_CODES, 500)
|
|
28
29
|
REQUEST_TIMEOUT_S = 60
|
|
29
30
|
RERANK_TIMEOUT_S = 15
|
|
30
31
|
_lock = threading.Lock()
|
|
@@ -189,6 +190,11 @@ def _retry_delay(failure, attempt):
|
|
|
189
190
|
|
|
190
191
|
|
|
191
192
|
def embed(model, texts, deadline=None, cancel_check=None, on_retry=None):
|
|
193
|
+
"""Vectors of `texts` from the embeddings home, in batches of BATCH, each tried up to MAX_ATTEMPTS times.
|
|
194
|
+
|
|
195
|
+
Returns the vectors in input order. Raises IndexCompatibilityError when the home serves another model,
|
|
196
|
+
model_breaker.CircuitOpen once repeated busy or failing answers opened the circuit (no request is sent while it is
|
|
197
|
+
open), RuntimeError when the home is not running or a batch fails every attempt."""
|
|
192
198
|
base = url()
|
|
193
199
|
if not base:
|
|
194
200
|
raise RuntimeError(f"The embeddings home is not running (no announce file at {ANNOUNCE}).")
|
|
@@ -217,8 +223,9 @@ def embed(model, texts, deadline=None, cancel_check=None, on_retry=None):
|
|
|
217
223
|
with model_breaker.slot(model):
|
|
218
224
|
got = _request(conn, "POST", "/embed", {"inputs": part})
|
|
219
225
|
except (OSError, http.client.HTTPException) as exc:
|
|
220
|
-
model_breaker.note(model, exc)
|
|
226
|
+
model_breaker.note(model, exc, HOME_FAILURE_CODES)
|
|
221
227
|
model_health.record("/v1/embeddings", {"model": model}, exc)
|
|
228
|
+
model_breaker.guard(model)
|
|
222
229
|
conn.close()
|
|
223
230
|
conn = _connection(base, _left(deadline))
|
|
224
231
|
if attempt == MAX_ATTEMPTS:
|
package/locales/pt-BR.json
CHANGED
|
@@ -845,7 +845,6 @@
|
|
|
845
845
|
"Its {0} entry does not cover {1}.": "A entrada {0} dele não cobre {1}.",
|
|
846
846
|
"It calls {0} instead of {1}.": "Ele chama {0} em vez de {1}.",
|
|
847
847
|
"Its tool list is ordered or written differently from the current one.": "A lista de ferramentas dele está em outra ordem ou escrita de outro jeito que a atual.",
|
|
848
|
-
"The daemon announcement ({0}) is outside the home folder; the hook cannot name it as its port file.": "O anúncio do daemon ({0}) está fora da pasta do usuário; o hook não consegue nomeá-lo como seu arquivo de porta.",
|
|
849
848
|
"Path contains a space and has no 8.3 short name ({0}); the hook was not installed.": "O caminho tem um espaço e não tem nome curto 8.3 ({0}); o hook não foi instalado.",
|
|
850
849
|
"{0} does not have the expected format (object with 'hooks'); nothing was changed.": "{0} não tem o formato esperado (objeto com 'hooks'); nada foi alterado.",
|
|
851
850
|
"{0} does not appear in {1} after writing; the file was restored.": "{0} não aparece em {1} depois de gravar; o arquivo foi restaurado.",
|
|
@@ -855,7 +854,6 @@
|
|
|
855
854
|
"Codex does not trust the hook in {0} after recording its approval.": "O Codex não confia no hook em {0} depois de gravar a aprovação.",
|
|
856
855
|
"Codex does not trust the audit hook in {0} after recording its approval.": "O Codex não confia no hook de auditoria em {0} depois de gravar a aprovação.",
|
|
857
856
|
"The hook approval does not read back from {0} after writing; the file was restored.": "A aprovação do hook não é lida de volta em {0} depois de gravar; o arquivo foi restaurado.",
|
|
858
|
-
"Smart Tool daemon has no registered URL; start the daemon before installing the hook.": "O serviço do Smart Tool não tem URL registrada; inicie o serviço antes de instalar o hook.",
|
|
859
857
|
"{0}: hooks.{1} is not a list; nothing was changed.": "{0}: hooks.{1} não é uma lista; nada foi alterado.",
|
|
860
858
|
"Smart Tool updated from {old} to {new}.": "Smart Tool atualizado da versão {old} para a {new}.",
|
|
861
859
|
"The update to {target} failed; Smart Tool {version} is still installed. It is tried again in 24 h, or now from this menu.": "A atualização para a versão {target} falhou; o Smart Tool {version} continua instalado. Ela será tentada de novo em 24 h, ou agora por este menu.",
|
|
@@ -1018,6 +1016,7 @@
|
|
|
1018
1016
|
},
|
|
1019
1017
|
"ignore": [
|
|
1020
1018
|
"and {0} more",
|
|
1019
|
+
"Stop",
|
|
1021
1020
|
"saved msg",
|
|
1022
1021
|
"more row-more",
|
|
1023
1022
|
"callout info",
|
package/model_breaker.py
CHANGED
|
@@ -13,7 +13,7 @@ import urllib.error
|
|
|
13
13
|
FAILURES_TO_OPEN = 3
|
|
14
14
|
PAUSE_S = 30
|
|
15
15
|
MAX_IN_FLIGHT = 6
|
|
16
|
-
|
|
16
|
+
BUSY_CODES = (429, 503)
|
|
17
17
|
_lock = threading.Lock()
|
|
18
18
|
_failures = {}
|
|
19
19
|
_open_until = {}
|
|
@@ -35,12 +35,13 @@ def guard(model):
|
|
|
35
35
|
"HTTP 429/503 answers from the gateway; no request was sent.")
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
def note(model, failure=None):
|
|
39
|
-
"""Records the outcome of a request: a success closes the circuit,
|
|
40
|
-
failure changes nothing. After the pause the first failure opens it again.
|
|
38
|
+
def note(model, failure=None, codes=BUSY_CODES):
|
|
39
|
+
"""Records the outcome of a request: a success closes the circuit, an HTTP status in `codes` counts toward opening
|
|
40
|
+
it, any other failure changes nothing. After the pause the first failure opens it again. `codes` is the gateway's
|
|
41
|
+
busy answers by default; the embeddings home adds its 500, an inference that fails until the home recovers."""
|
|
41
42
|
if not model:
|
|
42
43
|
return
|
|
43
|
-
busy = isinstance(failure, urllib.error.HTTPError) and failure.code in
|
|
44
|
+
busy = isinstance(failure, urllib.error.HTTPError) and failure.code in codes
|
|
44
45
|
with _lock:
|
|
45
46
|
if failure is None:
|
|
46
47
|
_failures[model] = 0
|
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.82",
|
|
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": "Allan Santos",
|
|
7
7
|
"homepage": "https://allansantos-dv.github.io/smart-tool/",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
package/process_limits.py
CHANGED
|
@@ -47,13 +47,27 @@ def kill_on_close_job(job_memory_bytes=None):
|
|
|
47
47
|
return kernel32, job
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
def
|
|
50
|
+
def file_tail(path, limit=STDERR_TAIL_BYTES):
|
|
51
|
+
"""The last `limit` bytes of the file at `path`, decoded and stripped. Raises OSError when it cannot be read."""
|
|
51
52
|
with open(path, "rb") as stream:
|
|
52
53
|
stream.seek(0, os.SEEK_END)
|
|
53
|
-
stream.seek(max(0, stream.tell() -
|
|
54
|
+
stream.seek(max(0, stream.tell() - limit))
|
|
54
55
|
return stream.read().decode("utf-8", "replace").strip()
|
|
55
56
|
|
|
56
57
|
|
|
58
|
+
def bind_to_kill_on_close_job(job, proc):
|
|
59
|
+
"""Puts `proc` in the kill-on-close job `job` ((kernel32, handle) from `kill_on_close_job`; created when None) and
|
|
60
|
+
returns the job. Raises OSError when the system refuses: an unbound child would outlive its owner."""
|
|
61
|
+
import ctypes
|
|
62
|
+
from ctypes import wintypes
|
|
63
|
+
if job is None:
|
|
64
|
+
job = kill_on_close_job()
|
|
65
|
+
kernel32, handle = job
|
|
66
|
+
if not kernel32.AssignProcessToJobObject(wintypes.HANDLE(handle), wintypes.HANDLE(proc._handle)):
|
|
67
|
+
raise ctypes.WinError(ctypes.get_last_error())
|
|
68
|
+
return job
|
|
69
|
+
|
|
70
|
+
|
|
57
71
|
def run_capped(argv, workdir, timeout, memory_bytes):
|
|
58
72
|
"""Runs `argv` in `workdir` with no window, no stdin, its output discarded and its error stream kept in a file there.
|
|
59
73
|
At the deadline the whole process tree is killed; a process that passes `memory_bytes` is killed by the system
|
|
@@ -68,7 +82,7 @@ def run_capped(argv, workdir, timeout, memory_bytes):
|
|
|
68
82
|
returncode = _run_in_job(argv, workdir, timeout, memory_bytes, errors)
|
|
69
83
|
else:
|
|
70
84
|
returncode = _run_in_group(argv, workdir, timeout, memory_bytes, errors)
|
|
71
|
-
return returncode,
|
|
85
|
+
return returncode, file_tail(error_path)
|
|
72
86
|
|
|
73
87
|
|
|
74
88
|
def _run_in_job(argv, workdir, timeout, memory_bytes, errors):
|
package/project_identity.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
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 stat
|
|
4
5
|
import time
|
|
5
6
|
|
|
7
|
+
_REPARSE_POINT = getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400)
|
|
6
8
|
RESOLVED_TTL_S = 3
|
|
7
9
|
RESOLVED_MAX = 4096
|
|
8
10
|
_resolved = {}
|
|
@@ -43,10 +45,26 @@ def legacy_ids(root):
|
|
|
43
45
|
for path in variants))
|
|
44
46
|
|
|
45
47
|
|
|
48
|
+
def _canonical_file(candidate):
|
|
49
|
+
"""canonical_root of a file inside a folder: the folder's resolution (cached like a root) plus the file name, when
|
|
50
|
+
one lstat shows the file itself is no symlink or junction. Every file of an index pass was resolved on its own,
|
|
51
|
+
the costliest call of a pass (one handle per file); this keeps one resolution per folder. A path that cannot be
|
|
52
|
+
read is resolved as before."""
|
|
53
|
+
absolute = os.path.abspath(os.path.expanduser(os.fspath(candidate)))
|
|
54
|
+
folder, name = os.path.split(absolute)
|
|
55
|
+
try:
|
|
56
|
+
leaf = os.lstat(absolute)
|
|
57
|
+
except OSError:
|
|
58
|
+
return canonical_root(absolute)
|
|
59
|
+
if not name or stat.S_ISLNK(leaf.st_mode) or getattr(leaf, "st_file_attributes", 0) & _REPARSE_POINT:
|
|
60
|
+
return canonical_root(absolute)
|
|
61
|
+
return os.path.normcase(os.path.join(display_root(folder), name))
|
|
62
|
+
|
|
63
|
+
|
|
46
64
|
def within_root(root, candidate):
|
|
47
65
|
try:
|
|
48
66
|
base = canonical_root(root)
|
|
49
|
-
resolved =
|
|
67
|
+
resolved = _canonical_file(candidate)
|
|
50
68
|
return os.path.commonpath([base, resolved]) == base
|
|
51
69
|
except (OSError, ValueError, TypeError):
|
|
52
70
|
return False
|