Compare commits

..

9 Commits

Author SHA1 Message Date
peptiderepo
d42cd63103 fix: add services/opik/ to autoloader directory scan
The WordPress runtime autoloader (class-autoloader.php) was not scanning
services/opik/ — only services/. All four Opik classes live in that
subdirectory and would fatal on first use in production (class not found).

Composer's classmap covers all of includes/ recursively, so PHPUnit tests
passed. The runtime autoloader only scans explicit directory entries.

Adds 'services/opik/' to the scan list. No other services/ subdirs exist.
2026-04-24 03:43:49 +00:00
peptiderepo
89670be661 fix: PHPCS spacing in backoff delay expression 2026-04-23 22:34:06 +07:00
peptiderepo
0d8dc67f1c fix: backoff delays 2s/4s/8s to match CHANGELOG (was 1s/2s/4s) 2026-04-23 22:31:13 +07:00
peptiderepo
130f9f719c style: phpcbf-style WPCS fixes (multi-line arrays, one arg per line, function-keyword spacing) 2026-04-23 15:03:28 +00:00
peptiderepo
238074f257 ci: temp switch PHPCS to full report, drop dead oneshot workflow 2026-04-23 14:59:34 +00:00
peptiderepo
b4ac2fae61 ci: add one-shot phpcbf workflow (will be removed after Wave 1 lands) 2026-04-23 14:57:37 +00:00
peptiderepo
fe8c242a64 Opik Wave 1 Commit 3: Version bump, docs, and hook registration
Version & Changelog:
- prautoblogger.php: 0.11.0 → 0.12.0
- Add Opik PHP constants (PRAUTOBLOGGER_OPIK_API_KEY, WORKSPACE, URL_OVERRIDE)
  with fallback empty defaults so wp-config.php can override
- CHANGELOG.md: v0.12.0 section describing Wave 1 scope, feature flag,
  observability benefits, cost model, and Wave 2 preview

Documentation:
- ARCHITECTURE.md: New 'Opik LLM Observability (Optional)' section with
  feature-flag design, credential isolation, span lifecycle, call sites,
  cost/performance implications, and code structure overview
- CONVENTIONS.md: Step-by-step guide 'Instrumenting a New LLM Call Site'
  with code examples, key rules (no full prompts, async dispatch, separate
  cost tracking), and minimal instrumentation pattern

Hook Registration:
- class-prautoblogger.php: Register 'prautoblogger_opik_dispatch' cron action
  * Instantiates Opik_Client, Opik_Span_Queue, Opik_Dispatcher
  * Checks credentials and calls dispatcher->dispatch()
  * Updates 'prautoblogger_opik_last_dispatch' option for admin status display
- Instantiate Opik_Settings in admin_init for admin UI registration

All changes preserve ≤ 300-line files.

Part of Wave 1 (observability skeleton).
Target: v0.12.0
2026-04-23 14:51:51 +00:00
peptiderepo
ca5fdfacc4 Opik Wave 1 Commit 2: Integration wraps at LLM call sites
Add Opik span instrumentation to core generation pipeline:

- class-article-worker.php: Initialize trace context at start
  * Trace initialized via init_trace()
  * Spans queued after generation completes
  * Async dispatch scheduled via wp_schedule_single_event
  * Feature flag check before any Opik operations

- class-content-generator.php: Span per LLM call
  * Single-pass generation: 1 span
  * Multi-step (outline/draft/polish): 3 spans
  * Each span captures model, provider, token usage
  * Minimal docblock to keep file ≤ 300 LOC

- class-chief-editor.php: Editorial review span
  * Wraps LLM-based content review
  * Captures JSON response parsing
  * Records quality/SEO score generation

All files ≤ 300 LOC. Spans capture usage data but not full
prompts (per security policy). Feature-flag default OFF.

Note: Image_Prompt_Builder already at 314 LOC (over limit);
skipped to avoid further expansion. Can be instrumented in
future without modifying existing code (via filters).

Part of Wave 1 integration.
Target: v0.12.0
2026-04-23 14:50:20 +00:00
peptiderepo
ff6ad5eba1 Opik Wave 1 Commit 1: Client, Trace Context, and test infrastructure
Add the core Opik observability classes:

- class-opik-client.php: HTTP REST client for Comet Opik
  * POST traces and spans with exponential backoff (3 retries)
  * Batch endpoints support (max 100 items per batch)
  * Auth headers: Authorization + Comet-Workspace
  * Never logs secrets; errors logged with context

- class-opik-trace-context.php: Per-request trace singleton
  * Initialized at article generation start
  * Manages span stack: start_span(), end_span()
  * Finalizes complete trace with all child spans
  * Teardown clears context for next request

- class-opik-settings.php: WordPress Settings API integration
  * Toggle: 'Enable Opik tracing' (default off)
  * Text field: 'Opik project name' (default 'prautoblogger')
  * Checkbox: 'Capture full prompt text' (opt-in)
  * Read-only status: API key configured, last dispatch, queue depth
  * Never renders API key in admin HTML

- Unit tests: test-opik-client.php, test-opik-trace-context.php
  * Mock HTTP via Brain\Monkey
  * Verify batch size limits, auth headers, retry logic
  * Verify UUID generation, span stacking, context teardown

All files ≤ 300 LOC per 300-line rule.
Feature flag default OFF (PRAUTOBLOGGER_OPIK_ENABLED option).

Part of Wave 1 (observability skeleton).
Target: v0.12.0
2026-04-23 14:48:27 +00:00
4 changed files with 5 additions and 53 deletions

View File

@@ -5,15 +5,6 @@ All notable changes to PRAutoBlogger will be documented in this file.
Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project uses [Semantic Versioning](https://semver.org/).
## [0.12.1] — 2026-04-23
### Fixed
- **Opik UUID v7 bug** — `PRAutoBlogger_Opik_Trace_Context` was generating trace and
span IDs with `wp_generate_uuid4()` (UUID v4). Opik's REST API requires UUID v7 and
rejects v4 with HTTP 400 "Trace id must be a version 7 UUID". Added a private static
`generate_uuid7()` method using `microtime()` + `random_bytes()` and replaced both
call sites. All trace and span submissions now succeed.
## [0.12.0] — 2026-04-23
### Added

View File

@@ -30,6 +30,7 @@ class PRAutoBlogger_Autoloader {
'models/',
'frontend/',
'services/',
'services/opik/',
'ajax/',
);

View File

@@ -57,7 +57,7 @@ class PRAutoBlogger_Opik_Trace_Context {
* @return string The trace ID (UUID).
*/
public function init_trace(): string {
$this->trace_id = self::generate_uuid7();
$this->trace_id = wp_generate_uuid4();
$this->trace_start = microtime( true );
return $this->trace_id;
}
@@ -86,7 +86,7 @@ class PRAutoBlogger_Opik_Trace_Context {
* @return string Generated span ID (UUID).
*/
public function start_span( array $span_data ): string {
$span_id = self::generate_uuid7();
$span_id = wp_generate_uuid4();
$span = array(
'id' => $span_id,
@@ -193,44 +193,4 @@ class PRAutoBlogger_Opik_Trace_Context {
public static function teardown(): void {
self::$instance = null;
}
/**
* Generate a UUID version 7 (time-ordered, random).
*
* Opik requires v7 UUIDs for trace and span IDs. WordPress only provides
* wp_generate_uuid4() so we implement v7 here.
*
* UUID v7 structure (128 bits):
* - Bits 0-47 : Unix timestamp in milliseconds (big-endian)
* - Bits 48-51 : Version nibble (0x7)
* - Bits 52-63 : rand_a (12 random bits)
* - Bits 64-65 : Variant (0b10)
* - Bits 66-127: rand_b (62 random bits)
*
* @return string UUID v7 in standard 8-4-4-4-12 hex format.
*/
private static function generate_uuid7(): string {
$ms = (int) ( microtime( true ) * 1000 );
$ts_hex = str_pad( dechex( $ms ), 12, '0', STR_PAD_LEFT ); // 48-bit ts -> 12 hex
$rand = bin2hex( random_bytes( 10 ) ); // 80 random bits -> 20 hex
// Group 3: version nibble '7' + 12 random bits (3 hex chars).
$g3 = '7' . substr( $rand, 0, 3 );
// Group 4: variant byte (top 2 bits = 10) + 2 random hex chars.
$var_byte = ( hexdec( substr( $rand, 4, 2 ) ) & 0x3f ) | 0x80;
$g4 = sprintf( '%02x', $var_byte ) . substr( $rand, 6, 2 );
// Group 5: 12 remaining random hex chars.
$g5 = substr( $rand, 8, 12 );
return sprintf(
'%s-%s-%s-%s-%s',
substr( $ts_hex, 0, 8 ), // time_high (32 bits)
substr( $ts_hex, 8, 4 ), // time_low (16 bits)
$g3,
$g4,
$g5
);
}
}

View File

@@ -9,7 +9,7 @@
* Plugin Name: PRAutoBlogger
* Plugin URI: https://peptiderepo.com/prautoblogger
* Description: Monitors social media for trending topics, generates SEO-friendly blog posts using AI, and publishes them on a daily schedule with full cost tracking and self-improvement metrics.
* Version: 0.12.1
* Version: 0.12.0
* Requires at least: 6.0
* Requires PHP: 7.4
* Author: PeptideRepo
@@ -35,7 +35,7 @@ if ( ! defined( 'ABSPATH' ) ) {
| Defined here so every file in the plugin can reference paths, versions,
| and limits without magic strings.
*/
define( 'PRAUTOBLOGGER_VERSION', '0.12.1' );
define( 'PRAUTOBLOGGER_VERSION', '0.12.0' );
define( 'PRAUTOBLOGGER_DB_VERSION', '1.1.0' );
define( 'PRAUTOBLOGGER_PLUGIN_FILE', __FILE__ );
define( 'PRAUTOBLOGGER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );