Repository navigation
fix: 插件模型调用不再全表扫描调用记录,修复整点任务导致的事件循环卡顿 - #126
Merged
Merged
Conversation
估算模型请求输入量时,request_estimate 会通过 last_mind_usage 查询本群最近一次聊天调用作为用量参照。这条查询没有可用索引,只能扫描整张 model_calls;scene 列排在 request、response 两个大字段后面,逐行判断时要读溢出页。插件调用记录的 plugin 有值,没有聊天回合的群永远匹配不到,每次都会扫完整张表。线上 8.7 GB 的库在容器内存不足时,每次查询在事件循环上同步阻塞约 20 秒:group_digest 的 hourly-notes 每个整点卡一次,daily-summary 连续调用模型时会连续卡好几分钟。 - 插件发起的模型调用(无论 role)不再查询聊天用量参照,插件 prompt 与聊天会话前缀不会匹配。 - 业务数据库格式升为 3,新增部分索引 model_call_mind_usage ON model_calls(scene,role,id) WHERE plugin IS NULL,聊天请求的这条查询改走索引。upgrade 接受格式 1、2,由补建缺失索引的循环建立新索引。 - 补充测试:插件请求不触发 last_mind_usage;查询计划使用新索引;格式 2 升级建立索引且可重复执行;格式 3 结构指纹。同步 CONTRIBUTING 格式表。
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改动内容
线上实例每个整点出现 15–23 秒的事件循环卡顿,22:00 的 group_digest daily-summary 连续调用模型时,从 22:00 到 22:07 几乎每分钟卡一次。loop_monitor 记录的调用栈全部停在
request_estimate→store.last_mind_usage的同步 SQLite 查询。原因:
last_mind_usage的查询(WHERE scene=? AND role='mind' AND plugin IS NULL ORDER BY id DESC LIMIT 1)没有可用索引,执行计划为SCAN model_calls。scene 列排在 request、response 两个大字段后面,逐行判断要读溢出页;插件调用记录的 plugin 有值,没有聊天回合的群永远匹配不到,每次都扫完整张表。线上库 8.7 GB,容器内存上限 1.25 GiB,页缓存装不下,每次都从磁盘读。request_estimate新增plugin参数,插件发起的模型调用(无论 role)不再查询聊天用量参照;request_model传入自己的 plugin。聊天回合和compact_now不受影响。model_call_mind_usage ON model_calls(scene,role,id) WHERE plugin IS NULL,查询计划改为SEARCH model_calls USING INDEX model_call_mind_usage (scene=? AND role=?)。其余 model_calls 查询在有无新索引时计划相同。配置与数据
业务数据库格式 2 → 3。
upgrade接受格式 1、2,由补建缺失索引的循环建立新索引。大库首次建索引需要读取大部分文件,可能需要数分钟,期间实例须停止。升级后 0.2.7 无法打开数据库,回退需要升级前的快照。配置格式和插件接口不变。核对
tests/test_mind_usage_anchor.py:插件请求不触发last_mind_usage(去掉 request.py 改动后该用例失败);查询计划使用新索引,无 SCAN、无 TEMP B-TREE。tests/test_jargon_migration.py新增格式 2 升级用例:建立索引、可重复执行、升级后last_mind_usage返回聊天调用。未确认
request_estimate中same_binding比较的request['settings']来自 JSON(retry_delays_seconds为 list),model_dump()为 tuple,两者永不相等,usage_assisted分支实际不会命中,compact_now的token_scale恒为 1.0。修复会改变聊天上下文预算估算,另行处理。