export class CheckpointError extends Error {
    constructor(message) {
        super(message);
        this.name = "CheckpointError";
    }
}
export class RestoreSignal extends Error {
    checkpoint;
    options;
    constructor(checkpoint, options) {
        super(`Restoring to checkpoint ${checkpoint.id}`);
        this.name = "RestoreSignal";
        this.checkpoint = checkpoint;
        this.options = options;
    }
}
export class ConcurrentInterruptError extends Error {
    constructor(message) {
        super(message);
        this.name = "ConcurrentInterruptError";
    }
}
export class AgencyCancelledError extends Error {
    constructor(reason) {
        super(reason ?? "Agent execution was cancelled");
        this.name = "AgencyCancelledError";
    }
}
/** Thrown by `runHandlerChain` (lib/runtime/interrupts.ts) when nested
 *  handler-chain dispatch depth exceeds `MAX_HANDLER_CHAIN_DEPTH`. Almost
 *  always indicates a handler raised an interrupt that re-enters the same
 *  handler (directly or via the chain dispatcher visiting every handler).
 *  Carries the interrupt kind that tripped the limit so the diagnostic
 *  points at the right place. */
export class HandlerRecursionError extends Error {
    kind;
    depth;
    constructor(kind, depth) {
        super(`Handler chain dispatch nested ${depth} levels deep while handling ` +
            `interrupt of kind "${kind}". This usually means a handler raised an ` +
            `interrupt that re-entered itself (the chain dispatcher visits every ` +
            `handler, even after one approves). Check whether the handler's body ` +
            `calls anything that raises an interrupt (\`with approve\`, file I/O, ` +
            `\`input()\`, etc.) and guard against re-entry — e.g. flip a sentinel ` +
            `flag BEFORE the call, not after.`);
        this.name = "HandlerRecursionError";
        this.kind = kind;
        this.depth = depth;
    }
}
export function isAbortError(error) {
    if (error instanceof AgencyCancelledError)
        return true;
    if (error instanceof DOMException && error.name === "AbortError")
        return true;
    if (error instanceof Error && error.name === "AbortError")
        return true;
    return false;
}
