feat: improve route throughput and reconcile delayed validation outcomes
This commit is contained in:
1 parent
5dead8172c
commit
43ecd36c0e
13 files changed
+468
-83
No files matched your search
@@ -46,6 +46,7 @@ DECISION_RECORD_FIELDS = {
|
||||
"taskType",
|
||||
"submitTime",
|
||||
"lastSyncTime",
|
||||
"lastOutcomeReconcileAt",
|
||||
"status",
|
||||
"verifyResult",
|
||||
"outcome",
|
||||
@@ -436,16 +437,49 @@ class OutcomeTracker:
|
||||
task_contexts: dict[str, dict[str, Any]] | None = None,
|
||||
) -> int:
|
||||
try:
|
||||
begin = self._last_sync_time
|
||||
# A small overlap tolerates delayed publication and boundary timestamps.
|
||||
begin = self._last_sync_time - timedelta(hours=24)
|
||||
end = utc_now()
|
||||
# Use fanout for pools to sync outcomes across all accounts
|
||||
list_kwargs: dict[str, Any] = {"begin_time": begin, "end_time": end, "page_size": 100, "only_mine": True}
|
||||
if isinstance(client, ModelHubClientPool):
|
||||
list_kwargs["_fanout_all"] = True
|
||||
tasks = client.list_tasks(**list_kwargs)
|
||||
except Exception:
|
||||
list_kwargs["_strict"] = True
|
||||
tasks = list(client.list_tasks(**list_kwargs))
|
||||
except Exception as exc:
|
||||
print(f"[outcome-sync] status=deferred reason=history_read_failed error={type(exc).__name__} cursor_advanced=false", flush=True)
|
||||
return 0
|
||||
|
||||
# Time filters are not sufficient to discover old tasks completing late.
|
||||
# Reconcile a bounded, rotating set of known pending models without a
|
||||
# time filter. Never infer completion from disappearance alone.
|
||||
pending_by_model: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
||||
for record in self._records:
|
||||
if record.get("outcome") == "pending" and record.get("modelId"):
|
||||
pending_by_model[str(record["modelId"])].append(record)
|
||||
floor = datetime.min.replace(tzinfo=end.tzinfo)
|
||||
def checked_at(rows):
|
||||
return max((parse_datetime(r.get("lastOutcomeReconcileAt")) or floor for r in rows), default=floor)
|
||||
eligible = [(model, rows) for model, rows in pending_by_model.items()
|
||||
if checked_at(rows) < end - timedelta(minutes=15)]
|
||||
eligible.sort(key=lambda item: (checked_at(item[1]), item[0]))
|
||||
reconciled = 0
|
||||
for model, rows in eligible[:4]:
|
||||
kwargs = {"model_id": model, "page_size": 100, "only_mine": True}
|
||||
if isinstance(client, ModelHubClientPool):
|
||||
kwargs.update({"_fanout_all": True, "_strict": True})
|
||||
for row in rows:
|
||||
row["lastOutcomeReconcileAt"] = end.isoformat()
|
||||
reconciled += 1
|
||||
try:
|
||||
wanted = {str(row.get("taskId")) for row in rows}
|
||||
tasks.extend(task for task in client.list_tasks(**kwargs)
|
||||
if str(task.get("taskId")) in wanted)
|
||||
except Exception as exc:
|
||||
print(f"[outcome-sync] reconciliation=deferred error={type(exc).__name__}", flush=True)
|
||||
# Overlap and model reconciliation may return the same task more than once.
|
||||
tasks = list({str(t.get("taskId")): t for t in tasks if t.get("taskId")}.values())
|
||||
|
||||
updated_count = 0
|
||||
enrichment_candidates: list[dict[str, Any]] = []
|
||||
for task in tasks:
|
||||
@@ -481,6 +515,11 @@ class OutcomeTracker:
|
||||
updated_count += 1
|
||||
else:
|
||||
status = str(task.get("status") or "").lower()
|
||||
# Overlap is for refreshing known tasks, not re-importing old
|
||||
# terminal rows already folded into a compact checkpoint.
|
||||
timestamp = parse_datetime(task.get("updateTime"))
|
||||
if self._checkpoint and timestamp and timestamp < self._last_sync_time:
|
||||
continue
|
||||
if status in TERMINAL_TASK_STATUSES:
|
||||
record = self._create_record_from_task(task)
|
||||
self._records.append(record)
|
||||
@@ -512,12 +551,14 @@ class OutcomeTracker:
|
||||
enrichment_candidates[:FAILURE_ENRICHMENT_LIMIT]
|
||||
)
|
||||
|
||||
if updated_count or enrichment_attempts:
|
||||
if updated_count or enrichment_attempts or reconciled:
|
||||
self._last_sync_time = end
|
||||
self._rebuild_failed_index()
|
||||
write_jsonl(self.recent_path, self._recent_records)
|
||||
self.save()
|
||||
|
||||
print(f"[outcome-sync] tasks={len(tasks)} updated={updated_count} reconciled_models={reconciled} pending={len(self.pending_records())}", flush=True)
|
||||
|
||||
return updated_count
|
||||
|
||||
def bootstrap_from_history_tasks(
|
||||
|
||||
Reference in new issue
Block a user