feat: add tiered durable state and memory bounds
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
PACKAGE_DIR = Path(__file__).resolve().parents[1] / "modelhub_submmit_api"
|
||||
@@ -68,6 +69,17 @@ class ModelScopeDiscoveryTests(unittest.TestCase):
|
||||
self.assertEqual(1786294389, int(first.timestamp())) # type: ignore[union-attr]
|
||||
self.assertEqual(1, metadata_client.calls)
|
||||
|
||||
def test_model_detail_cache_evicts_old_entries_at_fixed_limit(self) -> None:
|
||||
metadata_client = ModelMetadataClient()
|
||||
with patch.dict("os.environ", {"MODELSCOPE_DETAIL_CACHE_MAX_MODELS": "32"}):
|
||||
discovery = HuggingFaceDiscovery(legacy_http_client=metadata_client) # type: ignore[arg-type]
|
||||
for index in range(40):
|
||||
discovery.get_model_last_modified(f"owner/model-{index}")
|
||||
|
||||
self.assertEqual(32, len(discovery._model_last_modified_cache))
|
||||
discovery.get_model_last_modified("owner/model-0")
|
||||
self.assertEqual(41, metadata_client.calls)
|
||||
|
||||
def test_openapi_page_size_never_exceeds_platform_limit(self) -> None:
|
||||
http_client = RecordingHttpClient()
|
||||
discovery = HuggingFaceDiscovery(http_client=http_client) # type: ignore[arg-type]
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import gzip
|
||||
import tempfile
|
||||
import unittest
|
||||
from datetime import datetime, timezone
|
||||
@@ -19,6 +21,7 @@ from common import read_jsonl, write_json, write_jsonl # noqa: E402
|
||||
from config_optimizer import SafeConfigOptimizer # noqa: E402
|
||||
from hf_discovery import HuggingFaceDiscovery, parse_model_card_front_matter # noqa: E402
|
||||
from official_capabilities import OfficialCapabilityRegistry # noqa: E402
|
||||
from outcome_tracker import OutcomeTracker # noqa: E402
|
||||
from routing_engine import SuccessFirstRoutingEngine # noqa: E402
|
||||
from state_sync import StateGitSync # noqa: E402
|
||||
|
||||
@@ -44,6 +47,91 @@ class OfficialClient:
|
||||
|
||||
|
||||
class SuperAgentTests(unittest.TestCase):
|
||||
def test_outcome_history_compacts_to_checkpoint_recent_window_and_gzip_archive(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary_dir:
|
||||
root = Path(temporary_dir)
|
||||
outcomes = root / "outcomes.jsonl"
|
||||
checkpoint = root / "checkpoint.json"
|
||||
recent = root / "recent.jsonl"
|
||||
archive = root / "archive"
|
||||
rows = [
|
||||
{
|
||||
"taskId": str(index),
|
||||
"modelId": f"owner/model-{index}",
|
||||
"targetGpu": "gpu-a",
|
||||
"framework": "vllm",
|
||||
"taskType": "text-generation",
|
||||
"submitTime": f"2026-08-{1 + index // 100:02d}T00:00:{index % 60:02d}+00:00",
|
||||
"lastSyncTime": "2026-08-21T00:00:00+00:00",
|
||||
"outcome": "success" if index % 2 == 0 else "failed",
|
||||
"failureCategory": "model_runtime" if index % 2 else None,
|
||||
"failureScope": "model" if index % 2 else None,
|
||||
"logCosUrl": "https://secret.invalid/signed?token=hidden",
|
||||
}
|
||||
for index in range(600)
|
||||
]
|
||||
write_jsonl(outcomes, rows)
|
||||
with patch.dict(
|
||||
os.environ,
|
||||
{
|
||||
"MODELHUB_AGENT_OUTCOME_COMPACT_THRESHOLD": "500",
|
||||
"MODELHUB_AGENT_RECENT_OUTCOME_LIMIT": "100",
|
||||
},
|
||||
):
|
||||
tracker = OutcomeTracker(
|
||||
outcomes,
|
||||
checkpoint_path=checkpoint,
|
||||
recent_path=recent,
|
||||
archive_pending_dir=archive,
|
||||
)
|
||||
self.assertTrue(tracker.has_durable_checkpoint)
|
||||
self.assertEqual([], read_jsonl(outcomes))
|
||||
self.assertEqual(100, len(read_jsonl(recent)))
|
||||
report = tracker.get_stats_report()
|
||||
self.assertEqual(600, report["terminalRecords"])
|
||||
self.assertEqual(300, report["totals"]["successCount"])
|
||||
shard = next(archive.rglob("*.jsonl.gz"))
|
||||
import gzip
|
||||
|
||||
with gzip.open(shard, "rt", encoding="utf-8") as handle:
|
||||
archived_text = handle.read()
|
||||
self.assertNotIn("logCosUrl", archived_text)
|
||||
self.assertNotIn("token=hidden", archived_text)
|
||||
|
||||
restored = OutcomeTracker(
|
||||
outcomes,
|
||||
checkpoint_path=checkpoint,
|
||||
recent_path=recent,
|
||||
archive_pending_dir=archive,
|
||||
)
|
||||
self.assertEqual(600, restored.get_stats_report()["terminalRecords"])
|
||||
|
||||
restored.record_submission(
|
||||
model_id="owner/new-model",
|
||||
target_gpu="gpu-a",
|
||||
framework="vllm",
|
||||
task_type="text-generation",
|
||||
task_id="new-task",
|
||||
submit_time="2026-08-22T00:00:00+00:00",
|
||||
)
|
||||
restored._update_record_from_task(
|
||||
restored._by_task_id["new-task"],
|
||||
{"status": "success", "verifyResult": 1},
|
||||
)
|
||||
restored.save()
|
||||
updated = restored.get_stats_report()
|
||||
self.assertEqual(601, updated["terminalRecords"])
|
||||
self.assertEqual(301, updated["totals"]["successCount"])
|
||||
|
||||
restarted = OutcomeTracker(
|
||||
outcomes,
|
||||
checkpoint_path=checkpoint,
|
||||
recent_path=recent,
|
||||
archive_pending_dir=archive,
|
||||
)
|
||||
self.assertEqual(601, restarted.get_stats_report()["terminalRecords"])
|
||||
self.assertIn("599", restarted._by_task_id)
|
||||
|
||||
def test_official_registry_discovers_catalog_and_exact_model_routes(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary_dir:
|
||||
registry = OfficialCapabilityRegistry(Path(temporary_dir) / "official.json", log_fn=lambda _: None)
|
||||
@@ -149,6 +237,17 @@ class SuperAgentTests(unittest.TestCase):
|
||||
restored_project / ".modelhub_state" / "worker_crashes.jsonl",
|
||||
[{"at": "2026-08-21T01:00:00+00:00", "exitCode": 1}],
|
||||
)
|
||||
pending_archive = (
|
||||
project
|
||||
/ ".modelhub_state"
|
||||
/ "archive_pending"
|
||||
/ "outcomes"
|
||||
/ "2026-08"
|
||||
/ "shard.jsonl.gz"
|
||||
)
|
||||
pending_archive.parent.mkdir(parents=True, exist_ok=True)
|
||||
with gzip.open(pending_archive, "wt", encoding="utf-8") as handle:
|
||||
handle.write('{"taskId":"archived"}\n')
|
||||
credentials = {"username": "tester", "email": "tester@example.com", "password": "secret-value"}
|
||||
manager = StateGitSync(
|
||||
project_root=project,
|
||||
@@ -171,6 +270,9 @@ class SuperAgentTests(unittest.TestCase):
|
||||
]
|
||||
)
|
||||
self.assertIsNotNone(batch_id)
|
||||
self.assertFalse(pending_archive.exists())
|
||||
archive_refs = porcelain.ls_remote(str(remote)).refs
|
||||
self.assertIn(b"refs/heads/agent-archive-2026-08", archive_refs)
|
||||
manager.close()
|
||||
|
||||
restored = StateGitSync(
|
||||
|
||||
Reference in New Issue
Block a user