Files
momentum-system/tests/unit/test_recap_llm_pool.py
T
momentum devandClaude Opus 5.5 91af4c2e93 feat: nightly Ross-recap learning loop on the owner's local Ollama
Why: the pilot in data/ross showed that Ross Cameron's daily recaps, joined to
the SIP tape and to what our bot saw, answer "which of his trades could we have
taken and why didn't we". It was a set of one-off scripts run by hand; the
server-side youtube_claims pipeline has been dead since ~08-24 (YouTube blocks
the TrueNAS IP) and its captions fallback hid that. This makes the pilot a
nightly, local, cumulative loop without a paid LLM API.

youtube_claims/recap/ (python -m youtube_claims.recap.nightly):
- channel: yt-dlp flat listing of @DaytradeWarrior, m4a audio, faster-whisper
  small.en on CPU, pilot-format bundles (meta + our scanner list + transcript).
- extract: Ollama structured output (JSON schema) per EXTRACT_SPEC, chunked
  with overlap for long transcripts and merged. The verbatim rule is enforced in
  code: every quote must be found (token-level, one-word tolerance) and is
  snapped to the transcript's words; unquoted items, trades with no surviving
  quote, prices never spoken, and passed-on names never said are dropped.
- llm: endpoint pool over RECAP_OLLAMA_HOSTS (round-robin + failover, per-host
  model/ctx/think pins) since the owner runs several Ollama boxes.
- tape/join/ourside: ported join.py/ourside.py -- tickers are price-verified on
  the Alpaca SIP tape (plus an order-free check for his own named ticker), then
  paper (:5433) and live (:5434) event views are attached. Bars cached on disk.
- store + schema.sql: idempotent recap_* tables in the transcripts schema,
  upserted per video_id. report: the pilot's sections + "what changed since last
  run" against the previous day's metrics JSON.
- evaluate: scores any model/prompt against the 36 gold extractions.

Model choice (36 gold videos, final code path, P/R):
  gpt-oss:20b on the workstation GPU, two focused calls, think=low:
    trades P .84 R .70; tickers P .82 R .77; entry +-2% P .54 R .50;
    passed-on P .66 R .76; temperature 16/31; kind 30/36; ~49 s/video.
  tools:latest (4B, server) one call: trades P .81 R .72, tickers P .83 R .85,
    passed-on P .80 R .75, temperature 20/31, but it invents prices
    (entry P .42) and occasionally runs away (5-9 min). Prices drive the tape
    verification, so the more precise local model is the default.
  Iterations: prompt v2 + spoken-price rescale (699 -> 6.99) + split calls took
  gpt-oss trades F1 .74 -> .76-.78 and tickers .78 -> .79-.80; think=medium
  blew the token budget. qwen3-30b-a3b on mason only finished 5 videos (host
  saturated) -- inconclusive.

Also: youtube_claims/transcription.py::_captions_fallback now picks the
youtube-transcript-api shape by feature detection instead of retrying the
removed get_transcript on any error, and transcribe() reports both the whisper
and captions causes -- the IpBlocked error was being masked as AttributeError.

No trading code touched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 16:23:18 -05:00

66 lines
2.5 KiB
Python

"""Round-robin + failover across the owner's Ollama endpoints (no network: fake transport)."""
import urllib.error
import pytest
from youtube_claims.recap.llm import OllamaPool, parse_hosts
def test_parse_hosts_with_per_host_model_and_options():
eps = parse_hosts(" http://a:11434/ , http://b:11434=qwen3:30b#ctx=8192#think=false,, ")
assert [e.host for e in eps] == ["http://a:11434", "http://b:11434"]
assert eps[0].model is None and eps[0].think == "default"
assert eps[1].model == "qwen3:30b" and eps[1].num_ctx == 8192 and eps[1].think is False
def _pool(spec, models, fail=(), default="m1"):
calls = []
def call(system, user, *, model, host, num_ctx, think, **kw):
calls.append((host, model, num_ctx, think))
if host in fail:
raise urllib.error.URLError("connection refused")
return "{}", {"seconds": 1}
pool = OllamaPool(parse_hosts(spec), default, default_ctx=24576, default_think="low", call=call,
list_models=lambda h: models[h])
return pool, calls
def test_round_robin_over_hosts_that_have_the_model():
pool, calls = _pool("http://a,http://b,http://c", {"http://a": {"m1"}, "http://b": {"other"},
"http://c": {"m1:latest"}})
for _ in range(4):
pool.chat("s", "u")
assert [c[0] for c in calls] == ["http://a", "http://c", "http://c", "http://a"]
def test_failover_parks_a_dead_host():
pool, calls = _pool("http://a,http://b", {"http://a": {"m1"}, "http://b": {"m1"}}, fail={"http://a"})
_, st = pool.chat("s", "u")
assert st["host"] == "http://b"
pool.chat("s", "u")
assert [c[0] for c in calls] == ["http://a", "http://b", "http://b"] # a parked after failing
def test_per_host_model_and_options_are_used():
pool, calls = _pool("http://a=qwen#ctx=8192#think=false", {"http://a": {"qwen"}})
_, st = pool.chat("s", "u")
assert calls == [("http://a", "qwen", 8192, False)] and st["model"] == "qwen"
def test_no_endpoint_with_the_model_is_an_error():
pool, _ = _pool("http://a", {"http://a": {"x"}})
with pytest.raises(RuntimeError, match="no reachable Ollama endpoint"):
pool.chat("s", "u")
def test_client_errors_are_not_retried_elsewhere():
def call(*a, **kw):
raise RuntimeError("ollama 400: invalid think value")
pool = OllamaPool(parse_hosts("http://a,http://b"), "m1", call=call, list_models=lambda h: {"m1"})
with pytest.raises(RuntimeError, match="400"):
pool.chat("s", "u")