feat(cost): per-run cost governor — reserve-before-call enforcement

Pipeline v2 Phase 1, commit 3/4. Cost_Governor reserves each call's
worst-case estimate (prompt chars/4 + max_tokens via the #18 pricing
chain) against the run ledger row with one atomic conditional UPDATE
(the #10 lock discipline) BEFORE dispatch, then settles to actuals after
each response; failed calls release their hold. Integrated inside
OpenRouter_Provider::send_chat_completion so EVERY text-LLM call path is
guarded with zero call-site changes; the image curl_multi batch reserves
its summed estimate in Image_Pipeline before generate_image_batch.

Breach: run halted (sticky), overage recorded, Cost_Ceiling_Exception
aborts the un-dispatched call, process_next_queued_article aborts the
remaining queued articles of halted/failed runs, and the Review Queue
shows a 'Halted runs' notice (runs halted pre-post have no draft to
list). Ceiling = prautoblogger_per_run_cost_ceiling_usd setting (default
constant $0.50, snapshotted at run start, 0 = disabled). Standalone
no-run calls, the monthly cap, and the AI Gateway path are unchanged.

Under-300: provider API-key fetch+validation moved verbatim to
OpenRouter_Request_Builder::resolve_api_key().

Agent-Session: pipeline-v2-phase1-engineer-20260612
This commit is contained in:
peptiderepo
2026-06-11 04:20:30 +00:00
parent 1b04809092
commit 04f09f5f24
13 changed files with 667 additions and 47 deletions

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
/**
* phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- class naming convention differs from WordPress standard
*
* Thrown when a reserve-before-call would push a run past its cost ceiling.
*
* What: Dedicated exception type so call paths can distinguish "this run
* was halted by the per-run cost governor" from generic API
* failures. By the time it is thrown the governor has already
* marked the run `halted`, recorded the overage on the runs row,
* and logged — catchers must NOT dispatch the call they were about
* to make, and chained jobs abort via the run-status check in
* Pipeline_Runner::process_next_queued_article().
* Who triggers it: PRAutoBlogger_Cost_Governor on a failed reservation.
* Dependencies: none (extends RuntimeException).
*
* @see core/class-cost-governor.php — Sole thrower.
* @see core/class-pipeline-runner.php — Aborts queued articles of halted runs.
*/
class PRAutoBlogger_Cost_Ceiling_Exception extends \RuntimeException {
}