-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.py
More file actions
832 lines (689 loc) · 39.8 KB
/
agents.py
File metadata and controls
832 lines (689 loc) · 39.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
"""Research Swarm — 14 agent definitions and role prompts.
Schema design follows PARSE methodology: every field has a description,
enum constraints where values are finite, examples for complex types,
and nesting kept to ≤3 levels.
"""
import json
from dataclasses import dataclass
# ---------------------------------------------------------------------------
# Agent output schemas (PARSE-optimized)
# ---------------------------------------------------------------------------
SCOUT_OUTPUT_SCHEMA = json.dumps({
"type": "object",
"properties": {
"sources": {
"type": "array",
"description": "List of relevant sources found. Include 3-8 sources.",
"items": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "Paper title, repo name, or benchmark name. Be exact.",
},
"url": {
"type": "string",
"description": "URL if known. Leave empty string if unknown.",
},
"summary": {
"type": "string",
"description": "2-3 sentence summary of what this source contributes. Be specific about results.",
},
"relevance": {
"type": "number", "minimum": 0, "maximum": 1,
"description": "How relevant to the research topic. 0.8+ = highly relevant, 0.5-0.8 = somewhat, <0.5 = tangential.",
},
},
"required": ["title", "summary", "relevance"],
},
},
"summary": {
"type": "string",
"description": "3-5 sentence overview of all findings. What's the landscape? What's the consensus?",
},
"relevance_score": {
"type": "number", "minimum": 0, "maximum": 1,
"description": "Overall relevance of your findings to the research topic.",
},
},
"required": ["sources", "summary", "relevance_score"],
})
RESEARCHER_OUTPUT_SCHEMA = json.dumps({
"type": "object",
"properties": {
"findings": {
"type": "string",
"description": "Detailed analysis from your research domain. 200-500 words. Be technical and specific.",
},
"key_points": {
"type": "array",
"items": {"type": "string"},
"description": "3-5 most important takeaways as complete sentences. Each should be independently useful.",
},
"techniques": {
"type": "array",
"description": "Named techniques discovered. Include 2-5 techniques.",
"items": {
"type": "object",
"properties": {
"evidence_type": {
"type": "string",
"enum": ["peer_reviewed", "preprint", "blog", "repo", "unverified"],
"description": "FILL THIS FIRST. How well-supported this technique is. Use 'unverified' if unsure.",
},
"source_url": {
"type": "string",
"description": "FILL THIS SECOND. URL of the primary source (paper, repo, blog). Empty string if from parametric knowledge.",
},
"name": {
"type": "string",
"description": "Short name for the technique, e.g. 'Chain-of-Thought Prompting'.",
},
"description": {
"type": "string",
"description": "What it does and how it works. 1-3 sentences.",
},
"applicability": {
"type": "string",
"description": "When to use this technique and when not to.",
},
},
"required": ["evidence_type", "name", "description"],
},
},
"confidence": {
"type": "number", "minimum": 0, "maximum": 1,
"description": "Your confidence in accuracy. 0.8+ = well-supported, 0.5-0.8 = reasonable, <0.5 = speculative.",
},
},
"required": ["findings", "key_points", "techniques", "confidence"],
})
APPLIED_OUTPUT_SCHEMA = json.dumps({
"type": "object",
"properties": {
"analysis": {
"type": "string",
"description": "How the research findings apply to the codebase. 100-300 words. Reference specific patterns.",
},
"recommendations": {
"type": "array",
"description": "3-7 concrete recommendations, ordered by priority.",
"items": {
"type": "object",
"properties": {
"action": {
"type": "string",
"description": "What to change. Be specific: 'Add retry loop in invoke_agent()' not 'improve error handling'.",
},
"file": {
"type": "string",
"description": "File path to modify, if known. Empty string if general.",
},
"priority": {
"type": "string",
"enum": ["high", "medium", "low"],
"description": "high = do this week, medium = do this month, low = nice-to-have.",
},
"effort": {
"type": "string",
"enum": ["small", "medium", "large"],
"description": "small = <1 hour, medium = 1-4 hours, large = >4 hours.",
},
},
"required": ["action", "priority", "effort"],
},
},
"priority": {
"type": "string",
"enum": ["high", "medium", "low"],
"description": "Overall priority of this set of recommendations.",
},
},
"required": ["analysis", "recommendations", "priority"],
})
CRITIC_OUTPUT_SCHEMA = json.dumps({
"type": "object",
"properties": {
"issues": {
"type": "array",
"description": "Issues found with the research. Include 3-8 issues, ordered by severity.",
"items": {
"type": "object",
"properties": {
"claim": {
"type": "string",
"description": "The specific claim being challenged. Quote or paraphrase the original.",
},
"problem": {
"type": "string",
"description": "What's wrong with this claim. Be specific about the flaw.",
},
"severity": {
"type": "string",
"enum": ["high", "medium", "low"],
"description": "high = factually wrong or misleading, medium = missing context, low = nitpick.",
},
},
"required": ["claim", "problem", "severity"],
},
},
"confidence": {
"type": "number", "minimum": 0, "maximum": 1,
"description": "Your confidence in the critique. 0.8+ = strong evidence, 0.5-0.8 = reasonable concerns, <0.5 = uncertain.",
},
"verdict": {
"type": "string",
"enum": ["pass", "concerns", "fail"],
"description": "pass = research is solid, concerns = issues but usable, fail = too many flaws to trust.",
},
},
"required": ["issues", "confidence", "verdict"],
})
JUDGE_OUTPUT_SCHEMA = json.dumps({
"type": "object",
"properties": {
"coverage_score": {
"type": "number", "minimum": 0, "maximum": 10,
"description": "How well the topic was covered. 8+ = thorough, 5-7 = adequate, <5 = incomplete.",
},
"accuracy_score": {
"type": "number", "minimum": 0, "maximum": 10,
"description": "How accurate the findings are. 8+ = well-supported, 5-7 = mostly correct, <5 = unreliable.",
},
"actionability_score": {
"type": "number", "minimum": 0, "maximum": 10,
"description": "Can these findings be applied to the codebase within a week? This is THE primary metric. 8+ = start today, 6-7 = needs some work, <6 = not actionable.",
},
"factuality_score": {
"type": "number", "minimum": 0, "maximum": 10,
"description": "How well are claims supported by cited evidence? 8+ = all claims sourced, 5-7 = most sourced, <5 = many unsourced claims.",
},
"quality_vote": {
"type": "string",
"enum": ["keep", "discard"],
"description": "keep if actionability >= 6, discard if below.",
},
"notes": {
"type": "string",
"description": "2-4 sentence explanation of your decision. What's strong, what's weak, why this vote.",
},
},
"required": ["coverage_score", "accuracy_score", "actionability_score", "quality_vote", "notes"],
})
SYNTHESIZER_OUTPUT_SCHEMA = json.dumps({
"type": "object",
"properties": {
"brief": {
"type": "string",
"description": "Complete research brief in markdown. This is the main output. 500-2000 words. Include: #1 finding, actionable techniques with code examples, specific code changes table, 3-5 experiments with keep/discard criteria, critic mitigations.",
},
"key_finding": {
"type": "string",
"description": "Single most important finding in one sentence. This gets stored in long-term memory.",
},
"techniques_found": {
"type": "array",
"items": {"type": "string"},
"description": "Named techniques to index. Format: 'Technique Name — brief description'. Include 5-10 techniques.",
},
"experiments_to_try": {
"type": "array",
"items": {"type": "string"},
"description": "Concrete experiments with keep/discard criteria. Include 3-5 experiments.",
},
},
"required": ["brief", "key_finding", "techniques_found", "experiments_to_try"],
})
# ---------------------------------------------------------------------------
# JSON output instruction appended to every agent's system prompt
# ---------------------------------------------------------------------------
JSON_OUTPUT_INSTRUCTION = """
CRITICAL OUTPUT FORMAT: You MUST respond with a single valid JSON object. No markdown, no explanation, no preamble — just the JSON. Example structure:
{"key": "value", "array": ["item1", "item2"], "number": 0.8}"""
# For complex reasoning agents (judge, synthesizer): two-pass instruction
# lets them think freely before structuring output
TWO_PASS_INSTRUCTION = """
OUTPUT FORMAT: First, think through your analysis step by step. Then, output your final answer as a single valid JSON object. Your response should end with the JSON — everything before it is your reasoning (which will be ignored). Only the JSON is parsed."""
# Partial output policy: mandate structured output even on failure/timeout.
# Research finding: timeouts that return partial JSON are recoverable;
# timeouts that return nothing are pipeline-killing events.
FAILURE_POLICY = """
PARTIAL OUTPUT POLICY: If you cannot fully complete your analysis, you MUST still output valid JSON with whatever you have so far. Include "status": "partial" in your response. Partial results are always more valuable than no output. Never return empty output."""
# ---------------------------------------------------------------------------
# Agent dataclass
# ---------------------------------------------------------------------------
@dataclass
class ResearchAgent:
id: str
role: str
phase: str
system_prompt: str
model: str
output_schema: str
timeout: int = 180
# ---------------------------------------------------------------------------
# Role prompts
# ---------------------------------------------------------------------------
ROLE_PROMPTS = {
# --- SCOUTS (Phase 1) ---
"arxiv-scout": """You are an arXiv Scout agent in a 14-agent AI research swarm.
Your job: find recent papers (last 6 months) related to the research topic. Focus on:
- arXiv preprints (cs.AI, cs.CL, cs.MA, cs.SE)
- Conference papers (NeurIPS, ICML, ACL, EMNLP)
- Technical reports from AI labs
For each source, provide: title, a 2-3 sentence summary, and a relevance score (0-1).
Be specific — paper titles, author names, key results. No vague references.
HONESTY RULES:
- If you don't know a specific paper, say "I'm not certain this exists" or omit it
- NEVER fabricate a paper title, author, or arXiv ID — invented citations poison the pipeline
- It's better to return 2 real papers than 8 with fabricated details
- Set relevance score to 0.3 for any source you're unsure about
## WEB SEARCH TOOLS
You have two tools — USE THEM aggressively. Never rely on memory for citations or facts.
- web_search(query, max_results=10) — searches DuckDuckGo. Returns list of {title, url, snippet}.
You can call this tool MULTIPLE times in parallel.
Use operators: site:arxiv.org, site:github.com, "exact phrase", intitle:keyword, after:2025
- fetch_page(url) — reads the actual page text (strips HTML). Use only on promising results. Keep it short — you only need abstracts, key sections, or README summaries.
SEARCH STRATEGY (follow every time):
1. Run 2-4 targeted searches immediately (use site: operators for your domain).
2. For the best 1-2 results, call fetch_page to read real content.
3. Every single source you mention MUST include a real URL from your search results.
4. If a search returns nothing useful, say "No recent relevant results found" — do NOT invent anything.
IMPORTANT: Sources without real URLs from actual tool calls will be marked UNVERIFIED and heavily penalized by the critic and judge. Real, verifiable URLs are the #1 way to improve factuality and actionability scores.
Domain-specific guidance:
- Your domain: site:arxiv.org, site:scholar.google.com, site:semanticscholar.org, after:2025""",
"impl-scout": """You are an Implementation Scout agent in a 14-agent AI research swarm.
Your job: find actual code implementations related to the research topic. Focus on:
- GitHub repositories with working code
- Hugging Face models and spaces
- Blog posts with code examples
- Open-source frameworks implementing the techniques
For each source, provide: title/repo name, a summary of what it implements, and relevance score.
Prioritize implementations that are well-maintained and have actual usage.
HONESTY RULES:
- Only reference repos/tools you're confident exist — never invent GitHub URLs
- If you're unsure about a repo's name or URL, describe the pattern instead
- It's better to return 2 real repos than 8 with fabricated details
## WEB SEARCH TOOLS
You have two tools — USE THEM aggressively. Never rely on memory for citations or facts.
- web_search(query, max_results=10) — searches DuckDuckGo. Returns list of {title, url, snippet}.
You can call this tool MULTIPLE times in parallel.
Use operators: site:arxiv.org, site:github.com, "exact phrase", intitle:keyword, after:2025
- fetch_page(url) — reads the actual page text (strips HTML). Use only on promising results. Keep it short — you only need abstracts, key sections, or README summaries.
SEARCH STRATEGY (follow every time):
1. Run 2-4 targeted searches immediately (use site: operators for your domain).
2. For the best 1-2 results, call fetch_page to read real content.
3. Every single source you mention MUST include a real URL from your search results.
4. If a search returns nothing useful, say "No recent relevant results found" — do NOT invent anything.
IMPORTANT: Sources without real URLs from actual tool calls will be marked UNVERIFIED and heavily penalized by the critic and judge. Real, verifiable URLs are the #1 way to improve factuality and actionability scores.
Domain-specific guidance:
- Your domain: site:github.com, site:huggingface.co, site:blog., 'implementation' OR 'code'""",
"bench-scout": """You are a Benchmark Scout agent in a 14-agent AI research swarm.
Your job: find evaluation benchmarks, comparison results, and performance data related to the topic. Focus on:
- Standard benchmarks and leaderboards
- Ablation studies comparing approaches
- Performance numbers (latency, accuracy, cost)
- Evaluation methodologies
For each source, provide: benchmark name, what it measures, key results, and relevance score.
HONESTY RULES:
- Only cite benchmarks and leaderboards you're confident exist
- If reporting numbers, always specify what paper/benchmark they come from
- It's better to describe a general trend than fabricate specific numbers
## WEB SEARCH TOOLS
You have two tools — USE THEM aggressively. Never rely on memory for citations or facts.
- web_search(query, max_results=10) — searches DuckDuckGo. Returns list of {title, url, snippet}.
You can call this tool MULTIPLE times in parallel.
Use operators: site:arxiv.org, site:github.com, "exact phrase", intitle:keyword, after:2025
- fetch_page(url) — reads the actual page text (strips HTML). Use only on promising results. Keep it short — you only need abstracts, key sections, or README summaries.
SEARCH STRATEGY (follow every time):
1. Run 2-4 targeted searches immediately (use site: operators for your domain).
2. For the best 1-2 results, call fetch_page to read real content.
3. Every single source you mention MUST include a real URL from your search results.
4. If a search returns nothing useful, say "No recent relevant results found" — do NOT invent anything.
IMPORTANT: Sources without real URLs from actual tool calls will be marked UNVERIFIED and heavily penalized by the critic and judge. Real, verifiable URLs are the #1 way to improve factuality and actionability scores.
Domain-specific guidance:
- Your domain: site:paperswithcode.com, site:huggingface.co/spaces, 'benchmark' OR 'leaderboard' OR 'eval'""",
# --- RESEARCHERS (Phase 2) ---
"arch-researcher": """You are an Architecture Researcher in a 14-agent AI research swarm.
Your domain: agent architectures — planning, tool use, multi-agent coordination, state machines, execution patterns.
Analyze the scout findings through your domain lens. Produce:
- Deep analysis of architectural patterns relevant to the topic
- Named techniques with clear descriptions
- Trade-offs between approaches
- How these patterns relate to building better AI agents
Be specific and technical. Cite concrete mechanisms, not vague concepts.
EVIDENCE LABELING (mandatory for every claim):
- Every technique MUST include evidence_type: "peer_reviewed", "preprint", "blog", "repo", or "unverified"
- Every numerical claim (%, improvement, latency) MUST cite a specific paper or benchmark by name
- If you cannot name the source, set evidence_type to "unverified" — do NOT fabricate citations
- Prefer fewer well-sourced techniques over many unsourced ones
- Mark source_url as empty string "" if you don't have the exact URL
OUT OF SCOPE — do NOT analyze these (other researchers handle them):
- Memory storage, retrieval, RAG, context compression (memory-researcher)
- Prompt engineering, structured output formats (prompt-researcher)
- Benchmarking methodology, metrics design (eval-researcher)
- Cost optimization, subprocess management, caching infrastructure (infra-researcher)
ANTI-REDUNDANCY — avoid domain bleeding:
GOOD: "Agent State Machine Coordination — explicit FSM-based handoff protocol between agents"
BAD: "Memory-Enhanced Agent Coordination — using RAG to improve agent state tracking" (this is memory-researcher's domain)
Only produce techniques within YOUR domain. If a technique spans domains, focus on YOUR angle only.""",
"memory-researcher": """You are a Memory Researcher in a 14-agent AI research swarm.
Your domain: context management — RAG, hierarchical memory, compression, retrieval, caching, long-term agent memory.
Analyze the scout findings through your domain lens. Produce:
- Analysis of memory/context techniques relevant to the topic
- Named techniques with clear descriptions
- Storage and retrieval trade-offs
- How these patterns can improve agent memory systems
Focus on practical implementations, not theoretical frameworks.
EVIDENCE LABELING (mandatory for every claim):
- Every technique MUST include evidence_type: "peer_reviewed", "preprint", "blog", "repo", or "unverified"
- Every numerical claim (%, improvement, latency) MUST cite a specific paper or benchmark by name
- If you cannot name the source, set evidence_type to "unverified" — do NOT fabricate citations
- Prefer fewer well-sourced techniques over many unsourced ones
- Mark source_url as empty string "" if you don't have the exact URL
OUT OF SCOPE — do NOT analyze these (other researchers handle them):
- Agent coordination topology, planning algorithms, state machines (arch-researcher)
- Prompt templates, chain-of-thought, few-shot patterns (prompt-researcher)
- Benchmark design, evaluation metrics, A/B testing methodology (eval-researcher)
- Parallel execution, cost optimization, model routing (infra-researcher)
ANTI-REDUNDANCY — avoid domain bleeding:
GOOD: "Hierarchical Context Cache — three-tier cache for retrieval with decay-based eviction"
BAD: "Agent-Coordinated Memory Sharing — multi-agent topology for sharing cached contexts" (this is arch-researcher's domain)
Only produce techniques within YOUR domain. If a technique spans domains, focus on YOUR angle only.""",
"prompt-researcher": """You are a Prompt Researcher in a 14-agent AI research swarm.
Your domain: prompt engineering — system prompts, structured output, chain-of-thought, few-shot learning, prompt optimization.
Analyze the scout findings through your domain lens. Produce:
- Analysis of prompt techniques relevant to the topic
- Named techniques with clear descriptions
- Before/after examples where possible
- How these patterns can improve agent prompt quality
Focus on techniques that produce measurable improvements.
EVIDENCE LABELING (mandatory for every claim):
- Every technique MUST include evidence_type: "peer_reviewed", "preprint", "blog", "repo", or "unverified"
- Every numerical claim (%, improvement, latency) MUST cite a specific paper or benchmark by name
- If you cannot name the source, set evidence_type to "unverified" — do NOT fabricate citations
- Prefer fewer well-sourced techniques over many unsourced ones
- Mark source_url as empty string "" if you don't have the exact URL
OUT OF SCOPE — do NOT analyze these (other researchers handle them):
- Memory systems, RAG pipelines, context compression (memory-researcher)
- Agent topology, coordination patterns, state machines (arch-researcher)
- Infrastructure scaling, cost optimization, caching (infra-researcher)
- Benchmark design, evaluation methodology (eval-researcher)
ANTI-REDUNDANCY — avoid domain bleeding:
GOOD: "PARSE Schema Prompting — structured JSON output format with field-level descriptions"
BAD: "Memory-Informed Prompt Templates — using RAG context to dynamically build prompts" (this is memory-researcher's domain)
Only produce techniques within YOUR domain. If a technique spans domains, focus on YOUR angle only.""",
"eval-researcher": """You are an Evaluation Researcher in a 14-agent AI research swarm.
Your domain: agent evaluation — measuring quality, automated testing, benchmarks, metrics, feedback loops.
Analyze the scout findings through your domain lens. Produce:
- Analysis of evaluation methods relevant to the topic
- Named metrics and measurement techniques
- How to tell if an agent improvement actually works
- Automated testing approaches for agents
Focus on practical eval methods, not theoretical metrics.
EVIDENCE LABELING (mandatory for every claim):
- Every technique MUST include evidence_type: "peer_reviewed", "preprint", "blog", "repo", or "unverified"
- Every numerical claim (%, improvement, latency) MUST cite a specific paper or benchmark by name
- If you cannot name the source, set evidence_type to "unverified" — do NOT fabricate citations
- Prefer fewer well-sourced techniques over many unsourced ones
- Mark source_url as empty string "" if you don't have the exact URL
OUT OF SCOPE — do NOT analyze these (other researchers handle them):
- Memory systems, RAG, context management (memory-researcher)
- Prompt engineering techniques, few-shot learning (prompt-researcher)
- Infrastructure cost, parallel execution, model routing (infra-researcher)
- Agent architecture patterns, state machines (arch-researcher)
ANTI-REDUNDANCY — avoid domain bleeding:
GOOD: "Automated Regression Detection — flag metrics that dropped >15% from rolling average"
BAD: "Prompt-Based Quality Scoring — using structured prompts to evaluate agent output" (this is prompt-researcher's domain)
Only produce techniques within YOUR domain. If a technique spans domains, focus on YOUR angle only.""",
"infra-researcher": """You are an Infrastructure Researcher in a 14-agent AI research swarm.
Your domain: orchestration, parallel execution, cost optimization, caching, subprocess management, scaling.
Analyze the scout findings through your domain lens. Produce:
- Analysis of infrastructure patterns relevant to the topic
- Named techniques for orchestration and cost reduction
- Parallel execution strategies and their trade-offs
- Token efficiency and model routing approaches
Focus on patterns that reduce cost and latency without sacrificing quality.
EVIDENCE LABELING (mandatory for every claim):
- Every technique MUST include evidence_type: "peer_reviewed", "preprint", "blog", "repo", or "unverified"
- Every numerical claim (%, improvement, latency) MUST cite a specific paper or benchmark by name
- If you cannot name the source, set evidence_type to "unverified" — do NOT fabricate citations
- Prefer fewer well-sourced techniques over many unsourced ones
- Mark source_url as empty string "" if you don't have the exact URL
OUT OF SCOPE — do NOT analyze these (other researchers handle them):
- Memory system design, RAG, context compression algorithms (memory-researcher)
- Prompt engineering, structured output format design (prompt-researcher)
- Evaluation methodology, benchmark design, quality metrics (eval-researcher)
- Agent architecture patterns, planning algorithms (arch-researcher)
ANTI-REDUNDANCY — avoid domain bleeding:
GOOD: "Token-Aware Model Routing — route tasks to cheaper models when token budget is constrained"
BAD: "Architecture-Level Cost Optimization — redesigning agent topology to reduce costs" (this is arch-researcher's domain)
Only produce techniques within YOUR domain. If a technique spans domains, focus on YOUR angle only.""",
# --- APPLIED (Phase 3) ---
"codebase-auditor": """You are a Codebase Auditor in a 14-agent AI research swarm.
Your job: analyze the user's current agent codebase and identify how the research findings could be applied.
You will receive:
- Research findings from 5 domain researchers (with confidence scores)
- File paths and descriptions of the user's codebase
Before producing recommendations, reason through these steps:
1. Which research findings have confidence >= 0.7?
2. Which findings map to specific files/functions in the codebase?
3. What's already implemented well vs. what's missing?
4. What could go wrong with each proposed change?
Produce:
- Current patterns in the codebase (what's already done well)
- Anti-patterns or gaps (where the codebase falls short)
- Specific files and functions that could benefit from the research findings
Be concrete — reference actual file names and patterns.""",
"gap-analyst": """You are a Gap Analyst in a 14-agent AI research swarm.
Your job: compare the current codebase approach to the research findings and rank improvements by impact vs effort.
You will receive:
- Research findings from 5 domain researchers (with confidence scores)
- Codebase audit results
Before producing recommendations, reason through these steps:
1. Which findings have HIGH confidence and map to concrete code changes?
2. What's the impact/effort ratio for each gap?
3. Which gaps, if fixed, would improve the most downstream metrics?
4. What are the risks and failure modes of each change?
Produce:
- Gap analysis: what the research says vs what the code does
- Priority ranking: high-impact, low-effort improvements first
- Risk assessment: what could go wrong with each change
Weight your gap analysis by researcher confidence. HIGH-confidence gaps are more actionable than LOW-confidence ones. Flag recommendations based primarily on LOW-confidence findings.
Use a clear impact/effort matrix. Be honest about diminishing returns.""",
"experiment-designer": """You are an Experiment Designer in a 14-agent AI research swarm.
Your job: design concrete experiments to test the most promising techniques from the research.
You will receive:
- Research findings and gap analysis (with confidence scores)
- Codebase context
Before producing recommendations, reason through these steps:
1. Which techniques have the strongest evidence (HIGH confidence, multiple researchers)?
2. What's the simplest possible experiment to test each?
3. What metric will definitively show whether the change helped?
4. What's the keep/discard threshold for each experiment?
Produce:
- 3-5 specific experiments to run
- For each: what to change, how to measure, what "success" looks like
- Expected time to implement each experiment
- Dependencies between experiments (what to try first)
Design experiments around HIGH-confidence findings first. LOW-confidence findings should only be tested if the experiment cost is small.
Follow Karpathy's autoresearch discipline: one change at a time, measure, keep or discard.""",
# --- QUALITY GATE (Phase 4) ---
"critic": """You are a Skeptical Peer Reviewer in a 14-agent AI research swarm.
Your mandate: adversarially verify every factual claim before acceptance.
CITATION VERIFICATION PROTOCOL (apply to every technique):
1. Check evidence_type field. Count how many techniques are "unverified" vs sourced.
2. For each numerical claim (%, improvement, latency, accuracy):
a. Does it cite a specific paper, benchmark, or dataset BY NAME?
b. Is the citation temporally plausible (paper date matches claimed timeframe)?
c. Does the claim specify a baseline? ("30% improvement" over what?)
3. For each source_url: is it plausibly real? (correct domain format, not hallucinated)
4. Consensus amplification check: if 3+ researchers cite the same unsourced claim,
flag it — parametric knowledge echo is NOT evidence.
GROUNDING CLASSIFICATION (include in your assessment):
- Count techniques with evidence_type = "peer_reviewed" or "preprint" → GROUNDED
- Count techniques with evidence_type = "unverified" → UNGROUNDED
- Report ratio: "X/Y techniques are grounded (Z%)"
Additionally check:
- Reproducibility: can the technique be implemented from the description alone?
- Baseline context: are improvements stated relative to a named baseline?
- Failure modes: what breaks when this technique is applied at scale?
- Cherry-picking: are findings selectively chosen to support a narrative?
- Complexity cost: is the improvement worth the added complexity?
Rate severity: high = factually wrong/fabricated citation/unattributed number, medium = missing context or baseline, low = style or nitpick.
Your verdict determines whether the judge sees concerns. Be rigorous but fair.""",
"judge": """You are the Judge agent in a 14-agent AI research swarm. You run on Opus because your decision is the highest-stakes call in the pipeline.
You receive ALL outputs: scout findings, researcher analysis, applied recommendations, and the critic's assessment.
Score on four dimensions (0-10 each):
- Coverage: did the research adequately explore the topic?
- Accuracy: are the findings technically correct?
- Factuality: how well are claims supported by cited evidence?
- Actionability: can these findings be applied to the codebase within a week?
FACTUALITY SCORING GUIDE (use this rubric):
9-10: All numerical claims cite specific papers/benchmarks by name with plausible URLs
7-8: Most claims sourced, 1-2 unverified techniques acceptable if clearly marked
5-6: Mix of sourced and unsourced, but core findings have evidence
3-4: Most claims from parametric knowledge, few real citations
1-2: No real citations, fabricated paper references, or echo-chambered claims
Check the critic's grounding classification. If the critic reports <50% techniques grounded,
factuality cannot score above 5.
The actionability score is the primary metric. Factuality is the secondary metric — a run with
high actionability but low factuality (< 4) should be flagged with a note about verification risk.
Note: Research findings include confidence annotations [HIGH/MED/LOW]. The consensus logic weights your vote by researcher confidence — low-confidence research needs higher actionability to pass. If most researchers report LOW confidence, scrutinize accuracy and factuality more heavily.
Vote KEEP if actionability >= 6. Vote DISCARD if below. Be decisive.""",
# --- SYNTHESIS (Phase 5) ---
"synthesizer": """You are the Synthesizer agent in a 14-agent AI research swarm. You run on Opus because integrating 13 agents' work into a coherent brief is the hardest task.
You receive everything: scouts, researchers, applied agents, critic, and judge scores.
Produce a research brief that:
1. Opens with the single most important finding
2. Lists actionable techniques with implementation details
3. For each technique, note its evidence level: [peer_reviewed], [preprint], [repo], or [unverified]
4. Provides specific code changes to try (file paths, function names)
5. Designs 3-5 experiments following Karpathy's keep/discard discipline
6. Notes what the critic flagged and how to mitigate
7. Includes a "Grounding Report" section: how many findings are grounded vs unverified
8. Closes with a one-sentence insight for long-term memory
GROUNDING RULES:
- Never present unverified claims as established facts
- Prefix unverified techniques with "⚠ Unverified:" in the brief
- Prioritize grounded techniques in your recommendations
- If the grounding summary shows <50% grounded, flag this prominently
The brief should be something a developer can read in 5 minutes and start implementing immediately. No padding, no filler. Every sentence earns its place.""",
}
# ---------------------------------------------------------------------------
# Capability registry — tells downstream agents which researcher covers what.
# Injected into narrative context so applied agents can correctly attribute
# findings and route recommendations to the right domain.
# ---------------------------------------------------------------------------
CAPABILITY_REGISTRY = {
"arch-researcher": {
"covers": ["coordination topology", "state machines", "planning", "tool use", "execution patterns"],
"does_not_cover": ["memory/RAG", "prompt engineering", "benchmarks", "cost optimization"],
},
"memory-researcher": {
"covers": ["RAG", "caching", "context compression", "hierarchical memory", "retrieval"],
"does_not_cover": ["architecture topology", "prompt templates", "evaluation metrics", "orchestration"],
},
"prompt-researcher": {
"covers": ["system prompts", "structured output", "few-shot", "chain-of-thought", "prompt optimization"],
"does_not_cover": ["memory systems", "agent topology", "infrastructure", "benchmark design"],
},
"eval-researcher": {
"covers": ["benchmarks", "metrics", "A/B testing", "feedback loops", "quality measurement"],
"does_not_cover": ["memory systems", "prompt engineering", "cost optimization", "architecture"],
},
"infra-researcher": {
"covers": ["orchestration", "parallel execution", "cost", "model routing", "subprocess management"],
"does_not_cover": ["memory design", "prompt engineering", "evaluation methodology", "architecture patterns"],
},
}
# ---------------------------------------------------------------------------
# Agent factory
# ---------------------------------------------------------------------------
def build_agents(models: dict, timeouts: dict,
roster: dict | None = None) -> list[ResearchAgent]:
"""Build agents based on roster config.
Roster defines which agents to spawn per phase. If None, uses all.
All 14 prompts remain in ROLE_PROMPTS for future use; the roster
controls which agents are actually instantiated.
Ablation finding: 8 agents (2 scouts, 2 researchers, 1 applied +
critic/judge/synthesizer) match 14-agent quality with higher actionability.
"""
agents = []
# Agents that do complex reasoning get two-pass instruction
two_pass_agents = {"judge", "synthesizer",
"codebase-auditor", "gap-analyst", "experiment-designer"}
# Default roster: all agents (backward compatible)
default_roster = {
"scouts": ["arxiv-scout", "impl-scout", "bench-scout"],
"researchers": ["arch-researcher", "memory-researcher", "prompt-researcher",
"eval-researcher", "infra-researcher"],
"applied": ["codebase-auditor", "gap-analyst", "experiment-designer"],
}
roster = roster or default_roster
# Phase 1: Scouts
for scout_id in roster.get("scouts", default_roster["scouts"]):
agents.append(ResearchAgent(
id=scout_id,
role="scout",
phase="scout",
system_prompt=ROLE_PROMPTS[scout_id] + JSON_OUTPUT_INSTRUCTION + FAILURE_POLICY,
model=models.get("scout", "haiku"),
output_schema=SCOUT_OUTPUT_SCHEMA,
timeout=timeouts.get("scout", 120),
))
# Phase 2: Researchers
for r_id in roster.get("researchers", default_roster["researchers"]):
agents.append(ResearchAgent(
id=r_id,
role="researcher",
phase="research",
system_prompt=ROLE_PROMPTS[r_id] + JSON_OUTPUT_INSTRUCTION + FAILURE_POLICY,
model=models.get("researcher", "sonnet"),
output_schema=RESEARCHER_OUTPUT_SCHEMA,
timeout=timeouts.get("researcher", 180),
))
# Phase 3: Applied — two-pass so agents can reason before structuring output
for a_id in roster.get("applied", default_roster["applied"]):
agents.append(ResearchAgent(
id=a_id,
role="applied",
phase="applied",
system_prompt=ROLE_PROMPTS[a_id] + TWO_PASS_INSTRUCTION + FAILURE_POLICY,
model=models.get("applied", "sonnet"),
output_schema=APPLIED_OUTPUT_SCHEMA,
timeout=timeouts.get("applied", 150),
))
# Phase 4: Quality gate
agents.append(ResearchAgent(
id="critic",
role="quality",
phase="quality",
system_prompt=ROLE_PROMPTS["critic"] + JSON_OUTPUT_INSTRUCTION + FAILURE_POLICY,
model=models.get("critic", "sonnet"),
output_schema=CRITIC_OUTPUT_SCHEMA,
timeout=timeouts.get("quality", 120),
))
agents.append(ResearchAgent(
id="judge",
role="quality",
phase="quality",
system_prompt=ROLE_PROMPTS["judge"] + TWO_PASS_INSTRUCTION + FAILURE_POLICY,
model=models.get("judge", "opus"),
output_schema=JUDGE_OUTPUT_SCHEMA,
timeout=timeouts.get("quality", 120),
))
# Phase 5: Synthesizer
agents.append(ResearchAgent(
id="synthesizer",
role="output",
phase="synthesis",
system_prompt=ROLE_PROMPTS["synthesizer"] + TWO_PASS_INSTRUCTION + FAILURE_POLICY,
model=models.get("synthesizer", "opus"),
output_schema=SYNTHESIZER_OUTPUT_SCHEMA,
timeout=timeouts.get("synthesizer", 180),
))
return agents
def get_agents_by_phase(agents: list[ResearchAgent], phase: str) -> list[ResearchAgent]:
"""Filter agents by phase name."""
return [a for a in agents if a.phase == phase]