fix: improve cross-device evidence, request diagnostics and prompt efficiency
This commit is contained in:
1 parent
621f30e437
commit
a66803651a
60 files changed
+3097
-160
No files matched your search
@@ -42,7 +42,7 @@ from typing import Any, Literal
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
|
||||
from langchain_core.tools import BaseTool, tool
|
||||
|
||||
from pydantic import BaseModel, Field, ValidationError
|
||||
from pydantic import BaseModel, Field, ValidationError, field_validator
|
||||
|
||||
from artemis.core.tool_failure import ToolFailure, is_tool_failure
|
||||
from artemis.constants import CHECKER_MAX_ITERATIONS
|
||||
@@ -50,6 +50,8 @@ from artemis.runtime.case_result import CaseTestResult, normalize_case_result
|
||||
from artemis.context import ArtemisContext
|
||||
from artemis.data_engine.context_vars import CURRENT_TRACE_ID
|
||||
from artemis.data_engine.trace import trace, trace_langchain_tool
|
||||
from artemis.llm.parser import extract_thinking_tags
|
||||
from artemis.llm.structured import content_to_text
|
||||
from artemis.services.llm import acomplete, get_llm, invoke_llm_with_timeout_message
|
||||
from artemis.tools.history import get_history_tools
|
||||
from artemis.tools.scratchpad import get_read_note_tool_pure
|
||||
@@ -111,6 +113,20 @@ class CheckReport(BaseModel):
|
||||
),
|
||||
)
|
||||
|
||||
@field_validator("test_result", mode="before")
|
||||
@classmethod
|
||||
def decode_test_result(cls, value):
|
||||
"""Some compatible endpoints serialize a nested object a second time.
|
||||
|
||||
Decode only valid JSON here; the ordinary result schema still checks
|
||||
status, reason and evidence. Prose and malformed JSON remain errors.
|
||||
"""
|
||||
if isinstance(value, str):
|
||||
value = json.loads(value)
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError("test_result must encode a JSON object")
|
||||
return value
|
||||
|
||||
|
||||
def verdicts_allow_release(report: CheckReport) -> bool:
|
||||
"""Node-side release decision: every verify verdict passed or inconclusive.
|
||||
@@ -413,7 +429,41 @@ def _format_check_items(check_items: list) -> str:
|
||||
# --- Shared tool loop ----------------------------------------------------------------
|
||||
|
||||
|
||||
async def _structured_report(llm, messages) -> CheckReport:
|
||||
def _completed_text_report(response, check_items) -> CheckReport | None:
|
||||
"""Reuse a complete Checker verdict, never a draft, tool receipt or partial stream."""
|
||||
if not isinstance(response, AIMessage) or response.tool_calls or response.invalid_tool_calls:
|
||||
return None
|
||||
metadata = response.response_metadata
|
||||
if metadata.get("finish_reason") in {"length", "content_filter"} or metadata.get("status") in {
|
||||
"incomplete",
|
||||
"failed",
|
||||
"cancelled",
|
||||
"in_progress",
|
||||
}:
|
||||
return None
|
||||
text, _ = extract_thinking_tags(content_to_text(response.content))
|
||||
fences = re.findall(r"```(?:json)?\s*([\s\S]*?)```", text, flags=re.IGNORECASE)
|
||||
if len(fences) > 1:
|
||||
return None # Multiple candidate reports are ambiguous, not a final verdict.
|
||||
candidate = fences[0].strip() if fences else text.strip()
|
||||
try:
|
||||
report = CheckReport.model_validate(json.loads(candidate))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
# Empty/default-valued JSON is not a verdict. Require the overall result
|
||||
# and every declared check so an incomplete answer still gets formatted.
|
||||
if report.test_result is None or not report.test_result.reason.strip():
|
||||
return None
|
||||
expected = {(ci.kind, ci.text) for ci in check_items}
|
||||
covered = {(v.kind, v.item_text) for v in report.verdicts}
|
||||
if not expected <= covered:
|
||||
return None
|
||||
return report
|
||||
|
||||
|
||||
async def _structured_report(llm, messages, check_items=()) -> CheckReport:
|
||||
if messages and (report := _completed_text_report(messages[-1], check_items)) is not None:
|
||||
return report
|
||||
structured_llm = llm.with_structured_output(CheckReport)
|
||||
# A conversation must end with a user turn: Gemini rejects requests whose
|
||||
# last message is the model's own answer ("Requests ending with a model
|
||||
@@ -496,14 +546,14 @@ async def _run_check_loop(
|
||||
)
|
||||
)
|
||||
)
|
||||
report = await _structured_report(llm, messages)
|
||||
report = await _structured_report(llm, messages, check_items)
|
||||
break
|
||||
|
||||
active_llm = llm.bind_tools(tools=traced_tools)
|
||||
response = await invoke_llm_with_timeout_message(acomplete(active_llm, messages))
|
||||
|
||||
if not response.tool_calls:
|
||||
report = await _structured_report(llm, messages + [response])
|
||||
report = await _structured_report(llm, messages + [response], check_items)
|
||||
break
|
||||
|
||||
messages.append(response)
|
||||
|
||||
@@ -143,7 +143,11 @@ class ScrubEdgeCompressor:
|
||||
self._xml_candidates: list[int] = []
|
||||
|
||||
def compress(
|
||||
self, messages: list[BaseMessage], *, image_scrub_depth: int | None = None
|
||||
self,
|
||||
messages: list[BaseMessage],
|
||||
*,
|
||||
image_scrub_depth: int | None = None,
|
||||
protected_message_ids: set[int] | None = None,
|
||||
) -> None:
|
||||
"""Advance the scrub edge over newly appended messages in-place.
|
||||
|
||||
@@ -156,7 +160,7 @@ class ScrubEdgeCompressor:
|
||||
self._discover(messages)
|
||||
if not self._tracked and not self._xml_candidates:
|
||||
return
|
||||
self._scrub_edge(messages, image_scrub_depth)
|
||||
self._scrub_edge(messages, image_scrub_depth, protected_message_ids)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Discovery (incremental; frozen prefix is never rescanned)
|
||||
@@ -211,7 +215,10 @@ class ScrubEdgeCompressor:
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _scrub_edge(
|
||||
self, messages: list[BaseMessage], image_scrub_depth: int | None = None
|
||||
self,
|
||||
messages: list[BaseMessage],
|
||||
image_scrub_depth: int | None = None,
|
||||
protected_message_ids: set[int] | None = None,
|
||||
) -> None:
|
||||
total = len(self._tracked)
|
||||
scrub_depth = max(1, int(image_scrub_depth or self._image_scrub_depth))
|
||||
@@ -219,6 +226,8 @@ class ScrubEdgeCompressor:
|
||||
text_depth = self._xml_scrub_depth
|
||||
|
||||
for pos, rec in enumerate(self._tracked):
|
||||
if protected_message_ids and id(messages[rec["idx"]]) in protected_message_ids:
|
||||
continue # A newly delivered tool result must be seen before it can be scrubbed.
|
||||
if rec["idx"] in self._frozen:
|
||||
continue
|
||||
# 1 == most recent image-bearing message; a live tail outside the
|
||||
|
||||
@@ -57,4 +57,5 @@ Keep in mind that the mobile device is an asynchronous, constantly evolving envi
|
||||
|
||||
# 6. TERMINATION PROTOCOL (`report_task_status`)
|
||||
- **Completion (`status="completed"`)**: ONLY call `report_task_status(status="completed", ...)` after the latest screenshot and UI elements support the claimed result. If those sources disagree during a transition, wait and observe again before concluding. Tie each claimed page title or value to an observed screenshot or UI element; mark any missing evidence as unverified.
|
||||
- **Deliver the requested output**: Completing device actions does not complete a requested report. Before finishing, check the original goal and user guidance for required deliverables. Put the actual report/answer in the explanation field, covering the requested sections with observed findings, source-specific evidence and untested or blocked scope. Do not replace it with "completed" or a promise to provide it later. The short-paragraph rule above applies to turn commentary, not to this final deliverable. Use the existing record; retrieve only missing evidence instead of repeating the whole test. Do not invent findings or treat finding a product defect as execution failure when the goal is to investigate and report it.
|
||||
- **Failure (`status="failed"`)**: If the task is genuinely impossible due to paywalls, unrecoverable crashes, or hitting an absolute dead end after sensible fallbacks, call `report_task_status(status="failed", ...)` with the exact blocking reason.
|
||||
@@ -147,6 +147,7 @@ class FlashRunner:
|
||||
self.ctx = ctx
|
||||
self.goal = goal
|
||||
self.final_report_received = False
|
||||
self._android_observation_stale = False
|
||||
try:
|
||||
cfg = load_agent_config()
|
||||
self.max_turns = max_turns if max_turns is not None else cfg.flash.max_turns
|
||||
@@ -746,6 +747,9 @@ class FlashRunner:
|
||||
"error_code",
|
||||
"attachments",
|
||||
"recording",
|
||||
"pre_image_name",
|
||||
"post_image_name",
|
||||
"pre_image_valid",
|
||||
)
|
||||
if key in metadata
|
||||
}
|
||||
@@ -865,6 +869,13 @@ class FlashRunner:
|
||||
# A phone observation cannot serve as the before-image for a desktop action.
|
||||
pre_screenshot_bytes = plugins.previous_image(name, args)
|
||||
xml_list = ""
|
||||
if getattr(self.ctx, "execution_target", "android") != "computer":
|
||||
# Invalidate before dispatch: even a failed/interrupted PC
|
||||
# action may have changed the environment or the phone.
|
||||
self._android_observation_stale = True
|
||||
state.indexed_elements = []
|
||||
state.indexed_points = []
|
||||
state.latest_screenshot = None
|
||||
control = getattr(self.ctx, "task_control", None)
|
||||
if plugins and name in plugins.tool_names and isinstance(control, TaskControl):
|
||||
interrupted, exec_result = await control.run_interruptibly(
|
||||
@@ -936,6 +947,11 @@ class FlashRunner:
|
||||
exec_outcome=exec_result.text_summary,
|
||||
post_ui_elements_text=exec_result.ui_elements_text,
|
||||
observation_settled=(exec_result.metadata or {}).get("observation_settled"),
|
||||
receipt_only=bool(
|
||||
plugin_action
|
||||
and exec_result.metadata.get("read_only") is True
|
||||
and exec_result.status == "success"
|
||||
),
|
||||
action_key=str(tc_id),
|
||||
data_engine_step_id=recorded_step_id,
|
||||
# The model's own reasoning for this turn steers the lens's
|
||||
@@ -988,16 +1004,41 @@ class FlashRunner:
|
||||
)
|
||||
)
|
||||
if plugin_action and getattr(self.ctx, "execution_target", "android") != "computer":
|
||||
# The loop's observation always belongs to Android in a mixed task.
|
||||
# Plugin images travel in their labelled tool result and step record;
|
||||
# never pair a desktop bitmap with the phone's element indexes.
|
||||
shot_path, observation_image, observation_text = await capture_screenshot_and_parse_ui(
|
||||
self.ctx, state, self.controller
|
||||
)
|
||||
state.latest_screenshot = shot_path
|
||||
return observation_image, observation_text, action_sequence
|
||||
# The labelled tool result already carries the PC evidence. Do not
|
||||
# capture an idle phone or duplicate the PC bitmap in the phone tail.
|
||||
return None, self._plugin_observation_notice(), action_sequence
|
||||
return pre_screenshot_bytes, xml_list, action_sequence
|
||||
|
||||
@staticmethod
|
||||
def _plugin_observation_notice() -> str:
|
||||
return (
|
||||
"当前使用插件观察;实际画面和控件见对应工具返回,不是安卓画面。"
|
||||
"安卓旧截图、元素编号和坐标已失效。连续操作电脑无需重复观察手机;"
|
||||
"切回安卓时先单独调用 wait_for_delay(time_in_ms=0) 读取最新手机画面,"
|
||||
"收到后再决定手机动作。不要将电脑控件编号用于手机。"
|
||||
)
|
||||
|
||||
async def _refresh_observation(self, state: State):
|
||||
shot_path, image, elements = await capture_screenshot_and_parse_ui(
|
||||
self.ctx, state, self.controller, skip_settling=False
|
||||
)
|
||||
state.latest_screenshot = shot_path
|
||||
self._android_observation_stale = False
|
||||
return image, elements
|
||||
|
||||
@staticmethod
|
||||
def _defer_tool_calls(messages: list[BaseMessage], tool_calls: list) -> None:
|
||||
"""Close every pending call without executing decisions made on stale evidence."""
|
||||
for tc in tool_calls:
|
||||
messages.append(
|
||||
ToolMessage(
|
||||
tool_call_id=tc.get("id") or str(uuid.uuid4()),
|
||||
name=tc["name"],
|
||||
content="未执行此调用:已切回安卓并重新观察。请先读取最新手机画面和控件,再重新决定动作或结论。",
|
||||
status="error",
|
||||
)
|
||||
)
|
||||
|
||||
async def _invoke_model(self, llm, current_tools: list, messages: list[BaseMessage]):
|
||||
"""Binds the active tools and invokes the model through the LLM gateway."""
|
||||
# Bind active tools
|
||||
@@ -1048,16 +1089,31 @@ class FlashRunner:
|
||||
final_report is non-None only when 'report_task_status' was called.
|
||||
"""
|
||||
action_names = self.executor.action_tool_names | {"android_file"}
|
||||
from artemis.plugins.runtime import get_plugin_runtime
|
||||
|
||||
plugins = get_plugin_runtime(self.ctx)
|
||||
android_tools = action_names | {"ask_explorer"}
|
||||
if plugins:
|
||||
android_tools -= plugins.tool_names
|
||||
index_elements = self._turn_index_snapshot(state)
|
||||
for tc in tool_calls:
|
||||
for call_index, tc in enumerate(tool_calls):
|
||||
control = getattr(self.ctx, "task_control", None)
|
||||
if isinstance(control, TaskControl) and has_pending_instruction(control.directory):
|
||||
return None, pre_screenshot_bytes, xml_list, action_sequence
|
||||
name = tc["name"].split(":")[-1] if ":" in tc["name"] else tc["name"]
|
||||
args = tc.get("args") or {}
|
||||
tc_id = tc.get("id") or str(uuid.uuid4())
|
||||
logger.info(f"Executing Flash tool: {name}({args})")
|
||||
|
||||
returning_to_android = (
|
||||
getattr(self, "_android_observation_stale", False) and name in android_tools
|
||||
)
|
||||
if returning_to_android and name != "wait_for_delay":
|
||||
logger.info(f"Flash 暂缓 {name}:切回安卓,先刷新画面并重新决定动作。")
|
||||
pre_screenshot_bytes, xml_list = await self._refresh_observation(state)
|
||||
self._defer_tool_calls(messages, tool_calls[call_index:])
|
||||
return None, pre_screenshot_bytes, xml_list, action_sequence
|
||||
|
||||
logger.info(f"Executing Flash tool: {name}({args})")
|
||||
if name == "report_task_status":
|
||||
final_report = await self._finalize_task_report(
|
||||
name,
|
||||
@@ -1090,6 +1146,15 @@ class FlashRunner:
|
||||
native_text=native_text,
|
||||
index_elements=index_elements,
|
||||
)
|
||||
if returning_to_android:
|
||||
# wait_for_delay has no stale target and its normal post-action
|
||||
# observation is fresh. The model must see it before any further
|
||||
# calls (including a report) from this pre-observation batch.
|
||||
if pre_screenshot_bytes is None:
|
||||
pre_screenshot_bytes, xml_list = await self._refresh_observation(state)
|
||||
self._android_observation_stale = False
|
||||
self._defer_tool_calls(messages, tool_calls[call_index + 1 :])
|
||||
return None, pre_screenshot_bytes, xml_list, action_sequence
|
||||
if name not in action_names:
|
||||
index_elements = self._extend_index_snapshot(state, index_elements, before_len)
|
||||
return None, pre_screenshot_bytes, xml_list, action_sequence
|
||||
@@ -1135,10 +1200,7 @@ class FlashRunner:
|
||||
)
|
||||
|
||||
# Capture Initial State (Screenshot + UI Tree)
|
||||
shot_path, img_bytes, xml_list = await capture_screenshot_and_parse_ui(
|
||||
self.ctx, state, self.controller, skip_settling=False
|
||||
)
|
||||
state.latest_screenshot = shot_path
|
||||
img_bytes, xml_list = await self._refresh_observation(state)
|
||||
return ledger, img_bytes, xml_list
|
||||
|
||||
@staticmethod
|
||||
@@ -1196,6 +1258,7 @@ class FlashRunner:
|
||||
fallback_llm = None
|
||||
using_tool_fallback = False
|
||||
action_failure_attempts = 0
|
||||
refreshed_for_guidance = False
|
||||
|
||||
while limit is None or turns < limit:
|
||||
control = getattr(self.ctx, "task_control", None)
|
||||
@@ -1210,13 +1273,10 @@ class FlashRunner:
|
||||
action_failure_attempts = 0
|
||||
self._llm = llm
|
||||
(
|
||||
shot_path,
|
||||
current_pre_screenshot_bytes,
|
||||
current_xml_list,
|
||||
) = await capture_screenshot_and_parse_ui(
|
||||
self.ctx, state, self.controller, skip_settling=False
|
||||
)
|
||||
state.latest_screenshot = shot_path
|
||||
) = await self._refresh_observation(state)
|
||||
refreshed_for_guidance = True
|
||||
pending_notices.append(
|
||||
"设备在暂停期间可能被其他任务操作过。已重新观察画面;"
|
||||
"请根据当前状态和新指导重新判断操作,勿沿用旧坐标。"
|
||||
@@ -1236,6 +1296,11 @@ class FlashRunner:
|
||||
injected, injected_guidance = await self._read_injected_instruction(
|
||||
ledger.elapsed_label()
|
||||
)
|
||||
if injected and not refreshed_for_guidance:
|
||||
current_pre_screenshot_bytes, current_xml_list = await self._refresh_observation(
|
||||
state
|
||||
)
|
||||
refreshed_for_guidance = False
|
||||
|
||||
# Tool restriction on the final turn (bounded loops only)
|
||||
is_final = limit is not None and turns == limit
|
||||
@@ -1283,14 +1348,10 @@ class FlashRunner:
|
||||
no_tool_attempts = 0
|
||||
action_failure_attempts = 0
|
||||
self._llm = llm
|
||||
(
|
||||
shot_path,
|
||||
current_pre_screenshot_bytes,
|
||||
current_xml_list,
|
||||
) = await capture_screenshot_and_parse_ui(
|
||||
self.ctx, state, self.controller, skip_settling=False
|
||||
current_pre_screenshot_bytes, current_xml_list = await self._refresh_observation(
|
||||
state
|
||||
)
|
||||
state.latest_screenshot = shot_path
|
||||
refreshed_for_guidance = True
|
||||
pending_notices.append(
|
||||
"新指导已打断旧模型步骤。请重新观察画面并按最新指导决定下一步。"
|
||||
)
|
||||
@@ -1326,14 +1387,10 @@ class FlashRunner:
|
||||
using_tool_fallback = False
|
||||
no_tool_attempts = 0
|
||||
self._llm = llm
|
||||
(
|
||||
shot_path,
|
||||
current_pre_screenshot_bytes,
|
||||
current_xml_list,
|
||||
) = await capture_screenshot_and_parse_ui(
|
||||
self.ctx, state, self.controller, skip_settling=False
|
||||
current_pre_screenshot_bytes, current_xml_list = await self._refresh_observation(
|
||||
state
|
||||
)
|
||||
state.latest_screenshot = shot_path
|
||||
refreshed_for_guidance = True
|
||||
pending_notices.append("新指导已收到;忽略旧模型动作,重新观察画面。")
|
||||
turns -= 1
|
||||
continue
|
||||
@@ -1421,12 +1478,9 @@ class FlashRunner:
|
||||
break
|
||||
# A fresh, valid observation is required before spending any
|
||||
# fallback model calls. Hardware recovery is not model retry.
|
||||
(
|
||||
shot_path,
|
||||
current_pre_screenshot_bytes,
|
||||
current_xml_list,
|
||||
) = await capture_screenshot_and_parse_ui(self.ctx, state, self.controller)
|
||||
state.latest_screenshot = shot_path
|
||||
current_pre_screenshot_bytes, current_xml_list = await self._refresh_observation(
|
||||
state
|
||||
)
|
||||
fallback_llm = get_llm(self.ctx, name="operator", use_fallback=True)
|
||||
using_tool_fallback = True
|
||||
self._llm = fallback_llm
|
||||
|
||||
@@ -29,6 +29,7 @@ import asyncio
|
||||
from artemis.config.settings import settings
|
||||
import base64
|
||||
from pathlib import Path
|
||||
import sqlite3
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
@@ -231,6 +232,7 @@ class VisualStepSummarizer(StepMemoryService):
|
||||
focus: dict[str, str] | None = None,
|
||||
post_ui_elements_text: str | None = None,
|
||||
observation_settled: bool | None = None,
|
||||
receipt_only: bool = False,
|
||||
) -> None:
|
||||
"""Dispatches an asynchronous summarization task without blocking the caller.
|
||||
|
||||
@@ -252,6 +254,16 @@ class VisualStepSummarizer(StepMemoryService):
|
||||
else:
|
||||
key = action_key if action_key is not None else step_number
|
||||
|
||||
if action_name.startswith("plug_"):
|
||||
from artemis.config.constants import PLUGIN_SUMMARY_TEXT_CHARS
|
||||
from artemis.plugins.prompt_budget import bounded_result_text
|
||||
|
||||
exec_outcome = bounded_result_text(exec_outcome, limit=PLUGIN_SUMMARY_TEXT_CHARS)
|
||||
if post_ui_elements_text:
|
||||
post_ui_elements_text = bounded_result_text(
|
||||
post_ui_elements_text, limit=PLUGIN_SUMMARY_TEXT_CHARS
|
||||
)
|
||||
|
||||
payload = {
|
||||
"step_number": step_number,
|
||||
"action_name": action_name,
|
||||
@@ -264,6 +276,30 @@ class VisualStepSummarizer(StepMemoryService):
|
||||
"post_ui_elements_text": post_ui_elements_text,
|
||||
"observation_settled": observation_settled,
|
||||
}
|
||||
if receipt_only:
|
||||
from artemis.config.constants import PLUGIN_SUMMARY_TEXT_CHARS
|
||||
from artemis.plugins.prompt_budget import bounded_result_text
|
||||
|
||||
summary = (
|
||||
f"步骤 {step_number}:只读观察 {action_name};未执行写操作。"
|
||||
"以下为工具返回摘录,截图仍在本步骤证据中;不代表视觉验收通过。\n"
|
||||
+ bounded_result_text(exec_outcome, limit=PLUGIN_SUMMARY_TEXT_CHARS)
|
||||
)
|
||||
# Evidence has already been persisted by the caller. As with a
|
||||
# completed visual summary, do not retain every frame in memory.
|
||||
payload.update(pre_img_bytes=None, post_img_bytes=None, focus=None)
|
||||
self._step_inputs[key] = payload
|
||||
self._summaries[key] = summary
|
||||
for alias in aliases:
|
||||
self._aliases[alias] = key
|
||||
if data_engine_step_id is not None and self.ctx.data_engine:
|
||||
try:
|
||||
self.ctx.data_engine.update_step_summary(
|
||||
data_engine_step_id, summary, status="ready", source="tool_receipt"
|
||||
)
|
||||
except (OSError, ValueError, sqlite3.Error) as de_err:
|
||||
logger.debug(f"DataEngine receipt summary update skipped: {de_err}")
|
||||
return
|
||||
self.submit(key, payload, aliases=aliases)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"main_template": "# 输出语言要求\n所有自行撰写的自然语言说明必须使用简体中文,包括每一步的操作说明、计划与子任务、检查反馈、诊断结论、历史摘要和最终报告。英文示例仅示范格式,不决定输出语言。\n保留工具名、参数名、JSON 字段名、枚举值、状态码、计划语法标记、模板变量、命令、路径、URL、包名及标识符原文。界面文字、日志、错误信息、引用证据和用户指定的输入内容必须如实保留,可另附中文解释;不要为了中文化而翻译设备输入、搜索词或精确匹配目标。\n遵守各自原有输出结构;结构化结果只将允许自由填写的说明文本写成简体中文。只描述已有证据,不编造操作结果。\n\n每次调用设备操作工具前,用一小段简体中文说明当前观察、即将执行的动作和目的;操作后仅在获得结果或新观察时说明实际变化。不要只在最终答复中使用中文。工具参数中的自由描述文本也使用简体中文,精确引用的界面文字仍保留原文。\n\n# ROLE\nYou are the Android Device Operator. Your priority is execution accuracy. You must operate the device step-by-step to achieve the goal, avoiding blind attempts or loops.\n\n# OPERATIONAL PRINCIPLES\n- **Action Scope & Interaction Precision**: Always select gestures whose scope of effect matches the boundaries of the target element. Avoid shortcuts or batch actions from popups or menus if they risk affecting non-target UI components on the screen.\n- **Vision-First Priority**: The screenshot is the primary source of truth for the real device state. Always visually inspect the screen state and layout on the screenshot before referencing the element list. Use the 'Visible UI Elements' list strictly as an auxiliary lookup table to retrieve target element indices and coordinates.\n[% if \"wait_for_delay\" in available_tools %]- **Loading & Transitions**: When entering page transitions (e.g., app launching, web page loading, or progress indicators), use `wait_for_delay` to let the screen settle before executing target actions. Some transition states are difficult to discern, such as a thin progress line at the top of the browser screen during webpage loads. Before questioning whether an error has occurred, you can first adopt a waiting strategy to see how the device state evolves. If the state remains ambiguous after waiting, certain UI components might be malfunctioning or failing to load; in this case, consider retrying the action or pivoting to an alternative path.\n[% endif %]- **Large List & Long Text Exploration Strategy (Zero-Miss, Zero-Duplication Single-Pass Principle)**: When traversing large lists, browsing long text bodies, or scanning continuous content feeds, strictly adhere to the single-pass exploration standard:\n 1. **Unidirectional Anchor-Based Traversal (Zero-Miss)**: Maintain a consistent, unidirectional scan (e.g., uniform downward scrolling). Before each scroll, identify the bottom-most visible item/text segment as your visual anchor and carry that anchor plus the scan direction in short-term memory or the traversal ledger. After the scroll, evaluate this handoff before doing anything unrelated: if the anchor remains visible with new content beyond it, record the new content and continue in the same direction without scrolling back to verify; if the anchor is missing, continuity is uncertain, so perform at most one minimal reverse recovery to re-establish overlap, then resume the original direction. While the traversal's declared exit condition remains unresolved, keep this traversal active and do not advance to another milestone or mark it complete.\n 2. **Boundary-Proven Completion**: A single scroll that reveals no new content is not sufficient evidence that the list is complete. For an exhaustive traversal, stop only when an explicit end-of-list indicator is visible, or when one additional successful swipe in the same direction and within the same list container leaves the same final anchor visible with no new items and no loading state. This boundary probe must remain unidirectional; never reverse direction merely to prove completion. If the task declares an earlier exit condition (for example, find one verified match or process N items), that explicit condition may end the traversal without reaching the list boundary.\n 3. **Memory & Plan-Driven Deduplication (Zero-Duplication)**: Systematically log extracted item names, identifiers, or processing states into notes or memory as you progress. After every screen transition, cross-reference visible items against your recorded ledger and strictly interact only with newly surfaced, unrecorded items to eliminate redundant actions on overlapping elements. Proactively maintain subtasks in `task_plan` (e.g., segmenting by batch, category, or alphabetical ranges like `a-d`), tracking exact paths and progress to prevent backtLine truncated
|
||||
"main_template": "# 输出语言要求\n所有自行撰写的自然语言说明必须使用简体中文,包括每一步的操作说明、计划与子任务、检查反馈、诊断结论、历史摘要和最终报告。英文示例仅示范格式,不决定输出语言。\n保留工具名、参数名、JSON 字段名、枚举值、状态码、计划语法标记、模板变量、命令、路径、URL、包名及标识符原文。界面文字、日志、错误信息、引用证据和用户指定的输入内容必须如实保留,可另附中文解释;不要为了中文化而翻译设备输入、搜索词或精确匹配目标。\n遵守各自原有输出结构;结构化结果只将允许自由填写的说明文本写成简体中文。只描述已有证据,不编造操作结果。\n\n每次调用设备操作工具前,用一小段简体中文说明当前观察、即将执行的动作和目的;操作后仅在获得结果或新观察时说明实际变化。不要只在最终答复中使用中文。工具参数中的自由描述文本也使用简体中文,精确引用的界面文字仍保留原文。\n\n# ROLE\nYou are the Android Device Operator. Your priority is execution accuracy. You must operate the device step-by-step to achieve the goal, avoiding blind attempts or loops.\n\n# OPERATIONAL PRINCIPLES\n- **Action Scope & Interaction Precision**: Always select gestures whose scope of effect matches the boundaries of the target element. Avoid shortcuts or batch actions from popups or menus if they risk affecting non-target UI components on the screen.\n- **Vision-First Priority**: The screenshot is the primary source of truth for the real device state. Always visually inspect the screen state and layout on the screenshot before referencing the element list. Use the 'Visible UI Elements' list strictly as an auxiliary lookup table to retrieve target element indices and coordinates.\n[% if \"wait_for_delay\" in available_tools %]- **Loading & Transitions**: When entering page transitions (e.g., app launching, web page loading, or progress indicators), use `wait_for_delay` to let the screen settle before executing target actions. Some transition states are difficult to discern, such as a thin progress line at the top of the browser screen during webpage loads. Before questioning whether an error has occurred, you can first adopt a waiting strategy to see how the device state evolves. If the state remains ambiguous after waiting, certain UI components might be malfunctioning or failing to load; in this case, consider retrying the action or pivoting to an alternative path.\n[% endif %]- **Large List & Long Text Exploration Strategy (Zero-Miss, Zero-Duplication Single-Pass Principle)**: When traversing large lists, browsing long text bodies, or scanning continuous content feeds, strictly adhere to the single-pass exploration standard:\n 1. **Unidirectional Anchor-Based Traversal (Zero-Miss)**: Maintain a consistent, unidirectional scan (e.g., uniform downward scrolling). Before each scroll, identify the bottom-most visible item/text segment as your visual anchor and carry that anchor plus the scan direction in short-term memory or the traversal ledger. After the scroll, evaluate this handoff before doing anything unrelated: if the anchor remains visible with new content beyond it, record the new content and continue in the same direction without scrolling back to verify; if the anchor is missing, continuity is uncertain, so perform at most one minimal reverse recovery to re-establish overlap, then resume the original direction. While the traversal's declared exit condition remains unresolved, keep this traversal active and do not advance to another milestone or mark it complete.\n 2. **Boundary-Proven Completion**: A single scroll that reveals no new content is not sufficient evidence that the list is complete. For an exhaustive traversal, stop only when an explicit end-of-list indicator is visible, or when one additional successful swipe in the same direction and within the same list container leaves the same final anchor visible with no new items and no loading state. This boundary probe must remain unidirectional; never reverse direction merely to prove completion. If the task declares an earlier exit condition (for example, find one verified match or process N items), that explicit condition may end the traversal without reaching the list boundary.\n 3. **Memory & Plan-Driven Deduplication (Zero-Duplication)**: Systematically log extracted item names, identifiers, or processing states into notes or memory as you progress. After every screen transition, cross-reference visible items against your recorded ledger and strictly interact only with newly surfaced, unrecorded items to eliminate redundant actions on overlapping elements. Proactively maintain subtasks in `task_plan` (e.g., segmenting by batch, category, or alphabetical ranges like `a-d`), tracking exact paths and progress to prevent backtLine truncated
|
||||
}
|
||||
@@ -295,7 +295,16 @@ class OperatorNode:
|
||||
kwargs_to_pass.update(extra_kwargs)
|
||||
await component(builder, state, self.ctx, **kwargs_to_pass)
|
||||
|
||||
return builder.build()
|
||||
messages = builder.build()
|
||||
if getattr(state, "structured_decisions", None):
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
# The legacy prompt has no preceding AI tool calls. Deliver the
|
||||
# same evidence as observations, without orphan function outputs.
|
||||
for results in self._plugin_result_messages(state.last_execution_result).values():
|
||||
for result in results:
|
||||
messages.append(HumanMessage(content=result.content))
|
||||
return messages
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Transcript prompt path (M2, flag agent.memory.transcript.enabled)
|
||||
@@ -358,6 +367,11 @@ class OperatorNode:
|
||||
pass
|
||||
return ledger
|
||||
|
||||
def _plugin_result_messages(self, report: Any) -> dict[str, list[BaseMessage]]:
|
||||
from artemis.plugins.results import plugin_result_messages
|
||||
|
||||
return plugin_result_messages(report, self.ctx.data_engine)
|
||||
|
||||
async def _build_prompt_transcript(
|
||||
self,
|
||||
state: State,
|
||||
@@ -385,7 +399,15 @@ class OperatorNode:
|
||||
# 1. S region: rendered exactly once per session.
|
||||
if not ledger.has_static_prefix:
|
||||
static_text = render_transcript_static_system(self.prompts, self.ctx, state)
|
||||
ledger.set_static_prefix([SystemMessage(content=static_text)])
|
||||
prefix = []
|
||||
if getattr(self.ctx, "exploration_id", None):
|
||||
from artemis.exploration.runtime import exploration_prompt
|
||||
|
||||
prefix.append(exploration_prompt(self.ctx))
|
||||
if getattr(self.ctx, "plugins", None):
|
||||
prefix.append(self.ctx.plugins.prompt())
|
||||
prefix.append(static_text)
|
||||
ledger.set_static_prefix([SystemMessage(content="\n".join(prefix))])
|
||||
|
||||
# 2. F region cold start: an empty ledger over an existing step record
|
||||
# trail means the process restarted — freeze the compiled history once.
|
||||
@@ -423,10 +445,21 @@ class OperatorNode:
|
||||
ledger.commit_staged(
|
||||
step_key=getattr(state, "current_step_id", None),
|
||||
validator_result=validator_result,
|
||||
tool_results=self._plugin_result_messages(validator_result),
|
||||
)
|
||||
|
||||
# 4. Current tail: observation + plan recitation + injected components.
|
||||
builder = PromptBuilder()
|
||||
guidance = None
|
||||
if getattr(self.ctx, "exploration_id", None):
|
||||
from artemis.exploration.runtime import exploration_messages
|
||||
|
||||
# Guidance belongs at the append-only tail, never before the
|
||||
# reusable rules/history. Assistant progress does not change it.
|
||||
guidance = exploration_messages(self.ctx)[1].content
|
||||
if guidance != getattr(self, "_last_exploration_guidance", None):
|
||||
builder.add_human_content(guidance)
|
||||
self._pending_exploration_guidance = guidance
|
||||
builder.add_human_content(f"# CURRENT OBSERVATION [{ledger.elapsed_label()}]")
|
||||
components = [
|
||||
(PlanRecitationPromptComponent(), {}),
|
||||
@@ -468,6 +501,24 @@ class OperatorNode:
|
||||
mark_ephemeral(tail, builder.ephemeral_indices)
|
||||
|
||||
messages = ledger.render([tail])
|
||||
# Compaction can remove the original guidance message. Restore the
|
||||
# exact current instructions once at the new tail, without changing
|
||||
# the reusable prefix or relying on a lossy history capsule.
|
||||
if guidance is not None and not any(
|
||||
guidance
|
||||
in (
|
||||
message.content
|
||||
if isinstance(message.content, str)
|
||||
else "\n".join(
|
||||
block.get("text", "") for block in message.content if isinstance(block, dict)
|
||||
)
|
||||
)
|
||||
for message in messages
|
||||
if isinstance(message, HumanMessage)
|
||||
):
|
||||
tail_content.append({"type": "text", "text": guidance})
|
||||
tail.content = tail_content
|
||||
self._pending_exploration_guidance = guidance
|
||||
self._transcript_turn_tail = tail
|
||||
return messages
|
||||
|
||||
@@ -482,6 +533,9 @@ class OperatorNode:
|
||||
if ledger is not None:
|
||||
base = next(i for i, message in enumerate(messages) if message is tail)
|
||||
ledger.stage_turn(messages[base:])
|
||||
if hasattr(self, "_pending_exploration_guidance"):
|
||||
self._last_exploration_guidance = self._pending_exploration_guidance
|
||||
del self._pending_exploration_guidance
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to stage transcript turn: {e}")
|
||||
|
||||
@@ -547,7 +601,7 @@ class OperatorNode:
|
||||
t
|
||||
for t in traced_tools
|
||||
if not t.name.startswith("plug_")
|
||||
and t.name not in {"plugin_load", "plugin_read_file"}
|
||||
and t.name not in {"plugin_load", "plugin_read_file", "plugin_read_result"}
|
||||
]
|
||||
traced_tools.extend(plugins.langchain_tools())
|
||||
bound_llm = active_llm.bind_tools(tools=traced_tools)
|
||||
@@ -903,6 +957,9 @@ class OperatorNode:
|
||||
not plan_gate_bounced
|
||||
and iteration < max_iterations - 1
|
||||
and len(action_calls) == 1
|
||||
and not (
|
||||
plugins and plugins.is_read_only(normalize_name(action_calls[0]["name"]))
|
||||
)
|
||||
and not getattr(state, "open_incident", None)
|
||||
and self._plan_changed_this_turn() is False
|
||||
):
|
||||
@@ -1151,14 +1208,16 @@ class OperatorNode:
|
||||
active_background_tasks=active_background_tasks,
|
||||
newly_finished_tasks=newly_finished_tasks,
|
||||
)
|
||||
if getattr(self.ctx, "plugins", None):
|
||||
if self._transcript_turn_tail is None and getattr(self.ctx, "plugins", None):
|
||||
from langchain_core.messages import SystemMessage
|
||||
|
||||
messages.insert(0, SystemMessage(content=self.ctx.plugins.prompt()))
|
||||
if getattr(self.ctx, "exploration_id", None):
|
||||
if self._transcript_turn_tail is None and getattr(self.ctx, "exploration_id", None):
|
||||
from artemis.exploration.runtime import exploration_messages
|
||||
|
||||
messages[0:0] = exploration_messages(self.ctx)
|
||||
rules, guidance = exploration_messages(self.ctx)
|
||||
messages.insert(0, rules)
|
||||
messages.append(guidance)
|
||||
|
||||
# 8. Invoke LLM Loop
|
||||
new_subagent_calls = []
|
||||
@@ -1310,7 +1369,7 @@ class OperatorNode:
|
||||
|
||||
plugins = get_plugin_runtime(self.ctx)
|
||||
if plugins and tool_name in plugins.bindings:
|
||||
return [{"action": tool_name, "args": args}], None
|
||||
return [{"action": tool_name, "args": args, "tool_call_id": tc["id"]}], None
|
||||
|
||||
# Get device resolution fallbacks
|
||||
width = 1080
|
||||
|
||||
@@ -384,6 +384,27 @@ class ObservationPromptComponent(PromptComponent):
|
||||
latest_screenshot_b64 = kwargs.get("latest_screenshot_b64")
|
||||
minimal_list = kwargs.get("minimal_list")
|
||||
|
||||
report = getattr(state, "last_execution_result", None)
|
||||
actions = report.get("execution", []) if isinstance(report, dict) else []
|
||||
if (
|
||||
actions
|
||||
and not getattr(state, "injected_instruction", None)
|
||||
and all(
|
||||
isinstance(a, dict)
|
||||
and a.get("plugin_status") == "success"
|
||||
and a.get("post_image_name")
|
||||
for a in actions
|
||||
)
|
||||
and getattr(ctx, "execution_target", "android") != "computer"
|
||||
):
|
||||
builder.add_human_content(
|
||||
"当前电脑画面见刚返回的插件截图和控件;不重复附加无关手机截图。"
|
||||
"下方仍是当前手机控件,不能用于电脑操作。"
|
||||
"转回手机且需要视觉定位时先用 ask_explorer 获取手机观察。"
|
||||
)
|
||||
builder.add_human_content(f"--- Visible UI Elements ---\n{minimal_list}")
|
||||
return
|
||||
|
||||
if latest_screenshot_b64:
|
||||
builder.add_human_content("--- Current Screenshot ---")
|
||||
builder.add_human_content(
|
||||
@@ -684,6 +705,10 @@ def unwritten_action_streak(steps: list[dict]) -> int:
|
||||
for step in reversed(steps or []):
|
||||
if step_updated_task_plan(step):
|
||||
break
|
||||
report = step.get("last_execution_result")
|
||||
actions = report.get("execution", []) if isinstance(report, dict) else []
|
||||
if actions and all(isinstance(a, dict) and a.get("read_only") is True for a in actions):
|
||||
continue
|
||||
if step.get("action_taken"):
|
||||
streak += 1
|
||||
return streak
|
||||
|
||||
@@ -342,7 +342,9 @@ class PlannerNode:
|
||||
)
|
||||
|
||||
exploration_control = getattr(self.ctx, "task_control", None)
|
||||
current_messages[0:0] = exploration_messages(self.ctx, planning=True)
|
||||
rules, guidance = exploration_messages(self.ctx, planning=True)
|
||||
current_messages.insert(0, rules)
|
||||
current_messages.append(guidance)
|
||||
refresh_after_interrupt = False
|
||||
max_validation_attempts = 3
|
||||
max_iterations = 10
|
||||
|
||||
@@ -84,16 +84,19 @@ class SummarizerNode:
|
||||
|
||||
action_name, action_args = self._extract_action(record, state, ctx=self.ctx)
|
||||
result = getattr(state, "last_execution_result", None)
|
||||
if getattr(self.ctx, "execution_target", "android") == "mixed" and isinstance(result, dict):
|
||||
if any(
|
||||
action.get("plugin_id")
|
||||
for action in result.get("execution", [])
|
||||
if isinstance(action, dict)
|
||||
):
|
||||
# The step began with a mobile observation. Never describe a phone-to-desktop
|
||||
# image pair as a transition on one screen.
|
||||
pre_bytes = None
|
||||
post_bytes = None
|
||||
actions = result.get("execution", []) if isinstance(result, dict) else []
|
||||
plugin_actions = [
|
||||
a
|
||||
for a in actions
|
||||
if isinstance(a, dict)
|
||||
and (a.get("plugin_id") or str(a.get("action", "")).startswith("plug_"))
|
||||
]
|
||||
if plugin_actions:
|
||||
from artemis.data_engine.step_evidence import step_image_name
|
||||
|
||||
evidence = {"last_execution_result": result}
|
||||
pre_bytes = self._read_image_name(step_image_name(evidence, "pre"))
|
||||
post_bytes = self._read_image_name(step_image_name(evidence, "post"))
|
||||
# format_result_clean reports only errors/repairs; a clean run keeps
|
||||
# the controller's bare status word.
|
||||
exec_outcome = format_result_clean(result)
|
||||
@@ -102,6 +105,14 @@ class SummarizerNode:
|
||||
exec_outcome = str(result["status"])
|
||||
else:
|
||||
exec_outcome = "unknown"
|
||||
if plugin_actions:
|
||||
from artemis.config.constants import PLUGIN_SUMMARY_TEXT_CHARS
|
||||
from artemis.plugins.prompt_budget import bounded_result_text
|
||||
|
||||
exec_outcome += "\n" + bounded_result_text(
|
||||
"\n".join(str(a.get("result") or "") for a in plugin_actions),
|
||||
limit=PLUGIN_SUMMARY_TEXT_CHARS,
|
||||
)
|
||||
|
||||
# Operator focus: its own reasoning for the step (the target it chose
|
||||
# and the screen change it expected), the goal, the live sub-goal leaf
|
||||
@@ -123,9 +134,26 @@ class SummarizerNode:
|
||||
exec_outcome=exec_outcome,
|
||||
data_engine_step_id=step_id,
|
||||
focus=focus,
|
||||
receipt_only=bool(
|
||||
plugin_actions
|
||||
and len(plugin_actions) == len(actions)
|
||||
and all(
|
||||
a.get("read_only") is True and a.get("plugin_status") == "success"
|
||||
for a in plugin_actions
|
||||
)
|
||||
),
|
||||
)
|
||||
logger.info(f"Dispatched visual transition summary for step {step_number} ({step_id})")
|
||||
|
||||
def _read_image_name(self, name: str | None) -> bytes | None:
|
||||
if not name:
|
||||
return None
|
||||
try:
|
||||
return self.ctx.data_engine.get_image_path(name).read_bytes()
|
||||
except (OSError, ValueError) as exc:
|
||||
logger.warning(f"Could not read plugin evidence: {exc}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _operator_reasoning(record: Any, state: State) -> str | None:
|
||||
"""The operator's raw reasoning for this step: live state first, record second."""
|
||||
|
||||
@@ -186,7 +186,11 @@ async def _process_action(
|
||||
|
||||
plugins = get_plugin_runtime(node.ctx)
|
||||
if plugins and action_name in plugins.bindings:
|
||||
operation = plugins.execute(action_name, action_item.get("args", {}), str(uuid.uuid4()))
|
||||
operation = plugins.execute(
|
||||
action_name,
|
||||
action_item.get("args", {}),
|
||||
action_item.get("tool_call_id") or str(uuid.uuid4()),
|
||||
)
|
||||
control = getattr(node.ctx, "task_control", None)
|
||||
if isinstance(control, TaskControl):
|
||||
interrupted, result = await control.run_interruptibly(
|
||||
@@ -197,6 +201,7 @@ async def _process_action(
|
||||
if interrupted:
|
||||
outcome.success = False
|
||||
outcome.error_msg = "插件操作已打断,结果不确定;继续前必须重新观察。"
|
||||
outcome.action_item.update(result=outcome.error_msg, plugin_status="uncertain")
|
||||
return outcome
|
||||
else:
|
||||
result = await operation
|
||||
@@ -449,14 +454,14 @@ async def run_validation_loop(node, state: State) -> dict:
|
||||
execution.append(outcome.action_item)
|
||||
|
||||
post_image_name = None
|
||||
plugin_action = bool(outcome.action_item.get("plugin_id"))
|
||||
plugin_action = bool(outcome.action_item.get("plugin_id")) or str(
|
||||
action_name
|
||||
).startswith("plug_")
|
||||
mixed_plugin_action = (
|
||||
plugin_action and getattr(ctx, "execution_target", "android") != "computer"
|
||||
)
|
||||
if (
|
||||
plugin_action
|
||||
and not mixed_plugin_action
|
||||
and not outcome.action_item.get("pre_image_valid", False)
|
||||
if plugin_action and (
|
||||
mixed_plugin_action or not outcome.action_item.get("pre_image_valid", False)
|
||||
):
|
||||
# The turn began on another window/geometry. Keep that observation
|
||||
# out of the step's before/after evidence pair and archive exports.
|
||||
@@ -516,6 +521,10 @@ async def run_validation_loop(node, state: State) -> dict:
|
||||
if (last_screenshot_name and last_screenshot_name != decision_screenshot_name)
|
||||
else None
|
||||
)
|
||||
if len(execution) == 1 and (
|
||||
execution[0].get("plugin_id") or str(execution[0].get("action", "")).startswith("plug_")
|
||||
):
|
||||
distinct_post_image_name = execution[0].get("post_image_name")
|
||||
|
||||
if ctx.data_engine and step_id:
|
||||
ctx.data_engine.update_step_execution_result(
|
||||
|
||||
@@ -257,7 +257,12 @@ REPORT_TASK_STATUS_TOOL = ToolDeclaration(
|
||||
},
|
||||
"explanation": {
|
||||
"type": "string",
|
||||
"description": "Provide the final explanation or summary of the task execution.",
|
||||
"description": (
|
||||
"Provide the final answer. If the user requests a report or other written"
|
||||
" deliverable, include the actual content with all requested sections,"
|
||||
" observed findings, evidence and untested or blocked scope; a short"
|
||||
" completion notice is not a substitute. Do not invent missing evidence."
|
||||
),
|
||||
},
|
||||
},
|
||||
"required": ["status", "explanation"],
|
||||
|
||||
Reference in new issue
Block a user