Files
pr-vision/includes/core/class-prv-probe-result.php
peptiderepo 4eee9f5f64 PR Vision v0.3.0: per-call cost accountability + LLM call audit trail
P0 security:
- PRV_Gateway_Client: remove response body from RuntimeException (HTTP status only)
- PRV_Capture_Writer: allowlist-only write (prompt/response text, never headers/auth)
- Mandatory capture order: can_afford → probe → persist_result → update_row_cost → write_meta → capture_io
- capture_io last and wrapped in swallowing try/catch

New tables (SCHEMA_VERSION 3):
- prv_call_meta: cost/metadata per call, kept indefinitely
- prv_call_io: rendered prompt + raw response, pruned on retention window (default 90d)

New classes:
- PRV_Call_Meta_Table, PRV_Call_Io_Table: table schema + dbDelta
- PRV_Capture_Writer: metadata + I/O write; security allowlist enforced
- PRV_Prune_Cron: daily prv_daily_prune cron deletes aged prv_call_io rows only
- PRV_Cost_Rollup_Query: MTD summary, model breakdown, drill-down, projection
- PRV_Call_Log_Query: paginated + filtered call-log query + per-call I/O fetch
- PRV_Costs_Page + PRV_Costs_Renderer: Costs admin sub-page (cap progress, bento, model table, drill-down)
- PRV_Call_Log_Page + PRV_Call_Log_Table_Renderer: Call Log admin sub-page (filter bar, paginated table, WCAG AA pagination)
- PRV_Call_Drawer_Renderer: non-modal complementary drawer (5 states: Normal/Pruned/Legacy/Failed/Loading)
- PRV_Call_Drawer_Script: inline JS for drawer (300-line split from renderer)
- PRV_Call_Detail_Ajax: wp_ajax_prv_call_detail AJAX handler
- PRV_Admin_Page_Css: CSS provider (300-line split from PRV_Admin_Page)
- PRV_Probe_Run_Executor: inner probe loop (300-line split from PRV_Probe_Runner)

Updated:
- PRV_Probe_Result: tokens_in/tokens_out params + getters
- Both providers: report tokens_in/tokens_out from usage.prompt_tokens/completion_tokens
- PRV_Probe_Runner: slimmed to lock+UUID+delegation; injects PRV_Capture_Writer
- PRV_Config: get_io_retention_days(); PRV_IO_RETENTION_DEFAULT_DAYS constant
- PRV_Upgrader/Activator/Deactivator/Plugin: register new tables, cron, pages, AJAX
- Settings: retention days field + save in controller
- uninstall.php: DROP both new tables, clear prv_daily_prune cron
- Subnav 4-tab strip (Dashboard | Costs | Call Log | Settings) on all pages
- ARCHITECTURE.md, CHANGELOG.md, CONTEXT.md, CONVENTIONS.md updated

Tests:
- test-capture-writer.php: write_meta, write_io, P0 security (401 record has no key/Bearer/sk-or-)
- test-cost-audit.php: MTD reconciliation, projection, prune, forced capture_io exception, table drops

Agent-Session: peptiderepo-geomonitor-v0.3.0-cost-audit
2026-06-15 09:09:29 +00:00

112 lines
2.9 KiB
PHP

<?php
/**
* Immutable value object returned by every probe provider.
*
* @package PrVision
*/
declare(strict_types=1);
/**
* Value object: the result of a single LLM probe call.
*
* Returned by every PRV_Probe_Provider implementation. Immutable — all
* 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.
* Dependencies: None.
*
* @see interface-prv-probe-provider.php — Provider interface that returns this.
* @see class-prv-probe-runner.php — Consumes the result for storage.
* @see CONTEXT.md — "probe result", "cited", "our_position".
* @package PrVision
*/
class PRV_Probe_Result {
/**
* Constructor.
*
* @param string $raw_excerpt First ~500 chars of the LLM's response.
* @param string[] $source_domains Domains extracted from the provider's citations.
* @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 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(
private readonly string $raw_excerpt,
private readonly array $source_domains,
private readonly bool $cited,
private readonly ?int $our_position,
private readonly float $cost_usd,
private readonly ?int $tokens_in = null,
private readonly ?int $tokens_out = null
) {}
/**
* Get the raw LLM response excerpt.
*
* @return string
*/
public function get_raw_excerpt(): string {
return $this->raw_excerpt;
}
/**
* Get the extracted source domains.
*
* @return string[]
*/
public function get_source_domains(): array {
return $this->source_domains;
}
/**
* Whether peptiderepo.com was cited.
*
* @return bool
*/
public function is_cited(): bool {
return $this->cited;
}
/**
* Get 1-based position of peptiderepo.com in source list.
*
* @return int|null 1-based position or null when not cited.
*/
public function get_our_position(): ?int {
return $this->our_position;
}
/**
* Get the estimated cost of this probe call.
*
* @return float Estimated cost in USD.
*/
public function get_cost_usd(): float {
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;
}
}