SuperAPI AI 工具集

causal-inference

智能体与模型

查看项目

更新于 10-16 05:57

技能介绍

用途

Causal Inference 是一个轻量级因果层,用于预测动作的结果。它不是通过模式匹配相关性,而是通过建模干预和反事实来进行推断。

核心不变式:每个动作都必须能够表示为对因果模型的显式干预,并带有预测效果、不确定性以及可证伪的审计轨迹。计划必须在因果上有效,而不仅仅是看似合理。

何时触发

在任何高层动作上触发此技能,包括但不限于以下领域:

  • 沟通:发送邮件、发送消息、回复、跟进、通知、提及
  • 日历:创建/移动/取消会议、设置提醒、RSVP
  • 任务:创建/完成任务、推迟任务、设置优先级、分配
  • 文件:创建/编辑/共享文档、提交代码、部署
  • 社交:发帖、反应、评论、分享、私信
  • 购买:下单、订阅、取消、退款
  • 系统:配置变更、权限授予、集成设置

同样在以下情况触发:

  • 复盘结果——例如“那封邮件收到回复了吗?”→ 记录结果,更新估计
  • 调试失败——例如“为什么没有生效?”→ 追踪因果图
  • 回填历史——例如“分析我过去的邮件/日历”→ 解析日志,重建动作
  • 规划——例如“我应该现在发送还是稍后发送?”→ 查询因果模型

回填:从历史数据引导启动

不要从零开始。解析现有日志以重建过去的动作与结果。

邮件回填:

# Extract sent emails with reply status
gog gmail list --sent --after 2024-01-01 --format json > /tmp/sent_emails.json

# For each sent email, check if reply exists
python3 scripts/backfill_email.py /tmp/sent_emails.json

日历回填:

# Extract past events with attendance
gog calendar list --after 2024-01-01 --format json > /tmp/events.json

# Reconstruct: did meeting happen? was it moved? attendee count?
python3 scripts/backfill_calendar.py /tmp/events.json

消息回填(WhatsApp/Discord/Slack):

# Parse message history for send/reply patterns
wacli search --after 2024-01-01 --from me --format json > /tmp/wa_sent.json
python3 scripts/backfill_messages.py /tmp/wa_sent.json

通用回填模式:

# For any historical data source:
for record in historical_data:
    action_event = {
        "action": infer_action_type(record),
        "context": extract_context(record),
        "time": record["timestamp"],
        "pre_state": reconstruct_pre_state(record),
        "post_state": extract_post_state(record),
        "outcome": determine_outcome(record),
        "backfilled": True  # Mark as reconstructed
    }
    append_to_log(action_event)

架构

A. 动作日志(必需)

每个已执行动作都会发出结构化事件:

{
  "action": "send_followup",
  "domain": "email",
  "context": {"recipient_type": "warm_lead", "prior_touches": 2},
  "time": "2025-01-26T10:00:00Z",
  "pre_state": {"days_since_last_contact": 7},
  "post_state": {"reply_received": true, "reply_delay_hours": 4},
  "outcome": "positive_reply",
  "outcome_observed_at": "2025-01-26T14:00:00Z",
  "backfilled": false
}

存储在 memory/causal/action_log.jsonl。

B. 因果图(按领域)

每个领域从 10-30 个可观测变量开始。

邮件领域:

  • send_time → reply_prob
  • subject_style → open_rate
  • recipient_type → reply_prob
  • followup_count → reply_prob(递减)
  • time_since_last → reply_prob

日历领域:

  • meeting_time → attendance_rate
  • attendee_count → slip_risk
  • conflict_degree → reschedule_prob
  • buffer_time → focus_quality

消息领域:

  • response_delay → conversation_continuation
  • message_length → response_length
  • time_of_day → response_prob
  • platform → response_delay

任务领域:

  • due_date_proximity → completion_prob
  • priority_level → completion_speed
  • task_size → deferral_risk
  • context_switches → error_rate

将图定义存储在 memory/causal/graphs/。

C. 估计

对每个“旋钮”(干预变量),估计处理效应:

# Pseudo: effect of morning vs evening sends
effect = mean(reply_prob | send_time=morning) - mean(reply_prob | send_time=evening)
uncertainty = std_error(effect)

先使用简单回归或倾向匹配。当图明确且需要识别时,再升级到 do-calculus。

D. 决策策略

在执行动作之前:

  1. 识别干预变量
  2. 查询因果模型以获取预期结果分布
  3. 计算预期效用 + 不确定性边界
  4. 如果不确定性 > 阈值 或 预期危害 > 阈值 → 拒绝或升级给用户
  5. 记录预测以供后续验证

工作流

每次动作时:

执行前:

  1. 记录 pre_state
  2. 如果有足够历史数据:查询模型以获取预期结果
  3. 如果高不确定性或高风险:与用户确认

执行后:

  1. 记录 action + context + time
  2. 设置提醒以检查结果(如果非即时)

观察到结果时:

  1. 更新动作日志,加入 post_state + outcome
  2. 如果有足够新数据,重新估计处理效应

规划动作:

  1. 用户请求 → 识别候选动作
  2. 对每个动作: a. 映射到因果图上的干预 b. 预测 P(outcome | do(action)) c. 估计不确定性 d. 计算预期效用
  3. 按预期效用排序,按安全性过滤
  4. 执行最佳动作,记录预测
  5. 观察结果,更新模型

调试失败:

  1. 识别失败结果
  2. 回溯因果图
  3. 对每个上游节点: a. 值是否符合预期? b. 因果链接是否成立?
  4. 识别断裂的链接
  5. 计算本可防止失败的最小干预集
  6. 记录反事实以供学习

快速开始:今天就能引导启动

# 1. Create the infrastructure
mkdir -p memory/causal/graphs memory/causal/estimates

# 2. Initialize config
cat > memory/causal/config.yaml << 'EOF'
domains:
  - email
  - calendar
  - messaging
  - tasks

thresholds:
  max_uncertainty: 0.3
  min_expected_utility: 0.1

protected_actions:
  - delete_email
  - cancel_meeting
  - send_to_new_contact
  - financial_transaction
EOF

# 3. Backfill one domain (start with email)
python3 scripts/backfill_email.py

# 4. Estimate initial effects
python3 scripts/estimate_effect.py --treatment send_time --outcome reply_received --values morning,evening

安全约束

定义需要显式用户批准的“受保护变量”:

protected:
  - delete_email
  - cancel_meeting
  - send_to_new_contact
  - financial_transaction

thresholds:
  max_uncertainty: 0.3  # don't act if P(outcome) uncertainty > 30%
  min_expected_utility: 0.1  # don't act if expected gain < 10%

文件

  • memory/causal/action_log.jsonl — 所有记录的动作及其结果
  • memory/causal/graphs/ — 按领域的因果图定义
  • memory/causal/estimates/ — 学习到的处理效应
  • memory/causal/config.yaml — 安全阈值与受保护变量

参考

  • 关于形式化干预语义,见 references/do-calculus.md
  • 关于处理效应估计方法,见 references/estimation.md