diff --git a/packages/yugos/src/yugos/inference/calls.py b/packages/yugos/src/yugos/inference/calls.py index 93499ba..9810c17 100644 --- a/packages/yugos/src/yugos/inference/calls.py +++ b/packages/yugos/src/yugos/inference/calls.py @@ -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( @@ -89,7 +103,7 @@ class CallRecord(BaseModel): run_id: str question_id: str step: int - request: ModelRequest + request: ModelRequest | AccountedRequest response: ModelResponse diff --git a/packages/yugos/tests/test_concurrent_calls.py b/packages/yugos/tests/test_concurrent_calls.py index 5aeae19..0f8fb73 100644 --- a/packages/yugos/tests/test_concurrent_calls.py +++ b/packages/yugos/tests/test_concurrent_calls.py @@ -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, @@ -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)