@allansantos-dev/smart-tool 0.9.80 → 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 +129 -0
- package/NOTICE +1 -1
- package/README.md +59 -14
- 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 +42 -41
- package/code_graph.py +56 -15
- package/code_graph_java.mjs +21 -12
- package/code_health.py +19 -12
- package/code_impact.py +1 -1
- package/codex_config.py +3 -0
- package/config.py +35 -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/dossier.py +40 -5
- 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 +337 -23
- package/hooks/pretool_router.py +36 -29
- package/hooks/turn_end.py +40 -0
- package/index_views.py +5 -0
- package/install.py +90 -36
- package/install_runtime.py +24 -10
- package/lazy_mcp_proxy.py +10 -9
- package/link_check.py +69 -0
- package/local_embedder.py +9 -2
- package/locales/pt-BR.json +72 -4
- package/model_breaker.py +6 -5
- package/model_client.py +1 -1
- package/model_health.py +24 -6
- package/notice_action.py +9 -0
- package/package.json +2 -2
- package/pr_comment.py +94 -0
- package/pr_impact.py +216 -35
- package/process_limits.py +17 -3
- package/project_admission.py +17 -15
- package/project_identity.py +19 -1
- package/project_scan.py +98 -0
- package/removed_callers.py +146 -0
- package/scout_access.py +48 -0
- package/setup_ui.py +33 -4
- package/shell_reads.py +67 -0
- package/smart_tool_daemon.py +149 -39
- package/tray.py +60 -9
- package/update_splash.py +2 -2
- 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/web_document_graph.py +12 -1
package/client_hooks.py
CHANGED
|
@@ -10,13 +10,15 @@ import tomllib
|
|
|
10
10
|
|
|
11
11
|
import atomic_io
|
|
12
12
|
import codex_config
|
|
13
|
-
import daemon_launcher
|
|
14
13
|
import endpoint_sync
|
|
14
|
+
import hook_call
|
|
15
15
|
|
|
16
16
|
HOOK_SCRIPT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "hooks", "pretool_router.py")
|
|
17
17
|
MARKER = "pretool_router.py"
|
|
18
18
|
AUDIT_SCRIPT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "hooks", "session_audit.py")
|
|
19
19
|
AUDIT_MARKER = "session_audit.py"
|
|
20
|
+
TURN_SCRIPT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "hooks", "turn_end.py")
|
|
21
|
+
TURN_MARKER = "turn_end.py"
|
|
20
22
|
# Hook origin convention shared by every project that installs hooks: a failed command hook names its owner.
|
|
21
23
|
HOOK_ORIGIN = "--hook-origin=smart-tool"
|
|
22
24
|
AUDIT_MATCHER = "startup|resume|clear|compact|fork"
|
|
@@ -27,20 +29,17 @@ AUDIT_WAITING = "waiting_approval"
|
|
|
27
29
|
OWNER = "smart-tool"
|
|
28
30
|
MANAGER_TIMEOUT_S = 5
|
|
29
31
|
MANAGER_HELD = ("quarantined", "held")
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
#
|
|
33
|
-
HTTP_TIMEOUT_S = 15
|
|
34
|
-
# The PostToolUse waits for the Smart Tool answer of a redirected call (web_fetch reads a whole page).
|
|
35
|
-
POST_TIMEOUT_S = 60
|
|
32
|
+
LEGACY_URL_PATHS = ("/hooks/pretool", "/hooks/posttool", "/hooks/subagent-start")
|
|
33
|
+
# Each entry's timeout covers the restart wait of `hook_call` plus the longest answer the daemon takes on that route
|
|
34
|
+
# (a redirected shell search in the PreToolUse, a whole page in the PostToolUse of web_fetch).
|
|
36
35
|
HOME = os.path.expanduser("~")
|
|
37
36
|
PREVIEW_TTL_S = 600
|
|
38
37
|
CLIENTS = {
|
|
39
38
|
"claude": {"label": "Claude Code", "names": {"claude-code"}, "files": [os.path.join(endpoint_sync.claude_dir(), "settings.json")],
|
|
40
|
-
"docs": "https://code.claude.com/docs/en/hooks", "event": "PreToolUse", "matcher": "Grep|Glob|Read|Bash|PowerShell|WebSearch|WebFetch|Edit|MultiEdit|Write|mcp__smart-tool__smart_search|mcp__smart-tool__web_search|mcp__smart-tool__web_fetch",
|
|
39
|
+
"docs": "https://code.claude.com/docs/en/hooks", "event": "PreToolUse", "matcher": "Agent|Task|Grep|Glob|Read|Bash|PowerShell|WebSearch|WebFetch|Edit|MultiEdit|Write|mcp__smart-tool__smart_search|mcp__smart-tool__web_search|mcp__smart-tool__web_fetch",
|
|
41
40
|
"post_matcher": "Grep|Bash|PowerShell|WebFetch|WebSearch"},
|
|
42
41
|
"codex": {"label": "Codex", "prefix": "codex", "files": [os.path.join(endpoint_sync.codex_dir(), "hooks.json"), os.path.join(endpoint_sync.codex_dir(), "config.toml")],
|
|
43
|
-
"docs": "https://learn.chatgpt.com/docs/hooks", "event": "PreToolUse", "matcher": "Bash|apply_patch",
|
|
42
|
+
"docs": "https://learn.chatgpt.com/docs/hooks", "event": "PreToolUse", "matcher": "Bash|apply_patch", "post_matcher": "Bash",
|
|
44
43
|
"after_install": "Smart Tool recorded the approval of the new definition in Codex's config.toml, as /hooks "
|
|
45
44
|
"would, so Codex runs it from the next session on."},
|
|
46
45
|
"cursor": {"label": "Cursor", "prefix": "cursor", "files": [os.path.join(HOME, ".cursor", "hooks.json")],
|
|
@@ -58,17 +57,30 @@ def identify(client_info):
|
|
|
58
57
|
|
|
59
58
|
|
|
60
59
|
def _our_hook(hook):
|
|
61
|
-
return isinstance(hook, dict) and (MARKER in str(hook.get("command", "")) or
|
|
62
|
-
any(path in str(hook.get("url", "")) for path in
|
|
60
|
+
return isinstance(hook, dict) and (MARKER in str(hook.get("command", "")) or TURN_MARKER in str(hook.get("command", "")) or
|
|
61
|
+
any(path in str(hook.get("url", "")) for path in LEGACY_URL_PATHS))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _events(client):
|
|
65
|
+
"""Events install writes an entry of ours under for the client, known without the daemon (removal needs no URL)."""
|
|
66
|
+
spec = CLIENTS[client]
|
|
67
|
+
return [spec["event"], *(["PostToolUse"] if spec.get("post_matcher") else []), *(["SubagentStart", "Stop"] if client == "claude" else [])]
|
|
63
68
|
|
|
64
69
|
|
|
65
70
|
def _entries(client):
|
|
66
71
|
"""{event: (matcher, handler)} of our hook entries for the client: the PreToolUse, plus the PostToolUse that returns
|
|
67
|
-
the Smart Tool answer of a redirected call in Claude Code
|
|
72
|
+
the Smart Tool answer of a redirected call and the review of the files a shell call wrote, plus, in Claude Code, the
|
|
73
|
+
Stop command of the end-of-turn documentation step (matcher None: Stop takes none), plus the SubagentStart that tells
|
|
74
|
+
every subagent about smart_scout (no matcher: it reaches every agent type)."""
|
|
68
75
|
spec = CLIENTS[client]
|
|
69
76
|
entries = {spec["event"]: (spec["matcher"], _handler(client))}
|
|
70
77
|
if spec.get("post_matcher"):
|
|
71
|
-
entries["PostToolUse"] = (spec["post_matcher"], _handler(client,
|
|
78
|
+
entries["PostToolUse"] = (spec["post_matcher"], _handler(client, "posttool"))
|
|
79
|
+
if "SubagentStart" in _events(client):
|
|
80
|
+
entries["SubagentStart"] = (None, _handler(client, "subagent-start"))
|
|
81
|
+
if "Stop" in _events(client):
|
|
82
|
+
entries["Stop"] = (None, {"type": "command", "timeout": _timeout("stop"),
|
|
83
|
+
"command": f"{_shell_safe(sys.executable)} {_shell_safe(TURN_SCRIPT)} {HOOK_ORIGIN}"})
|
|
72
84
|
return entries
|
|
73
85
|
|
|
74
86
|
|
|
@@ -141,7 +153,7 @@ def inspect(client, trust=True, manager=True):
|
|
|
141
153
|
text = stream.read()
|
|
142
154
|
except FileNotFoundError:
|
|
143
155
|
continue
|
|
144
|
-
if MARKER not in text and
|
|
156
|
+
if MARKER not in text and not any(path in text for path in LEGACY_URL_PATHS):
|
|
145
157
|
continue
|
|
146
158
|
if not path.endswith(".json"):
|
|
147
159
|
return {"state": "current", "changes": []}
|
|
@@ -167,7 +179,7 @@ def _entry_changes(event, matcher, expected, groups):
|
|
|
167
179
|
group = ours[0]
|
|
168
180
|
changes = []
|
|
169
181
|
covered = str(group.get("matcher") or "").split("|")
|
|
170
|
-
left_out = [tool for tool in matcher.split("|") if tool not in covered]
|
|
182
|
+
left_out = [tool for tool in (matcher or "").split("|") if tool not in covered]
|
|
171
183
|
if left_out:
|
|
172
184
|
changes.append(f"Its {event} entry does not cover {', '.join(left_out)}.")
|
|
173
185
|
found = next(hook for hook in group["hooks"] if _our_hook(hook))
|
|
@@ -237,30 +249,20 @@ def _shell_safe(path):
|
|
|
237
249
|
return path.replace("\\", "/")
|
|
238
250
|
|
|
239
251
|
|
|
240
|
-
def
|
|
241
|
-
"""The
|
|
242
|
-
return
|
|
252
|
+
def _timeout(route):
|
|
253
|
+
"""The entry timeout of a route: the restart wait plus the longest answer, with a second to spare."""
|
|
254
|
+
return hook_call.ROUTE_TIMEOUT_S[route] + 1 + hook_call.RESTART_WINDOW_S
|
|
243
255
|
|
|
244
256
|
|
|
245
|
-
def
|
|
246
|
-
"""The
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
raise ValueError(f"The daemon announcement ({daemon_launcher.REGISTRY_PATH}) is outside the home folder; "
|
|
250
|
-
"the hook cannot name it as its port file.")
|
|
251
|
-
return f"~/{relative}"
|
|
257
|
+
def _command(client, route):
|
|
258
|
+
"""The command of a client for a route (`pretool`, `posttool`, `subagent-start`), signed with HOOK_ORIGIN at the
|
|
259
|
+
end; it finds the daemon through its run file on every call."""
|
|
260
|
+
return f"{_shell_safe(sys.executable)} {_shell_safe(HOOK_SCRIPT)} --client {client} --route {route} {HOOK_ORIGIN}"
|
|
252
261
|
|
|
253
262
|
|
|
254
|
-
def _handler(client, route=
|
|
255
|
-
"""
|
|
256
|
-
|
|
257
|
-
if CLIENTS[client].get("http"):
|
|
258
|
-
registry = daemon_launcher.read_registry()
|
|
259
|
-
if not registry or not registry.get("url"):
|
|
260
|
-
raise ValueError("Smart Tool daemon has no registered URL; start the daemon before installing the hook.")
|
|
261
|
-
query = f"client={client}&hook-origin={OWNER}&port-file={_port_file()}"
|
|
262
|
-
return {"type": "http", "url": f"{registry['url']}{route}?{query}", "timeout": timeout}
|
|
263
|
-
return {"type": "command", "command": _command(client), "timeout": timeout}
|
|
263
|
+
def _handler(client, route="pretool"):
|
|
264
|
+
"""The command handler of a client's entry for a route."""
|
|
265
|
+
return {"type": "command", "command": _command(client, route), "timeout": _timeout(route)}
|
|
264
266
|
|
|
265
267
|
|
|
266
268
|
def _slot(groups, is_ours):
|
|
@@ -299,7 +301,8 @@ def _plan(client_info):
|
|
|
299
301
|
raise ValueError(f"{path}: hooks.{event} is not a list; nothing was changed.")
|
|
300
302
|
before, slot = list(groups), _slot(groups, _our_hook)
|
|
301
303
|
groups[:] = _without_ours(groups)
|
|
302
|
-
changes[event] = (groups, {"matcher": matcher, "hooks": [handler]},
|
|
304
|
+
changes[event] = (groups, {"hooks": [handler]} if matcher is None else {"matcher": matcher, "hooks": [handler]},
|
|
305
|
+
slot, before)
|
|
303
306
|
return client, spec, path, data, changes
|
|
304
307
|
|
|
305
308
|
|
|
@@ -340,9 +343,8 @@ def install(client_info, token, confirm):
|
|
|
340
343
|
|
|
341
344
|
|
|
342
345
|
def remove(client):
|
|
343
|
-
"""Takes every
|
|
344
|
-
|
|
345
|
-
backup}."""
|
|
346
|
+
"""Takes every hook entry install wrote (each event of `_entries`) out of the client's hooks file after a backup;
|
|
347
|
+
other hooks stay, in Codex at the approval of their new index. Returns {client, changed, file, backup}."""
|
|
346
348
|
path = os.path.realpath(CLIENTS[client]["files"][0])
|
|
347
349
|
try:
|
|
348
350
|
with open(path, encoding="utf-8") as stream:
|
|
@@ -353,8 +355,7 @@ def remove(client):
|
|
|
353
355
|
if not isinstance(hooks, dict):
|
|
354
356
|
raise ValueError(f"{path} does not have the expected format (object with 'hooks'); nothing was changed.")
|
|
355
357
|
moved = {}
|
|
356
|
-
|
|
357
|
-
for event in (spec["event"], *(["PostToolUse"] if spec.get("post_matcher") else [])):
|
|
358
|
+
for event in _events(client):
|
|
358
359
|
groups = hooks.get(event)
|
|
359
360
|
if not isinstance(groups, list) or not any(_ours(group) for group in groups):
|
|
360
361
|
continue
|
package/code_graph.py
CHANGED
|
@@ -97,6 +97,28 @@ def _snapshot(path):
|
|
|
97
97
|
conn.close()
|
|
98
98
|
|
|
99
99
|
|
|
100
|
+
def python_import_targets(path,node,known):
|
|
101
|
+
"""The files of `known` that an import statement of the Python file `path` names.
|
|
102
|
+
|
|
103
|
+
Parameters: path (str) project-relative path of the importing file; node (ast.Import or ast.ImportFrom); known
|
|
104
|
+
(container of str) the project's files.
|
|
105
|
+
Returns: list of (alias, target, base): `target` is the file the alias resolves to or None; `base` is the module file of
|
|
106
|
+
a `from` import (None for a plain import and when the module is not a project file).
|
|
107
|
+
"""
|
|
108
|
+
def resolve_file(spec):
|
|
109
|
+
spec=posixpath.normpath(spec)
|
|
110
|
+
return next((p for p in (spec+'.py',spec+'/__init__.py','src/'+spec+'.py','src/'+spec+'/__init__.py') if p in known), None)
|
|
111
|
+
if isinstance(node,ast.Import):
|
|
112
|
+
return [(item,resolve_file(item.name.replace('.','/')),None) for item in node.names]
|
|
113
|
+
prefix=posixpath.dirname(path)
|
|
114
|
+
if node.level:
|
|
115
|
+
for _ in range(node.level-1):prefix=posixpath.dirname(prefix)
|
|
116
|
+
spec=posixpath.join(prefix,(node.module or '').replace('.','/'))
|
|
117
|
+
else:spec=(node.module or '').replace('.','/')
|
|
118
|
+
base=resolve_file(spec)
|
|
119
|
+
return [(item,(resolve_file(spec+'/'+item.name) if item.name!='*' else None) or base,base) for item in node.names]
|
|
120
|
+
|
|
121
|
+
|
|
100
122
|
def _python_graph(sources):
|
|
101
123
|
trees, symbols, dependencies, calls, unresolved, diagnostics = {}, [], [], [], [], []
|
|
102
124
|
decls, aliases, scopes, classes, owners, shadow_scopes, node_symbols = {}, {}, {}, {}, {}, {}, {}
|
|
@@ -104,9 +126,6 @@ def _python_graph(sources):
|
|
|
104
126
|
def bind_import(path,scope,name,binding):
|
|
105
127
|
table=aliases[path].setdefault(scope,{})
|
|
106
128
|
table[name]=binding if name not in table or table[name]==binding else None
|
|
107
|
-
def resolve_file(spec):
|
|
108
|
-
spec=posixpath.normpath(spec)
|
|
109
|
-
return next((p for p in (spec+'.py',spec+'/__init__.py','src/'+spec+'.py','src/'+spec+'/__init__.py') if p in sources), None)
|
|
110
129
|
for path, text in sources.items():
|
|
111
130
|
if not path.endswith('.py'):
|
|
112
131
|
continue
|
|
@@ -132,20 +151,11 @@ def _python_graph(sources):
|
|
|
132
151
|
collect(tree)
|
|
133
152
|
for node in ast.walk(tree):
|
|
134
153
|
if isinstance(node,ast.Import):
|
|
135
|
-
for item in node
|
|
136
|
-
spec=item.name.replace('.','/');target=resolve_file(spec)
|
|
154
|
+
for item,target,_base in python_import_targets(path,node,sources):
|
|
137
155
|
bind_import(path,owners[id(node)],item.asname or item.name,(target,None))
|
|
138
156
|
dependencies.append({'source':path,'target':target,'specifier':item.name,'line':node.lineno,'kind':'import','resolution':'resolved' if target else 'external'})
|
|
139
157
|
elif isinstance(node,ast.ImportFrom):
|
|
140
|
-
|
|
141
|
-
if node.level:
|
|
142
|
-
for _ in range(node.level-1):prefix=posixpath.dirname(prefix)
|
|
143
|
-
spec=posixpath.join(prefix,(node.module or '').replace('.','/'))
|
|
144
|
-
else:spec=(node.module or '').replace('.','/')
|
|
145
|
-
base=resolve_file(spec)
|
|
146
|
-
for item in node.names:
|
|
147
|
-
target=resolve_file(spec+'/'+item.name) if item.name!='*' else None
|
|
148
|
-
target=target or base
|
|
158
|
+
for item,target,base in python_import_targets(path,node,sources):
|
|
149
159
|
bind_import(path,owners[id(node)],item.asname or item.name,(target,None if target and target!=base else item.name))
|
|
150
160
|
dependencies.append({'source':path,'target':target,'specifier':('.'*node.level)+(node.module or '')+'.'+item.name,'line':node.lineno,'kind':'import','resolution':'resolved' if target else 'unresolved' if node.level else 'external'})
|
|
151
161
|
def chain(expr):
|
|
@@ -418,7 +428,9 @@ def _disk_save(key,data):
|
|
|
418
428
|
return None
|
|
419
429
|
|
|
420
430
|
|
|
421
|
-
def
|
|
431
|
+
def _cached_analysis(root):
|
|
432
|
+
"""(key, analysis) of the project's current index when memory or disk holds it; (key, None) when it does not and (None,
|
|
433
|
+
None) when the project has no index. Starts nothing."""
|
|
422
434
|
path=indexer.existing_db_path(root)
|
|
423
435
|
if not path:
|
|
424
436
|
return None,None
|
|
@@ -433,6 +445,35 @@ def cached_symbols(root):
|
|
|
433
445
|
with _LOCK:
|
|
434
446
|
_CACHE[key]=data
|
|
435
447
|
while len(_CACHE)>3:_CACHE.popitem(last=False)
|
|
448
|
+
return key,data
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def cached_graph(root):
|
|
452
|
+
"""The analysis of the project's current index when memory or disk already holds it, else None: it never analyzes and
|
|
453
|
+
never starts the background warm-up. The caller must only read it."""
|
|
454
|
+
return _cached_analysis(root)[1]
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
def import_edges(data):
|
|
458
|
+
"""Resolved import edges of an analysis: dict file -> set of project files it imports, every file of an edge a key."""
|
|
459
|
+
edges={}
|
|
460
|
+
for dependency in data.get('dependencies') or []:
|
|
461
|
+
if dependency.get('kind')=='import' and dependency.get('resolution')=='resolved' and dependency.get('target'):
|
|
462
|
+
edges.setdefault(dependency['source'],set()).add(dependency['target'])
|
|
463
|
+
edges.setdefault(dependency['target'],set())
|
|
464
|
+
return edges
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def cached_symbols(root):
|
|
468
|
+
"""Symbols by file of the project's current index when its analysis is cached, starting the background analysis when it
|
|
469
|
+
is not (the caller never waits for it).
|
|
470
|
+
|
|
471
|
+
Parameters: root (str) project folder.
|
|
472
|
+
Returns: tuple (dict {"symbols", "parsed", "note"} or None while no analysis is ready; str note or None).
|
|
473
|
+
"""
|
|
474
|
+
key,data=_cached_analysis(root)
|
|
475
|
+
if key is None:
|
|
476
|
+
return None,None
|
|
436
477
|
with _LOCK:
|
|
437
478
|
state=_WARM_STATE.get(root)
|
|
438
479
|
if data is None and state and state['key']==key and (state['result'] or time.time()-state['at']<WARM_RETRY_S):
|
package/code_graph_java.mjs
CHANGED
|
@@ -9,7 +9,7 @@ if(nodeModule.registerHooks){const bundle=new URL('chevrotain/lib/chevrotain.mjs
|
|
|
9
9
|
load(url,context,next){return url.startsWith('lodash-function:')?{format:'module',source:`import _ from ${JSON.stringify(lodash)};export default _.${url.slice(16)};`,shortCircuit:true}:next(url,context)}})}
|
|
10
10
|
const {parse}=await import(new URL('java-parser/src/index.js',modules).href);
|
|
11
11
|
const input=JSON.parse(fs.readFileSync(0,'utf8'));
|
|
12
|
-
const symbols=[],dependencies=[],calls=[],unresolved=[],diagnostics=[],units=[],classes=new Map(),
|
|
12
|
+
const symbols=[],dependencies=[],calls=[],unresolved=[],diagnostics=[],units=[],classes=new Map(),kids=new WeakMap(),declaredOnly=new Set();
|
|
13
13
|
const {symbols:MAX_SYMBOLS,edges:MAX_EDGES}=input.limits;const leftOut={symbols:0,edges:0};
|
|
14
14
|
function room(list,max,kind){if(list.length<max)return true;leftOut[kind]++;return false}
|
|
15
15
|
const NO_CHILDREN=Object.freeze([]);
|
|
@@ -20,23 +20,32 @@ function identifiers(n){const out=[];function visit(node){if(node?.children)for(
|
|
|
20
20
|
function text(u,n){return n?.location?u.text.slice(n.location.startOffset,n.location.endOffset+1):''}
|
|
21
21
|
function addSymbol(u,n,name,kind){if(!room(symbols,MAX_SYMBOLS,'symbols'))return null;const s={id:u.path+'::'+name+'@'+n.location.startLine+':'+n.location.startOffset,path:u.path,name,kind,language:'java',start_line:n.location.startLine,end_line:n.location.endLine};symbols.push(s);return s}
|
|
22
22
|
const classKinds=new Set(['normalClassDeclaration','normalInterfaceDeclaration','enumDeclaration','recordDeclaration']);
|
|
23
|
-
|
|
23
|
+
const METHOD_KINDS=new Set(['methodDeclaration','interfaceMethodDeclaration','constructorDeclaration','compactConstructorDeclaration']);
|
|
24
|
+
// Each file is parsed twice, once to index its types and once to walk its calls, so only one syntax tree is alive at a time.
|
|
25
|
+
for(const file of input.files){try{const tree=parse(file.text),pkg=first(tree,'packageDeclaration'),unit={...file,methodsAt:new Map(),package:pkg?identifiers(pkg).map(t=>t.image).join('.'): '',imports:[],classes:[]};
|
|
24
26
|
for(const n of all(tree,'importDeclaration')){const raw=text(unit,n).replace(/^import\s+/,'').replace(/;\s*$/,'').trim(),stat=raw.startsWith('static '),spec=(stat?raw.slice(7):raw).replace(/\s+/g,'');unit.imports.push({spec,static:stat,line:n.location.startLine})}
|
|
25
|
-
function scan(n,parent=null){let current=parent;if(classKinds.has(n.name)){const id=first(n,'typeIdentifier')||first(n,'typeName'),short=identifiers(id||n)[0]?.image;if(!short)return;const local=(parent?parent.local+'.':'')+short,fqn=(unit.package?unit.package+'.':'')+local;const symbol=addSymbol(unit,n,local,n.name==='normalInterfaceDeclaration'?'interface':'class');if(!symbol)return;current={unit,fqn,local,short,symbol,node:n,body:n.children.classBody?.[0]||n.children.interfaceBody?.[0]||n.children.recordBody?.[0]||n.children.enumBody?.[0],methods:[],fields:new Map(),extends:text(unit,n.children.classExtends?.[0]).replace(/^extends\s+/, '').trim()};classes.set(fqn,classes.has(fqn)?null:current);unit.classes.push(current)}
|
|
27
|
+
function scan(n,parent=null){let current=parent;if(classKinds.has(n.name)){const id=first(n,'typeIdentifier')||first(n,'typeName'),short=identifiers(id||n)[0]?.image;if(!short)return;const local=(parent?parent.local+'.':'')+short,fqn=(unit.package?unit.package+'.':'')+local;const symbol=addSymbol(unit,n,local,n.name==='normalInterfaceDeclaration'?'interface':'class');if(!symbol)return;current={unit,fqn,local,short,symbol,node:n,offset:n.location.startOffset,body:n.children.classBody?.[0]||n.children.interfaceBody?.[0]||n.children.recordBody?.[0]||n.children.enumBody?.[0],methods:[],fields:new Map(),extends:text(unit,n.children.classExtends?.[0]).replace(/^extends\s+/, '').trim()};classes.set(fqn,classes.has(fqn)?null:current);unit.classes.push(current)}
|
|
26
28
|
if(current&&n.name==='classBody'&&n!==current.body)return;
|
|
27
|
-
if(current&&
|
|
28
|
-
for(const child of children(n))scan(child,current)}scan(tree);units.push(unit)
|
|
29
|
+
if(current&&METHOD_KINDS.has(n.name)){const declarator=first(n,'methodDeclarator')||first(n,'constructorDeclarator'),ctor=n.name.includes('Constructor')||n.name==='constructorDeclaration',name=ctor?current.short:(declarator?.children.Identifier?.[0]?.image);if(name){const params=all(declarator||n,'formalParameter'),types=params.map(p=>text(unit,first(p,'unannType'))||'?');const signature=name+'('+types.join(',')+')';const sym=addSymbol(unit,n,current.local+'.'+signature,ctor?'constructor':'method');if(sym){sym.signature=signature;const method={symbol:sym,name,arity:params.length,types,cls:current,concrete:!!first(n,'block')||ctor};current.methods.push(method);unit.methodsAt.set(n.location.startOffset,method)}}}
|
|
30
|
+
for(const child of children(n))scan(child,current)}scan(tree);indexFields(unit);units.push(unit)
|
|
29
31
|
}catch{diagnostics.push({path:file.path,reason:'Invalid or fragmented Java, or syntax not supported by the parser.'})}}
|
|
32
|
+
function indexFields(unit){for(const cl of unit.classes){for(const node of scoped(cl.body,'fieldDeclaration')){const type=text(unit,first(node,'unannType'));for(const id of all(node,'variableDeclaratorId'))putBinding(cl.fields,identifiers(id)[0]?.image,type)}cl.bodyStart=cl.body?.location.startOffset;cl.node=cl.body=null}}
|
|
30
33
|
function resolveType(unit,raw,context){let name=raw.replace(/@[\w.]+(?:\([^)]*\))?/g,'').replace(/<.*>/g,'').replace(/\[\]|\.\.\./g,'').trim();if(!name||['void','boolean','byte','short','int','long','float','double','char','var'].includes(name))return null;if(classes.has(name))return classes.get(name);const candidates=new Set();const local=[context?context.fqn+'.'+name:'',unit.package?unit.package+'.'+name:name];for(const fq of local)if(classes.has(fq))candidates.add(fq);for(const item of unit.imports.filter(i=>!i.static)){if(item.spec.endsWith('.'+name)&&classes.has(item.spec))candidates.add(item.spec);if(item.spec.endsWith('.*')){const fq=item.spec.slice(0,-1)+name;if(classes.has(fq))candidates.add(fq)}}return candidates.size===1?classes.get([...candidates][0]):null}
|
|
31
|
-
function lookupMethod(cls,name,arity,visited=new Set()){if(!cls||visited.has(cls.fqn))return null;visited.add(cls.fqn);const methods=cls.methods.filter(m=>m.name===name&&m.arity===arity&&m.concrete);if(methods.length===1)return methods[0].symbol;if(methods.length>1)return null;return cls.extends?lookupMethod(resolveType(cls.unit,cls.extends,cls),name,arity,visited):null}
|
|
34
|
+
function lookupMethod(cls,name,arity,visited=new Set()){if(!cls||visited.has(cls.fqn))return null;visited.add(cls.fqn);const methods=cls.methods.filter(m=>m.name===name&&m.arity===arity&&m.concrete);if(methods.length===1)return methods[0].symbol;if(methods.length>1)return null;if(cls.symbol.kind==='interface')return declaredMethod(cls,name,arity);return cls.extends?lookupMethod(resolveType(cls.unit,cls.extends,cls),name,arity,visited):null}
|
|
35
|
+
/**
|
|
36
|
+
* The method an interface of the project declares with this name and arity, reached by a call through a field or
|
|
37
|
+
* variable of that type; a method the interface inherits from a library has no project symbol and stays unresolved.
|
|
38
|
+
* @param {object} cls interface entry; @param {string} name method name; @param {number} arity argument count
|
|
39
|
+
* @returns {object|null} the method symbol, or null when none or more than one matches
|
|
40
|
+
*/
|
|
41
|
+
function declaredMethod(cls,name,arity){const methods=cls.methods.filter(m=>m.name===name&&m.arity===arity);if(methods.length!==1)return null;declaredOnly.add(methods[0].symbol.id);return methods[0].symbol}
|
|
32
42
|
function putBinding(map,name,type){if(name)map.set(name,map.has(name)&&map.get(name)!==type?null:type)}
|
|
33
43
|
function scoped(node,name){const out=[];function scan(n){if(n!==node&&(classKinds.has(n.name)||n.name==='lambdaExpression'||n.name==='classBody'))return;if(n.name===name)out.push(n);for(const c of children(n))scan(c)}scan(node);return out}
|
|
34
44
|
function collectVariables(unit,node,map){for(const parentName of ['fieldDeclaration','localVariableDeclaration','variableParaRegularParameter','variableArityParameter'])for(const declaration of scoped(node,parentName)){const type=text(unit,first(declaration,'unannType'));for(const id of all(declaration,'variableDeclaratorId'))putBinding(map,identifiers(id)[0]?.image,type)}}
|
|
35
|
-
for(const unit of units){
|
|
36
|
-
for(const unit of units){
|
|
45
|
+
for(const unit of units){const tree=parse(unit.text);
|
|
37
46
|
for(const item of unit.imports){const spec=item.spec;let target=classes.get(item.static?spec.split('.').slice(0,-1).join('.'):spec);dependencies.push({source:unit.path,target:target?.unit.path||null,specifier:spec,line:item.line,kind:item.static?'java_static_import':'import',resolution:target?'resolved':'external'})}
|
|
38
|
-
function walk(n,cls=null,method=null,vars=new Map(),owner=null){if(classKinds.has(n.name))cls=unit.classes.find(c=>c.
|
|
39
|
-
if(
|
|
47
|
+
function walk(n,cls=null,method=null,vars=new Map(),owner=null){if(classKinds.has(n.name))cls=unit.classes.find(c=>c.offset===n.location.startOffset)||cls;if(cls&&n.name==='classBody'&&n.location.startOffset!==cls.bodyStart)return;
|
|
48
|
+
if(METHOD_KINDS.has(n.name)&&unit.methodsAt.has(n.location.startOffset)){method=unit.methodsAt.get(n.location.startOffset);vars=new Map(cls.fields);collectVariables(unit,n,vars);owner=method.symbol}
|
|
40
49
|
if(n.name==='lambdaExpression'){vars=new Map(vars);for(const param of identifiers(n.children.lambdaParameters?.[0]))vars.set(param.image,null);owner=addSymbol(unit,n,(cls?.local||'')+'.lambda@'+n.location.startLine,'function')||owner}
|
|
41
50
|
if(cls&&['unannType','classType','interfaceType'].includes(n.name)){const type=resolveType(unit,text(unit,n),cls);if(type&&type.unit.path!==unit.path&&room(dependencies,MAX_EDGES,'edges'))dependencies.push({source:unit.path,target:type.unit.path,specifier:type.fqn,line:n.location.startLine,kind:'java_type',resolution:'resolved'})}
|
|
42
51
|
if(cls&&n.name==='primary'){
|
|
@@ -49,13 +58,13 @@ for(const unit of units){
|
|
|
49
58
|
for(const suffix of suffixes){const invoke=suffix.children.methodInvocationSuffix?.[0];if(!invoke){chain+=text(unit,suffix);continue}const args=invoke.children.argumentList?.[0],arity=args?(args.children.expression||[]).length:0;const match=/^(?:([\w.$]+)\.)?([\w$]+)$/.exec(chain);let target=null;
|
|
50
59
|
if(match&&!fluent){const receiver=match[1],name=match[2];let type=null;if(!receiver||receiver==='this')type=cls;else if(receiver==='super')type=resolveType(unit,cls.extends,cls);else if(receiver.startsWith('this.'))type=resolveType(unit,cls.fields.get(receiver.slice(5))||'',cls);else if(vars.has(receiver))type=resolveType(unit,vars.get(receiver)||'',cls);else{type=resolveType(unit,receiver,cls);link(type,invoke.location.startLine)}target=lookupMethod(type,name,arity);if(!target&&!receiver){const candidates=unit.imports.filter(i=>i.static&&(i.spec.endsWith('.'+name)||i.spec.endsWith('.*'))).map(i=>lookupMethod(classes.get(i.spec.split('.').slice(0,-1).join('.')),name,arity)).filter(Boolean);if(candidates.length===1)target=candidates[0]}}
|
|
51
60
|
if(constructed&&!fluent){const methodName=/\.([\w$]+)$/.exec(chain)?.[1];if(methodName)target=lookupMethod(constructed,methodName,arity)}
|
|
52
|
-
if(target&&room(calls,MAX_EDGES,'edges'))calls.push({source:(owner||cls.symbol).id,target:target.id,line:invoke.location.startLine,kind:'call',resolution:'static'});
|
|
61
|
+
if(target&&room(calls,MAX_EDGES,'edges'))calls.push({source:(owner||cls.symbol).id,target:target.id,line:invoke.location.startLine,kind:'call',resolution:declaredOnly.has(target.id)?'declared':'static'});
|
|
53
62
|
else if(unresolved.length<1000)unresolved.push({path:unit.path,line:invoke.location.startLine,expression:chain.slice(0,140),reason:'External/dynamic type, interface without implementation, or ambiguous overload in the Java snapshot.'});
|
|
54
63
|
fluent=true;chain+='<result>';
|
|
55
64
|
}
|
|
56
65
|
}
|
|
57
66
|
for(const child of children(n))walk(child,cls,method,vars,owner)
|
|
58
|
-
}walk(
|
|
67
|
+
}walk(tree)
|
|
59
68
|
}
|
|
60
69
|
const ids=new Set(symbols.map(s=>s.id)),linked=calls.filter(c=>ids.has(c.source)&&ids.has(c.target));
|
|
61
70
|
leftOut.edges+=calls.length-linked.length;
|
package/code_health.py
CHANGED
|
@@ -40,8 +40,9 @@ def dead_code(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
|
40
40
|
"""Functions nothing calls and functions only tests call, from the static call graph.
|
|
41
41
|
|
|
42
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
|
-
is {"name", "path", "line", "public"}
|
|
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,
|
|
45
46
|
reflection or a name built at run time is not seen.
|
|
46
47
|
Raises: ValueError when the code graph is empty.
|
|
47
48
|
"""
|
|
@@ -55,7 +56,8 @@ def dead_code(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
|
55
56
|
|
|
56
57
|
return {"no_caller": [entry(a) for a in alone[:limit]], "only_tests": [entry(a) for a in tested[:limit]],
|
|
57
58
|
"counts": {"no_caller": len(alone), "only_tests": len(tested)},
|
|
58
|
-
"note": "Candidates from the static call graph (Python and Go only): read each one before deleting it."
|
|
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 ""}
|
|
59
61
|
|
|
60
62
|
|
|
61
63
|
def coupled_with(root, paths, view_id=None, limit=DEFAULT_LIMIT):
|
|
@@ -71,15 +73,25 @@ def coupled_with(root, paths, view_id=None, limit=DEFAULT_LIMIT):
|
|
|
71
73
|
commits, note = doc_insights.history(root)
|
|
72
74
|
if note:
|
|
73
75
|
return {"files": [], "note": note}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
return {"files": partners(doc_insights.coupling(model, commits), set(paths))[:limit], "note": ""}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def partners(pairs, changed):
|
|
80
|
+
"""The files that change with the changed ones, from the pairs `doc_insights.coupling` returns.
|
|
81
|
+
|
|
82
|
+
Parameters: pairs (list) rows (a, b, shared, degree, linked), strongest first; changed (set of str) the changed files.
|
|
83
|
+
Returns: list of {"path", "with", "shared", "degree"}, strongest first, one row per partner outside `changed`; a pair
|
|
84
|
+
where one file imports the other is skipped.
|
|
85
|
+
"""
|
|
86
|
+
rows, seen = [], set()
|
|
87
|
+
for first, second, shared, degree, linked in pairs:
|
|
76
88
|
if linked or (first in changed) == (second in changed):
|
|
77
89
|
continue
|
|
78
90
|
partner, anchor = (second, first) if first in changed else (first, second)
|
|
79
91
|
if partner not in seen:
|
|
80
92
|
seen.add(partner)
|
|
81
93
|
rows.append({"path": partner, "with": anchor, "shared": shared, "degree": int(degree * 100)})
|
|
82
|
-
return
|
|
94
|
+
return rows
|
|
83
95
|
|
|
84
96
|
|
|
85
97
|
def _pop_component(stack, on_stack, node):
|
|
@@ -144,12 +156,7 @@ def import_cycles(root, view_id=None, limit=DEFAULT_LIMIT):
|
|
|
144
156
|
data = code_graph.build_current(root, view_id, shared=True)
|
|
145
157
|
if not data.get("symbols") and not data.get("dependencies"):
|
|
146
158
|
raise ValueError(f"The code graph of {root} is empty: index the project first (project_manage action=index).")
|
|
147
|
-
|
|
148
|
-
for dependency in data.get("dependencies") or []:
|
|
149
|
-
if dependency.get("kind") == "import" and dependency.get("resolution") == "resolved" and dependency.get("target"):
|
|
150
|
-
edges.setdefault(dependency["source"], set()).add(dependency["target"])
|
|
151
|
-
edges.setdefault(dependency["target"], set())
|
|
152
|
-
cycles = sorted(_components(edges), key=lambda files: (-len(files), files))
|
|
159
|
+
cycles = sorted(_components(code_graph.import_edges(data)), key=lambda files: (-len(files), files))
|
|
153
160
|
note = code_graph.left_out_sentence(data) if data.get("truncated") else ""
|
|
154
161
|
return {"cycles": [{"files": files, "size": len(files)} for files in cycles[:limit]], "total": len(cycles),
|
|
155
162
|
"files_in_cycles": sum(len(files) for files in cycles), "note": note}
|
package/code_impact.py
CHANGED
|
@@ -127,7 +127,7 @@ def impact(root, symbol, view_id=None, depth=3, limit=30):
|
|
|
127
127
|
outgoing = [_site(by_id, c["target"], c["line"], label.get(t)) for t in targets for c in callees[t]
|
|
128
128
|
if c["target"] not in targets]
|
|
129
129
|
files = {s["path"] for s in found}
|
|
130
|
-
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})
|
|
131
131
|
ordered_tests = sorted(tests.values(), key=lambda t: (t["hops"], t["path"], t["line"]))
|
|
132
132
|
shown = direct[:limit] + outgoing[:limit] + ordered_tests[:limit]
|
|
133
133
|
_name_test_callbacks(root, by_id, shown)
|
package/codex_config.py
CHANGED
|
@@ -57,6 +57,9 @@ def _value(value):
|
|
|
57
57
|
.replace("\t", "\\t") + '"'
|
|
58
58
|
if isinstance(value, (list, tuple)):
|
|
59
59
|
return "[" + ", ".join(_value(item) for item in value) + "]"
|
|
60
|
+
if isinstance(value, dict):
|
|
61
|
+
return "{ " + ", ".join(f"{key if _BARE_KEY.match(key) else _value(key)} = {_value(item)}"
|
|
62
|
+
for key, item in value.items()) + " }"
|
|
60
63
|
raise TypeError(f"Unsupported value for config.toml: {value!r}")
|
|
61
64
|
|
|
62
65
|
|
package/config.py
CHANGED
|
@@ -47,6 +47,9 @@ DEFAULT_CONFIG = {
|
|
|
47
47
|
"duplicate_mode": "warn",
|
|
48
48
|
"dependency_mode": "warn",
|
|
49
49
|
"layer_mode": "warn",
|
|
50
|
+
"link_mode": "warn",
|
|
51
|
+
"pr_comment": "off",
|
|
52
|
+
"decision_mode": "warn",
|
|
50
53
|
"document_converter": "off",
|
|
51
54
|
"document_converter_python": "",
|
|
52
55
|
"docs_extras": {key: True for key in DOCS_EXTRAS},
|
|
@@ -84,6 +87,15 @@ DUPLICATE_MODES = ("warn", "off")
|
|
|
84
87
|
# off = say nothing and make no network call.
|
|
85
88
|
DEPENDENCY_MODES = ("warn", "off")
|
|
86
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")
|
|
87
99
|
# External converter read_document falls back to for a file type or a scanned PDF its native reader refuses: off = never;
|
|
88
100
|
# auto = the first of kreuzberg and docling found installed; or one of them by name. The engine is looked for in the
|
|
89
101
|
# interpreter named by `document_converter_python` (empty = the one Smart Tool runs on).
|
|
@@ -134,6 +146,29 @@ def layer_mode(cfg=None):
|
|
|
134
146
|
return value
|
|
135
147
|
|
|
136
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
|
+
|
|
137
172
|
def document_converter(cfg=None):
|
|
138
173
|
"""The external converter setting: `{"mode": one of DOCUMENT_CONVERTERS, "python": interpreter path or ""}`. An
|
|
139
174
|
unknown mode or a python that is not text fails loudly."""
|
package/daemon_launcher.py
CHANGED
|
@@ -19,8 +19,14 @@ import urllib.error
|
|
|
19
19
|
import urllib.request
|
|
20
20
|
from pathlib import Path
|
|
21
21
|
|
|
22
|
+
import venv_launch
|
|
23
|
+
|
|
22
24
|
RUN_DIR = paths.DATA_DIR
|
|
23
25
|
REGISTRY_PATH = os.path.join(RUN_DIR, "daemon.json")
|
|
26
|
+
LOG_PATH = os.path.join(RUN_DIR, "daemon.log")
|
|
27
|
+
SYNCHRONIZE = 0x00100000
|
|
28
|
+
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
|
29
|
+
INFINITE = 0xFFFFFFFF
|
|
24
30
|
LOCK_PATH = os.path.join(RUN_DIR, "daemon.lock")
|
|
25
31
|
LOCK_STALE_SECONDS = 30
|
|
26
32
|
UPDATE_MARKER = os.path.join(RUN_DIR, "updating")
|
|
@@ -85,12 +91,8 @@ def _spawn_daemon():
|
|
|
85
91
|
executable = executable.with_name("python.exe")
|
|
86
92
|
fd, launcher = tempfile.mkstemp(prefix="smart-tool-hidden-", suffix=".vbs")
|
|
87
93
|
try:
|
|
88
|
-
command = (f'Chr(34) & "{executable}" & Chr(34) & " " & '
|
|
89
|
-
f'Chr(34) & "{DAEMON_SCRIPT}" & Chr(34)')
|
|
90
94
|
with os.fdopen(fd, "w", encoding="utf-16") as stream:
|
|
91
|
-
stream.write(
|
|
92
|
-
stream.write(f'sh.CurrentDirectory = "{Path(DAEMON_SCRIPT).parent}"\n')
|
|
93
|
-
stream.write(f'sh.Run {command}, 0, False\n')
|
|
95
|
+
stream.write(venv_launch.hidden_run_vbs(executable, DAEMON_SCRIPT, Path(DAEMON_SCRIPT).parent))
|
|
94
96
|
result = subprocess.run(
|
|
95
97
|
["wscript.exe", "//B", "//NoLogo", launcher],
|
|
96
98
|
stdin=subprocess.DEVNULL, capture_output=True, timeout=10,
|
|
@@ -170,6 +172,36 @@ def start_daemon_if_down():
|
|
|
170
172
|
return False
|
|
171
173
|
|
|
172
174
|
|
|
175
|
+
def note_stop(message):
|
|
176
|
+
"""Appends a stop line to the daemon log, for stops the daemon cannot write itself: the installer closing it and
|
|
177
|
+
the supervisor seeing it end without a word (killed, crashed)."""
|
|
178
|
+
os.makedirs(RUN_DIR, exist_ok=True)
|
|
179
|
+
with open(LOG_PATH, "a", encoding="utf-8") as stream:
|
|
180
|
+
stream.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} {message}\n")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def wait_for_exit(pid):
|
|
184
|
+
"""Blocks until process `pid` ends and returns its exit code; None when it cannot be watched (already gone, not
|
|
185
|
+
ours to open, or not Windows), so the caller falls back to polling."""
|
|
186
|
+
if os.name != "nt" or not pid:
|
|
187
|
+
return None
|
|
188
|
+
import ctypes
|
|
189
|
+
from ctypes import wintypes
|
|
190
|
+
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
191
|
+
kernel32.OpenProcess.restype = wintypes.HANDLE
|
|
192
|
+
handle = kernel32.OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, False, int(pid))
|
|
193
|
+
if not handle:
|
|
194
|
+
return None
|
|
195
|
+
try:
|
|
196
|
+
kernel32.WaitForSingleObject(wintypes.HANDLE(handle), INFINITE)
|
|
197
|
+
code = wintypes.DWORD()
|
|
198
|
+
if not kernel32.GetExitCodeProcess(wintypes.HANDLE(handle), ctypes.byref(code)):
|
|
199
|
+
return None
|
|
200
|
+
return code.value
|
|
201
|
+
finally:
|
|
202
|
+
kernel32.CloseHandle(wintypes.HANDLE(handle))
|
|
203
|
+
|
|
204
|
+
|
|
173
205
|
def _wait_for_daemon(wait_seconds, poll_interval=0.1):
|
|
174
206
|
deadline = time.time() + wait_seconds
|
|
175
207
|
while time.time() < deadline:
|