119 lines
5.7 KiB
Python
119 lines
5.7 KiB
Python
import sys
|
|
import tempfile
|
|
import unittest
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'modelhub_submmit_api'))
|
|
from modelhub_client import ModelHubClient, ModelHubClientPool, ModelHubAPIError
|
|
from outcome_tracker import OutcomeTracker
|
|
from failure_taxonomy import classify_failure_report
|
|
|
|
|
|
class OutcomeReconciliationTests(unittest.TestCase):
|
|
def tracker(self, root):
|
|
return OutcomeTracker(root / 'outcomes.jsonl', checkpoint_path=root / 'checkpoint.json',
|
|
recent_path=root / 'recent.jsonl')
|
|
|
|
def test_history_ignores_false_last_page_and_deduplicates(self):
|
|
client = ModelHubClient(token='test')
|
|
rows = {1: [{'taskId': 'a'}], 2: [{'taskId': 'a'}, {'taskId': 'b'}], 3: []}
|
|
client.list_tasks_page = lambda **kw: {'data': {'pages': 1, 'total': 1, 'records': rows[kw['current']]}}
|
|
self.assertEqual(['a', 'b'], [r['taskId'] for r in client.list_tasks()])
|
|
|
|
def test_history_repeated_page_fails(self):
|
|
client = ModelHubClient(token='test')
|
|
client.list_tasks_page = lambda **kw: {'data': {'records': [{'taskId': 'a'}]}}
|
|
with self.assertRaises(ModelHubAPIError):
|
|
client.list_tasks()
|
|
|
|
def test_explicit_record_budget_still_works(self):
|
|
client = ModelHubClient(token='test')
|
|
client.list_tasks_page = lambda **kw: {'data': {'records': [{'taskId': 'a'}, {'taskId': 'b'}]}}
|
|
self.assertEqual([{'taskId': 'a'}], client.list_tasks(max_records=1))
|
|
|
|
def test_partial_account_read_does_not_advance_cursor(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
tracker = self.tracker(Path(tmp))
|
|
before = tracker._last_sync_time
|
|
good, bad = ModelHubClient(token='good'), ModelHubClient(token='bad')
|
|
good.list_tasks = lambda **kw: [{'taskId': 'done', 'status': 'failed'}]
|
|
def fail(**kw):
|
|
raise ModelHubAPIError('read failed')
|
|
bad.list_tasks = fail
|
|
pool = ModelHubClientPool([good, bad])
|
|
self.assertEqual(0, tracker.sync_from_api(pool))
|
|
self.assertEqual(before, tracker._last_sync_time)
|
|
self.assertEqual([], tracker._records)
|
|
|
|
def test_old_pending_result_reconciles_without_time_filter_and_retains_version(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
tracker = self.tracker(Path(tmp))
|
|
tracker.record_submission('owner/model', 'gpu', 'vllm', 'text-generation', 'old-task',
|
|
'2026-01-01T00:00:00Z')
|
|
version = tracker._by_task_id['old-task'].get('agentVersion')
|
|
client = ModelHubClient(token='test')
|
|
calls = []
|
|
def history(**kw):
|
|
calls.append(kw)
|
|
if kw.get('model_id'):
|
|
return [{'taskId': 'old-task', 'modelId': 'owner/model', 'gpuType': 'gpu',
|
|
'status': 'success', 'verifyResult': 1},
|
|
{'taskId': 'unrelated-old-task', 'status': 'failed'}]
|
|
return []
|
|
client.list_tasks = history
|
|
tracker.sync_from_api(client)
|
|
row = tracker._by_task_id['old-task']
|
|
self.assertEqual('success', row['outcome'])
|
|
self.assertEqual(version, row.get('agentVersion'))
|
|
self.assertNotIn('unrelated-old-task', tracker._by_task_id)
|
|
self.assertNotIn('begin_time', calls[-1])
|
|
tracker.sync_from_api(client)
|
|
self.assertEqual(1, tracker.get_stats_report()['totals']['successCount'])
|
|
|
|
def test_reconciliation_rotates_and_survives_restart(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
tracker = self.tracker(root)
|
|
for i in range(6):
|
|
tracker.record_submission(f'owner/model{i}', 'gpu', 'vllm', 'text-generation', str(i),
|
|
datetime.now(timezone.utc).isoformat())
|
|
client = ModelHubClient(token='test')
|
|
models = []
|
|
def history(**kw):
|
|
if kw.get('model_id'):
|
|
models.append(kw['model_id'])
|
|
return []
|
|
client.list_tasks = history
|
|
tracker.sync_from_api(client)
|
|
self.assertEqual(4, len(models))
|
|
tracker = self.tracker(root)
|
|
tracker.sync_from_api(client)
|
|
self.assertEqual(6, len(set(models)))
|
|
self.assertEqual(6, len(tracker.pending_records())) # Missing is not completed.
|
|
|
|
def test_missing_host_runtime_is_not_model_incompatibility(self):
|
|
result = classify_failure_report('EXECUTE_EMPTY_RESULT', [
|
|
'fork/exec /usr/bin/musa/mthreads-container-runtime: no such file or directory'])
|
|
self.assertEqual('platform_infrastructure', result.category)
|
|
self.assertFalse(result.needs_llm)
|
|
|
|
def test_overlap_does_not_reimport_compacted_old_terminal(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
tracker = self.tracker(Path(tmp))
|
|
tracker._checkpoint = {'version': 1, 'report': {}}
|
|
client = ModelHubClient(token='test')
|
|
client.list_tasks = lambda **kw: [{'taskId': 'old', 'status': 'success',
|
|
'verifyResult': 1, 'updateTime': '2020-01-01 00:00:00'}]
|
|
tracker.sync_from_api(client)
|
|
self.assertNotIn('old', tracker._by_task_id)
|
|
|
|
def test_state_sync_retains_reconciliation_marker(self):
|
|
from state_sync import STATE_OUTCOME_FIELDS
|
|
self.assertIn('lastOutcomeReconcileAt', STATE_OUTCOME_FIELDS)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|