/** consumer 必须消费的字段契约 */
export const CONSUMER_FIELD_CONTRACTS = {
    router: ["routes", "primary_triggers", "negative_triggers", "specificity"],
    expander: ["authority", "priority", "required_evidence", "asset_kind"],
    reviewer: ["asset_path", "contract_decision", "lifecycle_stage"],
    verifier: ["hard_blocks", "required_evidence", "fallback", "lifecycle_status"],
    deliverer: ["asset_path", "contract_decision", "lifecycle_stage"],
    adapter_prompt: ["中文语义", "模板变量", "输出格式", "消费快照摘要"],
    sync_migration: ["version", "schema", "hash", "user_modified", "migration_note"],
    init: ["asset_path", "contract_decision", "target_path"],
    sync_templates: ["asset_path", "contract_decision", "source_hash"],
    knowledge_index: ["asset_path", "contract_decision", "entry_type"],
    knowledge_selection: ["asset_path", "contract_decision", "lifecycle_stage"],
    scaffold: ["asset_path", "contract_decision", "template_vars"],
    contract_gate: ["asset_path", "contract_decision", "consume_category"],
    pipeline_sync: ["asset_path", "contract_decision", "target_path"],
};
/**
 * ConsumptionTraceStore — 可实例化的消费追踪存储。
 *
 * 使用类实例替代模块级全局状态，支持测试隔离。
 * 生产代码使用 globalTraceStore 单例，测试可创建独立实例。
 */
export class ConsumptionTraceStore {
    _store = [];
    _maxTrace;
    constructor(maxTrace = 500) {
        this._maxTrace = maxTrace;
    }
    /** 记录一条消费 trace */
    record(entry) {
        this._store.push(entry);
        if (this._store.length > this._maxTrace)
            this._store.splice(0, this._store.length - this._maxTrace);
    }
    /** 读取所有消费 trace */
    getAll() {
        return this._store;
    }
    /** 按消费者筛选 */
    getByConsumer(consumer) {
        return this._store.filter(t => t.consumer === consumer);
    }
    /** 清空 trace（测试用） */
    clear() {
        this._store.length = 0;
    }
    /** 验证 trace 条目完整性 */
    validateEntry(entry) {
        const required = [
            "consumer", "asset_id", "fields_consumed", "matched_reason", "task_id_or_route", "consumed_at",
        ];
        const missing = required.filter(f => !entry[f]);
        return { valid: missing.length === 0, missing };
    }
    /** 验证所有 trace */
    validateAll() {
        const issues = [];
        let invalidCount = 0;
        for (const entry of this._store) {
            const result = this.validateEntry(entry);
            if (!result.valid) {
                invalidCount++;
                issues.push(`trace entry invalid: consumer=${entry.consumer} asset_id=${entry.asset_id} missing=${result.missing.join(",")}`);
            }
        }
        return { valid: invalidCount === 0, invalidCount, issues };
    }
    /** 获取各消费者 trace 数量 */
    getTraceCounts() {
        const consumers = ["router", "expander", "verifier", "adapter_prompt", "sync_migration", "init", "sync_templates", "knowledge_index", "scaffold", "contract_gate", "pipeline_sync"];
        const counts = {};
        for (const c of consumers) {
            counts[c] = this._store.filter(t => t.consumer === c).length;
        }
        return counts;
    }
    /** 逐 consumer 校验 fields_consumed 是否满足契约 */
    validateFieldContracts() {
        const issues = [];
        for (const [consumer, requiredFields] of Object.entries(CONSUMER_FIELD_CONTRACTS)) {
            const traces = this._store.filter(t => t.consumer === consumer);
            if (traces.length === 0) {
                issues.push(`${consumer}: 无 trace 条目`);
                continue;
            }
            // 至少一条 trace 必须包含契约中的所有字段
            const hasComplete = traces.some(t => requiredFields.every(f => t.fields_consumed.includes(f)));
            if (!hasComplete) {
                const sample = traces[0].fields_consumed;
                issues.push(`${consumer}: fields_consumed 不满足契约 — 需要 [${requiredFields.join(",")}] 但仅发现 [${sample.join(",")}]`);
            }
        }
        return { valid: issues.length === 0, issues };
    }
}
/** 全局单例 — 生产代码使用此实例 */
export const globalTraceStore = new ConsumptionTraceStore();
// ── 向后兼容的模块级函数（委托到 globalTraceStore） ──
/** 记录一条消费 trace */
export function recordConsumptionTrace(entry) {
    globalTraceStore.record(entry);
}
/** 读取所有消费 trace */
export function getConsumptionTraces() {
    return globalTraceStore.getAll();
}
/** 按消费者筛选 */
export function getTracesByConsumer(consumer) {
    return globalTraceStore.getByConsumer(consumer);
}
/** 清空 trace（测试用） */
export function clearConsumptionTraces() {
    globalTraceStore.clear();
}
/** 验证所有 trace */
export function validateAllTraces() {
    return globalTraceStore.validateAll();
}
/** 获取各消费者 trace 数量 */
export function getTraceCounts() {
    return globalTraceStore.getTraceCounts();
}
/** 逐 consumer 校验 fields_consumed 是否满足契约 */
export function validateTraceFieldContracts() {
    return globalTraceStore.validateFieldContracts();
}
//# sourceMappingURL=consumption_trace_store.js.map