@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/code_health.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Questions about the shape of a project that the code graph and the git history answer without writing documentation:
|
|
2
|
+
which code files are the riskiest to change, which functions nothing calls, which imports form a cycle and which
|
|
3
|
+
comments sit where they should not.
|
|
4
|
+
|
|
5
|
+
The same tables the `docs/scope` pages hold (`doc_insights`), asked directly by `project_manage`; nothing is written.
|
|
6
|
+
"""
|
|
7
|
+
import code_graph
|
|
8
|
+
import comment_check
|
|
9
|
+
import doc_check
|
|
10
|
+
import doc_insights
|
|
11
|
+
import doc_scope
|
|
12
|
+
|
|
13
|
+
DEFAULT_LIMIT = 25
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _model(root, view_id):
|
|
17
|
+
model = doc_scope._Model(root, view_id)
|
|
18
|
+
if not model.graph.get("symbols"):
|
|
19
|
+
raise ValueError(f"The code graph of {root} is empty: index the project first (project_manage action=index).")
|
|
20
|
+
return model
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def hotspots(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
24
|
+
"""Code files ranked by the commits that touched them times their lines.
|
|
25
|
+
|
|
26
|
+
Parameters: root (str) project folder; view_id (str or None) index view; limit (int) rows returned.
|
|
27
|
+
Returns: dict {"files": [{"path", "commits", "lines", "score", "last_change"}], "total": int, "commits_read": int,
|
|
28
|
+
"note": str}; `note` is the reason no history could be read (not a git repository), otherwise "".
|
|
29
|
+
Raises: ValueError when the code graph is empty.
|
|
30
|
+
"""
|
|
31
|
+
model = _model(root, view_id)
|
|
32
|
+
commits, note = doc_insights.history(root)
|
|
33
|
+
rows = [] if note else doc_insights.hotspots(model, commits)
|
|
34
|
+
files = [{"path": path, "commits": count, "lines": lines, "score": count * lines, "last_change": doc_insights._date(last)}
|
|
35
|
+
for path, count, lines, last in rows[:limit]]
|
|
36
|
+
return {"files": files, "total": len(rows), "commits_read": len(commits), "note": note or ""}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def dead_code(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
40
|
+
"""Functions nothing calls and functions only tests call, from the static call graph.
|
|
41
|
+
|
|
42
|
+
Parameters: root (str) project folder; view_id (str or None) index view; limit (int) entries per list.
|
|
43
|
+
Returns: dict {"no_caller": [...], "only_tests": [...], "counts": {"no_caller", "only_tests"}, "note": str,
|
|
44
|
+
"graph_note": str}; each entry is {"name", "path", "line", "public"}; `graph_note` says what the analysis limits cut from
|
|
45
|
+
the graph, "" when they cut nothing. Candidates only: a call through a framework, a decorator registration,
|
|
46
|
+
reflection or a name built at run time is not seen.
|
|
47
|
+
Raises: ValueError when the code graph is empty.
|
|
48
|
+
"""
|
|
49
|
+
model = _model(root, view_id)
|
|
50
|
+
alone, tested = doc_insights.dead_code(model, model.entry_points())
|
|
51
|
+
|
|
52
|
+
def entry(anchor):
|
|
53
|
+
function = model.function_of[anchor]
|
|
54
|
+
return {"name": function["name"], "path": function["path"], "line": function["start"],
|
|
55
|
+
"public": bool(function["required"])}
|
|
56
|
+
|
|
57
|
+
return {"no_caller": [entry(a) for a in alone[:limit]], "only_tests": [entry(a) for a in tested[:limit]],
|
|
58
|
+
"counts": {"no_caller": len(alone), "only_tests": len(tested)},
|
|
59
|
+
"note": "Candidates from the static call graph (Python and Go only): read each one before deleting it.",
|
|
60
|
+
"graph_note": code_graph.left_out_sentence(model.graph) or ""}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def coupled_with(root, paths, view_id=None, limit=DEFAULT_LIMIT):
|
|
64
|
+
"""Files that usually change in the same commits as the given ones, left out of the given set.
|
|
65
|
+
|
|
66
|
+
Parameters: root (str) project folder; paths (list of str) project-relative changed files; view_id (str or None) index
|
|
67
|
+
view; limit (int) rows returned. Pairs where one file imports the other are skipped: the call graph already shows them.
|
|
68
|
+
Returns: dict {"files": [{"path", "with", "shared", "degree"}], "note": str}, strongest first, one row per partner;
|
|
69
|
+
degree is a percentage; `note` is the reason no history could be read, otherwise "".
|
|
70
|
+
Raises: ValueError when the code graph is empty.
|
|
71
|
+
"""
|
|
72
|
+
model = _model(root, view_id)
|
|
73
|
+
commits, note = doc_insights.history(root)
|
|
74
|
+
if note:
|
|
75
|
+
return {"files": [], "note": note}
|
|
76
|
+
changed, rows, seen = set(paths), [], set()
|
|
77
|
+
for first, second, shared, degree, linked in doc_insights.coupling(model, commits):
|
|
78
|
+
if linked or (first in changed) == (second in changed):
|
|
79
|
+
continue
|
|
80
|
+
partner, anchor = (second, first) if first in changed else (first, second)
|
|
81
|
+
if partner not in seen:
|
|
82
|
+
seen.add(partner)
|
|
83
|
+
rows.append({"path": partner, "with": anchor, "shared": shared, "degree": int(degree * 100)})
|
|
84
|
+
return {"files": rows[:limit], "note": ""}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _pop_component(stack, on_stack, node):
|
|
88
|
+
"""Members of the component rooted at `node`, taken off the Tarjan stack."""
|
|
89
|
+
group = []
|
|
90
|
+
member = None
|
|
91
|
+
while member != node:
|
|
92
|
+
member = stack.pop()
|
|
93
|
+
on_stack.discard(member)
|
|
94
|
+
group.append(member)
|
|
95
|
+
return sorted(group)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _components(edges):
|
|
99
|
+
"""Strongly connected components of size above one, and self-loops, of a directed graph (Tarjan, no recursion).
|
|
100
|
+
|
|
101
|
+
Parameters: edges (dict) node -> set of the nodes it points to.
|
|
102
|
+
Returns: list of sorted node lists.
|
|
103
|
+
"""
|
|
104
|
+
index, low, stack, on_stack, found = {}, {}, [], set(), []
|
|
105
|
+
for start in sorted(edges):
|
|
106
|
+
if start in index:
|
|
107
|
+
continue
|
|
108
|
+
index[start] = low[start] = len(index)
|
|
109
|
+
stack.append(start)
|
|
110
|
+
on_stack.add(start)
|
|
111
|
+
work = [(start, iter(sorted(edges[start])))]
|
|
112
|
+
while work:
|
|
113
|
+
node, children = work[-1]
|
|
114
|
+
descended = False
|
|
115
|
+
for child in children:
|
|
116
|
+
if child not in index:
|
|
117
|
+
index[child] = low[child] = len(index)
|
|
118
|
+
stack.append(child)
|
|
119
|
+
on_stack.add(child)
|
|
120
|
+
work.append((child, iter(sorted(edges[child]))))
|
|
121
|
+
descended = True
|
|
122
|
+
break
|
|
123
|
+
if child in on_stack:
|
|
124
|
+
low[node] = min(low[node], index[child])
|
|
125
|
+
if descended:
|
|
126
|
+
continue
|
|
127
|
+
work.pop()
|
|
128
|
+
if work:
|
|
129
|
+
parent = work[-1][0]
|
|
130
|
+
low[parent] = min(low[parent], low[node])
|
|
131
|
+
if low[node] == index[node]:
|
|
132
|
+
group = _pop_component(stack, on_stack, node)
|
|
133
|
+
if len(group) > 1 or node in edges[node]:
|
|
134
|
+
found.append(group)
|
|
135
|
+
return found
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def import_cycles(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
139
|
+
"""Groups of files that import each other, directly or through a chain.
|
|
140
|
+
|
|
141
|
+
Parameters: root (str) project folder; view_id (str or None) index view; limit (int) cycles returned.
|
|
142
|
+
Returns: dict {"cycles": [{"files": [...], "size": int}], "total": int, "files_in_cycles": int, "note": str}, the
|
|
143
|
+
largest cycle first; only imports the graph resolved to a file of the project count.
|
|
144
|
+
Raises: ValueError when the code graph is empty.
|
|
145
|
+
"""
|
|
146
|
+
data = code_graph.build_current(root, view_id, shared=True)
|
|
147
|
+
if not data.get("symbols") and not data.get("dependencies"):
|
|
148
|
+
raise ValueError(f"The code graph of {root} is empty: index the project first (project_manage action=index).")
|
|
149
|
+
edges = {}
|
|
150
|
+
for dependency in data.get("dependencies") or []:
|
|
151
|
+
if dependency.get("kind") == "import" and dependency.get("resolution") == "resolved" and dependency.get("target"):
|
|
152
|
+
edges.setdefault(dependency["source"], set()).add(dependency["target"])
|
|
153
|
+
edges.setdefault(dependency["target"], set())
|
|
154
|
+
cycles = sorted(_components(edges), key=lambda files: (-len(files), files))
|
|
155
|
+
note = code_graph.left_out_sentence(data) if data.get("truncated") else ""
|
|
156
|
+
return {"cycles": [{"files": files, "size": len(files)} for files in cycles[:limit]], "total": len(cycles),
|
|
157
|
+
"files_in_cycles": sum(len(files) for files in cycles), "note": note}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def comments(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
161
|
+
"""Comments in the wrong place across the code files of the project: commented-out code, history tags, loose
|
|
162
|
+
documentation and long inline blocks (`comment_check`), for files written before the edit hook existed.
|
|
163
|
+
|
|
164
|
+
Parameters: root (str) project folder; view_id (str or None) index view; limit (int) findings returned.
|
|
165
|
+
Returns: dict {"findings": [{"path", "line", "issue", "name", "text"}], "total": int, "by_issue": {issue: count},
|
|
166
|
+
"files_checked": int}.
|
|
167
|
+
Raises: ValueError when the code graph is empty.
|
|
168
|
+
"""
|
|
169
|
+
model = _model(root, view_id)
|
|
170
|
+
findings, checked = [], 0
|
|
171
|
+
for path in model.files():
|
|
172
|
+
if model.kind_of(path) != "code" or not doc_check.supported(path):
|
|
173
|
+
continue
|
|
174
|
+
text = model.read(path)
|
|
175
|
+
if not text:
|
|
176
|
+
continue
|
|
177
|
+
checked += 1
|
|
178
|
+
findings += [{"path": path, **item} for item in comment_check.review(path, None, text)]
|
|
179
|
+
by_issue = {}
|
|
180
|
+
for item in findings:
|
|
181
|
+
by_issue[item["issue"]] = by_issue.get(item["issue"], 0) + 1
|
|
182
|
+
return {"findings": findings[:limit], "total": len(findings), "by_issue": by_issue, "files_checked": checked}
|
package/code_impact.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""What changing a function touches, from the indexed code graph: where it is defined, who calls it, what it calls,
|
|
2
2
|
which tests reach it through static calls and which files import its module, each with the first line of its
|
|
3
3
|
docstring. Answers the question an agent asks before an edit without a chain of searches and file reads."""
|
|
4
|
+
import difflib
|
|
4
5
|
import os
|
|
5
6
|
import re
|
|
6
7
|
from collections import defaultdict
|
|
@@ -23,6 +24,15 @@ def _matches(symbols, name):
|
|
|
23
24
|
return exact or [s for s in pool if s["name"].split(".")[-1] == wanted]
|
|
24
25
|
|
|
25
26
|
|
|
27
|
+
def _closest_names(symbols, name, limit=3):
|
|
28
|
+
"""Up to `limit` indexed function or class names that resemble `name` (case ignored), best first."""
|
|
29
|
+
wanted = name.strip().rpartition("::")[2].casefold()
|
|
30
|
+
by_lower = {}
|
|
31
|
+
for symbol in symbols:
|
|
32
|
+
by_lower.setdefault(symbol["name"].split(".")[-1].casefold(), symbol["name"].split(".")[-1])
|
|
33
|
+
return [by_lower[key] for key in difflib.get_close_matches(wanted, list(by_lower), n=limit, cutoff=0.6)]
|
|
34
|
+
|
|
35
|
+
|
|
26
36
|
def _site(by_id, symbol_id, line, of=None):
|
|
27
37
|
symbol = by_id.get(symbol_id) or {}
|
|
28
38
|
site = {"id": symbol_id, "function": symbol.get("name", symbol_id), "path": symbol.get("path"), "line": line}
|
|
@@ -85,7 +95,9 @@ def impact(root, symbol, view_id=None, depth=3, limit=30):
|
|
|
85
95
|
diagnostics = [d.get("reason") for d in data.get("diagnostics") or [] if d.get("reason")]
|
|
86
96
|
if not found:
|
|
87
97
|
cause = f" Analysis problems: {'; '.join(diagnostics)}" if diagnostics else ""
|
|
88
|
-
|
|
98
|
+
close = _closest_names(data.get("symbols") or [], symbol)
|
|
99
|
+
hint = f" Closest names: {', '.join(close)}." if close else ""
|
|
100
|
+
raise ValueError(f"No function or class named {symbol!r} in the indexed view; check the name or reindex.{hint}{cause}")
|
|
89
101
|
profile = index_profile.current((index_scope.load_scope(root) or {}).get("profile"))
|
|
90
102
|
by_id = {s["id"]: s for s in data["symbols"]}
|
|
91
103
|
callers, callees = defaultdict(list), defaultdict(list)
|
|
@@ -115,7 +127,7 @@ def impact(root, symbol, view_id=None, depth=3, limit=30):
|
|
|
115
127
|
outgoing = [_site(by_id, c["target"], c["line"], label.get(t)) for t in targets for c in callees[t]
|
|
116
128
|
if c["target"] not in targets]
|
|
117
129
|
files = {s["path"] for s in found}
|
|
118
|
-
importers = sorted({d["source"] for d in data
|
|
130
|
+
importers = sorted({d["source"] for d in data.get("dependencies") or [] if d.get("target") in files and d["source"] not in files})
|
|
119
131
|
ordered_tests = sorted(tests.values(), key=lambda t: (t["hops"], t["path"], t["line"]))
|
|
120
132
|
shown = direct[:limit] + outgoing[:limit] + ordered_tests[:limit]
|
|
121
133
|
_name_test_callbacks(root, by_id, shown)
|
package/config.py
CHANGED
|
@@ -40,12 +40,18 @@ DEFAULT_CONFIG = {
|
|
|
40
40
|
"research_max_iterations": 3,
|
|
41
41
|
"quick_search_timeout_s": 120,
|
|
42
42
|
"site_memory_similarity_threshold": 0.35,
|
|
43
|
-
"
|
|
43
|
+
"embedder": "home",
|
|
44
44
|
"hook_mode": "redirect",
|
|
45
45
|
"doc_mode": "require",
|
|
46
46
|
"comment_mode": "require",
|
|
47
47
|
"duplicate_mode": "warn",
|
|
48
48
|
"dependency_mode": "warn",
|
|
49
|
+
"layer_mode": "warn",
|
|
50
|
+
"link_mode": "warn",
|
|
51
|
+
"pr_comment": "off",
|
|
52
|
+
"decision_mode": "warn",
|
|
53
|
+
"document_converter": "off",
|
|
54
|
+
"document_converter_python": "",
|
|
49
55
|
"docs_extras": {key: True for key in DOCS_EXTRAS},
|
|
50
56
|
"auto_update": True,
|
|
51
57
|
"language": "",
|
|
@@ -60,9 +66,10 @@ DEFAULT_CONFIG = {
|
|
|
60
66
|
},
|
|
61
67
|
}
|
|
62
68
|
|
|
63
|
-
#
|
|
64
|
-
#
|
|
65
|
-
|
|
69
|
+
# Embedder of the project indexes: home = the embeddings home daemon of this machine (casa-de-embeddings), whose rerank
|
|
70
|
+
# connector forwards to the host configured in it; gateway = the gateway's embedding_model and rerank_model, only when
|
|
71
|
+
# the user chooses it. Neither replaces the other when it fails.
|
|
72
|
+
EMBEDDERS = ("home", "gateway")
|
|
66
73
|
# PreToolUse hook: redirect = deny the native tool and point to Smart Tool; advise = let it run and add the tip to the
|
|
67
74
|
# agent's context; off = no routing at all.
|
|
68
75
|
HOOK_MODES = ("redirect", "advise", "off")
|
|
@@ -79,6 +86,20 @@ DUPLICATE_MODES = ("warn", "off")
|
|
|
79
86
|
# Edit hook on package.json, requirements*.txt and go.mod: warn = let the edit run and list the added or changed dependencies that have OSV advisories;
|
|
80
87
|
# off = say nothing and make no network call.
|
|
81
88
|
DEPENDENCY_MODES = ("warn", "off")
|
|
89
|
+
LAYER_MODES = ("warn", "off")
|
|
90
|
+
# Edit hook on HTML and Markdown files: warn = let the edit run and name the local links it adds or changes that point to a
|
|
91
|
+
# file that does not exist, and the ones that cannot be checked; off = say nothing. Never blocks.
|
|
92
|
+
LINK_MODES = ("warn", "off")
|
|
93
|
+
# Posting the pr_impact report as a comment on a pull request through the gh CLI, a write to GitHub with the user's own
|
|
94
|
+
# login: off = never; on = project_manage action=pr_impact_comment may post. Only the user turns it on, in the setup screen.
|
|
95
|
+
PR_COMMENT_MODES = ("off", "on")
|
|
96
|
+
# Edit hook on the functions an edit touches: warn = let the edit run and name the notes and commit messages of the project
|
|
97
|
+
# that record a decision about them; off = say nothing. Never blocks.
|
|
98
|
+
DECISION_MODES = ("warn", "off")
|
|
99
|
+
# External converter read_document falls back to for a file type or a scanned PDF its native reader refuses: off = never;
|
|
100
|
+
# auto = the first of kreuzberg and docling found installed; or one of them by name. The engine is looked for in the
|
|
101
|
+
# interpreter named by `document_converter_python` (empty = the one Smart Tool runs on).
|
|
102
|
+
DOCUMENT_CONVERTERS = ("off", "auto", "kreuzberg", "docling")
|
|
82
103
|
# Router model reviewing, in the background, searches the deterministic redirect rule let run: on = propose block
|
|
83
104
|
# patterns for the user to accept in the setup screen; off = no review.
|
|
84
105
|
PATTERN_PROPOSAL_MODES = ("on", "off")
|
|
@@ -118,6 +139,49 @@ def dependency_mode(cfg=None):
|
|
|
118
139
|
return value
|
|
119
140
|
|
|
120
141
|
|
|
142
|
+
def layer_mode(cfg=None):
|
|
143
|
+
value = (cfg if cfg is not None else load_config()).get("layer_mode") or DEFAULT_CONFIG["layer_mode"]
|
|
144
|
+
if value not in LAYER_MODES:
|
|
145
|
+
raise ValueError(f"Invalid layer_mode ({value!r}) in {CONFIG_PATH}: use warn or off.")
|
|
146
|
+
return value
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def link_mode(cfg=None):
|
|
150
|
+
value = (cfg if cfg is not None else load_config()).get("link_mode") or DEFAULT_CONFIG["link_mode"]
|
|
151
|
+
if value not in LINK_MODES:
|
|
152
|
+
raise ValueError(f"Invalid link_mode ({value!r}) in {CONFIG_PATH}: use warn or off.")
|
|
153
|
+
return value
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def pr_comment(cfg=None):
|
|
157
|
+
"""The user's consent to post the impact report on a pull request: `off` (default) or `on`; any other value fails loudly."""
|
|
158
|
+
value = (cfg if cfg is not None else load_config()).get("pr_comment") or DEFAULT_CONFIG["pr_comment"]
|
|
159
|
+
if value not in PR_COMMENT_MODES:
|
|
160
|
+
raise ValueError(f"Invalid pr_comment ({value!r}) in {CONFIG_PATH}: use off or on.")
|
|
161
|
+
return value
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def decision_mode(cfg=None):
|
|
165
|
+
"""The edit hook's decision-notes mode, `warn` or `off`; raises ValueError for any other value in the config file."""
|
|
166
|
+
value = (cfg if cfg is not None else load_config()).get("decision_mode") or DEFAULT_CONFIG["decision_mode"]
|
|
167
|
+
if value not in DECISION_MODES:
|
|
168
|
+
raise ValueError(f"Invalid decision_mode ({value!r}) in {CONFIG_PATH}: use warn or off.")
|
|
169
|
+
return value
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def document_converter(cfg=None):
|
|
173
|
+
"""The external converter setting: `{"mode": one of DOCUMENT_CONVERTERS, "python": interpreter path or ""}`. An
|
|
174
|
+
unknown mode or a python that is not text fails loudly."""
|
|
175
|
+
stored = cfg if cfg is not None else load_config()
|
|
176
|
+
mode = stored.get("document_converter") or DEFAULT_CONFIG["document_converter"]
|
|
177
|
+
python = stored.get("document_converter_python") or ""
|
|
178
|
+
if mode not in DOCUMENT_CONVERTERS:
|
|
179
|
+
raise ValueError(f"Invalid document_converter ({mode!r}) in {CONFIG_PATH}: use off, auto, kreuzberg or docling.")
|
|
180
|
+
if not isinstance(python, str):
|
|
181
|
+
raise ValueError(f"Invalid document_converter_python in {CONFIG_PATH}: a path to a Python interpreter.")
|
|
182
|
+
return {"mode": mode, "python": python}
|
|
183
|
+
|
|
184
|
+
|
|
121
185
|
def docs_extras(cfg=None):
|
|
122
186
|
"""Which extra documents the docs generator writes: every key of DOCS_EXTRAS, true unless the stored `docs_extras`
|
|
123
187
|
object sets it to false; an unknown key or a value that is not true or false fails loudly."""
|
|
@@ -183,10 +247,14 @@ def hook_mode(cfg=None):
|
|
|
183
247
|
return value
|
|
184
248
|
|
|
185
249
|
|
|
186
|
-
def
|
|
187
|
-
|
|
188
|
-
if
|
|
189
|
-
raise ValueError(f"
|
|
250
|
+
def embedder(cfg=None):
|
|
251
|
+
cfg = cfg if cfg is not None else load_config()
|
|
252
|
+
if "local_models" in cfg:
|
|
253
|
+
raise ValueError(f"{CONFIG_PATH} has the old 'local_models' setting: delete it and choose the embedder (home or "
|
|
254
|
+
"gateway) in the setup screen.")
|
|
255
|
+
value = cfg.get("embedder") or DEFAULT_CONFIG["embedder"]
|
|
256
|
+
if value not in EMBEDDERS:
|
|
257
|
+
raise ValueError(f"Invalid embedder ({value!r}) in {CONFIG_PATH}: use home or gateway.")
|
|
190
258
|
return value
|
|
191
259
|
|
|
192
260
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Credentials an edit writes, for the edit hook. Only the lines an edit adds or changes are read, and only for shapes that
|
|
2
|
+
cannot be anything else (a provider's token prefix with its fixed length, an AWS key pair line, a private-key header at
|
|
3
|
+
the start of a line): a false alarm costs more than a silence here, so a general "high entropy string" rule is not used.
|
|
4
|
+
Lines that say they are an example are skipped. The matched text is never repeated in a note."""
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
import doc_check
|
|
8
|
+
|
|
9
|
+
_PLACEHOLDER = re.compile(r"example|placeholder|your[_-]|dummy|fake|redacted|x{6,}|\*{4,}|<[^>]*>", re.I)
|
|
10
|
+
_PATTERNS = (
|
|
11
|
+
("private key", re.compile(r"^\s*-----BEGIN (?:[A-Z]+ )*PRIVATE KEY(?: BLOCK)?-----")),
|
|
12
|
+
("AWS access key id", re.compile(r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b")),
|
|
13
|
+
("AWS secret access key", re.compile(r"aws_secret_access_key[\"']?\s*[=:]\s*[\"']?[A-Za-z0-9/+=]{40}\b", re.I)),
|
|
14
|
+
("GitHub token", re.compile(r"\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{50,})")),
|
|
15
|
+
("Slack token", re.compile(r"\bxox[abprs]-[A-Za-z0-9-]{10,}")),
|
|
16
|
+
("Google API key", re.compile(r"\bAIza[0-9A-Za-z_-]{35}\b")),
|
|
17
|
+
("Stripe live key", re.compile(r"\b[sr]k_live_[0-9A-Za-z]{24,}")),
|
|
18
|
+
("Anthropic API key", re.compile(r"\bsk-ant-[A-Za-z0-9_-]{20,}")),
|
|
19
|
+
("npm token", re.compile(r"\bnpm_[A-Za-z0-9]{36}\b")),
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def review(before, after):
|
|
24
|
+
"""Credentials among the lines an edit adds or changes.
|
|
25
|
+
|
|
26
|
+
Parameters: before (str or None) text before the edit; after (str) text after it.
|
|
27
|
+
Returns: list of dicts with line (1-based) and kind, in line order, one per line; the secret itself is not included.
|
|
28
|
+
"""
|
|
29
|
+
lines = after.split("\n")
|
|
30
|
+
findings = []
|
|
31
|
+
for number in sorted(doc_check._touched_lines(before, after)):
|
|
32
|
+
if number > len(lines) or _PLACEHOLDER.search(lines[number - 1]):
|
|
33
|
+
continue
|
|
34
|
+
for kind, pattern in _PATTERNS:
|
|
35
|
+
if pattern.search(lines[number - 1]):
|
|
36
|
+
findings.append({"line": number, "kind": kind})
|
|
37
|
+
break
|
|
38
|
+
return findings
|