fix(orchestrator): definisci prompt_library PRIMA di istanziare HypothesisAgent
Bug introdotto in b6f48e4: HypothesisAgent(prompt_library=prompt_library) era
chiamato a riga 109, ma prompt_library veniva definito a riga 123 -> NameError
a runtime quando run_phase1 viene eseguito.
Spostato il blocco di setup prompt_library + set_cognitive_styles PRIMA della
istanziazione di HypothesisAgent.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,34 +3,12 @@ from __future__ import annotations
|
||||
import random
|
||||
|
||||
from ..genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from ..genome.mutation import COGNITIVE_STYLES
|
||||
from ..genome.prompt_library import PromptLibrary
|
||||
|
||||
STYLE_PROMPTS: dict[str, str] = {
|
||||
"physicist": (
|
||||
"Cerca leggi conservative, simmetrie, regimi di scala. "
|
||||
"Pensa in termini di flussi e potenziali."
|
||||
),
|
||||
"biologist": (
|
||||
"Cerca pattern adattivi, nicchie ecologiche, "
|
||||
"predator-prey dynamics tra partecipanti del mercato."
|
||||
),
|
||||
"historian": (
|
||||
"Cerca pattern ricorrenti su scale temporali multiple, "
|
||||
"analogie con regimi storici, mean reversion strutturali."
|
||||
),
|
||||
"meteorologist": (
|
||||
"Cerca regimi di volatilità che si autoalimentano, "
|
||||
"transizioni di stato come fronti, persistenza locale."
|
||||
),
|
||||
"ecologist": (
|
||||
"Cerca interazioni multi-asset, correlazioni cluster, "
|
||||
"segnali di stress sistemico nelle dinamiche di flusso."
|
||||
),
|
||||
"engineer": (
|
||||
"Cerca segnali con rapporto S/N favorevole, filtri causali, "
|
||||
"robustezza a perturbazioni di calibrazione."
|
||||
),
|
||||
}
|
||||
# Mantenuto come alias backcompat: equivalente a PromptLibrary.default().styles.
|
||||
# Nuovi caller dovrebbero usare PromptLibrary direttamente per supportare
|
||||
# l'override via prompts.json di una strategia.
|
||||
STYLE_PROMPTS: dict[str, str] = PromptLibrary.default().styles
|
||||
|
||||
|
||||
def build_initial_population(
|
||||
@@ -38,15 +16,22 @@ def build_initial_population(
|
||||
model_tier: ModelTier,
|
||||
rng: random.Random,
|
||||
feature_pool: tuple[str, ...] = ("close", "high", "low", "volume"),
|
||||
prompt_library: PromptLibrary | None = None,
|
||||
) -> list[HypothesisAgentGenome]:
|
||||
"""Costruisce una popolazione iniziale K varia per stile cognitivo + parametri."""
|
||||
"""Costruisce una popolazione iniziale K varia per stile cognitivo + parametri.
|
||||
|
||||
``prompt_library`` controlla quali stili sono disponibili e quale system_prompt
|
||||
iniziale viene assegnato. Default = builtin 6 stili (physicist, biologist, ...).
|
||||
Override tipico: ``PromptLibrary.from_json(strategy_crypto/prompts.json)``.
|
||||
"""
|
||||
lib = prompt_library or PromptLibrary.default()
|
||||
population: list[HypothesisAgentGenome] = []
|
||||
for i in range(k):
|
||||
style = COGNITIVE_STYLES[i % len(COGNITIVE_STYLES)]
|
||||
style = lib.style_at(i)
|
||||
n_features = rng.randint(1, len(feature_pool))
|
||||
feats = sorted(rng.sample(feature_pool, k=n_features))
|
||||
g = HypothesisAgentGenome(
|
||||
system_prompt=STYLE_PROMPTS[style],
|
||||
system_prompt=lib.directive(style),
|
||||
feature_access=feats,
|
||||
temperature=round(rng.uniform(0.7, 1.2), 2),
|
||||
top_p=0.95,
|
||||
|
||||
@@ -7,6 +7,10 @@ from .hypothesis import HypothesisAgentGenome
|
||||
|
||||
FEATURE_POOL: tuple[str, ...] = ("open", "high", "low", "close", "volume")
|
||||
|
||||
# Lista di default builtin (allineata con PromptLibrary.default()).
|
||||
# Il dispatcher run_phase1 sovrascrive `COGNITIVE_STYLES` con la lista letta
|
||||
# da prompts.json prima del loop GA, cosi' `mutate_cognitive_style` pesca
|
||||
# dai candidati corretti per la strategia in corso.
|
||||
COGNITIVE_STYLES: tuple[str, ...] = (
|
||||
"physicist",
|
||||
"biologist",
|
||||
@@ -17,6 +21,18 @@ COGNITIVE_STYLES: tuple[str, ...] = (
|
||||
)
|
||||
|
||||
|
||||
def set_cognitive_styles(styles: tuple[str, ...]) -> None:
|
||||
"""Sovrascrive la lista globale di stili candidati per la mutation.
|
||||
|
||||
Da chiamare PRIMA del GA loop (es. in run_phase1 dopo aver caricato la
|
||||
PromptLibrary). Non thread-safe: pensata per uno script CLI.
|
||||
"""
|
||||
global COGNITIVE_STYLES
|
||||
if not styles:
|
||||
raise ValueError("set_cognitive_styles: lista vuota")
|
||||
COGNITIVE_STYLES = tuple(styles)
|
||||
|
||||
|
||||
def _clone_with(g: HypothesisAgentGenome, **overrides: Any) -> HypothesisAgentGenome:
|
||||
payload: dict[str, Any] = g.to_dict()
|
||||
payload.update(overrides)
|
||||
|
||||
@@ -106,6 +106,12 @@ def run_phase1(
|
||||
|
||||
market = build_market_summary(train_ohlcv, symbol=cfg.symbol, timeframe=cfg.timeframe)
|
||||
|
||||
# Propaga la libreria di stili al modulo mutation (cosi' mutate_cognitive_style
|
||||
# pesca dai candidati coerenti col JSON della strategia in corso). Va FATTO
|
||||
# PRIMA di istanziare HypothesisAgent (che la riceve in costruttore).
|
||||
prompt_library = cfg.prompt_library or PromptLibrary.default()
|
||||
set_cognitive_styles(prompt_library.cognitive_styles)
|
||||
|
||||
hypothesis_agent = HypothesisAgent(llm=llm, prompt_library=prompt_library)
|
||||
falsification_agent = FalsificationAgent(
|
||||
fees_bp=cfg.fees_bp, n_trials_dsr=cfg.n_trials_dsr
|
||||
@@ -118,11 +124,6 @@ def run_phase1(
|
||||
)
|
||||
cost_tracker = CostTracker()
|
||||
|
||||
# Propaga la libreria di stili al modulo mutation (cosi' mutate_cognitive_style
|
||||
# pesca dai candidati coerenti col JSON della strategia in corso).
|
||||
prompt_library = cfg.prompt_library or PromptLibrary.default()
|
||||
set_cognitive_styles(prompt_library.cognitive_styles)
|
||||
|
||||
population = build_initial_population(
|
||||
k=cfg.population_size,
|
||||
model_tier=cfg.model_tier,
|
||||
|
||||
@@ -18,3 +18,4 @@ build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel.force-include]
|
||||
"strategy_crypto/strategies" = "strategy_crypto/strategies"
|
||||
"strategy_crypto/prompts.json" = "strategy_crypto/prompts.json"
|
||||
|
||||
Reference in New Issue
Block a user