/**
 * Thrown by ingest's afterWrite hook when a concurrent worker has already
 * inserted github_event_log for the same idempotency_key. Roll back the
 * SAVEPOINT so exactly one memory row exists per key.
 */
export class DuplicateIdempotencyError extends Error {
    idempotencyKey;
    constructor(idempotencyKey) {
        super(`duplicate github idempotency_key: ${idempotencyKey}`);
        this.name = 'DuplicateIdempotencyError';
        this.idempotencyKey = idempotencyKey;
    }
}
export function hasSeenKey(db, idempotencyKey) {
    const row = db.prepare(`SELECT 1 FROM github_event_log WHERE idempotency_key = ?`).get(idempotencyKey);
    return !!row;
}
export function markKeySeen(db, args) {
    db.prepare(`INSERT OR IGNORE INTO github_event_log (idempotency_key, delivery_id, event_name, ingested_at, memory_id) VALUES (?, ?, ?, ?, ?)`).run(args.idempotencyKey, args.deliveryId, args.eventName, new Date().toISOString(), args.memoryId);
}
export function lookupMemoryByKey(db, idempotencyKey) {
    const row = db.prepare(`SELECT memory_id FROM github_event_log WHERE idempotency_key = ?`).get(idempotencyKey);
    return row?.memory_id ?? null;
}
//# sourceMappingURL=idempotency.js.map