feat: unify history tools and improve execution reliability

This commit is contained in:
somew1nd committed 2026-09-03 16:05:47 -07:00
1 parent d23a4f6472
commit 2fea9865ee
116 files changed
+8291 -2503

No files matched your search

@@ -14,6 +14,7 @@
"""Recording files of cancelled / killed workers must survive and be served whole."""
import json
import os
from pathlib import Path
import time
@@ -171,3 +172,61 @@ def test_recover_orphaned_recording_returns_none_without_files(tmp_path):
missing = tmp_path / "missing" / "recording.mkv"
assert MediaService.recover_orphaned_recording(str(missing)) is None
assert MediaService.recover_orphaned_recording(None) is None
def test_resolve_video_segments_passes_through_session_offsets(tmp_path, monkeypatch):
folder = tmp_path / "artemis-traces" / "web_9_done"
folder.mkdir(parents=True)
(folder / "recording.mp4").write_bytes(b"seg0")
(folder / "recording_001.mp4").write_bytes(b"seg1")
(folder / "recording.json").write_text(
json.dumps(
{
"version": 2,
"duration": 15.0,
"segments": [
{
"file": "recording.mp4",
"start": 0.0,
"duration": 10.0,
"offset_ms": 1200,
"duration_ms": 10000,
"width": 1080,
"height": 1920,
},
{
"file": "recording_001.mp4",
"start": 10.0,
"duration": 5.0,
"offset_ms": 12700,
"duration_ms": 5000,
"width": 1920,
"height": 1080,
},
{"file": "legacy.mp4", "start": 15.0, "duration": 1.0, "width": 1, "height": 1},
],
}
),
encoding="utf-8",
)
(folder / "legacy.mp4").write_bytes(b"legacy")
monkeypatch.setattr(media_module, "WORKSPACE_ROOT", tmp_path)
segments = MediaService.resolve_video_segments(
"/videos/artemis-traces/web_9_done/recording.mp4"
)
assert [s["url"] for s in segments] == [
"/videos/artemis-traces/web_9_done/recording.mp4",
"/videos/artemis-traces/web_9_done/recording_001.mp4",
"/videos/artemis-traces/web_9_done/legacy.mp4",
]
assert [(s["start"], s["duration"]) for s in segments] == [
(0.0, 10.0),
(10.0, 5.0),
(15.0, 1.0),
]
assert segments[0]["offset_ms"] == 1200 and segments[0]["duration_ms"] == 10000
assert segments[1]["offset_ms"] == 12700 and segments[1]["duration_ms"] == 5000
# Legacy entries without offsets stay offset-free so the UI falls back to back-to-back.
assert "offset_ms" not in segments[2]
@@ -45,20 +45,38 @@ def test_get_session_checks_reads_ledger_and_outcome(tmp_path, monkeypatch):
json.dumps({"task_status": "completed", "tests": {"passed": 2, "failed": 0}}),
encoding="utf-8",
)
stream = {
"attempt_id": "abc#1",
"checkpoint_id": "abc",
"phase": "checkpoint",
"trace_id": "trace-1",
"ts": 1.5,
"truncated": False,
"dropped_chars": 0,
"segments": [
{"execution_id": "e1", "role": "thought", "when": 1.1, "text": "Looking"},
{"execution_id": "e1", "role": "answer", "when": 1.2, "text": "Seen"},
],
}
(session_dir / "check_streams.jsonl").write_text(
json.dumps(stream) + "\nnot json\n", encoding="utf-8"
)
result = MediaService.get_session_checks("sess-1")
assert [r["attempt_id"] for r in result["records"]] == ["abc#1", "final#1"]
assert result["records"][0]["subgoal_text"] == "Create the alarm"
assert result["run_outcome"]["task_status"] == "completed"
assert result["streams"] == [stream]
def test_get_session_checks_without_material(tmp_path, monkeypatch):
monkeypatch.setattr(media_service_module, "TRACES_PATH", tmp_path)
(tmp_path / "sess-2").mkdir()
assert MediaService.get_session_checks("sess-2") == {"records": [], "run_outcome": None}
assert MediaService.get_session_checks("missing") == {"records": [], "run_outcome": None}
empty = {"records": [], "streams": [], "run_outcome": None}
assert MediaService.get_session_checks("sess-2") == empty
assert MediaService.get_session_checks("missing") == empty
def test_checks_route_is_registered():
@@ -0,0 +1,119 @@
# 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.
"""``/api/sessions/{id}/usage``: token totals, live context and run tuning."""
import json
from apps.admin_console.database.connection import db_session
from apps.admin_console.database.repositories.session_repository import SessionRepository
def _insert_session(db_path, session_id, device_info):
with db_session(db_path) as conn:
conn.execute(
"INSERT INTO sessions (session_id, initial_goal, start_time, status, device_info)"
" VALUES (?, ?, ?, ?, ?)",
(session_id, "goal", 1.0, "completed", json.dumps(device_info)),
)
conn.commit()
def _insert_usage(db_path, session_id, trace_id, ts, payload, *, name="llm_usage"):
with db_session(db_path) as conn:
conn.execute(
"INSERT INTO traces (trace_id, session_id, type, name, timestamp, status, payload)"
" VALUES (?, ?, 'llm_call', ?, ?, 'success', ?)",
(trace_id, session_id, name, ts, json.dumps(payload)),
)
conn.commit()
def _usage(prompt, completion, node=None, cached=0):
payload = {
"prompt_tokens": prompt,
"completion_tokens": completion,
"total_tokens": prompt + completion,
"cached_tokens": cached,
"context_base_tokens": prompt,
}
if node:
payload["node"] = node
return payload
def test_usage_sums_all_agents_and_tracks_latest_operator_context(tmp_path):
db_path = tmp_path / "sessions.db"
repo = SessionRepository(db_path)
_insert_session(
db_path,
"s1",
{
"profile": "pro",
"run_tuning": {"verification_level": "checkpoints", "explorer_mode": "pro"},
},
)
_insert_usage(db_path, "s1", "t1", 10.0, _usage(30_000, 500, node="planner"))
_insert_usage(db_path, "s1", "t2", 11.0, _usage(120_000, 800, node="operator", cached=90_000))
_insert_usage(db_path, "s1", "t3", 12.0, _usage(5_000, 100, node="checker"))
_insert_usage(db_path, "s1", "t4", 13.0, _usage(150_000, 700, node="operator"))
# A retry trace shares the type but is not a usage record.
_insert_usage(db_path, "s1", "t5", 14.0, {"error": "503"}, name="llm_retry")
usage = repo.get_session_usage("s1")
assert usage["llm_calls"] == 4
assert usage["prompt_tokens"] == 305_000
assert usage["completion_tokens"] == 2_100
assert usage["total_tokens"] == 307_100
assert usage["cached_tokens"] == 90_000
assert usage["operator_context_tokens"] == 150_000
assert usage["operator_context_updated_at"] == 13.0
assert usage["operator_context_window_tokens"] == 1_000_000
assert usage["profile"] == "pro"
assert usage["run_tuning"] == {"verification_level": "checkpoints", "explorer_mode": "pro"}
def test_usage_flash_runner_counts_as_operator_and_has_no_tuning(tmp_path):
db_path = tmp_path / "sessions.db"
repo = SessionRepository(db_path)
_insert_session(db_path, "s2", {"profile": "flash"})
_insert_usage(db_path, "s2", "t1", 1.0, _usage(20_000, 200, node="FlashRunner"))
usage = repo.get_session_usage("s2")
assert usage["operator_context_tokens"] == 20_000
assert usage["total_tokens"] == 20_200
assert usage["profile"] == "flash"
assert usage["run_tuning"] is None
def test_usage_for_unknown_or_empty_session_is_zeroed(tmp_path):
db_path = tmp_path / "sessions.db"
repo = SessionRepository(db_path)
usage = repo.get_session_usage("missing")
assert usage["llm_calls"] == 0
assert usage["total_tokens"] == 0
assert usage["operator_context_tokens"] is None
assert usage["profile"] is None
assert usage["run_tuning"] is None
def test_usage_route_is_registered():
from apps.admin_console.routers import sessions
paths = {getattr(r, "path", None) for r in sessions.router.routes}
assert "/api/sessions/{session_id}/usage" in paths