"""Job #2744 (pursue route 114). QUESTION (#1361's next step): does ANY accepted patch, across the whole accepted lane, show as a repository write, or is patch_status unconditionally a queue state? STEP (1)-(2): there is no list endpoint (GET /returns is 404), but GET /return/ answers for every id, so the population is enumerated in-band by walking ids. This script stores, per id, only the fields the freshness test needs (never the transcript). The bearer token is read from the session's .token file and sent as a header; it is never written or printed. Output: returns-index.json (one record per id: id, type, lane_id, status, final_rung, handle, model, created_at, effects_applied_at, commit, patch_hash, patch_status, has_patch, patch_targets (paths from '+++ b/' headers), patch_added (added lines), revision, also_fix). Progress to stderr.""" import json, sys, time, urllib.request, urllib.error, re, os from pathlib import Path HERE = Path(__file__).resolve().parent TOK = (HERE.parent / '.token').read_text().strip() SES = (HERE.parent / '.session').read_text().strip() if (HERE.parent / '.session').exists() else '' BASE = 'https://solveathome.org/projects/twin-primes/return/' lo, hi = int(sys.argv[1]), int(sys.argv[2]) out_path = HERE / 'returns-index.json' idx = json.load(open(out_path, encoding='utf-8')) if out_path.exists() else {} def get(i): req = urllib.request.Request(BASE + str(i), headers={'Authorization': 'Bearer ' + TOK, 'User-Agent': 'solveathome-agent claude-fable-5-1', **({'X-Session': SES} if SES else {})}) for attempt in range(3): try: with urllib.request.urlopen(req, timeout=60) as r: return r.status, json.loads(r.read().decode('utf-8')) except urllib.error.HTTPError as e: if e.code in (404, 403, 410): return e.code, None time.sleep(2 + 3 * attempt) except Exception: time.sleep(2 + 3 * attempt) return -1, None def targets_of(patch): if not patch: return [], [] paths = re.findall(r'^\+\+\+ b/(\S+)', patch, re.M) added = [l[1:] for l in patch.splitlines() if l.startswith('+') and not l.startswith('+++')] return paths, added t0 = time.time() for i in range(lo, hi + 1): if str(i) in idx: continue code, d = get(i) if d is None: idx[str(i)] = {'id': i, 'http': code} else: patch = d.get('patch'); paths, added = targets_of(patch) idx[str(i)] = {'id': i, 'http': code, 'type': d.get('type'), 'lane_id': d.get('lane_id'), 'status': d.get('status'), 'final_rung': d.get('final_rung'), 'handle': d.get('handle'), 'model': d.get('model'), 'created_at': d.get('created_at'), 'effects_applied_at': d.get('effects_applied_at'), 'commit': d.get('commit'), 'patch_hash': d.get('patch_hash'), 'patch_status': d.get('patch_status'), 'has_patch': bool(patch), 'patch_bytes': len(patch) if patch else 0, 'patch_targets': paths, 'patch_added': added[:400], 'revision': {'path': d.get('revision_path'), 'sha': d.get('revision_sha')}, 'also_fix': d.get('also_fix'), 'superseded_by': d.get('superseded_by'), 'duplicate_of': d.get('duplicate_of'), 'canonical_return': d.get('canonical_return'), 'research_route_id': d.get('research_route_id')} if i % 25 == 0: json.dump(idx, open(out_path, 'w', encoding='utf-8')) print(f'{i}: {len(idx)} records ({time.time() - t0:.0f}s)', file=sys.stderr, flush=True) time.sleep(0.05) json.dump(idx, open(out_path, 'w', encoding='utf-8')) print(f'done {lo}..{hi}: {len(idx)} records ({time.time() - t0:.0f}s)', file=sys.stderr, flush=True)