AI: Return false from support checks when the wrapped builder throws - #12800
AI: Return false from support checks when the wrapped builder throws#12800jigneshbhavani wants to merge 3 commits into
Conversation
WP_AI_Client_Prompt_Builder::__call() only special-cased generating methods when converting a caught exception into the error state, so is_supported() and its siblings fell through to the fluent return and handed back the builder instance. That instance is truthy, so a failed support check read as supported. Return false from support check methods instead, matching what they already return when the builder is in a pre-existing error state.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
Fixes WP_AI_Client_Prompt_Builder::__call() so support-check methods (is_supported() / is_supported_for_*()) return false when the wrapped SDK PromptBuilder throws during the support check itself, matching existing behavior for pre-existing error states and prevented prompts.
Changes:
- Return
false(instead of the fluent builder instance) from__call()’s exception handler when the invoked method is a support-check method. - Add a PHPUnit regression test covering the thrown-during-support-check scenario and verifying the stored error is still returned by generating methods.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php |
Adjusts __call() catch behavior so support checks fail closed (false) when the wrapped builder throws. |
tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php |
Adds a regression test for support-check behavior when the wrapped builder throws, and asserts error-state propagation to generating methods. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The regression test relied on the document output modality throwing from the bundled client, which only reaches the bare is_supported() call. The seven is_supported_for_*() methods pass an explicit capability, so they never run the inference that throws and were not covered at all. Drive the throw from a mocked PromptBuilder instead and loop over all eight methods, and keep the document modality case as the route reported on the ticket, pinned to the error code so it fails if the bundled client stops throwing. See #65781.
gziolo
left a comment
There was a problem hiding this comment.
Thanks, this looks good. I applied the patch locally: the full ai-client group passes (266 tests), all 9 new tests fail with only the src/ change reverted, and phpcs and phpstan are clean.
The fix makes the catch block match the error state guard above it, which is the behavior the class already documents. Nice catch.
Since the patch settles what support checks return, three docblocks could be updated in the same commit:
-
Line 82:
@method bool|WP_Error is_supported(...)should be@method bool is_supported(...). No code path can return aWP_Errorhere, so this annotation is simply wrong. The other seven support checks are alreadybool. -
The
__call()docblock says the method "returns a WP_Error when a terminate method is called". It could also say that support check methods returnfalse. -
The class docblock says all later calls "return the same error state instance". Support checks return
falseinstead. This one is already true on trunk, so it is an old gap, not something you introduced.
Only the first is a real error. The other two are prose that drifted.
`is_supported()` is annotated `bool|WP_Error` but no path returns a `WP_Error`. Drop the `WP_Error`, and note in the `__call()` and class docblocks that support checks return false rather than the error state instance. See #65781.
|
All three applied in 69fc18c. On the first one, The
|
gziolo
left a comment
There was a problem hiding this comment.
Thank you for addressing feedback.
WP_AI_Client_Prompt_Builder::__call()catches an exception from the wrapped SDK builder, stores it as the error state, and then only special cases the generating methods:is_supported()and the sevenis_supported_for_*()methods are not generating methods, so they fall through to the fluent return and give back the builder instance. It is truthy, so a support check that failed reads as a success:Everywhere else in the class this is handled. The error state guard at the top of
__call()returnsfalsefor support checks, and so does the prevented prompt path. Only an exception raised during the support check itself takes the wrong branch.Reproducing it
ModalityEnum::document()is a documented factory, butPromptBuilder::inferCapabilityFromOutputModalities()has no branch for it and throws aRuntimeExceptionin its else. That call is outside the try blockisSupported()uses to swallowInvalidArgumentException, so it reaches the wrapper.Any throw from the wrapped builder during a support check behaves the same way. The document modality is just the shortest route to one.
Fix
Return
falsefor support check methods from the catch block, so they match what they already return when the builder was in an error state beforehand. The error is still stored, so a later generating call still gets theWP_Error, and the test covers that.Not a duplicate of #65505
#65505 is about the catch missing
Error. Its patch widenscatch ( Exception )tocatch ( Throwable )and leaves the return logic untouched, so a support check still comes back as the builder after it lands. The two changes are independent and touch different lines.Testing
test_support_check_methods_return_false_when_builder_throws()fails on trunk with:and passes with the patch. The full
ai-clientgroup is green, 258 tests.phpcspasses on both changed files.Trac ticket: https://core.trac.wordpress.org/ticket/65781
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Reading through the AI client for places where the code disagrees with its own documented behaviour, which is how this turned up, and drafting this description. I confirmed the throw path in the bundled client myself, wrote and ran the test, checked it fails without the patch, ran
phpcs, and I take responsibility for the change.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.