skill_preprocessing.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. """Shared SKILL.md preprocessing helpers: ``${HERMES_*}`` template tokens and
  2. inline ``!`cmd``` shell expansion."""
  3. import json
  4. import logging
  5. import re
  6. import subprocess
  7. from pathlib import Path
  8. from agent.compression_marker import elide
  9. from hermes_cli._subprocess_compat import IS_WINDOWS, windows_hide_flags
  10. logger = logging.getLogger(__name__)
  11. # ${HERMES_SKILL_DIR} / ${HERMES_SESSION_ID} tokens. Unresolvable ones (e.g. no
  12. # session) are left as-is so the author can spot them.
  13. _SKILL_TEMPLATE_RE = re.compile(r"\$\{(HERMES_SKILL_DIR|HERMES_SESSION_ID)\}")
  14. # Inline shell snippets like !`date +%Y-%m-%d` — single-line only.
  15. _INLINE_SHELL_RE = re.compile(r"!`([^`\n]+)`")
  16. # Cap inline-shell output so a runaway command can't blow out the context.
  17. _INLINE_SHELL_MAX_OUTPUT = 4000
  18. def load_skills_config() -> dict:
  19. """Load the ``skills`` section of config.yaml (best-effort)."""
  20. try:
  21. from hermes_cli.config import load_config_readonly
  22. skills_cfg = (load_config_readonly() or {}).get("skills")
  23. if isinstance(skills_cfg, dict):
  24. return skills_cfg
  25. except Exception:
  26. logger.debug("Could not read skills config", exc_info=True)
  27. return {}
  28. def substitute_template_vars(content: str, skill_dir: Path | None, session_id: str | None) -> str:
  29. """Replace ${HERMES_SKILL_DIR} / ${HERMES_SESSION_ID}; tokens without a value stay in place."""
  30. if not content:
  31. return content
  32. values = {
  33. "HERMES_SKILL_DIR": str(skill_dir) if skill_dir else None,
  34. "HERMES_SESSION_ID": str(session_id) if session_id else None,
  35. }
  36. return _SKILL_TEMPLATE_RE.sub(lambda m: values[m.group(1)] or m.group(0), content)
  37. def run_inline_shell(command: str, cwd: Path | None, timeout: int) -> str:
  38. """Run one inline-shell snippet and return its stdout (trimmed; stderr when
  39. stdout is empty). Failures return an ``[inline-shell ...]`` marker instead
  40. of raising, so one bad snippet can't wreck the whole skill message."""
  41. _popen_kwargs = {"creationflags": windows_hide_flags()} if IS_WINDOWS else {}
  42. from agent.delegation_context import delegated_child_subprocess_env
  43. try:
  44. bash = "bash"
  45. if IS_WINDOWS:
  46. # CreateProcess searches System32 before PATH and may pick WSL's
  47. # launcher. Reuse the terminal's native Git Bash resolution.
  48. from tools.environments.local import _find_bash
  49. bash = _find_bash()
  50. completed = subprocess.run(
  51. [bash, "-c", command],
  52. cwd=str(cwd) if cwd else None,
  53. capture_output=True,
  54. text=True, encoding='utf-8', errors='replace',
  55. timeout=max(1, int(timeout)),
  56. check=False,
  57. stdin=subprocess.DEVNULL,
  58. env=delegated_child_subprocess_env(),
  59. **_popen_kwargs,
  60. )
  61. except subprocess.TimeoutExpired:
  62. return f"[inline-shell timeout after {timeout}s: {command}]"
  63. except FileNotFoundError:
  64. return "[inline-shell error: bash not found]"
  65. except Exception as exc:
  66. # tests/conftest.py's live-system guard may block the os.kill that
  67. # subprocess.run uses to clean up a timed-out shell; report the timeout.
  68. if isinstance(exc, RuntimeError) and "live-system guard: blocked os.kill" in str(exc):
  69. return f"[inline-shell timeout after {timeout}s: {command}]"
  70. return f"[inline-shell error: {exc}]"
  71. output = (completed.stdout or "").rstrip("\n") or (completed.stderr or "").rstrip("\n")
  72. if completed.returncode != 0 and not output:
  73. # rc!=0 with no output at all is indistinguishable from a legit empty result; it is the
  74. # "interpreter never ran the command" signature (WSL stub without a distro) — say so.
  75. return f"[inline-shell exit {completed.returncode} with no output: {command}]"
  76. return elide(output, _INLINE_SHELL_MAX_OUTPUT)
  77. def expand_inline_shell(content: str, skill_dir: Path | None, timeout: int) -> str:
  78. """Replace every !`cmd` snippet with its stdout, run with the skill dir as CWD."""
  79. if "!`" not in content:
  80. return content
  81. def _replace(match: re.Match) -> str:
  82. cmd = match.group(1).strip()
  83. return run_inline_shell(cmd, skill_dir, timeout) if cmd else ""
  84. return _INLINE_SHELL_RE.sub(_replace, content)
  85. def _is_community_hub_skill(skill_dir: Path | None) -> bool:
  86. """Whether *skill_dir* is a hub-installed skill the scan gate classifies as community trust.
  87. The hub's INSTALL_POLICY blocks a community install on a caution/dangerous verdict — but
  88. ``--force`` (or a pre-scanner install) puts that skill on disk anyway, and the inline-shell
  89. DSL scans as high severity, so auto-executing it on view would re-arm exactly what the
  90. gate refused (#63307). Provenance is the hub lock entry (trusted/builtin entries expand;
  91. anything without one — bundled-synced, user-created, project/external — keeps the flag's
  92. contract). It covers every SKILL.md nested inside a community install and is read from
  93. the lock that owns the skills tree the dir lives in, not only the active profile's
  94. (``external_dirs`` can expose another profile's installs). A lock that exists but cannot
  95. be read fails closed for skills in its tree; no lock means no hub installs.
  96. """
  97. if skill_dir is None:
  98. return False
  99. try:
  100. from tools.skills_hub import HubLockFile
  101. from tools.skills_tool import _skills_dir
  102. target = skill_dir.resolve()
  103. locks = {HubLockFile().path.resolve(): _skills_dir().resolve()}
  104. for root in target.parents:
  105. if root.name == "skills" and (root / ".hub" / "lock.json").is_file():
  106. locks.setdefault((root / ".hub" / "lock.json").resolve(), root)
  107. for lock_path, root in locks.items():
  108. if not (target.is_relative_to(root) and lock_path.is_file()):
  109. continue
  110. try:
  111. installed = json.loads(lock_path.read_text(encoding="utf-8-sig"))["installed"]
  112. entries = [e for e in installed.values() if isinstance(e, dict)]
  113. except (OSError, ValueError, KeyError, TypeError, AttributeError):
  114. logger.debug("Unreadable hub lock %s; inline shell stays off for its tree", lock_path, exc_info=True)
  115. return True
  116. for entry in entries:
  117. rel = str(entry.get("install_path") or "")
  118. if entry.get("trust_level") == "community" and rel and target.is_relative_to((root / rel).resolve()):
  119. return True
  120. return False
  121. except Exception:
  122. logger.debug("Could not resolve hub provenance for inline-shell trust scoping", exc_info=True)
  123. return True
  124. def preprocess_skill_content(
  125. content: str,
  126. skill_dir: Path | None,
  127. session_id: str | None = None,
  128. skills_cfg: dict | None = None,
  129. ) -> str:
  130. """Apply configured SKILL.md template and inline-shell preprocessing."""
  131. if not content:
  132. return content
  133. cfg = skills_cfg if isinstance(skills_cfg, dict) else load_skills_config()
  134. if cfg.get("template_vars", True):
  135. content = substitute_template_vars(content, skill_dir, session_id)
  136. if cfg.get("inline_shell", False) and not _is_community_hub_skill(skill_dir):
  137. content = expand_inline_shell(content, skill_dir, int(cfg.get("inline_shell_timeout", 10) or 10))
  138. return content