g1t/services/runner/src/backup.ts

48 lines1,693 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Merge branch 'worktree-agent-ac5b181a013e54348'1/**
2 * Nightly backups (`RunnerService.startBackups`): each sweep claims a few
3 * of the backups the repos service queued and starts a sandbox for each,
4 * which runs the runner's `backup` mode (crates/runner backup.rs). The
5 * flow is in `crates/contracts/src/backups.rs`. Pure, so it is tested on
6 * its own.
7 */
8import type { BackupClaim } from "@g1t/contracts";
9
10/** A backup's time cap: a clone and a bundle of at most 1 GB. */
11export const BACKUP_MINUTES = 60;
12
13/** How many backups one sweep starts, and how many may run at once. */
14export type BackupPace = { perSweep: number; running: number };
15
16const DEFAULT_PACE: BackupPace = { perSweep: 4, running: 6 };
17
18function count(value: string | undefined, fallback: number): number {
19 if (value === undefined || value.trim() === "") return fallback;
20 const parsed = Number(value);
21 return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
22}
23
24/**
25 * BACKUPS_PER_SWEEP and BACKUPS_RUNNING, or their defaults. `0` per sweep
26 * starts none: backups are off.
27 */
28export function backupPace(perSweep: string | undefined, running: string | undefined): BackupPace {
29 return {
30 perSweep: count(perSweep, DEFAULT_PACE.perSweep),
31 running: count(running, DEFAULT_PACE.running),
32 };
33}
34
35/** One sandbox per job: asking twice starts nothing twice. */
36export function backupSandboxName(claim: BackupClaim): string {
37 return `backup-${claim.jobId}`;
38}
39
40/** What the sandbox is started with: its job, and nothing that reads git. */
41export function backupEnv(claim: BackupClaim, api: string): Record<string, string> {
42 return {
43 MODE: "backup",
44 G1T_API: api,
45 BACKUP_JOB: claim.jobId,
46 BACKUP_TOKEN: claim.token,
47 };
48}

This file's history is long; its oldest lines are credited to the oldest commit read.