1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""Prune atcr.io/zat.dev/stream via the PDS records atcr indexes.
atcr has no registry-API manifest DELETE (every DELETE returns 405, even
correctly scoped — recorded 2026-08-09). Images are really deleted by
removing their `io.atcr.tag` / `io.atcr.manifest` records from our own PDS,
which is what atcr indexes. That path is not quota-blocked. atcr's
server-side GC recomputes quota roughly daily, so usage lags deletion by up
to ~24h.
plan:
- inventory: public listRecords over io.atcr.tag + io.atcr.manifest
- scope: ONLY repository == "stream" or "stream/cache" — the same
collections hold other projects' images (zds), which must never be touched
- keep: every tag named by a receipt in receipts/*.json, plus 'latest';
manifests stay if reachable from a kept tag (index children included,
resolved through the raw manifest blob)
- delete: everything else stream-scoped, via com.atproto.repo.applyWrites
auth: ATCR_USER (default zat.dev) + ATCR_APP_PASSWORD, an atproto app
password — createSession against the account's PDS. Reads are public.
usage: scripts/registry-prune [--dry-run]
"""
import json
import os
import pathlib
import sys
import urllib.error
import urllib.request
HANDLE = os.environ.get("ATCR_USER", "zat.dev")
REPOSITORIES = {"stream", "stream/cache"}
DRY = "--dry-run" in sys.argv
BATCH = 200 # applyWrites cap
def get_json(url: str) -> dict:
with urllib.request.urlopen(url, timeout=30) as resp:
return json.load(resp)
def resolve_pds(handle: str) -> tuple[str, str]:
did = get_json(
"https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle"
f"?handle={handle}"
)["did"]
doc = get_json(f"https://plc.directory/{did}")
pds = next(
s["serviceEndpoint"] for s in doc["service"] if s["id"] == "#atproto_pds"
)
return did, pds
def list_records(pds: str, did: str, collection: str) -> list[dict]:
records, cursor = [], None
while True:
url = (
f"{pds}/xrpc/com.atproto.repo.listRecords"
f"?repo={did}&collection={collection}&limit=100"
)
if cursor:
url += f"&cursor={cursor}"
page = get_json(url)
records += page["records"]
cursor = page.get("cursor")
if not cursor or not page["records"]:
return records
def rkey(uri: str) -> str:
return uri.rsplit("/", 1)[1]
def bare_digest(d: str) -> str:
return d.removeprefix("sha256:")
def index_children(pds: str, did: str, record: dict) -> set[str]:
"""Child manifest digests of an OCI index, from the raw manifest blob."""
blob = record["value"].get("manifestBlob")
if not blob:
return set()
cid = blob["ref"]["$link"]
with urllib.request.urlopen(
f"{pds}/xrpc/com.atproto.sync.getBlob?did={did}&cid={cid}", timeout=30
) as resp:
manifest = json.load(resp)
return {bare_digest(m["digest"]) for m in manifest.get("manifests", [])}
def create_session(pds: str) -> dict:
password = os.environ.get("ATCR_APP_PASSWORD") or sys.exit(
"no ATCR_APP_PASSWORD in env"
)
req = urllib.request.Request(
f"{pds}/xrpc/com.atproto.server.createSession",
data=json.dumps({"identifier": HANDLE, "password": password}).encode(),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=30) as resp:
return json.load(resp)
def apply_deletes(pds: str, session: dict, doomed: list[tuple[str, str]]) -> None:
for start in range(0, len(doomed), BATCH):
writes = [
{
"$type": "com.atproto.repo.applyWrites#delete",
"collection": collection,
"rkey": key,
}
for collection, key in doomed[start : start + BATCH]
]
req = urllib.request.Request(
f"{pds}/xrpc/com.atproto.repo.applyWrites",
data=json.dumps({"repo": session["did"], "writes": writes}).encode(),
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {session['accessJwt']}",
},
)
urllib.request.urlopen(req, timeout=60).close()
print(f" deleted {len(writes)} records")
def main() -> None:
keep_tags = {"latest"}
for receipt in pathlib.Path("receipts").glob("*.json"):
keep_tags.add(receipt.stem)
print(f"keeping tags: {sorted(keep_tags)}")
did, pds = resolve_pds(HANDLE)
print(f"{HANDLE} = {did} @ {pds}")
tags = [
t
for t in list_records(pds, did, "io.atcr.tag")
if t["value"].get("repository") in REPOSITORIES
]
manifests = {
rkey(m["uri"]): m
for m in list_records(pds, did, "io.atcr.manifest")
if m["value"].get("repository") in REPOSITORIES
}
print(f"stream-scoped: {len(tags)} tags, {len(manifests)} manifests")
# closure of manifests reachable from kept tags (cache tags never kept)
keep_digests: set[str] = set()
doomed_tags: list[dict] = []
for tag in tags:
value = tag["value"]
kept = value["repository"] == "stream" and value["tag"] in keep_tags
if not kept:
doomed_tags.append(tag)
continue
digest = rkey(value["manifest"])
keep_digests.add(digest)
if digest in manifests:
keep_digests |= index_children(pds, did, manifests[digest])
doomed: list[tuple[str, str]] = []
for tag in doomed_tags:
print(f" doomed tag {rkey(tag['uri'])} ({tag['value']['tag']})")
doomed.append(("io.atcr.tag", rkey(tag["uri"])))
for digest, record in sorted(manifests.items()):
if digest in keep_digests:
continue
print(f" doomed manifest {digest[:19]}… ({record['value']['repository']})")
doomed.append(("io.atcr.manifest", digest))
kept = len(tags) - len(doomed_tags)
print(f"kept {kept} tags / {len(keep_digests)} reachable manifests; "
f"{len(doomed)} records doomed")
if not doomed:
print("nothing to prune.")
return
if DRY:
print("dry run: no deletes issued.")
return
apply_deletes(pds, create_session(pds), doomed)
print("done. atcr's server-side GC recomputes quota within ~24h.")
if __name__ == "__main__":
main()