compliance: flag-gate AI portfolio + cloud sync + Stripe; de-risk prompts; harden reviewer

Implements docs/read-markets-compliance-changes.md as flag-gated changes
(no deletions) so paused features stay in the tree for future re-enable.
All four flags default False so a fresh deploy is compliance-safe.

- New env flags: PORTFOLIO_AI_ENABLED, PORTFOLIO_SYNC_ENABLED,
  TICKER_UNIVERSE_AGGREGATE_ENABLED, SUBSCRIPTIONS_ENABLED.
- Gates: /api/analyze, /api/portfolio/sync*, /api/stripe/*, /pricing,
  ticker_universe writes, portfolio_analysis.analyse(). is_paid_active()
  returns True for any auth'd user when subscriptions are paused.
- Prompts (PROMPT_VERSION 10): universal _COMPLIANCE_RIDER prepended to
  every system prompt; watch list removed; price-target / close-above-below
  / trigger / forward-state-as-description rules added; SPECULATIVE
  pivoted to regime-only scenarios; daily + weekly digests tightened.
- Reviewer: deterministic regex/lexicon pre-check fail-closed under the
  Haiku call; portfolio rider gated by PORTFOLIO_AI_ENABLED; base prompt
  sharpened for forward-state and MAR forward-opinion patterns;
  ReviewerVerdict audit table; generate_with_review retry helper.
- Migration 0026: purge portfolio_sync + ticker_universe; create
  reviewer_verdicts.
- Copy: MAR cite fixed to Art 3(1)(35) + Art 20 + Del Reg 2016/958;
  portfolio reframed as browser-only viewer in disclaimer / privacy /
  terms / about / pricing / landing (en + it). TODO(legal) marker for
  lawyer sign-off on disclaimer.
- Tests: 13 lexicon + 6 reviewer compliance regressions; conftest enables
  all flags so existing 402 tests still cover their code paths.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-05-29 19:57:12 +02:00
parent ee8384f1ba
commit 47dce1a1a4
38 changed files with 1188 additions and 279 deletions

View file

@ -0,0 +1,78 @@
"""Tests for the deterministic regex/lexicon pre-check.
These are pure-function tests for ``app.services.output_review_lexicon.check``
no LLM, no DB, no fixtures. They protect both the obvious-rejects and
the false-positive guards (bare 'should', 'cut', 'hold' must not match in
benign contexts)."""
from __future__ import annotations
import pytest
from app.services.output_review_lexicon import check
# ---------------------------------------------------------------------------
# Obvious rejects — should hit and identify the firing rule
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("text,expected_rule", [
# First-match-wins ordering: action_phrase runs before advice_phrase, so
# a sentence with both ("you should buy") fires action_phrase. That's
# fine — the goal is to catch a violation, not to attribute the rule.
("You should buy the dip.", "action_phrase"),
("Investors should consider buying defensives.", "advice_phrase"),
("We recommend trimming this position.", "advice_phrase"),
("That stock is a buy at these levels.", "action_phrase"),
("Take profit on the position.", "action_phrase"),
("Trim your exposure to growth.", "action_phrase"),
("Rotate into defensives.", "action_phrase"),
("Overweight the sector into Q1.", "action_phrase"),
# Forecast / level register
("Our price target sits at $95.", "forecast_phrase"),
("Target of $93 looks reasonable.", "forecast_phrase"),
("There is support at $4,200.", "forecast_phrase"),
("Resistance near $570 is the level to watch.", "forecast_phrase"),
# Composed patterns
("Brent close above $93 would confirm.", "level_trigger"),
("If SOXX breaks below 570 the bid fades.", "level_trigger"),
("Watch for a move above 4,600.", "level_trigger"),
("Floor at $90 looks intact.", "forecast_phrase"),
# "X as a floor / ceiling" phrasing — not currently a hard rule in the
# lexicon (LLM layer catches it); see future-tightening note in the
# module docstring.
])
def test_lexicon_catches_obvious_violations(text, expected_rule):
hit = check(text)
assert hit is not None, f"should have flagged: {text!r}"
assert hit.rule == expected_rule, (
f"expected rule {expected_rule!r}, got {hit.rule!r} for {text!r}"
)
assert hit.snippet, "snippet should never be empty"
# ---------------------------------------------------------------------------
# False-positive guards from the brief — must NOT match
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("text", [
# The brief specifically calls these out as false-positive risks for
# bare-word matchers.
"Saudi price cuts pushed energy lower this week.",
"The cargo hold story dominated the tape.",
"OPEC may hold output steady at the next meeting.",
# State-level commentary the LLM layer should judge, not the lexicon.
"Valuations are stretched after the rally.",
"Real yields are restrictive across the curve.",
"Positioning is crowded in megacap tech.",
# Plain factual price citation — no trigger framing.
"Brent is trading at $90, down 12% YTD.",
"Gold is at $4,600 after a sharp move higher.",
# Empty / whitespace
"",
" ",
])
def test_lexicon_lets_clean_text_through(text):
hit = check(text)
assert hit is None, f"lexicon false-positive on: {text!r} (rule={hit and hit.rule})"