-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.py
More file actions
176 lines (144 loc) · 7.1 KB
/
verify.py
File metadata and controls
176 lines (144 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3.12
"""Verification layer - ground truth checking for ULTRON actions."""
import logging
import re
import subprocess
from dataclasses import dataclass
from pathlib import Path
import requests
log = logging.getLogger(__name__)
AOMS_URL = "http://localhost:9100"
@dataclass
class VerificationResult:
success: bool
method: str
evidence: str
confidence: float # 0-1
class Verifier:
def verify_file_exists(self, path: str, min_size: int = 10) -> VerificationResult:
p = Path(path).expanduser().resolve()
if not p.exists():
return VerificationResult(False, "file_exists", f"File not found: {path}", 1.0)
size = p.stat().st_size
if size < min_size:
return VerificationResult(False, "file_size", f"File too small: {size} bytes", 1.0)
return VerificationResult(True, "file_exists", f"File exists: {size} bytes", 1.0)
def verify_file_contains(self, path: str, expected: list[str]) -> VerificationResult:
p = Path(path).expanduser().resolve()
if not p.exists():
return VerificationResult(False, "file_contains", "File not found", 1.0)
content = p.read_text()
missing = [s for s in expected if s not in content]
if missing:
return VerificationResult(False, "file_contains", f"Missing: {missing}", 1.0)
return VerificationResult(True, "file_contains", "All expected content found", 1.0)
def verify_command_succeeds(self, cmd: str, timeout: int = 30) -> VerificationResult:
try:
result = subprocess.run(cmd, shell=True, capture_output=True, timeout=timeout, text=True)
if result.returncode == 0:
return VerificationResult(True, "command", f"Exit 0: {result.stdout[:200]}", 1.0)
return VerificationResult(False, "command", f"Exit {result.returncode}: {result.stderr[:200]}", 1.0)
except subprocess.TimeoutExpired:
return VerificationResult(False, "command", "Timeout", 1.0)
except Exception as e:
return VerificationResult(False, "command", str(e), 1.0)
def verify_url_accessible(self, url: str, expected_status: int = 200) -> VerificationResult:
try:
resp = requests.head(url, timeout=10, allow_redirects=True)
if resp.status_code == expected_status:
return VerificationResult(True, "url", f"Status {resp.status_code}", 1.0)
return VerificationResult(False, "url", f"Status {resp.status_code}, expected {expected_status}", 1.0)
except Exception as e:
return VerificationResult(False, "url", str(e), 1.0)
def verify_tweet_exists(self, tweet_id: str) -> VerificationResult:
try:
result = subprocess.run(
["xurl", "get", tweet_id],
capture_output=True, text=True, timeout=15,
)
if result.returncode == 0 and "text" in result.stdout:
return VerificationResult(True, "tweet", "Tweet found", 1.0)
return VerificationResult(False, "tweet", "Tweet not found or xurl failed", 0.8)
except Exception as e:
return VerificationResult(False, "tweet", str(e), 0.8)
def verify_github_repo(self, repo: str) -> VerificationResult:
try:
resp = requests.get(f"https://api.github.com/repos/{repo}", timeout=10)
if resp.status_code == 200:
return VerificationResult(True, "github", f"Repo exists: {repo}", 1.0)
return VerificationResult(False, "github", f"Status {resp.status_code}", 1.0)
except Exception as e:
return VerificationResult(False, "github", str(e), 1.0)
def verify_aoms_stored(self, query: str) -> VerificationResult:
try:
resp = requests.post(f"{AOMS_URL}/memory/search",
json={"query": query, "limit": 1}, timeout=5)
data = resp.json()
results = data if isinstance(data, list) else data.get("results", [])
if results:
return VerificationResult(True, "aoms", "Found in AOMS", 1.0)
return VerificationResult(False, "aoms", "Not found in AOMS", 0.8)
except Exception as e:
return VerificationResult(False, "aoms", str(e), 1.0)
# --- Extraction helpers ---
def extract_file_paths(text: str) -> list[str]:
"""Extract file paths from Ollama output."""
patterns = [
r'(?:outputs/[\w./-]+)',
r'(?:results/[\w./-]+)',
r'(?:~/curiosity/[\w./-]+)',
]
paths = []
for p in patterns:
paths.extend(re.findall(p, text))
return paths
def extract_tweet_id(text: str) -> str | None:
match = re.search(r'(?:status/|tweet_id["\s:=]+)(\d{10,})', text)
return match.group(1) if match else None
def extract_github_repo(text: str) -> str | None:
match = re.search(r'(?:github\.com/|repos/)([a-zA-Z0-9_-]+/[a-zA-Z0-9_.-]+)', text)
return match.group(1) if match else None
# --- Smart verification routing ---
def verify_task_completion(task: dict, result: str, verifier: Verifier | None = None) -> VerificationResult:
"""Choose verification method based on task type."""
if verifier is None:
verifier = Verifier()
task_name = task["name"].lower()
# File-related tasks
if any(w in task_name for w in ["write", "create file", "save", "readme"]):
paths = extract_file_paths(result)
if paths:
return verifier.verify_file_exists(paths[0])
# Check if result file was saved by goal_engine
from pathlib import Path as P
result_file = P(f"~/curiosity/results").expanduser() / task.get("_goal_id", "unknown") / f"{task['id']}.md"
return verifier.verify_file_exists(str(result_file), min_size=200)
# Tweet-related tasks
if any(w in task_name for w in ["tweet", "post to twitter", "x post"]):
tid = extract_tweet_id(result)
if tid:
return verifier.verify_tweet_exists(tid)
return VerificationResult(False, "tweet", "No tweet ID found in result", 0.5)
# GitHub tasks
if any(w in task_name for w in ["github", "repo", "repository"]):
repo = extract_github_repo(result)
if repo:
return verifier.verify_github_repo(repo)
return VerificationResult(False, "github", "No repo found in result", 0.5)
# Research tasks - verify stored in AOMS
if any(w in task_name for w in ["research", "analyze", "investigate"]):
return verifier.verify_aoms_stored(task["name"][:50])
# Default: content quality check
if len(result) > 200:
return VerificationResult(True, "content_length", f"Result: {len(result)} chars", 0.7)
return VerificationResult(False, "content_length", f"Result too short: {len(result)} chars", 0.5)
def should_ask_human(verification: VerificationResult, task: dict) -> bool:
"""Decide if human input is needed."""
if verification.confidence < 0.7:
return True
high_risk = ["delete", "publish", "send", "money", "payment", "deploy"]
if any(w in task["name"].lower() for w in high_risk):
return True
if task.get("retry_count", 0) >= 2:
return True
return False