Merged
fix(a4p): 冷启动并发消费时 SQLite 报 readonly #398
fix(a4p): 冷启动并发消费时 SQLite 报 readonly #398
Merged
zhangzjAdd on 6 days ago
zhangzj
zhangzj
6 days ago

Paired: GitHub #211 ↔ GitCode !398

关联 Issue

Closes #92

修改内容

  • 把 CREATE TABLE IF NOT EXISTS 从事务内移到 BEGIN IMMEDIATE 之前,避免"建库竞争 + 写事务"叠加导致的只读句柄;
  • consume() 增加一次轻量重试,仅针对 readonly 类 OperationalError;
  • 其它 sqlite3.Error / OSError 仍统一包装为 IntentTokenUsageStoreError("Intent token usage store unavailable"),对外契约不变;
  • 数据库文件已存在的常规路径行为不变。

验证

单元测试

tests/unit/test_stores.py ....... [100%]
7 passed in 1.40s

并发冷启动压测(20 轮,每轮全新目录 + 2 进程同时 consume)

20/20 rounds passed

修复前同一脚本 3/3 轮失败,报 attempt to write a readonly database。

Linked Closing Issues:

likedislike
Pull Request has been successfully merged by openJiuwen-bot.
(Thank you for zhangzj's contribution.)
zhangzjzhangzj
6 days ago Associated issue: [Bug] Intent token usage store 冷启动并发消费必然 SQLite readonly
atomgit-bot
atomgit-bot
6 days ago Comments:

变更摘要

本 PR 修复 A4P-python-sdk 中 SQLiteIntentTokenUsageStore 在冷启动并发消费场景下,多个连接竞争创建数据库文件时出现 attempt to write a readonly database 的问题。核心做法是将建表语句从事务内提前到 BEGIN IMMEDIATE 之前执行,并在 consume() 层针对 readonly 类错误增加一次轻量重试,同时保持对外异常契约不变。

主要改动

  • 建表语句前移: 将 CREATE TABLE IF NOT EXISTS intent_token_usage 提取为类常量 _SCHEMA_SQL,并在开启 BEGIN IMMEDIATE 写事务之前执行,避免“建库竞争 + 写事务”叠加导致只读句柄。
  • consume() 增加轻量重试: 新增 _WRITE_RETRY_ATTEMPTS = 2 与循环重试逻辑,仅在捕获到 sqlite3.OperationalError 且错误信息包含 readonly 时以 0.01 秒间隔重试一次,末次尝试或非只读错误则直接抛出。
  • 拆分 _consume_once(): 原 consume() 的单次执行逻辑整体下沉到私有方法 _consume_once(),接收已归一化的 normalized_token_id、max_executions、expire_at_epoch 参数,便于重试复用。
  • 异常包装职责上移: _consume_once() 内对 OSError 与 sqlite3.Error 由原包装改为原样抛出,统一由 consume() 包装为 IntentTokenUsageStoreError("Intent token usage store unavailable"),对外错误契约保持兼容。

关联 Issue:#92

likedislike
Incorrect?
atomgit-bot
atomgit-bot
6 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
atomgit-bot
atomgit-bot
6 days ago Comments:

AtomGit AI 助手使用指南

在 PR 评论中使用命令即可触发。支持在普通评论和代码行评论中使用。

功能 命令 说明
代码审查 /ai review 检查代码质量、潜在问题、安全风险
PR 摘要 /ai summary 生成 PR 变更内容的结构化摘要
代码解释 /ai explain 解释代码变更的逻辑和意图
自由提问 @atomgit-bot <问题> 询问关于本 PR 的任何问题
帮助 /ai help 显示此帮助信息

免责声明

AI 助手可能存在误判,请结合自身判断。可以对评论点 👍 或 👎 帮助我们改进。

likedislike
Incorrect?
OopenJiuwen-botCollaborators
6 days ago Set YiyangShao,xinyu-jiuwen as approver
OopenJiuwen-botCollaborators
6 days ago Added label: sig/sig-agent-protocol
openJiuwen-botCollaborators
6 days ago Comments:

欢迎来到 openJiuwen 社区

Hey @gp1000 , 感谢你对社区的贡献.

机器人使用手册

有关指令的使用,可以点击 此处 查看详情。开发人员可以在每个PR或Issue下方评论特定指令来触发机器人任务。

联系指引

有疑问可以联系 SIG: sig-agent-protocol ,
维护者是: @YiyangShao, @xinyu-jiuwen ,
审核者是: @gcw_hx9prznZ, @mingo007 .

likedislike
OopenJiuwen-botCollaborators
6 days ago Added label: openJiuwen-cla/yes
openJiuwen-botCollaborators
6 days ago Comments:

CLA 签署成功

gp1000, 感谢您的代码贡献。 所有的代码贡献者都已完成了 CLA 签署。 👍

likedislike
Oopenjiuwen-syncCollaborators
6 days ago Modify description of pull request
Oopenjiuwen-syncCollaborators
6 days ago Modify description of pull request
openJiuwen-botCollaborators
6 days ago Comments:

The pipeline(pipeline number:398) is running. Please wait a moment...

likedislike
OopenJiuwen-botCollaborators
6 days ago Added label: ci-running
OopenJiuwen-botCollaborators
6 days ago Removed label: ci-running
openJiuwen-botCollaborators
6 days ago Comments:
任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
A4P Java SDK UT ✅SUCCESS 点此跳转
likedislike
OopenJiuwen-botCollaborators
6 days ago Added label: ci-successful
maoqiming
maoqimingCollaborators
6 days ago Comments:

/lgtm
/approve

likedislike
OopenJiuwen-botCollaborators
6 days ago Added label: approvedlgtm-mingo007
openJiuwen-botCollaborators
6 days ago Comments:

Review Code Feedback

  • The label lgtm-mingo007, approved was added to this pull request. It means that mingo007 reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄
likedislike
OopenJiuwen-botCollaborators
6 days ago Closed associated issue
OopenJiuwen-botCollaborators
6 days ago Merged pull request
OopenJiuwen-botCollaborators
6 days ago Removed label: ci-successful