refactor(runtime): consolidate service event loop ownership - #4572
Open
qin-ctx wants to merge 7 commits into
Open
refactor(runtime): consolidate service event loop ownership#4572qin-ctx wants to merge 7 commits into
qin-ctx wants to merge 7 commits into
Conversation
Keep async orchestration on the service loop while moving pure CPU preparation and log I/O to their dedicated execution boundaries.
Preserve request observability while removing full Base64 scans and temporary payload copies.
Keep the stateful local model serialized while moving its synchronous inference to a worker thread.
Replace accepted Base64 image URLs with a fixed placeholder before trace and Langfuse export.
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.
Description
本 PR 通过重新划分执行职责,根治 Service Event Loop 被同步 CPU、日志格式化以及跨 Event Loop 调度阻塞的问题。它不为 A/B/C 分别增加补丁,而是收敛为三个明确的执行边界:
Before
QueueManager为每个 Queue 创建一个 daemon thread 和私有 Event Loop。Queue handler 先在 Queue Loop 中运行,再通过run_coroutine_threadsafe跳回 Service Loop;启动、停止、取消和 ACK 分散在两套生命周期中。run_async再跨一次 Loop。Message.estimated_tokens每次读取都会重新扫描全部 Parts。同一批消息在 pending-token、retention 和 context budget 阶段会重复计算。json.dumps(..., indent=2)整个请求体;QueueHandler.prepare()也在调用线程完成日志格式化,因此“大日志”仍会阻塞主循环。旧流程:
After
OpenVikingService.initialize()/close()直接 awaitQueueManager.start()/stop();每个 durable queue 是 Service Loop 上的一个asyncio.Task,按dequeue -> await handler -> ACK单向执行。service_loop,也不再使用run_coroutine_threadsafe/wrap_future。MessagePreparer作为纯 CPU Owner:在 worker thread 中完成消息构造、Turn 级工具输出选择、摘要、哈希和 Token 定稿;Service Loop 只执行ToolResultStore异步 I/O 和权威消息追加。Message的 Token 估算改为内存懒缓存,消息变更后由变更 Owner 显式刷新;本 PR 不增加 Token 持久化、历史回填或 JSONL Schema。新流程:
同一场景的前后对比
输入保持一致:向 Session 写入一个包含约 100 KB
tool_output的消息,随后触发 commit。role、parts、tool_outputmessage.id和tool_output_refPreparedMessageBatch对客户端返回字段、Session 消息格式、工具输出引用、Queue ACK 时机和失败恢复语义均不变。并发配置现在在配置边界要求
embedding.max_concurrent > 0、vlm.max_concurrent > 0,运行时不再静默猜测无效值。Human Involvement
Related Issue
无公开 GitHub Issue;问题来自内部对 Service Event Loop 阻塞问题 A/B/C 的复盘。
Type of Change
Changes Made
Testing
验证结果:
ruff check通过。python -m compileall -q openviking openviking_cli通过。git diff --check origin/main...HEAD通过。Checklist
Screenshots (if applicable)
不适用。
Additional Notes
origin/main:715 insertions / 955 deletions,净减少 240 行;Session本身净减少 352 行。MessagePreparer替代 Session 内消息构造、Turn 级工具输出选择和同步外部化职责;新旧两套机制没有并存。