perf(runtime): reduce database-bound turn latency - #670
Conversation
XuPeng-SH
left a comment
There was a problem hiding this comment.
结论:当前不建议合并,请先修复以下问题。
- [P1] 流式执行失去 terminal ownership 后仍会写派生状态
crates/runtime/src/server/run/lifecycle/mod.rs 的 terminal projection block(约 L16629-L16684)没有受 owner_terminal_committed 约束,并调用了无 generation fence 的 persist_usage、transcript materialization 和 run_after_core。当 terminal CAS 返回 Superseded、authority 不确定或 generation 过期时,旧 executor 仍可能覆盖 usage、写入 CSL/hook/observer/promotion/projection,污染企业审计、计费和 ContextPipeline 数据。
请让所有派生写入重新受 terminal owner fence 保护;删除额外的 unfenced usage 写入,只保留 atomic settlement 或 persist_usage_if_current_owner。需要补 terminal CAS=Superseded 的测试,断言这些派生 sink 不会被调用。
- [P1] best-effort projection 被放入 terminal SSE 关键路径
同一段代码在发布 terminal SSE 前 tokio::join! 等待 usage、transcript、hook DB、Memoria observer、SessionEnd hooks、promotion 和 projection。任一 sink 变慢都会阻塞已经 durable committed 的回答,导致客户端超时并触发重复请求;这也与本 PR 的降低 turn latency 目标相悖。
canonical commit、provider settlement、terminal CAS 才是响应正确性的关键路径。请恢复“先完成 terminal SSE,再执行派生 projection”的边界;如果必须提前执行,也必须有明确超时、降级和 owner fence。
- [P2] 删除
reasoning_done/thinking_done的 durable replay 会破坏断线恢复
crates/runtime/src/server/run/lifecycle/run_state.rs 的 terminal persistence 和 live persistence 过滤掉了 reasoning/thinking completion marker。客户端已经收到 reasoning delta 后断线重连时,没有 completion marker 可能无法关闭 thinking UI,直到整个 run 结束。
请保留轻量 completion marker,或先定义并实现明确的 reconnect state snapshot,再删除 marker,并补充断线恢复测试。
- [P1/process] PR 混入了尚未批准且已过时的 #663
PR #670 的 commits 包含 #663 的旧版 edge registry commit(9728d51),但 #663 仍为 Open / CHANGES_REQUESTED,当前 head 已经不同;当前 main 也不包含该 commit。PR 描述中“#663 changes are already part of the base”不成立。请基于最新 main 重新整理纯 latency 分支,避免把未批准的 edge claim/heartbeat/unregister 实现带入本 PR。
正向评价:atomic writer+turn admission、successful inference settlement、PipelineSession-scoped prompt cache、request-scoped discovery snapshot,以及 hash-domain separation 的方向是正确的,也符合 Astra 的企业 durable-agent 定位。但性能优化必须服从 authority fencing、Trace/审计完整性、Runner/Edge side-effect recovery 和断线恢复语义。
9c48ca5 to
6abe5e9
Compare
|
已按 review 完成自检和修复,提交为
本地定向验证通过:两项新增/恢复的 runtime tests、 |
|
The current head
The PR description has also been updated to reflect these ownership, SSE ordering, reconnect, and branch-scope corrections. Please re-review the current head rather than the superseded reviewed commit. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
当前 head 已修复上轮四个问题,但仍有一个需要阻断合并的持久化兼容性问题:
[P1] 复用历史 staged manifest 时没有把它重新激活
persist_database_immutables_in_tx 现在只在新插入时把 reachable 写成 1;当 INSERT IGNORE 命中已存在的相同 manifest 时,代码仅校验 manifest_json,不会把已有行从 reachable = 0 更新为 1。见:
Astra/crates/services/src/session_context_coordinator.rs
Lines 2597 to 2660 in 6abe5e9
这是升级兼容场景,不是理论分支:当前 main 的旧流程会先在独立事务里写入默认 reachable = 0 的 manifest,再在 canonical commit 事务里执行 UPDATE ... SET reachable = 1。进程中断、cursor conflict 或 commit 失败都可能留下内容完整但不可达的 staged manifest。升级到本 PR 后重试相同确定性 delta 会命中这行;随后 session head 被提交为该 manifest root,但 materialize 仍按 reachable = 1 过滤,canonical head 因而指向不可物化的历史。
请在确认 existing manifest 及其 references 完全匹配后,于同一事务显式激活该行(保留 generation/totals 等 fence,并校验恰好一行),并增加回归测试:预置相同 root、reachable = 0 的旧版 staged manifest,重试 commit 后应变为 reachable 且可 materialize。
6abe5e9 to
7ca196d
Compare
|
已修复这个 P1,并将分支 rebase 到最新 |
XuPeng-SH
left a comment
There was a problem hiding this comment.
重新审查了最新 head 7ca196d3。上轮指出的 legacy staged manifest 问题已经正确修复:existing row 与 references 先被完整校验,reachable = 0 再在同一事务内带 fence 激活,并有真实 MatrixOne 的 materialize 回归测试。
当前仍有一个合并阻断项:
[P1] rebase 后 runtime 测试无法编译
最新 main/#690 增加的测试仍调用已被本 PR 重命名移除的 CanonicalRewriteProof::new:
Astra/crates/runtime/src/server/run/lifecycle/tests.rs
Lines 833 to 837 in 7ca196d
本 PR 只保留 CanonicalRewriteProof::from_materialized_admission,因此 clippy、astra-runtime、core crates 和 online integration 都在同一处报 E0599;这不是 CI/flaky 或网络问题,而是确定性的 source integration failure。
请把该测试迁移到 from_materialized_admission,并继续保持两个 hash domain 分离:第二个参数应表达 synthetic/admitted manifest root,不要重新把 canonical_conversation_root(&prior) 当成 manifest root。修复后至少确保 runtime test compile、clippy 和相关测试通过。
7ca196d to
a906bc4
Compare
|
已修复最新 P1,并同时处理了冲突和同源编译问题。分支已 rebase 到 |
Summary
Ports the database-bound turn-latency work from #656 onto current
mainwithout restoring implementation thatmainhas already superseded.High MatrixOne latency magnifies every serialized read and write around a warm agent turn. This change keeps the same durable and authorization boundaries while reducing avoidable round trips and overlapping work that is causally independent:
reasoning_done/thinking_donemarkers durable for reconnect while raw reasoning content and deltas remain live-only;PipelineSession;reachable = 0staged manifests inside the canonical commit transaction, preserving generation and aggregate fences without adding a read to the new-manifest hot path.The branch was rebuilt directly on current
main. It contains no #663 Edge registry commit or implementation.Main-specific adaptation is deliberate:
fix(runtime): preserve prompt cache and durable provider context #693 preserves final wire-prompt cache diagnostics and durable provider-context recovery; the rebase keeps those semantics while retaining this PR's session-scoped static-section cache.
feat: prepare Astra for open source release #639 removed the legacy MatrixOne task-board owner; this PR does not reintroduce it.
mainalready owns atomic composite checkpoint persistence; this PR keeps that write-through contract and does not restore the rejected heavy-checkpoint deferral state.mainperforms authoritative logical-attempt recovery before prompt capture. Prompt capture remains after durable admission so a recovered attempt cannot persist a prompt under the preceding attempt identity.No heuristic fast path, Tool/Skill suppression, authorization relaxation, schema migration, or compatibility fallback is introduced.
Related issue
Follow-up/main port of #656. The original latency investigation and paired MOI integration are tracked in matrixorigin/matrixflow#16389.
Change type
User and compatibility impact
No intended user-visible or API compatibility change. Warm Server turns perform fewer serialized database operations. Terminal SSE is published immediately after the durable correctness boundary; slower derived projections cannot extend the visible stream tail. Reconnect still receives reasoning/thinking completion markers. No configuration, schema, or migration is required.
Architecture and complexity delta
SessionContextCoordinatorowns the combined admission snapshot and atomic writer-plus-turn reservation; inference execution owns atomic successful settlement; runtime lifecycle owns request-scoped capability discovery reuse and owner-fenced post-terminal projection ordering;PipelineSessionowns compiled static prompt sections.mainremain removed. The previously stacked fix(edge): make registry claim settlement idempotent #663 Edge registry changes were removed from this branch entirely. No compatibility shim or parallel store is added.Net diff against current
main: 25 files, +2566/-858; zero database tables, migrations, public statuses, or configuration fields.Verification
cargo fmt --all -- --check— passed on the rebuilt branch.\n -cargo check -p astra-services --tests— passed after the staged-manifest compatibility fix.\n -cargo clippy -p astra-services --tests -- -D warnings— passed after the staged-manifest compatibility fix.\n -cargo test -p astra-services session_context --lib— passed; 1,861 unrelated tests filtered by the targeted name.cargo check -p astra-services -p astra-runtime— passed before the review-only terminal ordering correction; its affected runtime target was compiled again by the tests below.cargo clippy -p astra-services -p astra-messaging -p astra-turn-core -p astra-runtime --all-targets -- -D warnings— passed before the review-only terminal ordering correction.cargo test -p astra-runtime superseded_terminal_skips_all_owner_derived_projections -- --nocapture— passed; a Superseded owner gate invokes none of the derived projection sinks.cargo test -p astra-runtime terminal_events_for_persistence_keeps_only_terminal_lifecycle_events -- --nocapture— passed; raw reasoning remains excluded while completion markers persist.cargo test -p astra-runtime turn::llm::durable::tests --lib -- --nocapture— 48 passed.cargo test -p astra-runtime turn::canonical_commit::tests --lib -- --nocapture— 2 passed after the fix(runtime): preserve prompt cache and durable provider context #693 rebase; provider replacement remains bound to the materialized conversation root while commit fencing retains the separate manifest root.cargo test -p astra-runtime typed_objective_relations_survive_real_tiered_compaction --lib -- --nocapture— passed after migrating the fix(runtime): preserve compacted user anchor at canonical commit #690 regression tofrom_materialized_admission.cargo clippy -p astra-runtime --all-targets -- -D warnings— passed after resolving the fix(runtime): preserve prompt cache and durable provider context #693 conflict and canonical-proof integration.cargo test -p astra-services resource_governor --lib— 17 passed.cargo test -p astra-turn-core static_sections_cache_is_scoped_to_pipeline_session— passed before the main rebase; the post-rebase workspace compile and Clippy gates cover the adapted call sites.cargo test -p astra-runtime compaction_ --lib— 114 passed before the main rebase; the main port retains the same compaction commits and post-rebase compile/Clippy gates passed.make test-contract— 10 admin, 4 HTTP, and 2 settings contract tests passed.make test-offline— progressed through Server-only runtime and Web profile tests, then stopped in an unchangedastra-edgetest because this macOS host rejected its temporary workspace-lock namespace. This branch changes neithercrates/astra-edgenorcrates/astra-tools; the same host condition is independent of this diff.completion_http_boundary_persists_success_failure_and_caller_cancellation— passed twice on this exact port; 2.29s including first schema readiness and 0.42s warm.git diff --check origin/main...HEAD— passed.main— passed on macOS and Linux; Linux completed in 2.46s.matrixone:latest:INSERT IGNOREreported one affected row for a duplicate debt, so the unsafe affected-row early return skipped authoritative identity recovery; removing that early return restored exactprovider_attempt_idrecovery, and the formerly deterministic failure passed 20/20 consecutive runs./chat/streamwith Astra Server and EPH Sandbox, including warm follow-up turns under injected SQL latency. This exactmainport was not redeployed.reachable = 0, then proves retry activation and materialization. Thismainport adds no schema or migration. Its caller-cancellation settlement path was rerun against local MatrixOne twice and converged correctly; the remaining adapted SQL paths retain the same transactional/fencing contracts and are covered by compile, Clippy, durable lifecycle, and contract tests above.Final checklist