From 9ca29c17cbd947a16e06ac2c57a3f955795dbd3d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 11:40:11 +0100 Subject: [PATCH 01/25] Revert template root directory ./ stripping Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Platform/Modules/Functions/Workers/Builds.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 92e00cd4e6..f04f7c3807 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -298,9 +298,6 @@ class Builds extends Action $templateRootDirectory = $template->getAttribute('rootDirectory', ''); $templateRootDirectory = \trim($templateRootDirectory, '/'); - while (\str_starts_with($templateRootDirectory, './')) { - $templateRootDirectory = \substr($templateRootDirectory, 2); - } if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { throw new \Exception('Invalid template root directory'); } @@ -426,9 +423,6 @@ class Builds extends Action $templateRootDirectory = $template->getAttribute('rootDirectory', ''); $templateRootDirectory = \trim($templateRootDirectory, '/'); - while (\str_starts_with($templateRootDirectory, './')) { - $templateRootDirectory = \substr($templateRootDirectory, 2); - } if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { throw new \Exception('Invalid template root directory'); } From f22c7425f758073fb84ee6a094621f12a6175f5a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 12:02:02 +0100 Subject: [PATCH 02/25] Revert sites test relaxation tied to template path stripping Co-Authored-By: Claude Opus 4.7 (1M context) --- tests/e2e/Services/Sites/SitesCustomServerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 9cca689780..2df37c6b1a 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -2274,7 +2274,7 @@ class SitesCustomServerTest extends Scope $deployment = $this->createTemplateDeployment($siteId, [ 'repository' => $template['providerRepositoryId'], 'owner' => $template['providerOwner'], - 'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'], + 'rootDirectory' => \ltrim($template['frameworks'][0]['providerRootDirectory'], './'), 'type' => 'tag', 'reference' => $template['providerVersion'], 'activate' => true @@ -2345,7 +2345,7 @@ class SitesCustomServerTest extends Scope $deployment = $this->createTemplateDeployment($siteId, [ 'repository' => $template['providerRepositoryId'], 'owner' => $template['providerOwner'], - 'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'], + 'rootDirectory' => \ltrim($template['frameworks'][0]['providerRootDirectory'], './'), 'type' => 'branch', 'reference' => 'main', 'activate' => true @@ -2423,7 +2423,7 @@ class SitesCustomServerTest extends Scope $deployment = $this->createTemplateDeployment($siteId, [ 'repository' => $template['providerRepositoryId'], 'owner' => $template['providerOwner'], - 'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'], + 'rootDirectory' => \ltrim($template['frameworks'][0]['providerRootDirectory'], './'), 'type' => 'commit', 'reference' => $latestCommit, 'activate' => true From 6ed54706b3cd87c4a348db931f37ffcb52d30bc5 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 12:22:59 +0100 Subject: [PATCH 03/25] Restore original ./ stripping for template and VCS root directories Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Modules/Functions/Workers/Builds.php | 21 ++++++++----------- .../Services/Sites/SitesCustomServerTest.php | 6 +++--- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f04f7c3807..dd0f9d541b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -297,10 +297,9 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \trim($templateRootDirectory, '/'); - if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { - throw new \Exception('Invalid template root directory'); - } + $templateRootDirectory = \rtrim($templateRootDirectory, '/'); + $templateRootDirectory = \ltrim($templateRootDirectory, '.'); + $templateRootDirectory = \ltrim($templateRootDirectory, '/'); if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { $stdout = ''; @@ -363,10 +362,9 @@ class Builds extends Action // VCS and VCS+Temaplte $tmpDirectory = '/tmp/builds/' . $deploymentId . '/code'; $rootDirectory = $resource->getAttribute('providerRootDirectory', ''); - $rootDirectory = \trim($rootDirectory, '/'); - if ($rootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $rootDirectory)) { - throw new \Exception('Invalid root directory'); - } + $rootDirectory = \rtrim($rootDirectory, '/'); + $rootDirectory = \ltrim($rootDirectory, '.'); + $rootDirectory = \ltrim($rootDirectory, '/'); $owner = $github->getOwnerName($providerInstallationId); $repositoryName = $github->getRepositoryName($providerRepositoryId); @@ -422,10 +420,9 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \trim($templateRootDirectory, '/'); - if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { - throw new \Exception('Invalid template root directory'); - } + $templateRootDirectory = \rtrim($templateRootDirectory, '/'); + $templateRootDirectory = \ltrim($templateRootDirectory, '.'); + $templateRootDirectory = \ltrim($templateRootDirectory, '/'); if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { // Clone template repo diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 2df37c6b1a..9cca689780 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -2274,7 +2274,7 @@ class SitesCustomServerTest extends Scope $deployment = $this->createTemplateDeployment($siteId, [ 'repository' => $template['providerRepositoryId'], 'owner' => $template['providerOwner'], - 'rootDirectory' => \ltrim($template['frameworks'][0]['providerRootDirectory'], './'), + 'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'], 'type' => 'tag', 'reference' => $template['providerVersion'], 'activate' => true @@ -2345,7 +2345,7 @@ class SitesCustomServerTest extends Scope $deployment = $this->createTemplateDeployment($siteId, [ 'repository' => $template['providerRepositoryId'], 'owner' => $template['providerOwner'], - 'rootDirectory' => \ltrim($template['frameworks'][0]['providerRootDirectory'], './'), + 'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'], 'type' => 'branch', 'reference' => 'main', 'activate' => true @@ -2423,7 +2423,7 @@ class SitesCustomServerTest extends Scope $deployment = $this->createTemplateDeployment($siteId, [ 'repository' => $template['providerRepositoryId'], 'owner' => $template['providerOwner'], - 'rootDirectory' => \ltrim($template['frameworks'][0]['providerRootDirectory'], './'), + 'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'], 'type' => 'commit', 'reference' => $latestCommit, 'activate' => true From 374f858db293d83ee298973759011f9b873b4439 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:14:39 +0100 Subject: [PATCH 04/25] chore: trace database worker with Span::add instead of Console logging Replace unstructured Console::info/error calls in the database worker with structured Span::add tags. Operation identifiers (project, database, collection, document) and queue metadata are recorded up front; swallowed errors (deleteByGroup, attribute NotFound) are surfaced as spans so they stay visible. Redundant Console::error calls before rethrows are dropped since the entry-point harness owns span error lifecycle. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index ee8494b382..abdd678816 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Modules\Databases\Workers; use Appwrite\Event\Message\Database as DatabaseMessage; use Appwrite\Event\Realtime; use Exception; -use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; @@ -18,6 +17,7 @@ use Utopia\Database\Query; use Utopia\Logger\Log; use Utopia\Platform\Action; use Utopia\Queue\Message; +use Utopia\Span\Span; class Databases extends Action { @@ -73,12 +73,22 @@ class Databases extends Action $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); + Span::add('project.id', $project->getId()); + Span::add('payload.type', $type); + Span::add('queue.pid', $message->getPid()); + Span::add('queue.name', $message->getQueue()); + Span::add('message.timestamp', (string) $message->getTimestamp()); + Span::add('database.collection.id', $collection->getId()); + Span::add('database.document.id', $document->getId()); + if ($database->isEmpty()) { throw new Exception('Missing database'); } $log->addTag('databaseId', $database->getId()); + Span::add('database.id', $database->getId()); + match (\strval($type)) { DATABASE_TYPE_DELETE_DATABASE => $this->deleteDatabase($database, $dbForProject, $dbForDatabases), DATABASE_TYPE_DELETE_COLLECTION => $this->deleteCollection($database, $collection, $dbForProject, $dbForDatabases), @@ -88,14 +98,6 @@ class Databases extends Action DATABASE_TYPE_DELETE_INDEX => $this->deleteIndex($database, $collection, $document, $project, $dbForPlatform, $dbForProject, $dbForDatabases, $queueForRealtime), default => throw new Exception('No database operation for type: ' . \strval($type)), }; - - Console::info("Finished processing database operation: \n" . \json_encode([ - 'type' => $type, - 'projectId' => $project->getId(), - 'databaseId' => $database->getId(), - 'collectionId' => $collection->getId(), - 'documentId' => $document->getId(), - ], JSON_PRETTY_PRINT)); } /** @@ -206,8 +208,6 @@ class Databases extends Action $dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available')); } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $attribute->setAttribute('error', $e->getMessage()); if (! $relatedAttribute->isEmpty()) { @@ -309,7 +309,7 @@ class Databases extends Action } } catch (NotFound $e) { - Console::error($e->getMessage()); + Span::add('database.attribute.not_found', $e->getMessage()); $dbForProject->deleteDocument('attributes', $attribute->getId()); @@ -318,8 +318,6 @@ class Databases extends Action } } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $attribute->setAttribute('error', $e->getMessage()); if (!$relatedAttribute->isEmpty()) { @@ -456,7 +454,6 @@ class Databases extends Action } $dbForProject->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'available')); } catch (\Throwable $e) { - Console::error($e->getMessage()); if ($e instanceof DatabaseException) { $index->setAttribute('error', $e->getMessage()); } @@ -512,8 +509,6 @@ class Databases extends Action $dbForProject->deleteDocument('indexes', $index->getId()); $index->setAttribute('status', 'deleted'); } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $index->setAttribute('error', $e->getMessage()); } @@ -613,8 +608,6 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { - $start = \microtime(true); - try { $count = $database->deleteDocuments( $collectionId, @@ -624,12 +617,11 @@ class Databases extends Action ); } catch (\Throwable $th) { $tenant = $database->getSharedTables() ? 'Tenant:'.$database->getTenant() : ''; - Console::error("Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); + Span::add('delete_by_group.error', "Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); return; } - $end = \microtime(true); - Console::info("Deleted {$count} documents/rows by group in " . ($end - $start) . " seconds"); + Span::add('delete_by_group.count', $count); } /** From a5cedf4671337f56b8b36f6d02382af39c924332 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:18:37 +0100 Subject: [PATCH 05/25] chore: resolve-adjacent spans and structured delete_by_group attributes Move each Span::add next to where its value is resolved instead of clumping them. Split the delete_by_group error string into separate structured attributes (namespace, collection.id, tenant, error) so it is no longer an unstructured log embedded in a span. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index abdd678816..2aaf86c9b0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -61,11 +61,25 @@ class Databases extends Action throw new Exception('Missing payload'); } + Span::add('project.id', $project->getId()); + Span::add('queue.pid', $message->getPid()); + Span::add('queue.name', $message->getQueue()); + Span::add('message.timestamp', (string) $message->getTimestamp()); + $databaseMessage = DatabaseMessage::fromArray($payload); + $type = $databaseMessage->type; + Span::add('payload.type', $type); + $document = $databaseMessage->row ?? $databaseMessage->document ?? new Document(); + Span::add('database.document.id', $document->getId()); + $collection = $databaseMessage->table ?? $databaseMessage->collection ?? new Document(); + Span::add('database.collection.id', $collection->getId()); + $database = $databaseMessage->database ?? new Document(); + Span::add('database.id', $database->getId()); + /** * @var Database $dbForDatabases */ @@ -73,22 +87,12 @@ class Databases extends Action $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); - Span::add('project.id', $project->getId()); - Span::add('payload.type', $type); - Span::add('queue.pid', $message->getPid()); - Span::add('queue.name', $message->getQueue()); - Span::add('message.timestamp', (string) $message->getTimestamp()); - Span::add('database.collection.id', $collection->getId()); - Span::add('database.document.id', $document->getId()); - if ($database->isEmpty()) { throw new Exception('Missing database'); } $log->addTag('databaseId', $database->getId()); - Span::add('database.id', $database->getId()); - match (\strval($type)) { DATABASE_TYPE_DELETE_DATABASE => $this->deleteDatabase($database, $dbForProject, $dbForDatabases), DATABASE_TYPE_DELETE_COLLECTION => $this->deleteCollection($database, $collection, $dbForProject, $dbForDatabases), @@ -616,8 +620,12 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - $tenant = $database->getSharedTables() ? 'Tenant:'.$database->getTenant() : ''; - Span::add('delete_by_group.error', "Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); + Span::add('delete_by_group.namespace', $database->getNamespace()); + Span::add('delete_by_group.collection.id', $collectionId); + if ($database->getSharedTables()) { + Span::add('delete_by_group.tenant', $database->getTenant()); + } + Span::add('delete_by_group.error', $th->getMessage()); return; } From 4433b10eafb57ed9ff6057b7bc52e4f04bfde275 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:23:17 +0100 Subject: [PATCH 06/25] chore: scope namespace/tenant spans under database Namespace and tenant are properties of the database, so trace them as database.namespace / database.tenant rather than under the operation. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Platform/Modules/Databases/Workers/Databases.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 2aaf86c9b0..702d33c2f0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -620,11 +620,11 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - Span::add('delete_by_group.namespace', $database->getNamespace()); - Span::add('delete_by_group.collection.id', $collectionId); + Span::add('database.namespace', $database->getNamespace()); if ($database->getSharedTables()) { - Span::add('delete_by_group.tenant', $database->getTenant()); + Span::add('database.tenant', $database->getTenant()); } + Span::add('delete_by_group.collection', $collectionId); Span::add('delete_by_group.error', $th->getMessage()); return; } From 85b5328f2ecf12f81f1fe7e58f80fc81f4ae30f6 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:24:32 +0100 Subject: [PATCH 07/25] chore: always record database namespace/tenant in deleteByGroup Namespace and tenant describe the database regardless of outcome, so record them (along with the target collection) up front instead of only on the error path. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Platform/Modules/Databases/Workers/Databases.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 702d33c2f0..89181bfd39 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -612,6 +612,10 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { + Span::add('database.namespace', $database->getNamespace()); + Span::add('database.tenant', $database->getTenant()); + Span::add('delete_by_group.collection', $collectionId); + try { $count = $database->deleteDocuments( $collectionId, @@ -620,11 +624,6 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - Span::add('database.namespace', $database->getNamespace()); - if ($database->getSharedTables()) { - Span::add('database.tenant', $database->getTenant()); - } - Span::add('delete_by_group.collection', $collectionId); Span::add('delete_by_group.error', $th->getMessage()); return; } From b3263138e8d12641fd2b57a0fb61bb3dcead3e7a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:26:17 +0100 Subject: [PATCH 08/25] chore: suffix delete_by_group collection span with .id Identifiers end in .id by convention; rename delete_by_group.collection to delete_by_group.collection.id. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 89181bfd39..f0765af2a4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -614,7 +614,7 @@ class Databases extends Action { Span::add('database.namespace', $database->getNamespace()); Span::add('database.tenant', $database->getTenant()); - Span::add('delete_by_group.collection', $collectionId); + Span::add('delete_by_group.collection.id', $collectionId); try { $count = $database->deleteDocuments( From 844d21d7763da0d121bb8d89d735617363916ad9 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:27:40 +0100 Subject: [PATCH 09/25] chore: record database namespace/tenant once in action These describe the project database connection and apply to every operation, so trace them up front in action() (from dbForProject, which deleteByGroup always uses) rather than inside deleteByGroup. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index f0765af2a4..cb3e7078a0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -79,6 +79,8 @@ class Databases extends Action $database = $databaseMessage->database ?? new Document(); Span::add('database.id', $database->getId()); + Span::add('database.namespace', $dbForProject->getNamespace()); + Span::add('database.tenant', $dbForProject->getTenant()); /** * @var Database $dbForDatabases @@ -612,8 +614,6 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { - Span::add('database.namespace', $database->getNamespace()); - Span::add('database.tenant', $database->getTenant()); Span::add('delete_by_group.collection.id', $collectionId); try { From bf4d620a5cc3b01825532843e1e58cb43317ccd2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:30:10 +0100 Subject: [PATCH 10/25] chore: let deleteByGroup failures reach the worker error handler Previously a failed batch delete was caught and swallowed, acking the message as if it succeeded even though the documents remained. Let the error propagate so the worker marks the message failed and retries. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index cb3e7078a0..2474ed99f4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -610,23 +610,20 @@ class Databases extends Action * @param Database $database * @param callable|null $callback * @return void - * @throws Exception + * @throws \Throwable */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { Span::add('delete_by_group.collection.id', $collectionId); - try { - $count = $database->deleteDocuments( - $collectionId, - $queries, - Database::DELETE_BATCH_SIZE, - $callback - ); - } catch (\Throwable $th) { - Span::add('delete_by_group.error', $th->getMessage()); - return; - } + // A failed batch delete is non-recoverable: let it propagate to the + // worker's error handler so the message is marked failed and retried. + $count = $database->deleteDocuments( + $collectionId, + $queries, + Database::DELETE_BATCH_SIZE, + $callback + ); Span::add('delete_by_group.count', $count); } From 5218fc8209ae06c89176b2885c77d7e9503f3853 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:30:53 +0100 Subject: [PATCH 11/25] chore: drop unnecessary comment in deleteByGroup Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 2474ed99f4..a763a62778 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -616,8 +616,6 @@ class Databases extends Action { Span::add('delete_by_group.collection.id', $collectionId); - // A failed batch delete is non-recoverable: let it propagate to the - // worker's error handler so the message is marked failed and retried. $count = $database->deleteDocuments( $collectionId, $queries, From 486d9271ac7e8458078b6b9d8e913e13c7770a05 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:36:27 +0100 Subject: [PATCH 12/25] chore: centralize project.id span; drop superfluous worker spans The project resource setter is the canonical owner of project.id (cloud already adds it there for every worker span). Add it to the OSS worker project resource too, and drop the redundant project.id from the database worker. Also drop queue.pid/queue.name/message.timestamp: queue.name duplicates the span action (worker.databases) and none were part of the logging this PR replaced. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/init/worker/message.php | 7 ++++++- .../Platform/Modules/Databases/Workers/Databases.php | 5 ----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/init/worker/message.php b/app/init/worker/message.php index ea6ba13aec..7cbfe19b0f 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -18,6 +18,7 @@ use Utopia\Logger\Log; use Utopia\Pools\Group; use Utopia\Queue\Publisher; use Utopia\Registry\Registry; +use Utopia\Span\Span; use Utopia\Storage\Device\Telemetry as TelemetryDevice; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -55,7 +56,11 @@ return function (Container $container): void { return $project; } - return $dbForPlatform->getDocument('projects', $project->getId()); + $project = $dbForPlatform->getDocument('projects', $project->getId()); + + Span::add('project.id', $project->getId()); + + return $project; }, ['message', 'dbForPlatform']); $container->set('dbForProject', fn (DatabaseFactory $databaseFactory, Document $project, Database $dbForPlatform) => $project->isEmpty() || $project->getId() === 'console' diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index a763a62778..333af87493 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -61,11 +61,6 @@ class Databases extends Action throw new Exception('Missing payload'); } - Span::add('project.id', $project->getId()); - Span::add('queue.pid', $message->getPid()); - Span::add('queue.name', $message->getQueue()); - Span::add('message.timestamp', (string) $message->getTimestamp()); - $databaseMessage = DatabaseMessage::fromArray($payload); $type = $databaseMessage->type; From 46b2a398ff9e08d8277e3bae98f6d9d485300c24 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:49:38 +0100 Subject: [PATCH 13/25] fix: attempt all collection cleanups before failing Dropping the deleteByGroup swallow meant a failure in the first cleanup (related-attribute purge) skipped the attribute and index cleanups, leaving orphaned metadata rows since the table was already dropped. Run every cleanup in deleteCollection regardless of individual failures, then rethrow the first error so the worker still marks the message failed and retries the outstanding work. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 65 ++++++++++++------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 333af87493..4339a0e165 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,33 +569,50 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - /** - * Related collections relating to current collection - */ - $this->deleteByGroup( - 'attributes', - [ + // The table is already dropped, so attempt every metadata cleanup even + // if an earlier one fails -- skipping the rest would leave orphaned + // attribute/index rows. Re-throw the first failure afterwards so the + // worker marks the message failed and retries the outstanding work. + $cleanups = [ + /** + * Related collections relating to current collection + */ + fn () => $this->deleteByGroup( + 'attributes', + [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('type', [Database::VAR_RELATIONSHIP]), + Query::notEqual('collectionInternalId', $collectionInternalId), + Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), + ], + $dbForProject, + function ($attribute) use ($dbForProject, $databaseInternalId) { + $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); + $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); + } + ), + fn () => $this->deleteByGroup('attributes', [ Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('type', [Database::VAR_RELATIONSHIP]), - Query::notEqual('collectionInternalId', $collectionInternalId), - Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), - ], - $dbForProject, - function ($attribute) use ($dbForProject, $databaseInternalId) { - $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); - $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject), + fn () => $this->deleteByGroup('indexes', [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject), + ]; + + $error = null; + foreach ($cleanups as $cleanup) { + try { + $cleanup(); + } catch (\Throwable $e) { + $error ??= $e; } - ); + } - $this->deleteByGroup('attributes', [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject); - - $this->deleteByGroup('indexes', [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject); + if ($error !== null) { + throw $error; + } } From 1a4f23d0adf07b252b1a3ec71aaca4f7d8ec832c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:51:05 +0100 Subject: [PATCH 14/25] refactor: extract attemptAll helper for collection cleanups Replace the inline cleanup array + accumulator loop with a small variadic attemptAll() helper, so deleteCollection reads as the intent: run all cleanups, rethrow the first failure. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 4339a0e165..2db37008b1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,14 +569,11 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - // The table is already dropped, so attempt every metadata cleanup even - // if an earlier one fails -- skipping the rest would leave orphaned - // attribute/index rows. Re-throw the first failure afterwards so the - // worker marks the message failed and retries the outstanding work. - $cleanups = [ - /** - * Related collections relating to current collection - */ + // The table is already dropped, so run every metadata cleanup even if + // one fails -- skipping the rest would orphan attribute/index rows. + // attemptAll re-throws the first failure so the message is retried. + $this->attemptAll( + // Related collections relating to current collection fn () => $this->deleteByGroup( 'attributes', [ @@ -599,9 +596,21 @@ class Databases extends Action Query::equal('databaseInternalId', [$databaseInternalId]), Query::equal('collectionInternalId', [$collectionInternalId]) ], $dbForProject), - ]; + ); + } + /** + * Run every cleanup even if some fail, then re-throw the first failure so + * the worker marks the message failed and retries the outstanding work. + * + * @param callable ...$cleanups + * @return void + * @throws \Throwable + */ + private function attemptAll(callable ...$cleanups): void + { $error = null; + foreach ($cleanups as $cleanup) { try { $cleanup(); From 9036e03b400324ca74a413ff1010f808fec06518 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:54:32 +0100 Subject: [PATCH 15/25] chore: drop redundant inline comment in deleteCollection Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 2db37008b1..fda1ebe923 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,9 +569,6 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - // The table is already dropped, so run every metadata cleanup even if - // one fails -- skipping the rest would orphan attribute/index rows. - // attemptAll re-throws the first failure so the message is retried. $this->attemptAll( // Related collections relating to current collection fn () => $this->deleteByGroup( From fad69ebd44684ef62a663d77888256f1a62594be Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:00:08 +0100 Subject: [PATCH 16/25] chore: keep deleteByGroup best-effort, swallowing failures Revert to the original swallow-and-continue: a failed batch delete is recorded on the span and skipped so the remaining cleanups still run. Drops the attemptAll helper and restores deleteCollection's sequential calls for a minimal diff. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 90 ++++++++----------- 1 file changed, 36 insertions(+), 54 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index fda1ebe923..a6b13857a5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,56 +569,33 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - $this->attemptAll( - // Related collections relating to current collection - fn () => $this->deleteByGroup( - 'attributes', - [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('type', [Database::VAR_RELATIONSHIP]), - Query::notEqual('collectionInternalId', $collectionInternalId), - Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), - ], - $dbForProject, - function ($attribute) use ($dbForProject, $databaseInternalId) { - $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); - $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); - } - ), - fn () => $this->deleteByGroup('attributes', [ + /** + * Related collections relating to current collection + */ + $this->deleteByGroup( + 'attributes', + [ Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject), - fn () => $this->deleteByGroup('indexes', [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject), - ); - } - - /** - * Run every cleanup even if some fail, then re-throw the first failure so - * the worker marks the message failed and retries the outstanding work. - * - * @param callable ...$cleanups - * @return void - * @throws \Throwable - */ - private function attemptAll(callable ...$cleanups): void - { - $error = null; - - foreach ($cleanups as $cleanup) { - try { - $cleanup(); - } catch (\Throwable $e) { - $error ??= $e; + Query::equal('type', [Database::VAR_RELATIONSHIP]), + Query::notEqual('collectionInternalId', $collectionInternalId), + Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), + ], + $dbForProject, + function ($attribute) use ($dbForProject, $databaseInternalId) { + $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); + $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); } - } + ); - if ($error !== null) { - throw $error; - } + $this->deleteByGroup('attributes', [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject); + + $this->deleteByGroup('indexes', [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject); } @@ -628,18 +605,23 @@ class Databases extends Action * @param Database $database * @param callable|null $callback * @return void - * @throws \Throwable + * @throws Exception */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { Span::add('delete_by_group.collection.id', $collectionId); - $count = $database->deleteDocuments( - $collectionId, - $queries, - Database::DELETE_BATCH_SIZE, - $callback - ); + try { + $count = $database->deleteDocuments( + $collectionId, + $queries, + Database::DELETE_BATCH_SIZE, + $callback + ); + } catch (\Throwable $th) { + Span::add('delete_by_group.error', $th->getMessage()); + return; + } Span::add('delete_by_group.count', $count); } From da8ff0f99c649f02964ccf3081c4a92d7b51c663 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 15:50:03 +0100 Subject: [PATCH 17/25] chore: replace deprecated Route::getParamsValues() in shared API Reconstruct request param values from per-request sources (path values + request params + param defaults) instead of reading the deprecated mutable value store on the shared Route object, mirroring the framework's own Http::getArguments() resolution precedence. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 2b39208fcd..7a36b012e3 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -38,6 +38,7 @@ use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Roles; use Utopia\Http\Http; use Utopia\Http\Route; +use Utopia\Http\Router; use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -862,7 +863,14 @@ Http::shutdown() } } - $requestParams = $route->getParamsValues(); + $preparedPath = Router::preparePath($route->getMatchedPath()); + $pathValues = $route->getPathValues($request, $preparedPath[0]); + $reqParams = $request->getParams(); + + $requestParams = []; + foreach ($route->getParams() as $key => $param) { + $requestParams[$key] = $pathValues[$key] ?? $reqParams[$key] ?? $param['default']; + } /** * Abuse labels From ad7a76abbb5d7866ff433ac06a23de3a54e6584d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:07:27 +0100 Subject: [PATCH 18/25] Use injected 'params' local instead of getPathValues reconstruction The http server exposes the route's path values as an injectable 'params' local, so inject it directly rather than rebuilding them via Router::preparePath() + Route::getPathValues(). Query/body params and declared defaults are still merged in to reproduce the full resolved param set (path values take precedence), matching prior getParamsValues() behaviour. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 7a36b012e3..c3db25ab72 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -38,7 +38,6 @@ use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Roles; use Utopia\Http\Http; use Utopia\Http\Route; -use Utopia\Http\Router; use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -787,6 +786,7 @@ Http::shutdown() Http::shutdown() ->groups(['api']) ->inject('route') + ->inject('params') ->inject('request') ->inject('response') ->inject('project') @@ -806,7 +806,7 @@ Http::shutdown() ->inject('bus') ->inject('apiKey') ->inject('mode') - ->action(function (Route $route, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); @@ -863,13 +863,14 @@ Http::shutdown() } } - $preparedPath = Router::preparePath($route->getMatchedPath()); - $pathValues = $route->getPathValues($request, $preparedPath[0]); + // $params holds the URL path values (injected from the route match); + // merge in query/body params and declared defaults to reproduce the + // full resolved param set, with path values taking precedence. $reqParams = $request->getParams(); $requestParams = []; foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $pathValues[$key] ?? $reqParams[$key] ?? $param['default']; + $requestParams[$key] = $params[$key] ?? $reqParams[$key] ?? $param['default']; } /** From 0bf406e597f2c9e02a226aa0a9e772ba40abacfc Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:18:00 +0100 Subject: [PATCH 19/25] Tighten param reconstruction Use Request::getParam() for the query/body+default fallback instead of a cached getParams() copy, dropping the temporary and shortening the diff. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index c3db25ab72..d22c80d1b2 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -863,14 +863,11 @@ Http::shutdown() } } - // $params holds the URL path values (injected from the route match); - // merge in query/body params and declared defaults to reproduce the - // full resolved param set, with path values taking precedence. - $reqParams = $request->getParams(); - + // Reproduce the resolved param set: injected path values take precedence + // over query/body params, falling back to each param's declared default. $requestParams = []; foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $params[$key] ?? $reqParams[$key] ?? $param['default']; + $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); } /** From cd0936c7214f953ff271f4f21e7ebef20b8596d7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:42:39 +0100 Subject: [PATCH 20/25] Extract response caching into its own shutdown hook Move the cache.resource / cache.resourceType handling out of the large shared shutdown hook into a dedicated Http::shutdown() hook in the 'api' group. It injects only what caching needs (route, params, request, response, project, user, dbForProject, authorization) and guards with early returns, leaving the main hook focused on events, audit, and usage. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 131 +++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 55 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index d22c80d1b2..4c947b5f4e 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -958,61 +958,6 @@ Http::shutdown() $publisherForAudits->enqueue(AuditMessage::fromContext($auditContext)); } - // Cache label - $useCache = $route->getLabel('cache', false); - if ($useCache) { - $resource = $resourceType = null; - $data = $response->getPayload(); - $statusCode = $response->getStatusCode(); - if (! empty($data['payload']) && $statusCode >= 200 && $statusCode < 300) { - $pattern = $route->getLabel('cache.resource', null); - if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); - } - - $pattern = $route->getLabel('cache.resourceType', null); - if (! empty($pattern)) { - $resourceType = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); - } - - $cache = new Cache( - new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) - ); - - $key = $request->cacheIdentifier(); - $signature = md5($data['payload']); - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - $accessedAt = $cacheLog->getAttribute('accessedAt', 0); - $now = DateTime::now(); - if ($cacheLog->isEmpty()) { - try { - $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ - '$id' => $key, - 'resource' => $resource, - 'resourceType' => $resourceType, - 'mimeType' => $response->getContentType(), - 'accessedAt' => $now, - 'signature' => $signature, - ]))); - } catch (DuplicateException) { - // Race condition: another concurrent request already created the cache document - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - } - } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { - $cacheLog->setAttribute('accessedAt', $now); - $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ - 'accessedAt' => $cacheLog->getAttribute('accessedAt') - ]))); - // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() - $cache->save($key, $data['payload']); - } - - if ($signature !== $cacheLog->getAttribute('signature')) { - $cache->save($key, $data['payload']); - } - } - } - if ($project->getId() !== 'console') { if (! $user->isPrivileged($authorization->getRoles())) { $bus->dispatch(new RequestCompleted( @@ -1051,6 +996,82 @@ Http::shutdown() } }); +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('params') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('dbForProject') + ->inject('authorization') + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Database $dbForProject, Authorization $authorization) use ($parseLabel) { + if (! $route->getLabel('cache', false)) { + return; + } + + $data = $response->getPayload(); + $statusCode = $response->getStatusCode(); + if (empty($data['payload']) || $statusCode < 200 || $statusCode >= 300) { + return; + } + + // Reproduce the resolved param set: injected path values take precedence + // over query/body params, falling back to each param's declared default. + $requestParams = []; + foreach ($route->getParams() as $key => $param) { + $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); + } + + $resource = $resourceType = null; + $pattern = $route->getLabel('cache.resource', null); + if (! empty($pattern)) { + $resource = $parseLabel($pattern, $data, $requestParams, $user, $project); + } + + $pattern = $route->getLabel('cache.resourceType', null); + if (! empty($pattern)) { + $resourceType = $parseLabel($pattern, $data, $requestParams, $user, $project); + } + + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) + ); + + $key = $request->cacheIdentifier(); + $signature = md5($data['payload']); + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); + $accessedAt = $cacheLog->getAttribute('accessedAt', 0); + $now = DateTime::now(); + if ($cacheLog->isEmpty()) { + try { + $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ + '$id' => $key, + 'resource' => $resource, + 'resourceType' => $resourceType, + 'mimeType' => $response->getContentType(), + 'accessedAt' => $now, + 'signature' => $signature, + ]))); + } catch (DuplicateException) { + // Race condition: another concurrent request already created the cache document + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); + } + } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { + $cacheLog->setAttribute('accessedAt', $now); + $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ + 'accessedAt' => $cacheLog->getAttribute('accessedAt') + ]))); + // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() + $cache->save($key, $data['payload']); + } + + if ($signature !== $cacheLog->getAttribute('signature')) { + $cache->save($key, $data['payload']); + } + }); + Http::init() ->groups(['usage']) ->action(function () { From c9059c75a47dda64150a46529eae12d5ca1fd6b7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:46:17 +0100 Subject: [PATCH 21/25] Use injected 'params' directly for audit resource labels audits.resource labels only reference URL path params (resource IDs), so the audit code can use the injected 'params' local directly instead of reconstructing the full resolved param set. The path+query/body+default reconstruction now lives only in the cache hook, where the screenshot cache.resource label genuinely needs query params. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 4c947b5f4e..7c7e7f2555 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -863,13 +863,6 @@ Http::shutdown() } } - // Reproduce the resolved param set: injected path values take precedence - // over query/body params, falling back to each param's declared default. - $requestParams = []; - foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); - } - /** * Abuse labels */ @@ -910,7 +903,7 @@ Http::shutdown() */ $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); + $resource = $parseLabel($pattern, $responsePayload, $params, $user, $project); if (! empty($resource) && $resource !== $pattern) { $auditContext->resource = $resource; } From 2e5a29c650993a7e7e6d85ee28666cd5d62ad6f7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:56:46 +0100 Subject: [PATCH 22/25] Extract request-completed/usage into its own shutdown hook; tighten cache hook Move the RequestCompleted dispatch and usage-metrics publishing out of the large shared shutdown hook into a dedicated Http::shutdown() hook that injects only what it needs (request, response, project, user, usage, publisherForUsage, authorization, bus, apiKey). Drop the now-unused usage/publisherForUsage/bus/apiKey/authorization injections from the main hook. Registered after the cache hook so cache writes keep happening before usage publishing, as in the original single-hook ordering. Also tighten the cache hook with guard clauses: hoist the cache/key/ signature setup, make the create path save-and-return explicitly instead of relying on the empty-document signature mismatch, resolve the resource labels lazily inside the create branch (so the param reconstruction and label parsing only run on a cache miss, not on every cached hit), and collapse the trailing save into the existing-entry path. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 156 +++++++++++++++++---------------- 1 file changed, 81 insertions(+), 75 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 7c7e7f2555..bc7b870cbc 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -794,19 +794,14 @@ Http::shutdown() ->inject('queueForEvents') ->inject('auditContext') ->inject('publisherForAudits') - ->inject('usage') - ->inject('publisherForUsage') ->inject('publisherForFunctions') ->inject('queueForWebhooks') ->inject('queueForRealtime') ->inject('dbForProject') - ->inject('authorization') ->inject('timelimit') ->inject('eventProcessor') - ->inject('bus') - ->inject('apiKey') ->inject('mode') - ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, callable $timelimit, EventProcessor $eventProcessor, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); @@ -950,43 +945,6 @@ Http::shutdown() $publisherForAudits->enqueue(AuditMessage::fromContext($auditContext)); } - - if ($project->getId() !== 'console') { - if (! $user->isPrivileged($authorization->getRoles())) { - $bus->dispatch(new RequestCompleted( - project: $project->getArrayCopy(), - request: $request, - response: $response, - )); - } - - // Publish usage metrics if context has data - if (! $usage->isEmpty()) { - $metrics = $usage->getMetrics(); - - // Filter out API key disabled metrics using suffix pattern matching - $disabledMetrics = $apiKey?->getDisabledMetrics() ?? []; - if (! empty($disabledMetrics)) { - $metrics = array_values(array_filter($metrics, function ($metric) use ($disabledMetrics) { - foreach ($disabledMetrics as $pattern) { - if (str_ends_with($metric['key'], $pattern)) { - return false; - } - } - - return true; - })); - } - - $message = new UsageMessage( - project: $project, - metrics: $metrics, - reduce: $usage->getReduce() - ); - - $publisherForUsage->enqueue($message); - } - } }); Http::shutdown() @@ -1010,61 +968,109 @@ Http::shutdown() return; } - // Reproduce the resolved param set: injected path values take precedence - // over query/body params, falling back to each param's declared default. - $requestParams = []; - foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); - } - - $resource = $resourceType = null; - $pattern = $route->getLabel('cache.resource', null); - if (! empty($pattern)) { - $resource = $parseLabel($pattern, $data, $requestParams, $user, $project); - } - - $pattern = $route->getLabel('cache.resourceType', null); - if (! empty($pattern)) { - $resourceType = $parseLabel($pattern, $data, $requestParams, $user, $project); - } - $cache = new Cache( new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) ); - $key = $request->cacheIdentifier(); $signature = md5($data['payload']); - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - $accessedAt = $cacheLog->getAttribute('accessedAt', 0); $now = DateTime::now(); + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); + + // First request for this resource: record it and persist the payload. if ($cacheLog->isEmpty()) { + // Resolve resource labels lazily — only needed when creating the entry. + $requestParams = []; + foreach ($route->getParams() as $paramKey => $param) { + $requestParams[$paramKey] = $params[$paramKey] ?? $request->getParam($paramKey, $param['default']); + } + + $resourcePattern = $route->getLabel('cache.resource', null); + $resourceTypePattern = $route->getLabel('cache.resourceType', null); + try { $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ '$id' => $key, - 'resource' => $resource, - 'resourceType' => $resourceType, + 'resource' => empty($resourcePattern) ? null : $parseLabel($resourcePattern, $data, $requestParams, $user, $project), + 'resourceType' => empty($resourceTypePattern) ? null : $parseLabel($resourceTypePattern, $data, $requestParams, $user, $project), 'mimeType' => $response->getContentType(), 'accessedAt' => $now, 'signature' => $signature, ]))); + $cache->save($key, $data['payload']); + + return; } catch (DuplicateException) { - // Race condition: another concurrent request already created the cache document + // Race condition: another concurrent request already created the entry. $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); } - } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { - $cacheLog->setAttribute('accessedAt', $now); - $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ - 'accessedAt' => $cacheLog->getAttribute('accessedAt') - ]))); - // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() - $cache->save($key, $data['payload']); } - if ($signature !== $cacheLog->getAttribute('signature')) { + // Existing entry: refresh the access time once per APP_CACHE_UPDATE window + // (keeps the file mtime current for TTL checks) and re-persist on content change. + $stale = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $cacheLog->getAttribute('accessedAt', 0); + if ($stale) { + $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ + 'accessedAt' => $now, + ]))); + } + + if ($stale || $signature !== $cacheLog->getAttribute('signature')) { $cache->save($key, $data['payload']); } }); +Http::shutdown() + ->groups(['api']) + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('usage') + ->inject('publisherForUsage') + ->inject('authorization') + ->inject('bus') + ->inject('apiKey') + ->action(function (Request $request, Response $response, Document $project, User $user, Context $usage, UsagePublisher $publisherForUsage, Authorization $authorization, Bus $bus, ?Key $apiKey) { + if ($project->getId() === 'console') { + return; + } + + if (! $user->isPrivileged($authorization->getRoles())) { + $bus->dispatch(new RequestCompleted( + project: $project->getArrayCopy(), + request: $request, + response: $response, + )); + } + + // Publish usage metrics if context has data + if (! $usage->isEmpty()) { + $metrics = $usage->getMetrics(); + + // Filter out API key disabled metrics using suffix pattern matching + $disabledMetrics = $apiKey?->getDisabledMetrics() ?? []; + if (! empty($disabledMetrics)) { + $metrics = array_values(array_filter($metrics, function ($metric) use ($disabledMetrics) { + foreach ($disabledMetrics as $pattern) { + if (str_ends_with($metric['key'], $pattern)) { + return false; + } + } + + return true; + })); + } + + $message = new UsageMessage( + project: $project, + metrics: $metrics, + reduce: $usage->getReduce() + ); + + $publisherForUsage->enqueue($message); + } + }); + Http::init() ->groups(['usage']) ->action(function () { From f0192c6561cb73a5ddfd974d66f9b05c7a641761 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 17:15:29 +0100 Subject: [PATCH 23/25] Split abuse and audit flows into their own shutdown hooks Break the remaining shared 'api' shutdown hook into focused hooks: one for event/realtime/function/webhook dispatch, one for abuse-reset handling, and one for audit logging. Each injects only what it uses, so the wide shared injection list is gone. Registered in the original execution order (events -> abuse -> audit -> cache -> usage) so behaviour is unchanged; the pre-existing 'session' group shutdown hook is unaffected. Applied early-return guards to the events and abuse hooks to flatten their nesting. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 213 +++++++++++++++++---------------- 1 file changed, 113 insertions(+), 100 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index bc7b870cbc..db1bf99edb 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -783,6 +783,118 @@ Http::shutdown() $dbForProject->purgeCachedDocument('users', $userId); }); +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('response') + ->inject('project') + ->inject('queueForEvents') + ->inject('publisherForFunctions') + ->inject('queueForWebhooks') + ->inject('queueForRealtime') + ->inject('dbForProject') + ->inject('eventProcessor') + ->action(function (Route $route, Response $response, Document $project, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, EventProcessor $eventProcessor) { + if (empty($queueForEvents->getEvent())) { + return; + } + + if (empty($queueForEvents->getPayload())) { + $queueForEvents->setPayload($response->getPayload()); + } + + // Get project and function/webhook events (cached) + $functionsEvents = $eventProcessor->getFunctionsEvents($project, $dbForProject); + $webhooksEvents = $eventProcessor->getWebhooksEvents($project); + + // Generate events for this operation + $generatedEvents = Event::generateEvents( + $queueForEvents->getEvent(), + $queueForEvents->getParams() + ); + + $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); + if ($project->getId() !== 'console' || $allowedOnConsole) { + $queueForRealtime + ->from($queueForEvents) + ->trigger(); + } + + // Only trigger functions if there are matching function events + if (! empty($functionsEvents)) { + foreach ($generatedEvents as $event) { + if (isset($functionsEvents[$event])) { + $publisherForFunctions->enqueue(FunctionMessage::fromEvent( + event: $queueForEvents->getEvent(), + params: $queueForEvents->getParams(), + project: $queueForEvents->getProject(), + user: $queueForEvents->getUser(), + userId: $queueForEvents->getUserId(), + payload: $queueForEvents->getPayload(), + platform: $queueForEvents->getPlatform(), + )); + break; + } + } + } + + // Only trigger webhooks if there are matching webhook events + if (! empty($webhooksEvents)) { + foreach ($generatedEvents as $event) { + if (isset($webhooksEvents[$event])) { + $queueForWebhooks + ->from($queueForEvents) + ->trigger(); + break; + } + } + } + }); + +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('timelimit') + ->action(function (Route $route, Request $request, Response $response, Document $project, User $user, callable $timelimit) { + $abuseEnabled = System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled'; + $abuseResetCode = $route->getLabel('abuse-reset', []); + $abuseResetCode = \is_array($abuseResetCode) ? $abuseResetCode : [$abuseResetCode]; + + if (! $abuseEnabled || \count($abuseResetCode) === 0 || ! \in_array($response->getStatusCode(), $abuseResetCode)) { + return; + } + + $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); + $abuseKeyLabel = (! is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; + + foreach ($abuseKeyLabel as $abuseKey) { + $start = $request->getContentRangeStart(); + $end = $request->getContentRangeEnd(); + $timeLimit = $timelimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600)); + $timeLimit + ->setParam('{projectId}', $project->getId()) + ->setParam('{userId}', $user->getId()) + ->setParam('{userAgent}', $request->getUserAgent('')) + ->setParam('{ip}', $request->getIP()) + ->setParam('{url}', $request->getHostname() . $route->getPath()) + ->setParam('{method}', $request->getMethod()) + ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); + + foreach ($request->getParams() as $key => $value) { // Set request params as potential abuse keys + if (! empty($value)) { + $timeLimit->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value); + } + } + + $abuse = new Abuse($timeLimit); + $abuse->reset(); + } + }); + Http::shutdown() ->groups(['api']) ->inject('route') @@ -791,111 +903,12 @@ Http::shutdown() ->inject('response') ->inject('project') ->inject('user') - ->inject('queueForEvents') ->inject('auditContext') ->inject('publisherForAudits') - ->inject('publisherForFunctions') - ->inject('queueForWebhooks') - ->inject('queueForRealtime') - ->inject('dbForProject') - ->inject('timelimit') - ->inject('eventProcessor') ->inject('mode') - ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, callable $timelimit, EventProcessor $eventProcessor, string $mode) use ($parseLabel) { - + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, AuditContext $auditContext, Audit $publisherForAudits, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); - if (! empty($queueForEvents->getEvent())) { - if (empty($queueForEvents->getPayload())) { - $queueForEvents->setPayload($responsePayload); - } - - // Get project and function/webhook events (cached) - $functionsEvents = $eventProcessor->getFunctionsEvents($project, $dbForProject); - $webhooksEvents = $eventProcessor->getWebhooksEvents($project); - - // Generate events for this operation - $generatedEvents = Event::generateEvents( - $queueForEvents->getEvent(), - $queueForEvents->getParams() - ); - - $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); - if ($project->getId() !== 'console' || $allowedOnConsole) { - $queueForRealtime - ->from($queueForEvents) - ->trigger(); - } - - // Only trigger functions if there are matching function events - if (! empty($functionsEvents)) { - foreach ($generatedEvents as $event) { - if (isset($functionsEvents[$event])) { - $publisherForFunctions->enqueue(FunctionMessage::fromEvent( - event: $queueForEvents->getEvent(), - params: $queueForEvents->getParams(), - project: $queueForEvents->getProject(), - user: $queueForEvents->getUser(), - userId: $queueForEvents->getUserId(), - payload: $queueForEvents->getPayload(), - platform: $queueForEvents->getPlatform(), - )); - break; - } - } - } - - // Only trigger webhooks if there are matching webhook events - if (! empty($webhooksEvents)) { - foreach ($generatedEvents as $event) { - if (isset($webhooksEvents[$event])) { - $queueForWebhooks - ->from($queueForEvents) - ->trigger(); - break; - } - } - } - } - - /** - * Abuse labels - */ - $abuseEnabled = System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled'; - $abuseResetCode = $route->getLabel('abuse-reset', []); - $abuseResetCode = \is_array($abuseResetCode) ? $abuseResetCode : [$abuseResetCode]; - - if ($abuseEnabled && \count($abuseResetCode) > 0 && \in_array($response->getStatusCode(), $abuseResetCode)) { - $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); - $abuseKeyLabel = (! is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; - - foreach ($abuseKeyLabel as $abuseKey) { - $start = $request->getContentRangeStart(); - $end = $request->getContentRangeEnd(); - $timeLimit = $timelimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600)); - $timeLimit - ->setParam('{projectId}', $project->getId()) - ->setParam('{userId}', $user->getId()) - ->setParam('{userAgent}', $request->getUserAgent('')) - ->setParam('{ip}', $request->getIP()) - ->setParam('{url}', $request->getHostname() . $route->getPath()) - ->setParam('{method}', $request->getMethod()) - ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); - - foreach ($request->getParams() as $key => $value) { // Set request params as potential abuse keys - if (! empty($value)) { - $timeLimit->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value); - } - } - - $abuse = new Abuse($timeLimit); - $abuse->reset(); - } - } - - /** - * Audit labels - */ $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { $resource = $parseLabel($pattern, $responsePayload, $params, $user, $project); From 47e2ccd96a6b7629d8dd62ff37387a74fb4eed8d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 2 Jun 2026 02:24:30 +0000 Subject: [PATCH 24/25] fix: reset queued event on non-final chunk in createFile On chunked uploads, the createFile route registers a templated event label `buckets.[bucketId].files.[fileId].create` during init. On an intermediate (non-final) chunk the request returns 201 but the event params are never set, so App::shutdown() calls Event::generateEvents() with an unpopulated [bucketId] template and throws "[bucketId] is missing from the params". Reset the queued event in the non-final-chunk branch so no templated event is generated until the upload completes, matching the reset() already used by other early-exit paths in the same closure. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Platform/Modules/Storage/Http/Buckets/Files/Create.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php index 348b19c039..69b27af25c 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php @@ -497,6 +497,8 @@ class Create extends Action ->setParam('bucketId', $bucket->getId()) ->setParam('fileId', $file->getId()) ->setContext('bucket', $bucket); + } else { + $queueForEvents->reset(); } $metadata = null; // was causing leaks as it was passed by reference From 4fad6b781a5d93319ce9310878421973622f217d Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 2 Jun 2026 09:53:08 +0530 Subject: [PATCH 25/25] Update telemetry dependency stack --- composer.json | 4 +- composer.lock | 111 ++++++++++++++++++++++++++------------------------ 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 53c9287f23..3c0735d67d 100644 --- a/composer.json +++ b/composer.json @@ -67,7 +67,7 @@ "utopia-php/emails": "0.7.*", "utopia-php/dns": "1.7.*", "utopia-php/dsn": "0.2.1", - "utopia-php/http": "2.0.0-rc3", + "utopia-php/http": "^2.0@RC", "utopia-php/fetch": "^1.1", "utopia-php/validators": "0.2.*", "utopia-php/image": "0.8.*", @@ -85,7 +85,7 @@ "utopia-php/registry": "0.5.*", "utopia-php/storage": "2.*", "utopia-php/system": "0.10.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "^0.4", "utopia-php/vcs": "4.*", "utopia-php/websocket": "1.0.*", "matomo/device-detector": "6.4.*", diff --git a/composer.lock b/composer.lock index 8b6f619655..600fa9d7ff 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "53a96b051328aa5572b1008496733f85", + "content-hash": "5e251e32e77d0eeac8d0aad25188f08a", "packages": [ { "name": "adhocore/jwt", @@ -2712,16 +2712,16 @@ }, { "name": "symfony/http-client", - "version": "v7.4.9", + "version": "v7.4.13", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6" + "reference": "e8a112b8415707265a7e614278136a9d92989a6a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", - "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", + "url": "https://api.github.com/repos/symfony/http-client/zipball/e8a112b8415707265a7e614278136a9d92989a6a", + "reference": "e8a112b8415707265a7e614278136a9d92989a6a", "shasum": "" }, "require": { @@ -2789,7 +2789,7 @@ "http" ], "support": { - "source": "https://github.com/symfony/http-client/tree/v7.4.9" + "source": "https://github.com/symfony/http-client/tree/v7.4.13" }, "funding": [ { @@ -2809,7 +2809,7 @@ "type": "tidelift" } ], - "time": "2026-04-29T13:25:15+00:00" + "time": "2026-05-24T09:57:54+00:00" }, { "name": "symfony/http-client-contracts", @@ -2980,16 +2980,16 @@ }, { "name": "symfony/polyfill-php82", - "version": "v1.37.0", + "version": "v1.38.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php82.git", - "reference": "34808efe3e68f69685796f7c253a2f1d8ea9df59" + "reference": "002dc0cfe5fd4ed6033d48f27d4f19a486c4b04b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/34808efe3e68f69685796f7c253a2f1d8ea9df59", - "reference": "34808efe3e68f69685796f7c253a2f1d8ea9df59", + "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/002dc0cfe5fd4ed6033d48f27d4f19a486c4b04b", + "reference": "002dc0cfe5fd4ed6033d48f27d4f19a486c4b04b", "shasum": "" }, "require": { @@ -3036,7 +3036,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php82/tree/v1.37.0" + "source": "https://github.com/symfony/polyfill-php82/tree/v1.38.1" }, "funding": [ { @@ -3056,20 +3056,20 @@ "type": "tidelift" } ], - "time": "2026-04-10T16:19:22+00:00" + "time": "2026-05-26T12:45:58+00:00" }, { "name": "symfony/polyfill-php83", - "version": "v1.37.0", + "version": "v1.38.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php83.git", - "reference": "3600c2cb22399e25bb226e4a135ce91eeb2a6149" + "reference": "8339098cae28673c15cce00d80734af0453054e2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/3600c2cb22399e25bb226e4a135ce91eeb2a6149", - "reference": "3600c2cb22399e25bb226e4a135ce91eeb2a6149", + "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/8339098cae28673c15cce00d80734af0453054e2", + "reference": "8339098cae28673c15cce00d80734af0453054e2", "shasum": "" }, "require": { @@ -3116,7 +3116,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.37.0" + "source": "https://github.com/symfony/polyfill-php83/tree/v1.38.1" }, "funding": [ { @@ -3136,7 +3136,7 @@ "type": "tidelift" } ], - "time": "2026-04-10T17:25:58+00:00" + "time": "2026-05-26T12:51:13+00:00" }, { "name": "symfony/polyfill-php85", @@ -3669,16 +3669,16 @@ }, { "name": "utopia-php/circuit-breaker", - "version": "0.3.0", + "version": "0.3.1", "source": { "type": "git", "url": "https://github.com/utopia-php/circuit-breaker.git", - "reference": "064243c1667778c00abf027ff53a735a228776de" + "reference": "db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/circuit-breaker/zipball/064243c1667778c00abf027ff53a735a228776de", - "reference": "064243c1667778c00abf027ff53a735a228776de", + "url": "https://api.github.com/repos/utopia-php/circuit-breaker/zipball/db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828", + "reference": "db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828", "shasum": "" }, "require": { @@ -3688,7 +3688,7 @@ "laravel/pint": "^1.29", "phpstan/phpstan": "^2.1", "phpunit/phpunit": "^10.0", - "utopia-php/telemetry": "0.2.*" + "utopia-php/telemetry": "^0.4" }, "suggest": { "ext-opentelemetry": "Required by utopia-php/telemetry when using OpenTelemetry metrics.", @@ -3725,9 +3725,9 @@ ], "support": { "issues": "https://github.com/utopia-php/circuit-breaker/issues", - "source": "https://github.com/utopia-php/circuit-breaker/tree/0.3.0" + "source": "https://github.com/utopia-php/circuit-breaker/tree/0.3.1" }, - "time": "2026-05-12T04:27:08+00:00" + "time": "2026-05-29T12:12:23+00:00" }, { "name": "utopia-php/cli", @@ -4346,16 +4346,16 @@ }, { "name": "utopia-php/http", - "version": "2.0.0-rc3", + "version": "2.0.0-rc4", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "e1a51a2eb906a3d461378fb551a647ef01714f1c" + "reference": "0396a959c6a3be7c16b4b72283347327a75d1f92" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/e1a51a2eb906a3d461378fb551a647ef01714f1c", - "reference": "e1a51a2eb906a3d461378fb551a647ef01714f1c", + "url": "https://api.github.com/repos/utopia-php/http/zipball/0396a959c6a3be7c16b4b72283347327a75d1f92", + "reference": "0396a959c6a3be7c16b4b72283347327a75d1f92", "shasum": "" }, "require": { @@ -4363,7 +4363,7 @@ "utopia-php/compression": "0.1.*", "utopia-php/di": "0.3.*", "utopia-php/servers": "0.4.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "^0.4", "utopia-php/validators": "0.2.*" }, "require-dev": { @@ -4396,9 +4396,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/2.0.0-rc3" + "source": "https://github.com/utopia-php/http/tree/2.0.0-rc4" }, - "time": "2026-05-21T14:42:41+00:00" + "time": "2026-06-02T04:17:22+00:00" }, { "name": "utopia-php/image", @@ -4976,25 +4976,26 @@ }, { "name": "utopia-php/queue", - "version": "0.18.4", + "version": "0.18.6", "source": { "type": "git", "url": "https://github.com/utopia-php/queue.git", - "reference": "1323b52f39b899c580dbb7af8562fc53d4e13e62" + "reference": "954cd3788b37697b83fd2757adcd93422f21a846" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/queue/zipball/1323b52f39b899c580dbb7af8562fc53d4e13e62", - "reference": "1323b52f39b899c580dbb7af8562fc53d4e13e62", + "url": "https://api.github.com/repos/utopia-php/queue/zipball/954cd3788b37697b83fd2757adcd93422f21a846", + "reference": "954cd3788b37697b83fd2757adcd93422f21a846", "shasum": "" }, "require": { "php": ">=8.3", "php-amqplib/php-amqplib": "^3.7", "utopia-php/di": "0.3.*", + "utopia-php/lock": "0.2.*", "utopia-php/pools": "1.*", "utopia-php/servers": "0.4.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "0.4.*", "utopia-php/validators": "0.2.*" }, "require-dev": { @@ -5036,9 +5037,9 @@ ], "support": { "issues": "https://github.com/utopia-php/queue/issues", - "source": "https://github.com/utopia-php/queue/tree/0.18.4" + "source": "https://github.com/utopia-php/queue/tree/0.18.6" }, - "time": "2026-05-21T14:43:24+00:00" + "time": "2026-06-01T19:47:57+00:00" }, { "name": "utopia-php/registry", @@ -5192,16 +5193,16 @@ }, { "name": "utopia-php/storage", - "version": "2.0.3", + "version": "2.0.4", "source": { "type": "git", "url": "https://github.com/utopia-php/storage.git", - "reference": "37129cf0bfcc03210172000e4388d4d3495ae013" + "reference": "a604b0ee011c4f366d53167caf2cf6b0f1d62d7e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/37129cf0bfcc03210172000e4388d4d3495ae013", - "reference": "37129cf0bfcc03210172000e4388d4d3495ae013", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/a604b0ee011c4f366d53167caf2cf6b0f1d62d7e", + "reference": "a604b0ee011c4f366d53167caf2cf6b0f1d62d7e", "shasum": "" }, "require": { @@ -5211,7 +5212,7 @@ "ext-zlib": "*", "php": ">=8.1", "utopia-php/system": "0.*.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "^0.4", "utopia-php/validators": "0.2.*" }, "require-dev": { @@ -5238,9 +5239,9 @@ ], "support": { "issues": "https://github.com/utopia-php/storage/issues", - "source": "https://github.com/utopia-php/storage/tree/2.0.3" + "source": "https://github.com/utopia-php/storage/tree/2.0.4" }, - "time": "2026-05-15T09:42:32+00:00" + "time": "2026-06-02T04:17:22+00:00" }, { "name": "utopia-php/system", @@ -5300,16 +5301,16 @@ }, { "name": "utopia-php/telemetry", - "version": "0.2.0", + "version": "0.4.0", "source": { "type": "git", "url": "https://github.com/utopia-php/telemetry.git", - "reference": "9997ebf59bb77920a7223ad73d834a76b09152c3" + "reference": "e0630df7d8176833cd4882f78814a5b893dcb0e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/9997ebf59bb77920a7223ad73d834a76b09152c3", - "reference": "9997ebf59bb77920a7223ad73d834a76b09152c3", + "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/e0630df7d8176833cd4882f78814a5b893dcb0e0", + "reference": "e0630df7d8176833cd4882f78814a5b893dcb0e0", "shasum": "" }, "require": { @@ -5349,9 +5350,9 @@ ], "support": { "issues": "https://github.com/utopia-php/telemetry/issues", - "source": "https://github.com/utopia-php/telemetry/tree/0.2.0" + "source": "https://github.com/utopia-php/telemetry/tree/0.4.0" }, - "time": "2025-12-17T07:56:38+00:00" + "time": "2026-05-29T11:57:04+00:00" }, { "name": "utopia-php/validators", @@ -8550,7 +8551,9 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": {}, + "stability-flags": { + "utopia-php/http": 5 + }, "prefer-stable": true, "prefer-lowest": false, "platform": {