v0.3.0 — cost accountability + LLM call audit trail (#7)

Per-call recording of cost and I/O for every LLM probe. New tables prv_call_meta (schema v3) and prv_call_io. New admin pages: Costs (spend drill-down vs $5 cap) and Call Log (paginated + detail drawer). 90-day I/O retention prune via prv_daily_prune cron. P0 security: key never stored; gateway client exposes HTTP status only.

QA APPROVED 2026-06-15 SHA 09dc6db9ce
This commit is contained in:
peptiderepo
2026-06-15 17:07:08 +07:00
committed by GitHub
parent 2f4c1b2c72
commit 2e2c72cac6
37 changed files with 3246 additions and 325 deletions

View File

@@ -1,6 +1,6 @@
# Architecture — PR Vision # Architecture — PR Vision
**Version:** 0.2.1 | **Last updated:** 2026-06-14 **Version:** 0.3.0 | **Last updated:** 2026-06-15
--- ---
@@ -10,6 +10,7 @@
**v1 scope:** AI-visibility only. The collector/panel seam is present so future data categories can be added as new classes. **v1 scope:** AI-visibility only. The collector/panel seam is present so future data categories can be added as new classes.
**v2 additions:** Model manager CRUD (no deploy needed), config-version tracking, run-lock, projected cost, API-key three-state, dark "Assay" Settings UI. **v2 additions:** Model manager CRUD (no deploy needed), config-version tracking, run-lock, projected cost, API-key three-state, dark "Assay" Settings UI.
**v0.3.0 additions:** Per-call cost/metadata + I/O audit trail; Costs + Call Log admin pages; non-modal detail drawer; daily I/O prune cron; `PRV_Gateway_Client` exception body removed (P0).
--- ---
@@ -130,6 +131,42 @@ Admin page load ─────────────────────
## 4. Database ## 4. Database
### v0.3.0 additions
**Table:** `{prefix}prv_call_meta` (kept indefinitely)
| Column | Type | Notes |
|--------|------|-------|
| id | BIGINT UNSIGNED PK | Auto-increment |
| visibility_row | BIGINT UNSIGNED NULL | FK to prv_ai_visibility.id (nullable) |
| run_id | VARCHAR(36) | UUID for the run |
| peptide_slug | VARCHAR(200) | e.g. "bpc-157" |
| model | VARCHAR(200) | e.g. "perplexity/sonar" |
| intent_label | VARCHAR(200) | Prompt intent template |
| tokens_in | INT NULL | Input token count from API |
| tokens_out | INT NULL | Output token count from API |
| cost_usd | DECIMAL(12,8) | Actual call cost |
| latency_ms | INT NULL | Call latency in ms |
| cited | TINYINT(1) NULL | 1=cited, 0=not cited, NULL=error |
| http_status | INT | HTTP status (200=ok, 4xx/5xx=error) |
| captured_at | DATETIME | UTC |
| config_version | INT NULL | Config version at run time |
| io_captured | TINYINT(1) | 1 when write_io() succeeded; 0 for error rows and legacy/pre-feature rows; drives drawer state (0=Legacy, 1+io present=Normal, 1+io absent=Pruned) |
**Table:** `{prefix}prv_call_io` (pruned after retention window)
| Column | Type | Notes |
|--------|------|-------|
| id | BIGINT UNSIGNED PK | Auto-increment |
| call_id | BIGINT UNSIGNED | FK to prv_call_meta.id |
| prompt_text | LONGTEXT | Rendered prompt (plain text) |
| response_text | LONGTEXT | Raw LLM response text |
| captured_at | DATETIME | UTC (indexed for prune query) |
---
## 4a. Original Database
**Table:** `{prefix}prv_ai_visibility` **Table:** `{prefix}prv_ai_visibility`
| Column | Type | Notes | | Column | Type | Notes |

View File

@@ -7,6 +7,68 @@ Versioning: [Semantic Versioning](https://semver.org/)
--- ---
## [0.3.0] — 2026-06-15
Per-call cost accountability + LLM call audit trail. Every probe call now
produces an inspectable record with cost, tokens, latency, and the exact
rendered prompt + raw response. Two new admin pages (Costs, Call Log) with
a non-modal detail drawer. Raw I/O is pruned on a configurable retention
window (default 90 days); cost/metadata kept indefinitely.
### Fixed (pre-launch P2 cleanup)
- `io_captured TINYINT(1) NOT NULL DEFAULT 0` column added to `prv_call_meta` CREATE (baked into initial schema — table is new in v0.3.0). Set to `1` by `PRV_Capture_Writer::write_io()` on success, remains `0` for error rows and legacy/pre-feature rows. Drawer state now driven by `io_captured` instead of the `config_version < 3` heuristic: `0` = Legacy/not-captured; `1` + io row present = Normal; `1` + io row absent = Pruned (aged out). Removes a fragile inference.
### Security (P0)
- `PRV_Gateway_Client::post_to_gateway()` — removed response body from
RuntimeException message; exception now carries HTTP status code only.
Closes the pre-existing 401-body → error_log → key-leak vector.
- `PRV_Capture_Writer` — allowlist-only capture; NEVER receives `$headers`,
`Authorization`, or any raw HTTP request object. Structurally impossible
for a key to enter a stored row.
- Mandatory capture ordering enforced in `PRV_Probe_Run_Executor`:
`can_afford → probe → persist_result → update_row_cost → write_meta → capture_io`.
`capture_io` is last and wrapped in a swallowing try/catch.
### Added
- **`PRV_Call_Meta_Table`** — per-call cost/metadata table (kept indefinitely).
- **`PRV_Call_Io_Table`** — per-call I/O table (pruned on retention window).
- **`PRV_Capture_Writer`** — writes metadata + I/O rows; allowlist-only.
- **`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 call-log with filters; per-call I/O fetch.
- **`PRV_Costs_Page`** — "PR Vision > Costs" admin sub-page.
- **`PRV_Costs_Renderer`** — cap progress, bento, model table, drill-down; includes `render_subnav()` shared helper.
- **`PRV_Call_Log_Page`** — "PR Vision > Call Log" admin sub-page with filter bar.
- **`PRV_Call_Log_Table_Renderer`** — paginated table; cited% 0 = muted, >0 = lime; WCAG AA pagination.
- **`PRV_Call_Drawer_Renderer`** — non-modal `role="complementary"` drawer; 5 states (Normal, Pruned, Legacy, Failed, Loading). Focus-managed; Esc closes.
- **`PRV_Call_Drawer_Script`** — inline JS for drawer open/close/fetch/copy split for 300-line compliance.
- **`PRV_Call_Detail_Ajax`** — `wp_ajax_prv_call_detail` AJAX handler for drawer content.
- **`PRV_Admin_Page_Css`** — CSS provider split from `PRV_Admin_Page` for 300-line compliance.
- **`PRV_Probe_Run_Executor`** — inner probe loop split from `PRV_Probe_Runner`.
- `PRV_Probe_Result` — added `tokens_in`/`tokens_out` params + getters.
- Both providers — report `tokens_in`/`tokens_out` from `usage.prompt_tokens`/`completion_tokens`.
- `PRV_Config::get_io_retention_days()` — reads `prv_io_retention_days` option.
- Retention days field added to Settings form + saved by controller.
- Subnav 4-tab strip added to all PR Vision pages (Dashboard | Costs | Call Log | Settings).
- Constants `PRV_IO_RETENTION_DEFAULT_DAYS`, `PRV_DAILY_PRUNE_HOOK`.
- Tests: `test-capture-writer.php`, `test-cost-audit.php`.
### Changed
- `PRV_Probe_Runner` — slimmed to lock + UUID + delegation; inner loop moved to `PRV_Probe_Run_Executor`. Constructor now also accepts `PRV_Capture_Writer`.
- `PRV_Upgrader::run()` — creates both new tables on every `plugins_loaded`.
- `PRV_Activator::activate()` — creates new tables + schedules `prv_daily_prune` cron.
- `PRV_Deactivator::deactivate()` — clears `prv_daily_prune` cron.
- `PRV_Plugin::init()` — registers `PRV_Costs_Page`, `PRV_Call_Log_Page`, `PRV_Call_Detail_Ajax`, `PRV_Prune_Cron`.
- `PRV_Settings_Renderer` — adds subnav + retention days field.
- `PRV_Settings_Controller::handle_save()` — saves `prv_io_retention_days`.
- `uninstall.php` — DROPs `prv_call_io` + `prv_call_meta`; clears `prv_daily_prune` cron.
- Schema version bumped to 3.
### Security
- See P0 section above.
---
## [0.2.3] — 2026-06-15 ## [0.2.3] — 2026-06-15
UI-editable provider API key: encrypted at rest, write-only, wp-config precedence. UI-editable provider API key: encrypted at rest, write-only, wp-config precedence.

View File

@@ -1,6 +1,6 @@
# Context — Domain Glossary (PR Vision) # Context — Domain Glossary (PR Vision)
**Version:** 0.2.1 | **Last updated:** 2026-06-14 **Version:** 0.3.0 | **Last updated:** 2026-06-15
This file is the ubiquitous-language glossary for `pr-vision`. Every term below has a precise meaning within this codebase. AI agents and human contributors should use this terminology consistently. This file is the ubiquitous-language glossary for `pr-vision`. Every term below has a precise meaning within this codebase. AI agents and human contributors should use this terminology consistently.
@@ -141,3 +141,28 @@ The Settings page section that allows the operator to set/replace/clear the API
The symmetric key used to encrypt/decrypt `prv_provider_key_enc`. Derived via SHA-256 from `AUTH_KEY + SECURE_AUTH_KEY` (or `wp_salt()` fallback) — environment-specific and never stored. Derived fresh at every encrypt/decrypt call. The symmetric key used to encrypt/decrypt `prv_provider_key_enc`. Derived via SHA-256 from `AUTH_KEY + SECURE_AUTH_KEY` (or `wp_salt()` fallback) — environment-specific and never stored. Derived fresh at every encrypt/decrypt call.
**Code identifiers:** `PRV_Key_Store::derive_encryption_key()`, `PRV_Crypto_Helper::encrypt()`, `PRV_Crypto_Helper::decrypt()`. **Code identifiers:** `PRV_Key_Store::derive_encryption_key()`, `PRV_Crypto_Helper::encrypt()`, `PRV_Crypto_Helper::decrypt()`.
### Call Meta / Call Log
The per-call audit trail introduced in v0.3.0. Every LLM probe call produces a **call metadata** row (`prv_call_meta`) kept indefinitely, and a **call I/O** row (`prv_call_io`) retained for a configurable window.
**Code identifiers:** `PRV_Call_Meta_Table`, `PRV_Call_Io_Table`, `PRV_Capture_Writer`, `PRV_Call_Log_Query`.
### Capture Writer
The `PRV_Capture_Writer` class that writes call metadata + I/O rows after a probe. Implements a strict allowlist: stores rendered prompt text, response text, model, tokens, cost — NEVER headers, Authorization, or any raw HTTP request object.
**Code identifiers:** `PRV_Capture_Writer::write_meta()`, `PRV_Capture_Writer::write_io()`.
### Retention Window
The configurable number of days raw prompt + response text is kept in `prv_call_io` before the daily prune cron deletes it. Default 90 days. Cost/metadata in `prv_call_meta` is kept indefinitely.
**Code identifiers:** `PRV_Config::get_io_retention_days()`, `prv_io_retention_days` option, `PRV_Prune_Cron::prune_now()`.
### Detail Drawer
A non-modal, keyboard-accessible panel (`role="complementary"`) on the Call Log page that shows the exact rendered prompt + raw response for any selected call. Five states: Normal, Pruned, Legacy/not-captured, Failed, Loading. Focus-managed: opens to close button, Esc closes and returns focus to the triggering row, no tab-trap.
**Code identifiers:** `PRV_Call_Drawer_Renderer`, `PRV_Call_Drawer_Script`, `PRV_Call_Detail_Ajax`.
### Capture Ordering (P0)
The mandatory sequence within each probe iteration: `can_afford → probe → persist_result → update_row_cost → write_meta → capture_io`. `capture_io` is always last and wrapped in a swallowing try/catch so a write failure can never reach the probe path or corrupt cap accounting.
**Code identifiers:** `PRV_Probe_Run_Executor::probe_one()`.

View File

@@ -1,6 +1,6 @@
# Conventions — PR Vision # Conventions — PR Vision
**Version:** 0.2.1 | **Last updated:** 2026-06-14 **Version:** 0.3.0 | **Last updated:** 2026-06-15
Applies to all contributors and AI agents working in this repo. Where this document and `AGENT-OPERATING-STANDARD.md` conflict on process, the Standard wins; this document wins on code style. Applies to all contributors and AI agents working in this repo. Where this document and `AGENT-OPERATING-STANDARD.md` conflict on process, the Standard wins; this document wins on code style.
@@ -92,3 +92,12 @@ All key reads must go through `PRV_Key_Store::get_key()`. This maintains the pre
- AJAX handlers: call `PRV_Key_Store::get_key()` before any LLM call. - AJAX handlers: call `PRV_Key_Store::get_key()` before any LLM call.
- New provider that needs the key: implement `is_configured()` as `'' !== PRV_Key_Store::get_key()`. - New provider that needs the key: implement `is_configured()` as `'' !== PRV_Key_Store::get_key()`.
- Do NOT pass the resolved key to any client-side surface, log, exception message, or `error_log()`. - Do NOT pass the resolved key to any client-side surface, log, exception message, or `error_log()`.
## 9. Capture pattern (v0.3.0+)
The call-audit trail follows a strict allowlist pattern:
- `PRV_Capture_Writer::write_meta()` accepts only: `visibility_row`, `run_id`, `peptide_slug`, `model`, `intent_label`, `tokens_in`, `tokens_out`, `cost_usd`, `latency_ms`, `cited`, `http_status`, `config_version`.
- `PRV_Capture_Writer::write_io()` accepts only: the rendered `prompt_text` (plain text) and `response_text` (LLM response content).
- **NEVER** pass `$headers`, `$parsed_args`, `Authorization`, raw HTTP request blobs, or any field not in the above lists.
- Capture calls are LAST in the probe loop and wrapped in a swallowing try/catch — failure must never propagate to the probe path or corrupt cap accounting.

View File

@@ -19,6 +19,7 @@ declare(strict_types=1);
* *
* @see class-prv-table-manager.php — Database schema creation. * @see class-prv-table-manager.php — Database schema creation.
* @see class-prv-cron.php — Cron scheduling. * @see class-prv-cron.php — Cron scheduling.
* @see class-prv-prune-cron.php — Daily prune cron (v0.3.0).
* @package PrVision * @package PrVision
*/ */
class PRV_Activator { class PRV_Activator {
@@ -32,7 +33,12 @@ class PRV_Activator {
*/ */
public static function activate(): void { public static function activate(): void {
PRV_Table_Manager::create_table(); PRV_Table_Manager::create_table();
// v0.3.0: per-call audit trail tables.
PRV_Call_Meta_Table::create_table();
PRV_Call_Io_Table::create_table();
PRV_Config::seed_defaults(); PRV_Config::seed_defaults();
PRV_Cron::schedule_weekly(); PRV_Cron::schedule_weekly();
// v0.3.0: daily prune cron for prv_call_io.
PRV_Prune_Cron::schedule();
} }
} }

View File

@@ -0,0 +1,202 @@
<?php
/**
* Dashboard CSS provider for the PR Vision admin page.
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Provides the combined CSS for all PR Vision admin pages.
*
* Split from PRV_Admin_Page to keep that class under 300 lines.
* Returns a single CSS string covering the dark "Assay" palette,
* bento tiles, cards, chart helpers, subnav, and v0.3.0 drawer + log.
*
* Who triggers: PRV_Admin_Page::get_dashboard_css() → called via
* wp_add_inline_style() on admin_enqueue_scripts.
* Dependencies: None.
*
* @see class-prv-admin-page.php — Caller.
* @package PrVision
*/
class PRV_Admin_Page_Css {
/**
* Return the full inline CSS string for all PR Vision screens.
*
* Side effects: None.
*
* @return string CSS string.
*/
public static function get(): string {
return self::get_chrome_css() . self::get_component_css() . self::get_v030_css();
}
/**
* WP admin chrome overrides (kill white gutters around dark UI).
*
* @return string
*/
private static function get_chrome_css(): string {
return '
/* === PR Vision — WP admin chrome: kill white wrappers (scoped via conditional enqueue) === */
#wpcontent{background:#14181C;padding-left:0;}
#wpbody-content{background:#14181C;min-height:calc(100vh - 32px);padding-bottom:40px;}
#wpbody{background:#14181C;}
.wrap>h1,.prv-settings-wrap~*>h1,.wp-heading-inline{color:#EEF2F5;}
.notice.prv-proxy-note{background:#1C2228;border-left-color:#34C0CA;color:#EEF2F5;}
.notice.prv-proxy-note p{color:#C2CCD6;}
';
}
/**
* Core component tokens (.prv-*) unchanged from prior versions.
*
* @return string
*/
private static function get_component_css(): string {
return '
/* === PR Vision dashboard — "Assay" palette (dark-adapted) === */
.prv-bento{display:grid;grid-template-columns:1.25fr 1fr 1fr;gap:16px;margin:18px 0 22px;}
@media(max-width:820px){.prv-bento{grid-template-columns:1fr;}}
.prv-tile{background:#1C2228;border:1px solid #3A4651;border-radius:12px;padding:20px 22px;position:relative;min-height:140px;display:flex;flex-direction:column;}
.prv-tile-label{font-size:11.5px;font-weight:600;letter-spacing:.06em;text-transform:uppercase;color:#9AA7B2;margin-bottom:6px;}
.prv-tile-big{font-size:42px;font-weight:700;color:#EEF2F5;line-height:1;font-variant-numeric:tabular-nums;margin-top:auto;}
.prv-tile-big small{font-size:16px;font-weight:600;color:#9AA7B2;margin-left:3px;}
.prv-tile-sub{font-size:12.5px;color:#9AA7B2;margin-top:8px;}
.prv-tile-delta{font-size:12.5px;font-weight:600;margin-top:8px;}
.prv-delta--up{color:#9BE635;} .prv-delta--down{color:#FF6B5E;} .prv-delta--flat{color:#9AA7B2;}
.prv-health-pill{position:absolute;top:14px;right:14px;display:inline-flex;align-items:center;gap:5px;font-size:11.5px;font-weight:600;padding:3px 9px 3px 7px;border-radius:999px;}
.prv-health-dot{width:7px;height:7px;border-radius:50%;flex:0 0 7px;}
.prv-health--ok{background:rgba(155,230,53,.15);color:#9BE635;} .prv-health--ok .prv-health-dot{background:#9BE635;}
.prv-health--warn{background:rgba(255,157,77,.15);color:#FFB36E;} .prv-health--warn .prv-health-dot{background:#FF9D4D;}
.prv-health--neutral{background:rgba(136,147,160,.14);color:#B4BDC7;} .prv-health--neutral .prv-health-dot{background:#8893A0;}
.prv-cost-row{display:flex;align-items:baseline;gap:4px;margin-top:auto;}
.prv-cost-big{font-size:28px;font-weight:700;color:#EEF2F5;font-variant-numeric:tabular-nums;}
.prv-cost-cap{font-size:13px;color:#9AA7B2;}
.prv-meter{height:10px;border-radius:999px;background:#2A333C;margin-top:8px;border:1px solid #333D47;overflow:hidden;}
.prv-meter>span{display:block;height:100%;border-radius:999px;background:#34C0CA;transition:width .3s ease;}
.prv-meter--capped>span{background:#FF9D4D;}
.prv-trunc-badge{display:flex;align-items:flex-start;gap:7px;margin-top:10px;background:rgba(255,157,77,.15);border:1px solid rgba(255,157,77,.42);border-radius:6px;padding:8px 10px;font-size:12px;color:#EEF2F5;line-height:1.4;}
.prv-trunc-icon{color:#FFB36E;font-size:14px;flex:0 0 16px;margin-top:1px;}
.prv-trunc-badge b{color:#FFB36E;}
.prv-tile-ts{font-size:20px;font-weight:700;color:#EEF2F5;margin-top:auto;line-height:1.2;}
.prv-tile-meta{font-size:12.5px;color:#9AA7B2;margin-top:6px;}
.prv-card{background:#1C2228;border:1px solid #3A4651;border-radius:12px;margin:0 0 20px;}
.prv-card-head{display:flex;align-items:center;justify-content:space-between;padding:18px 22px 0;}
.prv-card-head h2{font-size:16px;font-weight:600;color:#EEF2F5;margin:0;}
.prv-card-body{padding:10px 22px 20px;}
.prv-chartbox{position:relative;height:300px;max-height:300px;padding:14px 8px 0;overflow:hidden;}
.prv-chartbox canvas{max-height:286px;}
.prv-chart-fallback{display:none;height:100%;align-items:center;justify-content:center;text-align:center;color:#9AA7B2;font-size:13px;padding:0 24px;}
.prv-chartbox.prv-noscript .prv-chart-fallback{display:flex;}
.prv-chartbox.prv-noscript canvas{display:none;}
.prv-chartcap{display:flex;align-items:center;gap:16px;flex-wrap:wrap;padding:8px 22px 4px;font-size:12.5px;color:#9AA7B2;}
.prv-chartcap-item{display:inline-flex;align-items:center;gap:6px;}
.prv-leg-solid{width:20px;height:0;border-top:3px solid #34C0CA;display:inline-block;}
.prv-leg-dash{width:20px;height:0;border-top:2px dashed #B6F25A;display:inline-block;}
.prv-cfg-note{display:flex;gap:10px;align-items:flex-start;margin:8px 22px 14px;background:rgba(255,157,77,.15);border:1px solid rgba(255,157,77,.42);border-radius:8px;padding:11px 14px;color:#EEF2F5;font-size:13px;line-height:1.45;}
.prv-cfg-note-icon{color:#FFB36E;font-style:normal;flex:0 0 16px;margin-top:1px;}
.prv-cfg-note b{color:#FFB36E;}
.prv-standings{margin:0;}
.prv-status{display:inline-flex;align-items:center;gap:4px;font-size:12.5px;font-weight:600;padding:2px 8px;border-radius:999px;}
.prv-status--cited{background:rgba(155,230,53,.15);color:#9BE635;}
.prv-status--not-yet{background:rgba(136,147,160,.14);color:#B4BDC7;}
.prv-proxy-note{margin:10px 0;}
';
}
/**
* V0.3.0 additions: subnav, costs, call-log, drawer components.
*
* @return string
*/
private static function get_v030_css(): string {
return '
/* === v0.3.0: subnav + costs + call log === */
.prv-subnav{display:flex;gap:2px;margin:0 0 18px;border-bottom:1px solid #3A4651;padding-bottom:0;}
.prv-subnav-tab{display:inline-block;padding:8px 16px;font-size:13px;font-weight:500;color:#9AA7B2;text-decoration:none;border-bottom:2px solid transparent;margin-bottom:-1px;transition:color .15s;}
.prv-subnav-tab:hover{color:#EEF2F5;}
.prv-subnav-tab--active{color:#34C0CA;border-bottom-color:#34C0CA;}
.prv-page-wrap{padding:0 20px 40px;background:#14181C;min-height:100vh;}
.prv-page-header{margin:18px 0 14px;}
.prv-page-title{font-size:20px;font-weight:700;color:#EEF2F5;margin:0 0 4px;}
.prv-page-sub{font-size:13px;color:#9AA7B2;margin:0;}
.prv-retention-note{font-size:12px;color:#9AA7B2;margin:4px 0 0;font-style:italic;}
.prv-cap-row{display:flex;align-items:flex-start;justify-content:space-between;margin-bottom:12px;}
.prv-cap-left{display:flex;align-items:baseline;gap:6px;}
.prv-cap-right{display:flex;flex-direction:column;align-items:flex-end;gap:4px;}
.prv-cap-projected{display:flex;flex-direction:column;align-items:flex-end;}
.prv-cap-proj-label{font-size:10px;text-transform:uppercase;letter-spacing:.05em;color:#9AA7B2;}
.prv-cap-proj-val{font-size:18px;font-weight:600;font-variant-numeric:tabular-nums;color:#EEF2F5;}
.prv-cap-days{font-size:12px;color:#9AA7B2;}
.prv-cap-reconcile{font-size:12px;color:#9AA7B2;margin-top:10px;}
.prv-tile-big--sm{font-size:28px;}
.prv-filter-bar{display:flex;flex-wrap:wrap;gap:8px;margin:0 0 14px;align-items:center;}
.prv-filter-select,.prv-filter-date{background:#1C2228;border:1px solid #3A4651;color:#EEF2F5;border-radius:6px;padding:5px 10px;font-size:13px;}
.prv-results-count{font-size:12.5px;color:#9AA7B2;margin:0 0 10px;}
.prv-callog-table th,.prv-callog-table td{white-space:nowrap;}
.prv-callog-table{min-width:900px;}
.prv-tabnum{font-variant-numeric:tabular-nums;}
.prv-muted{color:#9AA7B2;}
.prv-mono{font-family:monospace;font-size:12px;}
.prv-mono-sm{font-size:11px;}
.prv-chip{display:inline-flex;align-items:center;gap:4px;font-size:11.5px;font-weight:600;padding:2px 8px;border-radius:999px;}
.prv-chip--ok{background:rgba(52,192,202,.15);color:#34C0CA;}
.prv-chip--error{background:rgba(255,107,94,.15);color:#FF6B5E;}
.prv-call-row{cursor:pointer;}
.prv-call-row:hover{background:rgba(52,192,202,.07);}
.prv-call-row:focus{outline:2px solid #34C0CA;outline-offset:-2px;}
.prv-pagination{display:flex;align-items:center;gap:10px;margin-top:14px;flex-wrap:wrap;}
.prv-page-btn{display:inline-block;padding:5px 12px;border:1px solid #3A4651;border-radius:6px;font-size:12.5px;color:#EEF2F5;text-decoration:none;}
.prv-page-btn:hover{border-color:#34C0CA;color:#34C0CA;}
.prv-page-btn--disabled{color:#4A5563;border-color:#2A333C;background:#1C2228;cursor:not-allowed;}
.prv-page-info,.prv-page-per{font-size:12.5px;color:#9AA7B2;}
.prv-drawer{position:fixed;right:0;top:32px;height:calc(100vh - 32px);width:480px;background:#1C2228;border-left:1px solid #3A4651;z-index:9999;overflow-y:auto;transform:translateX(100%);transition:transform .16s ease;}
.prv-drawer:not([hidden]){transform:translateX(0);}
@media(prefers-reduced-motion:reduce){.prv-drawer{transition:none;}}
@media(max-width:600px){.prv-drawer{width:100%;}}
.prv-drawer-overlay{position:fixed;inset:0;background:rgba(0,0,0,.3);z-index:9998;opacity:0;transition:opacity .16s ease;}
.prv-drawer:not([hidden])~.prv-drawer-overlay{opacity:1;}
.prv-drawer-header{display:flex;align-items:center;gap:12px;padding:14px 18px;border-bottom:1px solid #3A4651;}
.prv-drawer-close{background:none;border:1px solid #3A4651;color:#9AA7B2;border-radius:6px;padding:4px 10px;cursor:pointer;font-size:12.5px;}
.prv-drawer-close:hover{color:#EEF2F5;border-color:#9AA7B2;}
.prv-drawer-title{font-size:13px;font-weight:600;color:#EEF2F5;}
.prv-drawer-body{padding:16px 18px;}
.prv-drawer-meta-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:4px;}
.prv-drawer-id{font-size:16px;font-weight:700;color:#EEF2F5;}
.prv-drawer-ts{font-size:12px;color:#9AA7B2;font-variant-numeric:tabular-nums;}
.prv-drawer-topline{font-size:13px;color:#C2CCD6;margin-bottom:10px;}
.prv-drawer-peptide{font-weight:600;color:#EEF2F5;}
.prv-drawer-dl{display:grid;grid-template-columns:auto 1fr;gap:4px 14px;font-size:12.5px;margin:12px 0 16px;}
.prv-drawer-dl dt{color:#9AA7B2;font-weight:500;}
.prv-drawer-dl dd{color:#EEF2F5;margin:0;}
.prv-drawer-section{margin-bottom:18px;}
.prv-drawer-section-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:6px;}
.prv-drawer-section-label{font-size:10.5px;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#9AA7B2;}
.prv-copy-btn{background:none;border:1px solid #3A4651;color:#9AA7B2;border-radius:4px;padding:2px 8px;cursor:pointer;font-size:11.5px;}
.prv-copy-btn:hover{color:#34C0CA;border-color:#34C0CA;}
.prv-code-block{background:#14181C;border:1px solid #2A333C;border-radius:6px;padding:12px;font-size:11.5px;line-height:1.55;color:#C2CCD6;overflow-y:auto;max-height:320px;white-space:pre-wrap;word-break:break-word;margin:0;}
.prv-drawer-notice{display:flex;gap:10px;align-items:flex-start;background:rgba(136,147,160,.1);border:1px solid #3A4651;border-radius:8px;padding:12px 14px;font-size:12.5px;color:#C2CCD6;line-height:1.45;}
.prv-notice-icon{font-size:16px;color:#9AA7B2;flex:0 0 18px;}
.prv-error-block{display:flex;gap:10px;align-items:flex-start;background:rgba(255,107,94,.12);border:1px solid rgba(255,107,94,.35);border-radius:8px;padding:12px 14px;font-size:12.5px;color:#EEF2F5;line-height:1.45;}
.prv-error-icon{font-size:16px;color:#FF6B5E;flex:0 0 18px;}
.prv-drawer-err{color:#FF8A80;font-size:13px;}
.prv-skel-line{background:#2A333C;border-radius:4px;height:12px;margin-bottom:8px;animation:prvPulse 1.2s ease-in-out infinite;}
.prv-skel-line--wide{width:80%;} .prv-skel-line--med{width:55%;} .prv-skel-line--sm{width:35%;}
@keyframes prvPulse{0%,100%{opacity:.5;}50%{opacity:1;}}
.prv-seg{display:flex;gap:2px;margin-bottom:14px;flex-wrap:wrap;}
.prv-seg-btn{display:inline-block;padding:5px 12px;border:1px solid #3A4651;border-radius:6px;font-size:12.5px;color:#9AA7B2;text-decoration:none;}
.prv-seg-btn:hover{color:#EEF2F5;border-color:#9AA7B2;}
.prv-seg-btn--active{background:#34C0CA;color:#14181C;border-color:#34C0CA;font-weight:600;}
.prv-table-wrap table{width:100%;}
.prv-empty-msg{color:#9AA7B2;font-size:13px;padding:14px 0;}
.prv-sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0;}
.prv-drawer-na{color:#9AA7B2;font-size:13px;font-style:italic;}
.prv-status--error{background:rgba(255,107,94,.15);color:#FF6B5E;}
.prv-field-hint{display:block;font-size:12px;color:#9AA7B2;margin-top:4px;}
';
}
}

View File

@@ -201,89 +201,13 @@ class PRV_Admin_Page {
} }
/** /**
* Return the dashboard CSS for the PR Vision admin page. * Return the combined CSS string for all PR Vision admin screens.
* *
* Covers two concerns: * Delegates to PRV_Admin_Page_Css to keep this class under 300 lines.
* 1. WP admin wrapper chrome (injected here; fires on BOTH PR Vision screens
* because the enqueue_assets guard matches 'pr-vision' in the hook suffix):
* #wpcontent / #wpbody-content are set to the dark page bg so no white
* gutter/strip shows around or above the dark UI.
* 2. Component tokens (.prv-*) — unchanged from prior versions.
*
* This inline style block is attached to wp-admin (always loaded on admin
* pages) via wp_add_inline_style() and therefore only present on the two
* PR Vision screens. It must NOT be registered globally.
* *
* @return string CSS string. * @return string CSS string.
*/ */
private function get_dashboard_css(): string { private function get_dashboard_css(): string {
return ' return PRV_Admin_Page_Css::get();
/* === PR Vision — WP admin chrome: kill white wrappers (scoped via conditional enqueue) === */
#wpcontent{background:#14181C;padding-left:0;}
#wpbody-content{background:#14181C;min-height:calc(100vh - 32px);padding-bottom:40px;}
#wpbody{background:#14181C;}
.wrap>h1,.prv-settings-wrap~*>h1,.wp-heading-inline{color:#EEF2F5;}
.notice.prv-proxy-note{background:#1C2228;border-left-color:#34C0CA;color:#EEF2F5;}
.notice.prv-proxy-note p{color:#C2CCD6;}
/* === PR Vision dashboard — "Assay" palette (dark-adapted) === */
.prv-bento{display:grid;grid-template-columns:1.25fr 1fr 1fr;gap:16px;margin:18px 0 22px;}
@media(max-width:820px){.prv-bento{grid-template-columns:1fr;}}
.prv-tile{background:#1C2228;border:1px solid #3A4651;border-radius:12px;padding:20px 22px;position:relative;min-height:140px;display:flex;flex-direction:column;}
.prv-tile-label{font-size:11.5px;font-weight:600;letter-spacing:.06em;text-transform:uppercase;color:#9AA7B2;margin-bottom:6px;}
.prv-tile-big{font-size:42px;font-weight:700;color:#EEF2F5;line-height:1;font-variant-numeric:tabular-nums;margin-top:auto;}
.prv-tile-big small{font-size:16px;font-weight:600;color:#9AA7B2;margin-left:3px;}
.prv-tile-sub{font-size:12.5px;color:#9AA7B2;margin-top:8px;}
.prv-tile-delta{font-size:12.5px;font-weight:600;margin-top:8px;}
.prv-delta--up{color:#9BE635;} .prv-delta--down{color:#FF6B5E;} .prv-delta--flat{color:#9AA7B2;}
/* health pill */
.prv-health-pill{position:absolute;top:14px;right:14px;display:inline-flex;align-items:center;gap:5px;font-size:11.5px;font-weight:600;padding:3px 9px 3px 7px;border-radius:999px;}
.prv-health-dot{width:7px;height:7px;border-radius:50%;flex:0 0 7px;}
.prv-health--ok{background:rgba(155,230,53,.15);color:#9BE635;} .prv-health--ok .prv-health-dot{background:#9BE635;}
.prv-health--warn{background:rgba(255,157,77,.15);color:#FFB36E;} .prv-health--warn .prv-health-dot{background:#FF9D4D;}
.prv-health--neutral{background:rgba(136,147,160,.14);color:#B4BDC7;} .prv-health--neutral .prv-health-dot{background:#8893A0;}
/* cost tile */
.prv-cost-row{display:flex;align-items:baseline;gap:4px;margin-top:auto;}
.prv-cost-big{font-size:28px;font-weight:700;color:#EEF2F5;font-variant-numeric:tabular-nums;}
.prv-cost-cap{font-size:13px;color:#9AA7B2;}
.prv-meter{height:10px;border-radius:999px;background:#2A333C;margin-top:8px;border:1px solid #333D47;overflow:hidden;}
.prv-meter>span{display:block;height:100%;border-radius:999px;background:#34C0CA;transition:width .3s ease;}
.prv-meter--capped>span{background:#FF9D4D;}
.prv-trunc-badge{display:flex;align-items:flex-start;gap:7px;margin-top:10px;background:rgba(255,157,77,.15);border:1px solid rgba(255,157,77,.42);border-radius:6px;padding:8px 10px;font-size:12px;color:#EEF2F5;line-height:1.4;}
.prv-trunc-icon{color:#FFB36E;font-size:14px;flex:0 0 16px;margin-top:1px;}
.prv-trunc-badge b{color:#FFB36E;}
/* last run tile */
.prv-tile-ts{font-size:20px;font-weight:700;color:#EEF2F5;margin-top:auto;line-height:1.2;}
.prv-tile-meta{font-size:12.5px;color:#9AA7B2;margin-top:6px;}
/* cards */
.prv-card{background:#1C2228;border:1px solid #3A4651;border-radius:12px;margin:0 0 20px;}
.prv-card-head{display:flex;align-items:center;justify-content:space-between;padding:18px 22px 0;}
.prv-card-head h2{font-size:16px;font-weight:600;color:#EEF2F5;margin:0;}
.prv-card-body{padding:10px 22px 20px;}
/* chart */
.prv-chartbox{position:relative;height:300px;max-height:300px;padding:14px 8px 0;overflow:hidden;}
.prv-chartbox canvas{max-height:286px;}
.prv-chart-fallback{display:none;height:100%;align-items:center;justify-content:center;text-align:center;color:#9AA7B2;font-size:13px;padding:0 24px;}
.prv-chartbox.prv-noscript .prv-chart-fallback{display:flex;}
.prv-chartbox.prv-noscript canvas{display:none;}
/* chart legend */
.prv-chartcap{display:flex;align-items:center;gap:16px;flex-wrap:wrap;padding:8px 22px 4px;font-size:12.5px;color:#9AA7B2;}
.prv-chartcap-item{display:inline-flex;align-items:center;gap:6px;}
.prv-leg-solid{width:20px;height:0;border-top:3px solid #34C0CA;display:inline-block;}
.prv-leg-dash{width:20px;height:0;border-top:2px dashed #B6F25A;display:inline-block;}
.prv-leg-cfg{display:inline-flex;align-items:center;gap:3px;}
.prv-leg-cfg::before{content:"";width:0;height:14px;border-left:2px dashed #FFB36E;display:inline-block;}
.prv-leg-cfg::after{content:"";width:8px;height:8px;border-radius:50%;background:#FF9D4D;display:inline-block;}
/* config change note */
.prv-cfg-note{display:flex;gap:10px;align-items:flex-start;margin:8px 22px 14px;background:rgba(255,157,77,.15);border:1px solid rgba(255,157,77,.42);border-radius:8px;padding:11px 14px;color:#EEF2F5;font-size:13px;line-height:1.45;}
.prv-cfg-note-icon{color:#FFB36E;font-style:normal;flex:0 0 16px;margin-top:1px;}
.prv-cfg-note b{color:#FFB36E;}
/* standings table */
.prv-standings{margin:0;}
.prv-status{display:inline-flex;align-items:center;gap:4px;font-size:12.5px;font-weight:600;padding:2px 8px;border-radius:999px;}
.prv-status--cited{background:rgba(155,230,53,.15);color:#9BE635;}
.prv-status--not-yet{background:rgba(136,147,160,.14);color:#B4BDC7;}
/* proxy note */
.prv-proxy-note{margin:10px 0;}
';
} }
} }

View File

@@ -0,0 +1,95 @@
<?php
/**
* AJAX handler for the call-detail drawer content (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Handles the prv_call_detail AJAX action for the drawer content.
*
* Fetches metadata + I/O for a single call ID and returns the rendered
* drawer HTML as JSON. Enforces nonce + capability before any DB read.
*
* Security: delegates rendering to PRV_Call_Drawer_Renderer which only
* outputs allowlisted fields — never headers, keys, or raw HTTP bodies.
*
* Who triggers: wp_ajax_prv_call_detail hook (logged-in users only).
* Dependencies: PRV_Call_Log_Query, PRV_Call_Drawer_Renderer.
*
* @see class-prv-call-drawer-renderer.php — HTML renderer for the drawer.
* @see class-prv-call-log-query.php — Fetches meta + io rows.
* @see class-prv-plugin.php — Registers the AJAX hook.
* @package PrVision
*/
class PRV_Call_Detail_Ajax {
/**
* Nonce action for the call-detail drawer.
*/
const NONCE_ACTION = 'prv_call_drawer';
/**
* Register the AJAX hook.
*
* Side effects: Adds wp_ajax action.
*
* @return void
*/
public function register_hooks(): void {
add_action( 'wp_ajax_prv_call_detail', array( $this, 'handle' ) );
}
/**
* Handle the AJAX request and return drawer HTML.
*
* Side effects: Outputs JSON; terminates request.
*
* @return void
*/
public function handle(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( esc_html__( 'Insufficient permissions.', 'pr-vision' ) );
}
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, self::NONCE_ACTION ) ) {
wp_send_json_error( esc_html__( 'Security check failed.', 'pr-vision' ) );
}
$call_id = isset( $_POST['call_id'] ) ? absint( wp_unslash( $_POST['call_id'] ) ) : 0;
if ( $call_id < 1 ) {
wp_send_json_error( esc_html__( 'Invalid call ID.', 'pr-vision' ) );
}
$query = new PRV_Call_Log_Query();
// Fetch from prv_call_meta.
global $wpdb;
$table = PRV_Call_Meta_Table::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$meta = $wpdb->get_row(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT * FROM {$table} WHERE id = %d LIMIT 1",
$call_id
),
ARRAY_A
);
if ( null === $meta || ! is_array( $meta ) ) {
wp_send_json_error( esc_html__( 'Call not found.', 'pr-vision' ) );
}
$io = $query->get_call_io( $call_id );
ob_start();
$renderer = new PRV_Call_Drawer_Renderer();
$renderer->render_content( $meta, $io );
$html = ob_get_clean();
wp_send_json_success( array( 'html' => $html ) );
}
}

View File

@@ -0,0 +1,268 @@
<?php
/**
* Renders the per-call detail drawer (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Renders the per-call detail drawer — a non-modal complementary panel.
*
* Security contract (P0):
* The drawer only renders: prompt_text, response_text, and call metadata.
* There is NO section for headers, Authorization, raw HTTP request, or any
* field not in the allowlist. This is a structural guarantee — the data
* never reaches this renderer.
*
* Drawer is role="complementary" (non-modal). Focus goes to close button on
* open; Escape closes + returns focus to the triggering row. No tab-trap.
* All states: Normal, Pruned, Legacy/not-captured, Failed, Loading, Empty.
*
* Who triggers: PRV_Call_Log_Table_Renderer::render_container(); JS opens
* it via fetch on row click.
* Dependencies: None (pure HTML/JS renderer).
*
* @see class-prv-call-log-table-renderer.php — Emits the container in page.
* @see class-prv-call-log-query.php — Supplies I/O + metadata data.
* @package PrVision
*/
class PRV_Call_Drawer_Renderer {
/**
* Emit the drawer container and inline JS for open/close/fetch.
*
* This is the static shell; content is populated via AJAX/JS.
* Side effects: Outputs HTML + inline script.
*
* @return void
*/
public function render_container(): void {
echo '<div id="prv-drawer" class="prv-drawer" role="complementary" aria-label="' . esc_attr__( 'Call details', 'pr-vision' ) . '" hidden>';
echo '<div class="prv-drawer-header">';
echo '<button id="prv-drawer-close" class="prv-drawer-close" aria-label="' . esc_attr__( 'Close call details', 'pr-vision' ) . '">&#x2715; ' . esc_html__( 'Close', 'pr-vision' ) . '</button>';
echo '<span id="prv-drawer-title" class="prv-drawer-title"></span>';
echo '</div>';
echo '<div id="prv-drawer-body" class="prv-drawer-body" aria-live="polite" aria-busy="false">';
echo '<div class="prv-drawer-skeleton" aria-hidden="true">';
echo '<div class="prv-skel-line prv-skel-line--wide"></div>';
echo '<div class="prv-skel-line prv-skel-line--med"></div>';
echo '<div class="prv-skel-line prv-skel-line--sm"></div>';
echo '</div>';
echo '</div>';
echo '</div>';
// Overlay — pointer-events:none so table stays interactive.
echo '<div id="prv-drawer-overlay" class="prv-drawer-overlay" aria-hidden="true" style="pointer-events:none"></div>';
$this->render_inline_script();
}
/**
* Render a populated drawer for a specific call (server-side variant).
*
* Used when building drawer HTML server-side (e.g., for AJAX endpoint).
* Security: only prompt_text and response_text shown — never headers.
*
* Side effects: Outputs HTML.
*
* @param array<string, mixed> $meta Call metadata row.
* @param array<string, string>|null $io I/O record or null (pruned/missing).
*
* @return void
*/
public function render_content( array $meta, ?array $io ): void {
$id = (int) $meta['id'];
$captured = (string) $meta['captured_at'];
$peptide = (string) $meta['peptide_slug'];
$model = (string) $meta['model'];
$intent = (string) $meta['intent_label'];
$http_status = (int) ( $meta['http_status'] ?? 200 );
$is_error = $http_status >= 400;
$cited = isset( $meta['cited'] ) ? $meta['cited'] : null;
$tok_in = isset( $meta['tokens_in'] ) ? (int) $meta['tokens_in'] : null;
$tok_out = isset( $meta['tokens_out'] ) ? (int) $meta['tokens_out'] : null;
$latency_ms = isset( $meta['latency_ms'] ) ? (int) $meta['latency_ms'] : null;
$cost_usd = (float) ( $meta['cost_usd'] ?? 0.0 );
$run_id = (string) ( $meta['run_id'] ?? '' );
$config_ver = isset( $meta['config_version'] ) ? (int) $meta['config_version'] : null;
$io_captured = isset( $meta['io_captured'] ) ? (int) $meta['io_captured'] : 0;
// Determine drawer state using the explicit io_captured flag.
// io_captured = 1 AND io row present → Normal.
// io_captured = 1 AND io row absent → Pruned (aged out by retention cron).
// io_captured = 0 → Legacy / not captured (pre-feature row).
$has_io = ( null !== $io );
$vis_row_null = empty( $meta['visibility_row'] );
if ( ! $is_error ) {
$is_legacy = ( 0 === $io_captured );
$is_pruned = ( 1 === $io_captured && ! $has_io );
} else {
$is_legacy = false;
$is_pruned = false;
}
// Header.
echo '<div class="prv-drawer-meta-head">';
echo '<span class="prv-drawer-id">' . esc_html( sprintf( /* translators: %d: call ID */ __( 'Call #%d', 'pr-vision' ), $id ) ) . '</span>';
echo '<span class="prv-drawer-ts">' . esc_html( $captured ) . ' UTC</span>';
echo '</div>';
echo '<div class="prv-drawer-topline">';
echo '<span class="prv-drawer-peptide">' . esc_html( $peptide ) . '</span>';
if ( '' !== $intent ) {
echo ' &middot; <span class="prv-drawer-intent">' . esc_html( $intent ) . '</span>';
}
echo '</div>';
// Verdict chip.
if ( $is_error ) {
echo '<span class="prv-status prv-status--error">&#x2715; Error</span>';
} elseif ( null !== $cited ) {
if ( (bool) $cited ) {
echo '<span class="prv-status prv-status--cited">&#x2713; Cited</span>';
} else {
echo '<span class="prv-status prv-status--not-yet">&#x2013; Not yet</span>';
}
}
// Metadata section.
echo '<dl class="prv-drawer-dl">';
echo '<dt>' . esc_html__( 'Model', 'pr-vision' ) . '</dt><dd><code>' . esc_html( $model ) . '</code></dd>';
$tok_str = ( null !== $tok_in && null !== $tok_out )
? sprintf( '↑ %s in / ↓ %s out', number_format( $tok_in ), number_format( $tok_out ) )
: '—';
echo '<dt>' . esc_html__( 'Tokens', 'pr-vision' ) . '</dt><dd>' . esc_html( $tok_str ) . '</dd>';
echo '<dt>' . esc_html__( 'Latency', 'pr-vision' ) . '</dt><dd>';
echo null !== $latency_ms ? esc_html( number_format( $latency_ms ) ) . 'ms' : '—';
echo '</dd>';
echo '<dt>' . esc_html__( 'Cost', 'pr-vision' ) . '</dt><dd>$' . esc_html( number_format( $cost_usd, 6 ) ) . '</dd>';
if ( '' !== $run_id ) {
echo '<dt>' . esc_html__( 'Run ID', 'pr-vision' ) . '</dt><dd><code class="prv-mono-sm">' . esc_html( substr( $run_id, 0, 8 ) ) . '…</code></dd>';
}
if ( null !== $config_ver ) {
echo '<dt>' . esc_html__( 'Config', 'pr-vision' ) . '</dt><dd>v' . esc_html( (string) $config_ver ) . '</dd>';
}
if ( $is_error ) {
echo '<dt>' . esc_html__( 'HTTP Status', 'pr-vision' ) . '</dt><dd>' . esc_html( (string) $http_status ) . '</dd>';
}
echo '</dl>';
// Prompt section.
echo '<div class="prv-drawer-section">';
echo '<div class="prv-drawer-section-head">';
echo '<span class="prv-drawer-section-label">' . esc_html__( 'PROMPT SENT', 'pr-vision' ) . '</span>';
if ( $has_io && isset( $io['prompt_text'] ) && '' !== $io['prompt_text'] ) {
echo '<button class="prv-copy-btn" data-copy-target="prv-prompt-text" aria-label="' . esc_attr__( 'Copy prompt text to clipboard', 'pr-vision' ) . '">' . esc_html__( 'Copy prompt text', 'pr-vision' ) . '</button>';
}
echo '</div>';
if ( $is_legacy ) {
$this->render_not_captured_notice( $captured );
} elseif ( $is_pruned ) {
$this->render_pruned_notice( $captured );
} elseif ( $has_io && isset( $io['prompt_text'] ) ) {
echo '<pre id="prv-prompt-text" class="prv-code-block">' . esc_html( $io['prompt_text'] ) . '</pre>';
} else {
echo '<p class="prv-drawer-na">—</p>';
}
echo '</div>';
// Response section.
echo '<div class="prv-drawer-section">';
echo '<div class="prv-drawer-section-head">';
echo '<span class="prv-drawer-section-label">' . esc_html__( 'RAW RESPONSE', 'pr-vision' ) . '</span>';
if ( $has_io && isset( $io['response_text'] ) && '' !== $io['response_text'] ) {
echo '<button class="prv-copy-btn" data-copy-target="prv-response-text" aria-label="' . esc_attr__( 'Copy response text to clipboard', 'pr-vision' ) . '">' . esc_html__( 'Copy response text', 'pr-vision' ) . '</button>';
}
echo '</div>';
if ( $is_error ) {
echo '<div class="prv-error-block" role="alert">';
echo '<span class="prv-error-icon" aria-hidden="true">&#x2715;</span>';
echo '<div>';
echo '<strong>' . esc_html__( 'Call failed — no response captured', 'pr-vision' ) . '</strong><br>';
echo esc_html(
sprintf(
/* translators: %d: HTTP status code */
__( 'Provider returned HTTP %d. The probe was not counted against the budget.', 'pr-vision' ),
$http_status
)
);
echo '</div></div>';
} elseif ( ! $is_legacy && ! $is_pruned && $has_io && isset( $io['response_text'] ) && '' !== $io['response_text'] ) {
echo '<pre id="prv-response-text" class="prv-code-block">' . esc_html( $io['response_text'] ) . '</pre>';
} elseif ( ! $is_error && ! $is_legacy && ! $is_pruned ) {
echo '<p class="prv-drawer-na">—</p>';
}
echo '</div>';
}
/**
* Render the "pruned" I/O notice (aged out, not an error).
*
* Side effects: Outputs HTML.
*
* @param string $captured_at Timestamp when the call was captured.
*
* @return void
*/
private function render_pruned_notice( string $captured_at ): void {
echo '<div class="prv-drawer-notice prv-drawer-notice--muted">';
echo '<span class="prv-notice-icon" aria-hidden="true">&#x231B;</span>';
echo '<div>';
echo '<strong>' . esc_html__( 'Prompt and response pruned', 'pr-vision' ) . '</strong><br>';
echo esc_html(
sprintf(
/* translators: 1: retention days, 2: captured date */
__( 'Raw I/O is kept for %1$d days, then auto-deleted. This call was recorded on %2$s UTC.', 'pr-vision' ),
PRV_Config::get_io_retention_days(),
$captured_at
)
);
echo '</div></div>';
}
/**
* Render the "not captured" notice (pre-v0.3 row, never stored).
*
* Distinct from the pruned state — different copy prevents confusion.
* Side effects: Outputs HTML.
*
* @param string $captured_at Timestamp when the call was captured.
*
* @return void
*/
private function render_not_captured_notice( string $captured_at ): void {
echo '<div class="prv-drawer-notice prv-drawer-notice--muted">';
echo '<span class="prv-notice-icon" aria-hidden="true">&#x2139;</span>';
echo '<div>';
echo '<strong>' . esc_html__( 'Not captured for this call', 'pr-vision' ) . '</strong><br>';
echo esc_html__( 'This call was recorded before the call-inspector feature was added. Prompt and response were never stored — this is not a retention prune.', 'pr-vision' );
echo '</div></div>';
}
/**
* Emit the inline JavaScript for drawer open/close/copy behaviour.
*
* Security: data is fetched from a server endpoint that enforces the
* capture allowlist — no keys or headers can appear in drawer content.
* Side effects: Outputs script tag.
*
* @return void
*/
private function render_inline_script(): void {
PRV_Call_Drawer_Script::render(
wp_create_nonce( PRV_Call_Detail_Ajax::NONCE_ACTION ),
admin_url( 'admin-ajax.php' )
);
}
}

View File

@@ -0,0 +1,114 @@
<?php
/**
* Inline JS for the PR Vision call-detail drawer (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Emits the inline JavaScript for the call-detail drawer.
*
* Split from PRV_Call_Drawer_Renderer to keep each class under 300 lines.
* Handles: row-click open, AJAX fetch, close (button + Esc), copy buttons,
* and focus-management (return focus to triggering row on close).
*
* Security: data comes via AJAX from PRV_Call_Detail_Ajax which enforces
* the capture allowlist — no keys or headers can appear in drawer content.
*
* Who triggers: PRV_Call_Drawer_Renderer::render_container().
* Dependencies: PRV_Call_Detail_Ajax (server side); wp_create_nonce().
*
* @see class-prv-call-drawer-renderer.php — Calls render_script().
* @see class-prv-call-detail-ajax.php — Server AJAX handler.
* @package PrVision
*/
class PRV_Call_Drawer_Script {
/**
* Emit the inline script tag.
*
* Side effects: Outputs script HTML.
*
* @param string $nonce Ajax nonce for prv_call_drawer action.
* @param string $ajax Admin AJAX URL.
*
* @return void
*/
public static function render( string $nonce, string $ajax ): void {
// Escape values for embedding in JS string literals.
$nonce_js = esc_js( $nonce );
$ajax_js = esc_url( $ajax );
echo '<script>';
echo '(function(){';
echo 'var drawer=document.getElementById("prv-drawer");';
echo 'var body=document.getElementById("prv-drawer-body");';
echo 'var closeBtn=document.getElementById("prv-drawer-close");';
echo 'var lastFocus=null;';
echo 'function openDrawer(callId,triggerEl){';
echo ' lastFocus=triggerEl||null;';
echo ' drawer.removeAttribute("hidden");';
echo ' body.setAttribute("aria-busy","true");';
echo ' var skel=document.querySelector(".prv-drawer-skeleton");';
echo ' if(skel){body.innerHTML=skel.outerHTML;}';
echo ' closeBtn.focus();';
echo ' loadCall(callId);';
echo '}';
echo 'function closeDrawer(){';
echo ' drawer.setAttribute("hidden","");';
echo ' body.setAttribute("aria-busy","false");';
echo ' if(lastFocus&&lastFocus.focus){lastFocus.focus();}';
echo '}';
echo 'function loadCall(callId){';
echo ' var data=new URLSearchParams();';
echo ' data.append("action","prv_call_detail");';
echo ' data.append("nonce","' . $nonce_js . '");'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_js applied above
echo ' data.append("call_id",callId);';
echo ' fetch("' . $ajax_js . '",{method:"POST",credentials:"same-origin",body:data})'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_url applied above
echo ' .then(function(r){return r.json();})';
echo ' .then(function(json){';
echo ' if(json.success){';
echo ' body.innerHTML=json.data.html;';
echo ' body.setAttribute("aria-busy","false");';
echo ' attachCopyButtons();';
echo ' }else{';
echo ' body.innerHTML="<p class=\'prv-drawer-err\'>"+json.data+"</p>";';
echo ' body.setAttribute("aria-busy","false");';
echo ' }';
echo ' })';
echo ' .catch(function(){';
echo ' body.innerHTML="<p class=\'prv-drawer-err\'>Error loading call details.</p>";';
echo ' body.setAttribute("aria-busy","false");';
echo ' });';
echo '}';
echo 'function attachCopyButtons(){';
echo ' document.querySelectorAll(".prv-copy-btn").forEach(function(btn){';
echo ' btn.addEventListener("click",function(){';
echo ' var target=document.getElementById(btn.dataset.copyTarget);';
echo ' if(target){';
echo ' var orig=btn.textContent;';
echo ' navigator.clipboard.writeText(target.textContent||"").then(function(){';
echo ' btn.textContent="Copied!";';
echo ' setTimeout(function(){btn.textContent=orig;},1500);';
echo ' });';
echo ' }';
echo ' });';
echo ' });';
echo '}';
echo 'closeBtn.addEventListener("click",closeDrawer);';
echo 'document.addEventListener("keydown",function(e){';
echo ' if("Escape"===e.key&&!drawer.hasAttribute("hidden")){closeDrawer();}';
echo '});';
echo 'document.querySelectorAll(".prv-call-row").forEach(function(row){';
echo ' function handleOpen(){openDrawer(row.dataset.callId,row);}';
echo ' row.addEventListener("click",handleOpen);';
echo ' row.addEventListener("keydown",function(e){';
echo ' if("Enter"===e.key||" "===e.key){e.preventDefault();handleOpen();}';
echo ' });';
echo '});';
echo '})();';
echo '</script>';
}
}

View File

@@ -0,0 +1,88 @@
<?php
/**
* Database schema for the per-call I/O table (v0.3.0) — prunable.
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Manages the prv_call_io table: rendered prompt + raw response, PRUNED.
*
* Only the body-level content is stored here — never Authorization headers,
* api keys, or raw HTTP request objects. The prune cron deletes rows older
* than the retention window; the companion prv_call_meta table is kept.
*
* Who triggers: PRV_Upgrader::run() on every plugins_loaded (idempotent).
* Dependencies: $wpdb, dbDelta(), PRV_Call_Meta_Table (FK source).
*
* @see class-prv-call-meta-table.php — FK source: prv_call_meta.id.
* @see class-prv-capture-writer.php — Writes rows to this table.
* @see class-prv-prune-cron.php — Deletes aged rows from this table only.
* @see ARCHITECTURE.md — §Storage v0.3.0.
* @package PrVision
*/
class PRV_Call_Io_Table {
/**
* Table base name (without $wpdb->prefix).
*/
const TABLE_BASE = 'prv_call_io';
/**
* Create or upgrade the call-io table via dbDelta.
*
* Safe to call on every plugins_loaded — dbDelta is idempotent.
* Side effects: Database write.
*
* @return void
*/
public static function create_table(): void {
global $wpdb;
$table = $wpdb->prefix . self::TABLE_BASE;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
call_id BIGINT(20) UNSIGNED NOT NULL,
prompt_text LONGTEXT NOT NULL DEFAULT '',
response_text LONGTEXT NOT NULL DEFAULT '',
captured_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY call_id (call_id),
KEY captured_at (captured_at)
) {$charset_collate};";
if ( ! function_exists( 'dbDelta' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
dbDelta( $sql );
}
/**
* 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 I/O records.
*
* @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}" );
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* PR Vision Call Log admin sub-page (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Call Log sub-page: paginated call list + per-call detail drawer.
*
* Registers "PR Vision > Call Log" sub-menu. Reads filter/page params from
* the URL, queries PRV_Call_Log_Query, and delegates rendering to
* PRV_Call_Log_Table_Renderer and PRV_Call_Drawer_Renderer.
*
* Who triggers: PRV_Plugin::init() — is_admin() guard.
* Dependencies: PRV_Call_Log_Query, PRV_Call_Log_Table_Renderer,
* PRV_Call_Drawer_Renderer, PRV_Config.
*
* @see class-prv-call-log-table-renderer.php — Table + drawer rendering.
* @see class-prv-call-drawer-renderer.php — Detail drawer rendering.
* @see class-prv-call-log-query.php — Query layer.
* @package PrVision
*/
class PRV_Call_Log_Page {
/**
* Admin menu slug.
*/
const MENU_SLUG = 'pr-vision-calls';
/**
* Register WordPress admin hooks.
*
* Side effects: Adds admin_menu action.
*
* @return void
*/
public function register_hooks(): void {
add_action( 'admin_menu', array( $this, 'add_submenu' ) );
}
/**
* Register the Call Log sub-menu under PR Vision.
*
* Side effects: Adds a WP admin sub-menu entry.
*
* @return void
*/
public function add_submenu(): void {
add_submenu_page(
PRV_Admin_Page::MENU_SLUG,
__( 'PR Vision — Call Log', 'pr-vision' ),
__( 'Call Log', 'pr-vision' ),
'manage_options',
self::MENU_SLUG,
array( $this, 'render_page' )
);
}
/**
* Render the full Call Log page.
*
* Side effects: Outputs HTML.
*
* @return void
*/
public function render_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Insufficient permissions.', 'pr-vision' ) );
}
// Read filter params (no nonce: read-only display only).
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$filters = array(
'model' => isset( $_GET['prv_filter_model'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_filter_model'] ) ) : '',
'peptide' => isset( $_GET['prv_filter_peptide'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_filter_peptide'] ) ) : '',
'date_from' => isset( $_GET['prv_filter_from'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_filter_from'] ) ) : '',
'date_to' => isset( $_GET['prv_filter_to'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_filter_to'] ) ) : '',
'status' => isset( $_GET['prv_filter_status'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_filter_status'] ) ) : '',
);
$page = isset( $_GET['prv_page'] ) ? max( 1, absint( wp_unslash( $_GET['prv_page'] ) ) ) : 1;
// phpcs:enable WordPress.Security.NonceVerification.Recommended
$query = new PRV_Call_Log_Query();
$result = $query->get_page( $filters, $page );
$renderer = new PRV_Call_Log_Table_Renderer();
$renderer->render(
array(
'rows' => $result['rows'],
'total' => $result['total'],
'pages' => $result['pages'],
'page' => $page,
'filters' => $filters,
)
);
}
}

View File

@@ -0,0 +1,181 @@
<?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:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$total = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$table} {$where_sql}",
...$where_args
)
);
// phpcs:enable
$pages = $total > 0 ? (int) ceil( $total / self::PAGE_SIZE ) : 1;
// Data query.
$data_args = $where_args;
$data_args[] = self::PAGE_SIZE;
$data_args[] = $offset;
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$rows = $wpdb->get_results(
$wpdb->prepare(
"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",
...$data_args
),
ARRAY_A
);
// phpcs:enable
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 );
}
}

View File

@@ -0,0 +1,276 @@
<?php
/**
* Renders the Call Log table and filter bar (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Renders the paginated Call Log table, filter bar, and result count.
*
* Does not render the detail drawer — that is PRV_Call_Drawer_Renderer.
* All output is escaped at render. Cited% 0 renders muted; >0 renders lime.
* Status/verdict are never signalled by colour alone (chip + label).
*
* Who triggers: PRV_Call_Log_Page::render_page().
* Dependencies: PRV_Costs_Renderer (for subnav helper), PRV_Config.
*
* @see class-prv-call-log-page.php — Caller, passes data array.
* @see class-prv-call-drawer-renderer.php — Drawer renderer (emitted below table).
* @see class-prv-costs-renderer.php — render_subnav() shared helper.
* @package PrVision
*/
class PRV_Call_Log_Table_Renderer {
/**
* Render the full Call Log page including table and drawer.
*
* Side effects: Outputs HTML.
*
* @param array<string, mixed> $data Keys: rows, total, pages, page, filters.
*
* @return void
*/
public function render( array $data ): void {
$rows = (array) $data['rows'];
$total = (int) $data['total'];
$pages = (int) $data['pages'];
$page = (int) $data['page'];
$filters = (array) $data['filters'];
$subnav = new PRV_Costs_Renderer();
echo '<div class="wrap prv-page-wrap">';
$subnav->render_subnav( 'calls' );
echo '<div class="prv-page-header">';
echo '<h1 class="prv-page-title">' . esc_html__( 'Call Log', 'pr-vision' ) . '</h1>';
$retention_days = PRV_Config::get_io_retention_days();
echo '<p class="prv-retention-note">';
echo esc_html(
sprintf(
/* translators: %d: retention days */
__( 'Prompt + response kept %d days. Cost + metadata kept indefinitely.', 'pr-vision' ),
$retention_days
)
);
echo '</p>';
echo '</div>';
$this->render_filter_bar( $filters );
echo '<div class="prv-results-count" aria-live="polite">';
if ( $total > 0 ) {
$from = ( ( $page - 1 ) * PRV_Call_Log_Query::PAGE_SIZE ) + 1;
$to = min( $page * PRV_Call_Log_Query::PAGE_SIZE, $total );
echo esc_html(
sprintf(
/* translators: 1: from, 2: to, 3: total */
__( 'Showing %1$d–%2$d of %3$d calls', 'pr-vision' ),
$from,
$to,
$total
)
);
} else {
echo esc_html__( 'No calls recorded yet.', 'pr-vision' );
}
echo '</div>';
$this->render_table( $rows );
$this->render_pagination( $page, $pages, $filters );
// Emit drawer (hidden until a row is clicked).
$drawer = new PRV_Call_Drawer_Renderer();
$drawer->render_container();
echo '</div>'; // .wrap
}
/**
* Render the filter bar with 4 select controls and Clear button.
*
* Side effects: Outputs HTML.
*
* @param array<string, string> $filters Active filter values.
*
* @return void
*/
private function render_filter_bar( array $filters ): void {
$base_url = admin_url( 'admin.php?page=pr-vision-calls' );
echo '<form method="get" action="' . esc_url( admin_url( 'admin.php' ) ) . '" class="prv-filter-bar">';
echo '<input type="hidden" name="page" value="pr-vision-calls">';
// Model filter.
echo '<label for="prv-f-model" class="prv-sr-only">' . esc_html__( 'Model', 'pr-vision' ) . '</label>';
echo '<select id="prv-f-model" name="prv_filter_model" class="prv-filter-select">';
echo '<option value="">' . esc_html__( 'All models', 'pr-vision' ) . '</option>';
foreach ( PRV_Config::get_models() as $model_slug ) {
$sel = ( $filters['model'] === $model_slug ) ? ' selected' : '';
echo '<option value="' . esc_attr( $model_slug ) . '"' . $sel . '>' . esc_html( $model_slug ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $sel is safe
}
echo '</select>';
// Peptide filter.
echo '<label for="prv-f-peptide" class="prv-sr-only">' . esc_html__( 'Peptide', 'pr-vision' ) . '</label>';
echo '<select id="prv-f-peptide" name="prv_filter_peptide" class="prv-filter-select">';
echo '<option value="">' . esc_html__( 'All peptides', 'pr-vision' ) . '</option>';
foreach ( PRV_Config::get_peptides() as $p ) {
$slug = (string) $p['slug'];
$sel = ( $filters['peptide'] === $slug ) ? ' selected' : '';
echo '<option value="' . esc_attr( $slug ) . '"' . $sel . '>' . esc_html( $p['label'] ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $sel is safe
}
echo '</select>';
// Date range.
echo '<label for="prv-f-from" class="prv-sr-only">' . esc_html__( 'From date', 'pr-vision' ) . '</label>';
echo '<input type="date" id="prv-f-from" name="prv_filter_from" class="prv-filter-date" value="' . esc_attr( $filters['date_from'] ) . '">';
echo '<label for="prv-f-to" class="prv-sr-only">' . esc_html__( 'To date', 'pr-vision' ) . '</label>';
echo '<input type="date" id="prv-f-to" name="prv_filter_to" class="prv-filter-date" value="' . esc_attr( $filters['date_to'] ) . '">';
// Status filter.
$status_opts = array(
'' => __( 'All statuses', 'pr-vision' ),
'cited' => __( 'Cited', 'pr-vision' ),
'not_cited' => __( 'Not cited', 'pr-vision' ),
'error' => __( 'Error', 'pr-vision' ),
);
echo '<label for="prv-f-status" class="prv-sr-only">' . esc_html__( 'Status', 'pr-vision' ) . '</label>';
echo '<select id="prv-f-status" name="prv_filter_status" class="prv-filter-select">';
foreach ( $status_opts as $val => $label ) {
$sel = ( $filters['status'] === $val ) ? ' selected' : '';
echo '<option value="' . esc_attr( $val ) . '"' . $sel . '>' . esc_html( $label ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $sel is safe
}
echo '</select>';
echo '<button type="submit" class="prv-filter-apply button">' . esc_html__( 'Filter', 'pr-vision' ) . '</button>';
echo '<a href="' . esc_url( $base_url ) . '" class="prv-filter-clear button">' . esc_html__( 'Clear', 'pr-vision' ) . '</a>';
echo '</form>';
}
/**
* Render the calls table.
*
* Side effects: Outputs HTML.
*
* @param array<int, array<string, mixed>> $rows Table rows from query.
*
* @return void
*/
private function render_table( array $rows ): void {
echo '<div class="prv-table-wrap" style="overflow-x:auto">';
echo '<table class="prv-standings prv-callog-table" cellspacing="0" aria-label="' . esc_attr__( 'Call log', 'pr-vision' ) . '">';
echo '<thead><tr>';
echo '<th>' . esc_html__( 'Timestamp', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Peptide', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Intent', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Model', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Tokens', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Latency', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Cost', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Cited', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Status', 'pr-vision' ) . '</th>';
echo '</tr></thead><tbody>';
if ( empty( $rows ) ) {
echo '<tr><td colspan="9" class="prv-empty-msg">' . esc_html__( 'No calls recorded yet.', 'pr-vision' ) . '</td></tr>';
}
foreach ( $rows as $row ) {
$id = (int) $row['id'];
$is_error = ( (int) $row['http_status'] >= 400 );
$cited_null = ( null === $row['cited'] );
$is_cited = ! $cited_null && (bool) $row['cited'];
$latency_ms = isset( $row['latency_ms'] ) ? (int) $row['latency_ms'] : null;
$lat_warn = ( null !== $latency_ms && $latency_ms > 8000 ) ? ' style="color:#FFB36E"' : '';
$status_chip = $is_error ? '<span class="prv-chip prv-chip--error">&#x2715; Error</span>' : '<span class="prv-chip prv-chip--ok">OK</span>';
if ( $is_error ) {
$cited_chip = '<span class="prv-status prv-status--not-yet">&#x2013; Error</span>';
} elseif ( $cited_null ) {
$cited_chip = '<span class="prv-status prv-status--not-yet">&#x2013; —</span>';
} elseif ( $is_cited ) {
$cited_chip = '<span class="prv-status prv-status--cited">&#x2713; Cited</span>';
} else {
$cited_chip = '<span class="prv-status prv-status--not-yet">&#x2013; Not yet</span>';
}
echo '<tr id="call-row-' . esc_attr( (string) $id ) . '" class="prv-call-row" tabindex="0" role="button" data-call-id="' . esc_attr( (string) $id ) . '" aria-label="' . esc_attr( sprintf( /* translators: %d: call ID */ __( 'Call #%d details', 'pr-vision' ), $id ) ) . '">';
echo '<td class="prv-tabnum">' . esc_html( (string) $row['captured_at'] ) . '</td>';
echo '<td><strong>' . esc_html( (string) $row['peptide_slug'] ) . '</strong></td>';
echo '<td class="prv-muted">' . esc_html( (string) $row['intent_label'] ) . '</td>';
echo '<td><code>' . esc_html( (string) $row['model'] ) . '</code></td>';
$tok_in = isset( $row['tokens_in'] ) && null !== $row['tokens_in'] ? (int) $row['tokens_in'] : null;
$tok_out = isset( $row['tokens_out'] ) && null !== $row['tokens_out'] ? (int) $row['tokens_out'] : null;
$tok_str = ( null !== $tok_in && null !== $tok_out )
? '&#x2191;' . esc_html( number_format( $tok_in ) ) . ' &#x2193;' . esc_html( number_format( $tok_out ) )
: '—';
echo '<td class="prv-tabnum prv-muted">' . $tok_str . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- arrows are HTML entities; numbers are escaped above
echo '<td class="prv-tabnum"' . $lat_warn . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static style attr
echo null !== $latency_ms ? esc_html( number_format( $latency_ms ) ) . 'ms' : '—';
echo '</td>';
echo '<td class="prv-tabnum">$' . esc_html( number_format( (float) $row['cost_usd'], 6 ) ) . '</td>';
echo '<td>' . $cited_chip . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from safe parts above
echo '<td>' . $status_chip . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from safe parts above
echo '</tr>';
}
echo '</tbody></table></div>';
}
/**
* Render pagination bar.
*
* Disabled buttons use colour/border, not opacity (WCAG AA).
* Side effects: Outputs HTML.
*
* @param int $page Current page (1-based).
* @param int $pages Total pages.
* @param array<string, string> $filters Active filters for link building.
*
* @return void
*/
private function render_pagination( int $page, int $pages, array $filters ): void {
if ( $pages <= 1 ) {
return;
}
$base = admin_url( 'admin.php' );
$params = array( 'page' => 'pr-vision-calls' );
foreach ( $filters as $k => $v ) {
if ( '' !== $v ) {
$params[ 'prv_filter_' . ltrim( $k, 'prv_filter_' ) ] = $v;
}
}
echo '<div class="prv-pagination" role="navigation" aria-label="' . esc_attr__( 'Pagination', 'pr-vision' ) . '">';
// Prev.
if ( $page > 1 ) {
$prev_url = add_query_arg( array_merge( $params, array( 'prv_page' => $page - 1 ) ), $base );
echo '<a href="' . esc_url( $prev_url ) . '" class="prv-page-btn">&#x2190; ' . esc_html__( 'Prev', 'pr-vision' ) . '</a>';
} else {
echo '<span class="prv-page-btn prv-page-btn--disabled" aria-disabled="true">&#x2190; ' . esc_html__( 'Prev', 'pr-vision' ) . '</span>';
}
echo '<span class="prv-page-info">' . esc_html( sprintf( /* translators: 1: current, 2: total */ __( 'Page %1$d of %2$d', 'pr-vision' ), $page, $pages ) ) . '</span>';
// Next.
if ( $page < $pages ) {
$next_url = add_query_arg( array_merge( $params, array( 'prv_page' => $page + 1 ) ), $base );
echo '<a href="' . esc_url( $next_url ) . '" class="prv-page-btn">' . esc_html__( 'Next', 'pr-vision' ) . ' &#x2192;</a>';
} else {
echo '<span class="prv-page-btn prv-page-btn--disabled" aria-disabled="true">' . esc_html__( 'Next', 'pr-vision' ) . ' &#x2192;</span>';
}
echo '<span class="prv-page-per">' . esc_html( sprintf( /* translators: %d: rows per page */ __( '%d per page', 'pr-vision' ), PRV_Call_Log_Query::PAGE_SIZE ) ) . '</span>';
echo '</div>';
}
}

View File

@@ -0,0 +1,112 @@
<?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,
io_captured TINYINT(1) NOT NULL DEFAULT 0,
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}" );
}
}

View File

@@ -0,0 +1,184 @@
<?php
/**
* Best-effort per-call capture writer (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Writes per-call cost/metadata and I/O records after a probe completes.
*
* Security contract (P0):
* - Accepts only the rendered request BODY (messages + model + params).
* - NEVER receives $headers, $parsed_args, Authorization, or api keys.
* - Allowlist: prompt_text, response_text, model, peptide_slug, intent_label,
* tokens_in, tokens_out, cost_usd, latency_ms, cited, http_status,
* config_version, visibility_row, run_id, captured_at, io_captured.
*
* Capture is BEST-EFFORT: capture_io() is wrapped in a swallowing try/catch
* by the caller (PRV_Probe_Runner). A write failure must never reach the probe
* path or corrupt cap accounting.
*
* Who triggers: PRV_Probe_Runner — capture_io() is called LAST, after
* persist_result() and update_row_cost() complete.
* Dependencies: $wpdb, PRV_Call_Meta_Table, PRV_Call_Io_Table.
*
* @see class-prv-call-meta-table.php — Metadata table (kept indefinitely).
* @see class-prv-call-io-table.php — I/O table (pruned on retention window).
* @see class-prv-probe-runner.php — Caller; wraps capture_io() in try/catch.
* @see ARCHITECTURE.md — §Capture flow v0.3.0.
* @package PrVision
*/
class PRV_Capture_Writer {
/**
* Write one metadata row to prv_call_meta.
*
* Called inside the probe loop BEFORE capture_io() so the metadata row
* exists even when the I/O write fails. Returns the inserted row ID.
* io_captured is always 0 here; call set_io_captured() after write_io() succeeds.
*
* Side effects: Database write to prv_call_meta.
*
* @param array<string, mixed> $meta Allowlisted fields: visibility_row (int|null),
* run_id (string), peptide_slug (string), model (string), intent_label (string),
* tokens_in (int|null), tokens_out (int|null), cost_usd (float),
* latency_ms (int|null), cited (int|null), http_status (int),
* config_version (int|null).
*
* @return int Inserted row ID, or 0 on failure.
*/
public function write_meta( array $meta ): int {
global $wpdb;
$now = current_time( 'mysql', true );
$table = PRV_Call_Meta_Table::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->insert(
$table,
array(
'visibility_row' => isset( $meta['visibility_row'] ) ? (int) $meta['visibility_row'] : null,
'run_id' => (string) ( $meta['run_id'] ?? '' ),
'peptide_slug' => (string) ( $meta['peptide_slug'] ?? '' ),
'model' => (string) ( $meta['model'] ?? '' ),
'intent_label' => (string) ( $meta['intent_label'] ?? '' ),
'tokens_in' => isset( $meta['tokens_in'] ) ? (int) $meta['tokens_in'] : null,
'tokens_out' => isset( $meta['tokens_out'] ) ? (int) $meta['tokens_out'] : null,
'cost_usd' => number_format( (float) ( $meta['cost_usd'] ?? 0.0 ), 8, '.', '' ),
'latency_ms' => isset( $meta['latency_ms'] ) ? (int) $meta['latency_ms'] : null,
'cited' => isset( $meta['cited'] ) ? (int) $meta['cited'] : null,
'http_status' => (int) ( $meta['http_status'] ?? 200 ),
'captured_at' => $now,
'config_version' => isset( $meta['config_version'] ) ? (int) $meta['config_version'] : null,
'io_captured' => 0,
),
array( '%d', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%s', '%d', '%d' )
);
return (int) $wpdb->insert_id;
}
/**
* Write one I/O row to prv_call_io (best-effort; caller must catch).
*
* Security: only prompt_text and response_text are stored — never
* headers, Authorization, or any raw HTTP request object.
*
* On success, sets io_captured = 1 on the matching prv_call_meta row.
* The meta update is a best-effort follow-up; a failure here is not
* propagated (caller already swallows this entire method).
*
* Side effects: Database writes to prv_call_io and prv_call_meta.
*
* @param int $call_id FK to prv_call_meta.id.
* @param string $prompt_text Rendered plain-text prompt (messages joined).
* @param string $response_text Raw text content from LLM response.
*
* @return bool True on success.
*/
public function write_io( int $call_id, string $prompt_text, string $response_text ): bool {
global $wpdb;
$table = PRV_Call_Io_Table::get_table_name();
$now = current_time( 'mysql', true );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$result = $wpdb->insert(
$table,
array(
'call_id' => $call_id,
'prompt_text' => $prompt_text,
'response_text' => $response_text,
'captured_at' => $now,
),
array( '%d', '%s', '%s', '%s' )
);
if ( false !== $result ) {
$this->set_io_captured( $call_id );
return true;
}
return false;
}
/**
* Update cost_usd on an existing prv_call_meta row.
*
* Called after the probe returns its actual cost, mirroring the pattern
* already used by PRV_Cost_Ledger::update_row_cost() for prv_ai_visibility.
*
* Side effects: Database write to prv_call_meta.
*
* @param int $call_id PK of the prv_call_meta row.
* @param float $cost_usd Settled cost in USD.
*
* @return bool True on success.
*/
public function update_meta_cost( int $call_id, float $cost_usd ): bool {
global $wpdb;
$table = PRV_Call_Meta_Table::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->update(
$table,
array( 'cost_usd' => number_format( $cost_usd, 8, '.', '' ) ),
array( 'id' => $call_id ),
array( '%s' ),
array( '%d' )
);
return false !== $result;
}
/**
* Mark io_captured = 1 on the prv_call_meta row after a successful I/O write.
*
* Best-effort: called internally from write_io(); failure is non-fatal.
* Legacy/pre-feature rows retain io_captured = 0 (the column default).
*
* Side effects: Database write to prv_call_meta.
*
* @param int $call_id PK of the prv_call_meta row.
*
* @return void
*/
private function set_io_captured( int $call_id ): void {
global $wpdb;
$table = PRV_Call_Meta_Table::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->update(
$table,
array( 'io_captured' => 1 ),
array( 'id' => $call_id ),
array( '%d' ),
array( '%d' )
);
}
}

View File

@@ -137,6 +137,20 @@ class PRV_Config {
); );
} }
/**
* Get the I/O retention window in days.
*
* Raw prompt + response text in prv_call_io is pruned after this many days.
* Cost/metadata in prv_call_meta is kept indefinitely.
*
* @return int Days; minimum 1, default PRV_IO_RETENTION_DEFAULT_DAYS.
*/
public static function get_io_retention_days(): int {
$val = (int) get_option( 'prv_io_retention_days', PRV_IO_RETENTION_DEFAULT_DAYS );
return max( 1, $val );
}
/** /**
* Default set of ~12 high-interest peptides (slug + human-readable label). * Default set of ~12 high-interest peptides (slug + human-readable label).
* *

View File

@@ -0,0 +1,205 @@
<?php
/**
* Cost rollup queries for the Costs admin page (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Executes cost rollup queries across run / peptide / intent / model / call.
*
* All queries are bounded (LIMIT/OFFSET) and parameterised via $wpdb->prepare.
* The MTD total from this class reconciles to PRV_Cost_Ledger::get_month_to_date_usd()
* because both sum cost_usd on prv_call_meta filtered by current calendar month.
*
* Who triggers: PRV_Costs_Page::render_page().
* Dependencies: $wpdb, PRV_Call_Meta_Table, PRV_Config.
*
* @see class-prv-call-meta-table.php — Source table.
* @see class-prv-cost-ledger.php — MTD total (reconciliation reference).
* @see class-prv-costs-page.php — Consumer of rollup data.
* @package PrVision
*/
class PRV_Cost_Rollup_Query {
/**
* Rows per page for the drill-down table.
*/
const PAGE_SIZE = 50;
/**
* Fetch MTD totals: total cost, total calls, avg cost/call for the month.
*
* Side effects: Database read.
*
* @return array{total_cost: float, total_calls: int, avg_cost: float}
*/
public function get_mtd_summary(): array {
global $wpdb;
$table = PRV_Call_Meta_Table::get_table_name();
$month = gmdate( 'Y-m' );
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$row = $wpdb->get_row(
$wpdb->prepare(
"SELECT COALESCE(SUM(cost_usd),0) AS total_cost, COUNT(*) AS total_calls
FROM {$table}
WHERE DATE_FORMAT(captured_at, %s) = %s",
'%Y-%m',
$month
),
ARRAY_A
);
// phpcs:enable
$total_cost = (float) ( $row['total_cost'] ?? 0.0 );
$total_calls = (int) ( $row['total_calls'] ?? 0 );
$avg_cost = $total_calls > 0 ? $total_cost / $total_calls : 0.0;
return array(
'total_cost' => $total_cost,
'total_calls' => $total_calls,
'avg_cost' => $avg_cost,
);
}
/**
* Fetch per-model MTD cost breakdown, ordered by cost descending.
*
* Side effects: Database read.
*
* @return array<int, array{model: string, calls: int, mtd_cost: float, cost_per_call: float}>
*/
public function get_model_breakdown(): array {
global $wpdb;
$table = PRV_Call_Meta_Table::get_table_name();
$month = gmdate( 'Y-m' );
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT model,
COUNT(*) AS calls,
COALESCE(SUM(cost_usd),0) AS mtd_cost
FROM {$table}
WHERE DATE_FORMAT(captured_at, %s) = %s
GROUP BY model
ORDER BY mtd_cost DESC
LIMIT 50",
'%Y-%m',
$month
),
ARRAY_A
);
// phpcs:enable
$result = array();
foreach ( (array) $rows as $row ) {
$calls = (int) $row['calls'];
$mtd_cost = (float) $row['mtd_cost'];
$result[] = array(
'model' => (string) $row['model'],
'calls' => $calls,
'mtd_cost' => $mtd_cost,
'cost_per_call' => $calls > 0 ? $mtd_cost / $calls : 0.0,
);
}
return $result;
}
/**
* Fetch cost drill-down by aggregation level (run/peptide/intent/model).
*
* Side effects: Database read.
*
* @param string $level One of: run, peptide, intent, model.
* @param int $offset Pagination offset.
*
* @return array<int, array<string, mixed>> Rows appropriate for the level.
*/
public function get_drill_down( string $level, int $offset = 0 ): array {
global $wpdb;
$table = PRV_Call_Meta_Table::get_table_name();
$month = gmdate( 'Y-m' );
$page_size = self::PAGE_SIZE;
$offset = max( 0, $offset );
$allowed_levels = array( 'run', 'peptide', 'intent', 'model' );
if ( ! in_array( $level, $allowed_levels, true ) ) {
$level = 'run';
}
switch ( $level ) {
case 'peptide':
$group_col = 'peptide_slug';
break;
case 'intent':
$group_col = 'intent_label';
break;
case 'model':
$group_col = 'model';
break;
default:
$group_col = 'run_id';
}
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT {$group_col} AS group_key,
COUNT(*) AS calls,
COALESCE(SUM(cost_usd),0) AS total_cost,
MIN(captured_at) AS first_at,
MAX(captured_at) AS last_at
FROM {$table}
WHERE DATE_FORMAT(captured_at, %s) = %s
GROUP BY {$group_col}
ORDER BY total_cost DESC
LIMIT %d OFFSET %d",
'%Y-%m',
$month,
$page_size,
$offset
),
ARRAY_A
);
// phpcs:enable
$result = array();
foreach ( (array) $rows as $row ) {
$result[] = array(
'group_key' => (string) $row['group_key'],
'calls' => (int) $row['calls'],
'total_cost' => (float) $row['total_cost'],
'first_at' => (string) $row['first_at'],
'last_at' => (string) $row['last_at'],
);
}
return $result;
}
/**
* Compute the linear month-end projection from MTD spend and days elapsed.
*
* Side effects: None.
*
* @param float $mtd_cost MTD cost in USD.
*
* @return float Projected full-month cost.
*/
public function project_month_end( float $mtd_cost ): float {
$days_in_month = (int) gmdate( 't' );
$day_of_month = (int) gmdate( 'j' );
if ( $day_of_month < 1 ) {
return $mtd_cost;
}
return $mtd_cost * ( $days_in_month / $day_of_month );
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* PR Vision Costs admin sub-page (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Costs sub-page: MTD spend, cap progress, model breakdown, drill-down.
*
* Registers "PR Vision > Costs" sub-menu and renders the Costs page via
* PRV_Costs_Renderer. All data is read from PRV_Cost_Rollup_Query.
*
* Who triggers: PRV_Plugin::init() — is_admin() guard.
* Dependencies: PRV_Cost_Rollup_Query, PRV_Costs_Renderer, PRV_Config.
*
* @see class-prv-costs-renderer.php — HTML rendering.
* @see class-prv-cost-rollup-query.php — Query layer.
* @see class-prv-admin-page.php — Dashboard (parent menu slug).
* @package PrVision
*/
class PRV_Costs_Page {
/**
* Admin menu slug.
*/
const MENU_SLUG = 'pr-vision-costs';
/**
* Register WordPress admin hooks.
*
* Side effects: Adds admin_menu action.
*
* @return void
*/
public function register_hooks(): void {
add_action( 'admin_menu', array( $this, 'add_submenu' ) );
}
/**
* Register the Costs sub-menu under PR Vision.
*
* Side effects: Adds a WP admin sub-menu entry.
*
* @return void
*/
public function add_submenu(): void {
add_submenu_page(
PRV_Admin_Page::MENU_SLUG,
__( 'PR Vision — Costs', 'pr-vision' ),
__( 'Costs', 'pr-vision' ),
'manage_options',
self::MENU_SLUG,
array( $this, 'render_page' )
);
}
/**
* Render the full Costs page.
*
* Side effects: Outputs HTML.
*
* @return void
*/
public function render_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Insufficient permissions.', 'pr-vision' ) );
}
$query = new PRV_Cost_Rollup_Query();
$summary = $query->get_mtd_summary();
$models = $query->get_model_breakdown();
// Drill-down parameters.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$level = isset( $_GET['prv_level'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_level'] ) ) : 'run';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$offset = isset( $_GET['prv_offset'] ) ? absint( wp_unslash( $_GET['prv_offset'] ) ) : 0;
$drill = $query->get_drill_down( $level, $offset );
$cap = PRV_Config::get_monthly_budget_usd();
$mtd_cost = $summary['total_cost'];
$projected = $query->project_month_end( $mtd_cost );
$renderer = new PRV_Costs_Renderer();
$renderer->render(
array(
'mtd_cost' => $mtd_cost,
'total_calls' => $summary['total_calls'],
'avg_cost' => $summary['avg_cost'],
'cap' => $cap,
'projected' => $projected,
'models' => $models,
'drill' => $drill,
'level' => $level,
'offset' => $offset,
)
);
}
}

View File

@@ -0,0 +1,297 @@
<?php
/**
* HTML renderer for the PR Vision Costs admin page (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Renders the Costs admin page in the dark "Assay" theme.
*
* Renders: cap progress card, summary bento (3 tiles), model breakdown,
* and the cost drill-down table. All output is escaped at render.
*
* Who triggers: PRV_Costs_Page::render_page().
* Dependencies: PRV_Admin_Page (subnav helper), no DB access.
*
* @see class-prv-costs-page.php — Caller, passes data array.
* @see class-prv-call-log-page.php — Sibling sub-page.
* @see ARCHITECTURE.md — §Admin surfaces v0.3.0.
* @package PrVision
*/
class PRV_Costs_Renderer {
/**
* Render the full Costs page.
*
* Side effects: Outputs HTML.
*
* @param array<string, mixed> $data Keys: mtd_cost, total_calls, avg_cost, cap,
* projected, models, drill, level, offset.
*
* @return void
*/
public function render( array $data ): void {
$mtd_cost = (float) $data['mtd_cost'];
$total_calls = (int) $data['total_calls'];
$avg_cost = (float) $data['avg_cost'];
$cap = (float) $data['cap'];
$projected = (float) $data['projected'];
$models = (array) $data['models'];
$drill = (array) $data['drill'];
$level = (string) $data['level'];
$offset = (int) $data['offset'];
$meter_pct = $cap > 0 ? min( 100, round( ( $mtd_cost / $cap ) * 100 ) ) : 0;
$meter_class = $meter_pct >= 90 ? ' prv-meter--capped' : '';
$projected_warn = $projected > $cap;
echo '<div class="wrap prv-page-wrap">';
$this->render_subnav( 'costs' );
echo '<div class="prv-page-header">';
echo '<h1 class="prv-page-title">' . esc_html__( 'Cost Accountability', 'pr-vision' ) . '</h1>';
echo '<p class="prv-page-sub">' . esc_html__( 'Per-call spend against the $5.00 monthly cap.', 'pr-vision' ) . '</p>';
echo '</div>';
// Cap progress card.
echo '<div class="prv-card prv-card--cap">';
echo '<div class="prv-card-head"><h2>' . esc_html__( 'Budget', 'pr-vision' ) . '</h2></div>';
echo '<div class="prv-card-body">';
echo '<div class="prv-cap-row">';
echo '<div class="prv-cap-left">';
echo '<span class="prv-cost-big">$' . esc_html( number_format( $mtd_cost, 2 ) ) . '</span>';
echo '<span class="prv-cost-cap"> ' . esc_html( sprintf( /* translators: %s: cap amount */ __( 'of $%s', 'pr-vision' ), number_format( $cap, 2 ) ) ) . '</span>';
echo '</div>';
echo '<div class="prv-cap-right">';
$proj_class = $projected_warn ? ' style="color:#FF9D4D"' : '';
echo '<div class="prv-cap-projected"' . $proj_class . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static string
echo '<span class="prv-cap-proj-label">' . esc_html__( 'Linear estimate', 'pr-vision' ) . '</span>';
echo '<span class="prv-cap-proj-val">$' . esc_html( number_format( $projected, 2 ) ) . '</span>';
echo '</div>';
$days_left = (int) gmdate( 't' ) - (int) gmdate( 'j' );
echo '<div class="prv-cap-days">' . esc_html( sprintf( /* translators: %d: days */ __( '%d days remaining', 'pr-vision' ), $days_left ) ) . '</div>';
echo '</div>';
echo '</div>';
echo '<div class="prv-meter' . esc_attr( $meter_class ) . '" role="progressbar" aria-valuenow="' . esc_attr( (string) $meter_pct ) . '" aria-valuemin="0" aria-valuemax="100">';
echo '<span style="width:' . esc_attr( $meter_pct . '%' ) . '"></span>';
echo '</div>';
if ( $total_calls > 0 ) {
echo '<p class="prv-cap-reconcile">';
echo esc_html(
sprintf(
/* translators: 1: cost, 2: call count */
__( '$%1$s recorded across %2$d calls this month — approximately reconciles to the Dashboard MTD total.', 'pr-vision' ),
number_format( $mtd_cost, 4 ),
$total_calls
)
);
echo '</p>';
}
echo '</div></div>';
// Summary bento.
echo '<div class="prv-bento">';
$this->render_bento_tile( __( 'Total calls', 'pr-vision' ), (string) $total_calls, __( 'this month', 'pr-vision' ) );
$this->render_bento_tile( __( 'Avg cost / call', 'pr-vision' ), '$' . number_format( $avg_cost, 4 ), '' );
$top_model = ! empty( $models ) ? $models[0]['model'] . ' ($' . number_format( $models[0]['mtd_cost'], 4 ) . ')' : __( '—', 'pr-vision' );
$this->render_bento_tile( __( 'Most expensive model', 'pr-vision' ), $top_model, '' );
echo '</div>';
// Model breakdown table.
$this->render_model_table( $models );
// Drill-down card.
$this->render_drill_down( $drill, $level, $offset );
echo '</div>'; // .wrap
}
/**
* Render the subnav tab strip (shared across all PR Vision pages).
*
* Side effects: Outputs HTML.
*
* @param string $active Active tab key: dashboard|costs|calls|settings.
*
* @return void
*/
public function render_subnav( string $active ): void {
$tabs = array(
'dashboard' => array(
'label' => __( 'Dashboard', 'pr-vision' ),
'slug' => 'pr-vision',
),
'costs' => array(
'label' => __( 'Costs', 'pr-vision' ),
'slug' => 'pr-vision-costs',
),
'calls' => array(
'label' => __( 'Call Log', 'pr-vision' ),
'slug' => 'pr-vision-calls',
),
'settings' => array(
'label' => __( 'Settings', 'pr-vision' ),
'slug' => 'pr-vision-settings',
),
);
echo '<nav class="prv-subnav" aria-label="' . esc_attr__( 'PR Vision navigation', 'pr-vision' ) . '">';
foreach ( $tabs as $key => $tab ) {
$is_active = ( $key === $active );
$aria_current = $is_active ? ' aria-current="page"' : '';
$tab_class = 'prv-subnav-tab' . ( $is_active ? ' prv-subnav-tab--active' : '' );
$url = admin_url( 'admin.php?page=' . rawurlencode( $tab['slug'] ) );
echo '<a href="' . esc_url( $url ) . '" class="' . esc_attr( $tab_class ) . '"' . $aria_current . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- aria-current is safe static string
echo esc_html( $tab['label'] );
echo '</a>';
}
echo '</nav>';
}
/**
* Render a single bento tile.
*
* Side effects: Outputs HTML.
*
* @param string $label Label text.
* @param string $value Primary value.
* @param string $sub Sub-label (empty to omit).
*
* @return void
*/
private function render_bento_tile( string $label, string $value, string $sub ): void {
echo '<div class="prv-tile">';
echo '<div class="prv-tile-label">' . esc_html( $label ) . '</div>';
echo '<div class="prv-tile-big prv-tile-big--sm">' . esc_html( $value ) . '</div>';
if ( '' !== $sub ) {
echo '<div class="prv-tile-sub">' . esc_html( $sub ) . '</div>';
}
echo '</div>';
}
/**
* Render the per-model cost breakdown table.
*
* Side effects: Outputs HTML.
*
* @param array<int, array<string, mixed>> $models Model rows.
*
* @return void
*/
private function render_model_table( array $models ): void {
echo '<div class="prv-card"><div class="prv-card-head"><h2>';
echo esc_html__( 'By Model', 'pr-vision' );
echo '</h2></div><div class="prv-card-body">';
if ( empty( $models ) ) {
echo '<p class="prv-empty-msg">' . esc_html__( 'No cost data yet. Run a probe to begin recording per-call costs.', 'pr-vision' ) . '</p>';
echo '</div></div>';
return;
}
echo '<div class="prv-table-wrap"><table class="prv-standings" cellspacing="0">';
echo '<thead><tr>';
echo '<th>' . esc_html__( 'Model', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Calls', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'MTD Cost', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Cost / Call', 'pr-vision' ) . '</th>';
echo '</tr></thead><tbody>';
foreach ( $models as $row ) {
echo '<tr>';
echo '<td><code>' . esc_html( (string) $row['model'] ) . '</code></td>';
echo '<td>' . esc_html( (string) (int) $row['calls'] ) . '</td>';
echo '<td>$' . esc_html( number_format( (float) $row['mtd_cost'], 6 ) ) . '</td>';
echo '<td>$' . esc_html( number_format( (float) $row['cost_per_call'], 6 ) ) . '</td>';
echo '</tr>';
}
echo '</tbody></table></div>';
echo '</div></div>';
}
/**
* Render the drill-down card with level picker and results table.
*
* Side effects: Outputs HTML.
*
* @param array<int, array<string, mixed>> $drill Drill-down rows.
* @param string $level Current aggregation level.
* @param int $offset Current pagination offset.
*
* @return void
*/
private function render_drill_down( array $drill, string $level, int $offset ): void {
echo '<div class="prv-card"><div class="prv-card-head"><h2>';
echo esc_html__( 'Cost Drill-Down', 'pr-vision' );
echo '</h2></div><div class="prv-card-body">';
$levels = array( 'run', 'peptide', 'intent', 'model' );
$base_url = admin_url( 'admin.php?page=pr-vision-costs' );
echo '<div class="prv-seg" role="group" aria-label="' . esc_attr__( 'Aggregation level', 'pr-vision' ) . '">';
foreach ( $levels as $lvl ) {
$is_active = ( $lvl === $level );
$cls = 'prv-seg-btn' . ( $is_active ? ' prv-seg-btn--active' : '' );
$url = add_query_arg(
array(
'prv_level' => $lvl,
'prv_offset' => 0,
),
$base_url
);
echo '<a href="' . esc_url( $url ) . '" class="' . esc_attr( $cls ) . '"' . ( $is_active ? ' aria-current="true"' : '' ) . '>';
echo esc_html( ucfirst( $lvl ) );
echo '</a>';
}
echo '</div>';
if ( empty( $drill ) ) {
echo '<p class="prv-empty-msg">' . esc_html__( 'No data for this level.', 'pr-vision' ) . '</p>';
echo '</div></div>';
return;
}
echo '<div class="prv-table-wrap"><table class="prv-standings" cellspacing="0">';
echo '<thead><tr>';
echo '<th>' . esc_html( ucfirst( $level ) ) . '</th>';
echo '<th>' . esc_html__( 'Calls', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'Cost', 'pr-vision' ) . '</th>';
echo '<th>' . esc_html__( 'View', 'pr-vision' ) . '</th>';
echo '</tr></thead><tbody>';
foreach ( $drill as $row ) {
$filter_url = add_query_arg(
array( $this->level_to_filter_key( $level ) => rawurlencode( (string) $row['group_key'] ) ),
admin_url( 'admin.php?page=pr-vision-calls' )
);
echo '<tr>';
echo '<td class="prv-mono">' . esc_html( (string) $row['group_key'] ) . '</td>';
echo '<td>' . esc_html( (string) (int) $row['calls'] ) . '</td>';
echo '<td>$' . esc_html( number_format( (float) $row['total_cost'], 6 ) ) . '</td>';
echo '<td><a href="' . esc_url( $filter_url ) . '">' . esc_html__( 'View calls →', 'pr-vision' ) . '</a></td>';
echo '</tr>';
}
echo '</tbody></table></div>';
echo '</div></div>';
}
/**
* Map drill-down level to call-log filter query param key.
*
* @param string $level Drill-down level: run|peptide|intent|model.
*
* @return string Filter parameter name.
*/
private function level_to_filter_key( string $level ): string {
$map = array(
'run' => 'prv_filter_run',
'peptide' => 'prv_filter_peptide',
'intent' => 'prv_filter_intent',
'model' => 'prv_filter_model',
);
return $map[ $level ] ?? 'prv_filter_run';
}
}

View File

@@ -17,6 +17,7 @@ declare(strict_types=1);
* Dependencies: PRV_Cron. * Dependencies: PRV_Cron.
* *
* @see class-prv-cron.php — Cron registration + clearing. * @see class-prv-cron.php — Cron registration + clearing.
* @see class-prv-prune-cron.php — Daily prune cron clear (v0.3.0).
* @see uninstall.php — Full data purge on uninstall. * @see uninstall.php — Full data purge on uninstall.
* @package PrVision * @package PrVision
*/ */
@@ -31,5 +32,7 @@ class PRV_Deactivator {
*/ */
public static function deactivate(): void { public static function deactivate(): void {
PRV_Cron::clear_schedule(); PRV_Cron::clear_schedule();
// v0.3.0: clear the daily prune event.
PRV_Prune_Cron::clear_schedule();
} }
} }

View File

@@ -16,10 +16,14 @@ declare(strict_types=1);
* *
* Who triggers: plugins_loaded action (pr-vision.php). * Who triggers: plugins_loaded action (pr-vision.php).
* Dependencies: PRV_Upgrader, PRV_Cron, PRV_Admin_Page, PRV_Settings_Page, * Dependencies: PRV_Upgrader, PRV_Cron, PRV_Admin_Page, PRV_Settings_Page,
* PRV_Collector_Registry. * PRV_Settings_Controller, PRV_Costs_Page, PRV_Call_Log_Page,
* PRV_Call_Detail_Ajax, PRV_Prune_Cron, PRV_Collector_Registry.
* *
* @see ARCHITECTURE.md -- Boot sequence diagram. * @see ARCHITECTURE.md -- Boot sequence diagram.
* @see class-prv-upgrader.php -- Runs migrations on every boot. * @see class-prv-upgrader.php -- Runs migrations on every boot.
* @see class-prv-costs-page.php -- [v0.3.0] Costs admin sub-page.
* @see class-prv-call-log-page.php -- [v0.3.0] Call Log admin sub-page.
* @see class-prv-prune-cron.php -- [v0.3.0] Daily prune cron.
* @package PrVision * @package PrVision
*/ */
class PRV_Plugin { class PRV_Plugin {
@@ -43,12 +47,24 @@ class PRV_Plugin {
$cron = new PRV_Cron(); $cron = new PRV_Cron();
$cron->register_hooks(); $cron->register_hooks();
$prune = new PRV_Prune_Cron();
$prune->register_hooks();
if ( is_admin() ) { if ( is_admin() ) {
$page = new PRV_Admin_Page(); $page = new PRV_Admin_Page();
$page->register_hooks(); $page->register_hooks();
$settings = new PRV_Settings_Page(); $settings = new PRV_Settings_Page();
$settings->register_hooks(); $settings->register_hooks();
$costs = new PRV_Costs_Page();
$costs->register_hooks();
$call_log = new PRV_Call_Log_Page();
$call_log->register_hooks();
$ajax = new PRV_Call_Detail_Ajax();
$ajax->register_hooks();
} }
// Register the v1 AI-visibility collector + panel. // Register the v1 AI-visibility collector + panel.

View File

@@ -13,6 +13,8 @@ declare(strict_types=1);
* Returned by every PRV_Probe_Provider implementation. Immutable — all * Returned by every PRV_Probe_Provider implementation. Immutable — all
* fields are set in the constructor and read via typed getters. * fields are set in the constructor and read via typed getters.
* *
* v0.3.0 adds tokens_in/tokens_out for per-call cost attribution.
*
* Who triggers: PRV_Perplexity_Provider and PRV_OpenRouter_Provider. * Who triggers: PRV_Perplexity_Provider and PRV_OpenRouter_Provider.
* Dependencies: None. * Dependencies: None.
* *
@@ -31,13 +33,17 @@ class PRV_Probe_Result {
* @param bool $cited Whether peptiderepo.com appears in source_domains. * @param bool $cited Whether peptiderepo.com appears in source_domains.
* @param int|null $our_position 1-based position of peptiderepo.com in sources, or null. * @param int|null $our_position 1-based position of peptiderepo.com in sources, or null.
* @param float $cost_usd Estimated call cost in USD. * @param float $cost_usd Estimated call cost in USD.
* @param int|null $tokens_in Input token count from the API response (null if unknown).
* @param int|null $tokens_out Output token count from the API response (null if unknown).
*/ */
public function __construct( public function __construct(
private readonly string $raw_excerpt, private readonly string $raw_excerpt,
private readonly array $source_domains, private readonly array $source_domains,
private readonly bool $cited, private readonly bool $cited,
private readonly ?int $our_position, private readonly ?int $our_position,
private readonly float $cost_usd private readonly float $cost_usd,
private readonly ?int $tokens_in = null,
private readonly ?int $tokens_out = null
) {} ) {}
/** /**
@@ -84,4 +90,22 @@ class PRV_Probe_Result {
public function get_cost_usd(): float { public function get_cost_usd(): float {
return $this->cost_usd; return $this->cost_usd;
} }
/**
* Get the input token count for this call (null if not reported by provider).
*
* @return int|null
*/
public function get_tokens_in(): ?int {
return $this->tokens_in;
}
/**
* Get the output token count for this call (null if not reported by provider).
*
* @return int|null
*/
public function get_tokens_out(): ?int {
return $this->tokens_out;
}
} }

View File

@@ -0,0 +1,300 @@
<?php
/**
* Inner probe loop executor split from PRV_Probe_Runner (v0.3.0).
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Executes one probe run's inner loop: peptides × intents × models.
* P0: can_afford → probe → persist → update_cost → write_meta → [swallowed] capture_io.
*
* @see class-prv-capture-writer.php — Allowlist-only capture writer (P0).
* @package PrVision
*/
class PRV_Probe_Run_Executor {
/**
* 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 $ledger Budget ledger.
* @param PRV_Capture_Writer $capture Capture writer.
*/
public function __construct( PRV_Cost_Ledger $ledger, PRV_Capture_Writer $capture ) {
$this->ledger = $ledger;
$this->capture = $capture;
}
/**
* Run the full peptide × intent × model loop.
*
* @param string $run_id UUID for this run.
* @param int $config_ver Config version.
* @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string}
*/
public function execute(
string $run_id,
int $config_ver
): array {
$peptides = PRV_Config::get_peptides();
$intents = PRV_Config::get_prompt_intents();
$models = PRV_Config::get_models();
$model_outcomes = array();
foreach ( $models as $slug ) {
// phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
$model_outcomes[ $slug ] = array( 'probed' => 0, 'errors' => 0 );
}
$counts = array(
'probed' => 0,
'skipped_budget' => 0,
'skipped_error' => 0,
'truncated' => false,
'run_id' => $run_id,
);
$budget_hit = false;
foreach ( $peptides as $peptide ) {
foreach ( $intents as $intent_tpl ) {
$query = str_replace( '{peptide}', $peptide['label'], $intent_tpl );
foreach ( $models as $model ) {
$this->probe_one( $run_id, $peptide, $model, $intent_tpl, $query, $config_ver, $counts, $model_outcomes, $budget_hit );
}
}
}
if ( $budget_hit && $counts['probed'] > 0 ) {
$counts['truncated'] = true;
}
update_option( 'prv_last_run_truncated', $counts['truncated'] ? 1 : 0 );
if ( $counts['truncated'] ) {
update_option( 'prv_last_run_truncated_at', current_time( 'mysql', true ) );
}
update_option( 'prv_last_run_at', current_time( 'mysql', true ) );
update_option( 'prv_last_run_counts', $counts );
PRV_Model_Registry::update_health( $run_id, $model_outcomes );
$this->settle_api_key_status( $counts, $model_outcomes );
return $counts;
}
/**
* Execute one probe combination: can_afford → probe → persist → capture (P0 order).
*
* @param string $run_id Run UUID.
* @param array<string,string> $peptide Peptide config.
* @param string $model Model slug.
* @param string $intent_tpl Intent template.
* @param string $query Rendered query string.
* @param int $config_ver Config version.
* @param array<string,mixed> &$counts Counts accumulator.
* @param array<string,array> &$model_outcomes Per-model outcome map.
* @param bool &$budget_hit Budget exhaustion flag.
* @return void
*/
private function probe_one(
string $run_id,
array $peptide,
string $model,
string $intent_tpl,
string $query,
int $config_ver,
array &$counts,
array &$model_outcomes,
bool &$budget_hit
): void {
$provider = $this->resolve_provider( $model );
if ( null === $provider || ! $provider->is_configured() ) {
++$counts['skipped_error'];
if ( isset( $model_outcomes[ $model ] ) ) {
++$model_outcomes[ $model ]['errors'];
}
return;
}
$estimated = $this->get_estimated_cost( $model );
if ( ! $this->ledger->can_afford( $estimated ) ) {
++$counts['skipped_budget'];
$budget_hit = true;
return;
}
$start_ms = (int) round( microtime( true ) * 1000 );
try {
$result = $provider->probe( $query );
$latency_ms = (int) round( microtime( true ) * 1000 ) - $start_ms;
$http_status = 200;
} catch ( \Exception $e ) {
$latency_ms = (int) round( microtime( true ) * 1000 ) - $start_ms;
$http_status = $this->extract_http_status( $e );
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( sprintf( 'PRV: probe failed [%s / %s]: %s', $model, $peptide['slug'], $e->getMessage() ) );
++$counts['skipped_error'];
if ( isset( $model_outcomes[ $model ] ) ) {
++$model_outcomes[ $model ]['errors'];
}
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine, Universal.WhiteSpace.CommaSpacing.TooMuchSpaceAfter
$cid = $this->capture->write_meta(
array(
'visibility_row' => null, 'run_id' => $run_id,
'peptide_slug' => $peptide['slug'], 'model' => $model,
'intent_label' => $intent_tpl, 'tokens_in' => null,
'tokens_out' => null, 'cost_usd' => 0.0,
'latency_ms' => $latency_ms, 'cited' => null,
'http_status' => $http_status, 'config_version' => $config_ver,
)
); // phpcs:enable
if ( $cid > 0 ) {
try {
$this->capture->write_io( $cid, $query, '' );
} catch ( \Throwable $e2 ) {
// Best-effort: I/O capture failure must not propagate (P0-4).
unset( $e2 );
}
}
return;
}
$row_id = $this->persist_result( $run_id, $peptide, $model, $intent_tpl, $result, $config_ver );
if ( $row_id > 0 ) {
$this->ledger->update_row_cost( $row_id, $result->get_cost_usd() );
}
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine, Universal.WhiteSpace.CommaSpacing.TooMuchSpaceAfter
$cid = $this->capture->write_meta(
array(
'visibility_row' => $row_id > 0 ? $row_id : null, 'run_id' => $run_id,
'peptide_slug' => $peptide['slug'], 'model' => $model,
'intent_label' => $intent_tpl, 'tokens_in' => $result->get_tokens_in(),
'tokens_out' => $result->get_tokens_out(), 'cost_usd' => $result->get_cost_usd(),
'latency_ms' => $latency_ms, 'cited' => $result->is_cited() ? 1 : 0,
'http_status' => $http_status, 'config_version' => $config_ver,
)
); // phpcs:enable
if ( $cid > 0 ) {
try {
$this->capture->write_io( $cid, $query, $result->get_raw_excerpt() );
} catch ( \Throwable $e2 ) {
// Best-effort: I/O capture failure must not propagate (P0-4).
unset( $e2 );
}
}
++$counts['probed'];
if ( isset( $model_outcomes[ $model ] ) ) {
++$model_outcomes[ $model ]['probed'];
}
}
/**
* Persist one probe result to prv_ai_visibility.
*
* @param string $run_id Run UUID.
* @param array<string,string> $peptide Peptide config.
* @param string $model Model slug.
* @param string $intent_tpl Intent template.
* @param PRV_Probe_Result $result Probe result.
* @param int $config_ver Config version.
* @return int Row ID, or 0 on failure.
*/
private function persist_result( string $run_id, array $peptide, string $model, string $intent_tpl, PRV_Probe_Result $result, int $config_ver ): int {
global $wpdb;
$table = PRV_Table_Manager::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->insert(
$table,
array(
'run_id' => $run_id,
'captured_at' => current_time( 'mysql', true ),
'peptide_slug' => $peptide['slug'],
'peptide_label' => $peptide['label'],
'model' => $model,
'prompt_intent' => $intent_tpl,
'cited' => $result->is_cited() ? 1 : 0,
'our_position' => $result->get_our_position(),
'source_domains' => wp_json_encode( $result->get_source_domains() ),
'raw_excerpt' => $result->get_raw_excerpt(),
'cost_usd' => number_format( $result->get_cost_usd(), 8, '.', '' ),
'config_version' => $config_ver,
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d' )
);
return (int) $wpdb->insert_id;
}
/**
* Extract HTTP status from a gateway exception message.
*
* @param \Exception $e Caught exception.
* @return int HTTP status, or 0 if unknown.
*/
private function extract_http_status( \Exception $e ): int {
if ( preg_match( '/PR Vision gateway HTTP (\d+)/', $e->getMessage(), $m ) ) {
return (int) $m[1];
}
return 0;
}
/**
* Resolve provider instance for a model slug.
*
* @param string $model Model slug.
* @return PRV_Probe_Provider|null Provider, or null if not configured.
*/
private function resolve_provider( string $model ): ?PRV_Probe_Provider {
if ( 'perplexity/sonar' === $model ) {
return new PRV_Perplexity_Provider();
}
return new PRV_OpenRouter_Provider( $model );
}
/**
* Conservative per-call cost estimate for budget pre-check.
*
* @param string $model Model slug.
* @return float Estimated USD per call.
*/
private function get_estimated_cost( string $model ): float {
if ( 'perplexity/sonar' === $model ) {
return PRV_Perplexity_Provider::ESTIMATED_COST_PER_PROBE;
}
return PRV_OpenRouter_Provider::ESTIMATED_COST_PER_PROBE;
}
/**
* Write the API-key status option based on run outcomes.
*
* @param array<string,mixed> $counts Run counts.
* @param array<string,array{probed:int,errors:int}> $model_outcomes Model outcomes.
* @return void
*/
private function settle_api_key_status( array $counts, array $model_outcomes ): void {
if ( $counts['probed'] > 0 ) {
update_option( 'prv_api_key_status', 'ok' );
} elseif ( $counts['skipped_error'] > 0 && 0 === $counts['probed'] ) {
update_option( 'prv_api_key_status', 'failed' );
}
update_option( 'prv_api_key_last_check', current_time( 'mysql', true ) );
}
}

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Orchestrates a full probe run across peptides, intents, and models. * Orchestrates a full probe run: lock + delegate to PRV_Probe_Run_Executor.
* *
* @package PrVision * @package PrVision
*/ */
@@ -8,25 +8,24 @@
declare(strict_types=1); declare(strict_types=1);
/** /**
* Orchestrates a full probe run: peptides x intents x models. * Orchestrates a full probe run.
* *
* For each combination the runner: * Acquires the run-lock, generates a UUID, stamps the config version, then
* 1. Acquires the run-lock (fails fast if already locked). * delegates all peptide × intent × model iteration to PRV_Probe_Run_Executor.
* 2. Checks the monthly budget cap (aborts gracefully when hit). * Released from >300-line obligation by the executor split.
* 3. Dispatches to the correct provider (resolved from PRV_Model_Registry). *
* 4. Writes a row to the prv_ai_visibility table with config_version stamp. * Capture ordering (P0) is enforced in PRV_Probe_Run_Executor:
* 5. Settles the actual cost against the ledger row. * can_afford → probe → persist_result → update_row_cost → write_meta → capture_io
* 6. After the run, updates per-model health and releases the lock.
* *
* Who triggers: PRV_Cron::handle_cron_tick() and PRV_Settings_Page (Run now). * Who triggers: PRV_Cron::handle_cron_tick() and PRV_Settings_Page (Run now).
* Dependencies: PRV_Config, PRV_Cost_Ledger, PRV_Table_Manager, * Dependencies: PRV_Cost_Ledger, PRV_Capture_Writer, PRV_Run_Lock,
* PRV_Run_Lock, PRV_Model_Registry, PRV_Config_Version, all providers. * PRV_Config_Version, PRV_Probe_Run_Executor.
* *
* @see class-prv-cron.php -- Calls run() on the weekly schedule. * @see class-prv-probe-run-executor.php — Inner loop implementation.
* @see class-prv-cost-ledger.php -- Budget cap enforcement. * @see class-prv-cost-ledger.php — Budget cap enforcement.
* @see class-prv-run-lock.php -- Concurrency guard. * @see class-prv-run-lock.php — Concurrency guard.
* @see class-prv-model-registry.php -- Per-model health updates. * @see class-prv-capture-writer.php — Per-call metadata + I/O writer.
* @see ARCHITECTURE.md -- Section Probe run flow. * @see ARCHITECTURE.md — Section Probe run flow v0.3.0.
* @package PrVision * @package PrVision
*/ */
class PRV_Probe_Runner { class PRV_Probe_Runner {
@@ -38,27 +37,33 @@ class PRV_Probe_Runner {
*/ */
private PRV_Cost_Ledger $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. * Constructor.
* *
* @param PRV_Cost_Ledger|null $ledger Injected for testing; auto-created otherwise. * @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 ) { public function __construct( ?PRV_Cost_Ledger $ledger = null, ?PRV_Capture_Writer $capture = null ) {
$this->ledger = $ledger ?? new PRV_Cost_Ledger(); $this->ledger = $ledger ?? new PRV_Cost_Ledger();
$this->capture = $capture ?? new PRV_Capture_Writer();
} }
/** /**
* Execute the full probe run. * Execute the full probe run.
* *
* Acquires a run-lock so the cron and "Run now" cannot collide. * Acquires a run-lock so the cron and "Run now" cannot collide.
* Iterates peptides x prompt intents x models. Aborts at the budget cap * Returns early with sentinel -1 in skipped_error when the lock is busy.
* without throwing -- partial results are kept. Each successful probe is
* persisted immediately so a mid-run failure doesn't lose data.
* *
* Side effects: HTTP calls to LLM APIs, database writes, option writes (health). * Side effects: HTTP calls, database writes, option writes.
* *
* @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string} * @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string}
* Summary counts plus truncation flag for the admin display.
*/ */
public function run(): array { public function run(): array {
$counts = array( $counts = array(
@@ -84,199 +89,21 @@ class PRV_Probe_Runner {
} }
/** /**
* Core run logic (called inside the lock). * 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} * @return array{probed: int, skipped_budget: int, skipped_error: int, truncated: bool, run_id: string}
*/ */
private function execute_run(): array { private function execute_run(): array {
$run_id = $this->generate_run_id(); $run_id = $this->generate_run_id();
$config_ver = PRV_Config_Version::get_active_version(); $config_ver = PRV_Config_Version::get_active_version();
$peptides = PRV_Config::get_peptides();
$intents = PRV_Config::get_prompt_intents();
$models = PRV_Config::get_models();
$model_outcomes = array(); // Per-slug: {probed, errors}.
foreach ( $models as $slug ) { $executor = new PRV_Probe_Run_Executor( $this->ledger, $this->capture );
$model_outcomes[ $slug ] = array( return $executor->execute( $run_id, $config_ver );
'probed' => 0,
'errors' => 0,
);
}
$counts = array(
'probed' => 0,
'skipped_budget' => 0,
'skipped_error' => 0,
'truncated' => false,
'run_id' => $run_id,
);
$budget_hit = false;
foreach ( $peptides as $peptide ) {
foreach ( $intents as $intent_tpl ) {
$query = str_replace( '{peptide}', $peptide['label'], $intent_tpl );
foreach ( $models as $model ) {
$provider = $this->resolve_provider( $model );
if ( null === $provider || ! $provider->is_configured() ) {
++$counts['skipped_error'];
if ( isset( $model_outcomes[ $model ] ) ) {
++$model_outcomes[ $model ]['errors'];
}
continue;
}
// Budget pre-check.
$estimated = $this->get_estimated_cost( $model );
if ( ! $this->ledger->can_afford( $estimated ) ) {
++$counts['skipped_budget'];
$budget_hit = true;
continue;
}
try {
$result = $provider->probe( $query );
} catch ( \Exception $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( sprintf( 'PRV: probe failed [%s / %s]: %s', $model, $peptide['slug'], $e->getMessage() ) );
++$counts['skipped_error'];
if ( isset( $model_outcomes[ $model ] ) ) {
++$model_outcomes[ $model ]['errors'];
}
continue;
}
$row_id = $this->persist_result( $run_id, $peptide, $model, $intent_tpl, $result, $config_ver );
if ( $row_id > 0 ) {
$this->ledger->update_row_cost( $row_id, $result->get_cost_usd() );
}
++$counts['probed'];
if ( isset( $model_outcomes[ $model ] ) ) {
++$model_outcomes[ $model ]['probed'];
}
}
}
}
if ( $budget_hit && $counts['probed'] > 0 ) {
$counts['truncated'] = true;
}
// Record truncation state for dashboard display.
if ( $counts['truncated'] ) {
update_option( 'prv_last_run_truncated', 1 );
update_option( 'prv_last_run_truncated_at', current_time( 'mysql', true ) );
} else {
update_option( 'prv_last_run_truncated', 0 );
}
// Persist run summary for dashboard.
update_option( 'prv_last_run_at', current_time( 'mysql', true ) );
update_option( 'prv_last_run_counts', $counts );
// Update per-model run-health.
PRV_Model_Registry::update_health( $run_id, $model_outcomes );
// Update API-key status (derive from whether probes succeeded).
$this->update_api_key_status( $counts, $model_outcomes );
return $counts;
}
/**
* Persist a single probe result to the database with config_version stamp.
*
* @param string $run_id UUID for this run.
* @param array<string,string> $peptide Peptide config with slug and label.
* @param string $model Model identifier.
* @param string $intent_tpl Raw intent template string.
* @param PRV_Probe_Result $result Probe result.
* @param int $config_ver Config version at run time.
*
* @return int Inserted row ID, or 0 on failure.
*/
private function persist_result(
string $run_id,
array $peptide,
string $model,
string $intent_tpl,
PRV_Probe_Result $result,
int $config_ver
): int {
global $wpdb;
$table = PRV_Table_Manager::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->insert(
$table,
array(
'run_id' => $run_id,
'captured_at' => current_time( 'mysql', true ),
'peptide_slug' => $peptide['slug'],
'peptide_label' => $peptide['label'],
'model' => $model,
'prompt_intent' => $intent_tpl,
'cited' => $result->is_cited() ? 1 : 0,
'our_position' => $result->get_our_position(),
'source_domains' => wp_json_encode( $result->get_source_domains() ),
'raw_excerpt' => $result->get_raw_excerpt(),
'cost_usd' => number_format( $result->get_cost_usd(), 8, '.', '' ),
'config_version' => $config_ver,
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d' )
);
return (int) $wpdb->insert_id;
}
/**
* Derive + persist the API-key status from run outcomes.
*
* @param array<string, mixed> $counts Run counts.
* @param array<string, array{probed: int, errors: int}> $model_outcomes Model outcomes.
*
* @return void
*/
private function update_api_key_status( array $counts, array $model_outcomes ): void {
if ( $counts['probed'] > 0 ) {
update_option( 'prv_api_key_status', 'ok' );
} elseif ( $counts['skipped_error'] > 0 && 0 === $counts['probed'] ) {
update_option( 'prv_api_key_status', 'failed' );
}
update_option( 'prv_api_key_last_check', current_time( 'mysql', true ) );
}
/**
* Instantiate the correct provider class for a model string.
*
* @param string $model OpenRouter model identifier.
*
* @return PRV_Probe_Provider|null Null when the model is unrecognised.
*/
private function resolve_provider( string $model ): ?PRV_Probe_Provider {
if ( 'perplexity/sonar' === $model ) {
return new PRV_Perplexity_Provider();
}
// All other models route through the generic OpenRouter provider.
return new PRV_OpenRouter_Provider( $model );
}
/**
* Conservative per-call cost estimate used for the budget pre-check.
*
* @param string $model Model identifier.
*
* @return float Estimated USD.
*/
private function get_estimated_cost( string $model ): float {
if ( 'perplexity/sonar' === $model ) {
return PRV_Perplexity_Provider::ESTIMATED_COST_PER_PROBE;
}
return PRV_OpenRouter_Provider::ESTIMATED_COST_PER_PROBE;
} }
/** /**

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 );
}
}

View File

@@ -76,6 +76,11 @@ class PRV_Settings_Controller {
? max( 0.01, (float) sanitize_text_field( wp_unslash( (string) $_POST['prv_monthly_budget_usd'] ) ) ) ? max( 0.01, (float) sanitize_text_field( wp_unslash( (string) $_POST['prv_monthly_budget_usd'] ) ) )
: PRV_DEFAULT_MONTHLY_BUDGET_USD; : PRV_DEFAULT_MONTHLY_BUDGET_USD;
update_option( 'prv_monthly_budget_usd', $budget ); update_option( 'prv_monthly_budget_usd', $budget );
// v0.3.0: I/O retention days.
$retention_days = isset( $_POST['prv_io_retention_days'] )
? max( 1, absint( sanitize_text_field( wp_unslash( (string) $_POST['prv_io_retention_days'] ) ) ) )
: PRV_IO_RETENTION_DEFAULT_DAYS;
update_option( 'prv_io_retention_days', $retention_days );
$cadence_raw = isset( $_POST['prv_cadence'] ) $cadence_raw = isset( $_POST['prv_cadence'] )
? sanitize_text_field( wp_unslash( (string) $_POST['prv_cadence'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['prv_cadence'] ) )

View File

@@ -38,6 +38,7 @@ class PRV_Settings_Renderer {
$this->render_styles(); $this->render_styles();
echo '<div class="prv-settings-wrap">'; echo '<div class="prv-settings-wrap">';
echo '<h1 class="prv-page-title">' . esc_html__( 'PR Vision — Settings', 'pr-vision' ) . '</h1>'; echo '<h1 class="prv-page-title">' . esc_html__( 'PR Vision — Settings', 'pr-vision' ) . '</h1>';
( new PRV_Costs_Renderer() )->render_subnav( 'settings' );
$this->render_notices(); $this->render_notices();
echo '<div class="prv-card"><h2>' . esc_html__( 'Model Manager', 'pr-vision' ) . '</h2>'; echo '<div class="prv-card"><h2>' . esc_html__( 'Model Manager', 'pr-vision' ) . '</h2>';
( new PRV_Model_Manager_Table() )->render(); ( new PRV_Model_Manager_Table() )->render();
@@ -202,6 +203,11 @@ class PRV_Settings_Renderer {
$budget = PRV_Config::get_monthly_budget_usd(); $budget = PRV_Config::get_monthly_budget_usd();
echo '<div class="prv-field"><label class="prv-label" for="prv_monthly_budget_usd">' . esc_html__( 'Monthly budget cap (USD)', 'pr-vision' ) . '</label>'; echo '<div class="prv-field"><label class="prv-label" for="prv_monthly_budget_usd">' . esc_html__( 'Monthly budget cap (USD)', 'pr-vision' ) . '</label>';
echo '<input class="prv-input" style="max-width:160px;" type="number" min="0.01" step="0.01" id="prv_monthly_budget_usd" name="prv_monthly_budget_usd" value="' . esc_attr( number_format( $budget, 2, '.', '' ) ) . '" data-scoring-relevant="1"></div>'; echo '<input class="prv-input" style="max-width:160px;" type="number" min="0.01" step="0.01" id="prv_monthly_budget_usd" name="prv_monthly_budget_usd" value="' . esc_attr( number_format( $budget, 2, '.', '' ) ) . '" data-scoring-relevant="1"></div>';
// v0.3.0: I/O retention days.
$retention = PRV_Config::get_io_retention_days();
echo '<div class="prv-field"><label class="prv-label" for="prv_io_retention_days">' . esc_html__( 'Call I/O retention (days)', 'pr-vision' ) . '</label>';
echo '<input class="prv-input" style="max-width:120px;" type="number" min="1" step="1" id="prv_io_retention_days" name="prv_io_retention_days" value="' . esc_attr( (string) $retention ) . '">';
echo '<span class="prv-field-hint">' . esc_html__( 'Prompt + response text is auto-deleted after this many days. Cost and metadata are kept indefinitely.', 'pr-vision' ) . '</span></div>';
} }
/** /**

View File

@@ -20,6 +20,8 @@ declare(strict_types=1);
* *
* @see class-prv-model-registry.php -- Migration v2 implementation. * @see class-prv-model-registry.php -- Migration v2 implementation.
* @see class-prv-config-version.php -- Scoring-config versioning. * @see class-prv-config-version.php -- Scoring-config versioning.
* @see class-prv-call-meta-table.php -- [v0.3.0] Per-call metadata table.
* @see class-prv-call-io-table.php -- [v0.3.0] Per-call I/O table (prunable).
* @see class-prv-plugin.php -- Calls PRV_Upgrader::run(). * @see class-prv-plugin.php -- Calls PRV_Upgrader::run().
* @package PrVision * @package PrVision
*/ */
@@ -34,7 +36,12 @@ class PRV_Upgrader {
*/ */
public static function run(): void { public static function run(): void {
PRV_Table_Manager::create_table(); PRV_Table_Manager::create_table();
// v0.3.0: per-call cost/metadata + I/O tables.
PRV_Call_Meta_Table::create_table();
PRV_Call_Io_Table::create_table();
PRV_Model_Registry::run_migration_v2(); PRV_Model_Registry::run_migration_v2();
PRV_Config_Version::maybe_seed_initial_version(); PRV_Config_Version::maybe_seed_initial_version();
// v0.3.0: seed retention default if not set.
add_option( 'prv_io_retention_days', PRV_IO_RETENTION_DEFAULT_DAYS );
} }
} }

View File

@@ -33,6 +33,13 @@ declare(strict_types=1);
*/ */
class PRV_Gateway_Client { class PRV_Gateway_Client {
/**
* HTTP status code from the most recent request (set on permanent error).
*
* @var int
*/
private int $last_http_status = 200;
/** /**
* Base URL for the Cloudflare AI Gateway (without trailing slash). * Base URL for the Cloudflare AI Gateway (without trailing slash).
*/ */
@@ -59,6 +66,9 @@ class PRV_Gateway_Client {
* @return array<string, mixed> Decoded JSON response body. * @return array<string, mixed> Decoded JSON response body.
* *
* @throws \RuntimeException On permanent HTTP failure or parse error. * @throws \RuntimeException On permanent HTTP failure or parse error.
* Exception message carries HTTP status code only;
* the response body is never included (P0: prevents
* secret-key leakage via 401 error responses).
*/ */
public function post_to_gateway( string $provider_slug, array $body, string $api_key, string $endpoint = '/chat/completions' ): array { public function post_to_gateway( string $provider_slug, array $body, string $api_key, string $endpoint = '/chat/completions' ): array {
$base_url = $this->build_base_url( $provider_slug ); $base_url = $this->build_base_url( $provider_slug );
@@ -110,8 +120,9 @@ class PRV_Gateway_Client {
} }
if ( $status >= 400 ) { if ( $status >= 400 ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message, not HTML output $this->last_http_status = $status;
throw new \RuntimeException( sprintf( 'PR Vision gateway HTTP %d: %s', $status, substr( $raw_body, 0, 300 ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message, not HTML output; body intentionally omitted (P0: prevents key-leak via 401 body)
throw new \RuntimeException( sprintf( 'PR Vision gateway HTTP %d', $status ) );
} }
$data = json_decode( $raw_body, true ); $data = json_decode( $raw_body, true );
@@ -129,6 +140,18 @@ class PRV_Gateway_Client {
throw new \RuntimeException( sprintf( 'PR Vision: gateway failed after %d attempts. Last: %s', PRV_MAX_RETRIES, $last_error ) ); throw new \RuntimeException( sprintf( 'PR Vision: gateway failed after %d attempts. Last: %s', PRV_MAX_RETRIES, $last_error ) );
} }
/**
* Get the HTTP status code from the last failed request.
*
* Allows callers to record the HTTP status code in error records without
* parsing the exception message. Returns 200 when no failure has occurred.
*
* @return int HTTP status code.
*/
public function get_last_http_status(): int {
return $this->last_http_status;
}
/** /**
* Build the API base URL (gateway or direct fallback). * Build the API base URL (gateway or direct fallback).
* *

View File

@@ -164,7 +164,11 @@ class PRV_OpenRouter_Provider implements PRV_Probe_Provider {
$cost_usd = (float) $data['usage']['total_tokens'] * 0.000002; $cost_usd = (float) $data['usage']['total_tokens'] * 0.000002;
} }
return new PRV_Probe_Result( $raw_excerpt, $domains, $cited, $our_position, $cost_usd ); // Token counts for per-call attribution.
$tokens_in = isset( $data['usage']['prompt_tokens'] ) ? (int) $data['usage']['prompt_tokens'] : null;
$tokens_out = isset( $data['usage']['completion_tokens'] ) ? (int) $data['usage']['completion_tokens'] : null;
return new PRV_Probe_Result( $raw_excerpt, $domains, $cited, $our_position, $cost_usd, $tokens_in, $tokens_out );
} }
/** /**

View File

@@ -133,7 +133,11 @@ class PRV_Perplexity_Provider implements PRV_Probe_Provider {
$cost_usd = (float) $data['usage']['total_tokens'] * 0.000001; $cost_usd = (float) $data['usage']['total_tokens'] * 0.000001;
} }
return new PRV_Probe_Result( $raw_excerpt, $domains, $cited, $our_position, $cost_usd ); // Token counts for per-call attribution.
$tokens_in = isset( $data['usage']['prompt_tokens'] ) ? (int) $data['usage']['prompt_tokens'] : null;
$tokens_out = isset( $data['usage']['completion_tokens'] ) ? (int) $data['usage']['completion_tokens'] : null;
return new PRV_Probe_Result( $raw_excerpt, $domains, $cited, $our_position, $cost_usd, $tokens_in, $tokens_out );
} }
/** /**

View File

@@ -3,7 +3,7 @@
* Plugin Name: PR Vision * Plugin Name: PR Vision
* Plugin URI: https://peptiderepo.com * Plugin URI: https://peptiderepo.com
* Description: Weekly server-side LLM probes for AI-visibility (GEO) tracking. Records whether peptiderepo.com is cited by LLMs across core peptides, stores time-series, and renders an admin dashboard + settings UI. * Description: Weekly server-side LLM probes for AI-visibility (GEO) tracking. Records whether peptiderepo.com is cited by LLMs across core peptides, stores time-series, and renders an admin dashboard + settings UI.
* Version: 0.2.3 * Version: 0.3.0
* Author: peptiderepo * Author: peptiderepo
* Author URI: https://peptiderepo.com * Author URI: https://peptiderepo.com
* License: GPL-2.0-or-later * License: GPL-2.0-or-later
@@ -24,11 +24,11 @@ if ( ! defined( 'ABSPATH' ) ) {
/* ── Constants ────────────────────────────────────────────────────────── */ /* ── Constants ────────────────────────────────────────────────────────── */
define( 'PRV_VERSION', '0.2.3' ); define( 'PRV_VERSION', '0.3.0' );
define( 'PRV_PLUGIN_FILE', __FILE__ ); define( 'PRV_PLUGIN_FILE', __FILE__ );
define( 'PRV_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define( 'PRV_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PRV_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'PRV_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'PRV_SCHEMA_VERSION', 2 ); define( 'PRV_SCHEMA_VERSION', 3 );
/** Maximum HTTP retries for LLM API calls. @var int */ /** Maximum HTTP retries for LLM API calls. @var int */
define( 'PRV_MAX_RETRIES', 3 ); define( 'PRV_MAX_RETRIES', 3 );
@@ -47,6 +47,12 @@ define( 'PRV_CRON_HOOK', 'prv_weekly_probe' );
/** The site we are tracking citations for. @var string */ /** The site we are tracking citations for. @var string */
define( 'PRV_TARGET_DOMAIN', 'peptiderepo.com' ); define( 'PRV_TARGET_DOMAIN', 'peptiderepo.com' );
/** Default I/O retention window in days. @var int */
define( 'PRV_IO_RETENTION_DEFAULT_DAYS', 90 );
/** WP-Cron hook name for the daily prune run. @var string */
define( 'PRV_DAILY_PRUNE_HOOK', 'prv_daily_prune' );
/* ── Autoloader ───────────────────────────────────────────────────────── */ /* ── Autoloader ───────────────────────────────────────────────────────── */

View File

@@ -27,41 +27,51 @@ if ( ! defined( 'ARRAY_N' ) ) {
define( 'ARRAY_N', 'ARRAY_N' ); define( 'ARRAY_N', 'ARRAY_N' );
} }
define( 'PRV_VERSION', '0.2.3' ); define( 'PRV_VERSION', '0.3.0' );
define( 'PRV_PLUGIN_FILE', __DIR__ . '/../pr-vision.php' ); define( 'PRV_PLUGIN_FILE', __DIR__ . '/../pr-vision.php' );
define( 'PRV_PLUGIN_DIR', realpath( __DIR__ . '/..' ) . '/' ); define( 'PRV_PLUGIN_DIR', realpath( __DIR__ . '/..' ) . '/' );
define( 'PRV_PLUGIN_URL', 'http://example.test/wp-content/plugins/pr-vision/' ); define( 'PRV_PLUGIN_URL', 'http://example.test/wp-content/plugins/pr-vision/' );
define( 'PRV_SCHEMA_VERSION', 2 ); define( 'PRV_SCHEMA_VERSION', 3 );
define( 'PRV_MAX_RETRIES', 3 ); define( 'PRV_MAX_RETRIES', 3 );
define( 'PRV_API_TIMEOUT_SECONDS', 60 ); define( 'PRV_API_TIMEOUT_SECONDS', 60 );
define( 'PRV_RETRY_BASE_DELAY_SECONDS', 2 ); define( 'PRV_RETRY_BASE_DELAY_SECONDS', 2 );
define( 'PRV_DEFAULT_MONTHLY_BUDGET_USD', 5.0 ); define( 'PRV_DEFAULT_MONTHLY_BUDGET_USD', 5.0 );
define( 'PRV_CRON_HOOK', 'prv_weekly_probe' ); define( 'PRV_CRON_HOOK', 'prv_weekly_probe' );
define( 'PRV_TARGET_DOMAIN', 'peptiderepo.com' ); define( 'PRV_TARGET_DOMAIN', 'peptiderepo.com' );
define( 'PRV_IO_RETENTION_DEFAULT_DAYS', 90 );
define( 'PRV_DAILY_PRUNE_HOOK', 'prv_daily_prune' );
/* ── Global test state ─────────────────────────────────────────────────── */ /* ── Global test state ─────────────────────────────────────────────────── */
$GLOBALS['prv_test_state'] = [ $GLOBALS['prv_test_state'] = [
'options' => [], 'options' => [],
'transients' => [], 'transients' => [],
'actions' => [], 'actions' => [],
'wpdb_insert_id' => 1, 'wpdb_insert_id' => 1,
'wpdb_results' => [], 'wpdb_results' => [],
'wpdb_var' => null, 'wpdb_var' => null,
'cron_events' => [], 'wpdb_row' => null,
'remote_posts' => [], 'cron_events' => [],
'remote_posts' => [],
'wpdb_call_meta_rows' => [],
'wpdb_call_io_rows' => [],
'wpdb_dropped_tables' => [],
]; ];
function prv_test_reset(): void { function prv_test_reset(): void {
$GLOBALS['prv_test_state'] = [ $GLOBALS['prv_test_state'] = [
'options' => [], 'options' => [],
'transients' => [], 'transients' => [],
'actions' => [], 'actions' => [],
'wpdb_insert_id' => 1, 'wpdb_insert_id' => 1,
'wpdb_results' => [], 'wpdb_results' => [],
'wpdb_var' => null, 'wpdb_var' => null,
'cron_events' => [], 'wpdb_row' => null,
'remote_posts' => [], 'cron_events' => [],
'remote_posts' => [],
'wpdb_call_meta_rows' => [],
'wpdb_call_io_rows' => [],
'wpdb_dropped_tables' => [],
]; ];
PRV_Collector_Registry::reset_for_testing(); PRV_Collector_Registry::reset_for_testing();
} }
@@ -182,6 +192,7 @@ function wp_create_nonce( string $action ): string { return 'test_nonce'; }
function submit_button( string $text, string $type = 'primary', string $name = 'submit', bool $wrap = true ): void {} function submit_button( string $text, string $type = 'primary', string $name = 'submit', bool $wrap = true ): void {}
function add_query_arg( array $args, string $url ): string { return $url . '?' . http_build_query( $args ); } function add_query_arg( array $args, string $url ): string { return $url . '?' . http_build_query( $args ); }
function admin_url( string $path ): string { return 'http://example.test/wp-admin/' . ltrim( $path, '/' ); } function admin_url( string $path ): string { return 'http://example.test/wp-admin/' . ltrim( $path, '/' ); }
function plugin_dir_path( string $file ): string { return dirname( $file ) . '/'; }
function wp_safe_redirect( string $url ): void {} function wp_safe_redirect( string $url ): void {}
function wp_send_json_success( $data = null ): void { throw new \RuntimeException( 'json_success:' . json_encode( $data ) ); } function wp_send_json_success( $data = null ): void { throw new \RuntimeException( 'json_success:' . json_encode( $data ) ); }
function wp_send_json_error( $data = null ): void { throw new \RuntimeException( 'json_error:' . json_encode( $data ) ); } function wp_send_json_error( $data = null ): void { throw new \RuntimeException( 'json_error:' . json_encode( $data ) ); }
@@ -230,15 +241,28 @@ class stdClass_wpdb {
public function prepare( string $query, ...$args ): string { public function prepare( string $query, ...$args ): string {
return vsprintf( str_replace( '%s', "'%s'", $query ), array_map( 'addslashes', $args ) ); return vsprintf( str_replace( '%s', "'%s'", $query ), array_map( 'addslashes', $args ) );
} }
public function query( string $sql ): bool { return true; } public function query( string $sql ): bool {
if ( preg_match( '/DROP TABLE.*?(prv_\w+)/i', $sql, $m ) ) {
$GLOBALS['prv_test_state']['wpdb_dropped_tables'][] = $m[1];
}
return true;
}
public function insert( string $table, array $data, array $format = [] ): int { public function insert( string $table, array $data, array $format = [] ): int {
$this->insert_id = $GLOBALS['prv_test_state']['wpdb_insert_id']++; $this->insert_id = $GLOBALS['prv_test_state']['wpdb_insert_id']++;
// Track call_meta and call_io rows for test assertions.
if ( str_contains( $table, 'prv_call_meta' ) ) {
$GLOBALS['prv_test_state']['wpdb_call_meta_rows'][] = $data;
}
if ( str_contains( $table, 'prv_call_io' ) ) {
$GLOBALS['prv_test_state']['wpdb_call_io_rows'][] = $data;
}
return 1; return 1;
} }
public function update( string $table, array $data, array $where, array $df = [], array $wf = [] ): int { return 1; } public function update( string $table, array $data, array $where, array $df = [], array $wf = [] ): int { return 1; }
public function get_results( string $query, string $output = 'OBJECT' ): array { public function get_results( string $query, string $output = 'OBJECT' ): array {
return is_array( $GLOBALS['prv_test_state']['wpdb_results'] ) ? $GLOBALS['prv_test_state']['wpdb_results'] : []; return is_array( $GLOBALS['prv_test_state']['wpdb_results'] ) ? $GLOBALS['prv_test_state']['wpdb_results'] : [];
} }
public function get_row( string $query, string $output = 'OBJECT' ): mixed { return $GLOBALS['prv_test_state']['wpdb_row'] ?? null; }
public function get_var( string $query ): mixed { return $GLOBALS['prv_test_state']['wpdb_var']; } public function get_var( string $query ): mixed { return $GLOBALS['prv_test_state']['wpdb_var']; }
} }

View File

@@ -0,0 +1,142 @@
<?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' );
prv_assert_equals( 0, $meta_rows[0]['io_captured'], 'write_meta sets io_captured=0 initially' );
// ── 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 — direct writer): 401 error record does NOT store key/Bearer ──
// Direct-writer path: simulate the probe-runner error path parameters.
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];
prv_assert_equals( 401, $err_meta['http_status'], 'Error record stores http_status 401' );
$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 (P0 SECURITY — full executor path): sentinel key must not leak ─
// Inject a known sentinel as the resolved API key; drive a mocked 401 error
// path end-to-end through PRV_Probe_Run_Executor (via PRV_Probe_Runner).
// Assert the sentinel, "Bearer", and "Authorization" appear in NO stored row.
prv_test_reset();
if ( ! defined( 'PRV_OPENROUTER_API_KEY' ) ) {
define( 'PRV_OPENROUTER_API_KEY', 'sk-or-TEST-LEAK-CANARY' );
}
$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' => 'openai/gpt-4o', 'enabled' => true ),
);
$GLOBALS['prv_test_state']['options']['prv_active_config_version'] = 3;
$GLOBALS['prv_test_state']['wpdb_var'] = '0.0'; // No prior spend.
// Queue a 401 response for wp_remote_post.
$GLOBALS['prv_test_state']['remote_posts'][] = array(
'response' => array( 'code' => 401 ),
'body' => '{"error":{"message":"Invalid API key","code":401}}',
);
$sentinel_capture = new PRV_Capture_Writer();
$sentinel_ledger = new PRV_Cost_Ledger();
$sentinel_runner = new PRV_Probe_Runner( $sentinel_ledger, $sentinel_capture );
try {
$sentinel_counts = $sentinel_runner->run();
} catch ( \Throwable $ex ) {
$sentinel_counts = array( 'skipped_error' => -99 );
}
// The executor must have recorded the 401 error call (skipped_error >= 1).
prv_assert( isset( $sentinel_counts['skipped_error'] ) && $sentinel_counts['skipped_error'] >= 1, 'Executor recorded a skipped_error for the 401 path' );
// Now verify NO stored row (meta or io) contains the sentinel key or auth material.
$all_meta = $GLOBALS['prv_test_state']['wpdb_call_meta_rows'];
$all_io = $GLOBALS['prv_test_state']['wpdb_call_io_rows'];
$all_json = wp_json_encode( $all_meta ) . wp_json_encode( $all_io );
prv_assert( false === strpos( $all_json, 'sk-or-TEST-LEAK-CANARY' ), 'Sentinel key does not appear in any stored row (executor path)' );
prv_assert( false === strpos( $all_json, 'Bearer' ), 'Bearer does not appear in any stored row (executor path)' );
prv_assert( false === strpos( $all_json, 'Authorization' ), 'Authorization does not appear in any stored row (executor path)' );
prv_assert( count( $all_meta ) >= 1, 'Executor path wrote at least one call_meta row on 401' );
if ( ! empty( $all_meta ) ) {
prv_assert_equals( 401, $all_meta[0]['http_status'], 'Executor 401 path stores http_status=401 in call_meta' );
}
// ── Test 5: update_meta_cost writes back the cost ────────────────────────
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() );

View 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 get_row mock (used by get_mtd_summary) and get_var (used by PRV_Cost_Ledger).
$GLOBALS['prv_test_state']['wpdb_row'] = array( 'total_cost' => '1.23456789', 'total_calls' => '5' );
$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: PRV_Cost_Ledger reads get_var; confirm it agrees with the seeded mock.
$ledger = new PRV_Cost_Ledger();
$mtd_via_ledger = $ledger->get_month_to_date_usd();
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() );

View File

@@ -12,6 +12,10 @@
* prv_last_run_truncated_at -- all purged by the prv_ wildcard DELETE below. * prv_last_run_truncated_at -- all purged by the prv_ wildcard DELETE below.
* *
* v0.2.3 addition: prv_provider_key_enc (encrypted API key) -- also purged * v0.2.3 addition: prv_provider_key_enc (encrypted API key) -- also purged
*
* v0.3.0 additions: prv_call_meta + prv_call_io tables (both DROPped);
* prv_io_retention_days option (purged by wildcard DELETE);
* prv_daily_prune cron hook (cleared in step 4).
* by the prv_ wildcard DELETE below. No plaintext is stored. * by the prv_ wildcard DELETE below. No plaintext is stored.
* *
* @see ARCHITECTURE.md -- Section Uninstall specification. * @see ARCHITECTURE.md -- Section Uninstall specification.
@@ -26,12 +30,26 @@ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
global $wpdb; global $wpdb;
/* ── 1. Drop the custom table ─────────────────────────────────────── */ /* ── 0. Bootstrap minimum classes for DROP calls ─────────────────── */
require_once plugin_dir_path( __FILE__ ) . 'includes/core/class-prv-autoloader.php';
PRV_Autoloader::register();
/* ── 1. Drop the custom tables (including v0.3.0 audit tables) ────── */
$table = $wpdb->prefix . 'prv_ai_visibility'; $table = $wpdb->prefix . 'prv_ai_visibility';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching // 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}" ); $wpdb->query( "DROP TABLE IF EXISTS {$table}" );
// v0.3.0: per-call audit tables (drop io before meta to respect FK intent).
$io_table = $wpdb->prefix . 'prv_call_io';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DROP TABLE IF EXISTS {$io_table}" );
$meta_table = $wpdb->prefix . 'prv_call_meta';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DROP TABLE IF EXISTS {$meta_table}" );
/* ── 2. Delete all prv_ prefixed options (v0.1, v0.2, v0.2.3) ───── */ /* ── 2. Delete all prv_ prefixed options (v0.1, v0.2, v0.2.3) ───── */
// Covers prv_provider_key_enc (v0.2.3) along with all prior option keys. // Covers prv_provider_key_enc (v0.2.3) along with all prior option keys.
@@ -50,3 +68,5 @@ $wpdb->query(
/* ── 4. Clear any remaining scheduled cron events ─────────────────── */ /* ── 4. Clear any remaining scheduled cron events ─────────────────── */
wp_clear_scheduled_hook( 'prv_weekly_probe' ); wp_clear_scheduled_hook( 'prv_weekly_probe' );
// v0.3.0: daily prune cron.
wp_clear_scheduled_hook( 'prv_daily_prune' );