Repository navigation
perf(lark): single-pass delivery scandir, session backoff reuse, and answer-image exponential retry - #9352
Conversation
…answer-image exponential retry Signed-off-by: LoopX Agent <[email protected]>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · Gemini 4 · Google · self_reported
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)
动机 / Motivation
受影响调用方:个人飞书私聊机器人的守护进程轮询器、回答附图补发流程以及长期维护后台投递队列的值守开发者。
优化前后场景对比:优化前(基线 49881d5),每次巡检都会通过 glob 与多次 stat 全量扫描私聊投递收据目录,_web_projection_paths 每 5 秒再次重复全目录扫描,数百条同会话已投递收据在单轮内反复读取并哈希同一份 session.json,且对于无外链文本或仅含相对路径名、不可能通过公网 URL 解析的图片目标仍每 300 秒拉起来源鉴权与重复下发提示;优化后(提交 8345ea0),单遍 os.scandir 直接复用 DirEntry.stat 指纹并与 Web 投影共享Scanned列表,同轮跨记录共享 session_tokens 去重,且无效图片目标在鉴权前快速失败、按 300s->600s->1200s->2400s->3600s 指数退避并在通知已核验后跳过重复发送。
可观测收益:暖扫描下完全保留既有待投递集合与目录清理语义,同时显著降低多历史收据轮询开销,并在 600 行合成对比与相对路径图片负向用例中将单次无效 _source_authority 调用与重复 answer_image_notice 下发均消除(2 次降至 0 次 / 1 次)。
非目标边界:本次提交不修改协议 schema、不改变请求接入或 Core 权威存储落盘格式,也不涵盖在线桌面发行安装包重启升级。
当前遗留差距:安装版桌面打包包体升级及真实多千条线上投递收据长尾 CPU 监控仍需合入后在服务窗口继续观测。
在不改变既有收据格式与人工可见消息投递语义的前提下,先解决高频轮询目录扫描、session.json 重复哈希与不可能图片链接的 300 秒死循环退避,使后台飞书扩展在千级收据规模下保持低 CPU 与稳定延迟;如果不做该切片,每次守护轮询都会随历史对话增多线性恶化并消耗额外子进程资源。
改动思路 / Approach
入口位于轮询调度每轮调用的 LarkPrivateConversations.pending_delivery_paths,以及终端回复/历史补图入口 _deliver_completed_answer 和 _reconcile_historical_answer_images。权威状态依然保留在投递收据 JSON、Core 会话存储以及已核验 deliveries 标志中,缓存字典、退避计数器 retry_count 与 _web_projection_scan_rows 仅承担只读加速或重调度提示,不授予任何投递权限。
架构评估结论:将纯发现视图构造、指纹校验与退避策略从接近 2000 行的传输会话主文件中抽离至 private_delivery_discovery.py,在保持 Core 权限核验与文件收据权威地位不变的前提下消除跨层重复扫描与同会话哈希。
当前 PR 边界:限定在 loopx/extensions/lark 的发现视图提示复用、每轮 session_tokens 去重以及回答附图重试调度策略,不触及 Core 控制面状态机或持久化契约。
正路径方面,PrivateDeliveryDiscovery.rows_with_fingerprints 通过单次 os.scandir 遍历并在 file_change_fingerprint 未被测试猴补丁替换时直接提取 DirEntry.stat(follow_symlinks=True) 构造指纹元组传入 _observe,随后在 pending_delivery_paths 内通过 try...finally 暂挂 _web_projection_scan_rows 给 _web_projection_paths 直接消费,消除每 5 秒一次的二次全量扫盘;同时为同轮所有需要构造上下文恢复指纹的已投递条目传入同一个 session_tokens 缓存字典以及已知的 path_fp,让上百条共享同一会话的历史收据只解析一次 session.json 并复用现成指纹。针对图片恢复路径,由纯函数 answer_image_retry_delay 按 300s -> 600s -> 1200s -> 2400s -> 3600s 计算退避时长并随 retry_count 持久化(成功时清除),并在调用高开销 _source_authority 之前使用 remote_answer_image_urls_plausible 拦截相对路径目标或无 https:// 源消息,最后在 deliveries["answer_image_notice.<phase>"].verified is True 时免除重复提示发送。相比已有 FileChatDiscovery.rows_with_fingerprints,这里沿用同一接口形态并保留了 Lark 特有的 triage 与 repairs 投影字段。
具体改动 / Changes
本次变更严格映射自 docs/architecture/rfcs/loopx-overall-roadmap-v0.md(基线修订 49881d5)中关于控制运维开销与即时通讯媒体重传边界的要求,准则完成映射如下:
S10(通过有界读取与单遍发现消除引擎边界之上多余的文件扫描与状态哈希):implemented— 由精确头 8345ea0 的PrivateDeliveryDiscovery.rows_with_fingerprints与每轮session_tokens去重实现,对应单测与 600 行合成对比通过。S5(私聊多媒体恢复保持严格会话隔离、有界退避及稳健通知语义):implemented— 由answer_image_retry_delay、remote_answer_image_urls_plausible与已核验通知去重实现,对应负向测试用例与全套集成测试通过。
改动涵盖 4 个文件(3 个运行时扩展文件与 1 个性能测试文件),共新增 512 行、删除 122 行(private_conversations.py 由 1996 行净减至 1982 行)。
关键代码讲解
PrivateDeliveryDiscovery.rows_with_fingerprints(loopx/extensions/lark/private_delivery_discovery.py):单次os.scandir(self.root)枚举并按文件名排序.json文件,借助_entry_change_fingerprint复用entry.stat(follow_symlinks=True);当测试注入了自定义file_change_fingerprint时自动回退到原函数,维持零行为漂移,异常条目安全清理缓存后继续扫描后续条目。context_recovery_fingerprints与session_recovery_backoff_token(loopx/extensions/lark/private_delivery_discovery.py 与 L59):从LarkPrivateConversations迁移为纯函数,并将进程内缓存上限扩充至SESSION_BACKOFF_TOKEN_CACHE_LIMIT = 1024。context_recovery_fingerprints直接接受扫描阶段的path_fp与单轮共享字典session_tokens,避免为同一会话重复读取与 SHA-256 哈希。answer_image_retry_delay与remote_answer_image_urls_plausible(loopx/extensions/lark/private_delivery_discovery.py 与 L112):指数退避函数按 300 秒左移至多 4 位并封顶 3600 秒;URL 预检正则确认无显式修理方案answer_image_plan时源文本包含https://且所有目标均为https://链接,阻断本地相对路径文件名触发无谓来源探测。LarkPrivateConversations._deliver_completed_answer与pending_delivery_paths(loopx/extensions/lark/private_conversations.py 与 L1543):在完成应答与历史补图路径写入并在成功时移除retry_count,把remote_answer_image_urls_plausible放到authorized()之前,跳过verified is True的重复answer_image_notice;pending_delivery_paths收集scanned_rows后用try...finally传给_web_projection_paths()并对无操作分支复用current_fp。PrivateWebProjectionMixin._web_projection_paths(loopx/extensions/lark/private_web_projection.py):优先消费同轮传入的_web_projection_scan_rows,外部独立调用时仍回退至self._delivery_discovery.rows(),完全兼容原有独立调用方。
对主干的风险 / Risks
- 猴补丁与指纹一致性风险:若测试对
_file_discovery.file_change_fingerprint打补丁以模拟并发篡改,_entry_change_fingerprint与context_recovery_fingerprints已显式比对 import 时默认函数身份并在不一致时退回file_change_fingerprint(path)(注:private_delivery_discovery.py第 44 行保留了一处未使用的别名常量_DEFAULT_FD_FILE_CHANGE_FP,纯属静态冗余,不影响功能)。最坏情况下可通过删除快捷路径无条件stat恢复。 - 非
https://图片链接前提限制(Negative Path):remote_answer_image_urls_plausible要求未提供人工修理计划answer_image_plan的远程图片回复必须包含https://源链接与目标前缀。如存在相对本地文件名,负向用例验证会快速拒绝并进入 300s->600s 指数退避,同时仅发送一次answer_image_notice提示且完整保留已发送文本正文。若未来新增非 HTTPS 外链协议需在预检放行,当前影响可控。 - 重入作用域安全与生产覆盖:
_web_projection_scan_rows严格约束于单线程轮询 worker 的try...finally生命周期内,且不替代下游reconcile_request与 Web 投影对原收据与 Core 存储的完整重读。 - 本地实测与对比矩阵(精确头 8345ea0,未等待或依赖远程 CI):
ruff check全过;loopx check --scan-path0 错误 0 警告;loopx canary premerge --tier quick通过(0 失败 0 hold);相关单元测试84 passed in 18.96s与整合集成测试743 passed, 1 warning in 221.53s全部通过;在 600 行合成收据加相对路径图片负向样本的基线(49881d5a3ac25adf8c105b2dbfdb109716cefbcb)与精确头(8345ea0e3ebb8e3551675b3ea443f1c7703f3860)同机对照测试中,健康队列输出完全一致且pending_delivery_paths降至 16.2ms,无效图片_source_authority调用降至 0 次、重复通知降至 1 次。
我的整体评价 / Verdict
就长程稳定性(long_horizon: improved)与用户体验(user_experience: preserved;纯后台发现与调度优化不修改 UI 视图,故 visual_review 为 not_applicable)而言,本 PR 精准解决飞书私聊发现与失败图片重试中的核心放大效应,未削弱任何权限核验、失败回退与已投递通知保障,同时使超长传输模块 private_conversations.py 体积净减且零新增公共词表或破坏性迁移字段(仅在已有投递字典中按需增删 retry_count 整数并复用既有状态枚举)。残余边界仅在于 https:// 远程图片前置假设与尚未覆盖真实生产长时服务运行观测(合入后常规服务运行窗口持续观察即可)。精确头 8345ea0 本地全量验证已完整闭环,准予合入。
English verdict: APPROVE
|
Reviewer: model_agent · gpt-6.2-sol · OpenAI · runtime_reported · xhigh Request changes conclusion (author-owned PR; GitHub blocks formal self-review) 合入后审计 · 本轮在合入前复现了下述回归。主干合入提交 动机 / MotivationLark 回复 worker 会周期性扫描投递记录。历史记录多时,同一轮重复扫描目录、读取同一 Session 的指纹,会增加后台 I/O。图片在正文送达后由单独的 worker 核验,失败时继续重试。本 PR 可以减少扫描和重复尝试,但已获授权的图片仍需回到原私聊,且接收方应能从原请求读到核验结果。改动范围是 Lark transport 的投递发现和重试,沿用现有会话绑定和 provider 授权。 改动思路 / Approach扫描复用和退避应继续服务于既有发现与恢复逻辑。扫描行只用来发现候选,真正投递仍由原 journal、Core Turn、会话绑定和 provider 回执决定。在现有 transport 内复用单趟扫描及当轮 Session 观察是合理的。图片预检查可以省掉明显无效目标的 provider 读取,前提是它复用现有 URL 判定,避免拒绝规范化后仍有授权的地址。
具体改动 / Changes基线 LoopX 2.4 私聊验证与权限边界 docs/product/releases/v2.4.0.md @ 49881d5a3ac25adf8c105b2dbfdb109716cefbcb 要求把已获授权的原图发回原私聊,并读回原会话的媒体回执。当前提交未满足 original-private-image-delivery 的 URL 兼容输入,具体对照见下方。original-source-authorization 在已验证路径继续使用现有绑定及原 provider 核验。实时 provider 和实际部署的吞吐仍待验证。 关键代码讲解
对主干的风险 / Risks[P2,阻塞] 我的整体评价 / Verdict在两个精确源码工作树,用同一个隔离的原私聊场景运行真实 admit、正文 worker、媒体 worker 和回执读回。provider 和 HTTPS 网络响应由测试夹具提供。基线三种 scheme 均完成一次媒体投递,head 只有小写通过,其余两种没有图片读取或上传。只在预检查中恢复 scheme 等价的对照夹具使 head 的三种输入均通过。当前 head 的目录扫描和 App 图片投影两组测试共 32 项通过,现有普通及历史图片 worker 场景另有 47 项通过。 单趟扫描减少了已覆盖场景的重复观察,保留它和有上限的重试仍可完成这次优化。当前图片预检查让已获授权的跟进反复失败,延长了恢复时间并损害原私聊交付。 English verdict: REQUEST_CHANGES 8345ea0. A case-sensitive precheck permanently skips previously authorized HTTPS image URLs. The same private-worker test passes all three scheme spellings on base and fails two on head. |
Goal And Delivered Outcome
os.scandir& Row Reuse:Path.glob("*.json")+ extra warmPath.stat()calls inPrivateDeliveryDiscovery(rows_with_fingerprints()/rows()) with a singleos.scandirpass reusingDirEntry.stat()whenfile_change_fingerprintis unpatched, while keeping exact monkeypatch compatibility for fault-injection tests.pending_delivery_paths()insidePrivateWebProjectionMixin._web_projection_paths(), eliminating a second full delivery directory scan every 5 seconds.LarkPrivateConversationsintoprivate_delivery_discovery.py(session_recovery_backoff_tokenwithSESSION_BACKOFF_TOKEN_CACHE_LIMIT=1024,context_recovery_backoff_valid,context_recovery_fingerprints,record_context_recovery_noop_backoff,load_historical_image_plan), shrinkingprivate_conversations.pyfrom1,996to1,982LOC.session_tokensdeduplication and precomputed fingerprint reuse inpending_delivery_paths()so hundreds of historical delivered records sharing the same session resolvesession.jsononce per pass instead of once per record.answer_image_retry_delay()(300s -> 600s -> 1200s -> 2400s -> 3600scap) for repeated unverifiedanswer_images/historical_answer_imagesfollow-ups.remote_answer_image_urls_plausible()pre-check before_source_authoritywhenanswer_image_plan is Noneso non-https://relative filename targets or origin messages without remote URLs fail fast without spawning unnecessary provider CLI subprocesses, and skipped re-delivering an already-verifiedanswer_image_notice.Author Declaration
loopx/extensions/lark/private_delivery_discovery.py,loopx/extensions/lark/private_conversations.py,loopx/extensions/lark/private_web_projection.py, andtests/test_lark_steward_directory_scan_perf.py.Validation
8345ea0e3ebb8e3551675b3ea443f1c7703f3860staticruff checkpassed across all 4 touched files;private_conversations.py1,982 LOC;loopx checkboundary scan clean (errors: 0,warnings: 0)unit®ression172 passed in 87.67s(test_lark_steward_directory_scan_perf.py,test_chat_file_discovery.py,test_chat_conversation_bindings.py,test_steward_topic_lanes.py,test_maintainability_ratchet.py)integration648 passed in 259.08s(test_lark_private_conversations.py,test_lark_private_feedback.py,test_lark_app_image_projection.py)premergeloopx canary premerge --from-git-diff --git-diff-base origin/main --tier quick:ok: true,failures: 0,manual_holds: 0(direct checks + maintainability ratchet + semantic vocabulary smoke)synthetic_benchrows()=10.0ms,pending_delivery_paths()pass1 =42.5ms, warm backoff =38.7msFrontend / Visual Evidence
No UI or presentation surface was modified; private Lark reply rendering and App web projection formats are unchanged.