Skip to content

fix(functions): rebind generated branch domain on deployment activation - #13509

Merged
ArnabChatterjee20k merged 2 commits into
mainfrom
fix-vcs-deployment-id-missing-function
Sep 7, 2026
Merged

ArnabChatterjee20k merged 2 commits into
mainfrom
fix-vcs-deployment-id-missing-function

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

What

When a Git-connected function activates a deployment, the worker now also repoints the generated "Deployed from <branch>" domain (the branch-scoped rule) at that deployment — not only the branch-agnostic custom / per-deployment domains.

Problem

Reported from Cloud (region sfo, *.appwrite.network):

When connecting a Git repo to a Function, if the first deployment fails to build, the auto-generated branch domain (<hash>.appwrite.network, Deployed from main) stays permanently on 404 router_rule_not_found — even after a later deployment builds successfully and becomes Active. Adding a new custom domain resolves instantly (200, x-edge-rule-cache: hit).

The edge router is innocent here: the stuck domain returns x-edge-rule-cache: miss, so the edge queries Cloud fresh every time and Cloud has no rule bound to a deployment for that host. There is no negative caching at the edge — it faithfully relays Cloud's 404.

Root cause

A function's generated branch domain is a rules document with:

  • trigger = 'manual'
  • deploymentVcsProviderBranch = '<branch>' (e.g. main)
  • deploymentId = '' when it is created before any successful build (the classic trigger being an initial build failure, but also the ordinary first-deploy race)

On a successful, auto-activated build, Functions\Workers\Jobs::activate() walks the resource's manual rules and stamps the new deploymentId onto them — but its query filtered deploymentVcsProviderBranch => [''], i.e. only branch-agnostic rules. The branch-scoped generated domain never matched, so its deploymentId stayed empty and the host kept 404ing.

A manually added custom domain works because it is created after a deployment is live (so it is born already bound) and typically carries an empty branch — exactly the rows activate() already handled.

Fix

Include the activating deployment's own providerBranch in the rebind query:

$branch = $deployment->getAttribute('providerBranch', '');
$branches = $branch === '' ? [''] : ['', $branch];
// ...
Query::equal('deploymentVcsProviderBranch', $branches),
  • VCS deployment from main → rebinds ['', 'main']: branch-agnostic domains and the Deployed from main domain.
  • Non-VCS / manual deployment (branch = '') → unchanged (['']), so a domain for some other branch is never dragged onto it.

The rebind stays gated on activation (activate === true), matching the reported expectation that the branch domain should follow the deployment that becomes Active.

Tests

tests/unit/Platform/Modules/Functions/Workers/ActivateRulesTest.php:

  • testActivateRebindsGeneratedBranchDomain — a VCS deployment scopes the rebind to ['', 'main'] for function rules.
  • testActivateOfNonVcsDeploymentOnlyTouchesBranchAgnosticRules — a branchless deployment stays scoped to [''].
  • testActivateRepointsMatchedRuleAtTheNewDeployment — a matched rule's persisted deploymentId is set to the activating deployment and a RuleUpdated event is emitted.

composer format, composer lint, and composer analyze pass on the changed files.

Notes / follow-ups

  • This fixes forward: new activations rebind correctly. Domains already orphaned in production need a one-off backfill — the existing PatchBackfillRuleDeploymentIds task only matches synthetic <type>:<resourceId> deploymentIds via Query::contains('deploymentId', [':']), not these empty-string ones, so it won't catch them as-is.
  • Cloud ships an activate() override (src/Appwrite/Cloud/Platform/Workers/Jobs.php) carrying the same deploymentVcsProviderBranch => [''] filter; it needs the identical change to take effect on Cloud.

A Git-connected function's generated "Deployed from <branch>" domain is a
manual rule stored under its branch. When it is created before the first
successful build — e.g. after an initial build fails — the rule is born with
an empty deploymentId, and activate() only rebound rules with an empty
deploymentVcsProviderBranch, so the branch rule was skipped and the domain
stayed stuck on router_rule_not_found even after a later build activated.

Include the activating deployment's own branch in the rule rebind query so the
generated branch domain follows activation alongside the branch-agnostic
custom and per-deployment domains.
@greptile-apps

greptile-apps Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates function deployment activation so generated branch-scoped domains are rebound to the newly active deployment.

  • Includes both branch-agnostic rules and rules matching the deployment’s VCS provider branch.
  • Preserves branch-agnostic-only behavior for non-VCS deployments.
  • Adds unit coverage for query scoping, persisted deployment rebinding, and update-event dispatch.

Confidence Score: 5/5

The PR appears safe to merge; the branch-scoped rebinding is narrowly constrained and no actionable regressions remain.

The activating deployment’s branch is added without removing the existing branch-agnostic match, and branchless deployments retain the prior behavior. Resource identity, resource type, and manual-trigger constraints remain intact, with targeted tests covering the changed behavior.

Important Files Changed

Filename Overview
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Expands activation-time rule selection to include the activating deployment’s provider branch while retaining resource, type, and trigger constraints.
tests/unit/Platform/Modules/Functions/Workers/ActivateRulesTest.php Adds focused tests for VCS and non-VCS branch filtering, deployment ID persistence, and RuleUpdated event dispatch.

Reviews (2): Last reviewed commit: "test(functions): satisfy Rector on activ..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-vcs-deployment-id-missing-function (after).

Metric Before After Change
🚀 Requests/sec 179.04 175.22 ⚪ -2.1%
⏱️ Latency P50 99.57 ms 101.21 ms ⚪ +1.6%
⏱️ Latency P95 218.82 ms 225.42 ms ⚪ +3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 101.21 225.42 11,343 175.22 +6.6
Account 194.9 342.14 597 9.89 +16.34
TablesDB 100.88 179.65 6,169 97.89 +4.46
Storage 86.22 186.49 2,985 48.92 -1.95
Functions 140.54 273.97 1,592 26.78 +13.53

Top API waits (after)

API request Max wait (ms)
functions.variables.update 563.31
functions.create 519.8
account.name.update 465.92
storage.files.preview 450.27
tablesdb.rows.decrement 437.28

@ArnabChatterjee20k
ArnabChatterjee20k merged commit 8400b1b into main Sep 7, 2026
47 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Sep 10, 2026
2 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants