fix(world-model): scope WorldModel state per-task to prevent cross-task contamination
WorldModel routed all state through a single mutable _current_task_id, so concurrent tasks sharing one instance could corrupt each other's state. start_task() now returns a TaskWorldView handle scoped to that task; TaskRunner.run() threads it through as a local variable instead of reading self.world_model implicitly. Also extracts _start_world_view/ _record_step_result as reusable TaskRunner methods for composed runners. openspec: world-model capability, archived change world-model-runtime
This commit is contained in:
+84
-14
@@ -24,13 +24,23 @@ class WorldModel:
|
||||
|
||||
@property
|
||||
def state(self) -> WorldState:
|
||||
"""Convenience accessor for single-active-task usage only.
|
||||
|
||||
Relies on the shared ``_current_task_id`` pointer, so it is not
|
||||
safe when multiple tasks run concurrently against the same
|
||||
``WorldModel`` instance (a later ``start_task()`` call from another
|
||||
task can move this pointer out from under you). Concurrent callers
|
||||
must use the ``TaskWorldView`` returned by ``start_task()`` (or
|
||||
``state_for(task_id)``) instead.
|
||||
"""
|
||||
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:
|
||||
def start_task(self, task_id: str) -> "TaskWorldView":
|
||||
self._current_task_id = task_id
|
||||
return self._states.setdefault(task_id, self._new_state())
|
||||
state = self._states.setdefault(task_id, self._new_state())
|
||||
return TaskWorldView(self, task_id, state)
|
||||
|
||||
def state_for(self, task_id: str) -> WorldState | None:
|
||||
return self._states.get(task_id)
|
||||
@@ -41,26 +51,51 @@ class WorldModel:
|
||||
semantic_scene: SemanticScene | None,
|
||||
step: "PlannedStep",
|
||||
result: "StepResult",
|
||||
) -> None:
|
||||
"""Update the shared "current task" state. See `state`'s docstring."""
|
||||
self._apply(self.state, scene, semantic_scene, step, result)
|
||||
|
||||
def observe_task(
|
||||
self,
|
||||
task_id: str,
|
||||
scene: Scene,
|
||||
semantic_scene: SemanticScene | None,
|
||||
step: "PlannedStep",
|
||||
result: "StepResult",
|
||||
) -> None:
|
||||
"""Update `task_id`'s own state explicitly, independent of any other task."""
|
||||
state = self._states.setdefault(task_id, self._new_state())
|
||||
self._apply(state, scene, semantic_scene, step, result)
|
||||
|
||||
def _apply(
|
||||
self,
|
||||
state: WorldState,
|
||||
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)
|
||||
self._update_page(state, semantic_scene)
|
||||
self._update_app(state, step, result)
|
||||
self._update_variables(state, step)
|
||||
self._append_history(state, 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:
|
||||
def _update_page(self, state: WorldState, semantic_scene: SemanticScene | None) -> None:
|
||||
if semantic_scene is None:
|
||||
return
|
||||
page = semantic_scene.page.strip()
|
||||
if page:
|
||||
self.state.current_page = page
|
||||
state.current_page = page
|
||||
|
||||
def _update_app(self, step: "PlannedStep", result: "StepResult") -> None:
|
||||
def _update_app(
|
||||
self, state: WorldState, step: "PlannedStep", result: "StepResult"
|
||||
) -> None:
|
||||
if not getattr(result, "success", False):
|
||||
return
|
||||
action = getattr(step, "action", None)
|
||||
@@ -76,11 +111,11 @@ class WorldModel:
|
||||
logger.info("world model skipped app update: missing app identifier")
|
||||
return
|
||||
if action == "launch_app":
|
||||
self.state.current_app = app_id
|
||||
state.current_app = app_id
|
||||
return
|
||||
self.state.current_app = None
|
||||
state.current_app = None
|
||||
|
||||
def _update_variables(self, step: "PlannedStep") -> None:
|
||||
def _update_variables(self, state: WorldState, step: "PlannedStep") -> None:
|
||||
args = getattr(step, "args", {})
|
||||
if not isinstance(args, dict) or "remember" not in args:
|
||||
return
|
||||
@@ -88,16 +123,17 @@ class WorldModel:
|
||||
if not isinstance(remember, dict):
|
||||
logger.info("world model skipped remember update: value is not a mapping")
|
||||
return
|
||||
self.state.variables.update(remember)
|
||||
state.variables.update(remember)
|
||||
|
||||
def _append_history(
|
||||
self,
|
||||
state: WorldState,
|
||||
scene: Scene,
|
||||
semantic_scene: SemanticScene | None,
|
||||
step: "PlannedStep",
|
||||
result: "StepResult",
|
||||
) -> None:
|
||||
self.state.history.append(
|
||||
state.history.append(
|
||||
WorldEvent(
|
||||
scene_summary=semantic_scene or scene,
|
||||
action=str(getattr(step, "action", "unknown")),
|
||||
@@ -106,6 +142,40 @@ class WorldModel:
|
||||
)
|
||||
|
||||
|
||||
class TaskWorldView:
|
||||
"""Per-task handle into a `WorldModel`, scoped explicitly by `task_id`.
|
||||
|
||||
Returned by `WorldModel.start_task()` so a caller (e.g. `TaskRunner`)
|
||||
can keep observing/reading its own task's `WorldState` for the rest of
|
||||
that task's lifetime by holding onto this handle, rather than relying on
|
||||
`WorldModel`'s shared "current task" pointer. This makes it safe for
|
||||
multiple tasks to run concurrently against the same `WorldModel`
|
||||
instance without corrupting each other's state.
|
||||
"""
|
||||
|
||||
def __init__(self, model: WorldModel, task_id: str, state: WorldState) -> None:
|
||||
self._model = model
|
||||
self._task_id = task_id
|
||||
self._state = state
|
||||
|
||||
@property
|
||||
def task_id(self) -> str:
|
||||
return self._task_id
|
||||
|
||||
@property
|
||||
def state(self) -> WorldState:
|
||||
return self._state
|
||||
|
||||
def observe(
|
||||
self,
|
||||
scene: Scene,
|
||||
semantic_scene: SemanticScene | None,
|
||||
step: "PlannedStep",
|
||||
result: "StepResult",
|
||||
) -> None:
|
||||
self._model._apply(self._state, scene, semantic_scene, step, result)
|
||||
|
||||
|
||||
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