*/ private static array $cache = array(); /** * Per-request cache for effective (three-tier) resolution. * * @since 0.1.0 (F082) * @var array */ private static array $effective_cache = array(); /** * Per-request cache for server-level policy lookups. * * @since 0.1.0 (F082) * @var array */ private static array $policy_cache = array(); /** * Row-only exposure — the F030-critical semantics. See file docblock for * the invariant this method locks in. * * @since 0.1.0 (F017 — as `resolve()`, renamed 2026-09-05 per SEC-001) * * @param int $server_id MCP server id (references acrossai_mcp_servers.id). * @param string $ability_slug \WP_Ability::get_name() output. * @param array $meta The ability's `get_meta()` output — read to * extract `meta[mcp][public]` for the fallback. * @return bool True when the ability is effectively exposed on this server * via a row OR the meta fallback (server-level policy NOT * consulted — that's `resolve_effective()`'s job). */ public static function resolve_row_only( int $server_id, string $ability_slug, array $meta ): bool { $key = $server_id . ':' . $ability_slug; if ( array_key_exists( $key, self::$cache ) ) { return self::$cache[ $key ]; } $rows = Query::instance()->query( array( 'server_id' => $server_id, 'ability_slug' => $ability_slug, 'number' => 1, ) ); if ( ! empty( $rows ) ) { // B18 defense — $wpdb returns TINYINT as string; cast to bool. $result = (bool) $rows[0]->is_exposed; } else { $result = ! empty( $meta['mcp']['public'] ); } self::$cache[ $key ] = $result; return $result; } /** * Effective exposure — three-tier priority (F082). * * Priority order: * 1. Explicit row in `wp_acrossai_mcp_server_abilities` — highest. * `(bool) $row->is_exposed` wins over everything. * 2. Server-level policy on `MCPServer.abilities_default_policy`: * - 'expose' → true (operator wants every ability exposed by default) * - 'hide' → false (operator wants every ability hidden by default) * - other/'per-ability' → fall through to priority 3. * 3. `meta.mcp.public` fallback (same as row-only path). * * @since 0.1.0 (F082) * * @param int $server_id MCP server id. * @param string $ability_slug \WP_Ability::get_name() output. * @param array $meta The ability's `get_meta()` output. * @return bool */ public static function resolve_effective( int $server_id, string $ability_slug, array $meta ): bool { $key = $server_id . ':' . $ability_slug; if ( array_key_exists( $key, self::$effective_cache ) ) { return self::$effective_cache[ $key ]; } // Priority 1: explicit row wins over everything. $rows = Query::instance()->query( array( 'server_id' => $server_id, 'ability_slug' => $ability_slug, 'number' => 1, ) ); if ( ! empty( $rows ) ) { self::$effective_cache[ $key ] = (bool) $rows[0]->is_exposed; return self::$effective_cache[ $key ]; } // Priority 2: server-level policy. $policy = self::server_policy( $server_id ); if ( 'expose' === $policy ) { self::$effective_cache[ $key ] = true; return true; } if ( 'hide' === $policy ) { self::$effective_cache[ $key ] = false; return false; } // Priority 3: per-ability meta fallback (same as resolve_row_only). self::$effective_cache[ $key ] = ! empty( $meta['mcp']['public'] ); return self::$effective_cache[ $key ]; } /** * Look up the server-level default policy with per-request cache. * * @since 0.1.0 (F082) * * @param int $server_id MCP server id. * @return string One of `'per-ability' | 'expose' | 'hide'`. Falls back to * `'per-ability'` when the server row is missing or the * column value is not in the allowed set (safe default — * matches pre-F082 behaviour). */ private static function server_policy( int $server_id ): string { if ( array_key_exists( $server_id, self::$policy_cache ) ) { return self::$policy_cache[ $server_id ]; } $rows = MCPServerQuery::instance()->query( array( 'id' => $server_id, 'number' => 1, ) ); $policy = 'per-ability'; if ( ! empty( $rows ) && ! empty( $rows[0]->abilities_default_policy ) ) { $candidate = (string) $rows[0]->abilities_default_policy; if ( in_array( $candidate, array( 'expose', 'hide', 'per-ability' ), true ) ) { $policy = $candidate; } } self::$policy_cache[ $server_id ] = $policy; return $policy; } /** * Reset all three per-request caches. * * Production callers (F082 `PolicyTransition`, per-pair upsert snapshots * in `AbilitiesController::post_abilities()`) use this after writes so * post-change reads within the same request see fresh values; tests use * it between cases. Renamed from the test-only-sounding * `_reset_cache_for_tests()` once production code came to depend on it * (2026-09-06 architecture-review R2); that name survives below as a * delegating alias because F017's test contract pins it. * * @since 0.1.0 (F082) * @return void */ public static function reset_request_cache(): void { self::$cache = array(); self::$effective_cache = array(); self::$policy_cache = array(); } /** * Back-compat test alias for {@see reset_request_cache()}. * * F017's test contract pins this exact name (companion brief CONSTRAINT: * do not rename) — kept as a delegating alias; new code, production or * test, should call `reset_request_cache()`. * * @internal * @return void */ public static function _reset_cache_for_tests(): void { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Deliberate test-only alias; F017 tests depend on this exact name (companion brief CONSTRAINT: do not rename). self::reset_request_cache(); } }