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
128 lines
3.6 KiB
PHP
128 lines
3.6 KiB
PHP
<?php
|
||
/**
|
||
* Orchestrates a full probe run: lock + delegate to PRV_Probe_Run_Executor.
|
||
*
|
||
* @package PrVision
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Orchestrates a full probe run.
|
||
*
|
||
* 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_Cost_Ledger, PRV_Capture_Writer, PRV_Run_Lock,
|
||
* PRV_Config_Version, PRV_Probe_Run_Executor.
|
||
*
|
||
* @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 {
|
||
|
||
/**
|
||
* Budget ledger for cost-cap enforcement.
|
||
*
|
||
* @var PRV_Cost_Ledger
|
||
*/
|
||
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_Capture_Writer|null $capture Injected for testing; auto-created otherwise.
|
||
*/
|
||
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.
|
||
* Returns early with sentinel -1 in skipped_error when the lock is busy.
|
||
*
|
||
* Side effects: HTTP calls, database writes, option writes.
|
||
*
|
||
* @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string}
|
||
*/
|
||
public function run(): array {
|
||
$counts = array(
|
||
'probed' => 0,
|
||
'skipped_budget' => 0,
|
||
'skipped_error' => 0,
|
||
'truncated' => false,
|
||
'run_id' => '',
|
||
);
|
||
|
||
if ( ! PRV_Run_Lock::acquire() ) {
|
||
$counts['skipped_error'] = -1; // Sentinel: lock busy.
|
||
return $counts;
|
||
}
|
||
|
||
try {
|
||
$counts = $this->execute_run();
|
||
} finally {
|
||
PRV_Run_Lock::release();
|
||
}
|
||
|
||
return $counts;
|
||
}
|
||
|
||
/**
|
||
* 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();
|
||
|
||
$executor = new PRV_Probe_Run_Executor( $this->ledger, $this->capture );
|
||
return $executor->execute( $run_id, $config_ver );
|
||
}
|
||
|
||
/**
|
||
* Generate a UUID v4 run identifier.
|
||
*
|
||
* @return string
|
||
*/
|
||
private function generate_run_id(): string {
|
||
return sprintf(
|
||
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||
wp_rand( 0, 0xffff ),
|
||
wp_rand( 0, 0xffff ),
|
||
wp_rand( 0, 0xffff ),
|
||
wp_rand( 0, 0x0fff ) | 0x4000,
|
||
wp_rand( 0, 0x3fff ) | 0x8000,
|
||
wp_rand( 0, 0xffff ),
|
||
wp_rand( 0, 0xffff ),
|
||
wp_rand( 0, 0xffff )
|
||
);
|
||
}
|
||
}
|