@allansantos-dev/smart-tool 0.9.79 → 0.9.81
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 +132 -0
- package/README.md +82 -18
- package/browser_control.py +10 -3
- package/browser_service_client.py +3 -32
- package/chromium_flags.py +30 -0
- package/client_hooks.py +10 -8
- package/code_health.py +182 -0
- package/code_impact.py +14 -2
- package/config.py +76 -8
- package/credential_check.py +38 -0
- package/dependency_notice.py +262 -14
- package/document_converter.py +221 -0
- package/document_read.py +100 -25
- package/document_text.py +194 -6
- package/dossier.py +71 -9
- package/embedding_cache.py +32 -8
- package/freshness.py +3 -2
- package/hook_decision.py +289 -18
- package/hooks/pretool_router.py +5 -4
- package/indexer.py +92 -3
- package/install.py +66 -27
- package/install_runtime.py +24 -10
- package/layer_check.py +57 -0
- package/link_check.py +69 -0
- package/local_embedder.py +115 -58
- package/locales/pt-BR.json +122 -18
- package/low_priority.py +41 -0
- package/model_client.py +1 -1
- package/model_health.py +24 -6
- package/notice_action.py +9 -0
- package/package.json +1 -1
- package/pr_comment.py +94 -0
- package/pr_impact.py +292 -8
- package/process_limits.py +108 -0
- package/project_admission.py +17 -15
- package/project_monitor.py +33 -7
- package/project_scheduler.py +12 -4
- package/removed_callers.py +146 -0
- package/scout.py +6 -2
- package/setup_audit.py +34 -1
- package/setup_ui.py +241 -41
- package/smart_tool_daemon.py +403 -91
- package/tray.py +29 -9
- package/update_check.py +18 -0
- package/update_splash.py +2 -2
- package/usage_meter.py +78 -0
- package/version.py +1 -1
- package/web_adapters/browser/crawl4ai_search.py +7 -3
- package/web_document_graph.py +12 -1
- package/web_fetch.py +24 -0
- package/web_search_health.py +31 -0
package/freshness.py
CHANGED
|
@@ -74,7 +74,8 @@ def npm_registry_evidence(package):
|
|
|
74
74
|
|
|
75
75
|
NPM_NAME_RE = re.compile(r"(?:@[a-z0-9._-]+/)?[a-z0-9._-]+")
|
|
76
76
|
NAME_RES = {"npm": NPM_NAME_RE, "PyPI": re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]*"),
|
|
77
|
-
"Go": re.compile(r"[A-Za-z0-9][A-Za-z0-9._~/-]*")
|
|
77
|
+
"Go": re.compile(r"[A-Za-z0-9][A-Za-z0-9._~/-]*"), "crates.io": re.compile(r"[A-Za-z0-9][A-Za-z0-9_-]*"),
|
|
78
|
+
"Maven": re.compile(r"[A-Za-z0-9_.-]+:[A-Za-z0-9_.-]+")}
|
|
78
79
|
|
|
79
80
|
|
|
80
81
|
def _osv_query(ecosystem, package, package_version):
|
|
@@ -120,7 +121,7 @@ def npm_advisories(package, package_version):
|
|
|
120
121
|
def advisories_batch(ecosystem, packages, timeout):
|
|
121
122
|
"""Advisory IDs for each (package, version) of `packages` from one OSV `querybatch` call.
|
|
122
123
|
|
|
123
|
-
Parameters: ecosystem (str) "npm", "PyPI" or "
|
|
124
|
+
Parameters: ecosystem (str) "npm", "PyPI", "Go", "crates.io" or "Maven"; packages (list of (name, version)); timeout (float) seconds for
|
|
124
125
|
the request.
|
|
125
126
|
Returns: list in the same order, each the sorted ID list or None when that package's answer is incomplete
|
|
126
127
|
(next_page_token) or malformed, so it is unverified and not clean.
|
package/hook_decision.py
CHANGED
|
@@ -12,6 +12,8 @@ consulta legível nunca é negada: é respondida no contexto (`_answer_in_place`
|
|
|
12
12
|
não altera permissão.
|
|
13
13
|
"""
|
|
14
14
|
import os
|
|
15
|
+
import subprocess
|
|
16
|
+
import time
|
|
15
17
|
import uuid
|
|
16
18
|
|
|
17
19
|
import affected_tests
|
|
@@ -19,17 +21,22 @@ import code_graph
|
|
|
19
21
|
import code_impact
|
|
20
22
|
import comment_check
|
|
21
23
|
import config
|
|
24
|
+
import credential_check
|
|
22
25
|
import dependency_notice
|
|
23
26
|
import doc_check
|
|
24
27
|
import doc_scope
|
|
28
|
+
import dossier
|
|
25
29
|
import duplicates
|
|
26
30
|
import edit_preview
|
|
27
31
|
import index_profile
|
|
28
32
|
import index_scope
|
|
33
|
+
import layer_check
|
|
34
|
+
import link_check
|
|
29
35
|
import endpoint_sync
|
|
30
36
|
import project_admission
|
|
31
37
|
import project_store
|
|
32
38
|
import redirect_rule
|
|
39
|
+
import removed_callers
|
|
33
40
|
import requester
|
|
34
41
|
import router
|
|
35
42
|
import tool_redirect
|
|
@@ -45,6 +52,13 @@ SINGLE_LANGUAGE_REASON = "redirected native search: its pattern is the only quer
|
|
|
45
52
|
EDIT_TOOLS = ("Edit", "MultiEdit", "Write", "apply_patch")
|
|
46
53
|
MAX_DOC_FINDINGS = 5
|
|
47
54
|
MAX_DUPLICATE_FINDINGS = 3
|
|
55
|
+
MAX_DECISION_FUNCTIONS = 3
|
|
56
|
+
DECISION_TIMEOUT_MS = 300
|
|
57
|
+
MAX_SHELL_REVIEW_FILES = 20
|
|
58
|
+
MAX_REVIEW_BYTES = 1_000_000
|
|
59
|
+
# FAT keeps two-second timestamps: a file the call wrote can look older than the call.
|
|
60
|
+
MTIME_SLACK_S = 2.0
|
|
61
|
+
SHELL_START_TTL_S = 3600
|
|
48
62
|
SMART_TOOL_SERVER = "smart-tool"
|
|
49
63
|
SMART_SEARCH_TOOL = f"mcp__{SMART_TOOL_SERVER}__smart_search"
|
|
50
64
|
REASON_MAX_CHARS = 200
|
|
@@ -283,15 +297,112 @@ def _duplicate_lines(findings):
|
|
|
283
297
|
+ " If the copy is intentional (moving the function, a deliberate fork), go ahead.")
|
|
284
298
|
|
|
285
299
|
|
|
286
|
-
def
|
|
300
|
+
def _layer_lines(findings):
|
|
301
|
+
lines = [f"`{item['function']}` ({rel}:{item['line']}) now calls `{item['called']}` ({item['path']}), a function of the "
|
|
302
|
+
f"higher layer {item['to']}, from layer {item['from']}" for rel, item in findings]
|
|
303
|
+
return ("Smart Tool layers: " + "; ".join(lines) + ". Calls go down the declared layers (.smart-tool/layers.json); "
|
|
304
|
+
"if this one is intended, go ahead.")
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _callers_lines(answers):
|
|
308
|
+
lines = []
|
|
309
|
+
for answer in answers:
|
|
310
|
+
resigned = "changed" in answer
|
|
311
|
+
for item in answer["findings"][:MAX_DOC_FINDINGS]:
|
|
312
|
+
same = f" A function named {item['name'].rpartition('.')[2]} also exists in {', '.join(item['also_defined_in'])}." \
|
|
313
|
+
if item["also_defined_in"] else ""
|
|
314
|
+
sites = removed_callers.sites_text(item)
|
|
315
|
+
lines.append(f"`{item['name']}` ({item['path']}) has a new signature after this edit and is called from {sites} "
|
|
316
|
+
f"outside this file: check that the callers pass what it takes now.{same}" if resigned else
|
|
317
|
+
f"`{item['name']}` ({item['path']}) is gone after this edit but is still called from "
|
|
318
|
+
f"{sites}: update the callers or keep the name.{same}")
|
|
319
|
+
if not answer["findings"]:
|
|
320
|
+
names = ", ".join(f"`{name}`" for name in answer["changed" if resigned else "removed"])
|
|
321
|
+
lines.append(f"no caller of {names}, whose signature changed, was found." if resigned else
|
|
322
|
+
f"no caller of the removed {names} was found.")
|
|
323
|
+
notes = " ".join(dict.fromkeys(answer["note"] for answer in answers if answer["note"]))
|
|
324
|
+
return "Smart Tool callers: " + " ".join(lines) + (f" {notes}" if notes else "")
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def _credential_lines(findings):
|
|
328
|
+
lines = [f"{rel}:{item['line']} looks like a {item['kind']}" for rel, item in findings[:MAX_DOC_FINDINGS]]
|
|
329
|
+
if len(findings) > MAX_DOC_FINDINGS:
|
|
330
|
+
lines.append(f"{len(findings) - MAX_DOC_FINDINGS} more line(s) in the same situation.")
|
|
331
|
+
return ("Smart Tool credentials: " + "; ".join(lines) + ". Keep it out of the file: read it from an environment "
|
|
332
|
+
"variable or a secret store. If it is a fake value for a test, go ahead.")
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def _link_lines(broken, unchecked):
|
|
336
|
+
"""The link notes: the links that point to nothing, and the ones that could not be checked with the reason."""
|
|
337
|
+
parts = []
|
|
338
|
+
if broken:
|
|
339
|
+
lines = [f"{rel}:{item['line']} points to `{item['link']}`, which does not exist" for rel, item in broken[:MAX_DOC_FINDINGS]]
|
|
340
|
+
if len(broken) > MAX_DOC_FINDINGS:
|
|
341
|
+
lines.append(f"{len(broken) - MAX_DOC_FINDINGS} more link(s) in the same situation")
|
|
342
|
+
parts.append("Smart Tool links: " + "; ".join(lines) + ". Fix the path, or create the file if it is coming next.")
|
|
343
|
+
if unchecked:
|
|
344
|
+
lines = [f"{rel}{':' + str(item['line']) if item['line'] else ''}{' `' + item['link'] + '`' if item['link'] else ''} "
|
|
345
|
+
f"({item['why']})" for rel, item in unchecked[:MAX_DOC_FINDINGS]]
|
|
346
|
+
if len(unchecked) > MAX_DOC_FINDINGS:
|
|
347
|
+
lines.append(f"{len(unchecked) - MAX_DOC_FINDINGS} more not checked")
|
|
348
|
+
parts.append("Smart Tool links not checked: " + "; ".join(lines) + ".")
|
|
349
|
+
return parts
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _decision_lines(found):
|
|
355
|
+
"""The note that names the decisions recorded about the touched functions, each note once.
|
|
356
|
+
|
|
357
|
+
Parameters: found (list of (rel, function name, hits)) the functions with a note and their `dossier.notes_about` hits.
|
|
358
|
+
Returns: str, empty when every note was already said.
|
|
359
|
+
"""
|
|
360
|
+
seen, sentences = set(), []
|
|
361
|
+
for rel, name, hits in found:
|
|
362
|
+
fresh = [hit for hit in hits if hit["id"] not in seen]
|
|
363
|
+
seen.update(hit["id"] for hit in fresh)
|
|
364
|
+
if fresh:
|
|
365
|
+
sentences.append(f"`{name}` ({rel}) is named in " + "; ".join(f"`{hit['id']}` {hit['title']}" for hit in fresh))
|
|
366
|
+
return "Smart Tool decision notes: " + ". ".join(sentences) + ". Read them before changing what they record." \
|
|
367
|
+
if sentences else ""
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def _decision_findings(root, rel, touched, failures):
|
|
371
|
+
"""The recorded decisions about the touched functions of one file, looked up in the project's notes (not its commit
|
|
372
|
+
messages: reading them runs a `git log` the budget cannot cut) within `DECISION_TIMEOUT_MS` each.
|
|
373
|
+
|
|
374
|
+
Parameters: root (str) project folder; rel (str) path of the file relative to it; touched (list) the functions
|
|
375
|
+
`doc_check.written` found touched; failures (list) where a search that fails or is cut adds its line.
|
|
376
|
+
Returns: list of (rel, function name, hits) for the functions that a note or commit message names.
|
|
377
|
+
"""
|
|
378
|
+
found = []
|
|
379
|
+
for function in touched:
|
|
380
|
+
try:
|
|
381
|
+
answer = dossier.notes_about(root, rel, function["name"], timeout_ms=DECISION_TIMEOUT_MS, history=False)
|
|
382
|
+
except (OSError, ValueError, RuntimeError) as exc:
|
|
383
|
+
failures.append(f"Smart Tool decision notes check skipped: {exc}")
|
|
384
|
+
break
|
|
385
|
+
line = f"Smart Tool decision notes search cut: {'; '.join(answer['reasons'])}"
|
|
386
|
+
if answer["reasons"] and line not in failures:
|
|
387
|
+
failures.append(line)
|
|
388
|
+
if answer["hits"]:
|
|
389
|
+
found.append((rel, function["name"], answer["hits"]))
|
|
390
|
+
return found
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def _edit_review(payload, doc_mode, duplicate_mode, dependency_mode, comment_mode="off", layer_mode="off",
|
|
394
|
+
link_mode="off", decision_mode="off"):
|
|
287
395
|
"""Checks of an edit before it runs. Docstrings follow doc_mode, in code files of registered projects only (tests,
|
|
288
396
|
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
|
|
289
397
|
documented function whose signature changed always gets a reminder. Comments the edit adds in the wrong place
|
|
290
398
|
(see comment_check) follow comment_mode in the same files: require denies, remind adds context. Copies of indexed
|
|
291
399
|
functions follow duplicate_mode and only add context, never deny. An edit of a package.json, requirements*.txt or go.mod follows dependency_mode: advisories
|
|
292
|
-
of the added or changed dependencies are added as context, never a denial.
|
|
400
|
+
of the added or changed dependencies are added as context, never a denial. Local links an edit of an HTML or Markdown
|
|
401
|
+
file adds or changes follow link_mode: missing targets and links that cannot be checked are added as context. The notes
|
|
402
|
+
and commit messages that name a touched function follow decision_mode and only add context."""
|
|
293
403
|
with code_graph.pending_scope():
|
|
294
|
-
return _edit_review_scoped(payload, doc_mode, duplicate_mode, dependency_mode, comment_mode
|
|
404
|
+
return _edit_review_scoped(payload, doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode, link_mode,
|
|
405
|
+
decision_mode)
|
|
295
406
|
|
|
296
407
|
|
|
297
408
|
def _dependency_part(before, after, ecosystem):
|
|
@@ -307,19 +418,35 @@ def _dependency_part(before, after, ecosystem):
|
|
|
307
418
|
return f"{dependency_notice.PREFIX}dependency check skipped: {exc}"
|
|
308
419
|
|
|
309
420
|
|
|
310
|
-
def
|
|
311
|
-
|
|
421
|
+
def _review_changes(changes, doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode="off", link_mode="off",
|
|
422
|
+
decision_mode="off"):
|
|
423
|
+
"""The checks of an edit over (path, before, after) triples: docstrings, comments, documentation anchors, copies,
|
|
424
|
+
dependency advisories, local links, layers and the decisions recorded about the touched functions, each following its mode.
|
|
425
|
+
|
|
426
|
+
Parameters: changes (iterable of (absolute path, text before or None, text after)); the modes (`off` skips a check).
|
|
427
|
+
Returns: tuple (message parts, doc findings, comment findings); the parts are empty when nothing is worth saying.
|
|
428
|
+
"""
|
|
312
429
|
doc_findings, scope_findings, duplicate_findings, failures, dependency_parts = [], [], [], [], []
|
|
313
|
-
comment_findings = []
|
|
314
|
-
|
|
430
|
+
comment_findings, layer_findings, credential_findings, caller_answers = [], [], [], []
|
|
431
|
+
broken_links, unchecked_links = [], []
|
|
432
|
+
decision_findings, decisions_asked = [], 0
|
|
433
|
+
for path, before, after in changes:
|
|
434
|
+
root = _project_of(path)
|
|
435
|
+
rel = os.path.relpath(os.path.realpath(path), os.path.realpath(root)).replace(os.sep, "/") if root else os.path.basename(path)
|
|
436
|
+
credential_findings += [(rel, item) for item in credential_check.review(before, after)]
|
|
437
|
+
if link_mode != "off":
|
|
438
|
+
try:
|
|
439
|
+
found = link_check.review(path, before, after)
|
|
440
|
+
broken_links += [(rel, item) for item in found["broken"]]
|
|
441
|
+
unchecked_links += [(rel, item) for item in found["unchecked"]]
|
|
442
|
+
except (ValueError, RuntimeError, OSError) as exc:
|
|
443
|
+
failures.append(f"Smart Tool links check skipped: {exc}")
|
|
315
444
|
ecosystem = dependency_notice.ecosystem_of(path)
|
|
316
445
|
if dependency_mode != "off" and ecosystem:
|
|
317
446
|
dependency_parts += [part for part in (_dependency_part(before, after, ecosystem),) if part]
|
|
318
447
|
continue
|
|
319
448
|
if not doc_check.supported(path):
|
|
320
449
|
continue
|
|
321
|
-
root = _project_of(path)
|
|
322
|
-
rel = os.path.relpath(os.path.realpath(path), os.path.realpath(root)).replace(os.sep, "/") if root else os.path.basename(path)
|
|
323
450
|
edited = doc_check.written(path, before, after)
|
|
324
451
|
profile = index_profile.current((index_scope.load_scope(root) or {}).get("profile")) if root else None
|
|
325
452
|
if doc_mode != "off" and root and index_profile.kind(rel, profile) == "code":
|
|
@@ -336,10 +463,151 @@ def _edit_review_scoped(payload, doc_mode, duplicate_mode, dependency_mode, comm
|
|
|
336
463
|
duplicate_findings += [(rel, item) for item in duplicates.on_write(root, path, before, after, edited)]
|
|
337
464
|
except (ValueError, RuntimeError) as exc:
|
|
338
465
|
failures.append(f"Smart Tool duplicates check skipped: {exc}")
|
|
466
|
+
if layer_mode != "off" and root and edited["touched"]:
|
|
467
|
+
try:
|
|
468
|
+
layer_findings += [(rel, item) for item in layer_check.review(root, rel, before, after, edited)]
|
|
469
|
+
except (ValueError, RuntimeError, OSError) as exc:
|
|
470
|
+
failures.append(f"Smart Tool layers check skipped: {exc}")
|
|
471
|
+
if decision_mode != "off" and root and index_profile.kind(rel, profile) == "code":
|
|
472
|
+
asked = edited["touched"][:MAX_DECISION_FUNCTIONS - decisions_asked]
|
|
473
|
+
decisions_asked += len(asked)
|
|
474
|
+
decision_findings += _decision_findings(root, rel, asked, failures)
|
|
475
|
+
if root:
|
|
476
|
+
try:
|
|
477
|
+
for review, key in ((removed_callers.review, "removed"), (removed_callers.signature_review, "changed")):
|
|
478
|
+
answer = review(root, rel, before, after)
|
|
479
|
+
if answer["findings"] or answer[key] and answer["note"]:
|
|
480
|
+
caller_answers.append(answer)
|
|
481
|
+
except (ValueError, RuntimeError, OSError) as exc:
|
|
482
|
+
failures.append(f"Smart Tool callers check skipped: {exc}")
|
|
339
483
|
parts = ([_doc_lines(doc_findings)] if doc_findings else []) + \
|
|
340
484
|
([_comment_lines(comment_findings)] if comment_findings else []) + \
|
|
341
485
|
([_scope_lines(scope_findings)] if scope_findings else []) + \
|
|
342
|
-
([_duplicate_lines(duplicate_findings)] if duplicate_findings else []) +
|
|
486
|
+
([_duplicate_lines(duplicate_findings)] if duplicate_findings else []) + \
|
|
487
|
+
([_layer_lines(layer_findings)] if layer_findings else []) + \
|
|
488
|
+
([_decision_lines(decision_findings)] if decision_findings else []) + \
|
|
489
|
+
([_callers_lines(caller_answers)] if caller_answers else []) + \
|
|
490
|
+
([_credential_lines(credential_findings)] if credential_findings else []) + \
|
|
491
|
+
_link_lines(broken_links, unchecked_links) + failures + dependency_parts
|
|
492
|
+
return parts, doc_findings, comment_findings
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def review_diff(changes, cfg):
|
|
496
|
+
"""The notes the edit hook would have added for changes made without it (a commit, a patch applied by hand).
|
|
497
|
+
|
|
498
|
+
Parameters: changes (iterable of (absolute path, text before or None, text after)); cfg (dict) the loaded config.
|
|
499
|
+
Returns: str, the hook's message for the whole set; empty when every check is quiet or off.
|
|
500
|
+
"""
|
|
501
|
+
modes = (config.doc_mode(cfg), config.duplicate_mode(cfg), config.dependency_mode(cfg), config.comment_mode(cfg),
|
|
502
|
+
config.layer_mode(cfg), config.link_mode(cfg), config.decision_mode(cfg))
|
|
503
|
+
with code_graph.pending_scope():
|
|
504
|
+
return " ".join(_review_changes(changes, *modes)[0])
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def file_changes(root, base, rels):
|
|
508
|
+
"""The (path, text before, text after) triples `review_diff` takes, for the text files among `rels`.
|
|
509
|
+
|
|
510
|
+
Parameters: root (str) folder the paths are relative to; base (str) git revision holding the text before; rels (iterable
|
|
511
|
+
of str) forward-slash paths relative to root.
|
|
512
|
+
Returns: list of (absolute path, text at base or None for a file new since base, current text); a file that cannot be
|
|
513
|
+
read as UTF-8, is over MAX_REVIEW_BYTES or is gone is left out.
|
|
514
|
+
"""
|
|
515
|
+
triples = []
|
|
516
|
+
for rel in rels:
|
|
517
|
+
full = os.path.join(root, *rel.split("/"))
|
|
518
|
+
try:
|
|
519
|
+
if os.path.getsize(full) > MAX_REVIEW_BYTES:
|
|
520
|
+
continue
|
|
521
|
+
with open(full, encoding="utf-8") as stream:
|
|
522
|
+
after = stream.read()
|
|
523
|
+
except (OSError, UnicodeDecodeError):
|
|
524
|
+
continue
|
|
525
|
+
try:
|
|
526
|
+
before = affected_tests._git(root, "show", f"{base}:./{rel}")
|
|
527
|
+
except ValueError:
|
|
528
|
+
before = None
|
|
529
|
+
triples.append((full, before, after))
|
|
530
|
+
return triples
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
_SHELL_STARTS = {}
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def note_shell_start(payload):
|
|
537
|
+
"""Records when a shell call (Bash, PowerShell) starts, by its tool_use_id: the PostToolUse payload carries no start
|
|
538
|
+
time and no snapshot, so this is what tells the files the call wrote from the ones that were already changed."""
|
|
539
|
+
if not isinstance(payload, dict) or payload.get("tool_name") not in tool_redirect.NO_OP or not payload.get("tool_use_id"):
|
|
540
|
+
return
|
|
541
|
+
now = time.time()
|
|
542
|
+
for old in [key for key, at in _SHELL_STARTS.items() if now - at > SHELL_START_TTL_S]:
|
|
543
|
+
_SHELL_STARTS.pop(old, None)
|
|
544
|
+
_SHELL_STARTS[payload["tool_use_id"]] = now
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def _in_git_repository(folder):
|
|
548
|
+
"""Whether folder or one of its parents holds a `.git` entry (a folder, or a file for a worktree or submodule)."""
|
|
549
|
+
folder = os.path.abspath(folder)
|
|
550
|
+
while True:
|
|
551
|
+
if os.path.exists(os.path.join(folder, ".git")):
|
|
552
|
+
return True
|
|
553
|
+
parent = os.path.dirname(folder)
|
|
554
|
+
if parent == folder:
|
|
555
|
+
return False
|
|
556
|
+
folder = parent
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
def _shell_review(payload):
|
|
560
|
+
"""PostToolUse notes for the files a shell call wrote (`sed -i`, a redirection, `npm install`): the files git shows
|
|
561
|
+
changed since HEAD, tracked or not, whose modification time is not older than the call's start (see
|
|
562
|
+
`note_shell_start`), through the same checks as `review_diff`. A file the user or another tool changed before the
|
|
563
|
+
call is left out. Anything that stops the review, a git failure included, comes back as a note.
|
|
564
|
+
|
|
565
|
+
Parameters: payload (dict) the PostToolUse payload of the call.
|
|
566
|
+
Returns: the hook output, `{}` when there is nothing to say.
|
|
567
|
+
"""
|
|
568
|
+
started = _SHELL_STARTS.pop(payload["tool_use_id"], None)
|
|
569
|
+
cwd = payload.get("cwd")
|
|
570
|
+
cfg = config.load_config()
|
|
571
|
+
modes = (config.doc_mode(cfg), config.duplicate_mode(cfg), config.dependency_mode(cfg), config.comment_mode(cfg),
|
|
572
|
+
config.layer_mode(cfg), config.link_mode(cfg), config.decision_mode(cfg))
|
|
573
|
+
if set(modes) == {"off"}:
|
|
574
|
+
return {}
|
|
575
|
+
if not isinstance(cwd, str) or not os.path.isdir(cwd) or not _in_git_repository(cwd):
|
|
576
|
+
return {}
|
|
577
|
+
if started is None:
|
|
578
|
+
return _shell_note("the start of this call was not recorded, so the files it wrote cannot be told apart")
|
|
579
|
+
try:
|
|
580
|
+
changed = affected_tests._changes(cwd, "HEAD")
|
|
581
|
+
written = sorted(rel for rel, found in changed.items() if found["status"] != "deleted"
|
|
582
|
+
and _written_since(os.path.join(cwd, *rel.split("/")), started - MTIME_SLACK_S))
|
|
583
|
+
notes = review_diff(file_changes(cwd, "HEAD", written[:MAX_SHELL_REVIEW_FILES]), cfg)
|
|
584
|
+
except (OSError, ValueError, RuntimeError, subprocess.SubprocessError) as exc:
|
|
585
|
+
return _shell_note(str(exc))
|
|
586
|
+
if len(written) > MAX_SHELL_REVIEW_FILES:
|
|
587
|
+
notes += f" {len(written) - MAX_SHELL_REVIEW_FILES} more changed file(s) were not reviewed."
|
|
588
|
+
notes = notes.strip()
|
|
589
|
+
return {"hookSpecificOutput": {"hookEventName": "PostToolUse", "additionalContext": notes}} if notes else {}
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
def _written_since(path, moment):
|
|
593
|
+
"""Whether path was modified at or after moment (seconds since the epoch); a file that cannot be read was not."""
|
|
594
|
+
try:
|
|
595
|
+
return os.stat(path).st_mtime >= moment
|
|
596
|
+
except OSError:
|
|
597
|
+
return False
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def _shell_note(reason):
|
|
601
|
+
return {"hookSpecificOutput": {"hookEventName": "PostToolUse",
|
|
602
|
+
"additionalContext": f"Smart Tool shell review skipped: {reason}"}}
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
def _edit_review_scoped(payload, doc_mode, duplicate_mode, dependency_mode, comment_mode="off", layer_mode="off",
|
|
606
|
+
link_mode="off", decision_mode="off"):
|
|
607
|
+
"""`_edit_review` inside one `code_graph.pending_scope`, so the pending-changes scan runs once per review."""
|
|
608
|
+
parts, doc_findings, comment_findings = _review_changes(
|
|
609
|
+
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)
|
|
343
611
|
if not parts:
|
|
344
612
|
return {}
|
|
345
613
|
message = " ".join(parts)
|
|
@@ -354,19 +622,22 @@ def _edit_review_scoped(payload, doc_mode, duplicate_mode, dependency_mode, comm
|
|
|
354
622
|
|
|
355
623
|
def decide(payload, client, web_route, jobs):
|
|
356
624
|
"""Hook output for one PreToolUse payload: edits follow doc_mode (require | remind | off) and duplicate_mode
|
|
357
|
-
(warn | off) and dependency_mode (warn | off), the other tools follow hook_mode (redirect | advise | off). In redirect mode the Smart Tool call that
|
|
625
|
+
(warn | off) and dependency_mode (warn | off) and decision_mode (warn | off), and every edit review also notes credentials the edit writes, the other tools follow hook_mode (redirect | advise | off). In redirect mode the Smart Tool call that
|
|
358
626
|
replaces a redirected one starts in `jobs` under the call's tool_use_id (see `_redirect`); only a search whose
|
|
359
627
|
query cannot be built, or a client that sends no tool_use_id, is denied with the exact call to make, and never in a
|
|
360
628
|
subagent whose tool list has no Smart Tool MCP tool: it has nothing to switch to."""
|
|
361
629
|
cfg = config.load_config()
|
|
630
|
+
note_shell_start(payload)
|
|
362
631
|
if isinstance(payload, dict):
|
|
363
632
|
requester.note_origin(payload.get("session_id"), payload.get("cwd"))
|
|
364
633
|
if isinstance(payload, dict) and payload.get("tool_name") in EDIT_TOOLS:
|
|
365
|
-
doc_mode, duplicate_mode, dependency_mode, comment_mode = (
|
|
366
|
-
config.doc_mode(cfg), config.duplicate_mode(cfg), config.dependency_mode(cfg), config.comment_mode(cfg)
|
|
367
|
-
|
|
634
|
+
doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode, link_mode, decision_mode = (
|
|
635
|
+
config.doc_mode(cfg), config.duplicate_mode(cfg), config.dependency_mode(cfg), config.comment_mode(cfg),
|
|
636
|
+
config.layer_mode(cfg), config.link_mode(cfg), config.decision_mode(cfg))
|
|
637
|
+
if "off" == doc_mode == duplicate_mode == dependency_mode == comment_mode == layer_mode == link_mode == decision_mode:
|
|
368
638
|
return {}
|
|
369
|
-
return _edit_review(payload, doc_mode, duplicate_mode, dependency_mode, comment_mode
|
|
639
|
+
return _edit_review(payload, doc_mode, duplicate_mode, dependency_mode, comment_mode, layer_mode, link_mode,
|
|
640
|
+
decision_mode)
|
|
370
641
|
if isinstance(payload, dict) and str(payload.get("tool_name")).startswith(f"mcp__{SMART_TOOL_SERVER}__"):
|
|
371
642
|
if payload["tool_name"] == SMART_SEARCH_TOOL:
|
|
372
643
|
router.CLIENT.set(client)
|
|
@@ -472,8 +743,8 @@ def _log_served(client, payload):
|
|
|
472
743
|
|
|
473
744
|
def after(payload, client, jobs):
|
|
474
745
|
"""PostToolUse output: the call's own response with the Smart Tool answer started for its tool_use_id in the field
|
|
475
|
-
the model reads (`updatedToolOutput`)
|
|
476
|
-
|
|
746
|
+
the model reads (`updatedToolOutput`); for a shell call with no such answer, the notes of `_shell_review` on the files
|
|
747
|
+
it wrote; else `{}` (the native output stays)."""
|
|
477
748
|
if not isinstance(payload, dict) or not payload.get("tool_use_id"):
|
|
478
749
|
return {}
|
|
479
750
|
key, tool_name = payload["tool_use_id"], payload.get("tool_name")
|
|
@@ -481,7 +752,7 @@ def after(payload, client, jobs):
|
|
|
481
752
|
jobs.forget(key)
|
|
482
753
|
output = tool_redirect.output_for(tool_name, payload.get("tool_response"), text) if text else None
|
|
483
754
|
if output is None:
|
|
484
|
-
return {}
|
|
755
|
+
return _shell_review(payload) if tool_name in tool_redirect.NO_OP else {}
|
|
485
756
|
_log_served(client, payload)
|
|
486
757
|
return {"hookSpecificOutput": {"hookEventName": "PostToolUse", "updatedToolOutput": output}}
|
|
487
758
|
|
package/hooks/pretool_router.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""Hook
|
|
3
|
-
/hooks/
|
|
4
|
-
hook `type: "http"`. Falha aberto: daemon fora do ar ou erro = saída neutra `{}`, nunca `allow`.
|
|
2
|
+
"""Hook de comando (Codex): cliente fino que repassa o payload ao daemon do Smart Tool (POST /hooks/pretool, ou
|
|
3
|
+
/hooks/posttool com `--post`), onde a decisão roda já aquecida (`hook_decision.py`). O Claude Code chama as mesmas rotas
|
|
4
|
+
direto, como hook `type: "http"`. Falha aberto: daemon fora do ar ou erro = saída neutra `{}`, nunca `allow`.
|
|
5
5
|
"""
|
|
6
6
|
import json
|
|
7
7
|
import os
|
|
@@ -32,7 +32,8 @@ def main():
|
|
|
32
32
|
print(json.dumps({"systemMessage": "Smart Tool: daemon subindo, roteamento pulado nesta chamada."},
|
|
33
33
|
ensure_ascii=False))
|
|
34
34
|
return
|
|
35
|
-
|
|
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()})}",
|
|
36
37
|
data=raw, headers={"Content-Type": "application/json"}, method="POST")
|
|
37
38
|
with urllib.request.urlopen(request, timeout=TIMEOUT_S) as response:
|
|
38
39
|
output = json.loads(response.read() or b"{}")
|
package/indexer.py
CHANGED
|
@@ -226,6 +226,31 @@ def existing_db_path(root):
|
|
|
226
226
|
return None
|
|
227
227
|
|
|
228
228
|
|
|
229
|
+
def partial_index(root, model_id):
|
|
230
|
+
"""`(path, indexed_files)` of the generation being built for `model_id` that already holds chunks: the newest
|
|
231
|
+
`.building-*` file of the project's current view. `(None, set())` when no build is under way or it holds nothing yet.
|
|
232
|
+
A search may read it while the build commits file by file."""
|
|
233
|
+
prefix = os.path.basename(db_path(root))[:-len(".sqlite3")] + ".building-"
|
|
234
|
+
try:
|
|
235
|
+
names = [name for name in os.listdir(INDEX_DIR) if name.startswith(prefix) and name.endswith(".sqlite3")]
|
|
236
|
+
except OSError:
|
|
237
|
+
return None, set()
|
|
238
|
+
for path in sorted((os.path.join(INDEX_DIR, name) for name in names), key=os.path.getmtime, reverse=True):
|
|
239
|
+
try:
|
|
240
|
+
conn = _readonly(path)
|
|
241
|
+
try:
|
|
242
|
+
if _metadata(conn).get("model_id") != model_id:
|
|
243
|
+
continue
|
|
244
|
+
files = {row[0] for row in conn.execute("SELECT DISTINCT path FROM chunks")}
|
|
245
|
+
finally:
|
|
246
|
+
conn.close()
|
|
247
|
+
except (OSError, sqlite3.Error):
|
|
248
|
+
continue
|
|
249
|
+
if files:
|
|
250
|
+
return path, files
|
|
251
|
+
return None, set()
|
|
252
|
+
|
|
253
|
+
|
|
229
254
|
def _readonly(path):
|
|
230
255
|
"""Read-only connection to an index. A rollback journal left by a crashed writer (a hot journal) cannot be rolled
|
|
231
256
|
back by a read-only connection, which then fails every read with "attempt to write a readonly database": the
|
|
@@ -469,6 +494,30 @@ def chunk_file(root, rel_path, max_lines=CHUNK_LINES, max_chars=CHUNK_MAX_CHARS,
|
|
|
469
494
|
return chunks
|
|
470
495
|
|
|
471
496
|
|
|
497
|
+
def indexed_lines(root, rel_path, start, end):
|
|
498
|
+
"""`[(line_number, text)]` of lines `start..end` of rel_path as the index stored them (the extracted text of a PDF
|
|
499
|
+
or Office file, whose lines are not lines of the file), or None when the index holds none of them. Reads the chunk
|
|
500
|
+
rows only: no extraction of the document."""
|
|
501
|
+
db_file = existing_db_path(root)
|
|
502
|
+
if not db_file:
|
|
503
|
+
return None
|
|
504
|
+
try:
|
|
505
|
+
conn = _readonly(db_file)
|
|
506
|
+
try:
|
|
507
|
+
rows = conn.execute("SELECT start_line,text FROM chunks WHERE path=? AND start_line<=? AND end_line>=? "
|
|
508
|
+
"ORDER BY start_line,id", (rel_path, end, start)).fetchall()
|
|
509
|
+
finally:
|
|
510
|
+
conn.close()
|
|
511
|
+
except sqlite3.Error:
|
|
512
|
+
return None
|
|
513
|
+
found = {}
|
|
514
|
+
for first, text in rows:
|
|
515
|
+
for offset, line in enumerate(text.splitlines()):
|
|
516
|
+
found.setdefault(first + offset, line)
|
|
517
|
+
wanted = [(number, found[number]) for number in range(max(1, start), end + 1) if number in found]
|
|
518
|
+
return wanted or None
|
|
519
|
+
|
|
520
|
+
|
|
472
521
|
def _pack_vector(vector):
|
|
473
522
|
validate_vector(vector)
|
|
474
523
|
try:
|
|
@@ -795,6 +844,8 @@ def _reindex(root, included_paths, embed_batch, chunk_lines=CHUNK_LINES, on_prog
|
|
|
795
844
|
check()
|
|
796
845
|
if building and not stats["failed"]:
|
|
797
846
|
atomic_io.atomic_replace(work_path, project_identity.state_path(target, INDEX_DIR))
|
|
847
|
+
if current.get("model_id") not in (None, model_id):
|
|
848
|
+
stats["dropped_vectors"] = _drop_other_models(root, model_id, target)
|
|
798
849
|
stats["ready"] = not bool(stats["failed"])
|
|
799
850
|
stats["path"] = target if stats["ready"] else work_path
|
|
800
851
|
if building or total or stats["removed"] or full_check:
|
|
@@ -834,6 +885,43 @@ def _trim_other_views(root, target):
|
|
|
834
885
|
conn.close()
|
|
835
886
|
|
|
836
887
|
|
|
888
|
+
def _project_files(root):
|
|
889
|
+
"""Index files of the project in INDEX_DIR (every Git view, the legacy keys, builds, the vector cache)."""
|
|
890
|
+
for key in dict.fromkeys([project_identity.project_id(root), *project_identity.legacy_ids(root)]):
|
|
891
|
+
pattern = re.compile(re.escape(key) + r'((?:\.v-[a-f0-9]{16})?(?:\.previous|\.building-[a-f0-9]+|\.vectors)?\.sqlite3)$')
|
|
892
|
+
for name in os.listdir(INDEX_DIR):
|
|
893
|
+
if pattern.fullmatch(name):
|
|
894
|
+
yield os.path.join(INDEX_DIR, name)
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
def _drop_other_models(root, model_id, target):
|
|
898
|
+
"""After the index of a folder switched to `model_id`, deletes the vectors of the other models: the rows of the
|
|
899
|
+
vector cache and the other index files of the project (other Git views, legacy keys, unfinished builds) that hold
|
|
900
|
+
another model, with their saved graph analysis. Returns how many cached vectors were deleted."""
|
|
901
|
+
dropped = embedding_cache.drop_other_models(root, INDEX_DIR, model_id)
|
|
902
|
+
for path in list(_project_files(root)):
|
|
903
|
+
if path.endswith(".vectors.sqlite3") or os.path.normcase(path) == os.path.normcase(target):
|
|
904
|
+
continue
|
|
905
|
+
try:
|
|
906
|
+
conn = _readonly(path)
|
|
907
|
+
try:
|
|
908
|
+
model = _metadata(conn).get("model_id")
|
|
909
|
+
finally:
|
|
910
|
+
conn.close()
|
|
911
|
+
except sqlite3.Error as exc:
|
|
912
|
+
_notice(f"index {os.path.basename(path)}: not checked for vectors of another model ({exc})")
|
|
913
|
+
continue
|
|
914
|
+
if model != model_id:
|
|
915
|
+
for stale in (path, graph_cache_path(path)):
|
|
916
|
+
try:
|
|
917
|
+
os.remove(stale)
|
|
918
|
+
except FileNotFoundError:
|
|
919
|
+
pass
|
|
920
|
+
except OSError as exc:
|
|
921
|
+
_notice(f"index {os.path.basename(stale)}: vectors of {model} not deleted now ({exc})")
|
|
922
|
+
return dropped
|
|
923
|
+
|
|
924
|
+
|
|
837
925
|
def _compact(conn):
|
|
838
926
|
"""VACUUM when the removals left at least COMPACT_MIN_FREE_BYTES and COMPACT_MIN_FREE_SHARE of the file free."""
|
|
839
927
|
page = conn.execute("PRAGMA page_size").fetchone()[0]
|
|
@@ -995,11 +1083,12 @@ def overlay_exact_keys(overlay, query):
|
|
|
995
1083
|
if score == 1.0}
|
|
996
1084
|
|
|
997
1085
|
|
|
998
|
-
def search(root, query_vector=None, top_k=20, query=None, model_id=None, include=None, overlay=None):
|
|
1086
|
+
def search(root, query_vector=None, top_k=20, query=None, model_id=None, include=None, overlay=None, path=None):
|
|
999
1087
|
"""Vector plus lexical retrieval by fusion of relative scores, without network. `overlay` is
|
|
1000
1088
|
`{"drop": paths, "chunks": overlay_chunks(...)}`: the indexed chunks of the `drop` paths (stale or removed
|
|
1001
|
-
files) are ignored and the overlay chunks, read from disk, take part by keyword instead.
|
|
1002
|
-
|
|
1089
|
+
files) are ignored and the overlay chunks, read from disk, take part by keyword instead. `path` searches that
|
|
1090
|
+
index file (a `partial_index`) instead of the project's ready one."""
|
|
1091
|
+
path = path or existing_db_path(root)
|
|
1003
1092
|
if not path:
|
|
1004
1093
|
if not (overlay and overlay["chunks"] and query):
|
|
1005
1094
|
return []
|