Skip to content

feat: add opt-in strict chunk_index validation#14

Merged
rbcorrales merged 7 commits into
mainfrom
feat/strict-chunk-index-validation
May 28, 2026
Merged

feat: add opt-in strict chunk_index validation#14
rbcorrales merged 7 commits into
mainfrom
feat/strict-chunk-index-validation

Conversation

@rbcorrales
Copy link
Copy Markdown
Member

@rbcorrales rbcorrales commented May 27, 2026

Summary

Adds an opt-in strict mode to REST::insert_embedding_row(). When the wpvdb_strict_chunk_index filter returns true, a null, non-numeric, or negative chunk_index is rejected with a WP_Error (chunk_index_invalid, status 400) instead of silently defaulting to 0.

Background

062a942 gave chunk_index a null default plus a WP_DEBUG warning, then falls back to 0. That silent fallback is exactly the class of regression that produced the original all-chunk_index=0 corpus bug. Strict mode lets a site (or CI) opt into failing loudly so a caller passing a bad chunk_index is caught immediately.

Default behavior is unchanged: the filter defaults to false, so the legacy warn-then-default-to-0 path runs byte-for-byte as before. The new branch mirrors the existing embedding_invalid validation right below it (same Logger::error + WP_Error shape with status => 400).

Testing

php -l clean. phpcs --standard=phpcs.xml.dist introduces zero new violations (15 errors / 18 warnings before and after, all pre-existing baseline in this file). Default path untouched; only the new filter-gated branch is added.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rbcorrales rbcorrales requested a review from Copilot May 27, 2026 22:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in “strict mode” validation path to REST::insert_embedding_row() to prevent silently defaulting invalid chunk_index values to 0, helping catch caller regressions early while preserving current default behavior.

Changes:

  • Introduces wpvdb_strict_chunk_index filter (default false) to gate strict validation.
  • When strict mode is enabled, rejects chunk_index values that are null, non-numeric, or negative with a WP_Error (chunk_index_invalid, status 400).
  • Keeps existing warn-and-default-to-0 behavior unchanged when strict mode is disabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

rbcorrales and others added 2 commits May 27, 2026 18:15
Co-Authored-By: Codex <codex@openai.com>
Co-Authored-By: Codex <codex@openai.com>
@rbcorrales
Copy link
Copy Markdown
Member Author

rbcorrales commented May 27, 2026

Follow-up updates pushed in c4c5a01 and 5cddfd3.

These tighten the strict chunk index contract, expand REST coverage for edge cases and strict-off behavior, and add the bootstrap shims needed for the unit suite to run cleanly.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread tests/bootstrap.php Outdated
Comment thread tests/bootstrap.php
rbcorrales and others added 2 commits May 27, 2026 18:43
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-Authored-By: Codex <codex@openai.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread tests/unit/RESTTest.php
Comment thread tests/unit/RESTTest.php
Comment thread tests/bootstrap.php
Co-Authored-By: Codex <codex@openai.com>
@rbcorrales
Copy link
Copy Markdown
Member Author

Independently re-validated the whole PR before merge:

Tests: composer test:unit is green: 135 tests, 530 assertions, OK (includes the strict on / off / invalid RESTTest cases).

Feature (insert_embedding_row, class-wpvdb-rest.php:925-950): valid chunk_index = is_int and >= 0, or a ^\d+$ string within PHP_INT_MAX. Strict + invalid returns WP_Error( 'chunk_index_invalid', ..., 400 ). The default (non-strict) path is unchanged: null still warns and falls back to 0. Confirmed it correctly rejects floats and out-of-range values, which the original looser is_numeric check would have accepted.

Copilot review points, all verified in code:

  • accepted_args = 0 honored: tests/bootstrap.php:83-84 clamps max( 0, ... ) and array_slice( ..., 0, $accepted_args ), so a zero-arg callback is invoked with no args.
  • per-test filter reset: tests/filter-reset-hook.php (Before/After test hooks) registered as a phpunit extension; no cross-test leakage.
  • RESTTest zero-arg closures: registered with add_filter( ..., 10, 0 ) (RESTTest.php:29,65).
  • remove_filter cleanup: tests/bootstrap.php:50-55 unsets empty priority and tag buckets so has_filter() reports correctly.

Pre-existing note (not introduced here): composer test (all suites) errors because the api testsuite in phpunit.xml points at a non-existent tests/api dir. That is also present on main; composer test:unit is the working path. Worth a separate cleanup.

Mergeable and CLEAN.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rbcorrales
Copy link
Copy Markdown
Member Author

rbcorrales commented May 28, 2026

Folded a phpunit.xml fix (3d9bc8) into this PR rather than spinning a separate one, since this PR already owns the test-infrastructure changes.

Removed the api testsuite, which pointed at a non-existent tests/api directory and made composer test abort with Test directory tests/api not found before any suite ran (this was also true on main). composer test now executes the suites: unit is green (135 tests, 530 assertions), and integration runs as before (it needs a real DB/WP, so it skips or fails in a no-DB local run; that is unchanged and out of scope here). composer test:unit remains the clean no-DB path.

@rbcorrales rbcorrales merged commit 8bcb803 into main May 28, 2026
1 check failed
@rbcorrales rbcorrales deleted the feat/strict-chunk-index-validation branch May 28, 2026 00:11
@rbcorrales rbcorrales review requested due to automatic review settings May 28, 2026 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants