/**
 * 保留策略 — 问题三十八 (Observability Storage Contract) 的清理规则。
 *
 * 核心规则:
 * - evidence 必须可追溯且有 retention/cleanup 规则
 * - cleanup 不得删除仍被 TaskContext / GovernanceReport 引用的 evidence
 * - cleanup 默认 dry-run
 * - 日志/审计/metric 不得存 secret、完整 prompt、大段 stdout/stderr
 */
const BUILTIN_RETENTION_POLICIES = [
    {
        policy_id: "retention-evidence",
        record_type: "evidence",
        max_age_days: 90,
        max_count: 1000,
        dry_run_default: true,
        protected_while_referenced: true,
        redact_before_storage: true,
        forbidden_content: ["secret", "api_key", "password", "token"],
    },
    {
        policy_id: "retention-audit",
        record_type: "audit_event",
        max_age_days: 180,
        max_count: 5000,
        dry_run_default: true,
        protected_while_referenced: false,
        redact_before_storage: true,
        forbidden_content: ["secret", "raw_stdout", "raw_stderr"],
    },
    {
        policy_id: "retention-task-context",
        record_type: "task_context",
        max_age_days: 30,
        max_count: 100,
        dry_run_default: true,
        protected_while_referenced: true,
        redact_before_storage: true,
        forbidden_content: ["raw_prompt", "chain_of_thought", "secret", "raw_stdout", "raw_stderr"],
    },
    {
        policy_id: "retention-metric",
        record_type: "metric_sample",
        max_age_days: 60,
        max_count: 10000,
        dry_run_default: true,
        protected_while_referenced: false,
        redact_before_storage: true,
        forbidden_content: ["user_data_raw"],
    },
    {
        policy_id: "retention-user-summary",
        record_type: "user_summary",
        max_age_days: 30,
        max_count: 500,
        dry_run_default: true,
        protected_while_referenced: false,
        redact_before_storage: false,
        forbidden_content: ["secret"],
    },
];
/** 列出所有保留策略 */
export function listRetentionPolicies() {
    return [...BUILTIN_RETENTION_POLICIES];
}
/** 按记录类型获取策略 */
export function getRetentionPolicy(recordType) {
    return BUILTIN_RETENTION_POLICIES.find((p) => p.record_type === recordType);
}
/**
 * 检查记录是否包含禁止内容。
 */
export function checkForbiddenContent(recordType, content) {
    const policy = getRetentionPolicy(recordType);
    if (!policy)
        return [];
    const found = [];
    for (const forbidden of policy.forbidden_content) {
        if (content.toLowerCase().includes(forbidden.toLowerCase())) {
            found.push(forbidden);
        }
    }
    return found;
}
/**
 * 评估清理候选。
 * 默认 dry-run，不执行实际删除。
 */
export function evaluateCleanup(recordType, records, dryRun = true) {
    const policy = getRetentionPolicy(recordType);
    if (!policy) {
        return {
            policy_id: "unknown",
            total_records: records.length,
            eligible_for_cleanup: 0,
            protected_records: 0,
            dry_run: true,
            cleanup_candidates: [],
        };
    }
    let eligible = 0;
    let protected_count = 0;
    const candidates = [];
    for (const record of records) {
        if (record.age_days > policy.max_age_days) {
            if (policy.protected_while_referenced && record.is_referenced) {
                protected_count++;
            }
            else {
                eligible++;
                candidates.push(record.id);
            }
        }
    }
    return {
        policy_id: policy.policy_id,
        total_records: records.length,
        eligible_for_cleanup: eligible,
        protected_records: protected_count,
        dry_run: dryRun || policy.dry_run_default,
        cleanup_candidates: candidates,
    };
}
//# sourceMappingURL=retention_policy.js.map