refactor: organize pilot packages

Group review, feedback, evaluation, observability, and entrypoint code into packages. Keep thin top-level compatibility shims for existing scripts and imports, and mirror the structure in the tests.
This commit is contained in:
Claude
2026-09-01 00:59:51 +00:00
parent 3a110ab52c
commit 7a510a926d
66 changed files with 8174 additions and 8039 deletions
@@ -0,0 +1 @@
"""Observability tests."""
@@ -0,0 +1,282 @@
"""Unit tests for the per-review cost model. No network."""
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, "..", "..", ".."))
sys.path.insert(0, os.path.join(ROOT, "pilot"))
import cost_model as cm # noqa: E402
FACTORY = cm.measure_factory(ROOT)
# ---------------------------------------------------------------------------
# measure_factory — reads the real files
# ---------------------------------------------------------------------------
def test_measure_factory_finds_agent_and_skills():
assert FACTORY["agent"] > 500
for skill in cm.ALWAYS_SKILLS:
assert FACTORY[f"skill:{skill}"] > 100, skill
for lens in ("security", "tests", "perf"):
assert FACTORY[f"subagent:{lens}"] > 100, lens
def test_measure_factory_missing_root_is_empty_not_an_error():
f = cm.measure_factory("/nonexistent-path-for-test")
assert f == {"agent": 0, "subagent:security": 0, "subagent:tests": 0, "subagent:perf": 0}
# ---------------------------------------------------------------------------
# prefix_tokens — tiers load different skill sets
# ---------------------------------------------------------------------------
def test_prefix_grows_with_tier():
sizes = [cm.prefix_tokens(t, FACTORY) for t in ("trivial", "lite", "full", "oversized")]
assert sizes == sorted(sizes)
assert sizes[0] < sizes[-1]
def test_prefix_includes_harness_and_agent():
assert cm.prefix_tokens("trivial", FACTORY) > cm.HARNESS_TOKENS + FACTORY["agent"]
def test_unknown_tier_still_returns_the_always_skills():
assert cm.prefix_tokens("nope", FACTORY) == cm.prefix_tokens("trivial", FACTORY)
# ---------------------------------------------------------------------------
# tier_usage — the loop's resend behaviour is what costs money
# ---------------------------------------------------------------------------
def _tier(name):
return next(t for t in cm.DEFAULT_TIERS if t.name == name)
def test_caching_moves_the_stable_prefix_out_of_uncached_input():
t = _tier("full")
cached = cm.tier_usage(t, FACTORY, caching=True)
uncached = cm.tier_usage(t, FACTORY, caching=False)
assert cached.uncached_input < uncached.uncached_input
assert cached.cached_input > 0
assert uncached.cached_input == 0
assert uncached.cache_writes == 0
def test_total_input_is_the_same_work_either_way():
# Caching changes the *price* of the tokens, not how many are sent.
t = _tier("full")
a = cm.tier_usage(t, FACTORY, caching=True)
b = cm.tier_usage(t, FACTORY, caching=False)
# With caching the step-1 stable block is billed as a cache write rather
# than as input, so it moves columns — the grand total of tokens sent is
# identical.
assert a.total_input + a.cache_writes == b.total_input
def test_more_steps_cost_more_input():
base = _tier("lite")
more = cm.Tier(
"lite-long", base.diff_tokens, base.steps * 2, base.file_reads,
base.tokens_per_read, base.output_tokens,
)
assert cm.tier_usage(more, FACTORY).total_input > cm.tier_usage(base, FACTORY).total_input
def test_trivial_tier_does_no_tool_work():
u = cm.tier_usage(_tier("trivial"), FACTORY)
assert u.uncached_input == 0 # no tool-result tail at all
assert u.output > 0
def test_subagents_add_input_and_output():
t = _tier("full")
with_subs = cm.Tier(
t.name, t.diff_tokens, t.steps, t.file_reads, t.tokens_per_read,
t.output_tokens, subagents=2,
)
a, b = cm.tier_usage(t, FACTORY), cm.tier_usage(with_subs, FACTORY)
assert b.total_input > a.total_input
assert b.output > a.output
# ---------------------------------------------------------------------------
# cost — prices and discounts
# ---------------------------------------------------------------------------
def test_cost_is_ordered_by_model_price():
u = cm.tier_usage(_tier("full"), FACTORY)
opus = cm.cost(u, cm.PRICES["claude-opus-5"])
sonnet = cm.cost(u, cm.PRICES["claude-sonnet-5"])
haiku = cm.cost(u, cm.PRICES["claude-haiku-4-5"])
assert opus > sonnet > haiku > 0
def test_batch_is_exactly_half():
u = cm.tier_usage(_tier("full"), FACTORY)
p = cm.PRICES["claude-opus-5"]
assert abs(cm.cost(u, p, batch=True) * 2 - cm.cost(u, p)) < 1e-9
def test_cost_matches_a_hand_calculation():
u = cm.Usage(uncached_input=1_000_000, cached_input=1_000_000,
cache_writes=1_000_000, output=1_000_000)
p = cm.PRICES["claude-opus-5"] # 5 / 25 / 6.25 / 0.50
assert abs(cm.cost(u, p) - (5.00 + 0.50 + 6.25 + 25.00)) < 1e-9
def test_caching_is_cheaper_than_not_caching():
for name in ("lite", "full", "oversized"):
t = _tier(name)
p = cm.PRICES["claude-opus-5"]
assert cm.cost(cm.tier_usage(t, FACTORY, True), p) < \
cm.cost(cm.tier_usage(t, FACTORY, False), p), name
def test_cost_rises_monotonically_with_tier():
p = cm.PRICES["claude-sonnet-5"]
costs = [cm.cost(cm.tier_usage(_tier(n), FACTORY), p)
for n in ("trivial", "lite", "full", "oversized")]
assert costs == sorted(costs)
# ---------------------------------------------------------------------------
# blended + CLI
# ---------------------------------------------------------------------------
def test_blended_sits_between_the_cheapest_and_priciest_tier():
p = cm.PRICES["claude-opus-5"]
blended = cm.blended_cost(cm.DEFAULT_TIERS, FACTORY, p, True)
per_tier = [cm.cost(cm.tier_usage(t, FACTORY), p) for t in cm.DEFAULT_TIERS]
assert min(per_tier) < blended < max(per_tier)
def test_shares_that_do_not_sum_to_one_are_normalised():
p = cm.PRICES["claude-opus-5"]
tiers = [cm.Tier(t.name, t.diff_tokens, t.steps, t.file_reads,
t.tokens_per_read, t.output_tokens, t.subagents, share=t.share * 2)
for t in cm.DEFAULT_TIERS]
doubled = cm.blended_cost(tiers, FACTORY, p, True)
normal = cm.blended_cost(cm.DEFAULT_TIERS, FACTORY, p, True)
assert abs(doubled - normal) < 1e-9
def test_report_renders_every_requested_model():
text = cm.report(cm.DEFAULT_TIERS, 350, True, ["claude-opus-5", "gpt-5.6-luna"])
assert "Claude Opus 5" in text
assert "GPT-5.6 Luna" in text
assert "Claude Sonnet 5" not in text
assert "350 PRs/month" in text
def test_main_rejects_unknown_model(capsys):
try:
cm.main(["--models", "gpt-9"])
except SystemExit as e:
assert e.code != 0
else:
raise AssertionError("expected SystemExit")
def test_main_rejects_bad_mix():
try:
cm.main(["--mix", "50,50"])
except SystemExit as e:
assert e.code != 0
else:
raise AssertionError("expected SystemExit")
def test_main_runs(capsys):
assert cm.main(["--models", "claude-sonnet-5", "--prs-per-month", "10"]) == 0
assert "per month" in capsys.readouterr().out
# ---------------------------------------------------------------------------
# observed runs — the calibration anchor
# ---------------------------------------------------------------------------
def test_observed_runs_are_well_formed():
assert cm.OBSERVED_RUNS, "the model is a guess without at least one measurement"
for run in cm.OBSERVED_RUNS:
for key in ("label", "date", "tier", "steps", "input", "output",
"cache_read", "cache_write"):
assert key in run, f"{run.get('label')} missing {key}"
assert run["input"] > 0 and run["output"] > 0
assert run["tier"] in {t.name for t in cm.DEFAULT_TIERS}
def test_observed_usage_splits_cached_from_uncached():
run = {"input": 1000, "output": 100, "cache_read": 400, "cache_write": 50}
u = cm.observed_usage(run)
assert u.cached_input == 400
assert u.uncached_input == 600
assert u.cache_writes == 50
assert u.total_input == 1000
def test_observed_report_prices_every_model():
text = cm.observed_report(["claude-opus-5", "gpt-5.6-luna"])
assert "Claude Opus 5" in text
assert "GPT-5.6 Luna" in text
# Labels are generic (no internal repo names) for commercialization.
assert "gitea_admin" not in text
assert "internal/hardening-PR" in text
def test_model_is_within_an_order_of_magnitude_of_the_measurement():
# The first measurement corrected the tier assumptions by ~15x. This guards
# against drifting that far out again: predict the observed run's tier at
# its actual diff size and step count, and compare to what was measured.
run = cm.OBSERVED_RUNS[0]
base = _tier(run["tier"])
modelled = cm.Tier(
base.name, run["diff_tokens"], run["steps"], base.file_reads,
base.tokens_per_read, run["output"], run["subagents"],
)
predicted = cm.tier_usage(modelled, FACTORY, caching=False).total_input
measured = run["input"]
assert 0.4 < predicted / measured < 2.5, (predicted, measured)
# ---------------------------------------------------------------------------
# PRICES — the multi-provider table (GPT / Gemini / Grok)
# ---------------------------------------------------------------------------
NEW_KEYS = ("gpt-5", "gpt-5-mini", "gemini-2.5-pro",
"gemini-2.5-flash", "grok-4.5", "grok-4.3")
def test_prices_contains_new_providers():
for k in NEW_KEYS:
assert k in cm.PRICES, k
def test_cost_matches_published_gpt5():
# $1.25 in / $10.00 out / cached $0.125; cache_write = input
u = cm.Usage(uncached_input=1_000_000, cached_input=1_000_000,
cache_writes=1_000_000, output=1_000_000)
assert abs(cm.cost(u, cm.PRICES["gpt-5"]) - (1.25 + 0.125 + 1.25 + 10.00)) < 1e-9
def test_cost_matches_published_gemini_flash():
# $0.30 in / $2.50 out / cached $0.03; cache_write = input
u = cm.Usage(uncached_input=2_000_000, cached_input=0,
cache_writes=0, output=500_000)
expected = 2.00 * 0.30 + 0.50 * 2.50 # $0.60 + $1.25
assert abs(cm.cost(u, cm.PRICES["gemini-2.5-flash"]) - expected) < 1e-9
def test_cost_matches_published_grok45():
# $2.00 in / $6.00 out / cached $0.30; cache_write = input
u = cm.Usage(uncached_input=1_000_000, cached_input=1_000_000,
cache_writes=1_000_000, output=1_000_000)
assert abs(cm.cost(u, cm.PRICES["grok-4.5"]) - (2.00 + 0.30 + 2.00 + 6.00)) < 1e-9
@@ -0,0 +1,326 @@
"""Unit tests for Langfuse trace emission. No network.
`_post` is monkeypatched everywhere a POST would happen; a test that reaches
the real network is a bug in the test, not a slow test.
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, "..", "..", ".."))
sys.path.insert(0, os.path.join(ROOT, "pilot"))
import langfuse_trace as lt # noqa: E402
USAGE = {
"input": 2_000_000,
"output": 17_000,
"reasoning": 500,
"cache_read": 400_000,
"cache_write": 50_000,
"total": 2_017_000,
"cost": 0.0,
"steps": 28,
"duration_s": 348.3,
}
BASE = dict(
repo="techspark/pragent",
index="42",
sha="2613b3e1122334455",
title="Harden the review path",
usage=USAGE,
findings=[
{"severity": "critical", "path": "a.py"},
{"severity": "minor", "path": "b.py"},
{"severity": "minor", "path": "c.py"},
],
summary="Three findings.",
)
# ---------------------------------------------------------------------------
# model -> environment split (the whole point of the integration)
# ---------------------------------------------------------------------------
def test_claude_models_land_in_the_claude_environment():
assert lt.resolve_environment("headroom/claude-sonnet-5") == "claude"
assert lt.resolve_environment("claude-opus-5") == "claude"
def test_everything_else_lands_in_the_ollama_environment():
for m in (
"headroom/glm-5.2:cloud",
"headroom/MiniMax-M2.7",
"vllm-qwen38/qwen3.8-27b",
"gpt-5",
):
assert lt.resolve_environment(m) == "ollama", m
def test_provider_and_bare_model_are_split_on_the_first_slash_only():
assert lt.provider_of("vllm-qwen38/qwen3.8-27b") == "vllm-qwen38"
assert lt.strip_provider("headroom/glm-5.2:cloud") == "glm-5.2:cloud"
# A bare name has no provider prefix; default to the pilot's proxy.
assert lt.provider_of("glm-5.2:cloud") == "headroom"
assert lt.strip_provider("glm-5.2:cloud") == "glm-5.2:cloud"
# ---------------------------------------------------------------------------
# usage accounting
# ---------------------------------------------------------------------------
def test_cache_reads_are_subtracted_from_input_not_added():
# Langfuse sums usageDetails keys; opencode reports cache_read *inside*
# input, so reporting both raw would bill the prefix twice.
d = lt._usage_details(USAGE)
assert d["input"] == 2_000_000 - 400_000
assert d["cache_read_input_tokens"] == 400_000
assert d["cache_write_input_tokens"] == 50_000
assert d["output"] == 17_000
assert d["reasoning"] == 500
def test_zero_cache_fields_are_omitted_rather_than_sent_as_zero():
d = lt._usage_details({"input": 100, "output": 10})
assert d == {"input": 100, "output": 10}
def test_a_paid_model_is_priced_as_itself():
costs, basis = lt._cost_details(USAGE, "headroom/claude-sonnet-5")
assert costs["total"] > 0
assert basis == "actual"
def test_minimax_is_priced_against_the_comparison_target_not_zero():
# MiniMax-M2.7 is the model the webhook actually runs and it is absent from
# PRICES; charting it at $0 would make the whole dashboard a flat line.
costs, basis = lt._cost_details(USAGE, "headroom/MiniMax-M2.7")
assert costs["total"] > 0
assert basis == "equivalent:claude-sonnet-5"
def test_glm_is_priced_against_the_comparison_target():
costs, basis = lt._cost_details(USAGE, "headroom/glm-5.2:cloud")
assert costs["total"] > 0
assert basis.startswith("equivalent:")
def test_an_all_zero_price_entry_counts_as_free_not_as_priced():
# The self-hosted vLLM qwen IS in PRICES, at 0.00 across the board.
costs, basis = lt._cost_details(USAGE, "vllm-qwen38/qwen3.8-27b")
assert costs["total"] > 0
assert basis.startswith("equivalent:")
def test_explicit_price_target_wins_over_the_default():
costs, basis = lt._cost_details(USAGE, "headroom/MiniMax-M2.7", "claude-opus-5")
assert basis == "equivalent:claude-opus-5"
sonnet, _ = lt._cost_details(USAGE, "headroom/MiniMax-M2.7", "claude-sonnet-5")
assert costs["total"] > sonnet["total"]
def test_env_overrides_the_default_target(monkeypatch):
monkeypatch.setenv("PRAGENT_PRICE_TARGET", "claude-haiku-4-5")
assert lt.resolve_price_target() == "claude-haiku-4-5"
# An explicit argument still beats the env.
assert lt.resolve_price_target("gpt-5") == "gpt-5"
def test_unknown_comparison_target_yields_no_cost_block_rather_than_a_wrong_one():
costs, basis = lt._cost_details(USAGE, "headroom/MiniMax-M2.7", "not-a-real-model")
assert costs == {}
assert basis == ""
# ---------------------------------------------------------------------------
# batch shape
# ---------------------------------------------------------------------------
def test_batch_has_a_trace_and_a_generation_linked_by_trace_id():
batch = lt.build_batch(model="headroom/claude-sonnet-5", **BASE)
types = [e["type"] for e in batch]
# Scores ride in the same batch; the trace and generation lead it.
assert types[:2] == ["trace-create", "generation-create"]
trace, gen = batch[0], batch[1]
assert gen["body"]["traceId"] == trace["body"]["id"]
assert trace["body"]["environment"] == gen["body"]["environment"] == "claude"
def test_batch_without_usage_has_no_generation():
batch = lt.build_batch(model="headroom/glm-5.2:cloud", **{**BASE, "usage": None})
types = [e["type"] for e in batch]
assert "generation-create" not in types
assert types[0] == "trace-create"
def test_trace_carries_repo_pr_session_and_severity_counts():
batch = lt.build_batch(model="headroom/glm-5.2:cloud", **BASE)
body = batch[0]["body"]
assert body["sessionId"] == "techspark/pragent#42"
assert body["metadata"]["severities"] == {"critical": 1, "minor": 2}
assert body["metadata"]["findings"] == 3
assert "provider:headroom" in body["tags"]
assert "model:glm-5.2:cloud" in body["tags"]
def test_lens_names_become_tags():
batch = lt.build_batch(
model="headroom/glm-5.2:cloud", lenses=["security", "tests"], **BASE
)
assert "lens:security" in batch[0]["body"]["tags"]
assert "lens:tests" in batch[0]["body"]["tags"]
def test_cost_basis_is_tagged_so_equivalent_is_never_read_as_spend():
batch = lt.build_batch(model="headroom/MiniMax-M2.7", **BASE)
trace = batch[0]["body"]
assert "cost:equivalent:claude-sonnet-5" in trace["tags"]
assert trace["metadata"]["cost_basis"] == "equivalent:claude-sonnet-5"
paid = lt.build_batch(model="headroom/claude-sonnet-5", **BASE)
assert "cost:actual" in paid[0]["body"]["tags"]
def test_minimax_generation_carries_a_nonzero_cost():
batch = lt.build_batch(model="headroom/MiniMax-M2.7", **BASE)
assert batch[1]["body"]["costDetails"]["total"] > 0
def test_batch_is_json_serializable():
batch = lt.build_batch(model="headroom/claude-sonnet-5", **BASE)
json.dumps({"batch": batch})
# ---------------------------------------------------------------------------
# emit_review_trace — config gate and fail-open
# ---------------------------------------------------------------------------
def _configure(monkeypatch):
monkeypatch.setenv("LANGFUSE_HOST", "http://langfuse.test:3000/")
monkeypatch.setenv("LANGFUSE_PUBLIC_KEY", "pk-lf-test")
monkeypatch.setenv("LANGFUSE_SECRET_KEY", "sk-lf-test")
def test_no_config_means_no_post_and_no_error(monkeypatch):
for k in ("LANGFUSE_HOST", "LANGFUSE_PUBLIC_KEY", "LANGFUSE_SECRET_KEY"):
monkeypatch.delenv(k, raising=False)
calls = []
monkeypatch.setattr(lt, "_post", lambda *a, **k: calls.append(a) or 200)
assert lt.emit_review_trace(model="headroom/glm-5.2:cloud", **BASE) is False
assert calls == []
def test_configured_emit_posts_to_the_ingestion_endpoint(monkeypatch):
_configure(monkeypatch)
seen = {}
def fake_post(host, pk, sk, batch, timeout):
seen.update(host=host, pk=pk, sk=sk, batch=batch, timeout=timeout)
return 207
monkeypatch.setattr(lt, "_post", fake_post)
assert lt.emit_review_trace(model="headroom/claude-sonnet-5", **BASE) is True
# Trailing slash stripped so the path is not doubled.
assert seen["host"] == "http://langfuse.test:3000"
kinds = [e["type"] for e in seen["batch"]]
assert kinds[:2] == ["trace-create", "generation-create"]
assert "score-create" in kinds
def test_transport_failure_is_swallowed(monkeypatch):
_configure(monkeypatch)
def boom(*a, **k):
raise OSError("connection refused")
monkeypatch.setattr(lt, "_post", boom)
assert lt.emit_review_trace(model="headroom/glm-5.2:cloud", **BASE) is False
def test_non_success_status_reports_failure_without_raising(monkeypatch):
_configure(monkeypatch)
monkeypatch.setattr(lt, "_post", lambda *a, **k: 401)
assert lt.emit_review_trace(model="headroom/glm-5.2:cloud", **BASE) is False
# ---------------------------------------------------------------------------
# Scores folded into the review batch (added with eval_scores)
# ---------------------------------------------------------------------------
def _scores(events):
return {e["body"]["name"]: e["body"] for e in events if e["type"] == "score-create"}
def test_build_batch_appends_scores():
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t",
model="headroom/claude-sonnet-5",
usage={"input": 100, "output": 10},
findings=[{"severity": "high", "path": "a.py", "line": 1}],
)
names = set(_scores(events))
assert "finding_rate" in names
assert "severity_max" in names
def test_scores_attach_to_the_same_trace():
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t", model="m",
usage={"input": 1, "output": 1}, findings=[], trace_id="fixed-id",
)
for body in _scores(events).values():
assert body["traceId"] == "fixed-id"
def test_scores_inherit_the_trace_environment():
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t",
model="headroom/glm-5.2:cloud",
usage={"input": 1, "output": 1}, findings=[],
)
for body in _scores(events).values():
assert body["environment"] == "ollama"
def test_dropped_findings_scored_when_provided():
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t", model="m",
usage={"input": 1, "output": 1}, findings=[], dropped_count=3,
)
assert _scores(events)["dropped_findings"]["value"] == 3.0
def test_dropped_findings_absent_when_not_measured():
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t", model="m",
usage={"input": 1, "output": 1}, findings=[],
)
assert "dropped_findings" not in _scores(events)
def test_cost_score_carries_its_basis_in_the_comment():
# An equivalent-cost $/finding must never be read as money spent.
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t",
model="headroom/glm-5.2:cloud",
usage={"input": 1000, "output": 100}, findings=[{"severity": "low", "path": "a", "line": 1}],
)
cpf = _scores(events).get("cost_per_finding")
if cpf is not None: # only when cost_model could price the comparison target
assert "equivalent" in cpf["comment"]
def test_batch_without_usage_still_scores_findings():
# A run with no usage report still produced findings worth scoring.
events = lt.build_batch(
repo="o/r", index="1", sha="abc", title="t", model="m",
usage=None, findings=[{"severity": "critical", "path": "a", "line": 2}],
)
assert _scores(events)["severity_max"]["value"] == "critical"