feat: improve task execution, verification and packaging

Add execution incidents, action bursts and shared Flash history.

Expose verification and Explorer settings with streamed check results.

Support graceful cancellation, recording recovery and bundled UI resources.

Add CI quality checks and clean up comments and documentation.
This commit is contained in:
somew1nd committed 2026-09-02 21:45:51 -07:00
1 parent 5ccf06d677
commit 67248c887e
289 files changed
+15883 -6919

No files matched your search

@@ -0,0 +1,93 @@
# 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.
import asyncio
import json
import time
import pytest
from artemis.runtime import cancel_requests as cr
@pytest.fixture(autouse=True)
def isolated_dir(tmp_path, monkeypatch):
directory = tmp_path / "cancel-requests"
monkeypatch.setattr(cr, "get_temp_dir", lambda _subfolder=None: directory)
return directory
def test_session_marker_roundtrip(isolated_dir):
assert not cr.is_cancel_requested(session_id="s-1")
written = cr.request_cancel(session_id="s-1", reason="test")
assert len(written) == 1
assert written[0].parent == isolated_dir
assert cr.is_cancel_requested(session_id="s-1")
cr.clear_cancel_request(session_id="s-1")
assert not cr.is_cancel_requested(session_id="s-1")
def test_pid_marker_ignores_recycled_pid():
cr.request_cancel(pid=4242, process_created_at=1000.0)
assert cr.is_cancel_requested(pid=4242, process_created_at=1000.4)
assert not cr.is_cancel_requested(pid=4242, process_created_at=2000.0)
# A marker without a creation time applies to whoever holds the pid.
cr.request_cancel(pid=4343)
assert cr.is_cancel_requested(pid=4343, process_created_at=5.0)
def test_expired_markers_are_ignored_and_purged():
path = cr.request_cancel(session_id="old")[0]
payload = json.loads(path.read_text(encoding="utf-8"))
payload["requested_at"] = time.time() - cr.MARKER_TTL_SECONDS - 5
path.write_text(json.dumps(payload), encoding="utf-8")
assert not cr.is_cancel_requested(session_id="old")
assert cr.purge_expired() == 1
assert not path.exists()
def test_unreadable_pid_marker_is_not_trusted(isolated_dir):
isolated_dir.mkdir(parents=True, exist_ok=True)
(isolated_dir / "pid-77.cancel").write_text("{not json", encoding="utf-8")
assert not cr.is_cancel_requested(pid=77, process_created_at=1.0)
@pytest.mark.asyncio
async def test_watch_delivers_once_and_clears_marker():
delivered: list[int] = []
watcher = asyncio.create_task(
cr.watch_for_cancel_request(
lambda: delivered.append(1),
session_id="w-1",
pid=0,
poll_seconds=0.05,
)
)
await asyncio.sleep(0.12)
assert not delivered
assert not watcher.done()
cr.request_cancel(session_id="w-1")
assert await asyncio.wait_for(watcher, timeout=2) is True
assert delivered == [1]
assert not cr.is_cancel_requested(session_id="w-1")
+52 -1
View File
@@ -131,6 +131,58 @@ def test_submit_task_to_daemon():
assert data["conversation_id"] == "conv-123"
def _daemon_ok_response(body: bytes) -> MagicMock:
mock_resp = MagicMock()
mock_resp.status = 200
mock_resp.read.return_value = body
mock_resp.__enter__.return_value = mock_resp
return mock_resp
def test_submit_task_to_daemon_forwards_pro_tuning_knobs():
mock_resp = _daemon_ok_response(b'{"status": "queued", "tasks": [{"session_id": "s1"}]}')
with patch("urllib.request.urlopen", return_value=mock_resp) as mock_urlopen:
submit_task_to_daemon(
"audit goal",
profile="pro",
verification_level="strict",
explorer_mode="ultra",
)
data = json.loads(mock_urlopen.call_args[0][0].data.decode("utf-8"))
# JSON field names match RunRequest on /api/run.
assert data["verification_level"] == "strict"
assert data["explorer_mode"] == "ultra"
def test_submit_task_to_daemon_pro_tuning_defaults_to_null():
mock_resp = _daemon_ok_response(b'{"status": "queued", "tasks": [{"session_id": "s1"}]}')
with patch("urllib.request.urlopen", return_value=mock_resp) as mock_urlopen:
submit_task_to_daemon("plain goal", profile="flash")
data = json.loads(mock_urlopen.call_args[0][0].data.decode("utf-8"))
assert data["verification_level"] is None
assert data["explorer_mode"] is None
def test_submit_batch_to_daemon_forwards_pro_tuning_knobs():
from artemis.runtime.daemon_client import submit_batch_to_daemon
mock_resp = _daemon_ok_response(
b'{"status": "queued", "tasks": [{"session_id": "a"}, {"session_id": "b"}]}'
)
with patch("urllib.request.urlopen", return_value=mock_resp) as mock_urlopen:
res = submit_batch_to_daemon(
["goal a", "goal b"],
profile="pro",
verification_level="checkpoints",
explorer_mode="pro",
)
data = json.loads(mock_urlopen.call_args[0][0].data.decode("utf-8"))
assert res is not None and len(res["tasks"]) == 2
assert data["goals"] == ["goal a", "goal b"]
assert data["verification_level"] == "checkpoints"
assert data["explorer_mode"] == "pro"
def test_stop_task_on_daemon():
from artemis.runtime.daemon_client import stop_task_on_daemon
@@ -148,4 +200,3 @@ def test_stop_task_on_daemon():
mock_resp.read.return_value = b'{"status": "no_running_task"}'
with patch("urllib.request.urlopen", return_value=mock_resp):
assert stop_task_on_daemon("non-existent-sess") is False
+1 -3
View File
@@ -197,9 +197,7 @@ def test_pending_reservation_at_queue_head_does_not_block_other_devices():
pending_ticket = DeviceExecutionLock.reserve("unclaimed submission")
# device-alpha is a known idle device (a queued submission targets it), so
# the pending ticket parks there; device-beta must stay schedulable.
alpha_ticket = DeviceExecutionLock.reserve(
"task for device alpha", device_id="device-alpha"
)
alpha_ticket = DeviceExecutionLock.reserve("task for device alpha", device_id="device-alpha")
try:
beta = DeviceExecutionLock("device-beta", "task for device beta")
# Must acquire immediately even though the older pending ticket is at
@@ -202,9 +202,7 @@ def test_stop_server_terminates_tree(monkeypatch):
lambda: 1,
)
reconcile = MagicMock(return_value=1)
monkeypatch.setattr(
"artemis.runtime.server_lifecycle._reconcile_orphaned_sessions", reconcile
)
monkeypatch.setattr("artemis.runtime.server_lifecycle._reconcile_orphaned_sessions", reconcile)
success, msg, stopped = stop_server(8000)
assert success is True
@@ -218,9 +216,7 @@ def test_stop_server_terminates_tree(monkeypatch):
@pytest.mark.asyncio
async def test_api_server_status():
"""Verify GET /api/system/server-status returns server runtime metadata."""
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://localhost"
) as client:
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://localhost") as client:
response = await client.get("/api/system/server-status")
assert response.status_code == 200
data = response.json()
@@ -243,7 +239,9 @@ async def test_api_restart_endpoint():
assert response.status_code == 200
data = response.json()
assert data["status"] == "restarting"
assert "reconnection" in data["message"].lower() or "restarting" in data["message"].lower()
assert (
"reconnection" in data["message"].lower() or "restarting" in data["message"].lower()
)
mock_thread.assert_called_once()
mock_instance.start.assert_called_once()
@@ -254,9 +252,7 @@ async def test_api_shutdown_endpoint():
fake_server = MagicMock(should_exit=False)
app.state.uvicorn_server = fake_server
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://localhost"
) as client:
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://localhost") as client:
denied = await client.post("/api/system/shutdown")
assert denied.status_code == 403
+21 -12
View File
@@ -1,4 +1,4 @@
# Copyright 2026 Google LLC
# 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.
@@ -172,7 +172,7 @@ def test_update_on_corrupt_status_logs_and_drops(temp_trace_env, caplog):
assert not any("Dropping status update" in rec.message for rec in caplog.records)
def test_concurrent_read_modify_write_does_not_lose_updates(temp_trace_env):
def test_concurrent_read_modify_write_does_not_lose_updates(temp_trace_env, caplog):
"""Interleaved RMW updates from two writers must not lose each other's fields."""
trace_id = str(uuid.uuid4())
trace_store.init_trace(trace_id, "Concurrency", "Flash")
@@ -182,28 +182,37 @@ def test_concurrent_read_modify_write_does_not_lose_updates(temp_trace_env):
def set_errors():
try:
for i in range(iterations):
assert trace_store.update_trace_status(
trace_id, "running", error=f"err-{i}"
) is not None
assert (
trace_store.update_trace_status(trace_id, "running", error=f"err-{i}")
is not None
)
except BaseException as exc: # noqa: BLE001 - surfaced in the main thread
failures.append(exc)
def set_serials():
try:
for i in range(iterations):
assert trace_store.update_trace_device_serial(
trace_id, f"serial-{i}"
) is not None
assert trace_store.update_trace_device_serial(trace_id, f"serial-{i}") is not None
except BaseException as exc: # noqa: BLE001 - surfaced in the main thread
failures.append(exc)
threads = [threading.Thread(target=set_errors), threading.Thread(target=set_serials)]
for t in threads:
t.start()
for t in threads:
t.join(timeout=30.0)
with caplog.at_level(logging.WARNING, logger="artemis.runtime.trace_store"):
for t in threads:
t.start()
for t in threads:
t.join(timeout=30.0)
assert failures == []
# The lock is bounded by design: after _LOCK_TIMEOUT_SECONDS a writer
# proceeds last-writer-wins (with a WARNING) rather than deadlocking. On a
# heavily loaded machine that documented path can be taken, and then a
# lost field is expected rather than a serialization bug.
lock_degraded = [rec.message for rec in caplog.records if "trace status lock" in rec.message]
if lock_degraded:
pytest.skip(
f"status lock degraded under load, serialization not exercised: {lock_degraded[0]}"
)
final = trace_store.read_status(trace_id)
assert final is not None
# Every serialized RMW preserves the other writer's latest field, so both