Skip to content

Commit e0ad687

Browse files
committed
Improve N+1 documents
1 parent 96b476a commit e0ad687

2 files changed

Lines changed: 52 additions & 62 deletions

File tree

CHANGELOG.md

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
6. Admin-only context → Needs Review
2828
7. True N+1 with multi-object iteration → Confirmed
2929

30-
- **WordPress cache-primed hooks dictionary** – Added `WP_CACHE_PRIMED_HOOKS` mapping WordPress hooks (e.g., `show_user_profile`, `edit_user_profile`, `add_meta_boxes`) to their object types, enabling automatic detection of contexts where meta is pre-cached.
30+
- **WordPress cache-primed hooks dictionary** – Added `WP_CACHE_PRIMED_HOOKS` mapping WordPress hooks (e.g., `show_user_profile`, `edit_user_profile`, `add_meta_boxes`) to their object types, enabling automatic detection of contexts where meta is pre-cached. The dictionary is located in `dist/bin/ai-triage.py`. Developers can update it by adding new hooks and testing changes to ensure no regressions.
3131

3232
#### Grep Pattern Detector Enhancements (`dist/bin/check-performance.sh`)
3333
- **`is_wp_cache_primed_view()` helper function** – New function that detects WordPress admin views where meta cache is pre-primed based on file path patterns. Matches files like `view-wwlc-custom-fields-on-user-admin.php` and downgrades severity to INFO since WordPress primes user meta cache on `user-edit.php` before hooks fire.
@@ -1500,6 +1500,7 @@ Reason: WordPress primes user meta cache on user-edit.php before hooks fire
15001500
- **Pattern Library:** Now 28 total patterns (was 27)
15011501
- **Impact:** Helps identify and fix severe thank-you page performance issues on WooCommerce sites
15021502
- **Test Status:** ✅ Tested against Binoid site - successfully detected Smart Coupons with `wc_get_coupon_id_by_code()` calls
1503+
15031504
- **Main Scanner Integration** - Both coupon patterns now integrated into `check-performance.sh`
15041505
- **`wc-coupon-in-thankyou`** - Integrated at line 4627-4695 (after WooCommerce N+1 check)
15051506
- **`wc-smart-coupons-thankyou-perf`** - Integrated at line 4699-4778 (after coupon-in-thankyou check)
@@ -2152,58 +2153,6 @@ Reason: WordPress primes user meta cache on user-edit.php before hooks fire
21522153
21532154
## [1.0.76] - 2026-01-02
21542155
2155-
### Changed
2156-
- Increased default fixture validation coverage to run eight proof-of-detection checks, covering AJAX, REST routes, admin capability callbacks, and direct database access patterns.
2157-
2158-
### Added
2159-
- Made fixture validation count configurable via `FIXTURE_COUNT` template option or the `FIXTURE_VALIDATION_COUNT` environment variable (default: 8).
2160-
2161-
## [1.0.75] - 2026-01-02
2162-
2163-
### Added
2164-
- **Context-Aware Admin Capability Detection** - Dramatically reduced false positives for admin callback functions
2165-
- Created `find_callback_capability_check()` helper function to search for callback definitions in same file
2166-
- Extracts callback names from multiple patterns: string callbacks, array callbacks, class array callbacks
2167-
- Checks callback function body (next 50 lines) for capability checks
2168-
- Recognizes direct capability checks: `current_user_can()`, `user_can()`, `is_super_admin()`
2169-
- Recognizes WordPress menu functions with capability parameters (`add_menu_page`, `add_submenu_page`, etc.)
2170-
- Handles static method definitions (`public static function`)
2171-
- **Impact:** Reduced admin capability check false positives from 15 to 3 (80% reduction)
2172-
2173-
### Changed
2174-
- **Enhanced Admin Functions Without Capability Checks** - Improved detection logic
2175-
- Updated immediate context check to recognize menu functions with capability parameters
2176-
- Added callback lookup for `add_action`, `add_filter`, and menu registration functions
2177-
- Supports multiple callback syntax patterns (string, array, class array)
2178-
- Checks both immediate context (next 10 lines) and callback function body (next 50 lines)
2179-
2180-
### Technical Details
2181-
- **Files Modified:** `dist/bin/check-performance.sh`
2182-
- Lines 1048-1099: New helper function `find_callback_capability_check()`
2183-
- Lines 2041-2072: Enhanced admin capability check with callback lookup
2184-
- **Patterns Detected:**
2185-
- `add_action('hook', 'callback')` - String callback
2186-
- `add_action('hook', [$this, 'callback'])` - Array callback
2187-
- `add_action('hook', [__CLASS__, 'callback'])` - Class array callback
2188-
- `add_action('hook', array($this, 'callback'))` - Legacy array syntax
2189-
- **Capability Enforcement Patterns:**
2190-
- Direct: `current_user_can('capability')`
2191-
- Menu functions: `add_submenu_page(..., 'manage_options', ...)`
2192-
2193-
### Testing
2194-
- **Test Case:** PTT-MKII plugin (30 files, 8,736 LOC)
2195-
- **Before:** 15 findings (many false positives)
2196-
- **After:** 3 findings (legitimate issues)
2197-
- **False Positives Eliminated:** 12 (80% reduction)
2198-
- **Remaining Findings:** Legitimate security issues (admin enqueue scripts without capability checks)
2199-
2200-
### Performance
2201-
- **Impact:** Minimal - callback lookup only performed when admin patterns detected
2202-
- **Scope:** Same-file lookup only (no cross-file analysis)
2203-
- **Efficiency:** Uses grep and sed for fast pattern matching
2204-
2205-
## [1.0.74] - 2026-01-02
2206-
22072156
### Changed
22082157
- **Terminology Update: "DRY Violations""Magic String Detector"** - Renamed feature for clarity
22092158
- "DRY Violation Detection" is now "Magic String Detector ('DRY')"
@@ -2374,11 +2323,10 @@ Tested against real WordPress plugin:
23742323
- `top_k_groups`: Maximum number of violations to report (default: 15)
23752324
- `report_format`: Template for violation messages
23762325
- `sort_by`: Sort order for violations (`"file_count_desc"` or `"total_count_desc"`)
2377-
2378-
- **Text Output** - Added Magic String Detection ("DRY") section
2379-
- New section displayed after all direct pattern checks
2380-
- Shows pattern title and violation status for each aggregated pattern
2381-
- Displays "✓ No violations" or "⚠ Found magic strings" for each pattern
2326+
- **Text Output** - Added Magic String Detection ("DRY") section
2327+
- New section displayed after all direct pattern checks
2328+
- Shows pattern title and violation status for each aggregated pattern
2329+
- Displays "✓ No violations" or "⚠ Found magic strings" for each pattern
23822330
23832331
### Technical Details
23842332
- **Aggregation Algorithm:**
@@ -2677,7 +2625,24 @@ Tested against real WordPress plugin:
26772625
- **jq Integration:** Queries JSON config file directly for each severity lookup
26782626
- **Performance:** Minimal overhead - jq queries are fast and cached by OS
26792627
- **Config Validation:** Validates JSON syntax and severity level values (CRITICAL, HIGH, MEDIUM, LOW)
2680-
- **Comment Field Support:** Underscore-prefixed fields (_comment, _note, etc.) are ignored during parsing
2628+
- **Comment Field Support:** Underscore-prefixed fields (_comment, _note, etc.) are ignored by parser
2629+
- **Purpose:** Users can document why they changed severity levels
2630+
- **Examples:** `_comment: "Upgraded per security audit"`, `_ticket: "JIRA-1234"`, `_date: "2025-12-31"`
2631+
- **Parser Behavior:** Underscore-prefixed fields are filtered out during parsing (won't affect functionality)
2632+
- **Use Cases:** Document incidents, reference tickets, track authors, add dates, explain decisions
2633+
2634+
- **Example Configuration File** - Created `/dist/config/severity-levels.example.json` showing how to customize severity levels
2635+
- **Purpose:** Demonstrates comment field usage and severity customization patterns
2636+
- **Examples:** Shows upgrading/downgrading severity levels with documentation
2637+
- **Comment Examples:** Demonstrates `_comment`, `_note`, `_reason`, `_ticket`, `_date`, `_author` fields
2638+
- **Workflow Guide:** Includes step-by-step instructions in `_notes` section
2639+
2640+
- **Configuration Documentation** - Created `/dist/config/README.md` with comprehensive usage guide
2641+
- **Quick Start:** Copy, edit, and use custom config files
2642+
- **Comment Field Reference:** Table of common underscore field names and their purposes
2643+
- **Field Reference:** Which fields are editable vs. read-only
2644+
- **Best Practices:** DOs and DON'Ts for config customization
2645+
- **Example Workflow:** Complete workflow from copy to CI/CD integration
26812646
26822647
## [1.0.60] - 2025-12-31
26832648
@@ -2690,7 +2655,7 @@ Tested against real WordPress plugin:
26902655
- **Location:** `dist/config/severity-levels.json`
26912656
- **Usage (Day 2):** Users will copy this file, edit `level` fields, and pass `--severity-config <path>` to customize severity rankings
26922657
- **Factory Defaults:** Each check includes `factory_default` field for reference (users can always see original values)
2693-
- **Self-Documenting:** Includes instructions, version, last_updated, and total_checks in metadata
2658+
- **Self-Documented:** Includes instructions, version, last_updated, and total_checks in metadata
26942659
- **Comment Field Support:** Any field starting with underscore (`_comment`, `_note`, `_reason`, `_ticket`, etc.) is ignored by parser
26952660
- **Purpose:** Users can document why they changed severity levels
26962661
- **Examples:** `_comment: "Upgraded per security audit"`, `_ticket: "JIRA-1234"`, `_date: "2025-12-31"`
@@ -2887,7 +2852,7 @@ Tested against real WordPress plugin:
28872852
- Disabled `example-caller.yml` triggers (changed to `workflow_dispatch` only)
28882853
- This template file was causing duplicate CI runs
28892854
- Now only runs manually, preventing automatic triggers
2890-
- Added clear warnings that it's a template/example file
2855+
- Added clear warnings that it's a template file
28912856
- Created `.github/workflows/README.md` documenting:
28922857
- Why we use a single workflow
28932858
- How to modify CI behavior without creating new files
@@ -3312,7 +3277,7 @@ Tested against real WordPress plugin:
33123277
- Updated `PROJECT/PROJECT.md` "Current State", "Proposed Approach", and "Three-layer system" sections so they reflect the currently implemented toolkit pieces (grep-based CLI, fixture suite, CI wiring, and the Neochrome WP Toolkit demo plugin) and reference GPT 5.1 feedback via `BACKLOG.md` instead of an inline TEMP dump.
33133278
33143279
- **Version metadata**
3315-
- Bumped the CLI script and backlog version markers to 1.0.43 to keep JSON output, terminal banners, and documentation in sync with this changelog.
3280+
- Bumped the CLI script and backlog version markers to 1.0.43 to keep JSON output, log headers, and terminal banners stay in sync with the changelog.
33163281
33173282
---
33183283

CONTRIBUTING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,31 @@ Every new check **must** have test fixtures:
124124
- **Functions**: One responsibility per function
125125
- **Variables**: Use descriptive names (`FINDING_COUNT` not `fc`)
126126

127+
### Updating the Cache-Primed Hooks Dictionary
128+
129+
The **cache-primed hooks dictionary** (`WP_CACHE_PRIMED_HOOKS`) is located in `dist/bin/ai-triage.py`. This dictionary maps WordPress hooks to object types and is critical for reducing false positives in N+1 detection.
130+
131+
#### Steps to Update:
132+
1. **Identify New Hooks**:
133+
- Look for WordPress hooks that pre-cache metadata for objects (e.g., `user`, `post`, `comment`).
134+
2. **Add to the Dictionary**:
135+
- Open `dist/bin/ai-triage.py` and add the new hook to the `WP_CACHE_PRIMED_HOOKS` dictionary.
136+
3. **Test Your Changes**:
137+
- Run the test suite to ensure no regressions.
138+
- Add new test cases if necessary.
139+
4. **Submit a Pull Request**:
140+
- Include a clear description of the added hooks and their purpose.
141+
142+
#### Example:
143+
```python
144+
WP_CACHE_PRIMED_HOOKS = {
145+
'show_user_profile': 'user',
146+
'edit_user_profile': 'user',
147+
'add_meta_boxes': 'post',
148+
# Add new hooks here
149+
}
150+
```
151+
127152
---
128153

129154
## 🧪 Testing

0 commit comments

Comments
 (0)