Files
group_fqcd_jr/tools/probe_memory_state.py
T
lzf_0626 c0e5c80929 记忆系统:recall 结果接入 prompt + 可观测性(既有改动,代为提交)
## 说明

**这批改动不是本次会话写的**,它们在会话开始前就已在工作区里、一直未提交。
我做的是**验证**它确实成立,然后按你的指示代为提交。

出处:`docs/演示用/记忆系统排查报告-2026-09-14.md` 与同目录
`记忆系统修复文档-2026-09-14.md`(两份都在本次一并入库)。
排查报告的结论是「记忆系统没有坏」——库里有真实数据、170 条抽取事件全部消费成功;
真正的问题是「观测不到」+「召回结果没人消费」。

## 改动内容(按两份文档的编号)

- **F1 `RecalledMemory.content` 断头路**:`base.py` 新增 `memory_context_text()`,
  `risk_agent._agent_system_prompt` 接收并注入记忆段。无记忆时返回空串,
  因此 prompt 逐字不变 —— 这也是它能安全接线的理由。
- **F3 `governance.recall` 员工身份恒空**:补一条明确的语义日志。
  员工身份下召回的是"该用户自身作为客户"的记忆,恒为空属预期,
  但此前没有任何提示,运维看到 `count=0` 只会以为记忆坏了。
- **F4 可观测性**:`GET /api/v1/users/me/memories`(`stored` / `recalled` /
  `downstream` / `pending_events` 四段)+ 抽取与召回的 6 处日志 +
  三个只读探针 `tools/probe_memory_state.py`、`probe_memory_detail.py`、
  `probe_agent_types.py`。

**未实施**(文档明确留作待决,我也不代为决定):F2 `known` 引用校验永不触发
(需架构确认 memory 类 `source_references` 由业务填还是底座统一附加)、
F5 客服是否读写长期记忆(涉脱敏与复核,需产品+合规)。

## 我做的验证(会话内实测,非照录文档)

- 新接口 `GET /users/me/memories` 以 `cust_t` 调用 -> **HTTP 200**:

      stored:    total=2, by_status={'active': 2}
      recalled:  count=2, degraded=False
      两条记忆:preference:horizon='约三年'(0.95)、preference:risk_level='稳健型'(0.98)

  与排查报告 §〇 列出的那两条**完全吻合**。
- `pytest tests/unit tests/contract` 全绿(这批改动没有破坏既有测试)。

## 未验证的部分

`memory_context_text()` 接进 prompt 后的**端到端效果没有实测** —— 文档自己说明了
原因:当前 `risk` Agent 的召回恒空(员工身份不是客户),所以接线后行为不变,
要用测试替身才能验证注入。我没有为此编造证据。
2026-09-14 20:35:46 +08:00

122 lines
4.5 KiB
Python

"""记忆系统状态探针(只读):一次看清「库里有没有」「事件消费到哪了」。
用途:排查"记忆系统是否真的在工作"时,先跑这个脚本拿到事实基线,
再去看日志定位是哪一段断了。全部是 SELECT,不写任何数据。
用法:
D:/conda/envs/jr_py313/python.exe tools/probe_memory_state.py [customer_id]
"""
from __future__ import annotations
import asyncio
import sys
from typing import Any
from sqlalchemy import text
from app.infrastructure.db import SessionFactory
TABLES = (
"memory_unit",
"memory_evidence",
"memory_conflict",
"memory_sync_outbox",
"user_facts",
"profile_snapshots",
)
async def _scalar(session: Any, sql: str, **params: Any) -> Any:
try:
return await session.scalar(text(sql), params)
except Exception as exc: # 表不存在/连不上库都要能看,而不是整个脚本崩掉
return f"ERROR {type(exc).__name__}: {exc}"
async def main() -> None:
customer_id = int(sys.argv[1]) if len(sys.argv) > 1 else None
async with SessionFactory() as session:
print("=" * 60)
print("1) 记忆相关表的总行数")
print("=" * 60)
for table in TABLES:
print(f" {table:<22} {await _scalar(session, f'SELECT COUNT(*) FROM {table}')}")
print()
print("=" * 60)
print("2) memory_unit 按状态分组")
print("=" * 60)
try:
rows = (await session.execute(
text("SELECT status, COUNT(*) c FROM memory_unit GROUP BY status"))).all()
for row in rows:
print(f" {row[0]:<12} {row[1]}")
except Exception as exc:
print(f" ERROR {type(exc).__name__}: {exc}")
print()
print("=" * 60)
print("3) domain_event_outbox 按 (event_type, status) 分组")
print(" —— 这里能看到事件是不是堆在 pending(= Worker 没跑 / 处理失败)")
print("=" * 60)
try:
rows = (await session.execute(text(
"SELECT event_type, status, COUNT(*) c, MAX(occurred_at) latest "
"FROM domain_event_outbox GROUP BY event_type, status ORDER BY c DESC"
))).all()
if not rows:
print(" (空)")
for row in rows:
print(f" {row[0]:<38} {row[1]:<10} {row[2]:<6} latest={row[3]}")
except Exception as exc:
print(f" ERROR {type(exc).__name__}: {exc}")
if customer_id is not None:
print()
print("=" * 60)
print(f"4) 客户 {customer_id} 的记忆明细")
print("=" * 60)
try:
rows = (await session.execute(text(
"SELECT memory_uuid, memory_key, content, memory_type, status, "
"confidence, source_type, updated_at "
"FROM memory_unit WHERE customer_id=:cid ORDER BY updated_at DESC LIMIT 20"
), {"cid": customer_id})).all()
if not rows:
print(" (该客户没有任何 memory_unit 行)")
for row in rows:
print(f" [{row[4]}] {row[1]} = {row[2]!r}")
print(f" type={row[3]} conf={row[5]} src={row[6]} at={row[7]}")
except Exception as exc:
print(f" ERROR {type(exc).__name__}: {exc}")
print()
print(f" user_facts:")
try:
rows = (await session.execute(text(
"SELECT fact_key, confidence FROM user_facts "
"WHERE customer_id=:cid LIMIT 20"), {"cid": customer_id})).all()
for row in rows:
print(f" {row[0]} (conf={row[1]})")
if not rows:
print(" (无)")
except Exception as exc:
print(f" ERROR {type(exc).__name__}: {exc}")
print(f" profile_snapshots:")
try:
rows = (await session.execute(text(
"SELECT version, is_current, generated_at FROM profile_snapshots "
"WHERE customer_id=:cid ORDER BY id DESC LIMIT 5"), {"cid": customer_id})).all()
for row in rows:
print(f" v{row[0]} current={row[1]} at={row[2]}")
if not rows:
print(" (无)")
except Exception as exc:
print(f" ERROR {type(exc).__name__}: {exc}")
if __name__ == "__main__":
asyncio.run(main())