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:
94
tests/unit/test-capture-writer.php
Normal file
94
tests/unit/test-capture-writer.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests: PRV_Capture_Writer — metadata + I/O writes; security test.
|
||||
*
|
||||
* @package PrVision
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== test-capture-writer ===\n";
|
||||
prv_test_reset();
|
||||
|
||||
// ── Test 1: write_meta writes a row to prv_call_meta ─────────────────────
|
||||
$writer = new PRV_Capture_Writer();
|
||||
$call_id = $writer->write_meta(
|
||||
array(
|
||||
'visibility_row' => 10,
|
||||
'run_id' => 'abc-123',
|
||||
'peptide_slug' => 'bpc-157',
|
||||
'model' => 'perplexity/sonar',
|
||||
'intent_label' => 'what is {peptide}',
|
||||
'tokens_in' => 100,
|
||||
'tokens_out' => 250,
|
||||
'cost_usd' => 0.00123,
|
||||
'latency_ms' => 800,
|
||||
'cited' => 1,
|
||||
'http_status' => 200,
|
||||
'config_version' => 3,
|
||||
)
|
||||
);
|
||||
|
||||
prv_assert( $call_id > 0, 'write_meta returns a positive row ID' );
|
||||
$meta_rows = $GLOBALS['prv_test_state']['wpdb_call_meta_rows'];
|
||||
prv_assert( count( $meta_rows ) === 1, 'write_meta inserts exactly one call_meta row' );
|
||||
prv_assert_equals( 'bpc-157', $meta_rows[0]['peptide_slug'], 'write_meta stores peptide_slug' );
|
||||
prv_assert_equals( 100, $meta_rows[0]['tokens_in'], 'write_meta stores tokens_in' );
|
||||
prv_assert_equals( 250, $meta_rows[0]['tokens_out'], 'write_meta stores tokens_out' );
|
||||
|
||||
// ── Test 2: write_io writes a row to prv_call_io ─────────────────────────
|
||||
$ok = $writer->write_io( $call_id, 'what is BPC-157?', 'BPC-157 is a peptide...' );
|
||||
prv_assert( $ok, 'write_io returns true on success' );
|
||||
$io_rows = $GLOBALS['prv_test_state']['wpdb_call_io_rows'];
|
||||
prv_assert( count( $io_rows ) === 1, 'write_io inserts exactly one call_io row' );
|
||||
prv_assert_equals( 'what is BPC-157?', $io_rows[0]['prompt_text'], 'write_io stores prompt_text' );
|
||||
prv_assert_equals( 'BPC-157 is a peptide...', $io_rows[0]['response_text'], 'write_io stores response_text' );
|
||||
|
||||
// ── Test 3 (P0 SECURITY): 401 error record does NOT store key/Bearer ──────
|
||||
// Simulate the probe-runner error path: write_meta called with http_status=401
|
||||
// and write_io called with the rendered query only (no auth).
|
||||
prv_test_reset();
|
||||
$writer2 = new PRV_Capture_Writer();
|
||||
$writer2->write_meta(
|
||||
array(
|
||||
'visibility_row' => null,
|
||||
'run_id' => 'error-run-1',
|
||||
'peptide_slug' => 'tb-500',
|
||||
'model' => 'openai/gpt-4o',
|
||||
'intent_label' => 'what is {peptide}',
|
||||
'tokens_in' => null,
|
||||
'tokens_out' => null,
|
||||
'cost_usd' => 0.0,
|
||||
'latency_ms' => 150,
|
||||
'cited' => null,
|
||||
'http_status' => 401,
|
||||
'config_version' => 3,
|
||||
)
|
||||
);
|
||||
$writer2->write_io( 1, 'what is TB-500?', '' );
|
||||
|
||||
$err_meta = $GLOBALS['prv_test_state']['wpdb_call_meta_rows'][0];
|
||||
$err_io = $GLOBALS['prv_test_state']['wpdb_call_io_rows'][0];
|
||||
|
||||
// The error metadata must contain http_status=401.
|
||||
prv_assert_equals( 401, $err_meta['http_status'], 'Error record stores http_status 401' );
|
||||
|
||||
// The stored data must NOT contain any key-like strings.
|
||||
$all_stored = wp_json_encode( $err_meta ) . wp_json_encode( $err_io );
|
||||
prv_assert( false === strpos( $all_stored, 'Bearer' ), 'Stored data does not contain "Bearer"' );
|
||||
prv_assert( false === strpos( $all_stored, 'sk-or-' ), 'Stored data does not contain "sk-or-"' );
|
||||
prv_assert( false === strpos( $all_stored, 'Authorization' ), 'Stored data does not contain "Authorization"' );
|
||||
prv_assert( null === $err_meta['cited'], 'Error record has cited=null' );
|
||||
prv_assert_equals( 'what is TB-500?', $err_io['prompt_text'], 'Error record stores rendered query as prompt_text' );
|
||||
|
||||
// ── Test 4: update_meta_cost writes back the cost ────────────────────────
|
||||
// No direct state to assert in the stub (update is a no-op returning 1), but
|
||||
// confirm the method returns true.
|
||||
prv_test_reset();
|
||||
$writer3 = new PRV_Capture_Writer();
|
||||
$result = $writer3->update_meta_cost( 5, 0.004567 );
|
||||
prv_assert( $result, 'update_meta_cost returns true' );
|
||||
|
||||
exit( prv_test_summary() );
|
||||
91
tests/unit/test-cost-audit.php
Normal file
91
tests/unit/test-cost-audit.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests: cost audit trail — rollup reconciliation, prune, and probe-runner
|
||||
* best-effort capture.
|
||||
*
|
||||
* @package PrVision
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== test-cost-audit ===\n";
|
||||
prv_test_reset();
|
||||
|
||||
// ── Test 1: per-call cost sum reconciles to MTD via PRV_Cost_Rollup_Query ─
|
||||
// Seed the wpdb_var mock (returned by get_var) to simulate MTD total.
|
||||
$GLOBALS['prv_test_state']['wpdb_var'] = '1.23456789';
|
||||
$rollup = new PRV_Cost_Rollup_Query();
|
||||
$summary = $rollup->get_mtd_summary();
|
||||
prv_assert( $summary['total_cost'] > 0, 'get_mtd_summary returns positive cost when rows present' );
|
||||
|
||||
// Reconcile: the same wpdb_var stub also underlies PRV_Cost_Ledger.
|
||||
$ledger = new PRV_Cost_Ledger();
|
||||
$mtd_via_ledger = $ledger->get_month_to_date_usd();
|
||||
// Both read the same mock; confirm they agree.
|
||||
prv_assert_equals( (float) '1.23456789', $mtd_via_ledger, 'PRV_Cost_Ledger MTD matches mock value' );
|
||||
|
||||
// ── Test 2: project_month_end returns a numeric value ────────────────────
|
||||
$projected = $rollup->project_month_end( 1.23 );
|
||||
prv_assert( $projected >= 1.23, 'project_month_end returns at least MTD value' );
|
||||
prv_assert( is_float( $projected ), 'project_month_end returns a float' );
|
||||
|
||||
// ── Test 3: prune_now deletes only prv_call_io (not prv_call_meta) ───────
|
||||
// PRV_Prune_Cron::prune_now() runs a DELETE on prv_call_io. The wpdb stub
|
||||
// returns true for all queries; confirm it does NOT drop prv_call_meta.
|
||||
prv_test_reset();
|
||||
$deleted = PRV_Prune_Cron::prune_now();
|
||||
// The stub returns (int)true = 1; just confirm no exception and correct return type.
|
||||
prv_assert( is_int( $deleted ), 'prune_now returns an integer' );
|
||||
// Confirm prv_call_meta was not among dropped tables.
|
||||
prv_assert(
|
||||
! in_array( 'prv_call_meta', $GLOBALS['prv_test_state']['wpdb_dropped_tables'], true ),
|
||||
'prune_now does not drop prv_call_meta'
|
||||
);
|
||||
|
||||
// ── Test 4: forced capture_io exception does not break probe ──────────────
|
||||
// Create a mock capture writer that throws in write_io.
|
||||
class PRV_Capture_Writer_Throwing extends PRV_Capture_Writer {
|
||||
public function write_io( int $call_id, string $prompt_text, string $response_text ): bool {
|
||||
throw new \RuntimeException( 'Simulated capture_io failure' );
|
||||
}
|
||||
}
|
||||
|
||||
prv_test_reset();
|
||||
$GLOBALS['prv_test_state']['options']['prv_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['prv_test_state']['options']['prv_peptides'] = array( array( 'slug' => 'bpc-157', 'label' => 'BPC-157' ) );
|
||||
$GLOBALS['prv_test_state']['options']['prv_prompt_intents'] = array( 'what is {peptide}' );
|
||||
$GLOBALS['prv_test_state']['options']['prv_models_v2'] = array(
|
||||
array( 'slug' => 'perplexity/sonar', 'enabled' => true ),
|
||||
);
|
||||
$GLOBALS['prv_test_state']['options']['prv_active_config_version'] = 3;
|
||||
$GLOBALS['prv_test_state']['wpdb_var'] = '0.0'; // No prior spend.
|
||||
|
||||
$throwing_capture = new PRV_Capture_Writer_Throwing();
|
||||
$ledger2 = new PRV_Cost_Ledger();
|
||||
$runner = new PRV_Probe_Runner( $ledger2, $throwing_capture );
|
||||
|
||||
$exception_escaped = false;
|
||||
try {
|
||||
$counts = $runner->run();
|
||||
// If we got here, the exception was swallowed.
|
||||
} catch ( \Throwable $e ) {
|
||||
$exception_escaped = true;
|
||||
}
|
||||
prv_assert( ! $exception_escaped, 'forced capture_io exception does not propagate to probe runner caller' );
|
||||
prv_assert( is_array( $counts ?? null ), 'runner returns counts array even when capture_io throws' );
|
||||
|
||||
// ── Test 5: table drop methods work correctly ─────────────────────────────
|
||||
prv_test_reset();
|
||||
// Simulate what uninstall.php does: call the static drop methods directly.
|
||||
PRV_Table_Manager::drop_table();
|
||||
PRV_Call_Io_Table::drop_table();
|
||||
PRV_Call_Meta_Table::drop_table();
|
||||
|
||||
$dropped = $GLOBALS['prv_test_state']['wpdb_dropped_tables'];
|
||||
prv_assert( in_array( 'prv_call_io', $dropped, true ), 'drop_table() drops prv_call_io' );
|
||||
prv_assert( in_array( 'prv_call_meta', $dropped, true ), 'drop_table() drops prv_call_meta' );
|
||||
prv_assert( in_array( 'prv_ai_visibility', $dropped, true ), 'drop_table() drops prv_ai_visibility (existing table)' );
|
||||
|
||||
exit( prv_test_summary() );
|
||||
Reference in New Issue
Block a user