|
| 1 | +/** |
| 2 | + * WIP (work-in-progress) checkpoint loop for in-progress eval runs. |
| 3 | + * |
| 4 | + * Periodically force-pushes the partial run output directory to a unique |
| 5 | + * non-default branch (`agentv/inflight/<hostname>/<run-dir-basename>`) in the |
| 6 | + * configured results repository. This protects against pod/process loss by |
| 7 | + * keeping completed-test results durable without requiring PVC or S3. |
| 8 | + * |
| 9 | + * Branch lifecycle: |
| 10 | + * 1. Start: set up a persistent git worktree for the WIP branch. |
| 11 | + * 2. Running: every ~30s, copy run dir → worktree, amend-commit, force-push. |
| 12 | + * 3. Success: after final publish to the normal results branch, delete the WIP branch. |
| 13 | + * 4. Failure: leave the WIP branch for manual recovery. |
| 14 | + * |
| 15 | + * Manual recovery from a WIP branch: |
| 16 | + * git clone <results-repo> /tmp/recovery |
| 17 | + * cd /tmp/recovery && git checkout agentv/inflight/<hostname>/<run-dir> |
| 18 | + * cp -r .agentv/results/runs/<run-dir> <project>/.agentv/results/runs/ |
| 19 | + * agentv eval <eval-file> --output <project>/.agentv/results/runs/<run-dir> --resume |
| 20 | + * |
| 21 | + * All checkpoint operations are best-effort: failures are logged as warnings |
| 22 | + * and never propagate to the eval run. |
| 23 | + */ |
| 24 | + |
| 25 | +import { |
| 26 | + type NormalizedResultsConfig, |
| 27 | + type WipWorktreeHandle, |
| 28 | + buildWipBranchName, |
| 29 | + deleteWipBranch, |
| 30 | + pushWipCheckpoint, |
| 31 | + setupWipWorktree, |
| 32 | +} from '@agentv/core'; |
| 33 | + |
| 34 | +const WIP_CHECKPOINT_INTERVAL_MS = 30_000; |
| 35 | + |
| 36 | +export interface WipCheckpointLoopDependencies { |
| 37 | + readonly buildWipBranchName: (runDir: string) => string; |
| 38 | + readonly deleteWipBranch: typeof deleteWipBranch; |
| 39 | + readonly pushWipCheckpoint: typeof pushWipCheckpoint; |
| 40 | + readonly setupWipWorktree: typeof setupWipWorktree; |
| 41 | +} |
| 42 | + |
| 43 | +const defaultDependencies: WipCheckpointLoopDependencies = { |
| 44 | + buildWipBranchName, |
| 45 | + deleteWipBranch, |
| 46 | + pushWipCheckpoint, |
| 47 | + setupWipWorktree, |
| 48 | +}; |
| 49 | + |
| 50 | +function warnCheckpointError(context: string, error: unknown): void { |
| 51 | + const message = error instanceof Error ? error.message : String(error); |
| 52 | + console.warn(`WIP checkpoint: ${context}: ${message}`); |
| 53 | +} |
| 54 | + |
| 55 | +export class WipCheckpointLoop { |
| 56 | + readonly wipBranch: string; |
| 57 | + private readonly config: NormalizedResultsConfig; |
| 58 | + private readonly runDir: string; |
| 59 | + private readonly destinationPath: string; |
| 60 | + private readonly intervalMs: number; |
| 61 | + private readonly deps: WipCheckpointLoopDependencies; |
| 62 | + private handle: WipWorktreeHandle | undefined; |
| 63 | + private timer: ReturnType<typeof setInterval> | undefined; |
| 64 | + private checkpointInFlight: Promise<void> | undefined; |
| 65 | + private active = false; |
| 66 | + |
| 67 | + constructor(params: { |
| 68 | + readonly config: NormalizedResultsConfig; |
| 69 | + readonly runDir: string; |
| 70 | + readonly destinationPath: string; |
| 71 | + readonly intervalMs?: number; |
| 72 | + readonly dependencies?: WipCheckpointLoopDependencies; |
| 73 | + }) { |
| 74 | + this.config = params.config; |
| 75 | + this.runDir = params.runDir; |
| 76 | + this.destinationPath = params.destinationPath; |
| 77 | + this.intervalMs = params.intervalMs ?? WIP_CHECKPOINT_INTERVAL_MS; |
| 78 | + this.deps = params.dependencies ?? defaultDependencies; |
| 79 | + this.wipBranch = this.deps.buildWipBranchName(params.runDir); |
| 80 | + } |
| 81 | + |
| 82 | + async start(): Promise<void> { |
| 83 | + try { |
| 84 | + this.handle = await this.deps.setupWipWorktree({ |
| 85 | + config: this.config, |
| 86 | + wipBranch: this.wipBranch, |
| 87 | + }); |
| 88 | + } catch (err) { |
| 89 | + warnCheckpointError('failed to set up WIP worktree', err); |
| 90 | + return; |
| 91 | + } |
| 92 | + this.active = true; |
| 93 | + this.timer = setInterval(() => { |
| 94 | + this.runCheckpointIfIdle(); |
| 95 | + }, this.intervalMs); |
| 96 | + // Unref so the timer never prevents process exit. |
| 97 | + this.timer.unref?.(); |
| 98 | + } |
| 99 | + |
| 100 | + private runCheckpointIfIdle(): void { |
| 101 | + if (!this.active || this.checkpointInFlight) return; |
| 102 | + this.checkpointInFlight = this.checkpoint() |
| 103 | + .catch((err) => warnCheckpointError('push failed', err)) |
| 104 | + .finally(() => { |
| 105 | + this.checkpointInFlight = undefined; |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + private async checkpoint(): Promise<void> { |
| 110 | + if (!this.handle) return; |
| 111 | + await this.deps.pushWipCheckpoint({ |
| 112 | + handle: this.handle, |
| 113 | + sourceDir: this.runDir, |
| 114 | + destinationPath: this.destinationPath, |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + /** Stop the loop and clean up the local worktree. Does NOT delete the remote WIP branch. */ |
| 119 | + async stop(): Promise<void> { |
| 120 | + this.active = false; |
| 121 | + if (this.timer !== undefined) { |
| 122 | + clearInterval(this.timer); |
| 123 | + this.timer = undefined; |
| 124 | + } |
| 125 | + await this.checkpointInFlight; |
| 126 | + if (this.handle) { |
| 127 | + await this.handle |
| 128 | + .cleanup() |
| 129 | + .catch((err) => warnCheckpointError('worktree cleanup failed', err)); |
| 130 | + this.handle = undefined; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Stop the loop and delete the remote WIP branch. |
| 136 | + * Call after a successful run to keep the results repo tidy. |
| 137 | + */ |
| 138 | + async stopAndDeleteWipBranch(): Promise<void> { |
| 139 | + await this.stop(); |
| 140 | + try { |
| 141 | + await this.deps.deleteWipBranch({ config: this.config, wipBranch: this.wipBranch }); |
| 142 | + } catch (err) { |
| 143 | + warnCheckpointError(`failed to delete remote branch ${this.wipBranch}`, err); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments