@teevee98/synapse-hub 4.0.0 → 4.1.0
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/.agents/rules/context-engineering.md +3 -3
- package/.agents/rules/security_testing_rules.md +8 -5
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/agentskills.json +1 -1
- package/docs/ARCHITECTURE.en.md +1 -1
- package/docs/ARCHITECTURE.md +1 -1
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/skills_manifest.json +162 -54
- package/synapse.config.json +17 -1
- package/tools/__init__.py +1 -1
- package/tools/ast_cache.py +1 -3
- package/tools/author_integrity.py +7 -2
- package/tools/benchmark_swarm_context.py +242 -151
- package/tools/browser_helpers.js +2 -2
- package/tools/build_consolidated_report.py +5 -5
- package/tools/check_version_sync.py +56 -0
- package/tools/cli.py +3 -3
- package/tools/core/file_lock.py +81 -6
- package/tools/core/telemetry.py +139 -1
- package/tools/core/token_estimator.py +145 -0
- package/tools/count_tokens.py +28 -0
- package/tools/dashboard_generator.py +6 -6
- package/tools/debt_harvester.py +20 -9
- package/tools/diagram_architect.py +0 -1
- package/tools/distill_log.py +266 -89
- package/tools/git_worktree_manager.py +1 -1
- package/tools/mcp_docx_server.py +1 -1
- package/tools/mcp_excel_server.py +1 -1
- package/tools/mcp_server.py +1 -1
- package/tools/mechanical_ui_gate.py +1 -1
- package/tools/micro_mutator.py +51 -10
- package/tools/pairwise_generator.py +0 -1
- package/tools/pentest_scope_guard.py +328 -0
- package/tools/perf/benchmark_perf_runner.py +226 -0
- package/tools/rules_router.py +326 -103
- package/tools/token_telemetry.py +224 -0
- package/tools/ui_server.py +1 -1
- package/tools/update_sentinel.py +3 -3
- package/tools/verify_all.py +24 -6
- package/tools/welcome_page.py +1 -1
package/tools/cli.py
CHANGED
|
@@ -99,7 +99,7 @@ def cmd_set_lang(args):
|
|
|
99
99
|
def cmd_version(args):
|
|
100
100
|
"""Displays the current hub version, skill catalog count, and license information."""
|
|
101
101
|
manifest_path = os.path.join(REPO_ROOT, "skills_manifest.json")
|
|
102
|
-
version = "4.
|
|
102
|
+
version = "4.1.0"
|
|
103
103
|
if os.path.exists(manifest_path):
|
|
104
104
|
try:
|
|
105
105
|
import json
|
|
@@ -686,14 +686,14 @@ def cmd_trend(args):
|
|
|
686
686
|
|
|
687
687
|
def _hub_version():
|
|
688
688
|
manifest_path = os.path.join(REPO_ROOT, "skills_manifest.json")
|
|
689
|
-
version = "4.
|
|
689
|
+
version = "4.1.0"
|
|
690
690
|
if os.path.exists(manifest_path):
|
|
691
691
|
try:
|
|
692
692
|
import json
|
|
693
693
|
with open(manifest_path, "r", encoding="utf-8") as f:
|
|
694
694
|
version = json.load(f).get("version", version)
|
|
695
695
|
except (OSError, json.JSONDecodeError):
|
|
696
|
-
version = "4.
|
|
696
|
+
version = "4.1.0"
|
|
697
697
|
return version
|
|
698
698
|
|
|
699
699
|
|
package/tools/core/file_lock.py
CHANGED
|
@@ -28,7 +28,7 @@ from typing import Any, Optional
|
|
|
28
28
|
if sys.platform != "win32":
|
|
29
29
|
import fcntl
|
|
30
30
|
else:
|
|
31
|
-
|
|
31
|
+
pass # type: ignore
|
|
32
32
|
|
|
33
33
|
# Win32 Native Locking Constants (Boost B1)
|
|
34
34
|
LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
|
|
@@ -130,6 +130,33 @@ def _win32_unlock(fd: int) -> bool:
|
|
|
130
130
|
return False
|
|
131
131
|
|
|
132
132
|
|
|
133
|
+
def _is_process_alive(pid: int) -> bool:
|
|
134
|
+
"""Checks whether a process with the given PID is currently active."""
|
|
135
|
+
if pid <= 0:
|
|
136
|
+
return False
|
|
137
|
+
if sys.platform != "win32":
|
|
138
|
+
try:
|
|
139
|
+
os.kill(pid, 0)
|
|
140
|
+
return True
|
|
141
|
+
except OSError:
|
|
142
|
+
return False
|
|
143
|
+
else:
|
|
144
|
+
try:
|
|
145
|
+
if _HAS_CTYPES and hasattr(ctypes, "windll") and hasattr(ctypes.windll, "kernel32"):
|
|
146
|
+
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
|
147
|
+
SYNCHRONIZE = 0x00100000
|
|
148
|
+
handle = ctypes.windll.kernel32.OpenProcess(
|
|
149
|
+
PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, False, pid
|
|
150
|
+
)
|
|
151
|
+
if handle:
|
|
152
|
+
ctypes.windll.kernel32.CloseHandle(handle)
|
|
153
|
+
return True
|
|
154
|
+
return False
|
|
155
|
+
except Exception as err:
|
|
156
|
+
_ = err
|
|
157
|
+
return True
|
|
158
|
+
|
|
159
|
+
|
|
133
160
|
class FileLockTimeoutError(TimeoutError):
|
|
134
161
|
"""Raised when acquiring a file lock exceeds the allocated timeout."""
|
|
135
162
|
pass
|
|
@@ -137,7 +164,7 @@ class FileLockTimeoutError(TimeoutError):
|
|
|
137
164
|
|
|
138
165
|
class FileLock:
|
|
139
166
|
"""
|
|
140
|
-
Cross-platform advisory file lock context manager.
|
|
167
|
+
Cross-platform advisory file lock context manager with stale lock auto-reclamation.
|
|
141
168
|
"""
|
|
142
169
|
|
|
143
170
|
def __init__(
|
|
@@ -154,10 +181,27 @@ class FileLock:
|
|
|
154
181
|
self.poll_interval = poll_interval
|
|
155
182
|
self._fd: Optional[int] = None
|
|
156
183
|
|
|
184
|
+
def _read_lock_pid(self) -> Optional[int]:
|
|
185
|
+
"""Reads lock metadata (pid:timestamp) from the lock file if present."""
|
|
186
|
+
try:
|
|
187
|
+
if os.path.exists(self.lock_path):
|
|
188
|
+
with open(self.lock_path, "r", encoding="utf-8", errors="replace") as f:
|
|
189
|
+
content = f.read(64).strip()
|
|
190
|
+
if ":" in content:
|
|
191
|
+
pid_str = content.split(":", 1)[0]
|
|
192
|
+
if pid_str.isdigit():
|
|
193
|
+
return int(pid_str)
|
|
194
|
+
elif content.isdigit():
|
|
195
|
+
return int(content)
|
|
196
|
+
except Exception as err:
|
|
197
|
+
_ = err
|
|
198
|
+
return None
|
|
199
|
+
|
|
157
200
|
def acquire(self) -> FileLock:
|
|
158
|
-
"""Acquires the advisory lock, polling until timeout."""
|
|
201
|
+
"""Acquires the advisory lock, polling until timeout with stale-lock reclamation."""
|
|
159
202
|
os.makedirs(os.path.dirname(self.lock_path), exist_ok=True)
|
|
160
203
|
start = time.time()
|
|
204
|
+
stale_reclaimed = False
|
|
161
205
|
while True:
|
|
162
206
|
try:
|
|
163
207
|
if self._fd is None:
|
|
@@ -175,6 +219,14 @@ class FileLock:
|
|
|
175
219
|
_ = err
|
|
176
220
|
os.lseek(self._fd, 0, os.SEEK_SET)
|
|
177
221
|
_win32_lock(self._fd, shared=self.shared)
|
|
222
|
+
|
|
223
|
+
# Write PID into lock file metadata for stale lock tracking if exclusive
|
|
224
|
+
if not self.shared and self._fd is not None:
|
|
225
|
+
try:
|
|
226
|
+
os.lseek(self._fd, 0, os.SEEK_SET)
|
|
227
|
+
os.write(self._fd, f"{os.getpid()}:{int(time.time())}\n".encode("utf-8"))
|
|
228
|
+
except OSError as err:
|
|
229
|
+
_ = err
|
|
178
230
|
return self
|
|
179
231
|
except (BlockingIOError, OSError, PermissionError):
|
|
180
232
|
if self._fd is not None:
|
|
@@ -183,7 +235,22 @@ class FileLock:
|
|
|
183
235
|
except OSError as err:
|
|
184
236
|
_ = err
|
|
185
237
|
self._fd = None
|
|
186
|
-
|
|
238
|
+
|
|
239
|
+
elapsed = time.time() - start
|
|
240
|
+
if elapsed >= self.timeout:
|
|
241
|
+
# Attempt stale lock reclamation if holder PID is dead
|
|
242
|
+
if not stale_reclaimed:
|
|
243
|
+
holder_pid = self._read_lock_pid()
|
|
244
|
+
if holder_pid and not _is_process_alive(holder_pid):
|
|
245
|
+
try:
|
|
246
|
+
if os.path.exists(self.lock_path):
|
|
247
|
+
os.remove(self.lock_path)
|
|
248
|
+
stale_reclaimed = True
|
|
249
|
+
time.sleep(0.02)
|
|
250
|
+
continue
|
|
251
|
+
except OSError as err:
|
|
252
|
+
_ = err
|
|
253
|
+
|
|
187
254
|
raise FileLockTimeoutError(
|
|
188
255
|
f"Timed out after {self.timeout}s waiting for lock on {self.lock_path}"
|
|
189
256
|
)
|
|
@@ -220,7 +287,7 @@ def locked_read(file_path: str, timeout: float = 10.0) -> str:
|
|
|
220
287
|
def locked_write(file_path: str, content: str, timeout: float = 10.0, atomic: bool = True) -> None:
|
|
221
288
|
"""
|
|
222
289
|
Safely writes content to a file while holding an exclusive lock.
|
|
223
|
-
When atomic=True, writes to a temporary file first, then atomically replaces.
|
|
290
|
+
When atomic=True, writes to a temporary file first, then atomically replaces with Windows retry.
|
|
224
291
|
"""
|
|
225
292
|
os.makedirs(os.path.dirname(os.path.abspath(file_path)), exist_ok=True)
|
|
226
293
|
with FileLock(file_path, timeout=timeout, shared=False):
|
|
@@ -231,7 +298,15 @@ def locked_write(file_path: str, content: str, timeout: float = 10.0, atomic: bo
|
|
|
231
298
|
f.write(content)
|
|
232
299
|
f.flush()
|
|
233
300
|
os.fsync(f.fileno())
|
|
234
|
-
|
|
301
|
+
# Resilient atomic replace with retry backoff for Windows NTFS file lock latency
|
|
302
|
+
for attempt in range(5):
|
|
303
|
+
try:
|
|
304
|
+
os.replace(temp_path, file_path)
|
|
305
|
+
break
|
|
306
|
+
except (PermissionError, OSError) as err:
|
|
307
|
+
if attempt == 4:
|
|
308
|
+
raise
|
|
309
|
+
time.sleep(0.01 * (attempt + 1))
|
|
235
310
|
finally:
|
|
236
311
|
if os.path.exists(temp_path):
|
|
237
312
|
try:
|
package/tools/core/telemetry.py
CHANGED
|
@@ -31,7 +31,7 @@ import logging
|
|
|
31
31
|
import contextvars
|
|
32
32
|
from datetime import datetime, timezone
|
|
33
33
|
from dataclasses import dataclass
|
|
34
|
-
from typing import Dict, Any, Optional, Generator
|
|
34
|
+
from typing import Dict, Any, Optional, Generator, List
|
|
35
35
|
from contextlib import contextmanager
|
|
36
36
|
|
|
37
37
|
# W3C Constants
|
|
@@ -356,3 +356,141 @@ def trace_span(
|
|
|
356
356
|
**extra
|
|
357
357
|
)
|
|
358
358
|
set_current_trace_context(parent_ctx)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
# ---------------------------------------------------------------------------
|
|
362
|
+
# Session Token & MTTR Telemetry Ledger (Closed-Loop Quality & Efficiency)
|
|
363
|
+
# ---------------------------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
DEFAULT_SESSION_LEDGER = os.path.join(
|
|
366
|
+
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
|
|
367
|
+
"reports",
|
|
368
|
+
"session_telemetry_ledger.json",
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
@dataclass
|
|
373
|
+
class SessionTelemetryRecord:
|
|
374
|
+
session_id: str
|
|
375
|
+
timestamp: str
|
|
376
|
+
model: str
|
|
377
|
+
platform: str
|
|
378
|
+
task_type: str
|
|
379
|
+
stack: str
|
|
380
|
+
turns: int
|
|
381
|
+
input_tokens: int
|
|
382
|
+
output_tokens: int
|
|
383
|
+
cached_tokens: int
|
|
384
|
+
cache_hit_ratio: float
|
|
385
|
+
raw_log_bytes: int
|
|
386
|
+
distilled_log_bytes: int
|
|
387
|
+
distillation_reduction_pct: float
|
|
388
|
+
test_outcome: str
|
|
389
|
+
bug_resolved: bool
|
|
390
|
+
tokens_per_bug: int
|
|
391
|
+
elapsed_seconds: float
|
|
392
|
+
|
|
393
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
394
|
+
return {
|
|
395
|
+
"session_id": self.session_id,
|
|
396
|
+
"timestamp": self.timestamp,
|
|
397
|
+
"model": self.model,
|
|
398
|
+
"platform": self.platform,
|
|
399
|
+
"task_type": self.task_type,
|
|
400
|
+
"stack": self.stack,
|
|
401
|
+
"turns": self.turns,
|
|
402
|
+
"input_tokens": self.input_tokens,
|
|
403
|
+
"output_tokens": self.output_tokens,
|
|
404
|
+
"cached_tokens": self.cached_tokens,
|
|
405
|
+
"cache_hit_ratio": self.cache_hit_ratio,
|
|
406
|
+
"raw_log_bytes": self.raw_log_bytes,
|
|
407
|
+
"distilled_log_bytes": self.distilled_log_bytes,
|
|
408
|
+
"distillation_reduction_pct": self.distillation_reduction_pct,
|
|
409
|
+
"test_outcome": self.test_outcome,
|
|
410
|
+
"bug_resolved": self.bug_resolved,
|
|
411
|
+
"tokens_per_bug": self.tokens_per_bug,
|
|
412
|
+
"elapsed_seconds": self.elapsed_seconds,
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def record_session_telemetry(
|
|
417
|
+
record: SessionTelemetryRecord,
|
|
418
|
+
ledger_file: Optional[str] = None,
|
|
419
|
+
) -> None:
|
|
420
|
+
"""Appends a closed-loop session telemetry record to the JSON ledger with file locking."""
|
|
421
|
+
target_path = ledger_file or DEFAULT_SESSION_LEDGER
|
|
422
|
+
os.makedirs(os.path.dirname(os.path.abspath(target_path)), exist_ok=True)
|
|
423
|
+
|
|
424
|
+
records: List[Dict[str, Any]] = []
|
|
425
|
+
if os.path.exists(target_path):
|
|
426
|
+
try:
|
|
427
|
+
with open(target_path, "r", encoding="utf-8") as f:
|
|
428
|
+
records = json.load(f)
|
|
429
|
+
except (OSError, json.JSONDecodeError):
|
|
430
|
+
records = []
|
|
431
|
+
|
|
432
|
+
records.append(record.to_dict())
|
|
433
|
+
with open(target_path, "w", encoding="utf-8") as f:
|
|
434
|
+
json.dump(records, f, indent=2, ensure_ascii=False)
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def get_telemetry_summary(ledger_file: Optional[str] = None) -> Dict[str, Any]:
|
|
438
|
+
"""Computes median/P90 tokens per resolved bug, MTTR, and compression statistics."""
|
|
439
|
+
target_path = ledger_file or DEFAULT_SESSION_LEDGER
|
|
440
|
+
if not os.path.exists(target_path):
|
|
441
|
+
return {
|
|
442
|
+
"sample_size": 0,
|
|
443
|
+
"resolved_bugs": 0,
|
|
444
|
+
"median_tokens_per_bug": 0,
|
|
445
|
+
"p90_tokens_per_bug": 0,
|
|
446
|
+
"mttr_seconds": 0.0,
|
|
447
|
+
"avg_cache_hit_ratio": 0.0,
|
|
448
|
+
"avg_distillation_reduction_pct": 0.0,
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
try:
|
|
452
|
+
with open(target_path, "r", encoding="utf-8") as f:
|
|
453
|
+
records: List[Dict[str, Any]] = json.load(f)
|
|
454
|
+
except (OSError, json.JSONDecodeError):
|
|
455
|
+
records = []
|
|
456
|
+
|
|
457
|
+
if not records:
|
|
458
|
+
return {
|
|
459
|
+
"sample_size": 0,
|
|
460
|
+
"resolved_bugs": 0,
|
|
461
|
+
"median_tokens_per_bug": 0,
|
|
462
|
+
"p90_tokens_per_bug": 0,
|
|
463
|
+
"mttr_seconds": 0.0,
|
|
464
|
+
"avg_cache_hit_ratio": 0.0,
|
|
465
|
+
"avg_distillation_reduction_pct": 0.0,
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
resolved = [r for r in records if r.get("bug_resolved")]
|
|
469
|
+
tokens_per_bug = sorted([r.get("tokens_per_bug", 0) for r in resolved])
|
|
470
|
+
durations = [r.get("elapsed_seconds", 0.0) for r in resolved]
|
|
471
|
+
cache_ratios = [r.get("cache_hit_ratio", 0.0) for r in records]
|
|
472
|
+
distill_pcts = [r.get("distillation_reduction_pct", 0.0) for r in records]
|
|
473
|
+
|
|
474
|
+
def _median(vals: List[float]) -> float:
|
|
475
|
+
if not vals:
|
|
476
|
+
return 0.0
|
|
477
|
+
n = len(vals)
|
|
478
|
+
mid = n // 2
|
|
479
|
+
return (vals[mid] if n % 2 != 0 else (vals[mid - 1] + vals[mid]) / 2.0)
|
|
480
|
+
|
|
481
|
+
def _p90(vals: List[float]) -> float:
|
|
482
|
+
if not vals:
|
|
483
|
+
return 0.0
|
|
484
|
+
idx = int(len(vals) * 0.90)
|
|
485
|
+
return vals[min(idx, len(vals) - 1)]
|
|
486
|
+
|
|
487
|
+
return {
|
|
488
|
+
"sample_size": len(records),
|
|
489
|
+
"resolved_bugs": len(resolved),
|
|
490
|
+
"median_tokens_per_bug": int(_median(tokens_per_bug)),
|
|
491
|
+
"p90_tokens_per_bug": int(_p90(tokens_per_bug)),
|
|
492
|
+
"mttr_seconds": round(sum(durations) / max(1, len(durations)), 2),
|
|
493
|
+
"avg_cache_hit_ratio": round(sum(cache_ratios) / max(1, len(cache_ratios)), 3),
|
|
494
|
+
"avg_distillation_reduction_pct": round(sum(distill_pcts) / max(1, len(distill_pcts)), 2),
|
|
495
|
+
}
|
|
496
|
+
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 Thế Vị (@TeeVee98). All rights reserved.
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
# Project: Synapse-Hub (https://github.com/TeeVee98/Synapse-Hub)
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
tools/core/token_estimator.py — Unified Polyglot BPE Token Estimator
|
|
8
|
+
-------------------------------------------------------------------
|
|
9
|
+
Provides deterministic, calibrated token counting across codebases, markdown rules,
|
|
10
|
+
and system prompts. Standardizes token estimation across rules_router, distill_log,
|
|
11
|
+
benchmark_swarm_context, and count_tokens.
|
|
12
|
+
|
|
13
|
+
Key Capabilities:
|
|
14
|
+
1. High-accuracy fallback heuristic:
|
|
15
|
+
- English / ASCII code: ~3.85 characters/token (calibrated against cl100k_base / o200k_base).
|
|
16
|
+
- Non-ASCII / Vietnamese diacritics / Unicode: ~1.8 characters/token.
|
|
17
|
+
2. Optional native BPE tokenization if tiktoken is available in runtime.
|
|
18
|
+
3. Structured token breakdown (Rules, Catalog Metadata, Full Skill Bodies, System Prefix).
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import os
|
|
24
|
+
import re
|
|
25
|
+
import sys
|
|
26
|
+
from typing import Any, Dict, List, Optional
|
|
27
|
+
|
|
28
|
+
# Attempt native BPE import if available
|
|
29
|
+
_TIKTOKEN_ENCODER = None
|
|
30
|
+
try:
|
|
31
|
+
import tiktoken # type: ignore
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
_TIKTOKEN_ENCODER = tiktoken.get_encoding("cl100k_base")
|
|
35
|
+
except Exception:
|
|
36
|
+
_TIKTOKEN_ENCODER = None
|
|
37
|
+
except ImportError:
|
|
38
|
+
_TIKTOKEN_ENCODER = None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def count_tokens(text: str, model_hint: str = "cl100k") -> int:
|
|
42
|
+
"""
|
|
43
|
+
Counts or estimates tokens for arbitrary polyglot text or code.
|
|
44
|
+
If tiktoken is installed, calculates exact cl100k_base tokens.
|
|
45
|
+
Otherwise, applies calibrated polyglot heuristic:
|
|
46
|
+
- ASCII / Latin / Code: ~3.85 chars/token.
|
|
47
|
+
- Non-ASCII / Diacritics: ~1.8 chars/token.
|
|
48
|
+
"""
|
|
49
|
+
if not text:
|
|
50
|
+
return 0
|
|
51
|
+
|
|
52
|
+
if _TIKTOKEN_ENCODER is not None:
|
|
53
|
+
try:
|
|
54
|
+
return len(_TIKTOKEN_ENCODER.encode(text, disallowed_special=()))
|
|
55
|
+
except Exception as err:
|
|
56
|
+
_ = err
|
|
57
|
+
|
|
58
|
+
# Heuristic calibrated against standard LLM subword tokenizers
|
|
59
|
+
non_ascii_chars = len([c for c in text if ord(c) > 127])
|
|
60
|
+
ascii_chars = len(text) - non_ascii_chars
|
|
61
|
+
|
|
62
|
+
# Token density weights
|
|
63
|
+
ascii_tokens = ascii_chars / 3.85
|
|
64
|
+
unicode_tokens = non_ascii_chars / 1.80
|
|
65
|
+
|
|
66
|
+
total_est = int(ascii_tokens + unicode_tokens)
|
|
67
|
+
# Lower bound: words + punctuation count to prevent underestimation on short texts
|
|
68
|
+
word_tokens = len(re.findall(r"\w+|[^\w\s]", text, re.UNICODE))
|
|
69
|
+
|
|
70
|
+
return max(1, max(total_est, int(word_tokens * 1.05)))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def count_file_tokens(file_path: str) -> int:
|
|
74
|
+
"""Reads a file from disk and returns its exact/estimated token count."""
|
|
75
|
+
if not os.path.isfile(file_path):
|
|
76
|
+
return 0
|
|
77
|
+
try:
|
|
78
|
+
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
|
79
|
+
content = f.read()
|
|
80
|
+
return count_tokens(content)
|
|
81
|
+
except OSError:
|
|
82
|
+
return 0
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def calculate_hydration_payload(
|
|
86
|
+
repo_root: str,
|
|
87
|
+
rules: List[str],
|
|
88
|
+
skills: List[str],
|
|
89
|
+
) -> Dict[str, Any]:
|
|
90
|
+
"""
|
|
91
|
+
Computes precise dual-tier token metrics for a set of selected rules and skills:
|
|
92
|
+
1. summary: Catalog metadata only (name + summary description ~120 tokens/skill).
|
|
93
|
+
2. full: Complete SKILL.md body hydrated into context.
|
|
94
|
+
"""
|
|
95
|
+
rules_dir = os.path.join(repo_root, ".agents", "rules")
|
|
96
|
+
skills_root = os.path.join(repo_root, ".agents", "skills")
|
|
97
|
+
|
|
98
|
+
rule_tokens = 0
|
|
99
|
+
rule_bytes = 0
|
|
100
|
+
for r in rules:
|
|
101
|
+
p = os.path.join(rules_dir, r)
|
|
102
|
+
if os.path.isfile(p):
|
|
103
|
+
rule_tokens += count_file_tokens(p)
|
|
104
|
+
rule_bytes += os.path.getsize(p)
|
|
105
|
+
else:
|
|
106
|
+
rule_tokens += 500 # Default estimate for virtual rule
|
|
107
|
+
rule_bytes += 2000
|
|
108
|
+
|
|
109
|
+
catalog_skill_tokens = len(skills) * 120
|
|
110
|
+
full_skill_tokens = 0
|
|
111
|
+
full_skill_bytes = 0
|
|
112
|
+
|
|
113
|
+
# Locate each skill directory in .agents/skills
|
|
114
|
+
for skill_name in skills:
|
|
115
|
+
skill_file = _find_skill_file(skills_root, skill_name)
|
|
116
|
+
if skill_file and os.path.isfile(skill_file):
|
|
117
|
+
tokens = count_file_tokens(skill_file)
|
|
118
|
+
size = os.path.getsize(skill_file)
|
|
119
|
+
full_skill_tokens += tokens
|
|
120
|
+
full_skill_bytes += size
|
|
121
|
+
else:
|
|
122
|
+
# Fallback average skill size (~1,800 tokens, 7.5 KB)
|
|
123
|
+
full_skill_tokens += 1800
|
|
124
|
+
full_skill_bytes += 7500
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
"rule_tokens": rule_tokens,
|
|
128
|
+
"rule_bytes": rule_bytes,
|
|
129
|
+
"skill_catalog_tokens": catalog_skill_tokens,
|
|
130
|
+
"skill_full_body_tokens": full_skill_tokens,
|
|
131
|
+
"skill_full_bytes": full_skill_bytes,
|
|
132
|
+
"total_summary_tokens": rule_tokens + catalog_skill_tokens,
|
|
133
|
+
"total_hydrated_tokens": rule_tokens + full_skill_tokens,
|
|
134
|
+
"total_hydrated_bytes": rule_bytes + full_skill_bytes,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _find_skill_file(skills_root: str, skill_name: str) -> Optional[str]:
|
|
139
|
+
"""Finds SKILL.md for a given skill name across tiered category folders."""
|
|
140
|
+
if not os.path.isdir(skills_root):
|
|
141
|
+
return None
|
|
142
|
+
for root, dirs, files in os.walk(skills_root):
|
|
143
|
+
if os.path.basename(root) == skill_name and "SKILL.md" in files:
|
|
144
|
+
return os.path.join(root, "SKILL.md")
|
|
145
|
+
return None
|
package/tools/count_tokens.py
CHANGED
|
@@ -31,6 +31,10 @@ Usage:
|
|
|
31
31
|
|
|
32
32
|
import os
|
|
33
33
|
import sys
|
|
34
|
+
|
|
35
|
+
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
36
|
+
if _REPO_ROOT not in sys.path:
|
|
37
|
+
sys.path.insert(0, _REPO_ROOT)
|
|
34
38
|
import glob
|
|
35
39
|
import json
|
|
36
40
|
import re
|
|
@@ -1135,6 +1139,7 @@ def main():
|
|
|
1135
1139
|
parser.add_argument("--pricing", choices=list(PRICING_TABLE.keys()), default=None, help="Model pricing basis (default: auto-detected from transcript)")
|
|
1136
1140
|
parser.add_argument("--json", action="store_true", help="Output machine-readable JSON format")
|
|
1137
1141
|
parser.add_argument("--otel", "--opentelemetry", action="store_true", dest="otel", help="Export session spans formatted according to OpenTelemetry GenAI Semantic Conventions")
|
|
1142
|
+
parser.add_argument("--telemetry-summary", action="store_true", help="Display closed-loop MTTR and Tokens-per-Bug telemetry summary")
|
|
1138
1143
|
parser.add_argument("--file", type=str, help="Direct path to any transcript JSONL file")
|
|
1139
1144
|
|
|
1140
1145
|
args = parser.parse_args()
|
|
@@ -1146,6 +1151,29 @@ def main():
|
|
|
1146
1151
|
save_budget_config(b5, b7)
|
|
1147
1152
|
print(f"✅ Token budget updated: 5h = {format_token_shorthand(b5 or get_budget_config()['budget_5h'])}, 7d = {format_token_shorthand(b7 or get_budget_config()['budget_7d'])}")
|
|
1148
1153
|
|
|
1154
|
+
# Telemetry summary mode (MTTR & Token/Bug closed-loop ledger)
|
|
1155
|
+
if args.telemetry_summary:
|
|
1156
|
+
try:
|
|
1157
|
+
from tools.core.telemetry import get_telemetry_summary
|
|
1158
|
+
except ImportError:
|
|
1159
|
+
from core.telemetry import get_telemetry_summary
|
|
1160
|
+
summary = get_telemetry_summary()
|
|
1161
|
+
if args.json:
|
|
1162
|
+
print(json.dumps(summary, indent=2, ensure_ascii=False))
|
|
1163
|
+
else:
|
|
1164
|
+
print("╔══════════════════════════════════════════════════════════════════╗")
|
|
1165
|
+
print("║ SYNAPSE HUB — SESSION TELEMETRY & MTTR EFFICIENCY LEDGER ║")
|
|
1166
|
+
print("╚══════════════════════════════════════════════════════════════════╝")
|
|
1167
|
+
print(f" Total Tracked Sessions : {summary['sample_size']}")
|
|
1168
|
+
print(f" Successfully Resolved Defects : {summary['resolved_bugs']}")
|
|
1169
|
+
print(f" Median Tokens / Resolved Bug : {summary['median_tokens_per_bug']:,} tokens")
|
|
1170
|
+
print(f" P90 Tokens / Resolved Bug : {summary['p90_tokens_per_bug']:,} tokens")
|
|
1171
|
+
print(f" Mean Time To Resolution (MTTR): {summary['mttr_seconds']}s")
|
|
1172
|
+
print(f" Average Cache Hit Ratio : {summary['avg_cache_hit_ratio'] * 100:.1f}%")
|
|
1173
|
+
print(f" Average Log Distillation Red. : {summary['avg_distillation_reduction_pct']}%")
|
|
1174
|
+
print("════════════════════════════════════════════════════════════════════")
|
|
1175
|
+
return
|
|
1176
|
+
|
|
1149
1177
|
# Direct file audit mode
|
|
1150
1178
|
if args.file:
|
|
1151
1179
|
plat = detect_platform_from_file(args.file)
|
|
@@ -1348,7 +1348,7 @@ def _build_target_translations(
|
|
|
1348
1348
|
"th_step_status": "Trạng Thái",
|
|
1349
1349
|
"th_step_details": "Chi Tiết Kỹ Thuật (OTel & AST)",
|
|
1350
1350
|
|
|
1351
|
-
"footer_text": "Báo cáo được khởi tạo tự động bởi Synapse-Hub v4.
|
|
1351
|
+
"footer_text": "Báo cáo được khởi tạo tự động bởi Synapse-Hub v4.1.0 • Bản quyền © Thế Vị (@TeeVee98) • Chuẩn Clean POM & Kiểm Thử Tự Chủ",
|
|
1352
1352
|
}
|
|
1353
1353
|
|
|
1354
1354
|
translations_en = {
|
|
@@ -1426,7 +1426,7 @@ def _build_target_translations(
|
|
|
1426
1426
|
"th_step_status": "Status",
|
|
1427
1427
|
"th_step_details": "Technical Details (OTel & AST)",
|
|
1428
1428
|
|
|
1429
|
-
"footer_text": "Generated automatically by Synapse-Hub v4.
|
|
1429
|
+
"footer_text": "Generated automatically by Synapse-Hub v4.1.0 • Copyright © Thế Vị (@TeeVee98) • Clean POM & Autonomous Testing",
|
|
1430
1430
|
}
|
|
1431
1431
|
|
|
1432
1432
|
_inject_gate_translations(translations_vi, translations_en, gates)
|
|
@@ -1915,7 +1915,7 @@ def _build_hub_translations(
|
|
|
1915
1915
|
"""Builds bilingual dictionaries for Hub Dashboard."""
|
|
1916
1916
|
translations_vi = {
|
|
1917
1917
|
"header_title": "Bảng Điều Hành Chất Lượng Doanh Nghiệp",
|
|
1918
|
-
"header_subtitle": "Synapse-Hub v4.
|
|
1918
|
+
"header_subtitle": "Synapse-Hub v4.1.0 • Hệ Sinh Thái Kiểm Thử AI Agent Tự Chủ",
|
|
1919
1919
|
"nav_welcome": "Giới Thiệu",
|
|
1920
1920
|
"meta_mode": "🏷️ Chế độ: <strong>HỆ SINH THÁI QA HUB</strong>",
|
|
1921
1921
|
"meta_gen": f"🕒 Khởi tạo: <strong>{now_str}</strong>",
|
|
@@ -1985,12 +1985,12 @@ def _build_hub_translations(
|
|
|
1985
1985
|
"th_sk_ver": "Phiên Bản",
|
|
1986
1986
|
"th_sk_cap": "Khả Năng Nghiệp Vụ Cốt Lõi",
|
|
1987
1987
|
|
|
1988
|
-
"footer_text": "Bảng Điều Hành Chất Lượng Doanh Nghiệp • Synapse-Hub v4.
|
|
1988
|
+
"footer_text": "Bảng Điều Hành Chất Lượng Doanh Nghiệp • Synapse-Hub v4.1.0 • Bản quyền © Thế Vị (@TeeVee98) • Hệ Thống Kiểm Thử AI Agent Tự Chủ",
|
|
1989
1989
|
}
|
|
1990
1990
|
|
|
1991
1991
|
translations_en = {
|
|
1992
1992
|
"header_title": "Enterprise QA Executive Dashboard",
|
|
1993
|
-
"header_subtitle": "Synapse-Hub v4.
|
|
1993
|
+
"header_subtitle": "Synapse-Hub v4.1.0 • Autonomous AI Agent Testing Ecosystem",
|
|
1994
1994
|
"nav_welcome": "Welcome",
|
|
1995
1995
|
"meta_mode": "🏷️ Mode: <strong>QA HUB ECOSYSTEM</strong>",
|
|
1996
1996
|
"meta_gen": f"🕒 Generated: <strong>{now_str}</strong>",
|
|
@@ -2060,7 +2060,7 @@ def _build_hub_translations(
|
|
|
2060
2060
|
"th_sk_ver": "Version",
|
|
2061
2061
|
"th_sk_cap": "Core Capabilities",
|
|
2062
2062
|
|
|
2063
|
-
"footer_text": "Enterprise QA Executive Dashboard • Synapse-Hub v4.
|
|
2063
|
+
"footer_text": "Enterprise QA Executive Dashboard • Synapse-Hub v4.1.0 • Copyright © Thế Vị (@TeeVee98) • Autonomous AI Agent Testing Ecosystem",
|
|
2064
2064
|
}
|
|
2065
2065
|
|
|
2066
2066
|
_inject_gate_translations(translations_vi, translations_en, gates)
|
package/tools/debt_harvester.py
CHANGED
|
@@ -29,8 +29,8 @@ if hasattr(sys.stdout, 'reconfigure'):
|
|
|
29
29
|
try:
|
|
30
30
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
31
31
|
sys.stderr.reconfigure(encoding='utf-8')
|
|
32
|
-
except Exception:
|
|
33
|
-
|
|
32
|
+
except Exception as exc:
|
|
33
|
+
_ = exc
|
|
34
34
|
|
|
35
35
|
# Default directories to ignore during scan
|
|
36
36
|
IGNORED_DIRS = {
|
|
@@ -38,7 +38,7 @@ IGNORED_DIRS = {
|
|
|
38
38
|
"venv", ".venv", "env", ".env", "__pycache__", ".idea",
|
|
39
39
|
".vscode", "coverage", ".mypy_cache", ".ruff_cache",
|
|
40
40
|
".pytest_cache", ".sandbox_snapshots", "graphify-out",
|
|
41
|
-
"claude-ecosystem-repos", "Reports", "TestReports",
|
|
41
|
+
"claude-ecosystem-repos", "Reports", "reports", "TestReports",
|
|
42
42
|
"allure-results", "results", "execution_results", "bin", "Libs", "Drivers"
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -59,13 +59,15 @@ class ShortcutEntry:
|
|
|
59
59
|
self.description = ""
|
|
60
60
|
self.ceiling = ""
|
|
61
61
|
self.upgrade_trigger = ""
|
|
62
|
+
self.owner = "@TeeVee98 (Core Hub)"
|
|
63
|
+
self.sla = "v4.1.0-Sprint"
|
|
62
64
|
self.has_trigger = False
|
|
63
65
|
self._parse()
|
|
64
66
|
|
|
65
67
|
def _parse(self):
|
|
66
68
|
text = self.raw_comment
|
|
67
69
|
# Format patterns:
|
|
68
|
-
# 1. shortcut: <desc>, ceiling: <limit>, upgrade: <trigger>
|
|
70
|
+
# 1. shortcut: <desc>, ceiling: <limit>, upgrade: <trigger>, owner: <owner>, sla: <date>
|
|
69
71
|
# 2. shortcut: <desc>, <limit>, <trigger>
|
|
70
72
|
# 3. shortcut: <limit>, <when to upgrade>
|
|
71
73
|
|
|
@@ -74,12 +76,19 @@ class ShortcutEntry:
|
|
|
74
76
|
# Check explicit labels
|
|
75
77
|
ceiling_match = re.search(r'(?:ceiling|limit):\s*([^,]+)', text, re.IGNORECASE)
|
|
76
78
|
upgrade_match = re.search(r'(?:upgrade|when|trigger):\s*([^,]+)', text, re.IGNORECASE)
|
|
79
|
+
owner_match = re.search(r'owner:\s*([^,]+)', text, re.IGNORECASE)
|
|
80
|
+
sla_match = re.search(r'(?:sla|due):\s*([^,]+)', text, re.IGNORECASE)
|
|
81
|
+
|
|
82
|
+
if owner_match:
|
|
83
|
+
self.owner = owner_match.group(1).strip()
|
|
84
|
+
if sla_match:
|
|
85
|
+
self.sla = sla_match.group(1).strip()
|
|
77
86
|
|
|
78
87
|
if ceiling_match or upgrade_match:
|
|
79
88
|
self.ceiling = ceiling_match.group(1).strip() if ceiling_match else ""
|
|
80
89
|
self.upgrade_trigger = upgrade_match.group(1).strip() if upgrade_match else ""
|
|
81
|
-
# Description is what's left before ceiling/upgrade
|
|
82
|
-
desc_part = re.split(r'(?:ceiling|limit|upgrade|when|trigger):', text, flags=re.IGNORECASE)[0]
|
|
90
|
+
# Description is what's left before ceiling/upgrade/owner/sla
|
|
91
|
+
desc_part = re.split(r'(?:ceiling|limit|upgrade|when|trigger|owner|sla|due):', text, flags=re.IGNORECASE)[0]
|
|
83
92
|
self.description = desc_part.strip().rstrip(',')
|
|
84
93
|
elif len(parts) >= 2:
|
|
85
94
|
self.description = parts[0]
|
|
@@ -103,6 +112,8 @@ class ShortcutEntry:
|
|
|
103
112
|
"description": self.description or self.raw_comment,
|
|
104
113
|
"ceiling": self.ceiling or "Not specified",
|
|
105
114
|
"upgrade_trigger": self.upgrade_trigger or "No trigger specified",
|
|
115
|
+
"owner": self.owner,
|
|
116
|
+
"sla": self.sla,
|
|
106
117
|
"has_trigger": self.has_trigger,
|
|
107
118
|
"raw": self.raw_comment
|
|
108
119
|
}
|
|
@@ -172,13 +183,13 @@ def format_markdown(entries: List[ShortcutEntry], target_dir: str) -> str:
|
|
|
172
183
|
lines.append("🎉 **No shortcut debt found. Clean ledger.**")
|
|
173
184
|
return "\n".join(lines)
|
|
174
185
|
|
|
175
|
-
lines.append("| Location | Description | Ceiling (Limit) | Upgrade Trigger | Status |")
|
|
176
|
-
lines.append("| :--- | :--- | :--- | :--- | :--- |")
|
|
186
|
+
lines.append("| Location | Description | Ceiling (Limit) | Upgrade Trigger | Owner | SLA | Status |")
|
|
187
|
+
lines.append("| :--- | :--- | :--- | :--- | :--- | :--- | :--- |")
|
|
177
188
|
|
|
178
189
|
for e in entries:
|
|
179
190
|
status = "✅ Tracked" if e.has_trigger else "⚠️ `no-trigger`"
|
|
180
191
|
loc = f"`{e.file_path}:{e.line_no}`"
|
|
181
|
-
lines.append(f"| {loc} | {e.description} | {e.ceiling} | {e.upgrade_trigger or 'None'} | {status} |")
|
|
192
|
+
lines.append(f"| {loc} | {e.description} | {e.ceiling} | {e.upgrade_trigger or 'None'} | {e.owner} | {e.sla} | {status} |")
|
|
182
193
|
|
|
183
194
|
lines.append("")
|
|
184
195
|
lines.append("### Recommended Action")
|