File size: 11,847 Bytes
58fe8fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | """
ARCHITECT β semantic skill selector (Masterplan Β§5).
Upgrades the keyword-based ``architect/skill_registry`` with **semantic
similarity ranking** so the right domain skills are loaded into the LLM
context regardless of how the task is phrased.
Tier-5 design (Masterplan Β§7):
* Embeddings: sentence-transformers / MiniLM-L6-v2 (CPU, $0)
* Fallback: deterministic keyword-overlap scorer (no model needed)
* Cache: skill embeddings hashed on disk (JSON) so the model loads
*once* per process and *never* recomputes between calls.
Public surface:
select_for_task(task_description, repo_languages, repo_tech_stack,
attack_phase, top_k=5) -> str
Returns a single XML-flavoured ``<skills>...</skills>`` block ready
to be prepended to any system prompt.
pack(task_description, ...) -> list[Skill]
Same logic but returns the matched Skill objects (for callers who
want to render their own context format).
The module is designed to **never raise** in production: every external
dependency (sentence-transformers, numpy, sklearn) is optional and falls
back to a pure-Python implementation.
"""
from __future__ import annotations
import hashlib
import json
import logging
import math
import os
import re
import threading
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Iterable
from . import skill_registry
LOG = logging.getLogger("architect.skill_selector")
CACHE_DIR = Path(os.getenv("ARCHITECT_SKILL_CACHE", "/tmp/architect_skill_cache"))
CACHE_DIR.mkdir(parents=True, exist_ok=True)
# Phase β list of skill-name substrings that should get a relevance boost.
PHASE_BOOSTS: dict[str, tuple[str, ...]] = {
"recon": ("recon", "subdomain", "wayback", "shodan", "httpx", "fingerprint"),
"static": ("static", "sast", "taint", "semgrep", "ast", "code", "memory"),
"dynamic": ("fuzz", "dynamic", "browser", "runtime", "frida"),
"exploit": ("exploit", "rop", "heap", "buffer", "pwn", "shellcode", "primitive"),
"report": ("report", "p1", "p2", "cvss", "submission", "platform"),
"triage": ("methodology", "reference", "index", "report"),
}
_LOCK = threading.Lock()
_MODEL: Any = None
_EMBEDS: dict[str, list[float]] = {}
_SKILLS: list[skill_registry.Skill] = []
# ββ lazy model loading ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _try_load_model() -> Any:
"""Best-effort load of MiniLM. Returns None on any failure."""
global _MODEL
if _MODEL is not None:
return _MODEL
try:
from sentence_transformers import SentenceTransformer # type: ignore
model_name = os.getenv("ARCHITECT_EMBED_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
LOG.info("skill_selector: loading embed model %s", model_name)
_MODEL = SentenceTransformer(model_name)
return _MODEL
except Exception as exc: # noqa: BLE001
LOG.warning("skill_selector: sentence-transformers unavailable (%s) β using keyword fallback", exc)
_MODEL = False # sentinel: don't try again
return None
# ββ embedding cache βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _skill_hash(skill: skill_registry.Skill) -> str:
h = hashlib.sha256()
h.update(skill.name.encode())
h.update(skill.body.encode())
return h.hexdigest()[:16]
def _cache_path_for(model_name: str) -> Path:
safe = re.sub(r"[^a-zA-Z0-9_.-]", "_", model_name)
return CACHE_DIR / f"skills_{safe}.json"
def _load_disk_cache(model_name: str) -> dict[str, list[float]]:
p = _cache_path_for(model_name)
if not p.exists():
return {}
try:
return json.loads(p.read_text())
except Exception as exc: # noqa: BLE001
LOG.debug("skill_selector cache read failed: %s", exc)
return {}
def _save_disk_cache(model_name: str, data: dict[str, list[float]]) -> None:
p = _cache_path_for(model_name)
try:
p.write_text(json.dumps(data))
except Exception as exc: # noqa: BLE001
LOG.debug("skill_selector cache write failed: %s", exc)
def _ensure_skill_embeddings() -> dict[str, list[float]]:
"""
Return ``{skill_name: vector}`` for the entire registry. Computes only
the vectors that aren't already cached. If no embedding model is
available, returns an empty dict (callers must fall back to keywords).
"""
global _EMBEDS, _SKILLS
with _LOCK:
if not _SKILLS:
_SKILLS = skill_registry.load_all()
model = _try_load_model()
if not model:
return {}
model_name = os.getenv("ARCHITECT_EMBED_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
cache = _load_disk_cache(model_name)
missing: list[skill_registry.Skill] = []
result: dict[str, list[float]] = {}
for s in _SKILLS:
key = f"{s.name}:{_skill_hash(s)}"
if key in cache:
result[s.name] = cache[key]
else:
missing.append(s)
if missing:
try:
texts = [f"{m.name}\n{m.domain}\n{m.body[:2000]}" for m in missing]
vecs = model.encode(texts, normalize_embeddings=True).tolist()
for s, v in zip(missing, vecs):
key = f"{s.name}:{_skill_hash(s)}"
cache[key] = v
result[s.name] = v
_save_disk_cache(model_name, cache)
except Exception as exc: # noqa: BLE001
LOG.warning("skill_selector: embedding failed (%s) β keyword fallback", exc)
return {}
_EMBEDS = result
return result
# ββ similarity helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _cosine(a: list[float], b: list[float]) -> float:
if not a or not b or len(a) != len(b):
return 0.0
dot = sum(x * y for x, y in zip(a, b))
na = math.sqrt(sum(x * x for x in a))
nb = math.sqrt(sum(y * y for y in b))
if na == 0 or nb == 0:
return 0.0
return dot / (na * nb)
def _keyword_score(skill: skill_registry.Skill, tokens: set[str]) -> float:
"""Pure-Python fallback ranker. Counts shared whitespace tokens."""
body_tokens = set(re.findall(r"[a-z0-9_-]{3,}", (skill.name + " " + skill.body).lower()))
if not body_tokens:
return 0.0
overlap = body_tokens & tokens
return len(overlap) / max(1, math.sqrt(len(body_tokens)))
def _phase_boost(skill: skill_registry.Skill, phase: str) -> float:
needles = PHASE_BOOSTS.get(phase.lower(), ())
if not needles:
return 0.0
name = (skill.name + " " + skill.domain + " " + skill.path.as_posix()).lower()
return 0.10 * sum(1 for n in needles if n in name)
# ββ public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class Match:
skill: skill_registry.Skill
score: float
reason: str
def to_dict(self) -> dict[str, Any]:
return {
"name": self.skill.name,
"domain": self.skill.domain,
"path": str(self.skill.path),
"score": round(self.score, 4),
"reason": self.reason,
}
def pack(
task_description: str,
*,
repo_languages: Iterable[str] | None = None,
repo_tech_stack: Iterable[str] | None = None,
attack_phase: str = "static",
top_k: int = 5,
pin: Iterable[str] | None = None,
) -> list[Match]:
"""
Return up to ``top_k`` matched skills (highest score first).
``pin`` β names of skills that must always be included regardless of
score (useful for "always carry the bug-bounty methodology playbook").
"""
if not _SKILLS:
_ensure_skill_embeddings() # also populates _SKILLS even on fallback
if not _SKILLS:
return []
langs = [s.lower() for s in (repo_languages or [])]
techs = [s.lower() for s in (repo_tech_stack or [])]
query = " ".join([task_description, attack_phase, *langs, *techs])
embeds = _ensure_skill_embeddings()
use_embed = bool(embeds)
matches: list[Match] = []
if use_embed:
try:
qv = _MODEL.encode([query], normalize_embeddings=True).tolist()[0]
except Exception as exc: # noqa: BLE001
LOG.warning("skill_selector: query encode failed (%s) β keyword fallback", exc)
use_embed = False
if use_embed:
for s in _SKILLS:
v = embeds.get(s.name)
if not v:
continue
score = _cosine(qv, v) + _phase_boost(s, attack_phase)
matches.append(Match(skill=s, score=score, reason="semantic"))
else:
tokens = set(re.findall(r"[a-z0-9_-]{3,}", query.lower()))
for s in _SKILLS:
score = _keyword_score(s, tokens) + _phase_boost(s, attack_phase)
matches.append(Match(skill=s, score=score, reason="keyword"))
# Boost via the registry's structured trigger profile too.
profile = {"languages": langs, "asset_types": techs, "frameworks": techs}
for m in matches:
m.score += 0.05 * m.skill.matches(profile)
matches.sort(key=lambda m: m.score, reverse=True)
chosen = matches[:top_k]
pinned = {p.lower() for p in (pin or [])}
if pinned:
already = {m.skill.name.lower() for m in chosen}
for s in _SKILLS:
if s.name.lower() in pinned and s.name.lower() not in already:
chosen.append(Match(skill=s, score=999.0, reason="pinned"))
return chosen
def select_for_task(
task_description: str,
repo_languages: Iterable[str] | None = None,
repo_tech_stack: Iterable[str] | None = None,
attack_phase: str = "static",
top_k: int = 5,
pin: Iterable[str] | None = None,
) -> str:
"""
Render the matched skills into a context block ready to be prepended to
any LLM system prompt. Returns ``""`` when no skills match.
"""
chosen = pack(
task_description,
repo_languages=repo_languages,
repo_tech_stack=repo_tech_stack,
attack_phase=attack_phase,
top_k=top_k,
pin=pin,
)
if not chosen:
return ""
parts: list[str] = ["<skills>"]
parts.append(
f" <!-- Loaded {len(chosen)} skill(s) for phase={attack_phase!r}. "
"Apply them precisely; cite the skill name when you use it. -->"
)
for m in chosen:
parts.append(f" <skill name=\"{m.skill.name}\" domain=\"{m.skill.domain}\" "
f"score=\"{m.score:.3f}\" reason=\"{m.reason}\">")
parts.append(m.skill.body.strip())
parts.append(" </skill>")
parts.append("</skills>")
return "\n".join(parts)
def explain(task_description: str, **kwargs: Any) -> dict[str, Any]:
"""Diagnostic: returns the ranked match list as JSON-able dicts."""
chosen = pack(task_description, **kwargs)
return {
"task": task_description,
"engine": "semantic" if _try_load_model() else "keyword",
"matches": [m.to_dict() for m in chosen],
}
def stats() -> dict[str, Any]:
skills = skill_registry.load_all()
embeds = _EMBEDS or {}
return {
"total_skills": len(skills),
"embedded_skills": len(embeds),
"engine": "semantic" if _try_load_model() else "keyword",
"cache_dir": str(CACHE_DIR),
}
|