File size: 8,279 Bytes
1d50c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7636865
1d50c78
 
7636865
1d50c78
7636865
1d50c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754345f
 
 
 
 
 
 
 
 
1d50c78
 
 
754345f
1d50c78
 
 
 
 
 
 
 
 
 
 
 
 
 
754345f
 
 
1d50c78
 
 
 
 
 
754345f
 
 
 
 
 
 
 
1d50c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754345f
 
 
 
 
 
 
 
 
1d50c78
 
754345f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d50c78
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
"""Backstop sweeper for orphan ml-intern sandbox Spaces.

================================================================================
 Why this script exists
================================================================================

The agent creates a sandbox Space per session (template duplicated from
``burtenshaw/sandbox`` into the user's account, named ``<owner>/sandbox-<8hex>``).
``backend.session_manager.SessionManager._cleanup_sandbox`` deletes it at end of
session. In practice the cleanup misses some sandboxes:

- pod killed / OOM / pre-emption / deploy rollouts → ``finally`` block skipped
- WebSocket dropped without ``/shutdown`` from the client
- HF API transient failure on ``delete_repo`` (we retry now, but not infinitely)

The result observed 2026-04-27 was 2,310 orphan ``sandbox-*`` Spaces — every
sandbox ever created was still around. This script is the backstop: list every
``sandbox-*`` fork of ``burtenshaw/sandbox`` that hasn't been touched in N days
and delete it.

================================================================================
 Identification rules
================================================================================

A Space is considered an orphan ml-intern sandbox iff ALL hold:

1. Repo type = ``space``
2. Name matches ``<owner>/sandbox-[a-f0-9]{8}$`` (the agent's naming convention)
3. ``originRepo`` points at ``burtenshaw/sandbox`` (so we don't touch
   user-renamed lookalikes)
4. ``lastModified`` older than ``--max-age-days`` (default 7)

We DO NOT use the ``runtime.stage`` (sleeping/running) as a filter — a sandbox
that has been sleeping for 7 days is just as orphan as a deleted one but uses
no compute. The cleanup is about repo/storage hygiene, not about waking
something up to kill it.

================================================================================
 Safety
================================================================================

- ``--dry-run`` (default) prints what would be deleted, deletes nothing.
- ``--apply`` actually calls ``HfApi.delete_repo``.
- Hard cap ``--max-deletes`` (default 200) so a misconfigured run can't nuke
  thousands at once.
- Requires a token with admin rights via ``HF_ADMIN_TOKEN`` env var (the only
  way to delete a Space owned by another user).
- Logs every action to stdout in JSON Lines for downstream auditing.

================================================================================
 Manual usage
================================================================================

Run manually with an admin token when a backstop cleanup is needed:

    HF_ADMIN_TOKEN=... python scripts/sweep_orphan_sandboxes.py --apply --max-age-days 7
"""

import argparse
import json
import os
import re
import sys
import time
from datetime import datetime, timedelta, timezone

from huggingface_hub import HfApi
from huggingface_hub.utils import HfHubHTTPError

SANDBOX_NAME_RE = re.compile(r"^[^/]+/sandbox-[a-f0-9]{8}$")
TEMPLATE_REPO = "burtenshaw/sandbox"


def log(record: dict) -> None:
    """JSON Lines log so downstream tooling can grep / parse."""
    record["ts"] = datetime.now(timezone.utc).isoformat()
    print(json.dumps(record), flush=True)


def is_sandbox_fork(space) -> bool:
    """Filter: matches the ml-intern sandbox naming pattern.

    NOTE: We initially tried filtering on ``duplicated_from == burtenshaw/sandbox``
    too, for extra safety. That doesn't work — the HF REST API does not expose
    ``duplicated_from`` on ``SpaceInfo`` (verified against ``huggingface-hub``
    1.11+ and direct ``GET /api/spaces/{id}``: the field is None). The origin
    repo lives in MongoDB but isn't surfaced. So we rely on the naming pattern
    alone, which is specific enough: ``Sandbox.create()`` is the sole producer
    of ``<owner>/sandbox-<8 lowercase hex>``, and that pattern is unlikely to
    collide with user-created Spaces in practice. The ``--dry-run`` default
    is the user-facing safety net for the rare false-positive.
    """
    return bool(SANDBOX_NAME_RE.match(space.id))


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
    parser.add_argument(
        "--max-age-days",
        type=int,
        default=7,
        help="Delete sandboxes whose lastModified is older than this many days (default: 7)",
    )
    parser.add_argument(
        "--max-deletes",
        type=int,
        default=200,
        help="Hard cap on deletions per run, safety guard (default: 200)",
    )
    parser.add_argument(
        "--apply",
        action="store_true",
        help="Actually delete. Without this flag, dry-run only.",
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=10000,
        help="Max number of candidate Spaces to scan via list_spaces (default: 10000)",
    )
    args = parser.parse_args()

    token = os.environ.get("HF_ADMIN_TOKEN")
    if not token:
        log({"level": "error", "msg": "HF_ADMIN_TOKEN env var not set"})
        return 1

    api = HfApi(token=token)
    cutoff = datetime.now(timezone.utc) - timedelta(days=args.max_age_days)
    log(
        {
            "level": "info",
            "msg": "sweep_start",
            "cutoff": cutoff.isoformat(),
            "max_deletes": args.max_deletes,
            "apply": args.apply,
        }
    )

    # ``list_spaces`` doesn't filter by name pattern — we scan and filter
    # client-side. ``search="sandbox"`` narrows the network payload.
    candidates = api.list_spaces(search="sandbox", full=True, limit=args.limit)

    scanned = 0
    matched = 0
    deleted = 0
    failed = 0
    skipped_too_recent = 0
    skipped_capped = 0

    for space in candidates:
        scanned += 1
        if not is_sandbox_fork(space):
            continue
        matched += 1

        last_mod = getattr(space, "lastModified", None) or getattr(
            space, "last_modified", None
        )
        if isinstance(last_mod, str):
            last_mod = datetime.fromisoformat(last_mod.replace("Z", "+00:00"))
        if last_mod and last_mod > cutoff:
            skipped_too_recent += 1
            continue

        log(
            {
                "level": "info",
                "msg": "candidate",
                "space_id": space.id,
                "last_modified": last_mod.isoformat() if last_mod else None,
            }
        )

        if not args.apply:
            continue

        # When we hit the deletion cap, keep scanning so the final ``matched``
        # count reflects the *true* orphan size — not just what was scanned
        # before we stopped deleting. Operators planning multi-pass cleanups
        # need an accurate denominator to know when they're done.
        if deleted >= args.max_deletes:
            skipped_capped += 1
            continue

        try:
            api.delete_repo(repo_id=space.id, repo_type="space", token=token)
            deleted += 1
            log({"level": "info", "msg": "deleted", "space_id": space.id})
            # Light throttle to avoid hitting HF API rate limits.
            time.sleep(0.2)
        except HfHubHTTPError as e:
            failed += 1
            log(
                {
                    "level": "error",
                    "msg": "delete_failed",
                    "space_id": space.id,
                    "status": e.response.status_code,
                    "error": str(e)[:200],
                }
            )
        except Exception as e:
            failed += 1
            log(
                {
                    "level": "error",
                    "msg": "delete_failed",
                    "space_id": space.id,
                    "error": str(e)[:200],
                }
            )

    log(
        {
            "level": "info",
            "msg": "sweep_end",
            "scanned": scanned,
            "matched": matched,
            "skipped_too_recent": skipped_too_recent,
            "skipped_capped": skipped_capped,
            "deleted": deleted,
            "failed": failed,
            "capped": skipped_capped > 0,
            "apply": args.apply,
        }
    )

    return 0 if failed == 0 else 2


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