foley.agent.policy#
The SELECT policy: the fail-closed rights gate + the single generate-vs-retrieve branch.
Two load-bearing invariants live here as two physically separate pure functions, so neither can bypass the other:
gate_candidates()— the rights gate of the SELECT loop. Runs the fail-closedfoley.licensing.checked_refusal_reason()rule over the candidate set beforeany verification/ranking (invariant #3), sets
Candidate.license_ok, and drops every non-Truecandidate. The same rule re-checks placed sounds where they are used (foley.licensing.refusals(), the MCP plan/weave verbs, #191).decide()— the ONLY generate-vs-retrieve branch. A pure function of an already-gated+verified set; it never callskeep/search/generate(thefoley.agent.toolsloop owns all side effects).
Budget bounds the refine→re-retrieve and generate loops so a hard event can’t
run away. This module is stdlib-only (imports only foley.base /
foley.licensing) and does no I/O and no obs — the loop emits the audit Steps.
Module Attributes
a run that lands on |
Functions
|
The single generate-vs-retrieve branch — a PURE function (report 05 §4). |
|
Fail-closed license gate — run BEFORE verification/ranking (invariant #3). |
Classes
|
Bounded-cost accounting: per-event loop counts, and a cumulative dollar cap (#57). |
|
What |
|
The tiny result of |
- class foley.agent.policy.Budget(max_refine_loops=1, max_generations=1, allow_generate=True, max_usd=1.0, approve_unknown_cost=None, _refines=0, _gens=0, _spent_usd=0.0, _unknown_calls=0, _lock=allocate_lock())[source]#
Bases:
objectBounded-cost accounting: per-event loop counts, and a cumulative dollar cap (#57).
Two kinds of bound:
Per event —
max_refine_loops/max_generations: the loop callsrefine_ok()/gen_ok()to test, thenspend_refine()/spend_gen()to charge;reset()zeroes them at each event.Per run, cumulative —
max_usd(default $1): every paid call (generation, paid LLM rung) is checked withauthorize()before it is made and recorded withcharge()after.reset()never clears it, so the cap holds across the wholefind/scorerun. A call whose cost is unknown (estimateNone) is refused unlessapprove_unknown_cost(or$FOLEY_APPROVE_UNKNOWN_COST=1).
- authorize(estimate_usd, *, what)[source]#
Alias of
check()(kept for callers that only test affordability).- Return type:
- check(estimate_usd, *, what)[source]#
Raise if this run cannot afford
estimate_usd(nothing is reserved).- Return type:
- property overshoot_usd: float#
Dollars spent above
max_usd(0.0while within the cap) (#293).The cap is checked before each call against its estimate;
settle()then records the actual cost, which can be higher, so a run can end above the cap. This is that excess, so a host can say “at most the cap, plus this” and clamp a quote honestly. Once it is above zero every further paid call is refused (check()seesspent_usdover the cap).>>> b = Budget(max_usd=0.5) >>> b.reserve(0.4, what="x"); b.settle(0.4, 0.7) >>> round(b.overshoot_usd, 10), round(b.spent_usd, 10) (0.2, 0.7)
- reserve(estimate_usd, *, what)[source]#
Check, then count
estimate_usdas spent at once (atomic under a lock).Reserving before the call is what keeps concurrent calls on one budget (MCP tools run in a threadpool) from all passing the check before any is charged.
settle()later replaces the reservation with the actual cost, if known.- Return type:
- reset()[source]#
Zero the spend counters so the caps apply per event, not per passage.
The
findloop calls this at the top of each event so one hard event’s refine/generate spend never starves later events (the documented per-event semantics).- Return type:
- settle(reserved_usd, actual_usd)[source]#
Replace a reservation with the actual cost (
Noneactual: keep the reservation).A reservation of unknown cost (
Nonereserved, counted as an unknown call) settled with a known actual becomes that actual: it is summed, and no longer counted as unknown (#186: a source billed per compute second, priced after). Settling a known reservation with an equal actual changes nothing. An actual above the reservation can take the run overmax_usd: it is still recorded (never hidden), shows asovershoot_usd, and the next paid call is refused.>>> b = Budget(approve_unknown_cost=True) >>> b.reserve(None, what="x"); b.settle(None, 0.0425) >>> b.spent_usd, b._unknown_calls (0.0425, 0) >>> b.reserve(0.1, what="y"); b.settle(0.1, 0.1) >>> round(b.spent_usd, 10) 0.1425
- Return type:
- property unknown_cost_calls: int#
How many calls this run made whose cost is still unknown (not in
spent_usd).Each one was approved (
approve_unknown_cost);spent_usdis then a lower bound, never the whole cost.
- without_generation()[source]#
A fresh budget with these limits and the generation fallback off.
For a retrieval-only run (
foley.score(allow_generate=False)). The copy’s counters start at zero; spend made while it is in force is still charged to every enclosing budget (foley.cost.spend_scope()), so this one’s cap holds.- Return type:
- class foley.agent.policy.DecideAction(*values)[source]#
-
What
decide()chose for one event (the single branch’s outcomes).
- class foley.agent.policy.Decision(action, candidate=None, reason='')[source]#
Bases:
objectThe tiny result of
decide();reasonfeeds the refine hint + the audit Step.
- foley.agent.policy.SPEND_TOLERANCE_USD = 1e-12#
a run that lands on
max_usdthrough float addition is within it, for the next call’s check and forovershoot_usdalike (#316).- Type:
Float noise allowed on the cap
- foley.agent.policy.decide(event, kept, verified, *, tau_retrieve, budget, loop)[source]#
The single generate-vs-retrieve branch — a PURE function (report 05 §4).
Chooses among
DecideActionfrom the already-gated (kept) and already-verified (verified) sets. It performs no I/O and never callskeep/search/generate— thefoley.agent.toolsloop acts on the returnedDecision.Policy (report 05 §4):
a verified clip clearing
tau_retrieve→USE(the best one);verified-but-low-confidence with refine budget →
REFINE(feed the reason back);a non-diegetic cue, or a diegetic gap with no verified match, with generate budget →
GENERATE;otherwise →
DROP(silence), unless a lower-confidence verified clip exists and generation is off, in which case fall back to that best-effort pick.
- Parameters:
event (
SoundEvent) – The event being resolved.kept (
list[Candidate]) – The license-clean candidates (eachlicense_ok is True).verified (
list[Candidate]) – The subset ofkeptwhose verdict matched.tau_retrieve (
float) – The confidence threshold for auto-accepting a retrieved clip.budget (
Budget) – The per-event cost budget.loop (
int) – The current refine-loop index (for the audit reason).
- Return type:
- Returns:
A
Decision.
- foley.agent.policy.gate_candidates(candidates, intended_use)[source]#
Fail-closed license gate — run BEFORE verification/ranking (invariant #3).
For each candidate: apply
checked_refusal_reason()to its sound’s licence (any error ⇒ refused, fail-closed), record whether it is kept oncandidate.license_ok, and keep ONLY where it isTrue— aNone(never gated) orFalseis dropped.verify_match()asserts its survivors are license-clean.- Parameters:
candidates (
list[Candidate]) – The retrieved shortlist (license_oktypicallyNone).intended_use (
IntendedUse) – The caller’s declared rights intent.
- Return type:
- Returns:
The license-clean sublist (each with
license_ok is True), order preserved.