Skip to content

heymegabyte/agent-skills / 08-deploy-and-runtime-verification

deploy-and-runtime-verification

Unverified

MANDATORY deploy after every code change. Typecheck → deploy → purge CDN → E2E on production → visual verify → fix-forward loop. Workers Builds native CI/CD, D1 Time Travel PIT recovery, D1→R2 long-term backups, wrangler rollback, wrangler secrets management, structured observability, cross-browser smoke tests, rollback procedures, and GitHub auto-configuration.

FileKindSize
SKILL.mdskill md5.6 KB
backup-and-disaster-recovery.mdmarkdown2.7 KB
changelog-and-releases.mdmarkdown5.3 KB
ci-cd-pipeline.mdmarkdown7.1 KB
critical-css.mdmarkdown4.7 KB
font-subsetting.mdmarkdown4.5 KB
gh-fix-ci.mdmarkdown3.5 KB
launch-day-sequence.mdmarkdown5.2 KB
pipeline-health-check.mdmarkdown4.5 KB
r2-lifecycle.mdmarkdown5.6 KB
service-worker.mdmarkdown9.7 KB
uptime-and-health.mdmarkdown6.5 KB

r2-lifecycle.md

View on GitHub
# R2 Lifecycle Management (***UNIVERSAL — EVERY CLOUDFLARE WORKER WITH R2***)

R2 buckets accumulate stale deploy artifacts (Angular chunks, source-map files, old marketing builds) at one generation per deploy. Without lifecycle hygiene a 200-route site bucket bloats to 4,000+ objects in 6 months, $0.015/GB/month adds up, and audit / cleanup work explodes.

**Wrangler v4 does NOT list R2 objects** (`wrangler r2 object` = get | put | delete only) — use REST API or S3-compat CLI for inventory, wrangler for delete.

## Inventory (***THREE CANONICAL PATHS — PICK FIRST AVAILABLE***)

### (A) S3-compat CLI — preferred

When `aws-cli` or `rclone` installed. Set `~/.aws/credentials [r2]` profile with R2 access-key-id + secret from CF dashboard r2 API tokens page.

```bash
aws s3 ls "s3://project-sites-production/marketing/" --recursive \
  --endpoint-url "https://${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com" --profile r2 > /tmp/r2-marketing.txt
```

### (B) REST list — works with GLOBAL KEY, no install

Cursor pagination in 6 lines of bash.

```bash
ACCOUNT=84fa0d1b16ff8086dd958c468ce7fd59 BUCKET=project-sites-production PREFIX=marketing/
CURSOR=""; > /tmp/r2-marketing.json
echo "[" > /tmp/r2-marketing.json
FIRST=1
while :; do
  RESP=$(curl -s "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/r2/buckets/$BUCKET/objects?per_page=1000&prefix=$PREFIX${CURSOR:+&cursor=$CURSOR}" \
    -H "X-Auth-Email: $CLOUDFLARE_EMAIL" -H "X-Auth-Key: $CLOUDFLARE_API_KEY")
  KEYS=$(echo "$RESP" | jq -c '.result[]')
  [ -n "$KEYS" ] && { [ $FIRST -eq 0 ] && echo ","; echo "$KEYS" | paste -sd ',' -; FIRST=0; } >> /tmp/r2-marketing.json
  CURSOR=$(echo "$RESP" | jq -r 'if .result_info.is_truncated then .result_info.cursor else empty end')
  [ -z "$CURSOR" ] && break
done
echo "]" >> /tmp/r2-marketing.json
```

### (C) Quick key-only inventory (no metadata)

```bash
ACCOUNT=84fa0d1b16ff8086dd958c468ce7fd59 BUCKET=project-sites-production PREFIX=marketing/
CURSOR=""; > /tmp/r2-keys.txt
while :; do
  RESP=$(curl -s "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/r2/buckets/$BUCKET/objects?per_page=1000&prefix=$PREFIX${CURSOR:+&cursor=$CURSOR}" \
    -H "X-Auth-Email: $CLOUDFLARE_EMAIL" -H "X-Auth-Key: $CLOUDFLARE_API_KEY")
  echo "$RESP" | jq -r '.result[].key' >> /tmp/r2-keys.txt
  CURSOR=$(echo "$RESP" | jq -r 'if .result_info.is_truncated then .result_info.cursor else empty end')
  [ -z "$CURSOR" ] && break
done
wc -l /tmp/r2-keys.txt
```

### Common prefixes in `project-sites-production`

- `sites/<slug>/` — client data, PRESERVE
- `marketing/` — homepage assets
- `templates/<category>/`
- `app/` — legacy admin SPA
- `container/` — build artifacts
- `retrospectives/` — debug dumps
- `test/` — probes

## Stale-asset classification (***Angular/Vite hash-named chunks accumulate fastest***)

```bash
# Identify hash-named build artifacts: chunk-ABC123.js, main-XYZ456.js, polyfills-DEF.js
grep -E "/(chunk|main|polyfills)-[A-Z0-9]+\.js$" /tmp/r2-keys.txt > /tmp/stale-chunks.txt
# Identify all CSS bundles (Angular/Vite generate one per deploy)
grep -E "\.css$" /tmp/r2-keys.txt > /tmp/stale-css.txt
# Cross-reference against the LIVE homepage references — anything live = preserve
curl -fsSL https://projectsites.dev/ > /tmp/live-homepage.html
grep -oE 'src="[^"]+"|href="[^"]+"' /tmp/live-homepage.html | sed 's/^[^"]*"//; s/"$//' | sort -u > /tmp/live-refs.txt
# Final delete list = stale candidates MINUS live refs
comm -23 <(sort /tmp/stale-chunks.txt) <(sort /tmp/live-refs.txt) > /tmp/r2-delete.txt
```

## Batch delete (***ONLY AFTER USER CONFIRMATION on production buckets***)

```bash
# Dry-run first
wc -l /tmp/r2-delete.txt # confirm count matches expectation
head -10 /tmp/r2-delete.txt # confirm pattern matches expectation
# Execute with parallel concurrency. wrangler r2 object delete IS native (verified v4.63.0).
# P=4 safe, P=8 OK, never >P=16 — R2 rate-limits at 1000 ops/s/bucket
xargs -I{} -P 4 npx wrangler r2 object delete "<bucket>/{}" --remote < /tmp/r2-delete.txt
# Verify post-delete by re-listing
# (re-run inventory script above)
```

## Lifecycle policy (***SHIP WITH EVERY NEW BUCKET***)

Cloudflare R2 supports object lifecycle rules via `wrangler r2 bucket lifecycle`. Apply at bucket creation:

```bash
# Auto-delete deploy artifacts older than 30 days
npx wrangler r2 bucket lifecycle add <bucket> --prefix marketing/ --age-days 30 --action delete --remote
# Preserve client data (sites/) — no lifecycle rule, manual purge only on subscription end
# Transition rarely-accessed templates to Infrequent Access tier after 60 days
npx wrangler r2 bucket lifecycle add <bucket> --prefix templates/ --age-days 60 --action transition-ia --remote
```

Without lifecycle rules every deploy is a permanent cost. **Verify with** `npx wrangler r2 bucket lifecycle list <bucket> --remote`.

## Prevention (***BUILD-CONFIG CHANGES***)

- Vite / Angular `outDir` should write to a versioned subdirectory (`marketing/v<commit-sha>/`), with the worker resolving `marketing/index.html` → latest version. Old versions become trivially purgeable by directory.
- Worker upload script (`scripts/deploy-r2.mjs`) MUST delete old artifacts before uploading new ones — never just `--no-prefix` upload that accumulates.
- Per `~/.claude/rules/builtin-tools-first.md` — probe `wrangler <subcommand> --help` before citing — wrangler v4 r2 object has no `list`. REST endpoint or `aws s3 ls --endpoint-url` are the real list paths.

## See Also

- `~/.claude/rules/builtin-tools-first.md` (universal vendor-CLI-first rule)
- `~/.agentskills/08-deploy-and-runtime-verification/SKILL.md` (deploy gate)
- `migrations/_applied.md` (wrangler global-key rejection on D1 migrations only — does NOT extend to R2)
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall