Enterprise agent platform for building, deploying, and operating AI teammates.
"""The acceptance-time command classifier and the dispatch class it writes to the command row."""
import asyncio
import itertools
import logging
from unittest.mock import Mock
import pytest
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from xagent.web.models import database as database_module
from xagent.web.models.database import Base
from xagent.web.models.task import Task, TaskStatus
from xagent.web.models.task_admission import TaskAdmissionTicket
from xagent.web.models.task_command import TaskExecutionCommand
from xagent.web.models.task_interaction import INTERACTION_ORIGIN_VOCABULARY
from xagent.web.models.user import User
from xagent.web.services import task_command_transport as transport
from xagent.web.services import task_execution_admission as admission
from xagent.web.services import task_resume_command
from xagent.web.services.task_dispatch_cap import (
DISPATCH_CLASS_BATCH,
DISPATCH_CLASS_INTERACTIVE,
)
KINDS = transport.TaskCommandKind
EXECUTION = (KINDS.START, KINDS.MESSAGE, KINDS.RESUME, KINDS.RESUME_INPUT)
CONTROL = (KINDS.PAUSE, KINDS.CANCEL)
I = DISPATCH_CLASS_INTERACTIVE # noqa: E741
B = DISPATCH_CLASS_BATCH
@pytest.fixture
def host(tmp_path, monkeypatch):
engine = sa.create_engine(f"sqlite:///{tmp_path / 'classifier.db'}")
Base.metadata.create_all(engine)
sessions = sessionmaker(engine, expire_on_commit=False)
monkeypatch.setenv("XAGENT_SHARED_TASK_EXECUTION_ENABLED", "true")
monkeypatch.setattr(database_module, "_SessionLocal", sessions)
with sessions() as db:
user = User(username="owner", password_hash="unused")
db.add(user)
db.commit()
user_id = user.id
class Host:
def __init__(self):
self.sessions = sessions
self.engine = engine
self.user = user_id
self.counter = itertools.count(1)
def task(
self, source="internal", channel_name=None, declared=None, priority=None
):
with sessions() as db:
task = Task(
user_id=user_id,
title="Work",
status=TaskStatus.PENDING,
source=source,
channel_name=channel_name,
requested_dispatch_class=declared,
dispatch_priority=priority,
)
db.add(task)
db.commit()
return task.id
def enqueue(self, kind=KINDS.START, *, task_id=None, command_id=None):
task_id = task_id or self.task()
with sessions() as db:
return transport.enqueue_task_command(
db,
task_id=task_id,
actor_user_id=user_id,
command_id=command_id or f"{kind.value}-{task_id}",
kind=kind,
payload={},
)
def commands(self):
with sessions() as db:
return db.query(TaskExecutionCommand).count()
def seed(self, task_id, kind, dispatch_class):
with sessions() as db:
db.add(
TaskExecutionCommand(
task_id=task_id,
command_id=f"seed-{next(self.counter)}",
kind=kind,
payload={},
status="completed",
dispatch_class=dispatch_class,
)
)
db.commit()
def stage(self, kind, *, task_id, command_id=None, reserved=False):
command_id = command_id or (
f"auto-resume:{next(self.counter)}"
if reserved
else f"{kind.value}-{next(self.counter)}"
)
with sessions() as db:
staged = transport.stage_task_command(
db,
task_id=task_id,
actor_user_id=user_id,
command_id=command_id,
kind=kind,
payload={},
reserved=reserved,
)
db.commit()
return staged
def klass(self, db_id):
with sessions() as db:
return db.execute(
sa.select(TaskExecutionCommand.dispatch_class).where(
TaskExecutionCommand.id == db_id
)
).scalar_one()
def priority(self, db_id):
with sessions() as db:
return db.execute(
sa.select(TaskExecutionCommand.priority).where(
TaskExecutionCommand.id == db_id
)
).scalar_one()
def set_task_priority(self, task_id, priority):
with sessions() as db:
db.execute(
sa.update(Task)
.where(Task.id == task_id)
.values(dispatch_priority=priority)
)
db.commit()
def tickets(self):
with sessions() as db:
return [
t.bucket_key
for t in db.query(TaskAdmissionTicket).order_by("command_id")
]
try:
yield Host()
finally:
engine.dispose()
def policy(bucket="tenant:batch", capacity=1, max_pending=20):
return admission.AdmissionPolicy(bucket, capacity, max_pending)
def counting(result=None):
calls = []
def classify(db, command, view):
calls.append((command.task_id, command.kind))
return result() if result else admission.CommandClassification()
classify.calls = calls
return classify
def test_default_classifier_stages_no_ticket(host):
assert admission._installed.classifier is admission.upstream_command_classifier
assert admission.admission_enabled() is False
host.enqueue()
assert host.tickets() == []
with host.sessions() as db:
row = db.query(TaskExecutionCommand).one()
assert (row.kind, row.status) == ("start", "pending")
def test_classifier_called_once_per_execution_command(host):
classify = counting()
admission.set_command_classifier(classify, admission_enabled=False)
for kind in EXECUTION:
host.enqueue(kind)
assert sorted(kind for _, kind in classify.calls) == sorted(
kind.value for kind in EXECUTION
)
for kind in CONTROL:
host.enqueue(kind)
assert len(classify.calls) == len(EXECUTION)
task_id = host.task()
host.enqueue(KINDS.START, task_id=task_id, command_id="same")
seen = len(classify.calls)
replay = host.enqueue(KINDS.START, task_id=task_id, command_id="same")
assert replay.created is False
assert len(classify.calls) == seen
def test_admission_refusals_still_propagate(host):
admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(
admission=policy(max_pending=1)
),
admission_enabled=True,
)
host.enqueue(command_id="first")
assert host.commands() == 1
with pytest.raises(admission.AdmissionQueueFull):
host.enqueue(command_id="second")
assert host.commands() == 1
class Refused(Exception):
pass
def refuse(db, command, view):
raise Refused
admission.set_command_classifier(refuse, admission_enabled=True)
with pytest.raises(Refused):
host.enqueue(command_id="third")
assert host.commands() == 1
@pytest.mark.parametrize(
"install, expected",
[
pytest.param(lambda: None, False, id="unregistered"),
pytest.param(
lambda: admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(),
admission_enabled=False,
),
False,
id="declared-false",
),
pytest.param(
lambda: admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(),
admission_enabled=True,
),
True,
id="declared-true",
),
pytest.param(
lambda: admission.set_task_admission_hook(lambda db, command: None),
True,
id="legacy-hook",
),
pytest.param(
lambda: (
admission.set_task_admission_hook(lambda db, command: None),
admission.set_task_admission_hook(None),
),
False,
id="legacy-hook-cleared",
),
],
)
def test_admission_enabled_reflects_declaration(host, monkeypatch, install, expected):
install()
assert admission.admission_enabled() is expected
# waiting_for_capacity: with every command treated as blocked, only a
# declared admission host reports the wait.
monkeypatch.setattr(admission, "admission_eligible", lambda: sa.false())
command_id = host.enqueue().command_id
assert host.tickets() == []
with host.sessions() as db:
assert admission.waiting_for_capacity(db, command_id) is expected
# enqueue_resume_input acknowledges a queued reply under the same value.
async def passthrough(call):
return call()
monkeypatch.setattr(task_resume_command, "run_db_io_cancellation_safe", passthrough)
monkeypatch.setattr(task_resume_command, "_admit_reply", lambda *a: 1)
monkeypatch.setattr(task_resume_command, "_read_reply_outcome", lambda _id: None)
capacity_wait = Mock(
return_value={"run_id": "r", "state_version": 1, "control_state": "waiting"}
)
monkeypatch.setattr(task_resume_command, "_read_capacity_wait", capacity_wait)
monkeypatch.setattr(task_resume_command, "notify_task_command_dispatcher", Mock())
monkeypatch.setattr(
task_resume_command, "get_task_reply_wait_timeout_seconds", lambda: 0
)
from xagent.web.services import task_event_bridge
monkeypatch.setattr(task_event_bridge, "_bridge", Mock())
reply = Mock(command_id="c1")
async def submit():
return await task_resume_command.enqueue_resume_input(
reply, source="sdk", message_id="m"
)
if expected:
assert asyncio.run(submit()).queued is True
else:
with pytest.raises(task_resume_command.TaskResumeOutcomeUnknownError):
asyncio.run(submit())
# The acknowledge_queued gate: the capacity wait is read only when the
# installed classifier declared admission.
assert capacity_wait.called is expected
def test_classifier_sees_flushed_command(host):
seen = []
def classify(db, command, view):
seen.append((command.id is not None, command.kind, command.task_id))
return admission.CommandClassification()
admission.set_command_classifier(classify, admission_enabled=False)
task_id = host.task()
host.enqueue(KINDS.MESSAGE, task_id=task_id)
assert seen == [(True, "message", task_id)]
def test_classifier_result_shape_is_enforced(host):
admission.set_command_classifier(
lambda db, command, view: policy(), admission_enabled=True
)
with pytest.raises(TypeError, match="must return CommandClassification"):
host.enqueue(command_id="legacy-shape")
assert host.commands() == 0
admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(admission=policy()),
admission_enabled=False,
)
with pytest.raises(TypeError, match="admission_enabled=False"):
host.enqueue(command_id="undeclared")
assert host.commands() == 0
with pytest.raises(TypeError):
admission.CommandClassification(policy()) # type: ignore[misc]
with pytest.raises(ValueError):
admission.CommandClassification(dispatch_class="urgent") # type: ignore[arg-type]
assert admission.CommandClassification(admission=policy()).dispatch_class == (
"batch"
)
def test_legacy_hook_wrapper_matches_old_behavior(host):
received = []
def hook(db, command):
received.append((db, command))
return policy("legacy:bucket")
admission.set_task_admission_hook(hook)
assert admission.admission_enabled() is True
described = admission._describe(admission._installed.classifier)
assert hook.__qualname__ in described
assert "legacy_admission_hook" not in described
host.enqueue()
assert host.tickets() == ["legacy:bucket"]
assert len(received) == 1
db, command = received[0]
assert isinstance(db, sa.orm.Session)
assert isinstance(command, TaskExecutionCommand)
# The class follows the upstream rule: a message of an internal task is
# interactive, a start of an sdk task is batch. The hook never sees a view.
with host.sessions() as db:
assert db.query(TaskExecutionCommand).one().dispatch_class == "interactive"
sdk_task = host.task(source="sdk")
host.enqueue(KINDS.START, task_id=sdk_task)
with host.sessions() as db:
sdk_row = db.query(TaskExecutionCommand).filter_by(task_id=sdk_task).one()
assert sdk_row.dispatch_class == "batch"
assert len(received) == 2
def test_classifier_reset_fixture_isolates_tests_first():
admission.set_command_classifier(counting(), admission_enabled=True)
assert admission.admission_enabled() is True
def test_classifier_reset_fixture_isolates_tests():
assert admission._installed.classifier is admission.upstream_command_classifier
assert admission.admission_enabled() is False
@pytest.mark.parametrize("bad", [None, "classifier"])
def test_set_command_classifier_rejects_non_callable(bad):
with pytest.raises(TypeError):
admission.set_command_classifier(bad, admission_enabled=False)
assert admission._installed.classifier is admission.upstream_command_classifier
@pytest.mark.parametrize("bad", ["false", 1, None])
def test_set_command_classifier_rejects_non_bool_declaration(bad):
before = admission._installed
with pytest.raises(TypeError, match="admission_enabled must be a bool"):
admission.set_command_classifier(counting(), admission_enabled=bad)
assert admission._installed is before
assert admission._installed.classifier is admission.upstream_command_classifier
def test_legacy_clear_leaves_a_new_style_classifier_installed():
classify = counting()
admission.set_command_classifier(classify, admission_enabled=True)
admission.set_task_admission_hook(None)
assert admission._installed.classifier is classify
assert admission.admission_enabled() is True
admission.set_task_admission_hook(lambda db, command: None)
admission.set_task_admission_hook(None)
assert admission._installed.classifier is admission.upstream_command_classifier
# ---------------------------------------------------------------- I-4
_SOURCES = sorted(
INTERACTION_ORIGIN_VOCABULARY | {"external", None, "", "SDK", "future_unknown"},
key=lambda value: (value is not None, value or ""),
)
# Hand-written expectations, one literal per source.
_SOURCE_CLASS = {
"internal": I,
"widget": I,
"shared_link": I,
"sdk": B,
"trigger": B,
"a2a": B,
"external": None,
None: I,
"": I,
"SDK": None,
"future_unknown": None,
}
_FOREGROUND = {
"internal": {"resume_input": I, "other": I},
"widget": {"resume_input": I, "other": I},
"shared_link": {"resume_input": I, "other": I},
"sdk": {"resume_input": I, "other": B},
"trigger": {"resume_input": I, "other": B},
"a2a": {"resume_input": I, "other": B},
"external": {"resume_input": I, "other": B},
None: {"resume_input": I, "other": I},
"": {"resume_input": I, "other": I},
"SDK": {"resume_input": I, "other": B},
"future_unknown": {"resume_input": I, "other": B},
}
_BACKGROUND_UNKNOWN_SOURCE = {None: B, I: I, B: B}
def _view(source, *, background, previous, declared=None):
return admission.TaskClassificationView(
task_id=1,
source=source,
channel_name=None,
owner_user_id=1,
background=background,
previous_dispatch_class=previous,
requested_dispatch_class=declared,
dispatch_priority=None,
)
# A declaration that is not a dispatch class counts as no declaration.
_DECLARATIONS = (None, I, B, "bogus")
_SWITCH = (False, True)
def _expected_table():
table = {}
for source, kind, background, previous, declared, accept, fair in itertools.product(
_SOURCES, KINDS, (False, True), (None, I, B), _DECLARATIONS, _SWITCH, _SWITCH
):
accepted = declared in (I, B) and accept and fair
if background and accepted:
expected = declared
elif background and _SOURCE_CLASS[source] is not None:
expected = _SOURCE_CLASS[source]
elif background:
expected = _BACKGROUND_UNKNOWN_SOURCE[previous]
elif kind is KINDS.RESUME_INPUT:
expected = I
elif accepted:
expected = declared
else:
expected = _FOREGROUND[source]["other"]
table[(source, kind, background, previous, declared, accept, fair)] = expected
return table
def test_upstream_rule_table_covers_the_whole_input_space(monkeypatch):
table = _expected_table()
assert set(table) == set(
itertools.product(
_SOURCES,
KINDS,
(False, True),
(None, I, B),
_DECLARATIONS,
_SWITCH,
_SWITCH,
)
)
assert len(table) == 11 * 6 * 2 * 3 * 4 * 2 * 2
for key, expected in table.items():
source, kind, background, previous, declared, accept, fair = key
monkeypatch.setenv("XAGENT_DISPATCH_ACCEPT_REQUESTED_CLASS", str(accept))
monkeypatch.setenv("XAGENT_DISPATCHER_FAIR_SHARE", str(fair))
view = _view(
source, background=background, previous=previous, declared=declared
)
assert admission.upstream_dispatch_class(view, kind.value) == expected, key
# ---------------------------------------------------------------- I-24 and the view
def test_classifier_receives_the_task_view(host):
seen = []
def classify(db, command, view):
seen.append(view)
return admission.CommandClassification()
admission.set_command_classifier(classify, admission_enabled=False)
# The view carries the stored value as it is, even one that is not a class.
task_id = host.task(
source="widget", channel_name="support", declared="bogus", priority=3
)
host.stage(KINDS.START, task_id=task_id)
assert seen == [
admission.TaskClassificationView(
task_id=task_id,
source="widget",
channel_name="support",
owner_user_id=host.user,
background=False,
previous_dispatch_class=None,
requested_dispatch_class="bogus",
dispatch_priority=3,
)
]
def _previous_queries():
statements = []
def record(_conn, _cursor, statement, *_args):
if "task_execution_commands.dispatch_class" in statement and (
"LIMIT" in statement and "ORDER BY" in statement
):
statements.append(statement)
return statements, record
def test_background_resume_is_classified_by_source_not_by_the_previous_command(host):
# An SDK task: start, then a reply (interactive by kind), then a background
# resume. The reply must not make the resume interactive.
task_id = host.task(source="sdk")
start = host.stage(KINDS.START, task_id=task_id)
assert host.klass(start.staged_db_id) == B
reply = host.stage(KINDS.RESUME_INPUT, task_id=task_id)
assert host.klass(reply.staged_db_id) == I
resume = host.stage(KINDS.RESUME, task_id=task_id, reserved=True)
assert host.klass(resume.staged_db_id) == B
def test_background_resume_inherits_interactive_only_for_an_unknown_source(host):
task_id = host.task(source="custom")
host.seed(task_id, "start", I)
resume = host.stage(KINDS.RESUME, task_id=task_id, reserved=True)
assert host.klass(resume.staged_db_id) == I
def test_background_resume_sees_the_previous_class_in_its_view(host):
seen = []
def classify(db, command, view):
seen.append((view.background, view.previous_dispatch_class))
return admission.CommandClassification()
admission.set_command_classifier(classify, admission_enabled=False)
task_id = host.task(source="custom")
host.seed(task_id, "start", I)
host.seed(task_id, "message", B)
host.stage(KINDS.RESUME, task_id=task_id, reserved=True)
assert seen == [(True, B)]
def test_the_previous_command_is_read_only_for_background_commands(host):
statements, record = _previous_queries()
sa.event.listen(host.engine, "before_cursor_execute", record)
try:
sdk = host.task(source="sdk")
host.stage(KINDS.START, task_id=sdk)
host.stage(KINDS.RESUME_INPUT, task_id=sdk)
assert statements == []
host.stage(KINDS.RESUME, task_id=sdk, reserved=True)
assert len(statements) == 1
finally:
sa.event.remove(host.engine, "before_cursor_execute", record)
def test_a_custom_classifier_sees_the_previous_class_of_a_known_source(host):
seen = []
def classify(db, command, view):
seen.append((view.source, view.background, view.previous_dispatch_class))
return admission.CommandClassification()
admission.set_command_classifier(classify, admission_enabled=False)
task_id = host.task(source="sdk")
host.seed(task_id, "message", I)
host.stage(KINDS.RESUME, task_id=task_id, reserved=True)
assert seen == [("sdk", True, I)]
def test_a_classifier_with_the_old_signature_is_refused_at_install():
with pytest.raises(TypeError, match=r"accept \(db, command, view\)"):
admission.set_command_classifier(
lambda db, command: admission.CommandClassification(),
admission_enabled=False,
)
assert admission._installed.classifier is admission.upstream_command_classifier
def test_a_classifier_without_an_inspectable_signature_is_installed(monkeypatch):
def no_signature(_callable, **_kwargs):
raise ValueError("no signature found")
monkeypatch.setattr(admission.inspect, "signature", no_signature)
classify = counting()
admission.set_command_classifier(classify, admission_enabled=False)
assert admission._installed.classifier is classify
# ---------------------------------------------------------------- I-33
@pytest.fixture
def no_autoflush(host):
"""A session built the way production builds them (models/database.py)."""
factory = sessionmaker(host.engine, autoflush=False, expire_on_commit=False)
with factory() as probe:
assert probe.autoflush is False
return factory
def _install_by_kind(admission_enabled=False):
def classify(db, command, view):
klass = I if command.kind in ("message", "resume_input") else B
policy = (
admission.AdmissionPolicy("tenant:test", 5, 20)
if admission_enabled
else None
)
return admission.CommandClassification(dispatch_class=klass, admission=policy)
admission.set_command_classifier(classify, admission_enabled=admission_enabled)
def _count_updates(engine):
updates = []
def record(_conn, _cursor, statement, *_args):
if statement.lstrip().upper().startswith("UPDATE TASK_EXECUTION_COMMANDS"):
updates.append(statement)
return updates, record
@pytest.mark.parametrize("with_ticket", [False, True], ids=["no-ticket", "ticket"])
def test_dispatch_class_is_written_before_commit(host, no_autoflush, with_ticket):
_install_by_kind(admission_enabled=with_ticket)
task_id = host.task(source="internal")
with no_autoflush() as db:
staged = transport.stage_task_command(
db,
task_id=task_id,
actor_user_id=host.user,
command_id="interactive-1",
kind=KINDS.MESSAGE,
payload={},
)
# Before commit, read through the connection: no session autoflush.
assert (
db.connection()
.execute(
sa.text(
"SELECT dispatch_class FROM task_execution_commands WHERE id = :id"
),
{"id": staged.staged_db_id},
)
.scalar_one()
== I
)
db.commit()
with host.sessions() as fresh:
assert fresh.get(TaskExecutionCommand, staged.staged_db_id).dispatch_class == I
@pytest.mark.parametrize(
("kind", "expected_class", "expected_updates"),
[(KINDS.START, B, 0), (KINDS.MESSAGE, I, 1)],
ids=["batch-no-update", "interactive-one-update"],
)
def test_class_write_costs_one_update_only_when_it_differs_from_the_default(
host, kind, expected_class, expected_updates
):
_install_by_kind()
updates, record = _count_updates(host.engine)
sa.event.listen(host.engine, "before_cursor_execute", record)
try:
task_id = host.task(source="sdk")
staged = host.stage(kind, task_id=task_id)
finally:
sa.event.remove(host.engine, "before_cursor_execute", record)
assert host.klass(staged.staged_db_id) == expected_class
assert len(updates) == expected_updates
def test_control_commands_are_not_classified_and_stay_batch(host):
calls = []
def classify(db, command, view):
calls.append(command.kind)
return admission.CommandClassification(dispatch_class=I)
admission.set_command_classifier(classify, admission_enabled=False)
task_id = host.task(source="internal")
pause = host.stage(KINDS.PAUSE, task_id=task_id)
cancel = host.stage(KINDS.CANCEL, task_id=task_id)
assert calls == []
assert host.klass(pause.staged_db_id) == B
assert host.klass(cancel.staged_db_id) == B
def test_an_idempotent_replay_neither_classifies_nor_rewrites_the_class(host):
_install_by_kind()
task_id = host.task(source="sdk")
first = host.stage(KINDS.MESSAGE, task_id=task_id, command_id="same")
assert host.klass(first.staged_db_id) == I
calls = []
def always_batch(db, command, view):
calls.append(command.id)
return admission.CommandClassification(dispatch_class=B)
admission.set_command_classifier(always_batch, admission_enabled=False)
replay = host.stage(KINDS.MESSAGE, task_id=task_id, command_id="same")
assert replay.created is False
assert replay.staged_db_id == first.staged_db_id
assert calls == []
assert host.klass(first.staged_db_id) == I
def test_a_classifier_exception_rolls_the_acceptance_back(host):
class Refused(Exception):
pass
def refuse(db, command, view):
raise Refused
admission.set_command_classifier(refuse, admission_enabled=False)
task_id = host.task(source="sdk")
with host.sessions() as db:
with pytest.raises(Refused):
transport.stage_task_command(
db,
task_id=task_id,
actor_user_id=host.user,
command_id="refused",
kind=KINDS.MESSAGE,
payload={},
)
db.rollback()
with host.sessions() as db:
assert db.query(TaskExecutionCommand).count() == 0
# ---------------------------------------------------------------- I-19
def test_the_class_survives_defer_failure_and_retry(host, monkeypatch):
monkeypatch.setenv("XAGENT_SHARED_TASK_EXECUTION_ENABLED", "false")
monkeypatch.setattr(transport, "get_runner_id", lambda: "runner-a")
_install_by_kind()
task_id = host.task(source="sdk")
staged = host.stage(KINDS.MESSAGE, task_id=task_id)
command_id = staged.staged_db_id
assert host.klass(command_id) == I
# The deployment now classifies everything as batch.
admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(dispatch_class=B),
admission_enabled=False,
)
with host.sessions() as db:
claimed = transport.claim_task_command(
db, runner_id="runner-a", command_db_id=command_id
)
assert claimed is not None
assert host.klass(command_id) == I
assert transport.defer_task_command(command_id, "runner-a", "later")
assert host.klass(command_id) == I
with host.sessions() as db:
row = db.get(TaskExecutionCommand, command_id)
row.claim_expires_at = None
row.retry_available_at = None
db.commit()
claimed = transport.claim_task_command(
db, runner_id="runner-a", command_db_id=command_id
)
assert claimed is not None
assert transport.fail_task_command(
command_id, "runner-a", "boom", force_terminal=True
)
assert host.klass(command_id) == I
with host.sessions() as db:
assert db.get(TaskExecutionCommand, command_id).status == "failed"
assert transport.retry_failed_task_command(db, command_id)
assert host.klass(command_id) == I
# ---------------------------------------------------------------- declared class
ACCEPT = "XAGENT_DISPATCH_ACCEPT_REQUESTED_CLASS"
FAIR = "XAGENT_DISPATCHER_FAIR_SHARE"
def _settings(monkeypatch, *, accept, fair):
monkeypatch.setenv(ACCEPT, str(accept))
monkeypatch.setenv(FAIR, str(fair))
def test_each_command_judges_the_gates_when_it_is_enqueued(host, monkeypatch):
monkeypatch.delenv(ACCEPT, raising=False)
monkeypatch.delenv(FAIR, raising=False)
task_id = host.task(source="sdk", declared=I)
queued: list[tuple[int, str]] = []
def step(kind, expected, *, reserved=False):
queued.append(
(
host.stage(kind, task_id=task_id, reserved=reserved).staged_db_id,
expected,
)
)
for db_id, earlier in queued:
assert host.klass(db_id) == earlier
step(KINDS.START, B)
_settings(monkeypatch, accept=True, fair=True)
step(KINDS.MESSAGE, I)
_settings(monkeypatch, accept=False, fair=True)
step(KINDS.RESUME, B)
_settings(monkeypatch, accept=True, fair=True)
step(KINDS.RESUME, I, reserved=True)
_settings(monkeypatch, accept=True, fair=False)
step(KINDS.MESSAGE, B)
def test_no_declaration_reads_neither_setting(host, monkeypatch):
def boom(*_args, **_kwargs):
raise AssertionError("a setting was read")
monkeypatch.setattr(admission, "get_dispatch_accept_requested_class_enabled", boom)
monkeypatch.setattr(transport, "_fair_share_enabled", boom)
task_id = host.task(source="sdk")
assert host.klass(host.stage(KINDS.START, task_id=task_id).staged_db_id) == B
assert host.klass(host.stage(KINDS.MESSAGE, task_id=task_id).staged_db_id) == B
def test_an_unknown_stored_value_is_ignored_and_logged_once(host, monkeypatch, caplog):
_settings(monkeypatch, accept=True, fair=True)
task_id = host.task(source="internal", declared="bogus")
with caplog.at_level(logging.WARNING, logger=admission.logger.name):
staged = host.stage(KINDS.START, task_id=task_id)
assert host.klass(staged.staged_db_id) == I
messages = [
r.getMessage()
for r in caplog.records
if "requested dispatch class" in r.getMessage()
]
assert len(messages) == 1
assert "bogus" in messages[0]
def test_staging_reads_the_declaration_in_the_one_task_snapshot(host):
statements = []
def record(_conn, _cursor, statement, *_args):
if "FROM tasks" in statement and "tasks.channel_name" in statement:
statements.append(statement)
task_id = host.task(source="sdk", declared=I)
sa.event.listen(host.engine, "before_cursor_execute", record)
try:
host.stage(KINDS.START, task_id=task_id)
finally:
sa.event.remove(host.engine, "before_cursor_execute", record)
assert len(statements) == 1
assert "tasks.requested_dispatch_class" in statements[0]
# --- priority -------------------------------------------------------------------
# (kind, staged by the system itself) of every command the acceptance step sees.
PRIORITY_SCENARIOS = {
"start": (KINDS.START, False),
"message": (KINDS.MESSAGE, False),
"manual resume": (KINDS.RESUME, False),
"automatic resume": (KINDS.RESUME, True),
"answer": (KINDS.RESUME_INPUT, False),
"pause": (KINDS.PAUSE, False),
"cancel": (KINDS.CANCEL, False),
}
@pytest.mark.parametrize("scenario", PRIORITY_SCENARIOS)
@pytest.mark.parametrize("declared", [None, -10, 0, 7])
def test_a_command_inherits_the_tasks_priority_and_a_control_command_gets_zero(
host, declared, scenario
):
kind, reserved = PRIORITY_SCENARIOS[scenario]
task_id = host.task(priority=declared)
staged = host.stage(kind, task_id=task_id, reserved=reserved)
expected = 0 if kind in CONTROL else (declared or 0)
assert host.priority(staged.staged_db_id) == expected
@pytest.mark.parametrize("returned,expected", [(10, 10), (0, 0), (-10, -10), (None, 7)])
def test_a_priority_the_classifier_returns_wins_and_none_keeps_the_inherited_one(
host, returned, expected
):
admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(priority=returned),
admission_enabled=False,
)
task_id = host.task(priority=7)
staged = host.stage(KINDS.MESSAGE, task_id=task_id)
assert host.priority(staged.staged_db_id) == expected
@pytest.mark.parametrize("value", [-11, 11, True, False, 1.5, "1"])
def test_any_other_priority_is_refused(value):
for fine in (-10, 0, 10, None):
assert admission.CommandClassification(priority=fine).priority == fine
with pytest.raises(ValueError, match="Invalid priority"):
admission.CommandClassification(priority=value) # type: ignore[arg-type]
def test_the_priority_is_written_in_the_flush_of_the_dispatch_class(host, no_autoflush):
admission.set_command_classifier(
lambda db, command, view: admission.CommandClassification(priority=4),
admission_enabled=False,
)
task_id = host.task()
with no_autoflush() as db:
staged = transport.stage_task_command(
db,
task_id=task_id,
actor_user_id=host.user,
command_id="message-1",
kind=KINDS.MESSAGE,
payload={},
)
stored = db.execute(
sa.select(TaskExecutionCommand.priority).where(
TaskExecutionCommand.id == staged.staged_db_id
)
).scalar_one()
db.rollback()
assert stored == 4
def test_a_command_keeps_the_priority_it_was_accepted_with(host):
task_id = host.task(priority=3)
first = host.stage(KINDS.MESSAGE, task_id=task_id)
host.set_task_priority(task_id, 5)
second = host.stage(KINDS.MESSAGE, task_id=task_id)
assert (host.priority(first.staged_db_id), host.priority(second.staged_db_id)) == (
3,
5,
)