// Copyright 2024-2026 Tymofii Pidlisnyi. Apache-2.0 license. See LICENSE.
/**
 * APS v2 End-to-End Intent Binding (Distributed Responsibility Defense)
 *
 * In multi-agent pipelines, each agent says "I only performed my bounded task."
 * The original purpose gets lost in the delegation chain. This module carries
 * the original intent through the entire chain and checks for drift at each hop.
 */
const intentChains = new Map();
export function createIntentChain(chainId, originalIntent, principalId) {
    const chain = {
        original_intent: originalIntent,
        original_principal: principalId,
        chain: [],
    };
    intentChains.set(chainId, chain);
    return chain;
}
export function extendChain(chainId, agentId, localIntent) {
    const chain = intentChains.get(chainId);
    if (!chain)
        throw new Error(`Chain ${chainId} not found`);
    chain.chain.push({
        agent_id: agentId, local_intent: localIntent,
        hop: chain.chain.length + 1, timestamp: new Date().toISOString(),
    });
    return chain;
}
// Check if local intent at each hop drifts from original
export function validateChainIntegrity(chainId) {
    const chain = intentChains.get(chainId);
    if (!chain)
        throw new Error(`Chain ${chainId} not found`);
    const origWords = new Set(extractWords(chain.original_intent));
    const drifts = chain.chain.map(hop => {
        const localWords = new Set(extractWords(hop.local_intent));
        const intersection = [...origWords].filter(w => localWords.has(w)).length;
        const union = new Set([...origWords, ...localWords]).size;
        const similarity = union > 0 ? intersection / union : 1;
        return { agent_id: hop.agent_id, hop: hop.hop, drift: Math.round((1 - similarity) * 1000) / 1000 };
    });
    const maxDrift = drifts.length > 0 ? Math.max(...drifts.map(d => d.drift)) : 0;
    return {
        chain_id: chainId, original_intent: chain.original_intent,
        hops: chain.chain.length, drift_scores: drifts,
        max_drift: maxDrift, integrity_ok: maxDrift < 0.7,
    };
}
function extractWords(text) {
    return text.toLowerCase().replace(/[^a-z0-9\s]/g, ' ').split(/\s+/).filter(w => w.length > 2);
}
export function getIntentChain(chainId) { return intentChains.get(chainId); }
export function clearIntentBindingStores() { intentChains.clear(); }
//# sourceMappingURL=intent-binding.js.map