Compare commits

..

2 Commits

Author SHA1 Message Date
909e4e7ece feat: poka-yoke auto-revert incomplete skill edits on failure (#295)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 47s
Add tests/test_skill_manager_pokayoke.py
2026-04-14 03:03:22 +00:00
1ec0bdde87 feat: poka-yoke auto-revert incomplete skill edits on failure (#295)
Update tools/skill_manager_tool.py
2026-04-14 03:03:20 +00:00
4 changed files with 553 additions and 301 deletions

View File

@@ -41,42 +41,6 @@ from agent.model_metadata import is_local_endpoint
logger = logging.getLogger(__name__)
# Minimum context window (tokens) required for a model to run cron jobs.
# Models below this threshold are rejected at job startup.
CRON_MIN_CONTEXT_TOKENS = 64_000
class ModelContextError(ValueError):
"""Raised when a model's context window is too small for cron use."""
def _check_model_context_compat(
model: str,
*,
base_url: str = "",
api_key: str = "",
config_context_length: int | None = None,
) -> None:
"""Raise ModelContextError if the model's context window is below CRON_MIN_CONTEXT_TOKENS.
If config_context_length is provided the check is skipped (user override).
Detection failures are non-fatal (fail-open) — the job proceeds.
"""
if config_context_length is not None:
return
try:
from agent.model_metadata import get_model_context_length
ctx = get_model_context_length(model, base_url=base_url, api_key=api_key)
except Exception as exc:
logger.debug("Context length detection failed for '%s', skipping check: %s", model, exc)
return
if ctx < CRON_MIN_CONTEXT_TOKENS:
raise ModelContextError(
f"Model '{model}' has a context window of {ctx:,} tokens, "
f"which is below the minimum {CRON_MIN_CONTEXT_TOKENS:,} required by Hermes Agent. "
f"To override, set model.context_length in config.yaml."
)
# =====================================================================
# Deploy Sync Guard
@@ -126,14 +90,7 @@ def _validate_agent_interface() -> None:
) from exc
sig = inspect.signature(AIAgent.__init__)
params = sig.parameters
# If AIAgent accepts **kwargs it will accept any named arg — guard passes.
if any(p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values()):
_agent_interface_validated = True
logger.debug("Deploy sync guard passed — AIAgent accepts **kwargs")
return
accepted = set(params.keys()) - {"self"}
accepted = set(sig.parameters.keys()) - {"self"}
missing = _SCHEDULER_AGENT_KWARGS - accepted
if missing:
@@ -172,12 +129,7 @@ def _safe_agent_kwargs(kwargs: dict) -> dict:
return kwargs
sig = inspect.signature(AIAgent.__init__)
params = sig.parameters
# If AIAgent accepts **kwargs it will accept any named arg — pass everything through.
if any(p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values()):
return kwargs
accepted = set(params.keys()) - {"self"}
accepted = set(sig.parameters.keys()) - {"self"}
safe = {}
dropped = []
@@ -593,49 +545,7 @@ def _run_job_script(script_path: str) -> tuple[bool, str]:
return False, f"Script execution failed: {exc}"
_PROVIDER_ALIASES = {
"ollama": {"ollama", "localhost:11434"},
"anthropic": {"anthropic", "claude"},
"nous": {"nous", "mimo"},
"openrouter": {"openrouter"},
"openai": {"openai", "gpt"},
"gemini": {"gemini", "google"},
}
_CLOUD_PREFIXES = frozenset({"nous", "openrouter", "anthropic", "openai", "zai", "kimi", "gemini", "minimax"})
def _classify_runtime(provider: str, model: str) -> str:
"""Return 'cloud', 'local', or 'unknown' based on provider/model hints."""
p = (provider or "").strip().lower()
m = (model or "").strip().lower()
if p and p not in ("ollama", "local"):
return "cloud"
if "/" in m and m.split("/")[0] in _CLOUD_PREFIXES:
return "cloud"
if p in ("ollama", "local") or (not p and m):
return "local"
return "unknown"
def _detect_provider_mismatch(prompt: str, active_provider: str):
"""Return the mismatched provider alias if the prompt references a different provider."""
if not active_provider or not prompt:
return None
pl = prompt.lower()
al = active_provider.lower().strip()
active_group = next(
(g for g, aliases in _PROVIDER_ALIASES.items() if al in aliases or al.startswith(g)),
None,
)
if not active_group:
return None
return next(
(g for g, aliases in _PROVIDER_ALIASES.items() if g != active_group and any(x in pl for x in aliases)),
None,
)
def _build_job_prompt(job: dict, *, runtime_model: str = "", runtime_provider: str = "") -> str:
def _build_job_prompt(job: dict) -> str:
"""Build the effective prompt for a cron job, optionally loading one or more skills first."""
prompt = job.get("prompt", "")
skills = job.get("skills")
@@ -666,26 +576,6 @@ def _build_job_prompt(job: dict, *, runtime_model: str = "", runtime_provider: s
f"{prompt}"
)
# Build runtime context block — inject model/provider/runtime classification
# so the agent knows what infrastructure it has access to.
# Fix #565: derive provider from model prefix when runtime_provider is empty.
_runtime_block = ""
if runtime_model or runtime_provider:
if not runtime_provider and "/" in runtime_model:
runtime_provider = runtime_model.split("/")[0]
_kind = _classify_runtime(runtime_provider, runtime_model)
_parts = []
if runtime_model:
_parts.append(f"MODEL: {runtime_model}")
if runtime_provider:
_parts.append(f"PROVIDER: {runtime_provider}")
if _kind == "local":
_parts.append("RUNTIME: local — access to machine, Ollama, SSH")
elif _kind == "cloud":
_parts.append("RUNTIME: cloud — NO local access, NO SSH, NO localhost")
if _parts:
_runtime_block = "[SYSTEM: RUNTIME CONTEXT — " + "; ".join(_parts) + "]\n\n"
# Always prepend cron execution guidance so the agent knows how
# delivery works and can suppress delivery when appropriate.
cron_hint = (
@@ -707,7 +597,7 @@ def _build_job_prompt(job: dict, *, runtime_model: str = "", runtime_provider: s
"\"[SCRIPT_FAILED]: forge.alexanderwhitestone.com timed out\" "
"\"[SCRIPT_FAILED]: script exited with code 1\".]\\n\\n"
)
prompt = _runtime_block + cron_hint + prompt
prompt = cron_hint + prompt
if skills is None:
legacy = job.get("skill")
skills = [legacy] if legacy else []
@@ -777,23 +667,7 @@ def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
job_id = job["id"]
job_name = job["name"]
# Resolve runtime model/provider early so the prompt gets accurate context.
_runtime_model = job.get("model") or os.getenv("HERMES_MODEL") or ""
_runtime_provider = os.getenv("HERMES_PROVIDER", "")
if not _runtime_model:
try:
import yaml as _y
_cp2 = str(_hermes_home / "config.yaml")
if os.path.exists(_cp2):
with open(_cp2) as _f:
_ce = _y.safe_load(_f) or {}
_mc = _ce.get("model", {})
_runtime_model = _mc if isinstance(_mc, str) else (_mc.get("default", "") if isinstance(_mc, dict) else "")
except Exception:
pass
prompt = _build_job_prompt(job, runtime_model=_runtime_model, runtime_provider=_runtime_provider)
prompt = _build_job_prompt(job)
origin = _resolve_origin(job)
_cron_session_id = f"cron_{job_id}_{_hermes_now().strftime('%Y%m%d_%H%M%S')}"
@@ -905,14 +779,6 @@ def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
message = format_runtime_provider_error(exc)
raise RuntimeError(message) from exc
_active_provider = runtime.get("provider", "") or ""
_mismatch = _detect_provider_mismatch(job.get("prompt", ""), _active_provider)
if _mismatch:
logger.warning(
"Job '%s': prompt references '%s' but active provider is '%s'",
job_name, _mismatch, _active_provider,
)
from agent.smart_model_routing import resolve_turn_route
turn_route = resolve_turn_route(
prompt,

View File

@@ -7,7 +7,7 @@ from unittest.mock import AsyncMock, patch, MagicMock
import pytest
from cron.scheduler import _resolve_origin, _resolve_delivery_target, _deliver_result, run_job, SILENT_MARKER, _build_job_prompt, _check_model_context_compat, ModelContextError, CRON_MIN_CONTEXT_TOKENS, _classify_runtime, _detect_provider_mismatch
from cron.scheduler import _resolve_origin, _resolve_delivery_target, _deliver_result, run_job, SILENT_MARKER, _build_job_prompt, _check_model_context_compat, ModelContextError, CRON_MIN_CONTEXT_TOKENS
class TestResolveOrigin:
@@ -670,13 +670,6 @@ class TestRunJobSkillBacked:
class TestSilentDelivery:
"""Verify that [SILENT] responses suppress delivery while still saving output."""
@pytest.fixture(autouse=True)
def _isolate_lock(self, tmp_path):
"""Give each test its own tick lock file to prevent parallel test contention."""
with patch("cron.scheduler._LOCK_FILE", tmp_path / ".tick.lock"), \
patch("cron.scheduler._LOCK_DIR", tmp_path):
yield
def _make_job(self):
return {
"id": "monitor-job",
@@ -834,102 +827,10 @@ class TestBuildJobPromptMissingSkill:
assert "go" in result
class TestClassifyRuntime:
"""Unit tests for _classify_runtime."""
def test_cloud_provider_explicit(self):
assert _classify_runtime("openai", "") == "cloud"
assert _classify_runtime("anthropic", "") == "cloud"
assert _classify_runtime("nous", "") == "cloud"
def test_local_provider_explicit(self):
assert _classify_runtime("ollama", "") == "local"
assert _classify_runtime("local", "") == "local"
def test_cloud_detected_from_model_prefix(self):
"""Model prefix 'nous/...' should be classified as cloud even with no provider."""
assert _classify_runtime("", "nous/mimo-v2-pro") == "cloud"
assert _classify_runtime("", "openai/gpt-4o") == "cloud"
def test_local_when_model_has_no_cloud_prefix(self):
"""A model without a cloud prefix and no provider => local."""
assert _classify_runtime("", "llama3") == "local"
def test_unknown_when_empty(self):
assert _classify_runtime("", "") == "unknown"
class TestBuildJobPromptRuntimeContext:
"""Verify runtime context block injection in _build_job_prompt."""
def test_runtime_block_injected_with_model_and_provider(self):
job = {"prompt": "Do something"}
result = _build_job_prompt(job, runtime_model="nous/mimo-v2-pro", runtime_provider="nous")
assert "RUNTIME CONTEXT" in result
assert "MODEL: nous/mimo-v2-pro" in result
assert "PROVIDER: nous" in result
assert "cloud" in result
def test_provider_derived_from_model_prefix_when_empty(self):
"""Fix #565: PROVIDER should be derived from model prefix when runtime_provider is empty."""
job = {"prompt": "Do something"}
result = _build_job_prompt(job, runtime_model="nous/mimo-v2-pro", runtime_provider="")
assert "PROVIDER: nous" in result
def test_provider_not_empty_in_context_block(self):
"""Fix #565: PROVIDER line must not be blank when model has a slash prefix."""
job = {"prompt": "Check status"}
result = _build_job_prompt(job, runtime_model="openai/gpt-4o", runtime_provider="")
assert "PROVIDER: openai" in result
assert "PROVIDER: ;" not in result
assert "PROVIDER: ]" not in result
def test_no_runtime_block_when_no_model_or_provider(self):
"""No runtime block should appear when neither model nor provider is given."""
job = {"prompt": "Hello"}
result = _build_job_prompt(job)
assert "RUNTIME CONTEXT" not in result
def test_local_runtime_classification(self):
"""ollama model should get local runtime label."""
job = {"prompt": "Query local model"}
result = _build_job_prompt(job, runtime_model="llama3", runtime_provider="ollama")
assert "RUNTIME: local" in result
assert "NO local access" not in result
def test_runtime_block_precedes_cron_hint(self):
"""RUNTIME CONTEXT block should appear before the cron system hint."""
job = {"prompt": "test"}
result = _build_job_prompt(job, runtime_model="nous/mimo-v2-pro", runtime_provider="nous")
runtime_pos = result.index("RUNTIME CONTEXT")
cron_pos = result.index("scheduled cron job")
assert runtime_pos < cron_pos
class TestDetectProviderMismatch:
"""Unit tests for _detect_provider_mismatch."""
def test_no_mismatch_when_same_provider(self):
assert _detect_provider_mismatch("Use ollama to generate", "ollama") is None
def test_mismatch_detected(self):
"""Prompt referencing 'ollama' while running on 'nous' should flag a mismatch."""
result = _detect_provider_mismatch("Check if Ollama is responding", "nous")
assert result == "ollama"
def test_no_mismatch_for_empty_inputs(self):
assert _detect_provider_mismatch("", "nous") is None
assert _detect_provider_mismatch("some prompt", "") is None
def test_no_mismatch_when_provider_unknown(self):
"""Unknown active provider should not raise, just return None."""
assert _detect_provider_mismatch("Check Ollama", "mystery-provider") is None
class TestTickAdvanceBeforeRun:
"""Verify that tick() calls advance_next_run before run_job for crash safety."""
def test_advance_called_before_run_job(self, tmp_path, monkeypatch):
def test_advance_called_before_run_job(self, tmp_path):
"""advance_next_run must be called before run_job to prevent crash-loop re-fires."""
call_order = []
@@ -954,9 +855,7 @@ class TestTickAdvanceBeforeRun:
patch("cron.scheduler.run_job", side_effect=fake_run_job), \
patch("cron.scheduler.save_job_output", return_value=tmp_path / "out.md"), \
patch("cron.scheduler.mark_job_run"), \
patch("cron.scheduler._deliver_result"), \
patch("cron.scheduler._LOCK_FILE", tmp_path / ".tick.lock"), \
patch("cron.scheduler._LOCK_DIR", tmp_path):
patch("cron.scheduler._deliver_result"):
from cron.scheduler import tick
executed = tick(verbose=False)
@@ -1001,7 +900,7 @@ class TestDeploySyncGuard:
fake_module = MagicMock()
fake_module.AIAgent = FakeAIAgent
with pytest.raises(RuntimeError, match=r"(?s)missing params:.*tool_choice"):
with pytest.raises(RuntimeError, match="Missing parameters: tool_choice"):
with patch.dict("sys.modules", {"run_agent": fake_module}):
sched_mod._validate_agent_interface()
finally:

View File

@@ -0,0 +1,298 @@
"""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

View File

@@ -40,10 +40,55 @@ import shutil
import tempfile
from pathlib import Path
from hermes_constants import get_hermes_home
from typing import Dict, Any, Optional
from typing import Dict, Any, Optional, Tuple
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:
@@ -92,11 +137,6 @@ 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
# =============================================================================
@@ -224,13 +264,15 @@ 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 ".." in normalized.parts:
if has_traversal_component(file_path):
return "Path traversal ('..') is not allowed."
# Must be under an allowed subdirectory
@@ -245,6 +287,17 @@ 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.
@@ -339,31 +392,45 @@ 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)."""
"""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.
"""
err = _validate_frontmatter(content)
if err:
return {"success": False, "error": err}
return {"success": False, "error": f"Edit failed: {err} Original file preserved."}
err = _validate_content_size(content)
if err:
return {"success": False, "error": err}
return {"success": False, "error": f"Edit failed: {err} Original file preserved."}
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"
# Back up original content for rollback
# Snapshot original for rollback
original_content = skill_md.read_text(encoding="utf-8") if skill_md.exists() else None
_atomic_write_text(skill_md, content)
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.",
}
# 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)
return {"success": False, "error": scan_error}
_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."}
_log_skill_modification("edit", name, "SKILL.md", original_content, content, True)
return {
"success": True,
"message": f"Skill '{name}' updated.",
@@ -380,6 +447,9 @@ 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.
"""
@@ -399,7 +469,9 @@ def _patch_skill(
err = _validate_file_path(file_path)
if err:
return {"success": False, "error": err}
target = skill_dir / file_path
target, err = _resolve_skill_target(skill_dir, file_path)
if err:
return {"success": False, "error": err}
else:
# Patching SKILL.md
target = skill_dir / "SKILL.md"
@@ -415,7 +487,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, match_error = fuzzy_find_and_replace(
new_content, match_count, _strategy, match_error = fuzzy_find_and_replace(
content, old_string, new_string, replace_all
)
if match_error:
@@ -423,7 +495,7 @@ def _patch_skill(
preview = content[:500] + ("..." if len(content) > 500 else "")
return {
"success": False,
"error": match_error,
"error": f"Patch failed: {match_error} Original file preserved.",
"file_preview": preview,
}
@@ -431,7 +503,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": err}
return {"success": False, "error": f"Patch failed: {err} Original file preserved."}
# If patching SKILL.md, validate frontmatter is still intact
if not file_path:
@@ -439,18 +511,27 @@ def _patch_skill(
if err:
return {
"success": False,
"error": f"Patch would break SKILL.md structure: {err}",
"error": f"Patch failed: would break SKILL.md structure: {err} Original file preserved.",
}
original_content = content # for rollback
_atomic_write_text(target, new_content)
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.",
}
# Security scan — roll back on block
scan_error = _security_scan_skill(skill_dir)
if scan_error:
_atomic_write_text(target, original_content)
return {"success": False, "error": scan_error}
_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."}
_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 ''}).",
@@ -478,7 +559,10 @@ 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."""
"""Add or overwrite a supporting file within any skill directory.
Poka-yoke: reverts to original on failure.
"""
err = _validate_file_path(file_path)
if err:
return {"success": False, "error": err}
@@ -499,17 +583,27 @@ 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": err}
return {"success": False, "error": f"Write failed: {err} Original file preserved."}
existing = _find_skill(name)
if not existing:
return {"success": False, "error": f"Skill '{name}' not found. Create it first with action='create'."}
target = existing["path"] / file_path
target, err = _resolve_skill_target(existing["path"], file_path)
if err:
return {"success": False, "error": err}
target.parent.mkdir(parents=True, exist_ok=True)
# Back up for rollback
# Snapshot for rollback
original_content = target.read_text(encoding="utf-8") if target.exists() else None
_atomic_write_text(target, file_content)
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.",
}
# Security scan — roll back on block
scan_error = _security_scan_skill(existing["path"])
@@ -518,8 +612,10 @@ 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)
return {"success": False, "error": scan_error}
_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."}
_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}'.",
@@ -538,7 +634,9 @@ 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 = skill_dir / file_path
target, err = _resolve_skill_target(skill_dir, file_path)
if err:
return {"success": False, "error": err}
if not target.exists():
# List what's actually there for the model to see
available = []
@@ -554,6 +652,8 @@ 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
@@ -561,12 +661,96 @@ 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
# =============================================================================
@@ -589,19 +773,19 @@ def skill_manage(
"""
if action == "create":
if not content:
return json.dumps({"success": False, "error": "content is required for 'create'. Provide the full SKILL.md text (frontmatter + body)."}, ensure_ascii=False)
return tool_error("content is required for 'create'. Provide the full SKILL.md text (frontmatter + body).", success=False)
result = _create_skill(name, content, category)
elif action == "edit":
if not content:
return json.dumps({"success": False, "error": "content is required for 'edit'. Provide the full updated SKILL.md text."}, ensure_ascii=False)
return tool_error("content is required for 'edit'. Provide the full updated SKILL.md text.", success=False)
result = _edit_skill(name, content)
elif action == "patch":
if not old_string:
return json.dumps({"success": False, "error": "old_string is required for 'patch'. Provide the text to find."}, ensure_ascii=False)
return tool_error("old_string is required for 'patch'. Provide the text to find.", success=False)
if new_string is None:
return json.dumps({"success": False, "error": "new_string is required for 'patch'. Use empty string to delete matched text."}, ensure_ascii=False)
return tool_error("new_string is required for 'patch'. Use empty string to delete matched text.", success=False)
result = _patch_skill(name, old_string, new_string, file_path, replace_all)
elif action == "delete":
@@ -609,18 +793,21 @@ def skill_manage(
elif action == "write_file":
if not file_path:
return json.dumps({"success": False, "error": "file_path is required for 'write_file'. Example: 'references/api-guide.md'"}, ensure_ascii=False)
return tool_error("file_path is required for 'write_file'. Example: 'references/api-guide.md'", success=False)
if file_content is None:
return json.dumps({"success": False, "error": "file_content is required for 'write_file'."}, ensure_ascii=False)
return tool_error("file_content is required for 'write_file'.", success=False)
result = _write_file(name, file_path, file_content)
elif action == "remove_file":
if not file_path:
return json.dumps({"success": False, "error": "file_path is required for 'remove_file'."}, ensure_ascii=False)
return tool_error("file_path is required for 'remove_file'.", success=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"}
result = {"success": False, "error": f"Unknown action '{action}'. Use: create, edit, patch, delete, write_file, remove_file, validate"}
if result.get("success"):
try:
@@ -638,38 +825,40 @@ def skill_manage(
SKILL_MANAGE_SCHEMA = {
"name": "skill_manage",
"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."
),
"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."
),
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["create", "patch", "edit", "delete", "write_file", "remove_file"],
"enum": ["create", "patch", "edit", "delete", "write_file", "remove_file", "validate"],
"description": "The action to perform."
},
"name": {
"type": "string",
"description": (
"Skill name (lowercase, hyphens/underscores, max 64 chars). "
"Must match an existing skill for patch/edit/delete/write_file/remove_file."
"Required for create/patch/edit/delete/write_file/remove_file. "
"Optional for validate: omit to check all skills, provide to check one."
)
},
"content": {
@@ -727,7 +916,7 @@ SKILL_MANAGE_SCHEMA = {
# --- Registry ---
from tools.registry import registry
from tools.registry import registry, tool_error
registry.register(
name="skill_manage",