PR Vision v0.3.0: per-call cost accountability + LLM call audit trail
P0 security: - PRV_Gateway_Client: remove response body from RuntimeException (HTTP status only) - PRV_Capture_Writer: allowlist-only write (prompt/response text, never headers/auth) - Mandatory capture order: can_afford → probe → persist_result → update_row_cost → write_meta → capture_io - capture_io last and wrapped in swallowing try/catch New tables (SCHEMA_VERSION 3): - prv_call_meta: cost/metadata per call, kept indefinitely - prv_call_io: rendered prompt + raw response, pruned on retention window (default 90d) New classes: - PRV_Call_Meta_Table, PRV_Call_Io_Table: table schema + dbDelta - PRV_Capture_Writer: metadata + I/O write; security allowlist enforced - PRV_Prune_Cron: daily prv_daily_prune cron deletes aged prv_call_io rows only - PRV_Cost_Rollup_Query: MTD summary, model breakdown, drill-down, projection - PRV_Call_Log_Query: paginated + filtered call-log query + per-call I/O fetch - PRV_Costs_Page + PRV_Costs_Renderer: Costs admin sub-page (cap progress, bento, model table, drill-down) - PRV_Call_Log_Page + PRV_Call_Log_Table_Renderer: Call Log admin sub-page (filter bar, paginated table, WCAG AA pagination) - PRV_Call_Drawer_Renderer: non-modal complementary drawer (5 states: Normal/Pruned/Legacy/Failed/Loading) - PRV_Call_Drawer_Script: inline JS for drawer (300-line split from renderer) - PRV_Call_Detail_Ajax: wp_ajax_prv_call_detail AJAX handler - PRV_Admin_Page_Css: CSS provider (300-line split from PRV_Admin_Page) - PRV_Probe_Run_Executor: inner probe loop (300-line split from PRV_Probe_Runner) Updated: - PRV_Probe_Result: tokens_in/tokens_out params + getters - Both providers: report tokens_in/tokens_out from usage.prompt_tokens/completion_tokens - PRV_Probe_Runner: slimmed to lock+UUID+delegation; injects PRV_Capture_Writer - PRV_Config: get_io_retention_days(); PRV_IO_RETENTION_DEFAULT_DAYS constant - PRV_Upgrader/Activator/Deactivator/Plugin: register new tables, cron, pages, AJAX - Settings: retention days field + save in controller - uninstall.php: DROP both new tables, clear prv_daily_prune cron - Subnav 4-tab strip (Dashboard | Costs | Call Log | Settings) on all pages - ARCHITECTURE.md, CHANGELOG.md, CONTEXT.md, CONVENTIONS.md updated Tests: - test-capture-writer.php: write_meta, write_io, P0 security (401 record has no key/Bearer/sk-or-) - test-cost-audit.php: MTD reconciliation, projection, prune, forced capture_io exception, table drops Agent-Session: peptiderepo-geomonitor-v0.3.0-cost-audit
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Orchestrates a full probe run across peptides, intents, and models.
|
||||
* Orchestrates a full probe run: lock + delegate to PRV_Probe_Run_Executor.
|
||||
*
|
||||
* @package PrVision
|
||||
*/
|
||||
@@ -8,25 +8,24 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Orchestrates a full probe run: peptides x intents x models.
|
||||
* Orchestrates a full probe run.
|
||||
*
|
||||
* For each combination the runner:
|
||||
* 1. Acquires the run-lock (fails fast if already locked).
|
||||
* 2. Checks the monthly budget cap (aborts gracefully when hit).
|
||||
* 3. Dispatches to the correct provider (resolved from PRV_Model_Registry).
|
||||
* 4. Writes a row to the prv_ai_visibility table with config_version stamp.
|
||||
* 5. Settles the actual cost against the ledger row.
|
||||
* 6. After the run, updates per-model health and releases the lock.
|
||||
* Acquires the run-lock, generates a UUID, stamps the config version, then
|
||||
* delegates all peptide × intent × model iteration to PRV_Probe_Run_Executor.
|
||||
* Released from >300-line obligation by the executor split.
|
||||
*
|
||||
* Capture ordering (P0) is enforced in PRV_Probe_Run_Executor:
|
||||
* can_afford → probe → persist_result → update_row_cost → write_meta → capture_io
|
||||
*
|
||||
* Who triggers: PRV_Cron::handle_cron_tick() and PRV_Settings_Page (Run now).
|
||||
* Dependencies: PRV_Config, PRV_Cost_Ledger, PRV_Table_Manager,
|
||||
* PRV_Run_Lock, PRV_Model_Registry, PRV_Config_Version, all providers.
|
||||
* Dependencies: PRV_Cost_Ledger, PRV_Capture_Writer, PRV_Run_Lock,
|
||||
* PRV_Config_Version, PRV_Probe_Run_Executor.
|
||||
*
|
||||
* @see class-prv-cron.php -- Calls run() on the weekly schedule.
|
||||
* @see class-prv-cost-ledger.php -- Budget cap enforcement.
|
||||
* @see class-prv-run-lock.php -- Concurrency guard.
|
||||
* @see class-prv-model-registry.php -- Per-model health updates.
|
||||
* @see ARCHITECTURE.md -- Section Probe run flow.
|
||||
* @see class-prv-probe-run-executor.php — Inner loop implementation.
|
||||
* @see class-prv-cost-ledger.php — Budget cap enforcement.
|
||||
* @see class-prv-run-lock.php — Concurrency guard.
|
||||
* @see class-prv-capture-writer.php — Per-call metadata + I/O writer.
|
||||
* @see ARCHITECTURE.md — Section Probe run flow v0.3.0.
|
||||
* @package PrVision
|
||||
*/
|
||||
class PRV_Probe_Runner {
|
||||
@@ -38,27 +37,33 @@ class PRV_Probe_Runner {
|
||||
*/
|
||||
private PRV_Cost_Ledger $ledger;
|
||||
|
||||
/**
|
||||
* Per-call capture writer (best-effort; never in critical path).
|
||||
*
|
||||
* @var PRV_Capture_Writer
|
||||
*/
|
||||
private PRV_Capture_Writer $capture;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param PRV_Cost_Ledger|null $ledger Injected for testing; auto-created otherwise.
|
||||
* @param PRV_Cost_Ledger|null $ledger Injected for testing; auto-created otherwise.
|
||||
* @param PRV_Capture_Writer|null $capture Injected for testing; auto-created otherwise.
|
||||
*/
|
||||
public function __construct( ?PRV_Cost_Ledger $ledger = null ) {
|
||||
$this->ledger = $ledger ?? new PRV_Cost_Ledger();
|
||||
public function __construct( ?PRV_Cost_Ledger $ledger = null, ?PRV_Capture_Writer $capture = null ) {
|
||||
$this->ledger = $ledger ?? new PRV_Cost_Ledger();
|
||||
$this->capture = $capture ?? new PRV_Capture_Writer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the full probe run.
|
||||
*
|
||||
* Acquires a run-lock so the cron and "Run now" cannot collide.
|
||||
* Iterates peptides x prompt intents x models. Aborts at the budget cap
|
||||
* without throwing -- partial results are kept. Each successful probe is
|
||||
* persisted immediately so a mid-run failure doesn't lose data.
|
||||
* Returns early with sentinel -1 in skipped_error when the lock is busy.
|
||||
*
|
||||
* Side effects: HTTP calls to LLM APIs, database writes, option writes (health).
|
||||
* Side effects: HTTP calls, database writes, option writes.
|
||||
*
|
||||
* @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string}
|
||||
* Summary counts plus truncation flag for the admin display.
|
||||
*/
|
||||
public function run(): array {
|
||||
$counts = array(
|
||||
@@ -84,199 +89,21 @@ class PRV_Probe_Runner {
|
||||
}
|
||||
|
||||
/**
|
||||
* Core run logic (called inside the lock).
|
||||
* Core run logic executed inside the lock.
|
||||
*
|
||||
* Generates a run UUID, stamps the config version, then hands off to
|
||||
* PRV_Probe_Run_Executor which owns the inner loop.
|
||||
*
|
||||
* Side effects: Same as PRV_Probe_Run_Executor::execute().
|
||||
*
|
||||
* @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string}
|
||||
*/
|
||||
private function execute_run(): array {
|
||||
$run_id = $this->generate_run_id();
|
||||
$config_ver = PRV_Config_Version::get_active_version();
|
||||
$peptides = PRV_Config::get_peptides();
|
||||
$intents = PRV_Config::get_prompt_intents();
|
||||
$models = PRV_Config::get_models();
|
||||
$model_outcomes = array(); // Per-slug: {probed, errors}.
|
||||
$run_id = $this->generate_run_id();
|
||||
$config_ver = PRV_Config_Version::get_active_version();
|
||||
|
||||
foreach ( $models as $slug ) {
|
||||
$model_outcomes[ $slug ] = array(
|
||||
'probed' => 0,
|
||||
'errors' => 0,
|
||||
);
|
||||
}
|
||||
|
||||
$counts = array(
|
||||
'probed' => 0,
|
||||
'skipped_budget' => 0,
|
||||
'skipped_error' => 0,
|
||||
'truncated' => false,
|
||||
'run_id' => $run_id,
|
||||
);
|
||||
|
||||
$budget_hit = false;
|
||||
|
||||
foreach ( $peptides as $peptide ) {
|
||||
foreach ( $intents as $intent_tpl ) {
|
||||
$query = str_replace( '{peptide}', $peptide['label'], $intent_tpl );
|
||||
|
||||
foreach ( $models as $model ) {
|
||||
$provider = $this->resolve_provider( $model );
|
||||
|
||||
if ( null === $provider || ! $provider->is_configured() ) {
|
||||
++$counts['skipped_error'];
|
||||
if ( isset( $model_outcomes[ $model ] ) ) {
|
||||
++$model_outcomes[ $model ]['errors'];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Budget pre-check.
|
||||
$estimated = $this->get_estimated_cost( $model );
|
||||
if ( ! $this->ledger->can_afford( $estimated ) ) {
|
||||
++$counts['skipped_budget'];
|
||||
$budget_hit = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $provider->probe( $query );
|
||||
} catch ( \Exception $e ) {
|
||||
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
|
||||
error_log( sprintf( 'PRV: probe failed [%s / %s]: %s', $model, $peptide['slug'], $e->getMessage() ) );
|
||||
++$counts['skipped_error'];
|
||||
if ( isset( $model_outcomes[ $model ] ) ) {
|
||||
++$model_outcomes[ $model ]['errors'];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$row_id = $this->persist_result( $run_id, $peptide, $model, $intent_tpl, $result, $config_ver );
|
||||
if ( $row_id > 0 ) {
|
||||
$this->ledger->update_row_cost( $row_id, $result->get_cost_usd() );
|
||||
}
|
||||
|
||||
++$counts['probed'];
|
||||
if ( isset( $model_outcomes[ $model ] ) ) {
|
||||
++$model_outcomes[ $model ]['probed'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $budget_hit && $counts['probed'] > 0 ) {
|
||||
$counts['truncated'] = true;
|
||||
}
|
||||
|
||||
// Record truncation state for dashboard display.
|
||||
if ( $counts['truncated'] ) {
|
||||
update_option( 'prv_last_run_truncated', 1 );
|
||||
update_option( 'prv_last_run_truncated_at', current_time( 'mysql', true ) );
|
||||
} else {
|
||||
update_option( 'prv_last_run_truncated', 0 );
|
||||
}
|
||||
|
||||
// Persist run summary for dashboard.
|
||||
update_option( 'prv_last_run_at', current_time( 'mysql', true ) );
|
||||
update_option( 'prv_last_run_counts', $counts );
|
||||
|
||||
// Update per-model run-health.
|
||||
PRV_Model_Registry::update_health( $run_id, $model_outcomes );
|
||||
|
||||
// Update API-key status (derive from whether probes succeeded).
|
||||
$this->update_api_key_status( $counts, $model_outcomes );
|
||||
|
||||
return $counts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist a single probe result to the database with config_version stamp.
|
||||
*
|
||||
* @param string $run_id UUID for this run.
|
||||
* @param array<string,string> $peptide Peptide config with slug and label.
|
||||
* @param string $model Model identifier.
|
||||
* @param string $intent_tpl Raw intent template string.
|
||||
* @param PRV_Probe_Result $result Probe result.
|
||||
* @param int $config_ver Config version at run time.
|
||||
*
|
||||
* @return int Inserted row ID, or 0 on failure.
|
||||
*/
|
||||
private function persist_result(
|
||||
string $run_id,
|
||||
array $peptide,
|
||||
string $model,
|
||||
string $intent_tpl,
|
||||
PRV_Probe_Result $result,
|
||||
int $config_ver
|
||||
): int {
|
||||
global $wpdb;
|
||||
|
||||
$table = PRV_Table_Manager::get_table_name();
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$wpdb->insert(
|
||||
$table,
|
||||
array(
|
||||
'run_id' => $run_id,
|
||||
'captured_at' => current_time( 'mysql', true ),
|
||||
'peptide_slug' => $peptide['slug'],
|
||||
'peptide_label' => $peptide['label'],
|
||||
'model' => $model,
|
||||
'prompt_intent' => $intent_tpl,
|
||||
'cited' => $result->is_cited() ? 1 : 0,
|
||||
'our_position' => $result->get_our_position(),
|
||||
'source_domains' => wp_json_encode( $result->get_source_domains() ),
|
||||
'raw_excerpt' => $result->get_raw_excerpt(),
|
||||
'cost_usd' => number_format( $result->get_cost_usd(), 8, '.', '' ),
|
||||
'config_version' => $config_ver,
|
||||
),
|
||||
array( '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d' )
|
||||
);
|
||||
|
||||
return (int) $wpdb->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive + persist the API-key status from run outcomes.
|
||||
*
|
||||
* @param array<string, mixed> $counts Run counts.
|
||||
* @param array<string, array{probed: int, errors: int}> $model_outcomes Model outcomes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function update_api_key_status( array $counts, array $model_outcomes ): void {
|
||||
if ( $counts['probed'] > 0 ) {
|
||||
update_option( 'prv_api_key_status', 'ok' );
|
||||
} elseif ( $counts['skipped_error'] > 0 && 0 === $counts['probed'] ) {
|
||||
update_option( 'prv_api_key_status', 'failed' );
|
||||
}
|
||||
update_option( 'prv_api_key_last_check', current_time( 'mysql', true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate the correct provider class for a model string.
|
||||
*
|
||||
* @param string $model OpenRouter model identifier.
|
||||
*
|
||||
* @return PRV_Probe_Provider|null Null when the model is unrecognised.
|
||||
*/
|
||||
private function resolve_provider( string $model ): ?PRV_Probe_Provider {
|
||||
if ( 'perplexity/sonar' === $model ) {
|
||||
return new PRV_Perplexity_Provider();
|
||||
}
|
||||
// All other models route through the generic OpenRouter provider.
|
||||
return new PRV_OpenRouter_Provider( $model );
|
||||
}
|
||||
|
||||
/**
|
||||
* Conservative per-call cost estimate used for the budget pre-check.
|
||||
*
|
||||
* @param string $model Model identifier.
|
||||
*
|
||||
* @return float Estimated USD.
|
||||
*/
|
||||
private function get_estimated_cost( string $model ): float {
|
||||
if ( 'perplexity/sonar' === $model ) {
|
||||
return PRV_Perplexity_Provider::ESTIMATED_COST_PER_PROBE;
|
||||
}
|
||||
return PRV_OpenRouter_Provider::ESTIMATED_COST_PER_PROBE;
|
||||
$executor = new PRV_Probe_Run_Executor( $this->ledger, $this->capture );
|
||||
return $executor->execute( $run_id, $config_ver );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user