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.AgentEngine 即可让会话与「人在环」审批在重启后继续。
并发与异步:结构化存储以 SQLite(WAL 模式)落盘,各自持有独立连接;异步存储用 asyncio.Lock 串行化连接
访问,同步存储用 threading.Lock。SQLite 本地操作通常为亚毫秒级,对机器人场景可接受;超大规模部署可按相同
协议替换为真正的异步数据库后端。审批认领以「读取—校验—UPDATE ... WHERE state='awaiting_confirmation'
(依据 rowcount)」实现 compare-and-swap,从而在并发确认下保证至多一次执行。
SqliteMemoryStore
¶
SQLite-backed memory store partitioned by namespace and user/project scope.
源代码位于: feishu/agent/persistence.py
| Python | |
|---|---|
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
resolve_owner
async
¶
Persist links supplied by trusted caller identity, never by model tool arguments.
源代码位于: feishu/agent/persistence.py
recall
async
¶
recall(*, namespace: str, owner_key: str | None, query: str, limit: int = 12) -> list[MemoryRecord]
Search the caller-visible memories without making them part of a model prompt.
源代码位于: feishu/agent/persistence.py
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
¶
updated_at
async
¶
返回指定会话最近写入时间戳;未知会话返回 None。
源代码位于: feishu/agent/persistence.py
| Python | |
|---|---|
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 | |
|---|---|
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
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
SqlitePendingAuthorizationStore
¶
基于 SQLite 的 feishu.agent.session.PendingAuthorizationStore 实现,挂起授权跨重启存活。
OAuth callback 可能晚于原消息数分钟到达,甚至跨进程重启;该 store 以 authorization_id 保存恢复所需的
tool call 上下文,并用 claim 保证重复 callback 至多恢复一次。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str | Path
|
SQLite 数据库文件路径。 |
必需 |
|
int
|
等待授权的存活时长。默认为 |
_DEFAULT_AUTHORIZATION_TTL_SECONDS
|
|
int
|
冻结记录( |
_DEFAULT_EXECUTION_UNKNOWN_TTL_SECONDS
|
源代码位于: feishu/agent/persistence.py
| Python | |
|---|---|
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 | |
put
async
¶
put(authorization: PendingAuthorization) -> None
保存一次挂起授权;未设置 created_at 时以当前时间戳记。
源代码位于: feishu/agent/persistence.py
get
async
¶
get(authorization_id: str) -> PendingAuthorization | None
读取挂起授权而不移除;过期状态由 claim 判定,以便 callback 仍能回原会话提示用户。
pop
async
¶
pop(authorization_id: str) -> PendingAuthorization | None
取出并移除一次挂起授权,不存在时返回 None。
源代码位于: feishu/agent/persistence.py
claim
async
¶
claim(authorization_id: str) -> ClaimResult
原子认领一次授权,返回 feishu.agent.session.ClaimResult;仅 CLAIMED 可恢复工具。
源代码位于: feishu/agent/persistence.py
complete
async
¶
标记最终处置:retry 重开,unknown/frozen 冻结,其余终态移除。
源代码位于: feishu/agent/persistence.py
update
async
¶
update(authorization_id: str, mutator: Callable[[PendingAuthorization], tuple[T, PendingAuthorization]]) -> 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 化的字典。
源代码位于: feishu/agent/persistence.py
message_from_dict
¶
从 feishu.agent.persistence.message_to_dict 的产物还原 feishu.agent.llm.Message。
源代码位于: feishu/agent/persistence.py
| Python | |
|---|---|
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。
源代码位于: feishu/agent/persistence.py
authorization_to_dict
¶
authorization_to_dict(authorization: PendingAuthorization) -> dict[str, Any]
将 feishu.agent.session.PendingAuthorization 序列化为可 JSON 化的字典。
源代码位于: feishu/agent/persistence.py
authorization_from_dict
¶
authorization_from_dict(data: dict[str, Any]) -> PendingAuthorization
从 feishu.agent.persistence.authorization_to_dict 的产物还原挂起授权。