researcher.get_source_urls() returns gpt_researcher's visited_urls, which
its own skills/researcher.py._extract_content() populates straight from
each retriever result's result["href"] with zero scheme/host validation.
Every other URL touchpoint in this app is validated (ssrf_guard.py for the
input url, each redirect hop re-validated, the response-size cap) -- this
output path never was.
Confirmed live against this app's own real index.html template (not a
mock): handing it source_urls=["javascript:alert(1)"] renders
<a href="javascript:alert(1)" ...> verbatim. Jinja's autoescaping blocks
HTML-markup injection but does nothing for a dangerous scheme inside an
attribute value. This is worse than a one-shot API response: /b/{id}
permalinks persist and publicly share this exact rendering forever, to
anyone with the link, not just the original caller.
Fixed at the single choke point (brief.py, right where source_urls first
enters Wedgecheck's own code) with _filter_safe_source_urls(): keep only
http(s) entries, drop anything else with a logged warning. A "source" that
isn't http(s) was never a real citeable source anyway, so this is also
more correct, not just safer -- and if filtering empties the list, it
falls through the existing, already-correct 0-sources retry/fail-loud path
rather than silently serving a brief with a source quietly removed.
Verified: re-rendered the real index.html template after filtering --
javascript: payload dropped, real https source kept. Confirmed the filter
does not touch legitimate output by running it against real DuckDuckGo
result hrefs returned live by ddgs right now (weweb.io, supabase.com,
encore.dev) -- all three pass through unchanged. Ran the real app end to
end against the real production LLM relay + real duckduckgo retriever
twice more (not mocks) to confirm the existing 0-sources fail-loud path is
unaffected by this change (both hit the pre-existing, documented DDG
low-recall failure mode on content, same as before this fix, not a
regression).
222 lines
10 KiB
Python
222 lines
10 KiB
Python
"""Core Wedgecheck flow: product input -> sourced market brief.
|
|
|
|
The research/citation step is entirely gpt-researcher (Apache-2.0,
|
|
https://github.com/assafelovic/gpt-researcher). This file only builds the
|
|
query we hand it and reshapes its output into MarketBrief. No fact in the
|
|
output is written by Wedgecheck's own code -- every claim comes from
|
|
gpt-researcher's web-grounded report, cited to real source URLs.
|
|
|
|
gpt-researcher fires several sub-queries concurrently against the retriever
|
|
(see gpt_researcher/skills/researcher.py's asyncio.gather fan-out). The free
|
|
"duckduckgo" retriever (ddgs) has no retry of its own and silently swallows
|
|
any error into an empty result list (see
|
|
gpt_researcher/retrievers/duckduckgo/duckduckgo.py), so zero sources can come
|
|
back for the *whole* report with no exception raised anywhere. Left
|
|
unchecked, that produces a confident-looking but entirely unsourced report --
|
|
the exact failure this product exists to prevent. generate_market_brief()
|
|
below retries the full research pass (a fresh GPTResearcher -> fresh
|
|
retriever session) with a backoff on an empty source list, and raises
|
|
WedgecheckResearchError instead of returning an unsourced MarketBrief if
|
|
every attempt still comes back empty.
|
|
|
|
Retrying does NOT reliably fix this, and the error message below must not
|
|
claim otherwise. An earlier version of this module (and this comment)
|
|
assumed the cause was "a transient rate limit," written before
|
|
retriever_compat.py's real root-cause fix (the ddgs `region="wt-wt"` DNS bug,
|
|
which *was* a 100%-reproducible hard failure, now patched out). Once that bug
|
|
was fixed, "0 sources after N attempts" kept happening anyway on certain
|
|
phrasings -- confirmed live, twice, in two separate sessions, against the
|
|
identical description ("a simple habit tracker app for iOS") returning 0
|
|
sources on every one of 3 attempts both times. A rate limit would not fail
|
|
the exact same query deterministically across separate sessions; this is the
|
|
free DuckDuckGo HTML retriever's own low recall on some phrasings, which
|
|
retrying the identical query does not fix. Rephrasing the query or setting
|
|
RETRIEVER=tavily are the two things that actually help.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from urllib.parse import urlparse
|
|
|
|
from gpt_researcher import GPTResearcher
|
|
|
|
from .config import WedgecheckConfig
|
|
from .errors import WedgecheckResearchError
|
|
from .product_input import build_product_input
|
|
from .schema import MarketBrief
|
|
|
|
logger = logging.getLogger("wedgecheck.brief")
|
|
|
|
_SAFE_SOURCE_SCHEMES = {"http", "https"}
|
|
|
|
|
|
def _filter_safe_source_urls(source_urls: list[str]) -> list[str]:
|
|
"""Drop any "source URL" that isn't http(s) before it ever reaches
|
|
Wedgecheck's own code paths (the API response, sqlite, and -- the
|
|
concrete, reproduced-live risk -- `index.html`/`history.html`
|
|
rendering it straight into `href="{{ s }}"`).
|
|
|
|
`researcher.get_source_urls()` returns `self.visited_urls`, which
|
|
gpt_researcher's own `_extract_content()` (skills/researcher.py)
|
|
populates from `result["href"]` for every retriever result with zero
|
|
scheme or host validation -- the free `duckduckgo` retriever is the
|
|
default here, but nothing upstream guarantees every entry is a real
|
|
`http(s)` link. Every *other* URL touchpoint in this app is validated
|
|
(`ssrf_guard.py` for the input `url`, each redirect hop re-validated,
|
|
the response-size cap) except this one: the *output* source list was
|
|
rendered completely unvalidated.
|
|
|
|
Confirmed live against this app's own real `index.html` template
|
|
(not a mock): handing it `source_urls=["javascript:alert(1)"]`
|
|
renders `<a href="javascript:alert(1)" ...>` verbatim -- Jinja's
|
|
autoescaping blocks HTML-markup injection but does nothing for a
|
|
dangerous *scheme* inside an attribute value, so a single
|
|
non-http(s) entry becomes a real clickable XSS payload. This is
|
|
worse than a one-shot API response: `/b/{id}` permalinks (added this
|
|
product's earlier session) persist and publicly share this exact
|
|
rendering forever, to anyone with the link, not just the original
|
|
caller.
|
|
|
|
A "source" that isn't http(s) was never a real citeable source
|
|
anyway -- dropping it (not sanitizing/escaping it for display) keeps
|
|
this consistent with the product's own "every claim is cited to a
|
|
real source" promise, and lets a result that filters down to zero
|
|
sources fall through the existing, already-correct 0-sources
|
|
retry/fail-loud path below rather than silently serving a brief with
|
|
a source quietly removed."""
|
|
safe = []
|
|
for url in source_urls:
|
|
try:
|
|
scheme = urlparse(url).scheme.lower()
|
|
except ValueError:
|
|
continue
|
|
if scheme in _SAFE_SOURCE_SCHEMES:
|
|
safe.append(url)
|
|
else:
|
|
logger.warning(
|
|
"wedgecheck: dropped a non-http(s) source URL returned by "
|
|
"the retriever (scheme=%r) before it could reach any "
|
|
"render path: %r",
|
|
scheme,
|
|
url,
|
|
)
|
|
return safe
|
|
|
|
_QUERY_TEMPLATE = """\
|
|
Market and competitive brief for the following product/business:
|
|
|
|
{subject}
|
|
|
|
Research and report, with every factual claim grounded in a cited source:
|
|
1. Market signal: what real people search for or complain about in this
|
|
space (forums, review sites, search trends) -- evidence that the pain
|
|
point is real, not assumed.
|
|
2. Named, real competitors or alternatives, with their pricing if findable.
|
|
3. Common complaints/gaps in those competitors' own reviews or docs.
|
|
4. A specific, underserved positioning gap this product could own.
|
|
|
|
Do not invent competitors, numbers, or quotes. If a figure cannot be
|
|
sourced, say so explicitly instead of estimating silently.
|
|
"""
|
|
|
|
|
|
async def _run_one_research_attempt(
|
|
query: str,
|
|
) -> tuple[str, list[str], float]:
|
|
"""One full gpt-researcher pass. A fresh GPTResearcher per attempt means
|
|
a fresh retriever session, so a rate-limited DuckDuckGo burst on one
|
|
attempt doesn't carry over into the retry."""
|
|
researcher = GPTResearcher(query=query, report_type="research_report")
|
|
await researcher.conduct_research()
|
|
report_markdown = await researcher.write_report()
|
|
|
|
costs = researcher.get_costs()
|
|
cost_usd = float(costs) if isinstance(costs, (int, float)) else 0.0
|
|
source_urls = _filter_safe_source_urls(researcher.get_source_urls())
|
|
return report_markdown, source_urls, cost_usd
|
|
|
|
|
|
async def generate_market_brief(
|
|
*,
|
|
url: str | None,
|
|
description: str | None,
|
|
config: WedgecheckConfig | None = None,
|
|
) -> MarketBrief:
|
|
cfg = config or WedgecheckConfig.from_env()
|
|
cfg.apply_to_environ()
|
|
|
|
# build_product_input() does a synchronous `requests.get()` (see
|
|
# product_input.py) -- called directly from this async function it would
|
|
# block the entire single-worker event loop (and therefore every other
|
|
# concurrent request, including /healthz) for up to its own 15s timeout.
|
|
# Confirmed live: a URL pointed at a slow-responding page froze a
|
|
# concurrent /healthz call for ~7.6s. Run it in a thread instead.
|
|
product_input = await asyncio.to_thread(
|
|
build_product_input, url=url, description=description
|
|
)
|
|
query = _QUERY_TEMPLATE.format(subject=product_input.research_subject())
|
|
|
|
last_report_markdown = ""
|
|
total_cost_usd = 0.0
|
|
attempts = max(1, cfg.research_max_attempts)
|
|
|
|
for attempt in range(1, attempts + 1):
|
|
report_markdown, source_urls, cost_usd = await _run_one_research_attempt(query)
|
|
last_report_markdown = report_markdown
|
|
# Each attempt spins up a fresh GPTResearcher (see
|
|
# _run_one_research_attempt's own docstring), so get_costs() is
|
|
# per-attempt, not cumulative across retries -- it must be summed
|
|
# here. A real LLM call (query planning, and often report writing
|
|
# too) runs on every attempt regardless of whether that attempt
|
|
# found any sources, so a failed attempt is not free: a bare
|
|
# `cost_usd = cost_usd` (or tracking only the last/successful
|
|
# attempt) silently discards the real spend of every attempt before
|
|
# it, both in the success case (a brief that recovered on attempt 2
|
|
# or 3) and in the final-failure error message below (which claims
|
|
# "cost so far", not "cost of the last attempt").
|
|
total_cost_usd += cost_usd
|
|
|
|
if source_urls:
|
|
if attempt > 1:
|
|
logger.info(
|
|
"wedgecheck: research recovered %d source(s) on attempt %d/%d "
|
|
"(total cost across all attempts: $%.4f)",
|
|
len(source_urls),
|
|
attempt,
|
|
attempts,
|
|
total_cost_usd,
|
|
)
|
|
return MarketBrief(
|
|
input=product_input,
|
|
report_markdown=report_markdown,
|
|
source_urls=source_urls,
|
|
research_cost_usd=total_cost_usd,
|
|
)
|
|
|
|
logger.warning(
|
|
"wedgecheck: research attempt %d/%d returned 0 source URLs "
|
|
"(retriever=%s, cost so far $%.4f) -- usually the free "
|
|
"retriever's own low recall on this phrasing, not a rate limit; "
|
|
"retrying the identical query rarely recovers it",
|
|
attempt,
|
|
attempts,
|
|
cfg.retriever,
|
|
total_cost_usd,
|
|
)
|
|
if attempt < attempts:
|
|
await asyncio.sleep(cfg.research_retry_backoff_seconds * attempt)
|
|
|
|
raise WedgecheckResearchError(
|
|
f"gpt-researcher returned 0 source URLs after {attempts} attempt(s) "
|
|
f"using retriever={cfg.retriever!r}. Refusing to return an unsourced "
|
|
"report -- every Wedgecheck claim must be grounded in a real source. "
|
|
"This is usually the free retriever's own low recall on this exact "
|
|
"phrasing (not a rate limit -- retrying the same wording rarely "
|
|
"helps); try rephrasing the description, or set RETRIEVER to a "
|
|
"different gpt-researcher retriever (e.g. tavily, with "
|
|
"TAVILY_API_KEY set) for more reliable results. (Research cost so "
|
|
f"far: ${total_cost_usd:.4f}; last report was "
|
|
f"{len(last_report_markdown)} chars, discarded.)"
|
|
)
|