# Changelog

All notable changes to WP Agentic Writer are documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added
- **REST API documentation**: Added `docs/architecture/REST_API_ENDPOINTS.md` with complete endpoint reference
- **Architecture refactor**: Extracted REST controllers from `class-gutenberg-sidebar.php` into dedicated controller classes (`Controller_Chat`, `Controller_Cost`, `Controller_Models`, `Controller_Session`, `Controller_Conversation`, `Controller_Planning`, `Controller_Config`, `Controller_Writing`, `Controller_Refinement`, `Controller_Image`)
- **Helper utilities**: Extracted ~24 pure helper methods into `WP_Agentic_Writer_Sidebar_Helpers` class (`class-sidebar-helpers.php`): JSON extraction, plan normalization, block manipulation, refinement, SEO patterns, memory helpers
- **Rate limiting**: Added `WPAW_Rate_Limiter` class for REST endpoint protection (30/min chat, 20/min search, 10/min content fetch)
- **Session locking**: Added multi-tab safety via session locks (acquire, refresh, release)

### Changed
- **Post meta optimization**: Bulk fetch post meta in sidebar initialization for reduced database queries

### Fixed
- **Sidebar logger recursion**: Fixed wpawLog.error/log/warn calling themselves recursively (now call console.* directly)
- **Sidebar syntax**: Restored valid JavaScript syntax after debug logger conversion damaged string literals
- **Session ID security**: Session ID generation now uses `wp_generate_uuid4()` for cryptographically secure uniqueness (requires DB migration for existing sessions)
- **Font loading**: Fixed broken font loading in `agentic-sidebar.css` by correcting `.wpaw-sidebar-container` selector and `@import` paths
- **Dead stylesheet**: Removed `settings-v2.css` from enqueue queue (was loaded but not used)
- **Cost tracker backward compatibility**: Added `record_usage()` method to match WP AI Client wrapper contract
- **Cost attribution**: Added `record_usage_full()` method for accurate model/provider attribution in cost records
- **Cost table self-heal**: Added `SHOW TABLES LIKE` guard before `DESCRIBE` to handle missing table scenario
- **Conversation table versioning**: Fixed independent version tracking for conversations table vs. main plugin version
- **Clear context behavior**: Context service now clears all active sessions for a post when sessionId is not provided
- **Deactivation cleanup**: Now clears `wpaw_cleanup_old_sessions` scheduled hook
- **Legacy chat history migration**: `migrate_legacy_chat_history()` now deletes legacy meta and writes migration marker; `get_context()` performs migrate-on-read for posts with unmigrated legacy data
- **Legacy chat history deprecated**: `update_post_chat_history()` no longer writes to post meta; endpoint returns deprecation notice

### Added
- **Post-scoped authorization (complete sweep)**: Added `edit_post` capability checks to ALL post-scoped REST endpoints:
  - `handle_clear_context`, `handle_revise_plan`, `handle_block_refine`, `handle_refine_from_chat`
  - `handle_seo_audit`, `handle_suggest_keywords`, `handle_suggest_improvements`
  - Plus all previously fixed endpoints (chat, generate-plan, execute-article, reformat-blocks, regenerate-block, meta, image ops)
- **Frontend debug logging**: Added centralized debug logging utility (`wpawLog`) that respects the `wpAgenticWriter.debug` flag
- **Schema sync**: Cost tracking table `CREATE` statement now includes all columns (`provider`, `session_id`, `status`) with runtime migration fallback
- **Uninstall cleanup**: Consolidated uninstall hooks and removed redundant code paths

### Changed
- **record_usage() deprecated**: Marked `record_usage()` as deprecated in docblock; use `record_usage_full()` for accurate provider attribution
- **Session ID security**: Changed session ID generation from `wp_hash()` to `wp_generate_uuid4()` for better uniqueness (requires DB migration)

### Removed
- **Legacy chat history**: Post meta `_wpaw_chat_history` is deleted after successful migration (replaced by `_wpaw_chat_history_migrated` marker)
- **Deprecated files**: Moved `CREATE_TABLE.sql`, empty `languages/`, and other unused files to `legacy/` directory
- **Unused directories**: Removed `tests/`, `admin/`, `deprecated/`, `stitch/` directories (already moved to `legacy/`)

## [0.1.3] - 2025-05-24

### Added
- **Provider transparency**: Provider selection now returns structured result with `selected_provider`, `actual_provider`, and `fallback_used` flags
- **Cost tracking enhancements**: Extended cost tracking to capture provider name, session ID, and request status
- **Authorization hardening**: Added `edit_post` capability checks to REST API endpoints for writing state and conversations

### Fixed
- **OpenRouter cache conflict**: Split single transient into separate keys for model IDs and model objects
- **Provider contract mismatch**: All provider calls now properly unwrap the `WPAW_Provider_Selection_Result` object
- **Streaming chat variables**: Added proper initialization for `accumulated_content`, `chunks_emitted`, `total_cost` before closure use
- **Post meta bloat**: Stopped writing `_wpaw_chat_history` to post meta (conversations table is now the source of truth)

## [0.1.2] - 2025-05-17

### Fixed
- Clarification quiz flow improvements
- Block refinement hybrid implementation
- Language detection for multilingual content

## [0.1.1] - 2025-05-10

### Added
- Context optimization with summarization
- Intent detection for writing modes
- Block-based article structure with outline panel

### Fixed
- Writing state persistence
- Session management improvements

## [0.1.0] - 2025-05-01

### Added
- Initial release of WP Agentic Writer
- Plan-first AI writing workflow: Scribble → Research → Plan → Execute → Revise
- Integration with OpenRouter API
- Gutenberg sidebar for AI assistance
- Cost tracking for API usage
- Image generation support