refactor(llm): DRY router fallback error path and fix spawn_blocking for state saves#3432
Merged
refactor(llm): DRY router fallback error path and fix spawn_blocking for state saves#3432
Conversation
…for state saves Extract private `record_fallback_error` helper to deduplicate the error arm logic shared by `chat` and `chat_stream` fallback loops. The success paths diverge (quality gate + CoE vs plain stream open) and are kept separate; a NOTE comment in both methods documents the design decision. Convert `save_thompson_state`, `save_bandit_state`, and `save_reputation_state` from sync to `pub async fn` using `tokio::task::spawn_blocking` to avoid blocking the async executor on serialization + filesystem writes. `AnyProvider::save_router_state` now runs all three saves concurrently via `tokio::join!`. The sole caller in `zeph-core` is updated with `.await`. Resolves the FIXME at router/mod.rs:~733 (CPU-bound work on async thread).
f1632f1 to
02240de
Compare
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.
Summary
record_fallback_errorprivate helper to deduplicate the error-arm logic shared bychatandchat_streamfallback loops. The success paths diverge intentionally (quality gate + CoE vs plain stream) and are kept separate; a NOTE in both methods documents the decision.save_thompson_state,save_bandit_state,save_reputation_statetopub async fnwrappingtokio::task::spawn_blocking— resolves the FIXME for CPU-bound work on the async executor.AnyProvider::save_router_statenow runs all three saves concurrently viatokio::join!.zeph-core::agent::modupdated with.await.Motivation
Part of the m48 architectural cleanup plan (PR 2 of 8). Router was the highest bug-density file (5 follow-up PRs in recent history). Deduplicating the error path and offloading blocking I/O reduces future regression surface.
Test plan
cargo +nightly fmt --check— cleancargo clippy --workspace --all-features -- -D warnings— 0 warningscargo nextest run --config-file .github/nextest.toml -p zeph-llm -p zeph-core --lib --bins— 847 + 1502 passed