test: cover ai platform demo workflow
This commit is contained in:
@@ -242,7 +242,7 @@ def _repair_output(
|
||||
f"校验错误:{validation_error}\n"
|
||||
f"原始输出:{raw_output}"
|
||||
)
|
||||
invocation = _invoke_for_step(run, step, prompt, "repair")
|
||||
invocation = _invoke_for_step(run, step, prompt, "structure_repair")
|
||||
attempt = step.attempts.order_by("-created_at").first()
|
||||
if attempt:
|
||||
attempt.validation_error = validation_error
|
||||
|
||||
@@ -33,6 +33,15 @@ def execute_agent_run(self, run_id: int) -> None:
|
||||
"finished_at",
|
||||
]
|
||||
)
|
||||
OperationAuditLog.objects.create(
|
||||
user=run.created_by,
|
||||
module="agents",
|
||||
action="run_finished",
|
||||
target_type="agent_run",
|
||||
target_id=str(run.public_id),
|
||||
request_snapshot={"status": "timed_out"},
|
||||
result="timed_out",
|
||||
)
|
||||
|
||||
|
||||
@shared_task(name="apps.agents.tasks.cleanup_expired_agent_runs")
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import call_command
|
||||
|
||||
from apps.agents.models import AgentRun
|
||||
from apps.agents.services import build_config_snapshot
|
||||
from apps.modelhub.services import CompletionResult
|
||||
|
||||
from .helpers import create_agent, create_deployment
|
||||
@@ -65,6 +67,32 @@ def test_agent_run_permission_is_required(client, db):
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_queued_run_can_be_cancelled(client, db, monkeypatch):
|
||||
login_admin(client)
|
||||
definition, version = create_agent(published=True)
|
||||
run = AgentRun.objects.create(
|
||||
definition=definition,
|
||||
agent_version=version,
|
||||
input_payload={"problem": "无法登录"},
|
||||
config_snapshot=build_config_snapshot(version),
|
||||
total_steps=version.steps.count(),
|
||||
)
|
||||
revoked = []
|
||||
monkeypatch.setattr(
|
||||
"apps.agents.api.AsyncResult.revoke",
|
||||
lambda self, **kwargs: revoked.append(kwargs),
|
||||
)
|
||||
run.task_id = "queued-task-id"
|
||||
run.save(update_fields=["task_id"])
|
||||
|
||||
response = client.post(f"/api/agent-runs/{run.public_id}/cancel")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "cancelled"
|
||||
assert response.json()["cancellation_requested_at"] is not None
|
||||
assert revoked == [{"terminate": False}]
|
||||
|
||||
|
||||
def test_seed_command_creates_support_agent(client, db):
|
||||
deployment = create_deployment()
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from celery.exceptions import SoftTimeLimitExceeded
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.agents.models import AgentRun
|
||||
from apps.agents.services import build_config_snapshot, execute_agent_run
|
||||
from apps.agents.tasks import cleanup_expired_agent_runs
|
||||
from apps.agents.tasks import execute_agent_run as execute_agent_run_task
|
||||
from apps.audit.models import OperationAuditLog
|
||||
from apps.modelhub.services import CompletionResult
|
||||
|
||||
from .helpers import create_agent
|
||||
@@ -65,7 +68,10 @@ def test_invalid_structure_is_repaired_once(db, monkeypatch):
|
||||
step = run.steps.get()
|
||||
assert run.status == "succeeded"
|
||||
assert step.attempts.count() == 2
|
||||
assert list(step.attempts.values_list("attempt_type", flat=True)) == ["initial", "repair"]
|
||||
assert list(step.attempts.values_list("attempt_type", flat=True)) == [
|
||||
"initial",
|
||||
"structure_repair",
|
||||
]
|
||||
|
||||
|
||||
def test_second_invalid_structure_fails_and_stops(db, monkeypatch):
|
||||
@@ -103,6 +109,45 @@ def test_cancellation_before_first_step_stops_run(db):
|
||||
assert run.steps.count() == 0
|
||||
|
||||
|
||||
def test_cancellation_after_model_response_stops_following_steps(db, monkeypatch):
|
||||
_, version = create_agent()
|
||||
run = create_run(version)
|
||||
|
||||
def request(*args, **kwargs):
|
||||
AgentRun.objects.filter(id=run.id).update(cancellation_requested_at=timezone.now())
|
||||
return CompletionResult('{"summary":"已完成当前请求"}', 3, 2)
|
||||
|
||||
monkeypatch.setattr("apps.modelhub.services._request_completion", request)
|
||||
|
||||
execute_agent_run(run.id)
|
||||
|
||||
run.refresh_from_db()
|
||||
assert run.status == "cancelled"
|
||||
assert list(run.steps.values_list("code", flat=True)) == ["structure"]
|
||||
assert run.steps.get().status == "succeeded"
|
||||
assert run.final_output is None
|
||||
|
||||
|
||||
def test_soft_time_limit_marks_run_timed_out_and_audits(db, monkeypatch):
|
||||
_, version = create_agent()
|
||||
run = create_run(version)
|
||||
monkeypatch.setattr(
|
||||
"apps.agents.tasks.execute_agent_run_service",
|
||||
lambda *_: (_ for _ in ()).throw(SoftTimeLimitExceeded()),
|
||||
)
|
||||
|
||||
execute_agent_run_task.run(run.id)
|
||||
|
||||
run.refresh_from_db()
|
||||
assert run.status == "timed_out"
|
||||
assert run.error_code == "run_timeout"
|
||||
assert OperationAuditLog.objects.filter(
|
||||
target_id=str(run.public_id),
|
||||
action="run_finished",
|
||||
result="timed_out",
|
||||
).exists()
|
||||
|
||||
|
||||
def test_cleanup_removes_expired_runs_but_keeps_audit(db, settings):
|
||||
_, version = create_agent()
|
||||
run = create_run(version)
|
||||
@@ -112,3 +157,8 @@ def test_cleanup_removes_expired_runs_but_keeps_audit(db, settings):
|
||||
|
||||
assert deleted == 1
|
||||
assert not AgentRun.objects.filter(id=run.id).exists()
|
||||
assert OperationAuditLog.objects.filter(
|
||||
module="agents",
|
||||
action="cleanup_runs",
|
||||
request_snapshot__deleted_count=1,
|
||||
).exists()
|
||||
|
||||
Reference in New Issue
Block a user