Build reproducible capability advisor and scheduled source capture

Assistant: codex
Assistant-Model: gpt-6-astra
Assistant-Session: 01a09c77-b1a9-7eb3-b501-6cb24b458c97
This commit is contained in:
tegwick 2026-09-13 23:07:41 +02:00
parent a1edefd7c5
commit 29ceb77938
68 changed files with 2431 additions and 85 deletions

95
tests/test_advisor.py Normal file
View file

@ -0,0 +1,95 @@
import copy
import json
from pathlib import Path
import tempfile
import unittest
from intelligence_radar.advisor import advise, outcome, recommend, replay
REPO = Path(__file__).resolve().parents[1]
class AdvisorTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.task = json.loads((REPO/'examples/task.json').read_text())
self.catalog = json.loads((REPO/'examples/advisor-catalog.json').read_text())
self.policy = json.loads((REPO/'policies/advisor-v0.2.json').read_text())
def report(self):
return advise(self.root, self.task, self.catalog, self.policy, '2026-09-13', True)
def test_rank_cost_and_no_unearned_sufficiency(self):
r = self.report()
self.assertEqual(r['frontier'], 'frontier-fixture')
self.assertEqual(r['efficient'], 'efficient-fixture')
self.assertIsNone(r['budget'])
self.assertEqual(len(r['selected_benchmarks']), 5)
self.assertAlmostEqual(sum(b['weight'] for b in r['selected_benchmarks']), 1)
efficient = next(x for x in r['profiles'] if x['id']=='efficient-fixture')
self.assertAlmostEqual(efficient['cost']['scenario_usd'], .07)
def test_missing_score_never_imputed(self):
self.catalog['results'].pop(0)
report = self.report()
self.assertEqual(report['frontier'], 'efficient-fixture')
row = next(x for x in report['profiles'] if x['id']=='frontier-fixture')
self.assertIsNone(row['fitness_proxy'])
self.assertEqual(len(row['missing_measurements']), 1)
def test_constraints_remove_highest_scoring_profile(self):
self.catalog['profiles'][0]['attributes']['requester_access'] = False
self.assertEqual(self.report()['frontier'], 'efficient-fixture')
def test_missing_or_conflicting_price_prevents_cheapest_claim(self):
self.catalog['prices'].pop(0)
self.assertIsNone(self.report()['efficient'])
self.catalog['prices'].append(copy.deepcopy(self.catalog['prices'][0]))
self.assertIsNone(self.report()['efficient'])
def test_stale_future_and_wrong_version_results(self):
for field, value in [('observed_at','2026-09-14T00:00:00Z'),
('observed_at','2025-01-01T00:00:00Z'),
('benchmark_version','other')]:
with self.subTest(field=field,value=value):
original=self.catalog['results'][0].copy()
self.catalog['results'][0][field]=value
self.assertEqual(self.report()['frontier'], 'efficient-fixture')
self.catalog['results'][0]=original
def test_aggregate_and_short_selection_withhold_ranking(self):
self.catalog['benchmarks'][0]['aggregate']=True
self.assertIsNone(self.report()['frontier'])
def test_calibration_duplicate_protection_and_frozen_replay(self):
ident, initial = recommend(self.root,self.task,self.catalog,self.policy,'2026-09-13',True)
value=dict(recommendation_id=ident,profile_id='budget-fixture',run_id='run-0',
accepted=True,independent_verification=True,verifier_evidence='synthetic test run',
actual_usd=.01,runtime_seconds=1,completed_at='2026-09-13T22:00:00Z')
first=outcome(self.root,value)
self.assertEqual(outcome(self.root,value),first)
with self.assertRaises(ValueError):
outcome(self.root,dict(value,accepted=False))
for i in range(1,20):
outcome(self.root,dict(value,run_id=f'run-{i}'))
self.assertEqual(self.report()['budget'],'budget-fixture')
self.assertEqual(replay(self.root,ident), initial)
self.task['acceptance']+=' Different acceptance test.'
self.assertIsNone(self.report()['budget'])
def test_synthetic_requires_explicit_opt_in(self):
with self.assertRaises(ValueError):
advise(self.root,self.task,self.catalog,self.policy,'2026-09-13')
def test_invalid_numeric_inputs_rejected(self):
self.catalog['results'][0]['raw_score']=float('nan')
with self.assertRaises(ValueError):
self.report()
def test_replay_is_independent_of_json_object_key_order(self):
self.task['constraints']['unavailable_attribute'] = True
self.task['capabilities']['coding'] = .31
ident, initial = recommend(self.root,self.task,self.catalog,self.policy,'2026-09-13',True)
self.assertEqual(replay(self.root,ident), initial)

View file

@ -0,0 +1,67 @@
from datetime import datetime, timezone
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from intelligence_radar.ingest import capture, check_artifacts, MAX_BYTES
from intelligence_radar.scheduler import run_due
from intelligence_radar.store import all_records
class Response(io.BytesIO):
headers={'Content-Type':'text/plain'}
def geturl(self):
return 'https://example.invalid/source'
class IngestionTests(unittest.TestCase):
def setUp(self):
self.temp=tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root=Path(self.temp.name)
self.source={'id':'test','publisher':'test','url':'https://example.invalid/source'}
def test_capture_deduplication_and_integrity(self):
first,r=capture(self.root,self.source,opener=lambda *a,**k:Response(b'body'))
second,s=capture(self.root,self.source,opener=lambda *a,**k:Response(b'body'))
self.assertTrue(r['changed'])
self.assertFalse(s['changed'])
self.assertEqual(len(list((self.root/'data/artifacts').iterdir())),1)
self.assertEqual(check_artifacts(self.root),2)
(self.root/'data/artifacts'/r['content_sha256']).write_bytes(b'tampered')
with self.assertRaises(ValueError):
check_artifacts(self.root)
def test_retry_and_capture_limit(self):
with patch('intelligence_radar.ingest.time.sleep'), patch('urllib.request.urlopen'):
count=[0]
def fetch(*args,**kwargs):
count[0]+=1
if count[0]<3:
raise OSError('transient')
return Response(b'ok')
_,r=capture(self.root,self.source,opener=fetch)
self.assertEqual(r['attempts'],3)
with self.assertRaises(ValueError):
capture(self.root,self.source,opener=lambda *a,**k:Response(b'x'*(MAX_BYTES+1)))
def test_scheduler_retries_failed_job_and_preserves_existing_digest(self):
(self.root/'sources.json').write_text('{"sources": []}')
config={'timezone':'Europe/Berlin','jobs':[{'id':'daily','cadence':'daily','at':'21:30','sources':'sources.json'}]}
now=datetime(2026,9,13,20,tzinfo=timezone.utc)
failed=run_due(self.root,config,now,runner=lambda *a:{'ok':False})
self.assertFalse(failed['ok'])
passed=run_due(self.root,config,now,runner=lambda *a:{'ok':True})
self.assertTrue(passed['ok'])
self.assertEqual(len(passed['jobs']),1)
self.assertEqual(run_due(self.root,config,now,runner=lambda *a:self.fail('already done'))['jobs'],[])
self.assertEqual(len(all_records(self.root,'scheduler-runs')),2)
def test_weekly_iso_boundary_and_dst_zone(self):
(self.root/'sources.json').write_text('{"sources": []}')
config={'timezone':'Europe/Berlin','jobs':[{'id':'weekly','cadence':'weekly','weekday':6,'at':'20:00','sources':'sources.json'}]}
result=run_due(self.root,config,datetime(2026,1,4,19,tzinfo=timezone.utc),runner=lambda *a:{'ok':True})
self.assertEqual(result['jobs'][0]['period'],'2026-W01')