import sqlite3, json, uuid, datetime

db = sqlite3.connect("/opt/friend-cluster/data/friend-cluster-v1.db")
db.row_factory = sqlite3.Row

row = db.execute("SELECT * FROM config_snapshots WHERE id='snap-166ad3e9-7e6'").fetchone()
if not row:
    print("SOURCE NOT FOUND")
    raise SystemExit(1)

d = dict(row)

# Parse resources_json, remove feishu plugin config entries
resources = json.loads(d["resources_json"])
original_count = len(resources)
cleaned = [r for r in resources if not (r.get("resource_key") == "feishu" or r.get("kind") == "plugin.config" and r.get("resource_key") == "feishu")]
print(f"resources: {original_count} -> {len(cleaned)} (removed {original_count - len(cleaned)} feishu entries)")

# Also check for wechat plugin config that might cause same issue
wechat_before = len(cleaned)
cleaned = [r for r in cleaned if not (r.get("resource_key") == "wechat" and r.get("kind") == "plugin.config")]
if len(cleaned) < wechat_before:
    print(f"also removed {wechat_before - len(cleaned)} wechat entries")

new_id = "snap-" + uuid.uuid4().hex[:12]
now = datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z")

db.execute(
    "INSERT INTO config_snapshots (id, name, description, source_instance_id, friend_version, capture_mode, resources_json, extensions_json, warnings_json, created_by, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
    (
        new_id,
        "yunwo-baseline-v3-clean",
        "Clean snapshot: friend binary with skills.delete + brain.llm.stream, no feishu/wechat config",
        d["source_instance_id"],
        d["friend_version"],
        "manual",
        json.dumps(cleaned, ensure_ascii=False),
        d["extensions_json"],
        "[]",
        "static-token:admin",
        now,
    ),
)
db.commit()
print(f"NEW SNAPSHOT: {new_id}")

# Verify
row2 = db.execute("SELECT id, name FROM config_snapshots WHERE id=?", (new_id,)).fetchone()
print("verified:", dict(row2))
