/**
 * Delegation capacity validation — depth + width caps applied at the kernel
 * boundary before any write (session-hierarchy.md §6.5).
 *
 * Both `spawnSubSession()` and `broadcastHandoff()` call this module before
 * committing; violations abort with typed {@link DelegationCapacityExceeded}
 * and no partial writes occur (Convention #0: no workarounds; Convention #5:
 * deny-by-default).
 */
/**
 * Raised when a spawn / broadcast would exceed the project's configured
 * capacity caps. The check is a precondition — no partial writes occur on
 * violation (session-hierarchy.md §6.5).
 */
export class DelegationCapacityExceeded extends Error {
    details;
    constructor(details) {
        super(`Delegation capacity exceeded: ${details.dimension} ${details.current}/${details.limit} on ${details.sessionId}`);
        this.name = 'DelegationCapacityExceeded';
        this.details = details;
    }
}
/**
 * Default validator backed by {@link SessionStore}. Uses `getAncestry` for
 * depth (root-to-self chain length) and `getChildren` for width (count of
 * existing direct sub-sessions).
 */
export class DefaultCapacityValidator {
    store;
    constructor(store) {
        this.store = store;
    }
    async validateDepth(parentSessionId, projectMaxDepth, tenantId) {
        const ancestry = await this.store.getAncestry(parentSessionId, tenantId);
        // depth is 0-indexed — root session has depth 0. New child depth =
        // ancestry length (root-to-parent inclusive).
        const newDepth = ancestry.length;
        if (newDepth > projectMaxDepth) {
            throw new DelegationCapacityExceeded({
                dimension: 'depth',
                current: newDepth,
                limit: projectMaxDepth,
                sessionId: parentSessionId,
            });
        }
    }
    async validateWidth(parentSessionId, pendingNewChildren, projectMaxWidth, tenantId) {
        const existing = await this.store.getChildren(parentSessionId, tenantId);
        const total = existing.length + pendingNewChildren;
        if (total > projectMaxWidth) {
            throw new DelegationCapacityExceeded({
                dimension: 'width',
                current: total,
                limit: projectMaxWidth,
                sessionId: parentSessionId,
            });
        }
    }
}
//# sourceMappingURL=capacity.js.map