verify_cmd.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """``hermes verify`` — detect a project's run recipe and smoke-test it."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. import sys
  6. from pathlib import Path
  7. def run_verify_command(args) -> int:
  8. from agent.verify import load_or_detect, manifest_path, run_verify, save_manifest
  9. root = Path(getattr(args, "path", None) or ".").resolve()
  10. if not root.is_dir():
  11. print(f"error: not a directory: {root}", file=sys.stderr)
  12. return 2
  13. recipe, source = load_or_detect(root)
  14. if recipe is None:
  15. if args.json:
  16. print(json.dumps({"ok": False, "error": "no-recipe", "root": str(root)}))
  17. else:
  18. print(
  19. f"No recognizable project found at {root}.\n"
  20. f"Create {manifest_path(root)} to define a recipe manually.",
  21. file=sys.stderr,
  22. )
  23. return 1
  24. if source == "detected":
  25. _merge_project_facts_commands(root, recipe)
  26. if args.port:
  27. recipe.port = args.port
  28. if args.save:
  29. path = save_manifest(root, recipe)
  30. if not args.json:
  31. print(f"Saved manifest: {path}")
  32. if args.detect_only:
  33. print(json.dumps({"source": source, "recipe": recipe.to_dict()}, indent=None if args.json else 2))
  34. return 0
  35. phases = tuple(args.phase) if args.phase else None
  36. result = run_verify(
  37. root, recipe, phases=phases, phase_timeout=args.timeout, ready_timeout=args.ready_timeout,
  38. skip_start=args.skip_start, port_override=args.port,
  39. )
  40. _record_evidence(root, recipe, result, partial=bool(phases or args.skip_start))
  41. if args.json:
  42. payload = result.to_dict()
  43. payload["source"] = source
  44. print(json.dumps(payload))
  45. return 0 if result.ok else 1
  46. _print_human_report(recipe, source, result)
  47. return 0 if result.ok else 1
  48. def _merge_project_facts_commands(root: Path, recipe) -> None:
  49. """Fold ``detect_project_facts`` verify commands into a detected recipe (best-effort union).
  50. Never applied to a saved manifest — the user-edited manifest is the source of truth.
  51. """
  52. try:
  53. from agent.coding_context import detect_project_facts
  54. facts_commands = list(detect_project_facts(root).verify_commands)
  55. except Exception:
  56. return
  57. existing = {c.strip() for c in (*recipe.bootstrap, *recipe.build, *recipe.test) if c}
  58. for command in facts_commands:
  59. command = command.strip()
  60. if command and command not in existing:
  61. recipe.test.append(command)
  62. existing.add(command)
  63. def _record_evidence(root: Path, recipe, result, *, partial: bool) -> None:
  64. """Record the completed run into the verification evidence ledger.
  65. Fail-silent: a ledger problem must never change the CLI's exit code or output. ``partial``
  66. (a ``--phase`` subset or ``--skip-start``) downgrades the scope to ``targeted`` so a partial
  67. pass is never presented as a full workspace green.
  68. """
  69. try:
  70. from agent.verification_evidence import record_verify_run
  71. tails = [f"[{p.phase}] {p.command}\n{p.output_tail}" for p in result.phases if p.output_tail]
  72. if result.readiness is not None:
  73. tails.append(f"[start] {recipe.start} -> {_readiness_status(result.readiness)}")
  74. record_verify_run(
  75. root=root,
  76. session_id=os.environ.get("HERMES_SESSION_ID"),
  77. ok=result.ok,
  78. command="hermes verify",
  79. scope="targeted" if partial else "full",
  80. output="\n".join(tails),
  81. )
  82. except Exception:
  83. pass
  84. def _readiness_status(r) -> str:
  85. return f"ready (HTTP {r.status_code})" if r.ready else f"not ready ({r.error or 'timeout'})"
  86. def _print_human_report(recipe, source, result) -> None:
  87. print(f"Recipe: {recipe.name} ({recipe.kind}) — source: {source}")
  88. print()
  89. if result.phases:
  90. width = max(len(p.phase) for p in result.phases)
  91. for p in result.phases:
  92. status = "PASS" if p.ok else ("TIMEOUT" if p.timed_out else "FAIL")
  93. print(f" {p.phase.ljust(width)} {status:<7} {p.duration:6.1f}s {p.command}")
  94. else:
  95. print(" (no phases executed)")
  96. if result.readiness is not None:
  97. r = result.readiness
  98. print(f" {'start'.ljust(max(5, max((len(p.phase) for p in result.phases), default=5)))} "
  99. f"{'PASS' if r.ready else 'FAIL':<7} {r.duration:6.1f}s {recipe.start}")
  100. print()
  101. print(f"Readiness: {r.url} -> {_readiness_status(r)}")
  102. failed = [p for p in result.phases if not p.ok]
  103. print()
  104. print(f"Result: {'OK' if result.ok else 'FAILED'}")
  105. for p in failed:
  106. print(f"\n--- output tail: {p.phase} ({p.command}) ---")
  107. print(p.output_tail.rstrip())
  108. if result.readiness is not None and not result.readiness.ready and result.readiness.output_tail:
  109. print("\n--- output tail: start ---")
  110. print(result.readiness.output_tail.rstrip())