Files
elizaos--eliza/packages/benchmarks/tests/test_random_baseline_harness.py
wehub-resource-sync 426e9eeabd
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Waiting to run
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Waiting to run
Build Agent Image / build-and-push (push) Waiting to run
Chat shell gestures / Chat shell gesture + parity e2e (push) Waiting to run
ci / test (push) Waiting to run
ci / lint-and-format (push) Waiting to run
ci / build (push) Waiting to run
ci / dev-startup (push) Waiting to run
Cloud Gateway Discord / Test (push) Waiting to run
Cloud Gateway Webhook / Test (push) Waiting to run
Cloud Tests / lint-and-types (push) Waiting to run
Cloud Tests / unit-tests (push) Waiting to run
Cloud Tests / integration-tests (push) Waiting to run
Cloud Tests / e2e-tests (push) Blocked by required conditions
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Apps Worker (Product 2) / Determine environment (push) Waiting to run
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Blocked by required conditions
Deploy Eliza Provisioning Worker / Determine environment (push) Waiting to run
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Blocked by required conditions
Dev Smoke / Classify changed paths (push) Waiting to run
Dev Smoke / bun run dev onboarding chat (push) Blocked by required conditions
Dev Smoke / Vite HMR dependency-level smoke (push) Blocked by required conditions
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Waiting to run
gitleaks / gitleaks (push) Waiting to run
Markdown Links / Relative Markdown Links (push) Waiting to run
Publish @elizaos/example-code / check_npm (push) Waiting to run
Publish @elizaos/example-code / publish_npm (push) Blocked by required conditions
Publish @elizaos/plugin-elizacloud / verify_version (push) Waiting to run
Publish @elizaos/plugin-elizacloud / publish_npm (push) Blocked by required conditions
Quality (Extended) / Homepage Build (PR smoke) (push) Waiting to run
Quality (Extended) / Comment-only diff guard (push) Waiting to run
Quality (Extended) / Format + Type Safety Ratchet (push) Waiting to run
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Waiting to run
Quality (Extended) / Develop Gate (lint) (push) Waiting to run
Sandbox Live Smoke / Sandbox live smoke (push) Waiting to run
Snap Build & Test / Build Snap (amd64) (push) Waiting to run
Snap Build & Test / Build Snap (arm64) (push) Waiting to run
supply-chain / sbom (push) Waiting to run
supply-chain / vulnerability-scan (push) Waiting to run
Build, Push & Deploy to Phala Cloud / build-and-push (push) Waiting to run
Test Packaging / Validate Packaging Configs (push) Waiting to run
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Waiting to run
Test Packaging / Pack & Test JS Tarballs (push) Waiting to run
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Waiting to run
UI Fixture E2E / ui-fixture-e2e (push) Waiting to run
UI Fixture E2E / fixture-e2e (push) Waiting to run
UI Story Gate / story-gate (push) Waiting to run
vault-ci / test (macos-latest) (push) Waiting to run
vault-ci / test (ubuntu-latest) (push) Waiting to run
vault-ci / test (windows-latest) (push) Waiting to run
vault-ci / app-core wiring tests (push) Waiting to run
verify-patches / verify patches/CHECKSUMS.sha256 (push) Waiting to run
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Waiting to run
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Waiting to run
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Waiting to run
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Waiting to run
Voice Benchmark Smoke / voice bench smoke summary (push) Blocked by required conditions
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Waiting to run
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Waiting to run
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Waiting to run
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Waiting to run
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Waiting to run
Test Packaging / Build & Test PyPI Package (push) Waiting to run
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

384 lines
11 KiB
Python

"""Tests for the ``random_v1`` harness and the ``compare-vs-random`` CLI.
Covers three layers:
1. ``run_random_baseline`` in-process synthesizer — verifies it
produces the right result-file shape for a known benchmark
(``bfcl``), correctly reports ``incompatible`` for benchmarks whose
strategy is uninterpretable (``solana``), and gracefully degrades
for benchmarks with no result template registered (records via
metrics only).
2. ``run_compare_vs_random`` — populates a fresh SQLite DB with one
real agent run and one random_v1 baseline, then asserts the
function returns 0 when the lift beats the threshold and 1 when
it doesn't. Output is captured via ``capsys`` to confirm the
table headers print.
3. CLI dispatch — exercises ``build_parser()`` and verifies
``compare-vs-random`` is wired in.
"""
from __future__ import annotations
import io
import json
import sys
from contextlib import redirect_stdout
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT.parent))
from orchestrator.cli import build_parser # noqa: E402
from orchestrator.compare_vs_random import run_compare_vs_random # noqa: E402
from orchestrator.db import ( # noqa: E402
connect_database,
initialize_database,
insert_run_start,
update_run_result,
)
from orchestrator.random_baseline_runner import ( # noqa: E402
run_random_baseline,
)
# ----------------- in-process synthesizer ------------------
def test_random_baseline_bfcl_writes_result_file(tmp_path: Path) -> None:
outcome = run_random_baseline(
benchmark_id="bfcl",
output_dir=tmp_path,
score=0.0,
)
assert outcome.status == "succeeded"
assert outcome.is_meaningful is True
assert outcome.strategy_name == "function_call"
assert outcome.result_path is not None
assert outcome.result_path.exists()
payload = json.loads(outcome.result_path.read_text(encoding="utf-8"))
assert payload["metrics"]["overall_score"] == 0.0
def test_random_baseline_incompatible_for_solana(tmp_path: Path) -> None:
outcome = run_random_baseline(
benchmark_id="solana",
output_dir=tmp_path,
score=0.0,
)
assert outcome.status == "incompatible"
assert outcome.is_meaningful is False
assert outcome.note == "random baseline uninterpretable for this benchmark"
assert outcome.result_path is None
def test_random_baseline_unknown_benchmark_no_template(tmp_path: Path) -> None:
# ``tau-bench`` is in the registry as ``function_call`` but has no
# result template in _RESULT_TEMPLATES — expected to succeed but
# record via metrics only.
outcome = run_random_baseline(
benchmark_id="tau-bench",
output_dir=tmp_path,
score=0.0,
)
assert outcome.status == "succeeded"
assert outcome.result_path is None
assert outcome.note is not None
# ----------------- compare-vs-random orchestration ------------------
def _seed_db(
*,
workspace_root: Path,
benchmark_id: str,
agent_score: float,
random_score: float,
agent_label: str = "eliza",
) -> None:
db_path = workspace_root / "benchmarks" / "benchmark_results" / "orchestrator.sqlite"
db_path.parent.mkdir(parents=True, exist_ok=True)
conn = connect_database(db_path)
initialize_database(conn)
# Insert a synthetic run_group_id row by creating one row each.
# The compare-vs-random query reads benchmark_runs only, but we
# still need a valid run_group_id FK target to satisfy the schema.
conn.execute(
"""
INSERT INTO run_groups (
run_group_id, created_at, request_json, benchmarks_json,
repo_meta_json, created_by
) VALUES (?, ?, ?, ?, ?, ?)
""",
("rg_test", "2026-05-11T00:00:00+00:00", "{}", "[]", "{}", "tests"),
)
for run_id, agent, score in (
("run_agent", agent_label, agent_score),
("run_random", "random_v1", random_score),
):
insert_run_start(
conn,
run_id=run_id,
run_group_id="rg_test",
benchmark_id=benchmark_id,
benchmark_directory=benchmark_id,
signature=f"sig-{run_id}",
attempt=1,
agent=agent,
provider="test",
model="test-model",
extra_config={},
started_at="2026-05-11T00:00:00+00:00",
command=[],
cwd=str(workspace_root),
stdout_path="",
stderr_path="",
benchmark_version=None,
benchmarks_commit=None,
eliza_commit=None,
eliza_version=None,
)
update_run_result(
conn,
run_id=run_id,
status="succeeded",
ended_at="2026-05-11T00:00:01+00:00",
duration_seconds=1.0,
score=score,
unit="ratio",
higher_is_better=True,
metrics={},
result_json_path=None,
artifacts=[],
error=None,
high_score_label=None,
high_score_value=None,
delta_to_high_score=None,
)
conn.commit()
conn.close()
def test_compare_vs_random_passes_when_above_min_lift(tmp_path: Path) -> None:
_seed_db(
workspace_root=tmp_path,
benchmark_id="bfcl",
agent_score=0.8,
random_score=0.4, # 2x lift, above 1.5x threshold
)
buf = io.StringIO()
with redirect_stdout(buf):
rc = run_compare_vs_random(
workspace_root=tmp_path,
agents=["eliza"],
benchmarks=["bfcl"],
min_lift=1.5,
)
assert rc == 0
out = buf.getvalue()
assert "benchmark" in out
assert "lift" in out
assert "2.00x" in out
def test_compare_vs_random_fails_when_below_min_lift(tmp_path: Path) -> None:
_seed_db(
workspace_root=tmp_path,
benchmark_id="bfcl",
agent_score=0.5,
random_score=0.4, # 1.25x, below 1.5x threshold
)
buf = io.StringIO()
with redirect_stdout(buf):
rc = run_compare_vs_random(
workspace_root=tmp_path,
agents=["eliza"],
benchmarks=["bfcl"],
min_lift=1.5,
)
assert rc == 1
out = buf.getvalue()
assert "FAIL" in out
def test_compare_vs_random_ignores_newer_scoreless_success(tmp_path: Path) -> None:
_seed_db(
workspace_root=tmp_path,
benchmark_id="bfcl",
agent_score=0.8,
random_score=0.4,
)
db_path = tmp_path / "benchmarks" / "benchmark_results" / "orchestrator.sqlite"
conn = connect_database(db_path)
insert_run_start(
conn,
run_id="run_agent_scoreless",
run_group_id="rg_test",
benchmark_id="bfcl",
benchmark_directory="bfcl",
signature="sig-run_agent_scoreless",
attempt=2,
agent="eliza",
provider="test",
model="test-model",
extra_config={},
started_at="2026-05-11T00:01:00+00:00",
command=[],
cwd=str(tmp_path),
stdout_path="",
stderr_path="",
benchmark_version=None,
benchmarks_commit=None,
eliza_commit=None,
eliza_version=None,
)
update_run_result(
conn,
run_id="run_agent_scoreless",
status="succeeded",
ended_at="2026-05-11T00:01:01+00:00",
duration_seconds=1.0,
score=None,
unit=None,
higher_is_better=None,
metrics={"reason": "empty_result"},
result_json_path=None,
artifacts=[],
error=None,
high_score_label=None,
high_score_value=None,
delta_to_high_score=None,
)
conn.close()
buf = io.StringIO()
with redirect_stdout(buf):
rc = run_compare_vs_random(
workspace_root=tmp_path,
agents=["eliza"],
benchmarks=["bfcl"],
min_lift=1.5,
)
assert rc == 0
out = buf.getvalue()
assert "run_agent_scoreless" not in out
assert "2.00x" in out
def test_compare_vs_random_skips_threshold_when_baseline_missing(
tmp_path: Path,
) -> None:
db_path = tmp_path / "benchmarks" / "benchmark_results" / "orchestrator.sqlite"
db_path.parent.mkdir(parents=True, exist_ok=True)
conn = connect_database(db_path)
initialize_database(conn)
conn.execute(
"""
INSERT INTO run_groups (
run_group_id, created_at, request_json, benchmarks_json,
repo_meta_json, created_by
) VALUES (?, ?, ?, ?, ?, ?)
""",
("rg_nb", "2026-05-11T00:00:00+00:00", "{}", "[]", "{}", "tests"),
)
insert_run_start(
conn,
run_id="run_only_agent",
run_group_id="rg_nb",
benchmark_id="bfcl",
benchmark_directory="bfcl",
signature="sig",
attempt=1,
agent="eliza",
provider="test",
model="test-model",
extra_config={},
started_at="2026-05-11T00:00:00+00:00",
command=[],
cwd=str(tmp_path),
stdout_path="",
stderr_path="",
benchmark_version=None,
benchmarks_commit=None,
eliza_commit=None,
eliza_version=None,
)
update_run_result(
conn,
run_id="run_only_agent",
status="succeeded",
ended_at="2026-05-11T00:00:01+00:00",
duration_seconds=1.0,
score=0.1,
unit="ratio",
higher_is_better=True,
metrics={},
result_json_path=None,
artifacts=[],
error=None,
high_score_label=None,
high_score_value=None,
delta_to_high_score=None,
)
conn.commit()
conn.close()
buf = io.StringIO()
with redirect_stdout(buf):
rc = run_compare_vs_random(
workspace_root=tmp_path,
agents=["eliza"],
benchmarks=["bfcl"],
min_lift=1.5,
)
# No baseline -> nothing to enforce -> exit 0
assert rc == 0
# ----------------- CLI dispatch ------------------
def test_build_parser_includes_compare_vs_random() -> None:
parser = build_parser()
args = parser.parse_args(
[
"compare-vs-random",
"--agents",
"eliza,openclaw",
"--benchmarks",
"bfcl",
"--min-lift",
"2.0",
]
)
assert args.cmd == "compare-vs-random"
assert args.agents == "eliza,openclaw"
assert args.benchmarks == "bfcl"
assert args.min_lift == pytest.approx(2.0)
def test_random_v1_harness_accepted_by_selected_harnesses() -> None:
from orchestrator.cli import _selected_harnesses # noqa: PLC0415
parser = build_parser()
args = parser.parse_args(
[
"run",
"--benchmarks",
"bfcl",
"--harnesses",
"eliza,random_v1",
]
)
harnesses = _selected_harnesses(args)
assert "eliza" in harnesses
assert "random_v1" in harnesses