@allansantos-dev/smart-tool 0.9.13 → 0.9.15
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 +33 -0
- package/README.md +12 -4
- package/client_hooks.py +1 -1
- package/hook_decision.py +8 -0
- package/integration_metrics.py +50 -0
- package/package.json +1 -1
- package/redirect_rule.py +401 -67
- package/router.py +16 -9
- package/setup_audit.py +173 -14
- package/setup_ui.py +176 -1
- package/smart_tool_daemon.py +4 -0
- package/version.py +1 -1
package/setup_audit.py
CHANGED
|
@@ -33,7 +33,11 @@ import endpoint_sync
|
|
|
33
33
|
import paths
|
|
34
34
|
|
|
35
35
|
REPORT_DIR = os.path.join(paths.DATA_DIR, "setup-audit")
|
|
36
|
+
INDEX_NAME = "index.json"
|
|
36
37
|
PROBE_TIMEOUT_S = 15
|
|
38
|
+
SCHEDULE_S = 30 * 60
|
|
39
|
+
FIRST_RUN_DELAY_S = 120
|
|
40
|
+
PROBE_MAX_AGE_S = 6 * 3600
|
|
37
41
|
SKILL_DESCRIPTION_MAX = 1536
|
|
38
42
|
SKILL_BUDGET_CHARS = 8000
|
|
39
43
|
CODEX_PROJECT_DOC_MAX = 32 * 1024
|
|
@@ -192,13 +196,21 @@ def _ancestors(project):
|
|
|
192
196
|
current = parent
|
|
193
197
|
|
|
194
198
|
|
|
199
|
+
def _approvals(data, approval):
|
|
200
|
+
"""Folds one settings object's .mcp.json approvals (enableAllProjectMcpServers, enabledMcpjsonServers,
|
|
201
|
+
disabledMcpjsonServers) into approval."""
|
|
202
|
+
approval["all"] = approval["all"] or data.get("enableAllProjectMcpServers") is True
|
|
203
|
+
approval["on"].update(data.get("enabledMcpjsonServers") or [])
|
|
204
|
+
approval["off"].update(data.get("disabledMcpjsonServers") or [])
|
|
205
|
+
|
|
206
|
+
|
|
195
207
|
def collect_claude(project, snapshot):
|
|
196
208
|
"""Claude Code's effective set for project: settings layers, enabled plugins, hooks, MCP, skills, CLAUDE.md."""
|
|
197
209
|
layers = [(path, "managed") for path in _managed_settings()] + [
|
|
198
210
|
(os.path.join(endpoint_sync.claude_dir(), "settings.json"), "user"),
|
|
199
211
|
(os.path.join(project, ".claude", "settings.json"), "project"),
|
|
200
212
|
(os.path.join(project, ".claude", "settings.local.json"), "local")]
|
|
201
|
-
hooks, enabled, overrides, settings = [], {}, {}, []
|
|
213
|
+
hooks, enabled, overrides, settings, approval = [], {}, {}, [], {"all": False, "on": set(), "off": set()}
|
|
202
214
|
for path, scope in layers:
|
|
203
215
|
data = snapshot.json(path)
|
|
204
216
|
if data is None:
|
|
@@ -207,14 +219,19 @@ def collect_claude(project, snapshot):
|
|
|
207
219
|
hooks += _hooks_from(data, f"settings:{scope}")
|
|
208
220
|
enabled.update({k: v for k, v in (data.get("enabledPlugins") or {}).items()})
|
|
209
221
|
overrides.update(data.get("skillOverrides") or {})
|
|
222
|
+
_approvals(data, approval)
|
|
210
223
|
disabled_skills = {name for name, value in overrides.items() if value == "off"}
|
|
211
224
|
state = snapshot.json(endpoint_sync.claude_json()) or {}
|
|
212
225
|
mcp = _mcp_entries(state.get("mcpServers"), "user", endpoint_sync.claude_json())
|
|
213
226
|
project_state = (state.get("projects") or {}).get(os.path.abspath(project).replace("\\", "/")) or \
|
|
214
227
|
(state.get("projects") or {}).get(os.path.abspath(project)) or {}
|
|
215
228
|
mcp += _mcp_entries(project_state.get("mcpServers"), "local", endpoint_sync.claude_json())
|
|
229
|
+
_approvals(project_state, approval)
|
|
216
230
|
project_mcp = snapshot.json(os.path.join(project, ".mcp.json")) or {}
|
|
217
|
-
|
|
231
|
+
shared = _mcp_entries(project_mcp.get("mcpServers"), "project", os.path.join(project, ".mcp.json"))
|
|
232
|
+
unapproved = [e["name"] for e in shared
|
|
233
|
+
if e["name"] in approval["off"] or not (approval["all"] or e["name"] in approval["on"])]
|
|
234
|
+
mcp += [e for e in shared if e["name"] not in unapproved]
|
|
218
235
|
skills = _skills(snapshot, os.path.join(endpoint_sync.claude_dir(), "skills"), "user", disabled=disabled_skills)
|
|
219
236
|
skills += _skills(snapshot, os.path.join(project, ".claude", "skills"), "project", disabled=disabled_skills)
|
|
220
237
|
plugins = []
|
|
@@ -245,7 +262,7 @@ def collect_claude(project, snapshot):
|
|
|
245
262
|
if text is not None:
|
|
246
263
|
instructions.append({"path": path, "kind": kind, "bytes": len(text.encode("utf-8")), "_text": text})
|
|
247
264
|
return {"settings": settings, "plugins": plugins, "hooks": hooks, "mcp": mcp, "skills": skills,
|
|
248
|
-
"instructions": instructions}
|
|
265
|
+
"instructions": instructions, "unapproved_mcp": unapproved}
|
|
249
266
|
|
|
250
267
|
|
|
251
268
|
def collect_codex(project, snapshot):
|
|
@@ -513,16 +530,40 @@ def _rpc_http(entry, timeout):
|
|
|
513
530
|
return {"error": str(getattr(exc, "reason", exc)), "kind": "down"}
|
|
514
531
|
|
|
515
532
|
|
|
516
|
-
|
|
517
|
-
|
|
533
|
+
_PROBES = {}
|
|
534
|
+
_PROBES_LOCK = threading.Lock()
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
def _probe_key(entry, cwd):
|
|
538
|
+
"""Identity of a server launch (secrets only as a hash), shared by the projects and harnesses that register it;
|
|
539
|
+
the folder counts for servers a project registers itself and for arguments that may be relative paths."""
|
|
540
|
+
relative = any(not a.startswith("-") and not os.path.isabs(a) and ("/" in a or "\\" in a or "." in a)
|
|
541
|
+
for a in entry["args"])
|
|
542
|
+
per_project = entry["transport"] == "stdio" and (entry["scope"] in ("project", "local") or relative)
|
|
543
|
+
spec = [entry["transport"], entry.get("command"), entry["args"], entry.get("url"), sorted(entry["_env"].items()),
|
|
544
|
+
sorted(entry["_headers"].items()), entry.get("_bearer_env"), cwd if per_project else None]
|
|
545
|
+
return hashlib.sha256(json.dumps(spec, default=str).encode()).hexdigest()
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def probe_mcp(entries, cwd, timeout=PROBE_TIMEOUT_S, max_age=0):
|
|
549
|
+
"""Tools of each MCP server, started or called as a session would (in parallel). With max_age, a result of the
|
|
550
|
+
same launch younger than that is reused (failures never are: a server fixed meanwhile shows at once): the periodic
|
|
551
|
+
audit does not start every server of every project each time. Each result carries `probed_at`."""
|
|
518
552
|
def one(entry):
|
|
553
|
+
key = _probe_key(entry, cwd)
|
|
554
|
+
with _PROBES_LOCK:
|
|
555
|
+
cached = _PROBES.get(key)
|
|
556
|
+
if max_age and cached and not cached.get("error") and time.time() - cached["probed_at"] < max_age:
|
|
557
|
+
return cached
|
|
519
558
|
# A server whose config cannot even be used (a URL with an unexpanded variable, a bad port) is a finding.
|
|
520
559
|
try:
|
|
521
|
-
if entry["transport"] == "http"
|
|
522
|
-
return _rpc_http(entry, timeout)
|
|
523
|
-
return _rpc_stdio(entry, cwd, timeout)
|
|
560
|
+
result = _rpc_http(entry, timeout) if entry["transport"] == "http" else _rpc_stdio(entry, cwd, timeout)
|
|
524
561
|
except Exception as exc:
|
|
525
|
-
|
|
562
|
+
result = {"error": f"{type(exc).__name__}: {exc}"[:200], "kind": "down"}
|
|
563
|
+
result["probed_at"] = time.time()
|
|
564
|
+
with _PROBES_LOCK:
|
|
565
|
+
_PROBES[key] = result
|
|
566
|
+
return result
|
|
526
567
|
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
|
|
527
568
|
return list(pool.map(one, entries))
|
|
528
569
|
|
|
@@ -578,9 +619,10 @@ def _public(section):
|
|
|
578
619
|
return out
|
|
579
620
|
|
|
580
621
|
|
|
581
|
-
def run(project, probe=True, save=True):
|
|
622
|
+
def run(project, probe=True, save=True, probe_max_age=0, trigger="manual"):
|
|
582
623
|
"""Collects both harnesses for project, runs every check and returns the report; with save, stores it and
|
|
583
|
-
marks findings new or resolved against the previous report of the same project.
|
|
624
|
+
marks findings new or resolved against the previous report of the same project. probe_max_age reuses recent
|
|
625
|
+
MCP probes (periodic runs); trigger records who asked (manual or schedule)."""
|
|
584
626
|
project = os.path.abspath(project)
|
|
585
627
|
snapshot = _Snapshot()
|
|
586
628
|
claude, codex = collect_claude(project, snapshot), collect_codex(project, snapshot)
|
|
@@ -590,12 +632,16 @@ def run(project, probe=True, save=True):
|
|
|
590
632
|
findings += check_hook_overlap(harness, data["hooks"], tools)
|
|
591
633
|
findings += check_hook_targets(harness, data["hooks"], names, probe)
|
|
592
634
|
findings += check_skills(harness, data["skills"])
|
|
593
|
-
results = probe_mcp(data["mcp"], project) if probe
|
|
635
|
+
results = (probe_mcp(data["mcp"], project, max_age=probe_max_age) if probe
|
|
636
|
+
else [None] * len(data["mcp"]))
|
|
594
637
|
findings += check_mcp(harness, data["mcp"], results)
|
|
595
638
|
findings += check_instructions(project, claude, codex)
|
|
596
639
|
unverified = list(snapshot.unverified)
|
|
597
640
|
if not probe:
|
|
598
641
|
unverified.append("MCP servers and HTTP hook endpoints were not contacted (probe off)")
|
|
642
|
+
for name in claude["unapproved_mcp"]:
|
|
643
|
+
unverified.append(f".mcp.json server '{name}' is not approved in Claude Code, so it is neither loaded nor "
|
|
644
|
+
"started here")
|
|
599
645
|
if codex.get("plugins_unverified"):
|
|
600
646
|
unverified.append("Codex plugins (their skills and hooks) are not collected yet")
|
|
601
647
|
unverified += ["skill trigger overlap and contradictions between instructions need a model (not run)",
|
|
@@ -603,7 +649,7 @@ def run(project, probe=True, save=True):
|
|
|
603
649
|
key = hashlib.sha256(json.dumps(sorted(snapshot.files.items())).encode()).hexdigest()[:16]
|
|
604
650
|
order = {"error": 0, "warn": 1, "info": 2}
|
|
605
651
|
findings.sort(key=lambda f: (order[f["severity"]], f["harness"], f["check"], f["subject"]))
|
|
606
|
-
report = {"project": project, "checked_at": time.time(), "snapshot_key": key, "probe": probe,
|
|
652
|
+
report = {"project": project, "checked_at": time.time(), "snapshot_key": key, "probe": probe, "trigger": trigger,
|
|
607
653
|
"summary": {s: sum(f["severity"] == s for f in findings) for s in order},
|
|
608
654
|
"findings": findings, "unverified": unverified,
|
|
609
655
|
"claude": _public(claude), "codex": _public(codex), "files": len(snapshot.files)}
|
|
@@ -612,9 +658,70 @@ def run(project, probe=True, save=True):
|
|
|
612
658
|
return report
|
|
613
659
|
|
|
614
660
|
|
|
661
|
+
def _report_path(project):
|
|
662
|
+
return os.path.join(REPORT_DIR, hashlib.sha1(os.path.abspath(project).lower().encode()).hexdigest()[:12] + ".json")
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
def last_report(project):
|
|
666
|
+
"""The last stored report of project, or None when it was never audited."""
|
|
667
|
+
try:
|
|
668
|
+
with open(_report_path(project), encoding="utf-8") as stream:
|
|
669
|
+
return json.load(stream)
|
|
670
|
+
except FileNotFoundError:
|
|
671
|
+
return None
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def index():
|
|
675
|
+
"""{project: {summary, new, resolved, checked_at, trigger}} of every audited project (the periodic summary)."""
|
|
676
|
+
try:
|
|
677
|
+
with open(os.path.join(REPORT_DIR, INDEX_NAME), encoding="utf-8") as stream:
|
|
678
|
+
data = json.load(stream)
|
|
679
|
+
except FileNotFoundError:
|
|
680
|
+
return {}
|
|
681
|
+
if not isinstance(data, dict):
|
|
682
|
+
raise ValueError(f"{os.path.join(REPORT_DIR, INDEX_NAME)} is not a setup audit index: fix or delete it.")
|
|
683
|
+
return data
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
_INDEX_LOCK = threading.Lock()
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
def index_key(project):
|
|
690
|
+
"""The index key of a folder: the same for any spelling of it on this OS."""
|
|
691
|
+
return os.path.normcase(os.path.abspath(project))
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def _write_index(data):
|
|
695
|
+
atomic_io.write_secret_text(os.path.join(REPORT_DIR, INDEX_NAME), json.dumps(data, indent=1, ensure_ascii=False))
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
def prune_index(projects):
|
|
699
|
+
"""Drops the index entries and reports of folders no longer registered."""
|
|
700
|
+
keep = {index_key(p) for p in projects}
|
|
701
|
+
with _INDEX_LOCK:
|
|
702
|
+
data = index()
|
|
703
|
+
gone = [key for key in data if key not in keep]
|
|
704
|
+
for key in gone:
|
|
705
|
+
data.pop(key)
|
|
706
|
+
try:
|
|
707
|
+
os.remove(_report_path(key))
|
|
708
|
+
except FileNotFoundError:
|
|
709
|
+
pass
|
|
710
|
+
if gone:
|
|
711
|
+
_write_index(data)
|
|
712
|
+
return gone
|
|
713
|
+
|
|
714
|
+
|
|
615
715
|
def _save_with_diff(report):
|
|
716
|
+
"""Stores the report, marks findings new or resolved against the previous one and updates the index, all under
|
|
717
|
+
one lock: the scheduler, Run now and project_manage may audit the same folder at once."""
|
|
718
|
+
with _INDEX_LOCK:
|
|
719
|
+
_save_locked(report)
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
def _save_locked(report):
|
|
616
723
|
os.makedirs(REPORT_DIR, exist_ok=True)
|
|
617
|
-
last_path =
|
|
724
|
+
last_path = _report_path(report["project"])
|
|
618
725
|
try:
|
|
619
726
|
with open(last_path, encoding="utf-8") as stream:
|
|
620
727
|
previous = json.load(stream)
|
|
@@ -629,6 +736,58 @@ def _save_with_diff(report):
|
|
|
629
736
|
for finding in report["findings"]:
|
|
630
737
|
finding["new"] = finding["id"] not in before
|
|
631
738
|
atomic_io.write_secret_text(last_path, json.dumps(report, indent=1, ensure_ascii=False))
|
|
739
|
+
changes = report.get("changes") or {}
|
|
740
|
+
data = index()
|
|
741
|
+
data[index_key(report["project"])] = {"summary": report["summary"], "new": len(changes.get("new", [])),
|
|
742
|
+
"resolved": len(changes.get("resolved", [])),
|
|
743
|
+
"checked_at": report["checked_at"], "trigger": report["trigger"],
|
|
744
|
+
"snapshot_key": report["snapshot_key"]}
|
|
745
|
+
_write_index(data)
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
class Scheduler:
|
|
749
|
+
"""Periodic mode: audits every registered project whose folder exists, FIRST_RUN_DELAY_S after start and then
|
|
750
|
+
every SCHEDULE_S, one project at a time, reusing MCP probes younger than PROBE_MAX_AGE_S. projects() returns the
|
|
751
|
+
folders; log gets one line per run and every failure (a failing project never stops the others)."""
|
|
752
|
+
|
|
753
|
+
def __init__(self, projects, log, interval=SCHEDULE_S, first_delay=FIRST_RUN_DELAY_S):
|
|
754
|
+
self._projects, self._log, self._interval, self._first_delay = projects, log, interval, first_delay
|
|
755
|
+
self._stop = threading.Event()
|
|
756
|
+
self._thread = None
|
|
757
|
+
|
|
758
|
+
def start(self):
|
|
759
|
+
self._thread = threading.Thread(target=self._loop, name="setup-audit", daemon=True)
|
|
760
|
+
self._thread.start()
|
|
761
|
+
|
|
762
|
+
def stop(self):
|
|
763
|
+
self._stop.set()
|
|
764
|
+
|
|
765
|
+
def run_once(self):
|
|
766
|
+
"""Audits each project once; returns {project: summary or error text}."""
|
|
767
|
+
outcome, projects = {}, list(self._projects())
|
|
768
|
+
prune_index(projects)
|
|
769
|
+
for root in projects:
|
|
770
|
+
if self._stop.is_set():
|
|
771
|
+
break
|
|
772
|
+
if not os.path.isdir(root):
|
|
773
|
+
continue
|
|
774
|
+
try:
|
|
775
|
+
report = run(root, probe_max_age=PROBE_MAX_AGE_S, trigger="schedule")
|
|
776
|
+
outcome[root] = report["summary"]
|
|
777
|
+
except Exception as exc:
|
|
778
|
+
outcome[root] = f"{type(exc).__name__}: {exc}"[:200]
|
|
779
|
+
self._log(f"setup audit failed for {root}: {outcome[root]}")
|
|
780
|
+
errors = sum(v["error"] for v in outcome.values() if isinstance(v, dict))
|
|
781
|
+
self._log(f"setup audit: {len(outcome)} project(s), {errors} error finding(s)")
|
|
782
|
+
return outcome
|
|
783
|
+
|
|
784
|
+
def _loop(self):
|
|
785
|
+
if self._stop.wait(self._first_delay):
|
|
786
|
+
return
|
|
787
|
+
while True:
|
|
788
|
+
self.run_once()
|
|
789
|
+
if self._stop.wait(self._interval):
|
|
790
|
+
return
|
|
632
791
|
|
|
633
792
|
|
|
634
793
|
def markdown(report):
|
package/setup_ui.py
CHANGED
|
@@ -29,7 +29,10 @@ import gateway
|
|
|
29
29
|
import model_catalog
|
|
30
30
|
import model_defaults
|
|
31
31
|
import local_embedder
|
|
32
|
+
import project_store
|
|
32
33
|
import secret_store
|
|
34
|
+
import setup_audit
|
|
35
|
+
import integration_metrics
|
|
33
36
|
import version
|
|
34
37
|
import web_search_external
|
|
35
38
|
|
|
@@ -206,7 +209,8 @@ def handle_integration_get():
|
|
|
206
209
|
"duplicate_mode_error": duplicate_error, "duplicate_modes": list(config.DUPLICATE_MODES),
|
|
207
210
|
"pattern_proposals": proposals_mode, "pattern_proposals_error": proposals_error,
|
|
208
211
|
"block_patterns": {"proposals": stored["proposals"], "accepted": stored["accepted"],
|
|
209
|
-
"rejected": len(stored["rejected"]), "error": patterns_error}
|
|
212
|
+
"rejected": len(stored["rejected"]), "error": patterns_error},
|
|
213
|
+
"redirects": integration_metrics.redirects()}
|
|
210
214
|
|
|
211
215
|
|
|
212
216
|
def handle_integration_save(body):
|
|
@@ -251,6 +255,30 @@ def handle_integration_save(body):
|
|
|
251
255
|
raise SetupError("Invalid action: use hook_mode, register_mcp, browser_control, preview or install.")
|
|
252
256
|
|
|
253
257
|
|
|
258
|
+
def handle_audit(body):
|
|
259
|
+
"""Setup audit panel: `index` lists the registered projects with their last audit summary; `report` returns the
|
|
260
|
+
last stored report of one of them; `run` audits it now (MCP servers contacted unless probe is false). Only
|
|
261
|
+
registered project folders are accepted."""
|
|
262
|
+
if not isinstance(body, dict):
|
|
263
|
+
raise SetupError("Invalid request.")
|
|
264
|
+
roots = sorted({p["root"] for p in project_store.all_projects() if p.get("root")}, key=str.casefold)
|
|
265
|
+
try:
|
|
266
|
+
if body.get("action") == "index":
|
|
267
|
+
stored = setup_audit.index()
|
|
268
|
+
found = {root: stored[setup_audit.index_key(root)] for root in roots if setup_audit.index_key(root) in stored}
|
|
269
|
+
return {"projects": roots, "index": found, "schedule_s": setup_audit.SCHEDULE_S}
|
|
270
|
+
root = body.get("project_root")
|
|
271
|
+
if root not in roots:
|
|
272
|
+
raise SetupError("Choose one of the registered projects.")
|
|
273
|
+
if body.get("action") == "report":
|
|
274
|
+
return {"report": setup_audit.last_report(root)}
|
|
275
|
+
if body.get("action") == "run":
|
|
276
|
+
return {"report": setup_audit.run(root, probe=body.get("probe") is not False)}
|
|
277
|
+
except (OSError, ValueError) as exc:
|
|
278
|
+
raise SetupError(str(exc))
|
|
279
|
+
raise SetupError("Invalid action: use index, report or run.")
|
|
280
|
+
|
|
281
|
+
|
|
254
282
|
def handle_providers_get():
|
|
255
283
|
return {"providers": {
|
|
256
284
|
name: {**web_search_external.PROVIDERS[name], "status": config.provider_key_status(name)}
|
|
@@ -498,6 +526,15 @@ _PAGE_TEMPLATE = """<!doctype html>
|
|
|
498
526
|
.pattern-actions button { padding: 4px 10px; font-size: .78rem; }
|
|
499
527
|
.pattern-pager { justify-content: space-between; }
|
|
500
528
|
.pattern-blocking > summary { cursor: pointer; font: 600 .9rem var(--display); }
|
|
529
|
+
.audit-list { list-style: none; padding: 0; margin: 10px 0; }
|
|
530
|
+
.audit-row { padding: 10px 0; border-bottom: 1px solid var(--line); }
|
|
531
|
+
.audit-head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
|
|
532
|
+
.audit-sev { font: 600 .72rem var(--display); letter-spacing: .06em; text-transform: uppercase; }
|
|
533
|
+
.audit-error { color: var(--bad); }
|
|
534
|
+
.audit-warn { color: var(--warn); }
|
|
535
|
+
.audit-info { color: var(--muted); }
|
|
536
|
+
.audit-new { font: 600 .72rem var(--display); color: var(--signal); }
|
|
537
|
+
.audit-detail { font-size: .84rem; margin: 4px 0 0; overflow-wrap: anywhere; }
|
|
501
538
|
|
|
502
539
|
body:not(.ready) .station[data-live="1"] .wire::after { transform: scaleX(0); }
|
|
503
540
|
@media (max-width: 900px) { .panels { grid-template-columns: minmax(0, 1fr); } }
|
|
@@ -655,10 +692,22 @@ _PAGE_TEMPLATE = """<!doctype html>
|
|
|
655
692
|
<span class="hint">The hook decides with a fixed rule: content searches over a project folder go to smart_search. The scope model (else the router model) never delays a call: it reviews, afterwards, bulk reads the rule could not measure and proposes patterns, each checked against the logged calls. Nothing blocks until you accept it here.</span></div>
|
|
656
693
|
<div class="actions"><button type="button" id="btn-save-pattern-proposals">Save proposals setting</button></div>
|
|
657
694
|
<div id="patterns-list"></div>
|
|
695
|
+
<div id="redirects-panel" class="pattern-panel"></div>
|
|
658
696
|
<div class="msg" id="agents-msg" role="status"></div>
|
|
659
697
|
<div id="agents-list"></div>
|
|
660
698
|
</section>
|
|
661
699
|
|
|
700
|
+
<section id="audit-section">
|
|
701
|
+
<h2>Setup audit</h2>
|
|
702
|
+
<p class="lede">What Claude Code and Codex load for a project, and where they collide: hooks deciding on the same tool at once, hooks or MCP servers that do not work, tools offered twice, skills and instructions one agent never sees. It runs on its own every 30 minutes for the registered projects; nothing is changed.</p>
|
|
703
|
+
<div class="field"><label for="sel-audit-project">Project</label>
|
|
704
|
+
<select id="sel-audit-project"></select>
|
|
705
|
+
<span class="hint" id="audit-hint"></span></div>
|
|
706
|
+
<div class="actions"><button type="button" id="btn-audit-run">Run now</button></div>
|
|
707
|
+
<div class="msg" id="audit-msg" role="status"></div>
|
|
708
|
+
<div id="audit-report"></div>
|
|
709
|
+
</section>
|
|
710
|
+
|
|
662
711
|
<section id="providers-section">
|
|
663
712
|
<h2>Web search providers</h2>
|
|
664
713
|
<p class="lede">All of them work without a key, on each provider's public free tier. Add your own key only if you need more volume: it is tested with a real search before being saved and is protected by DPAPI on this Windows machine.</p>
|
|
@@ -956,6 +1005,7 @@ async function loadAgents() {
|
|
|
956
1005
|
if (document.activeElement !== $("sel-pattern-proposals")) $("sel-pattern-proposals").value = data.pattern_proposals;
|
|
957
1006
|
if (data.pattern_proposals_error) setMsg("agents-msg", "error", data.pattern_proposals_error);
|
|
958
1007
|
renderPatterns(data.block_patterns);
|
|
1008
|
+
renderRedirects(data.redirects);
|
|
959
1009
|
const list = $("agents-list");
|
|
960
1010
|
list.textContent = "";
|
|
961
1011
|
for (const item of data.clients) {
|
|
@@ -1103,6 +1153,23 @@ function patternRow(item, actions, selectable) {
|
|
|
1103
1153
|
row.appendChild(main);
|
|
1104
1154
|
return row;
|
|
1105
1155
|
}
|
|
1156
|
+
function renderRedirects(stats) {
|
|
1157
|
+
const panel = $("redirects-panel");
|
|
1158
|
+
panel.textContent = "";
|
|
1159
|
+
panel.append(patternEl("h3", "", "Native searches redirected (last " + stats.days + " days): " + stats.total));
|
|
1160
|
+
const out = stats.outcomes, tokens = stats.tokens_per_call;
|
|
1161
|
+
const lines = [
|
|
1162
|
+
"By tool: " + (Object.entries(stats.by_tool).map(([k, v]) => k + " " + v).join(", ") || "none"),
|
|
1163
|
+
"Next call of the same session: smart_search " + (out.followed || 0) + ", allowed native call " + (out.native || 0)
|
|
1164
|
+
+ ", blocked again " + (out.blocked_again || 0) + ", none within 15 min " + (out.no_other_call || 0)
|
|
1165
|
+
+ ", unknown (no session logged) " + (out.unknown || 0),
|
|
1166
|
+
"Context per call (measured " + tokens.measured + "): a native call of the kinds redirected ~" + tokens.native_redirected
|
|
1167
|
+
+ " tokens; a smart_search round trip ~" + tokens.smart_search + " tokens. A redirect saves context only when one"
|
|
1168
|
+
+ " smart_search replaces several native searches and reads; that part is not measured yet.",
|
|
1169
|
+
];
|
|
1170
|
+
for (const line of lines) panel.append(patternEl("p", "hint", line));
|
|
1171
|
+
for (const [reason, count] of Object.entries(stats.by_reason)) panel.append(patternEl("p", "hint mono", count + " × " + reason));
|
|
1172
|
+
}
|
|
1106
1173
|
function renderPatterns(patterns) {
|
|
1107
1174
|
lastPatterns = patterns;
|
|
1108
1175
|
const box = $("patterns-list");
|
|
@@ -1226,6 +1293,114 @@ $("btn-save-hook-mode").addEventListener("click", async () => {
|
|
|
1226
1293
|
}
|
|
1227
1294
|
});
|
|
1228
1295
|
|
|
1296
|
+
const AUDIT_SEVERITY = { error: "Error", warn: "Warning", info: "Note" };
|
|
1297
|
+
let auditProjects = [];
|
|
1298
|
+
function auditWhen(seconds) {
|
|
1299
|
+
return seconds ? new Date(seconds * 1000).toLocaleString() : "never";
|
|
1300
|
+
}
|
|
1301
|
+
function renderAudit(report) {
|
|
1302
|
+
const box = $("audit-report");
|
|
1303
|
+
box.replaceChildren();
|
|
1304
|
+
if (!report) {
|
|
1305
|
+
box.appendChild(patternEl("p", "hint", "Not audited yet: the next scheduled run covers it, or press Run now."));
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
const s = report.summary;
|
|
1309
|
+
box.appendChild(patternEl("p", "", s.error + " error(s), " + s.warn + " warning(s), " + s.info + " note(s) · "
|
|
1310
|
+
+ (report.trigger === "schedule" ? "scheduled run" : "run on request") + " at " + auditWhen(report.checked_at)));
|
|
1311
|
+
const changes = report.changes;
|
|
1312
|
+
if (changes) box.appendChild(patternEl("p", "hint", "Since the previous run: " + changes.new.length + " new, "
|
|
1313
|
+
+ changes.resolved.length + " resolved" + (changes.snapshot_changed ? " (configuration changed)." : ".")));
|
|
1314
|
+
const list = patternEl("ul", "audit-list");
|
|
1315
|
+
for (const finding of report.findings) {
|
|
1316
|
+
const row = patternEl("li", "audit-row");
|
|
1317
|
+
const head = patternEl("div", "audit-head");
|
|
1318
|
+
head.append(patternEl("span", "audit-sev audit-" + finding.severity, AUDIT_SEVERITY[finding.severity] || finding.severity),
|
|
1319
|
+
patternEl("span", "pattern-tool", finding.harness), patternEl("strong", "", finding.subject));
|
|
1320
|
+
if (finding.new) head.appendChild(patternEl("span", "audit-new", "new"));
|
|
1321
|
+
const detail = patternEl("p", "audit-detail", finding.detail + " ");
|
|
1322
|
+
if ((finding.source || "").startsWith("https://")) {
|
|
1323
|
+
const link = patternEl("a", "", "documentation");
|
|
1324
|
+
link.setAttribute("href", finding.source);
|
|
1325
|
+
link.setAttribute("target", "_blank");
|
|
1326
|
+
link.setAttribute("rel", "noopener noreferrer");
|
|
1327
|
+
detail.appendChild(link);
|
|
1328
|
+
}
|
|
1329
|
+
row.append(head, detail);
|
|
1330
|
+
list.appendChild(row);
|
|
1331
|
+
}
|
|
1332
|
+
if (report.findings.length) box.appendChild(list);
|
|
1333
|
+
else box.appendChild(patternEl("p", "msg ok", "No collision or broken piece found."));
|
|
1334
|
+
if (changes && changes.resolved.length) {
|
|
1335
|
+
const resolved = patternEl("details", "");
|
|
1336
|
+
resolved.appendChild(patternEl("summary", "", "Resolved since the previous run (" + changes.resolved.length + ")"));
|
|
1337
|
+
const items = patternEl("ul", "audit-list");
|
|
1338
|
+
for (const finding of changes.resolved) items.appendChild(patternEl("li", "audit-detail", finding.subject + ": " + finding.detail));
|
|
1339
|
+
resolved.appendChild(items);
|
|
1340
|
+
box.appendChild(resolved);
|
|
1341
|
+
}
|
|
1342
|
+
const costs = [];
|
|
1343
|
+
for (const side of ["claude", "codex"]) {
|
|
1344
|
+
const servers = ((report[side] || {}).mcp || []).filter((m) => m.tools !== undefined);
|
|
1345
|
+
if (servers.length) costs.push(side + ": " + servers.map((m) => m.name + " " + m.tools + " tools (~"
|
|
1346
|
+
+ Math.round(m.tool_chars / 4).toLocaleString() + " tokens)").join(", "));
|
|
1347
|
+
}
|
|
1348
|
+
if (costs.length) box.appendChild(patternEl("p", "hint", "MCP tools offered — " + costs.join(" · ")));
|
|
1349
|
+
const unverified = patternEl("details", "");
|
|
1350
|
+
unverified.appendChild(patternEl("summary", "", "Not verified (" + report.unverified.length + ")"));
|
|
1351
|
+
const items = patternEl("ul", "audit-list");
|
|
1352
|
+
for (const item of report.unverified) items.appendChild(patternEl("li", "audit-detail", item));
|
|
1353
|
+
unverified.appendChild(items);
|
|
1354
|
+
box.appendChild(unverified);
|
|
1355
|
+
}
|
|
1356
|
+
async function showAudit(root) {
|
|
1357
|
+
try {
|
|
1358
|
+
renderAudit((await post("/setup/audit", { action: "report", project_root: root })).report);
|
|
1359
|
+
} catch (error) {
|
|
1360
|
+
setMsg("audit-msg", "error", error.message);
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
async function loadAudit() {
|
|
1364
|
+
try {
|
|
1365
|
+
const data = await post("/setup/audit", { action: "index" });
|
|
1366
|
+
auditProjects = data.projects;
|
|
1367
|
+
const select = $("sel-audit-project");
|
|
1368
|
+
select.replaceChildren();
|
|
1369
|
+
for (const root of data.projects) {
|
|
1370
|
+
const entry = data.index[root];
|
|
1371
|
+
const label = root + (entry ? " — " + entry.summary.error + " error(s), " + entry.summary.warn + " warning(s)" : " — not audited yet");
|
|
1372
|
+
const option = patternEl("option", "", label);
|
|
1373
|
+
option.value = root;
|
|
1374
|
+
select.appendChild(option);
|
|
1375
|
+
}
|
|
1376
|
+
$("audit-hint").textContent = data.projects.length ? "Runs every " + Math.round(data.schedule_s / 60) + " minutes." : "Register a project to audit it.";
|
|
1377
|
+
$("btn-audit-run").disabled = !data.projects.length;
|
|
1378
|
+
if (data.projects.length) {
|
|
1379
|
+
const worst = data.projects.slice().sort((a, b) => ((data.index[b] || {summary: {error: -1}}).summary.error)
|
|
1380
|
+
- ((data.index[a] || {summary: {error: -1}}).summary.error))[0];
|
|
1381
|
+
select.value = worst;
|
|
1382
|
+
await showAudit(worst);
|
|
1383
|
+
}
|
|
1384
|
+
} catch (error) {
|
|
1385
|
+
setMsg("audit-msg", "error", "Setup audit unavailable: " + error.message);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
$("sel-audit-project").addEventListener("change", () => showAudit($("sel-audit-project").value));
|
|
1389
|
+
$("btn-audit-run").addEventListener("click", async () => {
|
|
1390
|
+
const button = $("btn-audit-run");
|
|
1391
|
+
button.disabled = true;
|
|
1392
|
+
setMsg("audit-msg", "", "Auditing; MCP servers are being contacted…");
|
|
1393
|
+
try {
|
|
1394
|
+
renderAudit((await post("/setup/audit", { action: "run", project_root: $("sel-audit-project").value })).report);
|
|
1395
|
+
setMsg("audit-msg", "ok", "Audit finished.");
|
|
1396
|
+
} catch (error) {
|
|
1397
|
+
setMsg("audit-msg", "error", error.message);
|
|
1398
|
+
} finally {
|
|
1399
|
+
button.disabled = false;
|
|
1400
|
+
}
|
|
1401
|
+
});
|
|
1402
|
+
loadAudit();
|
|
1403
|
+
|
|
1229
1404
|
const providerLabels = { keyless: ["warn", "No key · free tier"], configured: ["ok", "Key configured"], unreadable: ["bad", "Key unreadable in this Windows profile"] };
|
|
1230
1405
|
function providerCard(name, info) {
|
|
1231
1406
|
const card = document.createElement("div");
|
package/smart_tool_daemon.py
CHANGED
|
@@ -3473,6 +3473,7 @@ _SETUP_SAVES = {
|
|
|
3473
3473
|
"/setup/gateway": setup_ui.handle_gateway_save,
|
|
3474
3474
|
"/setup/autostart": setup_ui.handle_autostart_action,
|
|
3475
3475
|
"/setup/contact": setup_ui.handle_contact_save,
|
|
3476
|
+
"/setup/audit": setup_ui.handle_audit,
|
|
3476
3477
|
}
|
|
3477
3478
|
|
|
3478
3479
|
|
|
@@ -3789,9 +3790,12 @@ def main():
|
|
|
3789
3790
|
_log(f"project monitor started; {recovered} interrupted job(s) recovered")
|
|
3790
3791
|
except Exception as exc:
|
|
3791
3792
|
_log(f"project monitor unavailable: {_project_error(exc)}")
|
|
3793
|
+
audit = setup_audit.Scheduler(lambda: [p["root"] for p in project_store.all_projects() if p.get("root")], _log)
|
|
3794
|
+
audit.start()
|
|
3792
3795
|
try:
|
|
3793
3796
|
server.serve_forever()
|
|
3794
3797
|
finally:
|
|
3798
|
+
audit.stop()
|
|
3795
3799
|
if _PROJECT_MONITOR:
|
|
3796
3800
|
_PROJECT_MONITOR.stop()
|
|
3797
3801
|
with _JOBS_LOCK:
|
package/version.py
CHANGED