{
  "schema_version": "ai_agent_live_read_model_gate_v1",
  "generated_at": "2026-06-11T23:50:00+08:00",
  "program_status": {
    "overall_completion_percent": 55,
    "current_priority": "P2",
    "current_task_id": "P2-403B",
    "next_task_id": "P2-403C",
    "read_only_mode": true,
    "runtime_authority": "gate_plan_only_no_live_worker",
    "status_note": "P2-403B 已把 AgentSession / Redis Streams live read model 的唯讀查詢、worker gate、回滾與無寫入 smoke 變成可審查契約；尚未開啟 live DB query、Redis consumer group、runtime worker、learning writeback 或 Telegram 發送。"
  },
  "source_refs": [
    "apps/api/src/db/models.py:AgentSession",
    "apps/api/migrations/phase_aiops_p1_p2_p6_tables.sql:agent_sessions",
    "docs/evaluations/ai_agent_interaction_learning_proof_2026-06-11.json",
    "docs/superpowers/specs/2026-04-15-MASTER-ai-autonomous-flywheel-v2.md"
  ],
  "live_truth": {
    "live_agent_session_readback_enabled": false,
    "live_redis_stream_read_enabled": false,
    "runtime_worker_enabled": false,
    "telegram_receipt_send_enabled": false,
    "learning_writeback_enabled": false,
    "active_live_agent_sessions": 0,
    "live_redis_events_24h": 0,
    "live_handoffs_24h": 0,
    "live_learning_writes_24h": 0,
    "telegram_digest_receipts_24h": 0,
    "truth_note": "目前完成的是 read model gate plan；治理頁可以看見下一步要驗證什麼，但不能把 0 筆 live session 說成 Agent 已在 production 主動互動。"
  },
  "existing_storage_contract": {
    "db_table": "agent_sessions",
    "schema_status": "existing_phase_aiops_table_detected",
    "migration_delta_required": false,
    "approved_for_live_query": false,
    "safe_read_query_defined": true,
    "safe_selected_fields": [
      "id",
      "session_id",
      "incident_id",
      "agent_role",
      "input_hash",
      "latency_ms",
      "vote",
      "degraded",
      "created_at"
    ],
    "forbidden_selected_fields": [
      "output_json",
      "prompt",
      "raw_prompt",
      "conversation_transcript",
      "private_reasoning",
      "chain_of_thought",
      "secret_plaintext",
      "credential_value"
    ],
    "required_indexes": [
      "ix_agent_sessions_session_id",
      "ix_agent_sessions_incident_id",
      "ix_agent_sessions_created_at",
      "ix_agent_sessions_session_role"
    ],
    "read_query_contract": "SELECT id, session_id, incident_id, agent_role, input_hash, latency_ms, vote, degraded, created_at FROM agent_sessions WHERE created_at >= :window_start ORDER BY created_at DESC LIMIT :safe_limit",
    "query_limits": {
      "default_window_hours": 24,
      "max_limit": 50,
      "order_by": "created_at DESC"
    }
  },
  "redis_stream_contract": {
    "stream_namespace": "awoooi:agent:*",
    "candidate_streams": [
      "awoooi:agent:events",
      "awoooi:agent:handoffs",
      "awoooi:agent:learning"
    ],
    "consumer_group_allowed": false,
    "xadd_allowed": false,
    "xreadgroup_allowed": false,
    "dead_letter_required": true,
    "replay_required_before_worker": true,
    "event_envelope_required_fields": [
      "event_id",
      "trace_id",
      "session_id",
      "incident_id",
      "agent_id",
      "event_type",
      "event_status",
      "created_at",
      "evidence_ref"
    ],
    "forbidden_event_fields": [
      "secret_value",
      "credential_value",
      "raw_prompt",
      "conversation_transcript",
      "private_reasoning",
      "chain_of_thought"
    ]
  },
  "read_model_cards": [
    {
      "card_id": "agent_heartbeat_readback",
      "display_name": "Agent 心跳讀回",
      "owner_agent": "openclaw",
      "source_of_truth": "agent_sessions.created_at",
      "readiness_status": "query_contract_ready",
      "operator_signal": "看到各 Agent 最近一次安全欄位 readback 時間與 degraded 狀態。",
      "next_gate": "P2-403C Redis dry-run 前先做 DB 無寫入 smoke。"
    },
    {
      "card_id": "agent_vote_quality",
      "display_name": "投票與降級品質",
      "owner_agent": "openclaw",
      "source_of_truth": "agent_sessions.vote + degraded + latency_ms",
      "readiness_status": "query_contract_ready",
      "operator_signal": "看到 approve / reject / request_revision / degraded 的聚合趨勢。",
      "next_gate": "P2-403C 加上 message receipt 後才能判斷互相接手。"
    },
    {
      "card_id": "agent_role_turns",
      "display_name": "Agent role turn 摘要",
      "owner_agent": "hermes",
      "source_of_truth": "agent_sessions.session_id + agent_role",
      "readiness_status": "query_contract_ready",
      "operator_signal": "看到同一 session 內哪些角色參與，但不顯示未脫敏執行細節。",
      "next_gate": "P2-403C 定義 handoff envelope。"
    },
    {
      "card_id": "redis_message_receipts",
      "display_name": "Redis 訊息收據",
      "owner_agent": "hermes",
      "source_of_truth": "Redis Streams event envelope",
      "readiness_status": "approval_required",
      "operator_signal": "看到 XADD / ack / dead-letter 的脫敏收據；目前尚未允許讀寫 Redis。",
      "next_gate": "P2-403C consumer group dry-run approval。"
    },
    {
      "card_id": "learning_receipt_bridge",
      "display_name": "學習收據橋接",
      "owner_agent": "nemotron",
      "source_of_truth": "knowledge_entries + playbook_trust_history + replay_results",
      "readiness_status": "blocked_by_writeback_gate",
      "operator_signal": "看到學習回寫與 replay 分數改善的聚合摘要；目前 writeback 未批准。",
      "next_gate": "P2-403D learning writeback approval package。"
    }
  ],
  "worker_gate_plan": [
    {
      "gate_id": "db_read_model_no_write_smoke",
      "display_name": "DB read model 無寫入 smoke",
      "owner_agent": "openclaw",
      "status": "approval_required",
      "required_evidence": "pytest loader + API endpoint + SQL read-only query review；確認未 import DB session writer、未 INSERT / UPDATE / DELETE。",
      "blocked_action": "live_db_query"
    },
    {
      "gate_id": "redis_stream_replay_dry_run",
      "display_name": "Redis Streams replay dry-run",
      "owner_agent": "hermes",
      "status": "approval_required",
      "required_evidence": "固定 fixture 重放 event envelope，確認 ack / dead-letter / replay idempotency。",
      "blocked_action": "redis_consumer_group"
    },
    {
      "gate_id": "worker_kill_switch",
      "display_name": "worker 熔斷與停用開關",
      "owner_agent": "openclaw",
      "status": "blocked",
      "required_evidence": "環境變數預設關閉、runtime health gate、回滾腳本與告警降噪規則。",
      "blocked_action": "runtime_worker"
    },
    {
      "gate_id": "frontend_redaction_readback",
      "display_name": "前端 readback 脫敏驗證",
      "owner_agent": "hermes",
      "status": "approval_required",
      "required_evidence": "只顯示 safe fields、聚合數、event id、來源與 gate；未核准上下文與未脫敏執行細節不得進前端。",
      "blocked_action": "unsafe_frontend_display"
    },
    {
      "gate_id": "telegram_receipt_dry_run_gate",
      "display_name": "Telegram receipt dry-run gate",
      "owner_agent": "openclaw",
      "status": "blocked",
      "required_evidence": "Gateway dry-run、token 注入 E2E、failure-only / action-required policy、送達收據與 fallback。",
      "blocked_action": "telegram_send"
    }
  ],
  "rollback_plan": [
    {
      "rollback_id": "disable_feature_flag",
      "step": "若 read model API 發生異常，先關閉前端顯示與 runtime feature flag，保留 P2-403A 證據面。",
      "owner_agent": "openclaw"
    },
    {
      "rollback_id": "stop_worker_before_queue",
      "step": "若 P2-403C 後 worker 開始 dry-run，先停 worker，再檢查 Redis pending / dead-letter，不刪資料。",
      "owner_agent": "hermes"
    },
    {
      "rollback_id": "freeze_learning_writeback",
      "step": "若 learning bridge 發現髒資料，維持 writeback gate 關閉，只保留候選學習報告。",
      "owner_agent": "nemotron"
    },
    {
      "rollback_id": "telegram_send_lock",
      "step": "若 Telegram gateway 有錯誤，保留 queue / receipt evidence，但直接發送維持關閉。",
      "owner_agent": "openclaw"
    }
  ],
  "no_write_smoke_plan": [
    {
      "smoke_id": "loader_rejects_live_flags",
      "status": "defined",
      "writes_allowed": false,
      "assertion": "loader 會拒絕任何 live DB query、runtime worker、Redis consumer、Telegram 或 learning writeback allowed=true。"
    },
    {
      "smoke_id": "safe_field_projection",
      "status": "defined",
      "writes_allowed": false,
      "assertion": "safe_selected_fields 不包含 output_json、prompt、逐字稿、未核准推理或機密欄位。"
    },
    {
      "smoke_id": "api_returns_gate_only",
      "status": "defined",
      "writes_allowed": false,
      "assertion": "API 只回傳 committed snapshot，不連 DB、不連 Redis、不送 Telegram。"
    },
    {
      "smoke_id": "frontend_redaction_scan",
      "status": "defined",
      "writes_allowed": false,
      "assertion": "前端只顯示狀態、safe fields 數量、gate、聚合指標與下一步。"
    },
    {
      "smoke_id": "rollback_plan_present",
      "status": "defined",
      "writes_allowed": false,
      "assertion": "每個 future runtime gate 都有停用或凍結策略。"
    }
  ],
  "display_redaction_contract": {
    "redaction_required": true,
    "work_window_conversation_display_allowed": false,
    "agent_raw_output_display_allowed": false,
    "secret_value_display_allowed": false,
    "allowed_frontend_content": [
      "狀態",
      "聚合數",
      "safe field 名稱",
      "來源表",
      "event id",
      "gate id",
      "下一步",
      "回滾步驟摘要"
    ],
    "forbidden_frontend_content": [
      "未公開上下文",
      "未脫敏執行細節",
      "提示內容",
      "未核准推理",
      "機密值",
      "未脫敏 payload"
    ],
    "frontend_display_policy": "治理頁只顯示 gate、safe fields、聚合數、來源與下一步；未公開上下文、未脫敏執行細節、提示內容、機密值與未脫敏 payload 一律不進前端。"
  },
  "approval_boundaries": {
    "db_migration_allowed": false,
    "live_db_query_allowed": false,
    "redis_xadd_allowed": false,
    "redis_consumer_group_allowed": false,
    "runtime_worker_allowed": false,
    "telegram_direct_send_allowed": false,
    "learning_writeback_allowed": false,
    "secret_plaintext_allowed": false,
    "conversation_transcript_display_allowed": false,
    "private_reasoning_display_allowed": false,
    "agent_raw_output_display_allowed": false
  },
  "rollups": {
    "source_ref_count": 4,
    "read_model_card_count": 5,
    "gate_count": 5,
    "approval_required_gate_ids": [
      "db_read_model_no_write_smoke",
      "frontend_redaction_readback",
      "redis_stream_replay_dry_run",
      "telegram_receipt_dry_run_gate",
      "worker_kill_switch"
    ],
    "query_contract_ready_card_ids": [
      "agent_heartbeat_readback",
      "agent_role_turns",
      "agent_vote_quality"
    ],
    "rollback_step_count": 4,
    "no_write_smoke_count": 5,
    "forbidden_frontend_content_count": 6,
    "live_truth_count_total": 0
  }
}
