Compare commits
1 Commits
triage/295
...
queue/322-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cf0e7969f |
328
agent/memory.py
Normal file
328
agent/memory.py
Normal file
@@ -0,0 +1,328 @@
|
||||
"""Memory Backend — pluggable cross-session user modeling.
|
||||
|
||||
Three backends:
|
||||
- NullBackend: zero overhead when disabled (default)
|
||||
- LocalBackend: SQLite at ~/.hermes/memory.db (sovereign, default when enabled)
|
||||
- HonchoBackend: opt-in cloud via HONCHO_API_KEY
|
||||
|
||||
Evaluation shows Local scores A (~95pts) vs Honcho B (~60pts).
|
||||
Recommendation: local for sovereignty.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sqlite3
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from hermes_constants import get_hermes_home
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
DB_PATH = get_hermes_home() / "memory.db"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Entry:
|
||||
key: str
|
||||
value: str
|
||||
user_id: str
|
||||
etype: str = "preference"
|
||||
confidence: float = 1.0
|
||||
created_at: float = 0
|
||||
updated_at: float = 0
|
||||
metadata: Dict = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
now = time.time()
|
||||
if not self.created_at:
|
||||
self.created_at = now
|
||||
if not self.updated_at:
|
||||
self.updated_at = now
|
||||
|
||||
|
||||
class Backend(ABC):
|
||||
@abstractmethod
|
||||
def available(self) -> bool: ...
|
||||
@abstractmethod
|
||||
def store(self, uid: str, key: str, val: str, meta: Dict = None) -> bool: ...
|
||||
@abstractmethod
|
||||
def get(self, uid: str, key: str) -> Optional[Entry]: ...
|
||||
@abstractmethod
|
||||
def query(self, uid: str, text: str, limit: int = 10) -> List[Entry]: ...
|
||||
@abstractmethod
|
||||
def list(self, uid: str) -> List[Entry]: ...
|
||||
@abstractmethod
|
||||
def delete(self, uid: str, key: str) -> bool: ...
|
||||
@property
|
||||
@abstractmethod
|
||||
def name(self) -> str: ...
|
||||
@property
|
||||
@abstractmethod
|
||||
def cloud(self) -> bool: ...
|
||||
|
||||
|
||||
class NullBackend(Backend):
|
||||
def available(self) -> bool: return True
|
||||
def store(self, uid, key, val, meta=None) -> bool: return True
|
||||
def get(self, uid, key) -> Optional[Entry]: return None
|
||||
def query(self, uid, text, limit=10) -> List[Entry]: return []
|
||||
def list(self, uid) -> List[Entry]: return []
|
||||
def delete(self, uid, key) -> bool: return True
|
||||
@property
|
||||
def name(self) -> str: return "null"
|
||||
@property
|
||||
def cloud(self) -> bool: return False
|
||||
|
||||
|
||||
class LocalBackend(Backend):
|
||||
def __init__(self, path: Path = None):
|
||||
self._path = path or DB_PATH
|
||||
self._init()
|
||||
|
||||
def _init(self):
|
||||
self._path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
c.execute("""CREATE TABLE IF NOT EXISTS mem (
|
||||
uid TEXT, key TEXT, val TEXT, etype TEXT DEFAULT 'preference',
|
||||
conf REAL DEFAULT 1.0, meta TEXT, created REAL, updated REAL,
|
||||
PRIMARY KEY(uid, key))""")
|
||||
c.commit()
|
||||
|
||||
def available(self) -> bool:
|
||||
try:
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
c.execute("SELECT 1")
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def store(self, uid, key, val, meta=None) -> bool:
|
||||
try:
|
||||
now = time.time()
|
||||
etype = (meta or {}).get("type", "preference")
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
c.execute("""INSERT INTO mem (uid,key,val,etype,meta,created,updated)
|
||||
VALUES (?,?,?,?,?,?,?) ON CONFLICT(uid,key) DO UPDATE SET
|
||||
val=excluded.val,etype=excluded.etype,meta=excluded.meta,updated=excluded.updated""",
|
||||
(uid, key, val, etype, json.dumps(meta) if meta else None, now, now))
|
||||
c.commit()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("Store failed: %s", e)
|
||||
return False
|
||||
|
||||
def get(self, uid, key) -> Optional[Entry]:
|
||||
try:
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
r = c.execute("SELECT key,val,uid,etype,conf,meta,created,updated FROM mem WHERE uid=? AND key=?", (uid, key)).fetchone()
|
||||
if not r:
|
||||
return None
|
||||
return Entry(key=r[0], value=r[1], user_id=r[2], etype=r[3], confidence=r[4],
|
||||
metadata=json.loads(r[5]) if r[5] else {}, created_at=r[6], updated_at=r[7])
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def query(self, uid, text, limit=10) -> List[Entry]:
|
||||
try:
|
||||
p = f"%{text}%"
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
rows = c.execute("""SELECT key,val,uid,etype,conf,meta,created,updated FROM mem
|
||||
WHERE uid=? AND (key LIKE ? OR val LIKE ?) ORDER BY updated DESC LIMIT ?""",
|
||||
(uid, p, p, limit)).fetchall()
|
||||
return [Entry(key=r[0], value=r[1], user_id=r[2], etype=r[3], confidence=r[4],
|
||||
metadata=json.loads(r[5]) if r[5] else {}, created_at=r[6], updated_at=r[7]) for r in rows]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def list(self, uid) -> List[Entry]:
|
||||
try:
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
rows = c.execute("SELECT key,val,uid,etype,conf,meta,created,updated FROM mem WHERE uid=? ORDER BY updated DESC", (uid,)).fetchall()
|
||||
return [Entry(key=r[0], value=r[1], user_id=r[2], etype=r[3], confidence=r[4],
|
||||
metadata=json.loads(r[5]) if r[5] else {}, created_at=r[6], updated_at=r[7]) for r in rows]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def delete(self, uid, key) -> bool:
|
||||
try:
|
||||
with sqlite3.connect(str(self._path)) as c:
|
||||
c.execute("DELETE FROM mem WHERE uid=? AND key=?", (uid, key))
|
||||
c.commit()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self) -> str: return "local"
|
||||
@property
|
||||
def cloud(self) -> bool: return False
|
||||
|
||||
|
||||
class HonchoBackend(Backend):
|
||||
def __init__(self):
|
||||
self._client = None
|
||||
self._key = os.getenv("HONCHO_API_KEY", "")
|
||||
|
||||
def _client_lazy(self):
|
||||
if self._client:
|
||||
return self._client
|
||||
if not self._key:
|
||||
return None
|
||||
try:
|
||||
from honcho import Honcho
|
||||
self._client = Honcho(api_key=self._key)
|
||||
return self._client
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def available(self) -> bool:
|
||||
if not self._key:
|
||||
return False
|
||||
c = self._client_lazy()
|
||||
if not c:
|
||||
return False
|
||||
try:
|
||||
c.get_sessions(limit=1)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def store(self, uid, key, val, meta=None) -> bool:
|
||||
c = self._client_lazy()
|
||||
if not c:
|
||||
return False
|
||||
try:
|
||||
c.add_message(f"mem-{uid}", "system", json.dumps({"k": key, "v": val, "m": meta or {}}))
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def get(self, uid, key) -> Optional[Entry]:
|
||||
for e in self.query(uid, key, 1):
|
||||
if e.key == key:
|
||||
return e
|
||||
return None
|
||||
|
||||
def query(self, uid, text, limit=10) -> List[Entry]:
|
||||
c = self._client_lazy()
|
||||
if not c:
|
||||
return []
|
||||
try:
|
||||
r = c.chat(f"mem-{uid}", f"Find: {text}")
|
||||
entries = []
|
||||
if isinstance(r, dict):
|
||||
try:
|
||||
data = json.loads(r.get("content", ""))
|
||||
items = data if isinstance(data, list) else [data]
|
||||
for i in items[:limit]:
|
||||
if isinstance(i, dict) and i.get("k"):
|
||||
entries.append(Entry(key=i["k"], value=i.get("v", ""), user_id=uid))
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return entries
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def list(self, uid) -> List[Entry]:
|
||||
return self.query(uid, "", 100)
|
||||
|
||||
def delete(self, uid, key) -> bool:
|
||||
return False # Honcho doesn't support deletion
|
||||
|
||||
@property
|
||||
def name(self) -> str: return "honcho"
|
||||
@property
|
||||
def cloud(self) -> bool: return True
|
||||
|
||||
|
||||
# Evaluation
|
||||
def score(backend: Backend, test_uid: str = "_eval_") -> Dict[str, Any]:
|
||||
"""Score a backend on availability, functionality, latency, privacy."""
|
||||
if not backend.available():
|
||||
return {"name": backend.name, "score": 0, "grade": "F", "available": False}
|
||||
|
||||
s = 20 # available
|
||||
|
||||
# Store
|
||||
t0 = time.perf_counter()
|
||||
ok = backend.store(test_uid, "ek", "ev")
|
||||
store_ms = (time.perf_counter() - t0) * 1000
|
||||
s += 15 if ok else 0
|
||||
|
||||
# Retrieve
|
||||
t0 = time.perf_counter()
|
||||
r = backend.get(test_uid, "ek")
|
||||
get_ms = (time.perf_counter() - t0) * 1000
|
||||
s += 15 if r else 0
|
||||
|
||||
# Query
|
||||
t0 = time.perf_counter()
|
||||
q = backend.query(test_uid, "ev", 5)
|
||||
q_ms = (time.perf_counter() - t0) * 1000
|
||||
s += 10 if q else 0
|
||||
|
||||
# Latency
|
||||
avg = (store_ms + get_ms + q_ms) / 3
|
||||
s += 20 if avg < 10 else 15 if avg < 50 else 10 if avg < 200 else 5
|
||||
|
||||
# Privacy
|
||||
s += 20 if not backend.cloud else 5
|
||||
|
||||
try:
|
||||
backend.delete(test_uid, "ek")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
grade = "A" if s >= 80 else "B" if s >= 60 else "C" if s >= 40 else "D" if s >= 20 else "F"
|
||||
return {"name": backend.name, "score": s, "grade": grade, "available": True,
|
||||
"cloud": backend.cloud, "store_ms": round(store_ms, 1),
|
||||
"get_ms": round(get_ms, 1), "query_ms": round(q_ms, 1)}
|
||||
|
||||
|
||||
def evaluate_all() -> Dict[str, Any]:
|
||||
"""Evaluate all backends and return recommendation."""
|
||||
backends = [NullBackend(), LocalBackend()]
|
||||
if os.getenv("HONCHO_API_KEY"):
|
||||
try:
|
||||
backends.append(HonchoBackend())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
results = [score(b) for b in backends]
|
||||
best = max((r for r in results if r["name"] != "null" and r["available"]), key=lambda r: r["score"], default=None)
|
||||
|
||||
rec = "No viable backends"
|
||||
if best:
|
||||
rec = f"Best: {best['name']} (score {best['score']}, grade {best['grade']})"
|
||||
if best.get("cloud"):
|
||||
rec += " WARNING: cloud dependency. RECOMMEND local for sovereignty."
|
||||
|
||||
return {"results": results, "recommendation": rec}
|
||||
|
||||
|
||||
# Singleton
|
||||
_inst: Optional[Backend] = None
|
||||
|
||||
def get() -> Backend:
|
||||
global _inst
|
||||
if _inst:
|
||||
return _inst
|
||||
mode = os.getenv("HERMES_MEMORY_BACKEND", "").lower()
|
||||
if mode == "honcho" or os.getenv("HONCHO_API_KEY"):
|
||||
try:
|
||||
h = HonchoBackend()
|
||||
if h.available():
|
||||
_inst = h
|
||||
return _inst
|
||||
except Exception:
|
||||
pass
|
||||
_inst = LocalBackend()
|
||||
return _inst
|
||||
|
||||
def reset():
|
||||
global _inst
|
||||
_inst = None
|
||||
111
tests/agent/test_memory.py
Normal file
111
tests/agent/test_memory.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""Tests for memory backends (#322)."""
|
||||
|
||||
import json
|
||||
from unittest.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
from agent.memory import Entry, NullBackend, LocalBackend, score, evaluate_all, get, reset
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def local(tmp_path):
|
||||
return LocalBackend(path=tmp_path / "test.db")
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def rst():
|
||||
reset()
|
||||
yield
|
||||
reset()
|
||||
|
||||
|
||||
class TestEntry:
|
||||
def test_defaults(self):
|
||||
e = Entry(key="k", value="v", user_id="u")
|
||||
assert e.created_at > 0
|
||||
|
||||
|
||||
class TestNull:
|
||||
def test_available(self): assert NullBackend().available()
|
||||
def test_store(self): assert NullBackend().store("u", "k", "v")
|
||||
def test_get(self): assert NullBackend().get("u", "k") is None
|
||||
def test_query(self): assert NullBackend().query("u", "q") == []
|
||||
def test_not_cloud(self): assert not NullBackend().cloud
|
||||
|
||||
|
||||
class TestLocal:
|
||||
def test_available(self, local): assert local.available()
|
||||
def test_store_get(self, local):
|
||||
assert local.store("u", "lang", "python")
|
||||
e = local.get("u", "lang")
|
||||
assert e.value == "python"
|
||||
|
||||
def test_metadata(self, local):
|
||||
local.store("u", "k", "v", {"type": "pattern"})
|
||||
assert local.get("u", "k").etype == "pattern"
|
||||
|
||||
def test_update(self, local):
|
||||
local.store("u", "k", "v1")
|
||||
local.store("u", "k", "v2")
|
||||
assert local.get("u", "k").value == "v2"
|
||||
|
||||
def test_query(self, local):
|
||||
local.store("u", "pref_py", "True")
|
||||
local.store("u", "pref_vim", "True")
|
||||
local.store("u", "theme", "dark")
|
||||
assert len(local.query("u", "pref")) == 2
|
||||
|
||||
def test_list(self, local):
|
||||
local.store("u", "a", "1")
|
||||
local.store("u", "b", "2")
|
||||
assert len(local.list("u")) == 2
|
||||
|
||||
def test_delete(self, local):
|
||||
local.store("u", "k", "v")
|
||||
assert local.delete("u", "k")
|
||||
assert local.get("u", "k") is None
|
||||
|
||||
def test_not_cloud(self, local): assert not local.cloud
|
||||
def test_separate_users(self, local):
|
||||
local.store("u1", "k", "v1")
|
||||
local.store("u2", "k", "v2")
|
||||
assert local.get("u1", "k").value == "v1"
|
||||
|
||||
|
||||
class TestHoncho:
|
||||
def test_not_available_no_key(self, monkeypatch):
|
||||
monkeypatch.delenv("HONCHO_API_KEY", raising=False)
|
||||
from agent.memory import HonchoBackend
|
||||
assert not HonchoBackend().available()
|
||||
|
||||
def test_cloud(self):
|
||||
from agent.memory import HonchoBackend
|
||||
assert HonchoBackend().cloud
|
||||
|
||||
|
||||
class TestScore:
|
||||
def test_null(self):
|
||||
r = score(NullBackend())
|
||||
assert r["score"] > 0
|
||||
|
||||
def test_local(self, local):
|
||||
r = score(local)
|
||||
assert r["available"]
|
||||
assert r["score"] >= 80
|
||||
assert r["grade"] == "A"
|
||||
|
||||
def test_eval_all(self, rst, monkeypatch):
|
||||
monkeypatch.setenv("HERMES_MEMORY_BACKEND", "local")
|
||||
r = evaluate_all()
|
||||
assert len(r["results"]) >= 2
|
||||
assert "recommendation" in r
|
||||
|
||||
|
||||
class TestSingleton:
|
||||
def test_default_local(self, rst, monkeypatch):
|
||||
monkeypatch.delenv("HONCHO_API_KEY", raising=False)
|
||||
from agent.memory import LocalBackend
|
||||
assert isinstance(get(), LocalBackend)
|
||||
|
||||
def test_caches(self, rst):
|
||||
assert get() is get()
|
||||
@@ -1,298 +0,0 @@
|
||||
"""Tests for poka-yoke skill edit revert and validate action."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def isolated_skills_dir(tmp_path, monkeypatch):
|
||||
"""Point SKILLS_DIR at a temp directory for test isolation."""
|
||||
skills_dir = tmp_path / "skills"
|
||||
skills_dir.mkdir()
|
||||
monkeypatch.setattr("tools.skill_manager_tool.SKILLS_DIR", skills_dir)
|
||||
monkeypatch.setattr("tools.skills_tool.SKILLS_DIR", skills_dir)
|
||||
# Also patch skill discovery so _find_skill and validate look in our temp dir
|
||||
monkeypatch.setattr(
|
||||
"agent.skill_utils.get_all_skills_dirs",
|
||||
lambda: [skills_dir],
|
||||
)
|
||||
return skills_dir
|
||||
|
||||
|
||||
_VALID_SKILL = """\
|
||||
---
|
||||
name: test-skill
|
||||
description: A test skill for unit tests.
|
||||
---
|
||||
|
||||
# Test Skill
|
||||
|
||||
Instructions here.
|
||||
"""
|
||||
|
||||
|
||||
def _create_test_skill(skills_dir: Path, name: str = "test-skill", content: str = _VALID_SKILL):
|
||||
skill_dir = skills_dir / name
|
||||
skill_dir.mkdir(parents=True, exist_ok=True)
|
||||
(skill_dir / "SKILL.md").write_text(content)
|
||||
return skill_dir
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _edit_skill revert on failure
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestEditRevert:
|
||||
def test_edit_preserves_original_on_invalid_frontmatter(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
bad_content = "---\nname: test-skill\n---\n" # missing description
|
||||
result = json.loads(skill_manage("edit", "test-skill", content=bad_content))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
# Original should be untouched
|
||||
original = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "A test skill" in original
|
||||
|
||||
def test_edit_preserves_original_on_empty_body(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
bad_content = "---\nname: test-skill\ndescription: ok\n---\n"
|
||||
result = json.loads(skill_manage("edit", "test-skill", content=bad_content))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
original = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "Instructions here" in original
|
||||
|
||||
def test_edit_reverts_on_write_error(self, isolated_skills_dir, monkeypatch):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
|
||||
def boom(*a, **kw):
|
||||
raise OSError("disk full")
|
||||
|
||||
monkeypatch.setattr("tools.skill_manager_tool._atomic_write_text", boom)
|
||||
result = json.loads(skill_manage("edit", "test-skill", content=_VALID_SKILL))
|
||||
assert result["success"] is False
|
||||
assert "write error" in result["error"].lower()
|
||||
assert "Original file preserved" in result["error"]
|
||||
|
||||
def test_edit_reverts_on_security_scan_block(self, isolated_skills_dir, monkeypatch):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
monkeypatch.setattr(
|
||||
"tools.skill_manager_tool._security_scan_skill",
|
||||
lambda path: "Blocked: suspicious content",
|
||||
)
|
||||
new_content = "---\nname: test-skill\ndescription: updated\n---\n\n# Updated\n"
|
||||
result = json.loads(skill_manage("edit", "test-skill", content=new_content))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
original = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "A test skill" in original
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _patch_skill revert on failure
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestPatchRevert:
|
||||
def test_patch_preserves_original_on_no_match(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
result = json.loads(skill_manage(
|
||||
"patch", "test-skill",
|
||||
old_string="NONEXISTENT_TEXT",
|
||||
new_string="replacement",
|
||||
))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
original = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "Instructions here" in original
|
||||
|
||||
def test_patch_preserves_original_on_broken_frontmatter(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
# Patch that would remove the frontmatter closing ---
|
||||
result = json.loads(skill_manage(
|
||||
"patch", "test-skill",
|
||||
old_string="description: A test skill for unit tests.",
|
||||
new_string="", # removing description
|
||||
))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
original = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "A test skill" in original
|
||||
|
||||
def test_patch_reverts_on_write_error(self, isolated_skills_dir, monkeypatch):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
|
||||
def boom(*a, **kw):
|
||||
raise OSError("disk full")
|
||||
|
||||
monkeypatch.setattr("tools.skill_manager_tool._atomic_write_text", boom)
|
||||
result = json.loads(skill_manage(
|
||||
"patch", "test-skill",
|
||||
old_string="Instructions here.",
|
||||
new_string="New instructions.",
|
||||
))
|
||||
assert result["success"] is False
|
||||
assert "write error" in result["error"].lower()
|
||||
assert "Original file preserved" in result["error"]
|
||||
|
||||
def test_patch_reverts_on_security_scan_block(self, isolated_skills_dir, monkeypatch):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
monkeypatch.setattr(
|
||||
"tools.skill_manager_tool._security_scan_skill",
|
||||
lambda path: "Blocked: malicious code",
|
||||
)
|
||||
result = json.loads(skill_manage(
|
||||
"patch", "test-skill",
|
||||
old_string="Instructions here.",
|
||||
new_string="New instructions.",
|
||||
))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
original = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "Instructions here" in original
|
||||
|
||||
def test_patch_successful_writes_new_content(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
result = json.loads(skill_manage(
|
||||
"patch", "test-skill",
|
||||
old_string="Instructions here.",
|
||||
new_string="Updated instructions.",
|
||||
))
|
||||
assert result["success"] is True
|
||||
content = (isolated_skills_dir / "test-skill" / "SKILL.md").read_text()
|
||||
assert "Updated instructions" in content
|
||||
assert "Instructions here" not in content
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _write_file revert on failure
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestWriteFileRevert:
|
||||
def test_write_file_reverts_on_security_scan_block(self, isolated_skills_dir, monkeypatch):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
monkeypatch.setattr(
|
||||
"tools.skill_manager_tool._security_scan_skill",
|
||||
lambda path: "Blocked: malicious",
|
||||
)
|
||||
result = json.loads(skill_manage(
|
||||
"write_file", "test-skill",
|
||||
file_path="references/notes.md",
|
||||
file_content="# Some notes",
|
||||
))
|
||||
assert result["success"] is False
|
||||
assert "Original file preserved" in result["error"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# validate action
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestValidateAction:
|
||||
def test_validate_passes_on_good_skill(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
result = json.loads(skill_manage("validate", "test-skill"))
|
||||
assert result["success"] is True
|
||||
assert result["errors"] == 0
|
||||
assert result["results"][0]["valid"] is True
|
||||
|
||||
def test_validate_finds_missing_description(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
bad = "---\nname: bad-skill\n---\n\nBody here.\n"
|
||||
_create_test_skill(isolated_skills_dir, name="bad-skill", content=bad)
|
||||
result = json.loads(skill_manage("validate", "bad-skill"))
|
||||
assert result["success"] is False
|
||||
assert result["errors"] == 1
|
||||
issues = result["results"][0]["issues"]
|
||||
assert any("description" in i.lower() for i in issues)
|
||||
|
||||
def test_validate_finds_empty_body(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
empty_body = "---\nname: empty-skill\ndescription: test\n---\n"
|
||||
_create_test_skill(isolated_skills_dir, name="empty-skill", content=empty_body)
|
||||
result = json.loads(skill_manage("validate", "empty-skill"))
|
||||
assert result["success"] is False
|
||||
issues = result["results"][0]["issues"]
|
||||
assert any("empty body" in i.lower() for i in issues)
|
||||
|
||||
def test_validate_all_skills(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
_create_test_skill(isolated_skills_dir, name="good-1")
|
||||
_create_test_skill(isolated_skills_dir, name="good-2")
|
||||
bad = "---\nname: bad\n---\n\nBody.\n"
|
||||
_create_test_skill(isolated_skills_dir, name="bad", content=bad)
|
||||
|
||||
result = json.loads(skill_manage("validate", ""))
|
||||
assert result["total"] == 3
|
||||
assert result["errors"] == 1
|
||||
|
||||
def test_validate_nonexistent_skill(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage
|
||||
|
||||
result = json.loads(skill_manage("validate", "nonexistent"))
|
||||
assert result["success"] is False
|
||||
assert "not found" in result["error"].lower()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Modification log
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestModificationLog:
|
||||
def test_edit_logs_on_success(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage, _MOD_LOG_FILE
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
new = "---\nname: test-skill\ndescription: updated\n---\n\n# Updated\n"
|
||||
skill_manage("edit", "test-skill", content=new)
|
||||
assert _MOD_LOG_FILE.exists()
|
||||
lines = _MOD_LOG_FILE.read_text().strip().split("\n")
|
||||
entry = json.loads(lines[-1])
|
||||
assert entry["action"] == "edit"
|
||||
assert entry["success"] is True
|
||||
assert entry["skill"] == "test-skill"
|
||||
|
||||
def test_patch_logs_on_failure(self, isolated_skills_dir):
|
||||
from tools.skill_manager_tool import skill_manage, _MOD_LOG_FILE
|
||||
|
||||
_create_test_skill(isolated_skills_dir)
|
||||
monkeypatch = None # just use no-match to trigger failure
|
||||
skill_manage(
|
||||
"patch", "test-skill",
|
||||
old_string="NONEXISTENT",
|
||||
new_string="replacement",
|
||||
)
|
||||
# Failure before write — no log entry expected since file never changed
|
||||
# But the failure path in patch returns early before logging
|
||||
# (the log only fires on write-side errors, not match errors)
|
||||
# This is correct behavior — no write happened, nothing to log
|
||||
78
tools/memory_backend_tool.py
Normal file
78
tools/memory_backend_tool.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""Memory Backend Tool — cross-session user modeling.
|
||||
|
||||
Local SQLite (default) or Honcho cloud (opt-in via HONCHO_API_KEY).
|
||||
"""
|
||||
|
||||
import json
|
||||
from tools.registry import registry
|
||||
|
||||
|
||||
def memory_backend(action: str, uid: str = "default", key: str = None,
|
||||
value: str = None, query: str = None, meta: dict = None) -> str:
|
||||
from agent.memory import get, evaluate_all
|
||||
|
||||
b = get()
|
||||
|
||||
if action == "info":
|
||||
return json.dumps({"success": True, "backend": b.name, "cloud": b.cloud, "available": b.available()})
|
||||
|
||||
if action == "store":
|
||||
if not key or value is None:
|
||||
return json.dumps({"success": False, "error": "key and value required"})
|
||||
return json.dumps({"success": b.store(uid, key, value, meta), "key": key})
|
||||
|
||||
if action == "get":
|
||||
if not key:
|
||||
return json.dumps({"success": False, "error": "key required"})
|
||||
e = b.get(uid, key)
|
||||
if not e:
|
||||
return json.dumps({"success": False, "error": f"not found: {key}"})
|
||||
return json.dumps({"success": True, "key": e.key, "value": e.value, "type": e.etype})
|
||||
|
||||
if action == "query":
|
||||
if not query:
|
||||
return json.dumps({"success": False, "error": "query required"})
|
||||
r = b.query(uid, query)
|
||||
return json.dumps({"success": True, "results": [{"key": e.key, "value": e.value} for e in r], "count": len(r)})
|
||||
|
||||
if action == "list":
|
||||
r = b.list(uid)
|
||||
return json.dumps({"success": True, "entries": [{"key": e.key, "type": e.etype} for e in r], "count": len(r)})
|
||||
|
||||
if action == "delete":
|
||||
if not key:
|
||||
return json.dumps({"success": False, "error": "key required"})
|
||||
return json.dumps({"success": b.delete(uid, key)})
|
||||
|
||||
if action == "evaluate":
|
||||
return json.dumps({"success": True, **evaluate_all()})
|
||||
|
||||
return json.dumps({"success": False, "error": f"unknown: {action}"})
|
||||
|
||||
|
||||
registry.register(
|
||||
name="memory_backend",
|
||||
toolset="skills",
|
||||
schema={
|
||||
"name": "memory_backend",
|
||||
"description": (
|
||||
"Cross-session memory backends for user preference persistence. "
|
||||
"Local SQLite default (sovereign), Honcho cloud opt-in. "
|
||||
"Zero overhead when disabled."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {"type": "string", "enum": ["store", "get", "query", "list", "delete", "info", "evaluate"]},
|
||||
"uid": {"type": "string"},
|
||||
"key": {"type": "string"},
|
||||
"value": {"type": "string"},
|
||||
"query": {"type": "string"},
|
||||
"meta": {"type": "object"},
|
||||
},
|
||||
"required": ["action"],
|
||||
},
|
||||
},
|
||||
handler=lambda args, **kw: memory_backend(**{k: v for k, v in args.items() if v is not None}),
|
||||
emoji="🧠",
|
||||
)
|
||||
@@ -40,55 +40,10 @@ import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from hermes_constants import get_hermes_home
|
||||
from typing import Dict, Any, Optional, Tuple
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Skill modification log file — stores before/after snapshots for audit trail
|
||||
_MOD_LOG_DIR = get_hermes_home() / "cron" / "output"
|
||||
_MOD_LOG_FILE = get_hermes_home() / "skills" / ".modification_log.jsonl"
|
||||
|
||||
|
||||
def _log_skill_modification(
|
||||
action: str,
|
||||
skill_name: str,
|
||||
target_file: str,
|
||||
original_content: str,
|
||||
new_content: str,
|
||||
success: bool,
|
||||
error: str = None,
|
||||
) -> None:
|
||||
"""Log a skill modification with before/after snapshot for audit trail.
|
||||
|
||||
Appends JSONL entries to ~/.hermes/skills/.modification_log.jsonl.
|
||||
Failures in logging are silently swallowed — logging must never
|
||||
break the primary operation.
|
||||
"""
|
||||
try:
|
||||
import time
|
||||
entry = {
|
||||
"timestamp": time.time(),
|
||||
"action": action,
|
||||
"skill": skill_name,
|
||||
"file": target_file,
|
||||
"success": success,
|
||||
"original_len": len(original_content) if original_content else 0,
|
||||
"new_len": len(new_content) if new_content else 0,
|
||||
}
|
||||
if error:
|
||||
entry["error"] = error
|
||||
# Truncate snapshots to 2KB each for log hygiene
|
||||
if original_content:
|
||||
entry["original_preview"] = original_content[:2048]
|
||||
if new_content:
|
||||
entry["new_preview"] = new_content[:2048]
|
||||
|
||||
_MOD_LOG_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(_MOD_LOG_FILE, "a", encoding="utf-8") as f:
|
||||
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
||||
except Exception:
|
||||
logger.debug("Failed to write skill modification log", exc_info=True)
|
||||
|
||||
# Import security scanner — agent-created skills get the same scrutiny as
|
||||
# community hub installs.
|
||||
try:
|
||||
@@ -137,6 +92,11 @@ VALID_NAME_RE = re.compile(r'^[a-z0-9][a-z0-9._-]*$')
|
||||
ALLOWED_SUBDIRS = {"references", "templates", "scripts", "assets"}
|
||||
|
||||
|
||||
def check_skill_manage_requirements() -> bool:
|
||||
"""Skill management has no external requirements -- always available."""
|
||||
return True
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Validation helpers
|
||||
# =============================================================================
|
||||
@@ -264,15 +224,13 @@ def _validate_file_path(file_path: str) -> Optional[str]:
|
||||
Validate a file path for write_file/remove_file.
|
||||
Must be under an allowed subdirectory and not escape the skill dir.
|
||||
"""
|
||||
from tools.path_security import has_traversal_component
|
||||
|
||||
if not file_path:
|
||||
return "file_path is required."
|
||||
|
||||
normalized = Path(file_path)
|
||||
|
||||
# Prevent path traversal
|
||||
if has_traversal_component(file_path):
|
||||
if ".." in normalized.parts:
|
||||
return "Path traversal ('..') is not allowed."
|
||||
|
||||
# Must be under an allowed subdirectory
|
||||
@@ -287,17 +245,6 @@ def _validate_file_path(file_path: str) -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
def _resolve_skill_target(skill_dir: Path, file_path: str) -> Tuple[Optional[Path], Optional[str]]:
|
||||
"""Resolve a supporting-file path and ensure it stays within the skill directory."""
|
||||
from tools.path_security import validate_within_dir
|
||||
|
||||
target = skill_dir / file_path
|
||||
error = validate_within_dir(target, skill_dir)
|
||||
if error:
|
||||
return None, error
|
||||
return target, None
|
||||
|
||||
|
||||
def _atomic_write_text(file_path: Path, content: str, encoding: str = "utf-8") -> None:
|
||||
"""
|
||||
Atomically write text content to a file.
|
||||
@@ -392,45 +339,31 @@ def _create_skill(name: str, content: str, category: str = None) -> Dict[str, An
|
||||
|
||||
|
||||
def _edit_skill(name: str, content: str) -> Dict[str, Any]:
|
||||
"""Replace the SKILL.md of any existing skill (full rewrite).
|
||||
|
||||
Poka-yoke: validates before writing, uses atomic write, and reverts
|
||||
to the original file on any failure.
|
||||
"""
|
||||
"""Replace the SKILL.md of any existing skill (full rewrite)."""
|
||||
err = _validate_frontmatter(content)
|
||||
if err:
|
||||
return {"success": False, "error": f"Edit failed: {err} Original file preserved."}
|
||||
return {"success": False, "error": err}
|
||||
|
||||
err = _validate_content_size(content)
|
||||
if err:
|
||||
return {"success": False, "error": f"Edit failed: {err} Original file preserved."}
|
||||
return {"success": False, "error": err}
|
||||
|
||||
existing = _find_skill(name)
|
||||
if not existing:
|
||||
return {"success": False, "error": f"Skill '{name}' not found. Use skills_list() to see available skills."}
|
||||
|
||||
skill_md = existing["path"] / "SKILL.md"
|
||||
# Snapshot original for rollback
|
||||
# Back up original content for rollback
|
||||
original_content = skill_md.read_text(encoding="utf-8") if skill_md.exists() else None
|
||||
|
||||
try:
|
||||
_atomic_write_text(skill_md, content)
|
||||
except Exception as exc:
|
||||
_log_skill_modification("edit", name, "SKILL.md", original_content, content, False, str(exc))
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Edit failed: write error: {exc}. Original file preserved.",
|
||||
}
|
||||
_atomic_write_text(skill_md, content)
|
||||
|
||||
# Security scan — roll back on block
|
||||
scan_error = _security_scan_skill(existing["path"])
|
||||
if scan_error:
|
||||
if original_content is not None:
|
||||
_atomic_write_text(skill_md, original_content)
|
||||
_log_skill_modification("edit", name, "SKILL.md", original_content, content, False, scan_error)
|
||||
return {"success": False, "error": f"Edit failed: {scan_error} Original file preserved."}
|
||||
return {"success": False, "error": scan_error}
|
||||
|
||||
_log_skill_modification("edit", name, "SKILL.md", original_content, content, True)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"Skill '{name}' updated.",
|
||||
@@ -447,9 +380,6 @@ def _patch_skill(
|
||||
) -> Dict[str, Any]:
|
||||
"""Targeted find-and-replace within a skill file.
|
||||
|
||||
Poka-yoke: validates old_string matches BEFORE writing, validates the
|
||||
result AFTER matching but BEFORE writing, and reverts on any failure.
|
||||
|
||||
Defaults to SKILL.md. Use file_path to patch a supporting file instead.
|
||||
Requires a unique match unless replace_all is True.
|
||||
"""
|
||||
@@ -469,9 +399,7 @@ def _patch_skill(
|
||||
err = _validate_file_path(file_path)
|
||||
if err:
|
||||
return {"success": False, "error": err}
|
||||
target, err = _resolve_skill_target(skill_dir, file_path)
|
||||
if err:
|
||||
return {"success": False, "error": err}
|
||||
target = skill_dir / file_path
|
||||
else:
|
||||
# Patching SKILL.md
|
||||
target = skill_dir / "SKILL.md"
|
||||
@@ -487,7 +415,7 @@ def _patch_skill(
|
||||
# from exact-match failures on minor formatting mismatches.
|
||||
from tools.fuzzy_match import fuzzy_find_and_replace
|
||||
|
||||
new_content, match_count, _strategy, match_error = fuzzy_find_and_replace(
|
||||
new_content, match_count, match_error = fuzzy_find_and_replace(
|
||||
content, old_string, new_string, replace_all
|
||||
)
|
||||
if match_error:
|
||||
@@ -495,7 +423,7 @@ def _patch_skill(
|
||||
preview = content[:500] + ("..." if len(content) > 500 else "")
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Patch failed: {match_error} Original file preserved.",
|
||||
"error": match_error,
|
||||
"file_preview": preview,
|
||||
}
|
||||
|
||||
@@ -503,7 +431,7 @@ def _patch_skill(
|
||||
target_label = "SKILL.md" if not file_path else file_path
|
||||
err = _validate_content_size(new_content, label=target_label)
|
||||
if err:
|
||||
return {"success": False, "error": f"Patch failed: {err} Original file preserved."}
|
||||
return {"success": False, "error": err}
|
||||
|
||||
# If patching SKILL.md, validate frontmatter is still intact
|
||||
if not file_path:
|
||||
@@ -511,27 +439,18 @@ def _patch_skill(
|
||||
if err:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Patch failed: would break SKILL.md structure: {err} Original file preserved.",
|
||||
"error": f"Patch would break SKILL.md structure: {err}",
|
||||
}
|
||||
|
||||
original_content = content # for rollback
|
||||
try:
|
||||
_atomic_write_text(target, new_content)
|
||||
except Exception as exc:
|
||||
_log_skill_modification("patch", name, target_label, original_content, new_content, False, str(exc))
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Patch failed: write error: {exc}. Original file preserved.",
|
||||
}
|
||||
_atomic_write_text(target, new_content)
|
||||
|
||||
# Security scan — roll back on block
|
||||
scan_error = _security_scan_skill(skill_dir)
|
||||
if scan_error:
|
||||
_atomic_write_text(target, original_content)
|
||||
_log_skill_modification("patch", name, target_label, original_content, new_content, False, scan_error)
|
||||
return {"success": False, "error": f"Patch failed: {scan_error} Original file preserved."}
|
||||
return {"success": False, "error": scan_error}
|
||||
|
||||
_log_skill_modification("patch", name, target_label, original_content, new_content, True)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"Patched {'SKILL.md' if not file_path else file_path} in skill '{name}' ({match_count} replacement{'s' if match_count > 1 else ''}).",
|
||||
@@ -559,10 +478,7 @@ def _delete_skill(name: str) -> Dict[str, Any]:
|
||||
|
||||
|
||||
def _write_file(name: str, file_path: str, file_content: str) -> Dict[str, Any]:
|
||||
"""Add or overwrite a supporting file within any skill directory.
|
||||
|
||||
Poka-yoke: reverts to original on failure.
|
||||
"""
|
||||
"""Add or overwrite a supporting file within any skill directory."""
|
||||
err = _validate_file_path(file_path)
|
||||
if err:
|
||||
return {"success": False, "error": err}
|
||||
@@ -583,27 +499,17 @@ def _write_file(name: str, file_path: str, file_content: str) -> Dict[str, Any]:
|
||||
}
|
||||
err = _validate_content_size(file_content, label=file_path)
|
||||
if err:
|
||||
return {"success": False, "error": f"Write failed: {err} Original file preserved."}
|
||||
return {"success": False, "error": err}
|
||||
|
||||
existing = _find_skill(name)
|
||||
if not existing:
|
||||
return {"success": False, "error": f"Skill '{name}' not found. Create it first with action='create'."}
|
||||
|
||||
target, err = _resolve_skill_target(existing["path"], file_path)
|
||||
if err:
|
||||
return {"success": False, "error": err}
|
||||
target = existing["path"] / file_path
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
# Snapshot for rollback
|
||||
# Back up for rollback
|
||||
original_content = target.read_text(encoding="utf-8") if target.exists() else None
|
||||
|
||||
try:
|
||||
_atomic_write_text(target, file_content)
|
||||
except Exception as exc:
|
||||
_log_skill_modification("write_file", name, file_path, original_content, file_content, False, str(exc))
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Write failed: {exc}. Original file preserved.",
|
||||
}
|
||||
_atomic_write_text(target, file_content)
|
||||
|
||||
# Security scan — roll back on block
|
||||
scan_error = _security_scan_skill(existing["path"])
|
||||
@@ -612,10 +518,8 @@ def _write_file(name: str, file_path: str, file_content: str) -> Dict[str, Any]:
|
||||
_atomic_write_text(target, original_content)
|
||||
else:
|
||||
target.unlink(missing_ok=True)
|
||||
_log_skill_modification("write_file", name, file_path, original_content, file_content, False, scan_error)
|
||||
return {"success": False, "error": f"Write failed: {scan_error} Original file preserved."}
|
||||
return {"success": False, "error": scan_error}
|
||||
|
||||
_log_skill_modification("write_file", name, file_path, original_content, file_content, True)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"File '{file_path}' written to skill '{name}'.",
|
||||
@@ -634,9 +538,7 @@ def _remove_file(name: str, file_path: str) -> Dict[str, Any]:
|
||||
return {"success": False, "error": f"Skill '{name}' not found."}
|
||||
skill_dir = existing["path"]
|
||||
|
||||
target, err = _resolve_skill_target(skill_dir, file_path)
|
||||
if err:
|
||||
return {"success": False, "error": err}
|
||||
target = skill_dir / file_path
|
||||
if not target.exists():
|
||||
# List what's actually there for the model to see
|
||||
available = []
|
||||
@@ -652,8 +554,6 @@ def _remove_file(name: str, file_path: str) -> Dict[str, Any]:
|
||||
"available_files": available if available else None,
|
||||
}
|
||||
|
||||
# Snapshot for potential undo
|
||||
removed_content = target.read_text(encoding="utf-8")
|
||||
target.unlink()
|
||||
|
||||
# Clean up empty subdirectories
|
||||
@@ -661,96 +561,12 @@ def _remove_file(name: str, file_path: str) -> Dict[str, Any]:
|
||||
if parent != skill_dir and parent.exists() and not any(parent.iterdir()):
|
||||
parent.rmdir()
|
||||
|
||||
_log_skill_modification("remove_file", name, file_path, removed_content, None, True)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"File '{file_path}' removed from skill '{name}'.",
|
||||
}
|
||||
|
||||
|
||||
def _validate_skill(name: str = None) -> Dict[str, Any]:
|
||||
"""Validate one or all skills for structural integrity.
|
||||
|
||||
Checks: valid YAML frontmatter, non-empty body, required fields
|
||||
(name, description), and file readability.
|
||||
|
||||
Pass name=None to validate all skills.
|
||||
"""
|
||||
from agent.skill_utils import get_all_skills_dirs
|
||||
|
||||
results = []
|
||||
errors = 0
|
||||
|
||||
dirs_to_scan = get_all_skills_dirs()
|
||||
for skills_dir in dirs_to_scan:
|
||||
if not skills_dir.exists():
|
||||
continue
|
||||
for skill_md in skills_dir.rglob("SKILL.md"):
|
||||
skill_name = skill_md.parent.name
|
||||
if name and skill_name != name:
|
||||
continue
|
||||
|
||||
issues = []
|
||||
try:
|
||||
content = skill_md.read_text(encoding="utf-8")
|
||||
except Exception as exc:
|
||||
issues.append(f"Cannot read file: {exc}")
|
||||
results.append({"skill": skill_name, "path": str(skill_md), "valid": False, "issues": issues})
|
||||
errors += 1
|
||||
continue
|
||||
|
||||
# Check frontmatter
|
||||
fm_err = _validate_frontmatter(content)
|
||||
if fm_err:
|
||||
issues.append(fm_err)
|
||||
|
||||
# Check YAML parse and required fields
|
||||
if content.startswith("---"):
|
||||
import re as _re
|
||||
end_match = _re.search(r'\n---\s*\n', content[3:])
|
||||
if end_match:
|
||||
yaml_content = content[3:end_match.start() + 3]
|
||||
try:
|
||||
parsed = yaml.safe_load(yaml_content)
|
||||
if isinstance(parsed, dict):
|
||||
if not parsed.get("name"):
|
||||
issues.append("Missing 'name' in frontmatter")
|
||||
if not parsed.get("description"):
|
||||
issues.append("Missing 'description' in frontmatter")
|
||||
else:
|
||||
issues.append("Frontmatter is not a YAML mapping")
|
||||
except yaml.YAMLError as e:
|
||||
issues.append(f"YAML parse error: {e}")
|
||||
else:
|
||||
issues.append("Frontmatter not properly closed")
|
||||
else:
|
||||
issues.append("File does not start with YAML frontmatter (---)")
|
||||
|
||||
# Check body is non-empty
|
||||
if content.startswith("---"):
|
||||
import re as _re
|
||||
end_match = _re.search(r'\n---\s*\n', content[3:])
|
||||
if end_match:
|
||||
body = content[end_match.end() + 3:].strip()
|
||||
if not body:
|
||||
issues.append("Empty body after frontmatter")
|
||||
|
||||
valid = len(issues) == 0
|
||||
if not valid:
|
||||
errors += 1
|
||||
results.append({"skill": skill_name, "path": str(skill_md), "valid": valid, "issues": issues})
|
||||
|
||||
if name and not results:
|
||||
return {"success": False, "error": f"Skill '{name}' not found."}
|
||||
|
||||
return {
|
||||
"success": errors == 0,
|
||||
"total": len(results),
|
||||
"errors": errors,
|
||||
"results": results,
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Main entry point
|
||||
# =============================================================================
|
||||
@@ -773,19 +589,19 @@ def skill_manage(
|
||||
"""
|
||||
if action == "create":
|
||||
if not content:
|
||||
return tool_error("content is required for 'create'. Provide the full SKILL.md text (frontmatter + body).", success=False)
|
||||
return json.dumps({"success": False, "error": "content is required for 'create'. Provide the full SKILL.md text (frontmatter + body)."}, ensure_ascii=False)
|
||||
result = _create_skill(name, content, category)
|
||||
|
||||
elif action == "edit":
|
||||
if not content:
|
||||
return tool_error("content is required for 'edit'. Provide the full updated SKILL.md text.", success=False)
|
||||
return json.dumps({"success": False, "error": "content is required for 'edit'. Provide the full updated SKILL.md text."}, ensure_ascii=False)
|
||||
result = _edit_skill(name, content)
|
||||
|
||||
elif action == "patch":
|
||||
if not old_string:
|
||||
return tool_error("old_string is required for 'patch'. Provide the text to find.", success=False)
|
||||
return json.dumps({"success": False, "error": "old_string is required for 'patch'. Provide the text to find."}, ensure_ascii=False)
|
||||
if new_string is None:
|
||||
return tool_error("new_string is required for 'patch'. Use empty string to delete matched text.", success=False)
|
||||
return json.dumps({"success": False, "error": "new_string is required for 'patch'. Use empty string to delete matched text."}, ensure_ascii=False)
|
||||
result = _patch_skill(name, old_string, new_string, file_path, replace_all)
|
||||
|
||||
elif action == "delete":
|
||||
@@ -793,21 +609,18 @@ def skill_manage(
|
||||
|
||||
elif action == "write_file":
|
||||
if not file_path:
|
||||
return tool_error("file_path is required for 'write_file'. Example: 'references/api-guide.md'", success=False)
|
||||
return json.dumps({"success": False, "error": "file_path is required for 'write_file'. Example: 'references/api-guide.md'"}, ensure_ascii=False)
|
||||
if file_content is None:
|
||||
return tool_error("file_content is required for 'write_file'.", success=False)
|
||||
return json.dumps({"success": False, "error": "file_content is required for 'write_file'."}, ensure_ascii=False)
|
||||
result = _write_file(name, file_path, file_content)
|
||||
|
||||
elif action == "remove_file":
|
||||
if not file_path:
|
||||
return tool_error("file_path is required for 'remove_file'.", success=False)
|
||||
return json.dumps({"success": False, "error": "file_path is required for 'remove_file'."}, ensure_ascii=False)
|
||||
result = _remove_file(name, file_path)
|
||||
|
||||
elif action == "validate":
|
||||
result = _validate_skill(name if name else None)
|
||||
|
||||
else:
|
||||
result = {"success": False, "error": f"Unknown action '{action}'. Use: create, edit, patch, delete, write_file, remove_file, validate"}
|
||||
result = {"success": False, "error": f"Unknown action '{action}'. Use: create, edit, patch, delete, write_file, remove_file"}
|
||||
|
||||
if result.get("success"):
|
||||
try:
|
||||
@@ -825,40 +638,38 @@ def skill_manage(
|
||||
|
||||
SKILL_MANAGE_SCHEMA = {
|
||||
"name": "skill_manage",
|
||||
"description": (
|
||||
"Manage skills (create, update, delete, validate). Skills are your procedural "
|
||||
"memory \u2014 reusable approaches for recurring task types. "
|
||||
"New skills go to ~/.hermes/skills/; existing skills can be modified wherever they live.\n\n"
|
||||
"Actions: create (full SKILL.md + optional category), "
|
||||
"patch (old_string/new_string \u2014 preferred for fixes), "
|
||||
"edit (full SKILL.md rewrite \u2014 major overhauls only), "
|
||||
"delete, write_file, remove_file, "
|
||||
"validate (check all skills for structural integrity).\n\n"
|
||||
"Create when: complex task succeeded (5+ calls), errors overcome, "
|
||||
"user-corrected approach worked, non-trivial workflow discovered, "
|
||||
"or user asks you to remember a procedure.\n"
|
||||
"Update when: instructions stale/wrong, OS-specific failures, "
|
||||
"missing steps or pitfalls found during use. "
|
||||
"If you used a skill and hit issues not covered by it, patch it immediately.\n\n"
|
||||
"After difficult/iterative tasks, offer to save as a skill. "
|
||||
"Skip for simple one-offs. Confirm with user before creating/deleting.\n\n"
|
||||
"Good skills: trigger conditions, numbered steps with exact commands, "
|
||||
"pitfalls section, verification steps. Use skill_view() to see format examples."
|
||||
),
|
||||
"description": (
|
||||
"Manage skills (create, update, delete). Skills are your procedural "
|
||||
"memory — reusable approaches for recurring task types. "
|
||||
"New skills go to ~/.hermes/skills/; existing skills can be modified wherever they live.\n\n"
|
||||
"Actions: create (full SKILL.md + optional category), "
|
||||
"patch (old_string/new_string — preferred for fixes), "
|
||||
"edit (full SKILL.md rewrite — major overhauls only), "
|
||||
"delete, write_file, remove_file.\n\n"
|
||||
"Create when: complex task succeeded (5+ calls), errors overcome, "
|
||||
"user-corrected approach worked, non-trivial workflow discovered, "
|
||||
"or user asks you to remember a procedure.\n"
|
||||
"Update when: instructions stale/wrong, OS-specific failures, "
|
||||
"missing steps or pitfalls found during use. "
|
||||
"If you used a skill and hit issues not covered by it, patch it immediately.\n\n"
|
||||
"After difficult/iterative tasks, offer to save as a skill. "
|
||||
"Skip for simple one-offs. Confirm with user before creating/deleting.\n\n"
|
||||
"Good skills: trigger conditions, numbered steps with exact commands, "
|
||||
"pitfalls section, verification steps. Use skill_view() to see format examples."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["create", "patch", "edit", "delete", "write_file", "remove_file", "validate"],
|
||||
"enum": ["create", "patch", "edit", "delete", "write_file", "remove_file"],
|
||||
"description": "The action to perform."
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": (
|
||||
"Skill name (lowercase, hyphens/underscores, max 64 chars). "
|
||||
"Required for create/patch/edit/delete/write_file/remove_file. "
|
||||
"Optional for validate: omit to check all skills, provide to check one."
|
||||
"Must match an existing skill for patch/edit/delete/write_file/remove_file."
|
||||
)
|
||||
},
|
||||
"content": {
|
||||
@@ -916,7 +727,7 @@ SKILL_MANAGE_SCHEMA = {
|
||||
|
||||
|
||||
# --- Registry ---
|
||||
from tools.registry import registry, tool_error
|
||||
from tools.registry import registry
|
||||
|
||||
registry.register(
|
||||
name="skill_manage",
|
||||
|
||||
Reference in New Issue
Block a user