Skip to content

fix(sites): bind branch preview rules and build state to their project - #14786

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/preview-rule-ownership
Oct 22, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
fix/preview-rule-ownership

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What

Two hardening changes around site/function builds.

Branch preview rules

  • Compute\Base::activateBranchPreviewRule() updated the existing rule by md5(domain) on a duplicate without checking who owns it. It now loads the rule and repoints it only when projectInternalId, deploymentResourceType = site, deploymentResourceInternalId and deploymentVcsProviderBranch all match; otherwise it logs and skips the preview rule (manual branch-pinned rules still follow the deployment as before).
  • BranchDomain now hashes projectId . ':' . resourceId into 16 hex characters (was 7 characters of the unseparated concatenation). The domain label stays well under 63 characters.

Backwards compatibility: changing the hash alone would give every existing branch a new preview URL on its next build and freeze the old URL at its last deployment. To avoid that, the new Base::branchDomain() first computes the legacy domain ('legacy' => true on the filter) and keeps it when a rule for that domain already belongs to the same project, site and branch. Branches with an existing preview keep their URL; new branches get the wider hash. All three places that mint the branch preview rule (redeployVcsSite, the VCS push trait, and the jobs worker activation) use the same resolution.

Jobs worker build state

  • Cache markers (event, manifest, output, exit, complete) are now jobs-<marker>-<projectInternalId>-<id>, and the per-deployment lock is Deployments::lock($projectInternalId, $deploymentId), used by the worker and both Functions/Sites deployment cancel endpoints so writers and readers stay consistent.
  • Builds in flight during the rollout wrote their markers under the old keys; such a build may need a redeploy if its callbacks straddle the upgrade.

Tests

  • tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php: duplicate rule of the same branch is repointed; duplicate rule of another project, site, branch or a function is left alone; a branch with a legacy-format rule keeps its domain; a new branch gets the 16-character hash.
  • tests/unit/Filter/BranchDomainTest.php: separator makes site1/23 and site12/3 distinct; legacy output is pinned to the previous value.
  • tests/unit/Deployment/DeploymentsTest.php: the lock key differs per project.

An e2e test for the duplicate-rule check is not practical: it needs two projects whose site and project IDs produce the same branch domain plus a real VCS-triggered build, which the CI lanes cannot set up. The ownership logic is covered at the activateBranchPreviewRule() level instead, and the existing Sites/VCS e2e lanes cover the unchanged happy path. I did not run the docker e2e stack locally; relying on CI lanes.

Related: appwrite-labs/cloud#6673

Branch preview rules live in the shared platform rules collection, keyed
by md5 of the domain. On a duplicate, activation now repoints the rule
only when it belongs to the same project, site and branch, and logs and
skips otherwise. New preview domains hash the project and site IDs with
a separator into 16 hex characters; a branch that already has a rule
under the old 7-character hash keeps that domain so existing preview
URLs keep updating.

The jobs worker's build-state cache markers and the per-deployment lock
are now namespaced by the project's internal ID, in the worker and in
both cancel endpoints, so deployments with the same ID in different
projects cannot share state.
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Renamed cache markers strand builds across rolling deploys — Jobs.php:601

The change scopes deployment locks and dedupe/join cache keys per project (keying on the project sequence), aligns the cancel path with the new lock keys, and extracts a shared branchDomain() helper in Compute/Base that both the GitHub webhook path and function jobs use for legacy-hash resolution and preview-rule ownership checks. Unit tests cover Deployments, the BranchDomain filter, and branch preview rules. The main noted risk is the rolling-deploy window where old and new workers write/read differently-named cache markers.

Files Change
src/Appwrite/Deployment/Deployments.php Updates deployment helper used by jobs and status flows.
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Re-keys lock and marker cache keys with the project sequence.
src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php, src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php Aligns the cancel path with the new lock keys.
src/Appwrite/Platform/Modules/Compute/Base.php, src/Appwrite/Filter/BranchDomain.php, src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php Extracts the shared branchDomain() helper and uses it in the webhook path.
tests/unit/** Adds unit tests for Deployments, the BranchDomain filter, and branch preview rules.

Reviewed commit: 3842227

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Branch preview domains now use a wider project/resource hash while retaining existing legacy URLs for rules owned by the same site and branch. Build callback cache markers and deployment locks are scoped by project, with the cancellation endpoints using the same lock key as the worker.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 10
File Change
src/Appwrite/Deployment/Deployments.php Adds a shared project-scoped deployment lock-key helper.
src/Appwrite/Filter/BranchDomain.php Adds the wider separated project/resource hash and legacy hash option.
src/Appwrite/Platform/Modules/Compute/Base.php Resolves legacy branch URLs and verifies rule ownership before repointing.
src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php Uses the shared project-scoped lock for cancellation.
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Scopes callback cache markers and deployment locking by project.
src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php Uses the shared project-scoped lock for cancellation.
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php Uses the shared legacy-aware branch-domain resolver when creating previews.
tests/unit/Deployment/DeploymentsTest.php Checks that deployment lock keys differ across projects.
tests/unit/Filter/BranchDomainTest.php Covers separated resource hashes and the legacy hash output.
tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php Covers ownership checks, duplicate rules, and legacy preview URL retention.

Reviewed 3842227 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

Comment thread src/Appwrite/Deployment/Deployments.php Resolved

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR re-keys deployment lock and join-marker cache keys with the project sequence and refactors the branch preview-domain lookup into Compute/Base, with unit tests.

Key findings

  • 🟡 Renamed cache markers strand builds across rolling deploys — Jobs.php:601

Comment on lines +601 to 612
if ($cache->load(self::key('exit', $project, $deploymentId), self::DEDUPE_TTL) === false || $cache->load(self::key('complete', $project, $deploymentId), self::DEDUPE_TTL) === false) {
return $deployment;
}

if ($deviceForBuilds->getType() !== DeviceType::Local && $cache->load('jobs-output-' . $deploymentId, self::DEDUPE_TTL) === false) {
if ($deviceForBuilds->getType() !== DeviceType::Local && $cache->load(self::key('output', $project, $deploymentId), self::DEDUPE_TTL) === false) {
return $deployment;
}

$manifest = $isSite ? $cache->load('jobs-manifest-' . $deploymentId, self::DEDUPE_TTL) : null;
$manifest = $isSite ? $cache->load(self::key('manifest', $project, $deploymentId), self::DEDUPE_TTL) : null;
if ($isSite && $manifest === false) {
return $deployment;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Renamed cache markers strand builds across rolling deploys

This PR re-keys the join markers (jobs-exit-, jobs-complete-, jobs-output-, jobs-manifest-) from jobs-<marker>-<id> to jobs-<marker>-<sequence>-<id> via self::key() (src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:601-612). During a rolling deploy, an old-version pod writes terminal markers under the legacy unscoped names while a new-version pod's ready() join reads only the project-scoped names, so ready() never sees both markers and the deployment stays building. Because the dedupe key (jobs-event-) was renamed too, the old pod marks the CloudEvent id under a key the new pod does not honor, so at-least-once redelivery does not reliably rescue the join. The deployment remains stuck until a user cancels or redeploys.

📋 Prompt for AI Agents

In src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php, make self::key() callers (the marker saves at lines 364, 386, 485, 512 and the ready() loads at lines 601-609) tolerant of mixed versions for one release: when a versioned key (jobs-{marker}-{sequence}-{id}) is missing on load, fall back to the legacy key jobs-{marker}-{id}, and on save write both keys. Apply the same dual-write/dual-read to the jobs-event- dedupe check at line 166. Remove the fallbacks in the next release. This prevents in-flight builds from being stranded in building during a rolling deploy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving out the dual-read/dual-write on purpose. It would only help builds whose callbacks arrive during a rollout, with some handled by old pods and some by new ones. Those builds can stay in building until they are redeployed or canceled. The PR body already says so. Self-hosted upgrades recreate all containers at once, so a build there is interrupted either way. A fallback for one release would be temporary legacy code in the join path, and I would rather not ship it for a window that short. If maintainers prefer the fallback for Cloud rollouts, I can add it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a reasonable engineering trade-off, and the reasoning is clear. The risk is real but bounded: only in-flight builds whose callbacks are split across old and new pods during a rolling deploy window would be affected, and those can be recovered by redeploying. If the team is comfortable accepting that narrow breakage window for Cloud rollouts, the finding can stay as a known limitation rather than a code change. I'll leave this open since no code fix has been applied — feel free to close it manually if maintainers accept the risk.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/preview-rule-ownership (after).

Metric Before After Change
🚀 Requests/sec 179.45 192.36 🟢 +7.2%
⏱️ Latency P50 95.91 ms 88.7 ms 🟢 -7.5%
⏱️ Latency P95 228.12 ms 212.08 ms 🟢 -7%
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 88.7 212.08 12,141 192.36 -16.03
Account 166.97 319.04 639 10.59 -47.46
TablesDB 85.58 168.47 6,603 107.52 -9.19
Storage 81.44 194.86 3,195 53.84 -8.94
Functions 125.22 252.43 1,704 29.41 -44.59

Top API waits (after)

API request Max wait (ms)
functions.create 482.76
account.prefs.update 454.7
account.name.update 441.94
functions.variables.update 418.88
tablesdb.rows.create 404.21

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@ChiragAgg5k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit 9fc6d27 into main Oct 22, 2026
136 of 138 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/preview-rule-ownership branch October 22, 2026 19:44
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x.

(cherry picked from commit 9fc6d27)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x.

(cherry picked from commit 9fc6d27)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x.

(cherry picked from commit 9fc6d27)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x.

(cherry picked from commit 9fc6d27)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…rship

fix(sites): bind branch preview rules and build state to their project

(cherry picked from commit 9fc6d27)
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.

1 participant