Files
group_fqcd_jr/tools/probe_memory_detail.py
T

102 lines
3.8 KiB
Python
Raw Normal View History

"""记忆系统明细探针(只读):回答「写入了什么 / 今天有没有活动 / 为什么 dead」。"""
from __future__ import annotations
import asyncio
from typing import Any
from sqlalchemy import text
from app.infrastructure.db import SessionFactory
async def q(session: Any, sql: str, **params: Any) -> list[Any]:
try:
return list((await session.execute(text(sql), params)).all())
except Exception as exc:
print(f" ERROR {type(exc).__name__}: {exc}")
return []
async def main() -> None:
async with s_factory() as session:
print("=" * 66)
print("A) memory_unit 全部内容(写入是否成功的直接证据)")
print("=" * 66)
rows = await q(session, """
SELECT id, customer_id, memory_key, content, memory_type, status,
confidence, source_type, evidence_count, created_at, updated_at
FROM memory_unit ORDER BY updated_at DESC LIMIT 20
""")
if not rows:
print(" (memory_unit 为空 —— 一条记忆都没写入)")
for r in rows:
print(f" id={r[0]} customer={r[1]} [{r[5]}]")
print(f" key = {r[2]}")
print(f" value = {r[3]!r}")
print(f" type={r[4]} conf={r[6]} src={r[7]} evidence={r[8]}")
print(f" created={r[9]} updated={r[10]}")
print()
print("=" * 66)
print("B) 今天(2026-09-14)有没有任何新活动")
print("=" * 66)
for label, sql in (
("agent_run 今天创建", "SELECT COUNT(*) FROM agent_run WHERE created_at >= '2026-09-14'"),
("agent_run 最新一条", "SELECT MAX(created_at) FROM agent_run"),
("conversation_message 最新", "SELECT MAX(created_at) FROM conversation_message"),
("domain_event_outbox 最新", "SELECT MAX(created_at) FROM domain_event_outbox"),
("outbox pending 总数", "SELECT COUNT(*) FROM domain_event_outbox WHERE status='pending'"),
("outbox failed 总数", "SELECT COUNT(*) FROM domain_event_outbox WHERE status='failed'"),
):
rows = await q(session, sql)
print(f" {label:<26} {rows[0][0] if rows else '?'}")
print()
print("=" * 66)
print("C) 最近 10 条 outbox 事件(看最新动向)")
print("=" * 66)
rows = await q(session, """
SELECT event_type, status, retry_count, last_error, occurred_at
FROM domain_event_outbox ORDER BY id DESC LIMIT 10
""")
for r in rows:
err = (str(r[3])[:70] + "...") if r[3] and len(str(r[3])) > 70 else r[3]
print(f" {r[4]} | {r[0]:<34} | {r[1]:<9} | retry={r[2]}")
if err:
print(f" last_error: {err}")
print()
print("=" * 66)
print("D) agent.run_requested 死信的原因分布(取 5 条样本)")
print("=" * 66)
rows = await q(session, """
SELECT retry_count, last_error, occurred_at
FROM domain_event_outbox
WHERE event_type='agent.run_requested' AND status='dead'
ORDER BY id DESC LIMIT 5
""")
for r in rows:
err = (str(r[1])[:150] + "...") if r[1] and len(str(r[1])) > 150 else r[1]
print(f" {r[2]} retry={r[0]}")
print(f" {err}")
print()
print("=" * 66)
print("E) 最近 5 次 agent_run 的状态")
print("=" * 66)
rows = await q(session, """
SELECT run_id, agent_type, status, error_code, created_at
FROM agent_run ORDER BY id DESC LIMIT 5
""")
for r in rows:
print(f" {r[4]} | {r[1]:<18} | {r[2]:<10} | err={r[3]} | {r[0][:8]}")
def s_factory():
return SessionFactory()
if __name__ == "__main__":
asyncio.run(main())