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
16 changes: 15 additions & 1 deletion packages/yugos/src/yugos/inference/calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ class ModelRequest(BaseModel):
timeout_s: float = Field(gt=0)


class AccountedRequest(BaseModel):
"""A dispatched request as accounting keeps it: everything but its conversation.

A host restoring durable history charges each call's spend and never sends
it again, so the record needs the call's identity and model, not its input.
"""

model_config = ConfigDict(frozen=True, extra="forbid", allow_inf_nan=False)
call_id: str
model: ModelChoice
max_output_tokens: int | None = Field(default=None, ge=1)
timeout_s: float = Field(gt=0)


def committed_usd(calls) -> float:
"""Sum known bills. An open reservation stays in the reservation map."""
return math.fsum(
Expand Down Expand Up @@ -89,7 +103,7 @@ class CallRecord(BaseModel):
run_id: str
question_id: str
step: int
request: ModelRequest
request: ModelRequest | AccountedRequest
response: ModelResponse


Expand Down
26 changes: 26 additions & 0 deletions packages/yugos/tests/test_concurrent_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

from yugos.inference.actions import CallAction, ModelChoice
from yugos.inference.calls import (
AccountedRequest,
CallBudget,
CallError,
CallLimits,
CallRecord,
MeteredCalls,
ModelRequest,
ModelResponse,
ProviderCallFailed,
QuestionLimitExceeded,
Expand Down Expand Up @@ -378,6 +381,29 @@ def test_restored_history_counts_against_remaining_budget_and_question_calls():
assert len(resumed.calls) == 1 and resumed.cost_usd == 0.02


def test_history_restores_from_calls_kept_without_their_conversation():
limits = CallLimits(max_cost_usd=0.03, max_question_cost_usd=0.03, max_calls_per_question=1)
client = SimpleNamespace(
quote=lambda r: 0.02,
complete=lambda r: ModelResponse(text="paid", cost_usd=0.02, latency_s=0),
)
first = MeteredCalls("r", client, limits, lambda _: None)
first.execute(ACTION, "q1", deadline=time.monotonic() + 10)
stored = first.calls[0].model_dump(mode="json")
assert isinstance(CallRecord.model_validate(stored).request, ModelRequest)
del stored["request"]["input"]
kept = CallRecord.model_validate(stored)
assert isinstance(kept.request, AccountedRequest)
assert CallRecord.model_validate(kept.model_dump(mode="json")) == kept
resumed = MeteredCalls("r", client, limits, lambda _: None)
resumed.restore((kept,))
with pytest.raises(QuestionLimitExceeded, match="call limit"):
resumed.execute(ACTION, "q1", deadline=time.monotonic() + 10)
with pytest.raises(CallError, match="budget cannot fund"):
resumed.execute(ACTION, "q2", deadline=time.monotonic() + 10)
assert len(resumed.calls) == 1 and resumed.cost_usd == 0.02


@pytest.mark.parametrize("defect", ["duplicate", "unknown_cost", "different_run"])
def test_invalid_history_is_rejected_without_partially_restoring(defect):
limits = CallLimits(max_cost_usd=1, max_question_cost_usd=1)
Expand Down