34f88865da
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from multi_swarm.genome.hypothesis import HypothesisAgentGenome, ModelTier
|
|
from multi_swarm.persistence.repository import Repository
|
|
|
|
|
|
def make_genome(idx: int) -> HypothesisAgentGenome:
|
|
return HypothesisAgentGenome(
|
|
system_prompt=f"p-{idx}", feature_access=["close"], temperature=0.9,
|
|
top_p=0.95, model_tier=ModelTier.C, lookback_window=100, cognitive_style="x",
|
|
)
|
|
|
|
|
|
def test_repository_creates_schema(tmp_path: Path):
|
|
repo = Repository(db_path=tmp_path / "runs.db")
|
|
repo.init_schema()
|
|
assert (tmp_path / "runs.db").exists()
|
|
|
|
|
|
def test_repository_create_run_and_get(tmp_path: Path):
|
|
repo = Repository(db_path=tmp_path / "runs.db")
|
|
repo.init_schema()
|
|
run_id = repo.create_run(name="phase1-test", config={"k": 20})
|
|
run = repo.get_run(run_id)
|
|
assert run["name"] == "phase1-test"
|
|
assert json.loads(run["config_json"])["k"] == 20
|
|
|
|
|
|
def test_repository_save_genome_and_evaluation(tmp_path: Path):
|
|
repo = Repository(db_path=tmp_path / "runs.db")
|
|
repo.init_schema()
|
|
run_id = repo.create_run(name="t", config={})
|
|
g = make_genome(0)
|
|
repo.save_genome(run_id=run_id, generation_idx=0, genome=g)
|
|
repo.save_evaluation(
|
|
run_id=run_id, genome_id=g.id, fitness=0.5, dsr=0.7, dsr_pvalue=0.05,
|
|
sharpe=1.5, max_dd=0.2, total_return=0.3, n_trades=30,
|
|
parse_error=None, raw_text="(strategy ...)",
|
|
)
|
|
evals = repo.list_evaluations(run_id)
|
|
assert len(evals) == 1
|
|
assert evals[0]["fitness"] == 0.5
|
|
|
|
|
|
def test_repository_save_generation_summary(tmp_path: Path):
|
|
repo = Repository(db_path=tmp_path / "runs.db")
|
|
repo.init_schema()
|
|
run_id = repo.create_run(name="t", config={})
|
|
repo.save_generation_summary(
|
|
run_id=run_id, generation_idx=0, n_genomes=20,
|
|
fitness_median=0.3, fitness_max=0.8, fitness_p90=0.7, entropy=0.85,
|
|
)
|
|
gens = repo.list_generations(run_id)
|
|
assert len(gens) == 1
|
|
assert gens[0]["fitness_max"] == 0.8
|