export async function checkpointStateBeforeUpdate(config, fileStore) {
    const checkpointOptions = config.role === 'main' ? { validateSnapshot: validateMainSnapshot } : {};
    const { snapshot, disk } = await fileStore.checkpoint(checkpointOptions);
    if (config.role === 'main') {
        validateMainDiskCheckpoint(snapshot, disk);
    }
}
function validateMainSnapshot(snapshot) {
    requireMainPassword(snapshot);
}
function validateMainDiskCheckpoint(snapshot, disk) {
    const snapshotPassword = requireMainPassword(snapshot);
    if (!disk.passwordCredential) {
        throw new Error('main state checkpoint did not preserve the password credential on disk');
    }
    if (disk.passwordCredential.hash !== snapshotPassword.hash ||
        disk.passwordCredential.salt !== snapshotPassword.salt ||
        disk.passwordCredential.createdAt !== snapshotPassword.createdAt ||
        disk.passwordCredential.updatedAt !== snapshotPassword.updatedAt) {
        throw new Error('main state checkpoint changed the password credential on disk');
    }
    const diskAgentIds = new Set(disk.agents.map((agent) => agent.id));
    const missingAgentIds = snapshot.agents.map((agent) => agent.id).filter((agentId) => !diskAgentIds.has(agentId));
    if (missingAgentIds.length > 0) {
        throw new Error(`main state checkpoint lost ${missingAgentIds.length} registered secondary server(s)`);
    }
}
function requireMainPassword(snapshot) {
    if (!snapshot.passwordCredential) {
        throw new Error('main state has no password credential in memory');
    }
    return snapshot.passwordCredential;
}
//# sourceMappingURL=stateCheckpoint.js.map