feat: checkpoint device agent runtime milestones
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from core.models import Scene
|
||||
from semantic.models import SemanticScene
|
||||
from world.config import WorldConfig, load_config
|
||||
from world.models import WorldEvent, WorldState
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from runtime.executor import StepResult
|
||||
from runtime.planner import PlannedStep
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WorldModel:
|
||||
def __init__(self, *, config: WorldConfig | None = None) -> None:
|
||||
self.config = config or load_config()
|
||||
self._states: dict[str, WorldState] = {}
|
||||
self._current_task_id: str | None = None
|
||||
self._fallback_state = self._new_state()
|
||||
|
||||
@property
|
||||
def state(self) -> WorldState:
|
||||
if self._current_task_id is None:
|
||||
return self._fallback_state
|
||||
return self._states[self._current_task_id]
|
||||
|
||||
def start_task(self, task_id: str) -> WorldState:
|
||||
self._current_task_id = task_id
|
||||
return self._states.setdefault(task_id, self._new_state())
|
||||
|
||||
def state_for(self, task_id: str) -> WorldState | None:
|
||||
return self._states.get(task_id)
|
||||
|
||||
def observe(
|
||||
self,
|
||||
scene: Scene,
|
||||
semantic_scene: SemanticScene | None,
|
||||
step: "PlannedStep",
|
||||
result: "StepResult",
|
||||
) -> None:
|
||||
try:
|
||||
self._update_page(semantic_scene)
|
||||
self._update_app(step, result)
|
||||
self._update_variables(step)
|
||||
self._append_history(scene, semantic_scene, step, result)
|
||||
except Exception as exc:
|
||||
logger.info("world model update skipped after unexpected error: %s", exc)
|
||||
|
||||
def _new_state(self) -> WorldState:
|
||||
return WorldState.with_history_bound(self.config.history_size)
|
||||
|
||||
def _update_page(self, semantic_scene: SemanticScene | None) -> None:
|
||||
if semantic_scene is None:
|
||||
return
|
||||
page = semantic_scene.page.strip()
|
||||
if page:
|
||||
self.state.current_page = page
|
||||
|
||||
def _update_app(self, step: "PlannedStep", result: "StepResult") -> None:
|
||||
if not getattr(result, "success", False):
|
||||
return
|
||||
action = getattr(step, "action", None)
|
||||
if action not in {"launch_app", "terminate_app"}:
|
||||
return
|
||||
|
||||
args = getattr(step, "args", {})
|
||||
if not isinstance(args, dict):
|
||||
logger.info("world model skipped app update: step args are not a mapping")
|
||||
return
|
||||
app_id = _app_identifier(args)
|
||||
if not app_id:
|
||||
logger.info("world model skipped app update: missing app identifier")
|
||||
return
|
||||
if action == "launch_app":
|
||||
self.state.current_app = app_id
|
||||
return
|
||||
self.state.current_app = None
|
||||
|
||||
def _update_variables(self, step: "PlannedStep") -> None:
|
||||
args = getattr(step, "args", {})
|
||||
if not isinstance(args, dict) or "remember" not in args:
|
||||
return
|
||||
remember = args["remember"]
|
||||
if not isinstance(remember, dict):
|
||||
logger.info("world model skipped remember update: value is not a mapping")
|
||||
return
|
||||
self.state.variables.update(remember)
|
||||
|
||||
def _append_history(
|
||||
self,
|
||||
scene: Scene,
|
||||
semantic_scene: SemanticScene | None,
|
||||
step: "PlannedStep",
|
||||
result: "StepResult",
|
||||
) -> None:
|
||||
self.state.history.append(
|
||||
WorldEvent(
|
||||
scene_summary=semantic_scene or scene,
|
||||
action=str(getattr(step, "action", "unknown")),
|
||||
success=bool(getattr(result, "success", False)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _app_identifier(args: dict[str, Any]) -> str | None:
|
||||
value = args.get("bundle_id") or args.get("app_id")
|
||||
return value if isinstance(value, str) and value.strip() else None
|
||||
Reference in New Issue
Block a user