Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions services/android/tests/test_budget_supervision_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Why test_exhausted_active_budget_refuses_due_input_before_any_native_preflight can read no budget (CI run 44355808074).

Same synthetic native flow and the same .3 s timer as that test (host(delay=.3)). A gate on Supervision.tick (closed:
the whole tick is skipped, so nothing is pruned or admitted) orders the two events the test races: the test thread's
budget read and the timer wake's admission. The real lifecycle runs; only the tick's start is held.
"""
import threading
import time

import pytest

from agent_harness import Service
from test_order_supervision import host, messages, rows, until, wakes


@pytest.fixture
def gated(monkeypatch):
from deedkit_android import order_supervision
real, gate, ticks = order_supervision.Supervision.tick, threading.Event(), []

async def tick(self):
if not gate.is_set():
return # held: no refresh, no budget pruning, no due-wake admission
ticks.append(1)
return await real(self)
monkeypatch.setattr(order_supervision.Supervision, 'tick', tick)
return gate, ticks


def turns(record):
return len([r for r in messages(record) if r['message'].get('method') == 'turn/start'])


# spec: ANYM-993, ANYM-997
def test_budget_exhausted_before_the_first_tick_refuses_the_due_wake_before_any_native_input(tmp_path, monkeypatch,
gated, record_property):
"""Positive control: the ordering the existing test intends, without depending on how late its read is."""
gate, ticks = gated
config, record = host(tmp_path, monkeypatch, delay=.3)
with Service(config) as service:
request = service.start(service.lease(), goal='native-control')
service.finished(request['request_id'])
supervision, key = service.app.state.agents.supervision, ('one', request['request_id'])
assert not ticks and key in supervision.budgets and key in supervision.handles # retain made both
due, = rows(service, 'SELECT next_check_at FROM android_order_obligations')
slack = round(due['next_check_at'] - time.time(), 3) # what an ungated test has left for its read
budget = supervision.budgets[key]
budget['seconds_used'] = budget['seconds_limit']
before, foreground = record.read_text(), turns(record)
gate.set()
until(lambda: wakes(service) and wakes(service)[0]['hold_reason'] == 'lease_unavailable')
assert record.read_text() == before and turns(record) == foreground == 1
assert not rows(service, 'SELECT * FROM android_order_native_inputs')
assert not supervision.handles and not service.app.state.devices.locks['home'].locked()
record_property('dk90_control', {'slack_s_at_read': slack, 'wake': wakes(service)[0]['hold_reason']})


# spec: ANYM-993, ANYM-997
def test_a_budget_read_after_the_due_wake_was_admitted_finds_it_already_pruned(tmp_path, monkeypatch, gated,
record_property):
"""The CI state: the timer wake was admitted while the budget still had time, its native turn received the
obligation, the handle lost its refs and the same tick pruned the budget. A read now raises KeyError."""
gate, ticks = gated
config, record = host(tmp_path, monkeypatch, delay=.3)
with Service(config) as service:
request = service.start(service.lease(), goal='native-control')
service.finished(request['request_id'])
supervision, key = service.app.state.agents.supervision, ('one', request['request_id'])
assert key in supervision.budgets and key in supervision.handles # what a prompt read would have found
gate.set() # a late test thread: ticks run before it reads
until(lambda: rows(service, 'SELECT * FROM android_order_native_inputs'))
until(lambda: key not in supervision.budgets)
with pytest.raises(KeyError):
supervision.budgets[key] # line 566's read, at this point
obligation, = rows(service, 'SELECT lifecycle FROM android_order_obligations')
wake, = wakes(service)
state = {'budget': key in supervision.budgets, 'handles': len(supervision.handles), 'ticks': len(ticks) > 0,
'native_inputs': len(rows(service, 'SELECT * FROM android_order_native_inputs')),
'obligation': obligation['lifecycle'], 'wake': (wake['state'], wake['hold_reason']),
'turns': turns(record)}
record_property('dk90_late_read', state)
assert state == {'budget': False, 'handles': 0, 'ticks': True, 'native_inputs': 1, 'obligation': 'received',
'wake': ('accepted', None), 'turns': 2}, state
23 changes: 17 additions & 6 deletions services/android/tests/test_order_supervision.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,25 @@ def wall():

# spec: ANYM-993, ANYM-997
def test_exhausted_active_budget_refuses_due_input_before_any_native_preflight(tmp_path, monkeypatch):
from deedkit_android import order_supervision
real, gate = order_supervision.Supervision.tick, threading.Event()

async def tick(self):
if gate.is_set(): # no tick (budget prune or due-wake admission) before this test exhausts the budget
await real(self)
monkeypatch.setattr(order_supervision.Supervision, 'tick', tick) # monkeypatch restores it at teardown
config, record = host(tmp_path, monkeypatch, delay=.3)
with Service(config) as service:
request = service.start(service.lease(), goal='native-control')
service.finished(request['request_id'])
supervision = service.app.state.agents.supervision
budget = supervision.budgets[('one', request['request_id'])]
budget['seconds_used'] = budget['seconds_limit']
before = record.read_text()
try:
request = service.start(service.lease(), goal='native-control')
service.finished(request['request_id'])
supervision = service.app.state.agents.supervision
# A runner slower than the .3 s timer must not admit the wake first (CI run 44355808074).
budget = supervision.budgets[('one', request['request_id'])]
budget['seconds_used'] = budget['seconds_limit']
before = record.read_text()
finally:
gate.set()
until(lambda: wakes(service) and wakes(service)[0]['hold_reason'] == 'lease_unavailable')
assert record.read_text() == before
assert not rows(service, 'SELECT * FROM android_order_native_inputs')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def offer(*args, **kwargs):
lost, = until(lambda: [r for r in forwards(service) if r['state'] == 'held'])
usb = service.app.state.agents.usb
ended = usb.entries[('one', 'home')]
assert ended.task == first and ended.connector.task.done()
assert ended.task == first
# The 'held' commit precedes the connector task's completion in that same loop step; this thread can see
# the row in between (CI run 44354309423), so it waits for completion within until()'s bound.
until(lambda: ended.connector.task.done())
rig.phone.plug()
second = service.start(token, goal='native-control')['request_id']
assert entered.wait(5)
Expand Down
60 changes: 60 additions & 0 deletions services/android/tests/test_usb_settlement_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Why test_an_ended_request_handle_never_reacquires_while_the_new_request_awaits_admission saw a pending connector
task after its forward row read 'held' (CI run 44354309423).

Same synthetic listener/ADB flow as that test, up to the USB loss. A barrier inside ForwardJournal.save, right after
the 'held' row is committed, stops the event-loop thread there while the test thread reads what any cross-thread
observer reads at that instant. A loop callback queued at the barrier shows whether the connector task suspends again
after that commit. The real lifecycle runs; only that one commit is held, for at most 5 s.
"""
import asyncio
import threading

from agent_harness import Service
from test_order_supervision import until
from test_usb_order_restart_recovery import forwards
from test_usb_order_restart_recovery import rig # noqa: F401 - shared synthetic listener/ADB fixture


# spec: ANYM-997
def test_a_committed_held_forward_is_visible_before_its_connector_task_completes_in_that_same_loop_step(
rig, monkeypatch, record_property): # noqa: F811
from deedkit_android import order_usb_host
real, committed, resume, held, after = order_usb_host.ForwardJournal.save, threading.Event(), threading.Event(), [], []

def save(self, ident, **values):
real(self, ident, **values)
if values.get('state') == 'held' and not committed.is_set():
task = asyncio.current_task()
held.append((ident, values.get('reason'), task))
# Runs after the step that committed the row: is the connector task done by then?
asyncio.get_running_loop().call_soon(lambda: after.append(task.done()))
committed.set()
assert resume.wait(5)
monkeypatch.setattr(order_usb_host.ForwardJournal, 'save', save)
journal, rig.phone.journal = rig.phone.journal, []
try:
with Service(rig.config) as service:
token = service.lease()
first = service.start(token, goal='native-control')['request_id']
service.finished(first)
until(lambda: rig.phone.packages and forwards(service)[0]['collection'] == 'unconfirmed')
rig.phone.journal = journal
rig.phone.unplug()
rig.table.clear()
assert committed.wait(5)
# What the existing test's until() can return at this instant, from its own thread:
lost, = [r for r in forwards(service) if r['state'] == 'held']
ended = service.app.state.agents.usb.entries[('one', 'home')]
ident, reason, task = held[0]
visible = {'row': (lost['request_id'] == first, lost['id'] == ident == ended.receipt, lost['reason']),
'entry_task_is_first': ended.task == first, 'connector_task_done': ended.connector.task.done(),
'saving_task_is_connector': task is ended.connector.task}
resume.set()
until(lambda: after)
until(lambda: ended.connector.task.done()) # a stable read of the same condition
record_property('dk90_held_commit', {**visible, 'done_after_that_step': after[0], 'reason': reason})
assert visible == {'row': (True, True, 'collection_cleanup_unconfirmed'), 'entry_task_is_first': True,
'connector_task_done': False, 'saving_task_is_connector': True}, visible
assert after == [True] # no await between the commit and completion: no window inside the loop
finally:
resume.set()
Loading