feat: expose run step budgets to clients

This commit is contained in:
lda
2026-09-05 21:26:31 +07:00 Verified
parent 7141a8818d
commit 715035dce8
18 changed files with 458 additions and 18 deletions
+3
View File
@@ -124,6 +124,9 @@ def _run_payload(*, trace: list[dict[str, Any]] | None = None) -> dict[str, Any]
"error": None,
"output": {"result": "hello"},
"trace_count": 0 if trace is None else len(trace),
"max_steps": 10_000,
"steps_executed": 1,
"steps_remaining": 9_999,
"diagnostics": [],
"next_actions": {
"can_continue": False,
+28
View File
@@ -77,6 +77,9 @@ class _FakePort:
"error": None,
"output": {"result": "done"},
"trace_count": 0,
"max_steps": 10_000,
"steps_executed": 1,
"steps_remaining": 9_999,
"diagnostics": [],
"next_actions": {
"can_continue": False,
@@ -365,3 +368,28 @@ async def test_deployment_snapshot_defensively_copies_model_and_diagnostics() ->
assert deployment.diagnostics[0].message == "original"
await deployment.run({})
assert port.calls[-1][1]["deployment_id"] == "report.production"
@pytest.mark.asyncio
async def test_deployment_run_omits_max_steps_when_not_supplied() -> None:
artifact = _artifact()
deployment = await artifact.deploy("report.production")
port = cast(_FakePort, deployment._port)
await deployment.run({"topic": "workflow"})
run_calls = [params for name, params in port.calls if name == "run_deployment"]
assert len(run_calls) == 1
assert "max_steps" not in run_calls[0]
@pytest.mark.asyncio
async def test_deployment_run_threads_max_steps_when_supplied() -> None:
artifact = _artifact()
deployment = await artifact.deploy("report.production")
port = cast(_FakePort, deployment._port)
port.run_result.update({"max_steps": 7, "steps_executed": 2, "steps_remaining": 5})
run = await deployment.run({"topic": "workflow"}, max_steps=7)
run_calls = [params for name, params in port.calls if name == "run_deployment"]
assert run_calls[-1]["max_steps"] == 7
assert run.max_steps == 7
assert run.steps_executed == 2
assert run.steps_remaining == 5
+3
View File
@@ -73,6 +73,9 @@ def test_rich_representations_bound_large_values_and_redact_secret_like_fields()
interrupt=None,
diagnostics=(),
trace_count=1000,
max_steps=10_000,
steps_executed=3,
steps_remaining=9_997,
)
rendered = repr(run)
+67
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import inspect
from typing import Any, cast
import pytest
@@ -38,6 +39,9 @@ def _payload(
"error": None,
"output": None if status == "interrupted" else {"result": "done"},
"trace_count": 1,
"max_steps": 10_000,
"steps_executed": 1,
"steps_remaining": 9_999,
"diagnostics": [],
"next_actions": {
"can_continue": status == "interrupted",
@@ -253,3 +257,66 @@ async def test_run_snapshot_defensively_copies_nested_public_values() -> None:
assert run.diagnostics[0].message == "original"
await run.resume({"approved": True})
assert port.calls[-1][1]["run_id"] == "run-1"
def _budgeted_payload(
*, status: str = "interrupted", max_steps: int = 7
) -> dict[str, Any]:
payload = _payload(status=status)
payload.update(
{
"max_steps": max_steps,
"steps_executed": 2,
"steps_remaining": max_steps - 2,
}
)
return payload
@pytest.mark.asyncio
async def test_run_decoder_requires_budget_fields() -> None:
for field in ("max_steps", "steps_executed", "steps_remaining"):
payload = _budgeted_payload()
del payload[field]
with pytest.raises(InvalidResponse, match="workflow.runs.inspect"):
Run.from_payload(cast(WorkflowClientPort, _Port()), payload)
@pytest.mark.asyncio
async def test_run_exposes_step_budget() -> None:
run = Run.from_payload(
cast(WorkflowClientPort, _Port()), _budgeted_payload(max_steps=7)
)
assert run.max_steps == 7
assert run.steps_executed == 2
assert run.steps_remaining == 5
@pytest.mark.asyncio
async def test_refresh_preserves_step_budget() -> None:
port = _Port()
port.resume_payload = _budgeted_payload(status="completed", max_steps=9)
run = Run.from_payload(
cast(WorkflowClientPort, port), _budgeted_payload(max_steps=7)
)
refreshed = await run.refresh()
assert refreshed.max_steps == 9
assert refreshed.steps_executed == 2
assert refreshed.steps_remaining == 7
@pytest.mark.asyncio
async def test_resume_preserves_step_budget() -> None:
port = _Port()
port.resume_payload = _budgeted_payload(status="completed", max_steps=9)
run = Run.from_payload(
cast(WorkflowClientPort, port), _budgeted_payload(max_steps=7)
)
resumed = await run.resume({"approved": True})
assert resumed.max_steps == 9
assert resumed.steps_executed == 2
assert resumed.steps_remaining == 7
def test_resume_signature_excludes_max_steps() -> None:
assert "max_steps" not in inspect.signature(Run.resume).parameters