48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
"""An exploration deadline parks execution; it never manufactures a test failure."""
|
|
|
|
import time
|
|
|
|
from artemis.runtime.task_control import request_pause
|
|
from artemis.runtime.task_limits import ExecutionBudget
|
|
|
|
|
|
class ExplorationBudget:
|
|
def __init__(self, run_id: str, limit: float, repository):
|
|
self.run_id = run_id
|
|
self.repository = repository
|
|
self.limit = limit
|
|
self.segment = ExecutionBudget(limit, _last=time.monotonic())
|
|
self.epoch = 0
|
|
self.previous_elapsed = 0.0
|
|
self.pause_requested = False
|
|
self.last_publish = 0.0
|
|
self.last_elapsed = -1.0
|
|
|
|
def update(self, control: dict) -> float:
|
|
epoch = int(control.get("budget_epoch", 0))
|
|
if epoch > self.epoch:
|
|
self.previous_elapsed += self.segment.elapsed
|
|
self.segment = ExecutionBudget(self.limit, _last=time.monotonic())
|
|
self.epoch = epoch
|
|
self.pause_requested = False
|
|
excluded = control.get("status") in {
|
|
"queued",
|
|
"task_paused",
|
|
"resuming",
|
|
"waiting_user",
|
|
"demonstrating",
|
|
}
|
|
remaining = self.segment.update(excluded=excluded)
|
|
now = time.monotonic()
|
|
elapsed = self.previous_elapsed + self.segment.elapsed
|
|
if elapsed != self.last_elapsed and (
|
|
now - self.last_publish >= 1 or (remaining <= 0 and not self.pause_requested)
|
|
):
|
|
self.repository.record_elapsed(self.run_id, elapsed, self.epoch)
|
|
self.last_publish = now
|
|
self.last_elapsed = elapsed
|
|
if remaining <= 0 and not self.pause_requested:
|
|
request_pause(self.run_id, reason="execution_budget")
|
|
self.pause_requested = True
|
|
return remaining
|