Skip to content

Commit 40220d5

Browse files
matt2eclaude
andauthored
perf(desktop): publish mention sends before waking agents (#7154)
## Summary Cuts perceived agent-mention send latency by publishing the message first and waking the agent afterwards, instead of blocking the send on a synchronous agent start/deploy round-trip. A send that mentions a stopped or undeployed managed agent now shows the message immediately; the wake runs fire-and-forget after the relay accepts the publish. The already-running-agent send also gets faster via revalidation dedupe and NIP-11 caching. ## Changes ### Publish-first agent wake - Wakes for mentioned managed agents are collected during send preparation and flushed fire-and-forget only after `await send(...)` resolves. No start can fire — and no "your message was sent" toast can appear — for a message the relay never accepted; every abort path (cancel, readiness error, publish rejection, dismissed non-member prompt) simply drops the queue. Persona-create wakes ride the pending draft behind the non-member prompt for the same reason. - Each wake is bound to the tenant scope captured at send time: the new `useDetachedAgentStart` hook passes `expectedRelayUrl` + `expectedSignerPubkey` with every start, so a wake that outlives a community switch fails closed at the backend instead of spawning against the new tenant. A wake whose scope has not resolved yet (identity query still loading, blank stored relay URL) is refused with a recoverable toast rather than fired unscoped. - In-flight wakes are deduped through a module-level map keyed by `(relay URL, pubkey)` — the same tenant pair the backend keys on — so two quick sends or two composers cannot double-spawn a cold agent during the seconds-long start window. Entries are deliberately retained across community switches (the key *is* the tenant scope, so a retained entry can never affect another community, and clearing it let an A→B→A round trip deploy a provider agent twice) and self-clean when the start settles. - Wake-failure toasts are fenced to the community they fired in via a module-level scope mirror: a start that settles after a community switch logs instead of rendering community A's failure over community B's UI, and an A→B→A return re-delivers the warning where it is actionable. - Membership attach and access-policy writes stay synchronous, so the harness's first kind-39002 read still sees the channel. ### Replay floor - The send timestamp travels with the wake as `BUZZ_ACP_REPLAY_FLOOR`, threaded through both local spawns (`spawn_agent_child`) and provider deploys (`deploy_to_provider` injects it into `launch.policy_env`), so the harness's startup watermark replays back past the just-published triggering message no matter how long the spawn takes. `buzz-acp` clamps the floor to `[now − 15min, now]`. - The floor is captured at enqueue time, not flush time — the flush runs post-publish, so a flush-time stamp could exceed the message's `created_at` and skip the very message the floor exists to cover. - On local spawns the caller's floor is asserted *after* the user env layering (and the ambient parent-process value is stripped unconditionally), so a saved persona/global/agent env entry cannot shadow this send's floor — mirroring the shadow-strip the provider path applies to `launch.env`. Both halves share one `REPLAY_FLOOR_ENV_VAR` const. ### Send-path latency reductions (already-running agents) - Mention revalidation is deduped: the publish-boundary pass reuses the pre-side-effect authorization pass unless an awaited round-trip actually separated the two (background upload, link-preview settlement, DM expansion, a real access-policy/membership write, or active-huddle enrollment). This preserves the #5681 authorization boundary while making the common send single-pass. - NIP-11 `self` lookups are cached per relay URL for 5 minutes. Only verified values are cached — non-2xx and malformed responses stay retryable — and URL keying keeps community switches from serving another relay's identity. - `applyReusableAgentAccessPolicy` now reports its relay write explicitly (`{ agent, wrote }`) instead of signalling through object identity, so the revalidation trigger above is load-bearing by construction. ### File splits Four files crossed the repository file-size ratchet during this work; one cohesive unit was extracted from each rather than raising a ceiling — `runtime/setup_payload.rs`, `commands/agents_create_fields.rs`, `app_state_accessors.rs`, and `useEnsureAgentMentionsReady.ts`. The ratchet is green at the tip. ### Review follow-ups The three concrete findings from the first review round are fixed at the tip: the pre-publish wake and its false "your message was sent" toast (fixed by queueing wakes behind the publish), the stale cross-community failure toast (fixed by the scope-mirror fence), and the A→B→A duplicate provider deploy (fixed by retaining the tenant-keyed in-flight entries across switches). The fast-path admission-staleness point is answered in the review thread: deferred paths already re-validate at the publish boundary, and the remaining fast-path window is milliseconds against an irreducible network-transit race. Mid-branch send-perf instrumentation was added to attribute the residual spinner latency and reverted once that analysis concluded — it is net-zero in this diff. ### Deferred follow-ups Durable mention catch-up via `event_mentions` (option 2 step 3) and backend deploy-epoch coalescing for the wake paths that do not funnel through `useDetachedAgentStart` (Agents-panel Start, restore, inbound-persona deploys) are intentionally left for separate changes. ## Testing - `cargo test --lib` on desktop/src-tauri: 3054 passed; clippy `-D warnings` + fmt clean - Desktop unit tests: 5856 passed (the 5 failures are the pre-existing `inboxReopenNavigation` / `useRetainedProjectGitViews` baseline, present on origin/main); `tsc --noEmit` and biome clean - Full mentions (87), channels (89), and community-rail (25) Playwright smoke suites against `pnpm build:e2e` bundles, with 3× stress reruns of each new spec - The load-bearing regression specs were confirmed red on the pre-fix code: publish-failure → zero starts and no false toast, the dedupe hold (1 call vs 2), the fail-closed scope refusal, the rail-switch toast fence, and the A→B→A retention spec (1 deploy vs 2) - New unit coverage pins the queue contract (enqueue-time floors, attach-seam queueing), the scope capture and verbatim relay-URL handoff, the dedupe map's keying and settle-then-repermit behavior, the unscoped refusal, the toast-scope mirror, the `{ agent, wrote }` contract, and the replay-floor env layering on both spawn paths 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Matt Toohey <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 5073e07 commit 40220d5

32 files changed

Lines changed: 3786 additions & 458 deletions

‎crates/buzz-acp/src/config.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,15 @@ pub struct CliArgs {
516516
/// Requires `--lazy-pool`; ignored otherwise. 0 disables idle re-sleep.
517517
#[arg(long, env = "BUZZ_ACP_IDLE_POOL_SLEEP", default_value_t = 0)]
518518
pub idle_pool_sleep: u64,
519+
520+
/// Unix-seconds replay floor for the startup watermark. A publish-first
521+
/// mention send publishes the triggering message and then spawns this
522+
/// harness, passing the send timestamp here so the first REQ replays past
523+
/// that message however long the spawn takes. Floors older than 15 minutes
524+
/// are clamped to 15 minutes before startup; floors in the future are
525+
/// ignored (the watermark stays at startup time).
526+
#[arg(long, env = "BUZZ_ACP_REPLAY_FLOOR")]
527+
pub replay_floor: Option<u64>,
519528
}
520529

521530
/// Merged NIP-01 subscription filter for a single channel.
@@ -605,6 +614,12 @@ pub struct Config {
605614
/// woken lazy pool is torn back down to the empty-slot state. 0 = disabled.
606615
/// Only meaningful when `lazy_pool` is true.
607616
pub idle_pool_sleep_secs: u64,
617+
/// Optional unix-seconds replay floor for the startup watermark
618+
/// (`--replay-floor` / `BUZZ_ACP_REPLAY_FLOOR`), set by a publish-first
619+
/// mention send so the first REQ replays past the already-published
620+
/// triggering message. Clamped where consumed — see
621+
/// `startup_watermark_with_floor`.
622+
pub replay_floor_unix: Option<u64>,
608623
/// Agent owner pubkey (hex). Used for `--respond-to=owner-only` gate.
609624
/// Replaces the old REST-based owner lookup.
610625
pub agent_owner: Option<String>,
@@ -1185,6 +1200,7 @@ impl Config {
11851200
exit_after_inactivity_secs: args.exit_after_inactivity,
11861201
lazy_pool: args.lazy_pool,
11871202
idle_pool_sleep_secs: args.idle_pool_sleep,
1203+
replay_floor_unix: args.replay_floor,
11881204
agent_owner: args.agent_owner.map(|s| s.trim().to_ascii_lowercase()),
11891205
no_base_prompt: args.no_base_prompt,
11901206
base_prompt_content,
@@ -1560,6 +1576,7 @@ mod tests {
15601576
exit_after_inactivity_secs: 0,
15611577
lazy_pool: false,
15621578
idle_pool_sleep_secs: 0,
1579+
replay_floor_unix: None,
15631580
agent_owner: None,
15641581
no_base_prompt: false,
15651582
base_prompt_content: None,

‎crates/buzz-acp/src/lib.rs‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,63 @@ mod idle_pool_sleep_tests {
23872387
}
23882388
}
23892389

2390+
/// Oldest a caller-supplied replay floor may reach back from startup. Bounds
2391+
/// the stale-event burst when a spawn request sat around (e.g. the desktop
2392+
/// slept between the send and this spawn actually running).
2393+
const REPLAY_FLOOR_MAX_AGE_SECS: u64 = 15 * 60;
2394+
2395+
/// Resolve the startup watermark from process-start time and an optional
2396+
/// replay floor (`--replay-floor` / `BUZZ_ACP_REPLAY_FLOOR`).
2397+
///
2398+
/// A publish-first mention send publishes the triggering message BEFORE this
2399+
/// harness spawns, so the watermark must reach back to the send timestamp for
2400+
/// the first REQ (`since = watermark − 5s`) to replay that message. Floors
2401+
/// older than [`REPLAY_FLOOR_MAX_AGE_SECS`] clamp to that bound; floors in
2402+
/// the future clamp to `now` (a skewed sender must not push the watermark
2403+
/// forward past startup and re-open the blind spot the watermark closes).
2404+
fn startup_watermark_with_floor(now_unix: u64, replay_floor: Option<u64>) -> u64 {
2405+
match replay_floor {
2406+
Some(floor) => floor.clamp(now_unix.saturating_sub(REPLAY_FLOOR_MAX_AGE_SECS), now_unix),
2407+
None => now_unix,
2408+
}
2409+
}
2410+
2411+
#[cfg(test)]
2412+
mod replay_floor_tests {
2413+
use super::{startup_watermark_with_floor, REPLAY_FLOOR_MAX_AGE_SECS};
2414+
2415+
const NOW: u64 = 1_700_000_000;
2416+
2417+
#[test]
2418+
fn no_floor_keeps_startup_time() {
2419+
assert_eq!(startup_watermark_with_floor(NOW, None), NOW);
2420+
}
2421+
2422+
#[test]
2423+
fn recent_floor_moves_watermark_back_to_the_send_timestamp() {
2424+
// The publish-first case: message sent 4s before the harness booted.
2425+
assert_eq!(startup_watermark_with_floor(NOW, Some(NOW - 4)), NOW - 4);
2426+
}
2427+
2428+
#[test]
2429+
fn stale_floor_clamps_to_the_max_age_bound() {
2430+
assert_eq!(
2431+
startup_watermark_with_floor(NOW, Some(NOW - REPLAY_FLOOR_MAX_AGE_SECS - 1)),
2432+
NOW - REPLAY_FLOOR_MAX_AGE_SECS
2433+
);
2434+
}
2435+
2436+
#[test]
2437+
fn future_floor_is_ignored() {
2438+
assert_eq!(startup_watermark_with_floor(NOW, Some(NOW + 60)), NOW);
2439+
}
2440+
2441+
#[test]
2442+
fn early_epoch_now_does_not_underflow() {
2443+
assert_eq!(startup_watermark_with_floor(10, Some(0)), 0);
2444+
}
2445+
}
2446+
23902447
pub fn run() -> Result<()> {
23912448
config::propagate_legacy_env_vars();
23922449
tokio_main()
@@ -2484,10 +2541,24 @@ async fn tokio_main() -> Result<()> {
24842541
// the initial subscribe_since for channels discovered at startup. The Subscribe
24852542
// handler falls back to subscribe_since when last_seen is None, closing the
24862543
// blind spot between "agents ready" and "first REQ sent".
2487-
let startup_watermark: u64 = std::time::SystemTime::now()
2544+
//
2545+
// A publish-first mention send passes the triggering message's send
2546+
// timestamp as a replay floor (`--replay-floor` / `BUZZ_ACP_REPLAY_FLOOR`):
2547+
// the message is already on the relay when this process spawns, so the
2548+
// watermark must reach back to it for the first REQ to replay it — however
2549+
// long the spawn took.
2550+
let now_unix: u64 = std::time::SystemTime::now()
24882551
.duration_since(std::time::UNIX_EPOCH)
24892552
.unwrap_or_default()
24902553
.as_secs();
2554+
let startup_watermark = startup_watermark_with_floor(now_unix, config.replay_floor_unix);
2555+
if let Some(floor) = config.replay_floor_unix {
2556+
tracing::info!(
2557+
floor,
2558+
startup_watermark,
2559+
"applying replay floor to startup watermark"
2560+
);
2561+
}
24912562

24922563
let pubkey_hex = config.keys.public_key().to_hex();
24932564

@@ -8862,6 +8933,7 @@ mod build_mcp_servers_tests {
88628933
exit_after_inactivity_secs: 0,
88638934
lazy_pool: false,
88648935
idle_pool_sleep_secs: 0,
8936+
replay_floor_unix: None,
88658937
agent_owner: None,
88668938
no_base_prompt: false,
88678939
base_prompt_content: None,
@@ -9087,6 +9159,7 @@ mod error_outcome_emission_tests {
90879159
exit_after_inactivity_secs: 0,
90889160
lazy_pool: false,
90899161
idle_pool_sleep_secs: 0,
9162+
replay_floor_unix: None,
90909163
agent_owner: None,
90919164
no_base_prompt: false,
90929165
base_prompt_content: None,

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

Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ pub struct AppState {
129129
/// bounded and letting a later leave correctly flip the channel back to
130130
/// `is_member=false`.
131131
pub pending_owned_channels: Mutex<std::collections::HashSet<(String, String)>>,
132+
/// NIP-11 `self` pubkeys keyed by relay WS URL, each with its fetch
133+
/// instant. A relay's signing identity is effectively static, yet every
134+
/// send-time agent revalidation used to re-GET the document — one of the
135+
/// dominant costs of agent-mention send latency. Entries expire after
136+
/// `identity_archive::RELAY_SELF_CACHE_TTL` so a relay-side key rotation
137+
/// still converges. Keyed by URL, so switching communities can never serve
138+
/// another relay's identity; only verified `Some` values are stored (an
139+
/// outage or a document without `self` must stay retryable).
140+
pub relay_self_cache: Mutex<HashMap<String, (std::time::Instant, String)>>,
132141
pub archive_db: crate::archive::ArchiveDb,
133142
}
134143

@@ -231,86 +240,13 @@ pub fn build_app_state() -> AppState {
231240
#[cfg(feature = "mesh-llm")]
232241
mesh_coordinator: AsyncMutex::new(None),
233242
pending_owned_channels: Mutex::new(std::collections::HashSet::new()),
243+
relay_self_cache: Mutex::new(HashMap::new()),
234244
archive_db: crate::archive::ArchiveDb::default(),
235245
}
236246
}
237247

238-
impl AppState {
239-
/// Lock the huddle state mutex, converting a poisoned-lock error to a String.
240-
///
241-
/// Convenience wrapper — replaces 15+ instances of
242-
/// `state.huddle_state.lock().map_err(|e| e.to_string())?` throughout the
243-
/// huddle module.
244-
pub fn huddle(&self) -> Result<std::sync::MutexGuard<'_, crate::huddle::HuddleState>, String> {
245-
self.huddle_state.lock().map_err(|e| e.to_string())
246-
}
247-
248-
pub fn get_session_cache(&self, key: &ManagedAgentRuntimeKey) -> Option<SessionConfigCache> {
249-
self.session_config_cache.lock().ok()?.get(key).cloned()
250-
}
251-
252-
pub fn put_session_cache(&self, key: ManagedAgentRuntimeKey, cache: SessionConfigCache) {
253-
if let Ok(mut map) = self.session_config_cache.lock() {
254-
map.insert(key, cache);
255-
}
256-
}
257-
258-
pub fn clear_agent_session_cache(&self, key: &ManagedAgentRuntimeKey) {
259-
if let Ok(mut map) = self.session_config_cache.lock() {
260-
map.remove(key);
261-
}
262-
}
263-
264-
pub fn clear_agent_session_caches(&self, pubkey: &str) {
265-
if let Ok(mut map) = self.session_config_cache.lock() {
266-
map.retain(|key, _| key.pubkey != pubkey);
267-
}
268-
}
269-
270-
/// Return the active identity keys if they are in a signable state.
271-
///
272-
/// Returns `Err` when the identity is in a lost state (`identity_lost`
273-
/// — ephemeral key, user must re-import their nsec) or when the keyring
274-
/// is locked (`keyring_locked` — key is held in a keyring that is
275-
/// unavailable this boot). All signing and publish commands must call
276-
/// this instead of locking `state.keys` directly, so that recovery mode
277-
/// blocks publishing under an invalid or inaccessible identity.
278-
pub fn signing_keys(&self) -> Result<Keys, String> {
279-
if self
280-
.identity_lost
281-
.load(std::sync::atomic::Ordering::Acquire)
282-
|| self
283-
.keyring_locked
284-
.load(std::sync::atomic::Ordering::Acquire)
285-
{
286-
return Err("identity is in recovery mode; event signing is disabled \
287-
until the identity is restored and Buzz is relaunched"
288-
.to_string());
289-
}
290-
self.keys
291-
.lock()
292-
.map_err(|e| e.to_string())
293-
.map(|k| k.clone())
294-
}
295-
296-
/// Emit the current huddle state to the frontend via Tauri event.
297-
///
298-
/// Acquires both locks (app_handle + huddle_state), clones a snapshot,
299-
/// releases both, then emits. Best-effort — no-op if either lock is
300-
/// poisoned or the app_handle hasn't been set yet.
301-
pub fn emit_huddle_state_changed(&self) {
302-
let app = match self.app_handle.lock() {
303-
Ok(guard) => guard.clone(),
304-
Err(_) => return,
305-
};
306-
let Some(app) = app else { return };
307-
let snapshot = match self.huddle_state.lock() {
308-
Ok(hs) => hs.clone(),
309-
Err(_) => return,
310-
};
311-
crate::huddle::state::emit_huddle_state(&app, &snapshot);
312-
}
313-
}
248+
#[path = "app_state_accessors.rs"]
249+
mod accessors;
314250

315251
/// Resolve the user's identity key from the app data directory and wire
316252
/// the resulting [`RecoveryState`] into `AppState`.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//! Convenience accessors over [`AppState`]'s lock-guarded fields.
2+
//!
3+
//! Kept apart from `app_state.rs`, which owns the struct, its builder, and the
4+
//! identity-key resolution that populates it.
5+
6+
use nostr::Keys;
7+
8+
use crate::app_state::AppState;
9+
use crate::managed_agents::config_bridge::SessionConfigCache;
10+
use crate::managed_agents::ManagedAgentRuntimeKey;
11+
12+
impl AppState {
13+
/// Lock the huddle state mutex, converting a poisoned-lock error to a String.
14+
///
15+
/// Convenience wrapper — replaces 15+ instances of
16+
/// `state.huddle_state.lock().map_err(|e| e.to_string())?` throughout the
17+
/// huddle module.
18+
pub fn huddle(&self) -> Result<std::sync::MutexGuard<'_, crate::huddle::HuddleState>, String> {
19+
self.huddle_state.lock().map_err(|e| e.to_string())
20+
}
21+
22+
pub fn get_session_cache(&self, key: &ManagedAgentRuntimeKey) -> Option<SessionConfigCache> {
23+
self.session_config_cache.lock().ok()?.get(key).cloned()
24+
}
25+
26+
pub fn put_session_cache(&self, key: ManagedAgentRuntimeKey, cache: SessionConfigCache) {
27+
if let Ok(mut map) = self.session_config_cache.lock() {
28+
map.insert(key, cache);
29+
}
30+
}
31+
32+
pub fn clear_agent_session_cache(&self, key: &ManagedAgentRuntimeKey) {
33+
if let Ok(mut map) = self.session_config_cache.lock() {
34+
map.remove(key);
35+
}
36+
}
37+
38+
pub fn clear_agent_session_caches(&self, pubkey: &str) {
39+
if let Ok(mut map) = self.session_config_cache.lock() {
40+
map.retain(|key, _| key.pubkey != pubkey);
41+
}
42+
}
43+
44+
/// Return the active identity keys if they are in a signable state.
45+
///
46+
/// Returns `Err` when the identity is in a lost state (`identity_lost`
47+
/// — ephemeral key, user must re-import their nsec) or when the keyring
48+
/// is locked (`keyring_locked` — key is held in a keyring that is
49+
/// unavailable this boot). All signing and publish commands must call
50+
/// this instead of locking `state.keys` directly, so that recovery mode
51+
/// blocks publishing under an invalid or inaccessible identity.
52+
pub fn signing_keys(&self) -> Result<Keys, String> {
53+
if self
54+
.identity_lost
55+
.load(std::sync::atomic::Ordering::Acquire)
56+
|| self
57+
.keyring_locked
58+
.load(std::sync::atomic::Ordering::Acquire)
59+
{
60+
return Err("identity is in recovery mode; event signing is disabled \
61+
until the identity is restored and Buzz is relaunched"
62+
.to_string());
63+
}
64+
self.keys
65+
.lock()
66+
.map_err(|e| e.to_string())
67+
.map(|k| k.clone())
68+
}
69+
70+
/// Emit the current huddle state to the frontend via Tauri event.
71+
///
72+
/// Acquires both locks (app_handle + huddle_state), clones a snapshot,
73+
/// releases both, then emits. Best-effort — no-op if either lock is
74+
/// poisoned or the app_handle hasn't been set yet.
75+
pub fn emit_huddle_state_changed(&self) {
76+
let app = match self.app_handle.lock() {
77+
Ok(guard) => guard.clone(),
78+
Err(_) => return,
79+
};
80+
let Some(app) = app else { return };
81+
let snapshot = match self.huddle_state.lock() {
82+
Ok(hs) => hs.clone(),
83+
Err(_) => return,
84+
};
85+
crate::huddle::state::emit_huddle_state(&app, &snapshot);
86+
}
87+
}

0 commit comments

Comments
 (0)