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:
peptiderepo
2026-06-15 09:09:29 +00:00
parent 2f4c1b2c72
commit 4eee9f5f64
37 changed files with 3137 additions and 328 deletions

View File

@@ -0,0 +1,118 @@
<?php
/**
* Daily prune cron: delete aged prv_call_io rows (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Manages the daily prune cron that deletes aged prv_call_io rows.
*
* Retention window is set in Settings (default 90 days). Only prv_call_io
* rows are deleted — prv_call_meta (cost/metadata) is kept indefinitely.
* The cron is idempotent: calling schedule() when already scheduled is safe.
*
* Cleared in the deactivator AND uninstall so no orphan cron events remain.
*
* Who triggers: PRV_Activator (schedule), PRV_Deactivator (clear),
* uninstall.php (clear), self::handle_prune_tick() (prune).
* Dependencies: $wpdb, PRV_Call_Io_Table, PRV_Config.
*
* @see class-prv-call-io-table.php — Table from which aged rows are deleted.
* @see class-prv-config.php — get_io_retention_days() for the window.
* @see class-prv-activator.php — Calls schedule() on activation.
* @see class-prv-deactivator.php — Calls clear_schedule() on deactivation.
* @see uninstall.php — Calls clear_schedule() on uninstall.
* @package PrVision
*/
class PRV_Prune_Cron {
/**
* WP-Cron hook name for the daily prune event.
*/
const PRUNE_HOOK = 'prv_daily_prune';
/**
* Register the cron action hook.
*
* Side effects: Adds action for PRV_PRUNE_HOOK.
*
* @return void
*/
public function register_hooks(): void {
add_action( self::PRUNE_HOOK, array( $this, 'handle_prune_tick' ) );
}
/**
* Callback fired by WP-Cron daily.
*
* Deletes prv_call_io rows older than the configured retention window.
* Safe to re-run — idempotent on already-pruned data.
*
* Side effects: Database delete on prv_call_io.
*
* @return int Number of rows deleted.
*/
public function handle_prune_tick(): int {
return self::prune_now();
}
/**
* Perform the prune immediately (used by handle_prune_tick + tests).
*
* Deletes only prv_call_io rows; prv_call_meta is never touched.
*
* Side effects: Database delete on prv_call_io.
*
* @return int Number of rows deleted (0 when nothing to prune).
*/
public static function prune_now(): int {
global $wpdb;
$days = PRV_Config::get_io_retention_days();
$table = PRV_Call_Io_Table::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$deleted = $wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"DELETE FROM {$table} WHERE captured_at < DATE_SUB(UTC_TIMESTAMP(), INTERVAL %d DAY)",
$days
)
);
return (int) $deleted;
}
/**
* Schedule the daily prune event.
*
* Safe to call on re-activation — no-op when already scheduled.
* Side effects: Adds a WP-Cron daily event.
*
* @return void
*/
public static function schedule(): void {
if ( ! wp_next_scheduled( self::PRUNE_HOOK ) ) {
wp_schedule_event( time(), 'daily', self::PRUNE_HOOK );
}
}
/**
* Remove the prune cron schedule.
*
* Called on deactivation and uninstall.
* Side effects: Removes the WP-Cron event.
*
* @return void
*/
public static function clear_schedule(): void {
$timestamp = wp_next_scheduled( self::PRUNE_HOOK );
if ( false !== $timestamp ) {
wp_unschedule_event( $timestamp, self::PRUNE_HOOK );
}
wp_clear_scheduled_hook( self::PRUNE_HOOK );
}
}