fix(postgresql): register asyncpg JSON codecs and fix tool call recording on PG backend#2327
fix(postgresql): register asyncpg JSON codecs and fix tool call recording on PG backend#2327MIKAZE3 wants to merge 3 commits into
Conversation
dadachann
left a comment
There was a problem hiding this comment.
这个 PR 还没拆干净:wecombot.py 不属于 PostgreSQL 修复,而且调用的三个辅助方法在本 PR 里没有实现,单独合入会直接报错。monitoring.py 的类型判断也只是返回 None,没有正确取到消息对象。请把 WeCom 改动移出去,PG codec 和 monitoring 分开补测试后再提。
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
431246e to
cb22147
Compare
4877faf to
07cc868
Compare
07cc868 to
0a028e8
Compare
asyncpg returns JSON / JSONB columns as raw strings by default, which breaks every LangBot code path that expects a dict (mcp server loading, bot adapter config, pipeline config, etc.) when running on PostgreSQL. The asyncpg dialect's built-in on_connect hook should install the codecs, but it does not fire reliably on the SQLAlchemy 2.0 async engine path. Monkey-patch AsyncAdapt_asyncpg_dbapi.connect so that each new connection registers json / jsonb codecs eagerly via the wrapper's run_async hook.
…in tool context The previous result.first() + row[0] pattern returned a raw string (primary key value) instead of the ORM entity on PostgreSQL/asyncpg. Use result.scalars().first() per SQLAlchemy 2.0 convention so the MonitoringMessage object is correctly retrieved.
0a028e8 to
c274f5c
Compare
| row = result.first() | ||
| if row: | ||
| return row[0] | ||
| obj = result.scalars().first() |
There was a problem hiding this comment.
execute_async 用的是 AsyncConnection,这里的 scalars().first() 仍会返回第一列,也就是 id 字符串。我用当前 SQLAlchemy 2.0.46 和真实 MonitoringMessage 跑了一次,结果是 'msg-1',不是 ORM 对象。现在的测试只是把 mock 预设成对象,没覆盖真实结果形态。请改用正确的映射/实体构造方式,并用实际 DB connection 测。
| wrapper.run_async(_init) | ||
| return wrapper | ||
|
|
||
| AsyncAdapt_asyncpg_dbapi.connect = _patched |
There was a problem hiding this comment.
SQLAlchemy 2.0.46 的 PGDialect_asyncpg.on_connect 已经会为每个连接注册 JSON/JSONB codec。这里再 monkey-patch 私有 connect 是重复的,reload 时还会叠加 wrapper;现有测试只验证 patch 被调用,没有真实 PG 失败用例。请删除,或先给出可复现用例和真实 PostgreSQL 集成测试。
概述 / Overview
更改前后对比截图 / Screenshots
检查清单 / Checklist
PR 作者完成 / For PR author
请在方括号间写
x以打勾 / Please tick the box withx项目维护者完成 / For project maintainer