/**
 * File-backed heartbeat checkpoint repository.
 *
 * Owns single-checkpoint file I/O for one-off stored heartbeat usage. Scheduled
 * heartbeat tasks use `FileHeartbeatTaskRepository` because task checkpoints
 * are colocated with task/run storage.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname } from 'node:path';
import { AgentLoopCheckpointSchema } from '../tasks/schemas.js';
export class FileHeartbeatCheckpointRepository {
    path;
    constructor(options) {
        this.path = options.path;
    }
    async load() {
        if (!existsSync(this.path)) {
            return undefined;
        }
        try {
            const parsed = AgentLoopCheckpointSchema.safeParse(JSON.parse(readFileSync(this.path, 'utf8')));
            return parsed.success ? parsed.data : undefined;
        }
        catch {
            return undefined;
        }
    }
    async save(checkpoint) {
        mkdirSync(dirname(this.path), { recursive: true });
        writeFileSync(this.path, `${JSON.stringify(AgentLoopCheckpointSchema.parse(checkpoint), null, 2)}\n`);
    }
}
//# sourceMappingURL=repository.js.map