Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
"scripts": {
"build": "node ./node_modules/typescript/bin/tsc -b packages/runtime-core packages/runtime-playground packages/cli && node scripts/ensure-cli-bin-executable.mjs",
"cloudflare:dry-run": "npm exec -- wrangler deploy --dry-run --config packages/runtime-cloudflare/wrangler.jsonc",
"cloudflare:dry-run:d1": "npm exec -- wrangler deploy --dry-run --config packages/runtime-cloudflare/wrangler.d1.jsonc",
"cloudflare:local-gate": "node scripts/cloudflare-local-gate.mjs",
"cloudflare:local-gate:d1": "node scripts/cloudflare-local-gate.mjs --coordinator=d1",
"postinstall": "patch-package",
"generate:cloudflare-wordpress-runtime-corpus": "tsx scripts/generate-cloudflare-wordpress-runtime-corpus.ts",
"provision:cloudflare-wordpress-runtime-corpus": "node scripts/provision-cloudflare-wordpress-runtime-corpus.mjs",
Expand Down
10 changes: 6 additions & 4 deletions packages/runtime-cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ This candidate integration for [wp-codebox#1838](https://github.com/Automattic/w

## Runtime Architecture

The entry Worker executes PHP-WASM and WordPress. The named `WordPressStateCoordinator` Durable Object remains lightweight: it serializes a bounded lease and atomically promotes the current canonical R2 pointer with token, base-revision, and version checks. It never imports or instantiates PHP-WASM. State reads query the coordinator directly; every request that can observe canonical WordPress state acquires a lease first.
The entry Worker executes PHP-WASM and WordPress. Runtime behavior depends on the typed `RevisionCoordinator` contract rather than a Cloudflare storage product. The standard `worker-do.ts` entrypoint injects the existing `WordPressStateCoordinator` Durable Object adapter. The ChatGPT Sites-compatible `worker-d1.ts` entrypoint injects a D1 adapter that stores only the current pointer, version, lease token, lease base, and expiry in one conditional-update row. Both adapters serialize the same bounded lease and CAS promotion semantics; neither imports or instantiates PHP-WASM. MDI, disposable SQLite, and canonical R2 storage are unchanged.

On cold start, the entry Worker uses the acquired pointer to rebuild PHP-WASM's disposable SQLite index from canonical MDI Markdown and JSON files. A missing pointer materializes the packaged canonical MDI seed and boots one PHP-WASM primary runtime. The build-time PHP CLI generator creates that archive from `wordpress-install-seed.sqlite` through MDI's public `bootstrap_existing_cache()` API, validates its pinned MDI revision and input digest, and never packages SQLite. The runtime updates `siteurl` and `home` through WordPress APIs using the request origin and sets the admin password from `WORDPRESS_ADMIN_PASSWORD`; only WordPress's password hash is canonical. Bootstrap persists and CAS-promotes this mutation before serving the next request.

Canonical browser, health, and mutation boots require the separately managed `WORDPRESS_AUTH_SECRET` Worker secret. The entry Worker derives the eight WordPress auth keys and salts from that secret with a versioned, site-scoped (`default`) SHA-256 domain separator before `wp-load.php`. It never logs, persists, or returns the secret or derived values. Configure it independently from the bootstrap password with `wrangler secret put WORDPRESS_AUTH_SECRET --config packages/runtime-cloudflare/wrangler.jsonc`; rotating `WORDPRESS_ADMIN_PASSWORD` does not rotate authentication salts or invalidate sessions.

After each mutating HTTP request, the entry runtime invokes MDI's explicit request-boundary flush, collects canonical files, stores immutable content-addressed R2 objects and a revision manifest, then commits the new pointer through the held lease. GET, HEAD, and asset requests release without promotion. Failed requests abort their leases; stale leases recover by token/version/expiry checks. The entry isolate can cache one runtime only for the exact acquired pointer revision and exits it after promotion or when another isolate advances the pointer. It does not persist SQLite. Existing manifests are reused when canonical file hashes have not changed.

Anonymous HTML responses are stored as create-once, host-independent R2 artifacts under their canonical revision. An authorized `POST ?phase=operator-publish` promotes a bounded, sorted set of already-rendered routes by writing an immutable publication descriptor and then replacing one strongly consistent `sites/default/publications/current.json` pointer. Published GET and HEAD requests check a 60-second edge entry and then the R2 publication before constructing the Durable Object stub; authenticated, preview, admin, REST, and unpublished routes continue through WordPress. Promotion rejects missing, stale, duplicate, malformed, or conflicting artifacts. This is the coordinator-free reader boundary, not yet a complete route compiler or global CDN purge pipeline.

The same revision transaction persists bounded user-managed files under `wp-content/plugins`, `themes`, `languages`, and `mu-plugins`. Runtime-owned MDI, SQLite integration, and Codebox adapter files remain reconstructable artifacts and are excluded. Unchanged bundled-theme files are omitted by release hash, while modified files become canonical overrides. Public canonical plugin and theme assets serve directly from revision-addressed R2 cache entries before the immutable release corpus. Nonce-protected `wp-admin` GET actions such as plugin activation are classified as mutations so their filesystem and MDI option changes commit atomically. Existing revisions without `wpContent` remain valid.

The Worker forwards browser cookies directly to Playground and disables Playground's internal cookie store, preventing an empty per-isolate store from replacing a valid browser session after cold restart.
Expand All @@ -27,8 +29,8 @@ Canonical Cloudflare boots patch only the assembled PHP MEMFS copy of `/wordpres

1. Run `npm run generate:cloudflare-canonical-mdi-seed` and `npm run generate:cloudflare-wordpress-runtime-corpus` to regenerate deterministic runtime artifacts and manifests.
2. Run `npm run provision:cloudflare-wordpress-runtime-corpus -- --local --persist-to <directory>` to verify and upload all exact content-addressed artifacts into isolated local R2 storage. For an authorized deployment, run the provisioner with `--remote` and require every upload to succeed before deploying the Worker that imports their manifests.
3. Run `npm run test:cloudflare-runtime` for routing, canonical-state, artifact validation, source contract, and TypeScript coverage.
4. Run `npm run cloudflare:dry-run` to compile the Worker without creating Cloudflare resources.
5. Run `npm run cloudflare:local-gate` for isolated local workerd evidence. It generates and provisions all artifacts before workerd starts, then injects stable test-only admin-password and auth-secret values, uploads and activates a real plugin ZIP, verifies that plugin's REST route before and after Worker restart, and covers login, authenticated REST publication, media, public rendering, representative frontend/admin/editor assets, PHP diagnostics, session recovery, and a fresh login after restart.
3. Run `npm run test:cloudflare-runtime` for routing, coordinator composition, canonical-state, artifact validation, source contract, and TypeScript coverage.
4. Run `npm run cloudflare:dry-run` and `npm run cloudflare:dry-run:d1` to compile the Durable Object and D1 profiles without creating Cloudflare resources. The placeholder D1 database ID is for local/dry-run verification; an implementation supplies its provisioned binding at deployment.
5. Run `npm run cloudflare:local-gate` and `npm run cloudflare:local-gate:d1` for the same isolated workerd workflow through both coordinator implementations. Each gate generates and provisions all artifacts, verifies the selected backend through the state envelope, injects stable test-only admin-password, auth-secret, and operator-token values, uploads and activates a real plugin ZIP, promotes anonymous homepage and canonical-permalink artifacts, and proves an R2 publication read after Worker restart. They also cover login, concurrent canonical writes, authenticated REST publication, media, representative frontend/admin/editor assets, PHP diagnostics, session recovery, cron, and a fresh login after restart.

This document describes local candidate verification only. It does not claim remote deployment.
163 changes: 163 additions & 0 deletions packages/runtime-cloudflare/src/d1-revision-coordinator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { RevisionConflict, type MarkdownPointer, type RevisionCoordinator, type RevisionLease, type RevisionState } from "./revision-coordinator.js"

interface StateRow {
revision: string | null
manifest_key: string | null
persisted_at: string | null
version: number
lease_token: string | null
lease_base_revision: string | null
lease_version: number | null
lease_expires_at: number | null
}

const SITE_ID = "default"
const LEASE_MS = 90_000
const schemaReady = new WeakMap<object, Promise<void>>()

export class D1RevisionCoordinator implements RevisionCoordinator {
constructor(private readonly database: D1Database, private readonly leaseMs = LEASE_MS) {}

state(): Promise<RevisionState> {
return readWordPressState(this.database)
}

acquire(): Promise<RevisionLease> {
return beginStateLease(this.database, this.leaseMs)
}

async release(lease: RevisionLease): Promise<void> {
await releaseStateLease(this.database, lease)
}

async abort(lease: RevisionLease): Promise<void> {
await abortStateLease(this.database, lease)
}

commit(lease: RevisionLease, pointer: MarkdownPointer): Promise<{ pointer: MarkdownPointer; version: number }> {
return commitStateLease(this.database, lease, pointer)
}

async reset(): Promise<void> {
await resetWordPressState(this.database)
}
}

async function readWordPressState(database: D1Database): Promise<RevisionState> {
await ensureSchema(database)
const row = await readRow(database)
return {
schema: "wp-codebox/cloudflare-wordpress-state/v2",
store: "d1",
pointer: pointerFromRow(row),
version: row.version,
}
}

async function beginStateLease(database: D1Database, leaseMs = LEASE_MS): Promise<RevisionLease> {
await ensureSchema(database)
const now = Date.now()
const token = crypto.randomUUID()
const expiresAt = now + leaseMs
const result = await database.prepare(`UPDATE wp_codebox_state
SET lease_token = ?, lease_base_revision = revision, lease_version = version, lease_expires_at = ?
WHERE site_id = ? AND (lease_token IS NULL OR lease_expires_at <= ?)`)
.bind(token, expiresAt, SITE_ID, now).run()
if (result.meta.changes !== 1) {
const active = await readRow(database)
throw new RevisionConflict("A canonical WordPress lease is active.", active.lease_expires_at ?? undefined)
}
const row = await readRow(database)
if (row.lease_token !== token || row.lease_version === null) throw new RevisionConflict("The canonical WordPress lease was not acquired.")
return { token, pointer: pointerFromRow(row), version: row.lease_version, expiresAt }
}

async function releaseStateLease(database: D1Database, lease: RevisionLease): Promise<{ released: true }> {
await finishLease(database, lease, "release")
return { released: true }
}

async function abortStateLease(database: D1Database, lease: RevisionLease): Promise<{ aborted: true }> {
await finishLease(database, lease, "abort")
return { aborted: true }
}

async function commitStateLease(database: D1Database, lease: RevisionLease, pointer: MarkdownPointer): Promise<{ pointer: MarkdownPointer; version: number }> {
validatePointer(pointer)
await ensureSchema(database)
const baseRevision = lease.pointer?.revision ?? null
const result = await database.prepare(`UPDATE wp_codebox_state
SET revision = ?, manifest_key = ?, persisted_at = ?, version = version + 1,
lease_token = NULL, lease_base_revision = NULL, lease_version = NULL, lease_expires_at = NULL
WHERE site_id = ? AND lease_token = ? AND lease_expires_at > ? AND version = ? AND lease_version = ?
AND ((revision IS NULL AND ? IS NULL) OR revision = ?)
AND ((lease_base_revision IS NULL AND ? IS NULL) OR lease_base_revision = ?)`)
.bind(pointer.revision, pointer.manifestKey, pointer.persistedAt, SITE_ID, lease.token, Date.now(), lease.version, lease.version,
baseRevision, baseRevision, baseRevision, baseRevision).run()
if (result.meta.changes !== 1) throw new RevisionConflict("The canonical pointer changed before D1 promotion.")
return { pointer, version: lease.version + 1 }
}

async function resetWordPressState(database: D1Database): Promise<{ reset: true }> {
await ensureSchema(database)
await database.prepare(`UPDATE wp_codebox_state
SET revision = NULL, manifest_key = NULL, persisted_at = NULL, version = version + 1,
lease_token = NULL, lease_base_revision = NULL, lease_version = NULL, lease_expires_at = NULL
WHERE site_id = ?`).bind(SITE_ID).run()
return { reset: true }
}

async function finishLease(database: D1Database, lease: RevisionLease, action: "release" | "abort"): Promise<void> {
await ensureSchema(database)
const result = await database.prepare(`UPDATE wp_codebox_state
SET lease_token = NULL, lease_base_revision = NULL, lease_version = NULL, lease_expires_at = NULL
WHERE site_id = ? AND lease_token = ? AND lease_expires_at > ?`)
.bind(SITE_ID, lease.token, Date.now()).run()
if (result.meta.changes !== 1) throw new RevisionConflict(`The canonical WordPress lease cannot ${action} because it expired or changed.`)
}

async function readRow(database: D1Database): Promise<StateRow> {
const row = await database.prepare(`SELECT revision, manifest_key, persisted_at, version,
lease_token, lease_base_revision, lease_version, lease_expires_at
FROM wp_codebox_state WHERE site_id = ?`).bind(SITE_ID).first<StateRow>()
if (!row) throw new Error("D1 WordPress state row is unavailable.")
return row
}

function pointerFromRow(row: StateRow): MarkdownPointer | null {
if (row.revision === null && row.manifest_key === null && row.persisted_at === null) return null
const pointer = { revision: row.revision, manifestKey: row.manifest_key, persistedAt: row.persisted_at }
validatePointer(pointer)
return pointer as MarkdownPointer
}

function validatePointer(pointer: unknown): asserts pointer is MarkdownPointer {
if (!pointer || typeof pointer !== "object") throw new RevisionConflict("A complete canonical pointer is required for D1 promotion.")
const candidate = pointer as Partial<MarkdownPointer>
if (typeof candidate.revision !== "string" || typeof candidate.manifestKey !== "string" || typeof candidate.persistedAt !== "string") {
throw new RevisionConflict("A complete canonical pointer is required for D1 promotion.")
}
}

function ensureSchema(database: D1Database): Promise<void> {
const key = database as object
const existing = schemaReady.get(key)
if (existing) return existing
const pending = (async () => {
await database.prepare(`CREATE TABLE IF NOT EXISTS wp_codebox_state (
site_id TEXT PRIMARY KEY,
revision TEXT,
manifest_key TEXT,
persisted_at TEXT,
version INTEGER NOT NULL DEFAULT 0,
lease_token TEXT,
lease_base_revision TEXT,
lease_version INTEGER,
lease_expires_at INTEGER
)`).run()
await database.prepare(`INSERT OR IGNORE INTO wp_codebox_state (site_id, version) VALUES (?, 0)`).bind(SITE_ID).run()
})()
schemaReady.set(key, pending)
pending.catch(() => schemaReady.delete(key))
return pending
}
77 changes: 77 additions & 0 deletions packages/runtime-cloudflare/src/published-reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export const PUBLISHED_REVISION_SCHEMA = "wp-codebox/published-revision/v1" as const
export const PUBLISHED_PAGE_SCHEMA = "wp-codebox/wordpress-page/v2" as const
export const R2_PUBLISHED_CURRENT_KEY = "sites/default/publications/current.json"
export const R2_PUBLISHED_REVISION_PREFIX = "sites/default/publications/revisions"
export const MAX_PUBLISHED_ROUTES = 1_000
export const MAX_PUBLISHED_REVISION_BYTES = 512 * 1024
export const MAX_PUBLISHED_PAGE_BYTES = 8 * 1024 * 1024

export interface PublishedRoute {
route: string
objectKey: string
}

export interface PublishedRevision {
schema: typeof PUBLISHED_REVISION_SCHEMA
revision: string
canonicalRevision: string
publishedAt: string
routes: PublishedRoute[]
}

export function canonicalPublicRoute(input: Request | URL | string): string {
const url = input instanceof Request ? new URL(input.url) : input instanceof URL ? new URL(input) : new URL(input, "https://wp-codebox-runtime.invalid")
url.searchParams.sort()
return `${url.pathname}${url.search}`
}

export async function publishedPageObjectKey(canonicalRevision: string, route: string): Promise<string> {
if (!isRevision(canonicalRevision) || !isCanonicalRoute(route)) throw new Error("Published page identity is invalid.")
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(route))
const hash = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("")
return `sites/default/pages/${canonicalRevision}/${hash}.json`
}

export function publishedRevisionObjectKey(revision: string): string {
if (!isRevision(revision)) throw new Error("Published revision identity is invalid.")
return `${R2_PUBLISHED_REVISION_PREFIX}/${revision}.json`
}

export function validatePublishedRevision(value: unknown): PublishedRevision {
if (!value || typeof value !== "object") throw new Error("Published revision is invalid.")
const revision = value as Partial<PublishedRevision>
if (revision.schema !== PUBLISHED_REVISION_SCHEMA || !isRevision(revision.revision) || !isRevision(revision.canonicalRevision)
|| typeof revision.publishedAt !== "string" || !Number.isFinite(Date.parse(revision.publishedAt)) || !Array.isArray(revision.routes)
|| revision.routes.length === 0 || revision.routes.length > MAX_PUBLISHED_ROUTES) throw new Error("Published revision is invalid.")
let previous = ""
for (const route of revision.routes) {
if (!route || typeof route !== "object" || !isCanonicalRoute(route.route) || route.route <= previous
|| route.objectKey !== `sites/default/pages/${revision.canonicalRevision}/${route.objectKey.split("/").at(-1)}`
|| !/^sites\/default\/pages\/[a-f0-9-]{36}\/[a-f0-9]{64}\.json$/.test(route.objectKey)) throw new Error("Published revision route is invalid.")
previous = route.route
}
return revision as PublishedRevision
}

export function normalizePublishedRoutes(value: unknown): string[] {
if (!Array.isArray(value) || value.length === 0 || value.length > MAX_PUBLISHED_ROUTES) throw new Error("Publication requires a bounded routes array.")
const routes = value.map((route) => {
if (typeof route !== "string" || !route.startsWith("/")) throw new Error("Publication route is invalid.")
return canonicalPublicRoute(route)
}).sort()
if (routes.some((route, index) => index > 0 && route === routes[index - 1])) throw new Error("Publication routes must be unique.")
return routes
}

function isCanonicalRoute(route: string): boolean {
if (!route.startsWith("/") || route.includes("#") || route.includes("\\") || route.length > 2_048) return false
try {
return canonicalPublicRoute(route) === route
} catch {
return false
}
}

function isRevision(revision: unknown): revision is string {
return typeof revision === "string" && /^[a-f0-9-]{36}$/.test(revision)
}
2 changes: 2 additions & 0 deletions packages/runtime-cloudflare/src/request-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type WorkerRequestRoute =
| { kind: "r2-mutate" }
| { kind: "operator-reset" }
| { kind: "operator-restore" }
| { kind: "operator-publish" }
| { kind: "probe"; phase: string }

export function routeWorkerRequest(request: Request): WorkerRequestRoute {
Expand All @@ -15,5 +16,6 @@ export function routeWorkerRequest(request: Request): WorkerRequestRoute {
if (phase === "r2-mutate") return { kind: "r2-mutate" }
if (phase === "operator-reset") return { kind: "operator-reset" }
if (phase === "operator-restore") return { kind: "operator-restore" }
if (phase === "operator-publish") return { kind: "operator-publish" }
return { kind: "probe", phase }
}
Loading
Loading