From d8d1f3dee8dcb3c4cb625aaa3ba7c120d20343c5 Mon Sep 17 00:00:00 2001 From: ogt Date: Mon, 20 Apr 2026 05:21:17 +0800 Subject: [PATCH] fix: create ADR-012 agent tables migration + fix telegram_models import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migration 017: - CREATE TABLE IF NOT EXISTS agent_context, action_plans, action_outcomes, agent_strategy_weights (all four ADR-012 tables were missing from production DB) - These tables are required by ElephantAlpha AutonomousEngine coordination loop telegram_templates.py: - Fix: from database.telegram_models → database.trend_models (TelegramUser has always lived in trend_models; telegram_models module does not exist) Co-Authored-By: Claude Sonnet 4.6 --- migrations/017_adr012_agent_tables.sql | 64 ++++++++++++++++++++++++++ services/telegram_templates.py | 2 +- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 migrations/017_adr012_agent_tables.sql diff --git a/migrations/017_adr012_agent_tables.sql b/migrations/017_adr012_agent_tables.sql new file mode 100644 index 0000000..d67eb30 --- /dev/null +++ b/migrations/017_adr012_agent_tables.sql @@ -0,0 +1,64 @@ +-- Migration 017: Create ADR-012 Agent Action Ladder tables +-- Tables: agent_context, action_plans, action_outcomes, agent_strategy_weights +-- These are the core multi-agent shared-context and closed-loop learning tables. +-- Author: Antigravity / ADR-012 + +-- 1. Agent shared context (TTL-aware, cross-agent access) +CREATE TABLE IF NOT EXISTS agent_context ( + id SERIAL PRIMARY KEY, + session_id VARCHAR(64) NOT NULL, + agent_name VARCHAR(50) NOT NULL, + context_key VARCHAR(100) NOT NULL, + context_val TEXT, + created_at TIMESTAMP DEFAULT NOW(), + ttl_minutes INTEGER DEFAULT 60 +); +CREATE INDEX IF NOT EXISTS idx_agent_context_session_key + ON agent_context (session_id, agent_name, context_key); +CREATE INDEX IF NOT EXISTS idx_agent_context_session_ttl + ON agent_context (session_id, created_at); + +-- 2. Action plans (NemoTron output, pending review & execution tracking) +CREATE TABLE IF NOT EXISTS action_plans ( + id SERIAL PRIMARY KEY, + session_id VARCHAR(64), + plan_type VARCHAR(50), -- price_adjust / restock / campaign + sku VARCHAR(100), + payload TEXT, -- JSON action payload + status VARCHAR(20) DEFAULT 'pending', -- pending/approved/rejected/executed + created_by VARCHAR(50), -- nemotron / openclaw + approved_by VARCHAR(100), -- Telegram user_id + created_at TIMESTAMP DEFAULT NOW(), + executed_at TIMESTAMP +); +CREATE INDEX IF NOT EXISTS idx_action_plan_sku_status ON action_plans (sku, status); +CREATE INDEX IF NOT EXISTS idx_action_plan_created ON action_plans (created_at); + +-- 3. Action outcomes (closed-loop learning core) +CREATE TABLE IF NOT EXISTS action_outcomes ( + id SERIAL PRIMARY KEY, + plan_id INTEGER NOT NULL REFERENCES action_plans(id), + metric_type VARCHAR(50), -- sales_7d / price_rank / conversion + before_val FLOAT, + after_val FLOAT, + measured_at TIMESTAMP, + verdict VARCHAR(20), -- effective / neutral / backfired + created_at TIMESTAMP DEFAULT NOW() +); +CREATE INDEX IF NOT EXISTS idx_action_outcome_plan ON action_outcomes (plan_id); + +-- 4. Agent strategy weights (OpenClaw learning accumulation) +CREATE TABLE IF NOT EXISTS agent_strategy_weights ( + id SERIAL PRIMARY KEY, + strategy_key VARCHAR(100) UNIQUE NOT NULL, -- e.g. price_cut_when_gap_gt_5pct + weight FLOAT DEFAULT 1.0, + success_cnt INTEGER DEFAULT 0, + fail_cnt INTEGER DEFAULT 0, + updated_at TIMESTAMP DEFAULT NOW() +); +CREATE INDEX IF NOT EXISTS idx_strategy_key ON agent_strategy_weights (strategy_key); + +DO $$ +BEGIN + RAISE NOTICE 'Migration 017 done: agent_context / action_plans / action_outcomes / agent_strategy_weights'; +END $$; diff --git a/services/telegram_templates.py b/services/telegram_templates.py index 440b17f..e4ca3b4 100644 --- a/services/telegram_templates.py +++ b/services/telegram_templates.py @@ -3,7 +3,7 @@ import logging from typing import Any, Dict, Optional from database.manager import get_session -from database.telegram_models import TelegramUser +from database.trend_models import TelegramUser sys_log = logging.getLogger("TelegramTpl")