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
43 changes: 43 additions & 0 deletions pkgs/github-ci/worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,49 @@ Cutover state lives on #5693: deploy dark → replay smoke → one
repo-level dual-run webhook → (later, separately) org/App flip.
The Edge Function stays deployed as rollback until then.

## Cutover closeout (2026-11-14, task 16806)

The flip is complete and the Edge Function is retired:

- **Dedupe diff clean.** Every `dedupe_key` in the Postgres
`ci.events` ledger (11,203 keys) exists in Turso `ci_events`.
The only gap was the Postgres ledger's final 4 rows
(2026-11-06T17:47–18:00Z, received after the backfill watermark);
those 4 rows were backfilled into Turso and the diff re-ran at
zero missing. Turso held 27,648 rows at closeout, 16,445 of them
written after the Postgres ledger stopped.
- **Postgres write-silence verified.** `ci.events` last write is
2026-11-06T18:00:26Z — eight days of silence while the fleet
produced ~16k Turso events. No sender still reaches the old
receiver; every fleet repo's webhook points at this Worker
(verified per repo: code, opencode, openchamber, darwin-nix).
- **Edge Function retired.** `github-ci` (the last Edge Function
on muse-dev2, v9) was deleted via the Supabase Management API
on 2026-11-14; the project now has zero Edge Functions. The
Worker → Turso write path was verified live after the deletion
(new `ci_events` rows within a minute). Rollback, if ever
needed, is a redeploy of this Worker — its behavior is the
behavior-for-behavior port of the Edge Function.
- **The Postgres table stays.** `ci.events` remains read-only in
place for one retention window; dropping it is a separate,
separately-approved decision (not part of the cutover).

## Retention (90 days hot)

`ci_events` keeps 90 days hot in Turso, per the #5693 design.
The prune is `scripts/prune-ci-events.py` in this package:

```sh
python3 scripts/prune-ci-events.py # delete past 90 days
python3 scripts/prune-ci-events.py --dry-run # count only
```

It resolves the Turso token by name (`turso.fleet-platform-rw-token`)
and prints counts only. First run 2026-11-14 deleted 0 rows — the
ledger's oldest row is the 2026-10-30 backfill head, so the window
first binds in late January 2027; run the prune on fleet maintenance
cadence so the window is enforced from then on.

## GitHub mirror (#8580)

Fleet-repo `issues` / `pull_request` / `issue_comment` deliveries also
Expand Down
90 changes: 90 additions & 0 deletions pkgs/github-ci/worker/scripts/prune-ci-events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python3
"""Prune fleet-platform `ci_events` past the 90-day hot retention (#5693).

The ci.events ledger moved from Supabase Postgres to Turso
`fleet-platform.ci_events` (cutover completed 2026-11-14: dedupe diff
clean, Postgres write-silent since 2026-11-06, the Supabase Edge
Function receiver retired). The design's retention rule is 90 days
hot in Turso; this script is the prune that enforces it:

DELETE FROM ci_events WHERE received_at < datetime('now', '-90 days')

Usage: prune-ci-events.py [--days N] [--dry-run]

The Turso token is resolved by NAME only (`turso.fleet-platform-rw-token`
via `fleet secrets get`) and is never printed or written to disk.
Run it from any host that can reach the Turso pipeline endpoint and
the fleet secrets broker (the laptop, or an agent sandbox).
"""

import argparse
import json
import subprocess
import sys
import urllib.request

TURSO_URL = "https://fleet-platform-dezren39.aws-us-east-1.turso.io/v2/pipeline"
TOKEN_NAME = "turso.fleet-platform-rw-token"


def get_token():
out = subprocess.run(
["fleet", "secrets", "get", TOKEN_NAME],
capture_output=True, text=True, timeout=60,
)
if out.returncode != 0:
raise SystemExit(f"token fetch failed for {TOKEN_NAME} (rc={out.returncode})")
return out.stdout.strip()


def execute(token, sql):
body = json.dumps({
"requests": [
{"type": "execute", "stmt": {"sql": sql}},
{"type": "close"},
]
}).encode()
req = urllib.request.Request(
TURSO_URL, data=body,
headers={"Authorization": f"Bearer {token}",
"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=120) as resp:
payload = json.load(resp)
result = payload["results"][0]["response"]["result"]
if result is None:
raise SystemExit(f"pipeline error for: {sql[:60]}")
return result


def scalar(result):
return result["rows"][0][0]["value"]


def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--days", type=int, default=90,
help="hot retention window in days (default 90, per #5693 design)")
ap.add_argument("--dry-run", action="store_true",
help="count the rows past the window without deleting")
args = ap.parse_args()
if args.days < 1:
raise SystemExit("--days must be positive")

token = get_token()
where = f"received_at < datetime('now', '-{args.days} days')"
total = scalar(execute(token, "SELECT count(*) FROM ci_events"))
stale = scalar(execute(token, f"SELECT count(*) FROM ci_events WHERE {where}"))
print(f"ci_events total={total} past-{args.days}d={stale}")
if args.dry_run:
print("dry-run: nothing deleted")
return 0
result = execute(token, f"DELETE FROM ci_events WHERE {where}")
print(f"deleted={result['affected_row_count']}")
total_after = scalar(execute(token, "SELECT count(*) FROM ci_events"))
print(f"ci_events total after={total_after}")
return 0


if __name__ == "__main__":
sys.exit(main())