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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Package syncctl is the single writer for the private dataset.
//
// The dataset is a git repository, and git does not merge concurrent pushes:
// two writers racing each other means one of them is rejected and its data is
// lost or must be reapplied. Rather than scatter retry logic through the call
// sites, every write goes through one controller that:
//
//  1. Coalesces. Changes are queued and published in batches on a fixed
//     interval, so a burst of traffic becomes one commit instead of hundreds.
//  2. Serialises. Exactly one publish runs at a time, process-wide, so a race
//     with the remote cannot happen by construction.
//  3. Deduplicates. A queued path replaces any earlier queued version of
//     itself, so a file rewritten five times costs one write.
//  4. Skips unchanged files. Content is compared by git blob hash against the
//     remote tree, so an idle system produces no commits at all.
//  5. Adapts. If the remote rate-limits us, the flush interval doubles (with a
//     ceiling) and is restored on success: gentler when pushed, faster when not.
//  6. Never blocks a request. Enqueue is a memory operation with a hard budget;
//     if the queue is full the newest change is dropped and counted, because a
//     slow dataset must not slow down draws.
package syncctl

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"sort"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/archnemix/archx-eval/relay/internal/hf"
)

// Config controls batching and backoff.
type Config struct {
	// Interval is the target flush cadence.
	Interval time.Duration
	// MinInterval and MaxInterval bound the adaptive backoff.
	MinInterval time.Duration
	MaxInterval time.Duration
	// MaxFiles caps a single commit; larger queues split across commits.
	MaxFiles int
	// MaxQueued caps memory. Beyond this, new changes are dropped and counted.
	MaxQueued int
	// MaxRetries is how many times one batch is retried before being dropped.
	MaxRetries int
	// RetryBase is the first retry delay; it doubles each attempt.
	RetryBase time.Duration
}

// DefaultConfig is tuned for a private dataset with modest traffic.
func DefaultConfig() Config {
	return Config{
		Interval:    30 * time.Second,
		MinInterval: 15 * time.Second,
		MaxInterval: 10 * time.Minute,
		MaxFiles:    100,
		MaxQueued:   20000,
		MaxRetries:  4,
		RetryBase:   2 * time.Second,
	}
}

// Stats is a snapshot of controller health, safe to serve from /health.
type Stats struct {
	Queued        int    `json:"queued"`
	Published     int64  `json:"published"`
	Skipped       int64  `json:"skipped"`
	Retries       int64  `json:"retries"`
	Commits       int64  `json:"commits"`
	Failures      int64  `json:"failures"`
	Dropped       int64  `json:"dropped"`
	IntervalMS    int64  `json:"interval_ms"`
	LastFlushUnix int64  `json:"last_flush_unix"`
	LastError     string `json:"last_error,omitempty"`
}

// Controller is the single writer.
type Controller struct {
	cfg    Config
	remote hf.Remote
	log    *slog.Logger

	// mu guards the queue and the version cache. It is held only for short
	// in-memory operations, never across a network call.
	mu    sync.Mutex
	queue map[string][]byte

	// publishMu is held across the whole remote write. This is what actually
	// guarantees "one commit in flight": the queue lock is far too wide to hold
	// during a network call, and far too narrow to provide that guarantee.
	// A probe test asserts peak concurrency stays at 1.
	publishMu sync.Mutex

	// versions caches the remote blob hash per path so we do not re-list the
	// whole tree for every batch. Guarded by mu.
	versions map[string]string
	listed   bool

	interval time.Duration

	published atomic.Int64
	skipped   atomic.Int64
	retries   atomic.Int64
	commits   atomic.Int64
	failures  atomic.Int64
	dropped   atomic.Int64

	lastFlush atomic.Int64
	lastErr   atomic.Value // string

	stopOnce sync.Once
	stop     chan struct{}
	done     chan struct{}
}

// New builds a controller. It does not start the background loop; call Run.
func New(cfg Config, remote hf.Remote, log *slog.Logger) *Controller {
	if cfg.Interval <= 0 {
		cfg.Interval = DefaultConfig().Interval
	}
	if cfg.MaxFiles <= 0 {
		cfg.MaxFiles = DefaultConfig().MaxFiles
	}
	if cfg.MaxQueued <= 0 {
		cfg.MaxQueued = DefaultConfig().MaxQueued
	}
	// MinInterval/MaxInterval bound the adaptive backoff. Leaving them zero is
	// not harmless: widen() clamps to MaxInterval, so a zero ceiling would set
	// the interval to 0 on the first failure and the controller would hammer a
	// failing dataset in a tight loop instead of backing off.
	if cfg.MinInterval <= 0 {
		cfg.MinInterval = DefaultConfig().MinInterval
	}
	if cfg.MaxInterval <= 0 {
		cfg.MaxInterval = DefaultConfig().MaxInterval
	}
	if cfg.MaxInterval < cfg.Interval {
		cfg.MaxInterval = cfg.Interval
	}
	if cfg.MinInterval > cfg.Interval {
		cfg.MinInterval = cfg.Interval
	}
	if log == nil {
		log = slog.New(slog.NewTextHandler(io.Discard, nil))
	}
	return &Controller{
		cfg:      cfg,
		remote:   remote,
		log:      log,
		queue:    make(map[string][]byte, 64),
		versions: make(map[string]string, 256),
		interval: cfg.Interval,
		stop:     make(chan struct{}),
		done:     make(chan struct{}),
	}
}

// Enqueue records a change. It never blocks on the network and never returns an
// error to a request path: if the queue is full the change is dropped and
// counted, because losing a sync record is better than stalling a draw.
func (c *Controller) Enqueue(path string, content []byte) {
	if path == "" {
		return
	}
	c.mu.Lock()
	defer c.mu.Unlock()
	if _, exists := c.queue[path]; !exists && len(c.queue) >= c.cfg.MaxQueued {
		c.dropped.Add(1)
		return
	}
	// Replace, never append: the newest version of a path wins.
	c.queue[path] = content
}

// EnqueueFile is a convenience wrapper.
func (c *Controller) EnqueueFile(f hf.File) { c.Enqueue(f.Path, f.Content) }

// Queued reports how many distinct paths are waiting.
func (c *Controller) Queued() int {
	c.mu.Lock()
	defer c.mu.Unlock()
	return len(c.queue)
}

// Stats snapshots counters.
func (c *Controller) Stats() Stats {
	s := Stats{
		Queued:        c.Queued(),
		Published:     c.published.Load(),
		Skipped:       c.skipped.Load(),
		Retries:       c.retries.Load(),
		Commits:       c.commits.Load(),
		Failures:      c.failures.Load(),
		Dropped:       c.dropped.Load(),
		IntervalMS:    int64(c.currentInterval() / time.Millisecond),
		LastFlushUnix: c.lastFlush.Load(),
	}
	if v, ok := c.lastErr.Load().(string); ok {
		s.LastError = v
	}
	return s
}

func (c *Controller) currentInterval() time.Duration {
	c.mu.Lock()
	defer c.mu.Unlock()
	return c.interval
}

// Run drives the flush loop until the context is cancelled or Stop is called.
// A final flush is attempted on shutdown so queued work is not silently lost.
func (c *Controller) Run(ctx context.Context) {
	defer close(c.done)
	for {
		timer := time.NewTimer(c.currentInterval())
		select {
		case <-ctx.Done():
			timer.Stop()
			c.flushAll(context.Background())
			return
		case <-c.stop:
			timer.Stop()
			c.flushAll(context.Background())
			return
		case <-timer.C:
			c.flushAll(ctx)
		}
	}
}

// Stop asks the loop to finish and waits for it.
func (c *Controller) Stop() {
	c.stopOnce.Do(func() { close(c.stop) })
	<-c.done
}

// FlushNow publishes everything queued right now. Used by -sync and by tests.
func (c *Controller) FlushNow(ctx context.Context) error { return c.flushAll(ctx) }

// flushAll repeatedly publishes batches until the queue is empty or a batch
// fails. Every iteration takes the lock, so no other publish can interleave.
func (c *Controller) flushAll(ctx context.Context) error {
	var lastErr error
	for {
		err := c.flushOnce(ctx)
		if err != nil {
			lastErr = err
			if !isRetryable(err) {
				break
			}
		}
		if c.Queued() == 0 {
			break
		}
	}
	return lastErr
}

// flushOnce publishes at most MaxFiles changed files as a single commit.
func (c *Controller) flushOnce(ctx context.Context) error {
	// Lock ordering is always publishMu then mu, never the reverse. Enqueue
	// only ever takes mu, so it can never block behind a network call.
	c.publishMu.Lock()

	c.mu.Lock()
	if len(c.queue) == 0 {
		c.mu.Unlock()
		c.publishMu.Unlock()
		return nil
	}

	// Snapshot a batch and clear it from the queue. Clearing before the network
	// call means a change enqueued during the call lands in the next batch
	// instead of being silently included in a commit we might retry.
	batch := make(map[string][]byte, len(c.queue))
	paths := make([]string, 0, len(c.queue))
	for p := range c.queue {
		paths = append(paths, p)
	}
	sort.Strings(paths)
	if len(paths) > c.cfg.MaxFiles {
		paths = paths[:c.cfg.MaxFiles]
	}
	for _, p := range paths {
		batch[p] = c.queue[p]
		delete(c.queue, p)
	}
	for _, p := range paths {
		if !c.listed {
			break
		}
		if cached, ok := c.versions[p]; ok && cached == hf.BlobSHA(batch[p]) {
			delete(batch, p)
			c.skipped.Add(1)
		}
	}
	c.mu.Unlock()

	if len(batch) == 0 {
		c.publishMu.Unlock()
		return nil
	}

	// Refresh the remote tree once per process, and again whenever the remote
	// rejects a commit for being out of date.
	if err := c.ensureListed(ctx); err != nil {
		defer c.publishMu.Unlock()
		return c.requeue(batch, err)
	}

	c.mu.Lock()
	for p, content := range batch {
		if cached, ok := c.versions[p]; ok && cached == hf.BlobSHA(content) {
			delete(batch, p)
			c.skipped.Add(1)
		}
	}
	c.mu.Unlock()
	if len(batch) == 0 {
		c.publishMu.Unlock()
		return nil
	}

	files := make([]hf.File, 0, len(batch))
	for p, content := range batch {
		files = append(files, hf.File{Path: p, Content: content})
	}
	sort.Slice(files, func(i, j int) bool { return files[i].Path < files[j].Path })

	summary := fmt.Sprintf("relay sync: %d file(s) at %s",
		len(files), time.Now().UTC().Format(time.RFC3339))

	retried, err := c.publishWithRetry(ctx, summary, files)
	if retried {
		// The remote pushed back at us even though we eventually got through.
		// Slow down rather than hammering it again on the next tick.
		c.retries.Add(1)
		c.widen()
	}
	if err != nil {
		c.failures.Add(1)
		c.lastErr.Store(err.Error())
		c.widen()
		c.publishMu.Unlock()
		return c.requeue(batch, err)
	}

	// Success: record the new versions, narrow the interval back, count it.
	// Each lock is taken, released, and never held across another acquisition.
	c.mu.Lock()
	for _, f := range files {
		c.versions[f.Path] = f.Version()
	}
	remaining := len(c.queue)
	c.mu.Unlock()

	c.published.Add(int64(len(files)))
	c.commits.Add(1)
	c.lastFlush.Store(time.Now().Unix())
	c.lastErr.Store("")
	c.narrow()

	c.publishMu.Unlock()

	c.log.Info("dataset sync published",
		"files", len(files), "queued", remaining, "interval_s", int(c.currentInterval()/time.Second))
	return nil
}

// ensureListed loads the remote tree once so unchanged files can be skipped.
func (c *Controller) ensureListed(ctx context.Context) error {
	c.mu.Lock()
	need := !c.listed
	c.mu.Unlock()
	if !need {
		return nil
	}

	files, err := c.remote.List(ctx, "")
	if err != nil {
		return err
	}

	c.mu.Lock()
	for _, f := range files {
		c.versions[f.Path] = f.Version
	}
	c.listed = true
	c.mu.Unlock()

	c.log.Info("dataset tree indexed", "files", len(files))
	return nil
}

// publishWithRetry retries one commit, honouring Retry-After when given. The
// first return value reports whether at least one retry was needed.
func (c *Controller) publishWithRetry(ctx context.Context, summary string, files []hf.File) (bool, error) {
	delay := c.cfg.RetryBase
	var lastErr error
	retried := false
	for attempt := 0; attempt <= c.cfg.MaxRetries; attempt++ {
		if attempt > 0 {
			retried = true
			select {
			case <-ctx.Done():
				return retried, ctx.Err()
			case <-time.After(delay):
			}
			delay *= 2
		}
		err := c.remote.Publish(ctx, summary, files)
		if err == nil {
			return retried, nil
		}
		lastErr = err
		if !isRetryable(err) {
			return retried, err
		}
		// The remote tree may have moved; force a re-list on the next attempt.
		c.mu.Lock()
		c.listed = false
		c.mu.Unlock()

		if apiErr, ok := err.(*hf.APIError); ok && apiErr.RetryAfter > 0 {
			delay = apiErr.RetryAfter
		}
		c.log.Warn("dataset publish failed, will retry",
			"attempt", attempt+1, "err", err, "next_delay_ms", delay.Milliseconds())
	}
	return retried, lastErr
}

// requeue puts a failed batch back so the next flush retries it. Existing
// entries win: a newer version must not be overwritten by a stale one.
func (c *Controller) requeue(batch map[string][]byte, cause error) error {
	c.mu.Lock()
	defer c.mu.Unlock()
	restored := 0
	for p, content := range batch {
		if _, newer := c.queue[p]; newer {
			continue
		}
		c.queue[p] = content
		restored++
	}
	if restored > 0 {
		c.log.Warn("dataset batch requeued after failure", "files", restored, "err", cause)
	}
	return cause
}

// widen doubles the flush interval, up to the ceiling.
func (c *Controller) widen() {
	c.mu.Lock()
	defer c.mu.Unlock()
	next := c.interval * 2
	if next > c.cfg.MaxInterval {
		next = c.cfg.MaxInterval
	}
	if next != c.interval {
		c.log.Warn("backing off dataset sync", "interval_s", int(next/time.Second))
	}
	c.interval = next
}

// narrow eases the interval back towards the configured target.
func (c *Controller) narrow() {
	c.mu.Lock()
	defer c.mu.Unlock()
	next := c.interval * 3 / 4
	if next < c.cfg.MinInterval {
		next = c.cfg.MinInterval
	}
	if next < c.cfg.Interval {
		next = c.cfg.Interval
	}
	c.interval = next
}

// isRetryable reports whether a publish failure is worth retrying.
func isRetryable(err error) bool {
	var apiErr *hf.APIError
	if errors.As(err, &apiErr) {
		return apiErr.Retryable()
	}
	// A transport error (DNS, reset, timeout) is worth retrying; a malformed
	// payload is not, but we cannot distinguish those here, so be optimistic.
	if errors.Is(err, context.Canceled) {
		return false
	}
	return true
}

// Restore downloads a subtree from the dataset into a local directory. Reads
// are parallel, so this is fast even though writes are strictly ordered.
func Restore(ctx context.Context, remote hf.Remote, prefix, destRoot string, concurrency int) (int, error) {
	files, err := remote.List(ctx, prefix)
	if err != nil {
		return 0, err
	}
	paths := make([]string, 0, len(files))
	for _, f := range files {
		paths = append(paths, f.Path)
	}
	// Strip the prefix so a subtree's contents land directly in destRoot. The
	// bank is the reason this matters: restore("bank", BANK_DIR) must produce
	// BANK_DIR/python/easy.jsonl, not BANK_DIR/bank/python/easy.jsonl, or the
	// loader finds nothing and the relay serves 503 forever.
	d := hf.Downloader{Remote: remote, Concurrency: concurrency, Prefix: prefix}
	return d.DownloadAll(ctx, destRoot, paths)
}

// SanitizePath trims a leading slash and query so callers cannot smuggle a
// traversal sequence through a config value.
func SanitizePath(p string) string {
	// Trim both sides of every slash: a config value like "identities//" must
	// not become "identities/" and then be joined into a doubled path.
	return strings.Trim(strings.TrimSpace(p), "/")
}