read.markets/tests
Giorgio Gilestro f9534f7ad6 review: gate strategic-log, portfolio, chat, and digest on reviewer
Extends the reviewer agent — previously only protecting indicator
summaries — to every AI-generated surface that reaches a user. The
reviewer's prompt already rejects scratchpad, truncation,
meta-commentary, and (since a6e476b) financial advice; wiring it in
turns those rules from prompt-level "asks" into structural gates.

Four call sites updated:

- ai_log_job.run() : after each tone/analysis variant is generated,
  pass through review_read. On reject, log the reason and skip the
  StrategicLog insert; the API's existing "latest StrategicLog" lookup
  falls back to the previous clean log.

- services/portfolio_analysis.analyse() : on reject, raise a clean
  RuntimeError that the /api/analyze router already maps to HTTP 502
  with a retry-able message. Portfolio analysis isn't cached server-
  side, so the user retries; the reviewer's verdict reason goes into
  the AICall ledger as the leaked-status row's error column.

- routers/chat.chat() : on reject, instead of returning the raw
  assistant content we return a short refusal explaining the limit
  and inviting a rephrase. Adds ~1-2 s of latency per turn (one extra
  LLM call to Haiku) — the only user-facing latency tax.

- jobs/email_digest_job._generate_variants() : on reject, the variant
  is dropped for the cycle. Recipients on the rejected tone get no
  digest email this run, which is better than delivering inbox copy
  that drifts into advice (emails are unrecallable once sent).

In every case the AICall ledger row records the reviewer cost so
month_spend stays accurate across all paths.

The reviewer system prompt is slightly generalised to cover both the
indicator-summary case and the longer-form log/digest/chat case:
- removes "short interpretive read" framing
- softens the "any question" rule so genuine rhetorical structure in
  a long-form log doesn't trigger a reject

tests/conftest.py grows an autouse fixture that stubs review_read to
clean=True in every consumer module. Tests that mock the generator
shouldn't have to also mock the safety gate behind it; tests that
specifically want the reject branch can override with their own
monkeypatch. test_output_review.py is unaffected — it imports
review_read directly.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 14:40:04 +02:00
..
fixtures tests: add fabricated IBKR fixture for LLM parser 2026-05-27 12:06:47 +02:00
conftest.py review: gate strategic-log, portfolio, chat, and digest on reviewer 2026-05-29 14:40:04 +02:00
test_access.py phase D milestones 1+2: referral system + paid-access gate 2026-05-21 23:25:35 +01:00
test_api_helpers.py initial commit — cassandra v0.1 2026-05-15 21:56:10 +01:00
test_auth_session.py tests: backfill coverage for openrouter transport, auth sessions, cadence 2026-05-28 13:58:28 +02:00
test_branding_consistency.py css: split cassandra.css into per-section files 2026-05-28 12:31:29 +02:00
test_cadence_policy.py tests: backfill coverage for openrouter transport, auth sessions, cadence 2026-05-28 13:58:28 +02:00
test_chat_and_log_gates.py routers: extract chat + ops from api.py 2026-05-27 21:43:17 +02:00
test_cli.py phase D milestones 1+2: referral system + paid-access gate 2026-05-21 23:25:35 +01:00
test_config_loading.py test: drop stale "pie" assertion from test_default_groups_present 2026-05-26 00:20:01 +02:00
test_csv_import.py sync: encrypted cloud backup for portfolios + settings UX rework 2026-05-23 16:15:54 +02:00
test_digest_prompts.py openrouter: split into llm_prompts (prompt engineering) + transport 2026-05-27 21:27:23 +02:00
test_email_digest_job.py test+fix: make the suite run cleanly in the test container 2026-05-26 00:11:18 +02:00
test_email_render.py email: split digest renderer to digest_email.py 2026-05-27 21:33:06 +02:00
test_email_service.py brand: rename product to "Read the Markets" (read.markets) 2026-05-22 19:39:38 +01:00
test_email_unsubscribe.py email: tighten unsubscribe — test isolation, accurate comments, tighter assertion 2026-05-25 23:10:29 +02:00
test_glossary.py phase G: data minimisation + passwordless auth + DeepSeek-first LLM 2026-05-18 14:16:57 +01:00
test_i18n.py cleanup: drop redundant @pytest.mark.asyncio + fix log_id type 2026-05-27 19:32:38 +02:00
test_instrument_map.py phase B (1/4): CSV parser + InstrumentMap (T212 shortcode → Yahoo ticker) 2026-05-16 10:53:08 +01:00
test_llm_csv_parser.py models: align translation column naming + add token counts 2026-05-27 21:18:29 +02:00
test_localization_integration.py models: align translation column naming + add token counts 2026-05-27 21:18:29 +02:00
test_market_parsing.py initial commit — cassandra v0.1 2026-05-15 21:56:10 +01:00
test_news_parsing.py initial commit — cassandra v0.1 2026-05-15 21:56:10 +01:00
test_news_tagging.py news: auto-tag headlines + market-aware cadence + filter UI 2026-05-21 23:25:03 +01:00
test_news_window.py test+fix: make the suite run cleanly in the test container 2026-05-26 00:11:18 +02:00
test_openrouter_prompt.py openrouter: split into llm_prompts (prompt engineering) + transport 2026-05-27 21:27:23 +02:00
test_openrouter_transport.py llm: support JSON-mode + stop publishing the reasoning field 2026-05-29 13:02:36 +02:00
test_otp_service.py phase G: data minimisation + passwordless auth + DeepSeek-first LLM 2026-05-18 14:16:57 +01:00
test_output_review.py review: strip markdown code-fences from JSON verdicts 2026-05-29 13:27:37 +02:00
test_pending_cookie.py phase D milestones 1+2: referral system + paid-access gate 2026-05-21 23:25:35 +01:00
test_polar_webhook.py polar: build /api/polar/webhook handler 2026-05-26 17:42:41 +02:00
test_portfolio_analysis.py phase G: data minimisation + passwordless auth + DeepSeek-first LLM 2026-05-18 14:16:57 +01:00
test_portfolio_sync_api.py sync: detect orphaned blobs (pepper rotation) + fix AESGCM arg order 2026-05-25 12:49:11 +02:00
test_portfolio_sync_service.py sync: encrypted cloud backup for portfolios + settings UX rework 2026-05-23 16:15:54 +02:00
test_referral.py phase D milestones 1+2: referral system + paid-access gate 2026-05-21 23:25:35 +01:00
test_referral_conversion.py tests: extract _build_session_factory to a shared conftest fixture 2026-05-27 20:50:09 +02:00
test_settings_digest_api.py settings: digest opt-in + tone (PATCH /api/settings/digest + UI) 2026-05-25 23:23:03 +02:00
test_stripe_billing.py stripe: detect buyer currency at checkout (GBP/USD/EUR) 2026-05-28 12:42:40 +02:00
test_ticker_validate.py tests: extract _build_session_factory to a shared conftest fixture 2026-05-27 20:50:09 +02:00
test_universe_unlinkability.py phase G: data minimisation + passwordless auth + DeepSeek-first LLM 2026-05-18 14:16:57 +01:00
test_verify_subscribe.py ui: collapsible settings sections + welcome-email + larger auth inputs 2026-05-26 22:32:59 +02:00