Skip to content

Commit 258cc92

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw
authored
fix(desktop): survive degraded networks with rate-limit-aware relay client (#2197)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co>
1 parent 7b25950 commit 258cc92

29 files changed

Lines changed: 2429 additions & 95 deletions

‎AGENTS.md‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -462,34 +462,41 @@ new arbitrary text-size literal — px **or** rem/em. Genuinely decorative glyph
462462
(e.g. the `text-[6rem]` avatar emoji) are allowlisted by `path:line` in that
463463
script.
464464

465-
### Workspace Switching
465+
### Community Switching
466466

467-
The desktop app supports multiple workspaces (each backed by a different relay).
468-
Switching workspaces does **not** reload the page — it uses React key-based
469-
remounting. `<AppReady key={workspaceKey} />` in `App.tsx` forces the entire
470-
workspace-scoped subtree to unmount and remount with fresh state.
467+
The desktop app supports multiple communities (each backed by a different relay).
468+
Switching communities does **not** reload the page — it uses React key-based
469+
remounting. `<AppReady key={communityKey} />` in `App.tsx` forces the entire
470+
community-scoped subtree to unmount and remount with fresh state.
471471

472472
**Module-level singletons must be explicitly reset.** React remounting only
473473
clears React state (useState, useRef, context). Module-level variables (Maps,
474-
class instances, cached promises) survive across remounts. Every workspace-scoped
475-
singleton needs a reset function wired into `resetWorkspaceState()` in
476-
`desktop/src/features/workspaces/useWorkspaceInit.ts`.
474+
class instances, cached promises) survive across remounts. Every community-scoped
475+
singleton needs a reset function wired into `resetCommunityState()` in
476+
`desktop/src/features/communities/useCommunityInit.ts`.
477477

478-
Current singletons that are reset on workspace switch:
478+
Current singletons that are reset on community switch:
479479
- `relayClient.disconnect()` — WebSocket teardown + promise rejection
480+
- `resetRateLimitGate()` — clears any active rate-limit window from the old relay
481+
- `clearAllDrafts()` — message draft cache
482+
- `resetAgentObserverStore()` — agent observer relay store
483+
- `resetActiveAgentTurnsStore()` — active agent turn timers
484+
- `resetAgentWorkingSignal()` — agent working indicator signal
485+
- `resetSidebarRelayConnectionCardState()` — sidebar relay card dismiss state
480486
- `resetMediaCaches()` — proxy port and relay origin caches
487+
- `resetVideoPlayerState()` — video player singleton
488+
- `resetRenderScopedReactionHydration()` — reaction hydration cache
481489
- `clearSearchHitEventCache()` — search result event cache
482-
- `clearAllDrafts()` — message draft cache
490+
- `clearMarkdownNodeCache()` — markdown parse-node cache
483491

484492
**If you add a new module-level cache, Map, or class instance that holds
485-
workspace-scoped data, you must add its reset to `resetWorkspaceState()`.**
486-
Failure to do so causes data from the old workspace to leak into the new one.
493+
community-scoped data, you must add its reset to `resetCommunityState()`.**
494+
Failure to do so causes data from the old community to leak into the new one.
487495

488496
Key files:
489-
- `desktop/src/app/App.tsx` — workspace key, init gate, remount boundary
490-
- `desktop/src/features/workspaces/useWorkspaceInit.ts` — `resetWorkspaceState()`, applies config to Tauri backend
491-
- `desktop/src/features/workspaces/useWorkspaces.tsx` — `WorkspacesProvider` context (shared state for App + AppShell)
492-
- `desktop/src/main.tsx` — provider hierarchy (`QueryClientProvider` > `WorkspacesProvider` > `App`)
497+
- `desktop/src/app/App.tsx` — community key, init gate, remount boundary
498+
- `desktop/src/features/communities/useCommunityInit.ts` — `resetCommunityState()`, applies config to Tauri backend
499+
- `desktop/src/main.tsx` — provider hierarchy (`QueryClientProvider` > `App`)
493500

494501
---
495502

‎desktop/scripts/check-file-sizes.mjs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,16 @@ const overrides = new Map([
316316
// migration/materialize.rs; ratchet held at 1110.
317317
["src-tauri/src/migration_tests.rs", 1110],
318318
["src-tauri/src/nostr_convert.rs", 1126],
319-
["src/shared/api/relayClientSession.ts", 1022],
319+
// degraded-network resilience: relay.rs grew past 1000 with the addition of
320+
// relay_error_message hint-capping (oversized-hint test via loopback TCP) and
321+
// the relay_admission freshness-verification test. The loopback mock was
322+
// hardened (std::net + request-read-before-write) adding ~10 lines.
323+
// Queued to split test helpers to relay/tests.rs.
324+
["src-tauri/src/relay.rs", 1047],
325+
// degraded-network resilience: visibleChannelId field + getter/setter, NOTICE
326+
// handler for relay back-pressure, and rate-limit gate imports add ~74 lines
327+
// of load-bearing degraded-network recovery code. Queued to split.
328+
["src/shared/api/relayClientSession.ts", 1096],
320329
// Boot-time event sync (persona/team/agent event reconcile) was split out
321330
// to event_sync.rs, ratcheting this limit 1575 → 1310. Remaining content is
322331
// the pre-identity data migrations; still queued to split further.

‎desktop/src-tauri/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@ tempfile = "3"
123123
strip-ansi-escapes = "0.2"
124124

125125
[dev-dependencies]
126+
# `test-util` enables tokio's paused-clock (`start_paused`) so the relay
127+
# admission gate tests can assert exact wait durations without real sleeps.
128+
tokio = { version = "1", features = ["test-util"] }

‎desktop/src-tauri/src/commands/personas/snapshot/import.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ async fn submit_engram_event(
640640
use crate::relay::build_nip98_auth_header_for_keys;
641641
use reqwest::Method;
642642

643+
// Wait before signing: the relay enforces NIP-98 freshness (±60s) and the
644+
// gate may hold for up to MAX_HINT_SECONDS (300s). Building auth before the
645+
// wait produces a stale `created_at` that the relay will reject.
646+
crate::relay_admission::wait_for_rate_limit().await;
643647
let auth = build_nip98_auth_header_for_keys(agent_keys, &Method::POST, url, event_json)?;
644648
let mut request = state
645649
.http_client

‎desktop/src-tauri/src/commands/team_snapshot.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,10 @@ async fn submit_engram_event(
901901
use crate::relay::build_nip98_auth_header_for_keys;
902902
use reqwest::Method;
903903

904+
// Wait before signing: the relay enforces NIP-98 freshness (±60s) and the
905+
// gate may hold for up to MAX_HINT_SECONDS (300s). Building auth before the
906+
// wait produces a stale `created_at` that the relay will reject.
907+
crate::relay_admission::wait_for_rate_limit().await;
904908
let auth = build_nip98_auth_header_for_keys(agent_keys, &Method::POST, url, event_json)?;
905909
let mut request = state
906910
.http_client

‎desktop/src-tauri/src/commands/workspace.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ pub async fn apply_workspace(
143143
let mut override_guard = state.relay_url_override.lock().map_err(|e| e.to_string())?;
144144
*override_guard = Some(relay_url);
145145
}
146+
// Reset the Rust-side admission gate when switching workspace/community,
147+
// matching `resetRateLimitGate()` on the TS side (useCommunityInit.ts:38).
148+
crate::relay_admission::reset_gate_for_workspace_change();
146149

147150
if let Some(keys) = parsed_keys {
148151
let mut keys_guard = state.keys.lock().map_err(|e| e.to_string())?;

‎desktop/src-tauri/src/huddle/pipeline.rs‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ pub(crate) fn spawn_transcription_task(
305305
continue;
306306
}
307307
};
308+
// Wait before signing: the relay enforces NIP-98 freshness (±60s)
309+
// and the gate may hold for up to MAX_HINT_SECONDS (300s). Sign
310+
// the kind event and build NIP-98 auth after the wait so both
311+
// timestamps are fresh — single clean order: wait → sign → auth → send.
312+
crate::relay_admission::wait_for_rate_limit().await;
308313
let event = match builder.sign_with_keys(&keys) {
309314
Ok(e) => e,
310315
Err(e) => {
@@ -327,21 +332,23 @@ pub(crate) fn spawn_transcription_task(
327332
}
328333
};
329334

330-
let response = http_client
331-
.post(&url)
332-
.header("Authorization", auth_header)
333-
.header("Content-Type", "application/json")
334-
.body(body_bytes)
335-
.send()
336-
.await;
335+
let response = {
336+
http_client
337+
.post(&url)
338+
.header("Authorization", auth_header)
339+
.header("Content-Type", "application/json")
340+
.body(body_bytes)
341+
.send()
342+
.await
343+
};
337344

338345
match response {
339346
Ok(resp) if resp.status().is_success() => {}
340347
Ok(resp) => {
341-
eprintln!(
342-
"buzz-desktop: STT kind:9 post failed: HTTP {}",
343-
resp.status()
344-
);
348+
// Route through relay_error_message so a 429 arms the
349+
// admission gate for subsequent relay sends.
350+
let msg = crate::relay::relay_error_message(resp).await;
351+
eprintln!("buzz-desktop: STT kind:9 post failed: {msg}");
345352
}
346353
Err(e) => {
347354
eprintln!("buzz-desktop: STT kind:9 post failed: {e}");

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod nostr_convert;
2323
mod prevent_sleep;
2424
mod ptt_shortcut;
2525
mod relay;
26+
mod relay_admission;
2627
mod reset;
2728
mod secret_store;
2829
mod shutdown;

‎desktop/src-tauri/src/relay.rs‎

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ pub(crate) async fn parse_json_response<T: DeserializeOwned>(
231231
.map_err(|_| MALFORMED_RESPONSE_MESSAGE.to_string())
232232
}
233233

234+
/// Extract the `retry in Ns` hint from a rate-limit error string.
235+
///
236+
/// Matches the canonical format emitted by the relay in both HTTP 429 bodies
237+
/// and CLOSED/NOTICE messages: `quota exceeded; retry in 4s`.
238+
fn extract_retry_in_hint(body: &str) -> Option<u64> {
239+
let re_match = body.find("retry in ")?;
240+
let after = &body[re_match + "retry in ".len()..];
241+
let digits: String = after.chars().take_while(|c| c.is_ascii_digit()).collect();
242+
digits.parse::<u64>().ok()
243+
}
244+
234245
pub async fn relay_error_message(response: reqwest::Response) -> String {
235246
let status = response.status();
236247

@@ -250,6 +261,27 @@ pub async fn relay_error_message(response: reqwest::Response) -> String {
250261
// Real relay error: extract the structured message field if available.
251262
let body = response.text().await.unwrap_or_default();
252263

264+
// 429 Too Many Requests → typed `relay rate-limited:` prefix so the TS
265+
// client can activate the rate-limit gate without confusing it with a
266+
// connectivity failure (`relay unreachable:`). Also arm the Rust-side
267+
// admission gate here — the one place every relay HTTP error funnels
268+
// through — so the next relay-backed command waits out the quota window
269+
// instead of burning it (see `relay_admission`).
270+
if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
271+
let hint = extract_retry_in_hint(&body);
272+
// Clamp the hint to MAX_HINT_SECONDS before arming the Rust gate AND
273+
// before embedding it in the returned string. Every consumer (Rust gate
274+
// via `activate_rate_limit` and TS gate via `applyTauriRateLimitIfNeeded`)
275+
// must see the same capped value — a single policy point prevents the TS
276+
// gate from receiving an uncapped hint from an untrusted relay.
277+
let capped_hint = hint.map(|s| s.min(crate::relay_admission::MAX_HINT_SECONDS));
278+
crate::relay_admission::activate_rate_limit(capped_hint);
279+
if let Some(secs) = capped_hint {
280+
return format!("relay rate-limited: retry in {secs}s");
281+
}
282+
return "relay rate-limited: quota exceeded".to_string();
283+
}
284+
253285
if let Ok(value) = serde_json::from_str::<serde_json::Value>(&body) {
254286
if let Some(message) = value.get("message").and_then(serde_json::Value::as_str) {
255287
return format!("relay returned {status}: {message}");
@@ -286,6 +318,7 @@ pub async fn query_relay_at(
286318
api_base_url: &str,
287319
filters: &[serde_json::Value],
288320
) -> Result<Vec<nostr::Event>, String> {
321+
crate::relay_admission::wait_for_rate_limit().await;
289322
let url = format!("{}/query", api_base_url);
290323
let body_bytes =
291324
serde_json::to_vec(filters).map_err(|e| format!("filter serialization failed: {e}"))?;
@@ -382,6 +415,7 @@ pub async fn sync_managed_agent_profile(
382415
avatar_url: Option<&str>,
383416
auth_tag: Option<&str>, // NIP-OA auth tag JSON
384417
) -> Result<(), String> {
418+
crate::relay_admission::wait_for_rate_limit().await;
385419
// Build a signed kind:0 profile event (with optional NIP-OA auth tag).
386420
let event = build_profile_event(agent_keys, display_name, avatar_url, auth_tag)?;
387421
let event_json = event.as_json();
@@ -482,6 +516,7 @@ pub async fn submit_event(
482516
builder: nostr::EventBuilder,
483517
state: &AppState,
484518
) -> Result<SubmitEventResponse, String> {
519+
crate::relay_admission::wait_for_rate_limit().await;
485520
// All synchronous work (signing) must complete before any .await
486521
// so the MutexGuard is dropped and the future remains Send.
487522
let url = format!("{}/events", relay_api_base_url_with_override(state));
@@ -529,6 +564,7 @@ pub async fn submit_signed_event(
529564
event: &nostr::Event,
530565
state: &AppState,
531566
) -> Result<SubmitEventResponse, String> {
567+
crate::relay_admission::wait_for_rate_limit().await;
532568
let url = format!("{}/events", relay_api_base_url_with_override(state));
533569
let body_bytes = event.as_json().into_bytes();
534570
let auth_header = {
@@ -586,6 +622,7 @@ pub async fn submit_signed_event_with_keys(
586622
if event.pubkey != keys.public_key() {
587623
return Err("signed event does not match the publishing identity".to_string());
588624
}
625+
crate::relay_admission::wait_for_rate_limit().await;
589626
let url = format!("{}/events", relay_api_base_url_with_override(state));
590627
let body_bytes = event.as_json().into_bytes();
591628
let auth_header = build_nip98_auth_header_for_keys(keys, &Method::POST, &url, &body_bytes)?;
@@ -624,10 +661,106 @@ pub async fn submit_signed_event_with_keys(
624661
mod tests {
625662
use super::{
626663
build_profile_event, classify_intercepted_response, effective_agent_relay_url,
627-
parse_command_response, relay_http_base_url, MALFORMED_RESPONSE_MESSAGE,
664+
extract_retry_in_hint, parse_command_response, relay_http_base_url,
665+
MALFORMED_RESPONSE_MESSAGE,
628666
};
629667
use serde::Deserialize;
630668

669+
// ── extract_retry_in_hint ────────────────────────────────────────────────
670+
671+
#[test]
672+
fn extracts_hint_from_429_body() {
673+
assert_eq!(
674+
extract_retry_in_hint(r#"{"error":"rate-limited: quota exceeded; retry in 4s"}"#),
675+
Some(4)
676+
);
677+
}
678+
679+
#[test]
680+
fn extracts_hint_when_no_json_wrapper() {
681+
assert_eq!(extract_retry_in_hint("retry in 30s"), Some(30));
682+
}
683+
684+
#[test]
685+
fn returns_none_when_no_hint_present() {
686+
assert_eq!(
687+
extract_retry_in_hint(r#"{"error":"rate-limited: quota exceeded"}"#),
688+
None
689+
);
690+
assert_eq!(extract_retry_in_hint(""), None);
691+
}
692+
693+
#[test]
694+
fn overlong_digit_string_returns_none() {
695+
// A digit sequence that exceeds u64::MAX cannot be parsed; the function
696+
// must return None (→ caller uses the default) rather than panicking.
697+
assert_eq!(
698+
extract_retry_in_hint("retry in 99999999999999999999999s"),
699+
None
700+
);
701+
}
702+
703+
// ── relay_error_message: hint capping ────────────────────────────────────
704+
//
705+
// Verify that an oversized relay hint is capped in the returned message
706+
// string, not just inside `activate_rate_limit()`. This guarantees every
707+
// consumer — including the TS gate via `applyTauriRateLimitIfNeeded` —
708+
// receives the capped value rather than the raw untrusted relay value.
709+
710+
#[tokio::test]
711+
async fn oversized_hint_is_capped_in_relay_error_message_string() {
712+
use crate::relay_admission::MAX_HINT_SECONDS;
713+
use std::io::{Read as _, Write as _};
714+
715+
// Use a std::net listener on a std::thread — the same pattern as the
716+
// relay_admission loopback tests. This avoids two races that cause CI
717+
// failures with tokio::net + into_std():
718+
// 1. No request read: the client is still sending when the response
719+
// arrives → hyper `UnexpectedMessage`/`Canceled` under load.
720+
// 2. into_std() leaves the socket in nonblocking mode → write_all
721+
// may return WouldBlock and silently drop the response.
722+
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
723+
let addr = listener.local_addr().unwrap();
724+
725+
// Serve a 429 with a hint far exceeding MAX_HINT_SECONDS (300).
726+
let oversized = 1_000_000u64;
727+
let body = format!(r#"{{"error":"rate-limited: quota exceeded; retry in {oversized}s"}}"#);
728+
let body_len = body.len();
729+
std::thread::spawn(move || {
730+
if let Ok((mut stream, _)) = listener.accept() {
731+
// Read the request first so the client finishes sending before
732+
// we write the response — mirrors relay_admission.rs pattern.
733+
let mut buf = [0u8; 4096];
734+
let _ = stream.read(&mut buf);
735+
let response = format!(
736+
"HTTP/1.1 429 Too Many Requests\r\nContent-Type: application/json\r\nContent-Length: {body_len}\r\nConnection: close\r\n\r\n{body}"
737+
);
738+
let _ = stream.write_all(response.as_bytes());
739+
let _ = stream.flush();
740+
}
741+
});
742+
743+
let client = reqwest::Client::new();
744+
let response = client
745+
.get(format!("http://{addr}/"))
746+
.send()
747+
.await
748+
.expect("request must succeed");
749+
750+
let msg = super::relay_error_message(response).await;
751+
752+
// The message must embed the CAPPED hint, not the raw 1 000 000.
753+
assert_eq!(
754+
msg,
755+
format!("relay rate-limited: retry in {MAX_HINT_SECONDS}s"),
756+
"relay_error_message must embed the capped hint, not the raw untrusted value"
757+
);
758+
assert!(
759+
!msg.contains(&oversized.to_string()),
760+
"raw oversized hint must not appear in the message string"
761+
);
762+
}
763+
631764
// ── effective_agent_relay_url: per-agent override precedence ─────────────
632765

633766
#[test]

0 commit comments

Comments
 (0)