persistence
feishu.agent.persistence
¶
基于 SQLite / JSONL 的持久化默认实现:会话历史、挂起审批、幂等执行缓存与审计日志均可跨进程重启存活。
这些类分别实现 feishu.agent.session.SessionStore、feishu.agent.session.PendingApprovalStore、
feishu.agent.approval.ExecutionResultStore 与 feishu.agent.approval.AuditLog 协议,是内置 InMemory*
实现的持久化对应物:把它们传给 feishu.agent.loop.Agent 即可让会话与「人在环」审批在重启后继续。
并发与异步:结构化存储以 SQLite(WAL 模式)落盘,各自持有独立连接;异步存储用 asyncio.Lock 串行化连接
访问,同步存储用 threading.Lock。SQLite 本地操作通常为亚毫秒级,对机器人场景可接受;超大规模部署可按相同
协议替换为真正的异步数据库后端。审批认领以「读取—校验—UPDATE ... WHERE state='awaiting_confirmation'
(依据 rowcount)」实现 compare-and-swap,从而在并发确认下保证至多一次执行。
SqliteSessionStore
¶
基于 SQLite 的 feishu.agent.session.SessionStore 实现,会话历史跨重启存活。
每个会话以一行 JSON 存储其消息列表;append 为「读取—追加—写回」事务,并按 max_messages 截断最旧消息。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str | Path
|
SQLite 数据库文件路径,自动创建父目录并以 0o600 收紧权限。 |
必需 |
|
int
|
每个会话保留的最大消息数, |
400
|
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/agent/persistence.py
get
async
¶
读取指定会话的全部历史消息;会话不存在时返回空列表。
源代码位于: feishu/agent/persistence.py
append
async
¶
向指定会话追加消息,并按 max_messages 截断最旧消息。
源代码位于: feishu/agent/persistence.py
set
async
¶
SqlitePendingApprovalStore
¶
基于 SQLite 的 feishu.agent.session.PendingApprovalStore 实现,挂起审批跨重启存活。
实现完整的 CAS 生命周期(get/claim/complete/update,并保留 put/pop):claim 在事务内完成
存在性、TTL、防篡改与状态校验,并以 UPDATE ... WHERE state='awaiting_confirmation' 翻转状态,依据
rowcount 判定是否抢占成功,从而保证并发确认下至多一次执行。过期记录在访问时惰性清理。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str | Path
|
SQLite 数据库文件路径。 |
必需 |
|
int
|
等待确认的存活时长。默认为 |
_DEFAULT_TTL_SECONDS
|
|
int
|
冻结记录( |
_DEFAULT_EXECUTION_UNKNOWN_TTL_SECONDS
|
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/agent/persistence.py
| Python | |
|---|---|
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
put
async
¶
put(approval: PendingApproval) -> None
保存一次挂起的审批;未设置 created_at 时以当前时间戳记。
源代码位于: feishu/agent/persistence.py
get
async
¶
get(approval_id: str) -> PendingApproval | None
pop
async
¶
pop(approval_id: str) -> PendingApproval | None
取出并移除一次挂起审批,不存在时返回 None。
源代码位于: feishu/agent/persistence.py
claim
async
¶
claim(approval_id: str, *, expected_payload_sha256: str | None = None) -> ClaimResult
原子认领一次审批,返回 feishu.agent.session.ClaimResult;仅 CLAIMED 可继续执行。
源代码位于: feishu/agent/persistence.py
complete
async
¶
标记最终处置:成功/拒绝/取消即移除,结果未知则冻结为 execution_unknown。
源代码位于: feishu/agent/persistence.py
update
async
¶
update(approval_id: str, mutator: Callable[[PendingApproval], tuple[T, PendingApproval]]) -> T
以 compare-and-swap 方式原子更新一次审批:mutator(旧值) 返回 (返回值, 新值)。
源代码位于: feishu/agent/persistence.py
purge_expired
async
¶
purge_expired() -> int
删除所有已过期的审批记录,返回删除数量。
源代码位于: feishu/agent/persistence.py
SqliteExecutionResultStore
¶
基于 SQLite 的 feishu.agent.approval.ExecutionResultStore 实现:按幂等键缓存执行结果以支持重放。
put 同时为幂等键与各别名键写入指向同一结果的行;get 命中任一键即返回,从而让「已执行的写操作被再次
确认」时返回先前结果而非二次提交。方法为同步,以 threading.Lock 串行化独立连接。
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/agent/persistence.py
get
¶
按幂等键 / 别名键读取已缓存的执行结果记录,未命中返回 None。
源代码位于: feishu/agent/persistence.py
put
¶
put(idempotency_key: str, *, execution_status: str, result: Any, alias_lookup_keys: tuple[str, ...] = (), payload_sha256: str | None = None) -> None
写入一次执行结果,并为各别名键写入指向同一结果的行。
源代码位于: feishu/agent/persistence.py
JsonlAuditLog
¶
基于 JSONL 的 feishu.agent.approval.AuditLog 实现:仅追加地记录审批生命周期事件。
每行一条事件,仅记录负载的结构化摘要(feishu.agent.integrity.payload_summary)而非原始内容,避免敏感
数据落盘。文件以 0o600 创建,写入以 threading.Lock 串行化。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str | Path
|
审计日志文件路径(JSONL)。 |
必需 |
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/agent/persistence.py
append
¶
append(event_type: str, *, key: str, approval: PendingApproval | None = None, event_id: str | None = None, message_id: str | None = None, outcome: str = 'ok', error: str | None = None) -> None
追加一条审计事件。
源代码位于: feishu/agent/persistence.py
message_to_dict
¶
将 feishu.agent.llm.Message 序列化为可 JSON 化的字典。
message_from_dict
¶
从 feishu.agent.persistence.message_to_dict 的产物还原 feishu.agent.llm.Message。
源代码位于: feishu/agent/persistence.py
approval_to_dict
¶
approval_to_dict(approval: PendingApproval) -> dict[str, Any]
将 feishu.agent.session.PendingApproval 序列化为可 JSON 化的字典。
源代码位于: feishu/agent/persistence.py
approval_from_dict
¶
approval_from_dict(data: dict[str, Any]) -> PendingApproval
从 feishu.agent.persistence.approval_to_dict 的产物还原 feishu.agent.session.PendingApproval。