Files
Artemis-Eapil/artemis/tools/mobile/log_utils.py
T
somew1nd 206a9ce921 feat: initial release of ARTEMIS Android Agent & Automation Platform
ARTEMIS is an autonomous multimodal Android agent and mobile UI automation
platform engineered for physical devices and emulators. It combines state-of-the-art
multimodal Vision-Language Models (VLMs) with hierarchical closed-loop planning,
resilient self-healing, and deep Android subsystem integration.

Key Features & Architectural Highlights:
- Dual-Engine Orchestration:
  * Pro Engine: Multi-agent closed-loop (Planner -> Operator -> Validator/Failure Analyzer -> Summarizer) powered by LangGraph.
  * Flash Engine: Ultra-fast reactive loop for deterministic, low-latency UI execution (<35 steps).
- Multimodal Perception & Grounding:
  * Hybrid visual screenshot grounding, real-time OCR extraction, and XML accessibility hierarchy parsing.
  * Dynamic-First, Coordinate-Fallback locating strategy for maximum resilience against UI drift.
- Autonomous Self-Healing & Diagnostics:
  * Failure analyzer and safety net for popup dismissal, crash recovery, and auto-retry.
  * Diagnostic sub-agents for log analysis, video recording inspection, and object detection.
- Unified CLI & Python SDK:
  * Rich CLI with commands: init, doctor, run, trace, batch, bench, server, and mcp.
  * Fluent Python SDK with Pydantic structured output models and package locking.
- Multi-Provider LLM Architecture:
  * Support for Google Gemini, OpenAI, Anthropic Claude, Vertex AI, OpenRouter, xAI Grok, and local endpoints (Ollama/vLLM).
  * Centralized configuration in config/artemis.jsonc.
- Native MCP & Observability:
  * Model Context Protocol (MCP) server endpoints for AI IDEs.
  * Full trace recording with before/after screenshots, action overlays, and execution replays.

Change-Id: I430b93ee2b76b54cc5a3c6eeccd15d2cf29830a1
2026-08-13 11:46:47 -07:00

139 lines
4.8 KiB
Python

# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime, timedelta
import time
from artemis.context import ArtemisContext
from artemis.controllers.platform_specific_commands_controller import get_adb_device
from artemis.utils.logger import get_logger
logger = get_logger(__name__)
def parse_log_time(time_str: str) -> datetime:
"""Parses a log timestamp string into a datetime object."""
now = datetime.now()
current_year = now.year
def _try_parse(year: int) -> datetime | None:
for fmt in ("%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%d %H:%M:%S"):
try:
return datetime.strptime(f"{year}-{time_str}", fmt)
except ValueError:
pass
return None
dt = _try_parse(current_year)
if dt is None:
raise ValueError(f"Unsupported time format: {time_str}")
# If parsed time is in the future (allowing 1 hour grace for clock drift),
# we assume it belongs to the previous year.
if dt > now + timedelta(hours=1):
prev_dt = _try_parse(current_year - 1)
if prev_dt is not None:
dt = prev_dt
return dt
def resolve_time(
time_input: str | float | None, session_start: float | None
) -> tuple[str | None, datetime | None]:
"""Resolves relative or absolute time input into formatted string and datetime."""
if time_input is None:
return None, None
if isinstance(time_input, (int, float)) or (
isinstance(time_input, str) and time_input.replace(".", "", 1).isdigit()
):
# It's relative seconds
if session_start is None:
raise ValueError("Cannot resolve relative time without session start time.")
rel_sec = float(time_input)
abs_ts = session_start + rel_sec
t_struct = time.localtime(abs_ts)
t_str = time.strftime("%m-%d %H:%M:%S", t_struct)
ms = int((abs_ts - int(abs_ts)) * 1000)
formatted_str = f"{t_str}.{ms:03d}"
dt = datetime.fromtimestamp(abs_ts)
return formatted_str, dt
# Assume it's absolute time string "MM-DD HH:MM:SS.ms"
try:
dt = parse_log_time(str(time_input))
except ValueError as e:
raise ValueError(f"Unsupported time format: {time_input}") from e
return str(time_input), dt
# pylint: disable=too-many-locals
def fetch_and_filter_logs(
ctx: ArtemisContext,
lines: int = 1000,
since_time: str | None = None,
until_time: str | None = None,
) -> str:
"""Fetches logs from ADB device and filters them by time if needed."""
device = get_adb_device(ctx)
session_start = ctx.data_engine.session_start_time if ctx.data_engine else None
since_str, since_dt = resolve_time(since_time, session_start)
_, until_dt = resolve_time(until_time, session_start)
if since_str:
try:
logger.info(f"Attempting to fetch logs since {since_str}...")
logs = device.shell(f'logcat -v threadtime -t "{since_str}"')
except Exception as e: # pylint: disable=broad-exception-caught
logger.warning(
f"Failed to fetch logs since time directly: {e}. Falling back"
" to reading lines and filtering."
)
logs = device.shell(f"logcat -v threadtime -t {lines}")
else:
logger.info(f"Reading last {lines} lines of logs...")
logs = device.shell(f"logcat -v threadtime -t {lines}")
if since_dt or until_dt:
filtered_lines = []
for line in logs.splitlines():
if len(line) >= 18:
log_time_str = line[:18]
try:
log_dt = parse_log_time(log_time_str)
if since_dt and log_dt < since_dt:
continue
if until_dt and log_dt > until_dt:
continue
filtered_lines.append(line)
except ValueError:
pass
# Cap to the requested line limit so timestamp fetches don't overflow context
if 0 < lines < len(filtered_lines):
filtered_lines = filtered_lines[-lines:]
logs = "\n".join(filtered_lines)
else:
# Ensure fallback capping even when no datetime parsing occurred
log_lines = logs.splitlines()
if 0 < lines < len(log_lines):
logs = "\n".join(log_lines[-lines:])
return logs