@allansantos-dev/smart-tool 0.9.19 → 0.9.21
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 +34 -0
- package/README.md +1 -1
- package/affected_tests.py +38 -8
- package/browser_control.py +2 -2
- package/client_hooks.py +2 -2
- package/code_graph.py +4 -2
- package/doc_check.py +7 -3
- package/endpoint_sync.py +7 -2
- package/hooks/pretool_router.py +1 -1
- package/indexer.py +30 -1
- package/package.json +1 -1
- package/setup_audit.py +1 -1
- package/smart_tool_daemon.py +106 -3
- package/version.py +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.9.21 - beta
|
|
4
|
+
|
|
5
|
+
- The PreToolUse hook can no longer hold a tool call for long. Its decision has 4 s; past that it answers without one
|
|
6
|
+
(the tool runs) and the daemon log gets one line, `PreToolUse hook over 4 s for Write <file>: answered without a
|
|
7
|
+
decision`; an answer slower than 1 s is logged too. Before, Write and Edit reviews (docstring reminder, copied
|
|
8
|
+
function check) could take longer than the client's 15 s timeout on large TypeScript or Java files and large
|
|
9
|
+
repositories (75 cancelled hook calls in 9 days, 62 of them Write or Edit) and Bash calls of other sessions waited
|
|
10
|
+
behind them.
|
|
11
|
+
- Edit and Write reviews run on their own two workers: a third simultaneous edit goes through unreviewed (logged) instead
|
|
12
|
+
of queueing, and Bash, Read and Grep never wait for them.
|
|
13
|
+
- The Node code analyzer called by the edit hook stops after 10 s, and an analysis that failed or timed out is no
|
|
14
|
+
longer remembered as "this file has no functions".
|
|
15
|
+
- The timeout written in the client hook entry drops from 15 s to 6 s (Codex's thin command 14 s to 5 s). An entry
|
|
16
|
+
written by an older version shows as outdated on the setup screen until you accept the hook update (preview and
|
|
17
|
+
backup first).
|
|
18
|
+
|
|
19
|
+
## 0.9.20 - beta
|
|
20
|
+
|
|
21
|
+
- The first search of a folder that has no index yet no longer waits for the index. It answers at once with the files
|
|
22
|
+
that hold a query term, read from disk and ranked by keyword (up to 8 s of scanning, and it says how many files it
|
|
23
|
+
reached), and builds the index in the background; the answer says "Indexing in progress: this folder has no index
|
|
24
|
+
yet". A folder with no saved scope and no model to decide it still asks the agent for the scope, as before.
|
|
25
|
+
- `smart_search_result` hands the agent the result text itself when the job is done, the same text a search that
|
|
26
|
+
finishes within the wait returns. Before, it was a JSON wrapper that escaped the text and repeated the index stats
|
|
27
|
+
and the git view (~1.5 thousand characters of metadata per answer). A job still pending or failed comes without
|
|
28
|
+
those fields too.
|
|
29
|
+
- Playwright MCP starts with `--caps=` so a `PLAYWRIGHT_MCP_CAPS=network` in the user's environment cannot turn on
|
|
30
|
+
`browser_route` (measured on 0.0.83: with the variable set, 29 tools including `browser_route`; with `--caps=`, 25
|
|
31
|
+
and no `browser_route`).
|
|
32
|
+
- `CODEX_HOME` is followed everywhere the Codex config is read or written: the MCP registration check, the endpoint
|
|
33
|
+
sync after a port change and the Codex hook file (before, only the browser control and the setup audit followed it).
|
|
34
|
+
- `affected_tests` links a Python test to the modules it imports inside a string, as in
|
|
35
|
+
`subprocess.run([sys.executable, "-c", "import click"])`, so a change to that module selects the test.
|
|
36
|
+
|
|
3
37
|
## 0.9.19 - beta
|
|
4
38
|
|
|
5
39
|
- A search no longer waits for the index to catch up. It answers from the existing index at once, searches the files
|
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ registered as the `playwright` server where this machine lets a browser be drive
|
|
|
15
15
|
An optional hook routes the agent's native Grep/Glob/Read/Bash/WebSearch/WebFetch calls to these tools when they
|
|
16
16
|
are cheaper, or just tells the agent what Smart Tool would do.
|
|
17
17
|
|
|
18
|
-
**Status:** 0.9.
|
|
18
|
+
**Status:** 0.9.21 beta. Windows only. What changed in each version: [CHANGELOG.md](CHANGELOG.md).
|
|
19
19
|
|
|
20
20
|
## How it works
|
|
21
21
|
|
package/affected_tests.py
CHANGED
|
@@ -33,6 +33,8 @@ NOTE = ("Selection from the indexed import graph: tests that load code through s
|
|
|
33
33
|
"files and imports count before reindexing; pytest fixtures link a test to the conftest.py that defines them.")
|
|
34
34
|
TEST_SOURCES = (".py", ".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts", ".java")
|
|
35
35
|
_PATH_LITERAL = re.compile(r"""['"`]((?:\.{0,2}/)?[\w.-]+(?:/[\w.-]+)*\.(?:py|js|mjs|cjs|ts|mts|cts|java))['"`]""")
|
|
36
|
+
_STRING_LITERAL = re.compile(r"""'([^'\n]*)'|"([^"\n]*)\"""")
|
|
37
|
+
_IMPORT_STATEMENT = re.compile(r"(?:^|;)\s*(?:import|from)\s+([A-Za-z_]\w*(?:\.\w+)*)")
|
|
36
38
|
_HUNK = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", re.M)
|
|
37
39
|
|
|
38
40
|
|
|
@@ -213,12 +215,7 @@ def _path_literal_users(root, test_files, known):
|
|
|
213
215
|
if not test.lower().endswith(TEST_SOURCES):
|
|
214
216
|
continue
|
|
215
217
|
source = _read(os.path.join(root, test))
|
|
216
|
-
folders
|
|
217
|
-
while True:
|
|
218
|
-
folders.append(folder)
|
|
219
|
-
if not folder:
|
|
220
|
-
break
|
|
221
|
-
folder = os.path.dirname(folder)
|
|
218
|
+
folders = _folders_above(test)
|
|
222
219
|
for literal in set(_PATH_LITERAL.findall(source)):
|
|
223
220
|
for base in folders:
|
|
224
221
|
candidate = posixpath.normpath(posixpath.join(base, literal.removeprefix("./")))
|
|
@@ -228,6 +225,37 @@ def _path_literal_users(root, test_files, known):
|
|
|
228
225
|
return users
|
|
229
226
|
|
|
230
227
|
|
|
228
|
+
def _import_string_users(root, test_files, known):
|
|
229
|
+
"""Python modules a test imports inside a string (subprocess.run([sys.executable, "-c", "import click"])), resolved
|
|
230
|
+
to the package or module file under the project root, a `src` folder or a folder above the test: the code runs in
|
|
231
|
+
another process, so no import statement of the test links it."""
|
|
232
|
+
users = defaultdict(set)
|
|
233
|
+
for test in test_files:
|
|
234
|
+
if not test.lower().endswith(".py"):
|
|
235
|
+
continue
|
|
236
|
+
folders = ["src", *_folders_above(test)]
|
|
237
|
+
literals = (single or double for single, double in _STRING_LITERAL.findall(_read(os.path.join(root, test))))
|
|
238
|
+
for module in {name for text in literals for name in _IMPORT_STATEMENT.findall(text)}:
|
|
239
|
+
stem = module.replace(".", "/")
|
|
240
|
+
for base in folders:
|
|
241
|
+
target = next((candidate for candidate in (posixpath.join(base, stem + ".py"),
|
|
242
|
+
posixpath.join(base, stem, "__init__.py"))
|
|
243
|
+
if candidate in known and candidate != test), None)
|
|
244
|
+
if target:
|
|
245
|
+
users[target].add(test)
|
|
246
|
+
break
|
|
247
|
+
return users
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _folders_above(path):
|
|
251
|
+
"""The folder of `path` and every folder above it up to the project root (`""`), nearest first."""
|
|
252
|
+
folders, folder = [], os.path.dirname(path)
|
|
253
|
+
while folder:
|
|
254
|
+
folders.append(folder)
|
|
255
|
+
folder = os.path.dirname(folder)
|
|
256
|
+
return folders + [""]
|
|
257
|
+
|
|
258
|
+
|
|
231
259
|
_TEST_CASES = {
|
|
232
260
|
".py": re.compile(r"^\s*(async\s+)?def\s+test\w*\s*\(|^\s*class\s+Test\w*|unittest\.TestCase", re.M),
|
|
233
261
|
".java": re.compile(r"@(Test|ParameterizedTest|RepeatedTest|TestFactory|TestTemplate|RunWith|Suite)\b|"
|
|
@@ -296,8 +324,10 @@ def affected(root, base="HEAD", limit=30, view_id=None):
|
|
|
296
324
|
callers[call["target"]].add(call["source"])
|
|
297
325
|
for conftest, users in _fixture_users(root, test_files).items():
|
|
298
326
|
importers[conftest] |= users
|
|
299
|
-
|
|
300
|
-
|
|
327
|
+
known_files = {f["path"] for f in data.get("files") or []}
|
|
328
|
+
for linked in (_path_literal_users(root, test_files, known_files), _import_string_users(root, test_files, known_files)):
|
|
329
|
+
for target, users in linked.items():
|
|
330
|
+
importers[target] |= users
|
|
301
331
|
by_id = {s["id"]: s for s in data.get("symbols") or []}
|
|
302
332
|
run_all, not_analyzed, changed_code, changed_tests, conftests = [], [], [], [], []
|
|
303
333
|
for path, change in sorted(changes.items()):
|
package/browser_control.py
CHANGED
|
@@ -62,7 +62,7 @@ def server_command():
|
|
|
62
62
|
raise RuntimeError("Supported Node.js not found for Playwright MCP.")
|
|
63
63
|
if not CLI.is_file():
|
|
64
64
|
raise RuntimeError(f"Playwright MCP is not installed ({CLI}); reinstall Smart Tool.")
|
|
65
|
-
return [node, str(CLI), "--isolated", "--headless", "--no-webmcp", "--config", write_config(),
|
|
65
|
+
return [node, str(CLI), "--isolated", "--headless", "--no-webmcp", "--caps=", "--config", write_config(),
|
|
66
66
|
"--executable-path", chromium_executable(), "--output-dir", OUTPUT_DIR,
|
|
67
67
|
"--output-max-size", str(OUTPUT_MAX_BYTES)]
|
|
68
68
|
|
|
@@ -159,7 +159,7 @@ def registered():
|
|
|
159
159
|
|
|
160
160
|
def _codex_config():
|
|
161
161
|
"""Codex keeps config.toml in CODEX_HOME when set, else ~/.codex."""
|
|
162
|
-
return os.path.join(
|
|
162
|
+
return os.path.join(endpoint_sync.codex_dir(), "config.toml")
|
|
163
163
|
|
|
164
164
|
|
|
165
165
|
def _replace_text(path, text):
|
package/client_hooks.py
CHANGED
|
@@ -15,13 +15,13 @@ AUDIT_MARKER = "session_audit.py"
|
|
|
15
15
|
AUDIT_MATCHER = "startup|resume|clear"
|
|
16
16
|
AUDIT_TIMEOUT_S = 10
|
|
17
17
|
HTTP_PATH = "/hooks/pretool"
|
|
18
|
-
HTTP_TIMEOUT_S =
|
|
18
|
+
HTTP_TIMEOUT_S = 6
|
|
19
19
|
HOME = os.path.expanduser("~")
|
|
20
20
|
PREVIEW_TTL_S = 600
|
|
21
21
|
CLIENTS = {
|
|
22
22
|
"claude": {"label": "Claude Code", "names": {"claude-code"}, "files": [os.path.join(endpoint_sync.claude_dir(), "settings.json")],
|
|
23
23
|
"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", "http": True},
|
|
24
|
-
"codex": {"label": "Codex", "prefix": "codex", "files": [os.path.join(
|
|
24
|
+
"codex": {"label": "Codex", "prefix": "codex", "files": [os.path.join(endpoint_sync.codex_dir(), "hooks.json"), os.path.join(endpoint_sync.codex_dir(), "config.toml")],
|
|
25
25
|
"docs": "https://learn.chatgpt.com/docs/hooks", "event": "PreToolUse", "matcher": "Bash|apply_patch",
|
|
26
26
|
"after_install": "Codex only runs new hooks after the user approves the definition in /hooks."},
|
|
27
27
|
"cursor": {"label": "Cursor", "prefix": "cursor", "files": [os.path.join(HOME, ".cursor", "hooks.json")],
|
package/code_graph.py
CHANGED
|
@@ -233,14 +233,16 @@ def _python_graph(sources):
|
|
|
233
233
|
|
|
234
234
|
|
|
235
235
|
NODE_HEAP_MB = (1024, 4096)
|
|
236
|
+
ANALYZER_TIMEOUT_S = contextvars.ContextVar("analyzer_timeout_s", default=120)
|
|
236
237
|
|
|
237
238
|
|
|
238
239
|
def _run_analyzer(script, payload, failure):
|
|
239
240
|
"""Runs a Node analyzer, retrying once with a larger heap when it runs out of memory (634 TypeScript files, 2.9 MB,
|
|
240
|
-
needed more than the former fixed 512 MB).
|
|
241
|
+
needed more than the former fixed 512 MB). The run is killed after `ANALYZER_TIMEOUT_S` seconds (a context
|
|
242
|
+
variable: the edit hook lowers it so a slow file cannot hold its worker)."""
|
|
241
243
|
for heap in NODE_HEAP_MB:
|
|
242
244
|
process=subprocess.run([web_search_adapters.NODE_EXECUTABLE,f'--max-old-space-size={heap}',str(Path(__file__).with_name(script))],
|
|
243
|
-
input=payload,capture_output=True,text=True,encoding='utf-8',timeout=
|
|
245
|
+
input=payload,capture_output=True,text=True,encoding='utf-8',timeout=ANALYZER_TIMEOUT_S.get(),
|
|
244
246
|
creationflags=getattr(subprocess,'CREATE_NO_WINDOW',0))
|
|
245
247
|
if process.returncode==0:
|
|
246
248
|
return json.loads(process.stdout)
|
package/doc_check.py
CHANGED
|
@@ -166,11 +166,13 @@ def functions(path, source, symbols=None):
|
|
|
166
166
|
|
|
167
167
|
def analyze(path, *sources):
|
|
168
168
|
"""Analyzer symbols of several versions of one TypeScript/JavaScript/Java file, from one analyzer run for the
|
|
169
|
-
versions not seen yet (an edit hook needs the file before and after; the next edit's 'before' is this 'after').
|
|
169
|
+
versions not seen yet (an edit hook needs the file before and after; the next edit's 'before' is this 'after').
|
|
170
|
+
An analysis that failed or timed out is returned empty but not cached, so the next call tries again."""
|
|
170
171
|
lower, name = path.lower(), os.path.basename(path)
|
|
171
172
|
keys = [hashlib.sha1(f"{name}\0{source}".encode("utf-8")).hexdigest() for source in sources]
|
|
172
173
|
with _PARSED_LOCK:
|
|
173
174
|
missing = {key: source for key, source in zip(keys, sources) if key not in _PARSED}
|
|
175
|
+
found = {}
|
|
174
176
|
if missing:
|
|
175
177
|
batch = {f"v{i}/{name}": source for i, source in enumerate(missing.values())}
|
|
176
178
|
analysis = (code_graph._javascript_graph(batch) if lower.endswith(SCRIPT)
|
|
@@ -178,11 +180,13 @@ def analyze(path, *sources):
|
|
|
178
180
|
found = {key: [dict(s, path=name) for s in analysis.get("symbols") or [] if s.get("path") == f"v{i}/{name}"]
|
|
179
181
|
for i, key in enumerate(missing)}
|
|
180
182
|
with _PARSED_LOCK:
|
|
181
|
-
|
|
183
|
+
if not analysis.get("retryable"):
|
|
184
|
+
_PARSED.update(found)
|
|
182
185
|
while len(_PARSED) > MAX_PARSED:
|
|
183
186
|
_PARSED.popitem(last=False)
|
|
184
187
|
with _PARSED_LOCK:
|
|
185
|
-
|
|
188
|
+
cached = {key: _PARSED[key] for key in keys if key in _PARSED}
|
|
189
|
+
return [cached.get(key, found.get(key, [])) for key in keys]
|
|
186
190
|
|
|
187
191
|
|
|
188
192
|
def _touched_lines(before, after):
|
package/endpoint_sync.py
CHANGED
|
@@ -27,6 +27,11 @@ def claude_dir():
|
|
|
27
27
|
return os.environ.get("CLAUDE_CONFIG_DIR") or _home(".claude")
|
|
28
28
|
|
|
29
29
|
|
|
30
|
+
def codex_dir():
|
|
31
|
+
"""Codex keeps config.toml and hooks.json in CODEX_HOME when set, else ~/.codex."""
|
|
32
|
+
return os.environ.get("CODEX_HOME") or _home(".codex")
|
|
33
|
+
|
|
34
|
+
|
|
30
35
|
def claude_json():
|
|
31
36
|
"""User-scope MCP servers: $CLAUDE_CONFIG_DIR/.claude.json when set, else ~/.claude.json."""
|
|
32
37
|
custom = os.environ.get("CLAUDE_CONFIG_DIR")
|
|
@@ -72,7 +77,7 @@ def _codex_mcp(text, base):
|
|
|
72
77
|
|
|
73
78
|
def targets():
|
|
74
79
|
return ((claude_json(), _claude_mcp), (os.path.join(claude_dir(), "settings.json"), _claude_hook),
|
|
75
|
-
(
|
|
80
|
+
(os.path.join(codex_dir(), "config.toml"), _codex_mcp))
|
|
76
81
|
|
|
77
82
|
|
|
78
83
|
_CODEX_ANY_URL_RE = re.compile(r'\[mcp_servers\.smart-tool\][^\[]*?\burl\s*=\s*"([^"]+)"')
|
|
@@ -93,7 +98,7 @@ def registrations():
|
|
|
93
98
|
except (OSError, AttributeError):
|
|
94
99
|
break
|
|
95
100
|
try:
|
|
96
|
-
with open(
|
|
101
|
+
with open(os.path.join(codex_dir(), "config.toml"), encoding="utf-8") as stream:
|
|
97
102
|
match = _CODEX_ANY_URL_RE.search(stream.read())
|
|
98
103
|
found["codex"] = match.group(1) if match else None
|
|
99
104
|
except OSError:
|
package/hooks/pretool_router.py
CHANGED
package/indexer.py
CHANGED
|
@@ -843,6 +843,32 @@ def overlay_chunks(root, rel_paths):
|
|
|
843
843
|
return chunks
|
|
844
844
|
|
|
845
845
|
|
|
846
|
+
def keyword_chunks(root, rel_paths, queries, budget_s):
|
|
847
|
+
"""`(chunks, scanned)`: the chunks, read from disk right now, of the files among `rel_paths` that contain at least
|
|
848
|
+
one term of `queries` (the file name counts), for a project that has no index yet. The scan stops after `budget_s`
|
|
849
|
+
seconds; `scanned` is how many files it reached. Reading and skipping rules are the index's (`_stable_read`)."""
|
|
850
|
+
terms = [term.casefold() for term in dict.fromkeys(term for query in queries for term in _query_terms(query))]
|
|
851
|
+
deadline = time.monotonic() + budget_s
|
|
852
|
+
chunks, scanned = [], 0
|
|
853
|
+
for rel in rel_paths:
|
|
854
|
+
if time.monotonic() > deadline:
|
|
855
|
+
break
|
|
856
|
+
scanned += 1
|
|
857
|
+
data, encoding, _stat = _stable_read(root, rel)
|
|
858
|
+
if data is None:
|
|
859
|
+
continue
|
|
860
|
+
try:
|
|
861
|
+
text = data.decode(encoding or "utf-8", "ignore").casefold()
|
|
862
|
+
except LookupError:
|
|
863
|
+
continue
|
|
864
|
+
name = rel.casefold()
|
|
865
|
+
if not any(term in text or term in name for term in terms):
|
|
866
|
+
continue
|
|
867
|
+
pieces = chunk_file(root, rel, CHUNK_LINES, data=data, encoding=encoding)
|
|
868
|
+
chunks.extend((rel, start, end, piece) for start, end, piece in pieces or [])
|
|
869
|
+
return chunks, scanned
|
|
870
|
+
|
|
871
|
+
|
|
846
872
|
def _overlay_scored(chunks, query, alpha):
|
|
847
873
|
"""`[(score, path, start, end, text)]` for overlay chunks matching the query terms by keyword. A chunk that holds
|
|
848
874
|
every term scores 1.0, the top of the fused index scores (`search` ranks it first on a tie): the edited file is read
|
|
@@ -874,7 +900,10 @@ def search(root, query_vector=None, top_k=20, query=None, model_id=None, include
|
|
|
874
900
|
files) are ignored and the overlay chunks, read from disk, take part by keyword instead."""
|
|
875
901
|
path = existing_db_path(root)
|
|
876
902
|
if not path:
|
|
877
|
-
|
|
903
|
+
if not (overlay and overlay["chunks"] and query):
|
|
904
|
+
return []
|
|
905
|
+
extra = _overlay_scored([chunk for chunk in overlay["chunks"] if include is None or include(chunk[0])], query, 0.0)
|
|
906
|
+
return sorted(extra, key=lambda item: item[0], reverse=True)[:top_k]
|
|
878
907
|
kind_filter = include
|
|
879
908
|
if overlay and overlay["drop"]:
|
|
880
909
|
dropped = set(overlay["drop"])
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allansantos-dev/smart-tool",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.21",
|
|
4
4
|
"description": "Local MCP server that gives coding agents (Claude Code, Codex) cheaper, sharper tools than their built-in search. Windows.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Allan Santos",
|
package/setup_audit.py
CHANGED
package/smart_tool_daemon.py
CHANGED
|
@@ -70,6 +70,7 @@ import web_fetch
|
|
|
70
70
|
import version
|
|
71
71
|
import update_check
|
|
72
72
|
import gateway
|
|
73
|
+
import code_graph
|
|
73
74
|
import hook_decision
|
|
74
75
|
import endpoint_sync
|
|
75
76
|
|
|
@@ -176,6 +177,7 @@ _MAX_JOBS_RETAINED = 200
|
|
|
176
177
|
# nearly every call and costs the agent extra smart_search_result round trips. Codex gives an MCP call 60 s by default.
|
|
177
178
|
SMART_SEARCH_SYNC_WAIT_S = 25
|
|
178
179
|
OVERLAY_MAX_FILES = 40
|
|
180
|
+
COLD_SCAN_BUDGET_S = 8
|
|
179
181
|
EDIT_INDEX_DELAY_S = 8
|
|
180
182
|
# smart_search_result waits for the job this long before answering pending: answering at once had an agent poll the
|
|
181
183
|
# same job 7 times in 16 s and then give up on Smart Tool (claude -p session, 2026-10-09; job durations median 6 s,
|
|
@@ -875,6 +877,29 @@ def _index_lag(root, model, cfg, problem, job_id):
|
|
|
875
877
|
"refresh": bool(changed or removed or rescan or _profile_due(scope, scope_model))}
|
|
876
878
|
|
|
877
879
|
|
|
880
|
+
def _cold_index(root, cfg, problem):
|
|
881
|
+
"""Whether this search is the first one of a project with no index, and the index can be built in the background:
|
|
882
|
+
the gateway answers and the scope is saved or a model can decide it. Otherwise the search keeps its old course
|
|
883
|
+
(build first, or ask the agent for the scope)."""
|
|
884
|
+
if indexer.info(root).get("chunks") or problem:
|
|
885
|
+
return False
|
|
886
|
+
return bool(index_scope.valid_scope(index_scope.load_scope(root)) or cfg.get("scope_model") or cfg.get("router_model"))
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
def _cold_overlay(root, queries):
|
|
890
|
+
"""`(overlay, warning)` for a project with no index: the in-scope files (the saved scope, else the whole folder minus
|
|
891
|
+
the always-excluded names) holding a query term, read from disk and ranked by keyword, plus the notice that the
|
|
892
|
+
index is being built. The scan stops after `COLD_SCAN_BUDGET_S`."""
|
|
893
|
+
scope = index_scope.load_scope(root)
|
|
894
|
+
files = index_scope.resolve_included_files(root, scope if index_scope.valid_scope(scope) else {"include": [""]})
|
|
895
|
+
chunks, scanned = indexer.keyword_chunks(root, files, queries, COLD_SCAN_BUDGET_S)
|
|
896
|
+
reached = f"{scanned} of {len(files)} files" if scanned < len(files) else f"all {len(files)} files"
|
|
897
|
+
warning = ("Indexing in progress: this folder has no index yet, so the results are keyword matches read straight "
|
|
898
|
+
f"from disk in {reached}, without semantic ranking. The index is being built in the background: repeat "
|
|
899
|
+
"the search in a few minutes for the full ranking.")
|
|
900
|
+
return {"drop": [], "chunks": chunks}, warning
|
|
901
|
+
|
|
902
|
+
|
|
878
903
|
def _lag_overlay(root, lag):
|
|
879
904
|
"""`(overlay, warning)` for `indexer.search`: the stale files read from disk, searched by keyword in place of their
|
|
880
905
|
outdated chunks, and the notice telling the agent that indexing is in progress. `(None, "")` when the index is
|
|
@@ -1086,6 +1111,10 @@ def _perform_smart_search_isolated(arguments, job_id):
|
|
|
1086
1111
|
raise
|
|
1087
1112
|
warnings.append("Lexical index not updated; results from the last ready index. " + _project_error(exc))
|
|
1088
1113
|
search_model = None
|
|
1114
|
+
elif _cold_index(root, cfg, problem):
|
|
1115
|
+
overlay, notice = _cold_overlay(root, texts)
|
|
1116
|
+
search_model = None
|
|
1117
|
+
warnings += [text for text in (notice, _refresh_in_background(root, {"refresh": True})) if text]
|
|
1089
1118
|
elif problem and not local_embedder.is_local(model):
|
|
1090
1119
|
if not indexer.info(root).get("chunks"):
|
|
1091
1120
|
raise RuntimeError(problem)
|
|
@@ -1434,13 +1463,13 @@ def _handle_smart_search(arguments):
|
|
|
1434
1463
|
job_id, future = _submit_project_job("smart_search", arguments)
|
|
1435
1464
|
if future is None:
|
|
1436
1465
|
_metric("pending", reason="queued")
|
|
1437
|
-
return
|
|
1466
|
+
return _agent_search_answer(_smart_search_job_payload(job_id))
|
|
1438
1467
|
try:
|
|
1439
1468
|
result = future.result(timeout=SMART_SEARCH_SYNC_WAIT_S)
|
|
1440
1469
|
except concurrent.futures.TimeoutError:
|
|
1441
1470
|
if not future.done():
|
|
1442
1471
|
_metric("pending", reason="sync_timeout")
|
|
1443
|
-
return
|
|
1472
|
+
return _agent_search_answer(_smart_search_job_payload(job_id))
|
|
1444
1473
|
result = future.result()
|
|
1445
1474
|
_mark_notice_delivered(job_id)
|
|
1446
1475
|
return result
|
|
@@ -1546,9 +1575,27 @@ def _handle_smart_search_result(arguments):
|
|
|
1546
1575
|
time.sleep(0.25)
|
|
1547
1576
|
payload = _smart_search_job_payload(job_id)
|
|
1548
1577
|
_mark_notice_delivered(job_id)
|
|
1578
|
+
if payload["kind"] == "smart_search":
|
|
1579
|
+
return _agent_search_answer(payload)
|
|
1549
1580
|
return _to_json_sanitized(payload)
|
|
1550
1581
|
|
|
1551
1582
|
|
|
1583
|
+
_AGENT_SEARCH_DROPPED = ("kind", "project_id", "updated_at", "stats", "view")
|
|
1584
|
+
|
|
1585
|
+
|
|
1586
|
+
def _agent_search_answer(payload):
|
|
1587
|
+
"""What smart_search_result hands the agent for a smart_search job: the result text itself when done (the same text
|
|
1588
|
+
a search that finishes within the wait returns, no JSON wrapper that escapes it and repeats the index stats and
|
|
1589
|
+
the git view the setup page shows), otherwise the payload without those fields."""
|
|
1590
|
+
if payload["status"] == "done":
|
|
1591
|
+
warning = payload.get("warning")
|
|
1592
|
+
return f"{payload['result']}\nwarning: {warning}" if warning else payload["result"]
|
|
1593
|
+
slim = {key: value for key, value in payload.items() if key not in _AGENT_SEARCH_DROPPED}
|
|
1594
|
+
if not slim.get("cancel_requested"):
|
|
1595
|
+
slim.pop("cancel_requested", None)
|
|
1596
|
+
return _to_json_sanitized(slim)
|
|
1597
|
+
|
|
1598
|
+
|
|
1552
1599
|
def _project_payload(project):
|
|
1553
1600
|
data = dict(project)
|
|
1554
1601
|
data['dismissed_duplicates'] = len(data.get('dismissed_duplicates') or {})
|
|
@@ -3528,6 +3575,60 @@ def _web_route(tool, url):
|
|
|
3528
3575
|
|
|
3529
3576
|
|
|
3530
3577
|
HOOK_MAX_BODY_BYTES = 4 * 1024 * 1024
|
|
3578
|
+
# The client cancels a PreToolUse hook at its own timeout and lets the tool run, but the agent has waited all of it
|
|
3579
|
+
# (75 cancellations of 15 s in 9 days, 62 of them Write/Edit). The decision gets this much; past it the hook answers
|
|
3580
|
+
# without one and says so in the log. Edit reviews (docs, copies) are the slow ones, so they have their own small
|
|
3581
|
+
# pool: at most HOOK_EDIT_SLOTS run at once and a further edit is let through unreviewed instead of queueing.
|
|
3582
|
+
HOOK_BUDGET_S = 4.0
|
|
3583
|
+
HOOK_ANALYZER_TIMEOUT_S = 10
|
|
3584
|
+
HOOK_SLOW_LOG_S = 1.0
|
|
3585
|
+
HOOK_EDIT_SLOTS = 2
|
|
3586
|
+
_HOOK_POOL = concurrent.futures.ThreadPoolExecutor(4, thread_name_prefix="hook")
|
|
3587
|
+
_HOOK_EDIT_POOL = concurrent.futures.ThreadPoolExecutor(HOOK_EDIT_SLOTS, thread_name_prefix="hook-edit")
|
|
3588
|
+
_HOOK_EDIT_SLOTS = threading.BoundedSemaphore(HOOK_EDIT_SLOTS)
|
|
3589
|
+
|
|
3590
|
+
|
|
3591
|
+
def _hook_work(payload, client, slot):
|
|
3592
|
+
"""`hook_decision.decide` on a hook worker, with the analyzer timeout lowered and the edit slot released at the end
|
|
3593
|
+
(the worker may outlive the request that gave up on it)."""
|
|
3594
|
+
code_graph.ANALYZER_TIMEOUT_S.set(HOOK_ANALYZER_TIMEOUT_S)
|
|
3595
|
+
try:
|
|
3596
|
+
return hook_decision.decide(payload, client, _web_route)
|
|
3597
|
+
finally:
|
|
3598
|
+
if slot:
|
|
3599
|
+
slot.release()
|
|
3600
|
+
|
|
3601
|
+
|
|
3602
|
+
def _decide_within_budget(payload, client):
|
|
3603
|
+
"""The hook decision of `payload`, or None when it did not come within HOOK_BUDGET_S or no edit slot was free (the
|
|
3604
|
+
tool then runs as if the hook had nothing to say). Both cases, and any answer slower than HOOK_SLOW_LOG_S, leave one
|
|
3605
|
+
line in the daemon log with the tool, the file or command and the time."""
|
|
3606
|
+
tool = payload.get("tool_name") if isinstance(payload, dict) else None
|
|
3607
|
+
tool_input = payload.get("tool_input") if isinstance(payload, dict) else None
|
|
3608
|
+
target = str((tool_input or {}).get("file_path") or (tool_input or {}).get("command") or "")[:80]
|
|
3609
|
+
edit = tool in hook_decision.EDIT_TOOLS
|
|
3610
|
+
if edit and not _HOOK_EDIT_SLOTS.acquire(blocking=False):
|
|
3611
|
+
_log(f"PreToolUse hook skipped the review of {tool} {target}: {HOOK_EDIT_SLOTS} edit reviews already running, "
|
|
3612
|
+
"the edit goes through without one")
|
|
3613
|
+
return None
|
|
3614
|
+
started = time.monotonic()
|
|
3615
|
+
try:
|
|
3616
|
+
future = (_HOOK_EDIT_POOL if edit else _HOOK_POOL).submit(contextvars.copy_context().run, _hook_work, payload,
|
|
3617
|
+
client, _HOOK_EDIT_SLOTS if edit else None)
|
|
3618
|
+
except BaseException:
|
|
3619
|
+
if edit:
|
|
3620
|
+
_HOOK_EDIT_SLOTS.release()
|
|
3621
|
+
raise
|
|
3622
|
+
try:
|
|
3623
|
+
output = future.result(timeout=HOOK_BUDGET_S)
|
|
3624
|
+
except concurrent.futures.TimeoutError:
|
|
3625
|
+
_log(f"PreToolUse hook over {HOOK_BUDGET_S:g} s for {tool} {target}: answered without a decision, the work "
|
|
3626
|
+
"finishes in the background")
|
|
3627
|
+
return None
|
|
3628
|
+
took = time.monotonic() - started
|
|
3629
|
+
if took > HOOK_SLOW_LOG_S:
|
|
3630
|
+
_log(f"PreToolUse hook slow: {tool} {target} took {took:.1f} s")
|
|
3631
|
+
return output
|
|
3531
3632
|
|
|
3532
3633
|
|
|
3533
3634
|
def _pretool_hook(handler):
|
|
@@ -3541,10 +3642,12 @@ def _pretool_hook(handler):
|
|
|
3541
3642
|
_note_edit(payload)
|
|
3542
3643
|
except Exception as exc:
|
|
3543
3644
|
_log(f"edit index update skipped: {type(exc).__name__}: {exc}")
|
|
3544
|
-
output =
|
|
3645
|
+
output = _decide_within_budget(payload, client or None)
|
|
3545
3646
|
except Exception as exc:
|
|
3546
3647
|
_log(f"PreToolUse hook without a decision: {type(exc).__name__}: {exc}")
|
|
3547
3648
|
return {}
|
|
3649
|
+
if output is None:
|
|
3650
|
+
return {}
|
|
3548
3651
|
specific = output.get("hookSpecificOutput") or {}
|
|
3549
3652
|
if specific.get("permissionDecision", "deny") != "deny":
|
|
3550
3653
|
return {}
|
package/version.py
CHANGED