/**
 * Agent trust scorer — hierarchical trust decay for sub-agents.
 *
 * Agents encode their lineage in the identifier using '>' separators:
 *   "user-spawned"                    → base trust 0.9
 *   "user-spawned>task-1"             → 0.9 × 0.7 = 0.63
 *   "user-spawned>task-1>subtask-2"   → 0.9 × 0.7² = 0.44
 *   "cron"                            → base trust 0.5
 *   "agent-spawned"                   → base trust 0.3
 */
export const DEFAULT_AGENT_CONFIG = {
    originScores: {
        'user-spawned': 0.9,
        'user-approved': 0.85,
        'cron': 0.5,
        'agent-spawned': 0.3,
        'web': 0.2,
    },
    decayFactor: 0.7,
    maxDepth: 5,
};
/**
 * Score an agent based on its hierarchy identifier.
 * Returns a trust score between 0.0 and 1.0.
 */
export function scoreAgent(identifier, config = DEFAULT_AGENT_CONFIG) {
    const parts = identifier.split('>');
    const origin = parts[0];
    const depth = parts.length - 1;
    // Circuit breaker: block agents beyond max depth
    if (depth > config.maxDepth)
        return 0;
    const baseScore = config.originScores[origin] ?? 0.3;
    return Math.round(baseScore * Math.pow(config.decayFactor, depth) * 1000) / 1000;
}
/**
 * Get the depth of an agent in its hierarchy (0 = parent).
 */
export function getAgentDepth(identifier) {
    return identifier.split('>').length - 1;
}
/**
 * Build a human-readable hierarchy showing trust at each level.
 */
export function buildAgentHierarchy(identifier, config = DEFAULT_AGENT_CONFIG) {
    const parts = identifier.split('>');
    const hierarchy = [];
    for (let i = 0; i < parts.length; i++) {
        const path = parts.slice(0, i + 1).join('>');
        const score = scoreAgent(path, config);
        hierarchy.push(`${'  '.repeat(i)}${parts[i]} (trust=${score.toFixed(3)})`);
    }
    return hierarchy;
}
