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,111 @@
<?php
/**
* Database schema for per-call cost/metadata table (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Manages the prv_call_meta table: per-call cost + metadata kept indefinitely.
*
* Two tables separate concerns introduced in v0.3.0:
* prv_call_meta — cost + metadata KEPT (this class).
* prv_call_io — rendered prompt + raw response PRUNED (PRV_Call_Io_Table).
*
* Schema is forward-only: dbDelta adds columns; no destructive ALTER.
* SCHEMA_VERSION bumped to 3 to trigger the migration guard in PRV_Upgrader.
*
* Who triggers: PRV_Upgrader::run() on every plugins_loaded (idempotent).
* Dependencies: $wpdb, dbDelta(), PRV_Table_Manager (existing table retained).
*
* @see class-prv-call-io-table.php — Companion prunable I/O table.
* @see class-prv-upgrader.php — Calls create_table() on upgrade.
* @see class-prv-capture-writer.php — Writes rows to this table.
* @see ARCHITECTURE.md — §Storage v0.3.0.
* @package PrVision
*/
class PRV_Call_Meta_Table {
/**
* Table base name (without $wpdb->prefix).
*/
const TABLE_BASE = 'prv_call_meta';
/**
* Schema version introduced by this table.
*/
const SCHEMA_VERSION = 3;
/**
* Create or upgrade the call-meta table via dbDelta.
*
* Safe to call on every plugins_loaded — dbDelta is idempotent.
* Side effects: Database write; may update prv_schema_version option.
*
* @return void
*/
public static function create_table(): void {
global $wpdb;
$table = $wpdb->prefix . self::TABLE_BASE;
$vis_table = $wpdb->prefix . 'prv_ai_visibility';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
visibility_row BIGINT(20) UNSIGNED NULL DEFAULT NULL,
run_id VARCHAR(36) NOT NULL DEFAULT '',
peptide_slug VARCHAR(200) NOT NULL DEFAULT '',
model VARCHAR(200) NOT NULL DEFAULT '',
intent_label VARCHAR(200) NOT NULL DEFAULT '',
tokens_in INT(11) NULL DEFAULT NULL,
tokens_out INT(11) NULL DEFAULT NULL,
cost_usd DECIMAL(12,8) NOT NULL DEFAULT 0.00000000,
latency_ms INT(11) NULL DEFAULT NULL,
cited TINYINT(1) NULL DEFAULT NULL,
http_status INT(11) NOT NULL DEFAULT 200,
captured_at DATETIME NOT NULL,
config_version INT(11) NULL DEFAULT NULL,
PRIMARY KEY (id),
KEY visibility_row (visibility_row),
KEY run_id (run_id),
KEY peptide_slug (peptide_slug),
KEY model (model),
KEY captured_at (captured_at)
) {$charset_collate};";
if ( ! function_exists( 'dbDelta' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
dbDelta( $sql );
update_option( 'prv_schema_version', self::SCHEMA_VERSION );
}
/**
* Get the full table name including WP prefix.
*
* @return string
*/
public static function get_table_name(): string {
global $wpdb;
return $wpdb->prefix . self::TABLE_BASE;
}
/**
* Drop the table. Called from uninstall.php only.
*
* Side effects: Permanently destroys all call metadata.
*
* @return void
*/
public static function drop_table(): void {
global $wpdb;
$table = $wpdb->prefix . self::TABLE_BASE;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
}
}