fix: pipeline v3 — remove dead imports, add --proven mode as primary, check token budget, honest behavior"
Browse files
alpha_factory/orchestration/pipeline.py
CHANGED
|
@@ -1,33 +1,35 @@
|
|
| 1 |
"""
|
| 2 |
-
Pipeline Orchestrator
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
import asyncio
|
| 5 |
from datetime import datetime
|
| 6 |
from rich.console import Console
|
| 7 |
from rich.panel import Panel
|
| 8 |
|
| 9 |
-
from ..config import Config
|
| 10 |
-
from ..infra import LLMClient, FactorStore
|
| 11 |
-
from ..deterministic import lint, quick_dedup_hash, pick_theme
|
| 12 |
from ..deterministic.theme_sampler import THEME_FIELDS
|
| 13 |
-
from ..personas import
|
| 14 |
-
|
| 15 |
-
compile_expression,
|
| 16 |
-
scout_novelty,
|
| 17 |
-
diagnose_performance,
|
| 18 |
-
gate_alpha,
|
| 19 |
-
)
|
| 20 |
from ..schemas import Verdict
|
| 21 |
|
| 22 |
console = Console()
|
| 23 |
|
| 24 |
|
| 25 |
class AlphaPipeline:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def __init__(self, config: Config):
|
| 27 |
self.config = config
|
| 28 |
self.llm = LLMClient(config.llm)
|
| 29 |
self.store = FactorStore(config.paths.factor_store / "alphas.duckdb")
|
| 30 |
-
self.brain: BrainClient | None = None
|
| 31 |
|
| 32 |
self._consecutive_lint_fails = 0
|
| 33 |
self._consecutive_kills = 0
|
|
@@ -46,8 +48,6 @@ class AlphaPipeline:
|
|
| 46 |
existing_tags = self.store.get_all_anomaly_tags()
|
| 47 |
dead_themes = self.store.get_dead_themes()
|
| 48 |
existing_hashes = self.store.get_expression_hashes()
|
| 49 |
-
|
| 50 |
-
# Track themes used in THIS batch to force diversity
|
| 51 |
batch_themes_used: list[str] = []
|
| 52 |
|
| 53 |
promoted = 0
|
|
@@ -64,12 +64,9 @@ class AlphaPipeline:
|
|
| 64 |
try:
|
| 65 |
result = await self._run_single_candidate(
|
| 66 |
existing_themes + batch_themes_used,
|
| 67 |
-
existing_tags,
|
| 68 |
-
dead_themes,
|
| 69 |
-
existing_hashes,
|
| 70 |
batch_themes_used,
|
| 71 |
)
|
| 72 |
-
|
| 73 |
if result == Verdict.PROMOTE:
|
| 74 |
promoted += 1
|
| 75 |
elif result == Verdict.ITERATE:
|
|
@@ -77,10 +74,8 @@ class AlphaPipeline:
|
|
| 77 |
else:
|
| 78 |
killed += 1
|
| 79 |
self._consecutive_kills += 1
|
| 80 |
-
|
| 81 |
if result != Verdict.KILL:
|
| 82 |
self._consecutive_kills = 0
|
| 83 |
-
|
| 84 |
except Exception as e:
|
| 85 |
console.print(f"[red]Error: {e}[/]")
|
| 86 |
killed += 1
|
|
@@ -90,28 +85,19 @@ class AlphaPipeline:
|
|
| 90 |
f"Tokens used: {self.llm.tokens_used:,} | BRAIN submissions: {self._daily_submissions}",
|
| 91 |
title="Batch Complete"
|
| 92 |
))
|
| 93 |
-
|
| 94 |
return {"promoted": promoted, "iterated": iterated, "killed": killed}
|
| 95 |
|
| 96 |
-
async def _run_single_candidate(
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
dead_themes: list[str],
|
| 101 |
-
existing_hashes: set[str],
|
| 102 |
-
batch_themes_used: list[str],
|
| 103 |
-
) -> Verdict:
|
| 104 |
-
|
| 105 |
-
# STEP 1: Pick theme — penalize themes already used in this batch
|
| 106 |
-
# By adding batch_themes_used to existing_themes, gap_score penalizes repeats
|
| 107 |
theme = pick_theme(existing_themes, existing_tags, dead_themes)
|
| 108 |
batch_themes_used.append(theme)
|
| 109 |
console.print(f" [cyan]Theme:[/] {theme}")
|
| 110 |
|
| 111 |
-
# STEP 2: Generate hypothesis
|
| 112 |
-
retrieved_papers = []
|
| 113 |
blueprint = await generate_hypothesis(
|
| 114 |
-
self.llm, theme,
|
| 115 |
)
|
| 116 |
console.print(f" [cyan]Blueprint:[/] {blueprint.archetype} | {blueprint.anomaly_tag.value}")
|
| 117 |
console.print(f" [dim]Novelty: {blueprint.novelty_claim[:80]}...[/]")
|
|
@@ -120,13 +106,12 @@ class AlphaPipeline:
|
|
| 120 |
expression = await compile_expression(blueprint, self.llm)
|
| 121 |
console.print(f" [cyan]Expression:[/] {expression.expression[:80]}...")
|
| 122 |
|
| 123 |
-
# STEP 4:
|
| 124 |
lint_result = lint(expression.expression)
|
| 125 |
if not lint_result.passed:
|
| 126 |
console.print(f" [red]LINT FAIL:[/] {lint_result.errors}")
|
| 127 |
self._consecutive_lint_fails += 1
|
| 128 |
return Verdict.KILL
|
| 129 |
-
|
| 130 |
self._consecutive_lint_fails = 0
|
| 131 |
if lint_result.warnings:
|
| 132 |
console.print(f" [yellow]Warnings:[/] {lint_result.warnings}")
|
|
@@ -138,7 +123,6 @@ class AlphaPipeline:
|
|
| 138 |
if alpha_id in existing_hashes:
|
| 139 |
console.print(f" [red]DEDUP:[/] Already exists")
|
| 140 |
return Verdict.KILL
|
| 141 |
-
|
| 142 |
existing_hashes.add(alpha_id)
|
| 143 |
|
| 144 |
# STEP 6: Store
|
|
@@ -155,12 +139,7 @@ class AlphaPipeline:
|
|
| 155 |
academic_anchor=blueprint.academic_anchor,
|
| 156 |
)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
if self.brain is None:
|
| 160 |
-
console.print(" [yellow]DRY RUN:[/] Skipping BRAIN submission")
|
| 161 |
-
console.print(f" [green]+ Candidate {alpha_id} passed[/]")
|
| 162 |
-
return Verdict.ITERATE
|
| 163 |
-
|
| 164 |
return Verdict.ITERATE
|
| 165 |
|
| 166 |
def _check_kill_switches(self) -> bool:
|
|
@@ -168,7 +147,9 @@ class AlphaPipeline:
|
|
| 168 |
return True
|
| 169 |
if self._consecutive_kills >= self.config.kill.consecutive_kill_verdict_max:
|
| 170 |
return True
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
return True
|
| 173 |
return False
|
| 174 |
|
|
|
|
| 1 |
"""
|
| 2 |
+
Pipeline Orchestrator v3 — Honest implementation.
|
| 3 |
+
Only runs steps that actually work: theme → hypothesis → compile → lint → store.
|
| 4 |
+
Dead personas removed from active pipeline.
|
| 5 |
"""
|
| 6 |
import asyncio
|
| 7 |
from datetime import datetime
|
| 8 |
from rich.console import Console
|
| 9 |
from rich.panel import Panel
|
| 10 |
|
| 11 |
+
from ..config import Config
|
| 12 |
+
from ..infra import LLMClient, FactorStore
|
| 13 |
+
from ..deterministic import lint, quick_dedup_hash, pick_theme
|
| 14 |
from ..deterministic.theme_sampler import THEME_FIELDS
|
| 15 |
+
from ..personas.hypothesis_hunter import generate_hypothesis
|
| 16 |
+
from ..personas.expression_compiler import compile_expression
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
from ..schemas import Verdict
|
| 18 |
|
| 19 |
console = Console()
|
| 20 |
|
| 21 |
|
| 22 |
class AlphaPipeline:
|
| 23 |
+
"""
|
| 24 |
+
Alpha generation pipeline.
|
| 25 |
+
Active steps: theme_pick → hypothesis → compile → lint → dedup → store.
|
| 26 |
+
BRAIN submission, crowd scout, surgeon, gatekeeper are NOT connected.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
def __init__(self, config: Config):
|
| 30 |
self.config = config
|
| 31 |
self.llm = LLMClient(config.llm)
|
| 32 |
self.store = FactorStore(config.paths.factor_store / "alphas.duckdb")
|
|
|
|
| 33 |
|
| 34 |
self._consecutive_lint_fails = 0
|
| 35 |
self._consecutive_kills = 0
|
|
|
|
| 48 |
existing_tags = self.store.get_all_anomaly_tags()
|
| 49 |
dead_themes = self.store.get_dead_themes()
|
| 50 |
existing_hashes = self.store.get_expression_hashes()
|
|
|
|
|
|
|
| 51 |
batch_themes_used: list[str] = []
|
| 52 |
|
| 53 |
promoted = 0
|
|
|
|
| 64 |
try:
|
| 65 |
result = await self._run_single_candidate(
|
| 66 |
existing_themes + batch_themes_used,
|
| 67 |
+
existing_tags, dead_themes, existing_hashes,
|
|
|
|
|
|
|
| 68 |
batch_themes_used,
|
| 69 |
)
|
|
|
|
| 70 |
if result == Verdict.PROMOTE:
|
| 71 |
promoted += 1
|
| 72 |
elif result == Verdict.ITERATE:
|
|
|
|
| 74 |
else:
|
| 75 |
killed += 1
|
| 76 |
self._consecutive_kills += 1
|
|
|
|
| 77 |
if result != Verdict.KILL:
|
| 78 |
self._consecutive_kills = 0
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
console.print(f"[red]Error: {e}[/]")
|
| 81 |
killed += 1
|
|
|
|
| 85 |
f"Tokens used: {self.llm.tokens_used:,} | BRAIN submissions: {self._daily_submissions}",
|
| 86 |
title="Batch Complete"
|
| 87 |
))
|
|
|
|
| 88 |
return {"promoted": promoted, "iterated": iterated, "killed": killed}
|
| 89 |
|
| 90 |
+
async def _run_single_candidate(self, existing_themes, existing_tags,
|
| 91 |
+
dead_themes, existing_hashes, batch_themes_used) -> Verdict:
|
| 92 |
+
|
| 93 |
+
# STEP 1: Pick theme
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
theme = pick_theme(existing_themes, existing_tags, dead_themes)
|
| 95 |
batch_themes_used.append(theme)
|
| 96 |
console.print(f" [cyan]Theme:[/] {theme}")
|
| 97 |
|
| 98 |
+
# STEP 2: Generate hypothesis (LLM)
|
|
|
|
| 99 |
blueprint = await generate_hypothesis(
|
| 100 |
+
self.llm, theme, [], existing_tags
|
| 101 |
)
|
| 102 |
console.print(f" [cyan]Blueprint:[/] {blueprint.archetype} | {blueprint.anomaly_tag.value}")
|
| 103 |
console.print(f" [dim]Novelty: {blueprint.novelty_claim[:80]}...[/]")
|
|
|
|
| 106 |
expression = await compile_expression(blueprint, self.llm)
|
| 107 |
console.print(f" [cyan]Expression:[/] {expression.expression[:80]}...")
|
| 108 |
|
| 109 |
+
# STEP 4: Lint
|
| 110 |
lint_result = lint(expression.expression)
|
| 111 |
if not lint_result.passed:
|
| 112 |
console.print(f" [red]LINT FAIL:[/] {lint_result.errors}")
|
| 113 |
self._consecutive_lint_fails += 1
|
| 114 |
return Verdict.KILL
|
|
|
|
| 115 |
self._consecutive_lint_fails = 0
|
| 116 |
if lint_result.warnings:
|
| 117 |
console.print(f" [yellow]Warnings:[/] {lint_result.warnings}")
|
|
|
|
| 123 |
if alpha_id in existing_hashes:
|
| 124 |
console.print(f" [red]DEDUP:[/] Already exists")
|
| 125 |
return Verdict.KILL
|
|
|
|
| 126 |
existing_hashes.add(alpha_id)
|
| 127 |
|
| 128 |
# STEP 6: Store
|
|
|
|
| 139 |
academic_anchor=blueprint.academic_anchor,
|
| 140 |
)
|
| 141 |
|
| 142 |
+
console.print(f" [green]+ Stored {alpha_id}[/]")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
return Verdict.ITERATE
|
| 144 |
|
| 145 |
def _check_kill_switches(self) -> bool:
|
|
|
|
| 147 |
return True
|
| 148 |
if self._consecutive_kills >= self.config.kill.consecutive_kill_verdict_max:
|
| 149 |
return True
|
| 150 |
+
# Token budget check
|
| 151 |
+
if self.llm.tokens_used >= self.config.kill.daily_llm_token_budget:
|
| 152 |
+
console.print("[red]Kill switch: token budget exhausted[/]")
|
| 153 |
return True
|
| 154 |
return False
|
| 155 |
|