forked from karpathy/autoresearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresearchswarm_agent.py
More file actions
346 lines (303 loc) · 11.8 KB
/
Copy pathresearchswarm_agent.py
File metadata and controls
346 lines (303 loc) · 11.8 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""ResearchSwarm cognitive control layer.
This module leaves the existing training stack untouched and adds a separate
decision surface for Digital Cognitive Labor. The key capability is routing a
task into either:
1. text-based work the agent can execute directly in software, or
2. human-action work that requires a person, a physical actuator, or explicit
real-world intervention.
"""
from __future__ import annotations
from dataclasses import asdict, dataclass, field
from enum import Enum
import json
import re
from typing import Iterable
class TaskDomain(str, Enum):
TEXT_BASED = "text-based"
HUMAN_ACTION = "human-action"
HYBRID = "hybrid"
UNKNOWN = "unknown"
class CognitiveFunction(str, Enum):
PERCEIVE = "perceive"
UNDERSTAND = "understand"
PLAN = "plan"
EXECUTE = "execute"
VERIFY = "verify"
ESCALATE = "escalate"
TEXT_SIGNALS = {
"analyze",
"baseline",
"classify",
"code",
"compare",
"compute",
"debug",
"design",
"document",
"draft",
"evaluate",
"explain",
"extract",
"generate",
"implement",
"optimize",
"plan",
"prepare",
"run",
"review",
"refactor",
"research",
"route",
"summarize",
"train",
"training",
"translate",
"write",
}
HUMAN_ACTION_SIGNALS = {
"assemble",
"attend",
"bring",
"call",
"carry",
"clean",
"click",
"deliver",
"drive",
"email",
"fax",
"file",
"go",
"install",
"interview",
"lift",
"meet",
"move",
"negotiate",
"operate",
"phone",
"photograph",
"pick",
"press",
"purchase",
"repair",
"scan",
"ship",
"sign",
"speak",
"travel",
"visit",
"walk",
}
HUMAN_ONLY_PATTERNS = (
r"\bin person\b",
r"\bphysically\b",
r"\bon[- ]site\b",
r"\breal world\b",
r"\bwet signature\b",
r"\bmanual approval\b",
)
@dataclass
class TaskProfile:
instruction: str
domain: TaskDomain
confidence: float
text_signals: list[str] = field(default_factory=list)
human_action_signals: list[str] = field(default_factory=list)
digital_segments: list[str] = field(default_factory=list)
human_segments: list[str] = field(default_factory=list)
ambiguous_segments: list[str] = field(default_factory=list)
open_questions: list[str] = field(default_factory=list)
cognitive_functions: list[CognitiveFunction] = field(default_factory=list)
recommended_action: str = ""
execution_steps: list[str] = field(default_factory=list)
def to_dict(self) -> dict:
payload = asdict(self)
payload["domain"] = self.domain.value
payload["cognitive_functions"] = [item.value for item in self.cognitive_functions]
return payload
def _tokenize(text: str) -> list[str]:
return re.findall(r"[a-zA-Z][a-zA-Z0-9_-]*", text.lower())
def _ordered_unique(items: Iterable[str]) -> list[str]:
seen: set[str] = set()
output: list[str] = []
for item in items:
if item not in seen:
seen.add(item)
output.append(item)
return output
class DigitalCognitiveLaborAgent:
"""Routes tasks based on whether software can complete them autonomously."""
def _segment_instruction(self, instruction: str) -> list[str]:
parts = re.split(r"\b(?:and then|then|after that|afterwards|before|and)\b|[;,]", instruction, flags=re.IGNORECASE)
segments = [part.strip(" .") for part in parts if part.strip(" .")]
return segments or [instruction.strip()]
def classify_task(self, instruction: str) -> TaskProfile:
tokens = _tokenize(instruction)
text_hits = [token for token in tokens if token in TEXT_SIGNALS]
human_hits = [token for token in tokens if token in HUMAN_ACTION_SIGNALS]
human_pattern_hits = [pattern for pattern in HUMAN_ONLY_PATTERNS if re.search(pattern, instruction, re.IGNORECASE)]
segments = self._segment_instruction(instruction)
digital_segments, human_segments, ambiguous_segments = self._classify_segments(segments)
text_score = len(text_hits)
human_score = len(human_hits) + len(human_pattern_hits) * 2
if digital_segments and human_segments:
domain = TaskDomain.HYBRID
elif text_score and human_score:
domain = TaskDomain.HYBRID
elif human_score:
domain = TaskDomain.HUMAN_ACTION
elif text_score:
domain = TaskDomain.TEXT_BASED
else:
domain = TaskDomain.UNKNOWN
total_score = text_score + human_score
confidence = 0.5 if total_score == 0 else round(max(text_score, human_score) / total_score, 2)
cognitive_functions = self._infer_cognitive_functions(domain)
execution_steps = self._build_execution_steps(domain, instruction, digital_segments, human_segments, ambiguous_segments)
recommended_action = self._recommend_action(domain)
open_questions = self._build_open_questions(domain, ambiguous_segments)
return TaskProfile(
instruction=instruction,
domain=domain,
confidence=confidence,
text_signals=_ordered_unique(text_hits),
human_action_signals=_ordered_unique(human_hits + human_pattern_hits),
digital_segments=digital_segments,
human_segments=human_segments,
ambiguous_segments=ambiguous_segments,
open_questions=open_questions,
cognitive_functions=cognitive_functions,
recommended_action=recommended_action,
execution_steps=execution_steps,
)
def _classify_segments(self, segments: list[str]) -> tuple[list[str], list[str], list[str]]:
digital_segments: list[str] = []
human_segments: list[str] = []
ambiguous_segments: list[str] = []
for segment in segments:
segment_profile = self._score_segment(segment)
if segment_profile["text_score"] and segment_profile["human_score"]:
ambiguous_segments.append(segment)
elif segment_profile["human_score"]:
human_segments.append(segment)
elif segment_profile["text_score"]:
digital_segments.append(segment)
else:
ambiguous_segments.append(segment)
return digital_segments, human_segments, ambiguous_segments
def _score_segment(self, segment: str) -> dict[str, int]:
tokens = _tokenize(segment)
text_score = sum(1 for token in tokens if token in TEXT_SIGNALS)
human_score = sum(1 for token in tokens if token in HUMAN_ACTION_SIGNALS)
human_score += sum(2 for pattern in HUMAN_ONLY_PATTERNS if re.search(pattern, segment, re.IGNORECASE))
return {
"text_score": text_score,
"human_score": human_score,
}
def _infer_cognitive_functions(self, domain: TaskDomain) -> list[CognitiveFunction]:
if domain is TaskDomain.TEXT_BASED:
return [
CognitiveFunction.PERCEIVE,
CognitiveFunction.UNDERSTAND,
CognitiveFunction.PLAN,
CognitiveFunction.EXECUTE,
CognitiveFunction.VERIFY,
]
if domain is TaskDomain.HUMAN_ACTION:
return [
CognitiveFunction.PERCEIVE,
CognitiveFunction.UNDERSTAND,
CognitiveFunction.PLAN,
CognitiveFunction.ESCALATE,
CognitiveFunction.VERIFY,
]
if domain is TaskDomain.HYBRID:
return [
CognitiveFunction.PERCEIVE,
CognitiveFunction.UNDERSTAND,
CognitiveFunction.PLAN,
CognitiveFunction.EXECUTE,
CognitiveFunction.ESCALATE,
CognitiveFunction.VERIFY,
]
return [
CognitiveFunction.PERCEIVE,
CognitiveFunction.UNDERSTAND,
CognitiveFunction.PLAN,
]
def _recommend_action(self, domain: TaskDomain) -> str:
if domain is TaskDomain.TEXT_BASED:
return "Execute autonomously inside digital systems and return artifacts plus verification."
if domain is TaskDomain.HUMAN_ACTION:
return "Do not pretend to complete the task; produce a human handoff checklist and wait for real-world completion."
if domain is TaskDomain.HYBRID:
return "Complete the digital portion now, isolate the physical/manual portion, and create an explicit handoff boundary."
return "Clarify the task before execution because the domain is not yet distinguishable."
def _build_execution_steps(
self,
domain: TaskDomain,
instruction: str,
digital_segments: list[str],
human_segments: list[str],
ambiguous_segments: list[str],
) -> list[str]:
if domain is TaskDomain.TEXT_BASED:
steps = [
"Parse the request into inputs, constraints, and outputs.",
"Execute the required digital work product.",
"Verify the result against the stated objective.",
"Return the artifact, evidence, and any residual risks.",
]
if digital_segments:
steps.insert(1, f"Focus on the digital fragments: {'; '.join(digital_segments)}.")
return steps
if domain is TaskDomain.HUMAN_ACTION:
return [
"Identify the exact real-world action that cannot be completed by software.",
"Generate a checklist, prerequisites, and safety constraints for the human operator.",
"Mark the task as pending human execution rather than falsely reporting completion.",
"Request confirmation or evidence after the human step is done.",
]
if domain is TaskDomain.HYBRID:
steps = [
"Separate the task into digital sub-work and human-action sub-work.",
"Execute the digital sub-work immediately and prepare the handoff boundary.",
"Create a minimal handoff package for the human-action sub-work.",
"Resume verification once the human-action evidence is supplied.",
]
if digital_segments:
steps.insert(1, f"Digital sub-work: {'; '.join(digital_segments)}.")
if human_segments:
steps.insert(3, f"Human sub-work: {'; '.join(human_segments)}.")
if ambiguous_segments:
steps.append(f"Resolve the ambiguous fragments before final completion: {'; '.join(ambiguous_segments)}.")
return steps
return [
"Restate the task.",
"Ask a clarifying question focused on the execution medium.",
f"Clarify whether this request is meant to stay inside software systems: {instruction}",
]
def _build_open_questions(self, domain: TaskDomain, ambiguous_segments: list[str]) -> list[str]:
if domain is TaskDomain.UNKNOWN:
return [
"Should this task stay entirely inside software, or does it require a real-world step?",
]
if domain is TaskDomain.HYBRID and ambiguous_segments:
return [
f"Which of these fragments should remain with the human operator: {'; '.join(ambiguous_segments)}?",
]
return []
def process(self, instruction: str) -> dict:
profile = self.classify_task(instruction)
return profile.to_dict()
def main() -> None:
import argparse
parser = argparse.ArgumentParser(description="Classify Digital Cognitive Labor tasks for ResearchSwarm.")
parser.add_argument("instruction", help="Natural-language task to classify")
args = parser.parse_args()
agent = DigitalCognitiveLaborAgent()
report = agent.process(args.instruction)
print(json.dumps(report, indent=2))
if __name__ == "__main__":
main()