verify_hooks.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Verification-loop helpers for the ``pre_verify`` round-end gate.
  2. After code edits the loop fires ``pre_verify`` (directives resolved by
  3. :func:`hermes_cli.plugins.get_pre_verify_continue_message`). The shipped coding
  4. guidance rides on the evidence-based verification-stop nudge rather than a second
  5. default stop gate, so default token cost stays tied to the "missing verification
  6. evidence" decision while ``pre_verify`` remains free for user/plugin policy.
  7. """
  8. from __future__ import annotations
  9. from typing import Any, Optional
  10. from utils import is_truthy_value
  11. DEFAULT_MAX_VERIFY_NUDGES = 3
  12. # Appended to the verification-stop nudge when code lacks fresh evidence. Mirrors
  13. # the user-facing "clean your work" workflow without adding its own model turn.
  14. CODING_VERIFY_GUIDANCE = (
  15. "[Coding] Before you run tests/linters or call this done: if this is "
  16. "creative UI/visual work, hold off on tests and linters until the user says "
  17. "they like the result or you're about to commit. And before every commit, "
  18. "clean your work: keep it KISS/DRY, match the surrounding code style, and be "
  19. "elitist, shorthand, clever, concise, efficient, and elegant."
  20. )
  21. def max_verify_nudges(config: Optional[dict[str, Any]] = None) -> int:
  22. """Bound on consecutive ``pre_verify`` continue directives per turn (>= 0)."""
  23. try:
  24. return max(0, int(_agent_cfg(config).get("max_verify_nudges")))
  25. except (TypeError, ValueError):
  26. return DEFAULT_MAX_VERIFY_NUDGES
  27. def coding_verify_guidance(config: Optional[dict[str, Any]] = None) -> Optional[str]:
  28. """Return the optional guidance appended to verification-stop nudges."""
  29. if not is_truthy_value(_agent_cfg(config).get("verify_guidance", True), default=True):
  30. return None
  31. return CODING_VERIFY_GUIDANCE
  32. def _agent_cfg(config: Optional[dict[str, Any]]) -> dict[str, Any]:
  33. if config is None:
  34. try:
  35. from hermes_cli.config import load_config
  36. config = load_config()
  37. except Exception:
  38. config = {}
  39. agent_cfg = config.get("agent") if isinstance(config, dict) else None
  40. return agent_cfg if isinstance(agent_cfg, dict) else {}
  41. __all__ = ["CODING_VERIFY_GUIDANCE", "DEFAULT_MAX_VERIFY_NUDGES", "coding_verify_guidance", "max_verify_nudges"]