File size: 20,753 Bytes
fb34a4d
 
 
 
5b7d989
 
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7d989
fb34a4d
 
 
 
 
 
 
 
 
5b7d989
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
 
815ff1f
 
 
 
 
5b7d989
815ff1f
 
 
5b7d989
fb34a4d
 
815ff1f
 
fb34a4d
 
5b7d989
fb34a4d
5b7d989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f243d06
fb34a4d
5b7d989
f243d06
5b7d989
 
 
 
 
 
 
 
 
 
 
 
 
 
815ff1f
 
 
fb34a4d
 
 
 
 
 
 
 
 
 
 
5b7d989
 
 
fb34a4d
 
 
f243d06
 
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f243d06
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
 
08cdd1c
5b7d989
f243d06
fb34a4d
 
 
 
f243d06
fb34a4d
 
3ef3309
 
 
 
 
 
 
 
 
 
08cdd1c
5b7d989
3ef3309
 
6f91c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7d989
6f91c8f
 
5b7d989
 
 
 
fb34a4d
 
 
 
 
3ef3309
 
 
 
6f91c8f
 
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7d989
fb34a4d
 
f243d06
 
 
 
 
fb34a4d
 
 
 
 
 
 
 
 
 
 
f243d06
 
 
 
 
 
 
fb34a4d
 
 
f243d06
fb34a4d
 
 
 
 
f243d06
 
 
 
 
 
815ff1f
 
f243d06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
815ff1f
 
f243d06
 
 
 
 
815ff1f
f243d06
815ff1f
f243d06
 
 
 
 
 
 
815ff1f
 
 
f243d06
 
815ff1f
f243d06
 
 
 
815ff1f
f243d06
 
815ff1f
f243d06
 
 
 
 
 
 
 
815ff1f
f243d06
 
 
 
815ff1f
f243d06
 
 
815ff1f
f243d06
 
 
815ff1f
f243d06
 
 
 
 
 
 
 
 
fb34a4d
 
 
 
 
f243d06
fb34a4d
 
 
 
 
f243d06
fb34a4d
 
 
 
f243d06
fb34a4d
 
 
f243d06
 
fb34a4d
 
5b7d989
fb34a4d
 
 
 
 
 
 
f243d06
fb34a4d
 
 
 
 
f243d06
 
fb34a4d
 
 
 
 
 
 
 
f243d06
fb34a4d
 
 
f243d06
 
5b7d989
 
fb34a4d
f243d06
fb34a4d
 
 
 
 
 
f243d06
 
 
fb34a4d
f243d06
5b7d989
fb34a4d
 
 
 
 
f243d06
08cdd1c
5b7d989
fb34a4d
 
f243d06
fb34a4d
 
5b7d989
 
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
815ff1f
f243d06
 
 
 
 
 
 
815ff1f
fb34a4d
 
 
f243d06
5b7d989
 
fb34a4d
815ff1f
f243d06
 
fb34a4d
 
815ff1f
fb34a4d
 
 
 
 
f243d06
815ff1f
 
f243d06
815ff1f
f243d06
fb34a4d
 
 
815ff1f
f243d06
fb34a4d
 
 
5b7d989
fb34a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
815ff1f
fb34a4d
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env python3
"""
OpenCode Sync Engine
====================
Persists /projects/default (workspace), /data/share/opencode (OpenCode DB),
and /data/config/opencode (OpenCode config) to a private HF Dataset repo.

Usage:
  python3 /sync_engine.py restore   β€” pull dataset β†’ local dirs at startup
  python3 /sync_engine.py watch     β€” background sync daemon
  python3 /sync_engine.py sync      β€” one-shot sync then exit

Environment variables (set as Space Secrets):
  HF_TOKEN      β€” HuggingFace token with read+write access to the dataset
  HF_DATASET    β€” dataset repo id (default: Jishnupg/OpenCode-Storage)
"""

import sys
import os
import time
import hashlib
import logging
import traceback
import shutil
import tempfile
from pathlib import Path

# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(
    level=logging.INFO,
    format="[%(asctime)s] [SYNC] %(levelname)s %(message)s",
    datefmt="%H:%M:%S",
    stream=sys.stdout,
    force=True,
)
log = logging.getLogger("sync")


# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
HF_TOKEN   = os.environ.get("HF_TOKEN", "")
HF_DATASET = os.environ.get("HF_DATASET", "Jishnupg/OpenCode-Storage")

# ─── Watched directories ─────────────────────────────────────────────────────
# Scoped to the SPECIFIC OpenCode subdirectories only.
# The XDG roots /data/share and /data/config contain Android SDK caches,
# nvm installations, sdkbin-*/sdkinf-* files and other system noise β€”
# none of that should ever be synced.
#
#   "workspace" β†’ /projects/default          (user's project files)
#   "share"     β†’ /data/share/opencode       (OpenCode SQLite DB)
#   "config"    β†’ /data/config/opencode      (OpenCode config JSON)
# ─────────────────────────────────────────────────────────────────────────────
WATCH_DIRS: dict[str, Path] = {
    "workspace": Path("/projects/default"),
    "share":     Path("/data/share/opencode"),
    "config":    Path("/data/config/opencode"),
}

# Exact directory-name segments to skip anywhere in the path
IGNORE_NAMES: set[str] = {
    ".git",
    "node_modules",
    "__pycache__",
    ".cache",
    ".npm",
    ".yarn",
    ".pnpm",
    ".pip",
    "dist",
    "build",
    # Android / SDK
    ".android",
    ".gradle",
    ".kotlin",
    "sdktools",
    # Temp / logs / misc
    "tmp",
    "temp",
    "logs",
    ".local",
}

# Name *prefixes* to skip (catches sdkbin-*, sdkinf-*, etc.)
IGNORE_PREFIXES: tuple[str, ...] = (
    "sdkbin-",
    "sdkinf-",
    "tmp-",
    "temp-",
    ".~",
)

# File suffixes to skip
IGNORE_SUFFIXES: set[str] = {
    ".tmp", ".sock", ".pid", ".lock", ".pyc",
    ".log", ".bak", ".swp", ".swo",
}

MAX_FILE_BYTES          = 50 * 1_024 * 1_024   # 50 MB hard limit per file
POLL_INTERVAL_SECS      = 15                    # how often to check for changes
CHECKPOINT_INTERVAL_SECS = 300                  # full-disk re-scan interval


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _should_skip(path: Path) -> bool:
    """Return True if this file should not be synced."""
    for part in path.parts:
        if part in IGNORE_NAMES:
            return True
        for pfx in IGNORE_PREFIXES:
            if part.startswith(pfx):
                return True
    if path.suffix in IGNORE_SUFFIXES:
        return True
    try:
        if path.stat().st_size > MAX_FILE_BYTES:
            log.warning(f"Skip (too large): {path}")
            return True
    except OSError:
        return True
    return False


def _md5(path: Path) -> str:
    h = hashlib.md5()
    try:
        with open(path, "rb") as fh:
            for chunk in iter(lambda: fh.read(65_536), b""):
                h.update(chunk)
    except OSError:
        return ""
    return h.hexdigest()


def _repo_path(prefix: str, local_path: Path, base: Path) -> str:
    """Convert a local absolute path to its dataset repo path."""
    return f"{prefix}/{local_path.relative_to(base)}"


def _local_path(repo_file: str) -> Path | None:
    """Convert a dataset repo path back to a local absolute path."""
    parts = repo_file.split("/", 1)
    if len(parts) < 2:
        return None
    prefix, rel = parts
    base = WATCH_DIRS.get(prefix)
    if base is None:
        return None
    return base / rel


# ---------------------------------------------------------------------------
# SyncEngine
# ---------------------------------------------------------------------------

class SyncEngine:
    def __init__(self) -> None:
        self._api  = None
        self._known: dict[str, str] = {}   # repo_path β†’ md5

        if not HF_TOKEN:
            log.warning(
                "HF_TOKEN not set β€” sync is disabled.\n"
                "Add HF_TOKEN to your Space Secrets to enable persistent storage."
            )
            return

        try:
            from huggingface_hub import HfApi
            self._api = HfApi(token=HF_TOKEN)
            log.info(f"Sync engine ready β†’ {HF_DATASET}")
            log.info("Watching directories:")
            for prefix, path in WATCH_DIRS.items():
                log.info(f"  [{prefix}] β†’ {path}")
        except ImportError:
            log.error("huggingface_hub not installed β€” sync disabled")

    # ------------------------------------------------------------------
    # Repo helpers
    # ------------------------------------------------------------------

    def _ensure_repo(self) -> bool:
        try:
            self._api.create_repo(
                repo_id=HF_DATASET,
                repo_type="dataset",
                private=True,
                exist_ok=True,
            )
            return True
        except Exception as exc:
            log.error(f"Could not create/access dataset repo {HF_DATASET}: {exc}")
            log.error(traceback.format_exc())
            return False

    def _test_write(self) -> bool:
        try:
            from huggingface_hub import CommitOperationAdd
            import io
            self._api.create_commit(
                repo_id=HF_DATASET,
                repo_type="dataset",
                commit_message="sync: write-access test",
                operations=[CommitOperationAdd(
                    path_in_repo=".sync-ok",
                    path_or_fileobj=io.BytesIO(b"ok"),
                )],
            )
            log.info("βœ… Write access confirmed β€” dataset sync is active")
            return True
        except Exception as exc:
            log.error("❌ SYNC WRITE FAILED β€” files will NOT be saved to dataset")
            log.error(f"   Error: {exc}")
            log.error(traceback.format_exc())
            return False

    # ------------------------------------------------------------------
    # Restore (startup)
    # ------------------------------------------------------------------

    def restore(self) -> None:
        if not self._api:
            log.info("Restore skipped (sync disabled)")
            return

        if not self._ensure_repo():
            log.warning("Skipping restore β€” dataset repo not accessible")
            return

        self._test_write()

        log.info(f"=== RESTORE: pulling {HF_DATASET} ===")
        try:
            from huggingface_hub import snapshot_download
            with tempfile.TemporaryDirectory(prefix="hf_restore_") as tmp:
                local_repo = snapshot_download(
                    repo_id=HF_DATASET,
                    repo_type="dataset",
                    token=HF_TOKEN,
                    local_dir=tmp,
                    local_dir_use_symlinks=False,
                    ignore_patterns=["*.gitattributes", ".gitattributes"],
                )
                count = self._copy_snapshot(Path(local_repo))
            log.info(f"=== RESTORE complete: {count} files ===")
        except Exception as exc:
            log.warning(f"Restore skipped or partial: {exc}")
            log.warning(traceback.format_exc())

    def _copy_snapshot(self, repo_root: Path) -> int:
        """
        Copy files from a local snapshot dir to their actual locations.
        Only restores files that would NOT be skipped by _should_skip, so that
        a previously-contaminated dataset does not poison the watched dirs.
        """
        count = 0
        for prefix, dest_base in WATCH_DIRS.items():
            src_base = repo_root / prefix
            if not src_base.exists():
                log.info(f"  No '{prefix}' in dataset yet β€” skipping")
                continue
            for src_file in src_base.rglob("*"):
                if not src_file.is_file():
                    continue
                rel = src_file.relative_to(src_base)
                dest_file = dest_base / rel

                # Apply the same skip filter used during scanning β€”
                # prevents old contaminated dataset entries from coming back
                if _should_skip(dest_file):
                    log.debug(f"  Restore skipped (filtered): {dest_file}")
                    continue

                dest_file.parent.mkdir(parents=True, exist_ok=True)
                try:
                    shutil.copy2(src_file, dest_file)
                    log.info(f"  Restored: dataset:{prefix}/{rel} β†’ {dest_file}")
                    count += 1
                except Exception as exc:
                    log.warning(f"  Could not restore {dest_file}: {exc}")
        return count

    # ------------------------------------------------------------------
    # Local junk cleanup
    # ------------------------------------------------------------------

    def _clean_watched_dirs(self) -> int:
        """
        Remove files from the watched dirs that fail the _should_skip filter.
        This is a safety pass to evict any contamination left over from
        contaminated restores (e.g. Android SDK cache files in /data/share).
        Returns the number of files removed.
        """
        removed = 0
        for prefix, base in WATCH_DIRS.items():
            if not base.exists():
                continue
            for path in sorted(base.rglob("*"), reverse=True):  # deep first
                if not path.is_file():
                    continue
                if _should_skip(path):
                    try:
                        path.unlink()
                        log.info(f"  Cleaned junk from watched dir: {path}")
                        removed += 1
                    except Exception as exc:
                        log.warning(f"  Could not clean {path}: {exc}")
        # Remove now-empty dirs (cosmetic, non-fatal)
        for prefix, base in WATCH_DIRS.items():
            if not base.exists():
                continue
            for d in sorted(base.rglob("*"), reverse=True):
                if d.is_dir():
                    try:
                        d.rmdir()  # only succeeds if empty
                    except OSError:
                        pass
        return removed

    # ------------------------------------------------------------------
    # Dataset contamination purge
    # ------------------------------------------------------------------

    def _purge_dataset_junk(self, valid_repo_paths: set[str]) -> int:
        """
        Delete files from the dataset that are NOT in valid_repo_paths.
        Removes any entries that don't correspond to currently tracked local
        files, guarding against dataset contamination from future regressions.
        Returns the number of files deleted from the dataset.
        """
        if not self._api:
            return 0

        log.info("=== DATASET PURGE: scanning for stale entries ===")
        try:
            from huggingface_hub.hf_api import RepoFile
            dataset_files = self._api.list_repo_tree(
                repo_id=HF_DATASET,
                repo_type="dataset",
                recursive=True,
            )
            to_delete = []
            for f in dataset_files:
                if not isinstance(f, RepoFile):
                    continue  # skip directory entries
                rp = f.path
                if rp.startswith("."):   # .gitattributes, .sync-ok etc.
                    continue
                # If it's not in our current valid set, delete it
                if rp not in valid_repo_paths:
                    to_delete.append(rp)

            if not to_delete:
                log.info("  Dataset is clean β€” no stale entries found")
                return 0

            log.info(f"  Found {len(to_delete)} stale entries to remove from dataset")
            for rp in to_delete[:10]:
                log.info(f"    - {rp}")
            if len(to_delete) > 10:
                log.info(f"    ... and {len(to_delete) - 10} more")

            from huggingface_hub import CommitOperationDelete
            ops = [CommitOperationDelete(path_in_repo=rp) for rp in to_delete]

            # Delete in batches of 200 to stay within API limits
            batch_size = 200
            total_deleted = 0
            for i in range(0, len(ops), batch_size):
                batch = ops[i:i + batch_size]
                bnum = i // batch_size + 1
                self._api.create_commit(
                    repo_id=HF_DATASET,
                    repo_type="dataset",
                    commit_message=f"sync: purge {len(batch)} stale entries (batch {bnum})",
                    operations=batch,
                )
                total_deleted += len(batch)
                log.info(f"  Purge batch {bnum}: deleted {len(batch)} entries")

            log.info(f"=== DATASET PURGE complete: {total_deleted} entries removed ===")
            return total_deleted

        except Exception as exc:
            log.error(f"Dataset purge failed (non-fatal): {exc}")
            log.error(traceback.format_exc())
            return 0

    # ------------------------------------------------------------------
    # Scanning
    # ------------------------------------------------------------------

    def _scan(self) -> dict[str, str]:
        """Scan all watched dirs; return {repo_path: md5}."""
        result: dict[str, str] = {}
        for prefix, base in WATCH_DIRS.items():
            if not base.exists():
                continue
            try:
                n_tracked = n_skipped = 0
                for path in base.rglob("*"):
                    if not path.is_file():
                        continue
                    if _should_skip(path):
                        n_skipped += 1
                        continue
                    rp = _repo_path(prefix, path, base)
                    result[rp] = _md5(path)
                    n_tracked += 1
                log.debug(f"Scan [{prefix}] {base}: {n_tracked} tracked, {n_skipped} skipped")
            except Exception as exc:
                log.warning(f"Scan error in {base}: {exc}")
                log.warning(traceback.format_exc())
        return result

    # ------------------------------------------------------------------
    # Sync (one shot)
    # ------------------------------------------------------------------

    def sync_once(self) -> tuple[int, int]:
        """Detect changes vs _known and push them to the dataset."""
        if not self._api:
            return 0, 0

        current = self._scan()

        to_upload = [rp for rp, md5 in current.items() if self._known.get(rp) != md5]
        to_delete  = [rp for rp in self._known if rp not in current]

        if not to_upload and not to_delete:
            self._known = current
            return 0, 0

        try:
            from huggingface_hub import CommitOperationAdd, CommitOperationDelete

            ops: list = []
            for rp in to_upload:
                local = _local_path(rp)
                if local and local.exists():
                    log.info(f"  UPLOAD  local:{local}  β†’  dataset:{rp}")
                    ops.append(CommitOperationAdd(path_in_repo=rp, path_or_fileobj=str(local)))
                else:
                    log.warning(f"  UPLOAD skipped (file vanished): {rp}")
            for rp in to_delete:
                log.info(f"  DELETE  dataset:{rp}")
                ops.append(CommitOperationDelete(path_in_repo=rp))

            if not ops:
                self._known = current
                return 0, 0

            n_up  = len(to_upload)
            n_del = len(to_delete)
            msg   = f"sync: +{n_up} ~{n_del}"

            log.info(f"Committing '{msg}' β†’ {HF_DATASET} ({len(ops)} ops) ...")
            commit_info = self._api.create_commit(
                repo_id=HF_DATASET,
                repo_type="dataset",
                commit_message=msg,
                operations=ops,
            )
            url = getattr(commit_info, "commit_url", "ok")
            log.info(f"βœ… Commit created: {url}")
            log.info(f"↑ Pushed: +{n_up} uploads, -{n_del} deletes β†’ {HF_DATASET}")

            self._known = current
            return n_up, n_del

        except Exception as exc:
            log.error(f"❌ Commit FAILED (will retry next cycle): {exc}")
            log.error(traceback.format_exc())
            return 0, 0

    # ------------------------------------------------------------------
    # Watch loop (daemon)
    # ------------------------------------------------------------------

    def watch(self) -> None:
        if not self._api:
            log.info("Watch loop running in no-op mode (HF_TOKEN not set)")
            while True:
                time.sleep(3_600)

        # Step 1: Remove any junk files left in watched dirs from previous contamination
        log.info("=== WATCH: cleaning junk from watched dirs ===")
        removed = self._clean_watched_dirs()
        if removed:
            log.info(f"  Cleaned {removed} junk files from local watched dirs")
        else:
            log.info("  Watched dirs are clean")

        # Step 2: Establish baseline (only clean files now)
        log.info("=== WATCH: establishing baseline scan ===")
        self._known = self._scan()
        log.info(f"Baseline: {len(self._known)} files tracked")
        log.info("Polling every %ds. Watched dirs:", POLL_INTERVAL_SECS)
        for prefix, path in WATCH_DIRS.items():
            log.info(f"  [{prefix}] {path}")

        # Step 3: Purge any stale dataset entries not present in the baseline
        self._purge_dataset_junk(set(self._known.keys()))

        last_checkpoint = time.monotonic()

        # Step 4: Poll loop
        while True:
            time.sleep(POLL_INTERVAL_SECS)
            try:
                now = time.monotonic()

                if (now - last_checkpoint) >= CHECKPOINT_INTERVAL_SECS:
                    # Refresh the baseline FROM DISK; only upload genuinely changed files.
                    # We do NOT reset self._known to {} β€” that would cause mass re-uploads.
                    log.info("[CHECKPOINT] Refreshing baseline from disk ...")
                    self._known = self._scan()
                    last_checkpoint = now

                n_up, n_del = self.sync_once()

                if n_up or n_del:
                    log.info(f"Sync cycle: +{n_up} ~{n_del}")

            except Exception as exc:
                log.error(f"Watch cycle error: {exc}")
                log.error(traceback.format_exc())


# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------

def main() -> None:
    mode = sys.argv[1] if len(sys.argv) > 1 else "watch"

    for d in [
        "/data/share/opencode",
        "/data/config/opencode",
        "/data/cache/opencode",
        "/data/state/opencode",
        "/data/logs",
        "/data/workspaces",
        "/projects/default",
    ]:
        Path(d).mkdir(parents=True, exist_ok=True)

    engine = SyncEngine()

    if mode == "restore":
        engine.restore()
    elif mode == "watch":
        engine.watch()
    elif mode == "sync":
        engine.sync_once()
    else:
        print(f"Usage: sync_engine.py [restore|watch|sync]", file=sys.stderr)
        sys.exit(1)


if __name__ == "__main__":
    main()