} * `changed` is false on an FR-015 no-op (nothing written, no * action fired); `affected_slugs` maps each flipped slug to * its was/now effective-exposure pair. */ public static function apply( int $server_id, string $new_policy ): array { // Read the current policy off the server row. $server_rows = Query::instance()->query( array( 'id' => $server_id, 'number' => 1, ) ); $server_row = ! empty( $server_rows ) ? $server_rows[0] : null; $old_policy = ( $server_row && ! empty( $server_row->abilities_default_policy ) ) ? (string) $server_row->abilities_default_policy : 'per-ability'; // FR-015 no-op suppression — skip the DELETE, skip the action fire. if ( $old_policy === $new_policy ) { return array( 'changed' => false, 'old_policy' => $old_policy, 'affected_slugs' => array(), ); } // Snapshot pre-change effective state per registered ability. $snapshot = array(); if ( function_exists( 'wp_get_abilities' ) ) { foreach ( \wp_get_abilities() as $ability ) { $slug = (string) $ability->get_name(); $meta = $ability->get_meta(); $snapshot[ $slug ] = ExposureResolver::resolve_effective( $server_id, $slug, is_array( $meta ) ? $meta : array() ); } } // Persist the policy change on the server row. Query::instance()->update_item( $server_id, array( 'abilities_default_policy' => $new_policy ) ); // Clear every override row for this server (last-write-wins per spec // Clarifications Q5). Bulk delete + cache-group flush. MCPServerAbilityQuery::instance()->delete_items_for_server( $server_id ); // Bust the resolver caches so post-change reads are fresh. ExposureResolver::reset_request_cache(); // Compute post-change effective state + build the per-slug diff. $affected_slugs = array(); if ( function_exists( 'wp_get_abilities' ) ) { foreach ( \wp_get_abilities() as $ability ) { $slug = (string) $ability->get_name(); $meta = $ability->get_meta(); $now = ExposureResolver::resolve_effective( $server_id, $slug, is_array( $meta ) ? $meta : array() ); $was = isset( $snapshot[ $slug ] ) ? (bool) $snapshot[ $slug ] : false; if ( $was !== $now ) { $affected_slugs[ $slug ] = array( 'was' => (bool) $was, 'now' => (bool) $now, ); } } } /** * Fires after a server-level default ability policy transition (F082). * * Suppressed on no-op transitions (FR-015). Payload's `$affected_slugs` * is a map keyed by ability slug — `[ 'slug/name' => [ 'was' => bool, * 'now' => bool ] ]` — carrying the per-slug transition (spec * Clarifications Q2). Audit-log subscribers get the full diff without * having to re-derive it. * * @since 0.1.0 (F082) @experimental May change without notice before 1.0.0 * * @param int $server_id MCP server id. * @param string $old_policy Previous policy value ('per-ability' | 'expose' | 'hide'). * @param string $new_policy New policy value. * @param array $affected_slugs Map of slug => [ 'was' => bool, 'now' => bool ]. * @param int $user_id User who initiated the transition. */ do_action( 'acrossai_mcp_server_policy_changed', $server_id, $old_policy, $new_policy, $affected_slugs, get_current_user_id() ); return array( 'changed' => true, 'old_policy' => $old_policy, 'affected_slugs' => $affected_slugs, ); } }