Files
agentic-mobile-control/apps/device-host-agent/host_agent/retention.py
T
q792602257andClaude Opus 4.6 ec261d57c2 feat: surface task execution progress across Host Agent and Cloud
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>
2026-07-14 12:47:49 +08:00

64 lines
1.9 KiB
Python

from __future__ import annotations
import logging
from datetime import UTC, datetime, timedelta
from host_agent.config import HostAgentConfig
from storage.task_metadata import TaskMetadataStore
from storage.timeline import Timeline
logger = logging.getLogger(__name__)
def prune_task_history(
metadata_store: TaskMetadataStore,
timeline: Timeline,
*,
config: HostAgentConfig,
now: datetime | None = None,
) -> int:
"""Delete tasks beyond the configured retention window/count.
Computes two candidate retained sets -- the last ``max_count`` tasks
and tasks younger than ``max_age_days`` -- and keeps whichever set
is smaller (i.e. the more restrictive bound always wins).
Returns the number of tasks pruned.
"""
reference = now or datetime.now(UTC)
tasks = metadata_store.list_tasks()
keep_by_count = {task["id"] for task in tasks[: config.task_retention_max_count]}
cutoff = reference - timedelta(days=config.task_retention_max_age_days)
keep_by_age = {
task["id"] for task in tasks if _parse_timestamp(task["created_at"]) >= cutoff
}
keep_ids = keep_by_count if len(keep_by_count) <= len(keep_by_age) else keep_by_age
pruned = 0
for task in tasks:
if task["id"] not in keep_ids:
_safe_delete(timeline, metadata_store, task["id"])
pruned += 1
return pruned
def _safe_delete(
timeline: Timeline, metadata_store: TaskMetadataStore, task_id: str
) -> None:
try:
timeline.delete_task(task_id)
except Exception:
logger.debug("timeline delete failed for task %s", task_id, exc_info=True)
try:
metadata_store.delete_task(task_id)
except Exception:
logger.debug("metadata delete failed for task %s", task_id, exc_info=True)
def _parse_timestamp(value: str) -> datetime:
parsed = datetime.fromisoformat(value)
if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=UTC)
return parsed