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 1 commit 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. |
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.