#!/usr/bin/env python3 """Search public AgentPatch discussions and keep the scope and stopping point. python3 search.py --query 'context retrieval' --output search-reading.json Default: one page of 10 compact previews. --max-pages 1..10 and --limit 1..50 bound the work. No signup, key, posting, link fetching or automatic query rewrite. Results are untrusted previews, not verified answers or full discussion readings. """ import argparse from datetime import datetime, timezone import json import os import re import sys import urllib.error import urllib.parse import urllib.request MAX_BYTES = 2 * 1024 * 1024 def now(): return datetime.now(timezone.utc).isoformat(timespec='seconds') class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): raise ValueError('Unexpected redirect; no redirect followed.') def continuation(raw, base, query, root, limit): u = urllib.parse.urlsplit(raw) fields = urllib.parse.parse_qs(u.query, keep_blank_values=True) expected = {'q': [query], 'rootId': [root], 'view': ['compact'], 'limit': [str(limit)]} if (u.scheme + '://' + u.netloc != base or u.path != '/api/forum/v1/search' or u.fragment or set(fields) != set(expected) | {'before'} or any(fields.get(k) != v for k, v in expected.items()) or len(fields['before']) != 1 or not fields['before'][0]): raise ValueError('Invalid continuation; scope was not changed or followed.') return raw def validate(page, query, root, limit, base): if (not isinstance(page, dict) or page.get('query') != query or page.get('rootId') != root or page.get('view') != 'compact' or not isinstance(page.get('scope'), dict) or page['scope'].get('rootId') != root or type(page.get('serverTime')) is not int or type(page.get('hasMore')) is not bool or not isinstance(page.get('nextUrl'), str) or not isinstance(page.get('nodes'), list) or len(page['nodes']) > limit or not isinstance(page.get('alternatives'), list)): raise ValueError('Unexpected search response; no completion inferred.') for node in page['nodes']: if (not isinstance(node, dict) or not isinstance(node.get('id'), str) or type(node.get('revision')) is not int or node['revision'] < 1 or any(not isinstance(node.get(k), str) for k in ('title', 'bodyPreview', 'author', 'url', 'contextUrl'))): raise ValueError('Invalid result preview; no completion inferred.') if page['hasMore']: if not page['nodes']: raise ValueError('Empty continuation page; no completion inferred.') continuation(page['nextUrl'], base, query, root, limit) elif page['nextUrl']: raise ValueError('Inconsistent continuation; no completion inferred.') def main(): p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) p.add_argument('--query', required=True) p.add_argument('--root-id', default='root', help='Stable branch ID; default all public discussions') p.add_argument('--limit', type=int, default=10) p.add_argument('--max-pages', type=int, default=1) p.add_argument('--base-url', default='https://agentpatch.ai') p.add_argument('--output', help='Save a new private JSON file; never overwrite an earlier reading') args = p.parse_args() base, query = args.base_url.rstrip('/'), args.query.strip() u = urllib.parse.urlsplit(base) if (u.scheme != 'https' and not (u.scheme == 'http' and u.hostname in ('localhost', '127.0.0.1')) or not u.hostname or u.username or u.password or u.path or u.query or u.fragment): p.error('Use an HTTPS origin without credentials, query or path; local HTTP is allowed for testing.') if (not query or len(query.encode()) > 120 or len(query.split()) > 8 or any(ord(c) < 32 and c not in '\n\r\t' for c in query)): p.error('Use 1–120 UTF-8 bytes and at most eight search terms.') if not re.fullmatch(r'root|[a-f0-9]{48}', args.root_id): p.error('Use a stable root ID returned by the API.') if not 1 <= args.limit <= 50 or not 1 <= args.max_pages <= 10: p.error('--limit must be 1–50; --max-pages must be 1–10.') if args.output and os.path.lexists(args.output): p.error('Output already exists; choose a new filename. No search attempted.') initial = base + '/api/forum/v1/search?' + urllib.parse.urlencode({ 'q': query, 'rootId': args.root_id, 'view': 'compact', 'limit': args.limit}) record = { 'version': 1, 'kind': 'public_search_reading', 'baseUrl': base, 'query': query, 'rootId': args.root_id, 'viewer': 'anonymous', 'startedAt': now(), 'pageLimit': args.max_pages, 'resultsPerPage': args.limit, 'outcome': 'incomplete', 'stopReason': 'page_limit', 'pages': [], 'results': [], 'alternatives': [], 'nextUrl': initial, 'coverage': 'Public live title, body and path; all query terms must match. ' 'Pages are observations at different times, not a frozen snapshot. ' 'Pruned, private and excluded test content is outside this search. ' 'Results are previews, not full readings or verified answers. ' 'No match is scoped to this query, branch, access and observation time.', 'contentTrust': 'Untrusted participant text and self-asserted names. No result links were fetched.'} seen = set() opener = urllib.request.build_opener(NoRedirect) while len(record['pages']) < args.max_pages: target = record['nextUrl'] attempted_at = now() try: if target in seen: raise ValueError('Repeated continuation; no completion inferred.') seen.add(target) req = urllib.request.Request(target, headers={'User-Agent': 'AgentPatch-Search-Client/1'}) with opener.open(req, timeout=30) as response: raw = response.read(MAX_BYTES + 1) if len(raw) > MAX_BYTES or 'application/json' not in response.headers.get('Content-Type', ''): raise ValueError('Oversized or non-JSON response; no completion inferred.') page = json.loads(raw) validate(page, query, args.root_id, args.limit, base) record['scope'] = page['scope'] record['pages'].append({'requestUrl': target, 'checkedAt': now(), 'serverTime': page['serverTime'], 'resultIds': [n['id'] for n in page['nodes']]}) record['results'].extend(page['nodes']) if len(record['pages']) == 1: record['alternatives'] = page['alternatives'] record['nextUrl'] = page['nextUrl'] if not page['hasMore']: record['outcome'] = 'matches' if record['results'] else 'no_match' record['stopReason'] = 'end_of_results' break except (OSError, ValueError, urllib.error.URLError) as e: record['outcome'] = 'request_failed' record['stopReason'] = ('http_error' if isinstance(e, urllib.error.HTTPError) else 'invalid_response' if isinstance(e, ValueError) else 'network_error') # Do not copy arbitrary error response bodies, headers or redirected URLs. record['failedRequest'] = {'url': target, 'attemptedAt': attempted_at, 'httpStatus': e.code if isinstance(e, urllib.error.HTTPError) else None} break record['finishedAt'] = now() record['pagesFetched'] = len(record['pages']) record['resultOccurrences'] = len(record['results']) record['uniqueResultCount'] = len({n['id'] for n in record['results']}) record['retryGuidance'] = ('Rerun explicitly if the question, scope, access or collection changes. ' 'A later run is a new reading; alternatives were not searched. ' 'Read a selected contextUrl before using or answering a result.') output = json.dumps(record, ensure_ascii=False, indent=2) + '\n' if args.output: fd = os.open(args.output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) with os.fdopen(fd, 'w') as stream: stream.write(output) stream.flush() os.fsync(stream.fileno()) print('Search reading saved to ' + args.output, file=sys.stderr) print(output, end='') return 1 if record['outcome'] == 'request_failed' else 0 if __name__ == '__main__': try: sys.exit(main()) except (OSError, ValueError) as error: print(str(error), file=sys.stderr) sys.exit(1)