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
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python3
"""
Headless web research for Trask / Holocron.

stdin: JSON payload (query, allowed_url_prefixes, query_domains, source_urls, …)
stdout: JSON { report, research_information }

Discovery via DuckDuckGo; scrape via Crawl4AI (markdown); trafilatura fallback.
"""

from __future__ import annotations

import argparse
import asyncio
import contextlib
import json
import os
import re
import sys
from dataclasses import dataclass, field
from typing import Any
from urllib.parse import urlparse

MAX_CANDIDATE_URLS = 12
MAX_SCRAPE_URLS = 8
MAX_MARKDOWN_CHARS_PER_PAGE = 12_000
MIN_USABLE_BODY_CHARS = 280
SEARCH_RESULTS_PER_DOMAIN = 4

FORUM_CHROME_PATTERNS = [
    re.compile(r"\bsign up\b", re.I),
    re.compile(r"\ball activity\b", re.I),
    re.compile(r"\bmark site read\b", re.I),
    re.compile(r"\bactivity feed\b", re.I),
    re.compile(r"\bexisting user\? sign in\b", re.I),
    re.compile(r"\byour content feed\b", re.I),
]


def _normalize_prefix(value: str) -> str:
    return value.strip().rstrip("/")


def _url_allowed(url: str, prefixes: list[str]) -> bool:
    candidate = _normalize_prefix(url)
    for raw in prefixes:
        prefix = _normalize_prefix(raw)
        if not prefix:
            continue
        if candidate == prefix or candidate.startswith(prefix + "/"):
            return True
    return False


def _unique_urls(urls: list[str]) -> list[str]:
    seen: set[str] = set()
    out: list[str] = []
    for url in urls:
        u = url.strip()
        if not u or not u.startswith(("http://", "https://")):
            continue
        key = u.rstrip("/").lower()
        if key in seen:
            continue
        seen.add(key)
        out.append(u)
    return out


def _host_from_url(url: str) -> str:
    try:
        return urlparse(url).netloc.lower().replace("www.", "")
    except Exception:
        return ""


def _looks_like_forum_chrome(text: str) -> bool:
    if len(text) < 120:
        return True
    hits = sum(1 for pat in FORUM_CHROME_PATTERNS if pat.search(text))
    return hits >= 2


def _extract_follow_up_links(markdown: str, query: str, allowed_prefixes: list[str]) -> list[str]:
    tokens = _query_tokens(query)
    if not tokens:
        return []
    found: list[str] = []
    for _label, href in re.findall(r"\[([^\]]*)\]\((https?://[^)]+)\)", markdown):
        lower = f"{_label} {href}".lower()
        if not any(token in lower for token in tokens):
            continue
        if _url_allowed(href, allowed_prefixes):
            found.append(href)
    return _unique_urls(found)


def _truncate(text: str, limit: int) -> str:
    if len(text) <= limit:
        return text
    return text[: limit - 3].rstrip() + "..."


@dataclass
class PageEvidence:
    url: str
    markdown: str


@dataclass
class GatherResult:
    pages: list[PageEvidence] = field(default_factory=list)
    visited_urls: list[str] = field(default_factory=list)
    retrieved_urls: list[str] = field(default_factory=list)
    rejected_urls: list[str] = field(default_factory=list)
    candidate_urls: list[str] = field(default_factory=list)
    cache_stats: dict[str, int] = field(default_factory=dict)


def _query_tokens(query: str) -> set[str]:
    return {t for t in re.findall(r"[a-z0-9]{3,}", query.lower()) if t not in {"what", "where", "when", "does", "the", "for", "and", "how"}}


def _rank_source_urls(query: str, source_urls: list[str]) -> list[str]:
    tokens = _query_tokens(query)
    scored: list[tuple[int, str]] = []
    for url in source_urls:
        lower = url.lower()
        score = sum(2 for token in tokens if token in lower)
        if "technical" in lower or "reference" in lower or "neocities" in lower:
            score += 1
        scored.append((score, url))
    scored.sort(key=lambda pair: pair[0], reverse=True)
    return [url for _, url in scored]


def discover_urls(
    query: str,
    query_domains: list[str],
    source_urls: list[str],
    allowed_prefixes: list[str],
    cache_client: Any | None = None,
    cache_stats: dict[str, int] | None = None,
) -> list[str]:
    if cache_client is not None:
        try:
            from trask_cache import get_search, set_search

            cached = get_search(cache_client, query, query_domains)
            if cached is not None:
                if cache_stats is not None:
                    cache_stats["search_hits"] = cache_stats.get("search_hits", 0) + 1
                filtered = [u for u in cached if _url_allowed(u, allowed_prefixes)]
                return filtered[:MAX_CANDIDATE_URLS]
            if cache_stats is not None:
                cache_stats["search_misses"] = cache_stats.get("search_misses", 0) + 1
        except Exception:
            pass

    allowed_sources = [u for u in _unique_urls(source_urls) if _url_allowed(u, allowed_prefixes)]
    candidates: list[str] = _rank_source_urls(query, allowed_sources)

    domains = [d.strip() for d in query_domains if d.strip()]
    if not domains:
        domains = list({_host_from_url(p) for p in allowed_prefixes if _host_from_url(p)})

    try:
        from duckduckgo_search import DDGS

        with DDGS() as ddgs:
            for domain in domains[:6]:
                site_query = f"{query} site:{domain}"
                try:
                    for item in ddgs.text(site_query, max_results=SEARCH_RESULTS_PER_DOMAIN, backend="bing"):
                        href = (item.get("href") or item.get("url") or "").strip()
                        if href:
                            candidates.append(href)
                except Exception:
                    continue
            if len(candidates) < 3:
                try:
                    for item in ddgs.text(query, max_results=10, backend="bing"):
                        href = (item.get("href") or item.get("url") or "").strip()
                        if href:
                            candidates.append(href)
                except Exception:
                    pass
    except Exception:
        pass

    # DuckDuckGo may rate-limit; always keep ranked catalog homes as crawl seeds.
    for url in allowed_sources:
        if url not in candidates:
            candidates.append(url)

    filtered = [u for u in _unique_urls(candidates) if _url_allowed(u, allowed_prefixes)]
    result = filtered[:MAX_CANDIDATE_URLS]

    if cache_client is not None and result:
        try:
            from trask_cache import set_search

            set_search(cache_client, query, query_domains, result)
        except Exception:
            pass

    return result


def _trafilatura_fetch(url: str) -> str:
    try:
        import trafilatura

        downloaded = trafilatura.fetch_url(url)
        if not downloaded:
            return ""
        text = trafilatura.extract(downloaded, include_comments=False, include_tables=True)
        return (text or "").strip()
    except Exception:
        return ""


async def _crawl_with_shared_crawler(crawler: Any, url: str) -> str:
    try:
        from crawl4ai import CrawlerRunConfig, CacheMode

        run_config = CrawlerRunConfig(cache_mode=CacheMode.BYPASS, word_count_threshold=10)
        result = await crawler.arun(url=url, config=run_config)
        if result.success and result.markdown:
            return result.markdown.strip()
    except Exception:
        pass
    return _trafilatura_fetch(url)


@contextlib.contextmanager
def _redirect_stdout_to_stderr():
    previous = sys.stdout
    sys.stdout = sys.stderr
    try:
        yield
    finally:
        sys.stdout = previous


def _page_from_cache(url: str, cached_pages: dict[str, str]) -> str | None:
    body = cached_pages.get(url)
    if not body or len(body) < MIN_USABLE_BODY_CHARS or _looks_like_forum_chrome(body):
        return None
    return _truncate(body, MAX_MARKDOWN_CHARS_PER_PAGE)


async def _resolve_page_body(
    url: str,
    crawler: Any | None,
    cache_client: Any | None,
    cached_pages: dict[str, str],
    cache_stats: dict[str, int],
) -> str:
    cached = _page_from_cache(url, cached_pages)
    if cached is not None:
        cache_stats["page_hits"] = cache_stats.get("page_hits", 0) + 1
        return cached

    cache_stats["page_misses"] = cache_stats.get("page_misses", 0) + 1
    if crawler is not None:
        body = await _crawl_with_shared_crawler(crawler, url)
    else:
        body = _trafilatura_fetch(url)

    if body and len(body) >= MIN_USABLE_BODY_CHARS and not _looks_like_forum_chrome(body):
        trimmed = _truncate(body, MAX_MARKDOWN_CHARS_PER_PAGE)
        if cache_client is not None:
            try:
                from trask_cache import set_page

                set_page(cache_client, url, trimmed)
            except Exception:
                pass
        return trimmed
    return ""


async def gather_evidence(
    query: str,
    query_domains: list[str],
    source_urls: list[str],
    allowed_prefixes: list[str],
) -> GatherResult:
    result = GatherResult()
    cache_client = None
    try:
        from trask_cache import get_client, ping

        candidate = get_client()
        if candidate is not None and ping(candidate):
            cache_client = candidate
    except Exception:
        cache_client = None

    stats = result.cache_stats
    result.candidate_urls = discover_urls(
        query,
        query_domains,
        source_urls,
        allowed_prefixes,
        cache_client=cache_client,
        cache_stats=stats,
    )
    scrape_targets = list(result.candidate_urls[:MAX_SCRAPE_URLS])
    seen_targets = set(scrape_targets)

    cached_pages: dict[str, str] = {}
    if cache_client is not None and scrape_targets:
        try:
            from trask_cache import get_pages_bulk

            cached_pages = get_pages_bulk(cache_client, scrape_targets)
        except Exception:
            cached_pages = {}

    async def accept_url(url: str, crawler: Any | None) -> str | None:
        result.visited_urls.append(url)
        body = await _resolve_page_body(url, crawler, cache_client, cached_pages, stats)
        if not body:
            result.rejected_urls.append(url)
            return None
        result.pages.append(PageEvidence(url=url, markdown=body))
        result.retrieved_urls.append(url)
        return body

    try:
        from crawl4ai import AsyncWebCrawler, BrowserConfig

        browser_config = BrowserConfig(headless=True, verbose=False)
        with _redirect_stdout_to_stderr():
            async with AsyncWebCrawler(config=browser_config) as crawler:
                for url in list(scrape_targets):
                    body = await accept_url(url, crawler)
                    if not body:
                        continue
                    for follow_up in _extract_follow_up_links(body, query, allowed_prefixes):
                        if follow_up in seen_targets or len(scrape_targets) >= MAX_SCRAPE_URLS:
                            continue
                        seen_targets.add(follow_up)
                        scrape_targets.append(follow_up)
                        if cache_client is not None:
                            try:
                                from trask_cache import get_pages_bulk

                                cached_pages.update(get_pages_bulk(cache_client, [follow_up]))
                            except Exception:
                                pass
                        await accept_url(follow_up, crawler)
    except Exception:
        for url in scrape_targets:
            if url in result.retrieved_urls:
                continue
            await accept_url(url, None)

    return result


def build_report(query: str, gather: GatherResult) -> str:
    if not gather.pages:
        return "I could not complete live archive synthesis for this question right now."

    sections: list[str] = [
        f"# Research evidence for: {query.strip()}",
        "",
        "The following excerpts were retrieved from approved archive sources.",
        "",
    ]
    for page in gather.pages:
        sections.append(f"## Evidence from {page.url}")
        sections.append("")
        sections.append(page.markdown)
        sections.append("")
    return "\n".join(sections).strip()


def run_payload(payload: dict[str, Any]) -> dict[str, Any]:
    query = str(payload.get("query") or "").strip()
    if not query:
        raise ValueError("query is required")

    query_domains = [str(x) for x in (payload.get("query_domains") or []) if str(x).strip()]
    allowed_prefixes = [str(x) for x in (payload.get("allowed_url_prefixes") or []) if str(x).strip()]
    source_urls = [str(x) for x in (payload.get("source_urls") or []) if str(x).strip()]

    env_prefixes = os.environ.get("TRASK_ALLOWED_URL_PREFIXES", "")
    if env_prefixes and not allowed_prefixes:
        allowed_prefixes = [line.strip() for line in env_prefixes.splitlines() if line.strip()]
    env_domains = os.environ.get("TRASK_ALLOWED_QUERY_DOMAINS", "")
    if env_domains and not query_domains:
        query_domains = [line.strip() for line in env_domains.splitlines() if line.strip()]

    try:
        from trask_cache import (
            annotate_cache_meta,
            get_client,
            get_research,
            ping,
            research_key_for_payload,
            set_research,
        )

        cache_client = get_client()
        if cache_client is not None and ping(cache_client):
            rkey = research_key_for_payload(
                {
                    "query": query,
                    "query_domains": query_domains,
                    "allowed_url_prefixes": allowed_prefixes,
                    "source_urls": source_urls,
                },
            )
            cached_result = get_research(cache_client, rkey)
            if cached_result is not None:
                stats = dict((cached_result.get("research_information") or {}).get("cache") or {})
                stats["research_hits"] = stats.get("research_hits", 0) + 1
                return annotate_cache_meta(cached_result, stats)
    except Exception:
        pass

    gather = asyncio.run(
        gather_evidence(query, query_domains, source_urls, allowed_prefixes),
    )
    report = build_report(query, gather)

    result = {
        "report": report,
        "research_information": {
            "source_urls": gather.retrieved_urls,
            "cited_urls": gather.retrieved_urls,
            "retrieved_urls": gather.retrieved_urls,
            "visited_urls": gather.visited_urls,
            "query_domains": query_domains,
            "allowed_url_prefixes": allowed_prefixes,
            "rejected_source_urls": gather.rejected_urls,
        },
    }

    try:
        from trask_cache import annotate_cache_meta, get_client, ping, research_key_for_payload, set_research

        cache_client = get_client()
        if cache_client is not None and ping(cache_client) and gather.pages:
            rkey = research_key_for_payload(
                {
                    "query": query,
                    "query_domains": query_domains,
                    "allowed_url_prefixes": allowed_prefixes,
                    "source_urls": source_urls,
                },
            )
            stats = dict(gather.cache_stats)
            stats["research_misses"] = stats.get("research_misses", 0) + 1
            to_store = annotate_cache_meta(result, stats)
            set_research(cache_client, rkey, to_store)
            return to_store
    except Exception:
        pass

    if gather.cache_stats:
        try:
            from trask_cache import annotate_cache_meta

            return annotate_cache_meta(result, gather.cache_stats)
        except Exception:
            pass

    return result


def main() -> int:
    parser = argparse.ArgumentParser(description="Trask headless web research (Crawl4AI + DDG)")
    parser.add_argument("--dry-run", action="store_true", help="Import dependencies and exit 0")
    args = parser.parse_args()

    if args.dry_run:
        import crawl4ai  # noqa: F401
        import duckduckgo_search  # noqa: F401
        import trafilatura  # noqa: F401

        print(json.dumps({"ok": True, "backend": "crawl4ai"}))
        return 0

    raw = sys.stdin.read()
    if not raw.strip():
        print(json.dumps({"error": "empty stdin"}), file=sys.stderr)
        return 1

    try:
        payload = json.loads(raw)
    except json.JSONDecodeError as exc:
        print(json.dumps({"error": f"invalid json: {exc}"}), file=sys.stderr)
        return 1

    try:
        result = run_payload(payload)
    except Exception as exc:
        print(json.dumps({"error": str(exc)}), file=sys.stderr)
        return 1

    sys.stdout.write(json.dumps(result, ensure_ascii=False))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())