Commit 33642aa144d for woocommerce
commit 33642aa144d724078d8d064a7ac04d63ca5a8e84
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date: Mon Nov 23 10:32:42 2026 -0300
Say that an ability ran when its output is invalid (#71113)
* Say that the ability ran when its output is invalid
WordPress validates the output after the execute callback ran, so the
callback may already have saved changes. The error did not say so, and AI
agents over MCP ran the same write again. MCP clients receive only the
message, so the message now says that the ability ran.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Drop the callback_executed flag from the invalid output error
Core plans to drop the same flag in its fix. Keep the error data as Core
returns it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep every output error when adding the retry hint
A wp_ability_validate_output filter can return a WP_Error with several
errors. Copy the error and change only its first message, so the other
codes, messages and data reach the caller.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Assert only the error data field that the test checks
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep the original invalid output message for read-only abilities
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Document the read-only annotation for abilities that do not write
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Add the invalid output hint in execute()
WordPress adds its own data to the invalid output error in execute(), after
validate_output() returns. Adding the hint there lets a later check see that data.
An error that the execute callback returns keeps its message.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Add the invalid output hint only after the callback returned output
An error with the ability_invalid_output code can also come from the execute
callback or from the wp_pre_execute_ability filter. The ability then did not
reach output validation, so those errors keep their message.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Leave the invalid output message to WordPress 7.2
WordPress 7.2 adds its own retry hint to the invalid output error, so the
WooCommerce hint would show twice there. The override now returns the result
unchanged on WordPress 7.2 and later.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Apply the retry hint only to the legacy invalid output error
WordPress 7.2.1 returns ability_output_schema_mismatch and
ability_output_rejected with its own retry hint, so the error code
check alone keeps the hint from showing twice. Drop the WordPress 7.2
version check and key the tests on the error code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Add the invalid output hint in validate_output()
WordPress calls validate_output() only after the execute callback returned
output, so the hint belongs there. It stays with the call that ran the callback
when an output validation filter runs the same ability again, and it removes
the shared flag and the execute() and do_execute() overrides.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/fix-ability-invalid-output-retry b/plugins/woocommerce/changelog/fix-ability-invalid-output-retry
new file mode 100644
index 00000000000..eacebdeca47
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-ability-invalid-output-retry
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Tell callers, such as AI agents, that an ability ran when its output is invalid, so they check before they retry.
diff --git a/plugins/woocommerce/src/Internal/Abilities/AGENTS.md b/plugins/woocommerce/src/Internal/Abilities/AGENTS.md
index fb9058527be..fc3c36da5df 100644
--- a/plugins/woocommerce/src/Internal/Abilities/AGENTS.md
+++ b/plugins/woocommerce/src/Internal/Abilities/AGENTS.md
@@ -24,3 +24,9 @@ Keep:
## Omit unavailable output fields
Core ability output types are never nullable. Omit an unavailable field instead of returning `null`. Declare date fields as `string` with the `date-time` format.
+
+## Mark abilities that do not write as read-only
+
+Set `'readonly' => true` in the `annotations` meta of every ability that does not write. `Ability::validate_output()` adds a hint to an invalid output error that the ability may already have saved changes. It leaves out the hint only when `readonly` is `true`.
+
+Do not use `destructive` for this. An ability with `destructive: false`, such as a create or an email send, still changes the store.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Ability.php b/plugins/woocommerce/src/Internal/Abilities/Ability.php
index e8df8648ef5..4fe8f2ee107 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Ability.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Ability.php
@@ -130,4 +130,38 @@ class Ability extends \WP_Ability {
}
return null;
}
+
+ /**
+ * Validate the output, and tell the caller that the ability ran when the output is invalid.
+ *
+ * WordPress calls this method only after the execute callback returned output,
+ * so the callback may already have saved changes. The message says so, because some
+ * callers, such as MCP clients, receive only the message. Read-only abilities keep the
+ * original message, because they saved nothing.
+ *
+ * WordPress 7.2.1 returns its own error codes with this hint, so this override leaves them
+ * unchanged. Remove this override when WooCommerce requires WordPress 7.2.1.
+ *
+ * @since 11.4.0
+ *
+ * @param mixed $output The output of the execute callback.
+ * @return true|\WP_Error True when the output is valid, or a WP_Error.
+ */
+ protected function validate_output( $output ) {
+ $result = parent::validate_output( $output );
+
+ if ( ! is_wp_error( $result ) || 'ability_invalid_output' !== $result->get_error_code() || true === ( $this->get_meta_item( 'annotations' )['readonly'] ?? null ) ) {
+ return $result;
+ }
+
+ $error = clone $result;
+
+ $error->errors['ability_invalid_output'][0] = sprintf(
+ /* translators: %s: error message. */
+ __( '%s The ability ran, so its changes may already be saved. Check the current state before you run it again.', 'woocommerce' ),
+ $error->get_error_message()
+ );
+
+ return $error;
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php
index e22d290789d..e18c6eb42a8 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php
@@ -21,6 +21,24 @@ class AbilityTest extends \WC_Unit_Test_Case {
*/
private $received = array();
+ /**
+ * Assert that an invalid output error has the retry hint once: from WooCommerce on the
+ * legacy error code, from WordPress on the codes that WordPress 7.2.1 adds.
+ *
+ * @param \WP_Error $error The invalid output error.
+ */
+ private function assert_retry_hint_once( \WP_Error $error ): void {
+ $message = $error->get_error_message();
+ $woo_hint = 'Check the current state before you run it again';
+ if ( 'ability_invalid_output' === $error->get_error_code() ) {
+ $this->assertStringContainsString( $woo_hint, $message );
+ } else {
+ $this->assertSame( 'ability_output_schema_mismatch', $error->get_error_code() );
+ $this->assertStringContainsString( 'Check the outcome before retrying', $message );
+ $this->assertStringNotContainsString( $woo_hint, $message );
+ }
+ }
+
/**
* Build a test ability with a boolean, an integer, a list of unique integers and a nested boolean field.
*
@@ -68,6 +86,183 @@ class AbilityTest extends \WC_Unit_Test_Case {
);
}
+ /**
+ * @testdox Should say that the callback ran when the output is invalid, so the caller checks the state before a retry.
+ */
+ public function test_invalid_output_says_the_callback_ran(): void {
+ $runs = 0;
+ $ability = new Ability(
+ 'test/invalid-output',
+ array(
+ 'label' => 'Invalid output',
+ 'description' => 'Returns output that does not match its schema.',
+ 'category' => 'woocommerce',
+ 'output_schema' => array( 'type' => 'object' ),
+ 'execute_callback' => function () use ( &$runs ) {
+ ++$runs;
+ return 'not an object';
+ },
+ 'permission_callback' => '__return_true',
+ )
+ );
+
+ $result = $ability->execute();
+
+ $this->assertSame( 1, $runs );
+ $this->assertWPError( $result );
+ $this->assert_retry_hint_once( $result );
+ }
+
+ /**
+ * @testdox Should keep the original message when a read-only ability returns invalid output.
+ */
+ public function test_invalid_output_of_a_read_only_ability_has_no_retry_hint(): void {
+ $ability = new Ability(
+ 'test/invalid-output-read-only',
+ array(
+ 'label' => 'Invalid output',
+ 'description' => 'Returns output that does not match its schema.',
+ 'category' => 'woocommerce',
+ 'output_schema' => array( 'type' => 'object' ),
+ 'execute_callback' => fn() => 'not an object',
+ 'permission_callback' => '__return_true',
+ 'meta' => array( 'annotations' => array( 'readonly' => true ) ),
+ )
+ );
+
+ $result = $ability->execute();
+
+ $this->assertWPError( $result );
+ $this->assertStringNotContainsString( 'may already be saved', $result->get_error_message() );
+ }
+
+ /**
+ * @testdox Should keep the message of an error that the execute callback returns.
+ */
+ public function test_callback_error_has_no_retry_hint(): void {
+ $ability = new Ability(
+ 'test/callback-error',
+ array(
+ 'label' => 'Callback error',
+ 'description' => 'Returns its own error.',
+ 'category' => 'woocommerce',
+ 'output_schema' => array( 'type' => 'object' ),
+ 'execute_callback' => fn() => new \WP_Error( 'ability_invalid_output', 'The callback failed.' ),
+ 'permission_callback' => '__return_true',
+ )
+ );
+
+ $result = $ability->execute();
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'The callback failed.', $result->get_error_message() );
+ }
+
+ /**
+ * @testdox Should keep the message of an error that the wp_pre_execute_ability filter returns.
+ */
+ public function test_short_circuit_error_has_no_retry_hint(): void {
+ if ( version_compare( get_bloginfo( 'version' ), '7.1-alpha', '<' ) ) {
+ $this->markTestSkipped( 'The wp_pre_execute_ability filter exists since WordPress 7.1.' );
+ }
+
+ $ability = new Ability(
+ 'test/short-circuit-error',
+ array(
+ 'label' => 'Short-circuit error',
+ 'description' => 'Never runs its callback.',
+ 'category' => 'woocommerce',
+ 'output_schema' => array( 'type' => 'object' ),
+ 'execute_callback' => fn() => array(),
+ 'permission_callback' => '__return_true',
+ )
+ );
+
+ $short_circuit = fn() => new \WP_Error( 'ability_invalid_output', 'The filter failed.' );
+ add_filter( 'wp_pre_execute_ability', $short_circuit );
+
+ $result = $ability->execute();
+
+ remove_filter( 'wp_pre_execute_ability', $short_circuit );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'The filter failed.', $result->get_error_message() );
+ }
+
+ /**
+ * @testdox Should keep the retry hint when the output validation filter runs the same ability again.
+ */
+ public function test_nested_run_in_the_output_filter_keeps_the_retry_hint(): void {
+ if ( version_compare( get_bloginfo( 'version' ), '7.1-alpha', '<' ) ) {
+ $this->markTestSkipped( 'The wp_ability_validate_output filter exists since WordPress 7.1.' );
+ }
+
+ $ability = new Ability(
+ 'test/nested-run',
+ array(
+ 'label' => 'Nested run',
+ 'description' => 'Returns output that does not match its schema.',
+ 'category' => 'woocommerce',
+ 'input_schema' => array( 'type' => 'integer' ),
+ 'output_schema' => array( 'type' => 'object' ),
+ 'execute_callback' => fn() => 'not an object',
+ 'permission_callback' => '__return_true',
+ )
+ );
+
+ $nested = function ( $is_valid ) use ( $ability, &$nested ) {
+ remove_filter( 'wp_ability_validate_output', $nested );
+ $ability->execute( 'not an integer' );
+ return $is_valid;
+ };
+ add_filter( 'wp_ability_validate_output', $nested );
+
+ $result = $ability->execute( 1 );
+
+ remove_filter( 'wp_ability_validate_output', $nested );
+
+ $this->assertWPError( $result );
+ $this->assert_retry_hint_once( $result );
+ }
+
+ /**
+ * @testdox Should keep every error from the output validation filter when it adds the retry hint.
+ */
+ public function test_invalid_output_keeps_every_error(): void {
+ if ( version_compare( get_bloginfo( 'version' ), '7.1-alpha', '<' ) ) {
+ $this->markTestSkipped( 'The wp_ability_validate_output filter exists since WordPress 7.1.' );
+ }
+
+ $filter = function ( $is_valid ) {
+ $is_valid->add( 'output_policy', 'The output breaks a policy.', array( 'field' => 'name' ) );
+ return $is_valid;
+ };
+ add_filter( 'wp_ability_validate_output', $filter );
+
+ $ability = new Ability(
+ 'test/invalid-output-many-errors',
+ array(
+ 'label' => 'Invalid output',
+ 'description' => 'Returns output that does not match its schema.',
+ 'category' => 'woocommerce',
+ 'output_schema' => array( 'type' => 'object' ),
+ 'execute_callback' => fn() => 'not an object',
+ 'permission_callback' => '__return_true',
+ )
+ );
+
+ $result = $ability->execute();
+ remove_filter( 'wp_ability_validate_output', $filter );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'output_policy', $result->get_error_codes()[1] ?? null );
+ $this->assert_retry_hint_once( $result );
+ $this->assertSame( 'The output breaks a policy.', $result->get_error_message( 'output_policy' ) );
+ $data = $result->get_error_data( 'output_policy' );
+ $this->assertArrayHasKey( 'field', $data );
+ $this->assertSame( 'name', $data['field'] );
+ }
+
/**
* Boolean flag values that the schema accepts, with the boolean each one means.
*