mirror of
https://github.com/langgenius/dify.git
synced 2026-11-30 00:28:01 +08:00
30 lines
1.4 KiB
Python
30 lines
1.4 KiB
Python
"""Consumer-owned reads of proven usage for one frozen external execution.
|
|||
|
|
|
||
|
|
Usage is observational: missing evidence never means zero and cannot change a
|
||
|
|
business result, submit work, or authorize another execution.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from datetime import datetime
|
||
|
|
from typing import Protocol
|
||
|
|
|
||
|
|
from .runtime_contracts import RuntimeScope
|
||
|
|
|
||
|
|
|
||
|
|
class ExternalExecutionUsageQuery(Protocol):
|
||
|
|
def total_tokens(self, scope: RuntimeScope, invocation_id: str, *, now: datetime) -> int | None:
|
||
|
|
"""Read the admitted owner's retry prefix through this exact Invocation.
|
||
|
|
|
||
|
|
Preconditions: scope is the admitted execution owner and now is the
|
||
|
|
observation time. Reconstruct its complete persisted identity, validate
|
||
|
|
every attempt and retry link, and count only actual business dispatches.
|
||
|
|
Different connection revisions may belong to the same legal retry chain.
|
||
|
|
|
||
|
|
Postconditions: return a nonnegative sum only when every dispatched
|
||
|
|
attempt has a proven call-total summary; a reported zero remains zero.
|
||
|
|
None means no dispatch or unavailable/incomplete ownership, retention,
|
||
|
|
retry-chain or usage evidence. Never sum Trace steps or infer zero from
|
||
|
|
a missing record. Reads have no business or provider side effects.
|
||
|
|
Infrastructure failures may raise; callers must preserve the business
|
||
|
|
result and treat a failed read as unknown usage.
|
||
|
|
"""
|
||
|
|
...
|