feat: add agentic video analysis and clarify action results
Report device actions as dispatched and preserve full failure details. Route Flash through McpActionExecutor and use ToolFailure across helper tools and agent loops instead of inferring failure from response text. Require model-provided descriptions for coordinate targets. Keep observed element metadata separate from those descriptions in safety checks, visual summaries, history, and frontend labels. Add Gemini Interactions API video analysis with configurable processing mode, chunk size, and timeouts. Centralize Google provider capabilities and token usage normalization, and update google-genai to 2.22.0. Preserve system instructions on follow-up interactions, replan pending chunks when falling back to static processing, and reject unanswered native video and audio segments instead of caching placeholder results. Keep helper-tool failure status intact through history tools and committee calls. Validation: 1,762 Python unit tests passed (3 skipped), 40 frontend formatter tests passed, and the production UI build completed. Ruff lint, formatting, the quality ratchet, and git diff checks passed.
This commit is contained in:
1 parent
611d1a0947
commit
a45daf23f8
120 files changed
+6301
-1710
No files matched your search
@@ -14,10 +14,21 @@
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from artemis.agents.video_analyzer import chunk_conversation
|
||||
from artemis.agents.video_analyzer.chunk_conversation import (
|
||||
_MISSING_SUMMARY_ERROR,
|
||||
_NO_TOOL_CALL_ERROR,
|
||||
SubAgentAnswerExhausted,
|
||||
_ChunkMedia,
|
||||
_drive_sub_agent_loop,
|
||||
_run_native_chunk_conversation,
|
||||
)
|
||||
from artemis.agents.video_analyzer.video_analyzer import VideoAnalyzer
|
||||
from artemis.context import ArtemisContext
|
||||
from google.genai import types
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -29,6 +40,7 @@ async def test_video_analyzer_run():
|
||||
mock_ctx.llm_config = MagicMock()
|
||||
mock_ctx.llm_config.utils.video_analyzer = MagicMock()
|
||||
mock_ctx.llm_config.utils.video_analyzer.model = "gemini-3.7-flash"
|
||||
mock_ctx.agent_config = SimpleNamespace(video_analyzer=SimpleNamespace(processing="static"))
|
||||
|
||||
# Mock Gemini Client
|
||||
mock_client = MagicMock()
|
||||
@@ -80,6 +92,7 @@ async def test_video_analyzer_preserves_thought_signature():
|
||||
mock_ctx.llm_config = MagicMock()
|
||||
mock_ctx.llm_config.utils.video_analyzer = MagicMock()
|
||||
mock_ctx.llm_config.utils.video_analyzer.model = "gemini-3.7-flash"
|
||||
mock_ctx.agent_config = SimpleNamespace(video_analyzer=SimpleNamespace(processing="static"))
|
||||
|
||||
# Mock MobileDeviceController
|
||||
mock_controller = MagicMock()
|
||||
@@ -256,6 +269,7 @@ async def test_video_analyzer_sub_agent_confidence_validation():
|
||||
mock_ctx.llm_config = MagicMock()
|
||||
mock_ctx.llm_config.utils.video_analyzer = MagicMock()
|
||||
mock_ctx.llm_config.utils.video_analyzer.model = "gemini-3.7-flash"
|
||||
mock_ctx.agent_config = SimpleNamespace(video_analyzer=SimpleNamespace(processing="static"))
|
||||
|
||||
# Mock MobileDeviceController
|
||||
mock_controller = MagicMock()
|
||||
@@ -454,3 +468,149 @@ async def test_video_analyzer_sub_agent_confidence_validation():
|
||||
model_turns = [c for c in sub_turn_2_contents if c.role == "model"]
|
||||
assert len(model_turns) == 1
|
||||
assert model_turns[0].parts[0].thought_signature == sub_dummy_signature
|
||||
|
||||
|
||||
# ------------------------------------------------- static sub-agent loop
|
||||
|
||||
|
||||
def _static_media(tmp_path) -> _ChunkMedia:
|
||||
media = _ChunkMedia()
|
||||
media.path = tmp_path / "segment.mp4"
|
||||
media.path.write_bytes(b"video")
|
||||
media.compressed_path = media.path
|
||||
media.actual_start = 0.0
|
||||
media.prompt_with_context = "Watch the toggle."
|
||||
return media
|
||||
|
||||
|
||||
def _valid_static_event() -> dict:
|
||||
return {
|
||||
"start_time": 1.0,
|
||||
"end_time": 2.0,
|
||||
"transcription": "Battery Saver toggled on",
|
||||
"confidence_score": 0.9,
|
||||
"verification_timestamp_secs": 1.5,
|
||||
}
|
||||
|
||||
|
||||
def _scripted_static_turns(*turns):
|
||||
"""Fakes ``_stream_sub_agent_turn`` with ``(text, function_calls)`` per turn."""
|
||||
scripted = list(turns)
|
||||
|
||||
async def fake_turn(analyzer, sub_agent_contents, trace_id):
|
||||
text, function_calls = scripted.pop(0)
|
||||
return text, function_calls, []
|
||||
|
||||
return fake_turn
|
||||
|
||||
|
||||
def _static_analyzer() -> MagicMock:
|
||||
analyzer = MagicMock()
|
||||
analyzer.model_name = "gemini-3.7-flash"
|
||||
analyzer.sub_system_prompt = "system"
|
||||
analyzer.ctx.data_engine = None
|
||||
return analyzer
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_static_submit_answer_without_summary_is_sent_back_then_accepted(tmp_path):
|
||||
first = types.FunctionCall(
|
||||
name="submit_answer", args={"timeline_events": [_valid_static_event()]}
|
||||
)
|
||||
second = types.FunctionCall(
|
||||
name="submit_answer",
|
||||
args={"summary": "s2", "analysis": "a2", "timeline_events": []},
|
||||
)
|
||||
contents: list = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
chunk_conversation,
|
||||
"_stream_sub_agent_turn",
|
||||
new=_scripted_static_turns(("", [first]), ("", [second])),
|
||||
),
|
||||
patch.object(chunk_conversation, "_persist_timeline_events", new=AsyncMock()) as persist,
|
||||
):
|
||||
answer = await _drive_sub_agent_loop(
|
||||
_static_analyzer(), contents, None, _static_media(tmp_path), 0.0, 5.0, "verify"
|
||||
)
|
||||
|
||||
assert answer == ("s2", "a2")
|
||||
# Only the accepted answer reaches persistence.
|
||||
persist.assert_awaited_once()
|
||||
assert persist.await_args.args[1] == []
|
||||
rejection = contents[-1]
|
||||
assert rejection.role == "user"
|
||||
assert rejection.parts[0].function_response.response["error"] == _MISSING_SUMMARY_ERROR
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_static_loop_raises_when_no_turn_calls_submit_answer(tmp_path):
|
||||
with (
|
||||
patch.object(
|
||||
chunk_conversation,
|
||||
"_stream_sub_agent_turn",
|
||||
new=_scripted_static_turns(("text only", []), ("still text", [])),
|
||||
),
|
||||
patch.object(chunk_conversation, "_persist_timeline_events", new=AsyncMock()) as persist,
|
||||
pytest.raises(SubAgentAnswerExhausted) as raised,
|
||||
):
|
||||
await _drive_sub_agent_loop(
|
||||
_static_analyzer(), [], None, _static_media(tmp_path), 0.0, 5.0, "verify"
|
||||
)
|
||||
|
||||
assert raised.value.reason == _NO_TOOL_CALL_ERROR
|
||||
persist.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_static_last_turn_timeline_failure_raises_and_persists_nothing(tmp_path):
|
||||
bad = dict(_valid_static_event(), confidence_score="HIGH")
|
||||
out_of_range = dict(_valid_static_event(), verification_timestamp_secs=42.0)
|
||||
first = types.FunctionCall(
|
||||
name="submit_answer", args={"summary": "s1", "timeline_events": [bad]}
|
||||
)
|
||||
second = types.FunctionCall(
|
||||
name="submit_answer", args={"summary": "s2", "timeline_events": [out_of_range]}
|
||||
)
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
chunk_conversation,
|
||||
"_stream_sub_agent_turn",
|
||||
new=_scripted_static_turns(("", [first]), ("", [second])),
|
||||
),
|
||||
patch.object(chunk_conversation, "_persist_timeline_events", new=AsyncMock()) as persist,
|
||||
pytest.raises(SubAgentAnswerExhausted) as raised,
|
||||
):
|
||||
await _drive_sub_agent_loop(
|
||||
_static_analyzer(), [], None, _static_media(tmp_path), 0.0, 5.0, "verify"
|
||||
)
|
||||
|
||||
assert "verification_timestamp_secs" in raised.value.reason
|
||||
assert "42.0" in str(raised.value)
|
||||
persist.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exhausted_static_chunk_is_not_committed_to_the_blackboard(tmp_path):
|
||||
analyzer = _static_analyzer()
|
||||
uploaded = SimpleNamespace(name="files/abc", uri="https://files/abc", mime_type="video/mp4")
|
||||
analyzer.upload_and_poll_file = AsyncMock(return_value=uploaded)
|
||||
analyzer.client.aio.files.delete = AsyncMock()
|
||||
analyzer.cloud_files_to_cleanup = set()
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
chunk_conversation,
|
||||
"_stream_sub_agent_turn",
|
||||
new=_scripted_static_turns(("text only", []), ("still text", [])),
|
||||
),
|
||||
pytest.raises(SubAgentAnswerExhausted),
|
||||
):
|
||||
await _run_native_chunk_conversation(
|
||||
analyzer, _static_media(tmp_path), 0.0, 5.0, 0.0, 5.0, "verify", "lease-1"
|
||||
)
|
||||
|
||||
analyzer.blackboard.complete_segment.assert_not_called()
|
||||
analyzer.client.aio.files.delete.assert_awaited_once_with(name="files/abc")
|
||||
Reference in new issue
Block a user