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,180 @@
<?php
/**
* Call log queries for the Call Log admin page (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Executes bounded, filterable call-log queries across prv_call_meta.
*
* All queries are LIMIT/OFFSET paginated on the captured_at index.
* Filters: model, peptide_slug, date_from, date_to, status (ok/error/pruned).
* Returns structured arrays; rendering is delegated to PRV_Call_Log_Table_Renderer.
*
* Who triggers: PRV_Call_Log_Page::render_page().
* Dependencies: $wpdb, PRV_Call_Meta_Table, PRV_Call_Io_Table.
*
* @see class-prv-call-meta-table.php — Primary source table.
* @see class-prv-call-io-table.php — Joined for pruned detection.
* @see class-prv-call-log-table-renderer.php — Renders query results.
* @package PrVision
*/
class PRV_Call_Log_Query {
/**
* Default rows per page.
*/
const PAGE_SIZE = 50;
/**
* Fetch a paginated, filtered page of call-log rows.
*
* Returns metadata rows only; I/O content is fetched separately per-call.
* Side effects: Database read.
*
* @param array<string, string> $filters Keys: model, peptide, date_from,
* date_to, status (ok|error|cited|not_cited).
* @param int $page 1-based page number.
*
* @return array{rows: array<int, array<string, mixed>>, total: int, pages: int}
*/
public function get_page( array $filters, int $page = 1 ): array {
global $wpdb;
$page = max( 1, $page );
$offset = ( $page - 1 ) * self::PAGE_SIZE;
$table = PRV_Call_Meta_Table::get_table_name();
list( $where_sql, $where_args ) = $this->build_where( $filters );
// Count query.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$total = (int) $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT COUNT(*) FROM {$table} {$where_sql}",
...$where_args
)
);
$pages = $total > 0 ? (int) ceil( $total / self::PAGE_SIZE ) : 1;
// Data query.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$rows = $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT id, visibility_row, run_id, peptide_slug, model, intent_label,
tokens_in, tokens_out, cost_usd, latency_ms, cited,
http_status, captured_at, config_version
FROM {$table}
{$where_sql}
ORDER BY captured_at DESC
LIMIT %d OFFSET %d",
...$where_args,
self::PAGE_SIZE,
$offset
),
ARRAY_A
);
return array(
'rows' => is_array( $rows ) ? $rows : array(),
'total' => $total,
'pages' => $pages,
);
}
/**
* Fetch a single call's I/O record from prv_call_io.
*
* Returns null when the row has been pruned (aged out).
* Side effects: Database read.
*
* @param int $call_id PK of prv_call_meta row.
*
* @return array{prompt_text: string, response_text: string}|null Null when pruned.
*/
public function get_call_io( int $call_id ): ?array {
global $wpdb;
$table = PRV_Call_Io_Table::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$row = $wpdb->get_row(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT prompt_text, response_text FROM {$table} WHERE call_id = %d LIMIT 1",
$call_id
),
ARRAY_A
);
if ( null === $row || ! is_array( $row ) ) {
return null;
}
return array(
'prompt_text' => (string) $row['prompt_text'],
'response_text' => (string) $row['response_text'],
);
}
/**
* Build the WHERE clause and parameter list from filter array.
*
* Returns a two-element array: [sql_string, args_array].
*
* @param array<string, string> $filters See get_page() docblock.
*
* @return array{0: string, 1: array<int, mixed>}
*/
private function build_where( array $filters ): array {
$clauses = array();
$args = array();
if ( ! empty( $filters['model'] ) ) {
$clauses[] = 'model = %s';
$args[] = sanitize_text_field( $filters['model'] );
}
if ( ! empty( $filters['peptide'] ) ) {
$clauses[] = 'peptide_slug = %s';
$args[] = sanitize_text_field( $filters['peptide'] );
}
if ( ! empty( $filters['date_from'] ) ) {
$clauses[] = 'captured_at >= %s';
$args[] = sanitize_text_field( $filters['date_from'] ) . ' 00:00:00';
}
if ( ! empty( $filters['date_to'] ) ) {
$clauses[] = 'captured_at <= %s';
$args[] = sanitize_text_field( $filters['date_to'] ) . ' 23:59:59';
}
if ( ! empty( $filters['status'] ) ) {
switch ( $filters['status'] ) {
case 'error':
$clauses[] = 'http_status >= %d';
$args[] = 400;
break;
case 'cited':
$clauses[] = 'cited = %d';
$args[] = 1;
break;
case 'not_cited':
$clauses[] = 'cited = %d';
$args[] = 0;
break;
}
}
$where_sql = empty( $clauses ) ? '' : 'WHERE ' . implode( ' AND ', $clauses );
return array( $where_sql, $args );
}
}