From 64ee912e5978a10a07ec49a1fd2cceec04e8e863 Mon Sep 17 00:00:00 2001 From: Kyrian Obikwelu Date: Wed, 25 Jun 2025 04:23:48 +0100 Subject: [PATCH] fix: prevent cache deletion during Registry::load() The Registry::load() method was incorrectly calling clear() which deleted the cache before attempting to read from it, making caching completely ineffective. Modified clear() to accept an optional $includeCache parameter (default: true) to maintain backward compatibility while allowing load() to clear only local elements without destroying the cache. --- src/Registry.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Registry.php b/src/Registry.php index d2826a5..ddb3a51 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -85,7 +85,7 @@ public function load(): void return; } - $this->clear(); + $this->clear(false); try { $cached = $this->cache->get(self::DISCOVERED_ELEMENTS_CACHE_KEY); @@ -176,8 +176,6 @@ public function load(): void $this->logger->debug("Loaded {$loadCount} elements from cache."); } catch (CacheInvalidArgumentException $e) { $this->logger->error('Invalid registry cache key used.', ['key' => self::DISCOVERED_ELEMENTS_CACHE_KEY, 'exception' => $e]); - } catch (DefinitionException $e) { - $this->logger->error('Error hydrating definition from cache.', ['exception' => $e]); } catch (Throwable $e) { $this->logger->error('Unexpected error loading from cache.', ['key' => self::DISCOVERED_ELEMENTS_CACHE_KEY, 'exception' => $e]); } @@ -351,9 +349,14 @@ public function hasElements(): bool || ! empty($this->resourceTemplates); } - public function clear(): void + /** + * Clear discovered elements from registry + * + * @param bool $includeCache Whether to clear the cache as well (default: true) + */ + public function clear(bool $includeCache = true): void { - if ($this->cache !== null) { + if ($includeCache && $this->cache !== null) { try { $this->cache->delete(self::DISCOVERED_ELEMENTS_CACHE_KEY); $this->logger->debug('Registry cache cleared.'); @@ -438,24 +441,24 @@ public function getPrompt(string $name): ?RegisteredPrompt /** @return array */ public function getTools(): array { - return array_map(fn ($tool) => $tool->schema, $this->tools); + return array_map(fn($tool) => $tool->schema, $this->tools); } /** @return array */ public function getResources(): array { - return array_map(fn ($resource) => $resource->schema, $this->resources); + return array_map(fn($resource) => $resource->schema, $this->resources); } /** @return array */ public function getPrompts(): array { - return array_map(fn ($prompt) => $prompt->schema, $this->prompts); + return array_map(fn($prompt) => $prompt->schema, $this->prompts); } /** @return array */ public function getResourceTemplates(): array { - return array_map(fn ($template) => $template->schema, $this->resourceTemplates); + return array_map(fn($template) => $template->schema, $this->resourceTemplates); } }