Host Agent now persists step-level execution detail locally (via a real TaskMetadataStore/Timeline wired into TaskRunner) and reports a bounded in-progress snapshot piggybacked on lease renewal. Cloud persists that snapshot per active assignment and exposes it through the existing task list/detail query path; Cloud Console renders it as a live badge. Host Agent's local console gains authenticated, read-only task list and detail/timeline pages (same-origin, server-rendered) with inlined screenshots. Also fixes a pre-existing gap in the shared Timeline: the actual per-step LLM prompt is now recorded instead of the task goal, benefiting both Runtime and Host Agent consoles. When a host uses the cloud planner transport, each decide call's prompt and resulting tool decision are durably logged in a new planner_decision_log table (with bounded retention) and browsable from Cloud Console; direct-transport hosts explicitly surface a "not reported" state. Includes Alembic migrations 0008 (progress columns on scheduled_tasks) and 0009 (planner_decision_log), bounded Host-Agent-local retention, dual-backend repository parity, and Vitest + pytest coverage. Task 6.5 (manual end-to-end device verification) remains. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from core.errors import TaskFailedError
|
|
from core.models import Scene
|
|
from runtime.context import TaskContext
|
|
from runtime.planner import PlannedStep, Planner
|
|
from runtime.planner_config import PlannerConfig, load_config
|
|
from runtime.planner_prompts import PLANNER_SYSTEM_PROMPT, planner_user_prompt
|
|
from runtime.tool_calling_client import ToolCallingClient, build_client
|
|
from runtime.tool_specs import ALL_TOOL_SPECS
|
|
|
|
if TYPE_CHECKING:
|
|
from world.models import WorldState
|
|
|
|
FINISH_TASK_TOOL = "finish_task"
|
|
|
|
|
|
class AIPlanner(Planner):
|
|
def __init__(
|
|
self,
|
|
*,
|
|
client: ToolCallingClient | None = None,
|
|
config: PlannerConfig | None = None,
|
|
) -> None:
|
|
self.config = config or load_config()
|
|
self.client = client or build_client(self.config)
|
|
|
|
def plan(
|
|
self,
|
|
*,
|
|
goal: str,
|
|
scene: Scene,
|
|
context: TaskContext,
|
|
world: "WorldState | None" = None,
|
|
screenshot: bytes | None = None,
|
|
) -> list[PlannedStep]:
|
|
user_prompt = planner_user_prompt(
|
|
goal=goal,
|
|
scene_json=scene.to_dict(),
|
|
history_summary=_history_summary(world),
|
|
)
|
|
decision = self.client.decide(
|
|
system_prompt=PLANNER_SYSTEM_PROMPT,
|
|
user_prompt=user_prompt,
|
|
screenshot=screenshot,
|
|
tools=ALL_TOOL_SPECS,
|
|
timeout=self.config.timeout,
|
|
)
|
|
|
|
if decision.tool_name == FINISH_TASK_TOOL:
|
|
if decision.arguments.get("success"):
|
|
return []
|
|
raise TaskFailedError(decision.arguments.get("reason") or "task failed")
|
|
|
|
return [
|
|
PlannedStep(
|
|
action=decision.tool_name,
|
|
description=f"AI planner: {decision.tool_name}({decision.arguments})",
|
|
args=dict(decision.arguments),
|
|
prompt=decision.user_prompt or user_prompt,
|
|
)
|
|
]
|
|
|
|
def goal_reached(self, *, goal: str, scene: Scene, context: TaskContext) -> bool:
|
|
# Completion is signaled exclusively via the finish_task tool call
|
|
# (mapped to an empty plan above), never via this hook.
|
|
return False
|
|
|
|
|
|
def _history_summary(world: "WorldState | None") -> list[dict[str, Any]]:
|
|
if world is None:
|
|
return []
|
|
return [event.to_dict() for event in world.history]
|